| 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 | |
| 41 | struct bareudp_conf { |
| 42 | __be16 ethertype; |
| 43 | __be16 port; |
| 44 | u16 sport_min; |
| 45 | bool multi_proto_mode; |
| 46 | }; |
| 47 | |
| 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; |
| 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; |
| 59 | }; |
| 60 | |
| 61 | static int bareudp_udp_encap_recv(struct sock *sk, struct sk_buff *skb) |
| 62 | { |
| 63 | struct metadata_dst *tun_dst = NULL; |
| 64 | IP_TUNNEL_DECLARE_FLAGS(key) = { }; |
| 65 | struct bareudp_dev *bareudp; |
| 66 | unsigned short family; |
| 67 | unsigned int len; |
| 68 | __be16 proto; |
| 69 | void *oiph; |
| 70 | int err; |
| 71 | int nh; |
| 72 | |
| 73 | bareudp = rcu_dereference_sk_user_data(sk); |
| 74 | if (!bareudp) |
| 75 | goto drop; |
| 76 | |
| 77 | if (skb->protocol == htons(ETH_P_IP)) |
| 78 | family = AF_INET; |
| 79 | else |
| 80 | family = AF_INET6; |
| 81 | |
| 82 | if (bareudp->ethertype == htons(ETH_P_IP)) { |
| 83 | __u8 ipversion; |
| 84 | |
| 85 | if (skb_copy_bits(skb, BAREUDP_BASE_HLEN, to: &ipversion, |
| 86 | len: sizeof(ipversion))) { |
| 87 | dev_dstats_rx_dropped(dev: bareudp->dev); |
| 88 | goto drop; |
| 89 | } |
| 90 | ipversion >>= 4; |
| 91 | |
| 92 | if (ipversion == 4) { |
| 93 | proto = htons(ETH_P_IP); |
| 94 | } else if (ipversion == 6 && bareudp->multi_proto_mode) { |
| 95 | proto = htons(ETH_P_IPV6); |
| 96 | } else { |
| 97 | dev_dstats_rx_dropped(dev: bareudp->dev); |
| 98 | goto drop; |
| 99 | } |
| 100 | } else if (bareudp->ethertype == htons(ETH_P_MPLS_UC)) { |
| 101 | struct iphdr *tunnel_hdr; |
| 102 | |
| 103 | tunnel_hdr = (struct iphdr *)skb_network_header(skb); |
| 104 | if (tunnel_hdr->version == 4) { |
| 105 | if (!ipv4_is_multicast(addr: tunnel_hdr->daddr)) { |
| 106 | proto = bareudp->ethertype; |
| 107 | } else if (bareudp->multi_proto_mode && |
| 108 | ipv4_is_multicast(addr: tunnel_hdr->daddr)) { |
| 109 | proto = htons(ETH_P_MPLS_MC); |
| 110 | } else { |
| 111 | dev_dstats_rx_dropped(dev: bareudp->dev); |
| 112 | goto drop; |
| 113 | } |
| 114 | } else { |
| 115 | int addr_type; |
| 116 | struct ipv6hdr *tunnel_hdr_v6; |
| 117 | |
| 118 | tunnel_hdr_v6 = (struct ipv6hdr *)skb_network_header(skb); |
| 119 | addr_type = |
| 120 | ipv6_addr_type(addr: (struct in6_addr *)&tunnel_hdr_v6->daddr); |
| 121 | if (!(addr_type & IPV6_ADDR_MULTICAST)) { |
| 122 | proto = bareudp->ethertype; |
| 123 | } else if (bareudp->multi_proto_mode && |
| 124 | (addr_type & IPV6_ADDR_MULTICAST)) { |
| 125 | proto = htons(ETH_P_MPLS_MC); |
| 126 | } else { |
| 127 | dev_dstats_rx_dropped(dev: bareudp->dev); |
| 128 | goto drop; |
| 129 | } |
| 130 | } |
| 131 | } else { |
| 132 | proto = bareudp->ethertype; |
| 133 | } |
| 134 | |
| 135 | if (iptunnel_pull_header(skb, BAREUDP_BASE_HLEN, |
| 136 | inner_proto: proto, |
| 137 | xnet: !net_eq(net1: bareudp->net, |
| 138 | net2: dev_net(dev: bareudp->dev)))) { |
| 139 | dev_dstats_rx_dropped(dev: bareudp->dev); |
| 140 | goto drop; |
| 141 | } |
| 142 | |
| 143 | __set_bit(IP_TUNNEL_KEY_BIT, key); |
| 144 | |
| 145 | tun_dst = udp_tun_rx_dst(skb, family, flags: key, tunnel_id: 0, md_size: 0); |
| 146 | if (!tun_dst) { |
| 147 | dev_dstats_rx_dropped(dev: bareudp->dev); |
| 148 | goto drop; |
| 149 | } |
| 150 | skb_dst_set(skb, dst: &tun_dst->dst); |
| 151 | skb->dev = bareudp->dev; |
| 152 | skb_reset_mac_header(skb); |
| 153 | |
| 154 | /* Save offset of outer header relative to skb->head, |
| 155 | * because we are going to reset the network header to the inner header |
| 156 | * and might change skb->head. |
| 157 | */ |
| 158 | nh = skb_network_header(skb) - skb->head; |
| 159 | |
| 160 | skb_reset_network_header(skb); |
| 161 | |
| 162 | if (!pskb_inet_may_pull(skb)) { |
| 163 | DEV_STATS_INC(bareudp->dev, rx_length_errors); |
| 164 | DEV_STATS_INC(bareudp->dev, rx_errors); |
| 165 | goto drop; |
| 166 | } |
| 167 | |
| 168 | /* Get the outer header. */ |
| 169 | oiph = skb->head + nh; |
| 170 | |
| 171 | if (!ipv6_mod_enabled() || family == AF_INET) |
| 172 | err = IP_ECN_decapsulate(oiph, skb); |
| 173 | else |
| 174 | err = IP6_ECN_decapsulate(oipv6h: oiph, skb); |
| 175 | |
| 176 | if (unlikely(err)) { |
| 177 | if (log_ecn_error) { |
| 178 | if (!ipv6_mod_enabled() || family == AF_INET) |
| 179 | net_info_ratelimited("non-ECT from %pI4 " |
| 180 | "with TOS=%#x\n" , |
| 181 | &((struct iphdr *)oiph)->saddr, |
| 182 | ((struct iphdr *)oiph)->tos); |
| 183 | else |
| 184 | net_info_ratelimited("non-ECT from %pI6\n" , |
| 185 | &((struct ipv6hdr *)oiph)->saddr); |
| 186 | } |
| 187 | if (err > 1) { |
| 188 | DEV_STATS_INC(bareudp->dev, rx_frame_errors); |
| 189 | DEV_STATS_INC(bareudp->dev, rx_errors); |
| 190 | goto drop; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | len = skb->len; |
| 195 | err = gro_cells_receive(gcells: &bareudp->gro_cells, skb); |
| 196 | if (likely(err == NET_RX_SUCCESS)) |
| 197 | dev_dstats_rx_add(dev: bareudp->dev, len); |
| 198 | |
| 199 | return 0; |
| 200 | drop: |
| 201 | /* Consume bad packet */ |
| 202 | kfree_skb(skb); |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | static int bareudp_err_lookup(struct sock *sk, struct sk_buff *skb) |
| 208 | { |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static int bareudp_init(struct net_device *dev) |
| 213 | { |
| 214 | struct bareudp_dev *bareudp = netdev_priv(dev); |
| 215 | int err; |
| 216 | |
| 217 | err = gro_cells_init(gcells: &bareudp->gro_cells, dev); |
| 218 | if (err) |
| 219 | return err; |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | static void bareudp_uninit(struct net_device *dev) |
| 225 | { |
| 226 | struct bareudp_dev *bareudp = netdev_priv(dev); |
| 227 | |
| 228 | gro_cells_destroy(gcells: &bareudp->gro_cells); |
| 229 | } |
| 230 | |
| 231 | static struct socket *bareudp_create_sock(struct net *net, __be16 port) |
| 232 | { |
| 233 | struct udp_port_cfg udp_conf; |
| 234 | struct socket *sock; |
| 235 | int err; |
| 236 | |
| 237 | memset(&udp_conf, 0, sizeof(udp_conf)); |
| 238 | |
| 239 | if (ipv6_mod_enabled()) |
| 240 | udp_conf.family = AF_INET6; |
| 241 | else |
| 242 | udp_conf.family = AF_INET; |
| 243 | |
| 244 | udp_conf.local_udp_port = port; |
| 245 | /* Open UDP socket */ |
| 246 | err = udp_sock_create(net, cfg: &udp_conf, sockp: &sock); |
| 247 | if (err < 0) |
| 248 | return ERR_PTR(error: err); |
| 249 | |
| 250 | udp_allow_gso(sk: sock->sk); |
| 251 | return sock; |
| 252 | } |
| 253 | |
| 254 | /* Create new listen socket if needed */ |
| 255 | static int bareudp_socket_create(struct bareudp_dev *bareudp, __be16 port) |
| 256 | { |
| 257 | struct udp_tunnel_sock_cfg tunnel_cfg; |
| 258 | struct socket *sock; |
| 259 | |
| 260 | sock = bareudp_create_sock(net: bareudp->net, port); |
| 261 | if (IS_ERR(ptr: sock)) |
| 262 | return PTR_ERR(ptr: sock); |
| 263 | |
| 264 | /* Mark socket as an encapsulation socket */ |
| 265 | memset(&tunnel_cfg, 0, sizeof(tunnel_cfg)); |
| 266 | tunnel_cfg.sk_user_data = bareudp; |
| 267 | tunnel_cfg.encap_type = 1; |
| 268 | tunnel_cfg.encap_rcv = bareudp_udp_encap_recv; |
| 269 | tunnel_cfg.encap_err_lookup = bareudp_err_lookup; |
| 270 | tunnel_cfg.encap_destroy = NULL; |
| 271 | setup_udp_tunnel_sock(net: bareudp->net, sock, sock_cfg: &tunnel_cfg); |
| 272 | |
| 273 | rcu_assign_pointer(bareudp->sock, sock); |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | static int bareudp_open(struct net_device *dev) |
| 278 | { |
| 279 | struct bareudp_dev *bareudp = netdev_priv(dev); |
| 280 | int ret = 0; |
| 281 | |
| 282 | ret = bareudp_socket_create(bareudp, port: bareudp->port); |
| 283 | return ret; |
| 284 | } |
| 285 | |
| 286 | static void bareudp_sock_release(struct bareudp_dev *bareudp) |
| 287 | { |
| 288 | struct socket *sock; |
| 289 | |
| 290 | sock = bareudp->sock; |
| 291 | rcu_assign_pointer(bareudp->sock, NULL); |
| 292 | synchronize_net(); |
| 293 | udp_tunnel_sock_release(sock); |
| 294 | } |
| 295 | |
| 296 | static int bareudp_stop(struct net_device *dev) |
| 297 | { |
| 298 | struct bareudp_dev *bareudp = netdev_priv(dev); |
| 299 | |
| 300 | bareudp_sock_release(bareudp); |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | static int bareudp_xmit_skb(struct sk_buff *skb, struct net_device *dev, |
| 305 | struct bareudp_dev *bareudp, |
| 306 | const struct ip_tunnel_info *info) |
| 307 | { |
| 308 | bool udp_sum = test_bit(IP_TUNNEL_CSUM_BIT, info->key.tun_flags); |
| 309 | bool xnet = !net_eq(net1: bareudp->net, net2: dev_net(dev: bareudp->dev)); |
| 310 | bool use_cache = ip_tunnel_dst_cache_usable(skb, info); |
| 311 | struct socket *sock = rcu_dereference(bareudp->sock); |
| 312 | const struct ip_tunnel_key *key = &info->key; |
| 313 | struct rtable *rt; |
| 314 | __be16 sport, df; |
| 315 | int min_headroom; |
| 316 | __u8 tos, ttl; |
| 317 | __be32 saddr; |
| 318 | int err; |
| 319 | |
| 320 | if (skb_vlan_inet_prepare(skb, inner_proto_inherit: skb->protocol != htons(ETH_P_TEB))) |
| 321 | return -EINVAL; |
| 322 | |
| 323 | if (!sock) |
| 324 | return -ESHUTDOWN; |
| 325 | |
| 326 | sport = udp_flow_src_port(net: bareudp->net, skb, |
| 327 | min: bareudp->sport_min, USHRT_MAX, |
| 328 | use_eth: true); |
| 329 | rt = udp_tunnel_dst_lookup(skb, dev, net: bareudp->net, oif: 0, saddr: &saddr, key: &info->key, |
| 330 | sport, dport: bareudp->port, tos: key->tos, |
| 331 | dst_cache: use_cache ? |
| 332 | (struct dst_cache *)&info->dst_cache : NULL); |
| 333 | |
| 334 | if (IS_ERR(ptr: rt)) |
| 335 | return PTR_ERR(ptr: rt); |
| 336 | |
| 337 | skb_tunnel_check_pmtu(skb, encap_dst: &rt->dst, |
| 338 | BAREUDP_IPV4_HLEN + info->options_len, reply: false); |
| 339 | |
| 340 | tos = ip_tunnel_ecn_encap(tos: key->tos, iph: ip_hdr(skb), skb); |
| 341 | ttl = key->ttl; |
| 342 | df = test_bit(IP_TUNNEL_DONT_FRAGMENT_BIT, key->tun_flags) ? |
| 343 | htons(IP_DF) : 0; |
| 344 | skb_scrub_packet(skb, xnet); |
| 345 | |
| 346 | err = -ENOSPC; |
| 347 | if (!skb_pull(skb, len: skb_network_offset(skb))) |
| 348 | goto free_dst; |
| 349 | |
| 350 | min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len + |
| 351 | BAREUDP_BASE_HLEN + info->options_len + sizeof(struct iphdr); |
| 352 | |
| 353 | err = skb_cow_head(skb, headroom: min_headroom); |
| 354 | if (unlikely(err)) |
| 355 | goto free_dst; |
| 356 | |
| 357 | err = udp_tunnel_handle_offloads(skb, udp_csum: udp_sum); |
| 358 | if (err) |
| 359 | goto free_dst; |
| 360 | |
| 361 | skb_set_inner_protocol(skb, protocol: bareudp->ethertype); |
| 362 | udp_tunnel_xmit_skb(rt, sk: sock->sk, skb, src: saddr, dst: info->key.u.ipv4.dst, |
| 363 | tos, ttl, df, src_port: sport, dst_port: bareudp->port, |
| 364 | xnet: !net_eq(net1: bareudp->net, net2: dev_net(dev: bareudp->dev)), |
| 365 | nocheck: !test_bit(IP_TUNNEL_CSUM_BIT, |
| 366 | info->key.tun_flags)); |
| 367 | return 0; |
| 368 | |
| 369 | free_dst: |
| 370 | dst_release(dst: &rt->dst); |
| 371 | return err; |
| 372 | } |
| 373 | |
| 374 | static int bareudp6_xmit_skb(struct sk_buff *skb, struct net_device *dev, |
| 375 | struct bareudp_dev *bareudp, |
| 376 | const struct ip_tunnel_info *info) |
| 377 | { |
| 378 | bool udp_sum = test_bit(IP_TUNNEL_CSUM_BIT, info->key.tun_flags); |
| 379 | bool xnet = !net_eq(net1: bareudp->net, net2: dev_net(dev: bareudp->dev)); |
| 380 | bool use_cache = ip_tunnel_dst_cache_usable(skb, info); |
| 381 | struct socket *sock = rcu_dereference(bareudp->sock); |
| 382 | const struct ip_tunnel_key *key = &info->key; |
| 383 | struct dst_entry *dst = NULL; |
| 384 | struct in6_addr saddr, daddr; |
| 385 | int min_headroom; |
| 386 | __u8 prio, ttl; |
| 387 | __be16 sport; |
| 388 | int err; |
| 389 | |
| 390 | if (skb_vlan_inet_prepare(skb, inner_proto_inherit: skb->protocol != htons(ETH_P_TEB))) |
| 391 | return -EINVAL; |
| 392 | |
| 393 | if (!sock) |
| 394 | return -ESHUTDOWN; |
| 395 | |
| 396 | sport = udp_flow_src_port(net: bareudp->net, skb, |
| 397 | min: bareudp->sport_min, USHRT_MAX, |
| 398 | use_eth: true); |
| 399 | dst = udp_tunnel6_dst_lookup(skb, dev, net: bareudp->net, sock, oif: 0, saddr: &saddr, |
| 400 | key, sport, dport: bareudp->port, dsfield: key->tos, |
| 401 | dst_cache: use_cache ? |
| 402 | (struct dst_cache *) &info->dst_cache : NULL); |
| 403 | if (IS_ERR(ptr: dst)) |
| 404 | return PTR_ERR(ptr: dst); |
| 405 | |
| 406 | skb_tunnel_check_pmtu(skb, encap_dst: dst, BAREUDP_IPV6_HLEN + info->options_len, |
| 407 | reply: false); |
| 408 | |
| 409 | prio = ip_tunnel_ecn_encap(tos: key->tos, iph: ip_hdr(skb), skb); |
| 410 | ttl = key->ttl; |
| 411 | |
| 412 | skb_scrub_packet(skb, xnet); |
| 413 | |
| 414 | err = -ENOSPC; |
| 415 | if (!skb_pull(skb, len: skb_network_offset(skb))) |
| 416 | goto free_dst; |
| 417 | |
| 418 | min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len + |
| 419 | BAREUDP_BASE_HLEN + info->options_len + sizeof(struct ipv6hdr); |
| 420 | |
| 421 | err = skb_cow_head(skb, headroom: min_headroom); |
| 422 | if (unlikely(err)) |
| 423 | goto free_dst; |
| 424 | |
| 425 | err = udp_tunnel_handle_offloads(skb, udp_csum: udp_sum); |
| 426 | if (err) |
| 427 | goto free_dst; |
| 428 | |
| 429 | daddr = info->key.u.ipv6.dst; |
| 430 | udp_tunnel6_xmit_skb(dst, sk: sock->sk, skb, dev, |
| 431 | saddr: &saddr, daddr: &daddr, prio, ttl, |
| 432 | label: info->key.label, src_port: sport, dst_port: bareudp->port, |
| 433 | nocheck: !test_bit(IP_TUNNEL_CSUM_BIT, |
| 434 | info->key.tun_flags)); |
| 435 | return 0; |
| 436 | |
| 437 | free_dst: |
| 438 | dst_release(dst); |
| 439 | return err; |
| 440 | } |
| 441 | |
| 442 | static bool bareudp_proto_valid(struct bareudp_dev *bareudp, __be16 proto) |
| 443 | { |
| 444 | if (bareudp->ethertype == proto) |
| 445 | return true; |
| 446 | |
| 447 | if (!bareudp->multi_proto_mode) |
| 448 | return false; |
| 449 | |
| 450 | if (bareudp->ethertype == htons(ETH_P_MPLS_UC) && |
| 451 | proto == htons(ETH_P_MPLS_MC)) |
| 452 | return true; |
| 453 | |
| 454 | if (bareudp->ethertype == htons(ETH_P_IP) && |
| 455 | proto == htons(ETH_P_IPV6)) |
| 456 | return true; |
| 457 | |
| 458 | return false; |
| 459 | } |
| 460 | |
| 461 | static netdev_tx_t bareudp_xmit(struct sk_buff *skb, struct net_device *dev) |
| 462 | { |
| 463 | struct bareudp_dev *bareudp = netdev_priv(dev); |
| 464 | struct ip_tunnel_info *info = NULL; |
| 465 | int err; |
| 466 | |
| 467 | if (!bareudp_proto_valid(bareudp, proto: skb->protocol)) { |
| 468 | err = -EINVAL; |
| 469 | goto tx_error; |
| 470 | } |
| 471 | |
| 472 | info = skb_tunnel_info(skb); |
| 473 | if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) { |
| 474 | err = -EINVAL; |
| 475 | goto tx_error; |
| 476 | } |
| 477 | |
| 478 | rcu_read_lock(); |
| 479 | if (ipv6_mod_enabled() && info->mode & IP_TUNNEL_INFO_IPV6) |
| 480 | err = bareudp6_xmit_skb(skb, dev, bareudp, info); |
| 481 | else |
| 482 | err = bareudp_xmit_skb(skb, dev, bareudp, info); |
| 483 | |
| 484 | rcu_read_unlock(); |
| 485 | |
| 486 | if (likely(!err)) |
| 487 | return NETDEV_TX_OK; |
| 488 | tx_error: |
| 489 | dev_kfree_skb(skb); |
| 490 | |
| 491 | if (err == -ELOOP) |
| 492 | DEV_STATS_INC(dev, collisions); |
| 493 | else if (err == -ENETUNREACH) |
| 494 | DEV_STATS_INC(dev, tx_carrier_errors); |
| 495 | |
| 496 | DEV_STATS_INC(dev, tx_errors); |
| 497 | return NETDEV_TX_OK; |
| 498 | } |
| 499 | |
| 500 | static int bareudp_fill_metadata_dst(struct net_device *dev, |
| 501 | struct sk_buff *skb) |
| 502 | { |
| 503 | struct ip_tunnel_info *info = skb_tunnel_info(skb); |
| 504 | struct bareudp_dev *bareudp = netdev_priv(dev); |
| 505 | bool use_cache; |
| 506 | __be16 sport; |
| 507 | |
| 508 | use_cache = ip_tunnel_dst_cache_usable(skb, info); |
| 509 | sport = udp_flow_src_port(net: bareudp->net, skb, |
| 510 | min: bareudp->sport_min, USHRT_MAX, |
| 511 | use_eth: true); |
| 512 | |
| 513 | if (!ipv6_mod_enabled() || ip_tunnel_info_af(tun_info: info) == AF_INET) { |
| 514 | struct rtable *rt; |
| 515 | __be32 saddr; |
| 516 | |
| 517 | rt = udp_tunnel_dst_lookup(skb, dev, net: bareudp->net, oif: 0, saddr: &saddr, |
| 518 | key: &info->key, sport, dport: bareudp->port, |
| 519 | tos: info->key.tos, |
| 520 | dst_cache: use_cache ? &info->dst_cache : NULL); |
| 521 | if (IS_ERR(ptr: rt)) |
| 522 | return PTR_ERR(ptr: rt); |
| 523 | |
| 524 | ip_rt_put(rt); |
| 525 | info->key.u.ipv4.src = saddr; |
| 526 | } else if (ip_tunnel_info_af(tun_info: info) == AF_INET6) { |
| 527 | struct dst_entry *dst; |
| 528 | struct in6_addr saddr; |
| 529 | struct socket *sock = rcu_dereference(bareudp->sock); |
| 530 | |
| 531 | dst = udp_tunnel6_dst_lookup(skb, dev, net: bareudp->net, sock, |
| 532 | oif: 0, saddr: &saddr, key: &info->key, |
| 533 | sport, dport: bareudp->port, dsfield: info->key.tos, |
| 534 | dst_cache: use_cache ? &info->dst_cache : NULL); |
| 535 | if (IS_ERR(ptr: dst)) |
| 536 | return PTR_ERR(ptr: dst); |
| 537 | |
| 538 | dst_release(dst); |
| 539 | info->key.u.ipv6.src = saddr; |
| 540 | } else { |
| 541 | return -EINVAL; |
| 542 | } |
| 543 | |
| 544 | info->key.tp_src = sport; |
| 545 | info->key.tp_dst = bareudp->port; |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | static const struct net_device_ops bareudp_netdev_ops = { |
| 550 | .ndo_init = bareudp_init, |
| 551 | .ndo_uninit = bareudp_uninit, |
| 552 | .ndo_open = bareudp_open, |
| 553 | .ndo_stop = bareudp_stop, |
| 554 | .ndo_start_xmit = bareudp_xmit, |
| 555 | .ndo_fill_metadata_dst = bareudp_fill_metadata_dst, |
| 556 | }; |
| 557 | |
| 558 | static const struct nla_policy bareudp_policy[IFLA_BAREUDP_MAX + 1] = { |
| 559 | [IFLA_BAREUDP_PORT] = { .type = NLA_U16 }, |
| 560 | [IFLA_BAREUDP_ETHERTYPE] = { .type = NLA_U16 }, |
| 561 | [IFLA_BAREUDP_SRCPORT_MIN] = { .type = NLA_U16 }, |
| 562 | [IFLA_BAREUDP_MULTIPROTO_MODE] = { .type = NLA_FLAG }, |
| 563 | }; |
| 564 | |
| 565 | /* Info for udev, that this is a virtual tunnel endpoint */ |
| 566 | static const struct device_type bareudp_type = { |
| 567 | .name = "bareudp" , |
| 568 | }; |
| 569 | |
| 570 | /* Initialize the device structure. */ |
| 571 | static void bareudp_setup(struct net_device *dev) |
| 572 | { |
| 573 | dev->netdev_ops = &bareudp_netdev_ops; |
| 574 | dev->needs_free_netdev = true; |
| 575 | SET_NETDEV_DEVTYPE(dev, &bareudp_type); |
| 576 | dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST; |
| 577 | dev->features |= NETIF_F_RXCSUM; |
| 578 | dev->features |= NETIF_F_GSO_SOFTWARE; |
| 579 | dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST; |
| 580 | dev->hw_features |= NETIF_F_RXCSUM; |
| 581 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; |
| 582 | dev->hard_header_len = 0; |
| 583 | dev->addr_len = 0; |
| 584 | dev->mtu = ETH_DATA_LEN; |
| 585 | dev->min_mtu = IPV4_MIN_MTU; |
| 586 | dev->max_mtu = IP_MAX_MTU - BAREUDP_BASE_HLEN; |
| 587 | dev->type = ARPHRD_NONE; |
| 588 | netif_keep_dst(dev); |
| 589 | dev->priv_flags |= IFF_NO_QUEUE; |
| 590 | dev->lltx = true; |
| 591 | dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; |
| 592 | dev->pcpu_stat_type = NETDEV_PCPU_STAT_DSTATS; |
| 593 | } |
| 594 | |
| 595 | static int bareudp_validate(struct nlattr *tb[], struct nlattr *data[], |
| 596 | struct netlink_ext_ack *extack) |
| 597 | { |
| 598 | if (!data) { |
| 599 | NL_SET_ERR_MSG(extack, |
| 600 | "Not enough attributes provided to perform the operation" ); |
| 601 | return -EINVAL; |
| 602 | } |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | static int bareudp2info(struct nlattr *data[], struct bareudp_conf *conf, |
| 607 | struct netlink_ext_ack *extack) |
| 608 | { |
| 609 | memset(conf, 0, sizeof(*conf)); |
| 610 | |
| 611 | if (!data[IFLA_BAREUDP_PORT]) { |
| 612 | NL_SET_ERR_MSG(extack, "port not specified" ); |
| 613 | return -EINVAL; |
| 614 | } |
| 615 | if (!data[IFLA_BAREUDP_ETHERTYPE]) { |
| 616 | NL_SET_ERR_MSG(extack, "ethertype not specified" ); |
| 617 | return -EINVAL; |
| 618 | } |
| 619 | |
| 620 | conf->port = nla_get_u16(nla: data[IFLA_BAREUDP_PORT]); |
| 621 | conf->ethertype = nla_get_u16(nla: data[IFLA_BAREUDP_ETHERTYPE]); |
| 622 | |
| 623 | if (data[IFLA_BAREUDP_SRCPORT_MIN]) |
| 624 | conf->sport_min = nla_get_u16(nla: data[IFLA_BAREUDP_SRCPORT_MIN]); |
| 625 | |
| 626 | if (data[IFLA_BAREUDP_MULTIPROTO_MODE]) |
| 627 | conf->multi_proto_mode = true; |
| 628 | |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | static struct bareudp_dev *bareudp_find_dev(struct bareudp_net *bn, |
| 633 | const struct bareudp_conf *conf) |
| 634 | { |
| 635 | struct bareudp_dev *bareudp, *t = NULL; |
| 636 | |
| 637 | list_for_each_entry(bareudp, &bn->bareudp_list, next) { |
| 638 | if (conf->port == bareudp->port) |
| 639 | t = bareudp; |
| 640 | } |
| 641 | return t; |
| 642 | } |
| 643 | |
| 644 | static int bareudp_configure(struct net *net, struct net_device *dev, |
| 645 | struct bareudp_conf *conf, |
| 646 | struct netlink_ext_ack *extack) |
| 647 | { |
| 648 | struct bareudp_net *bn = net_generic(net, id: bareudp_net_id); |
| 649 | struct bareudp_dev *t, *bareudp = netdev_priv(dev); |
| 650 | int err; |
| 651 | |
| 652 | bareudp->net = net; |
| 653 | bareudp->dev = dev; |
| 654 | t = bareudp_find_dev(bn, conf); |
| 655 | if (t) { |
| 656 | NL_SET_ERR_MSG(extack, "Another bareudp device using the same port already exists" ); |
| 657 | return -EBUSY; |
| 658 | } |
| 659 | |
| 660 | if (conf->multi_proto_mode && |
| 661 | (conf->ethertype != htons(ETH_P_MPLS_UC) && |
| 662 | conf->ethertype != htons(ETH_P_IP))) { |
| 663 | NL_SET_ERR_MSG(extack, "Cannot set multiproto mode for this ethertype (only IPv4 and unicast MPLS are supported)" ); |
| 664 | return -EINVAL; |
| 665 | } |
| 666 | |
| 667 | bareudp->port = conf->port; |
| 668 | bareudp->ethertype = conf->ethertype; |
| 669 | bareudp->sport_min = conf->sport_min; |
| 670 | bareudp->multi_proto_mode = conf->multi_proto_mode; |
| 671 | |
| 672 | err = register_netdevice(dev); |
| 673 | if (err) |
| 674 | return err; |
| 675 | |
| 676 | list_add(new: &bareudp->next, head: &bn->bareudp_list); |
| 677 | return 0; |
| 678 | } |
| 679 | |
| 680 | static int bareudp_link_config(struct net_device *dev, |
| 681 | struct nlattr *tb[]) |
| 682 | { |
| 683 | int err; |
| 684 | |
| 685 | if (tb[IFLA_MTU]) { |
| 686 | err = dev_set_mtu(dev, nla_get_u32(nla: tb[IFLA_MTU])); |
| 687 | if (err) |
| 688 | return err; |
| 689 | } |
| 690 | return 0; |
| 691 | } |
| 692 | |
| 693 | static void bareudp_dellink(struct net_device *dev, struct list_head *head) |
| 694 | { |
| 695 | struct bareudp_dev *bareudp = netdev_priv(dev); |
| 696 | |
| 697 | list_del(entry: &bareudp->next); |
| 698 | unregister_netdevice_queue(dev, head); |
| 699 | } |
| 700 | |
| 701 | static int bareudp_newlink(struct net_device *dev, |
| 702 | struct rtnl_newlink_params *params, |
| 703 | struct netlink_ext_ack *extack) |
| 704 | { |
| 705 | struct net *link_net = rtnl_newlink_link_net(p: params); |
| 706 | struct nlattr **data = params->data; |
| 707 | struct nlattr **tb = params->tb; |
| 708 | struct bareudp_conf conf; |
| 709 | int err; |
| 710 | |
| 711 | err = bareudp2info(data, conf: &conf, extack); |
| 712 | if (err) |
| 713 | return err; |
| 714 | |
| 715 | err = bareudp_configure(net: link_net, dev, conf: &conf, extack); |
| 716 | if (err) |
| 717 | return err; |
| 718 | |
| 719 | err = bareudp_link_config(dev, tb); |
| 720 | if (err) |
| 721 | goto err_unconfig; |
| 722 | |
| 723 | return 0; |
| 724 | |
| 725 | err_unconfig: |
| 726 | bareudp_dellink(dev, NULL); |
| 727 | return err; |
| 728 | } |
| 729 | |
| 730 | static size_t bareudp_get_size(const struct net_device *dev) |
| 731 | { |
| 732 | return nla_total_size(payload: sizeof(__be16)) + /* IFLA_BAREUDP_PORT */ |
| 733 | nla_total_size(payload: sizeof(__be16)) + /* IFLA_BAREUDP_ETHERTYPE */ |
| 734 | nla_total_size(payload: sizeof(__u16)) + /* IFLA_BAREUDP_SRCPORT_MIN */ |
| 735 | nla_total_size(payload: 0) + /* IFLA_BAREUDP_MULTIPROTO_MODE */ |
| 736 | 0; |
| 737 | } |
| 738 | |
| 739 | static int bareudp_fill_info(struct sk_buff *skb, const struct net_device *dev) |
| 740 | { |
| 741 | struct bareudp_dev *bareudp = netdev_priv(dev); |
| 742 | |
| 743 | if (nla_put_be16(skb, attrtype: IFLA_BAREUDP_PORT, value: bareudp->port)) |
| 744 | goto nla_put_failure; |
| 745 | if (nla_put_be16(skb, attrtype: IFLA_BAREUDP_ETHERTYPE, value: bareudp->ethertype)) |
| 746 | goto nla_put_failure; |
| 747 | if (nla_put_u16(skb, attrtype: IFLA_BAREUDP_SRCPORT_MIN, value: bareudp->sport_min)) |
| 748 | goto nla_put_failure; |
| 749 | if (bareudp->multi_proto_mode && |
| 750 | nla_put_flag(skb, attrtype: IFLA_BAREUDP_MULTIPROTO_MODE)) |
| 751 | goto nla_put_failure; |
| 752 | |
| 753 | return 0; |
| 754 | |
| 755 | nla_put_failure: |
| 756 | return -EMSGSIZE; |
| 757 | } |
| 758 | |
| 759 | static struct rtnl_link_ops bareudp_link_ops __read_mostly = { |
| 760 | .kind = "bareudp" , |
| 761 | .maxtype = IFLA_BAREUDP_MAX, |
| 762 | .policy = bareudp_policy, |
| 763 | .priv_size = sizeof(struct bareudp_dev), |
| 764 | .setup = bareudp_setup, |
| 765 | .validate = bareudp_validate, |
| 766 | .newlink = bareudp_newlink, |
| 767 | .dellink = bareudp_dellink, |
| 768 | .get_size = bareudp_get_size, |
| 769 | .fill_info = bareudp_fill_info, |
| 770 | }; |
| 771 | |
| 772 | static __net_init int bareudp_init_net(struct net *net) |
| 773 | { |
| 774 | struct bareudp_net *bn = net_generic(net, id: bareudp_net_id); |
| 775 | |
| 776 | INIT_LIST_HEAD(list: &bn->bareudp_list); |
| 777 | return 0; |
| 778 | } |
| 779 | |
| 780 | static void __net_exit bareudp_exit_rtnl_net(struct net *net, |
| 781 | struct list_head *dev_kill_list) |
| 782 | { |
| 783 | struct bareudp_net *bn = net_generic(net, id: bareudp_net_id); |
| 784 | struct bareudp_dev *bareudp, *next; |
| 785 | |
| 786 | list_for_each_entry_safe(bareudp, next, &bn->bareudp_list, next) |
| 787 | bareudp_dellink(dev: bareudp->dev, head: dev_kill_list); |
| 788 | } |
| 789 | |
| 790 | static struct pernet_operations bareudp_net_ops = { |
| 791 | .init = bareudp_init_net, |
| 792 | .exit_rtnl = bareudp_exit_rtnl_net, |
| 793 | .id = &bareudp_net_id, |
| 794 | .size = sizeof(struct bareudp_net), |
| 795 | }; |
| 796 | |
| 797 | static int __init bareudp_init_module(void) |
| 798 | { |
| 799 | int rc; |
| 800 | |
| 801 | rc = register_pernet_subsys(&bareudp_net_ops); |
| 802 | if (rc) |
| 803 | goto out1; |
| 804 | |
| 805 | rc = rtnl_link_register(ops: &bareudp_link_ops); |
| 806 | if (rc) |
| 807 | goto out2; |
| 808 | |
| 809 | return 0; |
| 810 | out2: |
| 811 | unregister_pernet_subsys(&bareudp_net_ops); |
| 812 | out1: |
| 813 | return rc; |
| 814 | } |
| 815 | late_initcall(bareudp_init_module); |
| 816 | |
| 817 | static void __exit bareudp_cleanup_module(void) |
| 818 | { |
| 819 | rtnl_link_unregister(ops: &bareudp_link_ops); |
| 820 | unregister_pernet_subsys(&bareudp_net_ops); |
| 821 | } |
| 822 | module_exit(bareudp_cleanup_module); |
| 823 | |
| 824 | MODULE_ALIAS_RTNL_LINK("bareudp" ); |
| 825 | MODULE_LICENSE("GPL" ); |
| 826 | MODULE_AUTHOR("Martin Varghese <martin.varghese@nokia.com>" ); |
| 827 | MODULE_DESCRIPTION("Interface driver for UDP encapsulated traffic" ); |
| 828 | |