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>
19 #include <uapi/linux/fou.h>
20 #include <uapi/linux/genetlink.h>
31 struct list_head list;
35 #define FOU_F_REMCSUM_NOPARTIAL BIT(0)
41 struct udp_port_cfg udp_config;
44 static unsigned int fou_net_id;
47 struct list_head fou_list;
48 struct mutex fou_lock;
51 static inline struct fou *fou_from_sock(struct sock *sk)
53 return rcu_dereference_sk_user_data(sk);
56 static int fou_recv_pull(struct sk_buff *skb, struct fou *fou, size_t len)
58 /* Remove 'len' bytes from the packet (UDP header and
59 * FOU header if present).
61 if (fou->family == AF_INET)
62 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
64 ipv6_hdr(skb)->payload_len =
65 htons(ntohs(ipv6_hdr(skb)->payload_len) - len);
68 skb_postpull_rcsum(skb, udp_hdr(skb), len);
69 skb_reset_transport_header(skb);
70 return iptunnel_pull_offloads(skb);
73 static int fou_udp_recv(struct sock *sk, struct sk_buff *skb)
75 struct fou *fou = fou_from_sock(sk);
80 if (fou_recv_pull(skb, fou, sizeof(struct udphdr)))
83 return -fou->protocol;
90 static struct guehdr *gue_remcsum(struct sk_buff *skb, struct guehdr *guehdr,
91 void *data, size_t hdrlen, u8 ipproto,
95 size_t start = ntohs(pd[0]);
96 size_t offset = ntohs(pd[1]);
97 size_t plen = sizeof(struct udphdr) + hdrlen +
98 max_t(size_t, offset + sizeof(u16), start);
100 if (skb->remcsum_offload)
103 if (!pskb_may_pull(skb, plen))
105 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
107 skb_remcsum_process(skb, (void *)guehdr + hdrlen,
108 start, offset, nopartial);
113 static int gue_control_message(struct sk_buff *skb, struct guehdr *guehdr)
120 static int gue_udp_recv(struct sock *sk, struct sk_buff *skb)
122 struct fou *fou = fou_from_sock(sk);
123 size_t len, optlen, hdrlen;
124 struct guehdr *guehdr;
132 len = sizeof(struct udphdr) + sizeof(struct guehdr);
133 if (!pskb_may_pull(skb, len))
136 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
138 switch (guehdr->version) {
139 case 0: /* Full GUE header present */
143 /* Direct encapsulation of IPv4 or IPv6 */
147 switch (((struct iphdr *)guehdr)->version) {
158 if (fou_recv_pull(skb, fou, sizeof(struct udphdr)))
164 default: /* Undefined version */
168 optlen = guehdr->hlen << 2;
171 if (!pskb_may_pull(skb, len))
174 /* guehdr may change after pull */
175 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
177 if (validate_gue_flags(guehdr, optlen))
180 hdrlen = sizeof(struct guehdr) + optlen;
182 if (fou->family == AF_INET)
183 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
185 ipv6_hdr(skb)->payload_len =
186 htons(ntohs(ipv6_hdr(skb)->payload_len) - len);
188 /* Pull csum through the guehdr now . This can be used if
189 * there is a remote checksum offload.
191 skb_postpull_rcsum(skb, udp_hdr(skb), len);
195 if (guehdr->flags & GUE_FLAG_PRIV) {
196 __be32 flags = *(__be32 *)(data + doffset);
198 doffset += GUE_LEN_PRIV;
200 if (flags & GUE_PFLAG_REMCSUM) {
201 guehdr = gue_remcsum(skb, guehdr, data + doffset,
202 hdrlen, guehdr->proto_ctype,
204 FOU_F_REMCSUM_NOPARTIAL));
210 doffset += GUE_PLEN_REMCSUM;
214 if (unlikely(guehdr->control))
215 return gue_control_message(skb, guehdr);
217 proto_ctype = guehdr->proto_ctype;
218 __skb_pull(skb, sizeof(struct udphdr) + hdrlen);
219 skb_reset_transport_header(skb);
221 if (iptunnel_pull_offloads(skb))
231 static struct sk_buff *fou_gro_receive(struct sock *sk,
232 struct list_head *head,
235 const struct net_offload __rcu **offloads;
236 struct fou *fou = fou_from_sock(sk);
237 const struct net_offload *ops;
238 struct sk_buff *pp = NULL;
244 proto = fou->protocol;
246 /* We can clear the encap_mark for FOU as we are essentially doing
247 * one of two possible things. We are either adding an L4 tunnel
248 * header to the outer L3 tunnel header, or we are simply
249 * treating the GRE tunnel header as though it is a UDP protocol
250 * specific header such as VXLAN or GENEVE.
252 NAPI_GRO_CB(skb)->encap_mark = 0;
254 /* Flag this frame as already having an outer encap header */
255 NAPI_GRO_CB(skb)->is_fou = 1;
257 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
258 ops = rcu_dereference(offloads[proto]);
259 if (!ops || !ops->callbacks.gro_receive)
262 pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
268 static int fou_gro_complete(struct sock *sk, struct sk_buff *skb,
271 const struct net_offload __rcu **offloads;
272 struct fou *fou = fou_from_sock(sk);
273 const struct net_offload *ops;
282 proto = fou->protocol;
284 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
285 ops = rcu_dereference(offloads[proto]);
286 if (WARN_ON(!ops || !ops->callbacks.gro_complete)) {
291 err = ops->callbacks.gro_complete(skb, nhoff);
293 skb_set_inner_mac_header(skb, nhoff);
299 static struct guehdr *gue_gro_remcsum(struct sk_buff *skb, unsigned int off,
300 struct guehdr *guehdr, void *data,
301 size_t hdrlen, struct gro_remcsum *grc,
305 size_t start = ntohs(pd[0]);
306 size_t offset = ntohs(pd[1]);
308 if (skb->remcsum_offload)
311 if (!NAPI_GRO_CB(skb)->csum_valid)
314 guehdr = skb_gro_remcsum_process(skb, (void *)guehdr, off, hdrlen,
315 start, offset, grc, nopartial);
317 skb->remcsum_offload = 1;
322 static struct sk_buff *gue_gro_receive(struct sock *sk,
323 struct list_head *head,
326 const struct net_offload __rcu **offloads;
327 const struct net_offload *ops;
328 struct sk_buff *pp = NULL;
330 struct guehdr *guehdr;
331 size_t len, optlen, hdrlen, off;
335 struct fou *fou = fou_from_sock(sk);
336 struct gro_remcsum grc;
339 skb_gro_remcsum_init(&grc);
344 off = skb_gro_offset(skb);
345 len = off + sizeof(*guehdr);
347 guehdr = skb_gro_header(skb, len, off);
348 if (unlikely(!guehdr))
351 switch (guehdr->version) {
355 switch (((struct iphdr *)guehdr)->version) {
357 proto = IPPROTO_IPIP;
360 proto = IPPROTO_IPV6;
370 optlen = guehdr->hlen << 2;
373 if (!skb_gro_may_pull(skb, len)) {
374 guehdr = skb_gro_header_slow(skb, len, off);
375 if (unlikely(!guehdr))
379 if (unlikely(guehdr->control) || guehdr->version != 0 ||
380 validate_gue_flags(guehdr, optlen))
383 hdrlen = sizeof(*guehdr) + optlen;
385 /* Adjust NAPI_GRO_CB(skb)->csum to account for guehdr,
386 * this is needed if there is a remote checkcsum offload.
388 skb_gro_postpull_rcsum(skb, guehdr, hdrlen);
392 if (guehdr->flags & GUE_FLAG_PRIV) {
393 __be32 flags = *(__be32 *)(data + doffset);
395 doffset += GUE_LEN_PRIV;
397 if (flags & GUE_PFLAG_REMCSUM) {
398 guehdr = gue_gro_remcsum(skb, off, guehdr,
399 data + doffset, hdrlen, &grc,
401 FOU_F_REMCSUM_NOPARTIAL));
408 doffset += GUE_PLEN_REMCSUM;
412 skb_gro_pull(skb, hdrlen);
414 list_for_each_entry(p, head, list) {
415 const struct guehdr *guehdr2;
417 if (!NAPI_GRO_CB(p)->same_flow)
420 guehdr2 = (struct guehdr *)(p->data + off);
422 /* Compare base GUE header to be equal (covers
423 * hlen, version, proto_ctype, and flags.
425 if (guehdr->word != guehdr2->word) {
426 NAPI_GRO_CB(p)->same_flow = 0;
430 /* Compare optional fields are the same. */
431 if (guehdr->hlen && memcmp(&guehdr[1], &guehdr2[1],
432 guehdr->hlen << 2)) {
433 NAPI_GRO_CB(p)->same_flow = 0;
438 proto = guehdr->proto_ctype;
442 /* We can clear the encap_mark for GUE as we are essentially doing
443 * one of two possible things. We are either adding an L4 tunnel
444 * header to the outer L3 tunnel header, or we are simply
445 * treating the GRE tunnel header as though it is a UDP protocol
446 * specific header such as VXLAN or GENEVE.
448 NAPI_GRO_CB(skb)->encap_mark = 0;
450 /* Flag this frame as already having an outer encap header */
451 NAPI_GRO_CB(skb)->is_fou = 1;
453 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
454 ops = rcu_dereference(offloads[proto]);
455 if (!ops || !ops->callbacks.gro_receive)
458 pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
462 skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
467 static int gue_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
469 struct guehdr *guehdr = (struct guehdr *)(skb->data + nhoff);
470 const struct net_offload __rcu **offloads;
471 const struct net_offload *ops;
472 unsigned int guehlen = 0;
476 switch (guehdr->version) {
478 proto = guehdr->proto_ctype;
479 guehlen = sizeof(*guehdr) + (guehdr->hlen << 2);
482 switch (((struct iphdr *)guehdr)->version) {
484 proto = IPPROTO_IPIP;
487 proto = IPPROTO_IPV6;
497 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
498 ops = rcu_dereference(offloads[proto]);
499 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
502 err = ops->callbacks.gro_complete(skb, nhoff + guehlen);
504 skb_set_inner_mac_header(skb, nhoff + guehlen);
510 static bool fou_cfg_cmp(struct fou *fou, struct fou_cfg *cfg)
512 struct sock *sk = fou->sock->sk;
513 struct udp_port_cfg *udp_cfg = &cfg->udp_config;
515 if (fou->family != udp_cfg->family ||
516 fou->port != udp_cfg->local_udp_port ||
517 sk->sk_dport != udp_cfg->peer_udp_port ||
518 sk->sk_bound_dev_if != udp_cfg->bind_ifindex)
521 if (fou->family == AF_INET) {
522 if (sk->sk_rcv_saddr != udp_cfg->local_ip.s_addr ||
523 sk->sk_daddr != udp_cfg->peer_ip.s_addr)
527 #if IS_ENABLED(CONFIG_IPV6)
529 if (ipv6_addr_cmp(&sk->sk_v6_rcv_saddr, &udp_cfg->local_ip6) ||
530 ipv6_addr_cmp(&sk->sk_v6_daddr, &udp_cfg->peer_ip6))
540 static int fou_add_to_port_list(struct net *net, struct fou *fou,
543 struct fou_net *fn = net_generic(net, fou_net_id);
546 mutex_lock(&fn->fou_lock);
547 list_for_each_entry(fout, &fn->fou_list, list) {
548 if (fou_cfg_cmp(fout, cfg)) {
549 mutex_unlock(&fn->fou_lock);
554 list_add(&fou->list, &fn->fou_list);
555 mutex_unlock(&fn->fou_lock);
560 static void fou_release(struct fou *fou)
562 struct socket *sock = fou->sock;
564 list_del(&fou->list);
565 udp_tunnel_sock_release(sock);
570 static int fou_create(struct net *net, struct fou_cfg *cfg,
571 struct socket **sockp)
573 struct socket *sock = NULL;
574 struct fou *fou = NULL;
576 struct udp_tunnel_sock_cfg tunnel_cfg;
579 /* Open UDP socket */
580 err = udp_sock_create(net, &cfg->udp_config, &sock);
584 /* Allocate FOU port structure */
585 fou = kzalloc(sizeof(*fou), GFP_KERNEL);
593 fou->port = cfg->udp_config.local_udp_port;
594 fou->family = cfg->udp_config.family;
595 fou->flags = cfg->flags;
596 fou->type = cfg->type;
599 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
600 tunnel_cfg.encap_type = 1;
601 tunnel_cfg.sk_user_data = fou;
602 tunnel_cfg.encap_destroy = NULL;
604 /* Initial for fou type */
606 case FOU_ENCAP_DIRECT:
607 tunnel_cfg.encap_rcv = fou_udp_recv;
608 tunnel_cfg.gro_receive = fou_gro_receive;
609 tunnel_cfg.gro_complete = fou_gro_complete;
610 fou->protocol = cfg->protocol;
613 tunnel_cfg.encap_rcv = gue_udp_recv;
614 tunnel_cfg.gro_receive = gue_gro_receive;
615 tunnel_cfg.gro_complete = gue_gro_complete;
622 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
624 sk->sk_allocation = GFP_ATOMIC;
626 err = fou_add_to_port_list(net, fou, cfg);
638 udp_tunnel_sock_release(sock);
643 static int fou_destroy(struct net *net, struct fou_cfg *cfg)
645 struct fou_net *fn = net_generic(net, fou_net_id);
649 mutex_lock(&fn->fou_lock);
650 list_for_each_entry(fou, &fn->fou_list, list) {
651 if (fou_cfg_cmp(fou, cfg)) {
657 mutex_unlock(&fn->fou_lock);
662 static struct genl_family fou_nl_family;
664 static int parse_nl_config(struct genl_info *info,
667 bool has_local = false, has_peer = false;
672 memset(cfg, 0, sizeof(*cfg));
674 cfg->udp_config.family = AF_INET;
676 if (info->attrs[FOU_ATTR_AF]) {
677 u8 family = nla_get_u8(info->attrs[FOU_ATTR_AF]);
683 cfg->udp_config.ipv6_v6only = 1;
686 return -EAFNOSUPPORT;
689 cfg->udp_config.family = family;
692 if (info->attrs[FOU_ATTR_PORT]) {
693 port = nla_get_be16(info->attrs[FOU_ATTR_PORT]);
694 cfg->udp_config.local_udp_port = port;
697 if (info->attrs[FOU_ATTR_IPPROTO])
698 cfg->protocol = nla_get_u8(info->attrs[FOU_ATTR_IPPROTO]);
700 if (info->attrs[FOU_ATTR_TYPE])
701 cfg->type = nla_get_u8(info->attrs[FOU_ATTR_TYPE]);
703 if (info->attrs[FOU_ATTR_REMCSUM_NOPARTIAL])
704 cfg->flags |= FOU_F_REMCSUM_NOPARTIAL;
706 if (cfg->udp_config.family == AF_INET) {
707 if (info->attrs[FOU_ATTR_LOCAL_V4]) {
708 attr = info->attrs[FOU_ATTR_LOCAL_V4];
709 cfg->udp_config.local_ip.s_addr = nla_get_in_addr(attr);
713 if (info->attrs[FOU_ATTR_PEER_V4]) {
714 attr = info->attrs[FOU_ATTR_PEER_V4];
715 cfg->udp_config.peer_ip.s_addr = nla_get_in_addr(attr);
718 #if IS_ENABLED(CONFIG_IPV6)
720 if (info->attrs[FOU_ATTR_LOCAL_V6]) {
721 attr = info->attrs[FOU_ATTR_LOCAL_V6];
722 cfg->udp_config.local_ip6 = nla_get_in6_addr(attr);
726 if (info->attrs[FOU_ATTR_PEER_V6]) {
727 attr = info->attrs[FOU_ATTR_PEER_V6];
728 cfg->udp_config.peer_ip6 = nla_get_in6_addr(attr);
735 if (info->attrs[FOU_ATTR_PEER_PORT]) {
736 port = nla_get_be16(info->attrs[FOU_ATTR_PEER_PORT]);
737 cfg->udp_config.peer_udp_port = port;
743 if (info->attrs[FOU_ATTR_IFINDEX]) {
747 ifindex = nla_get_s32(info->attrs[FOU_ATTR_IFINDEX]);
749 cfg->udp_config.bind_ifindex = ifindex;
755 int fou_nl_add_doit(struct sk_buff *skb, struct genl_info *info)
757 struct net *net = genl_info_net(info);
761 err = parse_nl_config(info, &cfg);
765 return fou_create(net, &cfg, NULL);
768 int fou_nl_del_doit(struct sk_buff *skb, struct genl_info *info)
770 struct net *net = genl_info_net(info);
774 err = parse_nl_config(info, &cfg);
778 return fou_destroy(net, &cfg);
781 static int fou_fill_info(struct fou *fou, struct sk_buff *msg)
783 struct sock *sk = fou->sock->sk;
785 if (nla_put_u8(msg, FOU_ATTR_AF, fou->sock->sk->sk_family) ||
786 nla_put_be16(msg, FOU_ATTR_PORT, fou->port) ||
787 nla_put_be16(msg, FOU_ATTR_PEER_PORT, sk->sk_dport) ||
788 nla_put_u8(msg, FOU_ATTR_IPPROTO, fou->protocol) ||
789 nla_put_u8(msg, FOU_ATTR_TYPE, fou->type) ||
790 nla_put_s32(msg, FOU_ATTR_IFINDEX, sk->sk_bound_dev_if))
793 if (fou->flags & FOU_F_REMCSUM_NOPARTIAL)
794 if (nla_put_flag(msg, FOU_ATTR_REMCSUM_NOPARTIAL))
797 if (fou->sock->sk->sk_family == AF_INET) {
798 if (nla_put_in_addr(msg, FOU_ATTR_LOCAL_V4, sk->sk_rcv_saddr))
801 if (nla_put_in_addr(msg, FOU_ATTR_PEER_V4, sk->sk_daddr))
803 #if IS_ENABLED(CONFIG_IPV6)
805 if (nla_put_in6_addr(msg, FOU_ATTR_LOCAL_V6,
806 &sk->sk_v6_rcv_saddr))
809 if (nla_put_in6_addr(msg, FOU_ATTR_PEER_V6, &sk->sk_v6_daddr))
817 static int fou_dump_info(struct fou *fou, u32 portid, u32 seq,
818 u32 flags, struct sk_buff *skb, u8 cmd)
822 hdr = genlmsg_put(skb, portid, seq, &fou_nl_family, flags, cmd);
826 if (fou_fill_info(fou, skb) < 0)
827 goto nla_put_failure;
829 genlmsg_end(skb, hdr);
833 genlmsg_cancel(skb, hdr);
837 int fou_nl_get_doit(struct sk_buff *skb, struct genl_info *info)
839 struct net *net = genl_info_net(info);
840 struct fou_net *fn = net_generic(net, fou_net_id);
848 ret = parse_nl_config(info, &cfg);
851 port = cfg.udp_config.local_udp_port;
855 family = cfg.udp_config.family;
856 if (family != AF_INET && family != AF_INET6)
859 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
864 mutex_lock(&fn->fou_lock);
865 list_for_each_entry(fout, &fn->fou_list, list) {
866 if (fou_cfg_cmp(fout, &cfg)) {
867 ret = fou_dump_info(fout, info->snd_portid,
868 info->snd_seq, 0, msg,
873 mutex_unlock(&fn->fou_lock);
877 return genlmsg_reply(msg, info);
884 int fou_nl_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
886 struct net *net = sock_net(skb->sk);
887 struct fou_net *fn = net_generic(net, fou_net_id);
891 mutex_lock(&fn->fou_lock);
892 list_for_each_entry(fout, &fn->fou_list, list) {
893 if (idx++ < cb->args[0])
895 ret = fou_dump_info(fout, NETLINK_CB(cb->skb).portid,
896 cb->nlh->nlmsg_seq, NLM_F_MULTI,
901 mutex_unlock(&fn->fou_lock);
907 static struct genl_family fou_nl_family __ro_after_init = {
909 .name = FOU_GENL_NAME,
910 .version = FOU_GENL_VERSION,
911 .maxattr = FOU_ATTR_MAX,
912 .policy = fou_nl_policy,
914 .module = THIS_MODULE,
915 .small_ops = fou_nl_ops,
916 .n_small_ops = ARRAY_SIZE(fou_nl_ops),
917 .resv_start_op = FOU_CMD_GET + 1,
920 size_t fou_encap_hlen(struct ip_tunnel_encap *e)
922 return sizeof(struct udphdr);
924 EXPORT_SYMBOL(fou_encap_hlen);
926 size_t gue_encap_hlen(struct ip_tunnel_encap *e)
929 bool need_priv = false;
931 len = sizeof(struct udphdr) + sizeof(struct guehdr);
933 if (e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) {
934 len += GUE_PLEN_REMCSUM;
938 len += need_priv ? GUE_LEN_PRIV : 0;
942 EXPORT_SYMBOL(gue_encap_hlen);
944 int __fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
945 u8 *protocol, __be16 *sport, int type)
949 err = iptunnel_handle_offloads(skb, type);
953 *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
958 EXPORT_SYMBOL(__fou_build_header);
960 int __gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
961 u8 *protocol, __be16 *sport, int type)
963 struct guehdr *guehdr;
964 size_t hdrlen, optlen = 0;
966 bool need_priv = false;
969 if ((e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) &&
970 skb->ip_summed == CHECKSUM_PARTIAL) {
971 optlen += GUE_PLEN_REMCSUM;
972 type |= SKB_GSO_TUNNEL_REMCSUM;
976 optlen += need_priv ? GUE_LEN_PRIV : 0;
978 err = iptunnel_handle_offloads(skb, type);
982 /* Get source port (based on flow hash) before skb_push */
983 *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
986 hdrlen = sizeof(struct guehdr) + optlen;
988 skb_push(skb, hdrlen);
990 guehdr = (struct guehdr *)skb->data;
994 guehdr->hlen = optlen >> 2;
996 guehdr->proto_ctype = *protocol;
1001 __be32 *flags = data;
1003 guehdr->flags |= GUE_FLAG_PRIV;
1005 data += GUE_LEN_PRIV;
1007 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1008 u16 csum_start = skb_checksum_start_offset(skb);
1011 if (csum_start < hdrlen)
1014 csum_start -= hdrlen;
1015 pd[0] = htons(csum_start);
1016 pd[1] = htons(csum_start + skb->csum_offset);
1018 if (!skb_is_gso(skb)) {
1019 skb->ip_summed = CHECKSUM_NONE;
1020 skb->encapsulation = 0;
1023 *flags |= GUE_PFLAG_REMCSUM;
1024 data += GUE_PLEN_REMCSUM;
1031 EXPORT_SYMBOL(__gue_build_header);
1033 #ifdef CONFIG_NET_FOU_IP_TUNNELS
1035 static void fou_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e,
1036 struct flowi4 *fl4, u8 *protocol, __be16 sport)
1040 skb_push(skb, sizeof(struct udphdr));
1041 skb_reset_transport_header(skb);
1045 uh->dest = e->dport;
1047 uh->len = htons(skb->len);
1048 udp_set_csum(!(e->flags & TUNNEL_ENCAP_FLAG_CSUM), skb,
1049 fl4->saddr, fl4->daddr, skb->len);
1051 *protocol = IPPROTO_UDP;
1054 static int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
1055 u8 *protocol, struct flowi4 *fl4)
1057 int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
1062 err = __fou_build_header(skb, e, protocol, &sport, type);
1066 fou_build_udp(skb, e, fl4, protocol, sport);
1071 static int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
1072 u8 *protocol, struct flowi4 *fl4)
1074 int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
1079 err = __gue_build_header(skb, e, protocol, &sport, type);
1083 fou_build_udp(skb, e, fl4, protocol, sport);
1088 static int gue_err_proto_handler(int proto, struct sk_buff *skb, u32 info)
1090 const struct net_protocol *ipprot = rcu_dereference(inet_protos[proto]);
1092 if (ipprot && ipprot->err_handler) {
1093 if (!ipprot->err_handler(skb, info))
1100 static int gue_err(struct sk_buff *skb, u32 info)
1102 int transport_offset = skb_transport_offset(skb);
1103 struct guehdr *guehdr;
1107 len = sizeof(struct udphdr) + sizeof(struct guehdr);
1108 if (!pskb_may_pull(skb, transport_offset + len))
1111 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
1113 switch (guehdr->version) {
1114 case 0: /* Full GUE header present */
1117 /* Direct encapsulation of IPv4 or IPv6 */
1118 skb_set_transport_header(skb, -(int)sizeof(struct icmphdr));
1120 switch (((struct iphdr *)guehdr)->version) {
1122 ret = gue_err_proto_handler(IPPROTO_IPIP, skb, info);
1124 #if IS_ENABLED(CONFIG_IPV6)
1126 ret = gue_err_proto_handler(IPPROTO_IPV6, skb, info);
1134 default: /* Undefined version */
1138 if (guehdr->control)
1141 optlen = guehdr->hlen << 2;
1143 if (!pskb_may_pull(skb, transport_offset + len + optlen))
1146 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
1147 if (validate_gue_flags(guehdr, optlen))
1150 /* Handling exceptions for direct UDP encapsulation in GUE would lead to
1151 * recursion. Besides, this kind of encapsulation can't even be
1152 * configured currently. Discard this.
1154 if (guehdr->proto_ctype == IPPROTO_UDP ||
1155 guehdr->proto_ctype == IPPROTO_UDPLITE)
1158 skb_set_transport_header(skb, -(int)sizeof(struct icmphdr));
1159 ret = gue_err_proto_handler(guehdr->proto_ctype, skb, info);
1162 skb_set_transport_header(skb, transport_offset);
1167 static const struct ip_tunnel_encap_ops fou_iptun_ops = {
1168 .encap_hlen = fou_encap_hlen,
1169 .build_header = fou_build_header,
1170 .err_handler = gue_err,
1173 static const struct ip_tunnel_encap_ops gue_iptun_ops = {
1174 .encap_hlen = gue_encap_hlen,
1175 .build_header = gue_build_header,
1176 .err_handler = gue_err,
1179 static int ip_tunnel_encap_add_fou_ops(void)
1183 ret = ip_tunnel_encap_add_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
1185 pr_err("can't add fou ops\n");
1189 ret = ip_tunnel_encap_add_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
1191 pr_err("can't add gue ops\n");
1192 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
1199 static void ip_tunnel_encap_del_fou_ops(void)
1201 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
1202 ip_tunnel_encap_del_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
1207 static int ip_tunnel_encap_add_fou_ops(void)
1212 static void ip_tunnel_encap_del_fou_ops(void)
1218 static __net_init int fou_init_net(struct net *net)
1220 struct fou_net *fn = net_generic(net, fou_net_id);
1222 INIT_LIST_HEAD(&fn->fou_list);
1223 mutex_init(&fn->fou_lock);
1227 static __net_exit void fou_exit_net(struct net *net)
1229 struct fou_net *fn = net_generic(net, fou_net_id);
1230 struct fou *fou, *next;
1232 /* Close all the FOU sockets */
1233 mutex_lock(&fn->fou_lock);
1234 list_for_each_entry_safe(fou, next, &fn->fou_list, list)
1236 mutex_unlock(&fn->fou_lock);
1239 static struct pernet_operations fou_net_ops = {
1240 .init = fou_init_net,
1241 .exit = fou_exit_net,
1243 .size = sizeof(struct fou_net),
1246 static int __init fou_init(void)
1250 ret = register_pernet_device(&fou_net_ops);
1254 ret = genl_register_family(&fou_nl_family);
1258 ret = register_fou_bpf();
1262 ret = ip_tunnel_encap_add_fou_ops();
1267 genl_unregister_family(&fou_nl_family);
1269 unregister_pernet_device(&fou_net_ops);
1274 static void __exit fou_fini(void)
1276 ip_tunnel_encap_del_fou_ops();
1277 genl_unregister_family(&fou_nl_family);
1278 unregister_pernet_device(&fou_net_ops);
1281 module_init(fou_init);
1282 module_exit(fou_fini);
1284 MODULE_LICENSE("GPL");
1285 MODULE_DESCRIPTION("Foo over UDP");