1 // SPDX-License-Identifier: GPL-2.0
2 /* Bareudp: UDP tunnel encasulation for different Payload types like
4 * Copyright (c) 2019 Nokia, Inc.
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/etherdevice.h>
13 #include <linux/hash.h>
14 #include <net/dst_metadata.h>
15 #include <net/gro_cells.h>
16 #include <net/rtnetlink.h>
17 #include <net/protocol.h>
18 #include <net/ip6_tunnel.h>
19 #include <net/ip_tunnels.h>
20 #include <net/udp_tunnel.h>
21 #include <net/bareudp.h>
23 #define BAREUDP_BASE_HLEN sizeof(struct udphdr)
24 #define BAREUDP_IPV4_HLEN (sizeof(struct iphdr) + \
25 sizeof(struct udphdr))
26 #define BAREUDP_IPV6_HLEN (sizeof(struct ipv6hdr) + \
27 sizeof(struct udphdr))
29 static bool log_ecn_error = true;
30 module_param(log_ecn_error, bool, 0644);
31 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
33 /* per-network namespace private data for this module */
35 static unsigned int bareudp_net_id;
38 struct list_head bareudp_list;
41 /* Pseudo network device */
43 struct net *net; /* netns for packet i/o */
44 struct net_device *dev; /* netdev for bareudp tunnel */
48 bool multi_proto_mode;
49 struct socket __rcu *sock;
50 struct list_head next; /* bareudp node on namespace list */
51 struct gro_cells gro_cells;
54 static int bareudp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
56 struct metadata_dst *tun_dst = NULL;
57 struct bareudp_dev *bareudp;
58 unsigned short family;
64 bareudp = rcu_dereference_sk_user_data(sk);
68 if (skb->protocol == htons(ETH_P_IP))
73 if (bareudp->ethertype == htons(ETH_P_IP)) {
76 iphdr = (struct iphdr *)(skb->data + BAREUDP_BASE_HLEN);
77 if (iphdr->version == 4) {
78 proto = bareudp->ethertype;
79 } else if (bareudp->multi_proto_mode && (iphdr->version == 6)) {
80 proto = htons(ETH_P_IPV6);
82 bareudp->dev->stats.rx_dropped++;
85 } else if (bareudp->ethertype == htons(ETH_P_MPLS_UC)) {
86 struct iphdr *tunnel_hdr;
88 tunnel_hdr = (struct iphdr *)skb_network_header(skb);
89 if (tunnel_hdr->version == 4) {
90 if (!ipv4_is_multicast(tunnel_hdr->daddr)) {
91 proto = bareudp->ethertype;
92 } else if (bareudp->multi_proto_mode &&
93 ipv4_is_multicast(tunnel_hdr->daddr)) {
94 proto = htons(ETH_P_MPLS_MC);
96 bareudp->dev->stats.rx_dropped++;
101 struct ipv6hdr *tunnel_hdr_v6;
103 tunnel_hdr_v6 = (struct ipv6hdr *)skb_network_header(skb);
105 ipv6_addr_type((struct in6_addr *)&tunnel_hdr_v6->daddr);
106 if (!(addr_type & IPV6_ADDR_MULTICAST)) {
107 proto = bareudp->ethertype;
108 } else if (bareudp->multi_proto_mode &&
109 (addr_type & IPV6_ADDR_MULTICAST)) {
110 proto = htons(ETH_P_MPLS_MC);
112 bareudp->dev->stats.rx_dropped++;
117 proto = bareudp->ethertype;
120 if (iptunnel_pull_header(skb, BAREUDP_BASE_HLEN,
122 !net_eq(bareudp->net,
123 dev_net(bareudp->dev)))) {
124 bareudp->dev->stats.rx_dropped++;
127 tun_dst = udp_tun_rx_dst(skb, family, TUNNEL_KEY, 0, 0);
129 bareudp->dev->stats.rx_dropped++;
132 skb_dst_set(skb, &tun_dst->dst);
133 skb->dev = bareudp->dev;
134 oiph = skb_network_header(skb);
135 skb_reset_network_header(skb);
137 if (!IS_ENABLED(CONFIG_IPV6) || family == AF_INET)
138 err = IP_ECN_decapsulate(oiph, skb);
140 err = IP6_ECN_decapsulate(oiph, skb);
144 if (!IS_ENABLED(CONFIG_IPV6) || family == AF_INET)
145 net_info_ratelimited("non-ECT from %pI4 "
147 &((struct iphdr *)oiph)->saddr,
148 ((struct iphdr *)oiph)->tos);
150 net_info_ratelimited("non-ECT from %pI6\n",
151 &((struct ipv6hdr *)oiph)->saddr);
154 ++bareudp->dev->stats.rx_frame_errors;
155 ++bareudp->dev->stats.rx_errors;
161 err = gro_cells_receive(&bareudp->gro_cells, skb);
162 if (likely(err == NET_RX_SUCCESS))
163 dev_sw_netstats_rx_add(bareudp->dev, len);
167 /* Consume bad packet */
173 static int bareudp_err_lookup(struct sock *sk, struct sk_buff *skb)
178 static int bareudp_init(struct net_device *dev)
180 struct bareudp_dev *bareudp = netdev_priv(dev);
183 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
187 err = gro_cells_init(&bareudp->gro_cells, dev);
189 free_percpu(dev->tstats);
195 static void bareudp_uninit(struct net_device *dev)
197 struct bareudp_dev *bareudp = netdev_priv(dev);
199 gro_cells_destroy(&bareudp->gro_cells);
200 free_percpu(dev->tstats);
203 static struct socket *bareudp_create_sock(struct net *net, __be16 port)
205 struct udp_port_cfg udp_conf;
209 memset(&udp_conf, 0, sizeof(udp_conf));
210 #if IS_ENABLED(CONFIG_IPV6)
211 udp_conf.family = AF_INET6;
213 udp_conf.family = AF_INET;
215 udp_conf.local_udp_port = port;
216 /* Open UDP socket */
217 err = udp_sock_create(net, &udp_conf, &sock);
221 udp_allow_gso(sock->sk);
225 /* Create new listen socket if needed */
226 static int bareudp_socket_create(struct bareudp_dev *bareudp, __be16 port)
228 struct udp_tunnel_sock_cfg tunnel_cfg;
231 sock = bareudp_create_sock(bareudp->net, port);
233 return PTR_ERR(sock);
235 /* Mark socket as an encapsulation socket */
236 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
237 tunnel_cfg.sk_user_data = bareudp;
238 tunnel_cfg.encap_type = 1;
239 tunnel_cfg.encap_rcv = bareudp_udp_encap_recv;
240 tunnel_cfg.encap_err_lookup = bareudp_err_lookup;
241 tunnel_cfg.encap_destroy = NULL;
242 setup_udp_tunnel_sock(bareudp->net, sock, &tunnel_cfg);
244 rcu_assign_pointer(bareudp->sock, sock);
248 static int bareudp_open(struct net_device *dev)
250 struct bareudp_dev *bareudp = netdev_priv(dev);
253 ret = bareudp_socket_create(bareudp, bareudp->port);
257 static void bareudp_sock_release(struct bareudp_dev *bareudp)
261 sock = bareudp->sock;
262 rcu_assign_pointer(bareudp->sock, NULL);
264 udp_tunnel_sock_release(sock);
267 static int bareudp_stop(struct net_device *dev)
269 struct bareudp_dev *bareudp = netdev_priv(dev);
271 bareudp_sock_release(bareudp);
275 static int bareudp_xmit_skb(struct sk_buff *skb, struct net_device *dev,
276 struct bareudp_dev *bareudp,
277 const struct ip_tunnel_info *info)
279 bool xnet = !net_eq(bareudp->net, dev_net(bareudp->dev));
280 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
281 struct socket *sock = rcu_dereference(bareudp->sock);
282 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
283 const struct ip_tunnel_key *key = &info->key;
294 rt = ip_route_output_tunnel(skb, dev, bareudp->net, &saddr, info,
295 IPPROTO_UDP, use_cache);
300 skb_tunnel_check_pmtu(skb, &rt->dst,
301 BAREUDP_IPV4_HLEN + info->options_len, false);
303 sport = udp_flow_src_port(bareudp->net, skb,
304 bareudp->sport_min, USHRT_MAX,
306 tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
308 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
309 skb_scrub_packet(skb, xnet);
312 if (!skb_pull(skb, skb_network_offset(skb)))
315 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len +
316 BAREUDP_BASE_HLEN + info->options_len + sizeof(struct iphdr);
318 err = skb_cow_head(skb, min_headroom);
322 err = udp_tunnel_handle_offloads(skb, udp_sum);
326 skb_set_inner_protocol(skb, bareudp->ethertype);
327 udp_tunnel_xmit_skb(rt, sock->sk, skb, saddr, info->key.u.ipv4.dst,
328 tos, ttl, df, sport, bareudp->port,
329 !net_eq(bareudp->net, dev_net(bareudp->dev)),
330 !(info->key.tun_flags & TUNNEL_CSUM));
334 dst_release(&rt->dst);
338 static int bareudp6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
339 struct bareudp_dev *bareudp,
340 const struct ip_tunnel_info *info)
342 bool xnet = !net_eq(bareudp->net, dev_net(bareudp->dev));
343 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
344 struct socket *sock = rcu_dereference(bareudp->sock);
345 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
346 const struct ip_tunnel_key *key = &info->key;
347 struct dst_entry *dst = NULL;
348 struct in6_addr saddr, daddr;
357 dst = ip6_dst_lookup_tunnel(skb, dev, bareudp->net, sock, &saddr, info,
358 IPPROTO_UDP, use_cache);
362 skb_tunnel_check_pmtu(skb, dst, BAREUDP_IPV6_HLEN + info->options_len,
365 sport = udp_flow_src_port(bareudp->net, skb,
366 bareudp->sport_min, USHRT_MAX,
368 prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
371 skb_scrub_packet(skb, xnet);
374 if (!skb_pull(skb, skb_network_offset(skb)))
377 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len +
378 BAREUDP_BASE_HLEN + info->options_len + sizeof(struct ipv6hdr);
380 err = skb_cow_head(skb, min_headroom);
384 err = udp_tunnel_handle_offloads(skb, udp_sum);
388 daddr = info->key.u.ipv6.dst;
389 udp_tunnel6_xmit_skb(dst, sock->sk, skb, dev,
390 &saddr, &daddr, prio, ttl,
391 info->key.label, sport, bareudp->port,
392 !(info->key.tun_flags & TUNNEL_CSUM));
400 static bool bareudp_proto_valid(struct bareudp_dev *bareudp, __be16 proto)
402 if (bareudp->ethertype == proto)
405 if (!bareudp->multi_proto_mode)
408 if (bareudp->ethertype == htons(ETH_P_MPLS_UC) &&
409 proto == htons(ETH_P_MPLS_MC))
412 if (bareudp->ethertype == htons(ETH_P_IP) &&
413 proto == htons(ETH_P_IPV6))
419 static netdev_tx_t bareudp_xmit(struct sk_buff *skb, struct net_device *dev)
421 struct bareudp_dev *bareudp = netdev_priv(dev);
422 struct ip_tunnel_info *info = NULL;
425 if (!bareudp_proto_valid(bareudp, skb->protocol)) {
430 info = skb_tunnel_info(skb);
431 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
437 if (IS_ENABLED(CONFIG_IPV6) && info->mode & IP_TUNNEL_INFO_IPV6)
438 err = bareudp6_xmit_skb(skb, dev, bareudp, info);
440 err = bareudp_xmit_skb(skb, dev, bareudp, info);
450 dev->stats.collisions++;
451 else if (err == -ENETUNREACH)
452 dev->stats.tx_carrier_errors++;
454 dev->stats.tx_errors++;
458 static int bareudp_fill_metadata_dst(struct net_device *dev,
461 struct ip_tunnel_info *info = skb_tunnel_info(skb);
462 struct bareudp_dev *bareudp = netdev_priv(dev);
465 use_cache = ip_tunnel_dst_cache_usable(skb, info);
467 if (!IS_ENABLED(CONFIG_IPV6) || ip_tunnel_info_af(info) == AF_INET) {
471 rt = ip_route_output_tunnel(skb, dev, bareudp->net, &saddr,
472 info, IPPROTO_UDP, use_cache);
477 info->key.u.ipv4.src = saddr;
478 } else if (ip_tunnel_info_af(info) == AF_INET6) {
479 struct dst_entry *dst;
480 struct in6_addr saddr;
481 struct socket *sock = rcu_dereference(bareudp->sock);
483 dst = ip6_dst_lookup_tunnel(skb, dev, bareudp->net, sock,
484 &saddr, info, IPPROTO_UDP,
490 info->key.u.ipv6.src = saddr;
495 info->key.tp_src = udp_flow_src_port(bareudp->net, skb,
498 info->key.tp_dst = bareudp->port;
502 static const struct net_device_ops bareudp_netdev_ops = {
503 .ndo_init = bareudp_init,
504 .ndo_uninit = bareudp_uninit,
505 .ndo_open = bareudp_open,
506 .ndo_stop = bareudp_stop,
507 .ndo_start_xmit = bareudp_xmit,
508 .ndo_get_stats64 = dev_get_tstats64,
509 .ndo_fill_metadata_dst = bareudp_fill_metadata_dst,
512 static const struct nla_policy bareudp_policy[IFLA_BAREUDP_MAX + 1] = {
513 [IFLA_BAREUDP_PORT] = { .type = NLA_U16 },
514 [IFLA_BAREUDP_ETHERTYPE] = { .type = NLA_U16 },
515 [IFLA_BAREUDP_SRCPORT_MIN] = { .type = NLA_U16 },
516 [IFLA_BAREUDP_MULTIPROTO_MODE] = { .type = NLA_FLAG },
519 /* Info for udev, that this is a virtual tunnel endpoint */
520 static const struct device_type bareudp_type = {
524 /* Initialize the device structure. */
525 static void bareudp_setup(struct net_device *dev)
527 dev->netdev_ops = &bareudp_netdev_ops;
528 dev->needs_free_netdev = true;
529 SET_NETDEV_DEVTYPE(dev, &bareudp_type);
530 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
531 dev->features |= NETIF_F_RXCSUM;
532 dev->features |= NETIF_F_LLTX;
533 dev->features |= NETIF_F_GSO_SOFTWARE;
534 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
535 dev->hw_features |= NETIF_F_RXCSUM;
536 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
537 dev->hard_header_len = 0;
539 dev->mtu = ETH_DATA_LEN;
540 dev->min_mtu = IPV4_MIN_MTU;
541 dev->max_mtu = IP_MAX_MTU - BAREUDP_BASE_HLEN;
542 dev->type = ARPHRD_NONE;
544 dev->priv_flags |= IFF_NO_QUEUE;
545 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
548 static int bareudp_validate(struct nlattr *tb[], struct nlattr *data[],
549 struct netlink_ext_ack *extack)
552 NL_SET_ERR_MSG(extack,
553 "Not enough attributes provided to perform the operation");
559 static int bareudp2info(struct nlattr *data[], struct bareudp_conf *conf,
560 struct netlink_ext_ack *extack)
562 memset(conf, 0, sizeof(*conf));
564 if (!data[IFLA_BAREUDP_PORT]) {
565 NL_SET_ERR_MSG(extack, "port not specified");
568 if (!data[IFLA_BAREUDP_ETHERTYPE]) {
569 NL_SET_ERR_MSG(extack, "ethertype not specified");
573 if (data[IFLA_BAREUDP_PORT])
574 conf->port = nla_get_u16(data[IFLA_BAREUDP_PORT]);
576 if (data[IFLA_BAREUDP_ETHERTYPE])
577 conf->ethertype = nla_get_u16(data[IFLA_BAREUDP_ETHERTYPE]);
579 if (data[IFLA_BAREUDP_SRCPORT_MIN])
580 conf->sport_min = nla_get_u16(data[IFLA_BAREUDP_SRCPORT_MIN]);
582 if (data[IFLA_BAREUDP_MULTIPROTO_MODE])
583 conf->multi_proto_mode = true;
588 static struct bareudp_dev *bareudp_find_dev(struct bareudp_net *bn,
589 const struct bareudp_conf *conf)
591 struct bareudp_dev *bareudp, *t = NULL;
593 list_for_each_entry(bareudp, &bn->bareudp_list, next) {
594 if (conf->port == bareudp->port)
600 static int bareudp_configure(struct net *net, struct net_device *dev,
601 struct bareudp_conf *conf)
603 struct bareudp_net *bn = net_generic(net, bareudp_net_id);
604 struct bareudp_dev *t, *bareudp = netdev_priv(dev);
609 t = bareudp_find_dev(bn, conf);
613 if (conf->multi_proto_mode &&
614 (conf->ethertype != htons(ETH_P_MPLS_UC) &&
615 conf->ethertype != htons(ETH_P_IP)))
618 bareudp->port = conf->port;
619 bareudp->ethertype = conf->ethertype;
620 bareudp->sport_min = conf->sport_min;
621 bareudp->multi_proto_mode = conf->multi_proto_mode;
623 err = register_netdevice(dev);
627 list_add(&bareudp->next, &bn->bareudp_list);
631 static int bareudp_link_config(struct net_device *dev,
637 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
644 static void bareudp_dellink(struct net_device *dev, struct list_head *head)
646 struct bareudp_dev *bareudp = netdev_priv(dev);
648 list_del(&bareudp->next);
649 unregister_netdevice_queue(dev, head);
652 static int bareudp_newlink(struct net *net, struct net_device *dev,
653 struct nlattr *tb[], struct nlattr *data[],
654 struct netlink_ext_ack *extack)
656 struct bareudp_conf conf;
659 err = bareudp2info(data, &conf, extack);
663 err = bareudp_configure(net, dev, &conf);
667 err = bareudp_link_config(dev, tb);
674 bareudp_dellink(dev, NULL);
678 static size_t bareudp_get_size(const struct net_device *dev)
680 return nla_total_size(sizeof(__be16)) + /* IFLA_BAREUDP_PORT */
681 nla_total_size(sizeof(__be16)) + /* IFLA_BAREUDP_ETHERTYPE */
682 nla_total_size(sizeof(__u16)) + /* IFLA_BAREUDP_SRCPORT_MIN */
683 nla_total_size(0) + /* IFLA_BAREUDP_MULTIPROTO_MODE */
687 static int bareudp_fill_info(struct sk_buff *skb, const struct net_device *dev)
689 struct bareudp_dev *bareudp = netdev_priv(dev);
691 if (nla_put_be16(skb, IFLA_BAREUDP_PORT, bareudp->port))
692 goto nla_put_failure;
693 if (nla_put_be16(skb, IFLA_BAREUDP_ETHERTYPE, bareudp->ethertype))
694 goto nla_put_failure;
695 if (nla_put_u16(skb, IFLA_BAREUDP_SRCPORT_MIN, bareudp->sport_min))
696 goto nla_put_failure;
697 if (bareudp->multi_proto_mode &&
698 nla_put_flag(skb, IFLA_BAREUDP_MULTIPROTO_MODE))
699 goto nla_put_failure;
707 static struct rtnl_link_ops bareudp_link_ops __read_mostly = {
709 .maxtype = IFLA_BAREUDP_MAX,
710 .policy = bareudp_policy,
711 .priv_size = sizeof(struct bareudp_dev),
712 .setup = bareudp_setup,
713 .validate = bareudp_validate,
714 .newlink = bareudp_newlink,
715 .dellink = bareudp_dellink,
716 .get_size = bareudp_get_size,
717 .fill_info = bareudp_fill_info,
720 struct net_device *bareudp_dev_create(struct net *net, const char *name,
722 struct bareudp_conf *conf)
724 struct nlattr *tb[IFLA_MAX + 1];
725 struct net_device *dev;
728 memset(tb, 0, sizeof(tb));
729 dev = rtnl_create_link(net, name, name_assign_type,
730 &bareudp_link_ops, tb, NULL);
734 err = bareudp_configure(net, dev, conf);
739 err = dev_set_mtu(dev, IP_MAX_MTU - BAREUDP_BASE_HLEN);
743 err = rtnl_configure_link(dev, NULL);
749 bareudp_dellink(dev, NULL);
752 EXPORT_SYMBOL_GPL(bareudp_dev_create);
754 static __net_init int bareudp_init_net(struct net *net)
756 struct bareudp_net *bn = net_generic(net, bareudp_net_id);
758 INIT_LIST_HEAD(&bn->bareudp_list);
762 static void bareudp_destroy_tunnels(struct net *net, struct list_head *head)
764 struct bareudp_net *bn = net_generic(net, bareudp_net_id);
765 struct bareudp_dev *bareudp, *next;
767 list_for_each_entry_safe(bareudp, next, &bn->bareudp_list, next)
768 unregister_netdevice_queue(bareudp->dev, head);
771 static void __net_exit bareudp_exit_batch_net(struct list_head *net_list)
777 list_for_each_entry(net, net_list, exit_list)
778 bareudp_destroy_tunnels(net, &list);
780 /* unregister the devices gathered above */
781 unregister_netdevice_many(&list);
785 static struct pernet_operations bareudp_net_ops = {
786 .init = bareudp_init_net,
787 .exit_batch = bareudp_exit_batch_net,
788 .id = &bareudp_net_id,
789 .size = sizeof(struct bareudp_net),
792 static int __init bareudp_init_module(void)
796 rc = register_pernet_subsys(&bareudp_net_ops);
800 rc = rtnl_link_register(&bareudp_link_ops);
806 unregister_pernet_subsys(&bareudp_net_ops);
810 late_initcall(bareudp_init_module);
812 static void __exit bareudp_cleanup_module(void)
814 rtnl_link_unregister(&bareudp_link_ops);
815 unregister_pernet_subsys(&bareudp_net_ops);
817 module_exit(bareudp_cleanup_module);
819 MODULE_ALIAS_RTNL_LINK("bareudp");
820 MODULE_LICENSE("GPL");
822 MODULE_DESCRIPTION("Interface driver for UDP encapsulated traffic");