forked from xdp-project/xdp-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxdpfilt_prog.h
258 lines (220 loc) · 6.58 KB
/
xdpfilt_prog.h
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
/* SPDX-License-Identifier: GPL-2.0 */
/* XDP filter program fragment. This header file contains the full-featured
* program, split up with ifdefs. The actual program file in xdp_filt_kern.c
* includes this file multiple times with different #defines to create the
* different eBPF program sections that include only the needed features.
*/
#ifndef __XDPFILT_PROG_H
#define __XDPFILT_PROG_H
#include <linux/bpf.h>
#include <linux/in.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>
#include <xdp/xdp_helpers.h>
#include "common_kern_user.h"
/* Defines xdp_stats_map */
#include "xdp/xdp_stats_kern.h"
#include "xdp/parsing_helpers.h"
#ifdef FILT_MODE_DENY
#define VERDICT_HIT XDP_PASS
#define VERDICT_MISS XDP_DROP
#define FEATURE_OPMODE FEAT_DENY
#else
#define VERDICT_HIT XDP_DROP
#define VERDICT_MISS XDP_PASS
#define FEATURE_OPMODE FEAT_ALLOW
#endif
#define CHECK_RET(ret) \
do { \
if ((ret) < 0) { \
action = XDP_ABORTED; \
goto out; \
} \
} while (0)
#define CHECK_VERDICT(type, param) \
do { \
if ((action = lookup_verdict_##type(param)) != VERDICT_MISS) \
goto out; \
} while (0)
#define CHECK_MAP(map, key, mask) \
do { \
__u64 *value; \
value = bpf_map_lookup_elem(map, key); \
if ((value) && (*(value) & (mask)) == (mask)) { \
*value += (1 << COUNTER_SHIFT); \
return VERDICT_HIT; \
} \
} while (0)
#if defined(FILT_MODE_TCP) || defined(FILT_MODE_UDP)
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__uint(max_entries, 65536);
__type(key, __u32);
__type(value, __u64);
__uint(pinning, LIBBPF_PIN_BY_NAME);
} MAP_NAME_PORTS SEC(".maps");
#ifdef FILT_MODE_TCP
static int __always_inline lookup_verdict_tcp(struct tcphdr *tcphdr)
{
__u32 key;
key = tcphdr->dest;
CHECK_MAP(&filter_ports, &key, MAP_FLAG_DST | MAP_FLAG_TCP);
key = tcphdr->source;
CHECK_MAP(&filter_ports, &key, MAP_FLAG_SRC | MAP_FLAG_TCP);
return VERDICT_MISS;
}
#define FEATURE_TCP FEAT_TCP
#else
#define FEATURE_TCP 0
#endif
#ifdef FILT_MODE_UDP
static int __always_inline lookup_verdict_udp(struct udphdr *udphdr)
{
__u32 key;
key = udphdr->dest;
CHECK_MAP(&filter_ports, &key, MAP_FLAG_DST | MAP_FLAG_UDP);
key = udphdr->source;
CHECK_MAP(&filter_ports, &key, MAP_FLAG_SRC | MAP_FLAG_UDP);
return VERDICT_MISS;
}
#define FEATURE_UDP FEAT_UDP
#else
#define FEATURE_UDP 0
#endif
#else
#define FEATURE_UDP 0
#define FEATURE_TCP 0
#endif /* TCP || UDP */
#ifdef FILT_MODE_IPV4
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_HASH);
__uint(max_entries, 10000);
__type(key, __u32);
__type(value, __u64);
__uint(pinning, LIBBPF_PIN_BY_NAME);
} MAP_NAME_IPV4 SEC(".maps");
static int __always_inline lookup_verdict_ipv4(struct iphdr *iphdr)
{
__u32 addr;
addr = iphdr->daddr;
CHECK_MAP(&filter_ipv4, &addr, MAP_FLAG_DST);
addr = iphdr->saddr;
CHECK_MAP(&filter_ipv4, &addr, MAP_FLAG_SRC);
return VERDICT_MISS;
}
#define CHECK_VERDICT_IPV4(param) CHECK_VERDICT(ipv4, param)
#define FEATURE_IPV4 FEAT_IPV4
#else
#define FEATURE_IPV4 0
#define CHECK_VERDICT_IPV4(param)
#endif /* FILT_MODE_IPV4 */
#ifdef FILT_MODE_IPV6
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_HASH);
__uint(max_entries, 10000);
__type(key, struct in6_addr);
__type(value, __u64);
__uint(pinning, LIBBPF_PIN_BY_NAME);
} MAP_NAME_IPV6 SEC(".maps");
static int __always_inline lookup_verdict_ipv6(struct ipv6hdr *ipv6hdr)
{
struct in6_addr addr;
addr = ipv6hdr->daddr;
CHECK_MAP(&filter_ipv6, &addr, MAP_FLAG_DST);
addr = ipv6hdr->saddr;
CHECK_MAP(&filter_ipv6, &addr, MAP_FLAG_SRC);
return VERDICT_MISS;
}
#define CHECK_VERDICT_IPV6(param) CHECK_VERDICT(ipv6, param)
#define FEATURE_IPV6 FEAT_IPV6
#else
#define FEATURE_IPV6 0
#define CHECK_VERDICT_IPV6(param)
#endif /* FILT_MODE_IPV6 */
#ifdef FILT_MODE_ETHERNET
struct ethaddr {
__u8 addr[ETH_ALEN];
};
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_HASH);
__uint(max_entries, 10000);
__type(key, struct ethaddr);
__type(value, __u64);
__uint(pinning, LIBBPF_PIN_BY_NAME);
} MAP_NAME_ETHERNET SEC(".maps");
static int __always_inline lookup_verdict_ethernet(struct ethhdr *eth)
{
struct ethaddr addr = {};
__builtin_memcpy(&addr, eth->h_dest, sizeof(addr));
CHECK_MAP(&filter_ethernet, &addr, MAP_FLAG_DST);
__builtin_memcpy(&addr, eth->h_source, sizeof(addr));
CHECK_MAP(&filter_ethernet, &addr, MAP_FLAG_SRC);
return VERDICT_MISS;
}
#define CHECK_VERDICT_ETHERNET(param) CHECK_VERDICT(ethernet, param)
#define FEATURE_ETHERNET FEAT_ETHERNET
#else
#define FEATURE_ETHERNET 0
#define CHECK_VERDICT_ETHERNET(param)
#endif /* FILT_MODE_ETHERNET */
#ifndef FUNCNAME
#define FUNCNAME xdp_filt_unknown
#endif
struct {
__uint(priority, 10);
__uint(XDP_PASS, 1);
} XDP_RUN_CONFIG(FUNCNAME);
SEC("xdp")
int FUNCNAME(struct xdp_md *ctx)
{
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
__u32 action = VERDICT_MISS; /* Default action */
struct hdr_cursor nh;
struct ethhdr *eth;
int eth_type;
nh.pos = data;
eth_type = parse_ethhdr(&nh, data_end, ð);
CHECK_RET(eth_type);
CHECK_VERDICT_ETHERNET(eth);
#if defined(FILT_MODE_IPV4) || defined(FILT_MODE_IPV6) || \
defined(FILT_MODE_TCP) || defined(FILT_MODE_UDP)
struct iphdr *iphdr;
struct ipv6hdr *ipv6hdr;
int ip_type;
if (eth_type == bpf_htons(ETH_P_IP)) {
ip_type = parse_iphdr(&nh, data_end, &iphdr);
CHECK_RET(ip_type);
CHECK_VERDICT_IPV4(iphdr);
} else if (eth_type == bpf_htons(ETH_P_IPV6)) {
ip_type = parse_ip6hdr(&nh, data_end, &ipv6hdr);
CHECK_RET(ip_type);
CHECK_VERDICT_IPV6(ipv6hdr);
} else {
goto out;
}
#ifdef FILT_MODE_UDP
struct udphdr *udphdr;
if (ip_type == IPPROTO_UDP) {
CHECK_RET(parse_udphdr(&nh, data_end, &udphdr));
CHECK_VERDICT(udp, udphdr);
}
#endif /* FILT_MODE_UDP */
#ifdef FILT_MODE_TCP
struct tcphdr *tcphdr;
if (ip_type == IPPROTO_TCP) {
CHECK_RET(parse_tcphdr(&nh, data_end, &tcphdr));
CHECK_VERDICT(tcp, tcphdr);
}
#endif /* FILT_MODE_TCP*/
#endif /* FILT_MODE_{IPV4,IPV6,TCP,UDP} */
out:
return xdp_stats_record_action(ctx, action);
}
char _license[] SEC("license") = "GPL";
__u32 _features SEC("features") = (FEATURE_ETHERNET | FEATURE_IPV4 |
FEATURE_IPV6 | FEATURE_UDP | FEATURE_TCP |
FEATURE_OPMODE);
#else
#error "Multiple includes of xdpfilt_prog.h"
#endif // include guard