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