MIPS: Yosemite, Emma: Fix off-by-two in arcs_cmdline buffer size check
[linux-2.6/linux-mips.git] / net / 8021q / vlan_core.c
blob163397f1fd5adefd655efcfb681544b1a56655bf
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"
7 bool vlan_do_receive(struct sk_buff **skbp, bool last_handler)
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;
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
19 if (vlan_id && last_handler)
20 skb->pkt_type = PACKET_OTHERHOST;
21 return false;
24 skb = *skbp = skb_share_check(skb, GFP_ATOMIC);
25 if (unlikely(!skb))
26 return false;
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;
38 if (!(vlan_dev_info(vlan_dev)->flags & VLAN_FLAG_REORDER_HDR)) {
39 unsigned int offset = skb->data - skb_mac_header(skb);
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
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);
54 skb->priority = vlan_get_ingress_priority(vlan_dev, skb->vlan_tci);
55 skb->vlan_tci = 0;
57 rx_stats = this_cpu_ptr(vlan_dev_info(vlan_dev)->vlan_pcpu_stats);
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);
66 return true;
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)
73 struct vlan_group *grp = rcu_dereference_rtnl(real_dev->vlgrp);
75 if (grp) {
76 return vlan_group_get_device(grp, vlan_id);
77 } else {
79 * Bonding slaves do not have grp assigned to themselves.
80 * Grp is assigned to bonding master instead.
82 if (netif_is_bond_slave(real_dev))
83 return __vlan_find_dev_deep(real_dev->master, vlan_id);
86 return NULL;
88 EXPORT_SYMBOL(__vlan_find_dev_deep);
90 struct net_device *vlan_dev_real_dev(const struct net_device *dev)
92 return vlan_dev_info(dev)->real_dev;
94 EXPORT_SYMBOL(vlan_dev_real_dev);
96 u16 vlan_dev_vlan_id(const struct net_device *dev)
98 return vlan_dev_info(dev)->vlan_id;
100 EXPORT_SYMBOL(vlan_dev_vlan_id);
102 static struct sk_buff *vlan_reorder_header(struct sk_buff *skb)
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;
112 static void vlan_set_encap_proto(struct sk_buff *skb, struct vlan_hdr *vhdr)
114 __be16 proto;
115 unsigned char *rawp;
118 * Was a VLAN packet, grab the encapsulated protocol, which the layer
119 * three protocols care about.
122 proto = vhdr->h_vlan_encapsulated_proto;
123 if (ntohs(proto) >= 1536) {
124 skb->protocol = proto;
125 return;
128 rawp = skb->data;
129 if (*(unsigned short *) rawp == 0xFFFF)
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.
137 skb->protocol = htons(ETH_P_802_3);
138 else
140 * Real 802.2 LLC
142 skb->protocol = htons(ETH_P_802_2);
145 struct sk_buff *vlan_untag(struct sk_buff *skb)
147 struct vlan_hdr *vhdr;
148 u16 vlan_tci;
150 if (unlikely(vlan_tx_tag_present(skb))) {
151 /* vlan_tci is already set-up so leave this for another time */
152 return skb;
155 skb = skb_share_check(skb, GFP_ATOMIC);
156 if (unlikely(!skb))
157 goto err_free;
159 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
160 goto err_free;
162 vhdr = (struct vlan_hdr *) skb->data;
163 vlan_tci = ntohs(vhdr->h_vlan_TCI);
164 __vlan_hwaccel_put_tag(skb, vlan_tci);
166 skb_pull_rcsum(skb, VLAN_HLEN);
167 vlan_set_encap_proto(skb, vhdr);
169 skb = vlan_reorder_header(skb);
170 if (unlikely(!skb))
171 goto err_free;
173 skb_reset_network_header(skb);
174 skb_reset_transport_header(skb);
175 return skb;
177 err_free:
178 kfree_skb(skb);
179 return NULL;