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