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