blob: bf5b9b475bb7ad9b220435191333e8afedfe9fdc [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
Florian Westphale9801432021-02-12 15:59:54 -0800537static void mptcp_pm_nl_add_addr_received(struct mptcp_sock *msk)
Paolo Abeni01cacb02020-03-27 14:48:51 -0700538{
539 struct sock *sk = (struct sock *)msk;
Geliang Tanga914e582021-02-01 15:09:07 -0800540 unsigned int add_addr_accept_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700541 struct mptcp_addr_info remote;
542 struct mptcp_addr_info local;
Geliang Tanga914e582021-02-01 15:09:07 -0800543 unsigned int subflows_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700544
Geliang Tanga914e582021-02-01 15:09:07 -0800545 add_addr_accept_max = mptcp_pm_get_add_addr_accept_max(msk);
546 subflows_max = mptcp_pm_get_subflows_max(msk);
547
Paolo Abeni01cacb02020-03-27 14:48:51 -0700548 pr_debug("accepted %d:%d remote family %d",
Geliang Tanga914e582021-02-01 15:09:07 -0800549 msk->pm.add_addr_accepted, add_addr_accept_max,
Paolo Abeni01cacb02020-03-27 14:48:51 -0700550 msk->pm.remote.family);
Geliang Tangd84ad042021-03-26 11:26:32 -0700551
552 if (lookup_subflow_by_daddr(&msk->conn_list, &msk->pm.remote))
553 goto add_addr_echo;
554
Paolo Abeni01cacb02020-03-27 14:48:51 -0700555 msk->pm.add_addr_accepted++;
556 msk->pm.subflows++;
Geliang Tanga914e582021-02-01 15:09:07 -0800557 if (msk->pm.add_addr_accepted >= add_addr_accept_max ||
558 msk->pm.subflows >= subflows_max)
Paolo Abeni01cacb02020-03-27 14:48:51 -0700559 WRITE_ONCE(msk->pm.accept_addr, false);
560
561 /* connect to the specified remote address, using whatever
562 * local address the routing configuration will pick.
563 */
564 remote = msk->pm.remote;
565 if (!remote.port)
566 remote.port = sk->sk_dport;
567 memset(&local, 0, sizeof(local));
568 local.family = remote.family;
569
570 spin_unlock_bh(&msk->pm.lock);
Geliang Tangee285252021-08-17 15:07:22 -0700571 __mptcp_subflow_connect(sk, &local, &remote);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700572 spin_lock_bh(&msk->pm.lock);
Geliang Tang6a6c05a2020-09-24 08:29:50 +0800573
Geliang Tangd84ad042021-03-26 11:26:32 -0700574add_addr_echo:
Geliang Tangf7efc772021-03-26 11:26:31 -0700575 mptcp_pm_announce_addr(msk, &msk->pm.remote, true);
Geliang Tangb46a0232021-03-26 11:26:41 -0700576 mptcp_pm_nl_addr_send_ack(msk);
Geliang Tang84dfe362020-11-19 11:46:00 -0800577}
578
Geliang Tangb46a0232021-03-26 11:26:41 -0700579void mptcp_pm_nl_addr_send_ack(struct mptcp_sock *msk)
Geliang Tang84dfe362020-11-19 11:46:00 -0800580{
581 struct mptcp_subflow_context *subflow;
582
Florian Westphal3abc05d2021-02-04 15:23:30 -0800583 msk_owned_by_me(msk);
584 lockdep_assert_held(&msk->pm.lock);
585
Geliang Tang8dd5efb2021-03-26 11:26:40 -0700586 if (!mptcp_pm_should_add_signal(msk) &&
587 !mptcp_pm_should_rm_signal(msk))
Geliang Tang84dfe362020-11-19 11:46:00 -0800588 return;
589
590 __mptcp_flush_join_list(msk);
591 subflow = list_first_entry_or_null(&msk->conn_list, typeof(*subflow), node);
592 if (subflow) {
593 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
Paolo Abeni75e908c2021-06-21 15:54:34 -0700594 bool slow;
Geliang Tang84dfe362020-11-19 11:46:00 -0800595
596 spin_unlock_bh(&msk->pm.lock);
Geliang Tang8dd5efb2021-03-26 11:26:40 -0700597 pr_debug("send ack for %s%s%s",
598 mptcp_pm_should_add_signal(msk) ? "add_addr" : "rm_addr",
Geliang Tangb5a7acd2021-02-01 15:09:09 -0800599 mptcp_pm_should_add_signal_ipv6(msk) ? " [ipv6]" : "",
600 mptcp_pm_should_add_signal_port(msk) ? " [port]" : "");
Geliang Tangfbe0f872020-12-09 15:51:23 -0800601
Paolo Abeni75e908c2021-06-21 15:54:34 -0700602 slow = lock_sock_fast(ssk);
Geliang Tang84dfe362020-11-19 11:46:00 -0800603 tcp_send_ack(ssk);
Paolo Abeni75e908c2021-06-21 15:54:34 -0700604 unlock_sock_fast(ssk, slow);
Geliang Tang84dfe362020-11-19 11:46:00 -0800605 spin_lock_bh(&msk->pm.lock);
Geliang Tang84dfe362020-11-19 11:46:00 -0800606 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700607}
608
Geliang Tang06706542021-01-08 16:47:57 -0800609int mptcp_pm_nl_mp_prio_send_ack(struct mptcp_sock *msk,
610 struct mptcp_addr_info *addr,
611 u8 bkup)
612{
613 struct mptcp_subflow_context *subflow;
614
615 pr_debug("bkup=%d", bkup);
616
617 mptcp_for_each_subflow(msk, subflow) {
618 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
Geliang Tang0be2ac22021-01-08 16:48:01 -0800619 struct sock *sk = (struct sock *)msk;
Geliang Tang06706542021-01-08 16:47:57 -0800620 struct mptcp_addr_info local;
Paolo Abeni75e908c2021-06-21 15:54:34 -0700621 bool slow;
Geliang Tang06706542021-01-08 16:47:57 -0800622
623 local_address((struct sock_common *)ssk, &local);
624 if (!addresses_equal(&local, addr, addr->port))
625 continue;
626
627 subflow->backup = bkup;
628 subflow->send_mp_prio = 1;
629 subflow->request_bkup = bkup;
Geliang Tang0be2ac22021-01-08 16:48:01 -0800630 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIOTX);
Geliang Tang06706542021-01-08 16:47:57 -0800631
632 spin_unlock_bh(&msk->pm.lock);
633 pr_debug("send ack for mp_prio");
Paolo Abeni75e908c2021-06-21 15:54:34 -0700634 slow = lock_sock_fast(ssk);
Geliang Tang06706542021-01-08 16:47:57 -0800635 tcp_send_ack(ssk);
Paolo Abeni75e908c2021-06-21 15:54:34 -0700636 unlock_sock_fast(ssk, slow);
Geliang Tang06706542021-01-08 16:47:57 -0800637 spin_lock_bh(&msk->pm.lock);
638
639 return 0;
640 }
641
642 return -EINVAL;
643}
644
Geliang Tang9f12e972021-03-30 17:08:52 -0700645static void mptcp_pm_nl_rm_addr_or_subflow(struct mptcp_sock *msk,
646 const struct mptcp_rm_list *rm_list,
647 enum linux_mptcp_mib_field rm_type)
Geliang Tangd0876b22020-09-24 08:29:49 +0800648{
649 struct mptcp_subflow_context *subflow, *tmp;
650 struct sock *sk = (struct sock *)msk;
Geliang Tangd0b698c2021-03-12 17:16:15 -0800651 u8 i;
Geliang Tangd0876b22020-09-24 08:29:49 +0800652
Geliang Tang9f12e972021-03-30 17:08:52 -0700653 pr_debug("%s rm_list_nr %d",
654 rm_type == MPTCP_MIB_RMADDR ? "address" : "subflow", rm_list->nr);
Geliang Tangd0876b22020-09-24 08:29:49 +0800655
Florian Westphal3abc05d2021-02-04 15:23:30 -0800656 msk_owned_by_me(msk);
657
Geliang Tang9f12e972021-03-30 17:08:52 -0700658 if (!rm_list->nr)
Geliang Tangd0876b22020-09-24 08:29:49 +0800659 return;
660
661 if (list_empty(&msk->conn_list))
662 return;
663
Geliang Tang9f12e972021-03-30 17:08:52 -0700664 for (i = 0; i < rm_list->nr; i++) {
Geliang Tangd0b698c2021-03-12 17:16:15 -0800665 list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
666 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
667 int how = RCV_SHUTDOWN | SEND_SHUTDOWN;
Geliang Tang9f12e972021-03-30 17:08:52 -0700668 u8 id = subflow->local_id;
Geliang Tangd0876b22020-09-24 08:29:49 +0800669
Geliang Tang9f12e972021-03-30 17:08:52 -0700670 if (rm_type == MPTCP_MIB_RMADDR)
671 id = subflow->remote_id;
672
673 if (rm_list->ids[i] != id)
Geliang Tangd0b698c2021-03-12 17:16:15 -0800674 continue;
Geliang Tangd0876b22020-09-24 08:29:49 +0800675
Geliang Tang9f12e972021-03-30 17:08:52 -0700676 pr_debug(" -> %s rm_list_ids[%d]=%u local_id=%u remote_id=%u",
677 rm_type == MPTCP_MIB_RMADDR ? "address" : "subflow",
678 i, rm_list->ids[i], subflow->local_id, subflow->remote_id);
Geliang Tangd0b698c2021-03-12 17:16:15 -0800679 spin_unlock_bh(&msk->pm.lock);
680 mptcp_subflow_shutdown(sk, ssk, how);
681 mptcp_close_ssk(sk, ssk, subflow);
682 spin_lock_bh(&msk->pm.lock);
Geliang Tangd0876b22020-09-24 08:29:49 +0800683
Geliang Tang9f12e972021-03-30 17:08:52 -0700684 if (rm_type == MPTCP_MIB_RMADDR) {
685 msk->pm.add_addr_accepted--;
686 WRITE_ONCE(msk->pm.accept_addr, true);
687 } else if (rm_type == MPTCP_MIB_RMSUBFLOW) {
688 msk->pm.local_addr_used--;
689 }
Geliang Tangd0b698c2021-03-12 17:16:15 -0800690 msk->pm.subflows--;
Geliang Tang9f12e972021-03-30 17:08:52 -0700691 __MPTCP_INC_STATS(sock_net(sk), rm_type);
Geliang Tangd0b698c2021-03-12 17:16:15 -0800692 }
Geliang Tangd0876b22020-09-24 08:29:49 +0800693 }
694}
695
Geliang Tang9f12e972021-03-30 17:08:52 -0700696static void mptcp_pm_nl_rm_addr_received(struct mptcp_sock *msk)
697{
698 mptcp_pm_nl_rm_addr_or_subflow(msk, &msk->pm.rm_list_rx, MPTCP_MIB_RMADDR);
699}
700
701void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk,
702 const struct mptcp_rm_list *rm_list)
703{
704 mptcp_pm_nl_rm_addr_or_subflow(msk, rm_list, MPTCP_MIB_RMSUBFLOW);
705}
706
Florian Westphale9801432021-02-12 15:59:54 -0800707void mptcp_pm_nl_work(struct mptcp_sock *msk)
708{
709 struct mptcp_pm_data *pm = &msk->pm;
710
711 msk_owned_by_me(msk);
712
713 spin_lock_bh(&msk->pm.lock);
714
715 pr_debug("msk=%p status=%x", msk, pm->status);
716 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_RECEIVED)) {
717 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_RECEIVED);
718 mptcp_pm_nl_add_addr_received(msk);
719 }
720 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_SEND_ACK)) {
721 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_SEND_ACK);
Geliang Tangb46a0232021-03-26 11:26:41 -0700722 mptcp_pm_nl_addr_send_ack(msk);
Florian Westphale9801432021-02-12 15:59:54 -0800723 }
724 if (pm->status & BIT(MPTCP_PM_RM_ADDR_RECEIVED)) {
725 pm->status &= ~BIT(MPTCP_PM_RM_ADDR_RECEIVED);
726 mptcp_pm_nl_rm_addr_received(msk);
727 }
728 if (pm->status & BIT(MPTCP_PM_ESTABLISHED)) {
729 pm->status &= ~BIT(MPTCP_PM_ESTABLISHED);
730 mptcp_pm_nl_fully_established(msk);
731 }
732 if (pm->status & BIT(MPTCP_PM_SUBFLOW_ESTABLISHED)) {
733 pm->status &= ~BIT(MPTCP_PM_SUBFLOW_ESTABLISHED);
734 mptcp_pm_nl_subflow_established(msk);
735 }
736
737 spin_unlock_bh(&msk->pm.lock);
738}
739
Paolo Abeni01cacb02020-03-27 14:48:51 -0700740static bool address_use_port(struct mptcp_pm_addr_entry *entry)
741{
Geliang Tangdaa83ab2021-04-06 17:15:57 -0700742 return (entry->flags &
Paolo Abeni01cacb02020-03-27 14:48:51 -0700743 (MPTCP_PM_ADDR_FLAG_SIGNAL | MPTCP_PM_ADDR_FLAG_SUBFLOW)) ==
744 MPTCP_PM_ADDR_FLAG_SIGNAL;
745}
746
747static int mptcp_pm_nl_append_new_local_addr(struct pm_nl_pernet *pernet,
748 struct mptcp_pm_addr_entry *entry)
749{
750 struct mptcp_pm_addr_entry *cur;
Geliang Tang72603d22021-02-01 15:09:06 -0800751 unsigned int addr_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700752 int ret = -EINVAL;
753
754 spin_lock_bh(&pernet->lock);
755 /* to keep the code simple, don't do IDR-like allocation for address ID,
756 * just bail when we exceed limits
757 */
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800758 if (pernet->next_id == MAX_ADDR_ID)
759 pernet->next_id = 1;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700760 if (pernet->addrs >= MPTCP_PM_ADDR_MAX)
761 goto out;
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800762 if (test_bit(entry->addr.id, pernet->id_bitmap))
763 goto out;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700764
765 /* do not insert duplicate address, differentiate on port only
766 * singled addresses
767 */
768 list_for_each_entry(cur, &pernet->local_addr_list, list) {
769 if (addresses_equal(&cur->addr, &entry->addr,
770 address_use_port(entry) &&
771 address_use_port(cur)))
772 goto out;
773 }
774
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800775 if (!entry->addr.id) {
776find_next:
777 entry->addr.id = find_next_zero_bit(pernet->id_bitmap,
778 MAX_ADDR_ID + 1,
779 pernet->next_id);
780 if ((!entry->addr.id || entry->addr.id > MAX_ADDR_ID) &&
781 pernet->next_id != 1) {
782 pernet->next_id = 1;
783 goto find_next;
784 }
785 }
786
787 if (!entry->addr.id || entry->addr.id > MAX_ADDR_ID)
788 goto out;
789
790 __set_bit(entry->addr.id, pernet->id_bitmap);
791 if (entry->addr.id > pernet->next_id)
792 pernet->next_id = entry->addr.id;
793
Geliang Tangdaa83ab2021-04-06 17:15:57 -0700794 if (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
Geliang Tang72603d22021-02-01 15:09:06 -0800795 addr_max = pernet->add_addr_signal_max;
796 WRITE_ONCE(pernet->add_addr_signal_max, addr_max + 1);
797 }
Geliang Tangdaa83ab2021-04-06 17:15:57 -0700798 if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) {
Geliang Tang72603d22021-02-01 15:09:06 -0800799 addr_max = pernet->local_addr_max;
800 WRITE_ONCE(pernet->local_addr_max, addr_max + 1);
801 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700802
Paolo Abeni01cacb02020-03-27 14:48:51 -0700803 pernet->addrs++;
804 list_add_tail_rcu(&entry->list, &pernet->local_addr_list);
805 ret = entry->addr.id;
806
807out:
808 spin_unlock_bh(&pernet->lock);
809 return ret;
810}
811
Geliang Tang1729cf12021-02-01 15:09:12 -0800812static int mptcp_pm_nl_create_listen_socket(struct sock *sk,
813 struct mptcp_pm_addr_entry *entry)
814{
815 struct sockaddr_storage addr;
816 struct mptcp_sock *msk;
817 struct socket *ssock;
818 int backlog = 1024;
819 int err;
820
821 err = sock_create_kern(sock_net(sk), entry->addr.family,
822 SOCK_STREAM, IPPROTO_MPTCP, &entry->lsk);
823 if (err)
824 return err;
825
826 msk = mptcp_sk(entry->lsk->sk);
827 if (!msk) {
828 err = -EINVAL;
829 goto out;
830 }
831
832 ssock = __mptcp_nmpc_socket(msk);
833 if (!ssock) {
834 err = -EINVAL;
835 goto out;
836 }
837
838 mptcp_info2sockaddr(&entry->addr, &addr, entry->addr.family);
839 err = kernel_bind(ssock, (struct sockaddr *)&addr,
840 sizeof(struct sockaddr_in));
841 if (err) {
842 pr_warn("kernel_bind error, err=%d", err);
843 goto out;
844 }
845
846 err = kernel_listen(ssock, backlog);
847 if (err) {
848 pr_warn("kernel_listen error, err=%d", err);
849 goto out;
850 }
851
852 return 0;
853
854out:
855 sock_release(entry->lsk);
856 return err;
857}
858
Paolo Abeni01cacb02020-03-27 14:48:51 -0700859int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc)
860{
861 struct mptcp_pm_addr_entry *entry;
862 struct mptcp_addr_info skc_local;
863 struct mptcp_addr_info msk_local;
864 struct pm_nl_pernet *pernet;
865 int ret = -1;
866
867 if (WARN_ON_ONCE(!msk))
868 return -1;
869
870 /* The 0 ID mapping is defined by the first subflow, copied into the msk
871 * addr
872 */
873 local_address((struct sock_common *)msk, &msk_local);
Geliang Tang57025812020-09-08 10:49:38 +0800874 local_address((struct sock_common *)skc, &skc_local);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700875 if (addresses_equal(&msk_local, &skc_local, false))
876 return 0;
877
Geliang Tang57025812020-09-08 10:49:38 +0800878 if (address_zero(&skc_local))
879 return 0;
880
Paolo Abeni01cacb02020-03-27 14:48:51 -0700881 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
882
883 rcu_read_lock();
884 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
Geliang Tang60b57bf2021-02-01 15:09:16 -0800885 if (addresses_equal(&entry->addr, &skc_local, entry->addr.port)) {
Paolo Abeni01cacb02020-03-27 14:48:51 -0700886 ret = entry->addr.id;
887 break;
888 }
889 }
890 rcu_read_unlock();
891 if (ret >= 0)
892 return ret;
893
894 /* address not found, add to local list */
Geliang Tangf612eb72020-09-09 11:01:24 +0800895 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700896 if (!entry)
897 return -ENOMEM;
898
Paolo Abeni01cacb02020-03-27 14:48:51 -0700899 entry->addr = skc_local;
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800900 entry->addr.id = 0;
Geliang Tang1729cf12021-02-01 15:09:12 -0800901 entry->addr.port = 0;
Geliang Tangdaa83ab2021-04-06 17:15:57 -0700902 entry->ifindex = 0;
903 entry->flags = 0;
Geliang Tang1729cf12021-02-01 15:09:12 -0800904 entry->lsk = NULL;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700905 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
906 if (ret < 0)
907 kfree(entry);
908
909 return ret;
910}
911
912void mptcp_pm_nl_data_init(struct mptcp_sock *msk)
913{
914 struct mptcp_pm_data *pm = &msk->pm;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700915 bool subflows;
916
Geliang Tanga914e582021-02-01 15:09:07 -0800917 subflows = !!mptcp_pm_get_subflows_max(msk);
918 WRITE_ONCE(pm->work_pending, (!!mptcp_pm_get_local_addr_max(msk) && subflows) ||
919 !!mptcp_pm_get_add_addr_signal_max(msk));
920 WRITE_ONCE(pm->accept_addr, !!mptcp_pm_get_add_addr_accept_max(msk) && subflows);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700921 WRITE_ONCE(pm->accept_subflow, subflows);
922}
923
Florian Westphalb911c972021-02-12 16:00:01 -0800924#define MPTCP_PM_CMD_GRP_OFFSET 0
925#define MPTCP_PM_EV_GRP_OFFSET 1
Paolo Abeni01cacb02020-03-27 14:48:51 -0700926
927static const struct genl_multicast_group mptcp_pm_mcgrps[] = {
928 [MPTCP_PM_CMD_GRP_OFFSET] = { .name = MPTCP_PM_CMD_GRP_NAME, },
Florian Westphalb911c972021-02-12 16:00:01 -0800929 [MPTCP_PM_EV_GRP_OFFSET] = { .name = MPTCP_PM_EV_GRP_NAME,
930 .flags = GENL_UNS_ADMIN_PERM,
931 },
Paolo Abeni01cacb02020-03-27 14:48:51 -0700932};
933
934static const struct nla_policy
935mptcp_pm_addr_policy[MPTCP_PM_ADDR_ATTR_MAX + 1] = {
936 [MPTCP_PM_ADDR_ATTR_FAMILY] = { .type = NLA_U16, },
937 [MPTCP_PM_ADDR_ATTR_ID] = { .type = NLA_U8, },
938 [MPTCP_PM_ADDR_ATTR_ADDR4] = { .type = NLA_U32, },
Johannes Berg81408602020-08-18 10:17:31 +0200939 [MPTCP_PM_ADDR_ATTR_ADDR6] =
940 NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
Paolo Abeni01cacb02020-03-27 14:48:51 -0700941 [MPTCP_PM_ADDR_ATTR_PORT] = { .type = NLA_U16 },
942 [MPTCP_PM_ADDR_ATTR_FLAGS] = { .type = NLA_U32 },
943 [MPTCP_PM_ADDR_ATTR_IF_IDX] = { .type = NLA_S32 },
944};
945
946static const struct nla_policy mptcp_pm_policy[MPTCP_PM_ATTR_MAX + 1] = {
947 [MPTCP_PM_ATTR_ADDR] =
948 NLA_POLICY_NESTED(mptcp_pm_addr_policy),
949 [MPTCP_PM_ATTR_RCV_ADD_ADDRS] = { .type = NLA_U32, },
950 [MPTCP_PM_ATTR_SUBFLOWS] = { .type = NLA_U32, },
951};
952
Paolo Abeniff5a0b42021-08-13 15:15:45 -0700953void mptcp_pm_nl_subflow_chk_stale(const struct mptcp_sock *msk, struct sock *ssk)
954{
955 struct mptcp_subflow_context *iter, *subflow = mptcp_subflow_ctx(ssk);
956 struct sock *sk = (struct sock *)msk;
957 unsigned int active_max_loss_cnt;
958 struct net *net = sock_net(sk);
959 unsigned int stale_loss_cnt;
960 bool slow;
961
962 stale_loss_cnt = mptcp_stale_loss_cnt(net);
963 if (subflow->stale || !stale_loss_cnt || subflow->stale_count <= stale_loss_cnt)
964 return;
965
966 /* look for another available subflow not in loss state */
967 active_max_loss_cnt = max_t(int, stale_loss_cnt - 1, 1);
968 mptcp_for_each_subflow(msk, iter) {
969 if (iter != subflow && mptcp_subflow_active(iter) &&
970 iter->stale_count < active_max_loss_cnt) {
971 /* we have some alternatives, try to mark this subflow as idle ...*/
972 slow = lock_sock_fast(ssk);
973 if (!tcp_rtx_and_write_queues_empty(ssk)) {
974 subflow->stale = 1;
975 __mptcp_retransmit_pending_data(sk);
Paolo Abenifc1b4e32021-08-13 15:15:46 -0700976 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_SUBFLOWSTALE);
Paolo Abeniff5a0b42021-08-13 15:15:45 -0700977 }
978 unlock_sock_fast(ssk, slow);
979
980 /* always try to push the pending data regarless of re-injections:
981 * we can possibly use backup subflows now, and subflow selection
982 * is cheap under the msk socket lock
983 */
984 __mptcp_push_pending(sk, 0);
985 return;
986 }
987 }
988}
989
Paolo Abeni01cacb02020-03-27 14:48:51 -0700990static int mptcp_pm_family_to_addr(int family)
991{
992#if IS_ENABLED(CONFIG_MPTCP_IPV6)
993 if (family == AF_INET6)
994 return MPTCP_PM_ADDR_ATTR_ADDR6;
995#endif
996 return MPTCP_PM_ADDR_ATTR_ADDR4;
997}
998
999static int mptcp_pm_parse_addr(struct nlattr *attr, struct genl_info *info,
1000 bool require_family,
1001 struct mptcp_pm_addr_entry *entry)
1002{
1003 struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1];
1004 int err, addr_addr;
1005
1006 if (!attr) {
1007 GENL_SET_ERR_MSG(info, "missing address info");
1008 return -EINVAL;
1009 }
1010
1011 /* no validation needed - was already done via nested policy */
1012 err = nla_parse_nested_deprecated(tb, MPTCP_PM_ADDR_ATTR_MAX, attr,
1013 mptcp_pm_addr_policy, info->extack);
1014 if (err)
1015 return err;
1016
1017 memset(entry, 0, sizeof(*entry));
1018 if (!tb[MPTCP_PM_ADDR_ATTR_FAMILY]) {
1019 if (!require_family)
1020 goto skip_family;
1021
1022 NL_SET_ERR_MSG_ATTR(info->extack, attr,
1023 "missing family");
1024 return -EINVAL;
1025 }
1026
1027 entry->addr.family = nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_FAMILY]);
1028 if (entry->addr.family != AF_INET
1029#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1030 && entry->addr.family != AF_INET6
1031#endif
1032 ) {
1033 NL_SET_ERR_MSG_ATTR(info->extack, attr,
1034 "unknown address family");
1035 return -EINVAL;
1036 }
1037 addr_addr = mptcp_pm_family_to_addr(entry->addr.family);
1038 if (!tb[addr_addr]) {
1039 NL_SET_ERR_MSG_ATTR(info->extack, attr,
1040 "missing address data");
1041 return -EINVAL;
1042 }
1043
1044#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1045 if (entry->addr.family == AF_INET6)
1046 entry->addr.addr6 = nla_get_in6_addr(tb[addr_addr]);
1047 else
1048#endif
1049 entry->addr.addr.s_addr = nla_get_in_addr(tb[addr_addr]);
1050
1051skip_family:
Paolo Abenief0da3b2020-09-14 10:01:15 +02001052 if (tb[MPTCP_PM_ADDR_ATTR_IF_IDX]) {
1053 u32 val = nla_get_s32(tb[MPTCP_PM_ADDR_ATTR_IF_IDX]);
1054
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001055 entry->ifindex = val;
Paolo Abenief0da3b2020-09-14 10:01:15 +02001056 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001057
1058 if (tb[MPTCP_PM_ADDR_ATTR_ID])
1059 entry->addr.id = nla_get_u8(tb[MPTCP_PM_ADDR_ATTR_ID]);
1060
1061 if (tb[MPTCP_PM_ADDR_ATTR_FLAGS])
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001062 entry->flags = nla_get_u32(tb[MPTCP_PM_ADDR_ATTR_FLAGS]);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001063
Jianguo Wueb5fb622021-05-27 16:54:28 -07001064 if (tb[MPTCP_PM_ADDR_ATTR_PORT]) {
1065 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) {
1066 NL_SET_ERR_MSG_ATTR(info->extack, attr,
1067 "flags must have signal when using port");
1068 return -EINVAL;
1069 }
Geliang Tanga77e9172021-02-01 15:09:17 -08001070 entry->addr.port = htons(nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_PORT]));
Jianguo Wueb5fb622021-05-27 16:54:28 -07001071 }
Geliang Tanga77e9172021-02-01 15:09:17 -08001072
Paolo Abeni01cacb02020-03-27 14:48:51 -07001073 return 0;
1074}
1075
1076static struct pm_nl_pernet *genl_info_pm_nl(struct genl_info *info)
1077{
1078 return net_generic(genl_info_net(info), pm_nl_pernet_id);
1079}
1080
Geliang Tang875b76712021-02-01 15:09:08 -08001081static int mptcp_nl_add_subflow_or_signal_addr(struct net *net)
1082{
1083 struct mptcp_sock *msk;
1084 long s_slot = 0, s_num = 0;
1085
1086 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1087 struct sock *sk = (struct sock *)msk;
1088
1089 if (!READ_ONCE(msk->fully_established))
1090 goto next;
1091
1092 lock_sock(sk);
1093 spin_lock_bh(&msk->pm.lock);
1094 mptcp_pm_create_subflow_or_signal_addr(msk);
1095 spin_unlock_bh(&msk->pm.lock);
1096 release_sock(sk);
1097
1098next:
1099 sock_put(sk);
1100 cond_resched();
1101 }
1102
1103 return 0;
1104}
1105
Paolo Abeni01cacb02020-03-27 14:48:51 -07001106static int mptcp_nl_cmd_add_addr(struct sk_buff *skb, struct genl_info *info)
1107{
1108 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1109 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1110 struct mptcp_pm_addr_entry addr, *entry;
1111 int ret;
1112
1113 ret = mptcp_pm_parse_addr(attr, info, true, &addr);
1114 if (ret < 0)
1115 return ret;
1116
1117 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1118 if (!entry) {
1119 GENL_SET_ERR_MSG(info, "can't allocate addr");
1120 return -ENOMEM;
1121 }
1122
1123 *entry = addr;
Geliang Tang1729cf12021-02-01 15:09:12 -08001124 if (entry->addr.port) {
1125 ret = mptcp_pm_nl_create_listen_socket(skb->sk, entry);
1126 if (ret) {
1127 GENL_SET_ERR_MSG(info, "create listen socket error");
1128 kfree(entry);
1129 return ret;
1130 }
1131 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001132 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
1133 if (ret < 0) {
1134 GENL_SET_ERR_MSG(info, "too many addresses or duplicate one");
Geliang Tang1729cf12021-02-01 15:09:12 -08001135 if (entry->lsk)
1136 sock_release(entry->lsk);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001137 kfree(entry);
1138 return ret;
1139 }
1140
Geliang Tang875b76712021-02-01 15:09:08 -08001141 mptcp_nl_add_subflow_or_signal_addr(sock_net(skb->sk));
1142
Paolo Abeni01cacb02020-03-27 14:48:51 -07001143 return 0;
1144}
1145
1146static struct mptcp_pm_addr_entry *
1147__lookup_addr_by_id(struct pm_nl_pernet *pernet, unsigned int id)
1148{
1149 struct mptcp_pm_addr_entry *entry;
1150
1151 list_for_each_entry(entry, &pernet->local_addr_list, list) {
1152 if (entry->addr.id == id)
1153 return entry;
1154 }
1155 return NULL;
1156}
1157
Geliang Tangee285252021-08-17 15:07:22 -07001158int mptcp_pm_get_flags_and_ifindex_by_id(struct net *net, unsigned int id,
1159 u8 *flags, int *ifindex)
1160{
1161 struct mptcp_pm_addr_entry *entry;
1162
1163 *flags = 0;
1164 *ifindex = 0;
1165
1166 if (id) {
1167 rcu_read_lock();
1168 entry = __lookup_addr_by_id(net_generic(net, pm_nl_pernet_id), id);
1169 if (entry) {
1170 *flags = entry->flags;
1171 *ifindex = entry->ifindex;
1172 }
1173 rcu_read_unlock();
1174 }
1175
1176 return 0;
1177}
1178
Geliang Tangb6c08382020-09-24 08:29:54 +08001179static bool remove_anno_list_by_saddr(struct mptcp_sock *msk,
1180 struct mptcp_addr_info *addr)
1181{
Geliang Tang00cfd772020-09-24 08:30:02 +08001182 struct mptcp_pm_add_entry *entry;
Geliang Tangb6c08382020-09-24 08:29:54 +08001183
Davide Carattid58300c2021-05-25 14:23:13 -07001184 entry = mptcp_pm_del_add_timer(msk, addr, false);
Geliang Tang00cfd772020-09-24 08:30:02 +08001185 if (entry) {
1186 list_del(&entry->list);
1187 kfree(entry);
1188 return true;
Geliang Tangb6c08382020-09-24 08:29:54 +08001189 }
1190
1191 return false;
1192}
1193
1194static bool mptcp_pm_remove_anno_addr(struct mptcp_sock *msk,
1195 struct mptcp_addr_info *addr,
1196 bool force)
1197{
Geliang Tangcbde2782021-03-12 17:16:12 -08001198 struct mptcp_rm_list list = { .nr = 0 };
Geliang Tangb6c08382020-09-24 08:29:54 +08001199 bool ret;
1200
Geliang Tangcbde2782021-03-12 17:16:12 -08001201 list.ids[list.nr++] = addr->id;
1202
Geliang Tangb6c08382020-09-24 08:29:54 +08001203 ret = remove_anno_list_by_saddr(msk, addr);
Geliang Tang00cfd772020-09-24 08:30:02 +08001204 if (ret || force) {
1205 spin_lock_bh(&msk->pm.lock);
Geliang Tangcbde2782021-03-12 17:16:12 -08001206 mptcp_pm_remove_addr(msk, &list);
Geliang Tang00cfd772020-09-24 08:30:02 +08001207 spin_unlock_bh(&msk->pm.lock);
1208 }
Geliang Tangb6c08382020-09-24 08:29:54 +08001209 return ret;
1210}
1211
1212static int mptcp_nl_remove_subflow_and_signal_addr(struct net *net,
1213 struct mptcp_addr_info *addr)
1214{
1215 struct mptcp_sock *msk;
1216 long s_slot = 0, s_num = 0;
Geliang Tangddd14bb2021-03-12 17:16:16 -08001217 struct mptcp_rm_list list = { .nr = 0 };
Geliang Tangb6c08382020-09-24 08:29:54 +08001218
1219 pr_debug("remove_id=%d", addr->id);
1220
Geliang Tangddd14bb2021-03-12 17:16:16 -08001221 list.ids[list.nr++] = addr->id;
1222
Geliang Tangb6c08382020-09-24 08:29:54 +08001223 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1224 struct sock *sk = (struct sock *)msk;
1225 bool remove_subflow;
1226
1227 if (list_empty(&msk->conn_list)) {
1228 mptcp_pm_remove_anno_addr(msk, addr, false);
1229 goto next;
1230 }
1231
1232 lock_sock(sk);
1233 remove_subflow = lookup_subflow_by_saddr(&msk->conn_list, addr);
1234 mptcp_pm_remove_anno_addr(msk, addr, remove_subflow);
1235 if (remove_subflow)
Geliang Tangddd14bb2021-03-12 17:16:16 -08001236 mptcp_pm_remove_subflow(msk, &list);
Geliang Tangb6c08382020-09-24 08:29:54 +08001237 release_sock(sk);
1238
1239next:
1240 sock_put(sk);
1241 cond_resched();
1242 }
1243
1244 return 0;
1245}
1246
Geliang Tang1729cf12021-02-01 15:09:12 -08001247struct addr_entry_release_work {
1248 struct rcu_work rwork;
1249 struct mptcp_pm_addr_entry *entry;
1250};
1251
1252static void mptcp_pm_release_addr_entry(struct work_struct *work)
1253{
1254 struct addr_entry_release_work *w;
1255 struct mptcp_pm_addr_entry *entry;
1256
1257 w = container_of(to_rcu_work(work), struct addr_entry_release_work, rwork);
1258 entry = w->entry;
1259 if (entry) {
1260 if (entry->lsk)
1261 sock_release(entry->lsk);
1262 kfree(entry);
1263 }
1264 kfree(w);
1265}
1266
1267static void mptcp_pm_free_addr_entry(struct mptcp_pm_addr_entry *entry)
1268{
1269 struct addr_entry_release_work *w;
1270
1271 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1272 if (w) {
1273 INIT_RCU_WORK(&w->rwork, mptcp_pm_release_addr_entry);
1274 w->entry = entry;
1275 queue_rcu_work(system_wq, &w->rwork);
1276 }
1277}
1278
Geliang Tang740d7982021-03-30 17:08:53 -07001279static int mptcp_nl_remove_id_zero_address(struct net *net,
1280 struct mptcp_addr_info *addr)
1281{
1282 struct mptcp_rm_list list = { .nr = 0 };
1283 long s_slot = 0, s_num = 0;
1284 struct mptcp_sock *msk;
1285
1286 list.ids[list.nr++] = 0;
1287
1288 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1289 struct sock *sk = (struct sock *)msk;
1290 struct mptcp_addr_info msk_local;
1291
1292 if (list_empty(&msk->conn_list))
1293 goto next;
1294
1295 local_address((struct sock_common *)msk, &msk_local);
1296 if (!addresses_equal(&msk_local, addr, addr->port))
1297 goto next;
1298
1299 lock_sock(sk);
1300 spin_lock_bh(&msk->pm.lock);
1301 mptcp_pm_remove_addr(msk, &list);
1302 mptcp_pm_nl_rm_subflow_received(msk, &list);
1303 spin_unlock_bh(&msk->pm.lock);
1304 release_sock(sk);
1305
1306next:
1307 sock_put(sk);
1308 cond_resched();
1309 }
1310
1311 return 0;
1312}
1313
Paolo Abeni01cacb02020-03-27 14:48:51 -07001314static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info)
1315{
1316 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1317 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1318 struct mptcp_pm_addr_entry addr, *entry;
Geliang Tang72603d22021-02-01 15:09:06 -08001319 unsigned int addr_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001320 int ret;
1321
1322 ret = mptcp_pm_parse_addr(attr, info, false, &addr);
1323 if (ret < 0)
1324 return ret;
1325
Geliang Tang740d7982021-03-30 17:08:53 -07001326 /* the zero id address is special: the first address used by the msk
1327 * always gets such an id, so different subflows can have different zero
1328 * id addresses. Additionally zero id is not accounted for in id_bitmap.
1329 * Let's use an 'mptcp_rm_list' instead of the common remove code.
1330 */
1331 if (addr.addr.id == 0)
1332 return mptcp_nl_remove_id_zero_address(sock_net(skb->sk), &addr.addr);
1333
Paolo Abeni01cacb02020-03-27 14:48:51 -07001334 spin_lock_bh(&pernet->lock);
1335 entry = __lookup_addr_by_id(pernet, addr.addr.id);
1336 if (!entry) {
1337 GENL_SET_ERR_MSG(info, "address not found");
Geliang Tangb6c08382020-09-24 08:29:54 +08001338 spin_unlock_bh(&pernet->lock);
1339 return -EINVAL;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001340 }
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001341 if (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
Geliang Tang72603d22021-02-01 15:09:06 -08001342 addr_max = pernet->add_addr_signal_max;
1343 WRITE_ONCE(pernet->add_addr_signal_max, addr_max - 1);
1344 }
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001345 if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) {
Geliang Tang72603d22021-02-01 15:09:06 -08001346 addr_max = pernet->local_addr_max;
1347 WRITE_ONCE(pernet->local_addr_max, addr_max - 1);
1348 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001349
1350 pernet->addrs--;
1351 list_del_rcu(&entry->list);
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001352 __clear_bit(entry->addr.id, pernet->id_bitmap);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001353 spin_unlock_bh(&pernet->lock);
Geliang Tangb6c08382020-09-24 08:29:54 +08001354
1355 mptcp_nl_remove_subflow_and_signal_addr(sock_net(skb->sk), &entry->addr);
Geliang Tang1729cf12021-02-01 15:09:12 -08001356 mptcp_pm_free_addr_entry(entry);
Geliang Tangb6c08382020-09-24 08:29:54 +08001357
Paolo Abeni01cacb02020-03-27 14:48:51 -07001358 return ret;
1359}
1360
Geliang Tang06faa222021-03-12 17:16:17 -08001361static void mptcp_pm_remove_addrs_and_subflows(struct mptcp_sock *msk,
1362 struct list_head *rm_list)
1363{
1364 struct mptcp_rm_list alist = { .nr = 0 }, slist = { .nr = 0 };
1365 struct mptcp_pm_addr_entry *entry;
1366
1367 list_for_each_entry(entry, rm_list, list) {
1368 if (lookup_subflow_by_saddr(&msk->conn_list, &entry->addr) &&
1369 alist.nr < MPTCP_RM_IDS_MAX &&
1370 slist.nr < MPTCP_RM_IDS_MAX) {
1371 alist.ids[alist.nr++] = entry->addr.id;
1372 slist.ids[slist.nr++] = entry->addr.id;
1373 } else if (remove_anno_list_by_saddr(msk, &entry->addr) &&
1374 alist.nr < MPTCP_RM_IDS_MAX) {
1375 alist.ids[alist.nr++] = entry->addr.id;
1376 }
1377 }
1378
1379 if (alist.nr) {
1380 spin_lock_bh(&msk->pm.lock);
1381 mptcp_pm_remove_addr(msk, &alist);
1382 spin_unlock_bh(&msk->pm.lock);
1383 }
1384 if (slist.nr)
1385 mptcp_pm_remove_subflow(msk, &slist);
1386}
1387
1388static void mptcp_nl_remove_addrs_list(struct net *net,
1389 struct list_head *rm_list)
1390{
1391 long s_slot = 0, s_num = 0;
1392 struct mptcp_sock *msk;
1393
1394 if (list_empty(rm_list))
1395 return;
1396
1397 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1398 struct sock *sk = (struct sock *)msk;
1399
1400 lock_sock(sk);
1401 mptcp_pm_remove_addrs_and_subflows(msk, rm_list);
1402 release_sock(sk);
1403
1404 sock_put(sk);
1405 cond_resched();
1406 }
1407}
1408
Geliang Tang0e4a3e62021-03-12 17:16:18 -08001409static void __flush_addrs(struct list_head *list)
Paolo Abeni01cacb02020-03-27 14:48:51 -07001410{
Geliang Tang141694d2020-12-10 14:24:59 -08001411 while (!list_empty(list)) {
Paolo Abeni01cacb02020-03-27 14:48:51 -07001412 struct mptcp_pm_addr_entry *cur;
1413
Geliang Tang141694d2020-12-10 14:24:59 -08001414 cur = list_entry(list->next,
Paolo Abeni01cacb02020-03-27 14:48:51 -07001415 struct mptcp_pm_addr_entry, list);
1416 list_del_rcu(&cur->list);
Geliang Tang1729cf12021-02-01 15:09:12 -08001417 mptcp_pm_free_addr_entry(cur);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001418 }
1419}
1420
1421static void __reset_counters(struct pm_nl_pernet *pernet)
1422{
Geliang Tang72603d22021-02-01 15:09:06 -08001423 WRITE_ONCE(pernet->add_addr_signal_max, 0);
1424 WRITE_ONCE(pernet->add_addr_accept_max, 0);
1425 WRITE_ONCE(pernet->local_addr_max, 0);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001426 pernet->addrs = 0;
1427}
1428
1429static int mptcp_nl_cmd_flush_addrs(struct sk_buff *skb, struct genl_info *info)
1430{
1431 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
Geliang Tang141694d2020-12-10 14:24:59 -08001432 LIST_HEAD(free_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001433
1434 spin_lock_bh(&pernet->lock);
Geliang Tang141694d2020-12-10 14:24:59 -08001435 list_splice_init(&pernet->local_addr_list, &free_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001436 __reset_counters(pernet);
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001437 pernet->next_id = 1;
1438 bitmap_zero(pernet->id_bitmap, MAX_ADDR_ID + 1);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001439 spin_unlock_bh(&pernet->lock);
Geliang Tang0e4a3e62021-03-12 17:16:18 -08001440 mptcp_nl_remove_addrs_list(sock_net(skb->sk), &free_list);
1441 __flush_addrs(&free_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001442 return 0;
1443}
1444
1445static int mptcp_nl_fill_addr(struct sk_buff *skb,
1446 struct mptcp_pm_addr_entry *entry)
1447{
1448 struct mptcp_addr_info *addr = &entry->addr;
1449 struct nlattr *attr;
1450
1451 attr = nla_nest_start(skb, MPTCP_PM_ATTR_ADDR);
1452 if (!attr)
1453 return -EMSGSIZE;
1454
1455 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_FAMILY, addr->family))
1456 goto nla_put_failure;
Geliang Tanga77e9172021-02-01 15:09:17 -08001457 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_PORT, ntohs(addr->port)))
1458 goto nla_put_failure;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001459 if (nla_put_u8(skb, MPTCP_PM_ADDR_ATTR_ID, addr->id))
1460 goto nla_put_failure;
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001461 if (nla_put_u32(skb, MPTCP_PM_ADDR_ATTR_FLAGS, entry->flags))
Paolo Abeni01cacb02020-03-27 14:48:51 -07001462 goto nla_put_failure;
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001463 if (entry->ifindex &&
1464 nla_put_s32(skb, MPTCP_PM_ADDR_ATTR_IF_IDX, entry->ifindex))
Paolo Abeni01cacb02020-03-27 14:48:51 -07001465 goto nla_put_failure;
1466
Bo YUb4e0f9a2020-04-23 10:10:03 +08001467 if (addr->family == AF_INET &&
1468 nla_put_in_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR4,
1469 addr->addr.s_addr))
1470 goto nla_put_failure;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001471#if IS_ENABLED(CONFIG_MPTCP_IPV6)
Bo YUb4e0f9a2020-04-23 10:10:03 +08001472 else if (addr->family == AF_INET6 &&
1473 nla_put_in6_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR6, &addr->addr6))
1474 goto nla_put_failure;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001475#endif
1476 nla_nest_end(skb, attr);
1477 return 0;
1478
1479nla_put_failure:
1480 nla_nest_cancel(skb, attr);
1481 return -EMSGSIZE;
1482}
1483
1484static int mptcp_nl_cmd_get_addr(struct sk_buff *skb, struct genl_info *info)
1485{
1486 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1487 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1488 struct mptcp_pm_addr_entry addr, *entry;
1489 struct sk_buff *msg;
1490 void *reply;
1491 int ret;
1492
1493 ret = mptcp_pm_parse_addr(attr, info, false, &addr);
1494 if (ret < 0)
1495 return ret;
1496
1497 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1498 if (!msg)
1499 return -ENOMEM;
1500
1501 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1502 info->genlhdr->cmd);
1503 if (!reply) {
1504 GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1505 ret = -EMSGSIZE;
1506 goto fail;
1507 }
1508
1509 spin_lock_bh(&pernet->lock);
1510 entry = __lookup_addr_by_id(pernet, addr.addr.id);
1511 if (!entry) {
1512 GENL_SET_ERR_MSG(info, "address not found");
1513 ret = -EINVAL;
1514 goto unlock_fail;
1515 }
1516
1517 ret = mptcp_nl_fill_addr(msg, entry);
1518 if (ret)
1519 goto unlock_fail;
1520
1521 genlmsg_end(msg, reply);
1522 ret = genlmsg_reply(msg, info);
1523 spin_unlock_bh(&pernet->lock);
1524 return ret;
1525
1526unlock_fail:
1527 spin_unlock_bh(&pernet->lock);
1528
1529fail:
1530 nlmsg_free(msg);
1531 return ret;
1532}
1533
1534static int mptcp_nl_cmd_dump_addrs(struct sk_buff *msg,
1535 struct netlink_callback *cb)
1536{
1537 struct net *net = sock_net(msg->sk);
1538 struct mptcp_pm_addr_entry *entry;
1539 struct pm_nl_pernet *pernet;
1540 int id = cb->args[0];
1541 void *hdr;
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001542 int i;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001543
1544 pernet = net_generic(net, pm_nl_pernet_id);
1545
1546 spin_lock_bh(&pernet->lock);
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001547 for (i = id; i < MAX_ADDR_ID + 1; i++) {
1548 if (test_bit(i, pernet->id_bitmap)) {
1549 entry = __lookup_addr_by_id(pernet, i);
1550 if (!entry)
1551 break;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001552
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001553 if (entry->addr.id <= id)
1554 continue;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001555
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001556 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid,
1557 cb->nlh->nlmsg_seq, &mptcp_genl_family,
1558 NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR);
1559 if (!hdr)
1560 break;
1561
1562 if (mptcp_nl_fill_addr(msg, entry) < 0) {
1563 genlmsg_cancel(msg, hdr);
1564 break;
1565 }
1566
1567 id = entry->addr.id;
1568 genlmsg_end(msg, hdr);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001569 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001570 }
1571 spin_unlock_bh(&pernet->lock);
1572
1573 cb->args[0] = id;
1574 return msg->len;
1575}
1576
1577static int parse_limit(struct genl_info *info, int id, unsigned int *limit)
1578{
1579 struct nlattr *attr = info->attrs[id];
1580
1581 if (!attr)
1582 return 0;
1583
1584 *limit = nla_get_u32(attr);
1585 if (*limit > MPTCP_PM_ADDR_MAX) {
1586 GENL_SET_ERR_MSG(info, "limit greater than maximum");
1587 return -EINVAL;
1588 }
1589 return 0;
1590}
1591
1592static int
1593mptcp_nl_cmd_set_limits(struct sk_buff *skb, struct genl_info *info)
1594{
1595 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1596 unsigned int rcv_addrs, subflows;
1597 int ret;
1598
1599 spin_lock_bh(&pernet->lock);
1600 rcv_addrs = pernet->add_addr_accept_max;
1601 ret = parse_limit(info, MPTCP_PM_ATTR_RCV_ADD_ADDRS, &rcv_addrs);
1602 if (ret)
1603 goto unlock;
1604
1605 subflows = pernet->subflows_max;
1606 ret = parse_limit(info, MPTCP_PM_ATTR_SUBFLOWS, &subflows);
1607 if (ret)
1608 goto unlock;
1609
1610 WRITE_ONCE(pernet->add_addr_accept_max, rcv_addrs);
1611 WRITE_ONCE(pernet->subflows_max, subflows);
1612
1613unlock:
1614 spin_unlock_bh(&pernet->lock);
1615 return ret;
1616}
1617
1618static int
1619mptcp_nl_cmd_get_limits(struct sk_buff *skb, struct genl_info *info)
1620{
1621 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1622 struct sk_buff *msg;
1623 void *reply;
1624
1625 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1626 if (!msg)
1627 return -ENOMEM;
1628
1629 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1630 MPTCP_PM_CMD_GET_LIMITS);
1631 if (!reply)
1632 goto fail;
1633
1634 if (nla_put_u32(msg, MPTCP_PM_ATTR_RCV_ADD_ADDRS,
1635 READ_ONCE(pernet->add_addr_accept_max)))
1636 goto fail;
1637
1638 if (nla_put_u32(msg, MPTCP_PM_ATTR_SUBFLOWS,
1639 READ_ONCE(pernet->subflows_max)))
1640 goto fail;
1641
1642 genlmsg_end(msg, reply);
1643 return genlmsg_reply(msg, info);
1644
1645fail:
1646 GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1647 nlmsg_free(msg);
1648 return -EMSGSIZE;
1649}
1650
Geliang Tang0f9f6962021-01-08 16:47:59 -08001651static int mptcp_nl_addr_backup(struct net *net,
1652 struct mptcp_addr_info *addr,
1653 u8 bkup)
1654{
1655 long s_slot = 0, s_num = 0;
1656 struct mptcp_sock *msk;
1657 int ret = -EINVAL;
1658
1659 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1660 struct sock *sk = (struct sock *)msk;
1661
1662 if (list_empty(&msk->conn_list))
1663 goto next;
1664
1665 lock_sock(sk);
1666 spin_lock_bh(&msk->pm.lock);
1667 ret = mptcp_pm_nl_mp_prio_send_ack(msk, addr, bkup);
1668 spin_unlock_bh(&msk->pm.lock);
1669 release_sock(sk);
1670
1671next:
1672 sock_put(sk);
1673 cond_resched();
1674 }
1675
1676 return ret;
1677}
1678
1679static int mptcp_nl_cmd_set_flags(struct sk_buff *skb, struct genl_info *info)
1680{
1681 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1682 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1683 struct mptcp_pm_addr_entry addr, *entry;
1684 struct net *net = sock_net(skb->sk);
1685 u8 bkup = 0;
1686 int ret;
1687
1688 ret = mptcp_pm_parse_addr(attr, info, true, &addr);
1689 if (ret < 0)
1690 return ret;
1691
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001692 if (addr.flags & MPTCP_PM_ADDR_FLAG_BACKUP)
Geliang Tang0f9f6962021-01-08 16:47:59 -08001693 bkup = 1;
1694
1695 list_for_each_entry(entry, &pernet->local_addr_list, list) {
1696 if (addresses_equal(&entry->addr, &addr.addr, true)) {
1697 ret = mptcp_nl_addr_backup(net, &entry->addr, bkup);
1698 if (ret)
1699 return ret;
1700
1701 if (bkup)
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001702 entry->flags |= MPTCP_PM_ADDR_FLAG_BACKUP;
Geliang Tang0f9f6962021-01-08 16:47:59 -08001703 else
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001704 entry->flags &= ~MPTCP_PM_ADDR_FLAG_BACKUP;
Geliang Tang0f9f6962021-01-08 16:47:59 -08001705 }
1706 }
1707
1708 return 0;
1709}
1710
Florian Westphalb911c972021-02-12 16:00:01 -08001711static void mptcp_nl_mcast_send(struct net *net, struct sk_buff *nlskb, gfp_t gfp)
1712{
1713 genlmsg_multicast_netns(&mptcp_genl_family, net,
1714 nlskb, 0, MPTCP_PM_EV_GRP_OFFSET, gfp);
1715}
1716
1717static int mptcp_event_add_subflow(struct sk_buff *skb, const struct sock *ssk)
1718{
1719 const struct inet_sock *issk = inet_sk(ssk);
1720 const struct mptcp_subflow_context *sf;
1721
1722 if (nla_put_u16(skb, MPTCP_ATTR_FAMILY, ssk->sk_family))
1723 return -EMSGSIZE;
1724
1725 switch (ssk->sk_family) {
1726 case AF_INET:
1727 if (nla_put_in_addr(skb, MPTCP_ATTR_SADDR4, issk->inet_saddr))
1728 return -EMSGSIZE;
1729 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, issk->inet_daddr))
1730 return -EMSGSIZE;
1731 break;
1732#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1733 case AF_INET6: {
1734 const struct ipv6_pinfo *np = inet6_sk(ssk);
1735
1736 if (nla_put_in6_addr(skb, MPTCP_ATTR_SADDR6, &np->saddr))
1737 return -EMSGSIZE;
1738 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &ssk->sk_v6_daddr))
1739 return -EMSGSIZE;
1740 break;
1741 }
1742#endif
1743 default:
1744 WARN_ON_ONCE(1);
1745 return -EMSGSIZE;
1746 }
1747
1748 if (nla_put_be16(skb, MPTCP_ATTR_SPORT, issk->inet_sport))
1749 return -EMSGSIZE;
1750 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, issk->inet_dport))
1751 return -EMSGSIZE;
1752
1753 sf = mptcp_subflow_ctx(ssk);
1754 if (WARN_ON_ONCE(!sf))
1755 return -EINVAL;
1756
1757 if (nla_put_u8(skb, MPTCP_ATTR_LOC_ID, sf->local_id))
1758 return -EMSGSIZE;
1759
1760 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, sf->remote_id))
1761 return -EMSGSIZE;
1762
1763 return 0;
1764}
1765
1766static int mptcp_event_put_token_and_ssk(struct sk_buff *skb,
1767 const struct mptcp_sock *msk,
1768 const struct sock *ssk)
1769{
1770 const struct sock *sk = (const struct sock *)msk;
1771 const struct mptcp_subflow_context *sf;
1772 u8 sk_err;
1773
1774 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1775 return -EMSGSIZE;
1776
1777 if (mptcp_event_add_subflow(skb, ssk))
1778 return -EMSGSIZE;
1779
1780 sf = mptcp_subflow_ctx(ssk);
1781 if (WARN_ON_ONCE(!sf))
1782 return -EINVAL;
1783
1784 if (nla_put_u8(skb, MPTCP_ATTR_BACKUP, sf->backup))
1785 return -EMSGSIZE;
1786
1787 if (ssk->sk_bound_dev_if &&
1788 nla_put_s32(skb, MPTCP_ATTR_IF_IDX, ssk->sk_bound_dev_if))
1789 return -EMSGSIZE;
1790
1791 sk_err = ssk->sk_err;
1792 if (sk_err && sk->sk_state == TCP_ESTABLISHED &&
1793 nla_put_u8(skb, MPTCP_ATTR_ERROR, sk_err))
1794 return -EMSGSIZE;
1795
1796 return 0;
1797}
1798
1799static int mptcp_event_sub_established(struct sk_buff *skb,
1800 const struct mptcp_sock *msk,
1801 const struct sock *ssk)
1802{
1803 return mptcp_event_put_token_and_ssk(skb, msk, ssk);
1804}
1805
1806static int mptcp_event_sub_closed(struct sk_buff *skb,
1807 const struct mptcp_sock *msk,
1808 const struct sock *ssk)
1809{
Florian Westphaldc87efd2021-04-01 16:19:44 -07001810 const struct mptcp_subflow_context *sf;
1811
Florian Westphalb911c972021-02-12 16:00:01 -08001812 if (mptcp_event_put_token_and_ssk(skb, msk, ssk))
1813 return -EMSGSIZE;
1814
Florian Westphaldc87efd2021-04-01 16:19:44 -07001815 sf = mptcp_subflow_ctx(ssk);
1816 if (!sf->reset_seen)
1817 return 0;
1818
1819 if (nla_put_u32(skb, MPTCP_ATTR_RESET_REASON, sf->reset_reason))
1820 return -EMSGSIZE;
1821
1822 if (nla_put_u32(skb, MPTCP_ATTR_RESET_FLAGS, sf->reset_transient))
1823 return -EMSGSIZE;
1824
Florian Westphalb911c972021-02-12 16:00:01 -08001825 return 0;
1826}
1827
1828static int mptcp_event_created(struct sk_buff *skb,
1829 const struct mptcp_sock *msk,
1830 const struct sock *ssk)
1831{
1832 int err = nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token);
1833
1834 if (err)
1835 return err;
1836
1837 return mptcp_event_add_subflow(skb, ssk);
1838}
1839
1840void mptcp_event_addr_removed(const struct mptcp_sock *msk, uint8_t id)
1841{
1842 struct net *net = sock_net((const struct sock *)msk);
1843 struct nlmsghdr *nlh;
1844 struct sk_buff *skb;
1845
1846 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
1847 return;
1848
1849 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1850 if (!skb)
1851 return;
1852
1853 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, MPTCP_EVENT_REMOVED);
1854 if (!nlh)
1855 goto nla_put_failure;
1856
1857 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1858 goto nla_put_failure;
1859
1860 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, id))
1861 goto nla_put_failure;
1862
1863 genlmsg_end(skb, nlh);
1864 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC);
1865 return;
1866
1867nla_put_failure:
1868 kfree_skb(skb);
1869}
1870
1871void mptcp_event_addr_announced(const struct mptcp_sock *msk,
1872 const struct mptcp_addr_info *info)
1873{
1874 struct net *net = sock_net((const struct sock *)msk);
1875 struct nlmsghdr *nlh;
1876 struct sk_buff *skb;
1877
1878 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
1879 return;
1880
1881 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1882 if (!skb)
1883 return;
1884
1885 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0,
1886 MPTCP_EVENT_ANNOUNCED);
1887 if (!nlh)
1888 goto nla_put_failure;
1889
1890 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1891 goto nla_put_failure;
1892
1893 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, info->id))
1894 goto nla_put_failure;
1895
1896 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, info->port))
1897 goto nla_put_failure;
1898
1899 switch (info->family) {
1900 case AF_INET:
1901 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, info->addr.s_addr))
1902 goto nla_put_failure;
1903 break;
1904#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1905 case AF_INET6:
1906 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &info->addr6))
1907 goto nla_put_failure;
1908 break;
1909#endif
1910 default:
1911 WARN_ON_ONCE(1);
1912 goto nla_put_failure;
1913 }
1914
1915 genlmsg_end(skb, nlh);
1916 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC);
1917 return;
1918
1919nla_put_failure:
1920 kfree_skb(skb);
1921}
1922
1923void mptcp_event(enum mptcp_event_type type, const struct mptcp_sock *msk,
1924 const struct sock *ssk, gfp_t gfp)
1925{
1926 struct net *net = sock_net((const struct sock *)msk);
1927 struct nlmsghdr *nlh;
1928 struct sk_buff *skb;
1929
1930 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
1931 return;
1932
1933 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
1934 if (!skb)
1935 return;
1936
1937 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, type);
1938 if (!nlh)
1939 goto nla_put_failure;
1940
1941 switch (type) {
1942 case MPTCP_EVENT_UNSPEC:
1943 WARN_ON_ONCE(1);
1944 break;
1945 case MPTCP_EVENT_CREATED:
1946 case MPTCP_EVENT_ESTABLISHED:
1947 if (mptcp_event_created(skb, msk, ssk) < 0)
1948 goto nla_put_failure;
1949 break;
1950 case MPTCP_EVENT_CLOSED:
1951 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token) < 0)
1952 goto nla_put_failure;
1953 break;
1954 case MPTCP_EVENT_ANNOUNCED:
1955 case MPTCP_EVENT_REMOVED:
1956 /* call mptcp_event_addr_announced()/removed instead */
1957 WARN_ON_ONCE(1);
1958 break;
1959 case MPTCP_EVENT_SUB_ESTABLISHED:
1960 case MPTCP_EVENT_SUB_PRIORITY:
1961 if (mptcp_event_sub_established(skb, msk, ssk) < 0)
1962 goto nla_put_failure;
1963 break;
1964 case MPTCP_EVENT_SUB_CLOSED:
1965 if (mptcp_event_sub_closed(skb, msk, ssk) < 0)
1966 goto nla_put_failure;
1967 break;
1968 }
1969
1970 genlmsg_end(skb, nlh);
1971 mptcp_nl_mcast_send(net, skb, gfp);
1972 return;
1973
1974nla_put_failure:
1975 kfree_skb(skb);
1976}
1977
Rikard Falkeborn674d3ab2020-10-05 01:44:16 +02001978static const struct genl_small_ops mptcp_pm_ops[] = {
Paolo Abeni01cacb02020-03-27 14:48:51 -07001979 {
1980 .cmd = MPTCP_PM_CMD_ADD_ADDR,
1981 .doit = mptcp_nl_cmd_add_addr,
1982 .flags = GENL_ADMIN_PERM,
1983 },
1984 {
1985 .cmd = MPTCP_PM_CMD_DEL_ADDR,
1986 .doit = mptcp_nl_cmd_del_addr,
1987 .flags = GENL_ADMIN_PERM,
1988 },
1989 {
1990 .cmd = MPTCP_PM_CMD_FLUSH_ADDRS,
1991 .doit = mptcp_nl_cmd_flush_addrs,
1992 .flags = GENL_ADMIN_PERM,
1993 },
1994 {
1995 .cmd = MPTCP_PM_CMD_GET_ADDR,
1996 .doit = mptcp_nl_cmd_get_addr,
1997 .dumpit = mptcp_nl_cmd_dump_addrs,
1998 },
1999 {
2000 .cmd = MPTCP_PM_CMD_SET_LIMITS,
2001 .doit = mptcp_nl_cmd_set_limits,
2002 .flags = GENL_ADMIN_PERM,
2003 },
2004 {
2005 .cmd = MPTCP_PM_CMD_GET_LIMITS,
2006 .doit = mptcp_nl_cmd_get_limits,
2007 },
Geliang Tang0f9f6962021-01-08 16:47:59 -08002008 {
2009 .cmd = MPTCP_PM_CMD_SET_FLAGS,
2010 .doit = mptcp_nl_cmd_set_flags,
2011 .flags = GENL_ADMIN_PERM,
2012 },
Paolo Abeni01cacb02020-03-27 14:48:51 -07002013};
2014
2015static struct genl_family mptcp_genl_family __ro_after_init = {
2016 .name = MPTCP_PM_NAME,
2017 .version = MPTCP_PM_VER,
2018 .maxattr = MPTCP_PM_ATTR_MAX,
2019 .policy = mptcp_pm_policy,
2020 .netnsok = true,
2021 .module = THIS_MODULE,
Jakub Kicinski66a9b922020-10-02 14:49:54 -07002022 .small_ops = mptcp_pm_ops,
2023 .n_small_ops = ARRAY_SIZE(mptcp_pm_ops),
Paolo Abeni01cacb02020-03-27 14:48:51 -07002024 .mcgrps = mptcp_pm_mcgrps,
2025 .n_mcgrps = ARRAY_SIZE(mptcp_pm_mcgrps),
2026};
2027
2028static int __net_init pm_nl_init_net(struct net *net)
2029{
2030 struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id);
2031
2032 INIT_LIST_HEAD_RCU(&pernet->local_addr_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07002033 pernet->next_id = 1;
Paolo Abeniff5a0b42021-08-13 15:15:45 -07002034 pernet->stale_loss_cnt = 4;
Paolo Abeni01cacb02020-03-27 14:48:51 -07002035 spin_lock_init(&pernet->lock);
Jianguo Wuae514982021-05-27 16:54:27 -07002036
2037 /* No need to initialize other pernet fields, the struct is zeroed at
2038 * allocation time.
2039 */
2040
Paolo Abeni01cacb02020-03-27 14:48:51 -07002041 return 0;
2042}
2043
2044static void __net_exit pm_nl_exit_net(struct list_head *net_list)
2045{
2046 struct net *net;
2047
2048 list_for_each_entry(net, net_list, exit_list) {
Geliang Tang141694d2020-12-10 14:24:59 -08002049 struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id);
2050
Paolo Abeni01cacb02020-03-27 14:48:51 -07002051 /* net is removed from namespace list, can't race with
2052 * other modifiers
2053 */
Geliang Tang0e4a3e62021-03-12 17:16:18 -08002054 __flush_addrs(&pernet->local_addr_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07002055 }
2056}
2057
2058static struct pernet_operations mptcp_pm_pernet_ops = {
2059 .init = pm_nl_init_net,
2060 .exit_batch = pm_nl_exit_net,
2061 .id = &pm_nl_pernet_id,
2062 .size = sizeof(struct pm_nl_pernet),
2063};
2064
Paolo Abenid39dcec2020-06-26 19:29:59 +02002065void __init mptcp_pm_nl_init(void)
Paolo Abeni01cacb02020-03-27 14:48:51 -07002066{
2067 if (register_pernet_subsys(&mptcp_pm_pernet_ops) < 0)
2068 panic("Failed to register MPTCP PM pernet subsystem.\n");
2069
2070 if (genl_register_family(&mptcp_genl_family))
2071 panic("Failed to register MPTCP PM netlink family\n");
2072}