xref: /linux/net/mptcp/pm_netlink.c (revision 09e61efd884ca68a768717d60858f138685b161b)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Multipath TCP
3  *
4  * Copyright (c) 2020, Red Hat, Inc.
5  */
6 
7 #define pr_fmt(fmt) "MPTCP: " fmt
8 
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>
16 
17 #include "protocol.h"
18 #include "mib.h"
19 
20 /* forward declaration */
21 static struct genl_family mptcp_genl_family;
22 
23 static int pm_nl_pernet_id;
24 
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;
30 	u8			retrans_times;
31 };
32 
33 struct pm_nl_pernet {
34 	/* protects pernet updates */
35 	spinlock_t		lock;
36 	struct list_head	local_addr_list;
37 	unsigned int		addrs;
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;
43 	unsigned int		next_id;
44 	DECLARE_BITMAP(id_bitmap, MPTCP_PM_MAX_ADDR_ID + 1);
45 };
46 
47 #define MPTCP_PM_ADDR_MAX	8
48 #define ADD_ADDR_RETRANS_MAX	3
49 
50 static struct pm_nl_pernet *pm_nl_get_pernet(const struct net *net)
51 {
52 	return net_generic(net, pm_nl_pernet_id);
53 }
54 
55 static struct pm_nl_pernet *
56 pm_nl_get_pernet_from_msk(const struct mptcp_sock *msk)
57 {
58 	return pm_nl_get_pernet(sock_net((struct sock *)msk));
59 }
60 
61 bool mptcp_addresses_equal(const struct mptcp_addr_info *a,
62 			   const struct mptcp_addr_info *b, bool use_port)
63 {
64 	bool addr_equals = false;
65 
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)
70 		else
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;
78 #endif
79 	}
80 
81 	if (!addr_equals)
82 		return false;
83 	if (!use_port)
84 		return true;
85 
86 	return a->port == b->port;
87 }
88 
89 static void local_address(const struct sock_common *skc,
90 			  struct mptcp_addr_info *addr)
91 {
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;
99 #endif
100 }
101 
102 static void remote_address(const struct sock_common *skc,
103 			   struct mptcp_addr_info *addr)
104 {
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;
112 #endif
113 }
114 
115 static bool lookup_subflow_by_saddr(const struct list_head *list,
116 				    const struct mptcp_addr_info *saddr)
117 {
118 	struct mptcp_subflow_context *subflow;
119 	struct mptcp_addr_info cur;
120 	struct sock_common *skc;
121 
122 	list_for_each_entry(subflow, list, node) {
123 		skc = (struct sock_common *)mptcp_subflow_tcp_sock(subflow);
124 
125 		local_address(skc, &cur);
126 		if (mptcp_addresses_equal(&cur, saddr, saddr->port))
127 			return true;
128 	}
129 
130 	return false;
131 }
132 
133 static bool lookup_subflow_by_daddr(const struct list_head *list,
134 				    const struct mptcp_addr_info *daddr)
135 {
136 	struct mptcp_subflow_context *subflow;
137 	struct mptcp_addr_info cur;
138 	struct sock_common *skc;
139 
140 	list_for_each_entry(subflow, list, node) {
141 		skc = (struct sock_common *)mptcp_subflow_tcp_sock(subflow);
142 
143 		remote_address(skc, &cur);
144 		if (mptcp_addresses_equal(&cur, daddr, daddr->port))
145 			return true;
146 	}
147 
148 	return false;
149 }
150 
151 static struct mptcp_pm_addr_entry *
152 select_local_address(const struct pm_nl_pernet *pernet,
153 		     const struct mptcp_sock *msk)
154 {
155 	const struct sock *sk = (const struct sock *)msk;
156 	struct mptcp_pm_addr_entry *entry, *ret = NULL;
157 
158 	msk_owned_by_me(msk);
159 
160 	rcu_read_lock();
161 	list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
162 		if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW))
163 			continue;
164 
165 		if (!test_bit(entry->addr.id, msk->pm.id_avail_bitmap))
166 			continue;
167 
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)))
174 #endif
175 				continue;
176 		}
177 
178 		ret = entry;
179 		break;
180 	}
181 	rcu_read_unlock();
182 	return ret;
183 }
184 
185 static struct mptcp_pm_addr_entry *
186 select_signal_address(struct pm_nl_pernet *pernet, const struct mptcp_sock *msk)
187 {
188 	struct mptcp_pm_addr_entry *entry, *ret = NULL;
189 
190 	rcu_read_lock();
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.
195 	 */
196 	list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
197 		if (!test_bit(entry->addr.id, msk->pm.id_avail_bitmap))
198 			continue;
199 
200 		if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL))
201 			continue;
202 
203 		ret = entry;
204 		break;
205 	}
206 	rcu_read_unlock();
207 	return ret;
208 }
209 
210 unsigned int mptcp_pm_get_add_addr_signal_max(const struct mptcp_sock *msk)
211 {
212 	const struct pm_nl_pernet *pernet = pm_nl_get_pernet_from_msk(msk);
213 
214 	return READ_ONCE(pernet->add_addr_signal_max);
215 }
216 EXPORT_SYMBOL_GPL(mptcp_pm_get_add_addr_signal_max);
217 
218 unsigned int mptcp_pm_get_add_addr_accept_max(const struct mptcp_sock *msk)
219 {
220 	struct pm_nl_pernet *pernet = pm_nl_get_pernet_from_msk(msk);
221 
222 	return READ_ONCE(pernet->add_addr_accept_max);
223 }
224 EXPORT_SYMBOL_GPL(mptcp_pm_get_add_addr_accept_max);
225 
226 unsigned int mptcp_pm_get_subflows_max(const struct mptcp_sock *msk)
227 {
228 	struct pm_nl_pernet *pernet = pm_nl_get_pernet_from_msk(msk);
229 
230 	return READ_ONCE(pernet->subflows_max);
231 }
232 EXPORT_SYMBOL_GPL(mptcp_pm_get_subflows_max);
233 
234 unsigned int mptcp_pm_get_local_addr_max(const struct mptcp_sock *msk)
235 {
236 	struct pm_nl_pernet *pernet = pm_nl_get_pernet_from_msk(msk);
237 
238 	return READ_ONCE(pernet->local_addr_max);
239 }
240 EXPORT_SYMBOL_GPL(mptcp_pm_get_local_addr_max);
241 
242 bool mptcp_pm_nl_check_work_pending(struct mptcp_sock *msk)
243 {
244 	struct pm_nl_pernet *pernet = pm_nl_get_pernet_from_msk(msk);
245 
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);
250 		return false;
251 	}
252 	return true;
253 }
254 
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)
258 {
259 	struct mptcp_pm_add_entry *entry;
260 
261 	lockdep_assert_held(&msk->pm.lock);
262 
263 	list_for_each_entry(entry, &msk->pm.anno_list, list) {
264 		if (mptcp_addresses_equal(&entry->addr, addr, true))
265 			return entry;
266 	}
267 
268 	return NULL;
269 }
270 
271 bool mptcp_pm_sport_in_anno_list(struct mptcp_sock *msk, const struct sock *sk)
272 {
273 	struct mptcp_pm_add_entry *entry;
274 	struct mptcp_addr_info saddr;
275 	bool ret = false;
276 
277 	local_address((struct sock_common *)sk, &saddr);
278 
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)) {
282 			ret = true;
283 			goto out;
284 		}
285 	}
286 
287 out:
288 	spin_unlock_bh(&msk->pm.lock);
289 	return ret;
290 }
291 
292 static void mptcp_pm_add_timer(struct timer_list *timer)
293 {
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;
297 
298 	pr_debug("msk=%p", msk);
299 
300 	if (!msk)
301 		return;
302 
303 	if (inet_sk_state_load(sk) == TCP_CLOSE)
304 		return;
305 
306 	if (!entry->addr.id)
307 		return;
308 
309 	if (mptcp_pm_should_add_signal_addr(msk)) {
310 		sk_reset_timer(sk, timer, jiffies + TCP_RTO_MAX / 8);
311 		goto out;
312 	}
313 
314 	spin_lock_bh(&msk->pm.lock);
315 
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++;
321 	}
322 
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)));
326 
327 	spin_unlock_bh(&msk->pm.lock);
328 
329 	if (entry->retrans_times == ADD_ADDR_RETRANS_MAX)
330 		mptcp_pm_subflow_established(msk);
331 
332 out:
333 	__sock_put(sk);
334 }
335 
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)
339 {
340 	struct mptcp_pm_add_entry *entry;
341 	struct sock *sk = (struct sock *)msk;
342 
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);
348 
349 	if (entry && (!check_id || entry->addr.id == addr->id))
350 		sk_stop_timer_sync(sk, &entry->add_timer);
351 
352 	return entry;
353 }
354 
355 bool mptcp_pm_alloc_anno_list(struct mptcp_sock *msk,
356 			      const struct mptcp_pm_addr_entry *entry)
357 {
358 	struct mptcp_pm_add_entry *add_entry = NULL;
359 	struct sock *sk = (struct sock *)msk;
360 	struct net *net = sock_net(sk);
361 
362 	lockdep_assert_held(&msk->pm.lock);
363 
364 	add_entry = mptcp_lookup_anno_list_by_saddr(msk, &entry->addr);
365 
366 	if (add_entry) {
367 		if (mptcp_pm_is_kernel(msk))
368 			return false;
369 
370 		sk_reset_timer(sk, &add_entry->add_timer,
371 			       jiffies + mptcp_get_add_addr_timeout(net));
372 		return true;
373 	}
374 
375 	add_entry = kmalloc(sizeof(*add_entry), GFP_ATOMIC);
376 	if (!add_entry)
377 		return false;
378 
379 	list_add(&add_entry->list, &msk->pm.anno_list);
380 
381 	add_entry->addr = entry->addr;
382 	add_entry->sock = msk;
383 	add_entry->retrans_times = 0;
384 
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));
388 
389 	return true;
390 }
391 
392 void mptcp_pm_free_anno_list(struct mptcp_sock *msk)
393 {
394 	struct mptcp_pm_add_entry *entry, *tmp;
395 	struct sock *sk = (struct sock *)msk;
396 	LIST_HEAD(free_list);
397 
398 	pr_debug("msk=%p", msk);
399 
400 	spin_lock_bh(&msk->pm.lock);
401 	list_splice_init(&msk->pm.anno_list, &free_list);
402 	spin_unlock_bh(&msk->pm.lock);
403 
404 	list_for_each_entry_safe(entry, tmp, &free_list, list) {
405 		sk_stop_timer_sync(sk, &entry->add_timer);
406 		kfree(entry);
407 	}
408 }
409 
410 static bool lookup_address_in_vec(const struct mptcp_addr_info *addrs, unsigned int nr,
411 				  const struct mptcp_addr_info *addr)
412 {
413 	int i;
414 
415 	for (i = 0; i < nr; i++) {
416 		if (addrs[i].id == addr->id)
417 			return true;
418 	}
419 
420 	return false;
421 }
422 
423 /* Fill all the remote addresses into the array addrs[],
424  * and return the array size.
425  */
426 static unsigned int fill_remote_addresses_vec(struct mptcp_sock *msk, bool fullmesh,
427 					      struct mptcp_addr_info *addrs)
428 {
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;
434 	int i = 0;
435 
436 	subflows_max = mptcp_pm_get_subflows_max(msk);
437 	remote_address((struct sock_common *)sk, &remote);
438 
439 	/* Non-fullmesh endpoint, fill in the single entry
440 	 * corresponding to the primary MPC subflow remote address
441 	 */
442 	if (!fullmesh) {
443 		if (deny_id0)
444 			return 0;
445 
446 		msk->pm.subflows++;
447 		addrs[i++] = remote;
448 	} else {
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)
454 				continue;
455 
456 			if (!lookup_address_in_vec(addrs, i, &addrs[i]) &&
457 			    msk->pm.subflows < subflows_max) {
458 				msk->pm.subflows++;
459 				i++;
460 			}
461 		}
462 	}
463 
464 	return i;
465 }
466 
467 static void __mptcp_pm_send_ack(struct mptcp_sock *msk, struct mptcp_subflow_context *subflow,
468 				bool prio, bool backup)
469 {
470 	struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
471 	bool slow;
472 
473 	pr_debug("send ack for %s",
474 		 prio ? "mp_prio" : (mptcp_pm_should_add_signal(msk) ? "add_addr" : "rm_addr"));
475 
476 	slow = lock_sock_fast(ssk);
477 	if (prio) {
478 		if (subflow->backup != backup)
479 			msk->last_snd = NULL;
480 
481 		subflow->send_mp_prio = 1;
482 		subflow->backup = backup;
483 		subflow->request_bkup = backup;
484 	}
485 
486 	__mptcp_subflow_send_ack(ssk);
487 	unlock_sock_fast(ssk, slow);
488 }
489 
490 static void mptcp_pm_send_ack(struct mptcp_sock *msk, struct mptcp_subflow_context *subflow,
491 			      bool prio, bool backup)
492 {
493 	spin_unlock_bh(&msk->pm.lock);
494 	__mptcp_pm_send_ack(msk, subflow, prio, backup);
495 	spin_lock_bh(&msk->pm.lock);
496 }
497 
498 static struct mptcp_pm_addr_entry *
499 __lookup_addr_by_id(struct pm_nl_pernet *pernet, unsigned int id)
500 {
501 	struct mptcp_pm_addr_entry *entry;
502 
503 	list_for_each_entry(entry, &pernet->local_addr_list, list) {
504 		if (entry->addr.id == id)
505 			return entry;
506 	}
507 	return NULL;
508 }
509 
510 static struct mptcp_pm_addr_entry *
511 __lookup_addr(struct pm_nl_pernet *pernet, const struct mptcp_addr_info *info,
512 	      bool lookup_by_id)
513 {
514 	struct mptcp_pm_addr_entry *entry;
515 
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))
520 			return entry;
521 	}
522 	return NULL;
523 }
524 
525 static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk)
526 {
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;
533 
534 	pernet = pm_nl_get_pernet(sock_net(sk));
535 
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);
539 
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;
545 		bool backup = false;
546 
547 		local_address((struct sock_common *)msk->first, &mpc_addr);
548 		rcu_read_lock();
549 		entry = __lookup_addr(pernet, &mpc_addr, false);
550 		if (entry) {
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);
554 		}
555 		rcu_read_unlock();
556 
557 		if (backup)
558 			mptcp_pm_send_ack(msk, subflow, true, backup);
559 
560 		msk->pm.status |= BIT(MPTCP_PM_MPC_ENDPOINT_ACCOUNTED);
561 	}
562 
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);
567 
568 	/* check first for announce */
569 	if (msk->pm.add_addr_signaled < add_addr_signal_max) {
570 		local = select_signal_address(pernet, msk);
571 
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.
578 		 */
579 		if (msk->pm.addr_signal & BIT(MPTCP_ADD_ADDR_SIGNAL))
580 			return;
581 
582 		if (local) {
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);
588 			}
589 		}
590 	}
591 
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];
596 		bool fullmesh;
597 		int i, nr;
598 
599 		local = select_local_address(pernet, msk);
600 		if (!local)
601 			break;
602 
603 		fullmesh = !!(local->flags & MPTCP_PM_ADDR_FLAG_FULLMESH);
604 
605 		msk->pm.local_addr_used++;
606 		nr = fill_remote_addresses_vec(msk, fullmesh, addrs);
607 		if (nr)
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);
613 	}
614 	mptcp_pm_nl_check_work_pending(msk);
615 }
616 
617 static void mptcp_pm_nl_fully_established(struct mptcp_sock *msk)
618 {
619 	mptcp_pm_create_subflow_or_signal_addr(msk);
620 }
621 
622 static void mptcp_pm_nl_subflow_established(struct mptcp_sock *msk)
623 {
624 	mptcp_pm_create_subflow_or_signal_addr(msk);
625 }
626 
627 /* Fill all the local addresses into the array addrs[],
628  * and return the array size.
629  */
630 static unsigned int fill_local_addresses_vec(struct mptcp_sock *msk,
631 					     struct mptcp_addr_info *addrs)
632 {
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;
638 	int i = 0;
639 
640 	pernet = pm_nl_get_pernet_from_msk(msk);
641 	subflows_max = mptcp_pm_get_subflows_max(msk);
642 
643 	rcu_read_lock();
644 	list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
645 		if (!(entry->flags & MPTCP_PM_ADDR_FLAG_FULLMESH))
646 			continue;
647 
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)))
654 #endif
655 				continue;
656 		}
657 
658 		if (msk->pm.subflows < subflows_max) {
659 			msk->pm.subflows++;
660 			addrs[i++] = entry->addr;
661 		}
662 	}
663 	rcu_read_unlock();
664 
665 	/* If the array is empty, fill in the single
666 	 * 'IPADDRANY' local address
667 	 */
668 	if (!i) {
669 		memset(&local, 0, sizeof(local));
670 		local.family = msk->pm.remote.family;
671 
672 		msk->pm.subflows++;
673 		addrs[i++] = local;
674 	}
675 
676 	return i;
677 }
678 
679 static void mptcp_pm_nl_add_addr_received(struct mptcp_sock *msk)
680 {
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;
686 	int i, nr;
687 
688 	add_addr_accept_max = mptcp_pm_get_add_addr_accept_max(msk);
689 	subflows_max = mptcp_pm_get_subflows_max(msk);
690 
691 	pr_debug("accepted %d:%d remote family %d",
692 		 msk->pm.add_addr_accepted, add_addr_accept_max,
693 		 msk->pm.remote.family);
694 
695 	remote = msk->pm.remote;
696 	mptcp_pm_announce_addr(msk, &remote, true);
697 	mptcp_pm_nl_addr_send_ack(msk);
698 
699 	if (lookup_subflow_by_daddr(&msk->conn_list, &remote))
700 		return;
701 
702 	/* pick id 0 port, if none is provided the remote address */
703 	if (!remote.port)
704 		remote.port = sk->sk_dport;
705 
706 	/* connect to the specified remote address, using whatever
707 	 * local address the routing configuration will pick.
708 	 */
709 	nr = fill_local_addresses_vec(msk, addrs);
710 
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);
715 
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);
720 }
721 
722 void mptcp_pm_nl_addr_send_ack(struct mptcp_sock *msk)
723 {
724 	struct mptcp_subflow_context *subflow;
725 
726 	msk_owned_by_me(msk);
727 	lockdep_assert_held(&msk->pm.lock);
728 
729 	if (!mptcp_pm_should_add_signal(msk) &&
730 	    !mptcp_pm_should_rm_signal(msk))
731 		return;
732 
733 	subflow = list_first_entry_or_null(&msk->conn_list, typeof(*subflow), node);
734 	if (subflow)
735 		mptcp_pm_send_ack(msk, subflow, false, false);
736 }
737 
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,
741 				 u8 bkup)
742 {
743 	struct mptcp_subflow_context *subflow;
744 
745 	pr_debug("bkup=%d", bkup);
746 
747 	mptcp_for_each_subflow(msk, subflow) {
748 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
749 		struct mptcp_addr_info local, remote;
750 
751 		local_address((struct sock_common *)ssk, &local);
752 		if (!mptcp_addresses_equal(&local, addr, addr->port))
753 			continue;
754 
755 		if (rem && rem->family != AF_UNSPEC) {
756 			remote_address((struct sock_common *)ssk, &remote);
757 			if (!mptcp_addresses_equal(&remote, rem, rem->port))
758 				continue;
759 		}
760 
761 		__mptcp_pm_send_ack(msk, subflow, true, bkup);
762 		return 0;
763 	}
764 
765 	return -EINVAL;
766 }
767 
768 static bool mptcp_local_id_match(const struct mptcp_sock *msk, u8 local_id, u8 id)
769 {
770 	return local_id == id || (!local_id && msk->mpc_endpoint_id == id);
771 }
772 
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)
776 {
777 	struct mptcp_subflow_context *subflow, *tmp;
778 	struct sock *sk = (struct sock *)msk;
779 	u8 i;
780 
781 	pr_debug("%s rm_list_nr %d",
782 		 rm_type == MPTCP_MIB_RMADDR ? "address" : "subflow", rm_list->nr);
783 
784 	msk_owned_by_me(msk);
785 
786 	if (sk->sk_state == TCP_LISTEN)
787 		return;
788 
789 	if (!rm_list->nr)
790 		return;
791 
792 	if (list_empty(&msk->conn_list))
793 		return;
794 
795 	for (i = 0; i < rm_list->nr; i++) {
796 		u8 rm_id = rm_list->ids[i];
797 		bool removed = false;
798 
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;
803 
804 			if (rm_type == MPTCP_MIB_RMADDR && subflow->remote_id != rm_id)
805 				continue;
806 			if (rm_type == MPTCP_MIB_RMSUBFLOW && !mptcp_local_id_match(msk, id, rm_id))
807 				continue;
808 
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);
815 
816 			/* the following takes care of updating the subflows counter */
817 			mptcp_close_ssk(sk, ssk, subflow);
818 			spin_lock_bh(&msk->pm.lock);
819 
820 			removed = true;
821 			__MPTCP_INC_STATS(sock_net(sk), rm_type);
822 		}
823 		if (rm_type == MPTCP_MIB_RMSUBFLOW)
824 			__set_bit(rm_id ? rm_id : msk->mpc_endpoint_id, msk->pm.id_avail_bitmap);
825 		if (!removed)
826 			continue;
827 
828 		if (!mptcp_pm_is_kernel(msk))
829 			continue;
830 
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--;
836 		}
837 	}
838 }
839 
840 static void mptcp_pm_nl_rm_addr_received(struct mptcp_sock *msk)
841 {
842 	mptcp_pm_nl_rm_addr_or_subflow(msk, &msk->pm.rm_list_rx, MPTCP_MIB_RMADDR);
843 }
844 
845 void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk,
846 				     const struct mptcp_rm_list *rm_list)
847 {
848 	mptcp_pm_nl_rm_addr_or_subflow(msk, rm_list, MPTCP_MIB_RMSUBFLOW);
849 }
850 
851 void mptcp_pm_nl_work(struct mptcp_sock *msk)
852 {
853 	struct mptcp_pm_data *pm = &msk->pm;
854 
855 	msk_owned_by_me(msk);
856 
857 	if (!(pm->status & MPTCP_PM_WORK_MASK))
858 		return;
859 
860 	spin_lock_bh(&msk->pm.lock);
861 
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);
866 	}
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);
870 	}
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);
874 	}
875 	if (pm->status & BIT(MPTCP_PM_ESTABLISHED)) {
876 		pm->status &= ~BIT(MPTCP_PM_ESTABLISHED);
877 		mptcp_pm_nl_fully_established(msk);
878 	}
879 	if (pm->status & BIT(MPTCP_PM_SUBFLOW_ESTABLISHED)) {
880 		pm->status &= ~BIT(MPTCP_PM_SUBFLOW_ESTABLISHED);
881 		mptcp_pm_nl_subflow_established(msk);
882 	}
883 
884 	spin_unlock_bh(&msk->pm.lock);
885 }
886 
887 static bool address_use_port(struct mptcp_pm_addr_entry *entry)
888 {
889 	return (entry->flags &
890 		(MPTCP_PM_ADDR_FLAG_SIGNAL | MPTCP_PM_ADDR_FLAG_SUBFLOW)) ==
891 		MPTCP_PM_ADDR_FLAG_SIGNAL;
892 }
893 
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)
896 {
897 	if (entry->lsk)
898 		sock_release(entry->lsk);
899 	kfree(entry);
900 }
901 
902 static int mptcp_pm_nl_append_new_local_addr(struct pm_nl_pernet *pernet,
903 					     struct mptcp_pm_addr_entry *entry)
904 {
905 	struct mptcp_pm_addr_entry *cur, *del_entry = NULL;
906 	unsigned int addr_max;
907 	int ret = -EINVAL;
908 
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
912 	 */
913 	if (pernet->next_id == MPTCP_PM_MAX_ADDR_ID)
914 		pernet->next_id = 1;
915 	if (pernet->addrs >= MPTCP_PM_ADDR_MAX) {
916 		ret = -ERANGE;
917 		goto out;
918 	}
919 	if (test_bit(entry->addr.id, pernet->id_bitmap)) {
920 		ret = -EBUSY;
921 		goto out;
922 	}
923 
924 	/* do not insert duplicate address, differentiate on port only
925 	 * singled addresses
926 	 */
927 	if (!address_use_port(entry))
928 		entry->addr.port = 0;
929 	list_for_each_entry(cur, &pernet->local_addr_list, list) {
930 		if (mptcp_addresses_equal(&cur->addr, &entry->addr,
931 					  cur->addr.port || entry->addr.port)) {
932 			/* allow replacing the exiting endpoint only if such
933 			 * endpoint is an implicit one and the user-space
934 			 * did not provide an endpoint id
935 			 */
936 			if (!(cur->flags & MPTCP_PM_ADDR_FLAG_IMPLICIT)) {
937 				ret = -EEXIST;
938 				goto out;
939 			}
940 			if (entry->addr.id)
941 				goto out;
942 
943 			pernet->addrs--;
944 			entry->addr.id = cur->addr.id;
945 			list_del_rcu(&cur->list);
946 			del_entry = cur;
947 			break;
948 		}
949 	}
950 
951 	if (!entry->addr.id) {
952 find_next:
953 		entry->addr.id = find_next_zero_bit(pernet->id_bitmap,
954 						    MPTCP_PM_MAX_ADDR_ID + 1,
955 						    pernet->next_id);
956 		if (!entry->addr.id && pernet->next_id != 1) {
957 			pernet->next_id = 1;
958 			goto find_next;
959 		}
960 	}
961 
962 	if (!entry->addr.id)
963 		goto out;
964 
965 	__set_bit(entry->addr.id, pernet->id_bitmap);
966 	if (entry->addr.id > pernet->next_id)
967 		pernet->next_id = entry->addr.id;
968 
969 	if (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
970 		addr_max = pernet->add_addr_signal_max;
971 		WRITE_ONCE(pernet->add_addr_signal_max, addr_max + 1);
972 	}
973 	if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) {
974 		addr_max = pernet->local_addr_max;
975 		WRITE_ONCE(pernet->local_addr_max, addr_max + 1);
976 	}
977 
978 	pernet->addrs++;
979 	if (!entry->addr.port)
980 		list_add_tail_rcu(&entry->list, &pernet->local_addr_list);
981 	else
982 		list_add_rcu(&entry->list, &pernet->local_addr_list);
983 	ret = entry->addr.id;
984 
985 out:
986 	spin_unlock_bh(&pernet->lock);
987 
988 	/* just replaced an existing entry, free it */
989 	if (del_entry) {
990 		synchronize_rcu();
991 		__mptcp_pm_release_addr_entry(del_entry);
992 	}
993 	return ret;
994 }
995 
996 static int mptcp_pm_nl_create_listen_socket(struct sock *sk,
997 					    struct mptcp_pm_addr_entry *entry)
998 {
999 	int addrlen = sizeof(struct sockaddr_in);
1000 	struct sockaddr_storage addr;
1001 	struct mptcp_sock *msk;
1002 	struct socket *ssock;
1003 	int backlog = 1024;
1004 	int err;
1005 
1006 	err = sock_create_kern(sock_net(sk), entry->addr.family,
1007 			       SOCK_STREAM, IPPROTO_MPTCP, &entry->lsk);
1008 	if (err)
1009 		return err;
1010 
1011 	msk = mptcp_sk(entry->lsk->sk);
1012 	if (!msk)
1013 		return -EINVAL;
1014 
1015 	ssock = __mptcp_nmpc_socket(msk);
1016 	if (!ssock)
1017 		return -EINVAL;
1018 
1019 	mptcp_info2sockaddr(&entry->addr, &addr, entry->addr.family);
1020 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1021 	if (entry->addr.family == AF_INET6)
1022 		addrlen = sizeof(struct sockaddr_in6);
1023 #endif
1024 	err = kernel_bind(ssock, (struct sockaddr *)&addr, addrlen);
1025 	if (err)
1026 		return err;
1027 
1028 	err = kernel_listen(ssock, backlog);
1029 	if (err)
1030 		return err;
1031 
1032 	mptcp_event_pm_listener(ssock->sk, MPTCP_EVENT_LISTENER_CREATED);
1033 
1034 	return 0;
1035 }
1036 
1037 int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc)
1038 {
1039 	struct mptcp_pm_addr_entry *entry;
1040 	struct mptcp_addr_info skc_local;
1041 	struct mptcp_addr_info msk_local;
1042 	struct pm_nl_pernet *pernet;
1043 	int ret = -1;
1044 
1045 	if (WARN_ON_ONCE(!msk))
1046 		return -1;
1047 
1048 	/* The 0 ID mapping is defined by the first subflow, copied into the msk
1049 	 * addr
1050 	 */
1051 	local_address((struct sock_common *)msk, &msk_local);
1052 	local_address((struct sock_common *)skc, &skc_local);
1053 	if (mptcp_addresses_equal(&msk_local, &skc_local, false))
1054 		return 0;
1055 
1056 	if (mptcp_pm_is_userspace(msk))
1057 		return mptcp_userspace_pm_get_local_id(msk, &skc_local);
1058 
1059 	pernet = pm_nl_get_pernet_from_msk(msk);
1060 
1061 	rcu_read_lock();
1062 	list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
1063 		if (mptcp_addresses_equal(&entry->addr, &skc_local, entry->addr.port)) {
1064 			ret = entry->addr.id;
1065 			break;
1066 		}
1067 	}
1068 	rcu_read_unlock();
1069 	if (ret >= 0)
1070 		return ret;
1071 
1072 	/* address not found, add to local list */
1073 	entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
1074 	if (!entry)
1075 		return -ENOMEM;
1076 
1077 	entry->addr = skc_local;
1078 	entry->addr.id = 0;
1079 	entry->addr.port = 0;
1080 	entry->ifindex = 0;
1081 	entry->flags = MPTCP_PM_ADDR_FLAG_IMPLICIT;
1082 	entry->lsk = NULL;
1083 	ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
1084 	if (ret < 0)
1085 		kfree(entry);
1086 
1087 	return ret;
1088 }
1089 
1090 #define MPTCP_PM_CMD_GRP_OFFSET       0
1091 #define MPTCP_PM_EV_GRP_OFFSET        1
1092 
1093 static const struct genl_multicast_group mptcp_pm_mcgrps[] = {
1094 	[MPTCP_PM_CMD_GRP_OFFSET]	= { .name = MPTCP_PM_CMD_GRP_NAME, },
1095 	[MPTCP_PM_EV_GRP_OFFSET]        = { .name = MPTCP_PM_EV_GRP_NAME,
1096 					    .flags = GENL_UNS_ADMIN_PERM,
1097 					  },
1098 };
1099 
1100 static const struct nla_policy
1101 mptcp_pm_addr_policy[MPTCP_PM_ADDR_ATTR_MAX + 1] = {
1102 	[MPTCP_PM_ADDR_ATTR_FAMILY]	= { .type	= NLA_U16,	},
1103 	[MPTCP_PM_ADDR_ATTR_ID]		= { .type	= NLA_U8,	},
1104 	[MPTCP_PM_ADDR_ATTR_ADDR4]	= { .type	= NLA_U32,	},
1105 	[MPTCP_PM_ADDR_ATTR_ADDR6]	=
1106 		NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
1107 	[MPTCP_PM_ADDR_ATTR_PORT]	= { .type	= NLA_U16	},
1108 	[MPTCP_PM_ADDR_ATTR_FLAGS]	= { .type	= NLA_U32	},
1109 	[MPTCP_PM_ADDR_ATTR_IF_IDX]     = { .type	= NLA_S32	},
1110 };
1111 
1112 static const struct nla_policy mptcp_pm_policy[MPTCP_PM_ATTR_MAX + 1] = {
1113 	[MPTCP_PM_ATTR_ADDR]		=
1114 					NLA_POLICY_NESTED(mptcp_pm_addr_policy),
1115 	[MPTCP_PM_ATTR_RCV_ADD_ADDRS]	= { .type	= NLA_U32,	},
1116 	[MPTCP_PM_ATTR_SUBFLOWS]	= { .type	= NLA_U32,	},
1117 	[MPTCP_PM_ATTR_TOKEN]		= { .type	= NLA_U32,	},
1118 	[MPTCP_PM_ATTR_LOC_ID]		= { .type	= NLA_U8,	},
1119 	[MPTCP_PM_ATTR_ADDR_REMOTE]	=
1120 					NLA_POLICY_NESTED(mptcp_pm_addr_policy),
1121 };
1122 
1123 void mptcp_pm_nl_subflow_chk_stale(const struct mptcp_sock *msk, struct sock *ssk)
1124 {
1125 	struct mptcp_subflow_context *iter, *subflow = mptcp_subflow_ctx(ssk);
1126 	struct sock *sk = (struct sock *)msk;
1127 	unsigned int active_max_loss_cnt;
1128 	struct net *net = sock_net(sk);
1129 	unsigned int stale_loss_cnt;
1130 	bool slow;
1131 
1132 	stale_loss_cnt = mptcp_stale_loss_cnt(net);
1133 	if (subflow->stale || !stale_loss_cnt || subflow->stale_count <= stale_loss_cnt)
1134 		return;
1135 
1136 	/* look for another available subflow not in loss state */
1137 	active_max_loss_cnt = max_t(int, stale_loss_cnt - 1, 1);
1138 	mptcp_for_each_subflow(msk, iter) {
1139 		if (iter != subflow && mptcp_subflow_active(iter) &&
1140 		    iter->stale_count < active_max_loss_cnt) {
1141 			/* we have some alternatives, try to mark this subflow as idle ...*/
1142 			slow = lock_sock_fast(ssk);
1143 			if (!tcp_rtx_and_write_queues_empty(ssk)) {
1144 				subflow->stale = 1;
1145 				__mptcp_retransmit_pending_data(sk);
1146 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_SUBFLOWSTALE);
1147 			}
1148 			unlock_sock_fast(ssk, slow);
1149 
1150 			/* always try to push the pending data regardless of re-injections:
1151 			 * we can possibly use backup subflows now, and subflow selection
1152 			 * is cheap under the msk socket lock
1153 			 */
1154 			__mptcp_push_pending(sk, 0);
1155 			return;
1156 		}
1157 	}
1158 }
1159 
1160 static int mptcp_pm_family_to_addr(int family)
1161 {
1162 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1163 	if (family == AF_INET6)
1164 		return MPTCP_PM_ADDR_ATTR_ADDR6;
1165 #endif
1166 	return MPTCP_PM_ADDR_ATTR_ADDR4;
1167 }
1168 
1169 static int mptcp_pm_parse_pm_addr_attr(struct nlattr *tb[],
1170 				       const struct nlattr *attr,
1171 				       struct genl_info *info,
1172 				       struct mptcp_addr_info *addr,
1173 				       bool require_family)
1174 {
1175 	int err, addr_addr;
1176 
1177 	if (!attr) {
1178 		GENL_SET_ERR_MSG(info, "missing address info");
1179 		return -EINVAL;
1180 	}
1181 
1182 	/* no validation needed - was already done via nested policy */
1183 	err = nla_parse_nested_deprecated(tb, MPTCP_PM_ADDR_ATTR_MAX, attr,
1184 					  mptcp_pm_addr_policy, info->extack);
1185 	if (err)
1186 		return err;
1187 
1188 	if (tb[MPTCP_PM_ADDR_ATTR_ID])
1189 		addr->id = nla_get_u8(tb[MPTCP_PM_ADDR_ATTR_ID]);
1190 
1191 	if (!tb[MPTCP_PM_ADDR_ATTR_FAMILY]) {
1192 		if (!require_family)
1193 			return 0;
1194 
1195 		NL_SET_ERR_MSG_ATTR(info->extack, attr,
1196 				    "missing family");
1197 		return -EINVAL;
1198 	}
1199 
1200 	addr->family = nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_FAMILY]);
1201 	if (addr->family != AF_INET
1202 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1203 	    && addr->family != AF_INET6
1204 #endif
1205 	    ) {
1206 		NL_SET_ERR_MSG_ATTR(info->extack, attr,
1207 				    "unknown address family");
1208 		return -EINVAL;
1209 	}
1210 	addr_addr = mptcp_pm_family_to_addr(addr->family);
1211 	if (!tb[addr_addr]) {
1212 		NL_SET_ERR_MSG_ATTR(info->extack, attr,
1213 				    "missing address data");
1214 		return -EINVAL;
1215 	}
1216 
1217 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1218 	if (addr->family == AF_INET6)
1219 		addr->addr6 = nla_get_in6_addr(tb[addr_addr]);
1220 	else
1221 #endif
1222 		addr->addr.s_addr = nla_get_in_addr(tb[addr_addr]);
1223 
1224 	if (tb[MPTCP_PM_ADDR_ATTR_PORT])
1225 		addr->port = htons(nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_PORT]));
1226 
1227 	return 0;
1228 }
1229 
1230 int mptcp_pm_parse_addr(struct nlattr *attr, struct genl_info *info,
1231 			struct mptcp_addr_info *addr)
1232 {
1233 	struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1];
1234 
1235 	memset(addr, 0, sizeof(*addr));
1236 
1237 	return mptcp_pm_parse_pm_addr_attr(tb, attr, info, addr, true);
1238 }
1239 
1240 int mptcp_pm_parse_entry(struct nlattr *attr, struct genl_info *info,
1241 			 bool require_family,
1242 			 struct mptcp_pm_addr_entry *entry)
1243 {
1244 	struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1];
1245 	int err;
1246 
1247 	memset(entry, 0, sizeof(*entry));
1248 
1249 	err = mptcp_pm_parse_pm_addr_attr(tb, attr, info, &entry->addr, require_family);
1250 	if (err)
1251 		return err;
1252 
1253 	if (tb[MPTCP_PM_ADDR_ATTR_IF_IDX]) {
1254 		u32 val = nla_get_s32(tb[MPTCP_PM_ADDR_ATTR_IF_IDX]);
1255 
1256 		entry->ifindex = val;
1257 	}
1258 
1259 	if (tb[MPTCP_PM_ADDR_ATTR_FLAGS])
1260 		entry->flags = nla_get_u32(tb[MPTCP_PM_ADDR_ATTR_FLAGS]);
1261 
1262 	if (tb[MPTCP_PM_ADDR_ATTR_PORT])
1263 		entry->addr.port = htons(nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_PORT]));
1264 
1265 	return 0;
1266 }
1267 
1268 static struct pm_nl_pernet *genl_info_pm_nl(struct genl_info *info)
1269 {
1270 	return pm_nl_get_pernet(genl_info_net(info));
1271 }
1272 
1273 static int mptcp_nl_add_subflow_or_signal_addr(struct net *net)
1274 {
1275 	struct mptcp_sock *msk;
1276 	long s_slot = 0, s_num = 0;
1277 
1278 	while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1279 		struct sock *sk = (struct sock *)msk;
1280 
1281 		if (!READ_ONCE(msk->fully_established) ||
1282 		    mptcp_pm_is_userspace(msk))
1283 			goto next;
1284 
1285 		lock_sock(sk);
1286 		spin_lock_bh(&msk->pm.lock);
1287 		mptcp_pm_create_subflow_or_signal_addr(msk);
1288 		spin_unlock_bh(&msk->pm.lock);
1289 		release_sock(sk);
1290 
1291 next:
1292 		sock_put(sk);
1293 		cond_resched();
1294 	}
1295 
1296 	return 0;
1297 }
1298 
1299 static int mptcp_nl_cmd_add_addr(struct sk_buff *skb, struct genl_info *info)
1300 {
1301 	struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1302 	struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1303 	struct mptcp_pm_addr_entry addr, *entry;
1304 	int ret;
1305 
1306 	ret = mptcp_pm_parse_entry(attr, info, true, &addr);
1307 	if (ret < 0)
1308 		return ret;
1309 
1310 	if (addr.addr.port && !(addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) {
1311 		GENL_SET_ERR_MSG(info, "flags must have signal when using port");
1312 		return -EINVAL;
1313 	}
1314 
1315 	if (addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL &&
1316 	    addr.flags & MPTCP_PM_ADDR_FLAG_FULLMESH) {
1317 		GENL_SET_ERR_MSG(info, "flags mustn't have both signal and fullmesh");
1318 		return -EINVAL;
1319 	}
1320 
1321 	if (addr.flags & MPTCP_PM_ADDR_FLAG_IMPLICIT) {
1322 		GENL_SET_ERR_MSG(info, "can't create IMPLICIT endpoint");
1323 		return -EINVAL;
1324 	}
1325 
1326 	entry = kzalloc(sizeof(*entry), GFP_KERNEL_ACCOUNT);
1327 	if (!entry) {
1328 		GENL_SET_ERR_MSG(info, "can't allocate addr");
1329 		return -ENOMEM;
1330 	}
1331 
1332 	*entry = addr;
1333 	if (entry->addr.port) {
1334 		ret = mptcp_pm_nl_create_listen_socket(skb->sk, entry);
1335 		if (ret) {
1336 			GENL_SET_ERR_MSG_FMT(info, "create listen socket error: %d", ret);
1337 			goto out_free;
1338 		}
1339 	}
1340 	ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
1341 	if (ret < 0) {
1342 		GENL_SET_ERR_MSG_FMT(info, "too many addresses or duplicate one: %d", ret);
1343 		goto out_free;
1344 	}
1345 
1346 	mptcp_nl_add_subflow_or_signal_addr(sock_net(skb->sk));
1347 	return 0;
1348 
1349 out_free:
1350 	__mptcp_pm_release_addr_entry(entry);
1351 	return ret;
1352 }
1353 
1354 int mptcp_pm_get_flags_and_ifindex_by_id(struct mptcp_sock *msk, unsigned int id,
1355 					 u8 *flags, int *ifindex)
1356 {
1357 	struct mptcp_pm_addr_entry *entry;
1358 	struct sock *sk = (struct sock *)msk;
1359 	struct net *net = sock_net(sk);
1360 
1361 	*flags = 0;
1362 	*ifindex = 0;
1363 
1364 	if (id) {
1365 		if (mptcp_pm_is_userspace(msk))
1366 			return mptcp_userspace_pm_get_flags_and_ifindex_by_id(msk,
1367 									      id,
1368 									      flags,
1369 									      ifindex);
1370 
1371 		rcu_read_lock();
1372 		entry = __lookup_addr_by_id(pm_nl_get_pernet(net), id);
1373 		if (entry) {
1374 			*flags = entry->flags;
1375 			*ifindex = entry->ifindex;
1376 		}
1377 		rcu_read_unlock();
1378 	}
1379 
1380 	return 0;
1381 }
1382 
1383 static bool remove_anno_list_by_saddr(struct mptcp_sock *msk,
1384 				      const struct mptcp_addr_info *addr)
1385 {
1386 	struct mptcp_pm_add_entry *entry;
1387 
1388 	entry = mptcp_pm_del_add_timer(msk, addr, false);
1389 	if (entry) {
1390 		list_del(&entry->list);
1391 		kfree(entry);
1392 		return true;
1393 	}
1394 
1395 	return false;
1396 }
1397 
1398 static bool mptcp_pm_remove_anno_addr(struct mptcp_sock *msk,
1399 				      const struct mptcp_addr_info *addr,
1400 				      bool force)
1401 {
1402 	struct mptcp_rm_list list = { .nr = 0 };
1403 	bool ret;
1404 
1405 	list.ids[list.nr++] = addr->id;
1406 
1407 	ret = remove_anno_list_by_saddr(msk, addr);
1408 	if (ret || force) {
1409 		spin_lock_bh(&msk->pm.lock);
1410 		mptcp_pm_remove_addr(msk, &list);
1411 		spin_unlock_bh(&msk->pm.lock);
1412 	}
1413 	return ret;
1414 }
1415 
1416 static int mptcp_nl_remove_subflow_and_signal_addr(struct net *net,
1417 						   const struct mptcp_pm_addr_entry *entry)
1418 {
1419 	const struct mptcp_addr_info *addr = &entry->addr;
1420 	struct mptcp_rm_list list = { .nr = 0 };
1421 	long s_slot = 0, s_num = 0;
1422 	struct mptcp_sock *msk;
1423 
1424 	pr_debug("remove_id=%d", addr->id);
1425 
1426 	list.ids[list.nr++] = addr->id;
1427 
1428 	while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1429 		struct sock *sk = (struct sock *)msk;
1430 		bool remove_subflow;
1431 
1432 		if (mptcp_pm_is_userspace(msk))
1433 			goto next;
1434 
1435 		if (list_empty(&msk->conn_list)) {
1436 			mptcp_pm_remove_anno_addr(msk, addr, false);
1437 			goto next;
1438 		}
1439 
1440 		lock_sock(sk);
1441 		remove_subflow = lookup_subflow_by_saddr(&msk->conn_list, addr);
1442 		mptcp_pm_remove_anno_addr(msk, addr, remove_subflow &&
1443 					  !(entry->flags & MPTCP_PM_ADDR_FLAG_IMPLICIT));
1444 		if (remove_subflow)
1445 			mptcp_pm_remove_subflow(msk, &list);
1446 		release_sock(sk);
1447 
1448 next:
1449 		sock_put(sk);
1450 		cond_resched();
1451 	}
1452 
1453 	return 0;
1454 }
1455 
1456 static int mptcp_nl_remove_id_zero_address(struct net *net,
1457 					   struct mptcp_addr_info *addr)
1458 {
1459 	struct mptcp_rm_list list = { .nr = 0 };
1460 	long s_slot = 0, s_num = 0;
1461 	struct mptcp_sock *msk;
1462 
1463 	list.ids[list.nr++] = 0;
1464 
1465 	while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1466 		struct sock *sk = (struct sock *)msk;
1467 		struct mptcp_addr_info msk_local;
1468 
1469 		if (list_empty(&msk->conn_list) || mptcp_pm_is_userspace(msk))
1470 			goto next;
1471 
1472 		local_address((struct sock_common *)msk, &msk_local);
1473 		if (!mptcp_addresses_equal(&msk_local, addr, addr->port))
1474 			goto next;
1475 
1476 		lock_sock(sk);
1477 		spin_lock_bh(&msk->pm.lock);
1478 		mptcp_pm_remove_addr(msk, &list);
1479 		mptcp_pm_nl_rm_subflow_received(msk, &list);
1480 		spin_unlock_bh(&msk->pm.lock);
1481 		release_sock(sk);
1482 
1483 next:
1484 		sock_put(sk);
1485 		cond_resched();
1486 	}
1487 
1488 	return 0;
1489 }
1490 
1491 static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info)
1492 {
1493 	struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1494 	struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1495 	struct mptcp_pm_addr_entry addr, *entry;
1496 	unsigned int addr_max;
1497 	int ret;
1498 
1499 	ret = mptcp_pm_parse_entry(attr, info, false, &addr);
1500 	if (ret < 0)
1501 		return ret;
1502 
1503 	/* the zero id address is special: the first address used by the msk
1504 	 * always gets such an id, so different subflows can have different zero
1505 	 * id addresses. Additionally zero id is not accounted for in id_bitmap.
1506 	 * Let's use an 'mptcp_rm_list' instead of the common remove code.
1507 	 */
1508 	if (addr.addr.id == 0)
1509 		return mptcp_nl_remove_id_zero_address(sock_net(skb->sk), &addr.addr);
1510 
1511 	spin_lock_bh(&pernet->lock);
1512 	entry = __lookup_addr_by_id(pernet, addr.addr.id);
1513 	if (!entry) {
1514 		GENL_SET_ERR_MSG(info, "address not found");
1515 		spin_unlock_bh(&pernet->lock);
1516 		return -EINVAL;
1517 	}
1518 	if (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
1519 		addr_max = pernet->add_addr_signal_max;
1520 		WRITE_ONCE(pernet->add_addr_signal_max, addr_max - 1);
1521 	}
1522 	if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) {
1523 		addr_max = pernet->local_addr_max;
1524 		WRITE_ONCE(pernet->local_addr_max, addr_max - 1);
1525 	}
1526 
1527 	pernet->addrs--;
1528 	list_del_rcu(&entry->list);
1529 	__clear_bit(entry->addr.id, pernet->id_bitmap);
1530 	spin_unlock_bh(&pernet->lock);
1531 
1532 	mptcp_nl_remove_subflow_and_signal_addr(sock_net(skb->sk), entry);
1533 	synchronize_rcu();
1534 	__mptcp_pm_release_addr_entry(entry);
1535 
1536 	return ret;
1537 }
1538 
1539 void mptcp_pm_remove_addrs_and_subflows(struct mptcp_sock *msk,
1540 					struct list_head *rm_list)
1541 {
1542 	struct mptcp_rm_list alist = { .nr = 0 }, slist = { .nr = 0 };
1543 	struct mptcp_pm_addr_entry *entry;
1544 
1545 	list_for_each_entry(entry, rm_list, list) {
1546 		if (lookup_subflow_by_saddr(&msk->conn_list, &entry->addr) &&
1547 		    slist.nr < MPTCP_RM_IDS_MAX)
1548 			slist.ids[slist.nr++] = entry->addr.id;
1549 
1550 		if (remove_anno_list_by_saddr(msk, &entry->addr) &&
1551 		    alist.nr < MPTCP_RM_IDS_MAX)
1552 			alist.ids[alist.nr++] = entry->addr.id;
1553 	}
1554 
1555 	if (alist.nr) {
1556 		spin_lock_bh(&msk->pm.lock);
1557 		mptcp_pm_remove_addr(msk, &alist);
1558 		spin_unlock_bh(&msk->pm.lock);
1559 	}
1560 	if (slist.nr)
1561 		mptcp_pm_remove_subflow(msk, &slist);
1562 }
1563 
1564 static void mptcp_nl_remove_addrs_list(struct net *net,
1565 				       struct list_head *rm_list)
1566 {
1567 	long s_slot = 0, s_num = 0;
1568 	struct mptcp_sock *msk;
1569 
1570 	if (list_empty(rm_list))
1571 		return;
1572 
1573 	while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1574 		struct sock *sk = (struct sock *)msk;
1575 
1576 		if (!mptcp_pm_is_userspace(msk)) {
1577 			lock_sock(sk);
1578 			mptcp_pm_remove_addrs_and_subflows(msk, rm_list);
1579 			release_sock(sk);
1580 		}
1581 
1582 		sock_put(sk);
1583 		cond_resched();
1584 	}
1585 }
1586 
1587 /* caller must ensure the RCU grace period is already elapsed */
1588 static void __flush_addrs(struct list_head *list)
1589 {
1590 	while (!list_empty(list)) {
1591 		struct mptcp_pm_addr_entry *cur;
1592 
1593 		cur = list_entry(list->next,
1594 				 struct mptcp_pm_addr_entry, list);
1595 		list_del_rcu(&cur->list);
1596 		__mptcp_pm_release_addr_entry(cur);
1597 	}
1598 }
1599 
1600 static void __reset_counters(struct pm_nl_pernet *pernet)
1601 {
1602 	WRITE_ONCE(pernet->add_addr_signal_max, 0);
1603 	WRITE_ONCE(pernet->add_addr_accept_max, 0);
1604 	WRITE_ONCE(pernet->local_addr_max, 0);
1605 	pernet->addrs = 0;
1606 }
1607 
1608 static int mptcp_nl_cmd_flush_addrs(struct sk_buff *skb, struct genl_info *info)
1609 {
1610 	struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1611 	LIST_HEAD(free_list);
1612 
1613 	spin_lock_bh(&pernet->lock);
1614 	list_splice_init(&pernet->local_addr_list, &free_list);
1615 	__reset_counters(pernet);
1616 	pernet->next_id = 1;
1617 	bitmap_zero(pernet->id_bitmap, MPTCP_PM_MAX_ADDR_ID + 1);
1618 	spin_unlock_bh(&pernet->lock);
1619 	mptcp_nl_remove_addrs_list(sock_net(skb->sk), &free_list);
1620 	synchronize_rcu();
1621 	__flush_addrs(&free_list);
1622 	return 0;
1623 }
1624 
1625 static int mptcp_nl_fill_addr(struct sk_buff *skb,
1626 			      struct mptcp_pm_addr_entry *entry)
1627 {
1628 	struct mptcp_addr_info *addr = &entry->addr;
1629 	struct nlattr *attr;
1630 
1631 	attr = nla_nest_start(skb, MPTCP_PM_ATTR_ADDR);
1632 	if (!attr)
1633 		return -EMSGSIZE;
1634 
1635 	if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_FAMILY, addr->family))
1636 		goto nla_put_failure;
1637 	if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_PORT, ntohs(addr->port)))
1638 		goto nla_put_failure;
1639 	if (nla_put_u8(skb, MPTCP_PM_ADDR_ATTR_ID, addr->id))
1640 		goto nla_put_failure;
1641 	if (nla_put_u32(skb, MPTCP_PM_ADDR_ATTR_FLAGS, entry->flags))
1642 		goto nla_put_failure;
1643 	if (entry->ifindex &&
1644 	    nla_put_s32(skb, MPTCP_PM_ADDR_ATTR_IF_IDX, entry->ifindex))
1645 		goto nla_put_failure;
1646 
1647 	if (addr->family == AF_INET &&
1648 	    nla_put_in_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR4,
1649 			    addr->addr.s_addr))
1650 		goto nla_put_failure;
1651 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1652 	else if (addr->family == AF_INET6 &&
1653 		 nla_put_in6_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR6, &addr->addr6))
1654 		goto nla_put_failure;
1655 #endif
1656 	nla_nest_end(skb, attr);
1657 	return 0;
1658 
1659 nla_put_failure:
1660 	nla_nest_cancel(skb, attr);
1661 	return -EMSGSIZE;
1662 }
1663 
1664 static int mptcp_nl_cmd_get_addr(struct sk_buff *skb, struct genl_info *info)
1665 {
1666 	struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1667 	struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1668 	struct mptcp_pm_addr_entry addr, *entry;
1669 	struct sk_buff *msg;
1670 	void *reply;
1671 	int ret;
1672 
1673 	ret = mptcp_pm_parse_entry(attr, info, false, &addr);
1674 	if (ret < 0)
1675 		return ret;
1676 
1677 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1678 	if (!msg)
1679 		return -ENOMEM;
1680 
1681 	reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1682 				  info->genlhdr->cmd);
1683 	if (!reply) {
1684 		GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1685 		ret = -EMSGSIZE;
1686 		goto fail;
1687 	}
1688 
1689 	spin_lock_bh(&pernet->lock);
1690 	entry = __lookup_addr_by_id(pernet, addr.addr.id);
1691 	if (!entry) {
1692 		GENL_SET_ERR_MSG(info, "address not found");
1693 		ret = -EINVAL;
1694 		goto unlock_fail;
1695 	}
1696 
1697 	ret = mptcp_nl_fill_addr(msg, entry);
1698 	if (ret)
1699 		goto unlock_fail;
1700 
1701 	genlmsg_end(msg, reply);
1702 	ret = genlmsg_reply(msg, info);
1703 	spin_unlock_bh(&pernet->lock);
1704 	return ret;
1705 
1706 unlock_fail:
1707 	spin_unlock_bh(&pernet->lock);
1708 
1709 fail:
1710 	nlmsg_free(msg);
1711 	return ret;
1712 }
1713 
1714 static int mptcp_nl_cmd_dump_addrs(struct sk_buff *msg,
1715 				   struct netlink_callback *cb)
1716 {
1717 	struct net *net = sock_net(msg->sk);
1718 	struct mptcp_pm_addr_entry *entry;
1719 	struct pm_nl_pernet *pernet;
1720 	int id = cb->args[0];
1721 	void *hdr;
1722 	int i;
1723 
1724 	pernet = pm_nl_get_pernet(net);
1725 
1726 	spin_lock_bh(&pernet->lock);
1727 	for (i = id; i < MPTCP_PM_MAX_ADDR_ID + 1; i++) {
1728 		if (test_bit(i, pernet->id_bitmap)) {
1729 			entry = __lookup_addr_by_id(pernet, i);
1730 			if (!entry)
1731 				break;
1732 
1733 			if (entry->addr.id <= id)
1734 				continue;
1735 
1736 			hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid,
1737 					  cb->nlh->nlmsg_seq, &mptcp_genl_family,
1738 					  NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR);
1739 			if (!hdr)
1740 				break;
1741 
1742 			if (mptcp_nl_fill_addr(msg, entry) < 0) {
1743 				genlmsg_cancel(msg, hdr);
1744 				break;
1745 			}
1746 
1747 			id = entry->addr.id;
1748 			genlmsg_end(msg, hdr);
1749 		}
1750 	}
1751 	spin_unlock_bh(&pernet->lock);
1752 
1753 	cb->args[0] = id;
1754 	return msg->len;
1755 }
1756 
1757 static int parse_limit(struct genl_info *info, int id, unsigned int *limit)
1758 {
1759 	struct nlattr *attr = info->attrs[id];
1760 
1761 	if (!attr)
1762 		return 0;
1763 
1764 	*limit = nla_get_u32(attr);
1765 	if (*limit > MPTCP_PM_ADDR_MAX) {
1766 		GENL_SET_ERR_MSG(info, "limit greater than maximum");
1767 		return -EINVAL;
1768 	}
1769 	return 0;
1770 }
1771 
1772 static int
1773 mptcp_nl_cmd_set_limits(struct sk_buff *skb, struct genl_info *info)
1774 {
1775 	struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1776 	unsigned int rcv_addrs, subflows;
1777 	int ret;
1778 
1779 	spin_lock_bh(&pernet->lock);
1780 	rcv_addrs = pernet->add_addr_accept_max;
1781 	ret = parse_limit(info, MPTCP_PM_ATTR_RCV_ADD_ADDRS, &rcv_addrs);
1782 	if (ret)
1783 		goto unlock;
1784 
1785 	subflows = pernet->subflows_max;
1786 	ret = parse_limit(info, MPTCP_PM_ATTR_SUBFLOWS, &subflows);
1787 	if (ret)
1788 		goto unlock;
1789 
1790 	WRITE_ONCE(pernet->add_addr_accept_max, rcv_addrs);
1791 	WRITE_ONCE(pernet->subflows_max, subflows);
1792 
1793 unlock:
1794 	spin_unlock_bh(&pernet->lock);
1795 	return ret;
1796 }
1797 
1798 static int
1799 mptcp_nl_cmd_get_limits(struct sk_buff *skb, struct genl_info *info)
1800 {
1801 	struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1802 	struct sk_buff *msg;
1803 	void *reply;
1804 
1805 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1806 	if (!msg)
1807 		return -ENOMEM;
1808 
1809 	reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1810 				  MPTCP_PM_CMD_GET_LIMITS);
1811 	if (!reply)
1812 		goto fail;
1813 
1814 	if (nla_put_u32(msg, MPTCP_PM_ATTR_RCV_ADD_ADDRS,
1815 			READ_ONCE(pernet->add_addr_accept_max)))
1816 		goto fail;
1817 
1818 	if (nla_put_u32(msg, MPTCP_PM_ATTR_SUBFLOWS,
1819 			READ_ONCE(pernet->subflows_max)))
1820 		goto fail;
1821 
1822 	genlmsg_end(msg, reply);
1823 	return genlmsg_reply(msg, info);
1824 
1825 fail:
1826 	GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1827 	nlmsg_free(msg);
1828 	return -EMSGSIZE;
1829 }
1830 
1831 static void mptcp_pm_nl_fullmesh(struct mptcp_sock *msk,
1832 				 struct mptcp_addr_info *addr)
1833 {
1834 	struct mptcp_rm_list list = { .nr = 0 };
1835 
1836 	list.ids[list.nr++] = addr->id;
1837 
1838 	spin_lock_bh(&msk->pm.lock);
1839 	mptcp_pm_nl_rm_subflow_received(msk, &list);
1840 	mptcp_pm_create_subflow_or_signal_addr(msk);
1841 	spin_unlock_bh(&msk->pm.lock);
1842 }
1843 
1844 static int mptcp_nl_set_flags(struct net *net,
1845 			      struct mptcp_addr_info *addr,
1846 			      u8 bkup, u8 changed)
1847 {
1848 	long s_slot = 0, s_num = 0;
1849 	struct mptcp_sock *msk;
1850 	int ret = -EINVAL;
1851 
1852 	while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1853 		struct sock *sk = (struct sock *)msk;
1854 
1855 		if (list_empty(&msk->conn_list) || mptcp_pm_is_userspace(msk))
1856 			goto next;
1857 
1858 		lock_sock(sk);
1859 		if (changed & MPTCP_PM_ADDR_FLAG_BACKUP)
1860 			ret = mptcp_pm_nl_mp_prio_send_ack(msk, addr, NULL, bkup);
1861 		if (changed & MPTCP_PM_ADDR_FLAG_FULLMESH)
1862 			mptcp_pm_nl_fullmesh(msk, addr);
1863 		release_sock(sk);
1864 
1865 next:
1866 		sock_put(sk);
1867 		cond_resched();
1868 	}
1869 
1870 	return ret;
1871 }
1872 
1873 static int mptcp_nl_cmd_set_flags(struct sk_buff *skb, struct genl_info *info)
1874 {
1875 	struct mptcp_pm_addr_entry addr = { .addr = { .family = AF_UNSPEC }, }, *entry;
1876 	struct mptcp_pm_addr_entry remote = { .addr = { .family = AF_UNSPEC }, };
1877 	struct nlattr *attr_rem = info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE];
1878 	struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
1879 	struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1880 	struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1881 	u8 changed, mask = MPTCP_PM_ADDR_FLAG_BACKUP |
1882 			   MPTCP_PM_ADDR_FLAG_FULLMESH;
1883 	struct net *net = sock_net(skb->sk);
1884 	u8 bkup = 0, lookup_by_id = 0;
1885 	int ret;
1886 
1887 	ret = mptcp_pm_parse_entry(attr, info, false, &addr);
1888 	if (ret < 0)
1889 		return ret;
1890 
1891 	if (attr_rem) {
1892 		ret = mptcp_pm_parse_entry(attr_rem, info, false, &remote);
1893 		if (ret < 0)
1894 			return ret;
1895 	}
1896 
1897 	if (addr.flags & MPTCP_PM_ADDR_FLAG_BACKUP)
1898 		bkup = 1;
1899 	if (addr.addr.family == AF_UNSPEC) {
1900 		lookup_by_id = 1;
1901 		if (!addr.addr.id)
1902 			return -EOPNOTSUPP;
1903 	}
1904 
1905 	if (token)
1906 		return mptcp_userspace_pm_set_flags(sock_net(skb->sk),
1907 						    token, &addr, &remote, bkup);
1908 
1909 	spin_lock_bh(&pernet->lock);
1910 	entry = __lookup_addr(pernet, &addr.addr, lookup_by_id);
1911 	if (!entry) {
1912 		spin_unlock_bh(&pernet->lock);
1913 		return -EINVAL;
1914 	}
1915 	if ((addr.flags & MPTCP_PM_ADDR_FLAG_FULLMESH) &&
1916 	    (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) {
1917 		spin_unlock_bh(&pernet->lock);
1918 		return -EINVAL;
1919 	}
1920 
1921 	changed = (addr.flags ^ entry->flags) & mask;
1922 	entry->flags = (entry->flags & ~mask) | (addr.flags & mask);
1923 	addr = *entry;
1924 	spin_unlock_bh(&pernet->lock);
1925 
1926 	mptcp_nl_set_flags(net, &addr.addr, bkup, changed);
1927 	return 0;
1928 }
1929 
1930 static void mptcp_nl_mcast_send(struct net *net, struct sk_buff *nlskb, gfp_t gfp)
1931 {
1932 	genlmsg_multicast_netns(&mptcp_genl_family, net,
1933 				nlskb, 0, MPTCP_PM_EV_GRP_OFFSET, gfp);
1934 }
1935 
1936 bool mptcp_userspace_pm_active(const struct mptcp_sock *msk)
1937 {
1938 	return genl_has_listeners(&mptcp_genl_family,
1939 				  sock_net((const struct sock *)msk),
1940 				  MPTCP_PM_EV_GRP_OFFSET);
1941 }
1942 
1943 static int mptcp_event_add_subflow(struct sk_buff *skb, const struct sock *ssk)
1944 {
1945 	const struct inet_sock *issk = inet_sk(ssk);
1946 	const struct mptcp_subflow_context *sf;
1947 
1948 	if (nla_put_u16(skb, MPTCP_ATTR_FAMILY, ssk->sk_family))
1949 		return -EMSGSIZE;
1950 
1951 	switch (ssk->sk_family) {
1952 	case AF_INET:
1953 		if (nla_put_in_addr(skb, MPTCP_ATTR_SADDR4, issk->inet_saddr))
1954 			return -EMSGSIZE;
1955 		if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, issk->inet_daddr))
1956 			return -EMSGSIZE;
1957 		break;
1958 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1959 	case AF_INET6: {
1960 		const struct ipv6_pinfo *np = inet6_sk(ssk);
1961 
1962 		if (nla_put_in6_addr(skb, MPTCP_ATTR_SADDR6, &np->saddr))
1963 			return -EMSGSIZE;
1964 		if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &ssk->sk_v6_daddr))
1965 			return -EMSGSIZE;
1966 		break;
1967 	}
1968 #endif
1969 	default:
1970 		WARN_ON_ONCE(1);
1971 		return -EMSGSIZE;
1972 	}
1973 
1974 	if (nla_put_be16(skb, MPTCP_ATTR_SPORT, issk->inet_sport))
1975 		return -EMSGSIZE;
1976 	if (nla_put_be16(skb, MPTCP_ATTR_DPORT, issk->inet_dport))
1977 		return -EMSGSIZE;
1978 
1979 	sf = mptcp_subflow_ctx(ssk);
1980 	if (WARN_ON_ONCE(!sf))
1981 		return -EINVAL;
1982 
1983 	if (nla_put_u8(skb, MPTCP_ATTR_LOC_ID, sf->local_id))
1984 		return -EMSGSIZE;
1985 
1986 	if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, sf->remote_id))
1987 		return -EMSGSIZE;
1988 
1989 	return 0;
1990 }
1991 
1992 static int mptcp_event_put_token_and_ssk(struct sk_buff *skb,
1993 					 const struct mptcp_sock *msk,
1994 					 const struct sock *ssk)
1995 {
1996 	const struct sock *sk = (const struct sock *)msk;
1997 	const struct mptcp_subflow_context *sf;
1998 	u8 sk_err;
1999 
2000 	if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
2001 		return -EMSGSIZE;
2002 
2003 	if (mptcp_event_add_subflow(skb, ssk))
2004 		return -EMSGSIZE;
2005 
2006 	sf = mptcp_subflow_ctx(ssk);
2007 	if (WARN_ON_ONCE(!sf))
2008 		return -EINVAL;
2009 
2010 	if (nla_put_u8(skb, MPTCP_ATTR_BACKUP, sf->backup))
2011 		return -EMSGSIZE;
2012 
2013 	if (ssk->sk_bound_dev_if &&
2014 	    nla_put_s32(skb, MPTCP_ATTR_IF_IDX, ssk->sk_bound_dev_if))
2015 		return -EMSGSIZE;
2016 
2017 	sk_err = ssk->sk_err;
2018 	if (sk_err && sk->sk_state == TCP_ESTABLISHED &&
2019 	    nla_put_u8(skb, MPTCP_ATTR_ERROR, sk_err))
2020 		return -EMSGSIZE;
2021 
2022 	return 0;
2023 }
2024 
2025 static int mptcp_event_sub_established(struct sk_buff *skb,
2026 				       const struct mptcp_sock *msk,
2027 				       const struct sock *ssk)
2028 {
2029 	return mptcp_event_put_token_and_ssk(skb, msk, ssk);
2030 }
2031 
2032 static int mptcp_event_sub_closed(struct sk_buff *skb,
2033 				  const struct mptcp_sock *msk,
2034 				  const struct sock *ssk)
2035 {
2036 	const struct mptcp_subflow_context *sf;
2037 
2038 	if (mptcp_event_put_token_and_ssk(skb, msk, ssk))
2039 		return -EMSGSIZE;
2040 
2041 	sf = mptcp_subflow_ctx(ssk);
2042 	if (!sf->reset_seen)
2043 		return 0;
2044 
2045 	if (nla_put_u32(skb, MPTCP_ATTR_RESET_REASON, sf->reset_reason))
2046 		return -EMSGSIZE;
2047 
2048 	if (nla_put_u32(skb, MPTCP_ATTR_RESET_FLAGS, sf->reset_transient))
2049 		return -EMSGSIZE;
2050 
2051 	return 0;
2052 }
2053 
2054 static int mptcp_event_created(struct sk_buff *skb,
2055 			       const struct mptcp_sock *msk,
2056 			       const struct sock *ssk)
2057 {
2058 	int err = nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token);
2059 
2060 	if (err)
2061 		return err;
2062 
2063 	if (nla_put_u8(skb, MPTCP_ATTR_SERVER_SIDE, READ_ONCE(msk->pm.server_side)))
2064 		return -EMSGSIZE;
2065 
2066 	return mptcp_event_add_subflow(skb, ssk);
2067 }
2068 
2069 void mptcp_event_addr_removed(const struct mptcp_sock *msk, uint8_t id)
2070 {
2071 	struct net *net = sock_net((const struct sock *)msk);
2072 	struct nlmsghdr *nlh;
2073 	struct sk_buff *skb;
2074 
2075 	if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
2076 		return;
2077 
2078 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2079 	if (!skb)
2080 		return;
2081 
2082 	nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, MPTCP_EVENT_REMOVED);
2083 	if (!nlh)
2084 		goto nla_put_failure;
2085 
2086 	if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
2087 		goto nla_put_failure;
2088 
2089 	if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, id))
2090 		goto nla_put_failure;
2091 
2092 	genlmsg_end(skb, nlh);
2093 	mptcp_nl_mcast_send(net, skb, GFP_ATOMIC);
2094 	return;
2095 
2096 nla_put_failure:
2097 	nlmsg_free(skb);
2098 }
2099 
2100 void mptcp_event_addr_announced(const struct sock *ssk,
2101 				const struct mptcp_addr_info *info)
2102 {
2103 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
2104 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
2105 	struct net *net = sock_net(ssk);
2106 	struct nlmsghdr *nlh;
2107 	struct sk_buff *skb;
2108 
2109 	if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
2110 		return;
2111 
2112 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2113 	if (!skb)
2114 		return;
2115 
2116 	nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0,
2117 			  MPTCP_EVENT_ANNOUNCED);
2118 	if (!nlh)
2119 		goto nla_put_failure;
2120 
2121 	if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
2122 		goto nla_put_failure;
2123 
2124 	if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, info->id))
2125 		goto nla_put_failure;
2126 
2127 	if (nla_put_be16(skb, MPTCP_ATTR_DPORT,
2128 			 info->port == 0 ?
2129 			 inet_sk(ssk)->inet_dport :
2130 			 info->port))
2131 		goto nla_put_failure;
2132 
2133 	switch (info->family) {
2134 	case AF_INET:
2135 		if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, info->addr.s_addr))
2136 			goto nla_put_failure;
2137 		break;
2138 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2139 	case AF_INET6:
2140 		if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &info->addr6))
2141 			goto nla_put_failure;
2142 		break;
2143 #endif
2144 	default:
2145 		WARN_ON_ONCE(1);
2146 		goto nla_put_failure;
2147 	}
2148 
2149 	genlmsg_end(skb, nlh);
2150 	mptcp_nl_mcast_send(net, skb, GFP_ATOMIC);
2151 	return;
2152 
2153 nla_put_failure:
2154 	nlmsg_free(skb);
2155 }
2156 
2157 void mptcp_event_pm_listener(const struct sock *ssk,
2158 			     enum mptcp_event_type event)
2159 {
2160 	const struct inet_sock *issk = inet_sk(ssk);
2161 	struct net *net = sock_net(ssk);
2162 	struct nlmsghdr *nlh;
2163 	struct sk_buff *skb;
2164 
2165 	if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
2166 		return;
2167 
2168 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2169 	if (!skb)
2170 		return;
2171 
2172 	nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, event);
2173 	if (!nlh)
2174 		goto nla_put_failure;
2175 
2176 	if (nla_put_u16(skb, MPTCP_ATTR_FAMILY, ssk->sk_family))
2177 		goto nla_put_failure;
2178 
2179 	if (nla_put_be16(skb, MPTCP_ATTR_SPORT, issk->inet_sport))
2180 		goto nla_put_failure;
2181 
2182 	switch (ssk->sk_family) {
2183 	case AF_INET:
2184 		if (nla_put_in_addr(skb, MPTCP_ATTR_SADDR4, issk->inet_saddr))
2185 			goto nla_put_failure;
2186 		break;
2187 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2188 	case AF_INET6: {
2189 		const struct ipv6_pinfo *np = inet6_sk(ssk);
2190 
2191 		if (nla_put_in6_addr(skb, MPTCP_ATTR_SADDR6, &np->saddr))
2192 			goto nla_put_failure;
2193 		break;
2194 	}
2195 #endif
2196 	default:
2197 		WARN_ON_ONCE(1);
2198 		goto nla_put_failure;
2199 	}
2200 
2201 	genlmsg_end(skb, nlh);
2202 	mptcp_nl_mcast_send(net, skb, GFP_KERNEL);
2203 	return;
2204 
2205 nla_put_failure:
2206 	nlmsg_free(skb);
2207 }
2208 
2209 void mptcp_event(enum mptcp_event_type type, const struct mptcp_sock *msk,
2210 		 const struct sock *ssk, gfp_t gfp)
2211 {
2212 	struct net *net = sock_net((const struct sock *)msk);
2213 	struct nlmsghdr *nlh;
2214 	struct sk_buff *skb;
2215 
2216 	if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
2217 		return;
2218 
2219 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
2220 	if (!skb)
2221 		return;
2222 
2223 	nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, type);
2224 	if (!nlh)
2225 		goto nla_put_failure;
2226 
2227 	switch (type) {
2228 	case MPTCP_EVENT_UNSPEC:
2229 		WARN_ON_ONCE(1);
2230 		break;
2231 	case MPTCP_EVENT_CREATED:
2232 	case MPTCP_EVENT_ESTABLISHED:
2233 		if (mptcp_event_created(skb, msk, ssk) < 0)
2234 			goto nla_put_failure;
2235 		break;
2236 	case MPTCP_EVENT_CLOSED:
2237 		if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token) < 0)
2238 			goto nla_put_failure;
2239 		break;
2240 	case MPTCP_EVENT_ANNOUNCED:
2241 	case MPTCP_EVENT_REMOVED:
2242 		/* call mptcp_event_addr_announced()/removed instead */
2243 		WARN_ON_ONCE(1);
2244 		break;
2245 	case MPTCP_EVENT_SUB_ESTABLISHED:
2246 	case MPTCP_EVENT_SUB_PRIORITY:
2247 		if (mptcp_event_sub_established(skb, msk, ssk) < 0)
2248 			goto nla_put_failure;
2249 		break;
2250 	case MPTCP_EVENT_SUB_CLOSED:
2251 		if (mptcp_event_sub_closed(skb, msk, ssk) < 0)
2252 			goto nla_put_failure;
2253 		break;
2254 	case MPTCP_EVENT_LISTENER_CREATED:
2255 	case MPTCP_EVENT_LISTENER_CLOSED:
2256 		break;
2257 	}
2258 
2259 	genlmsg_end(skb, nlh);
2260 	mptcp_nl_mcast_send(net, skb, gfp);
2261 	return;
2262 
2263 nla_put_failure:
2264 	nlmsg_free(skb);
2265 }
2266 
2267 static const struct genl_small_ops mptcp_pm_ops[] = {
2268 	{
2269 		.cmd    = MPTCP_PM_CMD_ADD_ADDR,
2270 		.doit   = mptcp_nl_cmd_add_addr,
2271 		.flags  = GENL_UNS_ADMIN_PERM,
2272 	},
2273 	{
2274 		.cmd    = MPTCP_PM_CMD_DEL_ADDR,
2275 		.doit   = mptcp_nl_cmd_del_addr,
2276 		.flags  = GENL_UNS_ADMIN_PERM,
2277 	},
2278 	{
2279 		.cmd    = MPTCP_PM_CMD_FLUSH_ADDRS,
2280 		.doit   = mptcp_nl_cmd_flush_addrs,
2281 		.flags  = GENL_UNS_ADMIN_PERM,
2282 	},
2283 	{
2284 		.cmd    = MPTCP_PM_CMD_GET_ADDR,
2285 		.doit   = mptcp_nl_cmd_get_addr,
2286 		.dumpit   = mptcp_nl_cmd_dump_addrs,
2287 	},
2288 	{
2289 		.cmd    = MPTCP_PM_CMD_SET_LIMITS,
2290 		.doit   = mptcp_nl_cmd_set_limits,
2291 		.flags  = GENL_UNS_ADMIN_PERM,
2292 	},
2293 	{
2294 		.cmd    = MPTCP_PM_CMD_GET_LIMITS,
2295 		.doit   = mptcp_nl_cmd_get_limits,
2296 	},
2297 	{
2298 		.cmd    = MPTCP_PM_CMD_SET_FLAGS,
2299 		.doit   = mptcp_nl_cmd_set_flags,
2300 		.flags  = GENL_UNS_ADMIN_PERM,
2301 	},
2302 	{
2303 		.cmd    = MPTCP_PM_CMD_ANNOUNCE,
2304 		.doit   = mptcp_nl_cmd_announce,
2305 		.flags  = GENL_UNS_ADMIN_PERM,
2306 	},
2307 	{
2308 		.cmd    = MPTCP_PM_CMD_REMOVE,
2309 		.doit   = mptcp_nl_cmd_remove,
2310 		.flags  = GENL_UNS_ADMIN_PERM,
2311 	},
2312 	{
2313 		.cmd    = MPTCP_PM_CMD_SUBFLOW_CREATE,
2314 		.doit   = mptcp_nl_cmd_sf_create,
2315 		.flags  = GENL_UNS_ADMIN_PERM,
2316 	},
2317 	{
2318 		.cmd    = MPTCP_PM_CMD_SUBFLOW_DESTROY,
2319 		.doit   = mptcp_nl_cmd_sf_destroy,
2320 		.flags  = GENL_UNS_ADMIN_PERM,
2321 	},
2322 };
2323 
2324 static struct genl_family mptcp_genl_family __ro_after_init = {
2325 	.name		= MPTCP_PM_NAME,
2326 	.version	= MPTCP_PM_VER,
2327 	.maxattr	= MPTCP_PM_ATTR_MAX,
2328 	.policy		= mptcp_pm_policy,
2329 	.netnsok	= true,
2330 	.module		= THIS_MODULE,
2331 	.small_ops	= mptcp_pm_ops,
2332 	.n_small_ops	= ARRAY_SIZE(mptcp_pm_ops),
2333 	.resv_start_op	= MPTCP_PM_CMD_SUBFLOW_DESTROY + 1,
2334 	.mcgrps		= mptcp_pm_mcgrps,
2335 	.n_mcgrps	= ARRAY_SIZE(mptcp_pm_mcgrps),
2336 };
2337 
2338 static int __net_init pm_nl_init_net(struct net *net)
2339 {
2340 	struct pm_nl_pernet *pernet = pm_nl_get_pernet(net);
2341 
2342 	INIT_LIST_HEAD_RCU(&pernet->local_addr_list);
2343 
2344 	/* Cit. 2 subflows ought to be enough for anybody. */
2345 	pernet->subflows_max = 2;
2346 	pernet->next_id = 1;
2347 	pernet->stale_loss_cnt = 4;
2348 	spin_lock_init(&pernet->lock);
2349 
2350 	/* No need to initialize other pernet fields, the struct is zeroed at
2351 	 * allocation time.
2352 	 */
2353 
2354 	return 0;
2355 }
2356 
2357 static void __net_exit pm_nl_exit_net(struct list_head *net_list)
2358 {
2359 	struct net *net;
2360 
2361 	list_for_each_entry(net, net_list, exit_list) {
2362 		struct pm_nl_pernet *pernet = pm_nl_get_pernet(net);
2363 
2364 		/* net is removed from namespace list, can't race with
2365 		 * other modifiers, also netns core already waited for a
2366 		 * RCU grace period.
2367 		 */
2368 		__flush_addrs(&pernet->local_addr_list);
2369 	}
2370 }
2371 
2372 static struct pernet_operations mptcp_pm_pernet_ops = {
2373 	.init = pm_nl_init_net,
2374 	.exit_batch = pm_nl_exit_net,
2375 	.id = &pm_nl_pernet_id,
2376 	.size = sizeof(struct pm_nl_pernet),
2377 };
2378 
2379 void __init mptcp_pm_nl_init(void)
2380 {
2381 	if (register_pernet_subsys(&mptcp_pm_pernet_ops) < 0)
2382 		panic("Failed to register MPTCP PM pernet subsystem.\n");
2383 
2384 	if (genl_register_family(&mptcp_genl_family))
2385 		panic("Failed to register MPTCP PM netlink family\n");
2386 }
2387