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