summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/test_bpf_nf.c
blob: f7b330ddd007e8b14fe5cca353e13ce5c6daf663 (plain)
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
334
335
336
337
338
339
340
341
342
343
344
345
346
// SPDX-License-Identifier: GPL-2.0
#define BPF_NO_KFUNC_PROTOTYPES
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>

#define EAFNOSUPPORT 97
#define EPROTO 71
#define ENONET 64
#define EINVAL 22
#define ENOENT 2

#define NF_CT_ZONE_DIR_ORIG (1 << IP_CT_DIR_ORIGINAL)
#define NF_CT_ZONE_DIR_REPL (1 << IP_CT_DIR_REPLY)

extern unsigned long CONFIG_HZ __kconfig;

int test_einval_bpf_tuple = 0;
int test_einval_reserved = 0;
int test_einval_reserved_new = 0;
int test_einval_netns_id = 0;
int test_einval_len_opts = 0;
int test_eproto_l4proto = 0;
int test_enonet_netns_id = 0;
int test_enoent_lookup = 0;
int test_eafnosupport = 0;
int test_alloc_entry = -EINVAL;
int test_insert_entry = -EAFNOSUPPORT;
int test_succ_lookup = -ENOENT;
int test_ct_zone_id_alloc_entry = -EINVAL;
int test_ct_zone_id_insert_entry = -EAFNOSUPPORT;
int test_ct_zone_id_succ_lookup = -ENOENT;
int test_ct_zone_dir_enoent_lookup = 0;
int test_ct_zone_id_enoent_lookup = 0;
u32 test_delta_timeout = 0;
u32 test_status = 0;
u32 test_insert_lookup_mark = 0;
int test_snat_addr = -EINVAL;
int test_dnat_addr = -EINVAL;
__be32 saddr = 0;
__be16 sport = 0;
__be32 daddr = 0;
__be16 dport = 0;
int test_exist_lookup = -ENOENT;
u32 test_exist_lookup_mark = 0;

enum nf_nat_manip_type___local {
	NF_NAT_MANIP_SRC___local,
	NF_NAT_MANIP_DST___local
};

struct nf_conn;

struct bpf_ct_opts___local {
	s32 netns_id;
	s32 error;
	u8 l4proto;
	u8 dir;
	u8 reserved[2];
};

struct bpf_ct_opts___new {
	s32 netns_id;
	s32 error;
	u8 l4proto;
	u8 dir;
	u16 ct_zone_id;
	u8 ct_zone_dir;
	u8 reserved[3];
} __attribute__((preserve_access_index));

struct nf_conn *bpf_xdp_ct_alloc(struct xdp_md *, struct bpf_sock_tuple *, u32,
				 struct bpf_ct_opts___local *, u32) __ksym;
struct nf_conn *bpf_xdp_ct_lookup(struct xdp_md *, struct bpf_sock_tuple *, u32,
				  struct bpf_ct_opts___local *, u32) __ksym;
struct nf_conn *bpf_skb_ct_alloc(struct __sk_buff *, struct bpf_sock_tuple *, u32,
				 struct bpf_ct_opts___local *, u32) __ksym;
struct nf_conn *bpf_skb_ct_lookup(struct __sk_buff *, struct bpf_sock_tuple *, u32,
				  struct bpf_ct_opts___local *, u32) __ksym;
struct nf_conn *bpf_ct_insert_entry(struct nf_conn *) __ksym;
void bpf_ct_release(struct nf_conn *) __ksym;
void bpf_ct_set_timeout(struct nf_conn *, u32) __ksym;
int bpf_ct_change_timeout(struct nf_conn *, u32) __ksym;
int bpf_ct_set_status(struct nf_conn *, u32) __ksym;
int bpf_ct_change_status(struct nf_conn *, u32) __ksym;
int bpf_ct_set_nat_info(struct nf_conn *, union nf_inet_addr *,
			int port, enum nf_nat_manip_type___local) __ksym;

