Skip to content

Commit ffdbc35

Browse files
fomichevborkmann
authored andcommitted
bpf: Enable bpf_{g,s}etsockopt in BPF_CGROUP_INET{4,6}_GET{PEER,SOCK}NAME
Those hooks run as BPF_CGROUP_RUN_SA_PROG_LOCK and operate on a locked socket. Signed-off-by: Stanislav Fomichev <sdf@google.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/20210127232853.3753823-3-sdf@google.com
1 parent 7e88fe1 commit ffdbc35

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

net/core/filter.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7025,6 +7025,10 @@ sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
70257025
case BPF_CGROUP_INET6_CONNECT:
70267026
case BPF_CGROUP_UDP4_SENDMSG:
70277027
case BPF_CGROUP_UDP6_SENDMSG:
7028+
case BPF_CGROUP_INET4_GETPEERNAME:
7029+
case BPF_CGROUP_INET6_GETPEERNAME:
7030+
case BPF_CGROUP_INET4_GETSOCKNAME:
7031+
case BPF_CGROUP_INET6_GETSOCKNAME:
70287032
return &bpf_sock_addr_setsockopt_proto;
70297033
default:
70307034
return NULL;
@@ -7037,6 +7041,10 @@ sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
70377041
case BPF_CGROUP_INET6_CONNECT:
70387042
case BPF_CGROUP_UDP4_SENDMSG:
70397043
case BPF_CGROUP_UDP6_SENDMSG:
7044+
case BPF_CGROUP_INET4_GETPEERNAME:
7045+
case BPF_CGROUP_INET6_GETPEERNAME:
7046+
case BPF_CGROUP_INET4_GETSOCKNAME:
7047+
case BPF_CGROUP_INET6_GETSOCKNAME:
70407048
return &bpf_sock_addr_getsockopt_proto;
70417049
default:
70427050
return NULL;

tools/testing/selftests/bpf/progs/connect_force_port4.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <bpf/bpf_helpers.h>
1111
#include <bpf/bpf_endian.h>
1212

13+
#include <bpf_sockopt_helpers.h>
14+
1315
char _license[] SEC("license") = "GPL";
1416
int _version SEC("version") = 1;
1517

@@ -58,6 +60,9 @@ int connect4(struct bpf_sock_addr *ctx)
5860
SEC("cgroup/getsockname4")
5961
int getsockname4(struct bpf_sock_addr *ctx)
6062
{
63+
if (!get_set_sk_priority(ctx))
64+
return 1;
65+
6166
/* Expose local server as 1.2.3.4:60000 to client. */
6267
if (ctx->user_port == bpf_htons(60123)) {
6368
ctx->user_ip4 = bpf_htonl(0x01020304);
@@ -71,6 +76,9 @@ int getpeername4(struct bpf_sock_addr *ctx)
7176
{
7277
struct svc_addr *orig;
7378

79+
if (!get_set_sk_priority(ctx))
80+
return 1;
81+
7482
/* Expose service 1.2.3.4:60000 as peer instead of backend. */
7583
if (ctx->user_port == bpf_htons(60123)) {
7684
orig = bpf_sk_storage_get(&service_mapping, ctx->sk, 0, 0);

tools/testing/selftests/bpf/progs/connect_force_port6.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <bpf/bpf_helpers.h>
1010
#include <bpf/bpf_endian.h>
1111

12+
#include <bpf_sockopt_helpers.h>
13+
1214
char _license[] SEC("license") = "GPL";
1315
int _version SEC("version") = 1;
1416

@@ -63,6 +65,9 @@ int connect6(struct bpf_sock_addr *ctx)
6365
SEC("cgroup/getsockname6")
6466
int getsockname6(struct bpf_sock_addr *ctx)
6567
{
68+
if (!get_set_sk_priority(ctx))
69+
return 1;
70+
6671
/* Expose local server as [fc00::1]:60000 to client. */
6772
if (ctx->user_port == bpf_htons(60124)) {
6873
ctx->user_ip6[0] = bpf_htonl(0xfc000000);
@@ -79,6 +84,9 @@ int getpeername6(struct bpf_sock_addr *ctx)
7984
{
8085
struct svc_addr *orig;
8186

87+
if (!get_set_sk_priority(ctx))
88+
return 1;
89+
8290
/* Expose service [fc00::1]:60000 as peer instead of backend. */
8391
if (ctx->user_port == bpf_htons(60124)) {
8492
orig = bpf_sk_storage_get(&service_mapping, ctx->sk, 0, 0);

0 commit comments

Comments
 (0)