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