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