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++;
128 tun_dst = udp_tun_rx_dst(skb, family, TUNNEL_KEY, 0, 0);
130 bareudp->dev->stats.rx_dropped++;
133 skb_dst_set(skb, &tun_dst->dst);
134 skb->dev = bareudp->dev;
135 oiph = skb_network_header(skb);
136 skb_reset_network_header(skb);
138 if (!IS_ENABLED(CONFIG_IPV6) || family == AF_INET)
139 err = IP_ECN_decapsulate(oiph, skb);
141 err = IP6_ECN_decapsulate(oiph, skb);
145 if (!IS_ENABLED(CONFIG_IPV6) || family == AF_INET)
146 net_info_ratelimited("non-ECT from %pI4 "
148 &((struct iphdr *)oiph)->saddr,
149 ((struct iphdr *)oiph)->tos);
151 net_info_ratelimited("non-ECT from %pI6\n",
152 &((struct ipv6hdr *)oiph)->saddr);
155 ++bareudp->dev->stats.rx_frame_errors;
156 ++bareudp->dev->stats.rx_errors;
162 err = gro_cells_receive(&bareudp->gro_cells, skb);
163 if (likely(err == NET_RX_SUCCESS)) {
164 stats = this_cpu_ptr(bareudp->dev->tstats);
165 u64_stats_update_begin(&stats->syncp);
167 stats->rx_bytes += len;
168 u64_stats_update_end(&stats->syncp);
172 /* Consume bad packet */
178 static int bareudp_err_lookup(struct sock *sk, struct sk_buff *skb)
183 static int bareudp_init(struct net_device *dev)
185 struct bareudp_dev *bareudp = netdev_priv(dev);
188 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
192 err = gro_cells_init(&bareudp->gro_cells, dev);
194 free_percpu(dev->tstats);
200 static void bareudp_uninit(struct net_device *dev)
202 struct bareudp_dev *bareudp = netdev_priv(dev);
204 gro_cells_destroy(&bareudp->gro_cells);
205 free_percpu(dev->tstats);
208 static struct socket *bareudp_create_sock(struct net *net, __be16 port)
210 struct udp_port_cfg udp_conf;
214 memset(&udp_conf, 0, sizeof(udp_conf));
215 #if IS_ENABLED(CONFIG_IPV6)
216 udp_conf.family = AF_INET6;
218 udp_conf.family = AF_INET;
220 udp_conf.local_udp_port = port;
221 /* Open UDP socket */
222 err = udp_sock_create(net, &udp_conf, &sock);
229 /* Create new listen socket if needed */
230 static int bareudp_socket_create(struct bareudp_dev *bareudp, __be16 port)
232 struct udp_tunnel_sock_cfg tunnel_cfg;
235 sock = bareudp_create_sock(bareudp->net, port);
237 return PTR_ERR(sock);
239 /* Mark socket as an encapsulation socket */
240 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
241 tunnel_cfg.sk_user_data = bareudp;
242 tunnel_cfg.encap_type = 1;
243 tunnel_cfg.encap_rcv = bareudp_udp_encap_recv;
244 tunnel_cfg.encap_err_lookup = bareudp_err_lookup;
245 tunnel_cfg.encap_destroy = NULL;
246 setup_udp_tunnel_sock(bareudp->net, sock, &tunnel_cfg);
248 /* As the setup_udp_tunnel_sock does not call udp_encap_enable if the
249 * socket type is v6 an explicit call to udp_encap_enable is needed.
251 if (sock->sk->sk_family == AF_INET6)
254 rcu_assign_pointer(bareudp->sock, sock);
258 static int bareudp_open(struct net_device *dev)
260 struct bareudp_dev *bareudp = netdev_priv(dev);
263 ret = bareudp_socket_create(bareudp, bareudp->port);
267 static void bareudp_sock_release(struct bareudp_dev *bareudp)
271 sock = bareudp->sock;
272 rcu_assign_pointer(bareudp->sock, NULL);
274 udp_tunnel_sock_release(sock);
277 static int bareudp_stop(struct net_device *dev)
279 struct bareudp_dev *bareudp = netdev_priv(dev);
281 bareudp_sock_release(bareudp);
285 static int bareudp_xmit_skb(struct sk_buff *skb, struct net_device *dev,
286 struct bareudp_dev *bareudp,
287 const struct ip_tunnel_info *info)
289 bool xnet = !net_eq(bareudp->net, dev_net(bareudp->dev));
290 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
291 struct socket *sock = rcu_dereference(bareudp->sock);
292 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
293 const struct ip_tunnel_key *key = &info->key;
304 rt = ip_route_output_tunnel(skb, dev, bareudp->net, &saddr, info,
305 IPPROTO_UDP, use_cache);
310 skb_tunnel_check_pmtu(skb, &rt->dst,
311 BAREUDP_IPV4_HLEN + info->options_len, false);
313 sport = udp_flow_src_port(bareudp->net, skb,
314 bareudp->sport_min, USHRT_MAX,
316 tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
318 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
319 skb_scrub_packet(skb, xnet);
322 if (!skb_pull(skb, skb_network_offset(skb)))
325 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len +
326 BAREUDP_BASE_HLEN + info->options_len + sizeof(struct iphdr);
328 err = skb_cow_head(skb, min_headroom);
332 err = udp_tunnel_handle_offloads(skb, udp_sum);
336 skb_set_inner_protocol(skb, bareudp->ethertype);
337 udp_tunnel_xmit_skb(rt, sock->sk, skb, saddr, info->key.u.ipv4.dst,
338 tos, ttl, df, sport, bareudp->port,
339 !net_eq(bareudp->net, dev_net(bareudp->dev)),
340 !(info->key.tun_flags & TUNNEL_CSUM));
344 dst_release(&rt->dst);
348 static int bareudp6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
349 struct bareudp_dev *bareudp,
350 const struct ip_tunnel_info *info)
352 bool xnet = !net_eq(bareudp->net, dev_net(bareudp->dev));
353 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
354 struct socket *sock = rcu_dereference(bareudp->sock);
355 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
356 const struct ip_tunnel_key *key = &info->key;
357 struct dst_entry *dst = NULL;
358 struct in6_addr saddr, daddr;
367 dst = ip6_dst_lookup_tunnel(skb, dev, bareudp->net, sock, &saddr, info,
368 IPPROTO_UDP, use_cache);
372 skb_tunnel_check_pmtu(skb, dst, BAREUDP_IPV6_HLEN + info->options_len,
375 sport = udp_flow_src_port(bareudp->net, skb,
376 bareudp->sport_min, USHRT_MAX,
378 prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
381 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));
410 static bool bareudp_proto_valid(struct bareudp_dev *bareudp, __be16 proto)
412 if (bareudp->ethertype == proto)
415 if (!bareudp->multi_proto_mode)
418 if (bareudp->ethertype == htons(ETH_P_MPLS_UC) &&
419 proto == htons(ETH_P_MPLS_MC))
422 if (bareudp->ethertype == htons(ETH_P_IP) &&
423 proto == htons(ETH_P_IPV6))
429 static netdev_tx_t bareudp_xmit(struct sk_buff *skb, struct net_device *dev)
431 struct bareudp_dev *bareudp = netdev_priv(dev);
432 struct ip_tunnel_info *info = NULL;
435 if (!bareudp_proto_valid(bareudp, skb->protocol)) {
440 info = skb_tunnel_info(skb);
441 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
447 if (IS_ENABLED(CONFIG_IPV6) && info->mode & IP_TUNNEL_INFO_IPV6)
448 err = bareudp6_xmit_skb(skb, dev, bareudp, info);
450 err = bareudp_xmit_skb(skb, dev, bareudp, info);
460 dev->stats.collisions++;
461 else if (err == -ENETUNREACH)
462 dev->stats.tx_carrier_errors++;
464 dev->stats.tx_errors++;
468 static int bareudp_fill_metadata_dst(struct net_device *dev,
471 struct ip_tunnel_info *info = skb_tunnel_info(skb);
472 struct bareudp_dev *bareudp = netdev_priv(dev);
475 use_cache = ip_tunnel_dst_cache_usable(skb, info);
477 if (!IS_ENABLED(CONFIG_IPV6) || ip_tunnel_info_af(info) == AF_INET) {
481 rt = ip_route_output_tunnel(skb, dev, bareudp->net, &saddr,
482 info, IPPROTO_UDP, use_cache);
487 info->key.u.ipv4.src = saddr;
488 } else if (ip_tunnel_info_af(info) == AF_INET6) {
489 struct dst_entry *dst;
490 struct in6_addr saddr;
491 struct socket *sock = rcu_dereference(bareudp->sock);
493 dst = ip6_dst_lookup_tunnel(skb, dev, bareudp->net, sock,
494 &saddr, info, IPPROTO_UDP,
500 info->key.u.ipv6.src = saddr;
505 info->key.tp_src = udp_flow_src_port(bareudp->net, skb,
508 info->key.tp_dst = bareudp->port;
512 static const struct net_device_ops bareudp_netdev_ops = {
513 .ndo_init = bareudp_init,
514 .ndo_uninit = bareudp_uninit,
515 .ndo_open = bareudp_open,
516 .ndo_stop = bareudp_stop,
517 .ndo_start_xmit = bareudp_xmit,
518 .ndo_get_stats64 = ip_tunnel_get_stats64,
519 .ndo_fill_metadata_dst = bareudp_fill_metadata_dst,
522 static const struct nla_policy bareudp_policy[IFLA_BAREUDP_MAX + 1] = {
523 [IFLA_BAREUDP_PORT] = { .type = NLA_U16 },
524 [IFLA_BAREUDP_ETHERTYPE] = { .type = NLA_U16 },
525 [IFLA_BAREUDP_SRCPORT_MIN] = { .type = NLA_U16 },
526 [IFLA_BAREUDP_MULTIPROTO_MODE] = { .type = NLA_FLAG },
529 /* Info for udev, that this is a virtual tunnel endpoint */
530 static struct device_type bareudp_type = {
534 /* Initialize the device structure. */
535 static void bareudp_setup(struct net_device *dev)
537 dev->netdev_ops = &bareudp_netdev_ops;
538 dev->needs_free_netdev = true;
539 SET_NETDEV_DEVTYPE(dev, &bareudp_type);
540 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
541 dev->features |= NETIF_F_RXCSUM;
542 dev->features |= NETIF_F_GSO_SOFTWARE;
543 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
544 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
545 dev->hard_header_len = 0;
547 dev->mtu = ETH_DATA_LEN;
548 dev->min_mtu = IPV4_MIN_MTU;
549 dev->max_mtu = IP_MAX_MTU - BAREUDP_BASE_HLEN;
550 dev->type = ARPHRD_NONE;
552 dev->priv_flags |= IFF_NO_QUEUE;
553 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
556 static int bareudp_validate(struct nlattr *tb[], struct nlattr *data[],
557 struct netlink_ext_ack *extack)
560 NL_SET_ERR_MSG(extack,
561 "Not enough attributes provided to perform the operation");
567 static int bareudp2info(struct nlattr *data[], struct bareudp_conf *conf,
568 struct netlink_ext_ack *extack)
570 memset(conf, 0, sizeof(*conf));
572 if (!data[IFLA_BAREUDP_PORT]) {
573 NL_SET_ERR_MSG(extack, "port not specified");
576 if (!data[IFLA_BAREUDP_ETHERTYPE]) {
577 NL_SET_ERR_MSG(extack, "ethertype not specified");
581 if (data[IFLA_BAREUDP_PORT])
582 conf->port = nla_get_u16(data[IFLA_BAREUDP_PORT]);
584 if (data[IFLA_BAREUDP_ETHERTYPE])
585 conf->ethertype = nla_get_u16(data[IFLA_BAREUDP_ETHERTYPE]);
587 if (data[IFLA_BAREUDP_SRCPORT_MIN])
588 conf->sport_min = nla_get_u16(data[IFLA_BAREUDP_SRCPORT_MIN]);
590 if (data[IFLA_BAREUDP_MULTIPROTO_MODE])
591 conf->multi_proto_mode = true;
596 static struct bareudp_dev *bareudp_find_dev(struct bareudp_net *bn,
597 const struct bareudp_conf *conf)
599 struct bareudp_dev *bareudp, *t = NULL;
601 list_for_each_entry(bareudp, &bn->bareudp_list, next) {
602 if (conf->port == bareudp->port)
608 static int bareudp_configure(struct net *net, struct net_device *dev,
609 struct bareudp_conf *conf)
611 struct bareudp_net *bn = net_generic(net, bareudp_net_id);
612 struct bareudp_dev *t, *bareudp = netdev_priv(dev);
617 t = bareudp_find_dev(bn, conf);
621 if (conf->multi_proto_mode &&
622 (conf->ethertype != htons(ETH_P_MPLS_UC) &&
623 conf->ethertype != htons(ETH_P_IP)))
626 bareudp->port = conf->port;
627 bareudp->ethertype = conf->ethertype;
628 bareudp->sport_min = conf->sport_min;
629 bareudp->multi_proto_mode = conf->multi_proto_mode;
631 err = register_netdevice(dev);
635 list_add(&bareudp->next, &bn->bareudp_list);
639 static int bareudp_link_config(struct net_device *dev,
645 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
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 static void bareudp_dellink(struct net_device *dev, struct list_head *head)
676 struct bareudp_dev *bareudp = netdev_priv(dev);
678 list_del(&bareudp->next);
679 unregister_netdevice_queue(dev, head);
682 static size_t bareudp_get_size(const struct net_device *dev)
684 return nla_total_size(sizeof(__be16)) + /* IFLA_BAREUDP_PORT */
685 nla_total_size(sizeof(__be16)) + /* IFLA_BAREUDP_ETHERTYPE */
686 nla_total_size(sizeof(__u16)) + /* IFLA_BAREUDP_SRCPORT_MIN */
687 nla_total_size(0) + /* IFLA_BAREUDP_MULTIPROTO_MODE */
691 static int bareudp_fill_info(struct sk_buff *skb, const struct net_device *dev)
693 struct bareudp_dev *bareudp = netdev_priv(dev);
695 if (nla_put_be16(skb, IFLA_BAREUDP_PORT, bareudp->port))
696 goto nla_put_failure;
697 if (nla_put_be16(skb, IFLA_BAREUDP_ETHERTYPE, bareudp->ethertype))
698 goto nla_put_failure;
699 if (nla_put_u16(skb, IFLA_BAREUDP_SRCPORT_MIN, bareudp->sport_min))
700 goto nla_put_failure;
701 if (bareudp->multi_proto_mode &&
702 nla_put_flag(skb, IFLA_BAREUDP_MULTIPROTO_MODE))
703 goto nla_put_failure;
711 static struct rtnl_link_ops bareudp_link_ops __read_mostly = {
713 .maxtype = IFLA_BAREUDP_MAX,
714 .policy = bareudp_policy,
715 .priv_size = sizeof(struct bareudp_dev),
716 .setup = bareudp_setup,
717 .validate = bareudp_validate,
718 .newlink = bareudp_newlink,
719 .dellink = bareudp_dellink,
720 .get_size = bareudp_get_size,
721 .fill_info = bareudp_fill_info,
724 struct net_device *bareudp_dev_create(struct net *net, const char *name,
726 struct bareudp_conf *conf)
728 struct nlattr *tb[IFLA_MAX + 1];
729 struct net_device *dev;
730 LIST_HEAD(list_kill);
733 memset(tb, 0, sizeof(tb));
734 dev = rtnl_create_link(net, name, name_assign_type,
735 &bareudp_link_ops, tb, NULL);
739 err = bareudp_configure(net, dev, conf);
744 err = dev_set_mtu(dev, IP_MAX_MTU - BAREUDP_BASE_HLEN);
748 err = rtnl_configure_link(dev, NULL);
754 bareudp_dellink(dev, &list_kill);
755 unregister_netdevice_many(&list_kill);
758 EXPORT_SYMBOL_GPL(bareudp_dev_create);
760 static __net_init int bareudp_init_net(struct net *net)
762 struct bareudp_net *bn = net_generic(net, bareudp_net_id);
764 INIT_LIST_HEAD(&bn->bareudp_list);
768 static void bareudp_destroy_tunnels(struct net *net, struct list_head *head)
770 struct bareudp_net *bn = net_generic(net, bareudp_net_id);
771 struct bareudp_dev *bareudp, *next;
773 list_for_each_entry_safe(bareudp, next, &bn->bareudp_list, next)
774 unregister_netdevice_queue(bareudp->dev, head);
777 static void __net_exit bareudp_exit_batch_net(struct list_head *net_list)
783 list_for_each_entry(net, net_list, exit_list)
784 bareudp_destroy_tunnels(net, &list);
786 /* unregister the devices gathered above */
787 unregister_netdevice_many(&list);
791 static struct pernet_operations bareudp_net_ops = {
792 .init = bareudp_init_net,
793 .exit_batch = bareudp_exit_batch_net,
794 .id = &bareudp_net_id,
795 .size = sizeof(struct bareudp_net),
798 static int __init bareudp_init_module(void)
802 rc = register_pernet_subsys(&bareudp_net_ops);
806 rc = rtnl_link_register(&bareudp_link_ops);
812 unregister_pernet_subsys(&bareudp_net_ops);
816 late_initcall(bareudp_init_module);
818 static void __exit bareudp_cleanup_module(void)
820 rtnl_link_unregister(&bareudp_link_ops);
821 unregister_pernet_subsys(&bareudp_net_ops);
823 module_exit(bareudp_cleanup_module);
825 MODULE_ALIAS_RTNL_LINK("bareudp");
826 MODULE_LICENSE("GPL");
827 MODULE_AUTHOR("Martin Varghese <martin.varghese@nokia.com>");
828 MODULE_DESCRIPTION("Interface driver for UDP encapsulated traffic");