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 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 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 struct mptcp_sock *msk) 161 { 162 struct mptcp_pm_addr_entry *entry, *ret = NULL; 163 struct sock *sk = (struct sock *)msk; 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, 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(struct mptcp_sock *msk) 218 { 219 struct pm_nl_pernet *pernet; 220 221 pernet = net_generic(sock_net((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(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(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(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(struct mptcp_sock *msk, 268 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 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 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(struct mptcp_addr_info *addrs, unsigned int nr, 414 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(struct pm_nl_pernet *pernet, const struct mptcp_addr_info *addr) 497 { 498 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 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) { 1202 NL_SET_ERR_MSG_ATTR(info->extack, attr, 1203 "flags must have signal when using port"); 1204 return -EINVAL; 1205 } 1206 entry->addr.port = htons(nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_PORT])); 1207 } 1208 1209 return 0; 1210 } 1211 1212 static struct pm_nl_pernet *genl_info_pm_nl(struct genl_info *info) 1213 { 1214 return net_generic(genl_info_net(info), pm_nl_pernet_id); 1215 } 1216 1217 static int mptcp_nl_add_subflow_or_signal_addr(struct net *net) 1218 { 1219 struct mptcp_sock *msk; 1220 long s_slot = 0, s_num = 0; 1221 1222 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { 1223 struct sock *sk = (struct sock *)msk; 1224 1225 if (!READ_ONCE(msk->fully_established)) 1226 goto next; 1227 1228 lock_sock(sk); 1229 spin_lock_bh(&msk->pm.lock); 1230 mptcp_pm_create_subflow_or_signal_addr(msk); 1231 spin_unlock_bh(&msk->pm.lock); 1232 release_sock(sk); 1233 1234 next: 1235 sock_put(sk); 1236 cond_resched(); 1237 } 1238 1239 return 0; 1240 } 1241 1242 static int mptcp_nl_cmd_add_addr(struct sk_buff *skb, struct genl_info *info) 1243 { 1244 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR]; 1245 struct pm_nl_pernet *pernet = genl_info_pm_nl(info); 1246 struct mptcp_pm_addr_entry addr, *entry; 1247 int ret; 1248 1249 ret = mptcp_pm_parse_addr(attr, info, true, &addr); 1250 if (ret < 0) 1251 return ret; 1252 1253 entry = kmalloc(sizeof(*entry), GFP_KERNEL); 1254 if (!entry) { 1255 GENL_SET_ERR_MSG(info, "can't allocate addr"); 1256 return -ENOMEM; 1257 } 1258 1259 *entry = addr; 1260 if (entry->addr.port) { 1261 ret = mptcp_pm_nl_create_listen_socket(skb->sk, entry); 1262 if (ret) { 1263 GENL_SET_ERR_MSG(info, "create listen socket error"); 1264 kfree(entry); 1265 return ret; 1266 } 1267 } 1268 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry); 1269 if (ret < 0) { 1270 GENL_SET_ERR_MSG(info, "too many addresses or duplicate one"); 1271 if (entry->lsk) 1272 sock_release(entry->lsk); 1273 kfree(entry); 1274 return ret; 1275 } 1276 1277 mptcp_nl_add_subflow_or_signal_addr(sock_net(skb->sk)); 1278 1279 return 0; 1280 } 1281 1282 int mptcp_pm_get_flags_and_ifindex_by_id(struct net *net, unsigned int id, 1283 u8 *flags, int *ifindex) 1284 { 1285 struct mptcp_pm_addr_entry *entry; 1286 1287 *flags = 0; 1288 *ifindex = 0; 1289 1290 if (id) { 1291 rcu_read_lock(); 1292 entry = __lookup_addr_by_id(net_generic(net, pm_nl_pernet_id), id); 1293 if (entry) { 1294 *flags = entry->flags; 1295 *ifindex = entry->ifindex; 1296 } 1297 rcu_read_unlock(); 1298 } 1299 1300 return 0; 1301 } 1302 1303 static bool remove_anno_list_by_saddr(struct mptcp_sock *msk, 1304 struct mptcp_addr_info *addr) 1305 { 1306 struct mptcp_pm_add_entry *entry; 1307 1308 entry = mptcp_pm_del_add_timer(msk, addr, false); 1309 if (entry) { 1310 list_del(&entry->list); 1311 kfree(entry); 1312 return true; 1313 } 1314 1315 return false; 1316 } 1317 1318 static bool mptcp_pm_remove_anno_addr(struct mptcp_sock *msk, 1319 struct mptcp_addr_info *addr, 1320 bool force) 1321 { 1322 struct mptcp_rm_list list = { .nr = 0 }; 1323 bool ret; 1324 1325 list.ids[list.nr++] = addr->id; 1326 1327 ret = remove_anno_list_by_saddr(msk, addr); 1328 if (ret || force) { 1329 spin_lock_bh(&msk->pm.lock); 1330 mptcp_pm_remove_addr(msk, &list); 1331 spin_unlock_bh(&msk->pm.lock); 1332 } 1333 return ret; 1334 } 1335 1336 static int mptcp_nl_remove_subflow_and_signal_addr(struct net *net, 1337 struct mptcp_addr_info *addr) 1338 { 1339 struct mptcp_sock *msk; 1340 long s_slot = 0, s_num = 0; 1341 struct mptcp_rm_list list = { .nr = 0 }; 1342 1343 pr_debug("remove_id=%d", addr->id); 1344 1345 list.ids[list.nr++] = addr->id; 1346 1347 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { 1348 struct sock *sk = (struct sock *)msk; 1349 bool remove_subflow; 1350 1351 if (list_empty(&msk->conn_list)) { 1352 mptcp_pm_remove_anno_addr(msk, addr, false); 1353 goto next; 1354 } 1355 1356 lock_sock(sk); 1357 remove_subflow = lookup_subflow_by_saddr(&msk->conn_list, addr); 1358 mptcp_pm_remove_anno_addr(msk, addr, remove_subflow); 1359 if (remove_subflow) 1360 mptcp_pm_remove_subflow(msk, &list); 1361 release_sock(sk); 1362 1363 next: 1364 sock_put(sk); 1365 cond_resched(); 1366 } 1367 1368 return 0; 1369 } 1370 1371 /* caller must ensure the RCU grace period is already elapsed */ 1372 static void __mptcp_pm_release_addr_entry(struct mptcp_pm_addr_entry *entry) 1373 { 1374 if (entry->lsk) 1375 sock_release(entry->lsk); 1376 kfree(entry); 1377 } 1378 1379 static int mptcp_nl_remove_id_zero_address(struct net *net, 1380 struct mptcp_addr_info *addr) 1381 { 1382 struct mptcp_rm_list list = { .nr = 0 }; 1383 long s_slot = 0, s_num = 0; 1384 struct mptcp_sock *msk; 1385 1386 list.ids[list.nr++] = 0; 1387 1388 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { 1389 struct sock *sk = (struct sock *)msk; 1390 struct mptcp_addr_info msk_local; 1391 1392 if (list_empty(&msk->conn_list)) 1393 goto next; 1394 1395 local_address((struct sock_common *)msk, &msk_local); 1396 if (!addresses_equal(&msk_local, addr, addr->port)) 1397 goto next; 1398 1399 lock_sock(sk); 1400 spin_lock_bh(&msk->pm.lock); 1401 mptcp_pm_remove_addr(msk, &list); 1402 mptcp_pm_nl_rm_subflow_received(msk, &list); 1403 spin_unlock_bh(&msk->pm.lock); 1404 release_sock(sk); 1405 1406 next: 1407 sock_put(sk); 1408 cond_resched(); 1409 } 1410 1411 return 0; 1412 } 1413 1414 static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info) 1415 { 1416 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR]; 1417 struct pm_nl_pernet *pernet = genl_info_pm_nl(info); 1418 struct mptcp_pm_addr_entry addr, *entry; 1419 unsigned int addr_max; 1420 int ret; 1421 1422 ret = mptcp_pm_parse_addr(attr, info, false, &addr); 1423 if (ret < 0) 1424 return ret; 1425 1426 /* the zero id address is special: the first address used by the msk 1427 * always gets such an id, so different subflows can have different zero 1428 * id addresses. Additionally zero id is not accounted for in id_bitmap. 1429 * Let's use an 'mptcp_rm_list' instead of the common remove code. 1430 */ 1431 if (addr.addr.id == 0) 1432 return mptcp_nl_remove_id_zero_address(sock_net(skb->sk), &addr.addr); 1433 1434 spin_lock_bh(&pernet->lock); 1435 entry = __lookup_addr_by_id(pernet, addr.addr.id); 1436 if (!entry) { 1437 GENL_SET_ERR_MSG(info, "address not found"); 1438 spin_unlock_bh(&pernet->lock); 1439 return -EINVAL; 1440 } 1441 if (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) { 1442 addr_max = pernet->add_addr_signal_max; 1443 WRITE_ONCE(pernet->add_addr_signal_max, addr_max - 1); 1444 } 1445 if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) { 1446 addr_max = pernet->local_addr_max; 1447 WRITE_ONCE(pernet->local_addr_max, addr_max - 1); 1448 } 1449 1450 pernet->addrs--; 1451 list_del_rcu(&entry->list); 1452 __clear_bit(entry->addr.id, pernet->id_bitmap); 1453 spin_unlock_bh(&pernet->lock); 1454 1455 mptcp_nl_remove_subflow_and_signal_addr(sock_net(skb->sk), &entry->addr); 1456 synchronize_rcu(); 1457 __mptcp_pm_release_addr_entry(entry); 1458 1459 return ret; 1460 } 1461 1462 static void mptcp_pm_remove_addrs_and_subflows(struct mptcp_sock *msk, 1463 struct list_head *rm_list) 1464 { 1465 struct mptcp_rm_list alist = { .nr = 0 }, slist = { .nr = 0 }; 1466 struct mptcp_pm_addr_entry *entry; 1467 1468 list_for_each_entry(entry, rm_list, list) { 1469 if (lookup_subflow_by_saddr(&msk->conn_list, &entry->addr) && 1470 alist.nr < MPTCP_RM_IDS_MAX && 1471 slist.nr < MPTCP_RM_IDS_MAX) { 1472 alist.ids[alist.nr++] = entry->addr.id; 1473 slist.ids[slist.nr++] = entry->addr.id; 1474 } else if (remove_anno_list_by_saddr(msk, &entry->addr) && 1475 alist.nr < MPTCP_RM_IDS_MAX) { 1476 alist.ids[alist.nr++] = entry->addr.id; 1477 } 1478 } 1479 1480 if (alist.nr) { 1481 spin_lock_bh(&msk->pm.lock); 1482 mptcp_pm_remove_addr(msk, &alist); 1483 spin_unlock_bh(&msk->pm.lock); 1484 } 1485 if (slist.nr) 1486 mptcp_pm_remove_subflow(msk, &slist); 1487 } 1488 1489 static void mptcp_nl_remove_addrs_list(struct net *net, 1490 struct list_head *rm_list) 1491 { 1492 long s_slot = 0, s_num = 0; 1493 struct mptcp_sock *msk; 1494 1495 if (list_empty(rm_list)) 1496 return; 1497 1498 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { 1499 struct sock *sk = (struct sock *)msk; 1500 1501 lock_sock(sk); 1502 mptcp_pm_remove_addrs_and_subflows(msk, rm_list); 1503 release_sock(sk); 1504 1505 sock_put(sk); 1506 cond_resched(); 1507 } 1508 } 1509 1510 /* caller must ensure the RCU grace period is already elapsed */ 1511 static void __flush_addrs(struct list_head *list) 1512 { 1513 while (!list_empty(list)) { 1514 struct mptcp_pm_addr_entry *cur; 1515 1516 cur = list_entry(list->next, 1517 struct mptcp_pm_addr_entry, list); 1518 list_del_rcu(&cur->list); 1519 __mptcp_pm_release_addr_entry(cur); 1520 } 1521 } 1522 1523 static void __reset_counters(struct pm_nl_pernet *pernet) 1524 { 1525 WRITE_ONCE(pernet->add_addr_signal_max, 0); 1526 WRITE_ONCE(pernet->add_addr_accept_max, 0); 1527 WRITE_ONCE(pernet->local_addr_max, 0); 1528 pernet->addrs = 0; 1529 } 1530 1531 static int mptcp_nl_cmd_flush_addrs(struct sk_buff *skb, struct genl_info *info) 1532 { 1533 struct pm_nl_pernet *pernet = genl_info_pm_nl(info); 1534 LIST_HEAD(free_list); 1535 1536 spin_lock_bh(&pernet->lock); 1537 list_splice_init(&pernet->local_addr_list, &free_list); 1538 __reset_counters(pernet); 1539 pernet->next_id = 1; 1540 bitmap_zero(pernet->id_bitmap, MPTCP_PM_MAX_ADDR_ID + 1); 1541 spin_unlock_bh(&pernet->lock); 1542 mptcp_nl_remove_addrs_list(sock_net(skb->sk), &free_list); 1543 synchronize_rcu(); 1544 __flush_addrs(&free_list); 1545 return 0; 1546 } 1547 1548 static int mptcp_nl_fill_addr(struct sk_buff *skb, 1549 struct mptcp_pm_addr_entry *entry) 1550 { 1551 struct mptcp_addr_info *addr = &entry->addr; 1552 struct nlattr *attr; 1553 1554 attr = nla_nest_start(skb, MPTCP_PM_ATTR_ADDR); 1555 if (!attr) 1556 return -EMSGSIZE; 1557 1558 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_FAMILY, addr->family)) 1559 goto nla_put_failure; 1560 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_PORT, ntohs(addr->port))) 1561 goto nla_put_failure; 1562 if (nla_put_u8(skb, MPTCP_PM_ADDR_ATTR_ID, addr->id)) 1563 goto nla_put_failure; 1564 if (nla_put_u32(skb, MPTCP_PM_ADDR_ATTR_FLAGS, entry->flags)) 1565 goto nla_put_failure; 1566 if (entry->ifindex && 1567 nla_put_s32(skb, MPTCP_PM_ADDR_ATTR_IF_IDX, entry->ifindex)) 1568 goto nla_put_failure; 1569 1570 if (addr->family == AF_INET && 1571 nla_put_in_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR4, 1572 addr->addr.s_addr)) 1573 goto nla_put_failure; 1574 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1575 else if (addr->family == AF_INET6 && 1576 nla_put_in6_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR6, &addr->addr6)) 1577 goto nla_put_failure; 1578 #endif 1579 nla_nest_end(skb, attr); 1580 return 0; 1581 1582 nla_put_failure: 1583 nla_nest_cancel(skb, attr); 1584 return -EMSGSIZE; 1585 } 1586 1587 static int mptcp_nl_cmd_get_addr(struct sk_buff *skb, struct genl_info *info) 1588 { 1589 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR]; 1590 struct pm_nl_pernet *pernet = genl_info_pm_nl(info); 1591 struct mptcp_pm_addr_entry addr, *entry; 1592 struct sk_buff *msg; 1593 void *reply; 1594 int ret; 1595 1596 ret = mptcp_pm_parse_addr(attr, info, false, &addr); 1597 if (ret < 0) 1598 return ret; 1599 1600 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1601 if (!msg) 1602 return -ENOMEM; 1603 1604 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0, 1605 info->genlhdr->cmd); 1606 if (!reply) { 1607 GENL_SET_ERR_MSG(info, "not enough space in Netlink message"); 1608 ret = -EMSGSIZE; 1609 goto fail; 1610 } 1611 1612 spin_lock_bh(&pernet->lock); 1613 entry = __lookup_addr_by_id(pernet, addr.addr.id); 1614 if (!entry) { 1615 GENL_SET_ERR_MSG(info, "address not found"); 1616 ret = -EINVAL; 1617 goto unlock_fail; 1618 } 1619 1620 ret = mptcp_nl_fill_addr(msg, entry); 1621 if (ret) 1622 goto unlock_fail; 1623 1624 genlmsg_end(msg, reply); 1625 ret = genlmsg_reply(msg, info); 1626 spin_unlock_bh(&pernet->lock); 1627 return ret; 1628 1629 unlock_fail: 1630 spin_unlock_bh(&pernet->lock); 1631 1632 fail: 1633 nlmsg_free(msg); 1634 return ret; 1635 } 1636 1637 static int mptcp_nl_cmd_dump_addrs(struct sk_buff *msg, 1638 struct netlink_callback *cb) 1639 { 1640 struct net *net = sock_net(msg->sk); 1641 struct mptcp_pm_addr_entry *entry; 1642 struct pm_nl_pernet *pernet; 1643 int id = cb->args[0]; 1644 void *hdr; 1645 int i; 1646 1647 pernet = net_generic(net, pm_nl_pernet_id); 1648 1649 spin_lock_bh(&pernet->lock); 1650 for (i = id; i < MPTCP_PM_MAX_ADDR_ID + 1; i++) { 1651 if (test_bit(i, pernet->id_bitmap)) { 1652 entry = __lookup_addr_by_id(pernet, i); 1653 if (!entry) 1654 break; 1655 1656 if (entry->addr.id <= id) 1657 continue; 1658 1659 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, 1660 cb->nlh->nlmsg_seq, &mptcp_genl_family, 1661 NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR); 1662 if (!hdr) 1663 break; 1664 1665 if (mptcp_nl_fill_addr(msg, entry) < 0) { 1666 genlmsg_cancel(msg, hdr); 1667 break; 1668 } 1669 1670 id = entry->addr.id; 1671 genlmsg_end(msg, hdr); 1672 } 1673 } 1674 spin_unlock_bh(&pernet->lock); 1675 1676 cb->args[0] = id; 1677 return msg->len; 1678 } 1679 1680 static int parse_limit(struct genl_info *info, int id, unsigned int *limit) 1681 { 1682 struct nlattr *attr = info->attrs[id]; 1683 1684 if (!attr) 1685 return 0; 1686 1687 *limit = nla_get_u32(attr); 1688 if (*limit > MPTCP_PM_ADDR_MAX) { 1689 GENL_SET_ERR_MSG(info, "limit greater than maximum"); 1690 return -EINVAL; 1691 } 1692 return 0; 1693 } 1694 1695 static int 1696 mptcp_nl_cmd_set_limits(struct sk_buff *skb, struct genl_info *info) 1697 { 1698 struct pm_nl_pernet *pernet = genl_info_pm_nl(info); 1699 unsigned int rcv_addrs, subflows; 1700 int ret; 1701 1702 spin_lock_bh(&pernet->lock); 1703 rcv_addrs = pernet->add_addr_accept_max; 1704 ret = parse_limit(info, MPTCP_PM_ATTR_RCV_ADD_ADDRS, &rcv_addrs); 1705 if (ret) 1706 goto unlock; 1707 1708 subflows = pernet->subflows_max; 1709 ret = parse_limit(info, MPTCP_PM_ATTR_SUBFLOWS, &subflows); 1710 if (ret) 1711 goto unlock; 1712 1713 WRITE_ONCE(pernet->add_addr_accept_max, rcv_addrs); 1714 WRITE_ONCE(pernet->subflows_max, subflows); 1715 1716 unlock: 1717 spin_unlock_bh(&pernet->lock); 1718 return ret; 1719 } 1720 1721 static int 1722 mptcp_nl_cmd_get_limits(struct sk_buff *skb, struct genl_info *info) 1723 { 1724 struct pm_nl_pernet *pernet = genl_info_pm_nl(info); 1725 struct sk_buff *msg; 1726 void *reply; 1727 1728 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1729 if (!msg) 1730 return -ENOMEM; 1731 1732 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0, 1733 MPTCP_PM_CMD_GET_LIMITS); 1734 if (!reply) 1735 goto fail; 1736 1737 if (nla_put_u32(msg, MPTCP_PM_ATTR_RCV_ADD_ADDRS, 1738 READ_ONCE(pernet->add_addr_accept_max))) 1739 goto fail; 1740 1741 if (nla_put_u32(msg, MPTCP_PM_ATTR_SUBFLOWS, 1742 READ_ONCE(pernet->subflows_max))) 1743 goto fail; 1744 1745 genlmsg_end(msg, reply); 1746 return genlmsg_reply(msg, info); 1747 1748 fail: 1749 GENL_SET_ERR_MSG(info, "not enough space in Netlink message"); 1750 nlmsg_free(msg); 1751 return -EMSGSIZE; 1752 } 1753 1754 static int mptcp_nl_addr_backup(struct net *net, 1755 struct mptcp_addr_info *addr, 1756 u8 bkup) 1757 { 1758 long s_slot = 0, s_num = 0; 1759 struct mptcp_sock *msk; 1760 int ret = -EINVAL; 1761 1762 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { 1763 struct sock *sk = (struct sock *)msk; 1764 1765 if (list_empty(&msk->conn_list)) 1766 goto next; 1767 1768 lock_sock(sk); 1769 spin_lock_bh(&msk->pm.lock); 1770 ret = mptcp_pm_nl_mp_prio_send_ack(msk, addr, bkup); 1771 spin_unlock_bh(&msk->pm.lock); 1772 release_sock(sk); 1773 1774 next: 1775 sock_put(sk); 1776 cond_resched(); 1777 } 1778 1779 return ret; 1780 } 1781 1782 static int mptcp_nl_cmd_set_flags(struct sk_buff *skb, struct genl_info *info) 1783 { 1784 struct mptcp_pm_addr_entry addr = { .addr = { .family = AF_UNSPEC }, }, *entry; 1785 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR]; 1786 struct pm_nl_pernet *pernet = genl_info_pm_nl(info); 1787 struct net *net = sock_net(skb->sk); 1788 u8 bkup = 0, lookup_by_id = 0; 1789 int ret; 1790 1791 ret = mptcp_pm_parse_addr(attr, info, false, &addr); 1792 if (ret < 0) 1793 return ret; 1794 1795 if (addr.flags & MPTCP_PM_ADDR_FLAG_BACKUP) 1796 bkup = 1; 1797 if (addr.addr.family == AF_UNSPEC) { 1798 lookup_by_id = 1; 1799 if (!addr.addr.id) 1800 return -EOPNOTSUPP; 1801 } 1802 1803 spin_lock_bh(&pernet->lock); 1804 entry = __lookup_addr(pernet, &addr.addr, lookup_by_id); 1805 if (!entry) { 1806 spin_unlock_bh(&pernet->lock); 1807 return -EINVAL; 1808 } 1809 1810 if (bkup) 1811 entry->flags |= MPTCP_PM_ADDR_FLAG_BACKUP; 1812 else 1813 entry->flags &= ~MPTCP_PM_ADDR_FLAG_BACKUP; 1814 addr = *entry; 1815 spin_unlock_bh(&pernet->lock); 1816 1817 mptcp_nl_addr_backup(net, &addr.addr, bkup); 1818 return 0; 1819 } 1820 1821 static void mptcp_nl_mcast_send(struct net *net, struct sk_buff *nlskb, gfp_t gfp) 1822 { 1823 genlmsg_multicast_netns(&mptcp_genl_family, net, 1824 nlskb, 0, MPTCP_PM_EV_GRP_OFFSET, gfp); 1825 } 1826 1827 static int mptcp_event_add_subflow(struct sk_buff *skb, const struct sock *ssk) 1828 { 1829 const struct inet_sock *issk = inet_sk(ssk); 1830 const struct mptcp_subflow_context *sf; 1831 1832 if (nla_put_u16(skb, MPTCP_ATTR_FAMILY, ssk->sk_family)) 1833 return -EMSGSIZE; 1834 1835 switch (ssk->sk_family) { 1836 case AF_INET: 1837 if (nla_put_in_addr(skb, MPTCP_ATTR_SADDR4, issk->inet_saddr)) 1838 return -EMSGSIZE; 1839 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, issk->inet_daddr)) 1840 return -EMSGSIZE; 1841 break; 1842 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1843 case AF_INET6: { 1844 const struct ipv6_pinfo *np = inet6_sk(ssk); 1845 1846 if (nla_put_in6_addr(skb, MPTCP_ATTR_SADDR6, &np->saddr)) 1847 return -EMSGSIZE; 1848 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &ssk->sk_v6_daddr)) 1849 return -EMSGSIZE; 1850 break; 1851 } 1852 #endif 1853 default: 1854 WARN_ON_ONCE(1); 1855 return -EMSGSIZE; 1856 } 1857 1858 if (nla_put_be16(skb, MPTCP_ATTR_SPORT, issk->inet_sport)) 1859 return -EMSGSIZE; 1860 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, issk->inet_dport)) 1861 return -EMSGSIZE; 1862 1863 sf = mptcp_subflow_ctx(ssk); 1864 if (WARN_ON_ONCE(!sf)) 1865 return -EINVAL; 1866 1867 if (nla_put_u8(skb, MPTCP_ATTR_LOC_ID, sf->local_id)) 1868 return -EMSGSIZE; 1869 1870 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, sf->remote_id)) 1871 return -EMSGSIZE; 1872 1873 return 0; 1874 } 1875 1876 static int mptcp_event_put_token_and_ssk(struct sk_buff *skb, 1877 const struct mptcp_sock *msk, 1878 const struct sock *ssk) 1879 { 1880 const struct sock *sk = (const struct sock *)msk; 1881 const struct mptcp_subflow_context *sf; 1882 u8 sk_err; 1883 1884 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token)) 1885 return -EMSGSIZE; 1886 1887 if (mptcp_event_add_subflow(skb, ssk)) 1888 return -EMSGSIZE; 1889 1890 sf = mptcp_subflow_ctx(ssk); 1891 if (WARN_ON_ONCE(!sf)) 1892 return -EINVAL; 1893 1894 if (nla_put_u8(skb, MPTCP_ATTR_BACKUP, sf->backup)) 1895 return -EMSGSIZE; 1896 1897 if (ssk->sk_bound_dev_if && 1898 nla_put_s32(skb, MPTCP_ATTR_IF_IDX, ssk->sk_bound_dev_if)) 1899 return -EMSGSIZE; 1900 1901 sk_err = ssk->sk_err; 1902 if (sk_err && sk->sk_state == TCP_ESTABLISHED && 1903 nla_put_u8(skb, MPTCP_ATTR_ERROR, sk_err)) 1904 return -EMSGSIZE; 1905 1906 return 0; 1907 } 1908 1909 static int mptcp_event_sub_established(struct sk_buff *skb, 1910 const struct mptcp_sock *msk, 1911 const struct sock *ssk) 1912 { 1913 return mptcp_event_put_token_and_ssk(skb, msk, ssk); 1914 } 1915 1916 static int mptcp_event_sub_closed(struct sk_buff *skb, 1917 const struct mptcp_sock *msk, 1918 const struct sock *ssk) 1919 { 1920 const struct mptcp_subflow_context *sf; 1921 1922 if (mptcp_event_put_token_and_ssk(skb, msk, ssk)) 1923 return -EMSGSIZE; 1924 1925 sf = mptcp_subflow_ctx(ssk); 1926 if (!sf->reset_seen) 1927 return 0; 1928 1929 if (nla_put_u32(skb, MPTCP_ATTR_RESET_REASON, sf->reset_reason)) 1930 return -EMSGSIZE; 1931 1932 if (nla_put_u32(skb, MPTCP_ATTR_RESET_FLAGS, sf->reset_transient)) 1933 return -EMSGSIZE; 1934 1935 return 0; 1936 } 1937 1938 static int mptcp_event_created(struct sk_buff *skb, 1939 const struct mptcp_sock *msk, 1940 const struct sock *ssk) 1941 { 1942 int err = nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token); 1943 1944 if (err) 1945 return err; 1946 1947 return mptcp_event_add_subflow(skb, ssk); 1948 } 1949 1950 void mptcp_event_addr_removed(const struct mptcp_sock *msk, uint8_t id) 1951 { 1952 struct net *net = sock_net((const struct sock *)msk); 1953 struct nlmsghdr *nlh; 1954 struct sk_buff *skb; 1955 1956 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET)) 1957 return; 1958 1959 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); 1960 if (!skb) 1961 return; 1962 1963 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, MPTCP_EVENT_REMOVED); 1964 if (!nlh) 1965 goto nla_put_failure; 1966 1967 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token)) 1968 goto nla_put_failure; 1969 1970 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, id)) 1971 goto nla_put_failure; 1972 1973 genlmsg_end(skb, nlh); 1974 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC); 1975 return; 1976 1977 nla_put_failure: 1978 kfree_skb(skb); 1979 } 1980 1981 void mptcp_event_addr_announced(const struct mptcp_sock *msk, 1982 const struct mptcp_addr_info *info) 1983 { 1984 struct net *net = sock_net((const struct sock *)msk); 1985 struct nlmsghdr *nlh; 1986 struct sk_buff *skb; 1987 1988 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET)) 1989 return; 1990 1991 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); 1992 if (!skb) 1993 return; 1994 1995 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, 1996 MPTCP_EVENT_ANNOUNCED); 1997 if (!nlh) 1998 goto nla_put_failure; 1999 2000 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token)) 2001 goto nla_put_failure; 2002 2003 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, info->id)) 2004 goto nla_put_failure; 2005 2006 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, info->port)) 2007 goto nla_put_failure; 2008 2009 switch (info->family) { 2010 case AF_INET: 2011 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, info->addr.s_addr)) 2012 goto nla_put_failure; 2013 break; 2014 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2015 case AF_INET6: 2016 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &info->addr6)) 2017 goto nla_put_failure; 2018 break; 2019 #endif 2020 default: 2021 WARN_ON_ONCE(1); 2022 goto nla_put_failure; 2023 } 2024 2025 genlmsg_end(skb, nlh); 2026 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC); 2027 return; 2028 2029 nla_put_failure: 2030 kfree_skb(skb); 2031 } 2032 2033 void mptcp_event(enum mptcp_event_type type, const struct mptcp_sock *msk, 2034 const struct sock *ssk, gfp_t gfp) 2035 { 2036 struct net *net = sock_net((const struct sock *)msk); 2037 struct nlmsghdr *nlh; 2038 struct sk_buff *skb; 2039 2040 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET)) 2041 return; 2042 2043 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp); 2044 if (!skb) 2045 return; 2046 2047 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, type); 2048 if (!nlh) 2049 goto nla_put_failure; 2050 2051 switch (type) { 2052 case MPTCP_EVENT_UNSPEC: 2053 WARN_ON_ONCE(1); 2054 break; 2055 case MPTCP_EVENT_CREATED: 2056 case MPTCP_EVENT_ESTABLISHED: 2057 if (mptcp_event_created(skb, msk, ssk) < 0) 2058 goto nla_put_failure; 2059 break; 2060 case MPTCP_EVENT_CLOSED: 2061 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token) < 0) 2062 goto nla_put_failure; 2063 break; 2064 case MPTCP_EVENT_ANNOUNCED: 2065 case MPTCP_EVENT_REMOVED: 2066 /* call mptcp_event_addr_announced()/removed instead */ 2067 WARN_ON_ONCE(1); 2068 break; 2069 case MPTCP_EVENT_SUB_ESTABLISHED: 2070 case MPTCP_EVENT_SUB_PRIORITY: 2071 if (mptcp_event_sub_established(skb, msk, ssk) < 0) 2072 goto nla_put_failure; 2073 break; 2074 case MPTCP_EVENT_SUB_CLOSED: 2075 if (mptcp_event_sub_closed(skb, msk, ssk) < 0) 2076 goto nla_put_failure; 2077 break; 2078 } 2079 2080 genlmsg_end(skb, nlh); 2081 mptcp_nl_mcast_send(net, skb, gfp); 2082 return; 2083 2084 nla_put_failure: 2085 kfree_skb(skb); 2086 } 2087 2088 static const struct genl_small_ops mptcp_pm_ops[] = { 2089 { 2090 .cmd = MPTCP_PM_CMD_ADD_ADDR, 2091 .doit = mptcp_nl_cmd_add_addr, 2092 .flags = GENL_ADMIN_PERM, 2093 }, 2094 { 2095 .cmd = MPTCP_PM_CMD_DEL_ADDR, 2096 .doit = mptcp_nl_cmd_del_addr, 2097 .flags = GENL_ADMIN_PERM, 2098 }, 2099 { 2100 .cmd = MPTCP_PM_CMD_FLUSH_ADDRS, 2101 .doit = mptcp_nl_cmd_flush_addrs, 2102 .flags = GENL_ADMIN_PERM, 2103 }, 2104 { 2105 .cmd = MPTCP_PM_CMD_GET_ADDR, 2106 .doit = mptcp_nl_cmd_get_addr, 2107 .dumpit = mptcp_nl_cmd_dump_addrs, 2108 }, 2109 { 2110 .cmd = MPTCP_PM_CMD_SET_LIMITS, 2111 .doit = mptcp_nl_cmd_set_limits, 2112 .flags = GENL_ADMIN_PERM, 2113 }, 2114 { 2115 .cmd = MPTCP_PM_CMD_GET_LIMITS, 2116 .doit = mptcp_nl_cmd_get_limits, 2117 }, 2118 { 2119 .cmd = MPTCP_PM_CMD_SET_FLAGS, 2120 .doit = mptcp_nl_cmd_set_flags, 2121 .flags = GENL_ADMIN_PERM, 2122 }, 2123 }; 2124 2125 static struct genl_family mptcp_genl_family __ro_after_init = { 2126 .name = MPTCP_PM_NAME, 2127 .version = MPTCP_PM_VER, 2128 .maxattr = MPTCP_PM_ATTR_MAX, 2129 .policy = mptcp_pm_policy, 2130 .netnsok = true, 2131 .module = THIS_MODULE, 2132 .small_ops = mptcp_pm_ops, 2133 .n_small_ops = ARRAY_SIZE(mptcp_pm_ops), 2134 .mcgrps = mptcp_pm_mcgrps, 2135 .n_mcgrps = ARRAY_SIZE(mptcp_pm_mcgrps), 2136 }; 2137 2138 static int __net_init pm_nl_init_net(struct net *net) 2139 { 2140 struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id); 2141 2142 INIT_LIST_HEAD_RCU(&pernet->local_addr_list); 2143 2144 /* Cit. 2 subflows ought to be enough for anybody. */ 2145 pernet->subflows_max = 2; 2146 pernet->next_id = 1; 2147 pernet->stale_loss_cnt = 4; 2148 spin_lock_init(&pernet->lock); 2149 2150 /* No need to initialize other pernet fields, the struct is zeroed at 2151 * allocation time. 2152 */ 2153 2154 return 0; 2155 } 2156 2157 static void __net_exit pm_nl_exit_net(struct list_head *net_list) 2158 { 2159 struct net *net; 2160 2161 list_for_each_entry(net, net_list, exit_list) { 2162 struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id); 2163 2164 /* net is removed from namespace list, can't race with 2165 * other modifiers, also netns core already waited for a 2166 * RCU grace period. 2167 */ 2168 __flush_addrs(&pernet->local_addr_list); 2169 } 2170 } 2171 2172 static struct pernet_operations mptcp_pm_pernet_ops = { 2173 .init = pm_nl_init_net, 2174 .exit_batch = pm_nl_exit_net, 2175 .id = &pm_nl_pernet_id, 2176 .size = sizeof(struct pm_nl_pernet), 2177 }; 2178 2179 void __init mptcp_pm_nl_init(void) 2180 { 2181 if (register_pernet_subsys(&mptcp_pm_pernet_ops) < 0) 2182 panic("Failed to register MPTCP PM pernet subsystem.\n"); 2183 2184 if (genl_register_family(&mptcp_genl_family)) 2185 panic("Failed to register MPTCP PM netlink family\n"); 2186 } 2187