Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0
  2// Copyright (c) 2019 Facebook
  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_compiler.h"
 19
 20struct {
 21	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
 22	__uint(max_entries, 256);
 23	__type(key, __u32);
 24	__type(value, __u64);
 25} rxcnt SEC(".maps");
 26
 27struct {
 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");
 33
 34static __always_inline void count_tx(__u32 protocol)
 35{
 36	__u64 *rxcnt_count;
 37
 38	rxcnt_count = bpf_map_lookup_elem(&rxcnt, &protocol);
 39	if (rxcnt_count)
 40		*rxcnt_count += 1;
 41}
 42
 43static __always_inline int get_dport(void *trans_data, void *data_end,
 44				     __u8 protocol)
 45{
 46	struct tcphdr *th;
 47	struct udphdr *uh;
 48
 49	switch (protocol) {
 50	case IPPROTO_TCP:
 51		th = (struct tcphdr *)trans_data;
 52		if (th + 1 > data_end)
 53			return -1;
 54		return th->dest;
 55	case IPPROTO_UDP:
 56		uh = (struct udphdr *)trans_data;
 57		if (uh + 1 > data_end)
 58			return -1;
 59		return uh->dest;
 60	default:
 61		return 0;
 62	}
 63}
 64
 65static __always_inline void set_ethhdr(struct ethhdr *new_eth,
 66				       const struct ethhdr *old_eth,
 67				       const struct iptnl_info *tnl,
 68				       __be16 h_proto)
 69{
 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;
 73}
 74
 75static __always_inline int handle_ipv4(struct xdp_md *xdp)
 76{
 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);
 83	__u16 *next_iph;
 84	__u16 payload_len;
 85	struct vip vip = {};
 86	int dport;
 87	__u32 csum = 0;
 88	int i;
 89
 90	if (iph + 1 > data_end)
 91		return XDP_DROP;
 92
 93	dport = get_dport(iph + 1, data_end, iph->protocol);
 94	if (dport == -1)
 95		return XDP_DROP;
 96
 97	vip.protocol = iph->protocol;
 98	vip.family = AF_INET;
 99	vip.daddr.v4 = iph->daddr;
100	vip.dport = dport;
101	payload_len = bpf_ntohs(iph->tot_len);
102
103	tnl = bpf_map_lookup_elem(&vip2tnl, &vip);
104	/* It only does v4-in-v4 */
105	if (!tnl || tnl->family != AF_INET)
106		return XDP_PASS;
107
108	if (bpf_xdp_adjust_head(xdp, 0 - (int)sizeof(struct iphdr)))
109		return XDP_DROP;
110
111	data = (void *)(long)xdp->data;
112	data_end = (void *)(long)xdp->data_end;
113
114	new_eth = data;
115	iph = data + sizeof(*new_eth);
116	old_eth = data + sizeof(*iph);
117
118	if (new_eth + 1 > data_end ||
119	    old_eth + 1 > data_end ||
120	    iph + 1 > data_end)
121		return XDP_DROP;
122
123	set_ethhdr(new_eth, old_eth, tnl, bpf_htons(ETH_P_IP));
124
125	iph->version = 4;
126	iph->ihl = sizeof(*iph) >> 2;
127	iph->frag_off =	0;
128	iph->protocol = IPPROTO_IPIP;
129	iph->check = 0;
130	iph->tos = 0;
131	iph->tot_len = bpf_htons(payload_len + sizeof(*iph));
132	iph->daddr = tnl->daddr.v4;
133	iph->saddr = tnl->saddr.v4;
134	iph->ttl = 8;
135
136	next_iph = (__u16 *)iph;
137	__pragma_loop_no_unroll
138	for (i = 0; i < sizeof(*iph) >> 1; i++)
139		csum += *next_iph++;
140
141	iph->check = ~((csum & 0xffff) + (csum >> 16));
142
143	count_tx(vip.protocol);
144
145	return XDP_TX;
146}
147
148static __always_inline int handle_ipv6(struct xdp_md *xdp)
149{
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);
156	__u16 payload_len;
157	struct vip vip = {};
158	int dport;
159
160	if (ip6h + 1 > data_end)
161		return XDP_DROP;
162
163	dport = get_dport(ip6h + 1, data_end, ip6h->nexthdr);
164	if (dport == -1)
165		return XDP_DROP;
166
167	vip.protocol = ip6h->nexthdr;
168	vip.family = AF_INET6;
169	memcpy(vip.daddr.v6, ip6h->daddr.s6_addr32, sizeof(vip.daddr));
170	vip.dport = dport;
171	payload_len = ip6h->payload_len;
172
173	tnl = bpf_map_lookup_elem(&vip2tnl, &vip);
174	/* It only does v6-in-v6 */
175	if (!tnl || tnl->family != AF_INET6)
176		return XDP_PASS;
177
178	if (bpf_xdp_adjust_head(xdp, 0 - (int)sizeof(struct ipv6hdr)))
179		return XDP_DROP;
180
181	data = (void *)(long)xdp->data;
182	data_end = (void *)(long)xdp->data_end;
183
184	new_eth = data;
185	ip6h = data + sizeof(*new_eth);
186	old_eth = data + sizeof(*ip6h);
187
188	if (new_eth + 1 > data_end || old_eth + 1 > data_end ||
189	    ip6h + 1 > data_end)
190		return XDP_DROP;
191
192	set_ethhdr(new_eth, old_eth, tnl, bpf_htons(ETH_P_IPV6));
193
194	ip6h->version = 6;
195	ip6h->priority = 0;
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;
199	ip6h->hop_limit = 8;
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));
202
203	count_tx(vip.protocol);
204
205	return XDP_TX;
206}
207
208SEC("xdp")
209int _xdp_tx_iptunnel(struct xdp_md *xdp)
210{
211	void *data_end = (void *)(long)xdp->data_end;
212	void *data = (void *)(long)xdp->data;
213	struct ethhdr *eth = data;
214	__u16 h_proto;
215
216	if (eth + 1 > data_end)
217		return XDP_DROP;
218
219	h_proto = eth->h_proto;
220
221	if (h_proto == bpf_htons(ETH_P_IP))
222		return handle_ipv4(xdp);
223	else if (h_proto == bpf_htons(ETH_P_IPV6))
224
225		return handle_ipv6(xdp);
226	else
227		return XDP_DROP;
228}
229
230char _license[] SEC("license") = "GPL";
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2// Copyright (c) 2019 Facebook
  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
 19struct {
 20	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
 21	__uint(max_entries, 256);
 22	__type(key, __u32);
 23	__type(value, __u64);
 24} rxcnt SEC(".maps");
 25
 26struct {
 27	__uint(type, BPF_MAP_TYPE_HASH);
 28	__uint(max_entries, MAX_IPTNL_ENTRIES);
 29	__type(key, struct vip);
 30	__type(value, struct iptnl_info);
 31} vip2tnl SEC(".maps");
 32
 33static __always_inline void count_tx(__u32 protocol)
 34{
 35	__u64 *rxcnt_count;
 36
 37	rxcnt_count = bpf_map_lookup_elem(&rxcnt, &protocol);
 38	if (rxcnt_count)
 39		*rxcnt_count += 1;
 40}
 41
 42static __always_inline int get_dport(void *trans_data, void *data_end,
 43				     __u8 protocol)
 44{
 45	struct tcphdr *th;
 46	struct udphdr *uh;
 47
 48	switch (protocol) {
 49	case IPPROTO_TCP:
 50		th = (struct tcphdr *)trans_data;
 51		if (th + 1 > data_end)
 52			return -1;
 53		return th->dest;
 54	case IPPROTO_UDP:
 55		uh = (struct udphdr *)trans_data;
 56		if (uh + 1 > data_end)
 57			return -1;
 58		return uh->dest;
 59	default:
 60		return 0;
 61	}
 62}
 63
 64static __always_inline void set_ethhdr(struct ethhdr *new_eth,
 65				       const struct ethhdr *old_eth,
 66				       const struct iptnl_info *tnl,
 67				       __be16 h_proto)
 68{
 69	memcpy(new_eth->h_source, old_eth->h_dest, sizeof(new_eth->h_source));
 70	memcpy(new_eth->h_dest, tnl->dmac, sizeof(new_eth->h_dest));
 71	new_eth->h_proto = h_proto;
 72}
 73
 74static __always_inline int handle_ipv4(struct xdp_md *xdp)
 75{
 76	void *data_end = (void *)(long)xdp->data_end;
 77	void *data = (void *)(long)xdp->data;
 78	struct iptnl_info *tnl;
 79	struct ethhdr *new_eth;
 80	struct ethhdr *old_eth;
 81	struct iphdr *iph = data + sizeof(struct ethhdr);
 82	__u16 *next_iph;
 83	__u16 payload_len;
 84	struct vip vip = {};
 85	int dport;
 86	__u32 csum = 0;
 87	int i;
 88
 89	if (iph + 1 > data_end)
 90		return XDP_DROP;
 91
 92	dport = get_dport(iph + 1, data_end, iph->protocol);
 93	if (dport == -1)
 94		return XDP_DROP;
 95
 96	vip.protocol = iph->protocol;
 97	vip.family = AF_INET;
 98	vip.daddr.v4 = iph->daddr;
 99	vip.dport = dport;
100	payload_len = bpf_ntohs(iph->tot_len);
101
102	tnl = bpf_map_lookup_elem(&vip2tnl, &vip);
103	/* It only does v4-in-v4 */
104	if (!tnl || tnl->family != AF_INET)
105		return XDP_PASS;
106
107	if (bpf_xdp_adjust_head(xdp, 0 - (int)sizeof(struct iphdr)))
108		return XDP_DROP;
109
110	data = (void *)(long)xdp->data;
111	data_end = (void *)(long)xdp->data_end;
112
113	new_eth = data;
114	iph = data + sizeof(*new_eth);
115	old_eth = data + sizeof(*iph);
116
117	if (new_eth + 1 > data_end ||
118	    old_eth + 1 > data_end ||
119	    iph + 1 > data_end)
120		return XDP_DROP;
121
122	set_ethhdr(new_eth, old_eth, tnl, bpf_htons(ETH_P_IP));
123
124	iph->version = 4;
125	iph->ihl = sizeof(*iph) >> 2;
126	iph->frag_off =	0;
127	iph->protocol = IPPROTO_IPIP;
128	iph->check = 0;
129	iph->tos = 0;
130	iph->tot_len = bpf_htons(payload_len + sizeof(*iph));
131	iph->daddr = tnl->daddr.v4;
132	iph->saddr = tnl->saddr.v4;
133	iph->ttl = 8;
134
135	next_iph = (__u16 *)iph;
136#pragma clang loop unroll(disable)
137	for (i = 0; i < sizeof(*iph) >> 1; i++)
138		csum += *next_iph++;
139
140	iph->check = ~((csum & 0xffff) + (csum >> 16));
141
142	count_tx(vip.protocol);
143
144	return XDP_TX;
145}
146
147static __always_inline int handle_ipv6(struct xdp_md *xdp)
148{
149	void *data_end = (void *)(long)xdp->data_end;
150	void *data = (void *)(long)xdp->data;
151	struct iptnl_info *tnl;
152	struct ethhdr *new_eth;
153	struct ethhdr *old_eth;
154	struct ipv6hdr *ip6h = data + sizeof(struct ethhdr);
155	__u16 payload_len;
156	struct vip vip = {};
157	int dport;
158
159	if (ip6h + 1 > data_end)
160		return XDP_DROP;
161
162	dport = get_dport(ip6h + 1, data_end, ip6h->nexthdr);
163	if (dport == -1)
164		return XDP_DROP;
165
166	vip.protocol = ip6h->nexthdr;
167	vip.family = AF_INET6;
168	memcpy(vip.daddr.v6, ip6h->daddr.s6_addr32, sizeof(vip.daddr));
169	vip.dport = dport;
170	payload_len = ip6h->payload_len;
171
172	tnl = bpf_map_lookup_elem(&vip2tnl, &vip);
173	/* It only does v6-in-v6 */
174	if (!tnl || tnl->family != AF_INET6)
175		return XDP_PASS;
176
177	if (bpf_xdp_adjust_head(xdp, 0 - (int)sizeof(struct ipv6hdr)))
178		return XDP_DROP;
179
180	data = (void *)(long)xdp->data;
181	data_end = (void *)(long)xdp->data_end;
182
183	new_eth = data;
184	ip6h = data + sizeof(*new_eth);
185	old_eth = data + sizeof(*ip6h);
186
187	if (new_eth + 1 > data_end || old_eth + 1 > data_end ||
188	    ip6h + 1 > data_end)
189		return XDP_DROP;
190
191	set_ethhdr(new_eth, old_eth, tnl, bpf_htons(ETH_P_IPV6));
192
193	ip6h->version = 6;
194	ip6h->priority = 0;
195	memset(ip6h->flow_lbl, 0, sizeof(ip6h->flow_lbl));
196	ip6h->payload_len = bpf_htons(bpf_ntohs(payload_len) + sizeof(*ip6h));
197	ip6h->nexthdr = IPPROTO_IPV6;
198	ip6h->hop_limit = 8;
199	memcpy(ip6h->saddr.s6_addr32, tnl->saddr.v6, sizeof(tnl->saddr.v6));
200	memcpy(ip6h->daddr.s6_addr32, tnl->daddr.v6, sizeof(tnl->daddr.v6));
201
202	count_tx(vip.protocol);
203
204	return XDP_TX;
205}
206
207SEC("xdp")
208int _xdp_tx_iptunnel(struct xdp_md *xdp)
209{
210	void *data_end = (void *)(long)xdp->data_end;
211	void *data = (void *)(long)xdp->data;
212	struct ethhdr *eth = data;
213	__u16 h_proto;
214
215	if (eth + 1 > data_end)
216		return XDP_DROP;
217
218	h_proto = eth->h_proto;
219
220	if (h_proto == bpf_htons(ETH_P_IP))
221		return handle_ipv4(xdp);
222	else if (h_proto == bpf_htons(ETH_P_IPV6))
223
224		return handle_ipv6(xdp);
225	else
226		return XDP_DROP;
227}
228
229char _license[] SEC("license") = "GPL";