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