1 // SPDX-License-Identifier: GPL-2.0
2 /* Bareudp: UDP tunnel encasulation for different Payload types like
4 * Copyright (c) 2019 Nokia, Inc.
5 * Authors: Martin Varghese, <martin.varghese@nokia.com>
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;
45 bool multi_proto_mode;
48 /* Pseudo network device */
50 struct net *net; /* netns for packet i/o */
51 struct net_device *dev; /* netdev for bareudp tunnel */
55 bool multi_proto_mode;
56 struct socket __rcu *sock;
57 struct list_head next; /* bareudp node on namespace list */
58 struct gro_cells gro_cells;
61 static int bareudp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
63 struct metadata_dst *tun_dst = NULL;
64 struct bareudp_dev *bareudp;
65 unsigned short family;
71 bareudp = rcu_dereference_sk_user_data(sk);
75 if (skb->protocol == htons(ETH_P_IP))
80 if (bareudp->ethertype == htons(ETH_P_IP)) {
83 if (skb_copy_bits(skb, BAREUDP_BASE_HLEN, &ipversion,
85 bareudp->dev->stats.rx_dropped++;
91 proto = htons(ETH_P_IP);
92 } else if (ipversion == 6 && bareudp->multi_proto_mode) {
93 proto = htons(ETH_P_IPV6);
95 bareudp->dev->stats.rx_dropped++;
98 } else if (bareudp->ethertype == htons(ETH_P_MPLS_UC)) {
99 struct iphdr *tunnel_hdr;
101 tunnel_hdr = (struct iphdr *)skb_network_header(skb);
102 if (tunnel_hdr->version == 4) {
103 if (!ipv4_is_multicast(tunnel_hdr->daddr)) {
104 proto = bareudp->ethertype;
105 } else if (bareudp->multi_proto_mode &&
106 ipv4_is_multicast(tunnel_hdr->daddr)) {
107 proto = htons(ETH_P_MPLS_MC);
109 bareudp->dev->stats.rx_dropped++;
114 struct ipv6hdr *tunnel_hdr_v6;
116 tunnel_hdr_v6 = (struct ipv6hdr *)skb_network_header(skb);
118 ipv6_addr_type((struct in6_addr *)&tunnel_hdr_v6->daddr);
119 if (!(addr_type & IPV6_ADDR_MULTICAST)) {
120 proto = bareudp->ethertype;
121 } else if (bareudp->multi_proto_mode &&
122 (addr_type & IPV6_ADDR_MULTICAST)) {
123 proto = htons(ETH_P_MPLS_MC);
125 bareudp->dev->stats.rx_dropped++;
130 proto = bareudp->ethertype;
133 if (iptunnel_pull_header(skb, BAREUDP_BASE_HLEN,
135 !net_eq(bareudp->net,
136 dev_net(bareudp->dev)))) {
137 bareudp->dev->stats.rx_dropped++;
140 tun_dst = udp_tun_rx_dst(skb, family, TUNNEL_KEY, 0, 0);
142 bareudp->dev->stats.rx_dropped++;
145 skb_dst_set(skb, &tun_dst->dst);
146 skb->dev = bareudp->dev;
147 oiph = skb_network_header(skb);
148 skb_reset_network_header(skb);
149 skb_reset_mac_header(skb);
151 if (!ipv6_mod_enabled() || family == AF_INET)
152 err = IP_ECN_decapsulate(oiph, skb);
154 err = IP6_ECN_decapsulate(oiph, skb);
158 if (!ipv6_mod_enabled() || family == AF_INET)
159 net_info_ratelimited("non-ECT from %pI4 "
161 &((struct iphdr *)oiph)->saddr,
162 ((struct iphdr *)oiph)->tos);
164 net_info_ratelimited("non-ECT from %pI6\n",
165 &((struct ipv6hdr *)oiph)->saddr);
168 ++bareudp->dev->stats.rx_frame_errors;
169 ++bareudp->dev->stats.rx_errors;
175 err = gro_cells_receive(&bareudp->gro_cells, skb);
176 if (likely(err == NET_RX_SUCCESS))
177 dev_sw_netstats_rx_add(bareudp->dev, len);
181 /* Consume bad packet */
187 static int bareudp_err_lookup(struct sock *sk, struct sk_buff *skb)
192 static int bareudp_init(struct net_device *dev)
194 struct bareudp_dev *bareudp = netdev_priv(dev);
197 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
201 err = gro_cells_init(&bareudp->gro_cells, dev);
203 free_percpu(dev->tstats);
209 static void bareudp_uninit(struct net_device *dev)
211 struct bareudp_dev *bareudp = netdev_priv(dev);
213 gro_cells_destroy(&bareudp->gro_cells);
214 free_percpu(dev->tstats);
217 static struct socket *bareudp_create_sock(struct net *net, __be16 port)
219 struct udp_port_cfg udp_conf;
223 memset(&udp_conf, 0, sizeof(udp_conf));
225 if (ipv6_mod_enabled())
226 udp_conf.family = AF_INET6;
228 udp_conf.family = AF_INET;
230 udp_conf.local_udp_port = port;
231 /* Open UDP socket */
232 err = udp_sock_create(net, &udp_conf, &sock);
236 udp_allow_gso(sock->sk);
240 /* Create new listen socket if needed */
241 static int bareudp_socket_create(struct bareudp_dev *bareudp, __be16 port)
243 struct udp_tunnel_sock_cfg tunnel_cfg;
246 sock = bareudp_create_sock(bareudp->net, port);
248 return PTR_ERR(sock);
250 /* Mark socket as an encapsulation socket */
251 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
252 tunnel_cfg.sk_user_data = bareudp;
253 tunnel_cfg.encap_type = 1;
254 tunnel_cfg.encap_rcv = bareudp_udp_encap_recv;
255 tunnel_cfg.encap_err_lookup = bareudp_err_lookup;
256 tunnel_cfg.encap_destroy = NULL;
257 setup_udp_tunnel_sock(bareudp->net, sock, &tunnel_cfg);
259 rcu_assign_pointer(bareudp->sock, sock);
263 static int bareudp_open(struct net_device *dev)
265 struct bareudp_dev *bareudp = netdev_priv(dev);
268 ret = bareudp_socket_create(bareudp, bareudp->port);
272 static void bareudp_sock_release(struct bareudp_dev *bareudp)
276 sock = bareudp->sock;
277 rcu_assign_pointer(bareudp->sock, NULL);
279 udp_tunnel_sock_release(sock);
282 static int bareudp_stop(struct net_device *dev)
284 struct bareudp_dev *bareudp = netdev_priv(dev);
286 bareudp_sock_release(bareudp);
290 static int bareudp_xmit_skb(struct sk_buff *skb, struct net_device *dev,
291 struct bareudp_dev *bareudp,
292 const struct ip_tunnel_info *info)
294 bool xnet = !net_eq(bareudp->net, dev_net(bareudp->dev));
295 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
296 struct socket *sock = rcu_dereference(bareudp->sock);
297 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
298 const struct ip_tunnel_key *key = &info->key;
309 rt = ip_route_output_tunnel(skb, dev, bareudp->net, &saddr, info,
310 IPPROTO_UDP, use_cache);
315 skb_tunnel_check_pmtu(skb, &rt->dst,
316 BAREUDP_IPV4_HLEN + info->options_len, false);
318 sport = udp_flow_src_port(bareudp->net, skb,
319 bareudp->sport_min, USHRT_MAX,
321 tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
323 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
324 skb_scrub_packet(skb, xnet);
327 if (!skb_pull(skb, skb_network_offset(skb)))
330 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len +
331 BAREUDP_BASE_HLEN + info->options_len + sizeof(struct iphdr);
333 err = skb_cow_head(skb, min_headroom);
337 err = udp_tunnel_handle_offloads(skb, udp_sum);
341 skb_set_inner_protocol(skb, bareudp->ethertype);
342 udp_tunnel_xmit_skb(rt, sock->sk, skb, saddr, info->key.u.ipv4.dst,
343 tos, ttl, df, sport, bareudp->port,
344 !net_eq(bareudp->net, dev_net(bareudp->dev)),
345 !(info->key.tun_flags & TUNNEL_CSUM));
349 dst_release(&rt->dst);
353 static int bareudp6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
354 struct bareudp_dev *bareudp,
355 const struct ip_tunnel_info *info)
357 bool xnet = !net_eq(bareudp->net, dev_net(bareudp->dev));
358 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
359 struct socket *sock = rcu_dereference(bareudp->sock);
360 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
361 const struct ip_tunnel_key *key = &info->key;
362 struct dst_entry *dst = NULL;
363 struct in6_addr saddr, daddr;
372 dst = ip6_dst_lookup_tunnel(skb, dev, bareudp->net, sock, &saddr, info,
373 IPPROTO_UDP, use_cache);
377 skb_tunnel_check_pmtu(skb, dst, BAREUDP_IPV6_HLEN + info->options_len,
380 sport = udp_flow_src_port(bareudp->net, skb,
381 bareudp->sport_min, USHRT_MAX,
383 prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
386 skb_scrub_packet(skb, xnet);
389 if (!skb_pull(skb, skb_network_offset(skb)))
392 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len +
393 BAREUDP_BASE_HLEN + info->options_len + sizeof(struct ipv6hdr);
395 err = skb_cow_head(skb, min_headroom);
399 err = udp_tunnel_handle_offloads(skb, udp_sum);
403 daddr = info->key.u.ipv6.dst;
404 udp_tunnel6_xmit_skb(dst, sock->sk, skb, dev,
405 &saddr, &daddr, prio, ttl,
406 info->key.label, sport, bareudp->port,
407 !(info->key.tun_flags & TUNNEL_CSUM));
415 static bool bareudp_proto_valid(struct bareudp_dev *bareudp, __be16 proto)
417 if (bareudp->ethertype == proto)
420 if (!bareudp->multi_proto_mode)
423 if (bareudp->ethertype == htons(ETH_P_MPLS_UC) &&
424 proto == htons(ETH_P_MPLS_MC))
427 if (bareudp->ethertype == htons(ETH_P_IP) &&
428 proto == htons(ETH_P_IPV6))
434 static netdev_tx_t bareudp_xmit(struct sk_buff *skb, struct net_device *dev)
436 struct bareudp_dev *bareudp = netdev_priv(dev);
437 struct ip_tunnel_info *info = NULL;
440 if (!bareudp_proto_valid(bareudp, skb->protocol)) {
445 info = skb_tunnel_info(skb);
446 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
452 if (ipv6_mod_enabled() && info->mode & IP_TUNNEL_INFO_IPV6)
453 err = bareudp6_xmit_skb(skb, dev, bareudp, info);
455 err = bareudp_xmit_skb(skb, dev, bareudp, info);
465 dev->stats.collisions++;
466 else if (err == -ENETUNREACH)
467 dev->stats.tx_carrier_errors++;
469 dev->stats.tx_errors++;
473 static int bareudp_fill_metadata_dst(struct net_device *dev,
476 struct ip_tunnel_info *info = skb_tunnel_info(skb);
477 struct bareudp_dev *bareudp = netdev_priv(dev);
480 use_cache = ip_tunnel_dst_cache_usable(skb, info);
482 if (!ipv6_mod_enabled() || ip_tunnel_info_af(info) == AF_INET) {
486 rt = ip_route_output_tunnel(skb, dev, bareudp->net, &saddr,
487 info, IPPROTO_UDP, use_cache);
492 info->key.u.ipv4.src = saddr;
493 } else if (ip_tunnel_info_af(info) == AF_INET6) {
494 struct dst_entry *dst;
495 struct in6_addr saddr;
496 struct socket *sock = rcu_dereference(bareudp->sock);
498 dst = ip6_dst_lookup_tunnel(skb, dev, bareudp->net, sock,
499 &saddr, info, IPPROTO_UDP,
505 info->key.u.ipv6.src = saddr;
510 info->key.tp_src = udp_flow_src_port(bareudp->net, skb,
513 info->key.tp_dst = bareudp->port;
517 static const struct net_device_ops bareudp_netdev_ops = {
518 .ndo_init = bareudp_init,
519 .ndo_uninit = bareudp_uninit,
520 .ndo_open = bareudp_open,
521 .ndo_stop = bareudp_stop,
522 .ndo_start_xmit = bareudp_xmit,
523 .ndo_get_stats64 = dev_get_tstats64,
524 .ndo_fill_metadata_dst = bareudp_fill_metadata_dst,
527 static const struct nla_policy bareudp_policy[IFLA_BAREUDP_MAX + 1] = {
528 [IFLA_BAREUDP_PORT] = { .type = NLA_U16 },
529 [IFLA_BAREUDP_ETHERTYPE] = { .type = NLA_U16 },
530 [IFLA_BAREUDP_SRCPORT_MIN] = { .type = NLA_U16 },
531 [IFLA_BAREUDP_MULTIPROTO_MODE] = { .type = NLA_FLAG },
534 /* Info for udev, that this is a virtual tunnel endpoint */
535 static const struct device_type bareudp_type = {
539 /* Initialize the device structure. */
540 static void bareudp_setup(struct net_device *dev)
542 dev->netdev_ops = &bareudp_netdev_ops;
543 dev->needs_free_netdev = true;
544 SET_NETDEV_DEVTYPE(dev, &bareudp_type);
545 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
546 dev->features |= NETIF_F_RXCSUM;
547 dev->features |= NETIF_F_LLTX;
548 dev->features |= NETIF_F_GSO_SOFTWARE;
549 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
550 dev->hw_features |= NETIF_F_RXCSUM;
551 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
552 dev->hard_header_len = 0;
554 dev->mtu = ETH_DATA_LEN;
555 dev->min_mtu = IPV4_MIN_MTU;
556 dev->max_mtu = IP_MAX_MTU - BAREUDP_BASE_HLEN;
557 dev->type = ARPHRD_NONE;
559 dev->priv_flags |= IFF_NO_QUEUE;
560 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
563 static int bareudp_validate(struct nlattr *tb[], struct nlattr *data[],
564 struct netlink_ext_ack *extack)
567 NL_SET_ERR_MSG(extack,
568 "Not enough attributes provided to perform the operation");
574 static int bareudp2info(struct nlattr *data[], struct bareudp_conf *conf,
575 struct netlink_ext_ack *extack)
577 memset(conf, 0, sizeof(*conf));
579 if (!data[IFLA_BAREUDP_PORT]) {
580 NL_SET_ERR_MSG(extack, "port not specified");
583 if (!data[IFLA_BAREUDP_ETHERTYPE]) {
584 NL_SET_ERR_MSG(extack, "ethertype not specified");
588 conf->port = nla_get_u16(data[IFLA_BAREUDP_PORT]);
589 conf->ethertype = nla_get_u16(data[IFLA_BAREUDP_ETHERTYPE]);
591 if (data[IFLA_BAREUDP_SRCPORT_MIN])
592 conf->sport_min = nla_get_u16(data[IFLA_BAREUDP_SRCPORT_MIN]);
594 if (data[IFLA_BAREUDP_MULTIPROTO_MODE])
595 conf->multi_proto_mode = true;
600 static struct bareudp_dev *bareudp_find_dev(struct bareudp_net *bn,
601 const struct bareudp_conf *conf)
603 struct bareudp_dev *bareudp, *t = NULL;
605 list_for_each_entry(bareudp, &bn->bareudp_list, next) {
606 if (conf->port == bareudp->port)
612 static int bareudp_configure(struct net *net, struct net_device *dev,
613 struct bareudp_conf *conf,
614 struct netlink_ext_ack *extack)
616 struct bareudp_net *bn = net_generic(net, bareudp_net_id);
617 struct bareudp_dev *t, *bareudp = netdev_priv(dev);
622 t = bareudp_find_dev(bn, conf);
624 NL_SET_ERR_MSG(extack, "Another bareudp device using the same port already exists");
628 if (conf->multi_proto_mode &&
629 (conf->ethertype != htons(ETH_P_MPLS_UC) &&
630 conf->ethertype != htons(ETH_P_IP))) {
631 NL_SET_ERR_MSG(extack, "Cannot set multiproto mode for this ethertype (only IPv4 and unicast MPLS are supported)");
635 bareudp->port = conf->port;
636 bareudp->ethertype = conf->ethertype;
637 bareudp->sport_min = conf->sport_min;
638 bareudp->multi_proto_mode = conf->multi_proto_mode;
640 err = register_netdevice(dev);
644 list_add(&bareudp->next, &bn->bareudp_list);
648 static int bareudp_link_config(struct net_device *dev,
654 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
661 static void bareudp_dellink(struct net_device *dev, struct list_head *head)
663 struct bareudp_dev *bareudp = netdev_priv(dev);
665 list_del(&bareudp->next);
666 unregister_netdevice_queue(dev, head);
669 static int bareudp_newlink(struct net *net, struct net_device *dev,
670 struct nlattr *tb[], struct nlattr *data[],
671 struct netlink_ext_ack *extack)
673 struct bareudp_conf conf;
676 err = bareudp2info(data, &conf, extack);
680 err = bareudp_configure(net, dev, &conf, extack);
684 err = bareudp_link_config(dev, tb);
691 bareudp_dellink(dev, NULL);
695 static size_t bareudp_get_size(const struct net_device *dev)
697 return nla_total_size(sizeof(__be16)) + /* IFLA_BAREUDP_PORT */
698 nla_total_size(sizeof(__be16)) + /* IFLA_BAREUDP_ETHERTYPE */
699 nla_total_size(sizeof(__u16)) + /* IFLA_BAREUDP_SRCPORT_MIN */
700 nla_total_size(0) + /* IFLA_BAREUDP_MULTIPROTO_MODE */
704 static int bareudp_fill_info(struct sk_buff *skb, const struct net_device *dev)
706 struct bareudp_dev *bareudp = netdev_priv(dev);
708 if (nla_put_be16(skb, IFLA_BAREUDP_PORT, bareudp->port))
709 goto nla_put_failure;
710 if (nla_put_be16(skb, IFLA_BAREUDP_ETHERTYPE, bareudp->ethertype))
711 goto nla_put_failure;
712 if (nla_put_u16(skb, IFLA_BAREUDP_SRCPORT_MIN, bareudp->sport_min))
713 goto nla_put_failure;
714 if (bareudp->multi_proto_mode &&
715 nla_put_flag(skb, IFLA_BAREUDP_MULTIPROTO_MODE))
716 goto nla_put_failure;
724 static struct rtnl_link_ops bareudp_link_ops __read_mostly = {
726 .maxtype = IFLA_BAREUDP_MAX,
727 .policy = bareudp_policy,
728 .priv_size = sizeof(struct bareudp_dev),
729 .setup = bareudp_setup,
730 .validate = bareudp_validate,
731 .newlink = bareudp_newlink,
732 .dellink = bareudp_dellink,
733 .get_size = bareudp_get_size,
734 .fill_info = bareudp_fill_info,
737 static __net_init int bareudp_init_net(struct net *net)
739 struct bareudp_net *bn = net_generic(net, bareudp_net_id);
741 INIT_LIST_HEAD(&bn->bareudp_list);
745 static void bareudp_destroy_tunnels(struct net *net, struct list_head *head)
747 struct bareudp_net *bn = net_generic(net, bareudp_net_id);
748 struct bareudp_dev *bareudp, *next;
750 list_for_each_entry_safe(bareudp, next, &bn->bareudp_list, next)
751 unregister_netdevice_queue(bareudp->dev, head);
754 static void __net_exit bareudp_exit_batch_net(struct list_head *net_list)
760 list_for_each_entry(net, net_list, exit_list)
761 bareudp_destroy_tunnels(net, &list);
763 /* unregister the devices gathered above */
764 unregister_netdevice_many(&list);
768 static struct pernet_operations bareudp_net_ops = {
769 .init = bareudp_init_net,
770 .exit_batch = bareudp_exit_batch_net,
771 .id = &bareudp_net_id,
772 .size = sizeof(struct bareudp_net),
775 static int __init bareudp_init_module(void)
779 rc = register_pernet_subsys(&bareudp_net_ops);
783 rc = rtnl_link_register(&bareudp_link_ops);
789 unregister_pernet_subsys(&bareudp_net_ops);
793 late_initcall(bareudp_init_module);
795 static void __exit bareudp_cleanup_module(void)
797 rtnl_link_unregister(&bareudp_link_ops);
798 unregister_pernet_subsys(&bareudp_net_ops);
800 module_exit(bareudp_cleanup_module);
802 MODULE_ALIAS_RTNL_LINK("bareudp");
803 MODULE_LICENSE("GPL");
804 MODULE_AUTHOR("Martin Varghese <martin.varghese@nokia.com>");
805 MODULE_DESCRIPTION("Interface driver for UDP encapsulated traffic");