Merge branch 'writeback-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[pandora-kernel.git] / net / 8021q / vlan_core.c
1 #include <linux/skbuff.h>
2 #include <linux/netdevice.h>
3 #include <linux/if_vlan.h>
4 #include <linux/netpoll.h>
5 #include "vlan.h"
6
7 bool vlan_do_receive(struct sk_buff **skbp, bool last_handler)
8 {
9         struct sk_buff *skb = *skbp;
10         u16 vlan_id = skb->vlan_tci & VLAN_VID_MASK;
11         struct net_device *vlan_dev;
12         struct vlan_pcpu_stats *rx_stats;
13
14         vlan_dev = vlan_find_dev(skb->dev, vlan_id);
15         if (!vlan_dev) {
16                 /* Only the last call to vlan_do_receive() should change
17                  * pkt_type to PACKET_OTHERHOST
18                  */
19                 if (vlan_id && last_handler)
20                         skb->pkt_type = PACKET_OTHERHOST;
21                 return false;
22         }
23
24         skb = *skbp = skb_share_check(skb, GFP_ATOMIC);
25         if (unlikely(!skb))
26                 return false;
27
28         skb->dev = vlan_dev;
29         if (skb->pkt_type == PACKET_OTHERHOST) {
30                 /* Our lower layer thinks this is not local, let's make sure.
31                  * This allows the VLAN to have a different MAC than the
32                  * underlying device, and still route correctly. */
33                 if (!compare_ether_addr(eth_hdr(skb)->h_dest,
34                                         vlan_dev->dev_addr))
35                         skb->pkt_type = PACKET_HOST;
36         }
37
38         if (!(vlan_dev_info(vlan_dev)->flags & VLAN_FLAG_REORDER_HDR)) {
39                 unsigned int offset = skb->data - skb_mac_header(skb);
40
41                 /*
42                  * vlan_insert_tag expect skb->data pointing to mac header.
43                  * So change skb->data before calling it and change back to
44                  * original position later
45                  */
46                 skb_push(skb, offset);
47                 skb = *skbp = vlan_insert_tag(skb, skb->vlan_tci);
48                 if (!skb)
49                         return false;
50                 skb_pull(skb, offset + VLAN_HLEN);
51                 skb_reset_mac_len(skb);
52         }
53
54         skb->priority = vlan_get_ingress_priority(vlan_dev, skb->vlan_tci);
55         skb->vlan_tci = 0;
56
57         rx_stats = this_cpu_ptr(vlan_dev_info(vlan_dev)->vlan_pcpu_stats);
58
59         u64_stats_update_begin(&rx_stats->syncp);
60         rx_stats->rx_packets++;
61         rx_stats->rx_bytes += skb->len;
62         if (skb->pkt_type == PACKET_MULTICAST)
63                 rx_stats->rx_multicast++;
64         u64_stats_update_end(&rx_stats->syncp);
65
66         return true;
67 }
68
69 /* Must be invoked with rcu_read_lock or with RTNL. */
70 struct net_device *__vlan_find_dev_deep(struct net_device *real_dev,
71                                         u16 vlan_id)
72 {
73         struct vlan_group *grp = rcu_dereference_rtnl(real_dev->vlgrp);
74
75         if (grp) {
76                 return vlan_group_get_device(grp, vlan_id);
77         } else {
78                 /*
79                  * Bonding slaves do not have grp assigned to themselves.
80                  * Grp is assigned to bonding master instead.
81                  */
82                 if (netif_is_bond_slave(real_dev))
83                         return __vlan_find_dev_deep(real_dev->master, vlan_id);
84         }
85
86         return NULL;
87 }
88 EXPORT_SYMBOL(__vlan_find_dev_deep);
89
90 struct net_device *vlan_dev_real_dev(const struct net_device *dev)
91 {
92         return vlan_dev_info(dev)->real_dev;
93 }
94 EXPORT_SYMBOL(vlan_dev_real_dev);
95
96 u16 vlan_dev_vlan_id(const struct net_device *dev)
97 {
98         return vlan_dev_info(dev)->vlan_id;
99 }
100 EXPORT_SYMBOL(vlan_dev_vlan_id);
101
102 static struct sk_buff *vlan_reorder_header(struct sk_buff *skb)
103 {
104         if (skb_cow(skb, skb_headroom(skb)) < 0)
105                 return NULL;
106         memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN);
107         skb->mac_header += VLAN_HLEN;
108         skb_reset_mac_len(skb);
109         return skb;
110 }
111
112 static void vlan_set_encap_proto(struct sk_buff *skb, struct vlan_hdr *vhdr)
113 {
114         __be16 proto;
115         unsigned char *rawp;
116
117         /*
118          * Was a VLAN packet, grab the encapsulated protocol, which the layer
119          * three protocols care about.
120          */
121
122         proto = vhdr->h_vlan_encapsulated_proto;
123         if (ntohs(proto) >= 1536) {
124                 skb->protocol = proto;
125                 return;
126         }
127
128         rawp = skb->data;
129         if (*(unsigned short *) rawp == 0xFFFF)
130                 /*
131                  * This is a magic hack to spot IPX packets. Older Novell
132                  * breaks the protocol design and runs IPX over 802.3 without
133                  * an 802.2 LLC layer. We look for FFFF which isn't a used
134                  * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
135                  * but does for the rest.
136                  */
137                 skb->protocol = htons(ETH_P_802_3);
138         else
139                 /*
140                  * Real 802.2 LLC
141                  */
142                 skb->protocol = htons(ETH_P_802_2);
143 }
144
145 struct sk_buff *vlan_untag(struct sk_buff *skb)
146 {
147         struct vlan_hdr *vhdr;
148         u16 vlan_tci;
149
150         if (unlikely(vlan_tx_tag_present(skb))) {
151                 /* vlan_tci is already set-up so leave this for another time */
152                 return skb;
153         }
154
155         skb = skb_share_check(skb, GFP_ATOMIC);
156         if (unlikely(!skb))
157                 goto err_free;
158
159         if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
160                 goto err_free;
161
162         vhdr = (struct vlan_hdr *) skb->data;
163         vlan_tci = ntohs(vhdr->h_vlan_TCI);
164         __vlan_hwaccel_put_tag(skb, vlan_tci);
165
166         skb_pull_rcsum(skb, VLAN_HLEN);
167         vlan_set_encap_proto(skb, vhdr);
168
169         skb = vlan_reorder_header(skb);
170         if (unlikely(!skb))
171                 goto err_free;
172
173         skb_reset_network_header(skb);
174         skb_reset_transport_header(skb);
175         return skb;
176
177 err_free:
178         kfree_skb(skb);
179         return NULL;
180 }