-
Notifications
You must be signed in to change notification settings - Fork 1
/
xdp_nat64_kern.c
191 lines (153 loc) · 4.88 KB
/
xdp_nat64_kern.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* SPDX-License-Identifier: GPL-2.0 */
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <linux/bpf.h>
#include <linux/in.h>
#include <linux/in6.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <bpf/bpf_endian.h>
#include <linux/seg6.h>
#include <linux/seg6_local.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define V6_V4_HEADER_DIFF 20
#define V6_ADDRESS_SIZE 16
#define assert_len(target, end) \
if ((void *)(target + 1) > end) \
return XDP_DROP;
#define printk(fmt, ...) \
({ \
char ____fmt[] = fmt; \
bpf_trace_printk(____fmt, sizeof(____fmt), \
##__VA_ARGS__); \
})
static inline unsigned short checksum(unsigned short *buf, int bufsz) {
unsigned long sum = 0;
while (bufsz > 1) {
sum += *buf;
buf++;
bufsz -= 2;
}
if (bufsz == 1) {
sum += *(unsigned char *)buf;
}
sum = (sum & 0xffff) + (sum >> 16);
sum = (sum & 0xffff) + (sum >> 16);
return ~sum;
}
static inline void l2_swap(struct ethhdr *eth) {
__u8 tmp[ETH_ALEN];
memcpy(tmp, eth->h_dest, ETH_ALEN);
memcpy(eth->h_dest, eth->h_source, ETH_ALEN);
memcpy(eth->h_source, tmp, ETH_ALEN);
}
__attribute__((__always_inline__)) static inline int process_ipv4hdr(struct xdp_md *ctx, void *nxt_ptr, struct ethhdr *eth) {
void *data_end = (void *)(long)ctx->data_end;
struct iphdr *ipv4 = (struct iphdr *)nxt_ptr;
assert_len(ipv4, data_end);
if(bpf_xdp_adjust_head(ctx, -(V6_V4_HEADER_DIFF)) != 0) {
return XDP_PASS;
}
void *shift_data = (void *)(long)ctx->data;
void *shift_data_end = (void *)(long)ctx->data_end;
struct ethhdr *shift_eth = shift_data;
assert_len(shift_eth, shift_data_end);
eth = shift_data + V6_V4_HEADER_DIFF;
assert_len(eth, shift_data_end);
memcpy(shift_eth, eth, ETH_HLEN);
shift_eth->h_proto = bpf_htons(ETH_P_IPV6);
l2_swap(shift_eth);
ipv4 = (void *)(eth + 1);
assert_len(ipv4, shift_data_end);
struct ipv6hdr *ipv6 = (struct ipv6hdr *)((void *)(long)ctx->data + sizeof(*shift_eth));
assert_len(ipv6, shift_data_end);
ipv6->version = 6;
ipv6->priority = 0;
ipv6->flow_lbl[0] = 0;
ipv6->flow_lbl[1] = 0;
ipv6->flow_lbl[2] = 0;
ipv6->payload_len = bpf_htons(bpf_ntohs(ipv4->tot_len) - V6_V4_HEADER_DIFF);
ipv6->nexthdr = ipv4->protocol;
ipv6->hop_limit = ipv4->ttl;
__u8 src_u6_addr8[sizeof(ipv6->saddr.in6_u.u6_addr8)] = {
0x00, 0x64,
0xff, 0x9b,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
(ipv4->saddr >> 0) & 0xff, (ipv4->saddr >> 8) & 0xff,
(ipv4->saddr >> 16) & 0xff, (ipv4->saddr >> 24) & 0xff,
};
memcpy(ipv6->saddr.in6_u.u6_addr8, src_u6_addr8, sizeof(src_u6_addr8));
__u8 dst_u6_addr8[sizeof(ipv6->daddr.in6_u.u6_addr8)] = {
0x00, 0x64,
0xff, 0x9b,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
(ipv4->daddr >> 0) & 0xff, (ipv4->daddr >> 8) & 0xff,
(ipv4->daddr >> 16) & 0xff, (ipv4->daddr >> 24) & 0xff,
};
memcpy(ipv6->daddr.in6_u.u6_addr8, dst_u6_addr8, sizeof(dst_u6_addr8));
return XDP_TX;
}
__attribute__((__always_inline__)) static inline int process_ipv6hdr(struct xdp_md *ctx, void *nxt_ptr, struct ethhdr *eth) {
void *data_end = (void *)(long)ctx->data_end;
struct ipv6hdr *ipv6 = (struct ipv6hdr *)nxt_ptr;
assert_len(ipv6, data_end);
struct ethhdr tmp_eth = {};
memcpy(&tmp_eth, eth, ETH_HLEN);
l2_swap(&tmp_eth);
tmp_eth.h_proto = bpf_htons(ETH_P_IP);
struct iphdr tmp_ipv4 = {
.version = 4,
.ihl = 5,
.tos = 0,
.tot_len = bpf_htons(bpf_ntohs(ipv6->payload_len) + V6_V4_HEADER_DIFF),
.id = 0,
.frag_off = bpf_htons(0x01 << 14),
.ttl = ipv6->hop_limit,
.protocol = ipv6->nexthdr,
.saddr = ipv6->saddr.in6_u.u6_addr32[3],
.daddr = ipv6->daddr.in6_u.u6_addr32[3],
.check = 0
};
tmp_ipv4.check = checksum(&tmp_ipv4, sizeof(tmp_ipv4));
if(bpf_xdp_adjust_head(ctx, V6_V4_HEADER_DIFF) != 0) {
return XDP_PASS;
}
void *shift_data = (void *)(long)ctx->data;
void *shift_data_end = (void *)(long)ctx->data_end;
struct ethhdr *shift_eth = shift_data;
assert_len(shift_eth, shift_data_end);
memcpy(shift_eth, &tmp_eth, ETH_HLEN);
struct iphdr *ipv4 = (struct iphdr *)((void *)(long)ctx->data + sizeof(*shift_eth));
assert_len(ipv4, shift_data_end);
memcpy(ipv4, &tmp_ipv4, sizeof(tmp_ipv4));
return XDP_TX;
}
__attribute__((__always_inline__)) static inline int xdp_parse_l2(struct xdp_md *ctx) {
void *data = (void *)(long)ctx->data;
void *data_end = (void *)(long)ctx->data_end;
struct ethhdr *eth = data;
assert_len(eth, data_end);
if(eth->h_proto == bpf_htons(ETH_P_IP)) {
return process_ipv4hdr(ctx, eth + 1, eth);
}
if(eth->h_proto == bpf_htons(ETH_P_IPV6)) {
return process_ipv6hdr(ctx, eth + 1, eth);
}
return XDP_PASS;
}
SEC("xdp")
__attribute__((__always_inline__)) static inline int nat64(struct xdp_md *ctx) {
return xdp_parse_l2(ctx);
}
char _license[] SEC("license") = "GPL";