1 #include <linux/module.h>
2 #include <linux/errno.h>
3 #include <linux/socket.h>
4 #include <linux/skbuff.h>
7 #include <linux/icmpv6.h>
8 #include <linux/types.h>
9 #include <linux/kernel.h>
12 #include <net/ip6_tunnel.h>
13 #include <net/ip6_checksum.h>
14 #include <net/protocol.h>
16 #include <net/udp_tunnel.h>
18 #if IS_ENABLED(CONFIG_IPV6_FOU_TUNNEL)
20 static void fou6_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e,
21 struct flowi6 *fl6, u8 *protocol, __be16 sport)
25 skb_push(skb, sizeof(struct udphdr));
26 skb_reset_transport_header(skb);
32 uh->len = htons(skb->len);
33 udp6_set_csum(!(e->flags & TUNNEL_ENCAP_FLAG_CSUM6), skb,
34 &fl6->saddr, &fl6->daddr, skb->len);
36 *protocol = IPPROTO_UDP;
39 static int fou6_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
40 u8 *protocol, struct flowi6 *fl6)
44 int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM6 ?
45 SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
47 err = __fou_build_header(skb, e, protocol, &sport, type);
51 fou6_build_udp(skb, e, fl6, protocol, sport);
56 static int gue6_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
57 u8 *protocol, struct flowi6 *fl6)
61 int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM6 ?
62 SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
64 err = __gue_build_header(skb, e, protocol, &sport, type);
68 fou6_build_udp(skb, e, fl6, protocol, sport);
73 static int gue6_err_proto_handler(int proto, struct sk_buff *skb,
74 struct inet6_skb_parm *opt,
75 u8 type, u8 code, int offset, u32 info)
77 const struct inet6_protocol *ipprot;
79 ipprot = rcu_dereference(inet6_protos[proto]);
80 if (ipprot && ipprot->err_handler) {
81 if (!ipprot->err_handler(skb, opt, type, code, offset, info))
88 static int gue6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
89 u8 type, u8 code, int offset, __be32 info)
91 int transport_offset = skb_transport_offset(skb);
92 struct guehdr *guehdr;
96 if (skb->len < sizeof(struct udphdr) + sizeof(struct guehdr))
99 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
101 switch (guehdr->version) {
102 case 0: /* Full GUE header present */
105 /* Direct encasulation of IPv4 or IPv6 */
106 skb_set_transport_header(skb, -(int)sizeof(struct icmp6hdr));
108 switch (((struct iphdr *)guehdr)->version) {
110 ret = gue6_err_proto_handler(IPPROTO_IPIP, skb, opt,
111 type, code, offset, info);
114 ret = gue6_err_proto_handler(IPPROTO_IPV6, skb, opt,
115 type, code, offset, info);
122 default: /* Undefined version */
129 optlen = guehdr->hlen << 2;
131 if (validate_gue_flags(guehdr, optlen))
134 skb_set_transport_header(skb, -(int)sizeof(struct icmp6hdr));
135 ret = gue6_err_proto_handler(guehdr->proto_ctype, skb,
136 opt, type, code, offset, info);
139 skb_set_transport_header(skb, transport_offset);
144 static const struct ip6_tnl_encap_ops fou_ip6tun_ops = {
145 .encap_hlen = fou_encap_hlen,
146 .build_header = fou6_build_header,
147 .err_handler = gue6_err,
150 static const struct ip6_tnl_encap_ops gue_ip6tun_ops = {
151 .encap_hlen = gue_encap_hlen,
152 .build_header = gue6_build_header,
153 .err_handler = gue6_err,
156 static int ip6_tnl_encap_add_fou_ops(void)
160 ret = ip6_tnl_encap_add_ops(&fou_ip6tun_ops, TUNNEL_ENCAP_FOU);
162 pr_err("can't add fou6 ops\n");
166 ret = ip6_tnl_encap_add_ops(&gue_ip6tun_ops, TUNNEL_ENCAP_GUE);
168 pr_err("can't add gue6 ops\n");
169 ip6_tnl_encap_del_ops(&fou_ip6tun_ops, TUNNEL_ENCAP_FOU);
176 static void ip6_tnl_encap_del_fou_ops(void)
178 ip6_tnl_encap_del_ops(&fou_ip6tun_ops, TUNNEL_ENCAP_FOU);
179 ip6_tnl_encap_del_ops(&gue_ip6tun_ops, TUNNEL_ENCAP_GUE);
184 static int ip6_tnl_encap_add_fou_ops(void)
189 static void ip6_tnl_encap_del_fou_ops(void)
195 static int __init fou6_init(void)
199 ret = ip6_tnl_encap_add_fou_ops();
204 static void __exit fou6_fini(void)
206 ip6_tnl_encap_del_fou_ops();
209 module_init(fou6_init);
210 module_exit(fou6_fini);
212 MODULE_LICENSE("GPL");