blob: 7fa8d36e56d43d592fff3023b5d4dfbb842a4602 [file] [log] [blame]
Tom Herbert23461552014-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>
6#include <linux/udp.h>
7#include <linux/types.h>
8#include <linux/kernel.h>
9#include <net/genetlink.h>
Tom Herbert37dd0242014-10-03 15:48:09 -070010#include <net/gue.h>
Tom Herbert23461552014-09-17 12:25:56 -070011#include <net/ip.h>
Tom Herbertafe93322014-09-17 12:25:57 -070012#include <net/protocol.h>
Tom Herbert23461552014-09-17 12:25:56 -070013#include <net/udp.h>
14#include <net/udp_tunnel.h>
15#include <net/xfrm.h>
16#include <uapi/linux/fou.h>
17#include <uapi/linux/genetlink.h>
18
19static DEFINE_SPINLOCK(fou_lock);
20static LIST_HEAD(fou_list);
21
22struct fou {
23 struct socket *sock;
24 u8 protocol;
25 u16 port;
Tom Herbertafe93322014-09-17 12:25:57 -070026 struct udp_offload udp_offloads;
Tom Herbert23461552014-09-17 12:25:56 -070027 struct list_head list;
28};
29
30struct fou_cfg {
Tom Herbert37dd0242014-10-03 15:48:09 -070031 u16 type;
Tom Herbert23461552014-09-17 12:25:56 -070032 u8 protocol;
33 struct udp_port_cfg udp_config;
34};
35
36static inline struct fou *fou_from_sock(struct sock *sk)
37{
38 return sk->sk_user_data;
39}
40
Tom Herbert5024c332014-11-04 09:06:53 -080041static void fou_recv_pull(struct sk_buff *skb, size_t len)
Tom Herbert23461552014-09-17 12:25:56 -070042{
43 struct iphdr *iph = ip_hdr(skb);
44
45 /* Remove 'len' bytes from the packet (UDP header and
Tom Herbert5024c332014-11-04 09:06:53 -080046 * FOU header if present).
Tom Herbert23461552014-09-17 12:25:56 -070047 */
48 iph->tot_len = htons(ntohs(iph->tot_len) - len);
49 __skb_pull(skb, len);
50 skb_postpull_rcsum(skb, udp_hdr(skb), len);
51 skb_reset_transport_header(skb);
Tom Herbert23461552014-09-17 12:25:56 -070052}
53
54static int fou_udp_recv(struct sock *sk, struct sk_buff *skb)
55{
56 struct fou *fou = fou_from_sock(sk);
57
58 if (!fou)
59 return 1;
60
Tom Herbert5024c332014-11-04 09:06:53 -080061 fou_recv_pull(skb, sizeof(struct udphdr));
62
63 return -fou->protocol;
64}
65
Tom Herberta8d31c12014-11-04 09:06:57 -080066static struct guehdr *gue_remcsum(struct sk_buff *skb, struct guehdr *guehdr,
Tom Herbert4fd671d2014-11-25 11:21:20 -080067 void *data, size_t hdrlen, u8 ipproto)
Tom Herberta8d31c12014-11-04 09:06:57 -080068{
69 __be16 *pd = data;
Tom Herbert4fd671d2014-11-25 11:21:20 -080070 size_t start = ntohs(pd[0]);
71 size_t offset = ntohs(pd[1]);
72 size_t plen = hdrlen + max_t(size_t, offset + sizeof(u16), start);
Tom Herberta8d31c12014-11-04 09:06:57 -080073
Tom Herberta8d31c12014-11-04 09:06:57 -080074 if (!pskb_may_pull(skb, plen))
75 return NULL;
76 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
77
Tom Herbertdcdc8992015-02-02 16:07:34 -080078 skb_remcsum_process(skb, (void *)guehdr + hdrlen, start, offset);
Tom Herberta8d31c12014-11-04 09:06:57 -080079
80 return guehdr;
81}
82
Tom Herbert5024c332014-11-04 09:06:53 -080083static int gue_control_message(struct sk_buff *skb, struct guehdr *guehdr)
84{
85 /* No support yet */
86 kfree_skb(skb);
87 return 0;
Tom Herbert23461552014-09-17 12:25:56 -070088}
89
Tom Herbert37dd0242014-10-03 15:48:09 -070090static int gue_udp_recv(struct sock *sk, struct sk_buff *skb)
91{
92 struct fou *fou = fou_from_sock(sk);
Tom Herbert5024c332014-11-04 09:06:53 -080093 size_t len, optlen, hdrlen;
Tom Herbert37dd0242014-10-03 15:48:09 -070094 struct guehdr *guehdr;
Tom Herbert5024c332014-11-04 09:06:53 -080095 void *data;
Tom Herberta8d31c12014-11-04 09:06:57 -080096 u16 doffset = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -070097
98 if (!fou)
99 return 1;
100
101 len = sizeof(struct udphdr) + sizeof(struct guehdr);
102 if (!pskb_may_pull(skb, len))
103 goto drop;
104
Tom Herbert5024c332014-11-04 09:06:53 -0800105 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
Tom Herbert37dd0242014-10-03 15:48:09 -0700106
Tom Herbert5024c332014-11-04 09:06:53 -0800107 optlen = guehdr->hlen << 2;
108 len += optlen;
109
Tom Herbert37dd0242014-10-03 15:48:09 -0700110 if (!pskb_may_pull(skb, len))
111 goto drop;
112
Tom Herbert5024c332014-11-04 09:06:53 -0800113 /* guehdr may change after pull */
114 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
Li RongQingd8f00d22014-10-17 16:53:47 +0800115
Tom Herbert5024c332014-11-04 09:06:53 -0800116 hdrlen = sizeof(struct guehdr) + optlen;
117
118 if (guehdr->version != 0 || validate_gue_flags(guehdr, optlen))
Tom Herbert37dd0242014-10-03 15:48:09 -0700119 goto drop;
120
Tom Herberta8d31c12014-11-04 09:06:57 -0800121 hdrlen = sizeof(struct guehdr) + optlen;
122
123 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
124
Tom Herberta8d31c12014-11-04 09:06:57 -0800125 /* Pull csum through the guehdr now . This can be used if
126 * there is a remote checksum offload.
127 */
128 skb_postpull_rcsum(skb, udp_hdr(skb), len);
Tom Herbert5024c332014-11-04 09:06:53 -0800129
130 data = &guehdr[1];
131
132 if (guehdr->flags & GUE_FLAG_PRIV) {
Tom Herberta8d31c12014-11-04 09:06:57 -0800133 __be32 flags = *(__be32 *)(data + doffset);
Tom Herbert5024c332014-11-04 09:06:53 -0800134
Tom Herberta8d31c12014-11-04 09:06:57 -0800135 doffset += GUE_LEN_PRIV;
136
137 if (flags & GUE_PFLAG_REMCSUM) {
138 guehdr = gue_remcsum(skb, guehdr, data + doffset,
139 hdrlen, guehdr->proto_ctype);
140 if (!guehdr)
141 goto drop;
142
143 data = &guehdr[1];
144
145 doffset += GUE_PLEN_REMCSUM;
146 }
Tom Herbert37dd0242014-10-03 15:48:09 -0700147 }
148
Tom Herbert5024c332014-11-04 09:06:53 -0800149 if (unlikely(guehdr->control))
150 return gue_control_message(skb, guehdr);
151
Tom Herbert4fd671d2014-11-25 11:21:20 -0800152 __skb_pull(skb, sizeof(struct udphdr) + hdrlen);
Tom Herberta8d31c12014-11-04 09:06:57 -0800153 skb_reset_transport_header(skb);
154
Tom Herbert5024c332014-11-04 09:06:53 -0800155 return -guehdr->proto_ctype;
156
Tom Herbert37dd0242014-10-03 15:48:09 -0700157drop:
158 kfree_skb(skb);
159 return 0;
160}
161
Tom Herbertafe93322014-09-17 12:25:57 -0700162static struct sk_buff **fou_gro_receive(struct sk_buff **head,
Tom Herberta2b12f32015-01-12 17:00:37 -0800163 struct sk_buff *skb,
164 struct udp_offload *uoff)
Tom Herbertafe93322014-09-17 12:25:57 -0700165{
166 const struct net_offload *ops;
167 struct sk_buff **pp = NULL;
168 u8 proto = NAPI_GRO_CB(skb)->proto;
Tom Herbertefc98d02014-10-03 15:48:08 -0700169 const struct net_offload **offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700170
171 rcu_read_lock();
Tom Herbertefc98d02014-10-03 15:48:08 -0700172 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700173 ops = rcu_dereference(offloads[proto]);
174 if (!ops || !ops->callbacks.gro_receive)
175 goto out_unlock;
176
177 pp = ops->callbacks.gro_receive(head, skb);
178
179out_unlock:
180 rcu_read_unlock();
181
182 return pp;
183}
184
Tom Herberta2b12f32015-01-12 17:00:37 -0800185static int fou_gro_complete(struct sk_buff *skb, int nhoff,
186 struct udp_offload *uoff)
Tom Herbertafe93322014-09-17 12:25:57 -0700187{
188 const struct net_offload *ops;
189 u8 proto = NAPI_GRO_CB(skb)->proto;
190 int err = -ENOSYS;
Tom Herbertefc98d02014-10-03 15:48:08 -0700191 const struct net_offload **offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700192
Jesse Grosscfdf1e12014-11-10 11:45:13 -0800193 udp_tunnel_gro_complete(skb, nhoff);
194
Tom Herbertafe93322014-09-17 12:25:57 -0700195 rcu_read_lock();
Tom Herbertefc98d02014-10-03 15:48:08 -0700196 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700197 ops = rcu_dereference(offloads[proto]);
198 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
199 goto out_unlock;
200
201 err = ops->callbacks.gro_complete(skb, nhoff);
202
203out_unlock:
204 rcu_read_unlock();
205
206 return err;
207}
208
Tom Herberta8d31c12014-11-04 09:06:57 -0800209static struct guehdr *gue_gro_remcsum(struct sk_buff *skb, unsigned int off,
210 struct guehdr *guehdr, void *data,
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800211 size_t hdrlen, u8 ipproto,
212 struct gro_remcsum *grc)
Tom Herberta8d31c12014-11-04 09:06:57 -0800213{
214 __be16 *pd = data;
Tom Herbert4fd671d2014-11-25 11:21:20 -0800215 size_t start = ntohs(pd[0]);
216 size_t offset = ntohs(pd[1]);
217 size_t plen = hdrlen + max_t(size_t, offset + sizeof(u16), start);
Tom Herberta8d31c12014-11-04 09:06:57 -0800218
219 if (skb->remcsum_offload)
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800220 return NULL;
Tom Herberta8d31c12014-11-04 09:06:57 -0800221
Tom Herbert4fd671d2014-11-25 11:21:20 -0800222 if (!NAPI_GRO_CB(skb)->csum_valid)
Tom Herberta8d31c12014-11-04 09:06:57 -0800223 return NULL;
224
Tom Herberta8d31c12014-11-04 09:06:57 -0800225 /* Pull checksum that will be written */
226 if (skb_gro_header_hard(skb, off + plen)) {
227 guehdr = skb_gro_header_slow(skb, off + plen, off);
228 if (!guehdr)
229 return NULL;
230 }
231
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800232 skb_gro_remcsum_process(skb, (void *)guehdr + hdrlen,
233 start, offset, grc);
Tom Herberta8d31c12014-11-04 09:06:57 -0800234
235 skb->remcsum_offload = 1;
236
237 return guehdr;
238}
239
Tom Herbert37dd0242014-10-03 15:48:09 -0700240static struct sk_buff **gue_gro_receive(struct sk_buff **head,
Tom Herberta2b12f32015-01-12 17:00:37 -0800241 struct sk_buff *skb,
242 struct udp_offload *uoff)
Tom Herbert37dd0242014-10-03 15:48:09 -0700243{
244 const struct net_offload **offloads;
245 const struct net_offload *ops;
246 struct sk_buff **pp = NULL;
247 struct sk_buff *p;
Tom Herbert37dd0242014-10-03 15:48:09 -0700248 struct guehdr *guehdr;
Tom Herbert5024c332014-11-04 09:06:53 -0800249 size_t len, optlen, hdrlen, off;
250 void *data;
Tom Herberta8d31c12014-11-04 09:06:57 -0800251 u16 doffset = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700252 int flush = 1;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800253 struct gro_remcsum grc;
254
255 skb_gro_remcsum_init(&grc);
Tom Herbert37dd0242014-10-03 15:48:09 -0700256
257 off = skb_gro_offset(skb);
Tom Herbert5024c332014-11-04 09:06:53 -0800258 len = off + sizeof(*guehdr);
259
Tom Herbert37dd0242014-10-03 15:48:09 -0700260 guehdr = skb_gro_header_fast(skb, off);
Tom Herbert5024c332014-11-04 09:06:53 -0800261 if (skb_gro_header_hard(skb, len)) {
262 guehdr = skb_gro_header_slow(skb, len, off);
Tom Herbert37dd0242014-10-03 15:48:09 -0700263 if (unlikely(!guehdr))
264 goto out;
265 }
266
Tom Herbert5024c332014-11-04 09:06:53 -0800267 optlen = guehdr->hlen << 2;
268 len += optlen;
Tom Herbert37dd0242014-10-03 15:48:09 -0700269
Tom Herbert5024c332014-11-04 09:06:53 -0800270 if (skb_gro_header_hard(skb, len)) {
271 guehdr = skb_gro_header_slow(skb, len, off);
Tom Herbert37dd0242014-10-03 15:48:09 -0700272 if (unlikely(!guehdr))
Tom Herbert5024c332014-11-04 09:06:53 -0800273 goto out;
274 }
275
276 if (unlikely(guehdr->control) || guehdr->version != 0 ||
277 validate_gue_flags(guehdr, optlen))
278 goto out;
279
280 hdrlen = sizeof(*guehdr) + optlen;
281
Tom Herberta8d31c12014-11-04 09:06:57 -0800282 /* Adjust NAPI_GRO_CB(skb)->csum to account for guehdr,
283 * this is needed if there is a remote checkcsum offload.
284 */
Tom Herbert5024c332014-11-04 09:06:53 -0800285 skb_gro_postpull_rcsum(skb, guehdr, hdrlen);
286
287 data = &guehdr[1];
288
289 if (guehdr->flags & GUE_FLAG_PRIV) {
Tom Herberta8d31c12014-11-04 09:06:57 -0800290 __be32 flags = *(__be32 *)(data + doffset);
Tom Herbert5024c332014-11-04 09:06:53 -0800291
Tom Herberta8d31c12014-11-04 09:06:57 -0800292 doffset += GUE_LEN_PRIV;
293
294 if (flags & GUE_PFLAG_REMCSUM) {
295 guehdr = gue_gro_remcsum(skb, off, guehdr,
296 data + doffset, hdrlen,
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800297 guehdr->proto_ctype, &grc);
Tom Herberta8d31c12014-11-04 09:06:57 -0800298 if (!guehdr)
299 goto out;
300
301 data = &guehdr[1];
302
303 doffset += GUE_PLEN_REMCSUM;
304 }
Tom Herbert37dd0242014-10-03 15:48:09 -0700305 }
306
Tom Herberta8d31c12014-11-04 09:06:57 -0800307 skb_gro_pull(skb, hdrlen);
308
Tom Herbert37dd0242014-10-03 15:48:09 -0700309 flush = 0;
310
311 for (p = *head; p; p = p->next) {
312 const struct guehdr *guehdr2;
313
314 if (!NAPI_GRO_CB(p)->same_flow)
315 continue;
316
317 guehdr2 = (struct guehdr *)(p->data + off);
318
319 /* Compare base GUE header to be equal (covers
Tom Herbert5024c332014-11-04 09:06:53 -0800320 * hlen, version, proto_ctype, and flags.
Tom Herbert37dd0242014-10-03 15:48:09 -0700321 */
322 if (guehdr->word != guehdr2->word) {
323 NAPI_GRO_CB(p)->same_flow = 0;
324 continue;
325 }
326
327 /* Compare optional fields are the same. */
328 if (guehdr->hlen && memcmp(&guehdr[1], &guehdr2[1],
329 guehdr->hlen << 2)) {
330 NAPI_GRO_CB(p)->same_flow = 0;
331 continue;
332 }
333 }
334
Tom Herbert5024c332014-11-04 09:06:53 -0800335 rcu_read_lock();
336 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
337 ops = rcu_dereference(offloads[guehdr->proto_ctype]);
338 if (WARN_ON(!ops || !ops->callbacks.gro_receive))
339 goto out_unlock;
Tom Herbert37dd0242014-10-03 15:48:09 -0700340
341 pp = ops->callbacks.gro_receive(head, skb);
342
343out_unlock:
344 rcu_read_unlock();
345out:
346 NAPI_GRO_CB(skb)->flush |= flush;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800347 skb_gro_remcsum_cleanup(skb, &grc);
Tom Herbert37dd0242014-10-03 15:48:09 -0700348
349 return pp;
350}
351
Tom Herberta2b12f32015-01-12 17:00:37 -0800352static int gue_gro_complete(struct sk_buff *skb, int nhoff,
353 struct udp_offload *uoff)
Tom Herbert37dd0242014-10-03 15:48:09 -0700354{
355 const struct net_offload **offloads;
356 struct guehdr *guehdr = (struct guehdr *)(skb->data + nhoff);
357 const struct net_offload *ops;
358 unsigned int guehlen;
359 u8 proto;
360 int err = -ENOENT;
361
Tom Herbert5024c332014-11-04 09:06:53 -0800362 proto = guehdr->proto_ctype;
Tom Herbert37dd0242014-10-03 15:48:09 -0700363
364 guehlen = sizeof(*guehdr) + (guehdr->hlen << 2);
365
366 rcu_read_lock();
367 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
368 ops = rcu_dereference(offloads[proto]);
369 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
370 goto out_unlock;
371
372 err = ops->callbacks.gro_complete(skb, nhoff + guehlen);
373
374out_unlock:
375 rcu_read_unlock();
376 return err;
377}
378
Tom Herbert23461552014-09-17 12:25:56 -0700379static int fou_add_to_port_list(struct fou *fou)
380{
381 struct fou *fout;
382
383 spin_lock(&fou_lock);
384 list_for_each_entry(fout, &fou_list, list) {
385 if (fou->port == fout->port) {
386 spin_unlock(&fou_lock);
387 return -EALREADY;
388 }
389 }
390
391 list_add(&fou->list, &fou_list);
392 spin_unlock(&fou_lock);
393
394 return 0;
395}
396
397static void fou_release(struct fou *fou)
398{
399 struct socket *sock = fou->sock;
400 struct sock *sk = sock->sk;
401
402 udp_del_offload(&fou->udp_offloads);
403
404 list_del(&fou->list);
405
406 /* Remove hooks into tunnel socket */
407 sk->sk_user_data = NULL;
408
409 sock_release(sock);
410
411 kfree(fou);
412}
413
Tom Herbert37dd0242014-10-03 15:48:09 -0700414static int fou_encap_init(struct sock *sk, struct fou *fou, struct fou_cfg *cfg)
415{
416 udp_sk(sk)->encap_rcv = fou_udp_recv;
417 fou->protocol = cfg->protocol;
418 fou->udp_offloads.callbacks.gro_receive = fou_gro_receive;
419 fou->udp_offloads.callbacks.gro_complete = fou_gro_complete;
420 fou->udp_offloads.port = cfg->udp_config.local_udp_port;
421 fou->udp_offloads.ipproto = cfg->protocol;
422
423 return 0;
424}
425
426static int gue_encap_init(struct sock *sk, struct fou *fou, struct fou_cfg *cfg)
427{
428 udp_sk(sk)->encap_rcv = gue_udp_recv;
429 fou->udp_offloads.callbacks.gro_receive = gue_gro_receive;
430 fou->udp_offloads.callbacks.gro_complete = gue_gro_complete;
431 fou->udp_offloads.port = cfg->udp_config.local_udp_port;
432
433 return 0;
434}
435
Tom Herbert23461552014-09-17 12:25:56 -0700436static int fou_create(struct net *net, struct fou_cfg *cfg,
437 struct socket **sockp)
438{
439 struct fou *fou = NULL;
440 int err;
441 struct socket *sock = NULL;
442 struct sock *sk;
443
444 /* Open UDP socket */
445 err = udp_sock_create(net, &cfg->udp_config, &sock);
446 if (err < 0)
447 goto error;
448
449 /* Allocate FOU port structure */
450 fou = kzalloc(sizeof(*fou), GFP_KERNEL);
451 if (!fou) {
452 err = -ENOMEM;
453 goto error;
454 }
455
456 sk = sock->sk;
457
Tom Herbert37dd0242014-10-03 15:48:09 -0700458 fou->port = cfg->udp_config.local_udp_port;
459
460 /* Initial for fou type */
461 switch (cfg->type) {
462 case FOU_ENCAP_DIRECT:
463 err = fou_encap_init(sk, fou, cfg);
464 if (err)
465 goto error;
466 break;
467 case FOU_ENCAP_GUE:
468 err = gue_encap_init(sk, fou, cfg);
469 if (err)
470 goto error;
471 break;
472 default:
473 err = -EINVAL;
474 goto error;
475 }
Tom Herbert23461552014-09-17 12:25:56 -0700476
477 udp_sk(sk)->encap_type = 1;
478 udp_encap_enable();
479
480 sk->sk_user_data = fou;
481 fou->sock = sock;
482
Tom Herbert224d0192015-01-05 13:56:14 -0800483 inet_inc_convert_csum(sk);
Tom Herbert23461552014-09-17 12:25:56 -0700484
485 sk->sk_allocation = GFP_ATOMIC;
486
Tom Herbertafe93322014-09-17 12:25:57 -0700487 if (cfg->udp_config.family == AF_INET) {
488 err = udp_add_offload(&fou->udp_offloads);
489 if (err)
490 goto error;
491 }
492
Tom Herbert23461552014-09-17 12:25:56 -0700493 err = fou_add_to_port_list(fou);
494 if (err)
495 goto error;
496
497 if (sockp)
498 *sockp = sock;
499
500 return 0;
501
502error:
503 kfree(fou);
504 if (sock)
505 sock_release(sock);
506
507 return err;
508}
509
510static int fou_destroy(struct net *net, struct fou_cfg *cfg)
511{
512 struct fou *fou;
513 u16 port = cfg->udp_config.local_udp_port;
514 int err = -EINVAL;
515
516 spin_lock(&fou_lock);
517 list_for_each_entry(fou, &fou_list, list) {
518 if (fou->port == port) {
Tom Herbertafe93322014-09-17 12:25:57 -0700519 udp_del_offload(&fou->udp_offloads);
Tom Herbert23461552014-09-17 12:25:56 -0700520 fou_release(fou);
521 err = 0;
522 break;
523 }
524 }
525 spin_unlock(&fou_lock);
526
527 return err;
528}
529
530static struct genl_family fou_nl_family = {
531 .id = GENL_ID_GENERATE,
532 .hdrsize = 0,
533 .name = FOU_GENL_NAME,
534 .version = FOU_GENL_VERSION,
535 .maxattr = FOU_ATTR_MAX,
536 .netnsok = true,
537};
538
539static struct nla_policy fou_nl_policy[FOU_ATTR_MAX + 1] = {
540 [FOU_ATTR_PORT] = { .type = NLA_U16, },
541 [FOU_ATTR_AF] = { .type = NLA_U8, },
542 [FOU_ATTR_IPPROTO] = { .type = NLA_U8, },
Tom Herbert37dd0242014-10-03 15:48:09 -0700543 [FOU_ATTR_TYPE] = { .type = NLA_U8, },
Tom Herbert23461552014-09-17 12:25:56 -0700544};
545
546static int parse_nl_config(struct genl_info *info,
547 struct fou_cfg *cfg)
548{
549 memset(cfg, 0, sizeof(*cfg));
550
551 cfg->udp_config.family = AF_INET;
552
553 if (info->attrs[FOU_ATTR_AF]) {
554 u8 family = nla_get_u8(info->attrs[FOU_ATTR_AF]);
555
556 if (family != AF_INET && family != AF_INET6)
557 return -EINVAL;
558
559 cfg->udp_config.family = family;
560 }
561
562 if (info->attrs[FOU_ATTR_PORT]) {
563 u16 port = nla_get_u16(info->attrs[FOU_ATTR_PORT]);
564
565 cfg->udp_config.local_udp_port = port;
566 }
567
568 if (info->attrs[FOU_ATTR_IPPROTO])
569 cfg->protocol = nla_get_u8(info->attrs[FOU_ATTR_IPPROTO]);
570
Tom Herbert37dd0242014-10-03 15:48:09 -0700571 if (info->attrs[FOU_ATTR_TYPE])
572 cfg->type = nla_get_u8(info->attrs[FOU_ATTR_TYPE]);
573
Tom Herbert23461552014-09-17 12:25:56 -0700574 return 0;
575}
576
577static int fou_nl_cmd_add_port(struct sk_buff *skb, struct genl_info *info)
578{
579 struct fou_cfg cfg;
580 int err;
581
582 err = parse_nl_config(info, &cfg);
583 if (err)
584 return err;
585
586 return fou_create(&init_net, &cfg, NULL);
587}
588
589static int fou_nl_cmd_rm_port(struct sk_buff *skb, struct genl_info *info)
590{
591 struct fou_cfg cfg;
592
593 parse_nl_config(info, &cfg);
594
595 return fou_destroy(&init_net, &cfg);
596}
597
598static const struct genl_ops fou_nl_ops[] = {
599 {
600 .cmd = FOU_CMD_ADD,
601 .doit = fou_nl_cmd_add_port,
602 .policy = fou_nl_policy,
603 .flags = GENL_ADMIN_PERM,
604 },
605 {
606 .cmd = FOU_CMD_DEL,
607 .doit = fou_nl_cmd_rm_port,
608 .policy = fou_nl_policy,
609 .flags = GENL_ADMIN_PERM,
610 },
611};
612
Tom Herberta8c5f902014-11-12 11:54:09 -0800613size_t fou_encap_hlen(struct ip_tunnel_encap *e)
614{
615 return sizeof(struct udphdr);
616}
617EXPORT_SYMBOL(fou_encap_hlen);
618
619size_t gue_encap_hlen(struct ip_tunnel_encap *e)
620{
621 size_t len;
622 bool need_priv = false;
623
624 len = sizeof(struct udphdr) + sizeof(struct guehdr);
625
626 if (e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) {
627 len += GUE_PLEN_REMCSUM;
628 need_priv = true;
629 }
630
631 len += need_priv ? GUE_LEN_PRIV : 0;
632
633 return len;
634}
635EXPORT_SYMBOL(gue_encap_hlen);
636
Tom Herbert63487ba2014-11-04 09:06:51 -0800637static void fou_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e,
638 struct flowi4 *fl4, u8 *protocol, __be16 sport)
639{
640 struct udphdr *uh;
641
642 skb_push(skb, sizeof(struct udphdr));
643 skb_reset_transport_header(skb);
644
645 uh = udp_hdr(skb);
646
647 uh->dest = e->dport;
648 uh->source = sport;
649 uh->len = htons(skb->len);
650 uh->check = 0;
651 udp_set_csum(!(e->flags & TUNNEL_ENCAP_FLAG_CSUM), skb,
652 fl4->saddr, fl4->daddr, skb->len);
653
654 *protocol = IPPROTO_UDP;
655}
656
657int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
658 u8 *protocol, struct flowi4 *fl4)
659{
660 bool csum = !!(e->flags & TUNNEL_ENCAP_FLAG_CSUM);
661 int type = csum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
662 __be16 sport;
663
664 skb = iptunnel_handle_offloads(skb, csum, type);
665
666 if (IS_ERR(skb))
667 return PTR_ERR(skb);
668
669 sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
670 skb, 0, 0, false);
671 fou_build_udp(skb, e, fl4, protocol, sport);
672
673 return 0;
674}
675EXPORT_SYMBOL(fou_build_header);
676
677int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
678 u8 *protocol, struct flowi4 *fl4)
679{
680 bool csum = !!(e->flags & TUNNEL_ENCAP_FLAG_CSUM);
681 int type = csum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
682 struct guehdr *guehdr;
Tom Herbertb17f7092014-11-04 09:06:56 -0800683 size_t hdrlen, optlen = 0;
Tom Herbert63487ba2014-11-04 09:06:51 -0800684 __be16 sport;
Tom Herbert5024c332014-11-04 09:06:53 -0800685 void *data;
686 bool need_priv = false;
687
Tom Herbertb17f7092014-11-04 09:06:56 -0800688 if ((e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) &&
689 skb->ip_summed == CHECKSUM_PARTIAL) {
690 csum = false;
691 optlen += GUE_PLEN_REMCSUM;
692 type |= SKB_GSO_TUNNEL_REMCSUM;
693 need_priv = true;
694 }
695
Tom Herbert5024c332014-11-04 09:06:53 -0800696 optlen += need_priv ? GUE_LEN_PRIV : 0;
Tom Herbert63487ba2014-11-04 09:06:51 -0800697
698 skb = iptunnel_handle_offloads(skb, csum, type);
699
700 if (IS_ERR(skb))
701 return PTR_ERR(skb);
702
703 /* Get source port (based on flow hash) before skb_push */
704 sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
705 skb, 0, 0, false);
706
Tom Herbertb17f7092014-11-04 09:06:56 -0800707 hdrlen = sizeof(struct guehdr) + optlen;
708
709 skb_push(skb, hdrlen);
Tom Herbert63487ba2014-11-04 09:06:51 -0800710
711 guehdr = (struct guehdr *)skb->data;
712
Tom Herbert5024c332014-11-04 09:06:53 -0800713 guehdr->control = 0;
Tom Herbert63487ba2014-11-04 09:06:51 -0800714 guehdr->version = 0;
Tom Herbert5024c332014-11-04 09:06:53 -0800715 guehdr->hlen = optlen >> 2;
Tom Herbert63487ba2014-11-04 09:06:51 -0800716 guehdr->flags = 0;
Tom Herbert5024c332014-11-04 09:06:53 -0800717 guehdr->proto_ctype = *protocol;
718
719 data = &guehdr[1];
720
721 if (need_priv) {
722 __be32 *flags = data;
723
724 guehdr->flags |= GUE_FLAG_PRIV;
725 *flags = 0;
726 data += GUE_LEN_PRIV;
727
Tom Herbertb17f7092014-11-04 09:06:56 -0800728 if (type & SKB_GSO_TUNNEL_REMCSUM) {
729 u16 csum_start = skb_checksum_start_offset(skb);
730 __be16 *pd = data;
731
732 if (csum_start < hdrlen)
733 return -EINVAL;
734
735 csum_start -= hdrlen;
736 pd[0] = htons(csum_start);
737 pd[1] = htons(csum_start + skb->csum_offset);
738
739 if (!skb_is_gso(skb)) {
740 skb->ip_summed = CHECKSUM_NONE;
741 skb->encapsulation = 0;
742 }
743
744 *flags |= GUE_PFLAG_REMCSUM;
745 data += GUE_PLEN_REMCSUM;
746 }
747
Tom Herbert5024c332014-11-04 09:06:53 -0800748 }
Tom Herbert63487ba2014-11-04 09:06:51 -0800749
750 fou_build_udp(skb, e, fl4, protocol, sport);
751
752 return 0;
753}
754EXPORT_SYMBOL(gue_build_header);
755
Tom Herberta8c5f902014-11-12 11:54:09 -0800756#ifdef CONFIG_NET_FOU_IP_TUNNELS
757
758static const struct ip_tunnel_encap_ops __read_mostly fou_iptun_ops = {
759 .encap_hlen = fou_encap_hlen,
760 .build_header = fou_build_header,
761};
762
763static const struct ip_tunnel_encap_ops __read_mostly gue_iptun_ops = {
764 .encap_hlen = gue_encap_hlen,
765 .build_header = gue_build_header,
766};
767
768static int ip_tunnel_encap_add_fou_ops(void)
769{
770 int ret;
771
772 ret = ip_tunnel_encap_add_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
773 if (ret < 0) {
774 pr_err("can't add fou ops\n");
775 return ret;
776 }
777
778 ret = ip_tunnel_encap_add_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
779 if (ret < 0) {
780 pr_err("can't add gue ops\n");
781 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
782 return ret;
783 }
784
785 return 0;
786}
787
788static void ip_tunnel_encap_del_fou_ops(void)
789{
790 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
791 ip_tunnel_encap_del_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
792}
793
794#else
795
796static int ip_tunnel_encap_add_fou_ops(void)
797{
798 return 0;
799}
800
Thomas Graf882288c2014-11-13 12:48:21 +0100801static void ip_tunnel_encap_del_fou_ops(void)
Tom Herberta8c5f902014-11-12 11:54:09 -0800802{
803}
804
805#endif
806
Tom Herbert23461552014-09-17 12:25:56 -0700807static int __init fou_init(void)
808{
809 int ret;
810
811 ret = genl_register_family_with_ops(&fou_nl_family,
812 fou_nl_ops);
813
Tom Herberta8c5f902014-11-12 11:54:09 -0800814 if (ret < 0)
815 goto exit;
816
817 ret = ip_tunnel_encap_add_fou_ops();
818 if (ret < 0)
819 genl_unregister_family(&fou_nl_family);
820
821exit:
Tom Herbert23461552014-09-17 12:25:56 -0700822 return ret;
823}
824
825static void __exit fou_fini(void)
826{
827 struct fou *fou, *next;
828
Tom Herberta8c5f902014-11-12 11:54:09 -0800829 ip_tunnel_encap_del_fou_ops();
830
Tom Herbert23461552014-09-17 12:25:56 -0700831 genl_unregister_family(&fou_nl_family);
832
833 /* Close all the FOU sockets */
834
835 spin_lock(&fou_lock);
836 list_for_each_entry_safe(fou, next, &fou_list, list)
837 fou_release(fou);
838 spin_unlock(&fou_lock);
839}
840
841module_init(fou_init);
842module_exit(fou_fini);
843MODULE_AUTHOR("Tom Herbert <therbert@google.com>");
844MODULE_LICENSE("GPL");