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 sk->sk_user_data;
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 u8 proto = fou_from_sock(sk)->protocol;
237 const struct net_offload *ops;
238 struct sk_buff *pp = NULL;
240 /* We can clear the encap_mark for FOU as we are essentially doing
241 * one of two possible things. We are either adding an L4 tunnel
242 * header to the outer L3 tunnel header, or we are simply
243 * treating the GRE tunnel header as though it is a UDP protocol
244 * specific header such as VXLAN or GENEVE.
246 NAPI_GRO_CB(skb)->encap_mark = 0;
248 /* Flag this frame as already having an outer encap header */
249 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);
262 static int fou_gro_complete(struct sock *sk, struct sk_buff *skb,
265 const struct net_offload __rcu **offloads;
266 u8 proto = fou_from_sock(sk)->protocol;
267 const struct net_offload *ops;
270 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
271 ops = rcu_dereference(offloads[proto]);
272 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
275 err = ops->callbacks.gro_complete(skb, nhoff);
277 skb_set_inner_mac_header(skb, nhoff);
283 static struct guehdr *gue_gro_remcsum(struct sk_buff *skb, unsigned int off,
284 struct guehdr *guehdr, void *data,
285 size_t hdrlen, struct gro_remcsum *grc,
289 size_t start = ntohs(pd[0]);
290 size_t offset = ntohs(pd[1]);
292 if (skb->remcsum_offload)
295 if (!NAPI_GRO_CB(skb)->csum_valid)
298 guehdr = skb_gro_remcsum_process(skb, (void *)guehdr, off, hdrlen,
299 start, offset, grc, nopartial);
301 skb->remcsum_offload = 1;
306 static struct sk_buff *gue_gro_receive(struct sock *sk,
307 struct list_head *head,
310 const struct net_offload __rcu **offloads;
311 const struct net_offload *ops;
312 struct sk_buff *pp = NULL;
314 struct guehdr *guehdr;
315 size_t len, optlen, hdrlen, off;
319 struct fou *fou = fou_from_sock(sk);
320 struct gro_remcsum grc;
323 skb_gro_remcsum_init(&grc);
325 off = skb_gro_offset(skb);
326 len = off + sizeof(*guehdr);
328 guehdr = skb_gro_header(skb, len, off);
329 if (unlikely(!guehdr))
332 switch (guehdr->version) {
336 switch (((struct iphdr *)guehdr)->version) {
338 proto = IPPROTO_IPIP;
341 proto = IPPROTO_IPV6;
351 optlen = guehdr->hlen << 2;
354 if (skb_gro_header_hard(skb, len)) {
355 guehdr = skb_gro_header_slow(skb, len, off);
356 if (unlikely(!guehdr))
360 if (unlikely(guehdr->control) || guehdr->version != 0 ||
361 validate_gue_flags(guehdr, optlen))
364 hdrlen = sizeof(*guehdr) + optlen;
366 /* Adjust NAPI_GRO_CB(skb)->csum to account for guehdr,
367 * this is needed if there is a remote checkcsum offload.
369 skb_gro_postpull_rcsum(skb, guehdr, hdrlen);
373 if (guehdr->flags & GUE_FLAG_PRIV) {
374 __be32 flags = *(__be32 *)(data + doffset);
376 doffset += GUE_LEN_PRIV;
378 if (flags & GUE_PFLAG_REMCSUM) {
379 guehdr = gue_gro_remcsum(skb, off, guehdr,
380 data + doffset, hdrlen, &grc,
382 FOU_F_REMCSUM_NOPARTIAL));
389 doffset += GUE_PLEN_REMCSUM;
393 skb_gro_pull(skb, hdrlen);
395 list_for_each_entry(p, head, list) {
396 const struct guehdr *guehdr2;
398 if (!NAPI_GRO_CB(p)->same_flow)
401 guehdr2 = (struct guehdr *)(p->data + off);
403 /* Compare base GUE header to be equal (covers
404 * hlen, version, proto_ctype, and flags.
406 if (guehdr->word != guehdr2->word) {
407 NAPI_GRO_CB(p)->same_flow = 0;
411 /* Compare optional fields are the same. */
412 if (guehdr->hlen && memcmp(&guehdr[1], &guehdr2[1],
413 guehdr->hlen << 2)) {
414 NAPI_GRO_CB(p)->same_flow = 0;
419 proto = guehdr->proto_ctype;
423 /* We can clear the encap_mark for GUE as we are essentially doing
424 * one of two possible things. We are either adding an L4 tunnel
425 * header to the outer L3 tunnel header, or we are simply
426 * treating the GRE tunnel header as though it is a UDP protocol
427 * specific header such as VXLAN or GENEVE.
429 NAPI_GRO_CB(skb)->encap_mark = 0;
431 /* Flag this frame as already having an outer encap header */
432 NAPI_GRO_CB(skb)->is_fou = 1;
434 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
435 ops = rcu_dereference(offloads[proto]);
436 if (WARN_ON_ONCE(!ops || !ops->callbacks.gro_receive))
439 pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
443 skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
448 static int gue_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
450 struct guehdr *guehdr = (struct guehdr *)(skb->data + nhoff);
451 const struct net_offload __rcu **offloads;
452 const struct net_offload *ops;
453 unsigned int guehlen = 0;
457 switch (guehdr->version) {
459 proto = guehdr->proto_ctype;
460 guehlen = sizeof(*guehdr) + (guehdr->hlen << 2);
463 switch (((struct iphdr *)guehdr)->version) {
465 proto = IPPROTO_IPIP;
468 proto = IPPROTO_IPV6;
478 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
479 ops = rcu_dereference(offloads[proto]);
480 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
483 err = ops->callbacks.gro_complete(skb, nhoff + guehlen);
485 skb_set_inner_mac_header(skb, nhoff + guehlen);
491 static bool fou_cfg_cmp(struct fou *fou, struct fou_cfg *cfg)
493 struct sock *sk = fou->sock->sk;
494 struct udp_port_cfg *udp_cfg = &cfg->udp_config;
496 if (fou->family != udp_cfg->family ||
497 fou->port != udp_cfg->local_udp_port ||
498 sk->sk_dport != udp_cfg->peer_udp_port ||
499 sk->sk_bound_dev_if != udp_cfg->bind_ifindex)
502 if (fou->family == AF_INET) {
503 if (sk->sk_rcv_saddr != udp_cfg->local_ip.s_addr ||
504 sk->sk_daddr != udp_cfg->peer_ip.s_addr)
508 #if IS_ENABLED(CONFIG_IPV6)
510 if (ipv6_addr_cmp(&sk->sk_v6_rcv_saddr, &udp_cfg->local_ip6) ||
511 ipv6_addr_cmp(&sk->sk_v6_daddr, &udp_cfg->peer_ip6))
521 static int fou_add_to_port_list(struct net *net, struct fou *fou,
524 struct fou_net *fn = net_generic(net, fou_net_id);
527 mutex_lock(&fn->fou_lock);
528 list_for_each_entry(fout, &fn->fou_list, list) {
529 if (fou_cfg_cmp(fout, cfg)) {
530 mutex_unlock(&fn->fou_lock);
535 list_add(&fou->list, &fn->fou_list);
536 mutex_unlock(&fn->fou_lock);
541 static void fou_release(struct fou *fou)
543 struct socket *sock = fou->sock;
545 list_del(&fou->list);
546 udp_tunnel_sock_release(sock);
551 static int fou_create(struct net *net, struct fou_cfg *cfg,
552 struct socket **sockp)
554 struct socket *sock = NULL;
555 struct fou *fou = NULL;
557 struct udp_tunnel_sock_cfg tunnel_cfg;
560 /* Open UDP socket */
561 err = udp_sock_create(net, &cfg->udp_config, &sock);
565 /* Allocate FOU port structure */
566 fou = kzalloc(sizeof(*fou), GFP_KERNEL);
574 fou->port = cfg->udp_config.local_udp_port;
575 fou->family = cfg->udp_config.family;
576 fou->flags = cfg->flags;
577 fou->type = cfg->type;
580 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
581 tunnel_cfg.encap_type = 1;
582 tunnel_cfg.sk_user_data = fou;
583 tunnel_cfg.encap_destroy = NULL;
585 /* Initial for fou type */
587 case FOU_ENCAP_DIRECT:
588 tunnel_cfg.encap_rcv = fou_udp_recv;
589 tunnel_cfg.gro_receive = fou_gro_receive;
590 tunnel_cfg.gro_complete = fou_gro_complete;
591 fou->protocol = cfg->protocol;
594 tunnel_cfg.encap_rcv = gue_udp_recv;
595 tunnel_cfg.gro_receive = gue_gro_receive;
596 tunnel_cfg.gro_complete = gue_gro_complete;
603 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
605 sk->sk_allocation = GFP_ATOMIC;
607 err = fou_add_to_port_list(net, fou, cfg);
619 udp_tunnel_sock_release(sock);
624 static int fou_destroy(struct net *net, struct fou_cfg *cfg)
626 struct fou_net *fn = net_generic(net, fou_net_id);
630 mutex_lock(&fn->fou_lock);
631 list_for_each_entry(fou, &fn->fou_list, list) {
632 if (fou_cfg_cmp(fou, cfg)) {
638 mutex_unlock(&fn->fou_lock);
643 static struct genl_family fou_nl_family;
645 static int parse_nl_config(struct genl_info *info,
648 bool has_local = false, has_peer = false;
653 memset(cfg, 0, sizeof(*cfg));
655 cfg->udp_config.family = AF_INET;
657 if (info->attrs[FOU_ATTR_AF]) {
658 u8 family = nla_get_u8(info->attrs[FOU_ATTR_AF]);
664 cfg->udp_config.ipv6_v6only = 1;
667 return -EAFNOSUPPORT;
670 cfg->udp_config.family = family;
673 if (info->attrs[FOU_ATTR_PORT]) {
674 port = nla_get_be16(info->attrs[FOU_ATTR_PORT]);
675 cfg->udp_config.local_udp_port = port;
678 if (info->attrs[FOU_ATTR_IPPROTO])
679 cfg->protocol = nla_get_u8(info->attrs[FOU_ATTR_IPPROTO]);
681 if (info->attrs[FOU_ATTR_TYPE])
682 cfg->type = nla_get_u8(info->attrs[FOU_ATTR_TYPE]);
684 if (info->attrs[FOU_ATTR_REMCSUM_NOPARTIAL])
685 cfg->flags |= FOU_F_REMCSUM_NOPARTIAL;
687 if (cfg->udp_config.family == AF_INET) {
688 if (info->attrs[FOU_ATTR_LOCAL_V4]) {
689 attr = info->attrs[FOU_ATTR_LOCAL_V4];
690 cfg->udp_config.local_ip.s_addr = nla_get_in_addr(attr);
694 if (info->attrs[FOU_ATTR_PEER_V4]) {
695 attr = info->attrs[FOU_ATTR_PEER_V4];
696 cfg->udp_config.peer_ip.s_addr = nla_get_in_addr(attr);
699 #if IS_ENABLED(CONFIG_IPV6)
701 if (info->attrs[FOU_ATTR_LOCAL_V6]) {
702 attr = info->attrs[FOU_ATTR_LOCAL_V6];
703 cfg->udp_config.local_ip6 = nla_get_in6_addr(attr);
707 if (info->attrs[FOU_ATTR_PEER_V6]) {
708 attr = info->attrs[FOU_ATTR_PEER_V6];
709 cfg->udp_config.peer_ip6 = nla_get_in6_addr(attr);
716 if (info->attrs[FOU_ATTR_PEER_PORT]) {
717 port = nla_get_be16(info->attrs[FOU_ATTR_PEER_PORT]);
718 cfg->udp_config.peer_udp_port = port;
724 if (info->attrs[FOU_ATTR_IFINDEX]) {
728 ifindex = nla_get_s32(info->attrs[FOU_ATTR_IFINDEX]);
730 cfg->udp_config.bind_ifindex = ifindex;
736 int fou_nl_add_doit(struct sk_buff *skb, struct genl_info *info)
738 struct net *net = genl_info_net(info);
742 err = parse_nl_config(info, &cfg);
746 return fou_create(net, &cfg, NULL);
749 int fou_nl_del_doit(struct sk_buff *skb, struct genl_info *info)
751 struct net *net = genl_info_net(info);
755 err = parse_nl_config(info, &cfg);
759 return fou_destroy(net, &cfg);
762 static int fou_fill_info(struct fou *fou, struct sk_buff *msg)
764 struct sock *sk = fou->sock->sk;
766 if (nla_put_u8(msg, FOU_ATTR_AF, fou->sock->sk->sk_family) ||
767 nla_put_be16(msg, FOU_ATTR_PORT, fou->port) ||
768 nla_put_be16(msg, FOU_ATTR_PEER_PORT, sk->sk_dport) ||
769 nla_put_u8(msg, FOU_ATTR_IPPROTO, fou->protocol) ||
770 nla_put_u8(msg, FOU_ATTR_TYPE, fou->type) ||
771 nla_put_s32(msg, FOU_ATTR_IFINDEX, sk->sk_bound_dev_if))
774 if (fou->flags & FOU_F_REMCSUM_NOPARTIAL)
775 if (nla_put_flag(msg, FOU_ATTR_REMCSUM_NOPARTIAL))
778 if (fou->sock->sk->sk_family == AF_INET) {
779 if (nla_put_in_addr(msg, FOU_ATTR_LOCAL_V4, sk->sk_rcv_saddr))
782 if (nla_put_in_addr(msg, FOU_ATTR_PEER_V4, sk->sk_daddr))
784 #if IS_ENABLED(CONFIG_IPV6)
786 if (nla_put_in6_addr(msg, FOU_ATTR_LOCAL_V6,
787 &sk->sk_v6_rcv_saddr))
790 if (nla_put_in6_addr(msg, FOU_ATTR_PEER_V6, &sk->sk_v6_daddr))
798 static int fou_dump_info(struct fou *fou, u32 portid, u32 seq,
799 u32 flags, struct sk_buff *skb, u8 cmd)
803 hdr = genlmsg_put(skb, portid, seq, &fou_nl_family, flags, cmd);
807 if (fou_fill_info(fou, skb) < 0)
808 goto nla_put_failure;
810 genlmsg_end(skb, hdr);
814 genlmsg_cancel(skb, hdr);
818 int fou_nl_get_doit(struct sk_buff *skb, struct genl_info *info)
820 struct net *net = genl_info_net(info);
821 struct fou_net *fn = net_generic(net, fou_net_id);
829 ret = parse_nl_config(info, &cfg);
832 port = cfg.udp_config.local_udp_port;
836 family = cfg.udp_config.family;
837 if (family != AF_INET && family != AF_INET6)
840 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
845 mutex_lock(&fn->fou_lock);
846 list_for_each_entry(fout, &fn->fou_list, list) {
847 if (fou_cfg_cmp(fout, &cfg)) {
848 ret = fou_dump_info(fout, info->snd_portid,
849 info->snd_seq, 0, msg,
854 mutex_unlock(&fn->fou_lock);
858 return genlmsg_reply(msg, info);
865 int fou_nl_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
867 struct net *net = sock_net(skb->sk);
868 struct fou_net *fn = net_generic(net, fou_net_id);
872 mutex_lock(&fn->fou_lock);
873 list_for_each_entry(fout, &fn->fou_list, list) {
874 if (idx++ < cb->args[0])
876 ret = fou_dump_info(fout, NETLINK_CB(cb->skb).portid,
877 cb->nlh->nlmsg_seq, NLM_F_MULTI,
882 mutex_unlock(&fn->fou_lock);
888 static struct genl_family fou_nl_family __ro_after_init = {
890 .name = FOU_GENL_NAME,
891 .version = FOU_GENL_VERSION,
892 .maxattr = FOU_ATTR_MAX,
893 .policy = fou_nl_policy,
895 .module = THIS_MODULE,
896 .small_ops = fou_nl_ops,
897 .n_small_ops = ARRAY_SIZE(fou_nl_ops),
898 .resv_start_op = FOU_CMD_GET + 1,
901 size_t fou_encap_hlen(struct ip_tunnel_encap *e)
903 return sizeof(struct udphdr);
905 EXPORT_SYMBOL(fou_encap_hlen);
907 size_t gue_encap_hlen(struct ip_tunnel_encap *e)
910 bool need_priv = false;
912 len = sizeof(struct udphdr) + sizeof(struct guehdr);
914 if (e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) {
915 len += GUE_PLEN_REMCSUM;
919 len += need_priv ? GUE_LEN_PRIV : 0;
923 EXPORT_SYMBOL(gue_encap_hlen);
925 int __fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
926 u8 *protocol, __be16 *sport, int type)
930 err = iptunnel_handle_offloads(skb, type);
934 *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
939 EXPORT_SYMBOL(__fou_build_header);
941 int __gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
942 u8 *protocol, __be16 *sport, int type)
944 struct guehdr *guehdr;
945 size_t hdrlen, optlen = 0;
947 bool need_priv = false;
950 if ((e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) &&
951 skb->ip_summed == CHECKSUM_PARTIAL) {
952 optlen += GUE_PLEN_REMCSUM;
953 type |= SKB_GSO_TUNNEL_REMCSUM;
957 optlen += need_priv ? GUE_LEN_PRIV : 0;
959 err = iptunnel_handle_offloads(skb, type);
963 /* Get source port (based on flow hash) before skb_push */
964 *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
967 hdrlen = sizeof(struct guehdr) + optlen;
969 skb_push(skb, hdrlen);
971 guehdr = (struct guehdr *)skb->data;
975 guehdr->hlen = optlen >> 2;
977 guehdr->proto_ctype = *protocol;
982 __be32 *flags = data;
984 guehdr->flags |= GUE_FLAG_PRIV;
986 data += GUE_LEN_PRIV;
988 if (type & SKB_GSO_TUNNEL_REMCSUM) {
989 u16 csum_start = skb_checksum_start_offset(skb);
992 if (csum_start < hdrlen)
995 csum_start -= hdrlen;
996 pd[0] = htons(csum_start);
997 pd[1] = htons(csum_start + skb->csum_offset);
999 if (!skb_is_gso(skb)) {
1000 skb->ip_summed = CHECKSUM_NONE;
1001 skb->encapsulation = 0;
1004 *flags |= GUE_PFLAG_REMCSUM;
1005 data += GUE_PLEN_REMCSUM;
1012 EXPORT_SYMBOL(__gue_build_header);
1014 #ifdef CONFIG_NET_FOU_IP_TUNNELS
1016 static void fou_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e,
1017 struct flowi4 *fl4, u8 *protocol, __be16 sport)
1021 skb_push(skb, sizeof(struct udphdr));
1022 skb_reset_transport_header(skb);
1026 uh->dest = e->dport;
1028 uh->len = htons(skb->len);
1029 udp_set_csum(!(e->flags & TUNNEL_ENCAP_FLAG_CSUM), skb,
1030 fl4->saddr, fl4->daddr, skb->len);
1032 *protocol = IPPROTO_UDP;
1035 static int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
1036 u8 *protocol, struct flowi4 *fl4)
1038 int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
1043 err = __fou_build_header(skb, e, protocol, &sport, type);
1047 fou_build_udp(skb, e, fl4, protocol, sport);
1052 static int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
1053 u8 *protocol, struct flowi4 *fl4)
1055 int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
1060 err = __gue_build_header(skb, e, protocol, &sport, type);
1064 fou_build_udp(skb, e, fl4, protocol, sport);
1069 static int gue_err_proto_handler(int proto, struct sk_buff *skb, u32 info)
1071 const struct net_protocol *ipprot = rcu_dereference(inet_protos[proto]);
1073 if (ipprot && ipprot->err_handler) {
1074 if (!ipprot->err_handler(skb, info))
1081 static int gue_err(struct sk_buff *skb, u32 info)
1083 int transport_offset = skb_transport_offset(skb);
1084 struct guehdr *guehdr;
1088 len = sizeof(struct udphdr) + sizeof(struct guehdr);
1089 if (!pskb_may_pull(skb, transport_offset + len))
1092 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
1094 switch (guehdr->version) {
1095 case 0: /* Full GUE header present */
1098 /* Direct encapsulation of IPv4 or IPv6 */
1099 skb_set_transport_header(skb, -(int)sizeof(struct icmphdr));
1101 switch (((struct iphdr *)guehdr)->version) {
1103 ret = gue_err_proto_handler(IPPROTO_IPIP, skb, info);
1105 #if IS_ENABLED(CONFIG_IPV6)
1107 ret = gue_err_proto_handler(IPPROTO_IPV6, skb, info);
1115 default: /* Undefined version */
1119 if (guehdr->control)
1122 optlen = guehdr->hlen << 2;
1124 if (!pskb_may_pull(skb, transport_offset + len + optlen))
1127 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
1128 if (validate_gue_flags(guehdr, optlen))
1131 /* Handling exceptions for direct UDP encapsulation in GUE would lead to
1132 * recursion. Besides, this kind of encapsulation can't even be
1133 * configured currently. Discard this.
1135 if (guehdr->proto_ctype == IPPROTO_UDP ||
1136 guehdr->proto_ctype == IPPROTO_UDPLITE)
1139 skb_set_transport_header(skb, -(int)sizeof(struct icmphdr));
1140 ret = gue_err_proto_handler(guehdr->proto_ctype, skb, info);
1143 skb_set_transport_header(skb, transport_offset);
1148 static const struct ip_tunnel_encap_ops fou_iptun_ops = {
1149 .encap_hlen = fou_encap_hlen,
1150 .build_header = fou_build_header,
1151 .err_handler = gue_err,
1154 static const struct ip_tunnel_encap_ops gue_iptun_ops = {
1155 .encap_hlen = gue_encap_hlen,
1156 .build_header = gue_build_header,
1157 .err_handler = gue_err,
1160 static int ip_tunnel_encap_add_fou_ops(void)
1164 ret = ip_tunnel_encap_add_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
1166 pr_err("can't add fou ops\n");
1170 ret = ip_tunnel_encap_add_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
1172 pr_err("can't add gue ops\n");
1173 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
1180 static void ip_tunnel_encap_del_fou_ops(void)
1182 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
1183 ip_tunnel_encap_del_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
1188 static int ip_tunnel_encap_add_fou_ops(void)
1193 static void ip_tunnel_encap_del_fou_ops(void)
1199 static __net_init int fou_init_net(struct net *net)
1201 struct fou_net *fn = net_generic(net, fou_net_id);
1203 INIT_LIST_HEAD(&fn->fou_list);
1204 mutex_init(&fn->fou_lock);
1208 static __net_exit void fou_exit_net(struct net *net)
1210 struct fou_net *fn = net_generic(net, fou_net_id);
1211 struct fou *fou, *next;
1213 /* Close all the FOU sockets */
1214 mutex_lock(&fn->fou_lock);
1215 list_for_each_entry_safe(fou, next, &fn->fou_list, list)
1217 mutex_unlock(&fn->fou_lock);
1220 static struct pernet_operations fou_net_ops = {
1221 .init = fou_init_net,
1222 .exit = fou_exit_net,
1224 .size = sizeof(struct fou_net),
1227 static int __init fou_init(void)
1231 ret = register_pernet_device(&fou_net_ops);
1235 ret = genl_register_family(&fou_nl_family);
1239 ret = register_fou_bpf();
1243 ret = ip_tunnel_encap_add_fou_ops();
1248 genl_unregister_family(&fou_nl_family);
1250 unregister_pernet_device(&fou_net_ops);
1255 static void __exit fou_fini(void)
1257 ip_tunnel_encap_del_fou_ops();
1258 genl_unregister_family(&fou_nl_family);
1259 unregister_pernet_device(&fou_net_ops);
1262 module_init(fou_init);
1263 module_exit(fou_fini);
1265 MODULE_LICENSE("GPL");
1266 MODULE_DESCRIPTION("Foo over UDP");