vlan_core.c 4.4 KB

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