tcpedit.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /* $Id: tcpedit.c 1855 2007-05-02 04:54:38Z aturner $ */
  2. /*
  3. * Copyright (c) 2001-2007 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. #include "config.h"
  32. #include "defines.h"
  33. #include <ctype.h>
  34. #include <fcntl.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <sys/types.h>
  39. #include <unistd.h>
  40. #include <stdarg.h>
  41. #include "tcpedit-int.h"
  42. #include "tcpedit_stub.h"
  43. #include "portmap.h"
  44. #include "common.h"
  45. #include "edit_packet.h"
  46. #include "parse_args.h"
  47. #include "plugins/dlt_plugins.h"
  48. #include "lib/sll.h"
  49. #include "dlt.h"
  50. tOptDesc *const tcpedit_tcpedit_optDesc_p;
  51. /*
  52. * Processs a given packet and edit the pkthdr/pktdata structures
  53. * according to the rules in tcpedit
  54. * Returns: -1 on error
  55. * 0 on no change
  56. * 1 on change
  57. */
  58. int
  59. tcpedit_packet(tcpedit_t *tcpedit, struct pcap_pkthdr **pkthdr,
  60. u_char **pktdata, tcpr_dir_t direction)
  61. {
  62. ipv4_hdr_t *ip_hdr = NULL;
  63. arp_hdr_t *arp_hdr = NULL;
  64. int l2len = 0, l2proto, retval = 0, dlt, pktlen, lendiff;
  65. int needtorecalc = 0; /* did the packet change? if so, checksum */
  66. static u_char *packet = NULL; /* static buffer to hold packet data when padding out */
  67. assert(tcpedit);
  68. assert(pkthdr);
  69. assert(*pkthdr);
  70. assert(pktdata);
  71. assert(*pktdata);
  72. assert(tcpedit->validated);
  73. tcpedit->runtime.packetnum++;
  74. dbgx(2, "packet " COUNTER_SPEC " caplen %d",
  75. tcpedit->runtime.packetnum, (*pkthdr)->caplen);
  76. /*
  77. * if we are padding out the packet, we need to move the packet
  78. * data to a different buffer because the incoming **pktdata buffer
  79. * most likely isn't big enough for the extra padding
  80. */
  81. if (HAVE_OPT(FIXLEN) && strcmp(OPT_ARG(FIXLEN), "pad") == 0) {
  82. /* allocate our buffer the first time */
  83. if (packet == NULL)
  84. packet = safe_malloc(MAX_SNAPLEN);
  85. memcpy(packet, *pktdata, (*pkthdr)->caplen);
  86. *pktdata = packet;
  87. }
  88. /*
  89. * remove the Ethernet FCS (checksum)?
  90. * note that this feature requires the end user to be smart and
  91. * only set this flag IFF the pcap has the FCS. If not, then they
  92. * just removed 2 bytes of ACTUAL PACKET DATA. Sucks to be them.
  93. */
  94. if (tcpedit->efcs)
  95. (*pkthdr)->caplen -= 2;
  96. /* rewrite DLT */
  97. if ((pktlen = tcpedit_dlt_process(tcpedit->dlt_ctx, *pktdata, (*pkthdr)->caplen, direction)) == TCPEDIT_ERROR)
  98. errx(1, "%s", tcpedit_geterr(tcpedit));
  99. /* update our packet lengths (real/captured) based on L2 length changes */
  100. lendiff = pktlen - (*pkthdr)->caplen;
  101. (*pkthdr)->caplen += lendiff;
  102. (*pkthdr)->len += lendiff;
  103. dlt = tcpedit_dlt_dst(tcpedit->dlt_ctx);
  104. l2proto = tcpedit_dlt_proto(tcpedit->dlt_ctx, dlt, *pktdata, (*pkthdr)->caplen);
  105. l2len = tcpedit_dlt_l2len(tcpedit->dlt_ctx, dlt, *pktdata, (*pkthdr)->caplen);
  106. /* does packet have an IP header? if so set our pointer to it */
  107. if (l2proto == ETHERTYPE_IP) {
  108. ip_hdr = (ipv4_hdr_t *)tcpedit_dlt_l3data(tcpedit->dlt_ctx, dlt, *pktdata, (*pkthdr)->caplen);
  109. if (ip_hdr == NULL) {
  110. return -1;
  111. }
  112. dbg(3, "Packet has an IPv4 header...");
  113. } else {
  114. dbgx(3, "Packet isn't IPv4: 0x%02x", l2proto);
  115. /* non-IP packets have a NULL ip_hdr struct */
  116. ip_hdr = NULL;
  117. }
  118. /* rewrite IP addresses */
  119. if (tcpedit->rewrite_ip) {
  120. /* IP packets */
  121. if (ip_hdr != NULL) {
  122. if ((retval = rewrite_ipv4l3(tcpedit, ip_hdr, direction)) < 0)
  123. return -1;
  124. needtorecalc += retval;
  125. }
  126. /* ARP packets */
  127. else if (l2proto == ETHERTYPE_ARP) {
  128. arp_hdr = (arp_hdr_t *)(&(*pktdata)[l2len]);
  129. /* unlike, rewrite_ipl3, we don't care if the packet changed
  130. * because we never need to recalc the checksums for an ARP
  131. * packet. So ignore the return value
  132. */
  133. if (rewrite_iparp(tcpedit, arp_hdr, direction) < 0)
  134. return -1;
  135. }
  136. }
  137. /* rewrite ports */
  138. if (tcpedit->portmap != NULL && (ip_hdr != NULL)) {
  139. if ((retval = rewrite_ports(tcpedit, &ip_hdr)) < 0)
  140. return -1;
  141. needtorecalc += retval;
  142. }
  143. /* Untruncate packet? Only for IP packets */
  144. if ((tcpedit->fixlen) && (ip_hdr != NULL)) {
  145. if ((retval = untrunc_packet(tcpedit, *pkthdr, *pktdata, ip_hdr)) < 0)
  146. return -1;
  147. needtorecalc += retval;
  148. }
  149. /* do we need to spoof the src/dst IP address? */
  150. if (tcpedit->seed) {
  151. if (ip_hdr != NULL) {
  152. if ((retval = randomize_ipv4(tcpedit, *pkthdr, *pktdata,
  153. ip_hdr)) < 0)
  154. return -1;
  155. needtorecalc += retval;
  156. } else {
  157. if (direction == TCPR_DIR_C2S) {
  158. if (randomize_iparp(tcpedit, *pkthdr, *pktdata,
  159. tcpedit->runtime.dlt1) < 0)
  160. return -1;
  161. } else {
  162. if (randomize_iparp(tcpedit, *pkthdr, *pktdata,
  163. tcpedit->runtime.dlt2) < 0)
  164. return -1;
  165. }
  166. }
  167. }
  168. /* do we need to fix checksums? */
  169. if ((tcpedit->fixcsum || needtorecalc) && (ip_hdr != NULL)) {
  170. retval = fix_checksums(tcpedit, *pkthdr, ip_hdr);
  171. if (retval < 0) {
  172. return TCPEDIT_ERROR;
  173. } else if (retval == TCPEDIT_WARN) {
  174. warnx("%s", tcpedit_getwarn(tcpedit));
  175. }
  176. }
  177. tcpedit_dlt_merge_l3data(tcpedit->dlt_ctx, dlt, *pktdata, (*pkthdr)->caplen, (u_char *)ip_hdr);
  178. tcpedit->runtime.total_bytes += (*pkthdr)->caplen;
  179. tcpedit->runtime.pkts_edited ++;
  180. return retval;
  181. }
  182. /*
  183. * initializes the tcpedit library. returns 0 on success, -1 on error.
  184. */
  185. int
  186. tcpedit_init(tcpedit_t **tcpedit_ex, int dlt)
  187. {
  188. tcpedit_t *tcpedit;
  189. *tcpedit_ex = safe_malloc(sizeof(tcpedit_t));
  190. tcpedit = *tcpedit_ex;
  191. if ((tcpedit->dlt_ctx = tcpedit_dlt_init(tcpedit, dlt)) == NULL)
  192. return TCPEDIT_ERROR;
  193. tcpedit->mtu = DEFAULT_MTU; /* assume 802.3 Ethernet */
  194. memset(&(tcpedit->runtime), 0, sizeof(tcpedit_runtime_t));
  195. tcpedit->runtime.dlt1 = dlt;
  196. tcpedit->runtime.dlt2 = dlt;
  197. dbgx(1, "Input file (1) datalink type is %s\n",
  198. pcap_datalink_val_to_name(dlt));
  199. #ifdef FORCE_ALIGN
  200. tcpedit->runtime.l3buff = (u_char *)safe_malloc(MAXPACKET);
  201. #endif
  202. return TCPEDIT_OK;
  203. }
  204. /*
  205. * return the output DLT
  206. */
  207. int
  208. tcpedit_get_output_dlt(tcpedit_t *tcpedit)
  209. {
  210. assert(tcpedit);
  211. return tcpedit_dlt_output_dlt(tcpedit->dlt_ctx);
  212. }
  213. /*
  214. * Validates that given the current state of tcpedit that the given
  215. * pcap source and destination (based on DLT) can be properly rewritten
  216. * return 0 on sucess
  217. * return -1 on error
  218. * DO NOT USE!
  219. */
  220. int
  221. tcpedit_validate(tcpedit_t *tcpedit)
  222. {
  223. assert(tcpedit);
  224. tcpedit->validated = 1;
  225. /* we used to do a bunch of things here, but not anymore...
  226. * maybe I should find something to do or just get ride of it
  227. */
  228. return 0;
  229. }
  230. /*
  231. * return the error string when a tcpedit() function returns
  232. * an error
  233. */
  234. char *
  235. tcpedit_geterr(tcpedit_t *tcpedit)
  236. {
  237. assert(tcpedit);
  238. return tcpedit->runtime.errstr;
  239. }
  240. /*
  241. * used to set the error string when there is an error
  242. */
  243. void
  244. __tcpedit_seterr(tcpedit_t *tcpedit, const char *func, const int line, const char *file, const char *fmt, ...)
  245. {
  246. va_list ap;
  247. char errormsg[TCPEDIT_ERRSTR_LEN];
  248. assert(tcpedit);
  249. va_start(ap, fmt);
  250. if (fmt != NULL) {
  251. dbgx(1, fmt, ap);
  252. (void)vsnprintf(errormsg,
  253. (TCPEDIT_ERRSTR_LEN - 1), fmt, ap);
  254. }
  255. va_end(ap);
  256. snprintf(tcpedit->runtime.errstr, (TCPEDIT_ERRSTR_LEN -1), "From %s:%s() line %d:\n%s",
  257. file, func, line, errormsg);
  258. }
  259. /*
  260. * return the warning string when a tcpedit() function returns
  261. * a warning
  262. */
  263. char *
  264. tcpedit_getwarn(tcpedit_t *tcpedit)
  265. {
  266. assert(tcpedit);
  267. return tcpedit->runtime.warnstr;
  268. }
  269. /*
  270. * used to set the warning string when there is an warning
  271. */
  272. void
  273. tcpedit_setwarn(tcpedit_t *tcpedit, const char *fmt, ...)
  274. {
  275. va_list ap;
  276. assert(tcpedit);
  277. va_start(ap, fmt);
  278. if (fmt != NULL) {
  279. dbgx(1, fmt, ap);
  280. (void)vsnprintf(tcpedit->runtime.warnstr,
  281. (TCPEDIT_ERRSTR_LEN - 1), fmt, ap);
  282. }
  283. va_end(ap);
  284. }
  285. /*
  286. * Generic function which checks the TCPEDIT_* error code
  287. * and always returns OK or ERROR. For warnings, prints the
  288. * warning message and returns OK. For any other value, fails with
  289. * an assert.
  290. */
  291. int
  292. tcpedit_checkerror(tcpedit_t *tcpedit, const int rcode, const char *prefix) {
  293. assert(tcpedit);
  294. switch (rcode) {
  295. case TCPEDIT_OK:
  296. case TCPEDIT_ERROR:
  297. return rcode;
  298. break;
  299. case TCPEDIT_WARN:
  300. if (prefix != NULL) {
  301. fprintf(stderr, "Warning %s: %s\n", prefix, tcpedit_getwarn(tcpedit));
  302. } else {
  303. fprintf(stderr, "Warning: %s\n", tcpedit_getwarn(tcpedit));
  304. }
  305. return TCPEDIT_OK;
  306. break;
  307. default:
  308. assert(0 == 1); /* this should never happen! */
  309. break;
  310. }
  311. return TCPEDIT_ERROR;
  312. }
  313. /*
  314. * Cleans up after ourselves. Return 0 on success.
  315. */
  316. int
  317. tcpedit_close(tcpedit_t *tcpedit)
  318. {
  319. assert(tcpedit);
  320. dbgx(1, "tcpedit processed " COUNTER_SPEC " bytes in " COUNTER_SPEC
  321. " packets.\n", tcpedit->runtime.total_bytes,
  322. tcpedit->runtime.pkts_edited);
  323. /* free buffer if required */
  324. #ifdef FORCE_ALIGN
  325. free(tcpedit->runtime.l3buff);
  326. #endif
  327. return 0;
  328. }
  329. u_char *
  330. tcpedit_l3data(tcpedit_t *tcpedit, tcpedit_coder_t code, u_char *packet, const int pktlen)
  331. {
  332. u_char *result = NULL;
  333. if (code == BEFORE_PROCESS) {
  334. result = tcpedit_dlt_l3data(tcpedit->dlt_ctx, tcpedit->dlt_ctx->decoder->dlt, packet, pktlen);
  335. } else {
  336. result = tcpedit_dlt_l3data(tcpedit->dlt_ctx, tcpedit->dlt_ctx->encoder->dlt, packet, pktlen);
  337. }
  338. return result;
  339. }
  340. int
  341. tcpedit_l3proto(tcpedit_t *tcpedit, tcpedit_coder_t code, const u_char *packet, const int pktlen)
  342. {
  343. int result = 0;
  344. if (code == BEFORE_PROCESS) {
  345. tcpedit_dlt_proto(tcpedit->dlt_ctx, tcpedit->dlt_ctx->decoder->dlt, packet, pktlen);
  346. } else {
  347. tcpedit_dlt_proto(tcpedit->dlt_ctx, tcpedit->dlt_ctx->encoder->dlt, packet, pktlen);
  348. }
  349. return result;
  350. }
  351. /*
  352. Local Variables:
  353. mode:c
  354. indent-tabs-mode:nil
  355. c-basic-offset:4
  356. End:
  357. */