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->backup = backup; 480 subflow->request_bkup = backup; 481 } 482 483 __mptcp_subflow_send_ack(ssk); 484 unlock_sock_fast(ssk, slow); 485 } 486 487 static void mptcp_pm_send_ack(struct mptcp_sock *msk, struct mptcp_subflow_context *subflow, 488 bool prio, bool backup) 489 { 490 spin_unlock_bh(&msk->pm.lock); 491 __mptcp_pm_send_ack(msk, subflow, prio, backup); 492 spin_lock_bh(&msk->pm.lock); 493 } 494 495 static struct mptcp_pm_addr_entry * 496 __lookup_addr_by_id(struct pm_nl_pernet *pernet, unsigned int id) 497 { 498 struct mptcp_pm_addr_entry *entry; 499 500 list_for_each_entry(entry, &pernet->local_addr_list, list) { 501 if (entry->addr.id == id) 502 return entry; 503 } 504 return NULL; 505 } 506 507 static struct mptcp_pm_addr_entry * 508 __lookup_addr(struct pm_nl_pernet *pernet, const struct mptcp_addr_info *info, 509 bool lookup_by_id) 510 { 511 struct mptcp_pm_addr_entry *entry; 512 513 list_for_each_entry(entry, &pernet->local_addr_list, list) { 514 if ((!lookup_by_id && 515 mptcp_addresses_equal(&entry->addr, info, entry->addr.port)) || 516 (lookup_by_id && entry->addr.id == info->id)) 517 return entry; 518 } 519 return NULL; 520 } 521 522 static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk) 523 { 524 struct sock *sk = (struct sock *)msk; 525 struct mptcp_pm_addr_entry *local; 526 unsigned int add_addr_signal_max; 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, false); 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 local = select_signal_address(pernet, msk); 568 569 /* due to racing events on both ends we can reach here while 570 * previous add address is still running: if we invoke now 571 * mptcp_pm_announce_addr(), that will fail and the 572 * corresponding id will be marked as used. 573 * Instead let the PM machinery reschedule us when the 574 * current address announce will be completed. 575 */ 576 if (msk->pm.addr_signal & BIT(MPTCP_ADD_ADDR_SIGNAL)) 577 return; 578 579 if (local) { 580 if (mptcp_pm_alloc_anno_list(msk, &local->addr)) { 581 __clear_bit(local->addr.id, msk->pm.id_avail_bitmap); 582 msk->pm.add_addr_signaled++; 583 mptcp_pm_announce_addr(msk, &local->addr, false); 584 mptcp_pm_nl_addr_send_ack(msk); 585 } 586 } 587 } 588 589 /* check if should create a new subflow */ 590 while (msk->pm.local_addr_used < local_addr_max && 591 msk->pm.subflows < subflows_max) { 592 struct mptcp_addr_info addrs[MPTCP_PM_ADDR_MAX]; 593 bool fullmesh; 594 int i, nr; 595 596 local = select_local_address(pernet, msk); 597 if (!local) 598 break; 599 600 fullmesh = !!(local->flags & MPTCP_PM_ADDR_FLAG_FULLMESH); 601 602 msk->pm.local_addr_used++; 603 __clear_bit(local->addr.id, msk->pm.id_avail_bitmap); 604 nr = fill_remote_addresses_vec(msk, &local->addr, fullmesh, addrs); 605 if (nr == 0) 606 continue; 607 608 spin_unlock_bh(&msk->pm.lock); 609 for (i = 0; i < nr; i++) 610 __mptcp_subflow_connect(sk, &local->addr, &addrs[i]); 611 spin_lock_bh(&msk->pm.lock); 612 } 613 mptcp_pm_nl_check_work_pending(msk); 614 } 615 616 static void mptcp_pm_nl_fully_established(struct mptcp_sock *msk) 617 { 618 mptcp_pm_create_subflow_or_signal_addr(msk); 619 } 620 621 static void mptcp_pm_nl_subflow_established(struct mptcp_sock *msk) 622 { 623 mptcp_pm_create_subflow_or_signal_addr(msk); 624 } 625 626 /* Fill all the local addresses into the array addrs[], 627 * and return the array size. 628 */ 629 static unsigned int fill_local_addresses_vec(struct mptcp_sock *msk, 630 struct mptcp_addr_info *remote, 631 struct mptcp_addr_info *addrs) 632 { 633 struct sock *sk = (struct sock *)msk; 634 struct mptcp_pm_addr_entry *entry; 635 struct pm_nl_pernet *pernet; 636 unsigned int subflows_max; 637 int i = 0; 638 639 pernet = pm_nl_get_pernet_from_msk(msk); 640 subflows_max = mptcp_pm_get_subflows_max(msk); 641 642 rcu_read_lock(); 643 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) { 644 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_FULLMESH)) 645 continue; 646 647 if (!mptcp_pm_addr_families_match(sk, &entry->addr, remote)) 648 continue; 649 650 if (msk->pm.subflows < subflows_max) { 651 msk->pm.subflows++; 652 addrs[i++] = entry->addr; 653 } 654 } 655 rcu_read_unlock(); 656 657 /* If the array is empty, fill in the single 658 * 'IPADDRANY' local address 659 */ 660 if (!i) { 661 struct mptcp_addr_info local; 662 663 memset(&local, 0, sizeof(local)); 664 local.family = 665 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 666 remote->family == AF_INET6 && 667 ipv6_addr_v4mapped(&remote->addr6) ? AF_INET : 668 #endif 669 remote->family; 670 671 if (!mptcp_pm_addr_families_match(sk, &local, remote)) 672 return 0; 673 674 msk->pm.subflows++; 675 addrs[i++] = local; 676 } 677 678 return i; 679 } 680 681 static void mptcp_pm_nl_add_addr_received(struct mptcp_sock *msk) 682 { 683 struct mptcp_addr_info addrs[MPTCP_PM_ADDR_MAX]; 684 struct sock *sk = (struct sock *)msk; 685 unsigned int add_addr_accept_max; 686 struct mptcp_addr_info remote; 687 unsigned int subflows_max; 688 bool sf_created = false; 689 int i, nr; 690 691 add_addr_accept_max = mptcp_pm_get_add_addr_accept_max(msk); 692 subflows_max = mptcp_pm_get_subflows_max(msk); 693 694 pr_debug("accepted %d:%d remote family %d", 695 msk->pm.add_addr_accepted, add_addr_accept_max, 696 msk->pm.remote.family); 697 698 remote = msk->pm.remote; 699 mptcp_pm_announce_addr(msk, &remote, true); 700 mptcp_pm_nl_addr_send_ack(msk); 701 702 if (lookup_subflow_by_daddr(&msk->conn_list, &remote)) 703 return; 704 705 /* pick id 0 port, if none is provided the remote address */ 706 if (!remote.port) 707 remote.port = sk->sk_dport; 708 709 /* connect to the specified remote address, using whatever 710 * local address the routing configuration will pick. 711 */ 712 nr = fill_local_addresses_vec(msk, &remote, addrs); 713 if (nr == 0) 714 return; 715 716 spin_unlock_bh(&msk->pm.lock); 717 for (i = 0; i < nr; i++) 718 if (__mptcp_subflow_connect(sk, &addrs[i], &remote) == 0) 719 sf_created = true; 720 spin_lock_bh(&msk->pm.lock); 721 722 if (sf_created) { 723 msk->pm.add_addr_accepted++; 724 if (msk->pm.add_addr_accepted >= add_addr_accept_max || 725 msk->pm.subflows >= subflows_max) 726 WRITE_ONCE(msk->pm.accept_addr, false); 727 } 728 } 729 730 void mptcp_pm_nl_addr_send_ack(struct mptcp_sock *msk) 731 { 732 struct mptcp_subflow_context *subflow; 733 734 msk_owned_by_me(msk); 735 lockdep_assert_held(&msk->pm.lock); 736 737 if (!mptcp_pm_should_add_signal(msk) && 738 !mptcp_pm_should_rm_signal(msk)) 739 return; 740 741 subflow = list_first_entry_or_null(&msk->conn_list, typeof(*subflow), node); 742 if (subflow) 743 mptcp_pm_send_ack(msk, subflow, false, false); 744 } 745 746 int mptcp_pm_nl_mp_prio_send_ack(struct mptcp_sock *msk, 747 struct mptcp_addr_info *addr, 748 struct mptcp_addr_info *rem, 749 u8 bkup) 750 { 751 struct mptcp_subflow_context *subflow; 752 753 pr_debug("bkup=%d", bkup); 754 755 mptcp_for_each_subflow(msk, subflow) { 756 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 757 struct mptcp_addr_info local, remote; 758 759 mptcp_local_address((struct sock_common *)ssk, &local); 760 if (!mptcp_addresses_equal(&local, addr, addr->port)) 761 continue; 762 763 if (rem && rem->family != AF_UNSPEC) { 764 remote_address((struct sock_common *)ssk, &remote); 765 if (!mptcp_addresses_equal(&remote, rem, rem->port)) 766 continue; 767 } 768 769 __mptcp_pm_send_ack(msk, subflow, true, bkup); 770 return 0; 771 } 772 773 return -EINVAL; 774 } 775 776 static bool mptcp_local_id_match(const struct mptcp_sock *msk, u8 local_id, u8 id) 777 { 778 return local_id == id || (!local_id && msk->mpc_endpoint_id == id); 779 } 780 781 static void mptcp_pm_nl_rm_addr_or_subflow(struct mptcp_sock *msk, 782 const struct mptcp_rm_list *rm_list, 783 enum linux_mptcp_mib_field rm_type) 784 { 785 struct mptcp_subflow_context *subflow, *tmp; 786 struct sock *sk = (struct sock *)msk; 787 u8 i; 788 789 pr_debug("%s rm_list_nr %d", 790 rm_type == MPTCP_MIB_RMADDR ? "address" : "subflow", rm_list->nr); 791 792 msk_owned_by_me(msk); 793 794 if (sk->sk_state == TCP_LISTEN) 795 return; 796 797 if (!rm_list->nr) 798 return; 799 800 if (list_empty(&msk->conn_list)) 801 return; 802 803 for (i = 0; i < rm_list->nr; i++) { 804 u8 rm_id = rm_list->ids[i]; 805 bool removed = false; 806 807 mptcp_for_each_subflow_safe(msk, subflow, tmp) { 808 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 809 u8 remote_id = READ_ONCE(subflow->remote_id); 810 int how = RCV_SHUTDOWN | SEND_SHUTDOWN; 811 u8 id = subflow_get_local_id(subflow); 812 813 if (rm_type == MPTCP_MIB_RMADDR && remote_id != rm_id) 814 continue; 815 if (rm_type == MPTCP_MIB_RMSUBFLOW && !mptcp_local_id_match(msk, id, rm_id)) 816 continue; 817 818 pr_debug(" -> %s rm_list_ids[%d]=%u local_id=%u remote_id=%u mpc_id=%u", 819 rm_type == MPTCP_MIB_RMADDR ? "address" : "subflow", 820 i, rm_id, id, remote_id, msk->mpc_endpoint_id); 821 spin_unlock_bh(&msk->pm.lock); 822 mptcp_subflow_shutdown(sk, ssk, how); 823 824 /* the following takes care of updating the subflows counter */ 825 mptcp_close_ssk(sk, ssk, subflow); 826 spin_lock_bh(&msk->pm.lock); 827 828 removed = true; 829 if (rm_type == MPTCP_MIB_RMSUBFLOW) 830 __MPTCP_INC_STATS(sock_net(sk), rm_type); 831 } 832 if (rm_type == MPTCP_MIB_RMSUBFLOW) 833 __set_bit(rm_id ? rm_id : msk->mpc_endpoint_id, msk->pm.id_avail_bitmap); 834 else if (rm_type == MPTCP_MIB_RMADDR) 835 __MPTCP_INC_STATS(sock_net(sk), rm_type); 836 if (!removed) 837 continue; 838 839 if (!mptcp_pm_is_kernel(msk)) 840 continue; 841 842 if (rm_type == MPTCP_MIB_RMADDR) { 843 msk->pm.add_addr_accepted--; 844 WRITE_ONCE(msk->pm.accept_addr, true); 845 } else if (rm_type == MPTCP_MIB_RMSUBFLOW) { 846 msk->pm.local_addr_used--; 847 } 848 } 849 } 850 851 static void mptcp_pm_nl_rm_addr_received(struct mptcp_sock *msk) 852 { 853 mptcp_pm_nl_rm_addr_or_subflow(msk, &msk->pm.rm_list_rx, MPTCP_MIB_RMADDR); 854 } 855 856 void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk, 857 const struct mptcp_rm_list *rm_list) 858 { 859 mptcp_pm_nl_rm_addr_or_subflow(msk, rm_list, MPTCP_MIB_RMSUBFLOW); 860 } 861 862 void mptcp_pm_nl_work(struct mptcp_sock *msk) 863 { 864 struct mptcp_pm_data *pm = &msk->pm; 865 866 msk_owned_by_me(msk); 867 868 if (!(pm->status & MPTCP_PM_WORK_MASK)) 869 return; 870 871 spin_lock_bh(&msk->pm.lock); 872 873 pr_debug("msk=%p status=%x", msk, pm->status); 874 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_RECEIVED)) { 875 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_RECEIVED); 876 mptcp_pm_nl_add_addr_received(msk); 877 } 878 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_SEND_ACK)) { 879 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_SEND_ACK); 880 mptcp_pm_nl_addr_send_ack(msk); 881 } 882 if (pm->status & BIT(MPTCP_PM_RM_ADDR_RECEIVED)) { 883 pm->status &= ~BIT(MPTCP_PM_RM_ADDR_RECEIVED); 884 mptcp_pm_nl_rm_addr_received(msk); 885 } 886 if (pm->status & BIT(MPTCP_PM_ESTABLISHED)) { 887 pm->status &= ~BIT(MPTCP_PM_ESTABLISHED); 888 mptcp_pm_nl_fully_established(msk); 889 } 890 if (pm->status & BIT(MPTCP_PM_SUBFLOW_ESTABLISHED)) { 891 pm->status &= ~BIT(MPTCP_PM_SUBFLOW_ESTABLISHED); 892 mptcp_pm_nl_subflow_established(msk); 893 } 894 895 spin_unlock_bh(&msk->pm.lock); 896 } 897 898 static bool address_use_port(struct mptcp_pm_addr_entry *entry) 899 { 900 return (entry->flags & 901 (MPTCP_PM_ADDR_FLAG_SIGNAL | MPTCP_PM_ADDR_FLAG_SUBFLOW)) == 902 MPTCP_PM_ADDR_FLAG_SIGNAL; 903 } 904 905 /* caller must ensure the RCU grace period is already elapsed */ 906 static void __mptcp_pm_release_addr_entry(struct mptcp_pm_addr_entry *entry) 907 { 908 if (entry->lsk) 909 sock_release(entry->lsk); 910 kfree(entry); 911 } 912 913 static int mptcp_pm_nl_append_new_local_addr(struct pm_nl_pernet *pernet, 914 struct mptcp_pm_addr_entry *entry, 915 bool needs_id) 916 { 917 struct mptcp_pm_addr_entry *cur, *del_entry = NULL; 918 unsigned int addr_max; 919 int ret = -EINVAL; 920 921 spin_lock_bh(&pernet->lock); 922 /* to keep the code simple, don't do IDR-like allocation for address ID, 923 * just bail when we exceed limits 924 */ 925 if (pernet->next_id == MPTCP_PM_MAX_ADDR_ID) 926 pernet->next_id = 1; 927 if (pernet->addrs >= MPTCP_PM_ADDR_MAX) { 928 ret = -ERANGE; 929 goto out; 930 } 931 if (test_bit(entry->addr.id, pernet->id_bitmap)) { 932 ret = -EBUSY; 933 goto out; 934 } 935 936 /* do not insert duplicate address, differentiate on port only 937 * singled addresses 938 */ 939 if (!address_use_port(entry)) 940 entry->addr.port = 0; 941 list_for_each_entry(cur, &pernet->local_addr_list, list) { 942 if (mptcp_addresses_equal(&cur->addr, &entry->addr, 943 cur->addr.port || entry->addr.port)) { 944 /* allow replacing the exiting endpoint only if such 945 * endpoint is an implicit one and the user-space 946 * did not provide an endpoint id 947 */ 948 if (!(cur->flags & MPTCP_PM_ADDR_FLAG_IMPLICIT)) { 949 ret = -EEXIST; 950 goto out; 951 } 952 if (entry->addr.id) 953 goto out; 954 955 pernet->addrs--; 956 entry->addr.id = cur->addr.id; 957 list_del_rcu(&cur->list); 958 del_entry = cur; 959 break; 960 } 961 } 962 963 if (!entry->addr.id && needs_id) { 964 find_next: 965 entry->addr.id = find_next_zero_bit(pernet->id_bitmap, 966 MPTCP_PM_MAX_ADDR_ID + 1, 967 pernet->next_id); 968 if (!entry->addr.id && pernet->next_id != 1) { 969 pernet->next_id = 1; 970 goto find_next; 971 } 972 } 973 974 if (!entry->addr.id && needs_id) 975 goto out; 976 977 __set_bit(entry->addr.id, pernet->id_bitmap); 978 if (entry->addr.id > pernet->next_id) 979 pernet->next_id = entry->addr.id; 980 981 if (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) { 982 addr_max = pernet->add_addr_signal_max; 983 WRITE_ONCE(pernet->add_addr_signal_max, addr_max + 1); 984 } 985 if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) { 986 addr_max = pernet->local_addr_max; 987 WRITE_ONCE(pernet->local_addr_max, addr_max + 1); 988 } 989 990 pernet->addrs++; 991 if (!entry->addr.port) 992 list_add_tail_rcu(&entry->list, &pernet->local_addr_list); 993 else 994 list_add_rcu(&entry->list, &pernet->local_addr_list); 995 ret = entry->addr.id; 996 997 out: 998 spin_unlock_bh(&pernet->lock); 999 1000 /* just replaced an existing entry, free it */ 1001 if (del_entry) { 1002 synchronize_rcu(); 1003 __mptcp_pm_release_addr_entry(del_entry); 1004 } 1005 return ret; 1006 } 1007 1008 static struct lock_class_key mptcp_slock_keys[2]; 1009 static struct lock_class_key mptcp_keys[2]; 1010 1011 static int mptcp_pm_nl_create_listen_socket(struct sock *sk, 1012 struct mptcp_pm_addr_entry *entry) 1013 { 1014 bool is_ipv6 = sk->sk_family == AF_INET6; 1015 int addrlen = sizeof(struct sockaddr_in); 1016 struct sockaddr_storage addr; 1017 struct sock *newsk, *ssk; 1018 int backlog = 1024; 1019 int err; 1020 1021 err = sock_create_kern(sock_net(sk), entry->addr.family, 1022 SOCK_STREAM, IPPROTO_MPTCP, &entry->lsk); 1023 if (err) 1024 return err; 1025 1026 newsk = entry->lsk->sk; 1027 if (!newsk) 1028 return -EINVAL; 1029 1030 /* The subflow socket lock is acquired in a nested to the msk one 1031 * in several places, even by the TCP stack, and this msk is a kernel 1032 * socket: lockdep complains. Instead of propagating the _nested 1033 * modifiers in several places, re-init the lock class for the msk 1034 * socket to an mptcp specific one. 1035 */ 1036 sock_lock_init_class_and_name(newsk, 1037 is_ipv6 ? "mlock-AF_INET6" : "mlock-AF_INET", 1038 &mptcp_slock_keys[is_ipv6], 1039 is_ipv6 ? "msk_lock-AF_INET6" : "msk_lock-AF_INET", 1040 &mptcp_keys[is_ipv6]); 1041 1042 lock_sock(newsk); 1043 ssk = __mptcp_nmpc_sk(mptcp_sk(newsk)); 1044 release_sock(newsk); 1045 if (IS_ERR(ssk)) 1046 return PTR_ERR(ssk); 1047 1048 mptcp_info2sockaddr(&entry->addr, &addr, entry->addr.family); 1049 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1050 if (entry->addr.family == AF_INET6) 1051 addrlen = sizeof(struct sockaddr_in6); 1052 #endif 1053 if (ssk->sk_family == AF_INET) 1054 err = inet_bind_sk(ssk, (struct sockaddr *)&addr, addrlen); 1055 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1056 else if (ssk->sk_family == AF_INET6) 1057 err = inet6_bind_sk(ssk, (struct sockaddr *)&addr, addrlen); 1058 #endif 1059 if (err) 1060 return err; 1061 1062 /* We don't use mptcp_set_state() here because it needs to be called 1063 * under the msk socket lock. For the moment, that will not bring 1064 * anything more than only calling inet_sk_state_store(), because the 1065 * old status is known (TCP_CLOSE). 1066 */ 1067 inet_sk_state_store(newsk, TCP_LISTEN); 1068 lock_sock(ssk); 1069 err = __inet_listen_sk(ssk, backlog); 1070 if (!err) 1071 mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CREATED); 1072 release_sock(ssk); 1073 return err; 1074 } 1075 1076 int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct mptcp_addr_info *skc) 1077 { 1078 struct mptcp_pm_addr_entry *entry; 1079 struct pm_nl_pernet *pernet; 1080 int ret = -1; 1081 1082 pernet = pm_nl_get_pernet_from_msk(msk); 1083 1084 rcu_read_lock(); 1085 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) { 1086 if (mptcp_addresses_equal(&entry->addr, skc, entry->addr.port)) { 1087 ret = entry->addr.id; 1088 break; 1089 } 1090 } 1091 rcu_read_unlock(); 1092 if (ret >= 0) 1093 return ret; 1094 1095 /* address not found, add to local list */ 1096 entry = kmalloc(sizeof(*entry), GFP_ATOMIC); 1097 if (!entry) 1098 return -ENOMEM; 1099 1100 entry->addr = *skc; 1101 entry->addr.id = 0; 1102 entry->addr.port = 0; 1103 entry->ifindex = 0; 1104 entry->flags = MPTCP_PM_ADDR_FLAG_IMPLICIT; 1105 entry->lsk = NULL; 1106 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry, true); 1107 if (ret < 0) 1108 kfree(entry); 1109 1110 return ret; 1111 } 1112 1113 #define MPTCP_PM_CMD_GRP_OFFSET 0 1114 #define MPTCP_PM_EV_GRP_OFFSET 1 1115 1116 static const struct genl_multicast_group mptcp_pm_mcgrps[] = { 1117 [MPTCP_PM_CMD_GRP_OFFSET] = { .name = MPTCP_PM_CMD_GRP_NAME, }, 1118 [MPTCP_PM_EV_GRP_OFFSET] = { .name = MPTCP_PM_EV_GRP_NAME, 1119 .flags = GENL_UNS_ADMIN_PERM, 1120 }, 1121 }; 1122 1123 static const struct nla_policy 1124 mptcp_pm_addr_policy[MPTCP_PM_ADDR_ATTR_MAX + 1] = { 1125 [MPTCP_PM_ADDR_ATTR_FAMILY] = { .type = NLA_U16, }, 1126 [MPTCP_PM_ADDR_ATTR_ID] = { .type = NLA_U8, }, 1127 [MPTCP_PM_ADDR_ATTR_ADDR4] = { .type = NLA_U32, }, 1128 [MPTCP_PM_ADDR_ATTR_ADDR6] = 1129 NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)), 1130 [MPTCP_PM_ADDR_ATTR_PORT] = { .type = NLA_U16 }, 1131 [MPTCP_PM_ADDR_ATTR_FLAGS] = { .type = NLA_U32 }, 1132 [MPTCP_PM_ADDR_ATTR_IF_IDX] = { .type = NLA_S32 }, 1133 }; 1134 1135 static const struct nla_policy mptcp_pm_policy[MPTCP_PM_ATTR_MAX + 1] = { 1136 [MPTCP_PM_ATTR_ADDR] = 1137 NLA_POLICY_NESTED(mptcp_pm_addr_policy), 1138 [MPTCP_PM_ATTR_RCV_ADD_ADDRS] = { .type = NLA_U32, }, 1139 [MPTCP_PM_ATTR_SUBFLOWS] = { .type = NLA_U32, }, 1140 [MPTCP_PM_ATTR_TOKEN] = { .type = NLA_U32, }, 1141 [MPTCP_PM_ATTR_LOC_ID] = { .type = NLA_U8, }, 1142 [MPTCP_PM_ATTR_ADDR_REMOTE] = 1143 NLA_POLICY_NESTED(mptcp_pm_addr_policy), 1144 }; 1145 1146 void mptcp_pm_nl_subflow_chk_stale(const struct mptcp_sock *msk, struct sock *ssk) 1147 { 1148 struct mptcp_subflow_context *iter, *subflow = mptcp_subflow_ctx(ssk); 1149 struct sock *sk = (struct sock *)msk; 1150 unsigned int active_max_loss_cnt; 1151 struct net *net = sock_net(sk); 1152 unsigned int stale_loss_cnt; 1153 bool slow; 1154 1155 stale_loss_cnt = mptcp_stale_loss_cnt(net); 1156 if (subflow->stale || !stale_loss_cnt || subflow->stale_count <= stale_loss_cnt) 1157 return; 1158 1159 /* look for another available subflow not in loss state */ 1160 active_max_loss_cnt = max_t(int, stale_loss_cnt - 1, 1); 1161 mptcp_for_each_subflow(msk, iter) { 1162 if (iter != subflow && mptcp_subflow_active(iter) && 1163 iter->stale_count < active_max_loss_cnt) { 1164 /* we have some alternatives, try to mark this subflow as idle ...*/ 1165 slow = lock_sock_fast(ssk); 1166 if (!tcp_rtx_and_write_queues_empty(ssk)) { 1167 subflow->stale = 1; 1168 __mptcp_retransmit_pending_data(sk); 1169 MPTCP_INC_STATS(net, MPTCP_MIB_SUBFLOWSTALE); 1170 } 1171 unlock_sock_fast(ssk, slow); 1172 1173 /* always try to push the pending data regardless of re-injections: 1174 * we can possibly use backup subflows now, and subflow selection 1175 * is cheap under the msk socket lock 1176 */ 1177 __mptcp_push_pending(sk, 0); 1178 return; 1179 } 1180 } 1181 } 1182 1183 static int mptcp_pm_family_to_addr(int family) 1184 { 1185 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1186 if (family == AF_INET6) 1187 return MPTCP_PM_ADDR_ATTR_ADDR6; 1188 #endif 1189 return MPTCP_PM_ADDR_ATTR_ADDR4; 1190 } 1191 1192 static int mptcp_pm_parse_pm_addr_attr(struct nlattr *tb[], 1193 const struct nlattr *attr, 1194 struct genl_info *info, 1195 struct mptcp_addr_info *addr, 1196 bool require_family) 1197 { 1198 int err, addr_addr; 1199 1200 if (!attr) { 1201 GENL_SET_ERR_MSG(info, "missing address info"); 1202 return -EINVAL; 1203 } 1204 1205 /* no validation needed - was already done via nested policy */ 1206 err = nla_parse_nested_deprecated(tb, MPTCP_PM_ADDR_ATTR_MAX, attr, 1207 mptcp_pm_addr_policy, info->extack); 1208 if (err) 1209 return err; 1210 1211 if (tb[MPTCP_PM_ADDR_ATTR_ID]) 1212 addr->id = nla_get_u8(tb[MPTCP_PM_ADDR_ATTR_ID]); 1213 1214 if (!tb[MPTCP_PM_ADDR_ATTR_FAMILY]) { 1215 if (!require_family) 1216 return 0; 1217 1218 NL_SET_ERR_MSG_ATTR(info->extack, attr, 1219 "missing family"); 1220 return -EINVAL; 1221 } 1222 1223 addr->family = nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_FAMILY]); 1224 if (addr->family != AF_INET 1225 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1226 && addr->family != AF_INET6 1227 #endif 1228 ) { 1229 NL_SET_ERR_MSG_ATTR(info->extack, attr, 1230 "unknown address family"); 1231 return -EINVAL; 1232 } 1233 addr_addr = mptcp_pm_family_to_addr(addr->family); 1234 if (!tb[addr_addr]) { 1235 NL_SET_ERR_MSG_ATTR(info->extack, attr, 1236 "missing address data"); 1237 return -EINVAL; 1238 } 1239 1240 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1241 if (addr->family == AF_INET6) 1242 addr->addr6 = nla_get_in6_addr(tb[addr_addr]); 1243 else 1244 #endif 1245 addr->addr.s_addr = nla_get_in_addr(tb[addr_addr]); 1246 1247 if (tb[MPTCP_PM_ADDR_ATTR_PORT]) 1248 addr->port = htons(nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_PORT])); 1249 1250 return 0; 1251 } 1252 1253 int mptcp_pm_parse_addr(struct nlattr *attr, struct genl_info *info, 1254 struct mptcp_addr_info *addr) 1255 { 1256 struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1]; 1257 1258 memset(addr, 0, sizeof(*addr)); 1259 1260 return mptcp_pm_parse_pm_addr_attr(tb, attr, info, addr, true); 1261 } 1262 1263 int mptcp_pm_parse_entry(struct nlattr *attr, struct genl_info *info, 1264 bool require_family, 1265 struct mptcp_pm_addr_entry *entry) 1266 { 1267 struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1]; 1268 int err; 1269 1270 memset(entry, 0, sizeof(*entry)); 1271 1272 err = mptcp_pm_parse_pm_addr_attr(tb, attr, info, &entry->addr, require_family); 1273 if (err) 1274 return err; 1275 1276 if (tb[MPTCP_PM_ADDR_ATTR_IF_IDX]) { 1277 u32 val = nla_get_s32(tb[MPTCP_PM_ADDR_ATTR_IF_IDX]); 1278 1279 entry->ifindex = val; 1280 } 1281 1282 if (tb[MPTCP_PM_ADDR_ATTR_FLAGS]) 1283 entry->flags = nla_get_u32(tb[MPTCP_PM_ADDR_ATTR_FLAGS]); 1284 1285 if (tb[MPTCP_PM_ADDR_ATTR_PORT]) 1286 entry->addr.port = htons(nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_PORT])); 1287 1288 return 0; 1289 } 1290 1291 static struct pm_nl_pernet *genl_info_pm_nl(struct genl_info *info) 1292 { 1293 return pm_nl_get_pernet(genl_info_net(info)); 1294 } 1295 1296 static int mptcp_nl_add_subflow_or_signal_addr(struct net *net) 1297 { 1298 struct mptcp_sock *msk; 1299 long s_slot = 0, s_num = 0; 1300 1301 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { 1302 struct sock *sk = (struct sock *)msk; 1303 1304 if (!READ_ONCE(msk->fully_established) || 1305 mptcp_pm_is_userspace(msk)) 1306 goto next; 1307 1308 lock_sock(sk); 1309 spin_lock_bh(&msk->pm.lock); 1310 mptcp_pm_create_subflow_or_signal_addr(msk); 1311 spin_unlock_bh(&msk->pm.lock); 1312 release_sock(sk); 1313 1314 next: 1315 sock_put(sk); 1316 cond_resched(); 1317 } 1318 1319 return 0; 1320 } 1321 1322 static bool mptcp_pm_has_addr_attr_id(const struct nlattr *attr, 1323 struct genl_info *info) 1324 { 1325 struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1]; 1326 1327 if (!nla_parse_nested_deprecated(tb, MPTCP_PM_ADDR_ATTR_MAX, attr, 1328 mptcp_pm_addr_policy, info->extack) && 1329 tb[MPTCP_PM_ADDR_ATTR_ID]) 1330 return true; 1331 return false; 1332 } 1333 1334 static int mptcp_nl_cmd_add_addr(struct sk_buff *skb, struct genl_info *info) 1335 { 1336 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR]; 1337 struct pm_nl_pernet *pernet = genl_info_pm_nl(info); 1338 struct mptcp_pm_addr_entry addr, *entry; 1339 int ret; 1340 1341 ret = mptcp_pm_parse_entry(attr, info, true, &addr); 1342 if (ret < 0) 1343 return ret; 1344 1345 if (addr.addr.port && !(addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) { 1346 GENL_SET_ERR_MSG(info, "flags must have signal when using port"); 1347 return -EINVAL; 1348 } 1349 1350 if (addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL && 1351 addr.flags & MPTCP_PM_ADDR_FLAG_FULLMESH) { 1352 GENL_SET_ERR_MSG(info, "flags mustn't have both signal and fullmesh"); 1353 return -EINVAL; 1354 } 1355 1356 if (addr.flags & MPTCP_PM_ADDR_FLAG_IMPLICIT) { 1357 GENL_SET_ERR_MSG(info, "can't create IMPLICIT endpoint"); 1358 return -EINVAL; 1359 } 1360 1361 entry = kzalloc(sizeof(*entry), GFP_KERNEL_ACCOUNT); 1362 if (!entry) { 1363 GENL_SET_ERR_MSG(info, "can't allocate addr"); 1364 return -ENOMEM; 1365 } 1366 1367 *entry = addr; 1368 if (entry->addr.port) { 1369 ret = mptcp_pm_nl_create_listen_socket(skb->sk, entry); 1370 if (ret) { 1371 GENL_SET_ERR_MSG_FMT(info, "create listen socket error: %d", ret); 1372 goto out_free; 1373 } 1374 } 1375 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry, 1376 !mptcp_pm_has_addr_attr_id(attr, info)); 1377 if (ret < 0) { 1378 GENL_SET_ERR_MSG_FMT(info, "too many addresses or duplicate one: %d", ret); 1379 goto out_free; 1380 } 1381 1382 mptcp_nl_add_subflow_or_signal_addr(sock_net(skb->sk)); 1383 return 0; 1384 1385 out_free: 1386 __mptcp_pm_release_addr_entry(entry); 1387 return ret; 1388 } 1389 1390 int mptcp_pm_nl_get_flags_and_ifindex_by_id(struct mptcp_sock *msk, unsigned int id, 1391 u8 *flags, int *ifindex) 1392 { 1393 struct mptcp_pm_addr_entry *entry; 1394 struct sock *sk = (struct sock *)msk; 1395 struct net *net = sock_net(sk); 1396 1397 rcu_read_lock(); 1398 entry = __lookup_addr_by_id(pm_nl_get_pernet(net), id); 1399 if (entry) { 1400 *flags = entry->flags; 1401 *ifindex = entry->ifindex; 1402 } 1403 rcu_read_unlock(); 1404 1405 return 0; 1406 } 1407 1408 static bool remove_anno_list_by_saddr(struct mptcp_sock *msk, 1409 const struct mptcp_addr_info *addr) 1410 { 1411 struct mptcp_pm_add_entry *entry; 1412 1413 entry = mptcp_pm_del_add_timer(msk, addr, false); 1414 if (entry) { 1415 list_del(&entry->list); 1416 kfree(entry); 1417 return true; 1418 } 1419 1420 return false; 1421 } 1422 1423 static bool mptcp_pm_remove_anno_addr(struct mptcp_sock *msk, 1424 const struct mptcp_addr_info *addr, 1425 bool force) 1426 { 1427 struct mptcp_rm_list list = { .nr = 0 }; 1428 bool ret; 1429 1430 list.ids[list.nr++] = addr->id; 1431 1432 ret = remove_anno_list_by_saddr(msk, addr); 1433 if (ret || force) { 1434 spin_lock_bh(&msk->pm.lock); 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 1569 list_for_each_entry(entry, rm_list, list) { 1570 if ((remove_anno_list_by_saddr(msk, &entry->addr) || 1571 lookup_subflow_by_saddr(&msk->conn_list, &entry->addr)) && 1572 alist.nr < MPTCP_RM_IDS_MAX) 1573 alist.ids[alist.nr++] = entry->addr.id; 1574 } 1575 1576 if (alist.nr) { 1577 spin_lock_bh(&msk->pm.lock); 1578 mptcp_pm_remove_addr(msk, &alist); 1579 spin_unlock_bh(&msk->pm.lock); 1580 } 1581 } 1582 1583 void mptcp_pm_remove_addrs_and_subflows(struct mptcp_sock *msk, 1584 struct list_head *rm_list) 1585 { 1586 struct mptcp_rm_list alist = { .nr = 0 }, slist = { .nr = 0 }; 1587 struct mptcp_pm_addr_entry *entry; 1588 1589 list_for_each_entry(entry, rm_list, list) { 1590 if (lookup_subflow_by_saddr(&msk->conn_list, &entry->addr) && 1591 slist.nr < MPTCP_RM_IDS_MAX) 1592 slist.ids[slist.nr++] = entry->addr.id; 1593 1594 if (remove_anno_list_by_saddr(msk, &entry->addr) && 1595 alist.nr < MPTCP_RM_IDS_MAX) 1596 alist.ids[alist.nr++] = entry->addr.id; 1597 } 1598 1599 if (alist.nr) { 1600 spin_lock_bh(&msk->pm.lock); 1601 mptcp_pm_remove_addr(msk, &alist); 1602 spin_unlock_bh(&msk->pm.lock); 1603 } 1604 if (slist.nr) 1605 mptcp_pm_remove_subflow(msk, &slist); 1606 } 1607 1608 static void mptcp_nl_remove_addrs_list(struct net *net, 1609 struct list_head *rm_list) 1610 { 1611 long s_slot = 0, s_num = 0; 1612 struct mptcp_sock *msk; 1613 1614 if (list_empty(rm_list)) 1615 return; 1616 1617 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { 1618 struct sock *sk = (struct sock *)msk; 1619 1620 if (!mptcp_pm_is_userspace(msk)) { 1621 lock_sock(sk); 1622 mptcp_pm_remove_addrs_and_subflows(msk, rm_list); 1623 release_sock(sk); 1624 } 1625 1626 sock_put(sk); 1627 cond_resched(); 1628 } 1629 } 1630 1631 /* caller must ensure the RCU grace period is already elapsed */ 1632 static void __flush_addrs(struct list_head *list) 1633 { 1634 while (!list_empty(list)) { 1635 struct mptcp_pm_addr_entry *cur; 1636 1637 cur = list_entry(list->next, 1638 struct mptcp_pm_addr_entry, list); 1639 list_del_rcu(&cur->list); 1640 __mptcp_pm_release_addr_entry(cur); 1641 } 1642 } 1643 1644 static void __reset_counters(struct pm_nl_pernet *pernet) 1645 { 1646 WRITE_ONCE(pernet->add_addr_signal_max, 0); 1647 WRITE_ONCE(pernet->add_addr_accept_max, 0); 1648 WRITE_ONCE(pernet->local_addr_max, 0); 1649 pernet->addrs = 0; 1650 } 1651 1652 static int mptcp_nl_cmd_flush_addrs(struct sk_buff *skb, struct genl_info *info) 1653 { 1654 struct pm_nl_pernet *pernet = genl_info_pm_nl(info); 1655 LIST_HEAD(free_list); 1656 1657 spin_lock_bh(&pernet->lock); 1658 list_splice_init(&pernet->local_addr_list, &free_list); 1659 __reset_counters(pernet); 1660 pernet->next_id = 1; 1661 bitmap_zero(pernet->id_bitmap, MPTCP_PM_MAX_ADDR_ID + 1); 1662 spin_unlock_bh(&pernet->lock); 1663 mptcp_nl_remove_addrs_list(sock_net(skb->sk), &free_list); 1664 synchronize_rcu(); 1665 __flush_addrs(&free_list); 1666 return 0; 1667 } 1668 1669 static int mptcp_nl_fill_addr(struct sk_buff *skb, 1670 struct mptcp_pm_addr_entry *entry) 1671 { 1672 struct mptcp_addr_info *addr = &entry->addr; 1673 struct nlattr *attr; 1674 1675 attr = nla_nest_start(skb, MPTCP_PM_ATTR_ADDR); 1676 if (!attr) 1677 return -EMSGSIZE; 1678 1679 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_FAMILY, addr->family)) 1680 goto nla_put_failure; 1681 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_PORT, ntohs(addr->port))) 1682 goto nla_put_failure; 1683 if (nla_put_u8(skb, MPTCP_PM_ADDR_ATTR_ID, addr->id)) 1684 goto nla_put_failure; 1685 if (nla_put_u32(skb, MPTCP_PM_ADDR_ATTR_FLAGS, entry->flags)) 1686 goto nla_put_failure; 1687 if (entry->ifindex && 1688 nla_put_s32(skb, MPTCP_PM_ADDR_ATTR_IF_IDX, entry->ifindex)) 1689 goto nla_put_failure; 1690 1691 if (addr->family == AF_INET && 1692 nla_put_in_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR4, 1693 addr->addr.s_addr)) 1694 goto nla_put_failure; 1695 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1696 else if (addr->family == AF_INET6 && 1697 nla_put_in6_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR6, &addr->addr6)) 1698 goto nla_put_failure; 1699 #endif 1700 nla_nest_end(skb, attr); 1701 return 0; 1702 1703 nla_put_failure: 1704 nla_nest_cancel(skb, attr); 1705 return -EMSGSIZE; 1706 } 1707 1708 static int mptcp_nl_cmd_get_addr(struct sk_buff *skb, struct genl_info *info) 1709 { 1710 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR]; 1711 struct pm_nl_pernet *pernet = genl_info_pm_nl(info); 1712 struct mptcp_pm_addr_entry addr, *entry; 1713 struct sk_buff *msg; 1714 void *reply; 1715 int ret; 1716 1717 ret = mptcp_pm_parse_entry(attr, info, false, &addr); 1718 if (ret < 0) 1719 return ret; 1720 1721 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1722 if (!msg) 1723 return -ENOMEM; 1724 1725 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0, 1726 info->genlhdr->cmd); 1727 if (!reply) { 1728 GENL_SET_ERR_MSG(info, "not enough space in Netlink message"); 1729 ret = -EMSGSIZE; 1730 goto fail; 1731 } 1732 1733 spin_lock_bh(&pernet->lock); 1734 entry = __lookup_addr_by_id(pernet, addr.addr.id); 1735 if (!entry) { 1736 GENL_SET_ERR_MSG(info, "address not found"); 1737 ret = -EINVAL; 1738 goto unlock_fail; 1739 } 1740 1741 ret = mptcp_nl_fill_addr(msg, entry); 1742 if (ret) 1743 goto unlock_fail; 1744 1745 genlmsg_end(msg, reply); 1746 ret = genlmsg_reply(msg, info); 1747 spin_unlock_bh(&pernet->lock); 1748 return ret; 1749 1750 unlock_fail: 1751 spin_unlock_bh(&pernet->lock); 1752 1753 fail: 1754 nlmsg_free(msg); 1755 return ret; 1756 } 1757 1758 static int mptcp_nl_cmd_dump_addrs(struct sk_buff *msg, 1759 struct netlink_callback *cb) 1760 { 1761 struct net *net = sock_net(msg->sk); 1762 struct mptcp_pm_addr_entry *entry; 1763 struct pm_nl_pernet *pernet; 1764 int id = cb->args[0]; 1765 void *hdr; 1766 int i; 1767 1768 pernet = pm_nl_get_pernet(net); 1769 1770 spin_lock_bh(&pernet->lock); 1771 for (i = id; i < MPTCP_PM_MAX_ADDR_ID + 1; i++) { 1772 if (test_bit(i, pernet->id_bitmap)) { 1773 entry = __lookup_addr_by_id(pernet, i); 1774 if (!entry) 1775 break; 1776 1777 if (entry->addr.id <= id) 1778 continue; 1779 1780 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, 1781 cb->nlh->nlmsg_seq, &mptcp_genl_family, 1782 NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR); 1783 if (!hdr) 1784 break; 1785 1786 if (mptcp_nl_fill_addr(msg, entry) < 0) { 1787 genlmsg_cancel(msg, hdr); 1788 break; 1789 } 1790 1791 id = entry->addr.id; 1792 genlmsg_end(msg, hdr); 1793 } 1794 } 1795 spin_unlock_bh(&pernet->lock); 1796 1797 cb->args[0] = id; 1798 return msg->len; 1799 } 1800 1801 static int parse_limit(struct genl_info *info, int id, unsigned int *limit) 1802 { 1803 struct nlattr *attr = info->attrs[id]; 1804 1805 if (!attr) 1806 return 0; 1807 1808 *limit = nla_get_u32(attr); 1809 if (*limit > MPTCP_PM_ADDR_MAX) { 1810 GENL_SET_ERR_MSG(info, "limit greater than maximum"); 1811 return -EINVAL; 1812 } 1813 return 0; 1814 } 1815 1816 static int 1817 mptcp_nl_cmd_set_limits(struct sk_buff *skb, struct genl_info *info) 1818 { 1819 struct pm_nl_pernet *pernet = genl_info_pm_nl(info); 1820 unsigned int rcv_addrs, subflows; 1821 int ret; 1822 1823 spin_lock_bh(&pernet->lock); 1824 rcv_addrs = pernet->add_addr_accept_max; 1825 ret = parse_limit(info, MPTCP_PM_ATTR_RCV_ADD_ADDRS, &rcv_addrs); 1826 if (ret) 1827 goto unlock; 1828 1829 subflows = pernet->subflows_max; 1830 ret = parse_limit(info, MPTCP_PM_ATTR_SUBFLOWS, &subflows); 1831 if (ret) 1832 goto unlock; 1833 1834 WRITE_ONCE(pernet->add_addr_accept_max, rcv_addrs); 1835 WRITE_ONCE(pernet->subflows_max, subflows); 1836 1837 unlock: 1838 spin_unlock_bh(&pernet->lock); 1839 return ret; 1840 } 1841 1842 static int 1843 mptcp_nl_cmd_get_limits(struct sk_buff *skb, struct genl_info *info) 1844 { 1845 struct pm_nl_pernet *pernet = genl_info_pm_nl(info); 1846 struct sk_buff *msg; 1847 void *reply; 1848 1849 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1850 if (!msg) 1851 return -ENOMEM; 1852 1853 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0, 1854 MPTCP_PM_CMD_GET_LIMITS); 1855 if (!reply) 1856 goto fail; 1857 1858 if (nla_put_u32(msg, MPTCP_PM_ATTR_RCV_ADD_ADDRS, 1859 READ_ONCE(pernet->add_addr_accept_max))) 1860 goto fail; 1861 1862 if (nla_put_u32(msg, MPTCP_PM_ATTR_SUBFLOWS, 1863 READ_ONCE(pernet->subflows_max))) 1864 goto fail; 1865 1866 genlmsg_end(msg, reply); 1867 return genlmsg_reply(msg, info); 1868 1869 fail: 1870 GENL_SET_ERR_MSG(info, "not enough space in Netlink message"); 1871 nlmsg_free(msg); 1872 return -EMSGSIZE; 1873 } 1874 1875 static void mptcp_pm_nl_fullmesh(struct mptcp_sock *msk, 1876 struct mptcp_addr_info *addr) 1877 { 1878 struct mptcp_rm_list list = { .nr = 0 }; 1879 1880 list.ids[list.nr++] = addr->id; 1881 1882 spin_lock_bh(&msk->pm.lock); 1883 mptcp_pm_nl_rm_subflow_received(msk, &list); 1884 mptcp_pm_create_subflow_or_signal_addr(msk); 1885 spin_unlock_bh(&msk->pm.lock); 1886 } 1887 1888 static int mptcp_nl_set_flags(struct net *net, 1889 struct mptcp_addr_info *addr, 1890 u8 bkup, u8 changed) 1891 { 1892 long s_slot = 0, s_num = 0; 1893 struct mptcp_sock *msk; 1894 int ret = -EINVAL; 1895 1896 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { 1897 struct sock *sk = (struct sock *)msk; 1898 1899 if (list_empty(&msk->conn_list) || mptcp_pm_is_userspace(msk)) 1900 goto next; 1901 1902 lock_sock(sk); 1903 if (changed & MPTCP_PM_ADDR_FLAG_BACKUP) 1904 ret = mptcp_pm_nl_mp_prio_send_ack(msk, addr, NULL, bkup); 1905 if (changed & MPTCP_PM_ADDR_FLAG_FULLMESH) 1906 mptcp_pm_nl_fullmesh(msk, addr); 1907 release_sock(sk); 1908 1909 next: 1910 sock_put(sk); 1911 cond_resched(); 1912 } 1913 1914 return ret; 1915 } 1916 1917 int mptcp_pm_nl_set_flags(struct net *net, struct mptcp_pm_addr_entry *addr, u8 bkup) 1918 { 1919 struct pm_nl_pernet *pernet = pm_nl_get_pernet(net); 1920 u8 changed, mask = MPTCP_PM_ADDR_FLAG_BACKUP | 1921 MPTCP_PM_ADDR_FLAG_FULLMESH; 1922 struct mptcp_pm_addr_entry *entry; 1923 u8 lookup_by_id = 0; 1924 1925 if (addr->addr.family == AF_UNSPEC) { 1926 lookup_by_id = 1; 1927 if (!addr->addr.id) 1928 return -EOPNOTSUPP; 1929 } 1930 1931 spin_lock_bh(&pernet->lock); 1932 entry = __lookup_addr(pernet, &addr->addr, lookup_by_id); 1933 if (!entry) { 1934 spin_unlock_bh(&pernet->lock); 1935 return -EINVAL; 1936 } 1937 if ((addr->flags & MPTCP_PM_ADDR_FLAG_FULLMESH) && 1938 (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) { 1939 spin_unlock_bh(&pernet->lock); 1940 return -EINVAL; 1941 } 1942 1943 changed = (addr->flags ^ entry->flags) & mask; 1944 entry->flags = (entry->flags & ~mask) | (addr->flags & mask); 1945 *addr = *entry; 1946 spin_unlock_bh(&pernet->lock); 1947 1948 mptcp_nl_set_flags(net, &addr->addr, bkup, changed); 1949 return 0; 1950 } 1951 1952 static int mptcp_nl_cmd_set_flags(struct sk_buff *skb, struct genl_info *info) 1953 { 1954 struct mptcp_pm_addr_entry remote = { .addr = { .family = AF_UNSPEC }, }; 1955 struct mptcp_pm_addr_entry addr = { .addr = { .family = AF_UNSPEC }, }; 1956 struct nlattr *attr_rem = info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE]; 1957 struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN]; 1958 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR]; 1959 struct net *net = sock_net(skb->sk); 1960 u8 bkup = 0; 1961 int ret; 1962 1963 ret = mptcp_pm_parse_entry(attr, info, false, &addr); 1964 if (ret < 0) 1965 return ret; 1966 1967 if (attr_rem) { 1968 ret = mptcp_pm_parse_entry(attr_rem, info, false, &remote); 1969 if (ret < 0) 1970 return ret; 1971 } 1972 1973 if (addr.flags & MPTCP_PM_ADDR_FLAG_BACKUP) 1974 bkup = 1; 1975 1976 return mptcp_pm_set_flags(net, token, &addr, &remote, bkup); 1977 } 1978 1979 static void mptcp_nl_mcast_send(struct net *net, struct sk_buff *nlskb, gfp_t gfp) 1980 { 1981 genlmsg_multicast_netns(&mptcp_genl_family, net, 1982 nlskb, 0, MPTCP_PM_EV_GRP_OFFSET, gfp); 1983 } 1984 1985 bool mptcp_userspace_pm_active(const struct mptcp_sock *msk) 1986 { 1987 return genl_has_listeners(&mptcp_genl_family, 1988 sock_net((const struct sock *)msk), 1989 MPTCP_PM_EV_GRP_OFFSET); 1990 } 1991 1992 static int mptcp_event_add_subflow(struct sk_buff *skb, const struct sock *ssk) 1993 { 1994 const struct inet_sock *issk = inet_sk(ssk); 1995 const struct mptcp_subflow_context *sf; 1996 1997 if (nla_put_u16(skb, MPTCP_ATTR_FAMILY, ssk->sk_family)) 1998 return -EMSGSIZE; 1999 2000 switch (ssk->sk_family) { 2001 case AF_INET: 2002 if (nla_put_in_addr(skb, MPTCP_ATTR_SADDR4, issk->inet_saddr)) 2003 return -EMSGSIZE; 2004 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, issk->inet_daddr)) 2005 return -EMSGSIZE; 2006 break; 2007 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2008 case AF_INET6: { 2009 const struct ipv6_pinfo *np = inet6_sk(ssk); 2010 2011 if (nla_put_in6_addr(skb, MPTCP_ATTR_SADDR6, &np->saddr)) 2012 return -EMSGSIZE; 2013 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &ssk->sk_v6_daddr)) 2014 return -EMSGSIZE; 2015 break; 2016 } 2017 #endif 2018 default: 2019 WARN_ON_ONCE(1); 2020 return -EMSGSIZE; 2021 } 2022 2023 if (nla_put_be16(skb, MPTCP_ATTR_SPORT, issk->inet_sport)) 2024 return -EMSGSIZE; 2025 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, issk->inet_dport)) 2026 return -EMSGSIZE; 2027 2028 sf = mptcp_subflow_ctx(ssk); 2029 if (WARN_ON_ONCE(!sf)) 2030 return -EINVAL; 2031 2032 if (nla_put_u8(skb, MPTCP_ATTR_LOC_ID, subflow_get_local_id(sf))) 2033 return -EMSGSIZE; 2034 2035 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, sf->remote_id)) 2036 return -EMSGSIZE; 2037 2038 return 0; 2039 } 2040 2041 static int mptcp_event_put_token_and_ssk(struct sk_buff *skb, 2042 const struct mptcp_sock *msk, 2043 const struct sock *ssk) 2044 { 2045 const struct sock *sk = (const struct sock *)msk; 2046 const struct mptcp_subflow_context *sf; 2047 u8 sk_err; 2048 2049 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token)) 2050 return -EMSGSIZE; 2051 2052 if (mptcp_event_add_subflow(skb, ssk)) 2053 return -EMSGSIZE; 2054 2055 sf = mptcp_subflow_ctx(ssk); 2056 if (WARN_ON_ONCE(!sf)) 2057 return -EINVAL; 2058 2059 if (nla_put_u8(skb, MPTCP_ATTR_BACKUP, sf->backup)) 2060 return -EMSGSIZE; 2061 2062 if (ssk->sk_bound_dev_if && 2063 nla_put_s32(skb, MPTCP_ATTR_IF_IDX, ssk->sk_bound_dev_if)) 2064 return -EMSGSIZE; 2065 2066 sk_err = READ_ONCE(ssk->sk_err); 2067 if (sk_err && sk->sk_state == TCP_ESTABLISHED && 2068 nla_put_u8(skb, MPTCP_ATTR_ERROR, sk_err)) 2069 return -EMSGSIZE; 2070 2071 return 0; 2072 } 2073 2074 static int mptcp_event_sub_established(struct sk_buff *skb, 2075 const struct mptcp_sock *msk, 2076 const struct sock *ssk) 2077 { 2078 return mptcp_event_put_token_and_ssk(skb, msk, ssk); 2079 } 2080 2081 static int mptcp_event_sub_closed(struct sk_buff *skb, 2082 const struct mptcp_sock *msk, 2083 const struct sock *ssk) 2084 { 2085 const struct mptcp_subflow_context *sf; 2086 2087 if (mptcp_event_put_token_and_ssk(skb, msk, ssk)) 2088 return -EMSGSIZE; 2089 2090 sf = mptcp_subflow_ctx(ssk); 2091 if (!sf->reset_seen) 2092 return 0; 2093 2094 if (nla_put_u32(skb, MPTCP_ATTR_RESET_REASON, sf->reset_reason)) 2095 return -EMSGSIZE; 2096 2097 if (nla_put_u32(skb, MPTCP_ATTR_RESET_FLAGS, sf->reset_transient)) 2098 return -EMSGSIZE; 2099 2100 return 0; 2101 } 2102 2103 static int mptcp_event_created(struct sk_buff *skb, 2104 const struct mptcp_sock *msk, 2105 const struct sock *ssk) 2106 { 2107 int err = nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token); 2108 2109 if (err) 2110 return err; 2111 2112 if (nla_put_u8(skb, MPTCP_ATTR_SERVER_SIDE, READ_ONCE(msk->pm.server_side))) 2113 return -EMSGSIZE; 2114 2115 return mptcp_event_add_subflow(skb, ssk); 2116 } 2117 2118 void mptcp_event_addr_removed(const struct mptcp_sock *msk, uint8_t id) 2119 { 2120 struct net *net = sock_net((const struct sock *)msk); 2121 struct nlmsghdr *nlh; 2122 struct sk_buff *skb; 2123 2124 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET)) 2125 return; 2126 2127 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); 2128 if (!skb) 2129 return; 2130 2131 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, MPTCP_EVENT_REMOVED); 2132 if (!nlh) 2133 goto nla_put_failure; 2134 2135 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token)) 2136 goto nla_put_failure; 2137 2138 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, id)) 2139 goto nla_put_failure; 2140 2141 genlmsg_end(skb, nlh); 2142 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC); 2143 return; 2144 2145 nla_put_failure: 2146 nlmsg_free(skb); 2147 } 2148 2149 void mptcp_event_addr_announced(const struct sock *ssk, 2150 const struct mptcp_addr_info *info) 2151 { 2152 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 2153 struct mptcp_sock *msk = mptcp_sk(subflow->conn); 2154 struct net *net = sock_net(ssk); 2155 struct nlmsghdr *nlh; 2156 struct sk_buff *skb; 2157 2158 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET)) 2159 return; 2160 2161 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); 2162 if (!skb) 2163 return; 2164 2165 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, 2166 MPTCP_EVENT_ANNOUNCED); 2167 if (!nlh) 2168 goto nla_put_failure; 2169 2170 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token)) 2171 goto nla_put_failure; 2172 2173 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, info->id)) 2174 goto nla_put_failure; 2175 2176 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, 2177 info->port == 0 ? 2178 inet_sk(ssk)->inet_dport : 2179 info->port)) 2180 goto nla_put_failure; 2181 2182 switch (info->family) { 2183 case AF_INET: 2184 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, info->addr.s_addr)) 2185 goto nla_put_failure; 2186 break; 2187 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2188 case AF_INET6: 2189 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &info->addr6)) 2190 goto nla_put_failure; 2191 break; 2192 #endif 2193 default: 2194 WARN_ON_ONCE(1); 2195 goto nla_put_failure; 2196 } 2197 2198 genlmsg_end(skb, nlh); 2199 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC); 2200 return; 2201 2202 nla_put_failure: 2203 nlmsg_free(skb); 2204 } 2205 2206 void mptcp_event_pm_listener(const struct sock *ssk, 2207 enum mptcp_event_type event) 2208 { 2209 const struct inet_sock *issk = inet_sk(ssk); 2210 struct net *net = sock_net(ssk); 2211 struct nlmsghdr *nlh; 2212 struct sk_buff *skb; 2213 2214 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET)) 2215 return; 2216 2217 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 2218 if (!skb) 2219 return; 2220 2221 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, event); 2222 if (!nlh) 2223 goto nla_put_failure; 2224 2225 if (nla_put_u16(skb, MPTCP_ATTR_FAMILY, ssk->sk_family)) 2226 goto nla_put_failure; 2227 2228 if (nla_put_be16(skb, MPTCP_ATTR_SPORT, issk->inet_sport)) 2229 goto nla_put_failure; 2230 2231 switch (ssk->sk_family) { 2232 case AF_INET: 2233 if (nla_put_in_addr(skb, MPTCP_ATTR_SADDR4, issk->inet_saddr)) 2234 goto nla_put_failure; 2235 break; 2236 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2237 case AF_INET6: { 2238 const struct ipv6_pinfo *np = inet6_sk(ssk); 2239 2240 if (nla_put_in6_addr(skb, MPTCP_ATTR_SADDR6, &np->saddr)) 2241 goto nla_put_failure; 2242 break; 2243 } 2244 #endif 2245 default: 2246 WARN_ON_ONCE(1); 2247 goto nla_put_failure; 2248 } 2249 2250 genlmsg_end(skb, nlh); 2251 mptcp_nl_mcast_send(net, skb, GFP_KERNEL); 2252 return; 2253 2254 nla_put_failure: 2255 nlmsg_free(skb); 2256 } 2257 2258 void mptcp_event(enum mptcp_event_type type, const struct mptcp_sock *msk, 2259 const struct sock *ssk, gfp_t gfp) 2260 { 2261 struct net *net = sock_net((const struct sock *)msk); 2262 struct nlmsghdr *nlh; 2263 struct sk_buff *skb; 2264 2265 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET)) 2266 return; 2267 2268 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp); 2269 if (!skb) 2270 return; 2271 2272 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, type); 2273 if (!nlh) 2274 goto nla_put_failure; 2275 2276 switch (type) { 2277 case MPTCP_EVENT_UNSPEC: 2278 WARN_ON_ONCE(1); 2279 break; 2280 case MPTCP_EVENT_CREATED: 2281 case MPTCP_EVENT_ESTABLISHED: 2282 if (mptcp_event_created(skb, msk, ssk) < 0) 2283 goto nla_put_failure; 2284 break; 2285 case MPTCP_EVENT_CLOSED: 2286 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token) < 0) 2287 goto nla_put_failure; 2288 break; 2289 case MPTCP_EVENT_ANNOUNCED: 2290 case MPTCP_EVENT_REMOVED: 2291 /* call mptcp_event_addr_announced()/removed instead */ 2292 WARN_ON_ONCE(1); 2293 break; 2294 case MPTCP_EVENT_SUB_ESTABLISHED: 2295 case MPTCP_EVENT_SUB_PRIORITY: 2296 if (mptcp_event_sub_established(skb, msk, ssk) < 0) 2297 goto nla_put_failure; 2298 break; 2299 case MPTCP_EVENT_SUB_CLOSED: 2300 if (mptcp_event_sub_closed(skb, msk, ssk) < 0) 2301 goto nla_put_failure; 2302 break; 2303 case MPTCP_EVENT_LISTENER_CREATED: 2304 case MPTCP_EVENT_LISTENER_CLOSED: 2305 break; 2306 } 2307 2308 genlmsg_end(skb, nlh); 2309 mptcp_nl_mcast_send(net, skb, gfp); 2310 return; 2311 2312 nla_put_failure: 2313 nlmsg_free(skb); 2314 } 2315 2316 static const struct genl_small_ops mptcp_pm_ops[] = { 2317 { 2318 .cmd = MPTCP_PM_CMD_ADD_ADDR, 2319 .doit = mptcp_nl_cmd_add_addr, 2320 .flags = GENL_UNS_ADMIN_PERM, 2321 }, 2322 { 2323 .cmd = MPTCP_PM_CMD_DEL_ADDR, 2324 .doit = mptcp_nl_cmd_del_addr, 2325 .flags = GENL_UNS_ADMIN_PERM, 2326 }, 2327 { 2328 .cmd = MPTCP_PM_CMD_FLUSH_ADDRS, 2329 .doit = mptcp_nl_cmd_flush_addrs, 2330 .flags = GENL_UNS_ADMIN_PERM, 2331 }, 2332 { 2333 .cmd = MPTCP_PM_CMD_GET_ADDR, 2334 .doit = mptcp_nl_cmd_get_addr, 2335 .dumpit = mptcp_nl_cmd_dump_addrs, 2336 }, 2337 { 2338 .cmd = MPTCP_PM_CMD_SET_LIMITS, 2339 .doit = mptcp_nl_cmd_set_limits, 2340 .flags = GENL_UNS_ADMIN_PERM, 2341 }, 2342 { 2343 .cmd = MPTCP_PM_CMD_GET_LIMITS, 2344 .doit = mptcp_nl_cmd_get_limits, 2345 }, 2346 { 2347 .cmd = MPTCP_PM_CMD_SET_FLAGS, 2348 .doit = mptcp_nl_cmd_set_flags, 2349 .flags = GENL_UNS_ADMIN_PERM, 2350 }, 2351 { 2352 .cmd = MPTCP_PM_CMD_ANNOUNCE, 2353 .doit = mptcp_nl_cmd_announce, 2354 .flags = GENL_UNS_ADMIN_PERM, 2355 }, 2356 { 2357 .cmd = MPTCP_PM_CMD_REMOVE, 2358 .doit = mptcp_nl_cmd_remove, 2359 .flags = GENL_UNS_ADMIN_PERM, 2360 }, 2361 { 2362 .cmd = MPTCP_PM_CMD_SUBFLOW_CREATE, 2363 .doit = mptcp_nl_cmd_sf_create, 2364 .flags = GENL_UNS_ADMIN_PERM, 2365 }, 2366 { 2367 .cmd = MPTCP_PM_CMD_SUBFLOW_DESTROY, 2368 .doit = mptcp_nl_cmd_sf_destroy, 2369 .flags = GENL_UNS_ADMIN_PERM, 2370 }, 2371 }; 2372 2373 static struct genl_family mptcp_genl_family __ro_after_init = { 2374 .name = MPTCP_PM_NAME, 2375 .version = MPTCP_PM_VER, 2376 .maxattr = MPTCP_PM_ATTR_MAX, 2377 .policy = mptcp_pm_policy, 2378 .netnsok = true, 2379 .module = THIS_MODULE, 2380 .small_ops = mptcp_pm_ops, 2381 .n_small_ops = ARRAY_SIZE(mptcp_pm_ops), 2382 .resv_start_op = MPTCP_PM_CMD_SUBFLOW_DESTROY + 1, 2383 .mcgrps = mptcp_pm_mcgrps, 2384 .n_mcgrps = ARRAY_SIZE(mptcp_pm_mcgrps), 2385 }; 2386 2387 static int __net_init pm_nl_init_net(struct net *net) 2388 { 2389 struct pm_nl_pernet *pernet = pm_nl_get_pernet(net); 2390 2391 INIT_LIST_HEAD_RCU(&pernet->local_addr_list); 2392 2393 /* Cit. 2 subflows ought to be enough for anybody. */ 2394 pernet->subflows_max = 2; 2395 pernet->next_id = 1; 2396 pernet->stale_loss_cnt = 4; 2397 spin_lock_init(&pernet->lock); 2398 2399 /* No need to initialize other pernet fields, the struct is zeroed at 2400 * allocation time. 2401 */ 2402 2403 return 0; 2404 } 2405 2406 static void __net_exit pm_nl_exit_net(struct list_head *net_list) 2407 { 2408 struct net *net; 2409 2410 list_for_each_entry(net, net_list, exit_list) { 2411 struct pm_nl_pernet *pernet = pm_nl_get_pernet(net); 2412 2413 /* net is removed from namespace list, can't race with 2414 * other modifiers, also netns core already waited for a 2415 * RCU grace period. 2416 */ 2417 __flush_addrs(&pernet->local_addr_list); 2418 } 2419 } 2420 2421 static struct pernet_operations mptcp_pm_pernet_ops = { 2422 .init = pm_nl_init_net, 2423 .exit_batch = pm_nl_exit_net, 2424 .id = &pm_nl_pernet_id, 2425 .size = sizeof(struct pm_nl_pernet), 2426 }; 2427 2428 void __init mptcp_pm_nl_init(void) 2429 { 2430 if (register_pernet_subsys(&mptcp_pm_pernet_ops) < 0) 2431 panic("Failed to register MPTCP PM pernet subsystem.\n"); 2432 2433 if (genl_register_family(&mptcp_genl_family)) 2434 panic("Failed to register MPTCP PM netlink family\n"); 2435 } 2436