Skip to content

Commit 8ca3037

Browse files
jsitnickiborkmann
authored andcommitted
bpf, sockmap: Allow inserting listening TCP sockets into sockmap
In order for sockmap/sockhash types to become generic collections for storing TCP sockets we need to loosen the checks during map update, while tightening the checks in redirect helpers. Currently sock{map,hash} require the TCP socket to be in established state, which prevents inserting listening sockets. Change the update pre-checks so the socket can also be in listening state. Since it doesn't make sense to redirect with sock{map,hash} to listening sockets, add appropriate socket state checks to BPF redirect helpers too. Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: John Fastabend <john.fastabend@gmail.com> Link: https://lore.kernel.org/bpf/20200218171023.844439-5-jakub@cloudflare.com
1 parent e802515 commit 8ca3037

2 files changed

Lines changed: 45 additions & 20 deletions

File tree

‎net/core/sock_map.c‎

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,8 @@ static int sock_map_update_common(struct bpf_map *map, u32 idx,
391391
static bool sock_map_op_okay(const struct bpf_sock_ops_kern *ops)
392392
{
393393
return ops->op == BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB ||
394-
ops->op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB;
394+
ops->op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB ||
395+
ops->op == BPF_SOCK_OPS_TCP_LISTEN_CB;
395396
}
396397

397398
static bool sock_map_sk_is_suitable(const struct sock *sk)
@@ -400,6 +401,16 @@ static bool sock_map_sk_is_suitable(const struct sock *sk)
400401
sk->sk_protocol == IPPROTO_TCP;
401402
}
402403

404+
static bool sock_map_sk_state_allowed(const struct sock *sk)
405+
{
406+
return (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_LISTEN);
407+
}
408+
409+
static bool sock_map_redirect_allowed(const struct sock *sk)
410+
{
411+
return sk->sk_state != TCP_LISTEN;
412+
}
413+
403414
static int sock_map_update_elem(struct bpf_map *map, void *key,
404415
void *value, u64 flags)
405416
{
@@ -423,7 +434,7 @@ static int sock_map_update_elem(struct bpf_map *map, void *key,
423434
}
424435

425436
sock_map_sk_acquire(sk);
426-
if (sk->sk_state != TCP_ESTABLISHED)
437+
if (!sock_map_sk_state_allowed(sk))
427438
ret = -EOPNOTSUPP;
428439
else
429440
ret = sock_map_update_common(map, idx, sk, flags);
@@ -460,13 +471,17 @@ BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
460471
struct bpf_map *, map, u32, key, u64, flags)
461472
{
462473
struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
474+
struct sock *sk;
463475

464476
if (unlikely(flags & ~(BPF_F_INGRESS)))
465477
return SK_DROP;
466-
tcb->bpf.flags = flags;
467-
tcb->bpf.sk_redir = __sock_map_lookup_elem(map, key);
468-
if (!tcb->bpf.sk_redir)
478+
479+
sk = __sock_map_lookup_elem(map, key);
480+
if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
469481
return SK_DROP;
482+
483+
tcb->bpf.flags = flags;
484+
tcb->bpf.sk_redir = sk;
470485
return SK_PASS;
471486
}
472487

@@ -483,12 +498,17 @@ const struct bpf_func_proto bpf_sk_redirect_map_proto = {
483498
BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
484499
struct bpf_map *, map, u32, key, u64, flags)
485500
{
501+
struct sock *sk;
502+
486503
if (unlikely(flags & ~(BPF_F_INGRESS)))
487504
return SK_DROP;
488-
msg->flags = flags;
489-
msg->sk_redir = __sock_map_lookup_elem(map, key);
490-
if (!msg->sk_redir)
505+
506+
sk = __sock_map_lookup_elem(map, key);
507+
if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
491508
return SK_DROP;
509+
510+
msg->flags = flags;
511+
msg->sk_redir = sk;
492512
return SK_PASS;
493513
}
494514

@@ -748,7 +768,7 @@ static int sock_hash_update_elem(struct bpf_map *map, void *key,
748768
}
749769

750770
sock_map_sk_acquire(sk);
751-
if (sk->sk_state != TCP_ESTABLISHED)
771+
if (!sock_map_sk_state_allowed(sk))
752772
ret = -EOPNOTSUPP;
753773
else
754774
ret = sock_hash_update_common(map, key, sk, flags);
@@ -916,13 +936,17 @@ BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
916936
struct bpf_map *, map, void *, key, u64, flags)
917937
{
918938
struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
939+
struct sock *sk;
919940

920941
if (unlikely(flags & ~(BPF_F_INGRESS)))
921942
return SK_DROP;
922-
tcb->bpf.flags = flags;
923-
tcb->bpf.sk_redir = __sock_hash_lookup_elem(map, key);
924-
if (!tcb->bpf.sk_redir)
943+
944+
sk = __sock_hash_lookup_elem(map, key);
945+
if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
925946
return SK_DROP;
947+
948+
tcb->bpf.flags = flags;
949+
tcb->bpf.sk_redir = sk;
926950
return SK_PASS;
927951
}
928952

@@ -939,12 +963,17 @@ const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
939963
BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
940964
struct bpf_map *, map, void *, key, u64, flags)
941965
{
966+
struct sock *sk;
967+
942968
if (unlikely(flags & ~(BPF_F_INGRESS)))
943969
return SK_DROP;
944-
msg->flags = flags;
945-
msg->sk_redir = __sock_hash_lookup_elem(map, key);
946-
if (!msg->sk_redir)
970+
971+
sk = __sock_hash_lookup_elem(map, key);
972+
if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
947973
return SK_DROP;
974+
975+
msg->flags = flags;
976+
msg->sk_redir = sk;
948977
return SK_PASS;
949978
}
950979

‎tools/testing/selftests/bpf/test_maps.c‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -756,11 +756,7 @@ static void test_sockmap(unsigned int tasks, void *data)
756756
/* Test update without programs */
757757
for (i = 0; i < 6; i++) {
758758
err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
759-
if (i < 2 && !err) {
760-
printf("Allowed update sockmap '%i:%i' not in ESTABLISHED\n",
761-
i, sfd[i]);
762-
goto out_sockmap;
763-
} else if (i >= 2 && err) {
759+
if (err) {
764760
printf("Failed noprog update sockmap '%i:%i'\n",
765761
i, sfd[i]);
766762
goto out_sockmap;

0 commit comments

Comments
 (0)