tcpedit.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2024 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. *
  6. * The Tcpreplay Suite of tools is free software: you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or with the authors permission any later version.
  10. *
  11. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "defines.h"
  20. #include "config.h"
  21. #include "common.h"
  22. #include "edit_packet.h"
  23. #include "fuzzing.h"
  24. #include "incremental_checksum.h"
  25. #include "parse_args.h"
  26. #include "portmap.h"
  27. #include "rewrite_sequence.h"
  28. #include "tcpedit_stub.h"
  29. #include <stdarg.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <sys/types.h>
  33. /**
  34. * \brief Edit the given packet
  35. *
  36. * Process a given packet and edit the pkthdr/pktdata structures
  37. * according to the rules in tcpedit
  38. * Returns: TCPEDIT_ERROR on error
  39. * TCPEDIT_SOFT_ERROR on remove packet
  40. * 0 on no change
  41. * 1 on change
  42. */
  43. int
  44. tcpedit_packet(tcpedit_t *tcpedit, struct pcap_pkthdr **pkthdr, u_char **pktdata, tcpr_dir_t direction)
  45. {
  46. bool fuzz_once = tcpedit->fuzz_seed != 0;
  47. ipv4_hdr_t *ip_hdr;
  48. ipv6_hdr_t *ip6_hdr;
  49. arp_hdr_t *arp_hdr;
  50. int l2len, l2proto, retval;
  51. int dst_dlt, src_dlt, pktlen, lendiff;
  52. uint32_t ipflags, tclass;
  53. int needtorecalc; /* did the packet change? if so, checksum */
  54. u_char *packet;
  55. assert(tcpedit);
  56. assert(pkthdr);
  57. assert(*pkthdr);
  58. assert(pktdata);
  59. assert(*pktdata);
  60. assert(tcpedit->validated);
  61. packet = *pktdata;
  62. tcpedit->runtime.packetnum++;
  63. dbgx(3, "packet " COUNTER_SPEC " caplen %d", tcpedit->runtime.packetnum, (*pkthdr)->caplen);
  64. /*
  65. * remove the Ethernet FCS (checksum)?
  66. * note that this feature requires the end user to be smart and
  67. * only set this flag IFF the pcap has the FCS. If not, then they
  68. * just removed 2 bytes of ACTUAL PACKET DATA. Sucks to be them.
  69. */
  70. if (tcpedit->efcs > 0 && (*pkthdr)->len > 4) {
  71. if ((*pkthdr)->caplen == (*pkthdr)->len) {
  72. (*pkthdr)->caplen -= 4;
  73. }
  74. (*pkthdr)->len -= 4;
  75. }
  76. src_dlt = tcpedit_dlt_src(tcpedit->dlt_ctx);
  77. needtorecalc = 0;
  78. again:
  79. ip_hdr = NULL;
  80. ip6_hdr = NULL;
  81. arp_hdr = NULL;
  82. retval = 0;
  83. ipflags = 0;
  84. /* not everything has a L3 header, so check for errors. returns proto in network byte order */
  85. if ((l2proto = tcpedit_dlt_proto(tcpedit->dlt_ctx, src_dlt, packet, (int)(*pkthdr)->caplen)) < 0) {
  86. dbgx(2, "Packet " COUNTER_SPEC " has no L3+ header: %s", tcpedit->runtime.packetnum, tcpedit_geterr(tcpedit));
  87. return TCPEDIT_SOFT_ERROR;
  88. } else {
  89. dbgx(2, "Layer 3 protocol type is: 0x%04x", ntohs(l2proto));
  90. }
  91. /* rewrite Layer 2 */
  92. if ((pktlen = tcpedit_dlt_process(tcpedit->dlt_ctx, pktdata, (int)(*pkthdr)->caplen, direction)) < 0) {
  93. /* unable to edit packet, most likely 802.11 management or data QoS frame */
  94. dbgx(3, "Failed to edit DLT: %s", tcpedit_geterr(tcpedit));
  95. return TCPEDIT_SOFT_ERROR;
  96. }
  97. /* update our packet lengths (real/captured) based on L2 length changes */
  98. lendiff = pktlen - (int)(*pkthdr)->caplen;
  99. (*pkthdr)->caplen += lendiff;
  100. (*pkthdr)->len += lendiff;
  101. dst_dlt = tcpedit_dlt_dst(tcpedit->dlt_ctx);
  102. l2len = tcpedit_dlt_l2len(tcpedit->dlt_ctx, dst_dlt, packet, (int)(*pkthdr)->caplen);
  103. if (l2len == -1)
  104. return TCPEDIT_SOFT_ERROR;
  105. dbgx(2, "dst_dlt = %04x\tsrc_dlt = %04x\tproto = %04x\tl2len = %d", dst_dlt, src_dlt, ntohs(l2proto), l2len);
  106. /* does packet have an IP header? if so set our pointer to it */
  107. if (l2proto == htons(ETHERTYPE_IP)) {
  108. u_char *p;
  109. if ((*pkthdr)->caplen < l2len + sizeof(*ip_hdr)) {
  110. tcpedit_seterr(tcpedit,
  111. "Packet length %d is too short to contain a layer IP header for DLT 0x%04x",
  112. pktlen,
  113. dst_dlt);
  114. return TCPEDIT_SOFT_ERROR;
  115. }
  116. ip_hdr = (ipv4_hdr_t *)tcpedit_dlt_l3data(tcpedit->dlt_ctx, dst_dlt, packet, (int)(*pkthdr)->caplen);
  117. if (ip_hdr == NULL)
  118. return TCPEDIT_SOFT_ERROR;
  119. p = get_layer4_v4(ip_hdr, (u_char *)ip_hdr + (*pkthdr)->caplen - l2len);
  120. if (!p) {
  121. tcpedit_seterr(tcpedit,
  122. "Packet length %d is too short to contain a layer %d byte IP header for DLT 0x%04x",
  123. pktlen,
  124. ip_hdr->ip_hl << 2,
  125. dst_dlt);
  126. return TCPEDIT_SOFT_ERROR;
  127. }
  128. dbgx(3, "Packet has an IPv4 header: 0x%p...", ip_hdr);
  129. } else if (l2proto == htons(ETHERTYPE_IP6)) {
  130. u_char *p;
  131. if ((*pkthdr)->caplen < l2len + sizeof(*ip6_hdr)) {
  132. tcpedit_seterr(tcpedit,
  133. "Packet length %d is too short to contain a layer IPv6 header for DLT 0x%04x",
  134. pktlen,
  135. dst_dlt);
  136. return TCPEDIT_SOFT_ERROR;
  137. }
  138. ip6_hdr = (ipv6_hdr_t *)tcpedit_dlt_l3data(tcpedit->dlt_ctx, dst_dlt, packet, (int)(*pkthdr)->caplen);
  139. if (ip6_hdr == NULL)
  140. return TCPEDIT_SOFT_ERROR;
  141. p = get_layer4_v6(ip6_hdr, (u_char *)ip6_hdr + (*pkthdr)->caplen - l2len);
  142. if (!p) {
  143. tcpedit_seterr(tcpedit,
  144. "Packet length %d is too short to contain an IPv6 header for DLT 0x%04x",
  145. pktlen,
  146. dst_dlt);
  147. return TCPEDIT_SOFT_ERROR;
  148. }
  149. dbgx(3, "Packet has an IPv6 header: 0x%p...", ip6_hdr);
  150. } else {
  151. dbgx(3, "Packet isn't IPv4 or IPv6: 0x%04x", l2proto);
  152. /* non-IP packets have a NULL ip_hdr struct */
  153. ip_hdr = NULL;
  154. ip6_hdr = NULL;
  155. }
  156. /* The following edits only apply for IPv4 */
  157. if (ip_hdr != NULL) {
  158. /* set TOS ? */
  159. if (tcpedit->tos > -1) {
  160. volatile uint16_t oldval = *((uint16_t *)ip_hdr);
  161. volatile uint16_t newval;
  162. newval = *((uint16_t *)ip_hdr);
  163. newval = htons((ntohs(newval) & 0xff00) | (tcpedit->tos & 0xff));
  164. *((uint16_t *)ip_hdr) = newval;
  165. csum_replace2(&ip_hdr->ip_sum, oldval, newval);
  166. }
  167. /* rewrite the TTL */
  168. needtorecalc += rewrite_ipv4_ttl(tcpedit, ip_hdr);
  169. /* rewrite TCP/UDP ports */
  170. if (tcpedit->portmap != NULL) {
  171. if ((retval = rewrite_ipv4_ports(tcpedit, &ip_hdr, (int)(*pkthdr)->caplen - l2len)) < 0)
  172. return TCPEDIT_ERROR;
  173. needtorecalc += retval;
  174. }
  175. if (tcpedit->tcp_sequence_enable)
  176. rewrite_ipv4_tcp_sequence(tcpedit, &ip_hdr, (int)(*pkthdr)->caplen - l2len);
  177. }
  178. /* IPv6 edits */
  179. else if (ip6_hdr != NULL) {
  180. /* rewrite the hop limit */
  181. needtorecalc += rewrite_ipv6_hlim(tcpedit, ip6_hdr);
  182. /* set traffic class? */
  183. if (tcpedit->tclass > -1) {
  184. /* calculate the bits */
  185. tclass = tcpedit->tclass << 20;
  186. /* convert our 4 bytes to an int */
  187. memcpy(&ipflags, &ip6_hdr->ip_flags, 4);
  188. /* strip out the old tclass bits */
  189. ipflags = ntohl(ipflags) & 0xf00fffff;
  190. /* add the tclass bits back */
  191. ipflags += tclass;
  192. ipflags = htonl(ipflags);
  193. memcpy(&ip6_hdr->ip_flags, &ipflags, 4);
  194. }
  195. /* set the flow label? */
  196. if (tcpedit->flowlabel > -1) {
  197. memcpy(&ipflags, &ip6_hdr->ip_flags, 4);
  198. ipflags = ntohl(ipflags) & 0xfff00000;
  199. ipflags += tcpedit->flowlabel;
  200. ipflags = htonl(ipflags);
  201. memcpy(&ip6_hdr->ip_flags, &ipflags, 4);
  202. }
  203. /* rewrite TCP/UDP ports */
  204. if (tcpedit->portmap != NULL) {
  205. if ((retval = rewrite_ipv6_ports(tcpedit, &ip6_hdr, (int)(*pkthdr)->caplen - l2len)) < 0)
  206. return TCPEDIT_ERROR;
  207. needtorecalc += retval;
  208. }
  209. if (tcpedit->tcp_sequence_enable)
  210. rewrite_ipv6_tcp_sequence(tcpedit, &ip6_hdr, (int)(*pkthdr)->caplen - l2len);
  211. }
  212. if (fuzz_once) {
  213. fuzz_once = false;
  214. retval = fuzzing(tcpedit, *pkthdr, pktdata);
  215. if (retval < 0) {
  216. return TCPEDIT_ERROR;
  217. }
  218. needtorecalc += retval;
  219. goto again;
  220. }
  221. /* (Un)truncate or MTU truncate packet? */
  222. if (tcpedit->fixlen || tcpedit->mtu_truncate) {
  223. if ((retval = untrunc_packet(tcpedit, *pkthdr, pktdata, ip_hdr, ip6_hdr)) < 0)
  224. return TCPEDIT_ERROR;
  225. needtorecalc += retval;
  226. }
  227. /* rewrite IP addresses in IPv4/IPv6 or ARP */
  228. if (tcpedit->rewrite_ip) {
  229. /* IP packets */
  230. if (ip_hdr != NULL) {
  231. if ((retval = rewrite_ipv4l3(tcpedit, ip_hdr, direction, (int)(*pkthdr)->caplen - l2len)) < 0)
  232. return TCPEDIT_ERROR;
  233. needtorecalc += retval;
  234. } else if (ip6_hdr != NULL) {
  235. if ((retval = rewrite_ipv6l3(tcpedit, ip6_hdr, direction, (int)(*pkthdr)->caplen - l2len)) < 0)
  236. return TCPEDIT_ERROR;
  237. needtorecalc += retval;
  238. }
  239. /* ARP packets */
  240. else if (l2proto == htons(ETHERTYPE_ARP)) {
  241. arp_hdr = (arp_hdr_t *)&(packet[l2len]);
  242. /* unlike, rewrite_ipl3, we don't care if the packet changed
  243. * because we never need to recalc the checksums for an ARP
  244. * packet. So ignore the return value
  245. */
  246. if (rewrite_iparp(tcpedit, arp_hdr, direction) < 0)
  247. return TCPEDIT_ERROR;
  248. }
  249. }
  250. /* do we need to spoof the src/dst IP address in IPv4 or ARP? */
  251. if (tcpedit->seed) {
  252. /* IPv4 Packets */
  253. if (ip_hdr != NULL) {
  254. if ((retval = randomize_ipv4(tcpedit, *pkthdr, packet, ip_hdr, (int)(*pkthdr)->caplen - l2len)) < 0)
  255. return TCPEDIT_ERROR;
  256. needtorecalc += retval;
  257. } else if (ip6_hdr != NULL) {
  258. if ((retval = randomize_ipv6(tcpedit, *pkthdr, packet, ip6_hdr, (int)(*pkthdr)->caplen - l2len)) < 0)
  259. return TCPEDIT_ERROR;
  260. needtorecalc += retval;
  261. /* ARP packets */
  262. } else if (l2proto == htons(ETHERTYPE_ARP)) {
  263. if (direction == TCPR_DIR_C2S) {
  264. if (randomize_iparp(tcpedit, *pkthdr, packet, tcpedit->runtime.dlt1, (int)(*pkthdr)->caplen - l2len) <
  265. 0)
  266. return TCPEDIT_ERROR;
  267. } else {
  268. if (randomize_iparp(tcpedit, *pkthdr, packet, tcpedit->runtime.dlt2, (int)(*pkthdr)->caplen - l2len) <
  269. 0)
  270. return TCPEDIT_ERROR;
  271. }
  272. }
  273. }
  274. /* fixhdrlen option ensure IP header length is correct */
  275. /* do we need to fix checksums? -- must always do this last! */
  276. if (tcpedit->fixhdrlen) {
  277. /* ensure IP header length is correct */
  278. int changed = 0;
  279. if (ip_hdr != NULL) {
  280. changed = fix_ipv4_length(*pkthdr, ip_hdr, l2len);
  281. } else if (ip6_hdr != NULL) {
  282. changed = fix_ipv6_length(*pkthdr, ip6_hdr, l2len);
  283. }
  284. /* did the packet change? then needtorecalc checksum */
  285. if (changed > 0) {
  286. needtorecalc |= changed;
  287. }
  288. }
  289. /* do we need to fix checksums? -- must always do this last! */
  290. if ((tcpedit->fixcsum || needtorecalc > 0)) {
  291. if (ip_hdr != NULL) {
  292. dbgx(3, "doing IPv4 checksum: needtorecalc=%d", needtorecalc);
  293. retval = fix_ipv4_checksums(tcpedit, *pkthdr, ip_hdr, l2len);
  294. } else if (ip6_hdr != NULL) {
  295. dbgx(3, "doing IPv6 checksum: needtorecalc=%d", needtorecalc);
  296. retval = fix_ipv6_checksums(tcpedit, *pkthdr, ip6_hdr, l2len);
  297. } else {
  298. dbgx(3, "checksum not performed: needtorecalc=%d", needtorecalc);
  299. retval = TCPEDIT_OK;
  300. }
  301. if (retval < 0) {
  302. return TCPEDIT_ERROR;
  303. } else if (retval == TCPEDIT_WARN) {
  304. warnx("%s", tcpedit_getwarn(tcpedit));
  305. }
  306. }
  307. tcpedit_dlt_merge_l3data(tcpedit->dlt_ctx,
  308. dst_dlt,
  309. packet,
  310. (int)(*pkthdr)->caplen,
  311. (u_char *)ip_hdr,
  312. (u_char *)ip6_hdr);
  313. tcpedit->runtime.total_bytes += (*pkthdr)->caplen;
  314. tcpedit->runtime.pkts_edited++;
  315. return retval;
  316. }
  317. /**
  318. * initializes the tcpedit library. returns 0 on success, -1 on error.
  319. */
  320. int
  321. tcpedit_init(tcpedit_t **tcpedit_ex, int dlt)
  322. {
  323. tcpedit_t *tcpedit;
  324. *tcpedit_ex = safe_malloc(sizeof(tcpedit_t));
  325. tcpedit = *tcpedit_ex;
  326. if ((tcpedit->dlt_ctx = tcpedit_dlt_init(tcpedit, dlt)) == NULL)
  327. return TCPEDIT_ERROR;
  328. tcpedit->mtu = DEFAULT_MTU; /* assume 802.3 Ethernet */
  329. tcpedit->fuzz_factor = DEFAULT_FUZZ_FACTOR;
  330. /* disabled by default */
  331. tcpedit->tos = -1;
  332. tcpedit->tclass = -1;
  333. tcpedit->flowlabel = -1;
  334. tcpedit->editdir = TCPEDIT_EDIT_BOTH;
  335. memset(&(tcpedit->runtime), 0, sizeof(tcpedit_runtime_t));
  336. tcpedit->runtime.dlt1 = dlt;
  337. tcpedit->runtime.dlt2 = dlt;
  338. dbgx(1, "Input file (1) datalink type is %s", pcap_datalink_val_to_name(dlt));
  339. #ifdef FORCE_ALIGN
  340. tcpedit->runtime.l3buff = (u_char *)safe_malloc(MAXPACKET);
  341. #endif
  342. return TCPEDIT_OK;
  343. }
  344. /**
  345. * return the output DLT type
  346. */
  347. int
  348. tcpedit_get_output_dlt(tcpedit_t *tcpedit)
  349. {
  350. assert(tcpedit);
  351. return tcpedit_dlt_output_dlt(tcpedit->dlt_ctx);
  352. }
  353. /**
  354. * \brief tcpedit option validator. Call after tcpedit_init()
  355. *
  356. * Validates that given the current state of tcpedit that the given
  357. * pcap source and destination (based on DLT) can be properly rewritten
  358. * return 0 on success
  359. * return -1 on error
  360. * DO NOT USE!
  361. */
  362. int
  363. tcpedit_validate(tcpedit_t *tcpedit)
  364. {
  365. assert(tcpedit);
  366. tcpedit->validated = 1;
  367. /* we used to do a bunch of things here, but not anymore...
  368. * maybe I should find something to do or just get ride of it
  369. */
  370. return 0;
  371. }
  372. /**
  373. * return the error string when a tcpedit() function returns
  374. * TCPEDIT_ERROR
  375. */
  376. char *
  377. tcpedit_geterr(tcpedit_t *tcpedit)
  378. {
  379. assert(tcpedit);
  380. return tcpedit->runtime.errstr;
  381. }
  382. /**
  383. * \brief Internal function to set the tcpedit error string
  384. *
  385. * Used to set the error string when there is an error, result is retrieved
  386. * using tcpedit_geterr(). You shouldn't ever actually call this, but use
  387. * tcpedit_seterr() which is a macro wrapping this instead.
  388. */
  389. void
  390. __tcpedit_seterr(tcpedit_t *tcpedit, const char *func, int line, const char *file, const char *fmt, ...)
  391. {
  392. va_list ap;
  393. char errormsg[TCPEDIT_ERRSTR_LEN - 32];
  394. assert(tcpedit);
  395. va_start(ap, fmt);
  396. if (fmt != NULL) {
  397. (void)vsnprintf(errormsg, sizeof(errormsg), fmt, ap);
  398. }
  399. va_end(ap);
  400. snprintf(tcpedit->runtime.errstr,
  401. sizeof(tcpedit->runtime.errstr),
  402. "From %s:%s() line %d:\n%s",
  403. file,
  404. func,
  405. line,
  406. errormsg);
  407. }
  408. /**
  409. * return the warning string when a tcpedit() function returns
  410. * TCPEDIT_WARN
  411. */
  412. char *
  413. tcpedit_getwarn(tcpedit_t *tcpedit)
  414. {
  415. assert(tcpedit);
  416. return tcpedit->runtime.warnstr;
  417. }
  418. /**
  419. * used to set the warning string when there is an warning
  420. */
  421. void
  422. tcpedit_setwarn(tcpedit_t *tcpedit, const char *fmt, ...)
  423. {
  424. va_list ap;
  425. assert(tcpedit);
  426. va_start(ap, fmt);
  427. if (fmt != NULL)
  428. (void)vsnprintf(tcpedit->runtime.warnstr, sizeof(tcpedit->runtime.warnstr), fmt, ap);
  429. va_end(ap);
  430. }
  431. /**
  432. * \brief Checks the given error code and does the right thing
  433. *
  434. * Generic function which checks the TCPEDIT_* error code
  435. * and always returns OK or ERROR. For warnings, prints the
  436. * warning message and returns OK. For any other value, fails with
  437. * an assert.
  438. *
  439. * prefix is a string prepended to the error/warning
  440. */
  441. int
  442. tcpedit_checkerror(tcpedit_t *tcpedit, int rcode, const char *prefix)
  443. {
  444. assert(tcpedit);
  445. switch (rcode) {
  446. case TCPEDIT_OK:
  447. case TCPEDIT_ERROR:
  448. return rcode;
  449. case TCPEDIT_SOFT_ERROR:
  450. if (prefix != NULL) {
  451. fprintf(stderr, "Error %s: %s\n", prefix, tcpedit_geterr(tcpedit));
  452. } else {
  453. fprintf(stderr, "Error: %s\n", tcpedit_geterr(tcpedit));
  454. }
  455. break;
  456. case TCPEDIT_WARN:
  457. if (prefix != NULL) {
  458. fprintf(stderr, "Warning %s: %s\n", prefix, tcpedit_getwarn(tcpedit));
  459. } else {
  460. fprintf(stderr, "Warning: %s\n", tcpedit_getwarn(tcpedit));
  461. }
  462. return TCPEDIT_OK;
  463. default:
  464. assert(0 == 1); /* this should never happen! */
  465. break;
  466. }
  467. return TCPEDIT_ERROR;
  468. }
  469. /**
  470. * \brief Cleans up after ourselves. Return 0 on success.
  471. *
  472. * Clean up after ourselves and free the ptr.
  473. */
  474. int
  475. tcpedit_close(tcpedit_t **tcpedit_ex)
  476. {
  477. assert(*tcpedit_ex);
  478. tcpedit_t *tcpedit;
  479. tcpedit = *tcpedit_ex;
  480. dbgx(1,
  481. "tcpedit processed " COUNTER_SPEC " bytes in " COUNTER_SPEC " packets.",
  482. tcpedit->runtime.total_bytes,
  483. tcpedit->runtime.pkts_edited);
  484. /* free if required */
  485. if (tcpedit->dlt_ctx) {
  486. tcpedit_dlt_cleanup(tcpedit->dlt_ctx);
  487. tcpedit->dlt_ctx = NULL;
  488. }
  489. if (tcpedit->cidrmap1) {
  490. destroy_cidr(tcpedit->cidrmap1->from);
  491. tcpedit->cidrmap1->from = NULL;
  492. destroy_cidr(tcpedit->cidrmap1->to);
  493. tcpedit->cidrmap1->to = NULL;
  494. }
  495. if (tcpedit->cidrmap2 && tcpedit->cidrmap2 != tcpedit->cidrmap1) {
  496. destroy_cidr(tcpedit->cidrmap2->from);
  497. tcpedit->cidrmap2->from = NULL;
  498. destroy_cidr(tcpedit->cidrmap2->to);
  499. tcpedit->cidrmap2->to = NULL;
  500. safe_free(tcpedit->cidrmap2);
  501. tcpedit->cidrmap2 = NULL;
  502. }
  503. safe_free(tcpedit->cidrmap1);
  504. tcpedit->cidrmap1 = NULL;
  505. if (tcpedit->srcipmap) {
  506. destroy_cidr(tcpedit->srcipmap->from);
  507. tcpedit->srcipmap->from = NULL;
  508. destroy_cidr(tcpedit->srcipmap->to);
  509. tcpedit->srcipmap->to = NULL;
  510. }
  511. if (tcpedit->dstipmap && tcpedit->dstipmap != tcpedit->srcipmap) {
  512. destroy_cidr(tcpedit->dstipmap->from);
  513. tcpedit->dstipmap->from = NULL;
  514. destroy_cidr(tcpedit->dstipmap->to);
  515. tcpedit->dstipmap->to = NULL;
  516. safe_free(tcpedit->dstipmap);
  517. tcpedit->dstipmap = NULL;
  518. }
  519. safe_free(tcpedit->srcipmap);
  520. tcpedit->srcipmap = NULL;
  521. if (tcpedit->portmap) {
  522. free_portmap(tcpedit->portmap);
  523. tcpedit->portmap = NULL;
  524. }
  525. #ifdef FORCE_ALIGN
  526. safe_free(tcpedit->runtime.l3buff);
  527. tcpedit->runtime.l3buff = NULL;
  528. #endif
  529. safe_free(*tcpedit_ex);
  530. *tcpedit_ex = NULL;
  531. return 0;
  532. }
  533. /**
  534. * Return a ptr to the Layer 3 data. Returns TCPEDIT_ERROR on error
  535. */
  536. const u_char *
  537. tcpedit_l3data(tcpedit_t *tcpedit, tcpedit_coder code, u_char *packet, int pktlen)
  538. {
  539. u_char *result = NULL;
  540. if (code == BEFORE_PROCESS) {
  541. result = tcpedit_dlt_l3data(tcpedit->dlt_ctx, tcpedit->dlt_ctx->decoder->dlt, packet, pktlen);
  542. } else {
  543. result = tcpedit_dlt_l3data(tcpedit->dlt_ctx, tcpedit->dlt_ctx->encoder->dlt, packet, pktlen);
  544. }
  545. return result;
  546. }
  547. /**
  548. * Returns the layer 3 type, often encoded as the layer2.proto field
  549. */
  550. int
  551. tcpedit_l3proto(tcpedit_t *tcpedit, tcpedit_coder code, const u_char *packet, int pktlen)
  552. {
  553. int result;
  554. if (code == BEFORE_PROCESS) {
  555. result = tcpedit_dlt_proto(tcpedit->dlt_ctx, tcpedit->dlt_ctx->decoder->dlt, packet, pktlen);
  556. } else {
  557. result = tcpedit_dlt_proto(tcpedit->dlt_ctx, tcpedit->dlt_ctx->encoder->dlt, packet, pktlen);
  558. }
  559. return ntohs(result);
  560. }