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