1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/module.h>
3 #include <linux/errno.h>
4 #include <linux/socket.h>
5 #include <linux/skbuff.h>
7 #include <linux/icmp.h>
9 #include <linux/types.h>
10 #include <linux/kernel.h>
11 #include <net/genetlink.h>
16 #include <net/protocol.h>
18 #include <net/udp_tunnel.h>
20 #include <uapi/linux/fou.h>
21 #include <uapi/linux/genetlink.h>
30 struct list_head list;
34 #define FOU_F_REMCSUM_NOPARTIAL BIT(0)
40 struct udp_port_cfg udp_config;
43 static unsigned int fou_net_id;
46 struct list_head fou_list;
47 struct mutex fou_lock;
50 static inline struct fou *fou_from_sock(struct sock *sk)
52 return sk->sk_user_data;
55 static int fou_recv_pull(struct sk_buff *skb, struct fou *fou, size_t len)
57 /* Remove 'len' bytes from the packet (UDP header and
58 * FOU header if present).
60 if (fou->family == AF_INET)
61 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
63 ipv6_hdr(skb)->payload_len =
64 htons(ntohs(ipv6_hdr(skb)->payload_len) - len);
67 skb_postpull_rcsum(skb, udp_hdr(skb), len);
68 skb_reset_transport_header(skb);
69 return iptunnel_pull_offloads(skb);
72 static int fou_udp_recv(struct sock *sk, struct sk_buff *skb)
74 struct fou *fou = fou_from_sock(sk);
79 if (fou_recv_pull(skb, fou, sizeof(struct udphdr)))
82 return -fou->protocol;
89 static struct guehdr *gue_remcsum(struct sk_buff *skb, struct guehdr *guehdr,
90 void *data, size_t hdrlen, u8 ipproto,
94 size_t start = ntohs(pd[0]);
95 size_t offset = ntohs(pd[1]);
96 size_t plen = sizeof(struct udphdr) + hdrlen +
97 max_t(size_t, offset + sizeof(u16), start);
99 if (skb->remcsum_offload)
102 if (!pskb_may_pull(skb, plen))
104 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
106 skb_remcsum_process(skb, (void *)guehdr + hdrlen,
107 start, offset, nopartial);
112 static int gue_control_message(struct sk_buff *skb, struct guehdr *guehdr)
119 static int gue_udp_recv(struct sock *sk, struct sk_buff *skb)
121 struct fou *fou = fou_from_sock(sk);
122 size_t len, optlen, hdrlen;
123 struct guehdr *guehdr;
131 len = sizeof(struct udphdr) + sizeof(struct guehdr);
132 if (!pskb_may_pull(skb, len))
135 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
137 switch (guehdr->version) {
138 case 0: /* Full GUE header present */
142 /* Direct encapsulation of IPv4 or IPv6 */
146 switch (((struct iphdr *)guehdr)->version) {
157 if (fou_recv_pull(skb, fou, sizeof(struct udphdr)))
163 default: /* Undefined version */
167 optlen = guehdr->hlen << 2;
170 if (!pskb_may_pull(skb, len))
173 /* guehdr may change after pull */
174 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
176 if (validate_gue_flags(guehdr, optlen))
179 hdrlen = sizeof(struct guehdr) + optlen;
181 if (fou->family == AF_INET)
182 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
184 ipv6_hdr(skb)->payload_len =
185 htons(ntohs(ipv6_hdr(skb)->payload_len) - len);
187 /* Pull csum through the guehdr now . This can be used if
188 * there is a remote checksum offload.
190 skb_postpull_rcsum(skb, udp_hdr(skb), len);
194 if (guehdr->flags & GUE_FLAG_PRIV) {
195 __be32 flags = *(__be32 *)(data + doffset);
197 doffset += GUE_LEN_PRIV;
199 if (flags & GUE_PFLAG_REMCSUM) {
200 guehdr = gue_remcsum(skb, guehdr, data + doffset,
201 hdrlen, guehdr->proto_ctype,
203 FOU_F_REMCSUM_NOPARTIAL));
209 doffset += GUE_PLEN_REMCSUM;
213 if (unlikely(guehdr->control))
214 return gue_control_message(skb, guehdr);
216 proto_ctype = guehdr->proto_ctype;
217 __skb_pull(skb, sizeof(struct udphdr) + hdrlen);
218 skb_reset_transport_header(skb);
220 if (iptunnel_pull_offloads(skb))
230 static struct sk_buff *fou_gro_receive(struct sock *sk,
231 struct list_head *head,
234 const struct net_offload __rcu **offloads;
235 u8 proto = fou_from_sock(sk)->protocol;
236 const struct net_offload *ops;
237 struct sk_buff *pp = NULL;
239 /* 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 simply
242 * treating the GRE tunnel header as though it is a UDP protocol
243 * specific header such as VXLAN or GENEVE.
245 NAPI_GRO_CB(skb)->encap_mark = 0;
247 /* Flag this frame as already having an outer encap header */
248 NAPI_GRO_CB(skb)->is_fou = 1;
251 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
252 ops = rcu_dereference(offloads[proto]);
253 if (!ops || !ops->callbacks.gro_receive)
256 pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
264 static int fou_gro_complete(struct sock *sk, struct sk_buff *skb,
267 const struct net_offload __rcu **offloads;
268 u8 proto = fou_from_sock(sk)->protocol;
269 const struct net_offload *ops;
273 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
274 ops = rcu_dereference(offloads[proto]);
275 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
278 err = ops->callbacks.gro_complete(skb, nhoff);
280 skb_set_inner_mac_header(skb, nhoff);
288 static struct guehdr *gue_gro_remcsum(struct sk_buff *skb, unsigned int off,
289 struct guehdr *guehdr, void *data,
290 size_t hdrlen, struct gro_remcsum *grc,
294 size_t start = ntohs(pd[0]);
295 size_t offset = ntohs(pd[1]);
297 if (skb->remcsum_offload)
300 if (!NAPI_GRO_CB(skb)->csum_valid)
303 guehdr = skb_gro_remcsum_process(skb, (void *)guehdr, off, hdrlen,
304 start, offset, grc, nopartial);
306 skb->remcsum_offload = 1;
311 static struct sk_buff *gue_gro_receive(struct sock *sk,
312 struct list_head *head,
315 const struct net_offload __rcu **offloads;
316 const struct net_offload *ops;
317 struct sk_buff *pp = NULL;
319 struct guehdr *guehdr;
320 size_t len, optlen, hdrlen, off;
324 struct fou *fou = fou_from_sock(sk);
325 struct gro_remcsum grc;
328 skb_gro_remcsum_init(&grc);
330 off = skb_gro_offset(skb);
331 len = off + sizeof(*guehdr);
333 guehdr = skb_gro_header_fast(skb, off);
334 if (skb_gro_header_hard(skb, len)) {
335 guehdr = skb_gro_header_slow(skb, len, off);
336 if (unlikely(!guehdr))
340 switch (guehdr->version) {
344 switch (((struct iphdr *)guehdr)->version) {
346 proto = IPPROTO_IPIP;
349 proto = IPPROTO_IPV6;
359 optlen = guehdr->hlen << 2;
362 if (skb_gro_header_hard(skb, len)) {
363 guehdr = skb_gro_header_slow(skb, len, off);
364 if (unlikely(!guehdr))
368 if (unlikely(guehdr->control) || guehdr->version != 0 ||
369 validate_gue_flags(guehdr, optlen))
372 hdrlen = sizeof(*guehdr) + optlen;
374 /* Adjust NAPI_GRO_CB(skb)->csum to account for guehdr,
375 * this is needed if there is a remote checkcsum offload.
377 skb_gro_postpull_rcsum(skb, guehdr, hdrlen);
381 if (guehdr->flags & GUE_FLAG_PRIV) {
382 __be32 flags = *(__be32 *)(data + doffset);
384 doffset += GUE_LEN_PRIV;
386 if (flags & GUE_PFLAG_REMCSUM) {
387 guehdr = gue_gro_remcsum(skb, off, guehdr,
388 data + doffset, hdrlen, &grc,
390 FOU_F_REMCSUM_NOPARTIAL));
397 doffset += GUE_PLEN_REMCSUM;
401 skb_gro_pull(skb, hdrlen);
403 list_for_each_entry(p, head, list) {
404 const struct guehdr *guehdr2;
406 if (!NAPI_GRO_CB(p)->same_flow)
409 guehdr2 = (struct guehdr *)(p->data + off);
411 /* Compare base GUE header to be equal (covers
412 * hlen, version, proto_ctype, and flags.
414 if (guehdr->word != guehdr2->word) {
415 NAPI_GRO_CB(p)->same_flow = 0;
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;
427 proto = guehdr->proto_ctype;
431 /* 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 simply
434 * treating the GRE tunnel header as though it is a UDP protocol
435 * specific header such as VXLAN or GENEVE.
437 NAPI_GRO_CB(skb)->encap_mark = 0;
439 /* Flag this frame as already having an outer encap header */
440 NAPI_GRO_CB(skb)->is_fou = 1;
443 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
444 ops = rcu_dereference(offloads[proto]);
445 if (WARN_ON_ONCE(!ops || !ops->callbacks.gro_receive))
448 pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
454 skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
459 static int gue_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
461 struct guehdr *guehdr = (struct guehdr *)(skb->data + nhoff);
462 const struct net_offload __rcu **offloads;
463 const struct net_offload *ops;
464 unsigned int guehlen = 0;
468 switch (guehdr->version) {
470 proto = guehdr->proto_ctype;
471 guehlen = sizeof(*guehdr) + (guehdr->hlen << 2);
474 switch (((struct iphdr *)guehdr)->version) {
476 proto = IPPROTO_IPIP;
479 proto = IPPROTO_IPV6;
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))
495 err = ops->callbacks.gro_complete(skb, nhoff + guehlen);
497 skb_set_inner_mac_header(skb, nhoff + guehlen);
504 static bool fou_cfg_cmp(struct fou *fou, struct fou_cfg *cfg)
506 struct sock *sk = fou->sock->sk;
507 struct udp_port_cfg *udp_cfg = &cfg->udp_config;
509 if (fou->family != udp_cfg->family ||
510 fou->port != udp_cfg->local_udp_port ||
511 sk->sk_dport != udp_cfg->peer_udp_port ||
512 sk->sk_bound_dev_if != udp_cfg->bind_ifindex)
515 if (fou->family == AF_INET) {
516 if (sk->sk_rcv_saddr != udp_cfg->local_ip.s_addr ||
517 sk->sk_daddr != udp_cfg->peer_ip.s_addr)
521 #if IS_ENABLED(CONFIG_IPV6)
523 if (ipv6_addr_cmp(&sk->sk_v6_rcv_saddr, &udp_cfg->local_ip6) ||
524 ipv6_addr_cmp(&sk->sk_v6_daddr, &udp_cfg->peer_ip6))
534 static int fou_add_to_port_list(struct net *net, struct fou *fou,
537 struct fou_net *fn = net_generic(net, fou_net_id);
540 mutex_lock(&fn->fou_lock);
541 list_for_each_entry(fout, &fn->fou_list, list) {
542 if (fou_cfg_cmp(fout, cfg)) {
543 mutex_unlock(&fn->fou_lock);
548 list_add(&fou->list, &fn->fou_list);
549 mutex_unlock(&fn->fou_lock);
554 static void fou_release(struct fou *fou)
556 struct socket *sock = fou->sock;
558 list_del(&fou->list);
559 udp_tunnel_sock_release(sock);
564 static int fou_create(struct net *net, struct fou_cfg *cfg,
565 struct socket **sockp)
567 struct socket *sock = NULL;
568 struct fou *fou = NULL;
570 struct udp_tunnel_sock_cfg tunnel_cfg;
573 /* Open UDP socket */
574 err = udp_sock_create(net, &cfg->udp_config, &sock);
578 /* Allocate FOU port structure */
579 fou = kzalloc(sizeof(*fou), GFP_KERNEL);
587 fou->port = cfg->udp_config.local_udp_port;
588 fou->family = cfg->udp_config.family;
589 fou->flags = cfg->flags;
590 fou->type = cfg->type;
593 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
594 tunnel_cfg.encap_type = 1;
595 tunnel_cfg.sk_user_data = fou;
596 tunnel_cfg.encap_destroy = NULL;
598 /* Initial for fou type */
600 case FOU_ENCAP_DIRECT:
601 tunnel_cfg.encap_rcv = fou_udp_recv;
602 tunnel_cfg.gro_receive = fou_gro_receive;
603 tunnel_cfg.gro_complete = fou_gro_complete;
604 fou->protocol = cfg->protocol;
607 tunnel_cfg.encap_rcv = gue_udp_recv;
608 tunnel_cfg.gro_receive = gue_gro_receive;
609 tunnel_cfg.gro_complete = gue_gro_complete;
616 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
618 sk->sk_allocation = GFP_ATOMIC;
620 err = fou_add_to_port_list(net, fou, cfg);
632 udp_tunnel_sock_release(sock);
637 static int fou_destroy(struct net *net, struct fou_cfg *cfg)
639 struct fou_net *fn = net_generic(net, fou_net_id);
643 mutex_lock(&fn->fou_lock);
644 list_for_each_entry(fou, &fn->fou_list, list) {
645 if (fou_cfg_cmp(fou, cfg)) {
651 mutex_unlock(&fn->fou_lock);
656 static struct genl_family fou_nl_family;
658 static const struct nla_policy fou_nl_policy[FOU_ATTR_MAX + 1] = {
659 [FOU_ATTR_PORT] = { .type = NLA_U16, },
660 [FOU_ATTR_AF] = { .type = NLA_U8, },
661 [FOU_ATTR_IPPROTO] = { .type = NLA_U8, },
662 [FOU_ATTR_TYPE] = { .type = NLA_U8, },
663 [FOU_ATTR_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG, },
664 [FOU_ATTR_LOCAL_V4] = { .type = NLA_U32, },
665 [FOU_ATTR_PEER_V4] = { .type = NLA_U32, },
666 [FOU_ATTR_LOCAL_V6] = { .len = sizeof(struct in6_addr), },
667 [FOU_ATTR_PEER_V6] = { .len = sizeof(struct in6_addr), },
668 [FOU_ATTR_PEER_PORT] = { .type = NLA_U16, },
669 [FOU_ATTR_IFINDEX] = { .type = NLA_S32, },
672 static int parse_nl_config(struct genl_info *info,
675 bool has_local = false, has_peer = false;
680 memset(cfg, 0, sizeof(*cfg));
682 cfg->udp_config.family = AF_INET;
684 if (info->attrs[FOU_ATTR_AF]) {
685 u8 family = nla_get_u8(info->attrs[FOU_ATTR_AF]);
691 cfg->udp_config.ipv6_v6only = 1;
694 return -EAFNOSUPPORT;
697 cfg->udp_config.family = family;
700 if (info->attrs[FOU_ATTR_PORT]) {
701 port = nla_get_be16(info->attrs[FOU_ATTR_PORT]);
702 cfg->udp_config.local_udp_port = port;
705 if (info->attrs[FOU_ATTR_IPPROTO])
706 cfg->protocol = nla_get_u8(info->attrs[FOU_ATTR_IPPROTO]);
708 if (info->attrs[FOU_ATTR_TYPE])
709 cfg->type = nla_get_u8(info->attrs[FOU_ATTR_TYPE]);
711 if (info->attrs[FOU_ATTR_REMCSUM_NOPARTIAL])
712 cfg->flags |= FOU_F_REMCSUM_NOPARTIAL;
714 if (cfg->udp_config.family == AF_INET) {
715 if (info->attrs[FOU_ATTR_LOCAL_V4]) {
716 attr = info->attrs[FOU_ATTR_LOCAL_V4];
717 cfg->udp_config.local_ip.s_addr = nla_get_in_addr(attr);
721 if (info->attrs[FOU_ATTR_PEER_V4]) {
722 attr = info->attrs[FOU_ATTR_PEER_V4];
723 cfg->udp_config.peer_ip.s_addr = nla_get_in_addr(attr);
726 #if IS_ENABLED(CONFIG_IPV6)
728 if (info->attrs[FOU_ATTR_LOCAL_V6]) {
729 attr = info->attrs[FOU_ATTR_LOCAL_V6];
730 cfg->udp_config.local_ip6 = nla_get_in6_addr(attr);
734 if (info->attrs[FOU_ATTR_PEER_V6]) {
735 attr = info->attrs[FOU_ATTR_PEER_V6];
736 cfg->udp_config.peer_ip6 = nla_get_in6_addr(attr);
743 if (info->attrs[FOU_ATTR_PEER_PORT]) {
744 port = nla_get_be16(info->attrs[FOU_ATTR_PEER_PORT]);
745 cfg->udp_config.peer_udp_port = port;
751 if (info->attrs[FOU_ATTR_IFINDEX]) {
755 ifindex = nla_get_s32(info->attrs[FOU_ATTR_IFINDEX]);
757 cfg->udp_config.bind_ifindex = ifindex;
763 static int fou_nl_cmd_add_port(struct sk_buff *skb, struct genl_info *info)
765 struct net *net = genl_info_net(info);
769 err = parse_nl_config(info, &cfg);
773 return fou_create(net, &cfg, NULL);
776 static int fou_nl_cmd_rm_port(struct sk_buff *skb, struct genl_info *info)
778 struct net *net = genl_info_net(info);
782 err = parse_nl_config(info, &cfg);
786 return fou_destroy(net, &cfg);
789 static int fou_fill_info(struct fou *fou, struct sk_buff *msg)
791 struct sock *sk = fou->sock->sk;
793 if (nla_put_u8(msg, FOU_ATTR_AF, fou->sock->sk->sk_family) ||
794 nla_put_be16(msg, FOU_ATTR_PORT, fou->port) ||
795 nla_put_be16(msg, FOU_ATTR_PEER_PORT, sk->sk_dport) ||
796 nla_put_u8(msg, FOU_ATTR_IPPROTO, fou->protocol) ||
797 nla_put_u8(msg, FOU_ATTR_TYPE, fou->type) ||
798 nla_put_s32(msg, FOU_ATTR_IFINDEX, sk->sk_bound_dev_if))
801 if (fou->flags & FOU_F_REMCSUM_NOPARTIAL)
802 if (nla_put_flag(msg, FOU_ATTR_REMCSUM_NOPARTIAL))
805 if (fou->sock->sk->sk_family == AF_INET) {
806 if (nla_put_in_addr(msg, FOU_ATTR_LOCAL_V4, sk->sk_rcv_saddr))
809 if (nla_put_in_addr(msg, FOU_ATTR_PEER_V4, sk->sk_daddr))
811 #if IS_ENABLED(CONFIG_IPV6)
813 if (nla_put_in6_addr(msg, FOU_ATTR_LOCAL_V6,
814 &sk->sk_v6_rcv_saddr))
817 if (nla_put_in6_addr(msg, FOU_ATTR_PEER_V6, &sk->sk_v6_daddr))
825 static int fou_dump_info(struct fou *fou, u32 portid, u32 seq,
826 u32 flags, struct sk_buff *skb, u8 cmd)
830 hdr = genlmsg_put(skb, portid, seq, &fou_nl_family, flags, cmd);
834 if (fou_fill_info(fou, skb) < 0)
835 goto nla_put_failure;
837 genlmsg_end(skb, hdr);
841 genlmsg_cancel(skb, hdr);
845 static int fou_nl_cmd_get_port(struct sk_buff *skb, struct genl_info *info)
847 struct net *net = genl_info_net(info);
848 struct fou_net *fn = net_generic(net, fou_net_id);
856 ret = parse_nl_config(info, &cfg);
859 port = cfg.udp_config.local_udp_port;
863 family = cfg.udp_config.family;
864 if (family != AF_INET && family != AF_INET6)
867 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
872 mutex_lock(&fn->fou_lock);
873 list_for_each_entry(fout, &fn->fou_list, list) {
874 if (fou_cfg_cmp(fout, &cfg)) {
875 ret = fou_dump_info(fout, info->snd_portid,
876 info->snd_seq, 0, msg,
881 mutex_unlock(&fn->fou_lock);
885 return genlmsg_reply(msg, info);
892 static int fou_nl_dump(struct sk_buff *skb, struct netlink_callback *cb)
894 struct net *net = sock_net(skb->sk);
895 struct fou_net *fn = net_generic(net, fou_net_id);
899 mutex_lock(&fn->fou_lock);
900 list_for_each_entry(fout, &fn->fou_list, list) {
901 if (idx++ < cb->args[0])
903 ret = fou_dump_info(fout, NETLINK_CB(cb->skb).portid,
904 cb->nlh->nlmsg_seq, NLM_F_MULTI,
909 mutex_unlock(&fn->fou_lock);
915 static const struct genl_small_ops fou_nl_ops[] = {
918 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
919 .doit = fou_nl_cmd_add_port,
920 .flags = GENL_ADMIN_PERM,
924 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
925 .doit = fou_nl_cmd_rm_port,
926 .flags = GENL_ADMIN_PERM,
930 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
931 .doit = fou_nl_cmd_get_port,
932 .dumpit = fou_nl_dump,
936 static struct genl_family fou_nl_family __ro_after_init = {
938 .name = FOU_GENL_NAME,
939 .version = FOU_GENL_VERSION,
940 .maxattr = FOU_ATTR_MAX,
941 .policy = fou_nl_policy,
943 .module = THIS_MODULE,
944 .small_ops = fou_nl_ops,
945 .n_small_ops = ARRAY_SIZE(fou_nl_ops),
948 size_t fou_encap_hlen(struct ip_tunnel_encap *e)
950 return sizeof(struct udphdr);
952 EXPORT_SYMBOL(fou_encap_hlen);
954 size_t gue_encap_hlen(struct ip_tunnel_encap *e)
957 bool need_priv = false;
959 len = sizeof(struct udphdr) + sizeof(struct guehdr);
961 if (e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) {
962 len += GUE_PLEN_REMCSUM;
966 len += need_priv ? GUE_LEN_PRIV : 0;
970 EXPORT_SYMBOL(gue_encap_hlen);
972 int __fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
973 u8 *protocol, __be16 *sport, int type)
977 err = iptunnel_handle_offloads(skb, type);
981 *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
986 EXPORT_SYMBOL(__fou_build_header);
988 int __gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
989 u8 *protocol, __be16 *sport, int type)
991 struct guehdr *guehdr;
992 size_t hdrlen, optlen = 0;
994 bool need_priv = false;
997 if ((e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) &&
998 skb->ip_summed == CHECKSUM_PARTIAL) {
999 optlen += GUE_PLEN_REMCSUM;
1000 type |= SKB_GSO_TUNNEL_REMCSUM;
1004 optlen += need_priv ? GUE_LEN_PRIV : 0;
1006 err = iptunnel_handle_offloads(skb, type);
1010 /* Get source port (based on flow hash) before skb_push */
1011 *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
1014 hdrlen = sizeof(struct guehdr) + optlen;
1016 skb_push(skb, hdrlen);
1018 guehdr = (struct guehdr *)skb->data;
1020 guehdr->control = 0;
1021 guehdr->version = 0;
1022 guehdr->hlen = optlen >> 2;
1024 guehdr->proto_ctype = *protocol;
1029 __be32 *flags = data;
1031 guehdr->flags |= GUE_FLAG_PRIV;
1033 data += GUE_LEN_PRIV;
1035 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1036 u16 csum_start = skb_checksum_start_offset(skb);
1039 if (csum_start < hdrlen)
1042 csum_start -= hdrlen;
1043 pd[0] = htons(csum_start);
1044 pd[1] = htons(csum_start + skb->csum_offset);
1046 if (!skb_is_gso(skb)) {
1047 skb->ip_summed = CHECKSUM_NONE;
1048 skb->encapsulation = 0;
1051 *flags |= GUE_PFLAG_REMCSUM;
1052 data += GUE_PLEN_REMCSUM;
1059 EXPORT_SYMBOL(__gue_build_header);
1061 #ifdef CONFIG_NET_FOU_IP_TUNNELS
1063 static void fou_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e,
1064 struct flowi4 *fl4, u8 *protocol, __be16 sport)
1068 skb_push(skb, sizeof(struct udphdr));
1069 skb_reset_transport_header(skb);
1073 uh->dest = e->dport;
1075 uh->len = htons(skb->len);
1076 udp_set_csum(!(e->flags & TUNNEL_ENCAP_FLAG_CSUM), skb,
1077 fl4->saddr, fl4->daddr, skb->len);
1079 *protocol = IPPROTO_UDP;
1082 static int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
1083 u8 *protocol, struct flowi4 *fl4)
1085 int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
1090 err = __fou_build_header(skb, e, protocol, &sport, type);
1094 fou_build_udp(skb, e, fl4, protocol, sport);
1099 static int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
1100 u8 *protocol, struct flowi4 *fl4)
1102 int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
1107 err = __gue_build_header(skb, e, protocol, &sport, type);
1111 fou_build_udp(skb, e, fl4, protocol, sport);
1116 static int gue_err_proto_handler(int proto, struct sk_buff *skb, u32 info)
1118 const struct net_protocol *ipprot = rcu_dereference(inet_protos[proto]);
1120 if (ipprot && ipprot->err_handler) {
1121 if (!ipprot->err_handler(skb, info))
1128 static int gue_err(struct sk_buff *skb, u32 info)
1130 int transport_offset = skb_transport_offset(skb);
1131 struct guehdr *guehdr;
1135 len = sizeof(struct udphdr) + sizeof(struct guehdr);
1136 if (!pskb_may_pull(skb, transport_offset + len))
1139 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
1141 switch (guehdr->version) {
1142 case 0: /* Full GUE header present */
1145 /* Direct encapsulation of IPv4 or IPv6 */
1146 skb_set_transport_header(skb, -(int)sizeof(struct icmphdr));
1148 switch (((struct iphdr *)guehdr)->version) {
1150 ret = gue_err_proto_handler(IPPROTO_IPIP, skb, info);
1152 #if IS_ENABLED(CONFIG_IPV6)
1154 ret = gue_err_proto_handler(IPPROTO_IPV6, skb, info);
1162 default: /* Undefined version */
1166 if (guehdr->control)
1169 optlen = guehdr->hlen << 2;
1171 if (!pskb_may_pull(skb, transport_offset + len + optlen))
1174 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
1175 if (validate_gue_flags(guehdr, optlen))
1178 /* Handling exceptions for direct UDP encapsulation in GUE would lead to
1179 * recursion. Besides, this kind of encapsulation can't even be
1180 * configured currently. Discard this.
1182 if (guehdr->proto_ctype == IPPROTO_UDP ||
1183 guehdr->proto_ctype == IPPROTO_UDPLITE)
1186 skb_set_transport_header(skb, -(int)sizeof(struct icmphdr));
1187 ret = gue_err_proto_handler(guehdr->proto_ctype, skb, info);
1190 skb_set_transport_header(skb, transport_offset);
1195 static const struct ip_tunnel_encap_ops fou_iptun_ops = {
1196 .encap_hlen = fou_encap_hlen,
1197 .build_header = fou_build_header,
1198 .err_handler = gue_err,
1201 static const struct ip_tunnel_encap_ops gue_iptun_ops = {
1202 .encap_hlen = gue_encap_hlen,
1203 .build_header = gue_build_header,
1204 .err_handler = gue_err,
1207 static int ip_tunnel_encap_add_fou_ops(void)
1211 ret = ip_tunnel_encap_add_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
1213 pr_err("can't add fou ops\n");
1217 ret = ip_tunnel_encap_add_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
1219 pr_err("can't add gue ops\n");
1220 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
1227 static void ip_tunnel_encap_del_fou_ops(void)
1229 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
1230 ip_tunnel_encap_del_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
1235 static int ip_tunnel_encap_add_fou_ops(void)
1240 static void ip_tunnel_encap_del_fou_ops(void)
1246 static __net_init int fou_init_net(struct net *net)
1248 struct fou_net *fn = net_generic(net, fou_net_id);
1250 INIT_LIST_HEAD(&fn->fou_list);
1251 mutex_init(&fn->fou_lock);
1255 static __net_exit void fou_exit_net(struct net *net)
1257 struct fou_net *fn = net_generic(net, fou_net_id);
1258 struct fou *fou, *next;
1260 /* Close all the FOU sockets */
1261 mutex_lock(&fn->fou_lock);
1262 list_for_each_entry_safe(fou, next, &fn->fou_list, list)
1264 mutex_unlock(&fn->fou_lock);
1267 static struct pernet_operations fou_net_ops = {
1268 .init = fou_init_net,
1269 .exit = fou_exit_net,
1271 .size = sizeof(struct fou_net),
1274 static int __init fou_init(void)
1278 ret = register_pernet_device(&fou_net_ops);
1282 ret = genl_register_family(&fou_nl_family);
1286 ret = ip_tunnel_encap_add_fou_ops();
1290 genl_unregister_family(&fou_nl_family);
1292 unregister_pernet_device(&fou_net_ops);
1297 static void __exit fou_fini(void)
1299 ip_tunnel_encap_del_fou_ops();
1300 genl_unregister_family(&fou_nl_family);
1301 unregister_pernet_device(&fou_net_ops);
1304 module_init(fou_init);
1305 module_exit(fou_fini);
1307 MODULE_LICENSE("GPL");
1308 MODULE_DESCRIPTION("Foo over UDP");