static __always_inline void
nf_ct_test(struct nf_conn *(*lookup_fn)(void *, struct bpf_sock_tuple *, u32,
					struct bpf_ct_opts___local *, u32),
	   struct nf_conn *(*alloc_fn)(void *, struct bpf_sock_tuple *, u32,
				       struct bpf_ct_opts___local *, u32),
	   void *ctx)
{
	struct bpf_ct_opts___local opts_def = { .l4proto = IPPROTO_TCP, .netns_id = -1 };
	struct bpf_sock_tuple bpf_tuple;
	struct nf_conn *ct;

	__builtin_memset(&bpf_tuple, 0, sizeof(bpf_tuple.ipv4));

	ct = lookup_fn(ctx, NULL, 0, &opts_def, sizeof(opts_def));
	if (ct)
		bpf_ct_release(ct);
	else
		test_einval_bpf_tuple = opts_def.error;

	opts_def.reserved[0] = 1;
	ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
		       sizeof(opts_def));
	opts_def.reserved[0] = 0;
	opts_def.l4proto = IPPROTO_TCP;
	if (ct)
		bpf_ct_release(ct);
	else
		test_einval_reserved = opts_def.error;

	opts_def.netns_id = -2;
	ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
		       sizeof(opts_def));
	opts_def.netns_id = -1;
	if (ct)
		bpf_ct_release(ct);
	else
		test_einval_netns_id = opts_def.error;

	ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
		       sizeof(opts_def) - 1);
	if (ct)
		bpf_ct_release(ct);
	else
		test_einval_len_opts = opts_def.error;

	opts_def.l4proto = IPPROTO_ICMP;
	ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
		       sizeof(opts_def));
	opts_def.l4proto = IPPROTO_TCP;
	if (ct)
		bpf_ct_release(ct);
	else
		test_eproto_l4proto = opts_def.error;

	opts_def.netns_id = 0xf00f;
	ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
		       sizeof(opts_def));
	opts_def.netns_id = -1;
	if (ct)
		bpf_ct_release(ct);
	else
		test_enonet_netns_id = opts_def.error;

	ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
		       sizeof(opts_def));
	if (ct)
		bpf_ct_release(ct);
	else
		test_enoent_lookup = opts_def.error;

	ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4) - 1, &opts_def,
		       sizeof(opts_def));
	if (ct)
		bpf_ct_release(ct);
	else
		test_eafnosupport = opts_def.error;

	bpf_tuple.ipv4.saddr = bpf_get_prandom_u32(); /* src IP */
	bpf_tuple.ipv4.daddr = bpf_get_prandom_u32(); /* dst IP */
	bpf_tuple.ipv4.sport = bpf_get_prandom_u32(); /* src port */
	bpf_tuple.ipv4.dport = bpf_get_prandom_u32(); /* dst port */

	ct = alloc_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
		      sizeof(opts_def));
	if (ct) {
		__u16 sport = bpf_get_prandom_u32();
		__u16 dport = bpf_get_prandom_u32();
		union nf_inet_addr saddr = {};
		union nf_inet_addr daddr = {};
		struct nf_conn *ct_ins;

		bpf_ct_set_timeout(ct, 10000);
		ct->mark = 77;

		/* snat */
		saddr.ip = bpf_get_prandom_u32();
		bpf_ct_set_nat_info(ct, &saddr, sport, NF_NAT_MANIP_SRC___local);
		/* dnat */
		daddr.ip = bpf_get_prandom_u32();
		bpf_ct_set_nat_info(ct, &daddr, dport, NF_NAT_MANIP_DST___local);

		ct_ins = bpf_ct_insert_entry(ct);
		if (ct_ins) {
			struct nf_conn *ct_lk;

			ct_lk = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4),
					  &opts_def, sizeof(opts_def));
			if (ct_lk) {
				struct nf_conntrack_tuple *tuple;

				/* check snat and dnat addresses */
				tuple = &ct_lk->tuplehash[IP_CT_DIR_REPLY].tuple;
				if (tuple->dst.u3.ip == saddr.ip &&
				    tuple->dst.u.all == bpf_htons(sport))
					test_snat_addr = 0;
				if (tuple->src.u3.ip == daddr.ip &&
				    tuple->src.u.all == bpf_htons(dport))
					test_dnat_addr = 0;

				/* update ct entry timeout */
				bpf_ct_change_timeout(ct_lk, 10000);
				test_delta_timeout = ct_lk->timeout - bpf_jiffies64();
				test_delta_timeout /= CONFIG_HZ;
				test_insert_lookup_mark = ct_lk->mark;
				bpf_ct_change_status(ct_lk,
						     IPS_CONFIRMED | IPS_SEEN_REPLY);
				test_status = ct_lk->status;

				bpf_ct_release(ct_lk);
				test_succ_lookup = 0;
			}
			bpf_ct_release(ct_ins);
			test_insert_entry = 0;
		}
		test_alloc_entry = 0;
	}

	bpf_tuple.ipv4.saddr = saddr;
	bpf_tuple.ipv4.daddr = daddr;
	bpf_tuple.ipv4.sport = sport;
	bpf_tuple.ipv4.dport = dport;
	ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
		       sizeof(opts_def));
	if (ct) {
		test_exist_lookup = 0;
		if (ct->mark == 42) {
			ct->mark++;
			test_exist_lookup_mark = ct->mark;
		}
		bpf_ct_release(ct);
	} else {
		test_exist_lookup = opts_def.error;
	}
}

