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