1 /* 2 * GENEVE: Generic Network Virtualization Encapsulation 3 * 4 * Copyright (c) 2015 Red Hat, Inc. 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 as 8 * published by the Free Software Foundation. 9 */ 10 11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 12 13 #include <linux/kernel.h> 14 #include <linux/module.h> 15 #include <linux/netdevice.h> 16 #include <linux/etherdevice.h> 17 #include <linux/hash.h> 18 #include <net/dst_metadata.h> 19 #include <net/gro_cells.h> 20 #include <net/rtnetlink.h> 21 #include <net/geneve.h> 22 #include <net/protocol.h> 23 24 #define GENEVE_NETDEV_VER "0.6" 25 26 #define GENEVE_UDP_PORT 6081 27 28 #define GENEVE_N_VID (1u << 24) 29 #define GENEVE_VID_MASK (GENEVE_N_VID - 1) 30 31 #define VNI_HASH_BITS 10 32 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS) 33 34 static bool log_ecn_error = true; 35 module_param(log_ecn_error, bool, 0644); 36 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); 37 38 #define GENEVE_VER 0 39 #define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr)) 40 41 /* per-network namespace private data for this module */ 42 struct geneve_net { 43 struct list_head geneve_list; 44 struct list_head sock_list; 45 }; 46 47 static int geneve_net_id; 48 49 union geneve_addr { 50 struct sockaddr_in sin; 51 struct sockaddr_in6 sin6; 52 struct sockaddr sa; 53 }; 54 55 static union geneve_addr geneve_remote_unspec = { .sa.sa_family = AF_UNSPEC, }; 56 57 /* Pseudo network device */ 58 struct geneve_dev { 59 struct hlist_node hlist; /* vni hash table */ 60 struct net *net; /* netns for packet i/o */ 61 struct net_device *dev; /* netdev for geneve tunnel */ 62 struct geneve_sock *sock4; /* IPv4 socket used for geneve tunnel */ 63 #if IS_ENABLED(CONFIG_IPV6) 64 struct geneve_sock *sock6; /* IPv6 socket used for geneve tunnel */ 65 #endif 66 u8 vni[3]; /* virtual network ID for tunnel */ 67 u8 ttl; /* TTL override */ 68 u8 tos; /* TOS override */ 69 union geneve_addr remote; /* IP address for link partner */ 70 struct list_head next; /* geneve's per namespace list */ 71 __be16 dst_port; 72 bool collect_md; 73 struct gro_cells gro_cells; 74 }; 75 76 struct geneve_sock { 77 bool collect_md; 78 struct list_head list; 79 struct socket *sock; 80 struct rcu_head rcu; 81 int refcnt; 82 struct udp_offload udp_offloads; 83 struct hlist_head vni_list[VNI_HASH_SIZE]; 84 }; 85 86 static inline __u32 geneve_net_vni_hash(u8 vni[3]) 87 { 88 __u32 vnid; 89 90 vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2]; 91 return hash_32(vnid, VNI_HASH_BITS); 92 } 93 94 static __be64 vni_to_tunnel_id(const __u8 *vni) 95 { 96 #ifdef __BIG_ENDIAN 97 return (vni[0] << 16) | (vni[1] << 8) | vni[2]; 98 #else 99 return (__force __be64)(((__force u64)vni[0] << 40) | 100 ((__force u64)vni[1] << 48) | 101 ((__force u64)vni[2] << 56)); 102 #endif 103 } 104 105 static struct geneve_dev *geneve_lookup(struct geneve_sock *gs, 106 __be32 addr, u8 vni[]) 107 { 108 struct hlist_head *vni_list_head; 109 struct geneve_dev *geneve; 110 __u32 hash; 111 112 /* Find the device for this VNI */ 113 hash = geneve_net_vni_hash(vni); 114 vni_list_head = &gs->vni_list[hash]; 115 hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) { 116 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) && 117 addr == geneve->remote.sin.sin_addr.s_addr) 118 return geneve; 119 } 120 return NULL; 121 } 122 123 #if IS_ENABLED(CONFIG_IPV6) 124 static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs, 125 struct in6_addr addr6, u8 vni[]) 126 { 127 struct hlist_head *vni_list_head; 128 struct geneve_dev *geneve; 129 __u32 hash; 130 131 /* Find the device for this VNI */ 132 hash = geneve_net_vni_hash(vni); 133 vni_list_head = &gs->vni_list[hash]; 134 hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) { 135 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) && 136 ipv6_addr_equal(&addr6, &geneve->remote.sin6.sin6_addr)) 137 return geneve; 138 } 139 return NULL; 140 } 141 #endif 142 143 static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb) 144 { 145 return (struct genevehdr *)(udp_hdr(skb) + 1); 146 } 147 148 /* geneve receive/decap routine */ 149 static void geneve_rx(struct geneve_sock *gs, struct sk_buff *skb) 150 { 151 struct genevehdr *gnvh = geneve_hdr(skb); 152 struct metadata_dst *tun_dst = NULL; 153 struct geneve_dev *geneve = NULL; 154 struct pcpu_sw_netstats *stats; 155 struct iphdr *iph = NULL; 156 __be32 addr; 157 static u8 zero_vni[3]; 158 u8 *vni; 159 int err = 0; 160 sa_family_t sa_family; 161 #if IS_ENABLED(CONFIG_IPV6) 162 struct ipv6hdr *ip6h = NULL; 163 struct in6_addr addr6; 164 static struct in6_addr zero_addr6; 165 #endif 166 167 sa_family = gs->sock->sk->sk_family; 168 169 if (sa_family == AF_INET) { 170 iph = ip_hdr(skb); /* outer IP header... */ 171 172 if (gs->collect_md) { 173 vni = zero_vni; 174 addr = 0; 175 } else { 176 vni = gnvh->vni; 177 178 addr = iph->saddr; 179 } 180 181 geneve = geneve_lookup(gs, addr, vni); 182 #if IS_ENABLED(CONFIG_IPV6) 183 } else if (sa_family == AF_INET6) { 184 ip6h = ipv6_hdr(skb); /* outer IPv6 header... */ 185 186 if (gs->collect_md) { 187 vni = zero_vni; 188 addr6 = zero_addr6; 189 } else { 190 vni = gnvh->vni; 191 192 addr6 = ip6h->saddr; 193 } 194 195 geneve = geneve6_lookup(gs, addr6, vni); 196 #endif 197 } 198 if (!geneve) 199 goto drop; 200 201 if (ip_tunnel_collect_metadata() || gs->collect_md) { 202 __be16 flags; 203 204 flags = TUNNEL_KEY | TUNNEL_GENEVE_OPT | 205 (gnvh->oam ? TUNNEL_OAM : 0) | 206 (gnvh->critical ? TUNNEL_CRIT_OPT : 0); 207 208 tun_dst = udp_tun_rx_dst(skb, sa_family, flags, 209 vni_to_tunnel_id(gnvh->vni), 210 gnvh->opt_len * 4); 211 if (!tun_dst) 212 goto drop; 213 /* Update tunnel dst according to Geneve options. */ 214 ip_tunnel_info_opts_set(&tun_dst->u.tun_info, 215 gnvh->options, gnvh->opt_len * 4); 216 } else { 217 /* Drop packets w/ critical options, 218 * since we don't support any... 219 */ 220 if (gnvh->critical) 221 goto drop; 222 } 223 224 skb_reset_mac_header(skb); 225 skb_scrub_packet(skb, !net_eq(geneve->net, dev_net(geneve->dev))); 226 skb->protocol = eth_type_trans(skb, geneve->dev); 227 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); 228 229 if (tun_dst) 230 skb_dst_set(skb, &tun_dst->dst); 231 232 /* Ignore packet loops (and multicast echo) */ 233 if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr)) 234 goto drop; 235 236 skb_reset_network_header(skb); 237 238 if (iph) 239 err = IP_ECN_decapsulate(iph, skb); 240 #if IS_ENABLED(CONFIG_IPV6) 241 if (ip6h) 242 err = IP6_ECN_decapsulate(ip6h, skb); 243 #endif 244 245 if (unlikely(err)) { 246 if (log_ecn_error) { 247 if (iph) 248 net_info_ratelimited("non-ECT from %pI4 " 249 "with TOS=%#x\n", 250 &iph->saddr, iph->tos); 251 #if IS_ENABLED(CONFIG_IPV6) 252 if (ip6h) 253 net_info_ratelimited("non-ECT from %pI6\n", 254 &ip6h->saddr); 255 #endif 256 } 257 if (err > 1) { 258 ++geneve->dev->stats.rx_frame_errors; 259 ++geneve->dev->stats.rx_errors; 260 goto drop; 261 } 262 } 263 264 stats = this_cpu_ptr(geneve->dev->tstats); 265 u64_stats_update_begin(&stats->syncp); 266 stats->rx_packets++; 267 stats->rx_bytes += skb->len; 268 u64_stats_update_end(&stats->syncp); 269 270 gro_cells_receive(&geneve->gro_cells, skb); 271 return; 272 drop: 273 /* Consume bad packet */ 274 kfree_skb(skb); 275 } 276 277 /* Setup stats when device is created */ 278 static int geneve_init(struct net_device *dev) 279 { 280 struct geneve_dev *geneve = netdev_priv(dev); 281 int err; 282 283 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); 284 if (!dev->tstats) 285 return -ENOMEM; 286 287 err = gro_cells_init(&geneve->gro_cells, dev); 288 if (err) { 289 free_percpu(dev->tstats); 290 return err; 291 } 292 293 return 0; 294 } 295 296 static void geneve_uninit(struct net_device *dev) 297 { 298 struct geneve_dev *geneve = netdev_priv(dev); 299 300 gro_cells_destroy(&geneve->gro_cells); 301 free_percpu(dev->tstats); 302 } 303 304 /* Callback from net/ipv4/udp.c to receive packets */ 305 static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb) 306 { 307 struct genevehdr *geneveh; 308 struct geneve_sock *gs; 309 int opts_len; 310 311 /* Need Geneve and inner Ethernet header to be present */ 312 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN))) 313 goto error; 314 315 /* Return packets with reserved bits set */ 316 geneveh = geneve_hdr(skb); 317 if (unlikely(geneveh->ver != GENEVE_VER)) 318 goto error; 319 320 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB))) 321 goto error; 322 323 opts_len = geneveh->opt_len * 4; 324 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len, 325 htons(ETH_P_TEB))) 326 goto drop; 327 328 gs = rcu_dereference_sk_user_data(sk); 329 if (!gs) 330 goto drop; 331 332 geneve_rx(gs, skb); 333 return 0; 334 335 drop: 336 /* Consume bad packet */ 337 kfree_skb(skb); 338 return 0; 339 340 error: 341 /* Let the UDP layer deal with the skb */ 342 return 1; 343 } 344 345 static struct socket *geneve_create_sock(struct net *net, bool ipv6, 346 __be16 port) 347 { 348 struct socket *sock; 349 struct udp_port_cfg udp_conf; 350 int err; 351 352 memset(&udp_conf, 0, sizeof(udp_conf)); 353 354 if (ipv6) { 355 udp_conf.family = AF_INET6; 356 udp_conf.ipv6_v6only = 1; 357 } else { 358 udp_conf.family = AF_INET; 359 udp_conf.local_ip.s_addr = htonl(INADDR_ANY); 360 } 361 362 udp_conf.local_udp_port = port; 363 364 /* Open UDP socket */ 365 err = udp_sock_create(net, &udp_conf, &sock); 366 if (err < 0) 367 return ERR_PTR(err); 368 369 return sock; 370 } 371 372 static void geneve_notify_add_rx_port(struct geneve_sock *gs) 373 { 374 struct sock *sk = gs->sock->sk; 375 sa_family_t sa_family = sk->sk_family; 376 int err; 377 378 if (sa_family == AF_INET) { 379 err = udp_add_offload(&gs->udp_offloads); 380 if (err) 381 pr_warn("geneve: udp_add_offload failed with status %d\n", 382 err); 383 } 384 } 385 386 static int geneve_hlen(struct genevehdr *gh) 387 { 388 return sizeof(*gh) + gh->opt_len * 4; 389 } 390 391 static struct sk_buff **geneve_gro_receive(struct sk_buff **head, 392 struct sk_buff *skb, 393 struct udp_offload *uoff) 394 { 395 struct sk_buff *p, **pp = NULL; 396 struct genevehdr *gh, *gh2; 397 unsigned int hlen, gh_len, off_gnv; 398 const struct packet_offload *ptype; 399 __be16 type; 400 int flush = 1; 401 402 off_gnv = skb_gro_offset(skb); 403 hlen = off_gnv + sizeof(*gh); 404 gh = skb_gro_header_fast(skb, off_gnv); 405 if (skb_gro_header_hard(skb, hlen)) { 406 gh = skb_gro_header_slow(skb, hlen, off_gnv); 407 if (unlikely(!gh)) 408 goto out; 409 } 410 411 if (gh->ver != GENEVE_VER || gh->oam) 412 goto out; 413 gh_len = geneve_hlen(gh); 414 415 hlen = off_gnv + gh_len; 416 if (skb_gro_header_hard(skb, hlen)) { 417 gh = skb_gro_header_slow(skb, hlen, off_gnv); 418 if (unlikely(!gh)) 419 goto out; 420 } 421 422 flush = 0; 423 424 for (p = *head; p; p = p->next) { 425 if (!NAPI_GRO_CB(p)->same_flow) 426 continue; 427 428 gh2 = (struct genevehdr *)(p->data + off_gnv); 429 if (gh->opt_len != gh2->opt_len || 430 memcmp(gh, gh2, gh_len)) { 431 NAPI_GRO_CB(p)->same_flow = 0; 432 continue; 433 } 434 } 435 436 type = gh->proto_type; 437 438 rcu_read_lock(); 439 ptype = gro_find_receive_by_type(type); 440 if (!ptype) { 441 flush = 1; 442 goto out_unlock; 443 } 444 445 skb_gro_pull(skb, gh_len); 446 skb_gro_postpull_rcsum(skb, gh, gh_len); 447 pp = ptype->callbacks.gro_receive(head, skb); 448 449 out_unlock: 450 rcu_read_unlock(); 451 out: 452 NAPI_GRO_CB(skb)->flush |= flush; 453 454 return pp; 455 } 456 457 static int geneve_gro_complete(struct sk_buff *skb, int nhoff, 458 struct udp_offload *uoff) 459 { 460 struct genevehdr *gh; 461 struct packet_offload *ptype; 462 __be16 type; 463 int gh_len; 464 int err = -ENOSYS; 465 466 udp_tunnel_gro_complete(skb, nhoff); 467 468 gh = (struct genevehdr *)(skb->data + nhoff); 469 gh_len = geneve_hlen(gh); 470 type = gh->proto_type; 471 472 rcu_read_lock(); 473 ptype = gro_find_complete_by_type(type); 474 if (ptype) 475 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len); 476 477 rcu_read_unlock(); 478 return err; 479 } 480 481 /* Create new listen socket if needed */ 482 static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port, 483 bool ipv6) 484 { 485 struct geneve_net *gn = net_generic(net, geneve_net_id); 486 struct geneve_sock *gs; 487 struct socket *sock; 488 struct udp_tunnel_sock_cfg tunnel_cfg; 489 int h; 490 491 gs = kzalloc(sizeof(*gs), GFP_KERNEL); 492 if (!gs) 493 return ERR_PTR(-ENOMEM); 494 495 sock = geneve_create_sock(net, ipv6, port); 496 if (IS_ERR(sock)) { 497 kfree(gs); 498 return ERR_CAST(sock); 499 } 500 501 gs->sock = sock; 502 gs->refcnt = 1; 503 for (h = 0; h < VNI_HASH_SIZE; ++h) 504 INIT_HLIST_HEAD(&gs->vni_list[h]); 505 506 /* Initialize the geneve udp offloads structure */ 507 gs->udp_offloads.port = port; 508 gs->udp_offloads.callbacks.gro_receive = geneve_gro_receive; 509 gs->udp_offloads.callbacks.gro_complete = geneve_gro_complete; 510 geneve_notify_add_rx_port(gs); 511 512 /* Mark socket as an encapsulation socket */ 513 tunnel_cfg.sk_user_data = gs; 514 tunnel_cfg.encap_type = 1; 515 tunnel_cfg.encap_rcv = geneve_udp_encap_recv; 516 tunnel_cfg.encap_destroy = NULL; 517 setup_udp_tunnel_sock(net, sock, &tunnel_cfg); 518 list_add(&gs->list, &gn->sock_list); 519 return gs; 520 } 521 522 static void geneve_notify_del_rx_port(struct geneve_sock *gs) 523 { 524 struct sock *sk = gs->sock->sk; 525 sa_family_t sa_family = sk->sk_family; 526 527 if (sa_family == AF_INET) 528 udp_del_offload(&gs->udp_offloads); 529 } 530 531 static void __geneve_sock_release(struct geneve_sock *gs) 532 { 533 if (!gs || --gs->refcnt) 534 return; 535 536 list_del(&gs->list); 537 geneve_notify_del_rx_port(gs); 538 udp_tunnel_sock_release(gs->sock); 539 kfree_rcu(gs, rcu); 540 } 541 542 static void geneve_sock_release(struct geneve_dev *geneve) 543 { 544 __geneve_sock_release(geneve->sock4); 545 #if IS_ENABLED(CONFIG_IPV6) 546 __geneve_sock_release(geneve->sock6); 547 #endif 548 } 549 550 static struct geneve_sock *geneve_find_sock(struct geneve_net *gn, 551 sa_family_t family, 552 __be16 dst_port) 553 { 554 struct geneve_sock *gs; 555 556 list_for_each_entry(gs, &gn->sock_list, list) { 557 if (inet_sk(gs->sock->sk)->inet_sport == dst_port && 558 inet_sk(gs->sock->sk)->sk.sk_family == family) { 559 return gs; 560 } 561 } 562 return NULL; 563 } 564 565 static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6) 566 { 567 struct net *net = geneve->net; 568 struct geneve_net *gn = net_generic(net, geneve_net_id); 569 struct geneve_sock *gs; 570 __u32 hash; 571 572 gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->dst_port); 573 if (gs) { 574 gs->refcnt++; 575 goto out; 576 } 577 578 gs = geneve_socket_create(net, geneve->dst_port, ipv6); 579 if (IS_ERR(gs)) 580 return PTR_ERR(gs); 581 582 out: 583 gs->collect_md = geneve->collect_md; 584 #if IS_ENABLED(CONFIG_IPV6) 585 if (ipv6) 586 geneve->sock6 = gs; 587 else 588 #endif 589 geneve->sock4 = gs; 590 591 hash = geneve_net_vni_hash(geneve->vni); 592 hlist_add_head_rcu(&geneve->hlist, &gs->vni_list[hash]); 593 return 0; 594 } 595 596 static int geneve_open(struct net_device *dev) 597 { 598 struct geneve_dev *geneve = netdev_priv(dev); 599 bool ipv6 = geneve->remote.sa.sa_family == AF_INET6; 600 bool metadata = geneve->collect_md; 601 int ret = 0; 602 603 geneve->sock4 = NULL; 604 #if IS_ENABLED(CONFIG_IPV6) 605 geneve->sock6 = NULL; 606 if (ipv6 || metadata) 607 ret = geneve_sock_add(geneve, true); 608 #endif 609 if (!ret && (!ipv6 || metadata)) 610 ret = geneve_sock_add(geneve, false); 611 if (ret < 0) 612 geneve_sock_release(geneve); 613 614 return ret; 615 } 616 617 static int geneve_stop(struct net_device *dev) 618 { 619 struct geneve_dev *geneve = netdev_priv(dev); 620 621 if (!hlist_unhashed(&geneve->hlist)) 622 hlist_del_rcu(&geneve->hlist); 623 geneve_sock_release(geneve); 624 return 0; 625 } 626 627 static void geneve_build_header(struct genevehdr *geneveh, 628 __be16 tun_flags, u8 vni[3], 629 u8 options_len, u8 *options) 630 { 631 geneveh->ver = GENEVE_VER; 632 geneveh->opt_len = options_len / 4; 633 geneveh->oam = !!(tun_flags & TUNNEL_OAM); 634 geneveh->critical = !!(tun_flags & TUNNEL_CRIT_OPT); 635 geneveh->rsvd1 = 0; 636 memcpy(geneveh->vni, vni, 3); 637 geneveh->proto_type = htons(ETH_P_TEB); 638 geneveh->rsvd2 = 0; 639 640 memcpy(geneveh->options, options, options_len); 641 } 642 643 static int geneve_build_skb(struct rtable *rt, struct sk_buff *skb, 644 __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt, 645 bool csum, bool xnet) 646 { 647 struct genevehdr *gnvh; 648 int min_headroom; 649 int err; 650 651 skb_scrub_packet(skb, xnet); 652 653 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len 654 + GENEVE_BASE_HLEN + opt_len + sizeof(struct iphdr); 655 err = skb_cow_head(skb, min_headroom); 656 if (unlikely(err)) { 657 kfree_skb(skb); 658 goto free_rt; 659 } 660 661 skb = udp_tunnel_handle_offloads(skb, csum); 662 if (IS_ERR(skb)) { 663 err = PTR_ERR(skb); 664 goto free_rt; 665 } 666 667 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len); 668 geneve_build_header(gnvh, tun_flags, vni, opt_len, opt); 669 670 skb_set_inner_protocol(skb, htons(ETH_P_TEB)); 671 return 0; 672 673 free_rt: 674 ip_rt_put(rt); 675 return err; 676 } 677 678 #if IS_ENABLED(CONFIG_IPV6) 679 static int geneve6_build_skb(struct dst_entry *dst, struct sk_buff *skb, 680 __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt, 681 bool csum, bool xnet) 682 { 683 struct genevehdr *gnvh; 684 int min_headroom; 685 int err; 686 687 skb_scrub_packet(skb, xnet); 688 689 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len 690 + GENEVE_BASE_HLEN + opt_len + sizeof(struct ipv6hdr); 691 err = skb_cow_head(skb, min_headroom); 692 if (unlikely(err)) { 693 kfree_skb(skb); 694 goto free_dst; 695 } 696 697 skb = udp_tunnel_handle_offloads(skb, csum); 698 if (IS_ERR(skb)) { 699 err = PTR_ERR(skb); 700 goto free_dst; 701 } 702 703 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len); 704 geneve_build_header(gnvh, tun_flags, vni, opt_len, opt); 705 706 skb_set_inner_protocol(skb, htons(ETH_P_TEB)); 707 return 0; 708 709 free_dst: 710 dst_release(dst); 711 return err; 712 } 713 #endif 714 715 static struct rtable *geneve_get_v4_rt(struct sk_buff *skb, 716 struct net_device *dev, 717 struct flowi4 *fl4, 718 struct ip_tunnel_info *info) 719 { 720 struct geneve_dev *geneve = netdev_priv(dev); 721 struct rtable *rt = NULL; 722 __u8 tos; 723 724 memset(fl4, 0, sizeof(*fl4)); 725 fl4->flowi4_mark = skb->mark; 726 fl4->flowi4_proto = IPPROTO_UDP; 727 728 if (info) { 729 fl4->daddr = info->key.u.ipv4.dst; 730 fl4->saddr = info->key.u.ipv4.src; 731 fl4->flowi4_tos = RT_TOS(info->key.tos); 732 } else { 733 tos = geneve->tos; 734 if (tos == 1) { 735 const struct iphdr *iip = ip_hdr(skb); 736 737 tos = ip_tunnel_get_dsfield(iip, skb); 738 } 739 740 fl4->flowi4_tos = RT_TOS(tos); 741 fl4->daddr = geneve->remote.sin.sin_addr.s_addr; 742 } 743 744 rt = ip_route_output_key(geneve->net, fl4); 745 if (IS_ERR(rt)) { 746 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr); 747 return ERR_PTR(-ENETUNREACH); 748 } 749 if (rt->dst.dev == dev) { /* is this necessary? */ 750 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr); 751 ip_rt_put(rt); 752 return ERR_PTR(-ELOOP); 753 } 754 return rt; 755 } 756 757 #if IS_ENABLED(CONFIG_IPV6) 758 static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb, 759 struct net_device *dev, 760 struct flowi6 *fl6, 761 struct ip_tunnel_info *info) 762 { 763 struct geneve_dev *geneve = netdev_priv(dev); 764 struct geneve_sock *gs6 = geneve->sock6; 765 struct dst_entry *dst = NULL; 766 __u8 prio; 767 768 memset(fl6, 0, sizeof(*fl6)); 769 fl6->flowi6_mark = skb->mark; 770 fl6->flowi6_proto = IPPROTO_UDP; 771 772 if (info) { 773 fl6->daddr = info->key.u.ipv6.dst; 774 fl6->saddr = info->key.u.ipv6.src; 775 fl6->flowi6_tos = RT_TOS(info->key.tos); 776 } else { 777 prio = geneve->tos; 778 if (prio == 1) { 779 const struct iphdr *iip = ip_hdr(skb); 780 781 prio = ip_tunnel_get_dsfield(iip, skb); 782 } 783 784 fl6->flowi6_tos = RT_TOS(prio); 785 fl6->daddr = geneve->remote.sin6.sin6_addr; 786 } 787 788 if (ipv6_stub->ipv6_dst_lookup(geneve->net, gs6->sock->sk, &dst, fl6)) { 789 netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr); 790 return ERR_PTR(-ENETUNREACH); 791 } 792 if (dst->dev == dev) { /* is this necessary? */ 793 netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr); 794 dst_release(dst); 795 return ERR_PTR(-ELOOP); 796 } 797 798 return dst; 799 } 800 #endif 801 802 /* Convert 64 bit tunnel ID to 24 bit VNI. */ 803 static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni) 804 { 805 #ifdef __BIG_ENDIAN 806 vni[0] = (__force __u8)(tun_id >> 16); 807 vni[1] = (__force __u8)(tun_id >> 8); 808 vni[2] = (__force __u8)tun_id; 809 #else 810 vni[0] = (__force __u8)((__force u64)tun_id >> 40); 811 vni[1] = (__force __u8)((__force u64)tun_id >> 48); 812 vni[2] = (__force __u8)((__force u64)tun_id >> 56); 813 #endif 814 } 815 816 static netdev_tx_t geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev, 817 struct ip_tunnel_info *info) 818 { 819 struct geneve_dev *geneve = netdev_priv(dev); 820 struct geneve_sock *gs4 = geneve->sock4; 821 struct rtable *rt = NULL; 822 const struct iphdr *iip; /* interior IP header */ 823 int err = -EINVAL; 824 struct flowi4 fl4; 825 __u8 tos, ttl; 826 __be16 sport; 827 bool udp_csum; 828 __be16 df; 829 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev)); 830 831 if (geneve->collect_md) { 832 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) { 833 netdev_dbg(dev, "no tunnel metadata\n"); 834 goto tx_error; 835 } 836 if (info && ip_tunnel_info_af(info) != AF_INET) 837 goto tx_error; 838 } 839 840 rt = geneve_get_v4_rt(skb, dev, &fl4, info); 841 if (IS_ERR(rt)) { 842 err = PTR_ERR(rt); 843 goto tx_error; 844 } 845 846 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true); 847 skb_reset_mac_header(skb); 848 849 iip = ip_hdr(skb); 850 851 if (info) { 852 const struct ip_tunnel_key *key = &info->key; 853 u8 *opts = NULL; 854 u8 vni[3]; 855 856 tunnel_id_to_vni(key->tun_id, vni); 857 if (key->tun_flags & TUNNEL_GENEVE_OPT) 858 opts = ip_tunnel_info_opts(info); 859 860 udp_csum = !!(key->tun_flags & TUNNEL_CSUM); 861 err = geneve_build_skb(rt, skb, key->tun_flags, vni, 862 info->options_len, opts, udp_csum, xnet); 863 if (unlikely(err)) 864 goto err; 865 866 tos = ip_tunnel_ecn_encap(key->tos, iip, skb); 867 ttl = key->ttl; 868 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0; 869 } else { 870 udp_csum = false; 871 err = geneve_build_skb(rt, skb, 0, geneve->vni, 872 0, NULL, udp_csum, xnet); 873 if (unlikely(err)) 874 goto err; 875 876 tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, iip, skb); 877 ttl = geneve->ttl; 878 if (!ttl && IN_MULTICAST(ntohl(fl4.daddr))) 879 ttl = 1; 880 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst); 881 df = 0; 882 } 883 err = udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr, 884 tos, ttl, df, sport, geneve->dst_port, 885 !net_eq(geneve->net, dev_net(geneve->dev)), 886 !udp_csum); 887 888 iptunnel_xmit_stats(err, &dev->stats, dev->tstats); 889 return NETDEV_TX_OK; 890 891 tx_error: 892 dev_kfree_skb(skb); 893 err: 894 if (err == -ELOOP) 895 dev->stats.collisions++; 896 else if (err == -ENETUNREACH) 897 dev->stats.tx_carrier_errors++; 898 else 899 dev->stats.tx_errors++; 900 return NETDEV_TX_OK; 901 } 902 903 #if IS_ENABLED(CONFIG_IPV6) 904 static netdev_tx_t geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev, 905 struct ip_tunnel_info *info) 906 { 907 struct geneve_dev *geneve = netdev_priv(dev); 908 struct geneve_sock *gs6 = geneve->sock6; 909 struct dst_entry *dst = NULL; 910 const struct iphdr *iip; /* interior IP header */ 911 int err = -EINVAL; 912 struct flowi6 fl6; 913 __u8 prio, ttl; 914 __be16 sport; 915 bool udp_csum; 916 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev)); 917 918 if (geneve->collect_md) { 919 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) { 920 netdev_dbg(dev, "no tunnel metadata\n"); 921 goto tx_error; 922 } 923 } 924 925 dst = geneve_get_v6_dst(skb, dev, &fl6, info); 926 if (IS_ERR(dst)) { 927 err = PTR_ERR(dst); 928 goto tx_error; 929 } 930 931 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true); 932 skb_reset_mac_header(skb); 933 934 iip = ip_hdr(skb); 935 936 if (info) { 937 const struct ip_tunnel_key *key = &info->key; 938 u8 *opts = NULL; 939 u8 vni[3]; 940 941 tunnel_id_to_vni(key->tun_id, vni); 942 if (key->tun_flags & TUNNEL_GENEVE_OPT) 943 opts = ip_tunnel_info_opts(info); 944 945 udp_csum = !!(key->tun_flags & TUNNEL_CSUM); 946 err = geneve6_build_skb(dst, skb, key->tun_flags, vni, 947 info->options_len, opts, 948 udp_csum, xnet); 949 if (unlikely(err)) 950 goto err; 951 952 prio = ip_tunnel_ecn_encap(key->tos, iip, skb); 953 ttl = key->ttl; 954 } else { 955 udp_csum = false; 956 err = geneve6_build_skb(dst, skb, 0, geneve->vni, 957 0, NULL, udp_csum, xnet); 958 if (unlikely(err)) 959 goto err; 960 961 prio = ip_tunnel_ecn_encap(fl6.flowi6_tos, iip, skb); 962 ttl = geneve->ttl; 963 if (!ttl && ipv6_addr_is_multicast(&fl6.daddr)) 964 ttl = 1; 965 ttl = ttl ? : ip6_dst_hoplimit(dst); 966 } 967 err = udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev, 968 &fl6.saddr, &fl6.daddr, prio, ttl, 969 sport, geneve->dst_port, !udp_csum); 970 971 iptunnel_xmit_stats(err, &dev->stats, dev->tstats); 972 return NETDEV_TX_OK; 973 974 tx_error: 975 dev_kfree_skb(skb); 976 err: 977 if (err == -ELOOP) 978 dev->stats.collisions++; 979 else if (err == -ENETUNREACH) 980 dev->stats.tx_carrier_errors++; 981 else 982 dev->stats.tx_errors++; 983 return NETDEV_TX_OK; 984 } 985 #endif 986 987 static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev) 988 { 989 struct geneve_dev *geneve = netdev_priv(dev); 990 struct ip_tunnel_info *info = NULL; 991 992 if (geneve->collect_md) 993 info = skb_tunnel_info(skb); 994 995 #if IS_ENABLED(CONFIG_IPV6) 996 if ((info && ip_tunnel_info_af(info) == AF_INET6) || 997 (!info && geneve->remote.sa.sa_family == AF_INET6)) 998 return geneve6_xmit_skb(skb, dev, info); 999 #endif 1000 return geneve_xmit_skb(skb, dev, info); 1001 } 1002 1003 static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) 1004 { 1005 struct ip_tunnel_info *info = skb_tunnel_info(skb); 1006 struct geneve_dev *geneve = netdev_priv(dev); 1007 struct rtable *rt; 1008 struct flowi4 fl4; 1009 #if IS_ENABLED(CONFIG_IPV6) 1010 struct dst_entry *dst; 1011 struct flowi6 fl6; 1012 #endif 1013 1014 if (ip_tunnel_info_af(info) == AF_INET) { 1015 rt = geneve_get_v4_rt(skb, dev, &fl4, info); 1016 if (IS_ERR(rt)) 1017 return PTR_ERR(rt); 1018 1019 ip_rt_put(rt); 1020 info->key.u.ipv4.src = fl4.saddr; 1021 #if IS_ENABLED(CONFIG_IPV6) 1022 } else if (ip_tunnel_info_af(info) == AF_INET6) { 1023 dst = geneve_get_v6_dst(skb, dev, &fl6, info); 1024 if (IS_ERR(dst)) 1025 return PTR_ERR(dst); 1026 1027 dst_release(dst); 1028 info->key.u.ipv6.src = fl6.saddr; 1029 #endif 1030 } else { 1031 return -EINVAL; 1032 } 1033 1034 info->key.tp_src = udp_flow_src_port(geneve->net, skb, 1035 1, USHRT_MAX, true); 1036 info->key.tp_dst = geneve->dst_port; 1037 return 0; 1038 } 1039 1040 static const struct net_device_ops geneve_netdev_ops = { 1041 .ndo_init = geneve_init, 1042 .ndo_uninit = geneve_uninit, 1043 .ndo_open = geneve_open, 1044 .ndo_stop = geneve_stop, 1045 .ndo_start_xmit = geneve_xmit, 1046 .ndo_get_stats64 = ip_tunnel_get_stats64, 1047 .ndo_change_mtu = eth_change_mtu, 1048 .ndo_validate_addr = eth_validate_addr, 1049 .ndo_set_mac_address = eth_mac_addr, 1050 .ndo_fill_metadata_dst = geneve_fill_metadata_dst, 1051 }; 1052 1053 static void geneve_get_drvinfo(struct net_device *dev, 1054 struct ethtool_drvinfo *drvinfo) 1055 { 1056 strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version)); 1057 strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver)); 1058 } 1059 1060 static const struct ethtool_ops geneve_ethtool_ops = { 1061 .get_drvinfo = geneve_get_drvinfo, 1062 .get_link = ethtool_op_get_link, 1063 }; 1064 1065 /* Info for udev, that this is a virtual tunnel endpoint */ 1066 static struct device_type geneve_type = { 1067 .name = "geneve", 1068 }; 1069 1070 /* Initialize the device structure. */ 1071 static void geneve_setup(struct net_device *dev) 1072 { 1073 ether_setup(dev); 1074 1075 dev->netdev_ops = &geneve_netdev_ops; 1076 dev->ethtool_ops = &geneve_ethtool_ops; 1077 dev->destructor = free_netdev; 1078 1079 SET_NETDEV_DEVTYPE(dev, &geneve_type); 1080 1081 dev->features |= NETIF_F_LLTX; 1082 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM; 1083 dev->features |= NETIF_F_RXCSUM; 1084 dev->features |= NETIF_F_GSO_SOFTWARE; 1085 1086 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM; 1087 dev->hw_features |= NETIF_F_GSO_SOFTWARE; 1088 1089 netif_keep_dst(dev); 1090 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE; 1091 eth_hw_addr_random(dev); 1092 } 1093 1094 static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = { 1095 [IFLA_GENEVE_ID] = { .type = NLA_U32 }, 1096 [IFLA_GENEVE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) }, 1097 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) }, 1098 [IFLA_GENEVE_TTL] = { .type = NLA_U8 }, 1099 [IFLA_GENEVE_TOS] = { .type = NLA_U8 }, 1100 [IFLA_GENEVE_PORT] = { .type = NLA_U16 }, 1101 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG }, 1102 }; 1103 1104 static int geneve_validate(struct nlattr *tb[], struct nlattr *data[]) 1105 { 1106 if (tb[IFLA_ADDRESS]) { 1107 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) 1108 return -EINVAL; 1109 1110 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) 1111 return -EADDRNOTAVAIL; 1112 } 1113 1114 if (!data) 1115 return -EINVAL; 1116 1117 if (data[IFLA_GENEVE_ID]) { 1118 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]); 1119 1120 if (vni >= GENEVE_VID_MASK) 1121 return -ERANGE; 1122 } 1123 1124 return 0; 1125 } 1126 1127 static struct geneve_dev *geneve_find_dev(struct geneve_net *gn, 1128 __be16 dst_port, 1129 union geneve_addr *remote, 1130 u8 vni[], 1131 bool *tun_on_same_port, 1132 bool *tun_collect_md) 1133 { 1134 struct geneve_dev *geneve, *t; 1135 1136 *tun_on_same_port = false; 1137 *tun_collect_md = false; 1138 t = NULL; 1139 list_for_each_entry(geneve, &gn->geneve_list, next) { 1140 if (geneve->dst_port == dst_port) { 1141 *tun_collect_md = geneve->collect_md; 1142 *tun_on_same_port = true; 1143 } 1144 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) && 1145 !memcmp(remote, &geneve->remote, sizeof(geneve->remote)) && 1146 dst_port == geneve->dst_port) 1147 t = geneve; 1148 } 1149 return t; 1150 } 1151 1152 static int geneve_configure(struct net *net, struct net_device *dev, 1153 union geneve_addr *remote, 1154 __u32 vni, __u8 ttl, __u8 tos, __be16 dst_port, 1155 bool metadata) 1156 { 1157 struct geneve_net *gn = net_generic(net, geneve_net_id); 1158 struct geneve_dev *t, *geneve = netdev_priv(dev); 1159 bool tun_collect_md, tun_on_same_port; 1160 int err; 1161 1162 if (!remote) 1163 return -EINVAL; 1164 if (metadata && 1165 (remote->sa.sa_family != AF_UNSPEC || vni || tos || ttl)) 1166 return -EINVAL; 1167 1168 geneve->net = net; 1169 geneve->dev = dev; 1170 1171 geneve->vni[0] = (vni & 0x00ff0000) >> 16; 1172 geneve->vni[1] = (vni & 0x0000ff00) >> 8; 1173 geneve->vni[2] = vni & 0x000000ff; 1174 1175 if ((remote->sa.sa_family == AF_INET && 1176 IN_MULTICAST(ntohl(remote->sin.sin_addr.s_addr))) || 1177 (remote->sa.sa_family == AF_INET6 && 1178 ipv6_addr_is_multicast(&remote->sin6.sin6_addr))) 1179 return -EINVAL; 1180 geneve->remote = *remote; 1181 1182 geneve->ttl = ttl; 1183 geneve->tos = tos; 1184 geneve->dst_port = dst_port; 1185 geneve->collect_md = metadata; 1186 1187 t = geneve_find_dev(gn, dst_port, remote, geneve->vni, 1188 &tun_on_same_port, &tun_collect_md); 1189 if (t) 1190 return -EBUSY; 1191 1192 if (metadata) { 1193 if (tun_on_same_port) 1194 return -EPERM; 1195 } else { 1196 if (tun_collect_md) 1197 return -EPERM; 1198 } 1199 1200 err = register_netdevice(dev); 1201 if (err) 1202 return err; 1203 1204 list_add(&geneve->next, &gn->geneve_list); 1205 return 0; 1206 } 1207 1208 static int geneve_newlink(struct net *net, struct net_device *dev, 1209 struct nlattr *tb[], struct nlattr *data[]) 1210 { 1211 __be16 dst_port = htons(GENEVE_UDP_PORT); 1212 __u8 ttl = 0, tos = 0; 1213 bool metadata = false; 1214 union geneve_addr remote = geneve_remote_unspec; 1215 __u32 vni = 0; 1216 1217 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6]) 1218 return -EINVAL; 1219 1220 if (data[IFLA_GENEVE_REMOTE]) { 1221 remote.sa.sa_family = AF_INET; 1222 remote.sin.sin_addr.s_addr = 1223 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]); 1224 } 1225 1226 if (data[IFLA_GENEVE_REMOTE6]) { 1227 if (!IS_ENABLED(CONFIG_IPV6)) 1228 return -EPFNOSUPPORT; 1229 1230 remote.sa.sa_family = AF_INET6; 1231 remote.sin6.sin6_addr = 1232 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]); 1233 1234 if (ipv6_addr_type(&remote.sin6.sin6_addr) & 1235 IPV6_ADDR_LINKLOCAL) { 1236 netdev_dbg(dev, "link-local remote is unsupported\n"); 1237 return -EINVAL; 1238 } 1239 } 1240 1241 if (data[IFLA_GENEVE_ID]) 1242 vni = nla_get_u32(data[IFLA_GENEVE_ID]); 1243 1244 if (data[IFLA_GENEVE_TTL]) 1245 ttl = nla_get_u8(data[IFLA_GENEVE_TTL]); 1246 1247 if (data[IFLA_GENEVE_TOS]) 1248 tos = nla_get_u8(data[IFLA_GENEVE_TOS]); 1249 1250 if (data[IFLA_GENEVE_PORT]) 1251 dst_port = nla_get_be16(data[IFLA_GENEVE_PORT]); 1252 1253 if (data[IFLA_GENEVE_COLLECT_METADATA]) 1254 metadata = true; 1255 1256 return geneve_configure(net, dev, &remote, vni, ttl, tos, dst_port, 1257 metadata); 1258 } 1259 1260 static void geneve_dellink(struct net_device *dev, struct list_head *head) 1261 { 1262 struct geneve_dev *geneve = netdev_priv(dev); 1263 1264 list_del(&geneve->next); 1265 unregister_netdevice_queue(dev, head); 1266 } 1267 1268 static size_t geneve_get_size(const struct net_device *dev) 1269 { 1270 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */ 1271 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */ 1272 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */ 1273 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */ 1274 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */ 1275 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */ 1276 0; 1277 } 1278 1279 static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev) 1280 { 1281 struct geneve_dev *geneve = netdev_priv(dev); 1282 __u32 vni; 1283 1284 vni = (geneve->vni[0] << 16) | (geneve->vni[1] << 8) | geneve->vni[2]; 1285 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni)) 1286 goto nla_put_failure; 1287 1288 if (geneve->remote.sa.sa_family == AF_INET) { 1289 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE, 1290 geneve->remote.sin.sin_addr.s_addr)) 1291 goto nla_put_failure; 1292 #if IS_ENABLED(CONFIG_IPV6) 1293 } else { 1294 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6, 1295 &geneve->remote.sin6.sin6_addr)) 1296 goto nla_put_failure; 1297 #endif 1298 } 1299 1300 if (nla_put_u8(skb, IFLA_GENEVE_TTL, geneve->ttl) || 1301 nla_put_u8(skb, IFLA_GENEVE_TOS, geneve->tos)) 1302 goto nla_put_failure; 1303 1304 if (nla_put_be16(skb, IFLA_GENEVE_PORT, geneve->dst_port)) 1305 goto nla_put_failure; 1306 1307 if (geneve->collect_md) { 1308 if (nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA)) 1309 goto nla_put_failure; 1310 } 1311 1312 return 0; 1313 1314 nla_put_failure: 1315 return -EMSGSIZE; 1316 } 1317 1318 static struct rtnl_link_ops geneve_link_ops __read_mostly = { 1319 .kind = "geneve", 1320 .maxtype = IFLA_GENEVE_MAX, 1321 .policy = geneve_policy, 1322 .priv_size = sizeof(struct geneve_dev), 1323 .setup = geneve_setup, 1324 .validate = geneve_validate, 1325 .newlink = geneve_newlink, 1326 .dellink = geneve_dellink, 1327 .get_size = geneve_get_size, 1328 .fill_info = geneve_fill_info, 1329 }; 1330 1331 struct net_device *geneve_dev_create_fb(struct net *net, const char *name, 1332 u8 name_assign_type, u16 dst_port) 1333 { 1334 struct nlattr *tb[IFLA_MAX + 1]; 1335 struct net_device *dev; 1336 int err; 1337 1338 memset(tb, 0, sizeof(tb)); 1339 dev = rtnl_create_link(net, name, name_assign_type, 1340 &geneve_link_ops, tb); 1341 if (IS_ERR(dev)) 1342 return dev; 1343 1344 err = geneve_configure(net, dev, &geneve_remote_unspec, 1345 0, 0, 0, htons(dst_port), true); 1346 if (err) { 1347 free_netdev(dev); 1348 return ERR_PTR(err); 1349 } 1350 return dev; 1351 } 1352 EXPORT_SYMBOL_GPL(geneve_dev_create_fb); 1353 1354 static __net_init int geneve_init_net(struct net *net) 1355 { 1356 struct geneve_net *gn = net_generic(net, geneve_net_id); 1357 1358 INIT_LIST_HEAD(&gn->geneve_list); 1359 INIT_LIST_HEAD(&gn->sock_list); 1360 return 0; 1361 } 1362 1363 static void __net_exit geneve_exit_net(struct net *net) 1364 { 1365 struct geneve_net *gn = net_generic(net, geneve_net_id); 1366 struct geneve_dev *geneve, *next; 1367 struct net_device *dev, *aux; 1368 LIST_HEAD(list); 1369 1370 rtnl_lock(); 1371 1372 /* gather any geneve devices that were moved into this ns */ 1373 for_each_netdev_safe(net, dev, aux) 1374 if (dev->rtnl_link_ops == &geneve_link_ops) 1375 unregister_netdevice_queue(dev, &list); 1376 1377 /* now gather any other geneve devices that were created in this ns */ 1378 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) { 1379 /* If geneve->dev is in the same netns, it was already added 1380 * to the list by the previous loop. 1381 */ 1382 if (!net_eq(dev_net(geneve->dev), net)) 1383 unregister_netdevice_queue(geneve->dev, &list); 1384 } 1385 1386 /* unregister the devices gathered above */ 1387 unregister_netdevice_many(&list); 1388 rtnl_unlock(); 1389 } 1390 1391 static struct pernet_operations geneve_net_ops = { 1392 .init = geneve_init_net, 1393 .exit = geneve_exit_net, 1394 .id = &geneve_net_id, 1395 .size = sizeof(struct geneve_net), 1396 }; 1397 1398 static int __init geneve_init_module(void) 1399 { 1400 int rc; 1401 1402 rc = register_pernet_subsys(&geneve_net_ops); 1403 if (rc) 1404 goto out1; 1405 1406 rc = rtnl_link_register(&geneve_link_ops); 1407 if (rc) 1408 goto out2; 1409 1410 return 0; 1411 out2: 1412 unregister_pernet_subsys(&geneve_net_ops); 1413 out1: 1414 return rc; 1415 } 1416 late_initcall(geneve_init_module); 1417 1418 static void __exit geneve_cleanup_module(void) 1419 { 1420 rtnl_link_unregister(&geneve_link_ops); 1421 unregister_pernet_subsys(&geneve_net_ops); 1422 } 1423 module_exit(geneve_cleanup_module); 1424 1425 MODULE_LICENSE("GPL"); 1426 MODULE_VERSION(GENEVE_NETDEV_VER); 1427 MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>"); 1428 MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic"); 1429 MODULE_ALIAS_RTNL_LINK("geneve"); 1430