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