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_add_entry {
26 struct list_head list;
27 struct mptcp_addr_info addr;
28 struct timer_list add_timer;
29 struct mptcp_sock *sock;
34 /* protects pernet updates */
36 struct list_head local_addr_list;
38 unsigned int stale_loss_cnt;
39 unsigned int add_addr_signal_max;
40 unsigned int add_addr_accept_max;
41 unsigned int local_addr_max;
42 unsigned int subflows_max;
44 DECLARE_BITMAP(id_bitmap, MPTCP_PM_MAX_ADDR_ID + 1);
47 #define MPTCP_PM_ADDR_MAX 8
48 #define ADD_ADDR_RETRANS_MAX 3
50 static struct pm_nl_pernet *pm_nl_get_pernet(const struct net *net)
52 return net_generic(net, pm_nl_pernet_id);
55 static struct pm_nl_pernet *
56 pm_nl_get_pernet_from_msk(const struct mptcp_sock *msk)
58 return pm_nl_get_pernet(sock_net((struct sock *)msk));
61 bool mptcp_addresses_equal(const struct mptcp_addr_info *a,
62 const 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 void local_address(const struct sock_common *skc,
90 struct mptcp_addr_info *addr)
92 addr->family = skc->skc_family;
93 addr->port = htons(skc->skc_num);
94 if (addr->family == AF_INET)
95 addr->addr.s_addr = skc->skc_rcv_saddr;
96 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
97 else if (addr->family == AF_INET6)
98 addr->addr6 = skc->skc_v6_rcv_saddr;
102 static void remote_address(const struct sock_common *skc,
103 struct mptcp_addr_info *addr)
105 addr->family = skc->skc_family;
106 addr->port = skc->skc_dport;
107 if (addr->family == AF_INET)
108 addr->addr.s_addr = skc->skc_daddr;
109 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
110 else if (addr->family == AF_INET6)
111 addr->addr6 = skc->skc_v6_daddr;
115 static bool lookup_subflow_by_saddr(const struct list_head *list,
116 const struct mptcp_addr_info *saddr)
118 struct mptcp_subflow_context *subflow;
119 struct mptcp_addr_info cur;
120 struct sock_common *skc;
122 list_for_each_entry(subflow, list, node) {
123 skc = (struct sock_common *)mptcp_subflow_tcp_sock(subflow);
125 local_address(skc, &cur);
126 if (mptcp_addresses_equal(&cur, saddr, saddr->port))
133 static bool lookup_subflow_by_daddr(const struct list_head *list,
134 const struct mptcp_addr_info *daddr)
136 struct mptcp_subflow_context *subflow;
137 struct mptcp_addr_info cur;
138 struct sock_common *skc;
140 list_for_each_entry(subflow, list, node) {
141 skc = (struct sock_common *)mptcp_subflow_tcp_sock(subflow);
143 remote_address(skc, &cur);
144 if (mptcp_addresses_equal(&cur, daddr, daddr->port))
151 static struct mptcp_pm_addr_entry *
152 select_local_address(const struct pm_nl_pernet *pernet,
153 const struct mptcp_sock *msk)
155 const struct sock *sk = (const struct sock *)msk;
156 struct mptcp_pm_addr_entry *entry, *ret = NULL;
158 msk_owned_by_me(msk);
161 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
162 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW))
165 if (!test_bit(entry->addr.id, msk->pm.id_avail_bitmap))
168 if (entry->addr.family != sk->sk_family) {
169 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
170 if ((entry->addr.family == AF_INET &&
171 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) ||
172 (sk->sk_family == AF_INET &&
173 !ipv6_addr_v4mapped(&entry->addr.addr6)))
185 static struct mptcp_pm_addr_entry *
186 select_signal_address(struct pm_nl_pernet *pernet, const struct mptcp_sock *msk)
188 struct mptcp_pm_addr_entry *entry, *ret = NULL;
191 /* do not keep any additional per socket state, just signal
192 * the address list in order.
193 * Note: removal from the local address list during the msk life-cycle
194 * can lead to additional addresses not being announced.
196 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
197 if (!test_bit(entry->addr.id, msk->pm.id_avail_bitmap))
200 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL))
210 unsigned int mptcp_pm_get_add_addr_signal_max(const struct mptcp_sock *msk)
212 const struct pm_nl_pernet *pernet = pm_nl_get_pernet_from_msk(msk);
214 return READ_ONCE(pernet->add_addr_signal_max);
216 EXPORT_SYMBOL_GPL(mptcp_pm_get_add_addr_signal_max);
218 unsigned int mptcp_pm_get_add_addr_accept_max(const struct mptcp_sock *msk)
220 struct pm_nl_pernet *pernet = pm_nl_get_pernet_from_msk(msk);
222 return READ_ONCE(pernet->add_addr_accept_max);
224 EXPORT_SYMBOL_GPL(mptcp_pm_get_add_addr_accept_max);
226 unsigned int mptcp_pm_get_subflows_max(const struct mptcp_sock *msk)
228 struct pm_nl_pernet *pernet = pm_nl_get_pernet_from_msk(msk);
230 return READ_ONCE(pernet->subflows_max);
232 EXPORT_SYMBOL_GPL(mptcp_pm_get_subflows_max);
234 unsigned int mptcp_pm_get_local_addr_max(const struct mptcp_sock *msk)
236 struct pm_nl_pernet *pernet = pm_nl_get_pernet_from_msk(msk);
238 return READ_ONCE(pernet->local_addr_max);
240 EXPORT_SYMBOL_GPL(mptcp_pm_get_local_addr_max);
242 bool mptcp_pm_nl_check_work_pending(struct mptcp_sock *msk)
244 struct pm_nl_pernet *pernet = pm_nl_get_pernet_from_msk(msk);
246 if (msk->pm.subflows == mptcp_pm_get_subflows_max(msk) ||
247 (find_next_and_bit(pernet->id_bitmap, msk->pm.id_avail_bitmap,
248 MPTCP_PM_MAX_ADDR_ID + 1, 0) == MPTCP_PM_MAX_ADDR_ID + 1)) {
249 WRITE_ONCE(msk->pm.work_pending, false);
255 struct mptcp_pm_add_entry *
256 mptcp_lookup_anno_list_by_saddr(const struct mptcp_sock *msk,
257 const struct mptcp_addr_info *addr)
259 struct mptcp_pm_add_entry *entry;
261 lockdep_assert_held(&msk->pm.lock);
263 list_for_each_entry(entry, &msk->pm.anno_list, list) {
264 if (mptcp_addresses_equal(&entry->addr, addr, true))
271 bool mptcp_pm_sport_in_anno_list(struct mptcp_sock *msk, const struct sock *sk)
273 struct mptcp_pm_add_entry *entry;
274 struct mptcp_addr_info saddr;
277 local_address((struct sock_common *)sk, &saddr);
279 spin_lock_bh(&msk->pm.lock);
280 list_for_each_entry(entry, &msk->pm.anno_list, list) {
281 if (mptcp_addresses_equal(&entry->addr, &saddr, true)) {
288 spin_unlock_bh(&msk->pm.lock);
292 static void mptcp_pm_add_timer(struct timer_list *timer)
294 struct mptcp_pm_add_entry *entry = from_timer(entry, timer, add_timer);
295 struct mptcp_sock *msk = entry->sock;
296 struct sock *sk = (struct sock *)msk;
298 pr_debug("msk=%p", msk);
303 if (inet_sk_state_load(sk) == TCP_CLOSE)
309 if (mptcp_pm_should_add_signal_addr(msk)) {
310 sk_reset_timer(sk, timer, jiffies + TCP_RTO_MAX / 8);
314 spin_lock_bh(&msk->pm.lock);
316 if (!mptcp_pm_should_add_signal_addr(msk)) {
317 pr_debug("retransmit ADD_ADDR id=%d", entry->addr.id);
318 mptcp_pm_announce_addr(msk, &entry->addr, false);
319 mptcp_pm_add_addr_send_ack(msk);
320 entry->retrans_times++;
323 if (entry->retrans_times < ADD_ADDR_RETRANS_MAX)
324 sk_reset_timer(sk, timer,
325 jiffies + mptcp_get_add_addr_timeout(sock_net(sk)));
327 spin_unlock_bh(&msk->pm.lock);
329 if (entry->retrans_times == ADD_ADDR_RETRANS_MAX)
330 mptcp_pm_subflow_established(msk);
336 struct mptcp_pm_add_entry *
337 mptcp_pm_del_add_timer(struct mptcp_sock *msk,
338 const struct mptcp_addr_info *addr, bool check_id)
340 struct mptcp_pm_add_entry *entry;
341 struct sock *sk = (struct sock *)msk;
343 spin_lock_bh(&msk->pm.lock);
344 entry = mptcp_lookup_anno_list_by_saddr(msk, addr);
345 if (entry && (!check_id || entry->addr.id == addr->id))
346 entry->retrans_times = ADD_ADDR_RETRANS_MAX;
347 spin_unlock_bh(&msk->pm.lock);
349 if (entry && (!check_id || entry->addr.id == addr->id))
350 sk_stop_timer_sync(sk, &entry->add_timer);
355 bool mptcp_pm_alloc_anno_list(struct mptcp_sock *msk,
356 const struct mptcp_pm_addr_entry *entry)
358 struct mptcp_pm_add_entry *add_entry = NULL;
359 struct sock *sk = (struct sock *)msk;
360 struct net *net = sock_net(sk);
362 lockdep_assert_held(&msk->pm.lock);
364 add_entry = mptcp_lookup_anno_list_by_saddr(msk, &entry->addr);
367 if (mptcp_pm_is_kernel(msk))
370 sk_reset_timer(sk, &add_entry->add_timer,
371 jiffies + mptcp_get_add_addr_timeout(net));
375 add_entry = kmalloc(sizeof(*add_entry), GFP_ATOMIC);
379 list_add(&add_entry->list, &msk->pm.anno_list);
381 add_entry->addr = entry->addr;
382 add_entry->sock = msk;
383 add_entry->retrans_times = 0;
385 timer_setup(&add_entry->add_timer, mptcp_pm_add_timer, 0);
386 sk_reset_timer(sk, &add_entry->add_timer,
387 jiffies + mptcp_get_add_addr_timeout(net));
392 void mptcp_pm_free_anno_list(struct mptcp_sock *msk)
394 struct mptcp_pm_add_entry *entry, *tmp;
395 struct sock *sk = (struct sock *)msk;
396 LIST_HEAD(free_list);
398 pr_debug("msk=%p", msk);
400 spin_lock_bh(&msk->pm.lock);
401 list_splice_init(&msk->pm.anno_list, &free_list);
402 spin_unlock_bh(&msk->pm.lock);
404 list_for_each_entry_safe(entry, tmp, &free_list, list) {
405 sk_stop_timer_sync(sk, &entry->add_timer);
410 static bool lookup_address_in_vec(const struct mptcp_addr_info *addrs, unsigned int nr,
411 const struct mptcp_addr_info *addr)
415 for (i = 0; i < nr; i++) {
416 if (addrs[i].id == addr->id)
423 /* Fill all the remote addresses into the array addrs[],
424 * and return the array size.
426 static unsigned int fill_remote_addresses_vec(struct mptcp_sock *msk, bool fullmesh,
427 struct mptcp_addr_info *addrs)
429 bool deny_id0 = READ_ONCE(msk->pm.remote_deny_join_id0);
430 struct sock *sk = (struct sock *)msk, *ssk;
431 struct mptcp_subflow_context *subflow;
432 struct mptcp_addr_info remote = { 0 };
433 unsigned int subflows_max;
436 subflows_max = mptcp_pm_get_subflows_max(msk);
437 remote_address((struct sock_common *)sk, &remote);
439 /* Non-fullmesh endpoint, fill in the single entry
440 * corresponding to the primary MPC subflow remote address
449 mptcp_for_each_subflow(msk, subflow) {
450 ssk = mptcp_subflow_tcp_sock(subflow);
451 remote_address((struct sock_common *)ssk, &addrs[i]);
452 addrs[i].id = subflow->remote_id;
453 if (deny_id0 && !addrs[i].id)
456 if (!lookup_address_in_vec(addrs, i, &addrs[i]) &&
457 msk->pm.subflows < subflows_max) {
467 static void __mptcp_pm_send_ack(struct mptcp_sock *msk, struct mptcp_subflow_context *subflow,
468 bool prio, bool backup)
470 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
473 pr_debug("send ack for %s",
474 prio ? "mp_prio" : (mptcp_pm_should_add_signal(msk) ? "add_addr" : "rm_addr"));
476 slow = lock_sock_fast(ssk);
478 if (subflow->backup != backup)
479 msk->last_snd = NULL;
481 subflow->send_mp_prio = 1;
482 subflow->backup = backup;
483 subflow->request_bkup = backup;
486 __mptcp_subflow_send_ack(ssk);
487 unlock_sock_fast(ssk, slow);
490 static void mptcp_pm_send_ack(struct mptcp_sock *msk, struct mptcp_subflow_context *subflow,
491 bool prio, bool backup)
493 spin_unlock_bh(&msk->pm.lock);
494 __mptcp_pm_send_ack(msk, subflow, prio, backup);
495 spin_lock_bh(&msk->pm.lock);
498 static struct mptcp_pm_addr_entry *
499 __lookup_addr_by_id(struct pm_nl_pernet *pernet, unsigned int id)
501 struct mptcp_pm_addr_entry *entry;
503 list_for_each_entry(entry, &pernet->local_addr_list, list) {
504 if (entry->addr.id == id)
510 static struct mptcp_pm_addr_entry *
511 __lookup_addr(struct pm_nl_pernet *pernet, const struct mptcp_addr_info *info,
514 struct mptcp_pm_addr_entry *entry;
516 list_for_each_entry(entry, &pernet->local_addr_list, list) {
517 if ((!lookup_by_id &&
518 mptcp_addresses_equal(&entry->addr, info, entry->addr.port)) ||
519 (lookup_by_id && entry->addr.id == info->id))
525 static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk)
527 struct sock *sk = (struct sock *)msk;
528 struct mptcp_pm_addr_entry *local;
529 unsigned int add_addr_signal_max;
530 unsigned int local_addr_max;
531 struct pm_nl_pernet *pernet;
532 unsigned int subflows_max;
534 pernet = pm_nl_get_pernet(sock_net(sk));
536 add_addr_signal_max = mptcp_pm_get_add_addr_signal_max(msk);
537 local_addr_max = mptcp_pm_get_local_addr_max(msk);
538 subflows_max = mptcp_pm_get_subflows_max(msk);
540 /* do lazy endpoint usage accounting for the MPC subflows */
541 if (unlikely(!(msk->pm.status & BIT(MPTCP_PM_MPC_ENDPOINT_ACCOUNTED))) && msk->first) {
542 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(msk->first);
543 struct mptcp_pm_addr_entry *entry;
544 struct mptcp_addr_info mpc_addr;
547 local_address((struct sock_common *)msk->first, &mpc_addr);
549 entry = __lookup_addr(pernet, &mpc_addr, false);
551 __clear_bit(entry->addr.id, msk->pm.id_avail_bitmap);
552 msk->mpc_endpoint_id = entry->addr.id;
553 backup = !!(entry->flags & MPTCP_PM_ADDR_FLAG_BACKUP);
558 mptcp_pm_send_ack(msk, subflow, true, backup);
560 msk->pm.status |= BIT(MPTCP_PM_MPC_ENDPOINT_ACCOUNTED);
563 pr_debug("local %d:%d signal %d:%d subflows %d:%d\n",
564 msk->pm.local_addr_used, local_addr_max,
565 msk->pm.add_addr_signaled, add_addr_signal_max,
566 msk->pm.subflows, subflows_max);
568 /* check first for announce */
569 if (msk->pm.add_addr_signaled < add_addr_signal_max) {
570 local = select_signal_address(pernet, msk);
572 /* due to racing events on both ends we can reach here while
573 * previous add address is still running: if we invoke now
574 * mptcp_pm_announce_addr(), that will fail and the
575 * corresponding id will be marked as used.
576 * Instead let the PM machinery reschedule us when the
577 * current address announce will be completed.
579 if (msk->pm.addr_signal & BIT(MPTCP_ADD_ADDR_SIGNAL))
583 if (mptcp_pm_alloc_anno_list(msk, local)) {
584 __clear_bit(local->addr.id, msk->pm.id_avail_bitmap);
585 msk->pm.add_addr_signaled++;
586 mptcp_pm_announce_addr(msk, &local->addr, false);
587 mptcp_pm_nl_addr_send_ack(msk);
592 /* check if should create a new subflow */
593 while (msk->pm.local_addr_used < local_addr_max &&
594 msk->pm.subflows < subflows_max) {
595 struct mptcp_addr_info addrs[MPTCP_PM_ADDR_MAX];
599 local = select_local_address(pernet, msk);
603 fullmesh = !!(local->flags & MPTCP_PM_ADDR_FLAG_FULLMESH);
605 msk->pm.local_addr_used++;
606 nr = fill_remote_addresses_vec(msk, fullmesh, addrs);
608 __clear_bit(local->addr.id, msk->pm.id_avail_bitmap);
609 spin_unlock_bh(&msk->pm.lock);
610 for (i = 0; i < nr; i++)
611 __mptcp_subflow_connect(sk, &local->addr, &addrs[i]);
612 spin_lock_bh(&msk->pm.lock);
614 mptcp_pm_nl_check_work_pending(msk);
617 static void mptcp_pm_nl_fully_established(struct mptcp_sock *msk)
619 mptcp_pm_create_subflow_or_signal_addr(msk);
622 static void mptcp_pm_nl_subflow_established(struct mptcp_sock *msk)
624 mptcp_pm_create_subflow_or_signal_addr(msk);
627 /* Fill all the local addresses into the array addrs[],
628 * and return the array size.
630 static unsigned int fill_local_addresses_vec(struct mptcp_sock *msk,
631 struct mptcp_addr_info *addrs)
633 struct sock *sk = (struct sock *)msk;
634 struct mptcp_pm_addr_entry *entry;
635 struct mptcp_addr_info local;
636 struct pm_nl_pernet *pernet;
637 unsigned int subflows_max;
640 pernet = pm_nl_get_pernet_from_msk(msk);
641 subflows_max = mptcp_pm_get_subflows_max(msk);
644 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
645 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_FULLMESH))
648 if (entry->addr.family != sk->sk_family) {
649 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
650 if ((entry->addr.family == AF_INET &&
651 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) ||
652 (sk->sk_family == AF_INET &&
653 !ipv6_addr_v4mapped(&entry->addr.addr6)))
658 if (msk->pm.subflows < subflows_max) {
660 addrs[i++] = entry->addr;
665 /* If the array is empty, fill in the single
666 * 'IPADDRANY' local address
669 memset(&local, 0, sizeof(local));
670 local.family = msk->pm.remote.family;
679 static void mptcp_pm_nl_add_addr_received(struct mptcp_sock *msk)
681 struct mptcp_addr_info addrs[MPTCP_PM_ADDR_MAX];
682 struct sock *sk = (struct sock *)msk;
683 unsigned int add_addr_accept_max;
684 struct mptcp_addr_info remote;
685 unsigned int subflows_max;
688 add_addr_accept_max = mptcp_pm_get_add_addr_accept_max(msk);
689 subflows_max = mptcp_pm_get_subflows_max(msk);
691 pr_debug("accepted %d:%d remote family %d",
692 msk->pm.add_addr_accepted, add_addr_accept_max,
693 msk->pm.remote.family);
695 remote = msk->pm.remote;
696 mptcp_pm_announce_addr(msk, &remote, true);
697 mptcp_pm_nl_addr_send_ack(msk);
699 if (lookup_subflow_by_daddr(&msk->conn_list, &remote))
702 /* pick id 0 port, if none is provided the remote address */
704 remote.port = sk->sk_dport;
706 /* connect to the specified remote address, using whatever
707 * local address the routing configuration will pick.
709 nr = fill_local_addresses_vec(msk, addrs);
711 msk->pm.add_addr_accepted++;
712 if (msk->pm.add_addr_accepted >= add_addr_accept_max ||
713 msk->pm.subflows >= subflows_max)
714 WRITE_ONCE(msk->pm.accept_addr, false);
716 spin_unlock_bh(&msk->pm.lock);
717 for (i = 0; i < nr; i++)
718 __mptcp_subflow_connect(sk, &addrs[i], &remote);
719 spin_lock_bh(&msk->pm.lock);
722 void mptcp_pm_nl_addr_send_ack(struct mptcp_sock *msk)
724 struct mptcp_subflow_context *subflow;
726 msk_owned_by_me(msk);
727 lockdep_assert_held(&msk->pm.lock);
729 if (!mptcp_pm_should_add_signal(msk) &&
730 !mptcp_pm_should_rm_signal(msk))
733 subflow = list_first_entry_or_null(&msk->conn_list, typeof(*subflow), node);
735 mptcp_pm_send_ack(msk, subflow, false, false);
738 int mptcp_pm_nl_mp_prio_send_ack(struct mptcp_sock *msk,
739 struct mptcp_addr_info *addr,
740 struct mptcp_addr_info *rem,
743 struct mptcp_subflow_context *subflow;
745 pr_debug("bkup=%d", bkup);
747 mptcp_for_each_subflow(msk, subflow) {
748 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
749 struct mptcp_addr_info local, remote;
751 local_address((struct sock_common *)ssk, &local);
752 if (!mptcp_addresses_equal(&local, addr, addr->port))
755 if (rem && rem->family != AF_UNSPEC) {
756 remote_address((struct sock_common *)ssk, &remote);
757 if (!mptcp_addresses_equal(&remote, rem, rem->port))
761 __mptcp_pm_send_ack(msk, subflow, true, bkup);
768 static bool mptcp_local_id_match(const struct mptcp_sock *msk, u8 local_id, u8 id)
770 return local_id == id || (!local_id && msk->mpc_endpoint_id == id);
773 static void mptcp_pm_nl_rm_addr_or_subflow(struct mptcp_sock *msk,
774 const struct mptcp_rm_list *rm_list,
775 enum linux_mptcp_mib_field rm_type)
777 struct mptcp_subflow_context *subflow, *tmp;
778 struct sock *sk = (struct sock *)msk;
781 pr_debug("%s rm_list_nr %d",
782 rm_type == MPTCP_MIB_RMADDR ? "address" : "subflow", rm_list->nr);
784 msk_owned_by_me(msk);
786 if (sk->sk_state == TCP_LISTEN)
792 if (list_empty(&msk->conn_list))
795 for (i = 0; i < rm_list->nr; i++) {
796 u8 rm_id = rm_list->ids[i];
797 bool removed = false;
799 mptcp_for_each_subflow_safe(msk, subflow, tmp) {
800 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
801 int how = RCV_SHUTDOWN | SEND_SHUTDOWN;
802 u8 id = subflow->local_id;
804 if (rm_type == MPTCP_MIB_RMADDR && subflow->remote_id != rm_id)
806 if (rm_type == MPTCP_MIB_RMSUBFLOW && !mptcp_local_id_match(msk, id, rm_id))
809 pr_debug(" -> %s rm_list_ids[%d]=%u local_id=%u remote_id=%u mpc_id=%u",
810 rm_type == MPTCP_MIB_RMADDR ? "address" : "subflow",
811 i, rm_id, subflow->local_id, subflow->remote_id,
812 msk->mpc_endpoint_id);
813 spin_unlock_bh(&msk->pm.lock);
814 mptcp_subflow_shutdown(sk, ssk, how);
816 /* the following takes care of updating the subflows counter */
817 mptcp_close_ssk(sk, ssk, subflow);
818 spin_lock_bh(&msk->pm.lock);
821 __MPTCP_INC_STATS(sock_net(sk), rm_type);
823 if (rm_type == MPTCP_MIB_RMSUBFLOW)
824 __set_bit(rm_id ? rm_id : msk->mpc_endpoint_id, msk->pm.id_avail_bitmap);
828 if (!mptcp_pm_is_kernel(msk))
831 if (rm_type == MPTCP_MIB_RMADDR) {
832 msk->pm.add_addr_accepted--;
833 WRITE_ONCE(msk->pm.accept_addr, true);
834 } else if (rm_type == MPTCP_MIB_RMSUBFLOW) {
835 msk->pm.local_addr_used--;
840 static void mptcp_pm_nl_rm_addr_received(struct mptcp_sock *msk)
842 mptcp_pm_nl_rm_addr_or_subflow(msk, &msk->pm.rm_list_rx, MPTCP_MIB_RMADDR);
845 void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk,
846 const struct mptcp_rm_list *rm_list)
848 mptcp_pm_nl_rm_addr_or_subflow(msk, rm_list, MPTCP_MIB_RMSUBFLOW);
851 void mptcp_pm_nl_work(struct mptcp_sock *msk)
853 struct mptcp_pm_data *pm = &msk->pm;
855 msk_owned_by_me(msk);
857 if (!(pm->status & MPTCP_PM_WORK_MASK))
860 spin_lock_bh(&msk->pm.lock);
862 pr_debug("msk=%p status=%x", msk, pm->status);
863 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_RECEIVED)) {
864 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_RECEIVED);
865 mptcp_pm_nl_add_addr_received(msk);
867 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_SEND_ACK)) {
868 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_SEND_ACK);
869 mptcp_pm_nl_addr_send_ack(msk);
871 if (pm->status & BIT(MPTCP_PM_RM_ADDR_RECEIVED)) {
872 pm->status &= ~BIT(MPTCP_PM_RM_ADDR_RECEIVED);
873 mptcp_pm_nl_rm_addr_received(msk);
875 if (pm->status & BIT(MPTCP_PM_ESTABLISHED)) {
876 pm->status &= ~BIT(MPTCP_PM_ESTABLISHED);
877 mptcp_pm_nl_fully_established(msk);
879 if (pm->status & BIT(MPTCP_PM_SUBFLOW_ESTABLISHED)) {
880 pm->status &= ~BIT(MPTCP_PM_SUBFLOW_ESTABLISHED);
881 mptcp_pm_nl_subflow_established(msk);
884 spin_unlock_bh(&msk->pm.lock);
887 static bool address_use_port(struct mptcp_pm_addr_entry *entry)
889 return (entry->flags &
890 (MPTCP_PM_ADDR_FLAG_SIGNAL | MPTCP_PM_ADDR_FLAG_SUBFLOW)) ==
891 MPTCP_PM_ADDR_FLAG_SIGNAL;
894 /* caller must ensure the RCU grace period is already elapsed */
895 static void __mptcp_pm_release_addr_entry(struct mptcp_pm_addr_entry *entry)
898 sock_release(entry->lsk);
902 static int mptcp_pm_nl_append_new_local_addr(struct pm_nl_pernet *pernet,
903 struct mptcp_pm_addr_entry *entry)
905 struct mptcp_pm_addr_entry *cur, *del_entry = NULL;
906 unsigned int addr_max;
909 spin_lock_bh(&pernet->lock);
910 /* to keep the code simple, don't do IDR-like allocation for address ID,
911 * just bail when we exceed limits
913 if (pernet->next_id == MPTCP_PM_MAX_ADDR_ID)
915 if (pernet->addrs >= MPTCP_PM_ADDR_MAX)
917 if (test_bit(entry->addr.id, pernet->id_bitmap))
920 /* do not insert duplicate address, differentiate on port only
923 if (!address_use_port(entry))
924 entry->addr.port = 0;
925 list_for_each_entry(cur, &pernet->local_addr_list, list) {
926 if (mptcp_addresses_equal(&cur->addr, &entry->addr,
927 cur->addr.port || entry->addr.port)) {
928 /* allow replacing the exiting endpoint only if such
929 * endpoint is an implicit one and the user-space
930 * did not provide an endpoint id
932 if (!(cur->flags & MPTCP_PM_ADDR_FLAG_IMPLICIT))
938 entry->addr.id = cur->addr.id;
939 list_del_rcu(&cur->list);
945 if (!entry->addr.id) {
947 entry->addr.id = find_next_zero_bit(pernet->id_bitmap,
948 MPTCP_PM_MAX_ADDR_ID + 1,
950 if (!entry->addr.id && pernet->next_id != 1) {
959 __set_bit(entry->addr.id, pernet->id_bitmap);
960 if (entry->addr.id > pernet->next_id)
961 pernet->next_id = entry->addr.id;
963 if (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
964 addr_max = pernet->add_addr_signal_max;
965 WRITE_ONCE(pernet->add_addr_signal_max, addr_max + 1);
967 if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) {
968 addr_max = pernet->local_addr_max;
969 WRITE_ONCE(pernet->local_addr_max, addr_max + 1);
973 if (!entry->addr.port)
974 list_add_tail_rcu(&entry->list, &pernet->local_addr_list);
976 list_add_rcu(&entry->list, &pernet->local_addr_list);
977 ret = entry->addr.id;
980 spin_unlock_bh(&pernet->lock);
982 /* just replaced an existing entry, free it */
985 __mptcp_pm_release_addr_entry(del_entry);
990 static struct lock_class_key mptcp_slock_keys[2];
991 static struct lock_class_key mptcp_keys[2];
993 static int mptcp_pm_nl_create_listen_socket(struct sock *sk,
994 struct mptcp_pm_addr_entry *entry)
996 bool is_ipv6 = sk->sk_family == AF_INET6;
997 int addrlen = sizeof(struct sockaddr_in);
998 struct sockaddr_storage addr;
999 struct socket *ssock;
1004 err = sock_create_kern(sock_net(sk), entry->addr.family,
1005 SOCK_STREAM, IPPROTO_MPTCP, &entry->lsk);
1009 newsk = entry->lsk->sk;
1013 /* The subflow socket lock is acquired in a nested to the msk one
1014 * in several places, even by the TCP stack, and this msk is a kernel
1015 * socket: lockdep complains. Instead of propagating the _nested
1016 * modifiers in several places, re-init the lock class for the msk
1017 * socket to an mptcp specific one.
1019 sock_lock_init_class_and_name(newsk,
1020 is_ipv6 ? "mlock-AF_INET6" : "mlock-AF_INET",
1021 &mptcp_slock_keys[is_ipv6],
1022 is_ipv6 ? "msk_lock-AF_INET6" : "msk_lock-AF_INET",
1023 &mptcp_keys[is_ipv6]);
1026 ssock = __mptcp_nmpc_socket(mptcp_sk(newsk));
1027 release_sock(newsk);
1031 mptcp_info2sockaddr(&entry->addr, &addr, entry->addr.family);
1032 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1033 if (entry->addr.family == AF_INET6)
1034 addrlen = sizeof(struct sockaddr_in6);
1036 err = kernel_bind(ssock, (struct sockaddr *)&addr, addrlen);
1038 pr_warn("kernel_bind error, err=%d", err);
1042 inet_sk_state_store(newsk, TCP_LISTEN);
1043 err = kernel_listen(ssock, backlog);
1045 pr_warn("kernel_listen error, err=%d", err);
1052 int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc)
1054 struct mptcp_pm_addr_entry *entry;
1055 struct mptcp_addr_info skc_local;
1056 struct mptcp_addr_info msk_local;
1057 struct pm_nl_pernet *pernet;
1060 if (WARN_ON_ONCE(!msk))
1063 /* The 0 ID mapping is defined by the first subflow, copied into the msk
1066 local_address((struct sock_common *)msk, &msk_local);
1067 local_address((struct sock_common *)skc, &skc_local);
1068 if (mptcp_addresses_equal(&msk_local, &skc_local, false))
1071 if (mptcp_pm_is_userspace(msk))
1072 return mptcp_userspace_pm_get_local_id(msk, &skc_local);
1074 pernet = pm_nl_get_pernet_from_msk(msk);
1077 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
1078 if (mptcp_addresses_equal(&entry->addr, &skc_local, entry->addr.port)) {
1079 ret = entry->addr.id;
1087 /* address not found, add to local list */
1088 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
1092 entry->addr = skc_local;
1094 entry->addr.port = 0;
1096 entry->flags = MPTCP_PM_ADDR_FLAG_IMPLICIT;
1098 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
1105 #define MPTCP_PM_CMD_GRP_OFFSET 0
1106 #define MPTCP_PM_EV_GRP_OFFSET 1
1108 static const struct genl_multicast_group mptcp_pm_mcgrps[] = {
1109 [MPTCP_PM_CMD_GRP_OFFSET] = { .name = MPTCP_PM_CMD_GRP_NAME, },
1110 [MPTCP_PM_EV_GRP_OFFSET] = { .name = MPTCP_PM_EV_GRP_NAME,
1111 .flags = GENL_UNS_ADMIN_PERM,
1115 static const struct nla_policy
1116 mptcp_pm_addr_policy[MPTCP_PM_ADDR_ATTR_MAX + 1] = {
1117 [MPTCP_PM_ADDR_ATTR_FAMILY] = { .type = NLA_U16, },
1118 [MPTCP_PM_ADDR_ATTR_ID] = { .type = NLA_U8, },
1119 [MPTCP_PM_ADDR_ATTR_ADDR4] = { .type = NLA_U32, },
1120 [MPTCP_PM_ADDR_ATTR_ADDR6] =
1121 NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
1122 [MPTCP_PM_ADDR_ATTR_PORT] = { .type = NLA_U16 },
1123 [MPTCP_PM_ADDR_ATTR_FLAGS] = { .type = NLA_U32 },
1124 [MPTCP_PM_ADDR_ATTR_IF_IDX] = { .type = NLA_S32 },
1127 static const struct nla_policy mptcp_pm_policy[MPTCP_PM_ATTR_MAX + 1] = {
1128 [MPTCP_PM_ATTR_ADDR] =
1129 NLA_POLICY_NESTED(mptcp_pm_addr_policy),
1130 [MPTCP_PM_ATTR_RCV_ADD_ADDRS] = { .type = NLA_U32, },
1131 [MPTCP_PM_ATTR_SUBFLOWS] = { .type = NLA_U32, },
1132 [MPTCP_PM_ATTR_TOKEN] = { .type = NLA_U32, },
1133 [MPTCP_PM_ATTR_LOC_ID] = { .type = NLA_U8, },
1134 [MPTCP_PM_ATTR_ADDR_REMOTE] =
1135 NLA_POLICY_NESTED(mptcp_pm_addr_policy),
1138 void mptcp_pm_nl_subflow_chk_stale(const struct mptcp_sock *msk, struct sock *ssk)
1140 struct mptcp_subflow_context *iter, *subflow = mptcp_subflow_ctx(ssk);
1141 struct sock *sk = (struct sock *)msk;
1142 unsigned int active_max_loss_cnt;
1143 struct net *net = sock_net(sk);
1144 unsigned int stale_loss_cnt;
1147 stale_loss_cnt = mptcp_stale_loss_cnt(net);
1148 if (subflow->stale || !stale_loss_cnt || subflow->stale_count <= stale_loss_cnt)
1151 /* look for another available subflow not in loss state */
1152 active_max_loss_cnt = max_t(int, stale_loss_cnt - 1, 1);
1153 mptcp_for_each_subflow(msk, iter) {
1154 if (iter != subflow && mptcp_subflow_active(iter) &&
1155 iter->stale_count < active_max_loss_cnt) {
1156 /* we have some alternatives, try to mark this subflow as idle ...*/
1157 slow = lock_sock_fast(ssk);
1158 if (!tcp_rtx_and_write_queues_empty(ssk)) {
1160 __mptcp_retransmit_pending_data(sk);
1161 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_SUBFLOWSTALE);
1163 unlock_sock_fast(ssk, slow);
1165 /* always try to push the pending data regardless of re-injections:
1166 * we can possibly use backup subflows now, and subflow selection
1167 * is cheap under the msk socket lock
1169 __mptcp_push_pending(sk, 0);
1175 static int mptcp_pm_family_to_addr(int family)
1177 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1178 if (family == AF_INET6)
1179 return MPTCP_PM_ADDR_ATTR_ADDR6;
1181 return MPTCP_PM_ADDR_ATTR_ADDR4;
1184 static int mptcp_pm_parse_pm_addr_attr(struct nlattr *tb[],
1185 const struct nlattr *attr,
1186 struct genl_info *info,
1187 struct mptcp_addr_info *addr,
1188 bool require_family)
1193 GENL_SET_ERR_MSG(info, "missing address info");
1197 /* no validation needed - was already done via nested policy */
1198 err = nla_parse_nested_deprecated(tb, MPTCP_PM_ADDR_ATTR_MAX, attr,
1199 mptcp_pm_addr_policy, info->extack);
1203 if (tb[MPTCP_PM_ADDR_ATTR_ID])
1204 addr->id = nla_get_u8(tb[MPTCP_PM_ADDR_ATTR_ID]);
1206 if (!tb[MPTCP_PM_ADDR_ATTR_FAMILY]) {
1207 if (!require_family)
1210 NL_SET_ERR_MSG_ATTR(info->extack, attr,
1215 addr->family = nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_FAMILY]);
1216 if (addr->family != AF_INET
1217 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1218 && addr->family != AF_INET6
1221 NL_SET_ERR_MSG_ATTR(info->extack, attr,
1222 "unknown address family");
1225 addr_addr = mptcp_pm_family_to_addr(addr->family);
1226 if (!tb[addr_addr]) {
1227 NL_SET_ERR_MSG_ATTR(info->extack, attr,
1228 "missing address data");
1232 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1233 if (addr->family == AF_INET6)
1234 addr->addr6 = nla_get_in6_addr(tb[addr_addr]);
1237 addr->addr.s_addr = nla_get_in_addr(tb[addr_addr]);
1239 if (tb[MPTCP_PM_ADDR_ATTR_PORT])
1240 addr->port = htons(nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_PORT]));
1245 int mptcp_pm_parse_addr(struct nlattr *attr, struct genl_info *info,
1246 struct mptcp_addr_info *addr)
1248 struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1];
1250 memset(addr, 0, sizeof(*addr));
1252 return mptcp_pm_parse_pm_addr_attr(tb, attr, info, addr, true);
1255 int mptcp_pm_parse_entry(struct nlattr *attr, struct genl_info *info,
1256 bool require_family,
1257 struct mptcp_pm_addr_entry *entry)
1259 struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1];
1262 memset(entry, 0, sizeof(*entry));
1264 err = mptcp_pm_parse_pm_addr_attr(tb, attr, info, &entry->addr, require_family);
1268 if (tb[MPTCP_PM_ADDR_ATTR_IF_IDX]) {
1269 u32 val = nla_get_s32(tb[MPTCP_PM_ADDR_ATTR_IF_IDX]);
1271 entry->ifindex = val;
1274 if (tb[MPTCP_PM_ADDR_ATTR_FLAGS])
1275 entry->flags = nla_get_u32(tb[MPTCP_PM_ADDR_ATTR_FLAGS]);
1277 if (tb[MPTCP_PM_ADDR_ATTR_PORT])
1278 entry->addr.port = htons(nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_PORT]));
1283 static struct pm_nl_pernet *genl_info_pm_nl(struct genl_info *info)
1285 return pm_nl_get_pernet(genl_info_net(info));
1288 static int mptcp_nl_add_subflow_or_signal_addr(struct net *net)
1290 struct mptcp_sock *msk;
1291 long s_slot = 0, s_num = 0;
1293 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1294 struct sock *sk = (struct sock *)msk;
1296 if (!READ_ONCE(msk->fully_established) ||
1297 mptcp_pm_is_userspace(msk))
1301 spin_lock_bh(&msk->pm.lock);
1302 mptcp_pm_create_subflow_or_signal_addr(msk);
1303 spin_unlock_bh(&msk->pm.lock);
1314 static int mptcp_nl_cmd_add_addr(struct sk_buff *skb, struct genl_info *info)
1316 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1317 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1318 struct mptcp_pm_addr_entry addr, *entry;
1321 ret = mptcp_pm_parse_entry(attr, info, true, &addr);
1325 if (addr.addr.port && !(addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) {
1326 GENL_SET_ERR_MSG(info, "flags must have signal when using port");
1330 if (addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL &&
1331 addr.flags & MPTCP_PM_ADDR_FLAG_FULLMESH) {
1332 GENL_SET_ERR_MSG(info, "flags mustn't have both signal and fullmesh");
1336 if (addr.flags & MPTCP_PM_ADDR_FLAG_IMPLICIT) {
1337 GENL_SET_ERR_MSG(info, "can't create IMPLICIT endpoint");
1341 entry = kzalloc(sizeof(*entry), GFP_KERNEL_ACCOUNT);
1343 GENL_SET_ERR_MSG(info, "can't allocate addr");
1348 if (entry->addr.port) {
1349 ret = mptcp_pm_nl_create_listen_socket(skb->sk, entry);
1351 GENL_SET_ERR_MSG(info, "create listen socket error");
1355 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
1357 GENL_SET_ERR_MSG(info, "too many addresses or duplicate one");
1361 mptcp_nl_add_subflow_or_signal_addr(sock_net(skb->sk));
1365 __mptcp_pm_release_addr_entry(entry);
1369 int mptcp_pm_get_flags_and_ifindex_by_id(struct mptcp_sock *msk, unsigned int id,
1370 u8 *flags, int *ifindex)
1372 struct mptcp_pm_addr_entry *entry;
1373 struct sock *sk = (struct sock *)msk;
1374 struct net *net = sock_net(sk);
1380 if (mptcp_pm_is_userspace(msk))
1381 return mptcp_userspace_pm_get_flags_and_ifindex_by_id(msk,
1387 entry = __lookup_addr_by_id(pm_nl_get_pernet(net), id);
1389 *flags = entry->flags;
1390 *ifindex = entry->ifindex;
1398 static bool remove_anno_list_by_saddr(struct mptcp_sock *msk,
1399 const struct mptcp_addr_info *addr)
1401 struct mptcp_pm_add_entry *entry;
1403 entry = mptcp_pm_del_add_timer(msk, addr, false);
1405 list_del(&entry->list);
1413 static bool mptcp_pm_remove_anno_addr(struct mptcp_sock *msk,
1414 const struct mptcp_addr_info *addr,
1417 struct mptcp_rm_list list = { .nr = 0 };
1420 list.ids[list.nr++] = addr->id;
1422 ret = remove_anno_list_by_saddr(msk, addr);
1424 spin_lock_bh(&msk->pm.lock);
1425 mptcp_pm_remove_addr(msk, &list);
1426 spin_unlock_bh(&msk->pm.lock);
1431 static int mptcp_nl_remove_subflow_and_signal_addr(struct net *net,
1432 const struct mptcp_pm_addr_entry *entry)
1434 const struct mptcp_addr_info *addr = &entry->addr;
1435 struct mptcp_rm_list list = { .nr = 0 };
1436 long s_slot = 0, s_num = 0;
1437 struct mptcp_sock *msk;
1439 pr_debug("remove_id=%d", addr->id);
1441 list.ids[list.nr++] = addr->id;
1443 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1444 struct sock *sk = (struct sock *)msk;
1445 bool remove_subflow;
1447 if (mptcp_pm_is_userspace(msk))
1450 if (list_empty(&msk->conn_list)) {
1451 mptcp_pm_remove_anno_addr(msk, addr, false);
1456 remove_subflow = lookup_subflow_by_saddr(&msk->conn_list, addr);
1457 mptcp_pm_remove_anno_addr(msk, addr, remove_subflow &&
1458 !(entry->flags & MPTCP_PM_ADDR_FLAG_IMPLICIT));
1460 mptcp_pm_remove_subflow(msk, &list);
1471 static int mptcp_nl_remove_id_zero_address(struct net *net,
1472 struct mptcp_addr_info *addr)
1474 struct mptcp_rm_list list = { .nr = 0 };
1475 long s_slot = 0, s_num = 0;
1476 struct mptcp_sock *msk;
1478 list.ids[list.nr++] = 0;
1480 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1481 struct sock *sk = (struct sock *)msk;
1482 struct mptcp_addr_info msk_local;
1484 if (list_empty(&msk->conn_list) || mptcp_pm_is_userspace(msk))
1487 local_address((struct sock_common *)msk, &msk_local);
1488 if (!mptcp_addresses_equal(&msk_local, addr, addr->port))
1492 spin_lock_bh(&msk->pm.lock);
1493 mptcp_pm_remove_addr(msk, &list);
1494 mptcp_pm_nl_rm_subflow_received(msk, &list);
1495 spin_unlock_bh(&msk->pm.lock);
1506 static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info)
1508 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1509 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1510 struct mptcp_pm_addr_entry addr, *entry;
1511 unsigned int addr_max;
1514 ret = mptcp_pm_parse_entry(attr, info, false, &addr);
1518 /* the zero id address is special: the first address used by the msk
1519 * always gets such an id, so different subflows can have different zero
1520 * id addresses. Additionally zero id is not accounted for in id_bitmap.
1521 * Let's use an 'mptcp_rm_list' instead of the common remove code.
1523 if (addr.addr.id == 0)
1524 return mptcp_nl_remove_id_zero_address(sock_net(skb->sk), &addr.addr);
1526 spin_lock_bh(&pernet->lock);
1527 entry = __lookup_addr_by_id(pernet, addr.addr.id);
1529 GENL_SET_ERR_MSG(info, "address not found");
1530 spin_unlock_bh(&pernet->lock);
1533 if (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
1534 addr_max = pernet->add_addr_signal_max;
1535 WRITE_ONCE(pernet->add_addr_signal_max, addr_max - 1);
1537 if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) {
1538 addr_max = pernet->local_addr_max;
1539 WRITE_ONCE(pernet->local_addr_max, addr_max - 1);
1543 list_del_rcu(&entry->list);
1544 __clear_bit(entry->addr.id, pernet->id_bitmap);
1545 spin_unlock_bh(&pernet->lock);
1547 mptcp_nl_remove_subflow_and_signal_addr(sock_net(skb->sk), entry);
1549 __mptcp_pm_release_addr_entry(entry);
1554 void mptcp_pm_remove_addrs(struct mptcp_sock *msk, struct list_head *rm_list)
1556 struct mptcp_rm_list alist = { .nr = 0 };
1557 struct mptcp_pm_addr_entry *entry;
1559 list_for_each_entry(entry, rm_list, list) {
1560 remove_anno_list_by_saddr(msk, &entry->addr);
1561 if (alist.nr < MPTCP_RM_IDS_MAX)
1562 alist.ids[alist.nr++] = entry->addr.id;
1566 spin_lock_bh(&msk->pm.lock);
1567 mptcp_pm_remove_addr(msk, &alist);
1568 spin_unlock_bh(&msk->pm.lock);
1572 void mptcp_pm_remove_addrs_and_subflows(struct mptcp_sock *msk,
1573 struct list_head *rm_list)
1575 struct mptcp_rm_list alist = { .nr = 0 }, slist = { .nr = 0 };
1576 struct mptcp_pm_addr_entry *entry;
1578 list_for_each_entry(entry, rm_list, list) {
1579 if (lookup_subflow_by_saddr(&msk->conn_list, &entry->addr) &&
1580 slist.nr < MPTCP_RM_IDS_MAX)
1581 slist.ids[slist.nr++] = entry->addr.id;
1583 if (remove_anno_list_by_saddr(msk, &entry->addr) &&
1584 alist.nr < MPTCP_RM_IDS_MAX)
1585 alist.ids[alist.nr++] = entry->addr.id;
1589 spin_lock_bh(&msk->pm.lock);
1590 mptcp_pm_remove_addr(msk, &alist);
1591 spin_unlock_bh(&msk->pm.lock);
1594 mptcp_pm_remove_subflow(msk, &slist);
1597 static void mptcp_nl_remove_addrs_list(struct net *net,
1598 struct list_head *rm_list)
1600 long s_slot = 0, s_num = 0;
1601 struct mptcp_sock *msk;
1603 if (list_empty(rm_list))
1606 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1607 struct sock *sk = (struct sock *)msk;
1609 if (!mptcp_pm_is_userspace(msk)) {
1611 mptcp_pm_remove_addrs_and_subflows(msk, rm_list);
1620 /* caller must ensure the RCU grace period is already elapsed */
1621 static void __flush_addrs(struct list_head *list)
1623 while (!list_empty(list)) {
1624 struct mptcp_pm_addr_entry *cur;
1626 cur = list_entry(list->next,
1627 struct mptcp_pm_addr_entry, list);
1628 list_del_rcu(&cur->list);
1629 __mptcp_pm_release_addr_entry(cur);
1633 static void __reset_counters(struct pm_nl_pernet *pernet)
1635 WRITE_ONCE(pernet->add_addr_signal_max, 0);
1636 WRITE_ONCE(pernet->add_addr_accept_max, 0);
1637 WRITE_ONCE(pernet->local_addr_max, 0);
1641 static int mptcp_nl_cmd_flush_addrs(struct sk_buff *skb, struct genl_info *info)
1643 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1644 LIST_HEAD(free_list);
1646 spin_lock_bh(&pernet->lock);
1647 list_splice_init(&pernet->local_addr_list, &free_list);
1648 __reset_counters(pernet);
1649 pernet->next_id = 1;
1650 bitmap_zero(pernet->id_bitmap, MPTCP_PM_MAX_ADDR_ID + 1);
1651 spin_unlock_bh(&pernet->lock);
1652 mptcp_nl_remove_addrs_list(sock_net(skb->sk), &free_list);
1654 __flush_addrs(&free_list);
1658 static int mptcp_nl_fill_addr(struct sk_buff *skb,
1659 struct mptcp_pm_addr_entry *entry)
1661 struct mptcp_addr_info *addr = &entry->addr;
1662 struct nlattr *attr;
1664 attr = nla_nest_start(skb, MPTCP_PM_ATTR_ADDR);
1668 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_FAMILY, addr->family))
1669 goto nla_put_failure;
1670 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_PORT, ntohs(addr->port)))
1671 goto nla_put_failure;
1672 if (nla_put_u8(skb, MPTCP_PM_ADDR_ATTR_ID, addr->id))
1673 goto nla_put_failure;
1674 if (nla_put_u32(skb, MPTCP_PM_ADDR_ATTR_FLAGS, entry->flags))
1675 goto nla_put_failure;
1676 if (entry->ifindex &&
1677 nla_put_s32(skb, MPTCP_PM_ADDR_ATTR_IF_IDX, entry->ifindex))
1678 goto nla_put_failure;
1680 if (addr->family == AF_INET &&
1681 nla_put_in_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR4,
1683 goto nla_put_failure;
1684 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1685 else if (addr->family == AF_INET6 &&
1686 nla_put_in6_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR6, &addr->addr6))
1687 goto nla_put_failure;
1689 nla_nest_end(skb, attr);
1693 nla_nest_cancel(skb, attr);
1697 static int mptcp_nl_cmd_get_addr(struct sk_buff *skb, struct genl_info *info)
1699 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1700 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1701 struct mptcp_pm_addr_entry addr, *entry;
1702 struct sk_buff *msg;
1706 ret = mptcp_pm_parse_entry(attr, info, false, &addr);
1710 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1714 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1715 info->genlhdr->cmd);
1717 GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1722 spin_lock_bh(&pernet->lock);
1723 entry = __lookup_addr_by_id(pernet, addr.addr.id);
1725 GENL_SET_ERR_MSG(info, "address not found");
1730 ret = mptcp_nl_fill_addr(msg, entry);
1734 genlmsg_end(msg, reply);
1735 ret = genlmsg_reply(msg, info);
1736 spin_unlock_bh(&pernet->lock);
1740 spin_unlock_bh(&pernet->lock);
1747 static int mptcp_nl_cmd_dump_addrs(struct sk_buff *msg,
1748 struct netlink_callback *cb)
1750 struct net *net = sock_net(msg->sk);
1751 struct mptcp_pm_addr_entry *entry;
1752 struct pm_nl_pernet *pernet;
1753 int id = cb->args[0];
1757 pernet = pm_nl_get_pernet(net);
1759 spin_lock_bh(&pernet->lock);
1760 for (i = id; i < MPTCP_PM_MAX_ADDR_ID + 1; i++) {
1761 if (test_bit(i, pernet->id_bitmap)) {
1762 entry = __lookup_addr_by_id(pernet, i);
1766 if (entry->addr.id <= id)
1769 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid,
1770 cb->nlh->nlmsg_seq, &mptcp_genl_family,
1771 NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR);
1775 if (mptcp_nl_fill_addr(msg, entry) < 0) {
1776 genlmsg_cancel(msg, hdr);
1780 id = entry->addr.id;
1781 genlmsg_end(msg, hdr);
1784 spin_unlock_bh(&pernet->lock);
1790 static int parse_limit(struct genl_info *info, int id, unsigned int *limit)
1792 struct nlattr *attr = info->attrs[id];
1797 *limit = nla_get_u32(attr);
1798 if (*limit > MPTCP_PM_ADDR_MAX) {
1799 GENL_SET_ERR_MSG(info, "limit greater than maximum");
1806 mptcp_nl_cmd_set_limits(struct sk_buff *skb, struct genl_info *info)
1808 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1809 unsigned int rcv_addrs, subflows;
1812 spin_lock_bh(&pernet->lock);
1813 rcv_addrs = pernet->add_addr_accept_max;
1814 ret = parse_limit(info, MPTCP_PM_ATTR_RCV_ADD_ADDRS, &rcv_addrs);
1818 subflows = pernet->subflows_max;
1819 ret = parse_limit(info, MPTCP_PM_ATTR_SUBFLOWS, &subflows);
1823 WRITE_ONCE(pernet->add_addr_accept_max, rcv_addrs);
1824 WRITE_ONCE(pernet->subflows_max, subflows);
1827 spin_unlock_bh(&pernet->lock);
1832 mptcp_nl_cmd_get_limits(struct sk_buff *skb, struct genl_info *info)
1834 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1835 struct sk_buff *msg;
1838 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1842 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1843 MPTCP_PM_CMD_GET_LIMITS);
1847 if (nla_put_u32(msg, MPTCP_PM_ATTR_RCV_ADD_ADDRS,
1848 READ_ONCE(pernet->add_addr_accept_max)))
1851 if (nla_put_u32(msg, MPTCP_PM_ATTR_SUBFLOWS,
1852 READ_ONCE(pernet->subflows_max)))
1855 genlmsg_end(msg, reply);
1856 return genlmsg_reply(msg, info);
1859 GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1864 static void mptcp_pm_nl_fullmesh(struct mptcp_sock *msk,
1865 struct mptcp_addr_info *addr)
1867 struct mptcp_rm_list list = { .nr = 0 };
1869 list.ids[list.nr++] = addr->id;
1871 spin_lock_bh(&msk->pm.lock);
1872 mptcp_pm_nl_rm_subflow_received(msk, &list);
1873 mptcp_pm_create_subflow_or_signal_addr(msk);
1874 spin_unlock_bh(&msk->pm.lock);
1877 static int mptcp_nl_set_flags(struct net *net,
1878 struct mptcp_addr_info *addr,
1879 u8 bkup, u8 changed)
1881 long s_slot = 0, s_num = 0;
1882 struct mptcp_sock *msk;
1885 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1886 struct sock *sk = (struct sock *)msk;
1888 if (list_empty(&msk->conn_list) || mptcp_pm_is_userspace(msk))
1892 if (changed & MPTCP_PM_ADDR_FLAG_BACKUP)
1893 ret = mptcp_pm_nl_mp_prio_send_ack(msk, addr, NULL, bkup);
1894 if (changed & MPTCP_PM_ADDR_FLAG_FULLMESH)
1895 mptcp_pm_nl_fullmesh(msk, addr);
1906 static int mptcp_nl_cmd_set_flags(struct sk_buff *skb, struct genl_info *info)
1908 struct mptcp_pm_addr_entry addr = { .addr = { .family = AF_UNSPEC }, }, *entry;
1909 struct mptcp_pm_addr_entry remote = { .addr = { .family = AF_UNSPEC }, };
1910 struct nlattr *attr_rem = info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE];
1911 struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
1912 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1913 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1914 u8 changed, mask = MPTCP_PM_ADDR_FLAG_BACKUP |
1915 MPTCP_PM_ADDR_FLAG_FULLMESH;
1916 struct net *net = sock_net(skb->sk);
1917 u8 bkup = 0, lookup_by_id = 0;
1920 ret = mptcp_pm_parse_entry(attr, info, false, &addr);
1925 ret = mptcp_pm_parse_entry(attr_rem, info, false, &remote);
1930 if (addr.flags & MPTCP_PM_ADDR_FLAG_BACKUP)
1932 if (addr.addr.family == AF_UNSPEC) {
1939 return mptcp_userspace_pm_set_flags(sock_net(skb->sk),
1940 token, &addr, &remote, bkup);
1942 spin_lock_bh(&pernet->lock);
1943 entry = __lookup_addr(pernet, &addr.addr, lookup_by_id);
1945 spin_unlock_bh(&pernet->lock);
1948 if ((addr.flags & MPTCP_PM_ADDR_FLAG_FULLMESH) &&
1949 (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) {
1950 spin_unlock_bh(&pernet->lock);
1954 changed = (addr.flags ^ entry->flags) & mask;
1955 entry->flags = (entry->flags & ~mask) | (addr.flags & mask);
1957 spin_unlock_bh(&pernet->lock);
1959 mptcp_nl_set_flags(net, &addr.addr, bkup, changed);
1963 static void mptcp_nl_mcast_send(struct net *net, struct sk_buff *nlskb, gfp_t gfp)
1965 genlmsg_multicast_netns(&mptcp_genl_family, net,
1966 nlskb, 0, MPTCP_PM_EV_GRP_OFFSET, gfp);
1969 bool mptcp_userspace_pm_active(const struct mptcp_sock *msk)
1971 return genl_has_listeners(&mptcp_genl_family,
1972 sock_net((const struct sock *)msk),
1973 MPTCP_PM_EV_GRP_OFFSET);
1976 static int mptcp_event_add_subflow(struct sk_buff *skb, const struct sock *ssk)
1978 const struct inet_sock *issk = inet_sk(ssk);
1979 const struct mptcp_subflow_context *sf;
1981 if (nla_put_u16(skb, MPTCP_ATTR_FAMILY, ssk->sk_family))
1984 switch (ssk->sk_family) {
1986 if (nla_put_in_addr(skb, MPTCP_ATTR_SADDR4, issk->inet_saddr))
1988 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, issk->inet_daddr))
1991 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1993 const struct ipv6_pinfo *np = inet6_sk(ssk);
1995 if (nla_put_in6_addr(skb, MPTCP_ATTR_SADDR6, &np->saddr))
1997 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &ssk->sk_v6_daddr))
2007 if (nla_put_be16(skb, MPTCP_ATTR_SPORT, issk->inet_sport))
2009 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, issk->inet_dport))
2012 sf = mptcp_subflow_ctx(ssk);
2013 if (WARN_ON_ONCE(!sf))
2016 if (nla_put_u8(skb, MPTCP_ATTR_LOC_ID, sf->local_id))
2019 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, sf->remote_id))
2025 static int mptcp_event_put_token_and_ssk(struct sk_buff *skb,
2026 const struct mptcp_sock *msk,
2027 const struct sock *ssk)
2029 const struct sock *sk = (const struct sock *)msk;
2030 const struct mptcp_subflow_context *sf;
2033 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
2036 if (mptcp_event_add_subflow(skb, ssk))
2039 sf = mptcp_subflow_ctx(ssk);
2040 if (WARN_ON_ONCE(!sf))
2043 if (nla_put_u8(skb, MPTCP_ATTR_BACKUP, sf->backup))
2046 if (ssk->sk_bound_dev_if &&
2047 nla_put_s32(skb, MPTCP_ATTR_IF_IDX, ssk->sk_bound_dev_if))
2050 sk_err = ssk->sk_err;
2051 if (sk_err && sk->sk_state == TCP_ESTABLISHED &&
2052 nla_put_u8(skb, MPTCP_ATTR_ERROR, sk_err))
2058 static int mptcp_event_sub_established(struct sk_buff *skb,
2059 const struct mptcp_sock *msk,
2060 const struct sock *ssk)
2062 return mptcp_event_put_token_and_ssk(skb, msk, ssk);
2065 static int mptcp_event_sub_closed(struct sk_buff *skb,
2066 const struct mptcp_sock *msk,
2067 const struct sock *ssk)
2069 const struct mptcp_subflow_context *sf;
2071 if (mptcp_event_put_token_and_ssk(skb, msk, ssk))
2074 sf = mptcp_subflow_ctx(ssk);
2075 if (!sf->reset_seen)
2078 if (nla_put_u32(skb, MPTCP_ATTR_RESET_REASON, sf->reset_reason))
2081 if (nla_put_u32(skb, MPTCP_ATTR_RESET_FLAGS, sf->reset_transient))
2087 static int mptcp_event_created(struct sk_buff *skb,
2088 const struct mptcp_sock *msk,
2089 const struct sock *ssk)
2091 int err = nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token);
2096 if (nla_put_u8(skb, MPTCP_ATTR_SERVER_SIDE, READ_ONCE(msk->pm.server_side)))
2099 return mptcp_event_add_subflow(skb, ssk);
2102 void mptcp_event_addr_removed(const struct mptcp_sock *msk, uint8_t id)
2104 struct net *net = sock_net((const struct sock *)msk);
2105 struct nlmsghdr *nlh;
2106 struct sk_buff *skb;
2108 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
2111 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2115 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, MPTCP_EVENT_REMOVED);
2117 goto nla_put_failure;
2119 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
2120 goto nla_put_failure;
2122 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, id))
2123 goto nla_put_failure;
2125 genlmsg_end(skb, nlh);
2126 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC);
2133 void mptcp_event_addr_announced(const struct sock *ssk,
2134 const struct mptcp_addr_info *info)
2136 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
2137 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
2138 struct net *net = sock_net(ssk);
2139 struct nlmsghdr *nlh;
2140 struct sk_buff *skb;
2142 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
2145 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2149 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0,
2150 MPTCP_EVENT_ANNOUNCED);
2152 goto nla_put_failure;
2154 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
2155 goto nla_put_failure;
2157 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, info->id))
2158 goto nla_put_failure;
2160 if (nla_put_be16(skb, MPTCP_ATTR_DPORT,
2162 inet_sk(ssk)->inet_dport :
2164 goto nla_put_failure;
2166 switch (info->family) {
2168 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, info->addr.s_addr))
2169 goto nla_put_failure;
2171 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2173 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &info->addr6))
2174 goto nla_put_failure;
2179 goto nla_put_failure;
2182 genlmsg_end(skb, nlh);
2183 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC);
2190 void mptcp_event(enum mptcp_event_type type, const struct mptcp_sock *msk,
2191 const struct sock *ssk, gfp_t gfp)
2193 struct net *net = sock_net((const struct sock *)msk);
2194 struct nlmsghdr *nlh;
2195 struct sk_buff *skb;
2197 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
2200 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
2204 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, type);
2206 goto nla_put_failure;
2209 case MPTCP_EVENT_UNSPEC:
2212 case MPTCP_EVENT_CREATED:
2213 case MPTCP_EVENT_ESTABLISHED:
2214 if (mptcp_event_created(skb, msk, ssk) < 0)
2215 goto nla_put_failure;
2217 case MPTCP_EVENT_CLOSED:
2218 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token) < 0)
2219 goto nla_put_failure;
2221 case MPTCP_EVENT_ANNOUNCED:
2222 case MPTCP_EVENT_REMOVED:
2223 /* call mptcp_event_addr_announced()/removed instead */
2226 case MPTCP_EVENT_SUB_ESTABLISHED:
2227 case MPTCP_EVENT_SUB_PRIORITY:
2228 if (mptcp_event_sub_established(skb, msk, ssk) < 0)
2229 goto nla_put_failure;
2231 case MPTCP_EVENT_SUB_CLOSED:
2232 if (mptcp_event_sub_closed(skb, msk, ssk) < 0)
2233 goto nla_put_failure;
2237 genlmsg_end(skb, nlh);
2238 mptcp_nl_mcast_send(net, skb, gfp);
2245 static const struct genl_small_ops mptcp_pm_ops[] = {
2247 .cmd = MPTCP_PM_CMD_ADD_ADDR,
2248 .doit = mptcp_nl_cmd_add_addr,
2249 .flags = GENL_UNS_ADMIN_PERM,
2252 .cmd = MPTCP_PM_CMD_DEL_ADDR,
2253 .doit = mptcp_nl_cmd_del_addr,
2254 .flags = GENL_UNS_ADMIN_PERM,
2257 .cmd = MPTCP_PM_CMD_FLUSH_ADDRS,
2258 .doit = mptcp_nl_cmd_flush_addrs,
2259 .flags = GENL_UNS_ADMIN_PERM,
2262 .cmd = MPTCP_PM_CMD_GET_ADDR,
2263 .doit = mptcp_nl_cmd_get_addr,
2264 .dumpit = mptcp_nl_cmd_dump_addrs,
2267 .cmd = MPTCP_PM_CMD_SET_LIMITS,
2268 .doit = mptcp_nl_cmd_set_limits,
2269 .flags = GENL_UNS_ADMIN_PERM,
2272 .cmd = MPTCP_PM_CMD_GET_LIMITS,
2273 .doit = mptcp_nl_cmd_get_limits,
2276 .cmd = MPTCP_PM_CMD_SET_FLAGS,
2277 .doit = mptcp_nl_cmd_set_flags,
2278 .flags = GENL_UNS_ADMIN_PERM,
2281 .cmd = MPTCP_PM_CMD_ANNOUNCE,
2282 .doit = mptcp_nl_cmd_announce,
2283 .flags = GENL_UNS_ADMIN_PERM,
2286 .cmd = MPTCP_PM_CMD_REMOVE,
2287 .doit = mptcp_nl_cmd_remove,
2288 .flags = GENL_UNS_ADMIN_PERM,
2291 .cmd = MPTCP_PM_CMD_SUBFLOW_CREATE,
2292 .doit = mptcp_nl_cmd_sf_create,
2293 .flags = GENL_UNS_ADMIN_PERM,
2296 .cmd = MPTCP_PM_CMD_SUBFLOW_DESTROY,
2297 .doit = mptcp_nl_cmd_sf_destroy,
2298 .flags = GENL_UNS_ADMIN_PERM,
2302 static struct genl_family mptcp_genl_family __ro_after_init = {
2303 .name = MPTCP_PM_NAME,
2304 .version = MPTCP_PM_VER,
2305 .maxattr = MPTCP_PM_ATTR_MAX,
2306 .policy = mptcp_pm_policy,
2308 .module = THIS_MODULE,
2309 .small_ops = mptcp_pm_ops,
2310 .n_small_ops = ARRAY_SIZE(mptcp_pm_ops),
2311 .resv_start_op = MPTCP_PM_CMD_SUBFLOW_DESTROY + 1,
2312 .mcgrps = mptcp_pm_mcgrps,
2313 .n_mcgrps = ARRAY_SIZE(mptcp_pm_mcgrps),
2316 static int __net_init pm_nl_init_net(struct net *net)
2318 struct pm_nl_pernet *pernet = pm_nl_get_pernet(net);
2320 INIT_LIST_HEAD_RCU(&pernet->local_addr_list);
2322 /* Cit. 2 subflows ought to be enough for anybody. */
2323 pernet->subflows_max = 2;
2324 pernet->next_id = 1;
2325 pernet->stale_loss_cnt = 4;
2326 spin_lock_init(&pernet->lock);
2328 /* No need to initialize other pernet fields, the struct is zeroed at
2335 static void __net_exit pm_nl_exit_net(struct list_head *net_list)
2339 list_for_each_entry(net, net_list, exit_list) {
2340 struct pm_nl_pernet *pernet = pm_nl_get_pernet(net);
2342 /* net is removed from namespace list, can't race with
2343 * other modifiers, also netns core already waited for a
2346 __flush_addrs(&pernet->local_addr_list);
2350 static struct pernet_operations mptcp_pm_pernet_ops = {
2351 .init = pm_nl_init_net,
2352 .exit_batch = pm_nl_exit_net,
2353 .id = &pm_nl_pernet_id,
2354 .size = sizeof(struct pm_nl_pernet),
2357 void __init mptcp_pm_nl_init(void)
2359 if (register_pernet_subsys(&mptcp_pm_pernet_ops) < 0)
2360 panic("Failed to register MPTCP PM pernet subsystem.\n");
2362 if (genl_register_family(&mptcp_genl_family))
2363 panic("Failed to register MPTCP PM netlink family\n");