]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/8021q/vlan_dev.c
[VLAN]: Clean up vlan_skb_recv()
[net-next-2.6.git] / net / 8021q / vlan_dev.c
CommitLineData
1da177e4
LT
1/* -*- linux-c -*-
2 * INET 802.1Q VLAN
3 * Ethernet-type device handling.
4 *
5 * Authors: Ben Greear <greearb@candelatech.com>
ad712087 6 * Please send support related email to: netdev@vger.kernel.org
1da177e4 7 * VLAN Home Page: http://www.candelatech.com/~greear/vlan.html
122952fc 8 *
1da177e4
LT
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.
122952fc 15 *
1da177e4
LT
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 */
ef3eb3e5 50static int vlan_dev_rebuild_header(struct sk_buff *skb)
1da177e4
LT
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);
122952fc 61#endif
1da177e4 62 default:
40f98e1a
PM
63 pr_debug("%s: unable to resolve type %X addresses.\n",
64 dev->name, ntohs(veth->h_vlan_encapsulated_proto));
122952fc 65
1da177e4
LT
66 memcpy(veth->h_source, dev->dev_addr, ETH_ALEN);
67 break;
3ff50b79 68 }
1da177e4
LT
69
70 return 0;
71}
72
73static inline struct sk_buff *vlan_check_reorder_header(struct sk_buff *skb)
74{
9dfebcc6 75 if (vlan_dev_info(skb->dev)->flags & VLAN_FLAG_REORDER_HDR) {
1da177e4
LT
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);
b0e380b1 85 skb->mac_header += VLAN_HLEN;
1da177e4
LT
86 }
87 }
88
89 return skb;
90}
91
92/*
122952fc 93 * Determine the packet's protocol ID. The rule here is that we
1da177e4
LT
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...
2029cc2c
PM
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
1da177e4
LT
112 *
113 */
114int vlan_skb_recv(struct sk_buff *skb, struct net_device *dev,
2029cc2c 115 struct packet_type *ptype, struct net_device *orig_dev)
1da177e4 116{
31ffdbcb 117 unsigned char *rawp;
e7c243c9 118 struct vlan_hdr *vhdr;
1da177e4
LT
119 unsigned short vid;
120 struct net_device_stats *stats;
121 unsigned short vlan_TCI;
3c3f8f25 122 __be16 proto;
1da177e4 123
31ffdbcb
PM
124 if (dev->nd_net != &init_net)
125 goto err_free;
e730c155 126
2029cc2c
PM
127 skb = skb_share_check(skb, GFP_ATOMIC);
128 if (skb == NULL)
31ffdbcb 129 goto err_free;
e7c243c9 130
31ffdbcb
PM
131 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
132 goto err_free;
e7c243c9 133
31ffdbcb 134 vhdr = (struct vlan_hdr *)skb->data;
1da177e4 135 vlan_TCI = ntohs(vhdr->h_vlan_TCI);
1da177e4
LT
136 vid = (vlan_TCI & VLAN_VID_MASK);
137
1da177e4
LT
138 rcu_read_lock();
139 skb->dev = __find_vlan_dev(dev, vid);
140 if (!skb->dev) {
2029cc2c
PM
141 pr_debug("%s: ERROR: No net_device for VID: %u on dev: %s\n",
142 __FUNCTION__, (unsigned int)vid, dev->name);
31ffdbcb 143 goto err_unlock;
1da177e4
LT
144 }
145
146 skb->dev->last_rx = jiffies;
147
7bd38d77 148 stats = &skb->dev->stats;
1da177e4
LT
149 stats->rx_packets++;
150 stats->rx_bytes += skb->len;
151
cbb042f9 152 skb_pull_rcsum(skb, VLAN_HLEN);
a388442c 153
2029cc2c
PM
154 skb->priority = vlan_get_ingress_priority(skb->dev,
155 ntohs(vhdr->h_vlan_TCI));
1da177e4 156
40f98e1a
PM
157 pr_debug("%s: priority: %u for TCI: %hu\n",
158 __FUNCTION__, skb->priority, ntohs(vhdr->h_vlan_TCI));
1da177e4 159
1da177e4
LT
160 switch (skb->pkt_type) {
161 case PACKET_BROADCAST: /* Yeah, stats collect these together.. */
2029cc2c 162 /* stats->broadcast ++; // no such counter :-( */
1da177e4
LT
163 break;
164
165 case PACKET_MULTICAST:
166 stats->multicast++;
167 break;
168
122952fc 169 case PACKET_OTHERHOST:
1da177e4 170 /* Our lower layer thinks this is not local, let's make sure.
2029cc2c
PM
171 * This allows the VLAN to have a different MAC than the
172 * underlying device, and still route correctly.
1da177e4 173 */
2029cc2c
PM
174 if (!compare_ether_addr(eth_hdr(skb)->h_dest,
175 skb->dev->dev_addr))
1da177e4 176 skb->pkt_type = PACKET_HOST;
1da177e4
LT
177 break;
178 default:
179 break;
3ff50b79 180 }
1da177e4
LT
181
182 /* Was a VLAN packet, grab the encapsulated protocol, which the layer
183 * three protocols care about.
184 */
1da177e4 185 proto = vhdr->h_vlan_encapsulated_proto;
1da177e4 186 if (ntohs(proto) >= 1536) {
31ffdbcb
PM
187 skb->protocol = proto;
188 goto recv;
1da177e4
LT
189 }
190
1da177e4
LT
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 */
31ffdbcb 197 rawp = skb->data;
1da177e4 198 if (*(unsigned short *)rawp == 0xFFFF) {
b93b7eeb 199 skb->protocol = htons(ETH_P_802_3);
31ffdbcb 200 goto recv;
1da177e4
LT
201 }
202
203 /*
204 * Real 802.2 LLC
205 */
b93b7eeb 206 skb->protocol = htons(ETH_P_802_2);
1da177e4 207
31ffdbcb 208recv:
1da177e4 209 skb = vlan_check_reorder_header(skb);
31ffdbcb 210 if (!skb) {
1da177e4 211 stats->rx_errors++;
31ffdbcb 212 goto err_unlock;
1da177e4 213 }
31ffdbcb
PM
214
215 netif_rx(skb);
1da177e4 216 rcu_read_unlock();
31ffdbcb
PM
217 return NET_RX_SUCCESS;
218
219err_unlock:
220 rcu_read_unlock();
221err_free:
222 kfree_skb(skb);
223 return NET_RX_DROP;
1da177e4
LT
224}
225
2029cc2c
PM
226static inline unsigned short
227vlan_dev_get_egress_qos_mask(struct net_device *dev, struct sk_buff *skb)
1da177e4 228{
2029cc2c 229 struct vlan_priority_tci_mapping *mp;
1da177e4 230
2029cc2c 231 mp = vlan_dev_info(dev)->egress_priority_map[(skb->priority & 0xF)];
1da177e4
LT
232 while (mp) {
233 if (mp->priority == skb->priority) {
2029cc2c
PM
234 return mp->vlan_qos; /* This should already be shifted
235 * to mask correctly with the
236 * VLAN's TCI */
1da177e4
LT
237 }
238 mp = mp->next;
239 }
240 return 0;
241}
242
243/*
122952fc 244 * Create the VLAN header for an arbitrary protocol layer
1da177e4
LT
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 */
ef3eb3e5
PM
252static 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)
1da177e4
LT
256{
257 struct vlan_hdr *vhdr;
258 unsigned short veth_TCI = 0;
259 int rc = 0;
260 int build_vlan_header = 0;
2029cc2c 261 struct net_device *vdev = dev;
1da177e4 262
40f98e1a 263 pr_debug("%s: skb: %p type: %hx len: %u vlan_id: %hx, daddr: %p\n",
2029cc2c
PM
264 __FUNCTION__, skb, type, len, vlan_dev_info(dev)->vlan_id,
265 daddr);
1da177e4
LT
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
122952fc 271 * REORDER_HEADER flag is set) The drawback to this is some extra
1da177e4
LT
272 * header shuffling in the hard_start_xmit. Users can turn off this
273 * REORDER behaviour with the vconfig tool.
274 */
9dfebcc6 275 if (!(vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR))
a4bf3af4 276 build_vlan_header = 1;
1da177e4
LT
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
2029cc2c
PM
283 /* Now, construct the second two bytes. This field looks
284 * something like:
1da177e4
LT
285 * usr_priority: 3 bits (high bits)
286 * CFI 1 bit
287 * VLAN ID 12 bits (low bits)
288 *
289 */
9dfebcc6 290 veth_TCI = vlan_dev_info(dev)->vlan_id;
1da177e4
LT
291 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
292
293 vhdr->h_vlan_TCI = htons(veth_TCI);
294
295 /*
2029cc2c
PM
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.
1da177e4
LT
299 */
300
2029cc2c 301 if (type != ETH_P_802_3)
1da177e4 302 vhdr->h_vlan_encapsulated_proto = htons(type);
2029cc2c 303 else
1da177e4 304 vhdr->h_vlan_encapsulated_proto = htons(len);
279e172a
JB
305
306 skb->protocol = htons(ETH_P_8021Q);
be8bd863 307 skb_reset_network_header(skb);
1da177e4
LT
308 }
309
310 /* Before delegating work to the lower layer, enter our MAC-address */
311 if (saddr == NULL)
312 saddr = dev->dev_addr;
313
9dfebcc6 314 dev = vlan_dev_info(dev)->real_dev;
1da177e4 315
2029cc2c
PM
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...
1da177e4
LT
320 */
321
2029cc2c
PM
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
1da177e4
LT
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) {
7bd38d77 331 struct net_device_stats *stats = &vdev->stats;
1da177e4
LT
332 stats->tx_dropped++;
333 return -ENOMEM;
334 }
9dfebcc6 335 vlan_dev_info(vdev)->cnt_inc_headroom_on_tx++;
2029cc2c 336 pr_debug("%s: %s: had to grow skb\n", __FUNCTION__, vdev->name);
1da177e4
LT
337 }
338
339 if (build_vlan_header) {
340 /* Now make the underlying real hard header */
0c4e8581
SH
341 rc = dev_hard_header(skb, dev, ETH_P_8021Q, daddr, saddr,
342 len + VLAN_HLEN);
343 if (rc > 0)
1da177e4 344 rc += VLAN_HLEN;
0c4e8581 345 else if (rc < 0)
1da177e4 346 rc -= VLAN_HLEN;
0c4e8581 347 } else
2029cc2c
PM
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.
1da177e4 352 */
0c4e8581 353 rc = dev_hard_header(skb, dev, type, daddr, saddr, len);
1da177e4
LT
354
355 return rc;
356}
357
ef3eb3e5 358static int vlan_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1da177e4 359{
7bd38d77 360 struct net_device_stats *stats = &dev->stats;
1da177e4
LT
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
6ab3b487 369 if (veth->h_vlan_proto != htons(ETH_P_8021Q) ||
9dfebcc6 370 vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR) {
1da177e4
LT
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! */
9dfebcc6 375 vlan_dev_info(dev)->cnt_encap_on_xmit++;
1da177e4 376
40f98e1a
PM
377 pr_debug("%s: proto to encap: 0x%hx\n",
378 __FUNCTION__, htons(veth->h_vlan_proto));
1da177e4
LT
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 */
9dfebcc6 385 veth_TCI = vlan_dev_info(dev)->vlan_id;
1da177e4
LT
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
2029cc2c 394 if (orig_headroom < VLAN_HLEN)
9dfebcc6 395 vlan_dev_info(dev)->cnt_inc_headroom_on_tx++;
1da177e4
LT
396 }
397
40f98e1a 398 pr_debug("%s: about to send skb: %p to dev: %s\n",
1da177e4 399 __FUNCTION__, skb, skb->dev->name);
40f98e1a
PM
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);
1da177e4
LT
407
408 stats->tx_packets++; /* for statics only */
409 stats->tx_bytes += skb->len;
410
9dfebcc6 411 skb->dev = vlan_dev_info(dev)->real_dev;
1da177e4
LT
412 dev_queue_xmit(skb);
413
414 return 0;
415}
416
ef3eb3e5
PM
417static int vlan_dev_hwaccel_hard_start_xmit(struct sk_buff *skb,
418 struct net_device *dev)
1da177e4 419{
7bd38d77 420 struct net_device_stats *stats = &dev->stats;
1da177e4
LT
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 */
9dfebcc6 429 veth_TCI = vlan_dev_info(dev)->vlan_id;
1da177e4
LT
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
9dfebcc6 436 skb->dev = vlan_dev_info(dev)->real_dev;
1da177e4
LT
437 dev_queue_xmit(skb);
438
439 return 0;
440}
441
ef3eb3e5 442static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
1da177e4
LT
443{
444 /* TODO: gotta make sure the underlying layer can handle it,
445 * maybe an IFF_VLAN_CAPABLE flag for devices?
446 */
9dfebcc6 447 if (vlan_dev_info(dev)->real_dev->mtu < new_mtu)
1da177e4
LT
448 return -ERANGE;
449
450 dev->mtu = new_mtu;
451
452 return 0;
453}
454
c17d8874
PM
455void vlan_dev_set_ingress_priority(const struct net_device *dev,
456 u32 skb_prio, short vlan_prio)
1da177e4 457{
9dfebcc6 458 struct vlan_dev_info *vlan = vlan_dev_info(dev);
b020cb48
PM
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;
1da177e4
LT
466}
467
c17d8874
PM
468int vlan_dev_set_egress_priority(const struct net_device *dev,
469 u32 skb_prio, short vlan_prio)
1da177e4 470{
9dfebcc6 471 struct vlan_dev_info *vlan = vlan_dev_info(dev);
1da177e4
LT
472 struct vlan_priority_tci_mapping *mp = NULL;
473 struct vlan_priority_tci_mapping *np;
b020cb48 474 u32 vlan_qos = (vlan_prio << 13) & 0xE000;
122952fc 475
c17d8874 476 /* See if a priority mapping exists.. */
b020cb48 477 mp = vlan->egress_priority_map[skb_prio & 0xF];
c17d8874
PM
478 while (mp) {
479 if (mp->priority == skb_prio) {
b020cb48
PM
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;
c17d8874 485 return 0;
1da177e4 486 }
c17d8874 487 mp = mp->next;
1da177e4 488 }
c17d8874
PM
489
490 /* Create a new mapping then. */
b020cb48 491 mp = vlan->egress_priority_map[skb_prio & 0xF];
c17d8874
PM
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;
b020cb48
PM
498 np->vlan_qos = vlan_qos;
499 vlan->egress_priority_map[skb_prio & 0xF] = np;
500 if (vlan_qos)
501 vlan->nr_egress_mappings++;
c17d8874 502 return 0;
1da177e4
LT
503}
504
a4bf3af4 505/* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */
c17d8874
PM
506int vlan_dev_set_vlan_flag(const struct net_device *dev,
507 u32 flag, short flag_val)
1da177e4 508{
c17d8874 509 /* verify flag is supported */
a4bf3af4 510 if (flag == VLAN_FLAG_REORDER_HDR) {
2029cc2c 511 if (flag_val)
9dfebcc6 512 vlan_dev_info(dev)->flags |= VLAN_FLAG_REORDER_HDR;
2029cc2c 513 else
9dfebcc6 514 vlan_dev_info(dev)->flags &= ~VLAN_FLAG_REORDER_HDR;
c17d8874 515 return 0;
1da177e4 516 }
1da177e4
LT
517 return -EINVAL;
518}
519
c17d8874 520void vlan_dev_get_realdev_name(const struct net_device *dev, char *result)
1da177e4 521{
9dfebcc6 522 strncpy(result, vlan_dev_info(dev)->real_dev->name, 23);
1da177e4
LT
523}
524
c17d8874 525void vlan_dev_get_vid(const struct net_device *dev, unsigned short *result)
1da177e4 526{
9dfebcc6 527 *result = vlan_dev_info(dev)->vlan_id;
1da177e4
LT
528}
529
ef3eb3e5 530static int vlan_dev_open(struct net_device *dev)
1da177e4 531{
9dfebcc6 532 struct vlan_dev_info *vlan = vlan_dev_info(dev);
8c979c26
PM
533 struct net_device *real_dev = vlan->real_dev;
534 int err;
535
536 if (!(real_dev->flags & IFF_UP))
1da177e4
LT
537 return -ENETDOWN;
538
8c979c26
PM
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
6c78dcbd
PM
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
1da177e4
LT
551 return 0;
552}
553
ef3eb3e5 554static int vlan_dev_stop(struct net_device *dev)
1da177e4 555{
9dfebcc6 556 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
8c979c26 557
56addd6e 558 dev_mc_unsync(real_dev, dev);
6c78dcbd
PM
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
8c979c26
PM
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
1da177e4
LT
567 return 0;
568}
569
ef3eb3e5 570static int vlan_dev_set_mac_address(struct net_device *dev, void *p)
39aaac11 571{
9dfebcc6 572 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
39aaac11
PM
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
591out:
592 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
593 return 0;
594}
595
ef3eb3e5 596static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1da177e4 597{
9dfebcc6 598 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
1da177e4
LT
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
2029cc2c 605 switch (cmd) {
1da177e4
LT
606 case SIOCGMIIPHY:
607 case SIOCGMIIREG:
608 case SIOCSMIIREG:
122952fc 609 if (real_dev->do_ioctl && netif_device_present(real_dev))
1da177e4
LT
610 err = real_dev->do_ioctl(real_dev, &ifrr, cmd);
611 break;
1da177e4
LT
612 }
613
122952fc 614 if (!err)
1da177e4
LT
615 ifr->ifr_ifru = ifrr.ifr_ifru;
616
617 return err;
618}
619
ef3eb3e5 620static void vlan_dev_change_rx_flags(struct net_device *dev, int change)
6c78dcbd 621{
9dfebcc6 622 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
6c78dcbd
PM
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
ef3eb3e5 630static void vlan_dev_set_multicast_list(struct net_device *vlan_dev)
1da177e4 631{
9dfebcc6 632 dev_mc_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
1da177e4 633}
ef3eb3e5
PM
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 */
640static struct lock_class_key vlan_netdev_xmit_lock_key;
641
642static 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
648static int vlan_dev_init(struct net_device *dev)
649{
9dfebcc6 650 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
ef3eb3e5
PM
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
686void 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}