blob: 769a05d836da46c2eb3050ae595d2e5960b0a7a8 [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 Tang0f5c9e32020-12-09 15:51:24 -0800311 mptcp_pm_announce_addr(msk, &entry->addr, false, entry->addr.port);
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 Tang0f5c9e32020-12-09 15:51:24 -0800420 mptcp_pm_announce_addr(msk, &local->addr, false, local->addr.port);
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;
Geliang Tang0f5c9e32020-12-09 15:51:24 -0800471 bool use_port = false;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700472
Geliang Tanga914e582021-02-01 15:09:07 -0800473 add_addr_accept_max = mptcp_pm_get_add_addr_accept_max(msk);
474 subflows_max = mptcp_pm_get_subflows_max(msk);
475
Paolo Abeni01cacb02020-03-27 14:48:51 -0700476 pr_debug("accepted %d:%d remote family %d",
Geliang Tanga914e582021-02-01 15:09:07 -0800477 msk->pm.add_addr_accepted, add_addr_accept_max,
Paolo Abeni01cacb02020-03-27 14:48:51 -0700478 msk->pm.remote.family);
479 msk->pm.add_addr_accepted++;
480 msk->pm.subflows++;
Geliang Tanga914e582021-02-01 15:09:07 -0800481 if (msk->pm.add_addr_accepted >= add_addr_accept_max ||
482 msk->pm.subflows >= subflows_max)
Paolo Abeni01cacb02020-03-27 14:48:51 -0700483 WRITE_ONCE(msk->pm.accept_addr, false);
484
485 /* connect to the specified remote address, using whatever
486 * local address the routing configuration will pick.
487 */
488 remote = msk->pm.remote;
489 if (!remote.port)
490 remote.port = sk->sk_dport;
Geliang Tang0f5c9e32020-12-09 15:51:24 -0800491 else
492 use_port = true;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700493 memset(&local, 0, sizeof(local));
494 local.family = remote.family;
495
496 spin_unlock_bh(&msk->pm.lock);
Geliang Tang432d9e72020-12-09 15:51:28 -0800497 __mptcp_subflow_connect(sk, &local, &remote);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700498 spin_lock_bh(&msk->pm.lock);
Geliang Tang6a6c05a2020-09-24 08:29:50 +0800499
Geliang Tang0f5c9e32020-12-09 15:51:24 -0800500 mptcp_pm_announce_addr(msk, &remote, true, use_port);
Geliang Tang84dfe362020-11-19 11:46:00 -0800501 mptcp_pm_nl_add_addr_send_ack(msk);
502}
503
Florian Westphale9801432021-02-12 15:59:54 -0800504static void mptcp_pm_nl_add_addr_send_ack(struct mptcp_sock *msk)
Geliang Tang84dfe362020-11-19 11:46:00 -0800505{
506 struct mptcp_subflow_context *subflow;
507
Florian Westphal3abc05d2021-02-04 15:23:30 -0800508 msk_owned_by_me(msk);
509 lockdep_assert_held(&msk->pm.lock);
510
Geliang Tangb5a7acd2021-02-01 15:09:09 -0800511 if (!mptcp_pm_should_add_signal(msk))
Geliang Tang84dfe362020-11-19 11:46:00 -0800512 return;
513
514 __mptcp_flush_join_list(msk);
515 subflow = list_first_entry_or_null(&msk->conn_list, typeof(*subflow), node);
516 if (subflow) {
517 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
518 u8 add_addr;
519
520 spin_unlock_bh(&msk->pm.lock);
Geliang Tangb5a7acd2021-02-01 15:09:09 -0800521 pr_debug("send ack for add_addr%s%s",
522 mptcp_pm_should_add_signal_ipv6(msk) ? " [ipv6]" : "",
523 mptcp_pm_should_add_signal_port(msk) ? " [port]" : "");
Geliang Tangfbe0f872020-12-09 15:51:23 -0800524
Geliang Tang84dfe362020-11-19 11:46:00 -0800525 lock_sock(ssk);
526 tcp_send_ack(ssk);
527 release_sock(ssk);
528 spin_lock_bh(&msk->pm.lock);
529
Geliang Tang13ad9f02020-12-09 15:51:27 -0800530 add_addr = READ_ONCE(msk->pm.addr_signal);
Geliang Tangfbe0f872020-12-09 15:51:23 -0800531 if (mptcp_pm_should_add_signal_ipv6(msk))
532 add_addr &= ~BIT(MPTCP_ADD_ADDR_IPV6);
533 if (mptcp_pm_should_add_signal_port(msk))
534 add_addr &= ~BIT(MPTCP_ADD_ADDR_PORT);
Geliang Tang13ad9f02020-12-09 15:51:27 -0800535 WRITE_ONCE(msk->pm.addr_signal, add_addr);
Geliang Tang84dfe362020-11-19 11:46:00 -0800536 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700537}
538
Geliang Tang06706542021-01-08 16:47:57 -0800539int mptcp_pm_nl_mp_prio_send_ack(struct mptcp_sock *msk,
540 struct mptcp_addr_info *addr,
541 u8 bkup)
542{
543 struct mptcp_subflow_context *subflow;
544
545 pr_debug("bkup=%d", bkup);
546
547 mptcp_for_each_subflow(msk, subflow) {
548 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
Geliang Tang0be2ac22021-01-08 16:48:01 -0800549 struct sock *sk = (struct sock *)msk;
Geliang Tang06706542021-01-08 16:47:57 -0800550 struct mptcp_addr_info local;
551
552 local_address((struct sock_common *)ssk, &local);
553 if (!addresses_equal(&local, addr, addr->port))
554 continue;
555
556 subflow->backup = bkup;
557 subflow->send_mp_prio = 1;
558 subflow->request_bkup = bkup;
Geliang Tang0be2ac22021-01-08 16:48:01 -0800559 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIOTX);
Geliang Tang06706542021-01-08 16:47:57 -0800560
561 spin_unlock_bh(&msk->pm.lock);
562 pr_debug("send ack for mp_prio");
563 lock_sock(ssk);
564 tcp_send_ack(ssk);
565 release_sock(ssk);
566 spin_lock_bh(&msk->pm.lock);
567
568 return 0;
569 }
570
571 return -EINVAL;
572}
573
Florian Westphale9801432021-02-12 15:59:54 -0800574static void mptcp_pm_nl_rm_addr_received(struct mptcp_sock *msk)
Geliang Tangd0876b22020-09-24 08:29:49 +0800575{
576 struct mptcp_subflow_context *subflow, *tmp;
577 struct sock *sk = (struct sock *)msk;
Geliang Tangd0b698c2021-03-12 17:16:15 -0800578 u8 i;
Geliang Tangd0876b22020-09-24 08:29:49 +0800579
Geliang Tangd0b698c2021-03-12 17:16:15 -0800580 pr_debug("address rm_list_nr %d", msk->pm.rm_list_rx.nr);
Geliang Tangd0876b22020-09-24 08:29:49 +0800581
Florian Westphal3abc05d2021-02-04 15:23:30 -0800582 msk_owned_by_me(msk);
583
Geliang Tangd0b698c2021-03-12 17:16:15 -0800584 if (!msk->pm.rm_list_rx.nr)
Geliang Tangd0876b22020-09-24 08:29:49 +0800585 return;
586
587 if (list_empty(&msk->conn_list))
588 return;
589
Geliang Tangd0b698c2021-03-12 17:16:15 -0800590 for (i = 0; i < msk->pm.rm_list_rx.nr; i++) {
591 list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
592 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
593 int how = RCV_SHUTDOWN | SEND_SHUTDOWN;
Geliang Tangd0876b22020-09-24 08:29:49 +0800594
Geliang Tangd0b698c2021-03-12 17:16:15 -0800595 if (msk->pm.rm_list_rx.ids[i] != subflow->remote_id)
596 continue;
Geliang Tangd0876b22020-09-24 08:29:49 +0800597
Geliang Tangd0b698c2021-03-12 17:16:15 -0800598 pr_debug(" -> address rm_list_ids[%d]=%u", i, msk->pm.rm_list_rx.ids[i]);
599 spin_unlock_bh(&msk->pm.lock);
600 mptcp_subflow_shutdown(sk, ssk, how);
601 mptcp_close_ssk(sk, ssk, subflow);
602 spin_lock_bh(&msk->pm.lock);
Geliang Tangd0876b22020-09-24 08:29:49 +0800603
Geliang Tangd0b698c2021-03-12 17:16:15 -0800604 msk->pm.add_addr_accepted--;
605 msk->pm.subflows--;
606 WRITE_ONCE(msk->pm.accept_addr, true);
Geliang Tangd0876b22020-09-24 08:29:49 +0800607
Geliang Tangd0b698c2021-03-12 17:16:15 -0800608 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMADDR);
Geliang Tang7a7e52e2020-09-24 08:29:56 +0800609
Geliang Tangd0b698c2021-03-12 17:16:15 -0800610 break;
611 }
Geliang Tangd0876b22020-09-24 08:29:49 +0800612 }
613}
614
Florian Westphale9801432021-02-12 15:59:54 -0800615void mptcp_pm_nl_work(struct mptcp_sock *msk)
616{
617 struct mptcp_pm_data *pm = &msk->pm;
618
619 msk_owned_by_me(msk);
620
621 spin_lock_bh(&msk->pm.lock);
622
623 pr_debug("msk=%p status=%x", msk, pm->status);
624 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_RECEIVED)) {
625 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_RECEIVED);
626 mptcp_pm_nl_add_addr_received(msk);
627 }
628 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_SEND_ACK)) {
629 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_SEND_ACK);
630 mptcp_pm_nl_add_addr_send_ack(msk);
631 }
632 if (pm->status & BIT(MPTCP_PM_RM_ADDR_RECEIVED)) {
633 pm->status &= ~BIT(MPTCP_PM_RM_ADDR_RECEIVED);
634 mptcp_pm_nl_rm_addr_received(msk);
635 }
636 if (pm->status & BIT(MPTCP_PM_ESTABLISHED)) {
637 pm->status &= ~BIT(MPTCP_PM_ESTABLISHED);
638 mptcp_pm_nl_fully_established(msk);
639 }
640 if (pm->status & BIT(MPTCP_PM_SUBFLOW_ESTABLISHED)) {
641 pm->status &= ~BIT(MPTCP_PM_SUBFLOW_ESTABLISHED);
642 mptcp_pm_nl_subflow_established(msk);
643 }
644
645 spin_unlock_bh(&msk->pm.lock);
646}
647
Geliang Tangddd14bb2021-03-12 17:16:16 -0800648void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk,
649 const struct mptcp_rm_list *rm_list)
Geliang Tang0ee42612020-09-24 08:29:55 +0800650{
651 struct mptcp_subflow_context *subflow, *tmp;
652 struct sock *sk = (struct sock *)msk;
Geliang Tangddd14bb2021-03-12 17:16:16 -0800653 u8 i;
Geliang Tang0ee42612020-09-24 08:29:55 +0800654
Geliang Tangddd14bb2021-03-12 17:16:16 -0800655 pr_debug("subflow rm_list_nr %d", rm_list->nr);
Geliang Tang0ee42612020-09-24 08:29:55 +0800656
Florian Westphal3abc05d2021-02-04 15:23:30 -0800657 msk_owned_by_me(msk);
658
Geliang Tangddd14bb2021-03-12 17:16:16 -0800659 if (!rm_list->nr)
Geliang Tang0ee42612020-09-24 08:29:55 +0800660 return;
661
662 if (list_empty(&msk->conn_list))
663 return;
664
Geliang Tangddd14bb2021-03-12 17:16:16 -0800665 for (i = 0; i < rm_list->nr; i++) {
666 list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
667 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
668 int how = RCV_SHUTDOWN | SEND_SHUTDOWN;
Geliang Tang0ee42612020-09-24 08:29:55 +0800669
Geliang Tangddd14bb2021-03-12 17:16:16 -0800670 if (rm_list->ids[i] != subflow->local_id)
671 continue;
Geliang Tang0ee42612020-09-24 08:29:55 +0800672
Geliang Tangddd14bb2021-03-12 17:16:16 -0800673 pr_debug(" -> subflow rm_list_ids[%d]=%u", i, rm_list->ids[i]);
674 spin_unlock_bh(&msk->pm.lock);
675 mptcp_subflow_shutdown(sk, ssk, how);
676 mptcp_close_ssk(sk, ssk, subflow);
677 spin_lock_bh(&msk->pm.lock);
Geliang Tang0ee42612020-09-24 08:29:55 +0800678
Geliang Tangddd14bb2021-03-12 17:16:16 -0800679 msk->pm.local_addr_used--;
680 msk->pm.subflows--;
Geliang Tang0ee42612020-09-24 08:29:55 +0800681
Geliang Tangddd14bb2021-03-12 17:16:16 -0800682 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMSUBFLOW);
Geliang Tang7a7e52e2020-09-24 08:29:56 +0800683
Geliang Tangddd14bb2021-03-12 17:16:16 -0800684 break;
685 }
Geliang Tang0ee42612020-09-24 08:29:55 +0800686 }
687}
688
Paolo Abeni01cacb02020-03-27 14:48:51 -0700689static bool address_use_port(struct mptcp_pm_addr_entry *entry)
690{
Paolo Abenief0da3b2020-09-14 10:01:15 +0200691 return (entry->addr.flags &
Paolo Abeni01cacb02020-03-27 14:48:51 -0700692 (MPTCP_PM_ADDR_FLAG_SIGNAL | MPTCP_PM_ADDR_FLAG_SUBFLOW)) ==
693 MPTCP_PM_ADDR_FLAG_SIGNAL;
694}
695
696static int mptcp_pm_nl_append_new_local_addr(struct pm_nl_pernet *pernet,
697 struct mptcp_pm_addr_entry *entry)
698{
699 struct mptcp_pm_addr_entry *cur;
Geliang Tang72603d22021-02-01 15:09:06 -0800700 unsigned int addr_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700701 int ret = -EINVAL;
702
703 spin_lock_bh(&pernet->lock);
704 /* to keep the code simple, don't do IDR-like allocation for address ID,
705 * just bail when we exceed limits
706 */
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800707 if (pernet->next_id == MAX_ADDR_ID)
708 pernet->next_id = 1;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700709 if (pernet->addrs >= MPTCP_PM_ADDR_MAX)
710 goto out;
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800711 if (test_bit(entry->addr.id, pernet->id_bitmap))
712 goto out;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700713
714 /* do not insert duplicate address, differentiate on port only
715 * singled addresses
716 */
717 list_for_each_entry(cur, &pernet->local_addr_list, list) {
718 if (addresses_equal(&cur->addr, &entry->addr,
719 address_use_port(entry) &&
720 address_use_port(cur)))
721 goto out;
722 }
723
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800724 if (!entry->addr.id) {
725find_next:
726 entry->addr.id = find_next_zero_bit(pernet->id_bitmap,
727 MAX_ADDR_ID + 1,
728 pernet->next_id);
729 if ((!entry->addr.id || entry->addr.id > MAX_ADDR_ID) &&
730 pernet->next_id != 1) {
731 pernet->next_id = 1;
732 goto find_next;
733 }
734 }
735
736 if (!entry->addr.id || entry->addr.id > MAX_ADDR_ID)
737 goto out;
738
739 __set_bit(entry->addr.id, pernet->id_bitmap);
740 if (entry->addr.id > pernet->next_id)
741 pernet->next_id = entry->addr.id;
742
Geliang Tang72603d22021-02-01 15:09:06 -0800743 if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
744 addr_max = pernet->add_addr_signal_max;
745 WRITE_ONCE(pernet->add_addr_signal_max, addr_max + 1);
746 }
747 if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) {
748 addr_max = pernet->local_addr_max;
749 WRITE_ONCE(pernet->local_addr_max, addr_max + 1);
750 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700751
Paolo Abeni01cacb02020-03-27 14:48:51 -0700752 pernet->addrs++;
753 list_add_tail_rcu(&entry->list, &pernet->local_addr_list);
754 ret = entry->addr.id;
755
756out:
757 spin_unlock_bh(&pernet->lock);
758 return ret;
759}
760
Geliang Tang1729cf12021-02-01 15:09:12 -0800761static int mptcp_pm_nl_create_listen_socket(struct sock *sk,
762 struct mptcp_pm_addr_entry *entry)
763{
764 struct sockaddr_storage addr;
765 struct mptcp_sock *msk;
766 struct socket *ssock;
767 int backlog = 1024;
768 int err;
769
770 err = sock_create_kern(sock_net(sk), entry->addr.family,
771 SOCK_STREAM, IPPROTO_MPTCP, &entry->lsk);
772 if (err)
773 return err;
774
775 msk = mptcp_sk(entry->lsk->sk);
776 if (!msk) {
777 err = -EINVAL;
778 goto out;
779 }
780
781 ssock = __mptcp_nmpc_socket(msk);
782 if (!ssock) {
783 err = -EINVAL;
784 goto out;
785 }
786
787 mptcp_info2sockaddr(&entry->addr, &addr, entry->addr.family);
788 err = kernel_bind(ssock, (struct sockaddr *)&addr,
789 sizeof(struct sockaddr_in));
790 if (err) {
791 pr_warn("kernel_bind error, err=%d", err);
792 goto out;
793 }
794
795 err = kernel_listen(ssock, backlog);
796 if (err) {
797 pr_warn("kernel_listen error, err=%d", err);
798 goto out;
799 }
800
801 return 0;
802
803out:
804 sock_release(entry->lsk);
805 return err;
806}
807
Paolo Abeni01cacb02020-03-27 14:48:51 -0700808int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc)
809{
810 struct mptcp_pm_addr_entry *entry;
811 struct mptcp_addr_info skc_local;
812 struct mptcp_addr_info msk_local;
813 struct pm_nl_pernet *pernet;
814 int ret = -1;
815
816 if (WARN_ON_ONCE(!msk))
817 return -1;
818
819 /* The 0 ID mapping is defined by the first subflow, copied into the msk
820 * addr
821 */
822 local_address((struct sock_common *)msk, &msk_local);
Geliang Tang57025812020-09-08 10:49:38 +0800823 local_address((struct sock_common *)skc, &skc_local);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700824 if (addresses_equal(&msk_local, &skc_local, false))
825 return 0;
826
Geliang Tang57025812020-09-08 10:49:38 +0800827 if (address_zero(&skc_local))
828 return 0;
829
Paolo Abeni01cacb02020-03-27 14:48:51 -0700830 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
831
832 rcu_read_lock();
833 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
Geliang Tang60b57bf2021-02-01 15:09:16 -0800834 if (addresses_equal(&entry->addr, &skc_local, entry->addr.port)) {
Paolo Abeni01cacb02020-03-27 14:48:51 -0700835 ret = entry->addr.id;
836 break;
837 }
838 }
839 rcu_read_unlock();
840 if (ret >= 0)
841 return ret;
842
843 /* address not found, add to local list */
Geliang Tangf612eb72020-09-09 11:01:24 +0800844 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700845 if (!entry)
846 return -ENOMEM;
847
Paolo Abeni01cacb02020-03-27 14:48:51 -0700848 entry->addr = skc_local;
Paolo Abenief0da3b2020-09-14 10:01:15 +0200849 entry->addr.ifindex = 0;
850 entry->addr.flags = 0;
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800851 entry->addr.id = 0;
Geliang Tang1729cf12021-02-01 15:09:12 -0800852 entry->addr.port = 0;
853 entry->lsk = NULL;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700854 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
855 if (ret < 0)
856 kfree(entry);
857
858 return ret;
859}
860
861void mptcp_pm_nl_data_init(struct mptcp_sock *msk)
862{
863 struct mptcp_pm_data *pm = &msk->pm;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700864 bool subflows;
865
Geliang Tanga914e582021-02-01 15:09:07 -0800866 subflows = !!mptcp_pm_get_subflows_max(msk);
867 WRITE_ONCE(pm->work_pending, (!!mptcp_pm_get_local_addr_max(msk) && subflows) ||
868 !!mptcp_pm_get_add_addr_signal_max(msk));
869 WRITE_ONCE(pm->accept_addr, !!mptcp_pm_get_add_addr_accept_max(msk) && subflows);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700870 WRITE_ONCE(pm->accept_subflow, subflows);
871}
872
Florian Westphalb911c972021-02-12 16:00:01 -0800873#define MPTCP_PM_CMD_GRP_OFFSET 0
874#define MPTCP_PM_EV_GRP_OFFSET 1
Paolo Abeni01cacb02020-03-27 14:48:51 -0700875
876static const struct genl_multicast_group mptcp_pm_mcgrps[] = {
877 [MPTCP_PM_CMD_GRP_OFFSET] = { .name = MPTCP_PM_CMD_GRP_NAME, },
Florian Westphalb911c972021-02-12 16:00:01 -0800878 [MPTCP_PM_EV_GRP_OFFSET] = { .name = MPTCP_PM_EV_GRP_NAME,
879 .flags = GENL_UNS_ADMIN_PERM,
880 },
Paolo Abeni01cacb02020-03-27 14:48:51 -0700881};
882
883static const struct nla_policy
884mptcp_pm_addr_policy[MPTCP_PM_ADDR_ATTR_MAX + 1] = {
885 [MPTCP_PM_ADDR_ATTR_FAMILY] = { .type = NLA_U16, },
886 [MPTCP_PM_ADDR_ATTR_ID] = { .type = NLA_U8, },
887 [MPTCP_PM_ADDR_ATTR_ADDR4] = { .type = NLA_U32, },
Johannes Berg81408602020-08-18 10:17:31 +0200888 [MPTCP_PM_ADDR_ATTR_ADDR6] =
889 NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
Paolo Abeni01cacb02020-03-27 14:48:51 -0700890 [MPTCP_PM_ADDR_ATTR_PORT] = { .type = NLA_U16 },
891 [MPTCP_PM_ADDR_ATTR_FLAGS] = { .type = NLA_U32 },
892 [MPTCP_PM_ADDR_ATTR_IF_IDX] = { .type = NLA_S32 },
893};
894
895static const struct nla_policy mptcp_pm_policy[MPTCP_PM_ATTR_MAX + 1] = {
896 [MPTCP_PM_ATTR_ADDR] =
897 NLA_POLICY_NESTED(mptcp_pm_addr_policy),
898 [MPTCP_PM_ATTR_RCV_ADD_ADDRS] = { .type = NLA_U32, },
899 [MPTCP_PM_ATTR_SUBFLOWS] = { .type = NLA_U32, },
900};
901
902static int mptcp_pm_family_to_addr(int family)
903{
904#if IS_ENABLED(CONFIG_MPTCP_IPV6)
905 if (family == AF_INET6)
906 return MPTCP_PM_ADDR_ATTR_ADDR6;
907#endif
908 return MPTCP_PM_ADDR_ATTR_ADDR4;
909}
910
911static int mptcp_pm_parse_addr(struct nlattr *attr, struct genl_info *info,
912 bool require_family,
913 struct mptcp_pm_addr_entry *entry)
914{
915 struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1];
916 int err, addr_addr;
917
918 if (!attr) {
919 GENL_SET_ERR_MSG(info, "missing address info");
920 return -EINVAL;
921 }
922
923 /* no validation needed - was already done via nested policy */
924 err = nla_parse_nested_deprecated(tb, MPTCP_PM_ADDR_ATTR_MAX, attr,
925 mptcp_pm_addr_policy, info->extack);
926 if (err)
927 return err;
928
929 memset(entry, 0, sizeof(*entry));
930 if (!tb[MPTCP_PM_ADDR_ATTR_FAMILY]) {
931 if (!require_family)
932 goto skip_family;
933
934 NL_SET_ERR_MSG_ATTR(info->extack, attr,
935 "missing family");
936 return -EINVAL;
937 }
938
939 entry->addr.family = nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_FAMILY]);
940 if (entry->addr.family != AF_INET
941#if IS_ENABLED(CONFIG_MPTCP_IPV6)
942 && entry->addr.family != AF_INET6
943#endif
944 ) {
945 NL_SET_ERR_MSG_ATTR(info->extack, attr,
946 "unknown address family");
947 return -EINVAL;
948 }
949 addr_addr = mptcp_pm_family_to_addr(entry->addr.family);
950 if (!tb[addr_addr]) {
951 NL_SET_ERR_MSG_ATTR(info->extack, attr,
952 "missing address data");
953 return -EINVAL;
954 }
955
956#if IS_ENABLED(CONFIG_MPTCP_IPV6)
957 if (entry->addr.family == AF_INET6)
958 entry->addr.addr6 = nla_get_in6_addr(tb[addr_addr]);
959 else
960#endif
961 entry->addr.addr.s_addr = nla_get_in_addr(tb[addr_addr]);
962
963skip_family:
Paolo Abenief0da3b2020-09-14 10:01:15 +0200964 if (tb[MPTCP_PM_ADDR_ATTR_IF_IDX]) {
965 u32 val = nla_get_s32(tb[MPTCP_PM_ADDR_ATTR_IF_IDX]);
966
967 entry->addr.ifindex = val;
968 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700969
970 if (tb[MPTCP_PM_ADDR_ATTR_ID])
971 entry->addr.id = nla_get_u8(tb[MPTCP_PM_ADDR_ATTR_ID]);
972
973 if (tb[MPTCP_PM_ADDR_ATTR_FLAGS])
Paolo Abenief0da3b2020-09-14 10:01:15 +0200974 entry->addr.flags = nla_get_u32(tb[MPTCP_PM_ADDR_ATTR_FLAGS]);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700975
Geliang Tanga77e9172021-02-01 15:09:17 -0800976 if (tb[MPTCP_PM_ADDR_ATTR_PORT])
977 entry->addr.port = htons(nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_PORT]));
978
Paolo Abeni01cacb02020-03-27 14:48:51 -0700979 return 0;
980}
981
982static struct pm_nl_pernet *genl_info_pm_nl(struct genl_info *info)
983{
984 return net_generic(genl_info_net(info), pm_nl_pernet_id);
985}
986
Geliang Tang875b76712021-02-01 15:09:08 -0800987static int mptcp_nl_add_subflow_or_signal_addr(struct net *net)
988{
989 struct mptcp_sock *msk;
990 long s_slot = 0, s_num = 0;
991
992 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
993 struct sock *sk = (struct sock *)msk;
994
995 if (!READ_ONCE(msk->fully_established))
996 goto next;
997
998 lock_sock(sk);
999 spin_lock_bh(&msk->pm.lock);
1000 mptcp_pm_create_subflow_or_signal_addr(msk);
1001 spin_unlock_bh(&msk->pm.lock);
1002 release_sock(sk);
1003
1004next:
1005 sock_put(sk);
1006 cond_resched();
1007 }
1008
1009 return 0;
1010}
1011
Paolo Abeni01cacb02020-03-27 14:48:51 -07001012static int mptcp_nl_cmd_add_addr(struct sk_buff *skb, struct genl_info *info)
1013{
1014 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1015 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1016 struct mptcp_pm_addr_entry addr, *entry;
1017 int ret;
1018
1019 ret = mptcp_pm_parse_addr(attr, info, true, &addr);
1020 if (ret < 0)
1021 return ret;
1022
1023 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1024 if (!entry) {
1025 GENL_SET_ERR_MSG(info, "can't allocate addr");
1026 return -ENOMEM;
1027 }
1028
1029 *entry = addr;
Geliang Tang1729cf12021-02-01 15:09:12 -08001030 if (entry->addr.port) {
1031 ret = mptcp_pm_nl_create_listen_socket(skb->sk, entry);
1032 if (ret) {
1033 GENL_SET_ERR_MSG(info, "create listen socket error");
1034 kfree(entry);
1035 return ret;
1036 }
1037 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001038 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
1039 if (ret < 0) {
1040 GENL_SET_ERR_MSG(info, "too many addresses or duplicate one");
Geliang Tang1729cf12021-02-01 15:09:12 -08001041 if (entry->lsk)
1042 sock_release(entry->lsk);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001043 kfree(entry);
1044 return ret;
1045 }
1046
Geliang Tang875b76712021-02-01 15:09:08 -08001047 mptcp_nl_add_subflow_or_signal_addr(sock_net(skb->sk));
1048
Paolo Abeni01cacb02020-03-27 14:48:51 -07001049 return 0;
1050}
1051
1052static struct mptcp_pm_addr_entry *
1053__lookup_addr_by_id(struct pm_nl_pernet *pernet, unsigned int id)
1054{
1055 struct mptcp_pm_addr_entry *entry;
1056
1057 list_for_each_entry(entry, &pernet->local_addr_list, list) {
1058 if (entry->addr.id == id)
1059 return entry;
1060 }
1061 return NULL;
1062}
1063
Geliang Tangb6c08382020-09-24 08:29:54 +08001064static bool remove_anno_list_by_saddr(struct mptcp_sock *msk,
1065 struct mptcp_addr_info *addr)
1066{
Geliang Tang00cfd772020-09-24 08:30:02 +08001067 struct mptcp_pm_add_entry *entry;
Geliang Tangb6c08382020-09-24 08:29:54 +08001068
Geliang Tang00cfd772020-09-24 08:30:02 +08001069 entry = mptcp_pm_del_add_timer(msk, addr);
1070 if (entry) {
1071 list_del(&entry->list);
1072 kfree(entry);
1073 return true;
Geliang Tangb6c08382020-09-24 08:29:54 +08001074 }
1075
1076 return false;
1077}
1078
1079static bool mptcp_pm_remove_anno_addr(struct mptcp_sock *msk,
1080 struct mptcp_addr_info *addr,
1081 bool force)
1082{
Geliang Tangcbde2782021-03-12 17:16:12 -08001083 struct mptcp_rm_list list = { .nr = 0 };
Geliang Tangb6c08382020-09-24 08:29:54 +08001084 bool ret;
1085
Geliang Tangcbde2782021-03-12 17:16:12 -08001086 list.ids[list.nr++] = addr->id;
1087
Geliang Tangb6c08382020-09-24 08:29:54 +08001088 ret = remove_anno_list_by_saddr(msk, addr);
Geliang Tang00cfd772020-09-24 08:30:02 +08001089 if (ret || force) {
1090 spin_lock_bh(&msk->pm.lock);
Geliang Tangcbde2782021-03-12 17:16:12 -08001091 mptcp_pm_remove_addr(msk, &list);
Geliang Tang00cfd772020-09-24 08:30:02 +08001092 spin_unlock_bh(&msk->pm.lock);
1093 }
Geliang Tangb6c08382020-09-24 08:29:54 +08001094 return ret;
1095}
1096
1097static int mptcp_nl_remove_subflow_and_signal_addr(struct net *net,
1098 struct mptcp_addr_info *addr)
1099{
1100 struct mptcp_sock *msk;
1101 long s_slot = 0, s_num = 0;
Geliang Tangddd14bb2021-03-12 17:16:16 -08001102 struct mptcp_rm_list list = { .nr = 0 };
Geliang Tangb6c08382020-09-24 08:29:54 +08001103
1104 pr_debug("remove_id=%d", addr->id);
1105
Geliang Tangddd14bb2021-03-12 17:16:16 -08001106 list.ids[list.nr++] = addr->id;
1107
Geliang Tangb6c08382020-09-24 08:29:54 +08001108 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1109 struct sock *sk = (struct sock *)msk;
1110 bool remove_subflow;
1111
1112 if (list_empty(&msk->conn_list)) {
1113 mptcp_pm_remove_anno_addr(msk, addr, false);
1114 goto next;
1115 }
1116
1117 lock_sock(sk);
1118 remove_subflow = lookup_subflow_by_saddr(&msk->conn_list, addr);
1119 mptcp_pm_remove_anno_addr(msk, addr, remove_subflow);
1120 if (remove_subflow)
Geliang Tangddd14bb2021-03-12 17:16:16 -08001121 mptcp_pm_remove_subflow(msk, &list);
Geliang Tangb6c08382020-09-24 08:29:54 +08001122 release_sock(sk);
1123
1124next:
1125 sock_put(sk);
1126 cond_resched();
1127 }
1128
1129 return 0;
1130}
1131
Geliang Tang1729cf12021-02-01 15:09:12 -08001132struct addr_entry_release_work {
1133 struct rcu_work rwork;
1134 struct mptcp_pm_addr_entry *entry;
1135};
1136
1137static void mptcp_pm_release_addr_entry(struct work_struct *work)
1138{
1139 struct addr_entry_release_work *w;
1140 struct mptcp_pm_addr_entry *entry;
1141
1142 w = container_of(to_rcu_work(work), struct addr_entry_release_work, rwork);
1143 entry = w->entry;
1144 if (entry) {
1145 if (entry->lsk)
1146 sock_release(entry->lsk);
1147 kfree(entry);
1148 }
1149 kfree(w);
1150}
1151
1152static void mptcp_pm_free_addr_entry(struct mptcp_pm_addr_entry *entry)
1153{
1154 struct addr_entry_release_work *w;
1155
1156 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1157 if (w) {
1158 INIT_RCU_WORK(&w->rwork, mptcp_pm_release_addr_entry);
1159 w->entry = entry;
1160 queue_rcu_work(system_wq, &w->rwork);
1161 }
1162}
1163
Paolo Abeni01cacb02020-03-27 14:48:51 -07001164static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info)
1165{
1166 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1167 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1168 struct mptcp_pm_addr_entry addr, *entry;
Geliang Tang72603d22021-02-01 15:09:06 -08001169 unsigned int addr_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001170 int ret;
1171
1172 ret = mptcp_pm_parse_addr(attr, info, false, &addr);
1173 if (ret < 0)
1174 return ret;
1175
1176 spin_lock_bh(&pernet->lock);
1177 entry = __lookup_addr_by_id(pernet, addr.addr.id);
1178 if (!entry) {
1179 GENL_SET_ERR_MSG(info, "address not found");
Geliang Tangb6c08382020-09-24 08:29:54 +08001180 spin_unlock_bh(&pernet->lock);
1181 return -EINVAL;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001182 }
Geliang Tang72603d22021-02-01 15:09:06 -08001183 if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
1184 addr_max = pernet->add_addr_signal_max;
1185 WRITE_ONCE(pernet->add_addr_signal_max, addr_max - 1);
1186 }
1187 if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) {
1188 addr_max = pernet->local_addr_max;
1189 WRITE_ONCE(pernet->local_addr_max, addr_max - 1);
1190 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001191
1192 pernet->addrs--;
1193 list_del_rcu(&entry->list);
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001194 __clear_bit(entry->addr.id, pernet->id_bitmap);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001195 spin_unlock_bh(&pernet->lock);
Geliang Tangb6c08382020-09-24 08:29:54 +08001196
1197 mptcp_nl_remove_subflow_and_signal_addr(sock_net(skb->sk), &entry->addr);
Geliang Tang1729cf12021-02-01 15:09:12 -08001198 mptcp_pm_free_addr_entry(entry);
Geliang Tangb6c08382020-09-24 08:29:54 +08001199
Paolo Abeni01cacb02020-03-27 14:48:51 -07001200 return ret;
1201}
1202
Geliang Tang141694d2020-12-10 14:24:59 -08001203static void __flush_addrs(struct net *net, struct list_head *list)
Paolo Abeni01cacb02020-03-27 14:48:51 -07001204{
Geliang Tang141694d2020-12-10 14:24:59 -08001205 while (!list_empty(list)) {
Paolo Abeni01cacb02020-03-27 14:48:51 -07001206 struct mptcp_pm_addr_entry *cur;
1207
Geliang Tang141694d2020-12-10 14:24:59 -08001208 cur = list_entry(list->next,
Paolo Abeni01cacb02020-03-27 14:48:51 -07001209 struct mptcp_pm_addr_entry, list);
Geliang Tang141694d2020-12-10 14:24:59 -08001210 mptcp_nl_remove_subflow_and_signal_addr(net, &cur->addr);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001211 list_del_rcu(&cur->list);
Geliang Tang1729cf12021-02-01 15:09:12 -08001212 mptcp_pm_free_addr_entry(cur);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001213 }
1214}
1215
1216static void __reset_counters(struct pm_nl_pernet *pernet)
1217{
Geliang Tang72603d22021-02-01 15:09:06 -08001218 WRITE_ONCE(pernet->add_addr_signal_max, 0);
1219 WRITE_ONCE(pernet->add_addr_accept_max, 0);
1220 WRITE_ONCE(pernet->local_addr_max, 0);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001221 pernet->addrs = 0;
1222}
1223
1224static int mptcp_nl_cmd_flush_addrs(struct sk_buff *skb, struct genl_info *info)
1225{
1226 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
Geliang Tang141694d2020-12-10 14:24:59 -08001227 LIST_HEAD(free_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001228
1229 spin_lock_bh(&pernet->lock);
Geliang Tang141694d2020-12-10 14:24:59 -08001230 list_splice_init(&pernet->local_addr_list, &free_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001231 __reset_counters(pernet);
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001232 pernet->next_id = 1;
1233 bitmap_zero(pernet->id_bitmap, MAX_ADDR_ID + 1);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001234 spin_unlock_bh(&pernet->lock);
Geliang Tang141694d2020-12-10 14:24:59 -08001235 __flush_addrs(sock_net(skb->sk), &free_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001236 return 0;
1237}
1238
1239static int mptcp_nl_fill_addr(struct sk_buff *skb,
1240 struct mptcp_pm_addr_entry *entry)
1241{
1242 struct mptcp_addr_info *addr = &entry->addr;
1243 struct nlattr *attr;
1244
1245 attr = nla_nest_start(skb, MPTCP_PM_ATTR_ADDR);
1246 if (!attr)
1247 return -EMSGSIZE;
1248
1249 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_FAMILY, addr->family))
1250 goto nla_put_failure;
Geliang Tanga77e9172021-02-01 15:09:17 -08001251 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_PORT, ntohs(addr->port)))
1252 goto nla_put_failure;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001253 if (nla_put_u8(skb, MPTCP_PM_ADDR_ATTR_ID, addr->id))
1254 goto nla_put_failure;
Paolo Abenief0da3b2020-09-14 10:01:15 +02001255 if (nla_put_u32(skb, MPTCP_PM_ADDR_ATTR_FLAGS, entry->addr.flags))
Paolo Abeni01cacb02020-03-27 14:48:51 -07001256 goto nla_put_failure;
Paolo Abenief0da3b2020-09-14 10:01:15 +02001257 if (entry->addr.ifindex &&
1258 nla_put_s32(skb, MPTCP_PM_ADDR_ATTR_IF_IDX, entry->addr.ifindex))
Paolo Abeni01cacb02020-03-27 14:48:51 -07001259 goto nla_put_failure;
1260
Bo YUb4e0f9a2020-04-23 10:10:03 +08001261 if (addr->family == AF_INET &&
1262 nla_put_in_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR4,
1263 addr->addr.s_addr))
1264 goto nla_put_failure;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001265#if IS_ENABLED(CONFIG_MPTCP_IPV6)
Bo YUb4e0f9a2020-04-23 10:10:03 +08001266 else if (addr->family == AF_INET6 &&
1267 nla_put_in6_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR6, &addr->addr6))
1268 goto nla_put_failure;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001269#endif
1270 nla_nest_end(skb, attr);
1271 return 0;
1272
1273nla_put_failure:
1274 nla_nest_cancel(skb, attr);
1275 return -EMSGSIZE;
1276}
1277
1278static int mptcp_nl_cmd_get_addr(struct sk_buff *skb, struct genl_info *info)
1279{
1280 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1281 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1282 struct mptcp_pm_addr_entry addr, *entry;
1283 struct sk_buff *msg;
1284 void *reply;
1285 int ret;
1286
1287 ret = mptcp_pm_parse_addr(attr, info, false, &addr);
1288 if (ret < 0)
1289 return ret;
1290
1291 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1292 if (!msg)
1293 return -ENOMEM;
1294
1295 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1296 info->genlhdr->cmd);
1297 if (!reply) {
1298 GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1299 ret = -EMSGSIZE;
1300 goto fail;
1301 }
1302
1303 spin_lock_bh(&pernet->lock);
1304 entry = __lookup_addr_by_id(pernet, addr.addr.id);
1305 if (!entry) {
1306 GENL_SET_ERR_MSG(info, "address not found");
1307 ret = -EINVAL;
1308 goto unlock_fail;
1309 }
1310
1311 ret = mptcp_nl_fill_addr(msg, entry);
1312 if (ret)
1313 goto unlock_fail;
1314
1315 genlmsg_end(msg, reply);
1316 ret = genlmsg_reply(msg, info);
1317 spin_unlock_bh(&pernet->lock);
1318 return ret;
1319
1320unlock_fail:
1321 spin_unlock_bh(&pernet->lock);
1322
1323fail:
1324 nlmsg_free(msg);
1325 return ret;
1326}
1327
1328static int mptcp_nl_cmd_dump_addrs(struct sk_buff *msg,
1329 struct netlink_callback *cb)
1330{
1331 struct net *net = sock_net(msg->sk);
1332 struct mptcp_pm_addr_entry *entry;
1333 struct pm_nl_pernet *pernet;
1334 int id = cb->args[0];
1335 void *hdr;
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001336 int i;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001337
1338 pernet = net_generic(net, pm_nl_pernet_id);
1339
1340 spin_lock_bh(&pernet->lock);
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001341 for (i = id; i < MAX_ADDR_ID + 1; i++) {
1342 if (test_bit(i, pernet->id_bitmap)) {
1343 entry = __lookup_addr_by_id(pernet, i);
1344 if (!entry)
1345 break;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001346
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001347 if (entry->addr.id <= id)
1348 continue;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001349
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001350 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid,
1351 cb->nlh->nlmsg_seq, &mptcp_genl_family,
1352 NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR);
1353 if (!hdr)
1354 break;
1355
1356 if (mptcp_nl_fill_addr(msg, entry) < 0) {
1357 genlmsg_cancel(msg, hdr);
1358 break;
1359 }
1360
1361 id = entry->addr.id;
1362 genlmsg_end(msg, hdr);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001363 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001364 }
1365 spin_unlock_bh(&pernet->lock);
1366
1367 cb->args[0] = id;
1368 return msg->len;
1369}
1370
1371static int parse_limit(struct genl_info *info, int id, unsigned int *limit)
1372{
1373 struct nlattr *attr = info->attrs[id];
1374
1375 if (!attr)
1376 return 0;
1377
1378 *limit = nla_get_u32(attr);
1379 if (*limit > MPTCP_PM_ADDR_MAX) {
1380 GENL_SET_ERR_MSG(info, "limit greater than maximum");
1381 return -EINVAL;
1382 }
1383 return 0;
1384}
1385
1386static int
1387mptcp_nl_cmd_set_limits(struct sk_buff *skb, struct genl_info *info)
1388{
1389 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1390 unsigned int rcv_addrs, subflows;
1391 int ret;
1392
1393 spin_lock_bh(&pernet->lock);
1394 rcv_addrs = pernet->add_addr_accept_max;
1395 ret = parse_limit(info, MPTCP_PM_ATTR_RCV_ADD_ADDRS, &rcv_addrs);
1396 if (ret)
1397 goto unlock;
1398
1399 subflows = pernet->subflows_max;
1400 ret = parse_limit(info, MPTCP_PM_ATTR_SUBFLOWS, &subflows);
1401 if (ret)
1402 goto unlock;
1403
1404 WRITE_ONCE(pernet->add_addr_accept_max, rcv_addrs);
1405 WRITE_ONCE(pernet->subflows_max, subflows);
1406
1407unlock:
1408 spin_unlock_bh(&pernet->lock);
1409 return ret;
1410}
1411
1412static int
1413mptcp_nl_cmd_get_limits(struct sk_buff *skb, struct genl_info *info)
1414{
1415 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1416 struct sk_buff *msg;
1417 void *reply;
1418
1419 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1420 if (!msg)
1421 return -ENOMEM;
1422
1423 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1424 MPTCP_PM_CMD_GET_LIMITS);
1425 if (!reply)
1426 goto fail;
1427
1428 if (nla_put_u32(msg, MPTCP_PM_ATTR_RCV_ADD_ADDRS,
1429 READ_ONCE(pernet->add_addr_accept_max)))
1430 goto fail;
1431
1432 if (nla_put_u32(msg, MPTCP_PM_ATTR_SUBFLOWS,
1433 READ_ONCE(pernet->subflows_max)))
1434 goto fail;
1435
1436 genlmsg_end(msg, reply);
1437 return genlmsg_reply(msg, info);
1438
1439fail:
1440 GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1441 nlmsg_free(msg);
1442 return -EMSGSIZE;
1443}
1444
Geliang Tang0f9f6962021-01-08 16:47:59 -08001445static int mptcp_nl_addr_backup(struct net *net,
1446 struct mptcp_addr_info *addr,
1447 u8 bkup)
1448{
1449 long s_slot = 0, s_num = 0;
1450 struct mptcp_sock *msk;
1451 int ret = -EINVAL;
1452
1453 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1454 struct sock *sk = (struct sock *)msk;
1455
1456 if (list_empty(&msk->conn_list))
1457 goto next;
1458
1459 lock_sock(sk);
1460 spin_lock_bh(&msk->pm.lock);
1461 ret = mptcp_pm_nl_mp_prio_send_ack(msk, addr, bkup);
1462 spin_unlock_bh(&msk->pm.lock);
1463 release_sock(sk);
1464
1465next:
1466 sock_put(sk);
1467 cond_resched();
1468 }
1469
1470 return ret;
1471}
1472
1473static int mptcp_nl_cmd_set_flags(struct sk_buff *skb, struct genl_info *info)
1474{
1475 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1476 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1477 struct mptcp_pm_addr_entry addr, *entry;
1478 struct net *net = sock_net(skb->sk);
1479 u8 bkup = 0;
1480 int ret;
1481
1482 ret = mptcp_pm_parse_addr(attr, info, true, &addr);
1483 if (ret < 0)
1484 return ret;
1485
1486 if (addr.addr.flags & MPTCP_PM_ADDR_FLAG_BACKUP)
1487 bkup = 1;
1488
1489 list_for_each_entry(entry, &pernet->local_addr_list, list) {
1490 if (addresses_equal(&entry->addr, &addr.addr, true)) {
1491 ret = mptcp_nl_addr_backup(net, &entry->addr, bkup);
1492 if (ret)
1493 return ret;
1494
1495 if (bkup)
1496 entry->addr.flags |= MPTCP_PM_ADDR_FLAG_BACKUP;
1497 else
1498 entry->addr.flags &= ~MPTCP_PM_ADDR_FLAG_BACKUP;
1499 }
1500 }
1501
1502 return 0;
1503}
1504
Florian Westphalb911c972021-02-12 16:00:01 -08001505static void mptcp_nl_mcast_send(struct net *net, struct sk_buff *nlskb, gfp_t gfp)
1506{
1507 genlmsg_multicast_netns(&mptcp_genl_family, net,
1508 nlskb, 0, MPTCP_PM_EV_GRP_OFFSET, gfp);
1509}
1510
1511static int mptcp_event_add_subflow(struct sk_buff *skb, const struct sock *ssk)
1512{
1513 const struct inet_sock *issk = inet_sk(ssk);
1514 const struct mptcp_subflow_context *sf;
1515
1516 if (nla_put_u16(skb, MPTCP_ATTR_FAMILY, ssk->sk_family))
1517 return -EMSGSIZE;
1518
1519 switch (ssk->sk_family) {
1520 case AF_INET:
1521 if (nla_put_in_addr(skb, MPTCP_ATTR_SADDR4, issk->inet_saddr))
1522 return -EMSGSIZE;
1523 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, issk->inet_daddr))
1524 return -EMSGSIZE;
1525 break;
1526#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1527 case AF_INET6: {
1528 const struct ipv6_pinfo *np = inet6_sk(ssk);
1529
1530 if (nla_put_in6_addr(skb, MPTCP_ATTR_SADDR6, &np->saddr))
1531 return -EMSGSIZE;
1532 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &ssk->sk_v6_daddr))
1533 return -EMSGSIZE;
1534 break;
1535 }
1536#endif
1537 default:
1538 WARN_ON_ONCE(1);
1539 return -EMSGSIZE;
1540 }
1541
1542 if (nla_put_be16(skb, MPTCP_ATTR_SPORT, issk->inet_sport))
1543 return -EMSGSIZE;
1544 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, issk->inet_dport))
1545 return -EMSGSIZE;
1546
1547 sf = mptcp_subflow_ctx(ssk);
1548 if (WARN_ON_ONCE(!sf))
1549 return -EINVAL;
1550
1551 if (nla_put_u8(skb, MPTCP_ATTR_LOC_ID, sf->local_id))
1552 return -EMSGSIZE;
1553
1554 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, sf->remote_id))
1555 return -EMSGSIZE;
1556
1557 return 0;
1558}
1559
1560static int mptcp_event_put_token_and_ssk(struct sk_buff *skb,
1561 const struct mptcp_sock *msk,
1562 const struct sock *ssk)
1563{
1564 const struct sock *sk = (const struct sock *)msk;
1565 const struct mptcp_subflow_context *sf;
1566 u8 sk_err;
1567
1568 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1569 return -EMSGSIZE;
1570
1571 if (mptcp_event_add_subflow(skb, ssk))
1572 return -EMSGSIZE;
1573
1574 sf = mptcp_subflow_ctx(ssk);
1575 if (WARN_ON_ONCE(!sf))
1576 return -EINVAL;
1577
1578 if (nla_put_u8(skb, MPTCP_ATTR_BACKUP, sf->backup))
1579 return -EMSGSIZE;
1580
1581 if (ssk->sk_bound_dev_if &&
1582 nla_put_s32(skb, MPTCP_ATTR_IF_IDX, ssk->sk_bound_dev_if))
1583 return -EMSGSIZE;
1584
1585 sk_err = ssk->sk_err;
1586 if (sk_err && sk->sk_state == TCP_ESTABLISHED &&
1587 nla_put_u8(skb, MPTCP_ATTR_ERROR, sk_err))
1588 return -EMSGSIZE;
1589
1590 return 0;
1591}
1592
1593static int mptcp_event_sub_established(struct sk_buff *skb,
1594 const struct mptcp_sock *msk,
1595 const struct sock *ssk)
1596{
1597 return mptcp_event_put_token_and_ssk(skb, msk, ssk);
1598}
1599
1600static int mptcp_event_sub_closed(struct sk_buff *skb,
1601 const struct mptcp_sock *msk,
1602 const struct sock *ssk)
1603{
1604 if (mptcp_event_put_token_and_ssk(skb, msk, ssk))
1605 return -EMSGSIZE;
1606
1607 return 0;
1608}
1609
1610static int mptcp_event_created(struct sk_buff *skb,
1611 const struct mptcp_sock *msk,
1612 const struct sock *ssk)
1613{
1614 int err = nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token);
1615
1616 if (err)
1617 return err;
1618
1619 return mptcp_event_add_subflow(skb, ssk);
1620}
1621
1622void mptcp_event_addr_removed(const struct mptcp_sock *msk, uint8_t id)
1623{
1624 struct net *net = sock_net((const struct sock *)msk);
1625 struct nlmsghdr *nlh;
1626 struct sk_buff *skb;
1627
1628 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
1629 return;
1630
1631 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1632 if (!skb)
1633 return;
1634
1635 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, MPTCP_EVENT_REMOVED);
1636 if (!nlh)
1637 goto nla_put_failure;
1638
1639 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1640 goto nla_put_failure;
1641
1642 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, id))
1643 goto nla_put_failure;
1644
1645 genlmsg_end(skb, nlh);
1646 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC);
1647 return;
1648
1649nla_put_failure:
1650 kfree_skb(skb);
1651}
1652
1653void mptcp_event_addr_announced(const struct mptcp_sock *msk,
1654 const struct mptcp_addr_info *info)
1655{
1656 struct net *net = sock_net((const struct sock *)msk);
1657 struct nlmsghdr *nlh;
1658 struct sk_buff *skb;
1659
1660 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
1661 return;
1662
1663 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1664 if (!skb)
1665 return;
1666
1667 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0,
1668 MPTCP_EVENT_ANNOUNCED);
1669 if (!nlh)
1670 goto nla_put_failure;
1671
1672 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1673 goto nla_put_failure;
1674
1675 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, info->id))
1676 goto nla_put_failure;
1677
1678 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, info->port))
1679 goto nla_put_failure;
1680
1681 switch (info->family) {
1682 case AF_INET:
1683 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, info->addr.s_addr))
1684 goto nla_put_failure;
1685 break;
1686#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1687 case AF_INET6:
1688 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &info->addr6))
1689 goto nla_put_failure;
1690 break;
1691#endif
1692 default:
1693 WARN_ON_ONCE(1);
1694 goto nla_put_failure;
1695 }
1696
1697 genlmsg_end(skb, nlh);
1698 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC);
1699 return;
1700
1701nla_put_failure:
1702 kfree_skb(skb);
1703}
1704
1705void mptcp_event(enum mptcp_event_type type, const struct mptcp_sock *msk,
1706 const struct sock *ssk, gfp_t gfp)
1707{
1708 struct net *net = sock_net((const struct sock *)msk);
1709 struct nlmsghdr *nlh;
1710 struct sk_buff *skb;
1711
1712 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
1713 return;
1714
1715 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
1716 if (!skb)
1717 return;
1718
1719 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, type);
1720 if (!nlh)
1721 goto nla_put_failure;
1722
1723 switch (type) {
1724 case MPTCP_EVENT_UNSPEC:
1725 WARN_ON_ONCE(1);
1726 break;
1727 case MPTCP_EVENT_CREATED:
1728 case MPTCP_EVENT_ESTABLISHED:
1729 if (mptcp_event_created(skb, msk, ssk) < 0)
1730 goto nla_put_failure;
1731 break;
1732 case MPTCP_EVENT_CLOSED:
1733 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token) < 0)
1734 goto nla_put_failure;
1735 break;
1736 case MPTCP_EVENT_ANNOUNCED:
1737 case MPTCP_EVENT_REMOVED:
1738 /* call mptcp_event_addr_announced()/removed instead */
1739 WARN_ON_ONCE(1);
1740 break;
1741 case MPTCP_EVENT_SUB_ESTABLISHED:
1742 case MPTCP_EVENT_SUB_PRIORITY:
1743 if (mptcp_event_sub_established(skb, msk, ssk) < 0)
1744 goto nla_put_failure;
1745 break;
1746 case MPTCP_EVENT_SUB_CLOSED:
1747 if (mptcp_event_sub_closed(skb, msk, ssk) < 0)
1748 goto nla_put_failure;
1749 break;
1750 }
1751
1752 genlmsg_end(skb, nlh);
1753 mptcp_nl_mcast_send(net, skb, gfp);
1754 return;
1755
1756nla_put_failure:
1757 kfree_skb(skb);
1758}
1759
Rikard Falkeborn674d3ab2020-10-05 01:44:16 +02001760static const struct genl_small_ops mptcp_pm_ops[] = {
Paolo Abeni01cacb02020-03-27 14:48:51 -07001761 {
1762 .cmd = MPTCP_PM_CMD_ADD_ADDR,
1763 .doit = mptcp_nl_cmd_add_addr,
1764 .flags = GENL_ADMIN_PERM,
1765 },
1766 {
1767 .cmd = MPTCP_PM_CMD_DEL_ADDR,
1768 .doit = mptcp_nl_cmd_del_addr,
1769 .flags = GENL_ADMIN_PERM,
1770 },
1771 {
1772 .cmd = MPTCP_PM_CMD_FLUSH_ADDRS,
1773 .doit = mptcp_nl_cmd_flush_addrs,
1774 .flags = GENL_ADMIN_PERM,
1775 },
1776 {
1777 .cmd = MPTCP_PM_CMD_GET_ADDR,
1778 .doit = mptcp_nl_cmd_get_addr,
1779 .dumpit = mptcp_nl_cmd_dump_addrs,
1780 },
1781 {
1782 .cmd = MPTCP_PM_CMD_SET_LIMITS,
1783 .doit = mptcp_nl_cmd_set_limits,
1784 .flags = GENL_ADMIN_PERM,
1785 },
1786 {
1787 .cmd = MPTCP_PM_CMD_GET_LIMITS,
1788 .doit = mptcp_nl_cmd_get_limits,
1789 },
Geliang Tang0f9f6962021-01-08 16:47:59 -08001790 {
1791 .cmd = MPTCP_PM_CMD_SET_FLAGS,
1792 .doit = mptcp_nl_cmd_set_flags,
1793 .flags = GENL_ADMIN_PERM,
1794 },
Paolo Abeni01cacb02020-03-27 14:48:51 -07001795};
1796
1797static struct genl_family mptcp_genl_family __ro_after_init = {
1798 .name = MPTCP_PM_NAME,
1799 .version = MPTCP_PM_VER,
1800 .maxattr = MPTCP_PM_ATTR_MAX,
1801 .policy = mptcp_pm_policy,
1802 .netnsok = true,
1803 .module = THIS_MODULE,
Jakub Kicinski66a9b922020-10-02 14:49:54 -07001804 .small_ops = mptcp_pm_ops,
1805 .n_small_ops = ARRAY_SIZE(mptcp_pm_ops),
Paolo Abeni01cacb02020-03-27 14:48:51 -07001806 .mcgrps = mptcp_pm_mcgrps,
1807 .n_mcgrps = ARRAY_SIZE(mptcp_pm_mcgrps),
1808};
1809
1810static int __net_init pm_nl_init_net(struct net *net)
1811{
1812 struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id);
1813
1814 INIT_LIST_HEAD_RCU(&pernet->local_addr_list);
1815 __reset_counters(pernet);
1816 pernet->next_id = 1;
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001817 bitmap_zero(pernet->id_bitmap, MAX_ADDR_ID + 1);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001818 spin_lock_init(&pernet->lock);
1819 return 0;
1820}
1821
1822static void __net_exit pm_nl_exit_net(struct list_head *net_list)
1823{
1824 struct net *net;
1825
1826 list_for_each_entry(net, net_list, exit_list) {
Geliang Tang141694d2020-12-10 14:24:59 -08001827 struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id);
1828
Paolo Abeni01cacb02020-03-27 14:48:51 -07001829 /* net is removed from namespace list, can't race with
1830 * other modifiers
1831 */
Geliang Tang141694d2020-12-10 14:24:59 -08001832 __flush_addrs(net, &pernet->local_addr_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001833 }
1834}
1835
1836static struct pernet_operations mptcp_pm_pernet_ops = {
1837 .init = pm_nl_init_net,
1838 .exit_batch = pm_nl_exit_net,
1839 .id = &pm_nl_pernet_id,
1840 .size = sizeof(struct pm_nl_pernet),
1841};
1842
Paolo Abenid39dcec2020-06-26 19:29:59 +02001843void __init mptcp_pm_nl_init(void)
Paolo Abeni01cacb02020-03-27 14:48:51 -07001844{
1845 if (register_pernet_subsys(&mptcp_pm_pernet_ops) < 0)
1846 panic("Failed to register MPTCP PM pernet subsystem.\n");
1847
1848 if (genl_register_family(&mptcp_genl_family))
1849 panic("Failed to register MPTCP PM netlink family\n");
1850}