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