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