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