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 sock __rcu *sk; 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, &ipversion, 86 sizeof(ipversion))) { 87 dev_dstats_rx_dropped(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(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(tunnel_hdr->daddr)) { 106 proto = bareudp->ethertype; 107 } else if (bareudp->multi_proto_mode && 108 ipv4_is_multicast(tunnel_hdr->daddr)) { 109 proto = htons(ETH_P_MPLS_MC); 110 } else { 111 dev_dstats_rx_dropped(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((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(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 proto, 137 !net_eq(bareudp->net, 138 dev_net(bareudp->dev)))) { 139 dev_dstats_rx_dropped(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, key, 0, 0); 146 if (!tun_dst) { 147 dev_dstats_rx_dropped(bareudp->dev); 148 goto drop; 149 } 150 skb_dst_set(skb, &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(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(&bareudp->gro_cells, skb); 196 if (likely(err == NET_RX_SUCCESS)) 197 dev_dstats_rx_add(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(&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(&bareudp->gro_cells); 229 } 230 231 static struct sock *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, &udp_conf, &sock); 247 if (err < 0) 248 return ERR_PTR(err); 249 250 udp_allow_gso(sock->sk); 251 return sock->sk; 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 sock *sk; 259 260 sk = bareudp_create_sock(bareudp->net, port); 261 if (IS_ERR(sk)) 262 return PTR_ERR(sk); 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(bareudp->net, sk, &tunnel_cfg); 272 273 rcu_assign_pointer(bareudp->sk, sk); 274 return 0; 275 } 276 277 static int bareudp_open(struct net_device *dev) 278 { 279 struct bareudp_dev *bareudp = netdev_priv(dev); 280 281 return bareudp_socket_create(bareudp, bareudp->port); 282 } 283 284 static void bareudp_sock_release(struct bareudp_dev *bareudp) 285 { 286 struct sock *sk; 287 288 sk = bareudp->sk; 289 rcu_assign_pointer(bareudp->sk, NULL); 290 synchronize_net(); 291 udp_tunnel_sock_release(sk); 292 } 293 294 static int bareudp_stop(struct net_device *dev) 295 { 296 struct bareudp_dev *bareudp = netdev_priv(dev); 297 298 bareudp_sock_release(bareudp); 299 return 0; 300 } 301 302 static int bareudp_xmit_skb(struct sk_buff *skb, struct net_device *dev, 303 struct bareudp_dev *bareudp, 304 const struct ip_tunnel_info *info) 305 { 306 bool udp_sum = test_bit(IP_TUNNEL_CSUM_BIT, info->key.tun_flags); 307 bool xnet = !net_eq(bareudp->net, dev_net(bareudp->dev)); 308 bool use_cache = ip_tunnel_dst_cache_usable(skb, info); 309 struct sock *sk = rcu_dereference(bareudp->sk); 310 const struct ip_tunnel_key *key = &info->key; 311 struct rtable *rt; 312 __be16 sport, df; 313 int min_headroom; 314 __u8 tos, ttl; 315 __be32 saddr; 316 int err; 317 318 if (skb_vlan_inet_prepare(skb, skb->protocol != htons(ETH_P_TEB))) 319 return -EINVAL; 320 321 if (!sk) 322 return -ESHUTDOWN; 323 324 sport = udp_flow_src_port(bareudp->net, skb, 325 bareudp->sport_min, USHRT_MAX, 326 true); 327 rt = udp_tunnel_dst_lookup(skb, dev, bareudp->net, 0, &saddr, &info->key, 328 sport, bareudp->port, key->tos, 329 use_cache ? 330 (struct dst_cache *)&info->dst_cache : NULL); 331 332 if (IS_ERR(rt)) 333 return PTR_ERR(rt); 334 335 skb_tunnel_check_pmtu(skb, &rt->dst, 336 BAREUDP_IPV4_HLEN + info->options_len, false); 337 338 tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb); 339 ttl = key->ttl; 340 df = test_bit(IP_TUNNEL_DONT_FRAGMENT_BIT, key->tun_flags) ? 341 htons(IP_DF) : 0; 342 skb_scrub_packet(skb, xnet); 343 344 err = -ENOSPC; 345 if (!skb_pull(skb, skb_network_offset(skb))) 346 goto free_dst; 347 348 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len + 349 BAREUDP_BASE_HLEN + info->options_len + sizeof(struct iphdr); 350 351 err = skb_cow_head(skb, min_headroom); 352 if (unlikely(err)) 353 goto free_dst; 354 355 err = udp_tunnel_handle_offloads(skb, udp_sum); 356 if (err) 357 goto free_dst; 358 359 skb_set_inner_protocol(skb, bareudp->ethertype); 360 udp_tunnel_xmit_skb(rt, sk, skb, saddr, info->key.u.ipv4.dst, 361 tos, ttl, df, sport, bareudp->port, 362 !net_eq(bareudp->net, dev_net(bareudp->dev)), 363 !test_bit(IP_TUNNEL_CSUM_BIT, info->key.tun_flags), 364 0); 365 return 0; 366 367 free_dst: 368 dst_release(&rt->dst); 369 return err; 370 } 371 372 static int bareudp6_xmit_skb(struct sk_buff *skb, struct net_device *dev, 373 struct bareudp_dev *bareudp, 374 const struct ip_tunnel_info *info) 375 { 376 bool udp_sum = test_bit(IP_TUNNEL_CSUM_BIT, info->key.tun_flags); 377 bool xnet = !net_eq(bareudp->net, dev_net(bareudp->dev)); 378 bool use_cache = ip_tunnel_dst_cache_usable(skb, info); 379 struct sock *sk = rcu_dereference(bareudp->sk); 380 const struct ip_tunnel_key *key = &info->key; 381 struct dst_entry *dst = NULL; 382 struct in6_addr saddr, daddr; 383 int min_headroom; 384 __u8 prio, ttl; 385 __be16 sport; 386 int err; 387 388 if (skb_vlan_inet_prepare(skb, skb->protocol != htons(ETH_P_TEB))) 389 return -EINVAL; 390 391 if (!sk) 392 return -ESHUTDOWN; 393 394 sport = udp_flow_src_port(bareudp->net, skb, 395 bareudp->sport_min, USHRT_MAX, 396 true); 397 dst = udp_tunnel6_dst_lookup(skb, dev, bareudp->net, sk, 0, &saddr, 398 key, sport, bareudp->port, key->tos, 399 use_cache ? 400 (struct dst_cache *) &info->dst_cache : NULL); 401 if (IS_ERR(dst)) 402 return PTR_ERR(dst); 403 404 skb_tunnel_check_pmtu(skb, dst, BAREUDP_IPV6_HLEN + info->options_len, 405 false); 406 407 prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb); 408 ttl = key->ttl; 409 410 skb_scrub_packet(skb, xnet); 411 412 err = -ENOSPC; 413 if (!skb_pull(skb, skb_network_offset(skb))) 414 goto free_dst; 415 416 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len + 417 BAREUDP_BASE_HLEN + info->options_len + sizeof(struct ipv6hdr); 418 419 err = skb_cow_head(skb, min_headroom); 420 if (unlikely(err)) 421 goto free_dst; 422 423 err = udp_tunnel_handle_offloads(skb, udp_sum); 424 if (err) 425 goto free_dst; 426 427 daddr = info->key.u.ipv6.dst; 428 udp_tunnel6_xmit_skb(dst, sk, skb, dev, 429 &saddr, &daddr, prio, ttl, 430 info->key.label, sport, bareudp->port, 431 !test_bit(IP_TUNNEL_CSUM_BIT, 432 info->key.tun_flags), 433 0); 434 return 0; 435 436 free_dst: 437 dst_release(dst); 438 return err; 439 } 440 441 static bool bareudp_proto_valid(struct bareudp_dev *bareudp, __be16 proto) 442 { 443 if (bareudp->ethertype == proto) 444 return true; 445 446 if (!bareudp->multi_proto_mode) 447 return false; 448 449 if (bareudp->ethertype == htons(ETH_P_MPLS_UC) && 450 proto == htons(ETH_P_MPLS_MC)) 451 return true; 452 453 if (bareudp->ethertype == htons(ETH_P_IP) && 454 proto == htons(ETH_P_IPV6)) 455 return true; 456 457 return false; 458 } 459 460 static netdev_tx_t bareudp_xmit(struct sk_buff *skb, struct net_device *dev) 461 { 462 struct bareudp_dev *bareudp = netdev_priv(dev); 463 struct ip_tunnel_info *info = NULL; 464 int err; 465 466 if (!bareudp_proto_valid(bareudp, skb->protocol)) { 467 err = -EINVAL; 468 goto tx_error; 469 } 470 471 info = skb_tunnel_info(skb); 472 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) { 473 err = -EINVAL; 474 goto tx_error; 475 } 476 477 rcu_read_lock(); 478 if (ipv6_mod_enabled() && info->mode & IP_TUNNEL_INFO_IPV6) 479 err = bareudp6_xmit_skb(skb, dev, bareudp, info); 480 else 481 err = bareudp_xmit_skb(skb, dev, bareudp, info); 482 483 rcu_read_unlock(); 484 485 if (likely(!err)) 486 return NETDEV_TX_OK; 487 tx_error: 488 dev_kfree_skb(skb); 489 490 if (err == -ELOOP) 491 DEV_STATS_INC(dev, collisions); 492 else if (err == -ENETUNREACH) 493 DEV_STATS_INC(dev, tx_carrier_errors); 494 495 DEV_STATS_INC(dev, tx_errors); 496 return NETDEV_TX_OK; 497 } 498 499 static int bareudp_fill_metadata_dst(struct net_device *dev, 500 struct sk_buff *skb) 501 { 502 struct ip_tunnel_info *info = skb_tunnel_info(skb); 503 struct bareudp_dev *bareudp = netdev_priv(dev); 504 bool use_cache; 505 __be16 sport; 506 507 use_cache = ip_tunnel_dst_cache_usable(skb, info); 508 sport = udp_flow_src_port(bareudp->net, skb, 509 bareudp->sport_min, USHRT_MAX, 510 true); 511 512 if (!ipv6_mod_enabled() || ip_tunnel_info_af(info) == AF_INET) { 513 struct rtable *rt; 514 __be32 saddr; 515 516 rt = udp_tunnel_dst_lookup(skb, dev, bareudp->net, 0, &saddr, 517 &info->key, sport, bareudp->port, 518 info->key.tos, 519 use_cache ? &info->dst_cache : NULL); 520 if (IS_ERR(rt)) 521 return PTR_ERR(rt); 522 523 ip_rt_put(rt); 524 info->key.u.ipv4.src = saddr; 525 } else if (ip_tunnel_info_af(info) == AF_INET6) { 526 struct dst_entry *dst; 527 struct in6_addr saddr; 528 struct sock *sk; 529 530 sk = rcu_dereference(bareudp->sk); 531 if (!sk) 532 return -ESHUTDOWN; 533 534 dst = udp_tunnel6_dst_lookup(skb, dev, bareudp->net, sk, 535 0, &saddr, &info->key, 536 sport, bareudp->port, info->key.tos, 537 use_cache ? &info->dst_cache : NULL); 538 if (IS_ERR(dst)) 539 return PTR_ERR(dst); 540 541 dst_release(dst); 542 info->key.u.ipv6.src = saddr; 543 } else { 544 return -EINVAL; 545 } 546 547 info->key.tp_src = sport; 548 info->key.tp_dst = bareudp->port; 549 return 0; 550 } 551 552 static const struct net_device_ops bareudp_netdev_ops = { 553 .ndo_init = bareudp_init, 554 .ndo_uninit = bareudp_uninit, 555 .ndo_open = bareudp_open, 556 .ndo_stop = bareudp_stop, 557 .ndo_start_xmit = bareudp_xmit, 558 .ndo_fill_metadata_dst = bareudp_fill_metadata_dst, 559 }; 560 561 static const struct nla_policy bareudp_policy[IFLA_BAREUDP_MAX + 1] = { 562 [IFLA_BAREUDP_PORT] = { .type = NLA_U16 }, 563 [IFLA_BAREUDP_ETHERTYPE] = { .type = NLA_U16 }, 564 [IFLA_BAREUDP_SRCPORT_MIN] = { .type = NLA_U16 }, 565 [IFLA_BAREUDP_MULTIPROTO_MODE] = { .type = NLA_FLAG }, 566 }; 567 568 /* Info for udev, that this is a virtual tunnel endpoint */ 569 static const struct device_type bareudp_type = { 570 .name = "bareudp", 571 }; 572 573 /* Initialize the device structure. */ 574 static void bareudp_setup(struct net_device *dev) 575 { 576 dev->netdev_ops = &bareudp_netdev_ops; 577 dev->needs_free_netdev = true; 578 SET_NETDEV_DEVTYPE(dev, &bareudp_type); 579 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST; 580 dev->features |= NETIF_F_RXCSUM; 581 dev->features |= NETIF_F_GSO_SOFTWARE; 582 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST; 583 dev->hw_features |= NETIF_F_RXCSUM; 584 dev->hw_features |= NETIF_F_GSO_SOFTWARE; 585 dev->hard_header_len = 0; 586 dev->addr_len = 0; 587 dev->mtu = ETH_DATA_LEN; 588 dev->min_mtu = IPV4_MIN_MTU; 589 dev->max_mtu = IP_MAX_MTU - BAREUDP_BASE_HLEN; 590 dev->type = ARPHRD_NONE; 591 netif_keep_dst(dev); 592 dev->priv_flags |= IFF_NO_QUEUE; 593 dev->lltx = true; 594 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; 595 dev->pcpu_stat_type = NETDEV_PCPU_STAT_DSTATS; 596 } 597 598 static int bareudp_validate(struct nlattr *tb[], struct nlattr *data[], 599 struct netlink_ext_ack *extack) 600 { 601 if (!data) { 602 NL_SET_ERR_MSG(extack, 603 "Not enough attributes provided to perform the operation"); 604 return -EINVAL; 605 } 606 return 0; 607 } 608 609 static int bareudp2info(struct nlattr *data[], struct bareudp_conf *conf, 610 struct netlink_ext_ack *extack) 611 { 612 memset(conf, 0, sizeof(*conf)); 613 614 if (!data[IFLA_BAREUDP_PORT]) { 615 NL_SET_ERR_MSG(extack, "port not specified"); 616 return -EINVAL; 617 } 618 if (!data[IFLA_BAREUDP_ETHERTYPE]) { 619 NL_SET_ERR_MSG(extack, "ethertype not specified"); 620 return -EINVAL; 621 } 622 623 conf->port = nla_get_u16(data[IFLA_BAREUDP_PORT]); 624 conf->ethertype = nla_get_u16(data[IFLA_BAREUDP_ETHERTYPE]); 625 626 if (data[IFLA_BAREUDP_SRCPORT_MIN]) 627 conf->sport_min = nla_get_u16(data[IFLA_BAREUDP_SRCPORT_MIN]); 628 629 if (data[IFLA_BAREUDP_MULTIPROTO_MODE]) 630 conf->multi_proto_mode = true; 631 632 return 0; 633 } 634 635 static struct bareudp_dev *bareudp_find_dev(struct bareudp_net *bn, 636 const struct bareudp_conf *conf) 637 { 638 struct bareudp_dev *bareudp, *t = NULL; 639 640 list_for_each_entry(bareudp, &bn->bareudp_list, next) { 641 if (conf->port == bareudp->port) 642 t = bareudp; 643 } 644 return t; 645 } 646 647 static int bareudp_configure(struct net *net, struct net_device *dev, 648 struct bareudp_conf *conf, 649 struct netlink_ext_ack *extack) 650 { 651 struct bareudp_net *bn = net_generic(net, bareudp_net_id); 652 struct bareudp_dev *t, *bareudp = netdev_priv(dev); 653 int err; 654 655 bareudp->net = net; 656 bareudp->dev = dev; 657 t = bareudp_find_dev(bn, conf); 658 if (t) { 659 NL_SET_ERR_MSG(extack, "Another bareudp device using the same port already exists"); 660 return -EBUSY; 661 } 662 663 if (conf->multi_proto_mode && 664 (conf->ethertype != htons(ETH_P_MPLS_UC) && 665 conf->ethertype != htons(ETH_P_IP))) { 666 NL_SET_ERR_MSG(extack, "Cannot set multiproto mode for this ethertype (only IPv4 and unicast MPLS are supported)"); 667 return -EINVAL; 668 } 669 670 bareudp->port = conf->port; 671 bareudp->ethertype = conf->ethertype; 672 bareudp->sport_min = conf->sport_min; 673 bareudp->multi_proto_mode = conf->multi_proto_mode; 674 675 err = register_netdevice(dev); 676 if (err) 677 return err; 678 679 list_add(&bareudp->next, &bn->bareudp_list); 680 return 0; 681 } 682 683 static int bareudp_link_config(struct net_device *dev, 684 struct nlattr *tb[]) 685 { 686 int err; 687 688 if (tb[IFLA_MTU]) { 689 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); 690 if (err) 691 return err; 692 } 693 return 0; 694 } 695 696 static void bareudp_dellink(struct net_device *dev, struct list_head *head) 697 { 698 struct bareudp_dev *bareudp = netdev_priv(dev); 699 700 list_del(&bareudp->next); 701 unregister_netdevice_queue(dev, head); 702 } 703 704 static int bareudp_newlink(struct net_device *dev, 705 struct rtnl_newlink_params *params, 706 struct netlink_ext_ack *extack) 707 { 708 struct net *link_net = rtnl_newlink_link_net(params); 709 struct nlattr **data = params->data; 710 struct nlattr **tb = params->tb; 711 struct bareudp_conf conf; 712 int err; 713 714 err = bareudp2info(data, &conf, extack); 715 if (err) 716 return err; 717 718 err = bareudp_configure(link_net, dev, &conf, extack); 719 if (err) 720 return err; 721 722 err = bareudp_link_config(dev, tb); 723 if (err) 724 goto err_unconfig; 725 726 return 0; 727 728 err_unconfig: 729 bareudp_dellink(dev, NULL); 730 return err; 731 } 732 733 static size_t bareudp_get_size(const struct net_device *dev) 734 { 735 return nla_total_size(sizeof(__be16)) + /* IFLA_BAREUDP_PORT */ 736 nla_total_size(sizeof(__be16)) + /* IFLA_BAREUDP_ETHERTYPE */ 737 nla_total_size(sizeof(__u16)) + /* IFLA_BAREUDP_SRCPORT_MIN */ 738 nla_total_size(0) + /* IFLA_BAREUDP_MULTIPROTO_MODE */ 739 0; 740 } 741 742 static int bareudp_fill_info(struct sk_buff *skb, const struct net_device *dev) 743 { 744 struct bareudp_dev *bareudp = netdev_priv(dev); 745 746 if (nla_put_be16(skb, IFLA_BAREUDP_PORT, bareudp->port)) 747 goto nla_put_failure; 748 if (nla_put_be16(skb, IFLA_BAREUDP_ETHERTYPE, bareudp->ethertype)) 749 goto nla_put_failure; 750 if (nla_put_u16(skb, IFLA_BAREUDP_SRCPORT_MIN, bareudp->sport_min)) 751 goto nla_put_failure; 752 if (bareudp->multi_proto_mode && 753 nla_put_flag(skb, IFLA_BAREUDP_MULTIPROTO_MODE)) 754 goto nla_put_failure; 755 756 return 0; 757 758 nla_put_failure: 759 return -EMSGSIZE; 760 } 761 762 static struct rtnl_link_ops bareudp_link_ops __read_mostly = { 763 .kind = "bareudp", 764 .maxtype = IFLA_BAREUDP_MAX, 765 .policy = bareudp_policy, 766 .priv_size = sizeof(struct bareudp_dev), 767 .setup = bareudp_setup, 768 .validate = bareudp_validate, 769 .newlink = bareudp_newlink, 770 .dellink = bareudp_dellink, 771 .get_size = bareudp_get_size, 772 .fill_info = bareudp_fill_info, 773 }; 774 775 static __net_init int bareudp_init_net(struct net *net) 776 { 777 struct bareudp_net *bn = net_generic(net, bareudp_net_id); 778 779 INIT_LIST_HEAD(&bn->bareudp_list); 780 return 0; 781 } 782 783 static void __net_exit bareudp_exit_rtnl_net(struct net *net, 784 struct list_head *dev_kill_list) 785 { 786 struct bareudp_net *bn = net_generic(net, bareudp_net_id); 787 struct bareudp_dev *bareudp, *next; 788 789 list_for_each_entry_safe(bareudp, next, &bn->bareudp_list, next) 790 bareudp_dellink(bareudp->dev, dev_kill_list); 791 } 792 793 static struct pernet_operations bareudp_net_ops = { 794 .init = bareudp_init_net, 795 .exit_rtnl = bareudp_exit_rtnl_net, 796 .id = &bareudp_net_id, 797 .size = sizeof(struct bareudp_net), 798 }; 799 800 static int __init bareudp_init_module(void) 801 { 802 int rc; 803 804 rc = register_pernet_subsys(&bareudp_net_ops); 805 if (rc) 806 goto out1; 807 808 rc = rtnl_link_register(&bareudp_link_ops); 809 if (rc) 810 goto out2; 811 812 return 0; 813 out2: 814 unregister_pernet_subsys(&bareudp_net_ops); 815 out1: 816 return rc; 817 } 818 late_initcall(bareudp_init_module); 819 820 static void __exit bareudp_cleanup_module(void) 821 { 822 rtnl_link_unregister(&bareudp_link_ops); 823 unregister_pernet_subsys(&bareudp_net_ops); 824 } 825 module_exit(bareudp_cleanup_module); 826 827 MODULE_ALIAS_RTNL_LINK("bareudp"); 828 MODULE_LICENSE("GPL"); 829 MODULE_AUTHOR("Martin Varghese <martin.varghese@nokia.com>"); 830 MODULE_DESCRIPTION("Interface driver for UDP encapsulated traffic"); 831