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