1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2018 Facebook 4 */ 5 #include <linux/bpf.h> 6 #include <linux/err.h> 7 #include <linux/sock_diag.h> 8 #include <net/sock_reuseport.h> 9 10 struct reuseport_array { 11 struct bpf_map map; 12 struct sock __rcu *ptrs[]; 13 }; 14 15 static struct reuseport_array *reuseport_array(struct bpf_map *map) 16 { 17 return (struct reuseport_array *)map; 18 } 19 20 /* The caller must hold the reuseport_lock */ 21 void bpf_sk_reuseport_detach(struct sock *sk) 22 { 23 uintptr_t sk_user_data; 24 25 write_lock_bh(&sk->sk_callback_lock); 26 sk_user_data = (uintptr_t)sk->sk_user_data; 27 if (sk_user_data & SK_USER_DATA_BPF) { 28 struct sock __rcu **socks; 29 30 socks = (void *)(sk_user_data & SK_USER_DATA_PTRMASK); 31 WRITE_ONCE(sk->sk_user_data, NULL); 32 /* 33 * Do not move this NULL assignment outside of 34 * sk->sk_callback_lock because there is 35 * a race with reuseport_array_free() 36 * which does not hold the reuseport_lock. 37 */ 38 RCU_INIT_POINTER(*socks, NULL); 39 } 40 write_unlock_bh(&sk->sk_callback_lock); 41 } 42 43 static int reuseport_array_alloc_check(union bpf_attr *attr) 44 { 45 if (attr->value_size != sizeof(u32) && 46 attr->value_size != sizeof(u64)) 47 return -EINVAL; 48 49 return array_map_alloc_check(attr); 50 } 51 52 static void *reuseport_array_lookup_elem(struct bpf_map *map, void *key) 53 { 54 struct reuseport_array *array = reuseport_array(map); 55 u32 index = *(u32 *)key; 56 57 if (unlikely(index >= array->map.max_entries)) 58 return NULL; 59 60 return rcu_dereference(array->ptrs[index]); 61 } 62 63 /* Called from syscall only */ 64 static int reuseport_array_delete_elem(struct bpf_map *map, void *key) 65 { 66 struct reuseport_array *array = reuseport_array(map); 67 u32 index = *(u32 *)key; 68 struct sock *sk; 69 int err; 70 71 if (index >= map->max_entries) 72 return -E2BIG; 73 74 if (!rcu_access_pointer(array->ptrs[index])) 75 return -ENOENT; 76 77 spin_lock_bh(&reuseport_lock); 78 79 sk = rcu_dereference_protected(array->ptrs[index], 80 lockdep_is_held(&reuseport_lock)); 81 if (sk) { 82 write_lock_bh(&sk->sk_callback_lock); 83 WRITE_ONCE(sk->sk_user_data, NULL); 84 RCU_INIT_POINTER(array->ptrs[index], NULL); 85 write_unlock_bh(&sk->sk_callback_lock); 86 err = 0; 87 } else { 88 err = -ENOENT; 89 } 90 91 spin_unlock_bh(&reuseport_lock); 92 93 return err; 94 } 95 96 static void reuseport_array_free(struct bpf_map *map) 97 { 98 struct reuseport_array *array = reuseport_array(map); 99 struct sock *sk; 100 u32 i; 101 102 synchronize_rcu(); 103 104 /* 105 * ops->map_*_elem() will not be able to access this 106 * array now. Hence, this function only races with 107 * bpf_sk_reuseport_detach() which was triggerred by 108 * close() or disconnect(). 109 * 110 * This function and bpf_sk_reuseport_detach() are 111 * both removing sk from "array". Who removes it 112 * first does not matter. 113 * 114 * The only concern here is bpf_sk_reuseport_detach() 115 * may access "array" which is being freed here. 116 * bpf_sk_reuseport_detach() access this "array" 117 * through sk->sk_user_data _and_ with sk->sk_callback_lock 118 * held which is enough because this "array" is not freed 119 * until all sk->sk_user_data has stopped referencing this "array". 120 * 121 * Hence, due to the above, taking "reuseport_lock" is not 122 * needed here. 123 */ 124 125 /* 126 * Since reuseport_lock is not taken, sk is accessed under 127 * rcu_read_lock() 128 */ 129 rcu_read_lock(); 130 for (i = 0; i < map->max_entries; i++) { 131 sk = rcu_dereference(array->ptrs[i]); 132 if (sk) { 133 write_lock_bh(&sk->sk_callback_lock); 134 /* 135 * No need for WRITE_ONCE(). At this point, 136 * no one is reading it without taking the 137 * sk->sk_callback_lock. 138 */ 139 sk->sk_user_data = NULL; 140 write_unlock_bh(&sk->sk_callback_lock); 141 RCU_INIT_POINTER(array->ptrs[i], NULL); 142 } 143 } 144 rcu_read_unlock(); 145 146 /* 147 * Once reaching here, all sk->sk_user_data is not 148 * referenceing this "array". "array" can be freed now. 149 */ 150 bpf_map_area_free(array); 151 } 152 153 static struct bpf_map *reuseport_array_alloc(union bpf_attr *attr) 154 { 155 int err, numa_node = bpf_map_attr_numa_node(attr); 156 struct reuseport_array *array; 157 struct bpf_map_memory mem; 158 u64 array_size; 159 160 if (!bpf_capable()) 161 return ERR_PTR(-EPERM); 162 163 array_size = sizeof(*array); 164 array_size += (u64)attr->max_entries * sizeof(struct sock *); 165 166 err = bpf_map_charge_init(&mem, array_size); 167 if (err) 168 return ERR_PTR(err); 169 170 /* allocate all map elements and zero-initialize them */ 171 array = bpf_map_area_alloc(array_size, numa_node); 172 if (!array) { 173 bpf_map_charge_finish(&mem); 174 return ERR_PTR(-ENOMEM); 175 } 176 177 /* copy mandatory map attributes */ 178 bpf_map_init_from_attr(&array->map, attr); 179 bpf_map_charge_move(&array->map.memory, &mem); 180 181 return &array->map; 182 } 183 184 int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key, 185 void *value) 186 { 187 struct sock *sk; 188 int err; 189 190 if (map->value_size != sizeof(u64)) 191 return -ENOSPC; 192 193 rcu_read_lock(); 194 sk = reuseport_array_lookup_elem(map, key); 195 if (sk) { 196 *(u64 *)value = sock_gen_cookie(sk); 197 err = 0; 198 } else { 199 err = -ENOENT; 200 } 201 rcu_read_unlock(); 202 203 return err; 204 } 205 206 static int 207 reuseport_array_update_check(const struct reuseport_array *array, 208 const struct sock *nsk, 209 const struct sock *osk, 210 const struct sock_reuseport *nsk_reuse, 211 u32 map_flags) 212 { 213 if (osk && map_flags == BPF_NOEXIST) 214 return -EEXIST; 215 216 if (!osk && map_flags == BPF_EXIST) 217 return -ENOENT; 218 219 if (nsk->sk_protocol != IPPROTO_UDP && nsk->sk_protocol != IPPROTO_TCP) 220 return -ENOTSUPP; 221 222 if (nsk->sk_family != AF_INET && nsk->sk_family != AF_INET6) 223 return -ENOTSUPP; 224 225 if (nsk->sk_type != SOCK_STREAM && nsk->sk_type != SOCK_DGRAM) 226 return -ENOTSUPP; 227 228 /* 229 * sk must be hashed (i.e. listening in the TCP case or binded 230 * in the UDP case) and 231 * it must also be a SO_REUSEPORT sk (i.e. reuse cannot be NULL). 232 * 233 * Also, sk will be used in bpf helper that is protected by 234 * rcu_read_lock(). 235 */ 236 if (!sock_flag(nsk, SOCK_RCU_FREE) || !sk_hashed(nsk) || !nsk_reuse) 237 return -EINVAL; 238 239 /* READ_ONCE because the sk->sk_callback_lock may not be held here */ 240 if (READ_ONCE(nsk->sk_user_data)) 241 return -EBUSY; 242 243 return 0; 244 } 245 246 /* 247 * Called from syscall only. 248 * The "nsk" in the fd refcnt. 249 * The "osk" and "reuse" are protected by reuseport_lock. 250 */ 251 int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key, 252 void *value, u64 map_flags) 253 { 254 struct reuseport_array *array = reuseport_array(map); 255 struct sock *free_osk = NULL, *osk, *nsk; 256 struct sock_reuseport *reuse; 257 u32 index = *(u32 *)key; 258 uintptr_t sk_user_data; 259 struct socket *socket; 260 int err, fd; 261 262 if (map_flags > BPF_EXIST) 263 return -EINVAL; 264 265 if (index >= map->max_entries) 266 return -E2BIG; 267 268 if (map->value_size == sizeof(u64)) { 269 u64 fd64 = *(u64 *)value; 270 271 if (fd64 > S32_MAX) 272 return -EINVAL; 273 fd = fd64; 274 } else { 275 fd = *(int *)value; 276 } 277 278 socket = sockfd_lookup(fd, &err); 279 if (!socket) 280 return err; 281 282 nsk = socket->sk; 283 if (!nsk) { 284 err = -EINVAL; 285 goto put_file; 286 } 287 288 /* Quick checks before taking reuseport_lock */ 289 err = reuseport_array_update_check(array, nsk, 290 rcu_access_pointer(array->ptrs[index]), 291 rcu_access_pointer(nsk->sk_reuseport_cb), 292 map_flags); 293 if (err) 294 goto put_file; 295 296 spin_lock_bh(&reuseport_lock); 297 /* 298 * Some of the checks only need reuseport_lock 299 * but it is done under sk_callback_lock also 300 * for simplicity reason. 301 */ 302 write_lock_bh(&nsk->sk_callback_lock); 303 304 osk = rcu_dereference_protected(array->ptrs[index], 305 lockdep_is_held(&reuseport_lock)); 306 reuse = rcu_dereference_protected(nsk->sk_reuseport_cb, 307 lockdep_is_held(&reuseport_lock)); 308 err = reuseport_array_update_check(array, nsk, osk, reuse, map_flags); 309 if (err) 310 goto put_file_unlock; 311 312 sk_user_data = (uintptr_t)&array->ptrs[index] | SK_USER_DATA_NOCOPY | 313 SK_USER_DATA_BPF; 314 WRITE_ONCE(nsk->sk_user_data, (void *)sk_user_data); 315 rcu_assign_pointer(array->ptrs[index], nsk); 316 free_osk = osk; 317 err = 0; 318 319 put_file_unlock: 320 write_unlock_bh(&nsk->sk_callback_lock); 321 322 if (free_osk) { 323 write_lock_bh(&free_osk->sk_callback_lock); 324 WRITE_ONCE(free_osk->sk_user_data, NULL); 325 write_unlock_bh(&free_osk->sk_callback_lock); 326 } 327 328 spin_unlock_bh(&reuseport_lock); 329 put_file: 330 fput(socket->file); 331 return err; 332 } 333 334 /* Called from syscall */ 335 static int reuseport_array_get_next_key(struct bpf_map *map, void *key, 336 void *next_key) 337 { 338 struct reuseport_array *array = reuseport_array(map); 339 u32 index = key ? *(u32 *)key : U32_MAX; 340 u32 *next = (u32 *)next_key; 341 342 if (index >= array->map.max_entries) { 343 *next = 0; 344 return 0; 345 } 346 347 if (index == array->map.max_entries - 1) 348 return -ENOENT; 349 350 *next = index + 1; 351 return 0; 352 } 353 354 const struct bpf_map_ops reuseport_array_ops = { 355 .map_alloc_check = reuseport_array_alloc_check, 356 .map_alloc = reuseport_array_alloc, 357 .map_free = reuseport_array_free, 358 .map_lookup_elem = reuseport_array_lookup_elem, 359 .map_get_next_key = reuseport_array_get_next_key, 360 .map_delete_elem = reuseport_array_delete_elem, 361 }; 362