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