flows.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  3. * Copyright (c) 2013-2022 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  4. *
  5. * The Tcpreplay Suite of tools is free software: you can redistribute it
  6. * and/or modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or with the authors permission any later version.
  9. *
  10. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "flows.h"
  19. #include "tcpreplay_api.h"
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "../../lib/sll.h"
  24. #define JUNIPER_FLAG_NO_L2 0x02 /* L2 header */
  25. #define JUNIPER_FLAG_EXT 0x80 /* Juniper extensions present */
  26. #define JUNIPER_PCAP_MAGIC "MGC"
  27. /* 5-tuple plus VLAN ID */
  28. typedef struct flow_entry_data {
  29. union {
  30. struct in_addr in;
  31. struct in6_addr in6;
  32. } src_ip;
  33. union {
  34. struct in_addr in;
  35. struct in6_addr in6;
  36. } dst_ip;
  37. uint16_t src_port;
  38. uint16_t dst_port;
  39. uint16_t vlan;
  40. uint8_t protocol;
  41. } flow_entry_data_t;
  42. typedef struct flow_hash_entry {
  43. uint32_t key;
  44. flow_entry_data_t data;
  45. struct timeval ts_last_seen;
  46. struct flow_hash_entry *next;
  47. } flow_hash_entry_t;
  48. struct flow_hash_table {
  49. size_t num_buckets;
  50. flow_hash_entry_t **buckets;
  51. };
  52. static bool is_power_of_2(size_t n)
  53. {
  54. return (n != 0 && ((n & (n - 1)) == 0));
  55. }
  56. /*
  57. * Perl's hash function
  58. *
  59. * We do extensive hashing to prevent hash table collisions.
  60. * It will save time in the long run.
  61. */
  62. static inline uint32_t hash_func(const void *key, size_t length)
  63. {
  64. register size_t i = length;
  65. register uint32_t hv = 0;
  66. register const u_char *s = (u_char *)key;
  67. while (i--) {
  68. hv += *s++;
  69. hv += (hv << 10);
  70. hv ^= (hv >> 6);
  71. }
  72. hv += (hv << 3);
  73. hv ^= (hv >> 11);
  74. hv += (hv << 15);
  75. return hv;
  76. }
  77. /*
  78. * add hash value to hash table bucket
  79. */
  80. static inline flow_hash_entry_t *hash_add_entry(flow_hash_table_t *fht, const uint32_t hv,
  81. const uint32_t key, const flow_entry_data_t *hash_entry)
  82. {
  83. flow_hash_entry_t *he;
  84. assert(hv < fht->num_buckets);
  85. he = safe_malloc(sizeof (*he));
  86. if (!he) {
  87. warn("out of memory");
  88. return NULL;
  89. }
  90. he->key = key;
  91. he->next = fht->buckets[hv];
  92. fht->buckets[hv] = he;
  93. memcpy(&he->data, hash_entry, sizeof(he->data));
  94. return he;
  95. }
  96. /*
  97. * Search for this entry in the hash table and
  98. * insert it if not found. Report whether this
  99. * is a new, existing or expired flow.
  100. *
  101. * Only check for expiry if 'expiry' is set
  102. */
  103. static inline flow_entry_type_t hash_put_data(flow_hash_table_t *fht, const uint32_t key,
  104. const flow_entry_data_t *hash_entry, const struct timeval *tv, const int expiry)
  105. {
  106. uint32_t hash_value = key & (fht->num_buckets - 1);
  107. flow_hash_entry_t *he;
  108. flow_entry_type_t res = FLOW_ENTRY_INVALID;
  109. for (he = fht->buckets[hash_value]; he; he = he->next) {
  110. /*
  111. * found an existing entry with similar hash. double
  112. * check to see if it is our flow or just a collision
  113. */
  114. if (he->key == key && !memcmp(&he->data, hash_entry, sizeof(he->data)))
  115. break;
  116. }
  117. if (he) {
  118. /* this is not a new flow */
  119. if (expiry && tv->tv_sec > (expiry + he->ts_last_seen.tv_sec))
  120. res = FLOW_ENTRY_EXPIRED;
  121. else
  122. res = FLOW_ENTRY_EXISTING;
  123. if (expiry)
  124. TIMEVAL_SET(&he->ts_last_seen, tv);
  125. } else {
  126. /* this is a new flow */
  127. if ((he = hash_add_entry(fht, hash_value, key, hash_entry)) != NULL) {
  128. res = FLOW_ENTRY_NEW;
  129. if (expiry)
  130. TIMEVAL_SET(&he->ts_last_seen, tv);
  131. } else
  132. res = FLOW_ENTRY_INVALID;
  133. }
  134. dbgx(2, "flow type=%d", (int)res);
  135. return res;
  136. }
  137. /*
  138. * Decode the packet, study it's flow status and report
  139. */
  140. flow_entry_type_t flow_decode(flow_hash_table_t *fht, const struct pcap_pkthdr *pkthdr,
  141. const u_char *pktdata, const int datalink, const int expiry)
  142. {
  143. uint32_t pkt_len = pkthdr->caplen;
  144. const u_char *packet = pktdata;
  145. uint32_t _U_ vlan_offset;
  146. uint32_t _U_ l2offset;
  147. uint16_t ether_type = 0;
  148. ipv4_hdr_t *ip_hdr = NULL;
  149. ipv6_hdr_t *ip6_hdr = NULL;
  150. tcp_hdr_t *tcp_hdr;
  151. udp_hdr_t *udp_hdr;
  152. icmpv4_hdr_t *icmp_hdr;
  153. flow_entry_data_t entry;
  154. uint32_t l2len = 0;
  155. uint8_t protocol;
  156. uint32_t hash;
  157. int ip_len;
  158. int res;
  159. assert(fht);
  160. assert(packet);
  161. /*
  162. * extract the 5-tuple and populate the entry data
  163. */
  164. memset(&entry, 0, sizeof(entry));
  165. res = get_l2len_protocol(packet,
  166. pkt_len,
  167. datalink,
  168. &ether_type,
  169. &l2len,
  170. &l2offset,
  171. &vlan_offset);
  172. if (res == -1) {
  173. warnx("Unable to process unsupported DLT type: %s (0x%x)",
  174. pcap_datalink_val_to_description(datalink), datalink);
  175. return FLOW_ENTRY_INVALID;
  176. }
  177. assert(l2len > 0);
  178. if (ether_type == ETHERTYPE_IP) {
  179. if (pkt_len < l2len + sizeof(ipv4_hdr_t))
  180. return FLOW_ENTRY_INVALID;
  181. ip_hdr = (ipv4_hdr_t *)(packet + l2len);
  182. if (ip_hdr->ip_v != 4)
  183. return FLOW_ENTRY_NON_IP;
  184. ip_len = ip_hdr->ip_hl * 4;
  185. protocol = ip_hdr->ip_p;
  186. entry.src_ip.in = ip_hdr->ip_src;
  187. entry.dst_ip.in = ip_hdr->ip_dst;
  188. } else if (ether_type == ETHERTYPE_IP6) {
  189. if (pkt_len < l2len + sizeof(ipv6_hdr_t))
  190. return FLOW_ENTRY_INVALID;
  191. if ((packet[0] >> 4) != 6)
  192. return FLOW_ENTRY_NON_IP;
  193. ip6_hdr = (ipv6_hdr_t *)(packet + l2len);
  194. ip_len = sizeof(*ip6_hdr);
  195. protocol = ip6_hdr->ip_nh;
  196. if (protocol == 0) {
  197. struct tcpr_ipv6_ext_hdr_base *ext = (struct tcpr_ipv6_ext_hdr_base *)(ip6_hdr + 1);
  198. ip_len += (ext->ip_len + 1) * 8;
  199. protocol = ext->ip_nh;
  200. }
  201. memcpy(&entry.src_ip.in6, &ip6_hdr->ip_src, sizeof(entry.src_ip.in6));
  202. memcpy(&entry.dst_ip.in6, &ip6_hdr->ip_dst, sizeof(entry.dst_ip.in6));
  203. } else {
  204. return FLOW_ENTRY_NON_IP;
  205. }
  206. entry.protocol = protocol;
  207. switch (protocol) {
  208. case IPPROTO_UDP:
  209. if (pkt_len < (l2len + ip_len + sizeof(udp_hdr_t)))
  210. return FLOW_ENTRY_INVALID;
  211. udp_hdr = (udp_hdr_t*)(packet + ip_len + l2len);
  212. entry.src_port = udp_hdr->uh_sport;
  213. entry.dst_port = udp_hdr->uh_dport;
  214. break;
  215. case IPPROTO_TCP:
  216. if (pkt_len < (l2len + ip_len + sizeof(tcp_hdr_t)))
  217. return FLOW_ENTRY_INVALID;
  218. tcp_hdr = (tcp_hdr_t*)(packet + ip_len + l2len);
  219. entry.src_port = tcp_hdr->th_sport;
  220. entry.dst_port = tcp_hdr->th_dport;
  221. break;
  222. case IPPROTO_ICMP:
  223. case IPPROTO_ICMPV6:
  224. if (pkt_len < (l2len + ip_len + sizeof(icmpv4_hdr_t)))
  225. return FLOW_ENTRY_INVALID;
  226. icmp_hdr = (icmpv4_hdr_t*)(packet + ip_len + l2len);
  227. entry.src_port = icmp_hdr->icmp_type;
  228. entry.dst_port = icmp_hdr->icmp_code;
  229. break;
  230. }
  231. /* hash the 5-tuple */
  232. hash = hash_func(&entry, sizeof(entry));
  233. return hash_put_data(fht, hash, &entry, &pkthdr->ts, expiry);
  234. }
  235. static void flow_cache_clear(flow_hash_table_t *fht)
  236. {
  237. flow_hash_entry_t *fhe = NULL;
  238. flow_hash_entry_t *fhe_next = NULL;
  239. size_t i;
  240. for (i = 0; i < fht->num_buckets; i++) {
  241. if ((fhe = fht->buckets[i]) != NULL) {
  242. while (fhe) {
  243. fhe_next = fhe->next;
  244. safe_free(fhe);
  245. fhe = fhe_next;
  246. }
  247. fht->buckets[i] = NULL;
  248. }
  249. }
  250. }
  251. flow_hash_table_t *flow_hash_table_init(size_t n)
  252. {
  253. flow_hash_table_t *fht;
  254. if (!is_power_of_2(n))
  255. errx(-1, "invalid table size: %zu", n);
  256. fht = safe_malloc(sizeof(*fht));
  257. fht->num_buckets = n;
  258. fht->buckets = safe_malloc(sizeof(flow_hash_entry_t) * n);
  259. return fht;
  260. }
  261. void flow_hash_table_release(flow_hash_table_t *fht)
  262. {
  263. if (!fht)
  264. return;
  265. flow_cache_clear(fht);
  266. safe_free(fht->buckets);
  267. safe_free(fht);
  268. }