tcpedit.c 18 KB

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