blob: 95afe387b95296ca3e1ffb1006ffa80d0b8849f4 [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;
119 struct vlan_hdr *vhdr = (struct vlan_hdr *)(skb->data);
120 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
125 /* vlan_TCI = ntohs(get_unaligned(&vhdr->h_vlan_TCI)); */
126 vlan_TCI = ntohs(vhdr->h_vlan_TCI);
127
128 vid = (vlan_TCI & VLAN_VID_MASK);
129
130#ifdef VLAN_DEBUG
131 printk(VLAN_DBG "%s: skb: %p vlan_id: %hx\n",
132 __FUNCTION__, skb, vid);
133#endif
134
135 /* Ok, we will find the correct VLAN device, strip the header,
136 * and then go on as usual.
137 */
138
139 /* We have 12 bits of vlan ID.
140 *
141 * We must not drop allow preempt until we hold a
142 * reference to the device (netif_rx does that) or we
143 * fail.
144 */
145
146 rcu_read_lock();
147 skb->dev = __find_vlan_dev(dev, vid);
148 if (!skb->dev) {
149 rcu_read_unlock();
150
151#ifdef VLAN_DEBUG
152 printk(VLAN_DBG "%s: ERROR: No net_device for VID: %i on dev: %s [%i]\n",
153 __FUNCTION__, (unsigned int)(vid), dev->name, dev->ifindex);
154#endif
155 kfree_skb(skb);
156 return -1;
157 }
158
159 skb->dev->last_rx = jiffies;
160
161 /* Bump the rx counters for the VLAN device. */
162 stats = vlan_dev_get_stats(skb->dev);
163 stats->rx_packets++;
164 stats->rx_bytes += skb->len;
165
Herbert Xucbb042f2006-03-20 22:43:56 -0800166 /* Take off the VLAN header (4 bytes currently) */
167 skb_pull_rcsum(skb, VLAN_HLEN);
Stephen Hemmingera3884422005-12-14 16:23:16 -0800168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 /* Ok, lets check to make sure the device (dev) we
170 * came in on is what this VLAN is attached to.
171 */
172
173 if (dev != VLAN_DEV_INFO(skb->dev)->real_dev) {
174 rcu_read_unlock();
175
176#ifdef VLAN_DEBUG
177 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 +0900178 __FUNCTION__, skb, dev->name,
179 VLAN_DEV_INFO(skb->dev)->real_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 skb->dev->name);
181#endif
182 kfree_skb(skb);
183 stats->rx_errors++;
184 return -1;
185 }
186
187 /*
188 * Deal with ingress priority mapping.
189 */
190 skb->priority = vlan_get_ingress_priority(skb->dev, ntohs(vhdr->h_vlan_TCI));
191
192#ifdef VLAN_DEBUG
193 printk(VLAN_DBG "%s: priority: %lu for TCI: %hu (hbo)\n",
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900194 __FUNCTION__, (unsigned long)(skb->priority),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 ntohs(vhdr->h_vlan_TCI));
196#endif
197
198 /* The ethernet driver already did the pkt_type calculations
199 * for us...
200 */
201 switch (skb->pkt_type) {
202 case PACKET_BROADCAST: /* Yeah, stats collect these together.. */
203 // stats->broadcast ++; // no such counter :-(
204 break;
205
206 case PACKET_MULTICAST:
207 stats->multicast++;
208 break;
209
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900210 case PACKET_OTHERHOST:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /* Our lower layer thinks this is not local, let's make sure.
212 * This allows the VLAN to have a different MAC than the underlying
213 * device, and still route correctly.
214 */
Kris Katterjohnd3f4a682006-01-09 16:01:43 -0800215 if (!compare_ether_addr(eth_hdr(skb)->h_dest, skb->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 /* It is for our (changed) MAC-address! */
217 skb->pkt_type = PACKET_HOST;
218 }
219 break;
220 default:
221 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 /* Was a VLAN packet, grab the encapsulated protocol, which the layer
225 * three protocols care about.
226 */
227 /* proto = get_unaligned(&vhdr->h_vlan_encapsulated_proto); */
228 proto = vhdr->h_vlan_encapsulated_proto;
229
230 skb->protocol = proto;
231 if (ntohs(proto) >= 1536) {
232 /* place it back on the queue to be handled by
233 * true layer 3 protocols.
234 */
235
236 /* See if we are configured to re-write the VLAN header
237 * to make it look like ethernet...
238 */
239 skb = vlan_check_reorder_header(skb);
240
241 /* Can be null if skb-clone fails when re-ordering */
242 if (skb) {
243 netif_rx(skb);
244 } else {
245 /* TODO: Add a more specific counter here. */
246 stats->rx_errors++;
247 }
248 rcu_read_unlock();
249 return 0;
250 }
251
252 rawp = skb->data;
253
254 /*
255 * This is a magic hack to spot IPX packets. Older Novell breaks
256 * the protocol design and runs IPX over 802.3 without an 802.2 LLC
257 * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
258 * won't work for fault tolerant netware but does for the rest.
259 */
260 if (*(unsigned short *)rawp == 0xFFFF) {
YOSHIFUJI Hideakib93b7ee2007-03-25 20:12:18 -0700261 skb->protocol = htons(ETH_P_802_3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 /* place it back on the queue to be handled by true layer 3 protocols.
263 */
264
265 /* See if we are configured to re-write the VLAN header
266 * to make it look like ethernet...
267 */
268 skb = vlan_check_reorder_header(skb);
269
270 /* Can be null if skb-clone fails when re-ordering */
271 if (skb) {
272 netif_rx(skb);
273 } else {
274 /* TODO: Add a more specific counter here. */
275 stats->rx_errors++;
276 }
277 rcu_read_unlock();
278 return 0;
279 }
280
281 /*
282 * Real 802.2 LLC
283 */
YOSHIFUJI Hideakib93b7ee2007-03-25 20:12:18 -0700284 skb->protocol = htons(ETH_P_802_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 /* place it back on the queue to be handled by upper layer protocols.
286 */
287
288 /* See if we are configured to re-write the VLAN header
289 * to make it look like ethernet...
290 */
291 skb = vlan_check_reorder_header(skb);
292
293 /* Can be null if skb-clone fails when re-ordering */
294 if (skb) {
295 netif_rx(skb);
296 } else {
297 /* TODO: Add a more specific counter here. */
298 stats->rx_errors++;
299 }
300 rcu_read_unlock();
301 return 0;
302}
303
304static inline unsigned short vlan_dev_get_egress_qos_mask(struct net_device* dev,
305 struct sk_buff* skb)
306{
307 struct vlan_priority_tci_mapping *mp =
308 VLAN_DEV_INFO(dev)->egress_priority_map[(skb->priority & 0xF)];
309
310 while (mp) {
311 if (mp->priority == skb->priority) {
312 return mp->vlan_qos; /* This should already be shifted to mask
313 * correctly with the VLAN's TCI
314 */
315 }
316 mp = mp->next;
317 }
318 return 0;
319}
320
321/*
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900322 * Create the VLAN header for an arbitrary protocol layer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 *
324 * saddr=NULL means use device source address
325 * daddr=NULL means leave destination address (eg unresolved arp)
326 *
327 * This is called when the SKB is moving down the stack towards the
328 * physical devices.
329 */
330int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900331 unsigned short type, void *daddr, void *saddr,
332 unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 struct vlan_hdr *vhdr;
335 unsigned short veth_TCI = 0;
336 int rc = 0;
337 int build_vlan_header = 0;
338 struct net_device *vdev = dev; /* save this for the bottom of the method */
339
340#ifdef VLAN_DEBUG
341 printk(VLAN_DBG "%s: skb: %p type: %hx len: %x vlan_id: %hx, daddr: %p\n",
342 __FUNCTION__, skb, type, len, VLAN_DEV_INFO(dev)->vlan_id, daddr);
343#endif
344
345 /* build vlan header only if re_order_header flag is NOT set. This
346 * fixes some programs that get confused when they see a VLAN device
347 * sending a frame that is VLAN encoded (the consensus is that the VLAN
348 * device should look completely like an Ethernet device when the
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900349 * REORDER_HEADER flag is set) The drawback to this is some extra
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 * header shuffling in the hard_start_xmit. Users can turn off this
351 * REORDER behaviour with the vconfig tool.
352 */
Patrick McHardya4bf3af42007-06-13 12:07:37 -0700353 if (!(VLAN_DEV_INFO(dev)->flags & VLAN_FLAG_REORDER_HDR))
354 build_vlan_header = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 if (build_vlan_header) {
357 vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN);
358
359 /* build the four bytes that make this a VLAN header. */
360
361 /* Now, construct the second two bytes. This field looks something
362 * like:
363 * usr_priority: 3 bits (high bits)
364 * CFI 1 bit
365 * VLAN ID 12 bits (low bits)
366 *
367 */
368 veth_TCI = VLAN_DEV_INFO(dev)->vlan_id;
369 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
370
371 vhdr->h_vlan_TCI = htons(veth_TCI);
372
373 /*
374 * Set the protocol type.
375 * For a packet of type ETH_P_802_3 we put the length in here instead.
376 * It is up to the 802.2 layer to carry protocol information.
377 */
378
379 if (type != ETH_P_802_3) {
380 vhdr->h_vlan_encapsulated_proto = htons(type);
381 } else {
382 vhdr->h_vlan_encapsulated_proto = htons(len);
383 }
Jerome Borsboom279e1722007-04-13 16:12:47 -0700384
385 skb->protocol = htons(ETH_P_8021Q);
David S. Millerbe8bd862007-04-19 20:34:51 -0700386 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
389 /* Before delegating work to the lower layer, enter our MAC-address */
390 if (saddr == NULL)
391 saddr = dev->dev_addr;
392
393 dev = VLAN_DEV_INFO(dev)->real_dev;
394
395 /* MPLS can send us skbuffs w/out enough space. This check will grow the
396 * skb if it doesn't have enough headroom. Not a beautiful solution, so
397 * I'll tick a counter so that users can know it's happening... If they
398 * care...
399 */
400
401 /* NOTE: This may still break if the underlying device is not the final
402 * device (and thus there are more headers to add...) It should work for
403 * good-ole-ethernet though.
404 */
405 if (skb_headroom(skb) < dev->hard_header_len) {
406 struct sk_buff *sk_tmp = skb;
407 skb = skb_realloc_headroom(sk_tmp, dev->hard_header_len);
408 kfree_skb(sk_tmp);
409 if (skb == NULL) {
410 struct net_device_stats *stats = vlan_dev_get_stats(vdev);
411 stats->tx_dropped++;
412 return -ENOMEM;
413 }
414 VLAN_DEV_INFO(vdev)->cnt_inc_headroom_on_tx++;
415#ifdef VLAN_DEBUG
416 printk(VLAN_DBG "%s: %s: had to grow skb.\n", __FUNCTION__, vdev->name);
417#endif
418 }
419
420 if (build_vlan_header) {
421 /* Now make the underlying real hard header */
422 rc = dev->hard_header(skb, dev, ETH_P_8021Q, daddr, saddr, len + VLAN_HLEN);
423
424 if (rc > 0) {
425 rc += VLAN_HLEN;
426 } else if (rc < 0) {
427 rc -= VLAN_HLEN;
428 }
429 } else {
430 /* If here, then we'll just make a normal looking ethernet frame,
431 * but, the hard_start_xmit method will insert the tag (it has to
432 * be able to do this for bridged and other skbs that don't come
433 * down the protocol stack in an orderly manner.
434 */
435 rc = dev->hard_header(skb, dev, type, daddr, saddr, len);
436 }
437
438 return rc;
439}
440
441int vlan_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
442{
443 struct net_device_stats *stats = vlan_dev_get_stats(dev);
444 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
445
446 /* Handle non-VLAN frames if they are sent to us, for example by DHCP.
447 *
448 * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
449 * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
450 */
451
YOSHIFUJI Hideakib93b7ee2007-03-25 20:12:18 -0700452 if (veth->h_vlan_proto != htons(ETH_P_8021Q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 int orig_headroom = skb_headroom(skb);
454 unsigned short veth_TCI;
455
456 /* This is not a VLAN frame...but we can fix that! */
457 VLAN_DEV_INFO(dev)->cnt_encap_on_xmit++;
458
459#ifdef VLAN_DEBUG
460 printk(VLAN_DBG "%s: proto to encap: 0x%hx (hbo)\n",
461 __FUNCTION__, htons(veth->h_vlan_proto));
462#endif
463 /* Construct the second two bytes. This field looks something
464 * like:
465 * usr_priority: 3 bits (high bits)
466 * CFI 1 bit
467 * VLAN ID 12 bits (low bits)
468 */
469 veth_TCI = VLAN_DEV_INFO(dev)->vlan_id;
470 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
471
472 skb = __vlan_put_tag(skb, veth_TCI);
473 if (!skb) {
474 stats->tx_dropped++;
475 return 0;
476 }
477
478 if (orig_headroom < VLAN_HLEN) {
479 VLAN_DEV_INFO(dev)->cnt_inc_headroom_on_tx++;
480 }
481 }
482
483#ifdef VLAN_DEBUG
484 printk(VLAN_DBG "%s: about to send skb: %p to dev: %s\n",
485 __FUNCTION__, skb, skb->dev->name);
486 printk(VLAN_DBG " %2hx.%2hx.%2hx.%2xh.%2hx.%2hx %2hx.%2hx.%2hx.%2hx.%2hx.%2hx %4hx %4hx %4hx\n",
487 veth->h_dest[0], veth->h_dest[1], veth->h_dest[2], veth->h_dest[3], veth->h_dest[4], veth->h_dest[5],
488 veth->h_source[0], veth->h_source[1], veth->h_source[2], veth->h_source[3], veth->h_source[4], veth->h_source[5],
489 veth->h_vlan_proto, veth->h_vlan_TCI, veth->h_vlan_encapsulated_proto);
490#endif
491
492 stats->tx_packets++; /* for statics only */
493 stats->tx_bytes += skb->len;
494
495 skb->dev = VLAN_DEV_INFO(dev)->real_dev;
496 dev_queue_xmit(skb);
497
498 return 0;
499}
500
501int vlan_dev_hwaccel_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
502{
503 struct net_device_stats *stats = vlan_dev_get_stats(dev);
504 unsigned short veth_TCI;
505
506 /* Construct the second two bytes. This field looks something
507 * like:
508 * usr_priority: 3 bits (high bits)
509 * CFI 1 bit
510 * VLAN ID 12 bits (low bits)
511 */
512 veth_TCI = VLAN_DEV_INFO(dev)->vlan_id;
513 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
514 skb = __vlan_hwaccel_put_tag(skb, veth_TCI);
515
516 stats->tx_packets++;
517 stats->tx_bytes += skb->len;
518
519 skb->dev = VLAN_DEV_INFO(dev)->real_dev;
520 dev_queue_xmit(skb);
521
522 return 0;
523}
524
525int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
526{
527 /* TODO: gotta make sure the underlying layer can handle it,
528 * maybe an IFF_VLAN_CAPABLE flag for devices?
529 */
530 if (VLAN_DEV_INFO(dev)->real_dev->mtu < new_mtu)
531 return -ERANGE;
532
533 dev->mtu = new_mtu;
534
535 return 0;
536}
537
Patrick McHardyc17d8872007-06-13 12:05:22 -0700538void vlan_dev_set_ingress_priority(const struct net_device *dev,
539 u32 skb_prio, short vlan_prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Patrick McHardyb020cb42007-06-13 12:07:22 -0700541 struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev);
542
543 if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio)
544 vlan->nr_ingress_mappings--;
545 else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio)
546 vlan->nr_ingress_mappings++;
547
548 vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Patrick McHardyc17d8872007-06-13 12:05:22 -0700551int vlan_dev_set_egress_priority(const struct net_device *dev,
552 u32 skb_prio, short vlan_prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Patrick McHardyb020cb42007-06-13 12:07:22 -0700554 struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 struct vlan_priority_tci_mapping *mp = NULL;
556 struct vlan_priority_tci_mapping *np;
Patrick McHardyb020cb42007-06-13 12:07:22 -0700557 u32 vlan_qos = (vlan_prio << 13) & 0xE000;
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900558
Patrick McHardyc17d8872007-06-13 12:05:22 -0700559 /* See if a priority mapping exists.. */
Patrick McHardyb020cb42007-06-13 12:07:22 -0700560 mp = vlan->egress_priority_map[skb_prio & 0xF];
Patrick McHardyc17d8872007-06-13 12:05:22 -0700561 while (mp) {
562 if (mp->priority == skb_prio) {
Patrick McHardyb020cb42007-06-13 12:07:22 -0700563 if (mp->vlan_qos && !vlan_qos)
564 vlan->nr_egress_mappings--;
565 else if (!mp->vlan_qos && vlan_qos)
566 vlan->nr_egress_mappings++;
567 mp->vlan_qos = vlan_qos;
Patrick McHardyc17d8872007-06-13 12:05:22 -0700568 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
Patrick McHardyc17d8872007-06-13 12:05:22 -0700570 mp = mp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
Patrick McHardyc17d8872007-06-13 12:05:22 -0700572
573 /* Create a new mapping then. */
Patrick McHardyb020cb42007-06-13 12:07:22 -0700574 mp = vlan->egress_priority_map[skb_prio & 0xF];
Patrick McHardyc17d8872007-06-13 12:05:22 -0700575 np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL);
576 if (!np)
577 return -ENOBUFS;
578
579 np->next = mp;
580 np->priority = skb_prio;
Patrick McHardyb020cb42007-06-13 12:07:22 -0700581 np->vlan_qos = vlan_qos;
582 vlan->egress_priority_map[skb_prio & 0xF] = np;
583 if (vlan_qos)
584 vlan->nr_egress_mappings++;
Patrick McHardyc17d8872007-06-13 12:05:22 -0700585 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
Patrick McHardya4bf3af42007-06-13 12:07:37 -0700588/* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */
Patrick McHardyc17d8872007-06-13 12:05:22 -0700589int vlan_dev_set_vlan_flag(const struct net_device *dev,
590 u32 flag, short flag_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Patrick McHardyc17d8872007-06-13 12:05:22 -0700592 /* verify flag is supported */
Patrick McHardya4bf3af42007-06-13 12:07:37 -0700593 if (flag == VLAN_FLAG_REORDER_HDR) {
Patrick McHardyc17d8872007-06-13 12:05:22 -0700594 if (flag_val) {
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 } else {
Patrick McHardya4bf3af42007-06-13 12:07:37 -0700597 VLAN_DEV_INFO(dev)->flags &= ~VLAN_FLAG_REORDER_HDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
Patrick McHardyc17d8872007-06-13 12:05:22 -0700599 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
Patrick McHardyc17d8872007-06-13 12:05:22 -0700601 printk(KERN_ERR "%s: flag %i is not valid.\n", __FUNCTION__, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return -EINVAL;
603}
604
Patrick McHardyc17d8872007-06-13 12:05:22 -0700605void vlan_dev_get_realdev_name(const struct net_device *dev, char *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Patrick McHardyc17d8872007-06-13 12:05:22 -0700607 strncpy(result, VLAN_DEV_INFO(dev)->real_dev->name, 23);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
Patrick McHardyc17d8872007-06-13 12:05:22 -0700610void vlan_dev_get_vid(const struct net_device *dev, unsigned short *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Patrick McHardyc17d8872007-06-13 12:05:22 -0700612 *result = VLAN_DEV_INFO(dev)->vlan_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615int vlan_dev_set_mac_address(struct net_device *dev, void *addr_struct_p)
616{
617 struct sockaddr *addr = (struct sockaddr *)(addr_struct_p);
618 int i;
619
620 if (netif_running(dev))
621 return -EBUSY;
622
623 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
624
625 printk("%s: Setting MAC address to ", dev->name);
626 for (i = 0; i < 6; i++)
627 printk(" %2.2x", dev->dev_addr[i]);
628 printk(".\n");
629
630 if (memcmp(VLAN_DEV_INFO(dev)->real_dev->dev_addr,
631 dev->dev_addr,
632 dev->addr_len) != 0) {
633 if (!(VLAN_DEV_INFO(dev)->real_dev->flags & IFF_PROMISC)) {
634 int flgs = VLAN_DEV_INFO(dev)->real_dev->flags;
635
636 /* Increment our in-use promiscuity counter */
637 dev_set_promiscuity(VLAN_DEV_INFO(dev)->real_dev, 1);
638
639 /* Make PROMISC visible to the user. */
640 flgs |= IFF_PROMISC;
641 printk("VLAN (%s): Setting underlying device (%s) to promiscious mode.\n",
642 dev->name, VLAN_DEV_INFO(dev)->real_dev->name);
643 dev_change_flags(VLAN_DEV_INFO(dev)->real_dev, flgs);
644 }
645 } else {
646 printk("VLAN (%s): Underlying device (%s) has same MAC, not checking promiscious mode.\n",
647 dev->name, VLAN_DEV_INFO(dev)->real_dev->name);
648 }
649
650 return 0;
651}
652
653static inline int vlan_dmi_equals(struct dev_mc_list *dmi1,
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900654 struct dev_mc_list *dmi2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
656 return ((dmi1->dmi_addrlen == dmi2->dmi_addrlen) &&
657 (memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0));
658}
659
660/** dmi is a single entry into a dev_mc_list, a single node. mc_list is
661 * an entire list, and we'll iterate through it.
662 */
663static int vlan_should_add_mc(struct dev_mc_list *dmi, struct dev_mc_list *mc_list)
664{
665 struct dev_mc_list *idmi;
666
667 for (idmi = mc_list; idmi != NULL; ) {
668 if (vlan_dmi_equals(dmi, idmi)) {
669 if (dmi->dmi_users > idmi->dmi_users)
670 return 1;
671 else
672 return 0;
673 } else {
674 idmi = idmi->next;
675 }
676 }
677
678 return 1;
679}
680
681static inline void vlan_destroy_mc_list(struct dev_mc_list *mc_list)
682{
683 struct dev_mc_list *dmi = mc_list;
684 struct dev_mc_list *next;
685
686 while(dmi) {
687 next = dmi->next;
688 kfree(dmi);
689 dmi = next;
690 }
691}
692
693static void vlan_copy_mc_list(struct dev_mc_list *mc_list, struct vlan_dev_info *vlan_info)
694{
695 struct dev_mc_list *dmi, *new_dmi;
696
697 vlan_destroy_mc_list(vlan_info->old_mc_list);
698 vlan_info->old_mc_list = NULL;
699
700 for (dmi = mc_list; dmi != NULL; dmi = dmi->next) {
701 new_dmi = kmalloc(sizeof(*new_dmi), GFP_ATOMIC);
702 if (new_dmi == NULL) {
703 printk(KERN_ERR "vlan: cannot allocate memory. "
704 "Multicast may not work properly from now.\n");
705 return;
706 }
707
708 /* Copy whole structure, then make new 'next' pointer */
709 *new_dmi = *dmi;
710 new_dmi->next = vlan_info->old_mc_list;
711 vlan_info->old_mc_list = new_dmi;
712 }
713}
714
715static void vlan_flush_mc_list(struct net_device *dev)
716{
717 struct dev_mc_list *dmi = dev->mc_list;
718
719 while (dmi) {
720 printk(KERN_DEBUG "%s: del %.2x:%.2x:%.2x:%.2x:%.2x:%.2x mcast address from vlan interface\n",
721 dev->name,
722 dmi->dmi_addr[0],
723 dmi->dmi_addr[1],
724 dmi->dmi_addr[2],
725 dmi->dmi_addr[3],
726 dmi->dmi_addr[4],
727 dmi->dmi_addr[5]);
728 dev_mc_delete(dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
729 dmi = dev->mc_list;
730 }
731
732 /* dev->mc_list is NULL by the time we get here. */
733 vlan_destroy_mc_list(VLAN_DEV_INFO(dev)->old_mc_list);
734 VLAN_DEV_INFO(dev)->old_mc_list = NULL;
735}
736
737int vlan_dev_open(struct net_device *dev)
738{
739 if (!(VLAN_DEV_INFO(dev)->real_dev->flags & IFF_UP))
740 return -ENETDOWN;
741
742 return 0;
743}
744
745int vlan_dev_stop(struct net_device *dev)
746{
747 vlan_flush_mc_list(dev);
748 return 0;
749}
750
751int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
752{
753 struct net_device *real_dev = VLAN_DEV_INFO(dev)->real_dev;
754 struct ifreq ifrr;
755 int err = -EOPNOTSUPP;
756
757 strncpy(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
758 ifrr.ifr_ifru = ifr->ifr_ifru;
759
760 switch(cmd) {
761 case SIOCGMIIPHY:
762 case SIOCGMIIREG:
763 case SIOCSMIIREG:
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900764 if (real_dev->do_ioctl && netif_device_present(real_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 err = real_dev->do_ioctl(real_dev, &ifrr, cmd);
766 break;
767
768 case SIOCETHTOOL:
769 err = dev_ethtool(&ifrr);
770 }
771
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900772 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 ifr->ifr_ifru = ifrr.ifr_ifru;
774
775 return err;
776}
777
778/** Taken from Gleb + Lennert's VLAN code, and modified... */
779void vlan_dev_set_multicast_list(struct net_device *vlan_dev)
780{
781 struct dev_mc_list *dmi;
782 struct net_device *real_dev;
783 int inc;
784
785 if (vlan_dev && (vlan_dev->priv_flags & IFF_802_1Q_VLAN)) {
786 /* Then it's a real vlan device, as far as we can tell.. */
787 real_dev = VLAN_DEV_INFO(vlan_dev)->real_dev;
788
789 /* compare the current promiscuity to the last promisc we had.. */
790 inc = vlan_dev->promiscuity - VLAN_DEV_INFO(vlan_dev)->old_promiscuity;
791 if (inc) {
792 printk(KERN_INFO "%s: dev_set_promiscuity(master, %d)\n",
793 vlan_dev->name, inc);
794 dev_set_promiscuity(real_dev, inc); /* found in dev.c */
795 VLAN_DEV_INFO(vlan_dev)->old_promiscuity = vlan_dev->promiscuity;
796 }
797
798 inc = vlan_dev->allmulti - VLAN_DEV_INFO(vlan_dev)->old_allmulti;
799 if (inc) {
800 printk(KERN_INFO "%s: dev_set_allmulti(master, %d)\n",
801 vlan_dev->name, inc);
802 dev_set_allmulti(real_dev, inc); /* dev.c */
803 VLAN_DEV_INFO(vlan_dev)->old_allmulti = vlan_dev->allmulti;
804 }
805
806 /* looking for addresses to add to master's list */
807 for (dmi = vlan_dev->mc_list; dmi != NULL; dmi = dmi->next) {
808 if (vlan_should_add_mc(dmi, VLAN_DEV_INFO(vlan_dev)->old_mc_list)) {
809 dev_mc_add(real_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
810 printk(KERN_DEBUG "%s: add %.2x:%.2x:%.2x:%.2x:%.2x:%.2x mcast address to master interface\n",
811 vlan_dev->name,
812 dmi->dmi_addr[0],
813 dmi->dmi_addr[1],
814 dmi->dmi_addr[2],
815 dmi->dmi_addr[3],
816 dmi->dmi_addr[4],
817 dmi->dmi_addr[5]);
818 }
819 }
820
821 /* looking for addresses to delete from master's list */
822 for (dmi = VLAN_DEV_INFO(vlan_dev)->old_mc_list; dmi != NULL; dmi = dmi->next) {
823 if (vlan_should_add_mc(dmi, vlan_dev->mc_list)) {
824 /* if we think we should add it to the new list, then we should really
825 * delete it from the real list on the underlying device.
826 */
827 dev_mc_delete(real_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
828 printk(KERN_DEBUG "%s: del %.2x:%.2x:%.2x:%.2x:%.2x:%.2x mcast address from master interface\n",
829 vlan_dev->name,
830 dmi->dmi_addr[0],
831 dmi->dmi_addr[1],
832 dmi->dmi_addr[2],
833 dmi->dmi_addr[3],
834 dmi->dmi_addr[4],
835 dmi->dmi_addr[5]);
836 }
837 }
838
839 /* save multicast list */
840 vlan_copy_mc_list(vlan_dev->mc_list, VLAN_DEV_INFO(vlan_dev));
841 }
842}