1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * INET An implementation of the TCP Authentication Option (TCP-AO).
4 * See RFC5925.
5 *
6 * Authors: Dmitry Safonov <dima@arista.com>
7 * Francesco Ruggeri <fruggeri@arista.com>
8 * Salam Noureddine <noureddine@arista.com>
9 */
10 #define pr_fmt(fmt) "TCP: " fmt
11
12 #include <crypto/hash.h>
13 #include <linux/inetdevice.h>
14 #include <linux/tcp.h>
15
16 #include <net/tcp.h>
17 #include <net/ipv6.h>
18 #include <net/icmp.h>
19 #include <trace/events/tcp.h>
20
21 DEFINE_STATIC_KEY_DEFERRED_FALSE(tcp_ao_needed, HZ);
22
tcp_ao_calc_traffic_key(struct tcp_ao_key * mkt,u8 * key,void * ctx,unsigned int len,struct tcp_sigpool * hp)23 int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8 *key, void *ctx,
24 unsigned int len, struct tcp_sigpool *hp)
25 {
26 struct scatterlist sg;
27 int ret;
28
29 if (crypto_ahash_setkey(crypto_ahash_reqtfm(hp->req),
30 mkt->key, mkt->keylen))
31 goto clear_hash;
32
33 ret = crypto_ahash_init(hp->req);
34 if (ret)
35 goto clear_hash;
36
37 sg_init_one(&sg, ctx, len);
38 ahash_request_set_crypt(hp->req, &sg, key, len);
39 crypto_ahash_update(hp->req);
40
41 ret = crypto_ahash_final(hp->req);
42 if (ret)
43 goto clear_hash;
44
45 return 0;
46 clear_hash:
47 memset(key, 0, tcp_ao_digest_size(mkt));
48 return 1;
49 }
50
tcp_ao_ignore_icmp(const struct sock * sk,int family,int type,int code)51 bool tcp_ao_ignore_icmp(const struct sock *sk, int family, int type, int code)
52 {
53 bool ignore_icmp = false;
54 struct tcp_ao_info *ao;
55
56 if (!static_branch_unlikely(&tcp_ao_needed.key))
57 return false;
58
59 /* RFC5925, 7.8:
60 * >> A TCP-AO implementation MUST default to ignore incoming ICMPv4
61 * messages of Type 3 (destination unreachable), Codes 2-4 (protocol
62 * unreachable, port unreachable, and fragmentation needed -- ’hard
63 * errors’), and ICMPv6 Type 1 (destination unreachable), Code 1
64 * (administratively prohibited) and Code 4 (port unreachable) intended
65 * for connections in synchronized states (ESTABLISHED, FIN-WAIT-1, FIN-
66 * WAIT-2, CLOSE-WAIT, CLOSING, LAST-ACK, TIME-WAIT) that match MKTs.
67 */
68 if (family == AF_INET) {
69 if (type != ICMP_DEST_UNREACH)
70 return false;
71 if (code < ICMP_PROT_UNREACH || code > ICMP_FRAG_NEEDED)
72 return false;
73 } else {
74 if (type != ICMPV6_DEST_UNREACH)
75 return false;
76 if (code != ICMPV6_ADM_PROHIBITED && code != ICMPV6_PORT_UNREACH)
77 return false;
78 }
79
80 rcu_read_lock();
81 switch (sk->sk_state) {
82 case TCP_TIME_WAIT:
83 ao = rcu_dereference(tcp_twsk(sk)->ao_info);
84 break;
85 case TCP_SYN_SENT:
86 case TCP_SYN_RECV:
87 case TCP_LISTEN:
88 case TCP_NEW_SYN_RECV:
89 /* RFC5925 specifies to ignore ICMPs *only* on connections
90 * in synchronized states.
91 */
92 rcu_read_unlock();
93 return false;
94 default:
95 ao = rcu_dereference(tcp_sk(sk)->ao_info);
96 }
97
98 if (ao && !ao->accept_icmps) {
99 ignore_icmp = true;
100 __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAODROPPEDICMPS);
101 atomic64_inc(&ao->counters.dropped_icmp);
102 }
103 rcu_read_unlock();
104
105 return ignore_icmp;
106 }
107
108 /* Optimized version of tcp_ao_do_lookup(): only for sockets for which
109 * it's known that the keys in ao_info are matching peer's
110 * family/address/VRF/etc.
111 */
tcp_ao_established_key(const struct sock * sk,struct tcp_ao_info * ao,int sndid,int rcvid)112 struct tcp_ao_key *tcp_ao_established_key(const struct sock *sk,
113 struct tcp_ao_info *ao,
114 int sndid, int rcvid)
115 {
116 struct tcp_ao_key *key;
117
118 hlist_for_each_entry_rcu(key, &ao->head, node, lockdep_sock_is_held(sk)) {
119 if ((sndid >= 0 && key->sndid != sndid) ||
120 (rcvid >= 0 && key->rcvid != rcvid))
121 continue;
122 return key;
123 }
124
125 return NULL;
126 }
127
ipv4_prefix_cmp(const struct in_addr * addr1,const struct in_addr * addr2,unsigned int prefixlen)128 static int ipv4_prefix_cmp(const struct in_addr *addr1,
129 const struct in_addr *addr2,
130 unsigned int prefixlen)
131 {
132 __be32 mask = inet_make_mask(prefixlen);
133 __be32 a1 = addr1->s_addr & mask;
134 __be32 a2 = addr2->s_addr & mask;
135
136 if (a1 == a2)
137 return 0;
138 return memcmp(&a1, &a2, sizeof(a1));
139 }
140
__tcp_ao_key_cmp(const struct tcp_ao_key * key,int l3index,const union tcp_ao_addr * addr,u8 prefixlen,int family,int sndid,int rcvid)141 static int __tcp_ao_key_cmp(const struct tcp_ao_key *key, int l3index,
142 const union tcp_ao_addr *addr, u8 prefixlen,
143 int family, int sndid, int rcvid)
144 {
145 if (sndid >= 0 && key->sndid != sndid)
146 return (key->sndid > sndid) ? 1 : -1;
147 if (rcvid >= 0 && key->rcvid != rcvid)
148 return (key->rcvid > rcvid) ? 1 : -1;
149 if (l3index >= 0 && (key->keyflags & TCP_AO_KEYF_IFINDEX)) {
150 if (key->l3index != l3index)
151 return (key->l3index > l3index) ? 1 : -1;
152 }
153
154 if (family == AF_UNSPEC)
155 return 0;
156 if (key->family != family)
157 return (key->family > family) ? 1 : -1;
158
159 if (family == AF_INET) {
160 if (ntohl(key->addr.a4.s_addr) == INADDR_ANY)
161 return 0;
162 if (ntohl(addr->a4.s_addr) == INADDR_ANY)
163 return 0;
164 return ipv4_prefix_cmp(&key->addr.a4, &addr->a4, prefixlen);
165 #if IS_ENABLED(CONFIG_IPV6)
166 } else {
167 if (ipv6_addr_any(&key->addr.a6) || ipv6_addr_any(&addr->a6))
168 return 0;
169 if (ipv6_prefix_equal(&key->addr.a6, &addr->a6, prefixlen))
170 return 0;
171 return memcmp(&key->addr.a6, &addr->a6, sizeof(addr->a6));
172 #endif
173 }
174 return -1;
175 }
176
tcp_ao_key_cmp(const struct tcp_ao_key * key,int l3index,const union tcp_ao_addr * addr,u8 prefixlen,int family,int sndid,int rcvid)177 static int tcp_ao_key_cmp(const struct tcp_ao_key *key, int l3index,
178 const union tcp_ao_addr *addr, u8 prefixlen,
179 int family, int sndid, int rcvid)
180 {
181 #if IS_ENABLED(CONFIG_IPV6)
182 if (family == AF_INET6 && ipv6_addr_v4mapped(&addr->a6)) {
183 __be32 addr4 = addr->a6.s6_addr32[3];
184
185 return __tcp_ao_key_cmp(key, l3index,
186 (union tcp_ao_addr *)&addr4,
187 prefixlen, AF_INET, sndid, rcvid);
188 }
189 #endif
190 return __tcp_ao_key_cmp(key, l3index, addr,
191 prefixlen, family, sndid, rcvid);
192 }
193
__tcp_ao_do_lookup(const struct sock * sk,int l3index,const union tcp_ao_addr * addr,int family,u8 prefix,int sndid,int rcvid)194 static struct tcp_ao_key *__tcp_ao_do_lookup(const struct sock *sk, int l3index,
195 const union tcp_ao_addr *addr, int family, u8 prefix,
196 int sndid, int rcvid)
197 {
198 struct tcp_ao_key *key;
199 struct tcp_ao_info *ao;
200
201 if (!static_branch_unlikely(&tcp_ao_needed.key))
202 return NULL;
203
204 ao = rcu_dereference_check(tcp_sk(sk)->ao_info,
205 lockdep_sock_is_held(sk));
206 if (!ao)
207 return NULL;
208
209 hlist_for_each_entry_rcu(key, &ao->head, node, lockdep_sock_is_held(sk)) {
210 u8 prefixlen = min(prefix, key->prefixlen);
211
212 if (!tcp_ao_key_cmp(key, l3index, addr, prefixlen,
213 family, sndid, rcvid))
214 return key;
215 }
216 return NULL;
217 }
218
tcp_ao_do_lookup(const struct sock * sk,int l3index,const union tcp_ao_addr * addr,int family,int sndid,int rcvid)219 struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk, int l3index,
220 const union tcp_ao_addr *addr,
221 int family, int sndid, int rcvid)
222 {
223 return __tcp_ao_do_lookup(sk, l3index, addr, family, U8_MAX, sndid, rcvid);
224 }
225
tcp_ao_alloc_info(gfp_t flags)226 static struct tcp_ao_info *tcp_ao_alloc_info(gfp_t flags)
227 {
228 struct tcp_ao_info *ao;
229
230 ao = kzalloc(sizeof(*ao), flags);
231 if (!ao)
232 return NULL;
233 INIT_HLIST_HEAD(&ao->head);
234 refcount_set(&ao->refcnt, 1);
235
236 return ao;
237 }
238
tcp_ao_link_mkt(struct tcp_ao_info * ao,struct tcp_ao_key * mkt)239 static void tcp_ao_link_mkt(struct tcp_ao_info *ao, struct tcp_ao_key *mkt)
240 {
241 hlist_add_head_rcu(&mkt->node, &ao->head);
242 }
243
tcp_ao_copy_key(struct sock * sk,struct tcp_ao_key * key)244 static struct tcp_ao_key *tcp_ao_copy_key(struct sock *sk,
245 struct tcp_ao_key *key)
246 {
247 struct tcp_ao_key *new_key;
248
249 new_key = sock_kmalloc(sk, tcp_ao_sizeof_key(key),
250 GFP_ATOMIC);
251 if (!new_key)
252 return NULL;
253
254 *new_key = *key;
255 INIT_HLIST_NODE(&new_key->node);
256 tcp_sigpool_get(new_key->tcp_sigpool_id);
257 atomic64_set(&new_key->pkt_good, 0);
258 atomic64_set(&new_key->pkt_bad, 0);
259
260 return new_key;
261 }
262
tcp_ao_key_free_rcu(struct rcu_head * head)263 static void tcp_ao_key_free_rcu(struct rcu_head *head)
264 {
265 struct tcp_ao_key *key = container_of(head, struct tcp_ao_key, rcu);
266
267 tcp_sigpool_release(key->tcp_sigpool_id);
268 kfree_sensitive(key);
269 }
270
tcp_ao_info_free_rcu(struct rcu_head * head)271 static void tcp_ao_info_free_rcu(struct rcu_head *head)
272 {
273 struct tcp_ao_info *ao = container_of(head, struct tcp_ao_info, rcu);
274 struct tcp_ao_key *key;
275 struct hlist_node *n;
276
277 hlist_for_each_entry_safe(key, n, &ao->head, node) {
278 hlist_del(&key->node);
279 tcp_sigpool_release(key->tcp_sigpool_id);
280 kfree_sensitive(key);
281 }
282 kfree(ao);
283 static_branch_slow_dec_deferred(&tcp_ao_needed);
284 }
285
tcp_ao_sk_omem_free(struct sock * sk,struct tcp_ao_info * ao)286 static void tcp_ao_sk_omem_free(struct sock *sk, struct tcp_ao_info *ao)
287 {
288 size_t total_ao_sk_mem = 0;
289 struct tcp_ao_key *key;
290
291 hlist_for_each_entry(key, &ao->head, node)
292 total_ao_sk_mem += tcp_ao_sizeof_key(key);
293 atomic_sub(total_ao_sk_mem, &sk->sk_omem_alloc);
294 }
295
tcp_ao_destroy_sock(struct sock * sk,bool twsk)296 void tcp_ao_destroy_sock(struct sock *sk, bool twsk)
297 {
298 struct tcp_ao_info *ao;
299
300 if (twsk) {
301 ao = rcu_dereference_protected(tcp_twsk(sk)->ao_info, 1);
302 rcu_assign_pointer(tcp_twsk(sk)->ao_info, NULL);
303 } else {
304 ao = rcu_dereference_protected(tcp_sk(sk)->ao_info, 1);
305 rcu_assign_pointer(tcp_sk(sk)->ao_info, NULL);
306 }
307
308 if (!ao || !refcount_dec_and_test(&ao->refcnt))
309 return;
310
311 if (!twsk)
312 tcp_ao_sk_omem_free(sk, ao);
313 call_rcu(&ao->rcu, tcp_ao_info_free_rcu);
314 }
315
tcp_ao_time_wait(struct tcp_timewait_sock * tcptw,struct tcp_sock * tp)316 void tcp_ao_time_wait(struct tcp_timewait_sock *tcptw, struct tcp_sock *tp)
317 {
318 struct tcp_ao_info *ao_info = rcu_dereference_protected(tp->ao_info, 1);
319
320 if (ao_info) {
321 struct tcp_ao_key *key;
322 struct hlist_node *n;
323 int omem = 0;
324
325 hlist_for_each_entry_safe(key, n, &ao_info->head, node) {
326 omem += tcp_ao_sizeof_key(key);
327 }
328
329 refcount_inc(&ao_info->refcnt);
330 atomic_sub(omem, &(((struct sock *)tp)->sk_omem_alloc));
331 rcu_assign_pointer(tcptw->ao_info, ao_info);
332 } else {
333 tcptw->ao_info = NULL;
334 }
335 }
336
337 /* 4 tuple and ISNs are expected in NBO */
tcp_v4_ao_calc_key(struct tcp_ao_key * mkt,u8 * key,__be32 saddr,__be32 daddr,__be16 sport,__be16 dport,__be32 sisn,__be32 disn)338 static int tcp_v4_ao_calc_key(struct tcp_ao_key *mkt, u8 *key,
339 __be32 saddr, __be32 daddr,
340 __be16 sport, __be16 dport,
341 __be32 sisn, __be32 disn)
342 {
343 /* See RFC5926 3.1.1 */
344 struct kdf_input_block {
345 u8 counter;
346 u8 label[6];
347 struct tcp4_ao_context ctx;
348 __be16 outlen;
349 } __packed * tmp;
350 struct tcp_sigpool hp;
351 int err;
352
353 err = tcp_sigpool_start(mkt->tcp_sigpool_id, &hp);
354 if (err)
355 return err;
356
357 tmp = hp.scratch;
358 tmp->counter = 1;
359 memcpy(tmp->label, "TCP-AO", 6);
360 tmp->ctx.saddr = saddr;
361 tmp->ctx.daddr = daddr;
362 tmp->ctx.sport = sport;
363 tmp->ctx.dport = dport;
364 tmp->ctx.sisn = sisn;
365 tmp->ctx.disn = disn;
366 tmp->outlen = htons(tcp_ao_digest_size(mkt) * 8); /* in bits */
367
368 err = tcp_ao_calc_traffic_key(mkt, key, tmp, sizeof(*tmp), &hp);
369 tcp_sigpool_end(&hp);
370
371 return err;
372 }
373
tcp_v4_ao_calc_key_sk(struct tcp_ao_key * mkt,u8 * key,const struct sock * sk,__be32 sisn,__be32 disn,bool send)374 int tcp_v4_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key,
375 const struct sock *sk,
376 __be32 sisn, __be32 disn, bool send)
377 {
378 if (send)
379 return tcp_v4_ao_calc_key(mkt, key, sk->sk_rcv_saddr,
380 sk->sk_daddr, htons(sk->sk_num),
381 sk->sk_dport, sisn, disn);
382 else
383 return tcp_v4_ao_calc_key(mkt, key, sk->sk_daddr,
384 sk->sk_rcv_saddr, sk->sk_dport,
385 htons(sk->sk_num), disn, sisn);
386 }
387
tcp_ao_calc_key_sk(struct tcp_ao_key * mkt,u8 * key,const struct sock * sk,__be32 sisn,__be32 disn,bool send)388 static int tcp_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key,
389 const struct sock *sk,
390 __be32 sisn, __be32 disn, bool send)
391 {
392 if (mkt->family == AF_INET)
393 return tcp_v4_ao_calc_key_sk(mkt, key, sk, sisn, disn, send);
394 #if IS_ENABLED(CONFIG_IPV6)
395 else if (mkt->family == AF_INET6)
396 return tcp_v6_ao_calc_key_sk(mkt, key, sk, sisn, disn, send);
397 #endif
398 else
399 return -EOPNOTSUPP;
400 }
401
tcp_v4_ao_calc_key_rsk(struct tcp_ao_key * mkt,u8 * key,struct request_sock * req)402 int tcp_v4_ao_calc_key_rsk(struct tcp_ao_key *mkt, u8 *key,
403 struct request_sock *req)
404 {
405 struct inet_request_sock *ireq = inet_rsk(req);
406
407 return tcp_v4_ao_calc_key(mkt, key,
408 ireq->ir_loc_addr, ireq->ir_rmt_addr,
409 htons(ireq->ir_num), ireq->ir_rmt_port,
410 htonl(tcp_rsk(req)->snt_isn),
411 htonl(tcp_rsk(req)->rcv_isn));
412 }
413
tcp_v4_ao_calc_key_skb(struct tcp_ao_key * mkt,u8 * key,const struct sk_buff * skb,__be32 sisn,__be32 disn)414 static int tcp_v4_ao_calc_key_skb(struct tcp_ao_key *mkt, u8 *key,
415 const struct sk_buff *skb,
416 __be32 sisn, __be32 disn)
417 {
418 const struct iphdr *iph = ip_hdr(skb);
419 const struct tcphdr *th = tcp_hdr(skb);
420
421 return tcp_v4_ao_calc_key(mkt, key, iph->saddr, iph->daddr,
422 th->source, th->dest, sisn, disn);
423 }
424
tcp_ao_calc_key_skb(struct tcp_ao_key * mkt,u8 * key,const struct sk_buff * skb,__be32 sisn,__be32 disn,int family)425 static int tcp_ao_calc_key_skb(struct tcp_ao_key *mkt, u8 *key,
426 const struct sk_buff *skb,
427 __be32 sisn, __be32 disn, int family)
428 {
429 if (family == AF_INET)
430 return tcp_v4_ao_calc_key_skb(mkt, key, skb, sisn, disn);
431 #if IS_ENABLED(CONFIG_IPV6)
432 else if (family == AF_INET6)
433 return tcp_v6_ao_calc_key_skb(mkt, key, skb, sisn, disn);
434 #endif
435 return -EAFNOSUPPORT;
436 }
437
tcp_v4_ao_hash_pseudoheader(struct tcp_sigpool * hp,__be32 daddr,__be32 saddr,int nbytes)438 static int tcp_v4_ao_hash_pseudoheader(struct tcp_sigpool *hp,
439 __be32 daddr, __be32 saddr,
440 int nbytes)
441 {
442 struct tcp4_pseudohdr *bp;
443 struct scatterlist sg;
444
445 bp = hp->scratch;
446 bp->saddr = saddr;
447 bp->daddr = daddr;
448 bp->pad = 0;
449 bp->protocol = IPPROTO_TCP;
450 bp->len = cpu_to_be16(nbytes);
451
452 sg_init_one(&sg, bp, sizeof(*bp));
453 ahash_request_set_crypt(hp->req, &sg, NULL, sizeof(*bp));
454 return crypto_ahash_update(hp->req);
455 }
456
tcp_ao_hash_pseudoheader(unsigned short int family,const struct sock * sk,const struct sk_buff * skb,struct tcp_sigpool * hp,int nbytes)457 static int tcp_ao_hash_pseudoheader(unsigned short int family,
458 const struct sock *sk,
459 const struct sk_buff *skb,
460 struct tcp_sigpool *hp, int nbytes)
461 {
462 const struct tcphdr *th = tcp_hdr(skb);
463
464 /* TODO: Can we rely on checksum being zero to mean outbound pkt? */
465 if (!th->check) {
466 if (family == AF_INET)
467 return tcp_v4_ao_hash_pseudoheader(hp, sk->sk_daddr,
468 sk->sk_rcv_saddr, skb->len);
469 #if IS_ENABLED(CONFIG_IPV6)
470 else if (family == AF_INET6)
471 return tcp_v6_ao_hash_pseudoheader(hp, &sk->sk_v6_daddr,
472 &sk->sk_v6_rcv_saddr, skb->len);
473 #endif
474 else
475 return -EAFNOSUPPORT;
476 }
477
478 if (family == AF_INET) {
479 const struct iphdr *iph = ip_hdr(skb);
480
481 return tcp_v4_ao_hash_pseudoheader(hp, iph->daddr,
482 iph->saddr, skb->len);
483 #if IS_ENABLED(CONFIG_IPV6)
484 } else if (family == AF_INET6) {
485 const struct ipv6hdr *iph = ipv6_hdr(skb);
486
487 return tcp_v6_ao_hash_pseudoheader(hp, &iph->daddr,
488 &iph->saddr, skb->len);
489 #endif
490 }
491 return -EAFNOSUPPORT;
492 }
493
tcp_ao_compute_sne(u32 next_sne,u32 next_seq,u32 seq)494 u32 tcp_ao_compute_sne(u32 next_sne, u32 next_seq, u32 seq)
495 {
496 u32 sne = next_sne;
497
498 if (before(seq, next_seq)) {
499 if (seq > next_seq)
500 sne--;
501 } else {
502 if (seq < next_seq)
503 sne++;
504 }
505
506 return sne;
507 }
508
509 /* tcp_ao_hash_sne(struct tcp_sigpool *hp)
510 * @hp - used for hashing
511 * @sne - sne value
512 */
tcp_ao_hash_sne(struct tcp_sigpool * hp,u32 sne)513 static int tcp_ao_hash_sne(struct tcp_sigpool *hp, u32 sne)
514 {
515 struct scatterlist sg;
516 __be32 *bp;
517
518 bp = (__be32 *)hp->scratch;
519 *bp = htonl(sne);
520
521 sg_init_one(&sg, bp, sizeof(*bp));
522 ahash_request_set_crypt(hp->req, &sg, NULL, sizeof(*bp));
523 return crypto_ahash_update(hp->req);
524 }
525
tcp_ao_hash_header(struct tcp_sigpool * hp,const struct tcphdr * th,bool exclude_options,u8 * hash,int hash_offset,int hash_len)526 static int tcp_ao_hash_header(struct tcp_sigpool *hp,
527 const struct tcphdr *th,
528 bool exclude_options, u8 *hash,
529 int hash_offset, int hash_len)
530 {
531 struct scatterlist sg;
532 u8 *hdr = hp->scratch;
533 int err, len;
534
535 /* We are not allowed to change tcphdr, make a local copy */
536 if (exclude_options) {
537 len = sizeof(*th) + sizeof(struct tcp_ao_hdr) + hash_len;
538 memcpy(hdr, th, sizeof(*th));
539 memcpy(hdr + sizeof(*th),
540 (u8 *)th + hash_offset - sizeof(struct tcp_ao_hdr),
541 sizeof(struct tcp_ao_hdr));
542 memset(hdr + sizeof(*th) + sizeof(struct tcp_ao_hdr),
543 0, hash_len);
544 ((struct tcphdr *)hdr)->check = 0;
545 } else {
546 len = th->doff << 2;
547 memcpy(hdr, th, len);
548 /* zero out tcp-ao hash */
549 ((struct tcphdr *)hdr)->check = 0;
550 memset(hdr + hash_offset, 0, hash_len);
551 }
552
553 sg_init_one(&sg, hdr, len);
554 ahash_request_set_crypt(hp->req, &sg, NULL, len);
555 err = crypto_ahash_update(hp->req);
556 WARN_ON_ONCE(err != 0);
557 return err;
558 }
559
tcp_ao_hash_hdr(unsigned short int family,char * ao_hash,struct tcp_ao_key * key,const u8 * tkey,const union tcp_ao_addr * daddr,const union tcp_ao_addr * saddr,const struct tcphdr * th,u32 sne)560 int tcp_ao_hash_hdr(unsigned short int family, char *ao_hash,
561 struct tcp_ao_key *key, const u8 *tkey,
562 const union tcp_ao_addr *daddr,
563 const union tcp_ao_addr *saddr,
564 const struct tcphdr *th, u32 sne)
565 {
566 int tkey_len = tcp_ao_digest_size(key);
567 int hash_offset = ao_hash - (char *)th;
568 struct tcp_sigpool hp;
569 void *hash_buf = NULL;
570
571 hash_buf = kmalloc(tkey_len, GFP_ATOMIC);
572 if (!hash_buf)
573 goto clear_hash_noput;
574
575 if (tcp_sigpool_start(key->tcp_sigpool_id, &hp))
576 goto clear_hash_noput;
577
578 if (crypto_ahash_setkey(crypto_ahash_reqtfm(hp.req), tkey, tkey_len))
579 goto clear_hash;
580
581 if (crypto_ahash_init(hp.req))
582 goto clear_hash;
583
584 if (tcp_ao_hash_sne(&hp, sne))
585 goto clear_hash;
586 if (family == AF_INET) {
587 if (tcp_v4_ao_hash_pseudoheader(&hp, daddr->a4.s_addr,
588 saddr->a4.s_addr, th->doff * 4))
589 goto clear_hash;
590 #if IS_ENABLED(CONFIG_IPV6)
591 } else if (family == AF_INET6) {
592 if (tcp_v6_ao_hash_pseudoheader(&hp, &daddr->a6,
593 &saddr->a6, th->doff * 4))
594 goto clear_hash;
595 #endif
596 } else {
597 WARN_ON_ONCE(1);
598 goto clear_hash;
599 }
600 if (tcp_ao_hash_header(&hp, th,
601 !!(key->keyflags & TCP_AO_KEYF_EXCLUDE_OPT),
602 ao_hash, hash_offset, tcp_ao_maclen(key)))
603 goto clear_hash;
604 ahash_request_set_crypt(hp.req, NULL, hash_buf, 0);
605 if (crypto_ahash_final(hp.req))
606 goto clear_hash;
607
608 memcpy(ao_hash, hash_buf, tcp_ao_maclen(key));
609 tcp_sigpool_end(&hp);
610 kfree(hash_buf);
611 return 0;
612
613 clear_hash:
614 tcp_sigpool_end(&hp);
615 clear_hash_noput:
616 memset(ao_hash, 0, tcp_ao_maclen(key));
617 kfree(hash_buf);
618 return 1;
619 }
620
tcp_ao_hash_skb(unsigned short int family,char * ao_hash,struct tcp_ao_key * key,const struct sock * sk,const struct sk_buff * skb,const u8 * tkey,int hash_offset,u32 sne)621 int tcp_ao_hash_skb(unsigned short int family,
622 char *ao_hash, struct tcp_ao_key *key,
623 const struct sock *sk, const struct sk_buff *skb,
624 const u8 *tkey, int hash_offset, u32 sne)
625 {
626 const struct tcphdr *th = tcp_hdr(skb);
627 int tkey_len = tcp_ao_digest_size(key);
628 struct tcp_sigpool hp;
629 void *hash_buf = NULL;
630
631 hash_buf = kmalloc(tkey_len, GFP_ATOMIC);
632 if (!hash_buf)
633 goto clear_hash_noput;
634
635 if (tcp_sigpool_start(key->tcp_sigpool_id, &hp))
636 goto clear_hash_noput;
637
638 if (crypto_ahash_setkey(crypto_ahash_reqtfm(hp.req), tkey, tkey_len))
639 goto clear_hash;
640
641 /* For now use sha1 by default. Depends on alg in tcp_ao_key */
642 if (crypto_ahash_init(hp.req))
643 goto clear_hash;
644
645 if (tcp_ao_hash_sne(&hp, sne))
646 goto clear_hash;
647 if (tcp_ao_hash_pseudoheader(family, sk, skb, &hp, skb->len))
648 goto clear_hash;
649 if (tcp_ao_hash_header(&hp, th,
650 !!(key->keyflags & TCP_AO_KEYF_EXCLUDE_OPT),
651 ao_hash, hash_offset, tcp_ao_maclen(key)))
652 goto clear_hash;
653 if (tcp_sigpool_hash_skb_data(&hp, skb, th->doff << 2))
654 goto clear_hash;
655 ahash_request_set_crypt(hp.req, NULL, hash_buf, 0);
656 if (crypto_ahash_final(hp.req))
657 goto clear_hash;
658
659 memcpy(ao_hash, hash_buf, tcp_ao_maclen(key));
660 tcp_sigpool_end(&hp);
661 kfree(hash_buf);
662 return 0;
663
664 clear_hash:
665 tcp_sigpool_end(&hp);
666 clear_hash_noput:
667 memset(ao_hash, 0, tcp_ao_maclen(key));
668 kfree(hash_buf);
669 return 1;
670 }
671
tcp_v4_ao_hash_skb(char * ao_hash,struct tcp_ao_key * key,const struct sock * sk,const struct sk_buff * skb,const u8 * tkey,int hash_offset,u32 sne)672 int tcp_v4_ao_hash_skb(char *ao_hash, struct tcp_ao_key *key,
673 const struct sock *sk, const struct sk_buff *skb,
674 const u8 *tkey, int hash_offset, u32 sne)
675 {
676 return tcp_ao_hash_skb(AF_INET, ao_hash, key, sk, skb,
677 tkey, hash_offset, sne);
678 }
679
tcp_v4_ao_synack_hash(char * ao_hash,struct tcp_ao_key * ao_key,struct request_sock * req,const struct sk_buff * skb,int hash_offset,u32 sne)680 int tcp_v4_ao_synack_hash(char *ao_hash, struct tcp_ao_key *ao_key,
681 struct request_sock *req, const struct sk_buff *skb,
682 int hash_offset, u32 sne)
683 {
684 void *hash_buf = NULL;
685 int err;
686
687 hash_buf = kmalloc(tcp_ao_digest_size(ao_key), GFP_ATOMIC);
688 if (!hash_buf)
689 return -ENOMEM;
690
691 err = tcp_v4_ao_calc_key_rsk(ao_key, hash_buf, req);
692 if (err)
693 goto out;
694
695 err = tcp_ao_hash_skb(AF_INET, ao_hash, ao_key, req_to_sk(req), skb,
696 hash_buf, hash_offset, sne);
697 out:
698 kfree(hash_buf);
699 return err;
700 }
701
tcp_v4_ao_lookup_rsk(const struct sock * sk,struct request_sock * req,int sndid,int rcvid)702 struct tcp_ao_key *tcp_v4_ao_lookup_rsk(const struct sock *sk,
703 struct request_sock *req,
704 int sndid, int rcvid)
705 {
706 struct inet_request_sock *ireq = inet_rsk(req);
707 union tcp_ao_addr *addr = (union tcp_ao_addr *)&ireq->ir_rmt_addr;
708 int l3index;
709
710 l3index = l3mdev_master_ifindex_by_index(sock_net(sk), ireq->ir_iif);
711 return tcp_ao_do_lookup(sk, l3index, addr, AF_INET, sndid, rcvid);
712 }
713
tcp_v4_ao_lookup(const struct sock * sk,struct sock * addr_sk,int sndid,int rcvid)714 struct tcp_ao_key *tcp_v4_ao_lookup(const struct sock *sk, struct sock *addr_sk,
715 int sndid, int rcvid)
716 {
717 int l3index = l3mdev_master_ifindex_by_index(sock_net(sk),
718 addr_sk->sk_bound_dev_if);
719 union tcp_ao_addr *addr = (union tcp_ao_addr *)&addr_sk->sk_daddr;
720
721 return tcp_ao_do_lookup(sk, l3index, addr, AF_INET, sndid, rcvid);
722 }
723
tcp_ao_prepare_reset(const struct sock * sk,struct sk_buff * skb,const struct tcp_ao_hdr * aoh,int l3index,u32 seq,struct tcp_ao_key ** key,char ** traffic_key,bool * allocated_traffic_key,u8 * keyid,u32 * sne)724 int tcp_ao_prepare_reset(const struct sock *sk, struct sk_buff *skb,
725 const struct tcp_ao_hdr *aoh, int l3index, u32 seq,
726 struct tcp_ao_key **key, char **traffic_key,
727 bool *allocated_traffic_key, u8 *keyid, u32 *sne)
728 {
729 const struct tcphdr *th = tcp_hdr(skb);
730 struct tcp_ao_info *ao_info;
731
732 *allocated_traffic_key = false;
733 /* If there's no socket - than initial sisn/disn are unknown.
734 * Drop the segment. RFC5925 (7.7) advises to require graceful
735 * restart [RFC4724]. Alternatively, the RFC5925 advises to
736 * save/restore traffic keys before/after reboot.
737 * Linux TCP-AO support provides TCP_AO_ADD_KEY and TCP_AO_REPAIR
738 * options to restore a socket post-reboot.
739 */
740 if (!sk)
741 return -ENOTCONN;
742
743 if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_NEW_SYN_RECV)) {
744 unsigned int family = READ_ONCE(sk->sk_family);
745 union tcp_ao_addr *addr;
746 __be32 disn, sisn;
747
748 if (sk->sk_state == TCP_NEW_SYN_RECV) {
749 struct request_sock *req = inet_reqsk(sk);
750
751 sisn = htonl(tcp_rsk(req)->rcv_isn);
752 disn = htonl(tcp_rsk(req)->snt_isn);
753 *sne = tcp_ao_compute_sne(0, tcp_rsk(req)->snt_isn, seq);
754 } else {
755 sisn = th->seq;
756 disn = 0;
757 }
758 if (IS_ENABLED(CONFIG_IPV6) && family == AF_INET6)
759 addr = (union tcp_md5_addr *)&ipv6_hdr(skb)->saddr;
760 else
761 addr = (union tcp_md5_addr *)&ip_hdr(skb)->saddr;
762 #if IS_ENABLED(CONFIG_IPV6)
763 if (family == AF_INET6 && ipv6_addr_v4mapped(&sk->sk_v6_daddr))
764 family = AF_INET;
765 #endif
766
767 sk = sk_const_to_full_sk(sk);
768 ao_info = rcu_dereference(tcp_sk(sk)->ao_info);
769 if (!ao_info)
770 return -ENOENT;
771 *key = tcp_ao_do_lookup(sk, l3index, addr, family,
772 -1, aoh->rnext_keyid);
773 if (!*key)
774 return -ENOENT;
775 *traffic_key = kmalloc(tcp_ao_digest_size(*key), GFP_ATOMIC);
776 if (!*traffic_key)
777 return -ENOMEM;
778 *allocated_traffic_key = true;
779 if (tcp_ao_calc_key_skb(*key, *traffic_key, skb,
780 sisn, disn, family))
781 return -1;
782 *keyid = (*key)->rcvid;
783 } else {
784 struct tcp_ao_key *rnext_key;
785 u32 snd_basis;
786
787 if (sk->sk_state == TCP_TIME_WAIT) {
788 ao_info = rcu_dereference(tcp_twsk(sk)->ao_info);
789 snd_basis = tcp_twsk(sk)->tw_snd_nxt;
790 } else {
791 ao_info = rcu_dereference(tcp_sk(sk)->ao_info);
792 snd_basis = tcp_sk(sk)->snd_una;
793 }
794 if (!ao_info)
795 return -ENOENT;
796
797 *key = tcp_ao_established_key(sk, ao_info, aoh->rnext_keyid, -1);
798 if (!*key)
799 return -ENOENT;
800 *traffic_key = snd_other_key(*key);
801 rnext_key = READ_ONCE(ao_info->rnext_key);
802 *keyid = rnext_key->rcvid;
803 *sne = tcp_ao_compute_sne(READ_ONCE(ao_info->snd_sne),
804 snd_basis, seq);
805 }
806 return 0;
807 }
808
tcp_ao_transmit_skb(struct sock * sk,struct sk_buff * skb,struct tcp_ao_key * key,struct tcphdr * th,__u8 * hash_location)809 int tcp_ao_transmit_skb(struct sock *sk, struct sk_buff *skb,
810 struct tcp_ao_key *key, struct tcphdr *th,
811 __u8 *hash_location)
812 {
813 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
814 struct tcp_sock *tp = tcp_sk(sk);
815 struct tcp_ao_info *ao;
816 void *tkey_buf = NULL;
817 u8 *traffic_key;
818 u32 sne;
819
820 ao = rcu_dereference_protected(tcp_sk(sk)->ao_info,
821 lockdep_sock_is_held(sk));
822 traffic_key = snd_other_key(key);
823 if (unlikely(tcb->tcp_flags & TCPHDR_SYN)) {
824 __be32 disn;
825
826 if (!(tcb->tcp_flags & TCPHDR_ACK)) {
827 disn = 0;
828 tkey_buf = kmalloc(tcp_ao_digest_size(key), GFP_ATOMIC);
829 if (!tkey_buf)
830 return -ENOMEM;
831 traffic_key = tkey_buf;
832 } else {
833 disn = ao->risn;
834 }
835 tp->af_specific->ao_calc_key_sk(key, traffic_key,
836 sk, ao->lisn, disn, true);
837 }
838 sne = tcp_ao_compute_sne(READ_ONCE(ao->snd_sne), READ_ONCE(tp->snd_una),
839 ntohl(th->seq));
840 tp->af_specific->calc_ao_hash(hash_location, key, sk, skb, traffic_key,
841 hash_location - (u8 *)th, sne);
842 kfree(tkey_buf);
843 return 0;
844 }
845
tcp_ao_inbound_lookup(unsigned short int family,const struct sock * sk,const struct sk_buff * skb,int sndid,int rcvid,int l3index)846 static struct tcp_ao_key *tcp_ao_inbound_lookup(unsigned short int family,
847 const struct sock *sk, const struct sk_buff *skb,
848 int sndid, int rcvid, int l3index)
849 {
850 if (family == AF_INET) {
851 const struct iphdr *iph = ip_hdr(skb);
852
853 return tcp_ao_do_lookup(sk, l3index,
854 (union tcp_ao_addr *)&iph->saddr,
855 AF_INET, sndid, rcvid);
856 } else {
857 const struct ipv6hdr *iph = ipv6_hdr(skb);
858
859 return tcp_ao_do_lookup(sk, l3index,
860 (union tcp_ao_addr *)&iph->saddr,
861 AF_INET6, sndid, rcvid);
862 }
863 }
864
tcp_ao_syncookie(struct sock * sk,const struct sk_buff * skb,struct request_sock * req,unsigned short int family)865 void tcp_ao_syncookie(struct sock *sk, const struct sk_buff *skb,
866 struct request_sock *req, unsigned short int family)
867 {
868 struct tcp_request_sock *treq = tcp_rsk(req);
869 const struct tcphdr *th = tcp_hdr(skb);
870 const struct tcp_ao_hdr *aoh;
871 struct tcp_ao_key *key;
872 int l3index;
873
874 /* treq->af_specific is used to perform TCP_AO lookup
875 * in tcp_create_openreq_child().
876 */
877 #if IS_ENABLED(CONFIG_IPV6)
878 if (family == AF_INET6)
879 treq->af_specific = &tcp_request_sock_ipv6_ops;
880 else
881 #endif
882 treq->af_specific = &tcp_request_sock_ipv4_ops;
883
884 treq->used_tcp_ao = false;
885
886 if (tcp_parse_auth_options(th, NULL, &aoh) || !aoh)
887 return;
888
889 l3index = l3mdev_master_ifindex_by_index(sock_net(sk), inet_rsk(req)->ir_iif);
890 key = tcp_ao_inbound_lookup(family, sk, skb, -1, aoh->keyid, l3index);
891 if (!key)
892 /* Key not found, continue without TCP-AO */
893 return;
894
895 treq->ao_rcv_next = aoh->keyid;
896 treq->ao_keyid = aoh->rnext_keyid;
897 treq->used_tcp_ao = true;
898 }
899
900 static enum skb_drop_reason
tcp_ao_verify_hash(const struct sock * sk,const struct sk_buff * skb,unsigned short int family,struct tcp_ao_info * info,const struct tcp_ao_hdr * aoh,struct tcp_ao_key * key,u8 * traffic_key,u8 * phash,u32 sne,int l3index)901 tcp_ao_verify_hash(const struct sock *sk, const struct sk_buff *skb,
902 unsigned short int family, struct tcp_ao_info *info,
903 const struct tcp_ao_hdr *aoh, struct tcp_ao_key *key,
904 u8 *traffic_key, u8 *phash, u32 sne, int l3index)
905 {
906 const struct tcphdr *th = tcp_hdr(skb);
907 u8 maclen = tcp_ao_hdr_maclen(aoh);
908 void *hash_buf = NULL;
909
910 if (maclen != tcp_ao_maclen(key)) {
911 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOBAD);
912 atomic64_inc(&info->counters.pkt_bad);
913 atomic64_inc(&key->pkt_bad);
914 trace_tcp_ao_wrong_maclen(sk, skb, aoh->keyid,
915 aoh->rnext_keyid, maclen);
916 return SKB_DROP_REASON_TCP_AOFAILURE;
917 }
918
919 hash_buf = kmalloc(tcp_ao_digest_size(key), GFP_ATOMIC);
920 if (!hash_buf)
921 return SKB_DROP_REASON_NOT_SPECIFIED;
922
923 /* XXX: make it per-AF callback? */
924 tcp_ao_hash_skb(family, hash_buf, key, sk, skb, traffic_key,
925 (phash - (u8 *)th), sne);
926 if (memcmp(phash, hash_buf, maclen)) {
927 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOBAD);
928 atomic64_inc(&info->counters.pkt_bad);
929 atomic64_inc(&key->pkt_bad);
930 trace_tcp_ao_mismatch(sk, skb, aoh->keyid,
931 aoh->rnext_keyid, maclen);
932 kfree(hash_buf);
933 return SKB_DROP_REASON_TCP_AOFAILURE;
934 }
935 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOGOOD);
936 atomic64_inc(&info->counters.pkt_good);
937 atomic64_inc(&key->pkt_good);
938 kfree(hash_buf);
939 return SKB_NOT_DROPPED_YET;
940 }
941
942 enum skb_drop_reason
tcp_inbound_ao_hash(struct sock * sk,const struct sk_buff * skb,unsigned short int family,const struct request_sock * req,int l3index,const struct tcp_ao_hdr * aoh)943 tcp_inbound_ao_hash(struct sock *sk, const struct sk_buff *skb,
944 unsigned short int family, const struct request_sock *req,
945 int l3index, const struct tcp_ao_hdr *aoh)
946 {
947 const struct tcphdr *th = tcp_hdr(skb);
948 u8 maclen = tcp_ao_hdr_maclen(aoh);
949 u8 *phash = (u8 *)(aoh + 1); /* hash goes just after the header */
950 struct tcp_ao_info *info;
951 enum skb_drop_reason ret;
952 struct tcp_ao_key *key;
953 __be32 sisn, disn;
954 u8 *traffic_key;
955 int state;
956 u32 sne = 0;
957
958 info = rcu_dereference(tcp_sk(sk)->ao_info);
959 if (!info) {
960 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOKEYNOTFOUND);
961 trace_tcp_ao_key_not_found(sk, skb, aoh->keyid,
962 aoh->rnext_keyid, maclen);
963 return SKB_DROP_REASON_TCP_AOUNEXPECTED;
964 }
965
966 if (unlikely(th->syn)) {
967 sisn = th->seq;
968 disn = 0;
969 }
970
971 state = READ_ONCE(sk->sk_state);
972 /* Fast-path */
973 if (likely((1 << state) & TCP_AO_ESTABLISHED)) {
974 enum skb_drop_reason err;
975 struct tcp_ao_key *current_key;
976
977 /* Check if this socket's rnext_key matches the keyid in the
978 * packet. If not we lookup the key based on the keyid
979 * matching the rcvid in the mkt.
980 */
981 key = READ_ONCE(info->rnext_key);
982 if (key->rcvid != aoh->keyid) {
983 key = tcp_ao_established_key(sk, info, -1, aoh->keyid);
984 if (!key)
985 goto key_not_found;
986 }
987
988 /* Delayed retransmitted SYN */
989 if (unlikely(th->syn && !th->ack))
990 goto verify_hash;
991
992 sne = tcp_ao_compute_sne(info->rcv_sne, tcp_sk(sk)->rcv_nxt,
993 ntohl(th->seq));
994 /* Established socket, traffic key are cached */
995 traffic_key = rcv_other_key(key);
996 err = tcp_ao_verify_hash(sk, skb, family, info, aoh, key,
997 traffic_key, phash, sne, l3index);
998 if (err)
999 return err;
1000 current_key = READ_ONCE(info->current_key);
1001 /* Key rotation: the peer asks us to use new key (RNext) */
1002 if (unlikely(aoh->rnext_keyid != current_key->sndid)) {
1003 trace_tcp_ao_rnext_request(sk, skb, current_key->sndid,
1004 aoh->rnext_keyid,
1005 tcp_ao_hdr_maclen(aoh));
1006 /* If the key is not found we do nothing. */
1007 key = tcp_ao_established_key(sk, info, aoh->rnext_keyid, -1);
1008 if (key)
1009 /* pairs with tcp_ao_del_cmd */
1010 WRITE_ONCE(info->current_key, key);
1011 }
1012 return SKB_NOT_DROPPED_YET;
1013 }
1014
1015 if (unlikely(state == TCP_CLOSE))
1016 return SKB_DROP_REASON_TCP_CLOSE;
1017
1018 /* Lookup key based on peer address and keyid.
1019 * current_key and rnext_key must not be used on tcp listen
1020 * sockets as otherwise:
1021 * - request sockets would race on those key pointers
1022 * - tcp_ao_del_cmd() allows async key removal
1023 */
1024 key = tcp_ao_inbound_lookup(family, sk, skb, -1, aoh->keyid, l3index);
1025 if (!key)
1026 goto key_not_found;
1027
1028 if (th->syn && !th->ack)
1029 goto verify_hash;
1030
1031 if ((1 << state) & (TCPF_LISTEN | TCPF_NEW_SYN_RECV)) {
1032 /* Make the initial syn the likely case here */
1033 if (unlikely(req)) {
1034 sne = tcp_ao_compute_sne(0, tcp_rsk(req)->rcv_isn,
1035 ntohl(th->seq));
1036 sisn = htonl(tcp_rsk(req)->rcv_isn);
1037 disn = htonl(tcp_rsk(req)->snt_isn);
1038 } else if (unlikely(th->ack && !th->syn)) {
1039 /* Possible syncookie packet */
1040 sisn = htonl(ntohl(th->seq) - 1);
1041 disn = htonl(ntohl(th->ack_seq) - 1);
1042 sne = tcp_ao_compute_sne(0, ntohl(sisn),
1043 ntohl(th->seq));
1044 } else if (unlikely(!th->syn)) {
1045 /* no way to figure out initial sisn/disn - drop */
1046 return SKB_DROP_REASON_TCP_FLAGS;
1047 }
1048 } else if ((1 << state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
1049 disn = info->lisn;
1050 if (th->syn || th->rst)
1051 sisn = th->seq;
1052 else
1053 sisn = info->risn;
1054 } else {
1055 WARN_ONCE(1, "TCP-AO: Unexpected sk_state %d", state);
1056 return SKB_DROP_REASON_TCP_AOFAILURE;
1057 }
1058 verify_hash:
1059 traffic_key = kmalloc(tcp_ao_digest_size(key), GFP_ATOMIC);
1060 if (!traffic_key)
1061 return SKB_DROP_REASON_NOT_SPECIFIED;
1062 tcp_ao_calc_key_skb(key, traffic_key, skb, sisn, disn, family);
1063 ret = tcp_ao_verify_hash(sk, skb, family, info, aoh, key,
1064 traffic_key, phash, sne, l3index);
1065 kfree(traffic_key);
1066 return ret;
1067
1068 key_not_found:
1069 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOKEYNOTFOUND);
1070 atomic64_inc(&info->counters.key_not_found);
1071 trace_tcp_ao_key_not_found(sk, skb, aoh->keyid,
1072 aoh->rnext_keyid, maclen);
1073 return SKB_DROP_REASON_TCP_AOKEYNOTFOUND;
1074 }
1075
tcp_ao_cache_traffic_keys(const struct sock * sk,struct tcp_ao_info * ao,struct tcp_ao_key * ao_key)1076 static int tcp_ao_cache_traffic_keys(const struct sock *sk,
1077 struct tcp_ao_info *ao,
1078 struct tcp_ao_key *ao_key)
1079 {
1080 u8 *traffic_key = snd_other_key(ao_key);
1081 int ret;
1082
1083 ret = tcp_ao_calc_key_sk(ao_key, traffic_key, sk,
1084 ao->lisn, ao->risn, true);
1085 if (ret)
1086 return ret;
1087
1088 traffic_key = rcv_other_key(ao_key);
1089 ret = tcp_ao_calc_key_sk(ao_key, traffic_key, sk,
1090 ao->lisn, ao->risn, false);
1091 return ret;
1092 }
1093
tcp_ao_connect_init(struct sock * sk)1094 void tcp_ao_connect_init(struct sock *sk)
1095 {
1096 struct tcp_sock *tp = tcp_sk(sk);
1097 struct tcp_ao_info *ao_info;
1098 struct hlist_node *next;
1099 union tcp_ao_addr *addr;
1100 struct tcp_ao_key *key;
1101 int family, l3index;
1102
1103 ao_info = rcu_dereference_protected(tp->ao_info,
1104 lockdep_sock_is_held(sk));
1105 if (!ao_info)
1106 return;
1107
1108 /* Remove all keys that don't match the peer */
1109 family = sk->sk_family;
1110 if (family == AF_INET)
1111 addr = (union tcp_ao_addr *)&sk->sk_daddr;
1112 #if IS_ENABLED(CONFIG_IPV6)
1113 else if (family == AF_INET6)
1114 addr = (union tcp_ao_addr *)&sk->sk_v6_daddr;
1115 #endif
1116 else
1117 return;
1118 l3index = l3mdev_master_ifindex_by_index(sock_net(sk),
1119 sk->sk_bound_dev_if);
1120
1121 hlist_for_each_entry_safe(key, next, &ao_info->head, node) {
1122 if (!tcp_ao_key_cmp(key, l3index, addr, key->prefixlen, family, -1, -1))
1123 continue;
1124
1125 if (key == ao_info->current_key)
1126 ao_info->current_key = NULL;
1127 if (key == ao_info->rnext_key)
1128 ao_info->rnext_key = NULL;
1129 hlist_del_rcu(&key->node);
1130 atomic_sub(tcp_ao_sizeof_key(key), &sk->sk_omem_alloc);
1131 call_rcu(&key->rcu, tcp_ao_key_free_rcu);
1132 }
1133
1134 key = tp->af_specific->ao_lookup(sk, sk, -1, -1);
1135 if (key) {
1136 /* if current_key or rnext_key were not provided,
1137 * use the first key matching the peer
1138 */
1139 if (!ao_info->current_key)
1140 ao_info->current_key = key;
1141 if (!ao_info->rnext_key)
1142 ao_info->rnext_key = key;
1143 tp->tcp_header_len += tcp_ao_len_aligned(key);
1144
1145 ao_info->lisn = htonl(tp->write_seq);
1146 ao_info->snd_sne = 0;
1147 } else {
1148 /* Can't happen: tcp_connect() verifies that there's
1149 * at least one tcp-ao key that matches the remote peer.
1150 */
1151 WARN_ON_ONCE(1);
1152 rcu_assign_pointer(tp->ao_info, NULL);
1153 kfree(ao_info);
1154 }
1155 }
1156
tcp_ao_established(struct sock * sk)1157 void tcp_ao_established(struct sock *sk)
1158 {
1159 struct tcp_ao_info *ao;
1160 struct tcp_ao_key *key;
1161
1162 ao = rcu_dereference_protected(tcp_sk(sk)->ao_info,
1163 lockdep_sock_is_held(sk));
1164 if (!ao)
1165 return;
1166
1167 hlist_for_each_entry_rcu(key, &ao->head, node, lockdep_sock_is_held(sk))
1168 tcp_ao_cache_traffic_keys(sk, ao, key);
1169 }
1170
tcp_ao_finish_connect(struct sock * sk,struct sk_buff * skb)1171 void tcp_ao_finish_connect(struct sock *sk, struct sk_buff *skb)
1172 {
1173 struct tcp_ao_info *ao;
1174 struct tcp_ao_key *key;
1175
1176 ao = rcu_dereference_protected(tcp_sk(sk)->ao_info,
1177 lockdep_sock_is_held(sk));
1178 if (!ao)
1179 return;
1180
1181 /* sk with TCP_REPAIR_ON does not have skb in tcp_finish_connect */
1182 if (skb)
1183 WRITE_ONCE(ao->risn, tcp_hdr(skb)->seq);
1184 ao->rcv_sne = 0;
1185
1186 hlist_for_each_entry_rcu(key, &ao->head, node, lockdep_sock_is_held(sk))
1187 tcp_ao_cache_traffic_keys(sk, ao, key);
1188 }
1189
tcp_ao_copy_all_matching(const struct sock * sk,struct sock * newsk,struct request_sock * req,struct sk_buff * skb,int family)1190 int tcp_ao_copy_all_matching(const struct sock *sk, struct sock *newsk,
1191 struct request_sock *req, struct sk_buff *skb,
1192 int family)
1193 {
1194 struct tcp_ao_key *key, *new_key, *first_key;
1195 struct tcp_ao_info *new_ao, *ao;
1196 struct hlist_node *key_head;
1197 int l3index, ret = -ENOMEM;
1198 union tcp_ao_addr *addr;
1199 bool match = false;
1200
1201 ao = rcu_dereference(tcp_sk(sk)->ao_info);
1202 if (!ao)
1203 return 0;
1204
1205 /* New socket without TCP-AO on it */
1206 if (!tcp_rsk_used_ao(req))
1207 return 0;
1208
1209 new_ao = tcp_ao_alloc_info(GFP_ATOMIC);
1210 if (!new_ao)
1211 return -ENOMEM;
1212 new_ao->lisn = htonl(tcp_rsk(req)->snt_isn);
1213 new_ao->risn = htonl(tcp_rsk(req)->rcv_isn);
1214 new_ao->ao_required = ao->ao_required;
1215 new_ao->accept_icmps = ao->accept_icmps;
1216
1217 if (family == AF_INET) {
1218 addr = (union tcp_ao_addr *)&newsk->sk_daddr;
1219 #if IS_ENABLED(CONFIG_IPV6)
1220 } else if (family == AF_INET6) {
1221 addr = (union tcp_ao_addr *)&newsk->sk_v6_daddr;
1222 #endif
1223 } else {
1224 ret = -EAFNOSUPPORT;
1225 goto free_ao;
1226 }
1227 l3index = l3mdev_master_ifindex_by_index(sock_net(newsk),
1228 newsk->sk_bound_dev_if);
1229
1230 hlist_for_each_entry_rcu(key, &ao->head, node) {
1231 if (tcp_ao_key_cmp(key, l3index, addr, key->prefixlen, family, -1, -1))
1232 continue;
1233
1234 new_key = tcp_ao_copy_key(newsk, key);
1235 if (!new_key)
1236 goto free_and_exit;
1237
1238 tcp_ao_cache_traffic_keys(newsk, new_ao, new_key);
1239 tcp_ao_link_mkt(new_ao, new_key);
1240 match = true;
1241 }
1242
1243 if (!match) {
1244 /* RFC5925 (7.4.1) specifies that the TCP-AO status
1245 * of a connection is determined on the initial SYN.
1246 * At this point the connection was TCP-AO enabled, so
1247 * it can't switch to being unsigned if peer's key
1248 * disappears on the listening socket.
1249 */
1250 ret = -EKEYREJECTED;
1251 goto free_and_exit;
1252 }
1253
1254 if (!static_key_fast_inc_not_disabled(&tcp_ao_needed.key.key)) {
1255 ret = -EUSERS;
1256 goto free_and_exit;
1257 }
1258
1259 key_head = rcu_dereference(hlist_first_rcu(&new_ao->head));
1260 first_key = hlist_entry_safe(key_head, struct tcp_ao_key, node);
1261
1262 key = tcp_ao_established_key(req_to_sk(req), new_ao, tcp_rsk(req)->ao_keyid, -1);
1263 if (key)
1264 new_ao->current_key = key;
1265 else
1266 new_ao->current_key = first_key;
1267
1268 /* set rnext_key */
1269 key = tcp_ao_established_key(req_to_sk(req), new_ao, -1, tcp_rsk(req)->ao_rcv_next);
1270 if (key)
1271 new_ao->rnext_key = key;
1272 else
1273 new_ao->rnext_key = first_key;
1274
1275 sk_gso_disable(newsk);
1276 rcu_assign_pointer(tcp_sk(newsk)->ao_info, new_ao);
1277
1278 return 0;
1279
1280 free_and_exit:
1281 hlist_for_each_entry_safe(key, key_head, &new_ao->head, node) {
1282 hlist_del(&key->node);
1283 tcp_sigpool_release(key->tcp_sigpool_id);
1284 atomic_sub(tcp_ao_sizeof_key(key), &newsk->sk_omem_alloc);
1285 kfree_sensitive(key);
1286 }
1287 free_ao:
1288 kfree(new_ao);
1289 return ret;
1290 }
1291
tcp_ao_can_set_current_rnext(struct sock * sk)1292 static bool tcp_ao_can_set_current_rnext(struct sock *sk)
1293 {
1294 /* There aren't current/rnext keys on TCP_LISTEN sockets */
1295 if (sk->sk_state == TCP_LISTEN)
1296 return false;
1297 return true;
1298 }
1299
tcp_ao_verify_ipv4(struct sock * sk,struct tcp_ao_add * cmd,union tcp_ao_addr ** addr)1300 static int tcp_ao_verify_ipv4(struct sock *sk, struct tcp_ao_add *cmd,
1301 union tcp_ao_addr **addr)
1302 {
1303 struct sockaddr_in *sin = (struct sockaddr_in *)&cmd->addr;
1304 struct inet_sock *inet = inet_sk(sk);
1305
1306 if (sin->sin_family != AF_INET)
1307 return -EINVAL;
1308
1309 /* Currently matching is not performed on port (or port ranges) */
1310 if (sin->sin_port != 0)
1311 return -EINVAL;
1312
1313 /* Check prefix and trailing 0's in addr */
1314 if (cmd->prefix != 0) {
1315 __be32 mask;
1316
1317 if (ntohl(sin->sin_addr.s_addr) == INADDR_ANY)
1318 return -EINVAL;
1319 if (cmd->prefix > 32)
1320 return -EINVAL;
1321
1322 mask = inet_make_mask(cmd->prefix);
1323 if (sin->sin_addr.s_addr & ~mask)
1324 return -EINVAL;
1325
1326 /* Check that MKT address is consistent with socket */
1327 if (ntohl(inet->inet_daddr) != INADDR_ANY &&
1328 (inet->inet_daddr & mask) != sin->sin_addr.s_addr)
1329 return -EINVAL;
1330 } else {
1331 if (ntohl(sin->sin_addr.s_addr) != INADDR_ANY)
1332 return -EINVAL;
1333 }
1334
1335 *addr = (union tcp_ao_addr *)&sin->sin_addr;
1336 return 0;
1337 }
1338
tcp_ao_parse_crypto(struct tcp_ao_add * cmd,struct tcp_ao_key * key)1339 static int tcp_ao_parse_crypto(struct tcp_ao_add *cmd, struct tcp_ao_key *key)
1340 {
1341 unsigned int syn_tcp_option_space;
1342 bool is_kdf_aes_128_cmac = false;
1343 struct crypto_ahash *tfm;
1344 struct tcp_sigpool hp;
1345 void *tmp_key = NULL;
1346 int err;
1347
1348 /* RFC5926, 3.1.1.2. KDF_AES_128_CMAC */
1349 if (!strcmp("cmac(aes128)", cmd->alg_name)) {
1350 strscpy(cmd->alg_name, "cmac(aes)", sizeof(cmd->alg_name));
1351 is_kdf_aes_128_cmac = (cmd->keylen != 16);
1352 tmp_key = kmalloc(cmd->keylen, GFP_KERNEL);
1353 if (!tmp_key)
1354 return -ENOMEM;
1355 }
1356
1357 key->maclen = cmd->maclen ?: 12; /* 12 is the default in RFC5925 */
1358
1359 /* Check: maclen + tcp-ao header <= (MAX_TCP_OPTION_SPACE - mss
1360 * - tstamp (including sackperm)
1361 * - wscale),
1362 * see tcp_syn_options(), tcp_synack_options(), commit 33ad798c924b.
1363 *
1364 * In order to allow D-SACK with TCP-AO, the header size should be:
1365 * (MAX_TCP_OPTION_SPACE - TCPOLEN_TSTAMP_ALIGNED
1366 * - TCPOLEN_SACK_BASE_ALIGNED
1367 * - 2 * TCPOLEN_SACK_PERBLOCK) = 8 (maclen = 4),
1368 * see tcp_established_options().
1369 *
1370 * RFC5925, 2.2:
1371 * Typical MACs are 96-128 bits (12-16 bytes), but any length
1372 * that fits in the header of the segment being authenticated
1373 * is allowed.
1374 *
1375 * RFC5925, 7.6:
1376 * TCP-AO continues to consume 16 bytes in non-SYN segments,
1377 * leaving a total of 24 bytes for other options, of which
1378 * the timestamp consumes 10. This leaves 14 bytes, of which 10
1379 * are used for a single SACK block. When two SACK blocks are used,
1380 * such as to handle D-SACK, a smaller TCP-AO MAC would be required
1381 * to make room for the additional SACK block (i.e., to leave 18
1382 * bytes for the D-SACK variant of the SACK option) [RFC2883].
1383 * Note that D-SACK is not supportable in TCP MD5 in the presence
1384 * of timestamps, because TCP MD5’s MAC length is fixed and too
1385 * large to leave sufficient option space.
1386 */
1387 syn_tcp_option_space = MAX_TCP_OPTION_SPACE;
1388 syn_tcp_option_space -= TCPOLEN_MSS_ALIGNED;
1389 syn_tcp_option_space -= TCPOLEN_TSTAMP_ALIGNED;
1390 syn_tcp_option_space -= TCPOLEN_WSCALE_ALIGNED;
1391 if (tcp_ao_len_aligned(key) > syn_tcp_option_space) {
1392 err = -EMSGSIZE;
1393 goto err_kfree;
1394 }
1395
1396 key->keylen = cmd->keylen;
1397 memcpy(key->key, cmd->key, cmd->keylen);
1398
1399 err = tcp_sigpool_start(key->tcp_sigpool_id, &hp);
1400 if (err)
1401 goto err_kfree;
1402
1403 tfm = crypto_ahash_reqtfm(hp.req);
1404 if (is_kdf_aes_128_cmac) {
1405 void *scratch = hp.scratch;
1406 struct scatterlist sg;
1407
1408 memcpy(tmp_key, cmd->key, cmd->keylen);
1409 sg_init_one(&sg, tmp_key, cmd->keylen);
1410
1411 /* Using zero-key of 16 bytes as described in RFC5926 */
1412 memset(scratch, 0, 16);
1413 err = crypto_ahash_setkey(tfm, scratch, 16);
1414 if (err)
1415 goto err_pool_end;
1416
1417 err = crypto_ahash_init(hp.req);
1418 if (err)
1419 goto err_pool_end;
1420
1421 ahash_request_set_crypt(hp.req, &sg, key->key, cmd->keylen);
1422 err = crypto_ahash_update(hp.req);
1423 if (err)
1424 goto err_pool_end;
1425
1426 err |= crypto_ahash_final(hp.req);
1427 if (err)
1428 goto err_pool_end;
1429 key->keylen = 16;
1430 }
1431
1432 err = crypto_ahash_setkey(tfm, key->key, key->keylen);
1433 if (err)
1434 goto err_pool_end;
1435
1436 tcp_sigpool_end(&hp);
1437 kfree_sensitive(tmp_key);
1438
1439 if (tcp_ao_maclen(key) > key->digest_size)
1440 return -EINVAL;
1441
1442 return 0;
1443
1444 err_pool_end:
1445 tcp_sigpool_end(&hp);
1446 err_kfree:
1447 kfree_sensitive(tmp_key);
1448 return err;
1449 }
1450
1451 #if IS_ENABLED(CONFIG_IPV6)
tcp_ao_verify_ipv6(struct sock * sk,struct tcp_ao_add * cmd,union tcp_ao_addr ** paddr,unsigned short int * family)1452 static int tcp_ao_verify_ipv6(struct sock *sk, struct tcp_ao_add *cmd,
1453 union tcp_ao_addr **paddr,
1454 unsigned short int *family)
1455 {
1456 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&cmd->addr;
1457 struct in6_addr *addr = &sin6->sin6_addr;
1458 u8 prefix = cmd->prefix;
1459
1460 if (sin6->sin6_family != AF_INET6)
1461 return -EINVAL;
1462
1463 /* Currently matching is not performed on port (or port ranges) */
1464 if (sin6->sin6_port != 0)
1465 return -EINVAL;
1466
1467 /* Check prefix and trailing 0's in addr */
1468 if (cmd->prefix != 0 && ipv6_addr_v4mapped(addr)) {
1469 __be32 addr4 = addr->s6_addr32[3];
1470 __be32 mask;
1471
1472 if (prefix > 32 || ntohl(addr4) == INADDR_ANY)
1473 return -EINVAL;
1474
1475 mask = inet_make_mask(prefix);
1476 if (addr4 & ~mask)
1477 return -EINVAL;
1478
1479 /* Check that MKT address is consistent with socket */
1480 if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
1481 __be32 daddr4 = sk->sk_v6_daddr.s6_addr32[3];
1482
1483 if (!ipv6_addr_v4mapped(&sk->sk_v6_daddr))
1484 return -EINVAL;
1485 if ((daddr4 & mask) != addr4)
1486 return -EINVAL;
1487 }
1488
1489 *paddr = (union tcp_ao_addr *)&addr->s6_addr32[3];
1490 *family = AF_INET;
1491 return 0;
1492 } else if (cmd->prefix != 0) {
1493 struct in6_addr pfx;
1494
1495 if (ipv6_addr_any(addr) || prefix > 128)
1496 return -EINVAL;
1497
1498 ipv6_addr_prefix(&pfx, addr, prefix);
1499 if (ipv6_addr_cmp(&pfx, addr))
1500 return -EINVAL;
1501
1502 /* Check that MKT address is consistent with socket */
1503 if (!ipv6_addr_any(&sk->sk_v6_daddr) &&
1504 !ipv6_prefix_equal(&sk->sk_v6_daddr, addr, prefix))
1505
1506 return -EINVAL;
1507 } else {
1508 if (!ipv6_addr_any(addr))
1509 return -EINVAL;
1510 }
1511
1512 *paddr = (union tcp_ao_addr *)addr;
1513 return 0;
1514 }
1515 #else
tcp_ao_verify_ipv6(struct sock * sk,struct tcp_ao_add * cmd,union tcp_ao_addr ** paddr,unsigned short int * family)1516 static int tcp_ao_verify_ipv6(struct sock *sk, struct tcp_ao_add *cmd,
1517 union tcp_ao_addr **paddr,
1518 unsigned short int *family)
1519 {
1520 return -EOPNOTSUPP;
1521 }
1522 #endif
1523
setsockopt_ao_info(struct sock * sk)1524 static struct tcp_ao_info *setsockopt_ao_info(struct sock *sk)
1525 {
1526 if (sk_fullsock(sk)) {
1527 return rcu_dereference_protected(tcp_sk(sk)->ao_info,
1528 lockdep_sock_is_held(sk));
1529 } else if (sk->sk_state == TCP_TIME_WAIT) {
1530 return rcu_dereference_protected(tcp_twsk(sk)->ao_info,
1531 lockdep_sock_is_held(sk));
1532 }
1533 return ERR_PTR(-ESOCKTNOSUPPORT);
1534 }
1535
getsockopt_ao_info(struct sock * sk)1536 static struct tcp_ao_info *getsockopt_ao_info(struct sock *sk)
1537 {
1538 if (sk_fullsock(sk))
1539 return rcu_dereference(tcp_sk(sk)->ao_info);
1540 else if (sk->sk_state == TCP_TIME_WAIT)
1541 return rcu_dereference(tcp_twsk(sk)->ao_info);
1542
1543 return ERR_PTR(-ESOCKTNOSUPPORT);
1544 }
1545
1546 #define TCP_AO_KEYF_ALL (TCP_AO_KEYF_IFINDEX | TCP_AO_KEYF_EXCLUDE_OPT)
1547 #define TCP_AO_GET_KEYF_VALID (TCP_AO_KEYF_IFINDEX)
1548
tcp_ao_key_alloc(struct sock * sk,struct tcp_ao_add * cmd)1549 static struct tcp_ao_key *tcp_ao_key_alloc(struct sock *sk,
1550 struct tcp_ao_add *cmd)
1551 {
1552 const char *algo = cmd->alg_name;
1553 unsigned int digest_size;
1554 struct crypto_ahash *tfm;
1555 struct tcp_ao_key *key;
1556 struct tcp_sigpool hp;
1557 int err, pool_id;
1558 size_t size;
1559
1560 /* Force null-termination of alg_name */
1561 cmd->alg_name[ARRAY_SIZE(cmd->alg_name) - 1] = '\0';
1562
1563 /* RFC5926, 3.1.1.2. KDF_AES_128_CMAC */
1564 if (!strcmp("cmac(aes128)", algo))
1565 algo = "cmac(aes)";
1566
1567 /* Full TCP header (th->doff << 2) should fit into scratch area,
1568 * see tcp_ao_hash_header().
1569 */
1570 pool_id = tcp_sigpool_alloc_ahash(algo, 60);
1571 if (pool_id < 0)
1572 return ERR_PTR(pool_id);
1573
1574 err = tcp_sigpool_start(pool_id, &hp);
1575 if (err)
1576 goto err_free_pool;
1577
1578 tfm = crypto_ahash_reqtfm(hp.req);
1579 digest_size = crypto_ahash_digestsize(tfm);
1580 tcp_sigpool_end(&hp);
1581
1582 size = sizeof(struct tcp_ao_key) + (digest_size << 1);
1583 key = sock_kmalloc(sk, size, GFP_KERNEL);
1584 if (!key) {
1585 err = -ENOMEM;
1586 goto err_free_pool;
1587 }
1588
1589 key->tcp_sigpool_id = pool_id;
1590 key->digest_size = digest_size;
1591 return key;
1592
1593 err_free_pool:
1594 tcp_sigpool_release(pool_id);
1595 return ERR_PTR(err);
1596 }
1597
tcp_ao_add_cmd(struct sock * sk,unsigned short int family,sockptr_t optval,int optlen)1598 static int tcp_ao_add_cmd(struct sock *sk, unsigned short int family,
1599 sockptr_t optval, int optlen)
1600 {
1601 struct tcp_ao_info *ao_info;
1602 union tcp_ao_addr *addr;
1603 struct tcp_ao_key *key;
1604 struct tcp_ao_add cmd;
1605 int ret, l3index = 0;
1606 bool first = false;
1607
1608 if (optlen < sizeof(cmd))
1609 return -EINVAL;
1610
1611 ret = copy_struct_from_sockptr(&cmd, sizeof(cmd), optval, optlen);
1612 if (ret)
1613 return ret;
1614
1615 if (cmd.keylen > TCP_AO_MAXKEYLEN)
1616 return -EINVAL;
1617
1618 if (cmd.reserved != 0 || cmd.reserved2 != 0)
1619 return -EINVAL;
1620
1621 if (family == AF_INET)
1622 ret = tcp_ao_verify_ipv4(sk, &cmd, &addr);
1623 else
1624 ret = tcp_ao_verify_ipv6(sk, &cmd, &addr, &family);
1625 if (ret)
1626 return ret;
1627
1628 if (cmd.keyflags & ~TCP_AO_KEYF_ALL)
1629 return -EINVAL;
1630
1631 if (cmd.set_current || cmd.set_rnext) {
1632 if (!tcp_ao_can_set_current_rnext(sk))
1633 return -EINVAL;
1634 }
1635
1636 if (cmd.ifindex && !(cmd.keyflags & TCP_AO_KEYF_IFINDEX))
1637 return -EINVAL;
1638
1639 /* For cmd.tcp_ifindex = 0 the key will apply to the default VRF */
1640 if (cmd.keyflags & TCP_AO_KEYF_IFINDEX && cmd.ifindex) {
1641 int bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
1642 struct net_device *dev;
1643
1644 rcu_read_lock();
1645 dev = dev_get_by_index_rcu(sock_net(sk), cmd.ifindex);
1646 if (dev && netif_is_l3_master(dev))
1647 l3index = dev->ifindex;
1648 rcu_read_unlock();
1649
1650 if (!dev || !l3index)
1651 return -EINVAL;
1652
1653 if (!bound_dev_if || bound_dev_if != cmd.ifindex) {
1654 /* tcp_ao_established_key() doesn't expect having
1655 * non peer-matching key on an established TCP-AO
1656 * connection.
1657 */
1658 if (!((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)))
1659 return -EINVAL;
1660 }
1661
1662 /* It's still possible to bind after adding keys or even
1663 * re-bind to a different dev (with CAP_NET_RAW).
1664 * So, no reason to return error here, rather try to be
1665 * nice and warn the user.
1666 */
1667 if (bound_dev_if && bound_dev_if != cmd.ifindex)
1668 net_warn_ratelimited("AO key ifindex %d != sk bound ifindex %d\n",
1669 cmd.ifindex, bound_dev_if);
1670 }
1671
1672 /* Don't allow keys for peers that have a matching TCP-MD5 key */
1673 if (cmd.keyflags & TCP_AO_KEYF_IFINDEX) {
1674 /* Non-_exact version of tcp_md5_do_lookup() will
1675 * as well match keys that aren't bound to a specific VRF
1676 * (that will make them match AO key with
1677 * sysctl_tcp_l3dev_accept = 1
1678 */
1679 if (tcp_md5_do_lookup(sk, l3index, addr, family))
1680 return -EKEYREJECTED;
1681 } else {
1682 if (tcp_md5_do_lookup_any_l3index(sk, addr, family))
1683 return -EKEYREJECTED;
1684 }
1685
1686 ao_info = setsockopt_ao_info(sk);
1687 if (IS_ERR(ao_info))
1688 return PTR_ERR(ao_info);
1689
1690 if (!ao_info) {
1691 ao_info = tcp_ao_alloc_info(GFP_KERNEL);
1692 if (!ao_info)
1693 return -ENOMEM;
1694 first = true;
1695 } else {
1696 /* Check that neither RecvID nor SendID match any
1697 * existing key for the peer, RFC5925 3.1:
1698 * > The IDs of MKTs MUST NOT overlap where their
1699 * > TCP connection identifiers overlap.
1700 */
1701 if (__tcp_ao_do_lookup(sk, l3index, addr, family, cmd.prefix, -1, cmd.rcvid))
1702 return -EEXIST;
1703 if (__tcp_ao_do_lookup(sk, l3index, addr, family,
1704 cmd.prefix, cmd.sndid, -1))
1705 return -EEXIST;
1706 }
1707
1708 key = tcp_ao_key_alloc(sk, &cmd);
1709 if (IS_ERR(key)) {
1710 ret = PTR_ERR(key);
1711 goto err_free_ao;
1712 }
1713
1714 INIT_HLIST_NODE(&key->node);
1715 memcpy(&key->addr, addr, (family == AF_INET) ? sizeof(struct in_addr) :
1716 sizeof(struct in6_addr));
1717 key->prefixlen = cmd.prefix;
1718 key->family = family;
1719 key->keyflags = cmd.keyflags;
1720 key->sndid = cmd.sndid;
1721 key->rcvid = cmd.rcvid;
1722 key->l3index = l3index;
1723 atomic64_set(&key->pkt_good, 0);
1724 atomic64_set(&key->pkt_bad, 0);
1725
1726 ret = tcp_ao_parse_crypto(&cmd, key);
1727 if (ret < 0)
1728 goto err_free_sock;
1729
1730 if (!((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE))) {
1731 tcp_ao_cache_traffic_keys(sk, ao_info, key);
1732 if (first) {
1733 ao_info->current_key = key;
1734 ao_info->rnext_key = key;
1735 }
1736 }
1737
1738 tcp_ao_link_mkt(ao_info, key);
1739 if (first) {
1740 if (!static_branch_inc(&tcp_ao_needed.key)) {
1741 ret = -EUSERS;
1742 goto err_free_sock;
1743 }
1744 sk_gso_disable(sk);
1745 rcu_assign_pointer(tcp_sk(sk)->ao_info, ao_info);
1746 }
1747
1748 if (cmd.set_current)
1749 WRITE_ONCE(ao_info->current_key, key);
1750 if (cmd.set_rnext)
1751 WRITE_ONCE(ao_info->rnext_key, key);
1752 return 0;
1753
1754 err_free_sock:
1755 atomic_sub(tcp_ao_sizeof_key(key), &sk->sk_omem_alloc);
1756 tcp_sigpool_release(key->tcp_sigpool_id);
1757 kfree_sensitive(key);
1758 err_free_ao:
1759 if (first)
1760 kfree(ao_info);
1761 return ret;
1762 }
1763
tcp_ao_delete_key(struct sock * sk,struct tcp_ao_info * ao_info,bool del_async,struct tcp_ao_key * key,struct tcp_ao_key * new_current,struct tcp_ao_key * new_rnext)1764 static int tcp_ao_delete_key(struct sock *sk, struct tcp_ao_info *ao_info,
1765 bool del_async, struct tcp_ao_key *key,
1766 struct tcp_ao_key *new_current,
1767 struct tcp_ao_key *new_rnext)
1768 {
1769 int err;
1770
1771 hlist_del_rcu(&key->node);
1772
1773 /* Support for async delete on listening sockets: as they don't
1774 * need current_key/rnext_key maintaining, we don't need to check
1775 * them and we can just free all resources in RCU fashion.
1776 */
1777 if (del_async) {
1778 atomic_sub(tcp_ao_sizeof_key(key), &sk->sk_omem_alloc);
1779 call_rcu(&key->rcu, tcp_ao_key_free_rcu);
1780 return 0;
1781 }
1782
1783 /* At this moment another CPU could have looked this key up
1784 * while it was unlinked from the list. Wait for RCU grace period,
1785 * after which the key is off-list and can't be looked up again;
1786 * the rx path [just before RCU came] might have used it and set it
1787 * as current_key (very unlikely).
1788 * Free the key with next RCU grace period (in case it was
1789 * current_key before tcp_ao_current_rnext() might have
1790 * changed it in forced-delete).
1791 */
1792 synchronize_rcu();
1793 if (new_current)
1794 WRITE_ONCE(ao_info->current_key, new_current);
1795 if (new_rnext)
1796 WRITE_ONCE(ao_info->rnext_key, new_rnext);
1797
1798 if (unlikely(READ_ONCE(ao_info->current_key) == key ||
1799 READ_ONCE(ao_info->rnext_key) == key)) {
1800 err = -EBUSY;
1801 goto add_key;
1802 }
1803
1804 atomic_sub(tcp_ao_sizeof_key(key), &sk->sk_omem_alloc);
1805 call_rcu(&key->rcu, tcp_ao_key_free_rcu);
1806
1807 return 0;
1808 add_key:
1809 hlist_add_head_rcu(&key->node, &ao_info->head);
1810 return err;
1811 }
1812
1813 #define TCP_AO_DEL_KEYF_ALL (TCP_AO_KEYF_IFINDEX)
tcp_ao_del_cmd(struct sock * sk,unsigned short int family,sockptr_t optval,int optlen)1814 static int tcp_ao_del_cmd(struct sock *sk, unsigned short int family,
1815 sockptr_t optval, int optlen)
1816 {
1817 struct tcp_ao_key *key, *new_current = NULL, *new_rnext = NULL;
1818 int err, addr_len, l3index = 0;
1819 struct tcp_ao_info *ao_info;
1820 union tcp_ao_addr *addr;
1821 struct tcp_ao_del cmd;
1822 __u8 prefix;
1823 u16 port;
1824
1825 if (optlen < sizeof(cmd))
1826 return -EINVAL;
1827
1828 err = copy_struct_from_sockptr(&cmd, sizeof(cmd), optval, optlen);
1829 if (err)
1830 return err;
1831
1832 if (cmd.reserved != 0 || cmd.reserved2 != 0)
1833 return -EINVAL;
1834
1835 if (cmd.set_current || cmd.set_rnext) {
1836 if (!tcp_ao_can_set_current_rnext(sk))
1837 return -EINVAL;
1838 }
1839
1840 if (cmd.keyflags & ~TCP_AO_DEL_KEYF_ALL)
1841 return -EINVAL;
1842
1843 /* No sanity check for TCP_AO_KEYF_IFINDEX as if a VRF
1844 * was destroyed, there still should be a way to delete keys,
1845 * that were bound to that l3intf. So, fail late at lookup stage
1846 * if there is no key for that ifindex.
1847 */
1848 if (cmd.ifindex && !(cmd.keyflags & TCP_AO_KEYF_IFINDEX))
1849 return -EINVAL;
1850
1851 ao_info = setsockopt_ao_info(sk);
1852 if (IS_ERR(ao_info))
1853 return PTR_ERR(ao_info);
1854 if (!ao_info)
1855 return -ENOENT;
1856
1857 /* For sockets in TCP_CLOSED it's possible set keys that aren't
1858 * matching the future peer (address/VRF/etc),
1859 * tcp_ao_connect_init() will choose a correct matching MKT
1860 * if there's any.
1861 */
1862 if (cmd.set_current) {
1863 new_current = tcp_ao_established_key(sk, ao_info, cmd.current_key, -1);
1864 if (!new_current)
1865 return -ENOENT;
1866 }
1867 if (cmd.set_rnext) {
1868 new_rnext = tcp_ao_established_key(sk, ao_info, -1, cmd.rnext);
1869 if (!new_rnext)
1870 return -ENOENT;
1871 }
1872 if (cmd.del_async && sk->sk_state != TCP_LISTEN)
1873 return -EINVAL;
1874
1875 if (family == AF_INET) {
1876 struct sockaddr_in *sin = (struct sockaddr_in *)&cmd.addr;
1877
1878 addr = (union tcp_ao_addr *)&sin->sin_addr;
1879 addr_len = sizeof(struct in_addr);
1880 port = ntohs(sin->sin_port);
1881 } else {
1882 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&cmd.addr;
1883 struct in6_addr *addr6 = &sin6->sin6_addr;
1884
1885 if (ipv6_addr_v4mapped(addr6)) {
1886 addr = (union tcp_ao_addr *)&addr6->s6_addr32[3];
1887 addr_len = sizeof(struct in_addr);
1888 family = AF_INET;
1889 } else {
1890 addr = (union tcp_ao_addr *)addr6;
1891 addr_len = sizeof(struct in6_addr);
1892 }
1893 port = ntohs(sin6->sin6_port);
1894 }
1895 prefix = cmd.prefix;
1896
1897 /* Currently matching is not performed on port (or port ranges) */
1898 if (port != 0)
1899 return -EINVAL;
1900
1901 /* We could choose random present key here for current/rnext
1902 * but that's less predictable. Let's be strict and don't
1903 * allow removing a key that's in use. RFC5925 doesn't
1904 * specify how-to coordinate key removal, but says:
1905 * "It is presumed that an MKT affecting a particular
1906 * connection cannot be destroyed during an active connection"
1907 */
1908 hlist_for_each_entry_rcu(key, &ao_info->head, node,
1909 lockdep_sock_is_held(sk)) {
1910 if (cmd.sndid != key->sndid ||
1911 cmd.rcvid != key->rcvid)
1912 continue;
1913
1914 if (family != key->family ||
1915 prefix != key->prefixlen ||
1916 memcmp(addr, &key->addr, addr_len))
1917 continue;
1918
1919 if ((cmd.keyflags & TCP_AO_KEYF_IFINDEX) !=
1920 (key->keyflags & TCP_AO_KEYF_IFINDEX))
1921 continue;
1922
1923 if (key->l3index != l3index)
1924 continue;
1925
1926 if (key == new_current || key == new_rnext)
1927 continue;
1928
1929 return tcp_ao_delete_key(sk, ao_info, cmd.del_async, key,
1930 new_current, new_rnext);
1931 }
1932 return -ENOENT;
1933 }
1934
1935 /* cmd.ao_required makes a socket TCP-AO only.
1936 * Don't allow any md5 keys for any l3intf on the socket together with it.
1937 * Restricting it early in setsockopt() removes a check for
1938 * ao_info->ao_required on inbound tcp segment fast-path.
1939 */
tcp_ao_required_verify(struct sock * sk)1940 static int tcp_ao_required_verify(struct sock *sk)
1941 {
1942 #ifdef CONFIG_TCP_MD5SIG
1943 const struct tcp_md5sig_info *md5sig;
1944
1945 if (!static_branch_unlikely(&tcp_md5_needed.key))
1946 return 0;
1947
1948 md5sig = rcu_dereference_check(tcp_sk(sk)->md5sig_info,
1949 lockdep_sock_is_held(sk));
1950 if (!md5sig)
1951 return 0;
1952
1953 if (rcu_dereference_check(hlist_first_rcu(&md5sig->head),
1954 lockdep_sock_is_held(sk)))
1955 return 1;
1956 #endif
1957 return 0;
1958 }
1959
tcp_ao_info_cmd(struct sock * sk,unsigned short int family,sockptr_t optval,int optlen)1960 static int tcp_ao_info_cmd(struct sock *sk, unsigned short int family,
1961 sockptr_t optval, int optlen)
1962 {
1963 struct tcp_ao_key *new_current = NULL, *new_rnext = NULL;
1964 struct tcp_ao_info *ao_info;
1965 struct tcp_ao_info_opt cmd;
1966 bool first = false;
1967 int err;
1968
1969 if (optlen < sizeof(cmd))
1970 return -EINVAL;
1971
1972 err = copy_struct_from_sockptr(&cmd, sizeof(cmd), optval, optlen);
1973 if (err)
1974 return err;
1975
1976 if (cmd.set_current || cmd.set_rnext) {
1977 if (!tcp_ao_can_set_current_rnext(sk))
1978 return -EINVAL;
1979 }
1980
1981 if (cmd.reserved != 0 || cmd.reserved2 != 0)
1982 return -EINVAL;
1983
1984 ao_info = setsockopt_ao_info(sk);
1985 if (IS_ERR(ao_info))
1986 return PTR_ERR(ao_info);
1987 if (!ao_info) {
1988 if (!((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)))
1989 return -EINVAL;
1990 ao_info = tcp_ao_alloc_info(GFP_KERNEL);
1991 if (!ao_info)
1992 return -ENOMEM;
1993 first = true;
1994 }
1995
1996 if (cmd.ao_required && tcp_ao_required_verify(sk)) {
1997 err = -EKEYREJECTED;
1998 goto out;
1999 }
2000
2001 /* For sockets in TCP_CLOSED it's possible set keys that aren't
2002 * matching the future peer (address/port/VRF/etc),
2003 * tcp_ao_connect_init() will choose a correct matching MKT
2004 * if there's any.
2005 */
2006 if (cmd.set_current) {
2007 new_current = tcp_ao_established_key(sk, ao_info, cmd.current_key, -1);
2008 if (!new_current) {
2009 err = -ENOENT;
2010 goto out;
2011 }
2012 }
2013 if (cmd.set_rnext) {
2014 new_rnext = tcp_ao_established_key(sk, ao_info, -1, cmd.rnext);
2015 if (!new_rnext) {
2016 err = -ENOENT;
2017 goto out;
2018 }
2019 }
2020 if (cmd.set_counters) {
2021 atomic64_set(&ao_info->counters.pkt_good, cmd.pkt_good);
2022 atomic64_set(&ao_info->counters.pkt_bad, cmd.pkt_bad);
2023 atomic64_set(&ao_info->counters.key_not_found, cmd.pkt_key_not_found);
2024 atomic64_set(&ao_info->counters.ao_required, cmd.pkt_ao_required);
2025 atomic64_set(&ao_info->counters.dropped_icmp, cmd.pkt_dropped_icmp);
2026 }
2027
2028 ao_info->ao_required = cmd.ao_required;
2029 ao_info->accept_icmps = cmd.accept_icmps;
2030 if (new_current)
2031 WRITE_ONCE(ao_info->current_key, new_current);
2032 if (new_rnext)
2033 WRITE_ONCE(ao_info->rnext_key, new_rnext);
2034 if (first) {
2035 if (!static_branch_inc(&tcp_ao_needed.key)) {
2036 err = -EUSERS;
2037 goto out;
2038 }
2039 sk_gso_disable(sk);
2040 rcu_assign_pointer(tcp_sk(sk)->ao_info, ao_info);
2041 }
2042 return 0;
2043 out:
2044 if (first)
2045 kfree(ao_info);
2046 return err;
2047 }
2048
tcp_parse_ao(struct sock * sk,int cmd,unsigned short int family,sockptr_t optval,int optlen)2049 int tcp_parse_ao(struct sock *sk, int cmd, unsigned short int family,
2050 sockptr_t optval, int optlen)
2051 {
2052 if (WARN_ON_ONCE(family != AF_INET && family != AF_INET6))
2053 return -EAFNOSUPPORT;
2054
2055 switch (cmd) {
2056 case TCP_AO_ADD_KEY:
2057 return tcp_ao_add_cmd(sk, family, optval, optlen);
2058 case TCP_AO_DEL_KEY:
2059 return tcp_ao_del_cmd(sk, family, optval, optlen);
2060 case TCP_AO_INFO:
2061 return tcp_ao_info_cmd(sk, family, optval, optlen);
2062 default:
2063 WARN_ON_ONCE(1);
2064 return -EINVAL;
2065 }
2066 }
2067
tcp_v4_parse_ao(struct sock * sk,int cmd,sockptr_t optval,int optlen)2068 int tcp_v4_parse_ao(struct sock *sk, int cmd, sockptr_t optval, int optlen)
2069 {
2070 return tcp_parse_ao(sk, cmd, AF_INET, optval, optlen);
2071 }
2072
2073 /* tcp_ao_copy_mkts_to_user(ao_info, optval, optlen)
2074 *
2075 * @ao_info: struct tcp_ao_info on the socket that
2076 * socket getsockopt(TCP_AO_GET_KEYS) is executed on
2077 * @optval: pointer to array of tcp_ao_getsockopt structures in user space.
2078 * Must be != NULL.
2079 * @optlen: pointer to size of tcp_ao_getsockopt structure.
2080 * Must be != NULL.
2081 *
2082 * Return value: 0 on success, a negative error number otherwise.
2083 *
2084 * optval points to an array of tcp_ao_getsockopt structures in user space.
2085 * optval[0] is used as both input and output to getsockopt. It determines
2086 * which keys are returned by the kernel.
2087 * optval[0].nkeys is the size of the array in user space. On return it contains
2088 * the number of keys matching the search criteria.
2089 * If tcp_ao_getsockopt::get_all is set, then all keys in the socket are
2090 * returned, otherwise only keys matching <addr, prefix, sndid, rcvid>
2091 * in optval[0] are returned.
2092 * optlen is also used as both input and output. The user provides the size
2093 * of struct tcp_ao_getsockopt in user space, and the kernel returns the size
2094 * of the structure in kernel space.
2095 * The size of struct tcp_ao_getsockopt may differ between user and kernel.
2096 * There are three cases to consider:
2097 * * If usize == ksize, then keys are copied verbatim.
2098 * * If usize < ksize, then the userspace has passed an old struct to a
2099 * newer kernel. The rest of the trailing bytes in optval[0]
2100 * (ksize - usize) are interpreted as 0 by the kernel.
2101 * * If usize > ksize, then the userspace has passed a new struct to an
2102 * older kernel. The trailing bytes unknown to the kernel (usize - ksize)
2103 * are checked to ensure they are zeroed, otherwise -E2BIG is returned.
2104 * On return the kernel fills in min(usize, ksize) in each entry of the array.
2105 * The layout of the fields in the user and kernel structures is expected to
2106 * be the same (including in the 32bit vs 64bit case).
2107 */
tcp_ao_copy_mkts_to_user(const struct sock * sk,struct tcp_ao_info * ao_info,sockptr_t optval,sockptr_t optlen)2108 static int tcp_ao_copy_mkts_to_user(const struct sock *sk,
2109 struct tcp_ao_info *ao_info,
2110 sockptr_t optval, sockptr_t optlen)
2111 {
2112 struct tcp_ao_getsockopt opt_in, opt_out;
2113 struct tcp_ao_key *key, *current_key;
2114 bool do_address_matching = true;
2115 union tcp_ao_addr *addr = NULL;
2116 int err, l3index, user_len;
2117 unsigned int max_keys; /* maximum number of keys to copy to user */
2118 size_t out_offset = 0;
2119 size_t bytes_to_write; /* number of bytes to write to user level */
2120 u32 matched_keys; /* keys from ao_info matched so far */
2121 int optlen_out;
2122 __be16 port = 0;
2123
2124 if (copy_from_sockptr(&user_len, optlen, sizeof(int)))
2125 return -EFAULT;
2126
2127 if (user_len <= 0)
2128 return -EINVAL;
2129
2130 memset(&opt_in, 0, sizeof(struct tcp_ao_getsockopt));
2131 err = copy_struct_from_sockptr(&opt_in, sizeof(opt_in),
2132 optval, user_len);
2133 if (err < 0)
2134 return err;
2135
2136 if (opt_in.pkt_good || opt_in.pkt_bad)
2137 return -EINVAL;
2138 if (opt_in.keyflags & ~TCP_AO_GET_KEYF_VALID)
2139 return -EINVAL;
2140 if (opt_in.ifindex && !(opt_in.keyflags & TCP_AO_KEYF_IFINDEX))
2141 return -EINVAL;
2142
2143 if (opt_in.reserved != 0)
2144 return -EINVAL;
2145
2146 max_keys = opt_in.nkeys;
2147 l3index = (opt_in.keyflags & TCP_AO_KEYF_IFINDEX) ? opt_in.ifindex : -1;
2148
2149 if (opt_in.get_all || opt_in.is_current || opt_in.is_rnext) {
2150 if (opt_in.get_all && (opt_in.is_current || opt_in.is_rnext))
2151 return -EINVAL;
2152 do_address_matching = false;
2153 }
2154
2155 switch (opt_in.addr.ss_family) {
2156 case AF_INET: {
2157 struct sockaddr_in *sin;
2158 __be32 mask;
2159
2160 sin = (struct sockaddr_in *)&opt_in.addr;
2161 port = sin->sin_port;
2162 addr = (union tcp_ao_addr *)&sin->sin_addr;
2163
2164 if (opt_in.prefix > 32)
2165 return -EINVAL;
2166
2167 if (ntohl(sin->sin_addr.s_addr) == INADDR_ANY &&
2168 opt_in.prefix != 0)
2169 return -EINVAL;
2170
2171 mask = inet_make_mask(opt_in.prefix);
2172 if (sin->sin_addr.s_addr & ~mask)
2173 return -EINVAL;
2174
2175 break;
2176 }
2177 case AF_INET6: {
2178 struct sockaddr_in6 *sin6;
2179 struct in6_addr *addr6;
2180
2181 sin6 = (struct sockaddr_in6 *)&opt_in.addr;
2182 addr = (union tcp_ao_addr *)&sin6->sin6_addr;
2183 addr6 = &sin6->sin6_addr;
2184 port = sin6->sin6_port;
2185
2186 /* We don't have to change family and @addr here if
2187 * ipv6_addr_v4mapped() like in key adding:
2188 * tcp_ao_key_cmp() does it. Do the sanity checks though.
2189 */
2190 if (opt_in.prefix != 0) {
2191 if (ipv6_addr_v4mapped(addr6)) {
2192 __be32 mask, addr4 = addr6->s6_addr32[3];
2193
2194 if (opt_in.prefix > 32 ||
2195 ntohl(addr4) == INADDR_ANY)
2196 return -EINVAL;
2197 mask = inet_make_mask(opt_in.prefix);
2198 if (addr4 & ~mask)
2199 return -EINVAL;
2200 } else {
2201 struct in6_addr pfx;
2202
2203 if (ipv6_addr_any(addr6) ||
2204 opt_in.prefix > 128)
2205 return -EINVAL;
2206
2207 ipv6_addr_prefix(&pfx, addr6, opt_in.prefix);
2208 if (ipv6_addr_cmp(&pfx, addr6))
2209 return -EINVAL;
2210 }
2211 } else if (!ipv6_addr_any(addr6)) {
2212 return -EINVAL;
2213 }
2214 break;
2215 }
2216 case 0:
2217 if (!do_address_matching)
2218 break;
2219 fallthrough;
2220 default:
2221 return -EAFNOSUPPORT;
2222 }
2223
2224 if (!do_address_matching) {
2225 /* We could just ignore those, but let's do stricter checks */
2226 if (addr || port)
2227 return -EINVAL;
2228 if (opt_in.prefix || opt_in.sndid || opt_in.rcvid)
2229 return -EINVAL;
2230 }
2231
2232 bytes_to_write = min_t(int, user_len, sizeof(struct tcp_ao_getsockopt));
2233 matched_keys = 0;
2234 /* May change in RX, while we're dumping, pre-fetch it */
2235 current_key = READ_ONCE(ao_info->current_key);
2236
2237 hlist_for_each_entry_rcu(key, &ao_info->head, node,
2238 lockdep_sock_is_held(sk)) {
2239 if (opt_in.get_all)
2240 goto match;
2241
2242 if (opt_in.is_current || opt_in.is_rnext) {
2243 if (opt_in.is_current && key == current_key)
2244 goto match;
2245 if (opt_in.is_rnext && key == ao_info->rnext_key)
2246 goto match;
2247 continue;
2248 }
2249
2250 if (tcp_ao_key_cmp(key, l3index, addr, opt_in.prefix,
2251 opt_in.addr.ss_family,
2252 opt_in.sndid, opt_in.rcvid) != 0)
2253 continue;
2254 match:
2255 matched_keys++;
2256 if (matched_keys > max_keys)
2257 continue;
2258
2259 memset(&opt_out, 0, sizeof(struct tcp_ao_getsockopt));
2260
2261 if (key->family == AF_INET) {
2262 struct sockaddr_in *sin_out = (struct sockaddr_in *)&opt_out.addr;
2263
2264 sin_out->sin_family = key->family;
2265 sin_out->sin_port = 0;
2266 memcpy(&sin_out->sin_addr, &key->addr, sizeof(struct in_addr));
2267 } else {
2268 struct sockaddr_in6 *sin6_out = (struct sockaddr_in6 *)&opt_out.addr;
2269
2270 sin6_out->sin6_family = key->family;
2271 sin6_out->sin6_port = 0;
2272 memcpy(&sin6_out->sin6_addr, &key->addr, sizeof(struct in6_addr));
2273 }
2274 opt_out.sndid = key->sndid;
2275 opt_out.rcvid = key->rcvid;
2276 opt_out.prefix = key->prefixlen;
2277 opt_out.keyflags = key->keyflags;
2278 opt_out.is_current = (key == current_key);
2279 opt_out.is_rnext = (key == ao_info->rnext_key);
2280 opt_out.nkeys = 0;
2281 opt_out.maclen = key->maclen;
2282 opt_out.keylen = key->keylen;
2283 opt_out.ifindex = key->l3index;
2284 opt_out.pkt_good = atomic64_read(&key->pkt_good);
2285 opt_out.pkt_bad = atomic64_read(&key->pkt_bad);
2286 memcpy(&opt_out.key, key->key, key->keylen);
2287 tcp_sigpool_algo(key->tcp_sigpool_id, opt_out.alg_name, 64);
2288
2289 /* Copy key to user */
2290 if (copy_to_sockptr_offset(optval, out_offset,
2291 &opt_out, bytes_to_write))
2292 return -EFAULT;
2293 out_offset += user_len;
2294 }
2295
2296 optlen_out = (int)sizeof(struct tcp_ao_getsockopt);
2297 if (copy_to_sockptr(optlen, &optlen_out, sizeof(int)))
2298 return -EFAULT;
2299
2300 out_offset = offsetof(struct tcp_ao_getsockopt, nkeys);
2301 if (copy_to_sockptr_offset(optval, out_offset,
2302 &matched_keys, sizeof(u32)))
2303 return -EFAULT;
2304
2305 return 0;
2306 }
2307
tcp_ao_get_mkts(struct sock * sk,sockptr_t optval,sockptr_t optlen)2308 int tcp_ao_get_mkts(struct sock *sk, sockptr_t optval, sockptr_t optlen)
2309 {
2310 struct tcp_ao_info *ao_info;
2311
2312 ao_info = setsockopt_ao_info(sk);
2313 if (IS_ERR(ao_info))
2314 return PTR_ERR(ao_info);
2315 if (!ao_info)
2316 return -ENOENT;
2317
2318 return tcp_ao_copy_mkts_to_user(sk, ao_info, optval, optlen);
2319 }
2320
tcp_ao_get_sock_info(struct sock * sk,sockptr_t optval,sockptr_t optlen)2321 int tcp_ao_get_sock_info(struct sock *sk, sockptr_t optval, sockptr_t optlen)
2322 {
2323 struct tcp_ao_info_opt out, in = {};
2324 struct tcp_ao_key *current_key;
2325 struct tcp_ao_info *ao;
2326 int err, len;
2327
2328 if (copy_from_sockptr(&len, optlen, sizeof(int)))
2329 return -EFAULT;
2330
2331 if (len <= 0)
2332 return -EINVAL;
2333
2334 /* Copying this "in" only to check ::reserved, ::reserved2,
2335 * that may be needed to extend (struct tcp_ao_info_opt) and
2336 * what getsockopt() provides in future.
2337 */
2338 err = copy_struct_from_sockptr(&in, sizeof(in), optval, len);
2339 if (err)
2340 return err;
2341
2342 if (in.reserved != 0 || in.reserved2 != 0)
2343 return -EINVAL;
2344
2345 ao = setsockopt_ao_info(sk);
2346 if (IS_ERR(ao))
2347 return PTR_ERR(ao);
2348 if (!ao)
2349 return -ENOENT;
2350
2351 memset(&out, 0, sizeof(out));
2352 out.ao_required = ao->ao_required;
2353 out.accept_icmps = ao->accept_icmps;
2354 out.pkt_good = atomic64_read(&ao->counters.pkt_good);
2355 out.pkt_bad = atomic64_read(&ao->counters.pkt_bad);
2356 out.pkt_key_not_found = atomic64_read(&ao->counters.key_not_found);
2357 out.pkt_ao_required = atomic64_read(&ao->counters.ao_required);
2358 out.pkt_dropped_icmp = atomic64_read(&ao->counters.dropped_icmp);
2359
2360 current_key = READ_ONCE(ao->current_key);
2361 if (current_key) {
2362 out.set_current = 1;
2363 out.current_key = current_key->sndid;
2364 }
2365 if (ao->rnext_key) {
2366 out.set_rnext = 1;
2367 out.rnext = ao->rnext_key->rcvid;
2368 }
2369
2370 if (copy_to_sockptr(optval, &out, min_t(int, len, sizeof(out))))
2371 return -EFAULT;
2372
2373 return 0;
2374 }
2375
tcp_ao_set_repair(struct sock * sk,sockptr_t optval,unsigned int optlen)2376 int tcp_ao_set_repair(struct sock *sk, sockptr_t optval, unsigned int optlen)
2377 {
2378 struct tcp_sock *tp = tcp_sk(sk);
2379 struct tcp_ao_repair cmd;
2380 struct tcp_ao_key *key;
2381 struct tcp_ao_info *ao;
2382 int err;
2383
2384 if (optlen < sizeof(cmd))
2385 return -EINVAL;
2386
2387 err = copy_struct_from_sockptr(&cmd, sizeof(cmd), optval, optlen);
2388 if (err)
2389 return err;
2390
2391 if (!tp->repair)
2392 return -EPERM;
2393
2394 ao = setsockopt_ao_info(sk);
2395 if (IS_ERR(ao))
2396 return PTR_ERR(ao);
2397 if (!ao)
2398 return -ENOENT;
2399
2400 WRITE_ONCE(ao->lisn, cmd.snt_isn);
2401 WRITE_ONCE(ao->risn, cmd.rcv_isn);
2402 WRITE_ONCE(ao->snd_sne, cmd.snd_sne);
2403 WRITE_ONCE(ao->rcv_sne, cmd.rcv_sne);
2404
2405 hlist_for_each_entry_rcu(key, &ao->head, node, lockdep_sock_is_held(sk))
2406 tcp_ao_cache_traffic_keys(sk, ao, key);
2407
2408 return 0;
2409 }
2410
tcp_ao_get_repair(struct sock * sk,sockptr_t optval,sockptr_t optlen)2411 int tcp_ao_get_repair(struct sock *sk, sockptr_t optval, sockptr_t optlen)
2412 {
2413 struct tcp_sock *tp = tcp_sk(sk);
2414 struct tcp_ao_repair opt;
2415 struct tcp_ao_info *ao;
2416 int len;
2417
2418 if (copy_from_sockptr(&len, optlen, sizeof(int)))
2419 return -EFAULT;
2420
2421 if (len <= 0)
2422 return -EINVAL;
2423
2424 if (!tp->repair)
2425 return -EPERM;
2426
2427 rcu_read_lock();
2428 ao = getsockopt_ao_info(sk);
2429 if (IS_ERR_OR_NULL(ao)) {
2430 rcu_read_unlock();
2431 return ao ? PTR_ERR(ao) : -ENOENT;
2432 }
2433
2434 opt.snt_isn = ao->lisn;
2435 opt.rcv_isn = ao->risn;
2436 opt.snd_sne = READ_ONCE(ao->snd_sne);
2437 opt.rcv_sne = READ_ONCE(ao->rcv_sne);
2438 rcu_read_unlock();
2439
2440 if (copy_to_sockptr(optval, &opt, min_t(int, len, sizeof(opt))))
2441 return -EFAULT;
2442 return 0;
2443 }
2444