1 // SPDX-License-Identifier: GPL-2.0
5 #include <linux/stddef.h>
9 #include <sys/socket.h>
10 #include <netinet/tcp.h>
14 #include <bpf/bpf_helpers.h>
15 #include <bpf/bpf_endian.h>
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
32 static __inline int bind_to_device(struct bpf_sock_addr *ctx)
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 int veth1_idx, veth2_idx;
40 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
41 &veth1, sizeof(veth1)))
43 if (bpf_getsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
44 &veth1_idx, sizeof(veth1_idx)) || !veth1_idx)
46 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
47 &veth2, sizeof(veth2)))
49 if (bpf_getsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
50 &veth2_idx, sizeof(veth2_idx)) || !veth2_idx ||
51 veth1_idx == veth2_idx)
53 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
54 &missing, sizeof(missing)) != -ENODEV)
56 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
57 &veth1_idx, sizeof(veth1_idx)))
59 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
60 &del_bind, sizeof(del_bind)))
66 static __inline int bind_reuseport(struct bpf_sock_addr *ctx)
70 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_REUSEPORT,
73 if (bpf_getsockopt(ctx, SOL_SOCKET, SO_REUSEPORT,
74 &val, sizeof(val)) || !val)
77 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_REUSEPORT,
80 if (bpf_getsockopt(ctx, SOL_SOCKET, SO_REUSEPORT,
81 &val, sizeof(val)) || val)
87 static __inline int misc_opts(struct bpf_sock_addr *ctx, int opt)
89 int old, tmp, new = 0xeb9f;
91 /* Socket in test case has guarantee that old never equals to new. */
92 if (bpf_getsockopt(ctx, SOL_SOCKET, opt, &old, sizeof(old)) ||
95 if (bpf_setsockopt(ctx, SOL_SOCKET, opt, &new, sizeof(new)))
97 if (bpf_getsockopt(ctx, SOL_SOCKET, opt, &tmp, sizeof(tmp)) ||
100 if (bpf_setsockopt(ctx, SOL_SOCKET, opt, &old, sizeof(old)))
107 int bind_v6_prog(struct bpf_sock_addr *ctx)
118 if (sk->family != AF_INET6)
121 if (ctx->type != SOCK_STREAM && ctx->type != SOCK_DGRAM)
124 if (ctx->user_ip6[0] != bpf_htonl(SERV6_IP_0) ||
125 ctx->user_ip6[1] != bpf_htonl(SERV6_IP_1) ||
126 ctx->user_ip6[2] != bpf_htonl(SERV6_IP_2) ||
127 ctx->user_ip6[3] != bpf_htonl(SERV6_IP_3) ||
128 ctx->user_port != bpf_htons(SERV6_PORT))
132 for (i = 0; i < 4; i++) {
134 user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[0] << 0;
135 user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[1] << 8;
136 user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[2] << 16;
137 user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[3] << 24;
138 if (ctx->user_ip6[i] != user_ip6)
143 user_port |= ((volatile __u8 *)&ctx->user_port)[0] << 0;
144 user_port |= ((volatile __u8 *)&ctx->user_port)[1] << 8;
145 if (ctx->user_port != user_port)
149 for (i = 0; i < 4; i++) {
151 user_ip6 |= ((volatile __u16 *)&ctx->user_ip6[i])[0] << 0;
152 user_ip6 |= ((volatile __u16 *)&ctx->user_ip6[i])[1] << 16;
153 if (ctx->user_ip6[i] != user_ip6)
157 /* Bind to device and unbind it. */
158 if (bind_to_device(ctx))
161 /* Test for misc socket options. */
162 if (misc_opts(ctx, SO_MARK) || misc_opts(ctx, SO_PRIORITY))
165 /* Set reuseport and unset */
166 if (bind_reuseport(ctx))
169 ctx->user_ip6[0] = bpf_htonl(SERV6_REWRITE_IP_0);
170 ctx->user_ip6[1] = bpf_htonl(SERV6_REWRITE_IP_1);
171 ctx->user_ip6[2] = bpf_htonl(SERV6_REWRITE_IP_2);
172 ctx->user_ip6[3] = bpf_htonl(SERV6_REWRITE_IP_3);
173 ctx->user_port = bpf_htons(SERV6_REWRITE_PORT);
178 char _license[] SEC("license") = "GPL";