Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* -*- linux-c -*- |
| 2 | * INET 802.1Q VLAN |
| 3 | * Ethernet-type device handling. |
| 4 | * |
| 5 | * Authors: Ben Greear <greearb@candelatech.com> |
| 6 | * Please send support related email to: vlan@scry.wanfear.com |
| 7 | * VLAN Home Page: http://www.candelatech.com/~greear/vlan.html |
YOSHIFUJI Hideaki | 122952f | 2007-02-09 23:24:25 +0900 | [diff] [blame] | 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * Fixes: Mar 22 2001: Martin Bokaemper <mbokaemper@unispherenetworks.com> |
| 10 | * - reset skb->pkt_type on incoming packets when MAC was changed |
| 11 | * - see that changed MAC is saddr for outgoing packets |
| 12 | * Oct 20, 2001: Ard van Breeman: |
| 13 | * - Fix MC-list, finally. |
| 14 | * - Flush MC-list on VLAN destroy. |
YOSHIFUJI Hideaki | 122952f | 2007-02-09 23:24:25 +0900 | [diff] [blame] | 15 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | * |
| 17 | * This program is free software; you can redistribute it and/or |
| 18 | * modify it under the terms of the GNU General Public License |
| 19 | * as published by the Free Software Foundation; either version |
| 20 | * 2 of the License, or (at your option) any later version. |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/mm.h> |
| 25 | #include <linux/in.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <asm/uaccess.h> /* for copy_from_user */ |
| 28 | #include <linux/skbuff.h> |
| 29 | #include <linux/netdevice.h> |
| 30 | #include <linux/etherdevice.h> |
| 31 | #include <net/datalink.h> |
| 32 | #include <net/p8022.h> |
| 33 | #include <net/arp.h> |
| 34 | |
| 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 | */ |
| 50 | int vlan_dev_rebuild_header(struct sk_buff *skb) |
| 51 | { |
| 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 Hideaki | 122952f | 2007-02-09 23:24:25 +0900 | [diff] [blame] | 61 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | default: |
| 63 | printk(VLAN_DBG |
YOSHIFUJI Hideaki | 122952f | 2007-02-09 23:24:25 +0900 | [diff] [blame] | 64 | "%s: unable to resolve type %X addresses.\n", |
Alexey Dobriyan | d136fe7 | 2005-12-28 22:27:10 +0300 | [diff] [blame] | 65 | dev->name, ntohs(veth->h_vlan_encapsulated_proto)); |
YOSHIFUJI Hideaki | 122952f | 2007-02-09 23:24:25 +0900 | [diff] [blame] | 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | memcpy(veth->h_source, dev->dev_addr, ETH_ALEN); |
| 68 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 69 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static inline struct sk_buff *vlan_check_reorder_header(struct sk_buff *skb) |
| 75 | { |
Patrick McHardy | a4bf3af4 | 2007-06-13 12:07:37 -0700 | [diff] [blame] | 76 | if (VLAN_DEV_INFO(skb->dev)->flags & VLAN_FLAG_REORDER_HDR) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | 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 Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 86 | skb->mac_header += VLAN_HLEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | return skb; |
| 91 | } |
| 92 | |
| 93 | /* |
YOSHIFUJI Hideaki | 122952f | 2007-02-09 23:24:25 +0900 | [diff] [blame] | 94 | * Determine the packet's protocol ID. The rule here is that we |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | * assume 802.3 if the type field is short enough to be a length. |
| 96 | * This is normal practice and works for any 'now in use' protocol. |
| 97 | * |
| 98 | * Also, at this point we assume that we ARE dealing exclusively with |
| 99 | * VLAN packets, or packets that should be made into VLAN packets based |
| 100 | * on a default VLAN ID. |
| 101 | * |
| 102 | * NOTE: Should be similar to ethernet/eth.c. |
| 103 | * |
| 104 | * SANITY NOTE: This method is called when a packet is moving up the stack |
| 105 | * towards userland. To get here, it would have already passed |
| 106 | * through the ethernet/eth.c eth_type_trans() method. |
| 107 | * SANITY NOTE 2: We are referencing to the VLAN_HDR frields, which MAY be |
| 108 | * stored UNALIGNED in the memory. RISC systems don't like |
| 109 | * such cases very much... |
| 110 | * SANITY NOTE 2a: According to Dave Miller & Alexey, it will always be aligned, |
| 111 | * so there doesn't need to be any of the unaligned stuff. It has |
| 112 | * been commented out now... --Ben |
| 113 | * |
| 114 | */ |
| 115 | int vlan_skb_recv(struct sk_buff *skb, struct net_device *dev, |
YOSHIFUJI Hideaki | 122952f | 2007-02-09 23:24:25 +0900 | [diff] [blame] | 116 | struct packet_type* ptype, struct net_device *orig_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { |
| 118 | unsigned char *rawp = NULL; |
Evgeniy Polyakov | e7c243c | 2007-08-24 23:36:29 -0700 | [diff] [blame] | 119 | struct vlan_hdr *vhdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | unsigned short vid; |
| 121 | struct net_device_stats *stats; |
| 122 | unsigned short vlan_TCI; |
Alexey Dobriyan | 3c3f8f2 | 2005-09-19 15:41:28 -0700 | [diff] [blame] | 123 | __be16 proto; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
Evgeniy Polyakov | e7c243c | 2007-08-24 23:36:29 -0700 | [diff] [blame] | 125 | if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) |
| 126 | return -1; |
| 127 | |
| 128 | if (unlikely(!pskb_may_pull(skb, VLAN_HLEN))) { |
| 129 | kfree_skb(skb); |
| 130 | return -1; |
| 131 | } |
| 132 | |
| 133 | vhdr = (struct vlan_hdr *)(skb->data); |
| 134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | /* vlan_TCI = ntohs(get_unaligned(&vhdr->h_vlan_TCI)); */ |
| 136 | vlan_TCI = ntohs(vhdr->h_vlan_TCI); |
| 137 | |
| 138 | vid = (vlan_TCI & VLAN_VID_MASK); |
| 139 | |
| 140 | #ifdef VLAN_DEBUG |
| 141 | printk(VLAN_DBG "%s: skb: %p vlan_id: %hx\n", |
| 142 | __FUNCTION__, skb, vid); |
| 143 | #endif |
| 144 | |
| 145 | /* Ok, we will find the correct VLAN device, strip the header, |
| 146 | * and then go on as usual. |
| 147 | */ |
| 148 | |
| 149 | /* We have 12 bits of vlan ID. |
| 150 | * |
| 151 | * We must not drop allow preempt until we hold a |
| 152 | * reference to the device (netif_rx does that) or we |
| 153 | * fail. |
| 154 | */ |
| 155 | |
| 156 | rcu_read_lock(); |
| 157 | skb->dev = __find_vlan_dev(dev, vid); |
| 158 | if (!skb->dev) { |
| 159 | rcu_read_unlock(); |
| 160 | |
| 161 | #ifdef VLAN_DEBUG |
| 162 | printk(VLAN_DBG "%s: ERROR: No net_device for VID: %i on dev: %s [%i]\n", |
| 163 | __FUNCTION__, (unsigned int)(vid), dev->name, dev->ifindex); |
| 164 | #endif |
| 165 | kfree_skb(skb); |
| 166 | return -1; |
| 167 | } |
| 168 | |
| 169 | skb->dev->last_rx = jiffies; |
| 170 | |
| 171 | /* Bump the rx counters for the VLAN device. */ |
| 172 | stats = vlan_dev_get_stats(skb->dev); |
| 173 | stats->rx_packets++; |
| 174 | stats->rx_bytes += skb->len; |
| 175 | |
Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 176 | /* Take off the VLAN header (4 bytes currently) */ |
| 177 | skb_pull_rcsum(skb, VLAN_HLEN); |
Stephen Hemminger | a388442 | 2005-12-14 16:23:16 -0800 | [diff] [blame] | 178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | /* Ok, lets check to make sure the device (dev) we |
| 180 | * came in on is what this VLAN is attached to. |
| 181 | */ |
| 182 | |
| 183 | if (dev != VLAN_DEV_INFO(skb->dev)->real_dev) { |
| 184 | rcu_read_unlock(); |
| 185 | |
| 186 | #ifdef VLAN_DEBUG |
| 187 | printk(VLAN_DBG "%s: dropping skb: %p because came in on wrong device, dev: %s real_dev: %s, skb_dev: %s\n", |
YOSHIFUJI Hideaki | 122952f | 2007-02-09 23:24:25 +0900 | [diff] [blame] | 188 | __FUNCTION__, skb, dev->name, |
| 189 | VLAN_DEV_INFO(skb->dev)->real_dev->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | skb->dev->name); |
| 191 | #endif |
| 192 | kfree_skb(skb); |
| 193 | stats->rx_errors++; |
| 194 | return -1; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Deal with ingress priority mapping. |
| 199 | */ |
| 200 | skb->priority = vlan_get_ingress_priority(skb->dev, ntohs(vhdr->h_vlan_TCI)); |
| 201 | |
| 202 | #ifdef VLAN_DEBUG |
| 203 | printk(VLAN_DBG "%s: priority: %lu for TCI: %hu (hbo)\n", |
YOSHIFUJI Hideaki | 122952f | 2007-02-09 23:24:25 +0900 | [diff] [blame] | 204 | __FUNCTION__, (unsigned long)(skb->priority), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | ntohs(vhdr->h_vlan_TCI)); |
| 206 | #endif |
| 207 | |
| 208 | /* The ethernet driver already did the pkt_type calculations |
| 209 | * for us... |
| 210 | */ |
| 211 | switch (skb->pkt_type) { |
| 212 | case PACKET_BROADCAST: /* Yeah, stats collect these together.. */ |
| 213 | // stats->broadcast ++; // no such counter :-( |
| 214 | break; |
| 215 | |
| 216 | case PACKET_MULTICAST: |
| 217 | stats->multicast++; |
| 218 | break; |
| 219 | |
YOSHIFUJI Hideaki | 122952f | 2007-02-09 23:24:25 +0900 | [diff] [blame] | 220 | case PACKET_OTHERHOST: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | /* Our lower layer thinks this is not local, let's make sure. |
| 222 | * This allows the VLAN to have a different MAC than the underlying |
| 223 | * device, and still route correctly. |
| 224 | */ |
Kris Katterjohn | d3f4a68 | 2006-01-09 16:01:43 -0800 | [diff] [blame] | 225 | if (!compare_ether_addr(eth_hdr(skb)->h_dest, skb->dev->dev_addr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | /* It is for our (changed) MAC-address! */ |
| 227 | skb->pkt_type = PACKET_HOST; |
| 228 | } |
| 229 | break; |
| 230 | default: |
| 231 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 232 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
| 234 | /* Was a VLAN packet, grab the encapsulated protocol, which the layer |
| 235 | * three protocols care about. |
| 236 | */ |
| 237 | /* proto = get_unaligned(&vhdr->h_vlan_encapsulated_proto); */ |
| 238 | proto = vhdr->h_vlan_encapsulated_proto; |
| 239 | |
| 240 | skb->protocol = proto; |
| 241 | if (ntohs(proto) >= 1536) { |
| 242 | /* place it back on the queue to be handled by |
| 243 | * true layer 3 protocols. |
| 244 | */ |
| 245 | |
| 246 | /* See if we are configured to re-write the VLAN header |
| 247 | * to make it look like ethernet... |
| 248 | */ |
| 249 | skb = vlan_check_reorder_header(skb); |
| 250 | |
| 251 | /* Can be null if skb-clone fails when re-ordering */ |
| 252 | if (skb) { |
| 253 | netif_rx(skb); |
| 254 | } else { |
| 255 | /* TODO: Add a more specific counter here. */ |
| 256 | stats->rx_errors++; |
| 257 | } |
| 258 | rcu_read_unlock(); |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | rawp = skb->data; |
| 263 | |
| 264 | /* |
| 265 | * This is a magic hack to spot IPX packets. Older Novell breaks |
| 266 | * the protocol design and runs IPX over 802.3 without an 802.2 LLC |
| 267 | * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This |
| 268 | * won't work for fault tolerant netware but does for the rest. |
| 269 | */ |
| 270 | if (*(unsigned short *)rawp == 0xFFFF) { |
YOSHIFUJI Hideaki | b93b7ee | 2007-03-25 20:12:18 -0700 | [diff] [blame] | 271 | skb->protocol = htons(ETH_P_802_3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | /* place it back on the queue to be handled by true layer 3 protocols. |
| 273 | */ |
| 274 | |
| 275 | /* See if we are configured to re-write the VLAN header |
| 276 | * to make it look like ethernet... |
| 277 | */ |
| 278 | skb = vlan_check_reorder_header(skb); |
| 279 | |
| 280 | /* Can be null if skb-clone fails when re-ordering */ |
| 281 | if (skb) { |
| 282 | netif_rx(skb); |
| 283 | } else { |
| 284 | /* TODO: Add a more specific counter here. */ |
| 285 | stats->rx_errors++; |
| 286 | } |
| 287 | rcu_read_unlock(); |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * Real 802.2 LLC |
| 293 | */ |
YOSHIFUJI Hideaki | b93b7ee | 2007-03-25 20:12:18 -0700 | [diff] [blame] | 294 | skb->protocol = htons(ETH_P_802_2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | /* place it back on the queue to be handled by upper layer protocols. |
| 296 | */ |
| 297 | |
| 298 | /* See if we are configured to re-write the VLAN header |
| 299 | * to make it look like ethernet... |
| 300 | */ |
| 301 | skb = vlan_check_reorder_header(skb); |
| 302 | |
| 303 | /* Can be null if skb-clone fails when re-ordering */ |
| 304 | if (skb) { |
| 305 | netif_rx(skb); |
| 306 | } else { |
| 307 | /* TODO: Add a more specific counter here. */ |
| 308 | stats->rx_errors++; |
| 309 | } |
| 310 | rcu_read_unlock(); |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | static inline unsigned short vlan_dev_get_egress_qos_mask(struct net_device* dev, |
| 315 | struct sk_buff* skb) |
| 316 | { |
| 317 | struct vlan_priority_tci_mapping *mp = |
| 318 | VLAN_DEV_INFO(dev)->egress_priority_map[(skb->priority & 0xF)]; |
| 319 | |
| 320 | while (mp) { |
| 321 | if (mp->priority == skb->priority) { |
| 322 | return mp->vlan_qos; /* This should already be shifted to mask |
| 323 | * correctly with the VLAN's TCI |
| 324 | */ |
| 325 | } |
| 326 | mp = mp->next; |
| 327 | } |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | /* |
YOSHIFUJI Hideaki | 122952f | 2007-02-09 23:24:25 +0900 | [diff] [blame] | 332 | * Create the VLAN header for an arbitrary protocol layer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | * |
| 334 | * saddr=NULL means use device source address |
| 335 | * daddr=NULL means leave destination address (eg unresolved arp) |
| 336 | * |
| 337 | * This is called when the SKB is moving down the stack towards the |
| 338 | * physical devices. |
| 339 | */ |
| 340 | int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev, |
YOSHIFUJI Hideaki | 122952f | 2007-02-09 23:24:25 +0900 | [diff] [blame] | 341 | unsigned short type, void *daddr, void *saddr, |
| 342 | unsigned len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | { |
| 344 | struct vlan_hdr *vhdr; |
| 345 | unsigned short veth_TCI = 0; |
| 346 | int rc = 0; |
| 347 | int build_vlan_header = 0; |
| 348 | struct net_device *vdev = dev; /* save this for the bottom of the method */ |
| 349 | |
| 350 | #ifdef VLAN_DEBUG |
| 351 | printk(VLAN_DBG "%s: skb: %p type: %hx len: %x vlan_id: %hx, daddr: %p\n", |
| 352 | __FUNCTION__, skb, type, len, VLAN_DEV_INFO(dev)->vlan_id, daddr); |
| 353 | #endif |
| 354 | |
| 355 | /* build vlan header only if re_order_header flag is NOT set. This |
| 356 | * fixes some programs that get confused when they see a VLAN device |
| 357 | * sending a frame that is VLAN encoded (the consensus is that the VLAN |
| 358 | * device should look completely like an Ethernet device when the |
YOSHIFUJI Hideaki | 122952f | 2007-02-09 23:24:25 +0900 | [diff] [blame] | 359 | * REORDER_HEADER flag is set) The drawback to this is some extra |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | * header shuffling in the hard_start_xmit. Users can turn off this |
| 361 | * REORDER behaviour with the vconfig tool. |
| 362 | */ |
Patrick McHardy | a4bf3af4 | 2007-06-13 12:07:37 -0700 | [diff] [blame] | 363 | if (!(VLAN_DEV_INFO(dev)->flags & VLAN_FLAG_REORDER_HDR)) |
| 364 | build_vlan_header = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
| 366 | if (build_vlan_header) { |
| 367 | vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN); |
| 368 | |
| 369 | /* build the four bytes that make this a VLAN header. */ |
| 370 | |
| 371 | /* Now, construct the second two bytes. This field looks something |
| 372 | * like: |
| 373 | * usr_priority: 3 bits (high bits) |
| 374 | * CFI 1 bit |
| 375 | * VLAN ID 12 bits (low bits) |
| 376 | * |
| 377 | */ |
| 378 | veth_TCI = VLAN_DEV_INFO(dev)->vlan_id; |
| 379 | veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb); |
| 380 | |
| 381 | vhdr->h_vlan_TCI = htons(veth_TCI); |
| 382 | |
| 383 | /* |
| 384 | * Set the protocol type. |
| 385 | * For a packet of type ETH_P_802_3 we put the length in here instead. |
| 386 | * It is up to the 802.2 layer to carry protocol information. |
| 387 | */ |
| 388 | |
| 389 | if (type != ETH_P_802_3) { |
| 390 | vhdr->h_vlan_encapsulated_proto = htons(type); |
| 391 | } else { |
| 392 | vhdr->h_vlan_encapsulated_proto = htons(len); |
| 393 | } |
Jerome Borsboom | 279e172 | 2007-04-13 16:12:47 -0700 | [diff] [blame] | 394 | |
| 395 | skb->protocol = htons(ETH_P_8021Q); |
David S. Miller | be8bd86 | 2007-04-19 20:34:51 -0700 | [diff] [blame] | 396 | skb_reset_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /* Before delegating work to the lower layer, enter our MAC-address */ |
| 400 | if (saddr == NULL) |
| 401 | saddr = dev->dev_addr; |
| 402 | |
| 403 | dev = VLAN_DEV_INFO(dev)->real_dev; |
| 404 | |
| 405 | /* MPLS can send us skbuffs w/out enough space. This check will grow the |
| 406 | * skb if it doesn't have enough headroom. Not a beautiful solution, so |
| 407 | * I'll tick a counter so that users can know it's happening... If they |
| 408 | * care... |
| 409 | */ |
| 410 | |
| 411 | /* NOTE: This may still break if the underlying device is not the final |
| 412 | * device (and thus there are more headers to add...) It should work for |
| 413 | * good-ole-ethernet though. |
| 414 | */ |
| 415 | if (skb_headroom(skb) < dev->hard_header_len) { |
| 416 | struct sk_buff *sk_tmp = skb; |
| 417 | skb = skb_realloc_headroom(sk_tmp, dev->hard_header_len); |
| 418 | kfree_skb(sk_tmp); |
| 419 | if (skb == NULL) { |
| 420 | struct net_device_stats *stats = vlan_dev_get_stats(vdev); |
| 421 | stats->tx_dropped++; |
| 422 | return -ENOMEM; |
| 423 | } |
| 424 | VLAN_DEV_INFO(vdev)->cnt_inc_headroom_on_tx++; |
| 425 | #ifdef VLAN_DEBUG |
| 426 | printk(VLAN_DBG "%s: %s: had to grow skb.\n", __FUNCTION__, vdev->name); |
| 427 | #endif |
| 428 | } |
| 429 | |
| 430 | if (build_vlan_header) { |
| 431 | /* Now make the underlying real hard header */ |
| 432 | rc = dev->hard_header(skb, dev, ETH_P_8021Q, daddr, saddr, len + VLAN_HLEN); |
| 433 | |
| 434 | if (rc > 0) { |
| 435 | rc += VLAN_HLEN; |
| 436 | } else if (rc < 0) { |
| 437 | rc -= VLAN_HLEN; |
| 438 | } |
| 439 | } else { |
| 440 | /* If here, then we'll just make a normal looking ethernet frame, |
| 441 | * but, the hard_start_xmit method will insert the tag (it has to |
| 442 | * be able to do this for bridged and other skbs that don't come |
| 443 | * down the protocol stack in an orderly manner. |
| 444 | */ |
| 445 | rc = dev->hard_header(skb, dev, type, daddr, saddr, len); |
| 446 | } |
| 447 | |
| 448 | return rc; |
| 449 | } |
| 450 | |
| 451 | int vlan_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 452 | { |
| 453 | struct net_device_stats *stats = vlan_dev_get_stats(dev); |
| 454 | struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data); |
| 455 | |
| 456 | /* Handle non-VLAN frames if they are sent to us, for example by DHCP. |
| 457 | * |
| 458 | * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING |
| 459 | * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs... |
| 460 | */ |
| 461 | |
YOSHIFUJI Hideaki | b93b7ee | 2007-03-25 20:12:18 -0700 | [diff] [blame] | 462 | if (veth->h_vlan_proto != htons(ETH_P_8021Q)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | int orig_headroom = skb_headroom(skb); |
| 464 | unsigned short veth_TCI; |
| 465 | |
| 466 | /* This is not a VLAN frame...but we can fix that! */ |
| 467 | VLAN_DEV_INFO(dev)->cnt_encap_on_xmit++; |
| 468 | |
| 469 | #ifdef VLAN_DEBUG |
| 470 | printk(VLAN_DBG "%s: proto to encap: 0x%hx (hbo)\n", |
| 471 | __FUNCTION__, htons(veth->h_vlan_proto)); |
| 472 | #endif |
| 473 | /* Construct the second two bytes. This field looks something |
| 474 | * like: |
| 475 | * usr_priority: 3 bits (high bits) |
| 476 | * CFI 1 bit |
| 477 | * VLAN ID 12 bits (low bits) |
| 478 | */ |
| 479 | veth_TCI = VLAN_DEV_INFO(dev)->vlan_id; |
| 480 | veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb); |
| 481 | |
| 482 | skb = __vlan_put_tag(skb, veth_TCI); |
| 483 | if (!skb) { |
| 484 | stats->tx_dropped++; |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | if (orig_headroom < VLAN_HLEN) { |
| 489 | VLAN_DEV_INFO(dev)->cnt_inc_headroom_on_tx++; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | #ifdef VLAN_DEBUG |
| 494 | printk(VLAN_DBG "%s: about to send skb: %p to dev: %s\n", |
| 495 | __FUNCTION__, skb, skb->dev->name); |
| 496 | printk(VLAN_DBG " %2hx.%2hx.%2hx.%2xh.%2hx.%2hx %2hx.%2hx.%2hx.%2hx.%2hx.%2hx %4hx %4hx %4hx\n", |
| 497 | veth->h_dest[0], veth->h_dest[1], veth->h_dest[2], veth->h_dest[3], veth->h_dest[4], veth->h_dest[5], |
| 498 | veth->h_source[0], veth->h_source[1], veth->h_source[2], veth->h_source[3], veth->h_source[4], veth->h_source[5], |
| 499 | veth->h_vlan_proto, veth->h_vlan_TCI, veth->h_vlan_encapsulated_proto); |
| 500 | #endif |
| 501 | |
| 502 | stats->tx_packets++; /* for statics only */ |
| 503 | stats->tx_bytes += skb->len; |
| 504 | |
| 505 | skb->dev = VLAN_DEV_INFO(dev)->real_dev; |
| 506 | dev_queue_xmit(skb); |
| 507 | |
| 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | int vlan_dev_hwaccel_hard_start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 512 | { |
| 513 | struct net_device_stats *stats = vlan_dev_get_stats(dev); |
| 514 | unsigned short veth_TCI; |
| 515 | |
| 516 | /* Construct the second two bytes. This field looks something |
| 517 | * like: |
| 518 | * usr_priority: 3 bits (high bits) |
| 519 | * CFI 1 bit |
| 520 | * VLAN ID 12 bits (low bits) |
| 521 | */ |
| 522 | veth_TCI = VLAN_DEV_INFO(dev)->vlan_id; |
| 523 | veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb); |
| 524 | skb = __vlan_hwaccel_put_tag(skb, veth_TCI); |
| 525 | |
| 526 | stats->tx_packets++; |
| 527 | stats->tx_bytes += skb->len; |
| 528 | |
| 529 | skb->dev = VLAN_DEV_INFO(dev)->real_dev; |
| 530 | dev_queue_xmit(skb); |
| 531 | |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | int vlan_dev_change_mtu(struct net_device *dev, int new_mtu) |
| 536 | { |
| 537 | /* TODO: gotta make sure the underlying layer can handle it, |
| 538 | * maybe an IFF_VLAN_CAPABLE flag for devices? |
| 539 | */ |
| 540 | if (VLAN_DEV_INFO(dev)->real_dev->mtu < new_mtu) |
| 541 | return -ERANGE; |
| 542 | |
| 543 | dev->mtu = new_mtu; |
| 544 | |
| 545 | return 0; |
| 546 | } |
| 547 | |
Patrick McHardy | c17d887 | 2007-06-13 12:05:22 -0700 | [diff] [blame] | 548 | void vlan_dev_set_ingress_priority(const struct net_device *dev, |
| 549 | u32 skb_prio, short vlan_prio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | { |
Patrick McHardy | b020cb4 | 2007-06-13 12:07:22 -0700 | [diff] [blame] | 551 | struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev); |
| 552 | |
| 553 | if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio) |
| 554 | vlan->nr_ingress_mappings--; |
| 555 | else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio) |
| 556 | vlan->nr_ingress_mappings++; |
| 557 | |
| 558 | vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Patrick McHardy | c17d887 | 2007-06-13 12:05:22 -0700 | [diff] [blame] | 561 | int vlan_dev_set_egress_priority(const struct net_device *dev, |
| 562 | u32 skb_prio, short vlan_prio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | { |
Patrick McHardy | b020cb4 | 2007-06-13 12:07:22 -0700 | [diff] [blame] | 564 | struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | struct vlan_priority_tci_mapping *mp = NULL; |
| 566 | struct vlan_priority_tci_mapping *np; |
Patrick McHardy | b020cb4 | 2007-06-13 12:07:22 -0700 | [diff] [blame] | 567 | u32 vlan_qos = (vlan_prio << 13) & 0xE000; |
YOSHIFUJI Hideaki | 122952f | 2007-02-09 23:24:25 +0900 | [diff] [blame] | 568 | |
Patrick McHardy | c17d887 | 2007-06-13 12:05:22 -0700 | [diff] [blame] | 569 | /* See if a priority mapping exists.. */ |
Patrick McHardy | b020cb4 | 2007-06-13 12:07:22 -0700 | [diff] [blame] | 570 | mp = vlan->egress_priority_map[skb_prio & 0xF]; |
Patrick McHardy | c17d887 | 2007-06-13 12:05:22 -0700 | [diff] [blame] | 571 | while (mp) { |
| 572 | if (mp->priority == skb_prio) { |
Patrick McHardy | b020cb4 | 2007-06-13 12:07:22 -0700 | [diff] [blame] | 573 | if (mp->vlan_qos && !vlan_qos) |
| 574 | vlan->nr_egress_mappings--; |
| 575 | else if (!mp->vlan_qos && vlan_qos) |
| 576 | vlan->nr_egress_mappings++; |
| 577 | mp->vlan_qos = vlan_qos; |
Patrick McHardy | c17d887 | 2007-06-13 12:05:22 -0700 | [diff] [blame] | 578 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | } |
Patrick McHardy | c17d887 | 2007-06-13 12:05:22 -0700 | [diff] [blame] | 580 | mp = mp->next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | } |
Patrick McHardy | c17d887 | 2007-06-13 12:05:22 -0700 | [diff] [blame] | 582 | |
| 583 | /* Create a new mapping then. */ |
Patrick McHardy | b020cb4 | 2007-06-13 12:07:22 -0700 | [diff] [blame] | 584 | mp = vlan->egress_priority_map[skb_prio & 0xF]; |
Patrick McHardy | c17d887 | 2007-06-13 12:05:22 -0700 | [diff] [blame] | 585 | np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL); |
| 586 | if (!np) |
| 587 | return -ENOBUFS; |
| 588 | |
| 589 | np->next = mp; |
| 590 | np->priority = skb_prio; |
Patrick McHardy | b020cb4 | 2007-06-13 12:07:22 -0700 | [diff] [blame] | 591 | np->vlan_qos = vlan_qos; |
| 592 | vlan->egress_priority_map[skb_prio & 0xF] = np; |
| 593 | if (vlan_qos) |
| 594 | vlan->nr_egress_mappings++; |
Patrick McHardy | c17d887 | 2007-06-13 12:05:22 -0700 | [diff] [blame] | 595 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Patrick McHardy | a4bf3af4 | 2007-06-13 12:07:37 -0700 | [diff] [blame] | 598 | /* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */ |
Patrick McHardy | c17d887 | 2007-06-13 12:05:22 -0700 | [diff] [blame] | 599 | int vlan_dev_set_vlan_flag(const struct net_device *dev, |
| 600 | u32 flag, short flag_val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | { |
Patrick McHardy | c17d887 | 2007-06-13 12:05:22 -0700 | [diff] [blame] | 602 | /* verify flag is supported */ |
Patrick McHardy | a4bf3af4 | 2007-06-13 12:07:37 -0700 | [diff] [blame] | 603 | if (flag == VLAN_FLAG_REORDER_HDR) { |
Patrick McHardy | c17d887 | 2007-06-13 12:05:22 -0700 | [diff] [blame] | 604 | if (flag_val) { |
Patrick McHardy | a4bf3af4 | 2007-06-13 12:07:37 -0700 | [diff] [blame] | 605 | VLAN_DEV_INFO(dev)->flags |= VLAN_FLAG_REORDER_HDR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | } else { |
Patrick McHardy | a4bf3af4 | 2007-06-13 12:07:37 -0700 | [diff] [blame] | 607 | VLAN_DEV_INFO(dev)->flags &= ~VLAN_FLAG_REORDER_HDR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | } |
Patrick McHardy | c17d887 | 2007-06-13 12:05:22 -0700 | [diff] [blame] | 609 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | } |
Patrick McHardy | c17d887 | 2007-06-13 12:05:22 -0700 | [diff] [blame] | 611 | printk(KERN_ERR "%s: flag %i is not valid.\n", __FUNCTION__, flag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | return -EINVAL; |
| 613 | } |
| 614 | |
Patrick McHardy | c17d887 | 2007-06-13 12:05:22 -0700 | [diff] [blame] | 615 | void vlan_dev_get_realdev_name(const struct net_device *dev, char *result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | { |
Patrick McHardy | c17d887 | 2007-06-13 12:05:22 -0700 | [diff] [blame] | 617 | strncpy(result, VLAN_DEV_INFO(dev)->real_dev->name, 23); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Patrick McHardy | c17d887 | 2007-06-13 12:05:22 -0700 | [diff] [blame] | 620 | void vlan_dev_get_vid(const struct net_device *dev, unsigned short *result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | { |
Patrick McHardy | c17d887 | 2007-06-13 12:05:22 -0700 | [diff] [blame] | 622 | *result = VLAN_DEV_INFO(dev)->vlan_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | } |
| 624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | int vlan_dev_open(struct net_device *dev) |
| 626 | { |
Patrick McHardy | 8c979c2 | 2007-07-11 19:45:24 -0700 | [diff] [blame] | 627 | struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev); |
| 628 | struct net_device *real_dev = vlan->real_dev; |
| 629 | int err; |
| 630 | |
| 631 | if (!(real_dev->flags & IFF_UP)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | return -ENETDOWN; |
| 633 | |
Patrick McHardy | 8c979c2 | 2007-07-11 19:45:24 -0700 | [diff] [blame] | 634 | if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr)) { |
| 635 | err = dev_unicast_add(real_dev, dev->dev_addr, ETH_ALEN); |
| 636 | if (err < 0) |
| 637 | return err; |
| 638 | } |
| 639 | memcpy(vlan->real_dev_addr, real_dev->dev_addr, ETH_ALEN); |
| 640 | |
Patrick McHardy | 6c78dcb | 2007-07-14 18:52:56 -0700 | [diff] [blame] | 641 | if (dev->flags & IFF_ALLMULTI) |
| 642 | dev_set_allmulti(real_dev, 1); |
| 643 | if (dev->flags & IFF_PROMISC) |
| 644 | dev_set_promiscuity(real_dev, 1); |
| 645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | int vlan_dev_stop(struct net_device *dev) |
| 650 | { |
Patrick McHardy | 8c979c2 | 2007-07-11 19:45:24 -0700 | [diff] [blame] | 651 | struct net_device *real_dev = VLAN_DEV_INFO(dev)->real_dev; |
| 652 | |
Patrick McHardy | 56addd6 | 2007-07-14 18:53:28 -0700 | [diff] [blame] | 653 | dev_mc_unsync(real_dev, dev); |
Patrick McHardy | 6c78dcb | 2007-07-14 18:52:56 -0700 | [diff] [blame] | 654 | if (dev->flags & IFF_ALLMULTI) |
| 655 | dev_set_allmulti(real_dev, -1); |
| 656 | if (dev->flags & IFF_PROMISC) |
| 657 | dev_set_promiscuity(real_dev, -1); |
| 658 | |
Patrick McHardy | 8c979c2 | 2007-07-11 19:45:24 -0700 | [diff] [blame] | 659 | if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr)) |
| 660 | dev_unicast_delete(real_dev, dev->dev_addr, dev->addr_len); |
| 661 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
| 666 | { |
| 667 | struct net_device *real_dev = VLAN_DEV_INFO(dev)->real_dev; |
| 668 | struct ifreq ifrr; |
| 669 | int err = -EOPNOTSUPP; |
| 670 | |
| 671 | strncpy(ifrr.ifr_name, real_dev->name, IFNAMSIZ); |
| 672 | ifrr.ifr_ifru = ifr->ifr_ifru; |
| 673 | |
| 674 | switch(cmd) { |
| 675 | case SIOCGMIIPHY: |
| 676 | case SIOCGMIIREG: |
| 677 | case SIOCSMIIREG: |
YOSHIFUJI Hideaki | 122952f | 2007-02-09 23:24:25 +0900 | [diff] [blame] | 678 | if (real_dev->do_ioctl && netif_device_present(real_dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | err = real_dev->do_ioctl(real_dev, &ifrr, cmd); |
| 680 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | } |
| 682 | |
YOSHIFUJI Hideaki | 122952f | 2007-02-09 23:24:25 +0900 | [diff] [blame] | 683 | if (!err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | ifr->ifr_ifru = ifrr.ifr_ifru; |
| 685 | |
| 686 | return err; |
| 687 | } |
| 688 | |
Patrick McHardy | 6c78dcb | 2007-07-14 18:52:56 -0700 | [diff] [blame] | 689 | void vlan_change_rx_flags(struct net_device *dev, int change) |
| 690 | { |
| 691 | struct net_device *real_dev = VLAN_DEV_INFO(dev)->real_dev; |
| 692 | |
| 693 | if (change & IFF_ALLMULTI) |
| 694 | dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1); |
| 695 | if (change & IFF_PROMISC) |
| 696 | dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1); |
| 697 | } |
| 698 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | /** Taken from Gleb + Lennert's VLAN code, and modified... */ |
| 700 | void vlan_dev_set_multicast_list(struct net_device *vlan_dev) |
| 701 | { |
Patrick McHardy | 56addd6 | 2007-07-14 18:53:28 -0700 | [diff] [blame] | 702 | dev_mc_sync(VLAN_DEV_INFO(vlan_dev)->real_dev, vlan_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | } |