blob: 50d8edcbb89739189501855f71691148900b6021 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* -*- 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
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +09008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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.
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +090015 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
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 */
Patrick McHardyef3eb3e2008-01-21 00:22:11 -080050static int vlan_dev_rebuild_header(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
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);
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +090061#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 default:
63 printk(VLAN_DBG
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +090064 "%s: unable to resolve type %X addresses.\n",
Alexey Dobriyand136fe72005-12-28 22:27:10 +030065 dev->name, ntohs(veth->h_vlan_encapsulated_proto));
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +090066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 memcpy(veth->h_source, dev->dev_addr, ETH_ALEN);
68 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 return 0;
72}
73
74static inline struct sk_buff *vlan_check_reorder_header(struct sk_buff *skb)
75{
Patrick McHardya4bf3af42007-06-13 12:07:37 -070076 if (VLAN_DEV_INFO(skb->dev)->flags & VLAN_FLAG_REORDER_HDR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 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);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -070086 skb->mac_header += VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
88 }
89
90 return skb;
91}
92
93/*
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +090094 * Determine the packet's protocol ID. The rule here is that we
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 * 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 */
115int vlan_skb_recv(struct sk_buff *skb, struct net_device *dev,
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900116 struct packet_type* ptype, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 unsigned char *rawp = NULL;
Evgeniy Polyakove7c243c2007-08-24 23:36:29 -0700119 struct vlan_hdr *vhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 unsigned short vid;
121 struct net_device_stats *stats;
122 unsigned short vlan_TCI;
Alexey Dobriyan3c3f8f22005-09-19 15:41:28 -0700123 __be16 proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Eric W. Biedermane730c152007-09-17 11:53:39 -0700125 if (dev->nd_net != &init_net) {
126 kfree_skb(skb);
127 return -1;
128 }
129
Evgeniy Polyakove7c243c2007-08-24 23:36:29 -0700130 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 /* 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. */
Patrick McHardy7bd38d72008-01-21 00:19:31 -0800177 stats = &skb->dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 stats->rx_packets++;
179 stats->rx_bytes += skb->len;
180
Herbert Xucbb042f2006-03-20 22:43:56 -0800181 /* Take off the VLAN header (4 bytes currently) */
182 skb_pull_rcsum(skb, VLAN_HLEN);
Stephen Hemmingera3884422005-12-14 16:23:16 -0800183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 /*
185 * Deal with ingress priority mapping.
186 */
187 skb->priority = vlan_get_ingress_priority(skb->dev, ntohs(vhdr->h_vlan_TCI));
188
189#ifdef VLAN_DEBUG
190 printk(VLAN_DBG "%s: priority: %lu for TCI: %hu (hbo)\n",
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900191 __FUNCTION__, (unsigned long)(skb->priority),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 ntohs(vhdr->h_vlan_TCI));
193#endif
194
195 /* The ethernet driver already did the pkt_type calculations
196 * for us...
197 */
198 switch (skb->pkt_type) {
199 case PACKET_BROADCAST: /* Yeah, stats collect these together.. */
200 // stats->broadcast ++; // no such counter :-(
201 break;
202
203 case PACKET_MULTICAST:
204 stats->multicast++;
205 break;
206
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900207 case PACKET_OTHERHOST:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 /* Our lower layer thinks this is not local, let's make sure.
209 * This allows the VLAN to have a different MAC than the underlying
210 * device, and still route correctly.
211 */
Kris Katterjohnd3f4a682006-01-09 16:01:43 -0800212 if (!compare_ether_addr(eth_hdr(skb)->h_dest, skb->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* It is for our (changed) MAC-address! */
214 skb->pkt_type = PACKET_HOST;
215 }
216 break;
217 default:
218 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 /* Was a VLAN packet, grab the encapsulated protocol, which the layer
222 * three protocols care about.
223 */
224 /* proto = get_unaligned(&vhdr->h_vlan_encapsulated_proto); */
225 proto = vhdr->h_vlan_encapsulated_proto;
226
227 skb->protocol = proto;
228 if (ntohs(proto) >= 1536) {
229 /* place it back on the queue to be handled by
230 * true layer 3 protocols.
231 */
232
233 /* See if we are configured to re-write the VLAN header
234 * to make it look like ethernet...
235 */
236 skb = vlan_check_reorder_header(skb);
237
238 /* Can be null if skb-clone fails when re-ordering */
239 if (skb) {
240 netif_rx(skb);
241 } else {
242 /* TODO: Add a more specific counter here. */
243 stats->rx_errors++;
244 }
245 rcu_read_unlock();
246 return 0;
247 }
248
249 rawp = skb->data;
250
251 /*
252 * This is a magic hack to spot IPX packets. Older Novell breaks
253 * the protocol design and runs IPX over 802.3 without an 802.2 LLC
254 * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
255 * won't work for fault tolerant netware but does for the rest.
256 */
257 if (*(unsigned short *)rawp == 0xFFFF) {
YOSHIFUJI Hideakib93b7ee2007-03-25 20:12:18 -0700258 skb->protocol = htons(ETH_P_802_3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 /* place it back on the queue to be handled by true layer 3 protocols.
260 */
261
262 /* See if we are configured to re-write the VLAN header
263 * to make it look like ethernet...
264 */
265 skb = vlan_check_reorder_header(skb);
266
267 /* Can be null if skb-clone fails when re-ordering */
268 if (skb) {
269 netif_rx(skb);
270 } else {
271 /* TODO: Add a more specific counter here. */
272 stats->rx_errors++;
273 }
274 rcu_read_unlock();
275 return 0;
276 }
277
278 /*
279 * Real 802.2 LLC
280 */
YOSHIFUJI Hideakib93b7ee2007-03-25 20:12:18 -0700281 skb->protocol = htons(ETH_P_802_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 /* place it back on the queue to be handled by upper layer protocols.
283 */
284
285 /* See if we are configured to re-write the VLAN header
286 * to make it look like ethernet...
287 */
288 skb = vlan_check_reorder_header(skb);
289
290 /* Can be null if skb-clone fails when re-ordering */
291 if (skb) {
292 netif_rx(skb);
293 } else {
294 /* TODO: Add a more specific counter here. */
295 stats->rx_errors++;
296 }
297 rcu_read_unlock();
298 return 0;
299}
300
301static inline unsigned short vlan_dev_get_egress_qos_mask(struct net_device* dev,
302 struct sk_buff* skb)
303{
304 struct vlan_priority_tci_mapping *mp =
305 VLAN_DEV_INFO(dev)->egress_priority_map[(skb->priority & 0xF)];
306
307 while (mp) {
308 if (mp->priority == skb->priority) {
309 return mp->vlan_qos; /* This should already be shifted to mask
310 * correctly with the VLAN's TCI
311 */
312 }
313 mp = mp->next;
314 }
315 return 0;
316}
317
318/*
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900319 * Create the VLAN header for an arbitrary protocol layer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 *
321 * saddr=NULL means use device source address
322 * daddr=NULL means leave destination address (eg unresolved arp)
323 *
324 * This is called when the SKB is moving down the stack towards the
325 * physical devices.
326 */
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800327static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
328 unsigned short type,
329 const void *daddr, const void *saddr,
330 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 struct vlan_hdr *vhdr;
333 unsigned short veth_TCI = 0;
334 int rc = 0;
335 int build_vlan_header = 0;
336 struct net_device *vdev = dev; /* save this for the bottom of the method */
337
338#ifdef VLAN_DEBUG
339 printk(VLAN_DBG "%s: skb: %p type: %hx len: %x vlan_id: %hx, daddr: %p\n",
340 __FUNCTION__, skb, type, len, VLAN_DEV_INFO(dev)->vlan_id, daddr);
341#endif
342
343 /* build vlan header only if re_order_header flag is NOT set. This
344 * fixes some programs that get confused when they see a VLAN device
345 * sending a frame that is VLAN encoded (the consensus is that the VLAN
346 * device should look completely like an Ethernet device when the
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900347 * REORDER_HEADER flag is set) The drawback to this is some extra
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 * header shuffling in the hard_start_xmit. Users can turn off this
349 * REORDER behaviour with the vconfig tool.
350 */
Patrick McHardya4bf3af42007-06-13 12:07:37 -0700351 if (!(VLAN_DEV_INFO(dev)->flags & VLAN_FLAG_REORDER_HDR))
352 build_vlan_header = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 if (build_vlan_header) {
355 vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN);
356
357 /* build the four bytes that make this a VLAN header. */
358
359 /* Now, construct the second two bytes. This field looks something
360 * like:
361 * usr_priority: 3 bits (high bits)
362 * CFI 1 bit
363 * VLAN ID 12 bits (low bits)
364 *
365 */
366 veth_TCI = VLAN_DEV_INFO(dev)->vlan_id;
367 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
368
369 vhdr->h_vlan_TCI = htons(veth_TCI);
370
371 /*
372 * Set the protocol type.
373 * For a packet of type ETH_P_802_3 we put the length in here instead.
374 * It is up to the 802.2 layer to carry protocol information.
375 */
376
377 if (type != ETH_P_802_3) {
378 vhdr->h_vlan_encapsulated_proto = htons(type);
379 } else {
380 vhdr->h_vlan_encapsulated_proto = htons(len);
381 }
Jerome Borsboom279e1722007-04-13 16:12:47 -0700382
383 skb->protocol = htons(ETH_P_8021Q);
David S. Millerbe8bd862007-04-19 20:34:51 -0700384 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386
387 /* Before delegating work to the lower layer, enter our MAC-address */
388 if (saddr == NULL)
389 saddr = dev->dev_addr;
390
391 dev = VLAN_DEV_INFO(dev)->real_dev;
392
393 /* MPLS can send us skbuffs w/out enough space. This check will grow the
394 * skb if it doesn't have enough headroom. Not a beautiful solution, so
395 * I'll tick a counter so that users can know it's happening... If they
396 * care...
397 */
398
399 /* NOTE: This may still break if the underlying device is not the final
400 * device (and thus there are more headers to add...) It should work for
401 * good-ole-ethernet though.
402 */
403 if (skb_headroom(skb) < dev->hard_header_len) {
404 struct sk_buff *sk_tmp = skb;
405 skb = skb_realloc_headroom(sk_tmp, dev->hard_header_len);
406 kfree_skb(sk_tmp);
407 if (skb == NULL) {
Patrick McHardy7bd38d72008-01-21 00:19:31 -0800408 struct net_device_stats *stats = &vdev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 stats->tx_dropped++;
410 return -ENOMEM;
411 }
412 VLAN_DEV_INFO(vdev)->cnt_inc_headroom_on_tx++;
413#ifdef VLAN_DEBUG
414 printk(VLAN_DBG "%s: %s: had to grow skb.\n", __FUNCTION__, vdev->name);
415#endif
416 }
417
418 if (build_vlan_header) {
419 /* Now make the underlying real hard header */
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700420 rc = dev_hard_header(skb, dev, ETH_P_8021Q, daddr, saddr,
421 len + VLAN_HLEN);
422 if (rc > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 rc += VLAN_HLEN;
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700424 else if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 rc -= VLAN_HLEN;
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700426 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 /* If here, then we'll just make a normal looking ethernet frame,
428 * but, the hard_start_xmit method will insert the tag (it has to
429 * be able to do this for bridged and other skbs that don't come
430 * down the protocol stack in an orderly manner.
431 */
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700432 rc = dev_hard_header(skb, dev, type, daddr, saddr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 return rc;
435}
436
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800437static int vlan_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Patrick McHardy7bd38d72008-01-21 00:19:31 -0800439 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
441
442 /* Handle non-VLAN frames if they are sent to us, for example by DHCP.
443 *
444 * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
445 * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
446 */
447
Joonwoo Park6ab3b482007-11-29 22:16:41 +1100448 if (veth->h_vlan_proto != htons(ETH_P_8021Q) ||
449 VLAN_DEV_INFO(dev)->flags & VLAN_FLAG_REORDER_HDR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 int orig_headroom = skb_headroom(skb);
451 unsigned short veth_TCI;
452
453 /* This is not a VLAN frame...but we can fix that! */
454 VLAN_DEV_INFO(dev)->cnt_encap_on_xmit++;
455
456#ifdef VLAN_DEBUG
457 printk(VLAN_DBG "%s: proto to encap: 0x%hx (hbo)\n",
458 __FUNCTION__, htons(veth->h_vlan_proto));
459#endif
460 /* Construct the second two bytes. This field looks something
461 * like:
462 * usr_priority: 3 bits (high bits)
463 * CFI 1 bit
464 * VLAN ID 12 bits (low bits)
465 */
466 veth_TCI = VLAN_DEV_INFO(dev)->vlan_id;
467 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
468
469 skb = __vlan_put_tag(skb, veth_TCI);
470 if (!skb) {
471 stats->tx_dropped++;
472 return 0;
473 }
474
475 if (orig_headroom < VLAN_HLEN) {
476 VLAN_DEV_INFO(dev)->cnt_inc_headroom_on_tx++;
477 }
478 }
479
480#ifdef VLAN_DEBUG
481 printk(VLAN_DBG "%s: about to send skb: %p to dev: %s\n",
482 __FUNCTION__, skb, skb->dev->name);
483 printk(VLAN_DBG " %2hx.%2hx.%2hx.%2xh.%2hx.%2hx %2hx.%2hx.%2hx.%2hx.%2hx.%2hx %4hx %4hx %4hx\n",
484 veth->h_dest[0], veth->h_dest[1], veth->h_dest[2], veth->h_dest[3], veth->h_dest[4], veth->h_dest[5],
485 veth->h_source[0], veth->h_source[1], veth->h_source[2], veth->h_source[3], veth->h_source[4], veth->h_source[5],
486 veth->h_vlan_proto, veth->h_vlan_TCI, veth->h_vlan_encapsulated_proto);
487#endif
488
489 stats->tx_packets++; /* for statics only */
490 stats->tx_bytes += skb->len;
491
492 skb->dev = VLAN_DEV_INFO(dev)->real_dev;
493 dev_queue_xmit(skb);
494
495 return 0;
496}
497
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800498static int vlan_dev_hwaccel_hard_start_xmit(struct sk_buff *skb,
499 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Patrick McHardy7bd38d72008-01-21 00:19:31 -0800501 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 unsigned short veth_TCI;
503
504 /* Construct the second two bytes. This field looks something
505 * like:
506 * usr_priority: 3 bits (high bits)
507 * CFI 1 bit
508 * VLAN ID 12 bits (low bits)
509 */
510 veth_TCI = VLAN_DEV_INFO(dev)->vlan_id;
511 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
512 skb = __vlan_hwaccel_put_tag(skb, veth_TCI);
513
514 stats->tx_packets++;
515 stats->tx_bytes += skb->len;
516
517 skb->dev = VLAN_DEV_INFO(dev)->real_dev;
518 dev_queue_xmit(skb);
519
520 return 0;
521}
522
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800523static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 /* TODO: gotta make sure the underlying layer can handle it,
526 * maybe an IFF_VLAN_CAPABLE flag for devices?
527 */
528 if (VLAN_DEV_INFO(dev)->real_dev->mtu < new_mtu)
529 return -ERANGE;
530
531 dev->mtu = new_mtu;
532
533 return 0;
534}
535
Patrick McHardyc17d8872007-06-13 12:05:22 -0700536void vlan_dev_set_ingress_priority(const struct net_device *dev,
537 u32 skb_prio, short vlan_prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Patrick McHardyb020cb42007-06-13 12:07:22 -0700539 struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev);
540
541 if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio)
542 vlan->nr_ingress_mappings--;
543 else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio)
544 vlan->nr_ingress_mappings++;
545
546 vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Patrick McHardyc17d8872007-06-13 12:05:22 -0700549int vlan_dev_set_egress_priority(const struct net_device *dev,
550 u32 skb_prio, short vlan_prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Patrick McHardyb020cb42007-06-13 12:07:22 -0700552 struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 struct vlan_priority_tci_mapping *mp = NULL;
554 struct vlan_priority_tci_mapping *np;
Patrick McHardyb020cb42007-06-13 12:07:22 -0700555 u32 vlan_qos = (vlan_prio << 13) & 0xE000;
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900556
Patrick McHardyc17d8872007-06-13 12:05:22 -0700557 /* See if a priority mapping exists.. */
Patrick McHardyb020cb42007-06-13 12:07:22 -0700558 mp = vlan->egress_priority_map[skb_prio & 0xF];
Patrick McHardyc17d8872007-06-13 12:05:22 -0700559 while (mp) {
560 if (mp->priority == skb_prio) {
Patrick McHardyb020cb42007-06-13 12:07:22 -0700561 if (mp->vlan_qos && !vlan_qos)
562 vlan->nr_egress_mappings--;
563 else if (!mp->vlan_qos && vlan_qos)
564 vlan->nr_egress_mappings++;
565 mp->vlan_qos = vlan_qos;
Patrick McHardyc17d8872007-06-13 12:05:22 -0700566 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
Patrick McHardyc17d8872007-06-13 12:05:22 -0700568 mp = mp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
Patrick McHardyc17d8872007-06-13 12:05:22 -0700570
571 /* Create a new mapping then. */
Patrick McHardyb020cb42007-06-13 12:07:22 -0700572 mp = vlan->egress_priority_map[skb_prio & 0xF];
Patrick McHardyc17d8872007-06-13 12:05:22 -0700573 np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL);
574 if (!np)
575 return -ENOBUFS;
576
577 np->next = mp;
578 np->priority = skb_prio;
Patrick McHardyb020cb42007-06-13 12:07:22 -0700579 np->vlan_qos = vlan_qos;
580 vlan->egress_priority_map[skb_prio & 0xF] = np;
581 if (vlan_qos)
582 vlan->nr_egress_mappings++;
Patrick McHardyc17d8872007-06-13 12:05:22 -0700583 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Patrick McHardya4bf3af42007-06-13 12:07:37 -0700586/* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */
Patrick McHardyc17d8872007-06-13 12:05:22 -0700587int vlan_dev_set_vlan_flag(const struct net_device *dev,
588 u32 flag, short flag_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Patrick McHardyc17d8872007-06-13 12:05:22 -0700590 /* verify flag is supported */
Patrick McHardya4bf3af42007-06-13 12:07:37 -0700591 if (flag == VLAN_FLAG_REORDER_HDR) {
Patrick McHardyc17d8872007-06-13 12:05:22 -0700592 if (flag_val) {
Patrick McHardya4bf3af42007-06-13 12:07:37 -0700593 VLAN_DEV_INFO(dev)->flags |= VLAN_FLAG_REORDER_HDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 } else {
Patrick McHardya4bf3af42007-06-13 12:07:37 -0700595 VLAN_DEV_INFO(dev)->flags &= ~VLAN_FLAG_REORDER_HDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
Patrick McHardyc17d8872007-06-13 12:05:22 -0700597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
Patrick McHardyc17d8872007-06-13 12:05:22 -0700599 printk(KERN_ERR "%s: flag %i is not valid.\n", __FUNCTION__, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return -EINVAL;
601}
602
Patrick McHardyc17d8872007-06-13 12:05:22 -0700603void vlan_dev_get_realdev_name(const struct net_device *dev, char *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Patrick McHardyc17d8872007-06-13 12:05:22 -0700605 strncpy(result, VLAN_DEV_INFO(dev)->real_dev->name, 23);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Patrick McHardyc17d8872007-06-13 12:05:22 -0700608void vlan_dev_get_vid(const struct net_device *dev, unsigned short *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Patrick McHardyc17d8872007-06-13 12:05:22 -0700610 *result = VLAN_DEV_INFO(dev)->vlan_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800613static int vlan_dev_open(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Patrick McHardy8c979c22007-07-11 19:45:24 -0700615 struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev);
616 struct net_device *real_dev = vlan->real_dev;
617 int err;
618
619 if (!(real_dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return -ENETDOWN;
621
Patrick McHardy8c979c22007-07-11 19:45:24 -0700622 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr)) {
623 err = dev_unicast_add(real_dev, dev->dev_addr, ETH_ALEN);
624 if (err < 0)
625 return err;
626 }
627 memcpy(vlan->real_dev_addr, real_dev->dev_addr, ETH_ALEN);
628
Patrick McHardy6c78dcb2007-07-14 18:52:56 -0700629 if (dev->flags & IFF_ALLMULTI)
630 dev_set_allmulti(real_dev, 1);
631 if (dev->flags & IFF_PROMISC)
632 dev_set_promiscuity(real_dev, 1);
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return 0;
635}
636
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800637static int vlan_dev_stop(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Patrick McHardy8c979c22007-07-11 19:45:24 -0700639 struct net_device *real_dev = VLAN_DEV_INFO(dev)->real_dev;
640
Patrick McHardy56addd62007-07-14 18:53:28 -0700641 dev_mc_unsync(real_dev, dev);
Patrick McHardy6c78dcb2007-07-14 18:52:56 -0700642 if (dev->flags & IFF_ALLMULTI)
643 dev_set_allmulti(real_dev, -1);
644 if (dev->flags & IFF_PROMISC)
645 dev_set_promiscuity(real_dev, -1);
646
Patrick McHardy8c979c22007-07-11 19:45:24 -0700647 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
648 dev_unicast_delete(real_dev, dev->dev_addr, dev->addr_len);
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return 0;
651}
652
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800653static int vlan_dev_set_mac_address(struct net_device *dev, void *p)
Patrick McHardy39aaac12007-11-10 21:52:35 -0800654{
655 struct net_device *real_dev = VLAN_DEV_INFO(dev)->real_dev;
656 struct sockaddr *addr = p;
657 int err;
658
659 if (!is_valid_ether_addr(addr->sa_data))
660 return -EADDRNOTAVAIL;
661
662 if (!(dev->flags & IFF_UP))
663 goto out;
664
665 if (compare_ether_addr(addr->sa_data, real_dev->dev_addr)) {
666 err = dev_unicast_add(real_dev, addr->sa_data, ETH_ALEN);
667 if (err < 0)
668 return err;
669 }
670
671 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
672 dev_unicast_delete(real_dev, dev->dev_addr, ETH_ALEN);
673
674out:
675 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
676 return 0;
677}
678
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800679static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 struct net_device *real_dev = VLAN_DEV_INFO(dev)->real_dev;
682 struct ifreq ifrr;
683 int err = -EOPNOTSUPP;
684
685 strncpy(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
686 ifrr.ifr_ifru = ifr->ifr_ifru;
687
688 switch(cmd) {
689 case SIOCGMIIPHY:
690 case SIOCGMIIREG:
691 case SIOCSMIIREG:
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900692 if (real_dev->do_ioctl && netif_device_present(real_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 err = real_dev->do_ioctl(real_dev, &ifrr, cmd);
694 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900697 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 ifr->ifr_ifru = ifrr.ifr_ifru;
699
700 return err;
701}
702
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800703static void vlan_dev_change_rx_flags(struct net_device *dev, int change)
Patrick McHardy6c78dcb2007-07-14 18:52:56 -0700704{
705 struct net_device *real_dev = VLAN_DEV_INFO(dev)->real_dev;
706
707 if (change & IFF_ALLMULTI)
708 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
709 if (change & IFF_PROMISC)
710 dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1);
711}
712
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800713static void vlan_dev_set_multicast_list(struct net_device *vlan_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Patrick McHardy56addd62007-07-14 18:53:28 -0700715 dev_mc_sync(VLAN_DEV_INFO(vlan_dev)->real_dev, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800717
718/*
719 * vlan network devices have devices nesting below it, and are a special
720 * "super class" of normal network devices; split their locks off into a
721 * separate class since they always nest.
722 */
723static struct lock_class_key vlan_netdev_xmit_lock_key;
724
725static const struct header_ops vlan_header_ops = {
726 .create = vlan_dev_hard_header,
727 .rebuild = vlan_dev_rebuild_header,
728 .parse = eth_header_parse,
729};
730
731static int vlan_dev_init(struct net_device *dev)
732{
733 struct net_device *real_dev = VLAN_DEV_INFO(dev)->real_dev;
734 int subclass = 0;
735
736 /* IFF_BROADCAST|IFF_MULTICAST; ??? */
737 dev->flags = real_dev->flags & ~IFF_UP;
738 dev->iflink = real_dev->ifindex;
739 dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
740 (1<<__LINK_STATE_DORMANT))) |
741 (1<<__LINK_STATE_PRESENT);
742
743 /* ipv6 shared card related stuff */
744 dev->dev_id = real_dev->dev_id;
745
746 if (is_zero_ether_addr(dev->dev_addr))
747 memcpy(dev->dev_addr, real_dev->dev_addr, dev->addr_len);
748 if (is_zero_ether_addr(dev->broadcast))
749 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
750
751 if (real_dev->features & NETIF_F_HW_VLAN_TX) {
752 dev->header_ops = real_dev->header_ops;
753 dev->hard_header_len = real_dev->hard_header_len;
754 dev->hard_start_xmit = vlan_dev_hwaccel_hard_start_xmit;
755 } else {
756 dev->header_ops = &vlan_header_ops;
757 dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
758 dev->hard_start_xmit = vlan_dev_hard_start_xmit;
759 }
760
761 if (real_dev->priv_flags & IFF_802_1Q_VLAN)
762 subclass = 1;
763
764 lockdep_set_class_and_subclass(&dev->_xmit_lock,
765 &vlan_netdev_xmit_lock_key, subclass);
766 return 0;
767}
768
769void vlan_setup(struct net_device *dev)
770{
771 ether_setup(dev);
772
773 dev->priv_flags |= IFF_802_1Q_VLAN;
774 dev->tx_queue_len = 0;
775
776 dev->change_mtu = vlan_dev_change_mtu;
777 dev->init = vlan_dev_init;
778 dev->open = vlan_dev_open;
779 dev->stop = vlan_dev_stop;
780 dev->set_mac_address = vlan_dev_set_mac_address;
781 dev->set_multicast_list = vlan_dev_set_multicast_list;
782 dev->change_rx_flags = vlan_dev_change_rx_flags;
783 dev->do_ioctl = vlan_dev_ioctl;
784 dev->destructor = free_netdev;
785
786 memset(dev->broadcast, 0, ETH_ALEN);
787}