vlan_core.c 4.2 KB

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