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