tcpdump.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /* $Id: tcpdump.c 1462 2006-04-13 05:10:27Z 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 "defines.h"
  46. #include "common.h"
  47. #include <sys/types.h>
  48. #include <unistd.h>
  49. #include <sys/socket.h>
  50. #include <sys/wait.h>
  51. #include "tcpdump.h"
  52. #include "lib/strlcpy.h"
  53. #ifdef DEBUG
  54. extern int debug;
  55. #endif
  56. char *options_vec[OPTIONS_VEC_SIZE];
  57. void tcpdump_send_file_header(tcpdump_t *tcpdump);
  58. int tcpdump_fill_in_options(char *opt);
  59. int can_exec(const char *filename);
  60. int
  61. tcpdump_print(tcpdump_t *tcpdump, struct pcap_pkthdr *pkthdr, const u_char *data)
  62. {
  63. struct pollfd poller[1];
  64. int result;
  65. char decode[TCPDUMP_DECODE_LEN];
  66. poller[0].fd = tcpdump->infd;
  67. poller[0].events = POLLOUT;
  68. poller[0].revents = 0;
  69. /* wait until we can write to the tcpdump socket */
  70. result = poll(poller, 1, TCPDUMP_POLL_TIMEOUT);
  71. if (result < 0)
  72. errx(1, "Error during poll() to write to tcpdump\n%s", strerror(errno));
  73. if (result == 0)
  74. err(1, "poll() timeout... tcpdump seems to be having a problem keeping up\n"
  75. "Try increasing TCPDUMP_POLL_TIMEOUT");
  76. /* result > 0 if we get here */
  77. if (write(tcpdump->infd, (char *)pkthdr, sizeof(struct pcap_pkthdr))
  78. != sizeof(struct pcap_pkthdr))
  79. errx(1, "Error writing pcap file header to tcpdump\n%s", strerror(errno));
  80. #ifdef DEBUG
  81. if (debug >= 5) {
  82. if (write(tcpdump->debugfd, (char *)pkthdr, sizeof(struct pcap_pkthdr))
  83. != sizeof(struct pcap_pkthdr))
  84. errx(1, "Error writing pcap file header to tcpdump debug\n%s", strerror(errno));
  85. }
  86. #endif
  87. if (write(tcpdump->infd, data, pkthdr->caplen) != (ssize_t)pkthdr->caplen)
  88. errx(1, "Error writing packet data to tcpdump\n%s", strerror(errno));
  89. #ifdef DEBUG
  90. if (debug >= 5) {
  91. if (write(tcpdump->debugfd, data, pkthdr->caplen) != (ssize_t)pkthdr->caplen)
  92. errx(1, "Error writing packet data to tcpdump debug\n%s", strerror(errno));
  93. }
  94. #endif
  95. /* Wait for output from tcpdump */
  96. poller[0].fd = tcpdump->outfd;
  97. poller[0].events = POLLIN;
  98. poller[0].revents = 0;
  99. result = poll(poller, 1, TCPDUMP_POLL_TIMEOUT);
  100. if (result < 0)
  101. errx(1, "Error during poll() to write to tcpdump\n%s", strerror(errno));
  102. if (result == 0)
  103. err(1, "poll() timeout... tcpdump seems to be having a problem keeping up\n"
  104. "Try increasing TCPDUMP_POLL_TIMEOUT");
  105. /* result > 0 if we get here */
  106. if (read(tcpdump->outfd, &decode, TCPDUMP_DECODE_LEN) < 0)
  107. errx(1, "Error reading tcpdump decode: %s", strerror(errno));
  108. printf("%s", decode);
  109. return TRUE;
  110. }
  111. /*
  112. * swaps the pcap header bytes. Ripped right out of libpcap's file.c
  113. */
  114. static void
  115. swap_hdr(struct pcap_file_header *hp)
  116. {
  117. hp->version_major = SWAPSHORT(hp->version_major);
  118. hp->version_minor = SWAPSHORT(hp->version_minor);
  119. hp->thiszone = SWAPLONG(hp->thiszone);
  120. hp->sigfigs = SWAPLONG(hp->sigfigs);
  121. hp->snaplen = SWAPLONG(hp->snaplen);
  122. hp->linktype = SWAPLONG(hp->linktype);
  123. }
  124. int
  125. tcpdump_init(tcpdump_t *tcpdump)
  126. {
  127. FILE *f;
  128. struct pcap_file_header *pfh;
  129. u_int32_t magic;
  130. dbg(2, "preping the pcap file header for tcpdump");
  131. if (!tcpdump || !tcpdump->filename)
  132. return FALSE; /* nothing to init */
  133. /* is tcpdump executable? */
  134. if (! can_exec(TCPDUMP_BINARY)) {
  135. errx(1, "Unable to execute tcpdump binary: %s", TCPDUMP_BINARY);
  136. }
  137. /* Check if we can read the tracefile */
  138. if ( (f = fopen(tcpdump->filename, "r")) == NULL)
  139. errx(1, "Unable to open %s\n", tcpdump->filename);
  140. pfh = &(tcpdump->pfh);
  141. /* Read trace file header */
  142. if (fread(pfh, sizeof(struct pcap_file_header), 1, f) != 1)
  143. errx(1, "Unable to read pcap_file_header: %s", strerror(errno));
  144. if (pfh->magic != TCPDUMP_MAGIC && pfh->magic != PATCHED_TCPDUMP_MAGIC) {
  145. magic = SWAPLONG(pfh->magic);
  146. if (magic != TCPDUMP_MAGIC && magic != PATCHED_TCPDUMP_MAGIC)
  147. err(1, "Invalid pcap file magic number");
  148. swap_hdr(pfh);
  149. }
  150. fclose(f);
  151. /* force to standard pcap format (non-patched) */
  152. pfh->magic = TCPDUMP_MAGIC;
  153. #ifdef DEBUG
  154. if (debug >= 5)
  155. strlcpy(tcpdump->debugfile, TCPDUMP_DEBUG, sizeof(tcpdump->debugfile));
  156. #endif
  157. return TRUE;
  158. }
  159. int
  160. tcpdump_open(tcpdump_t *tcpdump)
  161. {
  162. int infd[2], outfd[2];
  163. if (! tcpdump)
  164. return FALSE;
  165. if (! tcpdump_init(tcpdump))
  166. return FALSE;
  167. /* copy over the args */
  168. dbg(2, "[child] Prepping tcpdump options...");
  169. tcpdump_fill_in_options(tcpdump->args);
  170. #ifdef DEBUG
  171. dbgx(5, "Opening tcpdump debug file: %s", tcpdump->debugfile);
  172. if (debug >= 5) {
  173. if ((tcpdump->debugfd = open(tcpdump->debugfile, O_WRONLY|O_CREAT|O_TRUNC,
  174. S_IREAD|S_IWRITE|S_IRGRP|S_IROTH)) == -1)
  175. errx(1, "Error opening tcpdump debug file: %s\n%s",
  176. tcpdump->debugfile, strerror(errno));
  177. }
  178. #endif
  179. dbg(2, "Starting tcpdump...");
  180. /* create our socket pair to send packet data to tcpdump via */
  181. if (socketpair(AF_UNIX, SOCK_STREAM, 0, infd) < 0)
  182. errx(1, "Unable to create stdin socket pair: %s", strerror(errno));
  183. /* create our socket pair to read packet decode from tcpdump */
  184. if (socketpair(AF_UNIX, SOCK_STREAM, 0, outfd) < 0)
  185. errx(1, "Unable to create stdout socket pair: %s", strerror(errno));
  186. if ((tcpdump->pid = fork() ) < 0)
  187. errx(1, "Fork failed: %s", strerror(errno));
  188. dbgx(2, "tcpdump pid: %d", tcpdump->pid);
  189. if (tcpdump->pid > 0) {
  190. /* we're still in tcpreplay */
  191. dbgx(2, "[parent] closing input fd %d", infd[1]);
  192. dbgx(2, "[parent] closing output fd %d", outfd[1]);
  193. close(infd[1]); /* close the tcpdump side */
  194. close(outfd[1]);
  195. tcpdump->infd = infd[0];
  196. tcpdump->outfd = outfd[0];
  197. if (fcntl(tcpdump->infd, F_SETFL, O_NONBLOCK) < 0)
  198. errx(1, "[parent] Unable to fcntl tcpreplay socket:\n%s", strerror(errno));
  199. if (fcntl(tcpdump->outfd, F_SETFL, O_NONBLOCK) < 0)
  200. errx(1, "[parent] Unable to fnctl stdout socket:\n%s", strerror(errno));
  201. /* send the pcap file header to tcpdump */
  202. tcpdump_send_file_header(tcpdump);
  203. }
  204. else {
  205. dbg(2, "[child] started the kid");
  206. /* we're in the child process */
  207. dbgx(2, "[child] closing in fd %d", infd[0]);
  208. dbgx(2, "[child] closing out fd %d", outfd[0]);
  209. close(infd[0]); /* close the tcpreplay side */
  210. close(outfd[0]);
  211. /* copy our side of the socketpair to our stdin */
  212. if (infd[1] != STDIN_FILENO) {
  213. if (dup2(infd[1], STDIN_FILENO) != STDIN_FILENO)
  214. errx(1, "[child] Unable to copy socket to stdin: %s",
  215. strerror(errno));
  216. }
  217. /* copy our side of the socketpair to our stdout */
  218. if (outfd[1] != STDOUT_FILENO) {
  219. if (dup2(outfd[1], STDOUT_FILENO) != STDOUT_FILENO)
  220. errx(1, "[child] Unable to copy socket to stdout: %s",
  221. strerror(errno));
  222. }
  223. /* exec tcpdump */
  224. dbg(2, "[child] Exec'ing tcpdump...");
  225. if (execv(TCPDUMP_BINARY, options_vec) < 0)
  226. errx(1, "Unable to exec tcpdump: %s", strerror(errno));
  227. }
  228. return TRUE;
  229. }
  230. int
  231. tcpdump_open_live(tcpdump_t *tcpdump, pcap_t *pcap) {
  232. assert(tcpdump);
  233. assert(pcap);
  234. return 1;
  235. }
  236. /* write the pcap header to the tcpdump child process */
  237. void
  238. tcpdump_send_file_header(tcpdump_t *tcpdump)
  239. {
  240. dbgx(2, "[parent] Sending pcap file header out fd %d...", tcpdump->infd);
  241. if (! tcpdump->infd)
  242. err(1, "[parent] tcpdump filehandle is zero.");
  243. if (write(tcpdump->infd, (void *)&(tcpdump->pfh), sizeof(struct pcap_file_header))
  244. != sizeof(struct pcap_file_header)) {
  245. errx(1, "[parent] tcpdump_send_file_header() error writing file header:\n%s",
  246. strerror(errno));
  247. }
  248. #ifdef DEBUG
  249. if (debug >= 5) {
  250. if (write(tcpdump->debugfd, (void *)&(tcpdump->pfh),
  251. sizeof(struct pcap_file_header))
  252. != sizeof(struct pcap_file_header)) {
  253. errx(1, "[parent] tcpdump_send_file_header() error writing file debug header:\n%s",
  254. strerror(errno));
  255. }
  256. }
  257. #endif
  258. }
  259. /* copy the string of args (*opt) to the vector (**opt_vec)
  260. * for a max of opt_len. Returns the number of options
  261. * in the vector
  262. */
  263. int
  264. tcpdump_fill_in_options(char *opt)
  265. {
  266. char options[256];
  267. char *arg, *newarg;
  268. int i = 1, arglen;
  269. char *token = NULL;
  270. /* zero out our options_vec for execv() */
  271. memset(options_vec, '\0', OPTIONS_VEC_SIZE);
  272. /* first arg should be the binary (by convention) */
  273. options_vec[0] = TCPDUMP_BINARY;
  274. /* prep args */
  275. memset(options, '\0', 256);
  276. if (opt != NULL) {
  277. strlcat(options, opt, sizeof(options));
  278. }
  279. strlcat(options, TCPDUMP_ARGS, sizeof(options));
  280. dbgx(2, "[child] Will execute: tcpdump %s", options);
  281. /* process args */
  282. /* process the first argument */
  283. arg = strtok_r(options, OPT_DELIM, &token);
  284. arglen = strlen(arg) + 2; /* -{arg}\0 */
  285. newarg = (char *)safe_malloc(arglen);
  286. strlcat(newarg, "-", arglen);
  287. strlcat(newarg, arg, arglen);
  288. options_vec[i++] = newarg;
  289. /* process the remaining args
  290. note that i < OPTIONS_VEC_SIZE - 1
  291. because: a) we need to add '-' as an option to the end
  292. b) because the array has to be null terminated
  293. */
  294. while (((arg = strtok_r(NULL, OPT_DELIM, &token)) != NULL) &&
  295. (i < OPTIONS_VEC_SIZE - 1)) {
  296. arglen = strlen(arg) + 2;
  297. newarg = (char *)safe_malloc(arglen);
  298. strlcat(newarg, "-", arglen);
  299. strlcat(newarg, arg, arglen);
  300. options_vec[i++] = newarg;
  301. }
  302. /* tell -r to read from stdin */
  303. options_vec[i] = "-";
  304. return(i);
  305. }
  306. void
  307. tcpdump_close(tcpdump_t *tcpdump)
  308. {
  309. if (! tcpdump)
  310. return;
  311. if (tcpdump->pid <= 0)
  312. return;
  313. dbgx(2, "[parent] killing tcpdump pid: %d", tcpdump->pid);
  314. kill(tcpdump->pid, SIGKILL);
  315. close(tcpdump->infd);
  316. close(tcpdump->outfd);
  317. if (waitpid(tcpdump->pid, NULL, 0) != tcpdump->pid)
  318. errx(1, "[parent] Error in waitpid: %s", strerror(errno));
  319. tcpdump->pid = 0;
  320. tcpdump->infd = 0;
  321. tcpdump->outfd = 0;
  322. }
  323. int
  324. can_exec(const char *filename)
  325. {
  326. struct stat st;
  327. if (!filename || filename[0] == '\0')
  328. return FALSE;
  329. /* Stat the file to see if it's executable and
  330. if the user may run it.
  331. */
  332. if (lstat(filename, &st) < 0)
  333. return FALSE;
  334. if ((st.st_mode & S_IXUSR) ||
  335. (st.st_mode & S_IXGRP) ||
  336. (st.st_mode & S_IXOTH))
  337. return TRUE;
  338. return FALSE;
  339. }
  340. /*
  341. Local Variables:
  342. mode:c
  343. indent-tabs-mode:nil
  344. c-basic-offset:4
  345. End:
  346. */