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