blob: 328759c32d616195ff634ad12369667d16e4f80e [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 */
50int vlan_dev_rebuild_header(struct sk_buff *skb)
51{
52 struct net_device *dev = skb->dev;
53 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
54
55 switch (veth->h_vlan_encapsulated_proto) {
56#ifdef CONFIG_INET
57 case __constant_htons(ETH_P_IP):
58
59 /* TODO: Confirm this will work with VLAN headers... */
60 return arp_find(veth->h_dest, skb);
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
Evgeniy Polyakove7c243c2007-08-24 23:36:29 -0700125 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
126 return -1;
127
128 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN))) {
129 kfree_skb(skb);
130 return -1;
131 }
132
133 vhdr = (struct vlan_hdr *)(skb->data);
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 /* vlan_TCI = ntohs(get_unaligned(&vhdr->h_vlan_TCI)); */
136 vlan_TCI = ntohs(vhdr->h_vlan_TCI);
137
138 vid = (vlan_TCI & VLAN_VID_MASK);
139
140#ifdef VLAN_DEBUG
141 printk(VLAN_DBG "%s: skb: %p vlan_id: %hx\n",
142 __FUNCTION__, skb, vid);
143#endif
144
145 /* Ok, we will find the correct VLAN device, strip the header,
146 * and then go on as usual.
147 */
148
149 /* We have 12 bits of vlan ID.
150 *
151 * We must not drop allow preempt until we hold a
152 * reference to the device (netif_rx does that) or we
153 * fail.
154 */
155
156 rcu_read_lock();
157 skb->dev = __find_vlan_dev(dev, vid);
158 if (!skb->dev) {
159 rcu_read_unlock();
160
161#ifdef VLAN_DEBUG
162 printk(VLAN_DBG "%s: ERROR: No net_device for VID: %i on dev: %s [%i]\n",
163 __FUNCTION__, (unsigned int)(vid), dev->name, dev->ifindex);
164#endif
165 kfree_skb(skb);
166 return -1;
167 }
168
169 skb->dev->last_rx = jiffies;
170
171 /* Bump the rx counters for the VLAN device. */
172 stats = vlan_dev_get_stats(skb->dev);
173 stats->rx_packets++;
174 stats->rx_bytes += skb->len;
175
Herbert Xucbb042f2006-03-20 22:43:56 -0800176 /* Take off the VLAN header (4 bytes currently) */
177 skb_pull_rcsum(skb, VLAN_HLEN);
Stephen Hemmingera3884422005-12-14 16:23:16 -0800178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 /* Ok, lets check to make sure the device (dev) we
180 * came in on is what this VLAN is attached to.
181 */
182
183 if (dev != VLAN_DEV_INFO(skb->dev)->real_dev) {
184 rcu_read_unlock();
185
186#ifdef VLAN_DEBUG
187 printk(VLAN_DBG "%s: dropping skb: %p because came in on wrong device, dev: %s real_dev: %s, skb_dev: %s\n",
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900188 __FUNCTION__, skb, dev->name,
189 VLAN_DEV_INFO(skb->dev)->real_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 skb->dev->name);
191#endif
192 kfree_skb(skb);
193 stats->rx_errors++;
194 return -1;
195 }
196
197 /*
198 * Deal with ingress priority mapping.
199 */
200 skb->priority = vlan_get_ingress_priority(skb->dev, ntohs(vhdr->h_vlan_TCI));
201
202#ifdef VLAN_DEBUG
203 printk(VLAN_DBG "%s: priority: %lu for TCI: %hu (hbo)\n",
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900204 __FUNCTION__, (unsigned long)(skb->priority),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 ntohs(vhdr->h_vlan_TCI));
206#endif
207
208 /* The ethernet driver already did the pkt_type calculations
209 * for us...
210 */
211 switch (skb->pkt_type) {
212 case PACKET_BROADCAST: /* Yeah, stats collect these together.. */
213 // stats->broadcast ++; // no such counter :-(
214 break;
215
216 case PACKET_MULTICAST:
217 stats->multicast++;
218 break;
219
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900220 case PACKET_OTHERHOST:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 /* Our lower layer thinks this is not local, let's make sure.
222 * This allows the VLAN to have a different MAC than the underlying
223 * device, and still route correctly.
224 */
Kris Katterjohnd3f4a682006-01-09 16:01:43 -0800225 if (!compare_ether_addr(eth_hdr(skb)->h_dest, skb->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 /* It is for our (changed) MAC-address! */
227 skb->pkt_type = PACKET_HOST;
228 }
229 break;
230 default:
231 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 /* Was a VLAN packet, grab the encapsulated protocol, which the layer
235 * three protocols care about.
236 */
237 /* proto = get_unaligned(&vhdr->h_vlan_encapsulated_proto); */
238 proto = vhdr->h_vlan_encapsulated_proto;
239
240 skb->protocol = proto;
241 if (ntohs(proto) >= 1536) {
242 /* place it back on the queue to be handled by
243 * true layer 3 protocols.
244 */
245
246 /* See if we are configured to re-write the VLAN header
247 * to make it look like ethernet...
248 */
249 skb = vlan_check_reorder_header(skb);
250
251 /* Can be null if skb-clone fails when re-ordering */
252 if (skb) {
253 netif_rx(skb);
254 } else {
255 /* TODO: Add a more specific counter here. */
256 stats->rx_errors++;
257 }
258 rcu_read_unlock();
259 return 0;
260 }
261
262 rawp = skb->data;
263
264 /*
265 * This is a magic hack to spot IPX packets. Older Novell breaks
266 * the protocol design and runs IPX over 802.3 without an 802.2 LLC
267 * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
268 * won't work for fault tolerant netware but does for the rest.
269 */
270 if (*(unsigned short *)rawp == 0xFFFF) {
YOSHIFUJI Hideakib93b7ee2007-03-25 20:12:18 -0700271 skb->protocol = htons(ETH_P_802_3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 /* place it back on the queue to be handled by true layer 3 protocols.
273 */
274
275 /* See if we are configured to re-write the VLAN header
276 * to make it look like ethernet...
277 */
278 skb = vlan_check_reorder_header(skb);
279
280 /* Can be null if skb-clone fails when re-ordering */
281 if (skb) {
282 netif_rx(skb);
283 } else {
284 /* TODO: Add a more specific counter here. */
285 stats->rx_errors++;
286 }
287 rcu_read_unlock();
288 return 0;
289 }
290
291 /*
292 * Real 802.2 LLC
293 */
YOSHIFUJI Hideakib93b7ee2007-03-25 20:12:18 -0700294 skb->protocol = htons(ETH_P_802_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 /* place it back on the queue to be handled by upper layer protocols.
296 */
297
298 /* See if we are configured to re-write the VLAN header
299 * to make it look like ethernet...
300 */
301 skb = vlan_check_reorder_header(skb);
302
303 /* Can be null if skb-clone fails when re-ordering */
304 if (skb) {
305 netif_rx(skb);
306 } else {
307 /* TODO: Add a more specific counter here. */
308 stats->rx_errors++;
309 }
310 rcu_read_unlock();
311 return 0;
312}
313
314static inline unsigned short vlan_dev_get_egress_qos_mask(struct net_device* dev,
315 struct sk_buff* skb)
316{
317 struct vlan_priority_tci_mapping *mp =
318 VLAN_DEV_INFO(dev)->egress_priority_map[(skb->priority & 0xF)];
319
320 while (mp) {
321 if (mp->priority == skb->priority) {
322 return mp->vlan_qos; /* This should already be shifted to mask
323 * correctly with the VLAN's TCI
324 */
325 }
326 mp = mp->next;
327 }
328 return 0;
329}
330
331/*
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900332 * Create the VLAN header for an arbitrary protocol layer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 *
334 * saddr=NULL means use device source address
335 * daddr=NULL means leave destination address (eg unresolved arp)
336 *
337 * This is called when the SKB is moving down the stack towards the
338 * physical devices.
339 */
340int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900341 unsigned short type, void *daddr, void *saddr,
342 unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 struct vlan_hdr *vhdr;
345 unsigned short veth_TCI = 0;
346 int rc = 0;
347 int build_vlan_header = 0;
348 struct net_device *vdev = dev; /* save this for the bottom of the method */
349
350#ifdef VLAN_DEBUG
351 printk(VLAN_DBG "%s: skb: %p type: %hx len: %x vlan_id: %hx, daddr: %p\n",
352 __FUNCTION__, skb, type, len, VLAN_DEV_INFO(dev)->vlan_id, daddr);
353#endif
354
355 /* build vlan header only if re_order_header flag is NOT set. This
356 * fixes some programs that get confused when they see a VLAN device
357 * sending a frame that is VLAN encoded (the consensus is that the VLAN
358 * device should look completely like an Ethernet device when the
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900359 * REORDER_HEADER flag is set) The drawback to this is some extra
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 * header shuffling in the hard_start_xmit. Users can turn off this
361 * REORDER behaviour with the vconfig tool.
362 */
Patrick McHardya4bf3af42007-06-13 12:07:37 -0700363 if (!(VLAN_DEV_INFO(dev)->flags & VLAN_FLAG_REORDER_HDR))
364 build_vlan_header = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 if (build_vlan_header) {
367 vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN);
368
369 /* build the four bytes that make this a VLAN header. */
370
371 /* Now, construct the second two bytes. This field looks something
372 * like:
373 * usr_priority: 3 bits (high bits)
374 * CFI 1 bit
375 * VLAN ID 12 bits (low bits)
376 *
377 */
378 veth_TCI = VLAN_DEV_INFO(dev)->vlan_id;
379 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
380
381 vhdr->h_vlan_TCI = htons(veth_TCI);
382
383 /*
384 * Set the protocol type.
385 * For a packet of type ETH_P_802_3 we put the length in here instead.
386 * It is up to the 802.2 layer to carry protocol information.
387 */
388
389 if (type != ETH_P_802_3) {
390 vhdr->h_vlan_encapsulated_proto = htons(type);
391 } else {
392 vhdr->h_vlan_encapsulated_proto = htons(len);
393 }
Jerome Borsboom279e1722007-04-13 16:12:47 -0700394
395 skb->protocol = htons(ETH_P_8021Q);
David S. Millerbe8bd862007-04-19 20:34:51 -0700396 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398
399 /* Before delegating work to the lower layer, enter our MAC-address */
400 if (saddr == NULL)
401 saddr = dev->dev_addr;
402
403 dev = VLAN_DEV_INFO(dev)->real_dev;
404
405 /* MPLS can send us skbuffs w/out enough space. This check will grow the
406 * skb if it doesn't have enough headroom. Not a beautiful solution, so
407 * I'll tick a counter so that users can know it's happening... If they
408 * care...
409 */
410
411 /* NOTE: This may still break if the underlying device is not the final
412 * device (and thus there are more headers to add...) It should work for
413 * good-ole-ethernet though.
414 */
415 if (skb_headroom(skb) < dev->hard_header_len) {
416 struct sk_buff *sk_tmp = skb;
417 skb = skb_realloc_headroom(sk_tmp, dev->hard_header_len);
418 kfree_skb(sk_tmp);
419 if (skb == NULL) {
420 struct net_device_stats *stats = vlan_dev_get_stats(vdev);
421 stats->tx_dropped++;
422 return -ENOMEM;
423 }
424 VLAN_DEV_INFO(vdev)->cnt_inc_headroom_on_tx++;
425#ifdef VLAN_DEBUG
426 printk(VLAN_DBG "%s: %s: had to grow skb.\n", __FUNCTION__, vdev->name);
427#endif
428 }
429
430 if (build_vlan_header) {
431 /* Now make the underlying real hard header */
432 rc = dev->hard_header(skb, dev, ETH_P_8021Q, daddr, saddr, len + VLAN_HLEN);
433
434 if (rc > 0) {
435 rc += VLAN_HLEN;
436 } else if (rc < 0) {
437 rc -= VLAN_HLEN;
438 }
439 } else {
440 /* If here, then we'll just make a normal looking ethernet frame,
441 * but, the hard_start_xmit method will insert the tag (it has to
442 * be able to do this for bridged and other skbs that don't come
443 * down the protocol stack in an orderly manner.
444 */
445 rc = dev->hard_header(skb, dev, type, daddr, saddr, len);
446 }
447
448 return rc;
449}
450
451int vlan_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
452{
453 struct net_device_stats *stats = vlan_dev_get_stats(dev);
454 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
455
456 /* Handle non-VLAN frames if they are sent to us, for example by DHCP.
457 *
458 * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
459 * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
460 */
461
YOSHIFUJI Hideakib93b7ee2007-03-25 20:12:18 -0700462 if (veth->h_vlan_proto != htons(ETH_P_8021Q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 int orig_headroom = skb_headroom(skb);
464 unsigned short veth_TCI;
465
466 /* This is not a VLAN frame...but we can fix that! */
467 VLAN_DEV_INFO(dev)->cnt_encap_on_xmit++;
468
469#ifdef VLAN_DEBUG
470 printk(VLAN_DBG "%s: proto to encap: 0x%hx (hbo)\n",
471 __FUNCTION__, htons(veth->h_vlan_proto));
472#endif
473 /* Construct the second two bytes. This field looks something
474 * like:
475 * usr_priority: 3 bits (high bits)
476 * CFI 1 bit
477 * VLAN ID 12 bits (low bits)
478 */
479 veth_TCI = VLAN_DEV_INFO(dev)->vlan_id;
480 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
481
482 skb = __vlan_put_tag(skb, veth_TCI);
483 if (!skb) {
484 stats->tx_dropped++;
485 return 0;
486 }
487
488 if (orig_headroom < VLAN_HLEN) {
489 VLAN_DEV_INFO(dev)->cnt_inc_headroom_on_tx++;
490 }
491 }
492
493#ifdef VLAN_DEBUG
494 printk(VLAN_DBG "%s: about to send skb: %p to dev: %s\n",
495 __FUNCTION__, skb, skb->dev->name);
496 printk(VLAN_DBG " %2hx.%2hx.%2hx.%2xh.%2hx.%2hx %2hx.%2hx.%2hx.%2hx.%2hx.%2hx %4hx %4hx %4hx\n",
497 veth->h_dest[0], veth->h_dest[1], veth->h_dest[2], veth->h_dest[3], veth->h_dest[4], veth->h_dest[5],
498 veth->h_source[0], veth->h_source[1], veth->h_source[2], veth->h_source[3], veth->h_source[4], veth->h_source[5],
499 veth->h_vlan_proto, veth->h_vlan_TCI, veth->h_vlan_encapsulated_proto);
500#endif
501
502 stats->tx_packets++; /* for statics only */
503 stats->tx_bytes += skb->len;
504
505 skb->dev = VLAN_DEV_INFO(dev)->real_dev;
506 dev_queue_xmit(skb);
507
508 return 0;
509}
510
511int vlan_dev_hwaccel_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
512{
513 struct net_device_stats *stats = vlan_dev_get_stats(dev);
514 unsigned short veth_TCI;
515
516 /* Construct the second two bytes. This field looks something
517 * like:
518 * usr_priority: 3 bits (high bits)
519 * CFI 1 bit
520 * VLAN ID 12 bits (low bits)
521 */
522 veth_TCI = VLAN_DEV_INFO(dev)->vlan_id;
523 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
524 skb = __vlan_hwaccel_put_tag(skb, veth_TCI);
525
526 stats->tx_packets++;
527 stats->tx_bytes += skb->len;
528
529 skb->dev = VLAN_DEV_INFO(dev)->real_dev;
530 dev_queue_xmit(skb);
531
532 return 0;
533}
534
535int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
536{
537 /* TODO: gotta make sure the underlying layer can handle it,
538 * maybe an IFF_VLAN_CAPABLE flag for devices?
539 */
540 if (VLAN_DEV_INFO(dev)->real_dev->mtu < new_mtu)
541 return -ERANGE;
542
543 dev->mtu = new_mtu;
544
545 return 0;
546}
547
Patrick McHardyc17d8872007-06-13 12:05:22 -0700548void vlan_dev_set_ingress_priority(const struct net_device *dev,
549 u32 skb_prio, short vlan_prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Patrick McHardyb020cb42007-06-13 12:07:22 -0700551 struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev);
552
553 if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio)
554 vlan->nr_ingress_mappings--;
555 else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio)
556 vlan->nr_ingress_mappings++;
557
558 vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
Patrick McHardyc17d8872007-06-13 12:05:22 -0700561int vlan_dev_set_egress_priority(const struct net_device *dev,
562 u32 skb_prio, short vlan_prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Patrick McHardyb020cb42007-06-13 12:07:22 -0700564 struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 struct vlan_priority_tci_mapping *mp = NULL;
566 struct vlan_priority_tci_mapping *np;
Patrick McHardyb020cb42007-06-13 12:07:22 -0700567 u32 vlan_qos = (vlan_prio << 13) & 0xE000;
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900568
Patrick McHardyc17d8872007-06-13 12:05:22 -0700569 /* See if a priority mapping exists.. */
Patrick McHardyb020cb42007-06-13 12:07:22 -0700570 mp = vlan->egress_priority_map[skb_prio & 0xF];
Patrick McHardyc17d8872007-06-13 12:05:22 -0700571 while (mp) {
572 if (mp->priority == skb_prio) {
Patrick McHardyb020cb42007-06-13 12:07:22 -0700573 if (mp->vlan_qos && !vlan_qos)
574 vlan->nr_egress_mappings--;
575 else if (!mp->vlan_qos && vlan_qos)
576 vlan->nr_egress_mappings++;
577 mp->vlan_qos = vlan_qos;
Patrick McHardyc17d8872007-06-13 12:05:22 -0700578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
Patrick McHardyc17d8872007-06-13 12:05:22 -0700580 mp = mp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
Patrick McHardyc17d8872007-06-13 12:05:22 -0700582
583 /* Create a new mapping then. */
Patrick McHardyb020cb42007-06-13 12:07:22 -0700584 mp = vlan->egress_priority_map[skb_prio & 0xF];
Patrick McHardyc17d8872007-06-13 12:05:22 -0700585 np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL);
586 if (!np)
587 return -ENOBUFS;
588
589 np->next = mp;
590 np->priority = skb_prio;
Patrick McHardyb020cb42007-06-13 12:07:22 -0700591 np->vlan_qos = vlan_qos;
592 vlan->egress_priority_map[skb_prio & 0xF] = np;
593 if (vlan_qos)
594 vlan->nr_egress_mappings++;
Patrick McHardyc17d8872007-06-13 12:05:22 -0700595 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
Patrick McHardya4bf3af42007-06-13 12:07:37 -0700598/* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */
Patrick McHardyc17d8872007-06-13 12:05:22 -0700599int vlan_dev_set_vlan_flag(const struct net_device *dev,
600 u32 flag, short flag_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Patrick McHardyc17d8872007-06-13 12:05:22 -0700602 /* verify flag is supported */
Patrick McHardya4bf3af42007-06-13 12:07:37 -0700603 if (flag == VLAN_FLAG_REORDER_HDR) {
Patrick McHardyc17d8872007-06-13 12:05:22 -0700604 if (flag_val) {
Patrick McHardya4bf3af42007-06-13 12:07:37 -0700605 VLAN_DEV_INFO(dev)->flags |= VLAN_FLAG_REORDER_HDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 } else {
Patrick McHardya4bf3af42007-06-13 12:07:37 -0700607 VLAN_DEV_INFO(dev)->flags &= ~VLAN_FLAG_REORDER_HDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
Patrick McHardyc17d8872007-06-13 12:05:22 -0700609 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
Patrick McHardyc17d8872007-06-13 12:05:22 -0700611 printk(KERN_ERR "%s: flag %i is not valid.\n", __FUNCTION__, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return -EINVAL;
613}
614
Patrick McHardyc17d8872007-06-13 12:05:22 -0700615void vlan_dev_get_realdev_name(const struct net_device *dev, char *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Patrick McHardyc17d8872007-06-13 12:05:22 -0700617 strncpy(result, VLAN_DEV_INFO(dev)->real_dev->name, 23);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Patrick McHardyc17d8872007-06-13 12:05:22 -0700620void vlan_dev_get_vid(const struct net_device *dev, unsigned short *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Patrick McHardyc17d8872007-06-13 12:05:22 -0700622 *result = VLAN_DEV_INFO(dev)->vlan_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625int vlan_dev_open(struct net_device *dev)
626{
Patrick McHardy8c979c22007-07-11 19:45:24 -0700627 struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev);
628 struct net_device *real_dev = vlan->real_dev;
629 int err;
630
631 if (!(real_dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return -ENETDOWN;
633
Patrick McHardy8c979c22007-07-11 19:45:24 -0700634 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr)) {
635 err = dev_unicast_add(real_dev, dev->dev_addr, ETH_ALEN);
636 if (err < 0)
637 return err;
638 }
639 memcpy(vlan->real_dev_addr, real_dev->dev_addr, ETH_ALEN);
640
Patrick McHardy6c78dcb2007-07-14 18:52:56 -0700641 if (dev->flags & IFF_ALLMULTI)
642 dev_set_allmulti(real_dev, 1);
643 if (dev->flags & IFF_PROMISC)
644 dev_set_promiscuity(real_dev, 1);
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return 0;
647}
648
649int vlan_dev_stop(struct net_device *dev)
650{
Patrick McHardy8c979c22007-07-11 19:45:24 -0700651 struct net_device *real_dev = VLAN_DEV_INFO(dev)->real_dev;
652
Patrick McHardy56addd62007-07-14 18:53:28 -0700653 dev_mc_unsync(real_dev, dev);
Patrick McHardy6c78dcb2007-07-14 18:52:56 -0700654 if (dev->flags & IFF_ALLMULTI)
655 dev_set_allmulti(real_dev, -1);
656 if (dev->flags & IFF_PROMISC)
657 dev_set_promiscuity(real_dev, -1);
658
Patrick McHardy8c979c22007-07-11 19:45:24 -0700659 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
660 dev_unicast_delete(real_dev, dev->dev_addr, dev->addr_len);
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return 0;
663}
664
665int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
666{
667 struct net_device *real_dev = VLAN_DEV_INFO(dev)->real_dev;
668 struct ifreq ifrr;
669 int err = -EOPNOTSUPP;
670
671 strncpy(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
672 ifrr.ifr_ifru = ifr->ifr_ifru;
673
674 switch(cmd) {
675 case SIOCGMIIPHY:
676 case SIOCGMIIREG:
677 case SIOCSMIIREG:
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900678 if (real_dev->do_ioctl && netif_device_present(real_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 err = real_dev->do_ioctl(real_dev, &ifrr, cmd);
680 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900683 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 ifr->ifr_ifru = ifrr.ifr_ifru;
685
686 return err;
687}
688
Patrick McHardy6c78dcb2007-07-14 18:52:56 -0700689void vlan_change_rx_flags(struct net_device *dev, int change)
690{
691 struct net_device *real_dev = VLAN_DEV_INFO(dev)->real_dev;
692
693 if (change & IFF_ALLMULTI)
694 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
695 if (change & IFF_PROMISC)
696 dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1);
697}
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699/** Taken from Gleb + Lennert's VLAN code, and modified... */
700void vlan_dev_set_multicast_list(struct net_device *vlan_dev)
701{
Patrick McHardy56addd62007-07-14 18:53:28 -0700702 dev_mc_sync(VLAN_DEV_INFO(vlan_dev)->real_dev, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}