send.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 "send.h"
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <stdint.h>
  12. #include <time.h>
  13. #include <unistd.h>
  14. #include <pthread.h>
  15. #include <string.h>
  16. #include <errno.h>
  17. #include <assert.h>
  18. #include "../lib/includes.h"
  19. #include "../lib/logger.h"
  20. #include "../lib/random.h"
  21. #include "../lib/blacklist.h"
  22. #include "../lib/lockfd.h"
  23. #include "aesrand.h"
  24. #include "get_gateway.h"
  25. #include "iterator.h"
  26. #include "probe_modules/packet.h"
  27. #include "probe_modules/probe_modules.h"
  28. #include "shard.h"
  29. #include "state.h"
  30. #include "validate.h"
  31. // OS specific functions called by send_run
  32. static inline int send_packet(sock_t sock, void *buf, int len, uint32_t idx);
  33. static inline int send_run_init(sock_t sock);
  34. // Include the right implementations
  35. #if defined(PFRING)
  36. #include "send-pfring.h"
  37. #elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
  38. #include "send-bsd.h"
  39. #else /* LINUX */
  40. #include "send-linux.h"
  41. #endif /* __APPLE__ || __FreeBSD__ || __NetBSD__ */
  42. // The iterator over the cyclic group
  43. // Lock for send run
  44. static pthread_mutex_t send_mutex = PTHREAD_MUTEX_INITIALIZER;
  45. // Source IP address for outgoing packets
  46. static in_addr_t srcip_first;
  47. static in_addr_t srcip_last;
  48. static uint32_t srcip_offset;
  49. static uint32_t num_src_addrs;
  50. // Source ports for outgoing packets
  51. static uint16_t num_src_ports;
  52. // global sender initialize (not thread specific)
  53. iterator_t* send_init(void)
  54. {
  55. // generate a new primitive root and starting position
  56. iterator_t *it;
  57. it = iterator_init(zconf.senders, zconf.shard_num, zconf.total_shards);
  58. // process the dotted-notation addresses passed to ZMAP and determine
  59. // the source addresses from which we'll send packets;
  60. srcip_first = inet_addr(zconf.source_ip_first);
  61. if (srcip_first == INADDR_NONE) {
  62. log_fatal("send", "invalid begin source ip address: `%s'",
  63. zconf.source_ip_first);
  64. }
  65. srcip_last = inet_addr(zconf.source_ip_last);
  66. if (srcip_last == INADDR_NONE) {
  67. log_fatal("send", "invalid end source ip address: `%s'",
  68. zconf.source_ip_last);
  69. }
  70. log_debug("send", "srcip_first: %u", srcip_first);
  71. log_debug("send", "srcip_last: %u", srcip_last);
  72. if (srcip_first == srcip_last) {
  73. srcip_offset = 0;
  74. num_src_addrs = 1;
  75. } else {
  76. uint32_t ip_first = ntohl(srcip_first);
  77. uint32_t ip_last = ntohl(srcip_last);
  78. assert(ip_first && ip_last);
  79. assert(ip_last > ip_first);
  80. uint32_t offset = (uint32_t) (aesrand_getword(zconf.aes) & 0xFFFFFFFF);
  81. srcip_offset = offset % (srcip_last - srcip_first);
  82. num_src_addrs = ip_last - ip_first + 1;
  83. }
  84. // process the source port range that ZMap is allowed to use
  85. num_src_ports = zconf.source_port_last - zconf.source_port_first + 1;
  86. log_debug("send", "will send from %i address%s on %u source ports",
  87. num_src_addrs, ((num_src_addrs ==1 ) ? "":"es"),
  88. num_src_ports);
  89. // global initialization for send module
  90. assert(zconf.probe_module);
  91. if (zconf.probe_module->global_initialize) {
  92. zconf.probe_module->global_initialize(&zconf);
  93. }
  94. // concert specified bandwidth to packet rate
  95. if (zconf.bandwidth > 0) {
  96. int pkt_len = zconf.probe_module->packet_length;
  97. pkt_len *= 8;
  98. pkt_len += 8*24; // 7 byte MAC preamble, 1 byte Start frame,
  99. // 4 byte CRC, 12 byte inter-frame gap
  100. if (pkt_len < 84*8) {
  101. pkt_len = 84*8;
  102. }
  103. if (zconf.bandwidth / pkt_len > 0xFFFFFFFF) {
  104. zconf.rate = 0;
  105. } else {
  106. zconf.rate = zconf.bandwidth / pkt_len;
  107. if (zconf.rate == 0) {
  108. log_warn("send", "bandwidth %lu bit/s is slower than 1 pkt/s, "
  109. "setting rate to 1 pkt/s", zconf.bandwidth);
  110. zconf.rate = 1;
  111. }
  112. }
  113. log_debug("send", "using bandwidth %lu bits/s, rate set to %d pkt/s",
  114. zconf.bandwidth, zconf.rate);
  115. }
  116. // Get the source hardware address, and give it to the probe
  117. // module
  118. if (!zconf.hw_mac_set) {
  119. if (get_iface_hw_addr(zconf.iface, zconf.hw_mac)) {
  120. log_fatal("send", "could not retrieve hardware address for "
  121. "interface: %s", zconf.iface);
  122. return NULL;
  123. }
  124. log_debug("send", "no source MAC provided. "
  125. "automatically detected %02x:%02x:%02x:%02x:%02x:%02x as hw "
  126. "interface for %s",
  127. zconf.hw_mac[0], zconf.hw_mac[1], zconf.hw_mac[2],
  128. zconf.hw_mac[3], zconf.hw_mac[4], zconf.hw_mac[5],
  129. zconf.iface);
  130. }
  131. log_debug("send", "source MAC address %02x:%02x:%02x:%02x:%02x:%02x",
  132. zconf.hw_mac[0], zconf.hw_mac[1], zconf.hw_mac[2],
  133. zconf.hw_mac[3], zconf.hw_mac[4], zconf.hw_mac[5]);
  134. if (zconf.dryrun) {
  135. log_info("send", "dryrun mode -- won't actually send packets");
  136. }
  137. // initialize random validation key
  138. validate_init();
  139. zsend.start = now();
  140. return it;
  141. }
  142. static inline ipaddr_n_t get_src_ip(ipaddr_n_t dst, int local_offset)
  143. {
  144. if (srcip_first == srcip_last) {
  145. return srcip_first;
  146. }
  147. return htonl(((ntohl(dst) + srcip_offset + local_offset)
  148. % num_src_addrs)) + srcip_first;
  149. }
  150. // one sender thread
  151. int send_run(sock_t st, shard_t *s)
  152. {
  153. log_trace("send", "send thread started");
  154. pthread_mutex_lock(&send_mutex);
  155. // Allocate a buffer to hold the outgoing packet
  156. char buf[MAX_PACKET_SIZE];
  157. memset(buf, 0, MAX_PACKET_SIZE);
  158. // OS specific per-thread init
  159. if (send_run_init(st)) {
  160. return -1;
  161. }
  162. // MAC address length in characters
  163. char mac_buf[(ETHER_ADDR_LEN * 2) + (ETHER_ADDR_LEN - 1) + 1];
  164. char *p = mac_buf;
  165. for(int i=0; i < ETHER_ADDR_LEN; i++) {
  166. if (i == ETHER_ADDR_LEN-1) {
  167. snprintf(p, 3, "%.2x", zconf.hw_mac[i]);
  168. p += 2;
  169. } else {
  170. snprintf(p, 4, "%.2x:", zconf.hw_mac[i]);
  171. p += 3;
  172. }
  173. }
  174. log_debug("send", "source MAC address %s",
  175. mac_buf);
  176. void *probe_data;
  177. if (zconf.probe_module->thread_initialize) {
  178. zconf.probe_module->thread_initialize(buf, zconf.hw_mac, zconf.gw_mac,
  179. zconf.target_port, &probe_data);
  180. }
  181. pthread_mutex_unlock(&send_mutex);
  182. // adaptive timing to hit target rate
  183. uint32_t count = 0;
  184. uint32_t last_count = count;
  185. double last_time = now();
  186. uint32_t delay = 0;
  187. int interval = 0;
  188. uint32_t max_targets = s->state.max_targets;
  189. volatile int vi;
  190. struct timespec ts, rem;
  191. double send_rate = (double) zconf.rate / zconf.senders;
  192. double slow_rate = 50; // packets per seconds per thread at which it uses the slow methos
  193. long nsec_per_sec = 1000 * 1000 * 1000;
  194. long long sleep_time = nsec_per_sec;
  195. if (zconf.rate > 0) {
  196. delay = 10000;
  197. if (send_rate < slow_rate) {
  198. // set the inital time difference
  199. sleep_time = nsec_per_sec / send_rate;
  200. last_time = now() - (1.0 / send_rate);
  201. } else {
  202. // estimate initial rate
  203. for (vi = delay; vi--; )
  204. ;
  205. delay *= 1 / (now() - last_time) / (zconf.rate / zconf.senders);
  206. interval = (zconf.rate / zconf.senders) / 20;
  207. last_time = now();
  208. }
  209. }
  210. uint32_t curr = shard_get_cur_ip(s);
  211. int attempts = zconf.num_retries + 1;
  212. uint32_t idx = 0;
  213. while (1) {
  214. // adaptive timing delay
  215. if (delay > 0) {
  216. count++;
  217. if (send_rate < slow_rate) {
  218. double t = now();
  219. double last_rate = (1.0 / (t - last_time));
  220. sleep_time *= ((last_rate / send_rate) + 1) / 2;
  221. ts.tv_sec = sleep_time / nsec_per_sec;
  222. ts.tv_nsec = sleep_time % nsec_per_sec;
  223. log_debug("sleep", "sleep for %d sec, %ld nanoseconds", ts.tv_sec, ts.tv_nsec);
  224. while (nanosleep(&ts, &rem) == -1) {}
  225. last_time = t;
  226. } else {
  227. for (vi = delay; vi--; )
  228. ;
  229. if (!interval || (count % interval == 0)) {
  230. double t = now();
  231. delay *= (double)(count - last_count)
  232. / (t - last_time) / (zconf.rate / zconf.senders);
  233. if (delay < 1)
  234. delay = 1;
  235. last_count = count;
  236. last_time = t;
  237. }
  238. }
  239. }
  240. if (zrecv.complete) {
  241. s->cb(s->id, s->arg);
  242. break;
  243. }
  244. if (s->state.sent >= max_targets) {
  245. s->cb(s->id, s->arg);
  246. log_trace("send", "send thread %hhu finished (max targets of %u reached)", s->id, max_targets);
  247. break;
  248. }
  249. if (zconf.max_runtime && zconf.max_runtime <= now() - zsend.start) {
  250. s->cb(s->id, s->arg);
  251. break;
  252. }
  253. if (curr == 0) {
  254. s->cb(s->id, s->arg);
  255. log_trace("send", "send thread %hhu finished, shard depleted", s->id);
  256. break;
  257. }
  258. s->state.sent++;
  259. for (int i=0; i < zconf.packet_streams; i++) {
  260. uint32_t src_ip = get_src_ip(curr, i);
  261. uint32_t validation[VALIDATE_BYTES/sizeof(uint32_t)];
  262. validate_gen(src_ip, curr, (uint8_t *)validation);
  263. zconf.probe_module->make_packet(buf, src_ip, curr, validation, i, probe_data);
  264. if (zconf.dryrun) {
  265. lock_file(stdout);
  266. zconf.probe_module->print_packet(stdout, buf);
  267. unlock_file(stdout);
  268. } else {
  269. int length = zconf.probe_module->packet_length;
  270. void *contents = buf + zconf.send_ip_pkts*sizeof(struct ether_header);
  271. for (int i = 0; i < attempts; ++i) {
  272. int rc = send_packet(st, contents, length, idx);
  273. if (rc < 0) {
  274. struct in_addr addr;
  275. addr.s_addr = curr;
  276. log_debug("send", "send_packet failed for %s. %s",
  277. inet_ntoa(addr), strerror(errno));
  278. s->state.failures++;
  279. } else {
  280. break;
  281. }
  282. }
  283. idx++;
  284. idx &= 0xFF;
  285. }
  286. }
  287. curr = shard_get_next_ip(s);
  288. }
  289. if (zconf.dryrun) {
  290. lock_file(stdout);
  291. fflush(stdout);
  292. unlock_file(stdout);
  293. }
  294. log_debug("send", "thread %hu finished", s->id);
  295. return EXIT_SUCCESS;
  296. }