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