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