1 /* -*- 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
8  *
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.
15  *
16  *
17  *		This program is free software; you can redistribute it and/or
18  *		modify it under the terms of the GNU General Public License
19  *		as published by the Free Software Foundation; either version
20  *		2 of the License, or (at your option) any later version.
21  */
22 
23 #include <linux/module.h>
24 #include <linux/mm.h>
25 #include <linux/in.h>
26 #include <linux/init.h>
27 #include <asm/uaccess.h> /* for copy_from_user */
28 #include <linux/skbuff.h>
29 #include <linux/netdevice.h>
30 #include <linux/etherdevice.h>
31 #include <net/datalink.h>
32 #include <net/p8022.h>
33 #include <net/arp.h>
34 #include <linux/brlock.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  */
vlan_dev_rebuild_header(struct sk_buff * skb)51 int vlan_dev_rebuild_header(struct sk_buff *skb)
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);
62 #endif
63 	default:
64 		printk(VLAN_DBG
65 		       "%s: unable to resolve type %X addresses.\n",
66 		       dev->name, (int)veth->h_vlan_encapsulated_proto);
67 
68 		memcpy(veth->h_source, dev->dev_addr, ETH_ALEN);
69 		break;
70 	};
71 
72 	return 0;
73 }
74 
vlan_check_reorder_header(struct sk_buff * skb)75 static inline struct sk_buff *vlan_check_reorder_header(struct sk_buff *skb)
76 {
77 	if (VLAN_DEV_INFO(skb->dev)->flags & 1) {
78 		if (skb_shared(skb) || skb_cloned(skb)) {
79 			struct sk_buff *nskb = skb_copy(skb, GFP_ATOMIC);
80 			kfree_skb(skb);
81 			skb = nskb;
82 		}
83 		if (skb) {
84 			/* Lifted from Gleb's VLAN code... */
85 			memmove(skb->data - ETH_HLEN,
86 				skb->data - VLAN_ETH_HLEN, 12);
87 			skb->mac.raw += VLAN_HLEN;
88 		}
89 	}
90 
91 	return skb;
92 }
93 
94 /*
95  *	Determine the packet's protocol ID. The rule here is that we
96  *	assume 802.3 if the type field is short enough to be a length.
97  *	This is normal practice and works for any 'now in use' protocol.
98  *
99  *  Also, at this point we assume that we ARE dealing exclusively with
100  *  VLAN packets, or packets that should be made into VLAN packets based
101  *  on a default VLAN ID.
102  *
103  *  NOTE:  Should be similar to ethernet/eth.c.
104  *
105  *  SANITY NOTE:  This method is called when a packet is moving up the stack
106  *                towards userland.  To get here, it would have already passed
107  *                through the ethernet/eth.c eth_type_trans() method.
108  *  SANITY NOTE 2: We are referencing to the VLAN_HDR frields, which MAY be
109  *                 stored UNALIGNED in the memory.  RISC systems don't like
110  *                 such cases very much...
111  *  SANITY NOTE 2a:  According to Dave Miller & Alexey, it will always be aligned,
112  *                 so there doesn't need to be any of the unaligned stuff.  It has
113  *                 been commented out now...  --Ben
114  *
115  */
vlan_skb_recv(struct sk_buff * skb,struct net_device * dev,struct packet_type * ptype)116 int vlan_skb_recv(struct sk_buff *skb, struct net_device *dev,
117                   struct packet_type* ptype)
118 {
119 	unsigned char *rawp = NULL;
120 	struct vlan_hdr *vhdr = (struct vlan_hdr *)(skb->data);
121 	unsigned short vid;
122 	struct net_device_stats *stats;
123 	unsigned short vlan_TCI;
124 	unsigned short proto;
125 
126 	/* vlan_TCI = ntohs(get_unaligned(&vhdr->h_vlan_TCI)); */
127 	vlan_TCI = ntohs(vhdr->h_vlan_TCI);
128 
129 	vid = (vlan_TCI & VLAN_VID_MASK);
130 
131 #ifdef VLAN_DEBUG
132 	printk(VLAN_DBG "%s: skb: %p vlan_id: %hx\n",
133 		__FUNCTION__, skb, vid);
134 #endif
135 
136 	/* Ok, we will find the correct VLAN device, strip the header,
137 	 * and then go on as usual.
138 	 */
139 
140 	/* We have 12 bits of vlan ID.
141 	 *
142 	 * We must not drop the vlan_group_lock until we hold a
143 	 * reference to the device (netif_rx does that) or we
144 	 * fail.
145 	 */
146 
147 	spin_lock_bh(&vlan_group_lock);
148 	skb->dev = __find_vlan_dev(dev, vid);
149 	if (!skb->dev) {
150 		spin_unlock_bh(&vlan_group_lock);
151 
152 #ifdef VLAN_DEBUG
153 		printk(VLAN_DBG "%s: ERROR: No net_device for VID: %i on dev: %s [%i]\n",
154 			__FUNCTION__, (unsigned int)(vid), dev->name, dev->ifindex);
155 #endif
156 		kfree_skb(skb);
157 		return -1;
158 	}
159 
160 	skb->dev->last_rx = jiffies;
161 
162 	/* Bump the rx counters for the VLAN device. */
163 	stats = vlan_dev_get_stats(skb->dev);
164 	stats->rx_packets++;
165 	stats->rx_bytes += skb->len;
166 
167 	skb_pull(skb, VLAN_HLEN); /* take off the VLAN header (4 bytes currently) */
168 
169 	/* 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 		spin_unlock_bh(&vlan_group_lock);
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",
178 			__FUNCTION__, skb, dev->name,
179 			VLAN_DEV_INFO(skb->dev)->real_dev->name,
180 			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",
194 		__FUNCTION__, (unsigned long)(skb->priority),
195 		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 
210 	case PACKET_OTHERHOST:
211 		/* 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 		 */
215 		if (memcmp(skb->mac.ethernet->h_dest, skb->dev->dev_addr, ETH_ALEN) == 0) {
216 			/* It is for our (changed) MAC-address! */
217 			skb->pkt_type = PACKET_HOST;
218 		}
219 		break;
220 	default:
221 		break;
222 	};
223 
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 		spin_unlock_bh(&vlan_group_lock);
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) {
261 		skb->protocol = __constant_htons(ETH_P_802_3);
262 		/* 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 		spin_unlock_bh(&vlan_group_lock);
278 		return 0;
279 	}
280 
281 	/*
282 	 *	Real 802.2 LLC
283 	 */
284 	skb->protocol = __constant_htons(ETH_P_802_2);
285 	/* 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 	spin_unlock_bh(&vlan_group_lock);
301 	return 0;
302 }
303 
vlan_dev_get_egress_qos_mask(struct net_device * dev,struct sk_buff * skb)304 static 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 /*
322  *	Create the VLAN header for an arbitrary protocol layer
323  *
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  */
vlan_dev_hard_header(struct sk_buff * skb,struct net_device * dev,unsigned short type,void * daddr,void * saddr,unsigned len)330 int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
331                          unsigned short type, void *daddr, void *saddr,
332                          unsigned len)
333 {
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
349 	 * REORDER_HEADER flag is set)	The drawback to this is some extra
350 	 * header shuffling in the hard_start_xmit.  Users can turn off this
351 	 * REORDER behaviour with the vconfig tool.
352 	 */
353 	build_vlan_header = ((VLAN_DEV_INFO(dev)->flags & 1) == 0);
354 
355 	if (build_vlan_header) {
356 		vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN);
357 
358 		/* build the four bytes that make this a VLAN header. */
359 
360 		/* Now, construct the second two bytes. This field looks something
361 		 * like:
362 		 * usr_priority: 3 bits	 (high bits)
363 		 * CFI		 1 bit
364 		 * VLAN ID	 12 bits (low bits)
365 		 *
366 		 */
367 		veth_TCI = VLAN_DEV_INFO(dev)->vlan_id;
368 		veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
369 
370 		vhdr->h_vlan_TCI = htons(veth_TCI);
371 
372 		/*
373 		 *  Set the protocol type.
374 		 *  For a packet of type ETH_P_802_3 we put the length in here instead.
375 		 *  It is up to the 802.2 layer to carry protocol information.
376 		 */
377 
378 		if (type != ETH_P_802_3) {
379 			vhdr->h_vlan_encapsulated_proto = htons(type);
380 		} else {
381 			vhdr->h_vlan_encapsulated_proto = htons(len);
382 		}
383 	}
384 
385 	/* Before delegating work to the lower layer, enter our MAC-address */
386 	if (saddr == NULL)
387 		saddr = dev->dev_addr;
388 
389 	dev = VLAN_DEV_INFO(dev)->real_dev;
390 
391 	/* MPLS can send us skbuffs w/out enough space.	 This check will grow the
392 	 * skb if it doesn't have enough headroom.  Not a beautiful solution, so
393 	 * I'll tick a counter so that users can know it's happening...	 If they
394 	 * care...
395 	 */
396 
397 	/* NOTE:  This may still break if the underlying device is not the final
398 	 * device (and thus there are more headers to add...)  It should work for
399 	 * good-ole-ethernet though.
400 	 */
401 	if (skb_headroom(skb) < dev->hard_header_len) {
402 		struct sk_buff *sk_tmp = skb;
403 		skb = skb_realloc_headroom(sk_tmp, dev->hard_header_len);
404 		kfree_skb(sk_tmp);
405 		if (skb == NULL) {
406 			struct net_device_stats *stats = vlan_dev_get_stats(vdev);
407 			stats->tx_dropped++;
408 			return -ENOMEM;
409 		}
410 		VLAN_DEV_INFO(vdev)->cnt_inc_headroom_on_tx++;
411 #ifdef VLAN_DEBUG
412 		printk(VLAN_DBG "%s: %s: had to grow skb.\n", __FUNCTION__, vdev->name);
413 #endif
414 	}
415 
416 	if (build_vlan_header) {
417 		/* Now make the underlying real hard header */
418 		rc = dev->hard_header(skb, dev, ETH_P_8021Q, daddr, saddr, len + VLAN_HLEN);
419 
420 		if (rc > 0) {
421 			rc += VLAN_HLEN;
422 		} else if (rc < 0) {
423 			rc -= VLAN_HLEN;
424 		}
425 	} else {
426 		/* If here, then we'll just make a normal looking ethernet frame,
427 		 * but, the hard_start_xmit method will insert the tag (it has to
428 		 * be able to do this for bridged and other skbs that don't come
429 		 * down the protocol stack in an orderly manner.
430 		 */
431 		rc = dev->hard_header(skb, dev, type, daddr, saddr, len);
432 	}
433 
434 	return rc;
435 }
436 
vlan_dev_hard_start_xmit(struct sk_buff * skb,struct net_device * dev)437 int vlan_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
438 {
439 	struct net_device_stats *stats = vlan_dev_get_stats(dev);
440 	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 
448 	if (veth->h_vlan_proto != __constant_htons(ETH_P_8021Q)) {
449 		int orig_headroom = skb_headroom(skb);
450 		unsigned short veth_TCI;
451 
452 		/* This is not a VLAN frame...but we can fix that! */
453 		VLAN_DEV_INFO(dev)->cnt_encap_on_xmit++;
454 
455 #ifdef VLAN_DEBUG
456 		printk(VLAN_DBG "%s: proto to encap: 0x%hx (hbo)\n",
457 			__FUNCTION__, htons(veth->h_vlan_proto));
458 #endif
459 		/* Construct the second two bytes. This field looks something
460 		 * like:
461 		 * usr_priority: 3 bits	 (high bits)
462 		 * CFI		 1 bit
463 		 * VLAN ID	 12 bits (low bits)
464 		 */
465 		veth_TCI = VLAN_DEV_INFO(dev)->vlan_id;
466 		veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
467 
468 		skb = __vlan_put_tag(skb, veth_TCI);
469 		if (!skb) {
470 			stats->tx_dropped++;
471 			return 0;
472 		}
473 
474 		if (orig_headroom < VLAN_HLEN) {
475 			VLAN_DEV_INFO(dev)->cnt_inc_headroom_on_tx++;
476 		}
477 	}
478 
479 #ifdef VLAN_DEBUG
480 	printk(VLAN_DBG "%s: about to send skb: %p to dev: %s\n",
481 		__FUNCTION__, skb, skb->dev->name);
482 	printk(VLAN_DBG "  %2hx.%2hx.%2hx.%2xh.%2hx.%2hx %2hx.%2hx.%2hx.%2hx.%2hx.%2hx %4hx %4hx %4hx\n",
483 	       veth->h_dest[0], veth->h_dest[1], veth->h_dest[2], veth->h_dest[3], veth->h_dest[4], veth->h_dest[5],
484 	       veth->h_source[0], veth->h_source[1], veth->h_source[2], veth->h_source[3], veth->h_source[4], veth->h_source[5],
485 	       veth->h_vlan_proto, veth->h_vlan_TCI, veth->h_vlan_encapsulated_proto);
486 #endif
487 
488 	stats->tx_packets++; /* for statics only */
489 	stats->tx_bytes += skb->len;
490 
491 	skb->dev = VLAN_DEV_INFO(dev)->real_dev;
492 	dev_queue_xmit(skb);
493 
494 	return 0;
495 }
496 
vlan_dev_hwaccel_hard_start_xmit(struct sk_buff * skb,struct net_device * dev)497 int vlan_dev_hwaccel_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
498 {
499 	struct net_device_stats *stats = vlan_dev_get_stats(dev);
500 	unsigned short veth_TCI;
501 
502 	/* Construct the second two bytes. This field looks something
503 	 * like:
504 	 * usr_priority: 3 bits	 (high bits)
505 	 * CFI		 1 bit
506 	 * VLAN ID	 12 bits (low bits)
507 	 */
508 	veth_TCI = VLAN_DEV_INFO(dev)->vlan_id;
509 	veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
510 	skb = __vlan_hwaccel_put_tag(skb, veth_TCI);
511 
512 	stats->tx_packets++;
513 	stats->tx_bytes += skb->len;
514 
515 	skb->dev = VLAN_DEV_INFO(dev)->real_dev;
516 	dev_queue_xmit(skb);
517 
518 	return 0;
519 }
520 
vlan_dev_change_mtu(struct net_device * dev,int new_mtu)521 int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
522 {
523 	/* TODO: gotta make sure the underlying layer can handle it,
524 	 * maybe an IFF_VLAN_CAPABLE flag for devices?
525 	 */
526 	if (VLAN_DEV_INFO(dev)->real_dev->mtu < new_mtu)
527 		return -ERANGE;
528 
529 	dev->mtu = new_mtu;
530 
531 	return 0;
532 }
533 
vlan_dev_set_ingress_priority(char * dev_name,__u32 skb_prio,short vlan_prio)534 int vlan_dev_set_ingress_priority(char *dev_name, __u32 skb_prio, short vlan_prio)
535 {
536 	struct net_device *dev = dev_get_by_name(dev_name);
537 
538 	if (dev) {
539 		if (dev->priv_flags & IFF_802_1Q_VLAN) {
540 			/* see if a priority mapping exists.. */
541 			VLAN_DEV_INFO(dev)->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
542 			dev_put(dev);
543 			return 0;
544 		}
545 
546 		dev_put(dev);
547 	}
548 	return -EINVAL;
549 }
550 
551 /* Remove all egress_priority_map hash table entries. --redlicha */
vlan_dev_destroy_egress_priority_map(struct net_device * dev)552 static void vlan_dev_destroy_egress_priority_map(struct net_device *dev)
553 {
554 	struct vlan_dev_info *info = VLAN_DEV_INFO(dev);
555 	struct vlan_priority_tci_mapping *m;
556 	int i;
557 
558 	for (i = 0; i < ARRAY_SIZE(info->egress_priority_map); i++) {
559 		while ((m = info->egress_priority_map[i])) {
560 			info->egress_priority_map[i] =
561 				info->egress_priority_map[i]->next;
562 			kfree(m);
563 		}
564 	}
565 }
566 
vlan_dev_set_egress_priority(char * dev_name,__u32 skb_prio,short vlan_prio)567 int vlan_dev_set_egress_priority(char *dev_name, __u32 skb_prio, short vlan_prio)
568 {
569 	struct net_device *dev = dev_get_by_name(dev_name);
570 	struct vlan_priority_tci_mapping *mp = NULL;
571 	struct vlan_priority_tci_mapping *np;
572 
573 	if (dev) {
574 		if (dev->priv_flags & IFF_802_1Q_VLAN) {
575 			/* See if a priority mapping exists.. */
576 			mp = VLAN_DEV_INFO(dev)->egress_priority_map[skb_prio & 0xF];
577 			while (mp) {
578 				if (mp->priority == skb_prio) {
579 					mp->vlan_qos = ((vlan_prio << 13) & 0xE000);
580 					dev_put(dev);
581 					return 0;
582 				}
583 				mp = mp->next;
584 			}
585 
586 			/* Create a new mapping then. */
587 			mp = VLAN_DEV_INFO(dev)->egress_priority_map[skb_prio & 0xF];
588 			np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL);
589 			if (np) {
590 				np->next = mp;
591 				np->priority = skb_prio;
592 				np->vlan_qos = ((vlan_prio << 13) & 0xE000);
593 				VLAN_DEV_INFO(dev)->egress_priority_map[skb_prio & 0xF] = np;
594 				dev_put(dev);
595 				return 0;
596 			} else {
597 				dev_put(dev);
598 				return -ENOBUFS;
599 			}
600 		}
601 		dev_put(dev);
602 	}
603 	return -EINVAL;
604 }
605 
606 /* Flags are defined in the vlan_dev_info class in include/linux/if_vlan.h file. */
vlan_dev_set_vlan_flag(char * dev_name,__u32 flag,short flag_val)607 int vlan_dev_set_vlan_flag(char *dev_name, __u32 flag, short flag_val)
608 {
609 	struct net_device *dev = dev_get_by_name(dev_name);
610 
611 	if (dev) {
612 		if (dev->priv_flags & IFF_802_1Q_VLAN) {
613 			/* verify flag is supported */
614 			if (flag == 1) {
615 				if (flag_val) {
616 					VLAN_DEV_INFO(dev)->flags |= 1;
617 				} else {
618 					VLAN_DEV_INFO(dev)->flags &= ~1;
619 				}
620 				dev_put(dev);
621 				return 0;
622 			} else {
623 				printk(KERN_ERR  "%s: flag %i is not valid.\n",
624 					__FUNCTION__, (int)(flag));
625 				dev_put(dev);
626 				return -EINVAL;
627 			}
628 		} else {
629 			printk(KERN_ERR
630 			       "%s: %s is not a vlan device, priv_flags: %hX.\n",
631 			       __FUNCTION__, dev->name, dev->priv_flags);
632 			dev_put(dev);
633 		}
634 	} else {
635 		printk(KERN_ERR  "%s: Could not find device: %s\n",
636 			__FUNCTION__, dev_name);
637 	}
638 
639 	return -EINVAL;
640 }
641 
642 
vlan_dev_get_realdev_name(const char * dev_name,char * result)643 int vlan_dev_get_realdev_name(const char *dev_name, char* result)
644 {
645 	struct net_device *dev = dev_get_by_name(dev_name);
646 	int rv = 0;
647 
648 	if (dev) {
649 		if (dev->priv_flags & IFF_802_1Q_VLAN) {
650 			strncpy(result, VLAN_DEV_INFO(dev)->real_dev->name, 23);
651 			dev_put(dev);
652 			rv = 0;
653 		} else {
654 			/*printk(KERN_ERR
655 			       "%s: %s is not a vlan device, priv_flags: %hX.\n",
656 			       __FUNCTION__, dev->name, dev->priv_flags);*/
657 			dev_put(dev);
658 			rv = -EINVAL;
659 		}
660 	} else {
661 		/* printk(KERN_ERR	 "%s: Could not find device: %s\n",
662 		   __FUNCTION__, dev_name); */
663 		rv = -ENODEV;
664 	}
665 
666 	return rv;
667 }
668 
vlan_dev_get_vid(const char * dev_name,unsigned short * result)669 int vlan_dev_get_vid(const char *dev_name, unsigned short* result)
670 {
671 	struct net_device *dev = dev_get_by_name(dev_name);
672 	int rv = 0;
673 
674 	if (dev) {
675 		if (dev->priv_flags & IFF_802_1Q_VLAN) {
676 			*result = VLAN_DEV_INFO(dev)->vlan_id;
677 			dev_put(dev);
678 			rv = 0;
679 		} else {
680 			/*printk(KERN_ERR
681 			       "%s: %s is not a vlan device, priv_flags: %hX.\n",
682 			       __FUNCTION__, dev->name, dev->priv_flags);*/
683 			dev_put(dev);
684 			rv = -EINVAL;
685 		}
686 	} else {
687 		/* printk(KERN_ERR	 "%s: Could not find device: %s\n",
688 		   __FUNCTION__, dev_name);*/
689 		rv = -ENODEV;
690 	}
691 
692 	return rv;
693 }
694 
695 
vlan_dev_set_mac_address(struct net_device * dev,void * addr_struct_p)696 int vlan_dev_set_mac_address(struct net_device *dev, void *addr_struct_p)
697 {
698 	struct sockaddr *addr = (struct sockaddr *)(addr_struct_p);
699 	int i;
700 
701 	if (netif_running(dev))
702 		return -EBUSY;
703 
704 	memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
705 
706 	printk("%s: Setting MAC address to ", dev->name);
707 	for (i = 0; i < 6; i++)
708 		printk(" %2.2x", dev->dev_addr[i]);
709 	printk(".\n");
710 
711 	if (memcmp(VLAN_DEV_INFO(dev)->real_dev->dev_addr,
712 		   dev->dev_addr,
713 		   dev->addr_len) != 0) {
714 		if (!(VLAN_DEV_INFO(dev)->real_dev->flags & IFF_PROMISC)) {
715 			int flgs = VLAN_DEV_INFO(dev)->real_dev->flags;
716 
717 			/* Increment our in-use promiscuity counter */
718 			dev_set_promiscuity(VLAN_DEV_INFO(dev)->real_dev, 1);
719 
720 			/* Make PROMISC visible to the user. */
721 			flgs |= IFF_PROMISC;
722 			printk("VLAN (%s):  Setting underlying device (%s) to promiscious mode.\n",
723 			       dev->name, VLAN_DEV_INFO(dev)->real_dev->name);
724 			dev_change_flags(VLAN_DEV_INFO(dev)->real_dev, flgs);
725 		}
726 	} else {
727 		printk("VLAN (%s):  Underlying device (%s) has same MAC, not checking promiscious mode.\n",
728 		       dev->name, VLAN_DEV_INFO(dev)->real_dev->name);
729 	}
730 
731 	return 0;
732 }
733 
vlan_dmi_equals(struct dev_mc_list * dmi1,struct dev_mc_list * dmi2)734 static inline int vlan_dmi_equals(struct dev_mc_list *dmi1,
735                                   struct dev_mc_list *dmi2)
736 {
737 	return ((dmi1->dmi_addrlen == dmi2->dmi_addrlen) &&
738 		(memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0));
739 }
740 
741 /** dmi is a single entry into a dev_mc_list, a single node.  mc_list is
742  *  an entire list, and we'll iterate through it.
743  */
vlan_should_add_mc(struct dev_mc_list * dmi,struct dev_mc_list * mc_list)744 static int vlan_should_add_mc(struct dev_mc_list *dmi, struct dev_mc_list *mc_list)
745 {
746 	struct dev_mc_list *idmi;
747 
748 	for (idmi = mc_list; idmi != NULL; ) {
749 		if (vlan_dmi_equals(dmi, idmi)) {
750 			if (dmi->dmi_users > idmi->dmi_users)
751 				return 1;
752 			else
753 				return 0;
754 		} else {
755 			idmi = idmi->next;
756 		}
757 	}
758 
759 	return 1;
760 }
761 
vlan_destroy_mc_list(struct dev_mc_list * mc_list)762 static inline void vlan_destroy_mc_list(struct dev_mc_list *mc_list)
763 {
764 	struct dev_mc_list *dmi = mc_list;
765 	struct dev_mc_list *next;
766 
767 	while(dmi) {
768 		next = dmi->next;
769 		kfree(dmi);
770 		dmi = next;
771 	}
772 }
773 
vlan_copy_mc_list(struct dev_mc_list * mc_list,struct vlan_dev_info * vlan_info)774 static void vlan_copy_mc_list(struct dev_mc_list *mc_list, struct vlan_dev_info *vlan_info)
775 {
776 	struct dev_mc_list *dmi, *new_dmi;
777 
778 	vlan_destroy_mc_list(vlan_info->old_mc_list);
779 	vlan_info->old_mc_list = NULL;
780 
781 	for (dmi = mc_list; dmi != NULL; dmi = dmi->next) {
782 		new_dmi = kmalloc(sizeof(*new_dmi), GFP_ATOMIC);
783 		if (new_dmi == NULL) {
784 			printk(KERN_ERR "vlan: cannot allocate memory. "
785 			       "Multicast may not work properly from now.\n");
786 			return;
787 		}
788 
789 		/* Copy whole structure, then make new 'next' pointer */
790 		*new_dmi = *dmi;
791 		new_dmi->next = vlan_info->old_mc_list;
792 		vlan_info->old_mc_list = new_dmi;
793 	}
794 }
795 
vlan_flush_mc_list(struct net_device * dev)796 static void vlan_flush_mc_list(struct net_device *dev)
797 {
798 	struct dev_mc_list *dmi = dev->mc_list;
799 
800 	while (dmi) {
801 		printk(KERN_DEBUG "%s: del %.2x:%.2x:%.2x:%.2x:%.2x:%.2x mcast address from vlan interface\n",
802 		       dev->name,
803 		       dmi->dmi_addr[0],
804 		       dmi->dmi_addr[1],
805 		       dmi->dmi_addr[2],
806 		       dmi->dmi_addr[3],
807 		       dmi->dmi_addr[4],
808 		       dmi->dmi_addr[5]);
809 		dev_mc_delete(dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
810 		dmi = dev->mc_list;
811 	}
812 
813 	/* dev->mc_list is NULL by the time we get here. */
814 	vlan_destroy_mc_list(VLAN_DEV_INFO(dev)->old_mc_list);
815 	VLAN_DEV_INFO(dev)->old_mc_list = NULL;
816 }
817 
vlan_dev_open(struct net_device * dev)818 int vlan_dev_open(struct net_device *dev)
819 {
820 	if (!(VLAN_DEV_INFO(dev)->real_dev->flags & IFF_UP))
821 		return -ENETDOWN;
822 
823 	return 0;
824 }
825 
vlan_dev_stop(struct net_device * dev)826 int vlan_dev_stop(struct net_device *dev)
827 {
828 	vlan_flush_mc_list(dev);
829 	return 0;
830 }
831 
vlan_dev_init(struct net_device * dev)832 int vlan_dev_init(struct net_device *dev)
833 {
834 	/* TODO:  figure this out, maybe do nothing?? */
835 	return 0;
836 }
837 
vlan_dev_destruct(struct net_device * dev)838 void vlan_dev_destruct(struct net_device *dev)
839 {
840 	if (dev) {
841 		vlan_flush_mc_list(dev);
842 		if (dev->priv) {
843 			if (VLAN_DEV_INFO(dev)->dent)
844 				BUG();
845 			/*
846 			 * Don't leak the hash table entries in
847 			 * VLAN_DEV_INFO(dev)->egress_priority_map! --redlicha
848 			 */
849 			vlan_dev_destroy_egress_priority_map(dev);
850 			kfree(dev->priv);
851 			dev->priv = NULL;
852 		}
853 	}
854 }
855 
856 /** Taken from Gleb + Lennert's VLAN code, and modified... */
vlan_dev_set_multicast_list(struct net_device * vlan_dev)857 void vlan_dev_set_multicast_list(struct net_device *vlan_dev)
858 {
859 	struct dev_mc_list *dmi;
860 	struct net_device *real_dev;
861 	int inc;
862 
863 	if (vlan_dev && (vlan_dev->priv_flags & IFF_802_1Q_VLAN)) {
864 		/* Then it's a real vlan device, as far as we can tell.. */
865 		real_dev = VLAN_DEV_INFO(vlan_dev)->real_dev;
866 
867 		/* compare the current promiscuity to the last promisc we had.. */
868 		inc = vlan_dev->promiscuity - VLAN_DEV_INFO(vlan_dev)->old_promiscuity;
869 		if (inc) {
870 			printk(KERN_INFO "%s: dev_set_promiscuity(master, %d)\n",
871 			       vlan_dev->name, inc);
872 			dev_set_promiscuity(real_dev, inc); /* found in dev.c */
873 			VLAN_DEV_INFO(vlan_dev)->old_promiscuity = vlan_dev->promiscuity;
874 		}
875 
876 		inc = vlan_dev->allmulti - VLAN_DEV_INFO(vlan_dev)->old_allmulti;
877 		if (inc) {
878 			printk(KERN_INFO "%s: dev_set_allmulti(master, %d)\n",
879 			       vlan_dev->name, inc);
880 			dev_set_allmulti(real_dev, inc); /* dev.c */
881 			VLAN_DEV_INFO(vlan_dev)->old_allmulti = vlan_dev->allmulti;
882 		}
883 
884 		/* looking for addresses to add to master's list */
885 		for (dmi = vlan_dev->mc_list; dmi != NULL; dmi = dmi->next) {
886 			if (vlan_should_add_mc(dmi, VLAN_DEV_INFO(vlan_dev)->old_mc_list)) {
887 				dev_mc_add(real_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
888 				printk(KERN_DEBUG "%s: add %.2x:%.2x:%.2x:%.2x:%.2x:%.2x mcast address to master interface\n",
889 				       vlan_dev->name,
890 				       dmi->dmi_addr[0],
891 				       dmi->dmi_addr[1],
892 				       dmi->dmi_addr[2],
893 				       dmi->dmi_addr[3],
894 				       dmi->dmi_addr[4],
895 				       dmi->dmi_addr[5]);
896 			}
897 		}
898 
899 		/* looking for addresses to delete from master's list */
900 		for (dmi = VLAN_DEV_INFO(vlan_dev)->old_mc_list; dmi != NULL; dmi = dmi->next) {
901 			if (vlan_should_add_mc(dmi, vlan_dev->mc_list)) {
902 				/* if we think we should add it to the new list, then we should really
903 				 * delete it from the real list on the underlying device.
904 				 */
905 				dev_mc_delete(real_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
906 				printk(KERN_DEBUG "%s: del %.2x:%.2x:%.2x:%.2x:%.2x:%.2x mcast address from master interface\n",
907 				       vlan_dev->name,
908 				       dmi->dmi_addr[0],
909 				       dmi->dmi_addr[1],
910 				       dmi->dmi_addr[2],
911 				       dmi->dmi_addr[3],
912 				       dmi->dmi_addr[4],
913 				       dmi->dmi_addr[5]);
914 			}
915 		}
916 
917 		/* save multicast list */
918 		vlan_copy_mc_list(vlan_dev->mc_list, VLAN_DEV_INFO(vlan_dev));
919 	}
920 }
921