Skip to content

Commit 9e83f54

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpf: expose bpf_{s,g}etsockopt helpers to bind{4,6} hooks'
Stanislav Fomichev says: ==================== This might be useful for the listener sockets to pre-populate some options. Since those helpers require locked sockets, I'm changing bind hooks to lock/unlock the sockets. This should not cause any performance overhead because at this point there shouldn't be any socket lock contention and the locking/unlocking should be cheap. Also, as part of the series, I convert test_sock_addr bpf assembly into C (and preserve the narrow load tests) to make it easier to extend with th bpf_setsockopt later on. v2: * remove version from bpf programs (Andrii Nakryiko) ==================== Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2 parents ba05817 + a540c81 commit 9e83f54

File tree

7 files changed

+245
-192
lines changed

7 files changed

+245
-192
lines changed

include/linux/bpf-cgroup.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,11 @@ int bpf_percpu_cgroup_storage_update(struct bpf_map *map, void *key,
246246
__ret; \
247247
})
248248

249-
#define BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr) \
250-
BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET4_BIND)
249+
#define BPF_CGROUP_RUN_PROG_INET4_BIND_LOCK(sk, uaddr) \
250+
BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_INET4_BIND, NULL)
251251

252-
#define BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr) \
253-
BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET6_BIND)
252+
#define BPF_CGROUP_RUN_PROG_INET6_BIND_LOCK(sk, uaddr) \
253+
BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_INET6_BIND, NULL)
254254

