Skip to content

Commit 67fa83f

Browse files
0x7f454c46davem330
authored andcommitted
net/tcp: Add static_key for TCP-AO
Similarly to TCP-MD5, add a static key to TCP-AO that is patched out when there are no keys on a machine and dynamically enabled with the first setsockopt(TCP_AO) adds a key on any socket. The static key is as well dynamically disabled later when the socket is destructed. The lifetime of enabled static key here is the same as ao_info: it is enabled on allocation, passed over from full socket to twsk and destructed when ao_info is scheduled for destruction. Signed-off-by: Dmitry Safonov <[email protected]> Acked-by: David Ahern <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent d6732b9 commit 67fa83f

File tree

6 files changed

+96
-44
lines changed

6 files changed

+96
-44
lines changed

include/net/tcp.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,14 +2288,18 @@ static inline void tcp_get_current_key(const struct sock *sk,
22882288
#if defined(CONFIG_TCP_AO) || defined(CONFIG_TCP_MD5SIG)
22892289
const struct tcp_sock *tp = tcp_sk(sk);
22902290
#endif
2291-
#ifdef CONFIG_TCP_AO
2292-
struct tcp_ao_info *ao;
22932291

2294-
ao = rcu_dereference_protected(tp->ao_info, lockdep_sock_is_held(sk));
2295-
if (ao) {
2296-
out->ao_key = READ_ONCE(ao->current_key);
2297-
out->type = TCP_KEY_AO;
2298-
return;
2292+
#ifdef CONFIG_TCP_AO
2293+
if (static_branch_unlikely(&tcp_ao_needed.key)) {
2294+
struct tcp_ao_info *ao;
2295+
2296+
ao = rcu_dereference_protected(tp->ao_info,
2297+
lockdep_sock_is_held(sk));
2298+
if (ao) {
2299+
out->ao_key = READ_ONCE(ao->current_key);
2300+
out->type = TCP_KEY_AO;
2301+
return;
2302+
}
22992303
}
23002304
#endif
23012305
#ifdef CONFIG_TCP_MD5SIG
@@ -2324,7 +2328,8 @@ static inline bool tcp_key_is_md5(const struct tcp_key *key)
23242328
static inline bool tcp_key_is_ao(const struct tcp_key *key)
23252329
{
23262330
#ifdef CONFIG_TCP_AO
2327-
if (key->type == TCP_KEY_AO)
2331+
if (static_branch_unlikely(&tcp_ao_needed.key) &&
2332+
key->type == TCP_KEY_AO)
23282333
return true;
23292334
#endif
23302335
return false;
@@ -2718,6 +2723,9 @@ static inline bool tcp_ao_required(struct sock *sk, const void *saddr,
27182723
struct tcp_ao_info *ao_info;
27192724
struct tcp_ao_key *ao_key;
27202725

2726+
if (!static_branch_unlikely(&tcp_ao_needed.key))
2727+
return false;
2728+
27212729
ao_info = rcu_dereference_check(tcp_sk(sk)->ao_info,
27222730
lockdep_sock_is_held(sk));
27232731
if (!ao_info)

include/net/tcp_ao.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ do { \
151151

152152
#ifdef CONFIG_TCP_AO
153153
/* TCP-AO structures and functions */
154+
#include <linux/jump_label.h>
155+
extern struct static_key_false_deferred tcp_ao_needed;
154156

155157
struct tcp4_ao_context {
156158
__be32 saddr;

net/ipv4/tcp_ao.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <net/ipv6.h>
1818
#include <net/icmp.h>
1919

20+
DEFINE_STATIC_KEY_DEFERRED_FALSE(tcp_ao_needed, HZ);
21+
2022
int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8 *key, void *ctx,
2123
unsigned int len, struct tcp_sigpool *hp)
2224
{
@@ -50,6 +52,9 @@ bool tcp_ao_ignore_icmp(const struct sock *sk, int family, int type, int code)
5052
bool ignore_icmp = false;
5153
struct tcp_ao_info *ao;
5254

55+
if (!static_branch_unlikely(&tcp_ao_needed.key))
56+
return false;
57+
5358
/* RFC5925, 7.8:
5459
* >> A TCP-AO implementation MUST default to ignore incoming ICMPv4
5560
* messages of Type 3 (destination unreachable), Codes 2-4 (protocol
@@ -185,6 +190,9 @@ static struct tcp_ao_key *__tcp_ao_do_lookup(const struct sock *sk,
185190
struct tcp_ao_key *key;
186191
struct tcp_ao_info *ao;
187192

193+
if (!static_branch_unlikely(&tcp_ao_needed.key))
194+
return NULL;
195+
188196
ao = rcu_dereference_check(tcp_sk(sk)->ao_info,
189197
lockdep_sock_is_held(sk));
190198
if (!ao)
@@ -276,6 +284,7 @@ void tcp_ao_destroy_sock(struct sock *sk, bool twsk)
276284
}
277285

278286
kfree_rcu(ao, rcu);
287+
static_branch_slow_dec_deferred(&tcp_ao_needed);
279288
}
280289

281290
void tcp_ao_time_wait(struct tcp_timewait_sock *tcptw, struct tcp_sock *tp)
@@ -1180,6 +1189,11 @@ int tcp_ao_copy_all_matching(const struct sock *sk, struct sock *newsk,
11801189
goto free_and_exit;
11811190
}
11821191

1192+
if (!static_key_fast_inc_not_disabled(&tcp_ao_needed.key.key)) {
1193+
ret = -EUSERS;
1194+
goto free_and_exit;
1195+
}
1196+
11831197
key_head = rcu_dereference(hlist_first_rcu(&new_ao->head));
11841198
first_key = hlist_entry_safe(key_head, struct tcp_ao_key, node);
11851199

@@ -1607,6 +1621,10 @@ static int tcp_ao_add_cmd(struct sock *sk, unsigned short int family,
16071621

16081622
tcp_ao_link_mkt(ao_info, key);
16091623
if (first) {
1624+
if (!static_branch_inc(&tcp_ao_needed.key)) {
1625+
ret = -EUSERS;
1626+
goto err_free_sock;
1627+
}
16101628
sk_gso_disable(sk);
16111629
rcu_assign_pointer(tcp_sk(sk)->ao_info, ao_info);
16121630
}
@@ -1875,6 +1893,10 @@ static int tcp_ao_info_cmd(struct sock *sk, unsigned short int family,
18751893
if (new_rnext)
18761894
WRITE_ONCE(ao_info->rnext_key, new_rnext);
18771895
if (first) {
1896+
if (!static_branch_inc(&tcp_ao_needed.key)) {
1897+
err = -EUSERS;
1898+
goto out;
1899+
}
18781900
sk_gso_disable(sk);
18791901
rcu_assign_pointer(tcp_sk(sk)->ao_info, ao_info);
18801902
}

net/ipv4/tcp_input.c

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3572,41 +3572,55 @@ static inline bool tcp_may_update_window(const struct tcp_sock *tp,
35723572
(ack_seq == tp->snd_wl1 && (nwin > tp->snd_wnd || !nwin));
35733573
}
35743574

3575-
/* If we update tp->snd_una, also update tp->bytes_acked */
3576-
static void tcp_snd_una_update(struct tcp_sock *tp, u32 ack)
3575+
static void tcp_snd_sne_update(struct tcp_sock *tp, u32 ack)
35773576
{
3578-
u32 delta = ack - tp->snd_una;
35793577
#ifdef CONFIG_TCP_AO
35803578
struct tcp_ao_info *ao;
3581-
#endif
35823579

3583-
sock_owned_by_me((struct sock *)tp);
3584-
tp->bytes_acked += delta;
3585-
#ifdef CONFIG_TCP_AO
3580+
if (!static_branch_unlikely(&tcp_ao_needed.key))
3581+
return;
3582+
35863583
ao = rcu_dereference_protected(tp->ao_info,
35873584
lockdep_sock_is_held((struct sock *)tp));
35883585
if (ao && ack < tp->snd_una)
35893586
ao->snd_sne++;
35903587
#endif
3588+
}
3589+
3590+
/* If we update tp->snd_una, also update tp->bytes_acked */
3591+
static void tcp_snd_una_update(struct tcp_sock *tp, u32 ack)
3592+
{
3593+
u32 delta = ack - tp->snd_una;
3594+
3595+
sock_owned_by_me((struct sock *)tp);
3596+
tp->bytes_acked += delta;
3597+
tcp_snd_sne_update(tp, ack);
35913598
tp->snd_una = ack;
35923599
}
35933600

3594-
/* If we update tp->rcv_nxt, also update tp->bytes_received */
3595-
static void tcp_rcv_nxt_update(struct tcp_sock *tp, u32 seq)
3601+
static void tcp_rcv_sne_update(struct tcp_sock *tp, u32 seq)
35963602
{
3597-
u32 delta = seq - tp->rcv_nxt;
35983603
#ifdef CONFIG_TCP_AO
35993604
struct tcp_ao_info *ao;
3600-
#endif
36013605

3602-
sock_owned_by_me((struct sock *)tp);
3603-
tp->bytes_received += delta;
3604-
#ifdef CONFIG_TCP_AO
3606+
if (!static_branch_unlikely(&tcp_ao_needed.key))
3607+
return;
3608+
36053609
ao = rcu_dereference_protected(tp->ao_info,
36063610
lockdep_sock_is_held((struct sock *)tp));
36073611
if (ao && seq < tp->rcv_nxt)
36083612
ao->rcv_sne++;
36093613
#endif
3614+
}
3615+
3616+
/* If we update tp->rcv_nxt, also update tp->bytes_received */
3617+
static void tcp_rcv_nxt_update(struct tcp_sock *tp, u32 seq)
3618+
{
3619+
u32 delta = seq - tp->rcv_nxt;
3620+
3621+
sock_owned_by_me((struct sock *)tp);
3622+
tp->bytes_received += delta;
3623+
tcp_rcv_sne_update(tp, seq);
36103624
WRITE_ONCE(tp->rcv_nxt, seq);
36113625
}
36123626

net/ipv4/tcp_ipv4.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,18 +1024,20 @@ static void tcp_v4_timewait_ack(struct sock *sk, struct sk_buff *skb)
10241024
#ifdef CONFIG_TCP_AO
10251025
struct tcp_ao_info *ao_info;
10261026

1027-
/* FIXME: the segment to-be-acked is not verified yet */
1028-
ao_info = rcu_dereference(tcptw->ao_info);
1029-
if (ao_info) {
1030-
const struct tcp_ao_hdr *aoh;
1027+
if (static_branch_unlikely(&tcp_ao_needed.key)) {
1028+
/* FIXME: the segment to-be-acked is not verified yet */
1029+
ao_info = rcu_dereference(tcptw->ao_info);
1030+
if (ao_info) {
1031+
const struct tcp_ao_hdr *aoh;
1032+
1033+
if (tcp_parse_auth_options(tcp_hdr(skb), NULL, &aoh)) {
1034+
inet_twsk_put(tw);
1035+
return;
1036+
}
10311037

1032-
if (tcp_parse_auth_options(tcp_hdr(skb), NULL, &aoh)) {
1033-
inet_twsk_put(tw);
1034-
return;
1038+
if (aoh)
1039+
key.ao_key = tcp_ao_established_key(ao_info, aoh->rnext_keyid, -1);
10351040
}
1036-
1037-
if (aoh)
1038-
key.ao_key = tcp_ao_established_key(ao_info, aoh->rnext_keyid, -1);
10391041
}
10401042
if (key.ao_key) {
10411043
struct tcp_ao_key *rnext_key;
@@ -1081,7 +1083,8 @@ static void tcp_v4_reqsk_send_ack(const struct sock *sk, struct sk_buff *skb,
10811083
tcp_sk(sk)->snd_nxt;
10821084

10831085
#ifdef CONFIG_TCP_AO
1084-
if (tcp_rsk_used_ao(req)) {
1086+
if (static_branch_unlikely(&tcp_ao_needed.key) &&
1087+
tcp_rsk_used_ao(req)) {
10851088
const union tcp_md5_addr *addr;
10861089
const struct tcp_ao_hdr *aoh;
10871090

net/ipv6/tcp_ipv6.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,17 +1154,19 @@ static void tcp_v6_timewait_ack(struct sock *sk, struct sk_buff *skb)
11541154
#ifdef CONFIG_TCP_AO
11551155
struct tcp_ao_info *ao_info;
11561156

1157-
/* FIXME: the segment to-be-acked is not verified yet */
1158-
ao_info = rcu_dereference(tcptw->ao_info);
1159-
if (ao_info) {
1160-
const struct tcp_ao_hdr *aoh;
1157+
if (static_branch_unlikely(&tcp_ao_needed.key)) {
11611158

1162-
/* Invalid TCP option size or twice included auth */
1163-
if (tcp_parse_auth_options(tcp_hdr(skb), NULL, &aoh))
1164-
goto out;
1165-
if (aoh) {
1166-
key.ao_key = tcp_ao_established_key(ao_info,
1167-
aoh->rnext_keyid, -1);
1159+
/* FIXME: the segment to-be-acked is not verified yet */
1160+
ao_info = rcu_dereference(tcptw->ao_info);
1161+
if (ao_info) {
1162+
const struct tcp_ao_hdr *aoh;
1163+
1164+
/* Invalid TCP option size or twice included auth */
1165+
if (tcp_parse_auth_options(tcp_hdr(skb), NULL, &aoh))
1166+
goto out;
1167+
if (aoh)
1168+
key.ao_key = tcp_ao_established_key(ao_info,
1169+
aoh->rnext_keyid, -1);
11681170
}
11691171
}
11701172
if (key.ao_key) {
@@ -1206,7 +1208,8 @@ static void tcp_v6_reqsk_send_ack(const struct sock *sk, struct sk_buff *skb,
12061208
struct tcp_key key = {};
12071209

12081210
#ifdef CONFIG_TCP_AO
1209-
if (tcp_rsk_used_ao(req)) {
1211+
if (static_branch_unlikely(&tcp_ao_needed.key) &&
1212+
tcp_rsk_used_ao(req)) {
12101213
const struct in6_addr *addr = &ipv6_hdr(skb)->saddr;
12111214
const struct tcp_ao_hdr *aoh;
12121215
int l3index;

0 commit comments

Comments
 (0)