Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* Multipath TCP |
| 3 | * |
| 4 | * Copyright (c) 2020, Red Hat, Inc. |
| 5 | */ |
| 6 | |
Geliang Tang | c85adce | 2020-04-03 17:14:08 +0800 | [diff] [blame] | 7 | #define pr_fmt(fmt) "MPTCP: " fmt |
| 8 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 9 | #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 Tang | 7a7e52e | 2020-09-24 08:29:56 +0800 | [diff] [blame] | 18 | #include "mib.h" |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 19 | |
| 20 | /* forward declaration */ |
| 21 | static struct genl_family mptcp_genl_family; |
| 22 | |
| 23 | static int pm_nl_pernet_id; |
| 24 | |
| 25 | struct mptcp_pm_addr_entry { |
| 26 | struct list_head list; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 27 | struct mptcp_addr_info addr; |
| 28 | struct rcu_head rcu; |
Geliang Tang | 1729cf1 | 2021-02-01 15:09:12 -0800 | [diff] [blame] | 29 | struct socket *lsk; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 30 | }; |
| 31 | |
Geliang Tang | 0abd40f | 2020-09-24 08:30:00 +0800 | [diff] [blame] | 32 | struct mptcp_pm_add_entry { |
| 33 | struct list_head list; |
| 34 | struct mptcp_addr_info addr; |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 35 | struct timer_list add_timer; |
| 36 | struct mptcp_sock *sock; |
| 37 | u8 retrans_times; |
Geliang Tang | 0abd40f | 2020-09-24 08:30:00 +0800 | [diff] [blame] | 38 | }; |
| 39 | |
Geliang Tang | efd5a4c | 2021-01-08 16:47:55 -0800 | [diff] [blame] | 40 | #define MAX_ADDR_ID 255 |
| 41 | #define BITMAP_SZ DIV_ROUND_UP(MAX_ADDR_ID + 1, BITS_PER_LONG) |
| 42 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 43 | struct 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 Tang | efd5a4c | 2021-01-08 16:47:55 -0800 | [diff] [blame] | 53 | unsigned long id_bitmap[BITMAP_SZ]; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | #define MPTCP_PM_ADDR_MAX 8 |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 57 | #define ADD_ADDR_RETRANS_MAX 3 |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 58 | |
Florian Westphal | e980143 | 2021-02-12 15:59:54 -0800 | [diff] [blame] | 59 | static void mptcp_pm_nl_add_addr_send_ack(struct mptcp_sock *msk); |
| 60 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 61 | static 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 Baerts | 7b9b0f7 | 2021-01-25 10:59:01 -0800 | [diff] [blame] | 66 | if (a->family == b->family) { |
| 67 | if (a->family == AF_INET) |
| 68 | addr_equals = a->addr.s_addr == b->addr.s_addr; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 69 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
Matthieu Baerts | 7b9b0f7 | 2021-01-25 10:59:01 -0800 | [diff] [blame] | 70 | 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 Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 78 | #endif |
Matthieu Baerts | 7b9b0f7 | 2021-01-25 10:59:01 -0800 | [diff] [blame] | 79 | } |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 80 | |
| 81 | if (!addr_equals) |
| 82 | return false; |
| 83 | if (!use_port) |
| 84 | return true; |
| 85 | |
| 86 | return a->port == b->port; |
| 87 | } |
| 88 | |
Geliang Tang | 5702581 | 2020-09-08 10:49:38 +0800 | [diff] [blame] | 89 | static 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 Tang | 60b57bf | 2021-02-01 15:09:16 -0800 | [diff] [blame] | 96 | return addresses_equal(addr, &zero, true); |
Geliang Tang | 5702581 | 2020-09-08 10:49:38 +0800 | [diff] [blame] | 97 | } |
| 98 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 99 | static void local_address(const struct sock_common *skc, |
| 100 | struct mptcp_addr_info *addr) |
| 101 | { |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 102 | addr->family = skc->skc_family; |
Geliang Tang | 5bc5638 | 2021-02-01 15:09:15 -0800 | [diff] [blame] | 103 | addr->port = htons(skc->skc_num); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 104 | 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 | |
| 112 | static 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 | |
| 125 | static 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 Tang | 60b57bf | 2021-02-01 15:09:16 -0800 | [diff] [blame] | 136 | if (addresses_equal(&cur, saddr, saddr->port)) |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 137 | return true; |
| 138 | } |
| 139 | |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | static struct mptcp_pm_addr_entry * |
| 144 | select_local_address(const struct pm_nl_pernet *pernet, |
| 145 | struct mptcp_sock *msk) |
| 146 | { |
| 147 | struct mptcp_pm_addr_entry *entry, *ret = NULL; |
Matthieu Baerts | 7b9b0f7 | 2021-01-25 10:59:01 -0800 | [diff] [blame] | 148 | struct sock *sk = (struct sock *)msk; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 149 | |
Florian Westphal | 3abc05d | 2021-02-04 15:23:30 -0800 | [diff] [blame] | 150 | msk_owned_by_me(msk); |
| 151 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 152 | rcu_read_lock(); |
Paolo Abeni | 1bc7327 | 2020-12-10 14:25:05 -0800 | [diff] [blame] | 153 | __mptcp_flush_join_list(msk); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 154 | list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) { |
Paolo Abeni | ef0da3b | 2020-09-14 10:01:15 +0200 | [diff] [blame] | 155 | if (!(entry->addr.flags & MPTCP_PM_ADDR_FLAG_SUBFLOW)) |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 156 | continue; |
| 157 | |
Matthieu Baerts | 7b9b0f7 | 2021-01-25 10:59:01 -0800 | [diff] [blame] | 158 | if (entry->addr.family != sk->sk_family) { |
| 159 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 160 | if ((entry->addr.family == AF_INET && |
| 161 | !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) || |
| 162 | (sk->sk_family == AF_INET && |
| 163 | !ipv6_addr_v4mapped(&entry->addr.addr6))) |
| 164 | #endif |
| 165 | continue; |
| 166 | } |
| 167 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 168 | /* avoid any address already in use by subflows and |
| 169 | * pending join |
| 170 | */ |
Matthieu Baerts | 7b9b0f7 | 2021-01-25 10:59:01 -0800 | [diff] [blame] | 171 | if (!lookup_subflow_by_saddr(&msk->conn_list, &entry->addr)) { |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 172 | ret = entry; |
| 173 | break; |
| 174 | } |
| 175 | } |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 176 | rcu_read_unlock(); |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | static struct mptcp_pm_addr_entry * |
| 181 | select_signal_address(struct pm_nl_pernet *pernet, unsigned int pos) |
| 182 | { |
| 183 | struct mptcp_pm_addr_entry *entry, *ret = NULL; |
| 184 | int i = 0; |
| 185 | |
| 186 | rcu_read_lock(); |
| 187 | /* do not keep any additional per socket state, just signal |
| 188 | * the address list in order. |
| 189 | * Note: removal from the local address list during the msk life-cycle |
| 190 | * can lead to additional addresses not being announced. |
| 191 | */ |
| 192 | list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) { |
Paolo Abeni | ef0da3b | 2020-09-14 10:01:15 +0200 | [diff] [blame] | 193 | if (!(entry->addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 194 | continue; |
| 195 | if (i++ == pos) { |
| 196 | ret = entry; |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | rcu_read_unlock(); |
| 201 | return ret; |
| 202 | } |
| 203 | |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 204 | unsigned int mptcp_pm_get_add_addr_signal_max(struct mptcp_sock *msk) |
| 205 | { |
| 206 | struct pm_nl_pernet *pernet; |
| 207 | |
| 208 | pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id); |
| 209 | return READ_ONCE(pernet->add_addr_signal_max); |
| 210 | } |
| 211 | EXPORT_SYMBOL_GPL(mptcp_pm_get_add_addr_signal_max); |
| 212 | |
| 213 | unsigned int mptcp_pm_get_add_addr_accept_max(struct mptcp_sock *msk) |
| 214 | { |
| 215 | struct pm_nl_pernet *pernet; |
| 216 | |
| 217 | pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id); |
| 218 | return READ_ONCE(pernet->add_addr_accept_max); |
| 219 | } |
| 220 | EXPORT_SYMBOL_GPL(mptcp_pm_get_add_addr_accept_max); |
| 221 | |
| 222 | unsigned int mptcp_pm_get_subflows_max(struct mptcp_sock *msk) |
| 223 | { |
| 224 | struct pm_nl_pernet *pernet; |
| 225 | |
| 226 | pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id); |
| 227 | return READ_ONCE(pernet->subflows_max); |
| 228 | } |
| 229 | EXPORT_SYMBOL_GPL(mptcp_pm_get_subflows_max); |
| 230 | |
Geliang Tang | 0caf3ad | 2021-02-12 16:52:02 -0800 | [diff] [blame] | 231 | unsigned int mptcp_pm_get_local_addr_max(struct mptcp_sock *msk) |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 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->local_addr_max); |
| 237 | } |
Geliang Tang | 0caf3ad | 2021-02-12 16:52:02 -0800 | [diff] [blame] | 238 | EXPORT_SYMBOL_GPL(mptcp_pm_get_local_addr_max); |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 239 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 240 | static void check_work_pending(struct mptcp_sock *msk) |
| 241 | { |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 242 | if (msk->pm.add_addr_signaled == mptcp_pm_get_add_addr_signal_max(msk) && |
| 243 | (msk->pm.local_addr_used == mptcp_pm_get_local_addr_max(msk) || |
| 244 | msk->pm.subflows == mptcp_pm_get_subflows_max(msk))) |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 245 | WRITE_ONCE(msk->pm.work_pending, false); |
| 246 | } |
| 247 | |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 248 | static struct mptcp_pm_add_entry * |
| 249 | lookup_anno_list_by_saddr(struct mptcp_sock *msk, |
| 250 | struct mptcp_addr_info *addr) |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 251 | { |
Geliang Tang | 0abd40f | 2020-09-24 08:30:00 +0800 | [diff] [blame] | 252 | struct mptcp_pm_add_entry *entry; |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 253 | |
Florian Westphal | 3abc05d | 2021-02-04 15:23:30 -0800 | [diff] [blame] | 254 | lockdep_assert_held(&msk->pm.lock); |
| 255 | |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 256 | list_for_each_entry(entry, &msk->pm.anno_list, list) { |
Geliang Tang | 60b57bf | 2021-02-01 15:09:16 -0800 | [diff] [blame] | 257 | if (addresses_equal(&entry->addr, addr, true)) |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 258 | return entry; |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 259 | } |
| 260 | |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 261 | return NULL; |
| 262 | } |
| 263 | |
Geliang Tang | 5bc5638 | 2021-02-01 15:09:15 -0800 | [diff] [blame] | 264 | bool mptcp_pm_sport_in_anno_list(struct mptcp_sock *msk, const struct sock *sk) |
| 265 | { |
| 266 | struct mptcp_pm_add_entry *entry; |
| 267 | struct mptcp_addr_info saddr; |
| 268 | bool ret = false; |
| 269 | |
| 270 | local_address((struct sock_common *)sk, &saddr); |
| 271 | |
| 272 | spin_lock_bh(&msk->pm.lock); |
| 273 | list_for_each_entry(entry, &msk->pm.anno_list, list) { |
| 274 | if (addresses_equal(&entry->addr, &saddr, true)) { |
| 275 | ret = true; |
| 276 | goto out; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | out: |
| 281 | spin_unlock_bh(&msk->pm.lock); |
| 282 | return ret; |
| 283 | } |
| 284 | |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 285 | static void mptcp_pm_add_timer(struct timer_list *timer) |
| 286 | { |
| 287 | struct mptcp_pm_add_entry *entry = from_timer(entry, timer, add_timer); |
| 288 | struct mptcp_sock *msk = entry->sock; |
| 289 | struct sock *sk = (struct sock *)msk; |
| 290 | |
| 291 | pr_debug("msk=%p", msk); |
| 292 | |
| 293 | if (!msk) |
| 294 | return; |
| 295 | |
| 296 | if (inet_sk_state_load(sk) == TCP_CLOSE) |
| 297 | return; |
| 298 | |
| 299 | if (!entry->addr.id) |
| 300 | return; |
| 301 | |
| 302 | if (mptcp_pm_should_add_signal(msk)) { |
| 303 | sk_reset_timer(sk, timer, jiffies + TCP_RTO_MAX / 8); |
| 304 | goto out; |
| 305 | } |
| 306 | |
| 307 | spin_lock_bh(&msk->pm.lock); |
| 308 | |
| 309 | if (!mptcp_pm_should_add_signal(msk)) { |
| 310 | pr_debug("retransmit ADD_ADDR id=%d", entry->addr.id); |
Geliang Tang | 0f5c9e3 | 2020-12-09 15:51:24 -0800 | [diff] [blame] | 311 | mptcp_pm_announce_addr(msk, &entry->addr, false, entry->addr.port); |
Geliang Tang | 84dfe36 | 2020-11-19 11:46:00 -0800 | [diff] [blame] | 312 | mptcp_pm_add_addr_send_ack(msk); |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 313 | entry->retrans_times++; |
| 314 | } |
| 315 | |
| 316 | if (entry->retrans_times < ADD_ADDR_RETRANS_MAX) |
Geliang Tang | 93f323b | 2020-11-03 11:05:07 -0800 | [diff] [blame] | 317 | sk_reset_timer(sk, timer, |
Geliang Tang | 724d06b | 2020-11-10 11:01:43 +0800 | [diff] [blame] | 318 | jiffies + mptcp_get_add_addr_timeout(sock_net(sk))); |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 319 | |
| 320 | spin_unlock_bh(&msk->pm.lock); |
| 321 | |
| 322 | out: |
| 323 | __sock_put(sk); |
| 324 | } |
| 325 | |
| 326 | struct mptcp_pm_add_entry * |
| 327 | mptcp_pm_del_add_timer(struct mptcp_sock *msk, |
| 328 | struct mptcp_addr_info *addr) |
| 329 | { |
| 330 | struct mptcp_pm_add_entry *entry; |
| 331 | struct sock *sk = (struct sock *)msk; |
| 332 | |
| 333 | spin_lock_bh(&msk->pm.lock); |
| 334 | entry = lookup_anno_list_by_saddr(msk, addr); |
| 335 | if (entry) |
| 336 | entry->retrans_times = ADD_ADDR_RETRANS_MAX; |
| 337 | spin_unlock_bh(&msk->pm.lock); |
| 338 | |
| 339 | if (entry) |
| 340 | sk_stop_timer_sync(sk, &entry->add_timer); |
| 341 | |
| 342 | return entry; |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | static bool mptcp_pm_alloc_anno_list(struct mptcp_sock *msk, |
| 346 | struct mptcp_pm_addr_entry *entry) |
| 347 | { |
Geliang Tang | 0abd40f | 2020-09-24 08:30:00 +0800 | [diff] [blame] | 348 | struct mptcp_pm_add_entry *add_entry = NULL; |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 349 | struct sock *sk = (struct sock *)msk; |
Geliang Tang | 93f323b | 2020-11-03 11:05:07 -0800 | [diff] [blame] | 350 | struct net *net = sock_net(sk); |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 351 | |
Florian Westphal | 3abc05d | 2021-02-04 15:23:30 -0800 | [diff] [blame] | 352 | lockdep_assert_held(&msk->pm.lock); |
| 353 | |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 354 | if (lookup_anno_list_by_saddr(msk, &entry->addr)) |
| 355 | return false; |
| 356 | |
Geliang Tang | 0abd40f | 2020-09-24 08:30:00 +0800 | [diff] [blame] | 357 | add_entry = kmalloc(sizeof(*add_entry), GFP_ATOMIC); |
| 358 | if (!add_entry) |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 359 | return false; |
| 360 | |
Geliang Tang | 0abd40f | 2020-09-24 08:30:00 +0800 | [diff] [blame] | 361 | list_add(&add_entry->list, &msk->pm.anno_list); |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 362 | |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 363 | add_entry->addr = entry->addr; |
| 364 | add_entry->sock = msk; |
| 365 | add_entry->retrans_times = 0; |
| 366 | |
| 367 | timer_setup(&add_entry->add_timer, mptcp_pm_add_timer, 0); |
Geliang Tang | 93f323b | 2020-11-03 11:05:07 -0800 | [diff] [blame] | 368 | sk_reset_timer(sk, &add_entry->add_timer, |
| 369 | jiffies + mptcp_get_add_addr_timeout(net)); |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 370 | |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 371 | return true; |
| 372 | } |
| 373 | |
| 374 | void mptcp_pm_free_anno_list(struct mptcp_sock *msk) |
| 375 | { |
Geliang Tang | 0abd40f | 2020-09-24 08:30:00 +0800 | [diff] [blame] | 376 | struct mptcp_pm_add_entry *entry, *tmp; |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 377 | struct sock *sk = (struct sock *)msk; |
| 378 | LIST_HEAD(free_list); |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 379 | |
| 380 | pr_debug("msk=%p", msk); |
| 381 | |
| 382 | spin_lock_bh(&msk->pm.lock); |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 383 | list_splice_init(&msk->pm.anno_list, &free_list); |
| 384 | spin_unlock_bh(&msk->pm.lock); |
| 385 | |
| 386 | list_for_each_entry_safe(entry, tmp, &free_list, list) { |
| 387 | sk_stop_timer_sync(sk, &entry->add_timer); |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 388 | kfree(entry); |
| 389 | } |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 390 | } |
| 391 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 392 | static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk) |
| 393 | { |
| 394 | struct sock *sk = (struct sock *)msk; |
| 395 | struct mptcp_pm_addr_entry *local; |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 396 | unsigned int add_addr_signal_max; |
| 397 | unsigned int local_addr_max; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 398 | struct pm_nl_pernet *pernet; |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 399 | unsigned int subflows_max; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 400 | |
Geliang Tang | 432d9e7 | 2020-12-09 15:51:28 -0800 | [diff] [blame] | 401 | pernet = net_generic(sock_net(sk), pm_nl_pernet_id); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 402 | |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 403 | add_addr_signal_max = mptcp_pm_get_add_addr_signal_max(msk); |
| 404 | local_addr_max = mptcp_pm_get_local_addr_max(msk); |
| 405 | subflows_max = mptcp_pm_get_subflows_max(msk); |
| 406 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 407 | pr_debug("local %d:%d signal %d:%d subflows %d:%d\n", |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 408 | msk->pm.local_addr_used, local_addr_max, |
| 409 | msk->pm.add_addr_signaled, add_addr_signal_max, |
| 410 | msk->pm.subflows, subflows_max); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 411 | |
| 412 | /* check first for announce */ |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 413 | if (msk->pm.add_addr_signaled < add_addr_signal_max) { |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 414 | local = select_signal_address(pernet, |
| 415 | msk->pm.add_addr_signaled); |
| 416 | |
| 417 | if (local) { |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 418 | if (mptcp_pm_alloc_anno_list(msk, local)) { |
| 419 | msk->pm.add_addr_signaled++; |
Geliang Tang | 0f5c9e3 | 2020-12-09 15:51:24 -0800 | [diff] [blame] | 420 | mptcp_pm_announce_addr(msk, &local->addr, false, local->addr.port); |
Geliang Tang | 84dfe36 | 2020-11-19 11:46:00 -0800 | [diff] [blame] | 421 | mptcp_pm_nl_add_addr_send_ack(msk); |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 422 | } |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 423 | } else { |
| 424 | /* pick failed, avoid fourther attempts later */ |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 425 | msk->pm.local_addr_used = add_addr_signal_max; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | check_work_pending(msk); |
| 429 | } |
| 430 | |
| 431 | /* check if should create a new subflow */ |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 432 | if (msk->pm.local_addr_used < local_addr_max && |
| 433 | msk->pm.subflows < subflows_max) { |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 434 | local = select_local_address(pernet, msk); |
| 435 | if (local) { |
Matthieu Baerts | 1f2f193 | 2021-01-25 10:59:02 -0800 | [diff] [blame] | 436 | struct mptcp_addr_info remote = { 0 }; |
| 437 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 438 | msk->pm.local_addr_used++; |
| 439 | msk->pm.subflows++; |
| 440 | check_work_pending(msk); |
Matthieu Baerts | 1f2f193 | 2021-01-25 10:59:02 -0800 | [diff] [blame] | 441 | remote_address((struct sock_common *)sk, &remote); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 442 | spin_unlock_bh(&msk->pm.lock); |
Paolo Abeni | ef0da3b | 2020-09-14 10:01:15 +0200 | [diff] [blame] | 443 | __mptcp_subflow_connect(sk, &local->addr, &remote); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 444 | spin_lock_bh(&msk->pm.lock); |
| 445 | return; |
| 446 | } |
| 447 | |
| 448 | /* lookup failed, avoid fourther attempts later */ |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 449 | msk->pm.local_addr_used = local_addr_max; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 450 | check_work_pending(msk); |
| 451 | } |
| 452 | } |
| 453 | |
Florian Westphal | e980143 | 2021-02-12 15:59:54 -0800 | [diff] [blame] | 454 | static void mptcp_pm_nl_fully_established(struct mptcp_sock *msk) |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 455 | { |
| 456 | mptcp_pm_create_subflow_or_signal_addr(msk); |
| 457 | } |
| 458 | |
Florian Westphal | e980143 | 2021-02-12 15:59:54 -0800 | [diff] [blame] | 459 | static void mptcp_pm_nl_subflow_established(struct mptcp_sock *msk) |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 460 | { |
| 461 | mptcp_pm_create_subflow_or_signal_addr(msk); |
| 462 | } |
| 463 | |
Florian Westphal | e980143 | 2021-02-12 15:59:54 -0800 | [diff] [blame] | 464 | static void mptcp_pm_nl_add_addr_received(struct mptcp_sock *msk) |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 465 | { |
| 466 | struct sock *sk = (struct sock *)msk; |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 467 | unsigned int add_addr_accept_max; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 468 | struct mptcp_addr_info remote; |
| 469 | struct mptcp_addr_info local; |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 470 | unsigned int subflows_max; |
Geliang Tang | 0f5c9e3 | 2020-12-09 15:51:24 -0800 | [diff] [blame] | 471 | bool use_port = false; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 472 | |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 473 | add_addr_accept_max = mptcp_pm_get_add_addr_accept_max(msk); |
| 474 | subflows_max = mptcp_pm_get_subflows_max(msk); |
| 475 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 476 | pr_debug("accepted %d:%d remote family %d", |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 477 | msk->pm.add_addr_accepted, add_addr_accept_max, |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 478 | msk->pm.remote.family); |
| 479 | msk->pm.add_addr_accepted++; |
| 480 | msk->pm.subflows++; |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 481 | if (msk->pm.add_addr_accepted >= add_addr_accept_max || |
| 482 | msk->pm.subflows >= subflows_max) |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 483 | WRITE_ONCE(msk->pm.accept_addr, false); |
| 484 | |
| 485 | /* connect to the specified remote address, using whatever |
| 486 | * local address the routing configuration will pick. |
| 487 | */ |
| 488 | remote = msk->pm.remote; |
| 489 | if (!remote.port) |
| 490 | remote.port = sk->sk_dport; |
Geliang Tang | 0f5c9e3 | 2020-12-09 15:51:24 -0800 | [diff] [blame] | 491 | else |
| 492 | use_port = true; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 493 | memset(&local, 0, sizeof(local)); |
| 494 | local.family = remote.family; |
| 495 | |
| 496 | spin_unlock_bh(&msk->pm.lock); |
Geliang Tang | 432d9e7 | 2020-12-09 15:51:28 -0800 | [diff] [blame] | 497 | __mptcp_subflow_connect(sk, &local, &remote); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 498 | spin_lock_bh(&msk->pm.lock); |
Geliang Tang | 6a6c05a | 2020-09-24 08:29:50 +0800 | [diff] [blame] | 499 | |
Geliang Tang | 0f5c9e3 | 2020-12-09 15:51:24 -0800 | [diff] [blame] | 500 | mptcp_pm_announce_addr(msk, &remote, true, use_port); |
Geliang Tang | 84dfe36 | 2020-11-19 11:46:00 -0800 | [diff] [blame] | 501 | mptcp_pm_nl_add_addr_send_ack(msk); |
| 502 | } |
| 503 | |
Florian Westphal | e980143 | 2021-02-12 15:59:54 -0800 | [diff] [blame] | 504 | static void mptcp_pm_nl_add_addr_send_ack(struct mptcp_sock *msk) |
Geliang Tang | 84dfe36 | 2020-11-19 11:46:00 -0800 | [diff] [blame] | 505 | { |
| 506 | struct mptcp_subflow_context *subflow; |
| 507 | |
Florian Westphal | 3abc05d | 2021-02-04 15:23:30 -0800 | [diff] [blame] | 508 | msk_owned_by_me(msk); |
| 509 | lockdep_assert_held(&msk->pm.lock); |
| 510 | |
Geliang Tang | b5a7acd | 2021-02-01 15:09:09 -0800 | [diff] [blame] | 511 | if (!mptcp_pm_should_add_signal(msk)) |
Geliang Tang | 84dfe36 | 2020-11-19 11:46:00 -0800 | [diff] [blame] | 512 | return; |
| 513 | |
| 514 | __mptcp_flush_join_list(msk); |
| 515 | subflow = list_first_entry_or_null(&msk->conn_list, typeof(*subflow), node); |
| 516 | if (subflow) { |
| 517 | struct sock *ssk = mptcp_subflow_tcp_sock(subflow); |
| 518 | u8 add_addr; |
| 519 | |
| 520 | spin_unlock_bh(&msk->pm.lock); |
Geliang Tang | b5a7acd | 2021-02-01 15:09:09 -0800 | [diff] [blame] | 521 | pr_debug("send ack for add_addr%s%s", |
| 522 | mptcp_pm_should_add_signal_ipv6(msk) ? " [ipv6]" : "", |
| 523 | mptcp_pm_should_add_signal_port(msk) ? " [port]" : ""); |
Geliang Tang | fbe0f87 | 2020-12-09 15:51:23 -0800 | [diff] [blame] | 524 | |
Geliang Tang | 84dfe36 | 2020-11-19 11:46:00 -0800 | [diff] [blame] | 525 | lock_sock(ssk); |
| 526 | tcp_send_ack(ssk); |
| 527 | release_sock(ssk); |
| 528 | spin_lock_bh(&msk->pm.lock); |
| 529 | |
Geliang Tang | 13ad9f0 | 2020-12-09 15:51:27 -0800 | [diff] [blame] | 530 | add_addr = READ_ONCE(msk->pm.addr_signal); |
Geliang Tang | fbe0f87 | 2020-12-09 15:51:23 -0800 | [diff] [blame] | 531 | if (mptcp_pm_should_add_signal_ipv6(msk)) |
| 532 | add_addr &= ~BIT(MPTCP_ADD_ADDR_IPV6); |
| 533 | if (mptcp_pm_should_add_signal_port(msk)) |
| 534 | add_addr &= ~BIT(MPTCP_ADD_ADDR_PORT); |
Geliang Tang | 13ad9f0 | 2020-12-09 15:51:27 -0800 | [diff] [blame] | 535 | WRITE_ONCE(msk->pm.addr_signal, add_addr); |
Geliang Tang | 84dfe36 | 2020-11-19 11:46:00 -0800 | [diff] [blame] | 536 | } |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Geliang Tang | 0670654 | 2021-01-08 16:47:57 -0800 | [diff] [blame] | 539 | int mptcp_pm_nl_mp_prio_send_ack(struct mptcp_sock *msk, |
| 540 | struct mptcp_addr_info *addr, |
| 541 | u8 bkup) |
| 542 | { |
| 543 | struct mptcp_subflow_context *subflow; |
| 544 | |
| 545 | pr_debug("bkup=%d", bkup); |
| 546 | |
| 547 | mptcp_for_each_subflow(msk, subflow) { |
| 548 | struct sock *ssk = mptcp_subflow_tcp_sock(subflow); |
Geliang Tang | 0be2ac2 | 2021-01-08 16:48:01 -0800 | [diff] [blame] | 549 | struct sock *sk = (struct sock *)msk; |
Geliang Tang | 0670654 | 2021-01-08 16:47:57 -0800 | [diff] [blame] | 550 | struct mptcp_addr_info local; |
| 551 | |
| 552 | local_address((struct sock_common *)ssk, &local); |
| 553 | if (!addresses_equal(&local, addr, addr->port)) |
| 554 | continue; |
| 555 | |
| 556 | subflow->backup = bkup; |
| 557 | subflow->send_mp_prio = 1; |
| 558 | subflow->request_bkup = bkup; |
Geliang Tang | 0be2ac2 | 2021-01-08 16:48:01 -0800 | [diff] [blame] | 559 | __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIOTX); |
Geliang Tang | 0670654 | 2021-01-08 16:47:57 -0800 | [diff] [blame] | 560 | |
| 561 | spin_unlock_bh(&msk->pm.lock); |
| 562 | pr_debug("send ack for mp_prio"); |
| 563 | lock_sock(ssk); |
| 564 | tcp_send_ack(ssk); |
| 565 | release_sock(ssk); |
| 566 | spin_lock_bh(&msk->pm.lock); |
| 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | return -EINVAL; |
| 572 | } |
| 573 | |
Florian Westphal | e980143 | 2021-02-12 15:59:54 -0800 | [diff] [blame] | 574 | static void mptcp_pm_nl_rm_addr_received(struct mptcp_sock *msk) |
Geliang Tang | d0876b2 | 2020-09-24 08:29:49 +0800 | [diff] [blame] | 575 | { |
| 576 | struct mptcp_subflow_context *subflow, *tmp; |
| 577 | struct sock *sk = (struct sock *)msk; |
Geliang Tang | d0b698c | 2021-03-12 17:16:15 -0800 | [diff] [blame] | 578 | u8 i; |
Geliang Tang | d0876b2 | 2020-09-24 08:29:49 +0800 | [diff] [blame] | 579 | |
Geliang Tang | d0b698c | 2021-03-12 17:16:15 -0800 | [diff] [blame] | 580 | pr_debug("address rm_list_nr %d", msk->pm.rm_list_rx.nr); |
Geliang Tang | d0876b2 | 2020-09-24 08:29:49 +0800 | [diff] [blame] | 581 | |
Florian Westphal | 3abc05d | 2021-02-04 15:23:30 -0800 | [diff] [blame] | 582 | msk_owned_by_me(msk); |
| 583 | |
Geliang Tang | d0b698c | 2021-03-12 17:16:15 -0800 | [diff] [blame] | 584 | if (!msk->pm.rm_list_rx.nr) |
Geliang Tang | d0876b2 | 2020-09-24 08:29:49 +0800 | [diff] [blame] | 585 | return; |
| 586 | |
| 587 | if (list_empty(&msk->conn_list)) |
| 588 | return; |
| 589 | |
Geliang Tang | d0b698c | 2021-03-12 17:16:15 -0800 | [diff] [blame] | 590 | for (i = 0; i < msk->pm.rm_list_rx.nr; i++) { |
| 591 | list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) { |
| 592 | struct sock *ssk = mptcp_subflow_tcp_sock(subflow); |
| 593 | int how = RCV_SHUTDOWN | SEND_SHUTDOWN; |
Geliang Tang | d0876b2 | 2020-09-24 08:29:49 +0800 | [diff] [blame] | 594 | |
Geliang Tang | d0b698c | 2021-03-12 17:16:15 -0800 | [diff] [blame] | 595 | if (msk->pm.rm_list_rx.ids[i] != subflow->remote_id) |
| 596 | continue; |
Geliang Tang | d0876b2 | 2020-09-24 08:29:49 +0800 | [diff] [blame] | 597 | |
Geliang Tang | d0b698c | 2021-03-12 17:16:15 -0800 | [diff] [blame] | 598 | pr_debug(" -> address rm_list_ids[%d]=%u", i, msk->pm.rm_list_rx.ids[i]); |
| 599 | spin_unlock_bh(&msk->pm.lock); |
| 600 | mptcp_subflow_shutdown(sk, ssk, how); |
| 601 | mptcp_close_ssk(sk, ssk, subflow); |
| 602 | spin_lock_bh(&msk->pm.lock); |
Geliang Tang | d0876b2 | 2020-09-24 08:29:49 +0800 | [diff] [blame] | 603 | |
Geliang Tang | d0b698c | 2021-03-12 17:16:15 -0800 | [diff] [blame] | 604 | msk->pm.add_addr_accepted--; |
| 605 | msk->pm.subflows--; |
| 606 | WRITE_ONCE(msk->pm.accept_addr, true); |
Geliang Tang | d0876b2 | 2020-09-24 08:29:49 +0800 | [diff] [blame] | 607 | |
Geliang Tang | d0b698c | 2021-03-12 17:16:15 -0800 | [diff] [blame] | 608 | __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMADDR); |
Geliang Tang | 7a7e52e | 2020-09-24 08:29:56 +0800 | [diff] [blame] | 609 | |
Geliang Tang | d0b698c | 2021-03-12 17:16:15 -0800 | [diff] [blame] | 610 | break; |
| 611 | } |
Geliang Tang | d0876b2 | 2020-09-24 08:29:49 +0800 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | |
Florian Westphal | e980143 | 2021-02-12 15:59:54 -0800 | [diff] [blame] | 615 | void mptcp_pm_nl_work(struct mptcp_sock *msk) |
| 616 | { |
| 617 | struct mptcp_pm_data *pm = &msk->pm; |
| 618 | |
| 619 | msk_owned_by_me(msk); |
| 620 | |
| 621 | spin_lock_bh(&msk->pm.lock); |
| 622 | |
| 623 | pr_debug("msk=%p status=%x", msk, pm->status); |
| 624 | if (pm->status & BIT(MPTCP_PM_ADD_ADDR_RECEIVED)) { |
| 625 | pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_RECEIVED); |
| 626 | mptcp_pm_nl_add_addr_received(msk); |
| 627 | } |
| 628 | if (pm->status & BIT(MPTCP_PM_ADD_ADDR_SEND_ACK)) { |
| 629 | pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_SEND_ACK); |
| 630 | mptcp_pm_nl_add_addr_send_ack(msk); |
| 631 | } |
| 632 | if (pm->status & BIT(MPTCP_PM_RM_ADDR_RECEIVED)) { |
| 633 | pm->status &= ~BIT(MPTCP_PM_RM_ADDR_RECEIVED); |
| 634 | mptcp_pm_nl_rm_addr_received(msk); |
| 635 | } |
| 636 | if (pm->status & BIT(MPTCP_PM_ESTABLISHED)) { |
| 637 | pm->status &= ~BIT(MPTCP_PM_ESTABLISHED); |
| 638 | mptcp_pm_nl_fully_established(msk); |
| 639 | } |
| 640 | if (pm->status & BIT(MPTCP_PM_SUBFLOW_ESTABLISHED)) { |
| 641 | pm->status &= ~BIT(MPTCP_PM_SUBFLOW_ESTABLISHED); |
| 642 | mptcp_pm_nl_subflow_established(msk); |
| 643 | } |
| 644 | |
| 645 | spin_unlock_bh(&msk->pm.lock); |
| 646 | } |
| 647 | |
Geliang Tang | ddd14bb | 2021-03-12 17:16:16 -0800 | [diff] [blame^] | 648 | void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk, |
| 649 | const struct mptcp_rm_list *rm_list) |
Geliang Tang | 0ee4261 | 2020-09-24 08:29:55 +0800 | [diff] [blame] | 650 | { |
| 651 | struct mptcp_subflow_context *subflow, *tmp; |
| 652 | struct sock *sk = (struct sock *)msk; |
Geliang Tang | ddd14bb | 2021-03-12 17:16:16 -0800 | [diff] [blame^] | 653 | u8 i; |
Geliang Tang | 0ee4261 | 2020-09-24 08:29:55 +0800 | [diff] [blame] | 654 | |
Geliang Tang | ddd14bb | 2021-03-12 17:16:16 -0800 | [diff] [blame^] | 655 | pr_debug("subflow rm_list_nr %d", rm_list->nr); |
Geliang Tang | 0ee4261 | 2020-09-24 08:29:55 +0800 | [diff] [blame] | 656 | |
Florian Westphal | 3abc05d | 2021-02-04 15:23:30 -0800 | [diff] [blame] | 657 | msk_owned_by_me(msk); |
| 658 | |
Geliang Tang | ddd14bb | 2021-03-12 17:16:16 -0800 | [diff] [blame^] | 659 | if (!rm_list->nr) |
Geliang Tang | 0ee4261 | 2020-09-24 08:29:55 +0800 | [diff] [blame] | 660 | return; |
| 661 | |
| 662 | if (list_empty(&msk->conn_list)) |
| 663 | return; |
| 664 | |
Geliang Tang | ddd14bb | 2021-03-12 17:16:16 -0800 | [diff] [blame^] | 665 | for (i = 0; i < rm_list->nr; i++) { |
| 666 | list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) { |
| 667 | struct sock *ssk = mptcp_subflow_tcp_sock(subflow); |
| 668 | int how = RCV_SHUTDOWN | SEND_SHUTDOWN; |
Geliang Tang | 0ee4261 | 2020-09-24 08:29:55 +0800 | [diff] [blame] | 669 | |
Geliang Tang | ddd14bb | 2021-03-12 17:16:16 -0800 | [diff] [blame^] | 670 | if (rm_list->ids[i] != subflow->local_id) |
| 671 | continue; |
Geliang Tang | 0ee4261 | 2020-09-24 08:29:55 +0800 | [diff] [blame] | 672 | |
Geliang Tang | ddd14bb | 2021-03-12 17:16:16 -0800 | [diff] [blame^] | 673 | pr_debug(" -> subflow rm_list_ids[%d]=%u", i, rm_list->ids[i]); |
| 674 | spin_unlock_bh(&msk->pm.lock); |
| 675 | mptcp_subflow_shutdown(sk, ssk, how); |
| 676 | mptcp_close_ssk(sk, ssk, subflow); |
| 677 | spin_lock_bh(&msk->pm.lock); |
Geliang Tang | 0ee4261 | 2020-09-24 08:29:55 +0800 | [diff] [blame] | 678 | |
Geliang Tang | ddd14bb | 2021-03-12 17:16:16 -0800 | [diff] [blame^] | 679 | msk->pm.local_addr_used--; |
| 680 | msk->pm.subflows--; |
Geliang Tang | 0ee4261 | 2020-09-24 08:29:55 +0800 | [diff] [blame] | 681 | |
Geliang Tang | ddd14bb | 2021-03-12 17:16:16 -0800 | [diff] [blame^] | 682 | __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMSUBFLOW); |
Geliang Tang | 7a7e52e | 2020-09-24 08:29:56 +0800 | [diff] [blame] | 683 | |
Geliang Tang | ddd14bb | 2021-03-12 17:16:16 -0800 | [diff] [blame^] | 684 | break; |
| 685 | } |
Geliang Tang | 0ee4261 | 2020-09-24 08:29:55 +0800 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 689 | static bool address_use_port(struct mptcp_pm_addr_entry *entry) |
| 690 | { |
Paolo Abeni | ef0da3b | 2020-09-14 10:01:15 +0200 | [diff] [blame] | 691 | return (entry->addr.flags & |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 692 | (MPTCP_PM_ADDR_FLAG_SIGNAL | MPTCP_PM_ADDR_FLAG_SUBFLOW)) == |
| 693 | MPTCP_PM_ADDR_FLAG_SIGNAL; |
| 694 | } |
| 695 | |
| 696 | static 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 Tang | 72603d2 | 2021-02-01 15:09:06 -0800 | [diff] [blame] | 700 | unsigned int addr_max; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 701 | 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 Tang | efd5a4c | 2021-01-08 16:47:55 -0800 | [diff] [blame] | 707 | if (pernet->next_id == MAX_ADDR_ID) |
| 708 | pernet->next_id = 1; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 709 | if (pernet->addrs >= MPTCP_PM_ADDR_MAX) |
| 710 | goto out; |
Geliang Tang | efd5a4c | 2021-01-08 16:47:55 -0800 | [diff] [blame] | 711 | if (test_bit(entry->addr.id, pernet->id_bitmap)) |
| 712 | goto out; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 713 | |
| 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 Tang | efd5a4c | 2021-01-08 16:47:55 -0800 | [diff] [blame] | 724 | if (!entry->addr.id) { |
| 725 | find_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 Tang | 72603d2 | 2021-02-01 15:09:06 -0800 | [diff] [blame] | 743 | if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL) { |
| 744 | addr_max = pernet->add_addr_signal_max; |
| 745 | WRITE_ONCE(pernet->add_addr_signal_max, addr_max + 1); |
| 746 | } |
| 747 | if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) { |
| 748 | addr_max = pernet->local_addr_max; |
| 749 | WRITE_ONCE(pernet->local_addr_max, addr_max + 1); |
| 750 | } |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 751 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 752 | pernet->addrs++; |
| 753 | list_add_tail_rcu(&entry->list, &pernet->local_addr_list); |
| 754 | ret = entry->addr.id; |
| 755 | |
| 756 | out: |
| 757 | spin_unlock_bh(&pernet->lock); |
| 758 | return ret; |
| 759 | } |
| 760 | |
Geliang Tang | 1729cf1 | 2021-02-01 15:09:12 -0800 | [diff] [blame] | 761 | static 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 | |
| 803 | out: |
| 804 | sock_release(entry->lsk); |
| 805 | return err; |
| 806 | } |
| 807 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 808 | int 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 Tang | 5702581 | 2020-09-08 10:49:38 +0800 | [diff] [blame] | 823 | local_address((struct sock_common *)skc, &skc_local); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 824 | if (addresses_equal(&msk_local, &skc_local, false)) |
| 825 | return 0; |
| 826 | |
Geliang Tang | 5702581 | 2020-09-08 10:49:38 +0800 | [diff] [blame] | 827 | if (address_zero(&skc_local)) |
| 828 | return 0; |
| 829 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 830 | 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 Tang | 60b57bf | 2021-02-01 15:09:16 -0800 | [diff] [blame] | 834 | if (addresses_equal(&entry->addr, &skc_local, entry->addr.port)) { |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 835 | 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 Tang | f612eb7 | 2020-09-09 11:01:24 +0800 | [diff] [blame] | 844 | entry = kmalloc(sizeof(*entry), GFP_ATOMIC); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 845 | if (!entry) |
| 846 | return -ENOMEM; |
| 847 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 848 | entry->addr = skc_local; |
Paolo Abeni | ef0da3b | 2020-09-14 10:01:15 +0200 | [diff] [blame] | 849 | entry->addr.ifindex = 0; |
| 850 | entry->addr.flags = 0; |
Geliang Tang | efd5a4c | 2021-01-08 16:47:55 -0800 | [diff] [blame] | 851 | entry->addr.id = 0; |
Geliang Tang | 1729cf1 | 2021-02-01 15:09:12 -0800 | [diff] [blame] | 852 | entry->addr.port = 0; |
| 853 | entry->lsk = NULL; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 854 | ret = mptcp_pm_nl_append_new_local_addr(pernet, entry); |
| 855 | if (ret < 0) |
| 856 | kfree(entry); |
| 857 | |
| 858 | return ret; |
| 859 | } |
| 860 | |
| 861 | void mptcp_pm_nl_data_init(struct mptcp_sock *msk) |
| 862 | { |
| 863 | struct mptcp_pm_data *pm = &msk->pm; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 864 | bool subflows; |
| 865 | |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 866 | 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 Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 870 | WRITE_ONCE(pm->accept_subflow, subflows); |
| 871 | } |
| 872 | |
Florian Westphal | b911c97 | 2021-02-12 16:00:01 -0800 | [diff] [blame] | 873 | #define MPTCP_PM_CMD_GRP_OFFSET 0 |
| 874 | #define MPTCP_PM_EV_GRP_OFFSET 1 |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 875 | |
| 876 | static const struct genl_multicast_group mptcp_pm_mcgrps[] = { |
| 877 | [MPTCP_PM_CMD_GRP_OFFSET] = { .name = MPTCP_PM_CMD_GRP_NAME, }, |
Florian Westphal | b911c97 | 2021-02-12 16:00:01 -0800 | [diff] [blame] | 878 | [MPTCP_PM_EV_GRP_OFFSET] = { .name = MPTCP_PM_EV_GRP_NAME, |
| 879 | .flags = GENL_UNS_ADMIN_PERM, |
| 880 | }, |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 881 | }; |
| 882 | |
| 883 | static const struct nla_policy |
| 884 | mptcp_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 Berg | 8140860 | 2020-08-18 10:17:31 +0200 | [diff] [blame] | 888 | [MPTCP_PM_ADDR_ATTR_ADDR6] = |
| 889 | NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)), |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 890 | [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 | |
| 895 | static 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 | |
| 902 | static int mptcp_pm_family_to_addr(int family) |
| 903 | { |
| 904 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 905 | if (family == AF_INET6) |
| 906 | return MPTCP_PM_ADDR_ATTR_ADDR6; |
| 907 | #endif |
| 908 | return MPTCP_PM_ADDR_ATTR_ADDR4; |
| 909 | } |
| 910 | |
| 911 | static int mptcp_pm_parse_addr(struct nlattr *attr, struct genl_info *info, |
| 912 | bool require_family, |
| 913 | struct mptcp_pm_addr_entry *entry) |
| 914 | { |
| 915 | struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1]; |
| 916 | int err, addr_addr; |
| 917 | |
| 918 | if (!attr) { |
| 919 | GENL_SET_ERR_MSG(info, "missing address info"); |
| 920 | return -EINVAL; |
| 921 | } |
| 922 | |
| 923 | /* no validation needed - was already done via nested policy */ |
| 924 | err = nla_parse_nested_deprecated(tb, MPTCP_PM_ADDR_ATTR_MAX, attr, |
| 925 | mptcp_pm_addr_policy, info->extack); |
| 926 | if (err) |
| 927 | return err; |
| 928 | |
| 929 | memset(entry, 0, sizeof(*entry)); |
| 930 | if (!tb[MPTCP_PM_ADDR_ATTR_FAMILY]) { |
| 931 | if (!require_family) |
| 932 | goto skip_family; |
| 933 | |
| 934 | NL_SET_ERR_MSG_ATTR(info->extack, attr, |
| 935 | "missing family"); |
| 936 | return -EINVAL; |
| 937 | } |
| 938 | |
| 939 | entry->addr.family = nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_FAMILY]); |
| 940 | if (entry->addr.family != AF_INET |
| 941 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 942 | && entry->addr.family != AF_INET6 |
| 943 | #endif |
| 944 | ) { |
| 945 | NL_SET_ERR_MSG_ATTR(info->extack, attr, |
| 946 | "unknown address family"); |
| 947 | return -EINVAL; |
| 948 | } |
| 949 | addr_addr = mptcp_pm_family_to_addr(entry->addr.family); |
| 950 | if (!tb[addr_addr]) { |
| 951 | NL_SET_ERR_MSG_ATTR(info->extack, attr, |
| 952 | "missing address data"); |
| 953 | return -EINVAL; |
| 954 | } |
| 955 | |
| 956 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 957 | if (entry->addr.family == AF_INET6) |
| 958 | entry->addr.addr6 = nla_get_in6_addr(tb[addr_addr]); |
| 959 | else |
| 960 | #endif |
| 961 | entry->addr.addr.s_addr = nla_get_in_addr(tb[addr_addr]); |
| 962 | |
| 963 | skip_family: |
Paolo Abeni | ef0da3b | 2020-09-14 10:01:15 +0200 | [diff] [blame] | 964 | if (tb[MPTCP_PM_ADDR_ATTR_IF_IDX]) { |
| 965 | u32 val = nla_get_s32(tb[MPTCP_PM_ADDR_ATTR_IF_IDX]); |
| 966 | |
| 967 | entry->addr.ifindex = val; |
| 968 | } |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 969 | |
| 970 | if (tb[MPTCP_PM_ADDR_ATTR_ID]) |
| 971 | entry->addr.id = nla_get_u8(tb[MPTCP_PM_ADDR_ATTR_ID]); |
| 972 | |
| 973 | if (tb[MPTCP_PM_ADDR_ATTR_FLAGS]) |
Paolo Abeni | ef0da3b | 2020-09-14 10:01:15 +0200 | [diff] [blame] | 974 | entry->addr.flags = nla_get_u32(tb[MPTCP_PM_ADDR_ATTR_FLAGS]); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 975 | |
Geliang Tang | a77e917 | 2021-02-01 15:09:17 -0800 | [diff] [blame] | 976 | if (tb[MPTCP_PM_ADDR_ATTR_PORT]) |
| 977 | entry->addr.port = htons(nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_PORT])); |
| 978 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 979 | return 0; |
| 980 | } |
| 981 | |
| 982 | static struct pm_nl_pernet *genl_info_pm_nl(struct genl_info *info) |
| 983 | { |
| 984 | return net_generic(genl_info_net(info), pm_nl_pernet_id); |
| 985 | } |
| 986 | |
Geliang Tang | 875b7671 | 2021-02-01 15:09:08 -0800 | [diff] [blame] | 987 | static int mptcp_nl_add_subflow_or_signal_addr(struct net *net) |
| 988 | { |
| 989 | struct mptcp_sock *msk; |
| 990 | long s_slot = 0, s_num = 0; |
| 991 | |
| 992 | while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { |
| 993 | struct sock *sk = (struct sock *)msk; |
| 994 | |
| 995 | if (!READ_ONCE(msk->fully_established)) |
| 996 | goto next; |
| 997 | |
| 998 | lock_sock(sk); |
| 999 | spin_lock_bh(&msk->pm.lock); |
| 1000 | mptcp_pm_create_subflow_or_signal_addr(msk); |
| 1001 | spin_unlock_bh(&msk->pm.lock); |
| 1002 | release_sock(sk); |
| 1003 | |
| 1004 | next: |
| 1005 | sock_put(sk); |
| 1006 | cond_resched(); |
| 1007 | } |
| 1008 | |
| 1009 | return 0; |
| 1010 | } |
| 1011 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1012 | static int mptcp_nl_cmd_add_addr(struct sk_buff *skb, struct genl_info *info) |
| 1013 | { |
| 1014 | struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR]; |
| 1015 | struct pm_nl_pernet *pernet = genl_info_pm_nl(info); |
| 1016 | struct mptcp_pm_addr_entry addr, *entry; |
| 1017 | int ret; |
| 1018 | |
| 1019 | ret = mptcp_pm_parse_addr(attr, info, true, &addr); |
| 1020 | if (ret < 0) |
| 1021 | return ret; |
| 1022 | |
| 1023 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 1024 | if (!entry) { |
| 1025 | GENL_SET_ERR_MSG(info, "can't allocate addr"); |
| 1026 | return -ENOMEM; |
| 1027 | } |
| 1028 | |
| 1029 | *entry = addr; |
Geliang Tang | 1729cf1 | 2021-02-01 15:09:12 -0800 | [diff] [blame] | 1030 | if (entry->addr.port) { |
| 1031 | ret = mptcp_pm_nl_create_listen_socket(skb->sk, entry); |
| 1032 | if (ret) { |
| 1033 | GENL_SET_ERR_MSG(info, "create listen socket error"); |
| 1034 | kfree(entry); |
| 1035 | return ret; |
| 1036 | } |
| 1037 | } |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1038 | ret = mptcp_pm_nl_append_new_local_addr(pernet, entry); |
| 1039 | if (ret < 0) { |
| 1040 | GENL_SET_ERR_MSG(info, "too many addresses or duplicate one"); |
Geliang Tang | 1729cf1 | 2021-02-01 15:09:12 -0800 | [diff] [blame] | 1041 | if (entry->lsk) |
| 1042 | sock_release(entry->lsk); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1043 | kfree(entry); |
| 1044 | return ret; |
| 1045 | } |
| 1046 | |
Geliang Tang | 875b7671 | 2021-02-01 15:09:08 -0800 | [diff] [blame] | 1047 | mptcp_nl_add_subflow_or_signal_addr(sock_net(skb->sk)); |
| 1048 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1049 | return 0; |
| 1050 | } |
| 1051 | |
| 1052 | static struct mptcp_pm_addr_entry * |
| 1053 | __lookup_addr_by_id(struct pm_nl_pernet *pernet, unsigned int id) |
| 1054 | { |
| 1055 | struct mptcp_pm_addr_entry *entry; |
| 1056 | |
| 1057 | list_for_each_entry(entry, &pernet->local_addr_list, list) { |
| 1058 | if (entry->addr.id == id) |
| 1059 | return entry; |
| 1060 | } |
| 1061 | return NULL; |
| 1062 | } |
| 1063 | |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 1064 | static bool remove_anno_list_by_saddr(struct mptcp_sock *msk, |
| 1065 | struct mptcp_addr_info *addr) |
| 1066 | { |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 1067 | struct mptcp_pm_add_entry *entry; |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 1068 | |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 1069 | entry = mptcp_pm_del_add_timer(msk, addr); |
| 1070 | if (entry) { |
| 1071 | list_del(&entry->list); |
| 1072 | kfree(entry); |
| 1073 | return true; |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | return false; |
| 1077 | } |
| 1078 | |
| 1079 | static bool mptcp_pm_remove_anno_addr(struct mptcp_sock *msk, |
| 1080 | struct mptcp_addr_info *addr, |
| 1081 | bool force) |
| 1082 | { |
Geliang Tang | cbde278 | 2021-03-12 17:16:12 -0800 | [diff] [blame] | 1083 | struct mptcp_rm_list list = { .nr = 0 }; |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 1084 | bool ret; |
| 1085 | |
Geliang Tang | cbde278 | 2021-03-12 17:16:12 -0800 | [diff] [blame] | 1086 | list.ids[list.nr++] = addr->id; |
| 1087 | |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 1088 | ret = remove_anno_list_by_saddr(msk, addr); |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 1089 | if (ret || force) { |
| 1090 | spin_lock_bh(&msk->pm.lock); |
Geliang Tang | cbde278 | 2021-03-12 17:16:12 -0800 | [diff] [blame] | 1091 | mptcp_pm_remove_addr(msk, &list); |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 1092 | spin_unlock_bh(&msk->pm.lock); |
| 1093 | } |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 1094 | return ret; |
| 1095 | } |
| 1096 | |
| 1097 | static int mptcp_nl_remove_subflow_and_signal_addr(struct net *net, |
| 1098 | struct mptcp_addr_info *addr) |
| 1099 | { |
| 1100 | struct mptcp_sock *msk; |
| 1101 | long s_slot = 0, s_num = 0; |
Geliang Tang | ddd14bb | 2021-03-12 17:16:16 -0800 | [diff] [blame^] | 1102 | struct mptcp_rm_list list = { .nr = 0 }; |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 1103 | |
| 1104 | pr_debug("remove_id=%d", addr->id); |
| 1105 | |
Geliang Tang | ddd14bb | 2021-03-12 17:16:16 -0800 | [diff] [blame^] | 1106 | list.ids[list.nr++] = addr->id; |
| 1107 | |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 1108 | while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { |
| 1109 | struct sock *sk = (struct sock *)msk; |
| 1110 | bool remove_subflow; |
| 1111 | |
| 1112 | if (list_empty(&msk->conn_list)) { |
| 1113 | mptcp_pm_remove_anno_addr(msk, addr, false); |
| 1114 | goto next; |
| 1115 | } |
| 1116 | |
| 1117 | lock_sock(sk); |
| 1118 | remove_subflow = lookup_subflow_by_saddr(&msk->conn_list, addr); |
| 1119 | mptcp_pm_remove_anno_addr(msk, addr, remove_subflow); |
| 1120 | if (remove_subflow) |
Geliang Tang | ddd14bb | 2021-03-12 17:16:16 -0800 | [diff] [blame^] | 1121 | mptcp_pm_remove_subflow(msk, &list); |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 1122 | release_sock(sk); |
| 1123 | |
| 1124 | next: |
| 1125 | sock_put(sk); |
| 1126 | cond_resched(); |
| 1127 | } |
| 1128 | |
| 1129 | return 0; |
| 1130 | } |
| 1131 | |
Geliang Tang | 1729cf1 | 2021-02-01 15:09:12 -0800 | [diff] [blame] | 1132 | struct addr_entry_release_work { |
| 1133 | struct rcu_work rwork; |
| 1134 | struct mptcp_pm_addr_entry *entry; |
| 1135 | }; |
| 1136 | |
| 1137 | static void mptcp_pm_release_addr_entry(struct work_struct *work) |
| 1138 | { |
| 1139 | struct addr_entry_release_work *w; |
| 1140 | struct mptcp_pm_addr_entry *entry; |
| 1141 | |
| 1142 | w = container_of(to_rcu_work(work), struct addr_entry_release_work, rwork); |
| 1143 | entry = w->entry; |
| 1144 | if (entry) { |
| 1145 | if (entry->lsk) |
| 1146 | sock_release(entry->lsk); |
| 1147 | kfree(entry); |
| 1148 | } |
| 1149 | kfree(w); |
| 1150 | } |
| 1151 | |
| 1152 | static void mptcp_pm_free_addr_entry(struct mptcp_pm_addr_entry *entry) |
| 1153 | { |
| 1154 | struct addr_entry_release_work *w; |
| 1155 | |
| 1156 | w = kmalloc(sizeof(*w), GFP_ATOMIC); |
| 1157 | if (w) { |
| 1158 | INIT_RCU_WORK(&w->rwork, mptcp_pm_release_addr_entry); |
| 1159 | w->entry = entry; |
| 1160 | queue_rcu_work(system_wq, &w->rwork); |
| 1161 | } |
| 1162 | } |
| 1163 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1164 | static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info) |
| 1165 | { |
| 1166 | struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR]; |
| 1167 | struct pm_nl_pernet *pernet = genl_info_pm_nl(info); |
| 1168 | struct mptcp_pm_addr_entry addr, *entry; |
Geliang Tang | 72603d2 | 2021-02-01 15:09:06 -0800 | [diff] [blame] | 1169 | unsigned int addr_max; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1170 | int ret; |
| 1171 | |
| 1172 | ret = mptcp_pm_parse_addr(attr, info, false, &addr); |
| 1173 | if (ret < 0) |
| 1174 | return ret; |
| 1175 | |
| 1176 | spin_lock_bh(&pernet->lock); |
| 1177 | entry = __lookup_addr_by_id(pernet, addr.addr.id); |
| 1178 | if (!entry) { |
| 1179 | GENL_SET_ERR_MSG(info, "address not found"); |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 1180 | spin_unlock_bh(&pernet->lock); |
| 1181 | return -EINVAL; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1182 | } |
Geliang Tang | 72603d2 | 2021-02-01 15:09:06 -0800 | [diff] [blame] | 1183 | if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL) { |
| 1184 | addr_max = pernet->add_addr_signal_max; |
| 1185 | WRITE_ONCE(pernet->add_addr_signal_max, addr_max - 1); |
| 1186 | } |
| 1187 | if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) { |
| 1188 | addr_max = pernet->local_addr_max; |
| 1189 | WRITE_ONCE(pernet->local_addr_max, addr_max - 1); |
| 1190 | } |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1191 | |
| 1192 | pernet->addrs--; |
| 1193 | list_del_rcu(&entry->list); |
Geliang Tang | efd5a4c | 2021-01-08 16:47:55 -0800 | [diff] [blame] | 1194 | __clear_bit(entry->addr.id, pernet->id_bitmap); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1195 | spin_unlock_bh(&pernet->lock); |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 1196 | |
| 1197 | mptcp_nl_remove_subflow_and_signal_addr(sock_net(skb->sk), &entry->addr); |
Geliang Tang | 1729cf1 | 2021-02-01 15:09:12 -0800 | [diff] [blame] | 1198 | mptcp_pm_free_addr_entry(entry); |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 1199 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1200 | return ret; |
| 1201 | } |
| 1202 | |
Geliang Tang | 141694d | 2020-12-10 14:24:59 -0800 | [diff] [blame] | 1203 | static void __flush_addrs(struct net *net, struct list_head *list) |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1204 | { |
Geliang Tang | 141694d | 2020-12-10 14:24:59 -0800 | [diff] [blame] | 1205 | while (!list_empty(list)) { |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1206 | struct mptcp_pm_addr_entry *cur; |
| 1207 | |
Geliang Tang | 141694d | 2020-12-10 14:24:59 -0800 | [diff] [blame] | 1208 | cur = list_entry(list->next, |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1209 | struct mptcp_pm_addr_entry, list); |
Geliang Tang | 141694d | 2020-12-10 14:24:59 -0800 | [diff] [blame] | 1210 | mptcp_nl_remove_subflow_and_signal_addr(net, &cur->addr); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1211 | list_del_rcu(&cur->list); |
Geliang Tang | 1729cf1 | 2021-02-01 15:09:12 -0800 | [diff] [blame] | 1212 | mptcp_pm_free_addr_entry(cur); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | static void __reset_counters(struct pm_nl_pernet *pernet) |
| 1217 | { |
Geliang Tang | 72603d2 | 2021-02-01 15:09:06 -0800 | [diff] [blame] | 1218 | WRITE_ONCE(pernet->add_addr_signal_max, 0); |
| 1219 | WRITE_ONCE(pernet->add_addr_accept_max, 0); |
| 1220 | WRITE_ONCE(pernet->local_addr_max, 0); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1221 | pernet->addrs = 0; |
| 1222 | } |
| 1223 | |
| 1224 | static int mptcp_nl_cmd_flush_addrs(struct sk_buff *skb, struct genl_info *info) |
| 1225 | { |
| 1226 | struct pm_nl_pernet *pernet = genl_info_pm_nl(info); |
Geliang Tang | 141694d | 2020-12-10 14:24:59 -0800 | [diff] [blame] | 1227 | LIST_HEAD(free_list); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1228 | |
| 1229 | spin_lock_bh(&pernet->lock); |
Geliang Tang | 141694d | 2020-12-10 14:24:59 -0800 | [diff] [blame] | 1230 | list_splice_init(&pernet->local_addr_list, &free_list); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1231 | __reset_counters(pernet); |
Geliang Tang | efd5a4c | 2021-01-08 16:47:55 -0800 | [diff] [blame] | 1232 | pernet->next_id = 1; |
| 1233 | bitmap_zero(pernet->id_bitmap, MAX_ADDR_ID + 1); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1234 | spin_unlock_bh(&pernet->lock); |
Geliang Tang | 141694d | 2020-12-10 14:24:59 -0800 | [diff] [blame] | 1235 | __flush_addrs(sock_net(skb->sk), &free_list); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1236 | return 0; |
| 1237 | } |
| 1238 | |
| 1239 | static int mptcp_nl_fill_addr(struct sk_buff *skb, |
| 1240 | struct mptcp_pm_addr_entry *entry) |
| 1241 | { |
| 1242 | struct mptcp_addr_info *addr = &entry->addr; |
| 1243 | struct nlattr *attr; |
| 1244 | |
| 1245 | attr = nla_nest_start(skb, MPTCP_PM_ATTR_ADDR); |
| 1246 | if (!attr) |
| 1247 | return -EMSGSIZE; |
| 1248 | |
| 1249 | if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_FAMILY, addr->family)) |
| 1250 | goto nla_put_failure; |
Geliang Tang | a77e917 | 2021-02-01 15:09:17 -0800 | [diff] [blame] | 1251 | if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_PORT, ntohs(addr->port))) |
| 1252 | goto nla_put_failure; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1253 | if (nla_put_u8(skb, MPTCP_PM_ADDR_ATTR_ID, addr->id)) |
| 1254 | goto nla_put_failure; |
Paolo Abeni | ef0da3b | 2020-09-14 10:01:15 +0200 | [diff] [blame] | 1255 | if (nla_put_u32(skb, MPTCP_PM_ADDR_ATTR_FLAGS, entry->addr.flags)) |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1256 | goto nla_put_failure; |
Paolo Abeni | ef0da3b | 2020-09-14 10:01:15 +0200 | [diff] [blame] | 1257 | if (entry->addr.ifindex && |
| 1258 | nla_put_s32(skb, MPTCP_PM_ADDR_ATTR_IF_IDX, entry->addr.ifindex)) |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1259 | goto nla_put_failure; |
| 1260 | |
Bo YU | b4e0f9a | 2020-04-23 10:10:03 +0800 | [diff] [blame] | 1261 | if (addr->family == AF_INET && |
| 1262 | nla_put_in_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR4, |
| 1263 | addr->addr.s_addr)) |
| 1264 | goto nla_put_failure; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1265 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
Bo YU | b4e0f9a | 2020-04-23 10:10:03 +0800 | [diff] [blame] | 1266 | else if (addr->family == AF_INET6 && |
| 1267 | nla_put_in6_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR6, &addr->addr6)) |
| 1268 | goto nla_put_failure; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1269 | #endif |
| 1270 | nla_nest_end(skb, attr); |
| 1271 | return 0; |
| 1272 | |
| 1273 | nla_put_failure: |
| 1274 | nla_nest_cancel(skb, attr); |
| 1275 | return -EMSGSIZE; |
| 1276 | } |
| 1277 | |
| 1278 | static int mptcp_nl_cmd_get_addr(struct sk_buff *skb, struct genl_info *info) |
| 1279 | { |
| 1280 | struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR]; |
| 1281 | struct pm_nl_pernet *pernet = genl_info_pm_nl(info); |
| 1282 | struct mptcp_pm_addr_entry addr, *entry; |
| 1283 | struct sk_buff *msg; |
| 1284 | void *reply; |
| 1285 | int ret; |
| 1286 | |
| 1287 | ret = mptcp_pm_parse_addr(attr, info, false, &addr); |
| 1288 | if (ret < 0) |
| 1289 | return ret; |
| 1290 | |
| 1291 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1292 | if (!msg) |
| 1293 | return -ENOMEM; |
| 1294 | |
| 1295 | reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0, |
| 1296 | info->genlhdr->cmd); |
| 1297 | if (!reply) { |
| 1298 | GENL_SET_ERR_MSG(info, "not enough space in Netlink message"); |
| 1299 | ret = -EMSGSIZE; |
| 1300 | goto fail; |
| 1301 | } |
| 1302 | |
| 1303 | spin_lock_bh(&pernet->lock); |
| 1304 | entry = __lookup_addr_by_id(pernet, addr.addr.id); |
| 1305 | if (!entry) { |
| 1306 | GENL_SET_ERR_MSG(info, "address not found"); |
| 1307 | ret = -EINVAL; |
| 1308 | goto unlock_fail; |
| 1309 | } |
| 1310 | |
| 1311 | ret = mptcp_nl_fill_addr(msg, entry); |
| 1312 | if (ret) |
| 1313 | goto unlock_fail; |
| 1314 | |
| 1315 | genlmsg_end(msg, reply); |
| 1316 | ret = genlmsg_reply(msg, info); |
| 1317 | spin_unlock_bh(&pernet->lock); |
| 1318 | return ret; |
| 1319 | |
| 1320 | unlock_fail: |
| 1321 | spin_unlock_bh(&pernet->lock); |
| 1322 | |
| 1323 | fail: |
| 1324 | nlmsg_free(msg); |
| 1325 | return ret; |
| 1326 | } |
| 1327 | |
| 1328 | static int mptcp_nl_cmd_dump_addrs(struct sk_buff *msg, |
| 1329 | struct netlink_callback *cb) |
| 1330 | { |
| 1331 | struct net *net = sock_net(msg->sk); |
| 1332 | struct mptcp_pm_addr_entry *entry; |
| 1333 | struct pm_nl_pernet *pernet; |
| 1334 | int id = cb->args[0]; |
| 1335 | void *hdr; |
Geliang Tang | efd5a4c | 2021-01-08 16:47:55 -0800 | [diff] [blame] | 1336 | int i; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1337 | |
| 1338 | pernet = net_generic(net, pm_nl_pernet_id); |
| 1339 | |
| 1340 | spin_lock_bh(&pernet->lock); |
Geliang Tang | efd5a4c | 2021-01-08 16:47:55 -0800 | [diff] [blame] | 1341 | for (i = id; i < MAX_ADDR_ID + 1; i++) { |
| 1342 | if (test_bit(i, pernet->id_bitmap)) { |
| 1343 | entry = __lookup_addr_by_id(pernet, i); |
| 1344 | if (!entry) |
| 1345 | break; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1346 | |
Geliang Tang | efd5a4c | 2021-01-08 16:47:55 -0800 | [diff] [blame] | 1347 | if (entry->addr.id <= id) |
| 1348 | continue; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1349 | |
Geliang Tang | efd5a4c | 2021-01-08 16:47:55 -0800 | [diff] [blame] | 1350 | hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, |
| 1351 | cb->nlh->nlmsg_seq, &mptcp_genl_family, |
| 1352 | NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR); |
| 1353 | if (!hdr) |
| 1354 | break; |
| 1355 | |
| 1356 | if (mptcp_nl_fill_addr(msg, entry) < 0) { |
| 1357 | genlmsg_cancel(msg, hdr); |
| 1358 | break; |
| 1359 | } |
| 1360 | |
| 1361 | id = entry->addr.id; |
| 1362 | genlmsg_end(msg, hdr); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1363 | } |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1364 | } |
| 1365 | spin_unlock_bh(&pernet->lock); |
| 1366 | |
| 1367 | cb->args[0] = id; |
| 1368 | return msg->len; |
| 1369 | } |
| 1370 | |
| 1371 | static int parse_limit(struct genl_info *info, int id, unsigned int *limit) |
| 1372 | { |
| 1373 | struct nlattr *attr = info->attrs[id]; |
| 1374 | |
| 1375 | if (!attr) |
| 1376 | return 0; |
| 1377 | |
| 1378 | *limit = nla_get_u32(attr); |
| 1379 | if (*limit > MPTCP_PM_ADDR_MAX) { |
| 1380 | GENL_SET_ERR_MSG(info, "limit greater than maximum"); |
| 1381 | return -EINVAL; |
| 1382 | } |
| 1383 | return 0; |
| 1384 | } |
| 1385 | |
| 1386 | static int |
| 1387 | mptcp_nl_cmd_set_limits(struct sk_buff *skb, struct genl_info *info) |
| 1388 | { |
| 1389 | struct pm_nl_pernet *pernet = genl_info_pm_nl(info); |
| 1390 | unsigned int rcv_addrs, subflows; |
| 1391 | int ret; |
| 1392 | |
| 1393 | spin_lock_bh(&pernet->lock); |
| 1394 | rcv_addrs = pernet->add_addr_accept_max; |
| 1395 | ret = parse_limit(info, MPTCP_PM_ATTR_RCV_ADD_ADDRS, &rcv_addrs); |
| 1396 | if (ret) |
| 1397 | goto unlock; |
| 1398 | |
| 1399 | subflows = pernet->subflows_max; |
| 1400 | ret = parse_limit(info, MPTCP_PM_ATTR_SUBFLOWS, &subflows); |
| 1401 | if (ret) |
| 1402 | goto unlock; |
| 1403 | |
| 1404 | WRITE_ONCE(pernet->add_addr_accept_max, rcv_addrs); |
| 1405 | WRITE_ONCE(pernet->subflows_max, subflows); |
| 1406 | |
| 1407 | unlock: |
| 1408 | spin_unlock_bh(&pernet->lock); |
| 1409 | return ret; |
| 1410 | } |
| 1411 | |
| 1412 | static int |
| 1413 | mptcp_nl_cmd_get_limits(struct sk_buff *skb, struct genl_info *info) |
| 1414 | { |
| 1415 | struct pm_nl_pernet *pernet = genl_info_pm_nl(info); |
| 1416 | struct sk_buff *msg; |
| 1417 | void *reply; |
| 1418 | |
| 1419 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1420 | if (!msg) |
| 1421 | return -ENOMEM; |
| 1422 | |
| 1423 | reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0, |
| 1424 | MPTCP_PM_CMD_GET_LIMITS); |
| 1425 | if (!reply) |
| 1426 | goto fail; |
| 1427 | |
| 1428 | if (nla_put_u32(msg, MPTCP_PM_ATTR_RCV_ADD_ADDRS, |
| 1429 | READ_ONCE(pernet->add_addr_accept_max))) |
| 1430 | goto fail; |
| 1431 | |
| 1432 | if (nla_put_u32(msg, MPTCP_PM_ATTR_SUBFLOWS, |
| 1433 | READ_ONCE(pernet->subflows_max))) |
| 1434 | goto fail; |
| 1435 | |
| 1436 | genlmsg_end(msg, reply); |
| 1437 | return genlmsg_reply(msg, info); |
| 1438 | |
| 1439 | fail: |
| 1440 | GENL_SET_ERR_MSG(info, "not enough space in Netlink message"); |
| 1441 | nlmsg_free(msg); |
| 1442 | return -EMSGSIZE; |
| 1443 | } |
| 1444 | |
Geliang Tang | 0f9f696 | 2021-01-08 16:47:59 -0800 | [diff] [blame] | 1445 | static int mptcp_nl_addr_backup(struct net *net, |
| 1446 | struct mptcp_addr_info *addr, |
| 1447 | u8 bkup) |
| 1448 | { |
| 1449 | long s_slot = 0, s_num = 0; |
| 1450 | struct mptcp_sock *msk; |
| 1451 | int ret = -EINVAL; |
| 1452 | |
| 1453 | while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { |
| 1454 | struct sock *sk = (struct sock *)msk; |
| 1455 | |
| 1456 | if (list_empty(&msk->conn_list)) |
| 1457 | goto next; |
| 1458 | |
| 1459 | lock_sock(sk); |
| 1460 | spin_lock_bh(&msk->pm.lock); |
| 1461 | ret = mptcp_pm_nl_mp_prio_send_ack(msk, addr, bkup); |
| 1462 | spin_unlock_bh(&msk->pm.lock); |
| 1463 | release_sock(sk); |
| 1464 | |
| 1465 | next: |
| 1466 | sock_put(sk); |
| 1467 | cond_resched(); |
| 1468 | } |
| 1469 | |
| 1470 | return ret; |
| 1471 | } |
| 1472 | |
| 1473 | static int mptcp_nl_cmd_set_flags(struct sk_buff *skb, struct genl_info *info) |
| 1474 | { |
| 1475 | struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR]; |
| 1476 | struct pm_nl_pernet *pernet = genl_info_pm_nl(info); |
| 1477 | struct mptcp_pm_addr_entry addr, *entry; |
| 1478 | struct net *net = sock_net(skb->sk); |
| 1479 | u8 bkup = 0; |
| 1480 | int ret; |
| 1481 | |
| 1482 | ret = mptcp_pm_parse_addr(attr, info, true, &addr); |
| 1483 | if (ret < 0) |
| 1484 | return ret; |
| 1485 | |
| 1486 | if (addr.addr.flags & MPTCP_PM_ADDR_FLAG_BACKUP) |
| 1487 | bkup = 1; |
| 1488 | |
| 1489 | list_for_each_entry(entry, &pernet->local_addr_list, list) { |
| 1490 | if (addresses_equal(&entry->addr, &addr.addr, true)) { |
| 1491 | ret = mptcp_nl_addr_backup(net, &entry->addr, bkup); |
| 1492 | if (ret) |
| 1493 | return ret; |
| 1494 | |
| 1495 | if (bkup) |
| 1496 | entry->addr.flags |= MPTCP_PM_ADDR_FLAG_BACKUP; |
| 1497 | else |
| 1498 | entry->addr.flags &= ~MPTCP_PM_ADDR_FLAG_BACKUP; |
| 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | return 0; |
| 1503 | } |
| 1504 | |
Florian Westphal | b911c97 | 2021-02-12 16:00:01 -0800 | [diff] [blame] | 1505 | static void mptcp_nl_mcast_send(struct net *net, struct sk_buff *nlskb, gfp_t gfp) |
| 1506 | { |
| 1507 | genlmsg_multicast_netns(&mptcp_genl_family, net, |
| 1508 | nlskb, 0, MPTCP_PM_EV_GRP_OFFSET, gfp); |
| 1509 | } |
| 1510 | |
| 1511 | static int mptcp_event_add_subflow(struct sk_buff *skb, const struct sock *ssk) |
| 1512 | { |
| 1513 | const struct inet_sock *issk = inet_sk(ssk); |
| 1514 | const struct mptcp_subflow_context *sf; |
| 1515 | |
| 1516 | if (nla_put_u16(skb, MPTCP_ATTR_FAMILY, ssk->sk_family)) |
| 1517 | return -EMSGSIZE; |
| 1518 | |
| 1519 | switch (ssk->sk_family) { |
| 1520 | case AF_INET: |
| 1521 | if (nla_put_in_addr(skb, MPTCP_ATTR_SADDR4, issk->inet_saddr)) |
| 1522 | return -EMSGSIZE; |
| 1523 | if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, issk->inet_daddr)) |
| 1524 | return -EMSGSIZE; |
| 1525 | break; |
| 1526 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 1527 | case AF_INET6: { |
| 1528 | const struct ipv6_pinfo *np = inet6_sk(ssk); |
| 1529 | |
| 1530 | if (nla_put_in6_addr(skb, MPTCP_ATTR_SADDR6, &np->saddr)) |
| 1531 | return -EMSGSIZE; |
| 1532 | if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &ssk->sk_v6_daddr)) |
| 1533 | return -EMSGSIZE; |
| 1534 | break; |
| 1535 | } |
| 1536 | #endif |
| 1537 | default: |
| 1538 | WARN_ON_ONCE(1); |
| 1539 | return -EMSGSIZE; |
| 1540 | } |
| 1541 | |
| 1542 | if (nla_put_be16(skb, MPTCP_ATTR_SPORT, issk->inet_sport)) |
| 1543 | return -EMSGSIZE; |
| 1544 | if (nla_put_be16(skb, MPTCP_ATTR_DPORT, issk->inet_dport)) |
| 1545 | return -EMSGSIZE; |
| 1546 | |
| 1547 | sf = mptcp_subflow_ctx(ssk); |
| 1548 | if (WARN_ON_ONCE(!sf)) |
| 1549 | return -EINVAL; |
| 1550 | |
| 1551 | if (nla_put_u8(skb, MPTCP_ATTR_LOC_ID, sf->local_id)) |
| 1552 | return -EMSGSIZE; |
| 1553 | |
| 1554 | if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, sf->remote_id)) |
| 1555 | return -EMSGSIZE; |
| 1556 | |
| 1557 | return 0; |
| 1558 | } |
| 1559 | |
| 1560 | static int mptcp_event_put_token_and_ssk(struct sk_buff *skb, |
| 1561 | const struct mptcp_sock *msk, |
| 1562 | const struct sock *ssk) |
| 1563 | { |
| 1564 | const struct sock *sk = (const struct sock *)msk; |
| 1565 | const struct mptcp_subflow_context *sf; |
| 1566 | u8 sk_err; |
| 1567 | |
| 1568 | if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token)) |
| 1569 | return -EMSGSIZE; |
| 1570 | |
| 1571 | if (mptcp_event_add_subflow(skb, ssk)) |
| 1572 | return -EMSGSIZE; |
| 1573 | |
| 1574 | sf = mptcp_subflow_ctx(ssk); |
| 1575 | if (WARN_ON_ONCE(!sf)) |
| 1576 | return -EINVAL; |
| 1577 | |
| 1578 | if (nla_put_u8(skb, MPTCP_ATTR_BACKUP, sf->backup)) |
| 1579 | return -EMSGSIZE; |
| 1580 | |
| 1581 | if (ssk->sk_bound_dev_if && |
| 1582 | nla_put_s32(skb, MPTCP_ATTR_IF_IDX, ssk->sk_bound_dev_if)) |
| 1583 | return -EMSGSIZE; |
| 1584 | |
| 1585 | sk_err = ssk->sk_err; |
| 1586 | if (sk_err && sk->sk_state == TCP_ESTABLISHED && |
| 1587 | nla_put_u8(skb, MPTCP_ATTR_ERROR, sk_err)) |
| 1588 | return -EMSGSIZE; |
| 1589 | |
| 1590 | return 0; |
| 1591 | } |
| 1592 | |
| 1593 | static int mptcp_event_sub_established(struct sk_buff *skb, |
| 1594 | const struct mptcp_sock *msk, |
| 1595 | const struct sock *ssk) |
| 1596 | { |
| 1597 | return mptcp_event_put_token_and_ssk(skb, msk, ssk); |
| 1598 | } |
| 1599 | |
| 1600 | static int mptcp_event_sub_closed(struct sk_buff *skb, |
| 1601 | const struct mptcp_sock *msk, |
| 1602 | const struct sock *ssk) |
| 1603 | { |
| 1604 | if (mptcp_event_put_token_and_ssk(skb, msk, ssk)) |
| 1605 | return -EMSGSIZE; |
| 1606 | |
| 1607 | return 0; |
| 1608 | } |
| 1609 | |
| 1610 | static int mptcp_event_created(struct sk_buff *skb, |
| 1611 | const struct mptcp_sock *msk, |
| 1612 | const struct sock *ssk) |
| 1613 | { |
| 1614 | int err = nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token); |
| 1615 | |
| 1616 | if (err) |
| 1617 | return err; |
| 1618 | |
| 1619 | return mptcp_event_add_subflow(skb, ssk); |
| 1620 | } |
| 1621 | |
| 1622 | void mptcp_event_addr_removed(const struct mptcp_sock *msk, uint8_t id) |
| 1623 | { |
| 1624 | struct net *net = sock_net((const struct sock *)msk); |
| 1625 | struct nlmsghdr *nlh; |
| 1626 | struct sk_buff *skb; |
| 1627 | |
| 1628 | if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET)) |
| 1629 | return; |
| 1630 | |
| 1631 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
| 1632 | if (!skb) |
| 1633 | return; |
| 1634 | |
| 1635 | nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, MPTCP_EVENT_REMOVED); |
| 1636 | if (!nlh) |
| 1637 | goto nla_put_failure; |
| 1638 | |
| 1639 | if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token)) |
| 1640 | goto nla_put_failure; |
| 1641 | |
| 1642 | if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, id)) |
| 1643 | goto nla_put_failure; |
| 1644 | |
| 1645 | genlmsg_end(skb, nlh); |
| 1646 | mptcp_nl_mcast_send(net, skb, GFP_ATOMIC); |
| 1647 | return; |
| 1648 | |
| 1649 | nla_put_failure: |
| 1650 | kfree_skb(skb); |
| 1651 | } |
| 1652 | |
| 1653 | void mptcp_event_addr_announced(const struct mptcp_sock *msk, |
| 1654 | const struct mptcp_addr_info *info) |
| 1655 | { |
| 1656 | struct net *net = sock_net((const struct sock *)msk); |
| 1657 | struct nlmsghdr *nlh; |
| 1658 | struct sk_buff *skb; |
| 1659 | |
| 1660 | if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET)) |
| 1661 | return; |
| 1662 | |
| 1663 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
| 1664 | if (!skb) |
| 1665 | return; |
| 1666 | |
| 1667 | nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, |
| 1668 | MPTCP_EVENT_ANNOUNCED); |
| 1669 | if (!nlh) |
| 1670 | goto nla_put_failure; |
| 1671 | |
| 1672 | if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token)) |
| 1673 | goto nla_put_failure; |
| 1674 | |
| 1675 | if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, info->id)) |
| 1676 | goto nla_put_failure; |
| 1677 | |
| 1678 | if (nla_put_be16(skb, MPTCP_ATTR_DPORT, info->port)) |
| 1679 | goto nla_put_failure; |
| 1680 | |
| 1681 | switch (info->family) { |
| 1682 | case AF_INET: |
| 1683 | if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, info->addr.s_addr)) |
| 1684 | goto nla_put_failure; |
| 1685 | break; |
| 1686 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 1687 | case AF_INET6: |
| 1688 | if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &info->addr6)) |
| 1689 | goto nla_put_failure; |
| 1690 | break; |
| 1691 | #endif |
| 1692 | default: |
| 1693 | WARN_ON_ONCE(1); |
| 1694 | goto nla_put_failure; |
| 1695 | } |
| 1696 | |
| 1697 | genlmsg_end(skb, nlh); |
| 1698 | mptcp_nl_mcast_send(net, skb, GFP_ATOMIC); |
| 1699 | return; |
| 1700 | |
| 1701 | nla_put_failure: |
| 1702 | kfree_skb(skb); |
| 1703 | } |
| 1704 | |
| 1705 | void mptcp_event(enum mptcp_event_type type, const struct mptcp_sock *msk, |
| 1706 | const struct sock *ssk, gfp_t gfp) |
| 1707 | { |
| 1708 | struct net *net = sock_net((const struct sock *)msk); |
| 1709 | struct nlmsghdr *nlh; |
| 1710 | struct sk_buff *skb; |
| 1711 | |
| 1712 | if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET)) |
| 1713 | return; |
| 1714 | |
| 1715 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp); |
| 1716 | if (!skb) |
| 1717 | return; |
| 1718 | |
| 1719 | nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, type); |
| 1720 | if (!nlh) |
| 1721 | goto nla_put_failure; |
| 1722 | |
| 1723 | switch (type) { |
| 1724 | case MPTCP_EVENT_UNSPEC: |
| 1725 | WARN_ON_ONCE(1); |
| 1726 | break; |
| 1727 | case MPTCP_EVENT_CREATED: |
| 1728 | case MPTCP_EVENT_ESTABLISHED: |
| 1729 | if (mptcp_event_created(skb, msk, ssk) < 0) |
| 1730 | goto nla_put_failure; |
| 1731 | break; |
| 1732 | case MPTCP_EVENT_CLOSED: |
| 1733 | if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token) < 0) |
| 1734 | goto nla_put_failure; |
| 1735 | break; |
| 1736 | case MPTCP_EVENT_ANNOUNCED: |
| 1737 | case MPTCP_EVENT_REMOVED: |
| 1738 | /* call mptcp_event_addr_announced()/removed instead */ |
| 1739 | WARN_ON_ONCE(1); |
| 1740 | break; |
| 1741 | case MPTCP_EVENT_SUB_ESTABLISHED: |
| 1742 | case MPTCP_EVENT_SUB_PRIORITY: |
| 1743 | if (mptcp_event_sub_established(skb, msk, ssk) < 0) |
| 1744 | goto nla_put_failure; |
| 1745 | break; |
| 1746 | case MPTCP_EVENT_SUB_CLOSED: |
| 1747 | if (mptcp_event_sub_closed(skb, msk, ssk) < 0) |
| 1748 | goto nla_put_failure; |
| 1749 | break; |
| 1750 | } |
| 1751 | |
| 1752 | genlmsg_end(skb, nlh); |
| 1753 | mptcp_nl_mcast_send(net, skb, gfp); |
| 1754 | return; |
| 1755 | |
| 1756 | nla_put_failure: |
| 1757 | kfree_skb(skb); |
| 1758 | } |
| 1759 | |
Rikard Falkeborn | 674d3ab | 2020-10-05 01:44:16 +0200 | [diff] [blame] | 1760 | static const struct genl_small_ops mptcp_pm_ops[] = { |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1761 | { |
| 1762 | .cmd = MPTCP_PM_CMD_ADD_ADDR, |
| 1763 | .doit = mptcp_nl_cmd_add_addr, |
| 1764 | .flags = GENL_ADMIN_PERM, |
| 1765 | }, |
| 1766 | { |
| 1767 | .cmd = MPTCP_PM_CMD_DEL_ADDR, |
| 1768 | .doit = mptcp_nl_cmd_del_addr, |
| 1769 | .flags = GENL_ADMIN_PERM, |
| 1770 | }, |
| 1771 | { |
| 1772 | .cmd = MPTCP_PM_CMD_FLUSH_ADDRS, |
| 1773 | .doit = mptcp_nl_cmd_flush_addrs, |
| 1774 | .flags = GENL_ADMIN_PERM, |
| 1775 | }, |
| 1776 | { |
| 1777 | .cmd = MPTCP_PM_CMD_GET_ADDR, |
| 1778 | .doit = mptcp_nl_cmd_get_addr, |
| 1779 | .dumpit = mptcp_nl_cmd_dump_addrs, |
| 1780 | }, |
| 1781 | { |
| 1782 | .cmd = MPTCP_PM_CMD_SET_LIMITS, |
| 1783 | .doit = mptcp_nl_cmd_set_limits, |
| 1784 | .flags = GENL_ADMIN_PERM, |
| 1785 | }, |
| 1786 | { |
| 1787 | .cmd = MPTCP_PM_CMD_GET_LIMITS, |
| 1788 | .doit = mptcp_nl_cmd_get_limits, |
| 1789 | }, |
Geliang Tang | 0f9f696 | 2021-01-08 16:47:59 -0800 | [diff] [blame] | 1790 | { |
| 1791 | .cmd = MPTCP_PM_CMD_SET_FLAGS, |
| 1792 | .doit = mptcp_nl_cmd_set_flags, |
| 1793 | .flags = GENL_ADMIN_PERM, |
| 1794 | }, |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1795 | }; |
| 1796 | |
| 1797 | static struct genl_family mptcp_genl_family __ro_after_init = { |
| 1798 | .name = MPTCP_PM_NAME, |
| 1799 | .version = MPTCP_PM_VER, |
| 1800 | .maxattr = MPTCP_PM_ATTR_MAX, |
| 1801 | .policy = mptcp_pm_policy, |
| 1802 | .netnsok = true, |
| 1803 | .module = THIS_MODULE, |
Jakub Kicinski | 66a9b92 | 2020-10-02 14:49:54 -0700 | [diff] [blame] | 1804 | .small_ops = mptcp_pm_ops, |
| 1805 | .n_small_ops = ARRAY_SIZE(mptcp_pm_ops), |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1806 | .mcgrps = mptcp_pm_mcgrps, |
| 1807 | .n_mcgrps = ARRAY_SIZE(mptcp_pm_mcgrps), |
| 1808 | }; |
| 1809 | |
| 1810 | static int __net_init pm_nl_init_net(struct net *net) |
| 1811 | { |
| 1812 | struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id); |
| 1813 | |
| 1814 | INIT_LIST_HEAD_RCU(&pernet->local_addr_list); |
| 1815 | __reset_counters(pernet); |
| 1816 | pernet->next_id = 1; |
Geliang Tang | efd5a4c | 2021-01-08 16:47:55 -0800 | [diff] [blame] | 1817 | bitmap_zero(pernet->id_bitmap, MAX_ADDR_ID + 1); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1818 | spin_lock_init(&pernet->lock); |
| 1819 | return 0; |
| 1820 | } |
| 1821 | |
| 1822 | static void __net_exit pm_nl_exit_net(struct list_head *net_list) |
| 1823 | { |
| 1824 | struct net *net; |
| 1825 | |
| 1826 | list_for_each_entry(net, net_list, exit_list) { |
Geliang Tang | 141694d | 2020-12-10 14:24:59 -0800 | [diff] [blame] | 1827 | struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id); |
| 1828 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1829 | /* net is removed from namespace list, can't race with |
| 1830 | * other modifiers |
| 1831 | */ |
Geliang Tang | 141694d | 2020-12-10 14:24:59 -0800 | [diff] [blame] | 1832 | __flush_addrs(net, &pernet->local_addr_list); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1833 | } |
| 1834 | } |
| 1835 | |
| 1836 | static struct pernet_operations mptcp_pm_pernet_ops = { |
| 1837 | .init = pm_nl_init_net, |
| 1838 | .exit_batch = pm_nl_exit_net, |
| 1839 | .id = &pm_nl_pernet_id, |
| 1840 | .size = sizeof(struct pm_nl_pernet), |
| 1841 | }; |
| 1842 | |
Paolo Abeni | d39dcec | 2020-06-26 19:29:59 +0200 | [diff] [blame] | 1843 | void __init mptcp_pm_nl_init(void) |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1844 | { |
| 1845 | if (register_pernet_subsys(&mptcp_pm_pernet_ops) < 0) |
| 1846 | panic("Failed to register MPTCP PM pernet subsystem.\n"); |
| 1847 | |
| 1848 | if (genl_register_family(&mptcp_genl_family)) |
| 1849 | panic("Failed to register MPTCP PM netlink family\n"); |
| 1850 | } |