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;
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 pcpu_sw_netstats *stats;
58 struct bareudp_dev *bareudp;
59 unsigned short family;
65 bareudp = rcu_dereference_sk_user_data(sk);
69 if (skb->protocol == htons(ETH_P_IP))
74 if (bareudp->ethertype == htons(ETH_P_IP)) {
77 iphdr = (struct iphdr *)(skb->data + BAREUDP_BASE_HLEN);
78 if (iphdr->version == 4) {
79 proto = bareudp->ethertype;
80 } else if (bareudp->multi_proto_mode && (iphdr->version == 6)) {
81 proto = htons(ETH_P_IPV6);
83 bareudp->dev->stats.rx_dropped++;
86 } else if (bareudp->ethertype == htons(ETH_P_MPLS_UC)) {
87 struct iphdr *tunnel_hdr;
89 tunnel_hdr = (struct iphdr *)skb_network_header(skb);
90 if (tunnel_hdr->version == 4) {
91 if (!ipv4_is_multicast(tunnel_hdr->daddr)) {
92 proto = bareudp->ethertype;
93 } else if (bareudp->multi_proto_mode &&
94 ipv4_is_multicast(tunnel_hdr->daddr)) {
95 proto = htons(ETH_P_MPLS_MC);
97 bareudp->dev->stats.rx_dropped++;
102 struct ipv6hdr *tunnel_hdr_v6;
104 tunnel_hdr_v6 = (struct ipv6hdr *)skb_network_header(skb);
106 ipv6_addr_type((struct in6_addr *)&tunnel_hdr_v6->daddr);
107 if (!(addr_type & IPV6_ADDR_MULTICAST)) {
108 proto = bareudp->ethertype;
109 } else if (bareudp->multi_proto_mode &&
110 (addr_type & IPV6_ADDR_MULTICAST)) {
111 proto = htons(ETH_P_MPLS_MC);
113 bareudp->dev->stats.rx_dropped++;
118 proto = bareudp->ethertype;
121 if (iptunnel_pull_header(skb, BAREUDP_BASE_HLEN,
123 !net_eq(bareudp->net,
124 dev_net(bareudp->dev)))) {
125 bareudp->dev->stats.rx_dropped++;
129 tun_dst = udp_tun_rx_dst(skb, family, TUNNEL_KEY, 0, 0);
131 bareudp->dev->stats.rx_dropped++;
134 skb_dst_set(skb, &tun_dst->dst);
135 skb->dev = bareudp->dev;
136 oiph = skb_network_header(skb);
137 skb_reset_network_header(skb);
139 if (family == AF_INET)
140 err = IP_ECN_decapsulate(oiph, skb);
141 #if IS_ENABLED(CONFIG_IPV6)
143 err = IP6_ECN_decapsulate(oiph, skb);
148 if (family == AF_INET)
149 net_info_ratelimited("non-ECT from %pI4 "
151 &((struct iphdr *)oiph)->saddr,
152 ((struct iphdr *)oiph)->tos);
153 #if IS_ENABLED(CONFIG_IPV6)
155 net_info_ratelimited("non-ECT from %pI6\n",
156 &((struct ipv6hdr *)oiph)->saddr);
160 ++bareudp->dev->stats.rx_frame_errors;
161 ++bareudp->dev->stats.rx_errors;
167 err = gro_cells_receive(&bareudp->gro_cells, skb);
168 if (likely(err == NET_RX_SUCCESS)) {
169 stats = this_cpu_ptr(bareudp->dev->tstats);
170 u64_stats_update_begin(&stats->syncp);
172 stats->rx_bytes += len;
173 u64_stats_update_end(&stats->syncp);
177 /* Consume bad packet */
183 static int bareudp_err_lookup(struct sock *sk, struct sk_buff *skb)
188 static int bareudp_init(struct net_device *dev)
190 struct bareudp_dev *bareudp = netdev_priv(dev);
193 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
197 err = gro_cells_init(&bareudp->gro_cells, dev);
199 free_percpu(dev->tstats);
205 static void bareudp_uninit(struct net_device *dev)
207 struct bareudp_dev *bareudp = netdev_priv(dev);
209 gro_cells_destroy(&bareudp->gro_cells);
210 free_percpu(dev->tstats);
213 static struct socket *bareudp_create_sock(struct net *net, __be16 port)
215 struct udp_port_cfg udp_conf;
219 memset(&udp_conf, 0, sizeof(udp_conf));
220 #if IS_ENABLED(CONFIG_IPV6)
221 udp_conf.family = AF_INET6;
223 udp_conf.family = AF_INET;
225 udp_conf.local_udp_port = port;
226 /* Open UDP socket */
227 err = udp_sock_create(net, &udp_conf, &sock);
234 /* Create new listen socket if needed */
235 static int bareudp_socket_create(struct bareudp_dev *bareudp, __be16 port)
237 struct udp_tunnel_sock_cfg tunnel_cfg;
240 sock = bareudp_create_sock(bareudp->net, port);
242 return PTR_ERR(sock);
244 /* Mark socket as an encapsulation socket */
245 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
246 tunnel_cfg.sk_user_data = bareudp;
247 tunnel_cfg.encap_type = 1;
248 tunnel_cfg.encap_rcv = bareudp_udp_encap_recv;
249 tunnel_cfg.encap_err_lookup = bareudp_err_lookup;
250 tunnel_cfg.encap_destroy = NULL;
251 setup_udp_tunnel_sock(bareudp->net, sock, &tunnel_cfg);
253 if (sock->sk->sk_family == AF_INET6)
256 rcu_assign_pointer(bareudp->sock, sock);
260 static int bareudp_open(struct net_device *dev)
262 struct bareudp_dev *bareudp = netdev_priv(dev);
265 ret = bareudp_socket_create(bareudp, bareudp->port);
269 static void bareudp_sock_release(struct bareudp_dev *bareudp)
273 sock = bareudp->sock;
274 rcu_assign_pointer(bareudp->sock, NULL);
276 udp_tunnel_sock_release(sock);
279 static int bareudp_stop(struct net_device *dev)
281 struct bareudp_dev *bareudp = netdev_priv(dev);
283 bareudp_sock_release(bareudp);
287 static int bareudp_xmit_skb(struct sk_buff *skb, struct net_device *dev,
288 struct bareudp_dev *bareudp,
289 const struct ip_tunnel_info *info)
291 bool xnet = !net_eq(bareudp->net, dev_net(bareudp->dev));
292 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
293 struct socket *sock = rcu_dereference(bareudp->sock);
294 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
295 const struct ip_tunnel_key *key = &info->key;
306 rt = ip_route_output_tunnel(skb, dev, bareudp->net, &saddr, info,
307 IPPROTO_UDP, use_cache);
312 skb_tunnel_check_pmtu(skb, &rt->dst,
313 BAREUDP_IPV4_HLEN + info->options_len);
315 sport = udp_flow_src_port(bareudp->net, skb,
316 bareudp->sport_min, USHRT_MAX,
318 tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
320 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
321 skb_scrub_packet(skb, xnet);
323 if (!skb_pull(skb, skb_network_offset(skb)))
326 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len +
327 BAREUDP_BASE_HLEN + info->options_len + sizeof(struct iphdr);
329 err = skb_cow_head(skb, min_headroom);
333 err = udp_tunnel_handle_offloads(skb, udp_sum);
337 skb_set_inner_protocol(skb, bareudp->ethertype);
338 udp_tunnel_xmit_skb(rt, sock->sk, skb, saddr, info->key.u.ipv4.dst,
339 tos, ttl, df, sport, bareudp->port,
340 !net_eq(bareudp->net, dev_net(bareudp->dev)),
341 !(info->key.tun_flags & TUNNEL_CSUM));
345 dst_release(&rt->dst);
349 #if IS_ENABLED(CONFIG_IPV6)
350 static int bareudp6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
351 struct bareudp_dev *bareudp,
352 const struct ip_tunnel_info *info)
354 bool xnet = !net_eq(bareudp->net, dev_net(bareudp->dev));
355 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
356 struct socket *sock = rcu_dereference(bareudp->sock);
357 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
358 const struct ip_tunnel_key *key = &info->key;
359 struct dst_entry *dst = NULL;
360 struct in6_addr saddr, daddr;
369 dst = ip6_dst_lookup_tunnel(skb, dev, bareudp->net, sock, &saddr, info,
370 IPPROTO_UDP, use_cache);
374 skb_tunnel_check_pmtu(skb, dst, BAREUDP_IPV6_HLEN + info->options_len);
376 sport = udp_flow_src_port(bareudp->net, skb,
377 bareudp->sport_min, USHRT_MAX,
379 prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
382 skb_scrub_packet(skb, xnet);
384 if (!skb_pull(skb, skb_network_offset(skb)))
387 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len +
388 BAREUDP_BASE_HLEN + info->options_len + sizeof(struct iphdr);
390 err = skb_cow_head(skb, min_headroom);
394 err = udp_tunnel_handle_offloads(skb, udp_sum);
398 daddr = info->key.u.ipv6.dst;
399 udp_tunnel6_xmit_skb(dst, sock->sk, skb, dev,
400 &saddr, &daddr, prio, ttl,
401 info->key.label, sport, bareudp->port,
402 !(info->key.tun_flags & TUNNEL_CSUM));
411 static netdev_tx_t bareudp_xmit(struct sk_buff *skb, struct net_device *dev)
413 struct bareudp_dev *bareudp = netdev_priv(dev);
414 struct ip_tunnel_info *info = NULL;
417 if (skb->protocol != bareudp->ethertype) {
418 if (!bareudp->multi_proto_mode ||
419 (skb->protocol != htons(ETH_P_MPLS_MC) &&
420 skb->protocol != htons(ETH_P_IPV6))) {
426 info = skb_tunnel_info(skb);
427 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
433 #if IS_ENABLED(CONFIG_IPV6)
434 if (info->mode & IP_TUNNEL_INFO_IPV6)
435 err = bareudp6_xmit_skb(skb, dev, bareudp, info);
438 err = bareudp_xmit_skb(skb, dev, bareudp, info);
448 dev->stats.collisions++;
449 else if (err == -ENETUNREACH)
450 dev->stats.tx_carrier_errors++;
452 dev->stats.tx_errors++;
456 static int bareudp_fill_metadata_dst(struct net_device *dev,
459 struct ip_tunnel_info *info = skb_tunnel_info(skb);
460 struct bareudp_dev *bareudp = netdev_priv(dev);
463 use_cache = ip_tunnel_dst_cache_usable(skb, info);
465 if (ip_tunnel_info_af(info) == AF_INET) {
469 rt = ip_route_output_tunnel(skb, dev, bareudp->net, &saddr,
470 info, IPPROTO_UDP, use_cache);
475 info->key.u.ipv4.src = saddr;
476 #if IS_ENABLED(CONFIG_IPV6)
477 } else if (ip_tunnel_info_af(info) == AF_INET6) {
478 struct dst_entry *dst;
479 struct in6_addr saddr;
480 struct socket *sock = rcu_dereference(bareudp->sock);
482 dst = ip6_dst_lookup_tunnel(skb, dev, bareudp->net, sock,
483 &saddr, info, IPPROTO_UDP,
489 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 = ip_tunnel_get_stats64,
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 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;
531 dev->features |= NETIF_F_RXCSUM;
532 dev->features |= NETIF_F_GSO_SOFTWARE;
533 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
534 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
535 dev->hard_header_len = 0;
537 dev->mtu = ETH_DATA_LEN;
538 dev->min_mtu = IPV4_MIN_MTU;
539 dev->max_mtu = IP_MAX_MTU - BAREUDP_BASE_HLEN;
540 dev->type = ARPHRD_NONE;
542 dev->priv_flags |= IFF_NO_QUEUE;
543 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
546 static int bareudp_validate(struct nlattr *tb[], struct nlattr *data[],
547 struct netlink_ext_ack *extack)
550 NL_SET_ERR_MSG(extack,
551 "Not enough attributes provided to perform the operation");
557 static int bareudp2info(struct nlattr *data[], struct bareudp_conf *conf)
559 if (!data[IFLA_BAREUDP_PORT] || !data[IFLA_BAREUDP_ETHERTYPE])
562 if (data[IFLA_BAREUDP_PORT])
563 conf->port = nla_get_u16(data[IFLA_BAREUDP_PORT]);
565 if (data[IFLA_BAREUDP_ETHERTYPE])
566 conf->ethertype = nla_get_u16(data[IFLA_BAREUDP_ETHERTYPE]);
568 if (data[IFLA_BAREUDP_SRCPORT_MIN])
569 conf->sport_min = nla_get_u16(data[IFLA_BAREUDP_SRCPORT_MIN]);
574 static struct bareudp_dev *bareudp_find_dev(struct bareudp_net *bn,
575 const struct bareudp_conf *conf)
577 struct bareudp_dev *bareudp, *t = NULL;
579 list_for_each_entry(bareudp, &bn->bareudp_list, next) {
580 if (conf->port == bareudp->port)
586 static int bareudp_configure(struct net *net, struct net_device *dev,
587 struct bareudp_conf *conf)
589 struct bareudp_net *bn = net_generic(net, bareudp_net_id);
590 struct bareudp_dev *t, *bareudp = netdev_priv(dev);
595 t = bareudp_find_dev(bn, conf);
599 if (conf->multi_proto_mode &&
600 (conf->ethertype != htons(ETH_P_MPLS_UC) &&
601 conf->ethertype != htons(ETH_P_IP)))
604 bareudp->port = conf->port;
605 bareudp->ethertype = conf->ethertype;
606 bareudp->sport_min = conf->sport_min;
607 bareudp->multi_proto_mode = conf->multi_proto_mode;
608 err = register_netdevice(dev);
612 list_add(&bareudp->next, &bn->bareudp_list);
616 static int bareudp_link_config(struct net_device *dev,
622 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
629 static int bareudp_newlink(struct net *net, struct net_device *dev,
630 struct nlattr *tb[], struct nlattr *data[],
631 struct netlink_ext_ack *extack)
633 struct bareudp_conf conf;
636 err = bareudp2info(data, &conf);
640 err = bareudp_configure(net, dev, &conf);
644 err = bareudp_link_config(dev, tb);
651 static void bareudp_dellink(struct net_device *dev, struct list_head *head)
653 struct bareudp_dev *bareudp = netdev_priv(dev);
655 list_del(&bareudp->next);
656 unregister_netdevice_queue(dev, head);
659 static size_t bareudp_get_size(const struct net_device *dev)
661 return nla_total_size(sizeof(__be16)) + /* IFLA_BAREUDP_PORT */
662 nla_total_size(sizeof(__be16)) + /* IFLA_BAREUDP_ETHERTYPE */
663 nla_total_size(sizeof(__u16)) + /* IFLA_BAREUDP_SRCPORT_MIN */
664 nla_total_size(0) + /* IFLA_BAREUDP_MULTIPROTO_MODE */
668 static int bareudp_fill_info(struct sk_buff *skb, const struct net_device *dev)
670 struct bareudp_dev *bareudp = netdev_priv(dev);
672 if (nla_put_be16(skb, IFLA_BAREUDP_PORT, bareudp->port))
673 goto nla_put_failure;
674 if (nla_put_be16(skb, IFLA_BAREUDP_ETHERTYPE, bareudp->ethertype))
675 goto nla_put_failure;
676 if (nla_put_u16(skb, IFLA_BAREUDP_SRCPORT_MIN, bareudp->sport_min))
677 goto nla_put_failure;
678 if (bareudp->multi_proto_mode &&
679 nla_put_flag(skb, IFLA_BAREUDP_MULTIPROTO_MODE))
680 goto nla_put_failure;
688 static struct rtnl_link_ops bareudp_link_ops __read_mostly = {
690 .maxtype = IFLA_BAREUDP_MAX,
691 .policy = bareudp_policy,
692 .priv_size = sizeof(struct bareudp_dev),
693 .setup = bareudp_setup,
694 .validate = bareudp_validate,
695 .newlink = bareudp_newlink,
696 .dellink = bareudp_dellink,
697 .get_size = bareudp_get_size,
698 .fill_info = bareudp_fill_info,
701 struct net_device *bareudp_dev_create(struct net *net, const char *name,
703 struct bareudp_conf *conf)
705 struct nlattr *tb[IFLA_MAX + 1];
706 struct net_device *dev;
707 LIST_HEAD(list_kill);
710 memset(tb, 0, sizeof(tb));
711 dev = rtnl_create_link(net, name, name_assign_type,
712 &bareudp_link_ops, tb, NULL);
716 err = bareudp_configure(net, dev, conf);
721 err = dev_set_mtu(dev, IP_MAX_MTU - BAREUDP_BASE_HLEN);
725 err = rtnl_configure_link(dev, NULL);
731 bareudp_dellink(dev, &list_kill);
732 unregister_netdevice_many(&list_kill);
735 EXPORT_SYMBOL_GPL(bareudp_dev_create);
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_LICENSE("GPL");
803 MODULE_AUTHOR("Martin Varghese <martin.varghese@nokia.com>");
804 MODULE_DESCRIPTION("Interface driver for UDP encapsulated traffic");