1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* -*- linux-c -*- 3 * INET 802.1Q VLAN 4 * Ethernet-type device handling. 5 * 6 * Authors: Ben Greear <greearb@candelatech.com> 7 * Please send support related email to: netdev@vger.kernel.org 8 * VLAN Home Page: http://www.candelatech.com/~greear/vlan.html 9 * 10 * Fixes: Mar 22 2001: Martin Bokaemper <mbokaemper@unispherenetworks.com> 11 * - reset skb->pkt_type on incoming packets when MAC was changed 12 * - see that changed MAC is saddr for outgoing packets 13 * Oct 20, 2001: Ard van Breeman: 14 * - Fix MC-list, finally. 15 * - Flush MC-list on VLAN destroy. 16 */ 17 18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 19 20 #include <linux/module.h> 21 #include <linux/slab.h> 22 #include <linux/skbuff.h> 23 #include <linux/netdevice.h> 24 #include <linux/net_tstamp.h> 25 #include <linux/etherdevice.h> 26 #include <linux/ethtool.h> 27 #include <linux/phy.h> 28 #include <net/arp.h> 29 #include <net/macsec.h> 30 #include <net/netdev_lock.h> 31 32 #include "vlan.h" 33 #include "vlanproc.h" 34 #include <linux/if_vlan.h> 35 #include <linux/netpoll.h> 36 37 /* 38 * Create the VLAN header for an arbitrary protocol layer 39 * 40 * saddr=NULL means use device source address 41 * daddr=NULL means leave destination address (eg unresolved arp) 42 * 43 * This is called when the SKB is moving down the stack towards the 44 * physical devices. 45 */ 46 static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev, 47 unsigned short type, 48 const void *daddr, const void *saddr, 49 unsigned int len) 50 { 51 struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 52 struct vlan_hdr *vhdr; 53 unsigned int vhdrlen = 0; 54 u16 vlan_tci = 0; 55 int rc; 56 57 if (!(READ_ONCE(vlan->flags) & VLAN_FLAG_REORDER_HDR)) { 58 unsigned int hlen = READ_ONCE(dev->hard_header_len) + 59 READ_ONCE(dev->needed_headroom); 60 61 if (skb_cow_head(skb, hlen) < 0) 62 return -ENOMEM; 63 vhdr = skb_push(skb, VLAN_HLEN); 64 65 vlan_tci = vlan->vlan_id; 66 vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb->priority); 67 vhdr->h_vlan_TCI = htons(vlan_tci); 68 69 /* 70 * Set the protocol type. For a packet of type ETH_P_802_3/2 we 71 * put the length in here instead. 72 */ 73 if (type != ETH_P_802_3 && type != ETH_P_802_2) 74 vhdr->h_vlan_encapsulated_proto = htons(type); 75 else 76 vhdr->h_vlan_encapsulated_proto = htons(len); 77 78 skb->protocol = vlan->vlan_proto; 79 type = ntohs(vlan->vlan_proto); 80 vhdrlen = VLAN_HLEN; 81 } 82 83 /* Before delegating work to the lower layer, enter our MAC-address */ 84 if (saddr == NULL) 85 saddr = dev->dev_addr; 86 87 /* Now make the underlying real hard header */ 88 dev = vlan->real_dev; 89 rc = dev_hard_header(skb, dev, type, daddr, saddr, len + vhdrlen); 90 if (rc > 0) 91 rc += vhdrlen; 92 return rc; 93 } 94 95 static inline netdev_tx_t vlan_netpoll_send_skb(struct vlan_dev_priv *vlan, struct sk_buff *skb) 96 { 97 #ifdef CONFIG_NET_POLL_CONTROLLER 98 return netpoll_send_skb(vlan->netpoll, skb); 99 #else 100 BUG(); 101 return NETDEV_TX_OK; 102 #endif 103 } 104 105 static netdev_tx_t vlan_dev_hard_start_xmit(struct sk_buff *skb, 106 struct net_device *dev) 107 { 108 struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 109 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data); 110 unsigned int len; 111 int ret; 112 113 /* Handle non-VLAN frames if they are sent to us, for example by DHCP. 114 * 115 * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING 116 * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs... 117 */ 118 if (READ_ONCE(vlan->flags) & VLAN_FLAG_REORDER_HDR || 119 veth->h_vlan_proto != vlan->vlan_proto) { 120 u16 vlan_tci; 121 vlan_tci = vlan->vlan_id; 122 vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb->priority); 123 __vlan_hwaccel_put_tag(skb, vlan->vlan_proto, vlan_tci); 124 } 125 126 skb->dev = vlan->real_dev; 127 len = skb->len; 128 if (unlikely(netpoll_tx_running(dev))) 129 return vlan_netpoll_send_skb(vlan, skb); 130 131 ret = dev_queue_xmit(skb); 132 133 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) { 134 struct vlan_pcpu_stats *stats; 135 136 stats = this_cpu_ptr(vlan->vlan_pcpu_stats); 137 u64_stats_update_begin(&stats->syncp); 138 u64_stats_inc(&stats->tx_packets); 139 u64_stats_add(&stats->tx_bytes, len); 140 u64_stats_update_end(&stats->syncp); 141 } else { 142 this_cpu_inc(vlan->vlan_pcpu_stats->tx_dropped); 143 } 144 145 return ret; 146 } 147 148 static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu) 149 { 150 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 151 unsigned int max_mtu = real_dev->mtu; 152 153 if (netif_reduces_vlan_mtu(real_dev)) 154 max_mtu -= VLAN_HLEN; 155 if (max_mtu < new_mtu) 156 return -ERANGE; 157 158 WRITE_ONCE(dev->mtu, new_mtu); 159 160 return 0; 161 } 162 163 void vlan_dev_set_ingress_priority(const struct net_device *dev, 164 u32 skb_prio, u16 vlan_prio) 165 { 166 struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 167 u32 *map = &vlan->ingress_priority_map[vlan_prio & 0x7]; 168 169 if (*map && !skb_prio) 170 WRITE_ONCE(vlan->nr_ingress_mappings, 171 vlan->nr_ingress_mappings - 1); 172 else if (!*map && skb_prio) 173 WRITE_ONCE(vlan->nr_ingress_mappings, 174 vlan->nr_ingress_mappings + 1); 175 176 WRITE_ONCE(*map, skb_prio); 177 } 178 179 int vlan_dev_set_egress_priority(const struct net_device *dev, 180 u32 skb_prio, u16 vlan_prio) 181 { 182 struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 183 struct vlan_priority_tci_mapping __rcu **mpp; 184 struct vlan_priority_tci_mapping *mp; 185 struct vlan_priority_tci_mapping *np; 186 u32 bucket = skb_prio & 0xF; 187 u32 vlan_qos = (vlan_prio << VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK; 188 189 /* See if a priority mapping exists.. */ 190 mpp = &vlan->egress_priority_map[bucket]; 191 mp = rtnl_dereference(*mpp); 192 while (mp) { 193 if (mp->priority == skb_prio) { 194 if (!vlan_qos) { 195 rcu_assign_pointer(*mpp, rtnl_dereference(mp->next)); 196 WRITE_ONCE(vlan->nr_egress_mappings, 197 vlan->nr_egress_mappings - 1); 198 kfree_rcu(mp, rcu); 199 } else { 200 WRITE_ONCE(mp->vlan_qos, vlan_qos); 201 } 202 return 0; 203 } 204 mpp = &mp->next; 205 mp = rtnl_dereference(*mpp); 206 } 207 208 /* Create a new mapping then. */ 209 if (!vlan_qos) 210 return 0; 211 212 np = kmalloc_obj(struct vlan_priority_tci_mapping); 213 if (!np) 214 return -ENOBUFS; 215 216 np->priority = skb_prio; 217 np->vlan_qos = vlan_qos; 218 RCU_INIT_POINTER(np->next, rtnl_dereference(vlan->egress_priority_map[bucket])); 219 rcu_assign_pointer(vlan->egress_priority_map[bucket], np); 220 if (vlan_qos) 221 WRITE_ONCE(vlan->nr_egress_mappings, 222 vlan->nr_egress_mappings + 1); 223 return 0; 224 } 225 226 /* Flags are defined in the vlan_flags enum in 227 * include/uapi/linux/if_vlan.h file. 228 */ 229 int vlan_dev_change_flags(const struct net_device *dev, u32 flags, u32 mask) 230 { 231 struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 232 u32 old_flags = vlan->flags; 233 u32 new_flags; 234 235 if (mask & ~(VLAN_FLAG_REORDER_HDR | VLAN_FLAG_GVRP | 236 VLAN_FLAG_LOOSE_BINDING | VLAN_FLAG_MVRP | 237 VLAN_FLAG_BRIDGE_BINDING)) 238 return -EINVAL; 239 240 new_flags = (old_flags & ~mask) | (flags & mask); 241 WRITE_ONCE(vlan->flags, new_flags); 242 243 if (netif_running(dev) && (new_flags ^ old_flags) & VLAN_FLAG_GVRP) { 244 if (new_flags & VLAN_FLAG_GVRP) 245 vlan_gvrp_request_join(dev); 246 else 247 vlan_gvrp_request_leave(dev); 248 } 249 250 if (netif_running(dev) && (new_flags ^ old_flags) & VLAN_FLAG_MVRP) { 251 if (new_flags & VLAN_FLAG_MVRP) 252 vlan_mvrp_request_join(dev); 253 else 254 vlan_mvrp_request_leave(dev); 255 } 256 return 0; 257 } 258 259 void vlan_dev_get_realdev_name(const struct net_device *dev, char *result, size_t size) 260 { 261 strscpy_pad(result, vlan_dev_priv(dev)->real_dev->name, size); 262 } 263 264 bool vlan_dev_inherit_address(struct net_device *dev, 265 struct net_device *real_dev) 266 { 267 if (dev->addr_assign_type != NET_ADDR_STOLEN) 268 return false; 269 270 eth_hw_addr_set(dev, real_dev->dev_addr); 271 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 272 return true; 273 } 274 275 static int vlan_dev_open(struct net_device *dev) 276 { 277 struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 278 struct net_device *real_dev = vlan->real_dev; 279 int err; 280 281 if (!(real_dev->flags & IFF_UP) && 282 !(vlan->flags & VLAN_FLAG_LOOSE_BINDING)) 283 return -ENETDOWN; 284 285 /* The explicit open supersedes any deferred link-state sync */ 286 netdev_work_cancel(dev, VLAN_WORK_LINK_STATE); 287 288 if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr) && 289 !vlan_dev_inherit_address(dev, real_dev)) { 290 err = dev_uc_add(real_dev, dev->dev_addr); 291 if (err < 0) 292 goto out; 293 } 294 295 ether_addr_copy(vlan->real_dev_addr, real_dev->dev_addr); 296 297 if (vlan->flags & VLAN_FLAG_GVRP) 298 vlan_gvrp_request_join(dev); 299 300 if (vlan->flags & VLAN_FLAG_MVRP) 301 vlan_mvrp_request_join(dev); 302 303 if (netif_carrier_ok(real_dev) && 304 !(vlan->flags & VLAN_FLAG_BRIDGE_BINDING)) 305 netif_carrier_on(dev); 306 return 0; 307 308 out: 309 netif_carrier_off(dev); 310 return err; 311 } 312 313 static int vlan_dev_stop(struct net_device *dev) 314 { 315 struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 316 struct net_device *real_dev = vlan->real_dev; 317 318 /* The explicit close supersedes any deferred link-state sync */ 319 netdev_work_cancel(dev, VLAN_WORK_LINK_STATE); 320 321 dev_mc_unsync(real_dev, dev); 322 dev_uc_unsync(real_dev, dev); 323 324 if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr)) 325 dev_uc_del(real_dev, dev->dev_addr); 326 327 if (!(vlan->flags & VLAN_FLAG_BRIDGE_BINDING)) 328 netif_carrier_off(dev); 329 return 0; 330 } 331 332 static int vlan_dev_set_mac_address(struct net_device *dev, void *p) 333 { 334 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 335 struct sockaddr *addr = p; 336 int err; 337 338 if (!is_valid_ether_addr(addr->sa_data)) 339 return -EADDRNOTAVAIL; 340 341 if (!(dev->flags & IFF_UP)) 342 goto out; 343 344 if (!ether_addr_equal(addr->sa_data, real_dev->dev_addr)) { 345 err = dev_uc_add(real_dev, addr->sa_data); 346 if (err < 0) 347 return err; 348 } 349 350 if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr)) 351 dev_uc_del(real_dev, dev->dev_addr); 352 353 out: 354 eth_hw_addr_set(dev, addr->sa_data); 355 return 0; 356 } 357 358 static int vlan_hwtstamp_get(struct net_device *dev, 359 struct kernel_hwtstamp_config *cfg) 360 { 361 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 362 363 return generic_hwtstamp_get_lower(real_dev, cfg); 364 } 365 366 static int vlan_hwtstamp_set(struct net_device *dev, 367 struct kernel_hwtstamp_config *cfg, 368 struct netlink_ext_ack *extack) 369 { 370 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 371 372 if (!net_eq(dev_net(dev), dev_net(real_dev))) 373 return -EOPNOTSUPP; 374 375 return generic_hwtstamp_set_lower(real_dev, cfg, extack); 376 } 377 378 static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 379 { 380 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 381 struct ifreq ifrr; 382 int err = -EOPNOTSUPP; 383 384 strscpy_pad(ifrr.ifr_name, real_dev->name, IFNAMSIZ); 385 ifrr.ifr_ifru = ifr->ifr_ifru; 386 387 switch (cmd) { 388 case SIOCGMIIPHY: 389 case SIOCGMIIREG: 390 case SIOCSMIIREG: 391 err = dev_eth_ioctl(real_dev, &ifrr, cmd); 392 break; 393 } 394 395 if (!err) 396 ifr->ifr_ifru = ifrr.ifr_ifru; 397 398 return err; 399 } 400 401 static int vlan_dev_neigh_setup(struct net_device *dev, struct neigh_parms *pa) 402 { 403 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 404 const struct net_device_ops *ops = real_dev->netdev_ops; 405 int err = 0; 406 407 if (netif_device_present(real_dev) && ops->ndo_neigh_setup) 408 err = ops->ndo_neigh_setup(real_dev, pa); 409 410 return err; 411 } 412 413 #if IS_ENABLED(CONFIG_FCOE) 414 static int vlan_dev_fcoe_ddp_setup(struct net_device *dev, u16 xid, 415 struct scatterlist *sgl, unsigned int sgc) 416 { 417 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 418 const struct net_device_ops *ops = real_dev->netdev_ops; 419 int rc = 0; 420 421 if (ops->ndo_fcoe_ddp_setup) 422 rc = ops->ndo_fcoe_ddp_setup(real_dev, xid, sgl, sgc); 423 424 return rc; 425 } 426 427 static int vlan_dev_fcoe_ddp_done(struct net_device *dev, u16 xid) 428 { 429 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 430 const struct net_device_ops *ops = real_dev->netdev_ops; 431 int len = 0; 432 433 if (ops->ndo_fcoe_ddp_done) 434 len = ops->ndo_fcoe_ddp_done(real_dev, xid); 435 436 return len; 437 } 438 439 static int vlan_dev_fcoe_enable(struct net_device *dev) 440 { 441 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 442 const struct net_device_ops *ops = real_dev->netdev_ops; 443 int rc = -EINVAL; 444 445 if (ops->ndo_fcoe_enable) 446 rc = ops->ndo_fcoe_enable(real_dev); 447 return rc; 448 } 449 450 static int vlan_dev_fcoe_disable(struct net_device *dev) 451 { 452 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 453 const struct net_device_ops *ops = real_dev->netdev_ops; 454 int rc = -EINVAL; 455 456 if (ops->ndo_fcoe_disable) 457 rc = ops->ndo_fcoe_disable(real_dev); 458 return rc; 459 } 460 461 static int vlan_dev_fcoe_ddp_target(struct net_device *dev, u16 xid, 462 struct scatterlist *sgl, unsigned int sgc) 463 { 464 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 465 const struct net_device_ops *ops = real_dev->netdev_ops; 466 int rc = 0; 467 468 if (ops->ndo_fcoe_ddp_target) 469 rc = ops->ndo_fcoe_ddp_target(real_dev, xid, sgl, sgc); 470 471 return rc; 472 } 473 #endif 474 475 #ifdef NETDEV_FCOE_WWNN 476 static int vlan_dev_fcoe_get_wwn(struct net_device *dev, u64 *wwn, int type) 477 { 478 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 479 const struct net_device_ops *ops = real_dev->netdev_ops; 480 int rc = -EINVAL; 481 482 if (ops->ndo_fcoe_get_wwn) 483 rc = ops->ndo_fcoe_get_wwn(real_dev, wwn, type); 484 return rc; 485 } 486 #endif 487 488 static void vlan_dev_change_rx_flags(struct net_device *dev, int change) 489 { 490 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 491 492 if (change & IFF_ALLMULTI) 493 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1); 494 if (change & IFF_PROMISC) 495 dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1); 496 } 497 498 static void vlan_dev_set_rx_mode(struct net_device *vlan_dev) 499 { 500 dev_mc_sync(vlan_dev_priv(vlan_dev)->real_dev, vlan_dev); 501 dev_uc_sync(vlan_dev_priv(vlan_dev)->real_dev, vlan_dev); 502 } 503 504 static __be16 vlan_parse_protocol(const struct sk_buff *skb) 505 { 506 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data); 507 508 return __vlan_get_protocol(skb, veth->h_vlan_proto, NULL); 509 } 510 511 static const struct header_ops vlan_header_ops = { 512 .create = vlan_dev_hard_header, 513 .parse = eth_header_parse, 514 .parse_protocol = vlan_parse_protocol, 515 }; 516 517 static const struct device_type vlan_type = { 518 .name = "vlan", 519 }; 520 521 static const struct net_device_ops vlan_netdev_ops; 522 523 static int vlan_dev_init(struct net_device *dev) 524 { 525 struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 526 struct net_device *real_dev = vlan->real_dev; 527 528 netif_carrier_off(dev); 529 530 /* IFF_BROADCAST|IFF_MULTICAST; ??? */ 531 dev->flags = real_dev->flags & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | 532 IFF_MASTER | IFF_SLAVE); 533 dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) | 534 (1<<__LINK_STATE_DORMANT))) | 535 (1<<__LINK_STATE_PRESENT); 536 537 if (vlan->flags & VLAN_FLAG_BRIDGE_BINDING) 538 dev->state |= (1 << __LINK_STATE_NOCARRIER); 539 540 dev->hw_features = NETIF_F_HW_CSUM | NETIF_F_SG | 541 NETIF_F_FRAGLIST | NETIF_F_GSO_SOFTWARE | 542 NETIF_F_GSO_ENCAP_ALL | 543 NETIF_F_HIGHDMA | NETIF_F_SCTP_CRC | 544 NETIF_F_FCOE_CRC | NETIF_F_FSO; 545 546 if (real_dev->vlan_features & NETIF_F_HW_MACSEC) 547 dev->hw_features |= NETIF_F_HW_MACSEC; 548 549 dev->features |= dev->hw_features; 550 dev->lltx = true; 551 dev->fcoe_mtu = true; 552 netif_inherit_tso_max(dev, real_dev); 553 if (dev->features & NETIF_F_VLAN_FEATURES) 554 netdev_warn(real_dev, "VLAN features are set incorrectly. Q-in-Q configurations may not work correctly.\n"); 555 556 dev->vlan_features = real_dev->vlan_features & 557 ~(NETIF_F_FCOE_CRC | NETIF_F_FSO); 558 dev->hw_enc_features = vlan_tnl_features(real_dev); 559 dev->mpls_features = real_dev->mpls_features; 560 561 /* ipv6 shared card related stuff */ 562 dev->dev_id = real_dev->dev_id; 563 564 if (is_zero_ether_addr(dev->dev_addr)) { 565 eth_hw_addr_set(dev, real_dev->dev_addr); 566 dev->addr_assign_type = NET_ADDR_STOLEN; 567 } 568 if (is_zero_ether_addr(dev->broadcast)) 569 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len); 570 571 #if IS_ENABLED(CONFIG_FCOE) 572 dev->fcoe_ddp_xid = real_dev->fcoe_ddp_xid; 573 #endif 574 575 dev->needed_headroom = real_dev->needed_headroom + VLAN_HLEN; 576 dev->needed_tailroom = real_dev->needed_tailroom; 577 dev->header_ops = &vlan_header_ops; 578 dev->hard_header_len = real_dev->hard_header_len; 579 580 dev->netdev_ops = &vlan_netdev_ops; 581 582 SET_NETDEV_DEVTYPE(dev, &vlan_type); 583 584 netdev_lockdep_set_classes(dev); 585 586 vlan->vlan_pcpu_stats = netdev_alloc_pcpu_stats(struct vlan_pcpu_stats); 587 if (!vlan->vlan_pcpu_stats) 588 return -ENOMEM; 589 590 /* Get vlan's reference to real_dev */ 591 netdev_hold(real_dev, &vlan->dev_tracker, GFP_KERNEL); 592 593 return 0; 594 } 595 596 /* Note: this function might be called multiple times for the same device. */ 597 void vlan_dev_free_egress_priority(const struct net_device *dev) 598 { 599 struct vlan_priority_tci_mapping *pm; 600 struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 601 int i; 602 603 for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) { 604 pm = rtnl_dereference(vlan->egress_priority_map[i]); 605 RCU_INIT_POINTER(vlan->egress_priority_map[i], NULL); 606 while (pm) { 607 struct vlan_priority_tci_mapping *next; 608 609 next = rtnl_dereference(pm->next); 610 kfree_rcu(pm, rcu); 611 pm = next; 612 } 613 } 614 WRITE_ONCE(vlan->nr_egress_mappings, 0); 615 } 616 617 static void vlan_dev_uninit(struct net_device *dev) 618 { 619 vlan_dev_free_egress_priority(dev); 620 } 621 622 static netdev_features_t vlan_dev_fix_features(struct net_device *dev, 623 netdev_features_t features) 624 { 625 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 626 netdev_features_t old_features = features; 627 netdev_features_t lower_features; 628 629 lower_features = netdev_intersect_features((real_dev->vlan_features | 630 NETIF_F_RXCSUM), 631 real_dev->features); 632 633 /* Add HW_CSUM setting to preserve user ability to control 634 * checksum offload on the vlan device. 635 */ 636 if (lower_features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM)) 637 lower_features |= NETIF_F_HW_CSUM; 638 features = netdev_intersect_features(features, lower_features); 639 features |= old_features & (NETIF_F_SOFT_FEATURES | NETIF_F_GSO_SOFTWARE); 640 641 return features; 642 } 643 644 static int vlan_ethtool_get_link_ksettings(struct net_device *dev, 645 struct ethtool_link_ksettings *cmd) 646 { 647 const struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 648 649 return __ethtool_get_link_ksettings(vlan->real_dev, cmd); 650 } 651 652 static void vlan_ethtool_get_drvinfo(struct net_device *dev, 653 struct ethtool_drvinfo *info) 654 { 655 strscpy(info->driver, vlan_fullname, sizeof(info->driver)); 656 strscpy(info->version, vlan_version, sizeof(info->version)); 657 strscpy(info->fw_version, "N/A", sizeof(info->fw_version)); 658 } 659 660 static int vlan_ethtool_get_ts_info(struct net_device *dev, 661 struct kernel_ethtool_ts_info *info) 662 { 663 const struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 664 return ethtool_get_ts_info_by_layer(vlan->real_dev, info); 665 } 666 667 static void vlan_dev_get_stats64(struct net_device *dev, 668 struct rtnl_link_stats64 *stats) 669 { 670 struct vlan_pcpu_stats *p; 671 u32 rx_errors = 0, tx_dropped = 0; 672 int i; 673 674 for_each_possible_cpu(i) { 675 u64 rxpackets, rxbytes, rxmulticast, txpackets, txbytes; 676 unsigned int start; 677 678 p = per_cpu_ptr(vlan_dev_priv(dev)->vlan_pcpu_stats, i); 679 do { 680 start = u64_stats_fetch_begin(&p->syncp); 681 rxpackets = u64_stats_read(&p->rx_packets); 682 rxbytes = u64_stats_read(&p->rx_bytes); 683 rxmulticast = u64_stats_read(&p->rx_multicast); 684 txpackets = u64_stats_read(&p->tx_packets); 685 txbytes = u64_stats_read(&p->tx_bytes); 686 } while (u64_stats_fetch_retry(&p->syncp, start)); 687 688 stats->rx_packets += rxpackets; 689 stats->rx_bytes += rxbytes; 690 stats->multicast += rxmulticast; 691 stats->tx_packets += txpackets; 692 stats->tx_bytes += txbytes; 693 /* rx_errors & tx_dropped are u32 */ 694 rx_errors += READ_ONCE(p->rx_errors); 695 tx_dropped += READ_ONCE(p->tx_dropped); 696 } 697 stats->rx_errors = rx_errors; 698 stats->tx_dropped = tx_dropped; 699 } 700 701 #ifdef CONFIG_NET_POLL_CONTROLLER 702 static void vlan_dev_poll_controller(struct net_device *dev) 703 { 704 return; 705 } 706 707 static int vlan_dev_netpoll_setup(struct net_device *dev) 708 { 709 struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 710 struct net_device *real_dev = vlan->real_dev; 711 struct netpoll *netpoll; 712 int err = 0; 713 714 netpoll = kzalloc_obj(*netpoll); 715 err = -ENOMEM; 716 if (!netpoll) 717 goto out; 718 719 err = __netpoll_setup(netpoll, real_dev); 720 if (err) { 721 kfree(netpoll); 722 goto out; 723 } 724 725 vlan->netpoll = netpoll; 726 727 out: 728 return err; 729 } 730 731 static void vlan_dev_netpoll_cleanup(struct net_device *dev) 732 { 733 struct vlan_dev_priv *vlan= vlan_dev_priv(dev); 734 struct netpoll *netpoll = vlan->netpoll; 735 736 if (!netpoll) 737 return; 738 739 vlan->netpoll = NULL; 740 __netpoll_free(netpoll); 741 } 742 #endif /* CONFIG_NET_POLL_CONTROLLER */ 743 744 static int vlan_dev_get_iflink(const struct net_device *dev) 745 { 746 const struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 747 748 return READ_ONCE(real_dev->ifindex); 749 } 750 751 static int vlan_dev_fill_forward_path(struct net_device_path_ctx *ctx, 752 struct net_device_path *path) 753 { 754 struct vlan_dev_priv *vlan = vlan_dev_priv(ctx->dev); 755 756 path->type = DEV_PATH_VLAN; 757 path->encap.id = vlan->vlan_id; 758 path->encap.proto = vlan->vlan_proto; 759 path->dev = ctx->dev; 760 ctx->dev = vlan->real_dev; 761 if (ctx->num_vlans >= ARRAY_SIZE(ctx->vlan)) 762 return -ENOSPC; 763 764 ctx->vlan[ctx->num_vlans].id = vlan->vlan_id; 765 ctx->vlan[ctx->num_vlans].proto = vlan->vlan_proto; 766 ctx->num_vlans++; 767 768 return 0; 769 } 770 771 #if IS_ENABLED(CONFIG_MACSEC) 772 773 static const struct macsec_ops *vlan_get_macsec_ops(const struct macsec_context *ctx) 774 { 775 return vlan_dev_priv(ctx->netdev)->real_dev->macsec_ops; 776 } 777 778 static int vlan_macsec_offload(int (* const func)(struct macsec_context *), 779 struct macsec_context *ctx) 780 { 781 if (unlikely(!func)) 782 return 0; 783 784 return (*func)(ctx); 785 } 786 787 static int vlan_macsec_dev_open(struct macsec_context *ctx) 788 { 789 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 790 791 if (!ops) 792 return -EOPNOTSUPP; 793 794 return vlan_macsec_offload(ops->mdo_dev_open, ctx); 795 } 796 797 static int vlan_macsec_dev_stop(struct macsec_context *ctx) 798 { 799 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 800 801 if (!ops) 802 return -EOPNOTSUPP; 803 804 return vlan_macsec_offload(ops->mdo_dev_stop, ctx); 805 } 806 807 static int vlan_macsec_add_secy(struct macsec_context *ctx) 808 { 809 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 810 811 if (!ops) 812 return -EOPNOTSUPP; 813 814 return vlan_macsec_offload(ops->mdo_add_secy, ctx); 815 } 816 817 static int vlan_macsec_upd_secy(struct macsec_context *ctx) 818 { 819 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 820 821 if (!ops) 822 return -EOPNOTSUPP; 823 824 return vlan_macsec_offload(ops->mdo_upd_secy, ctx); 825 } 826 827 static int vlan_macsec_del_secy(struct macsec_context *ctx) 828 { 829 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 830 831 if (!ops) 832 return -EOPNOTSUPP; 833 834 return vlan_macsec_offload(ops->mdo_del_secy, ctx); 835 } 836 837 static int vlan_macsec_add_rxsc(struct macsec_context *ctx) 838 { 839 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 840 841 if (!ops) 842 return -EOPNOTSUPP; 843 844 return vlan_macsec_offload(ops->mdo_add_rxsc, ctx); 845 } 846 847 static int vlan_macsec_upd_rxsc(struct macsec_context *ctx) 848 { 849 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 850 851 if (!ops) 852 return -EOPNOTSUPP; 853 854 return vlan_macsec_offload(ops->mdo_upd_rxsc, ctx); 855 } 856 857 static int vlan_macsec_del_rxsc(struct macsec_context *ctx) 858 { 859 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 860 861 if (!ops) 862 return -EOPNOTSUPP; 863 864 return vlan_macsec_offload(ops->mdo_del_rxsc, ctx); 865 } 866 867 static int vlan_macsec_add_rxsa(struct macsec_context *ctx) 868 { 869 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 870 871 if (!ops) 872 return -EOPNOTSUPP; 873 874 return vlan_macsec_offload(ops->mdo_add_rxsa, ctx); 875 } 876 877 static int vlan_macsec_upd_rxsa(struct macsec_context *ctx) 878 { 879 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 880 881 if (!ops) 882 return -EOPNOTSUPP; 883 884 return vlan_macsec_offload(ops->mdo_upd_rxsa, ctx); 885 } 886 887 static int vlan_macsec_del_rxsa(struct macsec_context *ctx) 888 { 889 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 890 891 if (!ops) 892 return -EOPNOTSUPP; 893 894 return vlan_macsec_offload(ops->mdo_del_rxsa, ctx); 895 } 896 897 static int vlan_macsec_add_txsa(struct macsec_context *ctx) 898 { 899 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 900 901 if (!ops) 902 return -EOPNOTSUPP; 903 904 return vlan_macsec_offload(ops->mdo_add_txsa, ctx); 905 } 906 907 static int vlan_macsec_upd_txsa(struct macsec_context *ctx) 908 { 909 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 910 911 if (!ops) 912 return -EOPNOTSUPP; 913 914 return vlan_macsec_offload(ops->mdo_upd_txsa, ctx); 915 } 916 917 static int vlan_macsec_del_txsa(struct macsec_context *ctx) 918 { 919 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 920 921 if (!ops) 922 return -EOPNOTSUPP; 923 924 return vlan_macsec_offload(ops->mdo_del_txsa, ctx); 925 } 926 927 static int vlan_macsec_get_dev_stats(struct macsec_context *ctx) 928 { 929 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 930 931 if (!ops) 932 return -EOPNOTSUPP; 933 934 return vlan_macsec_offload(ops->mdo_get_dev_stats, ctx); 935 } 936 937 static int vlan_macsec_get_tx_sc_stats(struct macsec_context *ctx) 938 { 939 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 940 941 if (!ops) 942 return -EOPNOTSUPP; 943 944 return vlan_macsec_offload(ops->mdo_get_tx_sc_stats, ctx); 945 } 946 947 static int vlan_macsec_get_tx_sa_stats(struct macsec_context *ctx) 948 { 949 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 950 951 if (!ops) 952 return -EOPNOTSUPP; 953 954 return vlan_macsec_offload(ops->mdo_get_tx_sa_stats, ctx); 955 } 956 957 static int vlan_macsec_get_rx_sc_stats(struct macsec_context *ctx) 958 { 959 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 960 961 if (!ops) 962 return -EOPNOTSUPP; 963 964 return vlan_macsec_offload(ops->mdo_get_rx_sc_stats, ctx); 965 } 966 967 static int vlan_macsec_get_rx_sa_stats(struct macsec_context *ctx) 968 { 969 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 970 971 if (!ops) 972 return -EOPNOTSUPP; 973 974 return vlan_macsec_offload(ops->mdo_get_rx_sa_stats, ctx); 975 } 976 977 static const struct macsec_ops macsec_offload_ops = { 978 /* Device wide */ 979 .mdo_dev_open = vlan_macsec_dev_open, 980 .mdo_dev_stop = vlan_macsec_dev_stop, 981 /* SecY */ 982 .mdo_add_secy = vlan_macsec_add_secy, 983 .mdo_upd_secy = vlan_macsec_upd_secy, 984 .mdo_del_secy = vlan_macsec_del_secy, 985 /* Security channels */ 986 .mdo_add_rxsc = vlan_macsec_add_rxsc, 987 .mdo_upd_rxsc = vlan_macsec_upd_rxsc, 988 .mdo_del_rxsc = vlan_macsec_del_rxsc, 989 /* Security associations */ 990 .mdo_add_rxsa = vlan_macsec_add_rxsa, 991 .mdo_upd_rxsa = vlan_macsec_upd_rxsa, 992 .mdo_del_rxsa = vlan_macsec_del_rxsa, 993 .mdo_add_txsa = vlan_macsec_add_txsa, 994 .mdo_upd_txsa = vlan_macsec_upd_txsa, 995 .mdo_del_txsa = vlan_macsec_del_txsa, 996 /* Statistics */ 997 .mdo_get_dev_stats = vlan_macsec_get_dev_stats, 998 .mdo_get_tx_sc_stats = vlan_macsec_get_tx_sc_stats, 999 .mdo_get_tx_sa_stats = vlan_macsec_get_tx_sa_stats, 1000 .mdo_get_rx_sc_stats = vlan_macsec_get_rx_sc_stats, 1001 .mdo_get_rx_sa_stats = vlan_macsec_get_rx_sa_stats, 1002 }; 1003 1004 #endif 1005 1006 static const struct ethtool_ops vlan_ethtool_ops = { 1007 .get_link_ksettings = vlan_ethtool_get_link_ksettings, 1008 .get_drvinfo = vlan_ethtool_get_drvinfo, 1009 .get_link = ethtool_op_get_link, 1010 .get_ts_info = vlan_ethtool_get_ts_info, 1011 }; 1012 1013 static void vlan_transfer_features(struct net_device *dev, 1014 struct net_device *vlandev) 1015 { 1016 struct vlan_dev_priv *vlan = vlan_dev_priv(vlandev); 1017 1018 netif_inherit_tso_max(vlandev, dev); 1019 1020 vlandev->needed_headroom = dev->needed_headroom + VLAN_HLEN; 1021 vlandev->needed_tailroom = dev->needed_tailroom; 1022 vlandev->hard_header_len = dev->hard_header_len; 1023 1024 #if IS_ENABLED(CONFIG_FCOE) 1025 vlandev->fcoe_ddp_xid = dev->fcoe_ddp_xid; 1026 #endif 1027 1028 vlandev->priv_flags &= ~IFF_XMIT_DST_RELEASE; 1029 vlandev->priv_flags |= (vlan->real_dev->priv_flags & IFF_XMIT_DST_RELEASE); 1030 vlandev->hw_enc_features = vlan_tnl_features(vlan->real_dev); 1031 1032 netdev_update_features(vlandev); 1033 } 1034 1035 static void vlan_dev_work(struct net_device *vlandev, unsigned long events) 1036 { 1037 struct vlan_dev_priv *vlan = vlan_dev_priv(vlandev); 1038 struct net_device *real_dev = vlan->real_dev; 1039 bool loose = vlan->flags & VLAN_FLAG_LOOSE_BINDING; 1040 unsigned int flgs; 1041 1042 if (events & VLAN_WORK_LINK_STATE) { 1043 flgs = netif_get_flags(vlandev); 1044 if (real_dev->flags & IFF_UP) { 1045 if (!(flgs & IFF_UP)) { 1046 if (!loose) 1047 netif_change_flags(vlandev, 1048 flgs | IFF_UP, NULL); 1049 vlan_stacked_transfer_operstate(real_dev, 1050 vlandev, vlan); 1051 } 1052 } else if ((flgs & IFF_UP) && !loose) { 1053 netif_change_flags(vlandev, flgs & ~IFF_UP, NULL); 1054 vlan_stacked_transfer_operstate(real_dev, vlandev, vlan); 1055 } 1056 } 1057 1058 if ((events & VLAN_WORK_MTU) && vlandev->mtu > real_dev->mtu) 1059 netif_set_mtu(vlandev, real_dev->mtu); 1060 1061 if (events & VLAN_WORK_FEATURES) 1062 vlan_transfer_features(real_dev, vlandev); 1063 } 1064 1065 static const struct net_device_ops vlan_netdev_ops = { 1066 .ndo_change_mtu = vlan_dev_change_mtu, 1067 .ndo_init = vlan_dev_init, 1068 .ndo_uninit = vlan_dev_uninit, 1069 .ndo_open = vlan_dev_open, 1070 .ndo_stop = vlan_dev_stop, 1071 .ndo_start_xmit = vlan_dev_hard_start_xmit, 1072 .ndo_validate_addr = eth_validate_addr, 1073 .ndo_set_mac_address = vlan_dev_set_mac_address, 1074 .ndo_set_rx_mode = vlan_dev_set_rx_mode, 1075 .ndo_change_rx_flags = vlan_dev_change_rx_flags, 1076 .ndo_work = vlan_dev_work, 1077 .ndo_eth_ioctl = vlan_dev_ioctl, 1078 .ndo_neigh_setup = vlan_dev_neigh_setup, 1079 .ndo_get_stats64 = vlan_dev_get_stats64, 1080 #if IS_ENABLED(CONFIG_FCOE) 1081 .ndo_fcoe_ddp_setup = vlan_dev_fcoe_ddp_setup, 1082 .ndo_fcoe_ddp_done = vlan_dev_fcoe_ddp_done, 1083 .ndo_fcoe_enable = vlan_dev_fcoe_enable, 1084 .ndo_fcoe_disable = vlan_dev_fcoe_disable, 1085 .ndo_fcoe_ddp_target = vlan_dev_fcoe_ddp_target, 1086 #endif 1087 #ifdef NETDEV_FCOE_WWNN 1088 .ndo_fcoe_get_wwn = vlan_dev_fcoe_get_wwn, 1089 #endif 1090 #ifdef CONFIG_NET_POLL_CONTROLLER 1091 .ndo_poll_controller = vlan_dev_poll_controller, 1092 .ndo_netpoll_setup = vlan_dev_netpoll_setup, 1093 .ndo_netpoll_cleanup = vlan_dev_netpoll_cleanup, 1094 #endif 1095 .ndo_fix_features = vlan_dev_fix_features, 1096 .ndo_get_iflink = vlan_dev_get_iflink, 1097 .ndo_fill_forward_path = vlan_dev_fill_forward_path, 1098 .ndo_hwtstamp_get = vlan_hwtstamp_get, 1099 .ndo_hwtstamp_set = vlan_hwtstamp_set, 1100 }; 1101 1102 static void vlan_dev_free(struct net_device *dev) 1103 { 1104 struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 1105 1106 free_percpu(vlan->vlan_pcpu_stats); 1107 vlan->vlan_pcpu_stats = NULL; 1108 1109 /* Get rid of the vlan's reference to real_dev */ 1110 netdev_put(vlan->real_dev, &vlan->dev_tracker); 1111 } 1112 1113 void vlan_setup(struct net_device *dev) 1114 { 1115 ether_setup(dev); 1116 1117 dev->priv_flags |= IFF_802_1Q_VLAN | IFF_NO_QUEUE; 1118 dev->priv_flags |= IFF_UNICAST_FLT; 1119 dev->priv_flags &= ~IFF_TX_SKB_SHARING; 1120 netif_keep_dst(dev); 1121 1122 dev->netdev_ops = &vlan_netdev_ops; 1123 dev->needs_free_netdev = true; 1124 dev->priv_destructor = vlan_dev_free; 1125 dev->ethtool_ops = &vlan_ethtool_ops; 1126 1127 #if IS_ENABLED(CONFIG_MACSEC) 1128 dev->macsec_ops = &macsec_offload_ops; 1129 #endif 1130 dev->min_mtu = 0; 1131 dev->max_mtu = ETH_MAX_MTU; 1132 1133 eth_zero_addr(dev->broadcast); 1134 } 1135