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