tcpedit.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /* $Id: tcpedit.c 2317 2009-05-07 17:57:11Z 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. ipv6_hdr_t *ip6_hdr = NULL;
  67. arp_hdr_t *arp_hdr = NULL;
  68. int l2len = 0, l2proto, retval = 0, dst_dlt, src_dlt, pktlen, lendiff;
  69. int ipflags = 0, tclass = 0;
  70. int needtorecalc = 0; /* did the packet change? if so, checksum */
  71. u_char *packet = *pktdata;
  72. assert(tcpedit);
  73. assert(pkthdr);
  74. assert(*pkthdr);
  75. assert(pktdata);
  76. assert(packet);
  77. assert(tcpedit->validated);
  78. tcpedit->runtime.packetnum++;
  79. dbgx(3, "packet " COUNTER_SPEC " caplen %d",
  80. tcpedit->runtime.packetnum, (*pkthdr)->caplen);
  81. /*
  82. * remove the Ethernet FCS (checksum)?
  83. * note that this feature requires the end user to be smart and
  84. * only set this flag IFF the pcap has the FCS. If not, then they
  85. * just removed 2 bytes of ACTUAL PACKET DATA. Sucks to be them.
  86. */
  87. if (tcpedit->efcs > 0) {
  88. (*pkthdr)->caplen -= 4;
  89. (*pkthdr)->len -= 4;
  90. }
  91. src_dlt = tcpedit_dlt_src(tcpedit->dlt_ctx);
  92. /* not everything has a L3 header, so check for errors. returns proto in network byte order */
  93. if ((l2proto = tcpedit_dlt_proto(tcpedit->dlt_ctx, src_dlt, packet, (*pkthdr)->caplen)) < 0) {
  94. dbg(2, "Packet has no L3+ header");
  95. } else {
  96. dbgx(2, "Layer 3 protocol type is: 0x%04x", ntohs(l2proto));
  97. }
  98. /* rewrite Layer 2 */
  99. if ((pktlen = tcpedit_dlt_process(tcpedit->dlt_ctx, pktdata, (*pkthdr)->caplen, direction)) == TCPEDIT_ERROR)
  100. errx(-1, "%s", tcpedit_geterr(tcpedit));
  101. /* unable to edit packet, most likely 802.11 management or data QoS frame */
  102. if (pktlen == TCPEDIT_SOFT_ERROR) {
  103. dbgx(3, "%s", tcpedit_geterr(tcpedit));
  104. return TCPEDIT_SOFT_ERROR;
  105. }
  106. /* update our packet lengths (real/captured) based on L2 length changes */
  107. lendiff = pktlen - (*pkthdr)->caplen;
  108. (*pkthdr)->caplen += lendiff;
  109. (*pkthdr)->len += lendiff;
  110. dst_dlt = tcpedit_dlt_dst(tcpedit->dlt_ctx);
  111. l2len = tcpedit_dlt_l2len(tcpedit->dlt_ctx, dst_dlt, packet, (*pkthdr)->caplen);
  112. dbgx(2, "dst_dlt = %04x\tsrc_dlt = %04x\tproto = %04x\tl2len = %d", dst_dlt, src_dlt, ntohs(l2proto), l2len);
  113. /* does packet have an IP header? if so set our pointer to it */
  114. if (l2proto == htons(ETHERTYPE_IP)) {
  115. ip_hdr = (ipv4_hdr_t *)tcpedit_dlt_l3data(tcpedit->dlt_ctx, src_dlt, packet, (*pkthdr)->caplen);
  116. if (ip_hdr == NULL) {
  117. return TCPEDIT_ERROR;
  118. }
  119. dbgx(3, "Packet has an IPv4 header: %p...", ip_hdr);
  120. } else if (l2proto == htons(ETHERTYPE_IP6)) {
  121. ip6_hdr = (ipv6_hdr_t *)tcpedit_dlt_l3data(tcpedit->dlt_ctx, src_dlt, packet, (*pkthdr)->caplen);
  122. if (ip6_hdr == NULL) {
  123. return TCPEDIT_ERROR;
  124. }
  125. dbgx(3, "Packet has an IPv6 header: %p...", ip6_hdr);
  126. } else {
  127. dbgx(3, "Packet isn't IPv4 or IPv6: 0x%04x", l2proto);
  128. /* non-IP packets have a NULL ip_hdr struct */
  129. ip_hdr = NULL;
  130. }
  131. /* The following edits only apply for IPv4 */
  132. if (ip_hdr != NULL) {
  133. /* set TOS ? */
  134. if (tcpedit->tos > -1)
  135. ip_hdr->ip_tos = tcpedit->tos;
  136. /* rewrite the TTL */
  137. needtorecalc += rewrite_ipv4_ttl(tcpedit, ip_hdr);
  138. /* rewrite TCP/UDP ports */
  139. if (tcpedit->portmap != NULL) {
  140. if ((retval = rewrite_ipv4_ports(tcpedit, &ip_hdr)) < 0)
  141. return TCPEDIT_ERROR;
  142. needtorecalc += retval;
  143. }
  144. }
  145. /* IPv6 edits */
  146. else if (ip6_hdr != NULL) {
  147. /* rewrite the hop limit */
  148. needtorecalc += rewrite_ipv6_hlim(tcpedit, ip6_hdr);
  149. /* set traffic class? */
  150. if (tcpedit->tclass > -1) {
  151. /* calculate the bits */
  152. tclass = tcpedit->tclass << 20;
  153. /* convert our 4 bytes to an int */
  154. memcpy(&ipflags, &ip6_hdr->ip_flags, 4);
  155. /* strip out the old tclass bits */
  156. ipflags = ntohl(ipflags) & 0xf00fffff;
  157. /* add the tclass bits back */
  158. ipflags += tclass;
  159. ipflags = htonl(ipflags);
  160. memcpy(&ip6_hdr->ip_flags, &ipflags, 4);
  161. }
  162. /* set the flow label? */
  163. if (tcpedit->flowlabel > -1) {
  164. memcpy(&ipflags, &ip6_hdr->ip_flags, 4);
  165. ipflags = ntohl(ipflags) & 0xfff00000;
  166. ipflags += tcpedit->flowlabel;
  167. ipflags = htonl(ipflags);
  168. memcpy(&ip6_hdr->ip_flags, &ipflags, 4);
  169. }
  170. /* rewrite TCP/UDP ports */
  171. if (tcpedit->portmap != NULL) {
  172. if ((retval = rewrite_ipv6_ports(tcpedit, &ip6_hdr)) < 0)
  173. return TCPEDIT_ERROR;
  174. needtorecalc += retval;
  175. }
  176. }
  177. /* (Un)truncate or MTU truncate packet? */
  178. if (tcpedit->fixlen || tcpedit->mtu_truncate) {
  179. if ((retval = untrunc_packet(tcpedit, *pkthdr, packet, ip_hdr, ip6_hdr)) < 0)
  180. return TCPEDIT_ERROR;
  181. needtorecalc += retval;
  182. }
  183. /* rewrite IP addresses in IPv4/IPv6 or ARP */
  184. if (tcpedit->rewrite_ip) {
  185. /* IP packets */
  186. if (ip_hdr != NULL) {
  187. if ((retval = rewrite_ipv4l3(tcpedit, ip_hdr, direction)) < 0)
  188. return TCPEDIT_ERROR;
  189. needtorecalc += retval;
  190. } else if (ip6_hdr != NULL) {
  191. if ((retval = rewrite_ipv6l3(tcpedit, ip6_hdr, direction)) < 0)
  192. return TCPEDIT_ERROR;
  193. needtorecalc += retval;
  194. }
  195. /* ARP packets */
  196. else if (l2proto == htons(ETHERTYPE_ARP)) {
  197. arp_hdr = (arp_hdr_t *)&(packet[l2len]);
  198. /* unlike, rewrite_ipl3, we don't care if the packet changed
  199. * because we never need to recalc the checksums for an ARP
  200. * packet. So ignore the return value
  201. */
  202. if (rewrite_iparp(tcpedit, arp_hdr, direction) < 0)
  203. return TCPEDIT_ERROR;
  204. }
  205. }
  206. /* do we need to spoof the src/dst IP address in IPv4 or ARP? */
  207. if (tcpedit->seed) {
  208. /* IPv4 Packets */
  209. if (ip_hdr != NULL) {
  210. if ((retval = randomize_ipv4(tcpedit, *pkthdr, packet,
  211. ip_hdr)) < 0)
  212. return TCPEDIT_ERROR;
  213. needtorecalc += retval;
  214. } else if (ip6_hdr != NULL) {
  215. if ((retval = randomize_ipv6(tcpedit, *pkthdr, packet,
  216. ip6_hdr)) < 0)
  217. return TCPEDIT_ERROR;
  218. needtorecalc += retval;
  219. /* ARP packets */
  220. } else if (l2proto == htons(ETHERTYPE_ARP)) {
  221. if (direction == TCPR_DIR_C2S) {
  222. if (randomize_iparp(tcpedit, *pkthdr, packet,
  223. tcpedit->runtime.dlt1) < 0)
  224. return TCPEDIT_ERROR;
  225. } else {
  226. if (randomize_iparp(tcpedit, *pkthdr, packet,
  227. tcpedit->runtime.dlt2) < 0)
  228. return TCPEDIT_ERROR;
  229. }
  230. }
  231. }
  232. /* do we need to fix checksums? -- must always do this last! */
  233. if ((tcpedit->fixcsum || needtorecalc)) {
  234. if (ip_hdr != NULL) {
  235. retval = fix_ipv4_checksums(tcpedit, *pkthdr, ip_hdr);
  236. } else if (ip6_hdr != NULL) {
  237. retval = fix_ipv6_checksums(tcpedit, *pkthdr, ip6_hdr);
  238. } else {
  239. retval = TCPEDIT_OK;
  240. }
  241. if (retval < 0) {
  242. return TCPEDIT_ERROR;
  243. } else if (retval == TCPEDIT_WARN) {
  244. warnx("%s", tcpedit_getwarn(tcpedit));
  245. }
  246. }
  247. tcpedit_dlt_merge_l3data(tcpedit->dlt_ctx, dst_dlt, packet, (*pkthdr)->caplen, (u_char *)ip_hdr);
  248. tcpedit->runtime.total_bytes += (*pkthdr)->caplen;
  249. tcpedit->runtime.pkts_edited ++;
  250. return retval;
  251. }
  252. /**
  253. * initializes the tcpedit library. returns 0 on success, -1 on error.
  254. */
  255. int
  256. tcpedit_init(tcpedit_t **tcpedit_ex, int dlt)
  257. {
  258. tcpedit_t *tcpedit;
  259. *tcpedit_ex = safe_malloc(sizeof(tcpedit_t));
  260. tcpedit = *tcpedit_ex;
  261. if ((tcpedit->dlt_ctx = tcpedit_dlt_init(tcpedit, dlt)) == NULL)
  262. return TCPEDIT_ERROR;
  263. tcpedit->mtu = DEFAULT_MTU; /* assume 802.3 Ethernet */
  264. /* disabled by default */
  265. tcpedit->tos = -1;
  266. tcpedit->tclass = -1;
  267. tcpedit->flowlabel = -1;
  268. memset(&(tcpedit->runtime), 0, sizeof(tcpedit_runtime_t));
  269. tcpedit->runtime.dlt1 = dlt;
  270. tcpedit->runtime.dlt2 = dlt;
  271. dbgx(1, "Input file (1) datalink type is %s\n",
  272. pcap_datalink_val_to_name(dlt));
  273. #ifdef FORCE_ALIGN
  274. tcpedit->runtime.l3buff = (u_char *)safe_malloc(MAXPACKET);
  275. #endif
  276. return TCPEDIT_OK;
  277. }
  278. /**
  279. * return the output DLT type
  280. */
  281. int
  282. tcpedit_get_output_dlt(tcpedit_t *tcpedit)
  283. {
  284. assert(tcpedit);
  285. return tcpedit_dlt_output_dlt(tcpedit->dlt_ctx);
  286. }
  287. /**
  288. * \brief tcpedit option validator. Call after tcpedit_init()
  289. *
  290. * Validates that given the current state of tcpedit that the given
  291. * pcap source and destination (based on DLT) can be properly rewritten
  292. * return 0 on sucess
  293. * return -1 on error
  294. * DO NOT USE!
  295. */
  296. int
  297. tcpedit_validate(tcpedit_t *tcpedit)
  298. {
  299. assert(tcpedit);
  300. tcpedit->validated = 1;
  301. /* we used to do a bunch of things here, but not anymore...
  302. * maybe I should find something to do or just get ride of it
  303. */
  304. return 0;
  305. }
  306. /**
  307. * return the error string when a tcpedit() function returns
  308. * TCPEDIT_ERROR
  309. */
  310. char *
  311. tcpedit_geterr(tcpedit_t *tcpedit)
  312. {
  313. assert(tcpedit);
  314. return tcpedit->runtime.errstr;
  315. }
  316. /**
  317. * \brief Internal function to set the tcpedit error string
  318. *
  319. * Used to set the error string when there is an error, result is retrieved
  320. * using tcpedit_geterr(). You shouldn't ever actually call this, but use
  321. * tcpedit_seterr() which is a macro wrapping this instead.
  322. */
  323. void
  324. __tcpedit_seterr(tcpedit_t *tcpedit, const char *func, const int line, const char *file, const char *fmt, ...)
  325. {
  326. va_list ap;
  327. char errormsg[TCPEDIT_ERRSTR_LEN];
  328. assert(tcpedit);
  329. va_start(ap, fmt);
  330. if (fmt != NULL) {
  331. (void)vsnprintf(errormsg,
  332. (TCPEDIT_ERRSTR_LEN - 1), fmt, ap);
  333. }
  334. va_end(ap);
  335. snprintf(tcpedit->runtime.errstr, (TCPEDIT_ERRSTR_LEN -1), "From %s:%s() line %d:\n%s",
  336. file, func, line, errormsg);
  337. }
  338. /**
  339. * return the warning string when a tcpedit() function returns
  340. * TCPEDIT_WARN
  341. */
  342. char *
  343. tcpedit_getwarn(tcpedit_t *tcpedit)
  344. {
  345. assert(tcpedit);
  346. return tcpedit->runtime.warnstr;
  347. }
  348. /**
  349. * used to set the warning string when there is an warning
  350. */
  351. void
  352. tcpedit_setwarn(tcpedit_t *tcpedit, const char *fmt, ...)
  353. {
  354. va_list ap;
  355. assert(tcpedit);
  356. va_start(ap, fmt);
  357. if (fmt != NULL)
  358. (void)vsnprintf(tcpedit->runtime.warnstr, (TCPEDIT_ERRSTR_LEN - 1), fmt, ap);
  359. va_end(ap);
  360. }
  361. /**
  362. * \brief Checks the given error code and does the right thing
  363. *
  364. * Generic function which checks the TCPEDIT_* error code
  365. * and always returns OK or ERROR. For warnings, prints the
  366. * warning message and returns OK. For any other value, fails with
  367. * an assert.
  368. *
  369. * prefix is a string prepended to the error/warning
  370. */
  371. int
  372. tcpedit_checkerror(tcpedit_t *tcpedit, const int rcode, const char *prefix) {
  373. assert(tcpedit);
  374. switch (rcode) {
  375. case TCPEDIT_OK:
  376. case TCPEDIT_ERROR:
  377. return rcode;
  378. break;
  379. case TCPEDIT_SOFT_ERROR:
  380. if (prefix != NULL) {
  381. fprintf(stderr, "Error %s: %s\n", prefix, tcpedit_geterr(tcpedit));
  382. } else {
  383. fprintf(stderr, "Error: %s\n", tcpedit_geterr(tcpedit));
  384. }
  385. break;
  386. case TCPEDIT_WARN:
  387. if (prefix != NULL) {
  388. fprintf(stderr, "Warning %s: %s\n", prefix, tcpedit_getwarn(tcpedit));
  389. } else {
  390. fprintf(stderr, "Warning: %s\n", tcpedit_getwarn(tcpedit));
  391. }
  392. return TCPEDIT_OK;
  393. break;
  394. default:
  395. assert(0 == 1); /* this should never happen! */
  396. break;
  397. }
  398. return TCPEDIT_ERROR;
  399. }
  400. /**
  401. * \brief Cleans up after ourselves. Return 0 on success.
  402. *
  403. * Clean up after ourselves, but does not actually free the ptr.
  404. */
  405. int
  406. tcpedit_close(tcpedit_t *tcpedit)
  407. {
  408. assert(tcpedit);
  409. dbgx(1, "tcpedit processed " COUNTER_SPEC " bytes in " COUNTER_SPEC
  410. " packets.\n", tcpedit->runtime.total_bytes,
  411. tcpedit->runtime.pkts_edited);
  412. /* free buffer if required */
  413. #ifdef FORCE_ALIGN
  414. safe_free(tcpedit->runtime.l3buff);
  415. #endif
  416. return 0;
  417. }
  418. /**
  419. * Return a ptr to the Layer 3 data. Returns TCPEDIT_ERROR on error
  420. */
  421. const u_char *
  422. tcpedit_l3data(tcpedit_t *tcpedit, tcpedit_coder_t code, u_char *packet, const int pktlen)
  423. {
  424. u_char *result = NULL;
  425. if (code == BEFORE_PROCESS) {
  426. result = tcpedit_dlt_l3data(tcpedit->dlt_ctx, tcpedit->dlt_ctx->decoder->dlt, packet, pktlen);
  427. } else {
  428. result = tcpedit_dlt_l3data(tcpedit->dlt_ctx, tcpedit->dlt_ctx->encoder->dlt, packet, pktlen);
  429. }
  430. return result;
  431. }
  432. /**
  433. * return the length of the layer 2 header. Returns TCPEDIT_ERROR on error
  434. */
  435. int
  436. tcpedit_l2len(tcpedit_t *tcpedit, tcpedit_coder_t code, u_char *packet, const int pktlen)
  437. {
  438. int result = 0;
  439. if (code == BEFORE_PROCESS) {
  440. result = tcpedit_dlt_l2len(tcpedit->dlt_ctx, tcpedit->dlt_ctx->decoder->dlt, packet, pktlen);
  441. } else {
  442. result = tcpedit_dlt_l2len(tcpedit->dlt_ctx, tcpedit->dlt_ctx->encoder->dlt, packet, pktlen);
  443. }
  444. return result;
  445. }
  446. /**
  447. * Returns the layer 3 type, often encoded as the layer2.proto field
  448. */
  449. int
  450. tcpedit_l3proto(tcpedit_t *tcpedit, tcpedit_coder_t code, const u_char *packet, const int pktlen)
  451. {
  452. int result = 0;
  453. if (code == BEFORE_PROCESS) {
  454. result = tcpedit_dlt_proto(tcpedit->dlt_ctx, tcpedit->dlt_ctx->decoder->dlt, packet, pktlen);
  455. } else {
  456. result = tcpedit_dlt_proto(tcpedit->dlt_ctx, tcpedit->dlt_ctx->encoder->dlt, packet, pktlen);
  457. }
  458. return ntohs(result);
  459. }
  460. /*
  461. u_char *
  462. tcpedit_srcmac(tcpedit_t *tcpedit, tcpedit_coder_t code, u_char *packet, const int pktlen)
  463. {
  464. }
  465. u_char *
  466. tcpedit_dstmac(tcpedit_t *tcpedit, tcpedit_coder_t code, u_char *packet, const int pktlen)
  467. {
  468. }
  469. int
  470. tcpedit_maclen(tcpedit_t *tcpedit, tcpedit_coder_t code)
  471. {
  472. }
  473. */