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