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>
19 /* forward declaration */
20 static struct genl_family mptcp_genl_family
;
22 static int pm_nl_pernet_id
;
24 struct mptcp_pm_addr_entry
{
25 struct list_head list
;
28 struct mptcp_addr_info addr
;
33 /* protects pernet updates */
35 struct list_head local_addr_list
;
37 unsigned int add_addr_signal_max
;
38 unsigned int add_addr_accept_max
;
39 unsigned int local_addr_max
;
40 unsigned int subflows_max
;
44 #define MPTCP_PM_ADDR_MAX 8
46 static bool addresses_equal(const struct mptcp_addr_info
*a
,
47 struct mptcp_addr_info
*b
, bool use_port
)
49 bool addr_equals
= false;
51 if (a
->family
!= b
->family
)
54 if (a
->family
== AF_INET
)
55 addr_equals
= a
->addr
.s_addr
== b
->addr
.s_addr
;
56 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
58 addr_equals
= !ipv6_addr_cmp(&a
->addr6
, &b
->addr6
);
66 return a
->port
== b
->port
;
69 static void local_address(const struct sock_common
*skc
,
70 struct mptcp_addr_info
*addr
)
73 addr
->family
= skc
->skc_family
;
74 if (addr
->family
== AF_INET
)
75 addr
->addr
.s_addr
= skc
->skc_rcv_saddr
;
76 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
77 else if (addr
->family
== AF_INET6
)
78 addr
->addr6
= skc
->skc_v6_rcv_saddr
;
82 static void remote_address(const struct sock_common
*skc
,
83 struct mptcp_addr_info
*addr
)
85 addr
->family
= skc
->skc_family
;
86 addr
->port
= skc
->skc_dport
;
87 if (addr
->family
== AF_INET
)
88 addr
->addr
.s_addr
= skc
->skc_daddr
;
89 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
90 else if (addr
->family
== AF_INET6
)
91 addr
->addr6
= skc
->skc_v6_daddr
;
95 static bool lookup_subflow_by_saddr(const struct list_head
*list
,
96 struct mptcp_addr_info
*saddr
)
98 struct mptcp_subflow_context
*subflow
;
99 struct mptcp_addr_info cur
;
100 struct sock_common
*skc
;
102 list_for_each_entry(subflow
, list
, node
) {
103 skc
= (struct sock_common
*)mptcp_subflow_tcp_sock(subflow
);
105 local_address(skc
, &cur
);
106 if (addresses_equal(&cur
, saddr
, false))
113 static struct mptcp_pm_addr_entry
*
114 select_local_address(const struct pm_nl_pernet
*pernet
,
115 struct mptcp_sock
*msk
)
117 struct mptcp_pm_addr_entry
*entry
, *ret
= NULL
;
120 spin_lock_bh(&msk
->join_list_lock
);
121 list_for_each_entry_rcu(entry
, &pernet
->local_addr_list
, list
) {
122 if (!(entry
->flags
& MPTCP_PM_ADDR_FLAG_SUBFLOW
))
125 /* avoid any address already in use by subflows and
128 if (entry
->addr
.family
== ((struct sock
*)msk
)->sk_family
&&
129 !lookup_subflow_by_saddr(&msk
->conn_list
, &entry
->addr
) &&
130 !lookup_subflow_by_saddr(&msk
->join_list
, &entry
->addr
)) {
135 spin_unlock_bh(&msk
->join_list_lock
);
140 static struct mptcp_pm_addr_entry
*
141 select_signal_address(struct pm_nl_pernet
*pernet
, unsigned int pos
)
143 struct mptcp_pm_addr_entry
*entry
, *ret
= NULL
;
147 /* do not keep any additional per socket state, just signal
148 * the address list in order.
149 * Note: removal from the local address list during the msk life-cycle
150 * can lead to additional addresses not being announced.
152 list_for_each_entry_rcu(entry
, &pernet
->local_addr_list
, list
) {
153 if (!(entry
->flags
& MPTCP_PM_ADDR_FLAG_SIGNAL
))
164 static void check_work_pending(struct mptcp_sock
*msk
)
166 if (msk
->pm
.add_addr_signaled
== msk
->pm
.add_addr_signal_max
&&
167 (msk
->pm
.local_addr_used
== msk
->pm
.local_addr_max
||
168 msk
->pm
.subflows
== msk
->pm
.subflows_max
))
169 WRITE_ONCE(msk
->pm
.work_pending
, false);
172 static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock
*msk
)
174 struct sock
*sk
= (struct sock
*)msk
;
175 struct mptcp_pm_addr_entry
*local
;
176 struct mptcp_addr_info remote
;
177 struct pm_nl_pernet
*pernet
;
179 pernet
= net_generic(sock_net((struct sock
*)msk
), pm_nl_pernet_id
);
181 pr_debug("local %d:%d signal %d:%d subflows %d:%d\n",
182 msk
->pm
.local_addr_used
, msk
->pm
.local_addr_max
,
183 msk
->pm
.add_addr_signaled
, msk
->pm
.add_addr_signal_max
,
184 msk
->pm
.subflows
, msk
->pm
.subflows_max
);
186 /* check first for announce */
187 if (msk
->pm
.add_addr_signaled
< msk
->pm
.add_addr_signal_max
) {
188 local
= select_signal_address(pernet
,
189 msk
->pm
.add_addr_signaled
);
192 msk
->pm
.add_addr_signaled
++;
193 mptcp_pm_announce_addr(msk
, &local
->addr
);
195 /* pick failed, avoid fourther attempts later */
196 msk
->pm
.local_addr_used
= msk
->pm
.add_addr_signal_max
;
199 check_work_pending(msk
);
202 /* check if should create a new subflow */
203 if (msk
->pm
.local_addr_used
< msk
->pm
.local_addr_max
&&
204 msk
->pm
.subflows
< msk
->pm
.subflows_max
) {
205 remote_address((struct sock_common
*)sk
, &remote
);
207 local
= select_local_address(pernet
, msk
);
209 msk
->pm
.local_addr_used
++;
211 check_work_pending(msk
);
212 spin_unlock_bh(&msk
->pm
.lock
);
213 __mptcp_subflow_connect(sk
, local
->ifindex
,
214 &local
->addr
, &remote
);
215 spin_lock_bh(&msk
->pm
.lock
);
219 /* lookup failed, avoid fourther attempts later */
220 msk
->pm
.local_addr_used
= msk
->pm
.local_addr_max
;
221 check_work_pending(msk
);
225 void mptcp_pm_nl_fully_established(struct mptcp_sock
*msk
)
227 mptcp_pm_create_subflow_or_signal_addr(msk
);
230 void mptcp_pm_nl_subflow_established(struct mptcp_sock
*msk
)
232 mptcp_pm_create_subflow_or_signal_addr(msk
);
235 void mptcp_pm_nl_add_addr_received(struct mptcp_sock
*msk
)
237 struct sock
*sk
= (struct sock
*)msk
;
238 struct mptcp_addr_info remote
;
239 struct mptcp_addr_info local
;
241 pr_debug("accepted %d:%d remote family %d",
242 msk
->pm
.add_addr_accepted
, msk
->pm
.add_addr_accept_max
,
243 msk
->pm
.remote
.family
);
244 msk
->pm
.add_addr_accepted
++;
246 if (msk
->pm
.add_addr_accepted
>= msk
->pm
.add_addr_accept_max
||
247 msk
->pm
.subflows
>= msk
->pm
.subflows_max
)
248 WRITE_ONCE(msk
->pm
.accept_addr
, false);
250 /* connect to the specified remote address, using whatever
251 * local address the routing configuration will pick.
253 remote
= msk
->pm
.remote
;
255 remote
.port
= sk
->sk_dport
;
256 memset(&local
, 0, sizeof(local
));
257 local
.family
= remote
.family
;
259 spin_unlock_bh(&msk
->pm
.lock
);
260 __mptcp_subflow_connect((struct sock
*)msk
, 0, &local
, &remote
);
261 spin_lock_bh(&msk
->pm
.lock
);
264 static bool address_use_port(struct mptcp_pm_addr_entry
*entry
)
266 return (entry
->flags
&
267 (MPTCP_PM_ADDR_FLAG_SIGNAL
| MPTCP_PM_ADDR_FLAG_SUBFLOW
)) ==
268 MPTCP_PM_ADDR_FLAG_SIGNAL
;
271 static int mptcp_pm_nl_append_new_local_addr(struct pm_nl_pernet
*pernet
,
272 struct mptcp_pm_addr_entry
*entry
)
274 struct mptcp_pm_addr_entry
*cur
;
277 spin_lock_bh(&pernet
->lock
);
278 /* to keep the code simple, don't do IDR-like allocation for address ID,
279 * just bail when we exceed limits
281 if (pernet
->next_id
> 255)
283 if (pernet
->addrs
>= MPTCP_PM_ADDR_MAX
)
286 /* do not insert duplicate address, differentiate on port only
289 list_for_each_entry(cur
, &pernet
->local_addr_list
, list
) {
290 if (addresses_equal(&cur
->addr
, &entry
->addr
,
291 address_use_port(entry
) &&
292 address_use_port(cur
)))
296 if (entry
->flags
& MPTCP_PM_ADDR_FLAG_SIGNAL
)
297 pernet
->add_addr_signal_max
++;
298 if (entry
->flags
& MPTCP_PM_ADDR_FLAG_SUBFLOW
)
299 pernet
->local_addr_max
++;
301 entry
->addr
.id
= pernet
->next_id
++;
303 list_add_tail_rcu(&entry
->list
, &pernet
->local_addr_list
);
304 ret
= entry
->addr
.id
;
307 spin_unlock_bh(&pernet
->lock
);
311 int mptcp_pm_nl_get_local_id(struct mptcp_sock
*msk
, struct sock_common
*skc
)
313 struct mptcp_pm_addr_entry
*entry
;
314 struct mptcp_addr_info skc_local
;
315 struct mptcp_addr_info msk_local
;
316 struct pm_nl_pernet
*pernet
;
319 if (WARN_ON_ONCE(!msk
))
322 /* The 0 ID mapping is defined by the first subflow, copied into the msk
325 local_address((struct sock_common
*)msk
, &msk_local
);
326 local_address((struct sock_common
*)msk
, &skc_local
);
327 if (addresses_equal(&msk_local
, &skc_local
, false))
330 pernet
= net_generic(sock_net((struct sock
*)msk
), pm_nl_pernet_id
);
333 list_for_each_entry_rcu(entry
, &pernet
->local_addr_list
, list
) {
334 if (addresses_equal(&entry
->addr
, &skc_local
, false)) {
335 ret
= entry
->addr
.id
;
343 /* address not found, add to local list */
344 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
349 entry
->addr
= skc_local
;
350 ret
= mptcp_pm_nl_append_new_local_addr(pernet
, entry
);
357 void mptcp_pm_nl_data_init(struct mptcp_sock
*msk
)
359 struct mptcp_pm_data
*pm
= &msk
->pm
;
360 struct pm_nl_pernet
*pernet
;
363 pernet
= net_generic(sock_net((struct sock
*)msk
), pm_nl_pernet_id
);
365 pm
->add_addr_signal_max
= READ_ONCE(pernet
->add_addr_signal_max
);
366 pm
->add_addr_accept_max
= READ_ONCE(pernet
->add_addr_accept_max
);
367 pm
->local_addr_max
= READ_ONCE(pernet
->local_addr_max
);
368 pm
->subflows_max
= READ_ONCE(pernet
->subflows_max
);
369 subflows
= !!pm
->subflows_max
;
370 WRITE_ONCE(pm
->work_pending
, (!!pm
->local_addr_max
&& subflows
) ||
371 !!pm
->add_addr_signal_max
);
372 WRITE_ONCE(pm
->accept_addr
, !!pm
->add_addr_accept_max
&& subflows
);
373 WRITE_ONCE(pm
->accept_subflow
, subflows
);
376 #define MPTCP_PM_CMD_GRP_OFFSET 0
378 static const struct genl_multicast_group mptcp_pm_mcgrps
[] = {
379 [MPTCP_PM_CMD_GRP_OFFSET
] = { .name
= MPTCP_PM_CMD_GRP_NAME
, },
382 static const struct nla_policy
383 mptcp_pm_addr_policy
[MPTCP_PM_ADDR_ATTR_MAX
+ 1] = {
384 [MPTCP_PM_ADDR_ATTR_FAMILY
] = { .type
= NLA_U16
, },
385 [MPTCP_PM_ADDR_ATTR_ID
] = { .type
= NLA_U8
, },
386 [MPTCP_PM_ADDR_ATTR_ADDR4
] = { .type
= NLA_U32
, },
387 [MPTCP_PM_ADDR_ATTR_ADDR6
] = { .type
= NLA_EXACT_LEN
,
388 .len
= sizeof(struct in6_addr
), },
389 [MPTCP_PM_ADDR_ATTR_PORT
] = { .type
= NLA_U16
},
390 [MPTCP_PM_ADDR_ATTR_FLAGS
] = { .type
= NLA_U32
},
391 [MPTCP_PM_ADDR_ATTR_IF_IDX
] = { .type
= NLA_S32
},
394 static const struct nla_policy mptcp_pm_policy
[MPTCP_PM_ATTR_MAX
+ 1] = {
395 [MPTCP_PM_ATTR_ADDR
] =
396 NLA_POLICY_NESTED(mptcp_pm_addr_policy
),
397 [MPTCP_PM_ATTR_RCV_ADD_ADDRS
] = { .type
= NLA_U32
, },
398 [MPTCP_PM_ATTR_SUBFLOWS
] = { .type
= NLA_U32
, },
401 static int mptcp_pm_family_to_addr(int family
)
403 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
404 if (family
== AF_INET6
)
405 return MPTCP_PM_ADDR_ATTR_ADDR6
;
407 return MPTCP_PM_ADDR_ATTR_ADDR4
;
410 static int mptcp_pm_parse_addr(struct nlattr
*attr
, struct genl_info
*info
,
412 struct mptcp_pm_addr_entry
*entry
)
414 struct nlattr
*tb
[MPTCP_PM_ADDR_ATTR_MAX
+ 1];
418 GENL_SET_ERR_MSG(info
, "missing address info");
422 /* no validation needed - was already done via nested policy */
423 err
= nla_parse_nested_deprecated(tb
, MPTCP_PM_ADDR_ATTR_MAX
, attr
,
424 mptcp_pm_addr_policy
, info
->extack
);
428 memset(entry
, 0, sizeof(*entry
));
429 if (!tb
[MPTCP_PM_ADDR_ATTR_FAMILY
]) {
433 NL_SET_ERR_MSG_ATTR(info
->extack
, attr
,
438 entry
->addr
.family
= nla_get_u16(tb
[MPTCP_PM_ADDR_ATTR_FAMILY
]);
439 if (entry
->addr
.family
!= AF_INET
440 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
441 && entry
->addr
.family
!= AF_INET6
444 NL_SET_ERR_MSG_ATTR(info
->extack
, attr
,
445 "unknown address family");
448 addr_addr
= mptcp_pm_family_to_addr(entry
->addr
.family
);
449 if (!tb
[addr_addr
]) {
450 NL_SET_ERR_MSG_ATTR(info
->extack
, attr
,
451 "missing address data");
455 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
456 if (entry
->addr
.family
== AF_INET6
)
457 entry
->addr
.addr6
= nla_get_in6_addr(tb
[addr_addr
]);
460 entry
->addr
.addr
.s_addr
= nla_get_in_addr(tb
[addr_addr
]);
463 if (tb
[MPTCP_PM_ADDR_ATTR_IF_IDX
])
464 entry
->ifindex
= nla_get_s32(tb
[MPTCP_PM_ADDR_ATTR_IF_IDX
]);
466 if (tb
[MPTCP_PM_ADDR_ATTR_ID
])
467 entry
->addr
.id
= nla_get_u8(tb
[MPTCP_PM_ADDR_ATTR_ID
]);
469 if (tb
[MPTCP_PM_ADDR_ATTR_FLAGS
])
470 entry
->flags
= nla_get_u32(tb
[MPTCP_PM_ADDR_ATTR_FLAGS
]);
475 static struct pm_nl_pernet
*genl_info_pm_nl(struct genl_info
*info
)
477 return net_generic(genl_info_net(info
), pm_nl_pernet_id
);
480 static int mptcp_nl_cmd_add_addr(struct sk_buff
*skb
, struct genl_info
*info
)
482 struct nlattr
*attr
= info
->attrs
[MPTCP_PM_ATTR_ADDR
];
483 struct pm_nl_pernet
*pernet
= genl_info_pm_nl(info
);
484 struct mptcp_pm_addr_entry addr
, *entry
;
487 ret
= mptcp_pm_parse_addr(attr
, info
, true, &addr
);
491 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
493 GENL_SET_ERR_MSG(info
, "can't allocate addr");
498 ret
= mptcp_pm_nl_append_new_local_addr(pernet
, entry
);
500 GENL_SET_ERR_MSG(info
, "too many addresses or duplicate one");
508 static struct mptcp_pm_addr_entry
*
509 __lookup_addr_by_id(struct pm_nl_pernet
*pernet
, unsigned int id
)
511 struct mptcp_pm_addr_entry
*entry
;
513 list_for_each_entry(entry
, &pernet
->local_addr_list
, list
) {
514 if (entry
->addr
.id
== id
)
520 static int mptcp_nl_cmd_del_addr(struct sk_buff
*skb
, struct genl_info
*info
)
522 struct nlattr
*attr
= info
->attrs
[MPTCP_PM_ATTR_ADDR
];
523 struct pm_nl_pernet
*pernet
= genl_info_pm_nl(info
);
524 struct mptcp_pm_addr_entry addr
, *entry
;
527 ret
= mptcp_pm_parse_addr(attr
, info
, false, &addr
);
531 spin_lock_bh(&pernet
->lock
);
532 entry
= __lookup_addr_by_id(pernet
, addr
.addr
.id
);
534 GENL_SET_ERR_MSG(info
, "address not found");
538 if (entry
->flags
& MPTCP_PM_ADDR_FLAG_SIGNAL
)
539 pernet
->add_addr_signal_max
--;
540 if (entry
->flags
& MPTCP_PM_ADDR_FLAG_SUBFLOW
)
541 pernet
->local_addr_max
--;
544 list_del_rcu(&entry
->list
);
545 kfree_rcu(entry
, rcu
);
547 spin_unlock_bh(&pernet
->lock
);
551 static void __flush_addrs(struct pm_nl_pernet
*pernet
)
553 while (!list_empty(&pernet
->local_addr_list
)) {
554 struct mptcp_pm_addr_entry
*cur
;
556 cur
= list_entry(pernet
->local_addr_list
.next
,
557 struct mptcp_pm_addr_entry
, list
);
558 list_del_rcu(&cur
->list
);
563 static void __reset_counters(struct pm_nl_pernet
*pernet
)
565 pernet
->add_addr_signal_max
= 0;
566 pernet
->add_addr_accept_max
= 0;
567 pernet
->local_addr_max
= 0;
571 static int mptcp_nl_cmd_flush_addrs(struct sk_buff
*skb
, struct genl_info
*info
)
573 struct pm_nl_pernet
*pernet
= genl_info_pm_nl(info
);
575 spin_lock_bh(&pernet
->lock
);
576 __flush_addrs(pernet
);
577 __reset_counters(pernet
);
578 spin_unlock_bh(&pernet
->lock
);
582 static int mptcp_nl_fill_addr(struct sk_buff
*skb
,
583 struct mptcp_pm_addr_entry
*entry
)
585 struct mptcp_addr_info
*addr
= &entry
->addr
;
588 attr
= nla_nest_start(skb
, MPTCP_PM_ATTR_ADDR
);
592 if (nla_put_u16(skb
, MPTCP_PM_ADDR_ATTR_FAMILY
, addr
->family
))
593 goto nla_put_failure
;
594 if (nla_put_u8(skb
, MPTCP_PM_ADDR_ATTR_ID
, addr
->id
))
595 goto nla_put_failure
;
596 if (nla_put_u32(skb
, MPTCP_PM_ADDR_ATTR_FLAGS
, entry
->flags
))
597 goto nla_put_failure
;
598 if (entry
->ifindex
&&
599 nla_put_s32(skb
, MPTCP_PM_ADDR_ATTR_IF_IDX
, entry
->ifindex
))
600 goto nla_put_failure
;
602 if (addr
->family
== AF_INET
&&
603 nla_put_in_addr(skb
, MPTCP_PM_ADDR_ATTR_ADDR4
,
605 goto nla_put_failure
;
606 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
607 else if (addr
->family
== AF_INET6
&&
608 nla_put_in6_addr(skb
, MPTCP_PM_ADDR_ATTR_ADDR6
, &addr
->addr6
))
609 goto nla_put_failure
;
611 nla_nest_end(skb
, attr
);
615 nla_nest_cancel(skb
, attr
);
619 static int mptcp_nl_cmd_get_addr(struct sk_buff
*skb
, struct genl_info
*info
)
621 struct nlattr
*attr
= info
->attrs
[MPTCP_PM_ATTR_ADDR
];
622 struct pm_nl_pernet
*pernet
= genl_info_pm_nl(info
);
623 struct mptcp_pm_addr_entry addr
, *entry
;
628 ret
= mptcp_pm_parse_addr(attr
, info
, false, &addr
);
632 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
636 reply
= genlmsg_put_reply(msg
, info
, &mptcp_genl_family
, 0,
639 GENL_SET_ERR_MSG(info
, "not enough space in Netlink message");
644 spin_lock_bh(&pernet
->lock
);
645 entry
= __lookup_addr_by_id(pernet
, addr
.addr
.id
);
647 GENL_SET_ERR_MSG(info
, "address not found");
652 ret
= mptcp_nl_fill_addr(msg
, entry
);
656 genlmsg_end(msg
, reply
);
657 ret
= genlmsg_reply(msg
, info
);
658 spin_unlock_bh(&pernet
->lock
);
662 spin_unlock_bh(&pernet
->lock
);
669 static int mptcp_nl_cmd_dump_addrs(struct sk_buff
*msg
,
670 struct netlink_callback
*cb
)
672 struct net
*net
= sock_net(msg
->sk
);
673 struct mptcp_pm_addr_entry
*entry
;
674 struct pm_nl_pernet
*pernet
;
675 int id
= cb
->args
[0];
678 pernet
= net_generic(net
, pm_nl_pernet_id
);
680 spin_lock_bh(&pernet
->lock
);
681 list_for_each_entry(entry
, &pernet
->local_addr_list
, list
) {
682 if (entry
->addr
.id
<= id
)
685 hdr
= genlmsg_put(msg
, NETLINK_CB(cb
->skb
).portid
,
686 cb
->nlh
->nlmsg_seq
, &mptcp_genl_family
,
687 NLM_F_MULTI
, MPTCP_PM_CMD_GET_ADDR
);
691 if (mptcp_nl_fill_addr(msg
, entry
) < 0) {
692 genlmsg_cancel(msg
, hdr
);
697 genlmsg_end(msg
, hdr
);
699 spin_unlock_bh(&pernet
->lock
);
705 static int parse_limit(struct genl_info
*info
, int id
, unsigned int *limit
)
707 struct nlattr
*attr
= info
->attrs
[id
];
712 *limit
= nla_get_u32(attr
);
713 if (*limit
> MPTCP_PM_ADDR_MAX
) {
714 GENL_SET_ERR_MSG(info
, "limit greater than maximum");
721 mptcp_nl_cmd_set_limits(struct sk_buff
*skb
, struct genl_info
*info
)
723 struct pm_nl_pernet
*pernet
= genl_info_pm_nl(info
);
724 unsigned int rcv_addrs
, subflows
;
727 spin_lock_bh(&pernet
->lock
);
728 rcv_addrs
= pernet
->add_addr_accept_max
;
729 ret
= parse_limit(info
, MPTCP_PM_ATTR_RCV_ADD_ADDRS
, &rcv_addrs
);
733 subflows
= pernet
->subflows_max
;
734 ret
= parse_limit(info
, MPTCP_PM_ATTR_SUBFLOWS
, &subflows
);
738 WRITE_ONCE(pernet
->add_addr_accept_max
, rcv_addrs
);
739 WRITE_ONCE(pernet
->subflows_max
, subflows
);
742 spin_unlock_bh(&pernet
->lock
);
747 mptcp_nl_cmd_get_limits(struct sk_buff
*skb
, struct genl_info
*info
)
749 struct pm_nl_pernet
*pernet
= genl_info_pm_nl(info
);
753 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
757 reply
= genlmsg_put_reply(msg
, info
, &mptcp_genl_family
, 0,
758 MPTCP_PM_CMD_GET_LIMITS
);
762 if (nla_put_u32(msg
, MPTCP_PM_ATTR_RCV_ADD_ADDRS
,
763 READ_ONCE(pernet
->add_addr_accept_max
)))
766 if (nla_put_u32(msg
, MPTCP_PM_ATTR_SUBFLOWS
,
767 READ_ONCE(pernet
->subflows_max
)))
770 genlmsg_end(msg
, reply
);
771 return genlmsg_reply(msg
, info
);
774 GENL_SET_ERR_MSG(info
, "not enough space in Netlink message");
779 static struct genl_ops mptcp_pm_ops
[] = {
781 .cmd
= MPTCP_PM_CMD_ADD_ADDR
,
782 .doit
= mptcp_nl_cmd_add_addr
,
783 .flags
= GENL_ADMIN_PERM
,
786 .cmd
= MPTCP_PM_CMD_DEL_ADDR
,
787 .doit
= mptcp_nl_cmd_del_addr
,
788 .flags
= GENL_ADMIN_PERM
,
791 .cmd
= MPTCP_PM_CMD_FLUSH_ADDRS
,
792 .doit
= mptcp_nl_cmd_flush_addrs
,
793 .flags
= GENL_ADMIN_PERM
,
796 .cmd
= MPTCP_PM_CMD_GET_ADDR
,
797 .doit
= mptcp_nl_cmd_get_addr
,
798 .dumpit
= mptcp_nl_cmd_dump_addrs
,
801 .cmd
= MPTCP_PM_CMD_SET_LIMITS
,
802 .doit
= mptcp_nl_cmd_set_limits
,
803 .flags
= GENL_ADMIN_PERM
,
806 .cmd
= MPTCP_PM_CMD_GET_LIMITS
,
807 .doit
= mptcp_nl_cmd_get_limits
,
811 static struct genl_family mptcp_genl_family __ro_after_init
= {
812 .name
= MPTCP_PM_NAME
,
813 .version
= MPTCP_PM_VER
,
814 .maxattr
= MPTCP_PM_ATTR_MAX
,
815 .policy
= mptcp_pm_policy
,
817 .module
= THIS_MODULE
,
819 .n_ops
= ARRAY_SIZE(mptcp_pm_ops
),
820 .mcgrps
= mptcp_pm_mcgrps
,
821 .n_mcgrps
= ARRAY_SIZE(mptcp_pm_mcgrps
),
824 static int __net_init
pm_nl_init_net(struct net
*net
)
826 struct pm_nl_pernet
*pernet
= net_generic(net
, pm_nl_pernet_id
);
828 INIT_LIST_HEAD_RCU(&pernet
->local_addr_list
);
829 __reset_counters(pernet
);
831 spin_lock_init(&pernet
->lock
);
835 static void __net_exit
pm_nl_exit_net(struct list_head
*net_list
)
839 list_for_each_entry(net
, net_list
, exit_list
) {
840 /* net is removed from namespace list, can't race with
843 __flush_addrs(net_generic(net
, pm_nl_pernet_id
));
847 static struct pernet_operations mptcp_pm_pernet_ops
= {
848 .init
= pm_nl_init_net
,
849 .exit_batch
= pm_nl_exit_net
,
850 .id
= &pm_nl_pernet_id
,
851 .size
= sizeof(struct pm_nl_pernet
),
854 void mptcp_pm_nl_init(void)
856 if (register_pernet_subsys(&mptcp_pm_pernet_ops
) < 0)
857 panic("Failed to register MPTCP PM pernet subsystem.\n");
859 if (genl_register_family(&mptcp_genl_family
))
860 panic("Failed to register MPTCP PM netlink family\n");