187091063SKumar Kartikeya Dwivedi // SPDX-License-Identifier: GPL-2.0
287091063SKumar Kartikeya Dwivedi #include <vmlinux.h>
387091063SKumar Kartikeya Dwivedi #include <bpf/bpf_helpers.h>
4b06b45e8SLorenzo Bianconi #include <bpf/bpf_endian.h>
587091063SKumar Kartikeya Dwivedi 
687091063SKumar Kartikeya Dwivedi #define EAFNOSUPPORT 97
787091063SKumar Kartikeya Dwivedi #define EPROTO 71
887091063SKumar Kartikeya Dwivedi #define ENONET 64
987091063SKumar Kartikeya Dwivedi #define EINVAL 22
1087091063SKumar Kartikeya Dwivedi #define ENOENT 2
1187091063SKumar Kartikeya Dwivedi 
126eb7fba0SLorenzo Bianconi extern unsigned long CONFIG_HZ __kconfig;
136eb7fba0SLorenzo Bianconi 
1487091063SKumar Kartikeya Dwivedi int test_einval_bpf_tuple = 0;
1587091063SKumar Kartikeya Dwivedi int test_einval_reserved = 0;
1687091063SKumar Kartikeya Dwivedi int test_einval_netns_id = 0;
1787091063SKumar Kartikeya Dwivedi int test_einval_len_opts = 0;
1887091063SKumar Kartikeya Dwivedi int test_eproto_l4proto = 0;
1987091063SKumar Kartikeya Dwivedi int test_enonet_netns_id = 0;
2087091063SKumar Kartikeya Dwivedi int test_enoent_lookup = 0;
2187091063SKumar Kartikeya Dwivedi int test_eafnosupport = 0;
226eb7fba0SLorenzo Bianconi int test_alloc_entry = -EINVAL;
236eb7fba0SLorenzo Bianconi int test_insert_entry = -EAFNOSUPPORT;
246eb7fba0SLorenzo Bianconi int test_succ_lookup = -ENOENT;
256eb7fba0SLorenzo Bianconi u32 test_delta_timeout = 0;
266eb7fba0SLorenzo Bianconi u32 test_status = 0;
27e2d75e95SDaniel Xu u32 test_insert_lookup_mark = 0;
28b06b45e8SLorenzo Bianconi int test_snat_addr = -EINVAL;
29b06b45e8SLorenzo Bianconi int test_dnat_addr = -EINVAL;
30e81fbd4cSDaniel Xu __be32 saddr = 0;
31e81fbd4cSDaniel Xu __be16 sport = 0;
32e81fbd4cSDaniel Xu __be32 daddr = 0;
33e81fbd4cSDaniel Xu __be16 dport = 0;
34e81fbd4cSDaniel Xu int test_exist_lookup = -ENOENT;
3599799de2SDaniel Xu u32 test_exist_lookup_mark = 0;
3687091063SKumar Kartikeya Dwivedi 
37*92afc532STiezhu Yang enum nf_nat_manip_type___local {
38*92afc532STiezhu Yang 	NF_NAT_MANIP_SRC___local,
39*92afc532STiezhu Yang 	NF_NAT_MANIP_DST___local
40*92afc532STiezhu Yang };
41*92afc532STiezhu Yang 
421058b6a7SKumar Kartikeya Dwivedi struct nf_conn;
431058b6a7SKumar Kartikeya Dwivedi 
441058b6a7SKumar Kartikeya Dwivedi struct bpf_ct_opts___local {
451058b6a7SKumar Kartikeya Dwivedi 	s32 netns_id;
461058b6a7SKumar Kartikeya Dwivedi 	s32 error;
471058b6a7SKumar Kartikeya Dwivedi 	u8 l4proto;
481058b6a7SKumar Kartikeya Dwivedi 	u8 reserved[3];
491058b6a7SKumar Kartikeya Dwivedi } __attribute__((preserve_access_index));
501058b6a7SKumar Kartikeya Dwivedi 
516eb7fba0SLorenzo Bianconi struct nf_conn *bpf_xdp_ct_alloc(struct xdp_md *, struct bpf_sock_tuple *, u32,
526eb7fba0SLorenzo Bianconi 				 struct bpf_ct_opts___local *, u32) __ksym;
5387091063SKumar Kartikeya Dwivedi struct nf_conn *bpf_xdp_ct_lookup(struct xdp_md *, struct bpf_sock_tuple *, u32,
541058b6a7SKumar Kartikeya Dwivedi 				  struct bpf_ct_opts___local *, u32) __ksym;
556eb7fba0SLorenzo Bianconi struct nf_conn *bpf_skb_ct_alloc(struct __sk_buff *, struct bpf_sock_tuple *, u32,
566eb7fba0SLorenzo Bianconi 				 struct bpf_ct_opts___local *, u32) __ksym;
5787091063SKumar Kartikeya Dwivedi struct nf_conn *bpf_skb_ct_lookup(struct __sk_buff *, struct bpf_sock_tuple *, u32,
581058b6a7SKumar Kartikeya Dwivedi 				  struct bpf_ct_opts___local *, u32) __ksym;
596eb7fba0SLorenzo Bianconi struct nf_conn *bpf_ct_insert_entry(struct nf_conn *) __ksym;
6087091063SKumar Kartikeya Dwivedi void bpf_ct_release(struct nf_conn *) __ksym;
616eb7fba0SLorenzo Bianconi void bpf_ct_set_timeout(struct nf_conn *, u32) __ksym;
626eb7fba0SLorenzo Bianconi int bpf_ct_change_timeout(struct nf_conn *, u32) __ksym;
636eb7fba0SLorenzo Bianconi int bpf_ct_set_status(struct nf_conn *, u32) __ksym;
646eb7fba0SLorenzo Bianconi int bpf_ct_change_status(struct nf_conn *, u32) __ksym;
65b06b45e8SLorenzo Bianconi int bpf_ct_set_nat_info(struct nf_conn *, union nf_inet_addr *,
66*92afc532STiezhu Yang 			int port, enum nf_nat_manip_type___local) __ksym;
6787091063SKumar Kartikeya Dwivedi 
6887091063SKumar Kartikeya Dwivedi 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)696eb7fba0SLorenzo Bianconi nf_ct_test(struct nf_conn *(*lookup_fn)(void *, struct bpf_sock_tuple *, u32,
706eb7fba0SLorenzo Bianconi 					struct bpf_ct_opts___local *, u32),
716eb7fba0SLorenzo Bianconi 	   struct nf_conn *(*alloc_fn)(void *, struct bpf_sock_tuple *, u32,
721058b6a7SKumar Kartikeya Dwivedi 				       struct bpf_ct_opts___local *, u32),
7387091063SKumar Kartikeya Dwivedi 	   void *ctx)
7487091063SKumar Kartikeya Dwivedi {
751058b6a7SKumar Kartikeya Dwivedi 	struct bpf_ct_opts___local opts_def = { .l4proto = IPPROTO_TCP, .netns_id = -1 };
7687091063SKumar Kartikeya Dwivedi 	struct bpf_sock_tuple bpf_tuple;
7787091063SKumar Kartikeya Dwivedi 	struct nf_conn *ct;
7887091063SKumar Kartikeya Dwivedi 
7987091063SKumar Kartikeya Dwivedi 	__builtin_memset(&bpf_tuple, 0, sizeof(bpf_tuple.ipv4));
8087091063SKumar Kartikeya Dwivedi 
816eb7fba0SLorenzo Bianconi 	ct = lookup_fn(ctx, NULL, 0, &opts_def, sizeof(opts_def));
8287091063SKumar Kartikeya Dwivedi 	if (ct)
8387091063SKumar Kartikeya Dwivedi 		bpf_ct_release(ct);
8487091063SKumar Kartikeya Dwivedi 	else
8587091063SKumar Kartikeya Dwivedi 		test_einval_bpf_tuple = opts_def.error;
8687091063SKumar Kartikeya Dwivedi 
8787091063SKumar Kartikeya Dwivedi 	opts_def.reserved[0] = 1;
886eb7fba0SLorenzo Bianconi 	ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
896eb7fba0SLorenzo Bianconi 		       sizeof(opts_def));
9087091063SKumar Kartikeya Dwivedi 	opts_def.reserved[0] = 0;
9187091063SKumar Kartikeya Dwivedi 	opts_def.l4proto = IPPROTO_TCP;
9287091063SKumar Kartikeya Dwivedi 	if (ct)
9387091063SKumar Kartikeya Dwivedi 		bpf_ct_release(ct);
9487091063SKumar Kartikeya Dwivedi 	else
9587091063SKumar Kartikeya Dwivedi 		test_einval_reserved = opts_def.error;
9687091063SKumar Kartikeya Dwivedi 
9787091063SKumar Kartikeya Dwivedi 	opts_def.netns_id = -2;
986eb7fba0SLorenzo Bianconi 	ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
996eb7fba0SLorenzo Bianconi 		       sizeof(opts_def));
10087091063SKumar Kartikeya Dwivedi 	opts_def.netns_id = -1;
10187091063SKumar Kartikeya Dwivedi 	if (ct)
10287091063SKumar Kartikeya Dwivedi 		bpf_ct_release(ct);
10387091063SKumar Kartikeya Dwivedi 	else
10487091063SKumar Kartikeya Dwivedi 		test_einval_netns_id = opts_def.error;
10587091063SKumar Kartikeya Dwivedi 
1066eb7fba0SLorenzo Bianconi 	ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
1076eb7fba0SLorenzo Bianconi 		       sizeof(opts_def) - 1);
10887091063SKumar Kartikeya Dwivedi 	if (ct)
10987091063SKumar Kartikeya Dwivedi 		bpf_ct_release(ct);
11087091063SKumar Kartikeya Dwivedi 	else
11187091063SKumar Kartikeya Dwivedi 		test_einval_len_opts = opts_def.error;
11287091063SKumar Kartikeya Dwivedi 
11387091063SKumar Kartikeya Dwivedi 	opts_def.l4proto = IPPROTO_ICMP;
1146eb7fba0SLorenzo Bianconi 	ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
1156eb7fba0SLorenzo Bianconi 		       sizeof(opts_def));
11687091063SKumar Kartikeya Dwivedi 	opts_def.l4proto = IPPROTO_TCP;
11787091063SKumar Kartikeya Dwivedi 	if (ct)
11887091063SKumar Kartikeya Dwivedi 		bpf_ct_release(ct);
11987091063SKumar Kartikeya Dwivedi 	else
12087091063SKumar Kartikeya Dwivedi 		test_eproto_l4proto = opts_def.error;
12187091063SKumar Kartikeya Dwivedi 
12287091063SKumar Kartikeya Dwivedi 	opts_def.netns_id = 0xf00f;
1236eb7fba0SLorenzo Bianconi 	ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
1246eb7fba0SLorenzo Bianconi 		       sizeof(opts_def));
12587091063SKumar Kartikeya Dwivedi 	opts_def.netns_id = -1;
12687091063SKumar Kartikeya Dwivedi 	if (ct)
12787091063SKumar Kartikeya Dwivedi 		bpf_ct_release(ct);
12887091063SKumar Kartikeya Dwivedi 	else
12987091063SKumar Kartikeya Dwivedi 		test_enonet_netns_id = opts_def.error;
13087091063SKumar Kartikeya Dwivedi 
1316eb7fba0SLorenzo Bianconi 	ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
1326eb7fba0SLorenzo Bianconi 		       sizeof(opts_def));
13387091063SKumar Kartikeya Dwivedi 	if (ct)
13487091063SKumar Kartikeya Dwivedi 		bpf_ct_release(ct);
13587091063SKumar Kartikeya Dwivedi 	else
13687091063SKumar Kartikeya Dwivedi 		test_enoent_lookup = opts_def.error;
13787091063SKumar Kartikeya Dwivedi 
1386eb7fba0SLorenzo Bianconi 	ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4) - 1, &opts_def,
1396eb7fba0SLorenzo Bianconi 		       sizeof(opts_def));
14087091063SKumar Kartikeya Dwivedi 	if (ct)
14187091063SKumar Kartikeya Dwivedi 		bpf_ct_release(ct);
14287091063SKumar Kartikeya Dwivedi 	else
14387091063SKumar Kartikeya Dwivedi 		test_eafnosupport = opts_def.error;
1446eb7fba0SLorenzo Bianconi 
1456eb7fba0SLorenzo Bianconi 	bpf_tuple.ipv4.saddr = bpf_get_prandom_u32(); /* src IP */
1466eb7fba0SLorenzo Bianconi 	bpf_tuple.ipv4.daddr = bpf_get_prandom_u32(); /* dst IP */
1476eb7fba0SLorenzo Bianconi 	bpf_tuple.ipv4.sport = bpf_get_prandom_u32(); /* src port */
1486eb7fba0SLorenzo Bianconi 	bpf_tuple.ipv4.dport = bpf_get_prandom_u32(); /* dst port */
1496eb7fba0SLorenzo Bianconi 
1506eb7fba0SLorenzo Bianconi 	ct = alloc_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
1516eb7fba0SLorenzo Bianconi 		      sizeof(opts_def));
1526eb7fba0SLorenzo Bianconi 	if (ct) {
153b06b45e8SLorenzo Bianconi 		__u16 sport = bpf_get_prandom_u32();
154b06b45e8SLorenzo Bianconi 		__u16 dport = bpf_get_prandom_u32();
155b06b45e8SLorenzo Bianconi 		union nf_inet_addr saddr = {};
156b06b45e8SLorenzo Bianconi 		union nf_inet_addr daddr = {};
1576eb7fba0SLorenzo Bianconi 		struct nf_conn *ct_ins;
1586eb7fba0SLorenzo Bianconi 
1596eb7fba0SLorenzo Bianconi 		bpf_ct_set_timeout(ct, 10000);
160e2d75e95SDaniel Xu 		ct->mark = 77;
1616eb7fba0SLorenzo Bianconi 
162b06b45e8SLorenzo Bianconi 		/* snat */
163b06b45e8SLorenzo Bianconi 		saddr.ip = bpf_get_prandom_u32();
164*92afc532STiezhu Yang 		bpf_ct_set_nat_info(ct, &saddr, sport, NF_NAT_MANIP_SRC___local);
165b06b45e8SLorenzo Bianconi 		/* dnat */
166b06b45e8SLorenzo Bianconi 		daddr.ip = bpf_get_prandom_u32();
167*92afc532STiezhu Yang 		bpf_ct_set_nat_info(ct, &daddr, dport, NF_NAT_MANIP_DST___local);
168b06b45e8SLorenzo Bianconi 
1696eb7fba0SLorenzo Bianconi 		ct_ins = bpf_ct_insert_entry(ct);
1706eb7fba0SLorenzo Bianconi 		if (ct_ins) {
1716eb7fba0SLorenzo Bianconi 			struct nf_conn *ct_lk;
1726eb7fba0SLorenzo Bianconi 
1736eb7fba0SLorenzo Bianconi 			ct_lk = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4),
1746eb7fba0SLorenzo Bianconi 					  &opts_def, sizeof(opts_def));
1756eb7fba0SLorenzo Bianconi 			if (ct_lk) {
176b06b45e8SLorenzo Bianconi 				struct nf_conntrack_tuple *tuple;
177b06b45e8SLorenzo Bianconi 
178b06b45e8SLorenzo Bianconi 				/* check snat and dnat addresses */
179b06b45e8SLorenzo Bianconi 				tuple = &ct_lk->tuplehash[IP_CT_DIR_REPLY].tuple;
180b06b45e8SLorenzo Bianconi 				if (tuple->dst.u3.ip == saddr.ip &&
181b06b45e8SLorenzo Bianconi 				    tuple->dst.u.all == bpf_htons(sport))
182b06b45e8SLorenzo Bianconi 					test_snat_addr = 0;
183b06b45e8SLorenzo Bianconi 				if (tuple->src.u3.ip == daddr.ip &&
184b06b45e8SLorenzo Bianconi 				    tuple->src.u.all == bpf_htons(dport))
185b06b45e8SLorenzo Bianconi 					test_dnat_addr = 0;
186b06b45e8SLorenzo Bianconi 
1876eb7fba0SLorenzo Bianconi 				/* update ct entry timeout */
1886eb7fba0SLorenzo Bianconi 				bpf_ct_change_timeout(ct_lk, 10000);
1896eb7fba0SLorenzo Bianconi 				test_delta_timeout = ct_lk->timeout - bpf_jiffies64();
1906eb7fba0SLorenzo Bianconi 				test_delta_timeout /= CONFIG_HZ;
191e2d75e95SDaniel Xu 				test_insert_lookup_mark = ct_lk->mark;
192f7c946f2SLorenzo Bianconi 				bpf_ct_change_status(ct_lk,
193f7c946f2SLorenzo Bianconi 						     IPS_CONFIRMED | IPS_SEEN_REPLY);
194f7c946f2SLorenzo Bianconi 				test_status = ct_lk->status;
195f7c946f2SLorenzo Bianconi 
1966eb7fba0SLorenzo Bianconi 				bpf_ct_release(ct_lk);
1976eb7fba0SLorenzo Bianconi 				test_succ_lookup = 0;
1986eb7fba0SLorenzo Bianconi 			}
1996eb7fba0SLorenzo Bianconi 			bpf_ct_release(ct_ins);
2006eb7fba0SLorenzo Bianconi 			test_insert_entry = 0;
2016eb7fba0SLorenzo Bianconi 		}
2026eb7fba0SLorenzo Bianconi 		test_alloc_entry = 0;
2036eb7fba0SLorenzo Bianconi 	}
204e81fbd4cSDaniel Xu 
205e81fbd4cSDaniel Xu 	bpf_tuple.ipv4.saddr = saddr;
206e81fbd4cSDaniel Xu 	bpf_tuple.ipv4.daddr = daddr;
207e81fbd4cSDaniel Xu 	bpf_tuple.ipv4.sport = sport;
208e81fbd4cSDaniel Xu 	bpf_tuple.ipv4.dport = dport;
209e81fbd4cSDaniel Xu 	ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
210e81fbd4cSDaniel Xu 		       sizeof(opts_def));
211e81fbd4cSDaniel Xu 	if (ct) {
212e81fbd4cSDaniel Xu 		test_exist_lookup = 0;
213e2d75e95SDaniel Xu 		if (ct->mark == 42) {
214e2d75e95SDaniel Xu 			ct->mark++;
215e2d75e95SDaniel Xu 			test_exist_lookup_mark = ct->mark;
216e2d75e95SDaniel Xu 		}
217e81fbd4cSDaniel Xu 		bpf_ct_release(ct);
218e81fbd4cSDaniel Xu 	} else {
219e81fbd4cSDaniel Xu 		test_exist_lookup = opts_def.error;
220e81fbd4cSDaniel Xu 	}
22187091063SKumar Kartikeya Dwivedi }
22287091063SKumar Kartikeya Dwivedi 
22387091063SKumar Kartikeya Dwivedi SEC("xdp")
nf_xdp_ct_test(struct xdp_md * ctx)22487091063SKumar Kartikeya Dwivedi int nf_xdp_ct_test(struct xdp_md *ctx)
22587091063SKumar Kartikeya Dwivedi {
2266eb7fba0SLorenzo Bianconi 	nf_ct_test((void *)bpf_xdp_ct_lookup, (void *)bpf_xdp_ct_alloc, ctx);
22787091063SKumar Kartikeya Dwivedi 	return 0;
22887091063SKumar Kartikeya Dwivedi }
22987091063SKumar Kartikeya Dwivedi 
23087091063SKumar Kartikeya Dwivedi SEC("tc")
nf_skb_ct_test(struct __sk_buff * ctx)23187091063SKumar Kartikeya Dwivedi int nf_skb_ct_test(struct __sk_buff *ctx)
23287091063SKumar Kartikeya Dwivedi {
2336eb7fba0SLorenzo Bianconi 	nf_ct_test((void *)bpf_skb_ct_lookup, (void *)bpf_skb_ct_alloc, ctx);
23487091063SKumar Kartikeya Dwivedi 	return 0;
23587091063SKumar Kartikeya Dwivedi }
23687091063SKumar Kartikeya Dwivedi 
23787091063SKumar Kartikeya Dwivedi char _license[] SEC("license") = "GPL";
238