18c2ecf20Sopenharmony_ci/* Copyright (c) 2016 Facebook
28c2ecf20Sopenharmony_ci *
38c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or
48c2ecf20Sopenharmony_ci * modify it under the terms of version 2 of the GNU General Public
58c2ecf20Sopenharmony_ci * License as published by the Free Software Foundation.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * This program shows how to use bpf_xdp_adjust_head() by
88c2ecf20Sopenharmony_ci * encapsulating the incoming packet in an IPv4/v6 header
98c2ecf20Sopenharmony_ci * and then XDP_TX it out.
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci#define KBUILD_MODNAME "foo"
128c2ecf20Sopenharmony_ci#include <uapi/linux/bpf.h>
138c2ecf20Sopenharmony_ci#include <linux/in.h>
148c2ecf20Sopenharmony_ci#include <linux/if_ether.h>
158c2ecf20Sopenharmony_ci#include <linux/if_packet.h>
168c2ecf20Sopenharmony_ci#include <linux/if_vlan.h>
178c2ecf20Sopenharmony_ci#include <linux/ip.h>
188c2ecf20Sopenharmony_ci#include <linux/ipv6.h>
198c2ecf20Sopenharmony_ci#include <bpf/bpf_helpers.h>
208c2ecf20Sopenharmony_ci#include "xdp_tx_iptunnel_common.h"
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistruct {
238c2ecf20Sopenharmony_ci	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
248c2ecf20Sopenharmony_ci	__type(key, __u32);
258c2ecf20Sopenharmony_ci	__type(value, __u64);
268c2ecf20Sopenharmony_ci	__uint(max_entries, 256);
278c2ecf20Sopenharmony_ci} rxcnt SEC(".maps");
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistruct {
308c2ecf20Sopenharmony_ci	__uint(type, BPF_MAP_TYPE_HASH);
318c2ecf20Sopenharmony_ci	__type(key, struct vip);
328c2ecf20Sopenharmony_ci	__type(value, struct iptnl_info);
338c2ecf20Sopenharmony_ci	__uint(max_entries, MAX_IPTNL_ENTRIES);
348c2ecf20Sopenharmony_ci} vip2tnl SEC(".maps");
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistatic __always_inline void count_tx(u32 protocol)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	u64 *rxcnt_count;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	rxcnt_count = bpf_map_lookup_elem(&rxcnt, &protocol);
418c2ecf20Sopenharmony_ci	if (rxcnt_count)
428c2ecf20Sopenharmony_ci		*rxcnt_count += 1;
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic __always_inline int get_dport(void *trans_data, void *data_end,
468c2ecf20Sopenharmony_ci				     u8 protocol)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	struct tcphdr *th;
498c2ecf20Sopenharmony_ci	struct udphdr *uh;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	switch (protocol) {
528c2ecf20Sopenharmony_ci	case IPPROTO_TCP:
538c2ecf20Sopenharmony_ci		th = (struct tcphdr *)trans_data;
548c2ecf20Sopenharmony_ci		if (th + 1 > data_end)
558c2ecf20Sopenharmony_ci			return -1;
568c2ecf20Sopenharmony_ci		return th->dest;
578c2ecf20Sopenharmony_ci	case IPPROTO_UDP:
588c2ecf20Sopenharmony_ci		uh = (struct udphdr *)trans_data;
598c2ecf20Sopenharmony_ci		if (uh + 1 > data_end)
608c2ecf20Sopenharmony_ci			return -1;
618c2ecf20Sopenharmony_ci		return uh->dest;
628c2ecf20Sopenharmony_ci	default:
638c2ecf20Sopenharmony_ci		return 0;
648c2ecf20Sopenharmony_ci	}
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic __always_inline void set_ethhdr(struct ethhdr *new_eth,
688c2ecf20Sopenharmony_ci				       const struct ethhdr *old_eth,
698c2ecf20Sopenharmony_ci				       const struct iptnl_info *tnl,
708c2ecf20Sopenharmony_ci				       __be16 h_proto)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	memcpy(new_eth->h_source, old_eth->h_dest, sizeof(new_eth->h_source));
738c2ecf20Sopenharmony_ci	memcpy(new_eth->h_dest, tnl->dmac, sizeof(new_eth->h_dest));
748c2ecf20Sopenharmony_ci	new_eth->h_proto = h_proto;
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic __always_inline int handle_ipv4(struct xdp_md *xdp)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	void *data_end = (void *)(long)xdp->data_end;
808c2ecf20Sopenharmony_ci	void *data = (void *)(long)xdp->data;
818c2ecf20Sopenharmony_ci	struct iptnl_info *tnl;
828c2ecf20Sopenharmony_ci	struct ethhdr *new_eth;
838c2ecf20Sopenharmony_ci	struct ethhdr *old_eth;
848c2ecf20Sopenharmony_ci	struct iphdr *iph = data + sizeof(struct ethhdr);
858c2ecf20Sopenharmony_ci	u16 *next_iph_u16;
868c2ecf20Sopenharmony_ci	u16 payload_len;
878c2ecf20Sopenharmony_ci	struct vip vip = {};
888c2ecf20Sopenharmony_ci	int dport;
898c2ecf20Sopenharmony_ci	u32 csum = 0;
908c2ecf20Sopenharmony_ci	int i;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	if (iph + 1 > data_end)
938c2ecf20Sopenharmony_ci		return XDP_DROP;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	dport = get_dport(iph + 1, data_end, iph->protocol);
968c2ecf20Sopenharmony_ci	if (dport == -1)
978c2ecf20Sopenharmony_ci		return XDP_DROP;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	vip.protocol = iph->protocol;
1008c2ecf20Sopenharmony_ci	vip.family = AF_INET;
1018c2ecf20Sopenharmony_ci	vip.daddr.v4 = iph->daddr;
1028c2ecf20Sopenharmony_ci	vip.dport = dport;
1038c2ecf20Sopenharmony_ci	payload_len = ntohs(iph->tot_len);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	tnl = bpf_map_lookup_elem(&vip2tnl, &vip);
1068c2ecf20Sopenharmony_ci	/* It only does v4-in-v4 */
1078c2ecf20Sopenharmony_ci	if (!tnl || tnl->family != AF_INET)
1088c2ecf20Sopenharmony_ci		return XDP_PASS;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	/* The vip key is found.  Add an IP header and send it out */
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	if (bpf_xdp_adjust_head(xdp, 0 - (int)sizeof(struct iphdr)))
1138c2ecf20Sopenharmony_ci		return XDP_DROP;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	data = (void *)(long)xdp->data;
1168c2ecf20Sopenharmony_ci	data_end = (void *)(long)xdp->data_end;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	new_eth = data;
1198c2ecf20Sopenharmony_ci	iph = data + sizeof(*new_eth);
1208c2ecf20Sopenharmony_ci	old_eth = data + sizeof(*iph);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	if (new_eth + 1 > data_end ||
1238c2ecf20Sopenharmony_ci	    old_eth + 1 > data_end ||
1248c2ecf20Sopenharmony_ci	    iph + 1 > data_end)
1258c2ecf20Sopenharmony_ci		return XDP_DROP;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	set_ethhdr(new_eth, old_eth, tnl, htons(ETH_P_IP));
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	iph->version = 4;
1308c2ecf20Sopenharmony_ci	iph->ihl = sizeof(*iph) >> 2;
1318c2ecf20Sopenharmony_ci	iph->frag_off =	0;
1328c2ecf20Sopenharmony_ci	iph->protocol = IPPROTO_IPIP;
1338c2ecf20Sopenharmony_ci	iph->check = 0;
1348c2ecf20Sopenharmony_ci	iph->tos = 0;
1358c2ecf20Sopenharmony_ci	iph->tot_len = htons(payload_len + sizeof(*iph));
1368c2ecf20Sopenharmony_ci	iph->daddr = tnl->daddr.v4;
1378c2ecf20Sopenharmony_ci	iph->saddr = tnl->saddr.v4;
1388c2ecf20Sopenharmony_ci	iph->ttl = 8;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	next_iph_u16 = (u16 *)iph;
1418c2ecf20Sopenharmony_ci#pragma clang loop unroll(full)
1428c2ecf20Sopenharmony_ci	for (i = 0; i < sizeof(*iph) >> 1; i++)
1438c2ecf20Sopenharmony_ci		csum += *next_iph_u16++;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	iph->check = ~((csum & 0xffff) + (csum >> 16));
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	count_tx(vip.protocol);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	return XDP_TX;
1508c2ecf20Sopenharmony_ci}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic __always_inline int handle_ipv6(struct xdp_md *xdp)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	void *data_end = (void *)(long)xdp->data_end;
1558c2ecf20Sopenharmony_ci	void *data = (void *)(long)xdp->data;
1568c2ecf20Sopenharmony_ci	struct iptnl_info *tnl;
1578c2ecf20Sopenharmony_ci	struct ethhdr *new_eth;
1588c2ecf20Sopenharmony_ci	struct ethhdr *old_eth;
1598c2ecf20Sopenharmony_ci	struct ipv6hdr *ip6h = data + sizeof(struct ethhdr);
1608c2ecf20Sopenharmony_ci	__u16 payload_len;
1618c2ecf20Sopenharmony_ci	struct vip vip = {};
1628c2ecf20Sopenharmony_ci	int dport;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	if (ip6h + 1 > data_end)
1658c2ecf20Sopenharmony_ci		return XDP_DROP;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	dport = get_dport(ip6h + 1, data_end, ip6h->nexthdr);
1688c2ecf20Sopenharmony_ci	if (dport == -1)
1698c2ecf20Sopenharmony_ci		return XDP_DROP;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	vip.protocol = ip6h->nexthdr;
1728c2ecf20Sopenharmony_ci	vip.family = AF_INET6;
1738c2ecf20Sopenharmony_ci	memcpy(vip.daddr.v6, ip6h->daddr.s6_addr32, sizeof(vip.daddr));
1748c2ecf20Sopenharmony_ci	vip.dport = dport;
1758c2ecf20Sopenharmony_ci	payload_len = ip6h->payload_len;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	tnl = bpf_map_lookup_elem(&vip2tnl, &vip);
1788c2ecf20Sopenharmony_ci	/* It only does v6-in-v6 */
1798c2ecf20Sopenharmony_ci	if (!tnl || tnl->family != AF_INET6)
1808c2ecf20Sopenharmony_ci		return XDP_PASS;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	/* The vip key is found.  Add an IP header and send it out */
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	if (bpf_xdp_adjust_head(xdp, 0 - (int)sizeof(struct ipv6hdr)))
1858c2ecf20Sopenharmony_ci		return XDP_DROP;
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	data = (void *)(long)xdp->data;
1888c2ecf20Sopenharmony_ci	data_end = (void *)(long)xdp->data_end;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	new_eth = data;
1918c2ecf20Sopenharmony_ci	ip6h = data + sizeof(*new_eth);
1928c2ecf20Sopenharmony_ci	old_eth = data + sizeof(*ip6h);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	if (new_eth + 1 > data_end ||
1958c2ecf20Sopenharmony_ci	    old_eth + 1 > data_end ||
1968c2ecf20Sopenharmony_ci	    ip6h + 1 > data_end)
1978c2ecf20Sopenharmony_ci		return XDP_DROP;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	set_ethhdr(new_eth, old_eth, tnl, htons(ETH_P_IPV6));
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	ip6h->version = 6;
2028c2ecf20Sopenharmony_ci	ip6h->priority = 0;
2038c2ecf20Sopenharmony_ci	memset(ip6h->flow_lbl, 0, sizeof(ip6h->flow_lbl));
2048c2ecf20Sopenharmony_ci	ip6h->payload_len = htons(ntohs(payload_len) + sizeof(*ip6h));
2058c2ecf20Sopenharmony_ci	ip6h->nexthdr = IPPROTO_IPV6;
2068c2ecf20Sopenharmony_ci	ip6h->hop_limit = 8;
2078c2ecf20Sopenharmony_ci	memcpy(ip6h->saddr.s6_addr32, tnl->saddr.v6, sizeof(tnl->saddr.v6));
2088c2ecf20Sopenharmony_ci	memcpy(ip6h->daddr.s6_addr32, tnl->daddr.v6, sizeof(tnl->daddr.v6));
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	count_tx(vip.protocol);
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	return XDP_TX;
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ciSEC("xdp_tx_iptunnel")
2168c2ecf20Sopenharmony_ciint _xdp_tx_iptunnel(struct xdp_md *xdp)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	void *data_end = (void *)(long)xdp->data_end;
2198c2ecf20Sopenharmony_ci	void *data = (void *)(long)xdp->data;
2208c2ecf20Sopenharmony_ci	struct ethhdr *eth = data;
2218c2ecf20Sopenharmony_ci	__u16 h_proto;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	if (eth + 1 > data_end)
2248c2ecf20Sopenharmony_ci		return XDP_DROP;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	h_proto = eth->h_proto;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	if (h_proto == htons(ETH_P_IP))
2298c2ecf20Sopenharmony_ci		return handle_ipv4(xdp);
2308c2ecf20Sopenharmony_ci	else if (h_proto == htons(ETH_P_IPV6))
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci		return handle_ipv6(xdp);
2338c2ecf20Sopenharmony_ci	else
2348c2ecf20Sopenharmony_ci		return XDP_PASS;
2358c2ecf20Sopenharmony_ci}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_cichar _license[] SEC("license") = "GPL";
238