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