-
Notifications
You must be signed in to change notification settings - Fork 2
/
xdplua.c
333 lines (260 loc) · 7.57 KB
/
xdplua.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <xdplua.h>
static void switch_eth(struct ethhdr *eth) {
unsigned char source[ETH_ALEN];
memcpy(source, eth->h_source, ETH_ALEN);
memcpy(eth->h_source, eth->h_dest, ETH_ALEN);
memcpy(eth->h_dest, source, ETH_ALEN);
}
static void switch_ip(struct iphdr *iph) {
unsigned int source;
source = iph->saddr;
iph->saddr = iph->daddr;
iph->daddr = source;
}
static void switch_udp(struct udphdr *uh) {
unsigned short source;
source = uh->source;
uh->source = uh->dest;
uh->dest = source;
}
static void switch_tcp(struct tcphdr *tcph) {
unsigned short source;
source = tcph->source;
tcph->source = tcph->dest;
tcph->dest = source;
}
static void handle_link_layer_reply(struct sk_buff *skb) {
struct ethhdr *eth;
eth = (struct ethhdr *) skb_mac_header(skb);
switch_eth(eth);
}
static void handle_network_layer_reply(struct sk_buff *skb, int lendiff) {
struct iphdr *iph;
skb_reset_network_header(skb);
iph = ip_hdr(skb);
switch_ip(iph);
}
static void adjust_iphdrlen(struct iphdr *iph, int lendiff) {
short iphdrlen;
iphdrlen = lendiff;
iph->tot_len = ntohs(iphdrlen);
}
static void handle_ip_checksum(struct iphdr *iph) {
iph->check = 0;
iph->check = ip_fast_csum((unsigned char *) iph, iph->ihl);
}
static void handle_tcp_checksum(struct iphdr *iph, struct tcphdr *tcph) {
int tcplen;
tcplen = tcph->doff * 4;
tcph->check = 0;
tcph->check = csum_tcpudp_magic(iph->saddr, iph->daddr, tcplen,
IPPROTO_TCP, csum_partial((unsigned char *)tcph, tcplen, 0));
}
static int xdplua_tcp_rst_reply(lua_State *L) {
struct iphdr *iph;
struct tcphdr *tcph;
int maclen;
int iplen;
int seqnum;
struct sk_buff *skb = (struct sk_buff *) lua_topointer(L, -1);
maclen = skb->data - skb_mac_header(skb);
handle_link_layer_reply(skb);
handle_network_layer_reply(skb, 0);
iph = ip_hdr(skb);
iplen = iph->ihl * 4;
skb_set_transport_header(skb, iplen);
tcph = tcp_hdr(skb);
switch_tcp(tcph);
adjust_iphdrlen(iph, sizeof(struct tcphdr) + iplen);
handle_ip_checksum(iph);
seqnum = tcph->seq;
tcph->seq = htonl(ntohl(tcph->ack_seq));
tcph->ack_seq = htonl(ntohl(seqnum) + tcph->syn + tcph->fin + skb->len - ip_hdrlen(skb) - (tcph->doff << 2));
tcph->doff = sizeof(struct tcphdr) / 4;
tcph->res1 = 0;
tcph->fin = 0;
tcph->syn = 0;
tcph->rst = 1;
tcph->psh = 0;
tcph->ack = 1;
tcph->urg = 0;
tcph->ece = 0;
tcph->cwr = 0;
tcph->window = 0;
__skb_set_length(skb, iplen + sizeof(struct tcphdr));
handle_tcp_checksum(iph, tcph);
return 0;
}
static int xdplua_udp_reply(lua_State *L) {
int additionallen;
size_t payloadlen;
int maclen;
short iplen;
short udplen;
unsigned char *tmp;
struct iphdr *iph;
struct udphdr *uh;
struct ethhdr *eth;
unsigned char *payload = ldata_topointer(L, 1, &payloadlen);
int payloadoff = luaL_checkinteger(L, 2);
struct sk_buff *skb = (struct sk_buff *) lua_topointer(L, 3);
if (!payload)
return luaL_error(L, "payload NULL");
maclen = skb->data - skb_mac_header(skb);
eth = (struct ethhdr *) skb_mac_header(skb);
switch_eth(eth);
skb_reset_network_header(skb);
iph = ip_hdr(skb);
switch_ip(iph);
payloadoff -= maclen;
additionallen = payloadoff + payloadlen - skb->len;
iplen = htons(iph->tot_len);
iplen += additionallen;
iph->tot_len = ntohs(iplen);
iph->check = 0;
iph->check = ip_fast_csum((unsigned char *) iph, iph->ihl);
if (additionallen != 0) {
if (additionallen > 0) {
if (__skb_grow(skb, payloadoff + payloadlen))
return luaL_error(L, "couldn't expand sk_buff\n");
} else {
__skb_set_length(skb, payloadlen + payloadoff);
}
}
tmp = (unsigned char *) skb_tail_pointer(skb);
tmp -= payloadlen;
memcpy(tmp, payload, payloadlen);
skb_set_transport_header(skb, iph->ihl * 4);
uh = udp_hdr(skb);
switch_udp(uh);
udplen = htons(uh->len);
udplen += additionallen;
uh->len = ntohs(udplen);
uh->check = 0;
uh->check = csum_tcpudp_magic(iph->saddr, iph->daddr, udplen,
IPPROTO_UDP, csum_partial((unsigned char *)uh, udplen, 0));
lua_pushinteger(L, XDP_TX);
return 1;
}
static int xdplua_fib_lookup(lua_State *L) {
struct xdp_rxq_info rxq;
struct sk_buff *skb;
struct xdp_buff ctx;
struct bpf_fib_lookup fib_params;
static const struct bpf_func_proto *fib_lookup_proto;
struct iphdr *iph;
int ret;
unsigned int ipdaddr = luaL_checkinteger(L, 1);
luaU_getregval(L, XDPLUA_SKBENTRY, &skb);
/* work around to use helper */
rxq.dev = skb->dev;
ctx.rxq = &rxq;
fib_lookup_proto = xdp_verifier_ops.get_func_proto(BPF_FUNC_fib_lookup,
NULL);
skb_reset_network_header(skb);
iph = ip_hdr(skb);
fib_params.family = AF_INET;
fib_params.tos = iph->tos;
fib_params.l4_protocol = iph->protocol;
fib_params.sport = 0;
fib_params.dport = 0;
fib_params.tot_len = ntohs(iph->tot_len);
fib_params.ipv4_src = iph->saddr;
fib_params.ipv4_dst = ntohl(ipdaddr);
fib_params.ifindex = skb->dev->ifindex;
ret = fib_lookup_proto->func((u64)&ctx, (u64)&fib_params,
(u64)sizeof(fib_params), (u64)0, (u64)0);
if (!ret) {
struct ethhdr *eth;
struct in_device* in_dev;
struct in_ifaddr* if_info;
struct net_device *fwd;
eth = (struct ethhdr *) skb_mac_header(skb);
memcpy(eth->h_dest, fib_params.dmac, ETH_ALEN);
memcpy(eth->h_source, fib_params.smac, ETH_ALEN);
fwd = dev_get_by_index_rcu(dev_net(skb->dev), fib_params.ifindex);
in_dev = (struct in_device *) fwd->ip_ptr;
if_info = in_dev->ifa_list;
for (;if_info;if_info=if_info->ifa_next) {
if (!(strcmp(if_info->ifa_label, fwd->name))) {
break;
}
}
iph->saddr = if_info->ifa_address;
iph->daddr = ntohs(ipdaddr);
lua_pushinteger(L, fib_params.ifindex);
return 1;
}
return 0;
}
static int xdplua_do_redirect(lua_State *L) {
struct sk_buff *skb;
struct iphdr *iph;
struct udphdr *uh;
struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
unsigned int ifindex = luaL_checkinteger(L, 1);
short udplen;
luaU_getregval(L, XDPLUA_SKBENTRY, &skb);
skb_reset_network_header(skb);
iph = ip_hdr(skb);
iph->check = 0;
iph->check = ip_fast_csum((unsigned char *) iph, iph->ihl);
skb_set_transport_header(skb, iph->ihl * 4);
uh = udp_hdr(skb);
udplen = htons(uh->len);
uh->check = 0;
uh->check = csum_tcpudp_magic(iph->saddr, iph->daddr, udplen,
IPPROTO_UDP, csum_partial((unsigned char *)uh, udplen, 0));
ri->tgt_index = ifindex;
ri->flags = 0;
WRITE_ONCE(ri->map, NULL);
return 0;
}
static int xdplua_get_ifindex(lua_State *L) {
struct sk_buff *skb;
luaU_getregval(L, XDPLUA_SKBENTRY, &skb);
lua_pushinteger(L, skb->dev->ifindex);
return 1;
}
static int xdplua_map_update_elem(lua_State *L) {
const struct bpf_func_proto *map_udpate_proto;
struct bpf_map *map;
int key;
int elem;
int ret;
map = lua_touserdata(L, 1);
key = lua_tointeger(L, 2);
elem = lua_tointeger(L, 3);
ret = CALLHELPER(map_udpate_proto, map_update_elem, map, &key, &elem, 0, 0);
lua_pushinteger(L, ret);
return 1;
}
static int xdplua_map_lookup_elem(lua_State *L) {
static const struct bpf_func_proto *map_lookup_proto;
struct bpf_map *map;
int key;
int *elem;
map = lua_touserdata(L, 1);
key = lua_tointeger(L, 2);
elem = (int *) CALLHELPER(map_lookup_proto, map_lookup_elem, map, &key,
0, 0, 0);
if (!elem)
return luaL_error(L, "unable to lookup element");
lua_pushinteger(L, *elem);
return 1;
}
static const luaL_Reg xdplua_lib[] = {
{"udp_reply", xdplua_udp_reply},
{"tcp_rst_reply", xdplua_tcp_rst_reply},
{"fib_lookup", xdplua_fib_lookup},
{"get_ifindex", xdplua_get_ifindex},
{"do_redirect", xdplua_do_redirect},
{"bpf_map_update_elem", xdplua_map_update_elem},
{"bpf_map_lookup_elem", xdplua_map_lookup_elem},
{NULL, NULL}
};
int luaopen_xdplua(lua_State *L)
{
luaL_newlib(L, xdplua_lib);
return 1;
}