tcpdump.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /* $Id: tcpdump.c 881 2004-11-07 04:16:05Z aturner $ */
  2. /*
  3. * Copyright (c) 2001-2004 Aaron Turner.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /*
  32. * This code allows us to use tcpdump to print packet decodes.
  33. * Basically, we create a local AF_UNIX socketpair, fork a copy
  34. * of ourselves, link 1/2 of the pair to STDIN of the child and
  35. * replace the child with tcpdump. We then send a "pcap" file
  36. * over the socket so that tcpdump can print it's decode to STDOUT.
  37. *
  38. * Idea and a lot of code stolen from Christain Kreibich's
  39. * <christian@whoop.org> libnetdude 0.4 code. Any bugs are mine. :)
  40. *
  41. * This product includes software developed by the University of California,
  42. * Lawrence Berkeley Laboratory and its contributors
  43. */
  44. #include "config.h"
  45. #include <sys/types.h>
  46. #include <unistd.h>
  47. #include <sys/socket.h>
  48. #include <sys/wait.h>
  49. #ifdef HAVE_SYS_POLL_H
  50. #include <sys/poll.h>
  51. #elif HAVE_POLL_H
  52. #include <poll.h>
  53. #else
  54. #include "fakepoll.h"
  55. #endif
  56. #include "tcpreplay.h"
  57. #include "tcpdump.h"
  58. #include "err.h"
  59. #ifdef DEBUG
  60. extern int debug;
  61. #endif
  62. extern struct options options;
  63. char *options_vec[OPTIONS_VEC_SIZE];
  64. void tcpdump_send_file_header(tcpdump_t *tcpdump);
  65. int tcpdump_fill_in_options(char *opt);
  66. int can_exec(const char *filename);
  67. int
  68. tcpdump_print(tcpdump_t *tcpdump, struct pcap_pkthdr *pkthdr, u_char *data)
  69. {
  70. struct pollfd poller[1];
  71. int result;
  72. poller[0].fd = tcpdump->fd;
  73. poller[0].events = POLLOUT;
  74. poller[0].revents = 0;
  75. /* wait until we can write to the tcpdump socket */
  76. result = poll(poller, 1, TCPDUMP_POLL_TIMEOUT);
  77. if (result < 0)
  78. errx(1, "Error during poll() to write to tcpdump\n%s", strerror(errno));
  79. if (result == 0)
  80. errx(1, "poll() timeout... tcpdump seems to be having a problem keeping up\n"
  81. "Try increasing TCPDUMP_POLL_TIMEOUT");
  82. /* result > 0 if we get here */
  83. if (write(tcpdump->fd, (char *)pkthdr, sizeof(struct pcap_pkthdr))
  84. != sizeof(struct pcap_pkthdr))
  85. errx(1, "Error writing pcap file header to tcpdump\n%s", strerror(errno));
  86. #ifdef DEBUG
  87. if (debug >= 5) {
  88. if (write(tcpdump->debugfd, (char *)pkthdr, sizeof(struct pcap_pkthdr))
  89. != sizeof(struct pcap_pkthdr))
  90. errx(1, "Error writing pcap file header to tcpdump debug\n%s", strerror(errno));
  91. }
  92. #endif
  93. if (write(tcpdump->fd, data, pkthdr->caplen)
  94. != pkthdr->caplen)
  95. errx(1, "Error writing packet data to tcpdump\n%s", strerror(errno));
  96. #ifdef DEBUG
  97. if (debug >= 5) {
  98. if (write(tcpdump->debugfd, data, pkthdr->caplen)
  99. != pkthdr->caplen)
  100. errx(1, "Error writing packet data to tcpdump debug\n%s", strerror(errno));
  101. }
  102. #endif
  103. fflush(stdout);
  104. return TRUE;
  105. }
  106. /*
  107. * swaps the pcap header bytes. Ripped right out of libpcap's savefile.c
  108. */
  109. static void
  110. swap_hdr(struct pcap_file_header *hp)
  111. {
  112. hp->version_major = SWAPSHORT(hp->version_major);
  113. hp->version_minor = SWAPSHORT(hp->version_minor);
  114. hp->thiszone = SWAPLONG(hp->thiszone);
  115. hp->sigfigs = SWAPLONG(hp->sigfigs);
  116. hp->snaplen = SWAPLONG(hp->snaplen);
  117. hp->linktype = SWAPLONG(hp->linktype);
  118. }
  119. int
  120. tcpdump_init(tcpdump_t *tcpdump)
  121. {
  122. FILE *f;
  123. struct pcap_file_header *pfh;
  124. u_int32_t magic;
  125. dbg(2, "tcpdump_init(): preping the pcap file header for tcpdump");
  126. if (!tcpdump || !tcpdump->filename)
  127. return FALSE; /* nothing to init */
  128. /* is tcpdump executable? */
  129. if (! can_exec(TCPDUMP_BINARY)) {
  130. errx(1, "tcpdump_init(): Unable to execute tcpdump binary: %s", TCPDUMP_BINARY);
  131. }
  132. /* Check if we can read the tracefile */
  133. if ( (f = fopen(tcpdump->filename, "r")) == NULL)
  134. errx(1, "tcpdump_init() error: unable to open %s\n", tcpdump->filename);
  135. pfh = &(tcpdump->pfh);
  136. /* Read trace file header */
  137. if (fread(pfh, sizeof(struct pcap_file_header), 1, f) != 1)
  138. errx(1, "tcpdump_init() error: unable to read pcap_file_header\n");
  139. if (pfh->magic != TCPDUMP_MAGIC && pfh->magic != PATCHED_TCPDUMP_MAGIC) {
  140. magic = SWAPLONG(pfh->magic);
  141. if (magic != TCPDUMP_MAGIC && magic != PATCHED_TCPDUMP_MAGIC)
  142. errx(1, "tcpdump_init(): bad dump file format");
  143. swap_hdr(pfh);
  144. }
  145. fclose(f);
  146. /* force to standard pcap format (non-patched) */
  147. pfh->magic = TCPDUMP_MAGIC;
  148. #ifdef DEBUG
  149. if (debug >= 5)
  150. strncpy(tcpdump->debugfile, TCPDUMP_DEBUG, sizeof(tcpdump->debugfile));
  151. #endif
  152. return TRUE;
  153. }
  154. int
  155. tcpdump_open(tcpdump_t *tcpdump)
  156. {
  157. int sockpair[2];
  158. if (! tcpdump)
  159. return FALSE;
  160. if (! tcpdump_init(tcpdump))
  161. return FALSE;
  162. /* copy over the args */
  163. dbg(2, "[child] Prepping tcpdump options...");
  164. tcpdump_fill_in_options(tcpdump->args);
  165. #ifdef DEBUG
  166. dbg(5, "Opening tcpdump debug file: %s", tcpdump->debugfile);
  167. if (debug >= 5) {
  168. if ((tcpdump->debugfd = open(tcpdump->debugfile, O_WRONLY|O_CREAT|O_TRUNC,
  169. S_IREAD|S_IWRITE|S_IRGRP|S_IROTH)) == -1)
  170. errx(1, "Error opening tcpdump debug file: %s\n%s",
  171. tcpdump->debugfile, strerror(errno));
  172. }
  173. #endif
  174. dbg(2, "Starting tcpdump...");
  175. /* create our socket pair to send packet data to tcpdump via */
  176. if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockpair) < 0)
  177. errx(1, "tcpdump_open() error: unable to create socket pair");
  178. if ((tcpdump->pid = fork() ) < 0)
  179. errx(1, "tcpdump_open() error: fork failed");
  180. dbg(2, "tcpdump pid: %d", tcpdump->pid);
  181. if (tcpdump->pid > 0) {
  182. /* we're still in tcpreplay */
  183. dbg(2, "[parent] closing fd %d", sockpair[1]);
  184. close(sockpair[1]); /* close the tcpdump side */
  185. tcpdump->fd = sockpair[0];
  186. if (fcntl(tcpdump->fd, F_SETFL, O_NONBLOCK) < 0)
  187. errx(1, "[parent] tcpdump_open() error: unable to fcntl tcpreplay socket:\n%s", strerror(errno));
  188. /* send the pcap file header to tcpdump */
  189. tcpdump_send_file_header(tcpdump);
  190. }
  191. else {
  192. dbg(2, "[child] started the kid");
  193. /* we're in the child process */
  194. dbg(2, "[child] closing fd %d", sockpair[0]);
  195. close(sockpair[0]); /* close the tcpreplay side */
  196. /* copy our side of the socketpair to our stdin */
  197. if (sockpair[1] != STDIN_FILENO) {
  198. if (dup2(sockpair[1], STDIN_FILENO) != STDIN_FILENO)
  199. errx(1, "[child] tcpdump_open() error: unable to copy socket to stdin");
  200. }
  201. /*
  202. if (sockpair[1] != STDOUT_FILENO) {
  203. if (dup2(sockpair[1], STDOUT_FILENO) != STDOUT_FILENO) {
  204. errx(1, "[child] tcpdump_open() error: unable to copy socket to stdout");
  205. }
  206. }
  207. */
  208. /* exec tcpdump */
  209. dbg(2, "[child] Exec'ing tcpdump...");
  210. if (execv(TCPDUMP_BINARY, options_vec) < 0)
  211. errx(1, "unable to exec tcpdump");
  212. }
  213. return TRUE;
  214. }
  215. /* write the pcap header to the tcpdump child process */
  216. void
  217. tcpdump_send_file_header(tcpdump_t *tcpdump)
  218. {
  219. dbg(2, "[parent] Sending pcap file header out fd %d...", tcpdump->fd);
  220. if (! tcpdump->fd)
  221. errx(1, "[parent] tcpdump filehandle is zero.");
  222. if (write(tcpdump->fd, (void *)&(tcpdump->pfh), sizeof(struct pcap_file_header))
  223. != sizeof(struct pcap_file_header)) {
  224. errx(1, "[parent] tcpdump_send_file_header() error writing file header:\n%s",
  225. strerror(errno));
  226. }
  227. #ifdef DEBUG
  228. if (debug >= 5) {
  229. if (write(tcpdump->debugfd, (void *)&(tcpdump->pfh),
  230. sizeof(struct pcap_file_header))
  231. != sizeof(struct pcap_file_header)) {
  232. errx(1, "[parent] tcpdump_send_file_header() error writing file debug header:\n%s",
  233. strerror(errno));
  234. }
  235. }
  236. #endif
  237. }
  238. /* copy the string of args (*opt) to the vector (**opt_vec)
  239. * for a max of opt_len. Returns the number of options
  240. * in the vector
  241. */
  242. int
  243. tcpdump_fill_in_options(char *opt)
  244. {
  245. char options[256];
  246. char *arg, *newarg;
  247. int i = 1, arglen;
  248. char *token = NULL;
  249. /* zero out our options_vec for execv() */
  250. memset(options_vec, '\0', OPTIONS_VEC_SIZE);
  251. /* first arg should be the binary (by convention) */
  252. options_vec[0] = TCPDUMP_BINARY;
  253. /* prep args */
  254. memset(options, '\0', 256);
  255. if (opt != NULL) {
  256. strncat(options, opt, 255);
  257. }
  258. strncat(options, TCPDUMP_ARGS, 255);
  259. dbg(2, "[child] Will execute: tcpdump %s", options);
  260. /* process args */
  261. /* process the first argument */
  262. arg = strtok_r(options, OPT_DELIM, &token);
  263. arglen = strlen(arg) + 2; /* -{arg}\0 */
  264. newarg = (char *)malloc(arglen);
  265. memset(newarg, '\0', arglen);
  266. strncat(newarg, "-", arglen -1);
  267. strncat(newarg, arg, arglen -1);
  268. options_vec[i++] = newarg;
  269. /* process the remaining args
  270. note that i < OPTIONS_VEC_SIZE - 1
  271. because: a) we need to add '-' as an option to the end
  272. b) because the array has to be null terminated
  273. */
  274. while (((arg = strtok_r(NULL, OPT_DELIM, &token)) != NULL) &&
  275. (i < OPTIONS_VEC_SIZE - 1)) {
  276. arglen = strlen(arg) + 2;
  277. newarg = (char *)malloc(arglen);
  278. memset(newarg, '\0', arglen);
  279. strncat(newarg, "-", arglen -1);
  280. strncat(newarg, arg, arglen -1);
  281. options_vec[i++] = newarg;
  282. }
  283. /* tell -r to read from stdin */
  284. options_vec[i] = "-";
  285. return(i);
  286. }
  287. void
  288. tcpdump_close(tcpdump_t *tcpdump)
  289. {
  290. if (! tcpdump)
  291. return;
  292. if (tcpdump->pid <= 0)
  293. return;
  294. dbg(2, "[parent] killing tcpdump pid: %d", tcpdump->pid);
  295. kill(tcpdump->pid, SIGKILL);
  296. close(tcpdump->fd);
  297. if (waitpid(tcpdump->pid, NULL, 0) != tcpdump->pid)
  298. errx(1, "[parent] Error in waitpid()");
  299. tcpdump->pid = 0;
  300. tcpdump->fd = 0;
  301. }
  302. int
  303. can_exec(const char *filename)
  304. {
  305. struct stat st;
  306. if (!filename || filename[0] == '\0')
  307. return FALSE;
  308. /* Stat the file to see if it's executable and
  309. if the user may run it.
  310. */
  311. if (lstat(filename, &st) < 0)
  312. return FALSE;
  313. if ((st.st_mode & S_IXUSR) ||
  314. (st.st_mode & S_IXGRP) ||
  315. (st.st_mode & S_IXOTH))
  316. return TRUE;
  317. return FALSE;
  318. }