PageRenderTime 33ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/FPEngine.cc

https://github.com/prakashgamit/nmap
C++ | 2693 lines | 1584 code | 372 blank | 737 comment | 494 complexity | e408f849bd72ee4e7f09dd652d820b7a MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-2.0, LGPL-2.1
  1. /***************************************************************************
  2. * FPEngine.cc -- Routines used for IPv6 OS detection via TCP/IP *
  3. * fingerprinting. * For more information on how this works in Nmap, see *
  4. * http://nmap.org/osdetect/ *
  5. * *
  6. ***********************IMPORTANT NMAP LICENSE TERMS************************
  7. * *
  8. * The Nmap Security Scanner is (C) 1996-2013 Insecure.Com LLC. Nmap is *
  9. * also a registered trademark of Insecure.Com LLC. This program is free *
  10. * software; you may redistribute and/or modify it under the terms of the *
  11. * GNU General Public License as published by the Free Software *
  12. * Foundation; Version 2 ("GPL"), BUT ONLY WITH ALL OF THE CLARIFICATIONS *
  13. * AND EXCEPTIONS DESCRIBED HEREIN. This guarantees your right to use, *
  14. * modify, and redistribute this software under certain conditions. If *
  15. * you wish to embed Nmap technology into proprietary software, we sell *
  16. * alternative licenses (contact sales@nmap.com). Dozens of software *
  17. * vendors already license Nmap technology such as host discovery, port *
  18. * scanning, OS detection, version detection, and the Nmap Scripting *
  19. * Engine. *
  20. * *
  21. * Note that the GPL places important restrictions on "derivative works", *
  22. * yet it does not provide a detailed definition of that term. To avoid *
  23. * misunderstandings, we interpret that term as broadly as copyright law *
  24. * allows. For example, we consider an application to constitute a *
  25. * derivative work for the purpose of this license if it does any of the *
  26. * following with any software or content covered by this license *
  27. * ("Covered Software"): *
  28. * *
  29. * o Integrates source code from Covered Software. *
  30. * *
  31. * o Reads or includes copyrighted data files, such as Nmap's nmap-os-db *
  32. * or nmap-service-probes. *
  33. * *
  34. * o Is designed specifically to execute Covered Software and parse the *
  35. * results (as opposed to typical shell or execution-menu apps, which will *
  36. * execute anything you tell them to). *
  37. * *
  38. * o Includes Covered Software in a proprietary executable installer. The *
  39. * installers produced by InstallShield are an example of this. Including *
  40. * Nmap with other software in compressed or archival form does not *
  41. * trigger this provision, provided appropriate open source decompression *
  42. * or de-archiving software is widely available for no charge. For the *
  43. * purposes of this license, an installer is considered to include Covered *
  44. * Software even if it actually retrieves a copy of Covered Software from *
  45. * another source during runtime (such as by downloading it from the *
  46. * Internet). *
  47. * *
  48. * o Links (statically or dynamically) to a library which does any of the *
  49. * above. *
  50. * *
  51. * o Executes a helper program, module, or script to do any of the above. *
  52. * *
  53. * This list is not exclusive, but is meant to clarify our interpretation *
  54. * of derived works with some common examples. Other people may interpret *
  55. * the plain GPL differently, so we consider this a special exception to *
  56. * the GPL that we apply to Covered Software. Works which meet any of *
  57. * these conditions must conform to all of the terms of this license, *
  58. * particularly including the GPL Section 3 requirements of providing *
  59. * source code and allowing free redistribution of the work as a whole. *
  60. * *
  61. * As another special exception to the GPL terms, Insecure.Com LLC grants *
  62. * permission to link the code of this program with any version of the *
  63. * OpenSSL library which is distributed under a license identical to that *
  64. * listed in the included docs/licenses/OpenSSL.txt file, and distribute *
  65. * linked combinations including the two. *
  66. * *
  67. * Any redistribution of Covered Software, including any derived works, *
  68. * must obey and carry forward all of the terms of this license, including *
  69. * obeying all GPL rules and restrictions. For example, source code of *
  70. * the whole work must be provided and free redistribution must be *
  71. * allowed. All GPL references to "this License", are to be treated as *
  72. * including the terms and conditions of this license text as well. *
  73. * *
  74. * Because this license imposes special exceptions to the GPL, Covered *
  75. * Work may not be combined (even as part of a larger work) with plain GPL *
  76. * software. The terms, conditions, and exceptions of this license must *
  77. * be included as well. This license is incompatible with some other open *
  78. * source licenses as well. In some cases we can relicense portions of *
  79. * Nmap or grant special permissions to use it in other open source *
  80. * software. Please contact fyodor@nmap.org with any such requests. *
  81. * Similarly, we don't incorporate incompatible open source software into *
  82. * Covered Software without special permission from the copyright holders. *
  83. * *
  84. * If you have any questions about the licensing restrictions on using *
  85. * Nmap in other works, are happy to help. As mentioned above, we also *
  86. * offer alternative license to integrate Nmap into proprietary *
  87. * applications and appliances. These contracts have been sold to dozens *
  88. * of software vendors, and generally include a perpetual license as well *
  89. * as providing for priority support and updates. They also fund the *
  90. * continued development of Nmap. Please email sales@nmap.com for further *
  91. * information. *
  92. * *
  93. * If you have received a written license agreement or contract for *
  94. * Covered Software stating terms other than these, you may choose to use *
  95. * and redistribute Covered Software under those terms instead of these. *
  96. * *
  97. * Source is provided to this software because we believe users have a *
  98. * right to know exactly what a program is going to do before they run it. *
  99. * This also allows you to audit the software for security holes (none *
  100. * have been found so far). *
  101. * *
  102. * Source code also allows you to port Nmap to new platforms, fix bugs, *
  103. * and add new features. You are highly encouraged to send your changes *
  104. * to the dev@nmap.org mailing list for possible incorporation into the *
  105. * main distribution. By sending these changes to Fyodor or one of the *
  106. * Insecure.Org development mailing lists, or checking them into the Nmap *
  107. * source code repository, it is understood (unless you specify otherwise) *
  108. * that you are offering the Nmap Project (Insecure.Com LLC) the *
  109. * unlimited, non-exclusive right to reuse, modify, and relicense the *
  110. * code. Nmap will always be available Open Source, but this is important *
  111. * because the inability to relicense code has caused devastating problems *
  112. * for other Free Software projects (such as KDE and NASM). We also *
  113. * occasionally relicense the code to third parties as discussed above. *
  114. * If you wish to specify special license conditions of your *
  115. * contributions, just say so when you send them. *
  116. * *
  117. * This program is distributed in the hope that it will be useful, but *
  118. * WITHOUT ANY WARRANTY; without even the implied warranty of *
  119. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Nmap *
  120. * license file for more details (it's in a COPYING file included with *
  121. * Nmap, and also available from https://svn.nmap.org/nmap/COPYING *
  122. * *
  123. ***************************************************************************/
  124. /* $Id$ */
  125. #include "FPEngine.h"
  126. #include "Target.h"
  127. #include "NmapOps.h"
  128. #include "nmap_error.h"
  129. #include "osscan.h"
  130. #include "libnetutil/npacket.h"
  131. #include "linear.h"
  132. extern NmapOps o;
  133. /******************************************************************************
  134. * Globals. *
  135. ******************************************************************************/
  136. /* This is the global network controller. FPHost classes use it to request
  137. * network resources and schedule packet transmissions. */
  138. FPNetworkControl global_netctl;
  139. /******************************************************************************
  140. * Implementation of class FPNetworkControl. *
  141. ******************************************************************************/
  142. FPNetworkControl::FPNetworkControl() {
  143. memset(&this->nsp, 0, sizeof(nsock_pool));
  144. memset(&this->pcap_nsi, 0, sizeof(pcap_nsi));
  145. memset(&this->pcap_ev_id, 0, sizeof(nsock_event_id));
  146. this->nsock_init = false;
  147. this->rawsd = -1;
  148. this->probes_sent = 0;
  149. this->responses_recv = 0;
  150. this->probes_timedout = 0;
  151. this->cc_cwnd = 0;
  152. this->cc_ssthresh = 0;
  153. }
  154. FPNetworkControl::~FPNetworkControl() {
  155. if (this->nsock_init) {
  156. nsock_event_cancel(this->nsp, this->pcap_ev_id, 0);
  157. nsp_delete(this->nsp);
  158. this->nsock_init = false;
  159. }
  160. }
  161. /* (Re)-Initialize object's state (default parameter setup and nsock
  162. * initialization). */
  163. void FPNetworkControl::init(const char *ifname, devtype iftype) {
  164. /* Init congestion control parameters */
  165. this->cc_init();
  166. /* If there was a previous nsock pool, delete it */
  167. if (this->pcap_nsi) {
  168. nsi_delete(this->pcap_nsi, NSOCK_PENDING_SILENT);
  169. }
  170. if (this->nsock_init) {
  171. nsock_event_cancel(this->nsp, this->pcap_ev_id, 0);
  172. nsp_delete(this->nsp);
  173. }
  174. /* Create a new nsock pool */
  175. if ((this->nsp = nsp_new(NULL)) == NULL)
  176. fatal("Unable to obtain an Nsock pool");
  177. nsock_set_log_function(this->nsp, nmap_nsock_stderr_logger);
  178. nmap_adjust_loglevel(this->nsp, o.packetTrace());
  179. nsp_setdevice(nsp, o.device);
  180. if (o.proxy_chain)
  181. nsp_set_proxychain(this->nsp, o.proxy_chain);
  182. /* Allow broadcast addresses */
  183. nsp_setbroadcast(this->nsp, 1);
  184. /* Allocate an NSI for packet capture */
  185. this->pcap_nsi = nsi_new(this->nsp, NULL);
  186. this->first_pcap_scheduled = false;
  187. /* Flag it as already initialized so we free this nsp next time */
  188. this->nsock_init = true;
  189. /* Obtain raw socket or check that we can obtain an eth descriptor. */
  190. if ((o.sendpref & PACKET_SEND_ETH) && iftype == devt_ethernet && ifname != NULL) {
  191. /* We don't need to store the eth handler because FPProbes come with a
  192. * suitable one (FPProbes::getEthernet()), we just attempt to obtain one
  193. * to see if it fails. */
  194. if (eth_open_cached(ifname) == NULL)
  195. fatal("dnet: failed to open device %s", ifname);
  196. this->rawsd = -1;
  197. } else {
  198. #ifdef WIN32
  199. win32_fatal_raw_sockets(ifname);
  200. #endif
  201. if (this->rawsd >= 0)
  202. close(this->rawsd);
  203. rawsd = nmap_raw_socket();
  204. if (rawsd < 0)
  205. pfatal("Couldn't obtain raw socket in %s", __func__);
  206. }
  207. /* De-register existing callers */
  208. while (this->callers.size() > 0) {
  209. this->callers.pop_back();
  210. }
  211. return;
  212. }
  213. /* This function initializes the controller's congestion control parameters.
  214. * The network controller uses TCP's Slow Start and Congestion Avoidance
  215. * algorithms from RFC 5681 (slightly modified for convenience).
  216. *
  217. * As the OS detection process does not open full TCP connections, we can't just
  218. * use ACKs (or the lack of ACKs) to increase or decrease the congestion window
  219. * so we use probe responses. Every time we get a response to an OS detection
  220. * probe, we treat it as if it was a TCP ACK in TCP's congestion control.
  221. *
  222. * Note that the initial Congestion Window is set to the number of timed
  223. * probes that we send to each target. This is necessary since we need to
  224. * know for sure that we can send that many packets in order to transmit them.
  225. * Otherwise, we could fail to deliver the probes 100ms apart. */
  226. int FPNetworkControl::cc_init() {
  227. this->probes_sent = 0;
  228. this->responses_recv = 0;
  229. this->probes_timedout = 0;
  230. this->cc_cwnd = OSSCAN_INITIAL_CWND;
  231. this->cc_ssthresh = OSSCAN_INITIAL_SSTHRESH;
  232. return OP_SUCCESS;
  233. }
  234. /* This method is used to indicate that we have scheduled the transmission of
  235. * one or more packets. This is used in congestion control to determine the
  236. * number of outstanding probes (number of probes sent but not answered yet)
  237. * and therefore, the effective transmission window. @param pkts indicates the
  238. * number of packets that were scheduled. Returns OP_SUCCESS on success and
  239. * OP_FAILURE in case of error. */
  240. int FPNetworkControl::cc_update_sent(int pkts = 1) {
  241. if (pkts <= 0)
  242. return OP_FAILURE;
  243. this->probes_sent+=pkts;
  244. return OP_SUCCESS;
  245. }
  246. /* This method is used to indicate that a drop has occurred. In TCP, drops are
  247. * detected by the absence of an ACK. However, we can't use that, since it is
  248. * very likely that our targets do not respond to some of our OS detection
  249. * probes intentionally. For this reason, we consider that a drop has occurred
  250. * when we receive a response for a probe that has already suffered one
  251. * retransmission (first transmission got dropped in transit, some later
  252. * transmission made it to the host and it responded). So when we detect a drop
  253. * we do the same as TCP, adjust the congestion window and the slow start
  254. * threshold. */
  255. int FPNetworkControl::cc_report_drop() {
  256. /* FROM RFC 5681
  257. When a TCP sender detects segment loss using the retransmission timer
  258. and the given segment has not yet been resent by way of the
  259. retransmission timer, the value of ssthresh MUST be set to no more
  260. than the value given in equation (4):
  261. ssthresh = max (FlightSize / 2, 2*SMSS) (4)
  262. where, as discussed above, FlightSize is the amount of outstanding
  263. data in the network.
  264. On the other hand, when a TCP sender detects segment loss using the
  265. retransmission timer and the given segment has already been
  266. retransmitted by way of the retransmission timer at least once, the
  267. value of ssthresh is held constant.
  268. */
  269. int probes_outstanding = this->probes_sent - this->responses_recv - this->probes_timedout;
  270. this->cc_ssthresh = MAX(probes_outstanding, OSSCAN_INITIAL_CWND);
  271. this->cc_cwnd = OSSCAN_INITIAL_CWND;
  272. return OP_SUCCESS;
  273. }
  274. /* This method is used to indicate that a response to a previous probe was
  275. * received. For us this is like getting and ACK in TCP congestion control, so
  276. * we update the congestion window (increase by one packet if we are in slow
  277. * start or increase it by a small percentage of a packet if we are in
  278. * congestion avoidance). */
  279. int FPNetworkControl::cc_update_received() {
  280. this->responses_recv++;
  281. /* If we are in Slow Start, increment congestion window by one packet.
  282. * (Note that we treat probe responses the same way TCP CC treats ACKs). */
  283. if (this->cc_cwnd < this->cc_ssthresh) {
  284. this->cc_cwnd += 1;
  285. /* Otherwise we are in Congestion Avoidance and CWND is incremented slowly,
  286. * approximately one packet per RTT */
  287. } else {
  288. this->cc_cwnd = this->cc_cwnd + 1/this->cc_cwnd;
  289. }
  290. if (o.debugging > 3) {
  291. log_write(LOG_PLAIN, "[FPNetworkControl] Congestion Control Parameters: cwnd=%f ssthresh=%f sent=%d recv=%d tout=%d outstanding=%d\n",
  292. this->cc_cwnd, this->cc_ssthresh, this->probes_sent, this->responses_recv, this->probes_timedout,
  293. this->probes_sent - this->responses_recv - this->probes_timedout);
  294. }
  295. return OP_SUCCESS;
  296. }
  297. /* This method is public and can be called by FPHosts to inform the controller
  298. * that a probe has experienced a final timeout. In other words, that no
  299. * response was received for the probe after doing the necessary retransmissions
  300. * and waiting for the RTO. This is used to decrease the number of outstanding
  301. * probes. Otherwise, if no host responded to the probes, the effective
  302. * transmission window could reach zero and prevent new probes from being sent,
  303. * clogging the engine. */
  304. int FPNetworkControl::cc_report_final_timeout() {
  305. this->probes_timedout++;
  306. return OP_SUCCESS;
  307. }
  308. /* This method is used by FPHosts to request permission to transmit a number of
  309. * probes. Permission is granted if the current congestion window allows the
  310. * transmission of new probes. It returns true if permission is granted and
  311. * false if it is denied. */
  312. bool FPNetworkControl::request_slots(size_t num_packets) {
  313. int probes_outstanding = this->probes_sent - this->responses_recv - this->probes_timedout;
  314. if (o.debugging > 3)
  315. log_write(LOG_PLAIN, "[FPNetworkControl] Slot request for %u packets. ProbesOutstanding=%d cwnd=%f ssthresh=%f\n",
  316. (unsigned int)num_packets, probes_outstanding, this->cc_cwnd, this->cc_ssthresh);
  317. /* If we still have room for more outstanding probes, let the caller
  318. * schedule transmissions. */
  319. if ((probes_outstanding + num_packets) <= this->cc_cwnd) {
  320. this->cc_update_sent(num_packets);
  321. return true;
  322. }
  323. return false;
  324. }
  325. /* This method lets FPHosts register themselves in the network controller so
  326. * the controller can call them back every time a packet they are interested
  327. * in is captured.*/
  328. int FPNetworkControl::register_caller(FPHost *newcaller) {
  329. this->callers.push_back(newcaller);
  330. return OP_SUCCESS;
  331. }
  332. /* This method lets FPHosts unregister themselves in the network controller so
  333. * the controller does not call them back again. This is called by hosts that
  334. * have already finished their OS detection. */
  335. int FPNetworkControl::unregister_caller(FPHost *oldcaller) {
  336. for (size_t i = 0; i < this->callers.size(); i++) {
  337. if (this->callers[i] == oldcaller) {
  338. this->callers.erase(this->callers.begin() + i);
  339. return OP_SUCCESS;
  340. }
  341. }
  342. return OP_FAILURE;
  343. }
  344. /* This method gets the controller ready for packet capture. Basically it
  345. * obtains a pcap descriptor from nsock and sets an appropriate BPF filter. */
  346. int FPNetworkControl::setup_sniffer(const char *iface, const char *bpf_filter) {
  347. char pcapdev[128];
  348. int rc;
  349. #ifdef WIN32
  350. /* Nmap normally uses device names obtained through dnet for interfaces, but
  351. Pcap has its own naming system. So the conversion is done here */
  352. if (!DnetName2PcapName(iface, pcapdev, sizeof(pcapdev))) {
  353. /* Oh crap -- couldn't find the corresponding dev apparently. Let's just go
  354. with what we have then ... */
  355. Strncpy(pcapdev, iface, sizeof(pcapdev));
  356. }
  357. #else
  358. Strncpy(pcapdev, iface, sizeof(pcapdev));
  359. #endif
  360. /* Obtain a pcap descriptor */
  361. rc = nsock_pcap_open(this->nsp, this->pcap_nsi, pcapdev, 8192, 0, bpf_filter);
  362. if (rc)
  363. fatal("Error opening capture device %s\n", pcapdev);
  364. /* Store the pcap NSI inside the pool so we can retrieve it inside a callback */
  365. nsp_setud(this->nsp, (void *)&(this->pcap_nsi));
  366. return OP_SUCCESS;
  367. }
  368. /* This method makes the controller process pending events (like packet
  369. * transmissions or packet captures). */
  370. void FPNetworkControl::handle_events() {
  371. nmap_adjust_loglevel(nsp, o.packetTrace());
  372. nsock_loop(nsp, 50);
  373. }
  374. /* This method lets FPHosts to schedule the transmission of an OS detection
  375. * probe. It takes an FPProbe pointer and the amount of milliseconds the
  376. * controller should wait before injecting the probe into the wire. */
  377. int FPNetworkControl::scheduleProbe(FPProbe *pkt, int in_msecs_time) {
  378. nsock_timer_create(this->nsp, probe_transmission_handler_wrapper, in_msecs_time, (void*)pkt);
  379. return OP_SUCCESS;
  380. }
  381. /* This is the handler for packet transmission. It is called by nsock whenever a timer expires,
  382. * which means that a new packet needs to be transmitted. Note that this method is not
  383. * called directly by Nsock but by the wrapper function probe_transmission_handler_wrapper().
  384. * The reason for that is because C++ does not allow to use class methods as callback
  385. * functions, so this is a small hack to make that happen. */
  386. void FPNetworkControl::probe_transmission_handler(nsock_pool nsp, nsock_event nse, void *arg) {
  387. assert(nsp_getud(nsp) != NULL);
  388. nsock_iod nsi_pcap = *((nsock_iod *)nsp_getud(nsp));
  389. enum nse_status status = nse_status(nse);
  390. enum nse_type type = nse_type(nse);
  391. FPProbe *myprobe = (FPProbe *)arg;
  392. u8 *buf;
  393. size_t len;
  394. if (status == NSE_STATUS_SUCCESS) {
  395. switch(type) {
  396. /* Timer events mean that we need to send a packet. */
  397. case NSE_TYPE_TIMER:
  398. /* The first time a packet is sent, we schedule a pcap event. After that
  399. * we don't have to worry since the response reception handler schedules
  400. * a new capture event for each captured packet. */
  401. if (!this->first_pcap_scheduled) {
  402. this->pcap_ev_id = nsock_pcap_read_packet(nsp, nsi_pcap, response_reception_handler_wrapper, -1, NULL);
  403. this->first_pcap_scheduled = true;
  404. }
  405. buf = myprobe->getPacketBuffer(&len);
  406. /* Send the packet*/
  407. assert(myprobe->host != NULL);
  408. if (send_ip_packet(this->rawsd, myprobe->getEthernet(), myprobe->host->getTargetAddress(), buf, len) == -1) {
  409. pfatal("Unable to send packet in %s", __func__);
  410. }
  411. myprobe->setTimeSent();
  412. free(buf);
  413. break;
  414. default:
  415. fatal("Unexpected Nsock event in probe_transmission_handler()");
  416. break;
  417. } /* switch(type) */
  418. } else if (status == NSE_STATUS_EOF) {
  419. if (o.debugging)
  420. log_write(LOG_PLAIN, "probe_transmission_handler(): EOF\n");
  421. } else if (status == NSE_STATUS_ERROR || status == NSE_STATUS_PROXYERROR) {
  422. if (o.debugging)
  423. log_write(LOG_PLAIN, "probe_transmission_handler(): %s failed: %s\n", nse_type2str(type), strerror(socket_errno()));
  424. } else if (status == NSE_STATUS_TIMEOUT) {
  425. if (o.debugging)
  426. log_write(LOG_PLAIN, "probe_transmission_handler(): %s timeout: %s\n", nse_type2str(type), strerror(socket_errno()));
  427. } else if (status == NSE_STATUS_CANCELLED) {
  428. if (o.debugging)
  429. log_write(LOG_PLAIN, "probe_transmission_handler(): %s canceled: %s\n", nse_type2str(type), strerror(socket_errno()));
  430. } else if (status == NSE_STATUS_KILL) {
  431. if (o.debugging)
  432. log_write(LOG_PLAIN, "probe_transmission_handler(): %s killed: %s\n", nse_type2str(type), strerror(socket_errno()));
  433. } else {
  434. if (o.debugging)
  435. log_write(LOG_PLAIN, "probe_transmission_handler(): Unknown status code %d\n", status);
  436. }
  437. return;
  438. }
  439. /* This is the handler for packet capture. It is called by nsock whenever libpcap
  440. * captures a packet from the network interface. This method basically captures
  441. * the packet, extracts its source IP address and tries to find an FPHost that
  442. * is targeting such address. If it does, it passes the packet to that FPHost
  443. * via callback() so the FPHost can determine if the packet is actually the
  444. * response to a FPProbe that it sent before. Note that this method is not
  445. * called directly by Nsock but by the wrapper function
  446. * response_reception_handler_wrapper(). See doc in probe_transmission_handler()
  447. * for details. */
  448. void FPNetworkControl::response_reception_handler(nsock_pool nsp, nsock_event nse, void *arg) {
  449. nsock_iod nsi = nse_iod(nse);
  450. enum nse_status status = nse_status(nse);
  451. enum nse_type type = nse_type(nse);
  452. const u8 *rcvd_pkt = NULL; /* Points to the captured packet */
  453. size_t rcvd_pkt_len = 0; /* Lenght of the captured packet */
  454. struct timeval pcaptime; /* Time the packet was captured */
  455. struct sockaddr_storage sent_ss;
  456. struct sockaddr_storage rcvd_ss;
  457. struct sockaddr_in *rcvd_ss4 = (struct sockaddr_in *)&rcvd_ss;
  458. struct sockaddr_in6 *rcvd_ss6 = (struct sockaddr_in6 *)&rcvd_ss;
  459. memset(&rcvd_ss, 0, sizeof(struct sockaddr_storage));
  460. IPv4Header ip4;
  461. IPv6Header ip6;
  462. int res = -1;
  463. struct timeval tv;
  464. gettimeofday(&tv, NULL);
  465. if (status == NSE_STATUS_SUCCESS) {
  466. switch(type) {
  467. case NSE_TYPE_PCAP_READ:
  468. /* Schedule a new pcap read operation */
  469. this->pcap_ev_id = nsock_pcap_read_packet(nsp, nsi, response_reception_handler_wrapper, -1, NULL);
  470. /* Get captured packet */
  471. nse_readpcap(nse, NULL, NULL, &rcvd_pkt, &rcvd_pkt_len, NULL, &pcaptime);
  472. /* Extract the packet's source address */
  473. ip4.storeRecvData(rcvd_pkt, rcvd_pkt_len);
  474. if (ip4.validate() != OP_FAILURE && ip4.getVersion() == 4) {
  475. ip4.getSourceAddress(&(rcvd_ss4->sin_addr));
  476. rcvd_ss4->sin_family = AF_INET;
  477. } else {
  478. ip6.storeRecvData(rcvd_pkt, rcvd_pkt_len);
  479. if (ip6.validate() != OP_FAILURE && ip6.getVersion() == 6) {
  480. ip6.getSourceAddress(&(rcvd_ss6->sin6_addr));
  481. rcvd_ss6->sin6_family = AF_INET6;
  482. } else {
  483. /* If we get here it means that the received packet is not
  484. * IPv4 or IPv6 so we just discard it returning. */
  485. return;
  486. }
  487. }
  488. /* Check if we have a caller that expects packets from this sender */
  489. for (size_t i = 0; i < this->callers.size(); i++) {
  490. /* Obtain the target address */
  491. sent_ss = *this->callers[i]->getTargetAddress();
  492. /* Check that the received packet is of the same address family */
  493. if (sent_ss.ss_family != rcvd_ss.ss_family)
  494. continue;
  495. /* Check that the captured packet's source address matches the
  496. * target address. If it matches, pass the received packet
  497. * to the appropriate FPHost object through callback(). */
  498. if (sockaddr_storage_equal(&rcvd_ss, &sent_ss)) {
  499. if ((res = this->callers[i]->callback(rcvd_pkt, rcvd_pkt_len, &tv)) >= 0) {
  500. /* If callback() returns >=0 it means that the packet we've just
  501. * passed was successfully matched with a previous probe. Now
  502. * update the count of received packets (so we can determine how
  503. * many outstanding packets are out there). Note that we only do
  504. * that if callback() returned >0 because 0 is a special case: a
  505. * reply to a retransmitted timed probe that was already replied
  506. * to in the past. We don't want to count replies to the same probe
  507. * more than once, so that's why we only update when res > 0. */
  508. if (res > 0)
  509. this->cc_update_received();
  510. /* When the callback returns more than 1 it means that the packet
  511. * was sent more than once before being answered. This means that
  512. * we experienced congestion (first transmission got dropped), so
  513. * we update our CC parameters to deal with the congestion. */
  514. if (res > 1) {
  515. this->cc_report_drop();
  516. }
  517. }
  518. return;
  519. }
  520. }
  521. break;
  522. default:
  523. fatal("Unexpected Nsock event in response_reception_handler()");
  524. break;
  525. } /* switch(type) */
  526. } else if (status == NSE_STATUS_EOF) {
  527. if (o.debugging)
  528. log_write(LOG_PLAIN, "response_reception_handler(): EOF\n");
  529. } else if (status == NSE_STATUS_ERROR || NSE_STATUS_PROXYERROR) {
  530. if (o.debugging)
  531. log_write(LOG_PLAIN, "response_reception_handler(): %s failed: %s\n", nse_type2str(type), strerror(socket_errno()));
  532. } else if (status == NSE_STATUS_TIMEOUT) {
  533. if (o.debugging)
  534. log_write(LOG_PLAIN, "response_reception_handler(): %s timeout: %s\n", nse_type2str(type), strerror(socket_errno()));
  535. } else if (status == NSE_STATUS_CANCELLED) {
  536. if (o.debugging)
  537. log_write(LOG_PLAIN, "response_reception_handler(): %s canceled: %s\n", nse_type2str(type), strerror(socket_errno()));
  538. } else if (status == NSE_STATUS_KILL) {
  539. if (o.debugging)
  540. log_write(LOG_PLAIN, "response_reception_handler(): %s killed: %s\n", nse_type2str(type), strerror(socket_errno()));
  541. } else {
  542. if (o.debugging)
  543. log_write(LOG_PLAIN, "response_reception_handler(): Unknown status code %d\n", status);
  544. }
  545. return;
  546. }
  547. /******************************************************************************
  548. * Implementation of class FPEngine. *
  549. ******************************************************************************/
  550. FPEngine::FPEngine() {
  551. this->osgroup_size = OSSCAN_GROUP_SIZE;
  552. }
  553. FPEngine::~FPEngine() {
  554. }
  555. /* Returns a suitable BPF filter for the OS detection. If less than 20 targets
  556. * are passed, the filter contains an explicit list of target addresses. It
  557. * looks similar to this:
  558. *
  559. * dst host fe80::250:56ff:fec0:1 and (src host fe80::20c:29ff:feb0:2316 or src host fe80::20c:29ff:fe9f:5bc2)
  560. *
  561. * When more than 20 targets are passed, a generic filter based on the source
  562. * address is used. The returned filter looks something like:
  563. *
  564. * dst host fe80::250:56ff:fec0:1
  565. */
  566. const char *FPEngine::bpf_filter(std::vector<Target *> &Targets) {
  567. static char pcap_filter[2048];
  568. /* 20 IPv6 addresses is max (46 byte addy + 14 (" or src host ")) * 20 == 1200 */
  569. char dst_hosts[1220];
  570. int filterlen = 0;
  571. int len = 0;
  572. unsigned int targetno;
  573. memset(pcap_filter, 0, sizeof(pcap_filter));
  574. /* If we have 20 or less targets, build a list of addresses so we can set
  575. * an explicit BPF filter */
  576. if (Targets.size() <= 20) {
  577. for (targetno = 0; targetno < Targets.size(); targetno++) {
  578. len = Snprintf(dst_hosts + filterlen,
  579. sizeof(dst_hosts) - filterlen,
  580. "%ssrc host %s", (targetno == 0)? "" : " or ",
  581. Targets[targetno]->targetipstr());
  582. if (len < 0 || len + filterlen >= (int) sizeof(dst_hosts))
  583. fatal("ran out of space in dst_hosts");
  584. filterlen += len;
  585. }
  586. if (len < 0 || len + filterlen >= (int) sizeof(dst_hosts))
  587. fatal("ran out of space in dst_hosts");
  588. len = Snprintf(pcap_filter, sizeof(pcap_filter), "dst host %s and (%s)",
  589. Targets[0]->sourceipstr(), dst_hosts);
  590. } else {
  591. len = Snprintf(pcap_filter, sizeof(pcap_filter), "dst host %s", Targets[0]->sourceipstr());
  592. }
  593. if (len < 0 || len >= (int) sizeof(pcap_filter))
  594. fatal("ran out of space in pcap filter");
  595. return pcap_filter;
  596. }
  597. /******************************************************************************
  598. * Implementation of class FPEngine6. *
  599. ******************************************************************************/
  600. FPEngine6::FPEngine6() {
  601. }
  602. FPEngine6::~FPEngine6() {
  603. }
  604. /* From FPModel.cc. */
  605. extern struct model FPModel;
  606. extern double FPscale[][2];
  607. extern double FPmean[][659];
  608. extern double FPvariance[][659];
  609. extern FingerMatch FPmatches[];
  610. /* Not all operating systems allow setting the flow label in outgoing packets;
  611. notably all Unixes other than Linux when using raw sockets. This function
  612. finds out whether the flow labels we set are likely really being sent.
  613. Otherwise, the operating system is probably filling in 0. Compare to the
  614. logic in send_ipv6_packet_eth_or_sd. */
  615. static bool can_set_flow_label(const struct eth_nfo *eth) {
  616. if (eth != NULL)
  617. return true;
  618. #if HAVE_IPV6_IPPROTO_RAW
  619. return true;
  620. #else
  621. return false;
  622. #endif
  623. }
  624. void FPHost6::fill_FPR(FingerPrintResultsIPv6 *FPR) {
  625. unsigned int i;
  626. FPR->begin_time = this->begin_time;
  627. for (i = 0; i < sizeof(this->fp_responses) / sizeof(this->fp_responses[0]); i++) {
  628. const FPResponse *resp;
  629. resp = this->fp_responses[i];
  630. if (resp != NULL) {
  631. FPR->fp_responses[i] = new FPResponse(resp->probe_id, resp->buf, resp->len,
  632. resp->senttime, resp->rcvdtime);
  633. }
  634. }
  635. /* Were we actually able to set the flow label? */
  636. FPR->flow_label = 0;
  637. for (i = 0; i < sizeof(this->fp_probes) / sizeof(this->fp_probes[0]); i++) {
  638. const FPProbe& probe = fp_probes[0];
  639. if (probe.is_set()) {
  640. if (can_set_flow_label(probe.getEthernet()))
  641. FPR->flow_label = OSDETECT_FLOW_LABEL;
  642. break;
  643. }
  644. }
  645. }
  646. static const IPv6Header *find_ipv6(const PacketElement *pe) {
  647. while (pe != NULL && pe->protocol_id() != HEADER_TYPE_IPv6)
  648. pe = pe->getNextElement();
  649. return (IPv6Header *) pe;
  650. }
  651. static const TCPHeader *find_tcp(const PacketElement *pe) {
  652. while (pe != NULL && pe->protocol_id() != HEADER_TYPE_TCP)
  653. pe = pe->getNextElement();
  654. return (TCPHeader *) pe;
  655. }
  656. static double vectorize_plen(const PacketElement *pe) {
  657. const IPv6Header *ipv6;
  658. ipv6 = find_ipv6(pe);
  659. if (ipv6 == NULL)
  660. return -1;
  661. else
  662. return ipv6->getPayloadLength();
  663. }
  664. static double vectorize_tc(const PacketElement *pe) {
  665. const IPv6Header *ipv6;
  666. ipv6 = find_ipv6(pe);
  667. if (ipv6 == NULL)
  668. return -1;
  669. else
  670. return ipv6->getTrafficClass();
  671. }
  672. static double vectorize_isr(std::map<std::string, FPPacket>& resps) {
  673. const char * const SEQ_PROBE_NAMES[] = {"S1", "S2", "S3", "S4", "S5", "S6"};
  674. u32 seqs[NELEMS(SEQ_PROBE_NAMES)];
  675. struct timeval times[NELEMS(SEQ_PROBE_NAMES)];
  676. unsigned int i, j;
  677. double sum, t;
  678. j = 0;
  679. for (i = 0; i < NELEMS(SEQ_PROBE_NAMES); i++) {
  680. const char *probe_name;
  681. const FPPacket *fp;
  682. const TCPHeader *tcp;
  683. std::map<std::string, FPPacket>::iterator it;
  684. probe_name = SEQ_PROBE_NAMES[i];
  685. it = resps.find(probe_name);
  686. if (it == resps.end())
  687. continue;
  688. fp = &it->second;
  689. tcp = find_tcp(fp->getPacket());
  690. if (tcp == NULL)
  691. continue;
  692. seqs[j] = tcp->getSeq();
  693. times[j] = fp->getTime();
  694. j++;
  695. }
  696. if (j < 2)
  697. return -1;
  698. sum = 0.0;
  699. for (i = 0; i < j - 1; i++)
  700. sum += seqs[i + 1] - seqs[i];
  701. t = TIMEVAL_FSEC_SUBTRACT(times[j - 1], times[0]);
  702. return sum / t;
  703. }
  704. static struct feature_node *vectorize(const FingerPrintResultsIPv6 *FPR) {
  705. const char * const IPV6_PROBE_NAMES[] = {"S1", "S2", "S3", "S4", "S5", "S6", "IE1", "IE2", "NS", "U1", "TECN", "T2", "T3", "T4", "T5", "T6", "T7"};
  706. const char * const TCP_PROBE_NAMES[] = {"S1", "S2", "S3", "S4", "S5", "S6", "TECN", "T2", "T3", "T4", "T5", "T6", "T7"};
  707. unsigned int nr_feature, i, idx;
  708. struct feature_node *features;
  709. std::map<std::string, FPPacket> resps;
  710. for (i = 0; i < NUM_FP_PROBES_IPv6; i++) {
  711. PacketElement *pe;
  712. if (FPR->fp_responses[i] == NULL)
  713. continue;
  714. pe = PacketParser::split(FPR->fp_responses[i]->buf, FPR->fp_responses[i]->len);
  715. assert(pe != NULL);
  716. resps[FPR->fp_responses[i]->probe_id].setPacket(pe);
  717. resps[FPR->fp_responses[i]->probe_id].setTime(&FPR->fp_responses[i]->senttime);
  718. }
  719. nr_feature = get_nr_feature(&FPModel);
  720. features = new feature_node[nr_feature + 1];
  721. for (i = 0; i < nr_feature; i++) {
  722. features[i].index = i + 1;
  723. features[i].value = -1;
  724. }
  725. features[i].index = -1;
  726. idx = 0;
  727. for (i = 0; i < NELEMS(IPV6_PROBE_NAMES); i++) {
  728. const char *probe_name;
  729. probe_name = IPV6_PROBE_NAMES[i];
  730. features[idx++].value = vectorize_plen(resps[probe_name].getPacket());
  731. features[idx++].value = vectorize_tc(resps[probe_name].getPacket());
  732. }
  733. /* TCP features */
  734. features[idx++].value = vectorize_isr(resps);
  735. for (i = 0; i < NELEMS(TCP_PROBE_NAMES); i++) {
  736. const char *probe_name;
  737. const TCPHeader *tcp;
  738. u16 flags;
  739. u16 mask;
  740. unsigned int j;
  741. int mss;
  742. int sackok;
  743. int wscale;
  744. probe_name = TCP_PROBE_NAMES[i];
  745. mss = -1;
  746. sackok = -1;
  747. wscale = -1;
  748. tcp = find_tcp(resps[probe_name].getPacket());
  749. if (tcp == NULL) {
  750. /* 48 TCP features. */
  751. idx += 48;
  752. continue;
  753. }
  754. features[idx++].value = tcp->getWindow();
  755. flags = tcp->getFlags16();
  756. for (mask = 0x001; mask <= 0x800; mask <<= 1)
  757. features[idx++].value = (flags & mask) != 0;
  758. for (j = 0; j < 16; j++) {
  759. nping_tcp_opt_t opt;
  760. opt = tcp->getOption(j);
  761. if (opt.value == NULL)
  762. break;
  763. features[idx++].value = opt.type;
  764. /* opt.len includes the two (type, len) bytes. */
  765. if (opt.type == TCPOPT_MSS && opt.len == 4 && mss == -1)
  766. mss = ntohs(*(u16 *) opt.value);
  767. else if (opt.type == TCPOPT_SACKOK && opt.len == 2 && sackok == -1)
  768. sackok = 1;
  769. else if (opt.type == TCPOPT_WSCALE && opt.len == 3 && wscale == -1)
  770. wscale = *(u8 *) opt.value;
  771. }
  772. for (; j < 16; j++)
  773. idx++;
  774. for (j = 0; j < 16; j++) {
  775. nping_tcp_opt_t opt;
  776. opt = tcp->getOption(j);
  777. if (opt.value == NULL)
  778. break;
  779. features[idx++].value = opt.len;
  780. }
  781. for (; j < 16; j++)
  782. idx++;
  783. features[idx++].value = mss;
  784. features[idx++].value = sackok;
  785. features[idx++].value = wscale;
  786. }
  787. assert(idx == nr_feature);
  788. if (o.debugging > 2) {
  789. log_write(LOG_PLAIN, "v = {");
  790. for (i = 0; i < nr_feature; i++)
  791. log_write(LOG_PLAIN, "%.16g, ", features[i].value);
  792. log_write(LOG_PLAIN, "};\n");
  793. }
  794. return features;
  795. }
  796. static void apply_scale(struct feature_node *features, unsigned int num_features,
  797. const double (*scale)[2]) {
  798. unsigned int i;
  799. for (i = 0; i < num_features; i++) {
  800. double val = features[i].value;
  801. if (val < 0)
  802. continue;
  803. val = (val + scale[i][0]) * scale[i][1];
  804. features[i].value = val;
  805. }
  806. }
  807. /* (label, prob) pairs for purpose of sorting. */
  808. struct label_prob {
  809. int label;
  810. double prob;
  811. };
  812. int label_prob_cmp(const void *a, const void *b) {
  813. const struct label_prob *la, *lb;
  814. la = (struct label_prob *) a;
  815. lb = (struct label_prob *) b;
  816. /* Sort descending. */
  817. if (la->prob > lb->prob)
  818. return -1;
  819. else if (la->prob < lb->prob)
  820. return 1;
  821. else
  822. return 0;
  823. }
  824. /* Return a measure of how much the given feature vector differs from the other
  825. members of the class given by label.
  826. This can be thought of as the distance from the given feature vector to the
  827. mean of the class in multidimensional space, after scaling. Each dimension is
  828. further scaled by the inverse of the sample variance of that feature. This is
  829. an approximation of the Mahalanobis distance
  830. (https://en.wikipedia.org/wiki/Mahalanobis_distance), which normally uses a
  831. full covariance matrix of the features. If we take the features to be
  832. pairwise independent (which they are not), then the covariance matrix is just
  833. the diagonal matrix containing per-feature variances, leading to the same
  834. calculation as is done below. Using only the per-feature variances rather
  835. than covariance matrices is to save space; it requires only n entries per
  836. class rather than n^2, where n is the length of a feature vector.
  837. It happens often that a feature's variance is undefined (because there is
  838. only one example in the class) or zero (because there are two identical
  839. values for that feature). Both these cases are mapped to zero by train.py,
  840. and we handle them the same way: by using a small default variance. This will
  841. tend to make small differences count a lot (because we probably want this
  842. fingerprint in order to expand the class), while still allowing near-perfect
  843. matches to match. */
  844. static double novelty_of(const struct feature_node *features, int label) {
  845. const double *means, *variances;
  846. int i, nr_feature;
  847. double sum;
  848. nr_feature = get_nr_feature(&FPModel);
  849. assert(0 <= label);
  850. assert(label < nr_feature);
  851. means = FPmean[label];
  852. variances = FPvariance[label];
  853. sum = 0.0;
  854. for (i = 0; i < nr_feature; i++) {
  855. double d, v;
  856. assert(i + 1 == features[i].index);
  857. d = features[i].value - means[i];
  858. v = variances[i];
  859. if (v == 0.0) {
  860. /* No variance? It means that samples were identical. Substitute a default
  861. variance. This will tend to make novelty large in these cases, which
  862. will hopefully encourage for submissions for this class. */
  863. v = 0.01;
  864. }
  865. sum += d * d / v;
  866. }
  867. return sqrt(sum);
  868. }
  869. static void classify(FingerPrintResultsIPv6 *FPR) {
  870. int nr_class, i;
  871. struct feature_node *features;
  872. double *values;
  873. struct label_prob *labels;
  874. nr_class = get_nr_class(&FPModel);
  875. features = vectorize(FPR);
  876. values = new double[nr_class];
  877. labels = new struct label_prob[nr_class];
  878. apply_scale(features, get_nr_feature(&FPModel), FPscale);
  879. predict_values(&FPModel, features, values);
  880. for (i = 0; i < nr_class; i++) {
  881. labels[i].label = i;
  882. labels[i].prob = 1.0 / (1.0 + exp(-values[i]));
  883. }
  884. qsort(labels, nr_class, sizeof(labels[0]), label_prob_cmp);
  885. for (i = 0; i < nr_class && i < MAX_FP_RESULTS; i++) {
  886. FPR->matches[i] = &o.os_labels_ipv6[labels[i].label];
  887. FPR->accuracy[i] = labels[i].prob;
  888. FPR->num_matches = i + 1;
  889. if (labels[i].prob >= 0.90 * labels[0].prob)
  890. FPR->num_perfect_matches = i + 1;
  891. if (o.debugging > 2) {
  892. printf("%7.4f %7.4f %3u %s\n", FPR->accuracy[i] * 100,
  893. novelty_of(features, labels[i].label), labels[i].label, FPR->matches[i]->OS_name);
  894. }
  895. }
  896. if (FPR->num_perfect_matches == 0) {
  897. FPR->overall_results = OSSCAN_NOMATCHES;
  898. } else if (FPR->num_perfect_matches == 1) {
  899. double novelty;
  900. novelty = novelty_of(features, labels[0].label);
  901. if (o.debugging > 1)
  902. log_write(LOG_PLAIN, "Novelty of closest match is %.3f.\n", novelty);
  903. if (novelty < FP_NOVELTY_THRESHOLD) {
  904. FPR->overall_results = OSSCAN_SUCCESS;
  905. } else {
  906. if (o.debugging > 0) {
  907. log_write(LOG_PLAIN, "Novelty of closest match is %.3f > %.3f; ignoring.\n",
  908. novelty, FP_NOVELTY_THRESHOLD);
  909. }
  910. FPR->overall_results = OSSCAN_NOMATCHES;
  911. FPR->num_perfect_matches = 0;
  912. }
  913. } else {
  914. FPR->overall_results = OSSCAN_NOMATCHES;
  915. FPR->num_perfect_matches = 0;
  916. }
  917. delete[] features;
  918. delete[] values;
  919. delete[] labels;
  920. }
  921. /* This method is the core of the FPEngine class. It takes a list of IPv6
  922. * targets that need to be fingerprinted. The method handles the whole
  923. * fingerprinting process, sending probes, collecting responses, analyzing
  924. * results and matching fingerprints. If everything goes well, the internal
  925. * state of the supplied target objects will be modified to reflect the results
  926. * of the */
  927. int FPEngine6::os_scan(std::vector<Target *> &Targets) {
  928. bool osscan_done = false;
  929. const char *bpf_filter = NULL;
  930. std::vector<FPHost6 *> curr_hosts; /* Hosts currently doing OS detection */
  931. std::vector<FPHost6 *> done_hosts; /* Hosts for which we already did OSdetect */
  932. std::vector<FPHost6 *> left_hosts; /* Hosts we have not yet started with */
  933. struct timeval begin_time;
  934. if (o.debugging)
  935. log_write(LOG_PLAIN, "Starting IPv6 OS Scan...\n");
  936. /* Initialize variables, timers, etc. */
  937. gettimeofday(&begin_time, NULL);
  938. global_netctl.init(Targets[0]->deviceName(), Targets[0]->ifType());
  939. for (size_t i = 0; i < Targets.size(); i++) {
  940. if (o.debugging > 3) {
  941. log_write(LOG_PLAIN, "[FPEngine] Allocating FPHost6 for %s %s\n",
  942. Targets[i]->targetipstr(), Targets[i]->sourceipstr());
  943. }
  944. FPHost6 *newhost = new FPHost6(Targets[i], &global_netctl);
  945. newhost->begin_time = begin_time;
  946. fphosts.push_back(newhost);
  947. }
  948. /* Build the BPF filter */
  949. bpf_filter = this->bpf_filter(Targets);
  950. if (o.debugging)
  951. log_write(LOG_PLAIN, "[FPEngine] Interface=%s BPF:%s\n", Targets[0]->deviceName(), bpf_filter);
  952. /* Set up the sniffer */
  953. global_netctl.setup_sniffer(Targets[0]->deviceName(), bpf_filter);
  954. /* Divide the targets into two groups, the ones we are going to start
  955. * processing, and the ones we leave for later. */
  956. for (size_t i = 0; i < Targets.size() && i < this->osgroup_size; i++) {
  957. curr_hosts.push_back(fphosts[i]);
  958. }
  959. for (size_t i = curr_hosts.size(); i < Targets.size(); i++) {
  960. left_hosts.push_back(fphosts[i]);
  961. }
  962. /* Do the OS detection rounds */
  963. while (!osscan_done) {
  964. osscan_done = true; /* It will remain true only when all hosts are .done() */
  965. if (o.debugging > 3) {
  966. log_write(LOG_PLAIN, "[FPEngine] CurrHosts=%d, LeftHosts=%d, DoneHosts=%d\n",
  967. (int) curr_hosts.size(), (int) left_hosts.size(), (int) done_hosts.size());
  968. }
  969. /* Go through the list of hosts and ask them to schedule their probes */
  970. for (unsigned int i = 0; i < curr_hosts.size(); i++) {
  971. /* If the host is not done yet, call schedule() to let it schedule
  972. * new probes, retransmissions, etc. */
  973. if (!curr_hosts[i]->done()) {
  974. osscan_done = false;
  975. curr_hosts[i]->schedule();
  976. if (o.debugging > 3)
  977. log_write(LOG_PLAIN, "[FPEngine] CurrHost #%u not done\n", i);
  978. /* If the host is done, take it out of the curr_hosts group and add it
  979. * to the done_hosts group. If we still have hosts left in the left_hosts
  980. * group, take the first one and insert it into curr_hosts. This way we
  981. * always have a full working group of hosts (unless we ran out of hosts,
  982. * of course). */
  983. } else {
  984. if (o.debugging > 3)
  985. log_write(LOG_PLAIN, "[FPEngine] CurrHost #%u done\n", i);
  986. if (o.debugging > 3)
  987. log_write(LOG_PLAIN, "[FPEngine] Moving done host %u to the done_hosts list\n", i);
  988. done_hosts.push_back(curr_hosts[i]);
  989. curr_hosts.erase(curr_hosts.begin() + i);
  990. /* If we still have hosts left, add one to the current group */
  991. if (left_hosts.size() > 0) {
  992. if (o.debugging > 3)
  993. log_write(LOG_PLAIN, "[FPEngine] Inserting one new hosts in the curr_hosts list.\n");
  994. curr_hosts.push_back(left_hosts[0]);
  995. left_hosts.erase(left_hosts.begin());
  996. osscan_done = false;
  997. }
  998. i--; /* Decrement i so we don't miss the host that is now in the
  999. * position of the host we've just removed from the list */
  1000. }
  1001. }
  1002. /* Handle scheduled events */
  1003. global_netctl.handle_events();
  1004. }
  1005. /* Once we've finished with all fphosts, check which ones were correctly
  1006. * fingerprinted, and update the Target objects. */
  1007. for (size_t i = 0; i < this->fphosts.size(); i++) {
  1008. fphosts[i]->finish();
  1009. fphosts[i]->fill_FPR((FingerPrintResultsIPv6 *) Targets[i]->FPR);
  1010. classify((FingerPrintResultsIPv6 *) Targets[i]->FPR);
  1011. }
  1012. /* Cleanup and return */
  1013. while (this->fphosts.size() > 0) {
  1014. FPHost6 *tmp = fphosts.back();
  1015. delete tmp;
  1016. fphosts.pop_back();
  1017. }
  1018. if (o.debugging)
  1019. log_write(LOG_PLAIN, "IPv6 OS Scan completed.\n");
  1020. return OP_SUCCESS;
  1021. }
  1022. /******************************************************************************
  1023. * Implementation of class FPHost. *
  1024. ******************************************************************************/
  1025. FPHost::FPHost() {
  1026. this->__reset();
  1027. }
  1028. FPHost::~FPHost() {
  1029. }
  1030. void FPHost::__reset() {
  1031. this->total_probes = 0;
  1032. this->timed_probes = 0;
  1033. this->probes_sent = 0;
  1034. this->probes_answered = 0;
  1035. this->probes_unanswered = 0;
  1036. this->detection_done = false;
  1037. this->timedprobes_sent = false;
  1038. this->target_host = NULL;
  1039. this->netctl = NULL;
  1040. this->netctl_registered = false;
  1041. this->tcpSeqBase = 0;
  1042. this->open_port_tcp = -1;
  1043. this->closed_port_tcp = -1;
  1044. this->closed_port_udp = -1;
  1045. this->tcp_port_base = -1;
  1046. this->udp_port_base = -1;
  1047. /* Retransmission time-out parameters.
  1048. *
  1049. * From RFC 2988:
  1050. * Until a round-trip time (RTT) measurement has been made for a segment
  1051. * sent between the sender and receiver, the sender SHOULD set
  1052. * RTO <- 3 seconds */
  1053. this->rto = OSSCAN_INITIAL_RTO;
  1054. this->rttvar = -1;
  1055. this->srtt = -1;
  1056. this->begin_time.tv_sec = 0;
  1057. this->begin_time.tv_usec = 0;
  1058. }
  1059. /* Returns the IP address of the target associated with the FPHost in
  1060. * stuct sockaddr_storage format. */
  1061. const struct sockaddr_storage *FPHost::getTargetAddress() {
  1062. return this->target_host->TargetSockAddr();
  1063. }
  1064. /* Accesses the Target object associated with the FPHost to extract the port
  1065. * numbers to be used in OS detection. In particular it extracts:
  1066. *
  1067. * - An open TCP port.
  1068. * - A closed TCP port.
  1069. * - A closed UDP port.
  1070. *
  1071. * When not enough information is found in the Target, the neccessary port
  1072. * numbers are generated randomly. */
  1073. int FPHost::choose_osscan_ports() {
  1074. Port *tport = NULL;
  1075. Port port;
  1076. /* Choose an open TCP port: First, check if the host already has a
  1077. * FingerPrintResults object that defines an open port. */
  1078. if (this->target_host->FPR != NULL && this->target_host->FPR->osscan_opentcpport > 0) {
  1079. this->open_port_tcp = this->target_host->FPR->osscan_opentcpport;
  1080. /* Otherwise, get the first open port that we've found open */
  1081. } else if ((tport = this->target_host->ports.nextPort(NULL, &port, IPPROTO_TCP, PORT_OPEN))) {
  1082. this->open_port_tcp = tport->portno;
  1083. /* If it is zero, let's try another one if there is one */
  1084. if (tport->portno == 0) {
  1085. if ((tport = this->target_host->ports.nextPort(tport, &port, IPPROTO_TCP, PORT_OPEN)))
  1086. this->open_port_tcp = tport->portno;
  1087. }
  1088. this->target_host->FPR->osscan_opentcpport = this->open_port_tcp;
  1089. } else {
  1090. /* If we don't have an open port, set it to -1 so we don't send probes that
  1091. * target TCP open ports */
  1092. this->open_port_tcp = -1;
  1093. }
  1094. /* Choose a closed TCP port. */
  1095. if (this->target_host->FPR != NULL && this->target_host->FPR->osscan_closedtcpport > 0) {
  1096. this->closed_port_tcp =this->target_host->FPR->osscan_closedtcpport;
  1097. } else if ((tport = this->target_host->ports.nextPort(NULL, &port, IPPROTO_TCP, PORT_CLOSED))) {
  1098. this->closed_port_tcp = tport->portno;
  1099. /* If it is zero, let's try another one if there is one */
  1100. if (tport->portno == 0)
  1101. if ((tport = this->target_host->ports.nextPort(tport, &port, IPPROTO_TCP, PORT_CLOSED)))
  1102. this->closed_port_tcp = tport->portno;
  1103. this->target_host->FPR->osscan_closedtcpport = this->closed_port_tcp;
  1104. } else if ((tport = this->target_host->ports.nextPort(NULL, &port, IPPROTO_TCP, PORT_UNFILTERED))) {
  1105. /* Well, we will settle for unfiltered */
  1106. this->closed_port_tcp = tport->portno;
  1107. /* But again we'd prefer not to have zero */
  1108. if (tport->portno == 0)
  1109. if ((tport =this->target_host->ports.nextPort(tport, &port, IPPROTO_TCP, PORT_UNFILTERED)))
  1110. this->closed_port_tcp = tport->portno;
  1111. } else {
  1112. /* If we don't have a closed port, set it to -1 so we don't send probes that
  1113. * target TCP closed ports. */
  1114. this->closed_port_tcp = -1;
  1115. }
  1116. /* Closed UDP port */
  1117. if (this->target_host->FPR != NULL && this->target_host->FPR->osscan_closedudpport > 0) {
  1118. this->closed_port_udp = this->target_host->FPR->osscan_closedudpport;
  1119. } else if ((tport = this->target_host->ports.nextPort(NULL, &port, IPPROTO_UDP, PORT_CLOSED))) {
  1120. this->closed_port_udp = tport->portno;
  1121. /* Not zero, if possible */
  1122. if (tport->portno == 0)
  1123. if ((tport = this->target_host->ports.nextPort(tport, &port, IPPROTO_UDP, PORT_CLOSED)))
  1124. this->closed_port_udp = tport->portno;
  1125. this->target_host->FPR->osscan_closedudpport = this->closed_port_udp;
  1126. } else if ((tport = this->target_host->ports.nextPort(NULL, &port, IPPROTO_UDP, PORT_UNFILTERED))) {
  1127. /* Well, we will settle for unfiltered */
  1128. this->closed_port_udp = tport->portno;
  1129. /* But not zero, please */
  1130. if (tport->portno == 0)
  1131. if ((tport = this->target_host->ports.nextPort(NULL, &port, IPPROTO_UDP, PORT_UNFILTERED)))
  1132. this->closed_port_udp = tport->portno;
  1133. } else {
  1134. /* Pick one at random. Shrug. */
  1135. this->closed_port_udp = (get_random_uint() % 14781) + 30000;
  1136. }
  1137. this->tcpSeqBase = get_random_u32();
  1138. this->tcp_port_base = o.magic_port_set ? o.magic_port : o.magic_port + get_random_u8();
  1139. this->udp_port_base = o.magic_port_set ? o.magic_port : o.magic_port + get_random_u8();
  1140. this->icmp_seq_counter = 0;
  1141. return OP_SUCCESS;
  1142. }
  1143. /* This method is called whenever we receive a response to a probe. It
  1144. * recomputes the host's retransmission timer based on the new RTT measure.
  1145. * @param measured_rtt_usecs is the new RTT observation in MICROseconds.
  1146. * @param retransmission indicates whether the observed RTT correspond to
  1147. * a packet that was transmitted more than once or not. It is used to
  1148. * avoid using RTT samples obtained from retransmissions (Karn's algorithm) */
  1149. int FPHost::update_RTO(int measured_rtt_usecs, bool retransmission) {
  1150. /* RFC 2988: TCP MUST use Karn's algorithm [KP87] for taking RTT samples. That
  1151. * is, RTT samples MUST NOT be made using segments that were
  1152. * retransmitted (and thus for which it is ambiguous whether the reply
  1153. * was for the first instance of the packet or a later instance).*/
  1154. if (retransmission == true)
  1155. return OP_SUCCESS;
  1156. /* RFC 2988: When the first RTT measurement R is made, the host MUST set
  1157. *
  1158. * SRTT <- R
  1159. * RTTVAR <- R/2
  1160. * RTO <- SRTT + max (G, K*RTTVAR)
  1161. *
  1162. * where K = 4, and G is the clock granularity.. */
  1163. if (this->srtt == -1 && this->rttvar == -1) {
  1164. this->srtt = measured_rtt_usecs;
  1165. this->rttvar = measured_rtt_usecs/2;
  1166. this->rto = this->srtt + MAX(500000, 4*this->rttvar); /* Asume a granularity of 1/2 sec */
  1167. } else {
  1168. /* RFC 2988: When a subsequent RTT measurement R' is made, a host MUST set
  1169. *
  1170. * RTTVAR <- (1 - beta) * RTTVAR + beta * |SRTT - R'|
  1171. * SRTT <- (1 - alpha) * SRTT + alpha * R'
  1172. *
  1173. * The above SHOULD be computed using alpha = 1/8 and beta = 1/4.
  1174. * After the computation, a host MUST update
  1175. *
  1176. * RTO <- SRTT + max (G, K*RTTVAR)
  1177. */
  1178. this->rttvar = ((1.0 - 0.25) * this->rttvar) + (0.25 * ABS(this->srtt - measured_rtt_usecs));
  1179. this->srtt = ((1.0 - 0.125) * this->srtt) + (0.125 * measured_rtt_usecs);
  1180. this->rto = this->srtt + MAX(500000, 4*this->rttvar);
  1181. }
  1182. /* RFC 2988: Whenever RTO is computed, if it is less than 1 second then the RTO
  1183. * SHOULD be rounded up to 1 second.
  1184. * [NOTE: In Nmap we find this excessive, so we set a minimum of 100ms
  1185. * (100,000 usecs). It may seem aggressive but waiting too long can cause
  1186. * the engine to fail to detect drops until many probes later on extremely
  1187. * low-latency networks (such as localhost scans). */
  1188. if (this->rto < (MIN_RTT_TIMEOUT*1000))
  1189. this->rto = (MIN_RTT_TIMEOUT*1000);
  1190. return this->rto;
  1191. }
  1192. /******************************************************************************
  1193. * Implementation of class FPHost6. *
  1194. ******************************************************************************/
  1195. FPHost6::FPHost6(Target *tgt, FPNetworkControl *fpnc) {
  1196. this->init(tgt, fpnc);
  1197. return;
  1198. }
  1199. FPHost6::~FPHost6() {
  1200. this->reset();
  1201. }
  1202. void FPHost6::reset() {
  1203. this->__reset();
  1204. for (unsigned int i = 0; i < NUM_FP_PROBES_IPv6; i++) {
  1205. this->fp_probes[i].reset();
  1206. if (this->fp_responses[i]) {
  1207. delete this->fp_responses[i];
  1208. this->fp_responses[i] = NULL;
  1209. }
  1210. }
  1211. }
  1212. void FPHost6::init(Target *tgt, FPNetworkControl *fpnc) {
  1213. this->target_host = tgt;
  1214. this->netctl = fpnc;
  1215. this->total_probes = 0;
  1216. this->timed_probes = 0;
  1217. /* Set state in the supplied Target */
  1218. if (this->target_host->FPR == NULL)
  1219. this->target_host->FPR = new FingerPrintResultsIPv6;
  1220. this->target_host->osscanSetFlag(OS_PERF);
  1221. /* Choose TCP/UDP ports for the prbes. */
  1222. this->choose_osscan_ports();
  1223. /* Build the list of OS detection probes */
  1224. this->build_probe_list();
  1225. for (unsigned int i = 0; i < NUM_FP_PROBES_IPv6; i++)
  1226. this->fp_responses[i] = NULL;
  1227. for (unsigned int i = 0; i < NUM_FP_TIMEDPROBES_IPv6; i++)
  1228. this->aux_resp[i] = NULL;
  1229. }
  1230. /* Get the hop limit encapsulated in an ICMPv6 error reply. Return -1 if it
  1231. * can't be found. */
  1232. static int get_encapsulated_hoplimit(const PacketElement *pe) {
  1233. /* Check that it's IPv6. */
  1234. if (pe == NULL || pe->protocol_id() != HEADER_TYPE_IPv6)
  1235. return -1;
  1236. /* Find the ICMPv6 payload. */
  1237. pe = pe->getNextElement();
  1238. for (; pe != NULL; pe = pe->getNextElement()) {
  1239. if (pe->protocol_id() == HEADER_TYPE_ICMPv6)
  1240. break;
  1241. }
  1242. if (pe == NULL)
  1243. return -1;
  1244. /* Check that encapsulated is IPv6. */
  1245. pe = pe->getNextElement();
  1246. if (pe == NULL || pe->protocol_id() != HEADER_TYPE_IPv6)
  1247. return -1;
  1248. return ((IPv6Header *) pe)->getHopLimit();
  1249. }
  1250. void FPHost6::finish() {
  1251. /* These probes are likely to get an ICMPv6 error (allowing us to calculate
  1252. distance. */
  1253. const char * const DISTANCE_PROBE_NAMES[] = { "IE2", "NI", "U1" };
  1254. int distance = -1;
  1255. int hoplimit_distance = -1;
  1256. enum dist_calc_method distance_calculation_method = DIST_METHOD_NONE;
  1257. unsigned int i;
  1258. /* Calculate distance based on hop limit difference. */
  1259. for (i = 0; i < NELEMS(DISTANCE_PROBE_NAMES); i++) {
  1260. const FPProbe *probe;
  1261. const FPResponse *resp;
  1262. const PacketElement *probe_pe;
  1263. PacketElement *resp_pe;
  1264. int sent_ttl, rcvd_ttl;
  1265. const char *probe_name;
  1266. probe_name = DISTANCE_PROBE_NAMES[i];
  1267. probe = this->getProbe(probe_name);
  1268. resp = this->getResponse(probe_name);
  1269. if (probe == NULL || resp == NULL)
  1270. continue;
  1271. probe_pe = probe->getPacket();
  1272. if (probe_pe->protocol_id() != HEADER_TYPE_IPv6)
  1273. continue;
  1274. sent_ttl = ((IPv6Header *) probe_pe)->getHopLimit();
  1275. resp_pe = PacketParser::split(resp->buf, resp->len);
  1276. assert(resp_pe != NULL);
  1277. rcvd_ttl = get_encapsulated_hoplimit(resp_pe);
  1278. if (rcvd_ttl != -1) {
  1279. if (o.debugging > 1) {
  1280. log_write(LOG_PLAIN, "Hop limit distance from %s probe: %d - %d + 1 == %d\n",
  1281. probe_name, sent_ttl, rcvd_ttl, sent_ttl - rcvd_ttl + 1);
  1282. }
  1283. /* Set only if not already set. */
  1284. if (hoplimit_distance == -1)
  1285. hoplimit_distance = sent_ttl - rcvd_ttl + 1;
  1286. /* Special case: for the U1 probe, mark that we found the port closed. */
  1287. if (this->target_host->FPR->osscan_closedudpport == -1 && strcmp(probe_name, "U1") == 0) {
  1288. const PacketElement *udp;
  1289. u16 portno;
  1290. udp = probe_pe->getNextElement();
  1291. assert(udp != NULL);
  1292. assert(udp->protocol_id() == HEADER_TYPE_UDP);
  1293. portno = ((UDPHeader *) udp)->getDestinationPort();
  1294. this->target_host->FPR->osscan_closedudpport = portno;
  1295. }
  1296. }
  1297. PacketParser::freePacketChain(resp_pe);
  1298. }
  1299. if (islocalhost(this->target_host->TargetSockAddr())) {
  1300. /* scanning localhost */
  1301. distance = 0;
  1302. distance_calculation_method = DIST_METHOD_LOCALHOST;
  1303. } else if (this->target_host->directlyConnected()) {
  1304. /* on the same network segment */
  1305. distance = 1;
  1306. distance_calculation_method = DIST_METHOD_DIRECT;
  1307. } else if (hoplimit_distance != -1) {
  1308. distance = hoplimit_distance;
  1309. distance_calculation_method = DIST_METHOD_ICMP;
  1310. }
  1311. this->target_host->distance = this->target_host->FPR->distance = distance;
  1312. this->target_host->distance_calculation_method = distance_calculation_method;
  1313. }
  1314. struct tcp_desc {
  1315. const char *id;
  1316. u16 win;
  1317. u8 flags;
  1318. u16 dstport;
  1319. u16 urgptr;
  1320. const char *opts;
  1321. unsigned int optslen;
  1322. };
  1323. static u8 get_hoplimit() {
  1324. if (o.ttl != -1)
  1325. return o.ttl;
  1326. else
  1327. return (get_random_uint() % 23) + 37;
  1328. }
  1329. static IPv6Header *make_tcp(const struct sockaddr_in6 *src,
  1330. const struct sockaddr_in6 *dst,
  1331. u32 fl, u16 win, u32 seq, u32 ack, u8 flags, u16 srcport, u16 dstport,
  1332. u16 urgptr, const char *opts, unsigned int optslen) {
  1333. IPv6Header *ip6;
  1334. TCPHeader *tcp;
  1335. /* Allocate an instance of the protocol headers */
  1336. ip6 = new IPv6Header();
  1337. tcp = new TCPHeader();
  1338. ip6->setSourceAddress(src->sin6_addr);
  1339. ip6->setDestinationAddress(dst->sin6_addr);
  1340. ip6->setFlowLabel(fl);
  1341. ip6->setHopLimit(get_hoplimit());
  1342. ip6->setNextHeader("TCP");
  1343. ip6->setNextElement(tcp);
  1344. tcp->setWindow(win);
  1345. tcp->setSeq(seq);
  1346. tcp->setAck(ack);
  1347. tcp->setFlags(flags);
  1348. tcp->setSourcePort(srcport);
  1349. tcp->setDestinationPort(dstport);
  1350. tcp->setUrgPointer(urgptr);
  1351. tcp->setOptions((u8 *) opts, optslen);
  1352. ip6->setPayloadLength(tcp->getLen());
  1353. tcp->setSum();
  1354. return ip6;
  1355. }
  1356. /* This method generates the list of OS detection probes to be sent to the
  1357. * target. It also sets up the list of responses. It is defined private
  1358. * because it is called by the constructor when the class is instantiated. */
  1359. int FPHost6::build_probe_list() {
  1360. #define OPEN 1
  1361. #define CLSD 0
  1362. /* TCP Options:
  1363. * S1-S6: six sequencing probes.
  1364. * TECN: ECN probe.
  1365. * T2-T7: other non-sequencing probes.
  1366. *
  1367. * option 0: WScale (10), Nop, MSS (1460), Timestamp, SackP
  1368. * option 1: MSS (1400), WScale (0), SackP, T(0xFFFFFFFF,0x0), EOL
  1369. * option 2: T(0xFFFFFFFF, 0x0), Nop, Nop, WScale (5), Nop, MSS (640)
  1370. * option 3: SackP, T(0xFFFFFFFF,0x0), WScale (10), EOL
  1371. * option 4: MSS (536), SackP, T(0xFFFFFFFF,0x0), WScale (10), EOL
  1372. * option 5: MSS (265), SackP, T(0xFFFFFFFF,0x0)
  1373. * option 6: WScale (10), Nop, MSS (1460), SackP, Nop, Nop
  1374. * option 7-11: WScale (10), Nop, MSS (265), T(0xFFFFFFFF,0x0), SackP
  1375. * option 12: WScale (15), Nop, MSS (265), T(0xFFFFFFFF,0x0), SackP */
  1376. const struct tcp_desc TCP_DESCS[] = {
  1377. { "S1", 1, 0x02, OPEN, 0,
  1378. "\x03\x03\x0A\x01\x02\x04\x05\xb4\x08\x0A\xff\xff\xff\xff\x00\x00\x00\x00\x04\x02", 20 },
  1379. { "S2", 63, 0x02, OPEN, 0,
  1380. "\x02\x04\x05\x78\x03\x03\x00\x04\x02\x08\x0A\xff\xff\xff\xff\x00\x00\x00\x00\x00", 20 },
  1381. { "S3", 4, 0x02, OPEN, 0,
  1382. "\x08\x0A\xff\xff\xff\xff\x00\x00\x00\x00\x01\x01\x03\x03\x05\x01\x02\x04\x02\x80", 20 },
  1383. { "S4", 4, 0x02, OPEN, 0,
  1384. "\x04\x02\x08\x0A\xff\xff\xff\xff\x00\x00\x00\x00\x03\x03\x0A\x00", 16 },
  1385. { "S5", 16, 0x02, OPEN, 0,
  1386. "\x02\x04\x02\x18\x04\x02\x08\x0A\xff\xff\xff\xff\x00\x00\x00\x00\x03\x03\x0A\x00", 20 },
  1387. { "S6", 512, 0x02, OPEN, 0,
  1388. "\x02\x04\x01\x09\x04\x02\x08\x0A\xff\xff\xff\xff\x00\x00\x00\x00", 16 },
  1389. { "TECN", 3, 0xc2, OPEN, 63477,
  1390. "\x03\x03\x0A\x01\x02\x04\x05\xb4\x04\x02\x01\x01", 12 },
  1391. { "T2", 128, 0x00, OPEN, 0,
  1392. "\x03\x03\x0A\x01\x02\x04\x01\x09\x08\x0A\xff\xff\xff\xff\x00\x00\x00\x00\x04\x02", 20 },
  1393. { "T3", 256, 0x2b, OPEN, 0,
  1394. "\x03\x03\x0A\x01\x02\x04\x01\x09\x08\x0A\xff\xff\xff\xff\x00\x00\x00\x00\x04\x02", 20 },
  1395. { "T4", 1024, 0x10, OPEN, 0,
  1396. "\x03\x03\x0A\x01\x02\x04\x01\x09\x08\x0A\xff\xff\xff\xff\x00\x00\x00\x00\x04\x02", 20 },
  1397. { "T5", 31337, 0x02, CLSD, 0,
  1398. "\x03\x03\x0A\x01\x02\x04\x01\x09\x08\x0A\xff\xff\xff\xff\x00\x00\x00\x00\x04\x02", 20 },
  1399. { "T6", 32768, 0x10, CLSD, 0,
  1400. "\x03\x03\x0A\x01\x02\x04\x01\x09\x08\x0A\xff\xff\xff\xff\x00\x00\x00\x00\x04\x02", 20 },
  1401. { "T7", 65535, 0x29, CLSD, 0,
  1402. "\x03\x03\x0f\x01\x02\x04\x01\x09\x08\x0A\xff\xff\xff\xff\x00\x00\x00\x00\x04\x02", 20 },
  1403. };
  1404. sockaddr_storage ss;
  1405. size_t slen = 0;
  1406. sockaddr_in6 *ss6 = (sockaddr_in6 *)&ss;
  1407. IPv6Header *ip6;
  1408. ICMPv6Header *icmp6;
  1409. UDPHeader *udp;
  1410. DestOptsHeader *dstopts;
  1411. RoutingHeader *routing;
  1412. HopByHopHeader *hopbyhop1, *hopbyhop2;
  1413. RawData *payload;
  1414. unsigned int i;
  1415. char payloadbuf[300];
  1416. assert(this->target_host != NULL);
  1417. /* Set timed TCP probes */
  1418. for (i = 0; i < NUM_FP_PROBES_IPv6_TCP && i < NUM_FP_TIMEDPROBES_IPv6; i++) {
  1419. /* If the probe is targeted to a TCP port and we don't have
  1420. * any port number for that particular state, skip the probe. */
  1421. if (TCP_DESCS[i].dstport == OPEN && this->open_port_tcp < 0)
  1422. continue;
  1423. if (TCP_DESCS[i].dstport == CLSD && this->closed_port_tcp < 0)
  1424. continue;
  1425. ip6 = make_tcp((struct sockaddr_in6 *) this->target_host->SourceSockAddr(),
  1426. (struct sockaddr_in6 *) this->target_host->TargetSockAddr(),
  1427. OSDETECT_FLOW_LABEL, TCP_DESCS[i].win, this->tcpSeqBase + i, get_random_u32(),
  1428. TCP_DESCS[i].flags, this->tcp_port_base + i,
  1429. TCP_DESCS[i].dstport == OPEN ? this->open_port_tcp : this->closed_port_tcp,
  1430. TCP_DESCS[i].urgptr, TCP_DESCS[i].opts, TCP_DESCS[i].optslen);
  1431. /* Store the probe in the list so we can send it later */
  1432. this->fp_probes[this->total_probes].host = this;
  1433. this->fp_probes[this->total_probes].setPacket(ip6);
  1434. this->fp_probes[this->total_probes].setProbeID(TCP_DESCS[i].id);
  1435. this->fp_probes[this->total_probes].setEthernet(this->target_host->SrcMACAddress(), this->target_host->NextHopMACAddress(), this->target_host->deviceName());
  1436. /* Mark as a timed probe. */
  1437. this->fp_probes[this->total_probes].setTimed();
  1438. this->timed_probes++;
  1439. this->total_probes++;
  1440. }
  1441. /* Set ICMPv6 probes */
  1442. memset(payloadbuf, 0, 120);
  1443. /* ICMP Probe #1: Echo Request with hop-by-hop options */
  1444. /* This one immediately follows the timed seq TCP probes, to allow testing for
  1445. shared flow label sequence. */
  1446. ip6 = new IPv6Header();
  1447. icmp6 = new ICMPv6Header();
  1448. hopbyhop1 = new HopByHopHeader();
  1449. payload = new RawData();
  1450. this->target_host->SourceSockAddr(&ss, &slen);
  1451. ip6->setSourceAddress(ss6->sin6_addr);
  1452. this->target_host->TargetSockAddr(&ss, &slen);
  1453. ip6->setDestinationAddress(ss6->sin6_addr);
  1454. ip6->setFlowLabel(OSDETECT_FLOW_LABEL);
  1455. ip6->setHopLimit(get_hoplimit());
  1456. ip6->setNextHeader((u8) HEADER_TYPE_IPv6_HOPOPT);
  1457. ip6->setNextElement(hopbyhop1);
  1458. hopbyhop1->setNextHeader(HEADER_TYPE_ICMPv6);
  1459. hopbyhop1->setNextElement(icmp6);
  1460. icmp6->setNextElement(payload);
  1461. payload->store((u8 *) payloadbuf, 120);
  1462. icmp6->setType(ICMPv6_ECHO);
  1463. icmp6->setCode(9); // But is supposed to be 0.
  1464. icmp6->setIdentifier(0xabcd);
  1465. icmp6->setSequence(this->icmp_seq_counter++);
  1466. icmp6->setTargetAddress(ss6->sin6_addr); // Should still contain target's addr
  1467. ip6->setPayloadLength();
  1468. icmp6->setSum();
  1469. this->fp_probes[this->total_probes].host = this;
  1470. this->fp_probes[this->total_probes].setPacket(ip6);
  1471. this->fp_probes[this->total_probes].setProbeID("IE1");
  1472. this->fp_probes[this->total_probes].setEthernet(this->target_host->SrcMACAddress(), this->target_host->NextHopMACAddress(), this->target_host->deviceName());
  1473. this->total_probes++;
  1474. /* ICMP Probe #2: Echo Request with badly ordered extension headers */
  1475. ip6 = new IPv6Header();
  1476. hopbyhop1 = new HopByHopHeader();
  1477. dstopts = new DestOptsHeader();
  1478. routing = new RoutingHeader();
  1479. hopbyhop2 = new HopByHopHeader();
  1480. icmp6 = new ICMPv6Header();
  1481. payload = new RawData();
  1482. this->target_host->SourceSockAddr(&ss, &slen);
  1483. ip6->setSourceAddress(ss6->sin6_addr);
  1484. this->target_host->TargetSockAddr(&ss, &slen);
  1485. ip6->setDestinationAddress(ss6->sin6_addr);
  1486. ip6->setFlowLabel(OSDETECT_FLOW_LABEL);
  1487. ip6->setHopLimit(get_hoplimit());
  1488. ip6->setNextHeader((u8) HEADER_TYPE_IPv6_HOPOPT);
  1489. ip6->setNextElement(hopbyhop1);
  1490. hopbyhop1->setNextHeader(HEADER_TYPE_IPv6_OPTS);
  1491. hopbyhop1->setNextElement(dstopts);
  1492. dstopts->setNextHeader(HEADER_TYPE_IPv6_ROUTE);
  1493. dstopts->setNextElement(routing);
  1494. routing->setNextHeader(HEADER_TYPE_IPv6_HOPOPT);
  1495. routing->setNextElement(hopbyhop2);
  1496. hopbyhop2->setNextHeader(HEADER_TYPE_ICMPv6);
  1497. hopbyhop2->setNextElement(icmp6);
  1498. icmp6->setType(ICMPv6_ECHO);
  1499. icmp6->setCode(0);
  1500. icmp6->setIdentifier(0xabcd);
  1501. icmp6->setSequence(this->icmp_seq_counter++);
  1502. icmp6->setTargetAddress(ss6->sin6_addr); // Should still contain target's addr
  1503. ip6->setPayloadLength();
  1504. icmp6->setSum();
  1505. this->fp_probes[this->total_probes].host = this;
  1506. this->fp_probes[this->total_probes].setPacket(ip6);
  1507. this->fp_probes[this->total_probes].setProbeID("IE2");
  1508. this->fp_probes[this->total_probes].setEthernet(this->target_host->SrcMACAddress(), this->target_host->NextHopMACAddress(), this->target_host->deviceName());
  1509. this->total_probes++;
  1510. /* ICMP Probe #3: Node Info Query (IPv4 addresses) */
  1511. ip6 = new IPv6Header();
  1512. icmp6 = new ICMPv6Header();
  1513. this->target_host->SourceSockAddr(&ss, &slen);
  1514. ip6->setSourceAddress(ss6->sin6_addr);
  1515. this->target_host->TargetSockAddr(&ss, &slen);
  1516. ip6->setDestinationAddress(ss6->sin6_addr);
  1517. ip6->setFlowLabel(OSDETECT_FLOW_LABEL);
  1518. ip6->setHopLimit(get_hoplimit());
  1519. ip6->setNextHeader("ICMPv6");
  1520. ip6->setNextElement(icmp6);
  1521. icmp6->setNextElement(payload);
  1522. payload->store((u8 *) &ss6->sin6_addr, IP6_ADDR_LEN);
  1523. icmp6->setType(ICMPv6_NODEINFOQUERY);
  1524. icmp6->setCode(ICMPv6_NODEINFOQUERY_IPv6ADDR);
  1525. icmp6->setQtype(NI_QTYPE_IPv4ADDRS);
  1526. icmp6->setA();
  1527. icmp6->setNonce((u8 *) "\x01\x02\x03\x04\x05\x06\x07\x0a");
  1528. icmp6->setSum();
  1529. ip6->setPayloadLength();
  1530. this->fp_probes[this->total_probes].host = this;
  1531. this->fp_probes[this->total_probes].setPacket(ip6);
  1532. this->fp_probes[this->total_probes].setProbeID("NI");
  1533. this->fp_probes[this->total_probes].setEthernet(this->target_host->SrcMACAddress(), this->target_host->NextHopMACAddress(), this->target_host->deviceName());
  1534. this->total_probes++;
  1535. /* ICMP Probe #4: Neighbor Solicitation. (only sent to on-link targets) */
  1536. if (this->target_host->directlyConnected()) {
  1537. ip6 = new IPv6Header();
  1538. icmp6 = new ICMPv6Header();
  1539. this->target_host->SourceSockAddr(&ss, &slen);
  1540. ip6->setSourceAddress(ss6->sin6_addr);
  1541. this->target_host->TargetSockAddr(&ss, &slen);
  1542. ip6->setDestinationAddress(ss6->sin6_addr);
  1543. ip6->setFlowLabel(OSDETECT_FLOW_LABEL);
  1544. /* RFC 2461 section 7.1.1: "A node MUST silently discard any received
  1545. Neighbor Solicitation messages that do not satisfy all of the following
  1546. validity checks: - The IP Hop Limit field has a value of 255 ... */
  1547. ip6->setHopLimit(255);
  1548. ip6->setNextHeader("ICMPv6");
  1549. ip6->setNextElement(icmp6);
  1550. icmp6->setType(ICMPv6_NGHBRSOLICIT);
  1551. icmp6->setCode(0);
  1552. icmp6->setTargetAddress(ss6->sin6_addr); // Should still contain target's addr
  1553. icmp6->setSum();
  1554. ip6->setPayloadLength();
  1555. this->fp_probes[this->total_probes].host = this;
  1556. this->fp_probes[this->total_probes].setPacket(ip6);
  1557. this->fp_probes[this->total_probes].setProbeID("NS");
  1558. this->fp_probes[this->total_probes].setEthernet(this->target_host->SrcMACAddress(), this->target_host->NextHopMACAddress(), this->target_host->deviceName());
  1559. this->total_probes++;
  1560. }
  1561. /* Set UDP probes */
  1562. memset(payloadbuf, 0x43, 300);
  1563. ip6 = new IPv6Header();
  1564. udp = new UDPHeader();
  1565. payload = new RawData();
  1566. this->target_host->SourceSockAddr(&ss, &slen);
  1567. ip6->setSourceAddress(ss6->sin6_addr);
  1568. this->target_host->TargetSockAddr(&ss, &slen);
  1569. ip6->setDestinationAddress(ss6->sin6_addr);
  1570. ip6->setFlowLabel(OSDETECT_FLOW_LABEL);
  1571. ip6->setHopLimit(get_hoplimit());
  1572. ip6->setNextHeader("UDP");
  1573. ip6->setNextElement(udp);
  1574. udp->setSourcePort(this->udp_port_base);
  1575. udp->setDestinationPort(this->closed_port_udp);
  1576. payload->store((u8 *) payloadbuf, 300);
  1577. udp->setNextElement(payload);
  1578. udp->setTotalLength();
  1579. udp->setSum();
  1580. ip6->setPayloadLength(udp->getLen());
  1581. this->fp_probes[this->total_probes].host = this;
  1582. this->fp_probes[this->total_probes].setPacket(ip6);
  1583. this->fp_probes[this->total_probes].setProbeID("U1");
  1584. this->fp_probes[this->total_probes].setEthernet(this->target_host->SrcMACAddress(), this->target_host->NextHopMACAddress(), this->target_host->deviceName());
  1585. this->total_probes++;
  1586. /* Set TECN probe */
  1587. if ((TCP_DESCS[i].dstport == OPEN && this->open_port_tcp >= 0)
  1588. || (TCP_DESCS[i].dstport == CLSD && this->closed_port_tcp >= 0)) {
  1589. ip6 = make_tcp((struct sockaddr_in6 *) this->target_host->SourceSockAddr(),
  1590. (struct sockaddr_in6 *) this->target_host->TargetSockAddr(),
  1591. OSDETECT_FLOW_LABEL, TCP_DESCS[i].win, this->tcpSeqBase + i, 0,
  1592. TCP_DESCS[i].flags, tcp_port_base + i,
  1593. TCP_DESCS[i].dstport == OPEN ? this->open_port_tcp : this->closed_port_tcp,
  1594. TCP_DESCS[i].urgptr, TCP_DESCS[i].opts, TCP_DESCS[i].optslen);
  1595. /* Store the probe in the list so we can send it later */
  1596. this->fp_probes[this->total_probes].host = this;
  1597. this->fp_probes[this->total_probes].setPacket(ip6);
  1598. this->fp_probes[this->total_probes].setProbeID(TCP_DESCS[i].id);
  1599. this->fp_probes[this->total_probes].setEthernet(this->target_host->SrcMACAddress(), this->target_host->NextHopMACAddress(), this->target_host->deviceName());
  1600. this->total_probes++;
  1601. }
  1602. i++;
  1603. /* Set untimed TCP probes */
  1604. for ( ; i < NUM_FP_PROBES_IPv6_TCP; i++) {
  1605. /* If the probe is targeted to a TCP port and we don't have
  1606. * any port number for that particular state, skip the probe. */
  1607. if (TCP_DESCS[i].dstport == OPEN && this->open_port_tcp < 0)
  1608. continue;
  1609. if (TCP_DESCS[i].dstport == CLSD && this->closed_port_tcp < 0)
  1610. continue;
  1611. ip6 = make_tcp((struct sockaddr_in6 *) this->target_host->SourceSockAddr(),
  1612. (struct sockaddr_in6 *) this->target_host->TargetSockAddr(),
  1613. OSDETECT_FLOW_LABEL, TCP_DESCS[i].win, this->tcpSeqBase + i, get_random_u32(),
  1614. TCP_DESCS[i].flags, tcp_port_base + i,
  1615. TCP_DESCS[i].dstport == OPEN ? this->open_port_tcp : this->closed_port_tcp,
  1616. TCP_DESCS[i].urgptr, TCP_DESCS[i].opts, TCP_DESCS[i].optslen);
  1617. /* Store the probe in the list so we can send it later */
  1618. this->fp_probes[this->total_probes].host = this;
  1619. this->fp_probes[this->total_probes].setPacket(ip6);
  1620. this->fp_probes[this->total_probes].setProbeID(TCP_DESCS[i].id);
  1621. this->fp_probes[this->total_probes].setEthernet(this->target_host->SrcMACAddress(), this->target_host->NextHopMACAddress(), this->target_host->deviceName());
  1622. this->total_probes++;
  1623. }
  1624. return OP_SUCCESS;
  1625. }
  1626. /* Indicates whether the OS detection process has finished for this host.
  1627. * Note that when "true" is returned the caller cannot assume that the host
  1628. * has been accurately fingerprinted, only that the OS detection process
  1629. * was carried out. In other words, when true is returned it means that the
  1630. * fingerprinting engine sent all OS detection probes, performed the necessary
  1631. * retransmission and attempted to capture the target's replies. In order to
  1632. * check if the detection was successful (if we actually know what OS the target
  1633. * is running), the status() method should be used. */
  1634. bool FPHost6::done() {
  1635. if (this->probes_sent == this->total_probes) {
  1636. if (this->probes_answered + this->probes_unanswered == this->total_probes)
  1637. return true;
  1638. }
  1639. return false;
  1640. }
  1641. /* Asks the host to schedule the transmission of probes (if they need to do so).
  1642. * This method is called repeatedly by the FPEngine to make the host request
  1643. * the probe transmissions that it needs. From the hosts point of view, it
  1644. * determines if new transmissions need to be scheduled based on the number
  1645. * of probes sent, the number of answers received, etc. Also, in order to
  1646. * transmit a packet, the network controller must approve it (hosts may not
  1647. * be able to send packets any time they want due to congestion control
  1648. * restrictions). */
  1649. int FPHost6::schedule() {
  1650. struct timeval now;
  1651. unsigned int timed_probes_answered = 0;
  1652. unsigned int timed_probes_timedout = 0;
  1653. /* The first time we are asked to schedule a packet, register ourselves in
  1654. * the network controller so it can call us back when packets that match our
  1655. * target are captured. */
  1656. if (this->netctl_registered == false && this->netctl != NULL) {
  1657. this->netctl->register_caller(this);
  1658. this->netctl_registered = true;
  1659. }
  1660. /* Make sure we have things to do, otherwise, just return. */
  1661. if (this->detection_done || (this->probes_answered + this->probes_unanswered == this->total_probes)) {
  1662. /* Update our internal state to indicate we have finished */
  1663. if (!this->detection_done)
  1664. this->set_done_and_wrap_up();
  1665. return OP_SUCCESS;
  1666. }
  1667. /* If we have not yet sent the timed probes (and we have timed probes to send)
  1668. * request permission from the network controller and schedule the transmission
  1669. * for all of them, 100ms apart from each other. We don't want all the hosts
  1670. * to schedule their transmission for the same exact time so we add a random
  1671. * offset (between 0 and 100ms) to the first transmission. All subsequent
  1672. * ones are sent 100ms apart from the first. Note that if we did not find
  1673. * and open port, then we just don't send the timed probes. */
  1674. if (this->timed_probes > 0 && this->timedprobes_sent == false) {
  1675. if (o.debugging > 3)
  1676. log_write(LOG_PLAIN, "[%s] %u Tx slots requested\n", this->target_host->targetipstr(), this->timed_probes);
  1677. if (this->netctl->request_slots(this->timed_probes) == true) {
  1678. if (o.debugging > 3)
  1679. log_write(LOG_PLAIN, "[%s] Slots granted!\n", this->target_host->targetipstr());
  1680. this->timedprobes_sent = true;
  1681. int whentostart = get_random_u16()%100;
  1682. for (size_t i = 0; i < this->timed_probes; i++) {
  1683. this->netctl->scheduleProbe(&(this->fp_probes[i]), whentostart + i*100);
  1684. this->probes_sent++;
  1685. }
  1686. return OP_SUCCESS;
  1687. }
  1688. if (o.debugging > 3)
  1689. log_write(LOG_PLAIN, "[%s] Slots denied.\n", this->target_host->targetipstr());
  1690. return OP_FAILURE;
  1691. } else if (this->timed_probes > 0 && this->timedprobes_sent && this->fp_probes[this->timed_probes - 1].getTimeSent().tv_sec == 0) {
  1692. /* If the sent time for the last timed probe has not been set, it means
  1693. * that we haven't sent all the timed probes yet, so we don't schedule
  1694. * any other probes, we just wait until our schedule() gets called again.
  1695. * We do this because we don't want to mess with the target's stack
  1696. * in the middle of our timed probes. Otherwise, we can screw up the
  1697. * TCP sequence generation tests, etc. We also get here when timed probes
  1698. * suffer a retransmission. In that case, we also stop sending packets
  1699. * to our target until we have sent all of them. */
  1700. if (o.debugging > 3)
  1701. log_write(LOG_PLAIN, "[%s] Waiting for all timed probes to be sent...\n", this->target_host->targetipstr());
  1702. return OP_FAILURE;
  1703. } else {
  1704. /* If we get here it means that either we have sent all the timed probes or
  1705. * we don't even have to send them (because no open port was found).
  1706. * At this point if we have other probes to transmit, schedule the next one.
  1707. * Also, check for timedout probes so we can retransmit one of them. */
  1708. if (o.debugging > 3 && this->timed_probes > 0 && this->probes_sent == this->timed_probes)
  1709. log_write(LOG_PLAIN, "[%s] All timed probes have been sent.\n", this->target_host->targetipstr());
  1710. if (this->probes_sent < this->total_probes) {
  1711. if (this->netctl->request_slots(1) == true) {
  1712. if (o.debugging > 3)
  1713. log_write(LOG_PLAIN, "[%s] Scheduling probe %s\n", this->target_host->targetipstr(), this->fp_probes[this->probes_sent].getProbeID());
  1714. this->netctl->scheduleProbe(&(this->fp_probes[this->probes_sent]), 0);
  1715. this->probes_sent++;
  1716. } else {
  1717. if (o.debugging > 3)
  1718. log_write(LOG_PLAIN, "[%s] Can't schedule probe %s\n", this->target_host->targetipstr(), this->fp_probes[this->probes_sent].getProbeID());
  1719. }
  1720. }
  1721. /**************************************************************************
  1722. * PROBE TIMEOUT HANDLING *
  1723. **************************************************************************/
  1724. if (o.debugging > 3)
  1725. log_write(LOG_PLAIN, "[%s] Checking for regular probe timeouts...\n", this->target_host->targetipstr());
  1726. /* Determine if some regular probe (not timed probes) has timedout. In that
  1727. * case, choose some outstanding probe to retransmit. */
  1728. gettimeofday(&now, NULL);
  1729. for (unsigned int i = this->timed_probes; i < this->probes_sent; i++) {
  1730. /* Skip probes that have already been answered */
  1731. if (this->fp_responses[i]) {
  1732. continue;
  1733. }
  1734. /* Skip probes that we have scheduled but have not been yet transmitted */
  1735. if (this->fp_probes[i].getTimeSent().tv_sec == 0)
  1736. continue;
  1737. /* Skip probes for which we didn't get a response after all
  1738. * retransmissions. */
  1739. if (this->fp_probes[i].probeFailed()) {
  1740. continue;
  1741. }
  1742. /* Check if the probe timedout */
  1743. if (TIMEVAL_SUBTRACT(now, this->fp_probes[i].getTimeSent()) >= this->rto) {
  1744. /* If we have reached the maximum number of retransmissions, mark the
  1745. * probe as failed. Otherwise, schedule its transmission. */
  1746. if (this->fp_probes[i].getRetransmissions() >= o.maxOSTries()) {
  1747. if (o.debugging > 3) {
  1748. log_write(LOG_PLAIN, "[%s] Probe #%d (%s) failed after %d retransmissions.\n",
  1749. this->target_host->targetipstr(), i, this->fp_probes[i].getProbeID(),
  1750. this->fp_probes[i].getRetransmissions());
  1751. }
  1752. this->fp_probes[i].setFailed();
  1753. /* Let the network controller know that we don't expect a response
  1754. * for the probe anymore so the number of outstanding probes is
  1755. * reduced and the effective window is incremented. */
  1756. this->netctl->cc_report_final_timeout();
  1757. /* Also, increase our unanswered counter so we can later decide
  1758. * if the process has finished. */
  1759. this->probes_unanswered++;
  1760. continue;
  1761. /* Otherwise, retransmit the packet.*/
  1762. } else {
  1763. /* Note that we do not request permission to re-transmit (we don't
  1764. * call request_slots(). In TCP one can retransmit timedout
  1765. * probes even when CWND is zero, as CWND only applies for new packets. */
  1766. if (o.debugging > 3) {
  1767. log_write(LOG_PLAIN, "[%s] Retransmitting probe #%d (%s) (retransmitted %d times already).\n",
  1768. this->target_host->targetipstr(), i, this->fp_probes[i].getProbeID(),
  1769. this->fp_probes[i].getRetransmissions());
  1770. }
  1771. this->fp_probes[i].incrementRetransmissions();
  1772. this->netctl->scheduleProbe(&(this->fp_probes[i]), 0);
  1773. break;
  1774. }
  1775. }
  1776. }
  1777. /* Now let's check the state of the timed probes. We iterate over the list
  1778. * of timed probes to count how many have been answered and how many have
  1779. * timed out. If answered + timeout == total_timed_probes, it's time to
  1780. * retransmit them. */
  1781. /* Make sure we are actually sending timed probes. */
  1782. if (this->timed_probes <= 0)
  1783. return OP_SUCCESS;
  1784. bool timed_failed = false;
  1785. if (o.debugging > 3)
  1786. log_write(LOG_PLAIN, "[%s] Checking for timed probe timeouts...\n", this->target_host->targetipstr());
  1787. for (unsigned int i = 0; i < this->timed_probes; i++) {
  1788. assert(this->fp_probes[i].isTimed());
  1789. /* Skip probes that have already been answered, but count how many of
  1790. * them are there. */
  1791. if (this->fp_responses[i]) {
  1792. timed_probes_answered++;
  1793. continue;
  1794. }
  1795. /* If there is some timed probe for which we have already scheduled its
  1796. * retransmission but it hasn't been sent yet, break the loop. We don't
  1797. * have to worry about retransmitting these probes yet.*/
  1798. if (this->fp_probes[i].getTimeSent().tv_sec == 0)
  1799. return OP_SUCCESS;
  1800. /* If we got a total timeout for any of the timed probes, we shouldn't
  1801. * attempt more retransmissions. We set a flag to indicate that but we
  1802. * still stay in the loop because we want to mark as "failed" any other
  1803. * probes we have not yet checked. */
  1804. if (this->fp_probes[i].probeFailed()) {
  1805. timed_failed = true;
  1806. continue;
  1807. }
  1808. /* Now check if the timed probe has timed out. If it suffered a total
  1809. * time out (max retransmissions done and still no answer) then mark
  1810. * it as such. Otherwise, count it so we can retransmit the whole
  1811. * group of timed probes later if appropriate. */
  1812. if (TIMEVAL_SUBTRACT(now, this->fp_probes[i].getTimeSent()) >= this->rto) {
  1813. if (o.debugging > 3) {
  1814. log_write(LOG_PLAIN, "[%s] timed probe %d (%s) timedout\n",
  1815. this->target_host->targetipstr(), i, this->fp_probes[i].getProbeID());
  1816. }
  1817. if (this->fp_probes[i].getRetransmissions() >= o.maxOSTries()) {
  1818. if (o.debugging > 3)
  1819. log_write(LOG_PLAIN, "[%s] Timed probe #%d (%s) failed after %d retransmissions.\n", this->target_host->targetipstr(), i, this->fp_probes[i].getProbeID(), this->fp_probes[i].getRetransmissions());
  1820. this->fp_probes[i].setFailed();
  1821. /* Let the network controller know that we don't expect a response
  1822. * for the probe anymore so the number of outstanding probes is
  1823. * reduced and the effective window is incremented. */
  1824. this->netctl->cc_report_final_timeout();
  1825. /* Also, increase our unanswered counter so we can later decide
  1826. * if the process has finished. */
  1827. this->probes_unanswered++;
  1828. } else {
  1829. if (o.debugging > 3)
  1830. log_write(LOG_PLAIN, "[%s] Timed probe #%d (%s) has timed out (%d retransmissions done).\n", this->target_host->targetipstr(), i, this->fp_probes[i].getProbeID(), this->fp_probes[i].getRetransmissions());
  1831. timed_probes_timedout++;
  1832. }
  1833. }
  1834. }
  1835. if (o.debugging > 3)
  1836. log_write(LOG_PLAIN, "[%s] Timed_probes=%d, answered=%u, timedout=%u\n", this->target_host->targetipstr(), this->timed_probes, timed_probes_answered, timed_probes_timedout);
  1837. /* If the probe that has timed out is a "timed probe" it means that
  1838. * we need to retransmit all timed probes, not only this one. For
  1839. * that, we wait until all timed probes have either timed out or
  1840. * been responded. When that happens, we do the following:
  1841. * 1) Store the responses we have received the last time we sent
  1842. * the timed probes in an aux array (this->aux_resp).
  1843. * 2) Clear the responses to the timed probes from the main
  1844. * response array (this->fp_responses).
  1845. * 3) Schedule the retransmission of all timed probes, 100ms apart. */
  1846. if (this->timed_probes > 0 && timed_failed == false && timed_probes_timedout > 0 && (timed_probes_answered + timed_probes_timedout == this->timed_probes)) {
  1847. /* Count the number of responses we have now and the number
  1848. * of responses we stored in the aux buffer last time. */
  1849. unsigned int responses_stored = 0;
  1850. unsigned int responses_now = 0;
  1851. for (unsigned int j = 0; j < this->timed_probes; j++) {
  1852. if (this->aux_resp[j] != NULL)
  1853. responses_stored++;
  1854. if (this->fp_responses[j] != NULL)
  1855. responses_now++;
  1856. }
  1857. /* If now we have more responses than before, copy our current
  1858. * set of responses to the aux array. Otherwise, just
  1859. * delete the current set of responses. */
  1860. for (unsigned int k = 0; k < this->timed_probes; k++) {
  1861. if (responses_now > responses_stored) {
  1862. /* Free previous allocations */
  1863. if (this->aux_resp[k] != NULL) {
  1864. delete this->aux_resp[k];
  1865. }
  1866. /* Move the current response to the aux array */
  1867. this->aux_resp[k] = this->fp_responses[k];
  1868. this->fp_responses[k] = NULL;
  1869. } else {
  1870. delete this->fp_responses[k];
  1871. this->fp_responses[k] = NULL;
  1872. }
  1873. }
  1874. /* Update answer count because now we expect new answers to the timed probes. */
  1875. assert(((int)this->probes_answered - (int)timed_probes_answered) >= 0);
  1876. this->probes_answered-= timed_probes_answered;
  1877. if (o.debugging > 3)
  1878. log_write(LOG_PLAIN, "[%s] Adjusting answer count: before=%d, after=%d\n", this->target_host->targetipstr(), this->probes_answered + timed_probes_answered, this->probes_answered);
  1879. /* Finally do the actual retransmission. Like the first time,
  1880. * we schedule them 100ms apart, starting at same random point
  1881. * between right now and 99ms. */
  1882. int whentostart = get_random_u16()%100;
  1883. for (size_t l = 0; l < this->timed_probes; l++) {
  1884. this->fp_probes[l].incrementRetransmissions();
  1885. this->netctl->scheduleProbe(&(this->fp_probes[l]), whentostart + l*100);
  1886. }
  1887. if (o.debugging > 3 && this->timed_probes > 0)
  1888. log_write(LOG_PLAIN, "[%s] Retransmitting timed probes (rcvd_before=%u, rcvd_now=%u times=%d).\n", this->target_host->targetipstr(), responses_stored, responses_now, this->fp_probes[0].getRetransmissions());
  1889. /* Reset our local counters. */
  1890. timed_probes_answered = 0;
  1891. timed_probes_timedout = 0;
  1892. }
  1893. }
  1894. return OP_FAILURE;
  1895. }
  1896. /* This method is called when we detect that the OS detection process for this
  1897. * host is completed. It basically updates the host's internal state to
  1898. * indicate that the processed finished and unregisters the host from the
  1899. * network controller so we don't get any more callbacks. Here we also handle
  1900. * the special case of the retransmitted "timed probes". When we have to
  1901. * retransmit such probes, we usually have two sets of responses: the ones we
  1902. * got for the last retransmission, and the ones we got in the best try before
  1903. * that. So what we have to do is to decide which set is the best and discard
  1904. * the other one.*/
  1905. int FPHost6::set_done_and_wrap_up() {
  1906. assert(this->probes_answered + this->probes_unanswered == this->total_probes);
  1907. /* Inform the network controller that we do not wish to continue
  1908. * receiving callbacks (it could happen if the system had some other
  1909. * connections established with the target) */
  1910. this->netctl->unregister_caller(this);
  1911. /* Set up an internal flag to indicate we have finished */
  1912. this->detection_done = true;
  1913. /* Check the state of the timed probe retransmissions. In particular if we
  1914. * retransmitted timed probes, we should have two sets of responses,
  1915. * the ones we got last time we retransmitted, and the best set of responses
  1916. * we got out of all previous retransmissions but the last one. So now, we
  1917. * determine which set is the best and discard the other one. Btw, none of
  1918. * these loops run if timed_probes == 0, so it's safe in all cases. */
  1919. /* First count the number of responses in each set. */
  1920. unsigned int stored = 0;
  1921. unsigned int current = 0;
  1922. for (unsigned int i = 0; i < this->timed_probes; i++) {
  1923. if (this->aux_resp[i] != NULL)
  1924. stored++;
  1925. if (this->fp_responses[i] != NULL)
  1926. current++;
  1927. }
  1928. /* If we got more responses in a previous try, use them and get rid of
  1929. * the current ones. */
  1930. if (stored > current) {
  1931. for (unsigned int i = 0; i < this->timed_probes; i++) {
  1932. if (this->fp_responses[i] != NULL)
  1933. delete this->fp_responses[i];
  1934. this->fp_responses[i] = this->aux_resp[i];
  1935. this->aux_resp[i] = NULL;
  1936. }
  1937. /* Otherwise, get rid of the stored responses, use the current set */
  1938. } else {
  1939. for (unsigned int i = 0; i < this->timed_probes; i++) {
  1940. if (this->aux_resp[i] != NULL) {
  1941. delete this->aux_resp[i];
  1942. this->aux_resp[i] = NULL;
  1943. }
  1944. }
  1945. }
  1946. return OP_SUCCESS;
  1947. }
  1948. /* This function is called by the network controller every time a packet of
  1949. * interest is captured. A "packet of interest" is a packet whose source
  1950. * address matches the IP address of the target associated with the FPHost
  1951. * instance. Inside the method, the received packet is processed in order to
  1952. * determine if it corresponds to a response to a previous FPProbe sent to
  1953. * that target. If the packet is a proper response, it will be stored for
  1954. * later processing, as it is part of the target's stack fingerprint. Returns
  1955. * a positive integer when the supplied packet could be successfully matched with
  1956. * a previously sent probe. The returned value indicates how many times the
  1957. * probe was sent before getting a reply: a return value of 1 means that we
  1958. * got a normal reply, value two means that we had to retransmit the packet
  1959. * once to get the reply, and so on. A return value of zero is a special case
  1960. * that indicates that the supplied packet is a response to a timed probed
  1961. * for which we already had received a reply in the past. This is necessary
  1962. * because we need to indicate the network controller that this is not a normal
  1963. * response to a retransmitted probe, and so, it should not be used to alter
  1964. * congestion control parameters. A negative return value indicates that the
  1965. * supplied packet is not a response to any probe sent by this host. */
  1966. int FPHost6::callback(const u8 *pkt, size_t pkt_len, const struct timeval *tv) {
  1967. PacketElement *rcvd = NULL;
  1968. /* Dummy packet to ensure destruction of rcvd. */
  1969. FPPacket dummy;
  1970. bool match_found = false;
  1971. int times_tx = 0;
  1972. /* Make sure we still expect callbacks */
  1973. if (this->detection_done)
  1974. return -1;
  1975. if (o.debugging > 3)
  1976. log_write(LOG_PLAIN, "[%s] Captured %lu bytes\n", this->target_host->targetipstr(), (unsigned long)pkt_len);
  1977. /* Convert the ugly raw buffer into a nice chain of PacketElement objects, so
  1978. * it's easier to parse the captured packet */
  1979. if ((rcvd = PacketParser::split(pkt, pkt_len, false)) == NULL)
  1980. return -2;
  1981. dummy.setPacket(rcvd);
  1982. /* Iterate over the list of sent probes and determine if the captured
  1983. * packet is a response to one of them. */
  1984. for (unsigned int i = 0; i < this->probes_sent; i++) {
  1985. /* Skip probes for which we already got a response */
  1986. if (this->fp_responses[i])
  1987. continue;
  1988. /* See if the received packet is a response to a probe */
  1989. if (this->fp_probes[i].isResponse(rcvd)) {
  1990. struct timeval now, time_sent;
  1991. gettimeofday(&now, NULL);
  1992. this->fp_responses[i] = new FPResponse(this->fp_probes[i].getProbeID(),
  1993. pkt, pkt_len, fp_probes[i].getTimeSent(), *tv);
  1994. this->fp_probes[i].incrementReplies();
  1995. match_found = true;
  1996. /* If the response that we've received is for a timed probe, we
  1997. * need to do a special handling. We don't want to report that
  1998. * we've received a response after N retransmissions because we
  1999. * may have re-sent the packet even if we got a response in the past.
  2000. * This happens when one of the timed probes times out and we
  2001. * retransmit all of them. We don't want the network controller to
  2002. * think there is congestion, so we only return the number of
  2003. * retransmissions if we didn't get a response before and we did now. */
  2004. if (this->fp_probes[i].isTimed() && this->fp_probes[i].getRetransmissions() > 0 && this->fp_probes[i].getReplies() > 1) {
  2005. times_tx = 0; // Special case.
  2006. } else {
  2007. times_tx = this->fp_probes[i].getRetransmissions()+1;
  2008. }
  2009. this->probes_answered++;
  2010. /* Recompute the Retransmission Timeout based on this new RTT observation. */
  2011. time_sent = this->fp_probes[i].getTimeSent();
  2012. assert(time_sent.tv_sec > 0);
  2013. this->update_RTO(TIMEVAL_SUBTRACT(now, time_sent), this->fp_probes[i].getRetransmissions() != 0);
  2014. break;
  2015. }
  2016. }
  2017. if (match_found) {
  2018. if (o.packetTrace()) {
  2019. log_write(LOG_PLAIN, "RCVD ");
  2020. rcvd->print(stdout, LOW_DETAIL);
  2021. log_write(LOG_PLAIN, "\n");
  2022. }
  2023. /* Here, check if with this match we completed the OS detection */
  2024. if (this->probes_answered + this->probes_unanswered == this->total_probes) {
  2025. /* Update our internal state to indicate we have finished */
  2026. this->set_done_and_wrap_up();
  2027. }
  2028. /* Return the number of times that the packet was transmitted before
  2029. * getting the reply. */
  2030. return times_tx;
  2031. } else {
  2032. return -3;
  2033. }
  2034. }
  2035. const FPProbe *FPHost6::getProbe(const char *id) {
  2036. unsigned int i;
  2037. for (i = 0; i < NUM_FP_PROBES_IPv6; i++) {
  2038. if (!this->fp_probes[i].is_set())
  2039. continue;
  2040. if (strcmp(this->fp_probes[i].getProbeID(), id) == 0)
  2041. return &this->fp_probes[i];
  2042. }
  2043. return NULL;
  2044. }
  2045. const FPResponse *FPHost6::getResponse(const char *id) {
  2046. unsigned int i;
  2047. for (i = 0; i < NUM_FP_PROBES_IPv6; i++) {
  2048. if (this->fp_responses[i] == NULL)
  2049. continue;
  2050. if (strcmp(this->fp_responses[i]->probe_id, id) == 0)
  2051. return this->fp_responses[i];
  2052. }
  2053. return NULL;
  2054. }
  2055. /******************************************************************************
  2056. * Implementation of class FPPacket. *
  2057. ******************************************************************************/
  2058. FPPacket::FPPacket() {
  2059. this->pkt = NULL;
  2060. this->__reset();
  2061. }
  2062. FPPacket::~FPPacket() {
  2063. this->__reset();
  2064. }
  2065. /* Resets all internal state, freeing any previously stored packets */
  2066. void FPPacket::__reset() {
  2067. this->link_eth = false;
  2068. memset(&(this->eth_hdr), 0, sizeof(struct eth_nfo));
  2069. PacketElement *me = this->pkt, *aux = NULL;
  2070. while (me != NULL) {
  2071. aux = me->getNextElement();
  2072. delete me;
  2073. me = aux;
  2074. }
  2075. this->pkt = NULL;
  2076. memset(&this->pkt_time, 0, sizeof(struct timeval));
  2077. }
  2078. /* Returns true if the FPPacket has been associated with a packet (through a
  2079. * call to setPacket(). This is equivalent to the following conditional:
  2080. * fppacket.getPacket() != NULL */
  2081. bool FPPacket::is_set() const {
  2082. if (this->pkt != NULL)
  2083. return true;
  2084. else
  2085. return false;
  2086. }
  2087. /* Associates de FPPacket instance with the first protocol header of a networkj
  2088. * packet. Such header may be linked to others through the setNextElement()
  2089. * mechanism. Note that FPPacket does NOT make a copy of the contents of the
  2090. * supplied pointer, it just stores the memory address. Therefore, the caller
  2091. * MUST ensure that the supplied pointer remains valid during the lifetime of
  2092. * the FPPacket instance.
  2093. *
  2094. * After calling this function, the FPPacket takes ownership of pkt and will
  2095. * delete pkt in its destructor. */
  2096. int FPPacket::setPacket(PacketElement *pkt) {
  2097. assert(pkt != NULL);
  2098. this->pkt = pkt;
  2099. return OP_SUCCESS;
  2100. }
  2101. /* Returns a newly allocated byte array with packet contents. The caller is
  2102. * responsible for freeing the buffer. */
  2103. u8 *FPPacket::getPacketBuffer(size_t *pkt_len) const {
  2104. u8 *pkt_buff;
  2105. pkt_buff = (u8 *)safe_malloc(this->pkt->getLen());
  2106. this->pkt->dumpToBinaryBuffer(pkt_buff, this->pkt->getLen());
  2107. *pkt_len = (size_t)this->pkt->getLen();
  2108. return pkt_buff;
  2109. }
  2110. /* Returns a pointer to first header of the packet associated with the FPPacket
  2111. * instance. Note that this method will return NULL unless a previous call to
  2112. * setPacket() has been made. */
  2113. const PacketElement *FPPacket::getPacket() const {
  2114. return this->pkt;
  2115. }
  2116. /* Returns the length of the packet associated with the FPPacket instance. Note
  2117. * that this method will return zero unless an actual packet was associated
  2118. * with the FPPacket object through a call to setPacket(). */
  2119. size_t FPPacket::getLength() const {
  2120. if (this->pkt != NULL)
  2121. return this->pkt->getLen();
  2122. else
  2123. return 0;
  2124. }
  2125. /* This method associates some link layer information with the packet. If
  2126. * sending at the ethernet level is not required, just call it passing NULL
  2127. * values, like this: instance.setEthernet(NULL, NULL, NULL);
  2128. * Otherwise, pass the source address, the next hop address and the name of
  2129. * the network interface the packet should be injected through. */
  2130. int FPPacket::setEthernet(const u8 *src_mac, const u8 *dst_mac, const char *devname) {
  2131. if (src_mac == NULL || dst_mac == NULL) {
  2132. memset(&(this->eth_hdr), 0, sizeof(struct eth_nfo));
  2133. this->link_eth = false;
  2134. return OP_FAILURE;
  2135. }
  2136. memcpy(this->eth_hdr.srcmac, src_mac, 6);
  2137. memcpy(this->eth_hdr.dstmac, dst_mac, 6);
  2138. this->link_eth = true;
  2139. if (devname != NULL) {
  2140. strncpy(this->eth_hdr.devname, devname, sizeof(this->eth_hdr.devname)-1);
  2141. if ((this->eth_hdr.ethsd = eth_open_cached(devname)) == NULL)
  2142. fatal("%s: Failed to open ethernet device (%s)", __func__, devname);
  2143. } else {
  2144. this->eth_hdr.devname[0] = '\0';
  2145. this->eth_hdr.ethsd = NULL;
  2146. }
  2147. return OP_SUCCESS;
  2148. }
  2149. /* Returns an eth_nfo structure that contains the necessary parameters to
  2150. * allow the transmission of the packet at the Ethernet level. Note that
  2151. * such structure is only returned if a previous call to setEthernet() has
  2152. * been made. If it hasn't, this means that the packet should be sent at
  2153. * the IP layer, and only NULL will be returned. */
  2154. const struct eth_nfo *FPPacket::getEthernet() const {
  2155. if (this->link_eth == true)
  2156. return &(this->eth_hdr);
  2157. else
  2158. return NULL;
  2159. }
  2160. /* Sets the internal time holder to the current time. */
  2161. int FPPacket::setTime(const struct timeval *tv) {
  2162. if (tv != NULL) {
  2163. this->pkt_time = *tv;
  2164. return 0;
  2165. } else {
  2166. return gettimeofday(&this->pkt_time, NULL);
  2167. }
  2168. }
  2169. /* Returns the value of the internal time holder */
  2170. struct timeval FPPacket::getTime() const {
  2171. return this->pkt_time;
  2172. }
  2173. /* Sets the internal time holder to zero. */
  2174. int FPPacket::resetTime() {
  2175. memset(&this->pkt_time, 0, sizeof(struct timeval));
  2176. return OP_SUCCESS;
  2177. }
  2178. /******************************************************************************
  2179. * Implementation of class FPProbe. *
  2180. ******************************************************************************/
  2181. FPProbe::FPProbe() {
  2182. this->probe_id = NULL;
  2183. this->host = NULL;
  2184. this->reset();
  2185. }
  2186. FPProbe::~FPProbe() {
  2187. if (this->probe_id != NULL)
  2188. free(this->probe_id);
  2189. }
  2190. void FPProbe::reset() {
  2191. this->probe_no = 0;
  2192. this->retransmissions = 0;
  2193. this->times_replied = 0;
  2194. this->failed = false;
  2195. this->timed = false;
  2196. if (this->probe_id != NULL)
  2197. free(this->probe_id);
  2198. this->probe_id = NULL;
  2199. /* Also call FPPacket::__reset() to free any exising packet information */
  2200. this->__reset();
  2201. }
  2202. /* Returns true if the supplied packet is a response to this FPProbe. This
  2203. * method handles IPv4, IPv6, ICMPv4, ICMPv6, TCP and UDP. Basically it uses
  2204. * PacketParser::is_response(). Check there for a list of matched packets and
  2205. * some usage examples.*/
  2206. bool FPProbe::isResponse(PacketElement *rcvd) {
  2207. /* If we don't have a record of even sending this probe, no packet can be a
  2208. response. */
  2209. if (this->pkt_time.tv_sec == 0 && this->pkt_time.tv_usec == 0)
  2210. return false;
  2211. return PacketParser::is_response(this->pkt, rcvd);
  2212. }
  2213. /* Store this probe's textual identifier. Note that this method makes a copy
  2214. * of the supplied string, so you can safely change its contents without
  2215. * affecting the object's state. */
  2216. int FPProbe::setProbeID(const char *id) {
  2217. this->probe_id = strdup(id);
  2218. return OP_SUCCESS;
  2219. }
  2220. /* Returns a pointer to probe's textual identifier. */
  2221. const char *FPProbe::getProbeID() const {
  2222. return this->probe_id;
  2223. }
  2224. /* Returns the number of times the probe has been scheduled for retransmission. */
  2225. int FPProbe::getRetransmissions() const {
  2226. return this->retransmissions;
  2227. }
  2228. /* Increment the number of times the probe has been scheduled for retransmission
  2229. * by one unit. It returns the current value of the retransmission counter. */
  2230. int FPProbe::incrementRetransmissions() {
  2231. this->retransmissions++;
  2232. return this->retransmissions;
  2233. }
  2234. /* Returns the number of times the probe has been replied. This applies for
  2235. * timed probes, which may be retransmitted even if we got a reply (becase
  2236. * another timed probe timeout and we had to retransmit all of them to keep
  2237. * the timing accurate). */
  2238. int FPProbe::getReplies() const {
  2239. return this->times_replied;
  2240. }
  2241. /* Increment the number of times the probe has been replied. It returns the
  2242. * current value of the reply counter. */
  2243. int FPProbe::incrementReplies() {
  2244. this->times_replied++;
  2245. return this->times_replied;
  2246. }
  2247. /* Sets the time at which the probe was sent */
  2248. int FPProbe::setTimeSent() {
  2249. return this->setTime();
  2250. }
  2251. /* Returns the time at which te packet was sent */
  2252. struct timeval FPProbe::getTimeSent() const {
  2253. return this->getTime();
  2254. }
  2255. /* Sets the time at which the probe was sent to zero. */
  2256. int FPProbe::resetTimeSent() {
  2257. return this->resetTime();
  2258. }
  2259. /* Returns true if this FPProbe did not receive any response after all
  2260. * necessary retransmissions. When it returns true, callers should not
  2261. * attempt to change the state of the FPProbe. */
  2262. bool FPProbe::probeFailed() const {
  2263. return this->failed;
  2264. }
  2265. /* This method should be called when the probe has been retransmitted as many
  2266. * times as we could and it still timed out without a response. Once this
  2267. * method is called, the state is irreversible (unless a call to FPProbe::reset()
  2268. * is made, in which case all internal state disappears) */
  2269. int FPProbe::setFailed() {
  2270. this->failed = true;
  2271. return OP_SUCCESS;
  2272. }
  2273. /* Returns true if the probe is one of the "timed probes". */
  2274. bool FPProbe::isTimed() const {
  2275. return this->timed;
  2276. }
  2277. /* Marks the probe as "timed". This is used to indicate that this probe has
  2278. * specific timing requirements (it must be sent exactly 100ms after the
  2279. * previous probe)., */
  2280. int FPProbe::setTimed() {
  2281. this->timed = true;
  2282. return OP_SUCCESS;
  2283. }
  2284. /******************************************************************************
  2285. * Implementation of class FPResponse. *
  2286. ******************************************************************************/
  2287. FPResponse::FPResponse(const char *probe_id, const u8 *buf, size_t len,
  2288. struct timeval senttime, struct timeval rcvdtime) {
  2289. this->probe_id = string_pool_insert(probe_id);
  2290. this->buf = (u8 *) safe_malloc(len);
  2291. memcpy(this->buf, buf, len);
  2292. this->len = len;
  2293. this->senttime = senttime;
  2294. this->rcvdtime = rcvdtime;
  2295. }
  2296. FPResponse::~FPResponse() {
  2297. free(buf);
  2298. }
  2299. /******************************************************************************
  2300. * Nsock handler wrappers. *
  2301. ******************************************************************************/
  2302. /* This handler is a wrapper for the FPNetworkControl::probe_transmission_handler()
  2303. * method. We need this because C++ does not allow to use class methods as
  2304. * callback functions for things like signal() or the Nsock lib. */
  2305. void probe_transmission_handler_wrapper(nsock_pool nsp, nsock_event nse, void *arg) {
  2306. global_netctl.probe_transmission_handler(nsp, nse, arg);
  2307. return;
  2308. }
  2309. /* This handler is a wrapper for the FPNetworkControl:response_reception_handler()
  2310. * method. We need this because C++ does not allow to use class methods as
  2311. * callback functions for things like signal() or the Nsock lib. */
  2312. void response_reception_handler_wrapper(nsock_pool nsp, nsock_event nse, void *arg) {
  2313. global_netctl.response_reception_handler(nsp, nse, arg);
  2314. return;
  2315. }