1 // SPDX-License-Identifier: GPL-2.0
3 #include <bpf/bpf_helpers.h>
4 #include <bpf/bpf_endian.h>
6 #define EAFNOSUPPORT 97
12 extern unsigned long CONFIG_HZ __kconfig;
14 int test_einval_bpf_tuple = 0;
15 int test_einval_reserved = 0;
16 int test_einval_netns_id = 0;
17 int test_einval_len_opts = 0;
18 int test_eproto_l4proto = 0;
19 int test_enonet_netns_id = 0;
20 int test_enoent_lookup = 0;
21 int test_eafnosupport = 0;
22 int test_alloc_entry = -EINVAL;
23 int test_insert_entry = -EAFNOSUPPORT;
24 int test_succ_lookup = -ENOENT;
25 u32 test_delta_timeout = 0;
27 u32 test_insert_lookup_mark = 0;
28 int test_snat_addr = -EINVAL;
29 int test_dnat_addr = -EINVAL;
34 int test_exist_lookup = -ENOENT;
35 u32 test_exist_lookup_mark = 0;
37 enum nf_nat_manip_type___local {
38 NF_NAT_MANIP_SRC___local,
39 NF_NAT_MANIP_DST___local
44 struct bpf_ct_opts___local {
49 } __attribute__((preserve_access_index));
51 struct nf_conn *bpf_xdp_ct_alloc(struct xdp_md *, struct bpf_sock_tuple *, u32,
52 struct bpf_ct_opts___local *, u32) __ksym;
53 struct nf_conn *bpf_xdp_ct_lookup(struct xdp_md *, struct bpf_sock_tuple *, u32,
54 struct bpf_ct_opts___local *, u32) __ksym;
55 struct nf_conn *bpf_skb_ct_alloc(struct __sk_buff *, struct bpf_sock_tuple *, u32,
56 struct bpf_ct_opts___local *, u32) __ksym;
57 struct nf_conn *bpf_skb_ct_lookup(struct __sk_buff *, struct bpf_sock_tuple *, u32,
58 struct bpf_ct_opts___local *, u32) __ksym;
59 struct nf_conn *bpf_ct_insert_entry(struct nf_conn *) __ksym;
60 void bpf_ct_release(struct nf_conn *) __ksym;
61 void bpf_ct_set_timeout(struct nf_conn *, u32) __ksym;
62 int bpf_ct_change_timeout(struct nf_conn *, u32) __ksym;
63 int bpf_ct_set_status(struct nf_conn *, u32) __ksym;
64 int bpf_ct_change_status(struct nf_conn *, u32) __ksym;
65 int bpf_ct_set_nat_info(struct nf_conn *, union nf_inet_addr *,
66 int port, enum nf_nat_manip_type___local) __ksym;
68 static __always_inline void
69 nf_ct_test(struct nf_conn *(*lookup_fn)(void *, struct bpf_sock_tuple *, u32,
70 struct bpf_ct_opts___local *, u32),
71 struct nf_conn *(*alloc_fn)(void *, struct bpf_sock_tuple *, u32,
72 struct bpf_ct_opts___local *, u32),
75 struct bpf_ct_opts___local opts_def = { .l4proto = IPPROTO_TCP, .netns_id = -1 };
76 struct bpf_sock_tuple bpf_tuple;
79 __builtin_memset(&bpf_tuple, 0, sizeof(bpf_tuple.ipv4));
81 ct = lookup_fn(ctx, NULL, 0, &opts_def, sizeof(opts_def));
85 test_einval_bpf_tuple = opts_def.error;
87 opts_def.reserved[0] = 1;
88 ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
90 opts_def.reserved[0] = 0;
91 opts_def.l4proto = IPPROTO_TCP;
95 test_einval_reserved = opts_def.error;
97 opts_def.netns_id = -2;
98 ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
100 opts_def.netns_id = -1;
104 test_einval_netns_id = opts_def.error;
106 ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
107 sizeof(opts_def) - 1);
111 test_einval_len_opts = opts_def.error;
113 opts_def.l4proto = IPPROTO_ICMP;
114 ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
116 opts_def.l4proto = IPPROTO_TCP;
120 test_eproto_l4proto = opts_def.error;
122 opts_def.netns_id = 0xf00f;
123 ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
125 opts_def.netns_id = -1;
129 test_enonet_netns_id = opts_def.error;
131 ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
136 test_enoent_lookup = opts_def.error;
138 ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4) - 1, &opts_def,
143 test_eafnosupport = opts_def.error;
145 bpf_tuple.ipv4.saddr = bpf_get_prandom_u32(); /* src IP */
146 bpf_tuple.ipv4.daddr = bpf_get_prandom_u32(); /* dst IP */
147 bpf_tuple.ipv4.sport = bpf_get_prandom_u32(); /* src port */
148 bpf_tuple.ipv4.dport = bpf_get_prandom_u32(); /* dst port */
150 ct = alloc_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
153 __u16 sport = bpf_get_prandom_u32();
154 __u16 dport = bpf_get_prandom_u32();
155 union nf_inet_addr saddr = {};
156 union nf_inet_addr daddr = {};
157 struct nf_conn *ct_ins;
159 bpf_ct_set_timeout(ct, 10000);
163 saddr.ip = bpf_get_prandom_u32();
164 bpf_ct_set_nat_info(ct, &saddr, sport, NF_NAT_MANIP_SRC___local);
166 daddr.ip = bpf_get_prandom_u32();
167 bpf_ct_set_nat_info(ct, &daddr, dport, NF_NAT_MANIP_DST___local);
169 ct_ins = bpf_ct_insert_entry(ct);
171 struct nf_conn *ct_lk;
173 ct_lk = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4),
174 &opts_def, sizeof(opts_def));
176 struct nf_conntrack_tuple *tuple;
178 /* check snat and dnat addresses */
179 tuple = &ct_lk->tuplehash[IP_CT_DIR_REPLY].tuple;
180 if (tuple->dst.u3.ip == saddr.ip &&
181 tuple->dst.u.all == bpf_htons(sport))
183 if (tuple->src.u3.ip == daddr.ip &&
184 tuple->src.u.all == bpf_htons(dport))
187 /* update ct entry timeout */
188 bpf_ct_change_timeout(ct_lk, 10000);
189 test_delta_timeout = ct_lk->timeout - bpf_jiffies64();
190 test_delta_timeout /= CONFIG_HZ;
191 test_insert_lookup_mark = ct_lk->mark;
192 bpf_ct_change_status(ct_lk,
193 IPS_CONFIRMED | IPS_SEEN_REPLY);
194 test_status = ct_lk->status;
196 bpf_ct_release(ct_lk);
197 test_succ_lookup = 0;
199 bpf_ct_release(ct_ins);
200 test_insert_entry = 0;
202 test_alloc_entry = 0;
205 bpf_tuple.ipv4.saddr = saddr;
206 bpf_tuple.ipv4.daddr = daddr;
207 bpf_tuple.ipv4.sport = sport;
208 bpf_tuple.ipv4.dport = dport;
209 ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
212 test_exist_lookup = 0;
213 if (ct->mark == 42) {
215 test_exist_lookup_mark = ct->mark;
219 test_exist_lookup = opts_def.error;
224 int nf_xdp_ct_test(struct xdp_md *ctx)
226 nf_ct_test((void *)bpf_xdp_ct_lookup, (void *)bpf_xdp_ct_alloc, ctx);
231 int nf_skb_ct_test(struct __sk_buff *ctx)
233 nf_ct_test((void *)bpf_skb_ct_lookup, (void *)bpf_skb_ct_alloc, ctx);
237 char _license[] SEC("license") = "GPL";