blob: 2ccac6bea57ee9973e206100fa7b37f661dd2aef [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>
Patrick McHardy75b88462008-07-08 03:22:42 -070031#include <linux/ethtool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <net/datalink.h>
33#include <net/p8022.h>
34#include <net/arp.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 */
Patrick McHardyef3eb3e2008-01-21 00:22:11 -080051static int vlan_dev_rebuild_header(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
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);
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +090062#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 default:
Patrick McHardy40f98e12008-01-21 00:24:30 -080064 pr_debug("%s: unable to resolve type %X addresses.\n",
65 dev->name, ntohs(veth->h_vlan_encapsulated_proto));
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +090066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 memcpy(veth->h_source, dev->dev_addr, ETH_ALEN);
68 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 return 0;
72}
73
74static inline struct sk_buff *vlan_check_reorder_header(struct sk_buff *skb)
75{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -080076 if (vlan_dev_info(skb->dev)->flags & VLAN_FLAG_REORDER_HDR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 if (skb_shared(skb) || skb_cloned(skb)) {
78 struct sk_buff *nskb = skb_copy(skb, GFP_ATOMIC);
79 kfree_skb(skb);
80 skb = nskb;
81 }
82 if (skb) {
83 /* Lifted from Gleb's VLAN code... */
84 memmove(skb->data - ETH_HLEN,
85 skb->data - VLAN_ETH_HLEN, 12);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -070086 skb->mac_header += VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
88 }
89
90 return skb;
91}
92
Pavel Emelyanov91b4f952008-01-21 00:28:03 -080093static inline void vlan_set_encap_proto(struct sk_buff *skb,
94 struct vlan_hdr *vhdr)
95{
96 __be16 proto;
97 unsigned char *rawp;
98
99 /*
100 * Was a VLAN packet, grab the encapsulated protocol, which the layer
101 * three protocols care about.
102 */
103
104 proto = vhdr->h_vlan_encapsulated_proto;
105 if (ntohs(proto) >= 1536) {
106 skb->protocol = proto;
107 return;
108 }
109
110 rawp = skb->data;
111 if (*(unsigned short *)rawp == 0xFFFF)
112 /*
113 * This is a magic hack to spot IPX packets. Older Novell
114 * breaks the protocol design and runs IPX over 802.3 without
115 * an 802.2 LLC layer. We look for FFFF which isn't a used
116 * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
117 * but does for the rest.
118 */
119 skb->protocol = htons(ETH_P_802_3);
120 else
121 /*
122 * Real 802.2 LLC
123 */
124 skb->protocol = htons(ETH_P_802_2);
125}
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/*
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900128 * Determine the packet's protocol ID. The rule here is that we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 * assume 802.3 if the type field is short enough to be a length.
130 * This is normal practice and works for any 'now in use' protocol.
131 *
132 * Also, at this point we assume that we ARE dealing exclusively with
133 * VLAN packets, or packets that should be made into VLAN packets based
134 * on a default VLAN ID.
135 *
136 * NOTE: Should be similar to ethernet/eth.c.
137 *
138 * SANITY NOTE: This method is called when a packet is moving up the stack
139 * towards userland. To get here, it would have already passed
140 * through the ethernet/eth.c eth_type_trans() method.
141 * SANITY NOTE 2: We are referencing to the VLAN_HDR frields, which MAY be
142 * stored UNALIGNED in the memory. RISC systems don't like
143 * such cases very much...
Patrick McHardy2029cc22008-01-21 00:26:41 -0800144 * SANITY NOTE 2a: According to Dave Miller & Alexey, it will always be
145 * aligned, so there doesn't need to be any of the unaligned
146 * stuff. It has been commented out now... --Ben
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 *
148 */
149int vlan_skb_recv(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy2029cc22008-01-21 00:26:41 -0800150 struct packet_type *ptype, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Evgeniy Polyakove7c243c2007-08-24 23:36:29 -0700152 struct vlan_hdr *vhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 struct net_device_stats *stats;
Patrick McHardy9bb85822008-07-08 03:24:44 -0700154 u16 vlan_id;
155 u16 vlan_tci;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Patrick McHardy2029cc22008-01-21 00:26:41 -0800157 skb = skb_share_check(skb, GFP_ATOMIC);
158 if (skb == NULL)
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800159 goto err_free;
Evgeniy Polyakove7c243c2007-08-24 23:36:29 -0700160
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800161 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
162 goto err_free;
Evgeniy Polyakove7c243c2007-08-24 23:36:29 -0700163
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800164 vhdr = (struct vlan_hdr *)skb->data;
Patrick McHardy9bb85822008-07-08 03:24:44 -0700165 vlan_tci = ntohs(vhdr->h_vlan_TCI);
166 vlan_id = vlan_tci & VLAN_VID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 rcu_read_lock();
Patrick McHardy9bb85822008-07-08 03:24:44 -0700169 skb->dev = __find_vlan_dev(dev, vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (!skb->dev) {
Patrick McHardy2029cc22008-01-21 00:26:41 -0800171 pr_debug("%s: ERROR: No net_device for VID: %u on dev: %s\n",
Patrick McHardy9bb85822008-07-08 03:24:44 -0700172 __func__, vlan_id, dev->name);
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800173 goto err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175
176 skb->dev->last_rx = jiffies;
177
Patrick McHardy7bd38d72008-01-21 00:19:31 -0800178 stats = &skb->dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 stats->rx_packets++;
180 stats->rx_bytes += skb->len;
181
Herbert Xucbb042f2006-03-20 22:43:56 -0800182 skb_pull_rcsum(skb, VLAN_HLEN);
Stephen Hemmingera3884422005-12-14 16:23:16 -0800183
Patrick McHardy9bb85822008-07-08 03:24:44 -0700184 skb->priority = vlan_get_ingress_priority(skb->dev, vlan_tci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Patrick McHardy40f98e12008-01-21 00:24:30 -0800186 pr_debug("%s: priority: %u for TCI: %hu\n",
Patrick McHardy9bb85822008-07-08 03:24:44 -0700187 __func__, skb->priority, vlan_tci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 switch (skb->pkt_type) {
190 case PACKET_BROADCAST: /* Yeah, stats collect these together.. */
Patrick McHardy2029cc22008-01-21 00:26:41 -0800191 /* stats->broadcast ++; // no such counter :-( */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 break;
193
194 case PACKET_MULTICAST:
195 stats->multicast++;
196 break;
197
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900198 case PACKET_OTHERHOST:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /* Our lower layer thinks this is not local, let's make sure.
Patrick McHardy2029cc22008-01-21 00:26:41 -0800200 * This allows the VLAN to have a different MAC than the
201 * underlying device, and still route correctly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 */
Patrick McHardy2029cc22008-01-21 00:26:41 -0800203 if (!compare_ether_addr(eth_hdr(skb)->h_dest,
204 skb->dev->dev_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 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
Pavel Emelyanov91b4f952008-01-21 00:28:03 -0800211 vlan_set_encap_proto(skb, vhdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 skb = vlan_check_reorder_header(skb);
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800214 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 stats->rx_errors++;
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800216 goto err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800218
219 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 rcu_read_unlock();
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800221 return NET_RX_SUCCESS;
222
223err_unlock:
224 rcu_read_unlock();
225err_free:
226 kfree_skb(skb);
227 return NET_RX_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Patrick McHardy9bb85822008-07-08 03:24:44 -0700230static inline u16
Patrick McHardy2029cc22008-01-21 00:26:41 -0800231vlan_dev_get_egress_qos_mask(struct net_device *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Patrick McHardy2029cc22008-01-21 00:26:41 -0800233 struct vlan_priority_tci_mapping *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Patrick McHardy2029cc22008-01-21 00:26:41 -0800235 mp = vlan_dev_info(dev)->egress_priority_map[(skb->priority & 0xF)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 while (mp) {
237 if (mp->priority == skb->priority) {
Patrick McHardy2029cc22008-01-21 00:26:41 -0800238 return mp->vlan_qos; /* This should already be shifted
239 * to mask correctly with the
240 * VLAN's TCI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242 mp = mp->next;
243 }
244 return 0;
245}
246
247/*
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900248 * Create the VLAN header for an arbitrary protocol layer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 *
250 * saddr=NULL means use device source address
251 * daddr=NULL means leave destination address (eg unresolved arp)
252 *
253 * This is called when the SKB is moving down the stack towards the
254 * physical devices.
255 */
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800256static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
257 unsigned short type,
258 const void *daddr, const void *saddr,
259 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 struct vlan_hdr *vhdr;
Patrick McHardy9bb85822008-07-08 03:24:44 -0700262 u16 vlan_tci = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 int rc = 0;
264 int build_vlan_header = 0;
Patrick McHardy2029cc22008-01-21 00:26:41 -0800265 struct net_device *vdev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Patrick McHardy40f98e12008-01-21 00:24:30 -0800267 pr_debug("%s: skb: %p type: %hx len: %u vlan_id: %hx, daddr: %p\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800268 __func__, skb, type, len, vlan_dev_info(dev)->vlan_id,
Patrick McHardy2029cc22008-01-21 00:26:41 -0800269 daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 /* build vlan header only if re_order_header flag is NOT set. This
272 * fixes some programs that get confused when they see a VLAN device
273 * sending a frame that is VLAN encoded (the consensus is that the VLAN
274 * device should look completely like an Ethernet device when the
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900275 * REORDER_HEADER flag is set) The drawback to this is some extra
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 * header shuffling in the hard_start_xmit. Users can turn off this
277 * REORDER behaviour with the vconfig tool.
278 */
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800279 if (!(vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR))
Patrick McHardya4bf3af42007-06-13 12:07:37 -0700280 build_vlan_header = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 if (build_vlan_header) {
283 vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN);
284
285 /* build the four bytes that make this a VLAN header. */
286
Patrick McHardy2029cc22008-01-21 00:26:41 -0800287 /* Now, construct the second two bytes. This field looks
288 * something like:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 * usr_priority: 3 bits (high bits)
290 * CFI 1 bit
291 * VLAN ID 12 bits (low bits)
292 *
293 */
Patrick McHardy9bb85822008-07-08 03:24:44 -0700294 vlan_tci = vlan_dev_info(dev)->vlan_id;
295 vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Patrick McHardy9bb85822008-07-08 03:24:44 -0700297 vhdr->h_vlan_TCI = htons(vlan_tci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 /*
Patrick McHardy2029cc22008-01-21 00:26:41 -0800300 * Set the protocol type. For a packet of type ETH_P_802_3 we
301 * put the length in here instead. It is up to the 802.2
302 * layer to carry protocol information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 */
304
Patrick McHardy2029cc22008-01-21 00:26:41 -0800305 if (type != ETH_P_802_3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 vhdr->h_vlan_encapsulated_proto = htons(type);
Patrick McHardy2029cc22008-01-21 00:26:41 -0800307 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 vhdr->h_vlan_encapsulated_proto = htons(len);
Jerome Borsboom279e1722007-04-13 16:12:47 -0700309
310 skb->protocol = htons(ETH_P_8021Q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312
313 /* Before delegating work to the lower layer, enter our MAC-address */
314 if (saddr == NULL)
315 saddr = dev->dev_addr;
316
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800317 dev = vlan_dev_info(dev)->real_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Patrick McHardy2029cc22008-01-21 00:26:41 -0800319 /* MPLS can send us skbuffs w/out enough space. This check will grow
320 * the skb if it doesn't have enough headroom. Not a beautiful solution,
321 * so I'll tick a counter so that users can know it's happening...
322 * If they care...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 */
324
Patrick McHardy2029cc22008-01-21 00:26:41 -0800325 /* NOTE: This may still break if the underlying device is not the final
326 * device (and thus there are more headers to add...) It should work for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 * good-ole-ethernet though.
328 */
329 if (skb_headroom(skb) < dev->hard_header_len) {
330 struct sk_buff *sk_tmp = skb;
331 skb = skb_realloc_headroom(sk_tmp, dev->hard_header_len);
332 kfree_skb(sk_tmp);
333 if (skb == NULL) {
Patrick McHardy7bd38d72008-01-21 00:19:31 -0800334 struct net_device_stats *stats = &vdev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 stats->tx_dropped++;
336 return -ENOMEM;
337 }
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800338 vlan_dev_info(vdev)->cnt_inc_headroom_on_tx++;
Harvey Harrison0dc47872008-03-05 20:47:47 -0800339 pr_debug("%s: %s: had to grow skb\n", __func__, vdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341
342 if (build_vlan_header) {
343 /* Now make the underlying real hard header */
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700344 rc = dev_hard_header(skb, dev, ETH_P_8021Q, daddr, saddr,
345 len + VLAN_HLEN);
346 if (rc > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 rc += VLAN_HLEN;
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700348 else if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 rc -= VLAN_HLEN;
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700350 } else
Patrick McHardy2029cc22008-01-21 00:26:41 -0800351 /* If here, then we'll just make a normal looking ethernet
352 * frame, but, the hard_start_xmit method will insert the tag
353 * (it has to be able to do this for bridged and other skbs
354 * that don't come down the protocol stack in an orderly manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 */
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700356 rc = dev_hard_header(skb, dev, type, daddr, saddr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 return rc;
359}
360
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800361static int vlan_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Patrick McHardy7bd38d72008-01-21 00:19:31 -0800363 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
David S. Miller55b01e82008-02-23 20:09:11 -0800365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /* Handle non-VLAN frames if they are sent to us, for example by DHCP.
367 *
368 * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
369 * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
370 */
371
Joonwoo Park6ab3b482007-11-29 22:16:41 +1100372 if (veth->h_vlan_proto != htons(ETH_P_8021Q) ||
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800373 vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 int orig_headroom = skb_headroom(skb);
Patrick McHardy9bb85822008-07-08 03:24:44 -0700375 u16 vlan_tci;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 /* This is not a VLAN frame...but we can fix that! */
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800378 vlan_dev_info(dev)->cnt_encap_on_xmit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Patrick McHardy40f98e12008-01-21 00:24:30 -0800380 pr_debug("%s: proto to encap: 0x%hx\n",
David S. Miller577f99c2008-03-18 00:37:55 -0700381 __func__, ntohs(veth->h_vlan_proto));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /* Construct the second two bytes. This field looks something
383 * like:
384 * usr_priority: 3 bits (high bits)
385 * CFI 1 bit
386 * VLAN ID 12 bits (low bits)
387 */
Patrick McHardy9bb85822008-07-08 03:24:44 -0700388 vlan_tci = vlan_dev_info(dev)->vlan_id;
389 vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Patrick McHardy9bb85822008-07-08 03:24:44 -0700391 skb = __vlan_put_tag(skb, vlan_tci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (!skb) {
393 stats->tx_dropped++;
394 return 0;
395 }
396
Patrick McHardy2029cc22008-01-21 00:26:41 -0800397 if (orig_headroom < VLAN_HLEN)
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800398 vlan_dev_info(dev)->cnt_inc_headroom_on_tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
Patrick McHardy40f98e12008-01-21 00:24:30 -0800401 pr_debug("%s: about to send skb: %p to dev: %s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800402 __func__, skb, skb->dev->name);
David S. Miller55b01e82008-02-23 20:09:11 -0800403 pr_debug(" " MAC_FMT " " MAC_FMT " %4hx %4hx %4hx\n",
404 veth->h_dest[0], veth->h_dest[1], veth->h_dest[2],
405 veth->h_dest[3], veth->h_dest[4], veth->h_dest[5],
406 veth->h_source[0], veth->h_source[1], veth->h_source[2],
407 veth->h_source[3], veth->h_source[4], veth->h_source[5],
Patrick McHardy40f98e12008-01-21 00:24:30 -0800408 veth->h_vlan_proto, veth->h_vlan_TCI,
409 veth->h_vlan_encapsulated_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 stats->tx_packets++; /* for statics only */
412 stats->tx_bytes += skb->len;
413
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800414 skb->dev = vlan_dev_info(dev)->real_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 dev_queue_xmit(skb);
416
417 return 0;
418}
419
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800420static int vlan_dev_hwaccel_hard_start_xmit(struct sk_buff *skb,
421 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Patrick McHardy7bd38d72008-01-21 00:19:31 -0800423 struct net_device_stats *stats = &dev->stats;
Patrick McHardy9bb85822008-07-08 03:24:44 -0700424 u16 vlan_tci;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 /* Construct the second two bytes. This field looks something
427 * like:
428 * usr_priority: 3 bits (high bits)
429 * CFI 1 bit
430 * VLAN ID 12 bits (low bits)
431 */
Patrick McHardy9bb85822008-07-08 03:24:44 -0700432 vlan_tci = vlan_dev_info(dev)->vlan_id;
433 vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
434 skb = __vlan_hwaccel_put_tag(skb, vlan_tci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 stats->tx_packets++;
437 stats->tx_bytes += skb->len;
438
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800439 skb->dev = vlan_dev_info(dev)->real_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 dev_queue_xmit(skb);
441
442 return 0;
443}
444
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800445static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 /* TODO: gotta make sure the underlying layer can handle it,
448 * maybe an IFF_VLAN_CAPABLE flag for devices?
449 */
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800450 if (vlan_dev_info(dev)->real_dev->mtu < new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return -ERANGE;
452
453 dev->mtu = new_mtu;
454
455 return 0;
456}
457
Patrick McHardyc17d8872007-06-13 12:05:22 -0700458void vlan_dev_set_ingress_priority(const struct net_device *dev,
Patrick McHardy9bb85822008-07-08 03:24:44 -0700459 u32 skb_prio, u16 vlan_prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800461 struct vlan_dev_info *vlan = vlan_dev_info(dev);
Patrick McHardyb020cb42007-06-13 12:07:22 -0700462
463 if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio)
464 vlan->nr_ingress_mappings--;
465 else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio)
466 vlan->nr_ingress_mappings++;
467
468 vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Patrick McHardyc17d8872007-06-13 12:05:22 -0700471int vlan_dev_set_egress_priority(const struct net_device *dev,
Patrick McHardy9bb85822008-07-08 03:24:44 -0700472 u32 skb_prio, u16 vlan_prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800474 struct vlan_dev_info *vlan = vlan_dev_info(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 struct vlan_priority_tci_mapping *mp = NULL;
476 struct vlan_priority_tci_mapping *np;
Patrick McHardyb020cb42007-06-13 12:07:22 -0700477 u32 vlan_qos = (vlan_prio << 13) & 0xE000;
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900478
Patrick McHardyc17d8872007-06-13 12:05:22 -0700479 /* See if a priority mapping exists.. */
Patrick McHardyb020cb42007-06-13 12:07:22 -0700480 mp = vlan->egress_priority_map[skb_prio & 0xF];
Patrick McHardyc17d8872007-06-13 12:05:22 -0700481 while (mp) {
482 if (mp->priority == skb_prio) {
Patrick McHardyb020cb42007-06-13 12:07:22 -0700483 if (mp->vlan_qos && !vlan_qos)
484 vlan->nr_egress_mappings--;
485 else if (!mp->vlan_qos && vlan_qos)
486 vlan->nr_egress_mappings++;
487 mp->vlan_qos = vlan_qos;
Patrick McHardyc17d8872007-06-13 12:05:22 -0700488 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
Patrick McHardyc17d8872007-06-13 12:05:22 -0700490 mp = mp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
Patrick McHardyc17d8872007-06-13 12:05:22 -0700492
493 /* Create a new mapping then. */
Patrick McHardyb020cb42007-06-13 12:07:22 -0700494 mp = vlan->egress_priority_map[skb_prio & 0xF];
Patrick McHardyc17d8872007-06-13 12:05:22 -0700495 np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL);
496 if (!np)
497 return -ENOBUFS;
498
499 np->next = mp;
500 np->priority = skb_prio;
Patrick McHardyb020cb42007-06-13 12:07:22 -0700501 np->vlan_qos = vlan_qos;
502 vlan->egress_priority_map[skb_prio & 0xF] = np;
503 if (vlan_qos)
504 vlan->nr_egress_mappings++;
Patrick McHardyc17d8872007-06-13 12:05:22 -0700505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
Patrick McHardya4bf3af42007-06-13 12:07:37 -0700508/* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */
Patrick McHardyb3ce0322008-07-05 21:26:27 -0700509int vlan_dev_change_flags(const struct net_device *dev, u32 flags, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Patrick McHardyb3ce0322008-07-05 21:26:27 -0700511 struct vlan_dev_info *vlan = vlan_dev_info(dev);
512 u32 old_flags = vlan->flags;
513
Patrick McHardy70c03b42008-07-05 21:26:57 -0700514 if (mask & ~(VLAN_FLAG_REORDER_HDR | VLAN_FLAG_GVRP))
Patrick McHardyb3ce0322008-07-05 21:26:27 -0700515 return -EINVAL;
516
517 vlan->flags = (old_flags & ~mask) | (flags & mask);
Patrick McHardy70c03b42008-07-05 21:26:57 -0700518
519 if (netif_running(dev) && (vlan->flags ^ old_flags) & VLAN_FLAG_GVRP) {
520 if (vlan->flags & VLAN_FLAG_GVRP)
521 vlan_gvrp_request_join(dev);
522 else
523 vlan_gvrp_request_leave(dev);
524 }
Patrick McHardyb3ce0322008-07-05 21:26:27 -0700525 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
Patrick McHardyc17d8872007-06-13 12:05:22 -0700528void vlan_dev_get_realdev_name(const struct net_device *dev, char *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800530 strncpy(result, vlan_dev_info(dev)->real_dev->name, 23);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800533static int vlan_dev_open(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800535 struct vlan_dev_info *vlan = vlan_dev_info(dev);
Patrick McHardy8c979c22007-07-11 19:45:24 -0700536 struct net_device *real_dev = vlan->real_dev;
537 int err;
538
539 if (!(real_dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return -ENETDOWN;
541
Patrick McHardy8c979c22007-07-11 19:45:24 -0700542 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr)) {
543 err = dev_unicast_add(real_dev, dev->dev_addr, ETH_ALEN);
544 if (err < 0)
545 return err;
546 }
547 memcpy(vlan->real_dev_addr, real_dev->dev_addr, ETH_ALEN);
548
Patrick McHardy6c78dcb2007-07-14 18:52:56 -0700549 if (dev->flags & IFF_ALLMULTI)
550 dev_set_allmulti(real_dev, 1);
551 if (dev->flags & IFF_PROMISC)
552 dev_set_promiscuity(real_dev, 1);
553
Patrick McHardy70c03b42008-07-05 21:26:57 -0700554 if (vlan->flags & VLAN_FLAG_GVRP)
555 vlan_gvrp_request_join(dev);
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return 0;
558}
559
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800560static int vlan_dev_stop(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Patrick McHardy70c03b42008-07-05 21:26:57 -0700562 struct vlan_dev_info *vlan = vlan_dev_info(dev);
563 struct net_device *real_dev = vlan->real_dev;
564
565 if (vlan->flags & VLAN_FLAG_GVRP)
566 vlan_gvrp_request_leave(dev);
Patrick McHardy8c979c22007-07-11 19:45:24 -0700567
Patrick McHardy56addd62007-07-14 18:53:28 -0700568 dev_mc_unsync(real_dev, dev);
Chris Leeche83a2ea2008-01-31 16:53:23 -0800569 dev_unicast_unsync(real_dev, dev);
Patrick McHardy6c78dcb2007-07-14 18:52:56 -0700570 if (dev->flags & IFF_ALLMULTI)
571 dev_set_allmulti(real_dev, -1);
572 if (dev->flags & IFF_PROMISC)
573 dev_set_promiscuity(real_dev, -1);
574
Patrick McHardy8c979c22007-07-11 19:45:24 -0700575 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
576 dev_unicast_delete(real_dev, dev->dev_addr, dev->addr_len);
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return 0;
579}
580
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800581static int vlan_dev_set_mac_address(struct net_device *dev, void *p)
Patrick McHardy39aaac12007-11-10 21:52:35 -0800582{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800583 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
Patrick McHardy39aaac12007-11-10 21:52:35 -0800584 struct sockaddr *addr = p;
585 int err;
586
587 if (!is_valid_ether_addr(addr->sa_data))
588 return -EADDRNOTAVAIL;
589
590 if (!(dev->flags & IFF_UP))
591 goto out;
592
593 if (compare_ether_addr(addr->sa_data, real_dev->dev_addr)) {
594 err = dev_unicast_add(real_dev, addr->sa_data, ETH_ALEN);
595 if (err < 0)
596 return err;
597 }
598
599 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
600 dev_unicast_delete(real_dev, dev->dev_addr, ETH_ALEN);
601
602out:
603 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
604 return 0;
605}
606
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800607static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800609 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 struct ifreq ifrr;
611 int err = -EOPNOTSUPP;
612
613 strncpy(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
614 ifrr.ifr_ifru = ifr->ifr_ifru;
615
Patrick McHardy2029cc22008-01-21 00:26:41 -0800616 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 case SIOCGMIIPHY:
618 case SIOCGMIIREG:
619 case SIOCSMIIREG:
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900620 if (real_dev->do_ioctl && netif_device_present(real_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 err = real_dev->do_ioctl(real_dev, &ifrr, cmd);
622 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900625 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 ifr->ifr_ifru = ifrr.ifr_ifru;
627
628 return err;
629}
630
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800631static void vlan_dev_change_rx_flags(struct net_device *dev, int change)
Patrick McHardy6c78dcb2007-07-14 18:52:56 -0700632{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800633 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
Patrick McHardy6c78dcb2007-07-14 18:52:56 -0700634
635 if (change & IFF_ALLMULTI)
636 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
637 if (change & IFF_PROMISC)
638 dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1);
639}
640
Chris Leeche83a2ea2008-01-31 16:53:23 -0800641static void vlan_dev_set_rx_mode(struct net_device *vlan_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800643 dev_mc_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
Chris Leeche83a2ea2008-01-31 16:53:23 -0800644 dev_unicast_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800646
647/*
648 * vlan network devices have devices nesting below it, and are a special
649 * "super class" of normal network devices; split their locks off into a
650 * separate class since they always nest.
651 */
652static struct lock_class_key vlan_netdev_xmit_lock_key;
653
654static const struct header_ops vlan_header_ops = {
655 .create = vlan_dev_hard_header,
656 .rebuild = vlan_dev_rebuild_header,
657 .parse = eth_header_parse,
658};
659
660static int vlan_dev_init(struct net_device *dev)
661{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800662 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800663 int subclass = 0;
664
665 /* IFF_BROADCAST|IFF_MULTICAST; ??? */
Patrick McHardy0ed21b32008-03-26 00:15:17 -0700666 dev->flags = real_dev->flags & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI);
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800667 dev->iflink = real_dev->ifindex;
668 dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
669 (1<<__LINK_STATE_DORMANT))) |
670 (1<<__LINK_STATE_PRESENT);
671
Patrick McHardy289c79a2008-05-23 00:22:04 -0700672 dev->features |= real_dev->features & real_dev->vlan_features;
Patrick McHardy5fb13572008-05-20 14:54:50 -0700673
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800674 /* ipv6 shared card related stuff */
675 dev->dev_id = real_dev->dev_id;
676
677 if (is_zero_ether_addr(dev->dev_addr))
678 memcpy(dev->dev_addr, real_dev->dev_addr, dev->addr_len);
679 if (is_zero_ether_addr(dev->broadcast))
680 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
681
682 if (real_dev->features & NETIF_F_HW_VLAN_TX) {
683 dev->header_ops = real_dev->header_ops;
684 dev->hard_header_len = real_dev->hard_header_len;
685 dev->hard_start_xmit = vlan_dev_hwaccel_hard_start_xmit;
686 } else {
687 dev->header_ops = &vlan_header_ops;
688 dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
689 dev->hard_start_xmit = vlan_dev_hard_start_xmit;
690 }
691
Joonwoo Park26a25232008-07-08 03:22:16 -0700692 if (is_vlan_dev(real_dev))
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800693 subclass = 1;
694
695 lockdep_set_class_and_subclass(&dev->_xmit_lock,
696 &vlan_netdev_xmit_lock_key, subclass);
697 return 0;
698}
699
Pavel Emelyanov23556322008-04-04 12:45:12 -0700700static void vlan_dev_uninit(struct net_device *dev)
701{
702 struct vlan_priority_tci_mapping *pm;
703 struct vlan_dev_info *vlan = vlan_dev_info(dev);
704 int i;
705
706 for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
707 while ((pm = vlan->egress_priority_map[i]) != NULL) {
708 vlan->egress_priority_map[i] = pm->next;
709 kfree(pm);
710 }
711 }
712}
713
Patrick McHardy75b88462008-07-08 03:22:42 -0700714static u32 vlan_ethtool_get_rx_csum(struct net_device *dev)
715{
716 const struct vlan_dev_info *vlan = vlan_dev_info(dev);
717 struct net_device *real_dev = vlan->real_dev;
718
719 if (real_dev->ethtool_ops == NULL ||
720 real_dev->ethtool_ops->get_rx_csum == NULL)
721 return 0;
722 return real_dev->ethtool_ops->get_rx_csum(real_dev);
723}
724
725static const struct ethtool_ops vlan_ethtool_ops = {
726 .get_link = ethtool_op_get_link,
727 .get_rx_csum = vlan_ethtool_get_rx_csum,
728};
729
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800730void vlan_setup(struct net_device *dev)
731{
732 ether_setup(dev);
733
734 dev->priv_flags |= IFF_802_1Q_VLAN;
735 dev->tx_queue_len = 0;
736
737 dev->change_mtu = vlan_dev_change_mtu;
738 dev->init = vlan_dev_init;
Pavel Emelyanov23556322008-04-04 12:45:12 -0700739 dev->uninit = vlan_dev_uninit;
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800740 dev->open = vlan_dev_open;
741 dev->stop = vlan_dev_stop;
742 dev->set_mac_address = vlan_dev_set_mac_address;
Chris Leeche83a2ea2008-01-31 16:53:23 -0800743 dev->set_rx_mode = vlan_dev_set_rx_mode;
744 dev->set_multicast_list = vlan_dev_set_rx_mode;
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800745 dev->change_rx_flags = vlan_dev_change_rx_flags;
746 dev->do_ioctl = vlan_dev_ioctl;
747 dev->destructor = free_netdev;
Patrick McHardy75b88462008-07-08 03:22:42 -0700748 dev->ethtool_ops = &vlan_ethtool_ops;
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800749
750 memset(dev->broadcast, 0, ETH_ALEN);
751}