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