blob: 53c09db080583d1e020543ce0185d5acf1d7dbb7 [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;
28 struct rcu_head rcu;
Geliang Tang1729cf12021-02-01 15:09:12 -080029 struct socket *lsk;
Paolo Abeni01cacb02020-03-27 14:48:51 -070030};
31
Geliang Tang0abd40f2020-09-24 08:30:00 +080032struct mptcp_pm_add_entry {
33 struct list_head list;
34 struct mptcp_addr_info addr;
Geliang Tang00cfd772020-09-24 08:30:02 +080035 struct timer_list add_timer;
36 struct mptcp_sock *sock;
37 u8 retrans_times;
Geliang Tang0abd40f2020-09-24 08:30:00 +080038};
39
Geliang Tangefd5a4c2021-01-08 16:47:55 -080040#define MAX_ADDR_ID 255
41#define BITMAP_SZ DIV_ROUND_UP(MAX_ADDR_ID + 1, BITS_PER_LONG)
42
Paolo Abeni01cacb02020-03-27 14:48:51 -070043struct pm_nl_pernet {
44 /* protects pernet updates */
45 spinlock_t lock;
46 struct list_head local_addr_list;
47 unsigned int addrs;
48 unsigned int add_addr_signal_max;
49 unsigned int add_addr_accept_max;
50 unsigned int local_addr_max;
51 unsigned int subflows_max;
52 unsigned int next_id;
Geliang Tangefd5a4c2021-01-08 16:47:55 -080053 unsigned long id_bitmap[BITMAP_SZ];
Paolo Abeni01cacb02020-03-27 14:48:51 -070054};
55
56#define MPTCP_PM_ADDR_MAX 8
Geliang Tang00cfd772020-09-24 08:30:02 +080057#define ADD_ADDR_RETRANS_MAX 3
Paolo Abeni01cacb02020-03-27 14:48:51 -070058
Florian Westphale9801432021-02-12 15:59:54 -080059static void mptcp_pm_nl_add_addr_send_ack(struct mptcp_sock *msk);
60
Paolo Abeni01cacb02020-03-27 14:48:51 -070061static bool addresses_equal(const struct mptcp_addr_info *a,
62 struct mptcp_addr_info *b, bool use_port)
63{
64 bool addr_equals = false;
65
Matthieu Baerts7b9b0f72021-01-25 10:59:01 -080066 if (a->family == b->family) {
67 if (a->family == AF_INET)
68 addr_equals = a->addr.s_addr == b->addr.s_addr;
Paolo Abeni01cacb02020-03-27 14:48:51 -070069#if IS_ENABLED(CONFIG_MPTCP_IPV6)
Matthieu Baerts7b9b0f72021-01-25 10:59:01 -080070 else
71 addr_equals = !ipv6_addr_cmp(&a->addr6, &b->addr6);
72 } else if (a->family == AF_INET) {
73 if (ipv6_addr_v4mapped(&b->addr6))
74 addr_equals = a->addr.s_addr == b->addr6.s6_addr32[3];
75 } else if (b->family == AF_INET) {
76 if (ipv6_addr_v4mapped(&a->addr6))
77 addr_equals = a->addr6.s6_addr32[3] == b->addr.s_addr;
Paolo Abeni01cacb02020-03-27 14:48:51 -070078#endif
Matthieu Baerts7b9b0f72021-01-25 10:59:01 -080079 }
Paolo Abeni01cacb02020-03-27 14:48:51 -070080
81 if (!addr_equals)
82 return false;
83 if (!use_port)
84 return true;
85
86 return a->port == b->port;
87}
88
Geliang Tang57025812020-09-08 10:49:38 +080089static bool address_zero(const struct mptcp_addr_info *addr)
90{
91 struct mptcp_addr_info zero;
92
93 memset(&zero, 0, sizeof(zero));
94 zero.family = addr->family;
95
Geliang Tang60b57bf2021-02-01 15:09:16 -080096 return addresses_equal(addr, &zero, true);
Geliang Tang57025812020-09-08 10:49:38 +080097}
98
Paolo Abeni01cacb02020-03-27 14:48:51 -070099static void local_address(const struct sock_common *skc,
100 struct mptcp_addr_info *addr)
101{
Paolo Abeni01cacb02020-03-27 14:48:51 -0700102 addr->family = skc->skc_family;
Geliang Tang5bc56382021-02-01 15:09:15 -0800103 addr->port = htons(skc->skc_num);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700104 if (addr->family == AF_INET)
105 addr->addr.s_addr = skc->skc_rcv_saddr;
106#if IS_ENABLED(CONFIG_MPTCP_IPV6)
107 else if (addr->family == AF_INET6)
108 addr->addr6 = skc->skc_v6_rcv_saddr;
109#endif
110}
111
112static void remote_address(const struct sock_common *skc,
113 struct mptcp_addr_info *addr)
114{
115 addr->family = skc->skc_family;
116 addr->port = skc->skc_dport;
117 if (addr->family == AF_INET)
118 addr->addr.s_addr = skc->skc_daddr;
119#if IS_ENABLED(CONFIG_MPTCP_IPV6)
120 else if (addr->family == AF_INET6)
121 addr->addr6 = skc->skc_v6_daddr;
122#endif
123}
124
125static bool lookup_subflow_by_saddr(const struct list_head *list,
126 struct mptcp_addr_info *saddr)
127{
128 struct mptcp_subflow_context *subflow;
129 struct mptcp_addr_info cur;
130 struct sock_common *skc;
131
132 list_for_each_entry(subflow, list, node) {
133 skc = (struct sock_common *)mptcp_subflow_tcp_sock(subflow);
134
135 local_address(skc, &cur);
Geliang Tang60b57bf2021-02-01 15:09:16 -0800136 if (addresses_equal(&cur, saddr, saddr->port))
Paolo Abeni01cacb02020-03-27 14:48:51 -0700137 return true;
138 }
139
140 return false;
141}
142
143static struct mptcp_pm_addr_entry *
144select_local_address(const struct pm_nl_pernet *pernet,
145 struct mptcp_sock *msk)
146{
147 struct mptcp_pm_addr_entry *entry, *ret = NULL;
Matthieu Baerts7b9b0f72021-01-25 10:59:01 -0800148 struct sock *sk = (struct sock *)msk;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700149
Florian Westphal3abc05d2021-02-04 15:23:30 -0800150 msk_owned_by_me(msk);
151
Paolo Abeni01cacb02020-03-27 14:48:51 -0700152 rcu_read_lock();
Paolo Abeni1bc73272020-12-10 14:25:05 -0800153 __mptcp_flush_join_list(msk);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700154 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
Paolo Abenief0da3b2020-09-14 10:01:15 +0200155 if (!(entry->addr.flags & MPTCP_PM_ADDR_FLAG_SUBFLOW))
Paolo Abeni01cacb02020-03-27 14:48:51 -0700156 continue;
157
Matthieu Baerts7b9b0f72021-01-25 10:59:01 -0800158 if (entry->addr.family != sk->sk_family) {
159#if IS_ENABLED(CONFIG_MPTCP_IPV6)
160 if ((entry->addr.family == AF_INET &&
161 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) ||
162 (sk->sk_family == AF_INET &&
163 !ipv6_addr_v4mapped(&entry->addr.addr6)))
164#endif
165 continue;
166 }
167
Paolo Abeni01cacb02020-03-27 14:48:51 -0700168 /* avoid any address already in use by subflows and
169 * pending join
170 */
Matthieu Baerts7b9b0f72021-01-25 10:59:01 -0800171 if (!lookup_subflow_by_saddr(&msk->conn_list, &entry->addr)) {
Paolo Abeni01cacb02020-03-27 14:48:51 -0700172 ret = entry;
173 break;
174 }
175 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700176 rcu_read_unlock();
177 return ret;
178}
179
180static struct mptcp_pm_addr_entry *
181select_signal_address(struct pm_nl_pernet *pernet, unsigned int pos)
182{
183 struct mptcp_pm_addr_entry *entry, *ret = NULL;
184 int i = 0;
185
186 rcu_read_lock();
187 /* do not keep any additional per socket state, just signal
188 * the address list in order.
189 * Note: removal from the local address list during the msk life-cycle
190 * can lead to additional addresses not being announced.
191 */
192 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
Paolo Abenief0da3b2020-09-14 10:01:15 +0200193 if (!(entry->addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL))
Paolo Abeni01cacb02020-03-27 14:48:51 -0700194 continue;
195 if (i++ == pos) {
196 ret = entry;
197 break;
198 }
199 }
200 rcu_read_unlock();
201 return ret;
202}
203
Geliang Tanga914e582021-02-01 15:09:07 -0800204unsigned int mptcp_pm_get_add_addr_signal_max(struct mptcp_sock *msk)
205{
206 struct pm_nl_pernet *pernet;
207
208 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
209 return READ_ONCE(pernet->add_addr_signal_max);
210}
211EXPORT_SYMBOL_GPL(mptcp_pm_get_add_addr_signal_max);
212
213unsigned int mptcp_pm_get_add_addr_accept_max(struct mptcp_sock *msk)
214{
215 struct pm_nl_pernet *pernet;
216
217 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
218 return READ_ONCE(pernet->add_addr_accept_max);
219}
220EXPORT_SYMBOL_GPL(mptcp_pm_get_add_addr_accept_max);
221
222unsigned int mptcp_pm_get_subflows_max(struct mptcp_sock *msk)
223{
224 struct pm_nl_pernet *pernet;
225
226 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
227 return READ_ONCE(pernet->subflows_max);
228}
229EXPORT_SYMBOL_GPL(mptcp_pm_get_subflows_max);
230
Geliang Tang0caf3ad2021-02-12 16:52:02 -0800231unsigned int mptcp_pm_get_local_addr_max(struct mptcp_sock *msk)
Geliang Tanga914e582021-02-01 15:09:07 -0800232{
233 struct pm_nl_pernet *pernet;
234
235 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
236 return READ_ONCE(pernet->local_addr_max);
237}
Geliang Tang0caf3ad2021-02-12 16:52:02 -0800238EXPORT_SYMBOL_GPL(mptcp_pm_get_local_addr_max);
Geliang Tanga914e582021-02-01 15:09:07 -0800239
Paolo Abeni01cacb02020-03-27 14:48:51 -0700240static void check_work_pending(struct mptcp_sock *msk)
241{
Geliang Tanga914e582021-02-01 15:09:07 -0800242 if (msk->pm.add_addr_signaled == mptcp_pm_get_add_addr_signal_max(msk) &&
243 (msk->pm.local_addr_used == mptcp_pm_get_local_addr_max(msk) ||
244 msk->pm.subflows == mptcp_pm_get_subflows_max(msk)))
Paolo Abeni01cacb02020-03-27 14:48:51 -0700245 WRITE_ONCE(msk->pm.work_pending, false);
246}
247
Geliang Tang00cfd772020-09-24 08:30:02 +0800248static struct mptcp_pm_add_entry *
249lookup_anno_list_by_saddr(struct mptcp_sock *msk,
250 struct mptcp_addr_info *addr)
Geliang Tangb6c08382020-09-24 08:29:54 +0800251{
Geliang Tang0abd40f2020-09-24 08:30:00 +0800252 struct mptcp_pm_add_entry *entry;
Geliang Tangb6c08382020-09-24 08:29:54 +0800253
Florian Westphal3abc05d2021-02-04 15:23:30 -0800254 lockdep_assert_held(&msk->pm.lock);
255
Geliang Tangb6c08382020-09-24 08:29:54 +0800256 list_for_each_entry(entry, &msk->pm.anno_list, list) {
Geliang Tang60b57bf2021-02-01 15:09:16 -0800257 if (addresses_equal(&entry->addr, addr, true))
Geliang Tang00cfd772020-09-24 08:30:02 +0800258 return entry;
Geliang Tangb6c08382020-09-24 08:29:54 +0800259 }
260
Geliang Tang00cfd772020-09-24 08:30:02 +0800261 return NULL;
262}
263
Geliang Tang5bc56382021-02-01 15:09:15 -0800264bool mptcp_pm_sport_in_anno_list(struct mptcp_sock *msk, const struct sock *sk)
265{
266 struct mptcp_pm_add_entry *entry;
267 struct mptcp_addr_info saddr;
268 bool ret = false;
269
270 local_address((struct sock_common *)sk, &saddr);
271
272 spin_lock_bh(&msk->pm.lock);
273 list_for_each_entry(entry, &msk->pm.anno_list, list) {
274 if (addresses_equal(&entry->addr, &saddr, true)) {
275 ret = true;
276 goto out;
277 }
278 }
279
280out:
281 spin_unlock_bh(&msk->pm.lock);
282 return ret;
283}
284
Geliang Tang00cfd772020-09-24 08:30:02 +0800285static void mptcp_pm_add_timer(struct timer_list *timer)
286{
287 struct mptcp_pm_add_entry *entry = from_timer(entry, timer, add_timer);
288 struct mptcp_sock *msk = entry->sock;
289 struct sock *sk = (struct sock *)msk;
290
291 pr_debug("msk=%p", msk);
292
293 if (!msk)
294 return;
295
296 if (inet_sk_state_load(sk) == TCP_CLOSE)
297 return;
298
299 if (!entry->addr.id)
300 return;
301
302 if (mptcp_pm_should_add_signal(msk)) {
303 sk_reset_timer(sk, timer, jiffies + TCP_RTO_MAX / 8);
304 goto out;
305 }
306
307 spin_lock_bh(&msk->pm.lock);
308
309 if (!mptcp_pm_should_add_signal(msk)) {
310 pr_debug("retransmit ADD_ADDR id=%d", entry->addr.id);
Geliang Tangf7efc772021-03-26 11:26:31 -0700311 mptcp_pm_announce_addr(msk, &entry->addr, false);
Geliang Tang84dfe362020-11-19 11:46:00 -0800312 mptcp_pm_add_addr_send_ack(msk);
Geliang Tang00cfd772020-09-24 08:30:02 +0800313 entry->retrans_times++;
314 }
315
316 if (entry->retrans_times < ADD_ADDR_RETRANS_MAX)
Geliang Tang93f323b2020-11-03 11:05:07 -0800317 sk_reset_timer(sk, timer,
Geliang Tang724d06b2020-11-10 11:01:43 +0800318 jiffies + mptcp_get_add_addr_timeout(sock_net(sk)));
Geliang Tang00cfd772020-09-24 08:30:02 +0800319
320 spin_unlock_bh(&msk->pm.lock);
321
322out:
323 __sock_put(sk);
324}
325
326struct mptcp_pm_add_entry *
327mptcp_pm_del_add_timer(struct mptcp_sock *msk,
328 struct mptcp_addr_info *addr)
329{
330 struct mptcp_pm_add_entry *entry;
331 struct sock *sk = (struct sock *)msk;
332
333 spin_lock_bh(&msk->pm.lock);
334 entry = lookup_anno_list_by_saddr(msk, addr);
335 if (entry)
336 entry->retrans_times = ADD_ADDR_RETRANS_MAX;
337 spin_unlock_bh(&msk->pm.lock);
338
339 if (entry)
340 sk_stop_timer_sync(sk, &entry->add_timer);
341
342 return entry;
Geliang Tangb6c08382020-09-24 08:29:54 +0800343}
344
345static bool mptcp_pm_alloc_anno_list(struct mptcp_sock *msk,
346 struct mptcp_pm_addr_entry *entry)
347{
Geliang Tang0abd40f2020-09-24 08:30:00 +0800348 struct mptcp_pm_add_entry *add_entry = NULL;
Geliang Tang00cfd772020-09-24 08:30:02 +0800349 struct sock *sk = (struct sock *)msk;
Geliang Tang93f323b2020-11-03 11:05:07 -0800350 struct net *net = sock_net(sk);
Geliang Tangb6c08382020-09-24 08:29:54 +0800351
Florian Westphal3abc05d2021-02-04 15:23:30 -0800352 lockdep_assert_held(&msk->pm.lock);
353
Geliang Tangb6c08382020-09-24 08:29:54 +0800354 if (lookup_anno_list_by_saddr(msk, &entry->addr))
355 return false;
356
Geliang Tang0abd40f2020-09-24 08:30:00 +0800357 add_entry = kmalloc(sizeof(*add_entry), GFP_ATOMIC);
358 if (!add_entry)
Geliang Tangb6c08382020-09-24 08:29:54 +0800359 return false;
360
Geliang Tang0abd40f2020-09-24 08:30:00 +0800361 list_add(&add_entry->list, &msk->pm.anno_list);
Geliang Tangb6c08382020-09-24 08:29:54 +0800362
Geliang Tang00cfd772020-09-24 08:30:02 +0800363 add_entry->addr = entry->addr;
364 add_entry->sock = msk;
365 add_entry->retrans_times = 0;
366
367 timer_setup(&add_entry->add_timer, mptcp_pm_add_timer, 0);
Geliang Tang93f323b2020-11-03 11:05:07 -0800368 sk_reset_timer(sk, &add_entry->add_timer,
369 jiffies + mptcp_get_add_addr_timeout(net));
Geliang Tang00cfd772020-09-24 08:30:02 +0800370
Geliang Tangb6c08382020-09-24 08:29:54 +0800371 return true;
372}
373
374void mptcp_pm_free_anno_list(struct mptcp_sock *msk)
375{
Geliang Tang0abd40f2020-09-24 08:30:00 +0800376 struct mptcp_pm_add_entry *entry, *tmp;
Geliang Tang00cfd772020-09-24 08:30:02 +0800377 struct sock *sk = (struct sock *)msk;
378 LIST_HEAD(free_list);
Geliang Tangb6c08382020-09-24 08:29:54 +0800379
380 pr_debug("msk=%p", msk);
381
382 spin_lock_bh(&msk->pm.lock);
Geliang Tang00cfd772020-09-24 08:30:02 +0800383 list_splice_init(&msk->pm.anno_list, &free_list);
384 spin_unlock_bh(&msk->pm.lock);
385
386 list_for_each_entry_safe(entry, tmp, &free_list, list) {
387 sk_stop_timer_sync(sk, &entry->add_timer);
Geliang Tangb6c08382020-09-24 08:29:54 +0800388 kfree(entry);
389 }
Geliang Tangb6c08382020-09-24 08:29:54 +0800390}
391
Paolo Abeni01cacb02020-03-27 14:48:51 -0700392static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk)
393{
394 struct sock *sk = (struct sock *)msk;
395 struct mptcp_pm_addr_entry *local;
Geliang Tanga914e582021-02-01 15:09:07 -0800396 unsigned int add_addr_signal_max;
397 unsigned int local_addr_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700398 struct pm_nl_pernet *pernet;
Geliang Tanga914e582021-02-01 15:09:07 -0800399 unsigned int subflows_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700400
Geliang Tang432d9e72020-12-09 15:51:28 -0800401 pernet = net_generic(sock_net(sk), pm_nl_pernet_id);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700402
Geliang Tanga914e582021-02-01 15:09:07 -0800403 add_addr_signal_max = mptcp_pm_get_add_addr_signal_max(msk);
404 local_addr_max = mptcp_pm_get_local_addr_max(msk);
405 subflows_max = mptcp_pm_get_subflows_max(msk);
406
Paolo Abeni01cacb02020-03-27 14:48:51 -0700407 pr_debug("local %d:%d signal %d:%d subflows %d:%d\n",
Geliang Tanga914e582021-02-01 15:09:07 -0800408 msk->pm.local_addr_used, local_addr_max,
409 msk->pm.add_addr_signaled, add_addr_signal_max,
410 msk->pm.subflows, subflows_max);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700411
412 /* check first for announce */
Geliang Tanga914e582021-02-01 15:09:07 -0800413 if (msk->pm.add_addr_signaled < add_addr_signal_max) {
Paolo Abeni01cacb02020-03-27 14:48:51 -0700414 local = select_signal_address(pernet,
415 msk->pm.add_addr_signaled);
416
417 if (local) {
Geliang Tangb6c08382020-09-24 08:29:54 +0800418 if (mptcp_pm_alloc_anno_list(msk, local)) {
419 msk->pm.add_addr_signaled++;
Geliang Tangf7efc772021-03-26 11:26:31 -0700420 mptcp_pm_announce_addr(msk, &local->addr, false);
Geliang Tang84dfe362020-11-19 11:46:00 -0800421 mptcp_pm_nl_add_addr_send_ack(msk);
Geliang Tangb6c08382020-09-24 08:29:54 +0800422 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700423 } else {
424 /* pick failed, avoid fourther attempts later */
Geliang Tanga914e582021-02-01 15:09:07 -0800425 msk->pm.local_addr_used = add_addr_signal_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700426 }
427
428 check_work_pending(msk);
429 }
430
431 /* check if should create a new subflow */
Geliang Tanga914e582021-02-01 15:09:07 -0800432 if (msk->pm.local_addr_used < local_addr_max &&
433 msk->pm.subflows < subflows_max) {
Paolo Abeni01cacb02020-03-27 14:48:51 -0700434 local = select_local_address(pernet, msk);
435 if (local) {
Matthieu Baerts1f2f1932021-01-25 10:59:02 -0800436 struct mptcp_addr_info remote = { 0 };
437
Paolo Abeni01cacb02020-03-27 14:48:51 -0700438 msk->pm.local_addr_used++;
439 msk->pm.subflows++;
440 check_work_pending(msk);
Matthieu Baerts1f2f1932021-01-25 10:59:02 -0800441 remote_address((struct sock_common *)sk, &remote);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700442 spin_unlock_bh(&msk->pm.lock);
Paolo Abenief0da3b2020-09-14 10:01:15 +0200443 __mptcp_subflow_connect(sk, &local->addr, &remote);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700444 spin_lock_bh(&msk->pm.lock);
445 return;
446 }
447
448 /* lookup failed, avoid fourther attempts later */
Geliang Tanga914e582021-02-01 15:09:07 -0800449 msk->pm.local_addr_used = local_addr_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700450 check_work_pending(msk);
451 }
452}
453
Florian Westphale9801432021-02-12 15:59:54 -0800454static void mptcp_pm_nl_fully_established(struct mptcp_sock *msk)
Paolo Abeni01cacb02020-03-27 14:48:51 -0700455{
456 mptcp_pm_create_subflow_or_signal_addr(msk);
457}
458
Florian Westphale9801432021-02-12 15:59:54 -0800459static void mptcp_pm_nl_subflow_established(struct mptcp_sock *msk)
Paolo Abeni01cacb02020-03-27 14:48:51 -0700460{
461 mptcp_pm_create_subflow_or_signal_addr(msk);
462}
463
Florian Westphale9801432021-02-12 15:59:54 -0800464static void mptcp_pm_nl_add_addr_received(struct mptcp_sock *msk)
Paolo Abeni01cacb02020-03-27 14:48:51 -0700465{
466 struct sock *sk = (struct sock *)msk;
Geliang Tanga914e582021-02-01 15:09:07 -0800467 unsigned int add_addr_accept_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700468 struct mptcp_addr_info remote;
469 struct mptcp_addr_info local;
Geliang Tanga914e582021-02-01 15:09:07 -0800470 unsigned int subflows_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700471
Geliang Tanga914e582021-02-01 15:09:07 -0800472 add_addr_accept_max = mptcp_pm_get_add_addr_accept_max(msk);
473 subflows_max = mptcp_pm_get_subflows_max(msk);
474
Paolo Abeni01cacb02020-03-27 14:48:51 -0700475 pr_debug("accepted %d:%d remote family %d",
Geliang Tanga914e582021-02-01 15:09:07 -0800476 msk->pm.add_addr_accepted, add_addr_accept_max,
Paolo Abeni01cacb02020-03-27 14:48:51 -0700477 msk->pm.remote.family);
478 msk->pm.add_addr_accepted++;
479 msk->pm.subflows++;
Geliang Tanga914e582021-02-01 15:09:07 -0800480 if (msk->pm.add_addr_accepted >= add_addr_accept_max ||
481 msk->pm.subflows >= subflows_max)
Paolo Abeni01cacb02020-03-27 14:48:51 -0700482 WRITE_ONCE(msk->pm.accept_addr, false);
483
484 /* connect to the specified remote address, using whatever
485 * local address the routing configuration will pick.
486 */
487 remote = msk->pm.remote;
488 if (!remote.port)
489 remote.port = sk->sk_dport;
490 memset(&local, 0, sizeof(local));
491 local.family = remote.family;
492
493 spin_unlock_bh(&msk->pm.lock);
Geliang Tang432d9e72020-12-09 15:51:28 -0800494 __mptcp_subflow_connect(sk, &local, &remote);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700495 spin_lock_bh(&msk->pm.lock);
Geliang Tang6a6c05a2020-09-24 08:29:50 +0800496
Geliang Tangf7efc772021-03-26 11:26:31 -0700497 mptcp_pm_announce_addr(msk, &msk->pm.remote, true);
Geliang Tang84dfe362020-11-19 11:46:00 -0800498 mptcp_pm_nl_add_addr_send_ack(msk);
499}
500
Florian Westphale9801432021-02-12 15:59:54 -0800501static void mptcp_pm_nl_add_addr_send_ack(struct mptcp_sock *msk)
Geliang Tang84dfe362020-11-19 11:46:00 -0800502{
503 struct mptcp_subflow_context *subflow;
504
Florian Westphal3abc05d2021-02-04 15:23:30 -0800505 msk_owned_by_me(msk);
506 lockdep_assert_held(&msk->pm.lock);
507
Geliang Tangb5a7acd2021-02-01 15:09:09 -0800508 if (!mptcp_pm_should_add_signal(msk))
Geliang Tang84dfe362020-11-19 11:46:00 -0800509 return;
510
511 __mptcp_flush_join_list(msk);
512 subflow = list_first_entry_or_null(&msk->conn_list, typeof(*subflow), node);
513 if (subflow) {
514 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
515 u8 add_addr;
516
517 spin_unlock_bh(&msk->pm.lock);
Geliang Tangb5a7acd2021-02-01 15:09:09 -0800518 pr_debug("send ack for add_addr%s%s",
519 mptcp_pm_should_add_signal_ipv6(msk) ? " [ipv6]" : "",
520 mptcp_pm_should_add_signal_port(msk) ? " [port]" : "");
Geliang Tangfbe0f872020-12-09 15:51:23 -0800521
Geliang Tang84dfe362020-11-19 11:46:00 -0800522 lock_sock(ssk);
523 tcp_send_ack(ssk);
524 release_sock(ssk);
525 spin_lock_bh(&msk->pm.lock);
526
Geliang Tang13ad9f02020-12-09 15:51:27 -0800527 add_addr = READ_ONCE(msk->pm.addr_signal);
Geliang Tangfbe0f872020-12-09 15:51:23 -0800528 if (mptcp_pm_should_add_signal_ipv6(msk))
529 add_addr &= ~BIT(MPTCP_ADD_ADDR_IPV6);
530 if (mptcp_pm_should_add_signal_port(msk))
531 add_addr &= ~BIT(MPTCP_ADD_ADDR_PORT);
Geliang Tang13ad9f02020-12-09 15:51:27 -0800532 WRITE_ONCE(msk->pm.addr_signal, add_addr);
Geliang Tang84dfe362020-11-19 11:46:00 -0800533 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700534}
535
Geliang Tang06706542021-01-08 16:47:57 -0800536int mptcp_pm_nl_mp_prio_send_ack(struct mptcp_sock *msk,
537 struct mptcp_addr_info *addr,
538 u8 bkup)
539{
540 struct mptcp_subflow_context *subflow;
541
542 pr_debug("bkup=%d", bkup);
543
544 mptcp_for_each_subflow(msk, subflow) {
545 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
Geliang Tang0be2ac22021-01-08 16:48:01 -0800546 struct sock *sk = (struct sock *)msk;
Geliang Tang06706542021-01-08 16:47:57 -0800547 struct mptcp_addr_info local;
548
549 local_address((struct sock_common *)ssk, &local);
550 if (!addresses_equal(&local, addr, addr->port))
551 continue;
552
553 subflow->backup = bkup;
554 subflow->send_mp_prio = 1;
555 subflow->request_bkup = bkup;
Geliang Tang0be2ac22021-01-08 16:48:01 -0800556 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIOTX);
Geliang Tang06706542021-01-08 16:47:57 -0800557
558 spin_unlock_bh(&msk->pm.lock);
559 pr_debug("send ack for mp_prio");
560 lock_sock(ssk);
561 tcp_send_ack(ssk);
562 release_sock(ssk);
563 spin_lock_bh(&msk->pm.lock);
564
565 return 0;
566 }
567
568 return -EINVAL;
569}
570
Florian Westphale9801432021-02-12 15:59:54 -0800571static void mptcp_pm_nl_rm_addr_received(struct mptcp_sock *msk)
Geliang Tangd0876b22020-09-24 08:29:49 +0800572{
573 struct mptcp_subflow_context *subflow, *tmp;
574 struct sock *sk = (struct sock *)msk;
Geliang Tangd0b698c2021-03-12 17:16:15 -0800575 u8 i;
Geliang Tangd0876b22020-09-24 08:29:49 +0800576
Geliang Tangd0b698c2021-03-12 17:16:15 -0800577 pr_debug("address rm_list_nr %d", msk->pm.rm_list_rx.nr);
Geliang Tangd0876b22020-09-24 08:29:49 +0800578
Florian Westphal3abc05d2021-02-04 15:23:30 -0800579 msk_owned_by_me(msk);
580
Geliang Tangd0b698c2021-03-12 17:16:15 -0800581 if (!msk->pm.rm_list_rx.nr)
Geliang Tangd0876b22020-09-24 08:29:49 +0800582 return;
583
584 if (list_empty(&msk->conn_list))
585 return;
586
Geliang Tangd0b698c2021-03-12 17:16:15 -0800587 for (i = 0; i < msk->pm.rm_list_rx.nr; i++) {
588 list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
589 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
590 int how = RCV_SHUTDOWN | SEND_SHUTDOWN;
Geliang Tangd0876b22020-09-24 08:29:49 +0800591
Geliang Tangd0b698c2021-03-12 17:16:15 -0800592 if (msk->pm.rm_list_rx.ids[i] != subflow->remote_id)
593 continue;
Geliang Tangd0876b22020-09-24 08:29:49 +0800594
Geliang Tangd0b698c2021-03-12 17:16:15 -0800595 pr_debug(" -> address rm_list_ids[%d]=%u", i, msk->pm.rm_list_rx.ids[i]);
596 spin_unlock_bh(&msk->pm.lock);
597 mptcp_subflow_shutdown(sk, ssk, how);
598 mptcp_close_ssk(sk, ssk, subflow);
599 spin_lock_bh(&msk->pm.lock);
Geliang Tangd0876b22020-09-24 08:29:49 +0800600
Geliang Tangd0b698c2021-03-12 17:16:15 -0800601 msk->pm.add_addr_accepted--;
602 msk->pm.subflows--;
603 WRITE_ONCE(msk->pm.accept_addr, true);
Geliang Tangd0876b22020-09-24 08:29:49 +0800604
Geliang Tangd0b698c2021-03-12 17:16:15 -0800605 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMADDR);
Geliang Tang7a7e52e2020-09-24 08:29:56 +0800606
Geliang Tangd0b698c2021-03-12 17:16:15 -0800607 break;
608 }
Geliang Tangd0876b22020-09-24 08:29:49 +0800609 }
610}
611
Florian Westphale9801432021-02-12 15:59:54 -0800612void mptcp_pm_nl_work(struct mptcp_sock *msk)
613{
614 struct mptcp_pm_data *pm = &msk->pm;
615
616 msk_owned_by_me(msk);
617
618 spin_lock_bh(&msk->pm.lock);
619
620 pr_debug("msk=%p status=%x", msk, pm->status);
621 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_RECEIVED)) {
622 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_RECEIVED);
623 mptcp_pm_nl_add_addr_received(msk);
624 }
625 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_SEND_ACK)) {
626 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_SEND_ACK);
627 mptcp_pm_nl_add_addr_send_ack(msk);
628 }
629 if (pm->status & BIT(MPTCP_PM_RM_ADDR_RECEIVED)) {
630 pm->status &= ~BIT(MPTCP_PM_RM_ADDR_RECEIVED);
631 mptcp_pm_nl_rm_addr_received(msk);
632 }
633 if (pm->status & BIT(MPTCP_PM_ESTABLISHED)) {
634 pm->status &= ~BIT(MPTCP_PM_ESTABLISHED);
635 mptcp_pm_nl_fully_established(msk);
636 }
637 if (pm->status & BIT(MPTCP_PM_SUBFLOW_ESTABLISHED)) {
638 pm->status &= ~BIT(MPTCP_PM_SUBFLOW_ESTABLISHED);
639 mptcp_pm_nl_subflow_established(msk);
640 }
641
642 spin_unlock_bh(&msk->pm.lock);
643}
644
Geliang Tangddd14bb2021-03-12 17:16:16 -0800645void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk,
646 const struct mptcp_rm_list *rm_list)
Geliang Tang0ee42612020-09-24 08:29:55 +0800647{
648 struct mptcp_subflow_context *subflow, *tmp;
649 struct sock *sk = (struct sock *)msk;
Geliang Tangddd14bb2021-03-12 17:16:16 -0800650 u8 i;
Geliang Tang0ee42612020-09-24 08:29:55 +0800651
Geliang Tangddd14bb2021-03-12 17:16:16 -0800652 pr_debug("subflow rm_list_nr %d", rm_list->nr);
Geliang Tang0ee42612020-09-24 08:29:55 +0800653
Florian Westphal3abc05d2021-02-04 15:23:30 -0800654 msk_owned_by_me(msk);
655
Geliang Tangddd14bb2021-03-12 17:16:16 -0800656 if (!rm_list->nr)
Geliang Tang0ee42612020-09-24 08:29:55 +0800657 return;
658
659 if (list_empty(&msk->conn_list))
660 return;
661
Geliang Tangddd14bb2021-03-12 17:16:16 -0800662 for (i = 0; i < rm_list->nr; i++) {
663 list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
664 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
665 int how = RCV_SHUTDOWN | SEND_SHUTDOWN;
Geliang Tang0ee42612020-09-24 08:29:55 +0800666
Geliang Tangddd14bb2021-03-12 17:16:16 -0800667 if (rm_list->ids[i] != subflow->local_id)
668 continue;
Geliang Tang0ee42612020-09-24 08:29:55 +0800669
Geliang Tangddd14bb2021-03-12 17:16:16 -0800670 pr_debug(" -> subflow rm_list_ids[%d]=%u", i, rm_list->ids[i]);
671 spin_unlock_bh(&msk->pm.lock);
672 mptcp_subflow_shutdown(sk, ssk, how);
673 mptcp_close_ssk(sk, ssk, subflow);
674 spin_lock_bh(&msk->pm.lock);
Geliang Tang0ee42612020-09-24 08:29:55 +0800675
Geliang Tangddd14bb2021-03-12 17:16:16 -0800676 msk->pm.local_addr_used--;
677 msk->pm.subflows--;
Geliang Tang0ee42612020-09-24 08:29:55 +0800678
Geliang Tangddd14bb2021-03-12 17:16:16 -0800679 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMSUBFLOW);
Geliang Tang7a7e52e2020-09-24 08:29:56 +0800680
Geliang Tangddd14bb2021-03-12 17:16:16 -0800681 break;
682 }
Geliang Tang0ee42612020-09-24 08:29:55 +0800683 }
684}
685
Paolo Abeni01cacb02020-03-27 14:48:51 -0700686static bool address_use_port(struct mptcp_pm_addr_entry *entry)
687{
Paolo Abenief0da3b2020-09-14 10:01:15 +0200688 return (entry->addr.flags &
Paolo Abeni01cacb02020-03-27 14:48:51 -0700689 (MPTCP_PM_ADDR_FLAG_SIGNAL | MPTCP_PM_ADDR_FLAG_SUBFLOW)) ==
690 MPTCP_PM_ADDR_FLAG_SIGNAL;
691}
692
693static int mptcp_pm_nl_append_new_local_addr(struct pm_nl_pernet *pernet,
694 struct mptcp_pm_addr_entry *entry)
695{
696 struct mptcp_pm_addr_entry *cur;
Geliang Tang72603d22021-02-01 15:09:06 -0800697 unsigned int addr_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700698 int ret = -EINVAL;
699
700 spin_lock_bh(&pernet->lock);
701 /* to keep the code simple, don't do IDR-like allocation for address ID,
702 * just bail when we exceed limits
703 */
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800704 if (pernet->next_id == MAX_ADDR_ID)
705 pernet->next_id = 1;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700706 if (pernet->addrs >= MPTCP_PM_ADDR_MAX)
707 goto out;
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800708 if (test_bit(entry->addr.id, pernet->id_bitmap))
709 goto out;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700710
711 /* do not insert duplicate address, differentiate on port only
712 * singled addresses
713 */
714 list_for_each_entry(cur, &pernet->local_addr_list, list) {
715 if (addresses_equal(&cur->addr, &entry->addr,
716 address_use_port(entry) &&
717 address_use_port(cur)))
718 goto out;
719 }
720
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800721 if (!entry->addr.id) {
722find_next:
723 entry->addr.id = find_next_zero_bit(pernet->id_bitmap,
724 MAX_ADDR_ID + 1,
725 pernet->next_id);
726 if ((!entry->addr.id || entry->addr.id > MAX_ADDR_ID) &&
727 pernet->next_id != 1) {
728 pernet->next_id = 1;
729 goto find_next;
730 }
731 }
732
733 if (!entry->addr.id || entry->addr.id > MAX_ADDR_ID)
734 goto out;
735
736 __set_bit(entry->addr.id, pernet->id_bitmap);
737 if (entry->addr.id > pernet->next_id)
738 pernet->next_id = entry->addr.id;
739
Geliang Tang72603d22021-02-01 15:09:06 -0800740 if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
741 addr_max = pernet->add_addr_signal_max;
742 WRITE_ONCE(pernet->add_addr_signal_max, addr_max + 1);
743 }
744 if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) {
745 addr_max = pernet->local_addr_max;
746 WRITE_ONCE(pernet->local_addr_max, addr_max + 1);
747 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700748
Paolo Abeni01cacb02020-03-27 14:48:51 -0700749 pernet->addrs++;
750 list_add_tail_rcu(&entry->list, &pernet->local_addr_list);
751 ret = entry->addr.id;
752
753out:
754 spin_unlock_bh(&pernet->lock);
755 return ret;
756}
757
Geliang Tang1729cf12021-02-01 15:09:12 -0800758static int mptcp_pm_nl_create_listen_socket(struct sock *sk,
759 struct mptcp_pm_addr_entry *entry)
760{
761 struct sockaddr_storage addr;
762 struct mptcp_sock *msk;
763 struct socket *ssock;
764 int backlog = 1024;
765 int err;
766
767 err = sock_create_kern(sock_net(sk), entry->addr.family,
768 SOCK_STREAM, IPPROTO_MPTCP, &entry->lsk);
769 if (err)
770 return err;
771
772 msk = mptcp_sk(entry->lsk->sk);
773 if (!msk) {
774 err = -EINVAL;
775 goto out;
776 }
777
778 ssock = __mptcp_nmpc_socket(msk);
779 if (!ssock) {
780 err = -EINVAL;
781 goto out;
782 }
783
784 mptcp_info2sockaddr(&entry->addr, &addr, entry->addr.family);
785 err = kernel_bind(ssock, (struct sockaddr *)&addr,
786 sizeof(struct sockaddr_in));
787 if (err) {
788 pr_warn("kernel_bind error, err=%d", err);
789 goto out;
790 }
791
792 err = kernel_listen(ssock, backlog);
793 if (err) {
794 pr_warn("kernel_listen error, err=%d", err);
795 goto out;
796 }
797
798 return 0;
799
800out:
801 sock_release(entry->lsk);
802 return err;
803}
804
Paolo Abeni01cacb02020-03-27 14:48:51 -0700805int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc)
806{
807 struct mptcp_pm_addr_entry *entry;
808 struct mptcp_addr_info skc_local;
809 struct mptcp_addr_info msk_local;
810 struct pm_nl_pernet *pernet;
811 int ret = -1;
812
813 if (WARN_ON_ONCE(!msk))
814 return -1;
815
816 /* The 0 ID mapping is defined by the first subflow, copied into the msk
817 * addr
818 */
819 local_address((struct sock_common *)msk, &msk_local);
Geliang Tang57025812020-09-08 10:49:38 +0800820 local_address((struct sock_common *)skc, &skc_local);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700821 if (addresses_equal(&msk_local, &skc_local, false))
822 return 0;
823
Geliang Tang57025812020-09-08 10:49:38 +0800824 if (address_zero(&skc_local))
825 return 0;
826
Paolo Abeni01cacb02020-03-27 14:48:51 -0700827 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
828
829 rcu_read_lock();
830 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
Geliang Tang60b57bf2021-02-01 15:09:16 -0800831 if (addresses_equal(&entry->addr, &skc_local, entry->addr.port)) {
Paolo Abeni01cacb02020-03-27 14:48:51 -0700832 ret = entry->addr.id;
833 break;
834 }
835 }
836 rcu_read_unlock();
837 if (ret >= 0)
838 return ret;
839
840 /* address not found, add to local list */
Geliang Tangf612eb72020-09-09 11:01:24 +0800841 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700842 if (!entry)
843 return -ENOMEM;
844
Paolo Abeni01cacb02020-03-27 14:48:51 -0700845 entry->addr = skc_local;
Paolo Abenief0da3b2020-09-14 10:01:15 +0200846 entry->addr.ifindex = 0;
847 entry->addr.flags = 0;
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800848 entry->addr.id = 0;
Geliang Tang1729cf12021-02-01 15:09:12 -0800849 entry->addr.port = 0;
850 entry->lsk = NULL;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700851 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
852 if (ret < 0)
853 kfree(entry);
854
855 return ret;
856}
857
858void mptcp_pm_nl_data_init(struct mptcp_sock *msk)
859{
860 struct mptcp_pm_data *pm = &msk->pm;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700861 bool subflows;
862
Geliang Tanga914e582021-02-01 15:09:07 -0800863 subflows = !!mptcp_pm_get_subflows_max(msk);
864 WRITE_ONCE(pm->work_pending, (!!mptcp_pm_get_local_addr_max(msk) && subflows) ||
865 !!mptcp_pm_get_add_addr_signal_max(msk));
866 WRITE_ONCE(pm->accept_addr, !!mptcp_pm_get_add_addr_accept_max(msk) && subflows);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700867 WRITE_ONCE(pm->accept_subflow, subflows);
868}
869
Florian Westphalb911c972021-02-12 16:00:01 -0800870#define MPTCP_PM_CMD_GRP_OFFSET 0
871#define MPTCP_PM_EV_GRP_OFFSET 1
Paolo Abeni01cacb02020-03-27 14:48:51 -0700872
873static const struct genl_multicast_group mptcp_pm_mcgrps[] = {
874 [MPTCP_PM_CMD_GRP_OFFSET] = { .name = MPTCP_PM_CMD_GRP_NAME, },
Florian Westphalb911c972021-02-12 16:00:01 -0800875 [MPTCP_PM_EV_GRP_OFFSET] = { .name = MPTCP_PM_EV_GRP_NAME,
876 .flags = GENL_UNS_ADMIN_PERM,
877 },
Paolo Abeni01cacb02020-03-27 14:48:51 -0700878};
879
880static const struct nla_policy
881mptcp_pm_addr_policy[MPTCP_PM_ADDR_ATTR_MAX + 1] = {
882 [MPTCP_PM_ADDR_ATTR_FAMILY] = { .type = NLA_U16, },
883 [MPTCP_PM_ADDR_ATTR_ID] = { .type = NLA_U8, },
884 [MPTCP_PM_ADDR_ATTR_ADDR4] = { .type = NLA_U32, },
Johannes Berg81408602020-08-18 10:17:31 +0200885 [MPTCP_PM_ADDR_ATTR_ADDR6] =
886 NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
Paolo Abeni01cacb02020-03-27 14:48:51 -0700887 [MPTCP_PM_ADDR_ATTR_PORT] = { .type = NLA_U16 },
888 [MPTCP_PM_ADDR_ATTR_FLAGS] = { .type = NLA_U32 },
889 [MPTCP_PM_ADDR_ATTR_IF_IDX] = { .type = NLA_S32 },
890};
891
892static const struct nla_policy mptcp_pm_policy[MPTCP_PM_ATTR_MAX + 1] = {
893 [MPTCP_PM_ATTR_ADDR] =
894 NLA_POLICY_NESTED(mptcp_pm_addr_policy),
895 [MPTCP_PM_ATTR_RCV_ADD_ADDRS] = { .type = NLA_U32, },
896 [MPTCP_PM_ATTR_SUBFLOWS] = { .type = NLA_U32, },
897};
898
899static int mptcp_pm_family_to_addr(int family)
900{
901#if IS_ENABLED(CONFIG_MPTCP_IPV6)
902 if (family == AF_INET6)
903 return MPTCP_PM_ADDR_ATTR_ADDR6;
904#endif
905 return MPTCP_PM_ADDR_ATTR_ADDR4;
906}
907
908static int mptcp_pm_parse_addr(struct nlattr *attr, struct genl_info *info,
909 bool require_family,
910 struct mptcp_pm_addr_entry *entry)
911{
912 struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1];
913 int err, addr_addr;
914
915 if (!attr) {
916 GENL_SET_ERR_MSG(info, "missing address info");
917 return -EINVAL;
918 }
919
920 /* no validation needed - was already done via nested policy */
921 err = nla_parse_nested_deprecated(tb, MPTCP_PM_ADDR_ATTR_MAX, attr,
922 mptcp_pm_addr_policy, info->extack);
923 if (err)
924 return err;
925
926 memset(entry, 0, sizeof(*entry));
927 if (!tb[MPTCP_PM_ADDR_ATTR_FAMILY]) {
928 if (!require_family)
929 goto skip_family;
930
931 NL_SET_ERR_MSG_ATTR(info->extack, attr,
932 "missing family");
933 return -EINVAL;
934 }
935
936 entry->addr.family = nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_FAMILY]);
937 if (entry->addr.family != AF_INET
938#if IS_ENABLED(CONFIG_MPTCP_IPV6)
939 && entry->addr.family != AF_INET6
940#endif
941 ) {
942 NL_SET_ERR_MSG_ATTR(info->extack, attr,
943 "unknown address family");
944 return -EINVAL;
945 }
946 addr_addr = mptcp_pm_family_to_addr(entry->addr.family);
947 if (!tb[addr_addr]) {
948 NL_SET_ERR_MSG_ATTR(info->extack, attr,
949 "missing address data");
950 return -EINVAL;
951 }
952
953#if IS_ENABLED(CONFIG_MPTCP_IPV6)
954 if (entry->addr.family == AF_INET6)
955 entry->addr.addr6 = nla_get_in6_addr(tb[addr_addr]);
956 else
957#endif
958 entry->addr.addr.s_addr = nla_get_in_addr(tb[addr_addr]);
959
960skip_family:
Paolo Abenief0da3b2020-09-14 10:01:15 +0200961 if (tb[MPTCP_PM_ADDR_ATTR_IF_IDX]) {
962 u32 val = nla_get_s32(tb[MPTCP_PM_ADDR_ATTR_IF_IDX]);
963
964 entry->addr.ifindex = val;
965 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700966
967 if (tb[MPTCP_PM_ADDR_ATTR_ID])
968 entry->addr.id = nla_get_u8(tb[MPTCP_PM_ADDR_ATTR_ID]);
969
970 if (tb[MPTCP_PM_ADDR_ATTR_FLAGS])
Paolo Abenief0da3b2020-09-14 10:01:15 +0200971 entry->addr.flags = nla_get_u32(tb[MPTCP_PM_ADDR_ATTR_FLAGS]);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700972
Geliang Tanga77e9172021-02-01 15:09:17 -0800973 if (tb[MPTCP_PM_ADDR_ATTR_PORT])
974 entry->addr.port = htons(nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_PORT]));
975
Paolo Abeni01cacb02020-03-27 14:48:51 -0700976 return 0;
977}
978
979static struct pm_nl_pernet *genl_info_pm_nl(struct genl_info *info)
980{
981 return net_generic(genl_info_net(info), pm_nl_pernet_id);
982}
983
Geliang Tang875b76712021-02-01 15:09:08 -0800984static int mptcp_nl_add_subflow_or_signal_addr(struct net *net)
985{
986 struct mptcp_sock *msk;
987 long s_slot = 0, s_num = 0;
988
989 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
990 struct sock *sk = (struct sock *)msk;
991
992 if (!READ_ONCE(msk->fully_established))
993 goto next;
994
995 lock_sock(sk);
996 spin_lock_bh(&msk->pm.lock);
997 mptcp_pm_create_subflow_or_signal_addr(msk);
998 spin_unlock_bh(&msk->pm.lock);
999 release_sock(sk);
1000
1001next:
1002 sock_put(sk);
1003 cond_resched();
1004 }
1005
1006 return 0;
1007}
1008
Paolo Abeni01cacb02020-03-27 14:48:51 -07001009static int mptcp_nl_cmd_add_addr(struct sk_buff *skb, struct genl_info *info)
1010{
1011 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1012 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1013 struct mptcp_pm_addr_entry addr, *entry;
1014 int ret;
1015
1016 ret = mptcp_pm_parse_addr(attr, info, true, &addr);
1017 if (ret < 0)
1018 return ret;
1019
1020 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1021 if (!entry) {
1022 GENL_SET_ERR_MSG(info, "can't allocate addr");
1023 return -ENOMEM;
1024 }
1025
1026 *entry = addr;
Geliang Tang1729cf12021-02-01 15:09:12 -08001027 if (entry->addr.port) {
1028 ret = mptcp_pm_nl_create_listen_socket(skb->sk, entry);
1029 if (ret) {
1030 GENL_SET_ERR_MSG(info, "create listen socket error");
1031 kfree(entry);
1032 return ret;
1033 }
1034 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001035 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
1036 if (ret < 0) {
1037 GENL_SET_ERR_MSG(info, "too many addresses or duplicate one");
Geliang Tang1729cf12021-02-01 15:09:12 -08001038 if (entry->lsk)
1039 sock_release(entry->lsk);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001040 kfree(entry);
1041 return ret;
1042 }
1043
Geliang Tang875b76712021-02-01 15:09:08 -08001044 mptcp_nl_add_subflow_or_signal_addr(sock_net(skb->sk));
1045
Paolo Abeni01cacb02020-03-27 14:48:51 -07001046 return 0;
1047}
1048
1049static struct mptcp_pm_addr_entry *
1050__lookup_addr_by_id(struct pm_nl_pernet *pernet, unsigned int id)
1051{
1052 struct mptcp_pm_addr_entry *entry;
1053
1054 list_for_each_entry(entry, &pernet->local_addr_list, list) {
1055 if (entry->addr.id == id)
1056 return entry;
1057 }
1058 return NULL;
1059}
1060
Geliang Tangb6c08382020-09-24 08:29:54 +08001061static bool remove_anno_list_by_saddr(struct mptcp_sock *msk,
1062 struct mptcp_addr_info *addr)
1063{
Geliang Tang00cfd772020-09-24 08:30:02 +08001064 struct mptcp_pm_add_entry *entry;
Geliang Tangb6c08382020-09-24 08:29:54 +08001065
Geliang Tang00cfd772020-09-24 08:30:02 +08001066 entry = mptcp_pm_del_add_timer(msk, addr);
1067 if (entry) {
1068 list_del(&entry->list);
1069 kfree(entry);
1070 return true;
Geliang Tangb6c08382020-09-24 08:29:54 +08001071 }
1072
1073 return false;
1074}
1075
1076static bool mptcp_pm_remove_anno_addr(struct mptcp_sock *msk,
1077 struct mptcp_addr_info *addr,
1078 bool force)
1079{
Geliang Tangcbde2782021-03-12 17:16:12 -08001080 struct mptcp_rm_list list = { .nr = 0 };
Geliang Tangb6c08382020-09-24 08:29:54 +08001081 bool ret;
1082
Geliang Tangcbde2782021-03-12 17:16:12 -08001083 list.ids[list.nr++] = addr->id;
1084
Geliang Tangb6c08382020-09-24 08:29:54 +08001085 ret = remove_anno_list_by_saddr(msk, addr);
Geliang Tang00cfd772020-09-24 08:30:02 +08001086 if (ret || force) {
1087 spin_lock_bh(&msk->pm.lock);
Geliang Tangcbde2782021-03-12 17:16:12 -08001088 mptcp_pm_remove_addr(msk, &list);
Geliang Tang00cfd772020-09-24 08:30:02 +08001089 spin_unlock_bh(&msk->pm.lock);
1090 }
Geliang Tangb6c08382020-09-24 08:29:54 +08001091 return ret;
1092}
1093
1094static int mptcp_nl_remove_subflow_and_signal_addr(struct net *net,
1095 struct mptcp_addr_info *addr)
1096{
1097 struct mptcp_sock *msk;
1098 long s_slot = 0, s_num = 0;
Geliang Tangddd14bb2021-03-12 17:16:16 -08001099 struct mptcp_rm_list list = { .nr = 0 };
Geliang Tangb6c08382020-09-24 08:29:54 +08001100
1101 pr_debug("remove_id=%d", addr->id);
1102
Geliang Tangddd14bb2021-03-12 17:16:16 -08001103 list.ids[list.nr++] = addr->id;
1104
Geliang Tangb6c08382020-09-24 08:29:54 +08001105 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1106 struct sock *sk = (struct sock *)msk;
1107 bool remove_subflow;
1108
1109 if (list_empty(&msk->conn_list)) {
1110 mptcp_pm_remove_anno_addr(msk, addr, false);
1111 goto next;
1112 }
1113
1114 lock_sock(sk);
1115 remove_subflow = lookup_subflow_by_saddr(&msk->conn_list, addr);
1116 mptcp_pm_remove_anno_addr(msk, addr, remove_subflow);
1117 if (remove_subflow)
Geliang Tangddd14bb2021-03-12 17:16:16 -08001118 mptcp_pm_remove_subflow(msk, &list);
Geliang Tangb6c08382020-09-24 08:29:54 +08001119 release_sock(sk);
1120
1121next:
1122 sock_put(sk);
1123 cond_resched();
1124 }
1125
1126 return 0;
1127}
1128
Geliang Tang1729cf12021-02-01 15:09:12 -08001129struct addr_entry_release_work {
1130 struct rcu_work rwork;
1131 struct mptcp_pm_addr_entry *entry;
1132};
1133
1134static void mptcp_pm_release_addr_entry(struct work_struct *work)
1135{
1136 struct addr_entry_release_work *w;
1137 struct mptcp_pm_addr_entry *entry;
1138
1139 w = container_of(to_rcu_work(work), struct addr_entry_release_work, rwork);
1140 entry = w->entry;
1141 if (entry) {
1142 if (entry->lsk)
1143 sock_release(entry->lsk);
1144 kfree(entry);
1145 }
1146 kfree(w);
1147}
1148
1149static void mptcp_pm_free_addr_entry(struct mptcp_pm_addr_entry *entry)
1150{
1151 struct addr_entry_release_work *w;
1152
1153 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1154 if (w) {
1155 INIT_RCU_WORK(&w->rwork, mptcp_pm_release_addr_entry);
1156 w->entry = entry;
1157 queue_rcu_work(system_wq, &w->rwork);
1158 }
1159}
1160
Paolo Abeni01cacb02020-03-27 14:48:51 -07001161static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info)
1162{
1163 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1164 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1165 struct mptcp_pm_addr_entry addr, *entry;
Geliang Tang72603d22021-02-01 15:09:06 -08001166 unsigned int addr_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001167 int ret;
1168
1169 ret = mptcp_pm_parse_addr(attr, info, false, &addr);
1170 if (ret < 0)
1171 return ret;
1172
1173 spin_lock_bh(&pernet->lock);
1174 entry = __lookup_addr_by_id(pernet, addr.addr.id);
1175 if (!entry) {
1176 GENL_SET_ERR_MSG(info, "address not found");
Geliang Tangb6c08382020-09-24 08:29:54 +08001177 spin_unlock_bh(&pernet->lock);
1178 return -EINVAL;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001179 }
Geliang Tang72603d22021-02-01 15:09:06 -08001180 if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
1181 addr_max = pernet->add_addr_signal_max;
1182 WRITE_ONCE(pernet->add_addr_signal_max, addr_max - 1);
1183 }
1184 if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) {
1185 addr_max = pernet->local_addr_max;
1186 WRITE_ONCE(pernet->local_addr_max, addr_max - 1);
1187 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001188
1189 pernet->addrs--;
1190 list_del_rcu(&entry->list);
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001191 __clear_bit(entry->addr.id, pernet->id_bitmap);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001192 spin_unlock_bh(&pernet->lock);
Geliang Tangb6c08382020-09-24 08:29:54 +08001193
1194 mptcp_nl_remove_subflow_and_signal_addr(sock_net(skb->sk), &entry->addr);
Geliang Tang1729cf12021-02-01 15:09:12 -08001195 mptcp_pm_free_addr_entry(entry);
Geliang Tangb6c08382020-09-24 08:29:54 +08001196
Paolo Abeni01cacb02020-03-27 14:48:51 -07001197 return ret;
1198}
1199
Geliang Tang06faa222021-03-12 17:16:17 -08001200static void mptcp_pm_remove_addrs_and_subflows(struct mptcp_sock *msk,
1201 struct list_head *rm_list)
1202{
1203 struct mptcp_rm_list alist = { .nr = 0 }, slist = { .nr = 0 };
1204 struct mptcp_pm_addr_entry *entry;
1205
1206 list_for_each_entry(entry, rm_list, list) {
1207 if (lookup_subflow_by_saddr(&msk->conn_list, &entry->addr) &&
1208 alist.nr < MPTCP_RM_IDS_MAX &&
1209 slist.nr < MPTCP_RM_IDS_MAX) {
1210 alist.ids[alist.nr++] = entry->addr.id;
1211 slist.ids[slist.nr++] = entry->addr.id;
1212 } else if (remove_anno_list_by_saddr(msk, &entry->addr) &&
1213 alist.nr < MPTCP_RM_IDS_MAX) {
1214 alist.ids[alist.nr++] = entry->addr.id;
1215 }
1216 }
1217
1218 if (alist.nr) {
1219 spin_lock_bh(&msk->pm.lock);
1220 mptcp_pm_remove_addr(msk, &alist);
1221 spin_unlock_bh(&msk->pm.lock);
1222 }
1223 if (slist.nr)
1224 mptcp_pm_remove_subflow(msk, &slist);
1225}
1226
1227static void mptcp_nl_remove_addrs_list(struct net *net,
1228 struct list_head *rm_list)
1229{
1230 long s_slot = 0, s_num = 0;
1231 struct mptcp_sock *msk;
1232
1233 if (list_empty(rm_list))
1234 return;
1235
1236 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1237 struct sock *sk = (struct sock *)msk;
1238
1239 lock_sock(sk);
1240 mptcp_pm_remove_addrs_and_subflows(msk, rm_list);
1241 release_sock(sk);
1242
1243 sock_put(sk);
1244 cond_resched();
1245 }
1246}
1247
Geliang Tang0e4a3e62021-03-12 17:16:18 -08001248static void __flush_addrs(struct list_head *list)
Paolo Abeni01cacb02020-03-27 14:48:51 -07001249{
Geliang Tang141694d2020-12-10 14:24:59 -08001250 while (!list_empty(list)) {
Paolo Abeni01cacb02020-03-27 14:48:51 -07001251 struct mptcp_pm_addr_entry *cur;
1252
Geliang Tang141694d2020-12-10 14:24:59 -08001253 cur = list_entry(list->next,
Paolo Abeni01cacb02020-03-27 14:48:51 -07001254 struct mptcp_pm_addr_entry, list);
1255 list_del_rcu(&cur->list);
Geliang Tang1729cf12021-02-01 15:09:12 -08001256 mptcp_pm_free_addr_entry(cur);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001257 }
1258}
1259
1260static void __reset_counters(struct pm_nl_pernet *pernet)
1261{
Geliang Tang72603d22021-02-01 15:09:06 -08001262 WRITE_ONCE(pernet->add_addr_signal_max, 0);
1263 WRITE_ONCE(pernet->add_addr_accept_max, 0);
1264 WRITE_ONCE(pernet->local_addr_max, 0);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001265 pernet->addrs = 0;
1266}
1267
1268static int mptcp_nl_cmd_flush_addrs(struct sk_buff *skb, struct genl_info *info)
1269{
1270 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
Geliang Tang141694d2020-12-10 14:24:59 -08001271 LIST_HEAD(free_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001272
1273 spin_lock_bh(&pernet->lock);
Geliang Tang141694d2020-12-10 14:24:59 -08001274 list_splice_init(&pernet->local_addr_list, &free_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001275 __reset_counters(pernet);
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001276 pernet->next_id = 1;
1277 bitmap_zero(pernet->id_bitmap, MAX_ADDR_ID + 1);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001278 spin_unlock_bh(&pernet->lock);
Geliang Tang0e4a3e62021-03-12 17:16:18 -08001279 mptcp_nl_remove_addrs_list(sock_net(skb->sk), &free_list);
1280 __flush_addrs(&free_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001281 return 0;
1282}
1283
1284static int mptcp_nl_fill_addr(struct sk_buff *skb,
1285 struct mptcp_pm_addr_entry *entry)
1286{
1287 struct mptcp_addr_info *addr = &entry->addr;
1288 struct nlattr *attr;
1289
1290 attr = nla_nest_start(skb, MPTCP_PM_ATTR_ADDR);
1291 if (!attr)
1292 return -EMSGSIZE;
1293
1294 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_FAMILY, addr->family))
1295 goto nla_put_failure;
Geliang Tanga77e9172021-02-01 15:09:17 -08001296 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_PORT, ntohs(addr->port)))
1297 goto nla_put_failure;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001298 if (nla_put_u8(skb, MPTCP_PM_ADDR_ATTR_ID, addr->id))
1299 goto nla_put_failure;
Paolo Abenief0da3b2020-09-14 10:01:15 +02001300 if (nla_put_u32(skb, MPTCP_PM_ADDR_ATTR_FLAGS, entry->addr.flags))
Paolo Abeni01cacb02020-03-27 14:48:51 -07001301 goto nla_put_failure;
Paolo Abenief0da3b2020-09-14 10:01:15 +02001302 if (entry->addr.ifindex &&
1303 nla_put_s32(skb, MPTCP_PM_ADDR_ATTR_IF_IDX, entry->addr.ifindex))
Paolo Abeni01cacb02020-03-27 14:48:51 -07001304 goto nla_put_failure;
1305
Bo YUb4e0f9a2020-04-23 10:10:03 +08001306 if (addr->family == AF_INET &&
1307 nla_put_in_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR4,
1308 addr->addr.s_addr))
1309 goto nla_put_failure;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001310#if IS_ENABLED(CONFIG_MPTCP_IPV6)
Bo YUb4e0f9a2020-04-23 10:10:03 +08001311 else if (addr->family == AF_INET6 &&
1312 nla_put_in6_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR6, &addr->addr6))
1313 goto nla_put_failure;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001314#endif
1315 nla_nest_end(skb, attr);
1316 return 0;
1317
1318nla_put_failure:
1319 nla_nest_cancel(skb, attr);
1320 return -EMSGSIZE;
1321}
1322
1323static int mptcp_nl_cmd_get_addr(struct sk_buff *skb, struct genl_info *info)
1324{
1325 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1326 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1327 struct mptcp_pm_addr_entry addr, *entry;
1328 struct sk_buff *msg;
1329 void *reply;
1330 int ret;
1331
1332 ret = mptcp_pm_parse_addr(attr, info, false, &addr);
1333 if (ret < 0)
1334 return ret;
1335
1336 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1337 if (!msg)
1338 return -ENOMEM;
1339
1340 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1341 info->genlhdr->cmd);
1342 if (!reply) {
1343 GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1344 ret = -EMSGSIZE;
1345 goto fail;
1346 }
1347
1348 spin_lock_bh(&pernet->lock);
1349 entry = __lookup_addr_by_id(pernet, addr.addr.id);
1350 if (!entry) {
1351 GENL_SET_ERR_MSG(info, "address not found");
1352 ret = -EINVAL;
1353 goto unlock_fail;
1354 }
1355
1356 ret = mptcp_nl_fill_addr(msg, entry);
1357 if (ret)
1358 goto unlock_fail;
1359
1360 genlmsg_end(msg, reply);
1361 ret = genlmsg_reply(msg, info);
1362 spin_unlock_bh(&pernet->lock);
1363 return ret;
1364
1365unlock_fail:
1366 spin_unlock_bh(&pernet->lock);
1367
1368fail:
1369 nlmsg_free(msg);
1370 return ret;
1371}
1372
1373static int mptcp_nl_cmd_dump_addrs(struct sk_buff *msg,
1374 struct netlink_callback *cb)
1375{
1376 struct net *net = sock_net(msg->sk);
1377 struct mptcp_pm_addr_entry *entry;
1378 struct pm_nl_pernet *pernet;
1379 int id = cb->args[0];
1380 void *hdr;
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001381 int i;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001382
1383 pernet = net_generic(net, pm_nl_pernet_id);
1384
1385 spin_lock_bh(&pernet->lock);
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001386 for (i = id; i < MAX_ADDR_ID + 1; i++) {
1387 if (test_bit(i, pernet->id_bitmap)) {
1388 entry = __lookup_addr_by_id(pernet, i);
1389 if (!entry)
1390 break;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001391
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001392 if (entry->addr.id <= id)
1393 continue;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001394
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001395 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid,
1396 cb->nlh->nlmsg_seq, &mptcp_genl_family,
1397 NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR);
1398 if (!hdr)
1399 break;
1400
1401 if (mptcp_nl_fill_addr(msg, entry) < 0) {
1402 genlmsg_cancel(msg, hdr);
1403 break;
1404 }
1405
1406 id = entry->addr.id;
1407 genlmsg_end(msg, hdr);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001408 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001409 }
1410 spin_unlock_bh(&pernet->lock);
1411
1412 cb->args[0] = id;
1413 return msg->len;
1414}
1415
1416static int parse_limit(struct genl_info *info, int id, unsigned int *limit)
1417{
1418 struct nlattr *attr = info->attrs[id];
1419
1420 if (!attr)
1421 return 0;
1422
1423 *limit = nla_get_u32(attr);
1424 if (*limit > MPTCP_PM_ADDR_MAX) {
1425 GENL_SET_ERR_MSG(info, "limit greater than maximum");
1426 return -EINVAL;
1427 }
1428 return 0;
1429}
1430
1431static int
1432mptcp_nl_cmd_set_limits(struct sk_buff *skb, struct genl_info *info)
1433{
1434 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1435 unsigned int rcv_addrs, subflows;
1436 int ret;
1437
1438 spin_lock_bh(&pernet->lock);
1439 rcv_addrs = pernet->add_addr_accept_max;
1440 ret = parse_limit(info, MPTCP_PM_ATTR_RCV_ADD_ADDRS, &rcv_addrs);
1441 if (ret)
1442 goto unlock;
1443
1444 subflows = pernet->subflows_max;
1445 ret = parse_limit(info, MPTCP_PM_ATTR_SUBFLOWS, &subflows);
1446 if (ret)
1447 goto unlock;
1448
1449 WRITE_ONCE(pernet->add_addr_accept_max, rcv_addrs);
1450 WRITE_ONCE(pernet->subflows_max, subflows);
1451
1452unlock:
1453 spin_unlock_bh(&pernet->lock);
1454 return ret;
1455}
1456
1457static int
1458mptcp_nl_cmd_get_limits(struct sk_buff *skb, struct genl_info *info)
1459{
1460 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1461 struct sk_buff *msg;
1462 void *reply;
1463
1464 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1465 if (!msg)
1466 return -ENOMEM;
1467
1468 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1469 MPTCP_PM_CMD_GET_LIMITS);
1470 if (!reply)
1471 goto fail;
1472
1473 if (nla_put_u32(msg, MPTCP_PM_ATTR_RCV_ADD_ADDRS,
1474 READ_ONCE(pernet->add_addr_accept_max)))
1475 goto fail;
1476
1477 if (nla_put_u32(msg, MPTCP_PM_ATTR_SUBFLOWS,
1478 READ_ONCE(pernet->subflows_max)))
1479 goto fail;
1480
1481 genlmsg_end(msg, reply);
1482 return genlmsg_reply(msg, info);
1483
1484fail:
1485 GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1486 nlmsg_free(msg);
1487 return -EMSGSIZE;
1488}
1489
Geliang Tang0f9f6962021-01-08 16:47:59 -08001490static int mptcp_nl_addr_backup(struct net *net,
1491 struct mptcp_addr_info *addr,
1492 u8 bkup)
1493{
1494 long s_slot = 0, s_num = 0;
1495 struct mptcp_sock *msk;
1496 int ret = -EINVAL;
1497
1498 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1499 struct sock *sk = (struct sock *)msk;
1500
1501 if (list_empty(&msk->conn_list))
1502 goto next;
1503
1504 lock_sock(sk);
1505 spin_lock_bh(&msk->pm.lock);
1506 ret = mptcp_pm_nl_mp_prio_send_ack(msk, addr, bkup);
1507 spin_unlock_bh(&msk->pm.lock);
1508 release_sock(sk);
1509
1510next:
1511 sock_put(sk);
1512 cond_resched();
1513 }
1514
1515 return ret;
1516}
1517
1518static int mptcp_nl_cmd_set_flags(struct sk_buff *skb, struct genl_info *info)
1519{
1520 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1521 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1522 struct mptcp_pm_addr_entry addr, *entry;
1523 struct net *net = sock_net(skb->sk);
1524 u8 bkup = 0;
1525 int ret;
1526
1527 ret = mptcp_pm_parse_addr(attr, info, true, &addr);
1528 if (ret < 0)
1529 return ret;
1530
1531 if (addr.addr.flags & MPTCP_PM_ADDR_FLAG_BACKUP)
1532 bkup = 1;
1533
1534 list_for_each_entry(entry, &pernet->local_addr_list, list) {
1535 if (addresses_equal(&entry->addr, &addr.addr, true)) {
1536 ret = mptcp_nl_addr_backup(net, &entry->addr, bkup);
1537 if (ret)
1538 return ret;
1539
1540 if (bkup)
1541 entry->addr.flags |= MPTCP_PM_ADDR_FLAG_BACKUP;
1542 else
1543 entry->addr.flags &= ~MPTCP_PM_ADDR_FLAG_BACKUP;
1544 }
1545 }
1546
1547 return 0;
1548}
1549
Florian Westphalb911c972021-02-12 16:00:01 -08001550static void mptcp_nl_mcast_send(struct net *net, struct sk_buff *nlskb, gfp_t gfp)
1551{
1552 genlmsg_multicast_netns(&mptcp_genl_family, net,
1553 nlskb, 0, MPTCP_PM_EV_GRP_OFFSET, gfp);
1554}
1555
1556static int mptcp_event_add_subflow(struct sk_buff *skb, const struct sock *ssk)
1557{
1558 const struct inet_sock *issk = inet_sk(ssk);
1559 const struct mptcp_subflow_context *sf;
1560
1561 if (nla_put_u16(skb, MPTCP_ATTR_FAMILY, ssk->sk_family))
1562 return -EMSGSIZE;
1563
1564 switch (ssk->sk_family) {
1565 case AF_INET:
1566 if (nla_put_in_addr(skb, MPTCP_ATTR_SADDR4, issk->inet_saddr))
1567 return -EMSGSIZE;
1568 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, issk->inet_daddr))
1569 return -EMSGSIZE;
1570 break;
1571#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1572 case AF_INET6: {
1573 const struct ipv6_pinfo *np = inet6_sk(ssk);
1574
1575 if (nla_put_in6_addr(skb, MPTCP_ATTR_SADDR6, &np->saddr))
1576 return -EMSGSIZE;
1577 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &ssk->sk_v6_daddr))
1578 return -EMSGSIZE;
1579 break;
1580 }
1581#endif
1582 default:
1583 WARN_ON_ONCE(1);
1584 return -EMSGSIZE;
1585 }
1586
1587 if (nla_put_be16(skb, MPTCP_ATTR_SPORT, issk->inet_sport))
1588 return -EMSGSIZE;
1589 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, issk->inet_dport))
1590 return -EMSGSIZE;
1591
1592 sf = mptcp_subflow_ctx(ssk);
1593 if (WARN_ON_ONCE(!sf))
1594 return -EINVAL;
1595
1596 if (nla_put_u8(skb, MPTCP_ATTR_LOC_ID, sf->local_id))
1597 return -EMSGSIZE;
1598
1599 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, sf->remote_id))
1600 return -EMSGSIZE;
1601
1602 return 0;
1603}
1604
1605static int mptcp_event_put_token_and_ssk(struct sk_buff *skb,
1606 const struct mptcp_sock *msk,
1607 const struct sock *ssk)
1608{
1609 const struct sock *sk = (const struct sock *)msk;
1610 const struct mptcp_subflow_context *sf;
1611 u8 sk_err;
1612
1613 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1614 return -EMSGSIZE;
1615
1616 if (mptcp_event_add_subflow(skb, ssk))
1617 return -EMSGSIZE;
1618
1619 sf = mptcp_subflow_ctx(ssk);
1620 if (WARN_ON_ONCE(!sf))
1621 return -EINVAL;
1622
1623 if (nla_put_u8(skb, MPTCP_ATTR_BACKUP, sf->backup))
1624 return -EMSGSIZE;
1625
1626 if (ssk->sk_bound_dev_if &&
1627 nla_put_s32(skb, MPTCP_ATTR_IF_IDX, ssk->sk_bound_dev_if))
1628 return -EMSGSIZE;
1629
1630 sk_err = ssk->sk_err;
1631 if (sk_err && sk->sk_state == TCP_ESTABLISHED &&
1632 nla_put_u8(skb, MPTCP_ATTR_ERROR, sk_err))
1633 return -EMSGSIZE;
1634
1635 return 0;
1636}
1637
1638static int mptcp_event_sub_established(struct sk_buff *skb,
1639 const struct mptcp_sock *msk,
1640 const struct sock *ssk)
1641{
1642 return mptcp_event_put_token_and_ssk(skb, msk, ssk);
1643}
1644
1645static int mptcp_event_sub_closed(struct sk_buff *skb,
1646 const struct mptcp_sock *msk,
1647 const struct sock *ssk)
1648{
1649 if (mptcp_event_put_token_and_ssk(skb, msk, ssk))
1650 return -EMSGSIZE;
1651
1652 return 0;
1653}
1654
1655static int mptcp_event_created(struct sk_buff *skb,
1656 const struct mptcp_sock *msk,
1657 const struct sock *ssk)
1658{
1659 int err = nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token);
1660
1661 if (err)
1662 return err;
1663
1664 return mptcp_event_add_subflow(skb, ssk);
1665}
1666
1667void mptcp_event_addr_removed(const struct mptcp_sock *msk, uint8_t id)
1668{
1669 struct net *net = sock_net((const struct sock *)msk);
1670 struct nlmsghdr *nlh;
1671 struct sk_buff *skb;
1672
1673 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
1674 return;
1675
1676 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1677 if (!skb)
1678 return;
1679
1680 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, MPTCP_EVENT_REMOVED);
1681 if (!nlh)
1682 goto nla_put_failure;
1683
1684 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1685 goto nla_put_failure;
1686
1687 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, id))
1688 goto nla_put_failure;
1689
1690 genlmsg_end(skb, nlh);
1691 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC);
1692 return;
1693
1694nla_put_failure:
1695 kfree_skb(skb);
1696}
1697
1698void mptcp_event_addr_announced(const struct mptcp_sock *msk,
1699 const struct mptcp_addr_info *info)
1700{
1701 struct net *net = sock_net((const struct sock *)msk);
1702 struct nlmsghdr *nlh;
1703 struct sk_buff *skb;
1704
1705 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
1706 return;
1707
1708 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1709 if (!skb)
1710 return;
1711
1712 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0,
1713 MPTCP_EVENT_ANNOUNCED);
1714 if (!nlh)
1715 goto nla_put_failure;
1716
1717 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1718 goto nla_put_failure;
1719
1720 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, info->id))
1721 goto nla_put_failure;
1722
1723 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, info->port))
1724 goto nla_put_failure;
1725
1726 switch (info->family) {
1727 case AF_INET:
1728 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, info->addr.s_addr))
1729 goto nla_put_failure;
1730 break;
1731#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1732 case AF_INET6:
1733 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &info->addr6))
1734 goto nla_put_failure;
1735 break;
1736#endif
1737 default:
1738 WARN_ON_ONCE(1);
1739 goto nla_put_failure;
1740 }
1741
1742 genlmsg_end(skb, nlh);
1743 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC);
1744 return;
1745
1746nla_put_failure:
1747 kfree_skb(skb);
1748}
1749
1750void mptcp_event(enum mptcp_event_type type, const struct mptcp_sock *msk,
1751 const struct sock *ssk, gfp_t gfp)
1752{
1753 struct net *net = sock_net((const struct sock *)msk);
1754 struct nlmsghdr *nlh;
1755 struct sk_buff *skb;
1756
1757 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
1758 return;
1759
1760 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
1761 if (!skb)
1762 return;
1763
1764 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, type);
1765 if (!nlh)
1766 goto nla_put_failure;
1767
1768 switch (type) {
1769 case MPTCP_EVENT_UNSPEC:
1770 WARN_ON_ONCE(1);
1771 break;
1772 case MPTCP_EVENT_CREATED:
1773 case MPTCP_EVENT_ESTABLISHED:
1774 if (mptcp_event_created(skb, msk, ssk) < 0)
1775 goto nla_put_failure;
1776 break;
1777 case MPTCP_EVENT_CLOSED:
1778 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token) < 0)
1779 goto nla_put_failure;
1780 break;
1781 case MPTCP_EVENT_ANNOUNCED:
1782 case MPTCP_EVENT_REMOVED:
1783 /* call mptcp_event_addr_announced()/removed instead */
1784 WARN_ON_ONCE(1);
1785 break;
1786 case MPTCP_EVENT_SUB_ESTABLISHED:
1787 case MPTCP_EVENT_SUB_PRIORITY:
1788 if (mptcp_event_sub_established(skb, msk, ssk) < 0)
1789 goto nla_put_failure;
1790 break;
1791 case MPTCP_EVENT_SUB_CLOSED:
1792 if (mptcp_event_sub_closed(skb, msk, ssk) < 0)
1793 goto nla_put_failure;
1794 break;
1795 }
1796
1797 genlmsg_end(skb, nlh);
1798 mptcp_nl_mcast_send(net, skb, gfp);
1799 return;
1800
1801nla_put_failure:
1802 kfree_skb(skb);
1803}
1804
Rikard Falkeborn674d3ab2020-10-05 01:44:16 +02001805static const struct genl_small_ops mptcp_pm_ops[] = {
Paolo Abeni01cacb02020-03-27 14:48:51 -07001806 {
1807 .cmd = MPTCP_PM_CMD_ADD_ADDR,
1808 .doit = mptcp_nl_cmd_add_addr,
1809 .flags = GENL_ADMIN_PERM,
1810 },
1811 {
1812 .cmd = MPTCP_PM_CMD_DEL_ADDR,
1813 .doit = mptcp_nl_cmd_del_addr,
1814 .flags = GENL_ADMIN_PERM,
1815 },
1816 {
1817 .cmd = MPTCP_PM_CMD_FLUSH_ADDRS,
1818 .doit = mptcp_nl_cmd_flush_addrs,
1819 .flags = GENL_ADMIN_PERM,
1820 },
1821 {
1822 .cmd = MPTCP_PM_CMD_GET_ADDR,
1823 .doit = mptcp_nl_cmd_get_addr,
1824 .dumpit = mptcp_nl_cmd_dump_addrs,
1825 },
1826 {
1827 .cmd = MPTCP_PM_CMD_SET_LIMITS,
1828 .doit = mptcp_nl_cmd_set_limits,
1829 .flags = GENL_ADMIN_PERM,
1830 },
1831 {
1832 .cmd = MPTCP_PM_CMD_GET_LIMITS,
1833 .doit = mptcp_nl_cmd_get_limits,
1834 },
Geliang Tang0f9f6962021-01-08 16:47:59 -08001835 {
1836 .cmd = MPTCP_PM_CMD_SET_FLAGS,
1837 .doit = mptcp_nl_cmd_set_flags,
1838 .flags = GENL_ADMIN_PERM,
1839 },
Paolo Abeni01cacb02020-03-27 14:48:51 -07001840};
1841
1842static struct genl_family mptcp_genl_family __ro_after_init = {
1843 .name = MPTCP_PM_NAME,
1844 .version = MPTCP_PM_VER,
1845 .maxattr = MPTCP_PM_ATTR_MAX,
1846 .policy = mptcp_pm_policy,
1847 .netnsok = true,
1848 .module = THIS_MODULE,
Jakub Kicinski66a9b922020-10-02 14:49:54 -07001849 .small_ops = mptcp_pm_ops,
1850 .n_small_ops = ARRAY_SIZE(mptcp_pm_ops),
Paolo Abeni01cacb02020-03-27 14:48:51 -07001851 .mcgrps = mptcp_pm_mcgrps,
1852 .n_mcgrps = ARRAY_SIZE(mptcp_pm_mcgrps),
1853};
1854
1855static int __net_init pm_nl_init_net(struct net *net)
1856{
1857 struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id);
1858
1859 INIT_LIST_HEAD_RCU(&pernet->local_addr_list);
1860 __reset_counters(pernet);
1861 pernet->next_id = 1;
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001862 bitmap_zero(pernet->id_bitmap, MAX_ADDR_ID + 1);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001863 spin_lock_init(&pernet->lock);
1864 return 0;
1865}
1866
1867static void __net_exit pm_nl_exit_net(struct list_head *net_list)
1868{
1869 struct net *net;
1870
1871 list_for_each_entry(net, net_list, exit_list) {
Geliang Tang141694d2020-12-10 14:24:59 -08001872 struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id);
1873
Paolo Abeni01cacb02020-03-27 14:48:51 -07001874 /* net is removed from namespace list, can't race with
1875 * other modifiers
1876 */
Geliang Tang0e4a3e62021-03-12 17:16:18 -08001877 __flush_addrs(&pernet->local_addr_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001878 }
1879}
1880
1881static struct pernet_operations mptcp_pm_pernet_ops = {
1882 .init = pm_nl_init_net,
1883 .exit_batch = pm_nl_exit_net,
1884 .id = &pm_nl_pernet_id,
1885 .size = sizeof(struct pm_nl_pernet),
1886};
1887
Paolo Abenid39dcec2020-06-26 19:29:59 +02001888void __init mptcp_pm_nl_init(void)
Paolo Abeni01cacb02020-03-27 14:48:51 -07001889{
1890 if (register_pernet_subsys(&mptcp_pm_pernet_ops) < 0)
1891 panic("Failed to register MPTCP PM pernet subsystem.\n");
1892
1893 if (genl_register_family(&mptcp_genl_family))
1894 panic("Failed to register MPTCP PM netlink family\n");
1895}