1212#include <linux/ip.h>
1313#include <linux/ipv6.h>
1414#include <linux/tcp.h>
15+ #include <linux/udp.h>
1516#include <linux/pkt_cls.h>
1617#include <linux/types.h>
1718
2021
2122static const int cfg_port = 8000 ;
2223
23- struct grev4hdr {
24- struct iphdr ip ;
24+ static const int cfg_udp_src = 20000 ;
25+ static const int cfg_udp_dst = 5555 ;
26+
27+ struct gre_hdr {
2528 __be16 flags ;
2629 __be16 protocol ;
2730} __attribute__((packed ));
2831
29- struct grev6hdr {
32+ union l4hdr {
33+ struct udphdr udp ;
34+ struct gre_hdr gre ;
35+ };
36+
37+ struct v4hdr {
38+ struct iphdr ip ;
39+ union l4hdr l4hdr ;
40+ } __attribute__((packed ));
41+
42+ struct v6hdr {
3043 struct ipv6hdr ip ;
31- __be16 flags ;
32- __be16 protocol ;
44+ union l4hdr l4hdr ;
3345} __attribute__((packed ));
3446
3547static __always_inline void set_ipv4_csum (struct iphdr * iph )
@@ -47,10 +59,10 @@ static __always_inline void set_ipv4_csum(struct iphdr *iph)
4759 iph -> check = ~((csum & 0xffff ) + (csum >> 16 ));
4860}
4961
50- static __always_inline int encap_ipv4 (struct __sk_buff * skb , bool with_gre )
62+ static __always_inline int encap_ipv4 (struct __sk_buff * skb , __u8 encap_proto )
5163{
52- struct grev4hdr h_outer ;
5364 struct iphdr iph_inner ;
65+ struct v4hdr h_outer ;
5466 struct tcphdr tcph ;
5567 __u64 flags ;
5668 int olen ;
@@ -70,12 +82,29 @@ static __always_inline int encap_ipv4(struct __sk_buff *skb, bool with_gre)
7082 if (tcph .dest != __bpf_constant_htons (cfg_port ))
7183 return TC_ACT_OK ;
7284
85+ olen = sizeof (h_outer .ip );
86+
7387 flags = BPF_F_ADJ_ROOM_FIXED_GSO | BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 ;
74- if (with_gre ) {
88+ switch (encap_proto ) {
89+ case IPPROTO_GRE :
7590 flags |= BPF_F_ADJ_ROOM_ENCAP_L4_GRE ;
76- olen = sizeof (h_outer );
77- } else {
78- olen = sizeof (h_outer .ip );
91+ olen += sizeof (h_outer .l4hdr .gre );
92+ h_outer .l4hdr .gre .protocol = bpf_htons (ETH_P_IP );
93+ h_outer .l4hdr .gre .flags = 0 ;
94+ break ;
95+ case IPPROTO_UDP :
96+ flags |= BPF_F_ADJ_ROOM_ENCAP_L4_UDP ;
97+ olen += sizeof (h_outer .l4hdr .udp );
98+ h_outer .l4hdr .udp .source = __bpf_constant_htons (cfg_udp_src );
99+ h_outer .l4hdr .udp .dest = __bpf_constant_htons (cfg_udp_dst );
100+ h_outer .l4hdr .udp .check = 0 ;
101+ h_outer .l4hdr .udp .len = bpf_htons (bpf_ntohs (iph_inner .tot_len ) +
102+ sizeof (h_outer .l4hdr .udp ));
103+ break ;
104+ case IPPROTO_IPIP :
105+ break ;
106+ default :
107+ return TC_ACT_OK ;
79108 }
80109
81110 /* add room between mac and network header */
@@ -85,16 +114,10 @@ static __always_inline int encap_ipv4(struct __sk_buff *skb, bool with_gre)
85114 /* prepare new outer network header */
86115 h_outer .ip = iph_inner ;
87116 h_outer .ip .tot_len = bpf_htons (olen +
88- bpf_htons (h_outer .ip .tot_len ));
89- if (with_gre ) {
90- h_outer .ip .protocol = IPPROTO_GRE ;
91- h_outer .protocol = bpf_htons (ETH_P_IP );
92- h_outer .flags = 0 ;
93- } else {
94- h_outer .ip .protocol = IPPROTO_IPIP ;
95- }
117+ bpf_ntohs (h_outer .ip .tot_len ));
118+ h_outer .ip .protocol = encap_proto ;
96119
97- set_ipv4_csum (( void * ) & h_outer .ip );
120+ set_ipv4_csum (& h_outer .ip );
98121
99122 /* store new outer network header */
100123 if (bpf_skb_store_bytes (skb , ETH_HLEN , & h_outer , olen ,
@@ -104,11 +127,12 @@ static __always_inline int encap_ipv4(struct __sk_buff *skb, bool with_gre)
104127 return TC_ACT_OK ;
105128}
106129
107- static __always_inline int encap_ipv6 (struct __sk_buff * skb , bool with_gre )
130+ static __always_inline int encap_ipv6 (struct __sk_buff * skb , __u8 encap_proto )
108131{
109132 struct ipv6hdr iph_inner ;
110- struct grev6hdr h_outer ;
133+ struct v6hdr h_outer ;
111134 struct tcphdr tcph ;
135+ __u16 tot_len ;
112136 __u64 flags ;
113137 int olen ;
114138
@@ -124,15 +148,32 @@ static __always_inline int encap_ipv6(struct __sk_buff *skb, bool with_gre)
124148 if (tcph .dest != __bpf_constant_htons (cfg_port ))
125149 return TC_ACT_OK ;
126150
151+ olen = sizeof (h_outer .ip );
152+
127153 flags = BPF_F_ADJ_ROOM_FIXED_GSO | BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 ;
128- if (with_gre ) {
154+ switch (encap_proto ) {
155+ case IPPROTO_GRE :
129156 flags |= BPF_F_ADJ_ROOM_ENCAP_L4_GRE ;
130- olen = sizeof (h_outer );
131- } else {
132- olen = sizeof (h_outer .ip );
157+ olen += sizeof (h_outer .l4hdr .gre );
158+ h_outer .l4hdr .gre .protocol = bpf_htons (ETH_P_IPV6 );
159+ h_outer .l4hdr .gre .flags = 0 ;
160+ break ;
161+ case IPPROTO_UDP :
162+ flags |= BPF_F_ADJ_ROOM_ENCAP_L4_UDP ;
163+ olen += sizeof (h_outer .l4hdr .udp );
164+ h_outer .l4hdr .udp .source = __bpf_constant_htons (cfg_udp_src );
165+ h_outer .l4hdr .udp .dest = __bpf_constant_htons (cfg_udp_dst );
166+ tot_len = bpf_ntohs (iph_inner .payload_len ) + sizeof (iph_inner ) +
167+ sizeof (h_outer .l4hdr .udp );
168+ h_outer .l4hdr .udp .check = 0 ;
169+ h_outer .l4hdr .udp .len = bpf_htons (tot_len );
170+ break ;
171+ case IPPROTO_IPV6 :
172+ break ;
173+ default :
174+ return TC_ACT_OK ;
133175 }
134176
135-
136177 /* add room between mac and network header */
137178 if (bpf_skb_adjust_room (skb , olen , BPF_ADJ_ROOM_MAC , flags ))
138179 return TC_ACT_SHOT ;
@@ -141,13 +182,8 @@ static __always_inline int encap_ipv6(struct __sk_buff *skb, bool with_gre)
141182 h_outer .ip = iph_inner ;
142183 h_outer .ip .payload_len = bpf_htons (olen +
143184 bpf_ntohs (h_outer .ip .payload_len ));
144- if (with_gre ) {
145- h_outer .ip .nexthdr = IPPROTO_GRE ;
146- h_outer .protocol = bpf_htons (ETH_P_IPV6 );
147- h_outer .flags = 0 ;
148- } else {
149- h_outer .ip .nexthdr = IPPROTO_IPV6 ;
150- }
185+
186+ h_outer .ip .nexthdr = encap_proto ;
151187
152188 /* store new outer network header */
153189 if (bpf_skb_store_bytes (skb , ETH_HLEN , & h_outer , olen ,
@@ -161,7 +197,7 @@ SEC("encap_ipip")
161197int __encap_ipip (struct __sk_buff * skb )
162198{
163199 if (skb -> protocol == __bpf_constant_htons (ETH_P_IP ))
164- return encap_ipv4 (skb , false );
200+ return encap_ipv4 (skb , IPPROTO_IPIP );
165201 else
166202 return TC_ACT_OK ;
167203}
@@ -170,7 +206,16 @@ SEC("encap_gre")
170206int __encap_gre (struct __sk_buff * skb )
171207{
172208 if (skb -> protocol == __bpf_constant_htons (ETH_P_IP ))
173- return encap_ipv4 (skb , true);
209+ return encap_ipv4 (skb , IPPROTO_GRE );
210+ else
211+ return TC_ACT_OK ;
212+ }
213+
214+ SEC ("encap_udp" )
215+ int __encap_udp (struct __sk_buff * skb )
216+ {
217+ if (skb -> protocol == __bpf_constant_htons (ETH_P_IP ))
218+ return encap_ipv4 (skb , IPPROTO_UDP );
174219 else
175220 return TC_ACT_OK ;
176221}
@@ -179,7 +224,7 @@ SEC("encap_ip6tnl")
179224int __encap_ip6tnl (struct __sk_buff * skb )
180225{
181226 if (skb -> protocol == __bpf_constant_htons (ETH_P_IPV6 ))
182- return encap_ipv6 (skb , false );
227+ return encap_ipv6 (skb , IPPROTO_IPV6 );
183228 else
184229 return TC_ACT_OK ;
185230}
@@ -188,23 +233,34 @@ SEC("encap_ip6gre")
188233int __encap_ip6gre (struct __sk_buff * skb )
189234{
190235 if (skb -> protocol == __bpf_constant_htons (ETH_P_IPV6 ))
191- return encap_ipv6 (skb , true);
236+ return encap_ipv6 (skb , IPPROTO_GRE );
237+ else
238+ return TC_ACT_OK ;
239+ }
240+
241+ SEC ("encap_ip6udp" )
242+ int __encap_ip6udp (struct __sk_buff * skb )
243+ {
244+ if (skb -> protocol == __bpf_constant_htons (ETH_P_IPV6 ))
245+ return encap_ipv6 (skb , IPPROTO_UDP );
192246 else
193247 return TC_ACT_OK ;
194248}
195249
196250static int decap_internal (struct __sk_buff * skb , int off , int len , char proto )
197251{
198- char buf [sizeof (struct grev6hdr )];
199- int olen ;
252+ char buf [sizeof (struct v6hdr )];
253+ int olen = len ;
200254
201255 switch (proto ) {
202256 case IPPROTO_IPIP :
203257 case IPPROTO_IPV6 :
204- olen = len ;
205258 break ;
206259 case IPPROTO_GRE :
207- olen = len + 4 /* gre hdr */ ;
260+ olen += sizeof (struct gre_hdr );
261+ break ;
262+ case IPPROTO_UDP :
263+ olen += sizeof (struct udphdr );
208264 break ;
209265 default :
210266 return TC_ACT_OK ;
0 commit comments