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