vlan: Use is_vlan_dev()
[linux-2.6-block.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
91b4f954
PE
92static 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
1da177e4 126/*
122952fc 127 * Determine the packet's protocol ID. The rule here is that we
1da177e4
LT
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...
2029cc2c
PM
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
1da177e4
LT
146 *
147 */
148int vlan_skb_recv(struct sk_buff *skb, struct net_device *dev,
2029cc2c 149 struct packet_type *ptype, struct net_device *orig_dev)
1da177e4 150{
e7c243c9 151 struct vlan_hdr *vhdr;
1da177e4
LT
152 unsigned short vid;
153 struct net_device_stats *stats;
154 unsigned short vlan_TCI;
1da177e4 155
2029cc2c
PM
156 skb = skb_share_check(skb, GFP_ATOMIC);
157 if (skb == NULL)
31ffdbcb 158 goto err_free;
e7c243c9 159
31ffdbcb
PM
160 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
161 goto err_free;
e7c243c9 162
31ffdbcb 163 vhdr = (struct vlan_hdr *)skb->data;
1da177e4 164 vlan_TCI = ntohs(vhdr->h_vlan_TCI);
1da177e4
LT
165 vid = (vlan_TCI & VLAN_VID_MASK);
166
1da177e4
LT
167 rcu_read_lock();
168 skb->dev = __find_vlan_dev(dev, vid);
169 if (!skb->dev) {
2029cc2c 170 pr_debug("%s: ERROR: No net_device for VID: %u on dev: %s\n",
0dc47877 171 __func__, (unsigned int)vid, dev->name);
31ffdbcb 172 goto err_unlock;
1da177e4
LT
173 }
174
175 skb->dev->last_rx = jiffies;
176
7bd38d77 177 stats = &skb->dev->stats;
1da177e4
LT
178 stats->rx_packets++;
179 stats->rx_bytes += skb->len;
180
cbb042f9 181 skb_pull_rcsum(skb, VLAN_HLEN);
a388442c 182
2029cc2c
PM
183 skb->priority = vlan_get_ingress_priority(skb->dev,
184 ntohs(vhdr->h_vlan_TCI));
1da177e4 185
40f98e1a 186 pr_debug("%s: priority: %u for TCI: %hu\n",
0dc47877 187 __func__, skb->priority, ntohs(vhdr->h_vlan_TCI));
1da177e4 188
1da177e4
LT
189 switch (skb->pkt_type) {
190 case PACKET_BROADCAST: /* Yeah, stats collect these together.. */
2029cc2c 191 /* stats->broadcast ++; // no such counter :-( */
1da177e4
LT
192 break;
193
194 case PACKET_MULTICAST:
195 stats->multicast++;
196 break;
197
122952fc 198 case PACKET_OTHERHOST:
1da177e4 199 /* Our lower layer thinks this is not local, let's make sure.
2029cc2c
PM
200 * This allows the VLAN to have a different MAC than the
201 * underlying device, and still route correctly.
1da177e4 202 */
2029cc2c
PM
203 if (!compare_ether_addr(eth_hdr(skb)->h_dest,
204 skb->dev->dev_addr))
1da177e4 205 skb->pkt_type = PACKET_HOST;
1da177e4
LT
206 break;
207 default:
208 break;
3ff50b79 209 }
1da177e4 210
91b4f954 211 vlan_set_encap_proto(skb, vhdr);
1da177e4 212
1da177e4 213 skb = vlan_check_reorder_header(skb);
31ffdbcb 214 if (!skb) {
1da177e4 215 stats->rx_errors++;
31ffdbcb 216 goto err_unlock;
1da177e4 217 }
31ffdbcb
PM
218
219 netif_rx(skb);
1da177e4 220 rcu_read_unlock();
31ffdbcb
PM
221 return NET_RX_SUCCESS;
222
223err_unlock:
224 rcu_read_unlock();
225err_free:
226 kfree_skb(skb);
227 return NET_RX_DROP;
1da177e4
LT
228}
229
2029cc2c
PM
230static inline unsigned short
231vlan_dev_get_egress_qos_mask(struct net_device *dev, struct sk_buff *skb)
1da177e4 232{
2029cc2c 233 struct vlan_priority_tci_mapping *mp;
1da177e4 234
2029cc2c 235 mp = vlan_dev_info(dev)->egress_priority_map[(skb->priority & 0xF)];
1da177e4
LT
236 while (mp) {
237 if (mp->priority == skb->priority) {
2029cc2c
PM
238 return mp->vlan_qos; /* This should already be shifted
239 * to mask correctly with the
240 * VLAN's TCI */
1da177e4
LT
241 }
242 mp = mp->next;
243 }
244 return 0;
245}
246
247/*
122952fc 248 * Create the VLAN header for an arbitrary protocol layer
1da177e4
LT
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 */
ef3eb3e5
PM
256static 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)
1da177e4
LT
260{
261 struct vlan_hdr *vhdr;
262 unsigned short veth_TCI = 0;
263 int rc = 0;
264 int build_vlan_header = 0;
2029cc2c 265 struct net_device *vdev = dev;
1da177e4 266
40f98e1a 267 pr_debug("%s: skb: %p type: %hx len: %u vlan_id: %hx, daddr: %p\n",
0dc47877 268 __func__, skb, type, len, vlan_dev_info(dev)->vlan_id,
2029cc2c 269 daddr);
1da177e4
LT
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
122952fc 275 * REORDER_HEADER flag is set) The drawback to this is some extra
1da177e4
LT
276 * header shuffling in the hard_start_xmit. Users can turn off this
277 * REORDER behaviour with the vconfig tool.
278 */
9dfebcc6 279 if (!(vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR))
a4bf3af4 280 build_vlan_header = 1;
1da177e4
LT
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
2029cc2c
PM
287 /* Now, construct the second two bytes. This field looks
288 * something like:
1da177e4
LT
289 * usr_priority: 3 bits (high bits)
290 * CFI 1 bit
291 * VLAN ID 12 bits (low bits)
292 *
293 */
9dfebcc6 294 veth_TCI = vlan_dev_info(dev)->vlan_id;
1da177e4
LT
295 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
296
297 vhdr->h_vlan_TCI = htons(veth_TCI);
298
299 /*
2029cc2c
PM
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.
1da177e4
LT
303 */
304
2029cc2c 305 if (type != ETH_P_802_3)
1da177e4 306 vhdr->h_vlan_encapsulated_proto = htons(type);
2029cc2c 307 else
1da177e4 308 vhdr->h_vlan_encapsulated_proto = htons(len);
279e172a
JB
309
310 skb->protocol = htons(ETH_P_8021Q);
1da177e4
LT
311 }
312
313 /* Before delegating work to the lower layer, enter our MAC-address */
314 if (saddr == NULL)
315 saddr = dev->dev_addr;
316
9dfebcc6 317 dev = vlan_dev_info(dev)->real_dev;
1da177e4 318
2029cc2c
PM
319 /* MPLS can send us skbuffs w/out enough space. This check will grow
320 * the skb if it doesn't have enough headroom. Not a beautiful solution,
321 * so I'll tick a counter so that users can know it's happening...
322 * If they care...
1da177e4
LT
323 */
324
2029cc2c
PM
325 /* NOTE: This may still break if the underlying device is not the final
326 * device (and thus there are more headers to add...) It should work for
1da177e4
LT
327 * good-ole-ethernet though.
328 */
329 if (skb_headroom(skb) < dev->hard_header_len) {
330 struct sk_buff *sk_tmp = skb;
331 skb = skb_realloc_headroom(sk_tmp, dev->hard_header_len);
332 kfree_skb(sk_tmp);
333 if (skb == NULL) {
7bd38d77 334 struct net_device_stats *stats = &vdev->stats;
1da177e4
LT
335 stats->tx_dropped++;
336 return -ENOMEM;
337 }
9dfebcc6 338 vlan_dev_info(vdev)->cnt_inc_headroom_on_tx++;
0dc47877 339 pr_debug("%s: %s: had to grow skb\n", __func__, vdev->name);
1da177e4
LT
340 }
341
342 if (build_vlan_header) {
343 /* Now make the underlying real hard header */
0c4e8581
SH
344 rc = dev_hard_header(skb, dev, ETH_P_8021Q, daddr, saddr,
345 len + VLAN_HLEN);
346 if (rc > 0)
1da177e4 347 rc += VLAN_HLEN;
0c4e8581 348 else if (rc < 0)
1da177e4 349 rc -= VLAN_HLEN;
0c4e8581 350 } else
2029cc2c
PM
351 /* If here, then we'll just make a normal looking ethernet
352 * frame, but, the hard_start_xmit method will insert the tag
353 * (it has to be able to do this for bridged and other skbs
354 * that don't come down the protocol stack in an orderly manner.
1da177e4 355 */
0c4e8581 356 rc = dev_hard_header(skb, dev, type, daddr, saddr, len);
1da177e4
LT
357
358 return rc;
359}
360
ef3eb3e5 361static int vlan_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1da177e4 362{
7bd38d77 363 struct net_device_stats *stats = &dev->stats;
1da177e4 364 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
55b01e86 365
1da177e4
LT
366 /* Handle non-VLAN frames if they are sent to us, for example by DHCP.
367 *
368 * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
369 * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
370 */
371
6ab3b487 372 if (veth->h_vlan_proto != htons(ETH_P_8021Q) ||
9dfebcc6 373 vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR) {
1da177e4
LT
374 int orig_headroom = skb_headroom(skb);
375 unsigned short veth_TCI;
376
377 /* This is not a VLAN frame...but we can fix that! */
9dfebcc6 378 vlan_dev_info(dev)->cnt_encap_on_xmit++;
1da177e4 379
40f98e1a 380 pr_debug("%s: proto to encap: 0x%hx\n",
577f99c1 381 __func__, ntohs(veth->h_vlan_proto));
1da177e4
LT
382 /* Construct the second two bytes. This field looks something
383 * like:
384 * usr_priority: 3 bits (high bits)
385 * CFI 1 bit
386 * VLAN ID 12 bits (low bits)
387 */
9dfebcc6 388 veth_TCI = vlan_dev_info(dev)->vlan_id;
1da177e4
LT
389 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
390
391 skb = __vlan_put_tag(skb, veth_TCI);
392 if (!skb) {
393 stats->tx_dropped++;
394 return 0;
395 }
396
2029cc2c 397 if (orig_headroom < VLAN_HLEN)
9dfebcc6 398 vlan_dev_info(dev)->cnt_inc_headroom_on_tx++;
1da177e4
LT
399 }
400
40f98e1a 401 pr_debug("%s: about to send skb: %p to dev: %s\n",
0dc47877 402 __func__, skb, skb->dev->name);
55b01e86
DM
403 pr_debug(" " MAC_FMT " " MAC_FMT " %4hx %4hx %4hx\n",
404 veth->h_dest[0], veth->h_dest[1], veth->h_dest[2],
405 veth->h_dest[3], veth->h_dest[4], veth->h_dest[5],
406 veth->h_source[0], veth->h_source[1], veth->h_source[2],
407 veth->h_source[3], veth->h_source[4], veth->h_source[5],
40f98e1a
PM
408 veth->h_vlan_proto, veth->h_vlan_TCI,
409 veth->h_vlan_encapsulated_proto);
1da177e4
LT
410
411 stats->tx_packets++; /* for statics only */
412 stats->tx_bytes += skb->len;
413
9dfebcc6 414 skb->dev = vlan_dev_info(dev)->real_dev;
1da177e4
LT
415 dev_queue_xmit(skb);
416
417 return 0;
418}
419
ef3eb3e5
PM
420static int vlan_dev_hwaccel_hard_start_xmit(struct sk_buff *skb,
421 struct net_device *dev)
1da177e4 422{
7bd38d77 423 struct net_device_stats *stats = &dev->stats;
1da177e4
LT
424 unsigned short veth_TCI;
425
426 /* Construct the second two bytes. This field looks something
427 * like:
428 * usr_priority: 3 bits (high bits)
429 * CFI 1 bit
430 * VLAN ID 12 bits (low bits)
431 */
9dfebcc6 432 veth_TCI = vlan_dev_info(dev)->vlan_id;
1da177e4
LT
433 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
434 skb = __vlan_hwaccel_put_tag(skb, veth_TCI);
435
436 stats->tx_packets++;
437 stats->tx_bytes += skb->len;
438
9dfebcc6 439 skb->dev = vlan_dev_info(dev)->real_dev;
1da177e4
LT
440 dev_queue_xmit(skb);
441
442 return 0;
443}
444
ef3eb3e5 445static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
1da177e4
LT
446{
447 /* TODO: gotta make sure the underlying layer can handle it,
448 * maybe an IFF_VLAN_CAPABLE flag for devices?
449 */
9dfebcc6 450 if (vlan_dev_info(dev)->real_dev->mtu < new_mtu)
1da177e4
LT
451 return -ERANGE;
452
453 dev->mtu = new_mtu;
454
455 return 0;
456}
457
c17d8874
PM
458void vlan_dev_set_ingress_priority(const struct net_device *dev,
459 u32 skb_prio, short vlan_prio)
1da177e4 460{
9dfebcc6 461 struct vlan_dev_info *vlan = vlan_dev_info(dev);
b020cb48
PM
462
463 if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio)
464 vlan->nr_ingress_mappings--;
465 else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio)
466 vlan->nr_ingress_mappings++;
467
468 vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
1da177e4
LT
469}
470
c17d8874
PM
471int vlan_dev_set_egress_priority(const struct net_device *dev,
472 u32 skb_prio, short vlan_prio)
1da177e4 473{
9dfebcc6 474 struct vlan_dev_info *vlan = vlan_dev_info(dev);
1da177e4
LT
475 struct vlan_priority_tci_mapping *mp = NULL;
476 struct vlan_priority_tci_mapping *np;
b020cb48 477 u32 vlan_qos = (vlan_prio << 13) & 0xE000;
122952fc 478
c17d8874 479 /* See if a priority mapping exists.. */
b020cb48 480 mp = vlan->egress_priority_map[skb_prio & 0xF];
c17d8874
PM
481 while (mp) {
482 if (mp->priority == skb_prio) {
b020cb48
PM
483 if (mp->vlan_qos && !vlan_qos)
484 vlan->nr_egress_mappings--;
485 else if (!mp->vlan_qos && vlan_qos)
486 vlan->nr_egress_mappings++;
487 mp->vlan_qos = vlan_qos;
c17d8874 488 return 0;
1da177e4 489 }
c17d8874 490 mp = mp->next;
1da177e4 491 }
c17d8874
PM
492
493 /* Create a new mapping then. */
b020cb48 494 mp = vlan->egress_priority_map[skb_prio & 0xF];
c17d8874
PM
495 np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL);
496 if (!np)
497 return -ENOBUFS;
498
499 np->next = mp;
500 np->priority = skb_prio;
b020cb48
PM
501 np->vlan_qos = vlan_qos;
502 vlan->egress_priority_map[skb_prio & 0xF] = np;
503 if (vlan_qos)
504 vlan->nr_egress_mappings++;
c17d8874 505 return 0;
1da177e4
LT
506}
507
a4bf3af4 508/* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */
b3ce0325 509int vlan_dev_change_flags(const struct net_device *dev, u32 flags, u32 mask)
1da177e4 510{
b3ce0325
PM
511 struct vlan_dev_info *vlan = vlan_dev_info(dev);
512 u32 old_flags = vlan->flags;
513
70c03b49 514 if (mask & ~(VLAN_FLAG_REORDER_HDR | VLAN_FLAG_GVRP))
b3ce0325
PM
515 return -EINVAL;
516
517 vlan->flags = (old_flags & ~mask) | (flags & mask);
70c03b49
PM
518
519 if (netif_running(dev) && (vlan->flags ^ old_flags) & VLAN_FLAG_GVRP) {
520 if (vlan->flags & VLAN_FLAG_GVRP)
521 vlan_gvrp_request_join(dev);
522 else
523 vlan_gvrp_request_leave(dev);
524 }
b3ce0325 525 return 0;
1da177e4
LT
526}
527
c17d8874 528void vlan_dev_get_realdev_name(const struct net_device *dev, char *result)
1da177e4 529{
9dfebcc6 530 strncpy(result, vlan_dev_info(dev)->real_dev->name, 23);
1da177e4
LT
531}
532
c17d8874 533void vlan_dev_get_vid(const struct net_device *dev, unsigned short *result)
1da177e4 534{
9dfebcc6 535 *result = vlan_dev_info(dev)->vlan_id;
1da177e4
LT
536}
537
ef3eb3e5 538static int vlan_dev_open(struct net_device *dev)
1da177e4 539{
9dfebcc6 540 struct vlan_dev_info *vlan = vlan_dev_info(dev);
8c979c26
PM
541 struct net_device *real_dev = vlan->real_dev;
542 int err;
543
544 if (!(real_dev->flags & IFF_UP))
1da177e4
LT
545 return -ENETDOWN;
546
8c979c26
PM
547 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr)) {
548 err = dev_unicast_add(real_dev, dev->dev_addr, ETH_ALEN);
549 if (err < 0)
550 return err;
551 }
552 memcpy(vlan->real_dev_addr, real_dev->dev_addr, ETH_ALEN);
553
6c78dcbd
PM
554 if (dev->flags & IFF_ALLMULTI)
555 dev_set_allmulti(real_dev, 1);
556 if (dev->flags & IFF_PROMISC)
557 dev_set_promiscuity(real_dev, 1);
558
70c03b49
PM
559 if (vlan->flags & VLAN_FLAG_GVRP)
560 vlan_gvrp_request_join(dev);
561
1da177e4
LT
562 return 0;
563}
564
ef3eb3e5 565static int vlan_dev_stop(struct net_device *dev)
1da177e4 566{
70c03b49
PM
567 struct vlan_dev_info *vlan = vlan_dev_info(dev);
568 struct net_device *real_dev = vlan->real_dev;
569
570 if (vlan->flags & VLAN_FLAG_GVRP)
571 vlan_gvrp_request_leave(dev);
8c979c26 572
56addd6e 573 dev_mc_unsync(real_dev, dev);
e83a2ea8 574 dev_unicast_unsync(real_dev, dev);
6c78dcbd
PM
575 if (dev->flags & IFF_ALLMULTI)
576 dev_set_allmulti(real_dev, -1);
577 if (dev->flags & IFF_PROMISC)
578 dev_set_promiscuity(real_dev, -1);
579
8c979c26
PM
580 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
581 dev_unicast_delete(real_dev, dev->dev_addr, dev->addr_len);
582
1da177e4
LT
583 return 0;
584}
585
ef3eb3e5 586static int vlan_dev_set_mac_address(struct net_device *dev, void *p)
39aaac11 587{
9dfebcc6 588 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
39aaac11
PM
589 struct sockaddr *addr = p;
590 int err;
591
592 if (!is_valid_ether_addr(addr->sa_data))
593 return -EADDRNOTAVAIL;
594
595 if (!(dev->flags & IFF_UP))
596 goto out;
597
598 if (compare_ether_addr(addr->sa_data, real_dev->dev_addr)) {
599 err = dev_unicast_add(real_dev, addr->sa_data, ETH_ALEN);
600 if (err < 0)
601 return err;
602 }
603
604 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
605 dev_unicast_delete(real_dev, dev->dev_addr, ETH_ALEN);
606
607out:
608 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
609 return 0;
610}
611
ef3eb3e5 612static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1da177e4 613{
9dfebcc6 614 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
1da177e4
LT
615 struct ifreq ifrr;
616 int err = -EOPNOTSUPP;
617
618 strncpy(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
619 ifrr.ifr_ifru = ifr->ifr_ifru;
620
2029cc2c 621 switch (cmd) {
1da177e4
LT
622 case SIOCGMIIPHY:
623 case SIOCGMIIREG:
624 case SIOCSMIIREG:
122952fc 625 if (real_dev->do_ioctl && netif_device_present(real_dev))
1da177e4
LT
626 err = real_dev->do_ioctl(real_dev, &ifrr, cmd);
627 break;
1da177e4
LT
628 }
629
122952fc 630 if (!err)
1da177e4
LT
631 ifr->ifr_ifru = ifrr.ifr_ifru;
632
633 return err;
634}
635
ef3eb3e5 636static void vlan_dev_change_rx_flags(struct net_device *dev, int change)
6c78dcbd 637{
9dfebcc6 638 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
6c78dcbd
PM
639
640 if (change & IFF_ALLMULTI)
641 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
642 if (change & IFF_PROMISC)
643 dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1);
644}
645
e83a2ea8 646static void vlan_dev_set_rx_mode(struct net_device *vlan_dev)
1da177e4 647{
9dfebcc6 648 dev_mc_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
e83a2ea8 649 dev_unicast_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
1da177e4 650}
ef3eb3e5
PM
651
652/*
653 * vlan network devices have devices nesting below it, and are a special
654 * "super class" of normal network devices; split their locks off into a
655 * separate class since they always nest.
656 */
657static struct lock_class_key vlan_netdev_xmit_lock_key;
658
659static const struct header_ops vlan_header_ops = {
660 .create = vlan_dev_hard_header,
661 .rebuild = vlan_dev_rebuild_header,
662 .parse = eth_header_parse,
663};
664
665static int vlan_dev_init(struct net_device *dev)
666{
9dfebcc6 667 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
ef3eb3e5
PM
668 int subclass = 0;
669
670 /* IFF_BROADCAST|IFF_MULTICAST; ??? */
0ed21b32 671 dev->flags = real_dev->flags & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI);
ef3eb3e5
PM
672 dev->iflink = real_dev->ifindex;
673 dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
674 (1<<__LINK_STATE_DORMANT))) |
675 (1<<__LINK_STATE_PRESENT);
676
289c79a4 677 dev->features |= real_dev->features & real_dev->vlan_features;
5fb13570 678
ef3eb3e5
PM
679 /* ipv6 shared card related stuff */
680 dev->dev_id = real_dev->dev_id;
681
682 if (is_zero_ether_addr(dev->dev_addr))
683 memcpy(dev->dev_addr, real_dev->dev_addr, dev->addr_len);
684 if (is_zero_ether_addr(dev->broadcast))
685 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
686
687 if (real_dev->features & NETIF_F_HW_VLAN_TX) {
688 dev->header_ops = real_dev->header_ops;
689 dev->hard_header_len = real_dev->hard_header_len;
690 dev->hard_start_xmit = vlan_dev_hwaccel_hard_start_xmit;
691 } else {
692 dev->header_ops = &vlan_header_ops;
693 dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
694 dev->hard_start_xmit = vlan_dev_hard_start_xmit;
695 }
696
26a25239 697 if (is_vlan_dev(real_dev))
ef3eb3e5
PM
698 subclass = 1;
699
700 lockdep_set_class_and_subclass(&dev->_xmit_lock,
701 &vlan_netdev_xmit_lock_key, subclass);
702 return 0;
703}
704
23556323
PE
705static void vlan_dev_uninit(struct net_device *dev)
706{
707 struct vlan_priority_tci_mapping *pm;
708 struct vlan_dev_info *vlan = vlan_dev_info(dev);
709 int i;
710
711 for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
712 while ((pm = vlan->egress_priority_map[i]) != NULL) {
713 vlan->egress_priority_map[i] = pm->next;
714 kfree(pm);
715 }
716 }
717}
718
ef3eb3e5
PM
719void vlan_setup(struct net_device *dev)
720{
721 ether_setup(dev);
722
723 dev->priv_flags |= IFF_802_1Q_VLAN;
724 dev->tx_queue_len = 0;
725
726 dev->change_mtu = vlan_dev_change_mtu;
727 dev->init = vlan_dev_init;
23556323 728 dev->uninit = vlan_dev_uninit;
ef3eb3e5
PM
729 dev->open = vlan_dev_open;
730 dev->stop = vlan_dev_stop;
731 dev->set_mac_address = vlan_dev_set_mac_address;
e83a2ea8
CL
732 dev->set_rx_mode = vlan_dev_set_rx_mode;
733 dev->set_multicast_list = vlan_dev_set_rx_mode;
ef3eb3e5
PM
734 dev->change_rx_flags = vlan_dev_change_rx_flags;
735 dev->do_ioctl = vlan_dev_ioctl;
736 dev->destructor = free_netdev;
737
738 memset(dev->broadcast, 0, ETH_ALEN);
739}