1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2017 - 2018 Covalent IO, Inc. http://covalent.io */
3
4 #include <linux/bpf.h>
5 #include <linux/btf_ids.h>
6 #include <linux/filter.h>
7 #include <linux/errno.h>
8 #include <linux/file.h>
9 #include <linux/net.h>
10 #include <linux/workqueue.h>
11 #include <linux/skmsg.h>
12 #include <linux/list.h>
13 #include <linux/jhash.h>
14 #include <linux/sock_diag.h>
15 #include <net/udp.h>
16
17 struct bpf_stab {
18 struct bpf_map map;
19 struct sock **sks;
20 struct sk_psock_progs progs;
21 raw_spinlock_t lock;
22 };
23
24 #define SOCK_CREATE_FLAG_MASK \
25 (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
26
27 static int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
28 struct bpf_prog *old, u32 which);
29 static struct sk_psock_progs *sock_map_progs(struct bpf_map *map);
30
sock_map_alloc(union bpf_attr * attr)31 static struct bpf_map *sock_map_alloc(union bpf_attr *attr)
32 {
33 struct bpf_stab *stab;
34
35 if (!capable(CAP_NET_ADMIN))
36 return ERR_PTR(-EPERM);
37 if (attr->max_entries == 0 ||
38 attr->key_size != 4 ||
39 (attr->value_size != sizeof(u32) &&
40 attr->value_size != sizeof(u64)) ||
41 attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
42 return ERR_PTR(-EINVAL);
43
44 stab = kzalloc(sizeof(*stab), GFP_USER | __GFP_ACCOUNT);
45 if (!stab)
46 return ERR_PTR(-ENOMEM);
47
48 bpf_map_init_from_attr(&stab->map, attr);
49 raw_spin_lock_init(&stab->lock);
50
51 stab->sks = bpf_map_area_alloc((u64) stab->map.max_entries *
52 sizeof(struct sock *),
53 stab->map.numa_node);
54 if (!stab->sks) {
55 kfree(stab);
56 return ERR_PTR(-ENOMEM);
57 }
58
59 return &stab->map;
60 }
61
sock_map_get_from_fd(const union bpf_attr * attr,struct bpf_prog * prog)62 int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog)
63 {
64 u32 ufd = attr->target_fd;
65 struct bpf_map *map;
66 struct fd f;
67 int ret;
68
69 if (attr->attach_flags || attr->replace_bpf_fd)
70 return -EINVAL;
71
72 f = fdget(ufd);
73 map = __bpf_map_get(f);
74 if (IS_ERR(map))
75 return PTR_ERR(map);
76 ret = sock_map_prog_update(map, prog, NULL, attr->attach_type);
77 fdput(f);
78 return ret;
79 }
80
sock_map_prog_detach(const union bpf_attr * attr,enum bpf_prog_type ptype)81 int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype)
82 {
83 u32 ufd = attr->target_fd;
84 struct bpf_prog *prog;
85 struct bpf_map *map;
86 struct fd f;
87 int ret;
88
89 if (attr->attach_flags || attr->replace_bpf_fd)
90 return -EINVAL;
91
92 f = fdget(ufd);
93 map = __bpf_map_get(f);
94 if (IS_ERR(map))
95 return PTR_ERR(map);
96
97 prog = bpf_prog_get(attr->attach_bpf_fd);
98 if (IS_ERR(prog)) {
99 ret = PTR_ERR(prog);
100 goto put_map;
101 }
102
103 if (prog->type != ptype) {
104 ret = -EINVAL;
105 goto put_prog;
106 }
107
108 ret = sock_map_prog_update(map, NULL, prog, attr->attach_type);
109 put_prog:
110 bpf_prog_put(prog);
111 put_map:
112 fdput(f);
113 return ret;
114 }
115
sock_map_sk_acquire(struct sock * sk)116 static void sock_map_sk_acquire(struct sock *sk)
117 __acquires(&sk->sk_lock.slock)
118 {
119 lock_sock(sk);
120 rcu_read_lock();
121 }
122
sock_map_sk_release(struct sock * sk)123 static void sock_map_sk_release(struct sock *sk)
124 __releases(&sk->sk_lock.slock)
125 {
126 rcu_read_unlock();
127 release_sock(sk);
128 }
129
sock_map_add_link(struct sk_psock * psock,struct sk_psock_link * link,struct bpf_map * map,void * link_raw)130 static void sock_map_add_link(struct sk_psock *psock,
131 struct sk_psock_link *link,
132 struct bpf_map *map, void *link_raw)
133 {
134 link->link_raw = link_raw;
135 link->map = map;
136 spin_lock_bh(&psock->link_lock);
137 list_add_tail(&link->list, &psock->link);
138 spin_unlock_bh(&psock->link_lock);
139 }
140
sock_map_del_link(struct sock * sk,struct sk_psock * psock,void * link_raw)141 static void sock_map_del_link(struct sock *sk,
142 struct sk_psock *psock, void *link_raw)
143 {
144 bool strp_stop = false, verdict_stop = false;
145 struct sk_psock_link *link, *tmp;
146
147 spin_lock_bh(&psock->link_lock);
148 list_for_each_entry_safe(link, tmp, &psock->link, list) {
149 if (link->link_raw == link_raw) {
150 struct bpf_map *map = link->map;
151 struct sk_psock_progs *progs = sock_map_progs(map);
152
153 if (psock->saved_data_ready && progs->stream_parser)
154 strp_stop = true;
155 if (psock->saved_data_ready && progs->stream_verdict)
156 verdict_stop = true;
157 if (psock->saved_data_ready && progs->skb_verdict)
158 verdict_stop = true;
159 list_del(&link->list);
160 sk_psock_free_link(link);
161 }
162 }
163 spin_unlock_bh(&psock->link_lock);
164 if (strp_stop || verdict_stop) {
165 write_lock_bh(&sk->sk_callback_lock);
166 if (strp_stop)
167 sk_psock_stop_strp(sk, psock);
168 if (verdict_stop)
169 sk_psock_stop_verdict(sk, psock);
170
171 if (psock->psock_update_sk_prot)
172 psock->psock_update_sk_prot(sk, psock, false);
173 write_unlock_bh(&sk->sk_callback_lock);
174 }
175 }
176
sock_map_unref(struct sock * sk,void * link_raw)177 static void sock_map_unref(struct sock *sk, void *link_raw)
178 {
179 struct sk_psock *psock = sk_psock(sk);
180
181 if (likely(psock)) {
182 sock_map_del_link(sk, psock, link_raw);
183 sk_psock_put(sk, psock);
184 }
185 }
186
sock_map_init_proto(struct sock * sk,struct sk_psock * psock)187 static int sock_map_init_proto(struct sock *sk, struct sk_psock *psock)
188 {
189 if (!sk->sk_prot->psock_update_sk_prot)
190 return -EINVAL;
191 psock->psock_update_sk_prot = sk->sk_prot->psock_update_sk_prot;
192 return sk->sk_prot->psock_update_sk_prot(sk, psock, false);
193 }
194
sock_map_psock_get_checked(struct sock * sk)195 static struct sk_psock *sock_map_psock_get_checked(struct sock *sk)
196 {
197 struct sk_psock *psock;
198
199 rcu_read_lock();
200 psock = sk_psock(sk);
201 if (psock) {
202 if (sk->sk_prot->close != sock_map_close) {
203 psock = ERR_PTR(-EBUSY);
204 goto out;
205 }
206
207 if (!refcount_inc_not_zero(&psock->refcnt))
208 psock = ERR_PTR(-EBUSY);
209 }
210 out:
211 rcu_read_unlock();
212 return psock;
213 }
214
sock_map_link(struct bpf_map * map,struct sock * sk)215 static int sock_map_link(struct bpf_map *map, struct sock *sk)
216 {
217 struct sk_psock_progs *progs = sock_map_progs(map);
218 struct bpf_prog *stream_verdict = NULL;
219 struct bpf_prog *stream_parser = NULL;
220 struct bpf_prog *skb_verdict = NULL;
221 struct bpf_prog *msg_parser = NULL;
222 struct sk_psock *psock;
223 int ret;
224
225 stream_verdict = READ_ONCE(progs->stream_verdict);
226 if (stream_verdict) {
227 stream_verdict = bpf_prog_inc_not_zero(stream_verdict);
228 if (IS_ERR(stream_verdict))
229 return PTR_ERR(stream_verdict);
230 }
231
232 stream_parser = READ_ONCE(progs->stream_parser);
233 if (stream_parser) {
234 stream_parser = bpf_prog_inc_not_zero(stream_parser);
235 if (IS_ERR(stream_parser)) {
236 ret = PTR_ERR(stream_parser);
237 goto out_put_stream_verdict;
238 }
239 }
240
241 msg_parser = READ_ONCE(progs->msg_parser);
242 if (msg_parser) {
243 msg_parser = bpf_prog_inc_not_zero(msg_parser);
244 if (IS_ERR(msg_parser)) {
245 ret = PTR_ERR(msg_parser);
246 goto out_put_stream_parser;
247 }
248 }
249
250 skb_verdict = READ_ONCE(progs->skb_verdict);
251 if (skb_verdict) {
252 skb_verdict = bpf_prog_inc_not_zero(skb_verdict);
253 if (IS_ERR(skb_verdict)) {
254 ret = PTR_ERR(skb_verdict);
255 goto out_put_msg_parser;
256 }
257 }
258
259 psock = sock_map_psock_get_checked(sk);
260 if (IS_ERR(psock)) {
261 ret = PTR_ERR(psock);
262 goto out_progs;
263 }
264
265 if (psock) {
266 if ((msg_parser && READ_ONCE(psock->progs.msg_parser)) ||
267 (stream_parser && READ_ONCE(psock->progs.stream_parser)) ||
268 (skb_verdict && READ_ONCE(psock->progs.skb_verdict)) ||
269 (skb_verdict && READ_ONCE(psock->progs.stream_verdict)) ||
270 (stream_verdict && READ_ONCE(psock->progs.skb_verdict)) ||
271 (stream_verdict && READ_ONCE(psock->progs.stream_verdict))) {
272 sk_psock_put(sk, psock);
273 ret = -EBUSY;
274 goto out_progs;
275 }
276 } else {
277 psock = sk_psock_init(sk, map->numa_node);
278 if (IS_ERR(psock)) {
279 ret = PTR_ERR(psock);
280 goto out_progs;
281 }
282 }
283
284 if (msg_parser)
285 psock_set_prog(&psock->progs.msg_parser, msg_parser);
286 if (stream_parser)
287 psock_set_prog(&psock->progs.stream_parser, stream_parser);
288 if (stream_verdict)
289 psock_set_prog(&psock->progs.stream_verdict, stream_verdict);
290 if (skb_verdict)
291 psock_set_prog(&psock->progs.skb_verdict, skb_verdict);
292
293 /* msg_* and stream_* programs references tracked in psock after this
294 * point. Reference dec and cleanup will occur through psock destructor
295 */
296 ret = sock_map_init_proto(sk, psock);
297 if (ret < 0) {
298 sk_psock_put(sk, psock);
299 goto out;
300 }
301
302 write_lock_bh(&sk->sk_callback_lock);
303 if (stream_parser && stream_verdict && !psock->saved_data_ready) {
304 ret = sk_psock_init_strp(sk, psock);
305 if (ret) {
306 write_unlock_bh(&sk->sk_callback_lock);
307 sk_psock_put(sk, psock);
308 goto out;
309 }
310 sk_psock_start_strp(sk, psock);
311 } else if (!stream_parser && stream_verdict && !psock->saved_data_ready) {
312 sk_psock_start_verdict(sk,psock);
313 } else if (!stream_verdict && skb_verdict && !psock->saved_data_ready) {
314 sk_psock_start_verdict(sk, psock);
315 }
316 write_unlock_bh(&sk->sk_callback_lock);
317 return 0;
318 out_progs:
319 if (skb_verdict)
320 bpf_prog_put(skb_verdict);
321 out_put_msg_parser:
322 if (msg_parser)
323 bpf_prog_put(msg_parser);
324 out_put_stream_parser:
325 if (stream_parser)
326 bpf_prog_put(stream_parser);
327 out_put_stream_verdict:
328 if (stream_verdict)
329 bpf_prog_put(stream_verdict);
330 out:
331 return ret;
332 }
333
sock_map_free(struct bpf_map * map)334 static void sock_map_free(struct bpf_map *map)
335 {
336 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
337 int i;
338
339 /* After the sync no updates or deletes will be in-flight so it
340 * is safe to walk map and remove entries without risking a race
341 * in EEXIST update case.
342 */
343 synchronize_rcu();
344 for (i = 0; i < stab->map.max_entries; i++) {
345 struct sock **psk = &stab->sks[i];
346 struct sock *sk;
347
348 sk = xchg(psk, NULL);
349 if (sk) {
350 sock_hold(sk);
351 lock_sock(sk);
352 rcu_read_lock();
353 sock_map_unref(sk, psk);
354 rcu_read_unlock();
355 release_sock(sk);
356 sock_put(sk);
357 }
358 }
359
360 /* wait for psock readers accessing its map link */
361 synchronize_rcu();
362
363 bpf_map_area_free(stab->sks);
364 kfree(stab);
365 }
366
sock_map_release_progs(struct bpf_map * map)367 static void sock_map_release_progs(struct bpf_map *map)
368 {
369 psock_progs_drop(&container_of(map, struct bpf_stab, map)->progs);
370 }
371
__sock_map_lookup_elem(struct bpf_map * map,u32 key)372 static struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key)
373 {
374 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
375
376 WARN_ON_ONCE(!rcu_read_lock_held());
377
378 if (unlikely(key >= map->max_entries))
379 return NULL;
380 return READ_ONCE(stab->sks[key]);
381 }
382
sock_map_lookup(struct bpf_map * map,void * key)383 static void *sock_map_lookup(struct bpf_map *map, void *key)
384 {
385 struct sock *sk;
386
387 sk = __sock_map_lookup_elem(map, *(u32 *)key);
388 if (!sk)
389 return NULL;
390 if (sk_is_refcounted(sk) && !refcount_inc_not_zero(&sk->sk_refcnt))
391 return NULL;
392 return sk;
393 }
394
sock_map_lookup_sys(struct bpf_map * map,void * key)395 static void *sock_map_lookup_sys(struct bpf_map *map, void *key)
396 {
397 struct sock *sk;
398
399 if (map->value_size != sizeof(u64))
400 return ERR_PTR(-ENOSPC);
401
402 sk = __sock_map_lookup_elem(map, *(u32 *)key);
403 if (!sk)
404 return ERR_PTR(-ENOENT);
405
406 __sock_gen_cookie(sk);
407 return &sk->sk_cookie;
408 }
409
__sock_map_delete(struct bpf_stab * stab,struct sock * sk_test,struct sock ** psk)410 static int __sock_map_delete(struct bpf_stab *stab, struct sock *sk_test,
411 struct sock **psk)
412 {
413 struct sock *sk;
414 int err = 0;
415 unsigned long flags;
416
417 raw_spin_lock_irqsave(&stab->lock, flags);
418 sk = *psk;
419 if (!sk_test || sk_test == sk)
420 sk = xchg(psk, NULL);
421
422 if (likely(sk))
423 sock_map_unref(sk, psk);
424 else
425 err = -EINVAL;
426
427 raw_spin_unlock_irqrestore(&stab->lock, flags);
428 return err;
429 }
430
sock_map_delete_from_link(struct bpf_map * map,struct sock * sk,void * link_raw)431 static void sock_map_delete_from_link(struct bpf_map *map, struct sock *sk,
432 void *link_raw)
433 {
434 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
435
436 __sock_map_delete(stab, sk, link_raw);
437 }
438
sock_map_delete_elem(struct bpf_map * map,void * key)439 static int sock_map_delete_elem(struct bpf_map *map, void *key)
440 {
441 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
442 u32 i = *(u32 *)key;
443 struct sock **psk;
444
445 if (unlikely(i >= map->max_entries))
446 return -EINVAL;
447
448 psk = &stab->sks[i];
449 return __sock_map_delete(stab, NULL, psk);
450 }
451
sock_map_get_next_key(struct bpf_map * map,void * key,void * next)452 static int sock_map_get_next_key(struct bpf_map *map, void *key, void *next)
453 {
454 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
455 u32 i = key ? *(u32 *)key : U32_MAX;
456 u32 *key_next = next;
457
458 if (i == stab->map.max_entries - 1)
459 return -ENOENT;
460 if (i >= stab->map.max_entries)
461 *key_next = 0;
462 else
463 *key_next = i + 1;
464 return 0;
465 }
466
sock_map_update_common(struct bpf_map * map,u32 idx,struct sock * sk,u64 flags)467 static int sock_map_update_common(struct bpf_map *map, u32 idx,
468 struct sock *sk, u64 flags)
469 {
470 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
471 struct sk_psock_link *link;
472 struct sk_psock *psock;
473 struct sock *osk;
474 int ret;
475
476 WARN_ON_ONCE(!rcu_read_lock_held());
477 if (unlikely(flags > BPF_EXIST))
478 return -EINVAL;
479 if (unlikely(idx >= map->max_entries))
480 return -E2BIG;
481
482 link = sk_psock_init_link();
483 if (!link)
484 return -ENOMEM;
485
486 ret = sock_map_link(map, sk);
487 if (ret < 0)
488 goto out_free;
489
490 psock = sk_psock(sk);
491 WARN_ON_ONCE(!psock);
492
493 raw_spin_lock_bh(&stab->lock);
494 osk = stab->sks[idx];
495 if (osk && flags == BPF_NOEXIST) {
496 ret = -EEXIST;
497 goto out_unlock;
498 } else if (!osk && flags == BPF_EXIST) {
499 ret = -ENOENT;
500 goto out_unlock;
501 }
502
503 sock_map_add_link(psock, link, map, &stab->sks[idx]);
504 stab->sks[idx] = sk;
505 if (osk)
506 sock_map_unref(osk, &stab->sks[idx]);
507 raw_spin_unlock_bh(&stab->lock);
508 return 0;
509 out_unlock:
510 raw_spin_unlock_bh(&stab->lock);
511 if (psock)
512 sk_psock_put(sk, psock);
513 out_free:
514 sk_psock_free_link(link);
515 return ret;
516 }
517
sock_map_op_okay(const struct bpf_sock_ops_kern * ops)518 static bool sock_map_op_okay(const struct bpf_sock_ops_kern *ops)
519 {
520 return ops->op == BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB ||
521 ops->op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB ||
522 ops->op == BPF_SOCK_OPS_TCP_LISTEN_CB;
523 }
524
sk_is_tcp(const struct sock * sk)525 static bool sk_is_tcp(const struct sock *sk)
526 {
527 return sk->sk_type == SOCK_STREAM &&
528 sk->sk_protocol == IPPROTO_TCP;
529 }
530
sock_map_redirect_allowed(const struct sock * sk)531 static bool sock_map_redirect_allowed(const struct sock *sk)
532 {
533 if (sk_is_tcp(sk))
534 return sk->sk_state != TCP_LISTEN;
535 else
536 return sk->sk_state == TCP_ESTABLISHED;
537 }
538
sock_map_sk_is_suitable(const struct sock * sk)539 static bool sock_map_sk_is_suitable(const struct sock *sk)
540 {
541 return !!sk->sk_prot->psock_update_sk_prot;
542 }
543
sock_map_sk_state_allowed(const struct sock * sk)544 static bool sock_map_sk_state_allowed(const struct sock *sk)
545 {
546 if (sk_is_tcp(sk))
547 return (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_LISTEN);
548 return true;
549 }
550
551 static int sock_hash_update_common(struct bpf_map *map, void *key,
552 struct sock *sk, u64 flags);
553
sock_map_update_elem_sys(struct bpf_map * map,void * key,void * value,u64 flags)554 int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value,
555 u64 flags)
556 {
557 struct socket *sock;
558 struct sock *sk;
559 int ret;
560 u64 ufd;
561
562 if (map->value_size == sizeof(u64))
563 ufd = *(u64 *)value;
564 else
565 ufd = *(u32 *)value;
566 if (ufd > S32_MAX)
567 return -EINVAL;
568
569 sock = sockfd_lookup(ufd, &ret);
570 if (!sock)
571 return ret;
572 sk = sock->sk;
573 if (!sk) {
574 ret = -EINVAL;
575 goto out;
576 }
577 if (!sock_map_sk_is_suitable(sk)) {
578 ret = -EOPNOTSUPP;
579 goto out;
580 }
581
582 sock_map_sk_acquire(sk);
583 if (!sock_map_sk_state_allowed(sk))
584 ret = -EOPNOTSUPP;
585 else if (map->map_type == BPF_MAP_TYPE_SOCKMAP)
586 ret = sock_map_update_common(map, *(u32 *)key, sk, flags);
587 else
588 ret = sock_hash_update_common(map, key, sk, flags);
589 sock_map_sk_release(sk);
590 out:
591 sockfd_put(sock);
592 return ret;
593 }
594
sock_map_update_elem(struct bpf_map * map,void * key,void * value,u64 flags)595 static int sock_map_update_elem(struct bpf_map *map, void *key,
596 void *value, u64 flags)
597 {
598 struct sock *sk = (struct sock *)value;
599 int ret;
600
601 if (unlikely(!sk || !sk_fullsock(sk)))
602 return -EINVAL;
603
604 if (!sock_map_sk_is_suitable(sk))
605 return -EOPNOTSUPP;
606
607 local_bh_disable();
608 bh_lock_sock(sk);
609 if (!sock_map_sk_state_allowed(sk))
610 ret = -EOPNOTSUPP;
611 else if (map->map_type == BPF_MAP_TYPE_SOCKMAP)
612 ret = sock_map_update_common(map, *(u32 *)key, sk, flags);
613 else
614 ret = sock_hash_update_common(map, key, sk, flags);
615 bh_unlock_sock(sk);
616 local_bh_enable();
617 return ret;
618 }
619
BPF_CALL_4(bpf_sock_map_update,struct bpf_sock_ops_kern *,sops,struct bpf_map *,map,void *,key,u64,flags)620 BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, sops,
621 struct bpf_map *, map, void *, key, u64, flags)
622 {
623 WARN_ON_ONCE(!rcu_read_lock_held());
624
625 if (likely(sock_map_sk_is_suitable(sops->sk) &&
626 sock_map_op_okay(sops)))
627 return sock_map_update_common(map, *(u32 *)key, sops->sk,
628 flags);
629 return -EOPNOTSUPP;
630 }
631
632 const struct bpf_func_proto bpf_sock_map_update_proto = {
633 .func = bpf_sock_map_update,
634 .gpl_only = false,
635 .pkt_access = true,
636 .ret_type = RET_INTEGER,
637 .arg1_type = ARG_PTR_TO_CTX,
638 .arg2_type = ARG_CONST_MAP_PTR,
639 .arg3_type = ARG_PTR_TO_MAP_KEY,
640 .arg4_type = ARG_ANYTHING,
641 };
642
BPF_CALL_4(bpf_sk_redirect_map,struct sk_buff *,skb,struct bpf_map *,map,u32,key,u64,flags)643 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
644 struct bpf_map *, map, u32, key, u64, flags)
645 {
646 struct sock *sk;
647
648 if (unlikely(flags & ~(BPF_F_INGRESS)))
649 return SK_DROP;
650
651 sk = __sock_map_lookup_elem(map, key);
652 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
653 return SK_DROP;
654
655 skb_bpf_set_redir(skb, sk, flags & BPF_F_INGRESS);
656 return SK_PASS;
657 }
658
659 const struct bpf_func_proto bpf_sk_redirect_map_proto = {
660 .func = bpf_sk_redirect_map,
661 .gpl_only = false,
662 .ret_type = RET_INTEGER,
663 .arg1_type = ARG_PTR_TO_CTX,
664 .arg2_type = ARG_CONST_MAP_PTR,
665 .arg3_type = ARG_ANYTHING,
666 .arg4_type = ARG_ANYTHING,
667 };
668
BPF_CALL_4(bpf_msg_redirect_map,struct sk_msg *,msg,struct bpf_map *,map,u32,key,u64,flags)669 BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
670 struct bpf_map *, map, u32, key, u64, flags)
671 {
672 struct sock *sk;
673
674 if (unlikely(flags & ~(BPF_F_INGRESS)))
675 return SK_DROP;
676
677 sk = __sock_map_lookup_elem(map, key);
678 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
679 return SK_DROP;
680 if (!(flags & BPF_F_INGRESS) && !sk_is_tcp(sk))
681 return SK_DROP;
682
683 msg->flags = flags;
684 msg->sk_redir = sk;
685 return SK_PASS;
686 }
687
688 const struct bpf_func_proto bpf_msg_redirect_map_proto = {
689 .func = bpf_msg_redirect_map,
690 .gpl_only = false,
691 .ret_type = RET_INTEGER,
692 .arg1_type = ARG_PTR_TO_CTX,
693 .arg2_type = ARG_CONST_MAP_PTR,
694 .arg3_type = ARG_ANYTHING,
695 .arg4_type = ARG_ANYTHING,
696 };
697
698 struct sock_map_seq_info {
699 struct bpf_map *map;
700 struct sock *sk;
701 u32 index;
702 };
703
704 struct bpf_iter__sockmap {
705 __bpf_md_ptr(struct bpf_iter_meta *, meta);
706 __bpf_md_ptr(struct bpf_map *, map);
707 __bpf_md_ptr(void *, key);
708 __bpf_md_ptr(struct sock *, sk);
709 };
710
DEFINE_BPF_ITER_FUNC(sockmap,struct bpf_iter_meta * meta,struct bpf_map * map,void * key,struct sock * sk)711 DEFINE_BPF_ITER_FUNC(sockmap, struct bpf_iter_meta *meta,
712 struct bpf_map *map, void *key,
713 struct sock *sk)
714
715 static void *sock_map_seq_lookup_elem(struct sock_map_seq_info *info)
716 {
717 if (unlikely(info->index >= info->map->max_entries))
718 return NULL;
719
720 info->sk = __sock_map_lookup_elem(info->map, info->index);
721
722 /* can't return sk directly, since that might be NULL */
723 return info;
724 }
725
sock_map_seq_start(struct seq_file * seq,loff_t * pos)726 static void *sock_map_seq_start(struct seq_file *seq, loff_t *pos)
727 __acquires(rcu)
728 {
729 struct sock_map_seq_info *info = seq->private;
730
731 if (*pos == 0)
732 ++*pos;
733
734 /* pairs with sock_map_seq_stop */
735 rcu_read_lock();
736 return sock_map_seq_lookup_elem(info);
737 }
738
sock_map_seq_next(struct seq_file * seq,void * v,loff_t * pos)739 static void *sock_map_seq_next(struct seq_file *seq, void *v, loff_t *pos)
740 __must_hold(rcu)
741 {
742 struct sock_map_seq_info *info = seq->private;
743
744 ++*pos;
745 ++info->index;
746
747 return sock_map_seq_lookup_elem(info);
748 }
749
sock_map_seq_show(struct seq_file * seq,void * v)750 static int sock_map_seq_show(struct seq_file *seq, void *v)
751 __must_hold(rcu)
752 {
753 struct sock_map_seq_info *info = seq->private;
754 struct bpf_iter__sockmap ctx = {};
755 struct bpf_iter_meta meta;
756 struct bpf_prog *prog;
757
758 meta.seq = seq;
759 prog = bpf_iter_get_info(&meta, !v);
760 if (!prog)
761 return 0;
762
763 ctx.meta = &meta;
764 ctx.map = info->map;
765 if (v) {
766 ctx.key = &info->index;
767 ctx.sk = info->sk;
768 }
769
770 return bpf_iter_run_prog(prog, &ctx);
771 }
772
sock_map_seq_stop(struct seq_file * seq,void * v)773 static void sock_map_seq_stop(struct seq_file *seq, void *v)
774 __releases(rcu)
775 {
776 if (!v)
777 (void)sock_map_seq_show(seq, NULL);
778
779 /* pairs with sock_map_seq_start */
780 rcu_read_unlock();
781 }
782
783 static const struct seq_operations sock_map_seq_ops = {
784 .start = sock_map_seq_start,
785 .next = sock_map_seq_next,
786 .stop = sock_map_seq_stop,
787 .show = sock_map_seq_show,
788 };
789
sock_map_init_seq_private(void * priv_data,struct bpf_iter_aux_info * aux)790 static int sock_map_init_seq_private(void *priv_data,
791 struct bpf_iter_aux_info *aux)
792 {
793 struct sock_map_seq_info *info = priv_data;
794
795 bpf_map_inc_with_uref(aux->map);
796 info->map = aux->map;
797 return 0;
798 }
799
sock_map_fini_seq_private(void * priv_data)800 static void sock_map_fini_seq_private(void *priv_data)
801 {
802 struct sock_map_seq_info *info = priv_data;
803
804 bpf_map_put_with_uref(info->map);
805 }
806
807 static const struct bpf_iter_seq_info sock_map_iter_seq_info = {
808 .seq_ops = &sock_map_seq_ops,
809 .init_seq_private = sock_map_init_seq_private,
810 .fini_seq_private = sock_map_fini_seq_private,
811 .seq_priv_size = sizeof(struct sock_map_seq_info),
812 };
813
814 static int sock_map_btf_id;
815 const struct bpf_map_ops sock_map_ops = {
816 .map_meta_equal = bpf_map_meta_equal,
817 .map_alloc = sock_map_alloc,
818 .map_free = sock_map_free,
819 .map_get_next_key = sock_map_get_next_key,
820 .map_lookup_elem_sys_only = sock_map_lookup_sys,
821 .map_update_elem = sock_map_update_elem,
822 .map_delete_elem = sock_map_delete_elem,
823 .map_lookup_elem = sock_map_lookup,
824 .map_release_uref = sock_map_release_progs,
825 .map_check_btf = map_check_no_btf,
826 .map_btf_name = "bpf_stab",
827 .map_btf_id = &sock_map_btf_id,
828 .iter_seq_info = &sock_map_iter_seq_info,
829 };
830
831 struct bpf_shtab_elem {
832 struct rcu_head rcu;
833 u32 hash;
834 struct sock *sk;
835 struct hlist_node node;
836 u8 key[];
837 };
838
839 struct bpf_shtab_bucket {
840 struct hlist_head head;
841 raw_spinlock_t lock;
842 };
843
844 struct bpf_shtab {
845 struct bpf_map map;
846 struct bpf_shtab_bucket *buckets;
847 u32 buckets_num;
848 u32 elem_size;
849 struct sk_psock_progs progs;
850 atomic_t count;
851 };
852
sock_hash_bucket_hash(const void * key,u32 len)853 static inline u32 sock_hash_bucket_hash(const void *key, u32 len)
854 {
855 return jhash(key, len, 0);
856 }
857
sock_hash_select_bucket(struct bpf_shtab * htab,u32 hash)858 static struct bpf_shtab_bucket *sock_hash_select_bucket(struct bpf_shtab *htab,
859 u32 hash)
860 {
861 return &htab->buckets[hash & (htab->buckets_num - 1)];
862 }
863
864 static struct bpf_shtab_elem *
sock_hash_lookup_elem_raw(struct hlist_head * head,u32 hash,void * key,u32 key_size)865 sock_hash_lookup_elem_raw(struct hlist_head *head, u32 hash, void *key,
866 u32 key_size)
867 {
868 struct bpf_shtab_elem *elem;
869
870 hlist_for_each_entry_rcu(elem, head, node) {
871 if (elem->hash == hash &&
872 !memcmp(&elem->key, key, key_size))
873 return elem;
874 }
875
876 return NULL;
877 }
878
__sock_hash_lookup_elem(struct bpf_map * map,void * key)879 static struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key)
880 {
881 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
882 u32 key_size = map->key_size, hash;
883 struct bpf_shtab_bucket *bucket;
884 struct bpf_shtab_elem *elem;
885
886 WARN_ON_ONCE(!rcu_read_lock_held());
887
888 hash = sock_hash_bucket_hash(key, key_size);
889 bucket = sock_hash_select_bucket(htab, hash);
890 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
891
892 return elem ? elem->sk : NULL;
893 }
894
sock_hash_free_elem(struct bpf_shtab * htab,struct bpf_shtab_elem * elem)895 static void sock_hash_free_elem(struct bpf_shtab *htab,
896 struct bpf_shtab_elem *elem)
897 {
898 atomic_dec(&htab->count);
899 kfree_rcu(elem, rcu);
900 }
901
sock_hash_delete_from_link(struct bpf_map * map,struct sock * sk,void * link_raw)902 static void sock_hash_delete_from_link(struct bpf_map *map, struct sock *sk,
903 void *link_raw)
904 {
905 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
906 struct bpf_shtab_elem *elem_probe, *elem = link_raw;
907 struct bpf_shtab_bucket *bucket;
908
909 WARN_ON_ONCE(!rcu_read_lock_held());
910 bucket = sock_hash_select_bucket(htab, elem->hash);
911
912 /* elem may be deleted in parallel from the map, but access here
913 * is okay since it's going away only after RCU grace period.
914 * However, we need to check whether it's still present.
915 */
916 raw_spin_lock_bh(&bucket->lock);
917 elem_probe = sock_hash_lookup_elem_raw(&bucket->head, elem->hash,
918 elem->key, map->key_size);
919 if (elem_probe && elem_probe == elem) {
920 hlist_del_rcu(&elem->node);
921 sock_map_unref(elem->sk, elem);
922 sock_hash_free_elem(htab, elem);
923 }
924 raw_spin_unlock_bh(&bucket->lock);
925 }
926
sock_hash_delete_elem(struct bpf_map * map,void * key)927 static int sock_hash_delete_elem(struct bpf_map *map, void *key)
928 {
929 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
930 u32 hash, key_size = map->key_size;
931 struct bpf_shtab_bucket *bucket;
932 struct bpf_shtab_elem *elem;
933 int ret = -ENOENT;
934 unsigned long flags;
935
936 hash = sock_hash_bucket_hash(key, key_size);
937 bucket = sock_hash_select_bucket(htab, hash);
938
939 raw_spin_lock_irqsave(&bucket->lock, flags);
940 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
941 if (elem) {
942 hlist_del_rcu(&elem->node);
943 sock_map_unref(elem->sk, elem);
944 sock_hash_free_elem(htab, elem);
945 ret = 0;
946 }
947 raw_spin_unlock_irqrestore(&bucket->lock, flags);
948 return ret;
949 }
950
sock_hash_alloc_elem(struct bpf_shtab * htab,void * key,u32 key_size,u32 hash,struct sock * sk,struct bpf_shtab_elem * old)951 static struct bpf_shtab_elem *sock_hash_alloc_elem(struct bpf_shtab *htab,
952 void *key, u32 key_size,
953 u32 hash, struct sock *sk,
954 struct bpf_shtab_elem *old)
955 {
956 struct bpf_shtab_elem *new;
957
958 if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
959 if (!old) {
960 atomic_dec(&htab->count);
961 return ERR_PTR(-E2BIG);
962 }
963 }
964
965 new = bpf_map_kmalloc_node(&htab->map, htab->elem_size,
966 GFP_ATOMIC | __GFP_NOWARN,
967 htab->map.numa_node);
968 if (!new) {
969 atomic_dec(&htab->count);
970 return ERR_PTR(-ENOMEM);
971 }
972 memcpy(new->key, key, key_size);
973 new->sk = sk;
974 new->hash = hash;
975 return new;
976 }
977
sock_hash_update_common(struct bpf_map * map,void * key,struct sock * sk,u64 flags)978 static int sock_hash_update_common(struct bpf_map *map, void *key,
979 struct sock *sk, u64 flags)
980 {
981 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
982 u32 key_size = map->key_size, hash;
983 struct bpf_shtab_elem *elem, *elem_new;
984 struct bpf_shtab_bucket *bucket;
985 struct sk_psock_link *link;
986 struct sk_psock *psock;
987 int ret;
988
989 WARN_ON_ONCE(!rcu_read_lock_held());
990 if (unlikely(flags > BPF_EXIST))
991 return -EINVAL;
992
993 link = sk_psock_init_link();
994 if (!link)
995 return -ENOMEM;
996
997 ret = sock_map_link(map, sk);
998 if (ret < 0)
999 goto out_free;
1000
1001 psock = sk_psock(sk);
1002 WARN_ON_ONCE(!psock);
1003
1004 hash = sock_hash_bucket_hash(key, key_size);
1005 bucket = sock_hash_select_bucket(htab, hash);
1006
1007 raw_spin_lock_bh(&bucket->lock);
1008 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
1009 if (elem && flags == BPF_NOEXIST) {
1010 ret = -EEXIST;
1011 goto out_unlock;
1012 } else if (!elem && flags == BPF_EXIST) {
1013 ret = -ENOENT;
1014 goto out_unlock;
1015 }
1016
1017 elem_new = sock_hash_alloc_elem(htab, key, key_size, hash, sk, elem);
1018 if (IS_ERR(elem_new)) {
1019 ret = PTR_ERR(elem_new);
1020 goto out_unlock;
1021 }
1022
1023 sock_map_add_link(psock, link, map, elem_new);
1024 /* Add new element to the head of the list, so that
1025 * concurrent search will find it before old elem.
1026 */
1027 hlist_add_head_rcu(&elem_new->node, &bucket->head);
1028 if (elem) {
1029 hlist_del_rcu(&elem->node);
1030 sock_map_unref(elem->sk, elem);
1031 sock_hash_free_elem(htab, elem);
1032 }
1033 raw_spin_unlock_bh(&bucket->lock);
1034 return 0;
1035 out_unlock:
1036 raw_spin_unlock_bh(&bucket->lock);
1037 sk_psock_put(sk, psock);
1038 out_free:
1039 sk_psock_free_link(link);
1040 return ret;
1041 }
1042
sock_hash_get_next_key(struct bpf_map * map,void * key,void * key_next)1043 static int sock_hash_get_next_key(struct bpf_map *map, void *key,
1044 void *key_next)
1045 {
1046 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
1047 struct bpf_shtab_elem *elem, *elem_next;
1048 u32 hash, key_size = map->key_size;
1049 struct hlist_head *head;
1050 int i = 0;
1051
1052 if (!key)
1053 goto find_first_elem;
1054 hash = sock_hash_bucket_hash(key, key_size);
1055 head = &sock_hash_select_bucket(htab, hash)->head;
1056 elem = sock_hash_lookup_elem_raw(head, hash, key, key_size);
1057 if (!elem)
1058 goto find_first_elem;
1059
1060 elem_next = hlist_entry_safe(rcu_dereference(hlist_next_rcu(&elem->node)),
1061 struct bpf_shtab_elem, node);
1062 if (elem_next) {
1063 memcpy(key_next, elem_next->key, key_size);
1064 return 0;
1065 }
1066
1067 i = hash & (htab->buckets_num - 1);
1068 i++;
1069 find_first_elem:
1070 for (; i < htab->buckets_num; i++) {
1071 head = &sock_hash_select_bucket(htab, i)->head;
1072 elem_next = hlist_entry_safe(rcu_dereference(hlist_first_rcu(head)),
1073 struct bpf_shtab_elem, node);
1074 if (elem_next) {
1075 memcpy(key_next, elem_next->key, key_size);
1076 return 0;
1077 }
1078 }
1079
1080 return -ENOENT;
1081 }
1082
sock_hash_alloc(union bpf_attr * attr)1083 static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
1084 {
1085 struct bpf_shtab *htab;
1086 int i, err;
1087
1088 if (!capable(CAP_NET_ADMIN))
1089 return ERR_PTR(-EPERM);
1090 if (attr->max_entries == 0 ||
1091 attr->key_size == 0 ||
1092 (attr->value_size != sizeof(u32) &&
1093 attr->value_size != sizeof(u64)) ||
1094 attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
1095 return ERR_PTR(-EINVAL);
1096 if (attr->key_size > MAX_BPF_STACK)
1097 return ERR_PTR(-E2BIG);
1098
1099 htab = kzalloc(sizeof(*htab), GFP_USER | __GFP_ACCOUNT);
1100 if (!htab)
1101 return ERR_PTR(-ENOMEM);
1102
1103 bpf_map_init_from_attr(&htab->map, attr);
1104
1105 htab->buckets_num = roundup_pow_of_two(htab->map.max_entries);
1106 htab->elem_size = sizeof(struct bpf_shtab_elem) +
1107 round_up(htab->map.key_size, 8);
1108 if (htab->buckets_num == 0 ||
1109 htab->buckets_num > U32_MAX / sizeof(struct bpf_shtab_bucket)) {
1110 err = -EINVAL;
1111 goto free_htab;
1112 }
1113
1114 htab->buckets = bpf_map_area_alloc(htab->buckets_num *
1115 sizeof(struct bpf_shtab_bucket),
1116 htab->map.numa_node);
1117 if (!htab->buckets) {
1118 err = -ENOMEM;
1119 goto free_htab;
1120 }
1121
1122 for (i = 0; i < htab->buckets_num; i++) {
1123 INIT_HLIST_HEAD(&htab->buckets[i].head);
1124 raw_spin_lock_init(&htab->buckets[i].lock);
1125 }
1126
1127 return &htab->map;
1128 free_htab:
1129 kfree(htab);
1130 return ERR_PTR(err);
1131 }
1132
sock_hash_free(struct bpf_map * map)1133 static void sock_hash_free(struct bpf_map *map)
1134 {
1135 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
1136 struct bpf_shtab_bucket *bucket;
1137 struct hlist_head unlink_list;
1138 struct bpf_shtab_elem *elem;
1139 struct hlist_node *node;
1140 int i;
1141
1142 /* After the sync no updates or deletes will be in-flight so it
1143 * is safe to walk map and remove entries without risking a race
1144 * in EEXIST update case.
1145 */
1146 synchronize_rcu();
1147 for (i = 0; i < htab->buckets_num; i++) {
1148 bucket = sock_hash_select_bucket(htab, i);
1149
1150 /* We are racing with sock_hash_delete_from_link to
1151 * enter the spin-lock critical section. Every socket on
1152 * the list is still linked to sockhash. Since link
1153 * exists, psock exists and holds a ref to socket. That
1154 * lets us to grab a socket ref too.
1155 */
1156 raw_spin_lock_bh(&bucket->lock);
1157 hlist_for_each_entry(elem, &bucket->head, node)
1158 sock_hold(elem->sk);
1159 hlist_move_list(&bucket->head, &unlink_list);
1160 raw_spin_unlock_bh(&bucket->lock);
1161
1162 /* Process removed entries out of atomic context to
1163 * block for socket lock before deleting the psock's
1164 * link to sockhash.
1165 */
1166 hlist_for_each_entry_safe(elem, node, &unlink_list, node) {
1167 hlist_del(&elem->node);
1168 lock_sock(elem->sk);
1169 rcu_read_lock();
1170 sock_map_unref(elem->sk, elem);
1171 rcu_read_unlock();
1172 release_sock(elem->sk);
1173 sock_put(elem->sk);
1174 sock_hash_free_elem(htab, elem);
1175 }
1176 }
1177
1178 /* wait for psock readers accessing its map link */
1179 synchronize_rcu();
1180
1181 bpf_map_area_free(htab->buckets);
1182 kfree(htab);
1183 }
1184
sock_hash_lookup_sys(struct bpf_map * map,void * key)1185 static void *sock_hash_lookup_sys(struct bpf_map *map, void *key)
1186 {
1187 struct sock *sk;
1188
1189 if (map->value_size != sizeof(u64))
1190 return ERR_PTR(-ENOSPC);
1191
1192 sk = __sock_hash_lookup_elem(map, key);
1193 if (!sk)
1194 return ERR_PTR(-ENOENT);
1195
1196 __sock_gen_cookie(sk);
1197 return &sk->sk_cookie;
1198 }
1199
sock_hash_lookup(struct bpf_map * map,void * key)1200 static void *sock_hash_lookup(struct bpf_map *map, void *key)
1201 {
1202 struct sock *sk;
1203
1204 sk = __sock_hash_lookup_elem(map, key);
1205 if (!sk)
1206 return NULL;
1207 if (sk_is_refcounted(sk) && !refcount_inc_not_zero(&sk->sk_refcnt))
1208 return NULL;
1209 return sk;
1210 }
1211
sock_hash_release_progs(struct bpf_map * map)1212 static void sock_hash_release_progs(struct bpf_map *map)
1213 {
1214 psock_progs_drop(&container_of(map, struct bpf_shtab, map)->progs);
1215 }
1216
BPF_CALL_4(bpf_sock_hash_update,struct bpf_sock_ops_kern *,sops,struct bpf_map *,map,void *,key,u64,flags)1217 BPF_CALL_4(bpf_sock_hash_update, struct bpf_sock_ops_kern *, sops,
1218 struct bpf_map *, map, void *, key, u64, flags)
1219 {
1220 WARN_ON_ONCE(!rcu_read_lock_held());
1221
1222 if (likely(sock_map_sk_is_suitable(sops->sk) &&
1223 sock_map_op_okay(sops)))
1224 return sock_hash_update_common(map, key, sops->sk, flags);
1225 return -EOPNOTSUPP;
1226 }
1227
1228 const struct bpf_func_proto bpf_sock_hash_update_proto = {
1229 .func = bpf_sock_hash_update,
1230 .gpl_only = false,
1231 .pkt_access = true,
1232 .ret_type = RET_INTEGER,
1233 .arg1_type = ARG_PTR_TO_CTX,
1234 .arg2_type = ARG_CONST_MAP_PTR,
1235 .arg3_type = ARG_PTR_TO_MAP_KEY,
1236 .arg4_type = ARG_ANYTHING,
1237 };
1238
BPF_CALL_4(bpf_sk_redirect_hash,struct sk_buff *,skb,struct bpf_map *,map,void *,key,u64,flags)1239 BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
1240 struct bpf_map *, map, void *, key, u64, flags)
1241 {
1242 struct sock *sk;
1243
1244 if (unlikely(flags & ~(BPF_F_INGRESS)))
1245 return SK_DROP;
1246
1247 sk = __sock_hash_lookup_elem(map, key);
1248 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1249 return SK_DROP;
1250
1251 skb_bpf_set_redir(skb, sk, flags & BPF_F_INGRESS);
1252 return SK_PASS;
1253 }
1254
1255 const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
1256 .func = bpf_sk_redirect_hash,
1257 .gpl_only = false,
1258 .ret_type = RET_INTEGER,
1259 .arg1_type = ARG_PTR_TO_CTX,
1260 .arg2_type = ARG_CONST_MAP_PTR,
1261 .arg3_type = ARG_PTR_TO_MAP_KEY,
1262 .arg4_type = ARG_ANYTHING,
1263 };
1264
BPF_CALL_4(bpf_msg_redirect_hash,struct sk_msg *,msg,struct bpf_map *,map,void *,key,u64,flags)1265 BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
1266 struct bpf_map *, map, void *, key, u64, flags)
1267 {
1268 struct sock *sk;
1269
1270 if (unlikely(flags & ~(BPF_F_INGRESS)))
1271 return SK_DROP;
1272
1273 sk = __sock_hash_lookup_elem(map, key);
1274 if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
1275 return SK_DROP;
1276 if (!(flags & BPF_F_INGRESS) && !sk_is_tcp(sk))
1277 return SK_DROP;
1278
1279 msg->flags = flags;
1280 msg->sk_redir = sk;
1281 return SK_PASS;
1282 }
1283
1284 const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
1285 .func = bpf_msg_redirect_hash,
1286 .gpl_only = false,
1287 .ret_type = RET_INTEGER,
1288 .arg1_type = ARG_PTR_TO_CTX,
1289 .arg2_type = ARG_CONST_MAP_PTR,
1290 .arg3_type = ARG_PTR_TO_MAP_KEY,
1291 .arg4_type = ARG_ANYTHING,
1292 };
1293
1294 struct sock_hash_seq_info {
1295 struct bpf_map *map;
1296 struct bpf_shtab *htab;
1297 u32 bucket_id;
1298 };
1299
sock_hash_seq_find_next(struct sock_hash_seq_info * info,struct bpf_shtab_elem * prev_elem)1300 static void *sock_hash_seq_find_next(struct sock_hash_seq_info *info,
1301 struct bpf_shtab_elem *prev_elem)
1302 {
1303 const struct bpf_shtab *htab = info->htab;
1304 struct bpf_shtab_bucket *bucket;
1305 struct bpf_shtab_elem *elem;
1306 struct hlist_node *node;
1307
1308 /* try to find next elem in the same bucket */
1309 if (prev_elem) {
1310 node = rcu_dereference(hlist_next_rcu(&prev_elem->node));
1311 elem = hlist_entry_safe(node, struct bpf_shtab_elem, node);
1312 if (elem)
1313 return elem;
1314
1315 /* no more elements, continue in the next bucket */
1316 info->bucket_id++;
1317 }
1318
1319 for (; info->bucket_id < htab->buckets_num; info->bucket_id++) {
1320 bucket = &htab->buckets[info->bucket_id];
1321 node = rcu_dereference(hlist_first_rcu(&bucket->head));
1322 elem = hlist_entry_safe(node, struct bpf_shtab_elem, node);
1323 if (elem)
1324 return elem;
1325 }
1326
1327 return NULL;
1328 }
1329
sock_hash_seq_start(struct seq_file * seq,loff_t * pos)1330 static void *sock_hash_seq_start(struct seq_file *seq, loff_t *pos)
1331 __acquires(rcu)
1332 {
1333 struct sock_hash_seq_info *info = seq->private;
1334
1335 if (*pos == 0)
1336 ++*pos;
1337
1338 /* pairs with sock_hash_seq_stop */
1339 rcu_read_lock();
1340 return sock_hash_seq_find_next(info, NULL);
1341 }
1342
sock_hash_seq_next(struct seq_file * seq,void * v,loff_t * pos)1343 static void *sock_hash_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1344 __must_hold(rcu)
1345 {
1346 struct sock_hash_seq_info *info = seq->private;
1347
1348 ++*pos;
1349 return sock_hash_seq_find_next(info, v);
1350 }
1351
sock_hash_seq_show(struct seq_file * seq,void * v)1352 static int sock_hash_seq_show(struct seq_file *seq, void *v)
1353 __must_hold(rcu)
1354 {
1355 struct sock_hash_seq_info *info = seq->private;
1356 struct bpf_iter__sockmap ctx = {};
1357 struct bpf_shtab_elem *elem = v;
1358 struct bpf_iter_meta meta;
1359 struct bpf_prog *prog;
1360
1361 meta.seq = seq;
1362 prog = bpf_iter_get_info(&meta, !elem);
1363 if (!prog)
1364 return 0;
1365
1366 ctx.meta = &meta;
1367 ctx.map = info->map;
1368 if (elem) {
1369 ctx.key = elem->key;
1370 ctx.sk = elem->sk;
1371 }
1372
1373 return bpf_iter_run_prog(prog, &ctx);
1374 }
1375
sock_hash_seq_stop(struct seq_file * seq,void * v)1376 static void sock_hash_seq_stop(struct seq_file *seq, void *v)
1377 __releases(rcu)
1378 {
1379 if (!v)
1380 (void)sock_hash_seq_show(seq, NULL);
1381
1382 /* pairs with sock_hash_seq_start */
1383 rcu_read_unlock();
1384 }
1385
1386 static const struct seq_operations sock_hash_seq_ops = {
1387 .start = sock_hash_seq_start,
1388 .next = sock_hash_seq_next,
1389 .stop = sock_hash_seq_stop,
1390 .show = sock_hash_seq_show,
1391 };
1392
sock_hash_init_seq_private(void * priv_data,struct bpf_iter_aux_info * aux)1393 static int sock_hash_init_seq_private(void *priv_data,
1394 struct bpf_iter_aux_info *aux)
1395 {
1396 struct sock_hash_seq_info *info = priv_data;
1397
1398 bpf_map_inc_with_uref(aux->map);
1399 info->map = aux->map;
1400 info->htab = container_of(aux->map, struct bpf_shtab, map);
1401 return 0;
1402 }
1403
sock_hash_fini_seq_private(void * priv_data)1404 static void sock_hash_fini_seq_private(void *priv_data)
1405 {
1406 struct sock_hash_seq_info *info = priv_data;
1407
1408 bpf_map_put_with_uref(info->map);
1409 }
1410
1411 static const struct bpf_iter_seq_info sock_hash_iter_seq_info = {
1412 .seq_ops = &sock_hash_seq_ops,
1413 .init_seq_private = sock_hash_init_seq_private,
1414 .fini_seq_private = sock_hash_fini_seq_private,
1415 .seq_priv_size = sizeof(struct sock_hash_seq_info),
1416 };
1417
1418 static int sock_hash_map_btf_id;
1419 const struct bpf_map_ops sock_hash_ops = {
1420 .map_meta_equal = bpf_map_meta_equal,
1421 .map_alloc = sock_hash_alloc,
1422 .map_free = sock_hash_free,
1423 .map_get_next_key = sock_hash_get_next_key,
1424 .map_update_elem = sock_map_update_elem,
1425 .map_delete_elem = sock_hash_delete_elem,
1426 .map_lookup_elem = sock_hash_lookup,
1427 .map_lookup_elem_sys_only = sock_hash_lookup_sys,
1428 .map_release_uref = sock_hash_release_progs,
1429 .map_check_btf = map_check_no_btf,
1430 .map_btf_name = "bpf_shtab",
1431 .map_btf_id = &sock_hash_map_btf_id,
1432 .iter_seq_info = &sock_hash_iter_seq_info,
1433 };
1434
sock_map_progs(struct bpf_map * map)1435 static struct sk_psock_progs *sock_map_progs(struct bpf_map *map)
1436 {
1437 switch (map->map_type) {
1438 case BPF_MAP_TYPE_SOCKMAP:
1439 return &container_of(map, struct bpf_stab, map)->progs;
1440 case BPF_MAP_TYPE_SOCKHASH:
1441 return &container_of(map, struct bpf_shtab, map)->progs;
1442 default:
1443 break;
1444 }
1445
1446 return NULL;
1447 }
1448
sock_map_prog_update(struct bpf_map * map,struct bpf_prog * prog,struct bpf_prog * old,u32 which)1449 static int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
1450 struct bpf_prog *old, u32 which)
1451 {
1452 struct sk_psock_progs *progs = sock_map_progs(map);
1453 struct bpf_prog **pprog;
1454
1455 if (!progs)
1456 return -EOPNOTSUPP;
1457
1458 switch (which) {
1459 case BPF_SK_MSG_VERDICT:
1460 pprog = &progs->msg_parser;
1461 break;
1462 #if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
1463 case BPF_SK_SKB_STREAM_PARSER:
1464 pprog = &progs->stream_parser;
1465 break;
1466 #endif
1467 case BPF_SK_SKB_STREAM_VERDICT:
1468 if (progs->skb_verdict)
1469 return -EBUSY;
1470 pprog = &progs->stream_verdict;
1471 break;
1472 case BPF_SK_SKB_VERDICT:
1473 if (progs->stream_verdict)
1474 return -EBUSY;
1475 pprog = &progs->skb_verdict;
1476 break;
1477 default:
1478 return -EOPNOTSUPP;
1479 }
1480
1481 if (old)
1482 return psock_replace_prog(pprog, prog, old);
1483
1484 psock_set_prog(pprog, prog);
1485 return 0;
1486 }
1487
sock_map_unlink(struct sock * sk,struct sk_psock_link * link)1488 static void sock_map_unlink(struct sock *sk, struct sk_psock_link *link)
1489 {
1490 switch (link->map->map_type) {
1491 case BPF_MAP_TYPE_SOCKMAP:
1492 return sock_map_delete_from_link(link->map, sk,
1493 link->link_raw);
1494 case BPF_MAP_TYPE_SOCKHASH:
1495 return sock_hash_delete_from_link(link->map, sk,
1496 link->link_raw);
1497 default:
1498 break;
1499 }
1500 }
1501
sock_map_remove_links(struct sock * sk,struct sk_psock * psock)1502 static void sock_map_remove_links(struct sock *sk, struct sk_psock *psock)
1503 {
1504 struct sk_psock_link *link;
1505
1506 while ((link = sk_psock_link_pop(psock))) {
1507 sock_map_unlink(sk, link);
1508 sk_psock_free_link(link);
1509 }
1510 }
1511
sock_map_unhash(struct sock * sk)1512 void sock_map_unhash(struct sock *sk)
1513 {
1514 void (*saved_unhash)(struct sock *sk);
1515 struct sk_psock *psock;
1516
1517 rcu_read_lock();
1518 psock = sk_psock(sk);
1519 if (unlikely(!psock)) {
1520 rcu_read_unlock();
1521 saved_unhash = READ_ONCE(sk->sk_prot)->unhash;
1522 } else {
1523 saved_unhash = psock->saved_unhash;
1524 sock_map_remove_links(sk, psock);
1525 rcu_read_unlock();
1526 }
1527 if (WARN_ON_ONCE(saved_unhash == sock_map_unhash))
1528 return;
1529 if (saved_unhash)
1530 saved_unhash(sk);
1531 }
1532 EXPORT_SYMBOL_GPL(sock_map_unhash);
1533
sock_map_destroy(struct sock * sk)1534 void sock_map_destroy(struct sock *sk)
1535 {
1536 void (*saved_destroy)(struct sock *sk);
1537 struct sk_psock *psock;
1538
1539 rcu_read_lock();
1540 psock = sk_psock_get(sk);
1541 if (unlikely(!psock)) {
1542 rcu_read_unlock();
1543 saved_destroy = READ_ONCE(sk->sk_prot)->destroy;
1544 } else {
1545 saved_destroy = psock->saved_destroy;
1546 sock_map_remove_links(sk, psock);
1547 rcu_read_unlock();
1548 sk_psock_stop(psock);
1549 sk_psock_put(sk, psock);
1550 }
1551 if (WARN_ON_ONCE(saved_destroy == sock_map_destroy))
1552 return;
1553 if (saved_destroy)
1554 saved_destroy(sk);
1555 }
1556 EXPORT_SYMBOL_GPL(sock_map_destroy);
1557
sock_map_close(struct sock * sk,long timeout)1558 void sock_map_close(struct sock *sk, long timeout)
1559 {
1560 void (*saved_close)(struct sock *sk, long timeout);
1561 struct sk_psock *psock;
1562
1563 lock_sock(sk);
1564 rcu_read_lock();
1565 psock = sk_psock_get(sk);
1566 if (unlikely(!psock)) {
1567 rcu_read_unlock();
1568 release_sock(sk);
1569 saved_close = READ_ONCE(sk->sk_prot)->close;
1570 } else {
1571 saved_close = psock->saved_close;
1572 sock_map_remove_links(sk, psock);
1573 rcu_read_unlock();
1574 sk_psock_stop(psock);
1575 release_sock(sk);
1576 cancel_work_sync(&psock->work);
1577 sk_psock_put(sk, psock);
1578 }
1579 /* Make sure we do not recurse. This is a bug.
1580 * Leak the socket instead of crashing on a stack overflow.
1581 */
1582 if (WARN_ON_ONCE(saved_close == sock_map_close))
1583 return;
1584 saved_close(sk, timeout);
1585 }
1586 EXPORT_SYMBOL_GPL(sock_map_close);
1587
sock_map_iter_attach_target(struct bpf_prog * prog,union bpf_iter_link_info * linfo,struct bpf_iter_aux_info * aux)1588 static int sock_map_iter_attach_target(struct bpf_prog *prog,
1589 union bpf_iter_link_info *linfo,
1590 struct bpf_iter_aux_info *aux)
1591 {
1592 struct bpf_map *map;
1593 int err = -EINVAL;
1594
1595 if (!linfo->map.map_fd)
1596 return -EBADF;
1597
1598 map = bpf_map_get_with_uref(linfo->map.map_fd);
1599 if (IS_ERR(map))
1600 return PTR_ERR(map);
1601
1602 if (map->map_type != BPF_MAP_TYPE_SOCKMAP &&
1603 map->map_type != BPF_MAP_TYPE_SOCKHASH)
1604 goto put_map;
1605
1606 if (prog->aux->max_rdonly_access > map->key_size) {
1607 err = -EACCES;
1608 goto put_map;
1609 }
1610
1611 aux->map = map;
1612 return 0;
1613
1614 put_map:
1615 bpf_map_put_with_uref(map);
1616 return err;
1617 }
1618
sock_map_iter_detach_target(struct bpf_iter_aux_info * aux)1619 static void sock_map_iter_detach_target(struct bpf_iter_aux_info *aux)
1620 {
1621 bpf_map_put_with_uref(aux->map);
1622 }
1623
1624 static struct bpf_iter_reg sock_map_iter_reg = {
1625 .target = "sockmap",
1626 .attach_target = sock_map_iter_attach_target,
1627 .detach_target = sock_map_iter_detach_target,
1628 .show_fdinfo = bpf_iter_map_show_fdinfo,
1629 .fill_link_info = bpf_iter_map_fill_link_info,
1630 .ctx_arg_info_size = 2,
1631 .ctx_arg_info = {
1632 { offsetof(struct bpf_iter__sockmap, key),
1633 PTR_TO_BUF | PTR_MAYBE_NULL | MEM_RDONLY },
1634 { offsetof(struct bpf_iter__sockmap, sk),
1635 PTR_TO_BTF_ID_OR_NULL },
1636 },
1637 };
1638
bpf_sockmap_iter_init(void)1639 static int __init bpf_sockmap_iter_init(void)
1640 {
1641 sock_map_iter_reg.ctx_arg_info[1].btf_id =
1642 btf_sock_ids[BTF_SOCK_TYPE_SOCK];
1643 return bpf_iter_reg_target(&sock_map_iter_reg);
1644 }
1645 late_initcall(bpf_sockmap_iter_init);
1646