blob: 57799af510885ea80f0867bf961296cba9cb2fa6 [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>
Patrick McHardyad712082008-01-21 00:27:00 -08006 * Please send support related email to: netdev@vger.kernel.org
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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{
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800117 unsigned char *rawp;
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
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800124 if (dev->nd_net != &init_net)
125 goto err_free;
Eric W. Biedermane730c152007-09-17 11:53:39 -0700126
Patrick McHardy2029cc22008-01-21 00:26:41 -0800127 skb = skb_share_check(skb, GFP_ATOMIC);
128 if (skb == NULL)
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800129 goto err_free;
Evgeniy Polyakove7c243c2007-08-24 23:36:29 -0700130
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800131 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
132 goto err_free;
Evgeniy Polyakove7c243c2007-08-24 23:36:29 -0700133
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800134 vhdr = (struct vlan_hdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 vlan_TCI = ntohs(vhdr->h_vlan_TCI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 vid = (vlan_TCI & VLAN_VID_MASK);
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 rcu_read_lock();
139 skb->dev = __find_vlan_dev(dev, vid);
140 if (!skb->dev) {
Patrick McHardy2029cc22008-01-21 00:26:41 -0800141 pr_debug("%s: ERROR: No net_device for VID: %u on dev: %s\n",
142 __FUNCTION__, (unsigned int)vid, dev->name);
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800143 goto err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145
146 skb->dev->last_rx = jiffies;
147
Patrick McHardy7bd38d72008-01-21 00:19:31 -0800148 stats = &skb->dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 stats->rx_packets++;
150 stats->rx_bytes += skb->len;
151
Herbert Xucbb042f2006-03-20 22:43:56 -0800152 skb_pull_rcsum(skb, VLAN_HLEN);
Stephen Hemmingera3884422005-12-14 16:23:16 -0800153
Patrick McHardy2029cc22008-01-21 00:26:41 -0800154 skb->priority = vlan_get_ingress_priority(skb->dev,
155 ntohs(vhdr->h_vlan_TCI));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Patrick McHardy40f98e12008-01-21 00:24:30 -0800157 pr_debug("%s: priority: %u for TCI: %hu\n",
158 __FUNCTION__, skb->priority, ntohs(vhdr->h_vlan_TCI));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 switch (skb->pkt_type) {
161 case PACKET_BROADCAST: /* Yeah, stats collect these together.. */
Patrick McHardy2029cc22008-01-21 00:26:41 -0800162 /* stats->broadcast ++; // no such counter :-( */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 break;
164
165 case PACKET_MULTICAST:
166 stats->multicast++;
167 break;
168
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900169 case PACKET_OTHERHOST:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 /* Our lower layer thinks this is not local, let's make sure.
Patrick McHardy2029cc22008-01-21 00:26:41 -0800171 * This allows the VLAN to have a different MAC than the
172 * underlying device, and still route correctly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 */
Patrick McHardy2029cc22008-01-21 00:26:41 -0800174 if (!compare_ether_addr(eth_hdr(skb)->h_dest,
175 skb->dev->dev_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 skb->pkt_type = PACKET_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 break;
178 default:
179 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 /* Was a VLAN packet, grab the encapsulated protocol, which the layer
183 * three protocols care about.
184 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 proto = vhdr->h_vlan_encapsulated_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (ntohs(proto) >= 1536) {
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800187 skb->protocol = proto;
188 goto recv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /*
192 * This is a magic hack to spot IPX packets. Older Novell breaks
193 * the protocol design and runs IPX over 802.3 without an 802.2 LLC
194 * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
195 * won't work for fault tolerant netware but does for the rest.
196 */
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800197 rawp = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (*(unsigned short *)rawp == 0xFFFF) {
YOSHIFUJI Hideakib93b7ee2007-03-25 20:12:18 -0700199 skb->protocol = htons(ETH_P_802_3);
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800200 goto recv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202
203 /*
204 * Real 802.2 LLC
205 */
YOSHIFUJI Hideakib93b7ee2007-03-25 20:12:18 -0700206 skb->protocol = htons(ETH_P_802_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800208recv:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 skb = vlan_check_reorder_header(skb);
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800210 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 stats->rx_errors++;
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800212 goto err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800214
215 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 rcu_read_unlock();
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800217 return NET_RX_SUCCESS;
218
219err_unlock:
220 rcu_read_unlock();
221err_free:
222 kfree_skb(skb);
223 return NET_RX_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Patrick McHardy2029cc22008-01-21 00:26:41 -0800226static inline unsigned short
227vlan_dev_get_egress_qos_mask(struct net_device *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Patrick McHardy2029cc22008-01-21 00:26:41 -0800229 struct vlan_priority_tci_mapping *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Patrick McHardy2029cc22008-01-21 00:26:41 -0800231 mp = vlan_dev_info(dev)->egress_priority_map[(skb->priority & 0xF)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 while (mp) {
233 if (mp->priority == skb->priority) {
Patrick McHardy2029cc22008-01-21 00:26:41 -0800234 return mp->vlan_qos; /* This should already be shifted
235 * to mask correctly with the
236 * VLAN's TCI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
238 mp = mp->next;
239 }
240 return 0;
241}
242
243/*
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900244 * Create the VLAN header for an arbitrary protocol layer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 *
246 * saddr=NULL means use device source address
247 * daddr=NULL means leave destination address (eg unresolved arp)
248 *
249 * This is called when the SKB is moving down the stack towards the
250 * physical devices.
251 */
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800252static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
253 unsigned short type,
254 const void *daddr, const void *saddr,
255 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 struct vlan_hdr *vhdr;
258 unsigned short veth_TCI = 0;
259 int rc = 0;
260 int build_vlan_header = 0;
Patrick McHardy2029cc22008-01-21 00:26:41 -0800261 struct net_device *vdev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Patrick McHardy40f98e12008-01-21 00:24:30 -0800263 pr_debug("%s: skb: %p type: %hx len: %u vlan_id: %hx, daddr: %p\n",
Patrick McHardy2029cc22008-01-21 00:26:41 -0800264 __FUNCTION__, skb, type, len, vlan_dev_info(dev)->vlan_id,
265 daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 /* build vlan header only if re_order_header flag is NOT set. This
268 * fixes some programs that get confused when they see a VLAN device
269 * sending a frame that is VLAN encoded (the consensus is that the VLAN
270 * device should look completely like an Ethernet device when the
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900271 * REORDER_HEADER flag is set) The drawback to this is some extra
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 * header shuffling in the hard_start_xmit. Users can turn off this
273 * REORDER behaviour with the vconfig tool.
274 */
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800275 if (!(vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR))
Patrick McHardya4bf3af42007-06-13 12:07:37 -0700276 build_vlan_header = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 if (build_vlan_header) {
279 vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN);
280
281 /* build the four bytes that make this a VLAN header. */
282
Patrick McHardy2029cc22008-01-21 00:26:41 -0800283 /* Now, construct the second two bytes. This field looks
284 * something like:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 * usr_priority: 3 bits (high bits)
286 * CFI 1 bit
287 * VLAN ID 12 bits (low bits)
288 *
289 */
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800290 veth_TCI = vlan_dev_info(dev)->vlan_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
292
293 vhdr->h_vlan_TCI = htons(veth_TCI);
294
295 /*
Patrick McHardy2029cc22008-01-21 00:26:41 -0800296 * Set the protocol type. For a packet of type ETH_P_802_3 we
297 * put the length in here instead. It is up to the 802.2
298 * layer to carry protocol information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 */
300
Patrick McHardy2029cc22008-01-21 00:26:41 -0800301 if (type != ETH_P_802_3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 vhdr->h_vlan_encapsulated_proto = htons(type);
Patrick McHardy2029cc22008-01-21 00:26:41 -0800303 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 vhdr->h_vlan_encapsulated_proto = htons(len);
Jerome Borsboom279e1722007-04-13 16:12:47 -0700305
306 skb->protocol = htons(ETH_P_8021Q);
David S. Millerbe8bd862007-04-19 20:34:51 -0700307 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
309
310 /* Before delegating work to the lower layer, enter our MAC-address */
311 if (saddr == NULL)
312 saddr = dev->dev_addr;
313
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800314 dev = vlan_dev_info(dev)->real_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Patrick McHardy2029cc22008-01-21 00:26:41 -0800316 /* MPLS can send us skbuffs w/out enough space. This check will grow
317 * the skb if it doesn't have enough headroom. Not a beautiful solution,
318 * so I'll tick a counter so that users can know it's happening...
319 * If they care...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 */
321
Patrick McHardy2029cc22008-01-21 00:26:41 -0800322 /* NOTE: This may still break if the underlying device is not the final
323 * device (and thus there are more headers to add...) It should work for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 * good-ole-ethernet though.
325 */
326 if (skb_headroom(skb) < dev->hard_header_len) {
327 struct sk_buff *sk_tmp = skb;
328 skb = skb_realloc_headroom(sk_tmp, dev->hard_header_len);
329 kfree_skb(sk_tmp);
330 if (skb == NULL) {
Patrick McHardy7bd38d72008-01-21 00:19:31 -0800331 struct net_device_stats *stats = &vdev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 stats->tx_dropped++;
333 return -ENOMEM;
334 }
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800335 vlan_dev_info(vdev)->cnt_inc_headroom_on_tx++;
Patrick McHardy2029cc22008-01-21 00:26:41 -0800336 pr_debug("%s: %s: had to grow skb\n", __FUNCTION__, vdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338
339 if (build_vlan_header) {
340 /* Now make the underlying real hard header */
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700341 rc = dev_hard_header(skb, dev, ETH_P_8021Q, daddr, saddr,
342 len + VLAN_HLEN);
343 if (rc > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 rc += VLAN_HLEN;
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700345 else if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 rc -= VLAN_HLEN;
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700347 } else
Patrick McHardy2029cc22008-01-21 00:26:41 -0800348 /* If here, then we'll just make a normal looking ethernet
349 * frame, but, the hard_start_xmit method will insert the tag
350 * (it has to be able to do this for bridged and other skbs
351 * that don't come down the protocol stack in an orderly manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 */
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700353 rc = dev_hard_header(skb, dev, type, daddr, saddr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 return rc;
356}
357
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800358static int vlan_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Patrick McHardy7bd38d72008-01-21 00:19:31 -0800360 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
362
363 /* Handle non-VLAN frames if they are sent to us, for example by DHCP.
364 *
365 * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
366 * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
367 */
368
Joonwoo Park6ab3b482007-11-29 22:16:41 +1100369 if (veth->h_vlan_proto != htons(ETH_P_8021Q) ||
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800370 vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 int orig_headroom = skb_headroom(skb);
372 unsigned short veth_TCI;
373
374 /* This is not a VLAN frame...but we can fix that! */
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800375 vlan_dev_info(dev)->cnt_encap_on_xmit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Patrick McHardy40f98e12008-01-21 00:24:30 -0800377 pr_debug("%s: proto to encap: 0x%hx\n",
378 __FUNCTION__, htons(veth->h_vlan_proto));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /* Construct the second two bytes. This field looks something
380 * like:
381 * usr_priority: 3 bits (high bits)
382 * CFI 1 bit
383 * VLAN ID 12 bits (low bits)
384 */
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800385 veth_TCI = vlan_dev_info(dev)->vlan_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
387
388 skb = __vlan_put_tag(skb, veth_TCI);
389 if (!skb) {
390 stats->tx_dropped++;
391 return 0;
392 }
393
Patrick McHardy2029cc22008-01-21 00:26:41 -0800394 if (orig_headroom < VLAN_HLEN)
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800395 vlan_dev_info(dev)->cnt_inc_headroom_on_tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397
Patrick McHardy40f98e12008-01-21 00:24:30 -0800398 pr_debug("%s: about to send skb: %p to dev: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 __FUNCTION__, skb, skb->dev->name);
Patrick McHardy40f98e12008-01-21 00:24:30 -0800400 pr_debug(" " MAC_FMT " " MAC_FMT " %4hx %4hx %4hx\n",
401 veth->h_dest[0], veth->h_dest[1], veth->h_dest[2],
402 veth->h_dest[3], veth->h_dest[4], veth->h_dest[5],
403 veth->h_source[0], veth->h_source[1], veth->h_source[2],
404 veth->h_source[3], veth->h_source[4], veth->h_source[5],
405 veth->h_vlan_proto, veth->h_vlan_TCI,
406 veth->h_vlan_encapsulated_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 stats->tx_packets++; /* for statics only */
409 stats->tx_bytes += skb->len;
410
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800411 skb->dev = vlan_dev_info(dev)->real_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 dev_queue_xmit(skb);
413
414 return 0;
415}
416
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800417static int vlan_dev_hwaccel_hard_start_xmit(struct sk_buff *skb,
418 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Patrick McHardy7bd38d72008-01-21 00:19:31 -0800420 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 unsigned short veth_TCI;
422
423 /* Construct the second two bytes. This field looks something
424 * like:
425 * usr_priority: 3 bits (high bits)
426 * CFI 1 bit
427 * VLAN ID 12 bits (low bits)
428 */
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800429 veth_TCI = vlan_dev_info(dev)->vlan_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
431 skb = __vlan_hwaccel_put_tag(skb, veth_TCI);
432
433 stats->tx_packets++;
434 stats->tx_bytes += skb->len;
435
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800436 skb->dev = vlan_dev_info(dev)->real_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 dev_queue_xmit(skb);
438
439 return 0;
440}
441
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800442static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 /* TODO: gotta make sure the underlying layer can handle it,
445 * maybe an IFF_VLAN_CAPABLE flag for devices?
446 */
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800447 if (vlan_dev_info(dev)->real_dev->mtu < new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return -ERANGE;
449
450 dev->mtu = new_mtu;
451
452 return 0;
453}
454
Patrick McHardyc17d8872007-06-13 12:05:22 -0700455void vlan_dev_set_ingress_priority(const struct net_device *dev,
456 u32 skb_prio, short vlan_prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800458 struct vlan_dev_info *vlan = vlan_dev_info(dev);
Patrick McHardyb020cb42007-06-13 12:07:22 -0700459
460 if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio)
461 vlan->nr_ingress_mappings--;
462 else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio)
463 vlan->nr_ingress_mappings++;
464
465 vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Patrick McHardyc17d8872007-06-13 12:05:22 -0700468int vlan_dev_set_egress_priority(const struct net_device *dev,
469 u32 skb_prio, short vlan_prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800471 struct vlan_dev_info *vlan = vlan_dev_info(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 struct vlan_priority_tci_mapping *mp = NULL;
473 struct vlan_priority_tci_mapping *np;
Patrick McHardyb020cb42007-06-13 12:07:22 -0700474 u32 vlan_qos = (vlan_prio << 13) & 0xE000;
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900475
Patrick McHardyc17d8872007-06-13 12:05:22 -0700476 /* See if a priority mapping exists.. */
Patrick McHardyb020cb42007-06-13 12:07:22 -0700477 mp = vlan->egress_priority_map[skb_prio & 0xF];
Patrick McHardyc17d8872007-06-13 12:05:22 -0700478 while (mp) {
479 if (mp->priority == skb_prio) {
Patrick McHardyb020cb42007-06-13 12:07:22 -0700480 if (mp->vlan_qos && !vlan_qos)
481 vlan->nr_egress_mappings--;
482 else if (!mp->vlan_qos && vlan_qos)
483 vlan->nr_egress_mappings++;
484 mp->vlan_qos = vlan_qos;
Patrick McHardyc17d8872007-06-13 12:05:22 -0700485 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
Patrick McHardyc17d8872007-06-13 12:05:22 -0700487 mp = mp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
Patrick McHardyc17d8872007-06-13 12:05:22 -0700489
490 /* Create a new mapping then. */
Patrick McHardyb020cb42007-06-13 12:07:22 -0700491 mp = vlan->egress_priority_map[skb_prio & 0xF];
Patrick McHardyc17d8872007-06-13 12:05:22 -0700492 np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL);
493 if (!np)
494 return -ENOBUFS;
495
496 np->next = mp;
497 np->priority = skb_prio;
Patrick McHardyb020cb42007-06-13 12:07:22 -0700498 np->vlan_qos = vlan_qos;
499 vlan->egress_priority_map[skb_prio & 0xF] = np;
500 if (vlan_qos)
501 vlan->nr_egress_mappings++;
Patrick McHardyc17d8872007-06-13 12:05:22 -0700502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
Patrick McHardya4bf3af42007-06-13 12:07:37 -0700505/* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */
Patrick McHardyc17d8872007-06-13 12:05:22 -0700506int vlan_dev_set_vlan_flag(const struct net_device *dev,
507 u32 flag, short flag_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Patrick McHardyc17d8872007-06-13 12:05:22 -0700509 /* verify flag is supported */
Patrick McHardya4bf3af42007-06-13 12:07:37 -0700510 if (flag == VLAN_FLAG_REORDER_HDR) {
Patrick McHardy2029cc22008-01-21 00:26:41 -0800511 if (flag_val)
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800512 vlan_dev_info(dev)->flags |= VLAN_FLAG_REORDER_HDR;
Patrick McHardy2029cc22008-01-21 00:26:41 -0800513 else
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800514 vlan_dev_info(dev)->flags &= ~VLAN_FLAG_REORDER_HDR;
Patrick McHardyc17d8872007-06-13 12:05:22 -0700515 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return -EINVAL;
518}
519
Patrick McHardyc17d8872007-06-13 12:05:22 -0700520void vlan_dev_get_realdev_name(const struct net_device *dev, char *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800522 strncpy(result, vlan_dev_info(dev)->real_dev->name, 23);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
Patrick McHardyc17d8872007-06-13 12:05:22 -0700525void vlan_dev_get_vid(const struct net_device *dev, unsigned short *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800527 *result = vlan_dev_info(dev)->vlan_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800530static int vlan_dev_open(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800532 struct vlan_dev_info *vlan = vlan_dev_info(dev);
Patrick McHardy8c979c22007-07-11 19:45:24 -0700533 struct net_device *real_dev = vlan->real_dev;
534 int err;
535
536 if (!(real_dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return -ENETDOWN;
538
Patrick McHardy8c979c22007-07-11 19:45:24 -0700539 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr)) {
540 err = dev_unicast_add(real_dev, dev->dev_addr, ETH_ALEN);
541 if (err < 0)
542 return err;
543 }
544 memcpy(vlan->real_dev_addr, real_dev->dev_addr, ETH_ALEN);
545
Patrick McHardy6c78dcb2007-07-14 18:52:56 -0700546 if (dev->flags & IFF_ALLMULTI)
547 dev_set_allmulti(real_dev, 1);
548 if (dev->flags & IFF_PROMISC)
549 dev_set_promiscuity(real_dev, 1);
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return 0;
552}
553
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800554static int vlan_dev_stop(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800556 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
Patrick McHardy8c979c22007-07-11 19:45:24 -0700557
Patrick McHardy56addd62007-07-14 18:53:28 -0700558 dev_mc_unsync(real_dev, dev);
Patrick McHardy6c78dcb2007-07-14 18:52:56 -0700559 if (dev->flags & IFF_ALLMULTI)
560 dev_set_allmulti(real_dev, -1);
561 if (dev->flags & IFF_PROMISC)
562 dev_set_promiscuity(real_dev, -1);
563
Patrick McHardy8c979c22007-07-11 19:45:24 -0700564 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
565 dev_unicast_delete(real_dev, dev->dev_addr, dev->addr_len);
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return 0;
568}
569
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800570static int vlan_dev_set_mac_address(struct net_device *dev, void *p)
Patrick McHardy39aaac12007-11-10 21:52:35 -0800571{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800572 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
Patrick McHardy39aaac12007-11-10 21:52:35 -0800573 struct sockaddr *addr = p;
574 int err;
575
576 if (!is_valid_ether_addr(addr->sa_data))
577 return -EADDRNOTAVAIL;
578
579 if (!(dev->flags & IFF_UP))
580 goto out;
581
582 if (compare_ether_addr(addr->sa_data, real_dev->dev_addr)) {
583 err = dev_unicast_add(real_dev, addr->sa_data, ETH_ALEN);
584 if (err < 0)
585 return err;
586 }
587
588 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
589 dev_unicast_delete(real_dev, dev->dev_addr, ETH_ALEN);
590
591out:
592 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
593 return 0;
594}
595
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800596static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800598 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 struct ifreq ifrr;
600 int err = -EOPNOTSUPP;
601
602 strncpy(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
603 ifrr.ifr_ifru = ifr->ifr_ifru;
604
Patrick McHardy2029cc22008-01-21 00:26:41 -0800605 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 case SIOCGMIIPHY:
607 case SIOCGMIIREG:
608 case SIOCSMIIREG:
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900609 if (real_dev->do_ioctl && netif_device_present(real_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 err = real_dev->do_ioctl(real_dev, &ifrr, cmd);
611 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
613
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900614 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 ifr->ifr_ifru = ifrr.ifr_ifru;
616
617 return err;
618}
619
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800620static void vlan_dev_change_rx_flags(struct net_device *dev, int change)
Patrick McHardy6c78dcb2007-07-14 18:52:56 -0700621{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800622 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
Patrick McHardy6c78dcb2007-07-14 18:52:56 -0700623
624 if (change & IFF_ALLMULTI)
625 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
626 if (change & IFF_PROMISC)
627 dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1);
628}
629
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800630static void vlan_dev_set_multicast_list(struct net_device *vlan_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800632 dev_mc_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800634
635/*
636 * vlan network devices have devices nesting below it, and are a special
637 * "super class" of normal network devices; split their locks off into a
638 * separate class since they always nest.
639 */
640static struct lock_class_key vlan_netdev_xmit_lock_key;
641
642static const struct header_ops vlan_header_ops = {
643 .create = vlan_dev_hard_header,
644 .rebuild = vlan_dev_rebuild_header,
645 .parse = eth_header_parse,
646};
647
648static int vlan_dev_init(struct net_device *dev)
649{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800650 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800651 int subclass = 0;
652
653 /* IFF_BROADCAST|IFF_MULTICAST; ??? */
654 dev->flags = real_dev->flags & ~IFF_UP;
655 dev->iflink = real_dev->ifindex;
656 dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
657 (1<<__LINK_STATE_DORMANT))) |
658 (1<<__LINK_STATE_PRESENT);
659
660 /* ipv6 shared card related stuff */
661 dev->dev_id = real_dev->dev_id;
662
663 if (is_zero_ether_addr(dev->dev_addr))
664 memcpy(dev->dev_addr, real_dev->dev_addr, dev->addr_len);
665 if (is_zero_ether_addr(dev->broadcast))
666 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
667
668 if (real_dev->features & NETIF_F_HW_VLAN_TX) {
669 dev->header_ops = real_dev->header_ops;
670 dev->hard_header_len = real_dev->hard_header_len;
671 dev->hard_start_xmit = vlan_dev_hwaccel_hard_start_xmit;
672 } else {
673 dev->header_ops = &vlan_header_ops;
674 dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
675 dev->hard_start_xmit = vlan_dev_hard_start_xmit;
676 }
677
678 if (real_dev->priv_flags & IFF_802_1Q_VLAN)
679 subclass = 1;
680
681 lockdep_set_class_and_subclass(&dev->_xmit_lock,
682 &vlan_netdev_xmit_lock_key, subclass);
683 return 0;
684}
685
686void vlan_setup(struct net_device *dev)
687{
688 ether_setup(dev);
689
690 dev->priv_flags |= IFF_802_1Q_VLAN;
691 dev->tx_queue_len = 0;
692
693 dev->change_mtu = vlan_dev_change_mtu;
694 dev->init = vlan_dev_init;
695 dev->open = vlan_dev_open;
696 dev->stop = vlan_dev_stop;
697 dev->set_mac_address = vlan_dev_set_mac_address;
698 dev->set_multicast_list = vlan_dev_set_multicast_list;
699 dev->change_rx_flags = vlan_dev_change_rx_flags;
700 dev->do_ioctl = vlan_dev_ioctl;
701 dev->destructor = free_netdev;
702
703 memset(dev->broadcast, 0, ETH_ALEN);
704}