1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2018 Facebook
7 #include <linux/sock_diag.h>
8 #include <net/sock_reuseport.h>
10 struct reuseport_array
{
12 struct sock __rcu
*ptrs
[];
15 static struct reuseport_array
*reuseport_array(struct bpf_map
*map
)
17 return (struct reuseport_array
*)map
;
20 /* The caller must hold the reuseport_lock */
21 void bpf_sk_reuseport_detach(struct sock
*sk
)
23 struct sock __rcu
**socks
;
25 write_lock_bh(&sk
->sk_callback_lock
);
26 socks
= sk
->sk_user_data
;
28 WRITE_ONCE(sk
->sk_user_data
, NULL
);
30 * Do not move this NULL assignment outside of
31 * sk->sk_callback_lock because there is
32 * a race with reuseport_array_free()
33 * which does not hold the reuseport_lock.
35 RCU_INIT_POINTER(*socks
, NULL
);
37 write_unlock_bh(&sk
->sk_callback_lock
);
40 static int reuseport_array_alloc_check(union bpf_attr
*attr
)
42 if (attr
->value_size
!= sizeof(u32
) &&
43 attr
->value_size
!= sizeof(u64
))
46 return array_map_alloc_check(attr
);
49 static void *reuseport_array_lookup_elem(struct bpf_map
*map
, void *key
)
51 struct reuseport_array
*array
= reuseport_array(map
);
52 u32 index
= *(u32
*)key
;
54 if (unlikely(index
>= array
->map
.max_entries
))
57 return rcu_dereference(array
->ptrs
[index
]);
60 /* Called from syscall only */
61 static int reuseport_array_delete_elem(struct bpf_map
*map
, void *key
)
63 struct reuseport_array
*array
= reuseport_array(map
);
64 u32 index
= *(u32
*)key
;
68 if (index
>= map
->max_entries
)
71 if (!rcu_access_pointer(array
->ptrs
[index
]))
74 spin_lock_bh(&reuseport_lock
);
76 sk
= rcu_dereference_protected(array
->ptrs
[index
],
77 lockdep_is_held(&reuseport_lock
));
79 write_lock_bh(&sk
->sk_callback_lock
);
80 WRITE_ONCE(sk
->sk_user_data
, NULL
);
81 RCU_INIT_POINTER(array
->ptrs
[index
], NULL
);
82 write_unlock_bh(&sk
->sk_callback_lock
);
88 spin_unlock_bh(&reuseport_lock
);
93 static void reuseport_array_free(struct bpf_map
*map
)
95 struct reuseport_array
*array
= reuseport_array(map
);
102 * ops->map_*_elem() will not be able to access this
103 * array now. Hence, this function only races with
104 * bpf_sk_reuseport_detach() which was triggerred by
105 * close() or disconnect().
107 * This function and bpf_sk_reuseport_detach() are
108 * both removing sk from "array". Who removes it
109 * first does not matter.
111 * The only concern here is bpf_sk_reuseport_detach()
112 * may access "array" which is being freed here.
113 * bpf_sk_reuseport_detach() access this "array"
114 * through sk->sk_user_data _and_ with sk->sk_callback_lock
115 * held which is enough because this "array" is not freed
116 * until all sk->sk_user_data has stopped referencing this "array".
118 * Hence, due to the above, taking "reuseport_lock" is not
123 * Since reuseport_lock is not taken, sk is accessed under
127 for (i
= 0; i
< map
->max_entries
; i
++) {
128 sk
= rcu_dereference(array
->ptrs
[i
]);
130 write_lock_bh(&sk
->sk_callback_lock
);
132 * No need for WRITE_ONCE(). At this point,
133 * no one is reading it without taking the
134 * sk->sk_callback_lock.
136 sk
->sk_user_data
= NULL
;
137 write_unlock_bh(&sk
->sk_callback_lock
);
138 RCU_INIT_POINTER(array
->ptrs
[i
], NULL
);
144 * Once reaching here, all sk->sk_user_data is not
145 * referenceing this "array". "array" can be freed now.
147 bpf_map_area_free(array
);
150 static struct bpf_map
*reuseport_array_alloc(union bpf_attr
*attr
)
152 int err
, numa_node
= bpf_map_attr_numa_node(attr
);
153 struct reuseport_array
*array
;
154 struct bpf_map_memory mem
;
157 if (!capable(CAP_SYS_ADMIN
))
158 return ERR_PTR(-EPERM
);
160 array_size
= sizeof(*array
);
161 array_size
+= (u64
)attr
->max_entries
* sizeof(struct sock
*);
163 err
= bpf_map_charge_init(&mem
, array_size
);
167 /* allocate all map elements and zero-initialize them */
168 array
= bpf_map_area_alloc(array_size
, numa_node
);
170 bpf_map_charge_finish(&mem
);
171 return ERR_PTR(-ENOMEM
);
174 /* copy mandatory map attributes */
175 bpf_map_init_from_attr(&array
->map
, attr
);
176 bpf_map_charge_move(&array
->map
.memory
, &mem
);
181 int bpf_fd_reuseport_array_lookup_elem(struct bpf_map
*map
, void *key
,
187 if (map
->value_size
!= sizeof(u64
))
191 sk
= reuseport_array_lookup_elem(map
, key
);
193 *(u64
*)value
= sock_gen_cookie(sk
);
204 reuseport_array_update_check(const struct reuseport_array
*array
,
205 const struct sock
*nsk
,
206 const struct sock
*osk
,
207 const struct sock_reuseport
*nsk_reuse
,
210 if (osk
&& map_flags
== BPF_NOEXIST
)
213 if (!osk
&& map_flags
== BPF_EXIST
)
216 if (nsk
->sk_protocol
!= IPPROTO_UDP
&& nsk
->sk_protocol
!= IPPROTO_TCP
)
219 if (nsk
->sk_family
!= AF_INET
&& nsk
->sk_family
!= AF_INET6
)
222 if (nsk
->sk_type
!= SOCK_STREAM
&& nsk
->sk_type
!= SOCK_DGRAM
)
226 * sk must be hashed (i.e. listening in the TCP case or binded
227 * in the UDP case) and
228 * it must also be a SO_REUSEPORT sk (i.e. reuse cannot be NULL).
230 * Also, sk will be used in bpf helper that is protected by
233 if (!sock_flag(nsk
, SOCK_RCU_FREE
) || !sk_hashed(nsk
) || !nsk_reuse
)
236 /* READ_ONCE because the sk->sk_callback_lock may not be held here */
237 if (READ_ONCE(nsk
->sk_user_data
))
244 * Called from syscall only.
245 * The "nsk" in the fd refcnt.
246 * The "osk" and "reuse" are protected by reuseport_lock.
248 int bpf_fd_reuseport_array_update_elem(struct bpf_map
*map
, void *key
,
249 void *value
, u64 map_flags
)
251 struct reuseport_array
*array
= reuseport_array(map
);
252 struct sock
*free_osk
= NULL
, *osk
, *nsk
;
253 struct sock_reuseport
*reuse
;
254 u32 index
= *(u32
*)key
;
255 struct socket
*socket
;
258 if (map_flags
> BPF_EXIST
)
261 if (index
>= map
->max_entries
)
264 if (map
->value_size
== sizeof(u64
)) {
265 u64 fd64
= *(u64
*)value
;
274 socket
= sockfd_lookup(fd
, &err
);
284 /* Quick checks before taking reuseport_lock */
285 err
= reuseport_array_update_check(array
, nsk
,
286 rcu_access_pointer(array
->ptrs
[index
]),
287 rcu_access_pointer(nsk
->sk_reuseport_cb
),
292 spin_lock_bh(&reuseport_lock
);
294 * Some of the checks only need reuseport_lock
295 * but it is done under sk_callback_lock also
296 * for simplicity reason.
298 write_lock_bh(&nsk
->sk_callback_lock
);
300 osk
= rcu_dereference_protected(array
->ptrs
[index
],
301 lockdep_is_held(&reuseport_lock
));
302 reuse
= rcu_dereference_protected(nsk
->sk_reuseport_cb
,
303 lockdep_is_held(&reuseport_lock
));
304 err
= reuseport_array_update_check(array
, nsk
, osk
, reuse
, map_flags
);
306 goto put_file_unlock
;
308 /* Ensure reuse->reuseport_id is set */
309 err
= reuseport_get_id(reuse
);
311 goto put_file_unlock
;
313 WRITE_ONCE(nsk
->sk_user_data
, &array
->ptrs
[index
]);
314 rcu_assign_pointer(array
->ptrs
[index
], nsk
);
319 write_unlock_bh(&nsk
->sk_callback_lock
);
322 write_lock_bh(&free_osk
->sk_callback_lock
);
323 WRITE_ONCE(free_osk
->sk_user_data
, NULL
);
324 write_unlock_bh(&free_osk
->sk_callback_lock
);
327 spin_unlock_bh(&reuseport_lock
);
333 /* Called from syscall */
334 static int reuseport_array_get_next_key(struct bpf_map
*map
, void *key
,
337 struct reuseport_array
*array
= reuseport_array(map
);
338 u32 index
= key
? *(u32
*)key
: U32_MAX
;
339 u32
*next
= (u32
*)next_key
;
341 if (index
>= array
->map
.max_entries
) {
346 if (index
== array
->map
.max_entries
- 1)
353 const struct bpf_map_ops reuseport_array_ops
= {
354 .map_alloc_check
= reuseport_array_alloc_check
,
355 .map_alloc
= reuseport_array_alloc
,
356 .map_free
= reuseport_array_free
,
357 .map_lookup_elem
= reuseport_array_lookup_elem
,
358 .map_get_next_key
= reuseport_array_get_next_key
,
359 .map_delete_elem
= reuseport_array_delete_elem
,