1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * GENEVE: Generic Network Virtualization Encapsulation 4 * 5 * Copyright (c) 2015 Red Hat, Inc. 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/ipv6_stubs.h> 15 #include <net/dst_metadata.h> 16 #include <net/gro_cells.h> 17 #include <net/rtnetlink.h> 18 #include <net/geneve.h> 19 #include <net/protocol.h> 20 21 #define GENEVE_NETDEV_VER "0.6" 22 23 #define GENEVE_N_VID (1u << 24) 24 #define GENEVE_VID_MASK (GENEVE_N_VID - 1) 25 26 #define VNI_HASH_BITS 10 27 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS) 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 #define GENEVE_VER 0 34 #define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr)) 35 #define GENEVE_IPV4_HLEN (ETH_HLEN + sizeof(struct iphdr) + GENEVE_BASE_HLEN) 36 #define GENEVE_IPV6_HLEN (ETH_HLEN + sizeof(struct ipv6hdr) + GENEVE_BASE_HLEN) 37 38 /* per-network namespace private data for this module */ 39 struct geneve_net { 40 struct list_head geneve_list; 41 struct list_head sock_list; 42 }; 43 44 static unsigned int geneve_net_id; 45 46 struct geneve_dev_node { 47 struct hlist_node hlist; 48 struct geneve_dev *geneve; 49 }; 50 51 /* Pseudo network device */ 52 struct geneve_dev { 53 struct geneve_dev_node hlist4; /* vni hash table for IPv4 socket */ 54 #if IS_ENABLED(CONFIG_IPV6) 55 struct geneve_dev_node hlist6; /* vni hash table for IPv6 socket */ 56 #endif 57 struct net *net; /* netns for packet i/o */ 58 struct net_device *dev; /* netdev for geneve tunnel */ 59 struct ip_tunnel_info info; 60 struct geneve_sock __rcu *sock4; /* IPv4 socket used for geneve tunnel */ 61 #if IS_ENABLED(CONFIG_IPV6) 62 struct geneve_sock __rcu *sock6; /* IPv6 socket used for geneve tunnel */ 63 #endif 64 struct list_head next; /* geneve's per namespace list */ 65 struct gro_cells gro_cells; 66 bool collect_md; 67 bool use_udp6_rx_checksums; 68 bool ttl_inherit; 69 enum ifla_geneve_df df; 70 }; 71 72 struct geneve_sock { 73 bool collect_md; 74 struct list_head list; 75 struct socket *sock; 76 struct rcu_head rcu; 77 int refcnt; 78 struct hlist_head vni_list[VNI_HASH_SIZE]; 79 }; 80 81 static inline __u32 geneve_net_vni_hash(u8 vni[3]) 82 { 83 __u32 vnid; 84 85 vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2]; 86 return hash_32(vnid, VNI_HASH_BITS); 87 } 88 89 static __be64 vni_to_tunnel_id(const __u8 *vni) 90 { 91 #ifdef __BIG_ENDIAN 92 return (vni[0] << 16) | (vni[1] << 8) | vni[2]; 93 #else 94 return (__force __be64)(((__force u64)vni[0] << 40) | 95 ((__force u64)vni[1] << 48) | 96 ((__force u64)vni[2] << 56)); 97 #endif 98 } 99 100 /* Convert 64 bit tunnel ID to 24 bit VNI. */ 101 static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni) 102 { 103 #ifdef __BIG_ENDIAN 104 vni[0] = (__force __u8)(tun_id >> 16); 105 vni[1] = (__force __u8)(tun_id >> 8); 106 vni[2] = (__force __u8)tun_id; 107 #else 108 vni[0] = (__force __u8)((__force u64)tun_id >> 40); 109 vni[1] = (__force __u8)((__force u64)tun_id >> 48); 110 vni[2] = (__force __u8)((__force u64)tun_id >> 56); 111 #endif 112 } 113 114 static bool eq_tun_id_and_vni(u8 *tun_id, u8 *vni) 115 { 116 return !memcmp(vni, &tun_id[5], 3); 117 } 118 119 static sa_family_t geneve_get_sk_family(struct geneve_sock *gs) 120 { 121 return gs->sock->sk->sk_family; 122 } 123 124 static struct geneve_dev *geneve_lookup(struct geneve_sock *gs, 125 __be32 addr, u8 vni[]) 126 { 127 struct hlist_head *vni_list_head; 128 struct geneve_dev_node *node; 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(node, vni_list_head, hlist) { 135 if (eq_tun_id_and_vni((u8 *)&node->geneve->info.key.tun_id, vni) && 136 addr == node->geneve->info.key.u.ipv4.dst) 137 return node->geneve; 138 } 139 return NULL; 140 } 141 142 #if IS_ENABLED(CONFIG_IPV6) 143 static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs, 144 struct in6_addr addr6, u8 vni[]) 145 { 146 struct hlist_head *vni_list_head; 147 struct geneve_dev_node *node; 148 __u32 hash; 149 150 /* Find the device for this VNI */ 151 hash = geneve_net_vni_hash(vni); 152 vni_list_head = &gs->vni_list[hash]; 153 hlist_for_each_entry_rcu(node, vni_list_head, hlist) { 154 if (eq_tun_id_and_vni((u8 *)&node->geneve->info.key.tun_id, vni) && 155 ipv6_addr_equal(&addr6, &node->geneve->info.key.u.ipv6.dst)) 156 return node->geneve; 157 } 158 return NULL; 159 } 160 #endif 161 162 static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb) 163 { 164 return (struct genevehdr *)(udp_hdr(skb) + 1); 165 } 166 167 static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs, 168 struct sk_buff *skb) 169 { 170 static u8 zero_vni[3]; 171 u8 *vni; 172 173 if (geneve_get_sk_family(gs) == AF_INET) { 174 struct iphdr *iph; 175 __be32 addr; 176 177 iph = ip_hdr(skb); /* outer IP header... */ 178 179 if (gs->collect_md) { 180 vni = zero_vni; 181 addr = 0; 182 } else { 183 vni = geneve_hdr(skb)->vni; 184 addr = iph->saddr; 185 } 186 187 return geneve_lookup(gs, addr, vni); 188 #if IS_ENABLED(CONFIG_IPV6) 189 } else if (geneve_get_sk_family(gs) == AF_INET6) { 190 static struct in6_addr zero_addr6; 191 struct ipv6hdr *ip6h; 192 struct in6_addr addr6; 193 194 ip6h = ipv6_hdr(skb); /* outer IPv6 header... */ 195 196 if (gs->collect_md) { 197 vni = zero_vni; 198 addr6 = zero_addr6; 199 } else { 200 vni = geneve_hdr(skb)->vni; 201 addr6 = ip6h->saddr; 202 } 203 204 return geneve6_lookup(gs, addr6, vni); 205 #endif 206 } 207 return NULL; 208 } 209 210 /* geneve receive/decap routine */ 211 static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs, 212 struct sk_buff *skb) 213 { 214 struct genevehdr *gnvh = geneve_hdr(skb); 215 struct metadata_dst *tun_dst = NULL; 216 struct pcpu_sw_netstats *stats; 217 unsigned int len; 218 int err = 0; 219 void *oiph; 220 221 if (ip_tunnel_collect_metadata() || gs->collect_md) { 222 __be16 flags; 223 224 flags = TUNNEL_KEY | TUNNEL_GENEVE_OPT | 225 (gnvh->oam ? TUNNEL_OAM : 0) | 226 (gnvh->critical ? TUNNEL_CRIT_OPT : 0); 227 228 tun_dst = udp_tun_rx_dst(skb, geneve_get_sk_family(gs), flags, 229 vni_to_tunnel_id(gnvh->vni), 230 gnvh->opt_len * 4); 231 if (!tun_dst) { 232 geneve->dev->stats.rx_dropped++; 233 goto drop; 234 } 235 /* Update tunnel dst according to Geneve options. */ 236 ip_tunnel_info_opts_set(&tun_dst->u.tun_info, 237 gnvh->options, gnvh->opt_len * 4, 238 TUNNEL_GENEVE_OPT); 239 } else { 240 /* Drop packets w/ critical options, 241 * since we don't support any... 242 */ 243 if (gnvh->critical) { 244 geneve->dev->stats.rx_frame_errors++; 245 geneve->dev->stats.rx_errors++; 246 goto drop; 247 } 248 } 249 250 skb_reset_mac_header(skb); 251 skb->protocol = eth_type_trans(skb, geneve->dev); 252 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); 253 254 if (tun_dst) 255 skb_dst_set(skb, &tun_dst->dst); 256 257 /* Ignore packet loops (and multicast echo) */ 258 if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr)) { 259 geneve->dev->stats.rx_errors++; 260 goto drop; 261 } 262 263 oiph = skb_network_header(skb); 264 skb_reset_network_header(skb); 265 266 if (geneve_get_sk_family(gs) == AF_INET) 267 err = IP_ECN_decapsulate(oiph, skb); 268 #if IS_ENABLED(CONFIG_IPV6) 269 else 270 err = IP6_ECN_decapsulate(oiph, skb); 271 #endif 272 273 if (unlikely(err)) { 274 if (log_ecn_error) { 275 if (geneve_get_sk_family(gs) == AF_INET) 276 net_info_ratelimited("non-ECT from %pI4 " 277 "with TOS=%#x\n", 278 &((struct iphdr *)oiph)->saddr, 279 ((struct iphdr *)oiph)->tos); 280 #if IS_ENABLED(CONFIG_IPV6) 281 else 282 net_info_ratelimited("non-ECT from %pI6\n", 283 &((struct ipv6hdr *)oiph)->saddr); 284 #endif 285 } 286 if (err > 1) { 287 ++geneve->dev->stats.rx_frame_errors; 288 ++geneve->dev->stats.rx_errors; 289 goto drop; 290 } 291 } 292 293 len = skb->len; 294 err = gro_cells_receive(&geneve->gro_cells, skb); 295 if (likely(err == NET_RX_SUCCESS)) { 296 stats = this_cpu_ptr(geneve->dev->tstats); 297 u64_stats_update_begin(&stats->syncp); 298 stats->rx_packets++; 299 stats->rx_bytes += len; 300 u64_stats_update_end(&stats->syncp); 301 } 302 return; 303 drop: 304 /* Consume bad packet */ 305 kfree_skb(skb); 306 } 307 308 /* Setup stats when device is created */ 309 static int geneve_init(struct net_device *dev) 310 { 311 struct geneve_dev *geneve = netdev_priv(dev); 312 int err; 313 314 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); 315 if (!dev->tstats) 316 return -ENOMEM; 317 318 err = gro_cells_init(&geneve->gro_cells, dev); 319 if (err) { 320 free_percpu(dev->tstats); 321 return err; 322 } 323 324 err = dst_cache_init(&geneve->info.dst_cache, GFP_KERNEL); 325 if (err) { 326 free_percpu(dev->tstats); 327 gro_cells_destroy(&geneve->gro_cells); 328 return err; 329 } 330 return 0; 331 } 332 333 static void geneve_uninit(struct net_device *dev) 334 { 335 struct geneve_dev *geneve = netdev_priv(dev); 336 337 dst_cache_destroy(&geneve->info.dst_cache); 338 gro_cells_destroy(&geneve->gro_cells); 339 free_percpu(dev->tstats); 340 } 341 342 /* Callback from net/ipv4/udp.c to receive packets */ 343 static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb) 344 { 345 struct genevehdr *geneveh; 346 struct geneve_dev *geneve; 347 struct geneve_sock *gs; 348 int opts_len; 349 350 /* Need UDP and Geneve header to be present */ 351 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN))) 352 goto drop; 353 354 /* Return packets with reserved bits set */ 355 geneveh = geneve_hdr(skb); 356 if (unlikely(geneveh->ver != GENEVE_VER)) 357 goto drop; 358 359 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB))) 360 goto drop; 361 362 gs = rcu_dereference_sk_user_data(sk); 363 if (!gs) 364 goto drop; 365 366 geneve = geneve_lookup_skb(gs, skb); 367 if (!geneve) 368 goto drop; 369 370 opts_len = geneveh->opt_len * 4; 371 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len, 372 htons(ETH_P_TEB), 373 !net_eq(geneve->net, dev_net(geneve->dev)))) { 374 geneve->dev->stats.rx_dropped++; 375 goto drop; 376 } 377 378 geneve_rx(geneve, gs, skb); 379 return 0; 380 381 drop: 382 /* Consume bad packet */ 383 kfree_skb(skb); 384 return 0; 385 } 386 387 /* Callback from net/ipv{4,6}/udp.c to check that we have a tunnel for errors */ 388 static int geneve_udp_encap_err_lookup(struct sock *sk, struct sk_buff *skb) 389 { 390 struct genevehdr *geneveh; 391 struct geneve_sock *gs; 392 u8 zero_vni[3] = { 0 }; 393 u8 *vni = zero_vni; 394 395 if (!pskb_may_pull(skb, skb_transport_offset(skb) + GENEVE_BASE_HLEN)) 396 return -EINVAL; 397 398 geneveh = geneve_hdr(skb); 399 if (geneveh->ver != GENEVE_VER) 400 return -EINVAL; 401 402 if (geneveh->proto_type != htons(ETH_P_TEB)) 403 return -EINVAL; 404 405 gs = rcu_dereference_sk_user_data(sk); 406 if (!gs) 407 return -ENOENT; 408 409 if (geneve_get_sk_family(gs) == AF_INET) { 410 struct iphdr *iph = ip_hdr(skb); 411 __be32 addr4 = 0; 412 413 if (!gs->collect_md) { 414 vni = geneve_hdr(skb)->vni; 415 addr4 = iph->daddr; 416 } 417 418 return geneve_lookup(gs, addr4, vni) ? 0 : -ENOENT; 419 } 420 421 #if IS_ENABLED(CONFIG_IPV6) 422 if (geneve_get_sk_family(gs) == AF_INET6) { 423 struct ipv6hdr *ip6h = ipv6_hdr(skb); 424 struct in6_addr addr6; 425 426 memset(&addr6, 0, sizeof(struct in6_addr)); 427 428 if (!gs->collect_md) { 429 vni = geneve_hdr(skb)->vni; 430 addr6 = ip6h->daddr; 431 } 432 433 return geneve6_lookup(gs, addr6, vni) ? 0 : -ENOENT; 434 } 435 #endif 436 437 return -EPFNOSUPPORT; 438 } 439 440 static struct socket *geneve_create_sock(struct net *net, bool ipv6, 441 __be16 port, bool ipv6_rx_csum) 442 { 443 struct socket *sock; 444 struct udp_port_cfg udp_conf; 445 int err; 446 447 memset(&udp_conf, 0, sizeof(udp_conf)); 448 449 if (ipv6) { 450 udp_conf.family = AF_INET6; 451 udp_conf.ipv6_v6only = 1; 452 udp_conf.use_udp6_rx_checksums = ipv6_rx_csum; 453 } else { 454 udp_conf.family = AF_INET; 455 udp_conf.local_ip.s_addr = htonl(INADDR_ANY); 456 } 457 458 udp_conf.local_udp_port = port; 459 460 /* Open UDP socket */ 461 err = udp_sock_create(net, &udp_conf, &sock); 462 if (err < 0) 463 return ERR_PTR(err); 464 465 return sock; 466 } 467 468 static int geneve_hlen(struct genevehdr *gh) 469 { 470 return sizeof(*gh) + gh->opt_len * 4; 471 } 472 473 static struct sk_buff *geneve_gro_receive(struct sock *sk, 474 struct list_head *head, 475 struct sk_buff *skb) 476 { 477 struct sk_buff *pp = NULL; 478 struct sk_buff *p; 479 struct genevehdr *gh, *gh2; 480 unsigned int hlen, gh_len, off_gnv; 481 const struct packet_offload *ptype; 482 __be16 type; 483 int flush = 1; 484 485 off_gnv = skb_gro_offset(skb); 486 hlen = off_gnv + sizeof(*gh); 487 gh = skb_gro_header_fast(skb, off_gnv); 488 if (skb_gro_header_hard(skb, hlen)) { 489 gh = skb_gro_header_slow(skb, hlen, off_gnv); 490 if (unlikely(!gh)) 491 goto out; 492 } 493 494 if (gh->ver != GENEVE_VER || gh->oam) 495 goto out; 496 gh_len = geneve_hlen(gh); 497 498 hlen = off_gnv + gh_len; 499 if (skb_gro_header_hard(skb, hlen)) { 500 gh = skb_gro_header_slow(skb, hlen, off_gnv); 501 if (unlikely(!gh)) 502 goto out; 503 } 504 505 list_for_each_entry(p, head, list) { 506 if (!NAPI_GRO_CB(p)->same_flow) 507 continue; 508 509 gh2 = (struct genevehdr *)(p->data + off_gnv); 510 if (gh->opt_len != gh2->opt_len || 511 memcmp(gh, gh2, gh_len)) { 512 NAPI_GRO_CB(p)->same_flow = 0; 513 continue; 514 } 515 } 516 517 type = gh->proto_type; 518 519 rcu_read_lock(); 520 ptype = gro_find_receive_by_type(type); 521 if (!ptype) 522 goto out_unlock; 523 524 skb_gro_pull(skb, gh_len); 525 skb_gro_postpull_rcsum(skb, gh, gh_len); 526 pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb); 527 flush = 0; 528 529 out_unlock: 530 rcu_read_unlock(); 531 out: 532 skb_gro_flush_final(skb, pp, flush); 533 534 return pp; 535 } 536 537 static int geneve_gro_complete(struct sock *sk, struct sk_buff *skb, 538 int nhoff) 539 { 540 struct genevehdr *gh; 541 struct packet_offload *ptype; 542 __be16 type; 543 int gh_len; 544 int err = -ENOSYS; 545 546 gh = (struct genevehdr *)(skb->data + nhoff); 547 gh_len = geneve_hlen(gh); 548 type = gh->proto_type; 549 550 rcu_read_lock(); 551 ptype = gro_find_complete_by_type(type); 552 if (ptype) 553 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len); 554 555 rcu_read_unlock(); 556 557 skb_set_inner_mac_header(skb, nhoff + gh_len); 558 559 return err; 560 } 561 562 /* Create new listen socket if needed */ 563 static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port, 564 bool ipv6, bool ipv6_rx_csum) 565 { 566 struct geneve_net *gn = net_generic(net, geneve_net_id); 567 struct geneve_sock *gs; 568 struct socket *sock; 569 struct udp_tunnel_sock_cfg tunnel_cfg; 570 int h; 571 572 gs = kzalloc(sizeof(*gs), GFP_KERNEL); 573 if (!gs) 574 return ERR_PTR(-ENOMEM); 575 576 sock = geneve_create_sock(net, ipv6, port, ipv6_rx_csum); 577 if (IS_ERR(sock)) { 578 kfree(gs); 579 return ERR_CAST(sock); 580 } 581 582 gs->sock = sock; 583 gs->refcnt = 1; 584 for (h = 0; h < VNI_HASH_SIZE; ++h) 585 INIT_HLIST_HEAD(&gs->vni_list[h]); 586 587 /* Initialize the geneve udp offloads structure */ 588 udp_tunnel_notify_add_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE); 589 590 /* Mark socket as an encapsulation socket */ 591 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg)); 592 tunnel_cfg.sk_user_data = gs; 593 tunnel_cfg.encap_type = 1; 594 tunnel_cfg.gro_receive = geneve_gro_receive; 595 tunnel_cfg.gro_complete = geneve_gro_complete; 596 tunnel_cfg.encap_rcv = geneve_udp_encap_recv; 597 tunnel_cfg.encap_err_lookup = geneve_udp_encap_err_lookup; 598 tunnel_cfg.encap_destroy = NULL; 599 setup_udp_tunnel_sock(net, sock, &tunnel_cfg); 600 list_add(&gs->list, &gn->sock_list); 601 return gs; 602 } 603 604 static void __geneve_sock_release(struct geneve_sock *gs) 605 { 606 if (!gs || --gs->refcnt) 607 return; 608 609 list_del(&gs->list); 610 udp_tunnel_notify_del_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE); 611 udp_tunnel_sock_release(gs->sock); 612 kfree_rcu(gs, rcu); 613 } 614 615 static void geneve_sock_release(struct geneve_dev *geneve) 616 { 617 struct geneve_sock *gs4 = rtnl_dereference(geneve->sock4); 618 #if IS_ENABLED(CONFIG_IPV6) 619 struct geneve_sock *gs6 = rtnl_dereference(geneve->sock6); 620 621 rcu_assign_pointer(geneve->sock6, NULL); 622 #endif 623 624 rcu_assign_pointer(geneve->sock4, NULL); 625 synchronize_net(); 626 627 __geneve_sock_release(gs4); 628 #if IS_ENABLED(CONFIG_IPV6) 629 __geneve_sock_release(gs6); 630 #endif 631 } 632 633 static struct geneve_sock *geneve_find_sock(struct geneve_net *gn, 634 sa_family_t family, 635 __be16 dst_port) 636 { 637 struct geneve_sock *gs; 638 639 list_for_each_entry(gs, &gn->sock_list, list) { 640 if (inet_sk(gs->sock->sk)->inet_sport == dst_port && 641 geneve_get_sk_family(gs) == family) { 642 return gs; 643 } 644 } 645 return NULL; 646 } 647 648 static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6) 649 { 650 struct net *net = geneve->net; 651 struct geneve_net *gn = net_generic(net, geneve_net_id); 652 struct geneve_dev_node *node; 653 struct geneve_sock *gs; 654 __u8 vni[3]; 655 __u32 hash; 656 657 gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->info.key.tp_dst); 658 if (gs) { 659 gs->refcnt++; 660 goto out; 661 } 662 663 gs = geneve_socket_create(net, geneve->info.key.tp_dst, ipv6, 664 geneve->use_udp6_rx_checksums); 665 if (IS_ERR(gs)) 666 return PTR_ERR(gs); 667 668 out: 669 gs->collect_md = geneve->collect_md; 670 #if IS_ENABLED(CONFIG_IPV6) 671 if (ipv6) { 672 rcu_assign_pointer(geneve->sock6, gs); 673 node = &geneve->hlist6; 674 } else 675 #endif 676 { 677 rcu_assign_pointer(geneve->sock4, gs); 678 node = &geneve->hlist4; 679 } 680 node->geneve = geneve; 681 682 tunnel_id_to_vni(geneve->info.key.tun_id, vni); 683 hash = geneve_net_vni_hash(vni); 684 hlist_add_head_rcu(&node->hlist, &gs->vni_list[hash]); 685 return 0; 686 } 687 688 static int geneve_open(struct net_device *dev) 689 { 690 struct geneve_dev *geneve = netdev_priv(dev); 691 bool metadata = geneve->collect_md; 692 bool ipv4, ipv6; 693 int ret = 0; 694 695 ipv6 = geneve->info.mode & IP_TUNNEL_INFO_IPV6 || metadata; 696 ipv4 = !ipv6 || metadata; 697 #if IS_ENABLED(CONFIG_IPV6) 698 if (ipv6) { 699 ret = geneve_sock_add(geneve, true); 700 if (ret < 0 && ret != -EAFNOSUPPORT) 701 ipv4 = false; 702 } 703 #endif 704 if (ipv4) 705 ret = geneve_sock_add(geneve, false); 706 if (ret < 0) 707 geneve_sock_release(geneve); 708 709 return ret; 710 } 711 712 static int geneve_stop(struct net_device *dev) 713 { 714 struct geneve_dev *geneve = netdev_priv(dev); 715 716 hlist_del_init_rcu(&geneve->hlist4.hlist); 717 #if IS_ENABLED(CONFIG_IPV6) 718 hlist_del_init_rcu(&geneve->hlist6.hlist); 719 #endif 720 geneve_sock_release(geneve); 721 return 0; 722 } 723 724 static void geneve_build_header(struct genevehdr *geneveh, 725 const struct ip_tunnel_info *info) 726 { 727 geneveh->ver = GENEVE_VER; 728 geneveh->opt_len = info->options_len / 4; 729 geneveh->oam = !!(info->key.tun_flags & TUNNEL_OAM); 730 geneveh->critical = !!(info->key.tun_flags & TUNNEL_CRIT_OPT); 731 geneveh->rsvd1 = 0; 732 tunnel_id_to_vni(info->key.tun_id, geneveh->vni); 733 geneveh->proto_type = htons(ETH_P_TEB); 734 geneveh->rsvd2 = 0; 735 736 if (info->key.tun_flags & TUNNEL_GENEVE_OPT) 737 ip_tunnel_info_opts_get(geneveh->options, info); 738 } 739 740 static int geneve_build_skb(struct dst_entry *dst, struct sk_buff *skb, 741 const struct ip_tunnel_info *info, 742 bool xnet, int ip_hdr_len) 743 { 744 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM); 745 struct genevehdr *gnvh; 746 int min_headroom; 747 int err; 748 749 skb_reset_mac_header(skb); 750 skb_scrub_packet(skb, xnet); 751 752 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len + 753 GENEVE_BASE_HLEN + info->options_len + ip_hdr_len; 754 err = skb_cow_head(skb, min_headroom); 755 if (unlikely(err)) 756 goto free_dst; 757 758 err = udp_tunnel_handle_offloads(skb, udp_sum); 759 if (err) 760 goto free_dst; 761 762 gnvh = __skb_push(skb, sizeof(*gnvh) + info->options_len); 763 geneve_build_header(gnvh, info); 764 skb_set_inner_protocol(skb, htons(ETH_P_TEB)); 765 return 0; 766 767 free_dst: 768 dst_release(dst); 769 return err; 770 } 771 772 static struct rtable *geneve_get_v4_rt(struct sk_buff *skb, 773 struct net_device *dev, 774 struct geneve_sock *gs4, 775 struct flowi4 *fl4, 776 const struct ip_tunnel_info *info) 777 { 778 bool use_cache = ip_tunnel_dst_cache_usable(skb, info); 779 struct geneve_dev *geneve = netdev_priv(dev); 780 struct dst_cache *dst_cache; 781 struct rtable *rt = NULL; 782 __u8 tos; 783 784 if (!gs4) 785 return ERR_PTR(-EIO); 786 787 memset(fl4, 0, sizeof(*fl4)); 788 fl4->flowi4_mark = skb->mark; 789 fl4->flowi4_proto = IPPROTO_UDP; 790 fl4->daddr = info->key.u.ipv4.dst; 791 fl4->saddr = info->key.u.ipv4.src; 792 793 tos = info->key.tos; 794 if ((tos == 1) && !geneve->collect_md) { 795 tos = ip_tunnel_get_dsfield(ip_hdr(skb), skb); 796 use_cache = false; 797 } 798 fl4->flowi4_tos = RT_TOS(tos); 799 800 dst_cache = (struct dst_cache *)&info->dst_cache; 801 if (use_cache) { 802 rt = dst_cache_get_ip4(dst_cache, &fl4->saddr); 803 if (rt) 804 return rt; 805 } 806 rt = ip_route_output_key(geneve->net, fl4); 807 if (IS_ERR(rt)) { 808 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr); 809 return ERR_PTR(-ENETUNREACH); 810 } 811 if (rt->dst.dev == dev) { /* is this necessary? */ 812 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr); 813 ip_rt_put(rt); 814 return ERR_PTR(-ELOOP); 815 } 816 if (use_cache) 817 dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr); 818 return rt; 819 } 820 821 #if IS_ENABLED(CONFIG_IPV6) 822 static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb, 823 struct net_device *dev, 824 struct geneve_sock *gs6, 825 struct flowi6 *fl6, 826 const struct ip_tunnel_info *info) 827 { 828 bool use_cache = ip_tunnel_dst_cache_usable(skb, info); 829 struct geneve_dev *geneve = netdev_priv(dev); 830 struct dst_entry *dst = NULL; 831 struct dst_cache *dst_cache; 832 __u8 prio; 833 834 if (!gs6) 835 return ERR_PTR(-EIO); 836 837 memset(fl6, 0, sizeof(*fl6)); 838 fl6->flowi6_mark = skb->mark; 839 fl6->flowi6_proto = IPPROTO_UDP; 840 fl6->daddr = info->key.u.ipv6.dst; 841 fl6->saddr = info->key.u.ipv6.src; 842 prio = info->key.tos; 843 if ((prio == 1) && !geneve->collect_md) { 844 prio = ip_tunnel_get_dsfield(ip_hdr(skb), skb); 845 use_cache = false; 846 } 847 848 fl6->flowlabel = ip6_make_flowinfo(RT_TOS(prio), 849 info->key.label); 850 dst_cache = (struct dst_cache *)&info->dst_cache; 851 if (use_cache) { 852 dst = dst_cache_get_ip6(dst_cache, &fl6->saddr); 853 if (dst) 854 return dst; 855 } 856 dst = ipv6_stub->ipv6_dst_lookup_flow(geneve->net, gs6->sock->sk, fl6, 857 NULL); 858 if (IS_ERR(dst)) { 859 netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr); 860 return ERR_PTR(-ENETUNREACH); 861 } 862 if (dst->dev == dev) { /* is this necessary? */ 863 netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr); 864 dst_release(dst); 865 return ERR_PTR(-ELOOP); 866 } 867 868 if (use_cache) 869 dst_cache_set_ip6(dst_cache, dst, &fl6->saddr); 870 return dst; 871 } 872 #endif 873 874 static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev, 875 struct geneve_dev *geneve, 876 const struct ip_tunnel_info *info) 877 { 878 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev)); 879 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4); 880 const struct ip_tunnel_key *key = &info->key; 881 struct rtable *rt; 882 struct flowi4 fl4; 883 __u8 tos, ttl; 884 __be16 df = 0; 885 __be16 sport; 886 int err; 887 888 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info); 889 if (IS_ERR(rt)) 890 return PTR_ERR(rt); 891 892 skb_tunnel_check_pmtu(skb, &rt->dst, 893 GENEVE_IPV4_HLEN + info->options_len); 894 895 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true); 896 if (geneve->collect_md) { 897 tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb); 898 ttl = key->ttl; 899 900 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0; 901 } else { 902 tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, ip_hdr(skb), skb); 903 if (geneve->ttl_inherit) 904 ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb); 905 else 906 ttl = key->ttl; 907 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst); 908 909 if (geneve->df == GENEVE_DF_SET) { 910 df = htons(IP_DF); 911 } else if (geneve->df == GENEVE_DF_INHERIT) { 912 struct ethhdr *eth = eth_hdr(skb); 913 914 if (ntohs(eth->h_proto) == ETH_P_IPV6) { 915 df = htons(IP_DF); 916 } else if (ntohs(eth->h_proto) == ETH_P_IP) { 917 struct iphdr *iph = ip_hdr(skb); 918 919 if (iph->frag_off & htons(IP_DF)) 920 df = htons(IP_DF); 921 } 922 } 923 } 924 925 err = geneve_build_skb(&rt->dst, skb, info, xnet, sizeof(struct iphdr)); 926 if (unlikely(err)) 927 return err; 928 929 udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr, 930 tos, ttl, df, sport, geneve->info.key.tp_dst, 931 !net_eq(geneve->net, dev_net(geneve->dev)), 932 !(info->key.tun_flags & TUNNEL_CSUM)); 933 return 0; 934 } 935 936 #if IS_ENABLED(CONFIG_IPV6) 937 static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev, 938 struct geneve_dev *geneve, 939 const struct ip_tunnel_info *info) 940 { 941 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev)); 942 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6); 943 const struct ip_tunnel_key *key = &info->key; 944 struct dst_entry *dst = NULL; 945 struct flowi6 fl6; 946 __u8 prio, ttl; 947 __be16 sport; 948 int err; 949 950 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info); 951 if (IS_ERR(dst)) 952 return PTR_ERR(dst); 953 954 skb_tunnel_check_pmtu(skb, dst, GENEVE_IPV6_HLEN + info->options_len); 955 956 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true); 957 if (geneve->collect_md) { 958 prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb); 959 ttl = key->ttl; 960 } else { 961 prio = ip_tunnel_ecn_encap(ip6_tclass(fl6.flowlabel), 962 ip_hdr(skb), skb); 963 if (geneve->ttl_inherit) 964 ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb); 965 else 966 ttl = key->ttl; 967 ttl = ttl ? : ip6_dst_hoplimit(dst); 968 } 969 err = geneve_build_skb(dst, skb, info, xnet, sizeof(struct ipv6hdr)); 970 if (unlikely(err)) 971 return err; 972 973 udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev, 974 &fl6.saddr, &fl6.daddr, prio, ttl, 975 info->key.label, sport, geneve->info.key.tp_dst, 976 !(info->key.tun_flags & TUNNEL_CSUM)); 977 return 0; 978 } 979 #endif 980 981 static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev) 982 { 983 struct geneve_dev *geneve = netdev_priv(dev); 984 struct ip_tunnel_info *info = NULL; 985 int err; 986 987 if (geneve->collect_md) { 988 info = skb_tunnel_info(skb); 989 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) { 990 err = -EINVAL; 991 netdev_dbg(dev, "no tunnel metadata\n"); 992 goto tx_error; 993 } 994 } else { 995 info = &geneve->info; 996 } 997 998 rcu_read_lock(); 999 #if IS_ENABLED(CONFIG_IPV6) 1000 if (info->mode & IP_TUNNEL_INFO_IPV6) 1001 err = geneve6_xmit_skb(skb, dev, geneve, info); 1002 else 1003 #endif 1004 err = geneve_xmit_skb(skb, dev, geneve, info); 1005 rcu_read_unlock(); 1006 1007 if (likely(!err)) 1008 return NETDEV_TX_OK; 1009 tx_error: 1010 dev_kfree_skb(skb); 1011 1012 if (err == -ELOOP) 1013 dev->stats.collisions++; 1014 else if (err == -ENETUNREACH) 1015 dev->stats.tx_carrier_errors++; 1016 1017 dev->stats.tx_errors++; 1018 return NETDEV_TX_OK; 1019 } 1020 1021 static int geneve_change_mtu(struct net_device *dev, int new_mtu) 1022 { 1023 if (new_mtu > dev->max_mtu) 1024 new_mtu = dev->max_mtu; 1025 else if (new_mtu < dev->min_mtu) 1026 new_mtu = dev->min_mtu; 1027 1028 dev->mtu = new_mtu; 1029 return 0; 1030 } 1031 1032 static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) 1033 { 1034 struct ip_tunnel_info *info = skb_tunnel_info(skb); 1035 struct geneve_dev *geneve = netdev_priv(dev); 1036 1037 if (ip_tunnel_info_af(info) == AF_INET) { 1038 struct rtable *rt; 1039 struct flowi4 fl4; 1040 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4); 1041 1042 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info); 1043 if (IS_ERR(rt)) 1044 return PTR_ERR(rt); 1045 1046 ip_rt_put(rt); 1047 info->key.u.ipv4.src = fl4.saddr; 1048 #if IS_ENABLED(CONFIG_IPV6) 1049 } else if (ip_tunnel_info_af(info) == AF_INET6) { 1050 struct dst_entry *dst; 1051 struct flowi6 fl6; 1052 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6); 1053 1054 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info); 1055 if (IS_ERR(dst)) 1056 return PTR_ERR(dst); 1057 1058 dst_release(dst); 1059 info->key.u.ipv6.src = fl6.saddr; 1060 #endif 1061 } else { 1062 return -EINVAL; 1063 } 1064 1065 info->key.tp_src = udp_flow_src_port(geneve->net, skb, 1066 1, USHRT_MAX, true); 1067 info->key.tp_dst = geneve->info.key.tp_dst; 1068 return 0; 1069 } 1070 1071 static const struct net_device_ops geneve_netdev_ops = { 1072 .ndo_init = geneve_init, 1073 .ndo_uninit = geneve_uninit, 1074 .ndo_open = geneve_open, 1075 .ndo_stop = geneve_stop, 1076 .ndo_start_xmit = geneve_xmit, 1077 .ndo_get_stats64 = ip_tunnel_get_stats64, 1078 .ndo_change_mtu = geneve_change_mtu, 1079 .ndo_validate_addr = eth_validate_addr, 1080 .ndo_set_mac_address = eth_mac_addr, 1081 .ndo_fill_metadata_dst = geneve_fill_metadata_dst, 1082 }; 1083 1084 static void geneve_get_drvinfo(struct net_device *dev, 1085 struct ethtool_drvinfo *drvinfo) 1086 { 1087 strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version)); 1088 strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver)); 1089 } 1090 1091 static const struct ethtool_ops geneve_ethtool_ops = { 1092 .get_drvinfo = geneve_get_drvinfo, 1093 .get_link = ethtool_op_get_link, 1094 }; 1095 1096 /* Info for udev, that this is a virtual tunnel endpoint */ 1097 static struct device_type geneve_type = { 1098 .name = "geneve", 1099 }; 1100 1101 /* Calls the ndo_udp_tunnel_add of the caller in order to 1102 * supply the listening GENEVE udp ports. Callers are expected 1103 * to implement the ndo_udp_tunnel_add. 1104 */ 1105 static void geneve_offload_rx_ports(struct net_device *dev, bool push) 1106 { 1107 struct net *net = dev_net(dev); 1108 struct geneve_net *gn = net_generic(net, geneve_net_id); 1109 struct geneve_sock *gs; 1110 1111 rcu_read_lock(); 1112 list_for_each_entry_rcu(gs, &gn->sock_list, list) { 1113 if (push) { 1114 udp_tunnel_push_rx_port(dev, gs->sock, 1115 UDP_TUNNEL_TYPE_GENEVE); 1116 } else { 1117 udp_tunnel_drop_rx_port(dev, gs->sock, 1118 UDP_TUNNEL_TYPE_GENEVE); 1119 } 1120 } 1121 rcu_read_unlock(); 1122 } 1123 1124 /* Initialize the device structure. */ 1125 static void geneve_setup(struct net_device *dev) 1126 { 1127 ether_setup(dev); 1128 1129 dev->netdev_ops = &geneve_netdev_ops; 1130 dev->ethtool_ops = &geneve_ethtool_ops; 1131 dev->needs_free_netdev = true; 1132 1133 SET_NETDEV_DEVTYPE(dev, &geneve_type); 1134 1135 dev->features |= NETIF_F_LLTX; 1136 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM; 1137 dev->features |= NETIF_F_RXCSUM; 1138 dev->features |= NETIF_F_GSO_SOFTWARE; 1139 1140 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM; 1141 dev->hw_features |= NETIF_F_GSO_SOFTWARE; 1142 1143 /* MTU range: 68 - (something less than 65535) */ 1144 dev->min_mtu = ETH_MIN_MTU; 1145 /* The max_mtu calculation does not take account of GENEVE 1146 * options, to avoid excluding potentially valid 1147 * configurations. This will be further reduced by IPvX hdr size. 1148 */ 1149 dev->max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len; 1150 1151 netif_keep_dst(dev); 1152 dev->priv_flags &= ~IFF_TX_SKB_SHARING; 1153 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE; 1154 eth_hw_addr_random(dev); 1155 } 1156 1157 static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = { 1158 [IFLA_GENEVE_ID] = { .type = NLA_U32 }, 1159 [IFLA_GENEVE_REMOTE] = { .len = sizeof_field(struct iphdr, daddr) }, 1160 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) }, 1161 [IFLA_GENEVE_TTL] = { .type = NLA_U8 }, 1162 [IFLA_GENEVE_TOS] = { .type = NLA_U8 }, 1163 [IFLA_GENEVE_LABEL] = { .type = NLA_U32 }, 1164 [IFLA_GENEVE_PORT] = { .type = NLA_U16 }, 1165 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG }, 1166 [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 }, 1167 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 }, 1168 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 }, 1169 [IFLA_GENEVE_TTL_INHERIT] = { .type = NLA_U8 }, 1170 [IFLA_GENEVE_DF] = { .type = NLA_U8 }, 1171 }; 1172 1173 static int geneve_validate(struct nlattr *tb[], struct nlattr *data[], 1174 struct netlink_ext_ack *extack) 1175 { 1176 if (tb[IFLA_ADDRESS]) { 1177 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) { 1178 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS], 1179 "Provided link layer address is not Ethernet"); 1180 return -EINVAL; 1181 } 1182 1183 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) { 1184 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS], 1185 "Provided Ethernet address is not unicast"); 1186 return -EADDRNOTAVAIL; 1187 } 1188 } 1189 1190 if (!data) { 1191 NL_SET_ERR_MSG(extack, 1192 "Not enough attributes provided to perform the operation"); 1193 return -EINVAL; 1194 } 1195 1196 if (data[IFLA_GENEVE_ID]) { 1197 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]); 1198 1199 if (vni >= GENEVE_N_VID) { 1200 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_ID], 1201 "Geneve ID must be lower than 16777216"); 1202 return -ERANGE; 1203 } 1204 } 1205 1206 if (data[IFLA_GENEVE_DF]) { 1207 enum ifla_geneve_df df = nla_get_u8(data[IFLA_GENEVE_DF]); 1208 1209 if (df < 0 || df > GENEVE_DF_MAX) { 1210 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_GENEVE_DF], 1211 "Invalid DF attribute"); 1212 return -EINVAL; 1213 } 1214 } 1215 1216 return 0; 1217 } 1218 1219 static struct geneve_dev *geneve_find_dev(struct geneve_net *gn, 1220 const struct ip_tunnel_info *info, 1221 bool *tun_on_same_port, 1222 bool *tun_collect_md) 1223 { 1224 struct geneve_dev *geneve, *t = NULL; 1225 1226 *tun_on_same_port = false; 1227 *tun_collect_md = false; 1228 list_for_each_entry(geneve, &gn->geneve_list, next) { 1229 if (info->key.tp_dst == geneve->info.key.tp_dst) { 1230 *tun_collect_md = geneve->collect_md; 1231 *tun_on_same_port = true; 1232 } 1233 if (info->key.tun_id == geneve->info.key.tun_id && 1234 info->key.tp_dst == geneve->info.key.tp_dst && 1235 !memcmp(&info->key.u, &geneve->info.key.u, sizeof(info->key.u))) 1236 t = geneve; 1237 } 1238 return t; 1239 } 1240 1241 static bool is_tnl_info_zero(const struct ip_tunnel_info *info) 1242 { 1243 return !(info->key.tun_id || info->key.tun_flags || info->key.tos || 1244 info->key.ttl || info->key.label || info->key.tp_src || 1245 memchr_inv(&info->key.u, 0, sizeof(info->key.u))); 1246 } 1247 1248 static bool geneve_dst_addr_equal(struct ip_tunnel_info *a, 1249 struct ip_tunnel_info *b) 1250 { 1251 if (ip_tunnel_info_af(a) == AF_INET) 1252 return a->key.u.ipv4.dst == b->key.u.ipv4.dst; 1253 else 1254 return ipv6_addr_equal(&a->key.u.ipv6.dst, &b->key.u.ipv6.dst); 1255 } 1256 1257 static int geneve_configure(struct net *net, struct net_device *dev, 1258 struct netlink_ext_ack *extack, 1259 const struct ip_tunnel_info *info, 1260 bool metadata, bool ipv6_rx_csum, 1261 bool ttl_inherit, enum ifla_geneve_df df) 1262 { 1263 struct geneve_net *gn = net_generic(net, geneve_net_id); 1264 struct geneve_dev *t, *geneve = netdev_priv(dev); 1265 bool tun_collect_md, tun_on_same_port; 1266 int err, encap_len; 1267 1268 if (metadata && !is_tnl_info_zero(info)) { 1269 NL_SET_ERR_MSG(extack, 1270 "Device is externally controlled, so attributes (VNI, Port, and so on) must not be specified"); 1271 return -EINVAL; 1272 } 1273 1274 geneve->net = net; 1275 geneve->dev = dev; 1276 1277 t = geneve_find_dev(gn, info, &tun_on_same_port, &tun_collect_md); 1278 if (t) 1279 return -EBUSY; 1280 1281 /* make enough headroom for basic scenario */ 1282 encap_len = GENEVE_BASE_HLEN + ETH_HLEN; 1283 if (!metadata && ip_tunnel_info_af(info) == AF_INET) { 1284 encap_len += sizeof(struct iphdr); 1285 dev->max_mtu -= sizeof(struct iphdr); 1286 } else { 1287 encap_len += sizeof(struct ipv6hdr); 1288 dev->max_mtu -= sizeof(struct ipv6hdr); 1289 } 1290 dev->needed_headroom = encap_len + ETH_HLEN; 1291 1292 if (metadata) { 1293 if (tun_on_same_port) { 1294 NL_SET_ERR_MSG(extack, 1295 "There can be only one externally controlled device on a destination port"); 1296 return -EPERM; 1297 } 1298 } else { 1299 if (tun_collect_md) { 1300 NL_SET_ERR_MSG(extack, 1301 "There already exists an externally controlled device on this destination port"); 1302 return -EPERM; 1303 } 1304 } 1305 1306 dst_cache_reset(&geneve->info.dst_cache); 1307 geneve->info = *info; 1308 geneve->collect_md = metadata; 1309 geneve->use_udp6_rx_checksums = ipv6_rx_csum; 1310 geneve->ttl_inherit = ttl_inherit; 1311 geneve->df = df; 1312 1313 err = register_netdevice(dev); 1314 if (err) 1315 return err; 1316 1317 list_add(&geneve->next, &gn->geneve_list); 1318 return 0; 1319 } 1320 1321 static void init_tnl_info(struct ip_tunnel_info *info, __u16 dst_port) 1322 { 1323 memset(info, 0, sizeof(*info)); 1324 info->key.tp_dst = htons(dst_port); 1325 } 1326 1327 static int geneve_nl2info(struct nlattr *tb[], struct nlattr *data[], 1328 struct netlink_ext_ack *extack, 1329 struct ip_tunnel_info *info, bool *metadata, 1330 bool *use_udp6_rx_checksums, bool *ttl_inherit, 1331 enum ifla_geneve_df *df, bool changelink) 1332 { 1333 int attrtype; 1334 1335 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6]) { 1336 NL_SET_ERR_MSG(extack, 1337 "Cannot specify both IPv4 and IPv6 Remote addresses"); 1338 return -EINVAL; 1339 } 1340 1341 if (data[IFLA_GENEVE_REMOTE]) { 1342 if (changelink && (ip_tunnel_info_af(info) == AF_INET6)) { 1343 attrtype = IFLA_GENEVE_REMOTE; 1344 goto change_notsup; 1345 } 1346 1347 info->key.u.ipv4.dst = 1348 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]); 1349 1350 if (ipv4_is_multicast(info->key.u.ipv4.dst)) { 1351 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE], 1352 "Remote IPv4 address cannot be Multicast"); 1353 return -EINVAL; 1354 } 1355 } 1356 1357 if (data[IFLA_GENEVE_REMOTE6]) { 1358 #if IS_ENABLED(CONFIG_IPV6) 1359 if (changelink && (ip_tunnel_info_af(info) == AF_INET)) { 1360 attrtype = IFLA_GENEVE_REMOTE6; 1361 goto change_notsup; 1362 } 1363 1364 info->mode = IP_TUNNEL_INFO_IPV6; 1365 info->key.u.ipv6.dst = 1366 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]); 1367 1368 if (ipv6_addr_type(&info->key.u.ipv6.dst) & 1369 IPV6_ADDR_LINKLOCAL) { 1370 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6], 1371 "Remote IPv6 address cannot be link-local"); 1372 return -EINVAL; 1373 } 1374 if (ipv6_addr_is_multicast(&info->key.u.ipv6.dst)) { 1375 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6], 1376 "Remote IPv6 address cannot be Multicast"); 1377 return -EINVAL; 1378 } 1379 info->key.tun_flags |= TUNNEL_CSUM; 1380 *use_udp6_rx_checksums = true; 1381 #else 1382 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6], 1383 "IPv6 support not enabled in the kernel"); 1384 return -EPFNOSUPPORT; 1385 #endif 1386 } 1387 1388 if (data[IFLA_GENEVE_ID]) { 1389 __u32 vni; 1390 __u8 tvni[3]; 1391 __be64 tunid; 1392 1393 vni = nla_get_u32(data[IFLA_GENEVE_ID]); 1394 tvni[0] = (vni & 0x00ff0000) >> 16; 1395 tvni[1] = (vni & 0x0000ff00) >> 8; 1396 tvni[2] = vni & 0x000000ff; 1397 1398 tunid = vni_to_tunnel_id(tvni); 1399 if (changelink && (tunid != info->key.tun_id)) { 1400 attrtype = IFLA_GENEVE_ID; 1401 goto change_notsup; 1402 } 1403 info->key.tun_id = tunid; 1404 } 1405 1406 if (data[IFLA_GENEVE_TTL_INHERIT]) { 1407 if (nla_get_u8(data[IFLA_GENEVE_TTL_INHERIT])) 1408 *ttl_inherit = true; 1409 else 1410 *ttl_inherit = false; 1411 } else if (data[IFLA_GENEVE_TTL]) { 1412 info->key.ttl = nla_get_u8(data[IFLA_GENEVE_TTL]); 1413 *ttl_inherit = false; 1414 } 1415 1416 if (data[IFLA_GENEVE_TOS]) 1417 info->key.tos = nla_get_u8(data[IFLA_GENEVE_TOS]); 1418 1419 if (data[IFLA_GENEVE_DF]) 1420 *df = nla_get_u8(data[IFLA_GENEVE_DF]); 1421 1422 if (data[IFLA_GENEVE_LABEL]) { 1423 info->key.label = nla_get_be32(data[IFLA_GENEVE_LABEL]) & 1424 IPV6_FLOWLABEL_MASK; 1425 if (info->key.label && (!(info->mode & IP_TUNNEL_INFO_IPV6))) { 1426 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_LABEL], 1427 "Label attribute only applies for IPv6 Geneve devices"); 1428 return -EINVAL; 1429 } 1430 } 1431 1432 if (data[IFLA_GENEVE_PORT]) { 1433 if (changelink) { 1434 attrtype = IFLA_GENEVE_PORT; 1435 goto change_notsup; 1436 } 1437 info->key.tp_dst = nla_get_be16(data[IFLA_GENEVE_PORT]); 1438 } 1439 1440 if (data[IFLA_GENEVE_COLLECT_METADATA]) { 1441 if (changelink) { 1442 attrtype = IFLA_GENEVE_COLLECT_METADATA; 1443 goto change_notsup; 1444 } 1445 *metadata = true; 1446 } 1447 1448 if (data[IFLA_GENEVE_UDP_CSUM]) { 1449 if (changelink) { 1450 attrtype = IFLA_GENEVE_UDP_CSUM; 1451 goto change_notsup; 1452 } 1453 if (nla_get_u8(data[IFLA_GENEVE_UDP_CSUM])) 1454 info->key.tun_flags |= TUNNEL_CSUM; 1455 } 1456 1457 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]) { 1458 #if IS_ENABLED(CONFIG_IPV6) 1459 if (changelink) { 1460 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_TX; 1461 goto change_notsup; 1462 } 1463 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX])) 1464 info->key.tun_flags &= ~TUNNEL_CSUM; 1465 #else 1466 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX], 1467 "IPv6 support not enabled in the kernel"); 1468 return -EPFNOSUPPORT; 1469 #endif 1470 } 1471 1472 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]) { 1473 #if IS_ENABLED(CONFIG_IPV6) 1474 if (changelink) { 1475 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_RX; 1476 goto change_notsup; 1477 } 1478 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX])) 1479 *use_udp6_rx_checksums = false; 1480 #else 1481 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX], 1482 "IPv6 support not enabled in the kernel"); 1483 return -EPFNOSUPPORT; 1484 #endif 1485 } 1486 1487 return 0; 1488 change_notsup: 1489 NL_SET_ERR_MSG_ATTR(extack, data[attrtype], 1490 "Changing VNI, Port, endpoint IP address family, external, and UDP checksum attributes are not supported"); 1491 return -EOPNOTSUPP; 1492 } 1493 1494 static void geneve_link_config(struct net_device *dev, 1495 struct ip_tunnel_info *info, struct nlattr *tb[]) 1496 { 1497 struct geneve_dev *geneve = netdev_priv(dev); 1498 int ldev_mtu = 0; 1499 1500 if (tb[IFLA_MTU]) { 1501 geneve_change_mtu(dev, nla_get_u32(tb[IFLA_MTU])); 1502 return; 1503 } 1504 1505 switch (ip_tunnel_info_af(info)) { 1506 case AF_INET: { 1507 struct flowi4 fl4 = { .daddr = info->key.u.ipv4.dst }; 1508 struct rtable *rt = ip_route_output_key(geneve->net, &fl4); 1509 1510 if (!IS_ERR(rt) && rt->dst.dev) { 1511 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV4_HLEN; 1512 ip_rt_put(rt); 1513 } 1514 break; 1515 } 1516 #if IS_ENABLED(CONFIG_IPV6) 1517 case AF_INET6: { 1518 struct rt6_info *rt; 1519 1520 if (!__in6_dev_get(dev)) 1521 break; 1522 1523 rt = rt6_lookup(geneve->net, &info->key.u.ipv6.dst, NULL, 0, 1524 NULL, 0); 1525 1526 if (rt && rt->dst.dev) 1527 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV6_HLEN; 1528 ip6_rt_put(rt); 1529 break; 1530 } 1531 #endif 1532 } 1533 1534 if (ldev_mtu <= 0) 1535 return; 1536 1537 geneve_change_mtu(dev, ldev_mtu - info->options_len); 1538 } 1539 1540 static int geneve_newlink(struct net *net, struct net_device *dev, 1541 struct nlattr *tb[], struct nlattr *data[], 1542 struct netlink_ext_ack *extack) 1543 { 1544 enum ifla_geneve_df df = GENEVE_DF_UNSET; 1545 bool use_udp6_rx_checksums = false; 1546 struct ip_tunnel_info info; 1547 bool ttl_inherit = false; 1548 bool metadata = false; 1549 int err; 1550 1551 init_tnl_info(&info, GENEVE_UDP_PORT); 1552 err = geneve_nl2info(tb, data, extack, &info, &metadata, 1553 &use_udp6_rx_checksums, &ttl_inherit, &df, false); 1554 if (err) 1555 return err; 1556 1557 err = geneve_configure(net, dev, extack, &info, metadata, 1558 use_udp6_rx_checksums, ttl_inherit, df); 1559 if (err) 1560 return err; 1561 1562 geneve_link_config(dev, &info, tb); 1563 1564 return 0; 1565 } 1566 1567 /* Quiesces the geneve device data path for both TX and RX. 1568 * 1569 * On transmit geneve checks for non-NULL geneve_sock before it proceeds. 1570 * So, if we set that socket to NULL under RCU and wait for synchronize_net() 1571 * to complete for the existing set of in-flight packets to be transmitted, 1572 * then we would have quiesced the transmit data path. All the future packets 1573 * will get dropped until we unquiesce the data path. 1574 * 1575 * On receive geneve dereference the geneve_sock stashed in the socket. So, 1576 * if we set that to NULL under RCU and wait for synchronize_net() to 1577 * complete, then we would have quiesced the receive data path. 1578 */ 1579 static void geneve_quiesce(struct geneve_dev *geneve, struct geneve_sock **gs4, 1580 struct geneve_sock **gs6) 1581 { 1582 *gs4 = rtnl_dereference(geneve->sock4); 1583 rcu_assign_pointer(geneve->sock4, NULL); 1584 if (*gs4) 1585 rcu_assign_sk_user_data((*gs4)->sock->sk, NULL); 1586 #if IS_ENABLED(CONFIG_IPV6) 1587 *gs6 = rtnl_dereference(geneve->sock6); 1588 rcu_assign_pointer(geneve->sock6, NULL); 1589 if (*gs6) 1590 rcu_assign_sk_user_data((*gs6)->sock->sk, NULL); 1591 #else 1592 *gs6 = NULL; 1593 #endif 1594 synchronize_net(); 1595 } 1596 1597 /* Resumes the geneve device data path for both TX and RX. */ 1598 static void geneve_unquiesce(struct geneve_dev *geneve, struct geneve_sock *gs4, 1599 struct geneve_sock __maybe_unused *gs6) 1600 { 1601 rcu_assign_pointer(geneve->sock4, gs4); 1602 if (gs4) 1603 rcu_assign_sk_user_data(gs4->sock->sk, gs4); 1604 #if IS_ENABLED(CONFIG_IPV6) 1605 rcu_assign_pointer(geneve->sock6, gs6); 1606 if (gs6) 1607 rcu_assign_sk_user_data(gs6->sock->sk, gs6); 1608 #endif 1609 synchronize_net(); 1610 } 1611 1612 static int geneve_changelink(struct net_device *dev, struct nlattr *tb[], 1613 struct nlattr *data[], 1614 struct netlink_ext_ack *extack) 1615 { 1616 struct geneve_dev *geneve = netdev_priv(dev); 1617 struct geneve_sock *gs4, *gs6; 1618 struct ip_tunnel_info info; 1619 bool metadata; 1620 bool use_udp6_rx_checksums; 1621 enum ifla_geneve_df df; 1622 bool ttl_inherit; 1623 int err; 1624 1625 /* If the geneve device is configured for metadata (or externally 1626 * controlled, for example, OVS), then nothing can be changed. 1627 */ 1628 if (geneve->collect_md) 1629 return -EOPNOTSUPP; 1630 1631 /* Start with the existing info. */ 1632 memcpy(&info, &geneve->info, sizeof(info)); 1633 metadata = geneve->collect_md; 1634 use_udp6_rx_checksums = geneve->use_udp6_rx_checksums; 1635 ttl_inherit = geneve->ttl_inherit; 1636 err = geneve_nl2info(tb, data, extack, &info, &metadata, 1637 &use_udp6_rx_checksums, &ttl_inherit, &df, true); 1638 if (err) 1639 return err; 1640 1641 if (!geneve_dst_addr_equal(&geneve->info, &info)) { 1642 dst_cache_reset(&info.dst_cache); 1643 geneve_link_config(dev, &info, tb); 1644 } 1645 1646 geneve_quiesce(geneve, &gs4, &gs6); 1647 geneve->info = info; 1648 geneve->collect_md = metadata; 1649 geneve->use_udp6_rx_checksums = use_udp6_rx_checksums; 1650 geneve->ttl_inherit = ttl_inherit; 1651 geneve_unquiesce(geneve, gs4, gs6); 1652 1653 return 0; 1654 } 1655 1656 static void geneve_dellink(struct net_device *dev, struct list_head *head) 1657 { 1658 struct geneve_dev *geneve = netdev_priv(dev); 1659 1660 list_del(&geneve->next); 1661 unregister_netdevice_queue(dev, head); 1662 } 1663 1664 static size_t geneve_get_size(const struct net_device *dev) 1665 { 1666 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */ 1667 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */ 1668 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */ 1669 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */ 1670 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_DF */ 1671 nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */ 1672 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */ 1673 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */ 1674 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */ 1675 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */ 1676 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */ 1677 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL_INHERIT */ 1678 0; 1679 } 1680 1681 static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev) 1682 { 1683 struct geneve_dev *geneve = netdev_priv(dev); 1684 struct ip_tunnel_info *info = &geneve->info; 1685 bool ttl_inherit = geneve->ttl_inherit; 1686 bool metadata = geneve->collect_md; 1687 __u8 tmp_vni[3]; 1688 __u32 vni; 1689 1690 tunnel_id_to_vni(info->key.tun_id, tmp_vni); 1691 vni = (tmp_vni[0] << 16) | (tmp_vni[1] << 8) | tmp_vni[2]; 1692 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni)) 1693 goto nla_put_failure; 1694 1695 if (!metadata && ip_tunnel_info_af(info) == AF_INET) { 1696 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE, 1697 info->key.u.ipv4.dst)) 1698 goto nla_put_failure; 1699 if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM, 1700 !!(info->key.tun_flags & TUNNEL_CSUM))) 1701 goto nla_put_failure; 1702 1703 #if IS_ENABLED(CONFIG_IPV6) 1704 } else if (!metadata) { 1705 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6, 1706 &info->key.u.ipv6.dst)) 1707 goto nla_put_failure; 1708 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX, 1709 !(info->key.tun_flags & TUNNEL_CSUM))) 1710 goto nla_put_failure; 1711 #endif 1712 } 1713 1714 if (nla_put_u8(skb, IFLA_GENEVE_TTL, info->key.ttl) || 1715 nla_put_u8(skb, IFLA_GENEVE_TOS, info->key.tos) || 1716 nla_put_be32(skb, IFLA_GENEVE_LABEL, info->key.label)) 1717 goto nla_put_failure; 1718 1719 if (nla_put_u8(skb, IFLA_GENEVE_DF, geneve->df)) 1720 goto nla_put_failure; 1721 1722 if (nla_put_be16(skb, IFLA_GENEVE_PORT, info->key.tp_dst)) 1723 goto nla_put_failure; 1724 1725 if (metadata && nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA)) 1726 goto nla_put_failure; 1727 1728 #if IS_ENABLED(CONFIG_IPV6) 1729 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX, 1730 !geneve->use_udp6_rx_checksums)) 1731 goto nla_put_failure; 1732 #endif 1733 1734 if (nla_put_u8(skb, IFLA_GENEVE_TTL_INHERIT, ttl_inherit)) 1735 goto nla_put_failure; 1736 1737 return 0; 1738 1739 nla_put_failure: 1740 return -EMSGSIZE; 1741 } 1742 1743 static struct rtnl_link_ops geneve_link_ops __read_mostly = { 1744 .kind = "geneve", 1745 .maxtype = IFLA_GENEVE_MAX, 1746 .policy = geneve_policy, 1747 .priv_size = sizeof(struct geneve_dev), 1748 .setup = geneve_setup, 1749 .validate = geneve_validate, 1750 .newlink = geneve_newlink, 1751 .changelink = geneve_changelink, 1752 .dellink = geneve_dellink, 1753 .get_size = geneve_get_size, 1754 .fill_info = geneve_fill_info, 1755 }; 1756 1757 struct net_device *geneve_dev_create_fb(struct net *net, const char *name, 1758 u8 name_assign_type, u16 dst_port) 1759 { 1760 struct nlattr *tb[IFLA_MAX + 1]; 1761 struct ip_tunnel_info info; 1762 struct net_device *dev; 1763 LIST_HEAD(list_kill); 1764 int err; 1765 1766 memset(tb, 0, sizeof(tb)); 1767 dev = rtnl_create_link(net, name, name_assign_type, 1768 &geneve_link_ops, tb, NULL); 1769 if (IS_ERR(dev)) 1770 return dev; 1771 1772 init_tnl_info(&info, dst_port); 1773 err = geneve_configure(net, dev, NULL, &info, 1774 true, true, false, GENEVE_DF_UNSET); 1775 if (err) { 1776 free_netdev(dev); 1777 return ERR_PTR(err); 1778 } 1779 1780 /* openvswitch users expect packet sizes to be unrestricted, 1781 * so set the largest MTU we can. 1782 */ 1783 err = geneve_change_mtu(dev, IP_MAX_MTU); 1784 if (err) 1785 goto err; 1786 1787 err = rtnl_configure_link(dev, NULL); 1788 if (err < 0) 1789 goto err; 1790 1791 return dev; 1792 err: 1793 geneve_dellink(dev, &list_kill); 1794 unregister_netdevice_many(&list_kill); 1795 return ERR_PTR(err); 1796 } 1797 EXPORT_SYMBOL_GPL(geneve_dev_create_fb); 1798 1799 static int geneve_netdevice_event(struct notifier_block *unused, 1800 unsigned long event, void *ptr) 1801 { 1802 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1803 1804 if (event == NETDEV_UDP_TUNNEL_PUSH_INFO || 1805 event == NETDEV_UDP_TUNNEL_DROP_INFO) { 1806 geneve_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO); 1807 } else if (event == NETDEV_UNREGISTER) { 1808 geneve_offload_rx_ports(dev, false); 1809 } else if (event == NETDEV_REGISTER) { 1810 geneve_offload_rx_ports(dev, true); 1811 } 1812 1813 return NOTIFY_DONE; 1814 } 1815 1816 static struct notifier_block geneve_notifier_block __read_mostly = { 1817 .notifier_call = geneve_netdevice_event, 1818 }; 1819 1820 static __net_init int geneve_init_net(struct net *net) 1821 { 1822 struct geneve_net *gn = net_generic(net, geneve_net_id); 1823 1824 INIT_LIST_HEAD(&gn->geneve_list); 1825 INIT_LIST_HEAD(&gn->sock_list); 1826 return 0; 1827 } 1828 1829 static void geneve_destroy_tunnels(struct net *net, struct list_head *head) 1830 { 1831 struct geneve_net *gn = net_generic(net, geneve_net_id); 1832 struct geneve_dev *geneve, *next; 1833 struct net_device *dev, *aux; 1834 1835 /* gather any geneve devices that were moved into this ns */ 1836 for_each_netdev_safe(net, dev, aux) 1837 if (dev->rtnl_link_ops == &geneve_link_ops) 1838 unregister_netdevice_queue(dev, head); 1839 1840 /* now gather any other geneve devices that were created in this ns */ 1841 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) { 1842 /* If geneve->dev is in the same netns, it was already added 1843 * to the list by the previous loop. 1844 */ 1845 if (!net_eq(dev_net(geneve->dev), net)) 1846 unregister_netdevice_queue(geneve->dev, head); 1847 } 1848 } 1849 1850 static void __net_exit geneve_exit_batch_net(struct list_head *net_list) 1851 { 1852 struct net *net; 1853 LIST_HEAD(list); 1854 1855 rtnl_lock(); 1856 list_for_each_entry(net, net_list, exit_list) 1857 geneve_destroy_tunnels(net, &list); 1858 1859 /* unregister the devices gathered above */ 1860 unregister_netdevice_many(&list); 1861 rtnl_unlock(); 1862 1863 list_for_each_entry(net, net_list, exit_list) { 1864 const struct geneve_net *gn = net_generic(net, geneve_net_id); 1865 1866 WARN_ON_ONCE(!list_empty(&gn->sock_list)); 1867 } 1868 } 1869 1870 static struct pernet_operations geneve_net_ops = { 1871 .init = geneve_init_net, 1872 .exit_batch = geneve_exit_batch_net, 1873 .id = &geneve_net_id, 1874 .size = sizeof(struct geneve_net), 1875 }; 1876 1877 static int __init geneve_init_module(void) 1878 { 1879 int rc; 1880 1881 rc = register_pernet_subsys(&geneve_net_ops); 1882 if (rc) 1883 goto out1; 1884 1885 rc = register_netdevice_notifier(&geneve_notifier_block); 1886 if (rc) 1887 goto out2; 1888 1889 rc = rtnl_link_register(&geneve_link_ops); 1890 if (rc) 1891 goto out3; 1892 1893 return 0; 1894 out3: 1895 unregister_netdevice_notifier(&geneve_notifier_block); 1896 out2: 1897 unregister_pernet_subsys(&geneve_net_ops); 1898 out1: 1899 return rc; 1900 } 1901 late_initcall(geneve_init_module); 1902 1903 static void __exit geneve_cleanup_module(void) 1904 { 1905 rtnl_link_unregister(&geneve_link_ops); 1906 unregister_netdevice_notifier(&geneve_notifier_block); 1907 unregister_pernet_subsys(&geneve_net_ops); 1908 } 1909 module_exit(geneve_cleanup_module); 1910 1911 MODULE_LICENSE("GPL"); 1912 MODULE_VERSION(GENEVE_NETDEV_VER); 1913 MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>"); 1914 MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic"); 1915 MODULE_ALIAS_RTNL_LINK("geneve"); 1916