xref: /openbmc/linux/tools/testing/selftests/bpf/progs/test_xdp_dynptr.c (revision 1bffcea42926b26e092045ac398850e80d950bb2)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Meta */
3 #include <stddef.h>
4 #include <string.h>
5 #include <linux/bpf.h>
6 #include <linux/if_ether.h>
7 #include <linux/if_packet.h>
8 #include <linux/ip.h>
9 #include <linux/ipv6.h>
10 #include <linux/in.h>
11 #include <linux/udp.h>
12 #include <linux/tcp.h>
13 #include <linux/pkt_cls.h>
14 #include <sys/socket.h>
15 #include <bpf/bpf_helpers.h>
16 #include <bpf/bpf_endian.h>
17 #include "test_iptunnel_common.h"
18 #include "bpf_kfuncs.h"
19 
20 const size_t tcphdr_sz = sizeof(struct tcphdr);
21 const size_t udphdr_sz = sizeof(struct udphdr);
22 const size_t ethhdr_sz = sizeof(struct ethhdr);
23 const size_t iphdr_sz = sizeof(struct iphdr);
24 const size_t ipv6hdr_sz = sizeof(struct ipv6hdr);
25 
26 struct {
27 	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
28 	__uint(max_entries, 256);
29 	__type(key, __u32);
30 	__type(value, __u64);
31 } rxcnt SEC(".maps");
32 
33 struct {
34 	__uint(type, BPF_MAP_TYPE_HASH);
35 	__uint(max_entries, MAX_IPTNL_ENTRIES);
36 	__type(key, struct vip);
37 	__type(value, struct iptnl_info);
38 } vip2tnl SEC(".maps");
39 
40 static __always_inline void count_tx(__u32 protocol)
41 {
42 	__u64 *rxcnt_count;
43 
44 	rxcnt_count = bpf_map_lookup_elem(&rxcnt, &protocol);
45 	if (rxcnt_count)
46 		*rxcnt_count += 1;
47 }
48 
49 static __always_inline int get_dport(void *trans_data, __u8 protocol)
50 {
51 	struct tcphdr *th;
52 	struct udphdr *uh;
53 
54 	switch (protocol) {
55 	case IPPROTO_TCP:
56 		th = (struct tcphdr *)trans_data;
57 		return th->dest;
58 	case IPPROTO_UDP:
59 		uh = (struct udphdr *)trans_data;
60 		return uh->dest;
61 	default:
62 		return 0;
63 	}
64 }
65 
66 static __always_inline void set_ethhdr(struct ethhdr *new_eth,
67 				       const struct ethhdr *old_eth,
68 				       const struct iptnl_info *tnl,
69 				       __be16 h_proto)
70 {
71 	memcpy(new_eth->h_source, old_eth->h_dest, sizeof(new_eth->h_source));
72 	memcpy(new_eth->h_dest, tnl->dmac, sizeof(new_eth->h_dest));
73 	new_eth->h_proto = h_proto;
74 }
75 
76 static __always_inline int handle_ipv4(struct xdp_md *xdp, struct bpf_dynptr *xdp_ptr)
77 {
78 	__u8 eth_buffer[ethhdr_sz + iphdr_sz + ethhdr_sz];
79 	__u8 iph_buffer_tcp[iphdr_sz + tcphdr_sz];
80 	__u8 iph_buffer_udp[iphdr_sz + udphdr_sz];
81 	struct bpf_dynptr new_xdp_ptr;
82 	struct iptnl_info *tnl;
83 	struct ethhdr *new_eth;
84 	struct ethhdr *old_eth;
85 	__u32 transport_hdr_sz;
86 	struct iphdr *iph;
87 	__u16 *next_iph;
88 	__u16 payload_len;
89 	struct vip vip = {};
90 	int dport;
91 	__u32 csum = 0;
92 	int i;
93 
94 	__builtin_memset(eth_buffer, 0, sizeof(eth_buffer));
95 	__builtin_memset(iph_buffer_tcp, 0, sizeof(iph_buffer_tcp));
96 	__builtin_memset(iph_buffer_udp, 0, sizeof(iph_buffer_udp));
97 
98 	if (ethhdr_sz + iphdr_sz + tcphdr_sz > xdp->data_end - xdp->data)
99 		iph = bpf_dynptr_slice(xdp_ptr, ethhdr_sz, iph_buffer_udp, sizeof(iph_buffer_udp));
100 	else
101 		iph = bpf_dynptr_slice(xdp_ptr, ethhdr_sz, iph_buffer_tcp, sizeof(iph_buffer_tcp));
102 
103 	if (!iph)
104 		return XDP_DROP;
105 
106 	dport = get_dport(iph + 1, iph->protocol);
107 	if (dport == -1)
108 		return XDP_DROP;
109 
110 	vip.protocol = iph->protocol;
111 	vip.family = AF_INET;
112 	vip.daddr.v4 = iph->daddr;
113 	vip.dport = dport;
114 	payload_len = bpf_ntohs(iph->tot_len);
115 
116 	tnl = bpf_map_lookup_elem(&vip2tnl, &vip);
117 	/* It only does v4-in-v4 */
118 	if (!tnl || tnl->family != AF_INET)
119 		return XDP_PASS;
120 
121 	if (bpf_xdp_adjust_head(xdp, 0 - (int)iphdr_sz))
122 		return XDP_DROP;
123 
124 	bpf_dynptr_from_xdp(xdp, 0, &new_xdp_ptr);
125 	new_eth = bpf_dynptr_slice_rdwr(&new_xdp_ptr, 0, eth_buffer, sizeof(eth_buffer));
126 	if (!new_eth)
127 		return XDP_DROP;
128 
129 	iph = (struct iphdr *)(new_eth + 1);
130 	old_eth = (struct ethhdr *)(iph + 1);
131 
132 	set_ethhdr(new_eth, old_eth, tnl, bpf_htons(ETH_P_IP));
133 
134 	if (new_eth == eth_buffer)
135 		bpf_dynptr_write(&new_xdp_ptr, 0, eth_buffer, sizeof(eth_buffer), 0);
136 
137 	iph->version = 4;
138 	iph->ihl = iphdr_sz >> 2;
139 	iph->frag_off =	0;
140 	iph->protocol = IPPROTO_IPIP;
141 	iph->check = 0;
142 	iph->tos = 0;
143 	iph->tot_len = bpf_htons(payload_len + iphdr_sz);
144 	iph->daddr = tnl->daddr.v4;
145 	iph->saddr = tnl->saddr.v4;
146 	iph->ttl = 8;
147 
148 	next_iph = (__u16 *)iph;
149 	for (i = 0; i < iphdr_sz >> 1; i++)
150 		csum += *next_iph++;
151 
152 	iph->check = ~((csum & 0xffff) + (csum >> 16));
153 
154 	count_tx(vip.protocol);
155 
156 	return XDP_TX;
157 }
158 
159 static __always_inline int handle_ipv6(struct xdp_md *xdp, struct bpf_dynptr *xdp_ptr)
160 {
161 	__u8 eth_buffer[ethhdr_sz + ipv6hdr_sz + ethhdr_sz];
162 	__u8 ip6h_buffer_tcp[ipv6hdr_sz + tcphdr_sz];
163 	__u8 ip6h_buffer_udp[ipv6hdr_sz + udphdr_sz];
164 	struct bpf_dynptr new_xdp_ptr;
165 	struct iptnl_info *tnl;
166 	struct ethhdr *new_eth;
167 	struct ethhdr *old_eth;
168 	__u32 transport_hdr_sz;
169 	struct ipv6hdr *ip6h;
170 	__u16 payload_len;
171 	struct vip vip = {};
172 	int dport;
173 
174 	__builtin_memset(eth_buffer, 0, sizeof(eth_buffer));
175 	__builtin_memset(ip6h_buffer_tcp, 0, sizeof(ip6h_buffer_tcp));
176 	__builtin_memset(ip6h_buffer_udp, 0, sizeof(ip6h_buffer_udp));
177 
178 	if (ethhdr_sz + iphdr_sz + tcphdr_sz > xdp->data_end - xdp->data)
179 		ip6h = bpf_dynptr_slice(xdp_ptr, ethhdr_sz, ip6h_buffer_udp, sizeof(ip6h_buffer_udp));
180 	else
181 		ip6h = bpf_dynptr_slice(xdp_ptr, ethhdr_sz, ip6h_buffer_tcp, sizeof(ip6h_buffer_tcp));
182 
183 	if (!ip6h)
184 		return XDP_DROP;
185 
186 	dport = get_dport(ip6h + 1, ip6h->nexthdr);
187 	if (dport == -1)
188 		return XDP_DROP;
189 
190 	vip.protocol = ip6h->nexthdr;
191 	vip.family = AF_INET6;
192 	memcpy(vip.daddr.v6, ip6h->daddr.s6_addr32, sizeof(vip.daddr));
193 	vip.dport = dport;
194 	payload_len = ip6h->payload_len;
195 
196 	tnl = bpf_map_lookup_elem(&vip2tnl, &vip);
197 	/* It only does v6-in-v6 */
198 	if (!tnl || tnl->family != AF_INET6)
199 		return XDP_PASS;
200 
201 	if (bpf_xdp_adjust_head(xdp, 0 - (int)ipv6hdr_sz))
202 		return XDP_DROP;
203 
204 	bpf_dynptr_from_xdp(xdp, 0, &new_xdp_ptr);
205 	new_eth = bpf_dynptr_slice_rdwr(&new_xdp_ptr, 0, eth_buffer, sizeof(eth_buffer));
206 	if (!new_eth)
207 		return XDP_DROP;
208 
209 	ip6h = (struct ipv6hdr *)(new_eth + 1);
210 	old_eth = (struct ethhdr *)(ip6h + 1);
211 
212 	set_ethhdr(new_eth, old_eth, tnl, bpf_htons(ETH_P_IPV6));
213 
214 	if (new_eth == eth_buffer)
215 		bpf_dynptr_write(&new_xdp_ptr, 0, eth_buffer, sizeof(eth_buffer), 0);
216 
217 	ip6h->version = 6;
218 	ip6h->priority = 0;
219 	memset(ip6h->flow_lbl, 0, sizeof(ip6h->flow_lbl));
220 	ip6h->payload_len = bpf_htons(bpf_ntohs(payload_len) + ipv6hdr_sz);
221 	ip6h->nexthdr = IPPROTO_IPV6;
222 	ip6h->hop_limit = 8;
223 	memcpy(ip6h->saddr.s6_addr32, tnl->saddr.v6, sizeof(tnl->saddr.v6));
224 	memcpy(ip6h->daddr.s6_addr32, tnl->daddr.v6, sizeof(tnl->daddr.v6));
225 
226 	count_tx(vip.protocol);
227 
228 	return XDP_TX;
229 }
230 
231 SEC("xdp")
232 int _xdp_tx_iptunnel(struct xdp_md *xdp)
233 {
234 	__u8 buffer[ethhdr_sz];
235 	struct bpf_dynptr ptr;
236 	struct ethhdr *eth;
237 	__u16 h_proto;
238 
239 	__builtin_memset(buffer, 0, sizeof(buffer));
240 
241 	bpf_dynptr_from_xdp(xdp, 0, &ptr);
242 	eth = bpf_dynptr_slice(&ptr, 0, buffer, sizeof(buffer));
243 	if (!eth)
244 		return XDP_DROP;
245 
246 	h_proto = eth->h_proto;
247 
248 	if (h_proto == bpf_htons(ETH_P_IP))
249 		return handle_ipv4(xdp, &ptr);
250 	else if (h_proto == bpf_htons(ETH_P_IPV6))
251 
252 		return handle_ipv6(xdp, &ptr);
253 	else
254 		return XDP_DROP;
255 }
256 
257 char _license[] SEC("license") = "GPL";
258