blob: 9efd3c67c1db1537afbb7eb4b53ce7a770e2136d [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) {
Patrick McHardy11a100f2008-07-08 15:36:57 -070077 if (skb_cow(skb, skb_headroom(skb)) < 0)
78 skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if (skb) {
80 /* Lifted from Gleb's VLAN code... */
81 memmove(skb->data - ETH_HLEN,
82 skb->data - VLAN_ETH_HLEN, 12);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -070083 skb->mac_header += VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85 }
86
87 return skb;
88}
89
Pavel Emelyanov91b4f952008-01-21 00:28:03 -080090static inline void vlan_set_encap_proto(struct sk_buff *skb,
91 struct vlan_hdr *vhdr)
92{
93 __be16 proto;
94 unsigned char *rawp;
95
96 /*
97 * Was a VLAN packet, grab the encapsulated protocol, which the layer
98 * three protocols care about.
99 */
100
101 proto = vhdr->h_vlan_encapsulated_proto;
102 if (ntohs(proto) >= 1536) {
103 skb->protocol = proto;
104 return;
105 }
106
107 rawp = skb->data;
108 if (*(unsigned short *)rawp == 0xFFFF)
109 /*
110 * This is a magic hack to spot IPX packets. Older Novell
111 * breaks the protocol design and runs IPX over 802.3 without
112 * an 802.2 LLC layer. We look for FFFF which isn't a used
113 * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
114 * but does for the rest.
115 */
116 skb->protocol = htons(ETH_P_802_3);
117 else
118 /*
119 * Real 802.2 LLC
120 */
121 skb->protocol = htons(ETH_P_802_2);
122}
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/*
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900125 * Determine the packet's protocol ID. The rule here is that we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 * assume 802.3 if the type field is short enough to be a length.
127 * This is normal practice and works for any 'now in use' protocol.
128 *
129 * Also, at this point we assume that we ARE dealing exclusively with
130 * VLAN packets, or packets that should be made into VLAN packets based
131 * on a default VLAN ID.
132 *
133 * NOTE: Should be similar to ethernet/eth.c.
134 *
135 * SANITY NOTE: This method is called when a packet is moving up the stack
136 * towards userland. To get here, it would have already passed
137 * through the ethernet/eth.c eth_type_trans() method.
138 * SANITY NOTE 2: We are referencing to the VLAN_HDR frields, which MAY be
139 * stored UNALIGNED in the memory. RISC systems don't like
140 * such cases very much...
Patrick McHardy2029cc22008-01-21 00:26:41 -0800141 * SANITY NOTE 2a: According to Dave Miller & Alexey, it will always be
142 * aligned, so there doesn't need to be any of the unaligned
143 * stuff. It has been commented out now... --Ben
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 *
145 */
146int vlan_skb_recv(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy2029cc22008-01-21 00:26:41 -0800147 struct packet_type *ptype, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Evgeniy Polyakove7c243c2007-08-24 23:36:29 -0700149 struct vlan_hdr *vhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 struct net_device_stats *stats;
Patrick McHardy9bb85822008-07-08 03:24:44 -0700151 u16 vlan_id;
152 u16 vlan_tci;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Patrick McHardy2029cc22008-01-21 00:26:41 -0800154 skb = skb_share_check(skb, GFP_ATOMIC);
155 if (skb == NULL)
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800156 goto err_free;
Evgeniy Polyakove7c243c2007-08-24 23:36:29 -0700157
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800158 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
159 goto err_free;
Evgeniy Polyakove7c243c2007-08-24 23:36:29 -0700160
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800161 vhdr = (struct vlan_hdr *)skb->data;
Patrick McHardy9bb85822008-07-08 03:24:44 -0700162 vlan_tci = ntohs(vhdr->h_vlan_TCI);
163 vlan_id = vlan_tci & VLAN_VID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 rcu_read_lock();
Patrick McHardy9bb85822008-07-08 03:24:44 -0700166 skb->dev = __find_vlan_dev(dev, vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (!skb->dev) {
Patrick McHardy2029cc22008-01-21 00:26:41 -0800168 pr_debug("%s: ERROR: No net_device for VID: %u on dev: %s\n",
Patrick McHardy9bb85822008-07-08 03:24:44 -0700169 __func__, vlan_id, dev->name);
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800170 goto err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172
173 skb->dev->last_rx = jiffies;
174
Patrick McHardy7bd38d72008-01-21 00:19:31 -0800175 stats = &skb->dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 stats->rx_packets++;
177 stats->rx_bytes += skb->len;
178
Herbert Xucbb042f2006-03-20 22:43:56 -0800179 skb_pull_rcsum(skb, VLAN_HLEN);
Stephen Hemmingera3884422005-12-14 16:23:16 -0800180
Patrick McHardy9bb85822008-07-08 03:24:44 -0700181 skb->priority = vlan_get_ingress_priority(skb->dev, vlan_tci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Patrick McHardy40f98e12008-01-21 00:24:30 -0800183 pr_debug("%s: priority: %u for TCI: %hu\n",
Patrick McHardy9bb85822008-07-08 03:24:44 -0700184 __func__, skb->priority, vlan_tci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 switch (skb->pkt_type) {
187 case PACKET_BROADCAST: /* Yeah, stats collect these together.. */
Patrick McHardy2029cc22008-01-21 00:26:41 -0800188 /* stats->broadcast ++; // no such counter :-( */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 break;
190
191 case PACKET_MULTICAST:
192 stats->multicast++;
193 break;
194
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900195 case PACKET_OTHERHOST:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /* Our lower layer thinks this is not local, let's make sure.
Patrick McHardy2029cc22008-01-21 00:26:41 -0800197 * This allows the VLAN to have a different MAC than the
198 * underlying device, and still route correctly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 */
Patrick McHardy2029cc22008-01-21 00:26:41 -0800200 if (!compare_ether_addr(eth_hdr(skb)->h_dest,
201 skb->dev->dev_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 skb->pkt_type = PACKET_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 break;
204 default:
205 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Pavel Emelyanov91b4f952008-01-21 00:28:03 -0800208 vlan_set_encap_proto(skb, vhdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 skb = vlan_check_reorder_header(skb);
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800211 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 stats->rx_errors++;
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800213 goto err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800215
216 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 rcu_read_unlock();
Patrick McHardy31ffdbcb2008-01-21 00:27:18 -0800218 return NET_RX_SUCCESS;
219
220err_unlock:
221 rcu_read_unlock();
222err_free:
223 kfree_skb(skb);
224 return NET_RX_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
Patrick McHardy9bb85822008-07-08 03:24:44 -0700227static inline u16
Patrick McHardy2029cc22008-01-21 00:26:41 -0800228vlan_dev_get_egress_qos_mask(struct net_device *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Patrick McHardy2029cc22008-01-21 00:26:41 -0800230 struct vlan_priority_tci_mapping *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Patrick McHardy2029cc22008-01-21 00:26:41 -0800232 mp = vlan_dev_info(dev)->egress_priority_map[(skb->priority & 0xF)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 while (mp) {
234 if (mp->priority == skb->priority) {
Patrick McHardy2029cc22008-01-21 00:26:41 -0800235 return mp->vlan_qos; /* This should already be shifted
236 * to mask correctly with the
237 * VLAN's TCI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239 mp = mp->next;
240 }
241 return 0;
242}
243
244/*
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900245 * Create the VLAN header for an arbitrary protocol layer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 *
247 * saddr=NULL means use device source address
248 * daddr=NULL means leave destination address (eg unresolved arp)
249 *
250 * This is called when the SKB is moving down the stack towards the
251 * physical devices.
252 */
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800253static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
254 unsigned short type,
255 const void *daddr, const void *saddr,
256 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 struct vlan_hdr *vhdr;
Patrick McHardy9bb85822008-07-08 03:24:44 -0700259 u16 vlan_tci = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 int rc = 0;
261 int build_vlan_header = 0;
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",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800264 __func__, skb, type, len, vlan_dev_info(dev)->vlan_id,
Patrick McHardy2029cc22008-01-21 00:26:41 -0800265 daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Patrick McHardy11a100f2008-07-08 15:36:57 -0700267 if (WARN_ON(skb_headroom(skb) < dev->hard_header_len))
268 return -ENOSPC;
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 /* build vlan header only if re_order_header flag is NOT set. This
271 * fixes some programs that get confused when they see a VLAN device
272 * sending a frame that is VLAN encoded (the consensus is that the VLAN
273 * device should look completely like an Ethernet device when the
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900274 * REORDER_HEADER flag is set) The drawback to this is some extra
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 * header shuffling in the hard_start_xmit. Users can turn off this
276 * REORDER behaviour with the vconfig tool.
277 */
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800278 if (!(vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR))
Patrick McHardya4bf3af42007-06-13 12:07:37 -0700279 build_vlan_header = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 if (build_vlan_header) {
282 vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN);
283
284 /* build the four bytes that make this a VLAN header. */
285
Patrick McHardy2029cc22008-01-21 00:26:41 -0800286 /* Now, construct the second two bytes. This field looks
287 * something like:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 * usr_priority: 3 bits (high bits)
289 * CFI 1 bit
290 * VLAN ID 12 bits (low bits)
291 *
292 */
Patrick McHardy9bb85822008-07-08 03:24:44 -0700293 vlan_tci = vlan_dev_info(dev)->vlan_id;
294 vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Patrick McHardy9bb85822008-07-08 03:24:44 -0700296 vhdr->h_vlan_TCI = htons(vlan_tci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 /*
Patrick McHardy2029cc22008-01-21 00:26:41 -0800299 * Set the protocol type. For a packet of type ETH_P_802_3 we
300 * put the length in here instead. It is up to the 802.2
301 * layer to carry protocol information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 */
303
Patrick McHardy2029cc22008-01-21 00:26:41 -0800304 if (type != ETH_P_802_3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 vhdr->h_vlan_encapsulated_proto = htons(type);
Patrick McHardy2029cc22008-01-21 00:26:41 -0800306 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 vhdr->h_vlan_encapsulated_proto = htons(len);
Jerome Borsboom279e1722007-04-13 16:12:47 -0700308
309 skb->protocol = htons(ETH_P_8021Q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311
312 /* Before delegating work to the lower layer, enter our MAC-address */
313 if (saddr == NULL)
314 saddr = dev->dev_addr;
315
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800316 dev = vlan_dev_info(dev)->real_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (build_vlan_header) {
319 /* Now make the underlying real hard header */
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700320 rc = dev_hard_header(skb, dev, ETH_P_8021Q, daddr, saddr,
321 len + VLAN_HLEN);
322 if (rc > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 rc += VLAN_HLEN;
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700324 else if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 rc -= VLAN_HLEN;
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700326 } else
Patrick McHardy2029cc22008-01-21 00:26:41 -0800327 /* If here, then we'll just make a normal looking ethernet
328 * frame, but, the hard_start_xmit method will insert the tag
329 * (it has to be able to do this for bridged and other skbs
330 * that don't come down the protocol stack in an orderly manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 */
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700332 rc = dev_hard_header(skb, dev, type, daddr, saddr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 return rc;
335}
336
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800337static int vlan_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Patrick McHardy7bd38d72008-01-21 00:19:31 -0800339 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
David S. Miller55b01e82008-02-23 20:09:11 -0800341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 /* Handle non-VLAN frames if they are sent to us, for example by DHCP.
343 *
344 * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
345 * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
346 */
347
Joonwoo Park6ab3b482007-11-29 22:16:41 +1100348 if (veth->h_vlan_proto != htons(ETH_P_8021Q) ||
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800349 vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 int orig_headroom = skb_headroom(skb);
Patrick McHardy9bb85822008-07-08 03:24:44 -0700351 u16 vlan_tci;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 /* This is not a VLAN frame...but we can fix that! */
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800354 vlan_dev_info(dev)->cnt_encap_on_xmit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Patrick McHardy40f98e12008-01-21 00:24:30 -0800356 pr_debug("%s: proto to encap: 0x%hx\n",
David S. Miller577f99c2008-03-18 00:37:55 -0700357 __func__, ntohs(veth->h_vlan_proto));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 /* Construct the second two bytes. This field looks something
359 * like:
360 * usr_priority: 3 bits (high bits)
361 * CFI 1 bit
362 * VLAN ID 12 bits (low bits)
363 */
Patrick McHardy9bb85822008-07-08 03:24:44 -0700364 vlan_tci = vlan_dev_info(dev)->vlan_id;
365 vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Patrick McHardy9bb85822008-07-08 03:24:44 -0700367 skb = __vlan_put_tag(skb, vlan_tci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (!skb) {
369 stats->tx_dropped++;
370 return 0;
371 }
372
Patrick McHardy2029cc22008-01-21 00:26:41 -0800373 if (orig_headroom < VLAN_HLEN)
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800374 vlan_dev_info(dev)->cnt_inc_headroom_on_tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376
Patrick McHardy40f98e12008-01-21 00:24:30 -0800377 pr_debug("%s: about to send skb: %p to dev: %s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800378 __func__, skb, skb->dev->name);
David S. Miller55b01e82008-02-23 20:09:11 -0800379 pr_debug(" " MAC_FMT " " MAC_FMT " %4hx %4hx %4hx\n",
380 veth->h_dest[0], veth->h_dest[1], veth->h_dest[2],
381 veth->h_dest[3], veth->h_dest[4], veth->h_dest[5],
382 veth->h_source[0], veth->h_source[1], veth->h_source[2],
383 veth->h_source[3], veth->h_source[4], veth->h_source[5],
Patrick McHardy40f98e12008-01-21 00:24:30 -0800384 veth->h_vlan_proto, veth->h_vlan_TCI,
385 veth->h_vlan_encapsulated_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 stats->tx_packets++; /* for statics only */
388 stats->tx_bytes += skb->len;
389
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800390 skb->dev = vlan_dev_info(dev)->real_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 dev_queue_xmit(skb);
392
393 return 0;
394}
395
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800396static int vlan_dev_hwaccel_hard_start_xmit(struct sk_buff *skb,
397 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Patrick McHardy7bd38d72008-01-21 00:19:31 -0800399 struct net_device_stats *stats = &dev->stats;
Patrick McHardy9bb85822008-07-08 03:24:44 -0700400 u16 vlan_tci;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 /* Construct the second two bytes. This field looks something
403 * like:
404 * usr_priority: 3 bits (high bits)
405 * CFI 1 bit
406 * VLAN ID 12 bits (low bits)
407 */
Patrick McHardy9bb85822008-07-08 03:24:44 -0700408 vlan_tci = vlan_dev_info(dev)->vlan_id;
409 vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
410 skb = __vlan_hwaccel_put_tag(skb, vlan_tci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 stats->tx_packets++;
413 stats->tx_bytes += skb->len;
414
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800415 skb->dev = vlan_dev_info(dev)->real_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 dev_queue_xmit(skb);
417
418 return 0;
419}
420
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800421static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 /* TODO: gotta make sure the underlying layer can handle it,
424 * maybe an IFF_VLAN_CAPABLE flag for devices?
425 */
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800426 if (vlan_dev_info(dev)->real_dev->mtu < new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return -ERANGE;
428
429 dev->mtu = new_mtu;
430
431 return 0;
432}
433
Patrick McHardyc17d8872007-06-13 12:05:22 -0700434void vlan_dev_set_ingress_priority(const struct net_device *dev,
Patrick McHardy9bb85822008-07-08 03:24:44 -0700435 u32 skb_prio, u16 vlan_prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800437 struct vlan_dev_info *vlan = vlan_dev_info(dev);
Patrick McHardyb020cb42007-06-13 12:07:22 -0700438
439 if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio)
440 vlan->nr_ingress_mappings--;
441 else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio)
442 vlan->nr_ingress_mappings++;
443
444 vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
Patrick McHardyc17d8872007-06-13 12:05:22 -0700447int vlan_dev_set_egress_priority(const struct net_device *dev,
Patrick McHardy9bb85822008-07-08 03:24:44 -0700448 u32 skb_prio, u16 vlan_prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800450 struct vlan_dev_info *vlan = vlan_dev_info(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 struct vlan_priority_tci_mapping *mp = NULL;
452 struct vlan_priority_tci_mapping *np;
Patrick McHardyb020cb42007-06-13 12:07:22 -0700453 u32 vlan_qos = (vlan_prio << 13) & 0xE000;
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900454
Patrick McHardyc17d8872007-06-13 12:05:22 -0700455 /* See if a priority mapping exists.. */
Patrick McHardyb020cb42007-06-13 12:07:22 -0700456 mp = vlan->egress_priority_map[skb_prio & 0xF];
Patrick McHardyc17d8872007-06-13 12:05:22 -0700457 while (mp) {
458 if (mp->priority == skb_prio) {
Patrick McHardyb020cb42007-06-13 12:07:22 -0700459 if (mp->vlan_qos && !vlan_qos)
460 vlan->nr_egress_mappings--;
461 else if (!mp->vlan_qos && vlan_qos)
462 vlan->nr_egress_mappings++;
463 mp->vlan_qos = vlan_qos;
Patrick McHardyc17d8872007-06-13 12:05:22 -0700464 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
Patrick McHardyc17d8872007-06-13 12:05:22 -0700466 mp = mp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
Patrick McHardyc17d8872007-06-13 12:05:22 -0700468
469 /* Create a new mapping then. */
Patrick McHardyb020cb42007-06-13 12:07:22 -0700470 mp = vlan->egress_priority_map[skb_prio & 0xF];
Patrick McHardyc17d8872007-06-13 12:05:22 -0700471 np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL);
472 if (!np)
473 return -ENOBUFS;
474
475 np->next = mp;
476 np->priority = skb_prio;
Patrick McHardyb020cb42007-06-13 12:07:22 -0700477 np->vlan_qos = vlan_qos;
478 vlan->egress_priority_map[skb_prio & 0xF] = np;
479 if (vlan_qos)
480 vlan->nr_egress_mappings++;
Patrick McHardyc17d8872007-06-13 12:05:22 -0700481 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Patrick McHardya4bf3af42007-06-13 12:07:37 -0700484/* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */
Patrick McHardyb3ce0322008-07-05 21:26:27 -0700485int vlan_dev_change_flags(const struct net_device *dev, u32 flags, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Patrick McHardyb3ce0322008-07-05 21:26:27 -0700487 struct vlan_dev_info *vlan = vlan_dev_info(dev);
488 u32 old_flags = vlan->flags;
489
Patrick McHardy70c03b42008-07-05 21:26:57 -0700490 if (mask & ~(VLAN_FLAG_REORDER_HDR | VLAN_FLAG_GVRP))
Patrick McHardyb3ce0322008-07-05 21:26:27 -0700491 return -EINVAL;
492
493 vlan->flags = (old_flags & ~mask) | (flags & mask);
Patrick McHardy70c03b42008-07-05 21:26:57 -0700494
495 if (netif_running(dev) && (vlan->flags ^ old_flags) & VLAN_FLAG_GVRP) {
496 if (vlan->flags & VLAN_FLAG_GVRP)
497 vlan_gvrp_request_join(dev);
498 else
499 vlan_gvrp_request_leave(dev);
500 }
Patrick McHardyb3ce0322008-07-05 21:26:27 -0700501 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Patrick McHardyc17d8872007-06-13 12:05:22 -0700504void vlan_dev_get_realdev_name(const struct net_device *dev, char *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800506 strncpy(result, vlan_dev_info(dev)->real_dev->name, 23);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800509static int vlan_dev_open(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800511 struct vlan_dev_info *vlan = vlan_dev_info(dev);
Patrick McHardy8c979c22007-07-11 19:45:24 -0700512 struct net_device *real_dev = vlan->real_dev;
513 int err;
514
515 if (!(real_dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return -ENETDOWN;
517
Patrick McHardy8c979c22007-07-11 19:45:24 -0700518 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr)) {
519 err = dev_unicast_add(real_dev, dev->dev_addr, ETH_ALEN);
520 if (err < 0)
Wang Chen89146502008-07-14 20:59:03 -0700521 goto out;
Patrick McHardy8c979c22007-07-11 19:45:24 -0700522 }
Patrick McHardy8c979c22007-07-11 19:45:24 -0700523
Wang Chen89146502008-07-14 20:59:03 -0700524 if (dev->flags & IFF_ALLMULTI) {
525 err = dev_set_allmulti(real_dev, 1);
526 if (err < 0)
527 goto del_unicast;
528 }
529 if (dev->flags & IFF_PROMISC) {
530 err = dev_set_promiscuity(real_dev, 1);
531 if (err < 0)
532 goto clear_allmulti;
533 }
534
535 memcpy(vlan->real_dev_addr, real_dev->dev_addr, ETH_ALEN);
Patrick McHardy6c78dcb2007-07-14 18:52:56 -0700536
Patrick McHardy70c03b42008-07-05 21:26:57 -0700537 if (vlan->flags & VLAN_FLAG_GVRP)
538 vlan_gvrp_request_join(dev);
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return 0;
Wang Chen89146502008-07-14 20:59:03 -0700541
542clear_allmulti:
543 if (dev->flags & IFF_ALLMULTI)
544 dev_set_allmulti(real_dev, -1);
545del_unicast:
546 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
547 dev_unicast_delete(real_dev, dev->dev_addr, ETH_ALEN);
548out:
549 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800552static int vlan_dev_stop(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Patrick McHardy70c03b42008-07-05 21:26:57 -0700554 struct vlan_dev_info *vlan = vlan_dev_info(dev);
555 struct net_device *real_dev = vlan->real_dev;
556
557 if (vlan->flags & VLAN_FLAG_GVRP)
558 vlan_gvrp_request_leave(dev);
Patrick McHardy8c979c22007-07-11 19:45:24 -0700559
Patrick McHardy56addd62007-07-14 18:53:28 -0700560 dev_mc_unsync(real_dev, dev);
Chris Leeche83a2ea2008-01-31 16:53:23 -0800561 dev_unicast_unsync(real_dev, dev);
Patrick McHardy6c78dcb2007-07-14 18:52:56 -0700562 if (dev->flags & IFF_ALLMULTI)
563 dev_set_allmulti(real_dev, -1);
564 if (dev->flags & IFF_PROMISC)
565 dev_set_promiscuity(real_dev, -1);
566
Patrick McHardy8c979c22007-07-11 19:45:24 -0700567 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
568 dev_unicast_delete(real_dev, dev->dev_addr, dev->addr_len);
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return 0;
571}
572
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800573static int vlan_dev_set_mac_address(struct net_device *dev, void *p)
Patrick McHardy39aaac12007-11-10 21:52:35 -0800574{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800575 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
Patrick McHardy39aaac12007-11-10 21:52:35 -0800576 struct sockaddr *addr = p;
577 int err;
578
579 if (!is_valid_ether_addr(addr->sa_data))
580 return -EADDRNOTAVAIL;
581
582 if (!(dev->flags & IFF_UP))
583 goto out;
584
585 if (compare_ether_addr(addr->sa_data, real_dev->dev_addr)) {
586 err = dev_unicast_add(real_dev, addr->sa_data, ETH_ALEN);
587 if (err < 0)
588 return err;
589 }
590
591 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
592 dev_unicast_delete(real_dev, dev->dev_addr, ETH_ALEN);
593
594out:
595 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
596 return 0;
597}
598
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800599static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800601 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 struct ifreq ifrr;
603 int err = -EOPNOTSUPP;
604
605 strncpy(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
606 ifrr.ifr_ifru = ifr->ifr_ifru;
607
Patrick McHardy2029cc22008-01-21 00:26:41 -0800608 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 case SIOCGMIIPHY:
610 case SIOCGMIIREG:
611 case SIOCSMIIREG:
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900612 if (real_dev->do_ioctl && netif_device_present(real_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 err = real_dev->do_ioctl(real_dev, &ifrr, cmd);
614 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900617 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 ifr->ifr_ifru = ifrr.ifr_ifru;
619
620 return err;
621}
622
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800623static void vlan_dev_change_rx_flags(struct net_device *dev, int change)
Patrick McHardy6c78dcb2007-07-14 18:52:56 -0700624{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800625 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
Patrick McHardy6c78dcb2007-07-14 18:52:56 -0700626
627 if (change & IFF_ALLMULTI)
628 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
629 if (change & IFF_PROMISC)
630 dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1);
631}
632
Chris Leeche83a2ea2008-01-31 16:53:23 -0800633static void vlan_dev_set_rx_mode(struct net_device *vlan_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800635 dev_mc_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
Chris Leeche83a2ea2008-01-31 16:53:23 -0800636 dev_unicast_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800638
639/*
640 * vlan network devices have devices nesting below it, and are a special
641 * "super class" of normal network devices; split their locks off into a
642 * separate class since they always nest.
643 */
644static struct lock_class_key vlan_netdev_xmit_lock_key;
645
David S. Millerc773e842008-07-08 23:13:53 -0700646static void vlan_dev_set_lockdep_one(struct netdev_queue *txq,
647 int subclass)
648{
649 lockdep_set_class_and_subclass(&txq->_xmit_lock,
650 &vlan_netdev_xmit_lock_key, subclass);
651}
652
653static void vlan_dev_set_lockdep_class(struct net_device *dev, int subclass)
654{
655 vlan_dev_set_lockdep_one(&dev->tx_queue, subclass);
656}
657
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800658static const struct header_ops vlan_header_ops = {
659 .create = vlan_dev_hard_header,
660 .rebuild = vlan_dev_rebuild_header,
661 .parse = eth_header_parse,
662};
663
664static int vlan_dev_init(struct net_device *dev)
665{
Patrick McHardy9dfebcc2008-01-21 00:26:07 -0800666 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800667 int subclass = 0;
668
669 /* IFF_BROADCAST|IFF_MULTICAST; ??? */
Patrick McHardy0ed21b32008-03-26 00:15:17 -0700670 dev->flags = real_dev->flags & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI);
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800671 dev->iflink = real_dev->ifindex;
672 dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
673 (1<<__LINK_STATE_DORMANT))) |
674 (1<<__LINK_STATE_PRESENT);
675
Patrick McHardy289c79a2008-05-23 00:22:04 -0700676 dev->features |= real_dev->features & real_dev->vlan_features;
Patrick McHardy5fb13572008-05-20 14:54:50 -0700677
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800678 /* ipv6 shared card related stuff */
679 dev->dev_id = real_dev->dev_id;
680
681 if (is_zero_ether_addr(dev->dev_addr))
682 memcpy(dev->dev_addr, real_dev->dev_addr, dev->addr_len);
683 if (is_zero_ether_addr(dev->broadcast))
684 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
685
686 if (real_dev->features & NETIF_F_HW_VLAN_TX) {
687 dev->header_ops = real_dev->header_ops;
688 dev->hard_header_len = real_dev->hard_header_len;
689 dev->hard_start_xmit = vlan_dev_hwaccel_hard_start_xmit;
690 } else {
691 dev->header_ops = &vlan_header_ops;
692 dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
693 dev->hard_start_xmit = vlan_dev_hard_start_xmit;
694 }
695
Joonwoo Park26a25232008-07-08 03:22:16 -0700696 if (is_vlan_dev(real_dev))
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800697 subclass = 1;
698
David S. Millerc773e842008-07-08 23:13:53 -0700699 vlan_dev_set_lockdep_class(dev, subclass);
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800700 return 0;
701}
702
Pavel Emelyanov23556322008-04-04 12:45:12 -0700703static void vlan_dev_uninit(struct net_device *dev)
704{
705 struct vlan_priority_tci_mapping *pm;
706 struct vlan_dev_info *vlan = vlan_dev_info(dev);
707 int i;
708
709 for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
710 while ((pm = vlan->egress_priority_map[i]) != NULL) {
711 vlan->egress_priority_map[i] = pm->next;
712 kfree(pm);
713 }
714 }
715}
716
Patrick McHardy75b88462008-07-08 03:22:42 -0700717static u32 vlan_ethtool_get_rx_csum(struct net_device *dev)
718{
719 const struct vlan_dev_info *vlan = vlan_dev_info(dev);
720 struct net_device *real_dev = vlan->real_dev;
721
722 if (real_dev->ethtool_ops == NULL ||
723 real_dev->ethtool_ops->get_rx_csum == NULL)
724 return 0;
725 return real_dev->ethtool_ops->get_rx_csum(real_dev);
726}
727
728static const struct ethtool_ops vlan_ethtool_ops = {
729 .get_link = ethtool_op_get_link,
730 .get_rx_csum = vlan_ethtool_get_rx_csum,
731};
732
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800733void vlan_setup(struct net_device *dev)
734{
735 ether_setup(dev);
736
737 dev->priv_flags |= IFF_802_1Q_VLAN;
738 dev->tx_queue_len = 0;
739
740 dev->change_mtu = vlan_dev_change_mtu;
741 dev->init = vlan_dev_init;
Pavel Emelyanov23556322008-04-04 12:45:12 -0700742 dev->uninit = vlan_dev_uninit;
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800743 dev->open = vlan_dev_open;
744 dev->stop = vlan_dev_stop;
745 dev->set_mac_address = vlan_dev_set_mac_address;
Chris Leeche83a2ea2008-01-31 16:53:23 -0800746 dev->set_rx_mode = vlan_dev_set_rx_mode;
747 dev->set_multicast_list = vlan_dev_set_rx_mode;
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800748 dev->change_rx_flags = vlan_dev_change_rx_flags;
749 dev->do_ioctl = vlan_dev_ioctl;
750 dev->destructor = free_netdev;
Patrick McHardy75b88462008-07-08 03:22:42 -0700751 dev->ethtool_ops = &vlan_ethtool_ops;
Patrick McHardyef3eb3e2008-01-21 00:22:11 -0800752
753 memset(dev->broadcast, 0, ETH_ALEN);
754}