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