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