1 #include <linux/skbuff.h>
2 #include <linux/netdevice.h>
3 #include <linux/if_vlan.h>
4 #include <linux/netpoll.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);
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;
24 skb = *skbp = skb_share_check(skb, GFP_ATOMIC);
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,
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);
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);
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);
69 /* Must be invoked with rcu_read_lock or with RTNL. */
70 struct net_device *__vlan_find_dev_deep(struct net_device *real_dev,
73 struct vlan_group *grp = rcu_dereference_rtnl(real_dev->vlgrp);
76 return vlan_group_get_device(grp, vlan_id);
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);
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)
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);
112 static void vlan_set_encap_proto(struct sk_buff *skb, struct vlan_hdr *vhdr)
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;
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);
142 skb->protocol = htons(ETH_P_802_2);
145 struct sk_buff *vlan_untag(struct sk_buff *skb)
147 struct vlan_hdr *vhdr;
150 if (unlikely(vlan_tx_tag_present(skb))) {
151 /* vlan_tci is already set-up so leave this for another time */
155 skb = skb_share_check(skb, GFP_ATOMIC);
159 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
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);
173 skb_reset_network_header(skb);
174 skb_reset_transport_header(skb);