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