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