vlan_core.c 4.3 KB

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