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