blob: 6e3df62a87d23369eabb8e46a07b1f3ad4ddac29 [file] [log] [blame]
Paolo Abeni01cacb02020-03-27 14:48:51 -07001// SPDX-License-Identifier: GPL-2.0
2/* Multipath TCP
3 *
4 * Copyright (c) 2020, Red Hat, Inc.
5 */
6
Geliang Tangc85adce2020-04-03 17:14:08 +08007#define pr_fmt(fmt) "MPTCP: " fmt
8
Paolo Abeni01cacb02020-03-27 14:48:51 -07009#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"
Geliang Tang7a7e52e2020-09-24 08:29:56 +080018#include "mib.h"
Paolo Abeni01cacb02020-03-27 14:48:51 -070019
20/* forward declaration */
21static struct genl_family mptcp_genl_family;
22
23static int pm_nl_pernet_id;
24
25struct mptcp_pm_addr_entry {
26 struct list_head list;
Paolo Abeni01cacb02020-03-27 14:48:51 -070027 struct mptcp_addr_info addr;
Geliang Tangdaa83ab2021-04-06 17:15:57 -070028 u8 flags;
29 int ifindex;
Geliang Tang1729cf12021-02-01 15:09:12 -080030 struct socket *lsk;
Paolo Abeni01cacb02020-03-27 14:48:51 -070031};
32
Geliang Tang0abd40f2020-09-24 08:30:00 +080033struct mptcp_pm_add_entry {
34 struct list_head list;
35 struct mptcp_addr_info addr;
Geliang Tang00cfd772020-09-24 08:30:02 +080036 struct timer_list add_timer;
37 struct mptcp_sock *sock;
38 u8 retrans_times;
Geliang Tang0abd40f2020-09-24 08:30:00 +080039};
40
Geliang Tangefd5a4c2021-01-08 16:47:55 -080041#define MAX_ADDR_ID 255
42#define BITMAP_SZ DIV_ROUND_UP(MAX_ADDR_ID + 1, BITS_PER_LONG)
43
Paolo Abeni01cacb02020-03-27 14:48:51 -070044struct pm_nl_pernet {
45 /* protects pernet updates */
46 spinlock_t lock;
47 struct list_head local_addr_list;
48 unsigned int addrs;
Paolo Abeniff5a0b42021-08-13 15:15:45 -070049 unsigned int stale_loss_cnt;
Paolo Abeni01cacb02020-03-27 14:48:51 -070050 unsigned int add_addr_signal_max;
51 unsigned int add_addr_accept_max;
52 unsigned int local_addr_max;
53 unsigned int subflows_max;
54 unsigned int next_id;
Geliang Tangefd5a4c2021-01-08 16:47:55 -080055 unsigned long id_bitmap[BITMAP_SZ];
Paolo Abeni01cacb02020-03-27 14:48:51 -070056};
57
58#define MPTCP_PM_ADDR_MAX 8
Geliang Tang00cfd772020-09-24 08:30:02 +080059#define ADD_ADDR_RETRANS_MAX 3
Paolo Abeni01cacb02020-03-27 14:48:51 -070060
61static bool addresses_equal(const struct mptcp_addr_info *a,
62 struct mptcp_addr_info *b, bool use_port)
63{
64 bool addr_equals = false;
65
Matthieu Baerts7b9b0f72021-01-25 10:59:01 -080066 if (a->family == b->family) {
67 if (a->family == AF_INET)
68 addr_equals = a->addr.s_addr == b->addr.s_addr;
Paolo Abeni01cacb02020-03-27 14:48:51 -070069#if IS_ENABLED(CONFIG_MPTCP_IPV6)
Matthieu Baerts7b9b0f72021-01-25 10:59:01 -080070 else
71 addr_equals = !ipv6_addr_cmp(&a->addr6, &b->addr6);
72 } else if (a->family == AF_INET) {
73 if (ipv6_addr_v4mapped(&b->addr6))
74 addr_equals = a->addr.s_addr == b->addr6.s6_addr32[3];
75 } else if (b->family == AF_INET) {
76 if (ipv6_addr_v4mapped(&a->addr6))
77 addr_equals = a->addr6.s6_addr32[3] == b->addr.s_addr;
Paolo Abeni01cacb02020-03-27 14:48:51 -070078#endif
Matthieu Baerts7b9b0f72021-01-25 10:59:01 -080079 }
Paolo Abeni01cacb02020-03-27 14:48:51 -070080
81 if (!addr_equals)
82 return false;
83 if (!use_port)
84 return true;
85
86 return a->port == b->port;
87}
88
Geliang Tang57025812020-09-08 10:49:38 +080089static bool address_zero(const struct mptcp_addr_info *addr)
90{
91 struct mptcp_addr_info zero;
92
93 memset(&zero, 0, sizeof(zero));
94 zero.family = addr->family;
95
Geliang Tang60b57bf2021-02-01 15:09:16 -080096 return addresses_equal(addr, &zero, true);
Geliang Tang57025812020-09-08 10:49:38 +080097}
98
Paolo Abeni01cacb02020-03-27 14:48:51 -070099static void local_address(const struct sock_common *skc,
100 struct mptcp_addr_info *addr)
101{
Paolo Abeni01cacb02020-03-27 14:48:51 -0700102 addr->family = skc->skc_family;
Geliang Tang5bc56382021-02-01 15:09:15 -0800103 addr->port = htons(skc->skc_num);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700104 if (addr->family == AF_INET)
105 addr->addr.s_addr = skc->skc_rcv_saddr;
106#if IS_ENABLED(CONFIG_MPTCP_IPV6)
107 else if (addr->family == AF_INET6)
108 addr->addr6 = skc->skc_v6_rcv_saddr;
109#endif
110}
111
112static void remote_address(const struct sock_common *skc,
113 struct mptcp_addr_info *addr)
114{
115 addr->family = skc->skc_family;
116 addr->port = skc->skc_dport;
117 if (addr->family == AF_INET)
118 addr->addr.s_addr = skc->skc_daddr;
119#if IS_ENABLED(CONFIG_MPTCP_IPV6)
120 else if (addr->family == AF_INET6)
121 addr->addr6 = skc->skc_v6_daddr;
122#endif
123}
124
125static bool lookup_subflow_by_saddr(const struct list_head *list,
126 struct mptcp_addr_info *saddr)
127{
128 struct mptcp_subflow_context *subflow;
129 struct mptcp_addr_info cur;
130 struct sock_common *skc;
131
132 list_for_each_entry(subflow, list, node) {
133 skc = (struct sock_common *)mptcp_subflow_tcp_sock(subflow);
134
135 local_address(skc, &cur);
Geliang Tang60b57bf2021-02-01 15:09:16 -0800136 if (addresses_equal(&cur, saddr, saddr->port))
Paolo Abeni01cacb02020-03-27 14:48:51 -0700137 return true;
138 }
139
140 return false;
141}
142
Geliang Tangd84ad042021-03-26 11:26:32 -0700143static bool lookup_subflow_by_daddr(const struct list_head *list,
144 struct mptcp_addr_info *daddr)
145{
146 struct mptcp_subflow_context *subflow;
147 struct mptcp_addr_info cur;
148 struct sock_common *skc;
149
150 list_for_each_entry(subflow, list, node) {
151 skc = (struct sock_common *)mptcp_subflow_tcp_sock(subflow);
152
153 remote_address(skc, &cur);
154 if (addresses_equal(&cur, daddr, daddr->port))
155 return true;
156 }
157
158 return false;
159}
160
Paolo Abeni01cacb02020-03-27 14:48:51 -0700161static struct mptcp_pm_addr_entry *
162select_local_address(const struct pm_nl_pernet *pernet,
163 struct mptcp_sock *msk)
164{
165 struct mptcp_pm_addr_entry *entry, *ret = NULL;
Matthieu Baerts7b9b0f72021-01-25 10:59:01 -0800166 struct sock *sk = (struct sock *)msk;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700167
Florian Westphal3abc05d2021-02-04 15:23:30 -0800168 msk_owned_by_me(msk);
169
Paolo Abeni01cacb02020-03-27 14:48:51 -0700170 rcu_read_lock();
Paolo Abeni1bc73272020-12-10 14:25:05 -0800171 __mptcp_flush_join_list(msk);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700172 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
Geliang Tangdaa83ab2021-04-06 17:15:57 -0700173 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW))
Paolo Abeni01cacb02020-03-27 14:48:51 -0700174 continue;
175
Matthieu Baerts7b9b0f72021-01-25 10:59:01 -0800176 if (entry->addr.family != sk->sk_family) {
177#if IS_ENABLED(CONFIG_MPTCP_IPV6)
178 if ((entry->addr.family == AF_INET &&
179 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) ||
180 (sk->sk_family == AF_INET &&
181 !ipv6_addr_v4mapped(&entry->addr.addr6)))
182#endif
183 continue;
184 }
185
Paolo Abeni01cacb02020-03-27 14:48:51 -0700186 /* avoid any address already in use by subflows and
187 * pending join
188 */
Matthieu Baerts7b9b0f72021-01-25 10:59:01 -0800189 if (!lookup_subflow_by_saddr(&msk->conn_list, &entry->addr)) {
Paolo Abeni01cacb02020-03-27 14:48:51 -0700190 ret = entry;
191 break;
192 }
193 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700194 rcu_read_unlock();
195 return ret;
196}
197
198static struct mptcp_pm_addr_entry *
199select_signal_address(struct pm_nl_pernet *pernet, unsigned int pos)
200{
201 struct mptcp_pm_addr_entry *entry, *ret = NULL;
202 int i = 0;
203
204 rcu_read_lock();
205 /* do not keep any additional per socket state, just signal
206 * the address list in order.
207 * Note: removal from the local address list during the msk life-cycle
208 * can lead to additional addresses not being announced.
209 */
210 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
Geliang Tangdaa83ab2021-04-06 17:15:57 -0700211 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL))
Paolo Abeni01cacb02020-03-27 14:48:51 -0700212 continue;
213 if (i++ == pos) {
214 ret = entry;
215 break;
216 }
217 }
218 rcu_read_unlock();
219 return ret;
220}
221
Geliang Tanga914e582021-02-01 15:09:07 -0800222unsigned int mptcp_pm_get_add_addr_signal_max(struct mptcp_sock *msk)
223{
224 struct pm_nl_pernet *pernet;
225
226 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
227 return READ_ONCE(pernet->add_addr_signal_max);
228}
229EXPORT_SYMBOL_GPL(mptcp_pm_get_add_addr_signal_max);
230
231unsigned int mptcp_pm_get_add_addr_accept_max(struct mptcp_sock *msk)
232{
233 struct pm_nl_pernet *pernet;
234
235 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
236 return READ_ONCE(pernet->add_addr_accept_max);
237}
238EXPORT_SYMBOL_GPL(mptcp_pm_get_add_addr_accept_max);
239
240unsigned int mptcp_pm_get_subflows_max(struct mptcp_sock *msk)
241{
242 struct pm_nl_pernet *pernet;
243
244 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
245 return READ_ONCE(pernet->subflows_max);
246}
247EXPORT_SYMBOL_GPL(mptcp_pm_get_subflows_max);
248
Geliang Tang0caf3ad2021-02-12 16:52:02 -0800249unsigned int mptcp_pm_get_local_addr_max(struct mptcp_sock *msk)
Geliang Tanga914e582021-02-01 15:09:07 -0800250{
251 struct pm_nl_pernet *pernet;
252
253 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
254 return READ_ONCE(pernet->local_addr_max);
255}
Geliang Tang0caf3ad2021-02-12 16:52:02 -0800256EXPORT_SYMBOL_GPL(mptcp_pm_get_local_addr_max);
Geliang Tanga914e582021-02-01 15:09:07 -0800257
Paolo Abeni01cacb02020-03-27 14:48:51 -0700258static void check_work_pending(struct mptcp_sock *msk)
259{
Geliang Tanga914e582021-02-01 15:09:07 -0800260 if (msk->pm.add_addr_signaled == mptcp_pm_get_add_addr_signal_max(msk) &&
261 (msk->pm.local_addr_used == mptcp_pm_get_local_addr_max(msk) ||
262 msk->pm.subflows == mptcp_pm_get_subflows_max(msk)))
Paolo Abeni01cacb02020-03-27 14:48:51 -0700263 WRITE_ONCE(msk->pm.work_pending, false);
264}
265
Geliang Tangd88c4762021-03-26 11:26:37 -0700266struct mptcp_pm_add_entry *
267mptcp_lookup_anno_list_by_saddr(struct mptcp_sock *msk,
268 struct mptcp_addr_info *addr)
Geliang Tangb6c08382020-09-24 08:29:54 +0800269{
Geliang Tang0abd40f2020-09-24 08:30:00 +0800270 struct mptcp_pm_add_entry *entry;
Geliang Tangb6c08382020-09-24 08:29:54 +0800271
Florian Westphal3abc05d2021-02-04 15:23:30 -0800272 lockdep_assert_held(&msk->pm.lock);
273
Geliang Tangb6c08382020-09-24 08:29:54 +0800274 list_for_each_entry(entry, &msk->pm.anno_list, list) {
Geliang Tang60b57bf2021-02-01 15:09:16 -0800275 if (addresses_equal(&entry->addr, addr, true))
Geliang Tang00cfd772020-09-24 08:30:02 +0800276 return entry;
Geliang Tangb6c08382020-09-24 08:29:54 +0800277 }
278
Geliang Tang00cfd772020-09-24 08:30:02 +0800279 return NULL;
280}
281
Geliang Tang5bc56382021-02-01 15:09:15 -0800282bool mptcp_pm_sport_in_anno_list(struct mptcp_sock *msk, const struct sock *sk)
283{
284 struct mptcp_pm_add_entry *entry;
285 struct mptcp_addr_info saddr;
286 bool ret = false;
287
288 local_address((struct sock_common *)sk, &saddr);
289
290 spin_lock_bh(&msk->pm.lock);
291 list_for_each_entry(entry, &msk->pm.anno_list, list) {
292 if (addresses_equal(&entry->addr, &saddr, true)) {
293 ret = true;
294 goto out;
295 }
296 }
297
298out:
299 spin_unlock_bh(&msk->pm.lock);
300 return ret;
301}
302
Geliang Tang00cfd772020-09-24 08:30:02 +0800303static void mptcp_pm_add_timer(struct timer_list *timer)
304{
305 struct mptcp_pm_add_entry *entry = from_timer(entry, timer, add_timer);
306 struct mptcp_sock *msk = entry->sock;
307 struct sock *sk = (struct sock *)msk;
308
309 pr_debug("msk=%p", msk);
310
311 if (!msk)
312 return;
313
314 if (inet_sk_state_load(sk) == TCP_CLOSE)
315 return;
316
317 if (!entry->addr.id)
318 return;
319
320 if (mptcp_pm_should_add_signal(msk)) {
321 sk_reset_timer(sk, timer, jiffies + TCP_RTO_MAX / 8);
322 goto out;
323 }
324
325 spin_lock_bh(&msk->pm.lock);
326
327 if (!mptcp_pm_should_add_signal(msk)) {
328 pr_debug("retransmit ADD_ADDR id=%d", entry->addr.id);
Geliang Tangf7efc772021-03-26 11:26:31 -0700329 mptcp_pm_announce_addr(msk, &entry->addr, false);
Geliang Tang84dfe362020-11-19 11:46:00 -0800330 mptcp_pm_add_addr_send_ack(msk);
Geliang Tang00cfd772020-09-24 08:30:02 +0800331 entry->retrans_times++;
332 }
333
334 if (entry->retrans_times < ADD_ADDR_RETRANS_MAX)
Geliang Tang93f323b2020-11-03 11:05:07 -0800335 sk_reset_timer(sk, timer,
Geliang Tang724d06b2020-11-10 11:01:43 +0800336 jiffies + mptcp_get_add_addr_timeout(sock_net(sk)));
Geliang Tang00cfd772020-09-24 08:30:02 +0800337
338 spin_unlock_bh(&msk->pm.lock);
339
Geliang Tang348d5c12021-03-26 11:26:34 -0700340 if (entry->retrans_times == ADD_ADDR_RETRANS_MAX)
341 mptcp_pm_subflow_established(msk);
342
Geliang Tang00cfd772020-09-24 08:30:02 +0800343out:
344 __sock_put(sk);
345}
346
347struct mptcp_pm_add_entry *
348mptcp_pm_del_add_timer(struct mptcp_sock *msk,
Davide Carattid58300c2021-05-25 14:23:13 -0700349 struct mptcp_addr_info *addr, bool check_id)
Geliang Tang00cfd772020-09-24 08:30:02 +0800350{
351 struct mptcp_pm_add_entry *entry;
352 struct sock *sk = (struct sock *)msk;
353
354 spin_lock_bh(&msk->pm.lock);
Geliang Tangd88c4762021-03-26 11:26:37 -0700355 entry = mptcp_lookup_anno_list_by_saddr(msk, addr);
Davide Carattid58300c2021-05-25 14:23:13 -0700356 if (entry && (!check_id || entry->addr.id == addr->id))
Geliang Tang00cfd772020-09-24 08:30:02 +0800357 entry->retrans_times = ADD_ADDR_RETRANS_MAX;
358 spin_unlock_bh(&msk->pm.lock);
359
Davide Carattid58300c2021-05-25 14:23:13 -0700360 if (entry && (!check_id || entry->addr.id == addr->id))
Geliang Tang00cfd772020-09-24 08:30:02 +0800361 sk_stop_timer_sync(sk, &entry->add_timer);
362
363 return entry;
Geliang Tangb6c08382020-09-24 08:29:54 +0800364}
365
366static bool mptcp_pm_alloc_anno_list(struct mptcp_sock *msk,
367 struct mptcp_pm_addr_entry *entry)
368{
Geliang Tang0abd40f2020-09-24 08:30:00 +0800369 struct mptcp_pm_add_entry *add_entry = NULL;
Geliang Tang00cfd772020-09-24 08:30:02 +0800370 struct sock *sk = (struct sock *)msk;
Geliang Tang93f323b2020-11-03 11:05:07 -0800371 struct net *net = sock_net(sk);
Geliang Tangb6c08382020-09-24 08:29:54 +0800372
Florian Westphal3abc05d2021-02-04 15:23:30 -0800373 lockdep_assert_held(&msk->pm.lock);
374
Geliang Tangd88c4762021-03-26 11:26:37 -0700375 if (mptcp_lookup_anno_list_by_saddr(msk, &entry->addr))
Geliang Tangb6c08382020-09-24 08:29:54 +0800376 return false;
377
Geliang Tang0abd40f2020-09-24 08:30:00 +0800378 add_entry = kmalloc(sizeof(*add_entry), GFP_ATOMIC);
379 if (!add_entry)
Geliang Tangb6c08382020-09-24 08:29:54 +0800380 return false;
381
Geliang Tang0abd40f2020-09-24 08:30:00 +0800382 list_add(&add_entry->list, &msk->pm.anno_list);
Geliang Tangb6c08382020-09-24 08:29:54 +0800383
Geliang Tang00cfd772020-09-24 08:30:02 +0800384 add_entry->addr = entry->addr;
385 add_entry->sock = msk;
386 add_entry->retrans_times = 0;
387
388 timer_setup(&add_entry->add_timer, mptcp_pm_add_timer, 0);
Geliang Tang93f323b2020-11-03 11:05:07 -0800389 sk_reset_timer(sk, &add_entry->add_timer,
390 jiffies + mptcp_get_add_addr_timeout(net));
Geliang Tang00cfd772020-09-24 08:30:02 +0800391
Geliang Tangb6c08382020-09-24 08:29:54 +0800392 return true;
393}
394
395void mptcp_pm_free_anno_list(struct mptcp_sock *msk)
396{
Geliang Tang0abd40f2020-09-24 08:30:00 +0800397 struct mptcp_pm_add_entry *entry, *tmp;
Geliang Tang00cfd772020-09-24 08:30:02 +0800398 struct sock *sk = (struct sock *)msk;
399 LIST_HEAD(free_list);
Geliang Tangb6c08382020-09-24 08:29:54 +0800400
401 pr_debug("msk=%p", msk);
402
403 spin_lock_bh(&msk->pm.lock);
Geliang Tang00cfd772020-09-24 08:30:02 +0800404 list_splice_init(&msk->pm.anno_list, &free_list);
405 spin_unlock_bh(&msk->pm.lock);
406
407 list_for_each_entry_safe(entry, tmp, &free_list, list) {
408 sk_stop_timer_sync(sk, &entry->add_timer);
Geliang Tangb6c08382020-09-24 08:29:54 +0800409 kfree(entry);
410 }
Geliang Tangb6c08382020-09-24 08:29:54 +0800411}
412
Geliang Tang2843ff62021-08-17 15:07:23 -0700413static bool lookup_address_in_vec(struct mptcp_addr_info *addrs, unsigned int nr,
414 struct mptcp_addr_info *addr)
415{
416 int i;
417
418 for (i = 0; i < nr; i++) {
419 if (addresses_equal(&addrs[i], addr, addr->port))
420 return true;
421 }
422
423 return false;
424}
425
426/* Fill all the remote addresses into the array addrs[],
427 * and return the array size.
428 */
429static unsigned int fill_remote_addresses_vec(struct mptcp_sock *msk, bool fullmesh,
430 struct mptcp_addr_info *addrs)
431{
432 struct sock *sk = (struct sock *)msk, *ssk;
433 struct mptcp_subflow_context *subflow;
434 struct mptcp_addr_info remote = { 0 };
435 unsigned int subflows_max;
436 int i = 0;
437
438 subflows_max = mptcp_pm_get_subflows_max(msk);
439
440 /* Non-fullmesh endpoint, fill in the single entry
441 * corresponding to the primary MPC subflow remote address
442 */
443 if (!fullmesh) {
444 remote_address((struct sock_common *)sk, &remote);
445 msk->pm.subflows++;
446 addrs[i++] = remote;
447 } else {
448 mptcp_for_each_subflow(msk, subflow) {
449 ssk = mptcp_subflow_tcp_sock(subflow);
450 remote_address((struct sock_common *)ssk, &remote);
451 if (!lookup_address_in_vec(addrs, i, &remote) &&
452 msk->pm.subflows < subflows_max) {
453 msk->pm.subflows++;
454 addrs[i++] = remote;
455 }
456 }
457 }
458
459 return i;
460}
461
Paolo Abeni01cacb02020-03-27 14:48:51 -0700462static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk)
463{
464 struct sock *sk = (struct sock *)msk;
465 struct mptcp_pm_addr_entry *local;
Geliang Tanga914e582021-02-01 15:09:07 -0800466 unsigned int add_addr_signal_max;
467 unsigned int local_addr_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700468 struct pm_nl_pernet *pernet;
Geliang Tanga914e582021-02-01 15:09:07 -0800469 unsigned int subflows_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700470
Geliang Tang432d9e72020-12-09 15:51:28 -0800471 pernet = net_generic(sock_net(sk), pm_nl_pernet_id);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700472
Geliang Tanga914e582021-02-01 15:09:07 -0800473 add_addr_signal_max = mptcp_pm_get_add_addr_signal_max(msk);
474 local_addr_max = mptcp_pm_get_local_addr_max(msk);
475 subflows_max = mptcp_pm_get_subflows_max(msk);
476
Paolo Abeni01cacb02020-03-27 14:48:51 -0700477 pr_debug("local %d:%d signal %d:%d subflows %d:%d\n",
Geliang Tanga914e582021-02-01 15:09:07 -0800478 msk->pm.local_addr_used, local_addr_max,
479 msk->pm.add_addr_signaled, add_addr_signal_max,
480 msk->pm.subflows, subflows_max);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700481
482 /* check first for announce */
Geliang Tanga914e582021-02-01 15:09:07 -0800483 if (msk->pm.add_addr_signaled < add_addr_signal_max) {
Paolo Abeni01cacb02020-03-27 14:48:51 -0700484 local = select_signal_address(pernet,
485 msk->pm.add_addr_signaled);
486
487 if (local) {
Geliang Tangb6c08382020-09-24 08:29:54 +0800488 if (mptcp_pm_alloc_anno_list(msk, local)) {
489 msk->pm.add_addr_signaled++;
Geliang Tangf7efc772021-03-26 11:26:31 -0700490 mptcp_pm_announce_addr(msk, &local->addr, false);
Geliang Tangb46a0232021-03-26 11:26:41 -0700491 mptcp_pm_nl_addr_send_ack(msk);
Geliang Tangb6c08382020-09-24 08:29:54 +0800492 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700493 } else {
494 /* pick failed, avoid fourther attempts later */
Geliang Tanga914e582021-02-01 15:09:07 -0800495 msk->pm.local_addr_used = add_addr_signal_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700496 }
497
498 check_work_pending(msk);
499 }
500
501 /* check if should create a new subflow */
Geliang Tanga914e582021-02-01 15:09:07 -0800502 if (msk->pm.local_addr_used < local_addr_max &&
Geliang Tangdf377be2021-06-22 12:25:20 -0700503 msk->pm.subflows < subflows_max &&
504 !READ_ONCE(msk->pm.remote_deny_join_id0)) {
Paolo Abeni01cacb02020-03-27 14:48:51 -0700505 local = select_local_address(pernet, msk);
506 if (local) {
Geliang Tang2843ff62021-08-17 15:07:23 -0700507 bool fullmesh = !!(local->flags & MPTCP_PM_ADDR_FLAG_FULLMESH);
508 struct mptcp_addr_info addrs[MPTCP_PM_ADDR_MAX];
509 int i, nr;
Matthieu Baerts1f2f1932021-01-25 10:59:02 -0800510
Paolo Abeni01cacb02020-03-27 14:48:51 -0700511 msk->pm.local_addr_used++;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700512 check_work_pending(msk);
Geliang Tang2843ff62021-08-17 15:07:23 -0700513 nr = fill_remote_addresses_vec(msk, fullmesh, addrs);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700514 spin_unlock_bh(&msk->pm.lock);
Geliang Tang2843ff62021-08-17 15:07:23 -0700515 for (i = 0; i < nr; i++)
516 __mptcp_subflow_connect(sk, &local->addr, &addrs[i]);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700517 spin_lock_bh(&msk->pm.lock);
518 return;
519 }
520
521 /* lookup failed, avoid fourther attempts later */
Geliang Tanga914e582021-02-01 15:09:07 -0800522 msk->pm.local_addr_used = local_addr_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700523 check_work_pending(msk);
524 }
525}
526
Florian Westphale9801432021-02-12 15:59:54 -0800527static void mptcp_pm_nl_fully_established(struct mptcp_sock *msk)
Paolo Abeni01cacb02020-03-27 14:48:51 -0700528{
529 mptcp_pm_create_subflow_or_signal_addr(msk);
530}
531
Florian Westphale9801432021-02-12 15:59:54 -0800532static void mptcp_pm_nl_subflow_established(struct mptcp_sock *msk)
Paolo Abeni01cacb02020-03-27 14:48:51 -0700533{
534 mptcp_pm_create_subflow_or_signal_addr(msk);
535}
536
Geliang Tang1a0d6132021-08-17 15:07:24 -0700537/* Fill all the local addresses into the array addrs[],
538 * and return the array size.
539 */
540static unsigned int fill_local_addresses_vec(struct mptcp_sock *msk,
541 struct mptcp_addr_info *addrs)
542{
543 struct sock *sk = (struct sock *)msk;
544 struct mptcp_pm_addr_entry *entry;
545 struct mptcp_addr_info local;
546 struct pm_nl_pernet *pernet;
547 unsigned int subflows_max;
548 int i = 0;
549
550 pernet = net_generic(sock_net(sk), pm_nl_pernet_id);
551 subflows_max = mptcp_pm_get_subflows_max(msk);
552
553 rcu_read_lock();
554 __mptcp_flush_join_list(msk);
555 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
556 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_FULLMESH))
557 continue;
558
559 if (entry->addr.family != sk->sk_family) {
560#if IS_ENABLED(CONFIG_MPTCP_IPV6)
561 if ((entry->addr.family == AF_INET &&
562 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) ||
563 (sk->sk_family == AF_INET &&
564 !ipv6_addr_v4mapped(&entry->addr.addr6)))
565#endif
566 continue;
567 }
568
569 if (msk->pm.subflows < subflows_max) {
570 msk->pm.subflows++;
571 addrs[i++] = entry->addr;
572 }
573 }
574 rcu_read_unlock();
575
576 /* If the array is empty, fill in the single
577 * 'IPADDRANY' local address
578 */
579 if (!i) {
580 memset(&local, 0, sizeof(local));
581 local.family = msk->pm.remote.family;
582
583 msk->pm.subflows++;
584 addrs[i++] = local;
585 }
586
587 return i;
588}
589
Florian Westphale9801432021-02-12 15:59:54 -0800590static void mptcp_pm_nl_add_addr_received(struct mptcp_sock *msk)
Paolo Abeni01cacb02020-03-27 14:48:51 -0700591{
Geliang Tang1a0d6132021-08-17 15:07:24 -0700592 struct mptcp_addr_info addrs[MPTCP_PM_ADDR_MAX];
Paolo Abeni01cacb02020-03-27 14:48:51 -0700593 struct sock *sk = (struct sock *)msk;
Geliang Tanga914e582021-02-01 15:09:07 -0800594 unsigned int add_addr_accept_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700595 struct mptcp_addr_info remote;
Geliang Tanga914e582021-02-01 15:09:07 -0800596 unsigned int subflows_max;
Geliang Tang1a0d6132021-08-17 15:07:24 -0700597 int i, nr;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700598
Geliang Tanga914e582021-02-01 15:09:07 -0800599 add_addr_accept_max = mptcp_pm_get_add_addr_accept_max(msk);
600 subflows_max = mptcp_pm_get_subflows_max(msk);
601
Paolo Abeni01cacb02020-03-27 14:48:51 -0700602 pr_debug("accepted %d:%d remote family %d",
Geliang Tanga914e582021-02-01 15:09:07 -0800603 msk->pm.add_addr_accepted, add_addr_accept_max,
Paolo Abeni01cacb02020-03-27 14:48:51 -0700604 msk->pm.remote.family);
Geliang Tangd84ad042021-03-26 11:26:32 -0700605
606 if (lookup_subflow_by_daddr(&msk->conn_list, &msk->pm.remote))
607 goto add_addr_echo;
608
Paolo Abeni01cacb02020-03-27 14:48:51 -0700609 /* connect to the specified remote address, using whatever
610 * local address the routing configuration will pick.
611 */
612 remote = msk->pm.remote;
613 if (!remote.port)
614 remote.port = sk->sk_dport;
Geliang Tang1a0d6132021-08-17 15:07:24 -0700615 nr = fill_local_addresses_vec(msk, addrs);
616
617 msk->pm.add_addr_accepted++;
618 if (msk->pm.add_addr_accepted >= add_addr_accept_max ||
619 msk->pm.subflows >= subflows_max)
620 WRITE_ONCE(msk->pm.accept_addr, false);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700621
622 spin_unlock_bh(&msk->pm.lock);
Geliang Tang1a0d6132021-08-17 15:07:24 -0700623 for (i = 0; i < nr; i++)
624 __mptcp_subflow_connect(sk, &addrs[i], &remote);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700625 spin_lock_bh(&msk->pm.lock);
Geliang Tang6a6c05a2020-09-24 08:29:50 +0800626
Geliang Tangd84ad042021-03-26 11:26:32 -0700627add_addr_echo:
Geliang Tangf7efc772021-03-26 11:26:31 -0700628 mptcp_pm_announce_addr(msk, &msk->pm.remote, true);
Geliang Tangb46a0232021-03-26 11:26:41 -0700629 mptcp_pm_nl_addr_send_ack(msk);
Geliang Tang84dfe362020-11-19 11:46:00 -0800630}
631
Geliang Tangb46a0232021-03-26 11:26:41 -0700632void mptcp_pm_nl_addr_send_ack(struct mptcp_sock *msk)
Geliang Tang84dfe362020-11-19 11:46:00 -0800633{
634 struct mptcp_subflow_context *subflow;
635
Florian Westphal3abc05d2021-02-04 15:23:30 -0800636 msk_owned_by_me(msk);
637 lockdep_assert_held(&msk->pm.lock);
638
Geliang Tang8dd5efb2021-03-26 11:26:40 -0700639 if (!mptcp_pm_should_add_signal(msk) &&
640 !mptcp_pm_should_rm_signal(msk))
Geliang Tang84dfe362020-11-19 11:46:00 -0800641 return;
642
643 __mptcp_flush_join_list(msk);
644 subflow = list_first_entry_or_null(&msk->conn_list, typeof(*subflow), node);
645 if (subflow) {
646 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
Paolo Abeni75e908c2021-06-21 15:54:34 -0700647 bool slow;
Geliang Tang84dfe362020-11-19 11:46:00 -0800648
649 spin_unlock_bh(&msk->pm.lock);
Geliang Tang8dd5efb2021-03-26 11:26:40 -0700650 pr_debug("send ack for %s%s%s",
651 mptcp_pm_should_add_signal(msk) ? "add_addr" : "rm_addr",
Geliang Tangb5a7acd2021-02-01 15:09:09 -0800652 mptcp_pm_should_add_signal_ipv6(msk) ? " [ipv6]" : "",
653 mptcp_pm_should_add_signal_port(msk) ? " [port]" : "");
Geliang Tangfbe0f872020-12-09 15:51:23 -0800654
Paolo Abeni75e908c2021-06-21 15:54:34 -0700655 slow = lock_sock_fast(ssk);
Geliang Tang84dfe362020-11-19 11:46:00 -0800656 tcp_send_ack(ssk);
Paolo Abeni75e908c2021-06-21 15:54:34 -0700657 unlock_sock_fast(ssk, slow);
Geliang Tang84dfe362020-11-19 11:46:00 -0800658 spin_lock_bh(&msk->pm.lock);
Geliang Tang84dfe362020-11-19 11:46:00 -0800659 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700660}
661
Geliang Tang06706542021-01-08 16:47:57 -0800662int mptcp_pm_nl_mp_prio_send_ack(struct mptcp_sock *msk,
663 struct mptcp_addr_info *addr,
664 u8 bkup)
665{
666 struct mptcp_subflow_context *subflow;
667
668 pr_debug("bkup=%d", bkup);
669
670 mptcp_for_each_subflow(msk, subflow) {
671 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
Geliang Tang0be2ac22021-01-08 16:48:01 -0800672 struct sock *sk = (struct sock *)msk;
Geliang Tang06706542021-01-08 16:47:57 -0800673 struct mptcp_addr_info local;
Paolo Abeni75e908c2021-06-21 15:54:34 -0700674 bool slow;
Geliang Tang06706542021-01-08 16:47:57 -0800675
676 local_address((struct sock_common *)ssk, &local);
677 if (!addresses_equal(&local, addr, addr->port))
678 continue;
679
680 subflow->backup = bkup;
681 subflow->send_mp_prio = 1;
682 subflow->request_bkup = bkup;
Geliang Tang0be2ac22021-01-08 16:48:01 -0800683 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIOTX);
Geliang Tang06706542021-01-08 16:47:57 -0800684
685 spin_unlock_bh(&msk->pm.lock);
686 pr_debug("send ack for mp_prio");
Paolo Abeni75e908c2021-06-21 15:54:34 -0700687 slow = lock_sock_fast(ssk);
Geliang Tang06706542021-01-08 16:47:57 -0800688 tcp_send_ack(ssk);
Paolo Abeni75e908c2021-06-21 15:54:34 -0700689 unlock_sock_fast(ssk, slow);
Geliang Tang06706542021-01-08 16:47:57 -0800690 spin_lock_bh(&msk->pm.lock);
691
692 return 0;
693 }
694
695 return -EINVAL;
696}
697
Geliang Tang9f12e972021-03-30 17:08:52 -0700698static void mptcp_pm_nl_rm_addr_or_subflow(struct mptcp_sock *msk,
699 const struct mptcp_rm_list *rm_list,
700 enum linux_mptcp_mib_field rm_type)
Geliang Tangd0876b22020-09-24 08:29:49 +0800701{
702 struct mptcp_subflow_context *subflow, *tmp;
703 struct sock *sk = (struct sock *)msk;
Geliang Tangd0b698c2021-03-12 17:16:15 -0800704 u8 i;
Geliang Tangd0876b22020-09-24 08:29:49 +0800705
Geliang Tang9f12e972021-03-30 17:08:52 -0700706 pr_debug("%s rm_list_nr %d",
707 rm_type == MPTCP_MIB_RMADDR ? "address" : "subflow", rm_list->nr);
Geliang Tangd0876b22020-09-24 08:29:49 +0800708
Florian Westphal3abc05d2021-02-04 15:23:30 -0800709 msk_owned_by_me(msk);
710
Geliang Tang9f12e972021-03-30 17:08:52 -0700711 if (!rm_list->nr)
Geliang Tangd0876b22020-09-24 08:29:49 +0800712 return;
713
714 if (list_empty(&msk->conn_list))
715 return;
716
Geliang Tang9f12e972021-03-30 17:08:52 -0700717 for (i = 0; i < rm_list->nr; i++) {
Geliang Tangd0b698c2021-03-12 17:16:15 -0800718 list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
719 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
720 int how = RCV_SHUTDOWN | SEND_SHUTDOWN;
Geliang Tang9f12e972021-03-30 17:08:52 -0700721 u8 id = subflow->local_id;
Geliang Tangd0876b22020-09-24 08:29:49 +0800722
Geliang Tang9f12e972021-03-30 17:08:52 -0700723 if (rm_type == MPTCP_MIB_RMADDR)
724 id = subflow->remote_id;
725
726 if (rm_list->ids[i] != id)
Geliang Tangd0b698c2021-03-12 17:16:15 -0800727 continue;
Geliang Tangd0876b22020-09-24 08:29:49 +0800728
Geliang Tang9f12e972021-03-30 17:08:52 -0700729 pr_debug(" -> %s rm_list_ids[%d]=%u local_id=%u remote_id=%u",
730 rm_type == MPTCP_MIB_RMADDR ? "address" : "subflow",
731 i, rm_list->ids[i], subflow->local_id, subflow->remote_id);
Geliang Tangd0b698c2021-03-12 17:16:15 -0800732 spin_unlock_bh(&msk->pm.lock);
733 mptcp_subflow_shutdown(sk, ssk, how);
734 mptcp_close_ssk(sk, ssk, subflow);
735 spin_lock_bh(&msk->pm.lock);
Geliang Tangd0876b22020-09-24 08:29:49 +0800736
Geliang Tang9f12e972021-03-30 17:08:52 -0700737 if (rm_type == MPTCP_MIB_RMADDR) {
738 msk->pm.add_addr_accepted--;
739 WRITE_ONCE(msk->pm.accept_addr, true);
740 } else if (rm_type == MPTCP_MIB_RMSUBFLOW) {
741 msk->pm.local_addr_used--;
742 }
Geliang Tangd0b698c2021-03-12 17:16:15 -0800743 msk->pm.subflows--;
Geliang Tang9f12e972021-03-30 17:08:52 -0700744 __MPTCP_INC_STATS(sock_net(sk), rm_type);
Geliang Tangd0b698c2021-03-12 17:16:15 -0800745 }
Geliang Tangd0876b22020-09-24 08:29:49 +0800746 }
747}
748
Geliang Tang9f12e972021-03-30 17:08:52 -0700749static void mptcp_pm_nl_rm_addr_received(struct mptcp_sock *msk)
750{
751 mptcp_pm_nl_rm_addr_or_subflow(msk, &msk->pm.rm_list_rx, MPTCP_MIB_RMADDR);
752}
753
754void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk,
755 const struct mptcp_rm_list *rm_list)
756{
757 mptcp_pm_nl_rm_addr_or_subflow(msk, rm_list, MPTCP_MIB_RMSUBFLOW);
758}
759
Florian Westphale9801432021-02-12 15:59:54 -0800760void mptcp_pm_nl_work(struct mptcp_sock *msk)
761{
762 struct mptcp_pm_data *pm = &msk->pm;
763
764 msk_owned_by_me(msk);
765
766 spin_lock_bh(&msk->pm.lock);
767
768 pr_debug("msk=%p status=%x", msk, pm->status);
769 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_RECEIVED)) {
770 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_RECEIVED);
771 mptcp_pm_nl_add_addr_received(msk);
772 }
773 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_SEND_ACK)) {
774 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_SEND_ACK);
Geliang Tangb46a0232021-03-26 11:26:41 -0700775 mptcp_pm_nl_addr_send_ack(msk);
Florian Westphale9801432021-02-12 15:59:54 -0800776 }
777 if (pm->status & BIT(MPTCP_PM_RM_ADDR_RECEIVED)) {
778 pm->status &= ~BIT(MPTCP_PM_RM_ADDR_RECEIVED);
779 mptcp_pm_nl_rm_addr_received(msk);
780 }
781 if (pm->status & BIT(MPTCP_PM_ESTABLISHED)) {
782 pm->status &= ~BIT(MPTCP_PM_ESTABLISHED);
783 mptcp_pm_nl_fully_established(msk);
784 }
785 if (pm->status & BIT(MPTCP_PM_SUBFLOW_ESTABLISHED)) {
786 pm->status &= ~BIT(MPTCP_PM_SUBFLOW_ESTABLISHED);
787 mptcp_pm_nl_subflow_established(msk);
788 }
789
790 spin_unlock_bh(&msk->pm.lock);
791}
792
Paolo Abeni01cacb02020-03-27 14:48:51 -0700793static bool address_use_port(struct mptcp_pm_addr_entry *entry)
794{
Geliang Tangdaa83ab2021-04-06 17:15:57 -0700795 return (entry->flags &
Paolo Abeni01cacb02020-03-27 14:48:51 -0700796 (MPTCP_PM_ADDR_FLAG_SIGNAL | MPTCP_PM_ADDR_FLAG_SUBFLOW)) ==
797 MPTCP_PM_ADDR_FLAG_SIGNAL;
798}
799
800static int mptcp_pm_nl_append_new_local_addr(struct pm_nl_pernet *pernet,
801 struct mptcp_pm_addr_entry *entry)
802{
803 struct mptcp_pm_addr_entry *cur;
Geliang Tang72603d22021-02-01 15:09:06 -0800804 unsigned int addr_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700805 int ret = -EINVAL;
806
807 spin_lock_bh(&pernet->lock);
808 /* to keep the code simple, don't do IDR-like allocation for address ID,
809 * just bail when we exceed limits
810 */
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800811 if (pernet->next_id == MAX_ADDR_ID)
812 pernet->next_id = 1;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700813 if (pernet->addrs >= MPTCP_PM_ADDR_MAX)
814 goto out;
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800815 if (test_bit(entry->addr.id, pernet->id_bitmap))
816 goto out;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700817
818 /* do not insert duplicate address, differentiate on port only
819 * singled addresses
820 */
821 list_for_each_entry(cur, &pernet->local_addr_list, list) {
822 if (addresses_equal(&cur->addr, &entry->addr,
823 address_use_port(entry) &&
824 address_use_port(cur)))
825 goto out;
826 }
827
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800828 if (!entry->addr.id) {
829find_next:
830 entry->addr.id = find_next_zero_bit(pernet->id_bitmap,
831 MAX_ADDR_ID + 1,
832 pernet->next_id);
833 if ((!entry->addr.id || entry->addr.id > MAX_ADDR_ID) &&
834 pernet->next_id != 1) {
835 pernet->next_id = 1;
836 goto find_next;
837 }
838 }
839
840 if (!entry->addr.id || entry->addr.id > MAX_ADDR_ID)
841 goto out;
842
843 __set_bit(entry->addr.id, pernet->id_bitmap);
844 if (entry->addr.id > pernet->next_id)
845 pernet->next_id = entry->addr.id;
846
Geliang Tangdaa83ab2021-04-06 17:15:57 -0700847 if (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
Geliang Tang72603d22021-02-01 15:09:06 -0800848 addr_max = pernet->add_addr_signal_max;
849 WRITE_ONCE(pernet->add_addr_signal_max, addr_max + 1);
850 }
Geliang Tangdaa83ab2021-04-06 17:15:57 -0700851 if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) {
Geliang Tang72603d22021-02-01 15:09:06 -0800852 addr_max = pernet->local_addr_max;
853 WRITE_ONCE(pernet->local_addr_max, addr_max + 1);
854 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700855
Paolo Abeni01cacb02020-03-27 14:48:51 -0700856 pernet->addrs++;
857 list_add_tail_rcu(&entry->list, &pernet->local_addr_list);
858 ret = entry->addr.id;
859
860out:
861 spin_unlock_bh(&pernet->lock);
862 return ret;
863}
864
Geliang Tang1729cf12021-02-01 15:09:12 -0800865static int mptcp_pm_nl_create_listen_socket(struct sock *sk,
866 struct mptcp_pm_addr_entry *entry)
867{
868 struct sockaddr_storage addr;
869 struct mptcp_sock *msk;
870 struct socket *ssock;
871 int backlog = 1024;
872 int err;
873
874 err = sock_create_kern(sock_net(sk), entry->addr.family,
875 SOCK_STREAM, IPPROTO_MPTCP, &entry->lsk);
876 if (err)
877 return err;
878
879 msk = mptcp_sk(entry->lsk->sk);
880 if (!msk) {
881 err = -EINVAL;
882 goto out;
883 }
884
885 ssock = __mptcp_nmpc_socket(msk);
886 if (!ssock) {
887 err = -EINVAL;
888 goto out;
889 }
890
891 mptcp_info2sockaddr(&entry->addr, &addr, entry->addr.family);
892 err = kernel_bind(ssock, (struct sockaddr *)&addr,
893 sizeof(struct sockaddr_in));
894 if (err) {
895 pr_warn("kernel_bind error, err=%d", err);
896 goto out;
897 }
898
899 err = kernel_listen(ssock, backlog);
900 if (err) {
901 pr_warn("kernel_listen error, err=%d", err);
902 goto out;
903 }
904
905 return 0;
906
907out:
908 sock_release(entry->lsk);
909 return err;
910}
911
Paolo Abeni01cacb02020-03-27 14:48:51 -0700912int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc)
913{
914 struct mptcp_pm_addr_entry *entry;
915 struct mptcp_addr_info skc_local;
916 struct mptcp_addr_info msk_local;
917 struct pm_nl_pernet *pernet;
918 int ret = -1;
919
920 if (WARN_ON_ONCE(!msk))
921 return -1;
922
923 /* The 0 ID mapping is defined by the first subflow, copied into the msk
924 * addr
925 */
926 local_address((struct sock_common *)msk, &msk_local);
Geliang Tang57025812020-09-08 10:49:38 +0800927 local_address((struct sock_common *)skc, &skc_local);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700928 if (addresses_equal(&msk_local, &skc_local, false))
929 return 0;
930
Geliang Tang57025812020-09-08 10:49:38 +0800931 if (address_zero(&skc_local))
932 return 0;
933
Paolo Abeni01cacb02020-03-27 14:48:51 -0700934 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
935
936 rcu_read_lock();
937 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
Geliang Tang60b57bf2021-02-01 15:09:16 -0800938 if (addresses_equal(&entry->addr, &skc_local, entry->addr.port)) {
Paolo Abeni01cacb02020-03-27 14:48:51 -0700939 ret = entry->addr.id;
940 break;
941 }
942 }
943 rcu_read_unlock();
944 if (ret >= 0)
945 return ret;
946
947 /* address not found, add to local list */
Geliang Tangf612eb72020-09-09 11:01:24 +0800948 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700949 if (!entry)
950 return -ENOMEM;
951
Paolo Abeni01cacb02020-03-27 14:48:51 -0700952 entry->addr = skc_local;
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800953 entry->addr.id = 0;
Geliang Tang1729cf12021-02-01 15:09:12 -0800954 entry->addr.port = 0;
Geliang Tangdaa83ab2021-04-06 17:15:57 -0700955 entry->ifindex = 0;
956 entry->flags = 0;
Geliang Tang1729cf12021-02-01 15:09:12 -0800957 entry->lsk = NULL;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700958 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
959 if (ret < 0)
960 kfree(entry);
961
962 return ret;
963}
964
965void mptcp_pm_nl_data_init(struct mptcp_sock *msk)
966{
967 struct mptcp_pm_data *pm = &msk->pm;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700968 bool subflows;
969
Geliang Tanga914e582021-02-01 15:09:07 -0800970 subflows = !!mptcp_pm_get_subflows_max(msk);
971 WRITE_ONCE(pm->work_pending, (!!mptcp_pm_get_local_addr_max(msk) && subflows) ||
972 !!mptcp_pm_get_add_addr_signal_max(msk));
973 WRITE_ONCE(pm->accept_addr, !!mptcp_pm_get_add_addr_accept_max(msk) && subflows);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700974 WRITE_ONCE(pm->accept_subflow, subflows);
975}
976
Florian Westphalb911c972021-02-12 16:00:01 -0800977#define MPTCP_PM_CMD_GRP_OFFSET 0
978#define MPTCP_PM_EV_GRP_OFFSET 1
Paolo Abeni01cacb02020-03-27 14:48:51 -0700979
980static const struct genl_multicast_group mptcp_pm_mcgrps[] = {
981 [MPTCP_PM_CMD_GRP_OFFSET] = { .name = MPTCP_PM_CMD_GRP_NAME, },
Florian Westphalb911c972021-02-12 16:00:01 -0800982 [MPTCP_PM_EV_GRP_OFFSET] = { .name = MPTCP_PM_EV_GRP_NAME,
983 .flags = GENL_UNS_ADMIN_PERM,
984 },
Paolo Abeni01cacb02020-03-27 14:48:51 -0700985};
986
987static const struct nla_policy
988mptcp_pm_addr_policy[MPTCP_PM_ADDR_ATTR_MAX + 1] = {
989 [MPTCP_PM_ADDR_ATTR_FAMILY] = { .type = NLA_U16, },
990 [MPTCP_PM_ADDR_ATTR_ID] = { .type = NLA_U8, },
991 [MPTCP_PM_ADDR_ATTR_ADDR4] = { .type = NLA_U32, },
Johannes Berg81408602020-08-18 10:17:31 +0200992 [MPTCP_PM_ADDR_ATTR_ADDR6] =
993 NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
Paolo Abeni01cacb02020-03-27 14:48:51 -0700994 [MPTCP_PM_ADDR_ATTR_PORT] = { .type = NLA_U16 },
995 [MPTCP_PM_ADDR_ATTR_FLAGS] = { .type = NLA_U32 },
996 [MPTCP_PM_ADDR_ATTR_IF_IDX] = { .type = NLA_S32 },
997};
998
999static const struct nla_policy mptcp_pm_policy[MPTCP_PM_ATTR_MAX + 1] = {
1000 [MPTCP_PM_ATTR_ADDR] =
1001 NLA_POLICY_NESTED(mptcp_pm_addr_policy),
1002 [MPTCP_PM_ATTR_RCV_ADD_ADDRS] = { .type = NLA_U32, },
1003 [MPTCP_PM_ATTR_SUBFLOWS] = { .type = NLA_U32, },
1004};
1005
Paolo Abeniff5a0b42021-08-13 15:15:45 -07001006void mptcp_pm_nl_subflow_chk_stale(const struct mptcp_sock *msk, struct sock *ssk)
1007{
1008 struct mptcp_subflow_context *iter, *subflow = mptcp_subflow_ctx(ssk);
1009 struct sock *sk = (struct sock *)msk;
1010 unsigned int active_max_loss_cnt;
1011 struct net *net = sock_net(sk);
1012 unsigned int stale_loss_cnt;
1013 bool slow;
1014
1015 stale_loss_cnt = mptcp_stale_loss_cnt(net);
1016 if (subflow->stale || !stale_loss_cnt || subflow->stale_count <= stale_loss_cnt)
1017 return;
1018
1019 /* look for another available subflow not in loss state */
1020 active_max_loss_cnt = max_t(int, stale_loss_cnt - 1, 1);
1021 mptcp_for_each_subflow(msk, iter) {
1022 if (iter != subflow && mptcp_subflow_active(iter) &&
1023 iter->stale_count < active_max_loss_cnt) {
1024 /* we have some alternatives, try to mark this subflow as idle ...*/
1025 slow = lock_sock_fast(ssk);
1026 if (!tcp_rtx_and_write_queues_empty(ssk)) {
1027 subflow->stale = 1;
1028 __mptcp_retransmit_pending_data(sk);
Paolo Abenifc1b4e32021-08-13 15:15:46 -07001029 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_SUBFLOWSTALE);
Paolo Abeniff5a0b42021-08-13 15:15:45 -07001030 }
1031 unlock_sock_fast(ssk, slow);
1032
1033 /* always try to push the pending data regarless of re-injections:
1034 * we can possibly use backup subflows now, and subflow selection
1035 * is cheap under the msk socket lock
1036 */
1037 __mptcp_push_pending(sk, 0);
1038 return;
1039 }
1040 }
1041}
1042
Paolo Abeni01cacb02020-03-27 14:48:51 -07001043static int mptcp_pm_family_to_addr(int family)
1044{
1045#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1046 if (family == AF_INET6)
1047 return MPTCP_PM_ADDR_ATTR_ADDR6;
1048#endif
1049 return MPTCP_PM_ADDR_ATTR_ADDR4;
1050}
1051
1052static int mptcp_pm_parse_addr(struct nlattr *attr, struct genl_info *info,
1053 bool require_family,
1054 struct mptcp_pm_addr_entry *entry)
1055{
1056 struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1];
1057 int err, addr_addr;
1058
1059 if (!attr) {
1060 GENL_SET_ERR_MSG(info, "missing address info");
1061 return -EINVAL;
1062 }
1063
1064 /* no validation needed - was already done via nested policy */
1065 err = nla_parse_nested_deprecated(tb, MPTCP_PM_ADDR_ATTR_MAX, attr,
1066 mptcp_pm_addr_policy, info->extack);
1067 if (err)
1068 return err;
1069
1070 memset(entry, 0, sizeof(*entry));
1071 if (!tb[MPTCP_PM_ADDR_ATTR_FAMILY]) {
1072 if (!require_family)
1073 goto skip_family;
1074
1075 NL_SET_ERR_MSG_ATTR(info->extack, attr,
1076 "missing family");
1077 return -EINVAL;
1078 }
1079
1080 entry->addr.family = nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_FAMILY]);
1081 if (entry->addr.family != AF_INET
1082#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1083 && entry->addr.family != AF_INET6
1084#endif
1085 ) {
1086 NL_SET_ERR_MSG_ATTR(info->extack, attr,
1087 "unknown address family");
1088 return -EINVAL;
1089 }
1090 addr_addr = mptcp_pm_family_to_addr(entry->addr.family);
1091 if (!tb[addr_addr]) {
1092 NL_SET_ERR_MSG_ATTR(info->extack, attr,
1093 "missing address data");
1094 return -EINVAL;
1095 }
1096
1097#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1098 if (entry->addr.family == AF_INET6)
1099 entry->addr.addr6 = nla_get_in6_addr(tb[addr_addr]);
1100 else
1101#endif
1102 entry->addr.addr.s_addr = nla_get_in_addr(tb[addr_addr]);
1103
1104skip_family:
Paolo Abenief0da3b2020-09-14 10:01:15 +02001105 if (tb[MPTCP_PM_ADDR_ATTR_IF_IDX]) {
1106 u32 val = nla_get_s32(tb[MPTCP_PM_ADDR_ATTR_IF_IDX]);
1107
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001108 entry->ifindex = val;
Paolo Abenief0da3b2020-09-14 10:01:15 +02001109 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001110
1111 if (tb[MPTCP_PM_ADDR_ATTR_ID])
1112 entry->addr.id = nla_get_u8(tb[MPTCP_PM_ADDR_ATTR_ID]);
1113
1114 if (tb[MPTCP_PM_ADDR_ATTR_FLAGS])
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001115 entry->flags = nla_get_u32(tb[MPTCP_PM_ADDR_ATTR_FLAGS]);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001116
Jianguo Wueb5fb622021-05-27 16:54:28 -07001117 if (tb[MPTCP_PM_ADDR_ATTR_PORT]) {
1118 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) {
1119 NL_SET_ERR_MSG_ATTR(info->extack, attr,
1120 "flags must have signal when using port");
1121 return -EINVAL;
1122 }
Geliang Tanga77e9172021-02-01 15:09:17 -08001123 entry->addr.port = htons(nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_PORT]));
Jianguo Wueb5fb622021-05-27 16:54:28 -07001124 }
Geliang Tanga77e9172021-02-01 15:09:17 -08001125
Paolo Abeni01cacb02020-03-27 14:48:51 -07001126 return 0;
1127}
1128
1129static struct pm_nl_pernet *genl_info_pm_nl(struct genl_info *info)
1130{
1131 return net_generic(genl_info_net(info), pm_nl_pernet_id);
1132}
1133
Geliang Tang875b76712021-02-01 15:09:08 -08001134static int mptcp_nl_add_subflow_or_signal_addr(struct net *net)
1135{
1136 struct mptcp_sock *msk;
1137 long s_slot = 0, s_num = 0;
1138
1139 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1140 struct sock *sk = (struct sock *)msk;
1141
1142 if (!READ_ONCE(msk->fully_established))
1143 goto next;
1144
1145 lock_sock(sk);
1146 spin_lock_bh(&msk->pm.lock);
1147 mptcp_pm_create_subflow_or_signal_addr(msk);
1148 spin_unlock_bh(&msk->pm.lock);
1149 release_sock(sk);
1150
1151next:
1152 sock_put(sk);
1153 cond_resched();
1154 }
1155
1156 return 0;
1157}
1158
Paolo Abeni01cacb02020-03-27 14:48:51 -07001159static int mptcp_nl_cmd_add_addr(struct sk_buff *skb, struct genl_info *info)
1160{
1161 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1162 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1163 struct mptcp_pm_addr_entry addr, *entry;
1164 int ret;
1165
1166 ret = mptcp_pm_parse_addr(attr, info, true, &addr);
1167 if (ret < 0)
1168 return ret;
1169
1170 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1171 if (!entry) {
1172 GENL_SET_ERR_MSG(info, "can't allocate addr");
1173 return -ENOMEM;
1174 }
1175
1176 *entry = addr;
Geliang Tang1729cf12021-02-01 15:09:12 -08001177 if (entry->addr.port) {
1178 ret = mptcp_pm_nl_create_listen_socket(skb->sk, entry);
1179 if (ret) {
1180 GENL_SET_ERR_MSG(info, "create listen socket error");
1181 kfree(entry);
1182 return ret;
1183 }
1184 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001185 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
1186 if (ret < 0) {
1187 GENL_SET_ERR_MSG(info, "too many addresses or duplicate one");
Geliang Tang1729cf12021-02-01 15:09:12 -08001188 if (entry->lsk)
1189 sock_release(entry->lsk);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001190 kfree(entry);
1191 return ret;
1192 }
1193
Geliang Tang875b76712021-02-01 15:09:08 -08001194 mptcp_nl_add_subflow_or_signal_addr(sock_net(skb->sk));
1195
Paolo Abeni01cacb02020-03-27 14:48:51 -07001196 return 0;
1197}
1198
1199static struct mptcp_pm_addr_entry *
1200__lookup_addr_by_id(struct pm_nl_pernet *pernet, unsigned int id)
1201{
1202 struct mptcp_pm_addr_entry *entry;
1203
1204 list_for_each_entry(entry, &pernet->local_addr_list, list) {
1205 if (entry->addr.id == id)
1206 return entry;
1207 }
1208 return NULL;
1209}
1210
Geliang Tangee285252021-08-17 15:07:22 -07001211int mptcp_pm_get_flags_and_ifindex_by_id(struct net *net, unsigned int id,
1212 u8 *flags, int *ifindex)
1213{
1214 struct mptcp_pm_addr_entry *entry;
1215
1216 *flags = 0;
1217 *ifindex = 0;
1218
1219 if (id) {
1220 rcu_read_lock();
1221 entry = __lookup_addr_by_id(net_generic(net, pm_nl_pernet_id), id);
1222 if (entry) {
1223 *flags = entry->flags;
1224 *ifindex = entry->ifindex;
1225 }
1226 rcu_read_unlock();
1227 }
1228
1229 return 0;
1230}
1231
Geliang Tangb6c08382020-09-24 08:29:54 +08001232static bool remove_anno_list_by_saddr(struct mptcp_sock *msk,
1233 struct mptcp_addr_info *addr)
1234{
Geliang Tang00cfd772020-09-24 08:30:02 +08001235 struct mptcp_pm_add_entry *entry;
Geliang Tangb6c08382020-09-24 08:29:54 +08001236
Davide Carattid58300c2021-05-25 14:23:13 -07001237 entry = mptcp_pm_del_add_timer(msk, addr, false);
Geliang Tang00cfd772020-09-24 08:30:02 +08001238 if (entry) {
1239 list_del(&entry->list);
1240 kfree(entry);
1241 return true;
Geliang Tangb6c08382020-09-24 08:29:54 +08001242 }
1243
1244 return false;
1245}
1246
1247static bool mptcp_pm_remove_anno_addr(struct mptcp_sock *msk,
1248 struct mptcp_addr_info *addr,
1249 bool force)
1250{
Geliang Tangcbde2782021-03-12 17:16:12 -08001251 struct mptcp_rm_list list = { .nr = 0 };
Geliang Tangb6c08382020-09-24 08:29:54 +08001252 bool ret;
1253
Geliang Tangcbde2782021-03-12 17:16:12 -08001254 list.ids[list.nr++] = addr->id;
1255
Geliang Tangb6c08382020-09-24 08:29:54 +08001256 ret = remove_anno_list_by_saddr(msk, addr);
Geliang Tang00cfd772020-09-24 08:30:02 +08001257 if (ret || force) {
1258 spin_lock_bh(&msk->pm.lock);
Geliang Tangcbde2782021-03-12 17:16:12 -08001259 mptcp_pm_remove_addr(msk, &list);
Geliang Tang00cfd772020-09-24 08:30:02 +08001260 spin_unlock_bh(&msk->pm.lock);
1261 }
Geliang Tangb6c08382020-09-24 08:29:54 +08001262 return ret;
1263}
1264
1265static int mptcp_nl_remove_subflow_and_signal_addr(struct net *net,
1266 struct mptcp_addr_info *addr)
1267{
1268 struct mptcp_sock *msk;
1269 long s_slot = 0, s_num = 0;
Geliang Tangddd14bb2021-03-12 17:16:16 -08001270 struct mptcp_rm_list list = { .nr = 0 };
Geliang Tangb6c08382020-09-24 08:29:54 +08001271
1272 pr_debug("remove_id=%d", addr->id);
1273
Geliang Tangddd14bb2021-03-12 17:16:16 -08001274 list.ids[list.nr++] = addr->id;
1275
Geliang Tangb6c08382020-09-24 08:29:54 +08001276 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1277 struct sock *sk = (struct sock *)msk;
1278 bool remove_subflow;
1279
1280 if (list_empty(&msk->conn_list)) {
1281 mptcp_pm_remove_anno_addr(msk, addr, false);
1282 goto next;
1283 }
1284
1285 lock_sock(sk);
1286 remove_subflow = lookup_subflow_by_saddr(&msk->conn_list, addr);
1287 mptcp_pm_remove_anno_addr(msk, addr, remove_subflow);
1288 if (remove_subflow)
Geliang Tangddd14bb2021-03-12 17:16:16 -08001289 mptcp_pm_remove_subflow(msk, &list);
Geliang Tangb6c08382020-09-24 08:29:54 +08001290 release_sock(sk);
1291
1292next:
1293 sock_put(sk);
1294 cond_resched();
1295 }
1296
1297 return 0;
1298}
1299
Geliang Tang1729cf12021-02-01 15:09:12 -08001300struct addr_entry_release_work {
1301 struct rcu_work rwork;
1302 struct mptcp_pm_addr_entry *entry;
1303};
1304
1305static void mptcp_pm_release_addr_entry(struct work_struct *work)
1306{
1307 struct addr_entry_release_work *w;
1308 struct mptcp_pm_addr_entry *entry;
1309
1310 w = container_of(to_rcu_work(work), struct addr_entry_release_work, rwork);
1311 entry = w->entry;
1312 if (entry) {
1313 if (entry->lsk)
1314 sock_release(entry->lsk);
1315 kfree(entry);
1316 }
1317 kfree(w);
1318}
1319
1320static void mptcp_pm_free_addr_entry(struct mptcp_pm_addr_entry *entry)
1321{
1322 struct addr_entry_release_work *w;
1323
1324 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1325 if (w) {
1326 INIT_RCU_WORK(&w->rwork, mptcp_pm_release_addr_entry);
1327 w->entry = entry;
1328 queue_rcu_work(system_wq, &w->rwork);
1329 }
1330}
1331
Geliang Tang740d7982021-03-30 17:08:53 -07001332static int mptcp_nl_remove_id_zero_address(struct net *net,
1333 struct mptcp_addr_info *addr)
1334{
1335 struct mptcp_rm_list list = { .nr = 0 };
1336 long s_slot = 0, s_num = 0;
1337 struct mptcp_sock *msk;
1338
1339 list.ids[list.nr++] = 0;
1340
1341 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1342 struct sock *sk = (struct sock *)msk;
1343 struct mptcp_addr_info msk_local;
1344
1345 if (list_empty(&msk->conn_list))
1346 goto next;
1347
1348 local_address((struct sock_common *)msk, &msk_local);
1349 if (!addresses_equal(&msk_local, addr, addr->port))
1350 goto next;
1351
1352 lock_sock(sk);
1353 spin_lock_bh(&msk->pm.lock);
1354 mptcp_pm_remove_addr(msk, &list);
1355 mptcp_pm_nl_rm_subflow_received(msk, &list);
1356 spin_unlock_bh(&msk->pm.lock);
1357 release_sock(sk);
1358
1359next:
1360 sock_put(sk);
1361 cond_resched();
1362 }
1363
1364 return 0;
1365}
1366
Paolo Abeni01cacb02020-03-27 14:48:51 -07001367static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info)
1368{
1369 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1370 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1371 struct mptcp_pm_addr_entry addr, *entry;
Geliang Tang72603d22021-02-01 15:09:06 -08001372 unsigned int addr_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001373 int ret;
1374
1375 ret = mptcp_pm_parse_addr(attr, info, false, &addr);
1376 if (ret < 0)
1377 return ret;
1378
Geliang Tang740d7982021-03-30 17:08:53 -07001379 /* the zero id address is special: the first address used by the msk
1380 * always gets such an id, so different subflows can have different zero
1381 * id addresses. Additionally zero id is not accounted for in id_bitmap.
1382 * Let's use an 'mptcp_rm_list' instead of the common remove code.
1383 */
1384 if (addr.addr.id == 0)
1385 return mptcp_nl_remove_id_zero_address(sock_net(skb->sk), &addr.addr);
1386
Paolo Abeni01cacb02020-03-27 14:48:51 -07001387 spin_lock_bh(&pernet->lock);
1388 entry = __lookup_addr_by_id(pernet, addr.addr.id);
1389 if (!entry) {
1390 GENL_SET_ERR_MSG(info, "address not found");
Geliang Tangb6c08382020-09-24 08:29:54 +08001391 spin_unlock_bh(&pernet->lock);
1392 return -EINVAL;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001393 }
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001394 if (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
Geliang Tang72603d22021-02-01 15:09:06 -08001395 addr_max = pernet->add_addr_signal_max;
1396 WRITE_ONCE(pernet->add_addr_signal_max, addr_max - 1);
1397 }
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001398 if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) {
Geliang Tang72603d22021-02-01 15:09:06 -08001399 addr_max = pernet->local_addr_max;
1400 WRITE_ONCE(pernet->local_addr_max, addr_max - 1);
1401 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001402
1403 pernet->addrs--;
1404 list_del_rcu(&entry->list);
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001405 __clear_bit(entry->addr.id, pernet->id_bitmap);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001406 spin_unlock_bh(&pernet->lock);
Geliang Tangb6c08382020-09-24 08:29:54 +08001407
1408 mptcp_nl_remove_subflow_and_signal_addr(sock_net(skb->sk), &entry->addr);
Geliang Tang1729cf12021-02-01 15:09:12 -08001409 mptcp_pm_free_addr_entry(entry);
Geliang Tangb6c08382020-09-24 08:29:54 +08001410
Paolo Abeni01cacb02020-03-27 14:48:51 -07001411 return ret;
1412}
1413
Geliang Tang06faa222021-03-12 17:16:17 -08001414static void mptcp_pm_remove_addrs_and_subflows(struct mptcp_sock *msk,
1415 struct list_head *rm_list)
1416{
1417 struct mptcp_rm_list alist = { .nr = 0 }, slist = { .nr = 0 };
1418 struct mptcp_pm_addr_entry *entry;
1419
1420 list_for_each_entry(entry, rm_list, list) {
1421 if (lookup_subflow_by_saddr(&msk->conn_list, &entry->addr) &&
1422 alist.nr < MPTCP_RM_IDS_MAX &&
1423 slist.nr < MPTCP_RM_IDS_MAX) {
1424 alist.ids[alist.nr++] = entry->addr.id;
1425 slist.ids[slist.nr++] = entry->addr.id;
1426 } else if (remove_anno_list_by_saddr(msk, &entry->addr) &&
1427 alist.nr < MPTCP_RM_IDS_MAX) {
1428 alist.ids[alist.nr++] = entry->addr.id;
1429 }
1430 }
1431
1432 if (alist.nr) {
1433 spin_lock_bh(&msk->pm.lock);
1434 mptcp_pm_remove_addr(msk, &alist);
1435 spin_unlock_bh(&msk->pm.lock);
1436 }
1437 if (slist.nr)
1438 mptcp_pm_remove_subflow(msk, &slist);
1439}
1440
1441static void mptcp_nl_remove_addrs_list(struct net *net,
1442 struct list_head *rm_list)
1443{
1444 long s_slot = 0, s_num = 0;
1445 struct mptcp_sock *msk;
1446
1447 if (list_empty(rm_list))
1448 return;
1449
1450 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1451 struct sock *sk = (struct sock *)msk;
1452
1453 lock_sock(sk);
1454 mptcp_pm_remove_addrs_and_subflows(msk, rm_list);
1455 release_sock(sk);
1456
1457 sock_put(sk);
1458 cond_resched();
1459 }
1460}
1461
Geliang Tang0e4a3e62021-03-12 17:16:18 -08001462static void __flush_addrs(struct list_head *list)
Paolo Abeni01cacb02020-03-27 14:48:51 -07001463{
Geliang Tang141694d2020-12-10 14:24:59 -08001464 while (!list_empty(list)) {
Paolo Abeni01cacb02020-03-27 14:48:51 -07001465 struct mptcp_pm_addr_entry *cur;
1466
Geliang Tang141694d2020-12-10 14:24:59 -08001467 cur = list_entry(list->next,
Paolo Abeni01cacb02020-03-27 14:48:51 -07001468 struct mptcp_pm_addr_entry, list);
1469 list_del_rcu(&cur->list);
Geliang Tang1729cf12021-02-01 15:09:12 -08001470 mptcp_pm_free_addr_entry(cur);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001471 }
1472}
1473
1474static void __reset_counters(struct pm_nl_pernet *pernet)
1475{
Geliang Tang72603d22021-02-01 15:09:06 -08001476 WRITE_ONCE(pernet->add_addr_signal_max, 0);
1477 WRITE_ONCE(pernet->add_addr_accept_max, 0);
1478 WRITE_ONCE(pernet->local_addr_max, 0);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001479 pernet->addrs = 0;
1480}
1481
1482static int mptcp_nl_cmd_flush_addrs(struct sk_buff *skb, struct genl_info *info)
1483{
1484 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
Geliang Tang141694d2020-12-10 14:24:59 -08001485 LIST_HEAD(free_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001486
1487 spin_lock_bh(&pernet->lock);
Geliang Tang141694d2020-12-10 14:24:59 -08001488 list_splice_init(&pernet->local_addr_list, &free_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001489 __reset_counters(pernet);
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001490 pernet->next_id = 1;
1491 bitmap_zero(pernet->id_bitmap, MAX_ADDR_ID + 1);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001492 spin_unlock_bh(&pernet->lock);
Geliang Tang0e4a3e62021-03-12 17:16:18 -08001493 mptcp_nl_remove_addrs_list(sock_net(skb->sk), &free_list);
1494 __flush_addrs(&free_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001495 return 0;
1496}
1497
1498static int mptcp_nl_fill_addr(struct sk_buff *skb,
1499 struct mptcp_pm_addr_entry *entry)
1500{
1501 struct mptcp_addr_info *addr = &entry->addr;
1502 struct nlattr *attr;
1503
1504 attr = nla_nest_start(skb, MPTCP_PM_ATTR_ADDR);
1505 if (!attr)
1506 return -EMSGSIZE;
1507
1508 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_FAMILY, addr->family))
1509 goto nla_put_failure;
Geliang Tanga77e9172021-02-01 15:09:17 -08001510 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_PORT, ntohs(addr->port)))
1511 goto nla_put_failure;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001512 if (nla_put_u8(skb, MPTCP_PM_ADDR_ATTR_ID, addr->id))
1513 goto nla_put_failure;
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001514 if (nla_put_u32(skb, MPTCP_PM_ADDR_ATTR_FLAGS, entry->flags))
Paolo Abeni01cacb02020-03-27 14:48:51 -07001515 goto nla_put_failure;
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001516 if (entry->ifindex &&
1517 nla_put_s32(skb, MPTCP_PM_ADDR_ATTR_IF_IDX, entry->ifindex))
Paolo Abeni01cacb02020-03-27 14:48:51 -07001518 goto nla_put_failure;
1519
Bo YUb4e0f9a2020-04-23 10:10:03 +08001520 if (addr->family == AF_INET &&
1521 nla_put_in_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR4,
1522 addr->addr.s_addr))
1523 goto nla_put_failure;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001524#if IS_ENABLED(CONFIG_MPTCP_IPV6)
Bo YUb4e0f9a2020-04-23 10:10:03 +08001525 else if (addr->family == AF_INET6 &&
1526 nla_put_in6_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR6, &addr->addr6))
1527 goto nla_put_failure;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001528#endif
1529 nla_nest_end(skb, attr);
1530 return 0;
1531
1532nla_put_failure:
1533 nla_nest_cancel(skb, attr);
1534 return -EMSGSIZE;
1535}
1536
1537static int mptcp_nl_cmd_get_addr(struct sk_buff *skb, struct genl_info *info)
1538{
1539 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1540 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1541 struct mptcp_pm_addr_entry addr, *entry;
1542 struct sk_buff *msg;
1543 void *reply;
1544 int ret;
1545
1546 ret = mptcp_pm_parse_addr(attr, info, false, &addr);
1547 if (ret < 0)
1548 return ret;
1549
1550 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1551 if (!msg)
1552 return -ENOMEM;
1553
1554 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1555 info->genlhdr->cmd);
1556 if (!reply) {
1557 GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1558 ret = -EMSGSIZE;
1559 goto fail;
1560 }
1561
1562 spin_lock_bh(&pernet->lock);
1563 entry = __lookup_addr_by_id(pernet, addr.addr.id);
1564 if (!entry) {
1565 GENL_SET_ERR_MSG(info, "address not found");
1566 ret = -EINVAL;
1567 goto unlock_fail;
1568 }
1569
1570 ret = mptcp_nl_fill_addr(msg, entry);
1571 if (ret)
1572 goto unlock_fail;
1573
1574 genlmsg_end(msg, reply);
1575 ret = genlmsg_reply(msg, info);
1576 spin_unlock_bh(&pernet->lock);
1577 return ret;
1578
1579unlock_fail:
1580 spin_unlock_bh(&pernet->lock);
1581
1582fail:
1583 nlmsg_free(msg);
1584 return ret;
1585}
1586
1587static int mptcp_nl_cmd_dump_addrs(struct sk_buff *msg,
1588 struct netlink_callback *cb)
1589{
1590 struct net *net = sock_net(msg->sk);
1591 struct mptcp_pm_addr_entry *entry;
1592 struct pm_nl_pernet *pernet;
1593 int id = cb->args[0];
1594 void *hdr;
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001595 int i;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001596
1597 pernet = net_generic(net, pm_nl_pernet_id);
1598
1599 spin_lock_bh(&pernet->lock);
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001600 for (i = id; i < MAX_ADDR_ID + 1; i++) {
1601 if (test_bit(i, pernet->id_bitmap)) {
1602 entry = __lookup_addr_by_id(pernet, i);
1603 if (!entry)
1604 break;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001605
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001606 if (entry->addr.id <= id)
1607 continue;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001608
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001609 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid,
1610 cb->nlh->nlmsg_seq, &mptcp_genl_family,
1611 NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR);
1612 if (!hdr)
1613 break;
1614
1615 if (mptcp_nl_fill_addr(msg, entry) < 0) {
1616 genlmsg_cancel(msg, hdr);
1617 break;
1618 }
1619
1620 id = entry->addr.id;
1621 genlmsg_end(msg, hdr);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001622 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001623 }
1624 spin_unlock_bh(&pernet->lock);
1625
1626 cb->args[0] = id;
1627 return msg->len;
1628}
1629
1630static int parse_limit(struct genl_info *info, int id, unsigned int *limit)
1631{
1632 struct nlattr *attr = info->attrs[id];
1633
1634 if (!attr)
1635 return 0;
1636
1637 *limit = nla_get_u32(attr);
1638 if (*limit > MPTCP_PM_ADDR_MAX) {
1639 GENL_SET_ERR_MSG(info, "limit greater than maximum");
1640 return -EINVAL;
1641 }
1642 return 0;
1643}
1644
1645static int
1646mptcp_nl_cmd_set_limits(struct sk_buff *skb, struct genl_info *info)
1647{
1648 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1649 unsigned int rcv_addrs, subflows;
1650 int ret;
1651
1652 spin_lock_bh(&pernet->lock);
1653 rcv_addrs = pernet->add_addr_accept_max;
1654 ret = parse_limit(info, MPTCP_PM_ATTR_RCV_ADD_ADDRS, &rcv_addrs);
1655 if (ret)
1656 goto unlock;
1657
1658 subflows = pernet->subflows_max;
1659 ret = parse_limit(info, MPTCP_PM_ATTR_SUBFLOWS, &subflows);
1660 if (ret)
1661 goto unlock;
1662
1663 WRITE_ONCE(pernet->add_addr_accept_max, rcv_addrs);
1664 WRITE_ONCE(pernet->subflows_max, subflows);
1665
1666unlock:
1667 spin_unlock_bh(&pernet->lock);
1668 return ret;
1669}
1670
1671static int
1672mptcp_nl_cmd_get_limits(struct sk_buff *skb, struct genl_info *info)
1673{
1674 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1675 struct sk_buff *msg;
1676 void *reply;
1677
1678 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1679 if (!msg)
1680 return -ENOMEM;
1681
1682 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1683 MPTCP_PM_CMD_GET_LIMITS);
1684 if (!reply)
1685 goto fail;
1686
1687 if (nla_put_u32(msg, MPTCP_PM_ATTR_RCV_ADD_ADDRS,
1688 READ_ONCE(pernet->add_addr_accept_max)))
1689 goto fail;
1690
1691 if (nla_put_u32(msg, MPTCP_PM_ATTR_SUBFLOWS,
1692 READ_ONCE(pernet->subflows_max)))
1693 goto fail;
1694
1695 genlmsg_end(msg, reply);
1696 return genlmsg_reply(msg, info);
1697
1698fail:
1699 GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1700 nlmsg_free(msg);
1701 return -EMSGSIZE;
1702}
1703
Geliang Tang0f9f6962021-01-08 16:47:59 -08001704static int mptcp_nl_addr_backup(struct net *net,
1705 struct mptcp_addr_info *addr,
1706 u8 bkup)
1707{
1708 long s_slot = 0, s_num = 0;
1709 struct mptcp_sock *msk;
1710 int ret = -EINVAL;
1711
1712 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1713 struct sock *sk = (struct sock *)msk;
1714
1715 if (list_empty(&msk->conn_list))
1716 goto next;
1717
1718 lock_sock(sk);
1719 spin_lock_bh(&msk->pm.lock);
1720 ret = mptcp_pm_nl_mp_prio_send_ack(msk, addr, bkup);
1721 spin_unlock_bh(&msk->pm.lock);
1722 release_sock(sk);
1723
1724next:
1725 sock_put(sk);
1726 cond_resched();
1727 }
1728
1729 return ret;
1730}
1731
1732static int mptcp_nl_cmd_set_flags(struct sk_buff *skb, struct genl_info *info)
1733{
1734 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1735 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1736 struct mptcp_pm_addr_entry addr, *entry;
1737 struct net *net = sock_net(skb->sk);
1738 u8 bkup = 0;
1739 int ret;
1740
1741 ret = mptcp_pm_parse_addr(attr, info, true, &addr);
1742 if (ret < 0)
1743 return ret;
1744
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001745 if (addr.flags & MPTCP_PM_ADDR_FLAG_BACKUP)
Geliang Tang0f9f6962021-01-08 16:47:59 -08001746 bkup = 1;
1747
1748 list_for_each_entry(entry, &pernet->local_addr_list, list) {
1749 if (addresses_equal(&entry->addr, &addr.addr, true)) {
1750 ret = mptcp_nl_addr_backup(net, &entry->addr, bkup);
1751 if (ret)
1752 return ret;
1753
1754 if (bkup)
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001755 entry->flags |= MPTCP_PM_ADDR_FLAG_BACKUP;
Geliang Tang0f9f6962021-01-08 16:47:59 -08001756 else
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001757 entry->flags &= ~MPTCP_PM_ADDR_FLAG_BACKUP;
Geliang Tang0f9f6962021-01-08 16:47:59 -08001758 }
1759 }
1760
1761 return 0;
1762}
1763
Florian Westphalb911c972021-02-12 16:00:01 -08001764static void mptcp_nl_mcast_send(struct net *net, struct sk_buff *nlskb, gfp_t gfp)
1765{
1766 genlmsg_multicast_netns(&mptcp_genl_family, net,
1767 nlskb, 0, MPTCP_PM_EV_GRP_OFFSET, gfp);
1768}
1769
1770static int mptcp_event_add_subflow(struct sk_buff *skb, const struct sock *ssk)
1771{
1772 const struct inet_sock *issk = inet_sk(ssk);
1773 const struct mptcp_subflow_context *sf;
1774
1775 if (nla_put_u16(skb, MPTCP_ATTR_FAMILY, ssk->sk_family))
1776 return -EMSGSIZE;
1777
1778 switch (ssk->sk_family) {
1779 case AF_INET:
1780 if (nla_put_in_addr(skb, MPTCP_ATTR_SADDR4, issk->inet_saddr))
1781 return -EMSGSIZE;
1782 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, issk->inet_daddr))
1783 return -EMSGSIZE;
1784 break;
1785#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1786 case AF_INET6: {
1787 const struct ipv6_pinfo *np = inet6_sk(ssk);
1788
1789 if (nla_put_in6_addr(skb, MPTCP_ATTR_SADDR6, &np->saddr))
1790 return -EMSGSIZE;
1791 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &ssk->sk_v6_daddr))
1792 return -EMSGSIZE;
1793 break;
1794 }
1795#endif
1796 default:
1797 WARN_ON_ONCE(1);
1798 return -EMSGSIZE;
1799 }
1800
1801 if (nla_put_be16(skb, MPTCP_ATTR_SPORT, issk->inet_sport))
1802 return -EMSGSIZE;
1803 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, issk->inet_dport))
1804 return -EMSGSIZE;
1805
1806 sf = mptcp_subflow_ctx(ssk);
1807 if (WARN_ON_ONCE(!sf))
1808 return -EINVAL;
1809
1810 if (nla_put_u8(skb, MPTCP_ATTR_LOC_ID, sf->local_id))
1811 return -EMSGSIZE;
1812
1813 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, sf->remote_id))
1814 return -EMSGSIZE;
1815
1816 return 0;
1817}
1818
1819static int mptcp_event_put_token_and_ssk(struct sk_buff *skb,
1820 const struct mptcp_sock *msk,
1821 const struct sock *ssk)
1822{
1823 const struct sock *sk = (const struct sock *)msk;
1824 const struct mptcp_subflow_context *sf;
1825 u8 sk_err;
1826
1827 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1828 return -EMSGSIZE;
1829
1830 if (mptcp_event_add_subflow(skb, ssk))
1831 return -EMSGSIZE;
1832
1833 sf = mptcp_subflow_ctx(ssk);
1834 if (WARN_ON_ONCE(!sf))
1835 return -EINVAL;
1836
1837 if (nla_put_u8(skb, MPTCP_ATTR_BACKUP, sf->backup))
1838 return -EMSGSIZE;
1839
1840 if (ssk->sk_bound_dev_if &&
1841 nla_put_s32(skb, MPTCP_ATTR_IF_IDX, ssk->sk_bound_dev_if))
1842 return -EMSGSIZE;
1843
1844 sk_err = ssk->sk_err;
1845 if (sk_err && sk->sk_state == TCP_ESTABLISHED &&
1846 nla_put_u8(skb, MPTCP_ATTR_ERROR, sk_err))
1847 return -EMSGSIZE;
1848
1849 return 0;
1850}
1851
1852static int mptcp_event_sub_established(struct sk_buff *skb,
1853 const struct mptcp_sock *msk,
1854 const struct sock *ssk)
1855{
1856 return mptcp_event_put_token_and_ssk(skb, msk, ssk);
1857}
1858
1859static int mptcp_event_sub_closed(struct sk_buff *skb,
1860 const struct mptcp_sock *msk,
1861 const struct sock *ssk)
1862{
Florian Westphaldc87efd2021-04-01 16:19:44 -07001863 const struct mptcp_subflow_context *sf;
1864
Florian Westphalb911c972021-02-12 16:00:01 -08001865 if (mptcp_event_put_token_and_ssk(skb, msk, ssk))
1866 return -EMSGSIZE;
1867
Florian Westphaldc87efd2021-04-01 16:19:44 -07001868 sf = mptcp_subflow_ctx(ssk);
1869 if (!sf->reset_seen)
1870 return 0;
1871
1872 if (nla_put_u32(skb, MPTCP_ATTR_RESET_REASON, sf->reset_reason))
1873 return -EMSGSIZE;
1874
1875 if (nla_put_u32(skb, MPTCP_ATTR_RESET_FLAGS, sf->reset_transient))
1876 return -EMSGSIZE;
1877
Florian Westphalb911c972021-02-12 16:00:01 -08001878 return 0;
1879}
1880
1881static int mptcp_event_created(struct sk_buff *skb,
1882 const struct mptcp_sock *msk,
1883 const struct sock *ssk)
1884{
1885 int err = nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token);
1886
1887 if (err)
1888 return err;
1889
1890 return mptcp_event_add_subflow(skb, ssk);
1891}
1892
1893void mptcp_event_addr_removed(const struct mptcp_sock *msk, uint8_t id)
1894{
1895 struct net *net = sock_net((const struct sock *)msk);
1896 struct nlmsghdr *nlh;
1897 struct sk_buff *skb;
1898
1899 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
1900 return;
1901
1902 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1903 if (!skb)
1904 return;
1905
1906 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, MPTCP_EVENT_REMOVED);
1907 if (!nlh)
1908 goto nla_put_failure;
1909
1910 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1911 goto nla_put_failure;
1912
1913 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, id))
1914 goto nla_put_failure;
1915
1916 genlmsg_end(skb, nlh);
1917 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC);
1918 return;
1919
1920nla_put_failure:
1921 kfree_skb(skb);
1922}
1923
1924void mptcp_event_addr_announced(const struct mptcp_sock *msk,
1925 const struct mptcp_addr_info *info)
1926{
1927 struct net *net = sock_net((const struct sock *)msk);
1928 struct nlmsghdr *nlh;
1929 struct sk_buff *skb;
1930
1931 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
1932 return;
1933
1934 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1935 if (!skb)
1936 return;
1937
1938 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0,
1939 MPTCP_EVENT_ANNOUNCED);
1940 if (!nlh)
1941 goto nla_put_failure;
1942
1943 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1944 goto nla_put_failure;
1945
1946 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, info->id))
1947 goto nla_put_failure;
1948
1949 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, info->port))
1950 goto nla_put_failure;
1951
1952 switch (info->family) {
1953 case AF_INET:
1954 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, info->addr.s_addr))
1955 goto nla_put_failure;
1956 break;
1957#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1958 case AF_INET6:
1959 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &info->addr6))
1960 goto nla_put_failure;
1961 break;
1962#endif
1963 default:
1964 WARN_ON_ONCE(1);
1965 goto nla_put_failure;
1966 }
1967
1968 genlmsg_end(skb, nlh);
1969 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC);
1970 return;
1971
1972nla_put_failure:
1973 kfree_skb(skb);
1974}
1975
1976void mptcp_event(enum mptcp_event_type type, const struct mptcp_sock *msk,
1977 const struct sock *ssk, gfp_t gfp)
1978{
1979 struct net *net = sock_net((const struct sock *)msk);
1980 struct nlmsghdr *nlh;
1981 struct sk_buff *skb;
1982
1983 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
1984 return;
1985
1986 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
1987 if (!skb)
1988 return;
1989
1990 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, type);
1991 if (!nlh)
1992 goto nla_put_failure;
1993
1994 switch (type) {
1995 case MPTCP_EVENT_UNSPEC:
1996 WARN_ON_ONCE(1);
1997 break;
1998 case MPTCP_EVENT_CREATED:
1999 case MPTCP_EVENT_ESTABLISHED:
2000 if (mptcp_event_created(skb, msk, ssk) < 0)
2001 goto nla_put_failure;
2002 break;
2003 case MPTCP_EVENT_CLOSED:
2004 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token) < 0)
2005 goto nla_put_failure;
2006 break;
2007 case MPTCP_EVENT_ANNOUNCED:
2008 case MPTCP_EVENT_REMOVED:
2009 /* call mptcp_event_addr_announced()/removed instead */
2010 WARN_ON_ONCE(1);
2011 break;
2012 case MPTCP_EVENT_SUB_ESTABLISHED:
2013 case MPTCP_EVENT_SUB_PRIORITY:
2014 if (mptcp_event_sub_established(skb, msk, ssk) < 0)
2015 goto nla_put_failure;
2016 break;
2017 case MPTCP_EVENT_SUB_CLOSED:
2018 if (mptcp_event_sub_closed(skb, msk, ssk) < 0)
2019 goto nla_put_failure;
2020 break;
2021 }
2022
2023 genlmsg_end(skb, nlh);
2024 mptcp_nl_mcast_send(net, skb, gfp);
2025 return;
2026
2027nla_put_failure:
2028 kfree_skb(skb);
2029}
2030
Rikard Falkeborn674d3ab2020-10-05 01:44:16 +02002031static const struct genl_small_ops mptcp_pm_ops[] = {
Paolo Abeni01cacb02020-03-27 14:48:51 -07002032 {
2033 .cmd = MPTCP_PM_CMD_ADD_ADDR,
2034 .doit = mptcp_nl_cmd_add_addr,
2035 .flags = GENL_ADMIN_PERM,
2036 },
2037 {
2038 .cmd = MPTCP_PM_CMD_DEL_ADDR,
2039 .doit = mptcp_nl_cmd_del_addr,
2040 .flags = GENL_ADMIN_PERM,
2041 },
2042 {
2043 .cmd = MPTCP_PM_CMD_FLUSH_ADDRS,
2044 .doit = mptcp_nl_cmd_flush_addrs,
2045 .flags = GENL_ADMIN_PERM,
2046 },
2047 {
2048 .cmd = MPTCP_PM_CMD_GET_ADDR,
2049 .doit = mptcp_nl_cmd_get_addr,
2050 .dumpit = mptcp_nl_cmd_dump_addrs,
2051 },
2052 {
2053 .cmd = MPTCP_PM_CMD_SET_LIMITS,
2054 .doit = mptcp_nl_cmd_set_limits,
2055 .flags = GENL_ADMIN_PERM,
2056 },
2057 {
2058 .cmd = MPTCP_PM_CMD_GET_LIMITS,
2059 .doit = mptcp_nl_cmd_get_limits,
2060 },
Geliang Tang0f9f6962021-01-08 16:47:59 -08002061 {
2062 .cmd = MPTCP_PM_CMD_SET_FLAGS,
2063 .doit = mptcp_nl_cmd_set_flags,
2064 .flags = GENL_ADMIN_PERM,
2065 },
Paolo Abeni01cacb02020-03-27 14:48:51 -07002066};
2067
2068static struct genl_family mptcp_genl_family __ro_after_init = {
2069 .name = MPTCP_PM_NAME,
2070 .version = MPTCP_PM_VER,
2071 .maxattr = MPTCP_PM_ATTR_MAX,
2072 .policy = mptcp_pm_policy,
2073 .netnsok = true,
2074 .module = THIS_MODULE,
Jakub Kicinski66a9b922020-10-02 14:49:54 -07002075 .small_ops = mptcp_pm_ops,
2076 .n_small_ops = ARRAY_SIZE(mptcp_pm_ops),
Paolo Abeni01cacb02020-03-27 14:48:51 -07002077 .mcgrps = mptcp_pm_mcgrps,
2078 .n_mcgrps = ARRAY_SIZE(mptcp_pm_mcgrps),
2079};
2080
2081static int __net_init pm_nl_init_net(struct net *net)
2082{
2083 struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id);
2084
2085 INIT_LIST_HEAD_RCU(&pernet->local_addr_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07002086 pernet->next_id = 1;
Paolo Abeniff5a0b42021-08-13 15:15:45 -07002087 pernet->stale_loss_cnt = 4;
Paolo Abeni01cacb02020-03-27 14:48:51 -07002088 spin_lock_init(&pernet->lock);
Jianguo Wuae514982021-05-27 16:54:27 -07002089
2090 /* No need to initialize other pernet fields, the struct is zeroed at
2091 * allocation time.
2092 */
2093
Paolo Abeni01cacb02020-03-27 14:48:51 -07002094 return 0;
2095}
2096
2097static void __net_exit pm_nl_exit_net(struct list_head *net_list)
2098{
2099 struct net *net;
2100
2101 list_for_each_entry(net, net_list, exit_list) {
Geliang Tang141694d2020-12-10 14:24:59 -08002102 struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id);
2103
Paolo Abeni01cacb02020-03-27 14:48:51 -07002104 /* net is removed from namespace list, can't race with
2105 * other modifiers
2106 */
Geliang Tang0e4a3e62021-03-12 17:16:18 -08002107 __flush_addrs(&pernet->local_addr_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07002108 }
2109}
2110
2111static struct pernet_operations mptcp_pm_pernet_ops = {
2112 .init = pm_nl_init_net,
2113 .exit_batch = pm_nl_exit_net,
2114 .id = &pm_nl_pernet_id,
2115 .size = sizeof(struct pm_nl_pernet),
2116};
2117
Paolo Abenid39dcec2020-06-26 19:29:59 +02002118void __init mptcp_pm_nl_init(void)
Paolo Abeni01cacb02020-03-27 14:48:51 -07002119{
2120 if (register_pernet_subsys(&mptcp_pm_pernet_ops) < 0)
2121 panic("Failed to register MPTCP PM pernet subsystem.\n");
2122
2123 if (genl_register_family(&mptcp_genl_family))
2124 panic("Failed to register MPTCP PM netlink family\n");
2125}