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