Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* Bareudp: UDP tunnel encasulation for different Payload types like |
| 3 | * MPLS, NSH, IP, etc. |
| 4 | * Copyright (c) 2019 Nokia, Inc. |
| 5 | * Authors: Martin Varghese, <martin.varghese@nokia.com> |
| 6 | */ |
| 7 | |
| 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 9 | |
| 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> |
| 22 | |
| 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)) |
| 28 | |
| 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"); |
| 32 | |
| 33 | /* per-network namespace private data for this module */ |
| 34 | |
| 35 | static unsigned int bareudp_net_id; |
| 36 | |
| 37 | struct bareudp_net { |
| 38 | struct list_head bareudp_list; |
| 39 | }; |
| 40 | |
Guillaume Nault | dcdd77e | 2021-12-10 20:56:39 +0100 | [diff] [blame] | 41 | struct bareudp_conf { |
| 42 | __be16 ethertype; |
| 43 | __be16 port; |
| 44 | u16 sport_min; |
| 45 | bool multi_proto_mode; |
| 46 | }; |
| 47 | |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 48 | /* Pseudo network device */ |
| 49 | struct bareudp_dev { |
| 50 | struct net *net; /* netns for packet i/o */ |
| 51 | struct net_device *dev; /* netdev for bareudp tunnel */ |
| 52 | __be16 ethertype; |
| 53 | __be16 port; |
| 54 | u16 sport_min; |
Martin Varghese | 4b5f672 | 2020-02-24 10:58:35 +0530 | [diff] [blame] | 55 | bool multi_proto_mode; |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 56 | struct socket __rcu *sock; |
| 57 | struct list_head next; /* bareudp node on namespace list */ |
| 58 | struct gro_cells gro_cells; |
| 59 | }; |
| 60 | |
| 61 | static int bareudp_udp_encap_recv(struct sock *sk, struct sk_buff *skb) |
| 62 | { |
| 63 | struct metadata_dst *tun_dst = NULL; |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 64 | struct bareudp_dev *bareudp; |
| 65 | unsigned short family; |
| 66 | unsigned int len; |
| 67 | __be16 proto; |
| 68 | void *oiph; |
| 69 | int err; |
| 70 | |
| 71 | bareudp = rcu_dereference_sk_user_data(sk); |
| 72 | if (!bareudp) |
| 73 | goto drop; |
| 74 | |
| 75 | if (skb->protocol == htons(ETH_P_IP)) |
| 76 | family = AF_INET; |
| 77 | else |
| 78 | family = AF_INET6; |
| 79 | |
Martin Varghese | 4b5f672 | 2020-02-24 10:58:35 +0530 | [diff] [blame] | 80 | if (bareudp->ethertype == htons(ETH_P_IP)) { |
Guillaume Nault | 143a852 | 2021-08-06 17:52:06 +0200 | [diff] [blame] | 81 | __u8 ipversion; |
Martin Varghese | 4b5f672 | 2020-02-24 10:58:35 +0530 | [diff] [blame] | 82 | |
Guillaume Nault | 143a852 | 2021-08-06 17:52:06 +0200 | [diff] [blame] | 83 | if (skb_copy_bits(skb, BAREUDP_BASE_HLEN, &ipversion, |
| 84 | sizeof(ipversion))) { |
| 85 | bareudp->dev->stats.rx_dropped++; |
| 86 | goto drop; |
| 87 | } |
| 88 | ipversion >>= 4; |
| 89 | |
| 90 | if (ipversion == 4) { |
| 91 | proto = htons(ETH_P_IP); |
| 92 | } else if (ipversion == 6 && bareudp->multi_proto_mode) { |
Martin Varghese | 4b5f672 | 2020-02-24 10:58:35 +0530 | [diff] [blame] | 93 | proto = htons(ETH_P_IPV6); |
| 94 | } else { |
| 95 | bareudp->dev->stats.rx_dropped++; |
| 96 | goto drop; |
| 97 | } |
| 98 | } else if (bareudp->ethertype == htons(ETH_P_MPLS_UC)) { |
| 99 | struct iphdr *tunnel_hdr; |
| 100 | |
| 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); |
| 108 | } else { |
| 109 | bareudp->dev->stats.rx_dropped++; |
| 110 | goto drop; |
| 111 | } |
| 112 | } else { |
| 113 | int addr_type; |
| 114 | struct ipv6hdr *tunnel_hdr_v6; |
| 115 | |
| 116 | tunnel_hdr_v6 = (struct ipv6hdr *)skb_network_header(skb); |
| 117 | addr_type = |
| 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); |
| 124 | } else { |
| 125 | bareudp->dev->stats.rx_dropped++; |
| 126 | goto drop; |
| 127 | } |
| 128 | } |
| 129 | } else { |
| 130 | proto = bareudp->ethertype; |
| 131 | } |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 132 | |
| 133 | if (iptunnel_pull_header(skb, BAREUDP_BASE_HLEN, |
| 134 | proto, |
| 135 | !net_eq(bareudp->net, |
| 136 | dev_net(bareudp->dev)))) { |
| 137 | bareudp->dev->stats.rx_dropped++; |
| 138 | goto drop; |
| 139 | } |
Martin Varghese | 4787dd5 | 2020-07-17 08:05:12 +0530 | [diff] [blame] | 140 | tun_dst = udp_tun_rx_dst(skb, family, TUNNEL_KEY, 0, 0); |
| 141 | if (!tun_dst) { |
| 142 | bareudp->dev->stats.rx_dropped++; |
| 143 | goto drop; |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 144 | } |
Martin Varghese | 4787dd5 | 2020-07-17 08:05:12 +0530 | [diff] [blame] | 145 | skb_dst_set(skb, &tun_dst->dst); |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 146 | skb->dev = bareudp->dev; |
| 147 | oiph = skb_network_header(skb); |
| 148 | skb_reset_network_header(skb); |
Guillaume Nault | 99c8719 | 2021-06-25 15:33:01 +0200 | [diff] [blame] | 149 | skb_reset_mac_header(skb); |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 150 | |
Arnd Bergmann | ee28755 | 2020-05-05 19:22:14 +0200 | [diff] [blame] | 151 | if (!IS_ENABLED(CONFIG_IPV6) || family == AF_INET) |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 152 | err = IP_ECN_decapsulate(oiph, skb); |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 153 | else |
| 154 | err = IP6_ECN_decapsulate(oiph, skb); |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 155 | |
| 156 | if (unlikely(err)) { |
| 157 | if (log_ecn_error) { |
Arnd Bergmann | ee28755 | 2020-05-05 19:22:14 +0200 | [diff] [blame] | 158 | if (!IS_ENABLED(CONFIG_IPV6) || family == AF_INET) |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 159 | net_info_ratelimited("non-ECT from %pI4 " |
| 160 | "with TOS=%#x\n", |
| 161 | &((struct iphdr *)oiph)->saddr, |
| 162 | ((struct iphdr *)oiph)->tos); |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 163 | else |
| 164 | net_info_ratelimited("non-ECT from %pI6\n", |
| 165 | &((struct ipv6hdr *)oiph)->saddr); |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 166 | } |
| 167 | if (err > 1) { |
| 168 | ++bareudp->dev->stats.rx_frame_errors; |
| 169 | ++bareudp->dev->stats.rx_errors; |
| 170 | goto drop; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | len = skb->len; |
| 175 | err = gro_cells_receive(&bareudp->gro_cells, skb); |
Fabian Frederick | 8fdfffd | 2020-10-05 22:35:15 +0200 | [diff] [blame] | 176 | if (likely(err == NET_RX_SUCCESS)) |
| 177 | dev_sw_netstats_rx_add(bareudp->dev, len); |
| 178 | |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 179 | return 0; |
| 180 | drop: |
| 181 | /* Consume bad packet */ |
| 182 | kfree_skb(skb); |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | static int bareudp_err_lookup(struct sock *sk, struct sk_buff *skb) |
| 188 | { |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static int bareudp_init(struct net_device *dev) |
| 193 | { |
| 194 | struct bareudp_dev *bareudp = netdev_priv(dev); |
| 195 | int err; |
| 196 | |
| 197 | dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
| 198 | if (!dev->tstats) |
| 199 | return -ENOMEM; |
| 200 | |
| 201 | err = gro_cells_init(&bareudp->gro_cells, dev); |
| 202 | if (err) { |
| 203 | free_percpu(dev->tstats); |
| 204 | return err; |
| 205 | } |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static void bareudp_uninit(struct net_device *dev) |
| 210 | { |
| 211 | struct bareudp_dev *bareudp = netdev_priv(dev); |
| 212 | |
| 213 | gro_cells_destroy(&bareudp->gro_cells); |
| 214 | free_percpu(dev->tstats); |
| 215 | } |
| 216 | |
| 217 | static struct socket *bareudp_create_sock(struct net *net, __be16 port) |
| 218 | { |
| 219 | struct udp_port_cfg udp_conf; |
| 220 | struct socket *sock; |
| 221 | int err; |
| 222 | |
| 223 | memset(&udp_conf, 0, sizeof(udp_conf)); |
| 224 | #if IS_ENABLED(CONFIG_IPV6) |
| 225 | udp_conf.family = AF_INET6; |
| 226 | #else |
| 227 | udp_conf.family = AF_INET; |
| 228 | #endif |
| 229 | udp_conf.local_udp_port = port; |
| 230 | /* Open UDP socket */ |
| 231 | err = udp_sock_create(net, &udp_conf, &sock); |
| 232 | if (err < 0) |
| 233 | return ERR_PTR(err); |
| 234 | |
Paolo Abeni | b03ef67 | 2021-03-30 12:28:55 +0200 | [diff] [blame] | 235 | udp_allow_gso(sock->sk); |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 236 | return sock; |
| 237 | } |
| 238 | |
| 239 | /* Create new listen socket if needed */ |
| 240 | static int bareudp_socket_create(struct bareudp_dev *bareudp, __be16 port) |
| 241 | { |
| 242 | struct udp_tunnel_sock_cfg tunnel_cfg; |
| 243 | struct socket *sock; |
| 244 | |
| 245 | sock = bareudp_create_sock(bareudp->net, port); |
| 246 | if (IS_ERR(sock)) |
| 247 | return PTR_ERR(sock); |
| 248 | |
| 249 | /* Mark socket as an encapsulation socket */ |
| 250 | memset(&tunnel_cfg, 0, sizeof(tunnel_cfg)); |
| 251 | tunnel_cfg.sk_user_data = bareudp; |
| 252 | tunnel_cfg.encap_type = 1; |
| 253 | tunnel_cfg.encap_rcv = bareudp_udp_encap_recv; |
| 254 | tunnel_cfg.encap_err_lookup = bareudp_err_lookup; |
| 255 | tunnel_cfg.encap_destroy = NULL; |
| 256 | setup_udp_tunnel_sock(bareudp->net, sock, &tunnel_cfg); |
| 257 | |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 258 | rcu_assign_pointer(bareudp->sock, sock); |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | static int bareudp_open(struct net_device *dev) |
| 263 | { |
| 264 | struct bareudp_dev *bareudp = netdev_priv(dev); |
| 265 | int ret = 0; |
| 266 | |
| 267 | ret = bareudp_socket_create(bareudp, bareudp->port); |
| 268 | return ret; |
| 269 | } |
| 270 | |
| 271 | static void bareudp_sock_release(struct bareudp_dev *bareudp) |
| 272 | { |
| 273 | struct socket *sock; |
| 274 | |
| 275 | sock = bareudp->sock; |
| 276 | rcu_assign_pointer(bareudp->sock, NULL); |
| 277 | synchronize_net(); |
| 278 | udp_tunnel_sock_release(sock); |
| 279 | } |
| 280 | |
| 281 | static int bareudp_stop(struct net_device *dev) |
| 282 | { |
| 283 | struct bareudp_dev *bareudp = netdev_priv(dev); |
| 284 | |
| 285 | bareudp_sock_release(bareudp); |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | static int bareudp_xmit_skb(struct sk_buff *skb, struct net_device *dev, |
| 290 | struct bareudp_dev *bareudp, |
| 291 | const struct ip_tunnel_info *info) |
| 292 | { |
| 293 | bool xnet = !net_eq(bareudp->net, dev_net(bareudp->dev)); |
| 294 | bool use_cache = ip_tunnel_dst_cache_usable(skb, info); |
| 295 | struct socket *sock = rcu_dereference(bareudp->sock); |
| 296 | bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM); |
| 297 | const struct ip_tunnel_key *key = &info->key; |
| 298 | struct rtable *rt; |
| 299 | __be16 sport, df; |
| 300 | int min_headroom; |
| 301 | __u8 tos, ttl; |
| 302 | __be32 saddr; |
| 303 | int err; |
| 304 | |
| 305 | if (!sock) |
| 306 | return -ESHUTDOWN; |
| 307 | |
| 308 | rt = ip_route_output_tunnel(skb, dev, bareudp->net, &saddr, info, |
| 309 | IPPROTO_UDP, use_cache); |
| 310 | |
| 311 | if (IS_ERR(rt)) |
| 312 | return PTR_ERR(rt); |
| 313 | |
| 314 | skb_tunnel_check_pmtu(skb, &rt->dst, |
Stefano Brivio | 4cb47a8 | 2020-08-04 07:53:43 +0200 | [diff] [blame] | 315 | BAREUDP_IPV4_HLEN + info->options_len, false); |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 316 | |
| 317 | sport = udp_flow_src_port(bareudp->net, skb, |
| 318 | bareudp->sport_min, USHRT_MAX, |
| 319 | true); |
| 320 | tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb); |
| 321 | ttl = key->ttl; |
| 322 | df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0; |
| 323 | skb_scrub_packet(skb, xnet); |
| 324 | |
David S. Miller | c102b6f | 2020-02-24 14:40:54 -0800 | [diff] [blame] | 325 | err = -ENOSPC; |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 326 | if (!skb_pull(skb, skb_network_offset(skb))) |
| 327 | goto free_dst; |
| 328 | |
| 329 | min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len + |
| 330 | BAREUDP_BASE_HLEN + info->options_len + sizeof(struct iphdr); |
| 331 | |
| 332 | err = skb_cow_head(skb, min_headroom); |
| 333 | if (unlikely(err)) |
| 334 | goto free_dst; |
| 335 | |
| 336 | err = udp_tunnel_handle_offloads(skb, udp_sum); |
| 337 | if (err) |
| 338 | goto free_dst; |
| 339 | |
| 340 | skb_set_inner_protocol(skb, bareudp->ethertype); |
| 341 | udp_tunnel_xmit_skb(rt, sock->sk, skb, saddr, info->key.u.ipv4.dst, |
| 342 | tos, ttl, df, sport, bareudp->port, |
| 343 | !net_eq(bareudp->net, dev_net(bareudp->dev)), |
| 344 | !(info->key.tun_flags & TUNNEL_CSUM)); |
| 345 | return 0; |
| 346 | |
| 347 | free_dst: |
| 348 | dst_release(&rt->dst); |
| 349 | return err; |
| 350 | } |
| 351 | |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 352 | static int bareudp6_xmit_skb(struct sk_buff *skb, struct net_device *dev, |
| 353 | struct bareudp_dev *bareudp, |
| 354 | const struct ip_tunnel_info *info) |
| 355 | { |
| 356 | bool xnet = !net_eq(bareudp->net, dev_net(bareudp->dev)); |
| 357 | bool use_cache = ip_tunnel_dst_cache_usable(skb, info); |
| 358 | struct socket *sock = rcu_dereference(bareudp->sock); |
| 359 | bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM); |
| 360 | const struct ip_tunnel_key *key = &info->key; |
| 361 | struct dst_entry *dst = NULL; |
| 362 | struct in6_addr saddr, daddr; |
| 363 | int min_headroom; |
| 364 | __u8 prio, ttl; |
| 365 | __be16 sport; |
| 366 | int err; |
| 367 | |
| 368 | if (!sock) |
| 369 | return -ESHUTDOWN; |
| 370 | |
| 371 | dst = ip6_dst_lookup_tunnel(skb, dev, bareudp->net, sock, &saddr, info, |
| 372 | IPPROTO_UDP, use_cache); |
| 373 | if (IS_ERR(dst)) |
| 374 | return PTR_ERR(dst); |
| 375 | |
Stefano Brivio | 4cb47a8 | 2020-08-04 07:53:43 +0200 | [diff] [blame] | 376 | skb_tunnel_check_pmtu(skb, dst, BAREUDP_IPV6_HLEN + info->options_len, |
| 377 | false); |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 378 | |
| 379 | sport = udp_flow_src_port(bareudp->net, skb, |
| 380 | bareudp->sport_min, USHRT_MAX, |
| 381 | true); |
| 382 | prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb); |
| 383 | ttl = key->ttl; |
| 384 | |
| 385 | skb_scrub_packet(skb, xnet); |
| 386 | |
David S. Miller | c102b6f | 2020-02-24 14:40:54 -0800 | [diff] [blame] | 387 | err = -ENOSPC; |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 388 | if (!skb_pull(skb, skb_network_offset(skb))) |
| 389 | goto free_dst; |
| 390 | |
| 391 | min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len + |
Taehee Yoo | 10ad3e9 | 2020-12-28 15:21:46 +0000 | [diff] [blame] | 392 | BAREUDP_BASE_HLEN + info->options_len + sizeof(struct ipv6hdr); |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 393 | |
| 394 | err = skb_cow_head(skb, min_headroom); |
| 395 | if (unlikely(err)) |
| 396 | goto free_dst; |
| 397 | |
| 398 | err = udp_tunnel_handle_offloads(skb, udp_sum); |
| 399 | if (err) |
| 400 | goto free_dst; |
| 401 | |
| 402 | daddr = info->key.u.ipv6.dst; |
| 403 | udp_tunnel6_xmit_skb(dst, sock->sk, skb, dev, |
| 404 | &saddr, &daddr, prio, ttl, |
| 405 | info->key.label, sport, bareudp->port, |
| 406 | !(info->key.tun_flags & TUNNEL_CSUM)); |
| 407 | return 0; |
| 408 | |
| 409 | free_dst: |
| 410 | dst_release(dst); |
| 411 | return err; |
| 412 | } |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 413 | |
Guillaume Nault | 302d201 | 2020-07-25 15:06:47 +0200 | [diff] [blame] | 414 | static bool bareudp_proto_valid(struct bareudp_dev *bareudp, __be16 proto) |
| 415 | { |
| 416 | if (bareudp->ethertype == proto) |
| 417 | return true; |
| 418 | |
| 419 | if (!bareudp->multi_proto_mode) |
| 420 | return false; |
| 421 | |
| 422 | if (bareudp->ethertype == htons(ETH_P_MPLS_UC) && |
| 423 | proto == htons(ETH_P_MPLS_MC)) |
| 424 | return true; |
| 425 | |
| 426 | if (bareudp->ethertype == htons(ETH_P_IP) && |
| 427 | proto == htons(ETH_P_IPV6)) |
| 428 | return true; |
| 429 | |
| 430 | return false; |
| 431 | } |
| 432 | |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 433 | static netdev_tx_t bareudp_xmit(struct sk_buff *skb, struct net_device *dev) |
| 434 | { |
| 435 | struct bareudp_dev *bareudp = netdev_priv(dev); |
| 436 | struct ip_tunnel_info *info = NULL; |
| 437 | int err; |
| 438 | |
Guillaume Nault | 302d201 | 2020-07-25 15:06:47 +0200 | [diff] [blame] | 439 | if (!bareudp_proto_valid(bareudp, skb->protocol)) { |
| 440 | err = -EINVAL; |
| 441 | goto tx_error; |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | info = skb_tunnel_info(skb); |
| 445 | if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) { |
| 446 | err = -EINVAL; |
| 447 | goto tx_error; |
| 448 | } |
| 449 | |
| 450 | rcu_read_lock(); |
Arnd Bergmann | ee28755 | 2020-05-05 19:22:14 +0200 | [diff] [blame] | 451 | if (IS_ENABLED(CONFIG_IPV6) && info->mode & IP_TUNNEL_INFO_IPV6) |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 452 | err = bareudp6_xmit_skb(skb, dev, bareudp, info); |
| 453 | else |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 454 | err = bareudp_xmit_skb(skb, dev, bareudp, info); |
| 455 | |
| 456 | rcu_read_unlock(); |
| 457 | |
| 458 | if (likely(!err)) |
| 459 | return NETDEV_TX_OK; |
| 460 | tx_error: |
| 461 | dev_kfree_skb(skb); |
| 462 | |
| 463 | if (err == -ELOOP) |
| 464 | dev->stats.collisions++; |
| 465 | else if (err == -ENETUNREACH) |
| 466 | dev->stats.tx_carrier_errors++; |
| 467 | |
| 468 | dev->stats.tx_errors++; |
| 469 | return NETDEV_TX_OK; |
| 470 | } |
| 471 | |
| 472 | static int bareudp_fill_metadata_dst(struct net_device *dev, |
| 473 | struct sk_buff *skb) |
| 474 | { |
| 475 | struct ip_tunnel_info *info = skb_tunnel_info(skb); |
| 476 | struct bareudp_dev *bareudp = netdev_priv(dev); |
| 477 | bool use_cache; |
| 478 | |
| 479 | use_cache = ip_tunnel_dst_cache_usable(skb, info); |
| 480 | |
Arnd Bergmann | ee28755 | 2020-05-05 19:22:14 +0200 | [diff] [blame] | 481 | if (!IS_ENABLED(CONFIG_IPV6) || ip_tunnel_info_af(info) == AF_INET) { |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 482 | struct rtable *rt; |
| 483 | __be32 saddr; |
| 484 | |
| 485 | rt = ip_route_output_tunnel(skb, dev, bareudp->net, &saddr, |
| 486 | info, IPPROTO_UDP, use_cache); |
| 487 | if (IS_ERR(rt)) |
| 488 | return PTR_ERR(rt); |
| 489 | |
| 490 | ip_rt_put(rt); |
| 491 | info->key.u.ipv4.src = saddr; |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 492 | } else if (ip_tunnel_info_af(info) == AF_INET6) { |
| 493 | struct dst_entry *dst; |
| 494 | struct in6_addr saddr; |
| 495 | struct socket *sock = rcu_dereference(bareudp->sock); |
| 496 | |
| 497 | dst = ip6_dst_lookup_tunnel(skb, dev, bareudp->net, sock, |
| 498 | &saddr, info, IPPROTO_UDP, |
| 499 | use_cache); |
| 500 | if (IS_ERR(dst)) |
| 501 | return PTR_ERR(dst); |
| 502 | |
| 503 | dst_release(dst); |
| 504 | info->key.u.ipv6.src = saddr; |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 505 | } else { |
| 506 | return -EINVAL; |
| 507 | } |
| 508 | |
| 509 | info->key.tp_src = udp_flow_src_port(bareudp->net, skb, |
| 510 | bareudp->sport_min, |
| 511 | USHRT_MAX, true); |
| 512 | info->key.tp_dst = bareudp->port; |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | static const struct net_device_ops bareudp_netdev_ops = { |
| 517 | .ndo_init = bareudp_init, |
| 518 | .ndo_uninit = bareudp_uninit, |
| 519 | .ndo_open = bareudp_open, |
| 520 | .ndo_stop = bareudp_stop, |
| 521 | .ndo_start_xmit = bareudp_xmit, |
Heiner Kallweit | b220a4a | 2020-11-07 21:52:06 +0100 | [diff] [blame] | 522 | .ndo_get_stats64 = dev_get_tstats64, |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 523 | .ndo_fill_metadata_dst = bareudp_fill_metadata_dst, |
| 524 | }; |
| 525 | |
| 526 | static const struct nla_policy bareudp_policy[IFLA_BAREUDP_MAX + 1] = { |
| 527 | [IFLA_BAREUDP_PORT] = { .type = NLA_U16 }, |
| 528 | [IFLA_BAREUDP_ETHERTYPE] = { .type = NLA_U16 }, |
| 529 | [IFLA_BAREUDP_SRCPORT_MIN] = { .type = NLA_U16 }, |
Martin Varghese | 4b5f672 | 2020-02-24 10:58:35 +0530 | [diff] [blame] | 530 | [IFLA_BAREUDP_MULTIPROTO_MODE] = { .type = NLA_FLAG }, |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 531 | }; |
| 532 | |
| 533 | /* Info for udev, that this is a virtual tunnel endpoint */ |
Jonas Bonn | cec8599 | 2020-12-02 13:23:24 +0100 | [diff] [blame] | 534 | static const struct device_type bareudp_type = { |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 535 | .name = "bareudp", |
| 536 | }; |
| 537 | |
| 538 | /* Initialize the device structure. */ |
| 539 | static void bareudp_setup(struct net_device *dev) |
| 540 | { |
| 541 | dev->netdev_ops = &bareudp_netdev_ops; |
| 542 | dev->needs_free_netdev = true; |
| 543 | SET_NETDEV_DEVTYPE(dev, &bareudp_type); |
Xin Long | 3224dcf | 2021-01-15 17:47:47 +0800 | [diff] [blame] | 544 | dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST; |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 545 | dev->features |= NETIF_F_RXCSUM; |
Taehee Yoo | d9e4498 | 2020-12-28 15:21:36 +0000 | [diff] [blame] | 546 | dev->features |= NETIF_F_LLTX; |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 547 | dev->features |= NETIF_F_GSO_SOFTWARE; |
Xin Long | 3224dcf | 2021-01-15 17:47:47 +0800 | [diff] [blame] | 548 | dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST; |
| 549 | dev->hw_features |= NETIF_F_RXCSUM; |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 550 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; |
| 551 | dev->hard_header_len = 0; |
| 552 | dev->addr_len = 0; |
| 553 | dev->mtu = ETH_DATA_LEN; |
| 554 | dev->min_mtu = IPV4_MIN_MTU; |
| 555 | dev->max_mtu = IP_MAX_MTU - BAREUDP_BASE_HLEN; |
| 556 | dev->type = ARPHRD_NONE; |
| 557 | netif_keep_dst(dev); |
| 558 | dev->priv_flags |= IFF_NO_QUEUE; |
| 559 | dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; |
| 560 | } |
| 561 | |
| 562 | static int bareudp_validate(struct nlattr *tb[], struct nlattr *data[], |
| 563 | struct netlink_ext_ack *extack) |
| 564 | { |
| 565 | if (!data) { |
| 566 | NL_SET_ERR_MSG(extack, |
| 567 | "Not enough attributes provided to perform the operation"); |
| 568 | return -EINVAL; |
| 569 | } |
| 570 | return 0; |
| 571 | } |
| 572 | |
Taehee Yoo | c46a49a | 2020-03-08 01:19:17 +0000 | [diff] [blame] | 573 | static int bareudp2info(struct nlattr *data[], struct bareudp_conf *conf, |
| 574 | struct netlink_ext_ack *extack) |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 575 | { |
Martin | b15bb88 | 2020-06-16 11:18:58 +0530 | [diff] [blame] | 576 | memset(conf, 0, sizeof(*conf)); |
| 577 | |
Taehee Yoo | c46a49a | 2020-03-08 01:19:17 +0000 | [diff] [blame] | 578 | if (!data[IFLA_BAREUDP_PORT]) { |
| 579 | NL_SET_ERR_MSG(extack, "port not specified"); |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 580 | return -EINVAL; |
Taehee Yoo | c46a49a | 2020-03-08 01:19:17 +0000 | [diff] [blame] | 581 | } |
| 582 | if (!data[IFLA_BAREUDP_ETHERTYPE]) { |
| 583 | NL_SET_ERR_MSG(extack, "ethertype not specified"); |
| 584 | return -EINVAL; |
| 585 | } |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 586 | |
Jean Sacren | 5bd6632 | 2021-10-28 12:24:53 -0600 | [diff] [blame] | 587 | conf->port = nla_get_u16(data[IFLA_BAREUDP_PORT]); |
| 588 | conf->ethertype = nla_get_u16(data[IFLA_BAREUDP_ETHERTYPE]); |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 589 | |
| 590 | if (data[IFLA_BAREUDP_SRCPORT_MIN]) |
| 591 | conf->sport_min = nla_get_u16(data[IFLA_BAREUDP_SRCPORT_MIN]); |
| 592 | |
Martin | 4c98045 | 2020-06-17 22:30:23 +0530 | [diff] [blame] | 593 | if (data[IFLA_BAREUDP_MULTIPROTO_MODE]) |
| 594 | conf->multi_proto_mode = true; |
| 595 | |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | static struct bareudp_dev *bareudp_find_dev(struct bareudp_net *bn, |
| 600 | const struct bareudp_conf *conf) |
| 601 | { |
| 602 | struct bareudp_dev *bareudp, *t = NULL; |
| 603 | |
| 604 | list_for_each_entry(bareudp, &bn->bareudp_list, next) { |
| 605 | if (conf->port == bareudp->port) |
| 606 | t = bareudp; |
| 607 | } |
| 608 | return t; |
| 609 | } |
| 610 | |
| 611 | static int bareudp_configure(struct net *net, struct net_device *dev, |
Guillaume Nault | b4bffa4 | 2021-12-13 19:17:17 +0100 | [diff] [blame] | 612 | struct bareudp_conf *conf, |
| 613 | struct netlink_ext_ack *extack) |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 614 | { |
| 615 | struct bareudp_net *bn = net_generic(net, bareudp_net_id); |
| 616 | struct bareudp_dev *t, *bareudp = netdev_priv(dev); |
| 617 | int err; |
| 618 | |
| 619 | bareudp->net = net; |
| 620 | bareudp->dev = dev; |
| 621 | t = bareudp_find_dev(bn, conf); |
Guillaume Nault | b4bffa4 | 2021-12-13 19:17:17 +0100 | [diff] [blame] | 622 | if (t) { |
| 623 | NL_SET_ERR_MSG(extack, "Another bareudp device using the same port already exists"); |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 624 | return -EBUSY; |
Guillaume Nault | b4bffa4 | 2021-12-13 19:17:17 +0100 | [diff] [blame] | 625 | } |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 626 | |
Martin Varghese | 4b5f672 | 2020-02-24 10:58:35 +0530 | [diff] [blame] | 627 | if (conf->multi_proto_mode && |
| 628 | (conf->ethertype != htons(ETH_P_MPLS_UC) && |
Guillaume Nault | b4bffa4 | 2021-12-13 19:17:17 +0100 | [diff] [blame] | 629 | conf->ethertype != htons(ETH_P_IP))) { |
| 630 | NL_SET_ERR_MSG(extack, "Cannot set multiproto mode for this ethertype (only IPv4 and unicast MPLS are supported)"); |
Martin Varghese | 4b5f672 | 2020-02-24 10:58:35 +0530 | [diff] [blame] | 631 | return -EINVAL; |
Guillaume Nault | b4bffa4 | 2021-12-13 19:17:17 +0100 | [diff] [blame] | 632 | } |
Martin Varghese | 4b5f672 | 2020-02-24 10:58:35 +0530 | [diff] [blame] | 633 | |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 634 | bareudp->port = conf->port; |
| 635 | bareudp->ethertype = conf->ethertype; |
| 636 | bareudp->sport_min = conf->sport_min; |
Martin Varghese | 4b5f672 | 2020-02-24 10:58:35 +0530 | [diff] [blame] | 637 | bareudp->multi_proto_mode = conf->multi_proto_mode; |
Martin | fe80536 | 2020-06-28 23:18:23 +0530 | [diff] [blame] | 638 | |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 639 | err = register_netdevice(dev); |
| 640 | if (err) |
| 641 | return err; |
| 642 | |
| 643 | list_add(&bareudp->next, &bn->bareudp_list); |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | static int bareudp_link_config(struct net_device *dev, |
| 648 | struct nlattr *tb[]) |
| 649 | { |
| 650 | int err; |
| 651 | |
| 652 | if (tb[IFLA_MTU]) { |
| 653 | err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); |
| 654 | if (err) |
| 655 | return err; |
| 656 | } |
| 657 | return 0; |
| 658 | } |
| 659 | |
Jakub Kicinski | 94bcfdb | 2021-01-05 11:07:25 -0800 | [diff] [blame] | 660 | static void bareudp_dellink(struct net_device *dev, struct list_head *head) |
| 661 | { |
| 662 | struct bareudp_dev *bareudp = netdev_priv(dev); |
| 663 | |
| 664 | list_del(&bareudp->next); |
| 665 | unregister_netdevice_queue(dev, head); |
| 666 | } |
| 667 | |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 668 | static int bareudp_newlink(struct net *net, struct net_device *dev, |
| 669 | struct nlattr *tb[], struct nlattr *data[], |
| 670 | struct netlink_ext_ack *extack) |
| 671 | { |
| 672 | struct bareudp_conf conf; |
| 673 | int err; |
| 674 | |
Taehee Yoo | c46a49a | 2020-03-08 01:19:17 +0000 | [diff] [blame] | 675 | err = bareudp2info(data, &conf, extack); |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 676 | if (err) |
| 677 | return err; |
| 678 | |
Guillaume Nault | b4bffa4 | 2021-12-13 19:17:17 +0100 | [diff] [blame] | 679 | err = bareudp_configure(net, dev, &conf, extack); |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 680 | if (err) |
| 681 | return err; |
| 682 | |
| 683 | err = bareudp_link_config(dev, tb); |
| 684 | if (err) |
Jakub Kicinski | 94bcfdb | 2021-01-05 11:07:25 -0800 | [diff] [blame] | 685 | goto err_unconfig; |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 686 | |
| 687 | return 0; |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 688 | |
Jakub Kicinski | 94bcfdb | 2021-01-05 11:07:25 -0800 | [diff] [blame] | 689 | err_unconfig: |
Jakub Kicinski | 1d04ccb | 2021-01-10 21:29:22 -0800 | [diff] [blame] | 690 | bareudp_dellink(dev, NULL); |
Jakub Kicinski | 94bcfdb | 2021-01-05 11:07:25 -0800 | [diff] [blame] | 691 | return err; |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | static size_t bareudp_get_size(const struct net_device *dev) |
| 695 | { |
| 696 | return nla_total_size(sizeof(__be16)) + /* IFLA_BAREUDP_PORT */ |
| 697 | nla_total_size(sizeof(__be16)) + /* IFLA_BAREUDP_ETHERTYPE */ |
| 698 | nla_total_size(sizeof(__u16)) + /* IFLA_BAREUDP_SRCPORT_MIN */ |
Martin Varghese | 4b5f672 | 2020-02-24 10:58:35 +0530 | [diff] [blame] | 699 | nla_total_size(0) + /* IFLA_BAREUDP_MULTIPROTO_MODE */ |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 700 | 0; |
| 701 | } |
| 702 | |
| 703 | static int bareudp_fill_info(struct sk_buff *skb, const struct net_device *dev) |
| 704 | { |
| 705 | struct bareudp_dev *bareudp = netdev_priv(dev); |
| 706 | |
| 707 | if (nla_put_be16(skb, IFLA_BAREUDP_PORT, bareudp->port)) |
| 708 | goto nla_put_failure; |
| 709 | if (nla_put_be16(skb, IFLA_BAREUDP_ETHERTYPE, bareudp->ethertype)) |
| 710 | goto nla_put_failure; |
| 711 | if (nla_put_u16(skb, IFLA_BAREUDP_SRCPORT_MIN, bareudp->sport_min)) |
| 712 | goto nla_put_failure; |
Martin Varghese | 4b5f672 | 2020-02-24 10:58:35 +0530 | [diff] [blame] | 713 | if (bareudp->multi_proto_mode && |
| 714 | nla_put_flag(skb, IFLA_BAREUDP_MULTIPROTO_MODE)) |
| 715 | goto nla_put_failure; |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 716 | |
| 717 | return 0; |
| 718 | |
| 719 | nla_put_failure: |
| 720 | return -EMSGSIZE; |
| 721 | } |
| 722 | |
| 723 | static struct rtnl_link_ops bareudp_link_ops __read_mostly = { |
| 724 | .kind = "bareudp", |
| 725 | .maxtype = IFLA_BAREUDP_MAX, |
| 726 | .policy = bareudp_policy, |
| 727 | .priv_size = sizeof(struct bareudp_dev), |
| 728 | .setup = bareudp_setup, |
| 729 | .validate = bareudp_validate, |
| 730 | .newlink = bareudp_newlink, |
| 731 | .dellink = bareudp_dellink, |
| 732 | .get_size = bareudp_get_size, |
| 733 | .fill_info = bareudp_fill_info, |
| 734 | }; |
| 735 | |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 736 | static __net_init int bareudp_init_net(struct net *net) |
| 737 | { |
| 738 | struct bareudp_net *bn = net_generic(net, bareudp_net_id); |
| 739 | |
| 740 | INIT_LIST_HEAD(&bn->bareudp_list); |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | static void bareudp_destroy_tunnels(struct net *net, struct list_head *head) |
| 745 | { |
| 746 | struct bareudp_net *bn = net_generic(net, bareudp_net_id); |
| 747 | struct bareudp_dev *bareudp, *next; |
| 748 | |
| 749 | list_for_each_entry_safe(bareudp, next, &bn->bareudp_list, next) |
| 750 | unregister_netdevice_queue(bareudp->dev, head); |
| 751 | } |
| 752 | |
| 753 | static void __net_exit bareudp_exit_batch_net(struct list_head *net_list) |
| 754 | { |
| 755 | struct net *net; |
| 756 | LIST_HEAD(list); |
| 757 | |
| 758 | rtnl_lock(); |
| 759 | list_for_each_entry(net, net_list, exit_list) |
| 760 | bareudp_destroy_tunnels(net, &list); |
| 761 | |
| 762 | /* unregister the devices gathered above */ |
| 763 | unregister_netdevice_many(&list); |
| 764 | rtnl_unlock(); |
| 765 | } |
| 766 | |
| 767 | static struct pernet_operations bareudp_net_ops = { |
| 768 | .init = bareudp_init_net, |
| 769 | .exit_batch = bareudp_exit_batch_net, |
| 770 | .id = &bareudp_net_id, |
| 771 | .size = sizeof(struct bareudp_net), |
| 772 | }; |
| 773 | |
| 774 | static int __init bareudp_init_module(void) |
| 775 | { |
| 776 | int rc; |
| 777 | |
| 778 | rc = register_pernet_subsys(&bareudp_net_ops); |
| 779 | if (rc) |
| 780 | goto out1; |
| 781 | |
| 782 | rc = rtnl_link_register(&bareudp_link_ops); |
| 783 | if (rc) |
| 784 | goto out2; |
| 785 | |
| 786 | return 0; |
| 787 | out2: |
| 788 | unregister_pernet_subsys(&bareudp_net_ops); |
| 789 | out1: |
| 790 | return rc; |
| 791 | } |
| 792 | late_initcall(bareudp_init_module); |
| 793 | |
| 794 | static void __exit bareudp_cleanup_module(void) |
| 795 | { |
| 796 | rtnl_link_unregister(&bareudp_link_ops); |
| 797 | unregister_pernet_subsys(&bareudp_net_ops); |
| 798 | } |
| 799 | module_exit(bareudp_cleanup_module); |
| 800 | |
Taehee Yoo | eea45da | 2020-03-08 01:19:07 +0000 | [diff] [blame] | 801 | MODULE_ALIAS_RTNL_LINK("bareudp"); |
Martin Varghese | 571912c | 2020-02-24 10:57:50 +0530 | [diff] [blame] | 802 | MODULE_LICENSE("GPL"); |
| 803 | MODULE_AUTHOR("Martin Varghese <martin.varghese@nokia.com>"); |
| 804 | MODULE_DESCRIPTION("Interface driver for UDP encapsulated traffic"); |