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