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