flow_dissector.c 3.1 KB

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