]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/8021q/vlan_dev.c
vlan: remove useless struct hlist_node declaration from if_vlan.h
[mirror_ubuntu-artful-kernel.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>
75b8846a 31#include <linux/ethtool.h>
1da177e4
LT
32#include <net/datalink.h>
33#include <net/p8022.h>
34#include <net/arp.h>
35
36#include "vlan.h"
37#include "vlanproc.h"
38#include <linux/if_vlan.h>
39#include <net/ip.h>
40
41/*
42 * Rebuild the Ethernet MAC header. This is called after an ARP
43 * (or in future other address resolution) has completed on this
44 * sk_buff. We now let ARP fill in the other fields.
45 *
46 * This routine CANNOT use cached dst->neigh!
47 * Really, it is used only when dst->neigh is wrong.
48 *
49 * TODO: This needs a checkup, I'm ignorant here. --BLG
50 */
ef3eb3e5 51static int vlan_dev_rebuild_header(struct sk_buff *skb)
1da177e4
LT
52{
53 struct net_device *dev = skb->dev;
54 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
55
56 switch (veth->h_vlan_encapsulated_proto) {
57#ifdef CONFIG_INET
58 case __constant_htons(ETH_P_IP):
59
60 /* TODO: Confirm this will work with VLAN headers... */
61 return arp_find(veth->h_dest, skb);
122952fc 62#endif
1da177e4 63 default:
40f98e1a
PM
64 pr_debug("%s: unable to resolve type %X addresses.\n",
65 dev->name, ntohs(veth->h_vlan_encapsulated_proto));
122952fc 66
1da177e4
LT
67 memcpy(veth->h_source, dev->dev_addr, ETH_ALEN);
68 break;
3ff50b79 69 }
1da177e4
LT
70
71 return 0;
72}
73
74static inline struct sk_buff *vlan_check_reorder_header(struct sk_buff *skb)
75{
9dfebcc6 76 if (vlan_dev_info(skb->dev)->flags & VLAN_FLAG_REORDER_HDR) {
1da177e4
LT
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);
b0e380b1 86 skb->mac_header += VLAN_HLEN;
1da177e4
LT
87 }
88 }
89
90 return skb;
91}
92
91b4f954
PE
93static inline void vlan_set_encap_proto(struct sk_buff *skb,
94 struct vlan_hdr *vhdr)
95{
96 __be16 proto;
97 unsigned char *rawp;
98
99 /*
100 * Was a VLAN packet, grab the encapsulated protocol, which the layer
101 * three protocols care about.
102 */
103
104 proto = vhdr->h_vlan_encapsulated_proto;
105 if (ntohs(proto) >= 1536) {
106 skb->protocol = proto;
107 return;
108 }
109
110 rawp = skb->data;
111 if (*(unsigned short *)rawp == 0xFFFF)
112 /*
113 * This is a magic hack to spot IPX packets. Older Novell
114 * breaks the protocol design and runs IPX over 802.3 without
115 * an 802.2 LLC layer. We look for FFFF which isn't a used
116 * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
117 * but does for the rest.
118 */
119 skb->protocol = htons(ETH_P_802_3);
120 else
121 /*
122 * Real 802.2 LLC
123 */
124 skb->protocol = htons(ETH_P_802_2);
125}
126
1da177e4 127/*
122952fc 128 * Determine the packet's protocol ID. The rule here is that we
1da177e4
LT
129 * assume 802.3 if the type field is short enough to be a length.
130 * This is normal practice and works for any 'now in use' protocol.
131 *
132 * Also, at this point we assume that we ARE dealing exclusively with
133 * VLAN packets, or packets that should be made into VLAN packets based
134 * on a default VLAN ID.
135 *
136 * NOTE: Should be similar to ethernet/eth.c.
137 *
138 * SANITY NOTE: This method is called when a packet is moving up the stack
139 * towards userland. To get here, it would have already passed
140 * through the ethernet/eth.c eth_type_trans() method.
141 * SANITY NOTE 2: We are referencing to the VLAN_HDR frields, which MAY be
142 * stored UNALIGNED in the memory. RISC systems don't like
143 * such cases very much...
2029cc2c
PM
144 * SANITY NOTE 2a: According to Dave Miller & Alexey, it will always be
145 * aligned, so there doesn't need to be any of the unaligned
146 * stuff. It has been commented out now... --Ben
1da177e4
LT
147 *
148 */
149int vlan_skb_recv(struct sk_buff *skb, struct net_device *dev,
2029cc2c 150 struct packet_type *ptype, struct net_device *orig_dev)
1da177e4 151{
e7c243c9 152 struct vlan_hdr *vhdr;
1da177e4
LT
153 unsigned short vid;
154 struct net_device_stats *stats;
155 unsigned short vlan_TCI;
1da177e4 156
2029cc2c
PM
157 skb = skb_share_check(skb, GFP_ATOMIC);
158 if (skb == NULL)
31ffdbcb 159 goto err_free;
e7c243c9 160
31ffdbcb
PM
161 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
162 goto err_free;
e7c243c9 163
31ffdbcb 164 vhdr = (struct vlan_hdr *)skb->data;
1da177e4 165 vlan_TCI = ntohs(vhdr->h_vlan_TCI);
1da177e4
LT
166 vid = (vlan_TCI & VLAN_VID_MASK);
167
1da177e4
LT
168 rcu_read_lock();
169 skb->dev = __find_vlan_dev(dev, vid);
170 if (!skb->dev) {
2029cc2c 171 pr_debug("%s: ERROR: No net_device for VID: %u on dev: %s\n",
0dc47877 172 __func__, (unsigned int)vid, dev->name);
31ffdbcb 173 goto err_unlock;
1da177e4
LT
174 }
175
176 skb->dev->last_rx = jiffies;
177
7bd38d77 178 stats = &skb->dev->stats;
1da177e4
LT
179 stats->rx_packets++;
180 stats->rx_bytes += skb->len;
181
cbb042f9 182 skb_pull_rcsum(skb, VLAN_HLEN);
a388442c 183
2029cc2c
PM
184 skb->priority = vlan_get_ingress_priority(skb->dev,
185 ntohs(vhdr->h_vlan_TCI));
1da177e4 186
40f98e1a 187 pr_debug("%s: priority: %u for TCI: %hu\n",
0dc47877 188 __func__, skb->priority, ntohs(vhdr->h_vlan_TCI));
1da177e4 189
1da177e4
LT
190 switch (skb->pkt_type) {
191 case PACKET_BROADCAST: /* Yeah, stats collect these together.. */
2029cc2c 192 /* stats->broadcast ++; // no such counter :-( */
1da177e4
LT
193 break;
194
195 case PACKET_MULTICAST:
196 stats->multicast++;
197 break;
198
122952fc 199 case PACKET_OTHERHOST:
1da177e4 200 /* Our lower layer thinks this is not local, let's make sure.
2029cc2c
PM
201 * This allows the VLAN to have a different MAC than the
202 * underlying device, and still route correctly.
1da177e4 203 */
2029cc2c
PM
204 if (!compare_ether_addr(eth_hdr(skb)->h_dest,
205 skb->dev->dev_addr))
1da177e4 206 skb->pkt_type = PACKET_HOST;
1da177e4
LT
207 break;
208 default:
209 break;
3ff50b79 210 }
1da177e4 211
91b4f954 212 vlan_set_encap_proto(skb, vhdr);
1da177e4 213
1da177e4 214 skb = vlan_check_reorder_header(skb);
31ffdbcb 215 if (!skb) {
1da177e4 216 stats->rx_errors++;
31ffdbcb 217 goto err_unlock;
1da177e4 218 }
31ffdbcb
PM
219
220 netif_rx(skb);
1da177e4 221 rcu_read_unlock();
31ffdbcb
PM
222 return NET_RX_SUCCESS;
223
224err_unlock:
225 rcu_read_unlock();
226err_free:
227 kfree_skb(skb);
228 return NET_RX_DROP;
1da177e4
LT
229}
230
2029cc2c
PM
231static inline unsigned short
232vlan_dev_get_egress_qos_mask(struct net_device *dev, struct sk_buff *skb)
1da177e4 233{
2029cc2c 234 struct vlan_priority_tci_mapping *mp;
1da177e4 235
2029cc2c 236 mp = vlan_dev_info(dev)->egress_priority_map[(skb->priority & 0xF)];
1da177e4
LT
237 while (mp) {
238 if (mp->priority == skb->priority) {
2029cc2c
PM
239 return mp->vlan_qos; /* This should already be shifted
240 * to mask correctly with the
241 * VLAN's TCI */
1da177e4
LT
242 }
243 mp = mp->next;
244 }
245 return 0;
246}
247
248/*
122952fc 249 * Create the VLAN header for an arbitrary protocol layer
1da177e4
LT
250 *
251 * saddr=NULL means use device source address
252 * daddr=NULL means leave destination address (eg unresolved arp)
253 *
254 * This is called when the SKB is moving down the stack towards the
255 * physical devices.
256 */
ef3eb3e5
PM
257static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
258 unsigned short type,
259 const void *daddr, const void *saddr,
260 unsigned int len)
1da177e4
LT
261{
262 struct vlan_hdr *vhdr;
263 unsigned short veth_TCI = 0;
264 int rc = 0;
265 int build_vlan_header = 0;
2029cc2c 266 struct net_device *vdev = dev;
1da177e4 267
40f98e1a 268 pr_debug("%s: skb: %p type: %hx len: %u vlan_id: %hx, daddr: %p\n",
0dc47877 269 __func__, skb, type, len, vlan_dev_info(dev)->vlan_id,
2029cc2c 270 daddr);
1da177e4
LT
271
272 /* build vlan header only if re_order_header flag is NOT set. This
273 * fixes some programs that get confused when they see a VLAN device
274 * sending a frame that is VLAN encoded (the consensus is that the VLAN
275 * device should look completely like an Ethernet device when the
122952fc 276 * REORDER_HEADER flag is set) The drawback to this is some extra
1da177e4
LT
277 * header shuffling in the hard_start_xmit. Users can turn off this
278 * REORDER behaviour with the vconfig tool.
279 */
9dfebcc6 280 if (!(vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR))
a4bf3af4 281 build_vlan_header = 1;
1da177e4
LT
282
283 if (build_vlan_header) {
284 vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN);
285
286 /* build the four bytes that make this a VLAN header. */
287
2029cc2c
PM
288 /* Now, construct the second two bytes. This field looks
289 * something like:
1da177e4
LT
290 * usr_priority: 3 bits (high bits)
291 * CFI 1 bit
292 * VLAN ID 12 bits (low bits)
293 *
294 */
9dfebcc6 295 veth_TCI = vlan_dev_info(dev)->vlan_id;
1da177e4
LT
296 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
297
298 vhdr->h_vlan_TCI = htons(veth_TCI);
299
300 /*
2029cc2c
PM
301 * Set the protocol type. For a packet of type ETH_P_802_3 we
302 * put the length in here instead. It is up to the 802.2
303 * layer to carry protocol information.
1da177e4
LT
304 */
305
2029cc2c 306 if (type != ETH_P_802_3)
1da177e4 307 vhdr->h_vlan_encapsulated_proto = htons(type);
2029cc2c 308 else
1da177e4 309 vhdr->h_vlan_encapsulated_proto = htons(len);
279e172a
JB
310
311 skb->protocol = htons(ETH_P_8021Q);
1da177e4
LT
312 }
313
314 /* Before delegating work to the lower layer, enter our MAC-address */
315 if (saddr == NULL)
316 saddr = dev->dev_addr;
317
9dfebcc6 318 dev = vlan_dev_info(dev)->real_dev;
1da177e4 319
2029cc2c
PM
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...
1da177e4
LT
324 */
325
2029cc2c
PM
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
1da177e4
LT
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) {
7bd38d77 335 struct net_device_stats *stats = &vdev->stats;
1da177e4
LT
336 stats->tx_dropped++;
337 return -ENOMEM;
338 }
9dfebcc6 339 vlan_dev_info(vdev)->cnt_inc_headroom_on_tx++;
0dc47877 340 pr_debug("%s: %s: had to grow skb\n", __func__, vdev->name);
1da177e4
LT
341 }
342
343 if (build_vlan_header) {
344 /* Now make the underlying real hard header */
0c4e8581
SH
345 rc = dev_hard_header(skb, dev, ETH_P_8021Q, daddr, saddr,
346 len + VLAN_HLEN);
347 if (rc > 0)
1da177e4 348 rc += VLAN_HLEN;
0c4e8581 349 else if (rc < 0)
1da177e4 350 rc -= VLAN_HLEN;
0c4e8581 351 } else
2029cc2c
PM
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.
1da177e4 356 */
0c4e8581 357 rc = dev_hard_header(skb, dev, type, daddr, saddr, len);
1da177e4
LT
358
359 return rc;
360}
361
ef3eb3e5 362static int vlan_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1da177e4 363{
7bd38d77 364 struct net_device_stats *stats = &dev->stats;
1da177e4 365 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
55b01e86 366
1da177e4
LT
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
6ab3b487 373 if (veth->h_vlan_proto != htons(ETH_P_8021Q) ||
9dfebcc6 374 vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR) {
1da177e4
LT
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! */
9dfebcc6 379 vlan_dev_info(dev)->cnt_encap_on_xmit++;
1da177e4 380
40f98e1a 381 pr_debug("%s: proto to encap: 0x%hx\n",
577f99c1 382 __func__, ntohs(veth->h_vlan_proto));
1da177e4
LT
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 */
9dfebcc6 389 veth_TCI = vlan_dev_info(dev)->vlan_id;
1da177e4
LT
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
2029cc2c 398 if (orig_headroom < VLAN_HLEN)
9dfebcc6 399 vlan_dev_info(dev)->cnt_inc_headroom_on_tx++;
1da177e4
LT
400 }
401
40f98e1a 402 pr_debug("%s: about to send skb: %p to dev: %s\n",
0dc47877 403 __func__, skb, skb->dev->name);
55b01e86
DM
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],
40f98e1a
PM
409 veth->h_vlan_proto, veth->h_vlan_TCI,
410 veth->h_vlan_encapsulated_proto);
1da177e4
LT
411
412 stats->tx_packets++; /* for statics only */
413 stats->tx_bytes += skb->len;
414
9dfebcc6 415 skb->dev = vlan_dev_info(dev)->real_dev;
1da177e4
LT
416 dev_queue_xmit(skb);
417
418 return 0;
419}
420
ef3eb3e5
PM
421static int vlan_dev_hwaccel_hard_start_xmit(struct sk_buff *skb,
422 struct net_device *dev)
1da177e4 423{
7bd38d77 424 struct net_device_stats *stats = &dev->stats;
1da177e4
LT
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 */
9dfebcc6 433 veth_TCI = vlan_dev_info(dev)->vlan_id;
1da177e4
LT
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
9dfebcc6 440 skb->dev = vlan_dev_info(dev)->real_dev;
1da177e4
LT
441 dev_queue_xmit(skb);
442
443 return 0;
444}
445
ef3eb3e5 446static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
1da177e4
LT
447{
448 /* TODO: gotta make sure the underlying layer can handle it,
449 * maybe an IFF_VLAN_CAPABLE flag for devices?
450 */
9dfebcc6 451 if (vlan_dev_info(dev)->real_dev->mtu < new_mtu)
1da177e4
LT
452 return -ERANGE;
453
454 dev->mtu = new_mtu;
455
456 return 0;
457}
458
c17d8874
PM
459void vlan_dev_set_ingress_priority(const struct net_device *dev,
460 u32 skb_prio, short vlan_prio)
1da177e4 461{
9dfebcc6 462 struct vlan_dev_info *vlan = vlan_dev_info(dev);
b020cb48
PM
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;
1da177e4
LT
470}
471
c17d8874
PM
472int vlan_dev_set_egress_priority(const struct net_device *dev,
473 u32 skb_prio, short vlan_prio)
1da177e4 474{
9dfebcc6 475 struct vlan_dev_info *vlan = vlan_dev_info(dev);
1da177e4
LT
476 struct vlan_priority_tci_mapping *mp = NULL;
477 struct vlan_priority_tci_mapping *np;
b020cb48 478 u32 vlan_qos = (vlan_prio << 13) & 0xE000;
122952fc 479
c17d8874 480 /* See if a priority mapping exists.. */
b020cb48 481 mp = vlan->egress_priority_map[skb_prio & 0xF];
c17d8874
PM
482 while (mp) {
483 if (mp->priority == skb_prio) {
b020cb48
PM
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;
c17d8874 489 return 0;
1da177e4 490 }
c17d8874 491 mp = mp->next;
1da177e4 492 }
c17d8874
PM
493
494 /* Create a new mapping then. */
b020cb48 495 mp = vlan->egress_priority_map[skb_prio & 0xF];
c17d8874
PM
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;
b020cb48
PM
502 np->vlan_qos = vlan_qos;
503 vlan->egress_priority_map[skb_prio & 0xF] = np;
504 if (vlan_qos)
505 vlan->nr_egress_mappings++;
c17d8874 506 return 0;
1da177e4
LT
507}
508
a4bf3af4 509/* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */
b3ce0325 510int vlan_dev_change_flags(const struct net_device *dev, u32 flags, u32 mask)
1da177e4 511{
b3ce0325
PM
512 struct vlan_dev_info *vlan = vlan_dev_info(dev);
513 u32 old_flags = vlan->flags;
514
70c03b49 515 if (mask & ~(VLAN_FLAG_REORDER_HDR | VLAN_FLAG_GVRP))
b3ce0325
PM
516 return -EINVAL;
517
518 vlan->flags = (old_flags & ~mask) | (flags & mask);
70c03b49
PM
519
520 if (netif_running(dev) && (vlan->flags ^ old_flags) & VLAN_FLAG_GVRP) {
521 if (vlan->flags & VLAN_FLAG_GVRP)
522 vlan_gvrp_request_join(dev);
523 else
524 vlan_gvrp_request_leave(dev);
525 }
b3ce0325 526 return 0;
1da177e4
LT
527}
528
c17d8874 529void vlan_dev_get_realdev_name(const struct net_device *dev, char *result)
1da177e4 530{
9dfebcc6 531 strncpy(result, vlan_dev_info(dev)->real_dev->name, 23);
1da177e4
LT
532}
533
ef3eb3e5 534static int vlan_dev_open(struct net_device *dev)
1da177e4 535{
9dfebcc6 536 struct vlan_dev_info *vlan = vlan_dev_info(dev);
8c979c26
PM
537 struct net_device *real_dev = vlan->real_dev;
538 int err;
539
540 if (!(real_dev->flags & IFF_UP))
1da177e4
LT
541 return -ENETDOWN;
542
8c979c26
PM
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
6c78dcbd
PM
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
70c03b49
PM
555 if (vlan->flags & VLAN_FLAG_GVRP)
556 vlan_gvrp_request_join(dev);
557
1da177e4
LT
558 return 0;
559}
560
ef3eb3e5 561static int vlan_dev_stop(struct net_device *dev)
1da177e4 562{
70c03b49
PM
563 struct vlan_dev_info *vlan = vlan_dev_info(dev);
564 struct net_device *real_dev = vlan->real_dev;
565
566 if (vlan->flags & VLAN_FLAG_GVRP)
567 vlan_gvrp_request_leave(dev);
8c979c26 568
56addd6e 569 dev_mc_unsync(real_dev, dev);
e83a2ea8 570 dev_unicast_unsync(real_dev, dev);
6c78dcbd
PM
571 if (dev->flags & IFF_ALLMULTI)
572 dev_set_allmulti(real_dev, -1);
573 if (dev->flags & IFF_PROMISC)
574 dev_set_promiscuity(real_dev, -1);
575
8c979c26
PM
576 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
577 dev_unicast_delete(real_dev, dev->dev_addr, dev->addr_len);
578
1da177e4
LT
579 return 0;
580}
581
ef3eb3e5 582static int vlan_dev_set_mac_address(struct net_device *dev, void *p)
39aaac11 583{
9dfebcc6 584 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
39aaac11
PM
585 struct sockaddr *addr = p;
586 int err;
587
588 if (!is_valid_ether_addr(addr->sa_data))
589 return -EADDRNOTAVAIL;
590
591 if (!(dev->flags & IFF_UP))
592 goto out;
593
594 if (compare_ether_addr(addr->sa_data, real_dev->dev_addr)) {
595 err = dev_unicast_add(real_dev, addr->sa_data, ETH_ALEN);
596 if (err < 0)
597 return err;
598 }
599
600 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
601 dev_unicast_delete(real_dev, dev->dev_addr, ETH_ALEN);
602
603out:
604 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
605 return 0;
606}
607
ef3eb3e5 608static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1da177e4 609{
9dfebcc6 610 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
1da177e4
LT
611 struct ifreq ifrr;
612 int err = -EOPNOTSUPP;
613
614 strncpy(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
615 ifrr.ifr_ifru = ifr->ifr_ifru;
616
2029cc2c 617 switch (cmd) {
1da177e4
LT
618 case SIOCGMIIPHY:
619 case SIOCGMIIREG:
620 case SIOCSMIIREG:
122952fc 621 if (real_dev->do_ioctl && netif_device_present(real_dev))
1da177e4
LT
622 err = real_dev->do_ioctl(real_dev, &ifrr, cmd);
623 break;
1da177e4
LT
624 }
625
122952fc 626 if (!err)
1da177e4
LT
627 ifr->ifr_ifru = ifrr.ifr_ifru;
628
629 return err;
630}
631
ef3eb3e5 632static void vlan_dev_change_rx_flags(struct net_device *dev, int change)
6c78dcbd 633{
9dfebcc6 634 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
6c78dcbd
PM
635
636 if (change & IFF_ALLMULTI)
637 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
638 if (change & IFF_PROMISC)
639 dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1);
640}
641
e83a2ea8 642static void vlan_dev_set_rx_mode(struct net_device *vlan_dev)
1da177e4 643{
9dfebcc6 644 dev_mc_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
e83a2ea8 645 dev_unicast_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
1da177e4 646}
ef3eb3e5
PM
647
648/*
649 * vlan network devices have devices nesting below it, and are a special
650 * "super class" of normal network devices; split their locks off into a
651 * separate class since they always nest.
652 */
653static struct lock_class_key vlan_netdev_xmit_lock_key;
654
655static const struct header_ops vlan_header_ops = {
656 .create = vlan_dev_hard_header,
657 .rebuild = vlan_dev_rebuild_header,
658 .parse = eth_header_parse,
659};
660
661static int vlan_dev_init(struct net_device *dev)
662{
9dfebcc6 663 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
ef3eb3e5
PM
664 int subclass = 0;
665
666 /* IFF_BROADCAST|IFF_MULTICAST; ??? */
0ed21b32 667 dev->flags = real_dev->flags & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI);
ef3eb3e5
PM
668 dev->iflink = real_dev->ifindex;
669 dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
670 (1<<__LINK_STATE_DORMANT))) |
671 (1<<__LINK_STATE_PRESENT);
672
289c79a4 673 dev->features |= real_dev->features & real_dev->vlan_features;
5fb13570 674
ef3eb3e5
PM
675 /* ipv6 shared card related stuff */
676 dev->dev_id = real_dev->dev_id;
677
678 if (is_zero_ether_addr(dev->dev_addr))
679 memcpy(dev->dev_addr, real_dev->dev_addr, dev->addr_len);
680 if (is_zero_ether_addr(dev->broadcast))
681 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
682
683 if (real_dev->features & NETIF_F_HW_VLAN_TX) {
684 dev->header_ops = real_dev->header_ops;
685 dev->hard_header_len = real_dev->hard_header_len;
686 dev->hard_start_xmit = vlan_dev_hwaccel_hard_start_xmit;
687 } else {
688 dev->header_ops = &vlan_header_ops;
689 dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
690 dev->hard_start_xmit = vlan_dev_hard_start_xmit;
691 }
692
26a25239 693 if (is_vlan_dev(real_dev))
ef3eb3e5
PM
694 subclass = 1;
695
696 lockdep_set_class_and_subclass(&dev->_xmit_lock,
697 &vlan_netdev_xmit_lock_key, subclass);
698 return 0;
699}
700
23556323
PE
701static void vlan_dev_uninit(struct net_device *dev)
702{
703 struct vlan_priority_tci_mapping *pm;
704 struct vlan_dev_info *vlan = vlan_dev_info(dev);
705 int i;
706
707 for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
708 while ((pm = vlan->egress_priority_map[i]) != NULL) {
709 vlan->egress_priority_map[i] = pm->next;
710 kfree(pm);
711 }
712 }
713}
714
75b8846a
PM
715static u32 vlan_ethtool_get_rx_csum(struct net_device *dev)
716{
717 const struct vlan_dev_info *vlan = vlan_dev_info(dev);
718 struct net_device *real_dev = vlan->real_dev;
719
720 if (real_dev->ethtool_ops == NULL ||
721 real_dev->ethtool_ops->get_rx_csum == NULL)
722 return 0;
723 return real_dev->ethtool_ops->get_rx_csum(real_dev);
724}
725
726static const struct ethtool_ops vlan_ethtool_ops = {
727 .get_link = ethtool_op_get_link,
728 .get_rx_csum = vlan_ethtool_get_rx_csum,
729};
730
ef3eb3e5
PM
731void vlan_setup(struct net_device *dev)
732{
733 ether_setup(dev);
734
735 dev->priv_flags |= IFF_802_1Q_VLAN;
736 dev->tx_queue_len = 0;
737
738 dev->change_mtu = vlan_dev_change_mtu;
739 dev->init = vlan_dev_init;
23556323 740 dev->uninit = vlan_dev_uninit;
ef3eb3e5
PM
741 dev->open = vlan_dev_open;
742 dev->stop = vlan_dev_stop;
743 dev->set_mac_address = vlan_dev_set_mac_address;
e83a2ea8
CL
744 dev->set_rx_mode = vlan_dev_set_rx_mode;
745 dev->set_multicast_list = vlan_dev_set_rx_mode;
ef3eb3e5
PM
746 dev->change_rx_flags = vlan_dev_change_rx_flags;
747 dev->do_ioctl = vlan_dev_ioctl;
748 dev->destructor = free_netdev;
75b8846a 749 dev->ethtool_ops = &vlan_ethtool_ops;
ef3eb3e5
PM
750
751 memset(dev->broadcast, 0, ETH_ALEN);
752}