summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/test_tc_tunnel.c
blob: 591f540ce513dea5de1cd82cd040e012854e42e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// SPDX-License-Identifier: GPL-2.0

/* In-place tunneling */

#include <linux/stddef.h>
#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
#include <linux/tcp.h>
#include <linux/pkt_cls.h>
#include <linux/types.h>

#include "bpf_endian.h"
#include "bpf_helpers.h"

static const int cfg_port = 8000;

static __always_inline void set_ipv4_csum(struct iphdr *iph)
{
	__u16 *iph16 = (__u16 *)iph;
	__u32 csum;
	int i;

	iph->check = 0;

#pragma clang loop unroll(full)
	for (i = 0, csum = 0; i < sizeof(*iph) >> 1; i++)
		csum += *iph16++;

	iph->check = ~((csum & 0xffff) + (csum >> 16));
}

static int encap_ipv4(struct __sk_buff *skb)
{
	struct iphdr iph_outer, iph_inner;
	struct tcphdr tcph;

	if (bpf_skb_load_bytes(skb, ETH_HLEN, &iph_inner,
			       sizeof(iph_inner)) < 0)
		return TC_ACT_OK;

	/* filter only packets we want */
	if (iph_inner.ihl != 5 || iph_inner.protocol != IPPROTO_TCP)
		return TC_ACT_OK;

	if (bpf_skb_load_bytes(skb, ETH_HLEN + sizeof(iph_inner),
			       &tcph, sizeof(tcph)) < 0)
		return TC_ACT_OK;

	if (tcph.dest != __bpf_constant_htons(cfg_port))
		return TC_ACT_OK;

	/* add room between mac and network header */
	if (bpf_skb_adjust_room(skb, sizeof(iph_outer), BPF_ADJ_ROOM_NET, 0))
		return TC_ACT_SHOT;

	/* prepare new outer network header */
	iph_outer = iph_inner;
	iph_outer.protocol = IPPROTO_IPIP;
	iph_outer.tot_len = bpf_htons(sizeof(iph_outer) +
				      bpf_htons(iph_outer.tot_len));
	set_ipv4_csum(&iph_outer);

	/* store new outer network header */
	if (bpf_skb_store_bytes(skb, ETH_HLEN, &iph_outer, sizeof(iph_outer),
				BPF_F_INVALIDATE_HASH) < 0)
		return TC_ACT_SHOT;

	/* bpf_skb_adjust_room has moved header to start of room: restore */
	if (bpf_skb_store_bytes(skb, ETH_HLEN + sizeof(iph_outer),
				&iph_inner, sizeof(iph_inner),
				BPF_F_INVALIDATE_HASH) < 0)
		return TC_ACT_SHOT;

	return TC_ACT_OK;
}

static int encap_ipv6(struct __sk_buff *skb)
{
	struct ipv6hdr iph_outer, iph_inner;
	struct tcphdr tcph;

	if (bpf_skb_load_bytes(skb, ETH_HLEN, &iph_inner,
			       sizeof(iph_inner)) < 0)
		return TC_ACT_OK;

	/* filter only packets we want */
	if (bpf_skb_load_bytes(skb, ETH_HLEN + sizeof(iph_inner),
			       &tcph, sizeof(tcph)) < 0)
		return TC_ACT_OK;

	if (tcph.dest != __bpf_constant_htons(cfg_port))
		return TC_ACT_OK;

	/* add room between mac and network header */
	if (bpf_skb_adjust_room(skb, sizeof(iph_outer), BPF_ADJ_ROOM_NET, 0))
		return TC_ACT_SHOT;

	/* prepare new outer network header */
	iph_outer = iph_inner;
	iph_outer.nexthdr = IPPROTO_IPV6;
	iph_outer.payload_len = bpf_htons(sizeof(iph_outer) +
					  bpf_ntohs(iph_outer.payload_len));

	/* store new outer network header */
	if (bpf_skb_store_bytes(skb, ETH_HLEN, &iph_outer, sizeof(iph_outer),
				BPF_F_INVALIDATE_HASH) < 0)
		return TC_ACT_SHOT;

	/* bpf_skb_adjust_room has moved header to start of room: restore */
	if (bpf_skb_store_bytes(skb, ETH_HLEN + sizeof(iph_outer),
				&iph_inner, sizeof(iph_inner),
				BPF_F_INVALIDATE_HASH) < 0)
		return TC_ACT_SHOT;

	return TC_ACT_OK;
}

SEC("encap")
int encap_f(struct __sk_buff *skb)
{
	switch (skb->protocol) {
	case __bpf_constant_htons(ETH_P_IP):
		return encap_ipv4(skb);
	case __bpf_constant_htons(ETH_P_IPV6):
		return encap_ipv6(skb);
	default:
		/* does not match, ignore */
		return TC_ACT_OK;
	}
}

static int decap_internal(struct __sk_buff *skb, int off, int len)
{
	char buf[sizeof(struct ipv6hdr)];

	if (bpf_skb_load_bytes(skb, off + len, &buf, len) < 0)
		return TC_ACT_OK;

	if (bpf_skb_adjust_room(skb, -len, BPF_ADJ_ROOM_NET, 0))
		return TC_ACT_SHOT;

	/* bpf_skb_adjust_room has moved outer over inner header: restore */
	if (bpf_skb_store_bytes(skb, off, buf, len, BPF_F_INVALIDATE_HASH) < 0)
		return TC_ACT_SHOT;

	return TC_ACT_OK;
}

static int decap_ipv4(struct __sk_buff *skb)
{
	struct iphdr iph_outer;

	if (bpf_skb_load_bytes(skb, ETH_HLEN, &iph_outer,
			       sizeof(iph_outer)) < 0)
		return TC_ACT_OK;

	if (iph_outer.ihl != 5 || iph_outer.protocol != IPPROTO_IPIP)
		return TC_ACT_OK;

	return decap_internal(skb, ETH_HLEN, sizeof(iph_outer));
}

static int decap_ipv6(struct __sk_buff *skb)
{
	struct ipv6hdr iph_outer;

	if (bpf_skb_load_bytes(skb, ETH_HLEN, &iph_outer,
			       sizeof(iph_outer)) < 0)
		return TC_ACT_OK;

	if (iph_outer.nexthdr != IPPROTO_IPV6)
		return TC_ACT_OK;

	return decap_internal(skb, ETH_HLEN, sizeof(iph_outer));
}

SEC("decap")
int decap_f(struct __sk_buff *skb)
{
	switch (skb->protocol) {
	case __bpf_constant_htons(ETH_P_IP):
		return decap_ipv4(skb);
	case __bpf_constant_htons(ETH_P_IPV6):
		return decap_ipv6(skb);
	default:
		/* does not match, ignore */
		return TC_ACT_OK;
	}
}

char __license[] SEC("license") = "GPL";