xref: /openbmc/linux/net/mptcp/pm_netlink.c (revision 27ab1c1c)
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 	struct rcu_head		rcu;
29 };
30 
31 struct mptcp_pm_add_entry {
32 	struct list_head	list;
33 	struct mptcp_addr_info	addr;
34 	struct timer_list	add_timer;
35 	struct mptcp_sock	*sock;
36 	u8			retrans_times;
37 };
38 
39 struct pm_nl_pernet {
40 	/* protects pernet updates */
41 	spinlock_t		lock;
42 	struct list_head	local_addr_list;
43 	unsigned int		addrs;
44 	unsigned int		add_addr_signal_max;
45 	unsigned int		add_addr_accept_max;
46 	unsigned int		local_addr_max;
47 	unsigned int		subflows_max;
48 	unsigned int		next_id;
49 };
50 
51 #define MPTCP_PM_ADDR_MAX	8
52 #define ADD_ADDR_RETRANS_MAX	3
53 
54 static bool addresses_equal(const struct mptcp_addr_info *a,
55 			    struct mptcp_addr_info *b, bool use_port)
56 {
57 	bool addr_equals = false;
58 
59 	if (a->family != b->family)
60 		return false;
61 
62 	if (a->family == AF_INET)
63 		addr_equals = a->addr.s_addr == b->addr.s_addr;
64 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
65 	else
66 		addr_equals = !ipv6_addr_cmp(&a->addr6, &b->addr6);
67 #endif
68 
69 	if (!addr_equals)
70 		return false;
71 	if (!use_port)
72 		return true;
73 
74 	return a->port == b->port;
75 }
76 
77 static bool address_zero(const struct mptcp_addr_info *addr)
78 {
79 	struct mptcp_addr_info zero;
80 
81 	memset(&zero, 0, sizeof(zero));
82 	zero.family = addr->family;
83 
84 	return addresses_equal(addr, &zero, false);
85 }
86 
87 static void local_address(const struct sock_common *skc,
88 			  struct mptcp_addr_info *addr)
89 {
90 	addr->port = 0;
91 	addr->family = skc->skc_family;
92 	if (addr->family == AF_INET)
93 		addr->addr.s_addr = skc->skc_rcv_saddr;
94 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
95 	else if (addr->family == AF_INET6)
96 		addr->addr6 = skc->skc_v6_rcv_saddr;
97 #endif
98 }
99 
100 static void remote_address(const struct sock_common *skc,
101 			   struct mptcp_addr_info *addr)
102 {
103 	addr->family = skc->skc_family;
104 	addr->port = skc->skc_dport;
105 	if (addr->family == AF_INET)
106 		addr->addr.s_addr = skc->skc_daddr;
107 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
108 	else if (addr->family == AF_INET6)
109 		addr->addr6 = skc->skc_v6_daddr;
110 #endif
111 }
112 
113 static bool lookup_subflow_by_saddr(const struct list_head *list,
114 				    struct mptcp_addr_info *saddr)
115 {
116 	struct mptcp_subflow_context *subflow;
117 	struct mptcp_addr_info cur;
118 	struct sock_common *skc;
119 
120 	list_for_each_entry(subflow, list, node) {
121 		skc = (struct sock_common *)mptcp_subflow_tcp_sock(subflow);
122 
123 		local_address(skc, &cur);
124 		if (addresses_equal(&cur, saddr, false))
125 			return true;
126 	}
127 
128 	return false;
129 }
130 
131 static struct mptcp_pm_addr_entry *
132 select_local_address(const struct pm_nl_pernet *pernet,
133 		     struct mptcp_sock *msk)
134 {
135 	struct mptcp_pm_addr_entry *entry, *ret = NULL;
136 
137 	rcu_read_lock();
138 	__mptcp_flush_join_list(msk);
139 	list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
140 		if (!(entry->addr.flags & MPTCP_PM_ADDR_FLAG_SUBFLOW))
141 			continue;
142 
143 		/* avoid any address already in use by subflows and
144 		 * pending join
145 		 */
146 		if (entry->addr.family == ((struct sock *)msk)->sk_family &&
147 		    !lookup_subflow_by_saddr(&msk->conn_list, &entry->addr)) {
148 			ret = entry;
149 			break;
150 		}
151 	}
152 	rcu_read_unlock();
153 	return ret;
154 }
155 
156 static struct mptcp_pm_addr_entry *
157 select_signal_address(struct pm_nl_pernet *pernet, unsigned int pos)
158 {
159 	struct mptcp_pm_addr_entry *entry, *ret = NULL;
160 	int i = 0;
161 
162 	rcu_read_lock();
163 	/* do not keep any additional per socket state, just signal
164 	 * the address list in order.
165 	 * Note: removal from the local address list during the msk life-cycle
166 	 * can lead to additional addresses not being announced.
167 	 */
168 	list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
169 		if (!(entry->addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL))
170 			continue;
171 		if (i++ == pos) {
172 			ret = entry;
173 			break;
174 		}
175 	}
176 	rcu_read_unlock();
177 	return ret;
178 }
179 
180 static void check_work_pending(struct mptcp_sock *msk)
181 {
182 	if (msk->pm.add_addr_signaled == msk->pm.add_addr_signal_max &&
183 	    (msk->pm.local_addr_used == msk->pm.local_addr_max ||
184 	     msk->pm.subflows == msk->pm.subflows_max))
185 		WRITE_ONCE(msk->pm.work_pending, false);
186 }
187 
188 static struct mptcp_pm_add_entry *
189 lookup_anno_list_by_saddr(struct mptcp_sock *msk,
190 			  struct mptcp_addr_info *addr)
191 {
192 	struct mptcp_pm_add_entry *entry;
193 
194 	list_for_each_entry(entry, &msk->pm.anno_list, list) {
195 		if (addresses_equal(&entry->addr, addr, false))
196 			return entry;
197 	}
198 
199 	return NULL;
200 }
201 
202 static void mptcp_pm_add_timer(struct timer_list *timer)
203 {
204 	struct mptcp_pm_add_entry *entry = from_timer(entry, timer, add_timer);
205 	struct mptcp_sock *msk = entry->sock;
206 	struct sock *sk = (struct sock *)msk;
207 
208 	pr_debug("msk=%p", msk);
209 
210 	if (!msk)
211 		return;
212 
213 	if (inet_sk_state_load(sk) == TCP_CLOSE)
214 		return;
215 
216 	if (!entry->addr.id)
217 		return;
218 
219 	if (mptcp_pm_should_add_signal(msk)) {
220 		sk_reset_timer(sk, timer, jiffies + TCP_RTO_MAX / 8);
221 		goto out;
222 	}
223 
224 	spin_lock_bh(&msk->pm.lock);
225 
226 	if (!mptcp_pm_should_add_signal(msk)) {
227 		pr_debug("retransmit ADD_ADDR id=%d", entry->addr.id);
228 		mptcp_pm_announce_addr(msk, &entry->addr, false, entry->addr.port);
229 		mptcp_pm_add_addr_send_ack(msk);
230 		entry->retrans_times++;
231 	}
232 
233 	if (entry->retrans_times < ADD_ADDR_RETRANS_MAX)
234 		sk_reset_timer(sk, timer,
235 			       jiffies + mptcp_get_add_addr_timeout(sock_net(sk)));
236 
237 	spin_unlock_bh(&msk->pm.lock);
238 
239 out:
240 	__sock_put(sk);
241 }
242 
243 struct mptcp_pm_add_entry *
244 mptcp_pm_del_add_timer(struct mptcp_sock *msk,
245 		       struct mptcp_addr_info *addr)
246 {
247 	struct mptcp_pm_add_entry *entry;
248 	struct sock *sk = (struct sock *)msk;
249 
250 	spin_lock_bh(&msk->pm.lock);
251 	entry = lookup_anno_list_by_saddr(msk, addr);
252 	if (entry)
253 		entry->retrans_times = ADD_ADDR_RETRANS_MAX;
254 	spin_unlock_bh(&msk->pm.lock);
255 
256 	if (entry)
257 		sk_stop_timer_sync(sk, &entry->add_timer);
258 
259 	return entry;
260 }
261 
262 static bool mptcp_pm_alloc_anno_list(struct mptcp_sock *msk,
263 				     struct mptcp_pm_addr_entry *entry)
264 {
265 	struct mptcp_pm_add_entry *add_entry = NULL;
266 	struct sock *sk = (struct sock *)msk;
267 	struct net *net = sock_net(sk);
268 
269 	if (lookup_anno_list_by_saddr(msk, &entry->addr))
270 		return false;
271 
272 	add_entry = kmalloc(sizeof(*add_entry), GFP_ATOMIC);
273 	if (!add_entry)
274 		return false;
275 
276 	list_add(&add_entry->list, &msk->pm.anno_list);
277 
278 	add_entry->addr = entry->addr;
279 	add_entry->sock = msk;
280 	add_entry->retrans_times = 0;
281 
282 	timer_setup(&add_entry->add_timer, mptcp_pm_add_timer, 0);
283 	sk_reset_timer(sk, &add_entry->add_timer,
284 		       jiffies + mptcp_get_add_addr_timeout(net));
285 
286 	return true;
287 }
288 
289 void mptcp_pm_free_anno_list(struct mptcp_sock *msk)
290 {
291 	struct mptcp_pm_add_entry *entry, *tmp;
292 	struct sock *sk = (struct sock *)msk;
293 	LIST_HEAD(free_list);
294 
295 	pr_debug("msk=%p", msk);
296 
297 	spin_lock_bh(&msk->pm.lock);
298 	list_splice_init(&msk->pm.anno_list, &free_list);
299 	spin_unlock_bh(&msk->pm.lock);
300 
301 	list_for_each_entry_safe(entry, tmp, &free_list, list) {
302 		sk_stop_timer_sync(sk, &entry->add_timer);
303 		kfree(entry);
304 	}
305 }
306 
307 static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk)
308 {
309 	struct mptcp_addr_info remote = { 0 };
310 	struct sock *sk = (struct sock *)msk;
311 	struct mptcp_pm_addr_entry *local;
312 	struct pm_nl_pernet *pernet;
313 
314 	pernet = net_generic(sock_net(sk), pm_nl_pernet_id);
315 
316 	pr_debug("local %d:%d signal %d:%d subflows %d:%d\n",
317 		 msk->pm.local_addr_used, msk->pm.local_addr_max,
318 		 msk->pm.add_addr_signaled, msk->pm.add_addr_signal_max,
319 		 msk->pm.subflows, msk->pm.subflows_max);
320 
321 	/* check first for announce */
322 	if (msk->pm.add_addr_signaled < msk->pm.add_addr_signal_max) {
323 		local = select_signal_address(pernet,
324 					      msk->pm.add_addr_signaled);
325 
326 		if (local) {
327 			if (mptcp_pm_alloc_anno_list(msk, local)) {
328 				msk->pm.add_addr_signaled++;
329 				mptcp_pm_announce_addr(msk, &local->addr, false, local->addr.port);
330 				mptcp_pm_nl_add_addr_send_ack(msk);
331 			}
332 		} else {
333 			/* pick failed, avoid fourther attempts later */
334 			msk->pm.local_addr_used = msk->pm.add_addr_signal_max;
335 		}
336 
337 		check_work_pending(msk);
338 	}
339 
340 	/* check if should create a new subflow */
341 	if (msk->pm.local_addr_used < msk->pm.local_addr_max &&
342 	    msk->pm.subflows < msk->pm.subflows_max) {
343 		remote_address((struct sock_common *)sk, &remote);
344 
345 		local = select_local_address(pernet, msk);
346 		if (local) {
347 			msk->pm.local_addr_used++;
348 			msk->pm.subflows++;
349 			check_work_pending(msk);
350 			spin_unlock_bh(&msk->pm.lock);
351 			__mptcp_subflow_connect(sk, &local->addr, &remote);
352 			spin_lock_bh(&msk->pm.lock);
353 			return;
354 		}
355 
356 		/* lookup failed, avoid fourther attempts later */
357 		msk->pm.local_addr_used = msk->pm.local_addr_max;
358 		check_work_pending(msk);
359 	}
360 }
361 
362 void mptcp_pm_nl_fully_established(struct mptcp_sock *msk)
363 {
364 	mptcp_pm_create_subflow_or_signal_addr(msk);
365 }
366 
367 void mptcp_pm_nl_subflow_established(struct mptcp_sock *msk)
368 {
369 	mptcp_pm_create_subflow_or_signal_addr(msk);
370 }
371 
372 void mptcp_pm_nl_add_addr_received(struct mptcp_sock *msk)
373 {
374 	struct sock *sk = (struct sock *)msk;
375 	struct mptcp_addr_info remote;
376 	struct mptcp_addr_info local;
377 	bool use_port = false;
378 
379 	pr_debug("accepted %d:%d remote family %d",
380 		 msk->pm.add_addr_accepted, msk->pm.add_addr_accept_max,
381 		 msk->pm.remote.family);
382 	msk->pm.add_addr_accepted++;
383 	msk->pm.subflows++;
384 	if (msk->pm.add_addr_accepted >= msk->pm.add_addr_accept_max ||
385 	    msk->pm.subflows >= msk->pm.subflows_max)
386 		WRITE_ONCE(msk->pm.accept_addr, false);
387 
388 	/* connect to the specified remote address, using whatever
389 	 * local address the routing configuration will pick.
390 	 */
391 	remote = msk->pm.remote;
392 	if (!remote.port)
393 		remote.port = sk->sk_dport;
394 	else
395 		use_port = true;
396 	memset(&local, 0, sizeof(local));
397 	local.family = remote.family;
398 
399 	spin_unlock_bh(&msk->pm.lock);
400 	__mptcp_subflow_connect(sk, &local, &remote);
401 	spin_lock_bh(&msk->pm.lock);
402 
403 	mptcp_pm_announce_addr(msk, &remote, true, use_port);
404 	mptcp_pm_nl_add_addr_send_ack(msk);
405 }
406 
407 void mptcp_pm_nl_add_addr_send_ack(struct mptcp_sock *msk)
408 {
409 	struct mptcp_subflow_context *subflow;
410 
411 	if (!mptcp_pm_should_add_signal_ipv6(msk) &&
412 	    !mptcp_pm_should_add_signal_port(msk))
413 		return;
414 
415 	__mptcp_flush_join_list(msk);
416 	subflow = list_first_entry_or_null(&msk->conn_list, typeof(*subflow), node);
417 	if (subflow) {
418 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
419 		u8 add_addr;
420 
421 		spin_unlock_bh(&msk->pm.lock);
422 		if (mptcp_pm_should_add_signal_ipv6(msk))
423 			pr_debug("send ack for add_addr6");
424 		if (mptcp_pm_should_add_signal_port(msk))
425 			pr_debug("send ack for add_addr_port");
426 
427 		lock_sock(ssk);
428 		tcp_send_ack(ssk);
429 		release_sock(ssk);
430 		spin_lock_bh(&msk->pm.lock);
431 
432 		add_addr = READ_ONCE(msk->pm.addr_signal);
433 		if (mptcp_pm_should_add_signal_ipv6(msk))
434 			add_addr &= ~BIT(MPTCP_ADD_ADDR_IPV6);
435 		if (mptcp_pm_should_add_signal_port(msk))
436 			add_addr &= ~BIT(MPTCP_ADD_ADDR_PORT);
437 		WRITE_ONCE(msk->pm.addr_signal, add_addr);
438 	}
439 }
440 
441 void mptcp_pm_nl_rm_addr_received(struct mptcp_sock *msk)
442 {
443 	struct mptcp_subflow_context *subflow, *tmp;
444 	struct sock *sk = (struct sock *)msk;
445 
446 	pr_debug("address rm_id %d", msk->pm.rm_id);
447 
448 	if (!msk->pm.rm_id)
449 		return;
450 
451 	if (list_empty(&msk->conn_list))
452 		return;
453 
454 	list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
455 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
456 		int how = RCV_SHUTDOWN | SEND_SHUTDOWN;
457 
458 		if (msk->pm.rm_id != subflow->remote_id)
459 			continue;
460 
461 		spin_unlock_bh(&msk->pm.lock);
462 		mptcp_subflow_shutdown(sk, ssk, how);
463 		__mptcp_close_ssk(sk, ssk, subflow);
464 		spin_lock_bh(&msk->pm.lock);
465 
466 		msk->pm.add_addr_accepted--;
467 		msk->pm.subflows--;
468 		WRITE_ONCE(msk->pm.accept_addr, true);
469 
470 		__MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMADDR);
471 
472 		break;
473 	}
474 }
475 
476 void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk, u8 rm_id)
477 {
478 	struct mptcp_subflow_context *subflow, *tmp;
479 	struct sock *sk = (struct sock *)msk;
480 
481 	pr_debug("subflow rm_id %d", rm_id);
482 
483 	if (!rm_id)
484 		return;
485 
486 	if (list_empty(&msk->conn_list))
487 		return;
488 
489 	list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
490 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
491 		int how = RCV_SHUTDOWN | SEND_SHUTDOWN;
492 
493 		if (rm_id != subflow->local_id)
494 			continue;
495 
496 		spin_unlock_bh(&msk->pm.lock);
497 		mptcp_subflow_shutdown(sk, ssk, how);
498 		__mptcp_close_ssk(sk, ssk, subflow);
499 		spin_lock_bh(&msk->pm.lock);
500 
501 		msk->pm.local_addr_used--;
502 		msk->pm.subflows--;
503 
504 		__MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMSUBFLOW);
505 
506 		break;
507 	}
508 }
509 
510 static bool address_use_port(struct mptcp_pm_addr_entry *entry)
511 {
512 	return (entry->addr.flags &
513 		(MPTCP_PM_ADDR_FLAG_SIGNAL | MPTCP_PM_ADDR_FLAG_SUBFLOW)) ==
514 		MPTCP_PM_ADDR_FLAG_SIGNAL;
515 }
516 
517 static int mptcp_pm_nl_append_new_local_addr(struct pm_nl_pernet *pernet,
518 					     struct mptcp_pm_addr_entry *entry)
519 {
520 	struct mptcp_pm_addr_entry *cur;
521 	int ret = -EINVAL;
522 
523 	spin_lock_bh(&pernet->lock);
524 	/* to keep the code simple, don't do IDR-like allocation for address ID,
525 	 * just bail when we exceed limits
526 	 */
527 	if (pernet->next_id > 255)
528 		goto out;
529 	if (pernet->addrs >= MPTCP_PM_ADDR_MAX)
530 		goto out;
531 
532 	/* do not insert duplicate address, differentiate on port only
533 	 * singled addresses
534 	 */
535 	list_for_each_entry(cur, &pernet->local_addr_list, list) {
536 		if (addresses_equal(&cur->addr, &entry->addr,
537 				    address_use_port(entry) &&
538 				    address_use_port(cur)))
539 			goto out;
540 	}
541 
542 	if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL)
543 		pernet->add_addr_signal_max++;
544 	if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SUBFLOW)
545 		pernet->local_addr_max++;
546 
547 	entry->addr.id = pernet->next_id++;
548 	pernet->addrs++;
549 	list_add_tail_rcu(&entry->list, &pernet->local_addr_list);
550 	ret = entry->addr.id;
551 
552 out:
553 	spin_unlock_bh(&pernet->lock);
554 	return ret;
555 }
556 
557 int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc)
558 {
559 	struct mptcp_pm_addr_entry *entry;
560 	struct mptcp_addr_info skc_local;
561 	struct mptcp_addr_info msk_local;
562 	struct pm_nl_pernet *pernet;
563 	int ret = -1;
564 
565 	if (WARN_ON_ONCE(!msk))
566 		return -1;
567 
568 	/* The 0 ID mapping is defined by the first subflow, copied into the msk
569 	 * addr
570 	 */
571 	local_address((struct sock_common *)msk, &msk_local);
572 	local_address((struct sock_common *)skc, &skc_local);
573 	if (addresses_equal(&msk_local, &skc_local, false))
574 		return 0;
575 
576 	if (address_zero(&skc_local))
577 		return 0;
578 
579 	pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
580 
581 	rcu_read_lock();
582 	list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
583 		if (addresses_equal(&entry->addr, &skc_local, false)) {
584 			ret = entry->addr.id;
585 			break;
586 		}
587 	}
588 	rcu_read_unlock();
589 	if (ret >= 0)
590 		return ret;
591 
592 	/* address not found, add to local list */
593 	entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
594 	if (!entry)
595 		return -ENOMEM;
596 
597 	entry->addr = skc_local;
598 	entry->addr.ifindex = 0;
599 	entry->addr.flags = 0;
600 	ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
601 	if (ret < 0)
602 		kfree(entry);
603 
604 	return ret;
605 }
606 
607 void mptcp_pm_nl_data_init(struct mptcp_sock *msk)
608 {
609 	struct mptcp_pm_data *pm = &msk->pm;
610 	struct pm_nl_pernet *pernet;
611 	bool subflows;
612 
613 	pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
614 
615 	pm->add_addr_signal_max = READ_ONCE(pernet->add_addr_signal_max);
616 	pm->add_addr_accept_max = READ_ONCE(pernet->add_addr_accept_max);
617 	pm->local_addr_max = READ_ONCE(pernet->local_addr_max);
618 	pm->subflows_max = READ_ONCE(pernet->subflows_max);
619 	subflows = !!pm->subflows_max;
620 	WRITE_ONCE(pm->work_pending, (!!pm->local_addr_max && subflows) ||
621 		   !!pm->add_addr_signal_max);
622 	WRITE_ONCE(pm->accept_addr, !!pm->add_addr_accept_max && subflows);
623 	WRITE_ONCE(pm->accept_subflow, subflows);
624 }
625 
626 #define MPTCP_PM_CMD_GRP_OFFSET	0
627 
628 static const struct genl_multicast_group mptcp_pm_mcgrps[] = {
629 	[MPTCP_PM_CMD_GRP_OFFSET]	= { .name = MPTCP_PM_CMD_GRP_NAME, },
630 };
631 
632 static const struct nla_policy
633 mptcp_pm_addr_policy[MPTCP_PM_ADDR_ATTR_MAX + 1] = {
634 	[MPTCP_PM_ADDR_ATTR_FAMILY]	= { .type	= NLA_U16,	},
635 	[MPTCP_PM_ADDR_ATTR_ID]		= { .type	= NLA_U8,	},
636 	[MPTCP_PM_ADDR_ATTR_ADDR4]	= { .type	= NLA_U32,	},
637 	[MPTCP_PM_ADDR_ATTR_ADDR6]	=
638 		NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
639 	[MPTCP_PM_ADDR_ATTR_PORT]	= { .type	= NLA_U16	},
640 	[MPTCP_PM_ADDR_ATTR_FLAGS]	= { .type	= NLA_U32	},
641 	[MPTCP_PM_ADDR_ATTR_IF_IDX]     = { .type	= NLA_S32	},
642 };
643 
644 static const struct nla_policy mptcp_pm_policy[MPTCP_PM_ATTR_MAX + 1] = {
645 	[MPTCP_PM_ATTR_ADDR]		=
646 					NLA_POLICY_NESTED(mptcp_pm_addr_policy),
647 	[MPTCP_PM_ATTR_RCV_ADD_ADDRS]	= { .type	= NLA_U32,	},
648 	[MPTCP_PM_ATTR_SUBFLOWS]	= { .type	= NLA_U32,	},
649 };
650 
651 static int mptcp_pm_family_to_addr(int family)
652 {
653 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
654 	if (family == AF_INET6)
655 		return MPTCP_PM_ADDR_ATTR_ADDR6;
656 #endif
657 	return MPTCP_PM_ADDR_ATTR_ADDR4;
658 }
659 
660 static int mptcp_pm_parse_addr(struct nlattr *attr, struct genl_info *info,
661 			       bool require_family,
662 			       struct mptcp_pm_addr_entry *entry)
663 {
664 	struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1];
665 	int err, addr_addr;
666 
667 	if (!attr) {
668 		GENL_SET_ERR_MSG(info, "missing address info");
669 		return -EINVAL;
670 	}
671 
672 	/* no validation needed - was already done via nested policy */
673 	err = nla_parse_nested_deprecated(tb, MPTCP_PM_ADDR_ATTR_MAX, attr,
674 					  mptcp_pm_addr_policy, info->extack);
675 	if (err)
676 		return err;
677 
678 	memset(entry, 0, sizeof(*entry));
679 	if (!tb[MPTCP_PM_ADDR_ATTR_FAMILY]) {
680 		if (!require_family)
681 			goto skip_family;
682 
683 		NL_SET_ERR_MSG_ATTR(info->extack, attr,
684 				    "missing family");
685 		return -EINVAL;
686 	}
687 
688 	entry->addr.family = nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_FAMILY]);
689 	if (entry->addr.family != AF_INET
690 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
691 	    && entry->addr.family != AF_INET6
692 #endif
693 	    ) {
694 		NL_SET_ERR_MSG_ATTR(info->extack, attr,
695 				    "unknown address family");
696 		return -EINVAL;
697 	}
698 	addr_addr = mptcp_pm_family_to_addr(entry->addr.family);
699 	if (!tb[addr_addr]) {
700 		NL_SET_ERR_MSG_ATTR(info->extack, attr,
701 				    "missing address data");
702 		return -EINVAL;
703 	}
704 
705 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
706 	if (entry->addr.family == AF_INET6)
707 		entry->addr.addr6 = nla_get_in6_addr(tb[addr_addr]);
708 	else
709 #endif
710 		entry->addr.addr.s_addr = nla_get_in_addr(tb[addr_addr]);
711 
712 skip_family:
713 	if (tb[MPTCP_PM_ADDR_ATTR_IF_IDX]) {
714 		u32 val = nla_get_s32(tb[MPTCP_PM_ADDR_ATTR_IF_IDX]);
715 
716 		entry->addr.ifindex = val;
717 	}
718 
719 	if (tb[MPTCP_PM_ADDR_ATTR_ID])
720 		entry->addr.id = nla_get_u8(tb[MPTCP_PM_ADDR_ATTR_ID]);
721 
722 	if (tb[MPTCP_PM_ADDR_ATTR_FLAGS])
723 		entry->addr.flags = nla_get_u32(tb[MPTCP_PM_ADDR_ATTR_FLAGS]);
724 
725 	return 0;
726 }
727 
728 static struct pm_nl_pernet *genl_info_pm_nl(struct genl_info *info)
729 {
730 	return net_generic(genl_info_net(info), pm_nl_pernet_id);
731 }
732 
733 static int mptcp_nl_cmd_add_addr(struct sk_buff *skb, struct genl_info *info)
734 {
735 	struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
736 	struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
737 	struct mptcp_pm_addr_entry addr, *entry;
738 	int ret;
739 
740 	ret = mptcp_pm_parse_addr(attr, info, true, &addr);
741 	if (ret < 0)
742 		return ret;
743 
744 	entry = kmalloc(sizeof(*entry), GFP_KERNEL);
745 	if (!entry) {
746 		GENL_SET_ERR_MSG(info, "can't allocate addr");
747 		return -ENOMEM;
748 	}
749 
750 	*entry = addr;
751 	ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
752 	if (ret < 0) {
753 		GENL_SET_ERR_MSG(info, "too many addresses or duplicate one");
754 		kfree(entry);
755 		return ret;
756 	}
757 
758 	return 0;
759 }
760 
761 static struct mptcp_pm_addr_entry *
762 __lookup_addr_by_id(struct pm_nl_pernet *pernet, unsigned int id)
763 {
764 	struct mptcp_pm_addr_entry *entry;
765 
766 	list_for_each_entry(entry, &pernet->local_addr_list, list) {
767 		if (entry->addr.id == id)
768 			return entry;
769 	}
770 	return NULL;
771 }
772 
773 static bool remove_anno_list_by_saddr(struct mptcp_sock *msk,
774 				      struct mptcp_addr_info *addr)
775 {
776 	struct mptcp_pm_add_entry *entry;
777 
778 	entry = mptcp_pm_del_add_timer(msk, addr);
779 	if (entry) {
780 		list_del(&entry->list);
781 		kfree(entry);
782 		return true;
783 	}
784 
785 	return false;
786 }
787 
788 static bool mptcp_pm_remove_anno_addr(struct mptcp_sock *msk,
789 				      struct mptcp_addr_info *addr,
790 				      bool force)
791 {
792 	bool ret;
793 
794 	ret = remove_anno_list_by_saddr(msk, addr);
795 	if (ret || force) {
796 		spin_lock_bh(&msk->pm.lock);
797 		mptcp_pm_remove_addr(msk, addr->id);
798 		spin_unlock_bh(&msk->pm.lock);
799 	}
800 	return ret;
801 }
802 
803 static int mptcp_nl_remove_subflow_and_signal_addr(struct net *net,
804 						   struct mptcp_addr_info *addr)
805 {
806 	struct mptcp_sock *msk;
807 	long s_slot = 0, s_num = 0;
808 
809 	pr_debug("remove_id=%d", addr->id);
810 
811 	while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
812 		struct sock *sk = (struct sock *)msk;
813 		bool remove_subflow;
814 
815 		if (list_empty(&msk->conn_list)) {
816 			mptcp_pm_remove_anno_addr(msk, addr, false);
817 			goto next;
818 		}
819 
820 		lock_sock(sk);
821 		remove_subflow = lookup_subflow_by_saddr(&msk->conn_list, addr);
822 		mptcp_pm_remove_anno_addr(msk, addr, remove_subflow);
823 		if (remove_subflow)
824 			mptcp_pm_remove_subflow(msk, addr->id);
825 		release_sock(sk);
826 
827 next:
828 		sock_put(sk);
829 		cond_resched();
830 	}
831 
832 	return 0;
833 }
834 
835 static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info)
836 {
837 	struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
838 	struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
839 	struct mptcp_pm_addr_entry addr, *entry;
840 	int ret;
841 
842 	ret = mptcp_pm_parse_addr(attr, info, false, &addr);
843 	if (ret < 0)
844 		return ret;
845 
846 	spin_lock_bh(&pernet->lock);
847 	entry = __lookup_addr_by_id(pernet, addr.addr.id);
848 	if (!entry) {
849 		GENL_SET_ERR_MSG(info, "address not found");
850 		spin_unlock_bh(&pernet->lock);
851 		return -EINVAL;
852 	}
853 	if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL)
854 		pernet->add_addr_signal_max--;
855 	if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SUBFLOW)
856 		pernet->local_addr_max--;
857 
858 	pernet->addrs--;
859 	list_del_rcu(&entry->list);
860 	spin_unlock_bh(&pernet->lock);
861 
862 	mptcp_nl_remove_subflow_and_signal_addr(sock_net(skb->sk), &entry->addr);
863 	kfree_rcu(entry, rcu);
864 
865 	return ret;
866 }
867 
868 static void __flush_addrs(struct net *net, struct list_head *list)
869 {
870 	while (!list_empty(list)) {
871 		struct mptcp_pm_addr_entry *cur;
872 
873 		cur = list_entry(list->next,
874 				 struct mptcp_pm_addr_entry, list);
875 		mptcp_nl_remove_subflow_and_signal_addr(net, &cur->addr);
876 		list_del_rcu(&cur->list);
877 		kfree_rcu(cur, rcu);
878 	}
879 }
880 
881 static void __reset_counters(struct pm_nl_pernet *pernet)
882 {
883 	pernet->add_addr_signal_max = 0;
884 	pernet->add_addr_accept_max = 0;
885 	pernet->local_addr_max = 0;
886 	pernet->addrs = 0;
887 }
888 
889 static int mptcp_nl_cmd_flush_addrs(struct sk_buff *skb, struct genl_info *info)
890 {
891 	struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
892 	LIST_HEAD(free_list);
893 
894 	spin_lock_bh(&pernet->lock);
895 	list_splice_init(&pernet->local_addr_list, &free_list);
896 	__reset_counters(pernet);
897 	spin_unlock_bh(&pernet->lock);
898 	__flush_addrs(sock_net(skb->sk), &free_list);
899 	return 0;
900 }
901 
902 static int mptcp_nl_fill_addr(struct sk_buff *skb,
903 			      struct mptcp_pm_addr_entry *entry)
904 {
905 	struct mptcp_addr_info *addr = &entry->addr;
906 	struct nlattr *attr;
907 
908 	attr = nla_nest_start(skb, MPTCP_PM_ATTR_ADDR);
909 	if (!attr)
910 		return -EMSGSIZE;
911 
912 	if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_FAMILY, addr->family))
913 		goto nla_put_failure;
914 	if (nla_put_u8(skb, MPTCP_PM_ADDR_ATTR_ID, addr->id))
915 		goto nla_put_failure;
916 	if (nla_put_u32(skb, MPTCP_PM_ADDR_ATTR_FLAGS, entry->addr.flags))
917 		goto nla_put_failure;
918 	if (entry->addr.ifindex &&
919 	    nla_put_s32(skb, MPTCP_PM_ADDR_ATTR_IF_IDX, entry->addr.ifindex))
920 		goto nla_put_failure;
921 
922 	if (addr->family == AF_INET &&
923 	    nla_put_in_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR4,
924 			    addr->addr.s_addr))
925 		goto nla_put_failure;
926 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
927 	else if (addr->family == AF_INET6 &&
928 		 nla_put_in6_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR6, &addr->addr6))
929 		goto nla_put_failure;
930 #endif
931 	nla_nest_end(skb, attr);
932 	return 0;
933 
934 nla_put_failure:
935 	nla_nest_cancel(skb, attr);
936 	return -EMSGSIZE;
937 }
938 
939 static int mptcp_nl_cmd_get_addr(struct sk_buff *skb, struct genl_info *info)
940 {
941 	struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
942 	struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
943 	struct mptcp_pm_addr_entry addr, *entry;
944 	struct sk_buff *msg;
945 	void *reply;
946 	int ret;
947 
948 	ret = mptcp_pm_parse_addr(attr, info, false, &addr);
949 	if (ret < 0)
950 		return ret;
951 
952 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
953 	if (!msg)
954 		return -ENOMEM;
955 
956 	reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
957 				  info->genlhdr->cmd);
958 	if (!reply) {
959 		GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
960 		ret = -EMSGSIZE;
961 		goto fail;
962 	}
963 
964 	spin_lock_bh(&pernet->lock);
965 	entry = __lookup_addr_by_id(pernet, addr.addr.id);
966 	if (!entry) {
967 		GENL_SET_ERR_MSG(info, "address not found");
968 		ret = -EINVAL;
969 		goto unlock_fail;
970 	}
971 
972 	ret = mptcp_nl_fill_addr(msg, entry);
973 	if (ret)
974 		goto unlock_fail;
975 
976 	genlmsg_end(msg, reply);
977 	ret = genlmsg_reply(msg, info);
978 	spin_unlock_bh(&pernet->lock);
979 	return ret;
980 
981 unlock_fail:
982 	spin_unlock_bh(&pernet->lock);
983 
984 fail:
985 	nlmsg_free(msg);
986 	return ret;
987 }
988 
989 static int mptcp_nl_cmd_dump_addrs(struct sk_buff *msg,
990 				   struct netlink_callback *cb)
991 {
992 	struct net *net = sock_net(msg->sk);
993 	struct mptcp_pm_addr_entry *entry;
994 	struct pm_nl_pernet *pernet;
995 	int id = cb->args[0];
996 	void *hdr;
997 
998 	pernet = net_generic(net, pm_nl_pernet_id);
999 
1000 	spin_lock_bh(&pernet->lock);
1001 	list_for_each_entry(entry, &pernet->local_addr_list, list) {
1002 		if (entry->addr.id <= id)
1003 			continue;
1004 
1005 		hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid,
1006 				  cb->nlh->nlmsg_seq, &mptcp_genl_family,
1007 				  NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR);
1008 		if (!hdr)
1009 			break;
1010 
1011 		if (mptcp_nl_fill_addr(msg, entry) < 0) {
1012 			genlmsg_cancel(msg, hdr);
1013 			break;
1014 		}
1015 
1016 		id = entry->addr.id;
1017 		genlmsg_end(msg, hdr);
1018 	}
1019 	spin_unlock_bh(&pernet->lock);
1020 
1021 	cb->args[0] = id;
1022 	return msg->len;
1023 }
1024 
1025 static int parse_limit(struct genl_info *info, int id, unsigned int *limit)
1026 {
1027 	struct nlattr *attr = info->attrs[id];
1028 
1029 	if (!attr)
1030 		return 0;
1031 
1032 	*limit = nla_get_u32(attr);
1033 	if (*limit > MPTCP_PM_ADDR_MAX) {
1034 		GENL_SET_ERR_MSG(info, "limit greater than maximum");
1035 		return -EINVAL;
1036 	}
1037 	return 0;
1038 }
1039 
1040 static int
1041 mptcp_nl_cmd_set_limits(struct sk_buff *skb, struct genl_info *info)
1042 {
1043 	struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1044 	unsigned int rcv_addrs, subflows;
1045 	int ret;
1046 
1047 	spin_lock_bh(&pernet->lock);
1048 	rcv_addrs = pernet->add_addr_accept_max;
1049 	ret = parse_limit(info, MPTCP_PM_ATTR_RCV_ADD_ADDRS, &rcv_addrs);
1050 	if (ret)
1051 		goto unlock;
1052 
1053 	subflows = pernet->subflows_max;
1054 	ret = parse_limit(info, MPTCP_PM_ATTR_SUBFLOWS, &subflows);
1055 	if (ret)
1056 		goto unlock;
1057 
1058 	WRITE_ONCE(pernet->add_addr_accept_max, rcv_addrs);
1059 	WRITE_ONCE(pernet->subflows_max, subflows);
1060 
1061 unlock:
1062 	spin_unlock_bh(&pernet->lock);
1063 	return ret;
1064 }
1065 
1066 static int
1067 mptcp_nl_cmd_get_limits(struct sk_buff *skb, struct genl_info *info)
1068 {
1069 	struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1070 	struct sk_buff *msg;
1071 	void *reply;
1072 
1073 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1074 	if (!msg)
1075 		return -ENOMEM;
1076 
1077 	reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1078 				  MPTCP_PM_CMD_GET_LIMITS);
1079 	if (!reply)
1080 		goto fail;
1081 
1082 	if (nla_put_u32(msg, MPTCP_PM_ATTR_RCV_ADD_ADDRS,
1083 			READ_ONCE(pernet->add_addr_accept_max)))
1084 		goto fail;
1085 
1086 	if (nla_put_u32(msg, MPTCP_PM_ATTR_SUBFLOWS,
1087 			READ_ONCE(pernet->subflows_max)))
1088 		goto fail;
1089 
1090 	genlmsg_end(msg, reply);
1091 	return genlmsg_reply(msg, info);
1092 
1093 fail:
1094 	GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1095 	nlmsg_free(msg);
1096 	return -EMSGSIZE;
1097 }
1098 
1099 static const struct genl_small_ops mptcp_pm_ops[] = {
1100 	{
1101 		.cmd    = MPTCP_PM_CMD_ADD_ADDR,
1102 		.doit   = mptcp_nl_cmd_add_addr,
1103 		.flags  = GENL_ADMIN_PERM,
1104 	},
1105 	{
1106 		.cmd    = MPTCP_PM_CMD_DEL_ADDR,
1107 		.doit   = mptcp_nl_cmd_del_addr,
1108 		.flags  = GENL_ADMIN_PERM,
1109 	},
1110 	{
1111 		.cmd    = MPTCP_PM_CMD_FLUSH_ADDRS,
1112 		.doit   = mptcp_nl_cmd_flush_addrs,
1113 		.flags  = GENL_ADMIN_PERM,
1114 	},
1115 	{
1116 		.cmd    = MPTCP_PM_CMD_GET_ADDR,
1117 		.doit   = mptcp_nl_cmd_get_addr,
1118 		.dumpit   = mptcp_nl_cmd_dump_addrs,
1119 	},
1120 	{
1121 		.cmd    = MPTCP_PM_CMD_SET_LIMITS,
1122 		.doit   = mptcp_nl_cmd_set_limits,
1123 		.flags  = GENL_ADMIN_PERM,
1124 	},
1125 	{
1126 		.cmd    = MPTCP_PM_CMD_GET_LIMITS,
1127 		.doit   = mptcp_nl_cmd_get_limits,
1128 	},
1129 };
1130 
1131 static struct genl_family mptcp_genl_family __ro_after_init = {
1132 	.name		= MPTCP_PM_NAME,
1133 	.version	= MPTCP_PM_VER,
1134 	.maxattr	= MPTCP_PM_ATTR_MAX,
1135 	.policy		= mptcp_pm_policy,
1136 	.netnsok	= true,
1137 	.module		= THIS_MODULE,
1138 	.small_ops	= mptcp_pm_ops,
1139 	.n_small_ops	= ARRAY_SIZE(mptcp_pm_ops),
1140 	.mcgrps		= mptcp_pm_mcgrps,
1141 	.n_mcgrps	= ARRAY_SIZE(mptcp_pm_mcgrps),
1142 };
1143 
1144 static int __net_init pm_nl_init_net(struct net *net)
1145 {
1146 	struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id);
1147 
1148 	INIT_LIST_HEAD_RCU(&pernet->local_addr_list);
1149 	__reset_counters(pernet);
1150 	pernet->next_id = 1;
1151 	spin_lock_init(&pernet->lock);
1152 	return 0;
1153 }
1154 
1155 static void __net_exit pm_nl_exit_net(struct list_head *net_list)
1156 {
1157 	struct net *net;
1158 
1159 	list_for_each_entry(net, net_list, exit_list) {
1160 		struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id);
1161 
1162 		/* net is removed from namespace list, can't race with
1163 		 * other modifiers
1164 		 */
1165 		__flush_addrs(net, &pernet->local_addr_list);
1166 	}
1167 }
1168 
1169 static struct pernet_operations mptcp_pm_pernet_ops = {
1170 	.init = pm_nl_init_net,
1171 	.exit_batch = pm_nl_exit_net,
1172 	.id = &pm_nl_pernet_id,
1173 	.size = sizeof(struct pm_nl_pernet),
1174 };
1175 
1176 void __init mptcp_pm_nl_init(void)
1177 {
1178 	if (register_pernet_subsys(&mptcp_pm_pernet_ops) < 0)
1179 		panic("Failed to register MPTCP PM pernet subsystem.\n");
1180 
1181 	if (genl_register_family(&mptcp_genl_family))
1182 		panic("Failed to register MPTCP PM netlink family\n");
1183 }
1184