1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (c) 2020, Red Hat, Inc.
7 #define pr_fmt(fmt) "MPTCP: " fmt
9 #include <linux/inet.h>
10 #include <linux/kernel.h>
12 #include <net/netns/generic.h>
13 #include <net/mptcp.h>
14 #include <net/genetlink.h>
15 #include <uapi/linux/mptcp.h>
20 /* forward declaration */
21 static struct genl_family mptcp_genl_family;
23 static int pm_nl_pernet_id;
25 struct mptcp_pm_addr_entry {
26 struct list_head list;
27 struct mptcp_addr_info addr;
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;
40 #define MAX_ADDR_ID 255
41 #define BITMAP_SZ DIV_ROUND_UP(MAX_ADDR_ID + 1, BITS_PER_LONG)
44 /* protects pernet updates */
46 struct list_head local_addr_list;
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;
53 unsigned long id_bitmap[BITMAP_SZ];
56 #define MPTCP_PM_ADDR_MAX 8
57 #define ADD_ADDR_RETRANS_MAX 3
59 static void mptcp_pm_nl_add_addr_send_ack(struct mptcp_sock *msk);
61 static bool addresses_equal(const struct mptcp_addr_info *a,
62 struct mptcp_addr_info *b, bool use_port)
64 bool addr_equals = false;
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)
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;
86 return a->port == b->port;
89 static bool address_zero(const struct mptcp_addr_info *addr)
91 struct mptcp_addr_info zero;
93 memset(&zero, 0, sizeof(zero));
94 zero.family = addr->family;
96 return addresses_equal(addr, &zero, true);
99 static void local_address(const struct sock_common *skc,
100 struct mptcp_addr_info *addr)
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;
112 static void remote_address(const struct sock_common *skc,
113 struct mptcp_addr_info *addr)
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;
125 static bool lookup_subflow_by_saddr(const struct list_head *list,
126 struct mptcp_addr_info *saddr)
128 struct mptcp_subflow_context *subflow;
129 struct mptcp_addr_info cur;
130 struct sock_common *skc;
132 list_for_each_entry(subflow, list, node) {
133 skc = (struct sock_common *)mptcp_subflow_tcp_sock(subflow);
135 local_address(skc, &cur);
136 if (addresses_equal(&cur, saddr, saddr->port))
143 static struct mptcp_pm_addr_entry *
144 select_local_address(const struct pm_nl_pernet *pernet,
145 struct mptcp_sock *msk)
147 struct mptcp_pm_addr_entry *entry, *ret = NULL;
148 struct sock *sk = (struct sock *)msk;
150 msk_owned_by_me(msk);
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))
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)))
168 /* avoid any address already in use by subflows and
171 if (!lookup_subflow_by_saddr(&msk->conn_list, &entry->addr)) {
180 static struct mptcp_pm_addr_entry *
181 select_signal_address(struct pm_nl_pernet *pernet, unsigned int pos)
183 struct mptcp_pm_addr_entry *entry, *ret = NULL;
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.
192 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
193 if (!(entry->addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL))
204 unsigned int mptcp_pm_get_add_addr_signal_max(struct mptcp_sock *msk)
206 struct pm_nl_pernet *pernet;
208 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
209 return READ_ONCE(pernet->add_addr_signal_max);
211 EXPORT_SYMBOL_GPL(mptcp_pm_get_add_addr_signal_max);
213 unsigned int mptcp_pm_get_add_addr_accept_max(struct mptcp_sock *msk)
215 struct pm_nl_pernet *pernet;
217 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
218 return READ_ONCE(pernet->add_addr_accept_max);
220 EXPORT_SYMBOL_GPL(mptcp_pm_get_add_addr_accept_max);
222 unsigned int mptcp_pm_get_subflows_max(struct mptcp_sock *msk)
224 struct pm_nl_pernet *pernet;
226 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
227 return READ_ONCE(pernet->subflows_max);
229 EXPORT_SYMBOL_GPL(mptcp_pm_get_subflows_max);
231 unsigned int mptcp_pm_get_local_addr_max(struct mptcp_sock *msk)
233 struct pm_nl_pernet *pernet;
235 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
236 return READ_ONCE(pernet->local_addr_max);
238 EXPORT_SYMBOL_GPL(mptcp_pm_get_local_addr_max);
240 static void check_work_pending(struct mptcp_sock *msk)
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);
248 static struct mptcp_pm_add_entry *
249 lookup_anno_list_by_saddr(struct mptcp_sock *msk,
250 struct mptcp_addr_info *addr)
252 struct mptcp_pm_add_entry *entry;
254 lockdep_assert_held(&msk->pm.lock);
256 list_for_each_entry(entry, &msk->pm.anno_list, list) {
257 if (addresses_equal(&entry->addr, addr, true))
264 bool mptcp_pm_sport_in_anno_list(struct mptcp_sock *msk, const struct sock *sk)
266 struct mptcp_pm_add_entry *entry;
267 struct mptcp_addr_info saddr;
270 local_address((struct sock_common *)sk, &saddr);
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)) {
281 spin_unlock_bh(&msk->pm.lock);
285 static void mptcp_pm_add_timer(struct timer_list *timer)
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;
291 pr_debug("msk=%p", msk);
296 if (inet_sk_state_load(sk) == TCP_CLOSE)
302 if (mptcp_pm_should_add_signal(msk)) {
303 sk_reset_timer(sk, timer, jiffies + TCP_RTO_MAX / 8);
307 spin_lock_bh(&msk->pm.lock);
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++;
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)));
320 spin_unlock_bh(&msk->pm.lock);
326 struct mptcp_pm_add_entry *
327 mptcp_pm_del_add_timer(struct mptcp_sock *msk,
328 struct mptcp_addr_info *addr)
330 struct mptcp_pm_add_entry *entry;
331 struct sock *sk = (struct sock *)msk;
333 spin_lock_bh(&msk->pm.lock);
334 entry = lookup_anno_list_by_saddr(msk, addr);
336 entry->retrans_times = ADD_ADDR_RETRANS_MAX;
337 spin_unlock_bh(&msk->pm.lock);
340 sk_stop_timer_sync(sk, &entry->add_timer);
345 static bool mptcp_pm_alloc_anno_list(struct mptcp_sock *msk,
346 struct mptcp_pm_addr_entry *entry)
348 struct mptcp_pm_add_entry *add_entry = NULL;
349 struct sock *sk = (struct sock *)msk;
350 struct net *net = sock_net(sk);
352 lockdep_assert_held(&msk->pm.lock);
354 if (lookup_anno_list_by_saddr(msk, &entry->addr))
357 add_entry = kmalloc(sizeof(*add_entry), GFP_ATOMIC);
361 list_add(&add_entry->list, &msk->pm.anno_list);
363 add_entry->addr = entry->addr;
364 add_entry->sock = msk;
365 add_entry->retrans_times = 0;
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));
374 void mptcp_pm_free_anno_list(struct mptcp_sock *msk)
376 struct mptcp_pm_add_entry *entry, *tmp;
377 struct sock *sk = (struct sock *)msk;
378 LIST_HEAD(free_list);
380 pr_debug("msk=%p", msk);
382 spin_lock_bh(&msk->pm.lock);
383 list_splice_init(&msk->pm.anno_list, &free_list);
384 spin_unlock_bh(&msk->pm.lock);
386 list_for_each_entry_safe(entry, tmp, &free_list, list) {
387 sk_stop_timer_sync(sk, &entry->add_timer);
392 static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk)
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;
401 pernet = net_generic(sock_net(sk), pm_nl_pernet_id);
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);
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);
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);
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);
424 /* pick failed, avoid fourther attempts later */
425 msk->pm.local_addr_used = add_addr_signal_max;
428 check_work_pending(msk);
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);
436 struct mptcp_addr_info remote = { 0 };
438 msk->pm.local_addr_used++;
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);
448 /* lookup failed, avoid fourther attempts later */
449 msk->pm.local_addr_used = local_addr_max;
450 check_work_pending(msk);
454 static void mptcp_pm_nl_fully_established(struct mptcp_sock *msk)
456 mptcp_pm_create_subflow_or_signal_addr(msk);
459 static void mptcp_pm_nl_subflow_established(struct mptcp_sock *msk)
461 mptcp_pm_create_subflow_or_signal_addr(msk);
464 static void mptcp_pm_nl_add_addr_received(struct mptcp_sock *msk)
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;
473 add_addr_accept_max = mptcp_pm_get_add_addr_accept_max(msk);
474 subflows_max = mptcp_pm_get_subflows_max(msk);
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++;
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);
485 /* connect to the specified remote address, using whatever
486 * local address the routing configuration will pick.
488 remote = msk->pm.remote;
490 remote.port = sk->sk_dport;
493 memset(&local, 0, sizeof(local));
494 local.family = remote.family;
496 spin_unlock_bh(&msk->pm.lock);
497 __mptcp_subflow_connect(sk, &local, &remote);
498 spin_lock_bh(&msk->pm.lock);
500 mptcp_pm_announce_addr(msk, &remote, true, use_port);
501 mptcp_pm_nl_add_addr_send_ack(msk);
504 static void mptcp_pm_nl_add_addr_send_ack(struct mptcp_sock *msk)
506 struct mptcp_subflow_context *subflow;
508 msk_owned_by_me(msk);
509 lockdep_assert_held(&msk->pm.lock);
511 if (!mptcp_pm_should_add_signal(msk))
514 __mptcp_flush_join_list(msk);
515 subflow = list_first_entry_or_null(&msk->conn_list, typeof(*subflow), node);
517 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
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]" : "");
528 spin_lock_bh(&msk->pm.lock);
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);
539 int mptcp_pm_nl_mp_prio_send_ack(struct mptcp_sock *msk,
540 struct mptcp_addr_info *addr,
543 struct mptcp_subflow_context *subflow;
545 pr_debug("bkup=%d", bkup);
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;
552 local_address((struct sock_common *)ssk, &local);
553 if (!addresses_equal(&local, addr, addr->port))
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);
561 spin_unlock_bh(&msk->pm.lock);
562 pr_debug("send ack for mp_prio");
566 spin_lock_bh(&msk->pm.lock);
574 static void mptcp_pm_nl_rm_addr_received(struct mptcp_sock *msk)
576 struct mptcp_subflow_context *subflow, *tmp;
577 struct sock *sk = (struct sock *)msk;
579 pr_debug("address rm_id %d", msk->pm.rm_id);
581 msk_owned_by_me(msk);
586 if (list_empty(&msk->conn_list))
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;
593 if (msk->pm.rm_id != subflow->remote_id)
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);
601 msk->pm.add_addr_accepted--;
603 WRITE_ONCE(msk->pm.accept_addr, true);
605 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMADDR);
611 void mptcp_pm_nl_work(struct mptcp_sock *msk)
613 struct mptcp_pm_data *pm = &msk->pm;
615 msk_owned_by_me(msk);
617 spin_lock_bh(&msk->pm.lock);
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);
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);
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);
632 if (pm->status & BIT(MPTCP_PM_ESTABLISHED)) {
633 pm->status &= ~BIT(MPTCP_PM_ESTABLISHED);
634 mptcp_pm_nl_fully_established(msk);
636 if (pm->status & BIT(MPTCP_PM_SUBFLOW_ESTABLISHED)) {
637 pm->status &= ~BIT(MPTCP_PM_SUBFLOW_ESTABLISHED);
638 mptcp_pm_nl_subflow_established(msk);
641 spin_unlock_bh(&msk->pm.lock);
644 void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk, u8 rm_id)
646 struct mptcp_subflow_context *subflow, *tmp;
647 struct sock *sk = (struct sock *)msk;
649 pr_debug("subflow rm_id %d", rm_id);
651 msk_owned_by_me(msk);
656 if (list_empty(&msk->conn_list))
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;
663 if (rm_id != subflow->local_id)
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);
671 msk->pm.local_addr_used--;
674 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMSUBFLOW);
680 static bool address_use_port(struct mptcp_pm_addr_entry *entry)
682 return (entry->addr.flags &
683 (MPTCP_PM_ADDR_FLAG_SIGNAL | MPTCP_PM_ADDR_FLAG_SUBFLOW)) ==
684 MPTCP_PM_ADDR_FLAG_SIGNAL;
687 static int mptcp_pm_nl_append_new_local_addr(struct pm_nl_pernet *pernet,
688 struct mptcp_pm_addr_entry *entry)
690 struct mptcp_pm_addr_entry *cur;
691 unsigned int addr_max;
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
698 if (pernet->next_id == MAX_ADDR_ID)
700 if (pernet->addrs >= MPTCP_PM_ADDR_MAX)
702 if (test_bit(entry->addr.id, pernet->id_bitmap))
705 /* do not insert duplicate address, differentiate on port only
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)))
715 if (!entry->addr.id) {
717 entry->addr.id = find_next_zero_bit(pernet->id_bitmap,
720 if ((!entry->addr.id || entry->addr.id > MAX_ADDR_ID) &&
721 pernet->next_id != 1) {
727 if (!entry->addr.id || entry->addr.id > MAX_ADDR_ID)
730 __set_bit(entry->addr.id, pernet->id_bitmap);
731 if (entry->addr.id > pernet->next_id)
732 pernet->next_id = entry->addr.id;
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);
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);
744 list_add_tail_rcu(&entry->list, &pernet->local_addr_list);
745 ret = entry->addr.id;
748 spin_unlock_bh(&pernet->lock);
752 static int mptcp_pm_nl_create_listen_socket(struct sock *sk,
753 struct mptcp_pm_addr_entry *entry)
755 struct sockaddr_storage addr;
756 struct mptcp_sock *msk;
757 struct socket *ssock;
761 err = sock_create_kern(sock_net(sk), entry->addr.family,
762 SOCK_STREAM, IPPROTO_MPTCP, &entry->lsk);
766 msk = mptcp_sk(entry->lsk->sk);
772 ssock = __mptcp_nmpc_socket(msk);
778 mptcp_info2sockaddr(&entry->addr, &addr, entry->addr.family);
779 err = kernel_bind(ssock, (struct sockaddr *)&addr,
780 sizeof(struct sockaddr_in));
782 pr_warn("kernel_bind error, err=%d", err);
786 err = kernel_listen(ssock, backlog);
788 pr_warn("kernel_listen error, err=%d", err);
795 sock_release(entry->lsk);
799 int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc)
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;
807 if (WARN_ON_ONCE(!msk))
810 /* The 0 ID mapping is defined by the first subflow, copied into the msk
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))
818 if (address_zero(&skc_local))
821 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
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;
834 /* address not found, add to local list */
835 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
839 entry->addr = skc_local;
840 entry->addr.ifindex = 0;
841 entry->addr.flags = 0;
843 entry->addr.port = 0;
845 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
852 void mptcp_pm_nl_data_init(struct mptcp_sock *msk)
854 struct mptcp_pm_data *pm = &msk->pm;
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);
864 #define MPTCP_PM_CMD_GRP_OFFSET 0
865 #define MPTCP_PM_EV_GRP_OFFSET 1
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,
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 },
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, },
893 static int mptcp_pm_family_to_addr(int family)
895 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
896 if (family == AF_INET6)
897 return MPTCP_PM_ADDR_ATTR_ADDR6;
899 return MPTCP_PM_ADDR_ATTR_ADDR4;
902 static int mptcp_pm_parse_addr(struct nlattr *attr, struct genl_info *info,
904 struct mptcp_pm_addr_entry *entry)
906 struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1];
910 GENL_SET_ERR_MSG(info, "missing address info");
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);
920 memset(entry, 0, sizeof(*entry));
921 if (!tb[MPTCP_PM_ADDR_ATTR_FAMILY]) {
925 NL_SET_ERR_MSG_ATTR(info->extack, attr,
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
936 NL_SET_ERR_MSG_ATTR(info->extack, attr,
937 "unknown address family");
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");
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]);
952 entry->addr.addr.s_addr = nla_get_in_addr(tb[addr_addr]);
955 if (tb[MPTCP_PM_ADDR_ATTR_IF_IDX]) {
956 u32 val = nla_get_s32(tb[MPTCP_PM_ADDR_ATTR_IF_IDX]);
958 entry->addr.ifindex = val;
961 if (tb[MPTCP_PM_ADDR_ATTR_ID])
962 entry->addr.id = nla_get_u8(tb[MPTCP_PM_ADDR_ATTR_ID]);
964 if (tb[MPTCP_PM_ADDR_ATTR_FLAGS])
965 entry->addr.flags = nla_get_u32(tb[MPTCP_PM_ADDR_ATTR_FLAGS]);
967 if (tb[MPTCP_PM_ADDR_ATTR_PORT])
968 entry->addr.port = htons(nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_PORT]));
973 static struct pm_nl_pernet *genl_info_pm_nl(struct genl_info *info)
975 return net_generic(genl_info_net(info), pm_nl_pernet_id);
978 static int mptcp_nl_add_subflow_or_signal_addr(struct net *net)
980 struct mptcp_sock *msk;
981 long s_slot = 0, s_num = 0;
983 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
984 struct sock *sk = (struct sock *)msk;
986 if (!READ_ONCE(msk->fully_established))
990 spin_lock_bh(&msk->pm.lock);
991 mptcp_pm_create_subflow_or_signal_addr(msk);
992 spin_unlock_bh(&msk->pm.lock);
1003 static int mptcp_nl_cmd_add_addr(struct sk_buff *skb, struct genl_info *info)
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;
1010 ret = mptcp_pm_parse_addr(attr, info, true, &addr);
1014 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1016 GENL_SET_ERR_MSG(info, "can't allocate addr");
1021 if (entry->addr.port) {
1022 ret = mptcp_pm_nl_create_listen_socket(skb->sk, entry);
1024 GENL_SET_ERR_MSG(info, "create listen socket error");
1029 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
1031 GENL_SET_ERR_MSG(info, "too many addresses or duplicate one");
1033 sock_release(entry->lsk);
1038 mptcp_nl_add_subflow_or_signal_addr(sock_net(skb->sk));
1043 static struct mptcp_pm_addr_entry *
1044 __lookup_addr_by_id(struct pm_nl_pernet *pernet, unsigned int id)
1046 struct mptcp_pm_addr_entry *entry;
1048 list_for_each_entry(entry, &pernet->local_addr_list, list) {
1049 if (entry->addr.id == id)
1055 static bool remove_anno_list_by_saddr(struct mptcp_sock *msk,
1056 struct mptcp_addr_info *addr)
1058 struct mptcp_pm_add_entry *entry;
1060 entry = mptcp_pm_del_add_timer(msk, addr);
1062 list_del(&entry->list);
1070 static bool mptcp_pm_remove_anno_addr(struct mptcp_sock *msk,
1071 struct mptcp_addr_info *addr,
1076 ret = remove_anno_list_by_saddr(msk, addr);
1078 spin_lock_bh(&msk->pm.lock);
1079 mptcp_pm_remove_addr(msk, addr->id);
1080 spin_unlock_bh(&msk->pm.lock);
1085 static int mptcp_nl_remove_subflow_and_signal_addr(struct net *net,
1086 struct mptcp_addr_info *addr)
1088 struct mptcp_sock *msk;
1089 long s_slot = 0, s_num = 0;
1091 pr_debug("remove_id=%d", addr->id);
1093 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1094 struct sock *sk = (struct sock *)msk;
1095 bool remove_subflow;
1097 if (list_empty(&msk->conn_list)) {
1098 mptcp_pm_remove_anno_addr(msk, addr, false);
1103 remove_subflow = lookup_subflow_by_saddr(&msk->conn_list, addr);
1104 mptcp_pm_remove_anno_addr(msk, addr, remove_subflow);
1106 mptcp_pm_remove_subflow(msk, addr->id);
1117 struct addr_entry_release_work {
1118 struct rcu_work rwork;
1119 struct mptcp_pm_addr_entry *entry;
1122 static void mptcp_pm_release_addr_entry(struct work_struct *work)
1124 struct addr_entry_release_work *w;
1125 struct mptcp_pm_addr_entry *entry;
1127 w = container_of(to_rcu_work(work), struct addr_entry_release_work, rwork);
1131 sock_release(entry->lsk);
1137 static void mptcp_pm_free_addr_entry(struct mptcp_pm_addr_entry *entry)
1139 struct addr_entry_release_work *w;
1141 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1143 INIT_RCU_WORK(&w->rwork, mptcp_pm_release_addr_entry);
1145 queue_rcu_work(system_wq, &w->rwork);
1149 static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info)
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;
1157 ret = mptcp_pm_parse_addr(attr, info, false, &addr);
1161 spin_lock_bh(&pernet->lock);
1162 entry = __lookup_addr_by_id(pernet, addr.addr.id);
1164 GENL_SET_ERR_MSG(info, "address not found");
1165 spin_unlock_bh(&pernet->lock);
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);
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);
1178 list_del_rcu(&entry->list);
1179 __clear_bit(entry->addr.id, pernet->id_bitmap);
1180 spin_unlock_bh(&pernet->lock);
1182 mptcp_nl_remove_subflow_and_signal_addr(sock_net(skb->sk), &entry->addr);
1183 mptcp_pm_free_addr_entry(entry);
1188 static void __flush_addrs(struct net *net, struct list_head *list)
1190 while (!list_empty(list)) {
1191 struct mptcp_pm_addr_entry *cur;
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);
1201 static void __reset_counters(struct pm_nl_pernet *pernet)
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);
1209 static int mptcp_nl_cmd_flush_addrs(struct sk_buff *skb, struct genl_info *info)
1211 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1212 LIST_HEAD(free_list);
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);
1224 static int mptcp_nl_fill_addr(struct sk_buff *skb,
1225 struct mptcp_pm_addr_entry *entry)
1227 struct mptcp_addr_info *addr = &entry->addr;
1228 struct nlattr *attr;
1230 attr = nla_nest_start(skb, MPTCP_PM_ATTR_ADDR);
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;
1246 if (addr->family == AF_INET &&
1247 nla_put_in_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR4,
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;
1255 nla_nest_end(skb, attr);
1259 nla_nest_cancel(skb, attr);
1263 static int mptcp_nl_cmd_get_addr(struct sk_buff *skb, struct genl_info *info)
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;
1272 ret = mptcp_pm_parse_addr(attr, info, false, &addr);
1276 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1280 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1281 info->genlhdr->cmd);
1283 GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1288 spin_lock_bh(&pernet->lock);
1289 entry = __lookup_addr_by_id(pernet, addr.addr.id);
1291 GENL_SET_ERR_MSG(info, "address not found");
1296 ret = mptcp_nl_fill_addr(msg, entry);
1300 genlmsg_end(msg, reply);
1301 ret = genlmsg_reply(msg, info);
1302 spin_unlock_bh(&pernet->lock);
1306 spin_unlock_bh(&pernet->lock);
1313 static int mptcp_nl_cmd_dump_addrs(struct sk_buff *msg,
1314 struct netlink_callback *cb)
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];
1323 pernet = net_generic(net, pm_nl_pernet_id);
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);
1332 if (entry->addr.id <= id)
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);
1341 if (mptcp_nl_fill_addr(msg, entry) < 0) {
1342 genlmsg_cancel(msg, hdr);
1346 id = entry->addr.id;
1347 genlmsg_end(msg, hdr);
1350 spin_unlock_bh(&pernet->lock);
1356 static int parse_limit(struct genl_info *info, int id, unsigned int *limit)
1358 struct nlattr *attr = info->attrs[id];
1363 *limit = nla_get_u32(attr);
1364 if (*limit > MPTCP_PM_ADDR_MAX) {
1365 GENL_SET_ERR_MSG(info, "limit greater than maximum");
1372 mptcp_nl_cmd_set_limits(struct sk_buff *skb, struct genl_info *info)
1374 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1375 unsigned int rcv_addrs, subflows;
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);
1384 subflows = pernet->subflows_max;
1385 ret = parse_limit(info, MPTCP_PM_ATTR_SUBFLOWS, &subflows);
1389 WRITE_ONCE(pernet->add_addr_accept_max, rcv_addrs);
1390 WRITE_ONCE(pernet->subflows_max, subflows);
1393 spin_unlock_bh(&pernet->lock);
1398 mptcp_nl_cmd_get_limits(struct sk_buff *skb, struct genl_info *info)
1400 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1401 struct sk_buff *msg;
1404 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1408 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1409 MPTCP_PM_CMD_GET_LIMITS);
1413 if (nla_put_u32(msg, MPTCP_PM_ATTR_RCV_ADD_ADDRS,
1414 READ_ONCE(pernet->add_addr_accept_max)))
1417 if (nla_put_u32(msg, MPTCP_PM_ATTR_SUBFLOWS,
1418 READ_ONCE(pernet->subflows_max)))
1421 genlmsg_end(msg, reply);
1422 return genlmsg_reply(msg, info);
1425 GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1430 static int mptcp_nl_addr_backup(struct net *net,
1431 struct mptcp_addr_info *addr,
1434 long s_slot = 0, s_num = 0;
1435 struct mptcp_sock *msk;
1438 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1439 struct sock *sk = (struct sock *)msk;
1441 if (list_empty(&msk->conn_list))
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);
1458 static int mptcp_nl_cmd_set_flags(struct sk_buff *skb, struct genl_info *info)
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);
1467 ret = mptcp_pm_parse_addr(attr, info, true, &addr);
1471 if (addr.addr.flags & MPTCP_PM_ADDR_FLAG_BACKUP)
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);
1481 entry->addr.flags |= MPTCP_PM_ADDR_FLAG_BACKUP;
1483 entry->addr.flags &= ~MPTCP_PM_ADDR_FLAG_BACKUP;
1490 static void mptcp_nl_mcast_send(struct net *net, struct sk_buff *nlskb, gfp_t gfp)
1492 genlmsg_multicast_netns(&mptcp_genl_family, net,
1493 nlskb, 0, MPTCP_PM_EV_GRP_OFFSET, gfp);
1496 static int mptcp_event_add_subflow(struct sk_buff *skb, const struct sock *ssk)
1498 const struct inet_sock *issk = inet_sk(ssk);
1499 const struct mptcp_subflow_context *sf;
1501 if (nla_put_u16(skb, MPTCP_ATTR_FAMILY, ssk->sk_family))
1504 switch (ssk->sk_family) {
1506 if (nla_put_in_addr(skb, MPTCP_ATTR_SADDR4, issk->inet_saddr))
1508 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, issk->inet_daddr))
1511 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1513 const struct ipv6_pinfo *np = inet6_sk(ssk);
1515 if (nla_put_in6_addr(skb, MPTCP_ATTR_SADDR6, &np->saddr))
1517 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &ssk->sk_v6_daddr))
1527 if (nla_put_be16(skb, MPTCP_ATTR_SPORT, issk->inet_sport))
1529 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, issk->inet_dport))
1532 sf = mptcp_subflow_ctx(ssk);
1533 if (WARN_ON_ONCE(!sf))
1536 if (nla_put_u8(skb, MPTCP_ATTR_LOC_ID, sf->local_id))
1539 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, sf->remote_id))
1545 static int mptcp_event_put_token_and_ssk(struct sk_buff *skb,
1546 const struct mptcp_sock *msk,
1547 const struct sock *ssk)
1549 const struct sock *sk = (const struct sock *)msk;
1550 const struct mptcp_subflow_context *sf;
1553 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1556 if (mptcp_event_add_subflow(skb, ssk))
1559 sf = mptcp_subflow_ctx(ssk);
1560 if (WARN_ON_ONCE(!sf))
1563 if (nla_put_u8(skb, MPTCP_ATTR_BACKUP, sf->backup))
1566 if (ssk->sk_bound_dev_if &&
1567 nla_put_s32(skb, MPTCP_ATTR_IF_IDX, ssk->sk_bound_dev_if))
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))
1578 static int mptcp_event_sub_established(struct sk_buff *skb,
1579 const struct mptcp_sock *msk,
1580 const struct sock *ssk)
1582 return mptcp_event_put_token_and_ssk(skb, msk, ssk);
1585 static int mptcp_event_sub_closed(struct sk_buff *skb,
1586 const struct mptcp_sock *msk,
1587 const struct sock *ssk)
1589 if (mptcp_event_put_token_and_ssk(skb, msk, ssk))
1595 static int mptcp_event_created(struct sk_buff *skb,
1596 const struct mptcp_sock *msk,
1597 const struct sock *ssk)
1599 int err = nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token);
1604 return mptcp_event_add_subflow(skb, ssk);
1607 void mptcp_event_addr_removed(const struct mptcp_sock *msk, uint8_t id)
1609 struct net *net = sock_net((const struct sock *)msk);
1610 struct nlmsghdr *nlh;
1611 struct sk_buff *skb;
1613 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
1616 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1620 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, MPTCP_EVENT_REMOVED);
1622 goto nla_put_failure;
1624 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1625 goto nla_put_failure;
1627 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, id))
1628 goto nla_put_failure;
1630 genlmsg_end(skb, nlh);
1631 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC);
1638 void mptcp_event_addr_announced(const struct mptcp_sock *msk,
1639 const struct mptcp_addr_info *info)
1641 struct net *net = sock_net((const struct sock *)msk);
1642 struct nlmsghdr *nlh;
1643 struct sk_buff *skb;
1645 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
1648 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1652 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0,
1653 MPTCP_EVENT_ANNOUNCED);
1655 goto nla_put_failure;
1657 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1658 goto nla_put_failure;
1660 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, info->id))
1661 goto nla_put_failure;
1663 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, info->port))
1664 goto nla_put_failure;
1666 switch (info->family) {
1668 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, info->addr.s_addr))
1669 goto nla_put_failure;
1671 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1673 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &info->addr6))
1674 goto nla_put_failure;
1679 goto nla_put_failure;
1682 genlmsg_end(skb, nlh);
1683 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC);
1690 void mptcp_event(enum mptcp_event_type type, const struct mptcp_sock *msk,
1691 const struct sock *ssk, gfp_t gfp)
1693 struct net *net = sock_net((const struct sock *)msk);
1694 struct nlmsghdr *nlh;
1695 struct sk_buff *skb;
1697 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
1700 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
1704 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, type);
1706 goto nla_put_failure;
1709 case MPTCP_EVENT_UNSPEC:
1712 case MPTCP_EVENT_CREATED:
1713 case MPTCP_EVENT_ESTABLISHED:
1714 if (mptcp_event_created(skb, msk, ssk) < 0)
1715 goto nla_put_failure;
1717 case MPTCP_EVENT_CLOSED:
1718 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token) < 0)
1719 goto nla_put_failure;
1721 case MPTCP_EVENT_ANNOUNCED:
1722 case MPTCP_EVENT_REMOVED:
1723 /* call mptcp_event_addr_announced()/removed instead */
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;
1731 case MPTCP_EVENT_SUB_CLOSED:
1732 if (mptcp_event_sub_closed(skb, msk, ssk) < 0)
1733 goto nla_put_failure;
1737 genlmsg_end(skb, nlh);
1738 mptcp_nl_mcast_send(net, skb, gfp);
1745 static const struct genl_small_ops mptcp_pm_ops[] = {
1747 .cmd = MPTCP_PM_CMD_ADD_ADDR,
1748 .doit = mptcp_nl_cmd_add_addr,
1749 .flags = GENL_ADMIN_PERM,
1752 .cmd = MPTCP_PM_CMD_DEL_ADDR,
1753 .doit = mptcp_nl_cmd_del_addr,
1754 .flags = GENL_ADMIN_PERM,
1757 .cmd = MPTCP_PM_CMD_FLUSH_ADDRS,
1758 .doit = mptcp_nl_cmd_flush_addrs,
1759 .flags = GENL_ADMIN_PERM,
1762 .cmd = MPTCP_PM_CMD_GET_ADDR,
1763 .doit = mptcp_nl_cmd_get_addr,
1764 .dumpit = mptcp_nl_cmd_dump_addrs,
1767 .cmd = MPTCP_PM_CMD_SET_LIMITS,
1768 .doit = mptcp_nl_cmd_set_limits,
1769 .flags = GENL_ADMIN_PERM,
1772 .cmd = MPTCP_PM_CMD_GET_LIMITS,
1773 .doit = mptcp_nl_cmd_get_limits,
1776 .cmd = MPTCP_PM_CMD_SET_FLAGS,
1777 .doit = mptcp_nl_cmd_set_flags,
1778 .flags = GENL_ADMIN_PERM,
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,
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),
1795 static int __net_init pm_nl_init_net(struct net *net)
1797 struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id);
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);
1807 static void __net_exit pm_nl_exit_net(struct list_head *net_list)
1811 list_for_each_entry(net, net_list, exit_list) {
1812 struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id);
1814 /* net is removed from namespace list, can't race with
1817 __flush_addrs(net, &pernet->local_addr_list);
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),
1828 void __init mptcp_pm_nl_init(void)
1830 if (register_pernet_subsys(&mptcp_pm_pernet_ops) < 0)
1831 panic("Failed to register MPTCP PM pernet subsystem.\n");
1833 if (genl_register_family(&mptcp_genl_family))
1834 panic("Failed to register MPTCP PM netlink family\n");