static __always_inline void
nf_ct_opts_new_test(struct nf_conn *(*lookup_fn)(void *, struct bpf_sock_tuple *, u32,
						 struct bpf_ct_opts___new *, u32),
		    struct nf_conn *(*alloc_fn)(void *, struct bpf_sock_tuple *, u32,
						struct bpf_ct_opts___new *, u32),
		    void *ctx)
{
	struct bpf_ct_opts___new opts_def = { .l4proto = IPPROTO_TCP, .netns_id = -1 };
	struct bpf_sock_tuple bpf_tuple;
	struct nf_conn *ct;

	__builtin_memset(&bpf_tuple, 0, sizeof(bpf_tuple.ipv4));

	opts_def.reserved[0] = 1;
	ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
		       sizeof(opts_def));
	opts_def.reserved[0] = 0;
	if (ct)
		bpf_ct_release(ct);
	else
		test_einval_reserved_new = opts_def.error;

	bpf_tuple.ipv4.saddr = bpf_get_prandom_u32(); /* src IP */
	bpf_tuple.ipv4.daddr = bpf_get_prandom_u32(); /* dst IP */
	bpf_tuple.ipv4.sport = bpf_get_prandom_u32(); /* src port */
	bpf_tuple.ipv4.dport = bpf_get_prandom_u32(); /* dst port */

	/* use non-default ct zone */
	opts_def.ct_zone_id = 10;
	opts_def.ct_zone_dir = NF_CT_ZONE_DIR_ORIG;
	ct = alloc_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
		      sizeof(opts_def));
	if (ct) {
		__u16 sport = bpf_get_prandom_u32();
		__u16 dport = bpf_get_prandom_u32();
		union nf_inet_addr saddr = {};
		union nf_inet_addr daddr = {};
		struct nf_conn *ct_ins;

		bpf_ct_set_timeout(ct, 10000);

		/* snat */
		saddr.ip = bpf_get_prandom_u32();
		bpf_ct_set_nat_info(ct, &saddr, sport, NF_NAT_MANIP_SRC___local);
		/* dnat */
		daddr.ip = bpf_get_prandom_u32();
		bpf_ct_set_nat_info(ct, &daddr, dport, NF_NAT_MANIP_DST___local);

		ct_ins = bpf_ct_insert_entry(ct);
		if (ct_ins) {
			struct nf_conn *ct_lk;

			/* entry should exist in same ct zone we inserted it */
			ct_lk = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4),
					  &opts_def, sizeof(opts_def));
			if (ct_lk) {
				bpf_ct_release(ct_lk);
				test_ct_zone_id_succ_lookup = 0;
			}

			/* entry should not exist with wrong direction */
			opts_def.ct_zone_dir = NF_CT_ZONE_DIR_REPL;
			ct_lk = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4),
					  &opts_def, sizeof(opts_def));
			opts_def.ct_zone_dir = NF_CT_ZONE_DIR_ORIG;
			if (ct_lk)
				bpf_ct_release(ct_lk);
			else
				test_ct_zone_dir_enoent_lookup = opts_def.error;

			/* entry should not exist in default ct zone */
			opts_def.ct_zone_id = 0;
			ct_lk = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4),
					  &opts_def, sizeof(opts_def));
			if (ct_lk)
				bpf_ct_release(ct_lk);
			else
				test_ct_zone_id_enoent_lookup = opts_def.error;

			bpf_ct_release(ct_ins);
			test_ct_zone_id_insert_entry = 0;
		}
		test_ct_zone_id_alloc_entry = 0;
	}
}

SEC("xdp")
int nf_xdp_ct_test(struct xdp_md *ctx)
{
	nf_ct_test((void *)bpf_xdp_ct_lookup, (void *)bpf_xdp_ct_alloc, ctx);
	nf_ct_opts_new_test((void *)bpf_xdp_ct_lookup, (void *)bpf_xdp_ct_alloc, ctx);
	return 0;
}

SEC("tc")
int nf_skb_ct_test(struct __sk_buff *ctx)
{
	nf_ct_test((void *)bpf_skb_ct_lookup, (void *)bpf_skb_ct_alloc, ctx);
	nf_ct_opts_new_test((void *)bpf_skb_ct_lookup, (void *)bpf_skb_ct_alloc, ctx);
	return 0;
}

char _license[] SEC("license") = "GPL";