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