1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * VXLAN: Virtual eXtensible Local Area Network 4 * 5 * Copyright (c) 2012-2013 Vyatta Inc. 6 */ 7 8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 #include <linux/errno.h> 13 #include <linux/slab.h> 14 #include <linux/udp.h> 15 #include <linux/igmp.h> 16 #include <linux/if_ether.h> 17 #include <linux/ethtool.h> 18 #include <linux/rhashtable.h> 19 #include <net/arp.h> 20 #include <net/ndisc.h> 21 #include <net/gro.h> 22 #include <net/ip.h> 23 #include <net/icmp.h> 24 #include <net/rtnetlink.h> 25 #include <net/inet_ecn.h> 26 #include <net/net_namespace.h> 27 #include <net/netns/generic.h> 28 #include <net/netdev_lock.h> 29 #include <net/tun_proto.h> 30 #include <net/vxlan.h> 31 #include <net/nexthop.h> 32 33 #if IS_ENABLED(CONFIG_IPV6) 34 #include <net/ip6_tunnel.h> 35 #include <net/ip6_checksum.h> 36 #endif 37 38 #include "vxlan_private.h" 39 40 #define VXLAN_VERSION "0.1" 41 42 #define FDB_AGE_DEFAULT 300 /* 5 min */ 43 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */ 44 45 /* UDP port for VXLAN traffic. 46 * The IANA assigned port is 4789, but the Linux default is 8472 47 * for compatibility with early adopters. 48 */ 49 static unsigned short vxlan_port __read_mostly = 8472; 50 module_param_named(udp_port, vxlan_port, ushort, 0444); 51 MODULE_PARM_DESC(udp_port, "Destination UDP port"); 52 53 static bool log_ecn_error = true; 54 module_param(log_ecn_error, bool, 0644); 55 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); 56 57 unsigned int vxlan_net_id; 58 59 const u8 all_zeros_mac[ETH_ALEN + 2]; 60 static struct rtnl_link_ops vxlan_link_ops; 61 62 static int vxlan_sock_add(struct vxlan_dev *vxlan); 63 64 static void vxlan_vs_del_dev(struct vxlan_dev *vxlan); 65 66 static const struct rhashtable_params vxlan_fdb_rht_params = { 67 .head_offset = offsetof(struct vxlan_fdb, rhnode), 68 .key_offset = offsetof(struct vxlan_fdb, key), 69 .key_len = sizeof(struct vxlan_fdb_key), 70 .automatic_shrinking = true, 71 }; 72 73 static inline bool vxlan_collect_metadata(struct vxlan_sock *vs) 74 { 75 return vs->flags & VXLAN_F_COLLECT_METADATA || 76 ip_tunnel_collect_metadata(); 77 } 78 79 /* Find VXLAN socket based on network namespace, address family, UDP port, 80 * enabled unshareable flags and socket device binding (see l3mdev with 81 * non-default VRF). 82 */ 83 static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family, 84 __be16 port, u32 flags, int ifindex) 85 { 86 struct vxlan_sock *vs; 87 88 flags &= VXLAN_F_RCV_FLAGS; 89 90 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) { 91 if (inet_sk(vs->sk)->inet_sport == port && 92 vxlan_get_sk_family(vs) == family && 93 vs->flags == flags && 94 vs->sk->sk_bound_dev_if == ifindex) 95 return vs; 96 } 97 return NULL; 98 } 99 100 static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, 101 int ifindex, __be32 vni, 102 struct vxlan_vni_node **vninode) 103 { 104 struct vxlan_vni_node *vnode; 105 struct vxlan_dev_node *node; 106 107 /* For flow based devices, map all packets to VNI 0 */ 108 if (vs->flags & VXLAN_F_COLLECT_METADATA && 109 !(vs->flags & VXLAN_F_VNIFILTER)) 110 vni = 0; 111 112 hlist_for_each_entry_rcu(node, vni_head(vs, vni), hlist) { 113 const struct vxlan_config *cfg; 114 115 if (!node->vxlan) 116 continue; 117 118 cfg = rcu_dereference(node->vxlan->cfg); 119 120 vnode = NULL; 121 if (cfg->flags & VXLAN_F_VNIFILTER) { 122 vnode = vxlan_vnifilter_lookup(node->vxlan, vni); 123 if (!vnode) 124 continue; 125 } else if (cfg->vni != vni) { 126 continue; 127 } 128 129 if (IS_ENABLED(CONFIG_IPV6)) { 130 if ((cfg->flags & VXLAN_F_IPV6_LINKLOCAL) && 131 cfg->remote_ifindex != ifindex) 132 continue; 133 } 134 135 if (vninode) 136 *vninode = vnode; 137 return node->vxlan; 138 } 139 140 return NULL; 141 } 142 143 /* Look up VNI in a per net namespace table */ 144 static struct vxlan_dev *vxlan_find_vni(struct net *net, int ifindex, 145 __be32 vni, sa_family_t family, 146 __be16 port, u32 flags) 147 { 148 struct vxlan_sock *vs; 149 150 vs = vxlan_find_sock(net, family, port, flags, ifindex); 151 if (!vs) 152 return NULL; 153 154 return vxlan_vs_find_vni(vs, ifindex, vni, NULL); 155 } 156 157 /* Fill in neighbour message in skbuff. */ 158 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan, 159 const struct vxlan_fdb *fdb, 160 u32 portid, u32 seq, int type, unsigned int flags, 161 const struct vxlan_rdst *rdst) 162 { 163 const struct vxlan_config *cfg = rcu_dereference_rtnl(vxlan->cfg); 164 unsigned long now = jiffies; 165 struct nda_cacheinfo ci; 166 bool send_ip, send_eth; 167 struct nlmsghdr *nlh; 168 struct nexthop *nh; 169 struct ndmsg *ndm; 170 int nh_family; 171 u32 nh_id; 172 173 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags); 174 if (nlh == NULL) 175 return -EMSGSIZE; 176 177 ndm = nlmsg_data(nlh); 178 memset(ndm, 0, sizeof(*ndm)); 179 180 send_eth = send_ip = true; 181 182 rcu_read_lock(); 183 nh = rcu_dereference(fdb->nh); 184 if (nh) { 185 nh_family = nexthop_get_family(nh); 186 nh_id = nh->id; 187 } 188 rcu_read_unlock(); 189 190 if (type == RTM_GETNEIGH) { 191 if (rdst) { 192 send_ip = !vxlan_addr_any(&rdst->remote_ip); 193 ndm->ndm_family = send_ip ? rdst->remote_ip.sa.sa_family : AF_INET; 194 } else if (nh) { 195 ndm->ndm_family = nh_family; 196 } 197 send_eth = !is_zero_ether_addr(fdb->key.eth_addr); 198 } else 199 ndm->ndm_family = AF_BRIDGE; 200 ndm->ndm_state = fdb->state; 201 ndm->ndm_ifindex = vxlan->dev->ifindex; 202 ndm->ndm_flags = fdb->flags; 203 if (rdst && rdst->offloaded) 204 ndm->ndm_flags |= NTF_OFFLOADED; 205 ndm->ndm_type = RTN_UNICAST; 206 207 if (!net_eq(dev_net(vxlan->dev), vxlan->net) && 208 nla_put_s32(skb, NDA_LINK_NETNSID, 209 peernet2id(dev_net(vxlan->dev), vxlan->net))) 210 goto nla_put_failure; 211 212 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->key.eth_addr)) 213 goto nla_put_failure; 214 if (nh) { 215 if (nla_put_u32(skb, NDA_NH_ID, nh_id)) 216 goto nla_put_failure; 217 } else if (rdst) { 218 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, 219 &rdst->remote_ip)) 220 goto nla_put_failure; 221 222 if (rdst->remote_port && 223 rdst->remote_port != cfg->dst_port && 224 nla_put_be16(skb, NDA_PORT, rdst->remote_port)) 225 goto nla_put_failure; 226 if (rdst->remote_vni != cfg->vni && 227 nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni))) 228 goto nla_put_failure; 229 if (rdst->remote_ifindex && 230 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex)) 231 goto nla_put_failure; 232 } 233 234 if ((cfg->flags & VXLAN_F_COLLECT_METADATA) && fdb->key.vni && 235 nla_put_u32(skb, NDA_SRC_VNI, 236 be32_to_cpu(fdb->key.vni))) 237 goto nla_put_failure; 238 239 ci.ndm_used = jiffies_to_clock_t(now - READ_ONCE(fdb->used)); 240 ci.ndm_confirmed = 0; 241 ci.ndm_updated = jiffies_to_clock_t(now - READ_ONCE(fdb->updated)); 242 ci.ndm_refcnt = 0; 243 244 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci)) 245 goto nla_put_failure; 246 247 nlmsg_end(skb, nlh); 248 return 0; 249 250 nla_put_failure: 251 nlmsg_cancel(skb, nlh); 252 return -EMSGSIZE; 253 } 254 255 static inline size_t vxlan_nlmsg_size(void) 256 { 257 return NLMSG_ALIGN(sizeof(struct ndmsg)) 258 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */ 259 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */ 260 + nla_total_size(sizeof(__be16)) /* NDA_PORT */ 261 + nla_total_size(sizeof(__be32)) /* NDA_VNI */ 262 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */ 263 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */ 264 + nla_total_size(sizeof(struct nda_cacheinfo)); 265 } 266 267 static void __vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb, 268 struct vxlan_rdst *rd, int type) 269 { 270 struct net *net = dev_net(vxlan->dev); 271 struct sk_buff *skb; 272 int err = -ENOBUFS; 273 274 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC); 275 if (skb == NULL) 276 goto errout; 277 278 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd); 279 if (err < 0) { 280 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */ 281 WARN_ON(err == -EMSGSIZE); 282 kfree_skb(skb); 283 goto errout; 284 } 285 286 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 287 return; 288 errout: 289 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 290 } 291 292 static void vxlan_fdb_switchdev_notifier_info(const struct vxlan_dev *vxlan, 293 const struct vxlan_fdb *fdb, 294 const struct vxlan_rdst *rd, 295 struct netlink_ext_ack *extack, 296 struct switchdev_notifier_vxlan_fdb_info *fdb_info) 297 { 298 fdb_info->info.dev = vxlan->dev; 299 fdb_info->info.extack = extack; 300 fdb_info->remote_ip = rd->remote_ip; 301 fdb_info->remote_port = rd->remote_port; 302 fdb_info->remote_vni = rd->remote_vni; 303 fdb_info->remote_ifindex = rd->remote_ifindex; 304 memcpy(fdb_info->eth_addr, fdb->key.eth_addr, ETH_ALEN); 305 fdb_info->vni = fdb->key.vni; 306 fdb_info->offloaded = rd->offloaded; 307 fdb_info->added_by_user = fdb->flags & NTF_VXLAN_ADDED_BY_USER; 308 } 309 310 static int vxlan_fdb_switchdev_call_notifiers(struct vxlan_dev *vxlan, 311 struct vxlan_fdb *fdb, 312 struct vxlan_rdst *rd, 313 bool adding, 314 struct netlink_ext_ack *extack) 315 { 316 struct switchdev_notifier_vxlan_fdb_info info; 317 enum switchdev_notifier_type notifier_type; 318 int ret; 319 320 if (WARN_ON(!rd)) 321 return 0; 322 323 notifier_type = adding ? SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE 324 : SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE; 325 vxlan_fdb_switchdev_notifier_info(vxlan, fdb, rd, NULL, &info); 326 ret = call_switchdev_notifiers(notifier_type, vxlan->dev, 327 &info.info, extack); 328 return notifier_to_errno(ret); 329 } 330 331 static int vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb, 332 struct vxlan_rdst *rd, int type, bool swdev_notify, 333 struct netlink_ext_ack *extack) 334 { 335 int err; 336 337 if (swdev_notify && rd) { 338 switch (type) { 339 case RTM_NEWNEIGH: 340 err = vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd, 341 true, extack); 342 if (err) 343 return err; 344 break; 345 case RTM_DELNEIGH: 346 vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd, 347 false, extack); 348 break; 349 } 350 } 351 352 __vxlan_fdb_notify(vxlan, fdb, rd, type); 353 return 0; 354 } 355 356 static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa) 357 { 358 struct vxlan_dev *vxlan = netdev_priv(dev); 359 struct vxlan_fdb f = { 360 .state = NUD_STALE, 361 }; 362 struct vxlan_rdst remote = { 363 .remote_ip = *ipa, /* goes to NDA_DST */ 364 .remote_vni = cpu_to_be32(VXLAN_N_VID), 365 }; 366 367 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL); 368 } 369 370 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN]) 371 { 372 struct vxlan_fdb f = { 373 .state = NUD_STALE, 374 }; 375 struct vxlan_rdst remote = { }; 376 377 memcpy(f.key.eth_addr, eth_addr, ETH_ALEN); 378 379 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL); 380 } 381 382 /* Look up Ethernet address in forwarding table */ 383 static struct vxlan_fdb *vxlan_find_mac_rcu(struct vxlan_dev *vxlan, 384 const struct vxlan_config *cfg, 385 const u8 *mac, __be32 vni) 386 { 387 struct vxlan_fdb_key key; 388 389 memset(&key, 0, sizeof(key)); 390 memcpy(key.eth_addr, mac, sizeof(key.eth_addr)); 391 if (!(cfg->flags & VXLAN_F_COLLECT_METADATA)) 392 key.vni = cfg->vni; 393 else 394 key.vni = vni; 395 396 return rhashtable_lookup(&vxlan->fdb_hash_tbl, &key, 397 vxlan_fdb_rht_params); 398 } 399 400 static struct vxlan_fdb *vxlan_find_mac_tx(struct vxlan_dev *vxlan, 401 const struct vxlan_config *cfg, 402 const u8 *mac, __be32 vni) 403 { 404 struct vxlan_fdb *f; 405 406 f = vxlan_find_mac_rcu(vxlan, cfg, mac, vni); 407 if (f) { 408 unsigned long now = jiffies; 409 410 if (READ_ONCE(f->used) != now) 411 WRITE_ONCE(f->used, now); 412 } 413 414 return f; 415 } 416 417 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan, 418 const u8 *mac, __be32 vni) 419 { 420 struct vxlan_fdb *f; 421 422 lockdep_assert_held_once(&vxlan->hash_lock); 423 424 rcu_read_lock(); 425 f = vxlan_find_mac_rcu(vxlan, rcu_dereference(vxlan->cfg), mac, vni); 426 rcu_read_unlock(); 427 428 return f; 429 } 430 431 /* caller should hold vxlan->hash_lock */ 432 static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f, 433 union vxlan_addr *ip, __be16 port, 434 __be32 vni, __u32 ifindex) 435 { 436 struct vxlan_rdst *rd; 437 438 list_for_each_entry(rd, &f->remotes, list) { 439 if (vxlan_addr_equal(&rd->remote_ip, ip) && 440 rd->remote_port == port && 441 rd->remote_vni == vni && 442 rd->remote_ifindex == ifindex) 443 return rd; 444 } 445 446 return NULL; 447 } 448 449 int vxlan_fdb_find_uc(struct net_device *dev, const u8 *mac, __be32 vni, 450 struct switchdev_notifier_vxlan_fdb_info *fdb_info) 451 { 452 struct vxlan_dev *vxlan = netdev_priv(dev); 453 u8 eth_addr[ETH_ALEN + 2] = { 0 }; 454 struct vxlan_rdst *rdst = NULL; 455 struct vxlan_fdb *f; 456 int rc = 0; 457 458 if (is_multicast_ether_addr(mac) || 459 is_zero_ether_addr(mac)) 460 return -EINVAL; 461 462 ether_addr_copy(eth_addr, mac); 463 464 rcu_read_lock(); 465 466 f = vxlan_find_mac_rcu(vxlan, rcu_dereference(vxlan->cfg), eth_addr, vni); 467 if (f) 468 rdst = first_remote_rcu(f); 469 if (!rdst) { 470 rc = -ENOENT; 471 goto out; 472 } 473 474 vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, NULL, fdb_info); 475 476 out: 477 rcu_read_unlock(); 478 return rc; 479 } 480 EXPORT_SYMBOL_GPL(vxlan_fdb_find_uc); 481 482 static int vxlan_fdb_notify_one(struct notifier_block *nb, 483 const struct vxlan_dev *vxlan, 484 const struct vxlan_fdb *f, 485 const struct vxlan_rdst *rdst, 486 struct netlink_ext_ack *extack) 487 { 488 struct switchdev_notifier_vxlan_fdb_info fdb_info; 489 int rc; 490 491 vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, extack, &fdb_info); 492 rc = nb->notifier_call(nb, SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE, 493 &fdb_info); 494 return notifier_to_errno(rc); 495 } 496 497 int vxlan_fdb_replay(const struct net_device *dev, __be32 vni, 498 struct notifier_block *nb, 499 struct netlink_ext_ack *extack) 500 { 501 struct vxlan_dev *vxlan; 502 struct vxlan_rdst *rdst; 503 struct vxlan_fdb *f; 504 int rc = 0; 505 506 if (!netif_is_vxlan(dev)) 507 return -EINVAL; 508 vxlan = netdev_priv(dev); 509 510 spin_lock_bh(&vxlan->hash_lock); 511 hlist_for_each_entry(f, &vxlan->fdb_list, fdb_node) { 512 if (f->key.vni == vni) { 513 list_for_each_entry(rdst, &f->remotes, list) { 514 rc = vxlan_fdb_notify_one(nb, vxlan, f, rdst, 515 extack); 516 if (rc) 517 goto unlock; 518 } 519 } 520 } 521 spin_unlock_bh(&vxlan->hash_lock); 522 return 0; 523 524 unlock: 525 spin_unlock_bh(&vxlan->hash_lock); 526 return rc; 527 } 528 EXPORT_SYMBOL_GPL(vxlan_fdb_replay); 529 530 void vxlan_fdb_clear_offload(const struct net_device *dev, __be32 vni) 531 { 532 struct vxlan_dev *vxlan; 533 struct vxlan_rdst *rdst; 534 struct vxlan_fdb *f; 535 536 if (!netif_is_vxlan(dev)) 537 return; 538 vxlan = netdev_priv(dev); 539 540 spin_lock_bh(&vxlan->hash_lock); 541 hlist_for_each_entry(f, &vxlan->fdb_list, fdb_node) { 542 if (f->key.vni == vni) { 543 list_for_each_entry(rdst, &f->remotes, list) 544 rdst->offloaded = false; 545 } 546 } 547 spin_unlock_bh(&vxlan->hash_lock); 548 549 } 550 EXPORT_SYMBOL_GPL(vxlan_fdb_clear_offload); 551 552 /* Replace destination of unicast mac */ 553 static int vxlan_fdb_replace(struct vxlan_fdb *f, 554 union vxlan_addr *ip, __be16 port, __be32 vni, 555 __u32 ifindex, struct vxlan_rdst *oldrd) 556 { 557 struct vxlan_rdst *rd; 558 559 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex); 560 if (rd) 561 return 0; 562 563 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list); 564 if (!rd) 565 return 0; 566 567 *oldrd = *rd; 568 dst_cache_reset(&rd->dst_cache); 569 rd->remote_ip = *ip; 570 rd->remote_port = port; 571 rd->remote_vni = vni; 572 rd->remote_ifindex = ifindex; 573 rd->offloaded = false; 574 return 1; 575 } 576 577 /* Add/update destinations for multicast */ 578 static int vxlan_fdb_append(struct vxlan_fdb *f, 579 union vxlan_addr *ip, __be16 port, __be32 vni, 580 __u32 ifindex, struct vxlan_rdst **rdp) 581 { 582 struct vxlan_rdst *rd; 583 584 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex); 585 if (rd) 586 return 0; 587 588 rd = kmalloc_obj(*rd, GFP_ATOMIC); 589 if (rd == NULL) 590 return -ENOMEM; 591 592 /* The driver can work correctly without a dst cache, so do not treat 593 * dst cache initialization errors as fatal. 594 */ 595 dst_cache_init(&rd->dst_cache, GFP_ATOMIC | __GFP_NOWARN); 596 597 rd->remote_ip = *ip; 598 rd->remote_port = port; 599 rd->offloaded = false; 600 rd->remote_vni = vni; 601 rd->remote_ifindex = ifindex; 602 603 list_add_tail_rcu(&rd->list, &f->remotes); 604 605 *rdp = rd; 606 return 1; 607 } 608 609 static bool vxlan_parse_gpe_proto(const struct vxlanhdr *hdr, __be16 *protocol) 610 { 611 const struct vxlanhdr_gpe *gpe = (const struct vxlanhdr_gpe *)hdr; 612 613 /* Need to have Next Protocol set for interfaces in GPE mode. */ 614 if (!gpe->np_applied) 615 return false; 616 /* "The initial version is 0. If a receiver does not support the 617 * version indicated it MUST drop the packet. 618 */ 619 if (gpe->version != 0) 620 return false; 621 /* "When the O bit is set to 1, the packet is an OAM packet and OAM 622 * processing MUST occur." However, we don't implement OAM 623 * processing, thus drop the packet. 624 */ 625 if (gpe->oam_flag) 626 return false; 627 628 *protocol = tun_p_to_eth_p(gpe->next_protocol); 629 if (!*protocol) 630 return false; 631 632 return true; 633 } 634 635 static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb, 636 unsigned int off, 637 struct vxlanhdr *vh, size_t hdrlen, 638 __be32 vni_field, 639 struct gro_remcsum *grc, 640 bool nopartial) 641 { 642 size_t start, offset; 643 644 if (skb->remcsum_offload) 645 return vh; 646 647 if (!NAPI_GRO_CB(skb)->csum_valid) 648 return NULL; 649 650 start = vxlan_rco_start(vni_field); 651 offset = start + vxlan_rco_offset(vni_field); 652 653 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen, 654 start, offset, grc, nopartial); 655 656 skb->remcsum_offload = 1; 657 658 return vh; 659 } 660 661 static struct vxlanhdr *vxlan_gro_prepare_receive(struct sock *sk, 662 struct list_head *head, 663 struct sk_buff *skb, 664 struct gro_remcsum *grc) 665 { 666 struct vxlanhdr *vh, *vh2; 667 unsigned int hlen, off_vx; 668 struct vxlan_sock *vs; 669 struct sk_buff *p; 670 __be32 flags; 671 672 skb_gro_remcsum_init(grc); 673 674 vs = rcu_dereference_sk_user_data(sk); 675 if (!vs) 676 return NULL; 677 678 off_vx = skb_gro_offset(skb); 679 hlen = off_vx + sizeof(*vh); 680 vh = skb_gro_header(skb, hlen, off_vx); 681 if (unlikely(!vh)) 682 return NULL; 683 684 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr)); 685 686 flags = vh->vx_flags; 687 688 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) { 689 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr), 690 vh->vx_vni, grc, 691 !!(vs->flags & 692 VXLAN_F_REMCSUM_NOPARTIAL)); 693 694 if (!vh) 695 return NULL; 696 } 697 698 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */ 699 700 list_for_each_entry(p, head, list) { 701 if (!NAPI_GRO_CB(p)->same_flow) 702 continue; 703 704 vh2 = (struct vxlanhdr *)(p->data + off_vx); 705 if (vh->vx_flags != vh2->vx_flags || 706 vh->vx_vni != vh2->vx_vni) { 707 NAPI_GRO_CB(p)->same_flow = 0; 708 continue; 709 } 710 } 711 712 return vh; 713 } 714 715 static struct sk_buff *vxlan_gro_receive(struct sock *sk, 716 struct list_head *head, 717 struct sk_buff *skb) 718 { 719 struct sk_buff *pp = NULL; 720 struct gro_remcsum grc; 721 int flush = 1; 722 723 if (vxlan_gro_prepare_receive(sk, head, skb, &grc)) { 724 pp = call_gro_receive(eth_gro_receive, head, skb); 725 flush = 0; 726 } 727 skb_gro_flush_final_remcsum(skb, pp, flush, &grc); 728 return pp; 729 } 730 731 static struct sk_buff *vxlan_gpe_gro_receive(struct sock *sk, 732 struct list_head *head, 733 struct sk_buff *skb) 734 { 735 const struct packet_offload *ptype; 736 struct sk_buff *pp = NULL; 737 struct gro_remcsum grc; 738 struct vxlanhdr *vh; 739 __be16 protocol; 740 int flush = 1; 741 742 vh = vxlan_gro_prepare_receive(sk, head, skb, &grc); 743 if (vh) { 744 if (!vxlan_parse_gpe_proto(vh, &protocol)) 745 goto out; 746 ptype = gro_find_receive_by_type(protocol); 747 if (!ptype) 748 goto out; 749 pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb); 750 flush = 0; 751 } 752 out: 753 skb_gro_flush_final_remcsum(skb, pp, flush, &grc); 754 return pp; 755 } 756 757 static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff) 758 { 759 /* Sets 'skb->inner_mac_header' since we are always called with 760 * 'skb->encapsulation' set. 761 */ 762 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr)); 763 } 764 765 static int vxlan_gpe_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff) 766 { 767 struct vxlanhdr *vh = (struct vxlanhdr *)(skb->data + nhoff); 768 const struct packet_offload *ptype; 769 int err = -ENOSYS; 770 __be16 protocol; 771 772 if (!vxlan_parse_gpe_proto(vh, &protocol)) 773 return err; 774 ptype = gro_find_complete_by_type(protocol); 775 if (ptype) 776 err = ptype->callbacks.gro_complete(skb, nhoff + sizeof(struct vxlanhdr)); 777 return err; 778 } 779 780 static struct vxlan_fdb *vxlan_fdb_alloc(struct vxlan_dev *vxlan, const u8 *mac, 781 __u16 state, __be32 src_vni, 782 __u16 ndm_flags) 783 { 784 struct vxlan_fdb *f; 785 786 f = kmalloc_obj(*f, GFP_ATOMIC); 787 if (!f) 788 return NULL; 789 memset(&f->key, 0, sizeof(f->key)); 790 f->state = state; 791 f->flags = ndm_flags; 792 f->updated = f->used = jiffies; 793 f->key.vni = src_vni; 794 f->nh = NULL; 795 RCU_INIT_POINTER(f->vdev, vxlan); 796 INIT_LIST_HEAD(&f->nh_list); 797 INIT_LIST_HEAD(&f->remotes); 798 memcpy(f->key.eth_addr, mac, ETH_ALEN); 799 800 return f; 801 } 802 803 static int vxlan_fdb_nh_update(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb, 804 u32 nhid, struct netlink_ext_ack *extack) 805 { 806 struct nexthop *old_nh = rtnl_dereference(fdb->nh); 807 const struct vxlan_config *cfg; 808 struct nexthop *nh; 809 int err = -EINVAL; 810 811 if (old_nh && old_nh->id == nhid) 812 return 0; 813 814 nh = nexthop_find_by_id(vxlan->net, nhid); 815 if (!nh) { 816 NL_SET_ERR_MSG(extack, "Nexthop id does not exist"); 817 goto err_inval; 818 } 819 820 if (!nexthop_get(nh)) { 821 NL_SET_ERR_MSG(extack, "Nexthop has been deleted"); 822 nh = NULL; 823 goto err_inval; 824 } 825 if (!nexthop_is_fdb(nh)) { 826 NL_SET_ERR_MSG(extack, "Nexthop is not a fdb nexthop"); 827 goto err_inval; 828 } 829 830 if (!nexthop_is_multipath(nh)) { 831 NL_SET_ERR_MSG(extack, "Nexthop is not a multipath group"); 832 goto err_inval; 833 } 834 835 /* check nexthop group family */ 836 cfg = rtnl_dereference(vxlan->cfg); 837 switch (cfg->remote_ip.sa.sa_family) { 838 case AF_INET: 839 if (!nexthop_has_v4(nh)) { 840 err = -EAFNOSUPPORT; 841 NL_SET_ERR_MSG(extack, "Nexthop group family not supported"); 842 goto err_inval; 843 } 844 break; 845 case AF_INET6: 846 if (nexthop_has_v4(nh)) { 847 err = -EAFNOSUPPORT; 848 NL_SET_ERR_MSG(extack, "Nexthop group family not supported"); 849 goto err_inval; 850 } 851 } 852 853 if (old_nh) { 854 list_del_rcu(&fdb->nh_list); 855 nexthop_put(old_nh); 856 } 857 rcu_assign_pointer(fdb->nh, nh); 858 list_add_tail_rcu(&fdb->nh_list, &nh->fdb_list); 859 return 1; 860 861 err_inval: 862 if (nh) 863 nexthop_put(nh); 864 return err; 865 } 866 867 int vxlan_fdb_create(struct vxlan_dev *vxlan, 868 const u8 *mac, union vxlan_addr *ip, 869 __u16 state, __be16 port, __be32 src_vni, 870 __be32 vni, __u32 ifindex, __u16 ndm_flags, 871 u32 nhid, struct vxlan_fdb **fdb, 872 struct netlink_ext_ack *extack) 873 { 874 const struct vxlan_config *cfg = rcu_dereference_rtnl(vxlan->cfg); 875 struct vxlan_rdst *rd = NULL; 876 struct vxlan_fdb *f; 877 int rc; 878 879 if (cfg->addrmax && 880 vxlan->addrcnt >= cfg->addrmax) 881 return -ENOSPC; 882 883 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip); 884 f = vxlan_fdb_alloc(vxlan, mac, state, src_vni, ndm_flags); 885 if (!f) 886 return -ENOMEM; 887 888 if (nhid) 889 rc = vxlan_fdb_nh_update(vxlan, f, nhid, extack); 890 else 891 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd); 892 if (rc < 0) 893 goto errout; 894 895 rc = rhashtable_lookup_insert_fast(&vxlan->fdb_hash_tbl, &f->rhnode, 896 vxlan_fdb_rht_params); 897 if (rc) 898 goto destroy_remote; 899 900 ++vxlan->addrcnt; 901 hlist_add_head_rcu(&f->fdb_node, &vxlan->fdb_list); 902 903 *fdb = f; 904 905 return 0; 906 907 destroy_remote: 908 if (rcu_access_pointer(f->nh)) { 909 list_del_rcu(&f->nh_list); 910 nexthop_put(rtnl_dereference(f->nh)); 911 } else { 912 list_del(&rd->list); 913 dst_cache_destroy(&rd->dst_cache); 914 kfree(rd); 915 } 916 errout: 917 kfree(f); 918 return rc; 919 } 920 921 static void __vxlan_fdb_free(struct vxlan_fdb *f) 922 { 923 struct vxlan_rdst *rd, *nd; 924 struct nexthop *nh; 925 926 nh = rcu_dereference_raw(f->nh); 927 if (nh) { 928 rcu_assign_pointer(f->nh, NULL); 929 rcu_assign_pointer(f->vdev, NULL); 930 nexthop_put(nh); 931 } 932 933 list_for_each_entry_safe(rd, nd, &f->remotes, list) { 934 dst_cache_destroy(&rd->dst_cache); 935 kfree(rd); 936 } 937 kfree(f); 938 } 939 940 static void vxlan_fdb_free(struct rcu_head *head) 941 { 942 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu); 943 944 __vxlan_fdb_free(f); 945 } 946 947 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f, 948 bool do_notify, bool swdev_notify) 949 { 950 struct vxlan_rdst *rd; 951 952 netdev_dbg(vxlan->dev, "delete %pM\n", f->key.eth_addr); 953 954 --vxlan->addrcnt; 955 if (do_notify) { 956 if (rcu_access_pointer(f->nh)) 957 vxlan_fdb_notify(vxlan, f, NULL, RTM_DELNEIGH, 958 swdev_notify, NULL); 959 else 960 list_for_each_entry(rd, &f->remotes, list) 961 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH, 962 swdev_notify, NULL); 963 } 964 965 hlist_del_init_rcu(&f->fdb_node); 966 rhashtable_remove_fast(&vxlan->fdb_hash_tbl, &f->rhnode, 967 vxlan_fdb_rht_params); 968 list_del_rcu(&f->nh_list); 969 call_rcu(&f->rcu, vxlan_fdb_free); 970 } 971 972 static void vxlan_dst_free(struct rcu_head *head) 973 { 974 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu); 975 976 dst_cache_destroy(&rd->dst_cache); 977 kfree(rd); 978 } 979 980 static int vxlan_fdb_update_existing(struct vxlan_dev *vxlan, 981 union vxlan_addr *ip, 982 __u16 state, __u16 flags, 983 __be16 port, __be32 vni, 984 __u32 ifindex, __u16 ndm_flags, 985 struct vxlan_fdb *f, u32 nhid, 986 bool swdev_notify, 987 struct netlink_ext_ack *extack) 988 { 989 __u16 fdb_flags = (ndm_flags & ~NTF_USE); 990 struct vxlan_rdst *rd = NULL; 991 struct vxlan_rdst oldrd; 992 int notify = 0; 993 int rc = 0; 994 int err; 995 996 if (nhid && !rcu_access_pointer(f->nh)) { 997 NL_SET_ERR_MSG(extack, 998 "Cannot replace an existing non nexthop fdb with a nexthop"); 999 return -EOPNOTSUPP; 1000 } 1001 1002 if (nhid && (flags & NLM_F_APPEND)) { 1003 NL_SET_ERR_MSG(extack, 1004 "Cannot append to a nexthop fdb"); 1005 return -EOPNOTSUPP; 1006 } 1007 1008 if (rcu_access_pointer(f->nh) && 1009 !(state & (NUD_PERMANENT | NUD_NOARP))) { 1010 NL_SET_ERR_MSG(extack, "Cannot make a nexthop fdb dynamic"); 1011 return -EOPNOTSUPP; 1012 } 1013 1014 /* Do not allow an externally learned entry to take over an entry added 1015 * by the user. 1016 */ 1017 if (!(fdb_flags & NTF_EXT_LEARNED) || 1018 !(f->flags & NTF_VXLAN_ADDED_BY_USER)) { 1019 if (f->state != state) { 1020 f->state = state; 1021 notify = 1; 1022 } 1023 if (f->flags != fdb_flags) { 1024 f->flags = fdb_flags; 1025 notify = 1; 1026 } 1027 } 1028 1029 if ((flags & NLM_F_REPLACE)) { 1030 /* Only change unicasts */ 1031 if (!(is_multicast_ether_addr(f->key.eth_addr) || 1032 is_zero_ether_addr(f->key.eth_addr))) { 1033 if (nhid) { 1034 rc = vxlan_fdb_nh_update(vxlan, f, nhid, extack); 1035 if (rc < 0) 1036 return rc; 1037 } else { 1038 rc = vxlan_fdb_replace(f, ip, port, vni, 1039 ifindex, &oldrd); 1040 } 1041 notify |= rc; 1042 } else { 1043 NL_SET_ERR_MSG(extack, "Cannot replace non-unicast fdb entries"); 1044 return -EOPNOTSUPP; 1045 } 1046 } 1047 if ((flags & NLM_F_APPEND) && 1048 (is_multicast_ether_addr(f->key.eth_addr) || 1049 is_zero_ether_addr(f->key.eth_addr))) { 1050 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd); 1051 1052 if (rc < 0) 1053 return rc; 1054 notify |= rc; 1055 } 1056 1057 if (ndm_flags & NTF_USE) 1058 WRITE_ONCE(f->updated, jiffies); 1059 1060 if (notify) { 1061 if (rd == NULL) 1062 rd = first_remote_rtnl(f); 1063 1064 WRITE_ONCE(f->updated, jiffies); 1065 err = vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH, 1066 swdev_notify, extack); 1067 if (err) 1068 goto err_notify; 1069 } 1070 1071 return 0; 1072 1073 err_notify: 1074 if (nhid) 1075 return err; 1076 if ((flags & NLM_F_REPLACE) && rc) 1077 *rd = oldrd; 1078 else if ((flags & NLM_F_APPEND) && rc) { 1079 list_del_rcu(&rd->list); 1080 call_rcu(&rd->rcu, vxlan_dst_free); 1081 } 1082 return err; 1083 } 1084 1085 static int vxlan_fdb_update_create(struct vxlan_dev *vxlan, 1086 const u8 *mac, union vxlan_addr *ip, 1087 __u16 state, __u16 flags, 1088 __be16 port, __be32 src_vni, __be32 vni, 1089 __u32 ifindex, __u16 ndm_flags, u32 nhid, 1090 bool swdev_notify, 1091 struct netlink_ext_ack *extack) 1092 { 1093 __u16 fdb_flags = (ndm_flags & ~NTF_USE); 1094 struct vxlan_fdb *f; 1095 int rc; 1096 1097 /* Disallow replace to add a multicast entry */ 1098 if ((flags & NLM_F_REPLACE) && 1099 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac))) 1100 return -EOPNOTSUPP; 1101 1102 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip); 1103 rc = vxlan_fdb_create(vxlan, mac, ip, state, port, src_vni, 1104 vni, ifindex, fdb_flags, nhid, &f, extack); 1105 if (rc < 0) 1106 return rc; 1107 1108 rc = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_NEWNEIGH, 1109 swdev_notify, extack); 1110 if (rc) 1111 goto err_notify; 1112 1113 return 0; 1114 1115 err_notify: 1116 vxlan_fdb_destroy(vxlan, f, false, false); 1117 return rc; 1118 } 1119 1120 /* Add new entry to forwarding table -- assumes lock held */ 1121 int vxlan_fdb_update(struct vxlan_dev *vxlan, 1122 const u8 *mac, union vxlan_addr *ip, 1123 __u16 state, __u16 flags, 1124 __be16 port, __be32 src_vni, __be32 vni, 1125 __u32 ifindex, __u16 ndm_flags, u32 nhid, 1126 bool swdev_notify, 1127 struct netlink_ext_ack *extack) 1128 { 1129 struct vxlan_fdb *f; 1130 1131 f = vxlan_find_mac(vxlan, mac, src_vni); 1132 if (f) { 1133 if (flags & NLM_F_EXCL) { 1134 netdev_dbg(vxlan->dev, 1135 "lost race to create %pM\n", mac); 1136 return -EEXIST; 1137 } 1138 1139 return vxlan_fdb_update_existing(vxlan, ip, state, flags, port, 1140 vni, ifindex, ndm_flags, f, 1141 nhid, swdev_notify, extack); 1142 } else { 1143 if (!(flags & NLM_F_CREATE)) 1144 return -ENOENT; 1145 1146 return vxlan_fdb_update_create(vxlan, mac, ip, state, flags, 1147 port, src_vni, vni, ifindex, 1148 ndm_flags, nhid, swdev_notify, 1149 extack); 1150 } 1151 } 1152 1153 static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f, 1154 struct vxlan_rdst *rd, bool swdev_notify) 1155 { 1156 list_del_rcu(&rd->list); 1157 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH, swdev_notify, NULL); 1158 call_rcu(&rd->rcu, vxlan_dst_free); 1159 } 1160 1161 static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan, 1162 union vxlan_addr *ip, __be16 *port, __be32 *src_vni, 1163 __be32 *vni, u32 *ifindex, u32 *nhid, 1164 struct netlink_ext_ack *extack) 1165 { 1166 const struct vxlan_config *cfg = rtnl_dereference(vxlan->cfg); 1167 struct net *net = dev_net(vxlan->dev); 1168 int err; 1169 1170 if (tb[NDA_NH_ID] && 1171 (tb[NDA_DST] || tb[NDA_VNI] || tb[NDA_IFINDEX] || tb[NDA_PORT])) { 1172 NL_SET_ERR_MSG(extack, "DST, VNI, ifindex and port are mutually exclusive with NH_ID"); 1173 return -EINVAL; 1174 } 1175 1176 if (tb[NDA_DST]) { 1177 err = vxlan_nla_get_addr(ip, tb[NDA_DST]); 1178 if (err) { 1179 NL_SET_ERR_MSG(extack, "Unsupported address family"); 1180 return err; 1181 } 1182 } else { 1183 const union vxlan_addr *remote = &cfg->remote_ip; 1184 1185 if (remote->sa.sa_family == AF_INET) { 1186 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY); 1187 ip->sa.sa_family = AF_INET; 1188 #if IS_ENABLED(CONFIG_IPV6) 1189 } else { 1190 ip->sin6.sin6_addr = in6addr_any; 1191 ip->sa.sa_family = AF_INET6; 1192 #endif 1193 } 1194 } 1195 1196 if (tb[NDA_PORT]) { 1197 if (nla_len(tb[NDA_PORT]) != sizeof(__be16)) { 1198 NL_SET_ERR_MSG(extack, "Invalid vxlan port"); 1199 return -EINVAL; 1200 } 1201 *port = nla_get_be16(tb[NDA_PORT]); 1202 } else { 1203 *port = cfg->dst_port; 1204 } 1205 1206 if (tb[NDA_VNI]) { 1207 if (nla_len(tb[NDA_VNI]) != sizeof(u32)) { 1208 NL_SET_ERR_MSG(extack, "Invalid vni"); 1209 return -EINVAL; 1210 } 1211 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI])); 1212 } else { 1213 *vni = cfg->vni; 1214 } 1215 1216 if (tb[NDA_SRC_VNI]) { 1217 if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32)) { 1218 NL_SET_ERR_MSG(extack, "Invalid src vni"); 1219 return -EINVAL; 1220 } 1221 *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI])); 1222 } else { 1223 *src_vni = cfg->vni; 1224 } 1225 1226 if (tb[NDA_IFINDEX]) { 1227 struct net_device *tdev; 1228 1229 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32)) { 1230 NL_SET_ERR_MSG(extack, "Invalid ifindex"); 1231 return -EINVAL; 1232 } 1233 *ifindex = nla_get_u32(tb[NDA_IFINDEX]); 1234 tdev = __dev_get_by_index(net, *ifindex); 1235 if (!tdev) { 1236 NL_SET_ERR_MSG(extack, "Device not found"); 1237 return -EADDRNOTAVAIL; 1238 } 1239 } else { 1240 *ifindex = 0; 1241 } 1242 1243 *nhid = nla_get_u32_default(tb[NDA_NH_ID], 0); 1244 1245 return 0; 1246 } 1247 1248 /* Add static entry (via netlink) */ 1249 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], 1250 struct net_device *dev, 1251 const unsigned char *addr, u16 vid, u16 flags, 1252 bool *notified, struct netlink_ext_ack *extack) 1253 { 1254 const struct vxlan_config *cfg; 1255 struct vxlan_dev *vxlan = netdev_priv(dev); 1256 /* struct net *net = dev_net(vxlan->dev); */ 1257 union vxlan_addr ip; 1258 __be16 port; 1259 __be32 src_vni, vni; 1260 u32 ifindex, nhid; 1261 int err; 1262 1263 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) { 1264 pr_info("RTM_NEWNEIGH with invalid state %#x\n", 1265 ndm->ndm_state); 1266 return -EINVAL; 1267 } 1268 1269 if (!tb || (!tb[NDA_DST] && !tb[NDA_NH_ID])) 1270 return -EINVAL; 1271 1272 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex, 1273 &nhid, extack); 1274 if (err) 1275 return err; 1276 1277 if (nhid && !(ndm->ndm_state & (NUD_PERMANENT | NUD_NOARP))) { 1278 NL_SET_ERR_MSG(extack, "A nexthop fdb cannot be dynamic"); 1279 return -EINVAL; 1280 } 1281 1282 cfg = rtnl_dereference(vxlan->cfg); 1283 if (cfg->remote_ip.sa.sa_family != ip.sa.sa_family) 1284 return -EAFNOSUPPORT; 1285 1286 spin_lock_bh(&vxlan->hash_lock); 1287 err = vxlan_fdb_update(vxlan, addr, &ip, ndm->ndm_state, flags, 1288 port, src_vni, vni, ifindex, 1289 ndm->ndm_flags | NTF_VXLAN_ADDED_BY_USER, 1290 nhid, true, extack); 1291 spin_unlock_bh(&vxlan->hash_lock); 1292 1293 if (!err) 1294 *notified = true; 1295 1296 return err; 1297 } 1298 1299 int __vxlan_fdb_delete(struct vxlan_dev *vxlan, 1300 const unsigned char *addr, union vxlan_addr ip, 1301 __be16 port, __be32 src_vni, __be32 vni, 1302 u32 ifindex, bool swdev_notify) 1303 { 1304 struct vxlan_rdst *rd = NULL; 1305 struct vxlan_fdb *f; 1306 int err = -ENOENT; 1307 1308 f = vxlan_find_mac(vxlan, addr, src_vni); 1309 if (!f) 1310 return err; 1311 1312 if (!vxlan_addr_any(&ip)) { 1313 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex); 1314 if (!rd) 1315 goto out; 1316 } 1317 1318 /* remove a destination if it's not the only one on the list, 1319 * otherwise destroy the fdb entry 1320 */ 1321 if (rd && !list_is_singular(&f->remotes)) { 1322 vxlan_fdb_dst_destroy(vxlan, f, rd, swdev_notify); 1323 goto out; 1324 } 1325 1326 vxlan_fdb_destroy(vxlan, f, true, swdev_notify); 1327 1328 out: 1329 return 0; 1330 } 1331 1332 /* Delete entry (via netlink) */ 1333 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[], 1334 struct net_device *dev, 1335 const unsigned char *addr, u16 vid, bool *notified, 1336 struct netlink_ext_ack *extack) 1337 { 1338 struct vxlan_dev *vxlan = netdev_priv(dev); 1339 union vxlan_addr ip; 1340 __be32 src_vni, vni; 1341 u32 ifindex, nhid; 1342 __be16 port; 1343 int err; 1344 1345 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex, 1346 &nhid, extack); 1347 if (err) 1348 return err; 1349 1350 spin_lock_bh(&vxlan->hash_lock); 1351 err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex, 1352 true); 1353 spin_unlock_bh(&vxlan->hash_lock); 1354 1355 if (!err) 1356 *notified = true; 1357 1358 return err; 1359 } 1360 1361 /* Dump forwarding table */ 1362 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, 1363 struct net_device *dev, 1364 struct net_device *filter_dev, int *idx) 1365 { 1366 struct ndo_fdb_dump_context *ctx = (void *)cb->ctx; 1367 struct vxlan_dev *vxlan = netdev_priv(dev); 1368 struct vxlan_fdb *f; 1369 int err = 0; 1370 1371 rcu_read_lock(); 1372 hlist_for_each_entry_rcu(f, &vxlan->fdb_list, fdb_node) { 1373 struct vxlan_rdst *rd; 1374 1375 if (rcu_access_pointer(f->nh)) { 1376 if (*idx < ctx->fdb_idx) 1377 goto skip_nh; 1378 err = vxlan_fdb_info(skb, vxlan, f, 1379 NETLINK_CB(cb->skb).portid, 1380 cb->nlh->nlmsg_seq, 1381 RTM_NEWNEIGH, NLM_F_MULTI, NULL); 1382 if (err < 0) { 1383 rcu_read_unlock(); 1384 goto out; 1385 } 1386 skip_nh: 1387 *idx += 1; 1388 continue; 1389 } 1390 1391 list_for_each_entry_rcu(rd, &f->remotes, list) { 1392 if (*idx < ctx->fdb_idx) 1393 goto skip; 1394 1395 err = vxlan_fdb_info(skb, vxlan, f, 1396 NETLINK_CB(cb->skb).portid, 1397 cb->nlh->nlmsg_seq, 1398 RTM_NEWNEIGH, NLM_F_MULTI, rd); 1399 if (err < 0) { 1400 rcu_read_unlock(); 1401 goto out; 1402 } 1403 skip: 1404 *idx += 1; 1405 } 1406 } 1407 rcu_read_unlock(); 1408 out: 1409 return err; 1410 } 1411 1412 static int vxlan_fdb_get(struct sk_buff *skb, 1413 struct nlattr *tb[], 1414 struct net_device *dev, 1415 const unsigned char *addr, 1416 u16 vid, u32 portid, u32 seq, 1417 struct netlink_ext_ack *extack) 1418 { 1419 struct vxlan_dev *vxlan = netdev_priv(dev); 1420 const struct vxlan_config *cfg; 1421 struct vxlan_fdb *f; 1422 __be32 vni; 1423 int err; 1424 1425 cfg = rcu_dereference_rtnl(vxlan->cfg); 1426 1427 if (tb[NDA_VNI]) 1428 vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI])); 1429 else 1430 vni = cfg->vni; 1431 1432 rcu_read_lock(); 1433 1434 f = vxlan_find_mac_rcu(vxlan, cfg, addr, vni); 1435 if (!f) { 1436 NL_SET_ERR_MSG(extack, "Fdb entry not found"); 1437 err = -ENOENT; 1438 goto errout; 1439 } 1440 1441 err = vxlan_fdb_info(skb, vxlan, f, portid, seq, 1442 RTM_NEWNEIGH, 0, first_remote_rcu(f)); 1443 errout: 1444 rcu_read_unlock(); 1445 return err; 1446 } 1447 1448 /* Watch incoming packets to learn mapping between Ethernet address 1449 * and Tunnel endpoint. 1450 */ 1451 static enum skb_drop_reason vxlan_snoop(struct net_device *dev, 1452 const struct vxlan_config *cfg, 1453 union vxlan_addr *src_ip, 1454 const u8 *src_mac, u32 src_ifindex, 1455 __be32 vni) 1456 { 1457 struct vxlan_dev *vxlan = netdev_priv(dev); 1458 struct vxlan_fdb *f; 1459 u32 ifindex = 0; 1460 1461 /* Ignore packets from invalid src-address */ 1462 if (!is_valid_ether_addr(src_mac)) 1463 return SKB_DROP_REASON_MAC_INVALID_SOURCE; 1464 1465 #if IS_ENABLED(CONFIG_IPV6) 1466 if (src_ip->sa.sa_family == AF_INET6 && 1467 (ipv6_addr_type(&src_ip->sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL)) 1468 ifindex = src_ifindex; 1469 #endif 1470 1471 f = vxlan_find_mac_rcu(vxlan, cfg, src_mac, vni); 1472 if (likely(f)) { 1473 struct vxlan_rdst *rdst = first_remote_rcu(f); 1474 unsigned long now = jiffies; 1475 1476 if (READ_ONCE(f->updated) != now) 1477 WRITE_ONCE(f->updated, now); 1478 1479 /* Don't override an fdb with nexthop with a learnt entry */ 1480 if (rcu_access_pointer(f->nh)) 1481 return SKB_DROP_REASON_VXLAN_ENTRY_EXISTS; 1482 1483 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) && 1484 rdst->remote_ifindex == ifindex)) 1485 return SKB_NOT_DROPPED_YET; 1486 1487 /* Don't migrate static entries, drop packets */ 1488 if (f->state & (NUD_PERMANENT | NUD_NOARP)) 1489 return SKB_DROP_REASON_VXLAN_ENTRY_EXISTS; 1490 1491 if (net_ratelimit()) 1492 netdev_info(dev, 1493 "%pM migrated from %pIS to %pIS\n", 1494 src_mac, &rdst->remote_ip.sa, &src_ip->sa); 1495 1496 rdst->remote_ip = *src_ip; 1497 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH, true, NULL); 1498 } else { 1499 /* learned new entry */ 1500 spin_lock(&vxlan->hash_lock); 1501 1502 /* close off race between vxlan_flush and incoming packets */ 1503 if (netif_running(dev)) 1504 vxlan_fdb_update(vxlan, src_mac, src_ip, 1505 NUD_REACHABLE, 1506 NLM_F_EXCL|NLM_F_CREATE, 1507 cfg->dst_port, 1508 vni, 1509 cfg->vni, 1510 ifindex, NTF_SELF, 0, true, NULL); 1511 spin_unlock(&vxlan->hash_lock); 1512 } 1513 1514 return SKB_NOT_DROPPED_YET; 1515 } 1516 1517 static bool __vxlan_sock_release_prep(struct vxlan_sock *vs) 1518 { 1519 ASSERT_RTNL(); 1520 1521 if (!vs) 1522 return false; 1523 if (!refcount_dec_and_test(&vs->refcnt)) 1524 return false; 1525 1526 hlist_del_rcu(&vs->hlist); 1527 udp_tunnel_notify_del_rx_port(vs->sk, 1528 (vs->flags & VXLAN_F_GPE) ? 1529 UDP_TUNNEL_TYPE_VXLAN_GPE : 1530 UDP_TUNNEL_TYPE_VXLAN); 1531 1532 return true; 1533 } 1534 1535 static void vxlan_sock_release(struct vxlan_dev *vxlan) 1536 { 1537 const struct vxlan_config *cfg = rtnl_dereference(vxlan->cfg); 1538 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock); 1539 #if IS_ENABLED(CONFIG_IPV6) 1540 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock); 1541 1542 RCU_INIT_POINTER(vxlan->vn6_sock, NULL); 1543 #endif 1544 1545 RCU_INIT_POINTER(vxlan->vn4_sock, NULL); 1546 1547 if (cfg->flags & VXLAN_F_VNIFILTER) 1548 vxlan_vs_del_vnigrp(vxlan); 1549 else 1550 vxlan_vs_del_dev(vxlan); 1551 1552 if (__vxlan_sock_release_prep(sock4)) { 1553 udp_tunnel_sock_release(sock4->sk); 1554 kfree_rcu(sock4, rcu); 1555 } 1556 1557 #if IS_ENABLED(CONFIG_IPV6) 1558 if (__vxlan_sock_release_prep(sock6)) { 1559 udp_tunnel_sock_release(sock6->sk); 1560 kfree_rcu(sock6, rcu); 1561 } 1562 #endif 1563 } 1564 1565 static enum skb_drop_reason vxlan_remcsum(struct sk_buff *skb, u32 vxflags) 1566 { 1567 const struct vxlanhdr *vh = vxlan_hdr(skb); 1568 enum skb_drop_reason reason; 1569 size_t start, offset; 1570 1571 if (!(vh->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload) 1572 return SKB_NOT_DROPPED_YET; 1573 1574 start = vxlan_rco_start(vh->vx_vni); 1575 offset = start + vxlan_rco_offset(vh->vx_vni); 1576 1577 reason = pskb_may_pull_reason(skb, offset + sizeof(u16)); 1578 if (reason) 1579 return reason; 1580 1581 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset, 1582 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL)); 1583 return SKB_NOT_DROPPED_YET; 1584 } 1585 1586 static void vxlan_parse_gbp_hdr(struct sk_buff *skb, u32 vxflags, 1587 struct vxlan_metadata *md) 1588 { 1589 const struct vxlanhdr *vh = vxlan_hdr(skb); 1590 const struct vxlanhdr_gbp *gbp; 1591 struct metadata_dst *tun_dst; 1592 1593 gbp = (const struct vxlanhdr_gbp *)vh; 1594 1595 if (!(vh->vx_flags & VXLAN_HF_GBP)) 1596 return; 1597 1598 md->gbp = ntohs(gbp->policy_id); 1599 1600 tun_dst = (struct metadata_dst *)skb_dst(skb); 1601 if (tun_dst) { 1602 __set_bit(IP_TUNNEL_VXLAN_OPT_BIT, 1603 tun_dst->u.tun_info.key.tun_flags); 1604 tun_dst->u.tun_info.options_len = sizeof(*md); 1605 } 1606 if (gbp->dont_learn) 1607 md->gbp |= VXLAN_GBP_DONT_LEARN; 1608 1609 if (gbp->policy_applied) 1610 md->gbp |= VXLAN_GBP_POLICY_APPLIED; 1611 1612 /* In flow-based mode, GBP is carried in dst_metadata */ 1613 if (!(vxflags & VXLAN_F_COLLECT_METADATA)) 1614 skb->mark = md->gbp; 1615 } 1616 1617 static enum skb_drop_reason vxlan_set_mac(struct vxlan_dev *vxlan, 1618 const struct vxlan_config *cfg, 1619 struct vxlan_sock *vs, 1620 struct sk_buff *skb, __be32 vni) 1621 { 1622 union vxlan_addr saddr; 1623 u32 ifindex = skb->dev->ifindex; 1624 1625 skb_reset_mac_header(skb); 1626 skb->protocol = eth_type_trans(skb, vxlan->dev); 1627 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); 1628 1629 /* Ignore packet loops (and multicast echo) */ 1630 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr)) 1631 return SKB_DROP_REASON_LOCAL_MAC; 1632 1633 /* Get address from the outer IP header */ 1634 if (vxlan_get_sk_family(vs) == AF_INET) { 1635 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr; 1636 saddr.sa.sa_family = AF_INET; 1637 #if IS_ENABLED(CONFIG_IPV6) 1638 } else { 1639 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr; 1640 saddr.sa.sa_family = AF_INET6; 1641 #endif 1642 } 1643 1644 if (!(cfg->flags & VXLAN_F_LEARN)) 1645 return SKB_NOT_DROPPED_YET; 1646 1647 return vxlan_snoop(skb->dev, cfg, &saddr, eth_hdr(skb)->h_source, 1648 ifindex, vni); 1649 } 1650 1651 static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph, 1652 struct sk_buff *skb) 1653 { 1654 int err = 0; 1655 1656 if (vxlan_get_sk_family(vs) == AF_INET) 1657 err = IP_ECN_decapsulate(oiph, skb); 1658 #if IS_ENABLED(CONFIG_IPV6) 1659 else 1660 err = IP6_ECN_decapsulate(oiph, skb); 1661 #endif 1662 1663 if (unlikely(err) && log_ecn_error) { 1664 if (vxlan_get_sk_family(vs) == AF_INET) 1665 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n", 1666 &((struct iphdr *)oiph)->saddr, 1667 ((struct iphdr *)oiph)->tos); 1668 else 1669 net_info_ratelimited("non-ECT from %pI6\n", 1670 &((struct ipv6hdr *)oiph)->saddr); 1671 } 1672 return err <= 1; 1673 } 1674 1675 static int vxlan_rcv(struct sock *sk, struct sk_buff *skb) 1676 { 1677 struct vxlan_vni_node *vninode = NULL; 1678 __be16 protocol = htons(ETH_P_TEB); 1679 const struct vxlan_config *cfg; 1680 enum skb_drop_reason reason; 1681 const struct vxlanhdr *vh; 1682 struct vxlan_metadata *md; 1683 struct vxlan_metadata _md; 1684 struct vxlan_dev *vxlan; 1685 bool raw_proto = false; 1686 struct vxlan_sock *vs; 1687 __be32 vni = 0; 1688 void *oiph; 1689 int nh; 1690 1691 md = &_md; 1692 1693 /* Need UDP and VXLAN header to be present */ 1694 reason = pskb_may_pull_reason(skb, VXLAN_HLEN); 1695 if (reason) 1696 goto drop; 1697 1698 vh = vxlan_hdr(skb); 1699 /* VNI flag always required to be set */ 1700 if (!(vh->vx_flags & VXLAN_HF_VNI)) { 1701 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n", 1702 ntohl(vh->vx_flags), ntohl(vh->vx_vni)); 1703 reason = SKB_DROP_REASON_VXLAN_INVALID_HDR; 1704 /* Return non vxlan pkt */ 1705 goto drop; 1706 } 1707 1708 vs = rcu_dereference_sk_user_data(sk); 1709 if (!vs) 1710 goto drop; 1711 1712 vni = vxlan_vni(vh->vx_vni); 1713 1714 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni, &vninode); 1715 if (!vxlan) { 1716 reason = SKB_DROP_REASON_VXLAN_VNI_NOT_FOUND; 1717 goto drop; 1718 } 1719 1720 cfg = rcu_dereference(vxlan->cfg); 1721 1722 if (vh->vx_flags & cfg->reserved_bits.vx_flags || 1723 vh->vx_vni & cfg->reserved_bits.vx_vni) { 1724 /* If the header uses bits besides those enabled by the 1725 * netdevice configuration, treat this as a malformed packet. 1726 * This behavior diverges from VXLAN RFC (RFC7348) which 1727 * stipulates that bits in reserved in reserved fields are to be 1728 * ignored. The approach here maintains compatibility with 1729 * previous stack code, and also is more robust and provides a 1730 * little more security in adding extensions to VXLAN. 1731 */ 1732 reason = SKB_DROP_REASON_VXLAN_INVALID_HDR; 1733 DEV_STATS_INC(vxlan->dev, rx_frame_errors); 1734 DEV_STATS_INC(vxlan->dev, rx_errors); 1735 vxlan_vnifilter_count(vxlan, cfg, vni, vninode, 1736 VXLAN_VNI_STATS_RX_ERRORS, 0); 1737 goto drop; 1738 } 1739 1740 if (cfg->flags & VXLAN_F_GPE) { 1741 if (!vxlan_parse_gpe_proto(vh, &protocol)) 1742 goto drop; 1743 raw_proto = true; 1744 } 1745 1746 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto, 1747 !net_eq(vxlan->net, dev_net(vxlan->dev)))) { 1748 reason = SKB_DROP_REASON_NOMEM; 1749 goto drop; 1750 } 1751 1752 if (cfg->flags & VXLAN_F_REMCSUM_RX) { 1753 reason = vxlan_remcsum(skb, cfg->flags); 1754 if (unlikely(reason)) 1755 goto drop; 1756 } 1757 1758 if (vxlan_collect_metadata(vs)) { 1759 IP_TUNNEL_DECLARE_FLAGS(flags) = { }; 1760 struct metadata_dst *tun_dst; 1761 1762 __set_bit(IP_TUNNEL_KEY_BIT, flags); 1763 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), flags, 1764 key32_to_tunnel_id(vni), sizeof(*md)); 1765 1766 if (!tun_dst) { 1767 reason = SKB_DROP_REASON_NOMEM; 1768 goto drop; 1769 } 1770 1771 md = ip_tunnel_info_opts(&tun_dst->u.tun_info); 1772 1773 skb_dst_set(skb, (struct dst_entry *)tun_dst); 1774 } else { 1775 memset(md, 0, sizeof(*md)); 1776 } 1777 1778 if (cfg->flags & VXLAN_F_GBP) 1779 vxlan_parse_gbp_hdr(skb, cfg->flags, md); 1780 /* Note that GBP and GPE can never be active together. This is 1781 * ensured in vxlan_dev_configure. 1782 */ 1783 1784 if (!raw_proto) { 1785 reason = vxlan_set_mac(vxlan, cfg, vs, skb, vni); 1786 if (reason) 1787 goto drop; 1788 } else { 1789 skb_reset_mac_header(skb); 1790 skb->dev = vxlan->dev; 1791 skb->pkt_type = PACKET_HOST; 1792 } 1793 1794 /* Save offset of outer header relative to skb->head, 1795 * because we are going to reset the network header to the inner header 1796 * and might change skb->head. 1797 */ 1798 nh = skb_network_header(skb) - skb->head; 1799 1800 skb_reset_network_header(skb); 1801 1802 reason = pskb_inet_may_pull_reason(skb); 1803 if (reason) { 1804 DEV_STATS_INC(vxlan->dev, rx_length_errors); 1805 DEV_STATS_INC(vxlan->dev, rx_errors); 1806 vxlan_vnifilter_count(vxlan, cfg, vni, vninode, 1807 VXLAN_VNI_STATS_RX_ERRORS, 0); 1808 goto drop; 1809 } 1810 1811 /* Get the outer header. */ 1812 oiph = skb->head + nh; 1813 1814 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) { 1815 reason = SKB_DROP_REASON_IP_TUNNEL_ECN; 1816 DEV_STATS_INC(vxlan->dev, rx_frame_errors); 1817 DEV_STATS_INC(vxlan->dev, rx_errors); 1818 vxlan_vnifilter_count(vxlan, cfg, vni, vninode, 1819 VXLAN_VNI_STATS_RX_ERRORS, 0); 1820 goto drop; 1821 } 1822 1823 rcu_read_lock(); 1824 1825 if (unlikely(!(vxlan->dev->flags & IFF_UP))) { 1826 rcu_read_unlock(); 1827 dev_dstats_rx_dropped(vxlan->dev); 1828 vxlan_vnifilter_count(vxlan, cfg, vni, vninode, 1829 VXLAN_VNI_STATS_RX_DROPS, 0); 1830 reason = SKB_DROP_REASON_DEV_READY; 1831 goto drop; 1832 } 1833 1834 dev_dstats_rx_add(vxlan->dev, skb->len); 1835 vxlan_vnifilter_count(vxlan, cfg, vni, vninode, VXLAN_VNI_STATS_RX, 1836 skb->len); 1837 gro_cells_receive(&vxlan->gro_cells, skb); 1838 1839 rcu_read_unlock(); 1840 1841 return 0; 1842 1843 drop: 1844 reason = reason ?: SKB_DROP_REASON_NOT_SPECIFIED; 1845 /* Consume bad packet */ 1846 kfree_skb_reason(skb, reason); 1847 return 0; 1848 } 1849 1850 static int vxlan_err_lookup(struct sock *sk, struct sk_buff *skb) 1851 { 1852 struct vxlan_dev *vxlan; 1853 struct vxlan_sock *vs; 1854 struct vxlanhdr *hdr; 1855 __be32 vni; 1856 1857 if (!pskb_may_pull(skb, skb_transport_offset(skb) + VXLAN_HLEN)) 1858 return -EINVAL; 1859 1860 hdr = vxlan_hdr(skb); 1861 1862 if (!(hdr->vx_flags & VXLAN_HF_VNI)) 1863 return -EINVAL; 1864 1865 vs = rcu_dereference_sk_user_data(sk); 1866 if (!vs) 1867 return -ENOENT; 1868 1869 vni = vxlan_vni(hdr->vx_vni); 1870 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni, NULL); 1871 if (!vxlan) 1872 return -ENOENT; 1873 1874 return 0; 1875 } 1876 1877 static int arp_reduce(struct net_device *dev, struct sk_buff *skb, 1878 const struct vxlan_config *cfg, __be32 vni) 1879 { 1880 struct vxlan_dev *vxlan = netdev_priv(dev); 1881 struct neighbour *n; 1882 struct arphdr *parp; 1883 u8 *arpptr, *sha; 1884 __be32 sip, tip; 1885 1886 if (dev->flags & IFF_NOARP) 1887 goto out; 1888 1889 if (!pskb_network_may_pull(skb, arp_hdr_len(dev))) { 1890 dev_dstats_tx_dropped(dev); 1891 vxlan_vnifilter_count(vxlan, cfg, vni, NULL, 1892 VXLAN_VNI_STATS_TX_DROPS, 0); 1893 goto out; 1894 } 1895 parp = arp_hdr(skb); 1896 1897 if ((parp->ar_hrd != htons(ARPHRD_ETHER) && 1898 parp->ar_hrd != htons(ARPHRD_IEEE802)) || 1899 parp->ar_pro != htons(ETH_P_IP) || 1900 parp->ar_op != htons(ARPOP_REQUEST) || 1901 parp->ar_hln != dev->addr_len || 1902 parp->ar_pln != 4) 1903 goto out; 1904 arpptr = (u8 *)parp + sizeof(struct arphdr); 1905 sha = arpptr; 1906 arpptr += dev->addr_len; /* sha */ 1907 memcpy(&sip, arpptr, sizeof(sip)); 1908 arpptr += sizeof(sip); 1909 arpptr += dev->addr_len; /* tha */ 1910 memcpy(&tip, arpptr, sizeof(tip)); 1911 1912 if (ipv4_is_loopback(tip) || 1913 ipv4_is_multicast(tip)) 1914 goto out; 1915 1916 n = ipv4_neigh_lookup(dev, &tip); 1917 if (n) { 1918 struct vxlan_rdst *rdst = NULL; 1919 u8 ha[ETH_ALEN] __aligned(2); 1920 struct vxlan_fdb *f; 1921 struct sk_buff *reply; 1922 1923 if (!(READ_ONCE(n->nud_state) & NUD_CONNECTED)) { 1924 neigh_release(n); 1925 goto out; 1926 } 1927 1928 neigh_ha_snapshot(ha, n, n->dev); 1929 1930 rcu_read_lock(); 1931 f = vxlan_find_mac_tx(vxlan, cfg, ha, vni); 1932 if (f) 1933 rdst = first_remote_rcu(f); 1934 if (rdst && vxlan_addr_any(&rdst->remote_ip)) { 1935 /* bridge-local neighbor */ 1936 neigh_release(n); 1937 rcu_read_unlock(); 1938 goto out; 1939 } 1940 rcu_read_unlock(); 1941 1942 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha, 1943 ha, sha); 1944 1945 neigh_release(n); 1946 1947 if (reply == NULL) 1948 goto out; 1949 1950 skb_reset_mac_header(reply); 1951 __skb_pull(reply, skb_network_offset(reply)); 1952 reply->ip_summed = CHECKSUM_UNNECESSARY; 1953 reply->pkt_type = PACKET_HOST; 1954 1955 if (netif_rx(reply) == NET_RX_DROP) { 1956 dev_dstats_rx_dropped(dev); 1957 vxlan_vnifilter_count(vxlan, cfg, vni, NULL, 1958 VXLAN_VNI_STATS_RX_DROPS, 0); 1959 } 1960 1961 } else if (cfg->flags & VXLAN_F_L3MISS) { 1962 union vxlan_addr ipa = { 1963 .sin.sin_addr.s_addr = tip, 1964 .sin.sin_family = AF_INET, 1965 }; 1966 1967 vxlan_ip_miss(dev, &ipa); 1968 } 1969 out: 1970 consume_skb(skb); 1971 return NETDEV_TX_OK; 1972 } 1973 1974 #if IS_ENABLED(CONFIG_IPV6) 1975 static struct sk_buff *vxlan_na_create(struct sk_buff *request, 1976 struct neighbour *n, u8 *ha, 1977 bool isrouter) 1978 { 1979 struct net_device *dev = request->dev; 1980 struct sk_buff *reply; 1981 struct nd_msg *ns, *na; 1982 struct ipv6hdr *pip6; 1983 u8 *daddr; 1984 int na_olen = 8; /* opt hdr + ETH_ALEN for target */ 1985 int headroom; 1986 int ns_olen; 1987 int i, len; 1988 1989 if (dev == NULL || !pskb_may_pull(request, request->len)) 1990 return NULL; 1991 1992 headroom = LL_RESERVED_SPACE(dev); 1993 len = headroom + sizeof(struct ipv6hdr) + 1994 sizeof(*na) + na_olen + dev->needed_tailroom; 1995 reply = alloc_skb(len, GFP_ATOMIC); 1996 if (reply == NULL) 1997 return NULL; 1998 1999 reply->protocol = htons(ETH_P_IPV6); 2000 reply->dev = dev; 2001 skb_reserve(reply, headroom); 2002 skb_push(reply, sizeof(struct ethhdr)); 2003 skb_reset_mac_header(reply); 2004 2005 ns = (struct nd_msg *)(ipv6_hdr(request) + 1); 2006 2007 daddr = eth_hdr(request)->h_source; 2008 ns_olen = request->len - skb_network_offset(request) - 2009 sizeof(struct ipv6hdr) - sizeof(*ns); 2010 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) { 2011 if (!ns->opt[i + 1] || i + (ns->opt[i + 1] << 3) > ns_olen) { 2012 kfree_skb(reply); 2013 return NULL; 2014 } 2015 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) { 2016 if ((ns->opt[i + 1] << 3) >= 2017 sizeof(struct nd_opt_hdr) + ETH_ALEN) 2018 daddr = ns->opt + i + sizeof(struct nd_opt_hdr); 2019 break; 2020 } 2021 } 2022 2023 /* Ethernet header */ 2024 ether_addr_copy(eth_hdr(reply)->h_dest, daddr); 2025 ether_addr_copy(eth_hdr(reply)->h_source, ha); 2026 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6); 2027 reply->protocol = htons(ETH_P_IPV6); 2028 2029 skb_pull(reply, sizeof(struct ethhdr)); 2030 skb_reset_network_header(reply); 2031 skb_put(reply, sizeof(struct ipv6hdr)); 2032 2033 /* IPv6 header */ 2034 2035 pip6 = ipv6_hdr(reply); 2036 memset(pip6, 0, sizeof(struct ipv6hdr)); 2037 pip6->version = 6; 2038 pip6->priority = ipv6_hdr(request)->priority; 2039 pip6->nexthdr = IPPROTO_ICMPV6; 2040 pip6->hop_limit = 255; 2041 pip6->daddr = ipv6_hdr(request)->saddr; 2042 pip6->saddr = *(struct in6_addr *)n->primary_key; 2043 2044 skb_pull(reply, sizeof(struct ipv6hdr)); 2045 skb_reset_transport_header(reply); 2046 2047 /* Neighbor Advertisement */ 2048 na = skb_put_zero(reply, sizeof(*na) + na_olen); 2049 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT; 2050 na->icmph.icmp6_router = isrouter; 2051 na->icmph.icmp6_override = 1; 2052 na->icmph.icmp6_solicited = 1; 2053 na->target = ns->target; 2054 ether_addr_copy(&na->opt[2], ha); 2055 na->opt[0] = ND_OPT_TARGET_LL_ADDR; 2056 na->opt[1] = na_olen >> 3; 2057 2058 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr, 2059 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6, 2060 csum_partial(na, sizeof(*na)+na_olen, 0)); 2061 2062 pip6->payload_len = htons(sizeof(*na)+na_olen); 2063 2064 skb_push(reply, sizeof(struct ipv6hdr)); 2065 2066 reply->ip_summed = CHECKSUM_UNNECESSARY; 2067 2068 return reply; 2069 } 2070 2071 static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, 2072 const struct vxlan_config *cfg, __be32 vni) 2073 { 2074 struct vxlan_dev *vxlan = netdev_priv(dev); 2075 const struct in6_addr *daddr; 2076 const struct ipv6hdr *iphdr; 2077 struct neighbour *n; 2078 struct nd_msg *msg; 2079 2080 rcu_read_lock(); 2081 if (unlikely(!ipv6_mod_enabled())) 2082 goto out; 2083 2084 iphdr = ipv6_hdr(skb); 2085 daddr = &iphdr->daddr; 2086 msg = (struct nd_msg *)(iphdr + 1); 2087 2088 if (ipv6_addr_loopback(daddr) || 2089 ipv6_addr_is_multicast(&msg->target)) 2090 goto out; 2091 2092 n = ipv6_neigh_lookup(dev, &msg->target); 2093 if (n) { 2094 struct vxlan_rdst *rdst = NULL; 2095 u8 ha[ETH_ALEN] __aligned(2); 2096 struct vxlan_fdb *f; 2097 struct sk_buff *reply; 2098 2099 if (!(READ_ONCE(n->nud_state) & NUD_CONNECTED)) { 2100 neigh_release(n); 2101 goto out; 2102 } 2103 2104 neigh_ha_snapshot(ha, n, n->dev); 2105 f = vxlan_find_mac_tx(vxlan, cfg, ha, vni); 2106 if (f) 2107 rdst = first_remote_rcu(f); 2108 if (rdst && vxlan_addr_any(&rdst->remote_ip)) { 2109 /* bridge-local neighbor */ 2110 neigh_release(n); 2111 goto out; 2112 } 2113 2114 reply = vxlan_na_create(skb, n, ha, 2115 !!(f ? f->flags & NTF_ROUTER : 0)); 2116 2117 neigh_release(n); 2118 2119 if (reply == NULL) 2120 goto out; 2121 2122 if (netif_rx(reply) == NET_RX_DROP) { 2123 dev_dstats_rx_dropped(dev); 2124 vxlan_vnifilter_count(vxlan, cfg, vni, NULL, 2125 VXLAN_VNI_STATS_RX_DROPS, 0); 2126 } 2127 } else if (cfg->flags & VXLAN_F_L3MISS) { 2128 union vxlan_addr ipa = { 2129 .sin6.sin6_addr = msg->target, 2130 .sin6.sin6_family = AF_INET6, 2131 }; 2132 2133 vxlan_ip_miss(dev, &ipa); 2134 } 2135 2136 out: 2137 rcu_read_unlock(); 2138 consume_skb(skb); 2139 return NETDEV_TX_OK; 2140 } 2141 #endif 2142 2143 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb, 2144 const struct vxlan_config *cfg) 2145 { 2146 struct neighbour *n; 2147 2148 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest)) 2149 return false; 2150 2151 n = NULL; 2152 switch (ntohs(eth_hdr(skb)->h_proto)) { 2153 case ETH_P_IP: 2154 { 2155 struct iphdr *pip; 2156 2157 if (!pskb_network_may_pull(skb, sizeof(struct iphdr))) 2158 return false; 2159 2160 pip = ip_hdr(skb); 2161 n = ipv4_neigh_lookup(dev, &pip->daddr); 2162 if (!n && (cfg->flags & VXLAN_F_L3MISS)) { 2163 union vxlan_addr ipa = { 2164 .sin.sin_addr.s_addr = pip->daddr, 2165 .sin.sin_family = AF_INET, 2166 }; 2167 2168 vxlan_ip_miss(dev, &ipa); 2169 return false; 2170 } 2171 2172 break; 2173 } 2174 #if IS_ENABLED(CONFIG_IPV6) 2175 case ETH_P_IPV6: 2176 { 2177 struct ipv6hdr *pip6; 2178 2179 /* check if ipv6.disable=1 set during boot was set 2180 * during booting so nd_tbl is not initialized 2181 */ 2182 if (!ipv6_mod_enabled()) 2183 return false; 2184 if (!pskb_network_may_pull(skb, sizeof(struct ipv6hdr))) 2185 return false; 2186 2187 pip6 = ipv6_hdr(skb); 2188 n = ipv6_neigh_lookup(dev, &pip6->daddr); 2189 if (!n && (cfg->flags & VXLAN_F_L3MISS)) { 2190 union vxlan_addr ipa = { 2191 .sin6.sin6_addr = pip6->daddr, 2192 .sin6.sin6_family = AF_INET6, 2193 }; 2194 2195 vxlan_ip_miss(dev, &ipa); 2196 return false; 2197 } 2198 2199 break; 2200 } 2201 #endif 2202 default: 2203 return false; 2204 } 2205 2206 if (n) { 2207 u8 haddr[ETH_ALEN]; 2208 bool diff; 2209 2210 neigh_ha_snapshot(haddr, n, dev); 2211 diff = !ether_addr_equal_unaligned(eth_hdr(skb)->h_dest, haddr); 2212 if (diff) { 2213 if (skb_cow_head(skb, 0)) { 2214 neigh_release(n); 2215 return false; 2216 } 2217 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest, 2218 dev->addr_len); 2219 memcpy(eth_hdr(skb)->h_dest, haddr, dev->addr_len); 2220 } 2221 neigh_release(n); 2222 return diff; 2223 } 2224 2225 return false; 2226 } 2227 2228 static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, __be16 protocol) 2229 { 2230 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh; 2231 2232 gpe->np_applied = 1; 2233 gpe->next_protocol = tun_p_from_eth_p(protocol); 2234 if (!gpe->next_protocol) 2235 return -EPFNOSUPPORT; 2236 return 0; 2237 } 2238 2239 static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst, 2240 int iphdr_len, __be32 vni, 2241 struct vxlan_metadata *md, u32 vxflags, 2242 bool udp_sum) 2243 { 2244 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL; 2245 __be16 inner_protocol = htons(ETH_P_TEB); 2246 struct vxlanhdr *vxh; 2247 bool double_encap; 2248 int min_headroom; 2249 int err; 2250 2251 if ((vxflags & VXLAN_F_REMCSUM_TX) && 2252 skb->ip_summed == CHECKSUM_PARTIAL) { 2253 int csum_start = skb_checksum_start_offset(skb); 2254 2255 if (csum_start <= VXLAN_MAX_REMCSUM_START && 2256 !(csum_start & VXLAN_RCO_SHIFT_MASK) && 2257 (skb->csum_offset == offsetof(struct udphdr, check) || 2258 skb->csum_offset == offsetof(struct tcphdr, check))) 2259 type |= SKB_GSO_TUNNEL_REMCSUM; 2260 } 2261 2262 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len 2263 + VXLAN_HLEN + iphdr_len; 2264 2265 /* Need space for new headers (invalidates iph ptr) */ 2266 err = skb_cow_head(skb, min_headroom); 2267 if (unlikely(err)) 2268 return err; 2269 2270 double_encap = udp_tunnel_handle_partial(skb); 2271 err = iptunnel_handle_offloads(skb, type); 2272 if (err) 2273 return err; 2274 2275 vxh = __skb_push(skb, sizeof(*vxh)); 2276 vxh->vx_flags = VXLAN_HF_VNI; 2277 vxh->vx_vni = vxlan_vni_field(vni); 2278 2279 if (type & SKB_GSO_TUNNEL_REMCSUM) { 2280 unsigned int start; 2281 2282 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr); 2283 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset); 2284 vxh->vx_flags |= VXLAN_HF_RCO; 2285 2286 if (!skb_is_gso(skb)) { 2287 skb->ip_summed = CHECKSUM_NONE; 2288 skb->encapsulation = 0; 2289 } 2290 } 2291 2292 if (vxflags & VXLAN_F_GBP) 2293 vxlan_build_gbp_hdr(vxh, md); 2294 if (vxflags & VXLAN_F_GPE) { 2295 err = vxlan_build_gpe_hdr(vxh, skb->protocol); 2296 if (err < 0) 2297 return err; 2298 inner_protocol = skb->protocol; 2299 } 2300 2301 udp_tunnel_set_inner_protocol(skb, double_encap, inner_protocol); 2302 return 0; 2303 } 2304 2305 /* Bypass encapsulation if the destination is local */ 2306 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan, 2307 struct vxlan_dev *dst_vxlan, 2308 const struct vxlan_config *src_cfg, 2309 __be32 vni, bool snoop) 2310 { 2311 const struct vxlan_config *dst_cfg; 2312 union vxlan_addr loopback; 2313 unsigned int len = skb->len; 2314 struct net_device *dev = dst_vxlan->dev; 2315 2316 skb->pkt_type = PACKET_HOST; 2317 skb->encapsulation = 0; 2318 skb->dev = dev; 2319 __skb_pull(skb, skb_network_offset(skb)); 2320 2321 rcu_read_lock(); 2322 dst_cfg = rcu_dereference(dst_vxlan->cfg); 2323 if (unlikely(!(dev->flags & IFF_UP))) { 2324 kfree_skb_reason(skb, SKB_DROP_REASON_DEV_READY); 2325 goto drop; 2326 } 2327 2328 if (dst_cfg->remote_ip.sa.sa_family == AF_INET) { 2329 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 2330 loopback.sa.sa_family = AF_INET; 2331 #if IS_ENABLED(CONFIG_IPV6) 2332 } else { 2333 loopback.sin6.sin6_addr = in6addr_loopback; 2334 loopback.sa.sa_family = AF_INET6; 2335 #endif 2336 } 2337 2338 if ((dst_cfg->flags & VXLAN_F_LEARN) && snoop) 2339 vxlan_snoop(dev, dst_cfg, &loopback, eth_hdr(skb)->h_source, 0, vni); 2340 2341 dev_dstats_tx_add(src_vxlan->dev, len); 2342 vxlan_vnifilter_count(src_vxlan, src_cfg, vni, NULL, VXLAN_VNI_STATS_TX, len); 2343 2344 if (__netif_rx(skb) == NET_RX_SUCCESS) { 2345 dev_dstats_rx_add(dst_vxlan->dev, len); 2346 vxlan_vnifilter_count(dst_vxlan, dst_cfg, vni, NULL, VXLAN_VNI_STATS_RX, 2347 len); 2348 } else { 2349 drop: 2350 dev_dstats_rx_dropped(dev); 2351 vxlan_vnifilter_count(dst_vxlan, dst_cfg, vni, NULL, 2352 VXLAN_VNI_STATS_RX_DROPS, 0); 2353 } 2354 rcu_read_unlock(); 2355 } 2356 2357 static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev, 2358 struct vxlan_dev *vxlan, 2359 const struct vxlan_config *cfg, 2360 int addr_family, 2361 __be16 dst_port, int dst_ifindex, __be32 vni, 2362 struct dst_entry *dst, 2363 u32 rt_flags) 2364 { 2365 #if IS_ENABLED(CONFIG_IPV6) 2366 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of 2367 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple 2368 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry. 2369 */ 2370 BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL); 2371 #endif 2372 /* Bypass encapsulation if the destination is local */ 2373 if (rt_flags & RTCF_LOCAL && 2374 !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) && 2375 cfg->flags & VXLAN_F_LOCALBYPASS) { 2376 struct vxlan_dev *dst_vxlan; 2377 2378 dst_release(dst); 2379 dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni, 2380 addr_family, dst_port, 2381 cfg->flags); 2382 if (!dst_vxlan) { 2383 DEV_STATS_INC(dev, tx_errors); 2384 vxlan_vnifilter_count(vxlan, cfg, vni, NULL, 2385 VXLAN_VNI_STATS_TX_ERRORS, 0); 2386 kfree_skb_reason(skb, SKB_DROP_REASON_VXLAN_VNI_NOT_FOUND); 2387 2388 return -ENOENT; 2389 } 2390 vxlan_encap_bypass(skb, vxlan, dst_vxlan, cfg, vni, true); 2391 return 1; 2392 } 2393 2394 return 0; 2395 } 2396 2397 void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, 2398 const struct vxlan_config *cfg, 2399 __be32 default_vni, struct vxlan_rdst *rdst, bool did_rsc) 2400 { 2401 unsigned int pkt_len = skb->len; 2402 struct vxlan_metadata _md = {}; 2403 __be16 src_port = 0, dst_port; 2404 struct dst_entry *ndst = NULL; 2405 enum skb_drop_reason reason; 2406 struct dst_cache *dst_cache; 2407 const struct iphdr *old_iph; 2408 struct ip_tunnel_info *info; 2409 struct ip_tunnel_key *pkey; 2410 struct vxlan_metadata *md; 2411 struct ip_tunnel_key key; 2412 struct vxlan_dev *vxlan; 2413 u32 flags = cfg->flags; 2414 bool udp_sum = false; 2415 bool no_eth_encap; 2416 int addr_family; 2417 bool use_cache; 2418 __be32 vni = 0; 2419 __u8 tos, ttl; 2420 int ifindex; 2421 int err = 0; 2422 bool xnet; 2423 2424 vxlan = netdev_priv(dev); 2425 xnet = !net_eq(vxlan->net, dev_net(vxlan->dev)); 2426 md = &_md; 2427 2428 no_eth_encap = flags & VXLAN_F_GPE && skb->protocol != htons(ETH_P_TEB); 2429 reason = skb_vlan_inet_prepare(skb, no_eth_encap); 2430 if (reason) 2431 goto drop; 2432 2433 reason = SKB_DROP_REASON_NOT_SPECIFIED; 2434 old_iph = ip_hdr(skb); 2435 2436 info = skb_tunnel_info(skb); 2437 use_cache = ip_tunnel_dst_cache_usable(skb, info); 2438 2439 if (rdst) { 2440 memset(&key, 0, sizeof(key)); 2441 pkey = &key; 2442 2443 if (vxlan_addr_any(&rdst->remote_ip)) { 2444 if (did_rsc) { 2445 /* short-circuited back to local bridge */ 2446 vxlan_encap_bypass(skb, vxlan, vxlan, cfg, 2447 default_vni, true); 2448 return; 2449 } 2450 goto drop; 2451 } 2452 2453 addr_family = cfg->saddr.sa.sa_family; 2454 dst_port = rdst->remote_port ? rdst->remote_port : cfg->dst_port; 2455 vni = (rdst->remote_vni) ? : default_vni; 2456 ifindex = rdst->remote_ifindex; 2457 2458 if (addr_family == AF_INET) { 2459 key.u.ipv4.src = cfg->saddr.sin.sin_addr.s_addr; 2460 key.u.ipv4.dst = rdst->remote_ip.sin.sin_addr.s_addr; 2461 } else { 2462 key.u.ipv6.src = cfg->saddr.sin6.sin6_addr; 2463 key.u.ipv6.dst = rdst->remote_ip.sin6.sin6_addr; 2464 } 2465 2466 dst_cache = &rdst->dst_cache; 2467 md->gbp = skb->mark; 2468 if (flags & VXLAN_F_TTL_INHERIT) { 2469 ttl = ip_tunnel_get_ttl(old_iph, skb); 2470 } else { 2471 ttl = cfg->ttl; 2472 if (!ttl && vxlan_addr_multicast(&rdst->remote_ip)) 2473 ttl = 1; 2474 } 2475 tos = cfg->tos; 2476 if (tos == 1) 2477 tos = ip_tunnel_get_dsfield(old_iph, skb); 2478 if (tos && !info) 2479 use_cache = false; 2480 2481 if (addr_family == AF_INET) 2482 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX); 2483 else 2484 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX); 2485 #if IS_ENABLED(CONFIG_IPV6) 2486 switch (cfg->label_policy) { 2487 case VXLAN_LABEL_FIXED: 2488 key.label = cfg->label; 2489 break; 2490 case VXLAN_LABEL_INHERIT: 2491 key.label = ip_tunnel_get_flowlabel(old_iph, skb); 2492 break; 2493 default: 2494 DEBUG_NET_WARN_ON_ONCE(1); 2495 goto drop; 2496 } 2497 #endif 2498 } else { 2499 if (!info) { 2500 WARN_ONCE(1, "%s: Missing encapsulation instructions\n", 2501 dev->name); 2502 goto drop; 2503 } 2504 pkey = &info->key; 2505 addr_family = ip_tunnel_info_af(info); 2506 dst_port = info->key.tp_dst ? : cfg->dst_port; 2507 vni = tunnel_id_to_key32(info->key.tun_id); 2508 ifindex = 0; 2509 dst_cache = &info->dst_cache; 2510 if (test_bit(IP_TUNNEL_VXLAN_OPT_BIT, info->key.tun_flags)) { 2511 if (info->options_len < sizeof(*md)) 2512 goto drop; 2513 md = ip_tunnel_info_opts(info); 2514 } 2515 ttl = info->key.ttl; 2516 tos = info->key.tos; 2517 udp_sum = test_bit(IP_TUNNEL_CSUM_BIT, info->key.tun_flags); 2518 } 2519 src_port = udp_flow_src_port(dev_net(dev), skb, cfg->port_min, 2520 cfg->port_max, true); 2521 2522 rcu_read_lock(); 2523 if (addr_family == AF_INET) { 2524 struct vxlan_sock *sock4; 2525 u16 ipcb_flags = 0; 2526 struct rtable *rt; 2527 __be16 df = 0; 2528 __be32 saddr; 2529 2530 sock4 = rcu_dereference(vxlan->vn4_sock); 2531 if (unlikely(!sock4)) { 2532 reason = SKB_DROP_REASON_DEV_READY; 2533 goto tx_error; 2534 } 2535 2536 if (!ifindex) 2537 ifindex = sock4->sk->sk_bound_dev_if; 2538 2539 rt = udp_tunnel_dst_lookup(skb, dev, vxlan->net, ifindex, 2540 &saddr, pkey, src_port, dst_port, 2541 tos, use_cache ? dst_cache : NULL); 2542 if (IS_ERR(rt)) { 2543 err = PTR_ERR(rt); 2544 reason = SKB_DROP_REASON_IP_OUTNOROUTES; 2545 goto tx_error; 2546 } 2547 2548 if (flags & VXLAN_F_MC_ROUTE) 2549 ipcb_flags |= IPSKB_MCROUTE; 2550 2551 if (!info) { 2552 /* Bypass encapsulation if the destination is local */ 2553 err = encap_bypass_if_local(skb, dev, vxlan, cfg, AF_INET, 2554 dst_port, ifindex, vni, 2555 &rt->dst, rt->rt_flags); 2556 if (err) 2557 goto out_unlock; 2558 2559 if (cfg->df == VXLAN_DF_SET) { 2560 df = htons(IP_DF); 2561 } else if (cfg->df == VXLAN_DF_INHERIT) { 2562 struct ethhdr *eth = eth_hdr(skb); 2563 2564 if (ntohs(eth->h_proto) == ETH_P_IPV6 || 2565 (ntohs(eth->h_proto) == ETH_P_IP && 2566 old_iph->frag_off & htons(IP_DF))) 2567 df = htons(IP_DF); 2568 } 2569 } else if (test_bit(IP_TUNNEL_DONT_FRAGMENT_BIT, 2570 info->key.tun_flags)) { 2571 df = htons(IP_DF); 2572 } 2573 2574 ndst = &rt->dst; 2575 err = skb_tunnel_check_pmtu(skb, ndst, vxlan_headroom(flags & VXLAN_F_GPE), 2576 netif_is_any_bridge_port(dev)); 2577 if (err < 0) { 2578 goto tx_error; 2579 } else if (err) { 2580 if (info) { 2581 struct ip_tunnel_info *unclone; 2582 2583 unclone = skb_tunnel_info_unclone(skb); 2584 if (unlikely(!unclone)) 2585 goto tx_error; 2586 2587 unclone->key.u.ipv4.src = pkey->u.ipv4.dst; 2588 unclone->key.u.ipv4.dst = saddr; 2589 } 2590 vxlan_encap_bypass(skb, vxlan, vxlan, cfg, vni, false); 2591 dst_release(ndst); 2592 goto out_unlock; 2593 } 2594 2595 tos = ip_tunnel_ecn_encap(tos, ip_hdr(skb), skb); 2596 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst); 2597 err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr), 2598 vni, md, flags, udp_sum); 2599 if (err < 0) { 2600 reason = SKB_DROP_REASON_NOMEM; 2601 goto tx_error; 2602 } 2603 2604 udp_tunnel_xmit_skb(rt, sock4->sk, skb, saddr, 2605 pkey->u.ipv4.dst, tos, ttl, df, 2606 src_port, dst_port, xnet, !udp_sum, 2607 ipcb_flags); 2608 #if IS_ENABLED(CONFIG_IPV6) 2609 } else { 2610 struct vxlan_sock *sock6; 2611 struct in6_addr saddr; 2612 u16 ip6cb_flags = 0; 2613 2614 sock6 = rcu_dereference(vxlan->vn6_sock); 2615 if (unlikely(!sock6)) { 2616 reason = SKB_DROP_REASON_DEV_READY; 2617 goto tx_error; 2618 } 2619 2620 if (!ifindex) 2621 ifindex = sock6->sk->sk_bound_dev_if; 2622 2623 ndst = udp_tunnel6_dst_lookup(skb, dev, vxlan->net, sock6->sk, 2624 ifindex, &saddr, pkey, 2625 src_port, dst_port, tos, 2626 use_cache ? dst_cache : NULL); 2627 if (IS_ERR(ndst)) { 2628 err = PTR_ERR(ndst); 2629 ndst = NULL; 2630 reason = SKB_DROP_REASON_IP_OUTNOROUTES; 2631 goto tx_error; 2632 } 2633 2634 if (flags & VXLAN_F_MC_ROUTE) 2635 ip6cb_flags |= IP6SKB_MCROUTE; 2636 2637 if (!info) { 2638 u32 rt6i_flags = dst_rt6_info(ndst)->rt6i_flags; 2639 2640 err = encap_bypass_if_local(skb, dev, vxlan, cfg, AF_INET6, 2641 dst_port, ifindex, vni, 2642 ndst, rt6i_flags); 2643 if (err) 2644 goto out_unlock; 2645 } 2646 2647 err = skb_tunnel_check_pmtu(skb, ndst, 2648 vxlan_headroom((flags & VXLAN_F_GPE) | VXLAN_F_IPV6), 2649 netif_is_any_bridge_port(dev)); 2650 if (err < 0) { 2651 goto tx_error; 2652 } else if (err) { 2653 if (info) { 2654 struct ip_tunnel_info *unclone; 2655 2656 unclone = skb_tunnel_info_unclone(skb); 2657 if (unlikely(!unclone)) 2658 goto tx_error; 2659 2660 unclone->key.u.ipv6.src = pkey->u.ipv6.dst; 2661 unclone->key.u.ipv6.dst = saddr; 2662 } 2663 2664 vxlan_encap_bypass(skb, vxlan, vxlan, cfg, vni, false); 2665 dst_release(ndst); 2666 goto out_unlock; 2667 } 2668 2669 tos = ip_tunnel_ecn_encap(tos, ip_hdr(skb), skb); 2670 ttl = ttl ? : ip6_dst_hoplimit(ndst); 2671 skb_scrub_packet(skb, xnet); 2672 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr), 2673 vni, md, flags, udp_sum); 2674 if (err < 0) { 2675 reason = SKB_DROP_REASON_NOMEM; 2676 goto tx_error; 2677 } 2678 2679 udp_tunnel6_xmit_skb(ndst, sock6->sk, skb, dev, 2680 &saddr, &pkey->u.ipv6.dst, tos, ttl, 2681 pkey->label, src_port, dst_port, !udp_sum, 2682 ip6cb_flags); 2683 #endif 2684 } 2685 vxlan_vnifilter_count(vxlan, cfg, vni, NULL, VXLAN_VNI_STATS_TX, pkt_len); 2686 out_unlock: 2687 rcu_read_unlock(); 2688 return; 2689 2690 drop: 2691 dev_dstats_tx_dropped(dev); 2692 vxlan_vnifilter_count(vxlan, cfg, vni, NULL, VXLAN_VNI_STATS_TX_DROPS, 0); 2693 kfree_skb_reason(skb, reason); 2694 return; 2695 2696 tx_error: 2697 rcu_read_unlock(); 2698 if (err == -ELOOP) 2699 DEV_STATS_INC(dev, collisions); 2700 else if (err == -ENETUNREACH) 2701 DEV_STATS_INC(dev, tx_carrier_errors); 2702 dst_release(ndst); 2703 DEV_STATS_INC(dev, tx_errors); 2704 vxlan_vnifilter_count(vxlan, cfg, vni, NULL, VXLAN_VNI_STATS_TX_ERRORS, 0); 2705 kfree_skb_reason(skb, reason); 2706 } 2707 2708 static void vxlan_xmit_nh(struct sk_buff *skb, struct net_device *dev, 2709 const struct vxlan_config *cfg, 2710 struct vxlan_fdb *f, __be32 vni, bool did_rsc) 2711 { 2712 struct vxlan_rdst nh_rdst; 2713 struct nexthop *nh; 2714 bool do_xmit; 2715 u32 hash; 2716 2717 memset(&nh_rdst, 0, sizeof(struct vxlan_rdst)); 2718 hash = skb_get_hash(skb); 2719 2720 nh = rcu_dereference(f->nh); 2721 if (!nh) 2722 goto drop; 2723 do_xmit = vxlan_fdb_nh_path_select(nh, hash, &nh_rdst); 2724 2725 if (likely(do_xmit)) 2726 vxlan_xmit_one(skb, dev, cfg, vni, &nh_rdst, did_rsc); 2727 else 2728 goto drop; 2729 2730 return; 2731 2732 drop: 2733 dev_dstats_tx_dropped(dev); 2734 vxlan_vnifilter_count(netdev_priv(dev), cfg, vni, NULL, 2735 VXLAN_VNI_STATS_TX_DROPS, 0); 2736 dev_kfree_skb(skb); 2737 } 2738 2739 static netdev_tx_t vxlan_xmit_nhid(struct sk_buff *skb, struct net_device *dev, 2740 u32 nhid, __be32 vni, 2741 const struct vxlan_config *cfg) 2742 { 2743 struct vxlan_rdst nh_rdst; 2744 struct nexthop *nh; 2745 bool do_xmit; 2746 u32 hash; 2747 2748 memset(&nh_rdst, 0, sizeof(struct vxlan_rdst)); 2749 hash = skb_get_hash(skb); 2750 2751 rcu_read_lock(); 2752 nh = nexthop_find_by_id(dev_net(dev), nhid); 2753 if (unlikely(!nh || !nexthop_is_fdb(nh) || !nexthop_is_multipath(nh))) { 2754 rcu_read_unlock(); 2755 goto drop; 2756 } 2757 do_xmit = vxlan_fdb_nh_path_select(nh, hash, &nh_rdst); 2758 rcu_read_unlock(); 2759 2760 if (cfg->saddr.sa.sa_family != nh_rdst.remote_ip.sa.sa_family) 2761 goto drop; 2762 2763 if (likely(do_xmit)) 2764 vxlan_xmit_one(skb, dev, cfg, vni, &nh_rdst, false); 2765 else 2766 goto drop; 2767 2768 return NETDEV_TX_OK; 2769 2770 drop: 2771 dev_dstats_tx_dropped(dev); 2772 vxlan_vnifilter_count(netdev_priv(dev), cfg, vni, NULL, 2773 VXLAN_VNI_STATS_TX_DROPS, 0); 2774 dev_kfree_skb(skb); 2775 return NETDEV_TX_OK; 2776 } 2777 2778 /* Transmit local packets over Vxlan 2779 * 2780 * Outer IP header inherits ECN and DF from inner header. 2781 * Outer UDP destination is the VXLAN assigned port. 2782 * source port is based on hash of flow 2783 */ 2784 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev) 2785 { 2786 struct vxlan_dev *vxlan = netdev_priv(dev); 2787 struct vxlan_rdst *rdst, *fdst = NULL; 2788 const struct ip_tunnel_info *info; 2789 const struct vxlan_config *cfg; 2790 __be32 default_vni; 2791 struct vxlan_fdb *f; 2792 struct ethhdr *eth; 2793 __be32 vni = 0; 2794 bool did_rsc; 2795 u32 nhid = 0; 2796 u32 flags; 2797 2798 rcu_read_lock(); 2799 cfg = rcu_dereference(vxlan->cfg); 2800 flags = cfg->flags; 2801 default_vni = cfg->vni; 2802 2803 info = skb_tunnel_info(skb); 2804 2805 skb_reset_mac_header(skb); 2806 2807 if (flags & VXLAN_F_COLLECT_METADATA) { 2808 if (info && info->mode & IP_TUNNEL_INFO_BRIDGE && 2809 info->mode & IP_TUNNEL_INFO_TX) { 2810 vni = tunnel_id_to_key32(info->key.tun_id); 2811 nhid = info->key.nhid; 2812 } else { 2813 if (info && info->mode & IP_TUNNEL_INFO_TX) 2814 vxlan_xmit_one(skb, dev, cfg, vni, NULL, false); 2815 else 2816 kfree_skb_reason(skb, SKB_DROP_REASON_TUNNEL_TXINFO); 2817 rcu_read_unlock(); 2818 return NETDEV_TX_OK; 2819 } 2820 } 2821 2822 if (flags & VXLAN_F_PROXY) { 2823 eth = eth_hdr(skb); 2824 if (ntohs(eth->h_proto) == ETH_P_ARP) { 2825 netdev_tx_t res = arp_reduce(dev, skb, cfg, vni); 2826 2827 rcu_read_unlock(); 2828 return res; 2829 } 2830 #if IS_ENABLED(CONFIG_IPV6) 2831 else if (ntohs(eth->h_proto) == ETH_P_IPV6 && 2832 pskb_network_may_pull(skb, sizeof(struct ipv6hdr) + 2833 sizeof(struct nd_msg)) && 2834 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) { 2835 struct nd_msg *m = (struct nd_msg *)(ipv6_hdr(skb) + 1); 2836 2837 if (m->icmph.icmp6_code == 0 && 2838 m->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) { 2839 netdev_tx_t res = neigh_reduce(dev, skb, cfg, vni); 2840 2841 rcu_read_unlock(); 2842 return res; 2843 } 2844 } 2845 #endif 2846 } 2847 2848 if (nhid) { 2849 netdev_tx_t res = vxlan_xmit_nhid(skb, dev, nhid, vni, cfg); 2850 2851 rcu_read_unlock(); 2852 return res; 2853 } 2854 2855 if (test_bit(VXLAN_DEV_F_MDB, &vxlan->flags)) { 2856 struct vxlan_mdb_entry *mdb_entry; 2857 2858 mdb_entry = vxlan_mdb_entry_skb_get(vxlan, cfg, skb, vni); 2859 if (mdb_entry) { 2860 netdev_tx_t ret = vxlan_mdb_xmit(vxlan, cfg, mdb_entry, skb); 2861 2862 rcu_read_unlock(); 2863 return ret; 2864 } 2865 } 2866 2867 eth = eth_hdr(skb); 2868 f = vxlan_find_mac_tx(vxlan, cfg, eth->h_dest, vni); 2869 did_rsc = false; 2870 2871 if (f && (f->flags & NTF_ROUTER) && (flags & VXLAN_F_RSC) && 2872 (ntohs(eth->h_proto) == ETH_P_IP || 2873 ntohs(eth->h_proto) == ETH_P_IPV6)) { 2874 did_rsc = route_shortcircuit(dev, skb, cfg); 2875 eth = eth_hdr(skb); 2876 if (did_rsc) 2877 f = vxlan_find_mac_tx(vxlan, cfg, eth->h_dest, vni); 2878 } 2879 2880 if (f == NULL) { 2881 f = vxlan_find_mac_tx(vxlan, cfg, all_zeros_mac, vni); 2882 if (f == NULL) { 2883 if ((flags & VXLAN_F_L2MISS) && 2884 !is_multicast_ether_addr(eth->h_dest)) 2885 vxlan_fdb_miss(vxlan, eth->h_dest); 2886 2887 dev_dstats_tx_dropped(dev); 2888 vxlan_vnifilter_count(vxlan, cfg, vni, NULL, 2889 VXLAN_VNI_STATS_TX_DROPS, 0); 2890 kfree_skb_reason(skb, SKB_DROP_REASON_NO_TX_TARGET); 2891 goto out; 2892 } 2893 } 2894 2895 if (rcu_access_pointer(f->nh)) { 2896 vxlan_xmit_nh(skb, dev, cfg, f, 2897 (vni ? : default_vni), did_rsc); 2898 } else { 2899 list_for_each_entry_rcu(rdst, &f->remotes, list) { 2900 struct sk_buff *skb1; 2901 2902 if (!fdst) { 2903 fdst = rdst; 2904 continue; 2905 } 2906 skb1 = skb_clone(skb, GFP_ATOMIC); 2907 if (skb1) 2908 vxlan_xmit_one(skb1, dev, cfg, vni, rdst, did_rsc); 2909 } 2910 if (fdst) 2911 vxlan_xmit_one(skb, dev, cfg, vni, fdst, did_rsc); 2912 else 2913 kfree_skb_reason(skb, SKB_DROP_REASON_NO_TX_TARGET); 2914 } 2915 2916 out: 2917 rcu_read_unlock(); 2918 return NETDEV_TX_OK; 2919 } 2920 2921 /* Walk the forwarding table and purge stale entries */ 2922 static void vxlan_cleanup(struct timer_list *t) 2923 { 2924 struct vxlan_dev *vxlan = timer_container_of(vxlan, t, age_timer); 2925 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL; 2926 const struct vxlan_config *cfg; 2927 struct vxlan_fdb *f; 2928 2929 if (!netif_running(vxlan->dev)) 2930 return; 2931 2932 rcu_read_lock(); 2933 cfg = rcu_dereference(vxlan->cfg); 2934 2935 hlist_for_each_entry_rcu(f, &vxlan->fdb_list, fdb_node) { 2936 unsigned long timeout; 2937 2938 if (f->state & (NUD_PERMANENT | NUD_NOARP)) 2939 continue; 2940 2941 if (f->flags & NTF_EXT_LEARNED) 2942 continue; 2943 2944 timeout = READ_ONCE(f->updated) + cfg->age_interval * HZ; 2945 if (time_before_eq(timeout, jiffies)) { 2946 spin_lock(&vxlan->hash_lock); 2947 if (!hlist_unhashed(&f->fdb_node)) { 2948 netdev_dbg(vxlan->dev, "garbage collect %pM\n", 2949 f->key.eth_addr); 2950 f->state = NUD_STALE; 2951 vxlan_fdb_destroy(vxlan, f, true, true); 2952 } 2953 spin_unlock(&vxlan->hash_lock); 2954 } else if (time_before(timeout, next_timer)) { 2955 next_timer = timeout; 2956 } 2957 } 2958 rcu_read_unlock(); 2959 2960 mod_timer(&vxlan->age_timer, next_timer); 2961 } 2962 2963 static void vxlan_vs_del_dev(struct vxlan_dev *vxlan) 2964 { 2965 ASSERT_RTNL(); 2966 2967 hlist_del_init_rcu(&vxlan->hlist4.hlist); 2968 #if IS_ENABLED(CONFIG_IPV6) 2969 hlist_del_init_rcu(&vxlan->hlist6.hlist); 2970 #endif 2971 } 2972 2973 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan, 2974 struct vxlan_dev_node *node) 2975 { 2976 const struct vxlan_config *cfg; 2977 __be32 vni; 2978 2979 ASSERT_RTNL(); 2980 2981 cfg = rtnl_dereference(vxlan->cfg); 2982 vni = cfg->vni; 2983 2984 node->vxlan = vxlan; 2985 hlist_add_head_rcu(&node->hlist, vni_head(vs, vni)); 2986 } 2987 2988 /* Setup stats when device is created */ 2989 static int vxlan_init(struct net_device *dev) 2990 { 2991 struct vxlan_dev *vxlan = netdev_priv(dev); 2992 const struct vxlan_config *cfg; 2993 int err; 2994 2995 cfg = rtnl_dereference(vxlan->cfg); 2996 2997 err = rhashtable_init(&vxlan->fdb_hash_tbl, &vxlan_fdb_rht_params); 2998 if (err) 2999 return err; 3000 3001 if (cfg->flags & VXLAN_F_VNIFILTER) { 3002 err = vxlan_vnigroup_init(vxlan); 3003 if (err) 3004 goto err_rhashtable_destroy; 3005 } 3006 3007 err = gro_cells_init(&vxlan->gro_cells, dev); 3008 if (err) 3009 goto err_vnigroup_uninit; 3010 3011 err = vxlan_mdb_init(vxlan); 3012 if (err) 3013 goto err_gro_cells_destroy; 3014 3015 netdev_lockdep_set_classes(dev); 3016 return 0; 3017 3018 err_gro_cells_destroy: 3019 gro_cells_destroy(&vxlan->gro_cells); 3020 err_vnigroup_uninit: 3021 if (cfg->flags & VXLAN_F_VNIFILTER) 3022 vxlan_vnigroup_uninit(vxlan); 3023 err_rhashtable_destroy: 3024 rhashtable_destroy(&vxlan->fdb_hash_tbl); 3025 return err; 3026 } 3027 3028 static void vxlan_uninit(struct net_device *dev) 3029 { 3030 struct vxlan_dev *vxlan = netdev_priv(dev); 3031 const struct vxlan_config *cfg; 3032 3033 cfg = rtnl_dereference(vxlan->cfg); 3034 3035 vxlan_mdb_fini(vxlan); 3036 3037 if (cfg->flags & VXLAN_F_VNIFILTER) 3038 vxlan_vnigroup_uninit(vxlan); 3039 3040 gro_cells_destroy(&vxlan->gro_cells); 3041 3042 rhashtable_destroy(&vxlan->fdb_hash_tbl); 3043 } 3044 3045 /* Start ageing timer and join group when device is brought up */ 3046 static int vxlan_open(struct net_device *dev) 3047 { 3048 struct vxlan_dev *vxlan = netdev_priv(dev); 3049 const struct vxlan_config *cfg; 3050 int ret; 3051 3052 ret = vxlan_sock_add(vxlan); 3053 if (ret < 0) 3054 return ret; 3055 3056 ret = vxlan_multicast_join(vxlan); 3057 if (ret) { 3058 vxlan_sock_release(vxlan); 3059 return ret; 3060 } 3061 3062 cfg = rtnl_dereference(vxlan->cfg); 3063 if (cfg->age_interval) 3064 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL); 3065 3066 return ret; 3067 } 3068 3069 struct vxlan_fdb_flush_desc { 3070 bool ignore_default_entry; 3071 unsigned long state; 3072 unsigned long state_mask; 3073 unsigned long flags; 3074 unsigned long flags_mask; 3075 __be32 src_vni; 3076 u32 nhid; 3077 __be32 vni; 3078 __be16 port; 3079 union vxlan_addr dst_ip; 3080 }; 3081 3082 static bool vxlan_fdb_is_default_entry(const struct vxlan_fdb *f, 3083 const struct vxlan_dev *vxlan) 3084 { 3085 const struct vxlan_config *cfg = rcu_dereference_rtnl(vxlan->cfg); 3086 3087 return is_zero_ether_addr(f->key.eth_addr) && 3088 f->key.vni == cfg->vni; 3089 } 3090 3091 static bool vxlan_fdb_nhid_matches(const struct vxlan_fdb *f, u32 nhid) 3092 { 3093 struct nexthop *nh = rtnl_dereference(f->nh); 3094 3095 return nh && nh->id == nhid; 3096 } 3097 3098 static bool vxlan_fdb_flush_matches(const struct vxlan_fdb *f, 3099 const struct vxlan_dev *vxlan, 3100 const struct vxlan_fdb_flush_desc *desc) 3101 { 3102 if (desc->state_mask && (f->state & desc->state_mask) != desc->state) 3103 return false; 3104 3105 if (desc->flags_mask && (f->flags & desc->flags_mask) != desc->flags) 3106 return false; 3107 3108 if (desc->ignore_default_entry && vxlan_fdb_is_default_entry(f, vxlan)) 3109 return false; 3110 3111 if (desc->src_vni && f->key.vni != desc->src_vni) 3112 return false; 3113 3114 if (desc->nhid && !vxlan_fdb_nhid_matches(f, desc->nhid)) 3115 return false; 3116 3117 return true; 3118 } 3119 3120 static bool 3121 vxlan_fdb_flush_should_match_remotes(const struct vxlan_fdb_flush_desc *desc) 3122 { 3123 return desc->vni || desc->port || desc->dst_ip.sa.sa_family; 3124 } 3125 3126 static bool 3127 vxlan_fdb_flush_remote_matches(const struct vxlan_fdb_flush_desc *desc, 3128 const struct vxlan_rdst *rd) 3129 { 3130 if (desc->vni && rd->remote_vni != desc->vni) 3131 return false; 3132 3133 if (desc->port && rd->remote_port != desc->port) 3134 return false; 3135 3136 if (desc->dst_ip.sa.sa_family && 3137 !vxlan_addr_equal(&rd->remote_ip, &desc->dst_ip)) 3138 return false; 3139 3140 return true; 3141 } 3142 3143 static void 3144 vxlan_fdb_flush_match_remotes(struct vxlan_fdb *f, struct vxlan_dev *vxlan, 3145 const struct vxlan_fdb_flush_desc *desc, 3146 bool *p_destroy_fdb) 3147 { 3148 struct vxlan_rdst *rd, *tmp; 3149 3150 list_for_each_entry_safe(rd, tmp, &f->remotes, list) { 3151 if (!vxlan_fdb_flush_remote_matches(desc, rd)) 3152 continue; 3153 3154 if (list_is_singular(&f->remotes)) { 3155 *p_destroy_fdb = true; 3156 return; 3157 } 3158 3159 vxlan_fdb_dst_destroy(vxlan, f, rd, true); 3160 } 3161 } 3162 3163 /* Purge the forwarding table */ 3164 static void vxlan_flush(struct vxlan_dev *vxlan, 3165 const struct vxlan_fdb_flush_desc *desc) 3166 { 3167 bool match_remotes = vxlan_fdb_flush_should_match_remotes(desc); 3168 struct vxlan_fdb *f; 3169 3170 rcu_read_lock(); 3171 hlist_for_each_entry_rcu(f, &vxlan->fdb_list, fdb_node) { 3172 if (!vxlan_fdb_flush_matches(f, vxlan, desc)) 3173 continue; 3174 3175 spin_lock_bh(&vxlan->hash_lock); 3176 if (hlist_unhashed(&f->fdb_node)) 3177 goto unlock; 3178 3179 if (match_remotes) { 3180 bool destroy_fdb = false; 3181 3182 vxlan_fdb_flush_match_remotes(f, vxlan, desc, 3183 &destroy_fdb); 3184 3185 if (!destroy_fdb) 3186 goto unlock; 3187 } 3188 3189 vxlan_fdb_destroy(vxlan, f, true, true); 3190 unlock: 3191 spin_unlock_bh(&vxlan->hash_lock); 3192 } 3193 rcu_read_unlock(); 3194 } 3195 3196 static const struct nla_policy vxlan_del_bulk_policy[NDA_MAX + 1] = { 3197 [NDA_SRC_VNI] = { .type = NLA_U32 }, 3198 [NDA_NH_ID] = { .type = NLA_U32 }, 3199 [NDA_VNI] = { .type = NLA_U32 }, 3200 [NDA_PORT] = { .type = NLA_U16 }, 3201 [NDA_DST] = NLA_POLICY_RANGE(NLA_BINARY, sizeof(struct in_addr), 3202 sizeof(struct in6_addr)), 3203 [NDA_NDM_STATE_MASK] = { .type = NLA_U16 }, 3204 [NDA_NDM_FLAGS_MASK] = { .type = NLA_U8 }, 3205 }; 3206 3207 #define VXLAN_FDB_FLUSH_IGNORED_NDM_FLAGS (NTF_MASTER | NTF_SELF) 3208 #define VXLAN_FDB_FLUSH_ALLOWED_NDM_STATES (NUD_PERMANENT | NUD_NOARP) 3209 #define VXLAN_FDB_FLUSH_ALLOWED_NDM_FLAGS (NTF_EXT_LEARNED | NTF_OFFLOADED | \ 3210 NTF_ROUTER) 3211 3212 static int vxlan_fdb_delete_bulk(struct nlmsghdr *nlh, struct net_device *dev, 3213 struct netlink_ext_ack *extack) 3214 { 3215 struct vxlan_dev *vxlan = netdev_priv(dev); 3216 struct vxlan_fdb_flush_desc desc = {}; 3217 struct ndmsg *ndm = nlmsg_data(nlh); 3218 struct nlattr *tb[NDA_MAX + 1]; 3219 u8 ndm_flags; 3220 int err; 3221 3222 ndm_flags = ndm->ndm_flags & ~VXLAN_FDB_FLUSH_IGNORED_NDM_FLAGS; 3223 3224 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, vxlan_del_bulk_policy, 3225 extack); 3226 if (err) 3227 return err; 3228 3229 if (ndm_flags & ~VXLAN_FDB_FLUSH_ALLOWED_NDM_FLAGS) { 3230 NL_SET_ERR_MSG(extack, "Unsupported fdb flush ndm flag bits set"); 3231 return -EINVAL; 3232 } 3233 if (ndm->ndm_state & ~VXLAN_FDB_FLUSH_ALLOWED_NDM_STATES) { 3234 NL_SET_ERR_MSG(extack, "Unsupported fdb flush ndm state bits set"); 3235 return -EINVAL; 3236 } 3237 3238 desc.state = ndm->ndm_state; 3239 desc.flags = ndm_flags; 3240 3241 if (tb[NDA_NDM_STATE_MASK]) 3242 desc.state_mask = nla_get_u16(tb[NDA_NDM_STATE_MASK]); 3243 3244 if (tb[NDA_NDM_FLAGS_MASK]) 3245 desc.flags_mask = nla_get_u8(tb[NDA_NDM_FLAGS_MASK]); 3246 3247 if (tb[NDA_SRC_VNI]) 3248 desc.src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI])); 3249 3250 if (tb[NDA_NH_ID]) 3251 desc.nhid = nla_get_u32(tb[NDA_NH_ID]); 3252 3253 if (tb[NDA_VNI]) 3254 desc.vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI])); 3255 3256 if (tb[NDA_PORT]) 3257 desc.port = nla_get_be16(tb[NDA_PORT]); 3258 3259 if (tb[NDA_DST]) { 3260 union vxlan_addr ip; 3261 3262 err = vxlan_nla_get_addr(&ip, tb[NDA_DST]); 3263 if (err) { 3264 NL_SET_ERR_MSG_ATTR(extack, tb[NDA_DST], 3265 "Unsupported address family"); 3266 return err; 3267 } 3268 desc.dst_ip = ip; 3269 } 3270 3271 vxlan_flush(vxlan, &desc); 3272 3273 return 0; 3274 } 3275 3276 /* Cleanup timer and forwarding table on shutdown */ 3277 static int vxlan_stop(struct net_device *dev) 3278 { 3279 struct vxlan_dev *vxlan = netdev_priv(dev); 3280 struct vxlan_fdb_flush_desc desc = { 3281 /* Default entry is deleted at vxlan_dellink. */ 3282 .ignore_default_entry = true, 3283 .state = 0, 3284 .state_mask = NUD_PERMANENT | NUD_NOARP, 3285 }; 3286 3287 vxlan_multicast_leave(vxlan); 3288 3289 timer_delete_sync(&vxlan->age_timer); 3290 3291 vxlan_flush(vxlan, &desc); 3292 vxlan_sock_release(vxlan); 3293 3294 return 0; 3295 } 3296 3297 /* Stub, nothing needs to be done. */ 3298 static void vxlan_set_multicast_list(struct net_device *dev) 3299 { 3300 } 3301 3302 static int vxlan_change_mtu(struct net_device *dev, int new_mtu) 3303 { 3304 struct vxlan_dev *vxlan = netdev_priv(dev); 3305 const struct vxlan_config *cfg; 3306 struct net_device *lowerdev; 3307 3308 cfg = rtnl_dereference(vxlan->cfg); 3309 3310 lowerdev = __dev_get_by_index(vxlan->net, cfg->remote_ifindex); 3311 3312 /* This check is different than dev->max_mtu, because it looks at 3313 * the lowerdev->mtu, rather than the static dev->max_mtu 3314 */ 3315 if (lowerdev) { 3316 int max_mtu = lowerdev->mtu - vxlan_headroom(cfg->flags); 3317 if (new_mtu > max_mtu) 3318 return -EINVAL; 3319 } 3320 3321 WRITE_ONCE(dev->mtu, new_mtu); 3322 return 0; 3323 } 3324 3325 static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) 3326 { 3327 struct vxlan_dev *vxlan = netdev_priv(dev); 3328 struct ip_tunnel_info *info = skb_tunnel_info(skb); 3329 const struct vxlan_config *cfg; 3330 __be16 sport, dport; 3331 3332 cfg = rcu_dereference(vxlan->cfg); 3333 3334 sport = udp_flow_src_port(dev_net(dev), skb, cfg->port_min, 3335 cfg->port_max, true); 3336 dport = info->key.tp_dst ? : cfg->dst_port; 3337 3338 if (ip_tunnel_info_af(info) == AF_INET) { 3339 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock); 3340 struct rtable *rt; 3341 3342 if (!sock4) 3343 return -EIO; 3344 3345 rt = udp_tunnel_dst_lookup(skb, dev, vxlan->net, 0, 3346 &info->key.u.ipv4.src, 3347 &info->key, 3348 sport, dport, info->key.tos, 3349 &info->dst_cache); 3350 if (IS_ERR(rt)) 3351 return PTR_ERR(rt); 3352 ip_rt_put(rt); 3353 } else { 3354 #if IS_ENABLED(CONFIG_IPV6) 3355 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock); 3356 struct dst_entry *ndst; 3357 3358 if (!sock6) 3359 return -EIO; 3360 3361 ndst = udp_tunnel6_dst_lookup(skb, dev, vxlan->net, sock6->sk, 3362 0, &info->key.u.ipv6.src, 3363 &info->key, 3364 sport, dport, info->key.tos, 3365 &info->dst_cache); 3366 if (IS_ERR(ndst)) 3367 return PTR_ERR(ndst); 3368 dst_release(ndst); 3369 #else /* !CONFIG_IPV6 */ 3370 return -EPFNOSUPPORT; 3371 #endif 3372 } 3373 info->key.tp_src = sport; 3374 info->key.tp_dst = dport; 3375 return 0; 3376 } 3377 3378 static const struct net_device_ops vxlan_netdev_ether_ops = { 3379 .ndo_init = vxlan_init, 3380 .ndo_uninit = vxlan_uninit, 3381 .ndo_open = vxlan_open, 3382 .ndo_stop = vxlan_stop, 3383 .ndo_start_xmit = vxlan_xmit, 3384 .ndo_set_rx_mode = vxlan_set_multicast_list, 3385 .ndo_change_mtu = vxlan_change_mtu, 3386 .ndo_validate_addr = eth_validate_addr, 3387 .ndo_set_mac_address = eth_mac_addr, 3388 .ndo_fdb_add = vxlan_fdb_add, 3389 .ndo_fdb_del = vxlan_fdb_delete, 3390 .ndo_fdb_del_bulk = vxlan_fdb_delete_bulk, 3391 .ndo_fdb_dump = vxlan_fdb_dump, 3392 .ndo_fdb_get = vxlan_fdb_get, 3393 .ndo_mdb_add = vxlan_mdb_add, 3394 .ndo_mdb_del = vxlan_mdb_del, 3395 .ndo_mdb_del_bulk = vxlan_mdb_del_bulk, 3396 .ndo_mdb_dump = vxlan_mdb_dump, 3397 .ndo_mdb_get = vxlan_mdb_get, 3398 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst, 3399 }; 3400 3401 static const struct net_device_ops vxlan_netdev_raw_ops = { 3402 .ndo_init = vxlan_init, 3403 .ndo_uninit = vxlan_uninit, 3404 .ndo_open = vxlan_open, 3405 .ndo_stop = vxlan_stop, 3406 .ndo_start_xmit = vxlan_xmit, 3407 .ndo_change_mtu = vxlan_change_mtu, 3408 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst, 3409 }; 3410 3411 /* Info for udev, that this is a virtual tunnel endpoint */ 3412 static const struct device_type vxlan_type = { 3413 .name = "vxlan", 3414 }; 3415 3416 /* Calls the ndo_udp_tunnel_add of the caller in order to 3417 * supply the listening VXLAN udp ports. Callers are expected 3418 * to implement the ndo_udp_tunnel_add. 3419 */ 3420 static void vxlan_offload_rx_ports(struct net_device *dev, bool push) 3421 { 3422 struct vxlan_sock *vs; 3423 struct net *net = dev_net(dev); 3424 struct vxlan_net *vn = net_generic(net, vxlan_net_id); 3425 unsigned int i; 3426 3427 ASSERT_RTNL(); 3428 3429 for (i = 0; i < PORT_HASH_SIZE; ++i) { 3430 hlist_for_each_entry(vs, &vn->sock_list[i], hlist) { 3431 unsigned short type; 3432 3433 if (vs->flags & VXLAN_F_GPE) 3434 type = UDP_TUNNEL_TYPE_VXLAN_GPE; 3435 else 3436 type = UDP_TUNNEL_TYPE_VXLAN; 3437 3438 if (push) 3439 udp_tunnel_push_rx_port(dev, vs->sk, type); 3440 else 3441 udp_tunnel_drop_rx_port(dev, vs->sk, type); 3442 } 3443 } 3444 } 3445 3446 static void vxlan_free_dev(struct net_device *dev) 3447 { 3448 struct vxlan_dev *vxlan = netdev_priv(dev); 3449 struct vxlan_config *cfg = rcu_dereference_protected(vxlan->cfg, 1); 3450 3451 RCU_INIT_POINTER(vxlan->cfg, NULL); 3452 kfree(cfg); 3453 } 3454 3455 /* Initialize the device structure. */ 3456 static void vxlan_setup(struct net_device *dev) 3457 { 3458 struct vxlan_dev *vxlan = netdev_priv(dev); 3459 3460 eth_hw_addr_random(dev); 3461 ether_setup(dev); 3462 3463 dev->priv_destructor = vxlan_free_dev; 3464 3465 dev->needs_free_netdev = true; 3466 SET_NETDEV_DEVTYPE(dev, &vxlan_type); 3467 3468 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST; 3469 dev->features |= NETIF_F_RXCSUM; 3470 dev->features |= NETIF_F_GSO_SOFTWARE; 3471 3472 /* Partial features are disabled by default. */ 3473 dev->vlan_features = dev->features; 3474 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST; 3475 dev->hw_features |= NETIF_F_RXCSUM; 3476 dev->hw_features |= NETIF_F_GSO_SOFTWARE; 3477 dev->hw_features |= UDP_TUNNEL_PARTIAL_FEATURES; 3478 dev->hw_features |= NETIF_F_GSO_PARTIAL; 3479 3480 dev->hw_enc_features = dev->hw_features; 3481 dev->gso_partial_features = UDP_TUNNEL_PARTIAL_FEATURES; 3482 dev->mangleid_features = NETIF_F_GSO_PARTIAL; 3483 3484 netif_keep_dst(dev); 3485 netif_set_tso_max_size(dev, GSO_MAX_SIZE); 3486 3487 dev->priv_flags |= IFF_NO_QUEUE; 3488 dev->change_proto_down = true; 3489 dev->lltx = true; 3490 3491 /* MTU range: 68 - 65535 */ 3492 dev->min_mtu = ETH_MIN_MTU; 3493 dev->max_mtu = ETH_MAX_MTU; 3494 3495 dev->pcpu_stat_type = NETDEV_PCPU_STAT_DSTATS; 3496 INIT_LIST_HEAD(&vxlan->next); 3497 spin_lock_init(&vxlan->hash_lock); 3498 3499 timer_setup(&vxlan->age_timer, vxlan_cleanup, TIMER_DEFERRABLE); 3500 3501 vxlan->dev = dev; 3502 3503 INIT_HLIST_HEAD(&vxlan->fdb_list); 3504 } 3505 3506 static void vxlan_ether_setup(struct net_device *dev) 3507 { 3508 dev->priv_flags &= ~IFF_TX_SKB_SHARING; 3509 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 3510 dev->netdev_ops = &vxlan_netdev_ether_ops; 3511 } 3512 3513 static void vxlan_raw_setup(struct net_device *dev) 3514 { 3515 dev->header_ops = NULL; 3516 dev->type = ARPHRD_NONE; 3517 dev->hard_header_len = 0; 3518 dev->addr_len = 0; 3519 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; 3520 dev->netdev_ops = &vxlan_netdev_raw_ops; 3521 } 3522 3523 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = { 3524 [IFLA_VXLAN_UNSPEC] = { .strict_start_type = IFLA_VXLAN_LOCALBYPASS }, 3525 [IFLA_VXLAN_ID] = { .type = NLA_U32 }, 3526 [IFLA_VXLAN_GROUP] = { .len = sizeof_field(struct iphdr, daddr) }, 3527 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) }, 3528 [IFLA_VXLAN_LINK] = { .type = NLA_U32 }, 3529 [IFLA_VXLAN_LOCAL] = { .len = sizeof_field(struct iphdr, saddr) }, 3530 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) }, 3531 [IFLA_VXLAN_TOS] = { .type = NLA_U8 }, 3532 [IFLA_VXLAN_TTL] = { .type = NLA_U8 }, 3533 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 }, 3534 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 }, 3535 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 }, 3536 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 }, 3537 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) }, 3538 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 }, 3539 [IFLA_VXLAN_RSC] = { .type = NLA_U8 }, 3540 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 }, 3541 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 }, 3542 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 }, 3543 [IFLA_VXLAN_PORT] = { .type = NLA_U16 }, 3544 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 }, 3545 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 }, 3546 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 }, 3547 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 }, 3548 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 }, 3549 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, }, 3550 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, }, 3551 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG }, 3552 [IFLA_VXLAN_TTL_INHERIT] = { .type = NLA_FLAG }, 3553 [IFLA_VXLAN_DF] = { .type = NLA_U8 }, 3554 [IFLA_VXLAN_VNIFILTER] = { .type = NLA_U8 }, 3555 [IFLA_VXLAN_LOCALBYPASS] = NLA_POLICY_MAX(NLA_U8, 1), 3556 [IFLA_VXLAN_LABEL_POLICY] = NLA_POLICY_MAX(NLA_U32, VXLAN_LABEL_MAX), 3557 [IFLA_VXLAN_RESERVED_BITS] = NLA_POLICY_EXACT_LEN(sizeof(struct vxlanhdr)), 3558 [IFLA_VXLAN_MC_ROUTE] = NLA_POLICY_MAX(NLA_U8, 1), 3559 }; 3560 3561 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[], 3562 struct netlink_ext_ack *extack) 3563 { 3564 if (tb[IFLA_ADDRESS]) { 3565 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) { 3566 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS], 3567 "Provided link layer address is not Ethernet"); 3568 return -EINVAL; 3569 } 3570 3571 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) { 3572 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS], 3573 "Provided Ethernet address is not unicast"); 3574 return -EADDRNOTAVAIL; 3575 } 3576 } 3577 3578 if (tb[IFLA_MTU]) { 3579 u32 mtu = nla_get_u32(tb[IFLA_MTU]); 3580 3581 if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU) { 3582 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU], 3583 "MTU must be between 68 and 65535"); 3584 return -EINVAL; 3585 } 3586 } 3587 3588 if (!data) { 3589 NL_SET_ERR_MSG(extack, 3590 "Required attributes not provided to perform the operation"); 3591 return -EINVAL; 3592 } 3593 3594 if (data[IFLA_VXLAN_ID]) { 3595 u32 id = nla_get_u32(data[IFLA_VXLAN_ID]); 3596 3597 if (id >= VXLAN_N_VID) { 3598 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_ID], 3599 "VXLAN ID must be lower than 16777216"); 3600 return -ERANGE; 3601 } 3602 } 3603 3604 if (data[IFLA_VXLAN_PORT_RANGE]) { 3605 const struct ifla_vxlan_port_range *p 3606 = nla_data(data[IFLA_VXLAN_PORT_RANGE]); 3607 3608 if (ntohs(p->high) < ntohs(p->low)) { 3609 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_PORT_RANGE], 3610 "Invalid source port range"); 3611 return -EINVAL; 3612 } 3613 } 3614 3615 if (data[IFLA_VXLAN_DF]) { 3616 enum ifla_vxlan_df df = nla_get_u8(data[IFLA_VXLAN_DF]); 3617 3618 if (df < 0 || df > VXLAN_DF_MAX) { 3619 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_DF], 3620 "Invalid DF attribute"); 3621 return -EINVAL; 3622 } 3623 } 3624 3625 return 0; 3626 } 3627 3628 static void vxlan_get_drvinfo(struct net_device *netdev, 3629 struct ethtool_drvinfo *drvinfo) 3630 { 3631 strscpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version)); 3632 strscpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver)); 3633 } 3634 3635 static int vxlan_get_link_ksettings(struct net_device *dev, 3636 struct ethtool_link_ksettings *cmd) 3637 { 3638 struct vxlan_dev *vxlan = netdev_priv(dev); 3639 const struct vxlan_config *cfg; 3640 struct net_device *lowerdev; 3641 3642 cfg = rtnl_dereference(vxlan->cfg); 3643 lowerdev = __dev_get_by_index(vxlan->net, cfg->remote_ifindex); 3644 3645 if (!lowerdev) { 3646 cmd->base.duplex = DUPLEX_UNKNOWN; 3647 cmd->base.port = PORT_OTHER; 3648 cmd->base.speed = SPEED_UNKNOWN; 3649 3650 return 0; 3651 } 3652 3653 return __ethtool_get_link_ksettings(lowerdev, cmd); 3654 } 3655 3656 static const struct ethtool_ops vxlan_ethtool_ops = { 3657 .get_drvinfo = vxlan_get_drvinfo, 3658 .get_link = ethtool_op_get_link, 3659 .get_link_ksettings = vxlan_get_link_ksettings, 3660 }; 3661 3662 static struct sock *vxlan_create_sock(struct net *net, bool ipv6, 3663 __be16 port, u32 flags, int ifindex) 3664 { 3665 struct socket *sock; 3666 struct udp_port_cfg udp_conf; 3667 int err; 3668 3669 memset(&udp_conf, 0, sizeof(udp_conf)); 3670 3671 if (ipv6) { 3672 udp_conf.family = AF_INET6; 3673 udp_conf.use_udp6_rx_checksums = 3674 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX); 3675 udp_conf.ipv6_v6only = 1; 3676 } else { 3677 udp_conf.family = AF_INET; 3678 } 3679 3680 udp_conf.local_udp_port = port; 3681 udp_conf.bind_ifindex = ifindex; 3682 3683 /* Open UDP socket */ 3684 err = udp_sock_create(net, &udp_conf, &sock); 3685 if (err < 0) 3686 return ERR_PTR(err); 3687 3688 udp_allow_gso(sock->sk); 3689 return sock->sk; 3690 } 3691 3692 /* Create new listen socket if needed */ 3693 static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6, 3694 __be16 port, u32 flags, 3695 int ifindex) 3696 { 3697 struct udp_tunnel_sock_cfg tunnel_cfg; 3698 struct vxlan_sock *vs; 3699 struct sock *sk; 3700 unsigned int h; 3701 3702 ASSERT_RTNL(); 3703 3704 vs = kzalloc_obj(*vs); 3705 if (!vs) 3706 return ERR_PTR(-ENOMEM); 3707 3708 for (h = 0; h < VNI_HASH_SIZE; ++h) 3709 INIT_HLIST_HEAD(&vs->vni_list[h]); 3710 3711 sk = vxlan_create_sock(net, ipv6, port, flags, ifindex); 3712 if (IS_ERR(sk)) { 3713 kfree(vs); 3714 return ERR_CAST(sk); 3715 } 3716 3717 vs->sk = sk; 3718 refcount_set(&vs->refcnt, 1); 3719 vs->flags = (flags & VXLAN_F_RCV_FLAGS); 3720 3721 hlist_add_head_rcu(&vs->hlist, vs_head(net, port)); 3722 udp_tunnel_notify_add_rx_port(sk, 3723 (vs->flags & VXLAN_F_GPE) ? 3724 UDP_TUNNEL_TYPE_VXLAN_GPE : 3725 UDP_TUNNEL_TYPE_VXLAN); 3726 3727 /* Mark socket as an encapsulation socket. */ 3728 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg)); 3729 tunnel_cfg.sk_user_data = vs; 3730 tunnel_cfg.encap_type = 1; 3731 tunnel_cfg.encap_rcv = vxlan_rcv; 3732 tunnel_cfg.encap_err_lookup = vxlan_err_lookup; 3733 tunnel_cfg.encap_destroy = NULL; 3734 if (vs->flags & VXLAN_F_GPE) { 3735 tunnel_cfg.gro_receive = vxlan_gpe_gro_receive; 3736 tunnel_cfg.gro_complete = vxlan_gpe_gro_complete; 3737 } else { 3738 tunnel_cfg.gro_receive = vxlan_gro_receive; 3739 tunnel_cfg.gro_complete = vxlan_gro_complete; 3740 } 3741 3742 setup_udp_tunnel_sock(net, sk, &tunnel_cfg); 3743 3744 return vs; 3745 } 3746 3747 static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6) 3748 { 3749 const struct vxlan_config *cfg = rtnl_dereference(vxlan->cfg); 3750 bool metadata = cfg->flags & VXLAN_F_COLLECT_METADATA; 3751 struct vxlan_sock *vs = NULL; 3752 struct vxlan_dev_node *node; 3753 int l3mdev_index = 0; 3754 3755 ASSERT_RTNL(); 3756 3757 if (cfg->remote_ifindex) 3758 l3mdev_index = l3mdev_master_upper_ifindex_by_index( 3759 vxlan->net, cfg->remote_ifindex); 3760 3761 if (!cfg->no_share) { 3762 rcu_read_lock(); 3763 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET, 3764 cfg->dst_port, cfg->flags, 3765 l3mdev_index); 3766 if (vs && !refcount_inc_not_zero(&vs->refcnt)) { 3767 rcu_read_unlock(); 3768 return -EBUSY; 3769 } 3770 rcu_read_unlock(); 3771 } 3772 if (!vs) 3773 vs = vxlan_socket_create(vxlan->net, ipv6, 3774 cfg->dst_port, cfg->flags, 3775 l3mdev_index); 3776 if (IS_ERR(vs)) 3777 return PTR_ERR(vs); 3778 #if IS_ENABLED(CONFIG_IPV6) 3779 if (ipv6) { 3780 rcu_assign_pointer(vxlan->vn6_sock, vs); 3781 node = &vxlan->hlist6; 3782 } else 3783 #endif 3784 { 3785 rcu_assign_pointer(vxlan->vn4_sock, vs); 3786 node = &vxlan->hlist4; 3787 } 3788 3789 if (metadata && (cfg->flags & VXLAN_F_VNIFILTER)) 3790 vxlan_vs_add_vnigrp(vxlan, vs, ipv6); 3791 else 3792 vxlan_vs_add_dev(vs, vxlan, node); 3793 3794 return 0; 3795 } 3796 3797 static int vxlan_sock_add(struct vxlan_dev *vxlan) 3798 { 3799 const struct vxlan_config *cfg = rtnl_dereference(vxlan->cfg); 3800 bool metadata, ipv6, ipv4; 3801 int ret = 0; 3802 3803 metadata = cfg->flags & VXLAN_F_COLLECT_METADATA; 3804 ipv6 = (cfg->flags & VXLAN_F_IPV6) || metadata; 3805 ipv4 = !ipv6 || metadata; 3806 3807 RCU_INIT_POINTER(vxlan->vn4_sock, NULL); 3808 #if IS_ENABLED(CONFIG_IPV6) 3809 RCU_INIT_POINTER(vxlan->vn6_sock, NULL); 3810 if (ipv6) { 3811 ret = __vxlan_sock_add(vxlan, true); 3812 if (ret < 0 && ret != -EAFNOSUPPORT) 3813 ipv4 = false; 3814 } 3815 #endif 3816 if (ipv4) 3817 ret = __vxlan_sock_add(vxlan, false); 3818 if (ret < 0) 3819 vxlan_sock_release(vxlan); 3820 return ret; 3821 } 3822 3823 int vxlan_vni_in_use(struct net *src_net, struct vxlan_dev *vxlan, 3824 const struct vxlan_config *conf, __be32 vni) 3825 { 3826 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id); 3827 struct vxlan_dev *tmp; 3828 3829 list_for_each_entry(tmp, &vn->vxlan_list, next) { 3830 const struct vxlan_config *tmp_cfg; 3831 3832 if (tmp == vxlan) 3833 continue; 3834 3835 tmp_cfg = rtnl_dereference(tmp->cfg); 3836 3837 if (tmp_cfg->flags & VXLAN_F_VNIFILTER) { 3838 if (!vxlan_vnifilter_lookup(tmp, vni)) 3839 continue; 3840 } else if (tmp_cfg->vni != vni) { 3841 continue; 3842 } 3843 if (tmp_cfg->dst_port != conf->dst_port) 3844 continue; 3845 if ((tmp_cfg->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) != 3846 (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6))) 3847 continue; 3848 3849 if ((conf->flags & VXLAN_F_IPV6_LINKLOCAL) && 3850 tmp_cfg->remote_ifindex != conf->remote_ifindex) 3851 continue; 3852 3853 return -EEXIST; 3854 } 3855 3856 return 0; 3857 } 3858 3859 static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf, 3860 struct net_device **lower, 3861 struct vxlan_dev *old, 3862 struct netlink_ext_ack *extack) 3863 { 3864 bool use_ipv6 = false; 3865 3866 if (conf->flags & VXLAN_F_GPE) { 3867 /* For now, allow GPE only together with 3868 * COLLECT_METADATA. This can be relaxed later; in such 3869 * case, the other side of the PtP link will have to be 3870 * provided. 3871 */ 3872 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) || 3873 !(conf->flags & VXLAN_F_COLLECT_METADATA)) { 3874 NL_SET_ERR_MSG(extack, 3875 "VXLAN GPE does not support this combination of attributes"); 3876 return -EINVAL; 3877 } 3878 } 3879 3880 if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) { 3881 /* Unless IPv6 is explicitly requested, assume IPv4 */ 3882 conf->remote_ip.sa.sa_family = AF_INET; 3883 conf->saddr.sa.sa_family = AF_INET; 3884 } else if (!conf->remote_ip.sa.sa_family) { 3885 conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family; 3886 } else if (!conf->saddr.sa.sa_family) { 3887 conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family; 3888 } 3889 3890 if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family) { 3891 NL_SET_ERR_MSG(extack, 3892 "Local and remote address must be from the same family"); 3893 return -EINVAL; 3894 } 3895 3896 if (vxlan_addr_multicast(&conf->saddr)) { 3897 NL_SET_ERR_MSG(extack, "Local address cannot be multicast"); 3898 return -EINVAL; 3899 } 3900 3901 if (conf->saddr.sa.sa_family == AF_INET6) { 3902 if (!IS_ENABLED(CONFIG_IPV6)) { 3903 NL_SET_ERR_MSG(extack, 3904 "IPv6 support not enabled in the kernel"); 3905 return -EPFNOSUPPORT; 3906 } 3907 use_ipv6 = true; 3908 conf->flags |= VXLAN_F_IPV6; 3909 3910 if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) { 3911 int local_type = 3912 ipv6_addr_type(&conf->saddr.sin6.sin6_addr); 3913 int remote_type = 3914 ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr); 3915 3916 if (local_type & IPV6_ADDR_LINKLOCAL) { 3917 if (!(remote_type & IPV6_ADDR_LINKLOCAL) && 3918 (remote_type != IPV6_ADDR_ANY)) { 3919 NL_SET_ERR_MSG(extack, 3920 "Invalid combination of local and remote address scopes"); 3921 return -EINVAL; 3922 } 3923 3924 conf->flags |= VXLAN_F_IPV6_LINKLOCAL; 3925 } else { 3926 if (remote_type == 3927 (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL)) { 3928 NL_SET_ERR_MSG(extack, 3929 "Invalid combination of local and remote address scopes"); 3930 return -EINVAL; 3931 } 3932 3933 conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL; 3934 } 3935 } 3936 } 3937 3938 if (conf->label && !use_ipv6) { 3939 NL_SET_ERR_MSG(extack, 3940 "Label attribute only applies to IPv6 VXLAN devices"); 3941 return -EINVAL; 3942 } 3943 3944 if (conf->label_policy && !use_ipv6) { 3945 NL_SET_ERR_MSG(extack, 3946 "Label policy only applies to IPv6 VXLAN devices"); 3947 return -EINVAL; 3948 } 3949 3950 if (conf->remote_ifindex) { 3951 struct net_device *lowerdev; 3952 3953 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex); 3954 if (!lowerdev) { 3955 NL_SET_ERR_MSG(extack, 3956 "Invalid local interface, device not found"); 3957 return -ENODEV; 3958 } 3959 3960 #if IS_ENABLED(CONFIG_IPV6) 3961 if (use_ipv6) { 3962 struct inet6_dev *idev = __in6_dev_get(lowerdev); 3963 3964 if (idev && idev->cnf.disable_ipv6) { 3965 NL_SET_ERR_MSG(extack, 3966 "IPv6 support disabled by administrator"); 3967 return -EPERM; 3968 } 3969 } 3970 #endif 3971 3972 *lower = lowerdev; 3973 } else { 3974 if (vxlan_addr_multicast(&conf->remote_ip)) { 3975 NL_SET_ERR_MSG(extack, 3976 "Local interface required for multicast remote destination"); 3977 3978 return -EINVAL; 3979 } 3980 3981 if ((conf->flags & VXLAN_F_VNIFILTER) && old && 3982 vxlan_vnifilter_has_multicast(old)) { 3983 NL_SET_ERR_MSG(extack, 3984 "Local interface required for multicast remote group"); 3985 return -EINVAL; 3986 } 3987 3988 #if IS_ENABLED(CONFIG_IPV6) 3989 if (conf->flags & VXLAN_F_IPV6_LINKLOCAL) { 3990 NL_SET_ERR_MSG(extack, 3991 "Local interface required for link-local local/remote addresses"); 3992 return -EINVAL; 3993 } 3994 #endif 3995 3996 *lower = NULL; 3997 } 3998 3999 if (!conf->dst_port) { 4000 if (conf->flags & VXLAN_F_GPE) 4001 conf->dst_port = htons(IANA_VXLAN_GPE_UDP_PORT); 4002 else 4003 conf->dst_port = htons(vxlan_port); 4004 } 4005 4006 if (!conf->age_interval) 4007 conf->age_interval = FDB_AGE_DEFAULT; 4008 4009 if (vxlan_vni_in_use(src_net, old, conf, conf->vni)) { 4010 NL_SET_ERR_MSG(extack, 4011 "A VXLAN device with the specified VNI already exists"); 4012 return -EEXIST; 4013 } 4014 4015 return 0; 4016 } 4017 4018 static void vxlan_config_apply(struct net_device *dev, 4019 struct vxlan_config *new_cfg, 4020 struct net_device *lowerdev, 4021 struct net *src_net, 4022 bool changelink, bool lowerdev_changed) 4023 { 4024 struct vxlan_dev *vxlan = netdev_priv(dev); 4025 unsigned short needed_headroom = ETH_HLEN; 4026 struct vxlan_config *old_cfg; 4027 int max_mtu = ETH_MAX_MTU; 4028 u32 flags = new_cfg->flags; 4029 4030 if (!changelink) { 4031 if (flags & VXLAN_F_GPE) 4032 vxlan_raw_setup(dev); 4033 else 4034 vxlan_ether_setup(dev); 4035 4036 if (new_cfg->mtu) 4037 WRITE_ONCE(dev->mtu, new_cfg->mtu); 4038 4039 vxlan->net = src_net; 4040 } 4041 4042 dev->needed_tailroom = 0; 4043 if (lowerdev) { 4044 netif_inherit_tso_max(dev, lowerdev); 4045 4046 needed_headroom = lowerdev->hard_header_len; 4047 needed_headroom += lowerdev->needed_headroom; 4048 4049 dev->needed_tailroom = lowerdev->needed_tailroom; 4050 4051 max_mtu = READ_ONCE(lowerdev->mtu) - vxlan_headroom(flags); 4052 if (max_mtu < ETH_MIN_MTU) 4053 max_mtu = ETH_MIN_MTU; 4054 4055 if (!changelink && !new_cfg->mtu) 4056 WRITE_ONCE(dev->mtu, max_mtu); 4057 } 4058 4059 /* A changelink leaving the lower device alone must not shrink the mtu */ 4060 if (lowerdev_changed && READ_ONCE(dev->mtu) > max_mtu) 4061 WRITE_ONCE(dev->mtu, max_mtu); 4062 4063 if (flags & VXLAN_F_COLLECT_METADATA) 4064 flags |= VXLAN_F_IPV6; 4065 needed_headroom += vxlan_headroom(flags); 4066 dev->needed_headroom = needed_headroom; 4067 4068 old_cfg = rtnl_dereference(vxlan->cfg); 4069 rcu_assign_pointer(vxlan->cfg, new_cfg); 4070 if (old_cfg) 4071 kfree_rcu(old_cfg, rcu); 4072 } 4073 4074 static int vxlan_dev_configure(struct net *src_net, struct net_device *dev, 4075 struct vxlan_config *conf, 4076 struct netlink_ext_ack *extack) 4077 { 4078 struct vxlan_dev *vxlan = netdev_priv(dev); 4079 struct net_device *lowerdev; 4080 struct vxlan_config *new_cfg; 4081 int ret; 4082 4083 ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan, extack); 4084 if (ret) 4085 return ret; 4086 4087 new_cfg = kmemdup(conf, sizeof(*conf), GFP_KERNEL); 4088 if (!new_cfg) 4089 return -ENOMEM; 4090 4091 vxlan_config_apply(dev, new_cfg, lowerdev, src_net, false, true); 4092 4093 return 0; 4094 } 4095 4096 static int vxlan_dev_create(struct net *net, struct net_device *dev, 4097 struct vxlan_config *conf, 4098 struct netlink_ext_ack *extack) 4099 { 4100 struct vxlan_net *vn = net_generic(net, vxlan_net_id); 4101 struct vxlan_dev *vxlan = netdev_priv(dev); 4102 struct net_device *remote_dev = NULL; 4103 const struct vxlan_config *cfg; 4104 int err; 4105 4106 err = vxlan_dev_configure(net, dev, conf, extack); 4107 if (err) 4108 return err; 4109 4110 cfg = rtnl_dereference(vxlan->cfg); 4111 4112 dev->ethtool_ops = &vxlan_ethtool_ops; 4113 4114 err = register_netdevice(dev); 4115 if (err) { 4116 vxlan_free_dev(dev); 4117 return err; 4118 } 4119 4120 if (cfg->remote_ifindex) { 4121 remote_dev = __dev_get_by_index(net, cfg->remote_ifindex); 4122 if (!remote_dev) { 4123 err = -ENODEV; 4124 goto unregister; 4125 } 4126 4127 err = netdev_upper_dev_link(remote_dev, dev, extack); 4128 if (err) 4129 goto unregister; 4130 4131 vxlan->lowerdev = remote_dev; 4132 } 4133 4134 err = rtnl_configure_link(dev, NULL, 0, NULL); 4135 if (err < 0) 4136 goto unlink; 4137 4138 /* create an fdb entry for a valid default destination */ 4139 if (!vxlan_addr_any(&cfg->remote_ip)) { 4140 union vxlan_addr rip = cfg->remote_ip; 4141 4142 spin_lock_bh(&vxlan->hash_lock); 4143 err = vxlan_fdb_update(vxlan, all_zeros_mac, 4144 &rip, 4145 NUD_REACHABLE | NUD_PERMANENT, 4146 NLM_F_EXCL | NLM_F_CREATE, 4147 cfg->dst_port, 4148 cfg->vni, 4149 cfg->vni, 4150 cfg->remote_ifindex, 4151 NTF_SELF, 0, true, extack); 4152 spin_unlock_bh(&vxlan->hash_lock); 4153 if (err) 4154 goto unlink; 4155 } 4156 4157 list_add(&vxlan->next, &vn->vxlan_list); 4158 4159 return 0; 4160 4161 unlink: 4162 if (remote_dev) 4163 netdev_upper_dev_unlink(remote_dev, dev); 4164 unregister: 4165 unregister_netdevice(dev); 4166 return err; 4167 } 4168 4169 /* Set/clear flags based on attribute */ 4170 static int vxlan_nl2flag(struct vxlan_config *conf, struct nlattr *tb[], 4171 int attrtype, unsigned long mask, bool changelink, 4172 bool changelink_supported, 4173 struct netlink_ext_ack *extack) 4174 { 4175 unsigned long flags; 4176 4177 if (!tb[attrtype]) 4178 return 0; 4179 4180 if (changelink && !changelink_supported) { 4181 vxlan_flag_attr_error(attrtype, extack); 4182 return -EOPNOTSUPP; 4183 } 4184 4185 if (vxlan_policy[attrtype].type == NLA_FLAG) 4186 flags = conf->flags | mask; 4187 else if (nla_get_u8(tb[attrtype])) 4188 flags = conf->flags | mask; 4189 else 4190 flags = conf->flags & ~mask; 4191 4192 conf->flags = flags; 4193 4194 return 0; 4195 } 4196 4197 static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[], 4198 struct net_device *dev, struct vxlan_config *conf, 4199 bool changelink, struct netlink_ext_ack *extack) 4200 { 4201 struct vxlanhdr used_bits = { 4202 .vx_flags = VXLAN_HF_VNI, 4203 .vx_vni = VXLAN_VNI_MASK, 4204 }; 4205 struct vxlan_dev *vxlan = netdev_priv(dev); 4206 int err = 0; 4207 4208 memset(conf, 0, sizeof(*conf)); 4209 4210 /* if changelink operation, start with old existing cfg */ 4211 if (changelink) { 4212 const struct vxlan_config *cfg = rtnl_dereference(vxlan->cfg); 4213 4214 if (cfg) 4215 memcpy(conf, cfg, sizeof(*conf)); 4216 } 4217 4218 if (data[IFLA_VXLAN_ID]) { 4219 __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID])); 4220 4221 if (changelink && (vni != conf->vni)) { 4222 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_ID], "Cannot change VNI"); 4223 return -EOPNOTSUPP; 4224 } 4225 conf->vni = vni; 4226 } 4227 4228 if (data[IFLA_VXLAN_GROUP]) { 4229 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET)) { 4230 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP], "New group address family does not match old group"); 4231 return -EOPNOTSUPP; 4232 } 4233 4234 conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]); 4235 conf->remote_ip.sa.sa_family = AF_INET; 4236 } else if (data[IFLA_VXLAN_GROUP6]) { 4237 if (!IS_ENABLED(CONFIG_IPV6)) { 4238 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "IPv6 support not enabled in the kernel"); 4239 return -EPFNOSUPPORT; 4240 } 4241 4242 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6)) { 4243 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "New group address family does not match old group"); 4244 return -EOPNOTSUPP; 4245 } 4246 4247 conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]); 4248 conf->remote_ip.sa.sa_family = AF_INET6; 4249 } 4250 4251 if (data[IFLA_VXLAN_LOCAL]) { 4252 if (changelink && (conf->saddr.sa.sa_family != AF_INET)) { 4253 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL], "New local address family does not match old"); 4254 return -EOPNOTSUPP; 4255 } 4256 4257 conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]); 4258 conf->saddr.sa.sa_family = AF_INET; 4259 } else if (data[IFLA_VXLAN_LOCAL6]) { 4260 if (!IS_ENABLED(CONFIG_IPV6)) { 4261 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "IPv6 support not enabled in the kernel"); 4262 return -EPFNOSUPPORT; 4263 } 4264 4265 if (changelink && (conf->saddr.sa.sa_family != AF_INET6)) { 4266 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "New local address family does not match old"); 4267 return -EOPNOTSUPP; 4268 } 4269 4270 /* TODO: respect scope id */ 4271 conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]); 4272 conf->saddr.sa.sa_family = AF_INET6; 4273 } 4274 4275 if (data[IFLA_VXLAN_LINK]) 4276 conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]); 4277 4278 if (data[IFLA_VXLAN_TOS]) 4279 conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]); 4280 4281 if (data[IFLA_VXLAN_TTL]) 4282 conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]); 4283 4284 if (data[IFLA_VXLAN_TTL_INHERIT]) { 4285 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_TTL_INHERIT, 4286 VXLAN_F_TTL_INHERIT, changelink, false, 4287 extack); 4288 if (err) 4289 return err; 4290 4291 } 4292 4293 if (data[IFLA_VXLAN_LABEL]) 4294 conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) & 4295 IPV6_FLOWLABEL_MASK; 4296 if (data[IFLA_VXLAN_LABEL_POLICY]) 4297 conf->label_policy = nla_get_u32(data[IFLA_VXLAN_LABEL_POLICY]); 4298 4299 if (data[IFLA_VXLAN_LEARNING]) { 4300 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_LEARNING, 4301 VXLAN_F_LEARN, changelink, true, 4302 extack); 4303 if (err) 4304 return err; 4305 } else if (!changelink) { 4306 /* default to learn on a new device */ 4307 conf->flags |= VXLAN_F_LEARN; 4308 } 4309 4310 if (data[IFLA_VXLAN_AGEING]) 4311 conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]); 4312 4313 if (data[IFLA_VXLAN_PROXY]) { 4314 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_PROXY, 4315 VXLAN_F_PROXY, changelink, false, 4316 extack); 4317 if (err) 4318 return err; 4319 } 4320 4321 if (data[IFLA_VXLAN_RSC]) { 4322 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_RSC, 4323 VXLAN_F_RSC, changelink, false, 4324 extack); 4325 if (err) 4326 return err; 4327 } 4328 4329 if (data[IFLA_VXLAN_L2MISS]) { 4330 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L2MISS, 4331 VXLAN_F_L2MISS, changelink, false, 4332 extack); 4333 if (err) 4334 return err; 4335 } 4336 4337 if (data[IFLA_VXLAN_L3MISS]) { 4338 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L3MISS, 4339 VXLAN_F_L3MISS, changelink, false, 4340 extack); 4341 if (err) 4342 return err; 4343 } 4344 4345 if (data[IFLA_VXLAN_LIMIT]) { 4346 if (changelink) { 4347 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LIMIT], 4348 "Cannot change limit"); 4349 return -EOPNOTSUPP; 4350 } 4351 conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]); 4352 } 4353 4354 if (data[IFLA_VXLAN_COLLECT_METADATA]) { 4355 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_COLLECT_METADATA, 4356 VXLAN_F_COLLECT_METADATA, changelink, false, 4357 extack); 4358 if (err) 4359 return err; 4360 } 4361 4362 if (data[IFLA_VXLAN_PORT_RANGE]) { 4363 if (!changelink) { 4364 const struct ifla_vxlan_port_range *p 4365 = nla_data(data[IFLA_VXLAN_PORT_RANGE]); 4366 conf->port_min = ntohs(p->low); 4367 conf->port_max = ntohs(p->high); 4368 } else { 4369 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT_RANGE], 4370 "Cannot change port range"); 4371 return -EOPNOTSUPP; 4372 } 4373 } 4374 4375 if (data[IFLA_VXLAN_PORT]) { 4376 if (changelink) { 4377 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT], 4378 "Cannot change port"); 4379 return -EOPNOTSUPP; 4380 } 4381 conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]); 4382 } 4383 4384 if (data[IFLA_VXLAN_UDP_CSUM]) { 4385 if (changelink) { 4386 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_UDP_CSUM], 4387 "Cannot change UDP_CSUM flag"); 4388 return -EOPNOTSUPP; 4389 } 4390 if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM])) 4391 conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX; 4392 } 4393 4394 if (data[IFLA_VXLAN_LOCALBYPASS]) { 4395 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_LOCALBYPASS, 4396 VXLAN_F_LOCALBYPASS, changelink, 4397 true, extack); 4398 if (err) 4399 return err; 4400 } else if (!changelink) { 4401 /* default to local bypass on a new device */ 4402 conf->flags |= VXLAN_F_LOCALBYPASS; 4403 } 4404 4405 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) { 4406 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_TX, 4407 VXLAN_F_UDP_ZERO_CSUM6_TX, changelink, 4408 false, extack); 4409 if (err) 4410 return err; 4411 } 4412 4413 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) { 4414 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, 4415 VXLAN_F_UDP_ZERO_CSUM6_RX, changelink, 4416 false, extack); 4417 if (err) 4418 return err; 4419 } 4420 4421 if (data[IFLA_VXLAN_REMCSUM_TX]) { 4422 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_TX, 4423 VXLAN_F_REMCSUM_TX, changelink, false, 4424 extack); 4425 if (err) 4426 return err; 4427 } 4428 4429 if (data[IFLA_VXLAN_REMCSUM_RX]) { 4430 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_RX, 4431 VXLAN_F_REMCSUM_RX, changelink, false, 4432 extack); 4433 if (err) 4434 return err; 4435 used_bits.vx_flags |= VXLAN_HF_RCO; 4436 used_bits.vx_vni |= ~VXLAN_VNI_MASK; 4437 } 4438 4439 if (data[IFLA_VXLAN_GBP]) { 4440 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GBP, 4441 VXLAN_F_GBP, changelink, false, extack); 4442 if (err) 4443 return err; 4444 used_bits.vx_flags |= VXLAN_GBP_USED_BITS; 4445 } 4446 4447 if (data[IFLA_VXLAN_GPE]) { 4448 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GPE, 4449 VXLAN_F_GPE, changelink, false, 4450 extack); 4451 if (err) 4452 return err; 4453 4454 used_bits.vx_flags |= VXLAN_GPE_USED_BITS; 4455 } 4456 4457 if (data[IFLA_VXLAN_RESERVED_BITS]) { 4458 struct vxlanhdr reserved_bits; 4459 4460 if (changelink) { 4461 NL_SET_ERR_MSG_ATTR(extack, 4462 data[IFLA_VXLAN_RESERVED_BITS], 4463 "Cannot change reserved_bits"); 4464 return -EOPNOTSUPP; 4465 } 4466 4467 nla_memcpy(&reserved_bits, data[IFLA_VXLAN_RESERVED_BITS], 4468 sizeof(reserved_bits)); 4469 if (used_bits.vx_flags & reserved_bits.vx_flags || 4470 used_bits.vx_vni & reserved_bits.vx_vni) { 4471 __be64 ub_be64, rb_be64; 4472 4473 memcpy(&ub_be64, &used_bits, sizeof(ub_be64)); 4474 memcpy(&rb_be64, &reserved_bits, sizeof(rb_be64)); 4475 4476 NL_SET_ERR_MSG_ATTR_FMT(extack, 4477 data[IFLA_VXLAN_RESERVED_BITS], 4478 "Used bits %#018llx cannot overlap reserved bits %#018llx", 4479 be64_to_cpu(ub_be64), 4480 be64_to_cpu(rb_be64)); 4481 return -EINVAL; 4482 } 4483 4484 conf->reserved_bits = reserved_bits; 4485 } else { 4486 /* For backwards compatibility, only allow reserved fields to be 4487 * used by VXLAN extensions if explicitly requested. 4488 */ 4489 conf->reserved_bits = (struct vxlanhdr) { 4490 .vx_flags = ~used_bits.vx_flags, 4491 .vx_vni = ~used_bits.vx_vni, 4492 }; 4493 } 4494 4495 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) { 4496 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_NOPARTIAL, 4497 VXLAN_F_REMCSUM_NOPARTIAL, changelink, 4498 false, extack); 4499 if (err) 4500 return err; 4501 } 4502 4503 if (data[IFLA_VXLAN_MC_ROUTE]) { 4504 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_MC_ROUTE, 4505 VXLAN_F_MC_ROUTE, changelink, 4506 true, extack); 4507 if (err) 4508 return err; 4509 } 4510 4511 if (tb[IFLA_MTU]) { 4512 if (changelink) { 4513 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU], 4514 "Cannot change mtu"); 4515 return -EOPNOTSUPP; 4516 } 4517 conf->mtu = nla_get_u32(tb[IFLA_MTU]); 4518 } 4519 4520 if (data[IFLA_VXLAN_DF]) 4521 conf->df = nla_get_u8(data[IFLA_VXLAN_DF]); 4522 4523 if (data[IFLA_VXLAN_VNIFILTER]) { 4524 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_VNIFILTER, 4525 VXLAN_F_VNIFILTER, changelink, false, 4526 extack); 4527 if (err) 4528 return err; 4529 4530 if ((conf->flags & VXLAN_F_VNIFILTER) && 4531 !(conf->flags & VXLAN_F_COLLECT_METADATA)) { 4532 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_VNIFILTER], 4533 "vxlan vnifilter only valid in collect metadata mode"); 4534 return -EINVAL; 4535 } 4536 } 4537 4538 return 0; 4539 } 4540 4541 static int vxlan_newlink(struct net_device *dev, 4542 struct rtnl_newlink_params *params, 4543 struct netlink_ext_ack *extack) 4544 { 4545 struct net *link_net = rtnl_newlink_link_net(params); 4546 struct nlattr **data = params->data; 4547 struct nlattr **tb = params->tb; 4548 struct vxlan_config conf; 4549 int err; 4550 4551 err = vxlan_nl2conf(tb, data, dev, &conf, false, extack); 4552 if (err) 4553 return err; 4554 4555 return vxlan_dev_create(link_net, dev, &conf, extack); 4556 } 4557 4558 static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[], 4559 struct nlattr *data[], 4560 struct netlink_ext_ack *extack) 4561 { 4562 bool lowerdev_changed, rem_ip_changed, change_igmp; 4563 struct vxlan_dev *vxlan = netdev_priv(dev); 4564 const struct vxlan_config *cfg; 4565 struct vxlan_config *new_cfg; 4566 struct net_device *lowerdev; 4567 struct vxlan_config conf; 4568 int err; 4569 4570 cfg = rtnl_dereference(vxlan->cfg); 4571 4572 if (!rtnl_dev_link_net_capable(dev, vxlan->net)) 4573 return -EPERM; 4574 4575 err = vxlan_nl2conf(tb, data, dev, &conf, true, extack); 4576 if (err) 4577 return err; 4578 4579 err = vxlan_config_validate(vxlan->net, &conf, &lowerdev, 4580 vxlan, extack); 4581 if (err) 4582 return err; 4583 4584 new_cfg = kmemdup(&conf, sizeof(conf), GFP_KERNEL); 4585 if (!new_cfg) 4586 return -ENOMEM; 4587 4588 lowerdev_changed = vxlan->lowerdev != lowerdev; 4589 if (lowerdev_changed) { 4590 err = netdev_adjacent_change_prepare(vxlan->lowerdev, lowerdev, 4591 dev, extack); 4592 if (err) { 4593 kfree(new_cfg); 4594 return err; 4595 } 4596 } 4597 4598 rem_ip_changed = !vxlan_addr_equal(&conf.remote_ip, &cfg->remote_ip); 4599 change_igmp = vxlan->dev->flags & IFF_UP && 4600 (rem_ip_changed || 4601 cfg->remote_ifindex != conf.remote_ifindex); 4602 4603 /* handle default dst entry */ 4604 if (rem_ip_changed || cfg->remote_ifindex != conf.remote_ifindex) { 4605 spin_lock_bh(&vxlan->hash_lock); 4606 if (!vxlan_addr_any(&conf.remote_ip)) { 4607 err = vxlan_fdb_update(vxlan, all_zeros_mac, 4608 &conf.remote_ip, 4609 NUD_REACHABLE | NUD_PERMANENT, 4610 NLM_F_APPEND | NLM_F_CREATE, 4611 cfg->dst_port, 4612 conf.vni, conf.vni, 4613 conf.remote_ifindex, 4614 NTF_SELF, 0, true, extack); 4615 if (err) { 4616 spin_unlock_bh(&vxlan->hash_lock); 4617 if (lowerdev_changed) 4618 netdev_adjacent_change_abort(vxlan->lowerdev, 4619 lowerdev, dev); 4620 kfree(new_cfg); 4621 return err; 4622 } 4623 } 4624 if (!vxlan_addr_any(&cfg->remote_ip)) 4625 __vxlan_fdb_delete(vxlan, all_zeros_mac, 4626 cfg->remote_ip, 4627 cfg->dst_port, 4628 cfg->vni, 4629 cfg->vni, 4630 cfg->remote_ifindex, 4631 true); 4632 spin_unlock_bh(&vxlan->hash_lock); 4633 4634 /* If vni filtering device, also update default fdb entries of 4635 * all vnis 4636 */ 4637 if (cfg->flags & VXLAN_F_VNIFILTER) { 4638 err = vxlan_vnilist_update_group(vxlan, &cfg->remote_ip, 4639 &conf.remote_ip, 4640 cfg->remote_ifindex, 4641 conf.remote_ifindex, 4642 extack); 4643 if (err) { 4644 vxlan_update_default_fdb_entry(vxlan, conf.vni, 4645 &conf.remote_ip, 4646 &cfg->remote_ip, 4647 conf.remote_ifindex, 4648 cfg->remote_ifindex, 4649 NULL); 4650 if (lowerdev_changed) 4651 netdev_adjacent_change_abort(vxlan->lowerdev, 4652 lowerdev, dev); 4653 kfree(new_cfg); 4654 return err; 4655 } 4656 } 4657 } 4658 4659 if (change_igmp && 4660 (vxlan_addr_multicast(&cfg->remote_ip) || 4661 (cfg->flags & VXLAN_F_VNIFILTER))) 4662 err = vxlan_multicast_leave(vxlan); 4663 4664 if (netif_running(dev) && conf.age_interval != cfg->age_interval) 4665 mod_timer(&vxlan->age_timer, jiffies); 4666 4667 if (lowerdev_changed) { 4668 if (lowerdev) 4669 netdev_adjacent_change_commit(vxlan->lowerdev, lowerdev, 4670 dev); 4671 else 4672 netdev_upper_dev_unlink(vxlan->lowerdev, dev); 4673 vxlan->lowerdev = lowerdev; 4674 } 4675 vxlan_config_apply(dev, new_cfg, lowerdev, vxlan->net, true, 4676 lowerdev_changed); 4677 4678 if (change_igmp && 4679 (vxlan_addr_multicast(&new_cfg->remote_ip) || 4680 (new_cfg->flags & VXLAN_F_VNIFILTER))) { 4681 int join_err = vxlan_multicast_join(vxlan); 4682 4683 if (join_err) 4684 err = join_err; 4685 } 4686 4687 return err; 4688 } 4689 4690 static void vxlan_dellink(struct net_device *dev, struct list_head *head) 4691 { 4692 struct vxlan_dev *vxlan = netdev_priv(dev); 4693 struct vxlan_fdb_flush_desc desc = {}; 4694 4695 vxlan_flush(vxlan, &desc); 4696 4697 list_del(&vxlan->next); 4698 unregister_netdevice_queue(dev, head); 4699 if (vxlan->lowerdev) 4700 netdev_upper_dev_unlink(vxlan->lowerdev, dev); 4701 } 4702 4703 static size_t vxlan_get_size(const struct net_device *dev) 4704 { 4705 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */ 4706 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */ 4707 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */ 4708 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */ 4709 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */ 4710 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL_INHERIT */ 4711 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */ 4712 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_DF */ 4713 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */ 4714 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LABEL_POLICY */ 4715 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */ 4716 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */ 4717 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */ 4718 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */ 4719 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */ 4720 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */ 4721 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */ 4722 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */ 4723 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */ 4724 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */ 4725 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */ 4726 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */ 4727 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */ 4728 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */ 4729 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LOCALBYPASS */ 4730 /* IFLA_VXLAN_PORT_RANGE */ 4731 nla_total_size(sizeof(struct ifla_vxlan_port_range)) + 4732 nla_total_size(0) + /* IFLA_VXLAN_GBP */ 4733 nla_total_size(0) + /* IFLA_VXLAN_GPE */ 4734 nla_total_size(0) + /* IFLA_VXLAN_REMCSUM_NOPARTIAL */ 4735 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_VNIFILTER */ 4736 /* IFLA_VXLAN_RESERVED_BITS */ 4737 nla_total_size(sizeof(struct vxlanhdr)) + 4738 0; 4739 } 4740 4741 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev) 4742 { 4743 const struct vxlan_dev *vxlan = netdev_priv(dev); 4744 struct ifla_vxlan_port_range ports; 4745 const struct vxlan_config *cfg; 4746 4747 rcu_read_lock(); 4748 cfg = rcu_dereference(vxlan->cfg); 4749 4750 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(cfg->vni))) 4751 goto nla_put_failure; 4752 4753 if (!vxlan_addr_any(&cfg->remote_ip)) { 4754 if (cfg->remote_ip.sa.sa_family == AF_INET) { 4755 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP, 4756 cfg->remote_ip.sin.sin_addr.s_addr)) 4757 goto nla_put_failure; 4758 #if IS_ENABLED(CONFIG_IPV6) 4759 } else { 4760 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6, 4761 &cfg->remote_ip.sin6.sin6_addr)) 4762 goto nla_put_failure; 4763 #endif 4764 } 4765 } 4766 4767 if (cfg->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, cfg->remote_ifindex)) 4768 goto nla_put_failure; 4769 4770 if (!vxlan_addr_any(&cfg->saddr)) { 4771 if (cfg->saddr.sa.sa_family == AF_INET) { 4772 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL, 4773 cfg->saddr.sin.sin_addr.s_addr)) 4774 goto nla_put_failure; 4775 #if IS_ENABLED(CONFIG_IPV6) 4776 } else { 4777 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6, 4778 &cfg->saddr.sin6.sin6_addr)) 4779 goto nla_put_failure; 4780 #endif 4781 } 4782 } 4783 4784 if (nla_put_u8(skb, IFLA_VXLAN_TTL, cfg->ttl) || 4785 nla_put_u8(skb, IFLA_VXLAN_TTL_INHERIT, 4786 !!(cfg->flags & VXLAN_F_TTL_INHERIT)) || 4787 nla_put_u8(skb, IFLA_VXLAN_TOS, cfg->tos) || 4788 nla_put_u8(skb, IFLA_VXLAN_DF, cfg->df) || 4789 nla_put_be32(skb, IFLA_VXLAN_LABEL, cfg->label) || 4790 nla_put_u32(skb, IFLA_VXLAN_LABEL_POLICY, cfg->label_policy) || 4791 nla_put_u8(skb, IFLA_VXLAN_LEARNING, 4792 !!(cfg->flags & VXLAN_F_LEARN)) || 4793 nla_put_u8(skb, IFLA_VXLAN_PROXY, 4794 !!(cfg->flags & VXLAN_F_PROXY)) || 4795 nla_put_u8(skb, IFLA_VXLAN_RSC, 4796 !!(cfg->flags & VXLAN_F_RSC)) || 4797 nla_put_u8(skb, IFLA_VXLAN_L2MISS, 4798 !!(cfg->flags & VXLAN_F_L2MISS)) || 4799 nla_put_u8(skb, IFLA_VXLAN_L3MISS, 4800 !!(cfg->flags & VXLAN_F_L3MISS)) || 4801 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA, 4802 !!(cfg->flags & VXLAN_F_COLLECT_METADATA)) || 4803 nla_put_u32(skb, IFLA_VXLAN_AGEING, cfg->age_interval) || 4804 nla_put_u32(skb, IFLA_VXLAN_LIMIT, cfg->addrmax) || 4805 nla_put_be16(skb, IFLA_VXLAN_PORT, cfg->dst_port) || 4806 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM, 4807 !(cfg->flags & VXLAN_F_UDP_ZERO_CSUM_TX)) || 4808 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX, 4809 !!(cfg->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) || 4810 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, 4811 !!(cfg->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) || 4812 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX, 4813 !!(cfg->flags & VXLAN_F_REMCSUM_TX)) || 4814 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX, 4815 !!(cfg->flags & VXLAN_F_REMCSUM_RX)) || 4816 nla_put_u8(skb, IFLA_VXLAN_LOCALBYPASS, 4817 !!(cfg->flags & VXLAN_F_LOCALBYPASS))) 4818 goto nla_put_failure; 4819 4820 ports.low = htons(cfg->port_min); 4821 ports.high = htons(cfg->port_max); 4822 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports)) 4823 goto nla_put_failure; 4824 4825 if (cfg->flags & VXLAN_F_GBP && 4826 nla_put_flag(skb, IFLA_VXLAN_GBP)) 4827 goto nla_put_failure; 4828 4829 if (cfg->flags & VXLAN_F_GPE && 4830 nla_put_flag(skb, IFLA_VXLAN_GPE)) 4831 goto nla_put_failure; 4832 4833 if (cfg->flags & VXLAN_F_REMCSUM_NOPARTIAL && 4834 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL)) 4835 goto nla_put_failure; 4836 4837 if (cfg->flags & VXLAN_F_VNIFILTER && 4838 nla_put_u8(skb, IFLA_VXLAN_VNIFILTER, 4839 !!(cfg->flags & VXLAN_F_VNIFILTER))) 4840 goto nla_put_failure; 4841 4842 if (nla_put(skb, IFLA_VXLAN_RESERVED_BITS, 4843 sizeof(cfg->reserved_bits), 4844 &cfg->reserved_bits)) 4845 goto nla_put_failure; 4846 4847 rcu_read_unlock(); 4848 return 0; 4849 4850 nla_put_failure: 4851 rcu_read_unlock(); 4852 return -EMSGSIZE; 4853 } 4854 4855 static struct net *vxlan_get_link_net(const struct net_device *dev) 4856 { 4857 struct vxlan_dev *vxlan = netdev_priv(dev); 4858 4859 return READ_ONCE(vxlan->net); 4860 } 4861 4862 static struct rtnl_link_ops vxlan_link_ops __read_mostly = { 4863 .kind = "vxlan", 4864 .maxtype = IFLA_VXLAN_MAX, 4865 .policy = vxlan_policy, 4866 .priv_size = sizeof(struct vxlan_dev), 4867 .setup = vxlan_setup, 4868 .validate = vxlan_validate, 4869 .newlink = vxlan_newlink, 4870 .changelink = vxlan_changelink, 4871 .dellink = vxlan_dellink, 4872 .get_size = vxlan_get_size, 4873 .fill_info = vxlan_fill_info, 4874 .get_link_net = vxlan_get_link_net, 4875 }; 4876 4877 static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn, 4878 struct net_device *dev) 4879 { 4880 struct vxlan_dev *vxlan, *next; 4881 LIST_HEAD(list_kill); 4882 4883 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) { 4884 const struct vxlan_config *cfg = rtnl_dereference(vxlan->cfg); 4885 4886 /* In case we created vxlan device with carrier 4887 * and we loose the carrier due to module unload 4888 * we also need to remove vxlan device. In other 4889 * cases, it's not necessary and remote_ifindex 4890 * is 0 here, so no matches. 4891 */ 4892 if (cfg->remote_ifindex == dev->ifindex) 4893 vxlan_dellink(vxlan->dev, &list_kill); 4894 } 4895 4896 unregister_netdevice_many(&list_kill); 4897 } 4898 4899 static int vxlan_netdevice_event(struct notifier_block *unused, 4900 unsigned long event, void *ptr) 4901 { 4902 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 4903 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); 4904 4905 if ((event == NETDEV_REGISTER || event == NETDEV_UNREGISTER) && 4906 netif_is_vxlan(dev)) 4907 vxlan_vnifilter_seq_inc(dev_net(dev)); 4908 4909 if (event == NETDEV_UNREGISTER) 4910 vxlan_handle_lowerdev_unregister(vn, dev); 4911 else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO) 4912 vxlan_offload_rx_ports(dev, true); 4913 else if (event == NETDEV_UDP_TUNNEL_DROP_INFO) 4914 vxlan_offload_rx_ports(dev, false); 4915 4916 return NOTIFY_DONE; 4917 } 4918 4919 static struct notifier_block vxlan_notifier_block __read_mostly = { 4920 .notifier_call = vxlan_netdevice_event, 4921 }; 4922 4923 static void 4924 vxlan_fdb_offloaded_set(struct net_device *dev, 4925 struct switchdev_notifier_vxlan_fdb_info *fdb_info) 4926 { 4927 struct vxlan_dev *vxlan = netdev_priv(dev); 4928 struct vxlan_rdst *rdst; 4929 struct vxlan_fdb *f; 4930 4931 spin_lock_bh(&vxlan->hash_lock); 4932 4933 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni); 4934 if (!f) 4935 goto out; 4936 4937 rdst = vxlan_fdb_find_rdst(f, &fdb_info->remote_ip, 4938 fdb_info->remote_port, 4939 fdb_info->remote_vni, 4940 fdb_info->remote_ifindex); 4941 if (!rdst) 4942 goto out; 4943 4944 rdst->offloaded = fdb_info->offloaded; 4945 4946 out: 4947 spin_unlock_bh(&vxlan->hash_lock); 4948 } 4949 4950 static int 4951 vxlan_fdb_external_learn_add(struct net_device *dev, 4952 struct switchdev_notifier_vxlan_fdb_info *fdb_info) 4953 { 4954 struct vxlan_dev *vxlan = netdev_priv(dev); 4955 struct netlink_ext_ack *extack; 4956 int err; 4957 4958 extack = switchdev_notifier_info_to_extack(&fdb_info->info); 4959 4960 spin_lock_bh(&vxlan->hash_lock); 4961 err = vxlan_fdb_update(vxlan, fdb_info->eth_addr, &fdb_info->remote_ip, 4962 NUD_REACHABLE, 4963 NLM_F_CREATE | NLM_F_REPLACE, 4964 fdb_info->remote_port, 4965 fdb_info->vni, 4966 fdb_info->remote_vni, 4967 fdb_info->remote_ifindex, 4968 NTF_USE | NTF_SELF | NTF_EXT_LEARNED, 4969 0, false, extack); 4970 spin_unlock_bh(&vxlan->hash_lock); 4971 4972 return err; 4973 } 4974 4975 static int 4976 vxlan_fdb_external_learn_del(struct net_device *dev, 4977 struct switchdev_notifier_vxlan_fdb_info *fdb_info) 4978 { 4979 struct vxlan_dev *vxlan = netdev_priv(dev); 4980 struct vxlan_fdb *f; 4981 int err = 0; 4982 4983 spin_lock_bh(&vxlan->hash_lock); 4984 4985 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni); 4986 if (!f) 4987 err = -ENOENT; 4988 else if (f->flags & NTF_EXT_LEARNED) 4989 err = __vxlan_fdb_delete(vxlan, fdb_info->eth_addr, 4990 fdb_info->remote_ip, 4991 fdb_info->remote_port, 4992 fdb_info->vni, 4993 fdb_info->remote_vni, 4994 fdb_info->remote_ifindex, 4995 false); 4996 4997 spin_unlock_bh(&vxlan->hash_lock); 4998 4999 return err; 5000 } 5001 5002 static int vxlan_switchdev_event(struct notifier_block *unused, 5003 unsigned long event, void *ptr) 5004 { 5005 struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 5006 struct switchdev_notifier_vxlan_fdb_info *fdb_info; 5007 int err = 0; 5008 5009 switch (event) { 5010 case SWITCHDEV_VXLAN_FDB_OFFLOADED: 5011 vxlan_fdb_offloaded_set(dev, ptr); 5012 break; 5013 case SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE: 5014 fdb_info = ptr; 5015 err = vxlan_fdb_external_learn_add(dev, fdb_info); 5016 if (err) { 5017 err = notifier_from_errno(err); 5018 break; 5019 } 5020 fdb_info->offloaded = true; 5021 vxlan_fdb_offloaded_set(dev, fdb_info); 5022 break; 5023 case SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE: 5024 fdb_info = ptr; 5025 err = vxlan_fdb_external_learn_del(dev, fdb_info); 5026 if (err) { 5027 err = notifier_from_errno(err); 5028 break; 5029 } 5030 fdb_info->offloaded = false; 5031 vxlan_fdb_offloaded_set(dev, fdb_info); 5032 break; 5033 } 5034 5035 return err; 5036 } 5037 5038 static struct notifier_block vxlan_switchdev_notifier_block __read_mostly = { 5039 .notifier_call = vxlan_switchdev_event, 5040 }; 5041 5042 static void vxlan_fdb_nh_flush(struct nexthop *nh) 5043 { 5044 struct vxlan_fdb *fdb; 5045 struct vxlan_dev *vxlan; 5046 5047 rcu_read_lock(); 5048 list_for_each_entry_rcu(fdb, &nh->fdb_list, nh_list) { 5049 vxlan = rcu_dereference(fdb->vdev); 5050 WARN_ON(!vxlan); 5051 spin_lock_bh(&vxlan->hash_lock); 5052 if (!hlist_unhashed(&fdb->fdb_node)) 5053 vxlan_fdb_destroy(vxlan, fdb, false, false); 5054 spin_unlock_bh(&vxlan->hash_lock); 5055 } 5056 rcu_read_unlock(); 5057 } 5058 5059 static int vxlan_nexthop_event(struct notifier_block *nb, 5060 unsigned long event, void *ptr) 5061 { 5062 struct nh_notifier_info *info = ptr; 5063 struct nexthop *nh; 5064 5065 if (event != NEXTHOP_EVENT_DEL) 5066 return NOTIFY_DONE; 5067 5068 nh = nexthop_find_by_id(info->net, info->id); 5069 if (!nh) 5070 return NOTIFY_DONE; 5071 5072 vxlan_fdb_nh_flush(nh); 5073 5074 return NOTIFY_DONE; 5075 } 5076 5077 static __net_init int vxlan_init_net(struct net *net) 5078 { 5079 struct vxlan_net *vn = net_generic(net, vxlan_net_id); 5080 unsigned int h; 5081 5082 INIT_LIST_HEAD(&vn->vxlan_list); 5083 vn->nexthop_notifier_block.notifier_call = vxlan_nexthop_event; 5084 5085 for (h = 0; h < PORT_HASH_SIZE; ++h) 5086 INIT_HLIST_HEAD(&vn->sock_list[h]); 5087 5088 return register_nexthop_notifier(net, &vn->nexthop_notifier_block, 5089 NULL); 5090 } 5091 5092 static void __net_exit vxlan_destroy_tunnels(struct vxlan_net *vn, 5093 struct list_head *dev_to_kill) 5094 { 5095 struct vxlan_dev *vxlan, *next; 5096 5097 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) 5098 vxlan_dellink(vxlan->dev, dev_to_kill); 5099 } 5100 5101 static void __net_exit vxlan_exit_rtnl(struct net *net, 5102 struct list_head *dev_to_kill) 5103 { 5104 struct vxlan_net *vn = net_generic(net, vxlan_net_id); 5105 5106 ASSERT_RTNL_NET(net); 5107 5108 __unregister_nexthop_notifier(net, &vn->nexthop_notifier_block); 5109 vxlan_destroy_tunnels(vn, dev_to_kill); 5110 } 5111 5112 static void __net_exit vxlan_exit_net(struct net *net) 5113 { 5114 struct vxlan_net *vn = net_generic(net, vxlan_net_id); 5115 unsigned int h; 5116 5117 for (h = 0; h < PORT_HASH_SIZE; ++h) 5118 WARN_ON_ONCE(!hlist_empty(&vn->sock_list[h])); 5119 } 5120 5121 static struct pernet_operations vxlan_net_ops = { 5122 .init = vxlan_init_net, 5123 .exit_rtnl = vxlan_exit_rtnl, 5124 .exit = vxlan_exit_net, 5125 .id = &vxlan_net_id, 5126 .size = sizeof(struct vxlan_net), 5127 }; 5128 5129 static int __init vxlan_init_module(void) 5130 { 5131 int rc; 5132 5133 rc = register_pernet_subsys(&vxlan_net_ops); 5134 if (rc) 5135 goto out1; 5136 5137 rc = register_netdevice_notifier(&vxlan_notifier_block); 5138 if (rc) 5139 goto out2; 5140 5141 rc = register_switchdev_notifier(&vxlan_switchdev_notifier_block); 5142 if (rc) 5143 goto out3; 5144 5145 rc = rtnl_link_register(&vxlan_link_ops); 5146 if (rc) 5147 goto out4; 5148 5149 rc = vxlan_vnifilter_init(); 5150 if (rc) 5151 goto out5; 5152 5153 return 0; 5154 out5: 5155 rtnl_link_unregister(&vxlan_link_ops); 5156 out4: 5157 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block); 5158 out3: 5159 unregister_netdevice_notifier(&vxlan_notifier_block); 5160 out2: 5161 unregister_pernet_subsys(&vxlan_net_ops); 5162 out1: 5163 return rc; 5164 } 5165 late_initcall(vxlan_init_module); 5166 5167 static void __exit vxlan_cleanup_module(void) 5168 { 5169 vxlan_vnifilter_uninit(); 5170 rtnl_link_unregister(&vxlan_link_ops); 5171 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block); 5172 unregister_netdevice_notifier(&vxlan_notifier_block); 5173 unregister_pernet_subsys(&vxlan_net_ops); 5174 /* rcu_barrier() is called by netns */ 5175 } 5176 module_exit(vxlan_cleanup_module); 5177 5178 MODULE_LICENSE("GPL"); 5179 MODULE_VERSION(VXLAN_VERSION); 5180 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>"); 5181 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic"); 5182 MODULE_ALIAS_RTNL_LINK("vxlan"); 5183