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