blob: 75af1f701e1d1fe93be901a569dd820d1ad01a9a [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
481static int
482lookup_id_by_addr(struct pm_nl_pernet *pernet, const struct mptcp_addr_info *addr)
483{
484 struct mptcp_pm_addr_entry *entry;
485 int ret = -1;
486
487 rcu_read_lock();
488 list_for_each_entry(entry, &pernet->local_addr_list, list) {
489 if (addresses_equal(&entry->addr, addr, entry->addr.port)) {
490 ret = entry->addr.id;
491 break;
492 }
493 }
494 rcu_read_unlock();
495 return ret;
496}
497
Paolo Abeni01cacb02020-03-27 14:48:51 -0700498static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk)
499{
500 struct sock *sk = (struct sock *)msk;
501 struct mptcp_pm_addr_entry *local;
Geliang Tanga914e582021-02-01 15:09:07 -0800502 unsigned int add_addr_signal_max;
503 unsigned int local_addr_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700504 struct pm_nl_pernet *pernet;
Geliang Tanga914e582021-02-01 15:09:07 -0800505 unsigned int subflows_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700506
Geliang Tang432d9e72020-12-09 15:51:28 -0800507 pernet = net_generic(sock_net(sk), pm_nl_pernet_id);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700508
Geliang Tanga914e582021-02-01 15:09:07 -0800509 add_addr_signal_max = mptcp_pm_get_add_addr_signal_max(msk);
510 local_addr_max = mptcp_pm_get_local_addr_max(msk);
511 subflows_max = mptcp_pm_get_subflows_max(msk);
512
Paolo Abeni86e39e02022-01-06 16:20:22 -0800513 /* do lazy endpoint usage accounting for the MPC subflows */
514 if (unlikely(!(msk->pm.status & BIT(MPTCP_PM_MPC_ENDPOINT_ACCOUNTED))) && msk->first) {
Paolo Abenia88c9e42022-01-06 16:20:23 -0800515 struct mptcp_addr_info mpc_addr;
Paolo Abeni86e39e02022-01-06 16:20:22 -0800516 int mpc_id;
517
Paolo Abenia88c9e42022-01-06 16:20:23 -0800518 local_address((struct sock_common *)msk->first, &mpc_addr);
519 mpc_id = lookup_id_by_addr(pernet, &mpc_addr);
520 if (mpc_id >= 0)
Paolo Abeni86e39e02022-01-06 16:20:22 -0800521 __clear_bit(mpc_id, msk->pm.id_avail_bitmap);
522
523 msk->pm.status |= BIT(MPTCP_PM_MPC_ENDPOINT_ACCOUNTED);
524 }
525
Paolo Abeni01cacb02020-03-27 14:48:51 -0700526 pr_debug("local %d:%d signal %d:%d subflows %d:%d\n",
Geliang Tanga914e582021-02-01 15:09:07 -0800527 msk->pm.local_addr_used, local_addr_max,
528 msk->pm.add_addr_signaled, add_addr_signal_max,
529 msk->pm.subflows, subflows_max);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700530
531 /* check first for announce */
Geliang Tanga914e582021-02-01 15:09:07 -0800532 if (msk->pm.add_addr_signaled < add_addr_signal_max) {
Paolo Abeni86e39e02022-01-06 16:20:22 -0800533 local = select_signal_address(pernet, msk);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700534
535 if (local) {
Geliang Tangb6c08382020-09-24 08:29:54 +0800536 if (mptcp_pm_alloc_anno_list(msk, local)) {
Paolo Abeni86e39e02022-01-06 16:20:22 -0800537 __clear_bit(local->addr.id, msk->pm.id_avail_bitmap);
Geliang Tangb6c08382020-09-24 08:29:54 +0800538 msk->pm.add_addr_signaled++;
Geliang Tangf7efc772021-03-26 11:26:31 -0700539 mptcp_pm_announce_addr(msk, &local->addr, false);
Geliang Tangb46a0232021-03-26 11:26:41 -0700540 mptcp_pm_nl_addr_send_ack(msk);
Geliang Tangb6c08382020-09-24 08:29:54 +0800541 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700542 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700543 }
544
545 /* check if should create a new subflow */
Paolo Abenia88c9e42022-01-06 16:20:23 -0800546 while (msk->pm.local_addr_used < local_addr_max &&
547 msk->pm.subflows < subflows_max) {
548 struct mptcp_addr_info addrs[MPTCP_PM_ADDR_MAX];
549 bool fullmesh;
550 int i, nr;
Matthieu Baerts1f2f1932021-01-25 10:59:02 -0800551
Paolo Abenia88c9e42022-01-06 16:20:23 -0800552 local = select_local_address(pernet, msk);
553 if (!local)
554 break;
555
556 fullmesh = !!(local->flags & MPTCP_PM_ADDR_FLAG_FULLMESH);
557
558 msk->pm.local_addr_used++;
559 nr = fill_remote_addresses_vec(msk, fullmesh, addrs);
560 if (nr)
561 __clear_bit(local->addr.id, msk->pm.id_avail_bitmap);
562 spin_unlock_bh(&msk->pm.lock);
563 for (i = 0; i < nr; i++)
564 __mptcp_subflow_connect(sk, &local->addr, &addrs[i]);
565 spin_lock_bh(&msk->pm.lock);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700566 }
Paolo Abenia88c9e42022-01-06 16:20:23 -0800567 mptcp_pm_nl_check_work_pending(msk);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700568}
569
Florian Westphale9801432021-02-12 15:59:54 -0800570static void mptcp_pm_nl_fully_established(struct mptcp_sock *msk)
Paolo Abeni01cacb02020-03-27 14:48:51 -0700571{
572 mptcp_pm_create_subflow_or_signal_addr(msk);
573}
574
Florian Westphale9801432021-02-12 15:59:54 -0800575static void mptcp_pm_nl_subflow_established(struct mptcp_sock *msk)
Paolo Abeni01cacb02020-03-27 14:48:51 -0700576{
577 mptcp_pm_create_subflow_or_signal_addr(msk);
578}
579
Geliang Tang1a0d6132021-08-17 15:07:24 -0700580/* Fill all the local addresses into the array addrs[],
581 * and return the array size.
582 */
583static unsigned int fill_local_addresses_vec(struct mptcp_sock *msk,
584 struct mptcp_addr_info *addrs)
585{
586 struct sock *sk = (struct sock *)msk;
587 struct mptcp_pm_addr_entry *entry;
588 struct mptcp_addr_info local;
589 struct pm_nl_pernet *pernet;
590 unsigned int subflows_max;
591 int i = 0;
592
593 pernet = net_generic(sock_net(sk), pm_nl_pernet_id);
594 subflows_max = mptcp_pm_get_subflows_max(msk);
595
596 rcu_read_lock();
Geliang Tang1a0d6132021-08-17 15:07:24 -0700597 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
598 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_FULLMESH))
599 continue;
600
601 if (entry->addr.family != sk->sk_family) {
602#if IS_ENABLED(CONFIG_MPTCP_IPV6)
603 if ((entry->addr.family == AF_INET &&
604 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) ||
605 (sk->sk_family == AF_INET &&
606 !ipv6_addr_v4mapped(&entry->addr.addr6)))
607#endif
608 continue;
609 }
610
611 if (msk->pm.subflows < subflows_max) {
612 msk->pm.subflows++;
613 addrs[i++] = entry->addr;
614 }
615 }
616 rcu_read_unlock();
617
618 /* If the array is empty, fill in the single
619 * 'IPADDRANY' local address
620 */
621 if (!i) {
622 memset(&local, 0, sizeof(local));
623 local.family = msk->pm.remote.family;
624
625 msk->pm.subflows++;
626 addrs[i++] = local;
627 }
628
629 return i;
630}
631
Florian Westphale9801432021-02-12 15:59:54 -0800632static void mptcp_pm_nl_add_addr_received(struct mptcp_sock *msk)
Paolo Abeni01cacb02020-03-27 14:48:51 -0700633{
Geliang Tang1a0d6132021-08-17 15:07:24 -0700634 struct mptcp_addr_info addrs[MPTCP_PM_ADDR_MAX];
Paolo Abeni01cacb02020-03-27 14:48:51 -0700635 struct sock *sk = (struct sock *)msk;
Geliang Tanga914e582021-02-01 15:09:07 -0800636 unsigned int add_addr_accept_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700637 struct mptcp_addr_info remote;
Geliang Tanga914e582021-02-01 15:09:07 -0800638 unsigned int subflows_max;
Geliang Tang1a0d6132021-08-17 15:07:24 -0700639 int i, nr;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700640
Geliang Tanga914e582021-02-01 15:09:07 -0800641 add_addr_accept_max = mptcp_pm_get_add_addr_accept_max(msk);
642 subflows_max = mptcp_pm_get_subflows_max(msk);
643
Paolo Abeni01cacb02020-03-27 14:48:51 -0700644 pr_debug("accepted %d:%d remote family %d",
Geliang Tanga914e582021-02-01 15:09:07 -0800645 msk->pm.add_addr_accepted, add_addr_accept_max,
Paolo Abeni01cacb02020-03-27 14:48:51 -0700646 msk->pm.remote.family);
Geliang Tangd84ad042021-03-26 11:26:32 -0700647
648 if (lookup_subflow_by_daddr(&msk->conn_list, &msk->pm.remote))
649 goto add_addr_echo;
650
Paolo Abeni01cacb02020-03-27 14:48:51 -0700651 /* connect to the specified remote address, using whatever
652 * local address the routing configuration will pick.
653 */
654 remote = msk->pm.remote;
655 if (!remote.port)
656 remote.port = sk->sk_dport;
Geliang Tang1a0d6132021-08-17 15:07:24 -0700657 nr = fill_local_addresses_vec(msk, addrs);
658
659 msk->pm.add_addr_accepted++;
660 if (msk->pm.add_addr_accepted >= add_addr_accept_max ||
661 msk->pm.subflows >= subflows_max)
662 WRITE_ONCE(msk->pm.accept_addr, false);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700663
664 spin_unlock_bh(&msk->pm.lock);
Geliang Tang1a0d6132021-08-17 15:07:24 -0700665 for (i = 0; i < nr; i++)
666 __mptcp_subflow_connect(sk, &addrs[i], &remote);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700667 spin_lock_bh(&msk->pm.lock);
Geliang Tang6a6c05a2020-09-24 08:29:50 +0800668
Geliang Tangd84ad042021-03-26 11:26:32 -0700669add_addr_echo:
Geliang Tangf7efc772021-03-26 11:26:31 -0700670 mptcp_pm_announce_addr(msk, &msk->pm.remote, true);
Geliang Tangb46a0232021-03-26 11:26:41 -0700671 mptcp_pm_nl_addr_send_ack(msk);
Geliang Tang84dfe362020-11-19 11:46:00 -0800672}
673
Geliang Tangb46a0232021-03-26 11:26:41 -0700674void mptcp_pm_nl_addr_send_ack(struct mptcp_sock *msk)
Geliang Tang84dfe362020-11-19 11:46:00 -0800675{
676 struct mptcp_subflow_context *subflow;
677
Florian Westphal3abc05d2021-02-04 15:23:30 -0800678 msk_owned_by_me(msk);
679 lockdep_assert_held(&msk->pm.lock);
680
Geliang Tang8dd5efb2021-03-26 11:26:40 -0700681 if (!mptcp_pm_should_add_signal(msk) &&
682 !mptcp_pm_should_rm_signal(msk))
Geliang Tang84dfe362020-11-19 11:46:00 -0800683 return;
684
Geliang Tang84dfe362020-11-19 11:46:00 -0800685 subflow = list_first_entry_or_null(&msk->conn_list, typeof(*subflow), node);
686 if (subflow) {
687 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
Geliang Tang84dfe362020-11-19 11:46:00 -0800688
689 spin_unlock_bh(&msk->pm.lock);
Yonglong Lic233ef12021-08-23 18:05:43 -0700690 pr_debug("send ack for %s",
691 mptcp_pm_should_add_signal(msk) ? "add_addr" : "rm_addr");
Geliang Tangfbe0f872020-12-09 15:51:23 -0800692
Mat Martineau340fa662021-09-02 11:51:19 -0700693 mptcp_subflow_send_ack(ssk);
Geliang Tang84dfe362020-11-19 11:46:00 -0800694 spin_lock_bh(&msk->pm.lock);
Geliang Tang84dfe362020-11-19 11:46:00 -0800695 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700696}
697
Mat Martineau3828c512021-10-15 16:05:52 -0700698static int mptcp_pm_nl_mp_prio_send_ack(struct mptcp_sock *msk,
699 struct mptcp_addr_info *addr,
700 u8 bkup)
Geliang Tang06706542021-01-08 16:47:57 -0800701{
702 struct mptcp_subflow_context *subflow;
703
704 pr_debug("bkup=%d", bkup);
705
706 mptcp_for_each_subflow(msk, subflow) {
707 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
Geliang Tang0be2ac22021-01-08 16:48:01 -0800708 struct sock *sk = (struct sock *)msk;
Geliang Tang06706542021-01-08 16:47:57 -0800709 struct mptcp_addr_info local;
710
711 local_address((struct sock_common *)ssk, &local);
712 if (!addresses_equal(&local, addr, addr->port))
713 continue;
714
715 subflow->backup = bkup;
716 subflow->send_mp_prio = 1;
717 subflow->request_bkup = bkup;
Geliang Tang0be2ac22021-01-08 16:48:01 -0800718 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIOTX);
Geliang Tang06706542021-01-08 16:47:57 -0800719
720 spin_unlock_bh(&msk->pm.lock);
721 pr_debug("send ack for mp_prio");
Mat Martineau340fa662021-09-02 11:51:19 -0700722 mptcp_subflow_send_ack(ssk);
Geliang Tang06706542021-01-08 16:47:57 -0800723 spin_lock_bh(&msk->pm.lock);
724
725 return 0;
726 }
727
728 return -EINVAL;
729}
730
Geliang Tang9f12e972021-03-30 17:08:52 -0700731static void mptcp_pm_nl_rm_addr_or_subflow(struct mptcp_sock *msk,
732 const struct mptcp_rm_list *rm_list,
733 enum linux_mptcp_mib_field rm_type)
Geliang Tangd0876b22020-09-24 08:29:49 +0800734{
735 struct mptcp_subflow_context *subflow, *tmp;
736 struct sock *sk = (struct sock *)msk;
Geliang Tangd0b698c2021-03-12 17:16:15 -0800737 u8 i;
Geliang Tangd0876b22020-09-24 08:29:49 +0800738
Geliang Tang9f12e972021-03-30 17:08:52 -0700739 pr_debug("%s rm_list_nr %d",
740 rm_type == MPTCP_MIB_RMADDR ? "address" : "subflow", rm_list->nr);
Geliang Tangd0876b22020-09-24 08:29:49 +0800741
Florian Westphal3abc05d2021-02-04 15:23:30 -0800742 msk_owned_by_me(msk);
743
Paolo Abenib0cdc5d2021-12-11 17:11:12 +0100744 if (sk->sk_state == TCP_LISTEN)
745 return;
746
Geliang Tang9f12e972021-03-30 17:08:52 -0700747 if (!rm_list->nr)
Geliang Tangd0876b22020-09-24 08:29:49 +0800748 return;
749
750 if (list_empty(&msk->conn_list))
751 return;
752
Geliang Tang9f12e972021-03-30 17:08:52 -0700753 for (i = 0; i < rm_list->nr; i++) {
Paolo Abenif7d6a232022-01-06 16:20:20 -0800754 bool removed = false;
755
Geliang Tangd0b698c2021-03-12 17:16:15 -0800756 list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
757 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
758 int how = RCV_SHUTDOWN | SEND_SHUTDOWN;
Geliang Tang9f12e972021-03-30 17:08:52 -0700759 u8 id = subflow->local_id;
Geliang Tangd0876b22020-09-24 08:29:49 +0800760
Geliang Tang9f12e972021-03-30 17:08:52 -0700761 if (rm_type == MPTCP_MIB_RMADDR)
762 id = subflow->remote_id;
763
764 if (rm_list->ids[i] != id)
Geliang Tangd0b698c2021-03-12 17:16:15 -0800765 continue;
Geliang Tangd0876b22020-09-24 08:29:49 +0800766
Geliang Tang9f12e972021-03-30 17:08:52 -0700767 pr_debug(" -> %s rm_list_ids[%d]=%u local_id=%u remote_id=%u",
768 rm_type == MPTCP_MIB_RMADDR ? "address" : "subflow",
769 i, rm_list->ids[i], subflow->local_id, subflow->remote_id);
Geliang Tangd0b698c2021-03-12 17:16:15 -0800770 spin_unlock_bh(&msk->pm.lock);
771 mptcp_subflow_shutdown(sk, ssk, how);
Paolo Abenia88c9e42022-01-06 16:20:23 -0800772
773 /* the following takes care of updating the subflows counter */
Geliang Tangd0b698c2021-03-12 17:16:15 -0800774 mptcp_close_ssk(sk, ssk, subflow);
775 spin_lock_bh(&msk->pm.lock);
Geliang Tangd0876b22020-09-24 08:29:49 +0800776
Paolo Abenif7d6a232022-01-06 16:20:20 -0800777 removed = true;
Geliang Tang9f12e972021-03-30 17:08:52 -0700778 __MPTCP_INC_STATS(sock_net(sk), rm_type);
Geliang Tangd0b698c2021-03-12 17:16:15 -0800779 }
Paolo Abeni86e39e02022-01-06 16:20:22 -0800780 __set_bit(rm_list->ids[1], msk->pm.id_avail_bitmap);
Paolo Abenif7d6a232022-01-06 16:20:20 -0800781 if (!removed)
782 continue;
783
784 if (rm_type == MPTCP_MIB_RMADDR) {
785 msk->pm.add_addr_accepted--;
786 WRITE_ONCE(msk->pm.accept_addr, true);
787 } else if (rm_type == MPTCP_MIB_RMSUBFLOW) {
788 msk->pm.local_addr_used--;
789 }
Geliang Tangd0876b22020-09-24 08:29:49 +0800790 }
791}
792
Geliang Tang9f12e972021-03-30 17:08:52 -0700793static void mptcp_pm_nl_rm_addr_received(struct mptcp_sock *msk)
794{
795 mptcp_pm_nl_rm_addr_or_subflow(msk, &msk->pm.rm_list_rx, MPTCP_MIB_RMADDR);
796}
797
798void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk,
799 const struct mptcp_rm_list *rm_list)
800{
801 mptcp_pm_nl_rm_addr_or_subflow(msk, rm_list, MPTCP_MIB_RMSUBFLOW);
802}
803
Florian Westphale9801432021-02-12 15:59:54 -0800804void mptcp_pm_nl_work(struct mptcp_sock *msk)
805{
806 struct mptcp_pm_data *pm = &msk->pm;
807
808 msk_owned_by_me(msk);
809
Paolo Abeni86e39e02022-01-06 16:20:22 -0800810 if (!(pm->status & MPTCP_PM_WORK_MASK))
811 return;
812
Florian Westphale9801432021-02-12 15:59:54 -0800813 spin_lock_bh(&msk->pm.lock);
814
815 pr_debug("msk=%p status=%x", msk, pm->status);
816 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_RECEIVED)) {
817 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_RECEIVED);
818 mptcp_pm_nl_add_addr_received(msk);
819 }
820 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_SEND_ACK)) {
821 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_SEND_ACK);
Geliang Tangb46a0232021-03-26 11:26:41 -0700822 mptcp_pm_nl_addr_send_ack(msk);
Florian Westphale9801432021-02-12 15:59:54 -0800823 }
824 if (pm->status & BIT(MPTCP_PM_RM_ADDR_RECEIVED)) {
825 pm->status &= ~BIT(MPTCP_PM_RM_ADDR_RECEIVED);
826 mptcp_pm_nl_rm_addr_received(msk);
827 }
828 if (pm->status & BIT(MPTCP_PM_ESTABLISHED)) {
829 pm->status &= ~BIT(MPTCP_PM_ESTABLISHED);
830 mptcp_pm_nl_fully_established(msk);
831 }
832 if (pm->status & BIT(MPTCP_PM_SUBFLOW_ESTABLISHED)) {
833 pm->status &= ~BIT(MPTCP_PM_SUBFLOW_ESTABLISHED);
834 mptcp_pm_nl_subflow_established(msk);
835 }
836
837 spin_unlock_bh(&msk->pm.lock);
838}
839
Paolo Abeni01cacb02020-03-27 14:48:51 -0700840static bool address_use_port(struct mptcp_pm_addr_entry *entry)
841{
Geliang Tangdaa83ab2021-04-06 17:15:57 -0700842 return (entry->flags &
Paolo Abeni01cacb02020-03-27 14:48:51 -0700843 (MPTCP_PM_ADDR_FLAG_SIGNAL | MPTCP_PM_ADDR_FLAG_SUBFLOW)) ==
844 MPTCP_PM_ADDR_FLAG_SIGNAL;
845}
846
847static int mptcp_pm_nl_append_new_local_addr(struct pm_nl_pernet *pernet,
848 struct mptcp_pm_addr_entry *entry)
849{
850 struct mptcp_pm_addr_entry *cur;
Geliang Tang72603d22021-02-01 15:09:06 -0800851 unsigned int addr_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700852 int ret = -EINVAL;
853
854 spin_lock_bh(&pernet->lock);
855 /* to keep the code simple, don't do IDR-like allocation for address ID,
856 * just bail when we exceed limits
857 */
Paolo Abeni86e39e02022-01-06 16:20:22 -0800858 if (pernet->next_id == MPTCP_PM_MAX_ADDR_ID)
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800859 pernet->next_id = 1;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700860 if (pernet->addrs >= MPTCP_PM_ADDR_MAX)
861 goto out;
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800862 if (test_bit(entry->addr.id, pernet->id_bitmap))
863 goto out;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700864
865 /* do not insert duplicate address, differentiate on port only
866 * singled addresses
867 */
868 list_for_each_entry(cur, &pernet->local_addr_list, list) {
869 if (addresses_equal(&cur->addr, &entry->addr,
870 address_use_port(entry) &&
871 address_use_port(cur)))
872 goto out;
873 }
874
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800875 if (!entry->addr.id) {
876find_next:
877 entry->addr.id = find_next_zero_bit(pernet->id_bitmap,
Paolo Abeni86e39e02022-01-06 16:20:22 -0800878 MPTCP_PM_MAX_ADDR_ID + 1,
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800879 pernet->next_id);
Jean Sacren59060a42021-12-17 15:37:02 -0800880 if (!entry->addr.id && pernet->next_id != 1) {
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800881 pernet->next_id = 1;
882 goto find_next;
883 }
884 }
885
Jean Sacren59060a42021-12-17 15:37:02 -0800886 if (!entry->addr.id)
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800887 goto out;
888
889 __set_bit(entry->addr.id, pernet->id_bitmap);
890 if (entry->addr.id > pernet->next_id)
891 pernet->next_id = entry->addr.id;
892
Geliang Tangdaa83ab2021-04-06 17:15:57 -0700893 if (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
Geliang Tang72603d22021-02-01 15:09:06 -0800894 addr_max = pernet->add_addr_signal_max;
895 WRITE_ONCE(pernet->add_addr_signal_max, addr_max + 1);
896 }
Geliang Tangdaa83ab2021-04-06 17:15:57 -0700897 if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) {
Geliang Tang72603d22021-02-01 15:09:06 -0800898 addr_max = pernet->local_addr_max;
899 WRITE_ONCE(pernet->local_addr_max, addr_max + 1);
900 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700901
Paolo Abeni01cacb02020-03-27 14:48:51 -0700902 pernet->addrs++;
903 list_add_tail_rcu(&entry->list, &pernet->local_addr_list);
904 ret = entry->addr.id;
905
906out:
907 spin_unlock_bh(&pernet->lock);
908 return ret;
909}
910
Geliang Tang1729cf12021-02-01 15:09:12 -0800911static int mptcp_pm_nl_create_listen_socket(struct sock *sk,
912 struct mptcp_pm_addr_entry *entry)
913{
914 struct sockaddr_storage addr;
915 struct mptcp_sock *msk;
916 struct socket *ssock;
917 int backlog = 1024;
918 int err;
919
920 err = sock_create_kern(sock_net(sk), entry->addr.family,
921 SOCK_STREAM, IPPROTO_MPTCP, &entry->lsk);
922 if (err)
923 return err;
924
925 msk = mptcp_sk(entry->lsk->sk);
926 if (!msk) {
927 err = -EINVAL;
928 goto out;
929 }
930
931 ssock = __mptcp_nmpc_socket(msk);
932 if (!ssock) {
933 err = -EINVAL;
934 goto out;
935 }
936
937 mptcp_info2sockaddr(&entry->addr, &addr, entry->addr.family);
938 err = kernel_bind(ssock, (struct sockaddr *)&addr,
939 sizeof(struct sockaddr_in));
940 if (err) {
941 pr_warn("kernel_bind error, err=%d", err);
942 goto out;
943 }
944
945 err = kernel_listen(ssock, backlog);
946 if (err) {
947 pr_warn("kernel_listen error, err=%d", err);
948 goto out;
949 }
950
951 return 0;
952
953out:
954 sock_release(entry->lsk);
955 return err;
956}
957
Paolo Abeni01cacb02020-03-27 14:48:51 -0700958int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc)
959{
960 struct mptcp_pm_addr_entry *entry;
961 struct mptcp_addr_info skc_local;
962 struct mptcp_addr_info msk_local;
963 struct pm_nl_pernet *pernet;
964 int ret = -1;
965
966 if (WARN_ON_ONCE(!msk))
967 return -1;
968
969 /* The 0 ID mapping is defined by the first subflow, copied into the msk
970 * addr
971 */
972 local_address((struct sock_common *)msk, &msk_local);
Geliang Tang57025812020-09-08 10:49:38 +0800973 local_address((struct sock_common *)skc, &skc_local);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700974 if (addresses_equal(&msk_local, &skc_local, false))
975 return 0;
976
Geliang Tang57025812020-09-08 10:49:38 +0800977 if (address_zero(&skc_local))
978 return 0;
979
Paolo Abeni01cacb02020-03-27 14:48:51 -0700980 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
981
982 rcu_read_lock();
983 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
Geliang Tang60b57bf2021-02-01 15:09:16 -0800984 if (addresses_equal(&entry->addr, &skc_local, entry->addr.port)) {
Paolo Abeni01cacb02020-03-27 14:48:51 -0700985 ret = entry->addr.id;
986 break;
987 }
988 }
989 rcu_read_unlock();
990 if (ret >= 0)
991 return ret;
992
993 /* address not found, add to local list */
Geliang Tangf612eb72020-09-09 11:01:24 +0800994 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700995 if (!entry)
996 return -ENOMEM;
997
Paolo Abeni01cacb02020-03-27 14:48:51 -0700998 entry->addr = skc_local;
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800999 entry->addr.id = 0;
Geliang Tang1729cf12021-02-01 15:09:12 -08001000 entry->addr.port = 0;
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001001 entry->ifindex = 0;
1002 entry->flags = 0;
Geliang Tang1729cf12021-02-01 15:09:12 -08001003 entry->lsk = NULL;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001004 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
1005 if (ret < 0)
1006 kfree(entry);
1007
1008 return ret;
1009}
1010
1011void mptcp_pm_nl_data_init(struct mptcp_sock *msk)
1012{
1013 struct mptcp_pm_data *pm = &msk->pm;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001014 bool subflows;
1015
Geliang Tanga914e582021-02-01 15:09:07 -08001016 subflows = !!mptcp_pm_get_subflows_max(msk);
1017 WRITE_ONCE(pm->work_pending, (!!mptcp_pm_get_local_addr_max(msk) && subflows) ||
1018 !!mptcp_pm_get_add_addr_signal_max(msk));
1019 WRITE_ONCE(pm->accept_addr, !!mptcp_pm_get_add_addr_accept_max(msk) && subflows);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001020 WRITE_ONCE(pm->accept_subflow, subflows);
1021}
1022
Florian Westphalb911c972021-02-12 16:00:01 -08001023#define MPTCP_PM_CMD_GRP_OFFSET 0
1024#define MPTCP_PM_EV_GRP_OFFSET 1
Paolo Abeni01cacb02020-03-27 14:48:51 -07001025
1026static const struct genl_multicast_group mptcp_pm_mcgrps[] = {
1027 [MPTCP_PM_CMD_GRP_OFFSET] = { .name = MPTCP_PM_CMD_GRP_NAME, },
Florian Westphalb911c972021-02-12 16:00:01 -08001028 [MPTCP_PM_EV_GRP_OFFSET] = { .name = MPTCP_PM_EV_GRP_NAME,
1029 .flags = GENL_UNS_ADMIN_PERM,
1030 },
Paolo Abeni01cacb02020-03-27 14:48:51 -07001031};
1032
1033static const struct nla_policy
1034mptcp_pm_addr_policy[MPTCP_PM_ADDR_ATTR_MAX + 1] = {
1035 [MPTCP_PM_ADDR_ATTR_FAMILY] = { .type = NLA_U16, },
1036 [MPTCP_PM_ADDR_ATTR_ID] = { .type = NLA_U8, },
1037 [MPTCP_PM_ADDR_ATTR_ADDR4] = { .type = NLA_U32, },
Johannes Berg81408602020-08-18 10:17:31 +02001038 [MPTCP_PM_ADDR_ATTR_ADDR6] =
1039 NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
Paolo Abeni01cacb02020-03-27 14:48:51 -07001040 [MPTCP_PM_ADDR_ATTR_PORT] = { .type = NLA_U16 },
1041 [MPTCP_PM_ADDR_ATTR_FLAGS] = { .type = NLA_U32 },
1042 [MPTCP_PM_ADDR_ATTR_IF_IDX] = { .type = NLA_S32 },
1043};
1044
1045static const struct nla_policy mptcp_pm_policy[MPTCP_PM_ATTR_MAX + 1] = {
1046 [MPTCP_PM_ATTR_ADDR] =
1047 NLA_POLICY_NESTED(mptcp_pm_addr_policy),
1048 [MPTCP_PM_ATTR_RCV_ADD_ADDRS] = { .type = NLA_U32, },
1049 [MPTCP_PM_ATTR_SUBFLOWS] = { .type = NLA_U32, },
1050};
1051
Paolo Abeniff5a0b42021-08-13 15:15:45 -07001052void mptcp_pm_nl_subflow_chk_stale(const struct mptcp_sock *msk, struct sock *ssk)
1053{
1054 struct mptcp_subflow_context *iter, *subflow = mptcp_subflow_ctx(ssk);
1055 struct sock *sk = (struct sock *)msk;
1056 unsigned int active_max_loss_cnt;
1057 struct net *net = sock_net(sk);
1058 unsigned int stale_loss_cnt;
1059 bool slow;
1060
1061 stale_loss_cnt = mptcp_stale_loss_cnt(net);
1062 if (subflow->stale || !stale_loss_cnt || subflow->stale_count <= stale_loss_cnt)
1063 return;
1064
1065 /* look for another available subflow not in loss state */
1066 active_max_loss_cnt = max_t(int, stale_loss_cnt - 1, 1);
1067 mptcp_for_each_subflow(msk, iter) {
1068 if (iter != subflow && mptcp_subflow_active(iter) &&
1069 iter->stale_count < active_max_loss_cnt) {
1070 /* we have some alternatives, try to mark this subflow as idle ...*/
1071 slow = lock_sock_fast(ssk);
1072 if (!tcp_rtx_and_write_queues_empty(ssk)) {
1073 subflow->stale = 1;
1074 __mptcp_retransmit_pending_data(sk);
Paolo Abenifc1b4e32021-08-13 15:15:46 -07001075 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_SUBFLOWSTALE);
Paolo Abeniff5a0b42021-08-13 15:15:45 -07001076 }
1077 unlock_sock_fast(ssk, slow);
1078
1079 /* always try to push the pending data regarless of re-injections:
1080 * we can possibly use backup subflows now, and subflow selection
1081 * is cheap under the msk socket lock
1082 */
1083 __mptcp_push_pending(sk, 0);
1084 return;
1085 }
1086 }
1087}
1088
Paolo Abeni01cacb02020-03-27 14:48:51 -07001089static int mptcp_pm_family_to_addr(int family)
1090{
1091#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1092 if (family == AF_INET6)
1093 return MPTCP_PM_ADDR_ATTR_ADDR6;
1094#endif
1095 return MPTCP_PM_ADDR_ATTR_ADDR4;
1096}
1097
1098static int mptcp_pm_parse_addr(struct nlattr *attr, struct genl_info *info,
1099 bool require_family,
1100 struct mptcp_pm_addr_entry *entry)
1101{
1102 struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1];
1103 int err, addr_addr;
1104
1105 if (!attr) {
1106 GENL_SET_ERR_MSG(info, "missing address info");
1107 return -EINVAL;
1108 }
1109
1110 /* no validation needed - was already done via nested policy */
1111 err = nla_parse_nested_deprecated(tb, MPTCP_PM_ADDR_ATTR_MAX, attr,
1112 mptcp_pm_addr_policy, info->extack);
1113 if (err)
1114 return err;
1115
1116 memset(entry, 0, sizeof(*entry));
1117 if (!tb[MPTCP_PM_ADDR_ATTR_FAMILY]) {
1118 if (!require_family)
1119 goto skip_family;
1120
1121 NL_SET_ERR_MSG_ATTR(info->extack, attr,
1122 "missing family");
1123 return -EINVAL;
1124 }
1125
1126 entry->addr.family = nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_FAMILY]);
1127 if (entry->addr.family != AF_INET
1128#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1129 && entry->addr.family != AF_INET6
1130#endif
1131 ) {
1132 NL_SET_ERR_MSG_ATTR(info->extack, attr,
1133 "unknown address family");
1134 return -EINVAL;
1135 }
1136 addr_addr = mptcp_pm_family_to_addr(entry->addr.family);
1137 if (!tb[addr_addr]) {
1138 NL_SET_ERR_MSG_ATTR(info->extack, attr,
1139 "missing address data");
1140 return -EINVAL;
1141 }
1142
1143#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1144 if (entry->addr.family == AF_INET6)
1145 entry->addr.addr6 = nla_get_in6_addr(tb[addr_addr]);
1146 else
1147#endif
1148 entry->addr.addr.s_addr = nla_get_in_addr(tb[addr_addr]);
1149
1150skip_family:
Paolo Abenief0da3b2020-09-14 10:01:15 +02001151 if (tb[MPTCP_PM_ADDR_ATTR_IF_IDX]) {
1152 u32 val = nla_get_s32(tb[MPTCP_PM_ADDR_ATTR_IF_IDX]);
1153
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001154 entry->ifindex = val;
Paolo Abenief0da3b2020-09-14 10:01:15 +02001155 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001156
1157 if (tb[MPTCP_PM_ADDR_ATTR_ID])
1158 entry->addr.id = nla_get_u8(tb[MPTCP_PM_ADDR_ATTR_ID]);
1159
1160 if (tb[MPTCP_PM_ADDR_ATTR_FLAGS])
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001161 entry->flags = nla_get_u32(tb[MPTCP_PM_ADDR_ATTR_FLAGS]);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001162
Jianguo Wueb5fb622021-05-27 16:54:28 -07001163 if (tb[MPTCP_PM_ADDR_ATTR_PORT]) {
1164 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) {
1165 NL_SET_ERR_MSG_ATTR(info->extack, attr,
1166 "flags must have signal when using port");
1167 return -EINVAL;
1168 }
Geliang Tanga77e9172021-02-01 15:09:17 -08001169 entry->addr.port = htons(nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_PORT]));
Jianguo Wueb5fb622021-05-27 16:54:28 -07001170 }
Geliang Tanga77e9172021-02-01 15:09:17 -08001171
Paolo Abeni01cacb02020-03-27 14:48:51 -07001172 return 0;
1173}
1174
1175static struct pm_nl_pernet *genl_info_pm_nl(struct genl_info *info)
1176{
1177 return net_generic(genl_info_net(info), pm_nl_pernet_id);
1178}
1179
Geliang Tang875b76712021-02-01 15:09:08 -08001180static int mptcp_nl_add_subflow_or_signal_addr(struct net *net)
1181{
1182 struct mptcp_sock *msk;
1183 long s_slot = 0, s_num = 0;
1184
1185 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1186 struct sock *sk = (struct sock *)msk;
1187
1188 if (!READ_ONCE(msk->fully_established))
1189 goto next;
1190
1191 lock_sock(sk);
1192 spin_lock_bh(&msk->pm.lock);
1193 mptcp_pm_create_subflow_or_signal_addr(msk);
1194 spin_unlock_bh(&msk->pm.lock);
1195 release_sock(sk);
1196
1197next:
1198 sock_put(sk);
1199 cond_resched();
1200 }
1201
1202 return 0;
1203}
1204
Paolo Abeni01cacb02020-03-27 14:48:51 -07001205static int mptcp_nl_cmd_add_addr(struct sk_buff *skb, struct genl_info *info)
1206{
1207 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1208 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1209 struct mptcp_pm_addr_entry addr, *entry;
1210 int ret;
1211
1212 ret = mptcp_pm_parse_addr(attr, info, true, &addr);
1213 if (ret < 0)
1214 return ret;
1215
1216 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1217 if (!entry) {
1218 GENL_SET_ERR_MSG(info, "can't allocate addr");
1219 return -ENOMEM;
1220 }
1221
1222 *entry = addr;
Geliang Tang1729cf12021-02-01 15:09:12 -08001223 if (entry->addr.port) {
1224 ret = mptcp_pm_nl_create_listen_socket(skb->sk, entry);
1225 if (ret) {
1226 GENL_SET_ERR_MSG(info, "create listen socket error");
1227 kfree(entry);
1228 return ret;
1229 }
1230 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001231 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
1232 if (ret < 0) {
1233 GENL_SET_ERR_MSG(info, "too many addresses or duplicate one");
Geliang Tang1729cf12021-02-01 15:09:12 -08001234 if (entry->lsk)
1235 sock_release(entry->lsk);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001236 kfree(entry);
1237 return ret;
1238 }
1239
Geliang Tang875b76712021-02-01 15:09:08 -08001240 mptcp_nl_add_subflow_or_signal_addr(sock_net(skb->sk));
1241
Paolo Abeni01cacb02020-03-27 14:48:51 -07001242 return 0;
1243}
1244
Geliang Tangee285252021-08-17 15:07:22 -07001245int mptcp_pm_get_flags_and_ifindex_by_id(struct net *net, unsigned int id,
1246 u8 *flags, int *ifindex)
1247{
1248 struct mptcp_pm_addr_entry *entry;
1249
1250 *flags = 0;
1251 *ifindex = 0;
1252
1253 if (id) {
1254 rcu_read_lock();
1255 entry = __lookup_addr_by_id(net_generic(net, pm_nl_pernet_id), id);
1256 if (entry) {
1257 *flags = entry->flags;
1258 *ifindex = entry->ifindex;
1259 }
1260 rcu_read_unlock();
1261 }
1262
1263 return 0;
1264}
1265
Geliang Tangb6c08382020-09-24 08:29:54 +08001266static bool remove_anno_list_by_saddr(struct mptcp_sock *msk,
1267 struct mptcp_addr_info *addr)
1268{
Geliang Tang00cfd772020-09-24 08:30:02 +08001269 struct mptcp_pm_add_entry *entry;
Geliang Tangb6c08382020-09-24 08:29:54 +08001270
Davide Carattid58300c2021-05-25 14:23:13 -07001271 entry = mptcp_pm_del_add_timer(msk, addr, false);
Geliang Tang00cfd772020-09-24 08:30:02 +08001272 if (entry) {
1273 list_del(&entry->list);
1274 kfree(entry);
1275 return true;
Geliang Tangb6c08382020-09-24 08:29:54 +08001276 }
1277
1278 return false;
1279}
1280
1281static bool mptcp_pm_remove_anno_addr(struct mptcp_sock *msk,
1282 struct mptcp_addr_info *addr,
1283 bool force)
1284{
Geliang Tangcbde2782021-03-12 17:16:12 -08001285 struct mptcp_rm_list list = { .nr = 0 };
Geliang Tangb6c08382020-09-24 08:29:54 +08001286 bool ret;
1287
Geliang Tangcbde2782021-03-12 17:16:12 -08001288 list.ids[list.nr++] = addr->id;
1289
Geliang Tangb6c08382020-09-24 08:29:54 +08001290 ret = remove_anno_list_by_saddr(msk, addr);
Geliang Tang00cfd772020-09-24 08:30:02 +08001291 if (ret || force) {
1292 spin_lock_bh(&msk->pm.lock);
Geliang Tangcbde2782021-03-12 17:16:12 -08001293 mptcp_pm_remove_addr(msk, &list);
Geliang Tang00cfd772020-09-24 08:30:02 +08001294 spin_unlock_bh(&msk->pm.lock);
1295 }
Geliang Tangb6c08382020-09-24 08:29:54 +08001296 return ret;
1297}
1298
1299static int mptcp_nl_remove_subflow_and_signal_addr(struct net *net,
1300 struct mptcp_addr_info *addr)
1301{
1302 struct mptcp_sock *msk;
1303 long s_slot = 0, s_num = 0;
Geliang Tangddd14bb2021-03-12 17:16:16 -08001304 struct mptcp_rm_list list = { .nr = 0 };
Geliang Tangb6c08382020-09-24 08:29:54 +08001305
1306 pr_debug("remove_id=%d", addr->id);
1307
Geliang Tangddd14bb2021-03-12 17:16:16 -08001308 list.ids[list.nr++] = addr->id;
1309
Geliang Tangb6c08382020-09-24 08:29:54 +08001310 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1311 struct sock *sk = (struct sock *)msk;
1312 bool remove_subflow;
1313
1314 if (list_empty(&msk->conn_list)) {
1315 mptcp_pm_remove_anno_addr(msk, addr, false);
1316 goto next;
1317 }
1318
1319 lock_sock(sk);
1320 remove_subflow = lookup_subflow_by_saddr(&msk->conn_list, addr);
1321 mptcp_pm_remove_anno_addr(msk, addr, remove_subflow);
1322 if (remove_subflow)
Geliang Tangddd14bb2021-03-12 17:16:16 -08001323 mptcp_pm_remove_subflow(msk, &list);
Geliang Tangb6c08382020-09-24 08:29:54 +08001324 release_sock(sk);
1325
1326next:
1327 sock_put(sk);
1328 cond_resched();
1329 }
1330
1331 return 0;
1332}
1333
Paolo Abenia0eea5f2021-08-18 16:42:36 -07001334/* caller must ensure the RCU grace period is already elapsed */
1335static void __mptcp_pm_release_addr_entry(struct mptcp_pm_addr_entry *entry)
Geliang Tang1729cf12021-02-01 15:09:12 -08001336{
Paolo Abenia0eea5f2021-08-18 16:42:36 -07001337 if (entry->lsk)
1338 sock_release(entry->lsk);
1339 kfree(entry);
Geliang Tang1729cf12021-02-01 15:09:12 -08001340}
1341
Geliang Tang740d7982021-03-30 17:08:53 -07001342static int mptcp_nl_remove_id_zero_address(struct net *net,
1343 struct mptcp_addr_info *addr)
1344{
1345 struct mptcp_rm_list list = { .nr = 0 };
1346 long s_slot = 0, s_num = 0;
1347 struct mptcp_sock *msk;
1348
1349 list.ids[list.nr++] = 0;
1350
1351 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1352 struct sock *sk = (struct sock *)msk;
1353 struct mptcp_addr_info msk_local;
1354
1355 if (list_empty(&msk->conn_list))
1356 goto next;
1357
1358 local_address((struct sock_common *)msk, &msk_local);
1359 if (!addresses_equal(&msk_local, addr, addr->port))
1360 goto next;
1361
1362 lock_sock(sk);
1363 spin_lock_bh(&msk->pm.lock);
1364 mptcp_pm_remove_addr(msk, &list);
1365 mptcp_pm_nl_rm_subflow_received(msk, &list);
1366 spin_unlock_bh(&msk->pm.lock);
1367 release_sock(sk);
1368
1369next:
1370 sock_put(sk);
1371 cond_resched();
1372 }
1373
1374 return 0;
1375}
1376
Paolo Abeni01cacb02020-03-27 14:48:51 -07001377static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info)
1378{
1379 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1380 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1381 struct mptcp_pm_addr_entry addr, *entry;
Geliang Tang72603d22021-02-01 15:09:06 -08001382 unsigned int addr_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001383 int ret;
1384
1385 ret = mptcp_pm_parse_addr(attr, info, false, &addr);
1386 if (ret < 0)
1387 return ret;
1388
Geliang Tang740d7982021-03-30 17:08:53 -07001389 /* the zero id address is special: the first address used by the msk
1390 * always gets such an id, so different subflows can have different zero
1391 * id addresses. Additionally zero id is not accounted for in id_bitmap.
1392 * Let's use an 'mptcp_rm_list' instead of the common remove code.
1393 */
1394 if (addr.addr.id == 0)
1395 return mptcp_nl_remove_id_zero_address(sock_net(skb->sk), &addr.addr);
1396
Paolo Abeni01cacb02020-03-27 14:48:51 -07001397 spin_lock_bh(&pernet->lock);
1398 entry = __lookup_addr_by_id(pernet, addr.addr.id);
1399 if (!entry) {
1400 GENL_SET_ERR_MSG(info, "address not found");
Geliang Tangb6c08382020-09-24 08:29:54 +08001401 spin_unlock_bh(&pernet->lock);
1402 return -EINVAL;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001403 }
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001404 if (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
Geliang Tang72603d22021-02-01 15:09:06 -08001405 addr_max = pernet->add_addr_signal_max;
1406 WRITE_ONCE(pernet->add_addr_signal_max, addr_max - 1);
1407 }
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001408 if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) {
Geliang Tang72603d22021-02-01 15:09:06 -08001409 addr_max = pernet->local_addr_max;
1410 WRITE_ONCE(pernet->local_addr_max, addr_max - 1);
1411 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001412
1413 pernet->addrs--;
1414 list_del_rcu(&entry->list);
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001415 __clear_bit(entry->addr.id, pernet->id_bitmap);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001416 spin_unlock_bh(&pernet->lock);
Geliang Tangb6c08382020-09-24 08:29:54 +08001417
1418 mptcp_nl_remove_subflow_and_signal_addr(sock_net(skb->sk), &entry->addr);
Paolo Abenia0eea5f2021-08-18 16:42:36 -07001419 synchronize_rcu();
1420 __mptcp_pm_release_addr_entry(entry);
Geliang Tangb6c08382020-09-24 08:29:54 +08001421
Paolo Abeni01cacb02020-03-27 14:48:51 -07001422 return ret;
1423}
1424
Geliang Tang06faa222021-03-12 17:16:17 -08001425static void mptcp_pm_remove_addrs_and_subflows(struct mptcp_sock *msk,
1426 struct list_head *rm_list)
1427{
1428 struct mptcp_rm_list alist = { .nr = 0 }, slist = { .nr = 0 };
1429 struct mptcp_pm_addr_entry *entry;
1430
1431 list_for_each_entry(entry, rm_list, list) {
1432 if (lookup_subflow_by_saddr(&msk->conn_list, &entry->addr) &&
1433 alist.nr < MPTCP_RM_IDS_MAX &&
1434 slist.nr < MPTCP_RM_IDS_MAX) {
1435 alist.ids[alist.nr++] = entry->addr.id;
1436 slist.ids[slist.nr++] = entry->addr.id;
1437 } else if (remove_anno_list_by_saddr(msk, &entry->addr) &&
1438 alist.nr < MPTCP_RM_IDS_MAX) {
1439 alist.ids[alist.nr++] = entry->addr.id;
1440 }
1441 }
1442
1443 if (alist.nr) {
1444 spin_lock_bh(&msk->pm.lock);
1445 mptcp_pm_remove_addr(msk, &alist);
1446 spin_unlock_bh(&msk->pm.lock);
1447 }
1448 if (slist.nr)
1449 mptcp_pm_remove_subflow(msk, &slist);
1450}
1451
1452static void mptcp_nl_remove_addrs_list(struct net *net,
1453 struct list_head *rm_list)
1454{
1455 long s_slot = 0, s_num = 0;
1456 struct mptcp_sock *msk;
1457
1458 if (list_empty(rm_list))
1459 return;
1460
1461 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1462 struct sock *sk = (struct sock *)msk;
1463
1464 lock_sock(sk);
1465 mptcp_pm_remove_addrs_and_subflows(msk, rm_list);
1466 release_sock(sk);
1467
1468 sock_put(sk);
1469 cond_resched();
1470 }
1471}
1472
Paolo Abenia0eea5f2021-08-18 16:42:36 -07001473/* caller must ensure the RCU grace period is already elapsed */
Geliang Tang0e4a3e62021-03-12 17:16:18 -08001474static void __flush_addrs(struct list_head *list)
Paolo Abeni01cacb02020-03-27 14:48:51 -07001475{
Geliang Tang141694d2020-12-10 14:24:59 -08001476 while (!list_empty(list)) {
Paolo Abeni01cacb02020-03-27 14:48:51 -07001477 struct mptcp_pm_addr_entry *cur;
1478
Geliang Tang141694d2020-12-10 14:24:59 -08001479 cur = list_entry(list->next,
Paolo Abeni01cacb02020-03-27 14:48:51 -07001480 struct mptcp_pm_addr_entry, list);
1481 list_del_rcu(&cur->list);
Paolo Abenia0eea5f2021-08-18 16:42:36 -07001482 __mptcp_pm_release_addr_entry(cur);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001483 }
1484}
1485
1486static void __reset_counters(struct pm_nl_pernet *pernet)
1487{
Geliang Tang72603d22021-02-01 15:09:06 -08001488 WRITE_ONCE(pernet->add_addr_signal_max, 0);
1489 WRITE_ONCE(pernet->add_addr_accept_max, 0);
1490 WRITE_ONCE(pernet->local_addr_max, 0);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001491 pernet->addrs = 0;
1492}
1493
1494static int mptcp_nl_cmd_flush_addrs(struct sk_buff *skb, struct genl_info *info)
1495{
1496 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
Geliang Tang141694d2020-12-10 14:24:59 -08001497 LIST_HEAD(free_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001498
1499 spin_lock_bh(&pernet->lock);
Geliang Tang141694d2020-12-10 14:24:59 -08001500 list_splice_init(&pernet->local_addr_list, &free_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001501 __reset_counters(pernet);
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001502 pernet->next_id = 1;
Paolo Abeni86e39e02022-01-06 16:20:22 -08001503 bitmap_zero(pernet->id_bitmap, MPTCP_PM_MAX_ADDR_ID + 1);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001504 spin_unlock_bh(&pernet->lock);
Geliang Tang0e4a3e62021-03-12 17:16:18 -08001505 mptcp_nl_remove_addrs_list(sock_net(skb->sk), &free_list);
Paolo Abenia0eea5f2021-08-18 16:42:36 -07001506 synchronize_rcu();
Geliang Tang0e4a3e62021-03-12 17:16:18 -08001507 __flush_addrs(&free_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001508 return 0;
1509}
1510
1511static int mptcp_nl_fill_addr(struct sk_buff *skb,
1512 struct mptcp_pm_addr_entry *entry)
1513{
1514 struct mptcp_addr_info *addr = &entry->addr;
1515 struct nlattr *attr;
1516
1517 attr = nla_nest_start(skb, MPTCP_PM_ATTR_ADDR);
1518 if (!attr)
1519 return -EMSGSIZE;
1520
1521 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_FAMILY, addr->family))
1522 goto nla_put_failure;
Geliang Tanga77e9172021-02-01 15:09:17 -08001523 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_PORT, ntohs(addr->port)))
1524 goto nla_put_failure;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001525 if (nla_put_u8(skb, MPTCP_PM_ADDR_ATTR_ID, addr->id))
1526 goto nla_put_failure;
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001527 if (nla_put_u32(skb, MPTCP_PM_ADDR_ATTR_FLAGS, entry->flags))
Paolo Abeni01cacb02020-03-27 14:48:51 -07001528 goto nla_put_failure;
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001529 if (entry->ifindex &&
1530 nla_put_s32(skb, MPTCP_PM_ADDR_ATTR_IF_IDX, entry->ifindex))
Paolo Abeni01cacb02020-03-27 14:48:51 -07001531 goto nla_put_failure;
1532
Bo YUb4e0f9a2020-04-23 10:10:03 +08001533 if (addr->family == AF_INET &&
1534 nla_put_in_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR4,
1535 addr->addr.s_addr))
1536 goto nla_put_failure;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001537#if IS_ENABLED(CONFIG_MPTCP_IPV6)
Bo YUb4e0f9a2020-04-23 10:10:03 +08001538 else if (addr->family == AF_INET6 &&
1539 nla_put_in6_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR6, &addr->addr6))
1540 goto nla_put_failure;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001541#endif
1542 nla_nest_end(skb, attr);
1543 return 0;
1544
1545nla_put_failure:
1546 nla_nest_cancel(skb, attr);
1547 return -EMSGSIZE;
1548}
1549
1550static int mptcp_nl_cmd_get_addr(struct sk_buff *skb, struct genl_info *info)
1551{
1552 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1553 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1554 struct mptcp_pm_addr_entry addr, *entry;
1555 struct sk_buff *msg;
1556 void *reply;
1557 int ret;
1558
1559 ret = mptcp_pm_parse_addr(attr, info, false, &addr);
1560 if (ret < 0)
1561 return ret;
1562
1563 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1564 if (!msg)
1565 return -ENOMEM;
1566
1567 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1568 info->genlhdr->cmd);
1569 if (!reply) {
1570 GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1571 ret = -EMSGSIZE;
1572 goto fail;
1573 }
1574
1575 spin_lock_bh(&pernet->lock);
1576 entry = __lookup_addr_by_id(pernet, addr.addr.id);
1577 if (!entry) {
1578 GENL_SET_ERR_MSG(info, "address not found");
1579 ret = -EINVAL;
1580 goto unlock_fail;
1581 }
1582
1583 ret = mptcp_nl_fill_addr(msg, entry);
1584 if (ret)
1585 goto unlock_fail;
1586
1587 genlmsg_end(msg, reply);
1588 ret = genlmsg_reply(msg, info);
1589 spin_unlock_bh(&pernet->lock);
1590 return ret;
1591
1592unlock_fail:
1593 spin_unlock_bh(&pernet->lock);
1594
1595fail:
1596 nlmsg_free(msg);
1597 return ret;
1598}
1599
1600static int mptcp_nl_cmd_dump_addrs(struct sk_buff *msg,
1601 struct netlink_callback *cb)
1602{
1603 struct net *net = sock_net(msg->sk);
1604 struct mptcp_pm_addr_entry *entry;
1605 struct pm_nl_pernet *pernet;
1606 int id = cb->args[0];
1607 void *hdr;
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001608 int i;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001609
1610 pernet = net_generic(net, pm_nl_pernet_id);
1611
1612 spin_lock_bh(&pernet->lock);
Paolo Abeni86e39e02022-01-06 16:20:22 -08001613 for (i = id; i < MPTCP_PM_MAX_ADDR_ID + 1; i++) {
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001614 if (test_bit(i, pernet->id_bitmap)) {
1615 entry = __lookup_addr_by_id(pernet, i);
1616 if (!entry)
1617 break;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001618
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001619 if (entry->addr.id <= id)
1620 continue;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001621
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001622 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid,
1623 cb->nlh->nlmsg_seq, &mptcp_genl_family,
1624 NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR);
1625 if (!hdr)
1626 break;
1627
1628 if (mptcp_nl_fill_addr(msg, entry) < 0) {
1629 genlmsg_cancel(msg, hdr);
1630 break;
1631 }
1632
1633 id = entry->addr.id;
1634 genlmsg_end(msg, hdr);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001635 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001636 }
1637 spin_unlock_bh(&pernet->lock);
1638
1639 cb->args[0] = id;
1640 return msg->len;
1641}
1642
1643static int parse_limit(struct genl_info *info, int id, unsigned int *limit)
1644{
1645 struct nlattr *attr = info->attrs[id];
1646
1647 if (!attr)
1648 return 0;
1649
1650 *limit = nla_get_u32(attr);
1651 if (*limit > MPTCP_PM_ADDR_MAX) {
1652 GENL_SET_ERR_MSG(info, "limit greater than maximum");
1653 return -EINVAL;
1654 }
1655 return 0;
1656}
1657
1658static int
1659mptcp_nl_cmd_set_limits(struct sk_buff *skb, struct genl_info *info)
1660{
1661 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1662 unsigned int rcv_addrs, subflows;
1663 int ret;
1664
1665 spin_lock_bh(&pernet->lock);
1666 rcv_addrs = pernet->add_addr_accept_max;
1667 ret = parse_limit(info, MPTCP_PM_ATTR_RCV_ADD_ADDRS, &rcv_addrs);
1668 if (ret)
1669 goto unlock;
1670
1671 subflows = pernet->subflows_max;
1672 ret = parse_limit(info, MPTCP_PM_ATTR_SUBFLOWS, &subflows);
1673 if (ret)
1674 goto unlock;
1675
1676 WRITE_ONCE(pernet->add_addr_accept_max, rcv_addrs);
1677 WRITE_ONCE(pernet->subflows_max, subflows);
1678
1679unlock:
1680 spin_unlock_bh(&pernet->lock);
1681 return ret;
1682}
1683
1684static int
1685mptcp_nl_cmd_get_limits(struct sk_buff *skb, struct genl_info *info)
1686{
1687 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1688 struct sk_buff *msg;
1689 void *reply;
1690
1691 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1692 if (!msg)
1693 return -ENOMEM;
1694
1695 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1696 MPTCP_PM_CMD_GET_LIMITS);
1697 if (!reply)
1698 goto fail;
1699
1700 if (nla_put_u32(msg, MPTCP_PM_ATTR_RCV_ADD_ADDRS,
1701 READ_ONCE(pernet->add_addr_accept_max)))
1702 goto fail;
1703
1704 if (nla_put_u32(msg, MPTCP_PM_ATTR_SUBFLOWS,
1705 READ_ONCE(pernet->subflows_max)))
1706 goto fail;
1707
1708 genlmsg_end(msg, reply);
1709 return genlmsg_reply(msg, info);
1710
1711fail:
1712 GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1713 nlmsg_free(msg);
1714 return -EMSGSIZE;
1715}
1716
Geliang Tang0f9f6962021-01-08 16:47:59 -08001717static int mptcp_nl_addr_backup(struct net *net,
1718 struct mptcp_addr_info *addr,
1719 u8 bkup)
1720{
1721 long s_slot = 0, s_num = 0;
1722 struct mptcp_sock *msk;
1723 int ret = -EINVAL;
1724
1725 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1726 struct sock *sk = (struct sock *)msk;
1727
1728 if (list_empty(&msk->conn_list))
1729 goto next;
1730
1731 lock_sock(sk);
1732 spin_lock_bh(&msk->pm.lock);
1733 ret = mptcp_pm_nl_mp_prio_send_ack(msk, addr, bkup);
1734 spin_unlock_bh(&msk->pm.lock);
1735 release_sock(sk);
1736
1737next:
1738 sock_put(sk);
1739 cond_resched();
1740 }
1741
1742 return ret;
1743}
1744
1745static int mptcp_nl_cmd_set_flags(struct sk_buff *skb, struct genl_info *info)
1746{
Davide Caratti602837e2021-12-03 14:35:36 -08001747 struct mptcp_pm_addr_entry addr = { .addr = { .family = AF_UNSPEC }, }, *entry;
Geliang Tang0f9f6962021-01-08 16:47:59 -08001748 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1749 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
Geliang Tang0f9f6962021-01-08 16:47:59 -08001750 struct net *net = sock_net(skb->sk);
Davide Caratti602837e2021-12-03 14:35:36 -08001751 u8 bkup = 0, lookup_by_id = 0;
Geliang Tang0f9f6962021-01-08 16:47:59 -08001752 int ret;
1753
Davide Caratti602837e2021-12-03 14:35:36 -08001754 ret = mptcp_pm_parse_addr(attr, info, false, &addr);
Geliang Tang0f9f6962021-01-08 16:47:59 -08001755 if (ret < 0)
1756 return ret;
1757
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001758 if (addr.flags & MPTCP_PM_ADDR_FLAG_BACKUP)
Geliang Tang0f9f6962021-01-08 16:47:59 -08001759 bkup = 1;
Davide Caratti602837e2021-12-03 14:35:36 -08001760 if (addr.addr.family == AF_UNSPEC) {
1761 lookup_by_id = 1;
1762 if (!addr.addr.id)
1763 return -EOPNOTSUPP;
1764 }
Geliang Tang0f9f6962021-01-08 16:47:59 -08001765
1766 list_for_each_entry(entry, &pernet->local_addr_list, list) {
Davide Caratti602837e2021-12-03 14:35:36 -08001767 if ((!lookup_by_id && addresses_equal(&entry->addr, &addr.addr, true)) ||
1768 (lookup_by_id && entry->addr.id == addr.addr.id)) {
Davide Caratti3f4a0892021-09-23 17:04:12 -07001769 mptcp_nl_addr_backup(net, &entry->addr, bkup);
Geliang Tang0f9f6962021-01-08 16:47:59 -08001770
1771 if (bkup)
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001772 entry->flags |= MPTCP_PM_ADDR_FLAG_BACKUP;
Geliang Tang0f9f6962021-01-08 16:47:59 -08001773 else
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001774 entry->flags &= ~MPTCP_PM_ADDR_FLAG_BACKUP;
Geliang Tang0f9f6962021-01-08 16:47:59 -08001775 }
1776 }
1777
1778 return 0;
1779}
1780
Florian Westphalb911c972021-02-12 16:00:01 -08001781static void mptcp_nl_mcast_send(struct net *net, struct sk_buff *nlskb, gfp_t gfp)
1782{
1783 genlmsg_multicast_netns(&mptcp_genl_family, net,
1784 nlskb, 0, MPTCP_PM_EV_GRP_OFFSET, gfp);
1785}
1786
1787static int mptcp_event_add_subflow(struct sk_buff *skb, const struct sock *ssk)
1788{
1789 const struct inet_sock *issk = inet_sk(ssk);
1790 const struct mptcp_subflow_context *sf;
1791
1792 if (nla_put_u16(skb, MPTCP_ATTR_FAMILY, ssk->sk_family))
1793 return -EMSGSIZE;
1794
1795 switch (ssk->sk_family) {
1796 case AF_INET:
1797 if (nla_put_in_addr(skb, MPTCP_ATTR_SADDR4, issk->inet_saddr))
1798 return -EMSGSIZE;
1799 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, issk->inet_daddr))
1800 return -EMSGSIZE;
1801 break;
1802#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1803 case AF_INET6: {
1804 const struct ipv6_pinfo *np = inet6_sk(ssk);
1805
1806 if (nla_put_in6_addr(skb, MPTCP_ATTR_SADDR6, &np->saddr))
1807 return -EMSGSIZE;
1808 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &ssk->sk_v6_daddr))
1809 return -EMSGSIZE;
1810 break;
1811 }
1812#endif
1813 default:
1814 WARN_ON_ONCE(1);
1815 return -EMSGSIZE;
1816 }
1817
1818 if (nla_put_be16(skb, MPTCP_ATTR_SPORT, issk->inet_sport))
1819 return -EMSGSIZE;
1820 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, issk->inet_dport))
1821 return -EMSGSIZE;
1822
1823 sf = mptcp_subflow_ctx(ssk);
1824 if (WARN_ON_ONCE(!sf))
1825 return -EINVAL;
1826
1827 if (nla_put_u8(skb, MPTCP_ATTR_LOC_ID, sf->local_id))
1828 return -EMSGSIZE;
1829
1830 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, sf->remote_id))
1831 return -EMSGSIZE;
1832
1833 return 0;
1834}
1835
1836static int mptcp_event_put_token_and_ssk(struct sk_buff *skb,
1837 const struct mptcp_sock *msk,
1838 const struct sock *ssk)
1839{
1840 const struct sock *sk = (const struct sock *)msk;
1841 const struct mptcp_subflow_context *sf;
1842 u8 sk_err;
1843
1844 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1845 return -EMSGSIZE;
1846
1847 if (mptcp_event_add_subflow(skb, ssk))
1848 return -EMSGSIZE;
1849
1850 sf = mptcp_subflow_ctx(ssk);
1851 if (WARN_ON_ONCE(!sf))
1852 return -EINVAL;
1853
1854 if (nla_put_u8(skb, MPTCP_ATTR_BACKUP, sf->backup))
1855 return -EMSGSIZE;
1856
1857 if (ssk->sk_bound_dev_if &&
1858 nla_put_s32(skb, MPTCP_ATTR_IF_IDX, ssk->sk_bound_dev_if))
1859 return -EMSGSIZE;
1860
1861 sk_err = ssk->sk_err;
1862 if (sk_err && sk->sk_state == TCP_ESTABLISHED &&
1863 nla_put_u8(skb, MPTCP_ATTR_ERROR, sk_err))
1864 return -EMSGSIZE;
1865
1866 return 0;
1867}
1868
1869static int mptcp_event_sub_established(struct sk_buff *skb,
1870 const struct mptcp_sock *msk,
1871 const struct sock *ssk)
1872{
1873 return mptcp_event_put_token_and_ssk(skb, msk, ssk);
1874}
1875
1876static int mptcp_event_sub_closed(struct sk_buff *skb,
1877 const struct mptcp_sock *msk,
1878 const struct sock *ssk)
1879{
Florian Westphaldc87efd2021-04-01 16:19:44 -07001880 const struct mptcp_subflow_context *sf;
1881
Florian Westphalb911c972021-02-12 16:00:01 -08001882 if (mptcp_event_put_token_and_ssk(skb, msk, ssk))
1883 return -EMSGSIZE;
1884
Florian Westphaldc87efd2021-04-01 16:19:44 -07001885 sf = mptcp_subflow_ctx(ssk);
1886 if (!sf->reset_seen)
1887 return 0;
1888
1889 if (nla_put_u32(skb, MPTCP_ATTR_RESET_REASON, sf->reset_reason))
1890 return -EMSGSIZE;
1891
1892 if (nla_put_u32(skb, MPTCP_ATTR_RESET_FLAGS, sf->reset_transient))
1893 return -EMSGSIZE;
1894
Florian Westphalb911c972021-02-12 16:00:01 -08001895 return 0;
1896}
1897
1898static int mptcp_event_created(struct sk_buff *skb,
1899 const struct mptcp_sock *msk,
1900 const struct sock *ssk)
1901{
1902 int err = nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token);
1903
1904 if (err)
1905 return err;
1906
1907 return mptcp_event_add_subflow(skb, ssk);
1908}
1909
1910void mptcp_event_addr_removed(const struct mptcp_sock *msk, uint8_t id)
1911{
1912 struct net *net = sock_net((const struct sock *)msk);
1913 struct nlmsghdr *nlh;
1914 struct sk_buff *skb;
1915
1916 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
1917 return;
1918
1919 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1920 if (!skb)
1921 return;
1922
1923 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, MPTCP_EVENT_REMOVED);
1924 if (!nlh)
1925 goto nla_put_failure;
1926
1927 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1928 goto nla_put_failure;
1929
1930 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, id))
1931 goto nla_put_failure;
1932
1933 genlmsg_end(skb, nlh);
1934 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC);
1935 return;
1936
1937nla_put_failure:
1938 kfree_skb(skb);
1939}
1940
1941void mptcp_event_addr_announced(const struct mptcp_sock *msk,
1942 const struct mptcp_addr_info *info)
1943{
1944 struct net *net = sock_net((const struct sock *)msk);
1945 struct nlmsghdr *nlh;
1946 struct sk_buff *skb;
1947
1948 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
1949 return;
1950
1951 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1952 if (!skb)
1953 return;
1954
1955 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0,
1956 MPTCP_EVENT_ANNOUNCED);
1957 if (!nlh)
1958 goto nla_put_failure;
1959
1960 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1961 goto nla_put_failure;
1962
1963 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, info->id))
1964 goto nla_put_failure;
1965
1966 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, info->port))
1967 goto nla_put_failure;
1968
1969 switch (info->family) {
1970 case AF_INET:
1971 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, info->addr.s_addr))
1972 goto nla_put_failure;
1973 break;
1974#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1975 case AF_INET6:
1976 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &info->addr6))
1977 goto nla_put_failure;
1978 break;
1979#endif
1980 default:
1981 WARN_ON_ONCE(1);
1982 goto nla_put_failure;
1983 }
1984
1985 genlmsg_end(skb, nlh);
1986 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC);
1987 return;
1988
1989nla_put_failure:
1990 kfree_skb(skb);
1991}
1992
1993void mptcp_event(enum mptcp_event_type type, const struct mptcp_sock *msk,
1994 const struct sock *ssk, gfp_t gfp)
1995{
1996 struct net *net = sock_net((const struct sock *)msk);
1997 struct nlmsghdr *nlh;
1998 struct sk_buff *skb;
1999
2000 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
2001 return;
2002
2003 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
2004 if (!skb)
2005 return;
2006
2007 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, type);
2008 if (!nlh)
2009 goto nla_put_failure;
2010
2011 switch (type) {
2012 case MPTCP_EVENT_UNSPEC:
2013 WARN_ON_ONCE(1);
2014 break;
2015 case MPTCP_EVENT_CREATED:
2016 case MPTCP_EVENT_ESTABLISHED:
2017 if (mptcp_event_created(skb, msk, ssk) < 0)
2018 goto nla_put_failure;
2019 break;
2020 case MPTCP_EVENT_CLOSED:
2021 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token) < 0)
2022 goto nla_put_failure;
2023 break;
2024 case MPTCP_EVENT_ANNOUNCED:
2025 case MPTCP_EVENT_REMOVED:
2026 /* call mptcp_event_addr_announced()/removed instead */
2027 WARN_ON_ONCE(1);
2028 break;
2029 case MPTCP_EVENT_SUB_ESTABLISHED:
2030 case MPTCP_EVENT_SUB_PRIORITY:
2031 if (mptcp_event_sub_established(skb, msk, ssk) < 0)
2032 goto nla_put_failure;
2033 break;
2034 case MPTCP_EVENT_SUB_CLOSED:
2035 if (mptcp_event_sub_closed(skb, msk, ssk) < 0)
2036 goto nla_put_failure;
2037 break;
2038 }
2039
2040 genlmsg_end(skb, nlh);
2041 mptcp_nl_mcast_send(net, skb, gfp);
2042 return;
2043
2044nla_put_failure:
2045 kfree_skb(skb);
2046}
2047
Rikard Falkeborn674d3ab2020-10-05 01:44:16 +02002048static const struct genl_small_ops mptcp_pm_ops[] = {
Paolo Abeni01cacb02020-03-27 14:48:51 -07002049 {
2050 .cmd = MPTCP_PM_CMD_ADD_ADDR,
2051 .doit = mptcp_nl_cmd_add_addr,
2052 .flags = GENL_ADMIN_PERM,
2053 },
2054 {
2055 .cmd = MPTCP_PM_CMD_DEL_ADDR,
2056 .doit = mptcp_nl_cmd_del_addr,
2057 .flags = GENL_ADMIN_PERM,
2058 },
2059 {
2060 .cmd = MPTCP_PM_CMD_FLUSH_ADDRS,
2061 .doit = mptcp_nl_cmd_flush_addrs,
2062 .flags = GENL_ADMIN_PERM,
2063 },
2064 {
2065 .cmd = MPTCP_PM_CMD_GET_ADDR,
2066 .doit = mptcp_nl_cmd_get_addr,
2067 .dumpit = mptcp_nl_cmd_dump_addrs,
2068 },
2069 {
2070 .cmd = MPTCP_PM_CMD_SET_LIMITS,
2071 .doit = mptcp_nl_cmd_set_limits,
2072 .flags = GENL_ADMIN_PERM,
2073 },
2074 {
2075 .cmd = MPTCP_PM_CMD_GET_LIMITS,
2076 .doit = mptcp_nl_cmd_get_limits,
2077 },
Geliang Tang0f9f6962021-01-08 16:47:59 -08002078 {
2079 .cmd = MPTCP_PM_CMD_SET_FLAGS,
2080 .doit = mptcp_nl_cmd_set_flags,
2081 .flags = GENL_ADMIN_PERM,
2082 },
Paolo Abeni01cacb02020-03-27 14:48:51 -07002083};
2084
2085static struct genl_family mptcp_genl_family __ro_after_init = {
2086 .name = MPTCP_PM_NAME,
2087 .version = MPTCP_PM_VER,
2088 .maxattr = MPTCP_PM_ATTR_MAX,
2089 .policy = mptcp_pm_policy,
2090 .netnsok = true,
2091 .module = THIS_MODULE,
Jakub Kicinski66a9b922020-10-02 14:49:54 -07002092 .small_ops = mptcp_pm_ops,
2093 .n_small_ops = ARRAY_SIZE(mptcp_pm_ops),
Paolo Abeni01cacb02020-03-27 14:48:51 -07002094 .mcgrps = mptcp_pm_mcgrps,
2095 .n_mcgrps = ARRAY_SIZE(mptcp_pm_mcgrps),
2096};
2097
2098static int __net_init pm_nl_init_net(struct net *net)
2099{
2100 struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id);
2101
2102 INIT_LIST_HEAD_RCU(&pernet->local_addr_list);
Paolo Abeni72bcbc42021-10-15 16:05:51 -07002103
2104 /* Cit. 2 subflows ought to be enough for anybody. */
2105 pernet->subflows_max = 2;
Paolo Abeni01cacb02020-03-27 14:48:51 -07002106 pernet->next_id = 1;
Paolo Abeniff5a0b42021-08-13 15:15:45 -07002107 pernet->stale_loss_cnt = 4;
Paolo Abeni01cacb02020-03-27 14:48:51 -07002108 spin_lock_init(&pernet->lock);
Jianguo Wuae514982021-05-27 16:54:27 -07002109
2110 /* No need to initialize other pernet fields, the struct is zeroed at
2111 * allocation time.
2112 */
2113
Paolo Abeni01cacb02020-03-27 14:48:51 -07002114 return 0;
2115}
2116
2117static void __net_exit pm_nl_exit_net(struct list_head *net_list)
2118{
2119 struct net *net;
2120
2121 list_for_each_entry(net, net_list, exit_list) {
Geliang Tang141694d2020-12-10 14:24:59 -08002122 struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id);
2123
Paolo Abeni01cacb02020-03-27 14:48:51 -07002124 /* net is removed from namespace list, can't race with
Paolo Abenia0eea5f2021-08-18 16:42:36 -07002125 * other modifiers, also netns core already waited for a
2126 * RCU grace period.
Paolo Abeni01cacb02020-03-27 14:48:51 -07002127 */
Geliang Tang0e4a3e62021-03-12 17:16:18 -08002128 __flush_addrs(&pernet->local_addr_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07002129 }
2130}
2131
2132static struct pernet_operations mptcp_pm_pernet_ops = {
2133 .init = pm_nl_init_net,
2134 .exit_batch = pm_nl_exit_net,
2135 .id = &pm_nl_pernet_id,
2136 .size = sizeof(struct pm_nl_pernet),
2137};
2138
Paolo Abenid39dcec2020-06-26 19:29:59 +02002139void __init mptcp_pm_nl_init(void)
Paolo Abeni01cacb02020-03-27 14:48:51 -07002140{
2141 if (register_pernet_subsys(&mptcp_pm_pernet_ops) < 0)
2142 panic("Failed to register MPTCP PM pernet subsystem.\n");
2143
2144 if (genl_register_family(&mptcp_genl_family))
2145 panic("Failed to register MPTCP PM netlink family\n");
2146}