recv.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * ZMap Copyright 2013 Regents of the University of Michigan
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy
  6. * of the License at http://www.apache.org/licenses/LICENSE-2.0
  7. */
  8. #include "recv.h"
  9. #include <assert.h>
  10. #include "../lib/includes.h"
  11. #include "../lib/logger.h"
  12. #include "../lib/pbm.h"
  13. #include <pthread.h>
  14. #include <unistd.h>
  15. #include "recv-internal.h"
  16. #include "state.h"
  17. #include "validate.h"
  18. #include "fieldset.h"
  19. #include "expression.h"
  20. #include "probe_modules/probe_modules.h"
  21. #include "output_modules/output_modules.h"
  22. static u_char fake_eth_hdr[65535];
  23. // bitmap of observed IP addresses
  24. static uint8_t **seen = NULL;
  25. void handle_packet(uint32_t buflen, const u_char *bytes) {
  26. if ((sizeof(struct ip) + (zconf.send_ip_pkts ? 0 : sizeof(struct ether_header))) > buflen) {
  27. // buffer not large enough to contain ethernet
  28. // and ip headers. further action would overrun buf
  29. return;
  30. }
  31. struct ip *ip_hdr = (struct ip *) &bytes[(zconf.send_ip_pkts ? 0 : sizeof(struct ether_header))];
  32. uint32_t src_ip = ip_hdr->ip_src.s_addr;
  33. uint32_t validation[VALIDATE_BYTES/sizeof(uint8_t)];
  34. // TODO: for TTL exceeded messages, ip_hdr->saddr is going to be different
  35. // and we must calculate off potential payload message instead
  36. validate_gen(ip_hdr->ip_dst.s_addr, ip_hdr->ip_src.s_addr, (uint8_t *) validation);
  37. if (!zconf.probe_module->validate_packet(ip_hdr, buflen - (zconf.send_ip_pkts ? 0 : sizeof(struct ether_header)),
  38. &src_ip, validation)) {
  39. return;
  40. }
  41. int is_repeat = pbm_check(seen, ntohl(src_ip));
  42. fieldset_t *fs = fs_new_fieldset();
  43. fs_add_ip_fields(fs, ip_hdr);
  44. // HACK:
  45. // probe modules (for whatever reason) expect the full ethernet frame
  46. // in process_packet. For VPN, we only get back an IP frame.
  47. // Here, we fake an ethernet frame (which is initialized to
  48. // have ETH_P_IP proto and 00s for dest/src).
  49. if (zconf.send_ip_pkts) {
  50. if (buflen > sizeof(fake_eth_hdr)) {
  51. buflen = sizeof(fake_eth_hdr);
  52. }
  53. memcpy(&fake_eth_hdr[sizeof(struct ether_header)], bytes, buflen);
  54. bytes = fake_eth_hdr;
  55. }
  56. zconf.probe_module->process_packet(bytes, buflen, fs);
  57. fs_add_system_fields(fs, is_repeat, zsend.complete);
  58. int success_index = zconf.fsconf.success_index;
  59. assert(success_index < fs->len);
  60. int is_success = fs_get_uint64_by_index(fs, success_index);
  61. if (is_success) {
  62. zrecv.success_total++;
  63. if (!is_repeat) {
  64. zrecv.success_unique++;
  65. pbm_set(seen, ntohl(src_ip));
  66. }
  67. if (zsend.complete) {
  68. zrecv.cooldown_total++;
  69. if (!is_repeat) {
  70. zrecv.cooldown_unique++;
  71. }
  72. }
  73. } else {
  74. zrecv.failure_total++;
  75. }
  76. // probe module includes app_success field
  77. if (zconf.fsconf.app_success_index >= 0) {
  78. int is_app_success = fs_get_uint64_by_index(fs,
  79. zconf.fsconf.app_success_index);
  80. if (is_app_success) {
  81. zrecv.app_success_total++;
  82. if (!is_repeat) {
  83. zrecv.app_success_unique++;
  84. }
  85. }
  86. }
  87. fieldset_t *o = NULL;
  88. // we need to translate the data provided by the probe module
  89. // into a fieldset that can be used by the output module
  90. if (!is_success && zconf.filter_unsuccessful) {
  91. goto cleanup;
  92. }
  93. if (is_repeat && zconf.filter_duplicates) {
  94. goto cleanup;
  95. }
  96. if (!evaluate_expression(zconf.filter.expression, fs)) {
  97. goto cleanup;
  98. }
  99. o = translate_fieldset(fs, &zconf.fsconf.translation);
  100. if (zconf.output_module && zconf.output_module->process_ip) {
  101. zconf.output_module->process_ip(o);
  102. }
  103. cleanup:
  104. fs_free(fs);
  105. free(o);
  106. if (zconf.output_module && zconf.output_module->update
  107. && !(zrecv.success_unique % zconf.output_module->update_interval)) {
  108. zconf.output_module->update(&zconf, &zsend, &zrecv);
  109. }
  110. }
  111. int recv_run(pthread_mutex_t *recv_ready_mutex)
  112. {
  113. log_trace("recv", "recv thread started");
  114. log_debug("recv", "capturing responses on %s", zconf.iface);
  115. if (!zconf.dryrun) {
  116. recv_init();
  117. }
  118. if (zconf.send_ip_pkts) {
  119. struct ether_header *eth = (struct ether_header *) fake_eth_hdr;
  120. memset(fake_eth_hdr, 0, sizeof(fake_eth_hdr));
  121. eth->ether_type = htons(ETHERTYPE_IP);
  122. }
  123. // initialize paged bitmap
  124. seen = pbm_init();
  125. if (zconf.filter_duplicates) {
  126. log_debug("recv", "duplicate responses will be excluded from output");
  127. } else {
  128. log_debug("recv", "duplicate responses will be included in output");
  129. }
  130. if (zconf.filter_unsuccessful) {
  131. log_debug("recv", "unsuccessful responses will be excluded from output");
  132. } else {
  133. log_debug("recv", "unsuccessful responses will be included in output");
  134. }
  135. pthread_mutex_lock(recv_ready_mutex);
  136. zconf.recv_ready = 1;
  137. pthread_mutex_unlock(recv_ready_mutex);
  138. zrecv.start = now();
  139. if (zconf.max_results == 0) {
  140. zconf.max_results = -1;
  141. }
  142. do {
  143. if (zconf.dryrun) {
  144. sleep(1);
  145. } else {
  146. recv_packets();
  147. if (zconf.max_results && zrecv.success_unique >= zconf.max_results) {
  148. break;
  149. }
  150. }
  151. } while (!(zsend.complete && (now()-zsend.finish > zconf.cooldown_secs)));
  152. zrecv.finish = now();
  153. // get final pcap statistics before closing
  154. recv_update_stats();
  155. if (!zconf.dryrun) {
  156. pthread_mutex_lock(recv_ready_mutex);
  157. recv_cleanup();
  158. pthread_mutex_unlock(recv_ready_mutex);
  159. }
  160. zrecv.complete = 1;
  161. log_debug("recv", "thread finished");
  162. return 0;
  163. }