1 // SPDX-License-Identifier: GPL-2.0 2 /* Multipath TCP 3 * 4 * Copyright (c) 2020, Red Hat, Inc. 5 */ 6 7 #define pr_fmt(fmt) "MPTCP: " fmt 8 9 #include <linux/inet.h> 10 #include <linux/kernel.h> 11 #include <net/tcp.h> 12 #include <net/netns/generic.h> 13 #include <net/mptcp.h> 14 #include <net/genetlink.h> 15 #include <uapi/linux/mptcp.h> 16 17 #include "protocol.h" 18 #include "mib.h" 19 20 /* forward declaration */ 21 static struct genl_family mptcp_genl_family; 22 23 static int pm_nl_pernet_id; 24 25 struct mptcp_pm_addr_entry { 26 struct list_head list; 27 struct mptcp_addr_info addr; 28 u8 flags; 29 int ifindex; 30 struct socket *lsk; 31 }; 32 33 struct mptcp_pm_add_entry { 34 struct list_head list; 35 struct mptcp_addr_info addr; 36 struct timer_list add_timer; 37 struct mptcp_sock *sock; 38 u8 retrans_times; 39 }; 40 41 #define MAX_ADDR_ID 255 42 #define BITMAP_SZ DIV_ROUND_UP(MAX_ADDR_ID + 1, BITS_PER_LONG) 43 44 struct pm_nl_pernet { 45 /* protects pernet updates */ 46 spinlock_t lock; 47 struct list_head local_addr_list; 48 unsigned int addrs; 49 unsigned int stale_loss_cnt; 50 unsigned int add_addr_signal_max; 51 unsigned int add_addr_accept_max; 52 unsigned int local_addr_max; 53 unsigned int subflows_max; 54 unsigned int next_id; 55 unsigned long id_bitmap[BITMAP_SZ]; 56 }; 57 58 #define MPTCP_PM_ADDR_MAX 8 59 #define ADD_ADDR_RETRANS_MAX 3 60 61 static bool addresses_equal(const struct mptcp_addr_info *a, 62 struct mptcp_addr_info *b, bool use_port) 63 { 64 bool addr_equals = false; 65 66 if (a->family == b->family) { 67 if (a->family == AF_INET) 68 addr_equals = a->addr.s_addr == b->addr.s_addr; 69 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 70 else 71 addr_equals = !ipv6_addr_cmp(&a->addr6, &b->addr6); 72 } else if (a->family == AF_INET) { 73 if (ipv6_addr_v4mapped(&b->addr6)) 74 addr_equals = a->addr.s_addr == b->addr6.s6_addr32[3]; 75 } else if (b->family == AF_INET) { 76 if (ipv6_addr_v4mapped(&a->addr6)) 77 addr_equals = a->addr6.s6_addr32[3] == b->addr.s_addr; 78 #endif 79 } 80 81 if (!addr_equals) 82 return false; 83 if (!use_port) 84 return true; 85 86 return a->port == b->port; 87 } 88 89 static bool address_zero(const struct mptcp_addr_info *addr) 90 { 91 struct mptcp_addr_info zero; 92 93 memset(&zero, 0, sizeof(zero)); 94 zero.family = addr->family; 95 96 return addresses_equal(addr, &zero, true); 97 } 98 99 static void local_address(const struct sock_common *skc, 100 struct mptcp_addr_info *addr) 101 { 102 addr->family = skc->skc_family; 103 addr->port = htons(skc->skc_num); 104 if (addr->family == AF_INET) 105 addr->addr.s_addr = skc->skc_rcv_saddr; 106 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 107 else if (addr->family == AF_INET6) 108 addr->addr6 = skc->skc_v6_rcv_saddr; 109 #endif 110 } 111 112 static void remote_address(const struct sock_common *skc, 113 struct mptcp_addr_info *addr) 114 { 115 addr->family = skc->skc_family; 116 addr->port = skc->skc_dport; 117 if (addr->family == AF_INET) 118 addr->addr.s_addr = skc->skc_daddr; 119 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 120 else if (addr->family == AF_INET6) 121 addr->addr6 = skc->skc_v6_daddr; 122 #endif 123 } 124 125 static bool lookup_subflow_by_saddr(const struct list_head *list, 126 struct mptcp_addr_info *saddr) 127 { 128 struct mptcp_subflow_context *subflow; 129 struct mptcp_addr_info cur; 130 struct sock_common *skc; 131 132 list_for_each_entry(subflow, list, node) { 133 skc = (struct sock_common *)mptcp_subflow_tcp_sock(subflow); 134 135 local_address(skc, &cur); 136 if (addresses_equal(&cur, saddr, saddr->port)) 137 return true; 138 } 139 140 return false; 141 } 142 143 static bool lookup_subflow_by_daddr(const struct list_head *list, 144 struct mptcp_addr_info *daddr) 145 { 146 struct mptcp_subflow_context *subflow; 147 struct mptcp_addr_info cur; 148 struct sock_common *skc; 149 150 list_for_each_entry(subflow, list, node) { 151 skc = (struct sock_common *)mptcp_subflow_tcp_sock(subflow); 152 153 remote_address(skc, &cur); 154 if (addresses_equal(&cur, daddr, daddr->port)) 155 return true; 156 } 157 158 return false; 159 } 160 161 static struct mptcp_pm_addr_entry * 162 select_local_address(const struct pm_nl_pernet *pernet, 163 struct mptcp_sock *msk) 164 { 165 struct mptcp_pm_addr_entry *entry, *ret = NULL; 166 struct sock *sk = (struct sock *)msk; 167 168 msk_owned_by_me(msk); 169 170 rcu_read_lock(); 171 __mptcp_flush_join_list(msk); 172 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) { 173 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW)) 174 continue; 175 176 if (entry->addr.family != sk->sk_family) { 177 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 178 if ((entry->addr.family == AF_INET && 179 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) || 180 (sk->sk_family == AF_INET && 181 !ipv6_addr_v4mapped(&entry->addr.addr6))) 182 #endif 183 continue; 184 } 185 186 /* avoid any address already in use by subflows and 187 * pending join 188 */ 189 if (!lookup_subflow_by_saddr(&msk->conn_list, &entry->addr)) { 190 ret = entry; 191 break; 192 } 193 } 194 rcu_read_unlock(); 195 return ret; 196 } 197 198 static struct mptcp_pm_addr_entry * 199 select_signal_address(struct pm_nl_pernet *pernet, unsigned int pos) 200 { 201 struct mptcp_pm_addr_entry *entry, *ret = NULL; 202 int i = 0; 203 204 rcu_read_lock(); 205 /* do not keep any additional per socket state, just signal 206 * the address list in order. 207 * Note: removal from the local address list during the msk life-cycle 208 * can lead to additional addresses not being announced. 209 */ 210 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) { 211 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) 212 continue; 213 if (i++ == pos) { 214 ret = entry; 215 break; 216 } 217 } 218 rcu_read_unlock(); 219 return ret; 220 } 221 222 unsigned int mptcp_pm_get_add_addr_signal_max(struct mptcp_sock *msk) 223 { 224 struct pm_nl_pernet *pernet; 225 226 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id); 227 return READ_ONCE(pernet->add_addr_signal_max); 228 } 229 EXPORT_SYMBOL_GPL(mptcp_pm_get_add_addr_signal_max); 230 231 unsigned int mptcp_pm_get_add_addr_accept_max(struct mptcp_sock *msk) 232 { 233 struct pm_nl_pernet *pernet; 234 235 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id); 236 return READ_ONCE(pernet->add_addr_accept_max); 237 } 238 EXPORT_SYMBOL_GPL(mptcp_pm_get_add_addr_accept_max); 239 240 unsigned int mptcp_pm_get_subflows_max(struct mptcp_sock *msk) 241 { 242 struct pm_nl_pernet *pernet; 243 244 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id); 245 return READ_ONCE(pernet->subflows_max); 246 } 247 EXPORT_SYMBOL_GPL(mptcp_pm_get_subflows_max); 248 249 unsigned int mptcp_pm_get_local_addr_max(struct mptcp_sock *msk) 250 { 251 struct pm_nl_pernet *pernet; 252 253 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id); 254 return READ_ONCE(pernet->local_addr_max); 255 } 256 EXPORT_SYMBOL_GPL(mptcp_pm_get_local_addr_max); 257 258 static void check_work_pending(struct mptcp_sock *msk) 259 { 260 if (msk->pm.add_addr_signaled == mptcp_pm_get_add_addr_signal_max(msk) && 261 (msk->pm.local_addr_used == mptcp_pm_get_local_addr_max(msk) || 262 msk->pm.subflows == mptcp_pm_get_subflows_max(msk))) 263 WRITE_ONCE(msk->pm.work_pending, false); 264 } 265 266 struct mptcp_pm_add_entry * 267 mptcp_lookup_anno_list_by_saddr(struct mptcp_sock *msk, 268 struct mptcp_addr_info *addr) 269 { 270 struct mptcp_pm_add_entry *entry; 271 272 lockdep_assert_held(&msk->pm.lock); 273 274 list_for_each_entry(entry, &msk->pm.anno_list, list) { 275 if (addresses_equal(&entry->addr, addr, true)) 276 return entry; 277 } 278 279 return NULL; 280 } 281 282 bool mptcp_pm_sport_in_anno_list(struct mptcp_sock *msk, const struct sock *sk) 283 { 284 struct mptcp_pm_add_entry *entry; 285 struct mptcp_addr_info saddr; 286 bool ret = false; 287 288 local_address((struct sock_common *)sk, &saddr); 289 290 spin_lock_bh(&msk->pm.lock); 291 list_for_each_entry(entry, &msk->pm.anno_list, list) { 292 if (addresses_equal(&entry->addr, &saddr, true)) { 293 ret = true; 294 goto out; 295 } 296 } 297 298 out: 299 spin_unlock_bh(&msk->pm.lock); 300 return ret; 301 } 302 303 static void mptcp_pm_add_timer(struct timer_list *timer) 304 { 305 struct mptcp_pm_add_entry *entry = from_timer(entry, timer, add_timer); 306 struct mptcp_sock *msk = entry->sock; 307 struct sock *sk = (struct sock *)msk; 308 309 pr_debug("msk=%p", msk); 310 311 if (!msk) 312 return; 313 314 if (inet_sk_state_load(sk) == TCP_CLOSE) 315 return; 316 317 if (!entry->addr.id) 318 return; 319 320 if (mptcp_pm_should_add_signal_addr(msk)) { 321 sk_reset_timer(sk, timer, jiffies + TCP_RTO_MAX / 8); 322 goto out; 323 } 324 325 spin_lock_bh(&msk->pm.lock); 326 327 if (!mptcp_pm_should_add_signal_addr(msk)) { 328 pr_debug("retransmit ADD_ADDR id=%d", entry->addr.id); 329 mptcp_pm_announce_addr(msk, &entry->addr, false); 330 mptcp_pm_add_addr_send_ack(msk); 331 entry->retrans_times++; 332 } 333 334 if (entry->retrans_times < ADD_ADDR_RETRANS_MAX) 335 sk_reset_timer(sk, timer, 336 jiffies + mptcp_get_add_addr_timeout(sock_net(sk))); 337 338 spin_unlock_bh(&msk->pm.lock); 339 340 if (entry->retrans_times == ADD_ADDR_RETRANS_MAX) 341 mptcp_pm_subflow_established(msk); 342 343 out: 344 __sock_put(sk); 345 } 346 347 struct mptcp_pm_add_entry * 348 mptcp_pm_del_add_timer(struct mptcp_sock *msk, 349 struct mptcp_addr_info *addr, bool check_id) 350 { 351 struct mptcp_pm_add_entry *entry; 352 struct sock *sk = (struct sock *)msk; 353 354 spin_lock_bh(&msk->pm.lock); 355 entry = mptcp_lookup_anno_list_by_saddr(msk, addr); 356 if (entry && (!check_id || entry->addr.id == addr->id)) 357 entry->retrans_times = ADD_ADDR_RETRANS_MAX; 358 spin_unlock_bh(&msk->pm.lock); 359 360 if (entry && (!check_id || entry->addr.id == addr->id)) 361 sk_stop_timer_sync(sk, &entry->add_timer); 362 363 return entry; 364 } 365 366 static bool mptcp_pm_alloc_anno_list(struct mptcp_sock *msk, 367 struct mptcp_pm_addr_entry *entry) 368 { 369 struct mptcp_pm_add_entry *add_entry = NULL; 370 struct sock *sk = (struct sock *)msk; 371 struct net *net = sock_net(sk); 372 373 lockdep_assert_held(&msk->pm.lock); 374 375 if (mptcp_lookup_anno_list_by_saddr(msk, &entry->addr)) 376 return false; 377 378 add_entry = kmalloc(sizeof(*add_entry), GFP_ATOMIC); 379 if (!add_entry) 380 return false; 381 382 list_add(&add_entry->list, &msk->pm.anno_list); 383 384 add_entry->addr = entry->addr; 385 add_entry->sock = msk; 386 add_entry->retrans_times = 0; 387 388 timer_setup(&add_entry->add_timer, mptcp_pm_add_timer, 0); 389 sk_reset_timer(sk, &add_entry->add_timer, 390 jiffies + mptcp_get_add_addr_timeout(net)); 391 392 return true; 393 } 394 395 void mptcp_pm_free_anno_list(struct mptcp_sock *msk) 396 { 397 struct mptcp_pm_add_entry *entry, *tmp; 398 struct sock *sk = (struct sock *)msk; 399 LIST_HEAD(free_list); 400 401 pr_debug("msk=%p", msk); 402 403 spin_lock_bh(&msk->pm.lock); 404 list_splice_init(&msk->pm.anno_list, &free_list); 405 spin_unlock_bh(&msk->pm.lock); 406 407 list_for_each_entry_safe(entry, tmp, &free_list, list) { 408 sk_stop_timer_sync(sk, &entry->add_timer); 409 kfree(entry); 410 } 411 } 412 413 static bool lookup_address_in_vec(struct mptcp_addr_info *addrs, unsigned int nr, 414 struct mptcp_addr_info *addr) 415 { 416 int i; 417 418 for (i = 0; i < nr; i++) { 419 if (addresses_equal(&addrs[i], addr, addr->port)) 420 return true; 421 } 422 423 return false; 424 } 425 426 /* Fill all the remote addresses into the array addrs[], 427 * and return the array size. 428 */ 429 static unsigned int fill_remote_addresses_vec(struct mptcp_sock *msk, bool fullmesh, 430 struct mptcp_addr_info *addrs) 431 { 432 struct sock *sk = (struct sock *)msk, *ssk; 433 struct mptcp_subflow_context *subflow; 434 struct mptcp_addr_info remote = { 0 }; 435 unsigned int subflows_max; 436 int i = 0; 437 438 subflows_max = mptcp_pm_get_subflows_max(msk); 439 440 /* Non-fullmesh endpoint, fill in the single entry 441 * corresponding to the primary MPC subflow remote address 442 */ 443 if (!fullmesh) { 444 remote_address((struct sock_common *)sk, &remote); 445 msk->pm.subflows++; 446 addrs[i++] = remote; 447 } else { 448 mptcp_for_each_subflow(msk, subflow) { 449 ssk = mptcp_subflow_tcp_sock(subflow); 450 remote_address((struct sock_common *)ssk, &remote); 451 if (!lookup_address_in_vec(addrs, i, &remote) && 452 msk->pm.subflows < subflows_max) { 453 msk->pm.subflows++; 454 addrs[i++] = remote; 455 } 456 } 457 } 458 459 return i; 460 } 461 462 static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk) 463 { 464 struct sock *sk = (struct sock *)msk; 465 struct mptcp_pm_addr_entry *local; 466 unsigned int add_addr_signal_max; 467 unsigned int local_addr_max; 468 struct pm_nl_pernet *pernet; 469 unsigned int subflows_max; 470 471 pernet = net_generic(sock_net(sk), pm_nl_pernet_id); 472 473 add_addr_signal_max = mptcp_pm_get_add_addr_signal_max(msk); 474 local_addr_max = mptcp_pm_get_local_addr_max(msk); 475 subflows_max = mptcp_pm_get_subflows_max(msk); 476 477 pr_debug("local %d:%d signal %d:%d subflows %d:%d\n", 478 msk->pm.local_addr_used, local_addr_max, 479 msk->pm.add_addr_signaled, add_addr_signal_max, 480 msk->pm.subflows, subflows_max); 481 482 /* check first for announce */ 483 if (msk->pm.add_addr_signaled < add_addr_signal_max) { 484 local = select_signal_address(pernet, 485 msk->pm.add_addr_signaled); 486 487 if (local) { 488 if (mptcp_pm_alloc_anno_list(msk, local)) { 489 msk->pm.add_addr_signaled++; 490 mptcp_pm_announce_addr(msk, &local->addr, false); 491 mptcp_pm_nl_addr_send_ack(msk); 492 } 493 } else { 494 /* pick failed, avoid fourther attempts later */ 495 msk->pm.local_addr_used = add_addr_signal_max; 496 } 497 498 check_work_pending(msk); 499 } 500 501 /* check if should create a new subflow */ 502 if (msk->pm.local_addr_used < local_addr_max && 503 msk->pm.subflows < subflows_max && 504 !READ_ONCE(msk->pm.remote_deny_join_id0)) { 505 local = select_local_address(pernet, msk); 506 if (local) { 507 bool fullmesh = !!(local->flags & MPTCP_PM_ADDR_FLAG_FULLMESH); 508 struct mptcp_addr_info addrs[MPTCP_PM_ADDR_MAX]; 509 int i, nr; 510 511 msk->pm.local_addr_used++; 512 check_work_pending(msk); 513 nr = fill_remote_addresses_vec(msk, fullmesh, addrs); 514 spin_unlock_bh(&msk->pm.lock); 515 for (i = 0; i < nr; i++) 516 __mptcp_subflow_connect(sk, &local->addr, &addrs[i]); 517 spin_lock_bh(&msk->pm.lock); 518 return; 519 } 520 521 /* lookup failed, avoid fourther attempts later */ 522 msk->pm.local_addr_used = local_addr_max; 523 check_work_pending(msk); 524 } 525 } 526 527 static void mptcp_pm_nl_fully_established(struct mptcp_sock *msk) 528 { 529 mptcp_pm_create_subflow_or_signal_addr(msk); 530 } 531 532 static void mptcp_pm_nl_subflow_established(struct mptcp_sock *msk) 533 { 534 mptcp_pm_create_subflow_or_signal_addr(msk); 535 } 536 537 /* Fill all the local addresses into the array addrs[], 538 * and return the array size. 539 */ 540 static unsigned int fill_local_addresses_vec(struct mptcp_sock *msk, 541 struct mptcp_addr_info *addrs) 542 { 543 struct sock *sk = (struct sock *)msk; 544 struct mptcp_pm_addr_entry *entry; 545 struct mptcp_addr_info local; 546 struct pm_nl_pernet *pernet; 547 unsigned int subflows_max; 548 int i = 0; 549 550 pernet = net_generic(sock_net(sk), pm_nl_pernet_id); 551 subflows_max = mptcp_pm_get_subflows_max(msk); 552 553 rcu_read_lock(); 554 __mptcp_flush_join_list(msk); 555 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) { 556 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_FULLMESH)) 557 continue; 558 559 if (entry->addr.family != sk->sk_family) { 560 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 561 if ((entry->addr.family == AF_INET && 562 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) || 563 (sk->sk_family == AF_INET && 564 !ipv6_addr_v4mapped(&entry->addr.addr6))) 565 #endif 566 continue; 567 } 568 569 if (msk->pm.subflows < subflows_max) { 570 msk->pm.subflows++; 571 addrs[i++] = entry->addr; 572 } 573 } 574 rcu_read_unlock(); 575 576 /* If the array is empty, fill in the single 577 * 'IPADDRANY' local address 578 */ 579 if (!i) { 580 memset(&local, 0, sizeof(local)); 581 local.family = msk->pm.remote.family; 582 583 msk->pm.subflows++; 584 addrs[i++] = local; 585 } 586 587 return i; 588 } 589 590 static void mptcp_pm_nl_add_addr_received(struct mptcp_sock *msk) 591 { 592 struct mptcp_addr_info addrs[MPTCP_PM_ADDR_MAX]; 593 struct sock *sk = (struct sock *)msk; 594 unsigned int add_addr_accept_max; 595 struct mptcp_addr_info remote; 596 unsigned int subflows_max; 597 int i, nr; 598 599 add_addr_accept_max = mptcp_pm_get_add_addr_accept_max(msk); 600 subflows_max = mptcp_pm_get_subflows_max(msk); 601 602 pr_debug("accepted %d:%d remote family %d", 603 msk->pm.add_addr_accepted, add_addr_accept_max, 604 msk->pm.remote.family); 605 606 if (lookup_subflow_by_daddr(&msk->conn_list, &msk->pm.remote)) 607 goto add_addr_echo; 608 609 /* connect to the specified remote address, using whatever 610 * local address the routing configuration will pick. 611 */ 612 remote = msk->pm.remote; 613 if (!remote.port) 614 remote.port = sk->sk_dport; 615 nr = fill_local_addresses_vec(msk, addrs); 616 617 msk->pm.add_addr_accepted++; 618 if (msk->pm.add_addr_accepted >= add_addr_accept_max || 619 msk->pm.subflows >= subflows_max) 620 WRITE_ONCE(msk->pm.accept_addr, false); 621 622 spin_unlock_bh(&msk->pm.lock); 623 for (i = 0; i < nr; i++) 624 __mptcp_subflow_connect(sk, &addrs[i], &remote); 625 spin_lock_bh(&msk->pm.lock); 626 627 add_addr_echo: 628 mptcp_pm_announce_addr(msk, &msk->pm.remote, true); 629 mptcp_pm_nl_addr_send_ack(msk); 630 } 631 632 void mptcp_pm_nl_addr_send_ack(struct mptcp_sock *msk) 633 { 634 struct mptcp_subflow_context *subflow; 635 636 msk_owned_by_me(msk); 637 lockdep_assert_held(&msk->pm.lock); 638 639 if (!mptcp_pm_should_add_signal(msk) && 640 !mptcp_pm_should_rm_signal(msk)) 641 return; 642 643 __mptcp_flush_join_list(msk); 644 subflow = list_first_entry_or_null(&msk->conn_list, typeof(*subflow), node); 645 if (subflow) { 646 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 647 bool slow; 648 649 spin_unlock_bh(&msk->pm.lock); 650 pr_debug("send ack for %s", 651 mptcp_pm_should_add_signal(msk) ? "add_addr" : "rm_addr"); 652 653 slow = lock_sock_fast(ssk); 654 tcp_send_ack(ssk); 655 unlock_sock_fast(ssk, slow); 656 spin_lock_bh(&msk->pm.lock); 657 } 658 } 659 660 int mptcp_pm_nl_mp_prio_send_ack(struct mptcp_sock *msk, 661 struct mptcp_addr_info *addr, 662 u8 bkup) 663 { 664 struct mptcp_subflow_context *subflow; 665 666 pr_debug("bkup=%d", bkup); 667 668 mptcp_for_each_subflow(msk, subflow) { 669 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 670 struct sock *sk = (struct sock *)msk; 671 struct mptcp_addr_info local; 672 bool slow; 673 674 local_address((struct sock_common *)ssk, &local); 675 if (!addresses_equal(&local, addr, addr->port)) 676 continue; 677 678 subflow->backup = bkup; 679 subflow->send_mp_prio = 1; 680 subflow->request_bkup = bkup; 681 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIOTX); 682 683 spin_unlock_bh(&msk->pm.lock); 684 pr_debug("send ack for mp_prio"); 685 slow = lock_sock_fast(ssk); 686 tcp_send_ack(ssk); 687 unlock_sock_fast(ssk, slow); 688 spin_lock_bh(&msk->pm.lock); 689 690 return 0; 691 } 692 693 return -EINVAL; 694 } 695 696 static void mptcp_pm_nl_rm_addr_or_subflow(struct mptcp_sock *msk, 697 const struct mptcp_rm_list *rm_list, 698 enum linux_mptcp_mib_field rm_type) 699 { 700 struct mptcp_subflow_context *subflow, *tmp; 701 struct sock *sk = (struct sock *)msk; 702 u8 i; 703 704 pr_debug("%s rm_list_nr %d", 705 rm_type == MPTCP_MIB_RMADDR ? "address" : "subflow", rm_list->nr); 706 707 msk_owned_by_me(msk); 708 709 if (!rm_list->nr) 710 return; 711 712 if (list_empty(&msk->conn_list)) 713 return; 714 715 for (i = 0; i < rm_list->nr; i++) { 716 list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) { 717 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 718 int how = RCV_SHUTDOWN | SEND_SHUTDOWN; 719 u8 id = subflow->local_id; 720 721 if (rm_type == MPTCP_MIB_RMADDR) 722 id = subflow->remote_id; 723 724 if (rm_list->ids[i] != id) 725 continue; 726 727 pr_debug(" -> %s rm_list_ids[%d]=%u local_id=%u remote_id=%u", 728 rm_type == MPTCP_MIB_RMADDR ? "address" : "subflow", 729 i, rm_list->ids[i], subflow->local_id, subflow->remote_id); 730 spin_unlock_bh(&msk->pm.lock); 731 mptcp_subflow_shutdown(sk, ssk, how); 732 mptcp_close_ssk(sk, ssk, subflow); 733 spin_lock_bh(&msk->pm.lock); 734 735 if (rm_type == MPTCP_MIB_RMADDR) { 736 msk->pm.add_addr_accepted--; 737 WRITE_ONCE(msk->pm.accept_addr, true); 738 } else if (rm_type == MPTCP_MIB_RMSUBFLOW) { 739 msk->pm.local_addr_used--; 740 } 741 msk->pm.subflows--; 742 __MPTCP_INC_STATS(sock_net(sk), rm_type); 743 } 744 } 745 } 746 747 static void mptcp_pm_nl_rm_addr_received(struct mptcp_sock *msk) 748 { 749 mptcp_pm_nl_rm_addr_or_subflow(msk, &msk->pm.rm_list_rx, MPTCP_MIB_RMADDR); 750 } 751 752 void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk, 753 const struct mptcp_rm_list *rm_list) 754 { 755 mptcp_pm_nl_rm_addr_or_subflow(msk, rm_list, MPTCP_MIB_RMSUBFLOW); 756 } 757 758 void mptcp_pm_nl_work(struct mptcp_sock *msk) 759 { 760 struct mptcp_pm_data *pm = &msk->pm; 761 762 msk_owned_by_me(msk); 763 764 spin_lock_bh(&msk->pm.lock); 765 766 pr_debug("msk=%p status=%x", msk, pm->status); 767 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_RECEIVED)) { 768 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_RECEIVED); 769 mptcp_pm_nl_add_addr_received(msk); 770 } 771 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_SEND_ACK)) { 772 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_SEND_ACK); 773 mptcp_pm_nl_addr_send_ack(msk); 774 } 775 if (pm->status & BIT(MPTCP_PM_RM_ADDR_RECEIVED)) { 776 pm->status &= ~BIT(MPTCP_PM_RM_ADDR_RECEIVED); 777 mptcp_pm_nl_rm_addr_received(msk); 778 } 779 if (pm->status & BIT(MPTCP_PM_ESTABLISHED)) { 780 pm->status &= ~BIT(MPTCP_PM_ESTABLISHED); 781 mptcp_pm_nl_fully_established(msk); 782 } 783 if (pm->status & BIT(MPTCP_PM_SUBFLOW_ESTABLISHED)) { 784 pm->status &= ~BIT(MPTCP_PM_SUBFLOW_ESTABLISHED); 785 mptcp_pm_nl_subflow_established(msk); 786 } 787 788 spin_unlock_bh(&msk->pm.lock); 789 } 790 791 static bool address_use_port(struct mptcp_pm_addr_entry *entry) 792 { 793 return (entry->flags & 794 (MPTCP_PM_ADDR_FLAG_SIGNAL | MPTCP_PM_ADDR_FLAG_SUBFLOW)) == 795 MPTCP_PM_ADDR_FLAG_SIGNAL; 796 } 797 798 static int mptcp_pm_nl_append_new_local_addr(struct pm_nl_pernet *pernet, 799 struct mptcp_pm_addr_entry *entry) 800 { 801 struct mptcp_pm_addr_entry *cur; 802 unsigned int addr_max; 803 int ret = -EINVAL; 804 805 spin_lock_bh(&pernet->lock); 806 /* to keep the code simple, don't do IDR-like allocation for address ID, 807 * just bail when we exceed limits 808 */ 809 if (pernet->next_id == MAX_ADDR_ID) 810 pernet->next_id = 1; 811 if (pernet->addrs >= MPTCP_PM_ADDR_MAX) 812 goto out; 813 if (test_bit(entry->addr.id, pernet->id_bitmap)) 814 goto out; 815 816 /* do not insert duplicate address, differentiate on port only 817 * singled addresses 818 */ 819 list_for_each_entry(cur, &pernet->local_addr_list, list) { 820 if (addresses_equal(&cur->addr, &entry->addr, 821 address_use_port(entry) && 822 address_use_port(cur))) 823 goto out; 824 } 825 826 if (!entry->addr.id) { 827 find_next: 828 entry->addr.id = find_next_zero_bit(pernet->id_bitmap, 829 MAX_ADDR_ID + 1, 830 pernet->next_id); 831 if ((!entry->addr.id || entry->addr.id > MAX_ADDR_ID) && 832 pernet->next_id != 1) { 833 pernet->next_id = 1; 834 goto find_next; 835 } 836 } 837 838 if (!entry->addr.id || entry->addr.id > MAX_ADDR_ID) 839 goto out; 840 841 __set_bit(entry->addr.id, pernet->id_bitmap); 842 if (entry->addr.id > pernet->next_id) 843 pernet->next_id = entry->addr.id; 844 845 if (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) { 846 addr_max = pernet->add_addr_signal_max; 847 WRITE_ONCE(pernet->add_addr_signal_max, addr_max + 1); 848 } 849 if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) { 850 addr_max = pernet->local_addr_max; 851 WRITE_ONCE(pernet->local_addr_max, addr_max + 1); 852 } 853 854 pernet->addrs++; 855 list_add_tail_rcu(&entry->list, &pernet->local_addr_list); 856 ret = entry->addr.id; 857 858 out: 859 spin_unlock_bh(&pernet->lock); 860 return ret; 861 } 862 863 static int mptcp_pm_nl_create_listen_socket(struct sock *sk, 864 struct mptcp_pm_addr_entry *entry) 865 { 866 struct sockaddr_storage addr; 867 struct mptcp_sock *msk; 868 struct socket *ssock; 869 int backlog = 1024; 870 int err; 871 872 err = sock_create_kern(sock_net(sk), entry->addr.family, 873 SOCK_STREAM, IPPROTO_MPTCP, &entry->lsk); 874 if (err) 875 return err; 876 877 msk = mptcp_sk(entry->lsk->sk); 878 if (!msk) { 879 err = -EINVAL; 880 goto out; 881 } 882 883 ssock = __mptcp_nmpc_socket(msk); 884 if (!ssock) { 885 err = -EINVAL; 886 goto out; 887 } 888 889 mptcp_info2sockaddr(&entry->addr, &addr, entry->addr.family); 890 err = kernel_bind(ssock, (struct sockaddr *)&addr, 891 sizeof(struct sockaddr_in)); 892 if (err) { 893 pr_warn("kernel_bind error, err=%d", err); 894 goto out; 895 } 896 897 err = kernel_listen(ssock, backlog); 898 if (err) { 899 pr_warn("kernel_listen error, err=%d", err); 900 goto out; 901 } 902 903 return 0; 904 905 out: 906 sock_release(entry->lsk); 907 return err; 908 } 909 910 int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc) 911 { 912 struct mptcp_pm_addr_entry *entry; 913 struct mptcp_addr_info skc_local; 914 struct mptcp_addr_info msk_local; 915 struct pm_nl_pernet *pernet; 916 int ret = -1; 917 918 if (WARN_ON_ONCE(!msk)) 919 return -1; 920 921 /* The 0 ID mapping is defined by the first subflow, copied into the msk 922 * addr 923 */ 924 local_address((struct sock_common *)msk, &msk_local); 925 local_address((struct sock_common *)skc, &skc_local); 926 if (addresses_equal(&msk_local, &skc_local, false)) 927 return 0; 928 929 if (address_zero(&skc_local)) 930 return 0; 931 932 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id); 933 934 rcu_read_lock(); 935 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) { 936 if (addresses_equal(&entry->addr, &skc_local, entry->addr.port)) { 937 ret = entry->addr.id; 938 break; 939 } 940 } 941 rcu_read_unlock(); 942 if (ret >= 0) 943 return ret; 944 945 /* address not found, add to local list */ 946 entry = kmalloc(sizeof(*entry), GFP_ATOMIC); 947 if (!entry) 948 return -ENOMEM; 949 950 entry->addr = skc_local; 951 entry->addr.id = 0; 952 entry->addr.port = 0; 953 entry->ifindex = 0; 954 entry->flags = 0; 955 entry->lsk = NULL; 956 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry); 957 if (ret < 0) 958 kfree(entry); 959 960 return ret; 961 } 962 963 void mptcp_pm_nl_data_init(struct mptcp_sock *msk) 964 { 965 struct mptcp_pm_data *pm = &msk->pm; 966 bool subflows; 967 968 subflows = !!mptcp_pm_get_subflows_max(msk); 969 WRITE_ONCE(pm->work_pending, (!!mptcp_pm_get_local_addr_max(msk) && subflows) || 970 !!mptcp_pm_get_add_addr_signal_max(msk)); 971 WRITE_ONCE(pm->accept_addr, !!mptcp_pm_get_add_addr_accept_max(msk) && subflows); 972 WRITE_ONCE(pm->accept_subflow, subflows); 973 } 974 975 #define MPTCP_PM_CMD_GRP_OFFSET 0 976 #define MPTCP_PM_EV_GRP_OFFSET 1 977 978 static const struct genl_multicast_group mptcp_pm_mcgrps[] = { 979 [MPTCP_PM_CMD_GRP_OFFSET] = { .name = MPTCP_PM_CMD_GRP_NAME, }, 980 [MPTCP_PM_EV_GRP_OFFSET] = { .name = MPTCP_PM_EV_GRP_NAME, 981 .flags = GENL_UNS_ADMIN_PERM, 982 }, 983 }; 984 985 static const struct nla_policy 986 mptcp_pm_addr_policy[MPTCP_PM_ADDR_ATTR_MAX + 1] = { 987 [MPTCP_PM_ADDR_ATTR_FAMILY] = { .type = NLA_U16, }, 988 [MPTCP_PM_ADDR_ATTR_ID] = { .type = NLA_U8, }, 989 [MPTCP_PM_ADDR_ATTR_ADDR4] = { .type = NLA_U32, }, 990 [MPTCP_PM_ADDR_ATTR_ADDR6] = 991 NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)), 992 [MPTCP_PM_ADDR_ATTR_PORT] = { .type = NLA_U16 }, 993 [MPTCP_PM_ADDR_ATTR_FLAGS] = { .type = NLA_U32 }, 994 [MPTCP_PM_ADDR_ATTR_IF_IDX] = { .type = NLA_S32 }, 995 }; 996 997 static const struct nla_policy mptcp_pm_policy[MPTCP_PM_ATTR_MAX + 1] = { 998 [MPTCP_PM_ATTR_ADDR] = 999 NLA_POLICY_NESTED(mptcp_pm_addr_policy), 1000 [MPTCP_PM_ATTR_RCV_ADD_ADDRS] = { .type = NLA_U32, }, 1001 [MPTCP_PM_ATTR_SUBFLOWS] = { .type = NLA_U32, }, 1002 }; 1003 1004 void mptcp_pm_nl_subflow_chk_stale(const struct mptcp_sock *msk, struct sock *ssk) 1005 { 1006 struct mptcp_subflow_context *iter, *subflow = mptcp_subflow_ctx(ssk); 1007 struct sock *sk = (struct sock *)msk; 1008 unsigned int active_max_loss_cnt; 1009 struct net *net = sock_net(sk); 1010 unsigned int stale_loss_cnt; 1011 bool slow; 1012 1013 stale_loss_cnt = mptcp_stale_loss_cnt(net); 1014 if (subflow->stale || !stale_loss_cnt || subflow->stale_count <= stale_loss_cnt) 1015 return; 1016 1017 /* look for another available subflow not in loss state */ 1018 active_max_loss_cnt = max_t(int, stale_loss_cnt - 1, 1); 1019 mptcp_for_each_subflow(msk, iter) { 1020 if (iter != subflow && mptcp_subflow_active(iter) && 1021 iter->stale_count < active_max_loss_cnt) { 1022 /* we have some alternatives, try to mark this subflow as idle ...*/ 1023 slow = lock_sock_fast(ssk); 1024 if (!tcp_rtx_and_write_queues_empty(ssk)) { 1025 subflow->stale = 1; 1026 __mptcp_retransmit_pending_data(sk); 1027 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_SUBFLOWSTALE); 1028 } 1029 unlock_sock_fast(ssk, slow); 1030 1031 /* always try to push the pending data regarless of re-injections: 1032 * we can possibly use backup subflows now, and subflow selection 1033 * is cheap under the msk socket lock 1034 */ 1035 __mptcp_push_pending(sk, 0); 1036 return; 1037 } 1038 } 1039 } 1040 1041 static int mptcp_pm_family_to_addr(int family) 1042 { 1043 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1044 if (family == AF_INET6) 1045 return MPTCP_PM_ADDR_ATTR_ADDR6; 1046 #endif 1047 return MPTCP_PM_ADDR_ATTR_ADDR4; 1048 } 1049 1050 static int mptcp_pm_parse_addr(struct nlattr *attr, struct genl_info *info, 1051 bool require_family, 1052 struct mptcp_pm_addr_entry *entry) 1053 { 1054 struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1]; 1055 int err, addr_addr; 1056 1057 if (!attr) { 1058 GENL_SET_ERR_MSG(info, "missing address info"); 1059 return -EINVAL; 1060 } 1061 1062 /* no validation needed - was already done via nested policy */ 1063 err = nla_parse_nested_deprecated(tb, MPTCP_PM_ADDR_ATTR_MAX, attr, 1064 mptcp_pm_addr_policy, info->extack); 1065 if (err) 1066 return err; 1067 1068 memset(entry, 0, sizeof(*entry)); 1069 if (!tb[MPTCP_PM_ADDR_ATTR_FAMILY]) { 1070 if (!require_family) 1071 goto skip_family; 1072 1073 NL_SET_ERR_MSG_ATTR(info->extack, attr, 1074 "missing family"); 1075 return -EINVAL; 1076 } 1077 1078 entry->addr.family = nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_FAMILY]); 1079 if (entry->addr.family != AF_INET 1080 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1081 && entry->addr.family != AF_INET6 1082 #endif 1083 ) { 1084 NL_SET_ERR_MSG_ATTR(info->extack, attr, 1085 "unknown address family"); 1086 return -EINVAL; 1087 } 1088 addr_addr = mptcp_pm_family_to_addr(entry->addr.family); 1089 if (!tb[addr_addr]) { 1090 NL_SET_ERR_MSG_ATTR(info->extack, attr, 1091 "missing address data"); 1092 return -EINVAL; 1093 } 1094 1095 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1096 if (entry->addr.family == AF_INET6) 1097 entry->addr.addr6 = nla_get_in6_addr(tb[addr_addr]); 1098 else 1099 #endif 1100 entry->addr.addr.s_addr = nla_get_in_addr(tb[addr_addr]); 1101 1102 skip_family: 1103 if (tb[MPTCP_PM_ADDR_ATTR_IF_IDX]) { 1104 u32 val = nla_get_s32(tb[MPTCP_PM_ADDR_ATTR_IF_IDX]); 1105 1106 entry->ifindex = val; 1107 } 1108 1109 if (tb[MPTCP_PM_ADDR_ATTR_ID]) 1110 entry->addr.id = nla_get_u8(tb[MPTCP_PM_ADDR_ATTR_ID]); 1111 1112 if (tb[MPTCP_PM_ADDR_ATTR_FLAGS]) 1113 entry->flags = nla_get_u32(tb[MPTCP_PM_ADDR_ATTR_FLAGS]); 1114 1115 if (tb[MPTCP_PM_ADDR_ATTR_PORT]) { 1116 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) { 1117 NL_SET_ERR_MSG_ATTR(info->extack, attr, 1118 "flags must have signal when using port"); 1119 return -EINVAL; 1120 } 1121 entry->addr.port = htons(nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_PORT])); 1122 } 1123 1124 return 0; 1125 } 1126 1127 static struct pm_nl_pernet *genl_info_pm_nl(struct genl_info *info) 1128 { 1129 return net_generic(genl_info_net(info), pm_nl_pernet_id); 1130 } 1131 1132 static int mptcp_nl_add_subflow_or_signal_addr(struct net *net) 1133 { 1134 struct mptcp_sock *msk; 1135 long s_slot = 0, s_num = 0; 1136 1137 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { 1138 struct sock *sk = (struct sock *)msk; 1139 1140 if (!READ_ONCE(msk->fully_established)) 1141 goto next; 1142 1143 lock_sock(sk); 1144 spin_lock_bh(&msk->pm.lock); 1145 mptcp_pm_create_subflow_or_signal_addr(msk); 1146 spin_unlock_bh(&msk->pm.lock); 1147 release_sock(sk); 1148 1149 next: 1150 sock_put(sk); 1151 cond_resched(); 1152 } 1153 1154 return 0; 1155 } 1156 1157 static int mptcp_nl_cmd_add_addr(struct sk_buff *skb, struct genl_info *info) 1158 { 1159 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR]; 1160 struct pm_nl_pernet *pernet = genl_info_pm_nl(info); 1161 struct mptcp_pm_addr_entry addr, *entry; 1162 int ret; 1163 1164 ret = mptcp_pm_parse_addr(attr, info, true, &addr); 1165 if (ret < 0) 1166 return ret; 1167 1168 entry = kmalloc(sizeof(*entry), GFP_KERNEL); 1169 if (!entry) { 1170 GENL_SET_ERR_MSG(info, "can't allocate addr"); 1171 return -ENOMEM; 1172 } 1173 1174 *entry = addr; 1175 if (entry->addr.port) { 1176 ret = mptcp_pm_nl_create_listen_socket(skb->sk, entry); 1177 if (ret) { 1178 GENL_SET_ERR_MSG(info, "create listen socket error"); 1179 kfree(entry); 1180 return ret; 1181 } 1182 } 1183 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry); 1184 if (ret < 0) { 1185 GENL_SET_ERR_MSG(info, "too many addresses or duplicate one"); 1186 if (entry->lsk) 1187 sock_release(entry->lsk); 1188 kfree(entry); 1189 return ret; 1190 } 1191 1192 mptcp_nl_add_subflow_or_signal_addr(sock_net(skb->sk)); 1193 1194 return 0; 1195 } 1196 1197 static struct mptcp_pm_addr_entry * 1198 __lookup_addr_by_id(struct pm_nl_pernet *pernet, unsigned int id) 1199 { 1200 struct mptcp_pm_addr_entry *entry; 1201 1202 list_for_each_entry(entry, &pernet->local_addr_list, list) { 1203 if (entry->addr.id == id) 1204 return entry; 1205 } 1206 return NULL; 1207 } 1208 1209 int mptcp_pm_get_flags_and_ifindex_by_id(struct net *net, unsigned int id, 1210 u8 *flags, int *ifindex) 1211 { 1212 struct mptcp_pm_addr_entry *entry; 1213 1214 *flags = 0; 1215 *ifindex = 0; 1216 1217 if (id) { 1218 rcu_read_lock(); 1219 entry = __lookup_addr_by_id(net_generic(net, pm_nl_pernet_id), id); 1220 if (entry) { 1221 *flags = entry->flags; 1222 *ifindex = entry->ifindex; 1223 } 1224 rcu_read_unlock(); 1225 } 1226 1227 return 0; 1228 } 1229 1230 static bool remove_anno_list_by_saddr(struct mptcp_sock *msk, 1231 struct mptcp_addr_info *addr) 1232 { 1233 struct mptcp_pm_add_entry *entry; 1234 1235 entry = mptcp_pm_del_add_timer(msk, addr, false); 1236 if (entry) { 1237 list_del(&entry->list); 1238 kfree(entry); 1239 return true; 1240 } 1241 1242 return false; 1243 } 1244 1245 static bool mptcp_pm_remove_anno_addr(struct mptcp_sock *msk, 1246 struct mptcp_addr_info *addr, 1247 bool force) 1248 { 1249 struct mptcp_rm_list list = { .nr = 0 }; 1250 bool ret; 1251 1252 list.ids[list.nr++] = addr->id; 1253 1254 ret = remove_anno_list_by_saddr(msk, addr); 1255 if (ret || force) { 1256 spin_lock_bh(&msk->pm.lock); 1257 mptcp_pm_remove_addr(msk, &list); 1258 spin_unlock_bh(&msk->pm.lock); 1259 } 1260 return ret; 1261 } 1262 1263 static int mptcp_nl_remove_subflow_and_signal_addr(struct net *net, 1264 struct mptcp_addr_info *addr) 1265 { 1266 struct mptcp_sock *msk; 1267 long s_slot = 0, s_num = 0; 1268 struct mptcp_rm_list list = { .nr = 0 }; 1269 1270 pr_debug("remove_id=%d", addr->id); 1271 1272 list.ids[list.nr++] = addr->id; 1273 1274 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { 1275 struct sock *sk = (struct sock *)msk; 1276 bool remove_subflow; 1277 1278 if (list_empty(&msk->conn_list)) { 1279 mptcp_pm_remove_anno_addr(msk, addr, false); 1280 goto next; 1281 } 1282 1283 lock_sock(sk); 1284 remove_subflow = lookup_subflow_by_saddr(&msk->conn_list, addr); 1285 mptcp_pm_remove_anno_addr(msk, addr, remove_subflow); 1286 if (remove_subflow) 1287 mptcp_pm_remove_subflow(msk, &list); 1288 release_sock(sk); 1289 1290 next: 1291 sock_put(sk); 1292 cond_resched(); 1293 } 1294 1295 return 0; 1296 } 1297 1298 /* caller must ensure the RCU grace period is already elapsed */ 1299 static void __mptcp_pm_release_addr_entry(struct mptcp_pm_addr_entry *entry) 1300 { 1301 if (entry->lsk) 1302 sock_release(entry->lsk); 1303 kfree(entry); 1304 } 1305 1306 static int mptcp_nl_remove_id_zero_address(struct net *net, 1307 struct mptcp_addr_info *addr) 1308 { 1309 struct mptcp_rm_list list = { .nr = 0 }; 1310 long s_slot = 0, s_num = 0; 1311 struct mptcp_sock *msk; 1312 1313 list.ids[list.nr++] = 0; 1314 1315 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { 1316 struct sock *sk = (struct sock *)msk; 1317 struct mptcp_addr_info msk_local; 1318 1319 if (list_empty(&msk->conn_list)) 1320 goto next; 1321 1322 local_address((struct sock_common *)msk, &msk_local); 1323 if (!addresses_equal(&msk_local, addr, addr->port)) 1324 goto next; 1325 1326 lock_sock(sk); 1327 spin_lock_bh(&msk->pm.lock); 1328 mptcp_pm_remove_addr(msk, &list); 1329 mptcp_pm_nl_rm_subflow_received(msk, &list); 1330 spin_unlock_bh(&msk->pm.lock); 1331 release_sock(sk); 1332 1333 next: 1334 sock_put(sk); 1335 cond_resched(); 1336 } 1337 1338 return 0; 1339 } 1340 1341 static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info) 1342 { 1343 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR]; 1344 struct pm_nl_pernet *pernet = genl_info_pm_nl(info); 1345 struct mptcp_pm_addr_entry addr, *entry; 1346 unsigned int addr_max; 1347 int ret; 1348 1349 ret = mptcp_pm_parse_addr(attr, info, false, &addr); 1350 if (ret < 0) 1351 return ret; 1352 1353 /* the zero id address is special: the first address used by the msk 1354 * always gets such an id, so different subflows can have different zero 1355 * id addresses. Additionally zero id is not accounted for in id_bitmap. 1356 * Let's use an 'mptcp_rm_list' instead of the common remove code. 1357 */ 1358 if (addr.addr.id == 0) 1359 return mptcp_nl_remove_id_zero_address(sock_net(skb->sk), &addr.addr); 1360 1361 spin_lock_bh(&pernet->lock); 1362 entry = __lookup_addr_by_id(pernet, addr.addr.id); 1363 if (!entry) { 1364 GENL_SET_ERR_MSG(info, "address not found"); 1365 spin_unlock_bh(&pernet->lock); 1366 return -EINVAL; 1367 } 1368 if (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) { 1369 addr_max = pernet->add_addr_signal_max; 1370 WRITE_ONCE(pernet->add_addr_signal_max, addr_max - 1); 1371 } 1372 if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) { 1373 addr_max = pernet->local_addr_max; 1374 WRITE_ONCE(pernet->local_addr_max, addr_max - 1); 1375 } 1376 1377 pernet->addrs--; 1378 list_del_rcu(&entry->list); 1379 __clear_bit(entry->addr.id, pernet->id_bitmap); 1380 spin_unlock_bh(&pernet->lock); 1381 1382 mptcp_nl_remove_subflow_and_signal_addr(sock_net(skb->sk), &entry->addr); 1383 synchronize_rcu(); 1384 __mptcp_pm_release_addr_entry(entry); 1385 1386 return ret; 1387 } 1388 1389 static void mptcp_pm_remove_addrs_and_subflows(struct mptcp_sock *msk, 1390 struct list_head *rm_list) 1391 { 1392 struct mptcp_rm_list alist = { .nr = 0 }, slist = { .nr = 0 }; 1393 struct mptcp_pm_addr_entry *entry; 1394 1395 list_for_each_entry(entry, rm_list, list) { 1396 if (lookup_subflow_by_saddr(&msk->conn_list, &entry->addr) && 1397 alist.nr < MPTCP_RM_IDS_MAX && 1398 slist.nr < MPTCP_RM_IDS_MAX) { 1399 alist.ids[alist.nr++] = entry->addr.id; 1400 slist.ids[slist.nr++] = entry->addr.id; 1401 } else if (remove_anno_list_by_saddr(msk, &entry->addr) && 1402 alist.nr < MPTCP_RM_IDS_MAX) { 1403 alist.ids[alist.nr++] = entry->addr.id; 1404 } 1405 } 1406 1407 if (alist.nr) { 1408 spin_lock_bh(&msk->pm.lock); 1409 mptcp_pm_remove_addr(msk, &alist); 1410 spin_unlock_bh(&msk->pm.lock); 1411 } 1412 if (slist.nr) 1413 mptcp_pm_remove_subflow(msk, &slist); 1414 } 1415 1416 static void mptcp_nl_remove_addrs_list(struct net *net, 1417 struct list_head *rm_list) 1418 { 1419 long s_slot = 0, s_num = 0; 1420 struct mptcp_sock *msk; 1421 1422 if (list_empty(rm_list)) 1423 return; 1424 1425 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { 1426 struct sock *sk = (struct sock *)msk; 1427 1428 lock_sock(sk); 1429 mptcp_pm_remove_addrs_and_subflows(msk, rm_list); 1430 release_sock(sk); 1431 1432 sock_put(sk); 1433 cond_resched(); 1434 } 1435 } 1436 1437 /* caller must ensure the RCU grace period is already elapsed */ 1438 static void __flush_addrs(struct list_head *list) 1439 { 1440 while (!list_empty(list)) { 1441 struct mptcp_pm_addr_entry *cur; 1442 1443 cur = list_entry(list->next, 1444 struct mptcp_pm_addr_entry, list); 1445 list_del_rcu(&cur->list); 1446 __mptcp_pm_release_addr_entry(cur); 1447 } 1448 } 1449 1450 static void __reset_counters(struct pm_nl_pernet *pernet) 1451 { 1452 WRITE_ONCE(pernet->add_addr_signal_max, 0); 1453 WRITE_ONCE(pernet->add_addr_accept_max, 0); 1454 WRITE_ONCE(pernet->local_addr_max, 0); 1455 pernet->addrs = 0; 1456 } 1457 1458 static int mptcp_nl_cmd_flush_addrs(struct sk_buff *skb, struct genl_info *info) 1459 { 1460 struct pm_nl_pernet *pernet = genl_info_pm_nl(info); 1461 LIST_HEAD(free_list); 1462 1463 spin_lock_bh(&pernet->lock); 1464 list_splice_init(&pernet->local_addr_list, &free_list); 1465 __reset_counters(pernet); 1466 pernet->next_id = 1; 1467 bitmap_zero(pernet->id_bitmap, MAX_ADDR_ID + 1); 1468 spin_unlock_bh(&pernet->lock); 1469 mptcp_nl_remove_addrs_list(sock_net(skb->sk), &free_list); 1470 synchronize_rcu(); 1471 __flush_addrs(&free_list); 1472 return 0; 1473 } 1474 1475 static int mptcp_nl_fill_addr(struct sk_buff *skb, 1476 struct mptcp_pm_addr_entry *entry) 1477 { 1478 struct mptcp_addr_info *addr = &entry->addr; 1479 struct nlattr *attr; 1480 1481 attr = nla_nest_start(skb, MPTCP_PM_ATTR_ADDR); 1482 if (!attr) 1483 return -EMSGSIZE; 1484 1485 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_FAMILY, addr->family)) 1486 goto nla_put_failure; 1487 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_PORT, ntohs(addr->port))) 1488 goto nla_put_failure; 1489 if (nla_put_u8(skb, MPTCP_PM_ADDR_ATTR_ID, addr->id)) 1490 goto nla_put_failure; 1491 if (nla_put_u32(skb, MPTCP_PM_ADDR_ATTR_FLAGS, entry->flags)) 1492 goto nla_put_failure; 1493 if (entry->ifindex && 1494 nla_put_s32(skb, MPTCP_PM_ADDR_ATTR_IF_IDX, entry->ifindex)) 1495 goto nla_put_failure; 1496 1497 if (addr->family == AF_INET && 1498 nla_put_in_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR4, 1499 addr->addr.s_addr)) 1500 goto nla_put_failure; 1501 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1502 else if (addr->family == AF_INET6 && 1503 nla_put_in6_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR6, &addr->addr6)) 1504 goto nla_put_failure; 1505 #endif 1506 nla_nest_end(skb, attr); 1507 return 0; 1508 1509 nla_put_failure: 1510 nla_nest_cancel(skb, attr); 1511 return -EMSGSIZE; 1512 } 1513 1514 static int mptcp_nl_cmd_get_addr(struct sk_buff *skb, struct genl_info *info) 1515 { 1516 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR]; 1517 struct pm_nl_pernet *pernet = genl_info_pm_nl(info); 1518 struct mptcp_pm_addr_entry addr, *entry; 1519 struct sk_buff *msg; 1520 void *reply; 1521 int ret; 1522 1523 ret = mptcp_pm_parse_addr(attr, info, false, &addr); 1524 if (ret < 0) 1525 return ret; 1526 1527 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1528 if (!msg) 1529 return -ENOMEM; 1530 1531 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0, 1532 info->genlhdr->cmd); 1533 if (!reply) { 1534 GENL_SET_ERR_MSG(info, "not enough space in Netlink message"); 1535 ret = -EMSGSIZE; 1536 goto fail; 1537 } 1538 1539 spin_lock_bh(&pernet->lock); 1540 entry = __lookup_addr_by_id(pernet, addr.addr.id); 1541 if (!entry) { 1542 GENL_SET_ERR_MSG(info, "address not found"); 1543 ret = -EINVAL; 1544 goto unlock_fail; 1545 } 1546 1547 ret = mptcp_nl_fill_addr(msg, entry); 1548 if (ret) 1549 goto unlock_fail; 1550 1551 genlmsg_end(msg, reply); 1552 ret = genlmsg_reply(msg, info); 1553 spin_unlock_bh(&pernet->lock); 1554 return ret; 1555 1556 unlock_fail: 1557 spin_unlock_bh(&pernet->lock); 1558 1559 fail: 1560 nlmsg_free(msg); 1561 return ret; 1562 } 1563 1564 static int mptcp_nl_cmd_dump_addrs(struct sk_buff *msg, 1565 struct netlink_callback *cb) 1566 { 1567 struct net *net = sock_net(msg->sk); 1568 struct mptcp_pm_addr_entry *entry; 1569 struct pm_nl_pernet *pernet; 1570 int id = cb->args[0]; 1571 void *hdr; 1572 int i; 1573 1574 pernet = net_generic(net, pm_nl_pernet_id); 1575 1576 spin_lock_bh(&pernet->lock); 1577 for (i = id; i < MAX_ADDR_ID + 1; i++) { 1578 if (test_bit(i, pernet->id_bitmap)) { 1579 entry = __lookup_addr_by_id(pernet, i); 1580 if (!entry) 1581 break; 1582 1583 if (entry->addr.id <= id) 1584 continue; 1585 1586 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, 1587 cb->nlh->nlmsg_seq, &mptcp_genl_family, 1588 NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR); 1589 if (!hdr) 1590 break; 1591 1592 if (mptcp_nl_fill_addr(msg, entry) < 0) { 1593 genlmsg_cancel(msg, hdr); 1594 break; 1595 } 1596 1597 id = entry->addr.id; 1598 genlmsg_end(msg, hdr); 1599 } 1600 } 1601 spin_unlock_bh(&pernet->lock); 1602 1603 cb->args[0] = id; 1604 return msg->len; 1605 } 1606 1607 static int parse_limit(struct genl_info *info, int id, unsigned int *limit) 1608 { 1609 struct nlattr *attr = info->attrs[id]; 1610 1611 if (!attr) 1612 return 0; 1613 1614 *limit = nla_get_u32(attr); 1615 if (*limit > MPTCP_PM_ADDR_MAX) { 1616 GENL_SET_ERR_MSG(info, "limit greater than maximum"); 1617 return -EINVAL; 1618 } 1619 return 0; 1620 } 1621 1622 static int 1623 mptcp_nl_cmd_set_limits(struct sk_buff *skb, struct genl_info *info) 1624 { 1625 struct pm_nl_pernet *pernet = genl_info_pm_nl(info); 1626 unsigned int rcv_addrs, subflows; 1627 int ret; 1628 1629 spin_lock_bh(&pernet->lock); 1630 rcv_addrs = pernet->add_addr_accept_max; 1631 ret = parse_limit(info, MPTCP_PM_ATTR_RCV_ADD_ADDRS, &rcv_addrs); 1632 if (ret) 1633 goto unlock; 1634 1635 subflows = pernet->subflows_max; 1636 ret = parse_limit(info, MPTCP_PM_ATTR_SUBFLOWS, &subflows); 1637 if (ret) 1638 goto unlock; 1639 1640 WRITE_ONCE(pernet->add_addr_accept_max, rcv_addrs); 1641 WRITE_ONCE(pernet->subflows_max, subflows); 1642 1643 unlock: 1644 spin_unlock_bh(&pernet->lock); 1645 return ret; 1646 } 1647 1648 static int 1649 mptcp_nl_cmd_get_limits(struct sk_buff *skb, struct genl_info *info) 1650 { 1651 struct pm_nl_pernet *pernet = genl_info_pm_nl(info); 1652 struct sk_buff *msg; 1653 void *reply; 1654 1655 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1656 if (!msg) 1657 return -ENOMEM; 1658 1659 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0, 1660 MPTCP_PM_CMD_GET_LIMITS); 1661 if (!reply) 1662 goto fail; 1663 1664 if (nla_put_u32(msg, MPTCP_PM_ATTR_RCV_ADD_ADDRS, 1665 READ_ONCE(pernet->add_addr_accept_max))) 1666 goto fail; 1667 1668 if (nla_put_u32(msg, MPTCP_PM_ATTR_SUBFLOWS, 1669 READ_ONCE(pernet->subflows_max))) 1670 goto fail; 1671 1672 genlmsg_end(msg, reply); 1673 return genlmsg_reply(msg, info); 1674 1675 fail: 1676 GENL_SET_ERR_MSG(info, "not enough space in Netlink message"); 1677 nlmsg_free(msg); 1678 return -EMSGSIZE; 1679 } 1680 1681 static int mptcp_nl_addr_backup(struct net *net, 1682 struct mptcp_addr_info *addr, 1683 u8 bkup) 1684 { 1685 long s_slot = 0, s_num = 0; 1686 struct mptcp_sock *msk; 1687 int ret = -EINVAL; 1688 1689 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { 1690 struct sock *sk = (struct sock *)msk; 1691 1692 if (list_empty(&msk->conn_list)) 1693 goto next; 1694 1695 lock_sock(sk); 1696 spin_lock_bh(&msk->pm.lock); 1697 ret = mptcp_pm_nl_mp_prio_send_ack(msk, addr, bkup); 1698 spin_unlock_bh(&msk->pm.lock); 1699 release_sock(sk); 1700 1701 next: 1702 sock_put(sk); 1703 cond_resched(); 1704 } 1705 1706 return ret; 1707 } 1708 1709 static int mptcp_nl_cmd_set_flags(struct sk_buff *skb, struct genl_info *info) 1710 { 1711 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR]; 1712 struct pm_nl_pernet *pernet = genl_info_pm_nl(info); 1713 struct mptcp_pm_addr_entry addr, *entry; 1714 struct net *net = sock_net(skb->sk); 1715 u8 bkup = 0; 1716 int ret; 1717 1718 ret = mptcp_pm_parse_addr(attr, info, true, &addr); 1719 if (ret < 0) 1720 return ret; 1721 1722 if (addr.flags & MPTCP_PM_ADDR_FLAG_BACKUP) 1723 bkup = 1; 1724 1725 list_for_each_entry(entry, &pernet->local_addr_list, list) { 1726 if (addresses_equal(&entry->addr, &addr.addr, true)) { 1727 ret = mptcp_nl_addr_backup(net, &entry->addr, bkup); 1728 if (ret) 1729 return ret; 1730 1731 if (bkup) 1732 entry->flags |= MPTCP_PM_ADDR_FLAG_BACKUP; 1733 else 1734 entry->flags &= ~MPTCP_PM_ADDR_FLAG_BACKUP; 1735 } 1736 } 1737 1738 return 0; 1739 } 1740 1741 static void mptcp_nl_mcast_send(struct net *net, struct sk_buff *nlskb, gfp_t gfp) 1742 { 1743 genlmsg_multicast_netns(&mptcp_genl_family, net, 1744 nlskb, 0, MPTCP_PM_EV_GRP_OFFSET, gfp); 1745 } 1746 1747 static int mptcp_event_add_subflow(struct sk_buff *skb, const struct sock *ssk) 1748 { 1749 const struct inet_sock *issk = inet_sk(ssk); 1750 const struct mptcp_subflow_context *sf; 1751 1752 if (nla_put_u16(skb, MPTCP_ATTR_FAMILY, ssk->sk_family)) 1753 return -EMSGSIZE; 1754 1755 switch (ssk->sk_family) { 1756 case AF_INET: 1757 if (nla_put_in_addr(skb, MPTCP_ATTR_SADDR4, issk->inet_saddr)) 1758 return -EMSGSIZE; 1759 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, issk->inet_daddr)) 1760 return -EMSGSIZE; 1761 break; 1762 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1763 case AF_INET6: { 1764 const struct ipv6_pinfo *np = inet6_sk(ssk); 1765 1766 if (nla_put_in6_addr(skb, MPTCP_ATTR_SADDR6, &np->saddr)) 1767 return -EMSGSIZE; 1768 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &ssk->sk_v6_daddr)) 1769 return -EMSGSIZE; 1770 break; 1771 } 1772 #endif 1773 default: 1774 WARN_ON_ONCE(1); 1775 return -EMSGSIZE; 1776 } 1777 1778 if (nla_put_be16(skb, MPTCP_ATTR_SPORT, issk->inet_sport)) 1779 return -EMSGSIZE; 1780 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, issk->inet_dport)) 1781 return -EMSGSIZE; 1782 1783 sf = mptcp_subflow_ctx(ssk); 1784 if (WARN_ON_ONCE(!sf)) 1785 return -EINVAL; 1786 1787 if (nla_put_u8(skb, MPTCP_ATTR_LOC_ID, sf->local_id)) 1788 return -EMSGSIZE; 1789 1790 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, sf->remote_id)) 1791 return -EMSGSIZE; 1792 1793 return 0; 1794 } 1795 1796 static int mptcp_event_put_token_and_ssk(struct sk_buff *skb, 1797 const struct mptcp_sock *msk, 1798 const struct sock *ssk) 1799 { 1800 const struct sock *sk = (const struct sock *)msk; 1801 const struct mptcp_subflow_context *sf; 1802 u8 sk_err; 1803 1804 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token)) 1805 return -EMSGSIZE; 1806 1807 if (mptcp_event_add_subflow(skb, ssk)) 1808 return -EMSGSIZE; 1809 1810 sf = mptcp_subflow_ctx(ssk); 1811 if (WARN_ON_ONCE(!sf)) 1812 return -EINVAL; 1813 1814 if (nla_put_u8(skb, MPTCP_ATTR_BACKUP, sf->backup)) 1815 return -EMSGSIZE; 1816 1817 if (ssk->sk_bound_dev_if && 1818 nla_put_s32(skb, MPTCP_ATTR_IF_IDX, ssk->sk_bound_dev_if)) 1819 return -EMSGSIZE; 1820 1821 sk_err = ssk->sk_err; 1822 if (sk_err && sk->sk_state == TCP_ESTABLISHED && 1823 nla_put_u8(skb, MPTCP_ATTR_ERROR, sk_err)) 1824 return -EMSGSIZE; 1825 1826 return 0; 1827 } 1828 1829 static int mptcp_event_sub_established(struct sk_buff *skb, 1830 const struct mptcp_sock *msk, 1831 const struct sock *ssk) 1832 { 1833 return mptcp_event_put_token_and_ssk(skb, msk, ssk); 1834 } 1835 1836 static int mptcp_event_sub_closed(struct sk_buff *skb, 1837 const struct mptcp_sock *msk, 1838 const struct sock *ssk) 1839 { 1840 const struct mptcp_subflow_context *sf; 1841 1842 if (mptcp_event_put_token_and_ssk(skb, msk, ssk)) 1843 return -EMSGSIZE; 1844 1845 sf = mptcp_subflow_ctx(ssk); 1846 if (!sf->reset_seen) 1847 return 0; 1848 1849 if (nla_put_u32(skb, MPTCP_ATTR_RESET_REASON, sf->reset_reason)) 1850 return -EMSGSIZE; 1851 1852 if (nla_put_u32(skb, MPTCP_ATTR_RESET_FLAGS, sf->reset_transient)) 1853 return -EMSGSIZE; 1854 1855 return 0; 1856 } 1857 1858 static int mptcp_event_created(struct sk_buff *skb, 1859 const struct mptcp_sock *msk, 1860 const struct sock *ssk) 1861 { 1862 int err = nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token); 1863 1864 if (err) 1865 return err; 1866 1867 return mptcp_event_add_subflow(skb, ssk); 1868 } 1869 1870 void mptcp_event_addr_removed(const struct mptcp_sock *msk, uint8_t id) 1871 { 1872 struct net *net = sock_net((const struct sock *)msk); 1873 struct nlmsghdr *nlh; 1874 struct sk_buff *skb; 1875 1876 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET)) 1877 return; 1878 1879 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); 1880 if (!skb) 1881 return; 1882 1883 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, MPTCP_EVENT_REMOVED); 1884 if (!nlh) 1885 goto nla_put_failure; 1886 1887 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token)) 1888 goto nla_put_failure; 1889 1890 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, id)) 1891 goto nla_put_failure; 1892 1893 genlmsg_end(skb, nlh); 1894 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC); 1895 return; 1896 1897 nla_put_failure: 1898 kfree_skb(skb); 1899 } 1900 1901 void mptcp_event_addr_announced(const struct mptcp_sock *msk, 1902 const struct mptcp_addr_info *info) 1903 { 1904 struct net *net = sock_net((const struct sock *)msk); 1905 struct nlmsghdr *nlh; 1906 struct sk_buff *skb; 1907 1908 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET)) 1909 return; 1910 1911 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); 1912 if (!skb) 1913 return; 1914 1915 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, 1916 MPTCP_EVENT_ANNOUNCED); 1917 if (!nlh) 1918 goto nla_put_failure; 1919 1920 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token)) 1921 goto nla_put_failure; 1922 1923 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, info->id)) 1924 goto nla_put_failure; 1925 1926 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, info->port)) 1927 goto nla_put_failure; 1928 1929 switch (info->family) { 1930 case AF_INET: 1931 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, info->addr.s_addr)) 1932 goto nla_put_failure; 1933 break; 1934 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1935 case AF_INET6: 1936 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &info->addr6)) 1937 goto nla_put_failure; 1938 break; 1939 #endif 1940 default: 1941 WARN_ON_ONCE(1); 1942 goto nla_put_failure; 1943 } 1944 1945 genlmsg_end(skb, nlh); 1946 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC); 1947 return; 1948 1949 nla_put_failure: 1950 kfree_skb(skb); 1951 } 1952 1953 void mptcp_event(enum mptcp_event_type type, const struct mptcp_sock *msk, 1954 const struct sock *ssk, gfp_t gfp) 1955 { 1956 struct net *net = sock_net((const struct sock *)msk); 1957 struct nlmsghdr *nlh; 1958 struct sk_buff *skb; 1959 1960 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET)) 1961 return; 1962 1963 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp); 1964 if (!skb) 1965 return; 1966 1967 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, type); 1968 if (!nlh) 1969 goto nla_put_failure; 1970 1971 switch (type) { 1972 case MPTCP_EVENT_UNSPEC: 1973 WARN_ON_ONCE(1); 1974 break; 1975 case MPTCP_EVENT_CREATED: 1976 case MPTCP_EVENT_ESTABLISHED: 1977 if (mptcp_event_created(skb, msk, ssk) < 0) 1978 goto nla_put_failure; 1979 break; 1980 case MPTCP_EVENT_CLOSED: 1981 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token) < 0) 1982 goto nla_put_failure; 1983 break; 1984 case MPTCP_EVENT_ANNOUNCED: 1985 case MPTCP_EVENT_REMOVED: 1986 /* call mptcp_event_addr_announced()/removed instead */ 1987 WARN_ON_ONCE(1); 1988 break; 1989 case MPTCP_EVENT_SUB_ESTABLISHED: 1990 case MPTCP_EVENT_SUB_PRIORITY: 1991 if (mptcp_event_sub_established(skb, msk, ssk) < 0) 1992 goto nla_put_failure; 1993 break; 1994 case MPTCP_EVENT_SUB_CLOSED: 1995 if (mptcp_event_sub_closed(skb, msk, ssk) < 0) 1996 goto nla_put_failure; 1997 break; 1998 } 1999 2000 genlmsg_end(skb, nlh); 2001 mptcp_nl_mcast_send(net, skb, gfp); 2002 return; 2003 2004 nla_put_failure: 2005 kfree_skb(skb); 2006 } 2007 2008 static const struct genl_small_ops mptcp_pm_ops[] = { 2009 { 2010 .cmd = MPTCP_PM_CMD_ADD_ADDR, 2011 .doit = mptcp_nl_cmd_add_addr, 2012 .flags = GENL_ADMIN_PERM, 2013 }, 2014 { 2015 .cmd = MPTCP_PM_CMD_DEL_ADDR, 2016 .doit = mptcp_nl_cmd_del_addr, 2017 .flags = GENL_ADMIN_PERM, 2018 }, 2019 { 2020 .cmd = MPTCP_PM_CMD_FLUSH_ADDRS, 2021 .doit = mptcp_nl_cmd_flush_addrs, 2022 .flags = GENL_ADMIN_PERM, 2023 }, 2024 { 2025 .cmd = MPTCP_PM_CMD_GET_ADDR, 2026 .doit = mptcp_nl_cmd_get_addr, 2027 .dumpit = mptcp_nl_cmd_dump_addrs, 2028 }, 2029 { 2030 .cmd = MPTCP_PM_CMD_SET_LIMITS, 2031 .doit = mptcp_nl_cmd_set_limits, 2032 .flags = GENL_ADMIN_PERM, 2033 }, 2034 { 2035 .cmd = MPTCP_PM_CMD_GET_LIMITS, 2036 .doit = mptcp_nl_cmd_get_limits, 2037 }, 2038 { 2039 .cmd = MPTCP_PM_CMD_SET_FLAGS, 2040 .doit = mptcp_nl_cmd_set_flags, 2041 .flags = GENL_ADMIN_PERM, 2042 }, 2043 }; 2044 2045 static struct genl_family mptcp_genl_family __ro_after_init = { 2046 .name = MPTCP_PM_NAME, 2047 .version = MPTCP_PM_VER, 2048 .maxattr = MPTCP_PM_ATTR_MAX, 2049 .policy = mptcp_pm_policy, 2050 .netnsok = true, 2051 .module = THIS_MODULE, 2052 .small_ops = mptcp_pm_ops, 2053 .n_small_ops = ARRAY_SIZE(mptcp_pm_ops), 2054 .mcgrps = mptcp_pm_mcgrps, 2055 .n_mcgrps = ARRAY_SIZE(mptcp_pm_mcgrps), 2056 }; 2057 2058 static int __net_init pm_nl_init_net(struct net *net) 2059 { 2060 struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id); 2061 2062 INIT_LIST_HEAD_RCU(&pernet->local_addr_list); 2063 pernet->next_id = 1; 2064 pernet->stale_loss_cnt = 4; 2065 spin_lock_init(&pernet->lock); 2066 2067 /* No need to initialize other pernet fields, the struct is zeroed at 2068 * allocation time. 2069 */ 2070 2071 return 0; 2072 } 2073 2074 static void __net_exit pm_nl_exit_net(struct list_head *net_list) 2075 { 2076 struct net *net; 2077 2078 list_for_each_entry(net, net_list, exit_list) { 2079 struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id); 2080 2081 /* net is removed from namespace list, can't race with 2082 * other modifiers, also netns core already waited for a 2083 * RCU grace period. 2084 */ 2085 __flush_addrs(&pernet->local_addr_list); 2086 } 2087 } 2088 2089 static struct pernet_operations mptcp_pm_pernet_ops = { 2090 .init = pm_nl_init_net, 2091 .exit_batch = pm_nl_exit_net, 2092 .id = &pm_nl_pernet_id, 2093 .size = sizeof(struct pm_nl_pernet), 2094 }; 2095 2096 void __init mptcp_pm_nl_init(void) 2097 { 2098 if (register_pernet_subsys(&mptcp_pm_pernet_ops) < 0) 2099 panic("Failed to register MPTCP PM pernet subsystem.\n"); 2100 2101 if (genl_register_family(&mptcp_genl_family)) 2102 panic("Failed to register MPTCP PM netlink family\n"); 2103 } 2104