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