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 	int veth1_idx, veth2_idx;
39 
40 	if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
41 			   &veth1, sizeof(veth1)))
42 		return 1;
43 	if (bpf_getsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
44 			   &veth1_idx, sizeof(veth1_idx)) || !veth1_idx)
45 		return 1;
46 	if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
47 			   &veth2, sizeof(veth2)))
48 		return 1;
49 	if (bpf_getsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
50 			   &veth2_idx, sizeof(veth2_idx)) || !veth2_idx ||
51 	    veth1_idx == veth2_idx)
52 		return 1;
53 	if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
54 			   &missing, sizeof(missing)) != -ENODEV)
55 		return 1;
56 	if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
57 			   &veth1_idx, sizeof(veth1_idx)))
58 		return 1;
59 	if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
60 			   &del_bind, sizeof(del_bind)))
61 		return 1;
62 
63 	return 0;
64 }
65 
66 static __inline int misc_opts(struct bpf_sock_addr *ctx, int opt)
67 {
68 	int old, tmp, new = 0xeb9f;
69 
70 	/* Socket in test case has guarantee that old never equals to new. */
71 	if (bpf_getsockopt(ctx, SOL_SOCKET, opt, &old, sizeof(old)) ||
72 	    old == new)
73 		return 1;
74 	if (bpf_setsockopt(ctx, SOL_SOCKET, opt, &new, sizeof(new)))
75 		return 1;
76 	if (bpf_getsockopt(ctx, SOL_SOCKET, opt, &tmp, sizeof(tmp)) ||
77 	    tmp != new)
78 		return 1;
79 	if (bpf_setsockopt(ctx, SOL_SOCKET, opt, &old, sizeof(old)))
80 		return 1;
81 
82 	return 0;
83 }
84 
85 SEC("cgroup/bind6")
86 int bind_v6_prog(struct bpf_sock_addr *ctx)
87 {
88 	struct bpf_sock *sk;
89 	__u32 user_ip6;
90 	__u16 user_port;
91 	int i;
92 
93 	sk = ctx->sk;
94 	if (!sk)
95 		return 0;
96 
97 	if (sk->family != AF_INET6)
98 		return 0;
99 
100 	if (ctx->type != SOCK_STREAM && ctx->type != SOCK_DGRAM)
101 		return 0;
102 
103 	if (ctx->user_ip6[0] != bpf_htonl(SERV6_IP_0) ||
104 	    ctx->user_ip6[1] != bpf_htonl(SERV6_IP_1) ||
105 	    ctx->user_ip6[2] != bpf_htonl(SERV6_IP_2) ||
106 	    ctx->user_ip6[3] != bpf_htonl(SERV6_IP_3) ||
107 	    ctx->user_port != bpf_htons(SERV6_PORT))
108 		return 0;
109 
110 	// u8 narrow loads:
111 	for (i = 0; i < 4; i++) {
112 		user_ip6 = 0;
113 		user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[0] << 0;
114 		user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[1] << 8;
115 		user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[2] << 16;
116 		user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[3] << 24;
117 		if (ctx->user_ip6[i] != user_ip6)
118 			return 0;
119 	}
120 
121 	user_port = 0;
122 	user_port |= ((volatile __u8 *)&ctx->user_port)[0] << 0;
123 	user_port |= ((volatile __u8 *)&ctx->user_port)[1] << 8;
124 	if (ctx->user_port != user_port)
125 		return 0;
126 
127 	// u16 narrow loads:
128 	for (i = 0; i < 4; i++) {
129 		user_ip6 = 0;
130 		user_ip6 |= ((volatile __u16 *)&ctx->user_ip6[i])[0] << 0;
131 		user_ip6 |= ((volatile __u16 *)&ctx->user_ip6[i])[1] << 16;
132 		if (ctx->user_ip6[i] != user_ip6)
133 			return 0;
134 	}
135 
136 	/* Bind to device and unbind it. */
137 	if (bind_to_device(ctx))
138 		return 0;
139 
140 	/* Test for misc socket options. */
141 	if (misc_opts(ctx, SO_MARK) || misc_opts(ctx, SO_PRIORITY))
142 		return 0;
143 
144 	ctx->user_ip6[0] = bpf_htonl(SERV6_REWRITE_IP_0);
145 	ctx->user_ip6[1] = bpf_htonl(SERV6_REWRITE_IP_1);
146 	ctx->user_ip6[2] = bpf_htonl(SERV6_REWRITE_IP_2);
147 	ctx->user_ip6[3] = bpf_htonl(SERV6_REWRITE_IP_3);
148 	ctx->user_port = bpf_htons(SERV6_REWRITE_PORT);
149 
150 	return 1;
151 }
152 
153 char _license[] SEC("license") = "GPL";
154