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