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