blob: 356f596e2032306933cbcd17b3838800fb25ccce [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
Paolo Abeni01cacb02020-03-27 14:48:51 -070041struct pm_nl_pernet {
42 /* protects pernet updates */
43 spinlock_t lock;
44 struct list_head local_addr_list;
45 unsigned int addrs;
Paolo Abeniff5a0b42021-08-13 15:15:45 -070046 unsigned int stale_loss_cnt;
Paolo Abeni01cacb02020-03-27 14:48:51 -070047 unsigned int add_addr_signal_max;
48 unsigned int add_addr_accept_max;
49 unsigned int local_addr_max;
50 unsigned int subflows_max;
51 unsigned int next_id;
Paolo Abeni86e39e02022-01-06 16:20:22 -080052 DECLARE_BITMAP(id_bitmap, MPTCP_PM_MAX_ADDR_ID + 1);
Paolo Abeni01cacb02020-03-27 14:48:51 -070053};
54
55#define MPTCP_PM_ADDR_MAX 8
Geliang Tang00cfd772020-09-24 08:30:02 +080056#define ADD_ADDR_RETRANS_MAX 3
Paolo Abeni01cacb02020-03-27 14:48:51 -070057
58static bool addresses_equal(const struct mptcp_addr_info *a,
Paolo Abeni86e39e02022-01-06 16:20:22 -080059 const struct mptcp_addr_info *b, bool use_port)
Paolo Abeni01cacb02020-03-27 14:48:51 -070060{
61 bool addr_equals = false;
62
Matthieu Baerts7b9b0f72021-01-25 10:59:01 -080063 if (a->family == b->family) {
64 if (a->family == AF_INET)
65 addr_equals = a->addr.s_addr == b->addr.s_addr;
Paolo Abeni01cacb02020-03-27 14:48:51 -070066#if IS_ENABLED(CONFIG_MPTCP_IPV6)
Matthieu Baerts7b9b0f72021-01-25 10:59:01 -080067 else
68 addr_equals = !ipv6_addr_cmp(&a->addr6, &b->addr6);
69 } else if (a->family == AF_INET) {
70 if (ipv6_addr_v4mapped(&b->addr6))
71 addr_equals = a->addr.s_addr == b->addr6.s6_addr32[3];
72 } else if (b->family == AF_INET) {
73 if (ipv6_addr_v4mapped(&a->addr6))
74 addr_equals = a->addr6.s6_addr32[3] == b->addr.s_addr;
Paolo Abeni01cacb02020-03-27 14:48:51 -070075#endif
Matthieu Baerts7b9b0f72021-01-25 10:59:01 -080076 }
Paolo Abeni01cacb02020-03-27 14:48:51 -070077
78 if (!addr_equals)
79 return false;
80 if (!use_port)
81 return true;
82
83 return a->port == b->port;
84}
85
Geliang Tang57025812020-09-08 10:49:38 +080086static bool address_zero(const struct mptcp_addr_info *addr)
87{
88 struct mptcp_addr_info zero;
89
90 memset(&zero, 0, sizeof(zero));
91 zero.family = addr->family;
92
Geliang Tang60b57bf2021-02-01 15:09:16 -080093 return addresses_equal(addr, &zero, true);
Geliang Tang57025812020-09-08 10:49:38 +080094}
95
Paolo Abeni01cacb02020-03-27 14:48:51 -070096static void local_address(const struct sock_common *skc,
97 struct mptcp_addr_info *addr)
98{
Paolo Abeni01cacb02020-03-27 14:48:51 -070099 addr->family = skc->skc_family;
Geliang Tang5bc56382021-02-01 15:09:15 -0800100 addr->port = htons(skc->skc_num);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700101 if (addr->family == AF_INET)
102 addr->addr.s_addr = skc->skc_rcv_saddr;
103#if IS_ENABLED(CONFIG_MPTCP_IPV6)
104 else if (addr->family == AF_INET6)
105 addr->addr6 = skc->skc_v6_rcv_saddr;
106#endif
107}
108
109static void remote_address(const struct sock_common *skc,
110 struct mptcp_addr_info *addr)
111{
112 addr->family = skc->skc_family;
113 addr->port = skc->skc_dport;
114 if (addr->family == AF_INET)
115 addr->addr.s_addr = skc->skc_daddr;
116#if IS_ENABLED(CONFIG_MPTCP_IPV6)
117 else if (addr->family == AF_INET6)
118 addr->addr6 = skc->skc_v6_daddr;
119#endif
120}
121
122static bool lookup_subflow_by_saddr(const struct list_head *list,
123 struct mptcp_addr_info *saddr)
124{
125 struct mptcp_subflow_context *subflow;
126 struct mptcp_addr_info cur;
127 struct sock_common *skc;
128
129 list_for_each_entry(subflow, list, node) {
130 skc = (struct sock_common *)mptcp_subflow_tcp_sock(subflow);
131
132 local_address(skc, &cur);
Geliang Tang60b57bf2021-02-01 15:09:16 -0800133 if (addresses_equal(&cur, saddr, saddr->port))
Paolo Abeni01cacb02020-03-27 14:48:51 -0700134 return true;
135 }
136
137 return false;
138}
139
Geliang Tangd84ad042021-03-26 11:26:32 -0700140static bool lookup_subflow_by_daddr(const struct list_head *list,
141 struct mptcp_addr_info *daddr)
142{
143 struct mptcp_subflow_context *subflow;
144 struct mptcp_addr_info cur;
145 struct sock_common *skc;
146
147 list_for_each_entry(subflow, list, node) {
148 skc = (struct sock_common *)mptcp_subflow_tcp_sock(subflow);
149
150 remote_address(skc, &cur);
151 if (addresses_equal(&cur, daddr, daddr->port))
152 return true;
153 }
154
155 return false;
156}
157
Paolo Abeni01cacb02020-03-27 14:48:51 -0700158static struct mptcp_pm_addr_entry *
159select_local_address(const struct pm_nl_pernet *pernet,
160 struct mptcp_sock *msk)
161{
162 struct mptcp_pm_addr_entry *entry, *ret = NULL;
Matthieu Baerts7b9b0f72021-01-25 10:59:01 -0800163 struct sock *sk = (struct sock *)msk;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700164
Florian Westphal3abc05d2021-02-04 15:23:30 -0800165 msk_owned_by_me(msk);
166
Paolo Abeni01cacb02020-03-27 14:48:51 -0700167 rcu_read_lock();
Paolo Abeni01cacb02020-03-27 14:48:51 -0700168 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
Geliang Tangdaa83ab2021-04-06 17:15:57 -0700169 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW))
Paolo Abeni01cacb02020-03-27 14:48:51 -0700170 continue;
171
Paolo Abeni86e39e02022-01-06 16:20:22 -0800172 if (!test_bit(entry->addr.id, msk->pm.id_avail_bitmap))
173 continue;
174
Matthieu Baerts7b9b0f72021-01-25 10:59:01 -0800175 if (entry->addr.family != sk->sk_family) {
176#if IS_ENABLED(CONFIG_MPTCP_IPV6)
177 if ((entry->addr.family == AF_INET &&
178 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) ||
179 (sk->sk_family == AF_INET &&
180 !ipv6_addr_v4mapped(&entry->addr.addr6)))
181#endif
182 continue;
183 }
184
Paolo Abeni86e39e02022-01-06 16:20:22 -0800185 ret = entry;
186 break;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700187 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700188 rcu_read_unlock();
189 return ret;
190}
191
192static struct mptcp_pm_addr_entry *
Paolo Abeni86e39e02022-01-06 16:20:22 -0800193select_signal_address(struct pm_nl_pernet *pernet, struct mptcp_sock *msk)
Paolo Abeni01cacb02020-03-27 14:48:51 -0700194{
195 struct mptcp_pm_addr_entry *entry, *ret = NULL;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700196
197 rcu_read_lock();
198 /* do not keep any additional per socket state, just signal
199 * the address list in order.
200 * Note: removal from the local address list during the msk life-cycle
201 * can lead to additional addresses not being announced.
202 */
203 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
Paolo Abeni86e39e02022-01-06 16:20:22 -0800204 if (!test_bit(entry->addr.id, msk->pm.id_avail_bitmap))
205 continue;
206
Geliang Tangdaa83ab2021-04-06 17:15:57 -0700207 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL))
Paolo Abeni01cacb02020-03-27 14:48:51 -0700208 continue;
Paolo Abeni86e39e02022-01-06 16:20:22 -0800209
210 ret = entry;
211 break;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700212 }
213 rcu_read_unlock();
214 return ret;
215}
216
Geliang Tanga914e582021-02-01 15:09:07 -0800217unsigned int mptcp_pm_get_add_addr_signal_max(struct mptcp_sock *msk)
218{
219 struct pm_nl_pernet *pernet;
220
221 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
222 return READ_ONCE(pernet->add_addr_signal_max);
223}
224EXPORT_SYMBOL_GPL(mptcp_pm_get_add_addr_signal_max);
225
226unsigned int mptcp_pm_get_add_addr_accept_max(struct mptcp_sock *msk)
227{
228 struct pm_nl_pernet *pernet;
229
230 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
231 return READ_ONCE(pernet->add_addr_accept_max);
232}
233EXPORT_SYMBOL_GPL(mptcp_pm_get_add_addr_accept_max);
234
235unsigned int mptcp_pm_get_subflows_max(struct mptcp_sock *msk)
236{
237 struct pm_nl_pernet *pernet;
238
239 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
240 return READ_ONCE(pernet->subflows_max);
241}
242EXPORT_SYMBOL_GPL(mptcp_pm_get_subflows_max);
243
Geliang Tang0caf3ad2021-02-12 16:52:02 -0800244unsigned int mptcp_pm_get_local_addr_max(struct mptcp_sock *msk)
Geliang Tanga914e582021-02-01 15:09:07 -0800245{
246 struct pm_nl_pernet *pernet;
247
248 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
249 return READ_ONCE(pernet->local_addr_max);
250}
Geliang Tang0caf3ad2021-02-12 16:52:02 -0800251EXPORT_SYMBOL_GPL(mptcp_pm_get_local_addr_max);
Geliang Tanga914e582021-02-01 15:09:07 -0800252
Paolo Abenia88c9e42022-01-06 16:20:23 -0800253bool mptcp_pm_nl_check_work_pending(struct mptcp_sock *msk)
Paolo Abeni01cacb02020-03-27 14:48:51 -0700254{
Paolo Abeni86e39e02022-01-06 16:20:22 -0800255 struct pm_nl_pernet *pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
256
257 if (msk->pm.subflows == mptcp_pm_get_subflows_max(msk) ||
258 (find_next_and_bit(pernet->id_bitmap, msk->pm.id_avail_bitmap,
Paolo Abenia88c9e42022-01-06 16:20:23 -0800259 MPTCP_PM_MAX_ADDR_ID + 1, 0) == MPTCP_PM_MAX_ADDR_ID + 1)) {
Paolo Abeni01cacb02020-03-27 14:48:51 -0700260 WRITE_ONCE(msk->pm.work_pending, false);
Paolo Abenia88c9e42022-01-06 16:20:23 -0800261 return false;
262 }
263 return true;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700264}
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
Yonglong Li18fc1a92021-08-23 18:05:40 -0700320 if (mptcp_pm_should_add_signal_addr(msk)) {
Geliang Tang00cfd772020-09-24 08:30:02 +0800321 sk_reset_timer(sk, timer, jiffies + TCP_RTO_MAX / 8);
322 goto out;
323 }
324
325 spin_lock_bh(&msk->pm.lock);
326
Yonglong Li18fc1a92021-08-23 18:05:40 -0700327 if (!mptcp_pm_should_add_signal_addr(msk)) {
Geliang Tang00cfd772020-09-24 08:30:02 +0800328 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{
Paolo Abenia88c9e42022-01-06 16:20:23 -0800432 bool deny_id0 = READ_ONCE(msk->pm.remote_deny_join_id0);
Geliang Tang2843ff62021-08-17 15:07:23 -0700433 struct sock *sk = (struct sock *)msk, *ssk;
434 struct mptcp_subflow_context *subflow;
435 struct mptcp_addr_info remote = { 0 };
436 unsigned int subflows_max;
437 int i = 0;
438
439 subflows_max = mptcp_pm_get_subflows_max(msk);
Paolo Abenia88c9e42022-01-06 16:20:23 -0800440 remote_address((struct sock_common *)sk, &remote);
Geliang Tang2843ff62021-08-17 15:07:23 -0700441
442 /* Non-fullmesh endpoint, fill in the single entry
443 * corresponding to the primary MPC subflow remote address
444 */
445 if (!fullmesh) {
Paolo Abenia88c9e42022-01-06 16:20:23 -0800446 if (deny_id0)
447 return 0;
448
Geliang Tang2843ff62021-08-17 15:07:23 -0700449 msk->pm.subflows++;
450 addrs[i++] = remote;
451 } else {
452 mptcp_for_each_subflow(msk, subflow) {
453 ssk = mptcp_subflow_tcp_sock(subflow);
Paolo Abenia88c9e42022-01-06 16:20:23 -0800454 remote_address((struct sock_common *)ssk, &addrs[i]);
455 if (deny_id0 && addresses_equal(&addrs[i], &remote, false))
456 continue;
457
458 if (!lookup_address_in_vec(addrs, i, &addrs[i]) &&
Geliang Tang2843ff62021-08-17 15:07:23 -0700459 msk->pm.subflows < subflows_max) {
460 msk->pm.subflows++;
Paolo Abenia88c9e42022-01-06 16:20:23 -0800461 i++;
Geliang Tang2843ff62021-08-17 15:07:23 -0700462 }
463 }
464 }
465
466 return i;
467}
468
Paolo Abeni86e39e02022-01-06 16:20:22 -0800469static struct mptcp_pm_addr_entry *
470__lookup_addr_by_id(struct pm_nl_pernet *pernet, unsigned int id)
471{
472 struct mptcp_pm_addr_entry *entry;
473
474 list_for_each_entry(entry, &pernet->local_addr_list, list) {
475 if (entry->addr.id == id)
476 return entry;
477 }
478 return NULL;
479}
480
Paolo Abeni8e9eaca2022-01-20 16:35:27 -0800481static struct mptcp_pm_addr_entry *
482__lookup_addr(struct pm_nl_pernet *pernet, const struct mptcp_addr_info *info,
483 bool lookup_by_id)
484{
485 struct mptcp_pm_addr_entry *entry;
486
487 list_for_each_entry(entry, &pernet->local_addr_list, list) {
488 if ((!lookup_by_id && addresses_equal(&entry->addr, info, true)) ||
489 (lookup_by_id && entry->addr.id == info->id))
490 return entry;
491 }
492 return NULL;
493}
494
Paolo Abeni86e39e02022-01-06 16:20:22 -0800495static int
496lookup_id_by_addr(struct pm_nl_pernet *pernet, const struct mptcp_addr_info *addr)
497{
498 struct mptcp_pm_addr_entry *entry;
499 int ret = -1;
500
501 rcu_read_lock();
502 list_for_each_entry(entry, &pernet->local_addr_list, list) {
503 if (addresses_equal(&entry->addr, addr, entry->addr.port)) {
504 ret = entry->addr.id;
505 break;
506 }
507 }
508 rcu_read_unlock();
509 return ret;
510}
511
Paolo Abeni01cacb02020-03-27 14:48:51 -0700512static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk)
513{
514 struct sock *sk = (struct sock *)msk;
515 struct mptcp_pm_addr_entry *local;
Geliang Tanga914e582021-02-01 15:09:07 -0800516 unsigned int add_addr_signal_max;
517 unsigned int local_addr_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700518 struct pm_nl_pernet *pernet;
Geliang Tanga914e582021-02-01 15:09:07 -0800519 unsigned int subflows_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700520
Geliang Tang432d9e72020-12-09 15:51:28 -0800521 pernet = net_generic(sock_net(sk), pm_nl_pernet_id);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700522
Geliang Tanga914e582021-02-01 15:09:07 -0800523 add_addr_signal_max = mptcp_pm_get_add_addr_signal_max(msk);
524 local_addr_max = mptcp_pm_get_local_addr_max(msk);
525 subflows_max = mptcp_pm_get_subflows_max(msk);
526
Paolo Abeni86e39e02022-01-06 16:20:22 -0800527 /* do lazy endpoint usage accounting for the MPC subflows */
528 if (unlikely(!(msk->pm.status & BIT(MPTCP_PM_MPC_ENDPOINT_ACCOUNTED))) && msk->first) {
Paolo Abenia88c9e42022-01-06 16:20:23 -0800529 struct mptcp_addr_info mpc_addr;
Paolo Abeni86e39e02022-01-06 16:20:22 -0800530 int mpc_id;
531
Paolo Abenia88c9e42022-01-06 16:20:23 -0800532 local_address((struct sock_common *)msk->first, &mpc_addr);
533 mpc_id = lookup_id_by_addr(pernet, &mpc_addr);
534 if (mpc_id >= 0)
Paolo Abeni86e39e02022-01-06 16:20:22 -0800535 __clear_bit(mpc_id, msk->pm.id_avail_bitmap);
536
537 msk->pm.status |= BIT(MPTCP_PM_MPC_ENDPOINT_ACCOUNTED);
538 }
539
Paolo Abeni01cacb02020-03-27 14:48:51 -0700540 pr_debug("local %d:%d signal %d:%d subflows %d:%d\n",
Geliang Tanga914e582021-02-01 15:09:07 -0800541 msk->pm.local_addr_used, local_addr_max,
542 msk->pm.add_addr_signaled, add_addr_signal_max,
543 msk->pm.subflows, subflows_max);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700544
545 /* check first for announce */
Geliang Tanga914e582021-02-01 15:09:07 -0800546 if (msk->pm.add_addr_signaled < add_addr_signal_max) {
Paolo Abeni86e39e02022-01-06 16:20:22 -0800547 local = select_signal_address(pernet, msk);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700548
549 if (local) {
Geliang Tangb6c08382020-09-24 08:29:54 +0800550 if (mptcp_pm_alloc_anno_list(msk, local)) {
Paolo Abeni86e39e02022-01-06 16:20:22 -0800551 __clear_bit(local->addr.id, msk->pm.id_avail_bitmap);
Geliang Tangb6c08382020-09-24 08:29:54 +0800552 msk->pm.add_addr_signaled++;
Geliang Tangf7efc772021-03-26 11:26:31 -0700553 mptcp_pm_announce_addr(msk, &local->addr, false);
Geliang Tangb46a0232021-03-26 11:26:41 -0700554 mptcp_pm_nl_addr_send_ack(msk);
Geliang Tangb6c08382020-09-24 08:29:54 +0800555 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700556 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700557 }
558
559 /* check if should create a new subflow */
Paolo Abenia88c9e42022-01-06 16:20:23 -0800560 while (msk->pm.local_addr_used < local_addr_max &&
561 msk->pm.subflows < subflows_max) {
562 struct mptcp_addr_info addrs[MPTCP_PM_ADDR_MAX];
563 bool fullmesh;
564 int i, nr;
Matthieu Baerts1f2f1932021-01-25 10:59:02 -0800565
Paolo Abenia88c9e42022-01-06 16:20:23 -0800566 local = select_local_address(pernet, msk);
567 if (!local)
568 break;
569
570 fullmesh = !!(local->flags & MPTCP_PM_ADDR_FLAG_FULLMESH);
571
572 msk->pm.local_addr_used++;
573 nr = fill_remote_addresses_vec(msk, fullmesh, addrs);
574 if (nr)
575 __clear_bit(local->addr.id, msk->pm.id_avail_bitmap);
576 spin_unlock_bh(&msk->pm.lock);
577 for (i = 0; i < nr; i++)
578 __mptcp_subflow_connect(sk, &local->addr, &addrs[i]);
579 spin_lock_bh(&msk->pm.lock);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700580 }
Paolo Abenia88c9e42022-01-06 16:20:23 -0800581 mptcp_pm_nl_check_work_pending(msk);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700582}
583
Florian Westphale9801432021-02-12 15:59:54 -0800584static void mptcp_pm_nl_fully_established(struct mptcp_sock *msk)
Paolo Abeni01cacb02020-03-27 14:48:51 -0700585{
586 mptcp_pm_create_subflow_or_signal_addr(msk);
587}
588
Florian Westphale9801432021-02-12 15:59:54 -0800589static void mptcp_pm_nl_subflow_established(struct mptcp_sock *msk)
Paolo Abeni01cacb02020-03-27 14:48:51 -0700590{
591 mptcp_pm_create_subflow_or_signal_addr(msk);
592}
593
Geliang Tang1a0d6132021-08-17 15:07:24 -0700594/* Fill all the local addresses into the array addrs[],
595 * and return the array size.
596 */
597static unsigned int fill_local_addresses_vec(struct mptcp_sock *msk,
598 struct mptcp_addr_info *addrs)
599{
600 struct sock *sk = (struct sock *)msk;
601 struct mptcp_pm_addr_entry *entry;
602 struct mptcp_addr_info local;
603 struct pm_nl_pernet *pernet;
604 unsigned int subflows_max;
605 int i = 0;
606
607 pernet = net_generic(sock_net(sk), pm_nl_pernet_id);
608 subflows_max = mptcp_pm_get_subflows_max(msk);
609
610 rcu_read_lock();
Geliang Tang1a0d6132021-08-17 15:07:24 -0700611 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
612 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_FULLMESH))
613 continue;
614
615 if (entry->addr.family != sk->sk_family) {
616#if IS_ENABLED(CONFIG_MPTCP_IPV6)
617 if ((entry->addr.family == AF_INET &&
618 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) ||
619 (sk->sk_family == AF_INET &&
620 !ipv6_addr_v4mapped(&entry->addr.addr6)))
621#endif
622 continue;
623 }
624
625 if (msk->pm.subflows < subflows_max) {
626 msk->pm.subflows++;
627 addrs[i++] = entry->addr;
628 }
629 }
630 rcu_read_unlock();
631
632 /* If the array is empty, fill in the single
633 * 'IPADDRANY' local address
634 */
635 if (!i) {
636 memset(&local, 0, sizeof(local));
637 local.family = msk->pm.remote.family;
638
639 msk->pm.subflows++;
640 addrs[i++] = local;
641 }
642
643 return i;
644}
645
Florian Westphale9801432021-02-12 15:59:54 -0800646static void mptcp_pm_nl_add_addr_received(struct mptcp_sock *msk)
Paolo Abeni01cacb02020-03-27 14:48:51 -0700647{
Geliang Tang1a0d6132021-08-17 15:07:24 -0700648 struct mptcp_addr_info addrs[MPTCP_PM_ADDR_MAX];
Paolo Abeni01cacb02020-03-27 14:48:51 -0700649 struct sock *sk = (struct sock *)msk;
Geliang Tanga914e582021-02-01 15:09:07 -0800650 unsigned int add_addr_accept_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700651 struct mptcp_addr_info remote;
Geliang Tanga914e582021-02-01 15:09:07 -0800652 unsigned int subflows_max;
Geliang Tang1a0d6132021-08-17 15:07:24 -0700653 int i, nr;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700654
Geliang Tanga914e582021-02-01 15:09:07 -0800655 add_addr_accept_max = mptcp_pm_get_add_addr_accept_max(msk);
656 subflows_max = mptcp_pm_get_subflows_max(msk);
657
Paolo Abeni01cacb02020-03-27 14:48:51 -0700658 pr_debug("accepted %d:%d remote family %d",
Geliang Tanga914e582021-02-01 15:09:07 -0800659 msk->pm.add_addr_accepted, add_addr_accept_max,
Paolo Abeni01cacb02020-03-27 14:48:51 -0700660 msk->pm.remote.family);
Geliang Tangd84ad042021-03-26 11:26:32 -0700661
662 if (lookup_subflow_by_daddr(&msk->conn_list, &msk->pm.remote))
663 goto add_addr_echo;
664
Paolo Abeni01cacb02020-03-27 14:48:51 -0700665 /* connect to the specified remote address, using whatever
666 * local address the routing configuration will pick.
667 */
668 remote = msk->pm.remote;
669 if (!remote.port)
670 remote.port = sk->sk_dport;
Geliang Tang1a0d6132021-08-17 15:07:24 -0700671 nr = fill_local_addresses_vec(msk, addrs);
672
673 msk->pm.add_addr_accepted++;
674 if (msk->pm.add_addr_accepted >= add_addr_accept_max ||
675 msk->pm.subflows >= subflows_max)
676 WRITE_ONCE(msk->pm.accept_addr, false);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700677
678 spin_unlock_bh(&msk->pm.lock);
Geliang Tang1a0d6132021-08-17 15:07:24 -0700679 for (i = 0; i < nr; i++)
680 __mptcp_subflow_connect(sk, &addrs[i], &remote);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700681 spin_lock_bh(&msk->pm.lock);
Geliang Tang6a6c05a2020-09-24 08:29:50 +0800682
Geliang Tangd84ad042021-03-26 11:26:32 -0700683add_addr_echo:
Geliang Tangf7efc772021-03-26 11:26:31 -0700684 mptcp_pm_announce_addr(msk, &msk->pm.remote, true);
Geliang Tangb46a0232021-03-26 11:26:41 -0700685 mptcp_pm_nl_addr_send_ack(msk);
Geliang Tang84dfe362020-11-19 11:46:00 -0800686}
687
Geliang Tangb46a0232021-03-26 11:26:41 -0700688void mptcp_pm_nl_addr_send_ack(struct mptcp_sock *msk)
Geliang Tang84dfe362020-11-19 11:46:00 -0800689{
690 struct mptcp_subflow_context *subflow;
691
Florian Westphal3abc05d2021-02-04 15:23:30 -0800692 msk_owned_by_me(msk);
693 lockdep_assert_held(&msk->pm.lock);
694
Geliang Tang8dd5efb2021-03-26 11:26:40 -0700695 if (!mptcp_pm_should_add_signal(msk) &&
696 !mptcp_pm_should_rm_signal(msk))
Geliang Tang84dfe362020-11-19 11:46:00 -0800697 return;
698
Geliang Tang84dfe362020-11-19 11:46:00 -0800699 subflow = list_first_entry_or_null(&msk->conn_list, typeof(*subflow), node);
700 if (subflow) {
701 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
Geliang Tang84dfe362020-11-19 11:46:00 -0800702
703 spin_unlock_bh(&msk->pm.lock);
Yonglong Lic233ef12021-08-23 18:05:43 -0700704 pr_debug("send ack for %s",
705 mptcp_pm_should_add_signal(msk) ? "add_addr" : "rm_addr");
Geliang Tangfbe0f872020-12-09 15:51:23 -0800706
Mat Martineau340fa662021-09-02 11:51:19 -0700707 mptcp_subflow_send_ack(ssk);
Geliang Tang84dfe362020-11-19 11:46:00 -0800708 spin_lock_bh(&msk->pm.lock);
Geliang Tang84dfe362020-11-19 11:46:00 -0800709 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700710}
711
Mat Martineau3828c512021-10-15 16:05:52 -0700712static int mptcp_pm_nl_mp_prio_send_ack(struct mptcp_sock *msk,
713 struct mptcp_addr_info *addr,
714 u8 bkup)
Geliang Tang06706542021-01-08 16:47:57 -0800715{
716 struct mptcp_subflow_context *subflow;
717
718 pr_debug("bkup=%d", bkup);
719
720 mptcp_for_each_subflow(msk, subflow) {
721 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
Geliang Tang0be2ac22021-01-08 16:48:01 -0800722 struct sock *sk = (struct sock *)msk;
Geliang Tang06706542021-01-08 16:47:57 -0800723 struct mptcp_addr_info local;
724
725 local_address((struct sock_common *)ssk, &local);
726 if (!addresses_equal(&local, addr, addr->port))
727 continue;
728
729 subflow->backup = bkup;
730 subflow->send_mp_prio = 1;
731 subflow->request_bkup = bkup;
Geliang Tang0be2ac22021-01-08 16:48:01 -0800732 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIOTX);
Geliang Tang06706542021-01-08 16:47:57 -0800733
734 spin_unlock_bh(&msk->pm.lock);
735 pr_debug("send ack for mp_prio");
Mat Martineau340fa662021-09-02 11:51:19 -0700736 mptcp_subflow_send_ack(ssk);
Geliang Tang06706542021-01-08 16:47:57 -0800737 spin_lock_bh(&msk->pm.lock);
738
739 return 0;
740 }
741
742 return -EINVAL;
743}
744
Geliang Tang9f12e972021-03-30 17:08:52 -0700745static void mptcp_pm_nl_rm_addr_or_subflow(struct mptcp_sock *msk,
746 const struct mptcp_rm_list *rm_list,
747 enum linux_mptcp_mib_field rm_type)
Geliang Tangd0876b22020-09-24 08:29:49 +0800748{
749 struct mptcp_subflow_context *subflow, *tmp;
750 struct sock *sk = (struct sock *)msk;
Geliang Tangd0b698c2021-03-12 17:16:15 -0800751 u8 i;
Geliang Tangd0876b22020-09-24 08:29:49 +0800752
Geliang Tang9f12e972021-03-30 17:08:52 -0700753 pr_debug("%s rm_list_nr %d",
754 rm_type == MPTCP_MIB_RMADDR ? "address" : "subflow", rm_list->nr);
Geliang Tangd0876b22020-09-24 08:29:49 +0800755
Florian Westphal3abc05d2021-02-04 15:23:30 -0800756 msk_owned_by_me(msk);
757
Paolo Abenib0cdc5d2021-12-11 17:11:12 +0100758 if (sk->sk_state == TCP_LISTEN)
759 return;
760
Geliang Tang9f12e972021-03-30 17:08:52 -0700761 if (!rm_list->nr)
Geliang Tangd0876b22020-09-24 08:29:49 +0800762 return;
763
764 if (list_empty(&msk->conn_list))
765 return;
766
Geliang Tang9f12e972021-03-30 17:08:52 -0700767 for (i = 0; i < rm_list->nr; i++) {
Paolo Abenif7d6a232022-01-06 16:20:20 -0800768 bool removed = false;
769
Geliang Tangd0b698c2021-03-12 17:16:15 -0800770 list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
771 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
772 int how = RCV_SHUTDOWN | SEND_SHUTDOWN;
Geliang Tang9f12e972021-03-30 17:08:52 -0700773 u8 id = subflow->local_id;
Geliang Tangd0876b22020-09-24 08:29:49 +0800774
Geliang Tang9f12e972021-03-30 17:08:52 -0700775 if (rm_type == MPTCP_MIB_RMADDR)
776 id = subflow->remote_id;
777
778 if (rm_list->ids[i] != id)
Geliang Tangd0b698c2021-03-12 17:16:15 -0800779 continue;
Geliang Tangd0876b22020-09-24 08:29:49 +0800780
Geliang Tang9f12e972021-03-30 17:08:52 -0700781 pr_debug(" -> %s rm_list_ids[%d]=%u local_id=%u remote_id=%u",
782 rm_type == MPTCP_MIB_RMADDR ? "address" : "subflow",
783 i, rm_list->ids[i], subflow->local_id, subflow->remote_id);
Geliang Tangd0b698c2021-03-12 17:16:15 -0800784 spin_unlock_bh(&msk->pm.lock);
785 mptcp_subflow_shutdown(sk, ssk, how);
Paolo Abenia88c9e42022-01-06 16:20:23 -0800786
787 /* the following takes care of updating the subflows counter */
Geliang Tangd0b698c2021-03-12 17:16:15 -0800788 mptcp_close_ssk(sk, ssk, subflow);
789 spin_lock_bh(&msk->pm.lock);
Geliang Tangd0876b22020-09-24 08:29:49 +0800790
Paolo Abenif7d6a232022-01-06 16:20:20 -0800791 removed = true;
Geliang Tang9f12e972021-03-30 17:08:52 -0700792 __MPTCP_INC_STATS(sock_net(sk), rm_type);
Geliang Tangd0b698c2021-03-12 17:16:15 -0800793 }
Geliang Tanga4c02142022-01-20 16:35:28 -0800794 __set_bit(rm_list->ids[i], msk->pm.id_avail_bitmap);
Paolo Abenif7d6a232022-01-06 16:20:20 -0800795 if (!removed)
796 continue;
797
798 if (rm_type == MPTCP_MIB_RMADDR) {
799 msk->pm.add_addr_accepted--;
800 WRITE_ONCE(msk->pm.accept_addr, true);
801 } else if (rm_type == MPTCP_MIB_RMSUBFLOW) {
802 msk->pm.local_addr_used--;
803 }
Geliang Tangd0876b22020-09-24 08:29:49 +0800804 }
805}
806
Geliang Tang9f12e972021-03-30 17:08:52 -0700807static void mptcp_pm_nl_rm_addr_received(struct mptcp_sock *msk)
808{
809 mptcp_pm_nl_rm_addr_or_subflow(msk, &msk->pm.rm_list_rx, MPTCP_MIB_RMADDR);
810}
811
812void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk,
813 const struct mptcp_rm_list *rm_list)
814{
815 mptcp_pm_nl_rm_addr_or_subflow(msk, rm_list, MPTCP_MIB_RMSUBFLOW);
816}
817
Florian Westphale9801432021-02-12 15:59:54 -0800818void mptcp_pm_nl_work(struct mptcp_sock *msk)
819{
820 struct mptcp_pm_data *pm = &msk->pm;
821
822 msk_owned_by_me(msk);
823
Paolo Abeni86e39e02022-01-06 16:20:22 -0800824 if (!(pm->status & MPTCP_PM_WORK_MASK))
825 return;
826
Florian Westphale9801432021-02-12 15:59:54 -0800827 spin_lock_bh(&msk->pm.lock);
828
829 pr_debug("msk=%p status=%x", msk, pm->status);
830 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_RECEIVED)) {
831 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_RECEIVED);
832 mptcp_pm_nl_add_addr_received(msk);
833 }
834 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_SEND_ACK)) {
835 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_SEND_ACK);
Geliang Tangb46a0232021-03-26 11:26:41 -0700836 mptcp_pm_nl_addr_send_ack(msk);
Florian Westphale9801432021-02-12 15:59:54 -0800837 }
838 if (pm->status & BIT(MPTCP_PM_RM_ADDR_RECEIVED)) {
839 pm->status &= ~BIT(MPTCP_PM_RM_ADDR_RECEIVED);
840 mptcp_pm_nl_rm_addr_received(msk);
841 }
842 if (pm->status & BIT(MPTCP_PM_ESTABLISHED)) {
843 pm->status &= ~BIT(MPTCP_PM_ESTABLISHED);
844 mptcp_pm_nl_fully_established(msk);
845 }
846 if (pm->status & BIT(MPTCP_PM_SUBFLOW_ESTABLISHED)) {
847 pm->status &= ~BIT(MPTCP_PM_SUBFLOW_ESTABLISHED);
848 mptcp_pm_nl_subflow_established(msk);
849 }
850
851 spin_unlock_bh(&msk->pm.lock);
852}
853
Paolo Abeni01cacb02020-03-27 14:48:51 -0700854static bool address_use_port(struct mptcp_pm_addr_entry *entry)
855{
Geliang Tangdaa83ab2021-04-06 17:15:57 -0700856 return (entry->flags &
Paolo Abeni01cacb02020-03-27 14:48:51 -0700857 (MPTCP_PM_ADDR_FLAG_SIGNAL | MPTCP_PM_ADDR_FLAG_SUBFLOW)) ==
858 MPTCP_PM_ADDR_FLAG_SIGNAL;
859}
860
861static int mptcp_pm_nl_append_new_local_addr(struct pm_nl_pernet *pernet,
862 struct mptcp_pm_addr_entry *entry)
863{
864 struct mptcp_pm_addr_entry *cur;
Geliang Tang72603d22021-02-01 15:09:06 -0800865 unsigned int addr_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700866 int ret = -EINVAL;
867
868 spin_lock_bh(&pernet->lock);
869 /* to keep the code simple, don't do IDR-like allocation for address ID,
870 * just bail when we exceed limits
871 */
Paolo Abeni86e39e02022-01-06 16:20:22 -0800872 if (pernet->next_id == MPTCP_PM_MAX_ADDR_ID)
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800873 pernet->next_id = 1;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700874 if (pernet->addrs >= MPTCP_PM_ADDR_MAX)
875 goto out;
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800876 if (test_bit(entry->addr.id, pernet->id_bitmap))
877 goto out;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700878
879 /* do not insert duplicate address, differentiate on port only
880 * singled addresses
881 */
882 list_for_each_entry(cur, &pernet->local_addr_list, list) {
883 if (addresses_equal(&cur->addr, &entry->addr,
884 address_use_port(entry) &&
885 address_use_port(cur)))
886 goto out;
887 }
888
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800889 if (!entry->addr.id) {
890find_next:
891 entry->addr.id = find_next_zero_bit(pernet->id_bitmap,
Paolo Abeni86e39e02022-01-06 16:20:22 -0800892 MPTCP_PM_MAX_ADDR_ID + 1,
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800893 pernet->next_id);
Jean Sacren59060a42021-12-17 15:37:02 -0800894 if (!entry->addr.id && pernet->next_id != 1) {
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800895 pernet->next_id = 1;
896 goto find_next;
897 }
898 }
899
Jean Sacren59060a42021-12-17 15:37:02 -0800900 if (!entry->addr.id)
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800901 goto out;
902
903 __set_bit(entry->addr.id, pernet->id_bitmap);
904 if (entry->addr.id > pernet->next_id)
905 pernet->next_id = entry->addr.id;
906
Geliang Tangdaa83ab2021-04-06 17:15:57 -0700907 if (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
Geliang Tang72603d22021-02-01 15:09:06 -0800908 addr_max = pernet->add_addr_signal_max;
909 WRITE_ONCE(pernet->add_addr_signal_max, addr_max + 1);
910 }
Geliang Tangdaa83ab2021-04-06 17:15:57 -0700911 if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) {
Geliang Tang72603d22021-02-01 15:09:06 -0800912 addr_max = pernet->local_addr_max;
913 WRITE_ONCE(pernet->local_addr_max, addr_max + 1);
914 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700915
Paolo Abeni01cacb02020-03-27 14:48:51 -0700916 pernet->addrs++;
917 list_add_tail_rcu(&entry->list, &pernet->local_addr_list);
918 ret = entry->addr.id;
919
920out:
921 spin_unlock_bh(&pernet->lock);
922 return ret;
923}
924
Geliang Tang1729cf12021-02-01 15:09:12 -0800925static int mptcp_pm_nl_create_listen_socket(struct sock *sk,
926 struct mptcp_pm_addr_entry *entry)
927{
Kishen Maloor029744c2022-02-09 17:25:08 -0800928 int addrlen = sizeof(struct sockaddr_in);
Geliang Tang1729cf12021-02-01 15:09:12 -0800929 struct sockaddr_storage addr;
930 struct mptcp_sock *msk;
931 struct socket *ssock;
932 int backlog = 1024;
933 int err;
934
935 err = sock_create_kern(sock_net(sk), entry->addr.family,
936 SOCK_STREAM, IPPROTO_MPTCP, &entry->lsk);
937 if (err)
938 return err;
939
940 msk = mptcp_sk(entry->lsk->sk);
941 if (!msk) {
942 err = -EINVAL;
943 goto out;
944 }
945
946 ssock = __mptcp_nmpc_socket(msk);
947 if (!ssock) {
948 err = -EINVAL;
949 goto out;
950 }
951
952 mptcp_info2sockaddr(&entry->addr, &addr, entry->addr.family);
Kishen Maloor029744c2022-02-09 17:25:08 -0800953#if IS_ENABLED(CONFIG_MPTCP_IPV6)
954 if (entry->addr.family == AF_INET6)
955 addrlen = sizeof(struct sockaddr_in6);
956#endif
957 err = kernel_bind(ssock, (struct sockaddr *)&addr, addrlen);
Geliang Tang1729cf12021-02-01 15:09:12 -0800958 if (err) {
959 pr_warn("kernel_bind error, err=%d", err);
960 goto out;
961 }
962
963 err = kernel_listen(ssock, backlog);
964 if (err) {
965 pr_warn("kernel_listen error, err=%d", err);
966 goto out;
967 }
968
969 return 0;
970
971out:
972 sock_release(entry->lsk);
973 return err;
974}
975
Paolo Abeni01cacb02020-03-27 14:48:51 -0700976int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc)
977{
978 struct mptcp_pm_addr_entry *entry;
979 struct mptcp_addr_info skc_local;
980 struct mptcp_addr_info msk_local;
981 struct pm_nl_pernet *pernet;
982 int ret = -1;
983
984 if (WARN_ON_ONCE(!msk))
985 return -1;
986
987 /* The 0 ID mapping is defined by the first subflow, copied into the msk
988 * addr
989 */
990 local_address((struct sock_common *)msk, &msk_local);
Geliang Tang57025812020-09-08 10:49:38 +0800991 local_address((struct sock_common *)skc, &skc_local);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700992 if (addresses_equal(&msk_local, &skc_local, false))
993 return 0;
994
Geliang Tang57025812020-09-08 10:49:38 +0800995 if (address_zero(&skc_local))
996 return 0;
997
Paolo Abeni01cacb02020-03-27 14:48:51 -0700998 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
999
1000 rcu_read_lock();
1001 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
Geliang Tang60b57bf2021-02-01 15:09:16 -08001002 if (addresses_equal(&entry->addr, &skc_local, entry->addr.port)) {
Paolo Abeni01cacb02020-03-27 14:48:51 -07001003 ret = entry->addr.id;
1004 break;
1005 }
1006 }
1007 rcu_read_unlock();
1008 if (ret >= 0)
1009 return ret;
1010
1011 /* address not found, add to local list */
Geliang Tangf612eb72020-09-09 11:01:24 +08001012 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001013 if (!entry)
1014 return -ENOMEM;
1015
Paolo Abeni01cacb02020-03-27 14:48:51 -07001016 entry->addr = skc_local;
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001017 entry->addr.id = 0;
Geliang Tang1729cf12021-02-01 15:09:12 -08001018 entry->addr.port = 0;
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001019 entry->ifindex = 0;
1020 entry->flags = 0;
Geliang Tang1729cf12021-02-01 15:09:12 -08001021 entry->lsk = NULL;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001022 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
1023 if (ret < 0)
1024 kfree(entry);
1025
1026 return ret;
1027}
1028
1029void mptcp_pm_nl_data_init(struct mptcp_sock *msk)
1030{
1031 struct mptcp_pm_data *pm = &msk->pm;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001032 bool subflows;
1033
Geliang Tanga914e582021-02-01 15:09:07 -08001034 subflows = !!mptcp_pm_get_subflows_max(msk);
1035 WRITE_ONCE(pm->work_pending, (!!mptcp_pm_get_local_addr_max(msk) && subflows) ||
1036 !!mptcp_pm_get_add_addr_signal_max(msk));
1037 WRITE_ONCE(pm->accept_addr, !!mptcp_pm_get_add_addr_accept_max(msk) && subflows);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001038 WRITE_ONCE(pm->accept_subflow, subflows);
1039}
1040
Florian Westphalb911c972021-02-12 16:00:01 -08001041#define MPTCP_PM_CMD_GRP_OFFSET 0
1042#define MPTCP_PM_EV_GRP_OFFSET 1
Paolo Abeni01cacb02020-03-27 14:48:51 -07001043
1044static const struct genl_multicast_group mptcp_pm_mcgrps[] = {
1045 [MPTCP_PM_CMD_GRP_OFFSET] = { .name = MPTCP_PM_CMD_GRP_NAME, },
Florian Westphalb911c972021-02-12 16:00:01 -08001046 [MPTCP_PM_EV_GRP_OFFSET] = { .name = MPTCP_PM_EV_GRP_NAME,
1047 .flags = GENL_UNS_ADMIN_PERM,
1048 },
Paolo Abeni01cacb02020-03-27 14:48:51 -07001049};
1050
1051static const struct nla_policy
1052mptcp_pm_addr_policy[MPTCP_PM_ADDR_ATTR_MAX + 1] = {
1053 [MPTCP_PM_ADDR_ATTR_FAMILY] = { .type = NLA_U16, },
1054 [MPTCP_PM_ADDR_ATTR_ID] = { .type = NLA_U8, },
1055 [MPTCP_PM_ADDR_ATTR_ADDR4] = { .type = NLA_U32, },
Johannes Berg81408602020-08-18 10:17:31 +02001056 [MPTCP_PM_ADDR_ATTR_ADDR6] =
1057 NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
Paolo Abeni01cacb02020-03-27 14:48:51 -07001058 [MPTCP_PM_ADDR_ATTR_PORT] = { .type = NLA_U16 },
1059 [MPTCP_PM_ADDR_ATTR_FLAGS] = { .type = NLA_U32 },
1060 [MPTCP_PM_ADDR_ATTR_IF_IDX] = { .type = NLA_S32 },
1061};
1062
1063static const struct nla_policy mptcp_pm_policy[MPTCP_PM_ATTR_MAX + 1] = {
1064 [MPTCP_PM_ATTR_ADDR] =
1065 NLA_POLICY_NESTED(mptcp_pm_addr_policy),
1066 [MPTCP_PM_ATTR_RCV_ADD_ADDRS] = { .type = NLA_U32, },
1067 [MPTCP_PM_ATTR_SUBFLOWS] = { .type = NLA_U32, },
1068};
1069
Paolo Abeniff5a0b42021-08-13 15:15:45 -07001070void mptcp_pm_nl_subflow_chk_stale(const struct mptcp_sock *msk, struct sock *ssk)
1071{
1072 struct mptcp_subflow_context *iter, *subflow = mptcp_subflow_ctx(ssk);
1073 struct sock *sk = (struct sock *)msk;
1074 unsigned int active_max_loss_cnt;
1075 struct net *net = sock_net(sk);
1076 unsigned int stale_loss_cnt;
1077 bool slow;
1078
1079 stale_loss_cnt = mptcp_stale_loss_cnt(net);
1080 if (subflow->stale || !stale_loss_cnt || subflow->stale_count <= stale_loss_cnt)
1081 return;
1082
1083 /* look for another available subflow not in loss state */
1084 active_max_loss_cnt = max_t(int, stale_loss_cnt - 1, 1);
1085 mptcp_for_each_subflow(msk, iter) {
1086 if (iter != subflow && mptcp_subflow_active(iter) &&
1087 iter->stale_count < active_max_loss_cnt) {
1088 /* we have some alternatives, try to mark this subflow as idle ...*/
1089 slow = lock_sock_fast(ssk);
1090 if (!tcp_rtx_and_write_queues_empty(ssk)) {
1091 subflow->stale = 1;
1092 __mptcp_retransmit_pending_data(sk);
Paolo Abenifc1b4e32021-08-13 15:15:46 -07001093 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_SUBFLOWSTALE);
Paolo Abeniff5a0b42021-08-13 15:15:45 -07001094 }
1095 unlock_sock_fast(ssk, slow);
1096
1097 /* always try to push the pending data regarless of re-injections:
1098 * we can possibly use backup subflows now, and subflow selection
1099 * is cheap under the msk socket lock
1100 */
1101 __mptcp_push_pending(sk, 0);
1102 return;
1103 }
1104 }
1105}
1106
Paolo Abeni01cacb02020-03-27 14:48:51 -07001107static int mptcp_pm_family_to_addr(int family)
1108{
1109#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1110 if (family == AF_INET6)
1111 return MPTCP_PM_ADDR_ATTR_ADDR6;
1112#endif
1113 return MPTCP_PM_ADDR_ATTR_ADDR4;
1114}
1115
1116static int mptcp_pm_parse_addr(struct nlattr *attr, struct genl_info *info,
1117 bool require_family,
1118 struct mptcp_pm_addr_entry *entry)
1119{
1120 struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1];
1121 int err, addr_addr;
1122
1123 if (!attr) {
1124 GENL_SET_ERR_MSG(info, "missing address info");
1125 return -EINVAL;
1126 }
1127
1128 /* no validation needed - was already done via nested policy */
1129 err = nla_parse_nested_deprecated(tb, MPTCP_PM_ADDR_ATTR_MAX, attr,
1130 mptcp_pm_addr_policy, info->extack);
1131 if (err)
1132 return err;
1133
1134 memset(entry, 0, sizeof(*entry));
1135 if (!tb[MPTCP_PM_ADDR_ATTR_FAMILY]) {
1136 if (!require_family)
1137 goto skip_family;
1138
1139 NL_SET_ERR_MSG_ATTR(info->extack, attr,
1140 "missing family");
1141 return -EINVAL;
1142 }
1143
1144 entry->addr.family = nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_FAMILY]);
1145 if (entry->addr.family != AF_INET
1146#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1147 && entry->addr.family != AF_INET6
1148#endif
1149 ) {
1150 NL_SET_ERR_MSG_ATTR(info->extack, attr,
1151 "unknown address family");
1152 return -EINVAL;
1153 }
1154 addr_addr = mptcp_pm_family_to_addr(entry->addr.family);
1155 if (!tb[addr_addr]) {
1156 NL_SET_ERR_MSG_ATTR(info->extack, attr,
1157 "missing address data");
1158 return -EINVAL;
1159 }
1160
1161#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1162 if (entry->addr.family == AF_INET6)
1163 entry->addr.addr6 = nla_get_in6_addr(tb[addr_addr]);
1164 else
1165#endif
1166 entry->addr.addr.s_addr = nla_get_in_addr(tb[addr_addr]);
1167
1168skip_family:
Paolo Abenief0da3b2020-09-14 10:01:15 +02001169 if (tb[MPTCP_PM_ADDR_ATTR_IF_IDX]) {
1170 u32 val = nla_get_s32(tb[MPTCP_PM_ADDR_ATTR_IF_IDX]);
1171
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001172 entry->ifindex = val;
Paolo Abenief0da3b2020-09-14 10:01:15 +02001173 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001174
1175 if (tb[MPTCP_PM_ADDR_ATTR_ID])
1176 entry->addr.id = nla_get_u8(tb[MPTCP_PM_ADDR_ATTR_ID]);
1177
1178 if (tb[MPTCP_PM_ADDR_ATTR_FLAGS])
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001179 entry->flags = nla_get_u32(tb[MPTCP_PM_ADDR_ATTR_FLAGS]);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001180
Jianguo Wueb5fb622021-05-27 16:54:28 -07001181 if (tb[MPTCP_PM_ADDR_ATTR_PORT]) {
1182 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) {
1183 NL_SET_ERR_MSG_ATTR(info->extack, attr,
1184 "flags must have signal when using port");
1185 return -EINVAL;
1186 }
Geliang Tanga77e9172021-02-01 15:09:17 -08001187 entry->addr.port = htons(nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_PORT]));
Jianguo Wueb5fb622021-05-27 16:54:28 -07001188 }
Geliang Tanga77e9172021-02-01 15:09:17 -08001189
Paolo Abeni01cacb02020-03-27 14:48:51 -07001190 return 0;
1191}
1192
1193static struct pm_nl_pernet *genl_info_pm_nl(struct genl_info *info)
1194{
1195 return net_generic(genl_info_net(info), pm_nl_pernet_id);
1196}
1197
Geliang Tang875b76712021-02-01 15:09:08 -08001198static int mptcp_nl_add_subflow_or_signal_addr(struct net *net)
1199{
1200 struct mptcp_sock *msk;
1201 long s_slot = 0, s_num = 0;
1202
1203 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1204 struct sock *sk = (struct sock *)msk;
1205
1206 if (!READ_ONCE(msk->fully_established))
1207 goto next;
1208
1209 lock_sock(sk);
1210 spin_lock_bh(&msk->pm.lock);
1211 mptcp_pm_create_subflow_or_signal_addr(msk);
1212 spin_unlock_bh(&msk->pm.lock);
1213 release_sock(sk);
1214
1215next:
1216 sock_put(sk);
1217 cond_resched();
1218 }
1219
1220 return 0;
1221}
1222
Paolo Abeni01cacb02020-03-27 14:48:51 -07001223static int mptcp_nl_cmd_add_addr(struct sk_buff *skb, struct genl_info *info)
1224{
1225 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1226 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1227 struct mptcp_pm_addr_entry addr, *entry;
1228 int ret;
1229
1230 ret = mptcp_pm_parse_addr(attr, info, true, &addr);
1231 if (ret < 0)
1232 return ret;
1233
1234 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1235 if (!entry) {
1236 GENL_SET_ERR_MSG(info, "can't allocate addr");
1237 return -ENOMEM;
1238 }
1239
1240 *entry = addr;
Geliang Tang1729cf12021-02-01 15:09:12 -08001241 if (entry->addr.port) {
1242 ret = mptcp_pm_nl_create_listen_socket(skb->sk, entry);
1243 if (ret) {
1244 GENL_SET_ERR_MSG(info, "create listen socket error");
1245 kfree(entry);
1246 return ret;
1247 }
1248 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001249 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
1250 if (ret < 0) {
1251 GENL_SET_ERR_MSG(info, "too many addresses or duplicate one");
Geliang Tang1729cf12021-02-01 15:09:12 -08001252 if (entry->lsk)
1253 sock_release(entry->lsk);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001254 kfree(entry);
1255 return ret;
1256 }
1257
Geliang Tang875b76712021-02-01 15:09:08 -08001258 mptcp_nl_add_subflow_or_signal_addr(sock_net(skb->sk));
1259
Paolo Abeni01cacb02020-03-27 14:48:51 -07001260 return 0;
1261}
1262
Geliang Tangee285252021-08-17 15:07:22 -07001263int mptcp_pm_get_flags_and_ifindex_by_id(struct net *net, unsigned int id,
1264 u8 *flags, int *ifindex)
1265{
1266 struct mptcp_pm_addr_entry *entry;
1267
1268 *flags = 0;
1269 *ifindex = 0;
1270
1271 if (id) {
1272 rcu_read_lock();
1273 entry = __lookup_addr_by_id(net_generic(net, pm_nl_pernet_id), id);
1274 if (entry) {
1275 *flags = entry->flags;
1276 *ifindex = entry->ifindex;
1277 }
1278 rcu_read_unlock();
1279 }
1280
1281 return 0;
1282}
1283
Geliang Tangb6c08382020-09-24 08:29:54 +08001284static bool remove_anno_list_by_saddr(struct mptcp_sock *msk,
1285 struct mptcp_addr_info *addr)
1286{
Geliang Tang00cfd772020-09-24 08:30:02 +08001287 struct mptcp_pm_add_entry *entry;
Geliang Tangb6c08382020-09-24 08:29:54 +08001288
Davide Carattid58300c2021-05-25 14:23:13 -07001289 entry = mptcp_pm_del_add_timer(msk, addr, false);
Geliang Tang00cfd772020-09-24 08:30:02 +08001290 if (entry) {
1291 list_del(&entry->list);
1292 kfree(entry);
1293 return true;
Geliang Tangb6c08382020-09-24 08:29:54 +08001294 }
1295
1296 return false;
1297}
1298
1299static bool mptcp_pm_remove_anno_addr(struct mptcp_sock *msk,
1300 struct mptcp_addr_info *addr,
1301 bool force)
1302{
Geliang Tangcbde2782021-03-12 17:16:12 -08001303 struct mptcp_rm_list list = { .nr = 0 };
Geliang Tangb6c08382020-09-24 08:29:54 +08001304 bool ret;
1305
Geliang Tangcbde2782021-03-12 17:16:12 -08001306 list.ids[list.nr++] = addr->id;
1307
Geliang Tangb6c08382020-09-24 08:29:54 +08001308 ret = remove_anno_list_by_saddr(msk, addr);
Geliang Tang00cfd772020-09-24 08:30:02 +08001309 if (ret || force) {
1310 spin_lock_bh(&msk->pm.lock);
Geliang Tangcbde2782021-03-12 17:16:12 -08001311 mptcp_pm_remove_addr(msk, &list);
Geliang Tang00cfd772020-09-24 08:30:02 +08001312 spin_unlock_bh(&msk->pm.lock);
1313 }
Geliang Tangb6c08382020-09-24 08:29:54 +08001314 return ret;
1315}
1316
1317static int mptcp_nl_remove_subflow_and_signal_addr(struct net *net,
1318 struct mptcp_addr_info *addr)
1319{
1320 struct mptcp_sock *msk;
1321 long s_slot = 0, s_num = 0;
Geliang Tangddd14bb2021-03-12 17:16:16 -08001322 struct mptcp_rm_list list = { .nr = 0 };
Geliang Tangb6c08382020-09-24 08:29:54 +08001323
1324 pr_debug("remove_id=%d", addr->id);
1325
Geliang Tangddd14bb2021-03-12 17:16:16 -08001326 list.ids[list.nr++] = addr->id;
1327
Geliang Tangb6c08382020-09-24 08:29:54 +08001328 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1329 struct sock *sk = (struct sock *)msk;
1330 bool remove_subflow;
1331
1332 if (list_empty(&msk->conn_list)) {
1333 mptcp_pm_remove_anno_addr(msk, addr, false);
1334 goto next;
1335 }
1336
1337 lock_sock(sk);
1338 remove_subflow = lookup_subflow_by_saddr(&msk->conn_list, addr);
1339 mptcp_pm_remove_anno_addr(msk, addr, remove_subflow);
1340 if (remove_subflow)
Geliang Tangddd14bb2021-03-12 17:16:16 -08001341 mptcp_pm_remove_subflow(msk, &list);
Geliang Tangb6c08382020-09-24 08:29:54 +08001342 release_sock(sk);
1343
1344next:
1345 sock_put(sk);
1346 cond_resched();
1347 }
1348
1349 return 0;
1350}
1351
Paolo Abenia0eea5f2021-08-18 16:42:36 -07001352/* caller must ensure the RCU grace period is already elapsed */
1353static void __mptcp_pm_release_addr_entry(struct mptcp_pm_addr_entry *entry)
Geliang Tang1729cf12021-02-01 15:09:12 -08001354{
Paolo Abenia0eea5f2021-08-18 16:42:36 -07001355 if (entry->lsk)
1356 sock_release(entry->lsk);
1357 kfree(entry);
Geliang Tang1729cf12021-02-01 15:09:12 -08001358}
1359
Geliang Tang740d7982021-03-30 17:08:53 -07001360static int mptcp_nl_remove_id_zero_address(struct net *net,
1361 struct mptcp_addr_info *addr)
1362{
1363 struct mptcp_rm_list list = { .nr = 0 };
1364 long s_slot = 0, s_num = 0;
1365 struct mptcp_sock *msk;
1366
1367 list.ids[list.nr++] = 0;
1368
1369 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1370 struct sock *sk = (struct sock *)msk;
1371 struct mptcp_addr_info msk_local;
1372
1373 if (list_empty(&msk->conn_list))
1374 goto next;
1375
1376 local_address((struct sock_common *)msk, &msk_local);
1377 if (!addresses_equal(&msk_local, addr, addr->port))
1378 goto next;
1379
1380 lock_sock(sk);
1381 spin_lock_bh(&msk->pm.lock);
1382 mptcp_pm_remove_addr(msk, &list);
1383 mptcp_pm_nl_rm_subflow_received(msk, &list);
1384 spin_unlock_bh(&msk->pm.lock);
1385 release_sock(sk);
1386
1387next:
1388 sock_put(sk);
1389 cond_resched();
1390 }
1391
1392 return 0;
1393}
1394
Paolo Abeni01cacb02020-03-27 14:48:51 -07001395static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info)
1396{
1397 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1398 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1399 struct mptcp_pm_addr_entry addr, *entry;
Geliang Tang72603d22021-02-01 15:09:06 -08001400 unsigned int addr_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001401 int ret;
1402
1403 ret = mptcp_pm_parse_addr(attr, info, false, &addr);
1404 if (ret < 0)
1405 return ret;
1406
Geliang Tang740d7982021-03-30 17:08:53 -07001407 /* the zero id address is special: the first address used by the msk
1408 * always gets such an id, so different subflows can have different zero
1409 * id addresses. Additionally zero id is not accounted for in id_bitmap.
1410 * Let's use an 'mptcp_rm_list' instead of the common remove code.
1411 */
1412 if (addr.addr.id == 0)
1413 return mptcp_nl_remove_id_zero_address(sock_net(skb->sk), &addr.addr);
1414
Paolo Abeni01cacb02020-03-27 14:48:51 -07001415 spin_lock_bh(&pernet->lock);
1416 entry = __lookup_addr_by_id(pernet, addr.addr.id);
1417 if (!entry) {
1418 GENL_SET_ERR_MSG(info, "address not found");
Geliang Tangb6c08382020-09-24 08:29:54 +08001419 spin_unlock_bh(&pernet->lock);
1420 return -EINVAL;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001421 }
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001422 if (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
Geliang Tang72603d22021-02-01 15:09:06 -08001423 addr_max = pernet->add_addr_signal_max;
1424 WRITE_ONCE(pernet->add_addr_signal_max, addr_max - 1);
1425 }
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001426 if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) {
Geliang Tang72603d22021-02-01 15:09:06 -08001427 addr_max = pernet->local_addr_max;
1428 WRITE_ONCE(pernet->local_addr_max, addr_max - 1);
1429 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001430
1431 pernet->addrs--;
1432 list_del_rcu(&entry->list);
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001433 __clear_bit(entry->addr.id, pernet->id_bitmap);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001434 spin_unlock_bh(&pernet->lock);
Geliang Tangb6c08382020-09-24 08:29:54 +08001435
1436 mptcp_nl_remove_subflow_and_signal_addr(sock_net(skb->sk), &entry->addr);
Paolo Abenia0eea5f2021-08-18 16:42:36 -07001437 synchronize_rcu();
1438 __mptcp_pm_release_addr_entry(entry);
Geliang Tangb6c08382020-09-24 08:29:54 +08001439
Paolo Abeni01cacb02020-03-27 14:48:51 -07001440 return ret;
1441}
1442
Geliang Tang06faa222021-03-12 17:16:17 -08001443static void mptcp_pm_remove_addrs_and_subflows(struct mptcp_sock *msk,
1444 struct list_head *rm_list)
1445{
1446 struct mptcp_rm_list alist = { .nr = 0 }, slist = { .nr = 0 };
1447 struct mptcp_pm_addr_entry *entry;
1448
1449 list_for_each_entry(entry, rm_list, list) {
1450 if (lookup_subflow_by_saddr(&msk->conn_list, &entry->addr) &&
1451 alist.nr < MPTCP_RM_IDS_MAX &&
1452 slist.nr < MPTCP_RM_IDS_MAX) {
1453 alist.ids[alist.nr++] = entry->addr.id;
1454 slist.ids[slist.nr++] = entry->addr.id;
1455 } else if (remove_anno_list_by_saddr(msk, &entry->addr) &&
1456 alist.nr < MPTCP_RM_IDS_MAX) {
1457 alist.ids[alist.nr++] = entry->addr.id;
1458 }
1459 }
1460
1461 if (alist.nr) {
1462 spin_lock_bh(&msk->pm.lock);
1463 mptcp_pm_remove_addr(msk, &alist);
1464 spin_unlock_bh(&msk->pm.lock);
1465 }
1466 if (slist.nr)
1467 mptcp_pm_remove_subflow(msk, &slist);
1468}
1469
1470static void mptcp_nl_remove_addrs_list(struct net *net,
1471 struct list_head *rm_list)
1472{
1473 long s_slot = 0, s_num = 0;
1474 struct mptcp_sock *msk;
1475
1476 if (list_empty(rm_list))
1477 return;
1478
1479 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1480 struct sock *sk = (struct sock *)msk;
1481
1482 lock_sock(sk);
1483 mptcp_pm_remove_addrs_and_subflows(msk, rm_list);
1484 release_sock(sk);
1485
1486 sock_put(sk);
1487 cond_resched();
1488 }
1489}
1490
Paolo Abenia0eea5f2021-08-18 16:42:36 -07001491/* caller must ensure the RCU grace period is already elapsed */
Geliang Tang0e4a3e62021-03-12 17:16:18 -08001492static void __flush_addrs(struct list_head *list)
Paolo Abeni01cacb02020-03-27 14:48:51 -07001493{
Geliang Tang141694d2020-12-10 14:24:59 -08001494 while (!list_empty(list)) {
Paolo Abeni01cacb02020-03-27 14:48:51 -07001495 struct mptcp_pm_addr_entry *cur;
1496
Geliang Tang141694d2020-12-10 14:24:59 -08001497 cur = list_entry(list->next,
Paolo Abeni01cacb02020-03-27 14:48:51 -07001498 struct mptcp_pm_addr_entry, list);
1499 list_del_rcu(&cur->list);
Paolo Abenia0eea5f2021-08-18 16:42:36 -07001500 __mptcp_pm_release_addr_entry(cur);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001501 }
1502}
1503
1504static void __reset_counters(struct pm_nl_pernet *pernet)
1505{
Geliang Tang72603d22021-02-01 15:09:06 -08001506 WRITE_ONCE(pernet->add_addr_signal_max, 0);
1507 WRITE_ONCE(pernet->add_addr_accept_max, 0);
1508 WRITE_ONCE(pernet->local_addr_max, 0);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001509 pernet->addrs = 0;
1510}
1511
1512static int mptcp_nl_cmd_flush_addrs(struct sk_buff *skb, struct genl_info *info)
1513{
1514 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
Geliang Tang141694d2020-12-10 14:24:59 -08001515 LIST_HEAD(free_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001516
1517 spin_lock_bh(&pernet->lock);
Geliang Tang141694d2020-12-10 14:24:59 -08001518 list_splice_init(&pernet->local_addr_list, &free_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001519 __reset_counters(pernet);
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001520 pernet->next_id = 1;
Paolo Abeni86e39e02022-01-06 16:20:22 -08001521 bitmap_zero(pernet->id_bitmap, MPTCP_PM_MAX_ADDR_ID + 1);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001522 spin_unlock_bh(&pernet->lock);
Geliang Tang0e4a3e62021-03-12 17:16:18 -08001523 mptcp_nl_remove_addrs_list(sock_net(skb->sk), &free_list);
Paolo Abenia0eea5f2021-08-18 16:42:36 -07001524 synchronize_rcu();
Geliang Tang0e4a3e62021-03-12 17:16:18 -08001525 __flush_addrs(&free_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001526 return 0;
1527}
1528
1529static int mptcp_nl_fill_addr(struct sk_buff *skb,
1530 struct mptcp_pm_addr_entry *entry)
1531{
1532 struct mptcp_addr_info *addr = &entry->addr;
1533 struct nlattr *attr;
1534
1535 attr = nla_nest_start(skb, MPTCP_PM_ATTR_ADDR);
1536 if (!attr)
1537 return -EMSGSIZE;
1538
1539 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_FAMILY, addr->family))
1540 goto nla_put_failure;
Geliang Tanga77e9172021-02-01 15:09:17 -08001541 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_PORT, ntohs(addr->port)))
1542 goto nla_put_failure;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001543 if (nla_put_u8(skb, MPTCP_PM_ADDR_ATTR_ID, addr->id))
1544 goto nla_put_failure;
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001545 if (nla_put_u32(skb, MPTCP_PM_ADDR_ATTR_FLAGS, entry->flags))
Paolo Abeni01cacb02020-03-27 14:48:51 -07001546 goto nla_put_failure;
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001547 if (entry->ifindex &&
1548 nla_put_s32(skb, MPTCP_PM_ADDR_ATTR_IF_IDX, entry->ifindex))
Paolo Abeni01cacb02020-03-27 14:48:51 -07001549 goto nla_put_failure;
1550
Bo YUb4e0f9a2020-04-23 10:10:03 +08001551 if (addr->family == AF_INET &&
1552 nla_put_in_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR4,
1553 addr->addr.s_addr))
1554 goto nla_put_failure;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001555#if IS_ENABLED(CONFIG_MPTCP_IPV6)
Bo YUb4e0f9a2020-04-23 10:10:03 +08001556 else if (addr->family == AF_INET6 &&
1557 nla_put_in6_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR6, &addr->addr6))
1558 goto nla_put_failure;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001559#endif
1560 nla_nest_end(skb, attr);
1561 return 0;
1562
1563nla_put_failure:
1564 nla_nest_cancel(skb, attr);
1565 return -EMSGSIZE;
1566}
1567
1568static int mptcp_nl_cmd_get_addr(struct sk_buff *skb, struct genl_info *info)
1569{
1570 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1571 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1572 struct mptcp_pm_addr_entry addr, *entry;
1573 struct sk_buff *msg;
1574 void *reply;
1575 int ret;
1576
1577 ret = mptcp_pm_parse_addr(attr, info, false, &addr);
1578 if (ret < 0)
1579 return ret;
1580
1581 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1582 if (!msg)
1583 return -ENOMEM;
1584
1585 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1586 info->genlhdr->cmd);
1587 if (!reply) {
1588 GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1589 ret = -EMSGSIZE;
1590 goto fail;
1591 }
1592
1593 spin_lock_bh(&pernet->lock);
1594 entry = __lookup_addr_by_id(pernet, addr.addr.id);
1595 if (!entry) {
1596 GENL_SET_ERR_MSG(info, "address not found");
1597 ret = -EINVAL;
1598 goto unlock_fail;
1599 }
1600
1601 ret = mptcp_nl_fill_addr(msg, entry);
1602 if (ret)
1603 goto unlock_fail;
1604
1605 genlmsg_end(msg, reply);
1606 ret = genlmsg_reply(msg, info);
1607 spin_unlock_bh(&pernet->lock);
1608 return ret;
1609
1610unlock_fail:
1611 spin_unlock_bh(&pernet->lock);
1612
1613fail:
1614 nlmsg_free(msg);
1615 return ret;
1616}
1617
1618static int mptcp_nl_cmd_dump_addrs(struct sk_buff *msg,
1619 struct netlink_callback *cb)
1620{
1621 struct net *net = sock_net(msg->sk);
1622 struct mptcp_pm_addr_entry *entry;
1623 struct pm_nl_pernet *pernet;
1624 int id = cb->args[0];
1625 void *hdr;
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001626 int i;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001627
1628 pernet = net_generic(net, pm_nl_pernet_id);
1629
1630 spin_lock_bh(&pernet->lock);
Paolo Abeni86e39e02022-01-06 16:20:22 -08001631 for (i = id; i < MPTCP_PM_MAX_ADDR_ID + 1; i++) {
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001632 if (test_bit(i, pernet->id_bitmap)) {
1633 entry = __lookup_addr_by_id(pernet, i);
1634 if (!entry)
1635 break;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001636
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001637 if (entry->addr.id <= id)
1638 continue;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001639
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001640 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid,
1641 cb->nlh->nlmsg_seq, &mptcp_genl_family,
1642 NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR);
1643 if (!hdr)
1644 break;
1645
1646 if (mptcp_nl_fill_addr(msg, entry) < 0) {
1647 genlmsg_cancel(msg, hdr);
1648 break;
1649 }
1650
1651 id = entry->addr.id;
1652 genlmsg_end(msg, hdr);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001653 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001654 }
1655 spin_unlock_bh(&pernet->lock);
1656
1657 cb->args[0] = id;
1658 return msg->len;
1659}
1660
1661static int parse_limit(struct genl_info *info, int id, unsigned int *limit)
1662{
1663 struct nlattr *attr = info->attrs[id];
1664
1665 if (!attr)
1666 return 0;
1667
1668 *limit = nla_get_u32(attr);
1669 if (*limit > MPTCP_PM_ADDR_MAX) {
1670 GENL_SET_ERR_MSG(info, "limit greater than maximum");
1671 return -EINVAL;
1672 }
1673 return 0;
1674}
1675
1676static int
1677mptcp_nl_cmd_set_limits(struct sk_buff *skb, struct genl_info *info)
1678{
1679 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1680 unsigned int rcv_addrs, subflows;
1681 int ret;
1682
1683 spin_lock_bh(&pernet->lock);
1684 rcv_addrs = pernet->add_addr_accept_max;
1685 ret = parse_limit(info, MPTCP_PM_ATTR_RCV_ADD_ADDRS, &rcv_addrs);
1686 if (ret)
1687 goto unlock;
1688
1689 subflows = pernet->subflows_max;
1690 ret = parse_limit(info, MPTCP_PM_ATTR_SUBFLOWS, &subflows);
1691 if (ret)
1692 goto unlock;
1693
1694 WRITE_ONCE(pernet->add_addr_accept_max, rcv_addrs);
1695 WRITE_ONCE(pernet->subflows_max, subflows);
1696
1697unlock:
1698 spin_unlock_bh(&pernet->lock);
1699 return ret;
1700}
1701
1702static int
1703mptcp_nl_cmd_get_limits(struct sk_buff *skb, struct genl_info *info)
1704{
1705 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1706 struct sk_buff *msg;
1707 void *reply;
1708
1709 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1710 if (!msg)
1711 return -ENOMEM;
1712
1713 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1714 MPTCP_PM_CMD_GET_LIMITS);
1715 if (!reply)
1716 goto fail;
1717
1718 if (nla_put_u32(msg, MPTCP_PM_ATTR_RCV_ADD_ADDRS,
1719 READ_ONCE(pernet->add_addr_accept_max)))
1720 goto fail;
1721
1722 if (nla_put_u32(msg, MPTCP_PM_ATTR_SUBFLOWS,
1723 READ_ONCE(pernet->subflows_max)))
1724 goto fail;
1725
1726 genlmsg_end(msg, reply);
1727 return genlmsg_reply(msg, info);
1728
1729fail:
1730 GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1731 nlmsg_free(msg);
1732 return -EMSGSIZE;
1733}
1734
Geliang Tang0f9f6962021-01-08 16:47:59 -08001735static int mptcp_nl_addr_backup(struct net *net,
1736 struct mptcp_addr_info *addr,
1737 u8 bkup)
1738{
1739 long s_slot = 0, s_num = 0;
1740 struct mptcp_sock *msk;
1741 int ret = -EINVAL;
1742
1743 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1744 struct sock *sk = (struct sock *)msk;
1745
1746 if (list_empty(&msk->conn_list))
1747 goto next;
1748
1749 lock_sock(sk);
1750 spin_lock_bh(&msk->pm.lock);
1751 ret = mptcp_pm_nl_mp_prio_send_ack(msk, addr, bkup);
1752 spin_unlock_bh(&msk->pm.lock);
1753 release_sock(sk);
1754
1755next:
1756 sock_put(sk);
1757 cond_resched();
1758 }
1759
1760 return ret;
1761}
1762
1763static int mptcp_nl_cmd_set_flags(struct sk_buff *skb, struct genl_info *info)
1764{
Davide Caratti602837e2021-12-03 14:35:36 -08001765 struct mptcp_pm_addr_entry addr = { .addr = { .family = AF_UNSPEC }, }, *entry;
Geliang Tang0f9f6962021-01-08 16:47:59 -08001766 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1767 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
Geliang Tang0f9f6962021-01-08 16:47:59 -08001768 struct net *net = sock_net(skb->sk);
Davide Caratti602837e2021-12-03 14:35:36 -08001769 u8 bkup = 0, lookup_by_id = 0;
Geliang Tang0f9f6962021-01-08 16:47:59 -08001770 int ret;
1771
Davide Caratti602837e2021-12-03 14:35:36 -08001772 ret = mptcp_pm_parse_addr(attr, info, false, &addr);
Geliang Tang0f9f6962021-01-08 16:47:59 -08001773 if (ret < 0)
1774 return ret;
1775
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001776 if (addr.flags & MPTCP_PM_ADDR_FLAG_BACKUP)
Geliang Tang0f9f6962021-01-08 16:47:59 -08001777 bkup = 1;
Davide Caratti602837e2021-12-03 14:35:36 -08001778 if (addr.addr.family == AF_UNSPEC) {
1779 lookup_by_id = 1;
1780 if (!addr.addr.id)
1781 return -EOPNOTSUPP;
1782 }
Geliang Tang0f9f6962021-01-08 16:47:59 -08001783
Paolo Abeni8e9eaca2022-01-20 16:35:27 -08001784 spin_lock_bh(&pernet->lock);
1785 entry = __lookup_addr(pernet, &addr.addr, lookup_by_id);
1786 if (!entry) {
1787 spin_unlock_bh(&pernet->lock);
1788 return -EINVAL;
Geliang Tang0f9f6962021-01-08 16:47:59 -08001789 }
1790
Paolo Abeni8e9eaca2022-01-20 16:35:27 -08001791 if (bkup)
1792 entry->flags |= MPTCP_PM_ADDR_FLAG_BACKUP;
1793 else
1794 entry->flags &= ~MPTCP_PM_ADDR_FLAG_BACKUP;
1795 addr = *entry;
1796 spin_unlock_bh(&pernet->lock);
1797
1798 mptcp_nl_addr_backup(net, &addr.addr, bkup);
Geliang Tang0f9f6962021-01-08 16:47:59 -08001799 return 0;
1800}
1801
Florian Westphalb911c972021-02-12 16:00:01 -08001802static void mptcp_nl_mcast_send(struct net *net, struct sk_buff *nlskb, gfp_t gfp)
1803{
1804 genlmsg_multicast_netns(&mptcp_genl_family, net,
1805 nlskb, 0, MPTCP_PM_EV_GRP_OFFSET, gfp);
1806}
1807
1808static int mptcp_event_add_subflow(struct sk_buff *skb, const struct sock *ssk)
1809{
1810 const struct inet_sock *issk = inet_sk(ssk);
1811 const struct mptcp_subflow_context *sf;
1812
1813 if (nla_put_u16(skb, MPTCP_ATTR_FAMILY, ssk->sk_family))
1814 return -EMSGSIZE;
1815
1816 switch (ssk->sk_family) {
1817 case AF_INET:
1818 if (nla_put_in_addr(skb, MPTCP_ATTR_SADDR4, issk->inet_saddr))
1819 return -EMSGSIZE;
1820 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, issk->inet_daddr))
1821 return -EMSGSIZE;
1822 break;
1823#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1824 case AF_INET6: {
1825 const struct ipv6_pinfo *np = inet6_sk(ssk);
1826
1827 if (nla_put_in6_addr(skb, MPTCP_ATTR_SADDR6, &np->saddr))
1828 return -EMSGSIZE;
1829 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &ssk->sk_v6_daddr))
1830 return -EMSGSIZE;
1831 break;
1832 }
1833#endif
1834 default:
1835 WARN_ON_ONCE(1);
1836 return -EMSGSIZE;
1837 }
1838
1839 if (nla_put_be16(skb, MPTCP_ATTR_SPORT, issk->inet_sport))
1840 return -EMSGSIZE;
1841 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, issk->inet_dport))
1842 return -EMSGSIZE;
1843
1844 sf = mptcp_subflow_ctx(ssk);
1845 if (WARN_ON_ONCE(!sf))
1846 return -EINVAL;
1847
1848 if (nla_put_u8(skb, MPTCP_ATTR_LOC_ID, sf->local_id))
1849 return -EMSGSIZE;
1850
1851 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, sf->remote_id))
1852 return -EMSGSIZE;
1853
1854 return 0;
1855}
1856
1857static int mptcp_event_put_token_and_ssk(struct sk_buff *skb,
1858 const struct mptcp_sock *msk,
1859 const struct sock *ssk)
1860{
1861 const struct sock *sk = (const struct sock *)msk;
1862 const struct mptcp_subflow_context *sf;
1863 u8 sk_err;
1864
1865 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1866 return -EMSGSIZE;
1867
1868 if (mptcp_event_add_subflow(skb, ssk))
1869 return -EMSGSIZE;
1870
1871 sf = mptcp_subflow_ctx(ssk);
1872 if (WARN_ON_ONCE(!sf))
1873 return -EINVAL;
1874
1875 if (nla_put_u8(skb, MPTCP_ATTR_BACKUP, sf->backup))
1876 return -EMSGSIZE;
1877
1878 if (ssk->sk_bound_dev_if &&
1879 nla_put_s32(skb, MPTCP_ATTR_IF_IDX, ssk->sk_bound_dev_if))
1880 return -EMSGSIZE;
1881
1882 sk_err = ssk->sk_err;
1883 if (sk_err && sk->sk_state == TCP_ESTABLISHED &&
1884 nla_put_u8(skb, MPTCP_ATTR_ERROR, sk_err))
1885 return -EMSGSIZE;
1886
1887 return 0;
1888}
1889
1890static int mptcp_event_sub_established(struct sk_buff *skb,
1891 const struct mptcp_sock *msk,
1892 const struct sock *ssk)
1893{
1894 return mptcp_event_put_token_and_ssk(skb, msk, ssk);
1895}
1896
1897static int mptcp_event_sub_closed(struct sk_buff *skb,
1898 const struct mptcp_sock *msk,
1899 const struct sock *ssk)
1900{
Florian Westphaldc87efd2021-04-01 16:19:44 -07001901 const struct mptcp_subflow_context *sf;
1902
Florian Westphalb911c972021-02-12 16:00:01 -08001903 if (mptcp_event_put_token_and_ssk(skb, msk, ssk))
1904 return -EMSGSIZE;
1905
Florian Westphaldc87efd2021-04-01 16:19:44 -07001906 sf = mptcp_subflow_ctx(ssk);
1907 if (!sf->reset_seen)
1908 return 0;
1909
1910 if (nla_put_u32(skb, MPTCP_ATTR_RESET_REASON, sf->reset_reason))
1911 return -EMSGSIZE;
1912
1913 if (nla_put_u32(skb, MPTCP_ATTR_RESET_FLAGS, sf->reset_transient))
1914 return -EMSGSIZE;
1915
Florian Westphalb911c972021-02-12 16:00:01 -08001916 return 0;
1917}
1918
1919static int mptcp_event_created(struct sk_buff *skb,
1920 const struct mptcp_sock *msk,
1921 const struct sock *ssk)
1922{
1923 int err = nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token);
1924
1925 if (err)
1926 return err;
1927
1928 return mptcp_event_add_subflow(skb, ssk);
1929}
1930
1931void mptcp_event_addr_removed(const struct mptcp_sock *msk, uint8_t id)
1932{
1933 struct net *net = sock_net((const struct sock *)msk);
1934 struct nlmsghdr *nlh;
1935 struct sk_buff *skb;
1936
1937 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
1938 return;
1939
1940 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1941 if (!skb)
1942 return;
1943
1944 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, MPTCP_EVENT_REMOVED);
1945 if (!nlh)
1946 goto nla_put_failure;
1947
1948 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1949 goto nla_put_failure;
1950
1951 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, id))
1952 goto nla_put_failure;
1953
1954 genlmsg_end(skb, nlh);
1955 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC);
1956 return;
1957
1958nla_put_failure:
1959 kfree_skb(skb);
1960}
1961
1962void mptcp_event_addr_announced(const struct mptcp_sock *msk,
1963 const struct mptcp_addr_info *info)
1964{
1965 struct net *net = sock_net((const struct sock *)msk);
1966 struct nlmsghdr *nlh;
1967 struct sk_buff *skb;
1968
1969 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
1970 return;
1971
1972 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1973 if (!skb)
1974 return;
1975
1976 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0,
1977 MPTCP_EVENT_ANNOUNCED);
1978 if (!nlh)
1979 goto nla_put_failure;
1980
1981 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1982 goto nla_put_failure;
1983
1984 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, info->id))
1985 goto nla_put_failure;
1986
1987 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, info->port))
1988 goto nla_put_failure;
1989
1990 switch (info->family) {
1991 case AF_INET:
1992 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, info->addr.s_addr))
1993 goto nla_put_failure;
1994 break;
1995#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1996 case AF_INET6:
1997 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &info->addr6))
1998 goto nla_put_failure;
1999 break;
2000#endif
2001 default:
2002 WARN_ON_ONCE(1);
2003 goto nla_put_failure;
2004 }
2005
2006 genlmsg_end(skb, nlh);
2007 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC);
2008 return;
2009
2010nla_put_failure:
2011 kfree_skb(skb);
2012}
2013
2014void mptcp_event(enum mptcp_event_type type, const struct mptcp_sock *msk,
2015 const struct sock *ssk, gfp_t gfp)
2016{
2017 struct net *net = sock_net((const struct sock *)msk);
2018 struct nlmsghdr *nlh;
2019 struct sk_buff *skb;
2020
2021 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
2022 return;
2023
2024 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
2025 if (!skb)
2026 return;
2027
2028 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, type);
2029 if (!nlh)
2030 goto nla_put_failure;
2031
2032 switch (type) {
2033 case MPTCP_EVENT_UNSPEC:
2034 WARN_ON_ONCE(1);
2035 break;
2036 case MPTCP_EVENT_CREATED:
2037 case MPTCP_EVENT_ESTABLISHED:
2038 if (mptcp_event_created(skb, msk, ssk) < 0)
2039 goto nla_put_failure;
2040 break;
2041 case MPTCP_EVENT_CLOSED:
2042 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token) < 0)
2043 goto nla_put_failure;
2044 break;
2045 case MPTCP_EVENT_ANNOUNCED:
2046 case MPTCP_EVENT_REMOVED:
2047 /* call mptcp_event_addr_announced()/removed instead */
2048 WARN_ON_ONCE(1);
2049 break;
2050 case MPTCP_EVENT_SUB_ESTABLISHED:
2051 case MPTCP_EVENT_SUB_PRIORITY:
2052 if (mptcp_event_sub_established(skb, msk, ssk) < 0)
2053 goto nla_put_failure;
2054 break;
2055 case MPTCP_EVENT_SUB_CLOSED:
2056 if (mptcp_event_sub_closed(skb, msk, ssk) < 0)
2057 goto nla_put_failure;
2058 break;
2059 }
2060
2061 genlmsg_end(skb, nlh);
2062 mptcp_nl_mcast_send(net, skb, gfp);
2063 return;
2064
2065nla_put_failure:
2066 kfree_skb(skb);
2067}
2068
Rikard Falkeborn674d3ab2020-10-05 01:44:16 +02002069static const struct genl_small_ops mptcp_pm_ops[] = {
Paolo Abeni01cacb02020-03-27 14:48:51 -07002070 {
2071 .cmd = MPTCP_PM_CMD_ADD_ADDR,
2072 .doit = mptcp_nl_cmd_add_addr,
2073 .flags = GENL_ADMIN_PERM,
2074 },
2075 {
2076 .cmd = MPTCP_PM_CMD_DEL_ADDR,
2077 .doit = mptcp_nl_cmd_del_addr,
2078 .flags = GENL_ADMIN_PERM,
2079 },
2080 {
2081 .cmd = MPTCP_PM_CMD_FLUSH_ADDRS,
2082 .doit = mptcp_nl_cmd_flush_addrs,
2083 .flags = GENL_ADMIN_PERM,
2084 },
2085 {
2086 .cmd = MPTCP_PM_CMD_GET_ADDR,
2087 .doit = mptcp_nl_cmd_get_addr,
2088 .dumpit = mptcp_nl_cmd_dump_addrs,
2089 },
2090 {
2091 .cmd = MPTCP_PM_CMD_SET_LIMITS,
2092 .doit = mptcp_nl_cmd_set_limits,
2093 .flags = GENL_ADMIN_PERM,
2094 },
2095 {
2096 .cmd = MPTCP_PM_CMD_GET_LIMITS,
2097 .doit = mptcp_nl_cmd_get_limits,
2098 },
Geliang Tang0f9f6962021-01-08 16:47:59 -08002099 {
2100 .cmd = MPTCP_PM_CMD_SET_FLAGS,
2101 .doit = mptcp_nl_cmd_set_flags,
2102 .flags = GENL_ADMIN_PERM,
2103 },
Paolo Abeni01cacb02020-03-27 14:48:51 -07002104};
2105
2106static struct genl_family mptcp_genl_family __ro_after_init = {
2107 .name = MPTCP_PM_NAME,
2108 .version = MPTCP_PM_VER,
2109 .maxattr = MPTCP_PM_ATTR_MAX,
2110 .policy = mptcp_pm_policy,
2111 .netnsok = true,
2112 .module = THIS_MODULE,
Jakub Kicinski66a9b922020-10-02 14:49:54 -07002113 .small_ops = mptcp_pm_ops,
2114 .n_small_ops = ARRAY_SIZE(mptcp_pm_ops),
Paolo Abeni01cacb02020-03-27 14:48:51 -07002115 .mcgrps = mptcp_pm_mcgrps,
2116 .n_mcgrps = ARRAY_SIZE(mptcp_pm_mcgrps),
2117};
2118
2119static int __net_init pm_nl_init_net(struct net *net)
2120{
2121 struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id);
2122
2123 INIT_LIST_HEAD_RCU(&pernet->local_addr_list);
Paolo Abeni72bcbc42021-10-15 16:05:51 -07002124
2125 /* Cit. 2 subflows ought to be enough for anybody. */
2126 pernet->subflows_max = 2;
Paolo Abeni01cacb02020-03-27 14:48:51 -07002127 pernet->next_id = 1;
Paolo Abeniff5a0b42021-08-13 15:15:45 -07002128 pernet->stale_loss_cnt = 4;
Paolo Abeni01cacb02020-03-27 14:48:51 -07002129 spin_lock_init(&pernet->lock);
Jianguo Wuae514982021-05-27 16:54:27 -07002130
2131 /* No need to initialize other pernet fields, the struct is zeroed at
2132 * allocation time.
2133 */
2134
Paolo Abeni01cacb02020-03-27 14:48:51 -07002135 return 0;
2136}
2137
2138static void __net_exit pm_nl_exit_net(struct list_head *net_list)
2139{
2140 struct net *net;
2141
2142 list_for_each_entry(net, net_list, exit_list) {
Geliang Tang141694d2020-12-10 14:24:59 -08002143 struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id);
2144
Paolo Abeni01cacb02020-03-27 14:48:51 -07002145 /* net is removed from namespace list, can't race with
Paolo Abenia0eea5f2021-08-18 16:42:36 -07002146 * other modifiers, also netns core already waited for a
2147 * RCU grace period.
Paolo Abeni01cacb02020-03-27 14:48:51 -07002148 */
Geliang Tang0e4a3e62021-03-12 17:16:18 -08002149 __flush_addrs(&pernet->local_addr_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07002150 }
2151}
2152
2153static struct pernet_operations mptcp_pm_pernet_ops = {
2154 .init = pm_nl_init_net,
2155 .exit_batch = pm_nl_exit_net,
2156 .id = &pm_nl_pernet_id,
2157 .size = sizeof(struct pm_nl_pernet),
2158};
2159
Paolo Abenid39dcec2020-06-26 19:29:59 +02002160void __init mptcp_pm_nl_init(void)
Paolo Abeni01cacb02020-03-27 14:48:51 -07002161{
2162 if (register_pernet_subsys(&mptcp_pm_pernet_ops) < 0)
2163 panic("Failed to register MPTCP PM pernet subsystem.\n");
2164
2165 if (genl_register_family(&mptcp_genl_family))
2166 panic("Failed to register MPTCP PM netlink family\n");
2167}