flow_dissector.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include <linux/skbuff.h>
  2. #include <linux/ip.h>
  3. #include <linux/ipv6.h>
  4. #include <linux/if_vlan.h>
  5. #include <net/ip.h>
  6. #include <linux/if_tunnel.h>
  7. #include <linux/if_pppox.h>
  8. #include <linux/ppp_defs.h>
  9. #include <net/flow_keys.h>
  10. /* copy saddr & daddr, possibly using 64bit load/store
  11. * Equivalent to : flow->src = iph->saddr;
  12. * flow->dst = iph->daddr;
  13. */
  14. static void iph_to_flow_copy_addrs(struct flow_keys *flow, const struct iphdr *iph)
  15. {
  16. BUILD_BUG_ON(offsetof(typeof(*flow), dst) !=
  17. offsetof(typeof(*flow), src) + sizeof(flow->src));
  18. memcpy(&flow->src, &iph->saddr, sizeof(flow->src) + sizeof(flow->dst));
  19. }
  20. bool skb_flow_dissect(const struct sk_buff *skb, struct flow_keys *flow)
  21. {
  22. int poff, nhoff = skb_network_offset(skb);
  23. u8 ip_proto;
  24. __be16 proto = skb->protocol;
  25. memset(flow, 0, sizeof(*flow));
  26. again:
  27. switch (proto) {
  28. case __constant_htons(ETH_P_IP): {
  29. const struct iphdr *iph;
  30. struct iphdr _iph;
  31. ip:
  32. iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
  33. if (!iph)
  34. return false;
  35. if (ip_is_fragment(iph))
  36. ip_proto = 0;
  37. else
  38. ip_proto = iph->protocol;
  39. iph_to_flow_copy_addrs(flow, iph);
  40. nhoff += iph->ihl * 4;
  41. break;
  42. }
  43. case __constant_htons(ETH_P_IPV6): {
  44. const struct ipv6hdr *iph;
  45. struct ipv6hdr _iph;
  46. ipv6:
  47. iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
  48. if (!iph)
  49. return false;
  50. ip_proto = iph->nexthdr;
  51. flow->src = iph->saddr.s6_addr32[3];
  52. flow->dst = iph->daddr.s6_addr32[3];
  53. nhoff += sizeof(struct ipv6hdr);
  54. break;
  55. }
  56. case __constant_htons(ETH_P_8021Q): {
  57. const struct vlan_hdr *vlan;
  58. struct vlan_hdr _vlan;
  59. vlan = skb_header_pointer(skb, nhoff, sizeof(_vlan), &_vlan);
  60. if (!vlan)
  61. return false;
  62. proto = vlan->h_vlan_encapsulated_proto;
  63. nhoff += sizeof(*vlan);
  64. goto again;
  65. }
  66. case __constant_htons(ETH_P_PPP_SES): {
  67. struct {
  68. struct pppoe_hdr hdr;
  69. __be16 proto;
  70. } *hdr, _hdr;
  71. hdr = skb_header_pointer(skb, nhoff, sizeof(_hdr), &_hdr);
  72. if (!hdr)
  73. return false;
  74. proto = hdr->proto;
  75. nhoff += PPPOE_SES_HLEN;
  76. switch (proto) {
  77. case __constant_htons(PPP_IP):
  78. goto ip;
  79. case __constant_htons(PPP_IPV6):
  80. goto ipv6;
  81. default:
  82. return false;
  83. }
  84. }
  85. default:
  86. return false;
  87. }
  88. switch (ip_proto) {
  89. case IPPROTO_GRE: {
  90. struct gre_hdr {
  91. __be16 flags;
  92. __be16 proto;
  93. } *hdr, _hdr;
  94. hdr = skb_header_pointer(skb, nhoff, sizeof(_hdr), &_hdr);
  95. if (!hdr)
  96. return false;
  97. /*
  98. * Only look inside GRE if version zero and no
  99. * routing
  100. */
  101. if (!(hdr->flags & (GRE_VERSION|GRE_ROUTING))) {
  102. proto = hdr->proto;
  103. nhoff += 4;
  104. if (hdr->flags & GRE_CSUM)
  105. nhoff += 4;
  106. if (hdr->flags & GRE_KEY)
  107. nhoff += 4;
  108. if (hdr->flags & GRE_SEQ)
  109. nhoff += 4;
  110. goto again;
  111. }
  112. break;
  113. }
  114. case IPPROTO_IPIP:
  115. goto again;
  116. default:
  117. break;
  118. }
  119. flow->ip_proto = ip_proto;
  120. poff = proto_ports_offset(ip_proto);
  121. if (poff >= 0) {
  122. __be32 *ports, _ports;
  123. nhoff += poff;
  124. ports = skb_header_pointer(skb, nhoff, sizeof(_ports), &_ports);
  125. if (ports)
  126. flow->ports = *ports;
  127. }
  128. return true;
  129. }
  130. EXPORT_SYMBOL(skb_flow_dissect);