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