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