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 | |
Yonglong Li | 18fc1a9 | 2021-08-23 18:05:40 -0700 | [diff] [blame] | 320 | if (mptcp_pm_should_add_signal_addr(msk)) { |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 321 | sk_reset_timer(sk, timer, jiffies + TCP_RTO_MAX / 8); |
| 322 | goto out; |
| 323 | } |
| 324 | |
| 325 | spin_lock_bh(&msk->pm.lock); |
| 326 | |
Yonglong Li | 18fc1a9 | 2021-08-23 18:05:40 -0700 | [diff] [blame] | 327 | if (!mptcp_pm_should_add_signal_addr(msk)) { |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 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 | |
Geliang Tang | 2843ff6 | 2021-08-17 15:07:23 -0700 | [diff] [blame] | 413 | static bool lookup_address_in_vec(struct mptcp_addr_info *addrs, unsigned int nr, |
| 414 | struct mptcp_addr_info *addr) |
| 415 | { |
| 416 | int i; |
| 417 | |
| 418 | for (i = 0; i < nr; i++) { |
| 419 | if (addresses_equal(&addrs[i], addr, addr->port)) |
| 420 | return true; |
| 421 | } |
| 422 | |
| 423 | return false; |
| 424 | } |
| 425 | |
| 426 | /* Fill all the remote addresses into the array addrs[], |
| 427 | * and return the array size. |
| 428 | */ |
| 429 | static unsigned int fill_remote_addresses_vec(struct mptcp_sock *msk, bool fullmesh, |
| 430 | struct mptcp_addr_info *addrs) |
| 431 | { |
| 432 | struct sock *sk = (struct sock *)msk, *ssk; |
| 433 | struct mptcp_subflow_context *subflow; |
| 434 | struct mptcp_addr_info remote = { 0 }; |
| 435 | unsigned int subflows_max; |
| 436 | int i = 0; |
| 437 | |
| 438 | subflows_max = mptcp_pm_get_subflows_max(msk); |
| 439 | |
| 440 | /* Non-fullmesh endpoint, fill in the single entry |
| 441 | * corresponding to the primary MPC subflow remote address |
| 442 | */ |
| 443 | if (!fullmesh) { |
| 444 | remote_address((struct sock_common *)sk, &remote); |
| 445 | msk->pm.subflows++; |
| 446 | addrs[i++] = remote; |
| 447 | } else { |
| 448 | mptcp_for_each_subflow(msk, subflow) { |
| 449 | ssk = mptcp_subflow_tcp_sock(subflow); |
| 450 | remote_address((struct sock_common *)ssk, &remote); |
| 451 | if (!lookup_address_in_vec(addrs, i, &remote) && |
| 452 | msk->pm.subflows < subflows_max) { |
| 453 | msk->pm.subflows++; |
| 454 | addrs[i++] = remote; |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | return i; |
| 460 | } |
| 461 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 462 | static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk) |
| 463 | { |
| 464 | struct sock *sk = (struct sock *)msk; |
| 465 | struct mptcp_pm_addr_entry *local; |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 466 | unsigned int add_addr_signal_max; |
| 467 | unsigned int local_addr_max; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 468 | struct pm_nl_pernet *pernet; |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 469 | unsigned int subflows_max; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 470 | |
Geliang Tang | 432d9e7 | 2020-12-09 15:51:28 -0800 | [diff] [blame] | 471 | pernet = net_generic(sock_net(sk), pm_nl_pernet_id); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 472 | |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 473 | add_addr_signal_max = mptcp_pm_get_add_addr_signal_max(msk); |
| 474 | local_addr_max = mptcp_pm_get_local_addr_max(msk); |
| 475 | subflows_max = mptcp_pm_get_subflows_max(msk); |
| 476 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 477 | pr_debug("local %d:%d signal %d:%d subflows %d:%d\n", |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 478 | msk->pm.local_addr_used, local_addr_max, |
| 479 | msk->pm.add_addr_signaled, add_addr_signal_max, |
| 480 | msk->pm.subflows, subflows_max); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 481 | |
| 482 | /* check first for announce */ |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 483 | if (msk->pm.add_addr_signaled < add_addr_signal_max) { |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 484 | local = select_signal_address(pernet, |
| 485 | msk->pm.add_addr_signaled); |
| 486 | |
| 487 | if (local) { |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 488 | if (mptcp_pm_alloc_anno_list(msk, local)) { |
| 489 | msk->pm.add_addr_signaled++; |
Geliang Tang | f7efc77 | 2021-03-26 11:26:31 -0700 | [diff] [blame] | 490 | mptcp_pm_announce_addr(msk, &local->addr, false); |
Geliang Tang | b46a023 | 2021-03-26 11:26:41 -0700 | [diff] [blame] | 491 | mptcp_pm_nl_addr_send_ack(msk); |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 492 | } |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 493 | } else { |
| 494 | /* pick failed, avoid fourther attempts later */ |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 495 | msk->pm.local_addr_used = add_addr_signal_max; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | check_work_pending(msk); |
| 499 | } |
| 500 | |
| 501 | /* check if should create a new subflow */ |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 502 | if (msk->pm.local_addr_used < local_addr_max && |
Geliang Tang | df377be | 2021-06-22 12:25:20 -0700 | [diff] [blame] | 503 | msk->pm.subflows < subflows_max && |
| 504 | !READ_ONCE(msk->pm.remote_deny_join_id0)) { |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 505 | local = select_local_address(pernet, msk); |
| 506 | if (local) { |
Geliang Tang | 2843ff6 | 2021-08-17 15:07:23 -0700 | [diff] [blame] | 507 | bool fullmesh = !!(local->flags & MPTCP_PM_ADDR_FLAG_FULLMESH); |
| 508 | struct mptcp_addr_info addrs[MPTCP_PM_ADDR_MAX]; |
| 509 | int i, nr; |
Matthieu Baerts | 1f2f193 | 2021-01-25 10:59:02 -0800 | [diff] [blame] | 510 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 511 | msk->pm.local_addr_used++; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 512 | check_work_pending(msk); |
Geliang Tang | 2843ff6 | 2021-08-17 15:07:23 -0700 | [diff] [blame] | 513 | nr = fill_remote_addresses_vec(msk, fullmesh, addrs); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 514 | spin_unlock_bh(&msk->pm.lock); |
Geliang Tang | 2843ff6 | 2021-08-17 15:07:23 -0700 | [diff] [blame] | 515 | for (i = 0; i < nr; i++) |
| 516 | __mptcp_subflow_connect(sk, &local->addr, &addrs[i]); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 517 | spin_lock_bh(&msk->pm.lock); |
| 518 | return; |
| 519 | } |
| 520 | |
| 521 | /* lookup failed, avoid fourther attempts later */ |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 522 | msk->pm.local_addr_used = local_addr_max; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 523 | check_work_pending(msk); |
| 524 | } |
| 525 | } |
| 526 | |
Florian Westphal | e980143 | 2021-02-12 15:59:54 -0800 | [diff] [blame] | 527 | static void mptcp_pm_nl_fully_established(struct mptcp_sock *msk) |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 528 | { |
| 529 | mptcp_pm_create_subflow_or_signal_addr(msk); |
| 530 | } |
| 531 | |
Florian Westphal | e980143 | 2021-02-12 15:59:54 -0800 | [diff] [blame] | 532 | static void mptcp_pm_nl_subflow_established(struct mptcp_sock *msk) |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 533 | { |
| 534 | mptcp_pm_create_subflow_or_signal_addr(msk); |
| 535 | } |
| 536 | |
Geliang Tang | 1a0d613 | 2021-08-17 15:07:24 -0700 | [diff] [blame] | 537 | /* Fill all the local addresses into the array addrs[], |
| 538 | * and return the array size. |
| 539 | */ |
| 540 | static unsigned int fill_local_addresses_vec(struct mptcp_sock *msk, |
| 541 | struct mptcp_addr_info *addrs) |
| 542 | { |
| 543 | struct sock *sk = (struct sock *)msk; |
| 544 | struct mptcp_pm_addr_entry *entry; |
| 545 | struct mptcp_addr_info local; |
| 546 | struct pm_nl_pernet *pernet; |
| 547 | unsigned int subflows_max; |
| 548 | int i = 0; |
| 549 | |
| 550 | pernet = net_generic(sock_net(sk), pm_nl_pernet_id); |
| 551 | subflows_max = mptcp_pm_get_subflows_max(msk); |
| 552 | |
| 553 | rcu_read_lock(); |
| 554 | __mptcp_flush_join_list(msk); |
| 555 | list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) { |
| 556 | if (!(entry->flags & MPTCP_PM_ADDR_FLAG_FULLMESH)) |
| 557 | continue; |
| 558 | |
| 559 | if (entry->addr.family != sk->sk_family) { |
| 560 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 561 | if ((entry->addr.family == AF_INET && |
| 562 | !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) || |
| 563 | (sk->sk_family == AF_INET && |
| 564 | !ipv6_addr_v4mapped(&entry->addr.addr6))) |
| 565 | #endif |
| 566 | continue; |
| 567 | } |
| 568 | |
| 569 | if (msk->pm.subflows < subflows_max) { |
| 570 | msk->pm.subflows++; |
| 571 | addrs[i++] = entry->addr; |
| 572 | } |
| 573 | } |
| 574 | rcu_read_unlock(); |
| 575 | |
| 576 | /* If the array is empty, fill in the single |
| 577 | * 'IPADDRANY' local address |
| 578 | */ |
| 579 | if (!i) { |
| 580 | memset(&local, 0, sizeof(local)); |
| 581 | local.family = msk->pm.remote.family; |
| 582 | |
| 583 | msk->pm.subflows++; |
| 584 | addrs[i++] = local; |
| 585 | } |
| 586 | |
| 587 | return i; |
| 588 | } |
| 589 | |
Florian Westphal | e980143 | 2021-02-12 15:59:54 -0800 | [diff] [blame] | 590 | static void mptcp_pm_nl_add_addr_received(struct mptcp_sock *msk) |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 591 | { |
Geliang Tang | 1a0d613 | 2021-08-17 15:07:24 -0700 | [diff] [blame] | 592 | struct mptcp_addr_info addrs[MPTCP_PM_ADDR_MAX]; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 593 | struct sock *sk = (struct sock *)msk; |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 594 | unsigned int add_addr_accept_max; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 595 | struct mptcp_addr_info remote; |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 596 | unsigned int subflows_max; |
Geliang Tang | 1a0d613 | 2021-08-17 15:07:24 -0700 | [diff] [blame] | 597 | int i, nr; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 598 | |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 599 | add_addr_accept_max = mptcp_pm_get_add_addr_accept_max(msk); |
| 600 | subflows_max = mptcp_pm_get_subflows_max(msk); |
| 601 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 602 | pr_debug("accepted %d:%d remote family %d", |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 603 | msk->pm.add_addr_accepted, add_addr_accept_max, |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 604 | msk->pm.remote.family); |
Geliang Tang | d84ad04 | 2021-03-26 11:26:32 -0700 | [diff] [blame] | 605 | |
| 606 | if (lookup_subflow_by_daddr(&msk->conn_list, &msk->pm.remote)) |
| 607 | goto add_addr_echo; |
| 608 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 609 | /* connect to the specified remote address, using whatever |
| 610 | * local address the routing configuration will pick. |
| 611 | */ |
| 612 | remote = msk->pm.remote; |
| 613 | if (!remote.port) |
| 614 | remote.port = sk->sk_dport; |
Geliang Tang | 1a0d613 | 2021-08-17 15:07:24 -0700 | [diff] [blame] | 615 | nr = fill_local_addresses_vec(msk, addrs); |
| 616 | |
| 617 | msk->pm.add_addr_accepted++; |
| 618 | if (msk->pm.add_addr_accepted >= add_addr_accept_max || |
| 619 | msk->pm.subflows >= subflows_max) |
| 620 | WRITE_ONCE(msk->pm.accept_addr, false); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 621 | |
| 622 | spin_unlock_bh(&msk->pm.lock); |
Geliang Tang | 1a0d613 | 2021-08-17 15:07:24 -0700 | [diff] [blame] | 623 | for (i = 0; i < nr; i++) |
| 624 | __mptcp_subflow_connect(sk, &addrs[i], &remote); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 625 | spin_lock_bh(&msk->pm.lock); |
Geliang Tang | 6a6c05a | 2020-09-24 08:29:50 +0800 | [diff] [blame] | 626 | |
Geliang Tang | d84ad04 | 2021-03-26 11:26:32 -0700 | [diff] [blame] | 627 | add_addr_echo: |
Geliang Tang | f7efc77 | 2021-03-26 11:26:31 -0700 | [diff] [blame] | 628 | mptcp_pm_announce_addr(msk, &msk->pm.remote, true); |
Geliang Tang | b46a023 | 2021-03-26 11:26:41 -0700 | [diff] [blame] | 629 | mptcp_pm_nl_addr_send_ack(msk); |
Geliang Tang | 84dfe36 | 2020-11-19 11:46:00 -0800 | [diff] [blame] | 630 | } |
| 631 | |
Geliang Tang | b46a023 | 2021-03-26 11:26:41 -0700 | [diff] [blame] | 632 | void mptcp_pm_nl_addr_send_ack(struct mptcp_sock *msk) |
Geliang Tang | 84dfe36 | 2020-11-19 11:46:00 -0800 | [diff] [blame] | 633 | { |
| 634 | struct mptcp_subflow_context *subflow; |
| 635 | |
Florian Westphal | 3abc05d | 2021-02-04 15:23:30 -0800 | [diff] [blame] | 636 | msk_owned_by_me(msk); |
| 637 | lockdep_assert_held(&msk->pm.lock); |
| 638 | |
Geliang Tang | 8dd5efb | 2021-03-26 11:26:40 -0700 | [diff] [blame] | 639 | if (!mptcp_pm_should_add_signal(msk) && |
| 640 | !mptcp_pm_should_rm_signal(msk)) |
Geliang Tang | 84dfe36 | 2020-11-19 11:46:00 -0800 | [diff] [blame] | 641 | return; |
| 642 | |
| 643 | __mptcp_flush_join_list(msk); |
| 644 | subflow = list_first_entry_or_null(&msk->conn_list, typeof(*subflow), node); |
| 645 | if (subflow) { |
| 646 | struct sock *ssk = mptcp_subflow_tcp_sock(subflow); |
Geliang Tang | 84dfe36 | 2020-11-19 11:46:00 -0800 | [diff] [blame] | 647 | |
| 648 | spin_unlock_bh(&msk->pm.lock); |
Yonglong Li | c233ef1 | 2021-08-23 18:05:43 -0700 | [diff] [blame] | 649 | pr_debug("send ack for %s", |
| 650 | mptcp_pm_should_add_signal(msk) ? "add_addr" : "rm_addr"); |
Geliang Tang | fbe0f87 | 2020-12-09 15:51:23 -0800 | [diff] [blame] | 651 | |
Mat Martineau | 340fa66 | 2021-09-02 11:51:19 -0700 | [diff] [blame] | 652 | mptcp_subflow_send_ack(ssk); |
Geliang Tang | 84dfe36 | 2020-11-19 11:46:00 -0800 | [diff] [blame] | 653 | spin_lock_bh(&msk->pm.lock); |
Geliang Tang | 84dfe36 | 2020-11-19 11:46:00 -0800 | [diff] [blame] | 654 | } |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Mat Martineau | 3828c51 | 2021-10-15 16:05:52 -0700 | [diff] [blame] | 657 | static int mptcp_pm_nl_mp_prio_send_ack(struct mptcp_sock *msk, |
| 658 | struct mptcp_addr_info *addr, |
| 659 | u8 bkup) |
Geliang Tang | 0670654 | 2021-01-08 16:47:57 -0800 | [diff] [blame] | 660 | { |
| 661 | struct mptcp_subflow_context *subflow; |
| 662 | |
| 663 | pr_debug("bkup=%d", bkup); |
| 664 | |
| 665 | mptcp_for_each_subflow(msk, subflow) { |
| 666 | struct sock *ssk = mptcp_subflow_tcp_sock(subflow); |
Geliang Tang | 0be2ac2 | 2021-01-08 16:48:01 -0800 | [diff] [blame] | 667 | struct sock *sk = (struct sock *)msk; |
Geliang Tang | 0670654 | 2021-01-08 16:47:57 -0800 | [diff] [blame] | 668 | struct mptcp_addr_info local; |
| 669 | |
| 670 | local_address((struct sock_common *)ssk, &local); |
| 671 | if (!addresses_equal(&local, addr, addr->port)) |
| 672 | continue; |
| 673 | |
| 674 | subflow->backup = bkup; |
| 675 | subflow->send_mp_prio = 1; |
| 676 | subflow->request_bkup = bkup; |
Geliang Tang | 0be2ac2 | 2021-01-08 16:48:01 -0800 | [diff] [blame] | 677 | __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIOTX); |
Geliang Tang | 0670654 | 2021-01-08 16:47:57 -0800 | [diff] [blame] | 678 | |
| 679 | spin_unlock_bh(&msk->pm.lock); |
| 680 | pr_debug("send ack for mp_prio"); |
Mat Martineau | 340fa66 | 2021-09-02 11:51:19 -0700 | [diff] [blame] | 681 | mptcp_subflow_send_ack(ssk); |
Geliang Tang | 0670654 | 2021-01-08 16:47:57 -0800 | [diff] [blame] | 682 | spin_lock_bh(&msk->pm.lock); |
| 683 | |
| 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | return -EINVAL; |
| 688 | } |
| 689 | |
Geliang Tang | 9f12e97 | 2021-03-30 17:08:52 -0700 | [diff] [blame] | 690 | static void mptcp_pm_nl_rm_addr_or_subflow(struct mptcp_sock *msk, |
| 691 | const struct mptcp_rm_list *rm_list, |
| 692 | enum linux_mptcp_mib_field rm_type) |
Geliang Tang | d0876b2 | 2020-09-24 08:29:49 +0800 | [diff] [blame] | 693 | { |
| 694 | struct mptcp_subflow_context *subflow, *tmp; |
| 695 | struct sock *sk = (struct sock *)msk; |
Geliang Tang | d0b698c | 2021-03-12 17:16:15 -0800 | [diff] [blame] | 696 | u8 i; |
Geliang Tang | d0876b2 | 2020-09-24 08:29:49 +0800 | [diff] [blame] | 697 | |
Geliang Tang | 9f12e97 | 2021-03-30 17:08:52 -0700 | [diff] [blame] | 698 | pr_debug("%s rm_list_nr %d", |
| 699 | rm_type == MPTCP_MIB_RMADDR ? "address" : "subflow", rm_list->nr); |
Geliang Tang | d0876b2 | 2020-09-24 08:29:49 +0800 | [diff] [blame] | 700 | |
Florian Westphal | 3abc05d | 2021-02-04 15:23:30 -0800 | [diff] [blame] | 701 | msk_owned_by_me(msk); |
| 702 | |
Geliang Tang | 9f12e97 | 2021-03-30 17:08:52 -0700 | [diff] [blame] | 703 | if (!rm_list->nr) |
Geliang Tang | d0876b2 | 2020-09-24 08:29:49 +0800 | [diff] [blame] | 704 | return; |
| 705 | |
| 706 | if (list_empty(&msk->conn_list)) |
| 707 | return; |
| 708 | |
Geliang Tang | 9f12e97 | 2021-03-30 17:08:52 -0700 | [diff] [blame] | 709 | for (i = 0; i < rm_list->nr; i++) { |
Geliang Tang | d0b698c | 2021-03-12 17:16:15 -0800 | [diff] [blame] | 710 | list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) { |
| 711 | struct sock *ssk = mptcp_subflow_tcp_sock(subflow); |
| 712 | int how = RCV_SHUTDOWN | SEND_SHUTDOWN; |
Geliang Tang | 9f12e97 | 2021-03-30 17:08:52 -0700 | [diff] [blame] | 713 | u8 id = subflow->local_id; |
Geliang Tang | d0876b2 | 2020-09-24 08:29:49 +0800 | [diff] [blame] | 714 | |
Geliang Tang | 9f12e97 | 2021-03-30 17:08:52 -0700 | [diff] [blame] | 715 | if (rm_type == MPTCP_MIB_RMADDR) |
| 716 | id = subflow->remote_id; |
| 717 | |
| 718 | if (rm_list->ids[i] != id) |
Geliang Tang | d0b698c | 2021-03-12 17:16:15 -0800 | [diff] [blame] | 719 | continue; |
Geliang Tang | d0876b2 | 2020-09-24 08:29:49 +0800 | [diff] [blame] | 720 | |
Geliang Tang | 9f12e97 | 2021-03-30 17:08:52 -0700 | [diff] [blame] | 721 | pr_debug(" -> %s rm_list_ids[%d]=%u local_id=%u remote_id=%u", |
| 722 | rm_type == MPTCP_MIB_RMADDR ? "address" : "subflow", |
| 723 | i, rm_list->ids[i], subflow->local_id, subflow->remote_id); |
Geliang Tang | d0b698c | 2021-03-12 17:16:15 -0800 | [diff] [blame] | 724 | spin_unlock_bh(&msk->pm.lock); |
| 725 | mptcp_subflow_shutdown(sk, ssk, how); |
| 726 | mptcp_close_ssk(sk, ssk, subflow); |
| 727 | spin_lock_bh(&msk->pm.lock); |
Geliang Tang | d0876b2 | 2020-09-24 08:29:49 +0800 | [diff] [blame] | 728 | |
Geliang Tang | 9f12e97 | 2021-03-30 17:08:52 -0700 | [diff] [blame] | 729 | if (rm_type == MPTCP_MIB_RMADDR) { |
| 730 | msk->pm.add_addr_accepted--; |
| 731 | WRITE_ONCE(msk->pm.accept_addr, true); |
| 732 | } else if (rm_type == MPTCP_MIB_RMSUBFLOW) { |
| 733 | msk->pm.local_addr_used--; |
| 734 | } |
Geliang Tang | d0b698c | 2021-03-12 17:16:15 -0800 | [diff] [blame] | 735 | msk->pm.subflows--; |
Geliang Tang | 9f12e97 | 2021-03-30 17:08:52 -0700 | [diff] [blame] | 736 | __MPTCP_INC_STATS(sock_net(sk), rm_type); |
Geliang Tang | d0b698c | 2021-03-12 17:16:15 -0800 | [diff] [blame] | 737 | } |
Geliang Tang | d0876b2 | 2020-09-24 08:29:49 +0800 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | |
Geliang Tang | 9f12e97 | 2021-03-30 17:08:52 -0700 | [diff] [blame] | 741 | static void mptcp_pm_nl_rm_addr_received(struct mptcp_sock *msk) |
| 742 | { |
| 743 | mptcp_pm_nl_rm_addr_or_subflow(msk, &msk->pm.rm_list_rx, MPTCP_MIB_RMADDR); |
| 744 | } |
| 745 | |
| 746 | void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk, |
| 747 | const struct mptcp_rm_list *rm_list) |
| 748 | { |
| 749 | mptcp_pm_nl_rm_addr_or_subflow(msk, rm_list, MPTCP_MIB_RMSUBFLOW); |
| 750 | } |
| 751 | |
Florian Westphal | e980143 | 2021-02-12 15:59:54 -0800 | [diff] [blame] | 752 | void mptcp_pm_nl_work(struct mptcp_sock *msk) |
| 753 | { |
| 754 | struct mptcp_pm_data *pm = &msk->pm; |
| 755 | |
| 756 | msk_owned_by_me(msk); |
| 757 | |
| 758 | spin_lock_bh(&msk->pm.lock); |
| 759 | |
| 760 | pr_debug("msk=%p status=%x", msk, pm->status); |
| 761 | if (pm->status & BIT(MPTCP_PM_ADD_ADDR_RECEIVED)) { |
| 762 | pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_RECEIVED); |
| 763 | mptcp_pm_nl_add_addr_received(msk); |
| 764 | } |
| 765 | if (pm->status & BIT(MPTCP_PM_ADD_ADDR_SEND_ACK)) { |
| 766 | pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_SEND_ACK); |
Geliang Tang | b46a023 | 2021-03-26 11:26:41 -0700 | [diff] [blame] | 767 | mptcp_pm_nl_addr_send_ack(msk); |
Florian Westphal | e980143 | 2021-02-12 15:59:54 -0800 | [diff] [blame] | 768 | } |
| 769 | if (pm->status & BIT(MPTCP_PM_RM_ADDR_RECEIVED)) { |
| 770 | pm->status &= ~BIT(MPTCP_PM_RM_ADDR_RECEIVED); |
| 771 | mptcp_pm_nl_rm_addr_received(msk); |
| 772 | } |
| 773 | if (pm->status & BIT(MPTCP_PM_ESTABLISHED)) { |
| 774 | pm->status &= ~BIT(MPTCP_PM_ESTABLISHED); |
| 775 | mptcp_pm_nl_fully_established(msk); |
| 776 | } |
| 777 | if (pm->status & BIT(MPTCP_PM_SUBFLOW_ESTABLISHED)) { |
| 778 | pm->status &= ~BIT(MPTCP_PM_SUBFLOW_ESTABLISHED); |
| 779 | mptcp_pm_nl_subflow_established(msk); |
| 780 | } |
| 781 | |
| 782 | spin_unlock_bh(&msk->pm.lock); |
| 783 | } |
| 784 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 785 | static bool address_use_port(struct mptcp_pm_addr_entry *entry) |
| 786 | { |
Geliang Tang | daa83ab | 2021-04-06 17:15:57 -0700 | [diff] [blame] | 787 | return (entry->flags & |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 788 | (MPTCP_PM_ADDR_FLAG_SIGNAL | MPTCP_PM_ADDR_FLAG_SUBFLOW)) == |
| 789 | MPTCP_PM_ADDR_FLAG_SIGNAL; |
| 790 | } |
| 791 | |
| 792 | static int mptcp_pm_nl_append_new_local_addr(struct pm_nl_pernet *pernet, |
| 793 | struct mptcp_pm_addr_entry *entry) |
| 794 | { |
| 795 | struct mptcp_pm_addr_entry *cur; |
Geliang Tang | 72603d2 | 2021-02-01 15:09:06 -0800 | [diff] [blame] | 796 | unsigned int addr_max; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 797 | int ret = -EINVAL; |
| 798 | |
| 799 | spin_lock_bh(&pernet->lock); |
| 800 | /* to keep the code simple, don't do IDR-like allocation for address ID, |
| 801 | * just bail when we exceed limits |
| 802 | */ |
Geliang Tang | efd5a4c | 2021-01-08 16:47:55 -0800 | [diff] [blame] | 803 | if (pernet->next_id == MAX_ADDR_ID) |
| 804 | pernet->next_id = 1; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 805 | if (pernet->addrs >= MPTCP_PM_ADDR_MAX) |
| 806 | goto out; |
Geliang Tang | efd5a4c | 2021-01-08 16:47:55 -0800 | [diff] [blame] | 807 | if (test_bit(entry->addr.id, pernet->id_bitmap)) |
| 808 | goto out; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 809 | |
| 810 | /* do not insert duplicate address, differentiate on port only |
| 811 | * singled addresses |
| 812 | */ |
| 813 | list_for_each_entry(cur, &pernet->local_addr_list, list) { |
| 814 | if (addresses_equal(&cur->addr, &entry->addr, |
| 815 | address_use_port(entry) && |
| 816 | address_use_port(cur))) |
| 817 | goto out; |
| 818 | } |
| 819 | |
Geliang Tang | efd5a4c | 2021-01-08 16:47:55 -0800 | [diff] [blame] | 820 | if (!entry->addr.id) { |
| 821 | find_next: |
| 822 | entry->addr.id = find_next_zero_bit(pernet->id_bitmap, |
| 823 | MAX_ADDR_ID + 1, |
| 824 | pernet->next_id); |
| 825 | if ((!entry->addr.id || entry->addr.id > MAX_ADDR_ID) && |
| 826 | pernet->next_id != 1) { |
| 827 | pernet->next_id = 1; |
| 828 | goto find_next; |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | if (!entry->addr.id || entry->addr.id > MAX_ADDR_ID) |
| 833 | goto out; |
| 834 | |
| 835 | __set_bit(entry->addr.id, pernet->id_bitmap); |
| 836 | if (entry->addr.id > pernet->next_id) |
| 837 | pernet->next_id = entry->addr.id; |
| 838 | |
Geliang Tang | daa83ab | 2021-04-06 17:15:57 -0700 | [diff] [blame] | 839 | if (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) { |
Geliang Tang | 72603d2 | 2021-02-01 15:09:06 -0800 | [diff] [blame] | 840 | addr_max = pernet->add_addr_signal_max; |
| 841 | WRITE_ONCE(pernet->add_addr_signal_max, addr_max + 1); |
| 842 | } |
Geliang Tang | daa83ab | 2021-04-06 17:15:57 -0700 | [diff] [blame] | 843 | if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) { |
Geliang Tang | 72603d2 | 2021-02-01 15:09:06 -0800 | [diff] [blame] | 844 | addr_max = pernet->local_addr_max; |
| 845 | WRITE_ONCE(pernet->local_addr_max, addr_max + 1); |
| 846 | } |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 847 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 848 | pernet->addrs++; |
| 849 | list_add_tail_rcu(&entry->list, &pernet->local_addr_list); |
| 850 | ret = entry->addr.id; |
| 851 | |
| 852 | out: |
| 853 | spin_unlock_bh(&pernet->lock); |
| 854 | return ret; |
| 855 | } |
| 856 | |
Geliang Tang | 1729cf1 | 2021-02-01 15:09:12 -0800 | [diff] [blame] | 857 | static int mptcp_pm_nl_create_listen_socket(struct sock *sk, |
| 858 | struct mptcp_pm_addr_entry *entry) |
| 859 | { |
| 860 | struct sockaddr_storage addr; |
| 861 | struct mptcp_sock *msk; |
| 862 | struct socket *ssock; |
| 863 | int backlog = 1024; |
| 864 | int err; |
| 865 | |
| 866 | err = sock_create_kern(sock_net(sk), entry->addr.family, |
| 867 | SOCK_STREAM, IPPROTO_MPTCP, &entry->lsk); |
| 868 | if (err) |
| 869 | return err; |
| 870 | |
| 871 | msk = mptcp_sk(entry->lsk->sk); |
| 872 | if (!msk) { |
| 873 | err = -EINVAL; |
| 874 | goto out; |
| 875 | } |
| 876 | |
| 877 | ssock = __mptcp_nmpc_socket(msk); |
| 878 | if (!ssock) { |
| 879 | err = -EINVAL; |
| 880 | goto out; |
| 881 | } |
| 882 | |
| 883 | mptcp_info2sockaddr(&entry->addr, &addr, entry->addr.family); |
| 884 | err = kernel_bind(ssock, (struct sockaddr *)&addr, |
| 885 | sizeof(struct sockaddr_in)); |
| 886 | if (err) { |
| 887 | pr_warn("kernel_bind error, err=%d", err); |
| 888 | goto out; |
| 889 | } |
| 890 | |
| 891 | err = kernel_listen(ssock, backlog); |
| 892 | if (err) { |
| 893 | pr_warn("kernel_listen error, err=%d", err); |
| 894 | goto out; |
| 895 | } |
| 896 | |
| 897 | return 0; |
| 898 | |
| 899 | out: |
| 900 | sock_release(entry->lsk); |
| 901 | return err; |
| 902 | } |
| 903 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 904 | int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc) |
| 905 | { |
| 906 | struct mptcp_pm_addr_entry *entry; |
| 907 | struct mptcp_addr_info skc_local; |
| 908 | struct mptcp_addr_info msk_local; |
| 909 | struct pm_nl_pernet *pernet; |
| 910 | int ret = -1; |
| 911 | |
| 912 | if (WARN_ON_ONCE(!msk)) |
| 913 | return -1; |
| 914 | |
| 915 | /* The 0 ID mapping is defined by the first subflow, copied into the msk |
| 916 | * addr |
| 917 | */ |
| 918 | local_address((struct sock_common *)msk, &msk_local); |
Geliang Tang | 5702581 | 2020-09-08 10:49:38 +0800 | [diff] [blame] | 919 | local_address((struct sock_common *)skc, &skc_local); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 920 | if (addresses_equal(&msk_local, &skc_local, false)) |
| 921 | return 0; |
| 922 | |
Geliang Tang | 5702581 | 2020-09-08 10:49:38 +0800 | [diff] [blame] | 923 | if (address_zero(&skc_local)) |
| 924 | return 0; |
| 925 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 926 | pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id); |
| 927 | |
| 928 | rcu_read_lock(); |
| 929 | list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) { |
Geliang Tang | 60b57bf | 2021-02-01 15:09:16 -0800 | [diff] [blame] | 930 | if (addresses_equal(&entry->addr, &skc_local, entry->addr.port)) { |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 931 | ret = entry->addr.id; |
| 932 | break; |
| 933 | } |
| 934 | } |
| 935 | rcu_read_unlock(); |
| 936 | if (ret >= 0) |
| 937 | return ret; |
| 938 | |
| 939 | /* address not found, add to local list */ |
Geliang Tang | f612eb7 | 2020-09-09 11:01:24 +0800 | [diff] [blame] | 940 | entry = kmalloc(sizeof(*entry), GFP_ATOMIC); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 941 | if (!entry) |
| 942 | return -ENOMEM; |
| 943 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 944 | entry->addr = skc_local; |
Geliang Tang | efd5a4c | 2021-01-08 16:47:55 -0800 | [diff] [blame] | 945 | entry->addr.id = 0; |
Geliang Tang | 1729cf1 | 2021-02-01 15:09:12 -0800 | [diff] [blame] | 946 | entry->addr.port = 0; |
Geliang Tang | daa83ab | 2021-04-06 17:15:57 -0700 | [diff] [blame] | 947 | entry->ifindex = 0; |
| 948 | entry->flags = 0; |
Geliang Tang | 1729cf1 | 2021-02-01 15:09:12 -0800 | [diff] [blame] | 949 | entry->lsk = NULL; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 950 | ret = mptcp_pm_nl_append_new_local_addr(pernet, entry); |
| 951 | if (ret < 0) |
| 952 | kfree(entry); |
| 953 | |
| 954 | return ret; |
| 955 | } |
| 956 | |
| 957 | void mptcp_pm_nl_data_init(struct mptcp_sock *msk) |
| 958 | { |
| 959 | struct mptcp_pm_data *pm = &msk->pm; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 960 | bool subflows; |
| 961 | |
Geliang Tang | a914e58 | 2021-02-01 15:09:07 -0800 | [diff] [blame] | 962 | subflows = !!mptcp_pm_get_subflows_max(msk); |
| 963 | WRITE_ONCE(pm->work_pending, (!!mptcp_pm_get_local_addr_max(msk) && subflows) || |
| 964 | !!mptcp_pm_get_add_addr_signal_max(msk)); |
| 965 | 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] | 966 | WRITE_ONCE(pm->accept_subflow, subflows); |
| 967 | } |
| 968 | |
Florian Westphal | b911c97 | 2021-02-12 16:00:01 -0800 | [diff] [blame] | 969 | #define MPTCP_PM_CMD_GRP_OFFSET 0 |
| 970 | #define MPTCP_PM_EV_GRP_OFFSET 1 |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 971 | |
| 972 | static const struct genl_multicast_group mptcp_pm_mcgrps[] = { |
| 973 | [MPTCP_PM_CMD_GRP_OFFSET] = { .name = MPTCP_PM_CMD_GRP_NAME, }, |
Florian Westphal | b911c97 | 2021-02-12 16:00:01 -0800 | [diff] [blame] | 974 | [MPTCP_PM_EV_GRP_OFFSET] = { .name = MPTCP_PM_EV_GRP_NAME, |
| 975 | .flags = GENL_UNS_ADMIN_PERM, |
| 976 | }, |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 977 | }; |
| 978 | |
| 979 | static const struct nla_policy |
| 980 | mptcp_pm_addr_policy[MPTCP_PM_ADDR_ATTR_MAX + 1] = { |
| 981 | [MPTCP_PM_ADDR_ATTR_FAMILY] = { .type = NLA_U16, }, |
| 982 | [MPTCP_PM_ADDR_ATTR_ID] = { .type = NLA_U8, }, |
| 983 | [MPTCP_PM_ADDR_ATTR_ADDR4] = { .type = NLA_U32, }, |
Johannes Berg | 8140860 | 2020-08-18 10:17:31 +0200 | [diff] [blame] | 984 | [MPTCP_PM_ADDR_ATTR_ADDR6] = |
| 985 | NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)), |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 986 | [MPTCP_PM_ADDR_ATTR_PORT] = { .type = NLA_U16 }, |
| 987 | [MPTCP_PM_ADDR_ATTR_FLAGS] = { .type = NLA_U32 }, |
| 988 | [MPTCP_PM_ADDR_ATTR_IF_IDX] = { .type = NLA_S32 }, |
| 989 | }; |
| 990 | |
| 991 | static const struct nla_policy mptcp_pm_policy[MPTCP_PM_ATTR_MAX + 1] = { |
| 992 | [MPTCP_PM_ATTR_ADDR] = |
| 993 | NLA_POLICY_NESTED(mptcp_pm_addr_policy), |
| 994 | [MPTCP_PM_ATTR_RCV_ADD_ADDRS] = { .type = NLA_U32, }, |
| 995 | [MPTCP_PM_ATTR_SUBFLOWS] = { .type = NLA_U32, }, |
| 996 | }; |
| 997 | |
Paolo Abeni | ff5a0b4 | 2021-08-13 15:15:45 -0700 | [diff] [blame] | 998 | void mptcp_pm_nl_subflow_chk_stale(const struct mptcp_sock *msk, struct sock *ssk) |
| 999 | { |
| 1000 | struct mptcp_subflow_context *iter, *subflow = mptcp_subflow_ctx(ssk); |
| 1001 | struct sock *sk = (struct sock *)msk; |
| 1002 | unsigned int active_max_loss_cnt; |
| 1003 | struct net *net = sock_net(sk); |
| 1004 | unsigned int stale_loss_cnt; |
| 1005 | bool slow; |
| 1006 | |
| 1007 | stale_loss_cnt = mptcp_stale_loss_cnt(net); |
| 1008 | if (subflow->stale || !stale_loss_cnt || subflow->stale_count <= stale_loss_cnt) |
| 1009 | return; |
| 1010 | |
| 1011 | /* look for another available subflow not in loss state */ |
| 1012 | active_max_loss_cnt = max_t(int, stale_loss_cnt - 1, 1); |
| 1013 | mptcp_for_each_subflow(msk, iter) { |
| 1014 | if (iter != subflow && mptcp_subflow_active(iter) && |
| 1015 | iter->stale_count < active_max_loss_cnt) { |
| 1016 | /* we have some alternatives, try to mark this subflow as idle ...*/ |
| 1017 | slow = lock_sock_fast(ssk); |
| 1018 | if (!tcp_rtx_and_write_queues_empty(ssk)) { |
| 1019 | subflow->stale = 1; |
| 1020 | __mptcp_retransmit_pending_data(sk); |
Paolo Abeni | fc1b4e3 | 2021-08-13 15:15:46 -0700 | [diff] [blame] | 1021 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_SUBFLOWSTALE); |
Paolo Abeni | ff5a0b4 | 2021-08-13 15:15:45 -0700 | [diff] [blame] | 1022 | } |
| 1023 | unlock_sock_fast(ssk, slow); |
| 1024 | |
| 1025 | /* always try to push the pending data regarless of re-injections: |
| 1026 | * we can possibly use backup subflows now, and subflow selection |
| 1027 | * is cheap under the msk socket lock |
| 1028 | */ |
| 1029 | __mptcp_push_pending(sk, 0); |
| 1030 | return; |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1035 | static int mptcp_pm_family_to_addr(int family) |
| 1036 | { |
| 1037 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 1038 | if (family == AF_INET6) |
| 1039 | return MPTCP_PM_ADDR_ATTR_ADDR6; |
| 1040 | #endif |
| 1041 | return MPTCP_PM_ADDR_ATTR_ADDR4; |
| 1042 | } |
| 1043 | |
| 1044 | static int mptcp_pm_parse_addr(struct nlattr *attr, struct genl_info *info, |
| 1045 | bool require_family, |
| 1046 | struct mptcp_pm_addr_entry *entry) |
| 1047 | { |
| 1048 | struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1]; |
| 1049 | int err, addr_addr; |
| 1050 | |
| 1051 | if (!attr) { |
| 1052 | GENL_SET_ERR_MSG(info, "missing address info"); |
| 1053 | return -EINVAL; |
| 1054 | } |
| 1055 | |
| 1056 | /* no validation needed - was already done via nested policy */ |
| 1057 | err = nla_parse_nested_deprecated(tb, MPTCP_PM_ADDR_ATTR_MAX, attr, |
| 1058 | mptcp_pm_addr_policy, info->extack); |
| 1059 | if (err) |
| 1060 | return err; |
| 1061 | |
| 1062 | memset(entry, 0, sizeof(*entry)); |
| 1063 | if (!tb[MPTCP_PM_ADDR_ATTR_FAMILY]) { |
| 1064 | if (!require_family) |
| 1065 | goto skip_family; |
| 1066 | |
| 1067 | NL_SET_ERR_MSG_ATTR(info->extack, attr, |
| 1068 | "missing family"); |
| 1069 | return -EINVAL; |
| 1070 | } |
| 1071 | |
| 1072 | entry->addr.family = nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_FAMILY]); |
| 1073 | if (entry->addr.family != AF_INET |
| 1074 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 1075 | && entry->addr.family != AF_INET6 |
| 1076 | #endif |
| 1077 | ) { |
| 1078 | NL_SET_ERR_MSG_ATTR(info->extack, attr, |
| 1079 | "unknown address family"); |
| 1080 | return -EINVAL; |
| 1081 | } |
| 1082 | addr_addr = mptcp_pm_family_to_addr(entry->addr.family); |
| 1083 | if (!tb[addr_addr]) { |
| 1084 | NL_SET_ERR_MSG_ATTR(info->extack, attr, |
| 1085 | "missing address data"); |
| 1086 | return -EINVAL; |
| 1087 | } |
| 1088 | |
| 1089 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 1090 | if (entry->addr.family == AF_INET6) |
| 1091 | entry->addr.addr6 = nla_get_in6_addr(tb[addr_addr]); |
| 1092 | else |
| 1093 | #endif |
| 1094 | entry->addr.addr.s_addr = nla_get_in_addr(tb[addr_addr]); |
| 1095 | |
| 1096 | skip_family: |
Paolo Abeni | ef0da3b | 2020-09-14 10:01:15 +0200 | [diff] [blame] | 1097 | if (tb[MPTCP_PM_ADDR_ATTR_IF_IDX]) { |
| 1098 | u32 val = nla_get_s32(tb[MPTCP_PM_ADDR_ATTR_IF_IDX]); |
| 1099 | |
Geliang Tang | daa83ab | 2021-04-06 17:15:57 -0700 | [diff] [blame] | 1100 | entry->ifindex = val; |
Paolo Abeni | ef0da3b | 2020-09-14 10:01:15 +0200 | [diff] [blame] | 1101 | } |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1102 | |
| 1103 | if (tb[MPTCP_PM_ADDR_ATTR_ID]) |
| 1104 | entry->addr.id = nla_get_u8(tb[MPTCP_PM_ADDR_ATTR_ID]); |
| 1105 | |
| 1106 | if (tb[MPTCP_PM_ADDR_ATTR_FLAGS]) |
Geliang Tang | daa83ab | 2021-04-06 17:15:57 -0700 | [diff] [blame] | 1107 | entry->flags = nla_get_u32(tb[MPTCP_PM_ADDR_ATTR_FLAGS]); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1108 | |
Jianguo Wu | eb5fb62 | 2021-05-27 16:54:28 -0700 | [diff] [blame] | 1109 | if (tb[MPTCP_PM_ADDR_ATTR_PORT]) { |
| 1110 | if (!(entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) { |
| 1111 | NL_SET_ERR_MSG_ATTR(info->extack, attr, |
| 1112 | "flags must have signal when using port"); |
| 1113 | return -EINVAL; |
| 1114 | } |
Geliang Tang | a77e917 | 2021-02-01 15:09:17 -0800 | [diff] [blame] | 1115 | 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] | 1116 | } |
Geliang Tang | a77e917 | 2021-02-01 15:09:17 -0800 | [diff] [blame] | 1117 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1118 | return 0; |
| 1119 | } |
| 1120 | |
| 1121 | static struct pm_nl_pernet *genl_info_pm_nl(struct genl_info *info) |
| 1122 | { |
| 1123 | return net_generic(genl_info_net(info), pm_nl_pernet_id); |
| 1124 | } |
| 1125 | |
Geliang Tang | 875b7671 | 2021-02-01 15:09:08 -0800 | [diff] [blame] | 1126 | static int mptcp_nl_add_subflow_or_signal_addr(struct net *net) |
| 1127 | { |
| 1128 | struct mptcp_sock *msk; |
| 1129 | long s_slot = 0, s_num = 0; |
| 1130 | |
| 1131 | while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { |
| 1132 | struct sock *sk = (struct sock *)msk; |
| 1133 | |
| 1134 | if (!READ_ONCE(msk->fully_established)) |
| 1135 | goto next; |
| 1136 | |
| 1137 | lock_sock(sk); |
| 1138 | spin_lock_bh(&msk->pm.lock); |
| 1139 | mptcp_pm_create_subflow_or_signal_addr(msk); |
| 1140 | spin_unlock_bh(&msk->pm.lock); |
| 1141 | release_sock(sk); |
| 1142 | |
| 1143 | next: |
| 1144 | sock_put(sk); |
| 1145 | cond_resched(); |
| 1146 | } |
| 1147 | |
| 1148 | return 0; |
| 1149 | } |
| 1150 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1151 | static int mptcp_nl_cmd_add_addr(struct sk_buff *skb, struct genl_info *info) |
| 1152 | { |
| 1153 | struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR]; |
| 1154 | struct pm_nl_pernet *pernet = genl_info_pm_nl(info); |
| 1155 | struct mptcp_pm_addr_entry addr, *entry; |
| 1156 | int ret; |
| 1157 | |
| 1158 | ret = mptcp_pm_parse_addr(attr, info, true, &addr); |
| 1159 | if (ret < 0) |
| 1160 | return ret; |
| 1161 | |
| 1162 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 1163 | if (!entry) { |
| 1164 | GENL_SET_ERR_MSG(info, "can't allocate addr"); |
| 1165 | return -ENOMEM; |
| 1166 | } |
| 1167 | |
| 1168 | *entry = addr; |
Geliang Tang | 1729cf1 | 2021-02-01 15:09:12 -0800 | [diff] [blame] | 1169 | if (entry->addr.port) { |
| 1170 | ret = mptcp_pm_nl_create_listen_socket(skb->sk, entry); |
| 1171 | if (ret) { |
| 1172 | GENL_SET_ERR_MSG(info, "create listen socket error"); |
| 1173 | kfree(entry); |
| 1174 | return ret; |
| 1175 | } |
| 1176 | } |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1177 | ret = mptcp_pm_nl_append_new_local_addr(pernet, entry); |
| 1178 | if (ret < 0) { |
| 1179 | GENL_SET_ERR_MSG(info, "too many addresses or duplicate one"); |
Geliang Tang | 1729cf1 | 2021-02-01 15:09:12 -0800 | [diff] [blame] | 1180 | if (entry->lsk) |
| 1181 | sock_release(entry->lsk); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1182 | kfree(entry); |
| 1183 | return ret; |
| 1184 | } |
| 1185 | |
Geliang Tang | 875b7671 | 2021-02-01 15:09:08 -0800 | [diff] [blame] | 1186 | mptcp_nl_add_subflow_or_signal_addr(sock_net(skb->sk)); |
| 1187 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1188 | return 0; |
| 1189 | } |
| 1190 | |
| 1191 | static struct mptcp_pm_addr_entry * |
| 1192 | __lookup_addr_by_id(struct pm_nl_pernet *pernet, unsigned int id) |
| 1193 | { |
| 1194 | struct mptcp_pm_addr_entry *entry; |
| 1195 | |
| 1196 | list_for_each_entry(entry, &pernet->local_addr_list, list) { |
| 1197 | if (entry->addr.id == id) |
| 1198 | return entry; |
| 1199 | } |
| 1200 | return NULL; |
| 1201 | } |
| 1202 | |
Geliang Tang | ee28525 | 2021-08-17 15:07:22 -0700 | [diff] [blame] | 1203 | int mptcp_pm_get_flags_and_ifindex_by_id(struct net *net, unsigned int id, |
| 1204 | u8 *flags, int *ifindex) |
| 1205 | { |
| 1206 | struct mptcp_pm_addr_entry *entry; |
| 1207 | |
| 1208 | *flags = 0; |
| 1209 | *ifindex = 0; |
| 1210 | |
| 1211 | if (id) { |
| 1212 | rcu_read_lock(); |
| 1213 | entry = __lookup_addr_by_id(net_generic(net, pm_nl_pernet_id), id); |
| 1214 | if (entry) { |
| 1215 | *flags = entry->flags; |
| 1216 | *ifindex = entry->ifindex; |
| 1217 | } |
| 1218 | rcu_read_unlock(); |
| 1219 | } |
| 1220 | |
| 1221 | return 0; |
| 1222 | } |
| 1223 | |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 1224 | static bool remove_anno_list_by_saddr(struct mptcp_sock *msk, |
| 1225 | struct mptcp_addr_info *addr) |
| 1226 | { |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 1227 | struct mptcp_pm_add_entry *entry; |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 1228 | |
Davide Caratti | d58300c | 2021-05-25 14:23:13 -0700 | [diff] [blame] | 1229 | entry = mptcp_pm_del_add_timer(msk, addr, false); |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 1230 | if (entry) { |
| 1231 | list_del(&entry->list); |
| 1232 | kfree(entry); |
| 1233 | return true; |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | return false; |
| 1237 | } |
| 1238 | |
| 1239 | static bool mptcp_pm_remove_anno_addr(struct mptcp_sock *msk, |
| 1240 | struct mptcp_addr_info *addr, |
| 1241 | bool force) |
| 1242 | { |
Geliang Tang | cbde278 | 2021-03-12 17:16:12 -0800 | [diff] [blame] | 1243 | struct mptcp_rm_list list = { .nr = 0 }; |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 1244 | bool ret; |
| 1245 | |
Geliang Tang | cbde278 | 2021-03-12 17:16:12 -0800 | [diff] [blame] | 1246 | list.ids[list.nr++] = addr->id; |
| 1247 | |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 1248 | ret = remove_anno_list_by_saddr(msk, addr); |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 1249 | if (ret || force) { |
| 1250 | spin_lock_bh(&msk->pm.lock); |
Geliang Tang | cbde278 | 2021-03-12 17:16:12 -0800 | [diff] [blame] | 1251 | mptcp_pm_remove_addr(msk, &list); |
Geliang Tang | 00cfd77 | 2020-09-24 08:30:02 +0800 | [diff] [blame] | 1252 | spin_unlock_bh(&msk->pm.lock); |
| 1253 | } |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 1254 | return ret; |
| 1255 | } |
| 1256 | |
| 1257 | static int mptcp_nl_remove_subflow_and_signal_addr(struct net *net, |
| 1258 | struct mptcp_addr_info *addr) |
| 1259 | { |
| 1260 | struct mptcp_sock *msk; |
| 1261 | long s_slot = 0, s_num = 0; |
Geliang Tang | ddd14bb | 2021-03-12 17:16:16 -0800 | [diff] [blame] | 1262 | struct mptcp_rm_list list = { .nr = 0 }; |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 1263 | |
| 1264 | pr_debug("remove_id=%d", addr->id); |
| 1265 | |
Geliang Tang | ddd14bb | 2021-03-12 17:16:16 -0800 | [diff] [blame] | 1266 | list.ids[list.nr++] = addr->id; |
| 1267 | |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 1268 | while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { |
| 1269 | struct sock *sk = (struct sock *)msk; |
| 1270 | bool remove_subflow; |
| 1271 | |
| 1272 | if (list_empty(&msk->conn_list)) { |
| 1273 | mptcp_pm_remove_anno_addr(msk, addr, false); |
| 1274 | goto next; |
| 1275 | } |
| 1276 | |
| 1277 | lock_sock(sk); |
| 1278 | remove_subflow = lookup_subflow_by_saddr(&msk->conn_list, addr); |
| 1279 | mptcp_pm_remove_anno_addr(msk, addr, remove_subflow); |
| 1280 | if (remove_subflow) |
Geliang Tang | ddd14bb | 2021-03-12 17:16:16 -0800 | [diff] [blame] | 1281 | mptcp_pm_remove_subflow(msk, &list); |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 1282 | release_sock(sk); |
| 1283 | |
| 1284 | next: |
| 1285 | sock_put(sk); |
| 1286 | cond_resched(); |
| 1287 | } |
| 1288 | |
| 1289 | return 0; |
| 1290 | } |
| 1291 | |
Paolo Abeni | a0eea5f | 2021-08-18 16:42:36 -0700 | [diff] [blame] | 1292 | /* caller must ensure the RCU grace period is already elapsed */ |
| 1293 | static void __mptcp_pm_release_addr_entry(struct mptcp_pm_addr_entry *entry) |
Geliang Tang | 1729cf1 | 2021-02-01 15:09:12 -0800 | [diff] [blame] | 1294 | { |
Paolo Abeni | a0eea5f | 2021-08-18 16:42:36 -0700 | [diff] [blame] | 1295 | if (entry->lsk) |
| 1296 | sock_release(entry->lsk); |
| 1297 | kfree(entry); |
Geliang Tang | 1729cf1 | 2021-02-01 15:09:12 -0800 | [diff] [blame] | 1298 | } |
| 1299 | |
Geliang Tang | 740d798 | 2021-03-30 17:08:53 -0700 | [diff] [blame] | 1300 | static int mptcp_nl_remove_id_zero_address(struct net *net, |
| 1301 | struct mptcp_addr_info *addr) |
| 1302 | { |
| 1303 | struct mptcp_rm_list list = { .nr = 0 }; |
| 1304 | long s_slot = 0, s_num = 0; |
| 1305 | struct mptcp_sock *msk; |
| 1306 | |
| 1307 | list.ids[list.nr++] = 0; |
| 1308 | |
| 1309 | while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { |
| 1310 | struct sock *sk = (struct sock *)msk; |
| 1311 | struct mptcp_addr_info msk_local; |
| 1312 | |
| 1313 | if (list_empty(&msk->conn_list)) |
| 1314 | goto next; |
| 1315 | |
| 1316 | local_address((struct sock_common *)msk, &msk_local); |
| 1317 | if (!addresses_equal(&msk_local, addr, addr->port)) |
| 1318 | goto next; |
| 1319 | |
| 1320 | lock_sock(sk); |
| 1321 | spin_lock_bh(&msk->pm.lock); |
| 1322 | mptcp_pm_remove_addr(msk, &list); |
| 1323 | mptcp_pm_nl_rm_subflow_received(msk, &list); |
| 1324 | spin_unlock_bh(&msk->pm.lock); |
| 1325 | release_sock(sk); |
| 1326 | |
| 1327 | next: |
| 1328 | sock_put(sk); |
| 1329 | cond_resched(); |
| 1330 | } |
| 1331 | |
| 1332 | return 0; |
| 1333 | } |
| 1334 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1335 | static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info) |
| 1336 | { |
| 1337 | struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR]; |
| 1338 | struct pm_nl_pernet *pernet = genl_info_pm_nl(info); |
| 1339 | struct mptcp_pm_addr_entry addr, *entry; |
Geliang Tang | 72603d2 | 2021-02-01 15:09:06 -0800 | [diff] [blame] | 1340 | unsigned int addr_max; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1341 | int ret; |
| 1342 | |
| 1343 | ret = mptcp_pm_parse_addr(attr, info, false, &addr); |
| 1344 | if (ret < 0) |
| 1345 | return ret; |
| 1346 | |
Geliang Tang | 740d798 | 2021-03-30 17:08:53 -0700 | [diff] [blame] | 1347 | /* the zero id address is special: the first address used by the msk |
| 1348 | * always gets such an id, so different subflows can have different zero |
| 1349 | * id addresses. Additionally zero id is not accounted for in id_bitmap. |
| 1350 | * Let's use an 'mptcp_rm_list' instead of the common remove code. |
| 1351 | */ |
| 1352 | if (addr.addr.id == 0) |
| 1353 | return mptcp_nl_remove_id_zero_address(sock_net(skb->sk), &addr.addr); |
| 1354 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1355 | spin_lock_bh(&pernet->lock); |
| 1356 | entry = __lookup_addr_by_id(pernet, addr.addr.id); |
| 1357 | if (!entry) { |
| 1358 | GENL_SET_ERR_MSG(info, "address not found"); |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 1359 | spin_unlock_bh(&pernet->lock); |
| 1360 | return -EINVAL; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1361 | } |
Geliang Tang | daa83ab | 2021-04-06 17:15:57 -0700 | [diff] [blame] | 1362 | if (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) { |
Geliang Tang | 72603d2 | 2021-02-01 15:09:06 -0800 | [diff] [blame] | 1363 | addr_max = pernet->add_addr_signal_max; |
| 1364 | WRITE_ONCE(pernet->add_addr_signal_max, addr_max - 1); |
| 1365 | } |
Geliang Tang | daa83ab | 2021-04-06 17:15:57 -0700 | [diff] [blame] | 1366 | if (entry->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW) { |
Geliang Tang | 72603d2 | 2021-02-01 15:09:06 -0800 | [diff] [blame] | 1367 | addr_max = pernet->local_addr_max; |
| 1368 | WRITE_ONCE(pernet->local_addr_max, addr_max - 1); |
| 1369 | } |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1370 | |
| 1371 | pernet->addrs--; |
| 1372 | list_del_rcu(&entry->list); |
Geliang Tang | efd5a4c | 2021-01-08 16:47:55 -0800 | [diff] [blame] | 1373 | __clear_bit(entry->addr.id, pernet->id_bitmap); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1374 | spin_unlock_bh(&pernet->lock); |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 1375 | |
| 1376 | mptcp_nl_remove_subflow_and_signal_addr(sock_net(skb->sk), &entry->addr); |
Paolo Abeni | a0eea5f | 2021-08-18 16:42:36 -0700 | [diff] [blame] | 1377 | synchronize_rcu(); |
| 1378 | __mptcp_pm_release_addr_entry(entry); |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 1379 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1380 | return ret; |
| 1381 | } |
| 1382 | |
Geliang Tang | 06faa22 | 2021-03-12 17:16:17 -0800 | [diff] [blame] | 1383 | static void mptcp_pm_remove_addrs_and_subflows(struct mptcp_sock *msk, |
| 1384 | struct list_head *rm_list) |
| 1385 | { |
| 1386 | struct mptcp_rm_list alist = { .nr = 0 }, slist = { .nr = 0 }; |
| 1387 | struct mptcp_pm_addr_entry *entry; |
| 1388 | |
| 1389 | list_for_each_entry(entry, rm_list, list) { |
| 1390 | if (lookup_subflow_by_saddr(&msk->conn_list, &entry->addr) && |
| 1391 | alist.nr < MPTCP_RM_IDS_MAX && |
| 1392 | slist.nr < MPTCP_RM_IDS_MAX) { |
| 1393 | alist.ids[alist.nr++] = entry->addr.id; |
| 1394 | slist.ids[slist.nr++] = entry->addr.id; |
| 1395 | } else if (remove_anno_list_by_saddr(msk, &entry->addr) && |
| 1396 | alist.nr < MPTCP_RM_IDS_MAX) { |
| 1397 | alist.ids[alist.nr++] = entry->addr.id; |
| 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | if (alist.nr) { |
| 1402 | spin_lock_bh(&msk->pm.lock); |
| 1403 | mptcp_pm_remove_addr(msk, &alist); |
| 1404 | spin_unlock_bh(&msk->pm.lock); |
| 1405 | } |
| 1406 | if (slist.nr) |
| 1407 | mptcp_pm_remove_subflow(msk, &slist); |
| 1408 | } |
| 1409 | |
| 1410 | static void mptcp_nl_remove_addrs_list(struct net *net, |
| 1411 | struct list_head *rm_list) |
| 1412 | { |
| 1413 | long s_slot = 0, s_num = 0; |
| 1414 | struct mptcp_sock *msk; |
| 1415 | |
| 1416 | if (list_empty(rm_list)) |
| 1417 | return; |
| 1418 | |
| 1419 | while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { |
| 1420 | struct sock *sk = (struct sock *)msk; |
| 1421 | |
| 1422 | lock_sock(sk); |
| 1423 | mptcp_pm_remove_addrs_and_subflows(msk, rm_list); |
| 1424 | release_sock(sk); |
| 1425 | |
| 1426 | sock_put(sk); |
| 1427 | cond_resched(); |
| 1428 | } |
| 1429 | } |
| 1430 | |
Paolo Abeni | a0eea5f | 2021-08-18 16:42:36 -0700 | [diff] [blame] | 1431 | /* caller must ensure the RCU grace period is already elapsed */ |
Geliang Tang | 0e4a3e6 | 2021-03-12 17:16:18 -0800 | [diff] [blame] | 1432 | static void __flush_addrs(struct list_head *list) |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1433 | { |
Geliang Tang | 141694d | 2020-12-10 14:24:59 -0800 | [diff] [blame] | 1434 | while (!list_empty(list)) { |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1435 | struct mptcp_pm_addr_entry *cur; |
| 1436 | |
Geliang Tang | 141694d | 2020-12-10 14:24:59 -0800 | [diff] [blame] | 1437 | cur = list_entry(list->next, |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1438 | struct mptcp_pm_addr_entry, list); |
| 1439 | list_del_rcu(&cur->list); |
Paolo Abeni | a0eea5f | 2021-08-18 16:42:36 -0700 | [diff] [blame] | 1440 | __mptcp_pm_release_addr_entry(cur); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | static void __reset_counters(struct pm_nl_pernet *pernet) |
| 1445 | { |
Geliang Tang | 72603d2 | 2021-02-01 15:09:06 -0800 | [diff] [blame] | 1446 | WRITE_ONCE(pernet->add_addr_signal_max, 0); |
| 1447 | WRITE_ONCE(pernet->add_addr_accept_max, 0); |
| 1448 | WRITE_ONCE(pernet->local_addr_max, 0); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1449 | pernet->addrs = 0; |
| 1450 | } |
| 1451 | |
| 1452 | static int mptcp_nl_cmd_flush_addrs(struct sk_buff *skb, struct genl_info *info) |
| 1453 | { |
| 1454 | struct pm_nl_pernet *pernet = genl_info_pm_nl(info); |
Geliang Tang | 141694d | 2020-12-10 14:24:59 -0800 | [diff] [blame] | 1455 | LIST_HEAD(free_list); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1456 | |
| 1457 | spin_lock_bh(&pernet->lock); |
Geliang Tang | 141694d | 2020-12-10 14:24:59 -0800 | [diff] [blame] | 1458 | list_splice_init(&pernet->local_addr_list, &free_list); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1459 | __reset_counters(pernet); |
Geliang Tang | efd5a4c | 2021-01-08 16:47:55 -0800 | [diff] [blame] | 1460 | pernet->next_id = 1; |
| 1461 | bitmap_zero(pernet->id_bitmap, MAX_ADDR_ID + 1); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1462 | spin_unlock_bh(&pernet->lock); |
Geliang Tang | 0e4a3e6 | 2021-03-12 17:16:18 -0800 | [diff] [blame] | 1463 | mptcp_nl_remove_addrs_list(sock_net(skb->sk), &free_list); |
Paolo Abeni | a0eea5f | 2021-08-18 16:42:36 -0700 | [diff] [blame] | 1464 | synchronize_rcu(); |
Geliang Tang | 0e4a3e6 | 2021-03-12 17:16:18 -0800 | [diff] [blame] | 1465 | __flush_addrs(&free_list); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1466 | return 0; |
| 1467 | } |
| 1468 | |
| 1469 | static int mptcp_nl_fill_addr(struct sk_buff *skb, |
| 1470 | struct mptcp_pm_addr_entry *entry) |
| 1471 | { |
| 1472 | struct mptcp_addr_info *addr = &entry->addr; |
| 1473 | struct nlattr *attr; |
| 1474 | |
| 1475 | attr = nla_nest_start(skb, MPTCP_PM_ATTR_ADDR); |
| 1476 | if (!attr) |
| 1477 | return -EMSGSIZE; |
| 1478 | |
| 1479 | if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_FAMILY, addr->family)) |
| 1480 | goto nla_put_failure; |
Geliang Tang | a77e917 | 2021-02-01 15:09:17 -0800 | [diff] [blame] | 1481 | if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_PORT, ntohs(addr->port))) |
| 1482 | goto nla_put_failure; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1483 | if (nla_put_u8(skb, MPTCP_PM_ADDR_ATTR_ID, addr->id)) |
| 1484 | goto nla_put_failure; |
Geliang Tang | daa83ab | 2021-04-06 17:15:57 -0700 | [diff] [blame] | 1485 | if (nla_put_u32(skb, MPTCP_PM_ADDR_ATTR_FLAGS, entry->flags)) |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1486 | goto nla_put_failure; |
Geliang Tang | daa83ab | 2021-04-06 17:15:57 -0700 | [diff] [blame] | 1487 | if (entry->ifindex && |
| 1488 | nla_put_s32(skb, MPTCP_PM_ADDR_ATTR_IF_IDX, entry->ifindex)) |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1489 | goto nla_put_failure; |
| 1490 | |
Bo YU | b4e0f9a | 2020-04-23 10:10:03 +0800 | [diff] [blame] | 1491 | if (addr->family == AF_INET && |
| 1492 | nla_put_in_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR4, |
| 1493 | addr->addr.s_addr)) |
| 1494 | goto nla_put_failure; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1495 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
Bo YU | b4e0f9a | 2020-04-23 10:10:03 +0800 | [diff] [blame] | 1496 | else if (addr->family == AF_INET6 && |
| 1497 | nla_put_in6_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR6, &addr->addr6)) |
| 1498 | goto nla_put_failure; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1499 | #endif |
| 1500 | nla_nest_end(skb, attr); |
| 1501 | return 0; |
| 1502 | |
| 1503 | nla_put_failure: |
| 1504 | nla_nest_cancel(skb, attr); |
| 1505 | return -EMSGSIZE; |
| 1506 | } |
| 1507 | |
| 1508 | static int mptcp_nl_cmd_get_addr(struct sk_buff *skb, struct genl_info *info) |
| 1509 | { |
| 1510 | struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR]; |
| 1511 | struct pm_nl_pernet *pernet = genl_info_pm_nl(info); |
| 1512 | struct mptcp_pm_addr_entry addr, *entry; |
| 1513 | struct sk_buff *msg; |
| 1514 | void *reply; |
| 1515 | int ret; |
| 1516 | |
| 1517 | ret = mptcp_pm_parse_addr(attr, info, false, &addr); |
| 1518 | if (ret < 0) |
| 1519 | return ret; |
| 1520 | |
| 1521 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1522 | if (!msg) |
| 1523 | return -ENOMEM; |
| 1524 | |
| 1525 | reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0, |
| 1526 | info->genlhdr->cmd); |
| 1527 | if (!reply) { |
| 1528 | GENL_SET_ERR_MSG(info, "not enough space in Netlink message"); |
| 1529 | ret = -EMSGSIZE; |
| 1530 | goto fail; |
| 1531 | } |
| 1532 | |
| 1533 | spin_lock_bh(&pernet->lock); |
| 1534 | entry = __lookup_addr_by_id(pernet, addr.addr.id); |
| 1535 | if (!entry) { |
| 1536 | GENL_SET_ERR_MSG(info, "address not found"); |
| 1537 | ret = -EINVAL; |
| 1538 | goto unlock_fail; |
| 1539 | } |
| 1540 | |
| 1541 | ret = mptcp_nl_fill_addr(msg, entry); |
| 1542 | if (ret) |
| 1543 | goto unlock_fail; |
| 1544 | |
| 1545 | genlmsg_end(msg, reply); |
| 1546 | ret = genlmsg_reply(msg, info); |
| 1547 | spin_unlock_bh(&pernet->lock); |
| 1548 | return ret; |
| 1549 | |
| 1550 | unlock_fail: |
| 1551 | spin_unlock_bh(&pernet->lock); |
| 1552 | |
| 1553 | fail: |
| 1554 | nlmsg_free(msg); |
| 1555 | return ret; |
| 1556 | } |
| 1557 | |
| 1558 | static int mptcp_nl_cmd_dump_addrs(struct sk_buff *msg, |
| 1559 | struct netlink_callback *cb) |
| 1560 | { |
| 1561 | struct net *net = sock_net(msg->sk); |
| 1562 | struct mptcp_pm_addr_entry *entry; |
| 1563 | struct pm_nl_pernet *pernet; |
| 1564 | int id = cb->args[0]; |
| 1565 | void *hdr; |
Geliang Tang | efd5a4c | 2021-01-08 16:47:55 -0800 | [diff] [blame] | 1566 | int i; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1567 | |
| 1568 | pernet = net_generic(net, pm_nl_pernet_id); |
| 1569 | |
| 1570 | spin_lock_bh(&pernet->lock); |
Geliang Tang | efd5a4c | 2021-01-08 16:47:55 -0800 | [diff] [blame] | 1571 | for (i = id; i < MAX_ADDR_ID + 1; i++) { |
| 1572 | if (test_bit(i, pernet->id_bitmap)) { |
| 1573 | entry = __lookup_addr_by_id(pernet, i); |
| 1574 | if (!entry) |
| 1575 | break; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1576 | |
Geliang Tang | efd5a4c | 2021-01-08 16:47:55 -0800 | [diff] [blame] | 1577 | if (entry->addr.id <= id) |
| 1578 | continue; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1579 | |
Geliang Tang | efd5a4c | 2021-01-08 16:47:55 -0800 | [diff] [blame] | 1580 | hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, |
| 1581 | cb->nlh->nlmsg_seq, &mptcp_genl_family, |
| 1582 | NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR); |
| 1583 | if (!hdr) |
| 1584 | break; |
| 1585 | |
| 1586 | if (mptcp_nl_fill_addr(msg, entry) < 0) { |
| 1587 | genlmsg_cancel(msg, hdr); |
| 1588 | break; |
| 1589 | } |
| 1590 | |
| 1591 | id = entry->addr.id; |
| 1592 | genlmsg_end(msg, hdr); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1593 | } |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 1594 | } |
| 1595 | spin_unlock_bh(&pernet->lock); |
| 1596 | |
| 1597 | cb->args[0] = id; |
| 1598 | return msg->len; |
| 1599 | } |
| 1600 | |
| 1601 | static int parse_limit(struct genl_info *info, int id, unsigned int *limit) |
| 1602 | { |
| 1603 | struct nlattr *attr = info->attrs[id]; |
| 1604 | |
| 1605 | if (!attr) |
| 1606 | return 0; |
| 1607 | |
| 1608 | *limit = nla_get_u32(attr); |
| 1609 | if (*limit > MPTCP_PM_ADDR_MAX) { |
| 1610 | GENL_SET_ERR_MSG(info, "limit greater than maximum"); |
| 1611 | return -EINVAL; |
| 1612 | } |
| 1613 | return 0; |
| 1614 | } |
| 1615 | |
| 1616 | static int |
| 1617 | mptcp_nl_cmd_set_limits(struct sk_buff *skb, struct genl_info *info) |
| 1618 | { |
| 1619 | struct pm_nl_pernet *pernet = genl_info_pm_nl(info); |
| 1620 | unsigned int rcv_addrs, subflows; |
| 1621 | int ret; |
| 1622 | |
| 1623 | spin_lock_bh(&pernet->lock); |
| 1624 | rcv_addrs = pernet->add_addr_accept_max; |
| 1625 | ret = parse_limit(info, MPTCP_PM_ATTR_RCV_ADD_ADDRS, &rcv_addrs); |
| 1626 | if (ret) |
| 1627 | goto unlock; |
| 1628 | |
| 1629 | subflows = pernet->subflows_max; |
| 1630 | ret = parse_limit(info, MPTCP_PM_ATTR_SUBFLOWS, &subflows); |
| 1631 | if (ret) |
| 1632 | goto unlock; |
| 1633 | |
| 1634 | WRITE_ONCE(pernet->add_addr_accept_max, rcv_addrs); |
| 1635 | WRITE_ONCE(pernet->subflows_max, subflows); |
| 1636 | |
| 1637 | unlock: |
| 1638 | spin_unlock_bh(&pernet->lock); |
| 1639 | return ret; |
| 1640 | } |
| 1641 | |
| 1642 | static int |
| 1643 | mptcp_nl_cmd_get_limits(struct sk_buff *skb, struct genl_info *info) |
| 1644 | { |
| 1645 | struct pm_nl_pernet *pernet = genl_info_pm_nl(info); |
| 1646 | struct sk_buff *msg; |
| 1647 | void *reply; |
| 1648 | |
| 1649 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1650 | if (!msg) |
| 1651 | return -ENOMEM; |
| 1652 | |
| 1653 | reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0, |
| 1654 | MPTCP_PM_CMD_GET_LIMITS); |
| 1655 | if (!reply) |
| 1656 | goto fail; |
| 1657 | |
| 1658 | if (nla_put_u32(msg, MPTCP_PM_ATTR_RCV_ADD_ADDRS, |
| 1659 | READ_ONCE(pernet->add_addr_accept_max))) |
| 1660 | goto fail; |
| 1661 | |
| 1662 | if (nla_put_u32(msg, MPTCP_PM_ATTR_SUBFLOWS, |
| 1663 | READ_ONCE(pernet->subflows_max))) |
| 1664 | goto fail; |
| 1665 | |
| 1666 | genlmsg_end(msg, reply); |
| 1667 | return genlmsg_reply(msg, info); |
| 1668 | |
| 1669 | fail: |
| 1670 | GENL_SET_ERR_MSG(info, "not enough space in Netlink message"); |
| 1671 | nlmsg_free(msg); |
| 1672 | return -EMSGSIZE; |
| 1673 | } |
| 1674 | |
Geliang Tang | 0f9f696 | 2021-01-08 16:47:59 -0800 | [diff] [blame] | 1675 | static int mptcp_nl_addr_backup(struct net *net, |
| 1676 | struct mptcp_addr_info *addr, |
| 1677 | u8 bkup) |
| 1678 | { |
| 1679 | long s_slot = 0, s_num = 0; |
| 1680 | struct mptcp_sock *msk; |
| 1681 | int ret = -EINVAL; |
| 1682 | |
| 1683 | while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) { |
| 1684 | struct sock *sk = (struct sock *)msk; |
| 1685 | |
| 1686 | if (list_empty(&msk->conn_list)) |
| 1687 | goto next; |
| 1688 | |
| 1689 | lock_sock(sk); |
| 1690 | spin_lock_bh(&msk->pm.lock); |
| 1691 | ret = mptcp_pm_nl_mp_prio_send_ack(msk, addr, bkup); |
| 1692 | spin_unlock_bh(&msk->pm.lock); |
| 1693 | release_sock(sk); |
| 1694 | |
| 1695 | next: |
| 1696 | sock_put(sk); |
| 1697 | cond_resched(); |
| 1698 | } |
| 1699 | |
| 1700 | return ret; |
| 1701 | } |
| 1702 | |
| 1703 | static int mptcp_nl_cmd_set_flags(struct sk_buff *skb, struct genl_info *info) |
| 1704 | { |
Davide Caratti | 602837e | 2021-12-03 14:35:36 -0800 | [diff] [blame^] | 1705 | struct mptcp_pm_addr_entry addr = { .addr = { .family = AF_UNSPEC }, }, *entry; |
Geliang Tang | 0f9f696 | 2021-01-08 16:47:59 -0800 | [diff] [blame] | 1706 | struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR]; |
| 1707 | struct pm_nl_pernet *pernet = genl_info_pm_nl(info); |
Geliang Tang | 0f9f696 | 2021-01-08 16:47:59 -0800 | [diff] [blame] | 1708 | struct net *net = sock_net(skb->sk); |
Davide Caratti | 602837e | 2021-12-03 14:35:36 -0800 | [diff] [blame^] | 1709 | u8 bkup = 0, lookup_by_id = 0; |
Geliang Tang | 0f9f696 | 2021-01-08 16:47:59 -0800 | [diff] [blame] | 1710 | int ret; |
| 1711 | |
Davide Caratti | 602837e | 2021-12-03 14:35:36 -0800 | [diff] [blame^] | 1712 | ret = mptcp_pm_parse_addr(attr, info, false, &addr); |
Geliang Tang | 0f9f696 | 2021-01-08 16:47:59 -0800 | [diff] [blame] | 1713 | if (ret < 0) |
| 1714 | return ret; |
| 1715 | |
Geliang Tang | daa83ab | 2021-04-06 17:15:57 -0700 | [diff] [blame] | 1716 | if (addr.flags & MPTCP_PM_ADDR_FLAG_BACKUP) |
Geliang Tang | 0f9f696 | 2021-01-08 16:47:59 -0800 | [diff] [blame] | 1717 | bkup = 1; |
Davide Caratti | 602837e | 2021-12-03 14:35:36 -0800 | [diff] [blame^] | 1718 | if (addr.addr.family == AF_UNSPEC) { |
| 1719 | lookup_by_id = 1; |
| 1720 | if (!addr.addr.id) |
| 1721 | return -EOPNOTSUPP; |
| 1722 | } |
Geliang Tang | 0f9f696 | 2021-01-08 16:47:59 -0800 | [diff] [blame] | 1723 | |
| 1724 | list_for_each_entry(entry, &pernet->local_addr_list, list) { |
Davide Caratti | 602837e | 2021-12-03 14:35:36 -0800 | [diff] [blame^] | 1725 | if ((!lookup_by_id && addresses_equal(&entry->addr, &addr.addr, true)) || |
| 1726 | (lookup_by_id && entry->addr.id == addr.addr.id)) { |
Davide Caratti | 3f4a089 | 2021-09-23 17:04:12 -0700 | [diff] [blame] | 1727 | mptcp_nl_addr_backup(net, &entry->addr, bkup); |
Geliang Tang | 0f9f696 | 2021-01-08 16:47:59 -0800 | [diff] [blame] | 1728 | |
| 1729 | if (bkup) |
Geliang Tang | daa83ab | 2021-04-06 17:15:57 -0700 | [diff] [blame] | 1730 | entry->flags |= MPTCP_PM_ADDR_FLAG_BACKUP; |
Geliang Tang | 0f9f696 | 2021-01-08 16:47:59 -0800 | [diff] [blame] | 1731 | else |
Geliang Tang | daa83ab | 2021-04-06 17:15:57 -0700 | [diff] [blame] | 1732 | entry->flags &= ~MPTCP_PM_ADDR_FLAG_BACKUP; |
Geliang Tang | 0f9f696 | 2021-01-08 16:47:59 -0800 | [diff] [blame] | 1733 | } |
| 1734 | } |
| 1735 | |
| 1736 | return 0; |
| 1737 | } |
| 1738 | |
Florian Westphal | b911c97 | 2021-02-12 16:00:01 -0800 | [diff] [blame] | 1739 | static void mptcp_nl_mcast_send(struct net *net, struct sk_buff *nlskb, gfp_t gfp) |
| 1740 | { |
| 1741 | genlmsg_multicast_netns(&mptcp_genl_family, net, |
| 1742 | nlskb, 0, MPTCP_PM_EV_GRP_OFFSET, gfp); |
| 1743 | } |
| 1744 | |
| 1745 | static int mptcp_event_add_subflow(struct sk_buff *skb, const struct sock *ssk) |
| 1746 | { |
| 1747 | const struct inet_sock *issk = inet_sk(ssk); |
| 1748 | const struct mptcp_subflow_context *sf; |
| 1749 | |
| 1750 | if (nla_put_u16(skb, MPTCP_ATTR_FAMILY, ssk->sk_family)) |
| 1751 | return -EMSGSIZE; |
| 1752 | |
| 1753 | switch (ssk->sk_family) { |
| 1754 | case AF_INET: |
| 1755 | if (nla_put_in_addr(skb, MPTCP_ATTR_SADDR4, issk->inet_saddr)) |
| 1756 | return -EMSGSIZE; |
| 1757 | if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, issk->inet_daddr)) |
| 1758 | return -EMSGSIZE; |
| 1759 | break; |
| 1760 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 1761 | case AF_INET6: { |
| 1762 | const struct ipv6_pinfo *np = inet6_sk(ssk); |
| 1763 | |
| 1764 | if (nla_put_in6_addr(skb, MPTCP_ATTR_SADDR6, &np->saddr)) |
| 1765 | return -EMSGSIZE; |
| 1766 | if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &ssk->sk_v6_daddr)) |
| 1767 | return -EMSGSIZE; |
| 1768 | break; |
| 1769 | } |
| 1770 | #endif |
| 1771 | default: |
| 1772 | WARN_ON_ONCE(1); |
| 1773 | return -EMSGSIZE; |
| 1774 | } |
| 1775 | |
| 1776 | if (nla_put_be16(skb, MPTCP_ATTR_SPORT, issk->inet_sport)) |
| 1777 | return -EMSGSIZE; |
| 1778 | if (nla_put_be16(skb, MPTCP_ATTR_DPORT, issk->inet_dport)) |
| 1779 | return -EMSGSIZE; |
| 1780 | |
| 1781 | sf = mptcp_subflow_ctx(ssk); |
| 1782 | if (WARN_ON_ONCE(!sf)) |
| 1783 | return -EINVAL; |
| 1784 | |
| 1785 | if (nla_put_u8(skb, MPTCP_ATTR_LOC_ID, sf->local_id)) |
| 1786 | return -EMSGSIZE; |
| 1787 | |
| 1788 | if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, sf->remote_id)) |
| 1789 | return -EMSGSIZE; |
| 1790 | |
| 1791 | return 0; |
| 1792 | } |
| 1793 | |
| 1794 | static int mptcp_event_put_token_and_ssk(struct sk_buff *skb, |
| 1795 | const struct mptcp_sock *msk, |
| 1796 | const struct sock *ssk) |
| 1797 | { |
| 1798 | const struct sock *sk = (const struct sock *)msk; |
| 1799 | const struct mptcp_subflow_context *sf; |
| 1800 | u8 sk_err; |
| 1801 | |
| 1802 | if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token)) |
| 1803 | return -EMSGSIZE; |
| 1804 | |
| 1805 | if (mptcp_event_add_subflow(skb, ssk)) |
| 1806 | return -EMSGSIZE; |
| 1807 | |
| 1808 | sf = mptcp_subflow_ctx(ssk); |
| 1809 | if (WARN_ON_ONCE(!sf)) |
| 1810 | return -EINVAL; |
| 1811 | |
| 1812 | if (nla_put_u8(skb, MPTCP_ATTR_BACKUP, sf->backup)) |
| 1813 | return -EMSGSIZE; |
| 1814 | |
| 1815 | if (ssk->sk_bound_dev_if && |
| 1816 | nla_put_s32(skb, MPTCP_ATTR_IF_IDX, ssk->sk_bound_dev_if)) |
| 1817 | return -EMSGSIZE; |
| 1818 | |
| 1819 | sk_err = ssk->sk_err; |
| 1820 | if (sk_err && sk->sk_state == TCP_ESTABLISHED && |
| 1821 | nla_put_u8(skb, MPTCP_ATTR_ERROR, sk_err)) |
| 1822 | return -EMSGSIZE; |
| 1823 | |
| 1824 | return 0; |
| 1825 | } |
| 1826 | |
| 1827 | static int mptcp_event_sub_established(struct sk_buff *skb, |
| 1828 | const struct mptcp_sock *msk, |
| 1829 | const struct sock *ssk) |
| 1830 | { |
| 1831 | return mptcp_event_put_token_and_ssk(skb, msk, ssk); |
| 1832 | } |
| 1833 | |
| 1834 | static int mptcp_event_sub_closed(struct sk_buff *skb, |
| 1835 | const struct mptcp_sock *msk, |
| 1836 | const struct sock *ssk) |
| 1837 | { |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 1838 | const struct mptcp_subflow_context *sf; |
| 1839 | |
Florian Westphal | b911c97 | 2021-02-12 16:00:01 -0800 | [diff] [blame] | 1840 | if (mptcp_event_put_token_and_ssk(skb, msk, ssk)) |
| 1841 | return -EMSGSIZE; |
| 1842 | |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 1843 | sf = mptcp_subflow_ctx(ssk); |
| 1844 | if (!sf->reset_seen) |
| 1845 | return 0; |
| 1846 | |
| 1847 | if (nla_put_u32(skb, MPTCP_ATTR_RESET_REASON, sf->reset_reason)) |
| 1848 | return -EMSGSIZE; |
| 1849 | |
| 1850 | if (nla_put_u32(skb, MPTCP_ATTR_RESET_FLAGS, sf->reset_transient)) |
| 1851 | return -EMSGSIZE; |
| 1852 | |
Florian Westphal | b911c97 | 2021-02-12 16:00:01 -0800 | [diff] [blame] | 1853 | return 0; |
| 1854 | } |
| 1855 | |
| 1856 | static int mptcp_event_created(struct sk_buff *skb, |
| 1857 | const struct mptcp_sock *msk, |
| 1858 | const struct sock *ssk) |
| 1859 | { |
| 1860 | int err = nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token); |
| 1861 | |
| 1862 | if (err) |
| 1863 | return err; |
| 1864 | |
| 1865 | return mptcp_event_add_subflow(skb, ssk); |
| 1866 | } |
| 1867 | |
| 1868 | void mptcp_event_addr_removed(const struct mptcp_sock *msk, uint8_t id) |
| 1869 | { |
| 1870 | struct net *net = sock_net((const struct sock *)msk); |
| 1871 | struct nlmsghdr *nlh; |
| 1872 | struct sk_buff *skb; |
| 1873 | |
| 1874 | if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET)) |
| 1875 | return; |
| 1876 | |
| 1877 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
| 1878 | if (!skb) |
| 1879 | return; |
| 1880 | |
| 1881 | nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, MPTCP_EVENT_REMOVED); |
| 1882 | if (!nlh) |
| 1883 | goto nla_put_failure; |
| 1884 | |
| 1885 | if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token)) |
| 1886 | goto nla_put_failure; |
| 1887 | |
| 1888 | if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, id)) |
| 1889 | goto nla_put_failure; |
| 1890 | |
| 1891 | genlmsg_end(skb, nlh); |
| 1892 | mptcp_nl_mcast_send(net, skb, GFP_ATOMIC); |
| 1893 | return; |
| 1894 | |
| 1895 | nla_put_failure: |
| 1896 | kfree_skb(skb); |
| 1897 | } |
| 1898 | |
| 1899 | void mptcp_event_addr_announced(const struct mptcp_sock *msk, |
| 1900 | const struct mptcp_addr_info *info) |
| 1901 | { |
| 1902 | struct net *net = sock_net((const struct sock *)msk); |
| 1903 | struct nlmsghdr *nlh; |
| 1904 | struct sk_buff *skb; |
| 1905 | |
| 1906 | if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET)) |
| 1907 | return; |
| 1908 | |
| 1909 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
| 1910 | if (!skb) |
| 1911 | return; |
| 1912 | |
| 1913 | nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, |
| 1914 | MPTCP_EVENT_ANNOUNCED); |
| 1915 | if (!nlh) |
| 1916 | goto nla_put_failure; |
| 1917 | |
| 1918 | if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token)) |
| 1919 | goto nla_put_failure; |
| 1920 | |
| 1921 | if (nla_put_u8(skb, MPTCP_ATTR_REM_ID, info->id)) |
| 1922 | goto nla_put_failure; |
| 1923 | |
| 1924 | if (nla_put_be16(skb, MPTCP_ATTR_DPORT, info->port)) |
| 1925 | goto nla_put_failure; |
| 1926 | |
| 1927 | switch (info->family) { |
| 1928 | case AF_INET: |
| 1929 | if (nla_put_in_addr(skb, MPTCP_ATTR_DADDR4, info->addr.s_addr)) |
| 1930 | goto nla_put_failure; |
| 1931 | break; |
| 1932 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 1933 | case AF_INET6: |
| 1934 | if (nla_put_in6_addr(skb, MPTCP_ATTR_DADDR6, &info->addr6)) |
| 1935 | goto nla_put_failure; |
| 1936 | break; |
| 1937 | #endif |
| 1938 | default: |
| 1939 | WARN_ON_ONCE(1); |
| 1940 | goto nla_put_failure; |
| 1941 | } |
| 1942 | |
| 1943 | genlmsg_end(skb, nlh); |
| 1944 | mptcp_nl_mcast_send(net, skb, GFP_ATOMIC); |
| 1945 | return; |
| 1946 | |
| 1947 | nla_put_failure: |
| 1948 | kfree_skb(skb); |
| 1949 | } |
| 1950 | |
| 1951 | void mptcp_event(enum mptcp_event_type type, const struct mptcp_sock *msk, |
| 1952 | const struct sock *ssk, gfp_t gfp) |
| 1953 | { |
| 1954 | struct net *net = sock_net((const struct sock *)msk); |
| 1955 | struct nlmsghdr *nlh; |
| 1956 | struct sk_buff *skb; |
| 1957 | |
| 1958 | if (!genl_has_listeners(&mptcp_genl_family, net, MPTCP_PM_EV_GRP_OFFSET)) |
| 1959 | return; |
| 1960 | |
| 1961 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp); |
| 1962 | if (!skb) |
| 1963 | return; |
| 1964 | |
| 1965 | nlh = genlmsg_put(skb, 0, 0, &mptcp_genl_family, 0, type); |
| 1966 | if (!nlh) |
| 1967 | goto nla_put_failure; |
| 1968 | |
| 1969 | switch (type) { |
| 1970 | case MPTCP_EVENT_UNSPEC: |
| 1971 | WARN_ON_ONCE(1); |
| 1972 | break; |
| 1973 | case MPTCP_EVENT_CREATED: |
| 1974 | case MPTCP_EVENT_ESTABLISHED: |
| 1975 | if (mptcp_event_created(skb, msk, ssk) < 0) |
| 1976 | goto nla_put_failure; |
| 1977 | break; |
| 1978 | case MPTCP_EVENT_CLOSED: |
| 1979 | if (nla_put_u32(skb, MPTCP_ATTR_TOKEN, msk->token) < 0) |
| 1980 | goto nla_put_failure; |
| 1981 | break; |
| 1982 | case MPTCP_EVENT_ANNOUNCED: |
| 1983 | case MPTCP_EVENT_REMOVED: |
| 1984 | /* call mptcp_event_addr_announced()/removed instead */ |
| 1985 | WARN_ON_ONCE(1); |
| 1986 | break; |
| 1987 | case MPTCP_EVENT_SUB_ESTABLISHED: |
| 1988 | case MPTCP_EVENT_SUB_PRIORITY: |
| 1989 | if (mptcp_event_sub_established(skb, msk, ssk) < 0) |
| 1990 | goto nla_put_failure; |
| 1991 | break; |
| 1992 | case MPTCP_EVENT_SUB_CLOSED: |
| 1993 | if (mptcp_event_sub_closed(skb, msk, ssk) < 0) |
| 1994 | goto nla_put_failure; |
| 1995 | break; |
| 1996 | } |
| 1997 | |
| 1998 | genlmsg_end(skb, nlh); |
| 1999 | mptcp_nl_mcast_send(net, skb, gfp); |
| 2000 | return; |
| 2001 | |
| 2002 | nla_put_failure: |
| 2003 | kfree_skb(skb); |
| 2004 | } |
| 2005 | |
Rikard Falkeborn | 674d3ab | 2020-10-05 01:44:16 +0200 | [diff] [blame] | 2006 | static const struct genl_small_ops mptcp_pm_ops[] = { |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 2007 | { |
| 2008 | .cmd = MPTCP_PM_CMD_ADD_ADDR, |
| 2009 | .doit = mptcp_nl_cmd_add_addr, |
| 2010 | .flags = GENL_ADMIN_PERM, |
| 2011 | }, |
| 2012 | { |
| 2013 | .cmd = MPTCP_PM_CMD_DEL_ADDR, |
| 2014 | .doit = mptcp_nl_cmd_del_addr, |
| 2015 | .flags = GENL_ADMIN_PERM, |
| 2016 | }, |
| 2017 | { |
| 2018 | .cmd = MPTCP_PM_CMD_FLUSH_ADDRS, |
| 2019 | .doit = mptcp_nl_cmd_flush_addrs, |
| 2020 | .flags = GENL_ADMIN_PERM, |
| 2021 | }, |
| 2022 | { |
| 2023 | .cmd = MPTCP_PM_CMD_GET_ADDR, |
| 2024 | .doit = mptcp_nl_cmd_get_addr, |
| 2025 | .dumpit = mptcp_nl_cmd_dump_addrs, |
| 2026 | }, |
| 2027 | { |
| 2028 | .cmd = MPTCP_PM_CMD_SET_LIMITS, |
| 2029 | .doit = mptcp_nl_cmd_set_limits, |
| 2030 | .flags = GENL_ADMIN_PERM, |
| 2031 | }, |
| 2032 | { |
| 2033 | .cmd = MPTCP_PM_CMD_GET_LIMITS, |
| 2034 | .doit = mptcp_nl_cmd_get_limits, |
| 2035 | }, |
Geliang Tang | 0f9f696 | 2021-01-08 16:47:59 -0800 | [diff] [blame] | 2036 | { |
| 2037 | .cmd = MPTCP_PM_CMD_SET_FLAGS, |
| 2038 | .doit = mptcp_nl_cmd_set_flags, |
| 2039 | .flags = GENL_ADMIN_PERM, |
| 2040 | }, |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 2041 | }; |
| 2042 | |
| 2043 | static struct genl_family mptcp_genl_family __ro_after_init = { |
| 2044 | .name = MPTCP_PM_NAME, |
| 2045 | .version = MPTCP_PM_VER, |
| 2046 | .maxattr = MPTCP_PM_ATTR_MAX, |
| 2047 | .policy = mptcp_pm_policy, |
| 2048 | .netnsok = true, |
| 2049 | .module = THIS_MODULE, |
Jakub Kicinski | 66a9b92 | 2020-10-02 14:49:54 -0700 | [diff] [blame] | 2050 | .small_ops = mptcp_pm_ops, |
| 2051 | .n_small_ops = ARRAY_SIZE(mptcp_pm_ops), |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 2052 | .mcgrps = mptcp_pm_mcgrps, |
| 2053 | .n_mcgrps = ARRAY_SIZE(mptcp_pm_mcgrps), |
| 2054 | }; |
| 2055 | |
| 2056 | static int __net_init pm_nl_init_net(struct net *net) |
| 2057 | { |
| 2058 | struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id); |
| 2059 | |
| 2060 | INIT_LIST_HEAD_RCU(&pernet->local_addr_list); |
Paolo Abeni | 72bcbc4 | 2021-10-15 16:05:51 -0700 | [diff] [blame] | 2061 | |
| 2062 | /* Cit. 2 subflows ought to be enough for anybody. */ |
| 2063 | pernet->subflows_max = 2; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 2064 | pernet->next_id = 1; |
Paolo Abeni | ff5a0b4 | 2021-08-13 15:15:45 -0700 | [diff] [blame] | 2065 | pernet->stale_loss_cnt = 4; |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 2066 | spin_lock_init(&pernet->lock); |
Jianguo Wu | ae51498 | 2021-05-27 16:54:27 -0700 | [diff] [blame] | 2067 | |
| 2068 | /* No need to initialize other pernet fields, the struct is zeroed at |
| 2069 | * allocation time. |
| 2070 | */ |
| 2071 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 2072 | return 0; |
| 2073 | } |
| 2074 | |
| 2075 | static void __net_exit pm_nl_exit_net(struct list_head *net_list) |
| 2076 | { |
| 2077 | struct net *net; |
| 2078 | |
| 2079 | list_for_each_entry(net, net_list, exit_list) { |
Geliang Tang | 141694d | 2020-12-10 14:24:59 -0800 | [diff] [blame] | 2080 | struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id); |
| 2081 | |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 2082 | /* net is removed from namespace list, can't race with |
Paolo Abeni | a0eea5f | 2021-08-18 16:42:36 -0700 | [diff] [blame] | 2083 | * other modifiers, also netns core already waited for a |
| 2084 | * RCU grace period. |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 2085 | */ |
Geliang Tang | 0e4a3e6 | 2021-03-12 17:16:18 -0800 | [diff] [blame] | 2086 | __flush_addrs(&pernet->local_addr_list); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 2087 | } |
| 2088 | } |
| 2089 | |
| 2090 | static struct pernet_operations mptcp_pm_pernet_ops = { |
| 2091 | .init = pm_nl_init_net, |
| 2092 | .exit_batch = pm_nl_exit_net, |
| 2093 | .id = &pm_nl_pernet_id, |
| 2094 | .size = sizeof(struct pm_nl_pernet), |
| 2095 | }; |
| 2096 | |
Paolo Abeni | d39dcec | 2020-06-26 19:29:59 +0200 | [diff] [blame] | 2097 | void __init mptcp_pm_nl_init(void) |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 2098 | { |
| 2099 | if (register_pernet_subsys(&mptcp_pm_pernet_ops) < 0) |
| 2100 | panic("Failed to register MPTCP PM pernet subsystem.\n"); |
| 2101 | |
| 2102 | if (genl_register_family(&mptcp_genl_family)) |
| 2103 | panic("Failed to register MPTCP PM netlink family\n"); |
| 2104 | } |