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>
29 struct list_head list;
33 #define FOU_F_REMCSUM_NOPARTIAL BIT(0)
39 struct udp_port_cfg udp_config;
42 static unsigned int fou_net_id;
45 struct list_head fou_list;
46 struct mutex fou_lock;
49 static inline struct fou *fou_from_sock(struct sock *sk)
51 return sk->sk_user_data;
54 static int fou_recv_pull(struct sk_buff *skb, struct fou *fou, size_t len)
56 /* Remove 'len' bytes from the packet (UDP header and
57 * FOU header if present).
59 if (fou->family == AF_INET)
60 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
62 ipv6_hdr(skb)->payload_len =
63 htons(ntohs(ipv6_hdr(skb)->payload_len) - len);
66 skb_postpull_rcsum(skb, udp_hdr(skb), len);
67 skb_reset_transport_header(skb);
68 return iptunnel_pull_offloads(skb);
71 static int fou_udp_recv(struct sock *sk, struct sk_buff *skb)
73 struct fou *fou = fou_from_sock(sk);
78 if (fou_recv_pull(skb, fou, sizeof(struct udphdr)))
81 return -fou->protocol;
88 static struct guehdr *gue_remcsum(struct sk_buff *skb, struct guehdr *guehdr,
89 void *data, size_t hdrlen, u8 ipproto,
93 size_t start = ntohs(pd[0]);
94 size_t offset = ntohs(pd[1]);
95 size_t plen = sizeof(struct udphdr) + hdrlen +
96 max_t(size_t, offset + sizeof(u16), start);
98 if (skb->remcsum_offload)
101 if (!pskb_may_pull(skb, plen))
103 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
105 skb_remcsum_process(skb, (void *)guehdr + hdrlen,
106 start, offset, nopartial);
111 static int gue_control_message(struct sk_buff *skb, struct guehdr *guehdr)
118 static int gue_udp_recv(struct sock *sk, struct sk_buff *skb)
120 struct fou *fou = fou_from_sock(sk);
121 size_t len, optlen, hdrlen;
122 struct guehdr *guehdr;
130 len = sizeof(struct udphdr) + sizeof(struct guehdr);
131 if (!pskb_may_pull(skb, len))
134 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
136 switch (guehdr->version) {
137 case 0: /* Full GUE header present */
141 /* Direct encapsulation of IPv4 or IPv6 */
145 switch (((struct iphdr *)guehdr)->version) {
156 if (fou_recv_pull(skb, fou, sizeof(struct udphdr)))
162 default: /* Undefined version */
166 optlen = guehdr->hlen << 2;
169 if (!pskb_may_pull(skb, len))
172 /* guehdr may change after pull */
173 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
175 if (validate_gue_flags(guehdr, optlen))
178 hdrlen = sizeof(struct guehdr) + optlen;
180 if (fou->family == AF_INET)
181 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
183 ipv6_hdr(skb)->payload_len =
184 htons(ntohs(ipv6_hdr(skb)->payload_len) - len);
186 /* Pull csum through the guehdr now . This can be used if
187 * there is a remote checksum offload.
189 skb_postpull_rcsum(skb, udp_hdr(skb), len);
193 if (guehdr->flags & GUE_FLAG_PRIV) {
194 __be32 flags = *(__be32 *)(data + doffset);
196 doffset += GUE_LEN_PRIV;
198 if (flags & GUE_PFLAG_REMCSUM) {
199 guehdr = gue_remcsum(skb, guehdr, data + doffset,
200 hdrlen, guehdr->proto_ctype,
202 FOU_F_REMCSUM_NOPARTIAL));
208 doffset += GUE_PLEN_REMCSUM;
212 if (unlikely(guehdr->control))
213 return gue_control_message(skb, guehdr);
215 proto_ctype = guehdr->proto_ctype;
216 __skb_pull(skb, sizeof(struct udphdr) + hdrlen);
217 skb_reset_transport_header(skb);
219 if (iptunnel_pull_offloads(skb))
229 static struct sk_buff *fou_gro_receive(struct sock *sk,
230 struct list_head *head,
233 const struct net_offload __rcu **offloads;
234 u8 proto = fou_from_sock(sk)->protocol;
235 const struct net_offload *ops;
236 struct sk_buff *pp = NULL;
238 /* We can clear the encap_mark for FOU as we are essentially doing
239 * one of two possible things. We are either adding an L4 tunnel
240 * header to the outer L3 tunnel header, or we are simply
241 * treating the GRE tunnel header as though it is a UDP protocol
242 * specific header such as VXLAN or GENEVE.
244 NAPI_GRO_CB(skb)->encap_mark = 0;
246 /* Flag this frame as already having an outer encap header */
247 NAPI_GRO_CB(skb)->is_fou = 1;
249 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
250 ops = rcu_dereference(offloads[proto]);
251 if (!ops || !ops->callbacks.gro_receive)
254 pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
260 static int fou_gro_complete(struct sock *sk, struct sk_buff *skb,
263 const struct net_offload __rcu **offloads;
264 u8 proto = fou_from_sock(sk)->protocol;
265 const struct net_offload *ops;
268 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
269 ops = rcu_dereference(offloads[proto]);
270 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
273 err = ops->callbacks.gro_complete(skb, nhoff);
275 skb_set_inner_mac_header(skb, nhoff);
281 static struct guehdr *gue_gro_remcsum(struct sk_buff *skb, unsigned int off,
282 struct guehdr *guehdr, void *data,
283 size_t hdrlen, struct gro_remcsum *grc,
287 size_t start = ntohs(pd[0]);
288 size_t offset = ntohs(pd[1]);
290 if (skb->remcsum_offload)
293 if (!NAPI_GRO_CB(skb)->csum_valid)
296 guehdr = skb_gro_remcsum_process(skb, (void *)guehdr, off, hdrlen,
297 start, offset, grc, nopartial);
299 skb->remcsum_offload = 1;
304 static struct sk_buff *gue_gro_receive(struct sock *sk,
305 struct list_head *head,
308 const struct net_offload __rcu **offloads;
309 const struct net_offload *ops;
310 struct sk_buff *pp = NULL;
312 struct guehdr *guehdr;
313 size_t len, optlen, hdrlen, off;
317 struct fou *fou = fou_from_sock(sk);
318 struct gro_remcsum grc;
321 skb_gro_remcsum_init(&grc);
323 off = skb_gro_offset(skb);
324 len = off + sizeof(*guehdr);
326 guehdr = skb_gro_header(skb, len, off);
327 if (unlikely(!guehdr))
330 switch (guehdr->version) {
334 switch (((struct iphdr *)guehdr)->version) {
336 proto = IPPROTO_IPIP;
339 proto = IPPROTO_IPV6;
349 optlen = guehdr->hlen << 2;
352 if (skb_gro_header_hard(skb, len)) {
353 guehdr = skb_gro_header_slow(skb, len, off);
354 if (unlikely(!guehdr))
358 if (unlikely(guehdr->control) || guehdr->version != 0 ||
359 validate_gue_flags(guehdr, optlen))
362 hdrlen = sizeof(*guehdr) + optlen;
364 /* Adjust NAPI_GRO_CB(skb)->csum to account for guehdr,
365 * this is needed if there is a remote checkcsum offload.
367 skb_gro_postpull_rcsum(skb, guehdr, hdrlen);
371 if (guehdr->flags & GUE_FLAG_PRIV) {
372 __be32 flags = *(__be32 *)(data + doffset);
374 doffset += GUE_LEN_PRIV;
376 if (flags & GUE_PFLAG_REMCSUM) {
377 guehdr = gue_gro_remcsum(skb, off, guehdr,
378 data + doffset, hdrlen, &grc,
380 FOU_F_REMCSUM_NOPARTIAL));
387 doffset += GUE_PLEN_REMCSUM;
391 skb_gro_pull(skb, hdrlen);
393 list_for_each_entry(p, head, list) {
394 const struct guehdr *guehdr2;
396 if (!NAPI_GRO_CB(p)->same_flow)
399 guehdr2 = (struct guehdr *)(p->data + off);
401 /* Compare base GUE header to be equal (covers
402 * hlen, version, proto_ctype, and flags.
404 if (guehdr->word != guehdr2->word) {
405 NAPI_GRO_CB(p)->same_flow = 0;
409 /* Compare optional fields are the same. */
410 if (guehdr->hlen && memcmp(&guehdr[1], &guehdr2[1],
411 guehdr->hlen << 2)) {
412 NAPI_GRO_CB(p)->same_flow = 0;
417 proto = guehdr->proto_ctype;
421 /* We can clear the encap_mark for GUE as we are essentially doing
422 * one of two possible things. We are either adding an L4 tunnel
423 * header to the outer L3 tunnel header, or we are simply
424 * treating the GRE tunnel header as though it is a UDP protocol
425 * specific header such as VXLAN or GENEVE.
427 NAPI_GRO_CB(skb)->encap_mark = 0;
429 /* Flag this frame as already having an outer encap header */
430 NAPI_GRO_CB(skb)->is_fou = 1;
432 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
433 ops = rcu_dereference(offloads[proto]);
434 if (WARN_ON_ONCE(!ops || !ops->callbacks.gro_receive))
437 pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
441 skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
446 static int gue_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
448 struct guehdr *guehdr = (struct guehdr *)(skb->data + nhoff);
449 const struct net_offload __rcu **offloads;
450 const struct net_offload *ops;
451 unsigned int guehlen = 0;
455 switch (guehdr->version) {
457 proto = guehdr->proto_ctype;
458 guehlen = sizeof(*guehdr) + (guehdr->hlen << 2);
461 switch (((struct iphdr *)guehdr)->version) {
463 proto = IPPROTO_IPIP;
466 proto = IPPROTO_IPV6;
476 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
477 ops = rcu_dereference(offloads[proto]);
478 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
481 err = ops->callbacks.gro_complete(skb, nhoff + guehlen);
483 skb_set_inner_mac_header(skb, nhoff + guehlen);
489 static bool fou_cfg_cmp(struct fou *fou, struct fou_cfg *cfg)
491 struct sock *sk = fou->sock->sk;
492 struct udp_port_cfg *udp_cfg = &cfg->udp_config;
494 if (fou->family != udp_cfg->family ||
495 fou->port != udp_cfg->local_udp_port ||
496 sk->sk_dport != udp_cfg->peer_udp_port ||
497 sk->sk_bound_dev_if != udp_cfg->bind_ifindex)
500 if (fou->family == AF_INET) {
501 if (sk->sk_rcv_saddr != udp_cfg->local_ip.s_addr ||
502 sk->sk_daddr != udp_cfg->peer_ip.s_addr)
506 #if IS_ENABLED(CONFIG_IPV6)
508 if (ipv6_addr_cmp(&sk->sk_v6_rcv_saddr, &udp_cfg->local_ip6) ||
509 ipv6_addr_cmp(&sk->sk_v6_daddr, &udp_cfg->peer_ip6))
519 static int fou_add_to_port_list(struct net *net, struct fou *fou,
522 struct fou_net *fn = net_generic(net, fou_net_id);
525 mutex_lock(&fn->fou_lock);
526 list_for_each_entry(fout, &fn->fou_list, list) {
527 if (fou_cfg_cmp(fout, cfg)) {
528 mutex_unlock(&fn->fou_lock);
533 list_add(&fou->list, &fn->fou_list);
534 mutex_unlock(&fn->fou_lock);
539 static void fou_release(struct fou *fou)
541 struct socket *sock = fou->sock;
543 list_del(&fou->list);
544 udp_tunnel_sock_release(sock);
549 static int fou_create(struct net *net, struct fou_cfg *cfg,
550 struct socket **sockp)
552 struct socket *sock = NULL;
553 struct fou *fou = NULL;
555 struct udp_tunnel_sock_cfg tunnel_cfg;
558 /* Open UDP socket */
559 err = udp_sock_create(net, &cfg->udp_config, &sock);
563 /* Allocate FOU port structure */
564 fou = kzalloc(sizeof(*fou), GFP_KERNEL);
572 fou->port = cfg->udp_config.local_udp_port;
573 fou->family = cfg->udp_config.family;
574 fou->flags = cfg->flags;
575 fou->type = cfg->type;
578 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
579 tunnel_cfg.encap_type = 1;
580 tunnel_cfg.sk_user_data = fou;
581 tunnel_cfg.encap_destroy = NULL;
583 /* Initial for fou type */
585 case FOU_ENCAP_DIRECT:
586 tunnel_cfg.encap_rcv = fou_udp_recv;
587 tunnel_cfg.gro_receive = fou_gro_receive;
588 tunnel_cfg.gro_complete = fou_gro_complete;
589 fou->protocol = cfg->protocol;
592 tunnel_cfg.encap_rcv = gue_udp_recv;
593 tunnel_cfg.gro_receive = gue_gro_receive;
594 tunnel_cfg.gro_complete = gue_gro_complete;
601 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
603 sk->sk_allocation = GFP_ATOMIC;
605 err = fou_add_to_port_list(net, fou, cfg);
617 udp_tunnel_sock_release(sock);
622 static int fou_destroy(struct net *net, struct fou_cfg *cfg)
624 struct fou_net *fn = net_generic(net, fou_net_id);
628 mutex_lock(&fn->fou_lock);
629 list_for_each_entry(fou, &fn->fou_list, list) {
630 if (fou_cfg_cmp(fou, cfg)) {
636 mutex_unlock(&fn->fou_lock);
641 static struct genl_family fou_nl_family;
643 static const struct nla_policy fou_nl_policy[FOU_ATTR_MAX + 1] = {
644 [FOU_ATTR_PORT] = { .type = NLA_U16, },
645 [FOU_ATTR_AF] = { .type = NLA_U8, },
646 [FOU_ATTR_IPPROTO] = { .type = NLA_U8, },
647 [FOU_ATTR_TYPE] = { .type = NLA_U8, },
648 [FOU_ATTR_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG, },
649 [FOU_ATTR_LOCAL_V4] = { .type = NLA_U32, },
650 [FOU_ATTR_PEER_V4] = { .type = NLA_U32, },
651 [FOU_ATTR_LOCAL_V6] = { .len = sizeof(struct in6_addr), },
652 [FOU_ATTR_PEER_V6] = { .len = sizeof(struct in6_addr), },
653 [FOU_ATTR_PEER_PORT] = { .type = NLA_U16, },
654 [FOU_ATTR_IFINDEX] = { .type = NLA_S32, },
657 static int parse_nl_config(struct genl_info *info,
660 bool has_local = false, has_peer = false;
665 memset(cfg, 0, sizeof(*cfg));
667 cfg->udp_config.family = AF_INET;
669 if (info->attrs[FOU_ATTR_AF]) {
670 u8 family = nla_get_u8(info->attrs[FOU_ATTR_AF]);
676 cfg->udp_config.ipv6_v6only = 1;
679 return -EAFNOSUPPORT;
682 cfg->udp_config.family = family;
685 if (info->attrs[FOU_ATTR_PORT]) {
686 port = nla_get_be16(info->attrs[FOU_ATTR_PORT]);
687 cfg->udp_config.local_udp_port = port;
690 if (info->attrs[FOU_ATTR_IPPROTO])
691 cfg->protocol = nla_get_u8(info->attrs[FOU_ATTR_IPPROTO]);
693 if (info->attrs[FOU_ATTR_TYPE])
694 cfg->type = nla_get_u8(info->attrs[FOU_ATTR_TYPE]);
696 if (info->attrs[FOU_ATTR_REMCSUM_NOPARTIAL])
697 cfg->flags |= FOU_F_REMCSUM_NOPARTIAL;
699 if (cfg->udp_config.family == AF_INET) {
700 if (info->attrs[FOU_ATTR_LOCAL_V4]) {
701 attr = info->attrs[FOU_ATTR_LOCAL_V4];
702 cfg->udp_config.local_ip.s_addr = nla_get_in_addr(attr);
706 if (info->attrs[FOU_ATTR_PEER_V4]) {
707 attr = info->attrs[FOU_ATTR_PEER_V4];
708 cfg->udp_config.peer_ip.s_addr = nla_get_in_addr(attr);
711 #if IS_ENABLED(CONFIG_IPV6)
713 if (info->attrs[FOU_ATTR_LOCAL_V6]) {
714 attr = info->attrs[FOU_ATTR_LOCAL_V6];
715 cfg->udp_config.local_ip6 = nla_get_in6_addr(attr);
719 if (info->attrs[FOU_ATTR_PEER_V6]) {
720 attr = info->attrs[FOU_ATTR_PEER_V6];
721 cfg->udp_config.peer_ip6 = nla_get_in6_addr(attr);
728 if (info->attrs[FOU_ATTR_PEER_PORT]) {
729 port = nla_get_be16(info->attrs[FOU_ATTR_PEER_PORT]);
730 cfg->udp_config.peer_udp_port = port;
736 if (info->attrs[FOU_ATTR_IFINDEX]) {
740 ifindex = nla_get_s32(info->attrs[FOU_ATTR_IFINDEX]);
742 cfg->udp_config.bind_ifindex = ifindex;
748 static int fou_nl_cmd_add_port(struct sk_buff *skb, struct genl_info *info)
750 struct net *net = genl_info_net(info);
754 err = parse_nl_config(info, &cfg);
758 return fou_create(net, &cfg, NULL);
761 static int fou_nl_cmd_rm_port(struct sk_buff *skb, struct genl_info *info)
763 struct net *net = genl_info_net(info);
767 err = parse_nl_config(info, &cfg);
771 return fou_destroy(net, &cfg);
774 static int fou_fill_info(struct fou *fou, struct sk_buff *msg)
776 struct sock *sk = fou->sock->sk;
778 if (nla_put_u8(msg, FOU_ATTR_AF, fou->sock->sk->sk_family) ||
779 nla_put_be16(msg, FOU_ATTR_PORT, fou->port) ||
780 nla_put_be16(msg, FOU_ATTR_PEER_PORT, sk->sk_dport) ||
781 nla_put_u8(msg, FOU_ATTR_IPPROTO, fou->protocol) ||
782 nla_put_u8(msg, FOU_ATTR_TYPE, fou->type) ||
783 nla_put_s32(msg, FOU_ATTR_IFINDEX, sk->sk_bound_dev_if))
786 if (fou->flags & FOU_F_REMCSUM_NOPARTIAL)
787 if (nla_put_flag(msg, FOU_ATTR_REMCSUM_NOPARTIAL))
790 if (fou->sock->sk->sk_family == AF_INET) {
791 if (nla_put_in_addr(msg, FOU_ATTR_LOCAL_V4, sk->sk_rcv_saddr))
794 if (nla_put_in_addr(msg, FOU_ATTR_PEER_V4, sk->sk_daddr))
796 #if IS_ENABLED(CONFIG_IPV6)
798 if (nla_put_in6_addr(msg, FOU_ATTR_LOCAL_V6,
799 &sk->sk_v6_rcv_saddr))
802 if (nla_put_in6_addr(msg, FOU_ATTR_PEER_V6, &sk->sk_v6_daddr))
810 static int fou_dump_info(struct fou *fou, u32 portid, u32 seq,
811 u32 flags, struct sk_buff *skb, u8 cmd)
815 hdr = genlmsg_put(skb, portid, seq, &fou_nl_family, flags, cmd);
819 if (fou_fill_info(fou, skb) < 0)
820 goto nla_put_failure;
822 genlmsg_end(skb, hdr);
826 genlmsg_cancel(skb, hdr);
830 static int fou_nl_cmd_get_port(struct sk_buff *skb, struct genl_info *info)
832 struct net *net = genl_info_net(info);
833 struct fou_net *fn = net_generic(net, fou_net_id);
841 ret = parse_nl_config(info, &cfg);
844 port = cfg.udp_config.local_udp_port;
848 family = cfg.udp_config.family;
849 if (family != AF_INET && family != AF_INET6)
852 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
857 mutex_lock(&fn->fou_lock);
858 list_for_each_entry(fout, &fn->fou_list, list) {
859 if (fou_cfg_cmp(fout, &cfg)) {
860 ret = fou_dump_info(fout, info->snd_portid,
861 info->snd_seq, 0, msg,
866 mutex_unlock(&fn->fou_lock);
870 return genlmsg_reply(msg, info);
877 static int fou_nl_dump(struct sk_buff *skb, struct netlink_callback *cb)
879 struct net *net = sock_net(skb->sk);
880 struct fou_net *fn = net_generic(net, fou_net_id);
884 mutex_lock(&fn->fou_lock);
885 list_for_each_entry(fout, &fn->fou_list, list) {
886 if (idx++ < cb->args[0])
888 ret = fou_dump_info(fout, NETLINK_CB(cb->skb).portid,
889 cb->nlh->nlmsg_seq, NLM_F_MULTI,
894 mutex_unlock(&fn->fou_lock);
900 static const struct genl_small_ops fou_nl_ops[] = {
903 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
904 .doit = fou_nl_cmd_add_port,
905 .flags = GENL_ADMIN_PERM,
909 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
910 .doit = fou_nl_cmd_rm_port,
911 .flags = GENL_ADMIN_PERM,
915 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
916 .doit = fou_nl_cmd_get_port,
917 .dumpit = fou_nl_dump,
921 static struct genl_family fou_nl_family __ro_after_init = {
923 .name = FOU_GENL_NAME,
924 .version = FOU_GENL_VERSION,
925 .maxattr = FOU_ATTR_MAX,
926 .policy = fou_nl_policy,
928 .module = THIS_MODULE,
929 .small_ops = fou_nl_ops,
930 .n_small_ops = ARRAY_SIZE(fou_nl_ops),
931 .resv_start_op = FOU_CMD_GET + 1,
934 size_t fou_encap_hlen(struct ip_tunnel_encap *e)
936 return sizeof(struct udphdr);
938 EXPORT_SYMBOL(fou_encap_hlen);
940 size_t gue_encap_hlen(struct ip_tunnel_encap *e)
943 bool need_priv = false;
945 len = sizeof(struct udphdr) + sizeof(struct guehdr);
947 if (e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) {
948 len += GUE_PLEN_REMCSUM;
952 len += need_priv ? GUE_LEN_PRIV : 0;
956 EXPORT_SYMBOL(gue_encap_hlen);
958 int __fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
959 u8 *protocol, __be16 *sport, int type)
963 err = iptunnel_handle_offloads(skb, type);
967 *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
972 EXPORT_SYMBOL(__fou_build_header);
974 int __gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
975 u8 *protocol, __be16 *sport, int type)
977 struct guehdr *guehdr;
978 size_t hdrlen, optlen = 0;
980 bool need_priv = false;
983 if ((e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) &&
984 skb->ip_summed == CHECKSUM_PARTIAL) {
985 optlen += GUE_PLEN_REMCSUM;
986 type |= SKB_GSO_TUNNEL_REMCSUM;
990 optlen += need_priv ? GUE_LEN_PRIV : 0;
992 err = iptunnel_handle_offloads(skb, type);
996 /* Get source port (based on flow hash) before skb_push */
997 *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
1000 hdrlen = sizeof(struct guehdr) + optlen;
1002 skb_push(skb, hdrlen);
1004 guehdr = (struct guehdr *)skb->data;
1006 guehdr->control = 0;
1007 guehdr->version = 0;
1008 guehdr->hlen = optlen >> 2;
1010 guehdr->proto_ctype = *protocol;
1015 __be32 *flags = data;
1017 guehdr->flags |= GUE_FLAG_PRIV;
1019 data += GUE_LEN_PRIV;
1021 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1022 u16 csum_start = skb_checksum_start_offset(skb);
1025 if (csum_start < hdrlen)
1028 csum_start -= hdrlen;
1029 pd[0] = htons(csum_start);
1030 pd[1] = htons(csum_start + skb->csum_offset);
1032 if (!skb_is_gso(skb)) {
1033 skb->ip_summed = CHECKSUM_NONE;
1034 skb->encapsulation = 0;
1037 *flags |= GUE_PFLAG_REMCSUM;
1038 data += GUE_PLEN_REMCSUM;
1045 EXPORT_SYMBOL(__gue_build_header);
1047 #ifdef CONFIG_NET_FOU_IP_TUNNELS
1049 static void fou_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e,
1050 struct flowi4 *fl4, u8 *protocol, __be16 sport)
1054 skb_push(skb, sizeof(struct udphdr));
1055 skb_reset_transport_header(skb);
1059 uh->dest = e->dport;
1061 uh->len = htons(skb->len);
1062 udp_set_csum(!(e->flags & TUNNEL_ENCAP_FLAG_CSUM), skb,
1063 fl4->saddr, fl4->daddr, skb->len);
1065 *protocol = IPPROTO_UDP;
1068 static int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
1069 u8 *protocol, struct flowi4 *fl4)
1071 int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
1076 err = __fou_build_header(skb, e, protocol, &sport, type);
1080 fou_build_udp(skb, e, fl4, protocol, sport);
1085 static int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
1086 u8 *protocol, struct flowi4 *fl4)
1088 int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
1093 err = __gue_build_header(skb, e, protocol, &sport, type);
1097 fou_build_udp(skb, e, fl4, protocol, sport);
1102 static int gue_err_proto_handler(int proto, struct sk_buff *skb, u32 info)
1104 const struct net_protocol *ipprot = rcu_dereference(inet_protos[proto]);
1106 if (ipprot && ipprot->err_handler) {
1107 if (!ipprot->err_handler(skb, info))
1114 static int gue_err(struct sk_buff *skb, u32 info)
1116 int transport_offset = skb_transport_offset(skb);
1117 struct guehdr *guehdr;
1121 len = sizeof(struct udphdr) + sizeof(struct guehdr);
1122 if (!pskb_may_pull(skb, transport_offset + len))
1125 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
1127 switch (guehdr->version) {
1128 case 0: /* Full GUE header present */
1131 /* Direct encapsulation of IPv4 or IPv6 */
1132 skb_set_transport_header(skb, -(int)sizeof(struct icmphdr));
1134 switch (((struct iphdr *)guehdr)->version) {
1136 ret = gue_err_proto_handler(IPPROTO_IPIP, skb, info);
1138 #if IS_ENABLED(CONFIG_IPV6)
1140 ret = gue_err_proto_handler(IPPROTO_IPV6, skb, info);
1148 default: /* Undefined version */
1152 if (guehdr->control)
1155 optlen = guehdr->hlen << 2;
1157 if (!pskb_may_pull(skb, transport_offset + len + optlen))
1160 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
1161 if (validate_gue_flags(guehdr, optlen))
1164 /* Handling exceptions for direct UDP encapsulation in GUE would lead to
1165 * recursion. Besides, this kind of encapsulation can't even be
1166 * configured currently. Discard this.
1168 if (guehdr->proto_ctype == IPPROTO_UDP ||
1169 guehdr->proto_ctype == IPPROTO_UDPLITE)
1172 skb_set_transport_header(skb, -(int)sizeof(struct icmphdr));
1173 ret = gue_err_proto_handler(guehdr->proto_ctype, skb, info);
1176 skb_set_transport_header(skb, transport_offset);
1181 static const struct ip_tunnel_encap_ops fou_iptun_ops = {
1182 .encap_hlen = fou_encap_hlen,
1183 .build_header = fou_build_header,
1184 .err_handler = gue_err,
1187 static const struct ip_tunnel_encap_ops gue_iptun_ops = {
1188 .encap_hlen = gue_encap_hlen,
1189 .build_header = gue_build_header,
1190 .err_handler = gue_err,
1193 static int ip_tunnel_encap_add_fou_ops(void)
1197 ret = ip_tunnel_encap_add_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
1199 pr_err("can't add fou ops\n");
1203 ret = ip_tunnel_encap_add_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
1205 pr_err("can't add gue ops\n");
1206 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
1213 static void ip_tunnel_encap_del_fou_ops(void)
1215 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
1216 ip_tunnel_encap_del_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
1221 static int ip_tunnel_encap_add_fou_ops(void)
1226 static void ip_tunnel_encap_del_fou_ops(void)
1232 static __net_init int fou_init_net(struct net *net)
1234 struct fou_net *fn = net_generic(net, fou_net_id);
1236 INIT_LIST_HEAD(&fn->fou_list);
1237 mutex_init(&fn->fou_lock);
1241 static __net_exit void fou_exit_net(struct net *net)
1243 struct fou_net *fn = net_generic(net, fou_net_id);
1244 struct fou *fou, *next;
1246 /* Close all the FOU sockets */
1247 mutex_lock(&fn->fou_lock);
1248 list_for_each_entry_safe(fou, next, &fn->fou_list, list)
1250 mutex_unlock(&fn->fou_lock);
1253 static struct pernet_operations fou_net_ops = {
1254 .init = fou_init_net,
1255 .exit = fou_exit_net,
1257 .size = sizeof(struct fou_net),
1260 static int __init fou_init(void)
1264 ret = register_pernet_device(&fou_net_ops);
1268 ret = genl_register_family(&fou_nl_family);
1272 ret = ip_tunnel_encap_add_fou_ops();
1276 genl_unregister_family(&fou_nl_family);
1278 unregister_pernet_device(&fou_net_ops);
1283 static void __exit fou_fini(void)
1285 ip_tunnel_encap_del_fou_ops();
1286 genl_unregister_family(&fou_nl_family);
1287 unregister_pernet_device(&fou_net_ops);
1290 module_init(fou_init);
1291 module_exit(fou_fini);
1292 MODULE_AUTHOR("Tom Herbert <therbert@google.com>");
1293 MODULE_LICENSE("GPL");
1294 MODULE_DESCRIPTION("Foo over UDP");