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 rcu_head rcu; 31 struct socket *lsk; 32 }; 33 34 struct mptcp_pm_add_entry { 35 struct list_head list; 36 struct mptcp_addr_info addr; 37 struct timer_list add_timer; 38 struct mptcp_sock *sock; 39 u8 retrans_times; 40 }; 41 42 #define MAX_ADDR_ID 255 43 #define BITMAP_SZ DIV_ROUND_UP(MAX_ADDR_ID + 1, BITS_PER_LONG) 44 45 struct pm_nl_pernet { 46 /* protects pernet updates */ 47 spinlock_t lock; 48 struct list_head local_addr_list; 49 unsigned int addrs; 50 unsigned int add_addr_signal_max; 51 unsigned int add_addr_accept_max; 52 unsigned int local_addr_max; 53 unsigned int subflows_max; 54 unsigned int next_id; 55 unsigned long id_bitmap[BITMAP_SZ]; 56 }; 57 58 #define MPTCP_PM_ADDR_MAX 8 59 #define ADD_ADDR_RETRANS_MAX 3 60 61 static bool addresses_equal(const struct mptcp_addr_info *a, 62 struct mptcp_addr_info *b, bool use_port) 63 { 64 bool addr_equals = false; 65 66 if (a->family == b->family) { 67 if (a->family == AF_INET) 68 addr_equals = a->addr.s_addr == b->addr.s_addr; 69 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 70 else 71 addr_equals = !ipv6_addr_cmp(&a->addr6, &b->addr6); 72 } else if (a->family == AF_INET) { 73 if (ipv6_addr_v4mapped(&b->addr6)) 74 addr_equals = a->addr.s_addr == b->addr6.s6_addr32[3]; 75 } else if (b->family == AF_INET) { 76 if (ipv6_addr_v4mapped(&a->addr6)) 77 addr_equals = a->addr6.s6_addr32[3] == b->addr.s_addr; 78 #endif 79 } 80 81 if (!addr_equals) 82 return false; 83 if (!use_port) 84 return true; 85 86 return a->port == b->port; 87 } 88 89 static bool address_zero(const struct mptcp_addr_info *addr) 90 { 91 struct mptcp_addr_info zero; 92 93 memset(&zero, 0, sizeof(zero)); 94 zero.family = addr->family; 95 96 return addresses_equal(addr, &zero, true); 97 } 98 99 static void local_address(const struct sock_common *skc, 100 struct mptcp_addr_info *addr) 101 { 102 addr->family = skc->skc_family; 103 addr->port = htons(skc->skc_num); 104 if (addr->family == AF_INET) 105 addr->addr.s_addr = skc->skc_rcv_saddr; 106 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 107 else if (addr->family == AF_INET6) 108 addr->addr6 = skc->skc_v6_rcv_saddr; 109 #endif 110 } 111 112 static void remote_address(const struct sock_common *skc, 113 struct mptcp_addr_info *addr) 114 { 115 addr->family = skc->skc_family; 116 addr->port = skc->skc_dport; 117 if (addr->family == AF_INET) 118 addr->addr.s_addr = skc->skc_daddr; 119 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 120 else if (addr->family == AF_INET6) 121 addr->addr6 = skc->skc_v6_daddr; 122 #endif 123 } 124 125 static bool lookup_subflow_by_saddr(const struct list_head *list, 126 struct mptcp_addr_info *saddr) 127 { 128 struct mptcp_subflow_context *subflow; 129 struct mptcp_addr_info cur; 130 struct sock_common *skc; 131 132 list_for_each_entry(subflow, list, node) { 133 skc = (struct sock_common *)mptcp_subflow_tcp_sock(subflow); 134 135 local_address(skc, &cur); 136 if (addresses_equal(&cur, saddr, saddr->port)) 137 return true; 138 } 139 140 return false; 141 } 142 143 static bool lookup_subflow_by_daddr(const struct list_head *list, 144 struct mptcp_addr_info *daddr) 145 { 146 struct mptcp_subflow_context *subflow; 147 struct mptcp_addr_info cur; 148 struct sock_common *skc; 149 150 list_for_each_entry(subflow, list, node) { 151 skc = (struct sock_common *)mptcp_subflow_tcp_sock(subflow); 152 153 remote_address(skc, &cur); 154 if (addresses_equal(&cur, daddr, daddr->port)) 155 return true; 156 } 157 158 return false; 159 } 160 161 static struct mptcp_pm_addr_entry * 162 select_local_address(const struct pm_nl_pernet *pernet, 163 struct mptcp_sock *msk) 164 { 165 struct mptcp_pm_addr_entry *entry, *ret = NULL; 166 struct sock *sk = (struct sock *)msk; 167 168 msk_owned_by_me(msk); 169 170 rcu_read_lock(); 171 __mptcp_flush_join_list(msk); 172 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) { 173 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW)) 174 continue; 175 176 if (entry->addr.family != sk->sk_family) { 177 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 178 if ((entry->addr.family == AF_INET && 179 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) || 180 (sk->sk_family == AF_INET && 181 !ipv6_addr_v4mapped(&entry->addr.addr6))) 182 #endif 183 continue; 184 } 185 186 /* avoid any address already in use by subflows and 187 * pending join 188 */ 189 if (!lookup_subflow_by_saddr(&msk->conn_list, &entry->addr)) { 190 ret = entry; 191 break; 192 } 193 } 194 rcu_read_unlock(); 195 return ret; 196 } 197 198 static struct mptcp_pm_addr_entry * 199 select_signal_address(struct pm_nl_pernet *pernet, unsigned int pos) 200 { 201 struct mptcp_pm_addr_entry *entry, *ret = NULL; 202 int i = 0; 203 204 rcu_read_lock(); 205 /* do not keep any additional per socket state, just signal 206 * the address list in order. 207 * Note: removal from the local address list during the msk life-cycle 208 * can lead to additional addresses not being announced. 209 */ 210 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) { 211 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) 212 continue; 213 if (i++ == pos) { 214 ret = entry; 215 break; 216 } 217 } 218 rcu_read_unlock(); 219 return ret; 220 } 221 222 unsigned int mptcp_pm_get_add_addr_signal_max(struct mptcp_sock *msk) 223 { 224 struct pm_nl_pernet *pernet; 225 226 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id); 227 return READ_ONCE(pernet->add_addr_signal_max); 228 } 229 EXPORT_SYMBOL_GPL(mptcp_pm_get_add_addr_signal_max); 230 231 unsigned int mptcp_pm_get_add_addr_accept_max(struct mptcp_sock *msk) 232 { 233 struct pm_nl_pernet *pernet; 234 235 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id); 236 return READ_ONCE(pernet->add_addr_accept_max); 237 } 238 EXPORT_SYMBOL_GPL(mptcp_pm_get_add_addr_accept_max); 239 240 unsigned int mptcp_pm_get_subflows_max(struct mptcp_sock *msk) 241 { 242 struct pm_nl_pernet *pernet; 243 244 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id); 245 return READ_ONCE(pernet->subflows_max); 246 } 247 EXPORT_SYMBOL_GPL(mptcp_pm_get_subflows_max); 248 249 unsigned int mptcp_pm_get_local_addr_max(struct mptcp_sock *msk) 250 { 251 struct pm_nl_pernet *pernet; 252 253 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id); 254 return READ_ONCE(pernet->local_addr_max); 255 } 256 EXPORT_SYMBOL_GPL(mptcp_pm_get_local_addr_max); 257 258 static void check_work_pending(struct mptcp_sock *msk) 259 { 260 if (msk->pm.add_addr_signaled == mptcp_pm_get_add_addr_signal_max(msk) && 261 (msk->pm.local_addr_used == mptcp_pm_get_local_addr_max(msk) || 262 msk->pm.subflows == mptcp_pm_get_subflows_max(msk))) 263 WRITE_ONCE(msk->pm.work_pending, false); 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(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(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 void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk) 414 { 415 struct sock *sk = (struct sock *)msk; 416 struct mptcp_pm_addr_entry *local; 417 unsigned int add_addr_signal_max; 418 unsigned int local_addr_max; 419 struct pm_nl_pernet *pernet; 420 unsigned int subflows_max; 421 422 pernet = net_generic(sock_net(sk), pm_nl_pernet_id); 423 424 add_addr_signal_max = mptcp_pm_get_add_addr_signal_max(msk); 425 local_addr_max = mptcp_pm_get_local_addr_max(msk); 426 subflows_max = mptcp_pm_get_subflows_max(msk); 427 428 pr_debug("local %d:%d signal %d:%d subflows %d:%d\n", 429 msk->pm.local_addr_used, local_addr_max, 430 msk->pm.add_addr_signaled, add_addr_signal_max, 431 msk->pm.subflows, subflows_max); 432 433 /* check first for announce */ 434 if (msk->pm.add_addr_signaled < add_addr_signal_max) { 435 local = select_signal_address(pernet, 436 msk->pm.add_addr_signaled); 437 438 if (local) { 439 if (mptcp_pm_alloc_anno_list(msk, local)) { 440 msk->pm.add_addr_signaled++; 441 mptcp_pm_announce_addr(msk, &local->addr, false); 442 mptcp_pm_nl_addr_send_ack(msk); 443 } 444 } else { 445 /* pick failed, avoid fourther attempts later */ 446 msk->pm.local_addr_used = add_addr_signal_max; 447 } 448 449 check_work_pending(msk); 450 } 451 452 /* check if should create a new subflow */ 453 if (msk->pm.local_addr_used < local_addr_max && 454 msk->pm.subflows < subflows_max && 455 !READ_ONCE(msk->pm.remote_deny_join_id0)) { 456 local = select_local_address(pernet, msk); 457 if (local) { 458 struct mptcp_addr_info remote = { 0 }; 459 460 msk->pm.local_addr_used++; 461 msk->pm.subflows++; 462 check_work_pending(msk); 463 remote_address((struct sock_common *)sk, &remote); 464 spin_unlock_bh(&msk->pm.lock); 465 __mptcp_subflow_connect(sk, &local->addr, &remote, 466 local->flags, local->ifindex); 467 spin_lock_bh(&msk->pm.lock); 468 return; 469 } 470 471 /* lookup failed, avoid fourther attempts later */ 472 msk->pm.local_addr_used = local_addr_max; 473 check_work_pending(msk); 474 } 475 } 476 477 static void mptcp_pm_nl_fully_established(struct mptcp_sock *msk) 478 { 479 mptcp_pm_create_subflow_or_signal_addr(msk); 480 } 481 482 static void mptcp_pm_nl_subflow_established(struct mptcp_sock *msk) 483 { 484 mptcp_pm_create_subflow_or_signal_addr(msk); 485 } 486 487 static void mptcp_pm_nl_add_addr_received(struct mptcp_sock *msk) 488 { 489 struct sock *sk = (struct sock *)msk; 490 unsigned int add_addr_accept_max; 491 struct mptcp_addr_info remote; 492 struct mptcp_addr_info local; 493 unsigned int subflows_max; 494 495 add_addr_accept_max = mptcp_pm_get_add_addr_accept_max(msk); 496 subflows_max = mptcp_pm_get_subflows_max(msk); 497 498 pr_debug("accepted %d:%d remote family %d", 499 msk->pm.add_addr_accepted, add_addr_accept_max, 500 msk->pm.remote.family); 501 502 if (lookup_subflow_by_daddr(&msk->conn_list, &msk->pm.remote)) 503 goto add_addr_echo; 504 505 msk->pm.add_addr_accepted++; 506 msk->pm.subflows++; 507 if (msk->pm.add_addr_accepted >= add_addr_accept_max || 508 msk->pm.subflows >= subflows_max) 509 WRITE_ONCE(msk->pm.accept_addr, false); 510 511 /* connect to the specified remote address, using whatever 512 * local address the routing configuration will pick. 513 */ 514 remote = msk->pm.remote; 515 if (!remote.port) 516 remote.port = sk->sk_dport; 517 memset(&local, 0, sizeof(local)); 518 local.family = remote.family; 519 520 spin_unlock_bh(&msk->pm.lock); 521 __mptcp_subflow_connect(sk, &local, &remote, 0, 0); 522 spin_lock_bh(&msk->pm.lock); 523 524 add_addr_echo: 525 mptcp_pm_announce_addr(msk, &msk->pm.remote, true); 526 mptcp_pm_nl_addr_send_ack(msk); 527 } 528 529 void mptcp_pm_nl_addr_send_ack(struct mptcp_sock *msk) 530 { 531 struct mptcp_subflow_context *subflow; 532 533 msk_owned_by_me(msk); 534 lockdep_assert_held(&msk->pm.lock); 535 536 if (!mptcp_pm_should_add_signal(msk) && 537 !mptcp_pm_should_rm_signal(msk)) 538 return; 539 540 __mptcp_flush_join_list(msk); 541 subflow = list_first_entry_or_null(&msk->conn_list, typeof(*subflow), node); 542 if (subflow) { 543 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 544 bool slow; 545 546 spin_unlock_bh(&msk->pm.lock); 547 pr_debug("send ack for %s%s%s", 548 mptcp_pm_should_add_signal(msk) ? "add_addr" : "rm_addr", 549 mptcp_pm_should_add_signal_ipv6(msk) ? " [ipv6]" : "", 550 mptcp_pm_should_add_signal_port(msk) ? " [port]" : ""); 551 552 slow = lock_sock_fast(ssk); 553 tcp_send_ack(ssk); 554 unlock_sock_fast(ssk, slow); 555 spin_lock_bh(&msk->pm.lock); 556 } 557 } 558 559 int mptcp_pm_nl_mp_prio_send_ack(struct mptcp_sock *msk, 560 struct mptcp_addr_info *addr, 561 u8 bkup) 562 { 563 struct mptcp_subflow_context *subflow; 564 565 pr_debug("bkup=%d", bkup); 566 567 mptcp_for_each_subflow(msk, subflow) { 568 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 569 struct sock *sk = (struct sock *)msk; 570 struct mptcp_addr_info local; 571 bool slow; 572 573 local_address((struct sock_common *)ssk, &local); 574 if (!addresses_equal(&local, addr, addr->port)) 575 continue; 576 577 subflow->backup = bkup; 578 subflow->send_mp_prio = 1; 579 subflow->request_bkup = bkup; 580 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIOTX); 581 582 spin_unlock_bh(&msk->pm.lock); 583 pr_debug("send ack for mp_prio"); 584 slow = lock_sock_fast(ssk); 585 tcp_send_ack(ssk); 586 unlock_sock_fast(ssk, slow); 587 spin_lock_bh(&msk->pm.lock); 588 589 return 0; 590 } 591 592 return -EINVAL; 593 } 594 595 static void mptcp_pm_nl_rm_addr_or_subflow(struct mptcp_sock *msk, 596 const struct mptcp_rm_list *rm_list, 597 enum linux_mptcp_mib_field rm_type) 598 { 599 struct mptcp_subflow_context *subflow, *tmp; 600 struct sock *sk = (struct sock *)msk; 601 u8 i; 602 603 pr_debug("%s rm_list_nr %d", 604 rm_type == MPTCP_MIB_RMADDR ? "address" : "subflow", rm_list->nr); 605 606 msk_owned_by_me(msk); 607 608 if (!rm_list->nr) 609 return; 610 611 if (list_empty(&msk->conn_list)) 612 return; 613 614 for (i = 0; i < rm_list->nr; i++) { 615 list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) { 616 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 617 int how = RCV_SHUTDOWN | SEND_SHUTDOWN; 618 u8 id = subflow->local_id; 619 620 if (rm_type == MPTCP_MIB_RMADDR) 621 id = subflow->remote_id; 622 623 if (rm_list->ids[i] != id) 624 continue; 625 626 pr_debug(" -> %s rm_list_ids[%d]=%u local_id=%u remote_id=%u", 627 rm_type == MPTCP_MIB_RMADDR ? "address" : "subflow", 628 i, rm_list->ids[i], subflow->local_id, subflow->remote_id); 629 spin_unlock_bh(&msk->pm.lock); 630 mptcp_subflow_shutdown(sk, ssk, how); 631 mptcp_close_ssk(sk, ssk, subflow); 632 spin_lock_bh(&msk->pm.lock); 633 634 if (rm_type == MPTCP_MIB_RMADDR) { 635 msk->pm.add_addr_accepted--; 636 WRITE_ONCE(msk->pm.accept_addr, true); 637 } else if (rm_type == MPTCP_MIB_RMSUBFLOW) { 638 msk->pm.local_addr_used--; 639 } 640 msk->pm.subflows--; 641 __MPTCP_INC_STATS(sock_net(sk), rm_type); 642 } 643 } 644 } 645 646 static void mptcp_pm_nl_rm_addr_received(struct mptcp_sock *msk) 647 { 648 mptcp_pm_nl_rm_addr_or_subflow(msk, &msk->pm.rm_list_rx, MPTCP_MIB_RMADDR); 649 } 650 651 void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk, 652 const struct mptcp_rm_list *rm_list) 653 { 654 mptcp_pm_nl_rm_addr_or_subflow(msk, rm_list, MPTCP_MIB_RMSUBFLOW); 655 } 656 657 void mptcp_pm_nl_work(struct mptcp_sock *msk) 658 { 659 struct mptcp_pm_data *pm = &msk->pm; 660 661 msk_owned_by_me(msk); 662 663 spin_lock_bh(&msk->pm.lock); 664 665 pr_debug("msk=%p status=%x", msk, pm->status); 666 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_RECEIVED)) { 667 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_RECEIVED); 668 mptcp_pm_nl_add_addr_received(msk); 669 } 670 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_SEND_ACK)) { 671 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_SEND_ACK); 672 mptcp_pm_nl_addr_send_ack(msk); 673 } 674 if (pm->status & BIT(MPTCP_PM_RM_ADDR_RECEIVED)) { 675 pm->status &= ~BIT(MPTCP_PM_RM_ADDR_RECEIVED); 676 mptcp_pm_nl_rm_addr_received(msk); 677 } 678 if (pm->status & BIT(MPTCP_PM_ESTABLISHED)) { 679 pm->status &= ~BIT(MPTCP_PM_ESTABLISHED); 680 mptcp_pm_nl_fully_established(msk); 681 } 682 if (pm->status & BIT(MPTCP_PM_SUBFLOW_ESTABLISHED)) { 683 pm->status &= ~BIT(MPTCP_PM_SUBFLOW_ESTABLISHED); 684 mptcp_pm_nl_subflow_established(msk); 685 } 686 687 spin_unlock_bh(&msk->pm.lock); 688 } 689 690 static bool address_use_port(struct mptcp_pm_addr_entry *entry) 691 { 692 return (entry->flags & 693 (MPTCP_PM_ADDR_FLAG_SIGNAL | MPTCP_PM_ADDR_FLAG_SUBFLOW)) == 694 MPTCP_PM_ADDR_FLAG_SIGNAL; 695 } 696 697 static int mptcp_pm_nl_append_new_local_addr(struct pm_nl_pernet *pernet, 698 struct mptcp_pm_addr_entry *entry) 699 { 700 struct mptcp_pm_addr_entry *cur; 701 unsigned int addr_max; 702 int ret = -EINVAL; 703 704 spin_lock_bh(&pernet->lock); 705 /* to keep the code simple, don't do IDR-like allocation for address ID, 706 * just bail when we exceed limits 707 */ 708 if (pernet->next_id == MAX_ADDR_ID) 709 pernet->next_id = 1; 710 if (pernet->addrs >= MPTCP_PM_ADDR_MAX) 711 goto out; 712 if (test_bit(entry->addr.id, pernet->id_bitmap)) 713 goto out; 714 715 /* do not insert duplicate address, differentiate on port only 716 * singled addresses 717 */ 718 list_for_each_entry(cur, &pernet->local_addr_list, list) { 719 if (addresses_equal(&cur->addr, &entry->addr, 720 address_use_port(entry) && 721 address_use_port(cur))) 722 goto out; 723 } 724 725 if (!entry->addr.id) { 726 find_next: 727 entry->addr.id = find_next_zero_bit(pernet->id_bitmap, 728 MAX_ADDR_ID + 1, 729 pernet->next_id); 730 if ((!entry->addr.id || entry->addr.id > MAX_ADDR_ID) && 731 pernet->next_id != 1) { 732 pernet->next_id = 1; 733 goto find_next; 734 } 735 } 736 737 if (!entry->addr.id || entry->addr.id > MAX_ADDR_ID) 738 goto out; 739 740 __set_bit(entry->addr.id, pernet->id_bitmap); 741 if (entry->addr.id > pernet->next_id) 742 pernet->next_id = entry->addr.id; 743 744 if (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) { 745 addr_max = pernet->add_addr_signal_max; 746 WRITE_ONCE(pernet->add_addr_signal_max, addr_max + 1); 747 } 748 if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) { 749 addr_max = pernet->local_addr_max; 750 WRITE_ONCE(pernet->local_addr_max, addr_max + 1); 751 } 752 753 pernet->addrs++; 754 list_add_tail_rcu(&entry->list, &pernet->local_addr_list); 755 ret = entry->addr.id; 756 757 out: 758 spin_unlock_bh(&pernet->lock); 759 return ret; 760 } 761 762 static int mptcp_pm_nl_create_listen_socket(struct sock *sk, 763 struct mptcp_pm_addr_entry *entry) 764 { 765 struct sockaddr_storage addr; 766 struct mptcp_sock *msk; 767 struct socket *ssock; 768 int backlog = 1024; 769 int err; 770 771 err = sock_create_kern(sock_net(sk), entry->addr.family, 772 SOCK_STREAM, IPPROTO_MPTCP, &entry->lsk); 773 if (err) 774 return err; 775 776 msk = mptcp_sk(entry->lsk->sk); 777 if (!msk) { 778 err = -EINVAL; 779 goto out; 780 } 781 782 ssock = __mptcp_nmpc_socket(msk); 783 if (!ssock) { 784 err = -EINVAL; 785 goto out; 786 } 787 788 mptcp_info2sockaddr(&entry->addr, &addr, entry->addr.family); 789 err = kernel_bind(ssock, (struct sockaddr *)&addr, 790 sizeof(struct sockaddr_in)); 791 if (err) { 792 pr_warn("kernel_bind error, err=%d", err); 793 goto out; 794 } 795 796 err = kernel_listen(ssock, backlog); 797 if (err) { 798 pr_warn("kernel_listen error, err=%d", err); 799 goto out; 800 } 801 802 return 0; 803 804 out: 805 sock_release(entry->lsk); 806 return err; 807 } 808 809 int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc) 810 { 811 struct mptcp_pm_addr_entry *entry; 812 struct mptcp_addr_info skc_local; 813 struct mptcp_addr_info msk_local; 814 struct pm_nl_pernet *pernet; 815 int ret = -1; 816 817 if (WARN_ON_ONCE(!msk)) 818 return -1; 819 820 /* The 0 ID mapping is defined by the first subflow, copied into the msk 821 * addr 822 */ 823 local_address((struct sock_common *)msk, &msk_local); 824 local_address((struct sock_common *)skc, &skc_local); 825 if (addresses_equal(&msk_local, &skc_local, false)) 826 return 0; 827 828 if (address_zero(&skc_local)) 829 return 0; 830 831 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id); 832 833 rcu_read_lock(); 834 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) { 835 if (addresses_equal(&entry->addr, &skc_local, entry->addr.port)) { 836 ret = entry->addr.id; 837 break; 838 } 839 } 840 rcu_read_unlock(); 841 if (ret >= 0) 842 return ret; 843 844 /* address not found, add to local list */ 845 entry = kmalloc(sizeof(*entry), GFP_ATOMIC); 846 if (!entry) 847 return -ENOMEM; 848 849 entry->addr = skc_local; 850 entry->addr.id = 0; 851 entry->addr.port = 0; 852 entry->ifindex = 0; 853 entry->flags = 0; 854 entry->lsk = NULL; 855 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry); 856 if (ret < 0) 857 kfree(entry); 858 859 return ret; 860 } 861 862 void mptcp_pm_nl_data_init(struct mptcp_sock *msk) 863 { 864 struct mptcp_pm_data *pm = &msk->pm; 865 bool subflows; 866 867 subflows = !!mptcp_pm_get_subflows_max(msk); 868 WRITE_ONCE(pm->work_pending, (!!mptcp_pm_get_local_addr_max(msk) && subflows) || 869 !!mptcp_pm_get_add_addr_signal_max(msk)); 870 WRITE_ONCE(pm->accept_addr, !!mptcp_pm_get_add_addr_accept_max(msk) && subflows); 871 WRITE_ONCE(pm->accept_subflow, subflows); 872 } 873 874 #define MPTCP_PM_CMD_GRP_OFFSET 0 875 #define MPTCP_PM_EV_GRP_OFFSET 1 876 877 static const struct genl_multicast_group mptcp_pm_mcgrps[] = { 878 [MPTCP_PM_CMD_GRP_OFFSET] = { .name = MPTCP_PM_CMD_GRP_NAME, }, 879 [MPTCP_PM_EV_GRP_OFFSET] = { .name = MPTCP_PM_EV_GRP_NAME, 880 .flags = GENL_UNS_ADMIN_PERM, 881 }, 882 }; 883 884 static const struct nla_policy 885 mptcp_pm_addr_policy[MPTCP_PM_ADDR_ATTR_MAX + 1] = { 886 [MPTCP_PM_ADDR_ATTR_FAMILY] = { .type = NLA_U16, }, 887 [MPTCP_PM_ADDR_ATTR_ID] = { .type = NLA_U8, }, 888 [MPTCP_PM_ADDR_ATTR_ADDR4] = { .type = NLA_U32, }, 889 [MPTCP_PM_ADDR_ATTR_ADDR6] = 890 NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)), 891 [MPTCP_PM_ADDR_ATTR_PORT] = { .type = NLA_U16 }, 892 [MPTCP_PM_ADDR_ATTR_FLAGS] = { .type = NLA_U32 }, 893 [MPTCP_PM_ADDR_ATTR_IF_IDX] = { .type = NLA_S32 }, 894 }; 895 896 static const struct nla_policy mptcp_pm_policy[MPTCP_PM_ATTR_MAX + 1] = { 897 [MPTCP_PM_ATTR_ADDR] = 898 NLA_POLICY_NESTED(mptcp_pm_addr_policy), 899 [MPTCP_PM_ATTR_RCV_ADD_ADDRS] = { .type = NLA_U32, }, 900 [MPTCP_PM_ATTR_SUBFLOWS] = { .type = NLA_U32, }, 901 }; 902 903 static int mptcp_pm_family_to_addr(int family) 904 { 905 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 906 if (family == AF_INET6) 907 return MPTCP_PM_ADDR_ATTR_ADDR6; 908 #endif 909 return MPTCP_PM_ADDR_ATTR_ADDR4; 910 } 911 912 static int mptcp_pm_parse_addr(struct nlattr *attr, struct genl_info *info, 913 bool require_family, 914 struct mptcp_pm_addr_entry *entry) 915 { 916 struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1]; 917 int err, addr_addr; 918 919 if (!attr) { 920 GENL_SET_ERR_MSG(info, "missing address info"); 921 return -EINVAL; 922 } 923 924 /* no validation needed - was already done via nested policy */ 925 err = nla_parse_nested_deprecated(tb, MPTCP_PM_ADDR_ATTR_MAX, attr, 926 mptcp_pm_addr_policy, info->extack); 927 if (err) 928 return err; 929 930 memset(entry, 0, sizeof(*entry)); 931 if (!tb[MPTCP_PM_ADDR_ATTR_FAMILY]) { 932 if (!require_family) 933 goto skip_family; 934 935 NL_SET_ERR_MSG_ATTR(info->extack, attr, 936 "missing family"); 937 return -EINVAL; 938 } 939 940 entry->addr.family = nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_FAMILY]); 941 if (entry->addr.family != AF_INET 942 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 943 && entry->addr.family != AF_INET6 944 #endif 945 ) { 946 NL_SET_ERR_MSG_ATTR(info->extack, attr, 947 "unknown address family"); 948 return -EINVAL; 949 } 950 addr_addr = mptcp_pm_family_to_addr(entry->addr.family); 951 if (!tb[addr_addr]) { 952 NL_SET_ERR_MSG_ATTR(info->extack, attr, 953 "missing address data"); 954 return -EINVAL; 955 } 956 957 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 958 if (entry->addr.family == AF_INET6) 959 entry->addr.addr6 = nla_get_in6_addr(tb[addr_addr]); 960 else 961 #endif 962 entry->addr.addr.s_addr = nla_get_in_addr(tb[addr_addr]); 963 964 skip_family: 965 if (tb[MPTCP_PM_ADDR_ATTR_IF_IDX]) { 966 u32 val = nla_get_s32(tb[MPTCP_PM_ADDR_ATTR_IF_IDX]); 967 968 entry->ifindex = val; 969 } 970 971 if (tb[MPTCP_PM_ADDR_ATTR_ID]) 972 entry->addr.id = nla_get_u8(tb[MPTCP_PM_ADDR_ATTR_ID]); 973 974 if (tb[MPTCP_PM_ADDR_ATTR_FLAGS]) 975 entry->flags = nla_get_u32(tb[MPTCP_PM_ADDR_ATTR_FLAGS]); 976 977 if (tb[MPTCP_PM_ADDR_ATTR_PORT]) { 978 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) { 979 NL_SET_ERR_MSG_ATTR(info->extack, attr, 980 "flags must have signal when using port"); 981 return -EINVAL; 982 } 983 entry->addr.port = htons(nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_PORT])); 984 } 985 986 return 0; 987 } 988 989 static struct pm_nl_pernet *genl_info_pm_nl(struct genl_info *info) 990 { 991 return net_generic(genl_info_net(info), pm_nl_pernet_id); 992 } 993 994 static int mptcp_nl_add_subflow_or_signal_addr(struct net *net) 995 { 996 struct mptcp_sock *msk; 997 long s_slot = 0, s_num = 0; 998 999 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { 1000 struct sock *sk = (struct sock *)msk; 1001 1002 if (!READ_ONCE(msk->fully_established)) 1003 goto next; 1004 1005 lock_sock(sk); 1006 spin_lock_bh(&msk->pm.lock); 1007 mptcp_pm_create_subflow_or_signal_addr(msk); 1008 spin_unlock_bh(&msk->pm.lock); 1009 release_sock(sk); 1010 1011 next: 1012 sock_put(sk); 1013 cond_resched(); 1014 } 1015 1016 return 0; 1017 } 1018 1019 static int mptcp_nl_cmd_add_addr(struct sk_buff *skb, struct genl_info *info) 1020 { 1021 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR]; 1022 struct pm_nl_pernet *pernet = genl_info_pm_nl(info); 1023 struct mptcp_pm_addr_entry addr, *entry; 1024 int ret; 1025 1026 ret = mptcp_pm_parse_addr(attr, info, true, &addr); 1027 if (ret < 0) 1028 return ret; 1029 1030 entry = kmalloc(sizeof(*entry), GFP_KERNEL); 1031 if (!entry) { 1032 GENL_SET_ERR_MSG(info, "can't allocate addr"); 1033 return -ENOMEM; 1034 } 1035 1036 *entry = addr; 1037 if (entry->addr.port) { 1038 ret = mptcp_pm_nl_create_listen_socket(skb->sk, entry); 1039 if (ret) { 1040 GENL_SET_ERR_MSG(info, "create listen socket error"); 1041 kfree(entry); 1042 return ret; 1043 } 1044 } 1045 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry); 1046 if (ret < 0) { 1047 GENL_SET_ERR_MSG(info, "too many addresses or duplicate one"); 1048 if (entry->lsk) 1049 sock_release(entry->lsk); 1050 kfree(entry); 1051 return ret; 1052 } 1053 1054 mptcp_nl_add_subflow_or_signal_addr(sock_net(skb->sk)); 1055 1056 return 0; 1057 } 1058 1059 static struct mptcp_pm_addr_entry * 1060 __lookup_addr_by_id(struct pm_nl_pernet *pernet, unsigned int id) 1061 { 1062 struct mptcp_pm_addr_entry *entry; 1063 1064 list_for_each_entry(entry, &pernet->local_addr_list, list) { 1065 if (entry->addr.id == id) 1066 return entry; 1067 } 1068 return NULL; 1069 } 1070 1071 static bool remove_anno_list_by_saddr(struct mptcp_sock *msk, 1072 struct mptcp_addr_info *addr) 1073 { 1074 struct mptcp_pm_add_entry *entry; 1075 1076 entry = mptcp_pm_del_add_timer(msk, addr, false); 1077 if (entry) { 1078 list_del(&entry->list); 1079 kfree(entry); 1080 return true; 1081 } 1082 1083 return false; 1084 } 1085 1086 static bool mptcp_pm_remove_anno_addr(struct mptcp_sock *msk, 1087 struct mptcp_addr_info *addr, 1088 bool force) 1089 { 1090 struct mptcp_rm_list list = { .nr = 0 }; 1091 bool ret; 1092 1093 list.ids[list.nr++] = addr->id; 1094 1095 ret = remove_anno_list_by_saddr(msk, addr); 1096 if (ret || force) { 1097 spin_lock_bh(&msk->pm.lock); 1098 mptcp_pm_remove_addr(msk, &list); 1099 spin_unlock_bh(&msk->pm.lock); 1100 } 1101 return ret; 1102 } 1103 1104 static int mptcp_nl_remove_subflow_and_signal_addr(struct net *net, 1105 struct mptcp_addr_info *addr) 1106 { 1107 struct mptcp_sock *msk; 1108 long s_slot = 0, s_num = 0; 1109 struct mptcp_rm_list list = { .nr = 0 }; 1110 1111 pr_debug("remove_id=%d", addr->id); 1112 1113 list.ids[list.nr++] = addr->id; 1114 1115 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { 1116 struct sock *sk = (struct sock *)msk; 1117 bool remove_subflow; 1118 1119 if (list_empty(&msk->conn_list)) { 1120 mptcp_pm_remove_anno_addr(msk, addr, false); 1121 goto next; 1122 } 1123 1124 lock_sock(sk); 1125 remove_subflow = lookup_subflow_by_saddr(&msk->conn_list, addr); 1126 mptcp_pm_remove_anno_addr(msk, addr, remove_subflow); 1127 if (remove_subflow) 1128 mptcp_pm_remove_subflow(msk, &list); 1129 release_sock(sk); 1130 1131 next: 1132 sock_put(sk); 1133 cond_resched(); 1134 } 1135 1136 return 0; 1137 } 1138 1139 struct addr_entry_release_work { 1140 struct rcu_work rwork; 1141 struct mptcp_pm_addr_entry *entry; 1142 }; 1143 1144 static void mptcp_pm_release_addr_entry(struct work_struct *work) 1145 { 1146 struct addr_entry_release_work *w; 1147 struct mptcp_pm_addr_entry *entry; 1148 1149 w = container_of(to_rcu_work(work), struct addr_entry_release_work, rwork); 1150 entry = w->entry; 1151 if (entry) { 1152 if (entry->lsk) 1153 sock_release(entry->lsk); 1154 kfree(entry); 1155 } 1156 kfree(w); 1157 } 1158 1159 static void mptcp_pm_free_addr_entry(struct mptcp_pm_addr_entry *entry) 1160 { 1161 struct addr_entry_release_work *w; 1162 1163 w = kmalloc(sizeof(*w), GFP_ATOMIC); 1164 if (w) { 1165 INIT_RCU_WORK(&w->rwork, mptcp_pm_release_addr_entry); 1166 w->entry = entry; 1167 queue_rcu_work(system_wq, &w->rwork); 1168 } 1169 } 1170 1171 static int mptcp_nl_remove_id_zero_address(struct net *net, 1172 struct mptcp_addr_info *addr) 1173 { 1174 struct mptcp_rm_list list = { .nr = 0 }; 1175 long s_slot = 0, s_num = 0; 1176 struct mptcp_sock *msk; 1177 1178 list.ids[list.nr++] = 0; 1179 1180 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { 1181 struct sock *sk = (struct sock *)msk; 1182 struct mptcp_addr_info msk_local; 1183 1184 if (list_empty(&msk->conn_list)) 1185 goto next; 1186 1187 local_address((struct sock_common *)msk, &msk_local); 1188 if (!addresses_equal(&msk_local, addr, addr->port)) 1189 goto next; 1190 1191 lock_sock(sk); 1192 spin_lock_bh(&msk->pm.lock); 1193 mptcp_pm_remove_addr(msk, &list); 1194 mptcp_pm_nl_rm_subflow_received(msk, &list); 1195 spin_unlock_bh(&msk->pm.lock); 1196 release_sock(sk); 1197 1198 next: 1199 sock_put(sk); 1200 cond_resched(); 1201 } 1202 1203 return 0; 1204 } 1205 1206 static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info) 1207 { 1208 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR]; 1209 struct pm_nl_pernet *pernet = genl_info_pm_nl(info); 1210 struct mptcp_pm_addr_entry addr, *entry; 1211 unsigned int addr_max; 1212 int ret; 1213 1214 ret = mptcp_pm_parse_addr(attr, info, false, &addr); 1215 if (ret < 0) 1216 return ret; 1217 1218 /* the zero id address is special: the first address used by the msk 1219 * always gets such an id, so different subflows can have different zero 1220 * id addresses. Additionally zero id is not accounted for in id_bitmap. 1221 * Let's use an 'mptcp_rm_list' instead of the common remove code. 1222 */ 1223 if (addr.addr.id == 0) 1224 return mptcp_nl_remove_id_zero_address(sock_net(skb->sk), &addr.addr); 1225 1226 spin_lock_bh(&pernet->lock); 1227 entry = __lookup_addr_by_id(pernet, addr.addr.id); 1228 if (!entry) { 1229 GENL_SET_ERR_MSG(info, "address not found"); 1230 spin_unlock_bh(&pernet->lock); 1231 return -EINVAL; 1232 } 1233 if (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) { 1234 addr_max = pernet->add_addr_signal_max; 1235 WRITE_ONCE(pernet->add_addr_signal_max, addr_max - 1); 1236 } 1237 if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) { 1238 addr_max = pernet->local_addr_max; 1239 WRITE_ONCE(pernet->local_addr_max, addr_max - 1); 1240 } 1241 1242 pernet->addrs--; 1243 list_del_rcu(&entry->list); 1244 __clear_bit(entry->addr.id, pernet->id_bitmap); 1245 spin_unlock_bh(&pernet->lock); 1246 1247 mptcp_nl_remove_subflow_and_signal_addr(sock_net(skb->sk), &entry->addr); 1248 mptcp_pm_free_addr_entry(entry); 1249 1250 return ret; 1251 } 1252 1253 static void mptcp_pm_remove_addrs_and_subflows(struct mptcp_sock *msk, 1254 struct list_head *rm_list) 1255 { 1256 struct mptcp_rm_list alist = { .nr = 0 }, slist = { .nr = 0 }; 1257 struct mptcp_pm_addr_entry *entry; 1258 1259 list_for_each_entry(entry, rm_list, list) { 1260 if (lookup_subflow_by_saddr(&msk->conn_list, &entry->addr) && 1261 alist.nr < MPTCP_RM_IDS_MAX && 1262 slist.nr < MPTCP_RM_IDS_MAX) { 1263 alist.ids[alist.nr++] = entry->addr.id; 1264 slist.ids[slist.nr++] = entry->addr.id; 1265 } else if (remove_anno_list_by_saddr(msk, &entry->addr) && 1266 alist.nr < MPTCP_RM_IDS_MAX) { 1267 alist.ids[alist.nr++] = entry->addr.id; 1268 } 1269 } 1270 1271 if (alist.nr) { 1272 spin_lock_bh(&msk->pm.lock); 1273 mptcp_pm_remove_addr(msk, &alist); 1274 spin_unlock_bh(&msk->pm.lock); 1275 } 1276 if (slist.nr) 1277 mptcp_pm_remove_subflow(msk, &slist); 1278 } 1279 1280 static void mptcp_nl_remove_addrs_list(struct net *net, 1281 struct list_head *rm_list) 1282 { 1283 long s_slot = 0, s_num = 0; 1284 struct mptcp_sock *msk; 1285 1286 if (list_empty(rm_list)) 1287 return; 1288 1289 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { 1290 struct sock *sk = (struct sock *)msk; 1291 1292 lock_sock(sk); 1293 mptcp_pm_remove_addrs_and_subflows(msk, rm_list); 1294 release_sock(sk); 1295 1296 sock_put(sk); 1297 cond_resched(); 1298 } 1299 } 1300 1301 static void __flush_addrs(struct list_head *list) 1302 { 1303 while (!list_empty(list)) { 1304 struct mptcp_pm_addr_entry *cur; 1305 1306 cur = list_entry(list->next, 1307 struct mptcp_pm_addr_entry, list); 1308 list_del_rcu(&cur->list); 1309 mptcp_pm_free_addr_entry(cur); 1310 } 1311 } 1312 1313 static void __reset_counters(struct pm_nl_pernet *pernet) 1314 { 1315 WRITE_ONCE(pernet->add_addr_signal_max, 0); 1316 WRITE_ONCE(pernet->add_addr_accept_max, 0); 1317 WRITE_ONCE(pernet->local_addr_max, 0); 1318 pernet->addrs = 0; 1319 } 1320 1321 static int mptcp_nl_cmd_flush_addrs(struct sk_buff *skb, struct genl_info *info) 1322 { 1323 struct pm_nl_pernet *pernet = genl_info_pm_nl(info); 1324 LIST_HEAD(free_list); 1325 1326 spin_lock_bh(&pernet->lock); 1327 list_splice_init(&pernet->local_addr_list, &free_list); 1328 __reset_counters(pernet); 1329 pernet->next_id = 1; 1330 bitmap_zero(pernet->id_bitmap, MAX_ADDR_ID + 1); 1331 spin_unlock_bh(&pernet->lock); 1332 mptcp_nl_remove_addrs_list(sock_net(skb->sk), &free_list); 1333 __flush_addrs(&free_list); 1334 return 0; 1335 } 1336 1337 static int mptcp_nl_fill_addr(struct sk_buff *skb, 1338 struct mptcp_pm_addr_entry *entry) 1339 { 1340 struct mptcp_addr_info *addr = &entry->addr; 1341 struct nlattr *attr; 1342 1343 attr = nla_nest_start(skb, MPTCP_PM_ATTR_ADDR); 1344 if (!attr) 1345 return -EMSGSIZE; 1346 1347 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_FAMILY, addr->family)) 1348 goto nla_put_failure; 1349 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_PORT, ntohs(addr->port))) 1350 goto nla_put_failure; 1351 if (nla_put_u8(skb, MPTCP_PM_ADDR_ATTR_ID, addr->id)) 1352 goto nla_put_failure; 1353 if (nla_put_u32(skb, MPTCP_PM_ADDR_ATTR_FLAGS, entry->flags)) 1354 goto nla_put_failure; 1355 if (entry->ifindex && 1356 nla_put_s32(skb, MPTCP_PM_ADDR_ATTR_IF_IDX, entry->ifindex)) 1357 goto nla_put_failure; 1358 1359 if (addr->family == AF_INET && 1360 nla_put_in_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR4, 1361 addr->addr.s_addr)) 1362 goto nla_put_failure; 1363 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1364 else if (addr->family == AF_INET6 && 1365 nla_put_in6_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR6, &addr->addr6)) 1366 goto nla_put_failure; 1367 #endif 1368 nla_nest_end(skb, attr); 1369 return 0; 1370 1371 nla_put_failure: 1372 nla_nest_cancel(skb, attr); 1373 return -EMSGSIZE; 1374 } 1375 1376 static int mptcp_nl_cmd_get_addr(struct sk_buff *skb, struct genl_info *info) 1377 { 1378 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR]; 1379 struct pm_nl_pernet *pernet = genl_info_pm_nl(info); 1380 struct mptcp_pm_addr_entry addr, *entry; 1381 struct sk_buff *msg; 1382 void *reply; 1383 int ret; 1384 1385 ret = mptcp_pm_parse_addr(attr, info, false, &addr); 1386 if (ret < 0) 1387 return ret; 1388 1389 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1390 if (!msg) 1391 return -ENOMEM; 1392 1393 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0, 1394 info->genlhdr->cmd); 1395 if (!reply) { 1396 GENL_SET_ERR_MSG(info, "not enough space in Netlink message"); 1397 ret = -EMSGSIZE; 1398 goto fail; 1399 } 1400 1401 spin_lock_bh(&pernet->lock); 1402 entry = __lookup_addr_by_id(pernet, addr.addr.id); 1403 if (!entry) { 1404 GENL_SET_ERR_MSG(info, "address not found"); 1405 ret = -EINVAL; 1406 goto unlock_fail; 1407 } 1408 1409 ret = mptcp_nl_fill_addr(msg, entry); 1410 if (ret) 1411 goto unlock_fail; 1412 1413 genlmsg_end(msg, reply); 1414 ret = genlmsg_reply(msg, info); 1415 spin_unlock_bh(&pernet->lock); 1416 return ret; 1417 1418 unlock_fail: 1419 spin_unlock_bh(&pernet->lock); 1420 1421 fail: 1422 nlmsg_free(msg); 1423 return ret; 1424 } 1425 1426 static int mptcp_nl_cmd_dump_addrs(struct sk_buff *msg, 1427 struct netlink_callback *cb) 1428 { 1429 struct net *net = sock_net(msg->sk); 1430 struct mptcp_pm_addr_entry *entry; 1431 struct pm_nl_pernet *pernet; 1432 int id = cb->args[0]; 1433 void *hdr; 1434 int i; 1435 1436 pernet = net_generic(net, pm_nl_pernet_id); 1437 1438 spin_lock_bh(&pernet->lock); 1439 for (i = id; i < MAX_ADDR_ID + 1; i++) { 1440 if (test_bit(i, pernet->id_bitmap)) { 1441 entry = __lookup_addr_by_id(pernet, i); 1442 if (!entry) 1443 break; 1444 1445 if (entry->addr.id <= id) 1446 continue; 1447 1448 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, 1449 cb->nlh->nlmsg_seq, &mptcp_genl_family, 1450 NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR); 1451 if (!hdr) 1452 break; 1453 1454 if (mptcp_nl_fill_addr(msg, entry) < 0) { 1455 genlmsg_cancel(msg, hdr); 1456 break; 1457 } 1458 1459 id = entry->addr.id; 1460 genlmsg_end(msg, hdr); 1461 } 1462 } 1463 spin_unlock_bh(&pernet->lock); 1464 1465 cb->args[0] = id; 1466 return msg->len; 1467 } 1468 1469 static int parse_limit(struct genl_info *info, int id, unsigned int *limit) 1470 { 1471 struct nlattr *attr = info->attrs[id]; 1472 1473 if (!attr) 1474 return 0; 1475 1476 *limit = nla_get_u32(attr); 1477 if (*limit > MPTCP_PM_ADDR_MAX) { 1478 GENL_SET_ERR_MSG(info, "limit greater than maximum"); 1479 return -EINVAL; 1480 } 1481 return 0; 1482 } 1483 1484 static int 1485 mptcp_nl_cmd_set_limits(struct sk_buff *skb, struct genl_info *info) 1486 { 1487 struct pm_nl_pernet *pernet = genl_info_pm_nl(info); 1488 unsigned int rcv_addrs, subflows; 1489 int ret; 1490 1491 spin_lock_bh(&pernet->lock); 1492 rcv_addrs = pernet->add_addr_accept_max; 1493 ret = parse_limit(info, MPTCP_PM_ATTR_RCV_ADD_ADDRS, &rcv_addrs); 1494 if (ret) 1495 goto unlock; 1496 1497 subflows = pernet->subflows_max; 1498 ret = parse_limit(info, MPTCP_PM_ATTR_SUBFLOWS, &subflows); 1499 if (ret) 1500 goto unlock; 1501 1502 WRITE_ONCE(pernet->add_addr_accept_max, rcv_addrs); 1503 WRITE_ONCE(pernet->subflows_max, subflows); 1504 1505 unlock: 1506 spin_unlock_bh(&pernet->lock); 1507 return ret; 1508 } 1509 1510 static int 1511 mptcp_nl_cmd_get_limits(struct sk_buff *skb, struct genl_info *info) 1512 { 1513 struct pm_nl_pernet *pernet = genl_info_pm_nl(info); 1514 struct sk_buff *msg; 1515 void *reply; 1516 1517 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1518 if (!msg) 1519 return -ENOMEM; 1520 1521 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0, 1522 MPTCP_PM_CMD_GET_LIMITS); 1523 if (!reply) 1524 goto fail; 1525 1526 if (nla_put_u32(msg, MPTCP_PM_ATTR_RCV_ADD_ADDRS, 1527 READ_ONCE(pernet->add_addr_accept_max))) 1528 goto fail; 1529 1530 if (nla_put_u32(msg, MPTCP_PM_ATTR_SUBFLOWS, 1531 READ_ONCE(pernet->subflows_max))) 1532 goto fail; 1533 1534 genlmsg_end(msg, reply); 1535 return genlmsg_reply(msg, info); 1536 1537 fail: 1538 GENL_SET_ERR_MSG(info, "not enough space in Netlink message"); 1539 nlmsg_free(msg); 1540 return -EMSGSIZE; 1541 } 1542 1543 static int mptcp_nl_addr_backup(struct net *net, 1544 struct mptcp_addr_info *addr, 1545 u8 bkup) 1546 { 1547 long s_slot = 0, s_num = 0; 1548 struct mptcp_sock *msk; 1549 int ret = -EINVAL; 1550 1551 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { 1552 struct sock *sk = (struct sock *)msk; 1553 1554 if (list_empty(&msk->conn_list)) 1555 goto next; 1556 1557 lock_sock(sk); 1558 spin_lock_bh(&msk->pm.lock); 1559 ret = mptcp_pm_nl_mp_prio_send_ack(msk, addr, bkup); 1560 spin_unlock_bh(&msk->pm.lock); 1561 release_sock(sk); 1562 1563 next: 1564 sock_put(sk); 1565 cond_resched(); 1566 } 1567 1568 return ret; 1569 } 1570 1571 static int mptcp_nl_cmd_set_flags(struct sk_buff *skb, struct genl_info *info) 1572 { 1573 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR]; 1574 struct pm_nl_pernet *pernet = genl_info_pm_nl(info); 1575 struct mptcp_pm_addr_entry addr, *entry; 1576 struct net *net = sock_net(skb->sk); 1577 u8 bkup = 0; 1578 int ret; 1579 1580 ret = mptcp_pm_parse_addr(attr, info, true, &addr); 1581 if (ret < 0) 1582 return ret; 1583 1584 if (addr.flags & MPTCP_PM_ADDR_FLAG_BACKUP) 1585 bkup = 1; 1586 1587 list_for_each_entry(entry, &pernet->local_addr_list, list) { 1588 if (addresses_equal(&entry->addr, &addr.addr, true)) { 1589 ret = mptcp_nl_addr_backup(net, &entry->addr, bkup); 1590 if (ret) 1591 return ret; 1592 1593 if (bkup) 1594 entry->flags |= MPTCP_PM_ADDR_FLAG_BACKUP; 1595 else 1596 entry->flags &= ~MPTCP_PM_ADDR_FLAG_BACKUP; 1597 } 1598 } 1599 1600 return 0; 1601 } 1602 1603 static void mptcp_nl_mcast_send(struct net *net, struct sk_buff *nlskb, gfp_t gfp) 1604 { 1605 genlmsg_multicast_netns(&mptcp_genl_family, net, 1606 nlskb, 0, MPTCP_PM_EV_GRP_OFFSET, gfp); 1607 } 1608 1609 static int mptcp_event_add_subflow(struct sk_buff *skb, const struct sock *ssk) 1610 { 1611 const struct inet_sock *issk = inet_sk(ssk); 1612 const struct mptcp_subflow_context *sf; 1613 1614 if (nla_put_u16(skb, MPTCP_ATTR_FAMILY, ssk->sk_family)) 1615 return -EMSGSIZE; 1616 1617 switch (ssk->sk_family) { 1618 case AF_INET: 1619 if (nla_put_in_addr(skb, MPTCP_ATTR_SADDR4, issk->inet_saddr)) 1620 return -EMSGSIZE; 1621 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, issk->inet_daddr)) 1622 return -EMSGSIZE; 1623 break; 1624 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1625 case AF_INET6: { 1626 const struct ipv6_pinfo *np = inet6_sk(ssk); 1627 1628 if (nla_put_in6_addr(skb, MPTCP_ATTR_SADDR6, &np->saddr)) 1629 return -EMSGSIZE; 1630 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &ssk->sk_v6_daddr)) 1631 return -EMSGSIZE; 1632 break; 1633 } 1634 #endif 1635 default: 1636 WARN_ON_ONCE(1); 1637 return -EMSGSIZE; 1638 } 1639 1640 if (nla_put_be16(skb, MPTCP_ATTR_SPORT, issk->inet_sport)) 1641 return -EMSGSIZE; 1642 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, issk->inet_dport)) 1643 return -EMSGSIZE; 1644 1645 sf = mptcp_subflow_ctx(ssk); 1646 if (WARN_ON_ONCE(!sf)) 1647 return -EINVAL; 1648 1649 if (nla_put_u8(skb, MPTCP_ATTR_LOC_ID, sf->local_id)) 1650 return -EMSGSIZE; 1651 1652 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, sf->remote_id)) 1653 return -EMSGSIZE; 1654 1655 return 0; 1656 } 1657 1658 static int mptcp_event_put_token_and_ssk(struct sk_buff *skb, 1659 const struct mptcp_sock *msk, 1660 const struct sock *ssk) 1661 { 1662 const struct sock *sk = (const struct sock *)msk; 1663 const struct mptcp_subflow_context *sf; 1664 u8 sk_err; 1665 1666 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token)) 1667 return -EMSGSIZE; 1668 1669 if (mptcp_event_add_subflow(skb, ssk)) 1670 return -EMSGSIZE; 1671 1672 sf = mptcp_subflow_ctx(ssk); 1673 if (WARN_ON_ONCE(!sf)) 1674 return -EINVAL; 1675 1676 if (nla_put_u8(skb, MPTCP_ATTR_BACKUP, sf->backup)) 1677 return -EMSGSIZE; 1678 1679 if (ssk->sk_bound_dev_if && 1680 nla_put_s32(skb, MPTCP_ATTR_IF_IDX, ssk->sk_bound_dev_if)) 1681 return -EMSGSIZE; 1682 1683 sk_err = ssk->sk_err; 1684 if (sk_err && sk->sk_state == TCP_ESTABLISHED && 1685 nla_put_u8(skb, MPTCP_ATTR_ERROR, sk_err)) 1686 return -EMSGSIZE; 1687 1688 return 0; 1689 } 1690 1691 static int mptcp_event_sub_established(struct sk_buff *skb, 1692 const struct mptcp_sock *msk, 1693 const struct sock *ssk) 1694 { 1695 return mptcp_event_put_token_and_ssk(skb, msk, ssk); 1696 } 1697 1698 static int mptcp_event_sub_closed(struct sk_buff *skb, 1699 const struct mptcp_sock *msk, 1700 const struct sock *ssk) 1701 { 1702 const struct mptcp_subflow_context *sf; 1703 1704 if (mptcp_event_put_token_and_ssk(skb, msk, ssk)) 1705 return -EMSGSIZE; 1706 1707 sf = mptcp_subflow_ctx(ssk); 1708 if (!sf->reset_seen) 1709 return 0; 1710 1711 if (nla_put_u32(skb, MPTCP_ATTR_RESET_REASON, sf->reset_reason)) 1712 return -EMSGSIZE; 1713 1714 if (nla_put_u32(skb, MPTCP_ATTR_RESET_FLAGS, sf->reset_transient)) 1715 return -EMSGSIZE; 1716 1717 return 0; 1718 } 1719 1720 static int mptcp_event_created(struct sk_buff *skb, 1721 const struct mptcp_sock *msk, 1722 const struct sock *ssk) 1723 { 1724 int err = nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token); 1725 1726 if (err) 1727 return err; 1728 1729 return mptcp_event_add_subflow(skb, ssk); 1730 } 1731 1732 void mptcp_event_addr_removed(const struct mptcp_sock *msk, uint8_t id) 1733 { 1734 struct net *net = sock_net((const struct sock *)msk); 1735 struct nlmsghdr *nlh; 1736 struct sk_buff *skb; 1737 1738 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET)) 1739 return; 1740 1741 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); 1742 if (!skb) 1743 return; 1744 1745 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, MPTCP_EVENT_REMOVED); 1746 if (!nlh) 1747 goto nla_put_failure; 1748 1749 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token)) 1750 goto nla_put_failure; 1751 1752 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, id)) 1753 goto nla_put_failure; 1754 1755 genlmsg_end(skb, nlh); 1756 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC); 1757 return; 1758 1759 nla_put_failure: 1760 kfree_skb(skb); 1761 } 1762 1763 void mptcp_event_addr_announced(const struct mptcp_sock *msk, 1764 const struct mptcp_addr_info *info) 1765 { 1766 struct net *net = sock_net((const struct sock *)msk); 1767 struct nlmsghdr *nlh; 1768 struct sk_buff *skb; 1769 1770 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET)) 1771 return; 1772 1773 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); 1774 if (!skb) 1775 return; 1776 1777 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, 1778 MPTCP_EVENT_ANNOUNCED); 1779 if (!nlh) 1780 goto nla_put_failure; 1781 1782 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token)) 1783 goto nla_put_failure; 1784 1785 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, info->id)) 1786 goto nla_put_failure; 1787 1788 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, info->port)) 1789 goto nla_put_failure; 1790 1791 switch (info->family) { 1792 case AF_INET: 1793 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, info->addr.s_addr)) 1794 goto nla_put_failure; 1795 break; 1796 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1797 case AF_INET6: 1798 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &info->addr6)) 1799 goto nla_put_failure; 1800 break; 1801 #endif 1802 default: 1803 WARN_ON_ONCE(1); 1804 goto nla_put_failure; 1805 } 1806 1807 genlmsg_end(skb, nlh); 1808 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC); 1809 return; 1810 1811 nla_put_failure: 1812 kfree_skb(skb); 1813 } 1814 1815 void mptcp_event(enum mptcp_event_type type, const struct mptcp_sock *msk, 1816 const struct sock *ssk, gfp_t gfp) 1817 { 1818 struct net *net = sock_net((const struct sock *)msk); 1819 struct nlmsghdr *nlh; 1820 struct sk_buff *skb; 1821 1822 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET)) 1823 return; 1824 1825 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp); 1826 if (!skb) 1827 return; 1828 1829 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, type); 1830 if (!nlh) 1831 goto nla_put_failure; 1832 1833 switch (type) { 1834 case MPTCP_EVENT_UNSPEC: 1835 WARN_ON_ONCE(1); 1836 break; 1837 case MPTCP_EVENT_CREATED: 1838 case MPTCP_EVENT_ESTABLISHED: 1839 if (mptcp_event_created(skb, msk, ssk) < 0) 1840 goto nla_put_failure; 1841 break; 1842 case MPTCP_EVENT_CLOSED: 1843 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token) < 0) 1844 goto nla_put_failure; 1845 break; 1846 case MPTCP_EVENT_ANNOUNCED: 1847 case MPTCP_EVENT_REMOVED: 1848 /* call mptcp_event_addr_announced()/removed instead */ 1849 WARN_ON_ONCE(1); 1850 break; 1851 case MPTCP_EVENT_SUB_ESTABLISHED: 1852 case MPTCP_EVENT_SUB_PRIORITY: 1853 if (mptcp_event_sub_established(skb, msk, ssk) < 0) 1854 goto nla_put_failure; 1855 break; 1856 case MPTCP_EVENT_SUB_CLOSED: 1857 if (mptcp_event_sub_closed(skb, msk, ssk) < 0) 1858 goto nla_put_failure; 1859 break; 1860 } 1861 1862 genlmsg_end(skb, nlh); 1863 mptcp_nl_mcast_send(net, skb, gfp); 1864 return; 1865 1866 nla_put_failure: 1867 kfree_skb(skb); 1868 } 1869 1870 static const struct genl_small_ops mptcp_pm_ops[] = { 1871 { 1872 .cmd = MPTCP_PM_CMD_ADD_ADDR, 1873 .doit = mptcp_nl_cmd_add_addr, 1874 .flags = GENL_ADMIN_PERM, 1875 }, 1876 { 1877 .cmd = MPTCP_PM_CMD_DEL_ADDR, 1878 .doit = mptcp_nl_cmd_del_addr, 1879 .flags = GENL_ADMIN_PERM, 1880 }, 1881 { 1882 .cmd = MPTCP_PM_CMD_FLUSH_ADDRS, 1883 .doit = mptcp_nl_cmd_flush_addrs, 1884 .flags = GENL_ADMIN_PERM, 1885 }, 1886 { 1887 .cmd = MPTCP_PM_CMD_GET_ADDR, 1888 .doit = mptcp_nl_cmd_get_addr, 1889 .dumpit = mptcp_nl_cmd_dump_addrs, 1890 }, 1891 { 1892 .cmd = MPTCP_PM_CMD_SET_LIMITS, 1893 .doit = mptcp_nl_cmd_set_limits, 1894 .flags = GENL_ADMIN_PERM, 1895 }, 1896 { 1897 .cmd = MPTCP_PM_CMD_GET_LIMITS, 1898 .doit = mptcp_nl_cmd_get_limits, 1899 }, 1900 { 1901 .cmd = MPTCP_PM_CMD_SET_FLAGS, 1902 .doit = mptcp_nl_cmd_set_flags, 1903 .flags = GENL_ADMIN_PERM, 1904 }, 1905 }; 1906 1907 static struct genl_family mptcp_genl_family __ro_after_init = { 1908 .name = MPTCP_PM_NAME, 1909 .version = MPTCP_PM_VER, 1910 .maxattr = MPTCP_PM_ATTR_MAX, 1911 .policy = mptcp_pm_policy, 1912 .netnsok = true, 1913 .module = THIS_MODULE, 1914 .small_ops = mptcp_pm_ops, 1915 .n_small_ops = ARRAY_SIZE(mptcp_pm_ops), 1916 .mcgrps = mptcp_pm_mcgrps, 1917 .n_mcgrps = ARRAY_SIZE(mptcp_pm_mcgrps), 1918 }; 1919 1920 static int __net_init pm_nl_init_net(struct net *net) 1921 { 1922 struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id); 1923 1924 INIT_LIST_HEAD_RCU(&pernet->local_addr_list); 1925 pernet->next_id = 1; 1926 spin_lock_init(&pernet->lock); 1927 1928 /* No need to initialize other pernet fields, the struct is zeroed at 1929 * allocation time. 1930 */ 1931 1932 return 0; 1933 } 1934 1935 static void __net_exit pm_nl_exit_net(struct list_head *net_list) 1936 { 1937 struct net *net; 1938 1939 list_for_each_entry(net, net_list, exit_list) { 1940 struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id); 1941 1942 /* net is removed from namespace list, can't race with 1943 * other modifiers 1944 */ 1945 __flush_addrs(&pernet->local_addr_list); 1946 } 1947 } 1948 1949 static struct pernet_operations mptcp_pm_pernet_ops = { 1950 .init = pm_nl_init_net, 1951 .exit_batch = pm_nl_exit_net, 1952 .id = &pm_nl_pernet_id, 1953 .size = sizeof(struct pm_nl_pernet), 1954 }; 1955 1956 void __init mptcp_pm_nl_init(void) 1957 { 1958 if (register_pernet_subsys(&mptcp_pm_pernet_ops) < 0) 1959 panic("Failed to register MPTCP PM pernet subsystem.\n"); 1960 1961 if (genl_register_family(&mptcp_genl_family)) 1962 panic("Failed to register MPTCP PM netlink family\n"); 1963 } 1964