flow_dissector.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. /* CVE-2013-4348 issue : make sure iph->ihl is not zero ... */
  35. if (!iph || iph->ihl < 5)
  36. return false;
  37. if (ip_is_fragment(iph))
  38. ip_proto = 0;
  39. else
  40. ip_proto = iph->protocol;
  41. iph_to_flow_copy_addrs(flow, iph);
  42. nhoff += iph->ihl * 4;
  43. break;
  44. }
  45. case __constant_htons(ETH_P_IPV6): {
  46. const struct ipv6hdr *iph;
  47. struct ipv6hdr _iph;
  48. ipv6:
  49. iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
  50. if (!iph)
  51. return false;
  52. ip_proto = iph->nexthdr;
  53. flow->src = iph->saddr.s6_addr32[3];
  54. flow->dst = iph->daddr.s6_addr32[3];
  55. nhoff += sizeof(struct ipv6hdr);
  56. break;
  57. }
  58. case __constant_htons(ETH_P_8021Q): {
  59. const struct vlan_hdr *vlan;
  60. struct vlan_hdr _vlan;
  61. vlan = skb_header_pointer(skb, nhoff, sizeof(_vlan), &_vlan);
  62. if (!vlan)
  63. return false;
  64. proto = vlan->h_vlan_encapsulated_proto;
  65. nhoff += sizeof(*vlan);
  66. goto again;
  67. }
  68. case __constant_htons(ETH_P_PPP_SES): {
  69. struct {
  70. struct pppoe_hdr hdr;
  71. __be16 proto;
  72. } *hdr, _hdr;
  73. hdr = skb_header_pointer(skb, nhoff, sizeof(_hdr), &_hdr);
  74. if (!hdr)
  75. return false;
  76. proto = hdr->proto;
  77. nhoff += PPPOE_SES_HLEN;
  78. switch (proto) {
  79. case __constant_htons(PPP_IP):
  80. goto ip;
  81. case __constant_htons(PPP_IPV6):
  82. goto ipv6;
  83. default:
  84. return false;
  85. }
  86. }
  87. default:
  88. return false;
  89. }
  90. switch (ip_proto) {
  91. case IPPROTO_GRE: {
  92. struct gre_hdr {
  93. __be16 flags;
  94. __be16 proto;
  95. } *hdr, _hdr;
  96. hdr = skb_header_pointer(skb, nhoff, sizeof(_hdr), &_hdr);
  97. if (!hdr)
  98. return false;
  99. /*
  100. * Only look inside GRE if version zero and no
  101. * routing
  102. */
  103. if (!(hdr->flags & (GRE_VERSION|GRE_ROUTING))) {
  104. proto = hdr->proto;
  105. nhoff += 4;
  106. if (hdr->flags & GRE_CSUM)
  107. nhoff += 4;
  108. if (hdr->flags & GRE_KEY)
  109. nhoff += 4;
  110. if (hdr->flags & GRE_SEQ)
  111. nhoff += 4;
  112. goto again;
  113. }
  114. break;
  115. }
  116. case IPPROTO_IPIP:
  117. goto again;
  118. default:
  119. break;
  120. }
  121. flow->ip_proto = ip_proto;
  122. poff = proto_ports_offset(ip_proto);
  123. if (poff >= 0) {
  124. __be32 *ports, _ports;
  125. ports = skb_header_pointer(skb, nhoff + poff,
  126. sizeof(_ports), &_ports);
  127. if (ports)
  128. flow->ports = *ports;
  129. }
  130. return true;
  131. }
  132. EXPORT_SYMBOL(skb_flow_dissect);