io_uring: ensure finish_wait() is always called in __io_uring_task_cancel()
[linux/fpc-iii.git] / net / mptcp / pm_netlink.c
bloba6d983d80576ad3db7960aad67d771370977301f
1 // SPDX-License-Identifier: GPL-2.0
2 /* Multipath TCP
4 * Copyright (c) 2020, Red Hat, Inc.
5 */
7 #define pr_fmt(fmt) "MPTCP: " fmt
9 #include <linux/inet.h>
10 #include <linux/kernel.h>
11 #include <net/tcp.h>
12 #include <net/netns/generic.h>
13 #include <net/mptcp.h>
14 #include <net/genetlink.h>
15 #include <uapi/linux/mptcp.h>
17 #include "protocol.h"
18 #include "mib.h"
20 /* forward declaration */
21 static struct genl_family mptcp_genl_family;
23 static int pm_nl_pernet_id;
25 struct mptcp_pm_addr_entry {
26 struct list_head list;
27 struct mptcp_addr_info addr;
28 struct rcu_head rcu;
31 struct mptcp_pm_add_entry {
32 struct list_head list;
33 struct mptcp_addr_info addr;
34 struct timer_list add_timer;
35 struct mptcp_sock *sock;
36 u8 retrans_times;
39 struct pm_nl_pernet {
40 /* protects pernet updates */
41 spinlock_t lock;
42 struct list_head local_addr_list;
43 unsigned int addrs;
44 unsigned int add_addr_signal_max;
45 unsigned int add_addr_accept_max;
46 unsigned int local_addr_max;
47 unsigned int subflows_max;
48 unsigned int next_id;
51 #define MPTCP_PM_ADDR_MAX 8
52 #define ADD_ADDR_RETRANS_MAX 3
54 static bool addresses_equal(const struct mptcp_addr_info *a,
55 struct mptcp_addr_info *b, bool use_port)
57 bool addr_equals = false;
59 if (a->family != b->family)
60 return false;
62 if (a->family == AF_INET)
63 addr_equals = a->addr.s_addr == b->addr.s_addr;
64 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
65 else
66 addr_equals = !ipv6_addr_cmp(&a->addr6, &b->addr6);
67 #endif
69 if (!addr_equals)
70 return false;
71 if (!use_port)
72 return true;
74 return a->port == b->port;
77 static bool address_zero(const struct mptcp_addr_info *addr)
79 struct mptcp_addr_info zero;
81 memset(&zero, 0, sizeof(zero));
82 zero.family = addr->family;
84 return addresses_equal(addr, &zero, false);
87 static void local_address(const struct sock_common *skc,
88 struct mptcp_addr_info *addr)
90 addr->port = 0;
91 addr->family = skc->skc_family;
92 if (addr->family == AF_INET)
93 addr->addr.s_addr = skc->skc_rcv_saddr;
94 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
95 else if (addr->family == AF_INET6)
96 addr->addr6 = skc->skc_v6_rcv_saddr;
97 #endif
100 static void remote_address(const struct sock_common *skc,
101 struct mptcp_addr_info *addr)
103 addr->family = skc->skc_family;
104 addr->port = skc->skc_dport;
105 if (addr->family == AF_INET)
106 addr->addr.s_addr = skc->skc_daddr;
107 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
108 else if (addr->family == AF_INET6)
109 addr->addr6 = skc->skc_v6_daddr;
110 #endif
113 static bool lookup_subflow_by_saddr(const struct list_head *list,
114 struct mptcp_addr_info *saddr)
116 struct mptcp_subflow_context *subflow;
117 struct mptcp_addr_info cur;
118 struct sock_common *skc;
120 list_for_each_entry(subflow, list, node) {
121 skc = (struct sock_common *)mptcp_subflow_tcp_sock(subflow);
123 local_address(skc, &cur);
124 if (addresses_equal(&cur, saddr, false))
125 return true;
128 return false;
131 static struct mptcp_pm_addr_entry *
132 select_local_address(const struct pm_nl_pernet *pernet,
133 struct mptcp_sock *msk)
135 struct mptcp_pm_addr_entry *entry, *ret = NULL;
137 rcu_read_lock();
138 __mptcp_flush_join_list(msk);
139 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
140 if (!(entry->addr.flags & MPTCP_PM_ADDR_FLAG_SUBFLOW))
141 continue;
143 /* avoid any address already in use by subflows and
144 * pending join
146 if (entry->addr.family == ((struct sock *)msk)->sk_family &&
147 !lookup_subflow_by_saddr(&msk->conn_list, &entry->addr)) {
148 ret = entry;
149 break;
152 rcu_read_unlock();
153 return ret;
156 static struct mptcp_pm_addr_entry *
157 select_signal_address(struct pm_nl_pernet *pernet, unsigned int pos)
159 struct mptcp_pm_addr_entry *entry, *ret = NULL;
160 int i = 0;
162 rcu_read_lock();
163 /* do not keep any additional per socket state, just signal
164 * the address list in order.
165 * Note: removal from the local address list during the msk life-cycle
166 * can lead to additional addresses not being announced.
168 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
169 if (!(entry->addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL))
170 continue;
171 if (i++ == pos) {
172 ret = entry;
173 break;
176 rcu_read_unlock();
177 return ret;
180 static void check_work_pending(struct mptcp_sock *msk)
182 if (msk->pm.add_addr_signaled == msk->pm.add_addr_signal_max &&
183 (msk->pm.local_addr_used == msk->pm.local_addr_max ||
184 msk->pm.subflows == msk->pm.subflows_max))
185 WRITE_ONCE(msk->pm.work_pending, false);
188 static struct mptcp_pm_add_entry *
189 lookup_anno_list_by_saddr(struct mptcp_sock *msk,
190 struct mptcp_addr_info *addr)
192 struct mptcp_pm_add_entry *entry;
194 list_for_each_entry(entry, &msk->pm.anno_list, list) {
195 if (addresses_equal(&entry->addr, addr, false))
196 return entry;
199 return NULL;
202 static void mptcp_pm_add_timer(struct timer_list *timer)
204 struct mptcp_pm_add_entry *entry = from_timer(entry, timer, add_timer);
205 struct mptcp_sock *msk = entry->sock;
206 struct sock *sk = (struct sock *)msk;
208 pr_debug("msk=%p", msk);
210 if (!msk)
211 return;
213 if (inet_sk_state_load(sk) == TCP_CLOSE)
214 return;
216 if (!entry->addr.id)
217 return;
219 if (mptcp_pm_should_add_signal(msk)) {
220 sk_reset_timer(sk, timer, jiffies + TCP_RTO_MAX / 8);
221 goto out;
224 spin_lock_bh(&msk->pm.lock);
226 if (!mptcp_pm_should_add_signal(msk)) {
227 pr_debug("retransmit ADD_ADDR id=%d", entry->addr.id);
228 mptcp_pm_announce_addr(msk, &entry->addr, false, entry->addr.port);
229 mptcp_pm_add_addr_send_ack(msk);
230 entry->retrans_times++;
233 if (entry->retrans_times < ADD_ADDR_RETRANS_MAX)
234 sk_reset_timer(sk, timer,
235 jiffies + mptcp_get_add_addr_timeout(sock_net(sk)));
237 spin_unlock_bh(&msk->pm.lock);
239 out:
240 __sock_put(sk);
243 struct mptcp_pm_add_entry *
244 mptcp_pm_del_add_timer(struct mptcp_sock *msk,
245 struct mptcp_addr_info *addr)
247 struct mptcp_pm_add_entry *entry;
248 struct sock *sk = (struct sock *)msk;
250 spin_lock_bh(&msk->pm.lock);
251 entry = lookup_anno_list_by_saddr(msk, addr);
252 if (entry)
253 entry->retrans_times = ADD_ADDR_RETRANS_MAX;
254 spin_unlock_bh(&msk->pm.lock);
256 if (entry)
257 sk_stop_timer_sync(sk, &entry->add_timer);
259 return entry;
262 static bool mptcp_pm_alloc_anno_list(struct mptcp_sock *msk,
263 struct mptcp_pm_addr_entry *entry)
265 struct mptcp_pm_add_entry *add_entry = NULL;
266 struct sock *sk = (struct sock *)msk;
267 struct net *net = sock_net(sk);
269 if (lookup_anno_list_by_saddr(msk, &entry->addr))
270 return false;
272 add_entry = kmalloc(sizeof(*add_entry), GFP_ATOMIC);
273 if (!add_entry)
274 return false;
276 list_add(&add_entry->list, &msk->pm.anno_list);
278 add_entry->addr = entry->addr;
279 add_entry->sock = msk;
280 add_entry->retrans_times = 0;
282 timer_setup(&add_entry->add_timer, mptcp_pm_add_timer, 0);
283 sk_reset_timer(sk, &add_entry->add_timer,
284 jiffies + mptcp_get_add_addr_timeout(net));
286 return true;
289 void mptcp_pm_free_anno_list(struct mptcp_sock *msk)
291 struct mptcp_pm_add_entry *entry, *tmp;
292 struct sock *sk = (struct sock *)msk;
293 LIST_HEAD(free_list);
295 pr_debug("msk=%p", msk);
297 spin_lock_bh(&msk->pm.lock);
298 list_splice_init(&msk->pm.anno_list, &free_list);
299 spin_unlock_bh(&msk->pm.lock);
301 list_for_each_entry_safe(entry, tmp, &free_list, list) {
302 sk_stop_timer_sync(sk, &entry->add_timer);
303 kfree(entry);
307 static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk)
309 struct mptcp_addr_info remote = { 0 };
310 struct sock *sk = (struct sock *)msk;
311 struct mptcp_pm_addr_entry *local;
312 struct pm_nl_pernet *pernet;
314 pernet = net_generic(sock_net(sk), pm_nl_pernet_id);
316 pr_debug("local %d:%d signal %d:%d subflows %d:%d\n",
317 msk->pm.local_addr_used, msk->pm.local_addr_max,
318 msk->pm.add_addr_signaled, msk->pm.add_addr_signal_max,
319 msk->pm.subflows, msk->pm.subflows_max);
321 /* check first for announce */
322 if (msk->pm.add_addr_signaled < msk->pm.add_addr_signal_max) {
323 local = select_signal_address(pernet,
324 msk->pm.add_addr_signaled);
326 if (local) {
327 if (mptcp_pm_alloc_anno_list(msk, local)) {
328 msk->pm.add_addr_signaled++;
329 mptcp_pm_announce_addr(msk, &local->addr, false, local->addr.port);
330 mptcp_pm_nl_add_addr_send_ack(msk);
332 } else {
333 /* pick failed, avoid fourther attempts later */
334 msk->pm.local_addr_used = msk->pm.add_addr_signal_max;
337 check_work_pending(msk);
340 /* check if should create a new subflow */
341 if (msk->pm.local_addr_used < msk->pm.local_addr_max &&
342 msk->pm.subflows < msk->pm.subflows_max) {
343 remote_address((struct sock_common *)sk, &remote);
345 local = select_local_address(pernet, msk);
346 if (local) {
347 msk->pm.local_addr_used++;
348 msk->pm.subflows++;
349 check_work_pending(msk);
350 spin_unlock_bh(&msk->pm.lock);
351 __mptcp_subflow_connect(sk, &local->addr, &remote);
352 spin_lock_bh(&msk->pm.lock);
353 return;
356 /* lookup failed, avoid fourther attempts later */
357 msk->pm.local_addr_used = msk->pm.local_addr_max;
358 check_work_pending(msk);
362 void mptcp_pm_nl_fully_established(struct mptcp_sock *msk)
364 mptcp_pm_create_subflow_or_signal_addr(msk);
367 void mptcp_pm_nl_subflow_established(struct mptcp_sock *msk)
369 mptcp_pm_create_subflow_or_signal_addr(msk);
372 void mptcp_pm_nl_add_addr_received(struct mptcp_sock *msk)
374 struct sock *sk = (struct sock *)msk;
375 struct mptcp_addr_info remote;
376 struct mptcp_addr_info local;
377 bool use_port = false;
379 pr_debug("accepted %d:%d remote family %d",
380 msk->pm.add_addr_accepted, msk->pm.add_addr_accept_max,
381 msk->pm.remote.family);
382 msk->pm.add_addr_accepted++;
383 msk->pm.subflows++;
384 if (msk->pm.add_addr_accepted >= msk->pm.add_addr_accept_max ||
385 msk->pm.subflows >= msk->pm.subflows_max)
386 WRITE_ONCE(msk->pm.accept_addr, false);
388 /* connect to the specified remote address, using whatever
389 * local address the routing configuration will pick.
391 remote = msk->pm.remote;
392 if (!remote.port)
393 remote.port = sk->sk_dport;
394 else
395 use_port = true;
396 memset(&local, 0, sizeof(local));
397 local.family = remote.family;
399 spin_unlock_bh(&msk->pm.lock);
400 __mptcp_subflow_connect(sk, &local, &remote);
401 spin_lock_bh(&msk->pm.lock);
403 mptcp_pm_announce_addr(msk, &remote, true, use_port);
404 mptcp_pm_nl_add_addr_send_ack(msk);
407 void mptcp_pm_nl_add_addr_send_ack(struct mptcp_sock *msk)
409 struct mptcp_subflow_context *subflow;
411 if (!mptcp_pm_should_add_signal_ipv6(msk) &&
412 !mptcp_pm_should_add_signal_port(msk))
413 return;
415 __mptcp_flush_join_list(msk);
416 subflow = list_first_entry_or_null(&msk->conn_list, typeof(*subflow), node);
417 if (subflow) {
418 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
419 u8 add_addr;
421 spin_unlock_bh(&msk->pm.lock);
422 if (mptcp_pm_should_add_signal_ipv6(msk))
423 pr_debug("send ack for add_addr6");
424 if (mptcp_pm_should_add_signal_port(msk))
425 pr_debug("send ack for add_addr_port");
427 lock_sock(ssk);
428 tcp_send_ack(ssk);
429 release_sock(ssk);
430 spin_lock_bh(&msk->pm.lock);
432 add_addr = READ_ONCE(msk->pm.addr_signal);
433 if (mptcp_pm_should_add_signal_ipv6(msk))
434 add_addr &= ~BIT(MPTCP_ADD_ADDR_IPV6);
435 if (mptcp_pm_should_add_signal_port(msk))
436 add_addr &= ~BIT(MPTCP_ADD_ADDR_PORT);
437 WRITE_ONCE(msk->pm.addr_signal, add_addr);
441 void mptcp_pm_nl_rm_addr_received(struct mptcp_sock *msk)
443 struct mptcp_subflow_context *subflow, *tmp;
444 struct sock *sk = (struct sock *)msk;
446 pr_debug("address rm_id %d", msk->pm.rm_id);
448 if (!msk->pm.rm_id)
449 return;
451 if (list_empty(&msk->conn_list))
452 return;
454 list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
455 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
456 int how = RCV_SHUTDOWN | SEND_SHUTDOWN;
458 if (msk->pm.rm_id != subflow->remote_id)
459 continue;
461 spin_unlock_bh(&msk->pm.lock);
462 mptcp_subflow_shutdown(sk, ssk, how);
463 __mptcp_close_ssk(sk, ssk, subflow);
464 spin_lock_bh(&msk->pm.lock);
466 msk->pm.add_addr_accepted--;
467 msk->pm.subflows--;
468 WRITE_ONCE(msk->pm.accept_addr, true);
470 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMADDR);
472 break;
476 void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk, u8 rm_id)
478 struct mptcp_subflow_context *subflow, *tmp;
479 struct sock *sk = (struct sock *)msk;
481 pr_debug("subflow rm_id %d", rm_id);
483 if (!rm_id)
484 return;
486 if (list_empty(&msk->conn_list))
487 return;
489 list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
490 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
491 int how = RCV_SHUTDOWN | SEND_SHUTDOWN;
493 if (rm_id != subflow->local_id)
494 continue;
496 spin_unlock_bh(&msk->pm.lock);
497 mptcp_subflow_shutdown(sk, ssk, how);
498 __mptcp_close_ssk(sk, ssk, subflow);
499 spin_lock_bh(&msk->pm.lock);
501 msk->pm.local_addr_used--;
502 msk->pm.subflows--;
504 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMSUBFLOW);
506 break;
510 static bool address_use_port(struct mptcp_pm_addr_entry *entry)
512 return (entry->addr.flags &
513 (MPTCP_PM_ADDR_FLAG_SIGNAL | MPTCP_PM_ADDR_FLAG_SUBFLOW)) ==
514 MPTCP_PM_ADDR_FLAG_SIGNAL;
517 static int mptcp_pm_nl_append_new_local_addr(struct pm_nl_pernet *pernet,
518 struct mptcp_pm_addr_entry *entry)
520 struct mptcp_pm_addr_entry *cur;
521 int ret = -EINVAL;
523 spin_lock_bh(&pernet->lock);
524 /* to keep the code simple, don't do IDR-like allocation for address ID,
525 * just bail when we exceed limits
527 if (pernet->next_id > 255)
528 goto out;
529 if (pernet->addrs >= MPTCP_PM_ADDR_MAX)
530 goto out;
532 /* do not insert duplicate address, differentiate on port only
533 * singled addresses
535 list_for_each_entry(cur, &pernet->local_addr_list, list) {
536 if (addresses_equal(&cur->addr, &entry->addr,
537 address_use_port(entry) &&
538 address_use_port(cur)))
539 goto out;
542 if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL)
543 pernet->add_addr_signal_max++;
544 if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SUBFLOW)
545 pernet->local_addr_max++;
547 entry->addr.id = pernet->next_id++;
548 pernet->addrs++;
549 list_add_tail_rcu(&entry->list, &pernet->local_addr_list);
550 ret = entry->addr.id;
552 out:
553 spin_unlock_bh(&pernet->lock);
554 return ret;
557 int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc)
559 struct mptcp_pm_addr_entry *entry;
560 struct mptcp_addr_info skc_local;
561 struct mptcp_addr_info msk_local;
562 struct pm_nl_pernet *pernet;
563 int ret = -1;
565 if (WARN_ON_ONCE(!msk))
566 return -1;
568 /* The 0 ID mapping is defined by the first subflow, copied into the msk
569 * addr
571 local_address((struct sock_common *)msk, &msk_local);
572 local_address((struct sock_common *)skc, &skc_local);
573 if (addresses_equal(&msk_local, &skc_local, false))
574 return 0;
576 if (address_zero(&skc_local))
577 return 0;
579 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
581 rcu_read_lock();
582 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
583 if (addresses_equal(&entry->addr, &skc_local, false)) {
584 ret = entry->addr.id;
585 break;
588 rcu_read_unlock();
589 if (ret >= 0)
590 return ret;
592 /* address not found, add to local list */
593 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
594 if (!entry)
595 return -ENOMEM;
597 entry->addr = skc_local;
598 entry->addr.ifindex = 0;
599 entry->addr.flags = 0;
600 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
601 if (ret < 0)
602 kfree(entry);
604 return ret;
607 void mptcp_pm_nl_data_init(struct mptcp_sock *msk)
609 struct mptcp_pm_data *pm = &msk->pm;
610 struct pm_nl_pernet *pernet;
611 bool subflows;
613 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
615 pm->add_addr_signal_max = READ_ONCE(pernet->add_addr_signal_max);
616 pm->add_addr_accept_max = READ_ONCE(pernet->add_addr_accept_max);
617 pm->local_addr_max = READ_ONCE(pernet->local_addr_max);
618 pm->subflows_max = READ_ONCE(pernet->subflows_max);
619 subflows = !!pm->subflows_max;
620 WRITE_ONCE(pm->work_pending, (!!pm->local_addr_max && subflows) ||
621 !!pm->add_addr_signal_max);
622 WRITE_ONCE(pm->accept_addr, !!pm->add_addr_accept_max && subflows);
623 WRITE_ONCE(pm->accept_subflow, subflows);
626 #define MPTCP_PM_CMD_GRP_OFFSET 0
628 static const struct genl_multicast_group mptcp_pm_mcgrps[] = {
629 [MPTCP_PM_CMD_GRP_OFFSET] = { .name = MPTCP_PM_CMD_GRP_NAME, },
632 static const struct nla_policy
633 mptcp_pm_addr_policy[MPTCP_PM_ADDR_ATTR_MAX + 1] = {
634 [MPTCP_PM_ADDR_ATTR_FAMILY] = { .type = NLA_U16, },
635 [MPTCP_PM_ADDR_ATTR_ID] = { .type = NLA_U8, },
636 [MPTCP_PM_ADDR_ATTR_ADDR4] = { .type = NLA_U32, },
637 [MPTCP_PM_ADDR_ATTR_ADDR6] =
638 NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
639 [MPTCP_PM_ADDR_ATTR_PORT] = { .type = NLA_U16 },
640 [MPTCP_PM_ADDR_ATTR_FLAGS] = { .type = NLA_U32 },
641 [MPTCP_PM_ADDR_ATTR_IF_IDX] = { .type = NLA_S32 },
644 static const struct nla_policy mptcp_pm_policy[MPTCP_PM_ATTR_MAX + 1] = {
645 [MPTCP_PM_ATTR_ADDR] =
646 NLA_POLICY_NESTED(mptcp_pm_addr_policy),
647 [MPTCP_PM_ATTR_RCV_ADD_ADDRS] = { .type = NLA_U32, },
648 [MPTCP_PM_ATTR_SUBFLOWS] = { .type = NLA_U32, },
651 static int mptcp_pm_family_to_addr(int family)
653 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
654 if (family == AF_INET6)
655 return MPTCP_PM_ADDR_ATTR_ADDR6;
656 #endif
657 return MPTCP_PM_ADDR_ATTR_ADDR4;
660 static int mptcp_pm_parse_addr(struct nlattr *attr, struct genl_info *info,
661 bool require_family,
662 struct mptcp_pm_addr_entry *entry)
664 struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1];
665 int err, addr_addr;
667 if (!attr) {
668 GENL_SET_ERR_MSG(info, "missing address info");
669 return -EINVAL;
672 /* no validation needed - was already done via nested policy */
673 err = nla_parse_nested_deprecated(tb, MPTCP_PM_ADDR_ATTR_MAX, attr,
674 mptcp_pm_addr_policy, info->extack);
675 if (err)
676 return err;
678 memset(entry, 0, sizeof(*entry));
679 if (!tb[MPTCP_PM_ADDR_ATTR_FAMILY]) {
680 if (!require_family)
681 goto skip_family;
683 NL_SET_ERR_MSG_ATTR(info->extack, attr,
684 "missing family");
685 return -EINVAL;
688 entry->addr.family = nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_FAMILY]);
689 if (entry->addr.family != AF_INET
690 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
691 && entry->addr.family != AF_INET6
692 #endif
694 NL_SET_ERR_MSG_ATTR(info->extack, attr,
695 "unknown address family");
696 return -EINVAL;
698 addr_addr = mptcp_pm_family_to_addr(entry->addr.family);
699 if (!tb[addr_addr]) {
700 NL_SET_ERR_MSG_ATTR(info->extack, attr,
701 "missing address data");
702 return -EINVAL;
705 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
706 if (entry->addr.family == AF_INET6)
707 entry->addr.addr6 = nla_get_in6_addr(tb[addr_addr]);
708 else
709 #endif
710 entry->addr.addr.s_addr = nla_get_in_addr(tb[addr_addr]);
712 skip_family:
713 if (tb[MPTCP_PM_ADDR_ATTR_IF_IDX]) {
714 u32 val = nla_get_s32(tb[MPTCP_PM_ADDR_ATTR_IF_IDX]);
716 entry->addr.ifindex = val;
719 if (tb[MPTCP_PM_ADDR_ATTR_ID])
720 entry->addr.id = nla_get_u8(tb[MPTCP_PM_ADDR_ATTR_ID]);
722 if (tb[MPTCP_PM_ADDR_ATTR_FLAGS])
723 entry->addr.flags = nla_get_u32(tb[MPTCP_PM_ADDR_ATTR_FLAGS]);
725 return 0;
728 static struct pm_nl_pernet *genl_info_pm_nl(struct genl_info *info)
730 return net_generic(genl_info_net(info), pm_nl_pernet_id);
733 static int mptcp_nl_cmd_add_addr(struct sk_buff *skb, struct genl_info *info)
735 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
736 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
737 struct mptcp_pm_addr_entry addr, *entry;
738 int ret;
740 ret = mptcp_pm_parse_addr(attr, info, true, &addr);
741 if (ret < 0)
742 return ret;
744 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
745 if (!entry) {
746 GENL_SET_ERR_MSG(info, "can't allocate addr");
747 return -ENOMEM;
750 *entry = addr;
751 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
752 if (ret < 0) {
753 GENL_SET_ERR_MSG(info, "too many addresses or duplicate one");
754 kfree(entry);
755 return ret;
758 return 0;
761 static struct mptcp_pm_addr_entry *
762 __lookup_addr_by_id(struct pm_nl_pernet *pernet, unsigned int id)
764 struct mptcp_pm_addr_entry *entry;
766 list_for_each_entry(entry, &pernet->local_addr_list, list) {
767 if (entry->addr.id == id)
768 return entry;
770 return NULL;
773 static bool remove_anno_list_by_saddr(struct mptcp_sock *msk,
774 struct mptcp_addr_info *addr)
776 struct mptcp_pm_add_entry *entry;
778 entry = mptcp_pm_del_add_timer(msk, addr);
779 if (entry) {
780 list_del(&entry->list);
781 kfree(entry);
782 return true;
785 return false;
788 static bool mptcp_pm_remove_anno_addr(struct mptcp_sock *msk,
789 struct mptcp_addr_info *addr,
790 bool force)
792 bool ret;
794 ret = remove_anno_list_by_saddr(msk, addr);
795 if (ret || force) {
796 spin_lock_bh(&msk->pm.lock);
797 mptcp_pm_remove_addr(msk, addr->id);
798 spin_unlock_bh(&msk->pm.lock);
800 return ret;
803 static int mptcp_nl_remove_subflow_and_signal_addr(struct net *net,
804 struct mptcp_addr_info *addr)
806 struct mptcp_sock *msk;
807 long s_slot = 0, s_num = 0;
809 pr_debug("remove_id=%d", addr->id);
811 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
812 struct sock *sk = (struct sock *)msk;
813 bool remove_subflow;
815 if (list_empty(&msk->conn_list)) {
816 mptcp_pm_remove_anno_addr(msk, addr, false);
817 goto next;
820 lock_sock(sk);
821 remove_subflow = lookup_subflow_by_saddr(&msk->conn_list, addr);
822 mptcp_pm_remove_anno_addr(msk, addr, remove_subflow);
823 if (remove_subflow)
824 mptcp_pm_remove_subflow(msk, addr->id);
825 release_sock(sk);
827 next:
828 sock_put(sk);
829 cond_resched();
832 return 0;
835 static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info)
837 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
838 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
839 struct mptcp_pm_addr_entry addr, *entry;
840 int ret;
842 ret = mptcp_pm_parse_addr(attr, info, false, &addr);
843 if (ret < 0)
844 return ret;
846 spin_lock_bh(&pernet->lock);
847 entry = __lookup_addr_by_id(pernet, addr.addr.id);
848 if (!entry) {
849 GENL_SET_ERR_MSG(info, "address not found");
850 spin_unlock_bh(&pernet->lock);
851 return -EINVAL;
853 if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL)
854 pernet->add_addr_signal_max--;
855 if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SUBFLOW)
856 pernet->local_addr_max--;
858 pernet->addrs--;
859 list_del_rcu(&entry->list);
860 spin_unlock_bh(&pernet->lock);
862 mptcp_nl_remove_subflow_and_signal_addr(sock_net(skb->sk), &entry->addr);
863 kfree_rcu(entry, rcu);
865 return ret;
868 static void __flush_addrs(struct net *net, struct list_head *list)
870 while (!list_empty(list)) {
871 struct mptcp_pm_addr_entry *cur;
873 cur = list_entry(list->next,
874 struct mptcp_pm_addr_entry, list);
875 mptcp_nl_remove_subflow_and_signal_addr(net, &cur->addr);
876 list_del_rcu(&cur->list);
877 kfree_rcu(cur, rcu);
881 static void __reset_counters(struct pm_nl_pernet *pernet)
883 pernet->add_addr_signal_max = 0;
884 pernet->add_addr_accept_max = 0;
885 pernet->local_addr_max = 0;
886 pernet->addrs = 0;
889 static int mptcp_nl_cmd_flush_addrs(struct sk_buff *skb, struct genl_info *info)
891 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
892 LIST_HEAD(free_list);
894 spin_lock_bh(&pernet->lock);
895 list_splice_init(&pernet->local_addr_list, &free_list);
896 __reset_counters(pernet);
897 spin_unlock_bh(&pernet->lock);
898 __flush_addrs(sock_net(skb->sk), &free_list);
899 return 0;
902 static int mptcp_nl_fill_addr(struct sk_buff *skb,
903 struct mptcp_pm_addr_entry *entry)
905 struct mptcp_addr_info *addr = &entry->addr;
906 struct nlattr *attr;
908 attr = nla_nest_start(skb, MPTCP_PM_ATTR_ADDR);
909 if (!attr)
910 return -EMSGSIZE;
912 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_FAMILY, addr->family))
913 goto nla_put_failure;
914 if (nla_put_u8(skb, MPTCP_PM_ADDR_ATTR_ID, addr->id))
915 goto nla_put_failure;
916 if (nla_put_u32(skb, MPTCP_PM_ADDR_ATTR_FLAGS, entry->addr.flags))
917 goto nla_put_failure;
918 if (entry->addr.ifindex &&
919 nla_put_s32(skb, MPTCP_PM_ADDR_ATTR_IF_IDX, entry->addr.ifindex))
920 goto nla_put_failure;
922 if (addr->family == AF_INET &&
923 nla_put_in_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR4,
924 addr->addr.s_addr))
925 goto nla_put_failure;
926 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
927 else if (addr->family == AF_INET6 &&
928 nla_put_in6_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR6, &addr->addr6))
929 goto nla_put_failure;
930 #endif
931 nla_nest_end(skb, attr);
932 return 0;
934 nla_put_failure:
935 nla_nest_cancel(skb, attr);
936 return -EMSGSIZE;
939 static int mptcp_nl_cmd_get_addr(struct sk_buff *skb, struct genl_info *info)
941 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
942 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
943 struct mptcp_pm_addr_entry addr, *entry;
944 struct sk_buff *msg;
945 void *reply;
946 int ret;
948 ret = mptcp_pm_parse_addr(attr, info, false, &addr);
949 if (ret < 0)
950 return ret;
952 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
953 if (!msg)
954 return -ENOMEM;
956 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
957 info->genlhdr->cmd);
958 if (!reply) {
959 GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
960 ret = -EMSGSIZE;
961 goto fail;
964 spin_lock_bh(&pernet->lock);
965 entry = __lookup_addr_by_id(pernet, addr.addr.id);
966 if (!entry) {
967 GENL_SET_ERR_MSG(info, "address not found");
968 ret = -EINVAL;
969 goto unlock_fail;
972 ret = mptcp_nl_fill_addr(msg, entry);
973 if (ret)
974 goto unlock_fail;
976 genlmsg_end(msg, reply);
977 ret = genlmsg_reply(msg, info);
978 spin_unlock_bh(&pernet->lock);
979 return ret;
981 unlock_fail:
982 spin_unlock_bh(&pernet->lock);
984 fail:
985 nlmsg_free(msg);
986 return ret;
989 static int mptcp_nl_cmd_dump_addrs(struct sk_buff *msg,
990 struct netlink_callback *cb)
992 struct net *net = sock_net(msg->sk);
993 struct mptcp_pm_addr_entry *entry;
994 struct pm_nl_pernet *pernet;
995 int id = cb->args[0];
996 void *hdr;
998 pernet = net_generic(net, pm_nl_pernet_id);
1000 spin_lock_bh(&pernet->lock);
1001 list_for_each_entry(entry, &pernet->local_addr_list, list) {
1002 if (entry->addr.id <= id)
1003 continue;
1005 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid,
1006 cb->nlh->nlmsg_seq, &mptcp_genl_family,
1007 NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR);
1008 if (!hdr)
1009 break;
1011 if (mptcp_nl_fill_addr(msg, entry) < 0) {
1012 genlmsg_cancel(msg, hdr);
1013 break;
1016 id = entry->addr.id;
1017 genlmsg_end(msg, hdr);
1019 spin_unlock_bh(&pernet->lock);
1021 cb->args[0] = id;
1022 return msg->len;
1025 static int parse_limit(struct genl_info *info, int id, unsigned int *limit)
1027 struct nlattr *attr = info->attrs[id];
1029 if (!attr)
1030 return 0;
1032 *limit = nla_get_u32(attr);
1033 if (*limit > MPTCP_PM_ADDR_MAX) {
1034 GENL_SET_ERR_MSG(info, "limit greater than maximum");
1035 return -EINVAL;
1037 return 0;
1040 static int
1041 mptcp_nl_cmd_set_limits(struct sk_buff *skb, struct genl_info *info)
1043 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1044 unsigned int rcv_addrs, subflows;
1045 int ret;
1047 spin_lock_bh(&pernet->lock);
1048 rcv_addrs = pernet->add_addr_accept_max;
1049 ret = parse_limit(info, MPTCP_PM_ATTR_RCV_ADD_ADDRS, &rcv_addrs);
1050 if (ret)
1051 goto unlock;
1053 subflows = pernet->subflows_max;
1054 ret = parse_limit(info, MPTCP_PM_ATTR_SUBFLOWS, &subflows);
1055 if (ret)
1056 goto unlock;
1058 WRITE_ONCE(pernet->add_addr_accept_max, rcv_addrs);
1059 WRITE_ONCE(pernet->subflows_max, subflows);
1061 unlock:
1062 spin_unlock_bh(&pernet->lock);
1063 return ret;
1066 static int
1067 mptcp_nl_cmd_get_limits(struct sk_buff *skb, struct genl_info *info)
1069 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1070 struct sk_buff *msg;
1071 void *reply;
1073 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1074 if (!msg)
1075 return -ENOMEM;
1077 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1078 MPTCP_PM_CMD_GET_LIMITS);
1079 if (!reply)
1080 goto fail;
1082 if (nla_put_u32(msg, MPTCP_PM_ATTR_RCV_ADD_ADDRS,
1083 READ_ONCE(pernet->add_addr_accept_max)))
1084 goto fail;
1086 if (nla_put_u32(msg, MPTCP_PM_ATTR_SUBFLOWS,
1087 READ_ONCE(pernet->subflows_max)))
1088 goto fail;
1090 genlmsg_end(msg, reply);
1091 return genlmsg_reply(msg, info);
1093 fail:
1094 GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1095 nlmsg_free(msg);
1096 return -EMSGSIZE;
1099 static const struct genl_small_ops mptcp_pm_ops[] = {
1101 .cmd = MPTCP_PM_CMD_ADD_ADDR,
1102 .doit = mptcp_nl_cmd_add_addr,
1103 .flags = GENL_ADMIN_PERM,
1106 .cmd = MPTCP_PM_CMD_DEL_ADDR,
1107 .doit = mptcp_nl_cmd_del_addr,
1108 .flags = GENL_ADMIN_PERM,
1111 .cmd = MPTCP_PM_CMD_FLUSH_ADDRS,
1112 .doit = mptcp_nl_cmd_flush_addrs,
1113 .flags = GENL_ADMIN_PERM,
1116 .cmd = MPTCP_PM_CMD_GET_ADDR,
1117 .doit = mptcp_nl_cmd_get_addr,
1118 .dumpit = mptcp_nl_cmd_dump_addrs,
1121 .cmd = MPTCP_PM_CMD_SET_LIMITS,
1122 .doit = mptcp_nl_cmd_set_limits,
1123 .flags = GENL_ADMIN_PERM,
1126 .cmd = MPTCP_PM_CMD_GET_LIMITS,
1127 .doit = mptcp_nl_cmd_get_limits,
1131 static struct genl_family mptcp_genl_family __ro_after_init = {
1132 .name = MPTCP_PM_NAME,
1133 .version = MPTCP_PM_VER,
1134 .maxattr = MPTCP_PM_ATTR_MAX,
1135 .policy = mptcp_pm_policy,
1136 .netnsok = true,
1137 .module = THIS_MODULE,
1138 .small_ops = mptcp_pm_ops,
1139 .n_small_ops = ARRAY_SIZE(mptcp_pm_ops),
1140 .mcgrps = mptcp_pm_mcgrps,
1141 .n_mcgrps = ARRAY_SIZE(mptcp_pm_mcgrps),
1144 static int __net_init pm_nl_init_net(struct net *net)
1146 struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id);
1148 INIT_LIST_HEAD_RCU(&pernet->local_addr_list);
1149 __reset_counters(pernet);
1150 pernet->next_id = 1;
1151 spin_lock_init(&pernet->lock);
1152 return 0;
1155 static void __net_exit pm_nl_exit_net(struct list_head *net_list)
1157 struct net *net;
1159 list_for_each_entry(net, net_list, exit_list) {
1160 struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id);
1162 /* net is removed from namespace list, can't race with
1163 * other modifiers
1165 __flush_addrs(net, &pernet->local_addr_list);
1169 static struct pernet_operations mptcp_pm_pernet_ops = {
1170 .init = pm_nl_init_net,
1171 .exit_batch = pm_nl_exit_net,
1172 .id = &pm_nl_pernet_id,
1173 .size = sizeof(struct pm_nl_pernet),
1176 void __init mptcp_pm_nl_init(void)
1178 if (register_pernet_subsys(&mptcp_pm_pernet_ops) < 0)
1179 panic("Failed to register MPTCP PM pernet subsystem.\n");
1181 if (genl_register_family(&mptcp_genl_family))
1182 panic("Failed to register MPTCP PM netlink family\n");