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
|
#include <linux/bpf.h>
#include <linux/in.h>
#include <bpf/bpf_helpers.h>
#include "xdp_checksum.h"
#include "xdp_manip.h"
#include "xdp_parser.h"
#ifdef XDP_DEBUG
#define dbg_bpf_printk(fmt, args...) bpf_printk(fmt, ##args)
#else
#define dbg_bpf_printk(fmt, args...)
#endif
#include "common.h"
SEC("xdp")
int xdp_udp_handler(struct xdp_md *ctx) {
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
__u32 action = XDP_PASS; /* Default action */
struct hdr_cursor nh = {.pos = data};
int nh_type;
struct ethhdr *ethhdr;
struct iphdr *iphdr = NULL;
struct ipv6hdr *ip6hdr = NULL;
struct udphdr *udphdr;
static const __u8 payload_request[] = {PAYLOAD_REQUEST};
static const __u32 payload_request_size = sizeof(payload_request);
static const __u8 payload_response[] = {PAYLOAD_RESPONSE};
__u32 payload_size;
nh_type = parse_ethhdr(&nh, data_end, ðhdr);
switch (__constant_ntohs(nh_type)) {
case ETH_P_IP:
nh_type = parse_iphdr(&nh, data_end, &iphdr);
break;
case ETH_P_IPV6:
nh_type = parse_ip6hdr(&nh, data_end, &ip6hdr);
break;
default:
goto out;
}
if (nh_type != IPPROTO_UDP) {
goto out;
}
if (parse_udphdr(&nh, data_end, &udphdr) < 0) {
action = XDP_ABORTED;
goto out;
}
if (udphdr->dest != __constant_htons(UDP_PORT)) {
goto out;
}
payload_size = (__u8 *)data_end - (__u8 *)nh.pos;
if (payload_size < payload_request_size) {
goto out;
}
if (nh.pos_u8 + payload_request_size + 1 > (__u8 *)data_end) {
goto out;
}
if (__builtin_memcmp(nh.pos_u8, payload_request, sizeof(payload_request)) !=
0) {
goto out;
}
dbg_bpf_printk("Received UDP Ping Request #%u with size %u", nh.pos_u8[11],
payload_size);
/* Shrink UDP packet for Ping Reply */
if (bpf_xdp_adjust_tail(ctx, payload_request_size - payload_size) != 0) {
return XDP_ABORTED;
}
/* Set data pointers (may not be valid anymore) */
data = (void *)(long)ctx->data;
data_end = (void *)(long)ctx->data_end;
nh.pos = data;
if (parse_ethhdr(&nh, data_end, ðhdr) < 0) {
action = XDP_ABORTED;
goto out;
}
payload_size -= 4;
if (iphdr != NULL) {
if (parse_iphdr(&nh, data_end, &iphdr) < 0) {
action = XDP_ABORTED;
goto out;
}
if (parse_udphdr(&nh, data_end, &udphdr) < 0) {
action = XDP_ABORTED;
goto out;
}
swap_eth_addr(ethhdr);
udphdr->len = __constant_htons(payload_size + sizeof(*udphdr));
iphdr->tot_len =
__constant_htons(iphdr->ihl * 4 + payload_size + sizeof(*udphdr));
swap_ip4_addr(iphdr);
iphdr->check = ip_checksum(iphdr);
if (nh.pos_u8 + sizeof(payload_response) > (__u8 *)data_end) {
action = XDP_ABORTED;
goto out;
}
__builtin_memcpy(nh.pos_u8, payload_response, sizeof(payload_response));
swap_udp_port(udphdr);
udphdr->check = udp_checksum(iphdr, udphdr, data_end);
} else if (ip6hdr != NULL) {
parse_ip6hdr(&nh, data_end, &ip6hdr);
goto out; // TODO: Implement!
} else {
action = XDP_ABORTED;
goto out;
}
action = XDP_TX;
out:
return action;
}
char _license[] SEC("license") =
"GPL"; // Please keep this to prevent tainting kernel
|