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 *mp = NULL; 176 struct vlan_priority_tci_mapping *np; 177 u32 vlan_qos = (vlan_prio << VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK; 178 179 /* See if a priority mapping exists.. */ 180 mp = vlan->egress_priority_map[skb_prio & 0xF]; 181 while (mp) { 182 if (mp->priority == skb_prio) { 183 if (mp->vlan_qos && !vlan_qos) 184 vlan->nr_egress_mappings--; 185 else if (!mp->vlan_qos && vlan_qos) 186 vlan->nr_egress_mappings++; 187 mp->vlan_qos = vlan_qos; 188 return 0; 189 } 190 mp = mp->next; 191 } 192 193 /* Create a new mapping then. */ 194 mp = vlan->egress_priority_map[skb_prio & 0xF]; 195 np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL); 196 if (!np) 197 return -ENOBUFS; 198 199 np->next = mp; 200 np->priority = skb_prio; 201 np->vlan_qos = vlan_qos; 202 /* Before inserting this element in hash table, make sure all its fields 203 * are committed to memory. 204 * coupled with smp_rmb() in vlan_dev_get_egress_qos_mask() 205 */ 206 smp_wmb(); 207 vlan->egress_priority_map[skb_prio & 0xF] = np; 208 if (vlan_qos) 209 vlan->nr_egress_mappings++; 210 return 0; 211 } 212 213 /* Flags are defined in the vlan_flags enum in 214 * include/uapi/linux/if_vlan.h file. 215 */ 216 int vlan_dev_change_flags(const struct net_device *dev, u32 flags, u32 mask) 217 { 218 struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 219 u32 old_flags = vlan->flags; 220 221 if (mask & ~(VLAN_FLAG_REORDER_HDR | VLAN_FLAG_GVRP | 222 VLAN_FLAG_LOOSE_BINDING | VLAN_FLAG_MVRP | 223 VLAN_FLAG_BRIDGE_BINDING)) 224 return -EINVAL; 225 226 vlan->flags = (old_flags & ~mask) | (flags & mask); 227 228 if (netif_running(dev) && (vlan->flags ^ old_flags) & VLAN_FLAG_GVRP) { 229 if (vlan->flags & VLAN_FLAG_GVRP) 230 vlan_gvrp_request_join(dev); 231 else 232 vlan_gvrp_request_leave(dev); 233 } 234 235 if (netif_running(dev) && (vlan->flags ^ old_flags) & VLAN_FLAG_MVRP) { 236 if (vlan->flags & VLAN_FLAG_MVRP) 237 vlan_mvrp_request_join(dev); 238 else 239 vlan_mvrp_request_leave(dev); 240 } 241 return 0; 242 } 243 244 void vlan_dev_get_realdev_name(const struct net_device *dev, char *result, size_t size) 245 { 246 strscpy_pad(result, vlan_dev_priv(dev)->real_dev->name, size); 247 } 248 249 bool vlan_dev_inherit_address(struct net_device *dev, 250 struct net_device *real_dev) 251 { 252 if (dev->addr_assign_type != NET_ADDR_STOLEN) 253 return false; 254 255 eth_hw_addr_set(dev, real_dev->dev_addr); 256 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 257 return true; 258 } 259 260 static int vlan_dev_open(struct net_device *dev) 261 { 262 struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 263 struct net_device *real_dev = vlan->real_dev; 264 int err; 265 266 if (!(real_dev->flags & IFF_UP) && 267 !(vlan->flags & VLAN_FLAG_LOOSE_BINDING)) 268 return -ENETDOWN; 269 270 if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr) && 271 !vlan_dev_inherit_address(dev, real_dev)) { 272 err = dev_uc_add(real_dev, dev->dev_addr); 273 if (err < 0) 274 goto out; 275 } 276 277 if (dev->flags & IFF_ALLMULTI) { 278 err = dev_set_allmulti(real_dev, 1); 279 if (err < 0) 280 goto del_unicast; 281 } 282 if (dev->flags & IFF_PROMISC) { 283 err = dev_set_promiscuity(real_dev, 1); 284 if (err < 0) 285 goto clear_allmulti; 286 } 287 288 ether_addr_copy(vlan->real_dev_addr, real_dev->dev_addr); 289 290 if (vlan->flags & VLAN_FLAG_GVRP) 291 vlan_gvrp_request_join(dev); 292 293 if (vlan->flags & VLAN_FLAG_MVRP) 294 vlan_mvrp_request_join(dev); 295 296 if (netif_carrier_ok(real_dev) && 297 !(vlan->flags & VLAN_FLAG_BRIDGE_BINDING)) 298 netif_carrier_on(dev); 299 return 0; 300 301 clear_allmulti: 302 if (dev->flags & IFF_ALLMULTI) 303 dev_set_allmulti(real_dev, -1); 304 del_unicast: 305 if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr)) 306 dev_uc_del(real_dev, dev->dev_addr); 307 out: 308 netif_carrier_off(dev); 309 return err; 310 } 311 312 static int vlan_dev_stop(struct net_device *dev) 313 { 314 struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 315 struct net_device *real_dev = vlan->real_dev; 316 317 dev_mc_unsync(real_dev, dev); 318 dev_uc_unsync(real_dev, dev); 319 if (dev->flags & IFF_ALLMULTI) 320 dev_set_allmulti(real_dev, -1); 321 if (dev->flags & IFF_PROMISC) 322 dev_set_promiscuity(real_dev, -1); 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 (dev->flags & IFF_UP) { 493 if (change & IFF_ALLMULTI) 494 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1); 495 if (change & IFF_PROMISC) 496 dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1); 497 } 498 } 499 500 static void vlan_dev_set_rx_mode(struct net_device *vlan_dev) 501 { 502 dev_mc_sync(vlan_dev_priv(vlan_dev)->real_dev, vlan_dev); 503 dev_uc_sync(vlan_dev_priv(vlan_dev)->real_dev, vlan_dev); 504 } 505 506 static __be16 vlan_parse_protocol(const struct sk_buff *skb) 507 { 508 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data); 509 510 return __vlan_get_protocol(skb, veth->h_vlan_proto, NULL); 511 } 512 513 static const struct header_ops vlan_header_ops = { 514 .create = vlan_dev_hard_header, 515 .parse = eth_header_parse, 516 .parse_protocol = vlan_parse_protocol, 517 }; 518 519 static int vlan_passthru_hard_header(struct sk_buff *skb, struct net_device *dev, 520 unsigned short type, 521 const void *daddr, const void *saddr, 522 unsigned int len) 523 { 524 struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 525 struct net_device *real_dev = vlan->real_dev; 526 527 if (saddr == NULL) 528 saddr = dev->dev_addr; 529 530 return dev_hard_header(skb, real_dev, type, daddr, saddr, len); 531 } 532 533 static const struct header_ops vlan_passthru_header_ops = { 534 .create = vlan_passthru_hard_header, 535 .parse = eth_header_parse, 536 .parse_protocol = vlan_parse_protocol, 537 }; 538 539 static const struct device_type vlan_type = { 540 .name = "vlan", 541 }; 542 543 static const struct net_device_ops vlan_netdev_ops; 544 545 static int vlan_dev_init(struct net_device *dev) 546 { 547 struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 548 struct net_device *real_dev = vlan->real_dev; 549 550 netif_carrier_off(dev); 551 552 /* IFF_BROADCAST|IFF_MULTICAST; ??? */ 553 dev->flags = real_dev->flags & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | 554 IFF_MASTER | IFF_SLAVE); 555 dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) | 556 (1<<__LINK_STATE_DORMANT))) | 557 (1<<__LINK_STATE_PRESENT); 558 559 if (vlan->flags & VLAN_FLAG_BRIDGE_BINDING) 560 dev->state |= (1 << __LINK_STATE_NOCARRIER); 561 562 dev->hw_features = NETIF_F_HW_CSUM | NETIF_F_SG | 563 NETIF_F_FRAGLIST | NETIF_F_GSO_SOFTWARE | 564 NETIF_F_GSO_ENCAP_ALL | 565 NETIF_F_HIGHDMA | NETIF_F_SCTP_CRC | 566 NETIF_F_FCOE_CRC | NETIF_F_FSO; 567 568 if (real_dev->vlan_features & NETIF_F_HW_MACSEC) 569 dev->hw_features |= NETIF_F_HW_MACSEC; 570 571 dev->features |= dev->hw_features; 572 dev->lltx = true; 573 dev->fcoe_mtu = true; 574 netif_inherit_tso_max(dev, real_dev); 575 if (dev->features & NETIF_F_VLAN_FEATURES) 576 netdev_warn(real_dev, "VLAN features are set incorrectly. Q-in-Q configurations may not work correctly.\n"); 577 578 dev->vlan_features = real_dev->vlan_features & 579 ~(NETIF_F_FCOE_CRC | NETIF_F_FSO); 580 dev->hw_enc_features = vlan_tnl_features(real_dev); 581 dev->mpls_features = real_dev->mpls_features; 582 583 /* ipv6 shared card related stuff */ 584 dev->dev_id = real_dev->dev_id; 585 586 if (is_zero_ether_addr(dev->dev_addr)) { 587 eth_hw_addr_set(dev, real_dev->dev_addr); 588 dev->addr_assign_type = NET_ADDR_STOLEN; 589 } 590 if (is_zero_ether_addr(dev->broadcast)) 591 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len); 592 593 #if IS_ENABLED(CONFIG_FCOE) 594 dev->fcoe_ddp_xid = real_dev->fcoe_ddp_xid; 595 #endif 596 597 dev->needed_headroom = real_dev->needed_headroom; 598 if (vlan_hw_offload_capable(real_dev->features, vlan->vlan_proto)) { 599 dev->header_ops = &vlan_passthru_header_ops; 600 dev->hard_header_len = real_dev->hard_header_len; 601 } else { 602 dev->header_ops = &vlan_header_ops; 603 dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN; 604 } 605 606 dev->netdev_ops = &vlan_netdev_ops; 607 608 SET_NETDEV_DEVTYPE(dev, &vlan_type); 609 610 netdev_lockdep_set_classes(dev); 611 612 vlan->vlan_pcpu_stats = netdev_alloc_pcpu_stats(struct vlan_pcpu_stats); 613 if (!vlan->vlan_pcpu_stats) 614 return -ENOMEM; 615 616 /* Get vlan's reference to real_dev */ 617 netdev_hold(real_dev, &vlan->dev_tracker, GFP_KERNEL); 618 619 return 0; 620 } 621 622 /* Note: this function might be called multiple times for the same device. */ 623 void vlan_dev_free_egress_priority(const struct net_device *dev) 624 { 625 struct vlan_priority_tci_mapping *pm; 626 struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 627 int i; 628 629 for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) { 630 while ((pm = vlan->egress_priority_map[i]) != NULL) { 631 vlan->egress_priority_map[i] = pm->next; 632 kfree(pm); 633 } 634 } 635 } 636 637 static void vlan_dev_uninit(struct net_device *dev) 638 { 639 vlan_dev_free_egress_priority(dev); 640 } 641 642 static netdev_features_t vlan_dev_fix_features(struct net_device *dev, 643 netdev_features_t features) 644 { 645 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 646 netdev_features_t old_features = features; 647 netdev_features_t lower_features; 648 649 lower_features = netdev_intersect_features((real_dev->vlan_features | 650 NETIF_F_RXCSUM), 651 real_dev->features); 652 653 /* Add HW_CSUM setting to preserve user ability to control 654 * checksum offload on the vlan device. 655 */ 656 if (lower_features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM)) 657 lower_features |= NETIF_F_HW_CSUM; 658 features = netdev_intersect_features(features, lower_features); 659 features |= old_features & (NETIF_F_SOFT_FEATURES | NETIF_F_GSO_SOFTWARE); 660 661 return features; 662 } 663 664 static int vlan_ethtool_get_link_ksettings(struct net_device *dev, 665 struct ethtool_link_ksettings *cmd) 666 { 667 const struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 668 669 return __ethtool_get_link_ksettings(vlan->real_dev, cmd); 670 } 671 672 static void vlan_ethtool_get_drvinfo(struct net_device *dev, 673 struct ethtool_drvinfo *info) 674 { 675 strscpy(info->driver, vlan_fullname, sizeof(info->driver)); 676 strscpy(info->version, vlan_version, sizeof(info->version)); 677 strscpy(info->fw_version, "N/A", sizeof(info->fw_version)); 678 } 679 680 static int vlan_ethtool_get_ts_info(struct net_device *dev, 681 struct kernel_ethtool_ts_info *info) 682 { 683 const struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 684 return ethtool_get_ts_info_by_layer(vlan->real_dev, info); 685 } 686 687 static void vlan_dev_get_stats64(struct net_device *dev, 688 struct rtnl_link_stats64 *stats) 689 { 690 struct vlan_pcpu_stats *p; 691 u32 rx_errors = 0, tx_dropped = 0; 692 int i; 693 694 for_each_possible_cpu(i) { 695 u64 rxpackets, rxbytes, rxmulticast, txpackets, txbytes; 696 unsigned int start; 697 698 p = per_cpu_ptr(vlan_dev_priv(dev)->vlan_pcpu_stats, i); 699 do { 700 start = u64_stats_fetch_begin(&p->syncp); 701 rxpackets = u64_stats_read(&p->rx_packets); 702 rxbytes = u64_stats_read(&p->rx_bytes); 703 rxmulticast = u64_stats_read(&p->rx_multicast); 704 txpackets = u64_stats_read(&p->tx_packets); 705 txbytes = u64_stats_read(&p->tx_bytes); 706 } while (u64_stats_fetch_retry(&p->syncp, start)); 707 708 stats->rx_packets += rxpackets; 709 stats->rx_bytes += rxbytes; 710 stats->multicast += rxmulticast; 711 stats->tx_packets += txpackets; 712 stats->tx_bytes += txbytes; 713 /* rx_errors & tx_dropped are u32 */ 714 rx_errors += READ_ONCE(p->rx_errors); 715 tx_dropped += READ_ONCE(p->tx_dropped); 716 } 717 stats->rx_errors = rx_errors; 718 stats->tx_dropped = tx_dropped; 719 } 720 721 #ifdef CONFIG_NET_POLL_CONTROLLER 722 static void vlan_dev_poll_controller(struct net_device *dev) 723 { 724 return; 725 } 726 727 static int vlan_dev_netpoll_setup(struct net_device *dev) 728 { 729 struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 730 struct net_device *real_dev = vlan->real_dev; 731 struct netpoll *netpoll; 732 int err = 0; 733 734 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL); 735 err = -ENOMEM; 736 if (!netpoll) 737 goto out; 738 739 err = __netpoll_setup(netpoll, real_dev); 740 if (err) { 741 kfree(netpoll); 742 goto out; 743 } 744 745 vlan->netpoll = netpoll; 746 747 out: 748 return err; 749 } 750 751 static void vlan_dev_netpoll_cleanup(struct net_device *dev) 752 { 753 struct vlan_dev_priv *vlan= vlan_dev_priv(dev); 754 struct netpoll *netpoll = vlan->netpoll; 755 756 if (!netpoll) 757 return; 758 759 vlan->netpoll = NULL; 760 __netpoll_free(netpoll); 761 } 762 #endif /* CONFIG_NET_POLL_CONTROLLER */ 763 764 static int vlan_dev_get_iflink(const struct net_device *dev) 765 { 766 const struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 767 768 return READ_ONCE(real_dev->ifindex); 769 } 770 771 static int vlan_dev_fill_forward_path(struct net_device_path_ctx *ctx, 772 struct net_device_path *path) 773 { 774 struct vlan_dev_priv *vlan = vlan_dev_priv(ctx->dev); 775 776 path->type = DEV_PATH_VLAN; 777 path->encap.id = vlan->vlan_id; 778 path->encap.proto = vlan->vlan_proto; 779 path->dev = ctx->dev; 780 ctx->dev = vlan->real_dev; 781 if (ctx->num_vlans >= ARRAY_SIZE(ctx->vlan)) 782 return -ENOSPC; 783 784 ctx->vlan[ctx->num_vlans].id = vlan->vlan_id; 785 ctx->vlan[ctx->num_vlans].proto = vlan->vlan_proto; 786 ctx->num_vlans++; 787 788 return 0; 789 } 790 791 #if IS_ENABLED(CONFIG_MACSEC) 792 793 static const struct macsec_ops *vlan_get_macsec_ops(const struct macsec_context *ctx) 794 { 795 return vlan_dev_priv(ctx->netdev)->real_dev->macsec_ops; 796 } 797 798 static int vlan_macsec_offload(int (* const func)(struct macsec_context *), 799 struct macsec_context *ctx) 800 { 801 if (unlikely(!func)) 802 return 0; 803 804 return (*func)(ctx); 805 } 806 807 static int vlan_macsec_dev_open(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_dev_open, ctx); 815 } 816 817 static int vlan_macsec_dev_stop(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_dev_stop, ctx); 825 } 826 827 static int vlan_macsec_add_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_add_secy, ctx); 835 } 836 837 static int vlan_macsec_upd_secy(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_upd_secy, ctx); 845 } 846 847 static int vlan_macsec_del_secy(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_del_secy, ctx); 855 } 856 857 static int vlan_macsec_add_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_add_rxsc, ctx); 865 } 866 867 static int vlan_macsec_upd_rxsc(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_upd_rxsc, ctx); 875 } 876 877 static int vlan_macsec_del_rxsc(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_del_rxsc, ctx); 885 } 886 887 static int vlan_macsec_add_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_add_rxsa, ctx); 895 } 896 897 static int vlan_macsec_upd_rxsa(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_upd_rxsa, ctx); 905 } 906 907 static int vlan_macsec_del_rxsa(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_del_rxsa, ctx); 915 } 916 917 static int vlan_macsec_add_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_add_txsa, ctx); 925 } 926 927 static int vlan_macsec_upd_txsa(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_upd_txsa, ctx); 935 } 936 937 static int vlan_macsec_del_txsa(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_del_txsa, ctx); 945 } 946 947 static int vlan_macsec_get_dev_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_dev_stats, ctx); 955 } 956 957 static int vlan_macsec_get_tx_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_tx_sc_stats, ctx); 965 } 966 967 static int vlan_macsec_get_tx_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_tx_sa_stats, ctx); 975 } 976 977 static int vlan_macsec_get_rx_sc_stats(struct macsec_context *ctx) 978 { 979 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 980 981 if (!ops) 982 return -EOPNOTSUPP; 983 984 return vlan_macsec_offload(ops->mdo_get_rx_sc_stats, ctx); 985 } 986 987 static int vlan_macsec_get_rx_sa_stats(struct macsec_context *ctx) 988 { 989 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 990 991 if (!ops) 992 return -EOPNOTSUPP; 993 994 return vlan_macsec_offload(ops->mdo_get_rx_sa_stats, ctx); 995 } 996 997 static const struct macsec_ops macsec_offload_ops = { 998 /* Device wide */ 999 .mdo_dev_open = vlan_macsec_dev_open, 1000 .mdo_dev_stop = vlan_macsec_dev_stop, 1001 /* SecY */ 1002 .mdo_add_secy = vlan_macsec_add_secy, 1003 .mdo_upd_secy = vlan_macsec_upd_secy, 1004 .mdo_del_secy = vlan_macsec_del_secy, 1005 /* Security channels */ 1006 .mdo_add_rxsc = vlan_macsec_add_rxsc, 1007 .mdo_upd_rxsc = vlan_macsec_upd_rxsc, 1008 .mdo_del_rxsc = vlan_macsec_del_rxsc, 1009 /* Security associations */ 1010 .mdo_add_rxsa = vlan_macsec_add_rxsa, 1011 .mdo_upd_rxsa = vlan_macsec_upd_rxsa, 1012 .mdo_del_rxsa = vlan_macsec_del_rxsa, 1013 .mdo_add_txsa = vlan_macsec_add_txsa, 1014 .mdo_upd_txsa = vlan_macsec_upd_txsa, 1015 .mdo_del_txsa = vlan_macsec_del_txsa, 1016 /* Statistics */ 1017 .mdo_get_dev_stats = vlan_macsec_get_dev_stats, 1018 .mdo_get_tx_sc_stats = vlan_macsec_get_tx_sc_stats, 1019 .mdo_get_tx_sa_stats = vlan_macsec_get_tx_sa_stats, 1020 .mdo_get_rx_sc_stats = vlan_macsec_get_rx_sc_stats, 1021 .mdo_get_rx_sa_stats = vlan_macsec_get_rx_sa_stats, 1022 }; 1023 1024 #endif 1025 1026 static const struct ethtool_ops vlan_ethtool_ops = { 1027 .get_link_ksettings = vlan_ethtool_get_link_ksettings, 1028 .get_drvinfo = vlan_ethtool_get_drvinfo, 1029 .get_link = ethtool_op_get_link, 1030 .get_ts_info = vlan_ethtool_get_ts_info, 1031 }; 1032 1033 static const struct net_device_ops vlan_netdev_ops = { 1034 .ndo_change_mtu = vlan_dev_change_mtu, 1035 .ndo_init = vlan_dev_init, 1036 .ndo_uninit = vlan_dev_uninit, 1037 .ndo_open = vlan_dev_open, 1038 .ndo_stop = vlan_dev_stop, 1039 .ndo_start_xmit = vlan_dev_hard_start_xmit, 1040 .ndo_validate_addr = eth_validate_addr, 1041 .ndo_set_mac_address = vlan_dev_set_mac_address, 1042 .ndo_set_rx_mode = vlan_dev_set_rx_mode, 1043 .ndo_change_rx_flags = vlan_dev_change_rx_flags, 1044 .ndo_eth_ioctl = vlan_dev_ioctl, 1045 .ndo_neigh_setup = vlan_dev_neigh_setup, 1046 .ndo_get_stats64 = vlan_dev_get_stats64, 1047 #if IS_ENABLED(CONFIG_FCOE) 1048 .ndo_fcoe_ddp_setup = vlan_dev_fcoe_ddp_setup, 1049 .ndo_fcoe_ddp_done = vlan_dev_fcoe_ddp_done, 1050 .ndo_fcoe_enable = vlan_dev_fcoe_enable, 1051 .ndo_fcoe_disable = vlan_dev_fcoe_disable, 1052 .ndo_fcoe_ddp_target = vlan_dev_fcoe_ddp_target, 1053 #endif 1054 #ifdef NETDEV_FCOE_WWNN 1055 .ndo_fcoe_get_wwn = vlan_dev_fcoe_get_wwn, 1056 #endif 1057 #ifdef CONFIG_NET_POLL_CONTROLLER 1058 .ndo_poll_controller = vlan_dev_poll_controller, 1059 .ndo_netpoll_setup = vlan_dev_netpoll_setup, 1060 .ndo_netpoll_cleanup = vlan_dev_netpoll_cleanup, 1061 #endif 1062 .ndo_fix_features = vlan_dev_fix_features, 1063 .ndo_get_iflink = vlan_dev_get_iflink, 1064 .ndo_fill_forward_path = vlan_dev_fill_forward_path, 1065 .ndo_hwtstamp_get = vlan_hwtstamp_get, 1066 .ndo_hwtstamp_set = vlan_hwtstamp_set, 1067 }; 1068 1069 static void vlan_dev_free(struct net_device *dev) 1070 { 1071 struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 1072 1073 free_percpu(vlan->vlan_pcpu_stats); 1074 vlan->vlan_pcpu_stats = NULL; 1075 1076 /* Get rid of the vlan's reference to real_dev */ 1077 netdev_put(vlan->real_dev, &vlan->dev_tracker); 1078 } 1079 1080 void vlan_setup(struct net_device *dev) 1081 { 1082 ether_setup(dev); 1083 1084 dev->priv_flags |= IFF_802_1Q_VLAN | IFF_NO_QUEUE; 1085 dev->priv_flags |= IFF_UNICAST_FLT; 1086 dev->priv_flags &= ~IFF_TX_SKB_SHARING; 1087 netif_keep_dst(dev); 1088 1089 dev->netdev_ops = &vlan_netdev_ops; 1090 dev->needs_free_netdev = true; 1091 dev->priv_destructor = vlan_dev_free; 1092 dev->ethtool_ops = &vlan_ethtool_ops; 1093 1094 #if IS_ENABLED(CONFIG_MACSEC) 1095 dev->macsec_ops = &macsec_offload_ops; 1096 #endif 1097 dev->min_mtu = 0; 1098 dev->max_mtu = ETH_MAX_MTU; 1099 1100 eth_zero_addr(dev->broadcast); 1101 } 1102