vlan_core.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include <linux/skbuff.h>
  2. #include <linux/netdevice.h>
  3. #include <linux/if_vlan.h>
  4. #include <linux/netpoll.h>
  5. #include <linux/export.h>
  6. #include "vlan.h"
  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. vlan_dev = vlan_find_dev(skb->dev, vlan_id);
  14. if (!vlan_dev) {
  15. /* Only the last call to vlan_do_receive() should change
  16. * pkt_type to PACKET_OTHERHOST
  17. */
  18. if (vlan_id && last_handler)
  19. skb->pkt_type = PACKET_OTHERHOST;
  20. return false;
  21. }
  22. skb = *skbp = skb_share_check(skb, GFP_ATOMIC);
  23. if (unlikely(!skb))
  24. return false;
  25. skb->dev = vlan_dev;
  26. if (skb->pkt_type == PACKET_OTHERHOST) {
  27. /* Our lower layer thinks this is not local, let's make sure.
  28. * This allows the VLAN to have a different MAC than the
  29. * underlying device, and still route correctly. */
  30. if (!compare_ether_addr(eth_hdr(skb)->h_dest,
  31. vlan_dev->dev_addr))
  32. skb->pkt_type = PACKET_HOST;
  33. }
  34. if (!(vlan_dev_info(vlan_dev)->flags & VLAN_FLAG_REORDER_HDR)) {
  35. unsigned int offset = skb->data - skb_mac_header(skb);
  36. /*
  37. * vlan_insert_tag expect skb->data pointing to mac header.
  38. * So change skb->data before calling it and change back to
  39. * original position later
  40. */
  41. skb_push(skb, offset);
  42. skb = *skbp = vlan_insert_tag(skb, skb->vlan_tci);
  43. if (!skb)
  44. return false;
  45. skb_pull(skb, offset + VLAN_HLEN);
  46. skb_reset_mac_len(skb);
  47. }
  48. skb->priority = vlan_get_ingress_priority(vlan_dev, skb->vlan_tci);
  49. skb->vlan_tci = 0;
  50. rx_stats = this_cpu_ptr(vlan_dev_info(vlan_dev)->vlan_pcpu_stats);
  51. u64_stats_update_begin(&rx_stats->syncp);
  52. rx_stats->rx_packets++;
  53. rx_stats->rx_bytes += skb->len;
  54. if (skb->pkt_type == PACKET_MULTICAST)
  55. rx_stats->rx_multicast++;
  56. u64_stats_update_end(&rx_stats->syncp);
  57. return true;
  58. }
  59. /* Must be invoked with rcu_read_lock or with RTNL. */
  60. struct net_device *__vlan_find_dev_deep(struct net_device *real_dev,
  61. u16 vlan_id)
  62. {
  63. struct vlan_group *grp = rcu_dereference_rtnl(real_dev->vlgrp);
  64. if (grp) {
  65. return vlan_group_get_device(grp, vlan_id);
  66. } else {
  67. /*
  68. * Bonding slaves do not have grp assigned to themselves.
  69. * Grp is assigned to bonding master instead.
  70. */
  71. if (netif_is_bond_slave(real_dev))
  72. return __vlan_find_dev_deep(real_dev->master, vlan_id);
  73. }
  74. return NULL;
  75. }
  76. EXPORT_SYMBOL(__vlan_find_dev_deep);
  77. struct net_device *vlan_dev_real_dev(const struct net_device *dev)
  78. {
  79. return vlan_dev_info(dev)->real_dev;
  80. }
  81. EXPORT_SYMBOL(vlan_dev_real_dev);
  82. u16 vlan_dev_vlan_id(const struct net_device *dev)
  83. {
  84. return vlan_dev_info(dev)->vlan_id;
  85. }
  86. EXPORT_SYMBOL(vlan_dev_vlan_id);
  87. static struct sk_buff *vlan_reorder_header(struct sk_buff *skb)
  88. {
  89. if (skb_cow(skb, skb_headroom(skb)) < 0)
  90. return NULL;
  91. memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN);
  92. skb->mac_header += VLAN_HLEN;
  93. skb_reset_mac_len(skb);
  94. return skb;
  95. }
  96. static void vlan_set_encap_proto(struct sk_buff *skb, struct vlan_hdr *vhdr)
  97. {
  98. __be16 proto;
  99. unsigned char *rawp;
  100. /*
  101. * Was a VLAN packet, grab the encapsulated protocol, which the layer
  102. * three protocols care about.
  103. */
  104. proto = vhdr->h_vlan_encapsulated_proto;
  105. if (ntohs(proto) >= 1536) {
  106. skb->protocol = proto;
  107. return;
  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. struct sk_buff *vlan_untag(struct sk_buff *skb)
  126. {
  127. struct vlan_hdr *vhdr;
  128. u16 vlan_tci;
  129. if (unlikely(vlan_tx_tag_present(skb))) {
  130. /* vlan_tci is already set-up so leave this for another time */
  131. return skb;
  132. }
  133. skb = skb_share_check(skb, GFP_ATOMIC);
  134. if (unlikely(!skb))
  135. goto err_free;
  136. if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
  137. goto err_free;
  138. vhdr = (struct vlan_hdr *) skb->data;
  139. vlan_tci = ntohs(vhdr->h_vlan_TCI);
  140. __vlan_hwaccel_put_tag(skb, vlan_tci);
  141. skb_pull_rcsum(skb, VLAN_HLEN);
  142. vlan_set_encap_proto(skb, vhdr);
  143. skb = vlan_reorder_header(skb);
  144. if (unlikely(!skb))
  145. goto err_free;
  146. skb_reset_network_header(skb);
  147. skb_reset_transport_header(skb);
  148. return skb;
  149. err_free:
  150. kfree_skb(skb);
  151. return NULL;
  152. }