[VLAN]: Clean up vlan_skb_recv()
[safe/jmp/linux-2.6] / net / 8021q / vlan_dev.c
1 /* -*- linux-c -*-
2  * INET         802.1Q VLAN
3  *              Ethernet-type device handling.
4  *
5  * Authors:     Ben Greear <greearb@candelatech.com>
6  *              Please send support related email to: netdev@vger.kernel.org
7  *              VLAN Home Page: http://www.candelatech.com/~greear/vlan.html
8  *
9  * Fixes:       Mar 22 2001: Martin Bokaemper <mbokaemper@unispherenetworks.com>
10  *                - reset skb->pkt_type on incoming packets when MAC was changed
11  *                - see that changed MAC is saddr for outgoing packets
12  *              Oct 20, 2001:  Ard van Breeman:
13  *                - Fix MC-list, finally.
14  *                - Flush MC-list on VLAN destroy.
15  *
16  *
17  *              This program is free software; you can redistribute it and/or
18  *              modify it under the terms of the GNU General Public License
19  *              as published by the Free Software Foundation; either version
20  *              2 of the License, or (at your option) any later version.
21  */
22
23 #include <linux/module.h>
24 #include <linux/mm.h>
25 #include <linux/in.h>
26 #include <linux/init.h>
27 #include <asm/uaccess.h> /* for copy_from_user */
28 #include <linux/skbuff.h>
29 #include <linux/netdevice.h>
30 #include <linux/etherdevice.h>
31 #include <net/datalink.h>
32 #include <net/p8022.h>
33 #include <net/arp.h>
34
35 #include "vlan.h"
36 #include "vlanproc.h"
37 #include <linux/if_vlan.h>
38 #include <net/ip.h>
39
40 /*
41  *      Rebuild the Ethernet MAC header. This is called after an ARP
42  *      (or in future other address resolution) has completed on this
43  *      sk_buff. We now let ARP fill in the other fields.
44  *
45  *      This routine CANNOT use cached dst->neigh!
46  *      Really, it is used only when dst->neigh is wrong.
47  *
48  * TODO:  This needs a checkup, I'm ignorant here. --BLG
49  */
50 static int vlan_dev_rebuild_header(struct sk_buff *skb)
51 {
52         struct net_device *dev = skb->dev;
53         struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
54
55         switch (veth->h_vlan_encapsulated_proto) {
56 #ifdef CONFIG_INET
57         case __constant_htons(ETH_P_IP):
58
59                 /* TODO:  Confirm this will work with VLAN headers... */
60                 return arp_find(veth->h_dest, skb);
61 #endif
62         default:
63                 pr_debug("%s: unable to resolve type %X addresses.\n",
64                          dev->name, ntohs(veth->h_vlan_encapsulated_proto));
65
66                 memcpy(veth->h_source, dev->dev_addr, ETH_ALEN);
67                 break;
68         }
69
70         return 0;
71 }
72
73 static inline struct sk_buff *vlan_check_reorder_header(struct sk_buff *skb)
74 {
75         if (vlan_dev_info(skb->dev)->flags & VLAN_FLAG_REORDER_HDR) {
76                 if (skb_shared(skb) || skb_cloned(skb)) {
77                         struct sk_buff *nskb = skb_copy(skb, GFP_ATOMIC);
78                         kfree_skb(skb);
79                         skb = nskb;
80                 }
81                 if (skb) {
82                         /* Lifted from Gleb's VLAN code... */
83                         memmove(skb->data - ETH_HLEN,
84                                 skb->data - VLAN_ETH_HLEN, 12);
85                         skb->mac_header += VLAN_HLEN;
86                 }
87         }
88
89         return skb;
90 }
91
92 /*
93  *      Determine the packet's protocol ID. The rule here is that we
94  *      assume 802.3 if the type field is short enough to be a length.
95  *      This is normal practice and works for any 'now in use' protocol.
96  *
97  *  Also, at this point we assume that we ARE dealing exclusively with
98  *  VLAN packets, or packets that should be made into VLAN packets based
99  *  on a default VLAN ID.
100  *
101  *  NOTE:  Should be similar to ethernet/eth.c.
102  *
103  *  SANITY NOTE:  This method is called when a packet is moving up the stack
104  *                towards userland.  To get here, it would have already passed
105  *                through the ethernet/eth.c eth_type_trans() method.
106  *  SANITY NOTE 2: We are referencing to the VLAN_HDR frields, which MAY be
107  *                 stored UNALIGNED in the memory.  RISC systems don't like
108  *                 such cases very much...
109  *  SANITY NOTE 2a: According to Dave Miller & Alexey, it will always be
110  *                  aligned, so there doesn't need to be any of the unaligned
111  *                  stuff.  It has been commented out now...  --Ben
112  *
113  */
114 int vlan_skb_recv(struct sk_buff *skb, struct net_device *dev,
115                   struct packet_type *ptype, struct net_device *orig_dev)
116 {
117         unsigned char *rawp;
118         struct vlan_hdr *vhdr;
119         unsigned short vid;
120         struct net_device_stats *stats;
121         unsigned short vlan_TCI;
122         __be16 proto;
123
124         if (dev->nd_net != &init_net)
125                 goto err_free;
126
127         skb = skb_share_check(skb, GFP_ATOMIC);
128         if (skb == NULL)
129                 goto err_free;
130
131         if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
132                 goto err_free;
133
134         vhdr = (struct vlan_hdr *)skb->data;
135         vlan_TCI = ntohs(vhdr->h_vlan_TCI);
136         vid = (vlan_TCI & VLAN_VID_MASK);
137
138         rcu_read_lock();
139         skb->dev = __find_vlan_dev(dev, vid);
140         if (!skb->dev) {
141                 pr_debug("%s: ERROR: No net_device for VID: %u on dev: %s\n",
142                          __FUNCTION__, (unsigned int)vid, dev->name);
143                 goto err_unlock;
144         }
145
146         skb->dev->last_rx = jiffies;
147
148         stats = &skb->dev->stats;
149         stats->rx_packets++;
150         stats->rx_bytes += skb->len;
151
152         skb_pull_rcsum(skb, VLAN_HLEN);
153
154         skb->priority = vlan_get_ingress_priority(skb->dev,
155                                                   ntohs(vhdr->h_vlan_TCI));
156
157         pr_debug("%s: priority: %u for TCI: %hu\n",
158                  __FUNCTION__, skb->priority, ntohs(vhdr->h_vlan_TCI));
159
160         switch (skb->pkt_type) {
161         case PACKET_BROADCAST: /* Yeah, stats collect these together.. */
162                 /* stats->broadcast ++; // no such counter :-( */
163                 break;
164
165         case PACKET_MULTICAST:
166                 stats->multicast++;
167                 break;
168
169         case PACKET_OTHERHOST:
170                 /* Our lower layer thinks this is not local, let's make sure.
171                  * This allows the VLAN to have a different MAC than the
172                  * underlying device, and still route correctly.
173                  */
174                 if (!compare_ether_addr(eth_hdr(skb)->h_dest,
175                                         skb->dev->dev_addr))
176                         skb->pkt_type = PACKET_HOST;
177                 break;
178         default:
179                 break;
180         }
181
182         /*  Was a VLAN packet, grab the encapsulated protocol, which the layer
183          * three protocols care about.
184          */
185         proto = vhdr->h_vlan_encapsulated_proto;
186         if (ntohs(proto) >= 1536) {
187                 skb->protocol = proto;
188                 goto recv;
189         }
190
191         /*
192          * This is a magic hack to spot IPX packets. Older Novell breaks
193          * the protocol design and runs IPX over 802.3 without an 802.2 LLC
194          * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
195          * won't work for fault tolerant netware but does for the rest.
196          */
197         rawp = skb->data;
198         if (*(unsigned short *)rawp == 0xFFFF) {
199                 skb->protocol = htons(ETH_P_802_3);
200                 goto recv;
201         }
202
203         /*
204          *      Real 802.2 LLC
205          */
206         skb->protocol = htons(ETH_P_802_2);
207
208 recv:
209         skb = vlan_check_reorder_header(skb);
210         if (!skb) {
211                 stats->rx_errors++;
212                 goto err_unlock;
213         }
214
215         netif_rx(skb);
216         rcu_read_unlock();
217         return NET_RX_SUCCESS;
218
219 err_unlock:
220         rcu_read_unlock();
221 err_free:
222         kfree_skb(skb);
223         return NET_RX_DROP;
224 }
225
226 static inline unsigned short
227 vlan_dev_get_egress_qos_mask(struct net_device *dev, struct sk_buff *skb)
228 {
229         struct vlan_priority_tci_mapping *mp;
230
231         mp = vlan_dev_info(dev)->egress_priority_map[(skb->priority & 0xF)];
232         while (mp) {
233                 if (mp->priority == skb->priority) {
234                         return mp->vlan_qos; /* This should already be shifted
235                                               * to mask correctly with the
236                                               * VLAN's TCI */
237                 }
238                 mp = mp->next;
239         }
240         return 0;
241 }
242
243 /*
244  *      Create the VLAN header for an arbitrary protocol layer
245  *
246  *      saddr=NULL      means use device source address
247  *      daddr=NULL      means leave destination address (eg unresolved arp)
248  *
249  *  This is called when the SKB is moving down the stack towards the
250  *  physical devices.
251  */
252 static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
253                                 unsigned short type,
254                                 const void *daddr, const void *saddr,
255                                 unsigned int len)
256 {
257         struct vlan_hdr *vhdr;
258         unsigned short veth_TCI = 0;
259         int rc = 0;
260         int build_vlan_header = 0;
261         struct net_device *vdev = dev;
262
263         pr_debug("%s: skb: %p type: %hx len: %u vlan_id: %hx, daddr: %p\n",
264                  __FUNCTION__, skb, type, len, vlan_dev_info(dev)->vlan_id,
265                  daddr);
266
267         /* build vlan header only if re_order_header flag is NOT set.  This
268          * fixes some programs that get confused when they see a VLAN device
269          * sending a frame that is VLAN encoded (the consensus is that the VLAN
270          * device should look completely like an Ethernet device when the
271          * REORDER_HEADER flag is set)  The drawback to this is some extra
272          * header shuffling in the hard_start_xmit.  Users can turn off this
273          * REORDER behaviour with the vconfig tool.
274          */
275         if (!(vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR))
276                 build_vlan_header = 1;
277
278         if (build_vlan_header) {
279                 vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN);
280
281                 /* build the four bytes that make this a VLAN header. */
282
283                 /* Now, construct the second two bytes. This field looks
284                  * something like:
285                  * usr_priority: 3 bits  (high bits)
286                  * CFI           1 bit
287                  * VLAN ID       12 bits (low bits)
288                  *
289                  */
290                 veth_TCI = vlan_dev_info(dev)->vlan_id;
291                 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
292
293                 vhdr->h_vlan_TCI = htons(veth_TCI);
294
295                 /*
296                  *  Set the protocol type. For a packet of type ETH_P_802_3 we
297                  *  put the length in here instead. It is up to the 802.2
298                  *  layer to carry protocol information.
299                  */
300
301                 if (type != ETH_P_802_3)
302                         vhdr->h_vlan_encapsulated_proto = htons(type);
303                 else
304                         vhdr->h_vlan_encapsulated_proto = htons(len);
305
306                 skb->protocol = htons(ETH_P_8021Q);
307                 skb_reset_network_header(skb);
308         }
309
310         /* Before delegating work to the lower layer, enter our MAC-address */
311         if (saddr == NULL)
312                 saddr = dev->dev_addr;
313
314         dev = vlan_dev_info(dev)->real_dev;
315
316         /* MPLS can send us skbuffs w/out enough space. This check will grow
317          * the skb if it doesn't have enough headroom. Not a beautiful solution,
318          * so I'll tick a counter so that users can know it's happening...
319          * If they care...
320          */
321
322         /* NOTE: This may still break if the underlying device is not the final
323          * device (and thus there are more headers to add...) It should work for
324          * good-ole-ethernet though.
325          */
326         if (skb_headroom(skb) < dev->hard_header_len) {
327                 struct sk_buff *sk_tmp = skb;
328                 skb = skb_realloc_headroom(sk_tmp, dev->hard_header_len);
329                 kfree_skb(sk_tmp);
330                 if (skb == NULL) {
331                         struct net_device_stats *stats = &vdev->stats;
332                         stats->tx_dropped++;
333                         return -ENOMEM;
334                 }
335                 vlan_dev_info(vdev)->cnt_inc_headroom_on_tx++;
336                 pr_debug("%s: %s: had to grow skb\n", __FUNCTION__, vdev->name);
337         }
338
339         if (build_vlan_header) {
340                 /* Now make the underlying real hard header */
341                 rc = dev_hard_header(skb, dev, ETH_P_8021Q, daddr, saddr,
342                                      len + VLAN_HLEN);
343                 if (rc > 0)
344                         rc += VLAN_HLEN;
345                 else if (rc < 0)
346                         rc -= VLAN_HLEN;
347         } else
348                 /* If here, then we'll just make a normal looking ethernet
349                  * frame, but, the hard_start_xmit method will insert the tag
350                  * (it has to be able to do this for bridged and other skbs
351                  * that don't come down the protocol stack in an orderly manner.
352                  */
353                 rc = dev_hard_header(skb, dev, type, daddr, saddr, len);
354
355         return rc;
356 }
357
358 static int vlan_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
359 {
360         struct net_device_stats *stats = &dev->stats;
361         struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
362
363         /* Handle non-VLAN frames if they are sent to us, for example by DHCP.
364          *
365          * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
366          * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
367          */
368
369         if (veth->h_vlan_proto != htons(ETH_P_8021Q) ||
370                 vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR) {
371                 int orig_headroom = skb_headroom(skb);
372                 unsigned short veth_TCI;
373
374                 /* This is not a VLAN frame...but we can fix that! */
375                 vlan_dev_info(dev)->cnt_encap_on_xmit++;
376
377                 pr_debug("%s: proto to encap: 0x%hx\n",
378                          __FUNCTION__, htons(veth->h_vlan_proto));
379                 /* Construct the second two bytes. This field looks something
380                  * like:
381                  * usr_priority: 3 bits  (high bits)
382                  * CFI           1 bit
383                  * VLAN ID       12 bits (low bits)
384                  */
385                 veth_TCI = vlan_dev_info(dev)->vlan_id;
386                 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
387
388                 skb = __vlan_put_tag(skb, veth_TCI);
389                 if (!skb) {
390                         stats->tx_dropped++;
391                         return 0;
392                 }
393
394                 if (orig_headroom < VLAN_HLEN)
395                         vlan_dev_info(dev)->cnt_inc_headroom_on_tx++;
396         }
397
398         pr_debug("%s: about to send skb: %p to dev: %s\n",
399                 __FUNCTION__, skb, skb->dev->name);
400         pr_debug("  " MAC_FMT " " MAC_FMT " %4hx %4hx %4hx\n",
401                  veth->h_dest[0], veth->h_dest[1], veth->h_dest[2],
402                  veth->h_dest[3], veth->h_dest[4], veth->h_dest[5],
403                  veth->h_source[0], veth->h_source[1], veth->h_source[2],
404                  veth->h_source[3], veth->h_source[4], veth->h_source[5],
405                  veth->h_vlan_proto, veth->h_vlan_TCI,
406                  veth->h_vlan_encapsulated_proto);
407
408         stats->tx_packets++; /* for statics only */
409         stats->tx_bytes += skb->len;
410
411         skb->dev = vlan_dev_info(dev)->real_dev;
412         dev_queue_xmit(skb);
413
414         return 0;
415 }
416
417 static int vlan_dev_hwaccel_hard_start_xmit(struct sk_buff *skb,
418                                             struct net_device *dev)
419 {
420         struct net_device_stats *stats = &dev->stats;
421         unsigned short veth_TCI;
422
423         /* Construct the second two bytes. This field looks something
424          * like:
425          * usr_priority: 3 bits  (high bits)
426          * CFI           1 bit
427          * VLAN ID       12 bits (low bits)
428          */
429         veth_TCI = vlan_dev_info(dev)->vlan_id;
430         veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
431         skb = __vlan_hwaccel_put_tag(skb, veth_TCI);
432
433         stats->tx_packets++;
434         stats->tx_bytes += skb->len;
435
436         skb->dev = vlan_dev_info(dev)->real_dev;
437         dev_queue_xmit(skb);
438
439         return 0;
440 }
441
442 static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
443 {
444         /* TODO: gotta make sure the underlying layer can handle it,
445          * maybe an IFF_VLAN_CAPABLE flag for devices?
446          */
447         if (vlan_dev_info(dev)->real_dev->mtu < new_mtu)
448                 return -ERANGE;
449
450         dev->mtu = new_mtu;
451
452         return 0;
453 }
454
455 void vlan_dev_set_ingress_priority(const struct net_device *dev,
456                                    u32 skb_prio, short vlan_prio)
457 {
458         struct vlan_dev_info *vlan = vlan_dev_info(dev);
459
460         if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio)
461                 vlan->nr_ingress_mappings--;
462         else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio)
463                 vlan->nr_ingress_mappings++;
464
465         vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
466 }
467
468 int vlan_dev_set_egress_priority(const struct net_device *dev,
469                                  u32 skb_prio, short vlan_prio)
470 {
471         struct vlan_dev_info *vlan = vlan_dev_info(dev);
472         struct vlan_priority_tci_mapping *mp = NULL;
473         struct vlan_priority_tci_mapping *np;
474         u32 vlan_qos = (vlan_prio << 13) & 0xE000;
475
476         /* See if a priority mapping exists.. */
477         mp = vlan->egress_priority_map[skb_prio & 0xF];
478         while (mp) {
479                 if (mp->priority == skb_prio) {
480                         if (mp->vlan_qos && !vlan_qos)
481                                 vlan->nr_egress_mappings--;
482                         else if (!mp->vlan_qos && vlan_qos)
483                                 vlan->nr_egress_mappings++;
484                         mp->vlan_qos = vlan_qos;
485                         return 0;
486                 }
487                 mp = mp->next;
488         }
489
490         /* Create a new mapping then. */
491         mp = vlan->egress_priority_map[skb_prio & 0xF];
492         np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL);
493         if (!np)
494                 return -ENOBUFS;
495
496         np->next = mp;
497         np->priority = skb_prio;
498         np->vlan_qos = vlan_qos;
499         vlan->egress_priority_map[skb_prio & 0xF] = np;
500         if (vlan_qos)
501                 vlan->nr_egress_mappings++;
502         return 0;
503 }
504
505 /* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */
506 int vlan_dev_set_vlan_flag(const struct net_device *dev,
507                            u32 flag, short flag_val)
508 {
509         /* verify flag is supported */
510         if (flag == VLAN_FLAG_REORDER_HDR) {
511                 if (flag_val)
512                         vlan_dev_info(dev)->flags |= VLAN_FLAG_REORDER_HDR;
513                 else
514                         vlan_dev_info(dev)->flags &= ~VLAN_FLAG_REORDER_HDR;
515                 return 0;
516         }
517         return -EINVAL;
518 }
519
520 void vlan_dev_get_realdev_name(const struct net_device *dev, char *result)
521 {
522         strncpy(result, vlan_dev_info(dev)->real_dev->name, 23);
523 }
524
525 void vlan_dev_get_vid(const struct net_device *dev, unsigned short *result)
526 {
527         *result = vlan_dev_info(dev)->vlan_id;
528 }
529
530 static int vlan_dev_open(struct net_device *dev)
531 {
532         struct vlan_dev_info *vlan = vlan_dev_info(dev);
533         struct net_device *real_dev = vlan->real_dev;
534         int err;
535
536         if (!(real_dev->flags & IFF_UP))
537                 return -ENETDOWN;
538
539         if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr)) {
540                 err = dev_unicast_add(real_dev, dev->dev_addr, ETH_ALEN);
541                 if (err < 0)
542                         return err;
543         }
544         memcpy(vlan->real_dev_addr, real_dev->dev_addr, ETH_ALEN);
545
546         if (dev->flags & IFF_ALLMULTI)
547                 dev_set_allmulti(real_dev, 1);
548         if (dev->flags & IFF_PROMISC)
549                 dev_set_promiscuity(real_dev, 1);
550
551         return 0;
552 }
553
554 static int vlan_dev_stop(struct net_device *dev)
555 {
556         struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
557
558         dev_mc_unsync(real_dev, dev);
559         if (dev->flags & IFF_ALLMULTI)
560                 dev_set_allmulti(real_dev, -1);
561         if (dev->flags & IFF_PROMISC)
562                 dev_set_promiscuity(real_dev, -1);
563
564         if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
565                 dev_unicast_delete(real_dev, dev->dev_addr, dev->addr_len);
566
567         return 0;
568 }
569
570 static int vlan_dev_set_mac_address(struct net_device *dev, void *p)
571 {
572         struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
573         struct sockaddr *addr = p;
574         int err;
575
576         if (!is_valid_ether_addr(addr->sa_data))
577                 return -EADDRNOTAVAIL;
578
579         if (!(dev->flags & IFF_UP))
580                 goto out;
581
582         if (compare_ether_addr(addr->sa_data, real_dev->dev_addr)) {
583                 err = dev_unicast_add(real_dev, addr->sa_data, ETH_ALEN);
584                 if (err < 0)
585                         return err;
586         }
587
588         if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
589                 dev_unicast_delete(real_dev, dev->dev_addr, ETH_ALEN);
590
591 out:
592         memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
593         return 0;
594 }
595
596 static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
597 {
598         struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
599         struct ifreq ifrr;
600         int err = -EOPNOTSUPP;
601
602         strncpy(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
603         ifrr.ifr_ifru = ifr->ifr_ifru;
604
605         switch (cmd) {
606         case SIOCGMIIPHY:
607         case SIOCGMIIREG:
608         case SIOCSMIIREG:
609                 if (real_dev->do_ioctl && netif_device_present(real_dev))
610                         err = real_dev->do_ioctl(real_dev, &ifrr, cmd);
611                 break;
612         }
613
614         if (!err)
615                 ifr->ifr_ifru = ifrr.ifr_ifru;
616
617         return err;
618 }
619
620 static void vlan_dev_change_rx_flags(struct net_device *dev, int change)
621 {
622         struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
623
624         if (change & IFF_ALLMULTI)
625                 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
626         if (change & IFF_PROMISC)
627                 dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1);
628 }
629
630 static void vlan_dev_set_multicast_list(struct net_device *vlan_dev)
631 {
632         dev_mc_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
633 }
634
635 /*
636  * vlan network devices have devices nesting below it, and are a special
637  * "super class" of normal network devices; split their locks off into a
638  * separate class since they always nest.
639  */
640 static struct lock_class_key vlan_netdev_xmit_lock_key;
641
642 static const struct header_ops vlan_header_ops = {
643         .create  = vlan_dev_hard_header,
644         .rebuild = vlan_dev_rebuild_header,
645         .parse   = eth_header_parse,
646 };
647
648 static int vlan_dev_init(struct net_device *dev)
649 {
650         struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
651         int subclass = 0;
652
653         /* IFF_BROADCAST|IFF_MULTICAST; ??? */
654         dev->flags  = real_dev->flags & ~IFF_UP;
655         dev->iflink = real_dev->ifindex;
656         dev->state  = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
657                                           (1<<__LINK_STATE_DORMANT))) |
658                       (1<<__LINK_STATE_PRESENT);
659
660         /* ipv6 shared card related stuff */
661         dev->dev_id = real_dev->dev_id;
662
663         if (is_zero_ether_addr(dev->dev_addr))
664                 memcpy(dev->dev_addr, real_dev->dev_addr, dev->addr_len);
665         if (is_zero_ether_addr(dev->broadcast))
666                 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
667
668         if (real_dev->features & NETIF_F_HW_VLAN_TX) {
669                 dev->header_ops      = real_dev->header_ops;
670                 dev->hard_header_len = real_dev->hard_header_len;
671                 dev->hard_start_xmit = vlan_dev_hwaccel_hard_start_xmit;
672         } else {
673                 dev->header_ops      = &vlan_header_ops;
674                 dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
675                 dev->hard_start_xmit = vlan_dev_hard_start_xmit;
676         }
677
678         if (real_dev->priv_flags & IFF_802_1Q_VLAN)
679                 subclass = 1;
680
681         lockdep_set_class_and_subclass(&dev->_xmit_lock,
682                                 &vlan_netdev_xmit_lock_key, subclass);
683         return 0;
684 }
685
686 void vlan_setup(struct net_device *dev)
687 {
688         ether_setup(dev);
689
690         dev->priv_flags         |= IFF_802_1Q_VLAN;
691         dev->tx_queue_len       = 0;
692
693         dev->change_mtu         = vlan_dev_change_mtu;
694         dev->init               = vlan_dev_init;
695         dev->open               = vlan_dev_open;
696         dev->stop               = vlan_dev_stop;
697         dev->set_mac_address    = vlan_dev_set_mac_address;
698         dev->set_multicast_list = vlan_dev_set_multicast_list;
699         dev->change_rx_flags    = vlan_dev_change_rx_flags;
700         dev->do_ioctl           = vlan_dev_ioctl;
701         dev->destructor         = free_netdev;
702
703         memset(dev->broadcast, 0, ETH_ALEN);
704 }