blob: 2ff7659b5e5fa70ef025dc711c06a1c066e976f6 [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:
Patrick McHardy40f98e12008-01-21 00:24:30 -080063 pr_debug("%s: unable to resolve type %X addresses.\n",
64 dev->name, ntohs(veth->h_vlan_encapsulated_proto));
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +090065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 memcpy(veth->h_source, dev->dev_addr, ETH_ALEN);
67 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 return 0;
71}
72
73static inline struct sk_buff *vlan_check_reorder_header(struct sk_buff *skb)
74{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -080075 if (vlan_dev_info(skb->dev)->flags & VLAN_FLAG_REORDER_HDR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (skb_shared(skb) || skb_cloned(skb)) {
77 struct sk_buff *nskb = skb_copy(skb, GFP_ATOMIC);
78 kfree_skb(skb);
79 skb = nskb;
80 }
81 if (skb) {
82 /* Lifted from Gleb's VLAN code... */
83 memmove(skb->data - ETH_HLEN,
84 skb->data - VLAN_ETH_HLEN, 12);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -070085 skb->mac_header += VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
87 }
88
89 return skb;
90}
91
92/*
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +090093 * Determine the packet's protocol ID. The rule here is that we
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * assume 802.3 if the type field is short enough to be a length.
95 * This is normal practice and works for any 'now in use' protocol.
96 *
97 * Also, at this point we assume that we ARE dealing exclusively with
98 * VLAN packets, or packets that should be made into VLAN packets based
99 * on a default VLAN ID.
100 *
101 * NOTE: Should be similar to ethernet/eth.c.
102 *
103 * SANITY NOTE: This method is called when a packet is moving up the stack
104 * towards userland. To get here, it would have already passed
105 * through the ethernet/eth.c eth_type_trans() method.
106 * SANITY NOTE 2: We are referencing to the VLAN_HDR frields, which MAY be
107 * stored UNALIGNED in the memory. RISC systems don't like
108 * such cases very much...
Patrick McHardy2029cc22008-01-21 00:26:41 -0800109 * SANITY NOTE 2a: According to Dave Miller & Alexey, it will always be
110 * aligned, so there doesn't need to be any of the unaligned
111 * stuff. It has been commented out now... --Ben
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 *
113 */
114int vlan_skb_recv(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy2029cc22008-01-21 00:26:41 -0800115 struct packet_type *ptype, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 unsigned char *rawp = NULL;
Evgeniy Polyakove7c243c2007-08-24 23:36:29 -0700118 struct vlan_hdr *vhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 unsigned short vid;
120 struct net_device_stats *stats;
121 unsigned short vlan_TCI;
Alexey Dobriyan3c3f8f22005-09-19 15:41:28 -0700122 __be16 proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Eric W. Biedermane730c152007-09-17 11:53:39 -0700124 if (dev->nd_net != &init_net) {
125 kfree_skb(skb);
126 return -1;
127 }
128
Patrick McHardy2029cc22008-01-21 00:26:41 -0800129 skb = skb_share_check(skb, GFP_ATOMIC);
130 if (skb == NULL)
Evgeniy Polyakove7c243c2007-08-24 23:36:29 -0700131 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 /* 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();
Patrick McHardy2029cc22008-01-21 00:26:41 -0800160 pr_debug("%s: ERROR: No net_device for VID: %u on dev: %s\n",
161 __FUNCTION__, (unsigned int)vid, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 kfree_skb(skb);
163 return -1;
164 }
165
166 skb->dev->last_rx = jiffies;
167
168 /* Bump the rx counters for the VLAN device. */
Patrick McHardy7bd38d72008-01-21 00:19:31 -0800169 stats = &skb->dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 stats->rx_packets++;
171 stats->rx_bytes += skb->len;
172
Herbert Xucbb042f2006-03-20 22:43:56 -0800173 /* Take off the VLAN header (4 bytes currently) */
174 skb_pull_rcsum(skb, VLAN_HLEN);
Stephen Hemmingera3884422005-12-14 16:23:16 -0800175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 /*
177 * Deal with ingress priority mapping.
178 */
Patrick McHardy2029cc22008-01-21 00:26:41 -0800179 skb->priority = vlan_get_ingress_priority(skb->dev,
180 ntohs(vhdr->h_vlan_TCI));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Patrick McHardy40f98e12008-01-21 00:24:30 -0800182 pr_debug("%s: priority: %u for TCI: %hu\n",
183 __FUNCTION__, skb->priority, ntohs(vhdr->h_vlan_TCI));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 /* The ethernet driver already did the pkt_type calculations
186 * for us...
187 */
188 switch (skb->pkt_type) {
189 case PACKET_BROADCAST: /* Yeah, stats collect these together.. */
Patrick McHardy2029cc22008-01-21 00:26:41 -0800190 /* stats->broadcast ++; // no such counter :-( */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 break;
192
193 case PACKET_MULTICAST:
194 stats->multicast++;
195 break;
196
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900197 case PACKET_OTHERHOST:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 /* Our lower layer thinks this is not local, let's make sure.
Patrick McHardy2029cc22008-01-21 00:26:41 -0800199 * This allows the VLAN to have a different MAC than the
200 * underlying device, and still route correctly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 */
Patrick McHardy2029cc22008-01-21 00:26:41 -0800202 if (!compare_ether_addr(eth_hdr(skb)->h_dest,
203 skb->dev->dev_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /* It is for our (changed) MAC-address! */
205 skb->pkt_type = PACKET_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 break;
207 default:
208 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 /* Was a VLAN packet, grab the encapsulated protocol, which the layer
212 * three protocols care about.
213 */
214 /* proto = get_unaligned(&vhdr->h_vlan_encapsulated_proto); */
215 proto = vhdr->h_vlan_encapsulated_proto;
216
217 skb->protocol = proto;
218 if (ntohs(proto) >= 1536) {
219 /* place it back on the queue to be handled by
220 * true layer 3 protocols.
221 */
222
223 /* See if we are configured to re-write the VLAN header
224 * to make it look like ethernet...
225 */
226 skb = vlan_check_reorder_header(skb);
227
228 /* Can be null if skb-clone fails when re-ordering */
229 if (skb) {
230 netif_rx(skb);
231 } else {
232 /* TODO: Add a more specific counter here. */
233 stats->rx_errors++;
234 }
235 rcu_read_unlock();
236 return 0;
237 }
238
239 rawp = skb->data;
240
241 /*
242 * This is a magic hack to spot IPX packets. Older Novell breaks
243 * the protocol design and runs IPX over 802.3 without an 802.2 LLC
244 * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
245 * won't work for fault tolerant netware but does for the rest.
246 */
247 if (*(unsigned short *)rawp == 0xFFFF) {
YOSHIFUJI Hideakib93b7ee2007-03-25 20:12:18 -0700248 skb->protocol = htons(ETH_P_802_3);
Patrick McHardy2029cc22008-01-21 00:26:41 -0800249 /* place it back on the queue to be handled by true layer 3
250 * protocols. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 /* See if we are configured to re-write the VLAN header
253 * to make it look like ethernet...
254 */
255 skb = vlan_check_reorder_header(skb);
256
257 /* Can be null if skb-clone fails when re-ordering */
258 if (skb) {
259 netif_rx(skb);
260 } else {
261 /* TODO: Add a more specific counter here. */
262 stats->rx_errors++;
263 }
264 rcu_read_unlock();
265 return 0;
266 }
267
268 /*
269 * Real 802.2 LLC
270 */
YOSHIFUJI Hideakib93b7ee2007-03-25 20:12:18 -0700271 skb->protocol = htons(ETH_P_802_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 /* place it back on the queue to be handled by upper layer 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
Patrick McHardy2029cc22008-01-21 00:26:41 -0800291static inline unsigned short
292vlan_dev_get_egress_qos_mask(struct net_device *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Patrick McHardy2029cc22008-01-21 00:26:41 -0800294 struct vlan_priority_tci_mapping *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Patrick McHardy2029cc22008-01-21 00:26:41 -0800296 mp = vlan_dev_info(dev)->egress_priority_map[(skb->priority & 0xF)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 while (mp) {
298 if (mp->priority == skb->priority) {
Patrick McHardy2029cc22008-01-21 00:26:41 -0800299 return mp->vlan_qos; /* This should already be shifted
300 * to mask correctly with the
301 * VLAN's TCI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303 mp = mp->next;
304 }
305 return 0;
306}
307
308/*
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900309 * Create the VLAN header for an arbitrary protocol layer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 *
311 * saddr=NULL means use device source address
312 * daddr=NULL means leave destination address (eg unresolved arp)
313 *
314 * This is called when the SKB is moving down the stack towards the
315 * physical devices.
316 */
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800317static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
318 unsigned short type,
319 const void *daddr, const void *saddr,
320 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 struct vlan_hdr *vhdr;
323 unsigned short veth_TCI = 0;
324 int rc = 0;
325 int build_vlan_header = 0;
Patrick McHardy2029cc22008-01-21 00:26:41 -0800326 struct net_device *vdev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Patrick McHardy40f98e12008-01-21 00:24:30 -0800328 pr_debug("%s: skb: %p type: %hx len: %u vlan_id: %hx, daddr: %p\n",
Patrick McHardy2029cc22008-01-21 00:26:41 -0800329 __FUNCTION__, skb, type, len, vlan_dev_info(dev)->vlan_id,
330 daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 /* build vlan header only if re_order_header flag is NOT set. This
333 * fixes some programs that get confused when they see a VLAN device
334 * sending a frame that is VLAN encoded (the consensus is that the VLAN
335 * device should look completely like an Ethernet device when the
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900336 * REORDER_HEADER flag is set) The drawback to this is some extra
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 * header shuffling in the hard_start_xmit. Users can turn off this
338 * REORDER behaviour with the vconfig tool.
339 */
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800340 if (!(vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR))
Patrick McHardya4bf3af42007-06-13 12:07:37 -0700341 build_vlan_header = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 if (build_vlan_header) {
344 vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN);
345
346 /* build the four bytes that make this a VLAN header. */
347
Patrick McHardy2029cc22008-01-21 00:26:41 -0800348 /* Now, construct the second two bytes. This field looks
349 * something like:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 * usr_priority: 3 bits (high bits)
351 * CFI 1 bit
352 * VLAN ID 12 bits (low bits)
353 *
354 */
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800355 veth_TCI = vlan_dev_info(dev)->vlan_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
357
358 vhdr->h_vlan_TCI = htons(veth_TCI);
359
360 /*
Patrick McHardy2029cc22008-01-21 00:26:41 -0800361 * Set the protocol type. For a packet of type ETH_P_802_3 we
362 * put the length in here instead. It is up to the 802.2
363 * layer to carry protocol information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 */
365
Patrick McHardy2029cc22008-01-21 00:26:41 -0800366 if (type != ETH_P_802_3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 vhdr->h_vlan_encapsulated_proto = htons(type);
Patrick McHardy2029cc22008-01-21 00:26:41 -0800368 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 vhdr->h_vlan_encapsulated_proto = htons(len);
Jerome Borsboom279e1722007-04-13 16:12:47 -0700370
371 skb->protocol = htons(ETH_P_8021Q);
David S. Millerbe8bd862007-04-19 20:34:51 -0700372 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374
375 /* Before delegating work to the lower layer, enter our MAC-address */
376 if (saddr == NULL)
377 saddr = dev->dev_addr;
378
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800379 dev = vlan_dev_info(dev)->real_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Patrick McHardy2029cc22008-01-21 00:26:41 -0800381 /* MPLS can send us skbuffs w/out enough space. This check will grow
382 * the skb if it doesn't have enough headroom. Not a beautiful solution,
383 * so I'll tick a counter so that users can know it's happening...
384 * If they care...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 */
386
Patrick McHardy2029cc22008-01-21 00:26:41 -0800387 /* NOTE: This may still break if the underlying device is not the final
388 * device (and thus there are more headers to add...) It should work for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 * good-ole-ethernet though.
390 */
391 if (skb_headroom(skb) < dev->hard_header_len) {
392 struct sk_buff *sk_tmp = skb;
393 skb = skb_realloc_headroom(sk_tmp, dev->hard_header_len);
394 kfree_skb(sk_tmp);
395 if (skb == NULL) {
Patrick McHardy7bd38d72008-01-21 00:19:31 -0800396 struct net_device_stats *stats = &vdev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 stats->tx_dropped++;
398 return -ENOMEM;
399 }
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800400 vlan_dev_info(vdev)->cnt_inc_headroom_on_tx++;
Patrick McHardy2029cc22008-01-21 00:26:41 -0800401 pr_debug("%s: %s: had to grow skb\n", __FUNCTION__, vdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403
404 if (build_vlan_header) {
405 /* Now make the underlying real hard header */
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700406 rc = dev_hard_header(skb, dev, ETH_P_8021Q, daddr, saddr,
407 len + VLAN_HLEN);
408 if (rc > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 rc += VLAN_HLEN;
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700410 else if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 rc -= VLAN_HLEN;
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700412 } else
Patrick McHardy2029cc22008-01-21 00:26:41 -0800413 /* If here, then we'll just make a normal looking ethernet
414 * frame, but, the hard_start_xmit method will insert the tag
415 * (it has to be able to do this for bridged and other skbs
416 * that don't come down the protocol stack in an orderly manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 */
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700418 rc = dev_hard_header(skb, dev, type, daddr, saddr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 return rc;
421}
422
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800423static int vlan_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Patrick McHardy7bd38d72008-01-21 00:19:31 -0800425 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
427
428 /* Handle non-VLAN frames if they are sent to us, for example by DHCP.
429 *
430 * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
431 * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
432 */
433
Joonwoo Park6ab3b482007-11-29 22:16:41 +1100434 if (veth->h_vlan_proto != htons(ETH_P_8021Q) ||
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800435 vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 int orig_headroom = skb_headroom(skb);
437 unsigned short veth_TCI;
438
439 /* This is not a VLAN frame...but we can fix that! */
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800440 vlan_dev_info(dev)->cnt_encap_on_xmit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Patrick McHardy40f98e12008-01-21 00:24:30 -0800442 pr_debug("%s: proto to encap: 0x%hx\n",
443 __FUNCTION__, htons(veth->h_vlan_proto));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 /* Construct the second two bytes. This field looks something
445 * like:
446 * usr_priority: 3 bits (high bits)
447 * CFI 1 bit
448 * VLAN ID 12 bits (low bits)
449 */
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800450 veth_TCI = vlan_dev_info(dev)->vlan_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
452
453 skb = __vlan_put_tag(skb, veth_TCI);
454 if (!skb) {
455 stats->tx_dropped++;
456 return 0;
457 }
458
Patrick McHardy2029cc22008-01-21 00:26:41 -0800459 if (orig_headroom < VLAN_HLEN)
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800460 vlan_dev_info(dev)->cnt_inc_headroom_on_tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
462
Patrick McHardy40f98e12008-01-21 00:24:30 -0800463 pr_debug("%s: about to send skb: %p to dev: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 __FUNCTION__, skb, skb->dev->name);
Patrick McHardy40f98e12008-01-21 00:24:30 -0800465 pr_debug(" " MAC_FMT " " MAC_FMT " %4hx %4hx %4hx\n",
466 veth->h_dest[0], veth->h_dest[1], veth->h_dest[2],
467 veth->h_dest[3], veth->h_dest[4], veth->h_dest[5],
468 veth->h_source[0], veth->h_source[1], veth->h_source[2],
469 veth->h_source[3], veth->h_source[4], veth->h_source[5],
470 veth->h_vlan_proto, veth->h_vlan_TCI,
471 veth->h_vlan_encapsulated_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 stats->tx_packets++; /* for statics only */
474 stats->tx_bytes += skb->len;
475
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800476 skb->dev = vlan_dev_info(dev)->real_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 dev_queue_xmit(skb);
478
479 return 0;
480}
481
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800482static int vlan_dev_hwaccel_hard_start_xmit(struct sk_buff *skb,
483 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Patrick McHardy7bd38d72008-01-21 00:19:31 -0800485 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 unsigned short veth_TCI;
487
488 /* Construct the second two bytes. This field looks something
489 * like:
490 * usr_priority: 3 bits (high bits)
491 * CFI 1 bit
492 * VLAN ID 12 bits (low bits)
493 */
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800494 veth_TCI = vlan_dev_info(dev)->vlan_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
496 skb = __vlan_hwaccel_put_tag(skb, veth_TCI);
497
498 stats->tx_packets++;
499 stats->tx_bytes += skb->len;
500
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800501 skb->dev = vlan_dev_info(dev)->real_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 dev_queue_xmit(skb);
503
504 return 0;
505}
506
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800507static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 /* TODO: gotta make sure the underlying layer can handle it,
510 * maybe an IFF_VLAN_CAPABLE flag for devices?
511 */
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800512 if (vlan_dev_info(dev)->real_dev->mtu < new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return -ERANGE;
514
515 dev->mtu = new_mtu;
516
517 return 0;
518}
519
Patrick McHardyc17d8872007-06-13 12:05:22 -0700520void vlan_dev_set_ingress_priority(const struct net_device *dev,
521 u32 skb_prio, short vlan_prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800523 struct vlan_dev_info *vlan = vlan_dev_info(dev);
Patrick McHardyb020cb42007-06-13 12:07:22 -0700524
525 if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio)
526 vlan->nr_ingress_mappings--;
527 else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio)
528 vlan->nr_ingress_mappings++;
529
530 vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
Patrick McHardyc17d8872007-06-13 12:05:22 -0700533int vlan_dev_set_egress_priority(const struct net_device *dev,
534 u32 skb_prio, short vlan_prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800536 struct vlan_dev_info *vlan = vlan_dev_info(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 struct vlan_priority_tci_mapping *mp = NULL;
538 struct vlan_priority_tci_mapping *np;
Patrick McHardyb020cb42007-06-13 12:07:22 -0700539 u32 vlan_qos = (vlan_prio << 13) & 0xE000;
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900540
Patrick McHardyc17d8872007-06-13 12:05:22 -0700541 /* See if a priority mapping exists.. */
Patrick McHardyb020cb42007-06-13 12:07:22 -0700542 mp = vlan->egress_priority_map[skb_prio & 0xF];
Patrick McHardyc17d8872007-06-13 12:05:22 -0700543 while (mp) {
544 if (mp->priority == skb_prio) {
Patrick McHardyb020cb42007-06-13 12:07:22 -0700545 if (mp->vlan_qos && !vlan_qos)
546 vlan->nr_egress_mappings--;
547 else if (!mp->vlan_qos && vlan_qos)
548 vlan->nr_egress_mappings++;
549 mp->vlan_qos = vlan_qos;
Patrick McHardyc17d8872007-06-13 12:05:22 -0700550 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
Patrick McHardyc17d8872007-06-13 12:05:22 -0700552 mp = mp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Patrick McHardyc17d8872007-06-13 12:05:22 -0700554
555 /* Create a new mapping then. */
Patrick McHardyb020cb42007-06-13 12:07:22 -0700556 mp = vlan->egress_priority_map[skb_prio & 0xF];
Patrick McHardyc17d8872007-06-13 12:05:22 -0700557 np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL);
558 if (!np)
559 return -ENOBUFS;
560
561 np->next = mp;
562 np->priority = skb_prio;
Patrick McHardyb020cb42007-06-13 12:07:22 -0700563 np->vlan_qos = vlan_qos;
564 vlan->egress_priority_map[skb_prio & 0xF] = np;
565 if (vlan_qos)
566 vlan->nr_egress_mappings++;
Patrick McHardyc17d8872007-06-13 12:05:22 -0700567 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Patrick McHardya4bf3af42007-06-13 12:07:37 -0700570/* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */
Patrick McHardyc17d8872007-06-13 12:05:22 -0700571int vlan_dev_set_vlan_flag(const struct net_device *dev,
572 u32 flag, short flag_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Patrick McHardyc17d8872007-06-13 12:05:22 -0700574 /* verify flag is supported */
Patrick McHardya4bf3af42007-06-13 12:07:37 -0700575 if (flag == VLAN_FLAG_REORDER_HDR) {
Patrick McHardy2029cc22008-01-21 00:26:41 -0800576 if (flag_val)
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800577 vlan_dev_info(dev)->flags |= VLAN_FLAG_REORDER_HDR;
Patrick McHardy2029cc22008-01-21 00:26:41 -0800578 else
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800579 vlan_dev_info(dev)->flags &= ~VLAN_FLAG_REORDER_HDR;
Patrick McHardyc17d8872007-06-13 12:05:22 -0700580 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return -EINVAL;
583}
584
Patrick McHardyc17d8872007-06-13 12:05:22 -0700585void vlan_dev_get_realdev_name(const struct net_device *dev, char *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800587 strncpy(result, vlan_dev_info(dev)->real_dev->name, 23);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
Patrick McHardyc17d8872007-06-13 12:05:22 -0700590void vlan_dev_get_vid(const struct net_device *dev, unsigned short *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800592 *result = vlan_dev_info(dev)->vlan_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800595static int vlan_dev_open(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800597 struct vlan_dev_info *vlan = vlan_dev_info(dev);
Patrick McHardy8c979c22007-07-11 19:45:24 -0700598 struct net_device *real_dev = vlan->real_dev;
599 int err;
600
601 if (!(real_dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return -ENETDOWN;
603
Patrick McHardy8c979c22007-07-11 19:45:24 -0700604 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr)) {
605 err = dev_unicast_add(real_dev, dev->dev_addr, ETH_ALEN);
606 if (err < 0)
607 return err;
608 }
609 memcpy(vlan->real_dev_addr, real_dev->dev_addr, ETH_ALEN);
610
Patrick McHardy6c78dcb2007-07-14 18:52:56 -0700611 if (dev->flags & IFF_ALLMULTI)
612 dev_set_allmulti(real_dev, 1);
613 if (dev->flags & IFF_PROMISC)
614 dev_set_promiscuity(real_dev, 1);
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return 0;
617}
618
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800619static int vlan_dev_stop(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800621 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
Patrick McHardy8c979c22007-07-11 19:45:24 -0700622
Patrick McHardy56addd62007-07-14 18:53:28 -0700623 dev_mc_unsync(real_dev, dev);
Patrick McHardy6c78dcb2007-07-14 18:52:56 -0700624 if (dev->flags & IFF_ALLMULTI)
625 dev_set_allmulti(real_dev, -1);
626 if (dev->flags & IFF_PROMISC)
627 dev_set_promiscuity(real_dev, -1);
628
Patrick McHardy8c979c22007-07-11 19:45:24 -0700629 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
630 dev_unicast_delete(real_dev, dev->dev_addr, dev->addr_len);
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return 0;
633}
634
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800635static int vlan_dev_set_mac_address(struct net_device *dev, void *p)
Patrick McHardy39aaac12007-11-10 21:52:35 -0800636{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800637 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
Patrick McHardy39aaac12007-11-10 21:52:35 -0800638 struct sockaddr *addr = p;
639 int err;
640
641 if (!is_valid_ether_addr(addr->sa_data))
642 return -EADDRNOTAVAIL;
643
644 if (!(dev->flags & IFF_UP))
645 goto out;
646
647 if (compare_ether_addr(addr->sa_data, real_dev->dev_addr)) {
648 err = dev_unicast_add(real_dev, addr->sa_data, ETH_ALEN);
649 if (err < 0)
650 return err;
651 }
652
653 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
654 dev_unicast_delete(real_dev, dev->dev_addr, ETH_ALEN);
655
656out:
657 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
658 return 0;
659}
660
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800661static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800663 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 struct ifreq ifrr;
665 int err = -EOPNOTSUPP;
666
667 strncpy(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
668 ifrr.ifr_ifru = ifr->ifr_ifru;
669
Patrick McHardy2029cc22008-01-21 00:26:41 -0800670 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 case SIOCGMIIPHY:
672 case SIOCGMIIREG:
673 case SIOCSMIIREG:
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900674 if (real_dev->do_ioctl && netif_device_present(real_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 err = real_dev->do_ioctl(real_dev, &ifrr, cmd);
676 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900679 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 ifr->ifr_ifru = ifrr.ifr_ifru;
681
682 return err;
683}
684
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800685static void vlan_dev_change_rx_flags(struct net_device *dev, int change)
Patrick McHardy6c78dcb2007-07-14 18:52:56 -0700686{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800687 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
Patrick McHardy6c78dcb2007-07-14 18:52:56 -0700688
689 if (change & IFF_ALLMULTI)
690 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
691 if (change & IFF_PROMISC)
692 dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1);
693}
694
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800695static void vlan_dev_set_multicast_list(struct net_device *vlan_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800697 dev_mc_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800699
700/*
701 * vlan network devices have devices nesting below it, and are a special
702 * "super class" of normal network devices; split their locks off into a
703 * separate class since they always nest.
704 */
705static struct lock_class_key vlan_netdev_xmit_lock_key;
706
707static const struct header_ops vlan_header_ops = {
708 .create = vlan_dev_hard_header,
709 .rebuild = vlan_dev_rebuild_header,
710 .parse = eth_header_parse,
711};
712
713static int vlan_dev_init(struct net_device *dev)
714{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800715 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800716 int subclass = 0;
717
718 /* IFF_BROADCAST|IFF_MULTICAST; ??? */
719 dev->flags = real_dev->flags & ~IFF_UP;
720 dev->iflink = real_dev->ifindex;
721 dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
722 (1<<__LINK_STATE_DORMANT))) |
723 (1<<__LINK_STATE_PRESENT);
724
725 /* ipv6 shared card related stuff */
726 dev->dev_id = real_dev->dev_id;
727
728 if (is_zero_ether_addr(dev->dev_addr))
729 memcpy(dev->dev_addr, real_dev->dev_addr, dev->addr_len);
730 if (is_zero_ether_addr(dev->broadcast))
731 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
732
733 if (real_dev->features & NETIF_F_HW_VLAN_TX) {
734 dev->header_ops = real_dev->header_ops;
735 dev->hard_header_len = real_dev->hard_header_len;
736 dev->hard_start_xmit = vlan_dev_hwaccel_hard_start_xmit;
737 } else {
738 dev->header_ops = &vlan_header_ops;
739 dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
740 dev->hard_start_xmit = vlan_dev_hard_start_xmit;
741 }
742
743 if (real_dev->priv_flags & IFF_802_1Q_VLAN)
744 subclass = 1;
745
746 lockdep_set_class_and_subclass(&dev->_xmit_lock,
747 &vlan_netdev_xmit_lock_key, subclass);
748 return 0;
749}
750
751void vlan_setup(struct net_device *dev)
752{
753 ether_setup(dev);
754
755 dev->priv_flags |= IFF_802_1Q_VLAN;
756 dev->tx_queue_len = 0;
757
758 dev->change_mtu = vlan_dev_change_mtu;
759 dev->init = vlan_dev_init;
760 dev->open = vlan_dev_open;
761 dev->stop = vlan_dev_stop;
762 dev->set_mac_address = vlan_dev_set_mac_address;
763 dev->set_multicast_list = vlan_dev_set_multicast_list;
764 dev->change_rx_flags = vlan_dev_change_rx_flags;
765 dev->do_ioctl = vlan_dev_ioctl;
766 dev->destructor = free_netdev;
767
768 memset(dev->broadcast, 0, ETH_ALEN);
769}