Merge tag 'clk-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/clk/linux
[deliverable/linux.git] / net / ipv4 / fou.c
1 #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>
10 #include <net/gue.h>
11 #include <net/ip.h>
12 #include <net/protocol.h>
13 #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
19 struct fou {
20 struct socket *sock;
21 u8 protocol;
22 u8 flags;
23 __be16 port;
24 u8 family;
25 u16 type;
26 struct list_head list;
27 struct rcu_head rcu;
28 };
29
30 #define FOU_F_REMCSUM_NOPARTIAL BIT(0)
31
32 struct fou_cfg {
33 u16 type;
34 u8 protocol;
35 u8 flags;
36 struct udp_port_cfg udp_config;
37 };
38
39 static unsigned int fou_net_id;
40
41 struct fou_net {
42 struct list_head fou_list;
43 struct mutex fou_lock;
44 };
45
46 static inline struct fou *fou_from_sock(struct sock *sk)
47 {
48 return sk->sk_user_data;
49 }
50
51 static int fou_recv_pull(struct sk_buff *skb, struct fou *fou, size_t len)
52 {
53 /* Remove 'len' bytes from the packet (UDP header and
54 * FOU header if present).
55 */
56 if (fou->family == AF_INET)
57 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
58 else
59 ipv6_hdr(skb)->payload_len =
60 htons(ntohs(ipv6_hdr(skb)->payload_len) - len);
61
62 __skb_pull(skb, len);
63 skb_postpull_rcsum(skb, udp_hdr(skb), len);
64 skb_reset_transport_header(skb);
65 return iptunnel_pull_offloads(skb);
66 }
67
68 static int fou_udp_recv(struct sock *sk, struct sk_buff *skb)
69 {
70 struct fou *fou = fou_from_sock(sk);
71
72 if (!fou)
73 return 1;
74
75 if (fou_recv_pull(skb, fou, sizeof(struct udphdr)))
76 goto drop;
77
78 return -fou->protocol;
79
80 drop:
81 kfree_skb(skb);
82 return 0;
83 }
84
85 static struct guehdr *gue_remcsum(struct sk_buff *skb, struct guehdr *guehdr,
86 void *data, size_t hdrlen, u8 ipproto,
87 bool nopartial)
88 {
89 __be16 *pd = data;
90 size_t start = ntohs(pd[0]);
91 size_t offset = ntohs(pd[1]);
92 size_t plen = sizeof(struct udphdr) + hdrlen +
93 max_t(size_t, offset + sizeof(u16), start);
94
95 if (skb->remcsum_offload)
96 return guehdr;
97
98 if (!pskb_may_pull(skb, plen))
99 return NULL;
100 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
101
102 skb_remcsum_process(skb, (void *)guehdr + hdrlen,
103 start, offset, nopartial);
104
105 return guehdr;
106 }
107
108 static int gue_control_message(struct sk_buff *skb, struct guehdr *guehdr)
109 {
110 /* No support yet */
111 kfree_skb(skb);
112 return 0;
113 }
114
115 static int gue_udp_recv(struct sock *sk, struct sk_buff *skb)
116 {
117 struct fou *fou = fou_from_sock(sk);
118 size_t len, optlen, hdrlen;
119 struct guehdr *guehdr;
120 void *data;
121 u16 doffset = 0;
122
123 if (!fou)
124 return 1;
125
126 len = sizeof(struct udphdr) + sizeof(struct guehdr);
127 if (!pskb_may_pull(skb, len))
128 goto drop;
129
130 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
131
132 optlen = guehdr->hlen << 2;
133 len += optlen;
134
135 if (!pskb_may_pull(skb, len))
136 goto drop;
137
138 /* guehdr may change after pull */
139 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
140
141 hdrlen = sizeof(struct guehdr) + optlen;
142
143 if (guehdr->version != 0 || validate_gue_flags(guehdr, optlen))
144 goto drop;
145
146 hdrlen = sizeof(struct guehdr) + optlen;
147
148 if (fou->family == AF_INET)
149 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
150 else
151 ipv6_hdr(skb)->payload_len =
152 htons(ntohs(ipv6_hdr(skb)->payload_len) - len);
153
154 /* Pull csum through the guehdr now . This can be used if
155 * there is a remote checksum offload.
156 */
157 skb_postpull_rcsum(skb, udp_hdr(skb), len);
158
159 data = &guehdr[1];
160
161 if (guehdr->flags & GUE_FLAG_PRIV) {
162 __be32 flags = *(__be32 *)(data + doffset);
163
164 doffset += GUE_LEN_PRIV;
165
166 if (flags & GUE_PFLAG_REMCSUM) {
167 guehdr = gue_remcsum(skb, guehdr, data + doffset,
168 hdrlen, guehdr->proto_ctype,
169 !!(fou->flags &
170 FOU_F_REMCSUM_NOPARTIAL));
171 if (!guehdr)
172 goto drop;
173
174 data = &guehdr[1];
175
176 doffset += GUE_PLEN_REMCSUM;
177 }
178 }
179
180 if (unlikely(guehdr->control))
181 return gue_control_message(skb, guehdr);
182
183 __skb_pull(skb, sizeof(struct udphdr) + hdrlen);
184 skb_reset_transport_header(skb);
185
186 if (iptunnel_pull_offloads(skb))
187 goto drop;
188
189 return -guehdr->proto_ctype;
190
191 drop:
192 kfree_skb(skb);
193 return 0;
194 }
195
196 static struct sk_buff **fou_gro_receive(struct sock *sk,
197 struct sk_buff **head,
198 struct sk_buff *skb)
199 {
200 const struct net_offload *ops;
201 struct sk_buff **pp = NULL;
202 u8 proto = fou_from_sock(sk)->protocol;
203 const struct net_offload **offloads;
204
205 /* We can clear the encap_mark for FOU as we are essentially doing
206 * one of two possible things. We are either adding an L4 tunnel
207 * header to the outer L3 tunnel header, or we are are simply
208 * treating the GRE tunnel header as though it is a UDP protocol
209 * specific header such as VXLAN or GENEVE.
210 */
211 NAPI_GRO_CB(skb)->encap_mark = 0;
212
213 /* Flag this frame as already having an outer encap header */
214 NAPI_GRO_CB(skb)->is_fou = 1;
215
216 rcu_read_lock();
217 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
218 ops = rcu_dereference(offloads[proto]);
219 if (!ops || !ops->callbacks.gro_receive)
220 goto out_unlock;
221
222 pp = ops->callbacks.gro_receive(head, skb);
223
224 out_unlock:
225 rcu_read_unlock();
226
227 return pp;
228 }
229
230 static int fou_gro_complete(struct sock *sk, struct sk_buff *skb,
231 int nhoff)
232 {
233 const struct net_offload *ops;
234 u8 proto = fou_from_sock(sk)->protocol;
235 int err = -ENOSYS;
236 const struct net_offload **offloads;
237
238 rcu_read_lock();
239 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
240 ops = rcu_dereference(offloads[proto]);
241 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
242 goto out_unlock;
243
244 err = ops->callbacks.gro_complete(skb, nhoff);
245
246 skb_set_inner_mac_header(skb, nhoff);
247
248 out_unlock:
249 rcu_read_unlock();
250
251 return err;
252 }
253
254 static struct guehdr *gue_gro_remcsum(struct sk_buff *skb, unsigned int off,
255 struct guehdr *guehdr, void *data,
256 size_t hdrlen, struct gro_remcsum *grc,
257 bool nopartial)
258 {
259 __be16 *pd = data;
260 size_t start = ntohs(pd[0]);
261 size_t offset = ntohs(pd[1]);
262
263 if (skb->remcsum_offload)
264 return guehdr;
265
266 if (!NAPI_GRO_CB(skb)->csum_valid)
267 return NULL;
268
269 guehdr = skb_gro_remcsum_process(skb, (void *)guehdr, off, hdrlen,
270 start, offset, grc, nopartial);
271
272 skb->remcsum_offload = 1;
273
274 return guehdr;
275 }
276
277 static struct sk_buff **gue_gro_receive(struct sock *sk,
278 struct sk_buff **head,
279 struct sk_buff *skb)
280 {
281 const struct net_offload **offloads;
282 const struct net_offload *ops;
283 struct sk_buff **pp = NULL;
284 struct sk_buff *p;
285 struct guehdr *guehdr;
286 size_t len, optlen, hdrlen, off;
287 void *data;
288 u16 doffset = 0;
289 int flush = 1;
290 struct fou *fou = fou_from_sock(sk);
291 struct gro_remcsum grc;
292
293 skb_gro_remcsum_init(&grc);
294
295 off = skb_gro_offset(skb);
296 len = off + sizeof(*guehdr);
297
298 guehdr = skb_gro_header_fast(skb, off);
299 if (skb_gro_header_hard(skb, len)) {
300 guehdr = skb_gro_header_slow(skb, len, off);
301 if (unlikely(!guehdr))
302 goto out;
303 }
304
305 optlen = guehdr->hlen << 2;
306 len += optlen;
307
308 if (skb_gro_header_hard(skb, len)) {
309 guehdr = skb_gro_header_slow(skb, len, off);
310 if (unlikely(!guehdr))
311 goto out;
312 }
313
314 if (unlikely(guehdr->control) || guehdr->version != 0 ||
315 validate_gue_flags(guehdr, optlen))
316 goto out;
317
318 hdrlen = sizeof(*guehdr) + optlen;
319
320 /* Adjust NAPI_GRO_CB(skb)->csum to account for guehdr,
321 * this is needed if there is a remote checkcsum offload.
322 */
323 skb_gro_postpull_rcsum(skb, guehdr, hdrlen);
324
325 data = &guehdr[1];
326
327 if (guehdr->flags & GUE_FLAG_PRIV) {
328 __be32 flags = *(__be32 *)(data + doffset);
329
330 doffset += GUE_LEN_PRIV;
331
332 if (flags & GUE_PFLAG_REMCSUM) {
333 guehdr = gue_gro_remcsum(skb, off, guehdr,
334 data + doffset, hdrlen, &grc,
335 !!(fou->flags &
336 FOU_F_REMCSUM_NOPARTIAL));
337
338 if (!guehdr)
339 goto out;
340
341 data = &guehdr[1];
342
343 doffset += GUE_PLEN_REMCSUM;
344 }
345 }
346
347 skb_gro_pull(skb, hdrlen);
348
349 for (p = *head; p; p = p->next) {
350 const struct guehdr *guehdr2;
351
352 if (!NAPI_GRO_CB(p)->same_flow)
353 continue;
354
355 guehdr2 = (struct guehdr *)(p->data + off);
356
357 /* Compare base GUE header to be equal (covers
358 * hlen, version, proto_ctype, and flags.
359 */
360 if (guehdr->word != guehdr2->word) {
361 NAPI_GRO_CB(p)->same_flow = 0;
362 continue;
363 }
364
365 /* Compare optional fields are the same. */
366 if (guehdr->hlen && memcmp(&guehdr[1], &guehdr2[1],
367 guehdr->hlen << 2)) {
368 NAPI_GRO_CB(p)->same_flow = 0;
369 continue;
370 }
371 }
372
373 /* We can clear the encap_mark for GUE as we are essentially doing
374 * one of two possible things. We are either adding an L4 tunnel
375 * header to the outer L3 tunnel header, or we are are simply
376 * treating the GRE tunnel header as though it is a UDP protocol
377 * specific header such as VXLAN or GENEVE.
378 */
379 NAPI_GRO_CB(skb)->encap_mark = 0;
380
381 /* Flag this frame as already having an outer encap header */
382 NAPI_GRO_CB(skb)->is_fou = 1;
383
384 rcu_read_lock();
385 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
386 ops = rcu_dereference(offloads[guehdr->proto_ctype]);
387 if (WARN_ON_ONCE(!ops || !ops->callbacks.gro_receive))
388 goto out_unlock;
389
390 pp = ops->callbacks.gro_receive(head, skb);
391 flush = 0;
392
393 out_unlock:
394 rcu_read_unlock();
395 out:
396 NAPI_GRO_CB(skb)->flush |= flush;
397 skb_gro_remcsum_cleanup(skb, &grc);
398
399 return pp;
400 }
401
402 static int gue_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
403 {
404 const struct net_offload **offloads;
405 struct guehdr *guehdr = (struct guehdr *)(skb->data + nhoff);
406 const struct net_offload *ops;
407 unsigned int guehlen;
408 u8 proto;
409 int err = -ENOENT;
410
411 proto = guehdr->proto_ctype;
412
413 guehlen = sizeof(*guehdr) + (guehdr->hlen << 2);
414
415 rcu_read_lock();
416 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
417 ops = rcu_dereference(offloads[proto]);
418 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
419 goto out_unlock;
420
421 err = ops->callbacks.gro_complete(skb, nhoff + guehlen);
422
423 skb_set_inner_mac_header(skb, nhoff + guehlen);
424
425 out_unlock:
426 rcu_read_unlock();
427 return err;
428 }
429
430 static int fou_add_to_port_list(struct net *net, struct fou *fou)
431 {
432 struct fou_net *fn = net_generic(net, fou_net_id);
433 struct fou *fout;
434
435 mutex_lock(&fn->fou_lock);
436 list_for_each_entry(fout, &fn->fou_list, list) {
437 if (fou->port == fout->port &&
438 fou->family == fout->family) {
439 mutex_unlock(&fn->fou_lock);
440 return -EALREADY;
441 }
442 }
443
444 list_add(&fou->list, &fn->fou_list);
445 mutex_unlock(&fn->fou_lock);
446
447 return 0;
448 }
449
450 static void fou_release(struct fou *fou)
451 {
452 struct socket *sock = fou->sock;
453
454 list_del(&fou->list);
455 udp_tunnel_sock_release(sock);
456
457 kfree_rcu(fou, rcu);
458 }
459
460 static int fou_create(struct net *net, struct fou_cfg *cfg,
461 struct socket **sockp)
462 {
463 struct socket *sock = NULL;
464 struct fou *fou = NULL;
465 struct sock *sk;
466 struct udp_tunnel_sock_cfg tunnel_cfg;
467 int err;
468
469 /* Open UDP socket */
470 err = udp_sock_create(net, &cfg->udp_config, &sock);
471 if (err < 0)
472 goto error;
473
474 /* Allocate FOU port structure */
475 fou = kzalloc(sizeof(*fou), GFP_KERNEL);
476 if (!fou) {
477 err = -ENOMEM;
478 goto error;
479 }
480
481 sk = sock->sk;
482
483 fou->port = cfg->udp_config.local_udp_port;
484 fou->family = cfg->udp_config.family;
485 fou->flags = cfg->flags;
486 fou->type = cfg->type;
487 fou->sock = sock;
488
489 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
490 tunnel_cfg.encap_type = 1;
491 tunnel_cfg.sk_user_data = fou;
492 tunnel_cfg.encap_destroy = NULL;
493
494 /* Initial for fou type */
495 switch (cfg->type) {
496 case FOU_ENCAP_DIRECT:
497 tunnel_cfg.encap_rcv = fou_udp_recv;
498 tunnel_cfg.gro_receive = fou_gro_receive;
499 tunnel_cfg.gro_complete = fou_gro_complete;
500 fou->protocol = cfg->protocol;
501 break;
502 case FOU_ENCAP_GUE:
503 tunnel_cfg.encap_rcv = gue_udp_recv;
504 tunnel_cfg.gro_receive = gue_gro_receive;
505 tunnel_cfg.gro_complete = gue_gro_complete;
506 break;
507 default:
508 err = -EINVAL;
509 goto error;
510 }
511
512 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
513
514 sk->sk_allocation = GFP_ATOMIC;
515
516 err = fou_add_to_port_list(net, fou);
517 if (err)
518 goto error;
519
520 if (sockp)
521 *sockp = sock;
522
523 return 0;
524
525 error:
526 kfree(fou);
527 if (sock)
528 udp_tunnel_sock_release(sock);
529
530 return err;
531 }
532
533 static int fou_destroy(struct net *net, struct fou_cfg *cfg)
534 {
535 struct fou_net *fn = net_generic(net, fou_net_id);
536 __be16 port = cfg->udp_config.local_udp_port;
537 u8 family = cfg->udp_config.family;
538 int err = -EINVAL;
539 struct fou *fou;
540
541 mutex_lock(&fn->fou_lock);
542 list_for_each_entry(fou, &fn->fou_list, list) {
543 if (fou->port == port && fou->family == family) {
544 fou_release(fou);
545 err = 0;
546 break;
547 }
548 }
549 mutex_unlock(&fn->fou_lock);
550
551 return err;
552 }
553
554 static struct genl_family fou_nl_family = {
555 .id = GENL_ID_GENERATE,
556 .hdrsize = 0,
557 .name = FOU_GENL_NAME,
558 .version = FOU_GENL_VERSION,
559 .maxattr = FOU_ATTR_MAX,
560 .netnsok = true,
561 };
562
563 static struct nla_policy fou_nl_policy[FOU_ATTR_MAX + 1] = {
564 [FOU_ATTR_PORT] = { .type = NLA_U16, },
565 [FOU_ATTR_AF] = { .type = NLA_U8, },
566 [FOU_ATTR_IPPROTO] = { .type = NLA_U8, },
567 [FOU_ATTR_TYPE] = { .type = NLA_U8, },
568 [FOU_ATTR_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG, },
569 };
570
571 static int parse_nl_config(struct genl_info *info,
572 struct fou_cfg *cfg)
573 {
574 memset(cfg, 0, sizeof(*cfg));
575
576 cfg->udp_config.family = AF_INET;
577
578 if (info->attrs[FOU_ATTR_AF]) {
579 u8 family = nla_get_u8(info->attrs[FOU_ATTR_AF]);
580
581 switch (family) {
582 case AF_INET:
583 break;
584 case AF_INET6:
585 cfg->udp_config.ipv6_v6only = 1;
586 break;
587 default:
588 return -EAFNOSUPPORT;
589 }
590
591 cfg->udp_config.family = family;
592 }
593
594 if (info->attrs[FOU_ATTR_PORT]) {
595 __be16 port = nla_get_be16(info->attrs[FOU_ATTR_PORT]);
596
597 cfg->udp_config.local_udp_port = port;
598 }
599
600 if (info->attrs[FOU_ATTR_IPPROTO])
601 cfg->protocol = nla_get_u8(info->attrs[FOU_ATTR_IPPROTO]);
602
603 if (info->attrs[FOU_ATTR_TYPE])
604 cfg->type = nla_get_u8(info->attrs[FOU_ATTR_TYPE]);
605
606 if (info->attrs[FOU_ATTR_REMCSUM_NOPARTIAL])
607 cfg->flags |= FOU_F_REMCSUM_NOPARTIAL;
608
609 return 0;
610 }
611
612 static int fou_nl_cmd_add_port(struct sk_buff *skb, struct genl_info *info)
613 {
614 struct net *net = genl_info_net(info);
615 struct fou_cfg cfg;
616 int err;
617
618 err = parse_nl_config(info, &cfg);
619 if (err)
620 return err;
621
622 return fou_create(net, &cfg, NULL);
623 }
624
625 static int fou_nl_cmd_rm_port(struct sk_buff *skb, struct genl_info *info)
626 {
627 struct net *net = genl_info_net(info);
628 struct fou_cfg cfg;
629 int err;
630
631 err = parse_nl_config(info, &cfg);
632 if (err)
633 return err;
634
635 return fou_destroy(net, &cfg);
636 }
637
638 static int fou_fill_info(struct fou *fou, struct sk_buff *msg)
639 {
640 if (nla_put_u8(msg, FOU_ATTR_AF, fou->sock->sk->sk_family) ||
641 nla_put_be16(msg, FOU_ATTR_PORT, fou->port) ||
642 nla_put_u8(msg, FOU_ATTR_IPPROTO, fou->protocol) ||
643 nla_put_u8(msg, FOU_ATTR_TYPE, fou->type))
644 return -1;
645
646 if (fou->flags & FOU_F_REMCSUM_NOPARTIAL)
647 if (nla_put_flag(msg, FOU_ATTR_REMCSUM_NOPARTIAL))
648 return -1;
649 return 0;
650 }
651
652 static int fou_dump_info(struct fou *fou, u32 portid, u32 seq,
653 u32 flags, struct sk_buff *skb, u8 cmd)
654 {
655 void *hdr;
656
657 hdr = genlmsg_put(skb, portid, seq, &fou_nl_family, flags, cmd);
658 if (!hdr)
659 return -ENOMEM;
660
661 if (fou_fill_info(fou, skb) < 0)
662 goto nla_put_failure;
663
664 genlmsg_end(skb, hdr);
665 return 0;
666
667 nla_put_failure:
668 genlmsg_cancel(skb, hdr);
669 return -EMSGSIZE;
670 }
671
672 static int fou_nl_cmd_get_port(struct sk_buff *skb, struct genl_info *info)
673 {
674 struct net *net = genl_info_net(info);
675 struct fou_net *fn = net_generic(net, fou_net_id);
676 struct sk_buff *msg;
677 struct fou_cfg cfg;
678 struct fou *fout;
679 __be16 port;
680 u8 family;
681 int ret;
682
683 ret = parse_nl_config(info, &cfg);
684 if (ret)
685 return ret;
686 port = cfg.udp_config.local_udp_port;
687 if (port == 0)
688 return -EINVAL;
689
690 family = cfg.udp_config.family;
691 if (family != AF_INET && family != AF_INET6)
692 return -EINVAL;
693
694 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
695 if (!msg)
696 return -ENOMEM;
697
698 ret = -ESRCH;
699 mutex_lock(&fn->fou_lock);
700 list_for_each_entry(fout, &fn->fou_list, list) {
701 if (port == fout->port && family == fout->family) {
702 ret = fou_dump_info(fout, info->snd_portid,
703 info->snd_seq, 0, msg,
704 info->genlhdr->cmd);
705 break;
706 }
707 }
708 mutex_unlock(&fn->fou_lock);
709 if (ret < 0)
710 goto out_free;
711
712 return genlmsg_reply(msg, info);
713
714 out_free:
715 nlmsg_free(msg);
716 return ret;
717 }
718
719 static int fou_nl_dump(struct sk_buff *skb, struct netlink_callback *cb)
720 {
721 struct net *net = sock_net(skb->sk);
722 struct fou_net *fn = net_generic(net, fou_net_id);
723 struct fou *fout;
724 int idx = 0, ret;
725
726 mutex_lock(&fn->fou_lock);
727 list_for_each_entry(fout, &fn->fou_list, list) {
728 if (idx++ < cb->args[0])
729 continue;
730 ret = fou_dump_info(fout, NETLINK_CB(cb->skb).portid,
731 cb->nlh->nlmsg_seq, NLM_F_MULTI,
732 skb, FOU_CMD_GET);
733 if (ret)
734 break;
735 }
736 mutex_unlock(&fn->fou_lock);
737
738 cb->args[0] = idx;
739 return skb->len;
740 }
741
742 static const struct genl_ops fou_nl_ops[] = {
743 {
744 .cmd = FOU_CMD_ADD,
745 .doit = fou_nl_cmd_add_port,
746 .policy = fou_nl_policy,
747 .flags = GENL_ADMIN_PERM,
748 },
749 {
750 .cmd = FOU_CMD_DEL,
751 .doit = fou_nl_cmd_rm_port,
752 .policy = fou_nl_policy,
753 .flags = GENL_ADMIN_PERM,
754 },
755 {
756 .cmd = FOU_CMD_GET,
757 .doit = fou_nl_cmd_get_port,
758 .dumpit = fou_nl_dump,
759 .policy = fou_nl_policy,
760 },
761 };
762
763 size_t fou_encap_hlen(struct ip_tunnel_encap *e)
764 {
765 return sizeof(struct udphdr);
766 }
767 EXPORT_SYMBOL(fou_encap_hlen);
768
769 size_t gue_encap_hlen(struct ip_tunnel_encap *e)
770 {
771 size_t len;
772 bool need_priv = false;
773
774 len = sizeof(struct udphdr) + sizeof(struct guehdr);
775
776 if (e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) {
777 len += GUE_PLEN_REMCSUM;
778 need_priv = true;
779 }
780
781 len += need_priv ? GUE_LEN_PRIV : 0;
782
783 return len;
784 }
785 EXPORT_SYMBOL(gue_encap_hlen);
786
787 static void fou_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e,
788 struct flowi4 *fl4, u8 *protocol, __be16 sport)
789 {
790 struct udphdr *uh;
791
792 skb_push(skb, sizeof(struct udphdr));
793 skb_reset_transport_header(skb);
794
795 uh = udp_hdr(skb);
796
797 uh->dest = e->dport;
798 uh->source = sport;
799 uh->len = htons(skb->len);
800 udp_set_csum(!(e->flags & TUNNEL_ENCAP_FLAG_CSUM), skb,
801 fl4->saddr, fl4->daddr, skb->len);
802
803 *protocol = IPPROTO_UDP;
804 }
805
806 int __fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
807 u8 *protocol, __be16 *sport, int type)
808 {
809 int err;
810
811 err = iptunnel_handle_offloads(skb, type);
812 if (err)
813 return err;
814
815 *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
816 skb, 0, 0, false);
817
818 return 0;
819 }
820 EXPORT_SYMBOL(__fou_build_header);
821
822 int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
823 u8 *protocol, struct flowi4 *fl4)
824 {
825 int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
826 SKB_GSO_UDP_TUNNEL;
827 __be16 sport;
828 int err;
829
830 err = __fou_build_header(skb, e, protocol, &sport, type);
831 if (err)
832 return err;
833
834 fou_build_udp(skb, e, fl4, protocol, sport);
835
836 return 0;
837 }
838 EXPORT_SYMBOL(fou_build_header);
839
840 int __gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
841 u8 *protocol, __be16 *sport, int type)
842 {
843 struct guehdr *guehdr;
844 size_t hdrlen, optlen = 0;
845 void *data;
846 bool need_priv = false;
847 int err;
848
849 if ((e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) &&
850 skb->ip_summed == CHECKSUM_PARTIAL) {
851 optlen += GUE_PLEN_REMCSUM;
852 type |= SKB_GSO_TUNNEL_REMCSUM;
853 need_priv = true;
854 }
855
856 optlen += need_priv ? GUE_LEN_PRIV : 0;
857
858 err = iptunnel_handle_offloads(skb, type);
859 if (err)
860 return err;
861
862 /* Get source port (based on flow hash) before skb_push */
863 *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
864 skb, 0, 0, false);
865
866 hdrlen = sizeof(struct guehdr) + optlen;
867
868 skb_push(skb, hdrlen);
869
870 guehdr = (struct guehdr *)skb->data;
871
872 guehdr->control = 0;
873 guehdr->version = 0;
874 guehdr->hlen = optlen >> 2;
875 guehdr->flags = 0;
876 guehdr->proto_ctype = *protocol;
877
878 data = &guehdr[1];
879
880 if (need_priv) {
881 __be32 *flags = data;
882
883 guehdr->flags |= GUE_FLAG_PRIV;
884 *flags = 0;
885 data += GUE_LEN_PRIV;
886
887 if (type & SKB_GSO_TUNNEL_REMCSUM) {
888 u16 csum_start = skb_checksum_start_offset(skb);
889 __be16 *pd = data;
890
891 if (csum_start < hdrlen)
892 return -EINVAL;
893
894 csum_start -= hdrlen;
895 pd[0] = htons(csum_start);
896 pd[1] = htons(csum_start + skb->csum_offset);
897
898 if (!skb_is_gso(skb)) {
899 skb->ip_summed = CHECKSUM_NONE;
900 skb->encapsulation = 0;
901 }
902
903 *flags |= GUE_PFLAG_REMCSUM;
904 data += GUE_PLEN_REMCSUM;
905 }
906
907 }
908
909 return 0;
910 }
911 EXPORT_SYMBOL(__gue_build_header);
912
913 int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
914 u8 *protocol, struct flowi4 *fl4)
915 {
916 int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
917 SKB_GSO_UDP_TUNNEL;
918 __be16 sport;
919 int err;
920
921 err = __gue_build_header(skb, e, protocol, &sport, type);
922 if (err)
923 return err;
924
925 fou_build_udp(skb, e, fl4, protocol, sport);
926
927 return 0;
928 }
929 EXPORT_SYMBOL(gue_build_header);
930
931 #ifdef CONFIG_NET_FOU_IP_TUNNELS
932
933 static const struct ip_tunnel_encap_ops fou_iptun_ops = {
934 .encap_hlen = fou_encap_hlen,
935 .build_header = fou_build_header,
936 };
937
938 static const struct ip_tunnel_encap_ops gue_iptun_ops = {
939 .encap_hlen = gue_encap_hlen,
940 .build_header = gue_build_header,
941 };
942
943 static int ip_tunnel_encap_add_fou_ops(void)
944 {
945 int ret;
946
947 ret = ip_tunnel_encap_add_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
948 if (ret < 0) {
949 pr_err("can't add fou ops\n");
950 return ret;
951 }
952
953 ret = ip_tunnel_encap_add_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
954 if (ret < 0) {
955 pr_err("can't add gue ops\n");
956 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
957 return ret;
958 }
959
960 return 0;
961 }
962
963 static void ip_tunnel_encap_del_fou_ops(void)
964 {
965 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
966 ip_tunnel_encap_del_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
967 }
968
969 #else
970
971 static int ip_tunnel_encap_add_fou_ops(void)
972 {
973 return 0;
974 }
975
976 static void ip_tunnel_encap_del_fou_ops(void)
977 {
978 }
979
980 #endif
981
982 static __net_init int fou_init_net(struct net *net)
983 {
984 struct fou_net *fn = net_generic(net, fou_net_id);
985
986 INIT_LIST_HEAD(&fn->fou_list);
987 mutex_init(&fn->fou_lock);
988 return 0;
989 }
990
991 static __net_exit void fou_exit_net(struct net *net)
992 {
993 struct fou_net *fn = net_generic(net, fou_net_id);
994 struct fou *fou, *next;
995
996 /* Close all the FOU sockets */
997 mutex_lock(&fn->fou_lock);
998 list_for_each_entry_safe(fou, next, &fn->fou_list, list)
999 fou_release(fou);
1000 mutex_unlock(&fn->fou_lock);
1001 }
1002
1003 static struct pernet_operations fou_net_ops = {
1004 .init = fou_init_net,
1005 .exit = fou_exit_net,
1006 .id = &fou_net_id,
1007 .size = sizeof(struct fou_net),
1008 };
1009
1010 static int __init fou_init(void)
1011 {
1012 int ret;
1013
1014 ret = register_pernet_device(&fou_net_ops);
1015 if (ret)
1016 goto exit;
1017
1018 ret = genl_register_family_with_ops(&fou_nl_family,
1019 fou_nl_ops);
1020 if (ret < 0)
1021 goto unregister;
1022
1023 ret = ip_tunnel_encap_add_fou_ops();
1024 if (ret == 0)
1025 return 0;
1026
1027 genl_unregister_family(&fou_nl_family);
1028 unregister:
1029 unregister_pernet_device(&fou_net_ops);
1030 exit:
1031 return ret;
1032 }
1033
1034 static void __exit fou_fini(void)
1035 {
1036 ip_tunnel_encap_del_fou_ops();
1037 genl_unregister_family(&fou_nl_family);
1038 unregister_pernet_device(&fou_net_ops);
1039 }
1040
1041 module_init(fou_init);
1042 module_exit(fou_fini);
1043 MODULE_AUTHOR("Tom Herbert <therbert@google.com>");
1044 MODULE_LICENSE("GPL");
This page took 0.054413 seconds and 6 git commands to generate.