flows.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  3. * Copyright (c) 2013-2018 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. /* 5-tuple plus VLAN ID */
  25. typedef struct flow_entry_data {
  26. union {
  27. struct in_addr in;
  28. struct in6_addr in6;
  29. } src_ip;
  30. union {
  31. struct in_addr in;
  32. struct in6_addr in6;
  33. } dst_ip;
  34. uint16_t src_port;
  35. uint16_t dst_port;
  36. uint16_t vlan;
  37. uint8_t protocol;
  38. } flow_entry_data_t;
  39. typedef struct flow_hash_entry {
  40. uint32_t key;
  41. flow_entry_data_t data;
  42. struct timeval ts_last_seen;
  43. struct flow_hash_entry *next;
  44. } flow_hash_entry_t;
  45. struct flow_hash_table {
  46. size_t num_buckets;
  47. flow_hash_entry_t **buckets;
  48. };
  49. static bool is_power_of_2(size_t n)
  50. {
  51. return (n != 0 && ((n & (n - 1)) == 0));
  52. }
  53. /*
  54. * Perl's hash function
  55. *
  56. * We do extensive hashing to prevent hash table collisions.
  57. * It will save time in the long run.
  58. */
  59. static inline uint32_t hash_func(const void *key, size_t length)
  60. {
  61. register size_t i = length;
  62. register uint32_t hv = 0;
  63. register const u_char *s = (u_char *)key;
  64. while (i--) {
  65. hv += *s++;
  66. hv += (hv << 10);
  67. hv ^= (hv >> 6);
  68. }
  69. hv += (hv << 3);
  70. hv ^= (hv >> 11);
  71. hv += (hv << 15);
  72. return hv;
  73. }
  74. /*
  75. * add hash value to hash table bucket
  76. */
  77. static inline flow_hash_entry_t *hash_add_entry(flow_hash_table_t *fht, const uint32_t hv,
  78. const uint32_t key, const flow_entry_data_t *hash_entry)
  79. {
  80. flow_hash_entry_t *he;
  81. assert(hv < fht->num_buckets);
  82. he = safe_malloc(sizeof (*he));
  83. if (!he) {
  84. warn("out of memory");
  85. return NULL;
  86. }
  87. he->key = key;
  88. he->next = fht->buckets[hv];
  89. fht->buckets[hv] = he;
  90. memcpy(&he->data, hash_entry, sizeof(he->data));
  91. return he;
  92. }
  93. /*
  94. * Search for this entry in the hash table and
  95. * insert it if not found. Report whether this
  96. * is a new, existing or expired flow.
  97. *
  98. * Only check for expiry if 'expiry' is set
  99. */
  100. static inline flow_entry_type_t hash_put_data(flow_hash_table_t *fht, const uint32_t key,
  101. const flow_entry_data_t *hash_entry, const struct timeval *tv, const int expiry)
  102. {
  103. uint32_t hash_value = key & (fht->num_buckets - 1);
  104. flow_hash_entry_t *he;
  105. flow_entry_type_t res = FLOW_ENTRY_INVALID;
  106. for (he = fht->buckets[hash_value]; he; he = he->next) {
  107. /*
  108. * found an existing entry with similar hash. double
  109. * check to see if it is our flow or just a collision
  110. */
  111. if (he->key == key && !memcmp(&he->data, hash_entry, sizeof(he->data)))
  112. break;
  113. }
  114. if (he) {
  115. /* this is not a new flow */
  116. if (expiry && tv->tv_sec > (expiry + he->ts_last_seen.tv_sec))
  117. res = FLOW_ENTRY_EXPIRED;
  118. else
  119. res = FLOW_ENTRY_EXISTING;
  120. if (expiry)
  121. TIMEVAL_SET(&he->ts_last_seen, tv);
  122. } else {
  123. /* this is a new flow */
  124. if ((he = hash_add_entry(fht, hash_value, key, hash_entry)) != NULL) {
  125. res = FLOW_ENTRY_NEW;
  126. if (expiry)
  127. TIMEVAL_SET(&he->ts_last_seen, tv);
  128. } else
  129. res = FLOW_ENTRY_INVALID;
  130. }
  131. dbgx(2, "flow type=%d", (int)res);
  132. return res;
  133. }
  134. /*
  135. * Decode the packet, study it's flow status and report
  136. */
  137. flow_entry_type_t flow_decode(flow_hash_table_t *fht, const struct pcap_pkthdr *pkthdr,
  138. const u_char *pktdata, const int datalink, const int expiry)
  139. {
  140. uint16_t ether_type = 0;
  141. ipv4_hdr_t *ip_hdr = NULL;
  142. ipv6_hdr_t *ip6_hdr = NULL;
  143. tcp_hdr_t *tcp_hdr;
  144. udp_hdr_t *udp_hdr;
  145. icmpv4_hdr_t *icmp_hdr;
  146. hdlc_hdr_t *hdlc_hdr;
  147. sll_hdr_t *sll_hdr;
  148. struct tcpr_pppserial_hdr *ppp;
  149. flow_entry_data_t entry;
  150. uint32_t l2_len = 0;
  151. int ip_len;
  152. uint8_t protocol;
  153. uint32_t hash;
  154. assert(fht);
  155. assert(pktdata);
  156. /*
  157. * extract the 5-tuple and populate the entry data
  158. */
  159. memset(&entry, 0, sizeof(entry));
  160. switch (datalink) {
  161. case DLT_LINUX_SLL:
  162. l2_len = 16;
  163. if (pkthdr->caplen < l2_len)
  164. return FLOW_ENTRY_INVALID;
  165. sll_hdr = (sll_hdr_t *)pktdata;
  166. ether_type = sll_hdr->sll_protocol;
  167. break;
  168. case DLT_PPP_SERIAL:
  169. l2_len = 4;
  170. if (pkthdr->caplen < l2_len)
  171. return FLOW_ENTRY_INVALID;
  172. ppp = (struct tcpr_pppserial_hdr *)pktdata;
  173. if (ntohs(ppp->protocol) == 0x0021)
  174. ether_type = htons(ETHERTYPE_IP);
  175. else
  176. ether_type = ppp->protocol;
  177. break;
  178. case DLT_C_HDLC:
  179. l2_len = 4;
  180. if (pkthdr->caplen < l2_len)
  181. return FLOW_ENTRY_INVALID;
  182. hdlc_hdr = (hdlc_hdr_t *)pktdata;
  183. ether_type = hdlc_hdr->protocol;
  184. break;
  185. case DLT_RAW:
  186. if ((pktdata[0] >> 4) == 4)
  187. ether_type = ETHERTYPE_IP;
  188. else if ((pktdata[0] >> 4) == 6)
  189. ether_type = ETHERTYPE_IP6;
  190. break;
  191. case DLT_JUNIPER_ETHER:
  192. if (pkthdr->caplen < 5)
  193. return FLOW_ENTRY_INVALID;
  194. if (memcmp(pktdata, "MGC", 3))
  195. warnx("No Magic Number found: %s (0x%x)",
  196. pcap_datalink_val_to_description(datalink), datalink);
  197. if ((pktdata[3] & 0x80) == 0x80) {
  198. l2_len = ntohs(*((uint16_t*)&pktdata[4]));
  199. l2_len += 6;
  200. } else {
  201. l2_len = 4; /* no header extensions */
  202. }
  203. /* fallthrough */
  204. case DLT_EN10MB:
  205. /* l2_len will be zero if we did not fall through */
  206. if (pkthdr->caplen < l2_len + sizeof(eth_hdr_t))
  207. return FLOW_ENTRY_INVALID;
  208. ether_type = ntohs(((eth_hdr_t*)(pktdata + l2_len))->ether_type);
  209. l2_len += sizeof(eth_hdr_t);
  210. while (ether_type == ETHERTYPE_VLAN) {
  211. if (pkthdr->caplen < l2_len + sizeof(vlan_hdr_t))
  212. return FLOW_ENTRY_INVALID;
  213. vlan_hdr_t *vlan_hdr = (vlan_hdr_t *)(pktdata + l2_len);
  214. entry.vlan = vlan_hdr->vlan_tci & htons(0xfff);
  215. ether_type = ntohs(vlan_hdr->vlan_tpid);
  216. l2_len += 4;
  217. }
  218. break;
  219. default:
  220. warnx("Unable to process unsupported DLT type: %s (0x%x)",
  221. pcap_datalink_val_to_description(datalink), datalink);
  222. return FLOW_ENTRY_INVALID;
  223. }
  224. if (ether_type == ETHERTYPE_IP) {
  225. if (pkthdr->caplen < l2_len + sizeof(ipv4_hdr_t))
  226. return FLOW_ENTRY_INVALID;
  227. ip_hdr = (ipv4_hdr_t *)(pktdata + l2_len);
  228. if (ip_hdr->ip_v != 4)
  229. return FLOW_ENTRY_NON_IP;
  230. ip_len = ip_hdr->ip_hl * 4;
  231. protocol = ip_hdr->ip_p;
  232. entry.src_ip.in = ip_hdr->ip_src;
  233. entry.dst_ip.in = ip_hdr->ip_dst;
  234. } else if (ether_type == ETHERTYPE_IP6) {
  235. if (pkthdr->caplen < l2_len + sizeof(ipv6_hdr_t))
  236. return FLOW_ENTRY_INVALID;
  237. if ((pktdata[0] >> 4) != 6)
  238. return FLOW_ENTRY_NON_IP;
  239. ip6_hdr = (ipv6_hdr_t *)(pktdata + l2_len);
  240. ip_len = sizeof(*ip6_hdr);
  241. protocol = ip6_hdr->ip_nh;
  242. if (protocol == 0) {
  243. struct tcpr_ipv6_ext_hdr_base *ext = (struct tcpr_ipv6_ext_hdr_base *)(ip6_hdr + 1);
  244. ip_len += (ext->ip_len + 1) * 8;
  245. protocol = ext->ip_nh;
  246. }
  247. memcpy(&entry.src_ip.in6, &ip6_hdr->ip_src, sizeof(entry.src_ip.in6));
  248. memcpy(&entry.dst_ip.in6, &ip6_hdr->ip_dst, sizeof(entry.dst_ip.in6));
  249. } else {
  250. return FLOW_ENTRY_NON_IP;
  251. }
  252. entry.protocol = protocol;
  253. switch (protocol) {
  254. case IPPROTO_UDP:
  255. if (pkthdr->caplen < (l2_len + ip_len + sizeof(udp_hdr_t)))
  256. return FLOW_ENTRY_INVALID;
  257. udp_hdr = (udp_hdr_t*)(pktdata + ip_len + l2_len);
  258. entry.src_port = udp_hdr->uh_sport;
  259. entry.dst_port = udp_hdr->uh_dport;
  260. break;
  261. case IPPROTO_TCP:
  262. if (pkthdr->caplen < (l2_len + ip_len + sizeof(tcp_hdr_t)))
  263. return FLOW_ENTRY_INVALID;
  264. tcp_hdr = (tcp_hdr_t*)(pktdata + ip_len + l2_len);
  265. entry.src_port = tcp_hdr->th_sport;
  266. entry.dst_port = tcp_hdr->th_dport;
  267. break;
  268. case IPPROTO_ICMP:
  269. case IPPROTO_ICMPV6:
  270. if (pkthdr->caplen < (l2_len + ip_len + sizeof(icmpv4_hdr_t)))
  271. return FLOW_ENTRY_INVALID;
  272. icmp_hdr = (icmpv4_hdr_t*)(pktdata + ip_len + l2_len);
  273. entry.src_port = icmp_hdr->icmp_type;
  274. entry.dst_port = icmp_hdr->icmp_code;
  275. break;
  276. }
  277. /* hash the 5-tuple */
  278. hash = hash_func(&entry, sizeof(entry));
  279. return hash_put_data(fht, hash, &entry, &pkthdr->ts, expiry);
  280. }
  281. static void flow_cache_clear(flow_hash_table_t *fht)
  282. {
  283. flow_hash_entry_t *fhe = NULL;
  284. flow_hash_entry_t *fhe_next = NULL;
  285. size_t i;
  286. for (i = 0; i < fht->num_buckets; i++) {
  287. if ((fhe = fht->buckets[i]) != NULL) {
  288. while (fhe) {
  289. fhe_next = fhe->next;
  290. safe_free(fhe);
  291. fhe = fhe_next;
  292. }
  293. fht->buckets[i] = NULL;
  294. }
  295. }
  296. }
  297. flow_hash_table_t *flow_hash_table_init(size_t n)
  298. {
  299. flow_hash_table_t *fht;
  300. if (!is_power_of_2(n))
  301. errx(-1, "invalid table size: %zu", n);
  302. fht = safe_malloc(sizeof(*fht));
  303. fht->num_buckets = n;
  304. fht->buckets = safe_malloc(sizeof(flow_hash_entry_t) * n);
  305. return fht;
  306. }
  307. void flow_hash_table_release(flow_hash_table_t *fht)
  308. {
  309. if (!fht)
  310. return;
  311. flow_cache_clear(fht);
  312. safe_free(fht->buckets);
  313. safe_free(fht);
  314. }