tcpedit.c 14 KB

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