1 // SPDX-License-Identifier: GPL-2.0
5 #include <linux/stddef.h>
12 #include <bpf/bpf_helpers.h>
13 #include <bpf/bpf_endian.h>
15 #define SERV6_IP_0 0xfaceb00c /* face:b00c:1234:5678::abcd */
16 #define SERV6_IP_1 0x12345678
17 #define SERV6_IP_2 0x00000000
18 #define SERV6_IP_3 0x0000abcd
19 #define SERV6_PORT 6060
20 #define SERV6_REWRITE_IP_0 0x00000000
21 #define SERV6_REWRITE_IP_1 0x00000000
22 #define SERV6_REWRITE_IP_2 0x00000000
23 #define SERV6_REWRITE_IP_3 0x00000001
24 #define SERV6_REWRITE_PORT 6666
30 static __inline int bind_to_device(struct bpf_sock_addr *ctx)
32 char veth1[IFNAMSIZ] = "test_sock_addr1";
33 char veth2[IFNAMSIZ] = "test_sock_addr2";
34 char missing[IFNAMSIZ] = "nonexistent_dev";
35 char del_bind[IFNAMSIZ] = "";
36 int veth1_idx, veth2_idx;
38 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
39 &veth1, sizeof(veth1)))
41 if (bpf_getsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
42 &veth1_idx, sizeof(veth1_idx)) || !veth1_idx)
44 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
45 &veth2, sizeof(veth2)))
47 if (bpf_getsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
48 &veth2_idx, sizeof(veth2_idx)) || !veth2_idx ||
49 veth1_idx == veth2_idx)
51 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
52 &missing, sizeof(missing)) != -ENODEV)
54 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
55 &veth1_idx, sizeof(veth1_idx)))
57 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
58 &del_bind, sizeof(del_bind)))
64 static __inline int bind_reuseport(struct bpf_sock_addr *ctx)
68 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_REUSEPORT,
71 if (bpf_getsockopt(ctx, SOL_SOCKET, SO_REUSEPORT,
72 &val, sizeof(val)) || !val)
75 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_REUSEPORT,
78 if (bpf_getsockopt(ctx, SOL_SOCKET, SO_REUSEPORT,
79 &val, sizeof(val)) || val)
85 static __inline int misc_opts(struct bpf_sock_addr *ctx, int opt)
87 int old, tmp, new = 0xeb9f;
89 /* Socket in test case has guarantee that old never equals to new. */
90 if (bpf_getsockopt(ctx, SOL_SOCKET, opt, &old, sizeof(old)) ||
93 if (bpf_setsockopt(ctx, SOL_SOCKET, opt, &new, sizeof(new)))
95 if (bpf_getsockopt(ctx, SOL_SOCKET, opt, &tmp, sizeof(tmp)) ||
98 if (bpf_setsockopt(ctx, SOL_SOCKET, opt, &old, sizeof(old)))
105 int bind_v6_prog(struct bpf_sock_addr *ctx)
116 if (sk->family != AF_INET6)
119 if (ctx->type != SOCK_STREAM && ctx->type != SOCK_DGRAM)
122 if (ctx->user_ip6[0] != bpf_htonl(SERV6_IP_0) ||
123 ctx->user_ip6[1] != bpf_htonl(SERV6_IP_1) ||
124 ctx->user_ip6[2] != bpf_htonl(SERV6_IP_2) ||
125 ctx->user_ip6[3] != bpf_htonl(SERV6_IP_3) ||
126 ctx->user_port != bpf_htons(SERV6_PORT))
130 for (i = 0; i < 4; i++) {
132 user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[0] << 0;
133 user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[1] << 8;
134 user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[2] << 16;
135 user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[3] << 24;
136 if (ctx->user_ip6[i] != user_ip6)
141 user_port |= ((volatile __u8 *)&ctx->user_port)[0] << 0;
142 user_port |= ((volatile __u8 *)&ctx->user_port)[1] << 8;
143 if (ctx->user_port != user_port)
147 for (i = 0; i < 4; i++) {
149 user_ip6 |= ((volatile __u16 *)&ctx->user_ip6[i])[0] << 0;
150 user_ip6 |= ((volatile __u16 *)&ctx->user_ip6[i])[1] << 16;
151 if (ctx->user_ip6[i] != user_ip6)
155 /* Bind to device and unbind it. */
156 if (bind_to_device(ctx))
159 /* Test for misc socket options. */
160 if (misc_opts(ctx, SO_MARK) || misc_opts(ctx, SO_PRIORITY))
163 /* Set reuseport and unset */
164 if (bind_reuseport(ctx))
167 ctx->user_ip6[0] = bpf_htonl(SERV6_REWRITE_IP_0);
168 ctx->user_ip6[1] = bpf_htonl(SERV6_REWRITE_IP_1);
169 ctx->user_ip6[2] = bpf_htonl(SERV6_REWRITE_IP_2);
170 ctx->user_ip6[3] = bpf_htonl(SERV6_REWRITE_IP_3);
171 ctx->user_port = bpf_htons(SERV6_REWRITE_PORT);
176 char _license[] SEC("license") = "GPL";