1 /* Copyright (c) 2016,2017 Facebook
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.
10 #include <linux/if_ether.h>
11 #include <linux/if_packet.h>
13 #include <linux/ipv6.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/bpf_helpers.h>
20 #include <bpf/bpf_endian.h>
21 #include "test_iptunnel_common.h"
24 __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
25 __uint(max_entries, 256);
31 __uint(type, BPF_MAP_TYPE_HASH);
32 __uint(max_entries, MAX_IPTNL_ENTRIES);
33 __type(key, struct vip);
34 __type(value, struct iptnl_info);
35 } vip2tnl SEC(".maps");
37 static __always_inline void count_tx(__u32 protocol)
41 rxcnt_count = bpf_map_lookup_elem(&rxcnt, &protocol);
46 static __always_inline int get_dport(void *trans_data, void *data_end,
54 th = (struct tcphdr *)trans_data;
55 if (th + 1 > data_end)
59 uh = (struct udphdr *)trans_data;
60 if (uh + 1 > data_end)
68 static __always_inline void set_ethhdr(struct ethhdr *new_eth,
69 const struct ethhdr *old_eth,
70 const struct iptnl_info *tnl,
73 memcpy(new_eth->h_source, old_eth->h_dest, sizeof(new_eth->h_source));
74 memcpy(new_eth->h_dest, tnl->dmac, sizeof(new_eth->h_dest));
75 new_eth->h_proto = h_proto;
78 static __always_inline int handle_ipv4(struct xdp_md *xdp)
80 void *data_end = (void *)(long)xdp->data_end;
81 void *data = (void *)(long)xdp->data;
82 struct iptnl_info *tnl;
83 struct ethhdr *new_eth;
84 struct ethhdr *old_eth;
85 struct iphdr *iph = data + sizeof(struct ethhdr);
93 if (iph + 1 > data_end)
96 dport = get_dport(iph + 1, data_end, iph->protocol);
100 vip.protocol = iph->protocol;
101 vip.family = AF_INET;
102 vip.daddr.v4 = iph->daddr;
104 payload_len = bpf_ntohs(iph->tot_len);
106 tnl = bpf_map_lookup_elem(&vip2tnl, &vip);
107 /* It only does v4-in-v4 */
108 if (!tnl || tnl->family != AF_INET)
111 if (bpf_xdp_adjust_head(xdp, 0 - (int)sizeof(struct iphdr)))
114 data = (void *)(long)xdp->data;
115 data_end = (void *)(long)xdp->data_end;
118 iph = data + sizeof(*new_eth);
119 old_eth = data + sizeof(*iph);
121 if (new_eth + 1 > data_end ||
122 old_eth + 1 > data_end ||
126 set_ethhdr(new_eth, old_eth, tnl, bpf_htons(ETH_P_IP));
129 iph->ihl = sizeof(*iph) >> 2;
131 iph->protocol = IPPROTO_IPIP;
134 iph->tot_len = bpf_htons(payload_len + sizeof(*iph));
135 iph->daddr = tnl->daddr.v4;
136 iph->saddr = tnl->saddr.v4;
139 next_iph = (__u16 *)iph;
140 #pragma clang loop unroll(full)
141 for (i = 0; i < sizeof(*iph) >> 1; i++)
144 iph->check = ~((csum & 0xffff) + (csum >> 16));
146 count_tx(vip.protocol);
151 static __always_inline int handle_ipv6(struct xdp_md *xdp)
153 void *data_end = (void *)(long)xdp->data_end;
154 void *data = (void *)(long)xdp->data;
155 struct iptnl_info *tnl;
156 struct ethhdr *new_eth;
157 struct ethhdr *old_eth;
158 struct ipv6hdr *ip6h = data + sizeof(struct ethhdr);
163 if (ip6h + 1 > data_end)
166 dport = get_dport(ip6h + 1, data_end, ip6h->nexthdr);
170 vip.protocol = ip6h->nexthdr;
171 vip.family = AF_INET6;
172 memcpy(vip.daddr.v6, ip6h->daddr.s6_addr32, sizeof(vip.daddr));
174 payload_len = ip6h->payload_len;
176 tnl = bpf_map_lookup_elem(&vip2tnl, &vip);
177 /* It only does v6-in-v6 */
178 if (!tnl || tnl->family != AF_INET6)
181 if (bpf_xdp_adjust_head(xdp, 0 - (int)sizeof(struct ipv6hdr)))
184 data = (void *)(long)xdp->data;
185 data_end = (void *)(long)xdp->data_end;
188 ip6h = data + sizeof(*new_eth);
189 old_eth = data + sizeof(*ip6h);
191 if (new_eth + 1 > data_end || old_eth + 1 > data_end ||
195 set_ethhdr(new_eth, old_eth, tnl, bpf_htons(ETH_P_IPV6));
199 memset(ip6h->flow_lbl, 0, sizeof(ip6h->flow_lbl));
200 ip6h->payload_len = bpf_htons(bpf_ntohs(payload_len) + sizeof(*ip6h));
201 ip6h->nexthdr = IPPROTO_IPV6;
203 memcpy(ip6h->saddr.s6_addr32, tnl->saddr.v6, sizeof(tnl->saddr.v6));
204 memcpy(ip6h->daddr.s6_addr32, tnl->daddr.v6, sizeof(tnl->daddr.v6));
206 count_tx(vip.protocol);
212 int _xdp_tx_iptunnel(struct xdp_md *xdp)
214 void *data_end = (void *)(long)xdp->data_end;
215 void *data = (void *)(long)xdp->data;
216 struct ethhdr *eth = data;
219 if (eth + 1 > data_end)
222 h_proto = eth->h_proto;
224 if (h_proto == bpf_htons(ETH_P_IP))
225 return handle_ipv4(xdp);
226 else if (h_proto == bpf_htons(ETH_P_IPV6))
228 return handle_ipv6(xdp);
233 char _license[] SEC("license") = "GPL";