Skip to content

Commit 0c48eef

Browse files
Cong WangAlexei Starovoitov
authored andcommitted
sock_map: Lift socket state restriction for datagram sockets
TCP and other connection oriented sockets have accept() for each incoming connection on the server side, hence they can just insert those fd's from accept() to sockmap, which are of course established. Now with datagram sockets begin to support sockmap and redirection, the restriction is no longer applicable to them, as they have no accept(). So we have to lift this restriction for them. This is fine, because inside bpf_sk_redirect_map() we still have another socket status check, sock_map_redirect_allowed(), as a guard. This also means they do not have to be removed from sockmap when disconnecting. Signed-off-by: Cong Wang <cong.wang@bytedance.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20210704190252.11866-3-xiyou.wangcong@gmail.com
1 parent 17edea2 commit 0c48eef

File tree

3 files changed

+6
-24
lines changed

3 files changed

+6
-24
lines changed

net/core/sock_map.c

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,6 @@ static struct sk_psock *sock_map_psock_get_checked(struct sock *sk)
211211
return psock;
212212
}
213213

214-
static bool sock_map_redirect_allowed(const struct sock *sk);
215-
216214
static int sock_map_link(struct bpf_map *map, struct sock *sk)
217215
{
218216
struct sk_psock_progs *progs = sock_map_progs(map);
@@ -223,13 +221,6 @@ static int sock_map_link(struct bpf_map *map, struct sock *sk)
223221
struct sk_psock *psock;
224222
int ret;
225223

226-
/* Only sockets we can redirect into/from in BPF need to hold
227-
* refs to parser/verdict progs and have their sk_data_ready
228-
* and sk_write_space callbacks overridden.
229-
*/
230-
if (!sock_map_redirect_allowed(sk))
231-
goto no_progs;
232-
233224
stream_verdict = READ_ONCE(progs->stream_verdict);
234225
if (stream_verdict) {
235226
stream_verdict = bpf_prog_inc_not_zero(stream_verdict);
@@ -264,7 +255,6 @@ static int sock_map_link(struct bpf_map *map, struct sock *sk)
264255
}
265256
}
266257

267-
no_progs:
268258
psock = sock_map_psock_get_checked(sk);
269259
if (IS_ERR(psock)) {
270260
ret = PTR_ERR(psock);
@@ -527,12 +517,6 @@ static bool sk_is_tcp(const struct sock *sk)
527517
sk->sk_protocol == IPPROTO_TCP;
528518
}
529519

530-
static bool sk_is_udp(const struct sock *sk)
531-
{
532-
return sk->sk_type == SOCK_DGRAM &&
533-
sk->sk_protocol == IPPROTO_UDP;
534-
}
535-
536520
static bool sock_map_redirect_allowed(const struct sock *sk)
537521
{
538522
if (sk_is_tcp(sk))
@@ -550,10 +534,7 @@ static bool sock_map_sk_state_allowed(const struct sock *sk)
550534
{
551535
if (sk_is_tcp(sk))
552536
return (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_LISTEN);
553-
else if (sk_is_udp(sk))
554-
return sk_hashed(sk);
555-
556-
return false;
537+
return true;
557538
}
558539

559540
static int sock_hash_update_common(struct bpf_map *map, void *key,

net/ipv4/udp_bpf.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ static struct proto udp_bpf_prots[UDP_BPF_NUM_PROTS];
112112
static void udp_bpf_rebuild_protos(struct proto *prot, const struct proto *base)
113113
{
114114
*prot = *base;
115-
prot->unhash = sock_map_unhash;
116115
prot->close = sock_map_close;
117116
prot->recvmsg = udp_bpf_recvmsg;
118117
}

tools/testing/selftests/bpf/prog_tests/sockmap_listen.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,11 @@ static void test_insert_opened(int family, int sotype, int mapfd)
351351
errno = 0;
352352
value = s;
353353
err = bpf_map_update_elem(mapfd, &key, &value, BPF_NOEXIST);
354-
if (!err || errno != EOPNOTSUPP)
355-
FAIL_ERRNO("map_update: expected EOPNOTSUPP");
356-
354+
if (sotype == SOCK_STREAM) {
355+
if (!err || errno != EOPNOTSUPP)
356+
FAIL_ERRNO("map_update: expected EOPNOTSUPP");
357+
} else if (err)
358+
FAIL_ERRNO("map_update: expected success");
357359
xclose(s);
358360
}
359361

0 commit comments

Comments
 (0)