1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 Facebook
6 #include <linux/if_ether.h>
7 #include <linux/if_packet.h>
9 #include <linux/ipv6.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_compiler.h"
21 __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
22 __uint(max_entries, 256);
28 __uint(type, BPF_MAP_TYPE_HASH);
29 __uint(max_entries, MAX_IPTNL_ENTRIES);
30 __type(key, struct vip);
31 __type(value, struct iptnl_info);
32 } vip2tnl SEC(".maps");
34 static __always_inline void count_tx(__u32 protocol)
38 rxcnt_count = bpf_map_lookup_elem(&rxcnt, &protocol);
43 static __always_inline int get_dport(void *trans_data, void *data_end,
51 th = (struct tcphdr *)trans_data;
52 if (th + 1 > data_end)
56 uh = (struct udphdr *)trans_data;
57 if (uh + 1 > data_end)
65 static __always_inline void set_ethhdr(struct ethhdr *new_eth,
66 const struct ethhdr *old_eth,
67 const struct iptnl_info *tnl,
70 memcpy(new_eth->h_source, old_eth->h_dest, sizeof(new_eth->h_source));
71 memcpy(new_eth->h_dest, tnl->dmac, sizeof(new_eth->h_dest));
72 new_eth->h_proto = h_proto;
75 static __always_inline int handle_ipv4(struct xdp_md *xdp)
77 void *data_end = (void *)(long)xdp->data_end;
78 void *data = (void *)(long)xdp->data;
79 struct iptnl_info *tnl;
80 struct ethhdr *new_eth;
81 struct ethhdr *old_eth;
82 struct iphdr *iph = data + sizeof(struct ethhdr);
90 if (iph + 1 > data_end)
93 dport = get_dport(iph + 1, data_end, iph->protocol);
97 vip.protocol = iph->protocol;
99 vip.daddr.v4 = iph->daddr;
101 payload_len = bpf_ntohs(iph->tot_len);
103 tnl = bpf_map_lookup_elem(&vip2tnl, &vip);
104 /* It only does v4-in-v4 */
105 if (!tnl || tnl->family != AF_INET)
108 if (bpf_xdp_adjust_head(xdp, 0 - (int)sizeof(struct iphdr)))
111 data = (void *)(long)xdp->data;
112 data_end = (void *)(long)xdp->data_end;
115 iph = data + sizeof(*new_eth);
116 old_eth = data + sizeof(*iph);
118 if (new_eth + 1 > data_end ||
119 old_eth + 1 > data_end ||
123 set_ethhdr(new_eth, old_eth, tnl, bpf_htons(ETH_P_IP));
126 iph->ihl = sizeof(*iph) >> 2;
128 iph->protocol = IPPROTO_IPIP;
131 iph->tot_len = bpf_htons(payload_len + sizeof(*iph));
132 iph->daddr = tnl->daddr.v4;
133 iph->saddr = tnl->saddr.v4;
136 next_iph = (__u16 *)iph;
137 __pragma_loop_no_unroll
138 for (i = 0; i < sizeof(*iph) >> 1; i++)
141 iph->check = ~((csum & 0xffff) + (csum >> 16));
143 count_tx(vip.protocol);
148 static __always_inline int handle_ipv6(struct xdp_md *xdp)
150 void *data_end = (void *)(long)xdp->data_end;
151 void *data = (void *)(long)xdp->data;
152 struct iptnl_info *tnl;
153 struct ethhdr *new_eth;
154 struct ethhdr *old_eth;
155 struct ipv6hdr *ip6h = data + sizeof(struct ethhdr);
160 if (ip6h + 1 > data_end)
163 dport = get_dport(ip6h + 1, data_end, ip6h->nexthdr);
167 vip.protocol = ip6h->nexthdr;
168 vip.family = AF_INET6;
169 memcpy(vip.daddr.v6, ip6h->daddr.s6_addr32, sizeof(vip.daddr));
171 payload_len = ip6h->payload_len;
173 tnl = bpf_map_lookup_elem(&vip2tnl, &vip);
174 /* It only does v6-in-v6 */
175 if (!tnl || tnl->family != AF_INET6)
178 if (bpf_xdp_adjust_head(xdp, 0 - (int)sizeof(struct ipv6hdr)))
181 data = (void *)(long)xdp->data;
182 data_end = (void *)(long)xdp->data_end;
185 ip6h = data + sizeof(*new_eth);
186 old_eth = data + sizeof(*ip6h);
188 if (new_eth + 1 > data_end || old_eth + 1 > data_end ||
192 set_ethhdr(new_eth, old_eth, tnl, bpf_htons(ETH_P_IPV6));
196 memset(ip6h->flow_lbl, 0, sizeof(ip6h->flow_lbl));
197 ip6h->payload_len = bpf_htons(bpf_ntohs(payload_len) + sizeof(*ip6h));
198 ip6h->nexthdr = IPPROTO_IPV6;
200 memcpy(ip6h->saddr.s6_addr32, tnl->saddr.v6, sizeof(tnl->saddr.v6));
201 memcpy(ip6h->daddr.s6_addr32, tnl->daddr.v6, sizeof(tnl->daddr.v6));
203 count_tx(vip.protocol);
209 int _xdp_tx_iptunnel(struct xdp_md *xdp)
211 void *data_end = (void *)(long)xdp->data_end;
212 void *data = (void *)(long)xdp->data;
213 struct ethhdr *eth = data;
216 if (eth + 1 > data_end)
219 h_proto = eth->h_proto;
221 if (h_proto == bpf_htons(ETH_P_IP))
222 return handle_ipv4(xdp);
223 else if (h_proto == bpf_htons(ETH_P_IPV6))
225 return handle_ipv6(xdp);
230 char _license[] SEC("license") = "GPL";