blob: f71e910670bffcdc12e1a00c6e3716a5af152aac [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
59static bool addresses_equal(const struct mptcp_addr_info *a,
60 struct mptcp_addr_info *b, bool use_port)
61{
62 bool addr_equals = false;
63
Matthieu Baerts7b9b0f72021-01-25 10:59:01 -080064 if (a->family == b->family) {
65 if (a->family == AF_INET)
66 addr_equals = a->addr.s_addr == b->addr.s_addr;
Paolo Abeni01cacb02020-03-27 14:48:51 -070067#if IS_ENABLED(CONFIG_MPTCP_IPV6)
Matthieu Baerts7b9b0f72021-01-25 10:59:01 -080068 else
69 addr_equals = !ipv6_addr_cmp(&a->addr6, &b->addr6);
70 } else if (a->family == AF_INET) {
71 if (ipv6_addr_v4mapped(&b->addr6))
72 addr_equals = a->addr.s_addr == b->addr6.s6_addr32[3];
73 } else if (b->family == AF_INET) {
74 if (ipv6_addr_v4mapped(&a->addr6))
75 addr_equals = a->addr6.s6_addr32[3] == b->addr.s_addr;
Paolo Abeni01cacb02020-03-27 14:48:51 -070076#endif
Matthieu Baerts7b9b0f72021-01-25 10:59:01 -080077 }
Paolo Abeni01cacb02020-03-27 14:48:51 -070078
79 if (!addr_equals)
80 return false;
81 if (!use_port)
82 return true;
83
84 return a->port == b->port;
85}
86
Geliang Tang57025812020-09-08 10:49:38 +080087static bool address_zero(const struct mptcp_addr_info *addr)
88{
89 struct mptcp_addr_info zero;
90
91 memset(&zero, 0, sizeof(zero));
92 zero.family = addr->family;
93
Geliang Tang60b57bf2021-02-01 15:09:16 -080094 return addresses_equal(addr, &zero, true);
Geliang Tang57025812020-09-08 10:49:38 +080095}
96
Paolo Abeni01cacb02020-03-27 14:48:51 -070097static void local_address(const struct sock_common *skc,
98 struct mptcp_addr_info *addr)
99{
Paolo Abeni01cacb02020-03-27 14:48:51 -0700100 addr->family = skc->skc_family;
Geliang Tang5bc56382021-02-01 15:09:15 -0800101 addr->port = htons(skc->skc_num);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700102 if (addr->family == AF_INET)
103 addr->addr.s_addr = skc->skc_rcv_saddr;
104#if IS_ENABLED(CONFIG_MPTCP_IPV6)
105 else if (addr->family == AF_INET6)
106 addr->addr6 = skc->skc_v6_rcv_saddr;
107#endif
108}
109
110static void remote_address(const struct sock_common *skc,
111 struct mptcp_addr_info *addr)
112{
113 addr->family = skc->skc_family;
114 addr->port = skc->skc_dport;
115 if (addr->family == AF_INET)
116 addr->addr.s_addr = skc->skc_daddr;
117#if IS_ENABLED(CONFIG_MPTCP_IPV6)
118 else if (addr->family == AF_INET6)
119 addr->addr6 = skc->skc_v6_daddr;
120#endif
121}
122
123static bool lookup_subflow_by_saddr(const struct list_head *list,
124 struct mptcp_addr_info *saddr)
125{
126 struct mptcp_subflow_context *subflow;
127 struct mptcp_addr_info cur;
128 struct sock_common *skc;
129
130 list_for_each_entry(subflow, list, node) {
131 skc = (struct sock_common *)mptcp_subflow_tcp_sock(subflow);
132
133 local_address(skc, &cur);
Geliang Tang60b57bf2021-02-01 15:09:16 -0800134 if (addresses_equal(&cur, saddr, saddr->port))
Paolo Abeni01cacb02020-03-27 14:48:51 -0700135 return true;
136 }
137
138 return false;
139}
140
Geliang Tangd84ad042021-03-26 11:26:32 -0700141static bool lookup_subflow_by_daddr(const struct list_head *list,
142 struct mptcp_addr_info *daddr)
143{
144 struct mptcp_subflow_context *subflow;
145 struct mptcp_addr_info cur;
146 struct sock_common *skc;
147
148 list_for_each_entry(subflow, list, node) {
149 skc = (struct sock_common *)mptcp_subflow_tcp_sock(subflow);
150
151 remote_address(skc, &cur);
152 if (addresses_equal(&cur, daddr, daddr->port))
153 return true;
154 }
155
156 return false;
157}
158
Paolo Abeni01cacb02020-03-27 14:48:51 -0700159static struct mptcp_pm_addr_entry *
160select_local_address(const struct pm_nl_pernet *pernet,
161 struct mptcp_sock *msk)
162{
163 struct mptcp_pm_addr_entry *entry, *ret = NULL;
Matthieu Baerts7b9b0f72021-01-25 10:59:01 -0800164 struct sock *sk = (struct sock *)msk;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700165
Florian Westphal3abc05d2021-02-04 15:23:30 -0800166 msk_owned_by_me(msk);
167
Paolo Abeni01cacb02020-03-27 14:48:51 -0700168 rcu_read_lock();
Paolo Abeni1bc73272020-12-10 14:25:05 -0800169 __mptcp_flush_join_list(msk);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700170 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
Paolo Abenief0da3b2020-09-14 10:01:15 +0200171 if (!(entry->addr.flags & MPTCP_PM_ADDR_FLAG_SUBFLOW))
Paolo Abeni01cacb02020-03-27 14:48:51 -0700172 continue;
173
Matthieu Baerts7b9b0f72021-01-25 10:59:01 -0800174 if (entry->addr.family != sk->sk_family) {
175#if IS_ENABLED(CONFIG_MPTCP_IPV6)
176 if ((entry->addr.family == AF_INET &&
177 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) ||
178 (sk->sk_family == AF_INET &&
179 !ipv6_addr_v4mapped(&entry->addr.addr6)))
180#endif
181 continue;
182 }
183
Paolo Abeni01cacb02020-03-27 14:48:51 -0700184 /* avoid any address already in use by subflows and
185 * pending join
186 */
Matthieu Baerts7b9b0f72021-01-25 10:59:01 -0800187 if (!lookup_subflow_by_saddr(&msk->conn_list, &entry->addr)) {
Paolo Abeni01cacb02020-03-27 14:48:51 -0700188 ret = entry;
189 break;
190 }
191 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700192 rcu_read_unlock();
193 return ret;
194}
195
196static struct mptcp_pm_addr_entry *
197select_signal_address(struct pm_nl_pernet *pernet, unsigned int pos)
198{
199 struct mptcp_pm_addr_entry *entry, *ret = NULL;
200 int i = 0;
201
202 rcu_read_lock();
203 /* do not keep any additional per socket state, just signal
204 * the address list in order.
205 * Note: removal from the local address list during the msk life-cycle
206 * can lead to additional addresses not being announced.
207 */
208 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
Paolo Abenief0da3b2020-09-14 10:01:15 +0200209 if (!(entry->addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL))
Paolo Abeni01cacb02020-03-27 14:48:51 -0700210 continue;
211 if (i++ == pos) {
212 ret = entry;
213 break;
214 }
215 }
216 rcu_read_unlock();
217 return ret;
218}
219
Geliang Tanga914e582021-02-01 15:09:07 -0800220unsigned int mptcp_pm_get_add_addr_signal_max(struct mptcp_sock *msk)
221{
222 struct pm_nl_pernet *pernet;
223
224 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
225 return READ_ONCE(pernet->add_addr_signal_max);
226}
227EXPORT_SYMBOL_GPL(mptcp_pm_get_add_addr_signal_max);
228
229unsigned int mptcp_pm_get_add_addr_accept_max(struct mptcp_sock *msk)
230{
231 struct pm_nl_pernet *pernet;
232
233 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
234 return READ_ONCE(pernet->add_addr_accept_max);
235}
236EXPORT_SYMBOL_GPL(mptcp_pm_get_add_addr_accept_max);
237
238unsigned int mptcp_pm_get_subflows_max(struct mptcp_sock *msk)
239{
240 struct pm_nl_pernet *pernet;
241
242 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
243 return READ_ONCE(pernet->subflows_max);
244}
245EXPORT_SYMBOL_GPL(mptcp_pm_get_subflows_max);
246
Geliang Tang0caf3ad2021-02-12 16:52:02 -0800247unsigned int mptcp_pm_get_local_addr_max(struct mptcp_sock *msk)
Geliang Tanga914e582021-02-01 15:09:07 -0800248{
249 struct pm_nl_pernet *pernet;
250
251 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
252 return READ_ONCE(pernet->local_addr_max);
253}
Geliang Tang0caf3ad2021-02-12 16:52:02 -0800254EXPORT_SYMBOL_GPL(mptcp_pm_get_local_addr_max);
Geliang Tanga914e582021-02-01 15:09:07 -0800255
Paolo Abeni01cacb02020-03-27 14:48:51 -0700256static void check_work_pending(struct mptcp_sock *msk)
257{
Geliang Tanga914e582021-02-01 15:09:07 -0800258 if (msk->pm.add_addr_signaled == mptcp_pm_get_add_addr_signal_max(msk) &&
259 (msk->pm.local_addr_used == mptcp_pm_get_local_addr_max(msk) ||
260 msk->pm.subflows == mptcp_pm_get_subflows_max(msk)))
Paolo Abeni01cacb02020-03-27 14:48:51 -0700261 WRITE_ONCE(msk->pm.work_pending, false);
262}
263
Geliang Tangd88c4762021-03-26 11:26:37 -0700264struct mptcp_pm_add_entry *
265mptcp_lookup_anno_list_by_saddr(struct mptcp_sock *msk,
266 struct mptcp_addr_info *addr)
Geliang Tangb6c08382020-09-24 08:29:54 +0800267{
Geliang Tang0abd40f2020-09-24 08:30:00 +0800268 struct mptcp_pm_add_entry *entry;
Geliang Tangb6c08382020-09-24 08:29:54 +0800269
Florian Westphal3abc05d2021-02-04 15:23:30 -0800270 lockdep_assert_held(&msk->pm.lock);
271
Geliang Tangb6c08382020-09-24 08:29:54 +0800272 list_for_each_entry(entry, &msk->pm.anno_list, list) {
Geliang Tang60b57bf2021-02-01 15:09:16 -0800273 if (addresses_equal(&entry->addr, addr, true))
Geliang Tang00cfd772020-09-24 08:30:02 +0800274 return entry;
Geliang Tangb6c08382020-09-24 08:29:54 +0800275 }
276
Geliang Tang00cfd772020-09-24 08:30:02 +0800277 return NULL;
278}
279
Geliang Tang5bc56382021-02-01 15:09:15 -0800280bool mptcp_pm_sport_in_anno_list(struct mptcp_sock *msk, const struct sock *sk)
281{
282 struct mptcp_pm_add_entry *entry;
283 struct mptcp_addr_info saddr;
284 bool ret = false;
285
286 local_address((struct sock_common *)sk, &saddr);
287
288 spin_lock_bh(&msk->pm.lock);
289 list_for_each_entry(entry, &msk->pm.anno_list, list) {
290 if (addresses_equal(&entry->addr, &saddr, true)) {
291 ret = true;
292 goto out;
293 }
294 }
295
296out:
297 spin_unlock_bh(&msk->pm.lock);
298 return ret;
299}
300
Geliang Tang00cfd772020-09-24 08:30:02 +0800301static void mptcp_pm_add_timer(struct timer_list *timer)
302{
303 struct mptcp_pm_add_entry *entry = from_timer(entry, timer, add_timer);
304 struct mptcp_sock *msk = entry->sock;
305 struct sock *sk = (struct sock *)msk;
306
307 pr_debug("msk=%p", msk);
308
309 if (!msk)
310 return;
311
312 if (inet_sk_state_load(sk) == TCP_CLOSE)
313 return;
314
315 if (!entry->addr.id)
316 return;
317
318 if (mptcp_pm_should_add_signal(msk)) {
319 sk_reset_timer(sk, timer, jiffies + TCP_RTO_MAX / 8);
320 goto out;
321 }
322
323 spin_lock_bh(&msk->pm.lock);
324
325 if (!mptcp_pm_should_add_signal(msk)) {
326 pr_debug("retransmit ADD_ADDR id=%d", entry->addr.id);
Geliang Tangf7efc772021-03-26 11:26:31 -0700327 mptcp_pm_announce_addr(msk, &entry->addr, false);
Geliang Tang84dfe362020-11-19 11:46:00 -0800328 mptcp_pm_add_addr_send_ack(msk);
Geliang Tang00cfd772020-09-24 08:30:02 +0800329 entry->retrans_times++;
330 }
331
332 if (entry->retrans_times < ADD_ADDR_RETRANS_MAX)
Geliang Tang93f323b2020-11-03 11:05:07 -0800333 sk_reset_timer(sk, timer,
Geliang Tang724d06b2020-11-10 11:01:43 +0800334 jiffies + mptcp_get_add_addr_timeout(sock_net(sk)));
Geliang Tang00cfd772020-09-24 08:30:02 +0800335
336 spin_unlock_bh(&msk->pm.lock);
337
Geliang Tang348d5c12021-03-26 11:26:34 -0700338 if (entry->retrans_times == ADD_ADDR_RETRANS_MAX)
339 mptcp_pm_subflow_established(msk);
340
Geliang Tang00cfd772020-09-24 08:30:02 +0800341out:
342 __sock_put(sk);
343}
344
345struct mptcp_pm_add_entry *
346mptcp_pm_del_add_timer(struct mptcp_sock *msk,
347 struct mptcp_addr_info *addr)
348{
349 struct mptcp_pm_add_entry *entry;
350 struct sock *sk = (struct sock *)msk;
351
352 spin_lock_bh(&msk->pm.lock);
Geliang Tangd88c4762021-03-26 11:26:37 -0700353 entry = mptcp_lookup_anno_list_by_saddr(msk, addr);
Geliang Tang00cfd772020-09-24 08:30:02 +0800354 if (entry)
355 entry->retrans_times = ADD_ADDR_RETRANS_MAX;
356 spin_unlock_bh(&msk->pm.lock);
357
358 if (entry)
359 sk_stop_timer_sync(sk, &entry->add_timer);
360
361 return entry;
Geliang Tangb6c08382020-09-24 08:29:54 +0800362}
363
364static bool mptcp_pm_alloc_anno_list(struct mptcp_sock *msk,
365 struct mptcp_pm_addr_entry *entry)
366{
Geliang Tang0abd40f2020-09-24 08:30:00 +0800367 struct mptcp_pm_add_entry *add_entry = NULL;
Geliang Tang00cfd772020-09-24 08:30:02 +0800368 struct sock *sk = (struct sock *)msk;
Geliang Tang93f323b2020-11-03 11:05:07 -0800369 struct net *net = sock_net(sk);
Geliang Tangb6c08382020-09-24 08:29:54 +0800370
Florian Westphal3abc05d2021-02-04 15:23:30 -0800371 lockdep_assert_held(&msk->pm.lock);
372
Geliang Tangd88c4762021-03-26 11:26:37 -0700373 if (mptcp_lookup_anno_list_by_saddr(msk, &entry->addr))
Geliang Tangb6c08382020-09-24 08:29:54 +0800374 return false;
375
Geliang Tang0abd40f2020-09-24 08:30:00 +0800376 add_entry = kmalloc(sizeof(*add_entry), GFP_ATOMIC);
377 if (!add_entry)
Geliang Tangb6c08382020-09-24 08:29:54 +0800378 return false;
379
Geliang Tang0abd40f2020-09-24 08:30:00 +0800380 list_add(&add_entry->list, &msk->pm.anno_list);
Geliang Tangb6c08382020-09-24 08:29:54 +0800381
Geliang Tang00cfd772020-09-24 08:30:02 +0800382 add_entry->addr = entry->addr;
383 add_entry->sock = msk;
384 add_entry->retrans_times = 0;
385
386 timer_setup(&add_entry->add_timer, mptcp_pm_add_timer, 0);
Geliang Tang93f323b2020-11-03 11:05:07 -0800387 sk_reset_timer(sk, &add_entry->add_timer,
388 jiffies + mptcp_get_add_addr_timeout(net));
Geliang Tang00cfd772020-09-24 08:30:02 +0800389
Geliang Tangb6c08382020-09-24 08:29:54 +0800390 return true;
391}
392
393void mptcp_pm_free_anno_list(struct mptcp_sock *msk)
394{
Geliang Tang0abd40f2020-09-24 08:30:00 +0800395 struct mptcp_pm_add_entry *entry, *tmp;
Geliang Tang00cfd772020-09-24 08:30:02 +0800396 struct sock *sk = (struct sock *)msk;
397 LIST_HEAD(free_list);
Geliang Tangb6c08382020-09-24 08:29:54 +0800398
399 pr_debug("msk=%p", msk);
400
401 spin_lock_bh(&msk->pm.lock);
Geliang Tang00cfd772020-09-24 08:30:02 +0800402 list_splice_init(&msk->pm.anno_list, &free_list);
403 spin_unlock_bh(&msk->pm.lock);
404
405 list_for_each_entry_safe(entry, tmp, &free_list, list) {
406 sk_stop_timer_sync(sk, &entry->add_timer);
Geliang Tangb6c08382020-09-24 08:29:54 +0800407 kfree(entry);
408 }
Geliang Tangb6c08382020-09-24 08:29:54 +0800409}
410
Paolo Abeni01cacb02020-03-27 14:48:51 -0700411static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk)
412{
413 struct sock *sk = (struct sock *)msk;
414 struct mptcp_pm_addr_entry *local;
Geliang Tanga914e582021-02-01 15:09:07 -0800415 unsigned int add_addr_signal_max;
416 unsigned int local_addr_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700417 struct pm_nl_pernet *pernet;
Geliang Tanga914e582021-02-01 15:09:07 -0800418 unsigned int subflows_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700419
Geliang Tang432d9e72020-12-09 15:51:28 -0800420 pernet = net_generic(sock_net(sk), pm_nl_pernet_id);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700421
Geliang Tanga914e582021-02-01 15:09:07 -0800422 add_addr_signal_max = mptcp_pm_get_add_addr_signal_max(msk);
423 local_addr_max = mptcp_pm_get_local_addr_max(msk);
424 subflows_max = mptcp_pm_get_subflows_max(msk);
425
Paolo Abeni01cacb02020-03-27 14:48:51 -0700426 pr_debug("local %d:%d signal %d:%d subflows %d:%d\n",
Geliang Tanga914e582021-02-01 15:09:07 -0800427 msk->pm.local_addr_used, local_addr_max,
428 msk->pm.add_addr_signaled, add_addr_signal_max,
429 msk->pm.subflows, subflows_max);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700430
431 /* check first for announce */
Geliang Tanga914e582021-02-01 15:09:07 -0800432 if (msk->pm.add_addr_signaled < add_addr_signal_max) {
Paolo Abeni01cacb02020-03-27 14:48:51 -0700433 local = select_signal_address(pernet,
434 msk->pm.add_addr_signaled);
435
436 if (local) {
Geliang Tangb6c08382020-09-24 08:29:54 +0800437 if (mptcp_pm_alloc_anno_list(msk, local)) {
438 msk->pm.add_addr_signaled++;
Geliang Tangf7efc772021-03-26 11:26:31 -0700439 mptcp_pm_announce_addr(msk, &local->addr, false);
Geliang Tang84dfe362020-11-19 11:46:00 -0800440 mptcp_pm_nl_add_addr_send_ack(msk);
Geliang Tangb6c08382020-09-24 08:29:54 +0800441 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700442 } else {
443 /* pick failed, avoid fourther attempts later */
Geliang Tanga914e582021-02-01 15:09:07 -0800444 msk->pm.local_addr_used = add_addr_signal_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700445 }
446
447 check_work_pending(msk);
448 }
449
450 /* check if should create a new subflow */
Geliang Tanga914e582021-02-01 15:09:07 -0800451 if (msk->pm.local_addr_used < local_addr_max &&
452 msk->pm.subflows < subflows_max) {
Paolo Abeni01cacb02020-03-27 14:48:51 -0700453 local = select_local_address(pernet, msk);
454 if (local) {
Matthieu Baerts1f2f1932021-01-25 10:59:02 -0800455 struct mptcp_addr_info remote = { 0 };
456
Paolo Abeni01cacb02020-03-27 14:48:51 -0700457 msk->pm.local_addr_used++;
458 msk->pm.subflows++;
459 check_work_pending(msk);
Matthieu Baerts1f2f1932021-01-25 10:59:02 -0800460 remote_address((struct sock_common *)sk, &remote);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700461 spin_unlock_bh(&msk->pm.lock);
Paolo Abenief0da3b2020-09-14 10:01:15 +0200462 __mptcp_subflow_connect(sk, &local->addr, &remote);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700463 spin_lock_bh(&msk->pm.lock);
464 return;
465 }
466
467 /* lookup failed, avoid fourther attempts later */
Geliang Tanga914e582021-02-01 15:09:07 -0800468 msk->pm.local_addr_used = local_addr_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700469 check_work_pending(msk);
470 }
471}
472
Florian Westphale9801432021-02-12 15:59:54 -0800473static void mptcp_pm_nl_fully_established(struct mptcp_sock *msk)
Paolo Abeni01cacb02020-03-27 14:48:51 -0700474{
475 mptcp_pm_create_subflow_or_signal_addr(msk);
476}
477
Florian Westphale9801432021-02-12 15:59:54 -0800478static void mptcp_pm_nl_subflow_established(struct mptcp_sock *msk)
Paolo Abeni01cacb02020-03-27 14:48:51 -0700479{
480 mptcp_pm_create_subflow_or_signal_addr(msk);
481}
482
Florian Westphale9801432021-02-12 15:59:54 -0800483static void mptcp_pm_nl_add_addr_received(struct mptcp_sock *msk)
Paolo Abeni01cacb02020-03-27 14:48:51 -0700484{
485 struct sock *sk = (struct sock *)msk;
Geliang Tanga914e582021-02-01 15:09:07 -0800486 unsigned int add_addr_accept_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700487 struct mptcp_addr_info remote;
488 struct mptcp_addr_info local;
Geliang Tanga914e582021-02-01 15:09:07 -0800489 unsigned int subflows_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700490
Geliang Tanga914e582021-02-01 15:09:07 -0800491 add_addr_accept_max = mptcp_pm_get_add_addr_accept_max(msk);
492 subflows_max = mptcp_pm_get_subflows_max(msk);
493
Paolo Abeni01cacb02020-03-27 14:48:51 -0700494 pr_debug("accepted %d:%d remote family %d",
Geliang Tanga914e582021-02-01 15:09:07 -0800495 msk->pm.add_addr_accepted, add_addr_accept_max,
Paolo Abeni01cacb02020-03-27 14:48:51 -0700496 msk->pm.remote.family);
Geliang Tangd84ad042021-03-26 11:26:32 -0700497
498 if (lookup_subflow_by_daddr(&msk->conn_list, &msk->pm.remote))
499 goto add_addr_echo;
500
Paolo Abeni01cacb02020-03-27 14:48:51 -0700501 msk->pm.add_addr_accepted++;
502 msk->pm.subflows++;
Geliang Tanga914e582021-02-01 15:09:07 -0800503 if (msk->pm.add_addr_accepted >= add_addr_accept_max ||
504 msk->pm.subflows >= subflows_max)
Paolo Abeni01cacb02020-03-27 14:48:51 -0700505 WRITE_ONCE(msk->pm.accept_addr, false);
506
507 /* connect to the specified remote address, using whatever
508 * local address the routing configuration will pick.
509 */
510 remote = msk->pm.remote;
511 if (!remote.port)
512 remote.port = sk->sk_dport;
513 memset(&local, 0, sizeof(local));
514 local.family = remote.family;
515
516 spin_unlock_bh(&msk->pm.lock);
Geliang Tang432d9e72020-12-09 15:51:28 -0800517 __mptcp_subflow_connect(sk, &local, &remote);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700518 spin_lock_bh(&msk->pm.lock);
Geliang Tang6a6c05a2020-09-24 08:29:50 +0800519
Geliang Tangd84ad042021-03-26 11:26:32 -0700520add_addr_echo:
Geliang Tangf7efc772021-03-26 11:26:31 -0700521 mptcp_pm_announce_addr(msk, &msk->pm.remote, true);
Geliang Tang84dfe362020-11-19 11:46:00 -0800522 mptcp_pm_nl_add_addr_send_ack(msk);
523}
524
Geliang Tang8dd5efb2021-03-26 11:26:40 -0700525void mptcp_pm_nl_add_addr_send_ack(struct mptcp_sock *msk)
Geliang Tang84dfe362020-11-19 11:46:00 -0800526{
527 struct mptcp_subflow_context *subflow;
528
Florian Westphal3abc05d2021-02-04 15:23:30 -0800529 msk_owned_by_me(msk);
530 lockdep_assert_held(&msk->pm.lock);
531
Geliang Tang8dd5efb2021-03-26 11:26:40 -0700532 if (!mptcp_pm_should_add_signal(msk) &&
533 !mptcp_pm_should_rm_signal(msk))
Geliang Tang84dfe362020-11-19 11:46:00 -0800534 return;
535
536 __mptcp_flush_join_list(msk);
537 subflow = list_first_entry_or_null(&msk->conn_list, typeof(*subflow), node);
538 if (subflow) {
539 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
Geliang Tang84dfe362020-11-19 11:46:00 -0800540
541 spin_unlock_bh(&msk->pm.lock);
Geliang Tang8dd5efb2021-03-26 11:26:40 -0700542 pr_debug("send ack for %s%s%s",
543 mptcp_pm_should_add_signal(msk) ? "add_addr" : "rm_addr",
Geliang Tangb5a7acd2021-02-01 15:09:09 -0800544 mptcp_pm_should_add_signal_ipv6(msk) ? " [ipv6]" : "",
545 mptcp_pm_should_add_signal_port(msk) ? " [port]" : "");
Geliang Tangfbe0f872020-12-09 15:51:23 -0800546
Geliang Tang84dfe362020-11-19 11:46:00 -0800547 lock_sock(ssk);
548 tcp_send_ack(ssk);
549 release_sock(ssk);
550 spin_lock_bh(&msk->pm.lock);
Geliang Tang84dfe362020-11-19 11:46:00 -0800551 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700552}
553
Geliang Tang06706542021-01-08 16:47:57 -0800554int mptcp_pm_nl_mp_prio_send_ack(struct mptcp_sock *msk,
555 struct mptcp_addr_info *addr,
556 u8 bkup)
557{
558 struct mptcp_subflow_context *subflow;
559
560 pr_debug("bkup=%d", bkup);
561
562 mptcp_for_each_subflow(msk, subflow) {
563 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
Geliang Tang0be2ac22021-01-08 16:48:01 -0800564 struct sock *sk = (struct sock *)msk;
Geliang Tang06706542021-01-08 16:47:57 -0800565 struct mptcp_addr_info local;
566
567 local_address((struct sock_common *)ssk, &local);
568 if (!addresses_equal(&local, addr, addr->port))
569 continue;
570
571 subflow->backup = bkup;
572 subflow->send_mp_prio = 1;
573 subflow->request_bkup = bkup;
Geliang Tang0be2ac22021-01-08 16:48:01 -0800574 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIOTX);
Geliang Tang06706542021-01-08 16:47:57 -0800575
576 spin_unlock_bh(&msk->pm.lock);
577 pr_debug("send ack for mp_prio");
578 lock_sock(ssk);
579 tcp_send_ack(ssk);
580 release_sock(ssk);
581 spin_lock_bh(&msk->pm.lock);
582
583 return 0;
584 }
585
586 return -EINVAL;
587}
588
Florian Westphale9801432021-02-12 15:59:54 -0800589static void mptcp_pm_nl_rm_addr_received(struct mptcp_sock *msk)
Geliang Tangd0876b22020-09-24 08:29:49 +0800590{
591 struct mptcp_subflow_context *subflow, *tmp;
592 struct sock *sk = (struct sock *)msk;
Geliang Tangd0b698c2021-03-12 17:16:15 -0800593 u8 i;
Geliang Tangd0876b22020-09-24 08:29:49 +0800594
Geliang Tangd0b698c2021-03-12 17:16:15 -0800595 pr_debug("address rm_list_nr %d", msk->pm.rm_list_rx.nr);
Geliang Tangd0876b22020-09-24 08:29:49 +0800596
Florian Westphal3abc05d2021-02-04 15:23:30 -0800597 msk_owned_by_me(msk);
598
Geliang Tangd0b698c2021-03-12 17:16:15 -0800599 if (!msk->pm.rm_list_rx.nr)
Geliang Tangd0876b22020-09-24 08:29:49 +0800600 return;
601
602 if (list_empty(&msk->conn_list))
603 return;
604
Geliang Tangd0b698c2021-03-12 17:16:15 -0800605 for (i = 0; i < msk->pm.rm_list_rx.nr; i++) {
606 list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
607 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
608 int how = RCV_SHUTDOWN | SEND_SHUTDOWN;
Geliang Tangd0876b22020-09-24 08:29:49 +0800609
Geliang Tangd0b698c2021-03-12 17:16:15 -0800610 if (msk->pm.rm_list_rx.ids[i] != subflow->remote_id)
611 continue;
Geliang Tangd0876b22020-09-24 08:29:49 +0800612
Geliang Tangd0b698c2021-03-12 17:16:15 -0800613 pr_debug(" -> address rm_list_ids[%d]=%u", i, msk->pm.rm_list_rx.ids[i]);
614 spin_unlock_bh(&msk->pm.lock);
615 mptcp_subflow_shutdown(sk, ssk, how);
616 mptcp_close_ssk(sk, ssk, subflow);
617 spin_lock_bh(&msk->pm.lock);
Geliang Tangd0876b22020-09-24 08:29:49 +0800618
Geliang Tangd0b698c2021-03-12 17:16:15 -0800619 msk->pm.add_addr_accepted--;
620 msk->pm.subflows--;
621 WRITE_ONCE(msk->pm.accept_addr, true);
Geliang Tangd0876b22020-09-24 08:29:49 +0800622
Geliang Tangd0b698c2021-03-12 17:16:15 -0800623 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMADDR);
Geliang Tang7a7e52e2020-09-24 08:29:56 +0800624
Geliang Tangd0b698c2021-03-12 17:16:15 -0800625 break;
626 }
Geliang Tangd0876b22020-09-24 08:29:49 +0800627 }
628}
629
Florian Westphale9801432021-02-12 15:59:54 -0800630void mptcp_pm_nl_work(struct mptcp_sock *msk)
631{
632 struct mptcp_pm_data *pm = &msk->pm;
633
634 msk_owned_by_me(msk);
635
636 spin_lock_bh(&msk->pm.lock);
637
638 pr_debug("msk=%p status=%x", msk, pm->status);
639 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_RECEIVED)) {
640 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_RECEIVED);
641 mptcp_pm_nl_add_addr_received(msk);
642 }
643 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_SEND_ACK)) {
644 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_SEND_ACK);
645 mptcp_pm_nl_add_addr_send_ack(msk);
646 }
647 if (pm->status & BIT(MPTCP_PM_RM_ADDR_RECEIVED)) {
648 pm->status &= ~BIT(MPTCP_PM_RM_ADDR_RECEIVED);
649 mptcp_pm_nl_rm_addr_received(msk);
650 }
651 if (pm->status & BIT(MPTCP_PM_ESTABLISHED)) {
652 pm->status &= ~BIT(MPTCP_PM_ESTABLISHED);
653 mptcp_pm_nl_fully_established(msk);
654 }
655 if (pm->status & BIT(MPTCP_PM_SUBFLOW_ESTABLISHED)) {
656 pm->status &= ~BIT(MPTCP_PM_SUBFLOW_ESTABLISHED);
657 mptcp_pm_nl_subflow_established(msk);
658 }
659
660 spin_unlock_bh(&msk->pm.lock);
661}
662
Geliang Tangddd14bb2021-03-12 17:16:16 -0800663void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk,
664 const struct mptcp_rm_list *rm_list)
Geliang Tang0ee42612020-09-24 08:29:55 +0800665{
666 struct mptcp_subflow_context *subflow, *tmp;
667 struct sock *sk = (struct sock *)msk;
Geliang Tangddd14bb2021-03-12 17:16:16 -0800668 u8 i;
Geliang Tang0ee42612020-09-24 08:29:55 +0800669
Geliang Tangddd14bb2021-03-12 17:16:16 -0800670 pr_debug("subflow rm_list_nr %d", rm_list->nr);
Geliang Tang0ee42612020-09-24 08:29:55 +0800671
Florian Westphal3abc05d2021-02-04 15:23:30 -0800672 msk_owned_by_me(msk);
673
Geliang Tangddd14bb2021-03-12 17:16:16 -0800674 if (!rm_list->nr)
Geliang Tang0ee42612020-09-24 08:29:55 +0800675 return;
676
677 if (list_empty(&msk->conn_list))
678 return;
679
Geliang Tangddd14bb2021-03-12 17:16:16 -0800680 for (i = 0; i < rm_list->nr; i++) {
681 list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
682 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
683 int how = RCV_SHUTDOWN | SEND_SHUTDOWN;
Geliang Tang0ee42612020-09-24 08:29:55 +0800684
Geliang Tangddd14bb2021-03-12 17:16:16 -0800685 if (rm_list->ids[i] != subflow->local_id)
686 continue;
Geliang Tang0ee42612020-09-24 08:29:55 +0800687
Geliang Tangddd14bb2021-03-12 17:16:16 -0800688 pr_debug(" -> subflow rm_list_ids[%d]=%u", i, rm_list->ids[i]);
689 spin_unlock_bh(&msk->pm.lock);
690 mptcp_subflow_shutdown(sk, ssk, how);
691 mptcp_close_ssk(sk, ssk, subflow);
692 spin_lock_bh(&msk->pm.lock);
Geliang Tang0ee42612020-09-24 08:29:55 +0800693
Geliang Tangddd14bb2021-03-12 17:16:16 -0800694 msk->pm.local_addr_used--;
695 msk->pm.subflows--;
Geliang Tang0ee42612020-09-24 08:29:55 +0800696
Geliang Tangddd14bb2021-03-12 17:16:16 -0800697 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMSUBFLOW);
Geliang Tang7a7e52e2020-09-24 08:29:56 +0800698
Geliang Tangddd14bb2021-03-12 17:16:16 -0800699 break;
700 }
Geliang Tang0ee42612020-09-24 08:29:55 +0800701 }
702}
703
Paolo Abeni01cacb02020-03-27 14:48:51 -0700704static bool address_use_port(struct mptcp_pm_addr_entry *entry)
705{
Paolo Abenief0da3b2020-09-14 10:01:15 +0200706 return (entry->addr.flags &
Paolo Abeni01cacb02020-03-27 14:48:51 -0700707 (MPTCP_PM_ADDR_FLAG_SIGNAL | MPTCP_PM_ADDR_FLAG_SUBFLOW)) ==
708 MPTCP_PM_ADDR_FLAG_SIGNAL;
709}
710
711static int mptcp_pm_nl_append_new_local_addr(struct pm_nl_pernet *pernet,
712 struct mptcp_pm_addr_entry *entry)
713{
714 struct mptcp_pm_addr_entry *cur;
Geliang Tang72603d22021-02-01 15:09:06 -0800715 unsigned int addr_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700716 int ret = -EINVAL;
717
718 spin_lock_bh(&pernet->lock);
719 /* to keep the code simple, don't do IDR-like allocation for address ID,
720 * just bail when we exceed limits
721 */
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800722 if (pernet->next_id == MAX_ADDR_ID)
723 pernet->next_id = 1;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700724 if (pernet->addrs >= MPTCP_PM_ADDR_MAX)
725 goto out;
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800726 if (test_bit(entry->addr.id, pernet->id_bitmap))
727 goto out;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700728
729 /* do not insert duplicate address, differentiate on port only
730 * singled addresses
731 */
732 list_for_each_entry(cur, &pernet->local_addr_list, list) {
733 if (addresses_equal(&cur->addr, &entry->addr,
734 address_use_port(entry) &&
735 address_use_port(cur)))
736 goto out;
737 }
738
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800739 if (!entry->addr.id) {
740find_next:
741 entry->addr.id = find_next_zero_bit(pernet->id_bitmap,
742 MAX_ADDR_ID + 1,
743 pernet->next_id);
744 if ((!entry->addr.id || entry->addr.id > MAX_ADDR_ID) &&
745 pernet->next_id != 1) {
746 pernet->next_id = 1;
747 goto find_next;
748 }
749 }
750
751 if (!entry->addr.id || entry->addr.id > MAX_ADDR_ID)
752 goto out;
753
754 __set_bit(entry->addr.id, pernet->id_bitmap);
755 if (entry->addr.id > pernet->next_id)
756 pernet->next_id = entry->addr.id;
757
Geliang Tang72603d22021-02-01 15:09:06 -0800758 if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
759 addr_max = pernet->add_addr_signal_max;
760 WRITE_ONCE(pernet->add_addr_signal_max, addr_max + 1);
761 }
762 if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) {
763 addr_max = pernet->local_addr_max;
764 WRITE_ONCE(pernet->local_addr_max, addr_max + 1);
765 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700766
Paolo Abeni01cacb02020-03-27 14:48:51 -0700767 pernet->addrs++;
768 list_add_tail_rcu(&entry->list, &pernet->local_addr_list);
769 ret = entry->addr.id;
770
771out:
772 spin_unlock_bh(&pernet->lock);
773 return ret;
774}
775
Geliang Tang1729cf12021-02-01 15:09:12 -0800776static int mptcp_pm_nl_create_listen_socket(struct sock *sk,
777 struct mptcp_pm_addr_entry *entry)
778{
779 struct sockaddr_storage addr;
780 struct mptcp_sock *msk;
781 struct socket *ssock;
782 int backlog = 1024;
783 int err;
784
785 err = sock_create_kern(sock_net(sk), entry->addr.family,
786 SOCK_STREAM, IPPROTO_MPTCP, &entry->lsk);
787 if (err)
788 return err;
789
790 msk = mptcp_sk(entry->lsk->sk);
791 if (!msk) {
792 err = -EINVAL;
793 goto out;
794 }
795
796 ssock = __mptcp_nmpc_socket(msk);
797 if (!ssock) {
798 err = -EINVAL;
799 goto out;
800 }
801
802 mptcp_info2sockaddr(&entry->addr, &addr, entry->addr.family);
803 err = kernel_bind(ssock, (struct sockaddr *)&addr,
804 sizeof(struct sockaddr_in));
805 if (err) {
806 pr_warn("kernel_bind error, err=%d", err);
807 goto out;
808 }
809
810 err = kernel_listen(ssock, backlog);
811 if (err) {
812 pr_warn("kernel_listen error, err=%d", err);
813 goto out;
814 }
815
816 return 0;
817
818out:
819 sock_release(entry->lsk);
820 return err;
821}
822
Paolo Abeni01cacb02020-03-27 14:48:51 -0700823int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc)
824{
825 struct mptcp_pm_addr_entry *entry;
826 struct mptcp_addr_info skc_local;
827 struct mptcp_addr_info msk_local;
828 struct pm_nl_pernet *pernet;
829 int ret = -1;
830
831 if (WARN_ON_ONCE(!msk))
832 return -1;
833
834 /* The 0 ID mapping is defined by the first subflow, copied into the msk
835 * addr
836 */
837 local_address((struct sock_common *)msk, &msk_local);
Geliang Tang57025812020-09-08 10:49:38 +0800838 local_address((struct sock_common *)skc, &skc_local);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700839 if (addresses_equal(&msk_local, &skc_local, false))
840 return 0;
841
Geliang Tang57025812020-09-08 10:49:38 +0800842 if (address_zero(&skc_local))
843 return 0;
844
Paolo Abeni01cacb02020-03-27 14:48:51 -0700845 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
846
847 rcu_read_lock();
848 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
Geliang Tang60b57bf2021-02-01 15:09:16 -0800849 if (addresses_equal(&entry->addr, &skc_local, entry->addr.port)) {
Paolo Abeni01cacb02020-03-27 14:48:51 -0700850 ret = entry->addr.id;
851 break;
852 }
853 }
854 rcu_read_unlock();
855 if (ret >= 0)
856 return ret;
857
858 /* address not found, add to local list */
Geliang Tangf612eb72020-09-09 11:01:24 +0800859 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700860 if (!entry)
861 return -ENOMEM;
862
Paolo Abeni01cacb02020-03-27 14:48:51 -0700863 entry->addr = skc_local;
Paolo Abenief0da3b2020-09-14 10:01:15 +0200864 entry->addr.ifindex = 0;
865 entry->addr.flags = 0;
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800866 entry->addr.id = 0;
Geliang Tang1729cf12021-02-01 15:09:12 -0800867 entry->addr.port = 0;
868 entry->lsk = NULL;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700869 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
870 if (ret < 0)
871 kfree(entry);
872
873 return ret;
874}
875
876void mptcp_pm_nl_data_init(struct mptcp_sock *msk)
877{
878 struct mptcp_pm_data *pm = &msk->pm;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700879 bool subflows;
880
Geliang Tanga914e582021-02-01 15:09:07 -0800881 subflows = !!mptcp_pm_get_subflows_max(msk);
882 WRITE_ONCE(pm->work_pending, (!!mptcp_pm_get_local_addr_max(msk) && subflows) ||
883 !!mptcp_pm_get_add_addr_signal_max(msk));
884 WRITE_ONCE(pm->accept_addr, !!mptcp_pm_get_add_addr_accept_max(msk) && subflows);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700885 WRITE_ONCE(pm->accept_subflow, subflows);
886}
887
Florian Westphalb911c972021-02-12 16:00:01 -0800888#define MPTCP_PM_CMD_GRP_OFFSET 0
889#define MPTCP_PM_EV_GRP_OFFSET 1
Paolo Abeni01cacb02020-03-27 14:48:51 -0700890
891static const struct genl_multicast_group mptcp_pm_mcgrps[] = {
892 [MPTCP_PM_CMD_GRP_OFFSET] = { .name = MPTCP_PM_CMD_GRP_NAME, },
Florian Westphalb911c972021-02-12 16:00:01 -0800893 [MPTCP_PM_EV_GRP_OFFSET] = { .name = MPTCP_PM_EV_GRP_NAME,
894 .flags = GENL_UNS_ADMIN_PERM,
895 },
Paolo Abeni01cacb02020-03-27 14:48:51 -0700896};
897
898static const struct nla_policy
899mptcp_pm_addr_policy[MPTCP_PM_ADDR_ATTR_MAX + 1] = {
900 [MPTCP_PM_ADDR_ATTR_FAMILY] = { .type = NLA_U16, },
901 [MPTCP_PM_ADDR_ATTR_ID] = { .type = NLA_U8, },
902 [MPTCP_PM_ADDR_ATTR_ADDR4] = { .type = NLA_U32, },
Johannes Berg81408602020-08-18 10:17:31 +0200903 [MPTCP_PM_ADDR_ATTR_ADDR6] =
904 NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
Paolo Abeni01cacb02020-03-27 14:48:51 -0700905 [MPTCP_PM_ADDR_ATTR_PORT] = { .type = NLA_U16 },
906 [MPTCP_PM_ADDR_ATTR_FLAGS] = { .type = NLA_U32 },
907 [MPTCP_PM_ADDR_ATTR_IF_IDX] = { .type = NLA_S32 },
908};
909
910static const struct nla_policy mptcp_pm_policy[MPTCP_PM_ATTR_MAX + 1] = {
911 [MPTCP_PM_ATTR_ADDR] =
912 NLA_POLICY_NESTED(mptcp_pm_addr_policy),
913 [MPTCP_PM_ATTR_RCV_ADD_ADDRS] = { .type = NLA_U32, },
914 [MPTCP_PM_ATTR_SUBFLOWS] = { .type = NLA_U32, },
915};
916
917static int mptcp_pm_family_to_addr(int family)
918{
919#if IS_ENABLED(CONFIG_MPTCP_IPV6)
920 if (family == AF_INET6)
921 return MPTCP_PM_ADDR_ATTR_ADDR6;
922#endif
923 return MPTCP_PM_ADDR_ATTR_ADDR4;
924}
925
926static int mptcp_pm_parse_addr(struct nlattr *attr, struct genl_info *info,
927 bool require_family,
928 struct mptcp_pm_addr_entry *entry)
929{
930 struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1];
931 int err, addr_addr;
932
933 if (!attr) {
934 GENL_SET_ERR_MSG(info, "missing address info");
935 return -EINVAL;
936 }
937
938 /* no validation needed - was already done via nested policy */
939 err = nla_parse_nested_deprecated(tb, MPTCP_PM_ADDR_ATTR_MAX, attr,
940 mptcp_pm_addr_policy, info->extack);
941 if (err)
942 return err;
943
944 memset(entry, 0, sizeof(*entry));
945 if (!tb[MPTCP_PM_ADDR_ATTR_FAMILY]) {
946 if (!require_family)
947 goto skip_family;
948
949 NL_SET_ERR_MSG_ATTR(info->extack, attr,
950 "missing family");
951 return -EINVAL;
952 }
953
954 entry->addr.family = nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_FAMILY]);
955 if (entry->addr.family != AF_INET
956#if IS_ENABLED(CONFIG_MPTCP_IPV6)
957 && entry->addr.family != AF_INET6
958#endif
959 ) {
960 NL_SET_ERR_MSG_ATTR(info->extack, attr,
961 "unknown address family");
962 return -EINVAL;
963 }
964 addr_addr = mptcp_pm_family_to_addr(entry->addr.family);
965 if (!tb[addr_addr]) {
966 NL_SET_ERR_MSG_ATTR(info->extack, attr,
967 "missing address data");
968 return -EINVAL;
969 }
970
971#if IS_ENABLED(CONFIG_MPTCP_IPV6)
972 if (entry->addr.family == AF_INET6)
973 entry->addr.addr6 = nla_get_in6_addr(tb[addr_addr]);
974 else
975#endif
976 entry->addr.addr.s_addr = nla_get_in_addr(tb[addr_addr]);
977
978skip_family:
Paolo Abenief0da3b2020-09-14 10:01:15 +0200979 if (tb[MPTCP_PM_ADDR_ATTR_IF_IDX]) {
980 u32 val = nla_get_s32(tb[MPTCP_PM_ADDR_ATTR_IF_IDX]);
981
982 entry->addr.ifindex = val;
983 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700984
985 if (tb[MPTCP_PM_ADDR_ATTR_ID])
986 entry->addr.id = nla_get_u8(tb[MPTCP_PM_ADDR_ATTR_ID]);
987
988 if (tb[MPTCP_PM_ADDR_ATTR_FLAGS])
Paolo Abenief0da3b2020-09-14 10:01:15 +0200989 entry->addr.flags = nla_get_u32(tb[MPTCP_PM_ADDR_ATTR_FLAGS]);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700990
Geliang Tanga77e9172021-02-01 15:09:17 -0800991 if (tb[MPTCP_PM_ADDR_ATTR_PORT])
992 entry->addr.port = htons(nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_PORT]));
993
Paolo Abeni01cacb02020-03-27 14:48:51 -0700994 return 0;
995}
996
997static struct pm_nl_pernet *genl_info_pm_nl(struct genl_info *info)
998{
999 return net_generic(genl_info_net(info), pm_nl_pernet_id);
1000}
1001
Geliang Tang875b76712021-02-01 15:09:08 -08001002static int mptcp_nl_add_subflow_or_signal_addr(struct net *net)
1003{
1004 struct mptcp_sock *msk;
1005 long s_slot = 0, s_num = 0;
1006
1007 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1008 struct sock *sk = (struct sock *)msk;
1009
1010 if (!READ_ONCE(msk->fully_established))
1011 goto next;
1012
1013 lock_sock(sk);
1014 spin_lock_bh(&msk->pm.lock);
1015 mptcp_pm_create_subflow_or_signal_addr(msk);
1016 spin_unlock_bh(&msk->pm.lock);
1017 release_sock(sk);
1018
1019next:
1020 sock_put(sk);
1021 cond_resched();
1022 }
1023
1024 return 0;
1025}
1026
Paolo Abeni01cacb02020-03-27 14:48:51 -07001027static int mptcp_nl_cmd_add_addr(struct sk_buff *skb, struct genl_info *info)
1028{
1029 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1030 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1031 struct mptcp_pm_addr_entry addr, *entry;
1032 int ret;
1033
1034 ret = mptcp_pm_parse_addr(attr, info, true, &addr);
1035 if (ret < 0)
1036 return ret;
1037
1038 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1039 if (!entry) {
1040 GENL_SET_ERR_MSG(info, "can't allocate addr");
1041 return -ENOMEM;
1042 }
1043
1044 *entry = addr;
Geliang Tang1729cf12021-02-01 15:09:12 -08001045 if (entry->addr.port) {
1046 ret = mptcp_pm_nl_create_listen_socket(skb->sk, entry);
1047 if (ret) {
1048 GENL_SET_ERR_MSG(info, "create listen socket error");
1049 kfree(entry);
1050 return ret;
1051 }
1052 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001053 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
1054 if (ret < 0) {
1055 GENL_SET_ERR_MSG(info, "too many addresses or duplicate one");
Geliang Tang1729cf12021-02-01 15:09:12 -08001056 if (entry->lsk)
1057 sock_release(entry->lsk);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001058 kfree(entry);
1059 return ret;
1060 }
1061
Geliang Tang875b76712021-02-01 15:09:08 -08001062 mptcp_nl_add_subflow_or_signal_addr(sock_net(skb->sk));
1063
Paolo Abeni01cacb02020-03-27 14:48:51 -07001064 return 0;
1065}
1066
1067static struct mptcp_pm_addr_entry *
1068__lookup_addr_by_id(struct pm_nl_pernet *pernet, unsigned int id)
1069{
1070 struct mptcp_pm_addr_entry *entry;
1071
1072 list_for_each_entry(entry, &pernet->local_addr_list, list) {
1073 if (entry->addr.id == id)
1074 return entry;
1075 }
1076 return NULL;
1077}
1078
Geliang Tangb6c08382020-09-24 08:29:54 +08001079static bool remove_anno_list_by_saddr(struct mptcp_sock *msk,
1080 struct mptcp_addr_info *addr)
1081{
Geliang Tang00cfd772020-09-24 08:30:02 +08001082 struct mptcp_pm_add_entry *entry;
Geliang Tangb6c08382020-09-24 08:29:54 +08001083
Geliang Tang00cfd772020-09-24 08:30:02 +08001084 entry = mptcp_pm_del_add_timer(msk, addr);
1085 if (entry) {
1086 list_del(&entry->list);
1087 kfree(entry);
1088 return true;
Geliang Tangb6c08382020-09-24 08:29:54 +08001089 }
1090
1091 return false;
1092}
1093
1094static bool mptcp_pm_remove_anno_addr(struct mptcp_sock *msk,
1095 struct mptcp_addr_info *addr,
1096 bool force)
1097{
Geliang Tangcbde2782021-03-12 17:16:12 -08001098 struct mptcp_rm_list list = { .nr = 0 };
Geliang Tangb6c08382020-09-24 08:29:54 +08001099 bool ret;
1100
Geliang Tangcbde2782021-03-12 17:16:12 -08001101 list.ids[list.nr++] = addr->id;
1102
Geliang Tangb6c08382020-09-24 08:29:54 +08001103 ret = remove_anno_list_by_saddr(msk, addr);
Geliang Tang00cfd772020-09-24 08:30:02 +08001104 if (ret || force) {
1105 spin_lock_bh(&msk->pm.lock);
Geliang Tangcbde2782021-03-12 17:16:12 -08001106 mptcp_pm_remove_addr(msk, &list);
Geliang Tang00cfd772020-09-24 08:30:02 +08001107 spin_unlock_bh(&msk->pm.lock);
1108 }
Geliang Tangb6c08382020-09-24 08:29:54 +08001109 return ret;
1110}
1111
1112static int mptcp_nl_remove_subflow_and_signal_addr(struct net *net,
1113 struct mptcp_addr_info *addr)
1114{
1115 struct mptcp_sock *msk;
1116 long s_slot = 0, s_num = 0;
Geliang Tangddd14bb2021-03-12 17:16:16 -08001117 struct mptcp_rm_list list = { .nr = 0 };
Geliang Tangb6c08382020-09-24 08:29:54 +08001118
1119 pr_debug("remove_id=%d", addr->id);
1120
Geliang Tangddd14bb2021-03-12 17:16:16 -08001121 list.ids[list.nr++] = addr->id;
1122
Geliang Tangb6c08382020-09-24 08:29:54 +08001123 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1124 struct sock *sk = (struct sock *)msk;
1125 bool remove_subflow;
1126
1127 if (list_empty(&msk->conn_list)) {
1128 mptcp_pm_remove_anno_addr(msk, addr, false);
1129 goto next;
1130 }
1131
1132 lock_sock(sk);
1133 remove_subflow = lookup_subflow_by_saddr(&msk->conn_list, addr);
1134 mptcp_pm_remove_anno_addr(msk, addr, remove_subflow);
1135 if (remove_subflow)
Geliang Tangddd14bb2021-03-12 17:16:16 -08001136 mptcp_pm_remove_subflow(msk, &list);
Geliang Tangb6c08382020-09-24 08:29:54 +08001137 release_sock(sk);
1138
1139next:
1140 sock_put(sk);
1141 cond_resched();
1142 }
1143
1144 return 0;
1145}
1146
Geliang Tang1729cf12021-02-01 15:09:12 -08001147struct addr_entry_release_work {
1148 struct rcu_work rwork;
1149 struct mptcp_pm_addr_entry *entry;
1150};
1151
1152static void mptcp_pm_release_addr_entry(struct work_struct *work)
1153{
1154 struct addr_entry_release_work *w;
1155 struct mptcp_pm_addr_entry *entry;
1156
1157 w = container_of(to_rcu_work(work), struct addr_entry_release_work, rwork);
1158 entry = w->entry;
1159 if (entry) {
1160 if (entry->lsk)
1161 sock_release(entry->lsk);
1162 kfree(entry);
1163 }
1164 kfree(w);
1165}
1166
1167static void mptcp_pm_free_addr_entry(struct mptcp_pm_addr_entry *entry)
1168{
1169 struct addr_entry_release_work *w;
1170
1171 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1172 if (w) {
1173 INIT_RCU_WORK(&w->rwork, mptcp_pm_release_addr_entry);
1174 w->entry = entry;
1175 queue_rcu_work(system_wq, &w->rwork);
1176 }
1177}
1178
Paolo Abeni01cacb02020-03-27 14:48:51 -07001179static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info)
1180{
1181 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1182 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1183 struct mptcp_pm_addr_entry addr, *entry;
Geliang Tang72603d22021-02-01 15:09:06 -08001184 unsigned int addr_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001185 int ret;
1186
1187 ret = mptcp_pm_parse_addr(attr, info, false, &addr);
1188 if (ret < 0)
1189 return ret;
1190
1191 spin_lock_bh(&pernet->lock);
1192 entry = __lookup_addr_by_id(pernet, addr.addr.id);
1193 if (!entry) {
1194 GENL_SET_ERR_MSG(info, "address not found");
Geliang Tangb6c08382020-09-24 08:29:54 +08001195 spin_unlock_bh(&pernet->lock);
1196 return -EINVAL;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001197 }
Geliang Tang72603d22021-02-01 15:09:06 -08001198 if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
1199 addr_max = pernet->add_addr_signal_max;
1200 WRITE_ONCE(pernet->add_addr_signal_max, addr_max - 1);
1201 }
1202 if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) {
1203 addr_max = pernet->local_addr_max;
1204 WRITE_ONCE(pernet->local_addr_max, addr_max - 1);
1205 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001206
1207 pernet->addrs--;
1208 list_del_rcu(&entry->list);
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001209 __clear_bit(entry->addr.id, pernet->id_bitmap);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001210 spin_unlock_bh(&pernet->lock);
Geliang Tangb6c08382020-09-24 08:29:54 +08001211
1212 mptcp_nl_remove_subflow_and_signal_addr(sock_net(skb->sk), &entry->addr);
Geliang Tang1729cf12021-02-01 15:09:12 -08001213 mptcp_pm_free_addr_entry(entry);
Geliang Tangb6c08382020-09-24 08:29:54 +08001214
Paolo Abeni01cacb02020-03-27 14:48:51 -07001215 return ret;
1216}
1217
Geliang Tang06faa222021-03-12 17:16:17 -08001218static void mptcp_pm_remove_addrs_and_subflows(struct mptcp_sock *msk,
1219 struct list_head *rm_list)
1220{
1221 struct mptcp_rm_list alist = { .nr = 0 }, slist = { .nr = 0 };
1222 struct mptcp_pm_addr_entry *entry;
1223
1224 list_for_each_entry(entry, rm_list, list) {
1225 if (lookup_subflow_by_saddr(&msk->conn_list, &entry->addr) &&
1226 alist.nr < MPTCP_RM_IDS_MAX &&
1227 slist.nr < MPTCP_RM_IDS_MAX) {
1228 alist.ids[alist.nr++] = entry->addr.id;
1229 slist.ids[slist.nr++] = entry->addr.id;
1230 } else if (remove_anno_list_by_saddr(msk, &entry->addr) &&
1231 alist.nr < MPTCP_RM_IDS_MAX) {
1232 alist.ids[alist.nr++] = entry->addr.id;
1233 }
1234 }
1235
1236 if (alist.nr) {
1237 spin_lock_bh(&msk->pm.lock);
1238 mptcp_pm_remove_addr(msk, &alist);
1239 spin_unlock_bh(&msk->pm.lock);
1240 }
1241 if (slist.nr)
1242 mptcp_pm_remove_subflow(msk, &slist);
1243}
1244
1245static void mptcp_nl_remove_addrs_list(struct net *net,
1246 struct list_head *rm_list)
1247{
1248 long s_slot = 0, s_num = 0;
1249 struct mptcp_sock *msk;
1250
1251 if (list_empty(rm_list))
1252 return;
1253
1254 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1255 struct sock *sk = (struct sock *)msk;
1256
1257 lock_sock(sk);
1258 mptcp_pm_remove_addrs_and_subflows(msk, rm_list);
1259 release_sock(sk);
1260
1261 sock_put(sk);
1262 cond_resched();
1263 }
1264}
1265
Geliang Tang0e4a3e62021-03-12 17:16:18 -08001266static void __flush_addrs(struct list_head *list)
Paolo Abeni01cacb02020-03-27 14:48:51 -07001267{
Geliang Tang141694d2020-12-10 14:24:59 -08001268 while (!list_empty(list)) {
Paolo Abeni01cacb02020-03-27 14:48:51 -07001269 struct mptcp_pm_addr_entry *cur;
1270
Geliang Tang141694d2020-12-10 14:24:59 -08001271 cur = list_entry(list->next,
Paolo Abeni01cacb02020-03-27 14:48:51 -07001272 struct mptcp_pm_addr_entry, list);
1273 list_del_rcu(&cur->list);
Geliang Tang1729cf12021-02-01 15:09:12 -08001274 mptcp_pm_free_addr_entry(cur);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001275 }
1276}
1277
1278static void __reset_counters(struct pm_nl_pernet *pernet)
1279{
Geliang Tang72603d22021-02-01 15:09:06 -08001280 WRITE_ONCE(pernet->add_addr_signal_max, 0);
1281 WRITE_ONCE(pernet->add_addr_accept_max, 0);
1282 WRITE_ONCE(pernet->local_addr_max, 0);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001283 pernet->addrs = 0;
1284}
1285
1286static int mptcp_nl_cmd_flush_addrs(struct sk_buff *skb, struct genl_info *info)
1287{
1288 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
Geliang Tang141694d2020-12-10 14:24:59 -08001289 LIST_HEAD(free_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001290
1291 spin_lock_bh(&pernet->lock);
Geliang Tang141694d2020-12-10 14:24:59 -08001292 list_splice_init(&pernet->local_addr_list, &free_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001293 __reset_counters(pernet);
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001294 pernet->next_id = 1;
1295 bitmap_zero(pernet->id_bitmap, MAX_ADDR_ID + 1);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001296 spin_unlock_bh(&pernet->lock);
Geliang Tang0e4a3e62021-03-12 17:16:18 -08001297 mptcp_nl_remove_addrs_list(sock_net(skb->sk), &free_list);
1298 __flush_addrs(&free_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001299 return 0;
1300}
1301
1302static int mptcp_nl_fill_addr(struct sk_buff *skb,
1303 struct mptcp_pm_addr_entry *entry)
1304{
1305 struct mptcp_addr_info *addr = &entry->addr;
1306 struct nlattr *attr;
1307
1308 attr = nla_nest_start(skb, MPTCP_PM_ATTR_ADDR);
1309 if (!attr)
1310 return -EMSGSIZE;
1311
1312 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_FAMILY, addr->family))
1313 goto nla_put_failure;
Geliang Tanga77e9172021-02-01 15:09:17 -08001314 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_PORT, ntohs(addr->port)))
1315 goto nla_put_failure;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001316 if (nla_put_u8(skb, MPTCP_PM_ADDR_ATTR_ID, addr->id))
1317 goto nla_put_failure;
Paolo Abenief0da3b2020-09-14 10:01:15 +02001318 if (nla_put_u32(skb, MPTCP_PM_ADDR_ATTR_FLAGS, entry->addr.flags))
Paolo Abeni01cacb02020-03-27 14:48:51 -07001319 goto nla_put_failure;
Paolo Abenief0da3b2020-09-14 10:01:15 +02001320 if (entry->addr.ifindex &&
1321 nla_put_s32(skb, MPTCP_PM_ADDR_ATTR_IF_IDX, entry->addr.ifindex))
Paolo Abeni01cacb02020-03-27 14:48:51 -07001322 goto nla_put_failure;
1323
Bo YUb4e0f9a2020-04-23 10:10:03 +08001324 if (addr->family == AF_INET &&
1325 nla_put_in_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR4,
1326 addr->addr.s_addr))
1327 goto nla_put_failure;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001328#if IS_ENABLED(CONFIG_MPTCP_IPV6)
Bo YUb4e0f9a2020-04-23 10:10:03 +08001329 else if (addr->family == AF_INET6 &&
1330 nla_put_in6_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR6, &addr->addr6))
1331 goto nla_put_failure;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001332#endif
1333 nla_nest_end(skb, attr);
1334 return 0;
1335
1336nla_put_failure:
1337 nla_nest_cancel(skb, attr);
1338 return -EMSGSIZE;
1339}
1340
1341static int mptcp_nl_cmd_get_addr(struct sk_buff *skb, struct genl_info *info)
1342{
1343 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1344 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1345 struct mptcp_pm_addr_entry addr, *entry;
1346 struct sk_buff *msg;
1347 void *reply;
1348 int ret;
1349
1350 ret = mptcp_pm_parse_addr(attr, info, false, &addr);
1351 if (ret < 0)
1352 return ret;
1353
1354 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1355 if (!msg)
1356 return -ENOMEM;
1357
1358 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1359 info->genlhdr->cmd);
1360 if (!reply) {
1361 GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1362 ret = -EMSGSIZE;
1363 goto fail;
1364 }
1365
1366 spin_lock_bh(&pernet->lock);
1367 entry = __lookup_addr_by_id(pernet, addr.addr.id);
1368 if (!entry) {
1369 GENL_SET_ERR_MSG(info, "address not found");
1370 ret = -EINVAL;
1371 goto unlock_fail;
1372 }
1373
1374 ret = mptcp_nl_fill_addr(msg, entry);
1375 if (ret)
1376 goto unlock_fail;
1377
1378 genlmsg_end(msg, reply);
1379 ret = genlmsg_reply(msg, info);
1380 spin_unlock_bh(&pernet->lock);
1381 return ret;
1382
1383unlock_fail:
1384 spin_unlock_bh(&pernet->lock);
1385
1386fail:
1387 nlmsg_free(msg);
1388 return ret;
1389}
1390
1391static int mptcp_nl_cmd_dump_addrs(struct sk_buff *msg,
1392 struct netlink_callback *cb)
1393{
1394 struct net *net = sock_net(msg->sk);
1395 struct mptcp_pm_addr_entry *entry;
1396 struct pm_nl_pernet *pernet;
1397 int id = cb->args[0];
1398 void *hdr;
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001399 int i;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001400
1401 pernet = net_generic(net, pm_nl_pernet_id);
1402
1403 spin_lock_bh(&pernet->lock);
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001404 for (i = id; i < MAX_ADDR_ID + 1; i++) {
1405 if (test_bit(i, pernet->id_bitmap)) {
1406 entry = __lookup_addr_by_id(pernet, i);
1407 if (!entry)
1408 break;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001409
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001410 if (entry->addr.id <= id)
1411 continue;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001412
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001413 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid,
1414 cb->nlh->nlmsg_seq, &mptcp_genl_family,
1415 NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR);
1416 if (!hdr)
1417 break;
1418
1419 if (mptcp_nl_fill_addr(msg, entry) < 0) {
1420 genlmsg_cancel(msg, hdr);
1421 break;
1422 }
1423
1424 id = entry->addr.id;
1425 genlmsg_end(msg, hdr);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001426 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001427 }
1428 spin_unlock_bh(&pernet->lock);
1429
1430 cb->args[0] = id;
1431 return msg->len;
1432}
1433
1434static int parse_limit(struct genl_info *info, int id, unsigned int *limit)
1435{
1436 struct nlattr *attr = info->attrs[id];
1437
1438 if (!attr)
1439 return 0;
1440
1441 *limit = nla_get_u32(attr);
1442 if (*limit > MPTCP_PM_ADDR_MAX) {
1443 GENL_SET_ERR_MSG(info, "limit greater than maximum");
1444 return -EINVAL;
1445 }
1446 return 0;
1447}
1448
1449static int
1450mptcp_nl_cmd_set_limits(struct sk_buff *skb, struct genl_info *info)
1451{
1452 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1453 unsigned int rcv_addrs, subflows;
1454 int ret;
1455
1456 spin_lock_bh(&pernet->lock);
1457 rcv_addrs = pernet->add_addr_accept_max;
1458 ret = parse_limit(info, MPTCP_PM_ATTR_RCV_ADD_ADDRS, &rcv_addrs);
1459 if (ret)
1460 goto unlock;
1461
1462 subflows = pernet->subflows_max;
1463 ret = parse_limit(info, MPTCP_PM_ATTR_SUBFLOWS, &subflows);
1464 if (ret)
1465 goto unlock;
1466
1467 WRITE_ONCE(pernet->add_addr_accept_max, rcv_addrs);
1468 WRITE_ONCE(pernet->subflows_max, subflows);
1469
1470unlock:
1471 spin_unlock_bh(&pernet->lock);
1472 return ret;
1473}
1474
1475static int
1476mptcp_nl_cmd_get_limits(struct sk_buff *skb, struct genl_info *info)
1477{
1478 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1479 struct sk_buff *msg;
1480 void *reply;
1481
1482 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1483 if (!msg)
1484 return -ENOMEM;
1485
1486 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1487 MPTCP_PM_CMD_GET_LIMITS);
1488 if (!reply)
1489 goto fail;
1490
1491 if (nla_put_u32(msg, MPTCP_PM_ATTR_RCV_ADD_ADDRS,
1492 READ_ONCE(pernet->add_addr_accept_max)))
1493 goto fail;
1494
1495 if (nla_put_u32(msg, MPTCP_PM_ATTR_SUBFLOWS,
1496 READ_ONCE(pernet->subflows_max)))
1497 goto fail;
1498
1499 genlmsg_end(msg, reply);
1500 return genlmsg_reply(msg, info);
1501
1502fail:
1503 GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1504 nlmsg_free(msg);
1505 return -EMSGSIZE;
1506}
1507
Geliang Tang0f9f6962021-01-08 16:47:59 -08001508static int mptcp_nl_addr_backup(struct net *net,
1509 struct mptcp_addr_info *addr,
1510 u8 bkup)
1511{
1512 long s_slot = 0, s_num = 0;
1513 struct mptcp_sock *msk;
1514 int ret = -EINVAL;
1515
1516 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1517 struct sock *sk = (struct sock *)msk;
1518
1519 if (list_empty(&msk->conn_list))
1520 goto next;
1521
1522 lock_sock(sk);
1523 spin_lock_bh(&msk->pm.lock);
1524 ret = mptcp_pm_nl_mp_prio_send_ack(msk, addr, bkup);
1525 spin_unlock_bh(&msk->pm.lock);
1526 release_sock(sk);
1527
1528next:
1529 sock_put(sk);
1530 cond_resched();
1531 }
1532
1533 return ret;
1534}
1535
1536static int mptcp_nl_cmd_set_flags(struct sk_buff *skb, struct genl_info *info)
1537{
1538 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1539 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1540 struct mptcp_pm_addr_entry addr, *entry;
1541 struct net *net = sock_net(skb->sk);
1542 u8 bkup = 0;
1543 int ret;
1544
1545 ret = mptcp_pm_parse_addr(attr, info, true, &addr);
1546 if (ret < 0)
1547 return ret;
1548
1549 if (addr.addr.flags & MPTCP_PM_ADDR_FLAG_BACKUP)
1550 bkup = 1;
1551
1552 list_for_each_entry(entry, &pernet->local_addr_list, list) {
1553 if (addresses_equal(&entry->addr, &addr.addr, true)) {
1554 ret = mptcp_nl_addr_backup(net, &entry->addr, bkup);
1555 if (ret)
1556 return ret;
1557
1558 if (bkup)
1559 entry->addr.flags |= MPTCP_PM_ADDR_FLAG_BACKUP;
1560 else
1561 entry->addr.flags &= ~MPTCP_PM_ADDR_FLAG_BACKUP;
1562 }
1563 }
1564
1565 return 0;
1566}
1567
Florian Westphalb911c972021-02-12 16:00:01 -08001568static void mptcp_nl_mcast_send(struct net *net, struct sk_buff *nlskb, gfp_t gfp)
1569{
1570 genlmsg_multicast_netns(&mptcp_genl_family, net,
1571 nlskb, 0, MPTCP_PM_EV_GRP_OFFSET, gfp);
1572}
1573
1574static int mptcp_event_add_subflow(struct sk_buff *skb, const struct sock *ssk)
1575{
1576 const struct inet_sock *issk = inet_sk(ssk);
1577 const struct mptcp_subflow_context *sf;
1578
1579 if (nla_put_u16(skb, MPTCP_ATTR_FAMILY, ssk->sk_family))
1580 return -EMSGSIZE;
1581
1582 switch (ssk->sk_family) {
1583 case AF_INET:
1584 if (nla_put_in_addr(skb, MPTCP_ATTR_SADDR4, issk->inet_saddr))
1585 return -EMSGSIZE;
1586 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, issk->inet_daddr))
1587 return -EMSGSIZE;
1588 break;
1589#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1590 case AF_INET6: {
1591 const struct ipv6_pinfo *np = inet6_sk(ssk);
1592
1593 if (nla_put_in6_addr(skb, MPTCP_ATTR_SADDR6, &np->saddr))
1594 return -EMSGSIZE;
1595 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &ssk->sk_v6_daddr))
1596 return -EMSGSIZE;
1597 break;
1598 }
1599#endif
1600 default:
1601 WARN_ON_ONCE(1);
1602 return -EMSGSIZE;
1603 }
1604
1605 if (nla_put_be16(skb, MPTCP_ATTR_SPORT, issk->inet_sport))
1606 return -EMSGSIZE;
1607 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, issk->inet_dport))
1608 return -EMSGSIZE;
1609
1610 sf = mptcp_subflow_ctx(ssk);
1611 if (WARN_ON_ONCE(!sf))
1612 return -EINVAL;
1613
1614 if (nla_put_u8(skb, MPTCP_ATTR_LOC_ID, sf->local_id))
1615 return -EMSGSIZE;
1616
1617 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, sf->remote_id))
1618 return -EMSGSIZE;
1619
1620 return 0;
1621}
1622
1623static int mptcp_event_put_token_and_ssk(struct sk_buff *skb,
1624 const struct mptcp_sock *msk,
1625 const struct sock *ssk)
1626{
1627 const struct sock *sk = (const struct sock *)msk;
1628 const struct mptcp_subflow_context *sf;
1629 u8 sk_err;
1630
1631 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1632 return -EMSGSIZE;
1633
1634 if (mptcp_event_add_subflow(skb, ssk))
1635 return -EMSGSIZE;
1636
1637 sf = mptcp_subflow_ctx(ssk);
1638 if (WARN_ON_ONCE(!sf))
1639 return -EINVAL;
1640
1641 if (nla_put_u8(skb, MPTCP_ATTR_BACKUP, sf->backup))
1642 return -EMSGSIZE;
1643
1644 if (ssk->sk_bound_dev_if &&
1645 nla_put_s32(skb, MPTCP_ATTR_IF_IDX, ssk->sk_bound_dev_if))
1646 return -EMSGSIZE;
1647
1648 sk_err = ssk->sk_err;
1649 if (sk_err && sk->sk_state == TCP_ESTABLISHED &&
1650 nla_put_u8(skb, MPTCP_ATTR_ERROR, sk_err))
1651 return -EMSGSIZE;
1652
1653 return 0;
1654}
1655
1656static int mptcp_event_sub_established(struct sk_buff *skb,
1657 const struct mptcp_sock *msk,
1658 const struct sock *ssk)
1659{
1660 return mptcp_event_put_token_and_ssk(skb, msk, ssk);
1661}
1662
1663static int mptcp_event_sub_closed(struct sk_buff *skb,
1664 const struct mptcp_sock *msk,
1665 const struct sock *ssk)
1666{
1667 if (mptcp_event_put_token_and_ssk(skb, msk, ssk))
1668 return -EMSGSIZE;
1669
1670 return 0;
1671}
1672
1673static int mptcp_event_created(struct sk_buff *skb,
1674 const struct mptcp_sock *msk,
1675 const struct sock *ssk)
1676{
1677 int err = nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token);
1678
1679 if (err)
1680 return err;
1681
1682 return mptcp_event_add_subflow(skb, ssk);
1683}
1684
1685void mptcp_event_addr_removed(const struct mptcp_sock *msk, uint8_t id)
1686{
1687 struct net *net = sock_net((const struct sock *)msk);
1688 struct nlmsghdr *nlh;
1689 struct sk_buff *skb;
1690
1691 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
1692 return;
1693
1694 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1695 if (!skb)
1696 return;
1697
1698 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, MPTCP_EVENT_REMOVED);
1699 if (!nlh)
1700 goto nla_put_failure;
1701
1702 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1703 goto nla_put_failure;
1704
1705 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, id))
1706 goto nla_put_failure;
1707
1708 genlmsg_end(skb, nlh);
1709 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC);
1710 return;
1711
1712nla_put_failure:
1713 kfree_skb(skb);
1714}
1715
1716void mptcp_event_addr_announced(const struct mptcp_sock *msk,
1717 const struct mptcp_addr_info *info)
1718{
1719 struct net *net = sock_net((const struct sock *)msk);
1720 struct nlmsghdr *nlh;
1721 struct sk_buff *skb;
1722
1723 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
1724 return;
1725
1726 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1727 if (!skb)
1728 return;
1729
1730 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0,
1731 MPTCP_EVENT_ANNOUNCED);
1732 if (!nlh)
1733 goto nla_put_failure;
1734
1735 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1736 goto nla_put_failure;
1737
1738 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, info->id))
1739 goto nla_put_failure;
1740
1741 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, info->port))
1742 goto nla_put_failure;
1743
1744 switch (info->family) {
1745 case AF_INET:
1746 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, info->addr.s_addr))
1747 goto nla_put_failure;
1748 break;
1749#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1750 case AF_INET6:
1751 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &info->addr6))
1752 goto nla_put_failure;
1753 break;
1754#endif
1755 default:
1756 WARN_ON_ONCE(1);
1757 goto nla_put_failure;
1758 }
1759
1760 genlmsg_end(skb, nlh);
1761 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC);
1762 return;
1763
1764nla_put_failure:
1765 kfree_skb(skb);
1766}
1767
1768void mptcp_event(enum mptcp_event_type type, const struct mptcp_sock *msk,
1769 const struct sock *ssk, gfp_t gfp)
1770{
1771 struct net *net = sock_net((const struct sock *)msk);
1772 struct nlmsghdr *nlh;
1773 struct sk_buff *skb;
1774
1775 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
1776 return;
1777
1778 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
1779 if (!skb)
1780 return;
1781
1782 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, type);
1783 if (!nlh)
1784 goto nla_put_failure;
1785
1786 switch (type) {
1787 case MPTCP_EVENT_UNSPEC:
1788 WARN_ON_ONCE(1);
1789 break;
1790 case MPTCP_EVENT_CREATED:
1791 case MPTCP_EVENT_ESTABLISHED:
1792 if (mptcp_event_created(skb, msk, ssk) < 0)
1793 goto nla_put_failure;
1794 break;
1795 case MPTCP_EVENT_CLOSED:
1796 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token) < 0)
1797 goto nla_put_failure;
1798 break;
1799 case MPTCP_EVENT_ANNOUNCED:
1800 case MPTCP_EVENT_REMOVED:
1801 /* call mptcp_event_addr_announced()/removed instead */
1802 WARN_ON_ONCE(1);
1803 break;
1804 case MPTCP_EVENT_SUB_ESTABLISHED:
1805 case MPTCP_EVENT_SUB_PRIORITY:
1806 if (mptcp_event_sub_established(skb, msk, ssk) < 0)
1807 goto nla_put_failure;
1808 break;
1809 case MPTCP_EVENT_SUB_CLOSED:
1810 if (mptcp_event_sub_closed(skb, msk, ssk) < 0)
1811 goto nla_put_failure;
1812 break;
1813 }
1814
1815 genlmsg_end(skb, nlh);
1816 mptcp_nl_mcast_send(net, skb, gfp);
1817 return;
1818
1819nla_put_failure:
1820 kfree_skb(skb);
1821}
1822
Rikard Falkeborn674d3ab2020-10-05 01:44:16 +02001823static const struct genl_small_ops mptcp_pm_ops[] = {
Paolo Abeni01cacb02020-03-27 14:48:51 -07001824 {
1825 .cmd = MPTCP_PM_CMD_ADD_ADDR,
1826 .doit = mptcp_nl_cmd_add_addr,
1827 .flags = GENL_ADMIN_PERM,
1828 },
1829 {
1830 .cmd = MPTCP_PM_CMD_DEL_ADDR,
1831 .doit = mptcp_nl_cmd_del_addr,
1832 .flags = GENL_ADMIN_PERM,
1833 },
1834 {
1835 .cmd = MPTCP_PM_CMD_FLUSH_ADDRS,
1836 .doit = mptcp_nl_cmd_flush_addrs,
1837 .flags = GENL_ADMIN_PERM,
1838 },
1839 {
1840 .cmd = MPTCP_PM_CMD_GET_ADDR,
1841 .doit = mptcp_nl_cmd_get_addr,
1842 .dumpit = mptcp_nl_cmd_dump_addrs,
1843 },
1844 {
1845 .cmd = MPTCP_PM_CMD_SET_LIMITS,
1846 .doit = mptcp_nl_cmd_set_limits,
1847 .flags = GENL_ADMIN_PERM,
1848 },
1849 {
1850 .cmd = MPTCP_PM_CMD_GET_LIMITS,
1851 .doit = mptcp_nl_cmd_get_limits,
1852 },
Geliang Tang0f9f6962021-01-08 16:47:59 -08001853 {
1854 .cmd = MPTCP_PM_CMD_SET_FLAGS,
1855 .doit = mptcp_nl_cmd_set_flags,
1856 .flags = GENL_ADMIN_PERM,
1857 },
Paolo Abeni01cacb02020-03-27 14:48:51 -07001858};
1859
1860static struct genl_family mptcp_genl_family __ro_after_init = {
1861 .name = MPTCP_PM_NAME,
1862 .version = MPTCP_PM_VER,
1863 .maxattr = MPTCP_PM_ATTR_MAX,
1864 .policy = mptcp_pm_policy,
1865 .netnsok = true,
1866 .module = THIS_MODULE,
Jakub Kicinski66a9b922020-10-02 14:49:54 -07001867 .small_ops = mptcp_pm_ops,
1868 .n_small_ops = ARRAY_SIZE(mptcp_pm_ops),
Paolo Abeni01cacb02020-03-27 14:48:51 -07001869 .mcgrps = mptcp_pm_mcgrps,
1870 .n_mcgrps = ARRAY_SIZE(mptcp_pm_mcgrps),
1871};
1872
1873static int __net_init pm_nl_init_net(struct net *net)
1874{
1875 struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id);
1876
1877 INIT_LIST_HEAD_RCU(&pernet->local_addr_list);
1878 __reset_counters(pernet);
1879 pernet->next_id = 1;
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001880 bitmap_zero(pernet->id_bitmap, MAX_ADDR_ID + 1);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001881 spin_lock_init(&pernet->lock);
1882 return 0;
1883}
1884
1885static void __net_exit pm_nl_exit_net(struct list_head *net_list)
1886{
1887 struct net *net;
1888
1889 list_for_each_entry(net, net_list, exit_list) {
Geliang Tang141694d2020-12-10 14:24:59 -08001890 struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id);
1891
Paolo Abeni01cacb02020-03-27 14:48:51 -07001892 /* net is removed from namespace list, can't race with
1893 * other modifiers
1894 */
Geliang Tang0e4a3e62021-03-12 17:16:18 -08001895 __flush_addrs(&pernet->local_addr_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001896 }
1897}
1898
1899static struct pernet_operations mptcp_pm_pernet_ops = {
1900 .init = pm_nl_init_net,
1901 .exit_batch = pm_nl_exit_net,
1902 .id = &pm_nl_pernet_id,
1903 .size = sizeof(struct pm_nl_pernet),
1904};
1905
Paolo Abenid39dcec2020-06-26 19:29:59 +02001906void __init mptcp_pm_nl_init(void)
Paolo Abeni01cacb02020-03-27 14:48:51 -07001907{
1908 if (register_pernet_subsys(&mptcp_pm_pernet_ops) < 0)
1909 panic("Failed to register MPTCP PM pernet subsystem.\n");
1910
1911 if (genl_register_family(&mptcp_genl_family))
1912 panic("Failed to register MPTCP PM netlink family\n");
1913}