xref: /linux/tools/testing/selftests/bpf/progs/test_xdp.c (revision 31d166642c7c601c65eccf0ff2e0afe9a0538be2)
1 /* Copyright (c) 2016,2017 Facebook
2  *
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of version 2 of the GNU General Public
5  * License as published by the Free Software Foundation.
6  */
7 #include <stddef.h>
8 #include <string.h>
9 #include <linux/bpf.h>
10 #include <linux/if_ether.h>
11 #include <linux/if_packet.h>
12 #include <linux/ip.h>
13 #include <linux/ipv6.h>
14 #include <linux/in.h>
15 #include <linux/udp.h>
16 #include <linux/tcp.h>
17 #include <linux/pkt_cls.h>
18 #include <sys/socket.h>
19 #include "bpf_helpers.h"
20 #include "bpf_endian.h"
21 #include "test_iptunnel_common.h"
22 
23 int _version SEC("version") = 1;
24 
25 struct {
26 	__u32 type;
27 	__u32 max_entries;
28 	__u32 *key;
29 	__u64 *value;
30 } rxcnt SEC(".maps") = {
31 	.type = BPF_MAP_TYPE_PERCPU_ARRAY,
32 	.max_entries = 256,
33 };
34 
35 struct {
36 	__u32 type;
37 	__u32 max_entries;
38 	struct vip *key;
39 	struct iptnl_info *value;
40 } vip2tnl SEC(".maps") = {
41 	.type = BPF_MAP_TYPE_HASH,
42 	.max_entries = MAX_IPTNL_ENTRIES,
43 };
44 
45 static __always_inline void count_tx(__u32 protocol)
46 {
47 	__u64 *rxcnt_count;
48 
49 	rxcnt_count = bpf_map_lookup_elem(&rxcnt, &protocol);
50 	if (rxcnt_count)
51 		*rxcnt_count += 1;
52 }
53 
54 static __always_inline int get_dport(void *trans_data, void *data_end,
55 				     __u8 protocol)
56 {
57 	struct tcphdr *th;
58 	struct udphdr *uh;
59 
60 	switch (protocol) {
61 	case IPPROTO_TCP:
62 		th = (struct tcphdr *)trans_data;
63 		if (th + 1 > data_end)
64 			return -1;
65 		return th->dest;
66 	case IPPROTO_UDP:
67 		uh = (struct udphdr *)trans_data;
68 		if (uh + 1 > data_end)
69 			return -1;
70 		return uh->dest;
71 	default:
72 		return 0;
73 	}
74 }
75 
76 static __always_inline void set_ethhdr(struct ethhdr *new_eth,
77 				       const struct ethhdr *old_eth,
78 				       const struct iptnl_info *tnl,
79 				       __be16 h_proto)
80 {
81 	memcpy(new_eth->h_source, old_eth->h_dest, sizeof(new_eth->h_source));
82 	memcpy(new_eth->h_dest, tnl->dmac, sizeof(new_eth->h_dest));
83 	new_eth->h_proto = h_proto;
84 }
85 
86 static __always_inline int handle_ipv4(struct xdp_md *xdp)
87 {
88 	void *data_end = (void *)(long)xdp->data_end;
89 	void *data = (void *)(long)xdp->data;
90 	struct iptnl_info *tnl;
91 	struct ethhdr *new_eth;
92 	struct ethhdr *old_eth;
93 	struct iphdr *iph = data + sizeof(struct ethhdr);
94 	__u16 *next_iph;
95 	__u16 payload_len;
96 	struct vip vip = {};
97 	int dport;
98 	__u32 csum = 0;
99 	int i;
100 
101 	if (iph + 1 > data_end)
102 		return XDP_DROP;
103 
104 	dport = get_dport(iph + 1, data_end, iph->protocol);
105 	if (dport == -1)
106 		return XDP_DROP;
107 
108 	vip.protocol = iph->protocol;
109 	vip.family = AF_INET;
110 	vip.daddr.v4 = iph->daddr;
111 	vip.dport = dport;
112 	payload_len = bpf_ntohs(iph->tot_len);
113 
114 	tnl = bpf_map_lookup_elem(&vip2tnl, &vip);
115 	/* It only does v4-in-v4 */
116 	if (!tnl || tnl->family != AF_INET)
117 		return XDP_PASS;
118 
119 	if (bpf_xdp_adjust_head(xdp, 0 - (int)sizeof(struct iphdr)))
120 		return XDP_DROP;
121 
122 	data = (void *)(long)xdp->data;
123 	data_end = (void *)(long)xdp->data_end;
124 
125 	new_eth = data;
126 	iph = data + sizeof(*new_eth);
127 	old_eth = data + sizeof(*iph);
128 
129 	if (new_eth + 1 > data_end ||
130 	    old_eth + 1 > data_end ||
131 	    iph + 1 > data_end)
132 		return XDP_DROP;
133 
134 	set_ethhdr(new_eth, old_eth, tnl, bpf_htons(ETH_P_IP));
135 
136 	iph->version = 4;
137 	iph->ihl = sizeof(*iph) >> 2;
138 	iph->frag_off =	0;
139 	iph->protocol = IPPROTO_IPIP;
140 	iph->check = 0;
141 	iph->tos = 0;
142 	iph->tot_len = bpf_htons(payload_len + sizeof(*iph));
143 	iph->daddr = tnl->daddr.v4;
144 	iph->saddr = tnl->saddr.v4;
145 	iph->ttl = 8;
146 
147 	next_iph = (__u16 *)iph;
148 #pragma clang loop unroll(full)
149 	for (i = 0; i < sizeof(*iph) >> 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)
160 {
161 	void *data_end = (void *)(long)xdp->data_end;
162 	void *data = (void *)(long)xdp->data;
163 	struct iptnl_info *tnl;
164 	struct ethhdr *new_eth;
165 	struct ethhdr *old_eth;
166 	struct ipv6hdr *ip6h = data + sizeof(struct ethhdr);
167 	__u16 payload_len;
168 	struct vip vip = {};
169 	int dport;
170 
171 	if (ip6h + 1 > data_end)
172 		return XDP_DROP;
173 
174 	dport = get_dport(ip6h + 1, data_end, ip6h->nexthdr);
175 	if (dport == -1)
176 		return XDP_DROP;
177 
178 	vip.protocol = ip6h->nexthdr;
179 	vip.family = AF_INET6;
180 	memcpy(vip.daddr.v6, ip6h->daddr.s6_addr32, sizeof(vip.daddr));
181 	vip.dport = dport;
182 	payload_len = ip6h->payload_len;
183 
184 	tnl = bpf_map_lookup_elem(&vip2tnl, &vip);
185 	/* It only does v6-in-v6 */
186 	if (!tnl || tnl->family != AF_INET6)
187 		return XDP_PASS;
188 
189 	if (bpf_xdp_adjust_head(xdp, 0 - (int)sizeof(struct ipv6hdr)))
190 		return XDP_DROP;
191 
192 	data = (void *)(long)xdp->data;
193 	data_end = (void *)(long)xdp->data_end;
194 
195 	new_eth = data;
196 	ip6h = data + sizeof(*new_eth);
197 	old_eth = data + sizeof(*ip6h);
198 
199 	if (new_eth + 1 > data_end || old_eth + 1 > data_end ||
200 	    ip6h + 1 > data_end)
201 		return XDP_DROP;
202 
203 	set_ethhdr(new_eth, old_eth, tnl, bpf_htons(ETH_P_IPV6));
204 
205 	ip6h->version = 6;
206 	ip6h->priority = 0;
207 	memset(ip6h->flow_lbl, 0, sizeof(ip6h->flow_lbl));
208 	ip6h->payload_len = bpf_htons(bpf_ntohs(payload_len) + sizeof(*ip6h));
209 	ip6h->nexthdr = IPPROTO_IPV6;
210 	ip6h->hop_limit = 8;
211 	memcpy(ip6h->saddr.s6_addr32, tnl->saddr.v6, sizeof(tnl->saddr.v6));
212 	memcpy(ip6h->daddr.s6_addr32, tnl->daddr.v6, sizeof(tnl->daddr.v6));
213 
214 	count_tx(vip.protocol);
215 
216 	return XDP_TX;
217 }
218 
219 SEC("xdp_tx_iptunnel")
220 int _xdp_tx_iptunnel(struct xdp_md *xdp)
221 {
222 	void *data_end = (void *)(long)xdp->data_end;
223 	void *data = (void *)(long)xdp->data;
224 	struct ethhdr *eth = data;
225 	__u16 h_proto;
226 
227 	if (eth + 1 > data_end)
228 		return XDP_DROP;
229 
230 	h_proto = eth->h_proto;
231 
232 	if (h_proto == bpf_htons(ETH_P_IP))
233 		return handle_ipv4(xdp);
234 	else if (h_proto == bpf_htons(ETH_P_IPV6))
235 
236 		return handle_ipv6(xdp);
237 	else
238 		return XDP_DROP;
239 }
240 
241 char _license[] SEC("license") = "GPL";
242