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