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