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