1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/module.h>
3 #include <linux/errno.h>
4 #include <linux/socket.h>
5 #include <linux/skbuff.h>
8 #include <linux/icmpv6.h>
9 #include <linux/types.h>
10 #include <linux/kernel.h>
13 #include <net/ip6_tunnel.h>
14 #include <net/ip6_checksum.h>
15 #include <net/protocol.h>
17 #include <net/udp_tunnel.h>
19 #if IS_ENABLED(CONFIG_IPV6_FOU_TUNNEL)
21 static void fou6_build_udp(struct sk_buff
*skb
, struct ip_tunnel_encap
*e
,
22 struct flowi6
*fl6
, u8
*protocol
, __be16 sport
)
26 skb_push(skb
, sizeof(struct udphdr
));
27 skb_reset_transport_header(skb
);
33 uh
->len
= htons(skb
->len
);
34 udp6_set_csum(!(e
->flags
& TUNNEL_ENCAP_FLAG_CSUM6
), skb
,
35 &fl6
->saddr
, &fl6
->daddr
, skb
->len
);
37 *protocol
= IPPROTO_UDP
;
40 static int fou6_build_header(struct sk_buff
*skb
, struct ip_tunnel_encap
*e
,
41 u8
*protocol
, struct flowi6
*fl6
)
45 int type
= e
->flags
& TUNNEL_ENCAP_FLAG_CSUM6
?
46 SKB_GSO_UDP_TUNNEL_CSUM
: SKB_GSO_UDP_TUNNEL
;
48 err
= __fou_build_header(skb
, e
, protocol
, &sport
, type
);
52 fou6_build_udp(skb
, e
, fl6
, protocol
, sport
);
57 static int gue6_build_header(struct sk_buff
*skb
, struct ip_tunnel_encap
*e
,
58 u8
*protocol
, struct flowi6
*fl6
)
62 int type
= e
->flags
& TUNNEL_ENCAP_FLAG_CSUM6
?
63 SKB_GSO_UDP_TUNNEL_CSUM
: SKB_GSO_UDP_TUNNEL
;
65 err
= __gue_build_header(skb
, e
, protocol
, &sport
, type
);
69 fou6_build_udp(skb
, e
, fl6
, protocol
, sport
);
74 static int gue6_err_proto_handler(int proto
, struct sk_buff
*skb
,
75 struct inet6_skb_parm
*opt
,
76 u8 type
, u8 code
, int offset
, __be32 info
)
78 const struct inet6_protocol
*ipprot
;
80 ipprot
= rcu_dereference(inet6_protos
[proto
]);
81 if (ipprot
&& ipprot
->err_handler
) {
82 if (!ipprot
->err_handler(skb
, opt
, type
, code
, offset
, info
))
89 static int gue6_err(struct sk_buff
*skb
, struct inet6_skb_parm
*opt
,
90 u8 type
, u8 code
, int offset
, __be32 info
)
92 int transport_offset
= skb_transport_offset(skb
);
93 struct guehdr
*guehdr
;
97 len
= sizeof(struct udphdr
) + sizeof(struct guehdr
);
98 if (!pskb_may_pull(skb
, transport_offset
+ len
))
101 guehdr
= (struct guehdr
*)&udp_hdr(skb
)[1];
103 switch (guehdr
->version
) {
104 case 0: /* Full GUE header present */
107 /* Direct encasulation of IPv4 or IPv6 */
108 skb_set_transport_header(skb
, -(int)sizeof(struct icmp6hdr
));
110 switch (((struct iphdr
*)guehdr
)->version
) {
112 ret
= gue6_err_proto_handler(IPPROTO_IPIP
, skb
, opt
,
113 type
, code
, offset
, info
);
116 ret
= gue6_err_proto_handler(IPPROTO_IPV6
, skb
, opt
,
117 type
, code
, offset
, info
);
124 default: /* Undefined version */
131 optlen
= guehdr
->hlen
<< 2;
133 if (!pskb_may_pull(skb
, transport_offset
+ len
+ optlen
))
136 guehdr
= (struct guehdr
*)&udp_hdr(skb
)[1];
137 if (validate_gue_flags(guehdr
, optlen
))
140 /* Handling exceptions for direct UDP encapsulation in GUE would lead to
141 * recursion. Besides, this kind of encapsulation can't even be
142 * configured currently. Discard this.
144 if (guehdr
->proto_ctype
== IPPROTO_UDP
||
145 guehdr
->proto_ctype
== IPPROTO_UDPLITE
)
148 skb_set_transport_header(skb
, -(int)sizeof(struct icmp6hdr
));
149 ret
= gue6_err_proto_handler(guehdr
->proto_ctype
, skb
,
150 opt
, type
, code
, offset
, info
);
153 skb_set_transport_header(skb
, transport_offset
);
158 static const struct ip6_tnl_encap_ops fou_ip6tun_ops
= {
159 .encap_hlen
= fou_encap_hlen
,
160 .build_header
= fou6_build_header
,
161 .err_handler
= gue6_err
,
164 static const struct ip6_tnl_encap_ops gue_ip6tun_ops
= {
165 .encap_hlen
= gue_encap_hlen
,
166 .build_header
= gue6_build_header
,
167 .err_handler
= gue6_err
,
170 static int ip6_tnl_encap_add_fou_ops(void)
174 ret
= ip6_tnl_encap_add_ops(&fou_ip6tun_ops
, TUNNEL_ENCAP_FOU
);
176 pr_err("can't add fou6 ops\n");
180 ret
= ip6_tnl_encap_add_ops(&gue_ip6tun_ops
, TUNNEL_ENCAP_GUE
);
182 pr_err("can't add gue6 ops\n");
183 ip6_tnl_encap_del_ops(&fou_ip6tun_ops
, TUNNEL_ENCAP_FOU
);
190 static void ip6_tnl_encap_del_fou_ops(void)
192 ip6_tnl_encap_del_ops(&fou_ip6tun_ops
, TUNNEL_ENCAP_FOU
);
193 ip6_tnl_encap_del_ops(&gue_ip6tun_ops
, TUNNEL_ENCAP_GUE
);
198 static int ip6_tnl_encap_add_fou_ops(void)
203 static void ip6_tnl_encap_del_fou_ops(void)
209 static int __init
fou6_init(void)
213 ret
= ip6_tnl_encap_add_fou_ops();
218 static void __exit
fou6_fini(void)
220 ip6_tnl_encap_del_fou_ops();
223 module_init(fou6_init
);
224 module_exit(fou6_fini
);
225 MODULE_AUTHOR("Tom Herbert <therbert@google.com>");
226 MODULE_LICENSE("GPL");