1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * INET An implementation of the TCP Authentication Option (TCP-AO).
10 #define pr_fmt(fmt) "TCP: " fmt
12 #include <crypto/hash.h>
13 #include <linux/inetdevice.h>
14 #include <linux/tcp.h>
19 #include <trace/events/tcp.h>
21 DEFINE_STATIC_KEY_DEFERRED_FALSE(tcp_ao_needed, HZ);
23 int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8 *key, void *ctx,
24 unsigned int len, struct tcp_sigpool *hp)
26 struct scatterlist sg;
29 if (crypto_ahash_setkey(crypto_ahash_reqtfm(hp->req),
30 mkt->key, mkt->keylen))
33 ret = crypto_ahash_init(hp->req);
37 sg_init_one(&sg, ctx, len);
38 ahash_request_set_crypt(hp->req, &sg, key, len);
39 crypto_ahash_update(hp->req);
41 ret = crypto_ahash_final(hp->req);
47 memset(key, 0, tcp_ao_digest_size(mkt));
51 bool tcp_ao_ignore_icmp(const struct sock *sk, int family, int type, int code)
53 bool ignore_icmp = false;
54 struct tcp_ao_info *ao;
56 if (!static_branch_unlikely(&tcp_ao_needed.key))
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.
68 if (family == AF_INET) {
69 if (type != ICMP_DEST_UNREACH)
71 if (code < ICMP_PROT_UNREACH || code > ICMP_FRAG_NEEDED)
74 if (type != ICMPV6_DEST_UNREACH)
76 if (code != ICMPV6_ADM_PROHIBITED && code != ICMPV6_PORT_UNREACH)
81 switch (sk->sk_state) {
83 ao = rcu_dereference(tcp_twsk(sk)->ao_info);
88 case TCP_NEW_SYN_RECV:
89 /* RFC5925 specifies to ignore ICMPs *only* on connections
90 * in synchronized states.
95 ao = rcu_dereference(tcp_sk(sk)->ao_info);
98 if (ao && !ao->accept_icmps) {
100 __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAODROPPEDICMPS);
101 atomic64_inc(&ao->counters.dropped_icmp);
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.
112 struct tcp_ao_key *tcp_ao_established_key(struct tcp_ao_info *ao,
113 int sndid, int rcvid)
115 struct tcp_ao_key *key;
117 hlist_for_each_entry_rcu(key, &ao->head, node) {
118 if ((sndid >= 0 && key->sndid != sndid) ||
119 (rcvid >= 0 && key->rcvid != rcvid))
127 static int ipv4_prefix_cmp(const struct in_addr *addr1,
128 const struct in_addr *addr2,
129 unsigned int prefixlen)
131 __be32 mask = inet_make_mask(prefixlen);
132 __be32 a1 = addr1->s_addr & mask;
133 __be32 a2 = addr2->s_addr & mask;
137 return memcmp(&a1, &a2, sizeof(a1));
140 static int __tcp_ao_key_cmp(const struct tcp_ao_key *key, int l3index,
141 const union tcp_ao_addr *addr, u8 prefixlen,
142 int family, int sndid, int rcvid)
144 if (sndid >= 0 && key->sndid != sndid)
145 return (key->sndid > sndid) ? 1 : -1;
146 if (rcvid >= 0 && key->rcvid != rcvid)
147 return (key->rcvid > rcvid) ? 1 : -1;
148 if (l3index >= 0 && (key->keyflags & TCP_AO_KEYF_IFINDEX)) {
149 if (key->l3index != l3index)
150 return (key->l3index > l3index) ? 1 : -1;
153 if (family == AF_UNSPEC)
155 if (key->family != family)
156 return (key->family > family) ? 1 : -1;
158 if (family == AF_INET) {
159 if (ntohl(key->addr.a4.s_addr) == INADDR_ANY)
161 if (ntohl(addr->a4.s_addr) == INADDR_ANY)
163 return ipv4_prefix_cmp(&key->addr.a4, &addr->a4, prefixlen);
164 #if IS_ENABLED(CONFIG_IPV6)
166 if (ipv6_addr_any(&key->addr.a6) || ipv6_addr_any(&addr->a6))
168 if (ipv6_prefix_equal(&key->addr.a6, &addr->a6, prefixlen))
170 return memcmp(&key->addr.a6, &addr->a6, sizeof(addr->a6));
176 static int tcp_ao_key_cmp(const struct tcp_ao_key *key, int l3index,
177 const union tcp_ao_addr *addr, u8 prefixlen,
178 int family, int sndid, int rcvid)
180 #if IS_ENABLED(CONFIG_IPV6)
181 if (family == AF_INET6 && ipv6_addr_v4mapped(&addr->a6)) {
182 __be32 addr4 = addr->a6.s6_addr32[3];
184 return __tcp_ao_key_cmp(key, l3index,
185 (union tcp_ao_addr *)&addr4,
186 prefixlen, AF_INET, sndid, rcvid);
189 return __tcp_ao_key_cmp(key, l3index, addr,
190 prefixlen, family, sndid, rcvid);
193 static struct tcp_ao_key *__tcp_ao_do_lookup(const struct sock *sk, int l3index,
194 const union tcp_ao_addr *addr, int family, u8 prefix,
195 int sndid, int rcvid)
197 struct tcp_ao_key *key;
198 struct tcp_ao_info *ao;
200 if (!static_branch_unlikely(&tcp_ao_needed.key))
203 ao = rcu_dereference_check(tcp_sk(sk)->ao_info,
204 lockdep_sock_is_held(sk));
208 hlist_for_each_entry_rcu(key, &ao->head, node) {
209 u8 prefixlen = min(prefix, key->prefixlen);
211 if (!tcp_ao_key_cmp(key, l3index, addr, prefixlen,
212 family, sndid, rcvid))
218 struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk, int l3index,
219 const union tcp_ao_addr *addr,
220 int family, int sndid, int rcvid)
222 return __tcp_ao_do_lookup(sk, l3index, addr, family, U8_MAX, sndid, rcvid);
225 static struct tcp_ao_info *tcp_ao_alloc_info(gfp_t flags)
227 struct tcp_ao_info *ao;
229 ao = kzalloc(sizeof(*ao), flags);
232 INIT_HLIST_HEAD(&ao->head);
233 refcount_set(&ao->refcnt, 1);
238 static void tcp_ao_link_mkt(struct tcp_ao_info *ao, struct tcp_ao_key *mkt)
240 hlist_add_head_rcu(&mkt->node, &ao->head);
243 static struct tcp_ao_key *tcp_ao_copy_key(struct sock *sk,
244 struct tcp_ao_key *key)
246 struct tcp_ao_key *new_key;
248 new_key = sock_kmalloc(sk, tcp_ao_sizeof_key(key),
254 INIT_HLIST_NODE(&new_key->node);
255 tcp_sigpool_get(new_key->tcp_sigpool_id);
256 atomic64_set(&new_key->pkt_good, 0);
257 atomic64_set(&new_key->pkt_bad, 0);
262 static void tcp_ao_key_free_rcu(struct rcu_head *head)
264 struct tcp_ao_key *key = container_of(head, struct tcp_ao_key, rcu);
266 tcp_sigpool_release(key->tcp_sigpool_id);
267 kfree_sensitive(key);
270 static void tcp_ao_info_free_rcu(struct rcu_head *head)
272 struct tcp_ao_info *ao = container_of(head, struct tcp_ao_info, rcu);
273 struct tcp_ao_key *key;
274 struct hlist_node *n;
276 hlist_for_each_entry_safe(key, n, &ao->head, node) {
277 hlist_del(&key->node);
278 tcp_sigpool_release(key->tcp_sigpool_id);
279 kfree_sensitive(key);
282 static_branch_slow_dec_deferred(&tcp_ao_needed);
285 static void tcp_ao_sk_omem_free(struct sock *sk, struct tcp_ao_info *ao)
287 size_t total_ao_sk_mem = 0;
288 struct tcp_ao_key *key;
290 hlist_for_each_entry(key, &ao->head, node)
291 total_ao_sk_mem += tcp_ao_sizeof_key(key);
292 atomic_sub(total_ao_sk_mem, &sk->sk_omem_alloc);
295 void tcp_ao_destroy_sock(struct sock *sk, bool twsk)
297 struct tcp_ao_info *ao;
300 ao = rcu_dereference_protected(tcp_twsk(sk)->ao_info, 1);
301 rcu_assign_pointer(tcp_twsk(sk)->ao_info, NULL);
303 ao = rcu_dereference_protected(tcp_sk(sk)->ao_info, 1);
304 rcu_assign_pointer(tcp_sk(sk)->ao_info, NULL);
307 if (!ao || !refcount_dec_and_test(&ao->refcnt))
311 tcp_ao_sk_omem_free(sk, ao);
312 call_rcu(&ao->rcu, tcp_ao_info_free_rcu);
315 void tcp_ao_time_wait(struct tcp_timewait_sock *tcptw, struct tcp_sock *tp)
317 struct tcp_ao_info *ao_info = rcu_dereference_protected(tp->ao_info, 1);
320 struct tcp_ao_key *key;
321 struct hlist_node *n;
324 hlist_for_each_entry_safe(key, n, &ao_info->head, node) {
325 omem += tcp_ao_sizeof_key(key);
328 refcount_inc(&ao_info->refcnt);
329 atomic_sub(omem, &(((struct sock *)tp)->sk_omem_alloc));
330 rcu_assign_pointer(tcptw->ao_info, ao_info);
332 tcptw->ao_info = NULL;
336 /* 4 tuple and ISNs are expected in NBO */
337 static int tcp_v4_ao_calc_key(struct tcp_ao_key *mkt, u8 *key,
338 __be32 saddr, __be32 daddr,
339 __be16 sport, __be16 dport,
340 __be32 sisn, __be32 disn)
342 /* See RFC5926 3.1.1 */
343 struct kdf_input_block {
346 struct tcp4_ao_context ctx;
349 struct tcp_sigpool hp;
352 err = tcp_sigpool_start(mkt->tcp_sigpool_id, &hp);
358 memcpy(tmp->label, "TCP-AO", 6);
359 tmp->ctx.saddr = saddr;
360 tmp->ctx.daddr = daddr;
361 tmp->ctx.sport = sport;
362 tmp->ctx.dport = dport;
363 tmp->ctx.sisn = sisn;
364 tmp->ctx.disn = disn;
365 tmp->outlen = htons(tcp_ao_digest_size(mkt) * 8); /* in bits */
367 err = tcp_ao_calc_traffic_key(mkt, key, tmp, sizeof(*tmp), &hp);
368 tcp_sigpool_end(&hp);
373 int tcp_v4_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key,
374 const struct sock *sk,
375 __be32 sisn, __be32 disn, bool send)
378 return tcp_v4_ao_calc_key(mkt, key, sk->sk_rcv_saddr,
379 sk->sk_daddr, htons(sk->sk_num),
380 sk->sk_dport, sisn, disn);
382 return tcp_v4_ao_calc_key(mkt, key, sk->sk_daddr,
383 sk->sk_rcv_saddr, sk->sk_dport,
384 htons(sk->sk_num), disn, sisn);
387 static int tcp_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key,
388 const struct sock *sk,
389 __be32 sisn, __be32 disn, bool send)
391 if (mkt->family == AF_INET)
392 return tcp_v4_ao_calc_key_sk(mkt, key, sk, sisn, disn, send);
393 #if IS_ENABLED(CONFIG_IPV6)
394 else if (mkt->family == AF_INET6)
395 return tcp_v6_ao_calc_key_sk(mkt, key, sk, sisn, disn, send);
401 int tcp_v4_ao_calc_key_rsk(struct tcp_ao_key *mkt, u8 *key,
402 struct request_sock *req)
404 struct inet_request_sock *ireq = inet_rsk(req);
406 return tcp_v4_ao_calc_key(mkt, key,
407 ireq->ir_loc_addr, ireq->ir_rmt_addr,
408 htons(ireq->ir_num), ireq->ir_rmt_port,
409 htonl(tcp_rsk(req)->snt_isn),
410 htonl(tcp_rsk(req)->rcv_isn));
413 static int tcp_v4_ao_calc_key_skb(struct tcp_ao_key *mkt, u8 *key,
414 const struct sk_buff *skb,
415 __be32 sisn, __be32 disn)
417 const struct iphdr *iph = ip_hdr(skb);
418 const struct tcphdr *th = tcp_hdr(skb);
420 return tcp_v4_ao_calc_key(mkt, key, iph->saddr, iph->daddr,
421 th->source, th->dest, sisn, disn);
424 static int tcp_ao_calc_key_skb(struct tcp_ao_key *mkt, u8 *key,
425 const struct sk_buff *skb,
426 __be32 sisn, __be32 disn, int family)
428 if (family == AF_INET)
429 return tcp_v4_ao_calc_key_skb(mkt, key, skb, sisn, disn);
430 #if IS_ENABLED(CONFIG_IPV6)
431 else if (family == AF_INET6)
432 return tcp_v6_ao_calc_key_skb(mkt, key, skb, sisn, disn);
434 return -EAFNOSUPPORT;
437 static int tcp_v4_ao_hash_pseudoheader(struct tcp_sigpool *hp,
438 __be32 daddr, __be32 saddr,
441 struct tcp4_pseudohdr *bp;
442 struct scatterlist sg;
448 bp->protocol = IPPROTO_TCP;
449 bp->len = cpu_to_be16(nbytes);
451 sg_init_one(&sg, bp, sizeof(*bp));
452 ahash_request_set_crypt(hp->req, &sg, NULL, sizeof(*bp));
453 return crypto_ahash_update(hp->req);
456 static int tcp_ao_hash_pseudoheader(unsigned short int family,
457 const struct sock *sk,
458 const struct sk_buff *skb,
459 struct tcp_sigpool *hp, int nbytes)
461 const struct tcphdr *th = tcp_hdr(skb);
463 /* TODO: Can we rely on checksum being zero to mean outbound pkt? */
465 if (family == AF_INET)
466 return tcp_v4_ao_hash_pseudoheader(hp, sk->sk_daddr,
467 sk->sk_rcv_saddr, skb->len);
468 #if IS_ENABLED(CONFIG_IPV6)
469 else if (family == AF_INET6)
470 return tcp_v6_ao_hash_pseudoheader(hp, &sk->sk_v6_daddr,
471 &sk->sk_v6_rcv_saddr, skb->len);
474 return -EAFNOSUPPORT;
477 if (family == AF_INET) {
478 const struct iphdr *iph = ip_hdr(skb);
480 return tcp_v4_ao_hash_pseudoheader(hp, iph->daddr,
481 iph->saddr, skb->len);
482 #if IS_ENABLED(CONFIG_IPV6)
483 } else if (family == AF_INET6) {
484 const struct ipv6hdr *iph = ipv6_hdr(skb);
486 return tcp_v6_ao_hash_pseudoheader(hp, &iph->daddr,
487 &iph->saddr, skb->len);
490 return -EAFNOSUPPORT;
493 u32 tcp_ao_compute_sne(u32 next_sne, u32 next_seq, u32 seq)
497 if (before(seq, next_seq)) {
508 /* tcp_ao_hash_sne(struct tcp_sigpool *hp)
509 * @hp - used for hashing
512 static int tcp_ao_hash_sne(struct tcp_sigpool *hp, u32 sne)
514 struct scatterlist sg;
517 bp = (__be32 *)hp->scratch;
520 sg_init_one(&sg, bp, sizeof(*bp));
521 ahash_request_set_crypt(hp->req, &sg, NULL, sizeof(*bp));
522 return crypto_ahash_update(hp->req);
525 static int tcp_ao_hash_header(struct tcp_sigpool *hp,
526 const struct tcphdr *th,
527 bool exclude_options, u8 *hash,
528 int hash_offset, int hash_len)
530 struct scatterlist sg;
531 u8 *hdr = hp->scratch;
534 /* We are not allowed to change tcphdr, make a local copy */
535 if (exclude_options) {
536 len = sizeof(*th) + sizeof(struct tcp_ao_hdr) + hash_len;
537 memcpy(hdr, th, sizeof(*th));
538 memcpy(hdr + sizeof(*th),
539 (u8 *)th + hash_offset - sizeof(struct tcp_ao_hdr),
540 sizeof(struct tcp_ao_hdr));
541 memset(hdr + sizeof(*th) + sizeof(struct tcp_ao_hdr),
543 ((struct tcphdr *)hdr)->check = 0;
546 memcpy(hdr, th, len);
547 /* zero out tcp-ao hash */
548 ((struct tcphdr *)hdr)->check = 0;
549 memset(hdr + hash_offset, 0, hash_len);
552 sg_init_one(&sg, hdr, len);
553 ahash_request_set_crypt(hp->req, &sg, NULL, len);
554 err = crypto_ahash_update(hp->req);
555 WARN_ON_ONCE(err != 0);
559 int tcp_ao_hash_hdr(unsigned short int family, char *ao_hash,
560 struct tcp_ao_key *key, const u8 *tkey,
561 const union tcp_ao_addr *daddr,
562 const union tcp_ao_addr *saddr,
563 const struct tcphdr *th, u32 sne)
565 int tkey_len = tcp_ao_digest_size(key);
566 int hash_offset = ao_hash - (char *)th;
567 struct tcp_sigpool hp;
568 void *hash_buf = NULL;
570 hash_buf = kmalloc(tkey_len, GFP_ATOMIC);
572 goto clear_hash_noput;
574 if (tcp_sigpool_start(key->tcp_sigpool_id, &hp))
575 goto clear_hash_noput;
577 if (crypto_ahash_setkey(crypto_ahash_reqtfm(hp.req), tkey, tkey_len))
580 if (crypto_ahash_init(hp.req))
583 if (tcp_ao_hash_sne(&hp, sne))
585 if (family == AF_INET) {
586 if (tcp_v4_ao_hash_pseudoheader(&hp, daddr->a4.s_addr,
587 saddr->a4.s_addr, th->doff * 4))
589 #if IS_ENABLED(CONFIG_IPV6)
590 } else if (family == AF_INET6) {
591 if (tcp_v6_ao_hash_pseudoheader(&hp, &daddr->a6,
592 &saddr->a6, th->doff * 4))
599 if (tcp_ao_hash_header(&hp, th,
600 !!(key->keyflags & TCP_AO_KEYF_EXCLUDE_OPT),
601 ao_hash, hash_offset, tcp_ao_maclen(key)))
603 ahash_request_set_crypt(hp.req, NULL, hash_buf, 0);
604 if (crypto_ahash_final(hp.req))
607 memcpy(ao_hash, hash_buf, tcp_ao_maclen(key));
608 tcp_sigpool_end(&hp);
613 tcp_sigpool_end(&hp);
615 memset(ao_hash, 0, tcp_ao_maclen(key));
620 int tcp_ao_hash_skb(unsigned short int family,
621 char *ao_hash, struct tcp_ao_key *key,
622 const struct sock *sk, const struct sk_buff *skb,
623 const u8 *tkey, int hash_offset, u32 sne)
625 const struct tcphdr *th = tcp_hdr(skb);
626 int tkey_len = tcp_ao_digest_size(key);
627 struct tcp_sigpool hp;
628 void *hash_buf = NULL;
630 hash_buf = kmalloc(tkey_len, GFP_ATOMIC);
632 goto clear_hash_noput;
634 if (tcp_sigpool_start(key->tcp_sigpool_id, &hp))
635 goto clear_hash_noput;
637 if (crypto_ahash_setkey(crypto_ahash_reqtfm(hp.req), tkey, tkey_len))
640 /* For now use sha1 by default. Depends on alg in tcp_ao_key */
641 if (crypto_ahash_init(hp.req))
644 if (tcp_ao_hash_sne(&hp, sne))
646 if (tcp_ao_hash_pseudoheader(family, sk, skb, &hp, skb->len))
648 if (tcp_ao_hash_header(&hp, th,
649 !!(key->keyflags & TCP_AO_KEYF_EXCLUDE_OPT),
650 ao_hash, hash_offset, tcp_ao_maclen(key)))
652 if (tcp_sigpool_hash_skb_data(&hp, skb, th->doff << 2))
654 ahash_request_set_crypt(hp.req, NULL, hash_buf, 0);
655 if (crypto_ahash_final(hp.req))
658 memcpy(ao_hash, hash_buf, tcp_ao_maclen(key));
659 tcp_sigpool_end(&hp);
664 tcp_sigpool_end(&hp);
666 memset(ao_hash, 0, tcp_ao_maclen(key));
671 int tcp_v4_ao_hash_skb(char *ao_hash, struct tcp_ao_key *key,
672 const struct sock *sk, const struct sk_buff *skb,
673 const u8 *tkey, int hash_offset, u32 sne)
675 return tcp_ao_hash_skb(AF_INET, ao_hash, key, sk, skb,
676 tkey, hash_offset, sne);
679 int tcp_v4_ao_synack_hash(char *ao_hash, struct tcp_ao_key *ao_key,
680 struct request_sock *req, const struct sk_buff *skb,
681 int hash_offset, u32 sne)
683 void *hash_buf = NULL;
686 hash_buf = kmalloc(tcp_ao_digest_size(ao_key), GFP_ATOMIC);
690 err = tcp_v4_ao_calc_key_rsk(ao_key, hash_buf, req);
694 err = tcp_ao_hash_skb(AF_INET, ao_hash, ao_key, req_to_sk(req), skb,
695 hash_buf, hash_offset, sne);
701 struct tcp_ao_key *tcp_v4_ao_lookup_rsk(const struct sock *sk,
702 struct request_sock *req,
703 int sndid, int rcvid)
705 struct inet_request_sock *ireq = inet_rsk(req);
706 union tcp_ao_addr *addr = (union tcp_ao_addr *)&ireq->ir_rmt_addr;
709 l3index = l3mdev_master_ifindex_by_index(sock_net(sk), ireq->ir_iif);
710 return tcp_ao_do_lookup(sk, l3index, addr, AF_INET, sndid, rcvid);
713 struct tcp_ao_key *tcp_v4_ao_lookup(const struct sock *sk, struct sock *addr_sk,
714 int sndid, int rcvid)
716 int l3index = l3mdev_master_ifindex_by_index(sock_net(sk),
717 addr_sk->sk_bound_dev_if);
718 union tcp_ao_addr *addr = (union tcp_ao_addr *)&addr_sk->sk_daddr;
720 return tcp_ao_do_lookup(sk, l3index, addr, AF_INET, sndid, rcvid);
723 int tcp_ao_prepare_reset(const struct sock *sk, struct sk_buff *skb,
724 const struct tcp_ao_hdr *aoh, int l3index, u32 seq,
725 struct tcp_ao_key **key, char **traffic_key,
726 bool *allocated_traffic_key, u8 *keyid, u32 *sne)
728 const struct tcphdr *th = tcp_hdr(skb);
729 struct tcp_ao_info *ao_info;
731 *allocated_traffic_key = false;
732 /* If there's no socket - than initial sisn/disn are unknown.
733 * Drop the segment. RFC5925 (7.7) advises to require graceful
734 * restart [RFC4724]. Alternatively, the RFC5925 advises to
735 * save/restore traffic keys before/after reboot.
736 * Linux TCP-AO support provides TCP_AO_ADD_KEY and TCP_AO_REPAIR
737 * options to restore a socket post-reboot.
742 if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_NEW_SYN_RECV)) {
743 unsigned int family = READ_ONCE(sk->sk_family);
744 union tcp_ao_addr *addr;
747 if (sk->sk_state == TCP_NEW_SYN_RECV) {
748 struct request_sock *req = inet_reqsk(sk);
750 sisn = htonl(tcp_rsk(req)->rcv_isn);
751 disn = htonl(tcp_rsk(req)->snt_isn);
752 *sne = tcp_ao_compute_sne(0, tcp_rsk(req)->snt_isn, seq);
757 if (IS_ENABLED(CONFIG_IPV6) && family == AF_INET6)
758 addr = (union tcp_md5_addr *)&ipv6_hdr(skb)->saddr;
760 addr = (union tcp_md5_addr *)&ip_hdr(skb)->saddr;
761 #if IS_ENABLED(CONFIG_IPV6)
762 if (family == AF_INET6 && ipv6_addr_v4mapped(&sk->sk_v6_daddr))
766 sk = sk_const_to_full_sk(sk);
767 ao_info = rcu_dereference(tcp_sk(sk)->ao_info);
770 *key = tcp_ao_do_lookup(sk, l3index, addr, family,
771 -1, aoh->rnext_keyid);
774 *traffic_key = kmalloc(tcp_ao_digest_size(*key), GFP_ATOMIC);
777 *allocated_traffic_key = true;
778 if (tcp_ao_calc_key_skb(*key, *traffic_key, skb,
781 *keyid = (*key)->rcvid;
783 struct tcp_ao_key *rnext_key;
786 if (sk->sk_state == TCP_TIME_WAIT) {
787 ao_info = rcu_dereference(tcp_twsk(sk)->ao_info);
788 snd_basis = tcp_twsk(sk)->tw_snd_nxt;
790 ao_info = rcu_dereference(tcp_sk(sk)->ao_info);
791 snd_basis = tcp_sk(sk)->snd_una;
796 *key = tcp_ao_established_key(ao_info, aoh->rnext_keyid, -1);
799 *traffic_key = snd_other_key(*key);
800 rnext_key = READ_ONCE(ao_info->rnext_key);
801 *keyid = rnext_key->rcvid;
802 *sne = tcp_ao_compute_sne(READ_ONCE(ao_info->snd_sne),
808 int tcp_ao_transmit_skb(struct sock *sk, struct sk_buff *skb,
809 struct tcp_ao_key *key, struct tcphdr *th,
812 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
813 struct tcp_sock *tp = tcp_sk(sk);
814 struct tcp_ao_info *ao;
815 void *tkey_buf = NULL;
819 ao = rcu_dereference_protected(tcp_sk(sk)->ao_info,
820 lockdep_sock_is_held(sk));
821 traffic_key = snd_other_key(key);
822 if (unlikely(tcb->tcp_flags & TCPHDR_SYN)) {
825 if (!(tcb->tcp_flags & TCPHDR_ACK)) {
827 tkey_buf = kmalloc(tcp_ao_digest_size(key), GFP_ATOMIC);
830 traffic_key = tkey_buf;
834 tp->af_specific->ao_calc_key_sk(key, traffic_key,
835 sk, ao->lisn, disn, true);
837 sne = tcp_ao_compute_sne(READ_ONCE(ao->snd_sne), READ_ONCE(tp->snd_una),
839 tp->af_specific->calc_ao_hash(hash_location, key, sk, skb, traffic_key,
840 hash_location - (u8 *)th, sne);
845 static struct tcp_ao_key *tcp_ao_inbound_lookup(unsigned short int family,
846 const struct sock *sk, const struct sk_buff *skb,
847 int sndid, int rcvid, int l3index)
849 if (family == AF_INET) {
850 const struct iphdr *iph = ip_hdr(skb);
852 return tcp_ao_do_lookup(sk, l3index,
853 (union tcp_ao_addr *)&iph->saddr,
854 AF_INET, sndid, rcvid);
856 const struct ipv6hdr *iph = ipv6_hdr(skb);
858 return tcp_ao_do_lookup(sk, l3index,
859 (union tcp_ao_addr *)&iph->saddr,
860 AF_INET6, sndid, rcvid);
864 void tcp_ao_syncookie(struct sock *sk, const struct sk_buff *skb,
865 struct request_sock *req, unsigned short int family)
867 struct tcp_request_sock *treq = tcp_rsk(req);
868 const struct tcphdr *th = tcp_hdr(skb);
869 const struct tcp_ao_hdr *aoh;
870 struct tcp_ao_key *key;
873 /* treq->af_specific is used to perform TCP_AO lookup
874 * in tcp_create_openreq_child().
876 #if IS_ENABLED(CONFIG_IPV6)
877 if (family == AF_INET6)
878 treq->af_specific = &tcp_request_sock_ipv6_ops;
881 treq->af_specific = &tcp_request_sock_ipv4_ops;
883 treq->used_tcp_ao = false;
885 if (tcp_parse_auth_options(th, NULL, &aoh) || !aoh)
888 l3index = l3mdev_master_ifindex_by_index(sock_net(sk), inet_rsk(req)->ir_iif);
889 key = tcp_ao_inbound_lookup(family, sk, skb, -1, aoh->keyid, l3index);
891 /* Key not found, continue without TCP-AO */
894 treq->ao_rcv_next = aoh->keyid;
895 treq->ao_keyid = aoh->rnext_keyid;
896 treq->used_tcp_ao = true;
899 static enum skb_drop_reason
900 tcp_ao_verify_hash(const struct sock *sk, const struct sk_buff *skb,
901 unsigned short int family, struct tcp_ao_info *info,
902 const struct tcp_ao_hdr *aoh, struct tcp_ao_key *key,
903 u8 *traffic_key, u8 *phash, u32 sne, int l3index)
905 const struct tcphdr *th = tcp_hdr(skb);
906 u8 maclen = tcp_ao_hdr_maclen(aoh);
907 void *hash_buf = NULL;
909 if (maclen != tcp_ao_maclen(key)) {
910 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOBAD);
911 atomic64_inc(&info->counters.pkt_bad);
912 atomic64_inc(&key->pkt_bad);
913 trace_tcp_ao_wrong_maclen(sk, skb, aoh->keyid,
914 aoh->rnext_keyid, maclen);
915 return SKB_DROP_REASON_TCP_AOFAILURE;
918 hash_buf = kmalloc(tcp_ao_digest_size(key), GFP_ATOMIC);
920 return SKB_DROP_REASON_NOT_SPECIFIED;
922 /* XXX: make it per-AF callback? */
923 tcp_ao_hash_skb(family, hash_buf, key, sk, skb, traffic_key,
924 (phash - (u8 *)th), sne);
925 if (memcmp(phash, hash_buf, maclen)) {
926 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOBAD);
927 atomic64_inc(&info->counters.pkt_bad);
928 atomic64_inc(&key->pkt_bad);
929 trace_tcp_ao_mismatch(sk, skb, aoh->keyid,
930 aoh->rnext_keyid, maclen);
932 return SKB_DROP_REASON_TCP_AOFAILURE;
934 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOGOOD);
935 atomic64_inc(&info->counters.pkt_good);
936 atomic64_inc(&key->pkt_good);
938 return SKB_NOT_DROPPED_YET;
942 tcp_inbound_ao_hash(struct sock *sk, const struct sk_buff *skb,
943 unsigned short int family, const struct request_sock *req,
944 int l3index, const struct tcp_ao_hdr *aoh)
946 const struct tcphdr *th = tcp_hdr(skb);
947 u8 maclen = tcp_ao_hdr_maclen(aoh);
948 u8 *phash = (u8 *)(aoh + 1); /* hash goes just after the header */
949 struct tcp_ao_info *info;
950 enum skb_drop_reason ret;
951 struct tcp_ao_key *key;
957 info = rcu_dereference(tcp_sk(sk)->ao_info);
959 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOKEYNOTFOUND);
960 trace_tcp_ao_key_not_found(sk, skb, aoh->keyid,
961 aoh->rnext_keyid, maclen);
962 return SKB_DROP_REASON_TCP_AOUNEXPECTED;
965 if (unlikely(th->syn)) {
970 state = READ_ONCE(sk->sk_state);
972 if (likely((1 << state) & TCP_AO_ESTABLISHED)) {
973 enum skb_drop_reason err;
974 struct tcp_ao_key *current_key;
976 /* Check if this socket's rnext_key matches the keyid in the
977 * packet. If not we lookup the key based on the keyid
978 * matching the rcvid in the mkt.
980 key = READ_ONCE(info->rnext_key);
981 if (key->rcvid != aoh->keyid) {
982 key = tcp_ao_established_key(info, -1, aoh->keyid);
987 /* Delayed retransmitted SYN */
988 if (unlikely(th->syn && !th->ack))
991 sne = tcp_ao_compute_sne(info->rcv_sne, tcp_sk(sk)->rcv_nxt,
993 /* Established socket, traffic key are cached */
994 traffic_key = rcv_other_key(key);
995 err = tcp_ao_verify_hash(sk, skb, family, info, aoh, key,
996 traffic_key, phash, sne, l3index);
999 current_key = READ_ONCE(info->current_key);
1000 /* Key rotation: the peer asks us to use new key (RNext) */
1001 if (unlikely(aoh->rnext_keyid != current_key->sndid)) {
1002 trace_tcp_ao_rnext_request(sk, skb, current_key->sndid,
1004 tcp_ao_hdr_maclen(aoh));
1005 /* If the key is not found we do nothing. */
1006 key = tcp_ao_established_key(info, aoh->rnext_keyid, -1);
1008 /* pairs with tcp_ao_del_cmd */
1009 WRITE_ONCE(info->current_key, key);
1011 return SKB_NOT_DROPPED_YET;
1014 if (unlikely(state == TCP_CLOSE))
1015 return SKB_DROP_REASON_TCP_CLOSE;
1017 /* Lookup key based on peer address and keyid.
1018 * current_key and rnext_key must not be used on tcp listen
1019 * sockets as otherwise:
1020 * - request sockets would race on those key pointers
1021 * - tcp_ao_del_cmd() allows async key removal
1023 key = tcp_ao_inbound_lookup(family, sk, skb, -1, aoh->keyid, l3index);
1027 if (th->syn && !th->ack)
1030 if ((1 << state) & (TCPF_LISTEN | TCPF_NEW_SYN_RECV)) {
1031 /* Make the initial syn the likely case here */
1032 if (unlikely(req)) {
1033 sne = tcp_ao_compute_sne(0, tcp_rsk(req)->rcv_isn,
1035 sisn = htonl(tcp_rsk(req)->rcv_isn);
1036 disn = htonl(tcp_rsk(req)->snt_isn);
1037 } else if (unlikely(th->ack && !th->syn)) {
1038 /* Possible syncookie packet */
1039 sisn = htonl(ntohl(th->seq) - 1);
1040 disn = htonl(ntohl(th->ack_seq) - 1);
1041 sne = tcp_ao_compute_sne(0, ntohl(sisn),
1043 } else if (unlikely(!th->syn)) {
1044 /* no way to figure out initial sisn/disn - drop */
1045 return SKB_DROP_REASON_TCP_FLAGS;
1047 } else if ((1 << state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
1049 if (th->syn || th->rst)
1054 WARN_ONCE(1, "TCP-AO: Unexpected sk_state %d", state);
1055 return SKB_DROP_REASON_TCP_AOFAILURE;
1058 traffic_key = kmalloc(tcp_ao_digest_size(key), GFP_ATOMIC);
1060 return SKB_DROP_REASON_NOT_SPECIFIED;
1061 tcp_ao_calc_key_skb(key, traffic_key, skb, sisn, disn, family);
1062 ret = tcp_ao_verify_hash(sk, skb, family, info, aoh, key,
1063 traffic_key, phash, sne, l3index);
1068 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOKEYNOTFOUND);
1069 atomic64_inc(&info->counters.key_not_found);
1070 trace_tcp_ao_key_not_found(sk, skb, aoh->keyid,
1071 aoh->rnext_keyid, maclen);
1072 return SKB_DROP_REASON_TCP_AOKEYNOTFOUND;
1075 static int tcp_ao_cache_traffic_keys(const struct sock *sk,
1076 struct tcp_ao_info *ao,
1077 struct tcp_ao_key *ao_key)
1079 u8 *traffic_key = snd_other_key(ao_key);
1082 ret = tcp_ao_calc_key_sk(ao_key, traffic_key, sk,
1083 ao->lisn, ao->risn, true);
1087 traffic_key = rcv_other_key(ao_key);
1088 ret = tcp_ao_calc_key_sk(ao_key, traffic_key, sk,
1089 ao->lisn, ao->risn, false);
1093 void tcp_ao_connect_init(struct sock *sk)
1095 struct tcp_sock *tp = tcp_sk(sk);
1096 struct tcp_ao_info *ao_info;
1097 struct hlist_node *next;
1098 union tcp_ao_addr *addr;
1099 struct tcp_ao_key *key;
1100 int family, l3index;
1102 ao_info = rcu_dereference_protected(tp->ao_info,
1103 lockdep_sock_is_held(sk));
1107 /* Remove all keys that don't match the peer */
1108 family = sk->sk_family;
1109 if (family == AF_INET)
1110 addr = (union tcp_ao_addr *)&sk->sk_daddr;
1111 #if IS_ENABLED(CONFIG_IPV6)
1112 else if (family == AF_INET6)
1113 addr = (union tcp_ao_addr *)&sk->sk_v6_daddr;
1117 l3index = l3mdev_master_ifindex_by_index(sock_net(sk),
1118 sk->sk_bound_dev_if);
1120 hlist_for_each_entry_safe(key, next, &ao_info->head, node) {
1121 if (!tcp_ao_key_cmp(key, l3index, addr, key->prefixlen, family, -1, -1))
1124 if (key == ao_info->current_key)
1125 ao_info->current_key = NULL;
1126 if (key == ao_info->rnext_key)
1127 ao_info->rnext_key = NULL;
1128 hlist_del_rcu(&key->node);
1129 atomic_sub(tcp_ao_sizeof_key(key), &sk->sk_omem_alloc);
1130 call_rcu(&key->rcu, tcp_ao_key_free_rcu);
1133 key = tp->af_specific->ao_lookup(sk, sk, -1, -1);
1135 /* if current_key or rnext_key were not provided,
1136 * use the first key matching the peer
1138 if (!ao_info->current_key)
1139 ao_info->current_key = key;
1140 if (!ao_info->rnext_key)
1141 ao_info->rnext_key = key;
1142 tp->tcp_header_len += tcp_ao_len_aligned(key);
1144 ao_info->lisn = htonl(tp->write_seq);
1145 ao_info->snd_sne = 0;
1147 /* Can't happen: tcp_connect() verifies that there's
1148 * at least one tcp-ao key that matches the remote peer.
1151 rcu_assign_pointer(tp->ao_info, NULL);
1156 void tcp_ao_established(struct sock *sk)
1158 struct tcp_ao_info *ao;
1159 struct tcp_ao_key *key;
1161 ao = rcu_dereference_protected(tcp_sk(sk)->ao_info,
1162 lockdep_sock_is_held(sk));
1166 hlist_for_each_entry_rcu(key, &ao->head, node)
1167 tcp_ao_cache_traffic_keys(sk, ao, key);
1170 void tcp_ao_finish_connect(struct sock *sk, struct sk_buff *skb)
1172 struct tcp_ao_info *ao;
1173 struct tcp_ao_key *key;
1175 ao = rcu_dereference_protected(tcp_sk(sk)->ao_info,
1176 lockdep_sock_is_held(sk));
1180 WRITE_ONCE(ao->risn, tcp_hdr(skb)->seq);
1183 hlist_for_each_entry_rcu(key, &ao->head, node)
1184 tcp_ao_cache_traffic_keys(sk, ao, key);
1187 int tcp_ao_copy_all_matching(const struct sock *sk, struct sock *newsk,
1188 struct request_sock *req, struct sk_buff *skb,
1191 struct tcp_ao_key *key, *new_key, *first_key;
1192 struct tcp_ao_info *new_ao, *ao;
1193 struct hlist_node *key_head;
1194 int l3index, ret = -ENOMEM;
1195 union tcp_ao_addr *addr;
1198 ao = rcu_dereference(tcp_sk(sk)->ao_info);
1202 /* New socket without TCP-AO on it */
1203 if (!tcp_rsk_used_ao(req))
1206 new_ao = tcp_ao_alloc_info(GFP_ATOMIC);
1209 new_ao->lisn = htonl(tcp_rsk(req)->snt_isn);
1210 new_ao->risn = htonl(tcp_rsk(req)->rcv_isn);
1211 new_ao->ao_required = ao->ao_required;
1212 new_ao->accept_icmps = ao->accept_icmps;
1214 if (family == AF_INET) {
1215 addr = (union tcp_ao_addr *)&newsk->sk_daddr;
1216 #if IS_ENABLED(CONFIG_IPV6)
1217 } else if (family == AF_INET6) {
1218 addr = (union tcp_ao_addr *)&newsk->sk_v6_daddr;
1221 ret = -EAFNOSUPPORT;
1224 l3index = l3mdev_master_ifindex_by_index(sock_net(newsk),
1225 newsk->sk_bound_dev_if);
1227 hlist_for_each_entry_rcu(key, &ao->head, node) {
1228 if (tcp_ao_key_cmp(key, l3index, addr, key->prefixlen, family, -1, -1))
1231 new_key = tcp_ao_copy_key(newsk, key);
1235 tcp_ao_cache_traffic_keys(newsk, new_ao, new_key);
1236 tcp_ao_link_mkt(new_ao, new_key);
1241 /* RFC5925 (7.4.1) specifies that the TCP-AO status
1242 * of a connection is determined on the initial SYN.
1243 * At this point the connection was TCP-AO enabled, so
1244 * it can't switch to being unsigned if peer's key
1245 * disappears on the listening socket.
1247 ret = -EKEYREJECTED;
1251 if (!static_key_fast_inc_not_disabled(&tcp_ao_needed.key.key)) {
1256 key_head = rcu_dereference(hlist_first_rcu(&new_ao->head));
1257 first_key = hlist_entry_safe(key_head, struct tcp_ao_key, node);
1259 key = tcp_ao_established_key(new_ao, tcp_rsk(req)->ao_keyid, -1);
1261 new_ao->current_key = key;
1263 new_ao->current_key = first_key;
1266 key = tcp_ao_established_key(new_ao, -1, tcp_rsk(req)->ao_rcv_next);
1268 new_ao->rnext_key = key;
1270 new_ao->rnext_key = first_key;
1272 sk_gso_disable(newsk);
1273 rcu_assign_pointer(tcp_sk(newsk)->ao_info, new_ao);
1278 hlist_for_each_entry_safe(key, key_head, &new_ao->head, node) {
1279 hlist_del(&key->node);
1280 tcp_sigpool_release(key->tcp_sigpool_id);
1281 atomic_sub(tcp_ao_sizeof_key(key), &newsk->sk_omem_alloc);
1282 kfree_sensitive(key);
1289 static bool tcp_ao_can_set_current_rnext(struct sock *sk)
1291 /* There aren't current/rnext keys on TCP_LISTEN sockets */
1292 if (sk->sk_state == TCP_LISTEN)
1297 static int tcp_ao_verify_ipv4(struct sock *sk, struct tcp_ao_add *cmd,
1298 union tcp_ao_addr **addr)
1300 struct sockaddr_in *sin = (struct sockaddr_in *)&cmd->addr;
1301 struct inet_sock *inet = inet_sk(sk);
1303 if (sin->sin_family != AF_INET)
1306 /* Currently matching is not performed on port (or port ranges) */
1307 if (sin->sin_port != 0)
1310 /* Check prefix and trailing 0's in addr */
1311 if (cmd->prefix != 0) {
1314 if (ntohl(sin->sin_addr.s_addr) == INADDR_ANY)
1316 if (cmd->prefix > 32)
1319 mask = inet_make_mask(cmd->prefix);
1320 if (sin->sin_addr.s_addr & ~mask)
1323 /* Check that MKT address is consistent with socket */
1324 if (ntohl(inet->inet_daddr) != INADDR_ANY &&
1325 (inet->inet_daddr & mask) != sin->sin_addr.s_addr)
1328 if (ntohl(sin->sin_addr.s_addr) != INADDR_ANY)
1332 *addr = (union tcp_ao_addr *)&sin->sin_addr;
1336 static int tcp_ao_parse_crypto(struct tcp_ao_add *cmd, struct tcp_ao_key *key)
1338 unsigned int syn_tcp_option_space;
1339 bool is_kdf_aes_128_cmac = false;
1340 struct crypto_ahash *tfm;
1341 struct tcp_sigpool hp;
1342 void *tmp_key = NULL;
1345 /* RFC5926, 3.1.1.2. KDF_AES_128_CMAC */
1346 if (!strcmp("cmac(aes128)", cmd->alg_name)) {
1347 strscpy(cmd->alg_name, "cmac(aes)", sizeof(cmd->alg_name));
1348 is_kdf_aes_128_cmac = (cmd->keylen != 16);
1349 tmp_key = kmalloc(cmd->keylen, GFP_KERNEL);
1354 key->maclen = cmd->maclen ?: 12; /* 12 is the default in RFC5925 */
1356 /* Check: maclen + tcp-ao header <= (MAX_TCP_OPTION_SPACE - mss
1357 * - tstamp (including sackperm)
1359 * see tcp_syn_options(), tcp_synack_options(), commit 33ad798c924b.
1361 * In order to allow D-SACK with TCP-AO, the header size should be:
1362 * (MAX_TCP_OPTION_SPACE - TCPOLEN_TSTAMP_ALIGNED
1363 * - TCPOLEN_SACK_BASE_ALIGNED
1364 * - 2 * TCPOLEN_SACK_PERBLOCK) = 8 (maclen = 4),
1365 * see tcp_established_options().
1368 * Typical MACs are 96-128 bits (12-16 bytes), but any length
1369 * that fits in the header of the segment being authenticated
1373 * TCP-AO continues to consume 16 bytes in non-SYN segments,
1374 * leaving a total of 24 bytes for other options, of which
1375 * the timestamp consumes 10. This leaves 14 bytes, of which 10
1376 * are used for a single SACK block. When two SACK blocks are used,
1377 * such as to handle D-SACK, a smaller TCP-AO MAC would be required
1378 * to make room for the additional SACK block (i.e., to leave 18
1379 * bytes for the D-SACK variant of the SACK option) [RFC2883].
1380 * Note that D-SACK is not supportable in TCP MD5 in the presence
1381 * of timestamps, because TCP MD5’s MAC length is fixed and too
1382 * large to leave sufficient option space.
1384 syn_tcp_option_space = MAX_TCP_OPTION_SPACE;
1385 syn_tcp_option_space -= TCPOLEN_MSS_ALIGNED;
1386 syn_tcp_option_space -= TCPOLEN_TSTAMP_ALIGNED;
1387 syn_tcp_option_space -= TCPOLEN_WSCALE_ALIGNED;
1388 if (tcp_ao_len_aligned(key) > syn_tcp_option_space) {
1393 key->keylen = cmd->keylen;
1394 memcpy(key->key, cmd->key, cmd->keylen);
1396 err = tcp_sigpool_start(key->tcp_sigpool_id, &hp);
1400 tfm = crypto_ahash_reqtfm(hp.req);
1401 if (is_kdf_aes_128_cmac) {
1402 void *scratch = hp.scratch;
1403 struct scatterlist sg;
1405 memcpy(tmp_key, cmd->key, cmd->keylen);
1406 sg_init_one(&sg, tmp_key, cmd->keylen);
1408 /* Using zero-key of 16 bytes as described in RFC5926 */
1409 memset(scratch, 0, 16);
1410 err = crypto_ahash_setkey(tfm, scratch, 16);
1414 err = crypto_ahash_init(hp.req);
1418 ahash_request_set_crypt(hp.req, &sg, key->key, cmd->keylen);
1419 err = crypto_ahash_update(hp.req);
1423 err |= crypto_ahash_final(hp.req);
1429 err = crypto_ahash_setkey(tfm, key->key, key->keylen);
1433 tcp_sigpool_end(&hp);
1434 kfree_sensitive(tmp_key);
1436 if (tcp_ao_maclen(key) > key->digest_size)
1442 tcp_sigpool_end(&hp);
1444 kfree_sensitive(tmp_key);
1448 #if IS_ENABLED(CONFIG_IPV6)
1449 static int tcp_ao_verify_ipv6(struct sock *sk, struct tcp_ao_add *cmd,
1450 union tcp_ao_addr **paddr,
1451 unsigned short int *family)
1453 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&cmd->addr;
1454 struct in6_addr *addr = &sin6->sin6_addr;
1455 u8 prefix = cmd->prefix;
1457 if (sin6->sin6_family != AF_INET6)
1460 /* Currently matching is not performed on port (or port ranges) */
1461 if (sin6->sin6_port != 0)
1464 /* Check prefix and trailing 0's in addr */
1465 if (cmd->prefix != 0 && ipv6_addr_v4mapped(addr)) {
1466 __be32 addr4 = addr->s6_addr32[3];
1469 if (prefix > 32 || ntohl(addr4) == INADDR_ANY)
1472 mask = inet_make_mask(prefix);
1476 /* Check that MKT address is consistent with socket */
1477 if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
1478 __be32 daddr4 = sk->sk_v6_daddr.s6_addr32[3];
1480 if (!ipv6_addr_v4mapped(&sk->sk_v6_daddr))
1482 if ((daddr4 & mask) != addr4)
1486 *paddr = (union tcp_ao_addr *)&addr->s6_addr32[3];
1489 } else if (cmd->prefix != 0) {
1490 struct in6_addr pfx;
1492 if (ipv6_addr_any(addr) || prefix > 128)
1495 ipv6_addr_prefix(&pfx, addr, prefix);
1496 if (ipv6_addr_cmp(&pfx, addr))
1499 /* Check that MKT address is consistent with socket */
1500 if (!ipv6_addr_any(&sk->sk_v6_daddr) &&
1501 !ipv6_prefix_equal(&sk->sk_v6_daddr, addr, prefix))
1505 if (!ipv6_addr_any(addr))
1509 *paddr = (union tcp_ao_addr *)addr;
1513 static int tcp_ao_verify_ipv6(struct sock *sk, struct tcp_ao_add *cmd,
1514 union tcp_ao_addr **paddr,
1515 unsigned short int *family)
1521 static struct tcp_ao_info *setsockopt_ao_info(struct sock *sk)
1523 if (sk_fullsock(sk)) {
1524 return rcu_dereference_protected(tcp_sk(sk)->ao_info,
1525 lockdep_sock_is_held(sk));
1526 } else if (sk->sk_state == TCP_TIME_WAIT) {
1527 return rcu_dereference_protected(tcp_twsk(sk)->ao_info,
1528 lockdep_sock_is_held(sk));
1530 return ERR_PTR(-ESOCKTNOSUPPORT);
1533 static struct tcp_ao_info *getsockopt_ao_info(struct sock *sk)
1535 if (sk_fullsock(sk))
1536 return rcu_dereference(tcp_sk(sk)->ao_info);
1537 else if (sk->sk_state == TCP_TIME_WAIT)
1538 return rcu_dereference(tcp_twsk(sk)->ao_info);
1540 return ERR_PTR(-ESOCKTNOSUPPORT);
1543 #define TCP_AO_KEYF_ALL (TCP_AO_KEYF_IFINDEX | TCP_AO_KEYF_EXCLUDE_OPT)
1544 #define TCP_AO_GET_KEYF_VALID (TCP_AO_KEYF_IFINDEX)
1546 static struct tcp_ao_key *tcp_ao_key_alloc(struct sock *sk,
1547 struct tcp_ao_add *cmd)
1549 const char *algo = cmd->alg_name;
1550 unsigned int digest_size;
1551 struct crypto_ahash *tfm;
1552 struct tcp_ao_key *key;
1553 struct tcp_sigpool hp;
1557 /* Force null-termination of alg_name */
1558 cmd->alg_name[ARRAY_SIZE(cmd->alg_name) - 1] = '\0';
1560 /* RFC5926, 3.1.1.2. KDF_AES_128_CMAC */
1561 if (!strcmp("cmac(aes128)", algo))
1564 /* Full TCP header (th->doff << 2) should fit into scratch area,
1565 * see tcp_ao_hash_header().
1567 pool_id = tcp_sigpool_alloc_ahash(algo, 60);
1569 return ERR_PTR(pool_id);
1571 err = tcp_sigpool_start(pool_id, &hp);
1575 tfm = crypto_ahash_reqtfm(hp.req);
1576 digest_size = crypto_ahash_digestsize(tfm);
1577 tcp_sigpool_end(&hp);
1579 size = sizeof(struct tcp_ao_key) + (digest_size << 1);
1580 key = sock_kmalloc(sk, size, GFP_KERNEL);
1586 key->tcp_sigpool_id = pool_id;
1587 key->digest_size = digest_size;
1591 tcp_sigpool_release(pool_id);
1592 return ERR_PTR(err);
1595 static int tcp_ao_add_cmd(struct sock *sk, unsigned short int family,
1596 sockptr_t optval, int optlen)
1598 struct tcp_ao_info *ao_info;
1599 union tcp_ao_addr *addr;
1600 struct tcp_ao_key *key;
1601 struct tcp_ao_add cmd;
1602 int ret, l3index = 0;
1605 if (optlen < sizeof(cmd))
1608 ret = copy_struct_from_sockptr(&cmd, sizeof(cmd), optval, optlen);
1612 if (cmd.keylen > TCP_AO_MAXKEYLEN)
1615 if (cmd.reserved != 0 || cmd.reserved2 != 0)
1618 if (family == AF_INET)
1619 ret = tcp_ao_verify_ipv4(sk, &cmd, &addr);
1621 ret = tcp_ao_verify_ipv6(sk, &cmd, &addr, &family);
1625 if (cmd.keyflags & ~TCP_AO_KEYF_ALL)
1628 if (cmd.set_current || cmd.set_rnext) {
1629 if (!tcp_ao_can_set_current_rnext(sk))
1633 if (cmd.ifindex && !(cmd.keyflags & TCP_AO_KEYF_IFINDEX))
1636 /* For cmd.tcp_ifindex = 0 the key will apply to the default VRF */
1637 if (cmd.keyflags & TCP_AO_KEYF_IFINDEX && cmd.ifindex) {
1638 int bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
1639 struct net_device *dev;
1642 dev = dev_get_by_index_rcu(sock_net(sk), cmd.ifindex);
1643 if (dev && netif_is_l3_master(dev))
1644 l3index = dev->ifindex;
1647 if (!dev || !l3index)
1650 if (!bound_dev_if || bound_dev_if != cmd.ifindex) {
1651 /* tcp_ao_established_key() doesn't expect having
1652 * non peer-matching key on an established TCP-AO
1655 if (!((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)))
1659 /* It's still possible to bind after adding keys or even
1660 * re-bind to a different dev (with CAP_NET_RAW).
1661 * So, no reason to return error here, rather try to be
1662 * nice and warn the user.
1664 if (bound_dev_if && bound_dev_if != cmd.ifindex)
1665 net_warn_ratelimited("AO key ifindex %d != sk bound ifindex %d\n",
1666 cmd.ifindex, bound_dev_if);
1669 /* Don't allow keys for peers that have a matching TCP-MD5 key */
1670 if (cmd.keyflags & TCP_AO_KEYF_IFINDEX) {
1671 /* Non-_exact version of tcp_md5_do_lookup() will
1672 * as well match keys that aren't bound to a specific VRF
1673 * (that will make them match AO key with
1674 * sysctl_tcp_l3dev_accept = 1
1676 if (tcp_md5_do_lookup(sk, l3index, addr, family))
1677 return -EKEYREJECTED;
1679 if (tcp_md5_do_lookup_any_l3index(sk, addr, family))
1680 return -EKEYREJECTED;
1683 ao_info = setsockopt_ao_info(sk);
1684 if (IS_ERR(ao_info))
1685 return PTR_ERR(ao_info);
1688 ao_info = tcp_ao_alloc_info(GFP_KERNEL);
1693 /* Check that neither RecvID nor SendID match any
1694 * existing key for the peer, RFC5925 3.1:
1695 * > The IDs of MKTs MUST NOT overlap where their
1696 * > TCP connection identifiers overlap.
1698 if (__tcp_ao_do_lookup(sk, l3index, addr, family, cmd.prefix, -1, cmd.rcvid))
1700 if (__tcp_ao_do_lookup(sk, l3index, addr, family,
1701 cmd.prefix, cmd.sndid, -1))
1705 key = tcp_ao_key_alloc(sk, &cmd);
1711 INIT_HLIST_NODE(&key->node);
1712 memcpy(&key->addr, addr, (family == AF_INET) ? sizeof(struct in_addr) :
1713 sizeof(struct in6_addr));
1714 key->prefixlen = cmd.prefix;
1715 key->family = family;
1716 key->keyflags = cmd.keyflags;
1717 key->sndid = cmd.sndid;
1718 key->rcvid = cmd.rcvid;
1719 key->l3index = l3index;
1720 atomic64_set(&key->pkt_good, 0);
1721 atomic64_set(&key->pkt_bad, 0);
1723 ret = tcp_ao_parse_crypto(&cmd, key);
1727 if (!((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE))) {
1728 tcp_ao_cache_traffic_keys(sk, ao_info, key);
1730 ao_info->current_key = key;
1731 ao_info->rnext_key = key;
1735 tcp_ao_link_mkt(ao_info, key);
1737 if (!static_branch_inc(&tcp_ao_needed.key)) {
1742 rcu_assign_pointer(tcp_sk(sk)->ao_info, ao_info);
1745 if (cmd.set_current)
1746 WRITE_ONCE(ao_info->current_key, key);
1748 WRITE_ONCE(ao_info->rnext_key, key);
1752 atomic_sub(tcp_ao_sizeof_key(key), &sk->sk_omem_alloc);
1753 tcp_sigpool_release(key->tcp_sigpool_id);
1754 kfree_sensitive(key);
1761 static int tcp_ao_delete_key(struct sock *sk, struct tcp_ao_info *ao_info,
1762 bool del_async, struct tcp_ao_key *key,
1763 struct tcp_ao_key *new_current,
1764 struct tcp_ao_key *new_rnext)
1768 hlist_del_rcu(&key->node);
1770 /* Support for async delete on listening sockets: as they don't
1771 * need current_key/rnext_key maintaining, we don't need to check
1772 * them and we can just free all resources in RCU fashion.
1775 atomic_sub(tcp_ao_sizeof_key(key), &sk->sk_omem_alloc);
1776 call_rcu(&key->rcu, tcp_ao_key_free_rcu);
1780 /* At this moment another CPU could have looked this key up
1781 * while it was unlinked from the list. Wait for RCU grace period,
1782 * after which the key is off-list and can't be looked up again;
1783 * the rx path [just before RCU came] might have used it and set it
1784 * as current_key (very unlikely).
1785 * Free the key with next RCU grace period (in case it was
1786 * current_key before tcp_ao_current_rnext() might have
1787 * changed it in forced-delete).
1791 WRITE_ONCE(ao_info->current_key, new_current);
1793 WRITE_ONCE(ao_info->rnext_key, new_rnext);
1795 if (unlikely(READ_ONCE(ao_info->current_key) == key ||
1796 READ_ONCE(ao_info->rnext_key) == key)) {
1801 atomic_sub(tcp_ao_sizeof_key(key), &sk->sk_omem_alloc);
1802 call_rcu(&key->rcu, tcp_ao_key_free_rcu);
1806 hlist_add_head_rcu(&key->node, &ao_info->head);
1810 #define TCP_AO_DEL_KEYF_ALL (TCP_AO_KEYF_IFINDEX)
1811 static int tcp_ao_del_cmd(struct sock *sk, unsigned short int family,
1812 sockptr_t optval, int optlen)
1814 struct tcp_ao_key *key, *new_current = NULL, *new_rnext = NULL;
1815 int err, addr_len, l3index = 0;
1816 struct tcp_ao_info *ao_info;
1817 union tcp_ao_addr *addr;
1818 struct tcp_ao_del cmd;
1822 if (optlen < sizeof(cmd))
1825 err = copy_struct_from_sockptr(&cmd, sizeof(cmd), optval, optlen);
1829 if (cmd.reserved != 0 || cmd.reserved2 != 0)
1832 if (cmd.set_current || cmd.set_rnext) {
1833 if (!tcp_ao_can_set_current_rnext(sk))
1837 if (cmd.keyflags & ~TCP_AO_DEL_KEYF_ALL)
1840 /* No sanity check for TCP_AO_KEYF_IFINDEX as if a VRF
1841 * was destroyed, there still should be a way to delete keys,
1842 * that were bound to that l3intf. So, fail late at lookup stage
1843 * if there is no key for that ifindex.
1845 if (cmd.ifindex && !(cmd.keyflags & TCP_AO_KEYF_IFINDEX))
1848 ao_info = setsockopt_ao_info(sk);
1849 if (IS_ERR(ao_info))
1850 return PTR_ERR(ao_info);
1854 /* For sockets in TCP_CLOSED it's possible set keys that aren't
1855 * matching the future peer (address/VRF/etc),
1856 * tcp_ao_connect_init() will choose a correct matching MKT
1859 if (cmd.set_current) {
1860 new_current = tcp_ao_established_key(ao_info, cmd.current_key, -1);
1864 if (cmd.set_rnext) {
1865 new_rnext = tcp_ao_established_key(ao_info, -1, cmd.rnext);
1869 if (cmd.del_async && sk->sk_state != TCP_LISTEN)
1872 if (family == AF_INET) {
1873 struct sockaddr_in *sin = (struct sockaddr_in *)&cmd.addr;
1875 addr = (union tcp_ao_addr *)&sin->sin_addr;
1876 addr_len = sizeof(struct in_addr);
1877 port = ntohs(sin->sin_port);
1879 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&cmd.addr;
1880 struct in6_addr *addr6 = &sin6->sin6_addr;
1882 if (ipv6_addr_v4mapped(addr6)) {
1883 addr = (union tcp_ao_addr *)&addr6->s6_addr32[3];
1884 addr_len = sizeof(struct in_addr);
1887 addr = (union tcp_ao_addr *)addr6;
1888 addr_len = sizeof(struct in6_addr);
1890 port = ntohs(sin6->sin6_port);
1892 prefix = cmd.prefix;
1894 /* Currently matching is not performed on port (or port ranges) */
1898 /* We could choose random present key here for current/rnext
1899 * but that's less predictable. Let's be strict and don't
1900 * allow removing a key that's in use. RFC5925 doesn't
1901 * specify how-to coordinate key removal, but says:
1902 * "It is presumed that an MKT affecting a particular
1903 * connection cannot be destroyed during an active connection"
1905 hlist_for_each_entry_rcu(key, &ao_info->head, node) {
1906 if (cmd.sndid != key->sndid ||
1907 cmd.rcvid != key->rcvid)
1910 if (family != key->family ||
1911 prefix != key->prefixlen ||
1912 memcmp(addr, &key->addr, addr_len))
1915 if ((cmd.keyflags & TCP_AO_KEYF_IFINDEX) !=
1916 (key->keyflags & TCP_AO_KEYF_IFINDEX))
1919 if (key->l3index != l3index)
1922 if (key == new_current || key == new_rnext)
1925 return tcp_ao_delete_key(sk, ao_info, cmd.del_async, key,
1926 new_current, new_rnext);
1931 /* cmd.ao_required makes a socket TCP-AO only.
1932 * Don't allow any md5 keys for any l3intf on the socket together with it.
1933 * Restricting it early in setsockopt() removes a check for
1934 * ao_info->ao_required on inbound tcp segment fast-path.
1936 static int tcp_ao_required_verify(struct sock *sk)
1938 #ifdef CONFIG_TCP_MD5SIG
1939 const struct tcp_md5sig_info *md5sig;
1941 if (!static_branch_unlikely(&tcp_md5_needed.key))
1944 md5sig = rcu_dereference_check(tcp_sk(sk)->md5sig_info,
1945 lockdep_sock_is_held(sk));
1949 if (rcu_dereference_check(hlist_first_rcu(&md5sig->head),
1950 lockdep_sock_is_held(sk)))
1956 static int tcp_ao_info_cmd(struct sock *sk, unsigned short int family,
1957 sockptr_t optval, int optlen)
1959 struct tcp_ao_key *new_current = NULL, *new_rnext = NULL;
1960 struct tcp_ao_info *ao_info;
1961 struct tcp_ao_info_opt cmd;
1965 if (optlen < sizeof(cmd))
1968 err = copy_struct_from_sockptr(&cmd, sizeof(cmd), optval, optlen);
1972 if (cmd.set_current || cmd.set_rnext) {
1973 if (!tcp_ao_can_set_current_rnext(sk))
1977 if (cmd.reserved != 0 || cmd.reserved2 != 0)
1980 ao_info = setsockopt_ao_info(sk);
1981 if (IS_ERR(ao_info))
1982 return PTR_ERR(ao_info);
1984 if (!((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)))
1986 ao_info = tcp_ao_alloc_info(GFP_KERNEL);
1992 if (cmd.ao_required && tcp_ao_required_verify(sk)) {
1993 err = -EKEYREJECTED;
1997 /* For sockets in TCP_CLOSED it's possible set keys that aren't
1998 * matching the future peer (address/port/VRF/etc),
1999 * tcp_ao_connect_init() will choose a correct matching MKT
2002 if (cmd.set_current) {
2003 new_current = tcp_ao_established_key(ao_info, cmd.current_key, -1);
2009 if (cmd.set_rnext) {
2010 new_rnext = tcp_ao_established_key(ao_info, -1, cmd.rnext);
2016 if (cmd.set_counters) {
2017 atomic64_set(&ao_info->counters.pkt_good, cmd.pkt_good);
2018 atomic64_set(&ao_info->counters.pkt_bad, cmd.pkt_bad);
2019 atomic64_set(&ao_info->counters.key_not_found, cmd.pkt_key_not_found);
2020 atomic64_set(&ao_info->counters.ao_required, cmd.pkt_ao_required);
2021 atomic64_set(&ao_info->counters.dropped_icmp, cmd.pkt_dropped_icmp);
2024 ao_info->ao_required = cmd.ao_required;
2025 ao_info->accept_icmps = cmd.accept_icmps;
2027 WRITE_ONCE(ao_info->current_key, new_current);
2029 WRITE_ONCE(ao_info->rnext_key, new_rnext);
2031 if (!static_branch_inc(&tcp_ao_needed.key)) {
2036 rcu_assign_pointer(tcp_sk(sk)->ao_info, ao_info);
2045 int tcp_parse_ao(struct sock *sk, int cmd, unsigned short int family,
2046 sockptr_t optval, int optlen)
2048 if (WARN_ON_ONCE(family != AF_INET && family != AF_INET6))
2049 return -EAFNOSUPPORT;
2052 case TCP_AO_ADD_KEY:
2053 return tcp_ao_add_cmd(sk, family, optval, optlen);
2054 case TCP_AO_DEL_KEY:
2055 return tcp_ao_del_cmd(sk, family, optval, optlen);
2057 return tcp_ao_info_cmd(sk, family, optval, optlen);
2064 int tcp_v4_parse_ao(struct sock *sk, int cmd, sockptr_t optval, int optlen)
2066 return tcp_parse_ao(sk, cmd, AF_INET, optval, optlen);
2069 /* tcp_ao_copy_mkts_to_user(ao_info, optval, optlen)
2071 * @ao_info: struct tcp_ao_info on the socket that
2072 * socket getsockopt(TCP_AO_GET_KEYS) is executed on
2073 * @optval: pointer to array of tcp_ao_getsockopt structures in user space.
2075 * @optlen: pointer to size of tcp_ao_getsockopt structure.
2078 * Return value: 0 on success, a negative error number otherwise.
2080 * optval points to an array of tcp_ao_getsockopt structures in user space.
2081 * optval[0] is used as both input and output to getsockopt. It determines
2082 * which keys are returned by the kernel.
2083 * optval[0].nkeys is the size of the array in user space. On return it contains
2084 * the number of keys matching the search criteria.
2085 * If tcp_ao_getsockopt::get_all is set, then all keys in the socket are
2086 * returned, otherwise only keys matching <addr, prefix, sndid, rcvid>
2087 * in optval[0] are returned.
2088 * optlen is also used as both input and output. The user provides the size
2089 * of struct tcp_ao_getsockopt in user space, and the kernel returns the size
2090 * of the structure in kernel space.
2091 * The size of struct tcp_ao_getsockopt may differ between user and kernel.
2092 * There are three cases to consider:
2093 * * If usize == ksize, then keys are copied verbatim.
2094 * * If usize < ksize, then the userspace has passed an old struct to a
2095 * newer kernel. The rest of the trailing bytes in optval[0]
2096 * (ksize - usize) are interpreted as 0 by the kernel.
2097 * * If usize > ksize, then the userspace has passed a new struct to an
2098 * older kernel. The trailing bytes unknown to the kernel (usize - ksize)
2099 * are checked to ensure they are zeroed, otherwise -E2BIG is returned.
2100 * On return the kernel fills in min(usize, ksize) in each entry of the array.
2101 * The layout of the fields in the user and kernel structures is expected to
2102 * be the same (including in the 32bit vs 64bit case).
2104 static int tcp_ao_copy_mkts_to_user(struct tcp_ao_info *ao_info,
2105 sockptr_t optval, sockptr_t optlen)
2107 struct tcp_ao_getsockopt opt_in, opt_out;
2108 struct tcp_ao_key *key, *current_key;
2109 bool do_address_matching = true;
2110 union tcp_ao_addr *addr = NULL;
2111 int err, l3index, user_len;
2112 unsigned int max_keys; /* maximum number of keys to copy to user */
2113 size_t out_offset = 0;
2114 size_t bytes_to_write; /* number of bytes to write to user level */
2115 u32 matched_keys; /* keys from ao_info matched so far */
2119 if (copy_from_sockptr(&user_len, optlen, sizeof(int)))
2125 memset(&opt_in, 0, sizeof(struct tcp_ao_getsockopt));
2126 err = copy_struct_from_sockptr(&opt_in, sizeof(opt_in),
2131 if (opt_in.pkt_good || opt_in.pkt_bad)
2133 if (opt_in.keyflags & ~TCP_AO_GET_KEYF_VALID)
2135 if (opt_in.ifindex && !(opt_in.keyflags & TCP_AO_KEYF_IFINDEX))
2138 if (opt_in.reserved != 0)
2141 max_keys = opt_in.nkeys;
2142 l3index = (opt_in.keyflags & TCP_AO_KEYF_IFINDEX) ? opt_in.ifindex : -1;
2144 if (opt_in.get_all || opt_in.is_current || opt_in.is_rnext) {
2145 if (opt_in.get_all && (opt_in.is_current || opt_in.is_rnext))
2147 do_address_matching = false;
2150 switch (opt_in.addr.ss_family) {
2152 struct sockaddr_in *sin;
2155 sin = (struct sockaddr_in *)&opt_in.addr;
2156 port = sin->sin_port;
2157 addr = (union tcp_ao_addr *)&sin->sin_addr;
2159 if (opt_in.prefix > 32)
2162 if (ntohl(sin->sin_addr.s_addr) == INADDR_ANY &&
2166 mask = inet_make_mask(opt_in.prefix);
2167 if (sin->sin_addr.s_addr & ~mask)
2173 struct sockaddr_in6 *sin6;
2174 struct in6_addr *addr6;
2176 sin6 = (struct sockaddr_in6 *)&opt_in.addr;
2177 addr = (union tcp_ao_addr *)&sin6->sin6_addr;
2178 addr6 = &sin6->sin6_addr;
2179 port = sin6->sin6_port;
2181 /* We don't have to change family and @addr here if
2182 * ipv6_addr_v4mapped() like in key adding:
2183 * tcp_ao_key_cmp() does it. Do the sanity checks though.
2185 if (opt_in.prefix != 0) {
2186 if (ipv6_addr_v4mapped(addr6)) {
2187 __be32 mask, addr4 = addr6->s6_addr32[3];
2189 if (opt_in.prefix > 32 ||
2190 ntohl(addr4) == INADDR_ANY)
2192 mask = inet_make_mask(opt_in.prefix);
2196 struct in6_addr pfx;
2198 if (ipv6_addr_any(addr6) ||
2199 opt_in.prefix > 128)
2202 ipv6_addr_prefix(&pfx, addr6, opt_in.prefix);
2203 if (ipv6_addr_cmp(&pfx, addr6))
2206 } else if (!ipv6_addr_any(addr6)) {
2212 if (!do_address_matching)
2216 return -EAFNOSUPPORT;
2219 if (!do_address_matching) {
2220 /* We could just ignore those, but let's do stricter checks */
2223 if (opt_in.prefix || opt_in.sndid || opt_in.rcvid)
2227 bytes_to_write = min_t(int, user_len, sizeof(struct tcp_ao_getsockopt));
2229 /* May change in RX, while we're dumping, pre-fetch it */
2230 current_key = READ_ONCE(ao_info->current_key);
2232 hlist_for_each_entry_rcu(key, &ao_info->head, node) {
2236 if (opt_in.is_current || opt_in.is_rnext) {
2237 if (opt_in.is_current && key == current_key)
2239 if (opt_in.is_rnext && key == ao_info->rnext_key)
2244 if (tcp_ao_key_cmp(key, l3index, addr, opt_in.prefix,
2245 opt_in.addr.ss_family,
2246 opt_in.sndid, opt_in.rcvid) != 0)
2250 if (matched_keys > max_keys)
2253 memset(&opt_out, 0, sizeof(struct tcp_ao_getsockopt));
2255 if (key->family == AF_INET) {
2256 struct sockaddr_in *sin_out = (struct sockaddr_in *)&opt_out.addr;
2258 sin_out->sin_family = key->family;
2259 sin_out->sin_port = 0;
2260 memcpy(&sin_out->sin_addr, &key->addr, sizeof(struct in_addr));
2262 struct sockaddr_in6 *sin6_out = (struct sockaddr_in6 *)&opt_out.addr;
2264 sin6_out->sin6_family = key->family;
2265 sin6_out->sin6_port = 0;
2266 memcpy(&sin6_out->sin6_addr, &key->addr, sizeof(struct in6_addr));
2268 opt_out.sndid = key->sndid;
2269 opt_out.rcvid = key->rcvid;
2270 opt_out.prefix = key->prefixlen;
2271 opt_out.keyflags = key->keyflags;
2272 opt_out.is_current = (key == current_key);
2273 opt_out.is_rnext = (key == ao_info->rnext_key);
2275 opt_out.maclen = key->maclen;
2276 opt_out.keylen = key->keylen;
2277 opt_out.ifindex = key->l3index;
2278 opt_out.pkt_good = atomic64_read(&key->pkt_good);
2279 opt_out.pkt_bad = atomic64_read(&key->pkt_bad);
2280 memcpy(&opt_out.key, key->key, key->keylen);
2281 tcp_sigpool_algo(key->tcp_sigpool_id, opt_out.alg_name, 64);
2283 /* Copy key to user */
2284 if (copy_to_sockptr_offset(optval, out_offset,
2285 &opt_out, bytes_to_write))
2287 out_offset += user_len;
2290 optlen_out = (int)sizeof(struct tcp_ao_getsockopt);
2291 if (copy_to_sockptr(optlen, &optlen_out, sizeof(int)))
2294 out_offset = offsetof(struct tcp_ao_getsockopt, nkeys);
2295 if (copy_to_sockptr_offset(optval, out_offset,
2296 &matched_keys, sizeof(u32)))
2302 int tcp_ao_get_mkts(struct sock *sk, sockptr_t optval, sockptr_t optlen)
2304 struct tcp_ao_info *ao_info;
2306 ao_info = setsockopt_ao_info(sk);
2307 if (IS_ERR(ao_info))
2308 return PTR_ERR(ao_info);
2312 return tcp_ao_copy_mkts_to_user(ao_info, optval, optlen);
2315 int tcp_ao_get_sock_info(struct sock *sk, sockptr_t optval, sockptr_t optlen)
2317 struct tcp_ao_info_opt out, in = {};
2318 struct tcp_ao_key *current_key;
2319 struct tcp_ao_info *ao;
2322 if (copy_from_sockptr(&len, optlen, sizeof(int)))
2328 /* Copying this "in" only to check ::reserved, ::reserved2,
2329 * that may be needed to extend (struct tcp_ao_info_opt) and
2330 * what getsockopt() provides in future.
2332 err = copy_struct_from_sockptr(&in, sizeof(in), optval, len);
2336 if (in.reserved != 0 || in.reserved2 != 0)
2339 ao = setsockopt_ao_info(sk);
2345 memset(&out, 0, sizeof(out));
2346 out.ao_required = ao->ao_required;
2347 out.accept_icmps = ao->accept_icmps;
2348 out.pkt_good = atomic64_read(&ao->counters.pkt_good);
2349 out.pkt_bad = atomic64_read(&ao->counters.pkt_bad);
2350 out.pkt_key_not_found = atomic64_read(&ao->counters.key_not_found);
2351 out.pkt_ao_required = atomic64_read(&ao->counters.ao_required);
2352 out.pkt_dropped_icmp = atomic64_read(&ao->counters.dropped_icmp);
2354 current_key = READ_ONCE(ao->current_key);
2356 out.set_current = 1;
2357 out.current_key = current_key->sndid;
2359 if (ao->rnext_key) {
2361 out.rnext = ao->rnext_key->rcvid;
2364 if (copy_to_sockptr(optval, &out, min_t(int, len, sizeof(out))))
2370 int tcp_ao_set_repair(struct sock *sk, sockptr_t optval, unsigned int optlen)
2372 struct tcp_sock *tp = tcp_sk(sk);
2373 struct tcp_ao_repair cmd;
2374 struct tcp_ao_key *key;
2375 struct tcp_ao_info *ao;
2378 if (optlen < sizeof(cmd))
2381 err = copy_struct_from_sockptr(&cmd, sizeof(cmd), optval, optlen);
2388 ao = setsockopt_ao_info(sk);
2394 WRITE_ONCE(ao->lisn, cmd.snt_isn);
2395 WRITE_ONCE(ao->risn, cmd.rcv_isn);
2396 WRITE_ONCE(ao->snd_sne, cmd.snd_sne);
2397 WRITE_ONCE(ao->rcv_sne, cmd.rcv_sne);
2399 hlist_for_each_entry_rcu(key, &ao->head, node)
2400 tcp_ao_cache_traffic_keys(sk, ao, key);
2405 int tcp_ao_get_repair(struct sock *sk, sockptr_t optval, sockptr_t optlen)
2407 struct tcp_sock *tp = tcp_sk(sk);
2408 struct tcp_ao_repair opt;
2409 struct tcp_ao_info *ao;
2412 if (copy_from_sockptr(&len, optlen, sizeof(int)))
2422 ao = getsockopt_ao_info(sk);
2423 if (IS_ERR_OR_NULL(ao)) {
2425 return ao ? PTR_ERR(ao) : -ENOENT;
2428 opt.snt_isn = ao->lisn;
2429 opt.rcv_isn = ao->risn;
2430 opt.snd_sne = READ_ONCE(ao->snd_sne);
2431 opt.rcv_sne = READ_ONCE(ao->rcv_sne);
2434 if (copy_to_sockptr(optval, &opt, min_t(int, len, sizeof(opt))))