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 /* The explicit open supersedes any deferred link-state sync */ 274 netdev_work_cancel(dev, VLAN_WORK_LINK_STATE); 275 276 if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr) && 277 !vlan_dev_inherit_address(dev, real_dev)) { 278 err = dev_uc_add(real_dev, dev->dev_addr); 279 if (err < 0) 280 goto out; 281 } 282 283 ether_addr_copy(vlan->real_dev_addr, real_dev->dev_addr); 284 285 if (vlan->flags & VLAN_FLAG_GVRP) 286 vlan_gvrp_request_join(dev); 287 288 if (vlan->flags & VLAN_FLAG_MVRP) 289 vlan_mvrp_request_join(dev); 290 291 if (netif_carrier_ok(real_dev) && 292 !(vlan->flags & VLAN_FLAG_BRIDGE_BINDING)) 293 netif_carrier_on(dev); 294 return 0; 295 296 out: 297 netif_carrier_off(dev); 298 return err; 299 } 300 301 static int vlan_dev_stop(struct net_device *dev) 302 { 303 struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 304 struct net_device *real_dev = vlan->real_dev; 305 306 /* The explicit close supersedes any deferred link-state sync */ 307 netdev_work_cancel(dev, VLAN_WORK_LINK_STATE); 308 309 dev_mc_unsync(real_dev, dev); 310 dev_uc_unsync(real_dev, dev); 311 312 if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr)) 313 dev_uc_del(real_dev, dev->dev_addr); 314 315 if (!(vlan->flags & VLAN_FLAG_BRIDGE_BINDING)) 316 netif_carrier_off(dev); 317 return 0; 318 } 319 320 static int vlan_dev_set_mac_address(struct net_device *dev, void *p) 321 { 322 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 323 struct sockaddr *addr = p; 324 int err; 325 326 if (!is_valid_ether_addr(addr->sa_data)) 327 return -EADDRNOTAVAIL; 328 329 if (!(dev->flags & IFF_UP)) 330 goto out; 331 332 if (!ether_addr_equal(addr->sa_data, real_dev->dev_addr)) { 333 err = dev_uc_add(real_dev, addr->sa_data); 334 if (err < 0) 335 return err; 336 } 337 338 if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr)) 339 dev_uc_del(real_dev, dev->dev_addr); 340 341 out: 342 eth_hw_addr_set(dev, addr->sa_data); 343 return 0; 344 } 345 346 static int vlan_hwtstamp_get(struct net_device *dev, 347 struct kernel_hwtstamp_config *cfg) 348 { 349 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 350 351 return generic_hwtstamp_get_lower(real_dev, cfg); 352 } 353 354 static int vlan_hwtstamp_set(struct net_device *dev, 355 struct kernel_hwtstamp_config *cfg, 356 struct netlink_ext_ack *extack) 357 { 358 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 359 360 if (!net_eq(dev_net(dev), dev_net(real_dev))) 361 return -EOPNOTSUPP; 362 363 return generic_hwtstamp_set_lower(real_dev, cfg, extack); 364 } 365 366 static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 367 { 368 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 369 struct ifreq ifrr; 370 int err = -EOPNOTSUPP; 371 372 strscpy_pad(ifrr.ifr_name, real_dev->name, IFNAMSIZ); 373 ifrr.ifr_ifru = ifr->ifr_ifru; 374 375 switch (cmd) { 376 case SIOCGMIIPHY: 377 case SIOCGMIIREG: 378 case SIOCSMIIREG: 379 err = dev_eth_ioctl(real_dev, &ifrr, cmd); 380 break; 381 } 382 383 if (!err) 384 ifr->ifr_ifru = ifrr.ifr_ifru; 385 386 return err; 387 } 388 389 static int vlan_dev_neigh_setup(struct net_device *dev, struct neigh_parms *pa) 390 { 391 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 392 const struct net_device_ops *ops = real_dev->netdev_ops; 393 int err = 0; 394 395 if (netif_device_present(real_dev) && ops->ndo_neigh_setup) 396 err = ops->ndo_neigh_setup(real_dev, pa); 397 398 return err; 399 } 400 401 #if IS_ENABLED(CONFIG_FCOE) 402 static int vlan_dev_fcoe_ddp_setup(struct net_device *dev, u16 xid, 403 struct scatterlist *sgl, unsigned int sgc) 404 { 405 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 406 const struct net_device_ops *ops = real_dev->netdev_ops; 407 int rc = 0; 408 409 if (ops->ndo_fcoe_ddp_setup) 410 rc = ops->ndo_fcoe_ddp_setup(real_dev, xid, sgl, sgc); 411 412 return rc; 413 } 414 415 static int vlan_dev_fcoe_ddp_done(struct net_device *dev, u16 xid) 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 len = 0; 420 421 if (ops->ndo_fcoe_ddp_done) 422 len = ops->ndo_fcoe_ddp_done(real_dev, xid); 423 424 return len; 425 } 426 427 static int vlan_dev_fcoe_enable(struct net_device *dev) 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 rc = -EINVAL; 432 433 if (ops->ndo_fcoe_enable) 434 rc = ops->ndo_fcoe_enable(real_dev); 435 return rc; 436 } 437 438 static int vlan_dev_fcoe_disable(struct net_device *dev) 439 { 440 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 441 const struct net_device_ops *ops = real_dev->netdev_ops; 442 int rc = -EINVAL; 443 444 if (ops->ndo_fcoe_disable) 445 rc = ops->ndo_fcoe_disable(real_dev); 446 return rc; 447 } 448 449 static int vlan_dev_fcoe_ddp_target(struct net_device *dev, u16 xid, 450 struct scatterlist *sgl, unsigned int sgc) 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 = 0; 455 456 if (ops->ndo_fcoe_ddp_target) 457 rc = ops->ndo_fcoe_ddp_target(real_dev, xid, sgl, sgc); 458 459 return rc; 460 } 461 #endif 462 463 #ifdef NETDEV_FCOE_WWNN 464 static int vlan_dev_fcoe_get_wwn(struct net_device *dev, u64 *wwn, int type) 465 { 466 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 467 const struct net_device_ops *ops = real_dev->netdev_ops; 468 int rc = -EINVAL; 469 470 if (ops->ndo_fcoe_get_wwn) 471 rc = ops->ndo_fcoe_get_wwn(real_dev, wwn, type); 472 return rc; 473 } 474 #endif 475 476 static void vlan_dev_change_rx_flags(struct net_device *dev, int change) 477 { 478 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 479 480 if (change & IFF_ALLMULTI) 481 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1); 482 if (change & IFF_PROMISC) 483 dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1); 484 } 485 486 static void vlan_dev_set_rx_mode(struct net_device *vlan_dev) 487 { 488 dev_mc_sync(vlan_dev_priv(vlan_dev)->real_dev, vlan_dev); 489 dev_uc_sync(vlan_dev_priv(vlan_dev)->real_dev, vlan_dev); 490 } 491 492 static __be16 vlan_parse_protocol(const struct sk_buff *skb) 493 { 494 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data); 495 496 return __vlan_get_protocol(skb, veth->h_vlan_proto, NULL); 497 } 498 499 static const struct header_ops vlan_header_ops = { 500 .create = vlan_dev_hard_header, 501 .parse = eth_header_parse, 502 .parse_protocol = vlan_parse_protocol, 503 }; 504 505 static const struct device_type vlan_type = { 506 .name = "vlan", 507 }; 508 509 static const struct net_device_ops vlan_netdev_ops; 510 511 static int vlan_dev_init(struct net_device *dev) 512 { 513 struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 514 struct net_device *real_dev = vlan->real_dev; 515 516 netif_carrier_off(dev); 517 518 /* IFF_BROADCAST|IFF_MULTICAST; ??? */ 519 dev->flags = real_dev->flags & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | 520 IFF_MASTER | IFF_SLAVE); 521 dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) | 522 (1<<__LINK_STATE_DORMANT))) | 523 (1<<__LINK_STATE_PRESENT); 524 525 if (vlan->flags & VLAN_FLAG_BRIDGE_BINDING) 526 dev->state |= (1 << __LINK_STATE_NOCARRIER); 527 528 dev->hw_features = NETIF_F_HW_CSUM | NETIF_F_SG | 529 NETIF_F_FRAGLIST | NETIF_F_GSO_SOFTWARE | 530 NETIF_F_GSO_ENCAP_ALL | 531 NETIF_F_HIGHDMA | NETIF_F_SCTP_CRC | 532 NETIF_F_FCOE_CRC | NETIF_F_FSO; 533 534 if (real_dev->vlan_features & NETIF_F_HW_MACSEC) 535 dev->hw_features |= NETIF_F_HW_MACSEC; 536 537 dev->features |= dev->hw_features; 538 dev->lltx = true; 539 dev->fcoe_mtu = true; 540 netif_inherit_tso_max(dev, real_dev); 541 if (dev->features & NETIF_F_VLAN_FEATURES) 542 netdev_warn(real_dev, "VLAN features are set incorrectly. Q-in-Q configurations may not work correctly.\n"); 543 544 dev->vlan_features = real_dev->vlan_features & 545 ~(NETIF_F_FCOE_CRC | NETIF_F_FSO); 546 dev->hw_enc_features = vlan_tnl_features(real_dev); 547 dev->mpls_features = real_dev->mpls_features; 548 549 /* ipv6 shared card related stuff */ 550 dev->dev_id = real_dev->dev_id; 551 552 if (is_zero_ether_addr(dev->dev_addr)) { 553 eth_hw_addr_set(dev, real_dev->dev_addr); 554 dev->addr_assign_type = NET_ADDR_STOLEN; 555 } 556 if (is_zero_ether_addr(dev->broadcast)) 557 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len); 558 559 #if IS_ENABLED(CONFIG_FCOE) 560 dev->fcoe_ddp_xid = real_dev->fcoe_ddp_xid; 561 #endif 562 563 dev->needed_headroom = real_dev->needed_headroom + VLAN_HLEN; 564 dev->needed_tailroom = real_dev->needed_tailroom; 565 dev->header_ops = &vlan_header_ops; 566 dev->hard_header_len = real_dev->hard_header_len; 567 568 dev->netdev_ops = &vlan_netdev_ops; 569 570 SET_NETDEV_DEVTYPE(dev, &vlan_type); 571 572 netdev_lockdep_set_classes(dev); 573 574 vlan->vlan_pcpu_stats = netdev_alloc_pcpu_stats(struct vlan_pcpu_stats); 575 if (!vlan->vlan_pcpu_stats) 576 return -ENOMEM; 577 578 /* Get vlan's reference to real_dev */ 579 netdev_hold(real_dev, &vlan->dev_tracker, GFP_KERNEL); 580 581 return 0; 582 } 583 584 /* Note: this function might be called multiple times for the same device. */ 585 void vlan_dev_free_egress_priority(const struct net_device *dev) 586 { 587 struct vlan_priority_tci_mapping *pm; 588 struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 589 int i; 590 591 for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) { 592 pm = rtnl_dereference(vlan->egress_priority_map[i]); 593 RCU_INIT_POINTER(vlan->egress_priority_map[i], NULL); 594 while (pm) { 595 struct vlan_priority_tci_mapping *next; 596 597 next = rtnl_dereference(pm->next); 598 kfree_rcu(pm, rcu); 599 pm = next; 600 } 601 } 602 vlan->nr_egress_mappings = 0; 603 } 604 605 static void vlan_dev_uninit(struct net_device *dev) 606 { 607 vlan_dev_free_egress_priority(dev); 608 } 609 610 static netdev_features_t vlan_dev_fix_features(struct net_device *dev, 611 netdev_features_t features) 612 { 613 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 614 netdev_features_t old_features = features; 615 netdev_features_t lower_features; 616 617 lower_features = netdev_intersect_features((real_dev->vlan_features | 618 NETIF_F_RXCSUM), 619 real_dev->features); 620 621 /* Add HW_CSUM setting to preserve user ability to control 622 * checksum offload on the vlan device. 623 */ 624 if (lower_features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM)) 625 lower_features |= NETIF_F_HW_CSUM; 626 features = netdev_intersect_features(features, lower_features); 627 features |= old_features & (NETIF_F_SOFT_FEATURES | NETIF_F_GSO_SOFTWARE); 628 629 return features; 630 } 631 632 static int vlan_ethtool_get_link_ksettings(struct net_device *dev, 633 struct ethtool_link_ksettings *cmd) 634 { 635 const struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 636 637 return __ethtool_get_link_ksettings(vlan->real_dev, cmd); 638 } 639 640 static void vlan_ethtool_get_drvinfo(struct net_device *dev, 641 struct ethtool_drvinfo *info) 642 { 643 strscpy(info->driver, vlan_fullname, sizeof(info->driver)); 644 strscpy(info->version, vlan_version, sizeof(info->version)); 645 strscpy(info->fw_version, "N/A", sizeof(info->fw_version)); 646 } 647 648 static int vlan_ethtool_get_ts_info(struct net_device *dev, 649 struct kernel_ethtool_ts_info *info) 650 { 651 const struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 652 return ethtool_get_ts_info_by_layer(vlan->real_dev, info); 653 } 654 655 static void vlan_dev_get_stats64(struct net_device *dev, 656 struct rtnl_link_stats64 *stats) 657 { 658 struct vlan_pcpu_stats *p; 659 u32 rx_errors = 0, tx_dropped = 0; 660 int i; 661 662 for_each_possible_cpu(i) { 663 u64 rxpackets, rxbytes, rxmulticast, txpackets, txbytes; 664 unsigned int start; 665 666 p = per_cpu_ptr(vlan_dev_priv(dev)->vlan_pcpu_stats, i); 667 do { 668 start = u64_stats_fetch_begin(&p->syncp); 669 rxpackets = u64_stats_read(&p->rx_packets); 670 rxbytes = u64_stats_read(&p->rx_bytes); 671 rxmulticast = u64_stats_read(&p->rx_multicast); 672 txpackets = u64_stats_read(&p->tx_packets); 673 txbytes = u64_stats_read(&p->tx_bytes); 674 } while (u64_stats_fetch_retry(&p->syncp, start)); 675 676 stats->rx_packets += rxpackets; 677 stats->rx_bytes += rxbytes; 678 stats->multicast += rxmulticast; 679 stats->tx_packets += txpackets; 680 stats->tx_bytes += txbytes; 681 /* rx_errors & tx_dropped are u32 */ 682 rx_errors += READ_ONCE(p->rx_errors); 683 tx_dropped += READ_ONCE(p->tx_dropped); 684 } 685 stats->rx_errors = rx_errors; 686 stats->tx_dropped = tx_dropped; 687 } 688 689 #ifdef CONFIG_NET_POLL_CONTROLLER 690 static void vlan_dev_poll_controller(struct net_device *dev) 691 { 692 return; 693 } 694 695 static int vlan_dev_netpoll_setup(struct net_device *dev) 696 { 697 struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 698 struct net_device *real_dev = vlan->real_dev; 699 struct netpoll *netpoll; 700 int err = 0; 701 702 netpoll = kzalloc_obj(*netpoll); 703 err = -ENOMEM; 704 if (!netpoll) 705 goto out; 706 707 err = __netpoll_setup(netpoll, real_dev); 708 if (err) { 709 kfree(netpoll); 710 goto out; 711 } 712 713 vlan->netpoll = netpoll; 714 715 out: 716 return err; 717 } 718 719 static void vlan_dev_netpoll_cleanup(struct net_device *dev) 720 { 721 struct vlan_dev_priv *vlan= vlan_dev_priv(dev); 722 struct netpoll *netpoll = vlan->netpoll; 723 724 if (!netpoll) 725 return; 726 727 vlan->netpoll = NULL; 728 __netpoll_free(netpoll); 729 } 730 #endif /* CONFIG_NET_POLL_CONTROLLER */ 731 732 static int vlan_dev_get_iflink(const struct net_device *dev) 733 { 734 const struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 735 736 return READ_ONCE(real_dev->ifindex); 737 } 738 739 static int vlan_dev_fill_forward_path(struct net_device_path_ctx *ctx, 740 struct net_device_path *path) 741 { 742 struct vlan_dev_priv *vlan = vlan_dev_priv(ctx->dev); 743 744 path->type = DEV_PATH_VLAN; 745 path->encap.id = vlan->vlan_id; 746 path->encap.proto = vlan->vlan_proto; 747 path->dev = ctx->dev; 748 ctx->dev = vlan->real_dev; 749 if (ctx->num_vlans >= ARRAY_SIZE(ctx->vlan)) 750 return -ENOSPC; 751 752 ctx->vlan[ctx->num_vlans].id = vlan->vlan_id; 753 ctx->vlan[ctx->num_vlans].proto = vlan->vlan_proto; 754 ctx->num_vlans++; 755 756 return 0; 757 } 758 759 #if IS_ENABLED(CONFIG_MACSEC) 760 761 static const struct macsec_ops *vlan_get_macsec_ops(const struct macsec_context *ctx) 762 { 763 return vlan_dev_priv(ctx->netdev)->real_dev->macsec_ops; 764 } 765 766 static int vlan_macsec_offload(int (* const func)(struct macsec_context *), 767 struct macsec_context *ctx) 768 { 769 if (unlikely(!func)) 770 return 0; 771 772 return (*func)(ctx); 773 } 774 775 static int vlan_macsec_dev_open(struct macsec_context *ctx) 776 { 777 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 778 779 if (!ops) 780 return -EOPNOTSUPP; 781 782 return vlan_macsec_offload(ops->mdo_dev_open, ctx); 783 } 784 785 static int vlan_macsec_dev_stop(struct macsec_context *ctx) 786 { 787 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 788 789 if (!ops) 790 return -EOPNOTSUPP; 791 792 return vlan_macsec_offload(ops->mdo_dev_stop, ctx); 793 } 794 795 static int vlan_macsec_add_secy(struct macsec_context *ctx) 796 { 797 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 798 799 if (!ops) 800 return -EOPNOTSUPP; 801 802 return vlan_macsec_offload(ops->mdo_add_secy, ctx); 803 } 804 805 static int vlan_macsec_upd_secy(struct macsec_context *ctx) 806 { 807 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 808 809 if (!ops) 810 return -EOPNOTSUPP; 811 812 return vlan_macsec_offload(ops->mdo_upd_secy, ctx); 813 } 814 815 static int vlan_macsec_del_secy(struct macsec_context *ctx) 816 { 817 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 818 819 if (!ops) 820 return -EOPNOTSUPP; 821 822 return vlan_macsec_offload(ops->mdo_del_secy, ctx); 823 } 824 825 static int vlan_macsec_add_rxsc(struct macsec_context *ctx) 826 { 827 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 828 829 if (!ops) 830 return -EOPNOTSUPP; 831 832 return vlan_macsec_offload(ops->mdo_add_rxsc, ctx); 833 } 834 835 static int vlan_macsec_upd_rxsc(struct macsec_context *ctx) 836 { 837 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 838 839 if (!ops) 840 return -EOPNOTSUPP; 841 842 return vlan_macsec_offload(ops->mdo_upd_rxsc, ctx); 843 } 844 845 static int vlan_macsec_del_rxsc(struct macsec_context *ctx) 846 { 847 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 848 849 if (!ops) 850 return -EOPNOTSUPP; 851 852 return vlan_macsec_offload(ops->mdo_del_rxsc, ctx); 853 } 854 855 static int vlan_macsec_add_rxsa(struct macsec_context *ctx) 856 { 857 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 858 859 if (!ops) 860 return -EOPNOTSUPP; 861 862 return vlan_macsec_offload(ops->mdo_add_rxsa, ctx); 863 } 864 865 static int vlan_macsec_upd_rxsa(struct macsec_context *ctx) 866 { 867 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 868 869 if (!ops) 870 return -EOPNOTSUPP; 871 872 return vlan_macsec_offload(ops->mdo_upd_rxsa, ctx); 873 } 874 875 static int vlan_macsec_del_rxsa(struct macsec_context *ctx) 876 { 877 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 878 879 if (!ops) 880 return -EOPNOTSUPP; 881 882 return vlan_macsec_offload(ops->mdo_del_rxsa, ctx); 883 } 884 885 static int vlan_macsec_add_txsa(struct macsec_context *ctx) 886 { 887 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 888 889 if (!ops) 890 return -EOPNOTSUPP; 891 892 return vlan_macsec_offload(ops->mdo_add_txsa, ctx); 893 } 894 895 static int vlan_macsec_upd_txsa(struct macsec_context *ctx) 896 { 897 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 898 899 if (!ops) 900 return -EOPNOTSUPP; 901 902 return vlan_macsec_offload(ops->mdo_upd_txsa, ctx); 903 } 904 905 static int vlan_macsec_del_txsa(struct macsec_context *ctx) 906 { 907 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 908 909 if (!ops) 910 return -EOPNOTSUPP; 911 912 return vlan_macsec_offload(ops->mdo_del_txsa, ctx); 913 } 914 915 static int vlan_macsec_get_dev_stats(struct macsec_context *ctx) 916 { 917 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 918 919 if (!ops) 920 return -EOPNOTSUPP; 921 922 return vlan_macsec_offload(ops->mdo_get_dev_stats, ctx); 923 } 924 925 static int vlan_macsec_get_tx_sc_stats(struct macsec_context *ctx) 926 { 927 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 928 929 if (!ops) 930 return -EOPNOTSUPP; 931 932 return vlan_macsec_offload(ops->mdo_get_tx_sc_stats, ctx); 933 } 934 935 static int vlan_macsec_get_tx_sa_stats(struct macsec_context *ctx) 936 { 937 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 938 939 if (!ops) 940 return -EOPNOTSUPP; 941 942 return vlan_macsec_offload(ops->mdo_get_tx_sa_stats, ctx); 943 } 944 945 static int vlan_macsec_get_rx_sc_stats(struct macsec_context *ctx) 946 { 947 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 948 949 if (!ops) 950 return -EOPNOTSUPP; 951 952 return vlan_macsec_offload(ops->mdo_get_rx_sc_stats, ctx); 953 } 954 955 static int vlan_macsec_get_rx_sa_stats(struct macsec_context *ctx) 956 { 957 const struct macsec_ops *ops = vlan_get_macsec_ops(ctx); 958 959 if (!ops) 960 return -EOPNOTSUPP; 961 962 return vlan_macsec_offload(ops->mdo_get_rx_sa_stats, ctx); 963 } 964 965 static const struct macsec_ops macsec_offload_ops = { 966 /* Device wide */ 967 .mdo_dev_open = vlan_macsec_dev_open, 968 .mdo_dev_stop = vlan_macsec_dev_stop, 969 /* SecY */ 970 .mdo_add_secy = vlan_macsec_add_secy, 971 .mdo_upd_secy = vlan_macsec_upd_secy, 972 .mdo_del_secy = vlan_macsec_del_secy, 973 /* Security channels */ 974 .mdo_add_rxsc = vlan_macsec_add_rxsc, 975 .mdo_upd_rxsc = vlan_macsec_upd_rxsc, 976 .mdo_del_rxsc = vlan_macsec_del_rxsc, 977 /* Security associations */ 978 .mdo_add_rxsa = vlan_macsec_add_rxsa, 979 .mdo_upd_rxsa = vlan_macsec_upd_rxsa, 980 .mdo_del_rxsa = vlan_macsec_del_rxsa, 981 .mdo_add_txsa = vlan_macsec_add_txsa, 982 .mdo_upd_txsa = vlan_macsec_upd_txsa, 983 .mdo_del_txsa = vlan_macsec_del_txsa, 984 /* Statistics */ 985 .mdo_get_dev_stats = vlan_macsec_get_dev_stats, 986 .mdo_get_tx_sc_stats = vlan_macsec_get_tx_sc_stats, 987 .mdo_get_tx_sa_stats = vlan_macsec_get_tx_sa_stats, 988 .mdo_get_rx_sc_stats = vlan_macsec_get_rx_sc_stats, 989 .mdo_get_rx_sa_stats = vlan_macsec_get_rx_sa_stats, 990 }; 991 992 #endif 993 994 static const struct ethtool_ops vlan_ethtool_ops = { 995 .get_link_ksettings = vlan_ethtool_get_link_ksettings, 996 .get_drvinfo = vlan_ethtool_get_drvinfo, 997 .get_link = ethtool_op_get_link, 998 .get_ts_info = vlan_ethtool_get_ts_info, 999 }; 1000 1001 static void vlan_transfer_features(struct net_device *dev, 1002 struct net_device *vlandev) 1003 { 1004 struct vlan_dev_priv *vlan = vlan_dev_priv(vlandev); 1005 1006 netif_inherit_tso_max(vlandev, dev); 1007 1008 vlandev->needed_headroom = dev->needed_headroom + VLAN_HLEN; 1009 vlandev->needed_tailroom = dev->needed_tailroom; 1010 vlandev->hard_header_len = dev->hard_header_len; 1011 1012 #if IS_ENABLED(CONFIG_FCOE) 1013 vlandev->fcoe_ddp_xid = dev->fcoe_ddp_xid; 1014 #endif 1015 1016 vlandev->priv_flags &= ~IFF_XMIT_DST_RELEASE; 1017 vlandev->priv_flags |= (vlan->real_dev->priv_flags & IFF_XMIT_DST_RELEASE); 1018 vlandev->hw_enc_features = vlan_tnl_features(vlan->real_dev); 1019 1020 netdev_update_features(vlandev); 1021 } 1022 1023 static void vlan_dev_work(struct net_device *vlandev, unsigned long events) 1024 { 1025 struct vlan_dev_priv *vlan = vlan_dev_priv(vlandev); 1026 struct net_device *real_dev = vlan->real_dev; 1027 bool loose = vlan->flags & VLAN_FLAG_LOOSE_BINDING; 1028 unsigned int flgs; 1029 1030 if (events & VLAN_WORK_LINK_STATE) { 1031 flgs = netif_get_flags(vlandev); 1032 if (real_dev->flags & IFF_UP) { 1033 if (!(flgs & IFF_UP)) { 1034 if (!loose) 1035 netif_change_flags(vlandev, 1036 flgs | IFF_UP, NULL); 1037 vlan_stacked_transfer_operstate(real_dev, 1038 vlandev, vlan); 1039 } 1040 } else if ((flgs & IFF_UP) && !loose) { 1041 netif_change_flags(vlandev, flgs & ~IFF_UP, NULL); 1042 vlan_stacked_transfer_operstate(real_dev, vlandev, vlan); 1043 } 1044 } 1045 1046 if ((events & VLAN_WORK_MTU) && vlandev->mtu > real_dev->mtu) 1047 netif_set_mtu(vlandev, real_dev->mtu); 1048 1049 if (events & VLAN_WORK_FEATURES) 1050 vlan_transfer_features(real_dev, vlandev); 1051 } 1052 1053 static const struct net_device_ops vlan_netdev_ops = { 1054 .ndo_change_mtu = vlan_dev_change_mtu, 1055 .ndo_init = vlan_dev_init, 1056 .ndo_uninit = vlan_dev_uninit, 1057 .ndo_open = vlan_dev_open, 1058 .ndo_stop = vlan_dev_stop, 1059 .ndo_start_xmit = vlan_dev_hard_start_xmit, 1060 .ndo_validate_addr = eth_validate_addr, 1061 .ndo_set_mac_address = vlan_dev_set_mac_address, 1062 .ndo_set_rx_mode = vlan_dev_set_rx_mode, 1063 .ndo_change_rx_flags = vlan_dev_change_rx_flags, 1064 .ndo_work = vlan_dev_work, 1065 .ndo_eth_ioctl = vlan_dev_ioctl, 1066 .ndo_neigh_setup = vlan_dev_neigh_setup, 1067 .ndo_get_stats64 = vlan_dev_get_stats64, 1068 #if IS_ENABLED(CONFIG_FCOE) 1069 .ndo_fcoe_ddp_setup = vlan_dev_fcoe_ddp_setup, 1070 .ndo_fcoe_ddp_done = vlan_dev_fcoe_ddp_done, 1071 .ndo_fcoe_enable = vlan_dev_fcoe_enable, 1072 .ndo_fcoe_disable = vlan_dev_fcoe_disable, 1073 .ndo_fcoe_ddp_target = vlan_dev_fcoe_ddp_target, 1074 #endif 1075 #ifdef NETDEV_FCOE_WWNN 1076 .ndo_fcoe_get_wwn = vlan_dev_fcoe_get_wwn, 1077 #endif 1078 #ifdef CONFIG_NET_POLL_CONTROLLER 1079 .ndo_poll_controller = vlan_dev_poll_controller, 1080 .ndo_netpoll_setup = vlan_dev_netpoll_setup, 1081 .ndo_netpoll_cleanup = vlan_dev_netpoll_cleanup, 1082 #endif 1083 .ndo_fix_features = vlan_dev_fix_features, 1084 .ndo_get_iflink = vlan_dev_get_iflink, 1085 .ndo_fill_forward_path = vlan_dev_fill_forward_path, 1086 .ndo_hwtstamp_get = vlan_hwtstamp_get, 1087 .ndo_hwtstamp_set = vlan_hwtstamp_set, 1088 }; 1089 1090 static void vlan_dev_free(struct net_device *dev) 1091 { 1092 struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 1093 1094 free_percpu(vlan->vlan_pcpu_stats); 1095 vlan->vlan_pcpu_stats = NULL; 1096 1097 /* Get rid of the vlan's reference to real_dev */ 1098 netdev_put(vlan->real_dev, &vlan->dev_tracker); 1099 } 1100 1101 void vlan_setup(struct net_device *dev) 1102 { 1103 ether_setup(dev); 1104 1105 dev->priv_flags |= IFF_802_1Q_VLAN | IFF_NO_QUEUE; 1106 dev->priv_flags |= IFF_UNICAST_FLT; 1107 dev->priv_flags &= ~IFF_TX_SKB_SHARING; 1108 netif_keep_dst(dev); 1109 1110 dev->netdev_ops = &vlan_netdev_ops; 1111 dev->needs_free_netdev = true; 1112 dev->priv_destructor = vlan_dev_free; 1113 dev->ethtool_ops = &vlan_ethtool_ops; 1114 1115 #if IS_ENABLED(CONFIG_MACSEC) 1116 dev->macsec_ops = &macsec_offload_ops; 1117 #endif 1118 dev->min_mtu = 0; 1119 dev->max_mtu = ETH_MAX_MTU; 1120 1121 eth_zero_addr(dev->broadcast); 1122 } 1123