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