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>
11 #include <net/inet_common.h>
12 #include <net/netns/generic.h>
13 #include <net/mptcp.h>
17 #include "mptcp_pm_gen.h"
19 static int pm_nl_pernet_id;
21 struct mptcp_pm_add_entry {
22 struct list_head list;
23 struct mptcp_addr_info addr;
25 struct timer_list add_timer;
26 struct mptcp_sock *sock;
30 /* protects pernet updates */
32 struct list_head local_addr_list;
34 unsigned int stale_loss_cnt;
35 unsigned int add_addr_signal_max;
36 unsigned int add_addr_accept_max;
37 unsigned int local_addr_max;
38 unsigned int subflows_max;
40 DECLARE_BITMAP(id_bitmap, MPTCP_PM_MAX_ADDR_ID + 1);
43 #define MPTCP_PM_ADDR_MAX 8
44 #define ADD_ADDR_RETRANS_MAX 3
46 static struct pm_nl_pernet *pm_nl_get_pernet(const struct net *net)
48 return net_generic(net, pm_nl_pernet_id);
51 static struct pm_nl_pernet *
52 pm_nl_get_pernet_from_msk(const struct mptcp_sock *msk)
54 return pm_nl_get_pernet(sock_net((struct sock *)msk));
57 bool mptcp_addresses_equal(const struct mptcp_addr_info *a,
58 const struct mptcp_addr_info *b, bool use_port)
60 bool addr_equals = false;
62 if (a->family == b->family) {
63 if (a->family == AF_INET)
64 addr_equals = a->addr.s_addr == b->addr.s_addr;
65 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
67 addr_equals = !ipv6_addr_cmp(&a->addr6, &b->addr6);
68 } else if (a->family == AF_INET) {
69 if (ipv6_addr_v4mapped(&b->addr6))
70 addr_equals = a->addr.s_addr == b->addr6.s6_addr32[3];
71 } else if (b->family == AF_INET) {
72 if (ipv6_addr_v4mapped(&a->addr6))
73 addr_equals = a->addr6.s6_addr32[3] == b->addr.s_addr;
82 return a->port == b->port;
85 void mptcp_local_address(const struct sock_common *skc, struct mptcp_addr_info *addr)
87 addr->family = skc->skc_family;
88 addr->port = htons(skc->skc_num);
89 if (addr->family == AF_INET)
90 addr->addr.s_addr = skc->skc_rcv_saddr;
91 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
92 else if (addr->family == AF_INET6)
93 addr->addr6 = skc->skc_v6_rcv_saddr;
97 static void remote_address(const struct sock_common *skc,
98 struct mptcp_addr_info *addr)
100 addr->family = skc->skc_family;
101 addr->port = skc->skc_dport;
102 if (addr->family == AF_INET)
103 addr->addr.s_addr = skc->skc_daddr;
104 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
105 else if (addr->family == AF_INET6)
106 addr->addr6 = skc->skc_v6_daddr;
110 static bool lookup_subflow_by_saddr(const struct list_head *list,
111 const struct mptcp_addr_info *saddr)
113 struct mptcp_subflow_context *subflow;
114 struct mptcp_addr_info cur;
115 struct sock_common *skc;
117 list_for_each_entry(subflow, list, node) {
118 skc = (struct sock_common *)mptcp_subflow_tcp_sock(subflow);
120 mptcp_local_address(skc, &cur);
121 if (mptcp_addresses_equal(&cur, saddr, saddr->port))
128 static bool lookup_subflow_by_daddr(const struct list_head *list,
129 const struct mptcp_addr_info *daddr)
131 struct mptcp_subflow_context *subflow;
132 struct mptcp_addr_info cur;
133 struct sock_common *skc;
135 list_for_each_entry(subflow, list, node) {
136 skc = (struct sock_common *)mptcp_subflow_tcp_sock(subflow);
138 remote_address(skc, &cur);
139 if (mptcp_addresses_equal(&cur, daddr, daddr->port))
147 select_local_address(const struct pm_nl_pernet *pernet,
148 const struct mptcp_sock *msk,
149 struct mptcp_pm_addr_entry *new_entry)
151 struct mptcp_pm_addr_entry *entry;
154 msk_owned_by_me(msk);
157 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
158 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW))
161 if (!test_bit(entry->addr.id, msk->pm.id_avail_bitmap))
174 select_signal_address(struct pm_nl_pernet *pernet, const struct mptcp_sock *msk,
175 struct mptcp_pm_addr_entry *new_entry)
177 struct mptcp_pm_addr_entry *entry;
181 /* do not keep any additional per socket state, just signal
182 * the address list in order.
183 * Note: removal from the local address list during the msk life-cycle
184 * can lead to additional addresses not being announced.
186 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
187 if (!test_bit(entry->addr.id, msk->pm.id_avail_bitmap))
190 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL))
202 unsigned int mptcp_pm_get_add_addr_signal_max(const struct mptcp_sock *msk)
204 const struct pm_nl_pernet *pernet = pm_nl_get_pernet_from_msk(msk);
206 return READ_ONCE(pernet->add_addr_signal_max);
208 EXPORT_SYMBOL_GPL(mptcp_pm_get_add_addr_signal_max);
210 unsigned int mptcp_pm_get_add_addr_accept_max(const struct mptcp_sock *msk)
212 struct pm_nl_pernet *pernet = pm_nl_get_pernet_from_msk(msk);
214 return READ_ONCE(pernet->add_addr_accept_max);
216 EXPORT_SYMBOL_GPL(mptcp_pm_get_add_addr_accept_max);
218 unsigned int mptcp_pm_get_subflows_max(const struct mptcp_sock *msk)
220 struct pm_nl_pernet *pernet = pm_nl_get_pernet_from_msk(msk);
222 return READ_ONCE(pernet->subflows_max);
224 EXPORT_SYMBOL_GPL(mptcp_pm_get_subflows_max);
226 unsigned int mptcp_pm_get_local_addr_max(const struct mptcp_sock *msk)
228 struct pm_nl_pernet *pernet = pm_nl_get_pernet_from_msk(msk);
230 return READ_ONCE(pernet->local_addr_max);
232 EXPORT_SYMBOL_GPL(mptcp_pm_get_local_addr_max);
234 bool mptcp_pm_nl_check_work_pending(struct mptcp_sock *msk)
236 struct pm_nl_pernet *pernet = pm_nl_get_pernet_from_msk(msk);
238 if (msk->pm.subflows == mptcp_pm_get_subflows_max(msk) ||
239 (find_next_and_bit(pernet->id_bitmap, msk->pm.id_avail_bitmap,
240 MPTCP_PM_MAX_ADDR_ID + 1, 0) == MPTCP_PM_MAX_ADDR_ID + 1)) {
241 WRITE_ONCE(msk->pm.work_pending, false);
247 struct mptcp_pm_add_entry *
248 mptcp_lookup_anno_list_by_saddr(const struct mptcp_sock *msk,
249 const struct mptcp_addr_info *addr)
251 struct mptcp_pm_add_entry *entry;
253 lockdep_assert_held(&msk->pm.lock);
255 list_for_each_entry(entry, &msk->pm.anno_list, list) {
256 if (mptcp_addresses_equal(&entry->addr, addr, true))
263 bool mptcp_pm_sport_in_anno_list(struct mptcp_sock *msk, const struct sock *sk)
265 struct mptcp_pm_add_entry *entry;
266 struct mptcp_addr_info saddr;
269 mptcp_local_address((struct sock_common *)sk, &saddr);
271 spin_lock_bh(&msk->pm.lock);
272 list_for_each_entry(entry, &msk->pm.anno_list, list) {
273 if (mptcp_addresses_equal(&entry->addr, &saddr, true)) {
280 spin_unlock_bh(&msk->pm.lock);
284 static void mptcp_pm_add_timer(struct timer_list *timer)
286 struct mptcp_pm_add_entry *entry = from_timer(entry, timer, add_timer);
287 struct mptcp_sock *msk = entry->sock;
288 struct sock *sk = (struct sock *)msk;
290 pr_debug("msk=%p", msk);
295 if (inet_sk_state_load(sk) == TCP_CLOSE)
301 if (mptcp_pm_should_add_signal_addr(msk)) {
302 sk_reset_timer(sk, timer, jiffies + TCP_RTO_MAX / 8);
306 spin_lock_bh(&msk->pm.lock);
308 if (!mptcp_pm_should_add_signal_addr(msk)) {
309 pr_debug("retransmit ADD_ADDR id=%d", entry->addr.id);
310 mptcp_pm_announce_addr(msk, &entry->addr, false);
311 mptcp_pm_add_addr_send_ack(msk);
312 entry->retrans_times++;
315 if (entry->retrans_times < ADD_ADDR_RETRANS_MAX)
316 sk_reset_timer(sk, timer,
317 jiffies + mptcp_get_add_addr_timeout(sock_net(sk)));
319 spin_unlock_bh(&msk->pm.lock);
321 if (entry->retrans_times == ADD_ADDR_RETRANS_MAX)
322 mptcp_pm_subflow_established(msk);
328 struct mptcp_pm_add_entry *
329 mptcp_pm_del_add_timer(struct mptcp_sock *msk,
330 const struct mptcp_addr_info *addr, bool check_id)
332 struct mptcp_pm_add_entry *entry;
333 struct sock *sk = (struct sock *)msk;
335 spin_lock_bh(&msk->pm.lock);
336 entry = mptcp_lookup_anno_list_by_saddr(msk, addr);
337 if (entry && (!check_id || entry->addr.id == addr->id))
338 entry->retrans_times = ADD_ADDR_RETRANS_MAX;
339 spin_unlock_bh(&msk->pm.lock);
341 if (entry && (!check_id || entry->addr.id == addr->id))
342 sk_stop_timer_sync(sk, &entry->add_timer);
347 bool mptcp_pm_alloc_anno_list(struct mptcp_sock *msk,
348 const struct mptcp_addr_info *addr)
350 struct mptcp_pm_add_entry *add_entry = NULL;
351 struct sock *sk = (struct sock *)msk;
352 struct net *net = sock_net(sk);
354 lockdep_assert_held(&msk->pm.lock);
356 add_entry = mptcp_lookup_anno_list_by_saddr(msk, addr);
359 if (WARN_ON_ONCE(mptcp_pm_is_kernel(msk)))
362 sk_reset_timer(sk, &add_entry->add_timer,
363 jiffies + mptcp_get_add_addr_timeout(net));
367 add_entry = kmalloc(sizeof(*add_entry), GFP_ATOMIC);
371 list_add(&add_entry->list, &msk->pm.anno_list);
373 add_entry->addr = *addr;
374 add_entry->sock = msk;
375 add_entry->retrans_times = 0;
377 timer_setup(&add_entry->add_timer, mptcp_pm_add_timer, 0);
378 sk_reset_timer(sk, &add_entry->add_timer,
379 jiffies + mptcp_get_add_addr_timeout(net));
384 void mptcp_pm_free_anno_list(struct mptcp_sock *msk)
386 struct mptcp_pm_add_entry *entry, *tmp;
387 struct sock *sk = (struct sock *)msk;
388 LIST_HEAD(free_list);
390 pr_debug("msk=%p", msk);
392 spin_lock_bh(&msk->pm.lock);
393 list_splice_init(&msk->pm.anno_list, &free_list);
394 spin_unlock_bh(&msk->pm.lock);
396 list_for_each_entry_safe(entry, tmp, &free_list, list) {
397 sk_stop_timer_sync(sk, &entry->add_timer);
402 /* Fill all the remote addresses into the array addrs[],
403 * and return the array size.
405 static unsigned int fill_remote_addresses_vec(struct mptcp_sock *msk,
406 struct mptcp_addr_info *local,
408 struct mptcp_addr_info *addrs)
410 bool deny_id0 = READ_ONCE(msk->pm.remote_deny_join_id0);
411 struct sock *sk = (struct sock *)msk, *ssk;
412 struct mptcp_subflow_context *subflow;
413 struct mptcp_addr_info remote = { 0 };
414 unsigned int subflows_max;
417 subflows_max = mptcp_pm_get_subflows_max(msk);
418 remote_address((struct sock_common *)sk, &remote);
420 /* Non-fullmesh endpoint, fill in the single entry
421 * corresponding to the primary MPC subflow remote address
427 if (!mptcp_pm_addr_families_match(sk, local, &remote))
433 DECLARE_BITMAP(unavail_id, MPTCP_PM_MAX_ADDR_ID + 1);
435 /* Forbid creation of new subflows matching existing
436 * ones, possibly already created by incoming ADD_ADDR
438 bitmap_zero(unavail_id, MPTCP_PM_MAX_ADDR_ID + 1);
439 mptcp_for_each_subflow(msk, subflow)
440 if (READ_ONCE(subflow->local_id) == local->id)
441 __set_bit(subflow->remote_id, unavail_id);
443 mptcp_for_each_subflow(msk, subflow) {
444 ssk = mptcp_subflow_tcp_sock(subflow);
445 remote_address((struct sock_common *)ssk, &addrs[i]);
446 addrs[i].id = READ_ONCE(subflow->remote_id);
447 if (deny_id0 && !addrs[i].id)
450 if (test_bit(addrs[i].id, unavail_id))
453 if (!mptcp_pm_addr_families_match(sk, local, &addrs[i]))
456 if (msk->pm.subflows < subflows_max) {
457 /* forbid creating multiple address towards
460 __set_bit(addrs[i].id, unavail_id);
470 static void __mptcp_pm_send_ack(struct mptcp_sock *msk, struct mptcp_subflow_context *subflow,
471 bool prio, bool backup)
473 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
476 pr_debug("send ack for %s",
477 prio ? "mp_prio" : (mptcp_pm_should_add_signal(msk) ? "add_addr" : "rm_addr"));
479 slow = lock_sock_fast(ssk);
481 subflow->send_mp_prio = 1;
482 subflow->request_bkup = backup;
485 __mptcp_subflow_send_ack(ssk);
486 unlock_sock_fast(ssk, slow);
489 static void mptcp_pm_send_ack(struct mptcp_sock *msk, struct mptcp_subflow_context *subflow,
490 bool prio, bool backup)
492 spin_unlock_bh(&msk->pm.lock);
493 __mptcp_pm_send_ack(msk, subflow, prio, backup);
494 spin_lock_bh(&msk->pm.lock);
497 static struct mptcp_pm_addr_entry *
498 __lookup_addr_by_id(struct pm_nl_pernet *pernet, unsigned int id)
500 struct mptcp_pm_addr_entry *entry;
502 list_for_each_entry(entry, &pernet->local_addr_list, list) {
503 if (entry->addr.id == id)
509 static struct mptcp_pm_addr_entry *
510 __lookup_addr(struct pm_nl_pernet *pernet, const struct mptcp_addr_info *info)
512 struct mptcp_pm_addr_entry *entry;
514 list_for_each_entry(entry, &pernet->local_addr_list, list) {
515 if (mptcp_addresses_equal(&entry->addr, info, entry->addr.port))
521 static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk)
523 struct sock *sk = (struct sock *)msk;
524 struct mptcp_pm_addr_entry local;
525 unsigned int add_addr_signal_max;
526 bool signal_and_subflow = false;
527 unsigned int local_addr_max;
528 struct pm_nl_pernet *pernet;
529 unsigned int subflows_max;
531 pernet = pm_nl_get_pernet(sock_net(sk));
533 add_addr_signal_max = mptcp_pm_get_add_addr_signal_max(msk);
534 local_addr_max = mptcp_pm_get_local_addr_max(msk);
535 subflows_max = mptcp_pm_get_subflows_max(msk);
537 /* do lazy endpoint usage accounting for the MPC subflows */
538 if (unlikely(!(msk->pm.status & BIT(MPTCP_PM_MPC_ENDPOINT_ACCOUNTED))) && msk->first) {
539 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(msk->first);
540 struct mptcp_pm_addr_entry *entry;
541 struct mptcp_addr_info mpc_addr;
544 mptcp_local_address((struct sock_common *)msk->first, &mpc_addr);
546 entry = __lookup_addr(pernet, &mpc_addr);
548 __clear_bit(entry->addr.id, msk->pm.id_avail_bitmap);
549 msk->mpc_endpoint_id = entry->addr.id;
550 backup = !!(entry->flags & MPTCP_PM_ADDR_FLAG_BACKUP);
555 mptcp_pm_send_ack(msk, subflow, true, backup);
557 msk->pm.status |= BIT(MPTCP_PM_MPC_ENDPOINT_ACCOUNTED);
560 pr_debug("local %d:%d signal %d:%d subflows %d:%d\n",
561 msk->pm.local_addr_used, local_addr_max,
562 msk->pm.add_addr_signaled, add_addr_signal_max,
563 msk->pm.subflows, subflows_max);
565 /* check first for announce */
566 if (msk->pm.add_addr_signaled < add_addr_signal_max) {
567 /* due to racing events on both ends we can reach here while
568 * previous add address is still running: if we invoke now
569 * mptcp_pm_announce_addr(), that will fail and the
570 * corresponding id will be marked as used.
571 * Instead let the PM machinery reschedule us when the
572 * current address announce will be completed.
574 if (msk->pm.addr_signal & BIT(MPTCP_ADD_ADDR_SIGNAL))
577 if (!select_signal_address(pernet, msk, &local))
580 /* If the alloc fails, we are on memory pressure, not worth
581 * continuing, and trying to create subflows.
583 if (!mptcp_pm_alloc_anno_list(msk, &local.addr))
586 __clear_bit(local.addr.id, msk->pm.id_avail_bitmap);
587 msk->pm.add_addr_signaled++;
588 mptcp_pm_announce_addr(msk, &local.addr, false);
589 mptcp_pm_nl_addr_send_ack(msk);
591 if (local.flags & MPTCP_PM_ADDR_FLAG_SUBFLOW)
592 signal_and_subflow = true;
596 /* check if should create a new subflow */
597 while (msk->pm.local_addr_used < local_addr_max &&
598 msk->pm.subflows < subflows_max) {
599 struct mptcp_addr_info addrs[MPTCP_PM_ADDR_MAX];
603 if (signal_and_subflow)
604 signal_and_subflow = false;
605 else if (!select_local_address(pernet, msk, &local))
608 fullmesh = !!(local.flags & MPTCP_PM_ADDR_FLAG_FULLMESH);
610 msk->pm.local_addr_used++;
611 __clear_bit(local.addr.id, msk->pm.id_avail_bitmap);
612 nr = fill_remote_addresses_vec(msk, &local.addr, fullmesh, addrs);
616 spin_unlock_bh(&msk->pm.lock);
617 for (i = 0; i < nr; i++)
618 __mptcp_subflow_connect(sk, &local.addr, &addrs[i]);
619 spin_lock_bh(&msk->pm.lock);
621 mptcp_pm_nl_check_work_pending(msk);
624 static void mptcp_pm_nl_fully_established(struct mptcp_sock *msk)
626 mptcp_pm_create_subflow_or_signal_addr(msk);
629 static void mptcp_pm_nl_subflow_established(struct mptcp_sock *msk)
631 mptcp_pm_create_subflow_or_signal_addr(msk);
634 /* Fill all the local addresses into the array addrs[],
635 * and return the array size.
637 static unsigned int fill_local_addresses_vec(struct mptcp_sock *msk,
638 struct mptcp_addr_info *remote,
639 struct mptcp_addr_info *addrs)
641 struct sock *sk = (struct sock *)msk;
642 struct mptcp_pm_addr_entry *entry;
643 struct mptcp_addr_info mpc_addr;
644 struct pm_nl_pernet *pernet;
645 unsigned int subflows_max;
648 pernet = pm_nl_get_pernet_from_msk(msk);
649 subflows_max = mptcp_pm_get_subflows_max(msk);
651 mptcp_local_address((struct sock_common *)msk, &mpc_addr);
654 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
655 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_FULLMESH))
658 if (!mptcp_pm_addr_families_match(sk, &entry->addr, remote))
661 if (msk->pm.subflows < subflows_max) {
663 addrs[i] = entry->addr;
665 /* Special case for ID0: set the correct ID */
666 if (mptcp_addresses_equal(&entry->addr, &mpc_addr, entry->addr.port))
674 /* If the array is empty, fill in the single
675 * 'IPADDRANY' local address
678 struct mptcp_addr_info local;
680 memset(&local, 0, sizeof(local));
682 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
683 remote->family == AF_INET6 &&
684 ipv6_addr_v4mapped(&remote->addr6) ? AF_INET :
688 if (!mptcp_pm_addr_families_match(sk, &local, remote))
698 static void mptcp_pm_nl_add_addr_received(struct mptcp_sock *msk)
700 struct mptcp_addr_info addrs[MPTCP_PM_ADDR_MAX];
701 struct sock *sk = (struct sock *)msk;
702 unsigned int add_addr_accept_max;
703 struct mptcp_addr_info remote;
704 unsigned int subflows_max;
705 bool sf_created = false;
708 add_addr_accept_max = mptcp_pm_get_add_addr_accept_max(msk);
709 subflows_max = mptcp_pm_get_subflows_max(msk);
711 pr_debug("accepted %d:%d remote family %d",
712 msk->pm.add_addr_accepted, add_addr_accept_max,
713 msk->pm.remote.family);
715 remote = msk->pm.remote;
716 mptcp_pm_announce_addr(msk, &remote, true);
717 mptcp_pm_nl_addr_send_ack(msk);
719 if (lookup_subflow_by_daddr(&msk->conn_list, &remote))
722 /* pick id 0 port, if none is provided the remote address */
724 remote.port = sk->sk_dport;
726 /* connect to the specified remote address, using whatever
727 * local address the routing configuration will pick.
729 nr = fill_local_addresses_vec(msk, &remote, addrs);
733 spin_unlock_bh(&msk->pm.lock);
734 for (i = 0; i < nr; i++)
735 if (__mptcp_subflow_connect(sk, &addrs[i], &remote) == 0)
737 spin_lock_bh(&msk->pm.lock);
740 msk->pm.add_addr_accepted++;
741 if (msk->pm.add_addr_accepted >= add_addr_accept_max ||
742 msk->pm.subflows >= subflows_max)
743 WRITE_ONCE(msk->pm.accept_addr, false);
747 void mptcp_pm_nl_addr_send_ack(struct mptcp_sock *msk)
749 struct mptcp_subflow_context *subflow;
751 msk_owned_by_me(msk);
752 lockdep_assert_held(&msk->pm.lock);
754 if (!mptcp_pm_should_add_signal(msk) &&
755 !mptcp_pm_should_rm_signal(msk))
758 subflow = list_first_entry_or_null(&msk->conn_list, typeof(*subflow), node);
760 mptcp_pm_send_ack(msk, subflow, false, false);
763 int mptcp_pm_nl_mp_prio_send_ack(struct mptcp_sock *msk,
764 struct mptcp_addr_info *addr,
765 struct mptcp_addr_info *rem,
768 struct mptcp_subflow_context *subflow;
770 pr_debug("bkup=%d", bkup);
772 mptcp_for_each_subflow(msk, subflow) {
773 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
774 struct mptcp_addr_info local, remote;
776 mptcp_local_address((struct sock_common *)ssk, &local);
777 if (!mptcp_addresses_equal(&local, addr, addr->port))
780 if (rem && rem->family != AF_UNSPEC) {
781 remote_address((struct sock_common *)ssk, &remote);
782 if (!mptcp_addresses_equal(&remote, rem, rem->port))
786 __mptcp_pm_send_ack(msk, subflow, true, bkup);
793 static bool mptcp_local_id_match(const struct mptcp_sock *msk, u8 local_id, u8 id)
795 return local_id == id || (!local_id && msk->mpc_endpoint_id == id);
798 static void mptcp_pm_nl_rm_addr_or_subflow(struct mptcp_sock *msk,
799 const struct mptcp_rm_list *rm_list,
800 enum linux_mptcp_mib_field rm_type)
802 struct mptcp_subflow_context *subflow, *tmp;
803 struct sock *sk = (struct sock *)msk;
806 pr_debug("%s rm_list_nr %d",
807 rm_type == MPTCP_MIB_RMADDR ? "address" : "subflow", rm_list->nr);
809 msk_owned_by_me(msk);
811 if (sk->sk_state == TCP_LISTEN)
817 if (list_empty(&msk->conn_list))
820 for (i = 0; i < rm_list->nr; i++) {
821 u8 rm_id = rm_list->ids[i];
822 bool removed = false;
824 mptcp_for_each_subflow_safe(msk, subflow, tmp) {
825 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
826 u8 remote_id = READ_ONCE(subflow->remote_id);
827 int how = RCV_SHUTDOWN | SEND_SHUTDOWN;
828 u8 id = subflow_get_local_id(subflow);
830 if (rm_type == MPTCP_MIB_RMADDR && remote_id != rm_id)
832 if (rm_type == MPTCP_MIB_RMSUBFLOW && !mptcp_local_id_match(msk, id, rm_id))
835 pr_debug(" -> %s rm_list_ids[%d]=%u local_id=%u remote_id=%u mpc_id=%u",
836 rm_type == MPTCP_MIB_RMADDR ? "address" : "subflow",
837 i, rm_id, id, remote_id, msk->mpc_endpoint_id);
838 spin_unlock_bh(&msk->pm.lock);
839 mptcp_subflow_shutdown(sk, ssk, how);
841 /* the following takes care of updating the subflows counter */
842 mptcp_close_ssk(sk, ssk, subflow);
843 spin_lock_bh(&msk->pm.lock);
845 removed |= subflow->request_join;
846 if (rm_type == MPTCP_MIB_RMSUBFLOW)
847 __MPTCP_INC_STATS(sock_net(sk), rm_type);
850 if (rm_type == MPTCP_MIB_RMADDR)
851 __MPTCP_INC_STATS(sock_net(sk), rm_type);
856 if (!mptcp_pm_is_kernel(msk))
859 if (rm_type == MPTCP_MIB_RMADDR && rm_id &&
860 !WARN_ON_ONCE(msk->pm.add_addr_accepted == 0)) {
861 /* Note: if the subflow has been closed before, this
862 * add_addr_accepted counter will not be decremented.
864 if (--msk->pm.add_addr_accepted < mptcp_pm_get_add_addr_accept_max(msk))
865 WRITE_ONCE(msk->pm.accept_addr, true);
870 static void mptcp_pm_nl_rm_addr_received(struct mptcp_sock *msk)
872 mptcp_pm_nl_rm_addr_or_subflow(msk, &msk->pm.rm_list_rx, MPTCP_MIB_RMADDR);
875 static void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk,
876 const struct mptcp_rm_list *rm_list)
878 mptcp_pm_nl_rm_addr_or_subflow(msk, rm_list, MPTCP_MIB_RMSUBFLOW);
881 void mptcp_pm_nl_work(struct mptcp_sock *msk)
883 struct mptcp_pm_data *pm = &msk->pm;
885 msk_owned_by_me(msk);
887 if (!(pm->status & MPTCP_PM_WORK_MASK))
890 spin_lock_bh(&msk->pm.lock);
892 pr_debug("msk=%p status=%x", msk, pm->status);
893 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_RECEIVED)) {
894 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_RECEIVED);
895 mptcp_pm_nl_add_addr_received(msk);
897 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_SEND_ACK)) {
898 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_SEND_ACK);
899 mptcp_pm_nl_addr_send_ack(msk);
901 if (pm->status & BIT(MPTCP_PM_RM_ADDR_RECEIVED)) {
902 pm->status &= ~BIT(MPTCP_PM_RM_ADDR_RECEIVED);
903 mptcp_pm_nl_rm_addr_received(msk);
905 if (pm->status & BIT(MPTCP_PM_ESTABLISHED)) {
906 pm->status &= ~BIT(MPTCP_PM_ESTABLISHED);
907 mptcp_pm_nl_fully_established(msk);
909 if (pm->status & BIT(MPTCP_PM_SUBFLOW_ESTABLISHED)) {
910 pm->status &= ~BIT(MPTCP_PM_SUBFLOW_ESTABLISHED);
911 mptcp_pm_nl_subflow_established(msk);
914 spin_unlock_bh(&msk->pm.lock);
917 static bool address_use_port(struct mptcp_pm_addr_entry *entry)
919 return (entry->flags &
920 (MPTCP_PM_ADDR_FLAG_SIGNAL | MPTCP_PM_ADDR_FLAG_SUBFLOW)) ==
921 MPTCP_PM_ADDR_FLAG_SIGNAL;
924 /* caller must ensure the RCU grace period is already elapsed */
925 static void __mptcp_pm_release_addr_entry(struct mptcp_pm_addr_entry *entry)
928 sock_release(entry->lsk);
932 static int mptcp_pm_nl_append_new_local_addr(struct pm_nl_pernet *pernet,
933 struct mptcp_pm_addr_entry *entry,
936 struct mptcp_pm_addr_entry *cur, *del_entry = NULL;
937 unsigned int addr_max;
940 spin_lock_bh(&pernet->lock);
941 /* to keep the code simple, don't do IDR-like allocation for address ID,
942 * just bail when we exceed limits
944 if (pernet->next_id == MPTCP_PM_MAX_ADDR_ID)
946 if (pernet->addrs >= MPTCP_PM_ADDR_MAX) {
950 if (test_bit(entry->addr.id, pernet->id_bitmap)) {
955 /* do not insert duplicate address, differentiate on port only
958 if (!address_use_port(entry))
959 entry->addr.port = 0;
960 list_for_each_entry(cur, &pernet->local_addr_list, list) {
961 if (mptcp_addresses_equal(&cur->addr, &entry->addr,
962 cur->addr.port || entry->addr.port)) {
963 /* allow replacing the exiting endpoint only if such
964 * endpoint is an implicit one and the user-space
965 * did not provide an endpoint id
967 if (!(cur->flags & MPTCP_PM_ADDR_FLAG_IMPLICIT)) {
975 entry->addr.id = cur->addr.id;
976 list_del_rcu(&cur->list);
982 if (!entry->addr.id && needs_id) {
984 entry->addr.id = find_next_zero_bit(pernet->id_bitmap,
985 MPTCP_PM_MAX_ADDR_ID + 1,
987 if (!entry->addr.id && pernet->next_id != 1) {
993 if (!entry->addr.id && needs_id)
996 __set_bit(entry->addr.id, pernet->id_bitmap);
997 if (entry->addr.id > pernet->next_id)
998 pernet->next_id = entry->addr.id;
1000 if (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
1001 addr_max = pernet->add_addr_signal_max;
1002 WRITE_ONCE(pernet->add_addr_signal_max, addr_max + 1);
1004 if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) {
1005 addr_max = pernet->local_addr_max;
1006 WRITE_ONCE(pernet->local_addr_max, addr_max + 1);
1010 if (!entry->addr.port)
1011 list_add_tail_rcu(&entry->list, &pernet->local_addr_list);
1013 list_add_rcu(&entry->list, &pernet->local_addr_list);
1014 ret = entry->addr.id;
1017 spin_unlock_bh(&pernet->lock);
1019 /* just replaced an existing entry, free it */
1022 __mptcp_pm_release_addr_entry(del_entry);
1027 static struct lock_class_key mptcp_slock_keys[2];
1028 static struct lock_class_key mptcp_keys[2];
1030 static int mptcp_pm_nl_create_listen_socket(struct sock *sk,
1031 struct mptcp_pm_addr_entry *entry)
1033 bool is_ipv6 = sk->sk_family == AF_INET6;
1034 int addrlen = sizeof(struct sockaddr_in);
1035 struct sockaddr_storage addr;
1036 struct sock *newsk, *ssk;
1040 err = sock_create_kern(sock_net(sk), entry->addr.family,
1041 SOCK_STREAM, IPPROTO_MPTCP, &entry->lsk);
1045 newsk = entry->lsk->sk;
1049 /* The subflow socket lock is acquired in a nested to the msk one
1050 * in several places, even by the TCP stack, and this msk is a kernel
1051 * socket: lockdep complains. Instead of propagating the _nested
1052 * modifiers in several places, re-init the lock class for the msk
1053 * socket to an mptcp specific one.
1055 sock_lock_init_class_and_name(newsk,
1056 is_ipv6 ? "mlock-AF_INET6" : "mlock-AF_INET",
1057 &mptcp_slock_keys[is_ipv6],
1058 is_ipv6 ? "msk_lock-AF_INET6" : "msk_lock-AF_INET",
1059 &mptcp_keys[is_ipv6]);
1062 ssk = __mptcp_nmpc_sk(mptcp_sk(newsk));
1063 release_sock(newsk);
1065 return PTR_ERR(ssk);
1067 mptcp_info2sockaddr(&entry->addr, &addr, entry->addr.family);
1068 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1069 if (entry->addr.family == AF_INET6)
1070 addrlen = sizeof(struct sockaddr_in6);
1072 if (ssk->sk_family == AF_INET)
1073 err = inet_bind_sk(ssk, (struct sockaddr *)&addr, addrlen);
1074 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1075 else if (ssk->sk_family == AF_INET6)
1076 err = inet6_bind_sk(ssk, (struct sockaddr *)&addr, addrlen);
1081 /* We don't use mptcp_set_state() here because it needs to be called
1082 * under the msk socket lock. For the moment, that will not bring
1083 * anything more than only calling inet_sk_state_store(), because the
1084 * old status is known (TCP_CLOSE).
1086 inet_sk_state_store(newsk, TCP_LISTEN);
1088 err = __inet_listen_sk(ssk, backlog);
1090 mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CREATED);
1095 int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct mptcp_addr_info *skc)
1097 struct mptcp_pm_addr_entry *entry;
1098 struct pm_nl_pernet *pernet;
1101 pernet = pm_nl_get_pernet_from_msk(msk);
1104 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
1105 if (mptcp_addresses_equal(&entry->addr, skc, entry->addr.port)) {
1106 ret = entry->addr.id;
1114 /* address not found, add to local list */
1115 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
1121 entry->addr.port = 0;
1123 entry->flags = MPTCP_PM_ADDR_FLAG_IMPLICIT;
1125 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry, true);
1132 bool mptcp_pm_nl_is_backup(struct mptcp_sock *msk, struct mptcp_addr_info *skc)
1134 struct pm_nl_pernet *pernet = pm_nl_get_pernet_from_msk(msk);
1135 struct mptcp_pm_addr_entry *entry;
1136 bool backup = false;
1139 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
1140 if (mptcp_addresses_equal(&entry->addr, skc, entry->addr.port)) {
1141 backup = !!(entry->flags & MPTCP_PM_ADDR_FLAG_BACKUP);
1150 #define MPTCP_PM_CMD_GRP_OFFSET 0
1151 #define MPTCP_PM_EV_GRP_OFFSET 1
1153 static const struct genl_multicast_group mptcp_pm_mcgrps[] = {
1154 [MPTCP_PM_CMD_GRP_OFFSET] = { .name = MPTCP_PM_CMD_GRP_NAME, },
1155 [MPTCP_PM_EV_GRP_OFFSET] = { .name = MPTCP_PM_EV_GRP_NAME,
1156 .flags = GENL_MCAST_CAP_NET_ADMIN,
1160 void mptcp_pm_nl_subflow_chk_stale(const struct mptcp_sock *msk, struct sock *ssk)
1162 struct mptcp_subflow_context *iter, *subflow = mptcp_subflow_ctx(ssk);
1163 struct sock *sk = (struct sock *)msk;
1164 unsigned int active_max_loss_cnt;
1165 struct net *net = sock_net(sk);
1166 unsigned int stale_loss_cnt;
1169 stale_loss_cnt = mptcp_stale_loss_cnt(net);
1170 if (subflow->stale || !stale_loss_cnt || subflow->stale_count <= stale_loss_cnt)
1173 /* look for another available subflow not in loss state */
1174 active_max_loss_cnt = max_t(int, stale_loss_cnt - 1, 1);
1175 mptcp_for_each_subflow(msk, iter) {
1176 if (iter != subflow && mptcp_subflow_active(iter) &&
1177 iter->stale_count < active_max_loss_cnt) {
1178 /* we have some alternatives, try to mark this subflow as idle ...*/
1179 slow = lock_sock_fast(ssk);
1180 if (!tcp_rtx_and_write_queues_empty(ssk)) {
1182 __mptcp_retransmit_pending_data(sk);
1183 MPTCP_INC_STATS(net, MPTCP_MIB_SUBFLOWSTALE);
1185 unlock_sock_fast(ssk, slow);
1187 /* always try to push the pending data regardless of re-injections:
1188 * we can possibly use backup subflows now, and subflow selection
1189 * is cheap under the msk socket lock
1191 __mptcp_push_pending(sk, 0);
1197 static int mptcp_pm_family_to_addr(int family)
1199 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1200 if (family == AF_INET6)
1201 return MPTCP_PM_ADDR_ATTR_ADDR6;
1203 return MPTCP_PM_ADDR_ATTR_ADDR4;
1206 static int mptcp_pm_parse_pm_addr_attr(struct nlattr *tb[],
1207 const struct nlattr *attr,
1208 struct genl_info *info,
1209 struct mptcp_addr_info *addr,
1210 bool require_family)
1215 GENL_SET_ERR_MSG(info, "missing address info");
1219 /* no validation needed - was already done via nested policy */
1220 err = nla_parse_nested_deprecated(tb, MPTCP_PM_ADDR_ATTR_MAX, attr,
1221 mptcp_pm_address_nl_policy, info->extack);
1225 if (tb[MPTCP_PM_ADDR_ATTR_ID])
1226 addr->id = nla_get_u8(tb[MPTCP_PM_ADDR_ATTR_ID]);
1228 if (!tb[MPTCP_PM_ADDR_ATTR_FAMILY]) {
1229 if (!require_family)
1232 NL_SET_ERR_MSG_ATTR(info->extack, attr,
1237 addr->family = nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_FAMILY]);
1238 if (addr->family != AF_INET
1239 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1240 && addr->family != AF_INET6
1243 NL_SET_ERR_MSG_ATTR(info->extack, attr,
1244 "unknown address family");
1247 addr_addr = mptcp_pm_family_to_addr(addr->family);
1248 if (!tb[addr_addr]) {
1249 NL_SET_ERR_MSG_ATTR(info->extack, attr,
1250 "missing address data");
1254 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1255 if (addr->family == AF_INET6)
1256 addr->addr6 = nla_get_in6_addr(tb[addr_addr]);
1259 addr->addr.s_addr = nla_get_in_addr(tb[addr_addr]);
1261 if (tb[MPTCP_PM_ADDR_ATTR_PORT])
1262 addr->port = htons(nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_PORT]));
1267 int mptcp_pm_parse_addr(struct nlattr *attr, struct genl_info *info,
1268 struct mptcp_addr_info *addr)
1270 struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1];
1272 memset(addr, 0, sizeof(*addr));
1274 return mptcp_pm_parse_pm_addr_attr(tb, attr, info, addr, true);
1277 int mptcp_pm_parse_entry(struct nlattr *attr, struct genl_info *info,
1278 bool require_family,
1279 struct mptcp_pm_addr_entry *entry)
1281 struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1];
1284 memset(entry, 0, sizeof(*entry));
1286 err = mptcp_pm_parse_pm_addr_attr(tb, attr, info, &entry->addr, require_family);
1290 if (tb[MPTCP_PM_ADDR_ATTR_IF_IDX]) {
1291 u32 val = nla_get_s32(tb[MPTCP_PM_ADDR_ATTR_IF_IDX]);
1293 entry->ifindex = val;
1296 if (tb[MPTCP_PM_ADDR_ATTR_FLAGS])
1297 entry->flags = nla_get_u32(tb[MPTCP_PM_ADDR_ATTR_FLAGS]);
1299 if (tb[MPTCP_PM_ADDR_ATTR_PORT])
1300 entry->addr.port = htons(nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_PORT]));
1305 static struct pm_nl_pernet *genl_info_pm_nl(struct genl_info *info)
1307 return pm_nl_get_pernet(genl_info_net(info));
1310 static int mptcp_nl_add_subflow_or_signal_addr(struct net *net)
1312 struct mptcp_sock *msk;
1313 long s_slot = 0, s_num = 0;
1315 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1316 struct sock *sk = (struct sock *)msk;
1318 if (!READ_ONCE(msk->fully_established) ||
1319 mptcp_pm_is_userspace(msk))
1323 spin_lock_bh(&msk->pm.lock);
1324 mptcp_pm_create_subflow_or_signal_addr(msk);
1325 spin_unlock_bh(&msk->pm.lock);
1336 static bool mptcp_pm_has_addr_attr_id(const struct nlattr *attr,
1337 struct genl_info *info)
1339 struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1];
1341 if (!nla_parse_nested_deprecated(tb, MPTCP_PM_ADDR_ATTR_MAX, attr,
1342 mptcp_pm_address_nl_policy, info->extack) &&
1343 tb[MPTCP_PM_ADDR_ATTR_ID])
1348 int mptcp_pm_nl_add_addr_doit(struct sk_buff *skb, struct genl_info *info)
1350 struct nlattr *attr = info->attrs[MPTCP_PM_ENDPOINT_ADDR];
1351 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1352 struct mptcp_pm_addr_entry addr, *entry;
1355 ret = mptcp_pm_parse_entry(attr, info, true, &addr);
1359 if (addr.addr.port && !address_use_port(&addr)) {
1360 GENL_SET_ERR_MSG(info, "flags must have signal and not subflow when using port");
1364 if (addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL &&
1365 addr.flags & MPTCP_PM_ADDR_FLAG_FULLMESH) {
1366 GENL_SET_ERR_MSG(info, "flags mustn't have both signal and fullmesh");
1370 if (addr.flags & MPTCP_PM_ADDR_FLAG_IMPLICIT) {
1371 GENL_SET_ERR_MSG(info, "can't create IMPLICIT endpoint");
1375 entry = kzalloc(sizeof(*entry), GFP_KERNEL_ACCOUNT);
1377 GENL_SET_ERR_MSG(info, "can't allocate addr");
1382 if (entry->addr.port) {
1383 ret = mptcp_pm_nl_create_listen_socket(skb->sk, entry);
1385 GENL_SET_ERR_MSG_FMT(info, "create listen socket error: %d", ret);
1389 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry,
1390 !mptcp_pm_has_addr_attr_id(attr, info));
1392 GENL_SET_ERR_MSG_FMT(info, "too many addresses or duplicate one: %d", ret);
1396 mptcp_nl_add_subflow_or_signal_addr(sock_net(skb->sk));
1400 __mptcp_pm_release_addr_entry(entry);
1404 int mptcp_pm_nl_get_flags_and_ifindex_by_id(struct mptcp_sock *msk, unsigned int id,
1405 u8 *flags, int *ifindex)
1407 struct mptcp_pm_addr_entry *entry;
1408 struct sock *sk = (struct sock *)msk;
1409 struct net *net = sock_net(sk);
1411 /* No entries with ID 0 */
1416 entry = __lookup_addr_by_id(pm_nl_get_pernet(net), id);
1418 *flags = entry->flags;
1419 *ifindex = entry->ifindex;
1426 static bool remove_anno_list_by_saddr(struct mptcp_sock *msk,
1427 const struct mptcp_addr_info *addr)
1429 struct mptcp_pm_add_entry *entry;
1431 entry = mptcp_pm_del_add_timer(msk, addr, false);
1433 list_del(&entry->list);
1441 static bool mptcp_pm_remove_anno_addr(struct mptcp_sock *msk,
1442 const struct mptcp_addr_info *addr,
1445 struct mptcp_rm_list list = { .nr = 0 };
1448 list.ids[list.nr++] = addr->id;
1450 ret = remove_anno_list_by_saddr(msk, addr);
1452 spin_lock_bh(&msk->pm.lock);
1454 __set_bit(addr->id, msk->pm.id_avail_bitmap);
1455 msk->pm.add_addr_signaled--;
1457 mptcp_pm_remove_addr(msk, &list);
1458 spin_unlock_bh(&msk->pm.lock);
1463 static void __mark_subflow_endp_available(struct mptcp_sock *msk, u8 id)
1465 /* If it was marked as used, and not ID 0, decrement local_addr_used */
1466 if (!__test_and_set_bit(id ? : msk->mpc_endpoint_id, msk->pm.id_avail_bitmap) &&
1467 id && !WARN_ON_ONCE(msk->pm.local_addr_used == 0))
1468 msk->pm.local_addr_used--;
1471 static int mptcp_nl_remove_subflow_and_signal_addr(struct net *net,
1472 const struct mptcp_pm_addr_entry *entry)
1474 const struct mptcp_addr_info *addr = &entry->addr;
1475 struct mptcp_rm_list list = { .nr = 0 };
1476 long s_slot = 0, s_num = 0;
1477 struct mptcp_sock *msk;
1479 pr_debug("remove_id=%d", addr->id);
1481 list.ids[list.nr++] = addr->id;
1483 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1484 struct sock *sk = (struct sock *)msk;
1485 bool remove_subflow;
1487 if (mptcp_pm_is_userspace(msk))
1490 if (list_empty(&msk->conn_list)) {
1491 mptcp_pm_remove_anno_addr(msk, addr, false);
1496 remove_subflow = lookup_subflow_by_saddr(&msk->conn_list, addr);
1497 mptcp_pm_remove_anno_addr(msk, addr, remove_subflow &&
1498 !(entry->flags & MPTCP_PM_ADDR_FLAG_IMPLICIT));
1500 if (remove_subflow) {
1501 spin_lock_bh(&msk->pm.lock);
1502 mptcp_pm_nl_rm_subflow_received(msk, &list);
1503 spin_unlock_bh(&msk->pm.lock);
1506 if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) {
1507 spin_lock_bh(&msk->pm.lock);
1508 __mark_subflow_endp_available(msk, list.ids[0]);
1509 spin_unlock_bh(&msk->pm.lock);
1522 static int mptcp_nl_remove_id_zero_address(struct net *net,
1523 struct mptcp_addr_info *addr)
1525 struct mptcp_rm_list list = { .nr = 0 };
1526 long s_slot = 0, s_num = 0;
1527 struct mptcp_sock *msk;
1529 list.ids[list.nr++] = 0;
1531 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1532 struct sock *sk = (struct sock *)msk;
1533 struct mptcp_addr_info msk_local;
1535 if (list_empty(&msk->conn_list) || mptcp_pm_is_userspace(msk))
1538 mptcp_local_address((struct sock_common *)msk, &msk_local);
1539 if (!mptcp_addresses_equal(&msk_local, addr, addr->port))
1543 spin_lock_bh(&msk->pm.lock);
1544 mptcp_pm_remove_addr(msk, &list);
1545 mptcp_pm_nl_rm_subflow_received(msk, &list);
1546 __mark_subflow_endp_available(msk, 0);
1547 spin_unlock_bh(&msk->pm.lock);
1558 int mptcp_pm_nl_del_addr_doit(struct sk_buff *skb, struct genl_info *info)
1560 struct nlattr *attr = info->attrs[MPTCP_PM_ENDPOINT_ADDR];
1561 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1562 struct mptcp_pm_addr_entry addr, *entry;
1563 unsigned int addr_max;
1566 ret = mptcp_pm_parse_entry(attr, info, false, &addr);
1570 /* the zero id address is special: the first address used by the msk
1571 * always gets such an id, so different subflows can have different zero
1572 * id addresses. Additionally zero id is not accounted for in id_bitmap.
1573 * Let's use an 'mptcp_rm_list' instead of the common remove code.
1575 if (addr.addr.id == 0)
1576 return mptcp_nl_remove_id_zero_address(sock_net(skb->sk), &addr.addr);
1578 spin_lock_bh(&pernet->lock);
1579 entry = __lookup_addr_by_id(pernet, addr.addr.id);
1581 GENL_SET_ERR_MSG(info, "address not found");
1582 spin_unlock_bh(&pernet->lock);
1585 if (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
1586 addr_max = pernet->add_addr_signal_max;
1587 WRITE_ONCE(pernet->add_addr_signal_max, addr_max - 1);
1589 if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) {
1590 addr_max = pernet->local_addr_max;
1591 WRITE_ONCE(pernet->local_addr_max, addr_max - 1);
1595 list_del_rcu(&entry->list);
1596 __clear_bit(entry->addr.id, pernet->id_bitmap);
1597 spin_unlock_bh(&pernet->lock);
1599 mptcp_nl_remove_subflow_and_signal_addr(sock_net(skb->sk), entry);
1601 __mptcp_pm_release_addr_entry(entry);
1606 void mptcp_pm_remove_addrs(struct mptcp_sock *msk, struct list_head *rm_list)
1608 struct mptcp_rm_list alist = { .nr = 0 };
1609 struct mptcp_pm_addr_entry *entry;
1612 list_for_each_entry(entry, rm_list, list) {
1613 if (alist.nr >= MPTCP_RM_IDS_MAX)
1616 /* only delete if either announced or matching a subflow */
1617 if (remove_anno_list_by_saddr(msk, &entry->addr))
1619 else if (!lookup_subflow_by_saddr(&msk->conn_list,
1623 alist.ids[alist.nr++] = entry->addr.id;
1627 spin_lock_bh(&msk->pm.lock);
1628 msk->pm.add_addr_signaled -= anno_nr;
1629 mptcp_pm_remove_addr(msk, &alist);
1630 spin_unlock_bh(&msk->pm.lock);
1634 static void mptcp_pm_remove_addrs_and_subflows(struct mptcp_sock *msk,
1635 struct list_head *rm_list)
1637 struct mptcp_rm_list alist = { .nr = 0 }, slist = { .nr = 0 };
1638 struct mptcp_pm_addr_entry *entry;
1640 list_for_each_entry(entry, rm_list, list) {
1641 if (slist.nr < MPTCP_RM_IDS_MAX &&
1642 lookup_subflow_by_saddr(&msk->conn_list, &entry->addr))
1643 slist.ids[slist.nr++] = entry->addr.id;
1645 if (alist.nr < MPTCP_RM_IDS_MAX &&
1646 remove_anno_list_by_saddr(msk, &entry->addr))
1647 alist.ids[alist.nr++] = entry->addr.id;
1650 spin_lock_bh(&msk->pm.lock);
1652 msk->pm.add_addr_signaled -= alist.nr;
1653 mptcp_pm_remove_addr(msk, &alist);
1656 mptcp_pm_nl_rm_subflow_received(msk, &slist);
1657 /* Reset counters: maybe some subflows have been removed before */
1658 bitmap_fill(msk->pm.id_avail_bitmap, MPTCP_PM_MAX_ADDR_ID + 1);
1659 msk->pm.local_addr_used = 0;
1660 spin_unlock_bh(&msk->pm.lock);
1663 static void mptcp_nl_remove_addrs_list(struct net *net,
1664 struct list_head *rm_list)
1666 long s_slot = 0, s_num = 0;
1667 struct mptcp_sock *msk;
1669 if (list_empty(rm_list))
1672 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1673 struct sock *sk = (struct sock *)msk;
1675 if (!mptcp_pm_is_userspace(msk)) {
1677 mptcp_pm_remove_addrs_and_subflows(msk, rm_list);
1686 /* caller must ensure the RCU grace period is already elapsed */
1687 static void __flush_addrs(struct list_head *list)
1689 while (!list_empty(list)) {
1690 struct mptcp_pm_addr_entry *cur;
1692 cur = list_entry(list->next,
1693 struct mptcp_pm_addr_entry, list);
1694 list_del_rcu(&cur->list);
1695 __mptcp_pm_release_addr_entry(cur);
1699 static void __reset_counters(struct pm_nl_pernet *pernet)
1701 WRITE_ONCE(pernet->add_addr_signal_max, 0);
1702 WRITE_ONCE(pernet->add_addr_accept_max, 0);
1703 WRITE_ONCE(pernet->local_addr_max, 0);
1707 int mptcp_pm_nl_flush_addrs_doit(struct sk_buff *skb, struct genl_info *info)
1709 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1710 LIST_HEAD(free_list);
1712 spin_lock_bh(&pernet->lock);
1713 list_splice_init(&pernet->local_addr_list, &free_list);
1714 __reset_counters(pernet);
1715 pernet->next_id = 1;
1716 bitmap_zero(pernet->id_bitmap, MPTCP_PM_MAX_ADDR_ID + 1);
1717 spin_unlock_bh(&pernet->lock);
1718 mptcp_nl_remove_addrs_list(sock_net(skb->sk), &free_list);
1720 __flush_addrs(&free_list);
1724 int mptcp_nl_fill_addr(struct sk_buff *skb,
1725 struct mptcp_pm_addr_entry *entry)
1727 struct mptcp_addr_info *addr = &entry->addr;
1728 struct nlattr *attr;
1730 attr = nla_nest_start(skb, MPTCP_PM_ATTR_ADDR);
1734 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_FAMILY, addr->family))
1735 goto nla_put_failure;
1736 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_PORT, ntohs(addr->port)))
1737 goto nla_put_failure;
1738 if (nla_put_u8(skb, MPTCP_PM_ADDR_ATTR_ID, addr->id))
1739 goto nla_put_failure;
1740 if (nla_put_u32(skb, MPTCP_PM_ADDR_ATTR_FLAGS, entry->flags))
1741 goto nla_put_failure;
1742 if (entry->ifindex &&
1743 nla_put_s32(skb, MPTCP_PM_ADDR_ATTR_IF_IDX, entry->ifindex))
1744 goto nla_put_failure;
1746 if (addr->family == AF_INET &&
1747 nla_put_in_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR4,
1749 goto nla_put_failure;
1750 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1751 else if (addr->family == AF_INET6 &&
1752 nla_put_in6_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR6, &addr->addr6))
1753 goto nla_put_failure;
1755 nla_nest_end(skb, attr);
1759 nla_nest_cancel(skb, attr);
1763 int mptcp_pm_nl_get_addr(struct sk_buff *skb, struct genl_info *info)
1765 struct nlattr *attr = info->attrs[MPTCP_PM_ENDPOINT_ADDR];
1766 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1767 struct mptcp_pm_addr_entry addr, *entry;
1768 struct sk_buff *msg;
1772 ret = mptcp_pm_parse_entry(attr, info, false, &addr);
1776 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1780 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1781 info->genlhdr->cmd);
1783 GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1788 spin_lock_bh(&pernet->lock);
1789 entry = __lookup_addr_by_id(pernet, addr.addr.id);
1791 GENL_SET_ERR_MSG(info, "address not found");
1796 ret = mptcp_nl_fill_addr(msg, entry);
1800 genlmsg_end(msg, reply);
1801 ret = genlmsg_reply(msg, info);
1802 spin_unlock_bh(&pernet->lock);
1806 spin_unlock_bh(&pernet->lock);
1813 int mptcp_pm_nl_get_addr_doit(struct sk_buff *skb, struct genl_info *info)
1815 return mptcp_pm_get_addr(skb, info);
1818 int mptcp_pm_nl_dump_addr(struct sk_buff *msg,
1819 struct netlink_callback *cb)
1821 struct net *net = sock_net(msg->sk);
1822 struct mptcp_pm_addr_entry *entry;
1823 struct pm_nl_pernet *pernet;
1824 int id = cb->args[0];
1828 pernet = pm_nl_get_pernet(net);
1830 spin_lock_bh(&pernet->lock);
1831 for (i = id; i < MPTCP_PM_MAX_ADDR_ID + 1; i++) {
1832 if (test_bit(i, pernet->id_bitmap)) {
1833 entry = __lookup_addr_by_id(pernet, i);
1837 if (entry->addr.id <= id)
1840 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid,
1841 cb->nlh->nlmsg_seq, &mptcp_genl_family,
1842 NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR);
1846 if (mptcp_nl_fill_addr(msg, entry) < 0) {
1847 genlmsg_cancel(msg, hdr);
1851 id = entry->addr.id;
1852 genlmsg_end(msg, hdr);
1855 spin_unlock_bh(&pernet->lock);
1861 int mptcp_pm_nl_get_addr_dumpit(struct sk_buff *msg,
1862 struct netlink_callback *cb)
1864 return mptcp_pm_dump_addr(msg, cb);
1867 static int parse_limit(struct genl_info *info, int id, unsigned int *limit)
1869 struct nlattr *attr = info->attrs[id];
1874 *limit = nla_get_u32(attr);
1875 if (*limit > MPTCP_PM_ADDR_MAX) {
1876 GENL_SET_ERR_MSG(info, "limit greater than maximum");
1882 int mptcp_pm_nl_set_limits_doit(struct sk_buff *skb, struct genl_info *info)
1884 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1885 unsigned int rcv_addrs, subflows;
1888 spin_lock_bh(&pernet->lock);
1889 rcv_addrs = pernet->add_addr_accept_max;
1890 ret = parse_limit(info, MPTCP_PM_ATTR_RCV_ADD_ADDRS, &rcv_addrs);
1894 subflows = pernet->subflows_max;
1895 ret = parse_limit(info, MPTCP_PM_ATTR_SUBFLOWS, &subflows);
1899 WRITE_ONCE(pernet->add_addr_accept_max, rcv_addrs);
1900 WRITE_ONCE(pernet->subflows_max, subflows);
1903 spin_unlock_bh(&pernet->lock);
1907 int mptcp_pm_nl_get_limits_doit(struct sk_buff *skb, struct genl_info *info)
1909 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1910 struct sk_buff *msg;
1913 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1917 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1918 MPTCP_PM_CMD_GET_LIMITS);
1922 if (nla_put_u32(msg, MPTCP_PM_ATTR_RCV_ADD_ADDRS,
1923 READ_ONCE(pernet->add_addr_accept_max)))
1926 if (nla_put_u32(msg, MPTCP_PM_ATTR_SUBFLOWS,
1927 READ_ONCE(pernet->subflows_max)))
1930 genlmsg_end(msg, reply);
1931 return genlmsg_reply(msg, info);
1934 GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1939 static void mptcp_pm_nl_fullmesh(struct mptcp_sock *msk,
1940 struct mptcp_addr_info *addr)
1942 struct mptcp_rm_list list = { .nr = 0 };
1944 list.ids[list.nr++] = addr->id;
1946 spin_lock_bh(&msk->pm.lock);
1947 mptcp_pm_nl_rm_subflow_received(msk, &list);
1948 __mark_subflow_endp_available(msk, list.ids[0]);
1949 mptcp_pm_create_subflow_or_signal_addr(msk);
1950 spin_unlock_bh(&msk->pm.lock);
1953 static int mptcp_nl_set_flags(struct net *net,
1954 struct mptcp_addr_info *addr,
1955 u8 bkup, u8 changed)
1957 long s_slot = 0, s_num = 0;
1958 struct mptcp_sock *msk;
1961 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1962 struct sock *sk = (struct sock *)msk;
1964 if (list_empty(&msk->conn_list) || mptcp_pm_is_userspace(msk))
1968 if (changed & MPTCP_PM_ADDR_FLAG_BACKUP)
1969 ret = mptcp_pm_nl_mp_prio_send_ack(msk, addr, NULL, bkup);
1970 if (changed & MPTCP_PM_ADDR_FLAG_FULLMESH)
1971 mptcp_pm_nl_fullmesh(msk, addr);
1982 int mptcp_pm_nl_set_flags(struct sk_buff *skb, struct genl_info *info)
1984 struct mptcp_pm_addr_entry addr = { .addr = { .family = AF_UNSPEC }, };
1985 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1986 u8 changed, mask = MPTCP_PM_ADDR_FLAG_BACKUP |
1987 MPTCP_PM_ADDR_FLAG_FULLMESH;
1988 struct net *net = sock_net(skb->sk);
1989 struct mptcp_pm_addr_entry *entry;
1990 struct pm_nl_pernet *pernet;
1991 u8 lookup_by_id = 0;
1995 pernet = pm_nl_get_pernet(net);
1997 ret = mptcp_pm_parse_entry(attr, info, false, &addr);
2001 if (addr.addr.family == AF_UNSPEC) {
2003 if (!addr.addr.id) {
2004 GENL_SET_ERR_MSG(info, "missing required inputs");
2009 if (addr.flags & MPTCP_PM_ADDR_FLAG_BACKUP)
2012 spin_lock_bh(&pernet->lock);
2013 entry = lookup_by_id ? __lookup_addr_by_id(pernet, addr.addr.id) :
2014 __lookup_addr(pernet, &addr.addr);
2016 spin_unlock_bh(&pernet->lock);
2017 GENL_SET_ERR_MSG(info, "address not found");
2020 if ((addr.flags & MPTCP_PM_ADDR_FLAG_FULLMESH) &&
2021 (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) {
2022 spin_unlock_bh(&pernet->lock);
2023 GENL_SET_ERR_MSG(info, "invalid addr flags");
2027 changed = (addr.flags ^ entry->flags) & mask;
2028 entry->flags = (entry->flags & ~mask) | (addr.flags & mask);
2030 spin_unlock_bh(&pernet->lock);
2032 mptcp_nl_set_flags(net, &addr.addr, bkup, changed);
2036 int mptcp_pm_nl_set_flags_doit(struct sk_buff *skb, struct genl_info *info)
2038 return mptcp_pm_set_flags(skb, info);
2041 static void mptcp_nl_mcast_send(struct net *net, struct sk_buff *nlskb, gfp_t gfp)
2043 genlmsg_multicast_netns(&mptcp_genl_family, net,
2044 nlskb, 0, MPTCP_PM_EV_GRP_OFFSET, gfp);
2047 bool mptcp_userspace_pm_active(const struct mptcp_sock *msk)
2049 return genl_has_listeners(&mptcp_genl_family,
2050 sock_net((const struct sock *)msk),
2051 MPTCP_PM_EV_GRP_OFFSET);
2054 static int mptcp_event_add_subflow(struct sk_buff *skb, const struct sock *ssk)
2056 const struct inet_sock *issk = inet_sk(ssk);
2057 const struct mptcp_subflow_context *sf;
2059 if (nla_put_u16(skb, MPTCP_ATTR_FAMILY, ssk->sk_family))
2062 switch (ssk->sk_family) {
2064 if (nla_put_in_addr(skb, MPTCP_ATTR_SADDR4, issk->inet_saddr))
2066 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, issk->inet_daddr))
2069 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2071 const struct ipv6_pinfo *np = inet6_sk(ssk);
2073 if (nla_put_in6_addr(skb, MPTCP_ATTR_SADDR6, &np->saddr))
2075 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &ssk->sk_v6_daddr))
2085 if (nla_put_be16(skb, MPTCP_ATTR_SPORT, issk->inet_sport))
2087 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, issk->inet_dport))
2090 sf = mptcp_subflow_ctx(ssk);
2091 if (WARN_ON_ONCE(!sf))
2094 if (nla_put_u8(skb, MPTCP_ATTR_LOC_ID, subflow_get_local_id(sf)))
2097 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, sf->remote_id))
2103 static int mptcp_event_put_token_and_ssk(struct sk_buff *skb,
2104 const struct mptcp_sock *msk,
2105 const struct sock *ssk)
2107 const struct sock *sk = (const struct sock *)msk;
2108 const struct mptcp_subflow_context *sf;
2111 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, READ_ONCE(msk->token)))
2114 if (mptcp_event_add_subflow(skb, ssk))
2117 sf = mptcp_subflow_ctx(ssk);
2118 if (WARN_ON_ONCE(!sf))
2121 if (nla_put_u8(skb, MPTCP_ATTR_BACKUP, sf->backup))
2124 if (ssk->sk_bound_dev_if &&
2125 nla_put_s32(skb, MPTCP_ATTR_IF_IDX, ssk->sk_bound_dev_if))
2128 sk_err = READ_ONCE(ssk->sk_err);
2129 if (sk_err && sk->sk_state == TCP_ESTABLISHED &&
2130 nla_put_u8(skb, MPTCP_ATTR_ERROR, sk_err))
2136 static int mptcp_event_sub_established(struct sk_buff *skb,
2137 const struct mptcp_sock *msk,
2138 const struct sock *ssk)
2140 return mptcp_event_put_token_and_ssk(skb, msk, ssk);
2143 static int mptcp_event_sub_closed(struct sk_buff *skb,
2144 const struct mptcp_sock *msk,
2145 const struct sock *ssk)
2147 const struct mptcp_subflow_context *sf;
2149 if (mptcp_event_put_token_and_ssk(skb, msk, ssk))
2152 sf = mptcp_subflow_ctx(ssk);
2153 if (!sf->reset_seen)
2156 if (nla_put_u32(skb, MPTCP_ATTR_RESET_REASON, sf->reset_reason))
2159 if (nla_put_u32(skb, MPTCP_ATTR_RESET_FLAGS, sf->reset_transient))
2165 static int mptcp_event_created(struct sk_buff *skb,
2166 const struct mptcp_sock *msk,
2167 const struct sock *ssk)
2169 int err = nla_put_u32(skb, MPTCP_ATTR_TOKEN, READ_ONCE(msk->token));
2174 if (nla_put_u8(skb, MPTCP_ATTR_SERVER_SIDE, READ_ONCE(msk->pm.server_side)))
2177 return mptcp_event_add_subflow(skb, ssk);
2180 void mptcp_event_addr_removed(const struct mptcp_sock *msk, uint8_t id)
2182 struct net *net = sock_net((const struct sock *)msk);
2183 struct nlmsghdr *nlh;
2184 struct sk_buff *skb;
2186 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
2189 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2193 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, MPTCP_EVENT_REMOVED);
2195 goto nla_put_failure;
2197 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, READ_ONCE(msk->token)))
2198 goto nla_put_failure;
2200 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, id))
2201 goto nla_put_failure;
2203 genlmsg_end(skb, nlh);
2204 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC);
2211 void mptcp_event_addr_announced(const struct sock *ssk,
2212 const struct mptcp_addr_info *info)
2214 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
2215 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
2216 struct net *net = sock_net(ssk);
2217 struct nlmsghdr *nlh;
2218 struct sk_buff *skb;
2220 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
2223 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2227 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0,
2228 MPTCP_EVENT_ANNOUNCED);
2230 goto nla_put_failure;
2232 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, READ_ONCE(msk->token)))
2233 goto nla_put_failure;
2235 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, info->id))
2236 goto nla_put_failure;
2238 if (nla_put_be16(skb, MPTCP_ATTR_DPORT,
2240 inet_sk(ssk)->inet_dport :
2242 goto nla_put_failure;
2244 switch (info->family) {
2246 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, info->addr.s_addr))
2247 goto nla_put_failure;
2249 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2251 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &info->addr6))
2252 goto nla_put_failure;
2257 goto nla_put_failure;
2260 genlmsg_end(skb, nlh);
2261 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC);
2268 void mptcp_event_pm_listener(const struct sock *ssk,
2269 enum mptcp_event_type event)
2271 const struct inet_sock *issk = inet_sk(ssk);
2272 struct net *net = sock_net(ssk);
2273 struct nlmsghdr *nlh;
2274 struct sk_buff *skb;
2276 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
2279 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2283 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, event);
2285 goto nla_put_failure;
2287 if (nla_put_u16(skb, MPTCP_ATTR_FAMILY, ssk->sk_family))
2288 goto nla_put_failure;
2290 if (nla_put_be16(skb, MPTCP_ATTR_SPORT, issk->inet_sport))
2291 goto nla_put_failure;
2293 switch (ssk->sk_family) {
2295 if (nla_put_in_addr(skb, MPTCP_ATTR_SADDR4, issk->inet_saddr))
2296 goto nla_put_failure;
2298 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2300 const struct ipv6_pinfo *np = inet6_sk(ssk);
2302 if (nla_put_in6_addr(skb, MPTCP_ATTR_SADDR6, &np->saddr))
2303 goto nla_put_failure;
2309 goto nla_put_failure;
2312 genlmsg_end(skb, nlh);
2313 mptcp_nl_mcast_send(net, skb, GFP_KERNEL);
2320 void mptcp_event(enum mptcp_event_type type, const struct mptcp_sock *msk,
2321 const struct sock *ssk, gfp_t gfp)
2323 struct net *net = sock_net((const struct sock *)msk);
2324 struct nlmsghdr *nlh;
2325 struct sk_buff *skb;
2327 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
2330 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
2334 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, type);
2336 goto nla_put_failure;
2339 case MPTCP_EVENT_UNSPEC:
2342 case MPTCP_EVENT_CREATED:
2343 case MPTCP_EVENT_ESTABLISHED:
2344 if (mptcp_event_created(skb, msk, ssk) < 0)
2345 goto nla_put_failure;
2347 case MPTCP_EVENT_CLOSED:
2348 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, READ_ONCE(msk->token)) < 0)
2349 goto nla_put_failure;
2351 case MPTCP_EVENT_ANNOUNCED:
2352 case MPTCP_EVENT_REMOVED:
2353 /* call mptcp_event_addr_announced()/removed instead */
2356 case MPTCP_EVENT_SUB_ESTABLISHED:
2357 case MPTCP_EVENT_SUB_PRIORITY:
2358 if (mptcp_event_sub_established(skb, msk, ssk) < 0)
2359 goto nla_put_failure;
2361 case MPTCP_EVENT_SUB_CLOSED:
2362 if (mptcp_event_sub_closed(skb, msk, ssk) < 0)
2363 goto nla_put_failure;
2365 case MPTCP_EVENT_LISTENER_CREATED:
2366 case MPTCP_EVENT_LISTENER_CLOSED:
2370 genlmsg_end(skb, nlh);
2371 mptcp_nl_mcast_send(net, skb, gfp);
2378 struct genl_family mptcp_genl_family __ro_after_init = {
2379 .name = MPTCP_PM_NAME,
2380 .version = MPTCP_PM_VER,
2382 .module = THIS_MODULE,
2383 .ops = mptcp_pm_nl_ops,
2384 .n_ops = ARRAY_SIZE(mptcp_pm_nl_ops),
2385 .resv_start_op = MPTCP_PM_CMD_SUBFLOW_DESTROY + 1,
2386 .mcgrps = mptcp_pm_mcgrps,
2387 .n_mcgrps = ARRAY_SIZE(mptcp_pm_mcgrps),
2390 static int __net_init pm_nl_init_net(struct net *net)
2392 struct pm_nl_pernet *pernet = pm_nl_get_pernet(net);
2394 INIT_LIST_HEAD_RCU(&pernet->local_addr_list);
2396 /* Cit. 2 subflows ought to be enough for anybody. */
2397 pernet->subflows_max = 2;
2398 pernet->next_id = 1;
2399 pernet->stale_loss_cnt = 4;
2400 spin_lock_init(&pernet->lock);
2402 /* No need to initialize other pernet fields, the struct is zeroed at
2409 static void __net_exit pm_nl_exit_net(struct list_head *net_list)
2413 list_for_each_entry(net, net_list, exit_list) {
2414 struct pm_nl_pernet *pernet = pm_nl_get_pernet(net);
2416 /* net is removed from namespace list, can't race with
2417 * other modifiers, also netns core already waited for a
2420 __flush_addrs(&pernet->local_addr_list);
2424 static struct pernet_operations mptcp_pm_pernet_ops = {
2425 .init = pm_nl_init_net,
2426 .exit_batch = pm_nl_exit_net,
2427 .id = &pm_nl_pernet_id,
2428 .size = sizeof(struct pm_nl_pernet),
2431 void __init mptcp_pm_nl_init(void)
2433 if (register_pernet_subsys(&mptcp_pm_pernet_ops) < 0)
2434 panic("Failed to register MPTCP PM pernet subsystem.\n");
2436 if (genl_register_family(&mptcp_genl_family))
2437 panic("Failed to register MPTCP PM netlink family\n");