module_icmp_echo_time.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * ZMap Copyright 2013 Regents of the University of Michigan
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy
  6. * of the License at http://www.apache.org/licenses/LICENSE-2.0
  7. */
  8. // probe module for performing ICMP echo request (ping) scans that allows calculation
  9. // of RTT
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <stdint.h>
  13. #include <time.h>
  14. #include <unistd.h>
  15. #include <string.h>
  16. #include <sys/time.h>
  17. #include "../../lib/includes.h"
  18. #include "probe_modules.h"
  19. #include "../fieldset.h"
  20. #include "packet.h"
  21. #include "validate.h"
  22. #define ICMP_SMALLEST_SIZE 5
  23. #define ICMP_TIMXCEED_UNREACH_HEADER_SIZE 8
  24. probe_module_t module_icmp_echo_time;
  25. struct icmp_payload_for_rtt {
  26. uint32_t sent_tv_sec;
  27. uint32_t sent_tv_usec;
  28. ipaddr_n_t dst;
  29. };
  30. static int icmp_echo_init_perthread(void* buf, macaddr_t *src,
  31. macaddr_t *gw, __attribute__((unused)) port_h_t dst_port,
  32. __attribute__((unused)) void **arg_ptr)
  33. {
  34. memset(buf, 0, MAX_PACKET_SIZE);
  35. struct ether_header *eth_header = (struct ether_header *) buf;
  36. make_eth_header(eth_header, src, gw);
  37. struct ip *ip_header = (struct ip *) (&eth_header[1]);
  38. uint16_t len = htons(sizeof(struct ip) + sizeof(struct icmp) - 8);
  39. make_ip_header(ip_header, IPPROTO_ICMP, len);
  40. struct icmp *icmp_header = (struct icmp*)(&ip_header[1]);
  41. make_icmp_header(icmp_header);
  42. return EXIT_SUCCESS;
  43. }
  44. static int icmp_echo_make_packet(void *buf, ipaddr_n_t src_ip, ipaddr_n_t dst_ip,
  45. uint32_t *validation, __attribute__((unused)) int probe_num,
  46. __attribute__((unused)) void *arg)
  47. {
  48. struct ether_header *eth_header = (struct ether_header *) buf;
  49. struct ip *ip_header = (struct ip *)(&eth_header[1]);
  50. struct icmp *icmp_header = (struct icmp*)(&ip_header[1]);
  51. struct icmp_payload_for_rtt *payload = (struct icmp_payload_for_rtt*)(((char *)icmp_header) + 8);
  52. uint16_t icmp_idnum = validation[2] & 0xFFFF;
  53. struct timeval tv;
  54. ip_header->ip_src.s_addr = src_ip;
  55. ip_header->ip_dst.s_addr = dst_ip;
  56. icmp_header->icmp_id = icmp_idnum;
  57. gettimeofday(&tv, NULL);
  58. payload->sent_tv_sec = tv.tv_sec;
  59. payload->sent_tv_usec = tv.tv_usec;
  60. payload->dst = dst_ip;
  61. icmp_header->icmp_cksum = 0;
  62. icmp_header->icmp_cksum = icmp_checksum((unsigned short *) icmp_header);
  63. ip_header->ip_sum = 0;
  64. ip_header->ip_sum = zmap_ip_checksum((unsigned short *) ip_header);
  65. return EXIT_SUCCESS;
  66. }
  67. static void icmp_echo_print_packet(FILE *fp, void* packet)
  68. {
  69. struct ether_header *ethh = (struct ether_header *) packet;
  70. struct ip *iph = (struct ip *) &ethh[1];
  71. struct icmp *icmp_header = (struct icmp*) (&iph[1]);
  72. fprintf(fp, "icmp { type: %u | code: %u "
  73. "| checksum: %#04X | id: %u | seq: %u }\n",
  74. icmp_header->icmp_type,
  75. icmp_header->icmp_code,
  76. ntohs(icmp_header->icmp_cksum),
  77. ntohs(icmp_header->icmp_id),
  78. ntohs(icmp_header->icmp_seq));
  79. fprintf_ip_header(fp, iph);
  80. fprintf_eth_header(fp, ethh);
  81. fprintf(fp, "------------------------------------------------------\n");
  82. }
  83. static int icmp_validate_packet(const struct ip *ip_hdr,
  84. uint32_t len, uint32_t *src_ip, uint32_t *validation)
  85. {
  86. if (ip_hdr->ip_p != IPPROTO_ICMP) {
  87. return 0;
  88. }
  89. if (((uint32_t) 4 * ip_hdr->ip_hl + ICMP_SMALLEST_SIZE) > len) {
  90. // buffer not large enough to contain expected icmp header
  91. return 0;
  92. }
  93. struct icmp *icmp_h = (struct icmp *) ((char *) ip_hdr + 4*ip_hdr->ip_hl);
  94. uint16_t icmp_idnum = icmp_h->icmp_id;
  95. // ICMP validation is tricky: for some packet types, we must look inside
  96. // the payload
  97. if (icmp_h->icmp_type == ICMP_TIMXCEED || icmp_h->icmp_type == ICMP_UNREACH) {
  98. // Should have 16B TimeExceeded/Dest_Unreachable header + original IP header
  99. // + 1st 8B of original ICMP frame
  100. if ((4*ip_hdr->ip_hl + ICMP_TIMXCEED_UNREACH_HEADER_SIZE +
  101. sizeof(struct ip)) > len) {
  102. return 0;
  103. }
  104. struct ip *ip_inner = (struct ip *)(icmp_h + 1);
  105. if (((uint32_t) 4 * ip_hdr->ip_hl + ICMP_TIMXCEED_UNREACH_HEADER_SIZE +
  106. 4*ip_inner->ip_hl + 8 /*1st 8 bytes of original*/ ) > len) {
  107. return 0;
  108. }
  109. struct icmp *icmp_inner = (struct icmp *)((char *) ip_inner + 4*ip_hdr->ip_hl);
  110. // Regenerate validation and icmp id based off inner payload
  111. icmp_idnum = icmp_inner->icmp_id;
  112. *src_ip = ip_inner->ip_dst.s_addr;
  113. validate_gen(ip_hdr->ip_dst.s_addr, ip_inner->ip_dst.s_addr,
  114. (uint8_t *) validation);
  115. }
  116. return 1;
  117. }
  118. static void icmp_echo_process_packet(const u_char *packet,
  119. __attribute__((unused)) uint32_t len, fieldset_t *fs)
  120. {
  121. struct ip *ip_hdr = (struct ip *) &packet[sizeof(struct ether_header)];
  122. struct icmp *icmp_hdr = (struct icmp *) ((char *) ip_hdr + 4*ip_hdr->ip_hl);
  123. fs_add_uint64(fs, "type", icmp_hdr->icmp_type);
  124. fs_add_uint64(fs, "code", icmp_hdr->icmp_code);
  125. fs_add_uint64(fs, "icmp-id", ntohs(icmp_hdr->icmp_id));
  126. fs_add_uint64(fs, "seq", ntohs(icmp_hdr->icmp_seq));
  127. struct icmp_payload_for_rtt *payload = (struct icmp_payload_for_rtt *)(((char *)icmp_hdr) + 8);
  128. fs_add_uint64(fs, "sent-timestamp-ts", (uint64_t)payload->sent_tv_sec);
  129. fs_add_uint64(fs, "sent-timestamp-us", (uint64_t)payload->sent_tv_usec);
  130. fs_add_uint64(fs, "dst-raw", (uint64_t)payload->dst);
  131. switch (icmp_hdr->icmp_type) {
  132. case ICMP_ECHOREPLY:
  133. fs_add_string(fs, "classification", (char*) "echoreply", 0);
  134. fs_add_uint64(fs, "success", 1);
  135. break;
  136. case ICMP_UNREACH:
  137. fs_add_string(fs, "classification", (char*) "unreach", 0);
  138. fs_add_uint64(fs, "success", 0);
  139. break;
  140. case ICMP_SOURCEQUENCH:
  141. fs_add_string(fs, "classification", (char*) "sourcequench", 0);
  142. fs_add_uint64(fs, "success", 0);
  143. break;
  144. case ICMP_REDIRECT:
  145. fs_add_string(fs, "classification", (char*) "redirect", 0);
  146. fs_add_uint64(fs, "success", 0);
  147. break;
  148. case ICMP_TIMXCEED:
  149. fs_add_string(fs, "classification", (char*) "timxceed", 0);
  150. fs_add_uint64(fs, "success", 0);
  151. break;
  152. default:
  153. fs_add_string(fs, "classification", (char*) "other", 0);
  154. fs_add_uint64(fs, "success", 0);
  155. break;
  156. }
  157. }
  158. static fielddef_t fields[] = {
  159. {.name="type", .type="int", .desc="icmp message type"},
  160. {.name="code", .type="int", .desc="icmp message sub type code"},
  161. {.name="icmp-id", .type="int", .desc="icmp id number"},
  162. {.name="seq", .type="int", .desc="icmp sequence number"},
  163. {.name="sent-timestamp-ts", .type="int", .desc="timestamp of sent probe in seconds since Epoch"},
  164. {.name="sent-timestamp-us", .type="int", .desc="microsecond part of sent timestamp"},
  165. {.name="dst-raw", .type="int", .desc="raw destination IP address of sent probe"},
  166. {.name="classification", .type="string", .desc="probe module classification"},
  167. {.name="success", .type="int", .desc="did probe module classify response as success"}
  168. };
  169. probe_module_t module_icmp_echo_time = {
  170. .name = "icmp_echo_time",
  171. .packet_length = 62,
  172. .pcap_filter = "icmp and icmp[0]!=8",
  173. .pcap_snaplen = 96,
  174. .port_args = 0,
  175. .thread_initialize = &icmp_echo_init_perthread,
  176. .make_packet = &icmp_echo_make_packet,
  177. .print_packet = &icmp_echo_print_packet,
  178. .process_packet = &icmp_echo_process_packet,
  179. .validate_packet = &icmp_validate_packet,
  180. .close = NULL,
  181. .fields = fields,
  182. .numfields = 9};