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