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