Merge tag 'clk-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/clk/linux
[linux/fpc-iii.git] / net / ipv4 / fou.c
blob5f9207c039e704e77c52fcf840e56a46b6b899fd
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>
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;
30 #define FOU_F_REMCSUM_NOPARTIAL BIT(0)
32 struct fou_cfg {
33 u16 type;
34 u8 protocol;
35 u8 flags;
36 struct udp_port_cfg udp_config;
39 static unsigned int fou_net_id;
41 struct fou_net {
42 struct list_head fou_list;
43 struct mutex fou_lock;
46 static inline struct fou *fou_from_sock(struct sock *sk)
48 return sk->sk_user_data;
51 static int fou_recv_pull(struct sk_buff *skb, struct fou *fou, size_t len)
53 /* Remove 'len' bytes from the packet (UDP header and
54 * FOU header if present).
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);
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);
68 static int fou_udp_recv(struct sock *sk, struct sk_buff *skb)
70 struct fou *fou = fou_from_sock(sk);
72 if (!fou)
73 return 1;
75 if (fou_recv_pull(skb, fou, sizeof(struct udphdr)))
76 goto drop;
78 return -fou->protocol;
80 drop:
81 kfree_skb(skb);
82 return 0;
85 static struct guehdr *gue_remcsum(struct sk_buff *skb, struct guehdr *guehdr,
86 void *data, size_t hdrlen, u8 ipproto,
87 bool nopartial)
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);
95 if (skb->remcsum_offload)
96 return guehdr;
98 if (!pskb_may_pull(skb, plen))
99 return NULL;
100 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
102 skb_remcsum_process(skb, (void *)guehdr + hdrlen,
103 start, offset, nopartial);
105 return guehdr;
108 static int gue_control_message(struct sk_buff *skb, struct guehdr *guehdr)
110 /* No support yet */
111 kfree_skb(skb);
112 return 0;
115 static int gue_udp_recv(struct sock *sk, struct sk_buff *skb)
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;
123 if (!fou)
124 return 1;
126 len = sizeof(struct udphdr) + sizeof(struct guehdr);
127 if (!pskb_may_pull(skb, len))
128 goto drop;
130 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
132 optlen = guehdr->hlen << 2;
133 len += optlen;
135 if (!pskb_may_pull(skb, len))
136 goto drop;
138 /* guehdr may change after pull */
139 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
141 hdrlen = sizeof(struct guehdr) + optlen;
143 if (guehdr->version != 0 || validate_gue_flags(guehdr, optlen))
144 goto drop;
146 hdrlen = sizeof(struct guehdr) + optlen;
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);
154 /* Pull csum through the guehdr now . This can be used if
155 * there is a remote checksum offload.
157 skb_postpull_rcsum(skb, udp_hdr(skb), len);
159 data = &guehdr[1];
161 if (guehdr->flags & GUE_FLAG_PRIV) {
162 __be32 flags = *(__be32 *)(data + doffset);
164 doffset += GUE_LEN_PRIV;
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;
174 data = &guehdr[1];
176 doffset += GUE_PLEN_REMCSUM;
180 if (unlikely(guehdr->control))
181 return gue_control_message(skb, guehdr);
183 __skb_pull(skb, sizeof(struct udphdr) + hdrlen);
184 skb_reset_transport_header(skb);
186 if (iptunnel_pull_offloads(skb))
187 goto drop;
189 return -guehdr->proto_ctype;
191 drop:
192 kfree_skb(skb);
193 return 0;
196 static struct sk_buff **fou_gro_receive(struct sock *sk,
197 struct sk_buff **head,
198 struct sk_buff *skb)
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;
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.
211 NAPI_GRO_CB(skb)->encap_mark = 0;
213 /* Flag this frame as already having an outer encap header */
214 NAPI_GRO_CB(skb)->is_fou = 1;
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;
222 pp = ops->callbacks.gro_receive(head, skb);
224 out_unlock:
225 rcu_read_unlock();
227 return pp;
230 static int fou_gro_complete(struct sock *sk, struct sk_buff *skb,
231 int nhoff)
233 const struct net_offload *ops;
234 u8 proto = fou_from_sock(sk)->protocol;
235 int err = -ENOSYS;
236 const struct net_offload **offloads;
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;
244 err = ops->callbacks.gro_complete(skb, nhoff);
246 skb_set_inner_mac_header(skb, nhoff);
248 out_unlock:
249 rcu_read_unlock();
251 return err;
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)
259 __be16 *pd = data;
260 size_t start = ntohs(pd[0]);
261 size_t offset = ntohs(pd[1]);
263 if (skb->remcsum_offload)
264 return guehdr;
266 if (!NAPI_GRO_CB(skb)->csum_valid)
267 return NULL;
269 guehdr = skb_gro_remcsum_process(skb, (void *)guehdr, off, hdrlen,
270 start, offset, grc, nopartial);
272 skb->remcsum_offload = 1;
274 return guehdr;
277 static struct sk_buff **gue_gro_receive(struct sock *sk,
278 struct sk_buff **head,
279 struct sk_buff *skb)
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;
293 skb_gro_remcsum_init(&grc);
295 off = skb_gro_offset(skb);
296 len = off + sizeof(*guehdr);
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;
305 optlen = guehdr->hlen << 2;
306 len += optlen;
308 if (skb_gro_header_hard(skb, len)) {
309 guehdr = skb_gro_header_slow(skb, len, off);
310 if (unlikely(!guehdr))
311 goto out;
314 if (unlikely(guehdr->control) || guehdr->version != 0 ||
315 validate_gue_flags(guehdr, optlen))
316 goto out;
318 hdrlen = sizeof(*guehdr) + optlen;
320 /* Adjust NAPI_GRO_CB(skb)->csum to account for guehdr,
321 * this is needed if there is a remote checkcsum offload.
323 skb_gro_postpull_rcsum(skb, guehdr, hdrlen);
325 data = &guehdr[1];
327 if (guehdr->flags & GUE_FLAG_PRIV) {
328 __be32 flags = *(__be32 *)(data + doffset);
330 doffset += GUE_LEN_PRIV;
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));
338 if (!guehdr)
339 goto out;
341 data = &guehdr[1];
343 doffset += GUE_PLEN_REMCSUM;
347 skb_gro_pull(skb, hdrlen);
349 for (p = *head; p; p = p->next) {
350 const struct guehdr *guehdr2;
352 if (!NAPI_GRO_CB(p)->same_flow)
353 continue;
355 guehdr2 = (struct guehdr *)(p->data + off);
357 /* Compare base GUE header to be equal (covers
358 * hlen, version, proto_ctype, and flags.
360 if (guehdr->word != guehdr2->word) {
361 NAPI_GRO_CB(p)->same_flow = 0;
362 continue;
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;
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.
379 NAPI_GRO_CB(skb)->encap_mark = 0;
381 /* Flag this frame as already having an outer encap header */
382 NAPI_GRO_CB(skb)->is_fou = 1;
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;
390 pp = ops->callbacks.gro_receive(head, skb);
391 flush = 0;
393 out_unlock:
394 rcu_read_unlock();
395 out:
396 NAPI_GRO_CB(skb)->flush |= flush;
397 skb_gro_remcsum_cleanup(skb, &grc);
399 return pp;
402 static int gue_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
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;
411 proto = guehdr->proto_ctype;
413 guehlen = sizeof(*guehdr) + (guehdr->hlen << 2);
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;
421 err = ops->callbacks.gro_complete(skb, nhoff + guehlen);
423 skb_set_inner_mac_header(skb, nhoff + guehlen);
425 out_unlock:
426 rcu_read_unlock();
427 return err;
430 static int fou_add_to_port_list(struct net *net, struct fou *fou)
432 struct fou_net *fn = net_generic(net, fou_net_id);
433 struct fou *fout;
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;
444 list_add(&fou->list, &fn->fou_list);
445 mutex_unlock(&fn->fou_lock);
447 return 0;
450 static void fou_release(struct fou *fou)
452 struct socket *sock = fou->sock;
454 list_del(&fou->list);
455 udp_tunnel_sock_release(sock);
457 kfree_rcu(fou, rcu);
460 static int fou_create(struct net *net, struct fou_cfg *cfg,
461 struct socket **sockp)
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;
469 /* Open UDP socket */
470 err = udp_sock_create(net, &cfg->udp_config, &sock);
471 if (err < 0)
472 goto error;
474 /* Allocate FOU port structure */
475 fou = kzalloc(sizeof(*fou), GFP_KERNEL);
476 if (!fou) {
477 err = -ENOMEM;
478 goto error;
481 sk = sock->sk;
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;
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;
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;
512 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
514 sk->sk_allocation = GFP_ATOMIC;
516 err = fou_add_to_port_list(net, fou);
517 if (err)
518 goto error;
520 if (sockp)
521 *sockp = sock;
523 return 0;
525 error:
526 kfree(fou);
527 if (sock)
528 udp_tunnel_sock_release(sock);
530 return err;
533 static int fou_destroy(struct net *net, struct fou_cfg *cfg)
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;
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;
549 mutex_unlock(&fn->fou_lock);
551 return err;
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,
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, },
571 static int parse_nl_config(struct genl_info *info,
572 struct fou_cfg *cfg)
574 memset(cfg, 0, sizeof(*cfg));
576 cfg->udp_config.family = AF_INET;
578 if (info->attrs[FOU_ATTR_AF]) {
579 u8 family = nla_get_u8(info->attrs[FOU_ATTR_AF]);
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;
591 cfg->udp_config.family = family;
594 if (info->attrs[FOU_ATTR_PORT]) {
595 __be16 port = nla_get_be16(info->attrs[FOU_ATTR_PORT]);
597 cfg->udp_config.local_udp_port = port;
600 if (info->attrs[FOU_ATTR_IPPROTO])
601 cfg->protocol = nla_get_u8(info->attrs[FOU_ATTR_IPPROTO]);
603 if (info->attrs[FOU_ATTR_TYPE])
604 cfg->type = nla_get_u8(info->attrs[FOU_ATTR_TYPE]);
606 if (info->attrs[FOU_ATTR_REMCSUM_NOPARTIAL])
607 cfg->flags |= FOU_F_REMCSUM_NOPARTIAL;
609 return 0;
612 static int fou_nl_cmd_add_port(struct sk_buff *skb, struct genl_info *info)
614 struct net *net = genl_info_net(info);
615 struct fou_cfg cfg;
616 int err;
618 err = parse_nl_config(info, &cfg);
619 if (err)
620 return err;
622 return fou_create(net, &cfg, NULL);
625 static int fou_nl_cmd_rm_port(struct sk_buff *skb, struct genl_info *info)
627 struct net *net = genl_info_net(info);
628 struct fou_cfg cfg;
629 int err;
631 err = parse_nl_config(info, &cfg);
632 if (err)
633 return err;
635 return fou_destroy(net, &cfg);
638 static int fou_fill_info(struct fou *fou, struct sk_buff *msg)
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;
646 if (fou->flags & FOU_F_REMCSUM_NOPARTIAL)
647 if (nla_put_flag(msg, FOU_ATTR_REMCSUM_NOPARTIAL))
648 return -1;
649 return 0;
652 static int fou_dump_info(struct fou *fou, u32 portid, u32 seq,
653 u32 flags, struct sk_buff *skb, u8 cmd)
655 void *hdr;
657 hdr = genlmsg_put(skb, portid, seq, &fou_nl_family, flags, cmd);
658 if (!hdr)
659 return -ENOMEM;
661 if (fou_fill_info(fou, skb) < 0)
662 goto nla_put_failure;
664 genlmsg_end(skb, hdr);
665 return 0;
667 nla_put_failure:
668 genlmsg_cancel(skb, hdr);
669 return -EMSGSIZE;
672 static int fou_nl_cmd_get_port(struct sk_buff *skb, struct genl_info *info)
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;
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;
690 family = cfg.udp_config.family;
691 if (family != AF_INET && family != AF_INET6)
692 return -EINVAL;
694 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
695 if (!msg)
696 return -ENOMEM;
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;
708 mutex_unlock(&fn->fou_lock);
709 if (ret < 0)
710 goto out_free;
712 return genlmsg_reply(msg, info);
714 out_free:
715 nlmsg_free(msg);
716 return ret;
719 static int fou_nl_dump(struct sk_buff *skb, struct netlink_callback *cb)
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;
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;
736 mutex_unlock(&fn->fou_lock);
738 cb->args[0] = idx;
739 return skb->len;
742 static const struct genl_ops fou_nl_ops[] = {
744 .cmd = FOU_CMD_ADD,
745 .doit = fou_nl_cmd_add_port,
746 .policy = fou_nl_policy,
747 .flags = GENL_ADMIN_PERM,
750 .cmd = FOU_CMD_DEL,
751 .doit = fou_nl_cmd_rm_port,
752 .policy = fou_nl_policy,
753 .flags = GENL_ADMIN_PERM,
756 .cmd = FOU_CMD_GET,
757 .doit = fou_nl_cmd_get_port,
758 .dumpit = fou_nl_dump,
759 .policy = fou_nl_policy,
763 size_t fou_encap_hlen(struct ip_tunnel_encap *e)
765 return sizeof(struct udphdr);
767 EXPORT_SYMBOL(fou_encap_hlen);
769 size_t gue_encap_hlen(struct ip_tunnel_encap *e)
771 size_t len;
772 bool need_priv = false;
774 len = sizeof(struct udphdr) + sizeof(struct guehdr);
776 if (e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) {
777 len += GUE_PLEN_REMCSUM;
778 need_priv = true;
781 len += need_priv ? GUE_LEN_PRIV : 0;
783 return len;
785 EXPORT_SYMBOL(gue_encap_hlen);
787 static void fou_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e,
788 struct flowi4 *fl4, u8 *protocol, __be16 sport)
790 struct udphdr *uh;
792 skb_push(skb, sizeof(struct udphdr));
793 skb_reset_transport_header(skb);
795 uh = udp_hdr(skb);
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);
803 *protocol = IPPROTO_UDP;
806 int __fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
807 u8 *protocol, __be16 *sport, int type)
809 int err;
811 err = iptunnel_handle_offloads(skb, type);
812 if (err)
813 return err;
815 *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
816 skb, 0, 0, false);
818 return 0;
820 EXPORT_SYMBOL(__fou_build_header);
822 int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
823 u8 *protocol, struct flowi4 *fl4)
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;
830 err = __fou_build_header(skb, e, protocol, &sport, type);
831 if (err)
832 return err;
834 fou_build_udp(skb, e, fl4, protocol, sport);
836 return 0;
838 EXPORT_SYMBOL(fou_build_header);
840 int __gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
841 u8 *protocol, __be16 *sport, int type)
843 struct guehdr *guehdr;
844 size_t hdrlen, optlen = 0;
845 void *data;
846 bool need_priv = false;
847 int err;
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;
856 optlen += need_priv ? GUE_LEN_PRIV : 0;
858 err = iptunnel_handle_offloads(skb, type);
859 if (err)
860 return err;
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);
866 hdrlen = sizeof(struct guehdr) + optlen;
868 skb_push(skb, hdrlen);
870 guehdr = (struct guehdr *)skb->data;
872 guehdr->control = 0;
873 guehdr->version = 0;
874 guehdr->hlen = optlen >> 2;
875 guehdr->flags = 0;
876 guehdr->proto_ctype = *protocol;
878 data = &guehdr[1];
880 if (need_priv) {
881 __be32 *flags = data;
883 guehdr->flags |= GUE_FLAG_PRIV;
884 *flags = 0;
885 data += GUE_LEN_PRIV;
887 if (type & SKB_GSO_TUNNEL_REMCSUM) {
888 u16 csum_start = skb_checksum_start_offset(skb);
889 __be16 *pd = data;
891 if (csum_start < hdrlen)
892 return -EINVAL;
894 csum_start -= hdrlen;
895 pd[0] = htons(csum_start);
896 pd[1] = htons(csum_start + skb->csum_offset);
898 if (!skb_is_gso(skb)) {
899 skb->ip_summed = CHECKSUM_NONE;
900 skb->encapsulation = 0;
903 *flags |= GUE_PFLAG_REMCSUM;
904 data += GUE_PLEN_REMCSUM;
909 return 0;
911 EXPORT_SYMBOL(__gue_build_header);
913 int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
914 u8 *protocol, struct flowi4 *fl4)
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;
921 err = __gue_build_header(skb, e, protocol, &sport, type);
922 if (err)
923 return err;
925 fou_build_udp(skb, e, fl4, protocol, sport);
927 return 0;
929 EXPORT_SYMBOL(gue_build_header);
931 #ifdef CONFIG_NET_FOU_IP_TUNNELS
933 static const struct ip_tunnel_encap_ops fou_iptun_ops = {
934 .encap_hlen = fou_encap_hlen,
935 .build_header = fou_build_header,
938 static const struct ip_tunnel_encap_ops gue_iptun_ops = {
939 .encap_hlen = gue_encap_hlen,
940 .build_header = gue_build_header,
943 static int ip_tunnel_encap_add_fou_ops(void)
945 int ret;
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;
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;
960 return 0;
963 static void ip_tunnel_encap_del_fou_ops(void)
965 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
966 ip_tunnel_encap_del_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
969 #else
971 static int ip_tunnel_encap_add_fou_ops(void)
973 return 0;
976 static void ip_tunnel_encap_del_fou_ops(void)
980 #endif
982 static __net_init int fou_init_net(struct net *net)
984 struct fou_net *fn = net_generic(net, fou_net_id);
986 INIT_LIST_HEAD(&fn->fou_list);
987 mutex_init(&fn->fou_lock);
988 return 0;
991 static __net_exit void fou_exit_net(struct net *net)
993 struct fou_net *fn = net_generic(net, fou_net_id);
994 struct fou *fou, *next;
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);
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),
1010 static int __init fou_init(void)
1012 int ret;
1014 ret = register_pernet_device(&fou_net_ops);
1015 if (ret)
1016 goto exit;
1018 ret = genl_register_family_with_ops(&fou_nl_family,
1019 fou_nl_ops);
1020 if (ret < 0)
1021 goto unregister;
1023 ret = ip_tunnel_encap_add_fou_ops();
1024 if (ret == 0)
1025 return 0;
1027 genl_unregister_family(&fou_nl_family);
1028 unregister:
1029 unregister_pernet_device(&fou_net_ops);
1030 exit:
1031 return ret;
1034 static void __exit fou_fini(void)
1036 ip_tunnel_encap_del_fou_ops();
1037 genl_unregister_family(&fou_nl_family);
1038 unregister_pernet_device(&fou_net_ops);
1041 module_init(fou_init);
1042 module_exit(fou_fini);
1043 MODULE_AUTHOR("Tom Herbert <therbert@google.com>");
1044 MODULE_LICENSE("GPL");