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