GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / net / 8021q / vlan_dev.c
blob3f017a7a75445fbf4f701211e64b1362cc9769ca
1 /* Modified by Broadcom Corp. Portions Copyright (c) Broadcom Corp, 2012. */
2 /* -*- linux-c -*-
3 * INET 802.1Q VLAN
4 * Ethernet-type device handling.
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
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.
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version
21 * 2 of the License, or (at your option) any later version.
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/skbuff.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/ethtool.h>
30 #include <net/arp.h>
32 #include "vlan.h"
33 #include "vlanproc.h"
34 #include <linux/if_vlan.h>
36 #include <typedefs.h>
37 #include <bcmdefs.h>
40 * Rebuild the Ethernet MAC header. This is called after an ARP
41 * (or in future other address resolution) has completed on this
42 * sk_buff. We now let ARP fill in the other fields.
44 * This routine CANNOT use cached dst->neigh!
45 * Really, it is used only when dst->neigh is wrong.
47 * TODO: This needs a checkup, I'm ignorant here. --BLG
49 static int vlan_dev_rebuild_header(struct sk_buff *skb)
51 struct net_device *dev = skb->dev;
52 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
54 switch (veth->h_vlan_encapsulated_proto) {
55 #ifdef CONFIG_INET
56 case htons(ETH_P_IP):
58 /* TODO: Confirm this will work with VLAN headers... */
59 return arp_find(veth->h_dest, skb);
60 #endif
61 default:
62 pr_debug("%s: unable to resolve type %X addresses.\n",
63 dev->name, ntohs(veth->h_vlan_encapsulated_proto));
65 memcpy(veth->h_source, dev->dev_addr, ETH_ALEN);
66 break;
69 return 0;
72 static inline struct sk_buff *vlan_check_reorder_header(struct sk_buff *skb)
74 if (vlan_dev_info(skb->dev)->flags & VLAN_FLAG_REORDER_HDR) {
75 if (skb_cow(skb, skb_headroom(skb)) < 0)
76 skb = NULL;
77 if (skb) {
78 /* Lifted from Gleb's VLAN code... */
79 memmove(skb->data - ETH_HLEN,
80 skb->data - VLAN_ETH_HLEN, 12);
81 skb->mac_header += VLAN_HLEN;
85 return skb;
88 static inline void vlan_set_encap_proto(struct sk_buff *skb,
89 struct vlan_hdr *vhdr)
91 __be16 proto;
92 unsigned char *rawp;
95 * Was a VLAN packet, grab the encapsulated protocol, which the layer
96 * three protocols care about.
99 proto = vhdr->h_vlan_encapsulated_proto;
100 if (ntohs(proto) >= 1536) {
101 skb->protocol = proto;
102 return;
105 rawp = skb->data;
106 if (*(unsigned short *)rawp == 0xFFFF)
108 * This is a magic hack to spot IPX packets. Older Novell
109 * breaks the protocol design and runs IPX over 802.3 without
110 * an 802.2 LLC layer. We look for FFFF which isn't a used
111 * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
112 * but does for the rest.
114 skb->protocol = htons(ETH_P_802_3);
115 else
117 * Real 802.2 LLC
119 skb->protocol = htons(ETH_P_802_2);
123 * Determine the packet's protocol ID. The rule here is that we
124 * assume 802.3 if the type field is short enough to be a length.
125 * This is normal practice and works for any 'now in use' protocol.
127 * Also, at this point we assume that we ARE dealing exclusively with
128 * VLAN packets, or packets that should be made into VLAN packets based
129 * on a default VLAN ID.
131 * NOTE: Should be similar to ethernet/eth.c.
133 * SANITY NOTE: This method is called when a packet is moving up the stack
134 * towards userland. To get here, it would have already passed
135 * through the ethernet/eth.c eth_type_trans() method.
136 * SANITY NOTE 2: We are referencing to the VLAN_HDR frields, which MAY be
137 * stored UNALIGNED in the memory. RISC systems don't like
138 * such cases very much...
139 * SANITY NOTE 2a: According to Dave Miller & Alexey, it will always be
140 * aligned, so there doesn't need to be any of the unaligned
141 * stuff. It has been commented out now... --Ben
144 int BCMFASTPATH_HOST vlan_skb_recv(struct sk_buff *skb, struct net_device *dev,
145 struct packet_type *ptype, struct net_device *orig_dev)
147 struct vlan_hdr *vhdr;
148 struct vlan_rx_stats *rx_stats;
149 struct net_device *vlan_dev;
150 u16 vlan_id;
151 u16 vlan_tci;
153 skb = skb_share_check(skb, GFP_ATOMIC);
154 if (skb == NULL)
155 goto err_free;
157 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
158 goto err_free;
160 vhdr = (struct vlan_hdr *)skb->data;
161 vlan_tci = ntohs(vhdr->h_vlan_TCI);
162 vlan_id = vlan_tci & VLAN_VID_MASK;
164 rcu_read_lock();
165 vlan_dev = __find_vlan_dev(dev, vlan_id);
167 /* If the VLAN device is defined, we use it.
168 * If not, and the VID is 0, it is a 802.1p packet (not
169 * really a VLAN), so we will just netif_rx it later to the
170 * original interface, but with the skb->proto set to the
171 * wrapped proto: we do nothing here.
174 if (!vlan_dev) {
175 if (vlan_id) {
176 pr_debug("%s: ERROR: No net_device for VID: %u on dev: %s\n",
177 __func__, vlan_id, dev->name);
178 goto err_unlock;
180 rx_stats = NULL;
181 } else {
182 skb->dev = vlan_dev;
184 rx_stats = per_cpu_ptr(vlan_dev_info(skb->dev)->vlan_rx_stats,
185 smp_processor_id());
186 u64_stats_update_begin(&rx_stats->syncp);
187 rx_stats->rx_packets++;
188 rx_stats->rx_bytes += skb->len;
190 skb->priority = vlan_get_ingress_priority(skb->dev, vlan_tci);
192 pr_debug("%s: priority: %u for TCI: %hu\n",
193 __func__, skb->priority, vlan_tci);
195 switch (skb->pkt_type) {
196 case PACKET_BROADCAST:
197 /* Yeah, stats collect these together.. */
198 /* stats->broadcast ++; // no such counter :-( */
199 break;
201 case PACKET_MULTICAST:
202 rx_stats->rx_multicast++;
203 break;
205 case PACKET_OTHERHOST:
206 /* Our lower layer thinks this is not local, let's make
207 * sure.
208 * This allows the VLAN to have a different MAC than the
209 * underlying device, and still route correctly.
211 if (!compare_ether_addr(eth_hdr(skb)->h_dest,
212 skb->dev->dev_addr))
213 skb->pkt_type = PACKET_HOST;
214 break;
215 default:
216 break;
218 u64_stats_update_end(&rx_stats->syncp);
221 skb_pull_rcsum(skb, VLAN_HLEN);
222 vlan_set_encap_proto(skb, vhdr);
224 if (vlan_dev) {
225 skb = vlan_check_reorder_header(skb);
226 if (!skb) {
227 rx_stats->rx_errors++;
228 goto err_unlock;
232 netif_rx(skb);
233 rcu_read_unlock();
234 return NET_RX_SUCCESS;
236 err_unlock:
237 rcu_read_unlock();
238 err_free:
239 kfree_skb(skb);
240 return NET_RX_DROP;
243 static inline u16
244 vlan_dev_get_egress_qos_mask(struct net_device *dev, struct sk_buff *skb)
246 struct vlan_priority_tci_mapping *mp;
248 mp = vlan_dev_info(dev)->egress_priority_map[(skb->priority & 0xF)];
249 while (mp) {
250 if (mp->priority == skb->priority) {
251 return mp->vlan_qos; /* This should already be shifted
252 * to mask correctly with the
253 * VLAN's TCI */
255 mp = mp->next;
257 return 0;
261 * Create the VLAN header for an arbitrary protocol layer
263 * saddr=NULL means use device source address
264 * daddr=NULL means leave destination address (eg unresolved arp)
266 * This is called when the SKB is moving down the stack towards the
267 * physical devices.
269 static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
270 unsigned short type,
271 const void *daddr, const void *saddr,
272 unsigned int len)
274 struct vlan_hdr *vhdr;
275 unsigned int vhdrlen = 0;
276 u16 vlan_tci = 0;
277 int rc;
279 if (WARN_ON(skb_headroom(skb) < dev->hard_header_len))
280 return -ENOSPC;
282 if (!(vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR)) {
283 vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN);
285 vlan_tci = vlan_dev_info(dev)->vlan_id;
286 vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
287 vhdr->h_vlan_TCI = htons(vlan_tci);
290 * Set the protocol type. For a packet of type ETH_P_802_3/2 we
291 * put the length in here instead.
293 if (type != ETH_P_802_3 && type != ETH_P_802_2)
294 vhdr->h_vlan_encapsulated_proto = htons(type);
295 else
296 vhdr->h_vlan_encapsulated_proto = htons(len);
298 skb->protocol = htons(ETH_P_8021Q);
299 type = ETH_P_8021Q;
300 vhdrlen = VLAN_HLEN;
303 /* Before delegating work to the lower layer, enter our MAC-address */
304 if (saddr == NULL)
305 saddr = dev->dev_addr;
307 /* Now make the underlying real hard header */
308 dev = vlan_dev_info(dev)->real_dev;
309 rc = dev_hard_header(skb, dev, type, daddr, saddr, len + vhdrlen);
310 if (rc > 0)
311 rc += vhdrlen;
312 return rc;
315 static netdev_tx_t BCMFASTPATH_HOST vlan_dev_hard_start_xmit(struct sk_buff *skb,
316 struct net_device *dev)
318 int i = skb_get_queue_mapping(skb);
319 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
320 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
321 unsigned int len;
322 int ret;
324 /* Handle non-VLAN frames if they are sent to us, for example by DHCP.
326 * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
327 * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
329 if (veth->h_vlan_proto != htons(ETH_P_8021Q) ||
330 vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR) {
331 unsigned int orig_headroom = skb_headroom(skb);
332 u16 vlan_tci;
334 vlan_dev_info(dev)->cnt_encap_on_xmit++;
336 vlan_tci = vlan_dev_info(dev)->vlan_id;
337 vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
338 skb = __vlan_put_tag(skb, vlan_tci);
339 if (!skb) {
340 txq->tx_dropped++;
341 return NETDEV_TX_OK;
344 if (orig_headroom < VLAN_HLEN)
345 vlan_dev_info(dev)->cnt_inc_headroom_on_tx++;
349 skb_set_dev(skb, vlan_dev_info(dev)->real_dev);
350 len = skb->len;
351 ret = dev_queue_xmit(skb);
353 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
354 txq->tx_packets++;
355 txq->tx_bytes += len;
356 } else
357 txq->tx_dropped++;
359 return ret;
362 static netdev_tx_t vlan_dev_hwaccel_hard_start_xmit(struct sk_buff *skb,
363 struct net_device *dev)
365 int i = skb_get_queue_mapping(skb);
366 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
367 u16 vlan_tci;
368 unsigned int len;
369 int ret;
371 vlan_tci = vlan_dev_info(dev)->vlan_id;
372 vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
373 skb = __vlan_hwaccel_put_tag(skb, vlan_tci);
375 skb->dev = vlan_dev_info(dev)->real_dev;
376 len = skb->len;
377 ret = dev_queue_xmit(skb);
379 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
380 txq->tx_packets++;
381 txq->tx_bytes += len;
382 } else
383 txq->tx_dropped++;
385 return ret;
388 static u16 vlan_dev_select_queue(struct net_device *dev, struct sk_buff *skb)
390 struct net_device *rdev = vlan_dev_info(dev)->real_dev;
391 const struct net_device_ops *ops = rdev->netdev_ops;
393 return ops->ndo_select_queue(rdev, skb);
396 static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
398 /* TODO: gotta make sure the underlying layer can handle it,
399 * maybe an IFF_VLAN_CAPABLE flag for devices?
401 if (vlan_dev_info(dev)->real_dev->mtu < new_mtu)
402 return -ERANGE;
404 dev->mtu = new_mtu;
406 return 0;
409 void vlan_dev_set_ingress_priority(const struct net_device *dev,
410 u32 skb_prio, u16 vlan_prio)
412 struct vlan_dev_info *vlan = vlan_dev_info(dev);
414 if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio)
415 vlan->nr_ingress_mappings--;
416 else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio)
417 vlan->nr_ingress_mappings++;
419 vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
422 int vlan_dev_set_egress_priority(const struct net_device *dev,
423 u32 skb_prio, u16 vlan_prio)
425 struct vlan_dev_info *vlan = vlan_dev_info(dev);
426 struct vlan_priority_tci_mapping *mp = NULL;
427 struct vlan_priority_tci_mapping *np;
428 u32 vlan_qos = (vlan_prio << VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK;
430 /* See if a priority mapping exists.. */
431 mp = vlan->egress_priority_map[skb_prio & 0xF];
432 while (mp) {
433 if (mp->priority == skb_prio) {
434 if (mp->vlan_qos && !vlan_qos)
435 vlan->nr_egress_mappings--;
436 else if (!mp->vlan_qos && vlan_qos)
437 vlan->nr_egress_mappings++;
438 mp->vlan_qos = vlan_qos;
439 return 0;
441 mp = mp->next;
444 /* Create a new mapping then. */
445 mp = vlan->egress_priority_map[skb_prio & 0xF];
446 np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL);
447 if (!np)
448 return -ENOBUFS;
450 np->next = mp;
451 np->priority = skb_prio;
452 np->vlan_qos = vlan_qos;
453 vlan->egress_priority_map[skb_prio & 0xF] = np;
454 if (vlan_qos)
455 vlan->nr_egress_mappings++;
456 return 0;
459 /* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */
460 int vlan_dev_change_flags(const struct net_device *dev, u32 flags, u32 mask)
462 struct vlan_dev_info *vlan = vlan_dev_info(dev);
463 u32 old_flags = vlan->flags;
465 if (mask & ~(VLAN_FLAG_REORDER_HDR | VLAN_FLAG_GVRP |
466 VLAN_FLAG_LOOSE_BINDING))
467 return -EINVAL;
469 vlan->flags = (old_flags & ~mask) | (flags & mask);
471 if (netif_running(dev) && (vlan->flags ^ old_flags) & VLAN_FLAG_GVRP) {
472 if (vlan->flags & VLAN_FLAG_GVRP)
473 vlan_gvrp_request_join(dev);
474 else
475 vlan_gvrp_request_leave(dev);
477 return 0;
480 void vlan_dev_get_realdev_name(const struct net_device *dev, char *result)
482 strncpy(result, vlan_dev_info(dev)->real_dev->name, 23);
485 static int vlan_dev_open(struct net_device *dev)
487 struct vlan_dev_info *vlan = vlan_dev_info(dev);
488 struct net_device *real_dev = vlan->real_dev;
489 int err;
491 if (!(real_dev->flags & IFF_UP) &&
492 !(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
493 return -ENETDOWN;
495 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr)) {
496 err = dev_uc_add(real_dev, dev->dev_addr);
497 if (err < 0)
498 goto out;
501 if (dev->flags & IFF_ALLMULTI) {
502 err = dev_set_allmulti(real_dev, 1);
503 if (err < 0)
504 goto del_unicast;
506 if (dev->flags & IFF_PROMISC) {
507 err = dev_set_promiscuity(real_dev, 1);
508 if (err < 0)
509 goto clear_allmulti;
512 memcpy(vlan->real_dev_addr, real_dev->dev_addr, ETH_ALEN);
514 if (vlan->flags & VLAN_FLAG_GVRP)
515 vlan_gvrp_request_join(dev);
517 if (netif_carrier_ok(real_dev))
518 netif_carrier_on(dev);
519 return 0;
521 clear_allmulti:
522 if (dev->flags & IFF_ALLMULTI)
523 dev_set_allmulti(real_dev, -1);
524 del_unicast:
525 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
526 dev_uc_del(real_dev, dev->dev_addr);
527 out:
528 netif_carrier_off(dev);
529 return err;
532 static int vlan_dev_stop(struct net_device *dev)
534 struct vlan_dev_info *vlan = vlan_dev_info(dev);
535 struct net_device *real_dev = vlan->real_dev;
537 if (vlan->flags & VLAN_FLAG_GVRP)
538 vlan_gvrp_request_leave(dev);
540 dev_mc_unsync(real_dev, dev);
541 dev_uc_unsync(real_dev, dev);
542 if (dev->flags & IFF_ALLMULTI)
543 dev_set_allmulti(real_dev, -1);
544 if (dev->flags & IFF_PROMISC)
545 dev_set_promiscuity(real_dev, -1);
547 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
548 dev_uc_del(real_dev, dev->dev_addr);
550 netif_carrier_off(dev);
551 return 0;
554 static int vlan_dev_set_mac_address(struct net_device *dev, void *p)
556 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
557 struct sockaddr *addr = p;
558 int err;
560 if (!is_valid_ether_addr(addr->sa_data))
561 return -EADDRNOTAVAIL;
563 if (!(dev->flags & IFF_UP))
564 goto out;
566 if (compare_ether_addr(addr->sa_data, real_dev->dev_addr)) {
567 err = dev_uc_add(real_dev, addr->sa_data);
568 if (err < 0)
569 return err;
572 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
573 dev_uc_del(real_dev, dev->dev_addr);
575 out:
576 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
577 return 0;
580 static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
582 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
583 const struct net_device_ops *ops = real_dev->netdev_ops;
584 struct ifreq ifrr;
585 int err = -EOPNOTSUPP;
587 strncpy(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
588 ifrr.ifr_ifru = ifr->ifr_ifru;
590 switch (cmd) {
591 case SIOCGMIIPHY:
592 case SIOCGMIIREG:
593 case SIOCSMIIREG:
594 if (netif_device_present(real_dev) && ops->ndo_do_ioctl)
595 err = ops->ndo_do_ioctl(real_dev, &ifrr, cmd);
596 break;
599 if (!err)
600 ifr->ifr_ifru = ifrr.ifr_ifru;
602 return err;
605 static int vlan_dev_neigh_setup(struct net_device *dev, struct neigh_parms *pa)
607 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
608 const struct net_device_ops *ops = real_dev->netdev_ops;
609 int err = 0;
611 if (netif_device_present(real_dev) && ops->ndo_neigh_setup)
612 err = ops->ndo_neigh_setup(real_dev, pa);
614 return err;
617 #if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
618 static int vlan_dev_fcoe_ddp_setup(struct net_device *dev, u16 xid,
619 struct scatterlist *sgl, unsigned int sgc)
621 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
622 const struct net_device_ops *ops = real_dev->netdev_ops;
623 int rc = 0;
625 if (ops->ndo_fcoe_ddp_setup)
626 rc = ops->ndo_fcoe_ddp_setup(real_dev, xid, sgl, sgc);
628 return rc;
631 static int vlan_dev_fcoe_ddp_done(struct net_device *dev, u16 xid)
633 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
634 const struct net_device_ops *ops = real_dev->netdev_ops;
635 int len = 0;
637 if (ops->ndo_fcoe_ddp_done)
638 len = ops->ndo_fcoe_ddp_done(real_dev, xid);
640 return len;
643 static int vlan_dev_fcoe_enable(struct net_device *dev)
645 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
646 const struct net_device_ops *ops = real_dev->netdev_ops;
647 int rc = -EINVAL;
649 if (ops->ndo_fcoe_enable)
650 rc = ops->ndo_fcoe_enable(real_dev);
651 return rc;
654 static int vlan_dev_fcoe_disable(struct net_device *dev)
656 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
657 const struct net_device_ops *ops = real_dev->netdev_ops;
658 int rc = -EINVAL;
660 if (ops->ndo_fcoe_disable)
661 rc = ops->ndo_fcoe_disable(real_dev);
662 return rc;
665 static int vlan_dev_fcoe_get_wwn(struct net_device *dev, u64 *wwn, int type)
667 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
668 const struct net_device_ops *ops = real_dev->netdev_ops;
669 int rc = -EINVAL;
671 if (ops->ndo_fcoe_get_wwn)
672 rc = ops->ndo_fcoe_get_wwn(real_dev, wwn, type);
673 return rc;
675 #endif
677 static void vlan_dev_change_rx_flags(struct net_device *dev, int change)
679 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
681 if (change & IFF_ALLMULTI)
682 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
683 if (change & IFF_PROMISC)
684 dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1);
687 static void vlan_dev_set_rx_mode(struct net_device *vlan_dev)
689 dev_mc_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
690 dev_uc_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
694 * vlan network devices have devices nesting below it, and are a special
695 * "super class" of normal network devices; split their locks off into a
696 * separate class since they always nest.
698 static struct lock_class_key vlan_netdev_xmit_lock_key;
699 static struct lock_class_key vlan_netdev_addr_lock_key;
701 static void vlan_dev_set_lockdep_one(struct net_device *dev,
702 struct netdev_queue *txq,
703 void *_subclass)
705 lockdep_set_class_and_subclass(&txq->_xmit_lock,
706 &vlan_netdev_xmit_lock_key,
707 *(int *)_subclass);
710 static void vlan_dev_set_lockdep_class(struct net_device *dev, int subclass)
712 lockdep_set_class_and_subclass(&dev->addr_list_lock,
713 &vlan_netdev_addr_lock_key,
714 subclass);
715 netdev_for_each_tx_queue(dev, vlan_dev_set_lockdep_one, &subclass);
718 static const struct header_ops vlan_header_ops = {
719 .create = vlan_dev_hard_header,
720 .rebuild = vlan_dev_rebuild_header,
721 .parse = eth_header_parse,
724 static const struct net_device_ops vlan_netdev_ops, vlan_netdev_accel_ops,
725 vlan_netdev_ops_sq, vlan_netdev_accel_ops_sq;
727 static int vlan_dev_init(struct net_device *dev)
729 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
730 int subclass = 0;
732 netif_carrier_off(dev);
734 /* IFF_BROADCAST|IFF_MULTICAST; ??? */
735 dev->flags = real_dev->flags & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
736 IFF_MASTER | IFF_SLAVE);
737 dev->iflink = real_dev->ifindex;
738 dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
739 (1<<__LINK_STATE_DORMANT))) |
740 (1<<__LINK_STATE_PRESENT);
742 dev->features |= real_dev->features & real_dev->vlan_features;
743 dev->gso_max_size = real_dev->gso_max_size;
745 /* ipv6 shared card related stuff */
746 dev->dev_id = real_dev->dev_id;
748 if (is_zero_ether_addr(dev->dev_addr))
749 memcpy(dev->dev_addr, real_dev->dev_addr, dev->addr_len);
750 if (is_zero_ether_addr(dev->broadcast))
751 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
753 #if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
754 dev->fcoe_ddp_xid = real_dev->fcoe_ddp_xid;
755 #endif
757 if (real_dev->features & NETIF_F_HW_VLAN_TX) {
758 dev->header_ops = real_dev->header_ops;
759 dev->hard_header_len = real_dev->hard_header_len;
760 if (real_dev->netdev_ops->ndo_select_queue)
761 dev->netdev_ops = &vlan_netdev_accel_ops_sq;
762 else
763 dev->netdev_ops = &vlan_netdev_accel_ops;
764 } else {
765 dev->header_ops = &vlan_header_ops;
766 dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
767 if (real_dev->netdev_ops->ndo_select_queue)
768 dev->netdev_ops = &vlan_netdev_ops_sq;
769 else
770 dev->netdev_ops = &vlan_netdev_ops;
773 if (is_vlan_dev(real_dev))
774 subclass = 1;
776 vlan_dev_set_lockdep_class(dev, subclass);
778 vlan_dev_info(dev)->vlan_rx_stats = alloc_percpu(struct vlan_rx_stats);
779 if (!vlan_dev_info(dev)->vlan_rx_stats)
780 return -ENOMEM;
782 return 0;
785 static void vlan_dev_uninit(struct net_device *dev)
787 struct vlan_priority_tci_mapping *pm;
788 struct vlan_dev_info *vlan = vlan_dev_info(dev);
789 int i;
791 free_percpu(vlan->vlan_rx_stats);
792 vlan->vlan_rx_stats = NULL;
793 for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
794 while ((pm = vlan->egress_priority_map[i]) != NULL) {
795 vlan->egress_priority_map[i] = pm->next;
796 kfree(pm);
801 static int vlan_ethtool_get_settings(struct net_device *dev,
802 struct ethtool_cmd *cmd)
804 const struct vlan_dev_info *vlan = vlan_dev_info(dev);
805 return dev_ethtool_get_settings(vlan->real_dev, cmd);
808 static void vlan_ethtool_get_drvinfo(struct net_device *dev,
809 struct ethtool_drvinfo *info)
811 strcpy(info->driver, vlan_fullname);
812 strcpy(info->version, vlan_version);
813 strcpy(info->fw_version, "N/A");
816 static u32 vlan_ethtool_get_rx_csum(struct net_device *dev)
818 const struct vlan_dev_info *vlan = vlan_dev_info(dev);
819 return dev_ethtool_get_rx_csum(vlan->real_dev);
822 static u32 vlan_ethtool_get_flags(struct net_device *dev)
824 const struct vlan_dev_info *vlan = vlan_dev_info(dev);
825 return dev_ethtool_get_flags(vlan->real_dev);
828 static struct rtnl_link_stats64 *vlan_dev_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
830 dev_txq_stats_fold(dev, stats);
832 if (vlan_dev_info(dev)->vlan_rx_stats) {
833 struct vlan_rx_stats *p, accum = {0};
834 int i;
836 for_each_possible_cpu(i) {
837 u64 rxpackets, rxbytes, rxmulticast;
838 unsigned int start;
840 p = per_cpu_ptr(vlan_dev_info(dev)->vlan_rx_stats, i);
841 do {
842 start = u64_stats_fetch_begin_bh(&p->syncp);
843 rxpackets = p->rx_packets;
844 rxbytes = p->rx_bytes;
845 rxmulticast = p->rx_multicast;
846 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
847 accum.rx_packets += rxpackets;
848 accum.rx_bytes += rxbytes;
849 accum.rx_multicast += rxmulticast;
850 /* rx_errors is an ulong, not protected by syncp */
851 accum.rx_errors += p->rx_errors;
853 stats->rx_packets = accum.rx_packets;
854 stats->rx_bytes = accum.rx_bytes;
855 stats->rx_errors = accum.rx_errors;
856 stats->multicast = accum.rx_multicast;
858 return stats;
861 static int vlan_ethtool_set_tso(struct net_device *dev, u32 data)
863 if (data) {
864 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
866 /* Underlying device must support TSO for VLAN-tagged packets
867 * and must have TSO enabled now.
869 if (!(real_dev->vlan_features & NETIF_F_TSO))
870 return -EOPNOTSUPP;
871 if (!(real_dev->features & NETIF_F_TSO))
872 return -EINVAL;
873 dev->features |= NETIF_F_TSO;
874 } else {
875 dev->features &= ~NETIF_F_TSO;
877 return 0;
880 static const struct ethtool_ops vlan_ethtool_ops = {
881 .get_settings = vlan_ethtool_get_settings,
882 .get_drvinfo = vlan_ethtool_get_drvinfo,
883 .get_link = ethtool_op_get_link,
884 .get_rx_csum = vlan_ethtool_get_rx_csum,
885 .get_flags = vlan_ethtool_get_flags,
886 .set_tso = vlan_ethtool_set_tso,
889 static const struct net_device_ops vlan_netdev_ops = {
890 .ndo_change_mtu = vlan_dev_change_mtu,
891 .ndo_init = vlan_dev_init,
892 .ndo_uninit = vlan_dev_uninit,
893 .ndo_open = vlan_dev_open,
894 .ndo_stop = vlan_dev_stop,
895 .ndo_start_xmit = vlan_dev_hard_start_xmit,
896 .ndo_validate_addr = eth_validate_addr,
897 .ndo_set_mac_address = vlan_dev_set_mac_address,
898 .ndo_set_rx_mode = vlan_dev_set_rx_mode,
899 .ndo_set_multicast_list = vlan_dev_set_rx_mode,
900 .ndo_change_rx_flags = vlan_dev_change_rx_flags,
901 .ndo_do_ioctl = vlan_dev_ioctl,
902 .ndo_neigh_setup = vlan_dev_neigh_setup,
903 .ndo_get_stats64 = vlan_dev_get_stats64,
904 #if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
905 .ndo_fcoe_ddp_setup = vlan_dev_fcoe_ddp_setup,
906 .ndo_fcoe_ddp_done = vlan_dev_fcoe_ddp_done,
907 .ndo_fcoe_enable = vlan_dev_fcoe_enable,
908 .ndo_fcoe_disable = vlan_dev_fcoe_disable,
909 .ndo_fcoe_get_wwn = vlan_dev_fcoe_get_wwn,
910 #endif
913 static const struct net_device_ops vlan_netdev_accel_ops = {
914 .ndo_change_mtu = vlan_dev_change_mtu,
915 .ndo_init = vlan_dev_init,
916 .ndo_uninit = vlan_dev_uninit,
917 .ndo_open = vlan_dev_open,
918 .ndo_stop = vlan_dev_stop,
919 .ndo_start_xmit = vlan_dev_hwaccel_hard_start_xmit,
920 .ndo_validate_addr = eth_validate_addr,
921 .ndo_set_mac_address = vlan_dev_set_mac_address,
922 .ndo_set_rx_mode = vlan_dev_set_rx_mode,
923 .ndo_set_multicast_list = vlan_dev_set_rx_mode,
924 .ndo_change_rx_flags = vlan_dev_change_rx_flags,
925 .ndo_do_ioctl = vlan_dev_ioctl,
926 .ndo_neigh_setup = vlan_dev_neigh_setup,
927 .ndo_get_stats64 = vlan_dev_get_stats64,
928 #if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
929 .ndo_fcoe_ddp_setup = vlan_dev_fcoe_ddp_setup,
930 .ndo_fcoe_ddp_done = vlan_dev_fcoe_ddp_done,
931 .ndo_fcoe_enable = vlan_dev_fcoe_enable,
932 .ndo_fcoe_disable = vlan_dev_fcoe_disable,
933 .ndo_fcoe_get_wwn = vlan_dev_fcoe_get_wwn,
934 #endif
937 static const struct net_device_ops vlan_netdev_ops_sq = {
938 .ndo_select_queue = vlan_dev_select_queue,
939 .ndo_change_mtu = vlan_dev_change_mtu,
940 .ndo_init = vlan_dev_init,
941 .ndo_uninit = vlan_dev_uninit,
942 .ndo_open = vlan_dev_open,
943 .ndo_stop = vlan_dev_stop,
944 .ndo_start_xmit = vlan_dev_hard_start_xmit,
945 .ndo_validate_addr = eth_validate_addr,
946 .ndo_set_mac_address = vlan_dev_set_mac_address,
947 .ndo_set_rx_mode = vlan_dev_set_rx_mode,
948 .ndo_set_multicast_list = vlan_dev_set_rx_mode,
949 .ndo_change_rx_flags = vlan_dev_change_rx_flags,
950 .ndo_do_ioctl = vlan_dev_ioctl,
951 .ndo_neigh_setup = vlan_dev_neigh_setup,
952 .ndo_get_stats64 = vlan_dev_get_stats64,
953 #if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
954 .ndo_fcoe_ddp_setup = vlan_dev_fcoe_ddp_setup,
955 .ndo_fcoe_ddp_done = vlan_dev_fcoe_ddp_done,
956 .ndo_fcoe_enable = vlan_dev_fcoe_enable,
957 .ndo_fcoe_disable = vlan_dev_fcoe_disable,
958 .ndo_fcoe_get_wwn = vlan_dev_fcoe_get_wwn,
959 #endif
962 static const struct net_device_ops vlan_netdev_accel_ops_sq = {
963 .ndo_select_queue = vlan_dev_select_queue,
964 .ndo_change_mtu = vlan_dev_change_mtu,
965 .ndo_init = vlan_dev_init,
966 .ndo_uninit = vlan_dev_uninit,
967 .ndo_open = vlan_dev_open,
968 .ndo_stop = vlan_dev_stop,
969 .ndo_start_xmit = vlan_dev_hwaccel_hard_start_xmit,
970 .ndo_validate_addr = eth_validate_addr,
971 .ndo_set_mac_address = vlan_dev_set_mac_address,
972 .ndo_set_rx_mode = vlan_dev_set_rx_mode,
973 .ndo_set_multicast_list = vlan_dev_set_rx_mode,
974 .ndo_change_rx_flags = vlan_dev_change_rx_flags,
975 .ndo_do_ioctl = vlan_dev_ioctl,
976 .ndo_neigh_setup = vlan_dev_neigh_setup,
977 .ndo_get_stats64 = vlan_dev_get_stats64,
978 #if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
979 .ndo_fcoe_ddp_setup = vlan_dev_fcoe_ddp_setup,
980 .ndo_fcoe_ddp_done = vlan_dev_fcoe_ddp_done,
981 .ndo_fcoe_enable = vlan_dev_fcoe_enable,
982 .ndo_fcoe_disable = vlan_dev_fcoe_disable,
983 .ndo_fcoe_get_wwn = vlan_dev_fcoe_get_wwn,
984 #endif
987 void vlan_setup(struct net_device *dev)
989 ether_setup(dev);
991 dev->priv_flags |= IFF_802_1Q_VLAN;
992 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
993 dev->tx_queue_len = 0;
995 dev->netdev_ops = &vlan_netdev_ops;
996 dev->destructor = free_netdev;
997 dev->ethtool_ops = &vlan_ethtool_ops;
999 memset(dev->broadcast, 0, ETH_ALEN);