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