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