blob: 64a39f30659f6282eb22bdb7303fafdf81670ff1 [file] [log] [blame]
Paolo Abeni01cacb02020-03-27 14:48:51 -07001// SPDX-License-Identifier: GPL-2.0
2/* Multipath TCP
3 *
4 * Copyright (c) 2020, Red Hat, Inc.
5 */
6
Geliang Tangc85adce2020-04-03 17:14:08 +08007#define pr_fmt(fmt) "MPTCP: " fmt
8
Paolo Abeni01cacb02020-03-27 14:48:51 -07009#include <linux/inet.h>
10#include <linux/kernel.h>
11#include <net/tcp.h>
12#include <net/netns/generic.h>
13#include <net/mptcp.h>
14#include <net/genetlink.h>
15#include <uapi/linux/mptcp.h>
16
17#include "protocol.h"
Geliang Tang7a7e52e2020-09-24 08:29:56 +080018#include "mib.h"
Paolo Abeni01cacb02020-03-27 14:48:51 -070019
20/* forward declaration */
21static struct genl_family mptcp_genl_family;
22
23static int pm_nl_pernet_id;
24
25struct mptcp_pm_addr_entry {
26 struct list_head list;
Paolo Abeni01cacb02020-03-27 14:48:51 -070027 struct mptcp_addr_info addr;
Geliang Tangdaa83ab2021-04-06 17:15:57 -070028 u8 flags;
29 int ifindex;
Geliang Tang1729cf12021-02-01 15:09:12 -080030 struct socket *lsk;
Paolo Abeni01cacb02020-03-27 14:48:51 -070031};
32
Geliang Tang0abd40f2020-09-24 08:30:00 +080033struct mptcp_pm_add_entry {
34 struct list_head list;
35 struct mptcp_addr_info addr;
Geliang Tang00cfd772020-09-24 08:30:02 +080036 struct timer_list add_timer;
37 struct mptcp_sock *sock;
38 u8 retrans_times;
Geliang Tang0abd40f2020-09-24 08:30:00 +080039};
40
Geliang Tangefd5a4c2021-01-08 16:47:55 -080041#define MAX_ADDR_ID 255
42#define BITMAP_SZ DIV_ROUND_UP(MAX_ADDR_ID + 1, BITS_PER_LONG)
43
Paolo Abeni01cacb02020-03-27 14:48:51 -070044struct pm_nl_pernet {
45 /* protects pernet updates */
46 spinlock_t lock;
47 struct list_head local_addr_list;
48 unsigned int addrs;
Paolo Abeniff5a0b42021-08-13 15:15:45 -070049 unsigned int stale_loss_cnt;
Paolo Abeni01cacb02020-03-27 14:48:51 -070050 unsigned int add_addr_signal_max;
51 unsigned int add_addr_accept_max;
52 unsigned int local_addr_max;
53 unsigned int subflows_max;
54 unsigned int next_id;
Geliang Tangefd5a4c2021-01-08 16:47:55 -080055 unsigned long id_bitmap[BITMAP_SZ];
Paolo Abeni01cacb02020-03-27 14:48:51 -070056};
57
58#define MPTCP_PM_ADDR_MAX 8
Geliang Tang00cfd772020-09-24 08:30:02 +080059#define ADD_ADDR_RETRANS_MAX 3
Paolo Abeni01cacb02020-03-27 14:48:51 -070060
61static 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
Geliang Tangd84ad042021-03-26 11:26:32 -0700143static bool lookup_subflow_by_daddr(const struct list_head *list,
144 struct mptcp_addr_info *daddr)
145{
146 struct mptcp_subflow_context *subflow;
147 struct mptcp_addr_info cur;
148 struct sock_common *skc;
149
150 list_for_each_entry(subflow, list, node) {
151 skc = (struct sock_common *)mptcp_subflow_tcp_sock(subflow);
152
153 remote_address(skc, &cur);
154 if (addresses_equal(&cur, daddr, daddr->port))
155 return true;
156 }
157
158 return false;
159}
160
Paolo Abeni01cacb02020-03-27 14:48:51 -0700161static struct mptcp_pm_addr_entry *
162select_local_address(const struct pm_nl_pernet *pernet,
163 struct mptcp_sock *msk)
164{
165 struct mptcp_pm_addr_entry *entry, *ret = NULL;
Matthieu Baerts7b9b0f72021-01-25 10:59:01 -0800166 struct sock *sk = (struct sock *)msk;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700167
Florian Westphal3abc05d2021-02-04 15:23:30 -0800168 msk_owned_by_me(msk);
169
Paolo Abeni01cacb02020-03-27 14:48:51 -0700170 rcu_read_lock();
Paolo Abeni1bc73272020-12-10 14:25:05 -0800171 __mptcp_flush_join_list(msk);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700172 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
Geliang Tangdaa83ab2021-04-06 17:15:57 -0700173 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW))
Paolo Abeni01cacb02020-03-27 14:48:51 -0700174 continue;
175
Matthieu Baerts7b9b0f72021-01-25 10:59:01 -0800176 if (entry->addr.family != sk->sk_family) {
177#if IS_ENABLED(CONFIG_MPTCP_IPV6)
178 if ((entry->addr.family == AF_INET &&
179 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) ||
180 (sk->sk_family == AF_INET &&
181 !ipv6_addr_v4mapped(&entry->addr.addr6)))
182#endif
183 continue;
184 }
185
Paolo Abeni01cacb02020-03-27 14:48:51 -0700186 /* avoid any address already in use by subflows and
187 * pending join
188 */
Matthieu Baerts7b9b0f72021-01-25 10:59:01 -0800189 if (!lookup_subflow_by_saddr(&msk->conn_list, &entry->addr)) {
Paolo Abeni01cacb02020-03-27 14:48:51 -0700190 ret = entry;
191 break;
192 }
193 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700194 rcu_read_unlock();
195 return ret;
196}
197
198static struct mptcp_pm_addr_entry *
199select_signal_address(struct pm_nl_pernet *pernet, unsigned int pos)
200{
201 struct mptcp_pm_addr_entry *entry, *ret = NULL;
202 int i = 0;
203
204 rcu_read_lock();
205 /* do not keep any additional per socket state, just signal
206 * the address list in order.
207 * Note: removal from the local address list during the msk life-cycle
208 * can lead to additional addresses not being announced.
209 */
210 list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
Geliang Tangdaa83ab2021-04-06 17:15:57 -0700211 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL))
Paolo Abeni01cacb02020-03-27 14:48:51 -0700212 continue;
213 if (i++ == pos) {
214 ret = entry;
215 break;
216 }
217 }
218 rcu_read_unlock();
219 return ret;
220}
221
Geliang Tanga914e582021-02-01 15:09:07 -0800222unsigned int mptcp_pm_get_add_addr_signal_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->add_addr_signal_max);
228}
229EXPORT_SYMBOL_GPL(mptcp_pm_get_add_addr_signal_max);
230
231unsigned int mptcp_pm_get_add_addr_accept_max(struct mptcp_sock *msk)
232{
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->add_addr_accept_max);
237}
238EXPORT_SYMBOL_GPL(mptcp_pm_get_add_addr_accept_max);
239
240unsigned int mptcp_pm_get_subflows_max(struct mptcp_sock *msk)
241{
242 struct pm_nl_pernet *pernet;
243
244 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
245 return READ_ONCE(pernet->subflows_max);
246}
247EXPORT_SYMBOL_GPL(mptcp_pm_get_subflows_max);
248
Geliang Tang0caf3ad2021-02-12 16:52:02 -0800249unsigned int mptcp_pm_get_local_addr_max(struct mptcp_sock *msk)
Geliang Tanga914e582021-02-01 15:09:07 -0800250{
251 struct pm_nl_pernet *pernet;
252
253 pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
254 return READ_ONCE(pernet->local_addr_max);
255}
Geliang Tang0caf3ad2021-02-12 16:52:02 -0800256EXPORT_SYMBOL_GPL(mptcp_pm_get_local_addr_max);
Geliang Tanga914e582021-02-01 15:09:07 -0800257
Paolo Abeni01cacb02020-03-27 14:48:51 -0700258static void check_work_pending(struct mptcp_sock *msk)
259{
Geliang Tanga914e582021-02-01 15:09:07 -0800260 if (msk->pm.add_addr_signaled == mptcp_pm_get_add_addr_signal_max(msk) &&
261 (msk->pm.local_addr_used == mptcp_pm_get_local_addr_max(msk) ||
262 msk->pm.subflows == mptcp_pm_get_subflows_max(msk)))
Paolo Abeni01cacb02020-03-27 14:48:51 -0700263 WRITE_ONCE(msk->pm.work_pending, false);
264}
265
Geliang Tangd88c4762021-03-26 11:26:37 -0700266struct mptcp_pm_add_entry *
267mptcp_lookup_anno_list_by_saddr(struct mptcp_sock *msk,
268 struct mptcp_addr_info *addr)
Geliang Tangb6c08382020-09-24 08:29:54 +0800269{
Geliang Tang0abd40f2020-09-24 08:30:00 +0800270 struct mptcp_pm_add_entry *entry;
Geliang Tangb6c08382020-09-24 08:29:54 +0800271
Florian Westphal3abc05d2021-02-04 15:23:30 -0800272 lockdep_assert_held(&msk->pm.lock);
273
Geliang Tangb6c08382020-09-24 08:29:54 +0800274 list_for_each_entry(entry, &msk->pm.anno_list, list) {
Geliang Tang60b57bf2021-02-01 15:09:16 -0800275 if (addresses_equal(&entry->addr, addr, true))
Geliang Tang00cfd772020-09-24 08:30:02 +0800276 return entry;
Geliang Tangb6c08382020-09-24 08:29:54 +0800277 }
278
Geliang Tang00cfd772020-09-24 08:30:02 +0800279 return NULL;
280}
281
Geliang Tang5bc56382021-02-01 15:09:15 -0800282bool mptcp_pm_sport_in_anno_list(struct mptcp_sock *msk, const struct sock *sk)
283{
284 struct mptcp_pm_add_entry *entry;
285 struct mptcp_addr_info saddr;
286 bool ret = false;
287
288 local_address((struct sock_common *)sk, &saddr);
289
290 spin_lock_bh(&msk->pm.lock);
291 list_for_each_entry(entry, &msk->pm.anno_list, list) {
292 if (addresses_equal(&entry->addr, &saddr, true)) {
293 ret = true;
294 goto out;
295 }
296 }
297
298out:
299 spin_unlock_bh(&msk->pm.lock);
300 return ret;
301}
302
Geliang Tang00cfd772020-09-24 08:30:02 +0800303static void mptcp_pm_add_timer(struct timer_list *timer)
304{
305 struct mptcp_pm_add_entry *entry = from_timer(entry, timer, add_timer);
306 struct mptcp_sock *msk = entry->sock;
307 struct sock *sk = (struct sock *)msk;
308
309 pr_debug("msk=%p", msk);
310
311 if (!msk)
312 return;
313
314 if (inet_sk_state_load(sk) == TCP_CLOSE)
315 return;
316
317 if (!entry->addr.id)
318 return;
319
320 if (mptcp_pm_should_add_signal(msk)) {
321 sk_reset_timer(sk, timer, jiffies + TCP_RTO_MAX / 8);
322 goto out;
323 }
324
325 spin_lock_bh(&msk->pm.lock);
326
327 if (!mptcp_pm_should_add_signal(msk)) {
328 pr_debug("retransmit ADD_ADDR id=%d", entry->addr.id);
Geliang Tangf7efc772021-03-26 11:26:31 -0700329 mptcp_pm_announce_addr(msk, &entry->addr, false);
Geliang Tang84dfe362020-11-19 11:46:00 -0800330 mptcp_pm_add_addr_send_ack(msk);
Geliang Tang00cfd772020-09-24 08:30:02 +0800331 entry->retrans_times++;
332 }
333
334 if (entry->retrans_times < ADD_ADDR_RETRANS_MAX)
Geliang Tang93f323b2020-11-03 11:05:07 -0800335 sk_reset_timer(sk, timer,
Geliang Tang724d06b2020-11-10 11:01:43 +0800336 jiffies + mptcp_get_add_addr_timeout(sock_net(sk)));
Geliang Tang00cfd772020-09-24 08:30:02 +0800337
338 spin_unlock_bh(&msk->pm.lock);
339
Geliang Tang348d5c12021-03-26 11:26:34 -0700340 if (entry->retrans_times == ADD_ADDR_RETRANS_MAX)
341 mptcp_pm_subflow_established(msk);
342
Geliang Tang00cfd772020-09-24 08:30:02 +0800343out:
344 __sock_put(sk);
345}
346
347struct mptcp_pm_add_entry *
348mptcp_pm_del_add_timer(struct mptcp_sock *msk,
Davide Carattid58300c2021-05-25 14:23:13 -0700349 struct mptcp_addr_info *addr, bool check_id)
Geliang Tang00cfd772020-09-24 08:30:02 +0800350{
351 struct mptcp_pm_add_entry *entry;
352 struct sock *sk = (struct sock *)msk;
353
354 spin_lock_bh(&msk->pm.lock);
Geliang Tangd88c4762021-03-26 11:26:37 -0700355 entry = mptcp_lookup_anno_list_by_saddr(msk, addr);
Davide Carattid58300c2021-05-25 14:23:13 -0700356 if (entry && (!check_id || entry->addr.id == addr->id))
Geliang Tang00cfd772020-09-24 08:30:02 +0800357 entry->retrans_times = ADD_ADDR_RETRANS_MAX;
358 spin_unlock_bh(&msk->pm.lock);
359
Davide Carattid58300c2021-05-25 14:23:13 -0700360 if (entry && (!check_id || entry->addr.id == addr->id))
Geliang Tang00cfd772020-09-24 08:30:02 +0800361 sk_stop_timer_sync(sk, &entry->add_timer);
362
363 return entry;
Geliang Tangb6c08382020-09-24 08:29:54 +0800364}
365
366static bool mptcp_pm_alloc_anno_list(struct mptcp_sock *msk,
367 struct mptcp_pm_addr_entry *entry)
368{
Geliang Tang0abd40f2020-09-24 08:30:00 +0800369 struct mptcp_pm_add_entry *add_entry = NULL;
Geliang Tang00cfd772020-09-24 08:30:02 +0800370 struct sock *sk = (struct sock *)msk;
Geliang Tang93f323b2020-11-03 11:05:07 -0800371 struct net *net = sock_net(sk);
Geliang Tangb6c08382020-09-24 08:29:54 +0800372
Florian Westphal3abc05d2021-02-04 15:23:30 -0800373 lockdep_assert_held(&msk->pm.lock);
374
Geliang Tangd88c4762021-03-26 11:26:37 -0700375 if (mptcp_lookup_anno_list_by_saddr(msk, &entry->addr))
Geliang Tangb6c08382020-09-24 08:29:54 +0800376 return false;
377
Geliang Tang0abd40f2020-09-24 08:30:00 +0800378 add_entry = kmalloc(sizeof(*add_entry), GFP_ATOMIC);
379 if (!add_entry)
Geliang Tangb6c08382020-09-24 08:29:54 +0800380 return false;
381
Geliang Tang0abd40f2020-09-24 08:30:00 +0800382 list_add(&add_entry->list, &msk->pm.anno_list);
Geliang Tangb6c08382020-09-24 08:29:54 +0800383
Geliang Tang00cfd772020-09-24 08:30:02 +0800384 add_entry->addr = entry->addr;
385 add_entry->sock = msk;
386 add_entry->retrans_times = 0;
387
388 timer_setup(&add_entry->add_timer, mptcp_pm_add_timer, 0);
Geliang Tang93f323b2020-11-03 11:05:07 -0800389 sk_reset_timer(sk, &add_entry->add_timer,
390 jiffies + mptcp_get_add_addr_timeout(net));
Geliang Tang00cfd772020-09-24 08:30:02 +0800391
Geliang Tangb6c08382020-09-24 08:29:54 +0800392 return true;
393}
394
395void mptcp_pm_free_anno_list(struct mptcp_sock *msk)
396{
Geliang Tang0abd40f2020-09-24 08:30:00 +0800397 struct mptcp_pm_add_entry *entry, *tmp;
Geliang Tang00cfd772020-09-24 08:30:02 +0800398 struct sock *sk = (struct sock *)msk;
399 LIST_HEAD(free_list);
Geliang Tangb6c08382020-09-24 08:29:54 +0800400
401 pr_debug("msk=%p", msk);
402
403 spin_lock_bh(&msk->pm.lock);
Geliang Tang00cfd772020-09-24 08:30:02 +0800404 list_splice_init(&msk->pm.anno_list, &free_list);
405 spin_unlock_bh(&msk->pm.lock);
406
407 list_for_each_entry_safe(entry, tmp, &free_list, list) {
408 sk_stop_timer_sync(sk, &entry->add_timer);
Geliang Tangb6c08382020-09-24 08:29:54 +0800409 kfree(entry);
410 }
Geliang Tangb6c08382020-09-24 08:29:54 +0800411}
412
Paolo Abeni01cacb02020-03-27 14:48:51 -0700413static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk)
414{
415 struct sock *sk = (struct sock *)msk;
416 struct mptcp_pm_addr_entry *local;
Geliang Tanga914e582021-02-01 15:09:07 -0800417 unsigned int add_addr_signal_max;
418 unsigned int local_addr_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700419 struct pm_nl_pernet *pernet;
Geliang Tanga914e582021-02-01 15:09:07 -0800420 unsigned int subflows_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700421
Geliang Tang432d9e72020-12-09 15:51:28 -0800422 pernet = net_generic(sock_net(sk), pm_nl_pernet_id);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700423
Geliang Tanga914e582021-02-01 15:09:07 -0800424 add_addr_signal_max = mptcp_pm_get_add_addr_signal_max(msk);
425 local_addr_max = mptcp_pm_get_local_addr_max(msk);
426 subflows_max = mptcp_pm_get_subflows_max(msk);
427
Paolo Abeni01cacb02020-03-27 14:48:51 -0700428 pr_debug("local %d:%d signal %d:%d subflows %d:%d\n",
Geliang Tanga914e582021-02-01 15:09:07 -0800429 msk->pm.local_addr_used, local_addr_max,
430 msk->pm.add_addr_signaled, add_addr_signal_max,
431 msk->pm.subflows, subflows_max);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700432
433 /* check first for announce */
Geliang Tanga914e582021-02-01 15:09:07 -0800434 if (msk->pm.add_addr_signaled < add_addr_signal_max) {
Paolo Abeni01cacb02020-03-27 14:48:51 -0700435 local = select_signal_address(pernet,
436 msk->pm.add_addr_signaled);
437
438 if (local) {
Geliang Tangb6c08382020-09-24 08:29:54 +0800439 if (mptcp_pm_alloc_anno_list(msk, local)) {
440 msk->pm.add_addr_signaled++;
Geliang Tangf7efc772021-03-26 11:26:31 -0700441 mptcp_pm_announce_addr(msk, &local->addr, false);
Geliang Tangb46a0232021-03-26 11:26:41 -0700442 mptcp_pm_nl_addr_send_ack(msk);
Geliang Tangb6c08382020-09-24 08:29:54 +0800443 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700444 } else {
445 /* pick failed, avoid fourther attempts later */
Geliang Tanga914e582021-02-01 15:09:07 -0800446 msk->pm.local_addr_used = add_addr_signal_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700447 }
448
449 check_work_pending(msk);
450 }
451
452 /* check if should create a new subflow */
Geliang Tanga914e582021-02-01 15:09:07 -0800453 if (msk->pm.local_addr_used < local_addr_max &&
Geliang Tangdf377be2021-06-22 12:25:20 -0700454 msk->pm.subflows < subflows_max &&
455 !READ_ONCE(msk->pm.remote_deny_join_id0)) {
Paolo Abeni01cacb02020-03-27 14:48:51 -0700456 local = select_local_address(pernet, msk);
457 if (local) {
Matthieu Baerts1f2f1932021-01-25 10:59:02 -0800458 struct mptcp_addr_info remote = { 0 };
459
Paolo Abeni01cacb02020-03-27 14:48:51 -0700460 msk->pm.local_addr_used++;
461 msk->pm.subflows++;
462 check_work_pending(msk);
Matthieu Baerts1f2f1932021-01-25 10:59:02 -0800463 remote_address((struct sock_common *)sk, &remote);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700464 spin_unlock_bh(&msk->pm.lock);
Geliang Tangee285252021-08-17 15:07:22 -0700465 __mptcp_subflow_connect(sk, &local->addr, &remote);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700466 spin_lock_bh(&msk->pm.lock);
467 return;
468 }
469
470 /* lookup failed, avoid fourther attempts later */
Geliang Tanga914e582021-02-01 15:09:07 -0800471 msk->pm.local_addr_used = local_addr_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700472 check_work_pending(msk);
473 }
474}
475
Florian Westphale9801432021-02-12 15:59:54 -0800476static void mptcp_pm_nl_fully_established(struct mptcp_sock *msk)
Paolo Abeni01cacb02020-03-27 14:48:51 -0700477{
478 mptcp_pm_create_subflow_or_signal_addr(msk);
479}
480
Florian Westphale9801432021-02-12 15:59:54 -0800481static void mptcp_pm_nl_subflow_established(struct mptcp_sock *msk)
Paolo Abeni01cacb02020-03-27 14:48:51 -0700482{
483 mptcp_pm_create_subflow_or_signal_addr(msk);
484}
485
Florian Westphale9801432021-02-12 15:59:54 -0800486static void mptcp_pm_nl_add_addr_received(struct mptcp_sock *msk)
Paolo Abeni01cacb02020-03-27 14:48:51 -0700487{
488 struct sock *sk = (struct sock *)msk;
Geliang Tanga914e582021-02-01 15:09:07 -0800489 unsigned int add_addr_accept_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700490 struct mptcp_addr_info remote;
491 struct mptcp_addr_info local;
Geliang Tanga914e582021-02-01 15:09:07 -0800492 unsigned int subflows_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -0700493
Geliang Tanga914e582021-02-01 15:09:07 -0800494 add_addr_accept_max = mptcp_pm_get_add_addr_accept_max(msk);
495 subflows_max = mptcp_pm_get_subflows_max(msk);
496
Paolo Abeni01cacb02020-03-27 14:48:51 -0700497 pr_debug("accepted %d:%d remote family %d",
Geliang Tanga914e582021-02-01 15:09:07 -0800498 msk->pm.add_addr_accepted, add_addr_accept_max,
Paolo Abeni01cacb02020-03-27 14:48:51 -0700499 msk->pm.remote.family);
Geliang Tangd84ad042021-03-26 11:26:32 -0700500
501 if (lookup_subflow_by_daddr(&msk->conn_list, &msk->pm.remote))
502 goto add_addr_echo;
503
Paolo Abeni01cacb02020-03-27 14:48:51 -0700504 msk->pm.add_addr_accepted++;
505 msk->pm.subflows++;
Geliang Tanga914e582021-02-01 15:09:07 -0800506 if (msk->pm.add_addr_accepted >= add_addr_accept_max ||
507 msk->pm.subflows >= subflows_max)
Paolo Abeni01cacb02020-03-27 14:48:51 -0700508 WRITE_ONCE(msk->pm.accept_addr, false);
509
510 /* connect to the specified remote address, using whatever
511 * local address the routing configuration will pick.
512 */
513 remote = msk->pm.remote;
514 if (!remote.port)
515 remote.port = sk->sk_dport;
516 memset(&local, 0, sizeof(local));
517 local.family = remote.family;
518
519 spin_unlock_bh(&msk->pm.lock);
Geliang Tangee285252021-08-17 15:07:22 -0700520 __mptcp_subflow_connect(sk, &local, &remote);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700521 spin_lock_bh(&msk->pm.lock);
Geliang Tang6a6c05a2020-09-24 08:29:50 +0800522
Geliang Tangd84ad042021-03-26 11:26:32 -0700523add_addr_echo:
Geliang Tangf7efc772021-03-26 11:26:31 -0700524 mptcp_pm_announce_addr(msk, &msk->pm.remote, true);
Geliang Tangb46a0232021-03-26 11:26:41 -0700525 mptcp_pm_nl_addr_send_ack(msk);
Geliang Tang84dfe362020-11-19 11:46:00 -0800526}
527
Geliang Tangb46a0232021-03-26 11:26:41 -0700528void mptcp_pm_nl_addr_send_ack(struct mptcp_sock *msk)
Geliang Tang84dfe362020-11-19 11:46:00 -0800529{
530 struct mptcp_subflow_context *subflow;
531
Florian Westphal3abc05d2021-02-04 15:23:30 -0800532 msk_owned_by_me(msk);
533 lockdep_assert_held(&msk->pm.lock);
534
Geliang Tang8dd5efb2021-03-26 11:26:40 -0700535 if (!mptcp_pm_should_add_signal(msk) &&
536 !mptcp_pm_should_rm_signal(msk))
Geliang Tang84dfe362020-11-19 11:46:00 -0800537 return;
538
539 __mptcp_flush_join_list(msk);
540 subflow = list_first_entry_or_null(&msk->conn_list, typeof(*subflow), node);
541 if (subflow) {
542 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
Paolo Abeni75e908c2021-06-21 15:54:34 -0700543 bool slow;
Geliang Tang84dfe362020-11-19 11:46:00 -0800544
545 spin_unlock_bh(&msk->pm.lock);
Geliang Tang8dd5efb2021-03-26 11:26:40 -0700546 pr_debug("send ack for %s%s%s",
547 mptcp_pm_should_add_signal(msk) ? "add_addr" : "rm_addr",
Geliang Tangb5a7acd2021-02-01 15:09:09 -0800548 mptcp_pm_should_add_signal_ipv6(msk) ? " [ipv6]" : "",
549 mptcp_pm_should_add_signal_port(msk) ? " [port]" : "");
Geliang Tangfbe0f872020-12-09 15:51:23 -0800550
Paolo Abeni75e908c2021-06-21 15:54:34 -0700551 slow = lock_sock_fast(ssk);
Geliang Tang84dfe362020-11-19 11:46:00 -0800552 tcp_send_ack(ssk);
Paolo Abeni75e908c2021-06-21 15:54:34 -0700553 unlock_sock_fast(ssk, slow);
Geliang Tang84dfe362020-11-19 11:46:00 -0800554 spin_lock_bh(&msk->pm.lock);
Geliang Tang84dfe362020-11-19 11:46:00 -0800555 }
Paolo Abeni01cacb02020-03-27 14:48:51 -0700556}
557
Geliang Tang06706542021-01-08 16:47:57 -0800558int mptcp_pm_nl_mp_prio_send_ack(struct mptcp_sock *msk,
559 struct mptcp_addr_info *addr,
560 u8 bkup)
561{
562 struct mptcp_subflow_context *subflow;
563
564 pr_debug("bkup=%d", bkup);
565
566 mptcp_for_each_subflow(msk, subflow) {
567 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
Geliang Tang0be2ac22021-01-08 16:48:01 -0800568 struct sock *sk = (struct sock *)msk;
Geliang Tang06706542021-01-08 16:47:57 -0800569 struct mptcp_addr_info local;
Paolo Abeni75e908c2021-06-21 15:54:34 -0700570 bool slow;
Geliang Tang06706542021-01-08 16:47:57 -0800571
572 local_address((struct sock_common *)ssk, &local);
573 if (!addresses_equal(&local, addr, addr->port))
574 continue;
575
576 subflow->backup = bkup;
577 subflow->send_mp_prio = 1;
578 subflow->request_bkup = bkup;
Geliang Tang0be2ac22021-01-08 16:48:01 -0800579 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIOTX);
Geliang Tang06706542021-01-08 16:47:57 -0800580
581 spin_unlock_bh(&msk->pm.lock);
582 pr_debug("send ack for mp_prio");
Paolo Abeni75e908c2021-06-21 15:54:34 -0700583 slow = lock_sock_fast(ssk);
Geliang Tang06706542021-01-08 16:47:57 -0800584 tcp_send_ack(ssk);
Paolo Abeni75e908c2021-06-21 15:54:34 -0700585 unlock_sock_fast(ssk, slow);
Geliang Tang06706542021-01-08 16:47:57 -0800586 spin_lock_bh(&msk->pm.lock);
587
588 return 0;
589 }
590
591 return -EINVAL;
592}
593
Geliang Tang9f12e972021-03-30 17:08:52 -0700594static void mptcp_pm_nl_rm_addr_or_subflow(struct mptcp_sock *msk,
595 const struct mptcp_rm_list *rm_list,
596 enum linux_mptcp_mib_field rm_type)
Geliang Tangd0876b22020-09-24 08:29:49 +0800597{
598 struct mptcp_subflow_context *subflow, *tmp;
599 struct sock *sk = (struct sock *)msk;
Geliang Tangd0b698c2021-03-12 17:16:15 -0800600 u8 i;
Geliang Tangd0876b22020-09-24 08:29:49 +0800601
Geliang Tang9f12e972021-03-30 17:08:52 -0700602 pr_debug("%s rm_list_nr %d",
603 rm_type == MPTCP_MIB_RMADDR ? "address" : "subflow", rm_list->nr);
Geliang Tangd0876b22020-09-24 08:29:49 +0800604
Florian Westphal3abc05d2021-02-04 15:23:30 -0800605 msk_owned_by_me(msk);
606
Geliang Tang9f12e972021-03-30 17:08:52 -0700607 if (!rm_list->nr)
Geliang Tangd0876b22020-09-24 08:29:49 +0800608 return;
609
610 if (list_empty(&msk->conn_list))
611 return;
612
Geliang Tang9f12e972021-03-30 17:08:52 -0700613 for (i = 0; i < rm_list->nr; i++) {
Geliang Tangd0b698c2021-03-12 17:16:15 -0800614 list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
615 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
616 int how = RCV_SHUTDOWN | SEND_SHUTDOWN;
Geliang Tang9f12e972021-03-30 17:08:52 -0700617 u8 id = subflow->local_id;
Geliang Tangd0876b22020-09-24 08:29:49 +0800618
Geliang Tang9f12e972021-03-30 17:08:52 -0700619 if (rm_type == MPTCP_MIB_RMADDR)
620 id = subflow->remote_id;
621
622 if (rm_list->ids[i] != id)
Geliang Tangd0b698c2021-03-12 17:16:15 -0800623 continue;
Geliang Tangd0876b22020-09-24 08:29:49 +0800624
Geliang Tang9f12e972021-03-30 17:08:52 -0700625 pr_debug(" -> %s rm_list_ids[%d]=%u local_id=%u remote_id=%u",
626 rm_type == MPTCP_MIB_RMADDR ? "address" : "subflow",
627 i, rm_list->ids[i], subflow->local_id, subflow->remote_id);
Geliang Tangd0b698c2021-03-12 17:16:15 -0800628 spin_unlock_bh(&msk->pm.lock);
629 mptcp_subflow_shutdown(sk, ssk, how);
630 mptcp_close_ssk(sk, ssk, subflow);
631 spin_lock_bh(&msk->pm.lock);
Geliang Tangd0876b22020-09-24 08:29:49 +0800632
Geliang Tang9f12e972021-03-30 17:08:52 -0700633 if (rm_type == MPTCP_MIB_RMADDR) {
634 msk->pm.add_addr_accepted--;
635 WRITE_ONCE(msk->pm.accept_addr, true);
636 } else if (rm_type == MPTCP_MIB_RMSUBFLOW) {
637 msk->pm.local_addr_used--;
638 }
Geliang Tangd0b698c2021-03-12 17:16:15 -0800639 msk->pm.subflows--;
Geliang Tang9f12e972021-03-30 17:08:52 -0700640 __MPTCP_INC_STATS(sock_net(sk), rm_type);
Geliang Tangd0b698c2021-03-12 17:16:15 -0800641 }
Geliang Tangd0876b22020-09-24 08:29:49 +0800642 }
643}
644
Geliang Tang9f12e972021-03-30 17:08:52 -0700645static void mptcp_pm_nl_rm_addr_received(struct mptcp_sock *msk)
646{
647 mptcp_pm_nl_rm_addr_or_subflow(msk, &msk->pm.rm_list_rx, MPTCP_MIB_RMADDR);
648}
649
650void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk,
651 const struct mptcp_rm_list *rm_list)
652{
653 mptcp_pm_nl_rm_addr_or_subflow(msk, rm_list, MPTCP_MIB_RMSUBFLOW);
654}
655
Florian Westphale9801432021-02-12 15:59:54 -0800656void mptcp_pm_nl_work(struct mptcp_sock *msk)
657{
658 struct mptcp_pm_data *pm = &msk->pm;
659
660 msk_owned_by_me(msk);
661
662 spin_lock_bh(&msk->pm.lock);
663
664 pr_debug("msk=%p status=%x", msk, pm->status);
665 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_RECEIVED)) {
666 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_RECEIVED);
667 mptcp_pm_nl_add_addr_received(msk);
668 }
669 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_SEND_ACK)) {
670 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_SEND_ACK);
Geliang Tangb46a0232021-03-26 11:26:41 -0700671 mptcp_pm_nl_addr_send_ack(msk);
Florian Westphale9801432021-02-12 15:59:54 -0800672 }
673 if (pm->status & BIT(MPTCP_PM_RM_ADDR_RECEIVED)) {
674 pm->status &= ~BIT(MPTCP_PM_RM_ADDR_RECEIVED);
675 mptcp_pm_nl_rm_addr_received(msk);
676 }
677 if (pm->status & BIT(MPTCP_PM_ESTABLISHED)) {
678 pm->status &= ~BIT(MPTCP_PM_ESTABLISHED);
679 mptcp_pm_nl_fully_established(msk);
680 }
681 if (pm->status & BIT(MPTCP_PM_SUBFLOW_ESTABLISHED)) {
682 pm->status &= ~BIT(MPTCP_PM_SUBFLOW_ESTABLISHED);
683 mptcp_pm_nl_subflow_established(msk);
684 }
685
686 spin_unlock_bh(&msk->pm.lock);
687}
688
Paolo Abeni01cacb02020-03-27 14:48:51 -0700689static bool address_use_port(struct mptcp_pm_addr_entry *entry)
690{
Geliang Tangdaa83ab2021-04-06 17:15:57 -0700691 return (entry->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 Tangdaa83ab2021-04-06 17:15:57 -0700743 if (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
Geliang Tang72603d22021-02-01 15:09:06 -0800744 addr_max = pernet->add_addr_signal_max;
745 WRITE_ONCE(pernet->add_addr_signal_max, addr_max + 1);
746 }
Geliang Tangdaa83ab2021-04-06 17:15:57 -0700747 if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) {
Geliang Tang72603d22021-02-01 15:09:06 -0800748 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;
Geliang Tangefd5a4c2021-01-08 16:47:55 -0800849 entry->addr.id = 0;
Geliang Tang1729cf12021-02-01 15:09:12 -0800850 entry->addr.port = 0;
Geliang Tangdaa83ab2021-04-06 17:15:57 -0700851 entry->ifindex = 0;
852 entry->flags = 0;
Geliang Tang1729cf12021-02-01 15:09:12 -0800853 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
Paolo Abeniff5a0b42021-08-13 15:15:45 -0700902void mptcp_pm_nl_subflow_chk_stale(const struct mptcp_sock *msk, struct sock *ssk)
903{
904 struct mptcp_subflow_context *iter, *subflow = mptcp_subflow_ctx(ssk);
905 struct sock *sk = (struct sock *)msk;
906 unsigned int active_max_loss_cnt;
907 struct net *net = sock_net(sk);
908 unsigned int stale_loss_cnt;
909 bool slow;
910
911 stale_loss_cnt = mptcp_stale_loss_cnt(net);
912 if (subflow->stale || !stale_loss_cnt || subflow->stale_count <= stale_loss_cnt)
913 return;
914
915 /* look for another available subflow not in loss state */
916 active_max_loss_cnt = max_t(int, stale_loss_cnt - 1, 1);
917 mptcp_for_each_subflow(msk, iter) {
918 if (iter != subflow && mptcp_subflow_active(iter) &&
919 iter->stale_count < active_max_loss_cnt) {
920 /* we have some alternatives, try to mark this subflow as idle ...*/
921 slow = lock_sock_fast(ssk);
922 if (!tcp_rtx_and_write_queues_empty(ssk)) {
923 subflow->stale = 1;
924 __mptcp_retransmit_pending_data(sk);
Paolo Abenifc1b4e32021-08-13 15:15:46 -0700925 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_SUBFLOWSTALE);
Paolo Abeniff5a0b42021-08-13 15:15:45 -0700926 }
927 unlock_sock_fast(ssk, slow);
928
929 /* always try to push the pending data regarless of re-injections:
930 * we can possibly use backup subflows now, and subflow selection
931 * is cheap under the msk socket lock
932 */
933 __mptcp_push_pending(sk, 0);
934 return;
935 }
936 }
937}
938
Paolo Abeni01cacb02020-03-27 14:48:51 -0700939static int mptcp_pm_family_to_addr(int family)
940{
941#if IS_ENABLED(CONFIG_MPTCP_IPV6)
942 if (family == AF_INET6)
943 return MPTCP_PM_ADDR_ATTR_ADDR6;
944#endif
945 return MPTCP_PM_ADDR_ATTR_ADDR4;
946}
947
948static int mptcp_pm_parse_addr(struct nlattr *attr, struct genl_info *info,
949 bool require_family,
950 struct mptcp_pm_addr_entry *entry)
951{
952 struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1];
953 int err, addr_addr;
954
955 if (!attr) {
956 GENL_SET_ERR_MSG(info, "missing address info");
957 return -EINVAL;
958 }
959
960 /* no validation needed - was already done via nested policy */
961 err = nla_parse_nested_deprecated(tb, MPTCP_PM_ADDR_ATTR_MAX, attr,
962 mptcp_pm_addr_policy, info->extack);
963 if (err)
964 return err;
965
966 memset(entry, 0, sizeof(*entry));
967 if (!tb[MPTCP_PM_ADDR_ATTR_FAMILY]) {
968 if (!require_family)
969 goto skip_family;
970
971 NL_SET_ERR_MSG_ATTR(info->extack, attr,
972 "missing family");
973 return -EINVAL;
974 }
975
976 entry->addr.family = nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_FAMILY]);
977 if (entry->addr.family != AF_INET
978#if IS_ENABLED(CONFIG_MPTCP_IPV6)
979 && entry->addr.family != AF_INET6
980#endif
981 ) {
982 NL_SET_ERR_MSG_ATTR(info->extack, attr,
983 "unknown address family");
984 return -EINVAL;
985 }
986 addr_addr = mptcp_pm_family_to_addr(entry->addr.family);
987 if (!tb[addr_addr]) {
988 NL_SET_ERR_MSG_ATTR(info->extack, attr,
989 "missing address data");
990 return -EINVAL;
991 }
992
993#if IS_ENABLED(CONFIG_MPTCP_IPV6)
994 if (entry->addr.family == AF_INET6)
995 entry->addr.addr6 = nla_get_in6_addr(tb[addr_addr]);
996 else
997#endif
998 entry->addr.addr.s_addr = nla_get_in_addr(tb[addr_addr]);
999
1000skip_family:
Paolo Abenief0da3b2020-09-14 10:01:15 +02001001 if (tb[MPTCP_PM_ADDR_ATTR_IF_IDX]) {
1002 u32 val = nla_get_s32(tb[MPTCP_PM_ADDR_ATTR_IF_IDX]);
1003
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001004 entry->ifindex = val;
Paolo Abenief0da3b2020-09-14 10:01:15 +02001005 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001006
1007 if (tb[MPTCP_PM_ADDR_ATTR_ID])
1008 entry->addr.id = nla_get_u8(tb[MPTCP_PM_ADDR_ATTR_ID]);
1009
1010 if (tb[MPTCP_PM_ADDR_ATTR_FLAGS])
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001011 entry->flags = nla_get_u32(tb[MPTCP_PM_ADDR_ATTR_FLAGS]);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001012
Jianguo Wueb5fb622021-05-27 16:54:28 -07001013 if (tb[MPTCP_PM_ADDR_ATTR_PORT]) {
1014 if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) {
1015 NL_SET_ERR_MSG_ATTR(info->extack, attr,
1016 "flags must have signal when using port");
1017 return -EINVAL;
1018 }
Geliang Tanga77e9172021-02-01 15:09:17 -08001019 entry->addr.port = htons(nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_PORT]));
Jianguo Wueb5fb622021-05-27 16:54:28 -07001020 }
Geliang Tanga77e9172021-02-01 15:09:17 -08001021
Paolo Abeni01cacb02020-03-27 14:48:51 -07001022 return 0;
1023}
1024
1025static struct pm_nl_pernet *genl_info_pm_nl(struct genl_info *info)
1026{
1027 return net_generic(genl_info_net(info), pm_nl_pernet_id);
1028}
1029
Geliang Tang875b76712021-02-01 15:09:08 -08001030static int mptcp_nl_add_subflow_or_signal_addr(struct net *net)
1031{
1032 struct mptcp_sock *msk;
1033 long s_slot = 0, s_num = 0;
1034
1035 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1036 struct sock *sk = (struct sock *)msk;
1037
1038 if (!READ_ONCE(msk->fully_established))
1039 goto next;
1040
1041 lock_sock(sk);
1042 spin_lock_bh(&msk->pm.lock);
1043 mptcp_pm_create_subflow_or_signal_addr(msk);
1044 spin_unlock_bh(&msk->pm.lock);
1045 release_sock(sk);
1046
1047next:
1048 sock_put(sk);
1049 cond_resched();
1050 }
1051
1052 return 0;
1053}
1054
Paolo Abeni01cacb02020-03-27 14:48:51 -07001055static int mptcp_nl_cmd_add_addr(struct sk_buff *skb, struct genl_info *info)
1056{
1057 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1058 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1059 struct mptcp_pm_addr_entry addr, *entry;
1060 int ret;
1061
1062 ret = mptcp_pm_parse_addr(attr, info, true, &addr);
1063 if (ret < 0)
1064 return ret;
1065
1066 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1067 if (!entry) {
1068 GENL_SET_ERR_MSG(info, "can't allocate addr");
1069 return -ENOMEM;
1070 }
1071
1072 *entry = addr;
Geliang Tang1729cf12021-02-01 15:09:12 -08001073 if (entry->addr.port) {
1074 ret = mptcp_pm_nl_create_listen_socket(skb->sk, entry);
1075 if (ret) {
1076 GENL_SET_ERR_MSG(info, "create listen socket error");
1077 kfree(entry);
1078 return ret;
1079 }
1080 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001081 ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
1082 if (ret < 0) {
1083 GENL_SET_ERR_MSG(info, "too many addresses or duplicate one");
Geliang Tang1729cf12021-02-01 15:09:12 -08001084 if (entry->lsk)
1085 sock_release(entry->lsk);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001086 kfree(entry);
1087 return ret;
1088 }
1089
Geliang Tang875b76712021-02-01 15:09:08 -08001090 mptcp_nl_add_subflow_or_signal_addr(sock_net(skb->sk));
1091
Paolo Abeni01cacb02020-03-27 14:48:51 -07001092 return 0;
1093}
1094
1095static struct mptcp_pm_addr_entry *
1096__lookup_addr_by_id(struct pm_nl_pernet *pernet, unsigned int id)
1097{
1098 struct mptcp_pm_addr_entry *entry;
1099
1100 list_for_each_entry(entry, &pernet->local_addr_list, list) {
1101 if (entry->addr.id == id)
1102 return entry;
1103 }
1104 return NULL;
1105}
1106
Geliang Tangee285252021-08-17 15:07:22 -07001107int mptcp_pm_get_flags_and_ifindex_by_id(struct net *net, unsigned int id,
1108 u8 *flags, int *ifindex)
1109{
1110 struct mptcp_pm_addr_entry *entry;
1111
1112 *flags = 0;
1113 *ifindex = 0;
1114
1115 if (id) {
1116 rcu_read_lock();
1117 entry = __lookup_addr_by_id(net_generic(net, pm_nl_pernet_id), id);
1118 if (entry) {
1119 *flags = entry->flags;
1120 *ifindex = entry->ifindex;
1121 }
1122 rcu_read_unlock();
1123 }
1124
1125 return 0;
1126}
1127
Geliang Tangb6c08382020-09-24 08:29:54 +08001128static bool remove_anno_list_by_saddr(struct mptcp_sock *msk,
1129 struct mptcp_addr_info *addr)
1130{
Geliang Tang00cfd772020-09-24 08:30:02 +08001131 struct mptcp_pm_add_entry *entry;
Geliang Tangb6c08382020-09-24 08:29:54 +08001132
Davide Carattid58300c2021-05-25 14:23:13 -07001133 entry = mptcp_pm_del_add_timer(msk, addr, false);
Geliang Tang00cfd772020-09-24 08:30:02 +08001134 if (entry) {
1135 list_del(&entry->list);
1136 kfree(entry);
1137 return true;
Geliang Tangb6c08382020-09-24 08:29:54 +08001138 }
1139
1140 return false;
1141}
1142
1143static bool mptcp_pm_remove_anno_addr(struct mptcp_sock *msk,
1144 struct mptcp_addr_info *addr,
1145 bool force)
1146{
Geliang Tangcbde2782021-03-12 17:16:12 -08001147 struct mptcp_rm_list list = { .nr = 0 };
Geliang Tangb6c08382020-09-24 08:29:54 +08001148 bool ret;
1149
Geliang Tangcbde2782021-03-12 17:16:12 -08001150 list.ids[list.nr++] = addr->id;
1151
Geliang Tangb6c08382020-09-24 08:29:54 +08001152 ret = remove_anno_list_by_saddr(msk, addr);
Geliang Tang00cfd772020-09-24 08:30:02 +08001153 if (ret || force) {
1154 spin_lock_bh(&msk->pm.lock);
Geliang Tangcbde2782021-03-12 17:16:12 -08001155 mptcp_pm_remove_addr(msk, &list);
Geliang Tang00cfd772020-09-24 08:30:02 +08001156 spin_unlock_bh(&msk->pm.lock);
1157 }
Geliang Tangb6c08382020-09-24 08:29:54 +08001158 return ret;
1159}
1160
1161static int mptcp_nl_remove_subflow_and_signal_addr(struct net *net,
1162 struct mptcp_addr_info *addr)
1163{
1164 struct mptcp_sock *msk;
1165 long s_slot = 0, s_num = 0;
Geliang Tangddd14bb2021-03-12 17:16:16 -08001166 struct mptcp_rm_list list = { .nr = 0 };
Geliang Tangb6c08382020-09-24 08:29:54 +08001167
1168 pr_debug("remove_id=%d", addr->id);
1169
Geliang Tangddd14bb2021-03-12 17:16:16 -08001170 list.ids[list.nr++] = addr->id;
1171
Geliang Tangb6c08382020-09-24 08:29:54 +08001172 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1173 struct sock *sk = (struct sock *)msk;
1174 bool remove_subflow;
1175
1176 if (list_empty(&msk->conn_list)) {
1177 mptcp_pm_remove_anno_addr(msk, addr, false);
1178 goto next;
1179 }
1180
1181 lock_sock(sk);
1182 remove_subflow = lookup_subflow_by_saddr(&msk->conn_list, addr);
1183 mptcp_pm_remove_anno_addr(msk, addr, remove_subflow);
1184 if (remove_subflow)
Geliang Tangddd14bb2021-03-12 17:16:16 -08001185 mptcp_pm_remove_subflow(msk, &list);
Geliang Tangb6c08382020-09-24 08:29:54 +08001186 release_sock(sk);
1187
1188next:
1189 sock_put(sk);
1190 cond_resched();
1191 }
1192
1193 return 0;
1194}
1195
Geliang Tang1729cf12021-02-01 15:09:12 -08001196struct addr_entry_release_work {
1197 struct rcu_work rwork;
1198 struct mptcp_pm_addr_entry *entry;
1199};
1200
1201static void mptcp_pm_release_addr_entry(struct work_struct *work)
1202{
1203 struct addr_entry_release_work *w;
1204 struct mptcp_pm_addr_entry *entry;
1205
1206 w = container_of(to_rcu_work(work), struct addr_entry_release_work, rwork);
1207 entry = w->entry;
1208 if (entry) {
1209 if (entry->lsk)
1210 sock_release(entry->lsk);
1211 kfree(entry);
1212 }
1213 kfree(w);
1214}
1215
1216static void mptcp_pm_free_addr_entry(struct mptcp_pm_addr_entry *entry)
1217{
1218 struct addr_entry_release_work *w;
1219
1220 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1221 if (w) {
1222 INIT_RCU_WORK(&w->rwork, mptcp_pm_release_addr_entry);
1223 w->entry = entry;
1224 queue_rcu_work(system_wq, &w->rwork);
1225 }
1226}
1227
Geliang Tang740d7982021-03-30 17:08:53 -07001228static int mptcp_nl_remove_id_zero_address(struct net *net,
1229 struct mptcp_addr_info *addr)
1230{
1231 struct mptcp_rm_list list = { .nr = 0 };
1232 long s_slot = 0, s_num = 0;
1233 struct mptcp_sock *msk;
1234
1235 list.ids[list.nr++] = 0;
1236
1237 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1238 struct sock *sk = (struct sock *)msk;
1239 struct mptcp_addr_info msk_local;
1240
1241 if (list_empty(&msk->conn_list))
1242 goto next;
1243
1244 local_address((struct sock_common *)msk, &msk_local);
1245 if (!addresses_equal(&msk_local, addr, addr->port))
1246 goto next;
1247
1248 lock_sock(sk);
1249 spin_lock_bh(&msk->pm.lock);
1250 mptcp_pm_remove_addr(msk, &list);
1251 mptcp_pm_nl_rm_subflow_received(msk, &list);
1252 spin_unlock_bh(&msk->pm.lock);
1253 release_sock(sk);
1254
1255next:
1256 sock_put(sk);
1257 cond_resched();
1258 }
1259
1260 return 0;
1261}
1262
Paolo Abeni01cacb02020-03-27 14:48:51 -07001263static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info)
1264{
1265 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1266 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1267 struct mptcp_pm_addr_entry addr, *entry;
Geliang Tang72603d22021-02-01 15:09:06 -08001268 unsigned int addr_max;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001269 int ret;
1270
1271 ret = mptcp_pm_parse_addr(attr, info, false, &addr);
1272 if (ret < 0)
1273 return ret;
1274
Geliang Tang740d7982021-03-30 17:08:53 -07001275 /* the zero id address is special: the first address used by the msk
1276 * always gets such an id, so different subflows can have different zero
1277 * id addresses. Additionally zero id is not accounted for in id_bitmap.
1278 * Let's use an 'mptcp_rm_list' instead of the common remove code.
1279 */
1280 if (addr.addr.id == 0)
1281 return mptcp_nl_remove_id_zero_address(sock_net(skb->sk), &addr.addr);
1282
Paolo Abeni01cacb02020-03-27 14:48:51 -07001283 spin_lock_bh(&pernet->lock);
1284 entry = __lookup_addr_by_id(pernet, addr.addr.id);
1285 if (!entry) {
1286 GENL_SET_ERR_MSG(info, "address not found");
Geliang Tangb6c08382020-09-24 08:29:54 +08001287 spin_unlock_bh(&pernet->lock);
1288 return -EINVAL;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001289 }
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001290 if (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
Geliang Tang72603d22021-02-01 15:09:06 -08001291 addr_max = pernet->add_addr_signal_max;
1292 WRITE_ONCE(pernet->add_addr_signal_max, addr_max - 1);
1293 }
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001294 if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) {
Geliang Tang72603d22021-02-01 15:09:06 -08001295 addr_max = pernet->local_addr_max;
1296 WRITE_ONCE(pernet->local_addr_max, addr_max - 1);
1297 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001298
1299 pernet->addrs--;
1300 list_del_rcu(&entry->list);
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001301 __clear_bit(entry->addr.id, pernet->id_bitmap);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001302 spin_unlock_bh(&pernet->lock);
Geliang Tangb6c08382020-09-24 08:29:54 +08001303
1304 mptcp_nl_remove_subflow_and_signal_addr(sock_net(skb->sk), &entry->addr);
Geliang Tang1729cf12021-02-01 15:09:12 -08001305 mptcp_pm_free_addr_entry(entry);
Geliang Tangb6c08382020-09-24 08:29:54 +08001306
Paolo Abeni01cacb02020-03-27 14:48:51 -07001307 return ret;
1308}
1309
Geliang Tang06faa222021-03-12 17:16:17 -08001310static void mptcp_pm_remove_addrs_and_subflows(struct mptcp_sock *msk,
1311 struct list_head *rm_list)
1312{
1313 struct mptcp_rm_list alist = { .nr = 0 }, slist = { .nr = 0 };
1314 struct mptcp_pm_addr_entry *entry;
1315
1316 list_for_each_entry(entry, rm_list, list) {
1317 if (lookup_subflow_by_saddr(&msk->conn_list, &entry->addr) &&
1318 alist.nr < MPTCP_RM_IDS_MAX &&
1319 slist.nr < MPTCP_RM_IDS_MAX) {
1320 alist.ids[alist.nr++] = entry->addr.id;
1321 slist.ids[slist.nr++] = entry->addr.id;
1322 } else if (remove_anno_list_by_saddr(msk, &entry->addr) &&
1323 alist.nr < MPTCP_RM_IDS_MAX) {
1324 alist.ids[alist.nr++] = entry->addr.id;
1325 }
1326 }
1327
1328 if (alist.nr) {
1329 spin_lock_bh(&msk->pm.lock);
1330 mptcp_pm_remove_addr(msk, &alist);
1331 spin_unlock_bh(&msk->pm.lock);
1332 }
1333 if (slist.nr)
1334 mptcp_pm_remove_subflow(msk, &slist);
1335}
1336
1337static void mptcp_nl_remove_addrs_list(struct net *net,
1338 struct list_head *rm_list)
1339{
1340 long s_slot = 0, s_num = 0;
1341 struct mptcp_sock *msk;
1342
1343 if (list_empty(rm_list))
1344 return;
1345
1346 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1347 struct sock *sk = (struct sock *)msk;
1348
1349 lock_sock(sk);
1350 mptcp_pm_remove_addrs_and_subflows(msk, rm_list);
1351 release_sock(sk);
1352
1353 sock_put(sk);
1354 cond_resched();
1355 }
1356}
1357
Geliang Tang0e4a3e62021-03-12 17:16:18 -08001358static void __flush_addrs(struct list_head *list)
Paolo Abeni01cacb02020-03-27 14:48:51 -07001359{
Geliang Tang141694d2020-12-10 14:24:59 -08001360 while (!list_empty(list)) {
Paolo Abeni01cacb02020-03-27 14:48:51 -07001361 struct mptcp_pm_addr_entry *cur;
1362
Geliang Tang141694d2020-12-10 14:24:59 -08001363 cur = list_entry(list->next,
Paolo Abeni01cacb02020-03-27 14:48:51 -07001364 struct mptcp_pm_addr_entry, list);
1365 list_del_rcu(&cur->list);
Geliang Tang1729cf12021-02-01 15:09:12 -08001366 mptcp_pm_free_addr_entry(cur);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001367 }
1368}
1369
1370static void __reset_counters(struct pm_nl_pernet *pernet)
1371{
Geliang Tang72603d22021-02-01 15:09:06 -08001372 WRITE_ONCE(pernet->add_addr_signal_max, 0);
1373 WRITE_ONCE(pernet->add_addr_accept_max, 0);
1374 WRITE_ONCE(pernet->local_addr_max, 0);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001375 pernet->addrs = 0;
1376}
1377
1378static int mptcp_nl_cmd_flush_addrs(struct sk_buff *skb, struct genl_info *info)
1379{
1380 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
Geliang Tang141694d2020-12-10 14:24:59 -08001381 LIST_HEAD(free_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001382
1383 spin_lock_bh(&pernet->lock);
Geliang Tang141694d2020-12-10 14:24:59 -08001384 list_splice_init(&pernet->local_addr_list, &free_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001385 __reset_counters(pernet);
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001386 pernet->next_id = 1;
1387 bitmap_zero(pernet->id_bitmap, MAX_ADDR_ID + 1);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001388 spin_unlock_bh(&pernet->lock);
Geliang Tang0e4a3e62021-03-12 17:16:18 -08001389 mptcp_nl_remove_addrs_list(sock_net(skb->sk), &free_list);
1390 __flush_addrs(&free_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001391 return 0;
1392}
1393
1394static int mptcp_nl_fill_addr(struct sk_buff *skb,
1395 struct mptcp_pm_addr_entry *entry)
1396{
1397 struct mptcp_addr_info *addr = &entry->addr;
1398 struct nlattr *attr;
1399
1400 attr = nla_nest_start(skb, MPTCP_PM_ATTR_ADDR);
1401 if (!attr)
1402 return -EMSGSIZE;
1403
1404 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_FAMILY, addr->family))
1405 goto nla_put_failure;
Geliang Tanga77e9172021-02-01 15:09:17 -08001406 if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_PORT, ntohs(addr->port)))
1407 goto nla_put_failure;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001408 if (nla_put_u8(skb, MPTCP_PM_ADDR_ATTR_ID, addr->id))
1409 goto nla_put_failure;
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001410 if (nla_put_u32(skb, MPTCP_PM_ADDR_ATTR_FLAGS, entry->flags))
Paolo Abeni01cacb02020-03-27 14:48:51 -07001411 goto nla_put_failure;
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001412 if (entry->ifindex &&
1413 nla_put_s32(skb, MPTCP_PM_ADDR_ATTR_IF_IDX, entry->ifindex))
Paolo Abeni01cacb02020-03-27 14:48:51 -07001414 goto nla_put_failure;
1415
Bo YUb4e0f9a2020-04-23 10:10:03 +08001416 if (addr->family == AF_INET &&
1417 nla_put_in_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR4,
1418 addr->addr.s_addr))
1419 goto nla_put_failure;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001420#if IS_ENABLED(CONFIG_MPTCP_IPV6)
Bo YUb4e0f9a2020-04-23 10:10:03 +08001421 else if (addr->family == AF_INET6 &&
1422 nla_put_in6_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR6, &addr->addr6))
1423 goto nla_put_failure;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001424#endif
1425 nla_nest_end(skb, attr);
1426 return 0;
1427
1428nla_put_failure:
1429 nla_nest_cancel(skb, attr);
1430 return -EMSGSIZE;
1431}
1432
1433static int mptcp_nl_cmd_get_addr(struct sk_buff *skb, struct genl_info *info)
1434{
1435 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1436 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1437 struct mptcp_pm_addr_entry addr, *entry;
1438 struct sk_buff *msg;
1439 void *reply;
1440 int ret;
1441
1442 ret = mptcp_pm_parse_addr(attr, info, false, &addr);
1443 if (ret < 0)
1444 return ret;
1445
1446 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1447 if (!msg)
1448 return -ENOMEM;
1449
1450 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1451 info->genlhdr->cmd);
1452 if (!reply) {
1453 GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1454 ret = -EMSGSIZE;
1455 goto fail;
1456 }
1457
1458 spin_lock_bh(&pernet->lock);
1459 entry = __lookup_addr_by_id(pernet, addr.addr.id);
1460 if (!entry) {
1461 GENL_SET_ERR_MSG(info, "address not found");
1462 ret = -EINVAL;
1463 goto unlock_fail;
1464 }
1465
1466 ret = mptcp_nl_fill_addr(msg, entry);
1467 if (ret)
1468 goto unlock_fail;
1469
1470 genlmsg_end(msg, reply);
1471 ret = genlmsg_reply(msg, info);
1472 spin_unlock_bh(&pernet->lock);
1473 return ret;
1474
1475unlock_fail:
1476 spin_unlock_bh(&pernet->lock);
1477
1478fail:
1479 nlmsg_free(msg);
1480 return ret;
1481}
1482
1483static int mptcp_nl_cmd_dump_addrs(struct sk_buff *msg,
1484 struct netlink_callback *cb)
1485{
1486 struct net *net = sock_net(msg->sk);
1487 struct mptcp_pm_addr_entry *entry;
1488 struct pm_nl_pernet *pernet;
1489 int id = cb->args[0];
1490 void *hdr;
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001491 int i;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001492
1493 pernet = net_generic(net, pm_nl_pernet_id);
1494
1495 spin_lock_bh(&pernet->lock);
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001496 for (i = id; i < MAX_ADDR_ID + 1; i++) {
1497 if (test_bit(i, pernet->id_bitmap)) {
1498 entry = __lookup_addr_by_id(pernet, i);
1499 if (!entry)
1500 break;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001501
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001502 if (entry->addr.id <= id)
1503 continue;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001504
Geliang Tangefd5a4c2021-01-08 16:47:55 -08001505 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid,
1506 cb->nlh->nlmsg_seq, &mptcp_genl_family,
1507 NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR);
1508 if (!hdr)
1509 break;
1510
1511 if (mptcp_nl_fill_addr(msg, entry) < 0) {
1512 genlmsg_cancel(msg, hdr);
1513 break;
1514 }
1515
1516 id = entry->addr.id;
1517 genlmsg_end(msg, hdr);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001518 }
Paolo Abeni01cacb02020-03-27 14:48:51 -07001519 }
1520 spin_unlock_bh(&pernet->lock);
1521
1522 cb->args[0] = id;
1523 return msg->len;
1524}
1525
1526static int parse_limit(struct genl_info *info, int id, unsigned int *limit)
1527{
1528 struct nlattr *attr = info->attrs[id];
1529
1530 if (!attr)
1531 return 0;
1532
1533 *limit = nla_get_u32(attr);
1534 if (*limit > MPTCP_PM_ADDR_MAX) {
1535 GENL_SET_ERR_MSG(info, "limit greater than maximum");
1536 return -EINVAL;
1537 }
1538 return 0;
1539}
1540
1541static int
1542mptcp_nl_cmd_set_limits(struct sk_buff *skb, struct genl_info *info)
1543{
1544 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1545 unsigned int rcv_addrs, subflows;
1546 int ret;
1547
1548 spin_lock_bh(&pernet->lock);
1549 rcv_addrs = pernet->add_addr_accept_max;
1550 ret = parse_limit(info, MPTCP_PM_ATTR_RCV_ADD_ADDRS, &rcv_addrs);
1551 if (ret)
1552 goto unlock;
1553
1554 subflows = pernet->subflows_max;
1555 ret = parse_limit(info, MPTCP_PM_ATTR_SUBFLOWS, &subflows);
1556 if (ret)
1557 goto unlock;
1558
1559 WRITE_ONCE(pernet->add_addr_accept_max, rcv_addrs);
1560 WRITE_ONCE(pernet->subflows_max, subflows);
1561
1562unlock:
1563 spin_unlock_bh(&pernet->lock);
1564 return ret;
1565}
1566
1567static int
1568mptcp_nl_cmd_get_limits(struct sk_buff *skb, struct genl_info *info)
1569{
1570 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1571 struct sk_buff *msg;
1572 void *reply;
1573
1574 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1575 if (!msg)
1576 return -ENOMEM;
1577
1578 reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1579 MPTCP_PM_CMD_GET_LIMITS);
1580 if (!reply)
1581 goto fail;
1582
1583 if (nla_put_u32(msg, MPTCP_PM_ATTR_RCV_ADD_ADDRS,
1584 READ_ONCE(pernet->add_addr_accept_max)))
1585 goto fail;
1586
1587 if (nla_put_u32(msg, MPTCP_PM_ATTR_SUBFLOWS,
1588 READ_ONCE(pernet->subflows_max)))
1589 goto fail;
1590
1591 genlmsg_end(msg, reply);
1592 return genlmsg_reply(msg, info);
1593
1594fail:
1595 GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1596 nlmsg_free(msg);
1597 return -EMSGSIZE;
1598}
1599
Geliang Tang0f9f6962021-01-08 16:47:59 -08001600static int mptcp_nl_addr_backup(struct net *net,
1601 struct mptcp_addr_info *addr,
1602 u8 bkup)
1603{
1604 long s_slot = 0, s_num = 0;
1605 struct mptcp_sock *msk;
1606 int ret = -EINVAL;
1607
1608 while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
1609 struct sock *sk = (struct sock *)msk;
1610
1611 if (list_empty(&msk->conn_list))
1612 goto next;
1613
1614 lock_sock(sk);
1615 spin_lock_bh(&msk->pm.lock);
1616 ret = mptcp_pm_nl_mp_prio_send_ack(msk, addr, bkup);
1617 spin_unlock_bh(&msk->pm.lock);
1618 release_sock(sk);
1619
1620next:
1621 sock_put(sk);
1622 cond_resched();
1623 }
1624
1625 return ret;
1626}
1627
1628static int mptcp_nl_cmd_set_flags(struct sk_buff *skb, struct genl_info *info)
1629{
1630 struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
1631 struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
1632 struct mptcp_pm_addr_entry addr, *entry;
1633 struct net *net = sock_net(skb->sk);
1634 u8 bkup = 0;
1635 int ret;
1636
1637 ret = mptcp_pm_parse_addr(attr, info, true, &addr);
1638 if (ret < 0)
1639 return ret;
1640
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001641 if (addr.flags & MPTCP_PM_ADDR_FLAG_BACKUP)
Geliang Tang0f9f6962021-01-08 16:47:59 -08001642 bkup = 1;
1643
1644 list_for_each_entry(entry, &pernet->local_addr_list, list) {
1645 if (addresses_equal(&entry->addr, &addr.addr, true)) {
1646 ret = mptcp_nl_addr_backup(net, &entry->addr, bkup);
1647 if (ret)
1648 return ret;
1649
1650 if (bkup)
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001651 entry->flags |= MPTCP_PM_ADDR_FLAG_BACKUP;
Geliang Tang0f9f6962021-01-08 16:47:59 -08001652 else
Geliang Tangdaa83ab2021-04-06 17:15:57 -07001653 entry->flags &= ~MPTCP_PM_ADDR_FLAG_BACKUP;
Geliang Tang0f9f6962021-01-08 16:47:59 -08001654 }
1655 }
1656
1657 return 0;
1658}
1659
Florian Westphalb911c972021-02-12 16:00:01 -08001660static void mptcp_nl_mcast_send(struct net *net, struct sk_buff *nlskb, gfp_t gfp)
1661{
1662 genlmsg_multicast_netns(&mptcp_genl_family, net,
1663 nlskb, 0, MPTCP_PM_EV_GRP_OFFSET, gfp);
1664}
1665
1666static int mptcp_event_add_subflow(struct sk_buff *skb, const struct sock *ssk)
1667{
1668 const struct inet_sock *issk = inet_sk(ssk);
1669 const struct mptcp_subflow_context *sf;
1670
1671 if (nla_put_u16(skb, MPTCP_ATTR_FAMILY, ssk->sk_family))
1672 return -EMSGSIZE;
1673
1674 switch (ssk->sk_family) {
1675 case AF_INET:
1676 if (nla_put_in_addr(skb, MPTCP_ATTR_SADDR4, issk->inet_saddr))
1677 return -EMSGSIZE;
1678 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, issk->inet_daddr))
1679 return -EMSGSIZE;
1680 break;
1681#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1682 case AF_INET6: {
1683 const struct ipv6_pinfo *np = inet6_sk(ssk);
1684
1685 if (nla_put_in6_addr(skb, MPTCP_ATTR_SADDR6, &np->saddr))
1686 return -EMSGSIZE;
1687 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &ssk->sk_v6_daddr))
1688 return -EMSGSIZE;
1689 break;
1690 }
1691#endif
1692 default:
1693 WARN_ON_ONCE(1);
1694 return -EMSGSIZE;
1695 }
1696
1697 if (nla_put_be16(skb, MPTCP_ATTR_SPORT, issk->inet_sport))
1698 return -EMSGSIZE;
1699 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, issk->inet_dport))
1700 return -EMSGSIZE;
1701
1702 sf = mptcp_subflow_ctx(ssk);
1703 if (WARN_ON_ONCE(!sf))
1704 return -EINVAL;
1705
1706 if (nla_put_u8(skb, MPTCP_ATTR_LOC_ID, sf->local_id))
1707 return -EMSGSIZE;
1708
1709 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, sf->remote_id))
1710 return -EMSGSIZE;
1711
1712 return 0;
1713}
1714
1715static int mptcp_event_put_token_and_ssk(struct sk_buff *skb,
1716 const struct mptcp_sock *msk,
1717 const struct sock *ssk)
1718{
1719 const struct sock *sk = (const struct sock *)msk;
1720 const struct mptcp_subflow_context *sf;
1721 u8 sk_err;
1722
1723 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1724 return -EMSGSIZE;
1725
1726 if (mptcp_event_add_subflow(skb, ssk))
1727 return -EMSGSIZE;
1728
1729 sf = mptcp_subflow_ctx(ssk);
1730 if (WARN_ON_ONCE(!sf))
1731 return -EINVAL;
1732
1733 if (nla_put_u8(skb, MPTCP_ATTR_BACKUP, sf->backup))
1734 return -EMSGSIZE;
1735
1736 if (ssk->sk_bound_dev_if &&
1737 nla_put_s32(skb, MPTCP_ATTR_IF_IDX, ssk->sk_bound_dev_if))
1738 return -EMSGSIZE;
1739
1740 sk_err = ssk->sk_err;
1741 if (sk_err && sk->sk_state == TCP_ESTABLISHED &&
1742 nla_put_u8(skb, MPTCP_ATTR_ERROR, sk_err))
1743 return -EMSGSIZE;
1744
1745 return 0;
1746}
1747
1748static int mptcp_event_sub_established(struct sk_buff *skb,
1749 const struct mptcp_sock *msk,
1750 const struct sock *ssk)
1751{
1752 return mptcp_event_put_token_and_ssk(skb, msk, ssk);
1753}
1754
1755static int mptcp_event_sub_closed(struct sk_buff *skb,
1756 const struct mptcp_sock *msk,
1757 const struct sock *ssk)
1758{
Florian Westphaldc87efd2021-04-01 16:19:44 -07001759 const struct mptcp_subflow_context *sf;
1760
Florian Westphalb911c972021-02-12 16:00:01 -08001761 if (mptcp_event_put_token_and_ssk(skb, msk, ssk))
1762 return -EMSGSIZE;
1763
Florian Westphaldc87efd2021-04-01 16:19:44 -07001764 sf = mptcp_subflow_ctx(ssk);
1765 if (!sf->reset_seen)
1766 return 0;
1767
1768 if (nla_put_u32(skb, MPTCP_ATTR_RESET_REASON, sf->reset_reason))
1769 return -EMSGSIZE;
1770
1771 if (nla_put_u32(skb, MPTCP_ATTR_RESET_FLAGS, sf->reset_transient))
1772 return -EMSGSIZE;
1773
Florian Westphalb911c972021-02-12 16:00:01 -08001774 return 0;
1775}
1776
1777static int mptcp_event_created(struct sk_buff *skb,
1778 const struct mptcp_sock *msk,
1779 const struct sock *ssk)
1780{
1781 int err = nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token);
1782
1783 if (err)
1784 return err;
1785
1786 return mptcp_event_add_subflow(skb, ssk);
1787}
1788
1789void mptcp_event_addr_removed(const struct mptcp_sock *msk, uint8_t id)
1790{
1791 struct net *net = sock_net((const struct sock *)msk);
1792 struct nlmsghdr *nlh;
1793 struct sk_buff *skb;
1794
1795 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
1796 return;
1797
1798 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1799 if (!skb)
1800 return;
1801
1802 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, MPTCP_EVENT_REMOVED);
1803 if (!nlh)
1804 goto nla_put_failure;
1805
1806 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1807 goto nla_put_failure;
1808
1809 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, id))
1810 goto nla_put_failure;
1811
1812 genlmsg_end(skb, nlh);
1813 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC);
1814 return;
1815
1816nla_put_failure:
1817 kfree_skb(skb);
1818}
1819
1820void mptcp_event_addr_announced(const struct mptcp_sock *msk,
1821 const struct mptcp_addr_info *info)
1822{
1823 struct net *net = sock_net((const struct sock *)msk);
1824 struct nlmsghdr *nlh;
1825 struct sk_buff *skb;
1826
1827 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
1828 return;
1829
1830 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1831 if (!skb)
1832 return;
1833
1834 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0,
1835 MPTCP_EVENT_ANNOUNCED);
1836 if (!nlh)
1837 goto nla_put_failure;
1838
1839 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token))
1840 goto nla_put_failure;
1841
1842 if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, info->id))
1843 goto nla_put_failure;
1844
1845 if (nla_put_be16(skb, MPTCP_ATTR_DPORT, info->port))
1846 goto nla_put_failure;
1847
1848 switch (info->family) {
1849 case AF_INET:
1850 if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, info->addr.s_addr))
1851 goto nla_put_failure;
1852 break;
1853#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1854 case AF_INET6:
1855 if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &info->addr6))
1856 goto nla_put_failure;
1857 break;
1858#endif
1859 default:
1860 WARN_ON_ONCE(1);
1861 goto nla_put_failure;
1862 }
1863
1864 genlmsg_end(skb, nlh);
1865 mptcp_nl_mcast_send(net, skb, GFP_ATOMIC);
1866 return;
1867
1868nla_put_failure:
1869 kfree_skb(skb);
1870}
1871
1872void mptcp_event(enum mptcp_event_type type, const struct mptcp_sock *msk,
1873 const struct sock *ssk, gfp_t gfp)
1874{
1875 struct net *net = sock_net((const struct sock *)msk);
1876 struct nlmsghdr *nlh;
1877 struct sk_buff *skb;
1878
1879 if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET))
1880 return;
1881
1882 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
1883 if (!skb)
1884 return;
1885
1886 nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, type);
1887 if (!nlh)
1888 goto nla_put_failure;
1889
1890 switch (type) {
1891 case MPTCP_EVENT_UNSPEC:
1892 WARN_ON_ONCE(1);
1893 break;
1894 case MPTCP_EVENT_CREATED:
1895 case MPTCP_EVENT_ESTABLISHED:
1896 if (mptcp_event_created(skb, msk, ssk) < 0)
1897 goto nla_put_failure;
1898 break;
1899 case MPTCP_EVENT_CLOSED:
1900 if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token) < 0)
1901 goto nla_put_failure;
1902 break;
1903 case MPTCP_EVENT_ANNOUNCED:
1904 case MPTCP_EVENT_REMOVED:
1905 /* call mptcp_event_addr_announced()/removed instead */
1906 WARN_ON_ONCE(1);
1907 break;
1908 case MPTCP_EVENT_SUB_ESTABLISHED:
1909 case MPTCP_EVENT_SUB_PRIORITY:
1910 if (mptcp_event_sub_established(skb, msk, ssk) < 0)
1911 goto nla_put_failure;
1912 break;
1913 case MPTCP_EVENT_SUB_CLOSED:
1914 if (mptcp_event_sub_closed(skb, msk, ssk) < 0)
1915 goto nla_put_failure;
1916 break;
1917 }
1918
1919 genlmsg_end(skb, nlh);
1920 mptcp_nl_mcast_send(net, skb, gfp);
1921 return;
1922
1923nla_put_failure:
1924 kfree_skb(skb);
1925}
1926
Rikard Falkeborn674d3ab2020-10-05 01:44:16 +02001927static const struct genl_small_ops mptcp_pm_ops[] = {
Paolo Abeni01cacb02020-03-27 14:48:51 -07001928 {
1929 .cmd = MPTCP_PM_CMD_ADD_ADDR,
1930 .doit = mptcp_nl_cmd_add_addr,
1931 .flags = GENL_ADMIN_PERM,
1932 },
1933 {
1934 .cmd = MPTCP_PM_CMD_DEL_ADDR,
1935 .doit = mptcp_nl_cmd_del_addr,
1936 .flags = GENL_ADMIN_PERM,
1937 },
1938 {
1939 .cmd = MPTCP_PM_CMD_FLUSH_ADDRS,
1940 .doit = mptcp_nl_cmd_flush_addrs,
1941 .flags = GENL_ADMIN_PERM,
1942 },
1943 {
1944 .cmd = MPTCP_PM_CMD_GET_ADDR,
1945 .doit = mptcp_nl_cmd_get_addr,
1946 .dumpit = mptcp_nl_cmd_dump_addrs,
1947 },
1948 {
1949 .cmd = MPTCP_PM_CMD_SET_LIMITS,
1950 .doit = mptcp_nl_cmd_set_limits,
1951 .flags = GENL_ADMIN_PERM,
1952 },
1953 {
1954 .cmd = MPTCP_PM_CMD_GET_LIMITS,
1955 .doit = mptcp_nl_cmd_get_limits,
1956 },
Geliang Tang0f9f6962021-01-08 16:47:59 -08001957 {
1958 .cmd = MPTCP_PM_CMD_SET_FLAGS,
1959 .doit = mptcp_nl_cmd_set_flags,
1960 .flags = GENL_ADMIN_PERM,
1961 },
Paolo Abeni01cacb02020-03-27 14:48:51 -07001962};
1963
1964static struct genl_family mptcp_genl_family __ro_after_init = {
1965 .name = MPTCP_PM_NAME,
1966 .version = MPTCP_PM_VER,
1967 .maxattr = MPTCP_PM_ATTR_MAX,
1968 .policy = mptcp_pm_policy,
1969 .netnsok = true,
1970 .module = THIS_MODULE,
Jakub Kicinski66a9b922020-10-02 14:49:54 -07001971 .small_ops = mptcp_pm_ops,
1972 .n_small_ops = ARRAY_SIZE(mptcp_pm_ops),
Paolo Abeni01cacb02020-03-27 14:48:51 -07001973 .mcgrps = mptcp_pm_mcgrps,
1974 .n_mcgrps = ARRAY_SIZE(mptcp_pm_mcgrps),
1975};
1976
1977static int __net_init pm_nl_init_net(struct net *net)
1978{
1979 struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id);
1980
1981 INIT_LIST_HEAD_RCU(&pernet->local_addr_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07001982 pernet->next_id = 1;
Paolo Abeniff5a0b42021-08-13 15:15:45 -07001983 pernet->stale_loss_cnt = 4;
Paolo Abeni01cacb02020-03-27 14:48:51 -07001984 spin_lock_init(&pernet->lock);
Jianguo Wuae514982021-05-27 16:54:27 -07001985
1986 /* No need to initialize other pernet fields, the struct is zeroed at
1987 * allocation time.
1988 */
1989
Paolo Abeni01cacb02020-03-27 14:48:51 -07001990 return 0;
1991}
1992
1993static void __net_exit pm_nl_exit_net(struct list_head *net_list)
1994{
1995 struct net *net;
1996
1997 list_for_each_entry(net, net_list, exit_list) {
Geliang Tang141694d2020-12-10 14:24:59 -08001998 struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id);
1999
Paolo Abeni01cacb02020-03-27 14:48:51 -07002000 /* net is removed from namespace list, can't race with
2001 * other modifiers
2002 */
Geliang Tang0e4a3e62021-03-12 17:16:18 -08002003 __flush_addrs(&pernet->local_addr_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -07002004 }
2005}
2006
2007static struct pernet_operations mptcp_pm_pernet_ops = {
2008 .init = pm_nl_init_net,
2009 .exit_batch = pm_nl_exit_net,
2010 .id = &pm_nl_pernet_id,
2011 .size = sizeof(struct pm_nl_pernet),
2012};
2013
Paolo Abenid39dcec2020-06-26 19:29:59 +02002014void __init mptcp_pm_nl_init(void)
Paolo Abeni01cacb02020-03-27 14:48:51 -07002015{
2016 if (register_pernet_subsys(&mptcp_pm_pernet_ops) < 0)
2017 panic("Failed to register MPTCP PM pernet subsystem.\n");
2018
2019 if (genl_register_family(&mptcp_genl_family))
2020 panic("Failed to register MPTCP PM netlink family\n");
2021}