blob: 12ce6c526d72bd15a16a9415ed992c25039d415a [file] [log] [blame]
Tom Herbert234615512014-09-17 12:25:56 -07001#include <linux/module.h>
2#include <linux/errno.h>
3#include <linux/socket.h>
4#include <linux/skbuff.h>
5#include <linux/ip.h>
Stefano Briviob8a51b32018-11-08 12:19:23 +01006#include <linux/icmp.h>
Tom Herbert234615512014-09-17 12:25:56 -07007#include <linux/udp.h>
8#include <linux/types.h>
9#include <linux/kernel.h>
10#include <net/genetlink.h>
Tom Herbert37dd0242014-10-03 15:48:09 -070011#include <net/gue.h>
stephen hemminger9dc621a2017-05-19 09:55:54 -070012#include <net/fou.h>
Tom Herbert234615512014-09-17 12:25:56 -070013#include <net/ip.h>
Tom Herbertafe93322014-09-17 12:25:57 -070014#include <net/protocol.h>
Tom Herbert234615512014-09-17 12:25:56 -070015#include <net/udp.h>
16#include <net/udp_tunnel.h>
17#include <net/xfrm.h>
18#include <uapi/linux/fou.h>
19#include <uapi/linux/genetlink.h>
20
Tom Herbert234615512014-09-17 12:25:56 -070021struct fou {
22 struct socket *sock;
23 u8 protocol;
Tom Herbertfe881ef2015-02-10 16:30:33 -080024 u8 flags;
WANG Cong4cbcdf22015-04-10 12:00:28 -070025 __be16 port;
Tom Herbert5f914b62016-05-18 09:06:16 -070026 u8 family;
WANG Cong7a6c8c32015-04-10 12:00:30 -070027 u16 type;
Tom Herbert234615512014-09-17 12:25:56 -070028 struct list_head list;
Hannes Frederic Sowa3036fac2015-12-15 21:01:53 +010029 struct rcu_head rcu;
Tom Herbert234615512014-09-17 12:25:56 -070030};
31
Tom Herbertfe881ef2015-02-10 16:30:33 -080032#define FOU_F_REMCSUM_NOPARTIAL BIT(0)
33
Tom Herbert234615512014-09-17 12:25:56 -070034struct fou_cfg {
Tom Herbert37dd0242014-10-03 15:48:09 -070035 u16 type;
Tom Herbert234615512014-09-17 12:25:56 -070036 u8 protocol;
Tom Herbertfe881ef2015-02-10 16:30:33 -080037 u8 flags;
Tom Herbert234615512014-09-17 12:25:56 -070038 struct udp_port_cfg udp_config;
39};
40
WANG Cong02d793c2015-04-10 12:00:29 -070041static unsigned int fou_net_id;
42
43struct fou_net {
44 struct list_head fou_list;
45 struct mutex fou_lock;
46};
47
Tom Herbert234615512014-09-17 12:25:56 -070048static inline struct fou *fou_from_sock(struct sock *sk)
49{
50 return sk->sk_user_data;
51}
52
Tom Herbert5f914b62016-05-18 09:06:16 -070053static int fou_recv_pull(struct sk_buff *skb, struct fou *fou, size_t len)
Tom Herbert234615512014-09-17 12:25:56 -070054{
Tom Herbert234615512014-09-17 12:25:56 -070055 /* Remove 'len' bytes from the packet (UDP header and
Tom Herbert5024c332014-11-04 09:06:53 -080056 * FOU header if present).
Tom Herbert234615512014-09-17 12:25:56 -070057 */
Tom Herbert5f914b62016-05-18 09:06:16 -070058 if (fou->family == AF_INET)
59 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
60 else
61 ipv6_hdr(skb)->payload_len =
62 htons(ntohs(ipv6_hdr(skb)->payload_len) - len);
63
Tom Herbert234615512014-09-17 12:25:56 -070064 __skb_pull(skb, len);
65 skb_postpull_rcsum(skb, udp_hdr(skb), len);
66 skb_reset_transport_header(skb);
Jesse Grossa09a4c82016-03-19 09:32:02 -070067 return iptunnel_pull_offloads(skb);
Tom Herbert234615512014-09-17 12:25:56 -070068}
69
70static int fou_udp_recv(struct sock *sk, struct sk_buff *skb)
71{
72 struct fou *fou = fou_from_sock(sk);
73
74 if (!fou)
75 return 1;
76
Tom Herbert5f914b62016-05-18 09:06:16 -070077 if (fou_recv_pull(skb, fou, sizeof(struct udphdr)))
Jesse Grossa09a4c82016-03-19 09:32:02 -070078 goto drop;
Tom Herbert5024c332014-11-04 09:06:53 -080079
80 return -fou->protocol;
Jesse Grossa09a4c82016-03-19 09:32:02 -070081
82drop:
83 kfree_skb(skb);
84 return 0;
Tom Herbert5024c332014-11-04 09:06:53 -080085}
86
Tom Herberta8d31c12014-11-04 09:06:57 -080087static struct guehdr *gue_remcsum(struct sk_buff *skb, struct guehdr *guehdr,
Tom Herbertfe881ef2015-02-10 16:30:33 -080088 void *data, size_t hdrlen, u8 ipproto,
89 bool nopartial)
Tom Herberta8d31c12014-11-04 09:06:57 -080090{
91 __be16 *pd = data;
Tom Herbert4fd671d2014-11-25 11:21:20 -080092 size_t start = ntohs(pd[0]);
93 size_t offset = ntohs(pd[1]);
Tom Herbertb7fe10e2015-08-19 17:07:32 -070094 size_t plen = sizeof(struct udphdr) + hdrlen +
95 max_t(size_t, offset + sizeof(u16), start);
96
97 if (skb->remcsum_offload)
98 return guehdr;
Tom Herberta8d31c12014-11-04 09:06:57 -080099
Tom Herberta8d31c12014-11-04 09:06:57 -0800100 if (!pskb_may_pull(skb, plen))
101 return NULL;
102 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
103
Tom Herbertfe881ef2015-02-10 16:30:33 -0800104 skb_remcsum_process(skb, (void *)guehdr + hdrlen,
105 start, offset, nopartial);
Tom Herberta8d31c12014-11-04 09:06:57 -0800106
107 return guehdr;
108}
109
Tom Herbert5024c332014-11-04 09:06:53 -0800110static int gue_control_message(struct sk_buff *skb, struct guehdr *guehdr)
111{
112 /* No support yet */
113 kfree_skb(skb);
114 return 0;
Tom Herbert234615512014-09-17 12:25:56 -0700115}
116
Tom Herbert37dd0242014-10-03 15:48:09 -0700117static int gue_udp_recv(struct sock *sk, struct sk_buff *skb)
118{
119 struct fou *fou = fou_from_sock(sk);
Tom Herbert5024c332014-11-04 09:06:53 -0800120 size_t len, optlen, hdrlen;
Tom Herbert37dd0242014-10-03 15:48:09 -0700121 struct guehdr *guehdr;
Tom Herbert5024c332014-11-04 09:06:53 -0800122 void *data;
Tom Herberta8d31c12014-11-04 09:06:57 -0800123 u16 doffset = 0;
Lorenzo Bianconi988dc4a2019-04-09 11:47:20 +0200124 u8 proto_ctype;
Tom Herbert37dd0242014-10-03 15:48:09 -0700125
126 if (!fou)
127 return 1;
128
129 len = sizeof(struct udphdr) + sizeof(struct guehdr);
130 if (!pskb_may_pull(skb, len))
131 goto drop;
132
Tom Herbert5024c332014-11-04 09:06:53 -0800133 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
Tom Herbert37dd0242014-10-03 15:48:09 -0700134
Tom Herbertc1e48af2016-06-06 16:06:02 -0700135 switch (guehdr->version) {
136 case 0: /* Full GUE header present */
137 break;
138
139 case 1: {
140 /* Direct encasulation of IPv4 or IPv6 */
141
142 int prot;
143
144 switch (((struct iphdr *)guehdr)->version) {
145 case 4:
146 prot = IPPROTO_IPIP;
147 break;
148 case 6:
149 prot = IPPROTO_IPV6;
150 break;
151 default:
152 goto drop;
153 }
154
155 if (fou_recv_pull(skb, fou, sizeof(struct udphdr)))
156 goto drop;
157
158 return -prot;
159 }
160
161 default: /* Undefined version */
162 goto drop;
163 }
164
Tom Herbert5024c332014-11-04 09:06:53 -0800165 optlen = guehdr->hlen << 2;
166 len += optlen;
167
Tom Herbert37dd0242014-10-03 15:48:09 -0700168 if (!pskb_may_pull(skb, len))
169 goto drop;
170
Tom Herbert5024c332014-11-04 09:06:53 -0800171 /* guehdr may change after pull */
172 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
Li RongQingd8f00d22014-10-17 16:53:47 +0800173
Tom Herbert5024c332014-11-04 09:06:53 -0800174 hdrlen = sizeof(struct guehdr) + optlen;
175
176 if (guehdr->version != 0 || validate_gue_flags(guehdr, optlen))
Tom Herbert37dd0242014-10-03 15:48:09 -0700177 goto drop;
178
Tom Herberta8d31c12014-11-04 09:06:57 -0800179 hdrlen = sizeof(struct guehdr) + optlen;
180
Tom Herbert5f914b62016-05-18 09:06:16 -0700181 if (fou->family == AF_INET)
182 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
183 else
184 ipv6_hdr(skb)->payload_len =
185 htons(ntohs(ipv6_hdr(skb)->payload_len) - len);
Tom Herberta8d31c12014-11-04 09:06:57 -0800186
Tom Herberta8d31c12014-11-04 09:06:57 -0800187 /* Pull csum through the guehdr now . This can be used if
188 * there is a remote checksum offload.
189 */
190 skb_postpull_rcsum(skb, udp_hdr(skb), len);
Tom Herbert5024c332014-11-04 09:06:53 -0800191
192 data = &guehdr[1];
193
194 if (guehdr->flags & GUE_FLAG_PRIV) {
Tom Herberta8d31c12014-11-04 09:06:57 -0800195 __be32 flags = *(__be32 *)(data + doffset);
Tom Herbert5024c332014-11-04 09:06:53 -0800196
Tom Herberta8d31c12014-11-04 09:06:57 -0800197 doffset += GUE_LEN_PRIV;
198
199 if (flags & GUE_PFLAG_REMCSUM) {
200 guehdr = gue_remcsum(skb, guehdr, data + doffset,
Tom Herbertfe881ef2015-02-10 16:30:33 -0800201 hdrlen, guehdr->proto_ctype,
202 !!(fou->flags &
203 FOU_F_REMCSUM_NOPARTIAL));
Tom Herberta8d31c12014-11-04 09:06:57 -0800204 if (!guehdr)
205 goto drop;
206
207 data = &guehdr[1];
208
209 doffset += GUE_PLEN_REMCSUM;
210 }
Tom Herbert37dd0242014-10-03 15:48:09 -0700211 }
212
Tom Herbert5024c332014-11-04 09:06:53 -0800213 if (unlikely(guehdr->control))
214 return gue_control_message(skb, guehdr);
215
Lorenzo Bianconi988dc4a2019-04-09 11:47:20 +0200216 proto_ctype = guehdr->proto_ctype;
Tom Herbert4fd671d2014-11-25 11:21:20 -0800217 __skb_pull(skb, sizeof(struct udphdr) + hdrlen);
Tom Herberta8d31c12014-11-04 09:06:57 -0800218 skb_reset_transport_header(skb);
219
Jesse Grossa09a4c82016-03-19 09:32:02 -0700220 if (iptunnel_pull_offloads(skb))
221 goto drop;
222
Lorenzo Bianconi988dc4a2019-04-09 11:47:20 +0200223 return -proto_ctype;
Tom Herbert5024c332014-11-04 09:06:53 -0800224
Tom Herbert37dd0242014-10-03 15:48:09 -0700225drop:
226 kfree_skb(skb);
227 return 0;
228}
229
David Millerd4546c22018-06-24 14:13:49 +0900230static struct sk_buff *fou_gro_receive(struct sock *sk,
231 struct list_head *head,
232 struct sk_buff *skb)
Tom Herbertafe93322014-09-17 12:25:57 -0700233{
Tom Herbertd92283e2016-04-05 08:22:54 -0700234 u8 proto = fou_from_sock(sk)->protocol;
Tom Herbertefc98d02014-10-03 15:48:08 -0700235 const struct net_offload **offloads;
David Millerd4546c22018-06-24 14:13:49 +0900236 const struct net_offload *ops;
237 struct sk_buff *pp = NULL;
Tom Herbertafe93322014-09-17 12:25:57 -0700238
Alexander Duyckc3483382016-03-29 14:55:22 -0700239 /* We can clear the encap_mark for FOU as we are essentially doing
240 * one of two possible things. We are either adding an L4 tunnel
241 * header to the outer L3 tunnel header, or we are are simply
242 * treating the GRE tunnel header as though it is a UDP protocol
243 * specific header such as VXLAN or GENEVE.
244 */
245 NAPI_GRO_CB(skb)->encap_mark = 0;
246
Alexander Duycka0ca1532016-04-05 09:13:39 -0700247 /* Flag this frame as already having an outer encap header */
248 NAPI_GRO_CB(skb)->is_fou = 1;
249
Tom Herbertafe93322014-09-17 12:25:57 -0700250 rcu_read_lock();
Tom Herbertefc98d02014-10-03 15:48:08 -0700251 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700252 ops = rcu_dereference(offloads[proto]);
253 if (!ops || !ops->callbacks.gro_receive)
254 goto out_unlock;
255
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200256 pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
Tom Herbertafe93322014-09-17 12:25:57 -0700257
258out_unlock:
259 rcu_read_unlock();
260
261 return pp;
262}
263
Tom Herbertd92283e2016-04-05 08:22:54 -0700264static int fou_gro_complete(struct sock *sk, struct sk_buff *skb,
265 int nhoff)
Tom Herbertafe93322014-09-17 12:25:57 -0700266{
267 const struct net_offload *ops;
Tom Herbertd92283e2016-04-05 08:22:54 -0700268 u8 proto = fou_from_sock(sk)->protocol;
Tom Herbertafe93322014-09-17 12:25:57 -0700269 int err = -ENOSYS;
Tom Herbertefc98d02014-10-03 15:48:08 -0700270 const struct net_offload **offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700271
272 rcu_read_lock();
Tom Herbertefc98d02014-10-03 15:48:08 -0700273 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700274 ops = rcu_dereference(offloads[proto]);
275 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
276 goto out_unlock;
277
278 err = ops->callbacks.gro_complete(skb, nhoff);
279
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700280 skb_set_inner_mac_header(skb, nhoff);
281
Tom Herbertafe93322014-09-17 12:25:57 -0700282out_unlock:
283 rcu_read_unlock();
284
285 return err;
286}
287
Tom Herberta8d31c12014-11-04 09:06:57 -0800288static struct guehdr *gue_gro_remcsum(struct sk_buff *skb, unsigned int off,
289 struct guehdr *guehdr, void *data,
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700290 size_t hdrlen, struct gro_remcsum *grc,
291 bool nopartial)
Tom Herberta8d31c12014-11-04 09:06:57 -0800292{
293 __be16 *pd = data;
Tom Herbert4fd671d2014-11-25 11:21:20 -0800294 size_t start = ntohs(pd[0]);
295 size_t offset = ntohs(pd[1]);
Tom Herberta8d31c12014-11-04 09:06:57 -0800296
297 if (skb->remcsum_offload)
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700298 return guehdr;
Tom Herberta8d31c12014-11-04 09:06:57 -0800299
Tom Herbert4fd671d2014-11-25 11:21:20 -0800300 if (!NAPI_GRO_CB(skb)->csum_valid)
Tom Herberta8d31c12014-11-04 09:06:57 -0800301 return NULL;
302
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700303 guehdr = skb_gro_remcsum_process(skb, (void *)guehdr, off, hdrlen,
304 start, offset, grc, nopartial);
Tom Herberta8d31c12014-11-04 09:06:57 -0800305
306 skb->remcsum_offload = 1;
307
308 return guehdr;
309}
310
David Millerd4546c22018-06-24 14:13:49 +0900311static struct sk_buff *gue_gro_receive(struct sock *sk,
312 struct list_head *head,
313 struct sk_buff *skb)
Tom Herbert37dd0242014-10-03 15:48:09 -0700314{
315 const struct net_offload **offloads;
316 const struct net_offload *ops;
David Millerd4546c22018-06-24 14:13:49 +0900317 struct sk_buff *pp = NULL;
Tom Herbert37dd0242014-10-03 15:48:09 -0700318 struct sk_buff *p;
Tom Herbert37dd0242014-10-03 15:48:09 -0700319 struct guehdr *guehdr;
Tom Herbert5024c332014-11-04 09:06:53 -0800320 size_t len, optlen, hdrlen, off;
321 void *data;
Tom Herberta8d31c12014-11-04 09:06:57 -0800322 u16 doffset = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700323 int flush = 1;
Tom Herbertd92283e2016-04-05 08:22:54 -0700324 struct fou *fou = fou_from_sock(sk);
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800325 struct gro_remcsum grc;
Tom Herbertc1e48af2016-06-06 16:06:02 -0700326 u8 proto;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800327
328 skb_gro_remcsum_init(&grc);
Tom Herbert37dd0242014-10-03 15:48:09 -0700329
330 off = skb_gro_offset(skb);
Tom Herbert5024c332014-11-04 09:06:53 -0800331 len = off + sizeof(*guehdr);
332
Tom Herbert37dd0242014-10-03 15:48:09 -0700333 guehdr = skb_gro_header_fast(skb, off);
Tom Herbert5024c332014-11-04 09:06:53 -0800334 if (skb_gro_header_hard(skb, len)) {
335 guehdr = skb_gro_header_slow(skb, len, off);
Tom Herbert37dd0242014-10-03 15:48:09 -0700336 if (unlikely(!guehdr))
337 goto out;
338 }
339
Tom Herbertc1e48af2016-06-06 16:06:02 -0700340 switch (guehdr->version) {
341 case 0:
342 break;
343 case 1:
344 switch (((struct iphdr *)guehdr)->version) {
345 case 4:
346 proto = IPPROTO_IPIP;
347 break;
348 case 6:
349 proto = IPPROTO_IPV6;
350 break;
351 default:
352 goto out;
353 }
354 goto next_proto;
355 default:
356 goto out;
357 }
358
Tom Herbert5024c332014-11-04 09:06:53 -0800359 optlen = guehdr->hlen << 2;
360 len += optlen;
Tom Herbert37dd0242014-10-03 15:48:09 -0700361
Tom Herbert5024c332014-11-04 09:06:53 -0800362 if (skb_gro_header_hard(skb, len)) {
363 guehdr = skb_gro_header_slow(skb, len, off);
Tom Herbert37dd0242014-10-03 15:48:09 -0700364 if (unlikely(!guehdr))
Tom Herbert5024c332014-11-04 09:06:53 -0800365 goto out;
366 }
367
368 if (unlikely(guehdr->control) || guehdr->version != 0 ||
369 validate_gue_flags(guehdr, optlen))
370 goto out;
371
372 hdrlen = sizeof(*guehdr) + optlen;
373
Tom Herberta8d31c12014-11-04 09:06:57 -0800374 /* Adjust NAPI_GRO_CB(skb)->csum to account for guehdr,
375 * this is needed if there is a remote checkcsum offload.
376 */
Tom Herbert5024c332014-11-04 09:06:53 -0800377 skb_gro_postpull_rcsum(skb, guehdr, hdrlen);
378
379 data = &guehdr[1];
380
381 if (guehdr->flags & GUE_FLAG_PRIV) {
Tom Herberta8d31c12014-11-04 09:06:57 -0800382 __be32 flags = *(__be32 *)(data + doffset);
Tom Herbert5024c332014-11-04 09:06:53 -0800383
Tom Herberta8d31c12014-11-04 09:06:57 -0800384 doffset += GUE_LEN_PRIV;
385
386 if (flags & GUE_PFLAG_REMCSUM) {
387 guehdr = gue_gro_remcsum(skb, off, guehdr,
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700388 data + doffset, hdrlen, &grc,
Tom Herbertfe881ef2015-02-10 16:30:33 -0800389 !!(fou->flags &
390 FOU_F_REMCSUM_NOPARTIAL));
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700391
Tom Herberta8d31c12014-11-04 09:06:57 -0800392 if (!guehdr)
393 goto out;
394
395 data = &guehdr[1];
396
397 doffset += GUE_PLEN_REMCSUM;
398 }
Tom Herbert37dd0242014-10-03 15:48:09 -0700399 }
400
Tom Herberta8d31c12014-11-04 09:06:57 -0800401 skb_gro_pull(skb, hdrlen);
402
David Millerd4546c22018-06-24 14:13:49 +0900403 list_for_each_entry(p, head, list) {
Tom Herbert37dd0242014-10-03 15:48:09 -0700404 const struct guehdr *guehdr2;
405
406 if (!NAPI_GRO_CB(p)->same_flow)
407 continue;
408
409 guehdr2 = (struct guehdr *)(p->data + off);
410
411 /* Compare base GUE header to be equal (covers
Tom Herbert5024c332014-11-04 09:06:53 -0800412 * hlen, version, proto_ctype, and flags.
Tom Herbert37dd0242014-10-03 15:48:09 -0700413 */
414 if (guehdr->word != guehdr2->word) {
415 NAPI_GRO_CB(p)->same_flow = 0;
416 continue;
417 }
418
419 /* Compare optional fields are the same. */
420 if (guehdr->hlen && memcmp(&guehdr[1], &guehdr2[1],
421 guehdr->hlen << 2)) {
422 NAPI_GRO_CB(p)->same_flow = 0;
423 continue;
424 }
425 }
426
Tom Herbertc1e48af2016-06-06 16:06:02 -0700427 proto = guehdr->proto_ctype;
428
429next_proto:
430
Alexander Duyckc3483382016-03-29 14:55:22 -0700431 /* We can clear the encap_mark for GUE as we are essentially doing
432 * one of two possible things. We are either adding an L4 tunnel
433 * header to the outer L3 tunnel header, or we are are simply
434 * treating the GRE tunnel header as though it is a UDP protocol
435 * specific header such as VXLAN or GENEVE.
436 */
437 NAPI_GRO_CB(skb)->encap_mark = 0;
438
Alexander Duycka0ca1532016-04-05 09:13:39 -0700439 /* Flag this frame as already having an outer encap header */
440 NAPI_GRO_CB(skb)->is_fou = 1;
441
Tom Herbert5024c332014-11-04 09:06:53 -0800442 rcu_read_lock();
443 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
Tom Herbertc1e48af2016-06-06 16:06:02 -0700444 ops = rcu_dereference(offloads[proto]);
Tom Herbert27013662015-08-19 17:07:34 -0700445 if (WARN_ON_ONCE(!ops || !ops->callbacks.gro_receive))
Tom Herbert5024c332014-11-04 09:06:53 -0800446 goto out_unlock;
Tom Herbert37dd0242014-10-03 15:48:09 -0700447
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200448 pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800449 flush = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700450
451out_unlock:
452 rcu_read_unlock();
453out:
Sabrina Dubroca603d4cf2018-06-30 17:38:55 +0200454 skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
Tom Herbert37dd0242014-10-03 15:48:09 -0700455
456 return pp;
457}
458
Tom Herbertd92283e2016-04-05 08:22:54 -0700459static int gue_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
Tom Herbert37dd0242014-10-03 15:48:09 -0700460{
461 const struct net_offload **offloads;
462 struct guehdr *guehdr = (struct guehdr *)(skb->data + nhoff);
463 const struct net_offload *ops;
Tom Herbertc1e48af2016-06-06 16:06:02 -0700464 unsigned int guehlen = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700465 u8 proto;
466 int err = -ENOENT;
467
Tom Herbertc1e48af2016-06-06 16:06:02 -0700468 switch (guehdr->version) {
469 case 0:
470 proto = guehdr->proto_ctype;
471 guehlen = sizeof(*guehdr) + (guehdr->hlen << 2);
472 break;
473 case 1:
474 switch (((struct iphdr *)guehdr)->version) {
475 case 4:
476 proto = IPPROTO_IPIP;
477 break;
478 case 6:
479 proto = IPPROTO_IPV6;
480 break;
481 default:
482 return err;
483 }
484 break;
485 default:
486 return err;
487 }
Tom Herbert37dd0242014-10-03 15:48:09 -0700488
489 rcu_read_lock();
490 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
491 ops = rcu_dereference(offloads[proto]);
492 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
493 goto out_unlock;
494
495 err = ops->callbacks.gro_complete(skb, nhoff + guehlen);
496
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700497 skb_set_inner_mac_header(skb, nhoff + guehlen);
498
Tom Herbert37dd0242014-10-03 15:48:09 -0700499out_unlock:
500 rcu_read_unlock();
501 return err;
502}
503
WANG Cong02d793c2015-04-10 12:00:29 -0700504static int fou_add_to_port_list(struct net *net, struct fou *fou)
Tom Herbert234615512014-09-17 12:25:56 -0700505{
WANG Cong02d793c2015-04-10 12:00:29 -0700506 struct fou_net *fn = net_generic(net, fou_net_id);
Tom Herbert234615512014-09-17 12:25:56 -0700507 struct fou *fout;
508
WANG Cong02d793c2015-04-10 12:00:29 -0700509 mutex_lock(&fn->fou_lock);
510 list_for_each_entry(fout, &fn->fou_list, list) {
Tom Herbert5f914b62016-05-18 09:06:16 -0700511 if (fou->port == fout->port &&
512 fou->family == fout->family) {
WANG Cong02d793c2015-04-10 12:00:29 -0700513 mutex_unlock(&fn->fou_lock);
Tom Herbert234615512014-09-17 12:25:56 -0700514 return -EALREADY;
515 }
516 }
517
WANG Cong02d793c2015-04-10 12:00:29 -0700518 list_add(&fou->list, &fn->fou_list);
519 mutex_unlock(&fn->fou_lock);
Tom Herbert234615512014-09-17 12:25:56 -0700520
521 return 0;
522}
523
524static void fou_release(struct fou *fou)
525{
526 struct socket *sock = fou->sock;
Tom Herbert234615512014-09-17 12:25:56 -0700527
Tom Herbert234615512014-09-17 12:25:56 -0700528 list_del(&fou->list);
WANG Cong02d793c2015-04-10 12:00:29 -0700529 udp_tunnel_sock_release(sock);
Tom Herbert234615512014-09-17 12:25:56 -0700530
Hannes Frederic Sowa3036fac2015-12-15 21:01:53 +0100531 kfree_rcu(fou, rcu);
Tom Herbert234615512014-09-17 12:25:56 -0700532}
533
534static int fou_create(struct net *net, struct fou_cfg *cfg,
535 struct socket **sockp)
536{
Tom Herbert234615512014-09-17 12:25:56 -0700537 struct socket *sock = NULL;
WANG Cong02d793c2015-04-10 12:00:29 -0700538 struct fou *fou = NULL;
Tom Herbert234615512014-09-17 12:25:56 -0700539 struct sock *sk;
Tom Herbert440924b2016-05-18 09:06:14 -0700540 struct udp_tunnel_sock_cfg tunnel_cfg;
WANG Cong02d793c2015-04-10 12:00:29 -0700541 int err;
Tom Herbert234615512014-09-17 12:25:56 -0700542
543 /* Open UDP socket */
544 err = udp_sock_create(net, &cfg->udp_config, &sock);
545 if (err < 0)
546 goto error;
547
548 /* Allocate FOU port structure */
549 fou = kzalloc(sizeof(*fou), GFP_KERNEL);
550 if (!fou) {
551 err = -ENOMEM;
552 goto error;
553 }
554
555 sk = sock->sk;
556
Tom Herbert37dd0242014-10-03 15:48:09 -0700557 fou->port = cfg->udp_config.local_udp_port;
Tom Herbert5f914b62016-05-18 09:06:16 -0700558 fou->family = cfg->udp_config.family;
559 fou->flags = cfg->flags;
Tom Herbert440924b2016-05-18 09:06:14 -0700560 fou->type = cfg->type;
561 fou->sock = sock;
562
563 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
564 tunnel_cfg.encap_type = 1;
565 tunnel_cfg.sk_user_data = fou;
566 tunnel_cfg.encap_destroy = NULL;
Tom Herbert37dd0242014-10-03 15:48:09 -0700567
568 /* Initial for fou type */
569 switch (cfg->type) {
570 case FOU_ENCAP_DIRECT:
Tom Herbert440924b2016-05-18 09:06:14 -0700571 tunnel_cfg.encap_rcv = fou_udp_recv;
572 tunnel_cfg.gro_receive = fou_gro_receive;
573 tunnel_cfg.gro_complete = fou_gro_complete;
574 fou->protocol = cfg->protocol;
Tom Herbert37dd0242014-10-03 15:48:09 -0700575 break;
576 case FOU_ENCAP_GUE:
Tom Herbert440924b2016-05-18 09:06:14 -0700577 tunnel_cfg.encap_rcv = gue_udp_recv;
578 tunnel_cfg.gro_receive = gue_gro_receive;
579 tunnel_cfg.gro_complete = gue_gro_complete;
Tom Herbert37dd0242014-10-03 15:48:09 -0700580 break;
581 default:
582 err = -EINVAL;
583 goto error;
584 }
Tom Herbert234615512014-09-17 12:25:56 -0700585
Tom Herbert440924b2016-05-18 09:06:14 -0700586 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Tom Herbert234615512014-09-17 12:25:56 -0700587
588 sk->sk_allocation = GFP_ATOMIC;
589
WANG Cong02d793c2015-04-10 12:00:29 -0700590 err = fou_add_to_port_list(net, fou);
Tom Herbert234615512014-09-17 12:25:56 -0700591 if (err)
592 goto error;
593
594 if (sockp)
595 *sockp = sock;
596
597 return 0;
598
599error:
600 kfree(fou);
601 if (sock)
WANG Cong02d793c2015-04-10 12:00:29 -0700602 udp_tunnel_sock_release(sock);
Tom Herbert234615512014-09-17 12:25:56 -0700603
604 return err;
605}
606
607static int fou_destroy(struct net *net, struct fou_cfg *cfg)
608{
WANG Cong02d793c2015-04-10 12:00:29 -0700609 struct fou_net *fn = net_generic(net, fou_net_id);
WANG Cong4cbcdf22015-04-10 12:00:28 -0700610 __be16 port = cfg->udp_config.local_udp_port;
Tom Herbert5f914b62016-05-18 09:06:16 -0700611 u8 family = cfg->udp_config.family;
Tom Herbert234615512014-09-17 12:25:56 -0700612 int err = -EINVAL;
WANG Cong02d793c2015-04-10 12:00:29 -0700613 struct fou *fou;
Tom Herbert234615512014-09-17 12:25:56 -0700614
WANG Cong02d793c2015-04-10 12:00:29 -0700615 mutex_lock(&fn->fou_lock);
616 list_for_each_entry(fou, &fn->fou_list, list) {
Tom Herbert5f914b62016-05-18 09:06:16 -0700617 if (fou->port == port && fou->family == family) {
Tom Herbert234615512014-09-17 12:25:56 -0700618 fou_release(fou);
619 err = 0;
620 break;
621 }
622 }
WANG Cong02d793c2015-04-10 12:00:29 -0700623 mutex_unlock(&fn->fou_lock);
Tom Herbert234615512014-09-17 12:25:56 -0700624
625 return err;
626}
627
Johannes Berg489111e2016-10-24 14:40:03 +0200628static struct genl_family fou_nl_family;
Tom Herbert234615512014-09-17 12:25:56 -0700629
stephen hemminger3f18ff22016-08-31 15:19:37 -0700630static const struct nla_policy fou_nl_policy[FOU_ATTR_MAX + 1] = {
Tom Herbert234615512014-09-17 12:25:56 -0700631 [FOU_ATTR_PORT] = { .type = NLA_U16, },
632 [FOU_ATTR_AF] = { .type = NLA_U8, },
633 [FOU_ATTR_IPPROTO] = { .type = NLA_U8, },
Tom Herbert37dd0242014-10-03 15:48:09 -0700634 [FOU_ATTR_TYPE] = { .type = NLA_U8, },
Tom Herbertfe881ef2015-02-10 16:30:33 -0800635 [FOU_ATTR_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG, },
Tom Herbert234615512014-09-17 12:25:56 -0700636};
637
638static int parse_nl_config(struct genl_info *info,
639 struct fou_cfg *cfg)
640{
641 memset(cfg, 0, sizeof(*cfg));
642
643 cfg->udp_config.family = AF_INET;
644
645 if (info->attrs[FOU_ATTR_AF]) {
646 u8 family = nla_get_u8(info->attrs[FOU_ATTR_AF]);
647
Tom Herbert5f914b62016-05-18 09:06:16 -0700648 switch (family) {
649 case AF_INET:
650 break;
651 case AF_INET6:
652 cfg->udp_config.ipv6_v6only = 1;
653 break;
654 default:
655 return -EAFNOSUPPORT;
656 }
Tom Herbert234615512014-09-17 12:25:56 -0700657
658 cfg->udp_config.family = family;
659 }
660
661 if (info->attrs[FOU_ATTR_PORT]) {
WANG Cong4cbcdf22015-04-10 12:00:28 -0700662 __be16 port = nla_get_be16(info->attrs[FOU_ATTR_PORT]);
Tom Herbert234615512014-09-17 12:25:56 -0700663
664 cfg->udp_config.local_udp_port = port;
665 }
666
667 if (info->attrs[FOU_ATTR_IPPROTO])
668 cfg->protocol = nla_get_u8(info->attrs[FOU_ATTR_IPPROTO]);
669
Tom Herbert37dd0242014-10-03 15:48:09 -0700670 if (info->attrs[FOU_ATTR_TYPE])
671 cfg->type = nla_get_u8(info->attrs[FOU_ATTR_TYPE]);
672
Tom Herbertfe881ef2015-02-10 16:30:33 -0800673 if (info->attrs[FOU_ATTR_REMCSUM_NOPARTIAL])
674 cfg->flags |= FOU_F_REMCSUM_NOPARTIAL;
675
Tom Herbert234615512014-09-17 12:25:56 -0700676 return 0;
677}
678
679static int fou_nl_cmd_add_port(struct sk_buff *skb, struct genl_info *info)
680{
WANG Cong02d793c2015-04-10 12:00:29 -0700681 struct net *net = genl_info_net(info);
Tom Herbert234615512014-09-17 12:25:56 -0700682 struct fou_cfg cfg;
683 int err;
684
685 err = parse_nl_config(info, &cfg);
686 if (err)
687 return err;
688
WANG Cong02d793c2015-04-10 12:00:29 -0700689 return fou_create(net, &cfg, NULL);
Tom Herbert234615512014-09-17 12:25:56 -0700690}
691
692static int fou_nl_cmd_rm_port(struct sk_buff *skb, struct genl_info *info)
693{
WANG Cong02d793c2015-04-10 12:00:29 -0700694 struct net *net = genl_info_net(info);
Tom Herbert234615512014-09-17 12:25:56 -0700695 struct fou_cfg cfg;
WANG Cong67270632015-04-10 12:00:27 -0700696 int err;
Tom Herbert234615512014-09-17 12:25:56 -0700697
WANG Cong67270632015-04-10 12:00:27 -0700698 err = parse_nl_config(info, &cfg);
699 if (err)
700 return err;
Tom Herbert234615512014-09-17 12:25:56 -0700701
WANG Cong02d793c2015-04-10 12:00:29 -0700702 return fou_destroy(net, &cfg);
Tom Herbert234615512014-09-17 12:25:56 -0700703}
704
WANG Cong7a6c8c32015-04-10 12:00:30 -0700705static int fou_fill_info(struct fou *fou, struct sk_buff *msg)
706{
707 if (nla_put_u8(msg, FOU_ATTR_AF, fou->sock->sk->sk_family) ||
708 nla_put_be16(msg, FOU_ATTR_PORT, fou->port) ||
709 nla_put_u8(msg, FOU_ATTR_IPPROTO, fou->protocol) ||
710 nla_put_u8(msg, FOU_ATTR_TYPE, fou->type))
711 return -1;
712
713 if (fou->flags & FOU_F_REMCSUM_NOPARTIAL)
714 if (nla_put_flag(msg, FOU_ATTR_REMCSUM_NOPARTIAL))
715 return -1;
716 return 0;
717}
718
719static int fou_dump_info(struct fou *fou, u32 portid, u32 seq,
720 u32 flags, struct sk_buff *skb, u8 cmd)
721{
722 void *hdr;
723
724 hdr = genlmsg_put(skb, portid, seq, &fou_nl_family, flags, cmd);
725 if (!hdr)
726 return -ENOMEM;
727
728 if (fou_fill_info(fou, skb) < 0)
729 goto nla_put_failure;
730
731 genlmsg_end(skb, hdr);
732 return 0;
733
734nla_put_failure:
735 genlmsg_cancel(skb, hdr);
736 return -EMSGSIZE;
737}
738
739static int fou_nl_cmd_get_port(struct sk_buff *skb, struct genl_info *info)
740{
741 struct net *net = genl_info_net(info);
742 struct fou_net *fn = net_generic(net, fou_net_id);
743 struct sk_buff *msg;
744 struct fou_cfg cfg;
745 struct fou *fout;
746 __be16 port;
Tom Herbert5f914b62016-05-18 09:06:16 -0700747 u8 family;
WANG Cong7a6c8c32015-04-10 12:00:30 -0700748 int ret;
749
750 ret = parse_nl_config(info, &cfg);
751 if (ret)
752 return ret;
753 port = cfg.udp_config.local_udp_port;
754 if (port == 0)
755 return -EINVAL;
756
Tom Herbert5f914b62016-05-18 09:06:16 -0700757 family = cfg.udp_config.family;
758 if (family != AF_INET && family != AF_INET6)
759 return -EINVAL;
760
WANG Cong7a6c8c32015-04-10 12:00:30 -0700761 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
762 if (!msg)
763 return -ENOMEM;
764
765 ret = -ESRCH;
766 mutex_lock(&fn->fou_lock);
767 list_for_each_entry(fout, &fn->fou_list, list) {
Tom Herbert5f914b62016-05-18 09:06:16 -0700768 if (port == fout->port && family == fout->family) {
WANG Cong7a6c8c32015-04-10 12:00:30 -0700769 ret = fou_dump_info(fout, info->snd_portid,
770 info->snd_seq, 0, msg,
771 info->genlhdr->cmd);
772 break;
773 }
774 }
775 mutex_unlock(&fn->fou_lock);
776 if (ret < 0)
777 goto out_free;
778
779 return genlmsg_reply(msg, info);
780
781out_free:
782 nlmsg_free(msg);
783 return ret;
784}
785
786static int fou_nl_dump(struct sk_buff *skb, struct netlink_callback *cb)
787{
788 struct net *net = sock_net(skb->sk);
789 struct fou_net *fn = net_generic(net, fou_net_id);
790 struct fou *fout;
791 int idx = 0, ret;
792
793 mutex_lock(&fn->fou_lock);
794 list_for_each_entry(fout, &fn->fou_list, list) {
795 if (idx++ < cb->args[0])
796 continue;
797 ret = fou_dump_info(fout, NETLINK_CB(cb->skb).portid,
798 cb->nlh->nlmsg_seq, NLM_F_MULTI,
799 skb, FOU_CMD_GET);
800 if (ret)
WANG Cong540207a2015-04-15 11:48:49 -0700801 break;
WANG Cong7a6c8c32015-04-10 12:00:30 -0700802 }
803 mutex_unlock(&fn->fou_lock);
804
WANG Cong7a6c8c32015-04-10 12:00:30 -0700805 cb->args[0] = idx;
806 return skb->len;
807}
808
Tom Herbert234615512014-09-17 12:25:56 -0700809static const struct genl_ops fou_nl_ops[] = {
810 {
811 .cmd = FOU_CMD_ADD,
812 .doit = fou_nl_cmd_add_port,
813 .policy = fou_nl_policy,
814 .flags = GENL_ADMIN_PERM,
815 },
816 {
817 .cmd = FOU_CMD_DEL,
818 .doit = fou_nl_cmd_rm_port,
819 .policy = fou_nl_policy,
820 .flags = GENL_ADMIN_PERM,
821 },
WANG Cong7a6c8c32015-04-10 12:00:30 -0700822 {
823 .cmd = FOU_CMD_GET,
824 .doit = fou_nl_cmd_get_port,
825 .dumpit = fou_nl_dump,
826 .policy = fou_nl_policy,
827 },
Tom Herbert234615512014-09-17 12:25:56 -0700828};
829
Johannes Berg56989f62016-10-24 14:40:05 +0200830static struct genl_family fou_nl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +0200831 .hdrsize = 0,
832 .name = FOU_GENL_NAME,
833 .version = FOU_GENL_VERSION,
834 .maxattr = FOU_ATTR_MAX,
835 .netnsok = true,
836 .module = THIS_MODULE,
837 .ops = fou_nl_ops,
838 .n_ops = ARRAY_SIZE(fou_nl_ops),
839};
840
Tom Herberta8c5f902014-11-12 11:54:09 -0800841size_t fou_encap_hlen(struct ip_tunnel_encap *e)
842{
843 return sizeof(struct udphdr);
844}
845EXPORT_SYMBOL(fou_encap_hlen);
846
847size_t gue_encap_hlen(struct ip_tunnel_encap *e)
848{
849 size_t len;
850 bool need_priv = false;
851
852 len = sizeof(struct udphdr) + sizeof(struct guehdr);
853
854 if (e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) {
855 len += GUE_PLEN_REMCSUM;
856 need_priv = true;
857 }
858
859 len += need_priv ? GUE_LEN_PRIV : 0;
860
861 return len;
862}
863EXPORT_SYMBOL(gue_encap_hlen);
864
Tom Herbertdc969b82016-05-18 09:06:15 -0700865int __fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
866 u8 *protocol, __be16 *sport, int type)
867{
868 int err;
869
870 err = iptunnel_handle_offloads(skb, type);
871 if (err)
872 return err;
873
874 *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
875 skb, 0, 0, false);
876
877 return 0;
878}
879EXPORT_SYMBOL(__fou_build_header);
880
Tom Herbertdc969b82016-05-18 09:06:15 -0700881int __gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
882 u8 *protocol, __be16 *sport, int type)
Tom Herbert63487ba2014-11-04 09:06:51 -0800883{
Tom Herbert63487ba2014-11-04 09:06:51 -0800884 struct guehdr *guehdr;
Tom Herbertb17f7092014-11-04 09:06:56 -0800885 size_t hdrlen, optlen = 0;
Tom Herbert5024c332014-11-04 09:06:53 -0800886 void *data;
887 bool need_priv = false;
Alexander Duyckaed069d2016-04-14 15:33:37 -0400888 int err;
Tom Herbert5024c332014-11-04 09:06:53 -0800889
Tom Herbertb17f7092014-11-04 09:06:56 -0800890 if ((e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) &&
891 skb->ip_summed == CHECKSUM_PARTIAL) {
Tom Herbertb17f7092014-11-04 09:06:56 -0800892 optlen += GUE_PLEN_REMCSUM;
893 type |= SKB_GSO_TUNNEL_REMCSUM;
894 need_priv = true;
895 }
896
Tom Herbert5024c332014-11-04 09:06:53 -0800897 optlen += need_priv ? GUE_LEN_PRIV : 0;
Tom Herbert63487ba2014-11-04 09:06:51 -0800898
Alexander Duyckaed069d2016-04-14 15:33:37 -0400899 err = iptunnel_handle_offloads(skb, type);
900 if (err)
901 return err;
Tom Herbert63487ba2014-11-04 09:06:51 -0800902
903 /* Get source port (based on flow hash) before skb_push */
Tom Herbertdc969b82016-05-18 09:06:15 -0700904 *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
905 skb, 0, 0, false);
Tom Herbert63487ba2014-11-04 09:06:51 -0800906
Tom Herbertb17f7092014-11-04 09:06:56 -0800907 hdrlen = sizeof(struct guehdr) + optlen;
908
909 skb_push(skb, hdrlen);
Tom Herbert63487ba2014-11-04 09:06:51 -0800910
911 guehdr = (struct guehdr *)skb->data;
912
Tom Herbert5024c332014-11-04 09:06:53 -0800913 guehdr->control = 0;
Tom Herbert63487ba2014-11-04 09:06:51 -0800914 guehdr->version = 0;
Tom Herbert5024c332014-11-04 09:06:53 -0800915 guehdr->hlen = optlen >> 2;
Tom Herbert63487ba2014-11-04 09:06:51 -0800916 guehdr->flags = 0;
Tom Herbert5024c332014-11-04 09:06:53 -0800917 guehdr->proto_ctype = *protocol;
918
919 data = &guehdr[1];
920
921 if (need_priv) {
922 __be32 *flags = data;
923
924 guehdr->flags |= GUE_FLAG_PRIV;
925 *flags = 0;
926 data += GUE_LEN_PRIV;
927
Tom Herbertb17f7092014-11-04 09:06:56 -0800928 if (type & SKB_GSO_TUNNEL_REMCSUM) {
929 u16 csum_start = skb_checksum_start_offset(skb);
930 __be16 *pd = data;
931
932 if (csum_start < hdrlen)
933 return -EINVAL;
934
935 csum_start -= hdrlen;
936 pd[0] = htons(csum_start);
937 pd[1] = htons(csum_start + skb->csum_offset);
938
939 if (!skb_is_gso(skb)) {
940 skb->ip_summed = CHECKSUM_NONE;
941 skb->encapsulation = 0;
942 }
943
944 *flags |= GUE_PFLAG_REMCSUM;
945 data += GUE_PLEN_REMCSUM;
946 }
947
Tom Herbert5024c332014-11-04 09:06:53 -0800948 }
Tom Herbert63487ba2014-11-04 09:06:51 -0800949
Tom Herbertdc969b82016-05-18 09:06:15 -0700950 return 0;
951}
952EXPORT_SYMBOL(__gue_build_header);
953
stephen hemminger9dc621a2017-05-19 09:55:54 -0700954#ifdef CONFIG_NET_FOU_IP_TUNNELS
955
956static void fou_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e,
957 struct flowi4 *fl4, u8 *protocol, __be16 sport)
958{
959 struct udphdr *uh;
960
961 skb_push(skb, sizeof(struct udphdr));
962 skb_reset_transport_header(skb);
963
964 uh = udp_hdr(skb);
965
966 uh->dest = e->dport;
967 uh->source = sport;
968 uh->len = htons(skb->len);
969 udp_set_csum(!(e->flags & TUNNEL_ENCAP_FLAG_CSUM), skb,
970 fl4->saddr, fl4->daddr, skb->len);
971
972 *protocol = IPPROTO_UDP;
973}
974
975static int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
976 u8 *protocol, struct flowi4 *fl4)
977{
978 int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
979 SKB_GSO_UDP_TUNNEL;
980 __be16 sport;
981 int err;
982
983 err = __fou_build_header(skb, e, protocol, &sport, type);
984 if (err)
985 return err;
986
987 fou_build_udp(skb, e, fl4, protocol, sport);
988
989 return 0;
990}
991
992static int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
993 u8 *protocol, struct flowi4 *fl4)
Tom Herbertdc969b82016-05-18 09:06:15 -0700994{
995 int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
996 SKB_GSO_UDP_TUNNEL;
997 __be16 sport;
998 int err;
999
1000 err = __gue_build_header(skb, e, protocol, &sport, type);
1001 if (err)
1002 return err;
1003
Tom Herbert63487ba2014-11-04 09:06:51 -08001004 fou_build_udp(skb, e, fl4, protocol, sport);
1005
1006 return 0;
1007}
Tom Herbert63487ba2014-11-04 09:06:51 -08001008
Stefano Briviob8a51b32018-11-08 12:19:23 +01001009static int gue_err_proto_handler(int proto, struct sk_buff *skb, u32 info)
1010{
1011 const struct net_protocol *ipprot = rcu_dereference(inet_protos[proto]);
1012
1013 if (ipprot && ipprot->err_handler) {
1014 if (!ipprot->err_handler(skb, info))
1015 return 0;
1016 }
1017
1018 return -ENOENT;
1019}
1020
1021static int gue_err(struct sk_buff *skb, u32 info)
1022{
1023 int transport_offset = skb_transport_offset(skb);
1024 struct guehdr *guehdr;
Eric Dumazet26fc1812019-01-11 06:27:35 -08001025 size_t len, optlen;
Stefano Briviob8a51b32018-11-08 12:19:23 +01001026 int ret;
1027
Eric Dumazet26fc1812019-01-11 06:27:35 -08001028 len = sizeof(struct udphdr) + sizeof(struct guehdr);
Eric Dumazet5355ed62019-03-06 10:41:00 -08001029 if (!pskb_may_pull(skb, transport_offset + len))
Stefano Briviob8a51b32018-11-08 12:19:23 +01001030 return -EINVAL;
1031
1032 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
1033
1034 switch (guehdr->version) {
1035 case 0: /* Full GUE header present */
1036 break;
1037 case 1: {
1038 /* Direct encasulation of IPv4 or IPv6 */
1039 skb_set_transport_header(skb, -(int)sizeof(struct icmphdr));
1040
1041 switch (((struct iphdr *)guehdr)->version) {
1042 case 4:
1043 ret = gue_err_proto_handler(IPPROTO_IPIP, skb, info);
1044 goto out;
1045#if IS_ENABLED(CONFIG_IPV6)
1046 case 6:
1047 ret = gue_err_proto_handler(IPPROTO_IPV6, skb, info);
1048 goto out;
1049#endif
1050 default:
1051 ret = -EOPNOTSUPP;
1052 goto out;
1053 }
1054 }
1055 default: /* Undefined version */
1056 return -EOPNOTSUPP;
1057 }
1058
1059 if (guehdr->control)
1060 return -ENOENT;
1061
1062 optlen = guehdr->hlen << 2;
1063
Eric Dumazet5355ed62019-03-06 10:41:00 -08001064 if (!pskb_may_pull(skb, transport_offset + len + optlen))
Eric Dumazet26fc1812019-01-11 06:27:35 -08001065 return -EINVAL;
1066
1067 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
Stefano Briviob8a51b32018-11-08 12:19:23 +01001068 if (validate_gue_flags(guehdr, optlen))
1069 return -EINVAL;
1070
Stefano Brivio11789032018-12-18 00:13:17 +01001071 /* Handling exceptions for direct UDP encapsulation in GUE would lead to
1072 * recursion. Besides, this kind of encapsulation can't even be
1073 * configured currently. Discard this.
1074 */
Stefano Briviobc6e0192019-01-03 21:43:34 +01001075 if (guehdr->proto_ctype == IPPROTO_UDP ||
1076 guehdr->proto_ctype == IPPROTO_UDPLITE)
Stefano Brivio11789032018-12-18 00:13:17 +01001077 return -EOPNOTSUPP;
1078
Stefano Briviob8a51b32018-11-08 12:19:23 +01001079 skb_set_transport_header(skb, -(int)sizeof(struct icmphdr));
1080 ret = gue_err_proto_handler(guehdr->proto_ctype, skb, info);
1081
1082out:
1083 skb_set_transport_header(skb, transport_offset);
1084 return ret;
1085}
1086
Tom Herberta8c5f902014-11-12 11:54:09 -08001087
Andi Kleen5eeb2922015-04-08 06:04:31 -07001088static const struct ip_tunnel_encap_ops fou_iptun_ops = {
Tom Herberta8c5f902014-11-12 11:54:09 -08001089 .encap_hlen = fou_encap_hlen,
1090 .build_header = fou_build_header,
Stefano Briviob8a51b32018-11-08 12:19:23 +01001091 .err_handler = gue_err,
Tom Herberta8c5f902014-11-12 11:54:09 -08001092};
1093
Andi Kleen5eeb2922015-04-08 06:04:31 -07001094static const struct ip_tunnel_encap_ops gue_iptun_ops = {
Tom Herberta8c5f902014-11-12 11:54:09 -08001095 .encap_hlen = gue_encap_hlen,
1096 .build_header = gue_build_header,
Stefano Briviob8a51b32018-11-08 12:19:23 +01001097 .err_handler = gue_err,
Tom Herberta8c5f902014-11-12 11:54:09 -08001098};
1099
1100static int ip_tunnel_encap_add_fou_ops(void)
1101{
1102 int ret;
1103
1104 ret = ip_tunnel_encap_add_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
1105 if (ret < 0) {
1106 pr_err("can't add fou ops\n");
1107 return ret;
1108 }
1109
1110 ret = ip_tunnel_encap_add_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
1111 if (ret < 0) {
1112 pr_err("can't add gue ops\n");
1113 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
1114 return ret;
1115 }
1116
1117 return 0;
1118}
1119
1120static void ip_tunnel_encap_del_fou_ops(void)
1121{
1122 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
1123 ip_tunnel_encap_del_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
1124}
1125
1126#else
1127
1128static int ip_tunnel_encap_add_fou_ops(void)
1129{
1130 return 0;
1131}
1132
Thomas Graf882288c2014-11-13 12:48:21 +01001133static void ip_tunnel_encap_del_fou_ops(void)
Tom Herberta8c5f902014-11-12 11:54:09 -08001134{
1135}
1136
1137#endif
1138
WANG Cong02d793c2015-04-10 12:00:29 -07001139static __net_init int fou_init_net(struct net *net)
1140{
1141 struct fou_net *fn = net_generic(net, fou_net_id);
1142
1143 INIT_LIST_HEAD(&fn->fou_list);
1144 mutex_init(&fn->fou_lock);
1145 return 0;
1146}
1147
1148static __net_exit void fou_exit_net(struct net *net)
1149{
1150 struct fou_net *fn = net_generic(net, fou_net_id);
1151 struct fou *fou, *next;
1152
1153 /* Close all the FOU sockets */
1154 mutex_lock(&fn->fou_lock);
1155 list_for_each_entry_safe(fou, next, &fn->fou_list, list)
1156 fou_release(fou);
1157 mutex_unlock(&fn->fou_lock);
1158}
1159
1160static struct pernet_operations fou_net_ops = {
1161 .init = fou_init_net,
1162 .exit = fou_exit_net,
1163 .id = &fou_net_id,
1164 .size = sizeof(struct fou_net),
1165};
1166
Tom Herbert234615512014-09-17 12:25:56 -07001167static int __init fou_init(void)
1168{
1169 int ret;
1170
WANG Cong02d793c2015-04-10 12:00:29 -07001171 ret = register_pernet_device(&fou_net_ops);
1172 if (ret)
Tom Herberta8c5f902014-11-12 11:54:09 -08001173 goto exit;
1174
Johannes Berg489111e2016-10-24 14:40:03 +02001175 ret = genl_register_family(&fou_nl_family);
Tom Herberta8c5f902014-11-12 11:54:09 -08001176 if (ret < 0)
WANG Cong02d793c2015-04-10 12:00:29 -07001177 goto unregister;
Tom Herberta8c5f902014-11-12 11:54:09 -08001178
WANG Cong02d793c2015-04-10 12:00:29 -07001179 ret = ip_tunnel_encap_add_fou_ops();
1180 if (ret == 0)
1181 return 0;
1182
1183 genl_unregister_family(&fou_nl_family);
1184unregister:
1185 unregister_pernet_device(&fou_net_ops);
Tom Herberta8c5f902014-11-12 11:54:09 -08001186exit:
Tom Herbert234615512014-09-17 12:25:56 -07001187 return ret;
1188}
1189
1190static void __exit fou_fini(void)
1191{
Tom Herberta8c5f902014-11-12 11:54:09 -08001192 ip_tunnel_encap_del_fou_ops();
Tom Herbert234615512014-09-17 12:25:56 -07001193 genl_unregister_family(&fou_nl_family);
WANG Cong02d793c2015-04-10 12:00:29 -07001194 unregister_pernet_device(&fou_net_ops);
Tom Herbert234615512014-09-17 12:25:56 -07001195}
1196
1197module_init(fou_init);
1198module_exit(fou_fini);
1199MODULE_AUTHOR("Tom Herbert <therbert@google.com>");
1200MODULE_LICENSE("GPL");