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; |
| 29 | }; |
| 30 | |
Geliang Tang | 0abd40f | 2020-09-24 08:30:00 +0800 | [diff] [blame] | 31 | struct mptcp_pm_add_entry { |
| 32 | struct list_head list; |
| 33 | struct mptcp_addr_info addr; |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 34 | struct timer_list add_timer; |
| 35 | struct mptcp_sock *sock; |
| 36 | u8 retrans_times; |
Geliang Tang | 0abd40f | 2020-09-24 08:30:00 +0800 | [diff] [blame] | 37 | }; |
| 38 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 39 | struct pm_nl_pernet { |
| 40 | /* protects pernet updates */ |
| 41 | spinlock_t lock; |
| 42 | struct list_head local_addr_list; |
| 43 | unsigned int addrs; |
| 44 | unsigned int add_addr_signal_max; |
| 45 | unsigned int add_addr_accept_max; |
| 46 | unsigned int local_addr_max; |
| 47 | unsigned int subflows_max; |
| 48 | unsigned int next_id; |
| 49 | }; |
| 50 | |
| 51 | #define MPTCP_PM_ADDR_MAX 8 |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 52 | #define ADD_ADDR_RETRANS_MAX 3 |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 53 | |
| 54 | static bool addresses_equal(const struct mptcp_addr_info *a, |
| 55 | struct mptcp_addr_info *b, bool use_port) |
| 56 | { |
| 57 | bool addr_equals = false; |
| 58 | |
| 59 | if (a->family != b->family) |
| 60 | return false; |
| 61 | |
| 62 | if (a->family == AF_INET) |
| 63 | addr_equals = a->addr.s_addr == b->addr.s_addr; |
| 64 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 65 | else |
| 66 | addr_equals = !ipv6_addr_cmp(&a->addr6, &b->addr6); |
| 67 | #endif |
| 68 | |
| 69 | if (!addr_equals) |
| 70 | return false; |
| 71 | if (!use_port) |
| 72 | return true; |
| 73 | |
| 74 | return a->port == b->port; |
| 75 | } |
| 76 | |
Geliang Tang | 5702581 | 2020-09-08 10:49:38 +0800 | [diff] [blame] | 77 | static bool address_zero(const struct mptcp_addr_info *addr) |
| 78 | { |
| 79 | struct mptcp_addr_info zero; |
| 80 | |
| 81 | memset(&zero, 0, sizeof(zero)); |
| 82 | zero.family = addr->family; |
| 83 | |
| 84 | return addresses_equal(addr, &zero, false); |
| 85 | } |
| 86 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 87 | static void local_address(const struct sock_common *skc, |
| 88 | struct mptcp_addr_info *addr) |
| 89 | { |
| 90 | addr->port = 0; |
| 91 | addr->family = skc->skc_family; |
| 92 | if (addr->family == AF_INET) |
| 93 | addr->addr.s_addr = skc->skc_rcv_saddr; |
| 94 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 95 | else if (addr->family == AF_INET6) |
| 96 | addr->addr6 = skc->skc_v6_rcv_saddr; |
| 97 | #endif |
| 98 | } |
| 99 | |
| 100 | static void remote_address(const struct sock_common *skc, |
| 101 | struct mptcp_addr_info *addr) |
| 102 | { |
| 103 | addr->family = skc->skc_family; |
| 104 | addr->port = skc->skc_dport; |
| 105 | if (addr->family == AF_INET) |
| 106 | addr->addr.s_addr = skc->skc_daddr; |
| 107 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 108 | else if (addr->family == AF_INET6) |
| 109 | addr->addr6 = skc->skc_v6_daddr; |
| 110 | #endif |
| 111 | } |
| 112 | |
| 113 | static bool lookup_subflow_by_saddr(const struct list_head *list, |
| 114 | struct mptcp_addr_info *saddr) |
| 115 | { |
| 116 | struct mptcp_subflow_context *subflow; |
| 117 | struct mptcp_addr_info cur; |
| 118 | struct sock_common *skc; |
| 119 | |
| 120 | list_for_each_entry(subflow, list, node) { |
| 121 | skc = (struct sock_common *)mptcp_subflow_tcp_sock(subflow); |
| 122 | |
| 123 | local_address(skc, &cur); |
| 124 | if (addresses_equal(&cur, saddr, false)) |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | static struct mptcp_pm_addr_entry * |
| 132 | select_local_address(const struct pm_nl_pernet *pernet, |
| 133 | struct mptcp_sock *msk) |
| 134 | { |
| 135 | struct mptcp_pm_addr_entry *entry, *ret = NULL; |
| 136 | |
| 137 | rcu_read_lock(); |
| 138 | spin_lock_bh(&msk->join_list_lock); |
| 139 | list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) { |
Paolo Abeni | ef0da3b | 2020-09-14 10:01:15 +0200 | [diff] [blame] | 140 | if (!(entry->addr.flags & MPTCP_PM_ADDR_FLAG_SUBFLOW)) |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 141 | continue; |
| 142 | |
| 143 | /* avoid any address already in use by subflows and |
| 144 | * pending join |
| 145 | */ |
| 146 | if (entry->addr.family == ((struct sock *)msk)->sk_family && |
| 147 | !lookup_subflow_by_saddr(&msk->conn_list, &entry->addr) && |
| 148 | !lookup_subflow_by_saddr(&msk->join_list, &entry->addr)) { |
| 149 | ret = entry; |
| 150 | break; |
| 151 | } |
| 152 | } |
| 153 | spin_unlock_bh(&msk->join_list_lock); |
| 154 | rcu_read_unlock(); |
| 155 | return ret; |
| 156 | } |
| 157 | |
| 158 | static struct mptcp_pm_addr_entry * |
| 159 | select_signal_address(struct pm_nl_pernet *pernet, unsigned int pos) |
| 160 | { |
| 161 | struct mptcp_pm_addr_entry *entry, *ret = NULL; |
| 162 | int i = 0; |
| 163 | |
| 164 | rcu_read_lock(); |
| 165 | /* do not keep any additional per socket state, just signal |
| 166 | * the address list in order. |
| 167 | * Note: removal from the local address list during the msk life-cycle |
| 168 | * can lead to additional addresses not being announced. |
| 169 | */ |
| 170 | list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) { |
Paolo Abeni | ef0da3b | 2020-09-14 10:01:15 +0200 | [diff] [blame] | 171 | if (!(entry->addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 172 | continue; |
| 173 | if (i++ == pos) { |
| 174 | ret = entry; |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | rcu_read_unlock(); |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | static void check_work_pending(struct mptcp_sock *msk) |
| 183 | { |
| 184 | if (msk->pm.add_addr_signaled == msk->pm.add_addr_signal_max && |
| 185 | (msk->pm.local_addr_used == msk->pm.local_addr_max || |
| 186 | msk->pm.subflows == msk->pm.subflows_max)) |
| 187 | WRITE_ONCE(msk->pm.work_pending, false); |
| 188 | } |
| 189 | |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 190 | static struct mptcp_pm_add_entry * |
| 191 | lookup_anno_list_by_saddr(struct mptcp_sock *msk, |
| 192 | struct mptcp_addr_info *addr) |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 193 | { |
Geliang Tang | 0abd40f | 2020-09-24 08:30:00 +0800 | [diff] [blame] | 194 | struct mptcp_pm_add_entry *entry; |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 195 | |
| 196 | list_for_each_entry(entry, &msk->pm.anno_list, list) { |
| 197 | if (addresses_equal(&entry->addr, addr, false)) |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 198 | return entry; |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 199 | } |
| 200 | |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 201 | return NULL; |
| 202 | } |
| 203 | |
| 204 | static void mptcp_pm_add_timer(struct timer_list *timer) |
| 205 | { |
| 206 | struct mptcp_pm_add_entry *entry = from_timer(entry, timer, add_timer); |
| 207 | struct mptcp_sock *msk = entry->sock; |
| 208 | struct sock *sk = (struct sock *)msk; |
| 209 | |
| 210 | pr_debug("msk=%p", msk); |
| 211 | |
| 212 | if (!msk) |
| 213 | return; |
| 214 | |
| 215 | if (inet_sk_state_load(sk) == TCP_CLOSE) |
| 216 | return; |
| 217 | |
| 218 | if (!entry->addr.id) |
| 219 | return; |
| 220 | |
| 221 | if (mptcp_pm_should_add_signal(msk)) { |
| 222 | sk_reset_timer(sk, timer, jiffies + TCP_RTO_MAX / 8); |
| 223 | goto out; |
| 224 | } |
| 225 | |
| 226 | spin_lock_bh(&msk->pm.lock); |
| 227 | |
| 228 | if (!mptcp_pm_should_add_signal(msk)) { |
| 229 | pr_debug("retransmit ADD_ADDR id=%d", entry->addr.id); |
| 230 | mptcp_pm_announce_addr(msk, &entry->addr, false); |
| 231 | entry->retrans_times++; |
| 232 | } |
| 233 | |
| 234 | if (entry->retrans_times < ADD_ADDR_RETRANS_MAX) |
| 235 | sk_reset_timer(sk, timer, jiffies + TCP_RTO_MAX); |
| 236 | |
| 237 | spin_unlock_bh(&msk->pm.lock); |
| 238 | |
| 239 | out: |
| 240 | __sock_put(sk); |
| 241 | } |
| 242 | |
| 243 | struct mptcp_pm_add_entry * |
| 244 | mptcp_pm_del_add_timer(struct mptcp_sock *msk, |
| 245 | struct mptcp_addr_info *addr) |
| 246 | { |
| 247 | struct mptcp_pm_add_entry *entry; |
| 248 | struct sock *sk = (struct sock *)msk; |
| 249 | |
| 250 | spin_lock_bh(&msk->pm.lock); |
| 251 | entry = lookup_anno_list_by_saddr(msk, addr); |
| 252 | if (entry) |
| 253 | entry->retrans_times = ADD_ADDR_RETRANS_MAX; |
| 254 | spin_unlock_bh(&msk->pm.lock); |
| 255 | |
| 256 | if (entry) |
| 257 | sk_stop_timer_sync(sk, &entry->add_timer); |
| 258 | |
| 259 | return entry; |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | static bool mptcp_pm_alloc_anno_list(struct mptcp_sock *msk, |
| 263 | struct mptcp_pm_addr_entry *entry) |
| 264 | { |
Geliang Tang | 0abd40f | 2020-09-24 08:30:00 +0800 | [diff] [blame] | 265 | struct mptcp_pm_add_entry *add_entry = NULL; |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 266 | struct sock *sk = (struct sock *)msk; |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 267 | |
| 268 | if (lookup_anno_list_by_saddr(msk, &entry->addr)) |
| 269 | return false; |
| 270 | |
Geliang Tang | 0abd40f | 2020-09-24 08:30:00 +0800 | [diff] [blame] | 271 | add_entry = kmalloc(sizeof(*add_entry), GFP_ATOMIC); |
| 272 | if (!add_entry) |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 273 | return false; |
| 274 | |
Geliang Tang | 0abd40f | 2020-09-24 08:30:00 +0800 | [diff] [blame] | 275 | list_add(&add_entry->list, &msk->pm.anno_list); |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 276 | |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 277 | add_entry->addr = entry->addr; |
| 278 | add_entry->sock = msk; |
| 279 | add_entry->retrans_times = 0; |
| 280 | |
| 281 | timer_setup(&add_entry->add_timer, mptcp_pm_add_timer, 0); |
| 282 | sk_reset_timer(sk, &add_entry->add_timer, jiffies + TCP_RTO_MAX); |
| 283 | |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 284 | return true; |
| 285 | } |
| 286 | |
| 287 | void mptcp_pm_free_anno_list(struct mptcp_sock *msk) |
| 288 | { |
Geliang Tang | 0abd40f | 2020-09-24 08:30:00 +0800 | [diff] [blame] | 289 | struct mptcp_pm_add_entry *entry, *tmp; |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 290 | struct sock *sk = (struct sock *)msk; |
| 291 | LIST_HEAD(free_list); |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 292 | |
| 293 | pr_debug("msk=%p", msk); |
| 294 | |
| 295 | spin_lock_bh(&msk->pm.lock); |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 296 | list_splice_init(&msk->pm.anno_list, &free_list); |
| 297 | spin_unlock_bh(&msk->pm.lock); |
| 298 | |
| 299 | list_for_each_entry_safe(entry, tmp, &free_list, list) { |
| 300 | sk_stop_timer_sync(sk, &entry->add_timer); |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 301 | kfree(entry); |
| 302 | } |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 303 | } |
| 304 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 305 | static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk) |
| 306 | { |
Geliang Tang | 2ff0e56 | 2020-09-08 10:49:39 +0800 | [diff] [blame] | 307 | struct mptcp_addr_info remote = { 0 }; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 308 | struct sock *sk = (struct sock *)msk; |
| 309 | struct mptcp_pm_addr_entry *local; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 310 | struct pm_nl_pernet *pernet; |
| 311 | |
| 312 | pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id); |
| 313 | |
| 314 | pr_debug("local %d:%d signal %d:%d subflows %d:%d\n", |
| 315 | msk->pm.local_addr_used, msk->pm.local_addr_max, |
| 316 | msk->pm.add_addr_signaled, msk->pm.add_addr_signal_max, |
| 317 | msk->pm.subflows, msk->pm.subflows_max); |
| 318 | |
| 319 | /* check first for announce */ |
| 320 | if (msk->pm.add_addr_signaled < msk->pm.add_addr_signal_max) { |
| 321 | local = select_signal_address(pernet, |
| 322 | msk->pm.add_addr_signaled); |
| 323 | |
| 324 | if (local) { |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 325 | if (mptcp_pm_alloc_anno_list(msk, local)) { |
| 326 | msk->pm.add_addr_signaled++; |
| 327 | mptcp_pm_announce_addr(msk, &local->addr, false); |
| 328 | } |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 329 | } else { |
| 330 | /* pick failed, avoid fourther attempts later */ |
| 331 | msk->pm.local_addr_used = msk->pm.add_addr_signal_max; |
| 332 | } |
| 333 | |
| 334 | check_work_pending(msk); |
| 335 | } |
| 336 | |
| 337 | /* check if should create a new subflow */ |
| 338 | if (msk->pm.local_addr_used < msk->pm.local_addr_max && |
| 339 | msk->pm.subflows < msk->pm.subflows_max) { |
| 340 | remote_address((struct sock_common *)sk, &remote); |
| 341 | |
| 342 | local = select_local_address(pernet, msk); |
| 343 | if (local) { |
| 344 | msk->pm.local_addr_used++; |
| 345 | msk->pm.subflows++; |
| 346 | check_work_pending(msk); |
| 347 | spin_unlock_bh(&msk->pm.lock); |
Paolo Abeni | ef0da3b | 2020-09-14 10:01:15 +0200 | [diff] [blame] | 348 | __mptcp_subflow_connect(sk, &local->addr, &remote); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 349 | spin_lock_bh(&msk->pm.lock); |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | /* lookup failed, avoid fourther attempts later */ |
| 354 | msk->pm.local_addr_used = msk->pm.local_addr_max; |
| 355 | check_work_pending(msk); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | void mptcp_pm_nl_fully_established(struct mptcp_sock *msk) |
| 360 | { |
| 361 | mptcp_pm_create_subflow_or_signal_addr(msk); |
| 362 | } |
| 363 | |
| 364 | void mptcp_pm_nl_subflow_established(struct mptcp_sock *msk) |
| 365 | { |
| 366 | mptcp_pm_create_subflow_or_signal_addr(msk); |
| 367 | } |
| 368 | |
| 369 | void mptcp_pm_nl_add_addr_received(struct mptcp_sock *msk) |
| 370 | { |
| 371 | struct sock *sk = (struct sock *)msk; |
| 372 | struct mptcp_addr_info remote; |
| 373 | struct mptcp_addr_info local; |
| 374 | |
| 375 | pr_debug("accepted %d:%d remote family %d", |
| 376 | msk->pm.add_addr_accepted, msk->pm.add_addr_accept_max, |
| 377 | msk->pm.remote.family); |
| 378 | msk->pm.add_addr_accepted++; |
| 379 | msk->pm.subflows++; |
| 380 | if (msk->pm.add_addr_accepted >= msk->pm.add_addr_accept_max || |
| 381 | msk->pm.subflows >= msk->pm.subflows_max) |
| 382 | WRITE_ONCE(msk->pm.accept_addr, false); |
| 383 | |
| 384 | /* connect to the specified remote address, using whatever |
| 385 | * local address the routing configuration will pick. |
| 386 | */ |
| 387 | remote = msk->pm.remote; |
| 388 | if (!remote.port) |
| 389 | remote.port = sk->sk_dport; |
| 390 | memset(&local, 0, sizeof(local)); |
| 391 | local.family = remote.family; |
| 392 | |
| 393 | spin_unlock_bh(&msk->pm.lock); |
Paolo Abeni | ef0da3b | 2020-09-14 10:01:15 +0200 | [diff] [blame] | 394 | __mptcp_subflow_connect((struct sock *)msk, &local, &remote); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 395 | spin_lock_bh(&msk->pm.lock); |
Geliang Tang | 6a6c05a | 2020-09-24 08:29:50 +0800 | [diff] [blame] | 396 | |
| 397 | mptcp_pm_announce_addr(msk, &remote, true); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Geliang Tang | d0876b2 | 2020-09-24 08:29:49 +0800 | [diff] [blame] | 400 | void mptcp_pm_nl_rm_addr_received(struct mptcp_sock *msk) |
| 401 | { |
| 402 | struct mptcp_subflow_context *subflow, *tmp; |
| 403 | struct sock *sk = (struct sock *)msk; |
| 404 | |
| 405 | pr_debug("address rm_id %d", msk->pm.rm_id); |
| 406 | |
| 407 | if (!msk->pm.rm_id) |
| 408 | return; |
| 409 | |
| 410 | if (list_empty(&msk->conn_list)) |
| 411 | return; |
| 412 | |
| 413 | list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) { |
| 414 | struct sock *ssk = mptcp_subflow_tcp_sock(subflow); |
| 415 | int how = RCV_SHUTDOWN | SEND_SHUTDOWN; |
| 416 | long timeout = 0; |
| 417 | |
| 418 | if (msk->pm.rm_id != subflow->remote_id) |
| 419 | continue; |
| 420 | |
| 421 | spin_unlock_bh(&msk->pm.lock); |
| 422 | mptcp_subflow_shutdown(sk, ssk, how); |
| 423 | __mptcp_close_ssk(sk, ssk, subflow, timeout); |
| 424 | spin_lock_bh(&msk->pm.lock); |
| 425 | |
| 426 | msk->pm.add_addr_accepted--; |
| 427 | msk->pm.subflows--; |
| 428 | WRITE_ONCE(msk->pm.accept_addr, true); |
| 429 | |
Geliang Tang | 7a7e52e | 2020-09-24 08:29:56 +0800 | [diff] [blame] | 430 | __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMADDR); |
| 431 | |
Geliang Tang | d0876b2 | 2020-09-24 08:29:49 +0800 | [diff] [blame] | 432 | break; |
| 433 | } |
| 434 | } |
| 435 | |
Geliang Tang | 0ee4261 | 2020-09-24 08:29:55 +0800 | [diff] [blame] | 436 | void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk, u8 rm_id) |
| 437 | { |
| 438 | struct mptcp_subflow_context *subflow, *tmp; |
| 439 | struct sock *sk = (struct sock *)msk; |
| 440 | |
| 441 | pr_debug("subflow rm_id %d", rm_id); |
| 442 | |
| 443 | if (!rm_id) |
| 444 | return; |
| 445 | |
| 446 | if (list_empty(&msk->conn_list)) |
| 447 | return; |
| 448 | |
| 449 | list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) { |
| 450 | struct sock *ssk = mptcp_subflow_tcp_sock(subflow); |
| 451 | int how = RCV_SHUTDOWN | SEND_SHUTDOWN; |
| 452 | long timeout = 0; |
| 453 | |
| 454 | if (rm_id != subflow->local_id) |
| 455 | continue; |
| 456 | |
| 457 | spin_unlock_bh(&msk->pm.lock); |
| 458 | mptcp_subflow_shutdown(sk, ssk, how); |
| 459 | __mptcp_close_ssk(sk, ssk, subflow, timeout); |
| 460 | spin_lock_bh(&msk->pm.lock); |
| 461 | |
| 462 | msk->pm.local_addr_used--; |
| 463 | msk->pm.subflows--; |
| 464 | |
Geliang Tang | 7a7e52e | 2020-09-24 08:29:56 +0800 | [diff] [blame] | 465 | __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMSUBFLOW); |
| 466 | |
Geliang Tang | 0ee4261 | 2020-09-24 08:29:55 +0800 | [diff] [blame] | 467 | break; |
| 468 | } |
| 469 | } |
| 470 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 471 | static bool address_use_port(struct mptcp_pm_addr_entry *entry) |
| 472 | { |
Paolo Abeni | ef0da3b | 2020-09-14 10:01:15 +0200 | [diff] [blame] | 473 | return (entry->addr.flags & |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 474 | (MPTCP_PM_ADDR_FLAG_SIGNAL | MPTCP_PM_ADDR_FLAG_SUBFLOW)) == |
| 475 | MPTCP_PM_ADDR_FLAG_SIGNAL; |
| 476 | } |
| 477 | |
| 478 | static int mptcp_pm_nl_append_new_local_addr(struct pm_nl_pernet *pernet, |
| 479 | struct mptcp_pm_addr_entry *entry) |
| 480 | { |
| 481 | struct mptcp_pm_addr_entry *cur; |
| 482 | int ret = -EINVAL; |
| 483 | |
| 484 | spin_lock_bh(&pernet->lock); |
| 485 | /* to keep the code simple, don't do IDR-like allocation for address ID, |
| 486 | * just bail when we exceed limits |
| 487 | */ |
| 488 | if (pernet->next_id > 255) |
| 489 | goto out; |
| 490 | if (pernet->addrs >= MPTCP_PM_ADDR_MAX) |
| 491 | goto out; |
| 492 | |
| 493 | /* do not insert duplicate address, differentiate on port only |
| 494 | * singled addresses |
| 495 | */ |
| 496 | list_for_each_entry(cur, &pernet->local_addr_list, list) { |
| 497 | if (addresses_equal(&cur->addr, &entry->addr, |
| 498 | address_use_port(entry) && |
| 499 | address_use_port(cur))) |
| 500 | goto out; |
| 501 | } |
| 502 | |
Paolo Abeni | ef0da3b | 2020-09-14 10:01:15 +0200 | [diff] [blame] | 503 | if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL) |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 504 | pernet->add_addr_signal_max++; |
Paolo Abeni | ef0da3b | 2020-09-14 10:01:15 +0200 | [diff] [blame] | 505 | if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 506 | pernet->local_addr_max++; |
| 507 | |
| 508 | entry->addr.id = pernet->next_id++; |
| 509 | pernet->addrs++; |
| 510 | list_add_tail_rcu(&entry->list, &pernet->local_addr_list); |
| 511 | ret = entry->addr.id; |
| 512 | |
| 513 | out: |
| 514 | spin_unlock_bh(&pernet->lock); |
| 515 | return ret; |
| 516 | } |
| 517 | |
| 518 | int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc) |
| 519 | { |
| 520 | struct mptcp_pm_addr_entry *entry; |
| 521 | struct mptcp_addr_info skc_local; |
| 522 | struct mptcp_addr_info msk_local; |
| 523 | struct pm_nl_pernet *pernet; |
| 524 | int ret = -1; |
| 525 | |
| 526 | if (WARN_ON_ONCE(!msk)) |
| 527 | return -1; |
| 528 | |
| 529 | /* The 0 ID mapping is defined by the first subflow, copied into the msk |
| 530 | * addr |
| 531 | */ |
| 532 | local_address((struct sock_common *)msk, &msk_local); |
Geliang Tang | 5702581 | 2020-09-08 10:49:38 +0800 | [diff] [blame] | 533 | local_address((struct sock_common *)skc, &skc_local); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 534 | if (addresses_equal(&msk_local, &skc_local, false)) |
| 535 | return 0; |
| 536 | |
Geliang Tang | 5702581 | 2020-09-08 10:49:38 +0800 | [diff] [blame] | 537 | if (address_zero(&skc_local)) |
| 538 | return 0; |
| 539 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 540 | pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id); |
| 541 | |
| 542 | rcu_read_lock(); |
| 543 | list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) { |
| 544 | if (addresses_equal(&entry->addr, &skc_local, false)) { |
| 545 | ret = entry->addr.id; |
| 546 | break; |
| 547 | } |
| 548 | } |
| 549 | rcu_read_unlock(); |
| 550 | if (ret >= 0) |
| 551 | return ret; |
| 552 | |
| 553 | /* address not found, add to local list */ |
Geliang Tang | f612eb7 | 2020-09-09 11:01:24 +0800 | [diff] [blame] | 554 | entry = kmalloc(sizeof(*entry), GFP_ATOMIC); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 555 | if (!entry) |
| 556 | return -ENOMEM; |
| 557 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 558 | entry->addr = skc_local; |
Paolo Abeni | ef0da3b | 2020-09-14 10:01:15 +0200 | [diff] [blame] | 559 | entry->addr.ifindex = 0; |
| 560 | entry->addr.flags = 0; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 561 | ret = mptcp_pm_nl_append_new_local_addr(pernet, entry); |
| 562 | if (ret < 0) |
| 563 | kfree(entry); |
| 564 | |
| 565 | return ret; |
| 566 | } |
| 567 | |
| 568 | void mptcp_pm_nl_data_init(struct mptcp_sock *msk) |
| 569 | { |
| 570 | struct mptcp_pm_data *pm = &msk->pm; |
| 571 | struct pm_nl_pernet *pernet; |
| 572 | bool subflows; |
| 573 | |
| 574 | pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id); |
| 575 | |
| 576 | pm->add_addr_signal_max = READ_ONCE(pernet->add_addr_signal_max); |
| 577 | pm->add_addr_accept_max = READ_ONCE(pernet->add_addr_accept_max); |
| 578 | pm->local_addr_max = READ_ONCE(pernet->local_addr_max); |
| 579 | pm->subflows_max = READ_ONCE(pernet->subflows_max); |
| 580 | subflows = !!pm->subflows_max; |
| 581 | WRITE_ONCE(pm->work_pending, (!!pm->local_addr_max && subflows) || |
| 582 | !!pm->add_addr_signal_max); |
| 583 | WRITE_ONCE(pm->accept_addr, !!pm->add_addr_accept_max && subflows); |
| 584 | WRITE_ONCE(pm->accept_subflow, subflows); |
| 585 | } |
| 586 | |
| 587 | #define MPTCP_PM_CMD_GRP_OFFSET 0 |
| 588 | |
| 589 | static const struct genl_multicast_group mptcp_pm_mcgrps[] = { |
| 590 | [MPTCP_PM_CMD_GRP_OFFSET] = { .name = MPTCP_PM_CMD_GRP_NAME, }, |
| 591 | }; |
| 592 | |
| 593 | static const struct nla_policy |
| 594 | mptcp_pm_addr_policy[MPTCP_PM_ADDR_ATTR_MAX + 1] = { |
| 595 | [MPTCP_PM_ADDR_ATTR_FAMILY] = { .type = NLA_U16, }, |
| 596 | [MPTCP_PM_ADDR_ATTR_ID] = { .type = NLA_U8, }, |
| 597 | [MPTCP_PM_ADDR_ATTR_ADDR4] = { .type = NLA_U32, }, |
Johannes Berg | 8140860 | 2020-08-18 10:17:31 +0200 | [diff] [blame] | 598 | [MPTCP_PM_ADDR_ATTR_ADDR6] = |
| 599 | NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)), |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 600 | [MPTCP_PM_ADDR_ATTR_PORT] = { .type = NLA_U16 }, |
| 601 | [MPTCP_PM_ADDR_ATTR_FLAGS] = { .type = NLA_U32 }, |
| 602 | [MPTCP_PM_ADDR_ATTR_IF_IDX] = { .type = NLA_S32 }, |
| 603 | }; |
| 604 | |
| 605 | static const struct nla_policy mptcp_pm_policy[MPTCP_PM_ATTR_MAX + 1] = { |
| 606 | [MPTCP_PM_ATTR_ADDR] = |
| 607 | NLA_POLICY_NESTED(mptcp_pm_addr_policy), |
| 608 | [MPTCP_PM_ATTR_RCV_ADD_ADDRS] = { .type = NLA_U32, }, |
| 609 | [MPTCP_PM_ATTR_SUBFLOWS] = { .type = NLA_U32, }, |
| 610 | }; |
| 611 | |
| 612 | static int mptcp_pm_family_to_addr(int family) |
| 613 | { |
| 614 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 615 | if (family == AF_INET6) |
| 616 | return MPTCP_PM_ADDR_ATTR_ADDR6; |
| 617 | #endif |
| 618 | return MPTCP_PM_ADDR_ATTR_ADDR4; |
| 619 | } |
| 620 | |
| 621 | static int mptcp_pm_parse_addr(struct nlattr *attr, struct genl_info *info, |
| 622 | bool require_family, |
| 623 | struct mptcp_pm_addr_entry *entry) |
| 624 | { |
| 625 | struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1]; |
| 626 | int err, addr_addr; |
| 627 | |
| 628 | if (!attr) { |
| 629 | GENL_SET_ERR_MSG(info, "missing address info"); |
| 630 | return -EINVAL; |
| 631 | } |
| 632 | |
| 633 | /* no validation needed - was already done via nested policy */ |
| 634 | err = nla_parse_nested_deprecated(tb, MPTCP_PM_ADDR_ATTR_MAX, attr, |
| 635 | mptcp_pm_addr_policy, info->extack); |
| 636 | if (err) |
| 637 | return err; |
| 638 | |
| 639 | memset(entry, 0, sizeof(*entry)); |
| 640 | if (!tb[MPTCP_PM_ADDR_ATTR_FAMILY]) { |
| 641 | if (!require_family) |
| 642 | goto skip_family; |
| 643 | |
| 644 | NL_SET_ERR_MSG_ATTR(info->extack, attr, |
| 645 | "missing family"); |
| 646 | return -EINVAL; |
| 647 | } |
| 648 | |
| 649 | entry->addr.family = nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_FAMILY]); |
| 650 | if (entry->addr.family != AF_INET |
| 651 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 652 | && entry->addr.family != AF_INET6 |
| 653 | #endif |
| 654 | ) { |
| 655 | NL_SET_ERR_MSG_ATTR(info->extack, attr, |
| 656 | "unknown address family"); |
| 657 | return -EINVAL; |
| 658 | } |
| 659 | addr_addr = mptcp_pm_family_to_addr(entry->addr.family); |
| 660 | if (!tb[addr_addr]) { |
| 661 | NL_SET_ERR_MSG_ATTR(info->extack, attr, |
| 662 | "missing address data"); |
| 663 | return -EINVAL; |
| 664 | } |
| 665 | |
| 666 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 667 | if (entry->addr.family == AF_INET6) |
| 668 | entry->addr.addr6 = nla_get_in6_addr(tb[addr_addr]); |
| 669 | else |
| 670 | #endif |
| 671 | entry->addr.addr.s_addr = nla_get_in_addr(tb[addr_addr]); |
| 672 | |
| 673 | skip_family: |
Paolo Abeni | ef0da3b | 2020-09-14 10:01:15 +0200 | [diff] [blame] | 674 | if (tb[MPTCP_PM_ADDR_ATTR_IF_IDX]) { |
| 675 | u32 val = nla_get_s32(tb[MPTCP_PM_ADDR_ATTR_IF_IDX]); |
| 676 | |
| 677 | entry->addr.ifindex = val; |
| 678 | } |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 679 | |
| 680 | if (tb[MPTCP_PM_ADDR_ATTR_ID]) |
| 681 | entry->addr.id = nla_get_u8(tb[MPTCP_PM_ADDR_ATTR_ID]); |
| 682 | |
| 683 | if (tb[MPTCP_PM_ADDR_ATTR_FLAGS]) |
Paolo Abeni | ef0da3b | 2020-09-14 10:01:15 +0200 | [diff] [blame] | 684 | entry->addr.flags = nla_get_u32(tb[MPTCP_PM_ADDR_ATTR_FLAGS]); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 685 | |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | static struct pm_nl_pernet *genl_info_pm_nl(struct genl_info *info) |
| 690 | { |
| 691 | return net_generic(genl_info_net(info), pm_nl_pernet_id); |
| 692 | } |
| 693 | |
| 694 | static int mptcp_nl_cmd_add_addr(struct sk_buff *skb, struct genl_info *info) |
| 695 | { |
| 696 | struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR]; |
| 697 | struct pm_nl_pernet *pernet = genl_info_pm_nl(info); |
| 698 | struct mptcp_pm_addr_entry addr, *entry; |
| 699 | int ret; |
| 700 | |
| 701 | ret = mptcp_pm_parse_addr(attr, info, true, &addr); |
| 702 | if (ret < 0) |
| 703 | return ret; |
| 704 | |
| 705 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 706 | if (!entry) { |
| 707 | GENL_SET_ERR_MSG(info, "can't allocate addr"); |
| 708 | return -ENOMEM; |
| 709 | } |
| 710 | |
| 711 | *entry = addr; |
| 712 | ret = mptcp_pm_nl_append_new_local_addr(pernet, entry); |
| 713 | if (ret < 0) { |
| 714 | GENL_SET_ERR_MSG(info, "too many addresses or duplicate one"); |
| 715 | kfree(entry); |
| 716 | return ret; |
| 717 | } |
| 718 | |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | static struct mptcp_pm_addr_entry * |
| 723 | __lookup_addr_by_id(struct pm_nl_pernet *pernet, unsigned int id) |
| 724 | { |
| 725 | struct mptcp_pm_addr_entry *entry; |
| 726 | |
| 727 | list_for_each_entry(entry, &pernet->local_addr_list, list) { |
| 728 | if (entry->addr.id == id) |
| 729 | return entry; |
| 730 | } |
| 731 | return NULL; |
| 732 | } |
| 733 | |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 734 | static bool remove_anno_list_by_saddr(struct mptcp_sock *msk, |
| 735 | struct mptcp_addr_info *addr) |
| 736 | { |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 737 | struct mptcp_pm_add_entry *entry; |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 738 | |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 739 | entry = mptcp_pm_del_add_timer(msk, addr); |
| 740 | if (entry) { |
| 741 | list_del(&entry->list); |
| 742 | kfree(entry); |
| 743 | return true; |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | return false; |
| 747 | } |
| 748 | |
| 749 | static bool mptcp_pm_remove_anno_addr(struct mptcp_sock *msk, |
| 750 | struct mptcp_addr_info *addr, |
| 751 | bool force) |
| 752 | { |
| 753 | bool ret; |
| 754 | |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 755 | ret = remove_anno_list_by_saddr(msk, addr); |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 756 | if (ret || force) { |
| 757 | spin_lock_bh(&msk->pm.lock); |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 758 | mptcp_pm_remove_addr(msk, addr->id); |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 759 | spin_unlock_bh(&msk->pm.lock); |
| 760 | } |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 761 | return ret; |
| 762 | } |
| 763 | |
| 764 | static int mptcp_nl_remove_subflow_and_signal_addr(struct net *net, |
| 765 | struct mptcp_addr_info *addr) |
| 766 | { |
| 767 | struct mptcp_sock *msk; |
| 768 | long s_slot = 0, s_num = 0; |
| 769 | |
| 770 | pr_debug("remove_id=%d", addr->id); |
| 771 | |
| 772 | while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { |
| 773 | struct sock *sk = (struct sock *)msk; |
| 774 | bool remove_subflow; |
| 775 | |
| 776 | if (list_empty(&msk->conn_list)) { |
| 777 | mptcp_pm_remove_anno_addr(msk, addr, false); |
| 778 | goto next; |
| 779 | } |
| 780 | |
| 781 | lock_sock(sk); |
| 782 | remove_subflow = lookup_subflow_by_saddr(&msk->conn_list, addr); |
| 783 | mptcp_pm_remove_anno_addr(msk, addr, remove_subflow); |
| 784 | if (remove_subflow) |
| 785 | mptcp_pm_remove_subflow(msk, addr->id); |
| 786 | release_sock(sk); |
| 787 | |
| 788 | next: |
| 789 | sock_put(sk); |
| 790 | cond_resched(); |
| 791 | } |
| 792 | |
| 793 | return 0; |
| 794 | } |
| 795 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 796 | static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info) |
| 797 | { |
| 798 | struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR]; |
| 799 | struct pm_nl_pernet *pernet = genl_info_pm_nl(info); |
| 800 | struct mptcp_pm_addr_entry addr, *entry; |
| 801 | int ret; |
| 802 | |
| 803 | ret = mptcp_pm_parse_addr(attr, info, false, &addr); |
| 804 | if (ret < 0) |
| 805 | return ret; |
| 806 | |
| 807 | spin_lock_bh(&pernet->lock); |
| 808 | entry = __lookup_addr_by_id(pernet, addr.addr.id); |
| 809 | if (!entry) { |
| 810 | GENL_SET_ERR_MSG(info, "address not found"); |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 811 | spin_unlock_bh(&pernet->lock); |
| 812 | return -EINVAL; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 813 | } |
Paolo Abeni | ef0da3b | 2020-09-14 10:01:15 +0200 | [diff] [blame] | 814 | if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL) |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 815 | pernet->add_addr_signal_max--; |
Paolo Abeni | ef0da3b | 2020-09-14 10:01:15 +0200 | [diff] [blame] | 816 | if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 817 | pernet->local_addr_max--; |
| 818 | |
| 819 | pernet->addrs--; |
| 820 | list_del_rcu(&entry->list); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 821 | spin_unlock_bh(&pernet->lock); |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 822 | |
| 823 | mptcp_nl_remove_subflow_and_signal_addr(sock_net(skb->sk), &entry->addr); |
| 824 | kfree_rcu(entry, rcu); |
| 825 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 826 | return ret; |
| 827 | } |
| 828 | |
| 829 | static void __flush_addrs(struct pm_nl_pernet *pernet) |
| 830 | { |
| 831 | while (!list_empty(&pernet->local_addr_list)) { |
| 832 | struct mptcp_pm_addr_entry *cur; |
| 833 | |
| 834 | cur = list_entry(pernet->local_addr_list.next, |
| 835 | struct mptcp_pm_addr_entry, list); |
| 836 | list_del_rcu(&cur->list); |
| 837 | kfree_rcu(cur, rcu); |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | static void __reset_counters(struct pm_nl_pernet *pernet) |
| 842 | { |
| 843 | pernet->add_addr_signal_max = 0; |
| 844 | pernet->add_addr_accept_max = 0; |
| 845 | pernet->local_addr_max = 0; |
| 846 | pernet->addrs = 0; |
| 847 | } |
| 848 | |
| 849 | static int mptcp_nl_cmd_flush_addrs(struct sk_buff *skb, struct genl_info *info) |
| 850 | { |
| 851 | struct pm_nl_pernet *pernet = genl_info_pm_nl(info); |
| 852 | |
| 853 | spin_lock_bh(&pernet->lock); |
| 854 | __flush_addrs(pernet); |
| 855 | __reset_counters(pernet); |
| 856 | spin_unlock_bh(&pernet->lock); |
| 857 | return 0; |
| 858 | } |
| 859 | |
| 860 | static int mptcp_nl_fill_addr(struct sk_buff *skb, |
| 861 | struct mptcp_pm_addr_entry *entry) |
| 862 | { |
| 863 | struct mptcp_addr_info *addr = &entry->addr; |
| 864 | struct nlattr *attr; |
| 865 | |
| 866 | attr = nla_nest_start(skb, MPTCP_PM_ATTR_ADDR); |
| 867 | if (!attr) |
| 868 | return -EMSGSIZE; |
| 869 | |
| 870 | if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_FAMILY, addr->family)) |
| 871 | goto nla_put_failure; |
| 872 | if (nla_put_u8(skb, MPTCP_PM_ADDR_ATTR_ID, addr->id)) |
| 873 | goto nla_put_failure; |
Paolo Abeni | ef0da3b | 2020-09-14 10:01:15 +0200 | [diff] [blame] | 874 | 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] | 875 | goto nla_put_failure; |
Paolo Abeni | ef0da3b | 2020-09-14 10:01:15 +0200 | [diff] [blame] | 876 | if (entry->addr.ifindex && |
| 877 | 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] | 878 | goto nla_put_failure; |
| 879 | |
Bo YU | b4e0f9a | 2020-04-23 10:10:03 +0800 | [diff] [blame] | 880 | if (addr->family == AF_INET && |
| 881 | nla_put_in_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR4, |
| 882 | addr->addr.s_addr)) |
| 883 | goto nla_put_failure; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 884 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
Bo YU | b4e0f9a | 2020-04-23 10:10:03 +0800 | [diff] [blame] | 885 | else if (addr->family == AF_INET6 && |
| 886 | nla_put_in6_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR6, &addr->addr6)) |
| 887 | goto nla_put_failure; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 888 | #endif |
| 889 | nla_nest_end(skb, attr); |
| 890 | return 0; |
| 891 | |
| 892 | nla_put_failure: |
| 893 | nla_nest_cancel(skb, attr); |
| 894 | return -EMSGSIZE; |
| 895 | } |
| 896 | |
| 897 | static int mptcp_nl_cmd_get_addr(struct sk_buff *skb, struct genl_info *info) |
| 898 | { |
| 899 | struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR]; |
| 900 | struct pm_nl_pernet *pernet = genl_info_pm_nl(info); |
| 901 | struct mptcp_pm_addr_entry addr, *entry; |
| 902 | struct sk_buff *msg; |
| 903 | void *reply; |
| 904 | int ret; |
| 905 | |
| 906 | ret = mptcp_pm_parse_addr(attr, info, false, &addr); |
| 907 | if (ret < 0) |
| 908 | return ret; |
| 909 | |
| 910 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 911 | if (!msg) |
| 912 | return -ENOMEM; |
| 913 | |
| 914 | reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0, |
| 915 | info->genlhdr->cmd); |
| 916 | if (!reply) { |
| 917 | GENL_SET_ERR_MSG(info, "not enough space in Netlink message"); |
| 918 | ret = -EMSGSIZE; |
| 919 | goto fail; |
| 920 | } |
| 921 | |
| 922 | spin_lock_bh(&pernet->lock); |
| 923 | entry = __lookup_addr_by_id(pernet, addr.addr.id); |
| 924 | if (!entry) { |
| 925 | GENL_SET_ERR_MSG(info, "address not found"); |
| 926 | ret = -EINVAL; |
| 927 | goto unlock_fail; |
| 928 | } |
| 929 | |
| 930 | ret = mptcp_nl_fill_addr(msg, entry); |
| 931 | if (ret) |
| 932 | goto unlock_fail; |
| 933 | |
| 934 | genlmsg_end(msg, reply); |
| 935 | ret = genlmsg_reply(msg, info); |
| 936 | spin_unlock_bh(&pernet->lock); |
| 937 | return ret; |
| 938 | |
| 939 | unlock_fail: |
| 940 | spin_unlock_bh(&pernet->lock); |
| 941 | |
| 942 | fail: |
| 943 | nlmsg_free(msg); |
| 944 | return ret; |
| 945 | } |
| 946 | |
| 947 | static int mptcp_nl_cmd_dump_addrs(struct sk_buff *msg, |
| 948 | struct netlink_callback *cb) |
| 949 | { |
| 950 | struct net *net = sock_net(msg->sk); |
| 951 | struct mptcp_pm_addr_entry *entry; |
| 952 | struct pm_nl_pernet *pernet; |
| 953 | int id = cb->args[0]; |
| 954 | void *hdr; |
| 955 | |
| 956 | pernet = net_generic(net, pm_nl_pernet_id); |
| 957 | |
| 958 | spin_lock_bh(&pernet->lock); |
| 959 | list_for_each_entry(entry, &pernet->local_addr_list, list) { |
| 960 | if (entry->addr.id <= id) |
| 961 | continue; |
| 962 | |
| 963 | hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, |
| 964 | cb->nlh->nlmsg_seq, &mptcp_genl_family, |
| 965 | NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR); |
| 966 | if (!hdr) |
| 967 | break; |
| 968 | |
| 969 | if (mptcp_nl_fill_addr(msg, entry) < 0) { |
| 970 | genlmsg_cancel(msg, hdr); |
| 971 | break; |
| 972 | } |
| 973 | |
| 974 | id = entry->addr.id; |
| 975 | genlmsg_end(msg, hdr); |
| 976 | } |
| 977 | spin_unlock_bh(&pernet->lock); |
| 978 | |
| 979 | cb->args[0] = id; |
| 980 | return msg->len; |
| 981 | } |
| 982 | |
| 983 | static int parse_limit(struct genl_info *info, int id, unsigned int *limit) |
| 984 | { |
| 985 | struct nlattr *attr = info->attrs[id]; |
| 986 | |
| 987 | if (!attr) |
| 988 | return 0; |
| 989 | |
| 990 | *limit = nla_get_u32(attr); |
| 991 | if (*limit > MPTCP_PM_ADDR_MAX) { |
| 992 | GENL_SET_ERR_MSG(info, "limit greater than maximum"); |
| 993 | return -EINVAL; |
| 994 | } |
| 995 | return 0; |
| 996 | } |
| 997 | |
| 998 | static int |
| 999 | mptcp_nl_cmd_set_limits(struct sk_buff *skb, struct genl_info *info) |
| 1000 | { |
| 1001 | struct pm_nl_pernet *pernet = genl_info_pm_nl(info); |
| 1002 | unsigned int rcv_addrs, subflows; |
| 1003 | int ret; |
| 1004 | |
| 1005 | spin_lock_bh(&pernet->lock); |
| 1006 | rcv_addrs = pernet->add_addr_accept_max; |
| 1007 | ret = parse_limit(info, MPTCP_PM_ATTR_RCV_ADD_ADDRS, &rcv_addrs); |
| 1008 | if (ret) |
| 1009 | goto unlock; |
| 1010 | |
| 1011 | subflows = pernet->subflows_max; |
| 1012 | ret = parse_limit(info, MPTCP_PM_ATTR_SUBFLOWS, &subflows); |
| 1013 | if (ret) |
| 1014 | goto unlock; |
| 1015 | |
| 1016 | WRITE_ONCE(pernet->add_addr_accept_max, rcv_addrs); |
| 1017 | WRITE_ONCE(pernet->subflows_max, subflows); |
| 1018 | |
| 1019 | unlock: |
| 1020 | spin_unlock_bh(&pernet->lock); |
| 1021 | return ret; |
| 1022 | } |
| 1023 | |
| 1024 | static int |
| 1025 | mptcp_nl_cmd_get_limits(struct sk_buff *skb, struct genl_info *info) |
| 1026 | { |
| 1027 | struct pm_nl_pernet *pernet = genl_info_pm_nl(info); |
| 1028 | struct sk_buff *msg; |
| 1029 | void *reply; |
| 1030 | |
| 1031 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1032 | if (!msg) |
| 1033 | return -ENOMEM; |
| 1034 | |
| 1035 | reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0, |
| 1036 | MPTCP_PM_CMD_GET_LIMITS); |
| 1037 | if (!reply) |
| 1038 | goto fail; |
| 1039 | |
| 1040 | if (nla_put_u32(msg, MPTCP_PM_ATTR_RCV_ADD_ADDRS, |
| 1041 | READ_ONCE(pernet->add_addr_accept_max))) |
| 1042 | goto fail; |
| 1043 | |
| 1044 | if (nla_put_u32(msg, MPTCP_PM_ATTR_SUBFLOWS, |
| 1045 | READ_ONCE(pernet->subflows_max))) |
| 1046 | goto fail; |
| 1047 | |
| 1048 | genlmsg_end(msg, reply); |
| 1049 | return genlmsg_reply(msg, info); |
| 1050 | |
| 1051 | fail: |
| 1052 | GENL_SET_ERR_MSG(info, "not enough space in Netlink message"); |
| 1053 | nlmsg_free(msg); |
| 1054 | return -EMSGSIZE; |
| 1055 | } |
| 1056 | |
Rikard Falkeborn | 674d3ab | 2020-10-05 01:44:16 +0200 | [diff] [blame] | 1057 | static const struct genl_small_ops mptcp_pm_ops[] = { |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1058 | { |
| 1059 | .cmd = MPTCP_PM_CMD_ADD_ADDR, |
| 1060 | .doit = mptcp_nl_cmd_add_addr, |
| 1061 | .flags = GENL_ADMIN_PERM, |
| 1062 | }, |
| 1063 | { |
| 1064 | .cmd = MPTCP_PM_CMD_DEL_ADDR, |
| 1065 | .doit = mptcp_nl_cmd_del_addr, |
| 1066 | .flags = GENL_ADMIN_PERM, |
| 1067 | }, |
| 1068 | { |
| 1069 | .cmd = MPTCP_PM_CMD_FLUSH_ADDRS, |
| 1070 | .doit = mptcp_nl_cmd_flush_addrs, |
| 1071 | .flags = GENL_ADMIN_PERM, |
| 1072 | }, |
| 1073 | { |
| 1074 | .cmd = MPTCP_PM_CMD_GET_ADDR, |
| 1075 | .doit = mptcp_nl_cmd_get_addr, |
| 1076 | .dumpit = mptcp_nl_cmd_dump_addrs, |
| 1077 | }, |
| 1078 | { |
| 1079 | .cmd = MPTCP_PM_CMD_SET_LIMITS, |
| 1080 | .doit = mptcp_nl_cmd_set_limits, |
| 1081 | .flags = GENL_ADMIN_PERM, |
| 1082 | }, |
| 1083 | { |
| 1084 | .cmd = MPTCP_PM_CMD_GET_LIMITS, |
| 1085 | .doit = mptcp_nl_cmd_get_limits, |
| 1086 | }, |
| 1087 | }; |
| 1088 | |
| 1089 | static struct genl_family mptcp_genl_family __ro_after_init = { |
| 1090 | .name = MPTCP_PM_NAME, |
| 1091 | .version = MPTCP_PM_VER, |
| 1092 | .maxattr = MPTCP_PM_ATTR_MAX, |
| 1093 | .policy = mptcp_pm_policy, |
| 1094 | .netnsok = true, |
| 1095 | .module = THIS_MODULE, |
Jakub Kicinski | 66a9b92 | 2020-10-02 14:49:54 -0700 | [diff] [blame] | 1096 | .small_ops = mptcp_pm_ops, |
| 1097 | .n_small_ops = ARRAY_SIZE(mptcp_pm_ops), |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1098 | .mcgrps = mptcp_pm_mcgrps, |
| 1099 | .n_mcgrps = ARRAY_SIZE(mptcp_pm_mcgrps), |
| 1100 | }; |
| 1101 | |
| 1102 | static int __net_init pm_nl_init_net(struct net *net) |
| 1103 | { |
| 1104 | struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id); |
| 1105 | |
| 1106 | INIT_LIST_HEAD_RCU(&pernet->local_addr_list); |
| 1107 | __reset_counters(pernet); |
| 1108 | pernet->next_id = 1; |
| 1109 | spin_lock_init(&pernet->lock); |
| 1110 | return 0; |
| 1111 | } |
| 1112 | |
| 1113 | static void __net_exit pm_nl_exit_net(struct list_head *net_list) |
| 1114 | { |
| 1115 | struct net *net; |
| 1116 | |
| 1117 | list_for_each_entry(net, net_list, exit_list) { |
| 1118 | /* net is removed from namespace list, can't race with |
| 1119 | * other modifiers |
| 1120 | */ |
| 1121 | __flush_addrs(net_generic(net, pm_nl_pernet_id)); |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | static struct pernet_operations mptcp_pm_pernet_ops = { |
| 1126 | .init = pm_nl_init_net, |
| 1127 | .exit_batch = pm_nl_exit_net, |
| 1128 | .id = &pm_nl_pernet_id, |
| 1129 | .size = sizeof(struct pm_nl_pernet), |
| 1130 | }; |
| 1131 | |
Paolo Abeni | d39dcec | 2020-06-26 19:29:59 +0200 | [diff] [blame] | 1132 | void __init mptcp_pm_nl_init(void) |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1133 | { |
| 1134 | if (register_pernet_subsys(&mptcp_pm_pernet_ops) < 0) |
| 1135 | panic("Failed to register MPTCP PM pernet subsystem.\n"); |
| 1136 | |
| 1137 | if (genl_register_family(&mptcp_genl_family)) |
| 1138 | panic("Failed to register MPTCP PM netlink family\n"); |
| 1139 | } |