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/dst_metadata.h> 16 #include <net/gro_cells.h> 17 #include <net/rtnetlink.h> 18 #include <net/geneve.h> 19 #include <net/gro.h> 20 #include <net/netdev_lock.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 #define GENEVE_OPT_NETDEV_CLASS 0x100 41 #define GENEVE_OPT_GRO_HINT_SIZE 8 42 #define GENEVE_OPT_GRO_HINT_TYPE 1 43 #define GENEVE_OPT_GRO_HINT_LEN 1 44 45 struct geneve_opt_gro_hint { 46 #if defined(__LITTLE_ENDIAN_BITFIELD) 47 u8 inner_proto_id:2, 48 nested_is_v6:1, 49 rsvd:5; 50 #elif defined(__BIG_ENDIAN_BITFIELD) 51 u8 rsvd:5, 52 nested_is_v6:1, 53 inner_proto_id:2; 54 #else 55 #error "Please fix <asm/byteorder.h>" 56 #endif 57 u8 nested_nh_offset; 58 u8 nested_tp_offset; 59 u8 nested_hdr_len; 60 }; 61 62 struct geneve_skb_cb { 63 unsigned int gro_hint_len; 64 struct geneve_opt_gro_hint gro_hint; 65 }; 66 67 #define GENEVE_SKB_CB(__skb) ((struct geneve_skb_cb *)&((__skb)->cb[0])) 68 69 /* per-network namespace private data for this module */ 70 struct geneve_net { 71 struct list_head geneve_list; 72 struct list_head sock_list; 73 struct mutex lock; 74 }; 75 76 static unsigned int geneve_net_id; 77 78 struct geneve_dev_node { 79 struct hlist_node hlist; 80 struct geneve_dev *geneve; 81 }; 82 83 struct geneve_config { 84 bool collect_md; 85 bool dualstack; 86 bool use_udp6_rx_checksums; 87 bool ttl_inherit; 88 bool gro_hint; 89 enum ifla_geneve_df df; 90 bool inner_proto_inherit; 91 u16 port_min; 92 u16 port_max; 93 94 struct rcu_head rcu; 95 /* Must be last --ends in a flexible-array member. */ 96 struct ip_tunnel_info info; 97 }; 98 99 /* Pseudo network device */ 100 struct geneve_dev { 101 struct geneve_dev_node hlist4; /* vni hash table for IPv4 socket */ 102 #if IS_ENABLED(CONFIG_IPV6) 103 struct geneve_dev_node hlist6; /* vni hash table for IPv6 socket */ 104 #endif 105 struct net *net; /* netns for packet i/o */ 106 struct net_device *dev; /* netdev for geneve tunnel */ 107 struct geneve_sock __rcu *sock4; /* IPv4 socket used for geneve tunnel */ 108 #if IS_ENABLED(CONFIG_IPV6) 109 struct geneve_sock __rcu *sock6; /* IPv6 socket used for geneve tunnel */ 110 #endif 111 struct list_head next; /* geneve's per namespace list */ 112 struct gro_cells gro_cells; 113 struct geneve_config __rcu *cfg; 114 }; 115 116 struct geneve_sock { 117 bool collect_md; 118 bool gro_hint; 119 struct list_head list; 120 struct sock *sk; 121 struct rcu_head rcu; 122 int refcnt; 123 struct hlist_head vni_list[VNI_HASH_SIZE]; 124 }; 125 126 static const __be16 proto_id_map[] = { htons(ETH_P_TEB), 127 htons(ETH_P_IPV6), 128 htons(ETH_P_IP) }; 129 130 static int proto_to_id(__be16 proto) 131 { 132 int i; 133 134 for (i = 0; i < ARRAY_SIZE(proto_id_map); i++) 135 if (proto_id_map[i] == proto) 136 return i; 137 138 return -1; 139 } 140 141 static inline __u32 geneve_net_vni_hash(u8 vni[3]) 142 { 143 __u32 vnid; 144 145 vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2]; 146 return hash_32(vnid, VNI_HASH_BITS); 147 } 148 149 static __be64 vni_to_tunnel_id(const __u8 *vni) 150 { 151 #ifdef __BIG_ENDIAN 152 return (vni[0] << 16) | (vni[1] << 8) | vni[2]; 153 #else 154 return (__force __be64)(((__force u64)vni[0] << 40) | 155 ((__force u64)vni[1] << 48) | 156 ((__force u64)vni[2] << 56)); 157 #endif 158 } 159 160 /* Convert 64 bit tunnel ID to 24 bit VNI. */ 161 static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni) 162 { 163 #ifdef __BIG_ENDIAN 164 vni[0] = (__force __u8)(tun_id >> 16); 165 vni[1] = (__force __u8)(tun_id >> 8); 166 vni[2] = (__force __u8)tun_id; 167 #else 168 vni[0] = (__force __u8)((__force u64)tun_id >> 40); 169 vni[1] = (__force __u8)((__force u64)tun_id >> 48); 170 vni[2] = (__force __u8)((__force u64)tun_id >> 56); 171 #endif 172 } 173 174 static bool eq_tun_id_and_vni(u8 *tun_id, u8 *vni) 175 { 176 return !memcmp(vni, &tun_id[5], 3); 177 } 178 179 static sa_family_t geneve_get_sk_family(struct geneve_sock *gs) 180 { 181 return gs->sk->sk_family; 182 } 183 184 static struct geneve_dev *geneve_lookup(struct geneve_sock *gs, 185 __be32 addr, u8 vni[]) 186 { 187 struct hlist_head *vni_list_head; 188 struct geneve_dev_node *node; 189 __u32 hash; 190 191 /* Find the device for this VNI */ 192 hash = geneve_net_vni_hash(vni); 193 vni_list_head = &gs->vni_list[hash]; 194 hlist_for_each_entry_rcu(node, vni_list_head, hlist) { 195 const struct geneve_config *cfg = rcu_dereference(node->geneve->cfg); 196 197 if (eq_tun_id_and_vni((u8 *)&cfg->info.key.tun_id, vni) && 198 addr == cfg->info.key.u.ipv4.dst) 199 return node->geneve; 200 } 201 return NULL; 202 } 203 204 #if IS_ENABLED(CONFIG_IPV6) 205 static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs, 206 struct in6_addr addr6, u8 vni[]) 207 { 208 struct hlist_head *vni_list_head; 209 struct geneve_dev_node *node; 210 __u32 hash; 211 212 /* Find the device for this VNI */ 213 hash = geneve_net_vni_hash(vni); 214 vni_list_head = &gs->vni_list[hash]; 215 hlist_for_each_entry_rcu(node, vni_list_head, hlist) { 216 const struct geneve_config *cfg = rcu_dereference(node->geneve->cfg); 217 218 if (eq_tun_id_and_vni((u8 *)&cfg->info.key.tun_id, vni) && 219 ipv6_addr_equal(&addr6, &cfg->info.key.u.ipv6.dst)) 220 return node->geneve; 221 } 222 return NULL; 223 } 224 #endif 225 226 static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb) 227 { 228 return (struct genevehdr *)(udp_hdr(skb) + 1); 229 } 230 231 static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs, 232 struct sk_buff *skb) 233 { 234 static u8 zero_vni[3]; 235 u8 *vni; 236 237 if (geneve_get_sk_family(gs) == AF_INET) { 238 struct iphdr *iph; 239 __be32 addr; 240 241 iph = ip_hdr(skb); /* outer IP header... */ 242 243 if (gs->collect_md) { 244 vni = zero_vni; 245 addr = 0; 246 } else { 247 vni = geneve_hdr(skb)->vni; 248 addr = iph->saddr; 249 } 250 251 return geneve_lookup(gs, addr, vni); 252 #if IS_ENABLED(CONFIG_IPV6) 253 } else if (geneve_get_sk_family(gs) == AF_INET6) { 254 static struct in6_addr zero_addr6; 255 struct ipv6hdr *ip6h; 256 struct in6_addr addr6; 257 258 ip6h = ipv6_hdr(skb); /* outer IPv6 header... */ 259 260 if (gs->collect_md) { 261 vni = zero_vni; 262 addr6 = zero_addr6; 263 } else { 264 vni = geneve_hdr(skb)->vni; 265 addr6 = ip6h->saddr; 266 } 267 268 return geneve6_lookup(gs, addr6, vni); 269 #endif 270 } 271 return NULL; 272 } 273 274 /* geneve receive/decap routine */ 275 static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs, 276 struct sk_buff *skb, const struct genevehdr *gnvh) 277 { 278 struct metadata_dst *tun_dst = NULL; 279 unsigned int len; 280 int nh, err = 0; 281 void *oiph; 282 283 if (ip_tunnel_collect_metadata() || gs->collect_md) { 284 IP_TUNNEL_DECLARE_FLAGS(flags) = { }; 285 286 __set_bit(IP_TUNNEL_KEY_BIT, flags); 287 __assign_bit(IP_TUNNEL_OAM_BIT, flags, gnvh->oam); 288 __assign_bit(IP_TUNNEL_CRIT_OPT_BIT, flags, gnvh->critical); 289 290 tun_dst = udp_tun_rx_dst(skb, geneve_get_sk_family(gs), flags, 291 vni_to_tunnel_id(gnvh->vni), 292 gnvh->opt_len * 4); 293 if (!tun_dst) { 294 dev_dstats_rx_dropped(geneve->dev); 295 goto drop; 296 } 297 /* Update tunnel dst according to Geneve options. */ 298 ip_tunnel_flags_zero(flags); 299 __set_bit(IP_TUNNEL_GENEVE_OPT_BIT, flags); 300 ip_tunnel_info_opts_set(&tun_dst->u.tun_info, 301 gnvh->options, gnvh->opt_len * 4, 302 flags); 303 } else { 304 /* Drop packets w/ critical options, 305 * since we don't support any... 306 */ 307 if (gnvh->critical) { 308 DEV_STATS_INC(geneve->dev, rx_frame_errors); 309 DEV_STATS_INC(geneve->dev, rx_errors); 310 goto drop; 311 } 312 } 313 314 if (tun_dst) 315 skb_dst_set(skb, &tun_dst->dst); 316 317 if (gnvh->proto_type == htons(ETH_P_TEB)) { 318 skb_reset_mac_header(skb); 319 skb->protocol = eth_type_trans(skb, geneve->dev); 320 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); 321 322 /* Ignore packet loops (and multicast echo) */ 323 if (ether_addr_equal(eth_hdr(skb)->h_source, 324 geneve->dev->dev_addr)) { 325 DEV_STATS_INC(geneve->dev, rx_errors); 326 goto drop; 327 } 328 } else { 329 skb_reset_mac_header(skb); 330 skb->dev = geneve->dev; 331 skb->pkt_type = PACKET_HOST; 332 } 333 334 /* Save offset of outer header relative to skb->head, 335 * because we are going to reset the network header to the inner header 336 * and might change skb->head. 337 */ 338 nh = skb_network_header(skb) - skb->head; 339 340 skb_reset_network_header(skb); 341 342 if (!pskb_inet_may_pull(skb)) { 343 DEV_STATS_INC(geneve->dev, rx_length_errors); 344 DEV_STATS_INC(geneve->dev, rx_errors); 345 goto drop; 346 } 347 348 /* Get the outer header. */ 349 oiph = skb->head + nh; 350 351 if (geneve_get_sk_family(gs) == AF_INET) 352 err = IP_ECN_decapsulate(oiph, skb); 353 #if IS_ENABLED(CONFIG_IPV6) 354 else 355 err = IP6_ECN_decapsulate(oiph, skb); 356 #endif 357 358 if (unlikely(err)) { 359 if (log_ecn_error) { 360 if (geneve_get_sk_family(gs) == AF_INET) 361 net_info_ratelimited("non-ECT from %pI4 " 362 "with TOS=%#x\n", 363 &((struct iphdr *)oiph)->saddr, 364 ((struct iphdr *)oiph)->tos); 365 #if IS_ENABLED(CONFIG_IPV6) 366 else 367 net_info_ratelimited("non-ECT from %pI6\n", 368 &((struct ipv6hdr *)oiph)->saddr); 369 #endif 370 } 371 if (err > 1) { 372 DEV_STATS_INC(geneve->dev, rx_frame_errors); 373 DEV_STATS_INC(geneve->dev, rx_errors); 374 goto drop; 375 } 376 } 377 378 /* Skip the additional GRO stage when hints are in use. */ 379 len = skb->len; 380 if (skb->encapsulation) 381 err = netif_rx(skb); 382 else 383 err = gro_cells_receive(&geneve->gro_cells, skb); 384 if (likely(err == NET_RX_SUCCESS)) 385 dev_dstats_rx_add(geneve->dev, len); 386 387 return; 388 drop: 389 /* Consume bad packet */ 390 kfree_skb(skb); 391 } 392 393 /* Setup stats when device is created */ 394 static int geneve_init(struct net_device *dev) 395 { 396 struct geneve_dev *geneve = netdev_priv(dev); 397 int err; 398 399 err = gro_cells_init(&geneve->gro_cells, dev); 400 if (err) 401 return err; 402 403 netdev_lockdep_set_classes(dev); 404 return 0; 405 } 406 407 static void geneve_uninit(struct net_device *dev) 408 { 409 struct geneve_dev *geneve = netdev_priv(dev); 410 411 gro_cells_destroy(&geneve->gro_cells); 412 } 413 414 static int geneve_hlen(const struct genevehdr *gh) 415 { 416 return sizeof(*gh) + gh->opt_len * 4; 417 } 418 419 /* 420 * Look for GRO hint in the genenve options; if not found or does not pass basic 421 * sanitization return 0, otherwise the offset WRT the geneve hdr start. 422 */ 423 static unsigned int 424 geneve_opt_gro_hint_off(const struct genevehdr *gh, __be16 *type, 425 unsigned int *gh_len) 426 { 427 struct geneve_opt *opt = (void *)(gh + 1); 428 unsigned int id, opt_len = gh->opt_len; 429 struct geneve_opt_gro_hint *gro_hint; 430 431 while (opt_len >= (GENEVE_OPT_GRO_HINT_SIZE >> 2)) { 432 if (opt->opt_class == htons(GENEVE_OPT_NETDEV_CLASS) && 433 opt->type == GENEVE_OPT_GRO_HINT_TYPE && 434 opt->length == GENEVE_OPT_GRO_HINT_LEN) 435 goto found; 436 437 /* check for bad opt len */ 438 if (opt->length + 1 >= opt_len) 439 return 0; 440 441 /* next opt */ 442 opt_len -= opt->length + 1; 443 opt = ((void *)opt) + ((opt->length + 1) << 2); 444 } 445 return 0; 446 447 found: 448 gro_hint = (struct geneve_opt_gro_hint *)opt->opt_data; 449 450 /* 451 * Sanitize the hinted hdrs: the nested transport is UDP and must fit 452 * the overall hinted hdr size. 453 */ 454 if (gro_hint->nested_tp_offset + sizeof(struct udphdr) > 455 gro_hint->nested_hdr_len) 456 return 0; 457 458 if (gro_hint->nested_nh_offset + 459 (gro_hint->nested_is_v6 ? sizeof(struct ipv6hdr) : 460 sizeof(struct iphdr)) > 461 gro_hint->nested_tp_offset) 462 return 0; 463 464 /* Allow only supported L2. */ 465 id = gro_hint->inner_proto_id; 466 if (id >= ARRAY_SIZE(proto_id_map)) 467 return 0; 468 469 *type = proto_id_map[id]; 470 *gh_len += gro_hint->nested_hdr_len; 471 472 return (void *)gro_hint - (void *)gh; 473 } 474 475 static const struct geneve_opt_gro_hint * 476 geneve_opt_gro_hint(const struct genevehdr *gh, unsigned int hint_off) 477 { 478 return (const struct geneve_opt_gro_hint *)((void *)gh + hint_off); 479 } 480 481 static unsigned int 482 geneve_sk_gro_hint_off(const struct sock *sk, const struct genevehdr *gh, 483 __be16 *type, unsigned int *gh_len) 484 { 485 const struct geneve_sock *gs = rcu_dereference_sk_user_data(sk); 486 487 if (!gs || !gs->gro_hint) 488 return 0; 489 return geneve_opt_gro_hint_off(gh, type, gh_len); 490 } 491 492 /* Validate the packet headers pointed by data WRT the provided hint */ 493 static bool 494 geneve_opt_gro_hint_validate(void *data, 495 const struct geneve_opt_gro_hint *gro_hint) 496 { 497 void *nested_nh = data + gro_hint->nested_nh_offset; 498 struct iphdr *iph; 499 500 if (gro_hint->nested_is_v6) { 501 struct ipv6hdr *ipv6h = nested_nh; 502 struct ipv6_opt_hdr *opth; 503 int offset, len; 504 505 if (ipv6h->nexthdr == IPPROTO_UDP) 506 return true; 507 508 offset = sizeof(*ipv6h) + gro_hint->nested_nh_offset; 509 while (offset + sizeof(*opth) <= gro_hint->nested_tp_offset) { 510 opth = data + offset; 511 512 len = ipv6_optlen(opth); 513 if (len + offset > gro_hint->nested_tp_offset) 514 return false; 515 if (opth->nexthdr == IPPROTO_UDP) 516 return true; 517 518 offset += len; 519 } 520 return false; 521 } 522 523 iph = nested_nh; 524 if (*(u8 *)iph != 0x45 || ip_is_fragment(iph) || 525 iph->protocol != IPPROTO_UDP || ip_fast_csum((u8 *)iph, 5)) 526 return false; 527 528 return true; 529 } 530 531 /* 532 * Validate the skb headers following the specified geneve hdr vs the 533 * provided hint, including nested L4 checksum. 534 * The caller already ensured that the relevant amount of data is available 535 * in the linear part. 536 */ 537 static bool 538 geneve_opt_gro_hint_validate_csum(const struct sk_buff *skb, 539 const struct genevehdr *gh, 540 const struct geneve_opt_gro_hint *gro_hint) 541 { 542 unsigned int plen, gh_len = geneve_hlen(gh); 543 void *nested = (void *)gh + gh_len; 544 struct udphdr *nested_uh; 545 unsigned int nested_len; 546 struct ipv6hdr *ipv6h; 547 struct iphdr *iph; 548 __wsum csum, psum; 549 550 if (!geneve_opt_gro_hint_validate(nested, gro_hint)) 551 return false; 552 553 /* Use GRO hints with nested csum only if the outer header has csum. */ 554 nested_uh = nested + gro_hint->nested_tp_offset; 555 if (!nested_uh->check || skb->ip_summed == CHECKSUM_PARTIAL) 556 return true; 557 558 if (!NAPI_GRO_CB(skb)->csum_valid) 559 return false; 560 561 /* Compute the complete checksum up to the nested transport. */ 562 plen = gh_len + gro_hint->nested_tp_offset; 563 csum = csum_sub(NAPI_GRO_CB(skb)->csum, csum_partial(gh, plen, 0)); 564 nested_len = skb_gro_len(skb) - plen; 565 566 /* Compute the nested pseudo header csum. */ 567 ipv6h = nested + gro_hint->nested_nh_offset; 568 iph = (struct iphdr *)ipv6h; 569 psum = gro_hint->nested_is_v6 ? 570 ~csum_unfold(csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, 571 nested_len, IPPROTO_UDP, 0)) : 572 csum_tcpudp_nofold(iph->saddr, iph->daddr, 573 nested_len, IPPROTO_UDP, 0); 574 575 return !csum_fold(csum_add(psum, csum)); 576 } 577 578 static int geneve_post_decap_hint(const struct sock *sk, struct sk_buff *skb, 579 unsigned int gh_len, 580 struct genevehdr **geneveh) 581 { 582 const struct geneve_opt_gro_hint *gro_hint; 583 unsigned int len, total_len, hint_off; 584 struct ipv6hdr *ipv6h; 585 struct iphdr *iph; 586 struct udphdr *uh; 587 __be16 p; 588 int err; 589 590 hint_off = geneve_sk_gro_hint_off(sk, *geneveh, &p, &len); 591 if (!hint_off) 592 return 0; 593 594 if (!skb_is_gso(skb)) 595 return 0; 596 597 gro_hint = geneve_opt_gro_hint(*geneveh, hint_off); 598 if (unlikely(!pskb_may_pull(skb, gro_hint->nested_hdr_len))) 599 return -ENOMEM; 600 601 *geneveh = geneve_hdr(skb); 602 gro_hint = geneve_opt_gro_hint(*geneveh, hint_off); 603 604 /* 605 * Validate hints from untrusted source before accessing 606 * the headers; csum will be checked later by the nested 607 * protocol rx path. 608 */ 609 if (unlikely(skb_shinfo(skb)->gso_type & SKB_GSO_DODGY && 610 !geneve_opt_gro_hint_validate(skb->data, gro_hint))) 611 return -EINVAL; 612 613 total_len = skb->len - gro_hint->nested_nh_offset; 614 if (total_len >= GRO_LEGACY_MAX_SIZE) 615 return -E2BIG; 616 617 err = skb_ensure_writable(skb, gro_hint->nested_tp_offset + sizeof(*uh)); 618 if (unlikely(err)) 619 return err; 620 621 *geneveh = geneve_hdr(skb); 622 gro_hint = geneve_opt_gro_hint(*geneveh, hint_off); 623 624 ipv6h = (void *)skb->data + gro_hint->nested_nh_offset; 625 iph = (struct iphdr *)ipv6h; 626 627 /* 628 * After stripping the outer encap, the packet still carries a 629 * tunnel encapsulation: the nested one. 630 */ 631 skb->encapsulation = 1; 632 633 /* GSO expect a valid transpor header, move it to the current one. */ 634 skb_set_transport_header(skb, gro_hint->nested_tp_offset); 635 636 /* Adjust the nested IP{6} hdr to actual GSO len. */ 637 if (gro_hint->nested_is_v6) { 638 ipv6h->payload_len = htons(total_len - sizeof(*ipv6h)); 639 } else { 640 __be16 old_len = iph->tot_len; 641 642 iph->tot_len = htons(total_len); 643 644 /* For IPv4 additionally adjust the nested csum. */ 645 csum_replace2(&iph->check, old_len, iph->tot_len); 646 ip_send_check(iph); 647 } 648 649 /* Adjust the nested UDP header len and checksum. */ 650 uh = udp_hdr(skb); 651 udp_set_len_short(uh, skb->len - gro_hint->nested_tp_offset); 652 if (uh->check) { 653 len = skb->len - gro_hint->nested_tp_offset; 654 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM; 655 if (gro_hint->nested_is_v6) 656 uh->check = ~udp_v6_check(len, &ipv6h->saddr, 657 &ipv6h->daddr, 0); 658 else 659 uh->check = ~udp_v4_check(len, iph->saddr, 660 iph->daddr, 0); 661 } else { 662 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL; 663 } 664 return 0; 665 } 666 667 /* Callback from net/ipv4/udp.c to receive packets */ 668 static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb) 669 { 670 const struct geneve_config *cfg; 671 struct genevehdr *geneveh; 672 struct geneve_dev *geneve; 673 struct geneve_sock *gs; 674 __be16 inner_proto; 675 int opts_len; 676 677 /* Need UDP and Geneve header to be present */ 678 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN))) 679 goto drop; 680 681 /* Return packets with reserved bits set */ 682 geneveh = geneve_hdr(skb); 683 if (unlikely(geneveh->ver != GENEVE_VER)) 684 goto drop; 685 686 gs = rcu_dereference_sk_user_data(sk); 687 if (!gs) 688 goto drop; 689 690 geneve = geneve_lookup_skb(gs, skb); 691 if (!geneve) 692 goto drop; 693 694 inner_proto = geneveh->proto_type; 695 696 cfg = rcu_dereference(geneve->cfg); 697 if (unlikely(!cfg || (!cfg->inner_proto_inherit && 698 inner_proto != htons(ETH_P_TEB)))) { 699 dev_dstats_rx_dropped(geneve->dev); 700 goto drop; 701 } 702 703 opts_len = geneveh->opt_len * 4; 704 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len, inner_proto, 705 !net_eq(geneve->net, dev_net(geneve->dev)))) { 706 dev_dstats_rx_dropped(geneve->dev); 707 goto drop; 708 } 709 710 /* 711 * After hint processing, the transport header points to the inner one 712 * and we can't use anymore on geneve_hdr(). 713 */ 714 geneveh = geneve_hdr(skb); 715 if (geneve_post_decap_hint(sk, skb, sizeof(struct genevehdr) + 716 opts_len, &geneveh)) { 717 DEV_STATS_INC(geneve->dev, rx_errors); 718 goto drop; 719 } 720 721 geneve_rx(geneve, gs, skb, geneveh); 722 return 0; 723 724 drop: 725 /* Consume bad packet */ 726 kfree_skb(skb); 727 return 0; 728 } 729 730 /* Callback from net/ipv{4,6}/udp.c to check that we have a tunnel for errors */ 731 static int geneve_udp_encap_err_lookup(struct sock *sk, struct sk_buff *skb) 732 { 733 struct genevehdr *geneveh; 734 struct geneve_sock *gs; 735 u8 zero_vni[3] = { 0 }; 736 u8 *vni = zero_vni; 737 738 if (!pskb_may_pull(skb, skb_transport_offset(skb) + GENEVE_BASE_HLEN)) 739 return -EINVAL; 740 741 geneveh = geneve_hdr(skb); 742 if (geneveh->ver != GENEVE_VER) 743 return -EINVAL; 744 745 if (geneveh->proto_type != htons(ETH_P_TEB)) 746 return -EINVAL; 747 748 gs = rcu_dereference_sk_user_data(sk); 749 if (!gs) 750 return -ENOENT; 751 752 if (geneve_get_sk_family(gs) == AF_INET) { 753 struct iphdr *iph = ip_hdr(skb); 754 __be32 addr4 = 0; 755 756 if (!gs->collect_md) { 757 vni = geneve_hdr(skb)->vni; 758 addr4 = iph->daddr; 759 } 760 761 return geneve_lookup(gs, addr4, vni) ? 0 : -ENOENT; 762 } 763 764 #if IS_ENABLED(CONFIG_IPV6) 765 if (geneve_get_sk_family(gs) == AF_INET6) { 766 struct ipv6hdr *ip6h = ipv6_hdr(skb); 767 struct in6_addr addr6; 768 769 memset(&addr6, 0, sizeof(struct in6_addr)); 770 771 if (!gs->collect_md) { 772 vni = geneve_hdr(skb)->vni; 773 addr6 = ip6h->daddr; 774 } 775 776 return geneve6_lookup(gs, addr6, vni) ? 0 : -ENOENT; 777 } 778 #endif 779 780 return -EPFNOSUPPORT; 781 } 782 783 static struct sock *geneve_create_sock(struct net *net, 784 struct geneve_dev *geneve, 785 const struct geneve_config *cfg, bool ipv6) 786 { 787 const struct ip_tunnel_info *info = &cfg->info; 788 struct udp_port_cfg udp_conf; 789 struct socket *sock; 790 int err; 791 792 memset(&udp_conf, 0, sizeof(udp_conf)); 793 794 #if IS_ENABLED(CONFIG_IPV6) 795 if (ipv6) { 796 udp_conf.family = AF_INET6; 797 udp_conf.ipv6_v6only = 1; 798 udp_conf.use_udp6_rx_checksums = cfg->use_udp6_rx_checksums; 799 udp_conf.local_ip6 = info->key.u.ipv6.src; 800 } else 801 #endif 802 { 803 udp_conf.family = AF_INET; 804 udp_conf.local_ip.s_addr = info->key.u.ipv4.src; 805 } 806 807 udp_conf.local_udp_port = info->key.tp_dst; 808 809 /* Open UDP socket */ 810 err = udp_sock_create(net, &udp_conf, &sock); 811 if (err < 0) 812 return ERR_PTR(err); 813 814 udp_allow_gso(sock->sk); 815 return sock->sk; 816 } 817 818 static bool geneve_hdr_match(struct sk_buff *skb, 819 const struct genevehdr *gh, 820 const struct genevehdr *gh2, 821 unsigned int hint_off) 822 { 823 const struct geneve_opt_gro_hint *gro_hint; 824 void *nested, *nested2, *nh, *nh2; 825 struct udphdr *udp, *udp2; 826 unsigned int gh_len; 827 828 /* Match the geneve hdr and options */ 829 if (gh->opt_len != gh2->opt_len) 830 return false; 831 832 gh_len = geneve_hlen(gh); 833 if (memcmp(gh, gh2, gh_len)) 834 return false; 835 836 if (!hint_off) 837 return true; 838 839 /* 840 * When gro is present consider the nested headers as part 841 * of the geneve options 842 */ 843 nested = (void *)gh + gh_len; 844 nested2 = (void *)gh2 + gh_len; 845 gro_hint = geneve_opt_gro_hint(gh, hint_off); 846 if (!memcmp(nested, nested2, gro_hint->nested_hdr_len)) 847 return true; 848 849 /* 850 * The nested headers differ; the packets can still belong to 851 * the same flow when IPs/proto/ports match; if so flushing is 852 * required. 853 */ 854 nh = nested + gro_hint->nested_nh_offset; 855 nh2 = nested2 + gro_hint->nested_nh_offset; 856 if (gro_hint->nested_is_v6) { 857 struct ipv6hdr *iph = nh, *iph2 = nh2; 858 unsigned int nested_nlen; 859 __be32 first_word; 860 861 first_word = *(__be32 *)iph ^ *(__be32 *)iph2; 862 if ((first_word & htonl(0xF00FFFFF)) || 863 !ipv6_addr_equal(&iph->saddr, &iph2->saddr) || 864 !ipv6_addr_equal(&iph->daddr, &iph2->daddr) || 865 iph->nexthdr != iph2->nexthdr) 866 return false; 867 868 nested_nlen = gro_hint->nested_tp_offset - 869 gro_hint->nested_nh_offset; 870 if (nested_nlen > sizeof(struct ipv6hdr) && 871 (memcmp(iph + 1, iph2 + 1, 872 nested_nlen - sizeof(struct ipv6hdr)))) 873 return false; 874 } else { 875 struct iphdr *iph = nh, *iph2 = nh2; 876 877 if ((iph->protocol ^ iph2->protocol) | 878 ((__force u32)iph->saddr ^ (__force u32)iph2->saddr) | 879 ((__force u32)iph->daddr ^ (__force u32)iph2->daddr)) 880 return false; 881 } 882 883 udp = nested + gro_hint->nested_tp_offset; 884 udp2 = nested2 + gro_hint->nested_tp_offset; 885 if (udp->source != udp2->source || udp->dest != udp2->dest || 886 udp->check != udp2->check) 887 return false; 888 889 NAPI_GRO_CB(skb)->flush = 1; 890 return true; 891 } 892 893 static struct sk_buff *geneve_gro_receive(struct sock *sk, 894 struct list_head *head, 895 struct sk_buff *skb) 896 { 897 unsigned int hlen, gh_len, off_gnv, hint_off; 898 const struct geneve_opt_gro_hint *gro_hint; 899 const struct packet_offload *ptype; 900 struct genevehdr *gh, *gh2; 901 struct sk_buff *pp = NULL; 902 struct sk_buff *p; 903 int flush = 1; 904 __be16 type; 905 906 off_gnv = skb_gro_offset(skb); 907 hlen = off_gnv + sizeof(*gh); 908 gh = skb_gro_header(skb, hlen, off_gnv); 909 if (unlikely(!gh)) 910 goto out; 911 912 if (gh->ver != GENEVE_VER || gh->oam) 913 goto out; 914 gh_len = geneve_hlen(gh); 915 type = gh->proto_type; 916 917 hlen = off_gnv + gh_len; 918 if (!skb_gro_may_pull(skb, hlen)) { 919 gh = skb_gro_header_slow(skb, hlen, off_gnv); 920 if (unlikely(!gh)) 921 goto out; 922 } 923 924 /* The GRO hint/nested hdr could use a different ethernet type. */ 925 hint_off = geneve_sk_gro_hint_off(sk, gh, &type, &gh_len); 926 if (hint_off) { 927 928 /* 929 * If the hint is present, and nested hdr validation fails, do 930 * not attempt plain GRO: it will ignore inner hdrs and cause 931 * OoO. 932 */ 933 gh = skb_gro_header(skb, off_gnv + gh_len, off_gnv); 934 if (unlikely(!gh)) 935 goto out; 936 937 gro_hint = geneve_opt_gro_hint(gh, hint_off); 938 if (!geneve_opt_gro_hint_validate_csum(skb, gh, gro_hint)) 939 goto out; 940 } 941 942 list_for_each_entry(p, head, list) { 943 if (!NAPI_GRO_CB(p)->same_flow) 944 continue; 945 946 gh2 = (struct genevehdr *)(p->data + off_gnv); 947 if (!geneve_hdr_match(skb, gh, gh2, hint_off)) { 948 NAPI_GRO_CB(p)->same_flow = 0; 949 continue; 950 } 951 } 952 953 skb_gro_pull(skb, gh_len); 954 skb_gro_postpull_rcsum(skb, gh, gh_len); 955 if (likely(type == htons(ETH_P_TEB))) 956 return call_gro_receive(eth_gro_receive, head, skb); 957 958 ptype = gro_find_receive_by_type(type); 959 if (!ptype) 960 goto out; 961 962 pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb); 963 flush = 0; 964 965 out: 966 skb_gro_flush_final(skb, pp, flush); 967 968 return pp; 969 } 970 971 static int geneve_gro_complete(struct sock *sk, struct sk_buff *skb, 972 int nhoff) 973 { 974 struct genevehdr *gh; 975 struct packet_offload *ptype; 976 __be16 type; 977 unsigned int gh_len; 978 int err = -ENOSYS; 979 980 gh = (struct genevehdr *)(skb->data + nhoff); 981 gh_len = geneve_hlen(gh); 982 type = gh->proto_type; 983 geneve_sk_gro_hint_off(sk, gh, &type, &gh_len); 984 985 /* Bail out if we are about to dispatch past the inner network header 986 * gro_receive() validated. An inner VLAN tag only pushes 987 * inner_network_offset out, so use a lower bound. 988 */ 989 if (skb->encapsulation) { 990 unsigned int inner_nh = nhoff + gh_len; 991 992 if (type == htons(ETH_P_TEB)) 993 inner_nh += ETH_HLEN; 994 995 if (unlikely(inner_nh > NAPI_GRO_CB(skb)->inner_network_offset)) 996 return -EINVAL; 997 } 998 999 /* since skb->encapsulation is set, eth_gro_complete() sets the inner mac header */ 1000 if (likely(type == htons(ETH_P_TEB))) 1001 return eth_gro_complete(skb, nhoff + gh_len); 1002 1003 ptype = gro_find_complete_by_type(type); 1004 if (ptype) 1005 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len); 1006 1007 skb_set_inner_mac_header(skb, nhoff + gh_len); 1008 1009 return err; 1010 } 1011 1012 /* Create new listen socket if needed */ 1013 static struct geneve_sock *geneve_socket_create(struct net *net, 1014 struct geneve_dev *geneve, 1015 const struct geneve_config *cfg, bool ipv6) 1016 { 1017 struct geneve_net *gn = net_generic(net, geneve_net_id); 1018 struct udp_tunnel_sock_cfg tunnel_cfg; 1019 struct geneve_sock *gs; 1020 struct sock *sk; 1021 int h; 1022 1023 gs = kzalloc_obj(*gs); 1024 if (!gs) 1025 return ERR_PTR(-ENOMEM); 1026 1027 sk = geneve_create_sock(net, geneve, cfg, ipv6); 1028 if (IS_ERR(sk)) { 1029 kfree(gs); 1030 return ERR_CAST(sk); 1031 } 1032 1033 gs->sk = sk; 1034 gs->refcnt = 1; 1035 for (h = 0; h < VNI_HASH_SIZE; ++h) 1036 INIT_HLIST_HEAD(&gs->vni_list[h]); 1037 1038 /* Mark socket as an encapsulation socket */ 1039 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg)); 1040 tunnel_cfg.sk_user_data = gs; 1041 tunnel_cfg.encap_type = 1; 1042 tunnel_cfg.gro_receive = geneve_gro_receive; 1043 tunnel_cfg.gro_complete = geneve_gro_complete; 1044 tunnel_cfg.encap_rcv = geneve_udp_encap_recv; 1045 tunnel_cfg.encap_err_lookup = geneve_udp_encap_err_lookup; 1046 tunnel_cfg.encap_destroy = NULL; 1047 setup_udp_tunnel_sock(net, sk, &tunnel_cfg); 1048 list_add(&gs->list, &gn->sock_list); 1049 return gs; 1050 } 1051 1052 static void __geneve_sock_release(struct geneve_dev *geneve, bool ipv6) 1053 { 1054 struct geneve_dev_node *node; 1055 struct geneve_sock *gs; 1056 struct geneve_net *gn; 1057 1058 #if IS_ENABLED(CONFIG_IPV6) 1059 if (ipv6) { 1060 gs = rtnl_dereference(geneve->sock6); 1061 rcu_assign_pointer(geneve->sock6, NULL); 1062 node = &geneve->hlist6; 1063 } else 1064 #endif 1065 { 1066 gs = rtnl_dereference(geneve->sock4); 1067 rcu_assign_pointer(geneve->sock4, NULL); 1068 node = &geneve->hlist4; 1069 } 1070 1071 if (!gs) 1072 return; 1073 1074 gn = net_generic(sock_net(gs->sk), geneve_net_id); 1075 mutex_lock(&gn->lock); 1076 1077 hlist_del_init_rcu(&node->hlist); 1078 1079 if (--gs->refcnt) { 1080 mutex_unlock(&gn->lock); 1081 return; 1082 } 1083 1084 list_del(&gs->list); 1085 mutex_unlock(&gn->lock); 1086 1087 udp_tunnel_notify_del_rx_port(gs->sk, UDP_TUNNEL_TYPE_GENEVE); 1088 udp_tunnel_sock_release(gs->sk); 1089 kfree_rcu(gs, rcu); 1090 } 1091 1092 static void geneve_sock_release(struct geneve_dev *geneve) 1093 { 1094 #if IS_ENABLED(CONFIG_IPV6) 1095 __geneve_sock_release(geneve, true); 1096 #endif 1097 __geneve_sock_release(geneve, false); 1098 } 1099 1100 static struct geneve_sock *geneve_find_sock(struct net *net, 1101 struct geneve_dev *geneve, 1102 const struct geneve_config *cfg, bool ipv6) 1103 { 1104 struct geneve_net *gn = net_generic(net, geneve_net_id); 1105 const struct ip_tunnel_info *info = &cfg->info; 1106 sa_family_t family = ipv6 ? AF_INET6 : AF_INET; 1107 bool gro_hint = cfg->gro_hint; 1108 __be16 dst_port = info->key.tp_dst; 1109 struct geneve_sock *gs; 1110 1111 list_for_each_entry(gs, &gn->sock_list, list) { 1112 if (inet_sk(gs->sk)->inet_sport != dst_port) 1113 continue; 1114 1115 if (geneve_get_sk_family(gs) != family) 1116 continue; 1117 1118 if (gs->gro_hint != gro_hint) 1119 continue; 1120 1121 if (family == AF_INET && 1122 inet_sk(gs->sk)->inet_saddr != info->key.u.ipv4.src) 1123 continue; 1124 1125 #if IS_ENABLED(CONFIG_IPV6) 1126 if (family == AF_INET6 && 1127 !ipv6_addr_equal(&gs->sk->sk_v6_rcv_saddr, &info->key.u.ipv6.src)) 1128 continue; 1129 #endif 1130 1131 return gs; 1132 } 1133 1134 return NULL; 1135 } 1136 1137 static int geneve_sock_add(struct geneve_dev *geneve, 1138 const struct geneve_config *cfg, bool ipv6) 1139 { 1140 struct net *net = geneve->net; 1141 struct geneve_dev_node *node; 1142 struct geneve_sock *gs; 1143 struct geneve_net *gn; 1144 bool created = false; 1145 __u8 vni[3]; 1146 int ret = 0; 1147 __u32 hash; 1148 1149 gn = net_generic(net, geneve_net_id); 1150 mutex_lock(&gn->lock); 1151 1152 gs = geneve_find_sock(net, geneve, cfg, ipv6); 1153 if (gs) { 1154 gs->refcnt++; 1155 } else { 1156 gs = geneve_socket_create(net, geneve, cfg, ipv6); 1157 if (IS_ERR(gs)) { 1158 ret = PTR_ERR(gs); 1159 goto out; 1160 } 1161 1162 created = true; 1163 } 1164 1165 gs->collect_md = cfg->collect_md; 1166 gs->gro_hint = cfg->gro_hint; 1167 1168 #if IS_ENABLED(CONFIG_IPV6) 1169 if (ipv6) { 1170 rcu_assign_pointer(geneve->sock6, gs); 1171 node = &geneve->hlist6; 1172 } else 1173 #endif 1174 { 1175 rcu_assign_pointer(geneve->sock4, gs); 1176 node = &geneve->hlist4; 1177 } 1178 node->geneve = geneve; 1179 1180 tunnel_id_to_vni(cfg->info.key.tun_id, vni); 1181 hash = geneve_net_vni_hash(vni); 1182 hlist_add_head_rcu(&node->hlist, &gs->vni_list[hash]); 1183 1184 out: 1185 mutex_unlock(&gn->lock); 1186 1187 if (created) { 1188 /* Initialize the geneve udp offloads structure */ 1189 udp_tunnel_notify_add_rx_port(gs->sk, UDP_TUNNEL_TYPE_GENEVE); 1190 } 1191 1192 return ret; 1193 } 1194 1195 static int geneve_open(struct net_device *dev) 1196 { 1197 struct geneve_dev *geneve = netdev_priv(dev); 1198 const struct geneve_config *cfg; 1199 bool ipv4, ipv6, dualstack; 1200 int ret = 0; 1201 1202 cfg = rtnl_dereference(geneve->cfg); 1203 dualstack = cfg->dualstack; 1204 ipv6 = cfg->info.mode & IP_TUNNEL_INFO_IPV6 || dualstack; 1205 ipv4 = !ipv6 || dualstack; 1206 #if IS_ENABLED(CONFIG_IPV6) 1207 if (ipv6) { 1208 ret = geneve_sock_add(geneve, cfg, true); 1209 if (ret < 0 && ret != -EAFNOSUPPORT) 1210 ipv4 = false; 1211 } 1212 #endif 1213 if (ipv4) 1214 ret = geneve_sock_add(geneve, cfg, false); 1215 if (ret < 0) 1216 geneve_sock_release(geneve); 1217 1218 return ret; 1219 } 1220 1221 static int geneve_stop(struct net_device *dev) 1222 { 1223 struct geneve_dev *geneve = netdev_priv(dev); 1224 1225 geneve_sock_release(geneve); 1226 return 0; 1227 } 1228 1229 static void geneve_build_header(struct genevehdr *geneveh, 1230 const struct ip_tunnel_info *info, 1231 __be16 inner_proto) 1232 { 1233 geneveh->ver = GENEVE_VER; 1234 geneveh->opt_len = info->options_len / 4; 1235 geneveh->oam = test_bit(IP_TUNNEL_OAM_BIT, info->key.tun_flags); 1236 geneveh->critical = test_bit(IP_TUNNEL_CRIT_OPT_BIT, 1237 info->key.tun_flags); 1238 geneveh->rsvd1 = 0; 1239 tunnel_id_to_vni(info->key.tun_id, geneveh->vni); 1240 geneveh->proto_type = inner_proto; 1241 geneveh->rsvd2 = 0; 1242 1243 if (test_bit(IP_TUNNEL_GENEVE_OPT_BIT, info->key.tun_flags)) 1244 ip_tunnel_info_opts_get(geneveh->options, info); 1245 } 1246 1247 static int geneve_build_gro_hint_opt(const struct geneve_dev *geneve, 1248 const struct geneve_config *cfg, 1249 struct sk_buff *skb) 1250 { 1251 struct geneve_skb_cb *cb = GENEVE_SKB_CB(skb); 1252 struct geneve_opt_gro_hint *hint; 1253 unsigned int nhlen; 1254 bool nested_is_v6; 1255 int id; 1256 1257 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct geneve_skb_cb)); 1258 cb->gro_hint_len = 0; 1259 1260 /* Try to add the GRO hint only in case of double encap. */ 1261 if (!cfg->gro_hint || !skb->encapsulation) 1262 return 0; 1263 1264 /* 1265 * The nested headers must fit the geneve opt len fields and the 1266 * nested encap must carry a nested transport (UDP) header. 1267 */ 1268 nhlen = skb_inner_mac_header(skb) - skb->data; 1269 if (nhlen > 255 || !skb_transport_header_was_set(skb) || 1270 skb->inner_protocol_type != ENCAP_TYPE_ETHER || 1271 (skb_transport_offset(skb) + sizeof(struct udphdr) > nhlen)) 1272 return 0; 1273 1274 id = proto_to_id(skb->inner_protocol); 1275 if (id < 0) 1276 return 0; 1277 1278 nested_is_v6 = skb->protocol == htons(ETH_P_IPV6); 1279 if (nested_is_v6) { 1280 int start = skb_network_offset(skb) + sizeof(struct ipv6hdr); 1281 u8 proto = ipv6_hdr(skb)->nexthdr; 1282 __be16 foff; 1283 1284 if (ipv6_skip_exthdr(skb, start, &proto, &foff) < 0 || 1285 proto != IPPROTO_UDP) 1286 return 0; 1287 } else { 1288 if (ip_hdr(skb)->protocol != IPPROTO_UDP) 1289 return 0; 1290 } 1291 1292 hint = &cb->gro_hint; 1293 memset(hint, 0, sizeof(*hint)); 1294 hint->inner_proto_id = id; 1295 hint->nested_is_v6 = skb->protocol == htons(ETH_P_IPV6); 1296 hint->nested_nh_offset = skb_network_offset(skb); 1297 hint->nested_tp_offset = skb_transport_offset(skb); 1298 hint->nested_hdr_len = nhlen; 1299 cb->gro_hint_len = GENEVE_OPT_GRO_HINT_SIZE; 1300 return GENEVE_OPT_GRO_HINT_SIZE; 1301 } 1302 1303 static void geneve_put_gro_hint_opt(struct genevehdr *gnvh, int opt_size, 1304 const struct geneve_opt_gro_hint *hint) 1305 { 1306 struct geneve_opt *gro_opt; 1307 1308 /* geneve_build_header() did not took in account the GRO hint. */ 1309 gnvh->opt_len = (opt_size + GENEVE_OPT_GRO_HINT_SIZE) >> 2; 1310 1311 gro_opt = (void *)(gnvh + 1) + opt_size; 1312 memset(gro_opt, 0, sizeof(*gro_opt)); 1313 1314 gro_opt->opt_class = htons(GENEVE_OPT_NETDEV_CLASS); 1315 gro_opt->type = GENEVE_OPT_GRO_HINT_TYPE; 1316 gro_opt->length = GENEVE_OPT_GRO_HINT_LEN; 1317 memcpy(gro_opt + 1, hint, sizeof(*hint)); 1318 } 1319 1320 static int geneve_build_skb(struct dst_entry *dst, struct sk_buff *skb, 1321 const struct ip_tunnel_info *info, 1322 const struct geneve_dev *geneve, 1323 const struct geneve_config *cfg, int ip_hdr_len) 1324 { 1325 bool udp_sum = test_bit(IP_TUNNEL_CSUM_BIT, info->key.tun_flags); 1326 bool inner_proto_inherit = cfg->inner_proto_inherit; 1327 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev)); 1328 struct geneve_skb_cb *cb = GENEVE_SKB_CB(skb); 1329 struct genevehdr *gnvh; 1330 __be16 inner_proto; 1331 bool double_encap; 1332 int min_headroom; 1333 int opt_size; 1334 int err; 1335 1336 skb_reset_mac_header(skb); 1337 skb_scrub_packet(skb, xnet); 1338 1339 opt_size = info->options_len + cb->gro_hint_len; 1340 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len + 1341 GENEVE_BASE_HLEN + opt_size + ip_hdr_len; 1342 err = skb_cow_head(skb, min_headroom); 1343 if (unlikely(err)) 1344 goto free_dst; 1345 1346 double_encap = udp_tunnel_handle_partial(skb); 1347 err = udp_tunnel_handle_offloads(skb, udp_sum); 1348 if (err) 1349 goto free_dst; 1350 1351 gnvh = __skb_push(skb, sizeof(*gnvh) + opt_size); 1352 inner_proto = inner_proto_inherit ? skb->protocol : htons(ETH_P_TEB); 1353 geneve_build_header(gnvh, info, inner_proto); 1354 1355 if (cb->gro_hint_len) 1356 geneve_put_gro_hint_opt(gnvh, info->options_len, &cb->gro_hint); 1357 1358 udp_tunnel_set_inner_protocol(skb, double_encap, inner_proto); 1359 return 0; 1360 1361 free_dst: 1362 dst_release(dst); 1363 return err; 1364 } 1365 1366 static u8 geneve_get_dsfield(struct sk_buff *skb, struct net_device *dev, 1367 const struct geneve_config *cfg, 1368 const struct ip_tunnel_info *info, 1369 bool *use_cache) 1370 { 1371 u8 dsfield; 1372 1373 dsfield = info->key.tos; 1374 if (cfg && dsfield == 1 && !cfg->collect_md) { 1375 dsfield = ip_tunnel_get_dsfield(ip_hdr(skb), skb); 1376 *use_cache = false; 1377 } 1378 1379 return dsfield; 1380 } 1381 1382 static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev, 1383 struct geneve_dev *geneve, 1384 const struct geneve_config *cfg, 1385 const struct ip_tunnel_info *info) 1386 { 1387 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4); 1388 const struct ip_tunnel_key *key = &info->key; 1389 struct rtable *rt; 1390 bool use_cache; 1391 __u8 tos, ttl; 1392 __be16 df = 0; 1393 __be32 saddr; 1394 __be16 sport; 1395 int err; 1396 1397 if (skb_vlan_inet_prepare(skb, cfg->inner_proto_inherit)) 1398 return -EINVAL; 1399 1400 if (!gs4) 1401 return -EIO; 1402 1403 use_cache = ip_tunnel_dst_cache_usable(skb, info); 1404 tos = geneve_get_dsfield(skb, dev, cfg, info, &use_cache); 1405 sport = udp_flow_src_port(geneve->net, skb, 1406 cfg->port_min, 1407 cfg->port_max, true); 1408 1409 rt = udp_tunnel_dst_lookup(skb, dev, geneve->net, 0, &saddr, 1410 &info->key, 1411 sport, cfg->info.key.tp_dst, tos, 1412 use_cache ? 1413 (struct dst_cache *)&info->dst_cache : NULL); 1414 if (IS_ERR(rt)) 1415 return PTR_ERR(rt); 1416 1417 if (cfg->info.key.u.ipv4.src && 1418 saddr != cfg->info.key.u.ipv4.src) { 1419 dst_release(&rt->dst); 1420 return -EADDRNOTAVAIL; 1421 } 1422 1423 err = skb_tunnel_check_pmtu(skb, &rt->dst, 1424 GENEVE_IPV4_HLEN + info->options_len + 1425 geneve_build_gro_hint_opt(geneve, cfg, skb), 1426 netif_is_any_bridge_port(dev)); 1427 if (err < 0) { 1428 dst_release(&rt->dst); 1429 return err; 1430 } else if (err) { 1431 struct ip_tunnel_info *info; 1432 1433 info = skb_tunnel_info(skb); 1434 if (info) { 1435 struct ip_tunnel_info *unclone; 1436 1437 unclone = skb_tunnel_info_unclone(skb); 1438 if (unlikely(!unclone)) { 1439 dst_release(&rt->dst); 1440 return -ENOMEM; 1441 } 1442 1443 unclone->key.u.ipv4.dst = saddr; 1444 unclone->key.u.ipv4.src = info->key.u.ipv4.dst; 1445 } 1446 1447 if (!pskb_may_pull(skb, ETH_HLEN)) { 1448 dst_release(&rt->dst); 1449 return -EINVAL; 1450 } 1451 1452 skb->protocol = eth_type_trans(skb, geneve->dev); 1453 __netif_rx(skb); 1454 dst_release(&rt->dst); 1455 return -EMSGSIZE; 1456 } 1457 1458 tos = ip_tunnel_ecn_encap(tos, ip_hdr(skb), skb); 1459 if (cfg->collect_md) { 1460 ttl = key->ttl; 1461 1462 df = test_bit(IP_TUNNEL_DONT_FRAGMENT_BIT, key->tun_flags) ? 1463 htons(IP_DF) : 0; 1464 } else { 1465 if (cfg->ttl_inherit) 1466 ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb); 1467 else 1468 ttl = key->ttl; 1469 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst); 1470 1471 if (cfg->df == GENEVE_DF_SET) { 1472 df = htons(IP_DF); 1473 } else if (cfg->df == GENEVE_DF_INHERIT) { 1474 struct ethhdr *eth = skb_eth_hdr(skb); 1475 1476 if (ntohs(eth->h_proto) == ETH_P_IPV6) { 1477 df = htons(IP_DF); 1478 } else if (ntohs(eth->h_proto) == ETH_P_IP) { 1479 struct iphdr *iph = ip_hdr(skb); 1480 1481 if (iph->frag_off & htons(IP_DF)) 1482 df = htons(IP_DF); 1483 } 1484 } 1485 } 1486 1487 err = geneve_build_skb(&rt->dst, skb, info, geneve, cfg, 1488 sizeof(struct iphdr)); 1489 if (unlikely(err)) 1490 return err; 1491 1492 udp_tunnel_xmit_skb(rt, gs4->sk, skb, saddr, info->key.u.ipv4.dst, 1493 tos, ttl, df, sport, cfg->info.key.tp_dst, 1494 !net_eq(geneve->net, dev_net(geneve->dev)), 1495 !test_bit(IP_TUNNEL_CSUM_BIT, info->key.tun_flags), 1496 0); 1497 return 0; 1498 } 1499 1500 #if IS_ENABLED(CONFIG_IPV6) 1501 static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev, 1502 struct geneve_dev *geneve, 1503 const struct geneve_config *cfg, 1504 const struct ip_tunnel_info *info) 1505 { 1506 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6); 1507 const struct ip_tunnel_key *key = &info->key; 1508 struct dst_entry *dst = NULL; 1509 struct in6_addr saddr; 1510 bool use_cache; 1511 __u8 prio, ttl; 1512 __be16 sport; 1513 int err; 1514 1515 if (skb_vlan_inet_prepare(skb, cfg->inner_proto_inherit)) 1516 return -EINVAL; 1517 1518 if (!gs6) 1519 return -EIO; 1520 1521 use_cache = ip_tunnel_dst_cache_usable(skb, info); 1522 prio = geneve_get_dsfield(skb, dev, cfg, info, &use_cache); 1523 sport = udp_flow_src_port(geneve->net, skb, 1524 cfg->port_min, 1525 cfg->port_max, true); 1526 1527 dst = udp_tunnel6_dst_lookup(skb, dev, geneve->net, gs6->sk, 0, 1528 &saddr, key, sport, 1529 cfg->info.key.tp_dst, prio, 1530 use_cache ? 1531 (struct dst_cache *)&info->dst_cache : NULL); 1532 if (IS_ERR(dst)) 1533 return PTR_ERR(dst); 1534 1535 if (!ipv6_addr_any(&cfg->info.key.u.ipv6.src) && 1536 !ipv6_addr_equal(&saddr, &cfg->info.key.u.ipv6.src)) { 1537 dst_release(dst); 1538 return -EADDRNOTAVAIL; 1539 } 1540 1541 err = skb_tunnel_check_pmtu(skb, dst, 1542 GENEVE_IPV6_HLEN + info->options_len + 1543 geneve_build_gro_hint_opt(geneve, cfg, skb), 1544 netif_is_any_bridge_port(dev)); 1545 if (err < 0) { 1546 dst_release(dst); 1547 return err; 1548 } else if (err) { 1549 struct ip_tunnel_info *info = skb_tunnel_info(skb); 1550 1551 if (info) { 1552 struct ip_tunnel_info *unclone; 1553 1554 unclone = skb_tunnel_info_unclone(skb); 1555 if (unlikely(!unclone)) { 1556 dst_release(dst); 1557 return -ENOMEM; 1558 } 1559 1560 unclone->key.u.ipv6.dst = saddr; 1561 unclone->key.u.ipv6.src = info->key.u.ipv6.dst; 1562 } 1563 1564 if (!pskb_may_pull(skb, ETH_HLEN)) { 1565 dst_release(dst); 1566 return -EINVAL; 1567 } 1568 1569 skb->protocol = eth_type_trans(skb, geneve->dev); 1570 __netif_rx(skb); 1571 dst_release(dst); 1572 return -EMSGSIZE; 1573 } 1574 1575 prio = ip_tunnel_ecn_encap(prio, ip_hdr(skb), skb); 1576 if (cfg->collect_md) { 1577 ttl = key->ttl; 1578 } else { 1579 if (cfg->ttl_inherit) 1580 ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb); 1581 else 1582 ttl = key->ttl; 1583 ttl = ttl ? : ip6_dst_hoplimit(dst); 1584 } 1585 err = geneve_build_skb(dst, skb, info, geneve, cfg, sizeof(struct ipv6hdr)); 1586 if (unlikely(err)) 1587 return err; 1588 1589 udp_tunnel6_xmit_skb(dst, gs6->sk, skb, dev, 1590 &saddr, &key->u.ipv6.dst, prio, ttl, 1591 info->key.label, sport, cfg->info.key.tp_dst, 1592 !test_bit(IP_TUNNEL_CSUM_BIT, 1593 info->key.tun_flags), 1594 0); 1595 return 0; 1596 } 1597 #endif 1598 1599 static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev) 1600 { 1601 struct geneve_dev *geneve = netdev_priv(dev); 1602 const struct ip_tunnel_info *info = NULL; 1603 const struct geneve_config *cfg; 1604 int err; 1605 1606 rcu_read_lock(); 1607 cfg = rcu_dereference(geneve->cfg); 1608 if (cfg->collect_md) { 1609 info = skb_tunnel_info(skb); 1610 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) { 1611 netdev_dbg(dev, "no tunnel metadata\n"); 1612 dev_kfree_skb(skb); 1613 dev_dstats_tx_dropped(dev); 1614 rcu_read_unlock(); 1615 return NETDEV_TX_OK; 1616 } 1617 } else { 1618 info = &cfg->info; 1619 } 1620 1621 #if IS_ENABLED(CONFIG_IPV6) 1622 if (info->mode & IP_TUNNEL_INFO_IPV6) 1623 err = geneve6_xmit_skb(skb, dev, geneve, cfg, info); 1624 else 1625 #endif 1626 err = geneve_xmit_skb(skb, dev, geneve, cfg, info); 1627 rcu_read_unlock(); 1628 1629 if (likely(!err)) 1630 return NETDEV_TX_OK; 1631 1632 if (err != -EMSGSIZE) 1633 dev_kfree_skb(skb); 1634 1635 if (err == -ELOOP) 1636 DEV_STATS_INC(dev, collisions); 1637 else if (err == -ENETUNREACH) 1638 DEV_STATS_INC(dev, tx_carrier_errors); 1639 1640 DEV_STATS_INC(dev, tx_errors); 1641 return NETDEV_TX_OK; 1642 } 1643 1644 static int geneve_change_mtu(struct net_device *dev, int new_mtu) 1645 { 1646 if (new_mtu > dev->max_mtu) 1647 new_mtu = dev->max_mtu; 1648 else if (new_mtu < dev->min_mtu) 1649 new_mtu = dev->min_mtu; 1650 1651 WRITE_ONCE(dev->mtu, new_mtu); 1652 return 0; 1653 } 1654 1655 static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) 1656 { 1657 struct ip_tunnel_info *info = skb_tunnel_info(skb); 1658 struct geneve_dev *geneve = netdev_priv(dev); 1659 const struct geneve_config *cfg; 1660 __be16 sport; 1661 1662 cfg = rcu_dereference(geneve->cfg); 1663 if (unlikely(!cfg)) 1664 return -ENODEV; 1665 1666 if (ip_tunnel_info_af(info) == AF_INET) { 1667 struct rtable *rt; 1668 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4); 1669 bool use_cache; 1670 __be32 saddr; 1671 u8 tos; 1672 1673 if (!gs4) 1674 return -EIO; 1675 1676 use_cache = ip_tunnel_dst_cache_usable(skb, info); 1677 tos = geneve_get_dsfield(skb, dev, cfg, info, &use_cache); 1678 sport = udp_flow_src_port(geneve->net, skb, 1679 cfg->port_min, 1680 cfg->port_max, true); 1681 1682 rt = udp_tunnel_dst_lookup(skb, dev, geneve->net, 0, &saddr, 1683 &info->key, 1684 sport, cfg->info.key.tp_dst, 1685 tos, 1686 use_cache ? &info->dst_cache : NULL); 1687 if (IS_ERR(rt)) 1688 return PTR_ERR(rt); 1689 1690 ip_rt_put(rt); 1691 info->key.u.ipv4.src = saddr; 1692 #if IS_ENABLED(CONFIG_IPV6) 1693 } else if (ip_tunnel_info_af(info) == AF_INET6) { 1694 struct dst_entry *dst; 1695 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6); 1696 struct in6_addr saddr; 1697 bool use_cache; 1698 u8 prio; 1699 1700 if (!gs6) 1701 return -EIO; 1702 1703 use_cache = ip_tunnel_dst_cache_usable(skb, info); 1704 prio = geneve_get_dsfield(skb, dev, cfg, info, &use_cache); 1705 sport = udp_flow_src_port(geneve->net, skb, 1706 cfg->port_min, 1707 cfg->port_max, true); 1708 1709 dst = udp_tunnel6_dst_lookup(skb, dev, geneve->net, gs6->sk, 0, 1710 &saddr, &info->key, sport, 1711 cfg->info.key.tp_dst, prio, 1712 use_cache ? &info->dst_cache : NULL); 1713 if (IS_ERR(dst)) 1714 return PTR_ERR(dst); 1715 1716 dst_release(dst); 1717 info->key.u.ipv6.src = saddr; 1718 #endif 1719 } else { 1720 return -EINVAL; 1721 } 1722 1723 info->key.tp_src = sport; 1724 info->key.tp_dst = cfg->info.key.tp_dst; 1725 return 0; 1726 } 1727 1728 static const struct net_device_ops geneve_netdev_ops = { 1729 .ndo_init = geneve_init, 1730 .ndo_uninit = geneve_uninit, 1731 .ndo_open = geneve_open, 1732 .ndo_stop = geneve_stop, 1733 .ndo_start_xmit = geneve_xmit, 1734 .ndo_change_mtu = geneve_change_mtu, 1735 .ndo_validate_addr = eth_validate_addr, 1736 .ndo_set_mac_address = eth_mac_addr, 1737 .ndo_fill_metadata_dst = geneve_fill_metadata_dst, 1738 }; 1739 1740 static void geneve_get_drvinfo(struct net_device *dev, 1741 struct ethtool_drvinfo *drvinfo) 1742 { 1743 strscpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version)); 1744 strscpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver)); 1745 } 1746 1747 static const struct ethtool_ops geneve_ethtool_ops = { 1748 .get_drvinfo = geneve_get_drvinfo, 1749 .get_link = ethtool_op_get_link, 1750 }; 1751 1752 /* Info for udev, that this is a virtual tunnel endpoint */ 1753 static const struct device_type geneve_type = { 1754 .name = "geneve", 1755 }; 1756 1757 /* Calls the ndo_udp_tunnel_add of the caller in order to 1758 * supply the listening GENEVE udp ports. Callers are expected 1759 * to implement the ndo_udp_tunnel_add. 1760 */ 1761 static void geneve_offload_rx_ports(struct net_device *dev, bool push) 1762 { 1763 struct net *net = dev_net(dev); 1764 struct geneve_net *gn = net_generic(net, geneve_net_id); 1765 struct geneve_sock *gs; 1766 1767 ASSERT_RTNL(); 1768 1769 mutex_lock(&gn->lock); 1770 1771 list_for_each_entry(gs, &gn->sock_list, list) { 1772 if (push) { 1773 udp_tunnel_push_rx_port(dev, gs->sk, 1774 UDP_TUNNEL_TYPE_GENEVE); 1775 } else { 1776 udp_tunnel_drop_rx_port(dev, gs->sk, 1777 UDP_TUNNEL_TYPE_GENEVE); 1778 } 1779 } 1780 1781 mutex_unlock(&gn->lock); 1782 } 1783 1784 static struct geneve_config *geneve_config_alloc(const struct geneve_config *src) 1785 { 1786 struct geneve_config *cfg; 1787 int err; 1788 1789 cfg = kmemdup(src, sizeof(*src), GFP_KERNEL); 1790 if (!cfg) 1791 return ERR_PTR(-ENOMEM); 1792 1793 cfg->info.dst_cache.cache = NULL; 1794 err = dst_cache_init(&cfg->info.dst_cache, GFP_KERNEL); 1795 if (err) { 1796 kfree(cfg); 1797 return ERR_PTR(err); 1798 } 1799 1800 return cfg; 1801 } 1802 1803 static void geneve_config_free(struct geneve_config *cfg) 1804 { 1805 if (cfg) { 1806 dst_cache_destroy(&cfg->info.dst_cache); 1807 kfree(cfg); 1808 } 1809 } 1810 1811 static void geneve_config_free_rcu(struct rcu_head *head) 1812 { 1813 struct geneve_config *cfg = container_of(head, struct geneve_config, rcu); 1814 1815 geneve_config_free(cfg); 1816 } 1817 1818 /* Initialize the device structure. */ 1819 static void geneve_free_dev(struct net_device *dev) 1820 { 1821 struct geneve_dev *geneve = netdev_priv(dev); 1822 struct geneve_config *cfg = rcu_dereference_protected(geneve->cfg, 1); 1823 1824 geneve_config_free(cfg); 1825 RCU_INIT_POINTER(geneve->cfg, NULL); 1826 } 1827 1828 static void geneve_setup(struct net_device *dev) 1829 { 1830 ether_setup(dev); 1831 1832 dev->netdev_ops = &geneve_netdev_ops; 1833 dev->ethtool_ops = &geneve_ethtool_ops; 1834 dev->needs_free_netdev = true; 1835 dev->priv_destructor = geneve_free_dev; 1836 1837 SET_NETDEV_DEVTYPE(dev, &geneve_type); 1838 1839 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST; 1840 dev->features |= NETIF_F_RXCSUM; 1841 dev->features |= NETIF_F_GSO_SOFTWARE; 1842 1843 /* Partial features are disabled by default. */ 1844 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST; 1845 dev->hw_features |= NETIF_F_RXCSUM; 1846 dev->hw_features |= NETIF_F_GSO_SOFTWARE; 1847 dev->hw_features |= UDP_TUNNEL_PARTIAL_FEATURES; 1848 dev->hw_features |= NETIF_F_GSO_PARTIAL; 1849 1850 dev->hw_enc_features = dev->hw_features; 1851 dev->gso_partial_features = UDP_TUNNEL_PARTIAL_FEATURES; 1852 dev->mangleid_features = NETIF_F_GSO_PARTIAL; 1853 1854 dev->pcpu_stat_type = NETDEV_PCPU_STAT_DSTATS; 1855 /* MTU range: 68 - (something less than 65535) */ 1856 dev->min_mtu = ETH_MIN_MTU; 1857 /* The max_mtu calculation does not take account of GENEVE 1858 * options, to avoid excluding potentially valid 1859 * configurations. This will be further reduced by IPvX hdr size. 1860 */ 1861 dev->max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len; 1862 1863 netif_keep_dst(dev); 1864 netif_set_tso_max_size(dev, GSO_MAX_SIZE); 1865 1866 dev->priv_flags &= ~IFF_TX_SKB_SHARING; 1867 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE; 1868 dev->lltx = true; 1869 eth_hw_addr_random(dev); 1870 } 1871 1872 static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = { 1873 [IFLA_GENEVE_UNSPEC] = { .strict_start_type = IFLA_GENEVE_INNER_PROTO_INHERIT }, 1874 [IFLA_GENEVE_ID] = { .type = NLA_U32 }, 1875 [IFLA_GENEVE_REMOTE] = { .len = sizeof_field(struct iphdr, daddr) }, 1876 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) }, 1877 [IFLA_GENEVE_TTL] = { .type = NLA_U8 }, 1878 [IFLA_GENEVE_TOS] = { .type = NLA_U8 }, 1879 [IFLA_GENEVE_LABEL] = { .type = NLA_U32 }, 1880 [IFLA_GENEVE_PORT] = { .type = NLA_U16 }, 1881 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG }, 1882 [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 }, 1883 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 }, 1884 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 }, 1885 [IFLA_GENEVE_TTL_INHERIT] = { .type = NLA_U8 }, 1886 [IFLA_GENEVE_DF] = { .type = NLA_U8 }, 1887 [IFLA_GENEVE_INNER_PROTO_INHERIT] = { .type = NLA_FLAG }, 1888 [IFLA_GENEVE_PORT_RANGE] = NLA_POLICY_EXACT_LEN(sizeof(struct ifla_geneve_port_range)), 1889 [IFLA_GENEVE_GRO_HINT] = { .type = NLA_FLAG }, 1890 [IFLA_GENEVE_LOCAL] = { .type = NLA_BE32 }, 1891 [IFLA_GENEVE_LOCAL6] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)), 1892 }; 1893 1894 static int geneve_validate(struct nlattr *tb[], struct nlattr *data[], 1895 struct netlink_ext_ack *extack) 1896 { 1897 if (tb[IFLA_ADDRESS]) { 1898 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) { 1899 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS], 1900 "Provided link layer address is not Ethernet"); 1901 return -EINVAL; 1902 } 1903 1904 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) { 1905 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS], 1906 "Provided Ethernet address is not unicast"); 1907 return -EADDRNOTAVAIL; 1908 } 1909 } 1910 1911 if (!data) { 1912 NL_SET_ERR_MSG(extack, 1913 "Not enough attributes provided to perform the operation"); 1914 return -EINVAL; 1915 } 1916 1917 if (data[IFLA_GENEVE_ID]) { 1918 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]); 1919 1920 if (vni >= GENEVE_N_VID) { 1921 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_ID], 1922 "Geneve ID must be lower than 16777216"); 1923 return -ERANGE; 1924 } 1925 } 1926 1927 if (data[IFLA_GENEVE_DF]) { 1928 enum ifla_geneve_df df = nla_get_u8(data[IFLA_GENEVE_DF]); 1929 1930 if (df < 0 || df > GENEVE_DF_MAX) { 1931 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_DF], 1932 "Invalid DF attribute"); 1933 return -EINVAL; 1934 } 1935 } 1936 1937 if (data[IFLA_GENEVE_PORT_RANGE]) { 1938 const struct ifla_geneve_port_range *p; 1939 1940 p = nla_data(data[IFLA_GENEVE_PORT_RANGE]); 1941 if (ntohs(p->high) < ntohs(p->low)) { 1942 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_PORT_RANGE], 1943 "Invalid source port range"); 1944 return -EINVAL; 1945 } 1946 } 1947 1948 return 0; 1949 } 1950 1951 static bool geneve_saddr_wildcard(const struct ip_tunnel_info *info) 1952 { 1953 if (ip_tunnel_info_af(info) == AF_INET) { 1954 if (!info->key.u.ipv4.src) 1955 return true; 1956 #if IS_ENABLED(CONFIG_IPV6) 1957 } else { 1958 if (ipv6_addr_any(&info->key.u.ipv6.src)) 1959 return true; 1960 #endif 1961 } 1962 1963 return false; 1964 } 1965 1966 static bool geneve_saddr_conflict(const struct ip_tunnel_info *a, 1967 const struct ip_tunnel_info *b) 1968 { 1969 if (ip_tunnel_info_af(a) != ip_tunnel_info_af(b)) 1970 return false; 1971 1972 if (geneve_saddr_wildcard(a) || geneve_saddr_wildcard(b)) 1973 return true; 1974 1975 if (ip_tunnel_info_af(a) == AF_INET) { 1976 if (a->key.u.ipv4.src == b->key.u.ipv4.src) 1977 return true; 1978 #if IS_ENABLED(CONFIG_IPV6) 1979 } else { 1980 if (ipv6_addr_equal(&a->key.u.ipv6.src, &b->key.u.ipv6.src)) 1981 return true; 1982 #endif 1983 } 1984 1985 return false; 1986 } 1987 1988 static struct geneve_dev *geneve_find_dev(struct geneve_net *gn, 1989 const struct geneve_config *cfg, 1990 const struct ip_tunnel_info *info, 1991 bool *tun_on_same_port, 1992 bool *tun_collect_md) 1993 { 1994 struct geneve_dev *geneve, *t = NULL; 1995 1996 *tun_on_same_port = false; 1997 *tun_collect_md = false; 1998 1999 mutex_lock(&gn->lock); 2000 2001 list_for_each_entry(geneve, &gn->geneve_list, next) { 2002 const struct geneve_config *gcfg = rtnl_dereference(geneve->cfg); 2003 2004 if (info->key.tp_dst == gcfg->info.key.tp_dst && 2005 (cfg->dualstack || gcfg->dualstack || 2006 geneve_saddr_conflict(info, &gcfg->info))) { 2007 *tun_collect_md |= gcfg->collect_md; 2008 *tun_on_same_port = true; 2009 } 2010 if (info->key.tun_id == gcfg->info.key.tun_id && 2011 info->key.tp_dst == gcfg->info.key.tp_dst && 2012 !memcmp(&info->key.u, &gcfg->info.key.u, sizeof(info->key.u))) 2013 t = geneve; 2014 } 2015 2016 mutex_unlock(&gn->lock); 2017 2018 return t; 2019 } 2020 2021 static bool is_tnl_info_zero(const struct ip_tunnel_info *info) 2022 { 2023 return !(info->key.tun_id || info->key.tos || 2024 !ip_tunnel_flags_empty(info->key.tun_flags) || 2025 info->key.ttl || info->key.label || info->key.tp_src || 2026 #if IS_ENABLED(CONFIG_IPV6) 2027 (ip_tunnel_info_af(info) == AF_INET6 && 2028 !ipv6_addr_any(&info->key.u.ipv6.dst)) || 2029 #endif 2030 (ip_tunnel_info_af(info) == AF_INET && 2031 info->key.u.ipv4.dst)); 2032 } 2033 2034 static bool geneve_dst_addr_equal(struct ip_tunnel_info *a, 2035 struct ip_tunnel_info *b) 2036 { 2037 if (ip_tunnel_info_af(a) == AF_INET) 2038 return a->key.u.ipv4.dst == b->key.u.ipv4.dst; 2039 else 2040 return ipv6_addr_equal(&a->key.u.ipv6.dst, &b->key.u.ipv6.dst); 2041 } 2042 2043 static int geneve_configure(struct net *net, struct net_device *dev, 2044 struct netlink_ext_ack *extack, 2045 const struct geneve_config *cfg) 2046 { 2047 struct geneve_net *gn = net_generic(net, geneve_net_id); 2048 struct geneve_dev *t, *geneve = netdev_priv(dev); 2049 const struct ip_tunnel_info *info = &cfg->info; 2050 bool tun_collect_md, tun_on_same_port; 2051 struct geneve_config *new_cfg; 2052 int err, encap_len; 2053 2054 if (cfg->collect_md && !is_tnl_info_zero(info)) { 2055 NL_SET_ERR_MSG(extack, 2056 "Device is externally controlled, so attributes (VNI, Port, and so on) must not be specified"); 2057 return -EINVAL; 2058 } 2059 2060 geneve->net = net; 2061 geneve->dev = dev; 2062 2063 t = geneve_find_dev(gn, cfg, info, &tun_on_same_port, &tun_collect_md); 2064 if (t) 2065 return -EBUSY; 2066 2067 /* make enough headroom for basic scenario */ 2068 encap_len = GENEVE_BASE_HLEN + ETH_HLEN; 2069 if (!cfg->collect_md && ip_tunnel_info_af(info) == AF_INET) { 2070 encap_len += sizeof(struct iphdr); 2071 dev->max_mtu -= sizeof(struct iphdr); 2072 } else { 2073 encap_len += sizeof(struct ipv6hdr); 2074 dev->max_mtu -= sizeof(struct ipv6hdr); 2075 } 2076 dev->needed_headroom = encap_len + ETH_HLEN; 2077 2078 if (cfg->collect_md) { 2079 if (tun_on_same_port) { 2080 NL_SET_ERR_MSG(extack, 2081 "There can be only one externally controlled device on a destination port and a source address"); 2082 return -EPERM; 2083 } 2084 } else { 2085 if (tun_collect_md) { 2086 NL_SET_ERR_MSG(extack, 2087 "There already exists an externally controlled device on this destination port and the source address"); 2088 return -EPERM; 2089 } 2090 } 2091 2092 new_cfg = geneve_config_alloc(cfg); 2093 if (IS_ERR(new_cfg)) 2094 return PTR_ERR(new_cfg); 2095 2096 rcu_assign_pointer(geneve->cfg, new_cfg); 2097 2098 if (cfg->inner_proto_inherit) { 2099 dev->header_ops = NULL; 2100 dev->type = ARPHRD_NONE; 2101 dev->hard_header_len = 0; 2102 dev->addr_len = 0; 2103 dev->flags = IFF_POINTOPOINT | IFF_NOARP; 2104 } 2105 2106 err = register_netdevice(dev); 2107 if (err) { 2108 geneve_free_dev(dev); 2109 return err; 2110 } 2111 2112 mutex_lock(&gn->lock); 2113 list_add(&geneve->next, &gn->geneve_list); 2114 mutex_unlock(&gn->lock); 2115 2116 return 0; 2117 } 2118 2119 static void init_tnl_info(struct ip_tunnel_info *info, __u16 dst_port) 2120 { 2121 memset(info, 0, sizeof(*info)); 2122 info->key.tp_dst = htons(dst_port); 2123 } 2124 2125 static int geneve_nl2info(struct nlattr *tb[], struct nlattr *data[], 2126 struct netlink_ext_ack *extack, 2127 struct geneve_config *cfg, bool changelink) 2128 { 2129 struct ip_tunnel_info *info = &cfg->info; 2130 int attrtype; 2131 2132 if (data[IFLA_GENEVE_COLLECT_METADATA]) { 2133 if (changelink) { 2134 attrtype = IFLA_GENEVE_COLLECT_METADATA; 2135 goto change_notsup; 2136 } 2137 2138 cfg->collect_md = true; 2139 cfg->dualstack = true; 2140 } 2141 2142 if ((data[IFLA_GENEVE_LOCAL] || data[IFLA_GENEVE_REMOTE]) && 2143 (data[IFLA_GENEVE_LOCAL6] || data[IFLA_GENEVE_REMOTE6])) { 2144 NL_SET_ERR_MSG(extack, 2145 "Cannot specify both IPv4/IPv6 Remote/Local addresses"); 2146 return -EINVAL; 2147 } 2148 2149 if (data[IFLA_GENEVE_REMOTE]) { 2150 if (changelink && (ip_tunnel_info_af(info) == AF_INET6)) { 2151 attrtype = IFLA_GENEVE_REMOTE; 2152 goto change_notsup; 2153 } 2154 2155 info->key.u.ipv4.dst = 2156 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]); 2157 2158 if (ipv4_is_multicast(info->key.u.ipv4.dst)) { 2159 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE], 2160 "Remote IPv4 address cannot be Multicast"); 2161 return -EINVAL; 2162 } 2163 } 2164 2165 if (data[IFLA_GENEVE_REMOTE6]) { 2166 #if IS_ENABLED(CONFIG_IPV6) 2167 int addr_type; 2168 2169 if (changelink && (ip_tunnel_info_af(info) == AF_INET)) { 2170 attrtype = IFLA_GENEVE_REMOTE6; 2171 goto change_notsup; 2172 } 2173 2174 info->mode = IP_TUNNEL_INFO_IPV6; 2175 info->key.u.ipv6.dst = 2176 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]); 2177 2178 addr_type = ipv6_addr_type(&info->key.u.ipv6.dst); 2179 if (addr_type & IPV6_ADDR_LINKLOCAL) { 2180 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6], 2181 "Remote IPv6 address cannot be link-local"); 2182 return -EINVAL; 2183 } 2184 if (addr_type & IPV6_ADDR_MULTICAST) { 2185 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6], 2186 "Remote IPv6 address cannot be Multicast"); 2187 return -EINVAL; 2188 } 2189 __set_bit(IP_TUNNEL_CSUM_BIT, info->key.tun_flags); 2190 cfg->use_udp6_rx_checksums = true; 2191 #else 2192 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6], 2193 "IPv6 support not enabled in the kernel"); 2194 return -EPFNOSUPPORT; 2195 #endif 2196 } 2197 2198 if (data[IFLA_GENEVE_LOCAL]) { 2199 if (changelink) { 2200 __be32 src = nla_get_in_addr(data[IFLA_GENEVE_LOCAL]); 2201 2202 if (ip_tunnel_info_af(info) == AF_INET6 || 2203 src != info->key.u.ipv4.src) { 2204 attrtype = IFLA_GENEVE_LOCAL; 2205 goto change_notsup; 2206 } 2207 } else { 2208 info->key.u.ipv4.src = nla_get_in_addr(data[IFLA_GENEVE_LOCAL]); 2209 2210 if (ipv4_is_multicast(info->key.u.ipv4.src)) { 2211 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_LOCAL], 2212 "Local IPv4 address cannot be Multicast"); 2213 return -EINVAL; 2214 } 2215 2216 cfg->dualstack = false; 2217 } 2218 } 2219 2220 if (data[IFLA_GENEVE_LOCAL6]) { 2221 #if IS_ENABLED(CONFIG_IPV6) 2222 if (changelink) { 2223 struct in6_addr src = nla_get_in6_addr(data[IFLA_GENEVE_LOCAL6]); 2224 2225 if (ip_tunnel_info_af(info) == AF_INET || 2226 !ipv6_addr_equal(&src, &info->key.u.ipv6.src)) { 2227 attrtype = IFLA_GENEVE_LOCAL6; 2228 goto change_notsup; 2229 } 2230 } else { 2231 int addr_type; 2232 2233 info->mode = IP_TUNNEL_INFO_IPV6; 2234 info->key.u.ipv6.src = nla_get_in6_addr(data[IFLA_GENEVE_LOCAL6]); 2235 2236 addr_type = ipv6_addr_type(&info->key.u.ipv6.src); 2237 if (addr_type & IPV6_ADDR_LINKLOCAL) { 2238 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_LOCAL6], 2239 "Local IPv6 address cannot be link-local"); 2240 return -EINVAL; 2241 } 2242 if (addr_type & IPV6_ADDR_MULTICAST) { 2243 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_LOCAL6], 2244 "Local IPv6 address cannot be Multicast"); 2245 return -EINVAL; 2246 } 2247 2248 cfg->dualstack = false; 2249 } 2250 #else 2251 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_LOCAL6], 2252 "IPv6 support not enabled in the kernel"); 2253 return -EPFNOSUPPORT; 2254 #endif 2255 } 2256 2257 if (data[IFLA_GENEVE_ID]) { 2258 __u32 vni; 2259 __u8 tvni[3]; 2260 __be64 tunid; 2261 2262 vni = nla_get_u32(data[IFLA_GENEVE_ID]); 2263 tvni[0] = (vni & 0x00ff0000) >> 16; 2264 tvni[1] = (vni & 0x0000ff00) >> 8; 2265 tvni[2] = vni & 0x000000ff; 2266 2267 tunid = vni_to_tunnel_id(tvni); 2268 if (changelink && (tunid != info->key.tun_id)) { 2269 attrtype = IFLA_GENEVE_ID; 2270 goto change_notsup; 2271 } 2272 info->key.tun_id = tunid; 2273 } 2274 2275 if (data[IFLA_GENEVE_TTL_INHERIT]) { 2276 if (nla_get_u8(data[IFLA_GENEVE_TTL_INHERIT])) 2277 cfg->ttl_inherit = true; 2278 else 2279 cfg->ttl_inherit = false; 2280 } else if (data[IFLA_GENEVE_TTL]) { 2281 info->key.ttl = nla_get_u8(data[IFLA_GENEVE_TTL]); 2282 cfg->ttl_inherit = false; 2283 } 2284 2285 if (data[IFLA_GENEVE_TOS]) 2286 info->key.tos = nla_get_u8(data[IFLA_GENEVE_TOS]); 2287 2288 if (data[IFLA_GENEVE_DF]) 2289 cfg->df = nla_get_u8(data[IFLA_GENEVE_DF]); 2290 2291 if (data[IFLA_GENEVE_LABEL]) { 2292 info->key.label = nla_get_be32(data[IFLA_GENEVE_LABEL]) & 2293 IPV6_FLOWLABEL_MASK; 2294 if (info->key.label && (!(info->mode & IP_TUNNEL_INFO_IPV6))) { 2295 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_LABEL], 2296 "Label attribute only applies for IPv6 Geneve devices"); 2297 return -EINVAL; 2298 } 2299 } 2300 2301 if (data[IFLA_GENEVE_PORT]) { 2302 if (changelink) { 2303 attrtype = IFLA_GENEVE_PORT; 2304 goto change_notsup; 2305 } 2306 info->key.tp_dst = nla_get_be16(data[IFLA_GENEVE_PORT]); 2307 } 2308 2309 if (data[IFLA_GENEVE_PORT_RANGE]) { 2310 const struct ifla_geneve_port_range *p; 2311 2312 if (changelink) { 2313 attrtype = IFLA_GENEVE_PORT_RANGE; 2314 goto change_notsup; 2315 } 2316 p = nla_data(data[IFLA_GENEVE_PORT_RANGE]); 2317 cfg->port_min = ntohs(p->low); 2318 cfg->port_max = ntohs(p->high); 2319 } 2320 2321 if (data[IFLA_GENEVE_UDP_CSUM]) { 2322 if (changelink) { 2323 attrtype = IFLA_GENEVE_UDP_CSUM; 2324 goto change_notsup; 2325 } 2326 if (nla_get_u8(data[IFLA_GENEVE_UDP_CSUM])) 2327 __set_bit(IP_TUNNEL_CSUM_BIT, info->key.tun_flags); 2328 } 2329 2330 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]) { 2331 #if IS_ENABLED(CONFIG_IPV6) 2332 if (changelink) { 2333 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_TX; 2334 goto change_notsup; 2335 } 2336 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX])) 2337 __clear_bit(IP_TUNNEL_CSUM_BIT, info->key.tun_flags); 2338 #else 2339 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX], 2340 "IPv6 support not enabled in the kernel"); 2341 return -EPFNOSUPPORT; 2342 #endif 2343 } 2344 2345 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]) { 2346 #if IS_ENABLED(CONFIG_IPV6) 2347 if (changelink) { 2348 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_RX; 2349 goto change_notsup; 2350 } 2351 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX])) 2352 cfg->use_udp6_rx_checksums = false; 2353 #else 2354 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX], 2355 "IPv6 support not enabled in the kernel"); 2356 return -EPFNOSUPPORT; 2357 #endif 2358 } 2359 2360 if (data[IFLA_GENEVE_INNER_PROTO_INHERIT]) { 2361 if (changelink) { 2362 attrtype = IFLA_GENEVE_INNER_PROTO_INHERIT; 2363 goto change_notsup; 2364 } 2365 cfg->inner_proto_inherit = true; 2366 } 2367 2368 if (data[IFLA_GENEVE_GRO_HINT]) { 2369 if (changelink) { 2370 attrtype = IFLA_GENEVE_GRO_HINT; 2371 goto change_notsup; 2372 } 2373 cfg->gro_hint = true; 2374 } 2375 2376 return 0; 2377 change_notsup: 2378 NL_SET_ERR_MSG_ATTR(extack, data[attrtype], 2379 "Changing VNI, Port, endpoint IP address family, external, inner_proto_inherit, gro_hint and UDP checksum attributes are not supported"); 2380 return -EOPNOTSUPP; 2381 } 2382 2383 static void geneve_link_config(struct net_device *dev, 2384 struct ip_tunnel_info *info, struct nlattr *tb[]) 2385 { 2386 struct geneve_dev *geneve = netdev_priv(dev); 2387 int ldev_mtu = 0; 2388 2389 if (tb[IFLA_MTU]) { 2390 geneve_change_mtu(dev, nla_get_u32(tb[IFLA_MTU])); 2391 return; 2392 } 2393 2394 switch (ip_tunnel_info_af(info)) { 2395 case AF_INET: { 2396 struct flowi4 fl4 = { .daddr = info->key.u.ipv4.dst }; 2397 struct rtable *rt = ip_route_output_key(geneve->net, &fl4); 2398 2399 if (!IS_ERR(rt) && rt->dst.dev) { 2400 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV4_HLEN; 2401 ip_rt_put(rt); 2402 } 2403 break; 2404 } 2405 #if IS_ENABLED(CONFIG_IPV6) 2406 case AF_INET6: { 2407 struct rt6_info *rt; 2408 2409 if (!__in6_dev_get(dev)) 2410 break; 2411 2412 rt = rt6_lookup(geneve->net, &info->key.u.ipv6.dst, NULL, 0, 2413 NULL, 0); 2414 2415 if (rt && rt->dst.dev) 2416 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV6_HLEN; 2417 ip6_rt_put(rt); 2418 break; 2419 } 2420 #endif 2421 } 2422 2423 if (ldev_mtu <= 0) 2424 return; 2425 2426 geneve_change_mtu(dev, ldev_mtu - info->options_len); 2427 } 2428 2429 static int geneve_newlink(struct net_device *dev, 2430 struct rtnl_newlink_params *params, 2431 struct netlink_ext_ack *extack) 2432 { 2433 struct net *link_net = rtnl_newlink_link_net(params); 2434 struct nlattr **data = params->data; 2435 struct nlattr **tb = params->tb; 2436 struct geneve_config cfg = { 2437 .df = GENEVE_DF_UNSET, 2438 .use_udp6_rx_checksums = false, 2439 .ttl_inherit = false, 2440 .collect_md = false, 2441 .dualstack = false, 2442 .port_min = 1, 2443 .port_max = USHRT_MAX, 2444 }; 2445 int err; 2446 2447 init_tnl_info(&cfg.info, GENEVE_UDP_PORT); 2448 err = geneve_nl2info(tb, data, extack, &cfg, false); 2449 if (err) 2450 return err; 2451 2452 err = geneve_configure(link_net, dev, extack, &cfg); 2453 if (err) 2454 return err; 2455 2456 geneve_link_config(dev, &cfg.info, tb); 2457 2458 return 0; 2459 } 2460 2461 /* Update the device configuration under RTNL. 2462 * We use RCU swap to update the configuration atomically, so the data path 2463 * (both TX and RX) can continue running without interruption or packet loss. 2464 */ 2465 static int geneve_changelink(struct net_device *dev, struct nlattr *tb[], 2466 struct nlattr *data[], 2467 struct netlink_ext_ack *extack) 2468 { 2469 struct geneve_dev *geneve = netdev_priv(dev); 2470 struct geneve_config *old_cfg = rtnl_dereference(geneve->cfg); 2471 struct geneve_config *cfg; 2472 int err; 2473 2474 if (!rtnl_dev_link_net_capable(dev, geneve->net)) 2475 return -EPERM; 2476 2477 /* If the geneve device is configured for metadata (or externally 2478 * controlled, for example, OVS), then nothing can be changed. 2479 */ 2480 if (old_cfg->collect_md) 2481 return -EOPNOTSUPP; 2482 2483 /* Start with the existing info. */ 2484 cfg = geneve_config_alloc(old_cfg); 2485 if (IS_ERR(cfg)) 2486 return PTR_ERR(cfg); 2487 2488 err = geneve_nl2info(tb, data, extack, cfg, true); 2489 if (err) 2490 goto err_free_cfg; 2491 2492 if (!geneve_dst_addr_equal(&old_cfg->info, &cfg->info)) 2493 geneve_link_config(dev, &cfg->info, tb); 2494 2495 rcu_assign_pointer(geneve->cfg, cfg); 2496 2497 call_rcu_hurry(&old_cfg->rcu, geneve_config_free_rcu); 2498 return 0; 2499 2500 err_free_cfg: 2501 geneve_config_free(cfg); 2502 return err; 2503 } 2504 2505 static void __geneve_dellink(struct net *net, struct net_device *dev, 2506 struct list_head *head) 2507 { 2508 struct geneve_dev *geneve = netdev_priv(dev); 2509 2510 list_del_init(&geneve->next); 2511 unregister_netdevice_queue_net(net, dev, head); 2512 } 2513 2514 static void geneve_dellink(struct net_device *dev, struct list_head *head) 2515 { 2516 struct geneve_dev *geneve = netdev_priv(dev); 2517 struct geneve_net *gn; 2518 2519 gn = net_generic(geneve->net, geneve_net_id); 2520 2521 mutex_lock(&gn->lock); 2522 if (!list_empty(&geneve->next)) 2523 __geneve_dellink(dev_net(dev), dev, head); 2524 mutex_unlock(&gn->lock); 2525 } 2526 2527 static size_t geneve_get_size(const struct net_device *dev) 2528 { 2529 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */ 2530 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */ 2531 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_LOCAL{6} */ 2532 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */ 2533 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */ 2534 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_DF */ 2535 nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */ 2536 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */ 2537 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */ 2538 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */ 2539 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */ 2540 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */ 2541 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL_INHERIT */ 2542 nla_total_size(0) + /* IFLA_GENEVE_INNER_PROTO_INHERIT */ 2543 nla_total_size(sizeof(struct ifla_geneve_port_range)) + /* IFLA_GENEVE_PORT_RANGE */ 2544 nla_total_size(0) + /* IFLA_GENEVE_GRO_HINT */ 2545 0; 2546 } 2547 2548 static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev) 2549 { 2550 const struct geneve_dev *geneve = netdev_priv(dev); 2551 struct ifla_geneve_port_range ports; 2552 const struct geneve_config *cfg; 2553 const struct ip_tunnel_info *info; 2554 bool ttl_inherit, metadata; 2555 __u8 tmp_vni[3]; 2556 __u32 vni; 2557 int err = 0; 2558 2559 rcu_read_lock(); 2560 cfg = rcu_dereference(geneve->cfg); 2561 if (!cfg) { 2562 err = -ENODEV; 2563 goto out; 2564 } 2565 2566 info = &cfg->info; 2567 ttl_inherit = cfg->ttl_inherit; 2568 metadata = cfg->collect_md; 2569 ports.low = htons(cfg->port_min); 2570 ports.high = htons(cfg->port_max); 2571 2572 tunnel_id_to_vni(info->key.tun_id, tmp_vni); 2573 vni = (tmp_vni[0] << 16) | (tmp_vni[1] << 8) | tmp_vni[2]; 2574 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni)) 2575 goto nla_put_failure; 2576 2577 if (!metadata && ip_tunnel_info_af(info) == AF_INET) { 2578 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE, 2579 info->key.u.ipv4.dst)) 2580 goto nla_put_failure; 2581 if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM, 2582 test_bit(IP_TUNNEL_CSUM_BIT, 2583 info->key.tun_flags))) 2584 goto nla_put_failure; 2585 2586 #if IS_ENABLED(CONFIG_IPV6) 2587 } else if (!metadata) { 2588 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6, 2589 &info->key.u.ipv6.dst)) 2590 goto nla_put_failure; 2591 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX, 2592 !test_bit(IP_TUNNEL_CSUM_BIT, 2593 info->key.tun_flags))) 2594 goto nla_put_failure; 2595 #endif 2596 } 2597 2598 if (!cfg->dualstack) { 2599 if (ip_tunnel_info_af(info) == AF_INET) { 2600 if ((info->key.u.ipv4.src || 2601 metadata) && 2602 nla_put_in_addr(skb, IFLA_GENEVE_LOCAL, 2603 info->key.u.ipv4.src)) 2604 goto nla_put_failure; 2605 #if IS_ENABLED(CONFIG_IPV6) 2606 } else { 2607 if ((!ipv6_addr_any(&info->key.u.ipv6.src) || 2608 metadata) && 2609 nla_put_in6_addr(skb, IFLA_GENEVE_LOCAL6, 2610 &info->key.u.ipv6.src)) 2611 goto nla_put_failure; 2612 #endif 2613 } 2614 } 2615 2616 if (nla_put_u8(skb, IFLA_GENEVE_TTL, info->key.ttl) || 2617 nla_put_u8(skb, IFLA_GENEVE_TOS, info->key.tos) || 2618 nla_put_be32(skb, IFLA_GENEVE_LABEL, info->key.label)) 2619 goto nla_put_failure; 2620 2621 if (nla_put_u8(skb, IFLA_GENEVE_DF, cfg->df)) 2622 goto nla_put_failure; 2623 2624 if (nla_put_be16(skb, IFLA_GENEVE_PORT, info->key.tp_dst)) 2625 goto nla_put_failure; 2626 2627 if (metadata && nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA)) 2628 goto nla_put_failure; 2629 2630 #if IS_ENABLED(CONFIG_IPV6) 2631 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX, 2632 !cfg->use_udp6_rx_checksums)) 2633 goto nla_put_failure; 2634 #endif 2635 2636 if (nla_put_u8(skb, IFLA_GENEVE_TTL_INHERIT, ttl_inherit)) 2637 goto nla_put_failure; 2638 2639 if (cfg->inner_proto_inherit && 2640 nla_put_flag(skb, IFLA_GENEVE_INNER_PROTO_INHERIT)) 2641 goto nla_put_failure; 2642 2643 if (nla_put(skb, IFLA_GENEVE_PORT_RANGE, sizeof(ports), &ports)) 2644 goto nla_put_failure; 2645 2646 if (cfg->gro_hint && 2647 nla_put_flag(skb, IFLA_GENEVE_GRO_HINT)) 2648 goto nla_put_failure; 2649 2650 out: 2651 rcu_read_unlock(); 2652 return err; 2653 2654 nla_put_failure: 2655 err = -EMSGSIZE; 2656 goto out; 2657 } 2658 2659 static struct rtnl_link_ops geneve_link_ops __read_mostly = { 2660 .kind = "geneve", 2661 .maxtype = IFLA_GENEVE_MAX, 2662 .policy = geneve_policy, 2663 .priv_size = sizeof(struct geneve_dev), 2664 .setup = geneve_setup, 2665 .validate = geneve_validate, 2666 .newlink = geneve_newlink, 2667 .changelink = geneve_changelink, 2668 .dellink = geneve_dellink, 2669 .get_size = geneve_get_size, 2670 .fill_info = geneve_fill_info, 2671 }; 2672 2673 static int geneve_netdevice_event(struct notifier_block *unused, 2674 unsigned long event, void *ptr) 2675 { 2676 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 2677 2678 if (event == NETDEV_UDP_TUNNEL_PUSH_INFO) 2679 geneve_offload_rx_ports(dev, true); 2680 else if (event == NETDEV_UDP_TUNNEL_DROP_INFO) 2681 geneve_offload_rx_ports(dev, false); 2682 2683 return NOTIFY_DONE; 2684 } 2685 2686 static struct notifier_block geneve_notifier_block __read_mostly = { 2687 .notifier_call = geneve_netdevice_event, 2688 }; 2689 2690 static __net_init int geneve_init_net(struct net *net) 2691 { 2692 struct geneve_net *gn = net_generic(net, geneve_net_id); 2693 2694 INIT_LIST_HEAD(&gn->geneve_list); 2695 INIT_LIST_HEAD(&gn->sock_list); 2696 mutex_init(&gn->lock); 2697 2698 return 0; 2699 } 2700 2701 static void __net_exit geneve_exit_rtnl_net(struct net *net, 2702 struct list_head *dev_to_kill) 2703 { 2704 struct geneve_net *gn = net_generic(net, geneve_net_id); 2705 struct geneve_dev *geneve, *next; 2706 2707 mutex_lock(&gn->lock); 2708 2709 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) 2710 __geneve_dellink(net, geneve->dev, dev_to_kill); 2711 2712 mutex_unlock(&gn->lock); 2713 } 2714 2715 static void __net_exit geneve_exit_net(struct net *net) 2716 { 2717 const struct geneve_net *gn = net_generic(net, geneve_net_id); 2718 2719 WARN_ON_ONCE(!list_empty(&gn->sock_list)); 2720 } 2721 2722 static struct pernet_operations geneve_net_ops = { 2723 .init = geneve_init_net, 2724 .exit_rtnl = geneve_exit_rtnl_net, 2725 .exit = geneve_exit_net, 2726 .id = &geneve_net_id, 2727 .size = sizeof(struct geneve_net), 2728 }; 2729 2730 static int __init geneve_init_module(void) 2731 { 2732 int rc; 2733 2734 rc = register_pernet_subsys(&geneve_net_ops); 2735 if (rc) 2736 goto out1; 2737 2738 rc = register_netdevice_notifier(&geneve_notifier_block); 2739 if (rc) 2740 goto out2; 2741 2742 rc = rtnl_link_register(&geneve_link_ops); 2743 if (rc) 2744 goto out3; 2745 2746 return 0; 2747 out3: 2748 unregister_netdevice_notifier(&geneve_notifier_block); 2749 out2: 2750 unregister_pernet_subsys(&geneve_net_ops); 2751 out1: 2752 return rc; 2753 } 2754 late_initcall(geneve_init_module); 2755 2756 static void __exit geneve_cleanup_module(void) 2757 { 2758 rtnl_link_unregister(&geneve_link_ops); 2759 unregister_netdevice_notifier(&geneve_notifier_block); 2760 unregister_pernet_subsys(&geneve_net_ops); 2761 rcu_barrier(); 2762 } 2763 module_exit(geneve_cleanup_module); 2764 2765 MODULE_LICENSE("GPL"); 2766 MODULE_VERSION(GENEVE_NETDEV_VER); 2767 MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>"); 2768 MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic"); 2769 MODULE_ALIAS_RTNL_LINK("geneve"); 2770