255255
#define BPF_CGROUP_PRE_CONNECT_ENABLED(sk) (cgroup_bpf_enabled && \
256256
sk->sk_prot->pre_connect)
@@ -434,8 +434,8 @@ static inline int bpf_percpu_cgroup_storage_update(struct bpf_map *map,
434434
#define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk,skb) ({ 0; })
435435
#define BPF_CGROUP_RUN_PROG_INET_SOCK(sk) ({ 0; })
436436
#define BPF_CGROUP_RUN_PROG_INET_SOCK_RELEASE(sk) ({ 0; })
437-
#define BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr) ({ 0; })
438-
#define BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr) ({ 0; })
437+
#define BPF_CGROUP_RUN_PROG_INET4_BIND_LOCK(sk, uaddr) ({ 0; })
438+
#define BPF_CGROUP_RUN_PROG_INET6_BIND_LOCK(sk, uaddr) ({ 0; })
439439
#define BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk) ({ 0; })
440440
#define BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk) ({ 0; })
441441
#define BPF_CGROUP_RUN_PROG_INET4_CONNECT(sk, uaddr) ({ 0; })

net/core/filter.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6995,6 +6995,8 @@ sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
69956995
return &bpf_sk_storage_delete_proto;
69966996
case BPF_FUNC_setsockopt:
69976997
switch (prog->expected_attach_type) {
6998+
case BPF_CGROUP_INET4_BIND:
6999+
case BPF_CGROUP_INET6_BIND:
69987000
case BPF_CGROUP_INET4_CONNECT:
69997001
case BPF_CGROUP_INET6_CONNECT:
70007002
return &bpf_sock_addr_setsockopt_proto;
@@ -7003,6 +7005,8 @@ sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
70037005
}
70047006
case BPF_FUNC_getsockopt:
70057007
switch (prog->expected_attach_type) {
7008+
case BPF_CGROUP_INET4_BIND:
7009+
case BPF_CGROUP_INET6_BIND:
70067010
case BPF_CGROUP_INET4_CONNECT:
70077011
case BPF_CGROUP_INET6_CONNECT:
70087012
return &bpf_sock_addr_getsockopt_proto;

net/ipv4/af_inet.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
450450
/* BPF prog is run before any checks are done so that if the prog
451451
* changes context in a wrong way it will be caught.
452452
*/
453-
err = BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr);
453+
err = BPF_CGROUP_RUN_PROG_INET4_BIND_LOCK(sk, uaddr);
454454
if (err)
455455
return err;
456456

net/ipv6/af_inet6.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
451451
/* BPF prog is run before any checks are done so that if the prog
452452
* changes context in a wrong way it will be caught.
453453
*/
454-
err = BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr);
454+
err = BPF_CGROUP_RUN_PROG_INET6_BIND_LOCK(sk, uaddr);
455455
if (err)
456456
return err;
457457

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <string.h>
4+
5+
#include <linux/stddef.h>
6+
#include <linux/bpf.h>
7+
#include <linux/in.h>
8+
#include <linux/in6.h>
9+
#include <sys/socket.h>
10+
#include <netinet/tcp.h>
11+
#include <linux/if.h>
12+
#include <errno.h>
13+
14+
#include <bpf/bpf_helpers.h>
15+
#include <bpf/bpf_endian.h>
16+
17+
#define SERV4_IP 0xc0a801feU /* 192.168.1.254 */
18+
#define SERV4_PORT 4040
19+
#define SERV4_REWRITE_IP 0x7f000001U /* 127.0.0.1 */
20+
#define SERV4_REWRITE_PORT 4444
21+
22+
#ifndef IFNAMSIZ
23+
#define IFNAMSIZ 16
24+
#endif
25+
26+
static __inline int bind_to_device(struct bpf_sock_addr *ctx)
27+
{
28+
char veth1[IFNAMSIZ] = "test_sock_addr1";
29+
char veth2[IFNAMSIZ] = "test_sock_addr2";
30+
char missing[IFNAMSIZ] = "nonexistent_dev";
31+
char del_bind[IFNAMSIZ] = "";
32+
33+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
34+
&veth1, sizeof(veth1)))
35+
return 1;
36+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
37+
&veth2, sizeof(veth2)))
38+
return 1;
39+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
40+
&missing, sizeof(missing)) != -ENODEV)
41+
return 1;
42+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
43+
&del_bind, sizeof(del_bind)))
44+
return 1;
45+
46+
return 0;
47+
}
48+
49+
SEC("cgroup/bind4")
50+
int bind_v4_prog(struct bpf_sock_addr *ctx)
51+
{
52+
struct bpf_sock *sk;
53+
__u32 user_ip4;
54+
__u16 user_port;
55+
56+
sk = ctx->sk;
57+
if (!sk)
58+
return 0;
59+
60+
if (sk->family != AF_INET)
61+
return 0;
62+
63+
if (ctx->type != SOCK_STREAM && ctx->type != SOCK_DGRAM)
64+
return 0;
65+
66+
if (ctx->user_ip4 != bpf_htonl(SERV4_IP) ||
67+
ctx->user_port != bpf_htons(SERV4_PORT))
68+
return 0;
69+
70+
// u8 narrow loads:
71+
user_ip4 = 0;
72+
user_ip4 |= ((volatile __u8 *)&ctx->user_ip4)[0] << 0;
73+
user_ip4 |= ((volatile __u8 *)&ctx->user_ip4)[1] << 8;
74+
user_ip4 |= ((volatile __u8 *)&ctx->user_ip4)[2] << 16;
75+
user_ip4 |= ((volatile __u8 *)&ctx->user_ip4)[3] << 24;
76+
if (ctx->user_ip4 != user_ip4)
77+
return 0;
78+
79+
user_port = 0;
80+
user_port |= ((volatile __u8 *)&ctx->user_port)[0] << 0;
81+
user_port |= ((volatile __u8 *)&ctx->user_port)[1] << 8;
82+
if (ctx->user_port != user_port)
83+
return 0;
84+
85+
// u16 narrow loads:
86+
user_ip4 = 0;
87+
user_ip4 |= ((volatile __u16 *)&ctx->user_ip4)[0] << 0;
88+
user_ip4 |= ((volatile __u16 *)&ctx->user_ip4)[1] << 16;
89+
if (ctx->user_ip4 != user_ip4)
90+
return 0;
91+
92+
/* Bind to device and unbind it. */
93+
if (bind_to_device(ctx))
94+
return 0;
95+
96+
ctx->user_ip4 = bpf_htonl(SERV4_REWRITE_IP);
97+
ctx->user_port = bpf_htons(SERV4_REWRITE_PORT);
98+
99+
return 1;
100+
}
101+
102+
char _license[] SEC("license") = "GPL";
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <string.h>
4+
5+
#include <linux/stddef.h>
6+
#include <linux/bpf.h>
7+
#include <linux/in.h>
8+
#include <linux/in6.h>
9+
#include <sys/socket.h>
10+
#include <netinet/tcp.h>
11+
#include <linux/if.h>
12+
#include <errno.h>
13+
14+
#include <bpf/bpf_helpers.h>
15+
#include <bpf/bpf_endian.h>
16+
17+
#define SERV6_IP_0 0xfaceb00c /* face:b00c:1234:5678::abcd */
18+
#define SERV6_IP_1 0x12345678
19+
#define SERV6_IP_2 0x00000000
20+
#define SERV6_IP_3 0x0000abcd
21+
#define SERV6_PORT 6060
22+
#define SERV6_REWRITE_IP_0 0x00000000
23+
#define SERV6_REWRITE_IP_1 0x00000000
24+
#define SERV6_REWRITE_IP_2 0x00000000
25+
#define SERV6_REWRITE_IP_3 0x00000001
26+
#define SERV6_REWRITE_PORT 6666
27+
28+
#ifndef IFNAMSIZ
29+
#define IFNAMSIZ 16
30+
#endif
31+
32+
static __inline int bind_to_device(struct bpf_sock_addr *ctx)
33+
{
34+
char veth1[IFNAMSIZ] = "test_sock_addr1";
35+
char veth2[IFNAMSIZ] = "test_sock_addr2";
36+
char missing[IFNAMSIZ] = "nonexistent_dev";
37+
char del_bind[IFNAMSIZ] = "";
38+
39+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
40+
&veth1, sizeof(veth1)))
41+
return 1;
42+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
43+
&veth2, sizeof(veth2)))
44+
return 1;
45+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
46+
&missing, sizeof(missing)) != -ENODEV)
47+
return 1;
48+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
49+
&del_bind, sizeof(del_bind)))
50+
return 1;
51+
52+
return 0;
53+
}
54+
55+
SEC("cgroup/bind6")
56+
int bind_v6_prog(struct bpf_sock_addr *ctx)
57+
{
58+
struct bpf_sock *sk;
59+
__u32 user_ip6;
60+
__u16 user_port;
61+
int i;
62+
63+
sk = ctx->sk;
64+
if (!sk)
65+
return 0;
66+
67+
if (sk->family != AF_INET6)
68+
return 0;
69+
70+
if (ctx->type != SOCK_STREAM && ctx->type != SOCK_DGRAM)
71+
return 0;
72+
73+
if (ctx->user_ip6[0] != bpf_htonl(SERV6_IP_0) ||
74+
ctx->user_ip6[1] != bpf_htonl(SERV6_IP_1) ||
75+
ctx->user_ip6[2] != bpf_htonl(SERV6_IP_2) ||
76+
ctx->user_ip6[3] != bpf_htonl(SERV6_IP_3) ||
77+
ctx->user_port != bpf_htons(SERV6_PORT))
78+
return 0;
79+
80+
// u8 narrow loads:
81+
for (i = 0; i < 4; i++) {
82+
user_ip6 = 0;
83+
user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[0] << 0;
84+
user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[1] << 8;
85+
user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[2] << 16;
86+
user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[3] << 24;
87+
if (ctx->user_ip6[i] != user_ip6)
88+
return 0;
89+
}
90+
91+
user_port = 0;
92+
user_port |= ((volatile __u8 *)&ctx->user_port)[0] << 0;
93+
user_port |= ((volatile __u8 *)&ctx->user_port)[1] << 8;
94+
if (ctx->user_port != user_port)
95+
return 0;
96+
97+
// u16 narrow loads:
98+
for (i = 0; i < 4; i++) {
99+
user_ip6 = 0;
100+
user_ip6 |= ((volatile __u16 *)&ctx->user_ip6[i])[0] << 0;
101+
user_ip6 |= ((volatile __u16 *)&ctx->user_ip6[i])[1] << 16;
102+
if (ctx->user_ip6[i] != user_ip6)
103+
return 0;
104+
}
105+
106+
/* Bind to device and unbind it. */
107+
if (bind_to_device(ctx))
108+
return 0;
109+
110+
ctx->user_ip6[0] = bpf_htonl(SERV6_REWRITE_IP_0);
111+
ctx->user_ip6[1] = bpf_htonl(SERV6_REWRITE_IP_1);
112+
ctx->user_ip6[2] = bpf_htonl(SERV6_REWRITE_IP_2);
113+
ctx->user_ip6[3] = bpf_htonl(SERV6_REWRITE_IP_3);
114+
ctx->user_port = bpf_htons(SERV6_REWRITE_PORT);
115+
116+
return 1;
117+
}
118+
119+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)