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 #include "bind_prog.h"
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
26 static __inline int bind_to_device(struct bpf_sock_addr *ctx)
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 int veth1_idx, veth2_idx;
34 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
35 &veth1, sizeof(veth1)))
37 if (bpf_getsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
38 &veth1_idx, sizeof(veth1_idx)) || !veth1_idx)
40 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
41 &veth2, sizeof(veth2)))
43 if (bpf_getsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
44 &veth2_idx, sizeof(veth2_idx)) || !veth2_idx ||
45 veth1_idx == veth2_idx)
47 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
48 &missing, sizeof(missing)) != -ENODEV)
50 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
51 &veth1_idx, sizeof(veth1_idx)))
53 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
54 &del_bind, sizeof(del_bind)))
60 static __inline int bind_reuseport(struct bpf_sock_addr *ctx)
64 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_REUSEPORT,
67 if (bpf_getsockopt(ctx, SOL_SOCKET, SO_REUSEPORT,
68 &val, sizeof(val)) || !val)
71 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_REUSEPORT,
74 if (bpf_getsockopt(ctx, SOL_SOCKET, SO_REUSEPORT,
75 &val, sizeof(val)) || val)
81 static __inline int misc_opts(struct bpf_sock_addr *ctx, int opt)
83 int old, tmp, new = 0xeb9f;
85 /* Socket in test case has guarantee that old never equals to new. */
86 if (bpf_getsockopt(ctx, SOL_SOCKET, opt, &old, sizeof(old)) ||
89 if (bpf_setsockopt(ctx, SOL_SOCKET, opt, &new, sizeof(new)))
91 if (bpf_getsockopt(ctx, SOL_SOCKET, opt, &tmp, sizeof(tmp)) ||
94 if (bpf_setsockopt(ctx, SOL_SOCKET, opt, &old, sizeof(old)))
101 int bind_v4_prog(struct bpf_sock_addr *ctx)
111 if (sk->family != AF_INET)
114 if (ctx->type != SOCK_STREAM && ctx->type != SOCK_DGRAM)
117 if (ctx->user_ip4 != bpf_htonl(SERV4_IP) ||
118 ctx->user_port != bpf_htons(SERV4_PORT))
123 user_ip4 |= load_byte(ctx->user_ip4, 0, sizeof(user_ip4));
124 user_ip4 |= load_byte(ctx->user_ip4, 1, sizeof(user_ip4));
125 user_ip4 |= load_byte(ctx->user_ip4, 2, sizeof(user_ip4));
126 user_ip4 |= load_byte(ctx->user_ip4, 3, sizeof(user_ip4));
127 if (ctx->user_ip4 != user_ip4)
131 user_port |= load_byte(ctx->user_port, 0, sizeof(user_port));
132 user_port |= load_byte(ctx->user_port, 1, sizeof(user_port));
133 if (ctx->user_port != user_port)
138 user_ip4 |= load_word(ctx->user_ip4, 0, sizeof(user_ip4));
139 user_ip4 |= load_word(ctx->user_ip4, 1, sizeof(user_ip4));
140 if (ctx->user_ip4 != user_ip4)
143 /* Bind to device and unbind it. */
144 if (bind_to_device(ctx))
147 /* Test for misc socket options. */
148 if (misc_opts(ctx, SO_MARK) || misc_opts(ctx, SO_PRIORITY))
151 /* Set reuseport and unset */
152 if (bind_reuseport(ctx))
155 ctx->user_ip4 = bpf_htonl(SERV4_REWRITE_IP);
156 ctx->user_port = bpf_htons(SERV4_REWRITE_PORT);
162 int bind_v4_deny_prog(struct bpf_sock_addr *ctx)
167 char _license[] SEC("license") = "GPL";