xref: /linux/net/ipv6/fou6.c (revision dfecb0c5af3b07ebfa84be63a7a21bfc9e29a872)
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>
6 #include <linux/ip.h>
7 #include <linux/udp.h>
8 #include <linux/icmpv6.h>
9 #include <linux/types.h>
10 #include <linux/kernel.h>
11 #include <net/fou.h>
12 #include <net/ip.h>
13 #include <net/ip6_tunnel.h>
14 #include <net/ip6_checksum.h>
15 #include <net/protocol.h>
16 #include <net/udp.h>
17 #include <net/udp_tunnel.h>
18 
19 #if IS_ENABLED(CONFIG_IPV6_FOU_TUNNEL)
20 
21 static void fou6_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e,
22 			   struct flowi6 *fl6, u8 *protocol, __be16 sport)
23 {
24 	struct udphdr *uh;
25 
26 	skb_push(skb, sizeof(struct udphdr));
27 	skb_reset_transport_header(skb);
28 
29 	uh = udp_hdr(skb);
30 
31 	uh->dest = e->dport;
32 	uh->source = sport;
33 	uh->len = htons(skb->len);
34 	udp6_set_csum(!(e->flags & TUNNEL_ENCAP_FLAG_CSUM6), skb,
35 		      &fl6->saddr, &fl6->daddr, skb->len);
36 
37 	*protocol = IPPROTO_UDP;
38 }
39 
40 static int fou6_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
41 			     u8 *protocol, struct flowi6 *fl6)
42 {
43 	__be16 sport;
44 	int err;
45 	int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM6 ?
46 		SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
47 
48 	err = __fou_build_header(skb, e, protocol, &sport, type);
49 	if (err)
50 		return err;
51 
52 	fou6_build_udp(skb, e, fl6, protocol, sport);
53 
54 	return 0;
55 }
56 
57 static int gue6_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
58 			     u8 *protocol, struct flowi6 *fl6)
59 {
60 	__be16 sport;
61 	int err;
62 	int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM6 ?
63 		SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
64 
65 	err = __gue_build_header(skb, e, protocol, &sport, type);
66 	if (err)
67 		return err;
68 
69 	fou6_build_udp(skb, e, fl6, protocol, sport);
70 
71 	return 0;
72 }
73 
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)
77 {
78 	const struct inet6_protocol *ipprot;
79 
80 	ipprot = rcu_dereference(inet6_protos[proto]);
81 	if (ipprot && ipprot->err_handler) {
82 		if (!ipprot->err_handler(skb, opt, type, code, offset, info))
83 			return 0;
84 	}
85 
86 	return -ENOENT;
87 }
88 
89 static int gue6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
90 		    u8 type, u8 code, int offset, __be32 info)
91 {
92 	int transport_offset = skb_transport_offset(skb);
93 	struct guehdr *guehdr;
94 	size_t len, optlen;
95 	int ret;
96 
97 	len = sizeof(struct udphdr) + sizeof(struct guehdr);
98 	if (!pskb_may_pull(skb, transport_offset + len))
99 		return -EINVAL;
100 
101 	guehdr = (struct guehdr *)&udp_hdr(skb)[1];
102 
103 	switch (guehdr->version) {
104 	case 0: /* Full GUE header present */
105 		break;
106 	case 1: {
107 		/* Direct encasulation of IPv4 or IPv6 */
108 		skb_set_transport_header(skb, -(int)sizeof(struct icmp6hdr));
109 
110 		switch (((struct iphdr *)guehdr)->version) {
111 		case 4:
112 			ret = gue6_err_proto_handler(IPPROTO_IPIP, skb, opt,
113 						     type, code, offset, info);
114 			goto out;
115 		case 6:
116 			ret = gue6_err_proto_handler(IPPROTO_IPV6, skb, opt,
117 						     type, code, offset, info);
118 			goto out;
119 		default:
120 			ret = -EOPNOTSUPP;
121 			goto out;
122 		}
123 	}
124 	default: /* Undefined version */
125 		return -EOPNOTSUPP;
126 	}
127 
128 	if (guehdr->control)
129 		return -ENOENT;
130 
131 	optlen = guehdr->hlen << 2;
132 
133 	if (!pskb_may_pull(skb, transport_offset + len + optlen))
134 		return -EINVAL;
135 
136 	guehdr = (struct guehdr *)&udp_hdr(skb)[1];
137 	if (validate_gue_flags(guehdr, optlen))
138 		return -EINVAL;
139 
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.
143 	 */
144 	if (guehdr->proto_ctype == IPPROTO_UDP)
145 		return -EOPNOTSUPP;
146 
147 	skb_set_transport_header(skb, -(int)sizeof(struct icmp6hdr));
148 	ret = gue6_err_proto_handler(guehdr->proto_ctype, skb,
149 				     opt, type, code, offset, info);
150 
151 out:
152 	skb_set_transport_header(skb, transport_offset);
153 	return ret;
154 }
155 
156 
157 static const struct ip6_tnl_encap_ops fou_ip6tun_ops = {
158 	.encap_hlen = fou_encap_hlen,
159 	.build_header = fou6_build_header,
160 	.err_handler = gue6_err,
161 };
162 
163 static const struct ip6_tnl_encap_ops gue_ip6tun_ops = {
164 	.encap_hlen = gue_encap_hlen,
165 	.build_header = gue6_build_header,
166 	.err_handler = gue6_err,
167 };
168 
169 static int ip6_tnl_encap_add_fou_ops(void)
170 {
171 	int ret;
172 
173 	ret = ip6_tnl_encap_add_ops(&fou_ip6tun_ops, TUNNEL_ENCAP_FOU);
174 	if (ret < 0) {
175 		pr_err("can't add fou6 ops\n");
176 		return ret;
177 	}
178 
179 	ret = ip6_tnl_encap_add_ops(&gue_ip6tun_ops, TUNNEL_ENCAP_GUE);
180 	if (ret < 0) {
181 		pr_err("can't add gue6 ops\n");
182 		ip6_tnl_encap_del_ops(&fou_ip6tun_ops, TUNNEL_ENCAP_FOU);
183 		return ret;
184 	}
185 
186 	return 0;
187 }
188 
189 static void ip6_tnl_encap_del_fou_ops(void)
190 {
191 	ip6_tnl_encap_del_ops(&fou_ip6tun_ops, TUNNEL_ENCAP_FOU);
192 	ip6_tnl_encap_del_ops(&gue_ip6tun_ops, TUNNEL_ENCAP_GUE);
193 }
194 
195 #else
196 
197 static int ip6_tnl_encap_add_fou_ops(void)
198 {
199 	return 0;
200 }
201 
202 static void ip6_tnl_encap_del_fou_ops(void)
203 {
204 }
205 
206 #endif
207 
208 static int __init fou6_init(void)
209 {
210 	int ret;
211 
212 	ret = ip6_tnl_encap_add_fou_ops();
213 
214 	return ret;
215 }
216 
217 static void __exit fou6_fini(void)
218 {
219 	ip6_tnl_encap_del_fou_ops();
220 }
221 
222 module_init(fou6_init);
223 module_exit(fou6_fini);
224 MODULE_AUTHOR("Tom Herbert <therbert@google.com>");
225 MODULE_LICENSE("GPL");
226 MODULE_DESCRIPTION("Foo over UDP (IPv6)");
227