1 // SPDX-License-Identifier: GPL-2.0
2 #include <vmlinux.h>
3 #include <bpf/bpf_helpers.h>
4 #include <bpf/bpf_endian.h>
5 
6 #define EAFNOSUPPORT 97
7 #define EPROTO 71
8 #define ENONET 64
9 #define EINVAL 22
10 #define ENOENT 2
11 
12 extern unsigned long CONFIG_HZ __kconfig;
13 
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;
26 u32 test_status = 0;
27 u32 test_insert_lookup_mark = 0;
28 int test_snat_addr = -EINVAL;
29 int test_dnat_addr = -EINVAL;
30 __be32 saddr = 0;
31 __be16 sport = 0;
32 __be32 daddr = 0;
33 __be16 dport = 0;
34 int test_exist_lookup = -ENOENT;
35 u32 test_exist_lookup_mark = 0;
36 
37 enum nf_nat_manip_type___local {
38 	NF_NAT_MANIP_SRC___local,
39 	NF_NAT_MANIP_DST___local
40 };
41 
42 struct nf_conn;
43 
44 struct bpf_ct_opts___local {
45 	s32 netns_id;
46 	s32 error;
47 	u8 l4proto;
48 	u8 reserved[3];
49 } __attribute__((preserve_access_index));
50 
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;
67 
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),
73 	   void *ctx)
74 {
75 	struct bpf_ct_opts___local opts_def = { .l4proto = IPPROTO_TCP, .netns_id = -1 };
76 	struct bpf_sock_tuple bpf_tuple;
77 	struct nf_conn *ct;
78 	int err;
79 
80 	__builtin_memset(&bpf_tuple, 0, sizeof(bpf_tuple.ipv4));
81 
82 	ct = lookup_fn(ctx, NULL, 0, &opts_def, sizeof(opts_def));
83 	if (ct)
84 		bpf_ct_release(ct);
85 	else
86 		test_einval_bpf_tuple = opts_def.error;
87 
88 	opts_def.reserved[0] = 1;
89 	ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
90 		       sizeof(opts_def));
91 	opts_def.reserved[0] = 0;
92 	opts_def.l4proto = IPPROTO_TCP;
93 	if (ct)
94 		bpf_ct_release(ct);
95 	else
96 		test_einval_reserved = opts_def.error;
97 
98 	opts_def.netns_id = -2;
99 	ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
100 		       sizeof(opts_def));
101 	opts_def.netns_id = -1;
102 	if (ct)
103 		bpf_ct_release(ct);
104 	else
105 		test_einval_netns_id = opts_def.error;
106 
107 	ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
108 		       sizeof(opts_def) - 1);
109 	if (ct)
110 		bpf_ct_release(ct);
111 	else
112 		test_einval_len_opts = opts_def.error;
113 
114 	opts_def.l4proto = IPPROTO_ICMP;
115 	ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
116 		       sizeof(opts_def));
117 	opts_def.l4proto = IPPROTO_TCP;
118 	if (ct)
119 		bpf_ct_release(ct);
120 	else
121 		test_eproto_l4proto = opts_def.error;
122 
123 	opts_def.netns_id = 0xf00f;
124 	ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
125 		       sizeof(opts_def));
126 	opts_def.netns_id = -1;
127 	if (ct)
128 		bpf_ct_release(ct);
129 	else
130 		test_enonet_netns_id = opts_def.error;
131 
132 	ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
133 		       sizeof(opts_def));
134 	if (ct)
135 		bpf_ct_release(ct);
136 	else
137 		test_enoent_lookup = opts_def.error;
138 
139 	ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4) - 1, &opts_def,
140 		       sizeof(opts_def));
141 	if (ct)
142 		bpf_ct_release(ct);
143 	else
144 		test_eafnosupport = opts_def.error;
145 
146 	bpf_tuple.ipv4.saddr = bpf_get_prandom_u32(); /* src IP */
147 	bpf_tuple.ipv4.daddr = bpf_get_prandom_u32(); /* dst IP */
148 	bpf_tuple.ipv4.sport = bpf_get_prandom_u32(); /* src port */
149 	bpf_tuple.ipv4.dport = bpf_get_prandom_u32(); /* dst port */
150 
151 	ct = alloc_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
152 		      sizeof(opts_def));
153 	if (ct) {
154 		__u16 sport = bpf_get_prandom_u32();
155 		__u16 dport = bpf_get_prandom_u32();
156 		union nf_inet_addr saddr = {};
157 		union nf_inet_addr daddr = {};
158 		struct nf_conn *ct_ins;
159 
160 		bpf_ct_set_timeout(ct, 10000);
161 		ct->mark = 77;
162 
163 		/* snat */
164 		saddr.ip = bpf_get_prandom_u32();
165 		bpf_ct_set_nat_info(ct, &saddr, sport, NF_NAT_MANIP_SRC___local);
166 		/* dnat */
167 		daddr.ip = bpf_get_prandom_u32();
168 		bpf_ct_set_nat_info(ct, &daddr, dport, NF_NAT_MANIP_DST___local);
169 
170 		ct_ins = bpf_ct_insert_entry(ct);
171 		if (ct_ins) {
172 			struct nf_conn *ct_lk;
173 
174 			ct_lk = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4),
175 					  &opts_def, sizeof(opts_def));
176 			if (ct_lk) {
177 				struct nf_conntrack_tuple *tuple;
178 
179 				/* check snat and dnat addresses */
180 				tuple = &ct_lk->tuplehash[IP_CT_DIR_REPLY].tuple;
181 				if (tuple->dst.u3.ip == saddr.ip &&
182 				    tuple->dst.u.all == bpf_htons(sport))
183 					test_snat_addr = 0;
184 				if (tuple->src.u3.ip == daddr.ip &&
185 				    tuple->src.u.all == bpf_htons(dport))
186 					test_dnat_addr = 0;
187 
188 				/* update ct entry timeout */
189 				bpf_ct_change_timeout(ct_lk, 10000);
190 				test_delta_timeout = ct_lk->timeout - bpf_jiffies64();
191 				test_delta_timeout /= CONFIG_HZ;
192 				test_insert_lookup_mark = ct_lk->mark;
193 				bpf_ct_change_status(ct_lk,
194 						     IPS_CONFIRMED | IPS_SEEN_REPLY);
195 				test_status = ct_lk->status;
196 
197 				bpf_ct_release(ct_lk);
198 				test_succ_lookup = 0;
199 			}
200 			bpf_ct_release(ct_ins);
201 			test_insert_entry = 0;
202 		}
203 		test_alloc_entry = 0;
204 	}
205 
206 	bpf_tuple.ipv4.saddr = saddr;
207 	bpf_tuple.ipv4.daddr = daddr;
208 	bpf_tuple.ipv4.sport = sport;
209 	bpf_tuple.ipv4.dport = dport;
210 	ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
211 		       sizeof(opts_def));
212 	if (ct) {
213 		test_exist_lookup = 0;
214 		if (ct->mark == 42) {
215 			ct->mark++;
216 			test_exist_lookup_mark = ct->mark;
217 		}
218 		bpf_ct_release(ct);
219 	} else {
220 		test_exist_lookup = opts_def.error;
221 	}
222 }
223 
224 SEC("xdp")
225 int nf_xdp_ct_test(struct xdp_md *ctx)
226 {
227 	nf_ct_test((void *)bpf_xdp_ct_lookup, (void *)bpf_xdp_ct_alloc, ctx);
228 	return 0;
229 }
230 
231 SEC("tc")
232 int nf_skb_ct_test(struct __sk_buff *ctx)
233 {
234 	nf_ct_test((void *)bpf_skb_ct_lookup, (void *)bpf_skb_ct_alloc, ctx);
235 	return 0;
236 }
237 
238 char _license[] SEC("license") = "GPL";
239