xref: /linux/net/mptcp/pm_netlink.c (revision c7546e2c3cb739a3c1a2f5acaf9bb629d401afe5)
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 (inet_sk_state_load(ssk) == TCP_CLOSE)
864 				continue;
865 			if (rm_type == MPTCP_MIB_RMADDR && remote_id != rm_id)
866 				continue;
867 			if (rm_type == MPTCP_MIB_RMSUBFLOW && id != rm_id)
868 				continue;
869 
870 			pr_debug(" -> %s rm_list_ids[%d]=%u local_id=%u remote_id=%u mpc_id=%u\n",
871 				 rm_type == MPTCP_MIB_RMADDR ? "address" : "subflow",
872 				 i, rm_id, id, remote_id, msk->mpc_endpoint_id);
873 			spin_unlock_bh(&msk->pm.lock);
874 			mptcp_subflow_shutdown(sk, ssk, how);
875 
876 			/* the following takes care of updating the subflows counter */
877 			mptcp_close_ssk(sk, ssk, subflow);
878 			spin_lock_bh(&msk->pm.lock);
879 
880 			removed |= subflow->request_join;
881 			if (rm_type == MPTCP_MIB_RMSUBFLOW)
882 				__MPTCP_INC_STATS(sock_net(sk), rm_type);
883 		}
884 
885 		if (rm_type == MPTCP_MIB_RMADDR)
886 			__MPTCP_INC_STATS(sock_net(sk), rm_type);
887 
888 		if (!removed)
889 			continue;
890 
891 		if (!mptcp_pm_is_kernel(msk))
892 			continue;
893 
894 		if (rm_type == MPTCP_MIB_RMADDR && rm_id &&
895 		    !WARN_ON_ONCE(msk->pm.add_addr_accepted == 0)) {
896 			/* Note: if the subflow has been closed before, this
897 			 * add_addr_accepted counter will not be decremented.
898 			 */
899 			if (--msk->pm.add_addr_accepted < mptcp_pm_get_add_addr_accept_max(msk))
900 				WRITE_ONCE(msk->pm.accept_addr, true);
901 		}
902 	}
903 }
904 
905 static void mptcp_pm_nl_rm_addr_received(struct mptcp_sock *msk)
906 {
907 	mptcp_pm_nl_rm_addr_or_subflow(msk, &msk->pm.rm_list_rx, MPTCP_MIB_RMADDR);
908 }
909 
910 static void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk,
911 					    const struct mptcp_rm_list *rm_list)
912 {
913 	mptcp_pm_nl_rm_addr_or_subflow(msk, rm_list, MPTCP_MIB_RMSUBFLOW);
914 }
915 
916 void mptcp_pm_nl_work(struct mptcp_sock *msk)
917 {
918 	struct mptcp_pm_data *pm = &msk->pm;
919 
920 	msk_owned_by_me(msk);
921 
922 	if (!(pm->status & MPTCP_PM_WORK_MASK))
923 		return;
924 
925 	spin_lock_bh(&msk->pm.lock);
926 
927 	pr_debug("msk=%p status=%x\n", msk, pm->status);
928 	if (pm->status & BIT(MPTCP_PM_ADD_ADDR_RECEIVED)) {
929 		pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_RECEIVED);
930 		mptcp_pm_nl_add_addr_received(msk);
931 	}
932 	if (pm->status & BIT(MPTCP_PM_ADD_ADDR_SEND_ACK)) {
933 		pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_SEND_ACK);
934 		mptcp_pm_nl_addr_send_ack(msk);
935 	}
936 	if (pm->status & BIT(MPTCP_PM_RM_ADDR_RECEIVED)) {
937 		pm->status &= ~BIT(MPTCP_PM_RM_ADDR_RECEIVED);
938 		mptcp_pm_nl_rm_addr_received(msk);
939 	}
940 	if (pm->status & BIT(MPTCP_PM_ESTABLISHED)) {
941 		pm->status &= ~BIT(MPTCP_PM_ESTABLISHED);
942 		mptcp_pm_nl_fully_established(msk);
943 	}
944 	if (pm->status & BIT(MPTCP_PM_SUBFLOW_ESTABLISHED)) {
945 		pm->status &= ~BIT(MPTCP_PM_SUBFLOW_ESTABLISHED);
946 		mptcp_pm_nl_subflow_established(msk);
947 	}
948 
949 	spin_unlock_bh(&msk->pm.lock);
950 }
951 
952 static bool address_use_port(struct mptcp_pm_addr_entry *entry)
953 {
954 	return (entry->flags &
955 		(MPTCP_PM_ADDR_FLAG_SIGNAL | MPTCP_PM_ADDR_FLAG_SUBFLOW)) ==
956 		MPTCP_PM_ADDR_FLAG_SIGNAL;
957 }
958 
959 /* caller must ensure the RCU grace period is already elapsed */
960 static void __mptcp_pm_release_addr_entry(struct mptcp_pm_addr_entry *entry)
961 {
962 	if (entry->lsk)
963 		sock_release(entry->lsk);
964 	kfree(entry);
965 }
966 
967 static int mptcp_pm_nl_append_new_local_addr(struct pm_nl_pernet *pernet,
968 					     struct mptcp_pm_addr_entry *entry,
969 					     bool needs_id)
970 {
971 	struct mptcp_pm_addr_entry *cur, *del_entry = NULL;
972 	unsigned int addr_max;
973 	int ret = -EINVAL;
974 
975 	spin_lock_bh(&pernet->lock);
976 	/* to keep the code simple, don't do IDR-like allocation for address ID,
977 	 * just bail when we exceed limits
978 	 */
979 	if (pernet->next_id == MPTCP_PM_MAX_ADDR_ID)
980 		pernet->next_id = 1;
981 	if (pernet->addrs >= MPTCP_PM_ADDR_MAX) {
982 		ret = -ERANGE;
983 		goto out;
984 	}
985 	if (test_bit(entry->addr.id, pernet->id_bitmap)) {
986 		ret = -EBUSY;
987 		goto out;
988 	}
989 
990 	/* do not insert duplicate address, differentiate on port only
991 	 * singled addresses
992 	 */
993 	if (!address_use_port(entry))
994 		entry->addr.port = 0;
995 	list_for_each_entry(cur, &pernet->local_addr_list, list) {
996 		if (mptcp_addresses_equal(&cur->addr, &entry->addr,
997 					  cur->addr.port || entry->addr.port)) {
998 			/* allow replacing the exiting endpoint only if such
999 			 * endpoint is an implicit one and the user-space
1000 			 * did not provide an endpoint id
1001 			 */
1002 			if (!(cur->flags & MPTCP_PM_ADDR_FLAG_IMPLICIT)) {
1003 				ret = -EEXIST;
1004 				goto out;
1005 			}
1006 			if (entry->addr.id)
1007 				goto out;
1008 
1009 			pernet->addrs--;
1010 			entry->addr.id = cur->addr.id;
1011 			list_del_rcu(&cur->list);
1012 			del_entry = cur;
1013 			break;
1014 		}
1015 	}
1016 
1017 	if (!entry->addr.id && needs_id) {
1018 find_next:
1019 		entry->addr.id = find_next_zero_bit(pernet->id_bitmap,
1020 						    MPTCP_PM_MAX_ADDR_ID + 1,
1021 						    pernet->next_id);
1022 		if (!entry->addr.id && pernet->next_id != 1) {
1023 			pernet->next_id = 1;
1024 			goto find_next;
1025 		}
1026 	}
1027 
1028 	if (!entry->addr.id && needs_id)
1029 		goto out;
1030 
1031 	__set_bit(entry->addr.id, pernet->id_bitmap);
1032 	if (entry->addr.id > pernet->next_id)
1033 		pernet->next_id = entry->addr.id;
1034 
1035 	if (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
1036 		addr_max = pernet->add_addr_signal_max;
1037 		WRITE_ONCE(pernet->add_addr_signal_max, addr_max + 1);
1038 	}
1039 	if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) {
1040 		addr_max = pernet->local_addr_max;
1041 		WRITE_ONCE(pernet->local_addr_max, addr_max + 1);
1042 	}
1043 
1044 	pernet->addrs++;
1045 	if (!entry->addr.port)
1046 		list_add_tail_rcu(&entry->list, &pernet->local_addr_list);
1047 	else
1048 		list_add_rcu(&entry->list, &pernet->local_addr_list);
1049 	ret = entry->addr.id;
1050 
1051 out:
1052 	spin_unlock_bh(&pernet->lock);
1053 
1054 	/* just replaced an existing entry, free it */
1055 	if (del_entry) {
1056 		synchronize_rcu();
1057 		__mptcp_pm_release_addr_entry(del_entry);
1058 	}
1059 	return ret;
1060 }
1061 
1062 static struct lock_class_key mptcp_slock_keys[2];
1063 static struct lock_class_key mptcp_keys[2];
1064 
1065 static int mptcp_pm_nl_create_listen_socket(struct sock *sk,
1066 					    struct mptcp_pm_addr_entry *entry)
1067 {
1068 	bool is_ipv6 = sk->sk_family == AF_INET6;
1069 	int addrlen = sizeof(struct sockaddr_in);
1070 	struct sockaddr_storage addr;
1071 	struct sock *newsk, *ssk;
1072 	int backlog = 1024;
1073 	int err;
1074 
1075 	err = sock_create_kern(sock_net(sk), entry->addr.family,
1076 			       SOCK_STREAM, IPPROTO_MPTCP, &entry->lsk);
1077 	if (err)
1078 		return err;
1079 
1080 	newsk = entry->lsk->sk;
1081 	if (!newsk)
1082 		return -EINVAL;
1083 
1084 	/* The subflow socket lock is acquired in a nested to the msk one
1085 	 * in several places, even by the TCP stack, and this msk is a kernel
1086 	 * socket: lockdep complains. Instead of propagating the _nested
1087 	 * modifiers in several places, re-init the lock class for the msk
1088 	 * socket to an mptcp specific one.
1089 	 */
1090 	sock_lock_init_class_and_name(newsk,
1091 				      is_ipv6 ? "mlock-AF_INET6" : "mlock-AF_INET",
1092 				      &mptcp_slock_keys[is_ipv6],
1093 				      is_ipv6 ? "msk_lock-AF_INET6" : "msk_lock-AF_INET",
1094 				      &mptcp_keys[is_ipv6]);
1095 
1096 	lock_sock(newsk);
1097 	ssk = __mptcp_nmpc_sk(mptcp_sk(newsk));
1098 	release_sock(newsk);
1099 	if (IS_ERR(ssk))
1100 		return PTR_ERR(ssk);
1101 
1102 	mptcp_info2sockaddr(&entry->addr, &addr, entry->addr.family);
1103 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1104 	if (entry->addr.family == AF_INET6)
1105 		addrlen = sizeof(struct sockaddr_in6);
1106 #endif
1107 	if (ssk->sk_family == AF_INET)
1108 		err = inet_bind_sk(ssk, (struct sockaddr *)&addr, addrlen);
1109 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1110 	else if (ssk->sk_family == AF_INET6)
1111 		err = inet6_bind_sk(ssk, (struct sockaddr *)&addr, addrlen);
1112 #endif
1113 	if (err)
1114 		return err;
1115 
1116 	/* We don't use mptcp_set_state() here because it needs to be called
1117 	 * under the msk socket lock. For the moment, that will not bring
1118 	 * anything more than only calling inet_sk_state_store(), because the
1119 	 * old status is known (TCP_CLOSE).
1120 	 */
1121 	inet_sk_state_store(newsk, TCP_LISTEN);
1122 	lock_sock(ssk);
1123 	err = __inet_listen_sk(ssk, backlog);
1124 	if (!err)
1125 		mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CREATED);
1126 	release_sock(ssk);
1127 	return err;
1128 }
1129 
1130 int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct mptcp_addr_info *skc)
1131 {
1132 	struct mptcp_pm_addr_entry *entry;
1133 	struct pm_nl_pernet *pernet;
1134 	int ret = -1;
1135 
1136 	pernet = pm_nl_get_pernet_from_msk(msk);
1137 
1138 	rcu_read_lock();
1139 	list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
1140 		if (mptcp_addresses_equal(&entry->addr, skc, entry->addr.port)) {
1141 			ret = entry->addr.id;
1142 			break;
1143 		}
1144 	}
1145 	rcu_read_unlock();
1146 	if (ret >= 0)
1147 		return ret;
1148 
1149 	/* address not found, add to local list */
1150 	entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
1151 	if (!entry)
1152 		return -ENOMEM;
1153 
1154 	entry->addr = *skc;
1155 	entry->addr.id = 0;
1156 	entry->addr.port = 0;
1157 	entry->ifindex = 0;
1158 	entry->flags = MPTCP_PM_ADDR_FLAG_IMPLICIT;
1159 	entry->lsk = NULL;
1160 	ret = mptcp_pm_nl_append_new_local_addr(pernet, entry, true);
1161 	if (ret < 0)
1162 		kfree(entry);
1163 
1164 	return ret;
1165 }
1166 
1167 bool mptcp_pm_nl_is_backup(struct mptcp_sock *msk, struct mptcp_addr_info *skc)
1168 {
1169 	struct pm_nl_pernet *pernet = pm_nl_get_pernet_from_msk(msk);
1170 	struct mptcp_pm_addr_entry *entry;
1171 	bool backup = false;
1172 
1173 	rcu_read_lock();
1174 	list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
1175 		if (mptcp_addresses_equal(&entry->addr, skc, entry->addr.port)) {
1176 			backup = !!(entry->flags & MPTCP_PM_ADDR_FLAG_BACKUP);
1177 			break;
1178 		}
1179 	}
1180 	rcu_read_unlock();
1181 
1182 	return backup;
1183 }
1184 
1185 #define MPTCP_PM_CMD_GRP_OFFSET       0
1186 #define MPTCP_PM_EV_GRP_OFFSET        1
1187 
1188 static const struct genl_multicast_group mptcp_pm_mcgrps[] = {
1189 	[MPTCP_PM_CMD_GRP_OFFSET]	= { .name = MPTCP_PM_CMD_GRP_NAME, },
1190 	[MPTCP_PM_EV_GRP_OFFSET]        = { .name = MPTCP_PM_EV_GRP_NAME,
1191 					    .flags = GENL_MCAST_CAP_NET_ADMIN,
1192 					  },
1193 };
1194 
1195 void mptcp_pm_nl_subflow_chk_stale(const struct mptcp_sock *msk, struct sock *ssk)
1196 {
1197 	struct mptcp_subflow_context *iter, *subflow = mptcp_subflow_ctx(ssk);
1198 	struct sock *sk = (struct sock *)msk;
1199 	unsigned int active_max_loss_cnt;
1200 	struct net *net = sock_net(sk);
1201 	unsigned int stale_loss_cnt;
1202 	bool slow;
1203 
1204 	stale_loss_cnt = mptcp_stale_loss_cnt(net);
1205 	if (subflow->stale || !stale_loss_cnt || subflow->stale_count <= stale_loss_cnt)
1206 		return;
1207 
1208 	/* look for another available subflow not in loss state */
1209 	active_max_loss_cnt = max_t(int, stale_loss_cnt - 1, 1);
1210 	mptcp_for_each_subflow(msk, iter) {
1211 		if (iter != subflow && mptcp_subflow_active(iter) &&
1212 		    iter->stale_count < active_max_loss_cnt) {
1213 			/* we have some alternatives, try to mark this subflow as idle ...*/
1214 			slow = lock_sock_fast(ssk);
1215 			if (!tcp_rtx_and_write_queues_empty(ssk)) {
1216 				subflow->stale = 1;
1217 				__mptcp_retransmit_pending_data(sk);
1218 				MPTCP_INC_STATS(net, MPTCP_MIB_SUBFLOWSTALE);
1219 			}
1220 			unlock_sock_fast(ssk, slow);
1221 
1222 			/* always try to push the pending data regardless of re-injections:
1223 			 * we can possibly use backup subflows now, and subflow selection
1224 			 * is cheap under the msk socket lock
1225 			 */
1226 			__mptcp_push_pending(sk, 0);
1227 			return;
1228 		}
1229 	}
1230 }
1231 
1232 static int mptcp_pm_family_to_addr(int family)
1233 {
1234 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1235 	if (family == AF_INET6)
1236 		return MPTCP_PM_ADDR_ATTR_ADDR6;
1237 #endif
1238 	return MPTCP_PM_ADDR_ATTR_ADDR4;
1239 }
1240 
1241 static int mptcp_pm_parse_pm_addr_attr(struct nlattr *tb[],
1242 				       const struct nlattr *attr,
1243 				       struct genl_info *info,
1244 				       struct mptcp_addr_info *addr,
1245 				       bool require_family)
1246 {
1247 	int err, addr_addr;
1248 
1249 	if (!attr) {
1250 		GENL_SET_ERR_MSG(info, "missing address info");
1251 		return -EINVAL;
1252 	}
1253 
1254 	/* no validation needed - was already done via nested policy */
1255 	err = nla_parse_nested_deprecated(tb, MPTCP_PM_ADDR_ATTR_MAX, attr,
1256 					  mptcp_pm_address_nl_policy, info->extack);
1257 	if (err)
1258 		return err;
1259 
1260 	if (tb[MPTCP_PM_ADDR_ATTR_ID])
1261 		addr->id = nla_get_u8(tb[MPTCP_PM_ADDR_ATTR_ID]);
1262 
1263 	if (!tb[MPTCP_PM_ADDR_ATTR_FAMILY]) {
1264 		if (!require_family)
1265 			return 0;
1266 
1267 		NL_SET_ERR_MSG_ATTR(info->extack, attr,
1268 				    "missing family");
1269 		return -EINVAL;
1270 	}
1271 
1272 	addr->family = nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_FAMILY]);
1273 	if (addr->family != AF_INET
1274 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1275 	    && addr->family != AF_INET6
1276 #endif
1277 	    ) {
1278 		NL_SET_ERR_MSG_ATTR(info->extack, attr,
1279 				    "unknown address family");
1280 		return -EINVAL;
1281 	}
1282 	addr_addr = mptcp_pm_family_to_addr(addr->family);
1283 	if (!tb[addr_addr]) {
1284 		NL_SET_ERR_MSG_ATTR(info->extack, attr,
1285 				    "missing address data");
1286 		return -EINVAL;
1287 	}
1288 
1289 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1290 	if (addr->family == AF_INET6)
1291 		addr->addr6 = nla_get_in6_addr(tb[addr_addr]);
1292 	else
1293 #endif
1294 		addr->addr.s_addr = nla_get_in_addr(tb[addr_addr]);
1295 
1296 	if (tb[MPTCP_PM_ADDR_ATTR_PORT])
1297 		addr->port = htons(nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_PORT]));
1298 
1299 	return 0;
1300 }
1301 
1302 int mptcp_pm_parse_addr(struct nlattr *attr, struct genl_info *info,
1303 			struct mptcp_addr_info *addr)
1304 {
1305 	struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1];
1306 
1307 	memset(addr, 0, sizeof(*addr));
1308 
1309 	return mptcp_pm_parse_pm_addr_attr(tb, attr, info, addr, true);
1310 }
1311 
1312 int mptcp_pm_parse_entry(struct nlattr *attr, struct genl_info *info,
1313 			 bool require_family,
1314 			 struct mptcp_pm_addr_entry *entry)
1315 {
1316 	struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1];
1317 	int err;
1318 
1319 	memset(entry, 0, sizeof(*entry));
1320 
1321 	err = mptcp_pm_parse_pm_addr_attr(tb, attr, info, &entry->addr, require_family);
1322 	if (err)
1323 		return err;
1324 
1325 	if (tb[MPTCP_PM_ADDR_ATTR_IF_IDX]) {
1326 		u32 val = nla_get_s32(tb[MPTCP_PM_ADDR_ATTR_IF_IDX]);
1327 
1328 		entry->ifindex = val;
1329 	}
1330 
1331 	if (tb[MPTCP_PM_ADDR_ATTR_FLAGS])
1332 		entry->flags = nla_get_u32(tb[MPTCP_PM_ADDR_ATTR_FLAGS]);
1333 
1334 	if (tb[MPTCP_PM_ADDR_ATTR_PORT])
1335 		entry->addr.port = htons(nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_PORT]));
1336 
1337 	return 0;
1338 }
1339 
1340 static struct pm_nl_pernet *genl_info_pm_nl(struct genl_info *info)
1341 {
1342 	return pm_nl_get_pernet(genl_info_net(info));
1343 }
1344 
1345 static int mptcp_nl_add_subflow_or_signal_addr(struct net *net,
1346 					       struct mptcp_addr_info *addr)
1347 {
1348 	struct mptcp_sock *msk;
1349 	long s_slot = 0, s_num = 0;
1350 
1351 	while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1352 		struct sock *sk = (struct sock *)msk;
1353 		struct mptcp_addr_info mpc_addr;
1354 
1355 		if (!READ_ONCE(msk->fully_established) ||
1356 		    mptcp_pm_is_userspace(msk))
1357 			goto next;
1358 
1359 		/* if the endp linked to the init sf is re-added with a != ID */
1360 		mptcp_local_address((struct sock_common *)msk, &mpc_addr);
1361 
1362 		lock_sock(sk);
1363 		spin_lock_bh(&msk->pm.lock);
1364 		if (mptcp_addresses_equal(addr, &mpc_addr, addr->port))
1365 			msk->mpc_endpoint_id = addr->id;
1366 		mptcp_pm_create_subflow_or_signal_addr(msk);
1367 		spin_unlock_bh(&msk->pm.lock);
1368 		release_sock(sk);
1369 
1370 next:
1371 		sock_put(sk);
1372 		cond_resched();
1373 	}
1374 
1375 	return 0;
1376 }
1377 
1378 static bool mptcp_pm_has_addr_attr_id(const struct nlattr *attr,
1379 				      struct genl_info *info)
1380 {
1381 	struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1];
1382 
1383 	if (!nla_parse_nested_deprecated(tb, MPTCP_PM_ADDR_ATTR_MAX, attr,
1384 					 mptcp_pm_address_nl_policy, info->extack) &&
1385 	    tb[MPTCP_PM_ADDR_ATTR_ID])
1386 		return true;
1387 	return false;
1388 }
1389 
1390 int mptcp_pm_nl_add_addr_doit(struct sk_buff *skb, struct genl_info *info)
1391 {
1392 	struct nlattr *attr = info->attrs[MPTCP_PM_ENDPOINT_ADDR];
1393 	struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1394 	struct mptcp_pm_addr_entry addr, *entry;
1395 	int ret;
1396 
1397 	ret = mptcp_pm_parse_entry(attr, info, true, &addr);
1398 	if (ret < 0)
1399 		return ret;
1400 
1401 	if (addr.addr.port && !address_use_port(&addr)) {
1402 		GENL_SET_ERR_MSG(info, "flags must have signal and not subflow when using port");
1403 		return -EINVAL;
1404 	}
1405 
1406 	if (addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL &&
1407 	    addr.flags & MPTCP_PM_ADDR_FLAG_FULLMESH) {
1408 		GENL_SET_ERR_MSG(info, "flags mustn't have both signal and fullmesh");
1409 		return -EINVAL;
1410 	}
1411 
1412 	if (addr.flags & MPTCP_PM_ADDR_FLAG_IMPLICIT) {
1413 		GENL_SET_ERR_MSG(info, "can't create IMPLICIT endpoint");
1414 		return -EINVAL;
1415 	}
1416 
1417 	entry = kzalloc(sizeof(*entry), GFP_KERNEL_ACCOUNT);
1418 	if (!entry) {
1419 		GENL_SET_ERR_MSG(info, "can't allocate addr");
1420 		return -ENOMEM;
1421 	}
1422 
1423 	*entry = addr;
1424 	if (entry->addr.port) {
1425 		ret = mptcp_pm_nl_create_listen_socket(skb->sk, entry);
1426 		if (ret) {
1427 			GENL_SET_ERR_MSG_FMT(info, "create listen socket error: %d", ret);
1428 			goto out_free;
1429 		}
1430 	}
1431 	ret = mptcp_pm_nl_append_new_local_addr(pernet, entry,
1432 						!mptcp_pm_has_addr_attr_id(attr, info));
1433 	if (ret < 0) {
1434 		GENL_SET_ERR_MSG_FMT(info, "too many addresses or duplicate one: %d", ret);
1435 		goto out_free;
1436 	}
1437 
1438 	mptcp_nl_add_subflow_or_signal_addr(sock_net(skb->sk), &entry->addr);
1439 	return 0;
1440 
1441 out_free:
1442 	__mptcp_pm_release_addr_entry(entry);
1443 	return ret;
1444 }
1445 
1446 static bool remove_anno_list_by_saddr(struct mptcp_sock *msk,
1447 				      const struct mptcp_addr_info *addr)
1448 {
1449 	struct mptcp_pm_add_entry *entry;
1450 
1451 	entry = mptcp_pm_del_add_timer(msk, addr, false);
1452 	if (entry) {
1453 		kfree(entry);
1454 		return true;
1455 	}
1456 
1457 	return false;
1458 }
1459 
1460 static u8 mptcp_endp_get_local_id(struct mptcp_sock *msk,
1461 				  const struct mptcp_addr_info *addr)
1462 {
1463 	return msk->mpc_endpoint_id == addr->id ? 0 : addr->id;
1464 }
1465 
1466 static bool mptcp_pm_remove_anno_addr(struct mptcp_sock *msk,
1467 				      const struct mptcp_addr_info *addr,
1468 				      bool force)
1469 {
1470 	struct mptcp_rm_list list = { .nr = 0 };
1471 	bool ret;
1472 
1473 	list.ids[list.nr++] = mptcp_endp_get_local_id(msk, addr);
1474 
1475 	ret = remove_anno_list_by_saddr(msk, addr);
1476 	if (ret || force) {
1477 		spin_lock_bh(&msk->pm.lock);
1478 		if (ret) {
1479 			__set_bit(addr->id, msk->pm.id_avail_bitmap);
1480 			msk->pm.add_addr_signaled--;
1481 		}
1482 		mptcp_pm_remove_addr(msk, &list);
1483 		spin_unlock_bh(&msk->pm.lock);
1484 	}
1485 	return ret;
1486 }
1487 
1488 static void __mark_subflow_endp_available(struct mptcp_sock *msk, u8 id)
1489 {
1490 	/* If it was marked as used, and not ID 0, decrement local_addr_used */
1491 	if (!__test_and_set_bit(id ? : msk->mpc_endpoint_id, msk->pm.id_avail_bitmap) &&
1492 	    id && !WARN_ON_ONCE(msk->pm.local_addr_used == 0))
1493 		msk->pm.local_addr_used--;
1494 }
1495 
1496 static int mptcp_nl_remove_subflow_and_signal_addr(struct net *net,
1497 						   const struct mptcp_pm_addr_entry *entry)
1498 {
1499 	const struct mptcp_addr_info *addr = &entry->addr;
1500 	struct mptcp_rm_list list = { .nr = 1 };
1501 	long s_slot = 0, s_num = 0;
1502 	struct mptcp_sock *msk;
1503 
1504 	pr_debug("remove_id=%d\n", addr->id);
1505 
1506 	while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1507 		struct sock *sk = (struct sock *)msk;
1508 		bool remove_subflow;
1509 
1510 		if (mptcp_pm_is_userspace(msk))
1511 			goto next;
1512 
1513 		if (list_empty(&msk->conn_list)) {
1514 			mptcp_pm_remove_anno_addr(msk, addr, false);
1515 			goto next;
1516 		}
1517 
1518 		lock_sock(sk);
1519 		remove_subflow = lookup_subflow_by_saddr(&msk->conn_list, addr);
1520 		mptcp_pm_remove_anno_addr(msk, addr, remove_subflow &&
1521 					  !(entry->flags & MPTCP_PM_ADDR_FLAG_IMPLICIT));
1522 
1523 		list.ids[0] = mptcp_endp_get_local_id(msk, addr);
1524 		if (remove_subflow) {
1525 			spin_lock_bh(&msk->pm.lock);
1526 			mptcp_pm_nl_rm_subflow_received(msk, &list);
1527 			spin_unlock_bh(&msk->pm.lock);
1528 		}
1529 
1530 		if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) {
1531 			spin_lock_bh(&msk->pm.lock);
1532 			__mark_subflow_endp_available(msk, list.ids[0]);
1533 			spin_unlock_bh(&msk->pm.lock);
1534 		}
1535 
1536 		if (msk->mpc_endpoint_id == entry->addr.id)
1537 			msk->mpc_endpoint_id = 0;
1538 		release_sock(sk);
1539 
1540 next:
1541 		sock_put(sk);
1542 		cond_resched();
1543 	}
1544 
1545 	return 0;
1546 }
1547 
1548 static int mptcp_nl_remove_id_zero_address(struct net *net,
1549 					   struct mptcp_addr_info *addr)
1550 {
1551 	struct mptcp_rm_list list = { .nr = 0 };
1552 	long s_slot = 0, s_num = 0;
1553 	struct mptcp_sock *msk;
1554 
1555 	list.ids[list.nr++] = 0;
1556 
1557 	while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1558 		struct sock *sk = (struct sock *)msk;
1559 		struct mptcp_addr_info msk_local;
1560 
1561 		if (list_empty(&msk->conn_list) || mptcp_pm_is_userspace(msk))
1562 			goto next;
1563 
1564 		mptcp_local_address((struct sock_common *)msk, &msk_local);
1565 		if (!mptcp_addresses_equal(&msk_local, addr, addr->port))
1566 			goto next;
1567 
1568 		lock_sock(sk);
1569 		spin_lock_bh(&msk->pm.lock);
1570 		mptcp_pm_remove_addr(msk, &list);
1571 		mptcp_pm_nl_rm_subflow_received(msk, &list);
1572 		__mark_subflow_endp_available(msk, 0);
1573 		spin_unlock_bh(&msk->pm.lock);
1574 		release_sock(sk);
1575 
1576 next:
1577 		sock_put(sk);
1578 		cond_resched();
1579 	}
1580 
1581 	return 0;
1582 }
1583 
1584 int mptcp_pm_nl_del_addr_doit(struct sk_buff *skb, struct genl_info *info)
1585 {
1586 	struct nlattr *attr = info->attrs[MPTCP_PM_ENDPOINT_ADDR];
1587 	struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1588 	struct mptcp_pm_addr_entry addr, *entry;
1589 	unsigned int addr_max;
1590 	int ret;
1591 
1592 	ret = mptcp_pm_parse_entry(attr, info, false, &addr);
1593 	if (ret < 0)
1594 		return ret;
1595 
1596 	/* the zero id address is special: the first address used by the msk
1597 	 * always gets such an id, so different subflows can have different zero
1598 	 * id addresses. Additionally zero id is not accounted for in id_bitmap.
1599 	 * Let's use an 'mptcp_rm_list' instead of the common remove code.
1600 	 */
1601 	if (addr.addr.id == 0)
1602 		return mptcp_nl_remove_id_zero_address(sock_net(skb->sk), &addr.addr);
1603 
1604 	spin_lock_bh(&pernet->lock);
1605 	entry = __lookup_addr_by_id(pernet, addr.addr.id);
1606 	if (!entry) {
1607 		GENL_SET_ERR_MSG(info, "address not found");
1608 		spin_unlock_bh(&pernet->lock);
1609 		return -EINVAL;
1610 	}
1611 	if (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
1612 		addr_max = pernet->add_addr_signal_max;
1613 		WRITE_ONCE(pernet->add_addr_signal_max, addr_max - 1);
1614 	}
1615 	if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) {
1616 		addr_max = pernet->local_addr_max;
1617 		WRITE_ONCE(pernet->local_addr_max, addr_max - 1);
1618 	}
1619 
1620 	pernet->addrs--;
1621 	list_del_rcu(&entry->list);
1622 	__clear_bit(entry->addr.id, pernet->id_bitmap);
1623 	spin_unlock_bh(&pernet->lock);
1624 
1625 	mptcp_nl_remove_subflow_and_signal_addr(sock_net(skb->sk), entry);
1626 	synchronize_rcu();
1627 	__mptcp_pm_release_addr_entry(entry);
1628 
1629 	return ret;
1630 }
1631 
1632 /* Called from the userspace PM only */
1633 void mptcp_pm_remove_addrs(struct mptcp_sock *msk, struct list_head *rm_list)
1634 {
1635 	struct mptcp_rm_list alist = { .nr = 0 };
1636 	struct mptcp_pm_addr_entry *entry;
1637 	int anno_nr = 0;
1638 
1639 	list_for_each_entry(entry, rm_list, list) {
1640 		if (alist.nr >= MPTCP_RM_IDS_MAX)
1641 			break;
1642 
1643 		/* only delete if either announced or matching a subflow */
1644 		if (remove_anno_list_by_saddr(msk, &entry->addr))
1645 			anno_nr++;
1646 		else if (!lookup_subflow_by_saddr(&msk->conn_list,
1647 						  &entry->addr))
1648 			continue;
1649 
1650 		alist.ids[alist.nr++] = entry->addr.id;
1651 	}
1652 
1653 	if (alist.nr) {
1654 		spin_lock_bh(&msk->pm.lock);
1655 		msk->pm.add_addr_signaled -= anno_nr;
1656 		mptcp_pm_remove_addr(msk, &alist);
1657 		spin_unlock_bh(&msk->pm.lock);
1658 	}
1659 }
1660 
1661 /* Called from the in-kernel PM only */
1662 static void mptcp_pm_flush_addrs_and_subflows(struct mptcp_sock *msk,
1663 					      struct list_head *rm_list)
1664 {
1665 	struct mptcp_rm_list alist = { .nr = 0 }, slist = { .nr = 0 };
1666 	struct mptcp_pm_addr_entry *entry;
1667 
1668 	list_for_each_entry(entry, rm_list, list) {
1669 		if (slist.nr < MPTCP_RM_IDS_MAX &&
1670 		    lookup_subflow_by_saddr(&msk->conn_list, &entry->addr))
1671 			slist.ids[slist.nr++] = mptcp_endp_get_local_id(msk, &entry->addr);
1672 
1673 		if (alist.nr < MPTCP_RM_IDS_MAX &&
1674 		    remove_anno_list_by_saddr(msk, &entry->addr))
1675 			alist.ids[alist.nr++] = mptcp_endp_get_local_id(msk, &entry->addr);
1676 	}
1677 
1678 	spin_lock_bh(&msk->pm.lock);
1679 	if (alist.nr) {
1680 		msk->pm.add_addr_signaled -= alist.nr;
1681 		mptcp_pm_remove_addr(msk, &alist);
1682 	}
1683 	if (slist.nr)
1684 		mptcp_pm_nl_rm_subflow_received(msk, &slist);
1685 	/* Reset counters: maybe some subflows have been removed before */
1686 	bitmap_fill(msk->pm.id_avail_bitmap, MPTCP_PM_MAX_ADDR_ID + 1);
1687 	msk->pm.local_addr_used = 0;
1688 	spin_unlock_bh(&msk->pm.lock);
1689 }
1690 
1691 static void mptcp_nl_flush_addrs_list(struct net *net,
1692 				      struct list_head *rm_list)
1693 {
1694 	long s_slot = 0, s_num = 0;
1695 	struct mptcp_sock *msk;
1696 
1697 	if (list_empty(rm_list))
1698 		return;
1699 
1700 	while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1701 		struct sock *sk = (struct sock *)msk;
1702 
1703 		if (!mptcp_pm_is_userspace(msk)) {
1704 			lock_sock(sk);
1705 			mptcp_pm_flush_addrs_and_subflows(msk, rm_list);
1706 			release_sock(sk);
1707 		}
1708 
1709 		sock_put(sk);
1710 		cond_resched();
1711 	}
1712 }
1713 
1714 /* caller must ensure the RCU grace period is already elapsed */
1715 static void __flush_addrs(struct list_head *list)
1716 {
1717 	while (!list_empty(list)) {
1718 		struct mptcp_pm_addr_entry *cur;
1719 
1720 		cur = list_entry(list->next,
1721 				 struct mptcp_pm_addr_entry, list);
1722 		list_del_rcu(&cur->list);
1723 		__mptcp_pm_release_addr_entry(cur);
1724 	}
1725 }
1726 
1727 static void __reset_counters(struct pm_nl_pernet *pernet)
1728 {
1729 	WRITE_ONCE(pernet->add_addr_signal_max, 0);
1730 	WRITE_ONCE(pernet->add_addr_accept_max, 0);
1731 	WRITE_ONCE(pernet->local_addr_max, 0);
1732 	pernet->addrs = 0;
1733 }
1734 
1735 int mptcp_pm_nl_flush_addrs_doit(struct sk_buff *skb, struct genl_info *info)
1736 {
1737 	struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1738 	LIST_HEAD(free_list);
1739 
1740 	spin_lock_bh(&pernet->lock);
1741 	list_splice_init(&pernet->local_addr_list, &free_list);
1742 	__reset_counters(pernet);
1743 	pernet->next_id = 1;
1744 	bitmap_zero(pernet->id_bitmap, MPTCP_PM_MAX_ADDR_ID + 1);
1745 	spin_unlock_bh(&pernet->lock);
1746 	mptcp_nl_flush_addrs_list(sock_net(skb->sk), &free_list);
1747 	synchronize_rcu();
1748 	__flush_addrs(&free_list);
1749 	return 0;
1750 }
1751 
1752 int mptcp_nl_fill_addr(struct sk_buff *skb,
1753 		       struct mptcp_pm_addr_entry *entry)
1754 {
1755 	struct mptcp_addr_info *addr = &entry->addr;
1756 	struct nlattr *attr;
1757 
1758 	attr = nla_nest_start(skb, MPTCP_PM_ATTR_ADDR);
1759 	if (!attr)
1760 		return -EMSGSIZE;
1761 
1762 	if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_FAMILY, addr->family))
1763 		goto nla_put_failure;
1764 	if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_PORT, ntohs(addr->port)))
1765 		goto nla_put_failure;
1766 	if (nla_put_u8(skb, MPTCP_PM_ADDR_ATTR_ID, addr->id))
1767 		goto nla_put_failure;
1768 	if (nla_put_u32(skb, MPTCP_PM_ADDR_ATTR_FLAGS, entry->flags))
1769 		goto nla_put_failure;
1770 	if (entry->ifindex &&
1771 	    nla_put_s32(skb, MPTCP_PM_ADDR_ATTR_IF_IDX, entry->ifindex))
1772 		goto nla_put_failure;
1773 
1774 	if (addr->family == AF_INET &&
1775 	    nla_put_in_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR4,
1776 			    addr->addr.s_addr))
1777 		goto nla_put_failure;
1778 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1779 	else if (addr->family == AF_INET6 &&
1780 		 nla_put_in6_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR6, &addr->addr6))
1781 		goto nla_put_failure;
1782 #endif
1783 	nla_nest_end(skb, attr);
1784 	return 0;
1785 
1786 nla_put_failure:
1787 	nla_nest_cancel(skb, attr);
1788 	return -EMSGSIZE;
1789 }
1790 
1791 int mptcp_pm_nl_get_addr(struct sk_buff *skb, struct genl_info *info)
1792 {
1793 	struct nlattr *attr = info->attrs[MPTCP_PM_ENDPOINT_ADDR];
1794 	struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1795 	struct mptcp_pm_addr_entry addr, *entry;
1796 	struct sk_buff *msg;
1797 	void *reply;
1798 	int ret;
1799 
1800 	ret = mptcp_pm_parse_entry(attr, info, false, &addr);
1801 	if (ret < 0)
1802 		return ret;
1803 
1804 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1805 	if (!msg)
1806 		return -ENOMEM;
1807 
1808 	reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1809 				  info->genlhdr->cmd);
1810 	if (!reply) {
1811 		GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1812 		ret = -EMSGSIZE;
1813 		goto fail;
1814 	}
1815 
1816 	spin_lock_bh(&pernet->lock);
1817 	entry = __lookup_addr_by_id(pernet, addr.addr.id);
1818 	if (!entry) {
1819 		GENL_SET_ERR_MSG(info, "address not found");
1820 		ret = -EINVAL;
1821 		goto unlock_fail;
1822 	}
1823 
1824 	ret = mptcp_nl_fill_addr(msg, entry);
1825 	if (ret)
1826 		goto unlock_fail;
1827 
1828 	genlmsg_end(msg, reply);
1829 	ret = genlmsg_reply(msg, info);
1830 	spin_unlock_bh(&pernet->lock);
1831 	return ret;
1832 
1833 unlock_fail:
1834 	spin_unlock_bh(&pernet->lock);
1835 
1836 fail:
1837 	nlmsg_free(msg);
1838 	return ret;
1839 }
1840 
1841 int mptcp_pm_nl_get_addr_doit(struct sk_buff *skb, struct genl_info *info)
1842 {
1843 	return mptcp_pm_get_addr(skb, info);
1844 }
1845 
1846 int mptcp_pm_nl_dump_addr(struct sk_buff *msg,
1847 			  struct netlink_callback *cb)
1848 {
1849 	struct net *net = sock_net(msg->sk);
1850 	struct mptcp_pm_addr_entry *entry;
1851 	struct pm_nl_pernet *pernet;
1852 	int id = cb->args[0];
1853 	void *hdr;
1854 	int i;
1855 
1856 	pernet = pm_nl_get_pernet(net);
1857 
1858 	spin_lock_bh(&pernet->lock);
1859 	for (i = id; i < MPTCP_PM_MAX_ADDR_ID + 1; i++) {
1860 		if (test_bit(i, pernet->id_bitmap)) {
1861 			entry = __lookup_addr_by_id(pernet, i);
1862 			if (!entry)
1863 				break;
1864 
1865 			if (entry->addr.id <= id)
1866 				continue;
1867 
1868 			hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid,
1869 					  cb->nlh->nlmsg_seq, &mptcp_genl_family,
1870 					  NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR);
1871 			if (!hdr)
1872 				break;
1873 
1874 			if (mptcp_nl_fill_addr(msg, entry) < 0) {
1875 				genlmsg_cancel(msg, hdr);
1876 				break;
1877 			}
1878 
1879 			id = entry->addr.id;
1880 			genlmsg_end(msg, hdr);
1881 		}
1882 	}
1883 	spin_unlock_bh(&pernet->lock);
1884 
1885 	cb->args[0] = id;
1886 	return msg->len;
1887 }
1888 
1889 int mptcp_pm_nl_get_addr_dumpit(struct sk_buff *msg,
1890 				struct netlink_callback *cb)
1891 {
1892 	return mptcp_pm_dump_addr(msg, cb);
1893 }
1894 
1895 static int parse_limit(struct genl_info *info, int id, unsigned int *limit)
1896 {
1897 	struct nlattr *attr = info->attrs[id];
1898 
1899 	if (!attr)
1900 		return 0;
1901 
1902 	*limit = nla_get_u32(attr);
1903 	if (*limit > MPTCP_PM_ADDR_MAX) {
1904 		GENL_SET_ERR_MSG(info, "limit greater than maximum");
1905 		return -EINVAL;
1906 	}
1907 	return 0;
1908 }
1909 
1910 int mptcp_pm_nl_set_limits_doit(struct sk_buff *skb, struct genl_info *info)
1911 {
1912 	struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1913 	unsigned int rcv_addrs, subflows;
1914 	int ret;
1915 
1916 	spin_lock_bh(&pernet->lock);
1917 	rcv_addrs = pernet->add_addr_accept_max;
1918 	ret = parse_limit(info, MPTCP_PM_ATTR_RCV_ADD_ADDRS, &rcv_addrs);
1919 	if (ret)
1920 		goto unlock;
1921 
1922 	subflows = pernet->subflows_max;
1923 	ret = parse_limit(info, MPTCP_PM_ATTR_SUBFLOWS, &subflows);
1924 	if (ret)
1925 		goto unlock;
1926 
1927 	WRITE_ONCE(pernet->add_addr_accept_max, rcv_addrs);
1928 	WRITE_ONCE(pernet->subflows_max, subflows);
1929 
1930 unlock:
1931 	spin_unlock_bh(&pernet->lock);
1932 	return ret;
1933 }
1934 
1935 int mptcp_pm_nl_get_limits_doit(struct sk_buff *skb, struct genl_info *info)
1936 {
1937 	struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1938 	struct sk_buff *msg;
1939 	void *reply;
1940 
1941 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1942 	if (!msg)
1943 		return -ENOMEM;
1944 
1945 	reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1946 				  MPTCP_PM_CMD_GET_LIMITS);
1947 	if (!reply)
1948 		goto fail;
1949 
1950 	if (nla_put_u32(msg, MPTCP_PM_ATTR_RCV_ADD_ADDRS,
1951 			READ_ONCE(pernet->add_addr_accept_max)))
1952 		goto fail;
1953 
1954 	if (nla_put_u32(msg, MPTCP_PM_ATTR_SUBFLOWS,
1955 			READ_ONCE(pernet->subflows_max)))
1956 		goto fail;
1957 
1958 	genlmsg_end(msg, reply);
1959 	return genlmsg_reply(msg, info);
1960 
1961 fail:
1962 	GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1963 	nlmsg_free(msg);
1964 	return -EMSGSIZE;
1965 }
1966 
1967 static void mptcp_pm_nl_fullmesh(struct mptcp_sock *msk,
1968 				 struct mptcp_addr_info *addr)
1969 {
1970 	struct mptcp_rm_list list = { .nr = 0 };
1971 
1972 	list.ids[list.nr++] = mptcp_endp_get_local_id(msk, addr);
1973 
1974 	spin_lock_bh(&msk->pm.lock);
1975 	mptcp_pm_nl_rm_subflow_received(msk, &list);
1976 	__mark_subflow_endp_available(msk, list.ids[0]);
1977 	mptcp_pm_create_subflow_or_signal_addr(msk);
1978 	spin_unlock_bh(&msk->pm.lock);
1979 }
1980 
1981 static int mptcp_nl_set_flags(struct net *net,
1982 			      struct mptcp_addr_info *addr,
1983 			      u8 bkup, u8 changed)
1984 {
1985 	long s_slot = 0, s_num = 0;
1986 	struct mptcp_sock *msk;
1987 	int ret = -EINVAL;
1988 
1989 	while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1990 		struct sock *sk = (struct sock *)msk;
1991 
1992 		if (list_empty(&msk->conn_list) || mptcp_pm_is_userspace(msk))
1993 			goto next;
1994 
1995 		lock_sock(sk);
1996 		if (changed & MPTCP_PM_ADDR_FLAG_BACKUP)
1997 			ret = mptcp_pm_nl_mp_prio_send_ack(msk, addr, NULL, bkup);
1998 		if (changed & MPTCP_PM_ADDR_FLAG_FULLMESH)
1999 			mptcp_pm_nl_fullmesh(msk, addr);
2000 		release_sock(sk);
2001 
2002 next:
2003 		sock_put(sk);
2004 		cond_resched();
2005 	}
2006 
2007 	return ret;
2008 }
2009 
2010 int mptcp_pm_nl_set_flags(struct sk_buff *skb, struct genl_info *info)
2011 {
2012 	struct mptcp_pm_addr_entry addr = { .addr = { .family = AF_UNSPEC }, };
2013 	struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
2014 	u8 changed, mask = MPTCP_PM_ADDR_FLAG_BACKUP |
2015 			   MPTCP_PM_ADDR_FLAG_FULLMESH;
2016 	struct net *net = sock_net(skb->sk);
2017 	struct mptcp_pm_addr_entry *entry;
2018 	struct pm_nl_pernet *pernet;
2019 	u8 lookup_by_id = 0;
2020 	u8 bkup = 0;
2021 	int ret;
2022 
2023 	pernet = pm_nl_get_pernet(net);
2024 
2025 	ret = mptcp_pm_parse_entry(attr, info, false, &addr);
2026 	if (ret < 0)
2027 		return ret;
2028 
2029 	if (addr.addr.family == AF_UNSPEC) {
2030 		lookup_by_id = 1;
2031 		if (!addr.addr.id) {
2032 			GENL_SET_ERR_MSG(info, "missing required inputs");
2033 			return -EOPNOTSUPP;
2034 		}
2035 	}
2036 
2037 	if (addr.flags & MPTCP_PM_ADDR_FLAG_BACKUP)
2038 		bkup = 1;
2039 
2040 	spin_lock_bh(&pernet->lock);
2041 	entry = lookup_by_id ? __lookup_addr_by_id(pernet, addr.addr.id) :
2042 			       __lookup_addr(pernet, &addr.addr);
2043 	if (!entry) {
2044 		spin_unlock_bh(&pernet->lock);
2045 		GENL_SET_ERR_MSG(info, "address not found");
2046 		return -EINVAL;
2047 	}
2048 	if ((addr.flags & MPTCP_PM_ADDR_FLAG_FULLMESH) &&
2049 	    (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) {
2050 		spin_unlock_bh(&pernet->lock);
2051 		GENL_SET_ERR_MSG(info, "invalid addr flags");
2052 		return -EINVAL;
2053 	}
2054 
2055 	changed = (addr.flags ^ entry->flags) & mask;
2056 	entry->flags = (entry->flags & ~mask) | (addr.flags & mask);
2057 	addr = *entry;
2058 	spin_unlock_bh(&pernet->lock);
2059 
2060 	mptcp_nl_set_flags(net, &addr.addr, bkup, changed);
2061 	return 0;
2062 }
2063 
2064 int mptcp_pm_nl_set_flags_doit(struct sk_buff *skb, struct genl_info *info)
2065 {
2066 	return mptcp_pm_set_flags(skb, info);
2067 }
2068 
2069 static void mptcp_nl_mcast_send(struct net *net, struct sk_buff *nlskb, gfp_t gfp)
2070 {
2071 	genlmsg_multicast_netns(&mptcp_genl_family, net,
2072 				nlskb, 0, MPTCP_PM_EV_GRP_OFFSET, gfp);
2073 }
2074 
2075 bool mptcp_userspace_pm_active(const struct mptcp_sock *msk)
2076 {
2077 	return genl_has_listeners(&mptcp_genl_family,
2078 				  sock_net((const struct sock *)msk),
2079 				  MPTCP_PM_EV_GRP_OFFSET);
2080 }
2081 
2082 static int mptcp_event_add_subflow(struct sk_buff *skb, const struct sock *ssk)
2083 {
2084 	const struct inet_sock *issk = inet_sk(ssk);
2085 	const struct mptcp_subflow_context *sf;
2086 
2087 	if (nla_put_u16(skb, MPTCP_ATTR_FAMILY, ssk->sk_family))
2088 		return -EMSGSIZE;
2089 
2090 	switch (ssk->sk_family) {
2091 	case AF_INET:
2092 		if (nla_put_in_addr(skb, MPTCP_ATTR_SADDR4, issk->inet_saddr))
2093 			return -EMSGSIZE;
2094 		if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, issk->inet_daddr))
2095 			return -EMSGSIZE;
2096 		break;
2097 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2098 	case AF_INET6: {
2099 		const struct ipv6_pinfo *np = inet6_sk(ssk);
2100 
2101 		if (nla_put_in6_addr(skb, MPTCP_ATTR_SADDR6, &np->saddr))
2102 			return -EMSGSIZE;
2103 		if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &ssk->sk_v6_daddr))
2104 			return -EMSGSIZE;
2105 		break;
2106 	}
2107 #endif
2108 	default:
2109 		WARN_ON_ONCE(1);
2110 		return -EMSGSIZE;
2111 	}
2112 
2113 	if (nla_put_be16(skb, MPTCP_ATTR_SPORT, issk->inet_sport))
2114 		return -EMSGSIZE;
2115 	if (nla_put_be16(skb, MPTCP_ATTR_DPORT, issk->inet_dport))
2116 		return -EMSGSIZE;
2117 
2118 	sf = mptcp_subflow_ctx(ssk);
2119 	if (WARN_ON_ONCE(!sf))
2120 		return -EINVAL;
2121 
2122 	if (nla_put_u8(skb, MPTCP_ATTR_LOC_ID, subflow_get_local_id(sf)))
2123 		return -EMSGSIZE;
2124 
2125 	if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, sf->remote_id))
2126 		return -EMSGSIZE;
2127 
2128 	return 0;
2129 }
2130 
2131 static int mptcp_event_put_token_and_ssk(struct sk_buff *skb,
2132 					 const struct mptcp_sock *msk,
2133 					 const struct sock *ssk)
2134 {
2135 	const struct sock *sk = (const struct sock *)msk;
2136 	const struct mptcp_subflow_context *sf;
2137 	u8 sk_err;
2138 
2139 	if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, READ_ONCE(msk->token)))
2140 		return -EMSGSIZE;
2141 
2142 	if (mptcp_event_add_subflow(skb, ssk))
2143 		return -EMSGSIZE;
2144 
2145 	sf = mptcp_subflow_ctx(ssk);
2146 	if (WARN_ON_ONCE(!sf))
2147 		return -EINVAL;
2148 
2149 	if (nla_put_u8(skb, MPTCP_ATTR_BACKUP, sf->backup))
2150 		return -EMSGSIZE;
2151 
2152 	if (ssk->sk_bound_dev_if &&
2153 	    nla_put_s32(skb, MPTCP_ATTR_IF_IDX, ssk->sk_bound_dev_if))
2154 		return -EMSGSIZE;
2155 
2156 	sk_err = READ_ONCE(ssk->sk_err);
2157 	if (sk_err && sk->sk_state == TCP_ESTABLISHED &&
2158 	    nla_put_u8(skb, MPTCP_ATTR_ERROR, sk_err))
2159 		return -EMSGSIZE;
2160 
2161 	return 0;
2162 }
2163 
2164 static int mptcp_event_sub_established(struct sk_buff *skb,
2165 				       const struct mptcp_sock *msk,
2166 				       const struct sock *ssk)
2167 {
2168 	return mptcp_event_put_token_and_ssk(skb, msk, ssk);
2169 }
2170 
2171 static int mptcp_event_sub_closed(struct sk_buff *skb,
2172 				  const struct mptcp_sock *msk,
2173 				  const struct sock *ssk)
2174 {
2175 	const struct mptcp_subflow_context *sf;
2176 
2177 	if (mptcp_event_put_token_and_ssk(skb, msk, ssk))
2178 		return -EMSGSIZE;
2179 
2180 	sf = mptcp_subflow_ctx(ssk);
2181 	if (!sf->reset_seen)
2182 		return 0;
2183 
2184 	if (nla_put_u32(skb, MPTCP_ATTR_RESET_REASON, sf->reset_reason))
2185 		return -EMSGSIZE;
2186 
2187 	if (nla_put_u32(skb, MPTCP_ATTR_RESET_FLAGS, sf->reset_transient))
2188 		return -EMSGSIZE;
2189 
2190 	return 0;
2191 }
2192 
2193 static int mptcp_event_created(struct sk_buff *skb,
2194 			       const struct mptcp_sock *msk,
2195 			       const struct sock *ssk)
2196 {
2197 	int err = nla_put_u32(skb, MPTCP_ATTR_TOKEN, READ_ONCE(msk->token));
2198 
2199 	if (err)
2200 		return err;
2201 
2202 	if (nla_put_u8(skb, MPTCP_ATTR_SERVER_SIDE, READ_ONCE(msk->pm.server_side)))
2203 		return -EMSGSIZE;
2204 
2205 	return mptcp_event_add_subflow(skb, ssk);
2206 }
2207 
2208 void mptcp_event_addr_removed(const struct mptcp_sock *msk, uint8_t id)
2209 {
2210 	struct net *net = sock_net((const struct sock *)msk);
2211 	struct nlmsghdr *nlh;
2212 	struct sk_buff *skb;
2213 
2214 	if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
2215 		return;
2216 
2217 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2218 	if (!skb)
2219 		return;
2220 
2221 	nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, MPTCP_EVENT_REMOVED);
2222 	if (!nlh)
2223 		goto nla_put_failure;
2224 
2225 	if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, READ_ONCE(msk->token)))
2226 		goto nla_put_failure;
2227 
2228 	if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, id))
2229 		goto nla_put_failure;
2230 
2231 	genlmsg_end(skb, nlh);
2232 	mptcp_nl_mcast_send(net, skb, GFP_ATOMIC);
2233 	return;
2234 
2235 nla_put_failure:
2236 	nlmsg_free(skb);
2237 }
2238 
2239 void mptcp_event_addr_announced(const struct sock *ssk,
2240 				const struct mptcp_addr_info *info)
2241 {
2242 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
2243 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
2244 	struct net *net = sock_net(ssk);
2245 	struct nlmsghdr *nlh;
2246 	struct sk_buff *skb;
2247 
2248 	if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
2249 		return;
2250 
2251 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2252 	if (!skb)
2253 		return;
2254 
2255 	nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0,
2256 			  MPTCP_EVENT_ANNOUNCED);
2257 	if (!nlh)
2258 		goto nla_put_failure;
2259 
2260 	if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, READ_ONCE(msk->token)))
2261 		goto nla_put_failure;
2262 
2263 	if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, info->id))
2264 		goto nla_put_failure;
2265 
2266 	if (nla_put_be16(skb, MPTCP_ATTR_DPORT,
2267 			 info->port == 0 ?
2268 			 inet_sk(ssk)->inet_dport :
2269 			 info->port))
2270 		goto nla_put_failure;
2271 
2272 	switch (info->family) {
2273 	case AF_INET:
2274 		if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, info->addr.s_addr))
2275 			goto nla_put_failure;
2276 		break;
2277 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2278 	case AF_INET6:
2279 		if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &info->addr6))
2280 			goto nla_put_failure;
2281 		break;
2282 #endif
2283 	default:
2284 		WARN_ON_ONCE(1);
2285 		goto nla_put_failure;
2286 	}
2287 
2288 	genlmsg_end(skb, nlh);
2289 	mptcp_nl_mcast_send(net, skb, GFP_ATOMIC);
2290 	return;
2291 
2292 nla_put_failure:
2293 	nlmsg_free(skb);
2294 }
2295 
2296 void mptcp_event_pm_listener(const struct sock *ssk,
2297 			     enum mptcp_event_type event)
2298 {
2299 	const struct inet_sock *issk = inet_sk(ssk);
2300 	struct net *net = sock_net(ssk);
2301 	struct nlmsghdr *nlh;
2302 	struct sk_buff *skb;
2303 
2304 	if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
2305 		return;
2306 
2307 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2308 	if (!skb)
2309 		return;
2310 
2311 	nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, event);
2312 	if (!nlh)
2313 		goto nla_put_failure;
2314 
2315 	if (nla_put_u16(skb, MPTCP_ATTR_FAMILY, ssk->sk_family))
2316 		goto nla_put_failure;
2317 
2318 	if (nla_put_be16(skb, MPTCP_ATTR_SPORT, issk->inet_sport))
2319 		goto nla_put_failure;
2320 
2321 	switch (ssk->sk_family) {
2322 	case AF_INET:
2323 		if (nla_put_in_addr(skb, MPTCP_ATTR_SADDR4, issk->inet_saddr))
2324 			goto nla_put_failure;
2325 		break;
2326 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2327 	case AF_INET6: {
2328 		const struct ipv6_pinfo *np = inet6_sk(ssk);
2329 
2330 		if (nla_put_in6_addr(skb, MPTCP_ATTR_SADDR6, &np->saddr))
2331 			goto nla_put_failure;
2332 		break;
2333 	}
2334 #endif
2335 	default:
2336 		WARN_ON_ONCE(1);
2337 		goto nla_put_failure;
2338 	}
2339 
2340 	genlmsg_end(skb, nlh);
2341 	mptcp_nl_mcast_send(net, skb, GFP_KERNEL);
2342 	return;
2343 
2344 nla_put_failure:
2345 	nlmsg_free(skb);
2346 }
2347 
2348 void mptcp_event(enum mptcp_event_type type, const struct mptcp_sock *msk,
2349 		 const struct sock *ssk, gfp_t gfp)
2350 {
2351 	struct net *net = sock_net((const struct sock *)msk);
2352 	struct nlmsghdr *nlh;
2353 	struct sk_buff *skb;
2354 
2355 	if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
2356 		return;
2357 
2358 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
2359 	if (!skb)
2360 		return;
2361 
2362 	nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, type);
2363 	if (!nlh)
2364 		goto nla_put_failure;
2365 
2366 	switch (type) {
2367 	case MPTCP_EVENT_UNSPEC:
2368 		WARN_ON_ONCE(1);
2369 		break;
2370 	case MPTCP_EVENT_CREATED:
2371 	case MPTCP_EVENT_ESTABLISHED:
2372 		if (mptcp_event_created(skb, msk, ssk) < 0)
2373 			goto nla_put_failure;
2374 		break;
2375 	case MPTCP_EVENT_CLOSED:
2376 		if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, READ_ONCE(msk->token)) < 0)
2377 			goto nla_put_failure;
2378 		break;
2379 	case MPTCP_EVENT_ANNOUNCED:
2380 	case MPTCP_EVENT_REMOVED:
2381 		/* call mptcp_event_addr_announced()/removed instead */
2382 		WARN_ON_ONCE(1);
2383 		break;
2384 	case MPTCP_EVENT_SUB_ESTABLISHED:
2385 	case MPTCP_EVENT_SUB_PRIORITY:
2386 		if (mptcp_event_sub_established(skb, msk, ssk) < 0)
2387 			goto nla_put_failure;
2388 		break;
2389 	case MPTCP_EVENT_SUB_CLOSED:
2390 		if (mptcp_event_sub_closed(skb, msk, ssk) < 0)
2391 			goto nla_put_failure;
2392 		break;
2393 	case MPTCP_EVENT_LISTENER_CREATED:
2394 	case MPTCP_EVENT_LISTENER_CLOSED:
2395 		break;
2396 	}
2397 
2398 	genlmsg_end(skb, nlh);
2399 	mptcp_nl_mcast_send(net, skb, gfp);
2400 	return;
2401 
2402 nla_put_failure:
2403 	nlmsg_free(skb);
2404 }
2405 
2406 struct genl_family mptcp_genl_family __ro_after_init = {
2407 	.name		= MPTCP_PM_NAME,
2408 	.version	= MPTCP_PM_VER,
2409 	.netnsok	= true,
2410 	.module		= THIS_MODULE,
2411 	.ops		= mptcp_pm_nl_ops,
2412 	.n_ops		= ARRAY_SIZE(mptcp_pm_nl_ops),
2413 	.resv_start_op	= MPTCP_PM_CMD_SUBFLOW_DESTROY + 1,
2414 	.mcgrps		= mptcp_pm_mcgrps,
2415 	.n_mcgrps	= ARRAY_SIZE(mptcp_pm_mcgrps),
2416 };
2417 
2418 static int __net_init pm_nl_init_net(struct net *net)
2419 {
2420 	struct pm_nl_pernet *pernet = pm_nl_get_pernet(net);
2421 
2422 	INIT_LIST_HEAD_RCU(&pernet->local_addr_list);
2423 
2424 	/* Cit. 2 subflows ought to be enough for anybody. */
2425 	pernet->subflows_max = 2;
2426 	pernet->next_id = 1;
2427 	pernet->stale_loss_cnt = 4;
2428 	spin_lock_init(&pernet->lock);
2429 
2430 	/* No need to initialize other pernet fields, the struct is zeroed at
2431 	 * allocation time.
2432 	 */
2433 
2434 	return 0;
2435 }
2436 
2437 static void __net_exit pm_nl_exit_net(struct list_head *net_list)
2438 {
2439 	struct net *net;
2440 
2441 	list_for_each_entry(net, net_list, exit_list) {
2442 		struct pm_nl_pernet *pernet = pm_nl_get_pernet(net);
2443 
2444 		/* net is removed from namespace list, can't race with
2445 		 * other modifiers, also netns core already waited for a
2446 		 * RCU grace period.
2447 		 */
2448 		__flush_addrs(&pernet->local_addr_list);
2449 	}
2450 }
2451 
2452 static struct pernet_operations mptcp_pm_pernet_ops = {
2453 	.init = pm_nl_init_net,
2454 	.exit_batch = pm_nl_exit_net,
2455 	.id = &pm_nl_pernet_id,
2456 	.size = sizeof(struct pm_nl_pernet),
2457 };
2458 
2459 void __init mptcp_pm_nl_init(void)
2460 {
2461 	if (register_pernet_subsys(&mptcp_pm_pernet_ops) < 0)
2462 		panic("Failed to register MPTCP PM pernet subsystem.\n");
2463 
2464 	if (genl_register_family(&mptcp_genl_family))
2465 		panic("Failed to register MPTCP PM netlink family\n");
2466 }
2467