1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (c) 2020, Red Hat, Inc.
7 #define pr_fmt(fmt) "MPTCP: " fmt
9 #include <linux/inet.h>
10 #include <linux/kernel.h>
12 #include <net/netns/generic.h>
13 #include <net/mptcp.h>
14 #include <net/genetlink.h>
15 #include <uapi/linux/mptcp.h>
20 /* forward declaration */
21 static struct genl_family mptcp_genl_family
;
23 static int pm_nl_pernet_id
;
25 struct mptcp_pm_addr_entry
{
26 struct list_head list
;
27 struct mptcp_addr_info addr
;
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
;
40 /* protects pernet updates */
42 struct list_head local_addr_list
;
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
;
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
)
62 if (a
->family
== AF_INET
)
63 addr_equals
= a
->addr
.s_addr
== b
->addr
.s_addr
;
64 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
66 addr_equals
= !ipv6_addr_cmp(&a
->addr6
, &b
->addr6
);
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
)
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
;
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
;
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))
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
;
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
))
143 /* avoid any address already in use by subflows and
146 if (entry
->addr
.family
== ((struct sock
*)msk
)->sk_family
&&
147 !lookup_subflow_by_saddr(&msk
->conn_list
, &entry
->addr
)) {
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
;
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
))
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))
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
);
213 if (inet_sk_state_load(sk
) == TCP_CLOSE
)
219 if (mptcp_pm_should_add_signal(msk
)) {
220 sk_reset_timer(sk
, timer
, jiffies
+ TCP_RTO_MAX
/ 8);
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
);
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
);
253 entry
->retrans_times
= ADD_ADDR_RETRANS_MAX
;
254 spin_unlock_bh(&msk
->pm
.lock
);
257 sk_stop_timer_sync(sk
, &entry
->add_timer
);
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
))
272 add_entry
= kmalloc(sizeof(*add_entry
), GFP_ATOMIC
);
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
));
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
);
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
);
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
);
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
);
347 msk
->pm
.local_addr_used
++;
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
);
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
++;
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
;
393 remote
.port
= sk
->sk_dport
;
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
))
415 __mptcp_flush_join_list(msk
);
416 subflow
= list_first_entry_or_null(&msk
->conn_list
, typeof(*subflow
), node
);
418 struct sock
*ssk
= mptcp_subflow_tcp_sock(subflow
);
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");
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
);
451 if (list_empty(&msk
->conn_list
))
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
)
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
--;
468 WRITE_ONCE(msk
->pm
.accept_addr
, true);
470 __MPTCP_INC_STATS(sock_net(sk
), MPTCP_MIB_RMADDR
);
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
);
486 if (list_empty(&msk
->conn_list
))
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
)
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
--;
504 __MPTCP_INC_STATS(sock_net(sk
), MPTCP_MIB_RMSUBFLOW
);
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
;
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)
529 if (pernet
->addrs
>= MPTCP_PM_ADDR_MAX
)
532 /* do not insert duplicate address, differentiate on port only
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
)))
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
++;
549 list_add_tail_rcu(&entry
->list
, &pernet
->local_addr_list
);
550 ret
= entry
->addr
.id
;
553 spin_unlock_bh(&pernet
->lock
);
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
;
565 if (WARN_ON_ONCE(!msk
))
568 /* The 0 ID mapping is defined by the first subflow, copied into the msk
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))
576 if (address_zero(&skc_local
))
579 pernet
= net_generic(sock_net((struct sock
*)msk
), pm_nl_pernet_id
);
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
;
592 /* address not found, add to local list */
593 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
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
);
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
;
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
;
657 return MPTCP_PM_ADDR_ATTR_ADDR4
;
660 static int mptcp_pm_parse_addr(struct nlattr
*attr
, struct genl_info
*info
,
662 struct mptcp_pm_addr_entry
*entry
)
664 struct nlattr
*tb
[MPTCP_PM_ADDR_ATTR_MAX
+ 1];
668 GENL_SET_ERR_MSG(info
, "missing address info");
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
);
678 memset(entry
, 0, sizeof(*entry
));
679 if (!tb
[MPTCP_PM_ADDR_ATTR_FAMILY
]) {
683 NL_SET_ERR_MSG_ATTR(info
->extack
, attr
,
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
694 NL_SET_ERR_MSG_ATTR(info
->extack
, attr
,
695 "unknown address family");
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");
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
]);
710 entry
->addr
.addr
.s_addr
= nla_get_in_addr(tb
[addr_addr
]);
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
]);
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
;
740 ret
= mptcp_pm_parse_addr(attr
, info
, true, &addr
);
744 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
746 GENL_SET_ERR_MSG(info
, "can't allocate addr");
751 ret
= mptcp_pm_nl_append_new_local_addr(pernet
, entry
);
753 GENL_SET_ERR_MSG(info
, "too many addresses or duplicate one");
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
)
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
);
780 list_del(&entry
->list
);
788 static bool mptcp_pm_remove_anno_addr(struct mptcp_sock
*msk
,
789 struct mptcp_addr_info
*addr
,
794 ret
= remove_anno_list_by_saddr(msk
, addr
);
796 spin_lock_bh(&msk
->pm
.lock
);
797 mptcp_pm_remove_addr(msk
, addr
->id
);
798 spin_unlock_bh(&msk
->pm
.lock
);
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
;
815 if (list_empty(&msk
->conn_list
)) {
816 mptcp_pm_remove_anno_addr(msk
, addr
, false);
821 remove_subflow
= lookup_subflow_by_saddr(&msk
->conn_list
, addr
);
822 mptcp_pm_remove_anno_addr(msk
, addr
, remove_subflow
);
824 mptcp_pm_remove_subflow(msk
, addr
->id
);
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
;
842 ret
= mptcp_pm_parse_addr(attr
, info
, false, &addr
);
846 spin_lock_bh(&pernet
->lock
);
847 entry
= __lookup_addr_by_id(pernet
, addr
.addr
.id
);
849 GENL_SET_ERR_MSG(info
, "address not found");
850 spin_unlock_bh(&pernet
->lock
);
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
--;
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
);
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
);
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;
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
);
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
;
908 attr
= nla_nest_start(skb
, MPTCP_PM_ATTR_ADDR
);
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
,
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
;
931 nla_nest_end(skb
, attr
);
935 nla_nest_cancel(skb
, attr
);
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
;
948 ret
= mptcp_pm_parse_addr(attr
, info
, false, &addr
);
952 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
956 reply
= genlmsg_put_reply(msg
, info
, &mptcp_genl_family
, 0,
959 GENL_SET_ERR_MSG(info
, "not enough space in Netlink message");
964 spin_lock_bh(&pernet
->lock
);
965 entry
= __lookup_addr_by_id(pernet
, addr
.addr
.id
);
967 GENL_SET_ERR_MSG(info
, "address not found");
972 ret
= mptcp_nl_fill_addr(msg
, entry
);
976 genlmsg_end(msg
, reply
);
977 ret
= genlmsg_reply(msg
, info
);
978 spin_unlock_bh(&pernet
->lock
);
982 spin_unlock_bh(&pernet
->lock
);
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];
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
)
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
);
1011 if (mptcp_nl_fill_addr(msg
, entry
) < 0) {
1012 genlmsg_cancel(msg
, hdr
);
1016 id
= entry
->addr
.id
;
1017 genlmsg_end(msg
, hdr
);
1019 spin_unlock_bh(&pernet
->lock
);
1025 static int parse_limit(struct genl_info
*info
, int id
, unsigned int *limit
)
1027 struct nlattr
*attr
= info
->attrs
[id
];
1032 *limit
= nla_get_u32(attr
);
1033 if (*limit
> MPTCP_PM_ADDR_MAX
) {
1034 GENL_SET_ERR_MSG(info
, "limit greater than maximum");
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
;
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
);
1053 subflows
= pernet
->subflows_max
;
1054 ret
= parse_limit(info
, MPTCP_PM_ATTR_SUBFLOWS
, &subflows
);
1058 WRITE_ONCE(pernet
->add_addr_accept_max
, rcv_addrs
);
1059 WRITE_ONCE(pernet
->subflows_max
, subflows
);
1062 spin_unlock_bh(&pernet
->lock
);
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
;
1073 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1077 reply
= genlmsg_put_reply(msg
, info
, &mptcp_genl_family
, 0,
1078 MPTCP_PM_CMD_GET_LIMITS
);
1082 if (nla_put_u32(msg
, MPTCP_PM_ATTR_RCV_ADD_ADDRS
,
1083 READ_ONCE(pernet
->add_addr_accept_max
)))
1086 if (nla_put_u32(msg
, MPTCP_PM_ATTR_SUBFLOWS
,
1087 READ_ONCE(pernet
->subflows_max
)))
1090 genlmsg_end(msg
, reply
);
1091 return genlmsg_reply(msg
, info
);
1094 GENL_SET_ERR_MSG(info
, "not enough space in Netlink message");
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
,
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
);
1155 static void __net_exit
pm_nl_exit_net(struct list_head
*net_list
)
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
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");