fragroute.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * fragroute.c
  3. *
  4. * Copyright (c) 2001 Dug Song <dugsong@monkey.org>
  5. * Copyright (c) 2007-2008 Aaron Turner.
  6. * Copyright (c) 2013-2024 Fred Klassen - AppNeta
  7. * $Id$
  8. */
  9. #include "defines.h"
  10. #include "config.h"
  11. #include "common.h"
  12. #include "lib/queue.h"
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #ifdef HAVE_LIBDNET
  17. /* need to undef these which are pulled in via defines.h, prior to importing dnet.h */
  18. #undef icmp_id
  19. #undef icmp_seq
  20. #undef icmp_data
  21. #undef icmp_mask
  22. #ifdef HAVE_DNET_H
  23. #include <dnet.h>
  24. #endif
  25. #ifdef HAVE_DUMBNET_H
  26. #include <dumbnet.h>
  27. #endif
  28. #endif
  29. #include "fragroute.h"
  30. #include "mod.h"
  31. #include "pkt.h"
  32. void
  33. fragroute_close(fragroute_t *ctx)
  34. {
  35. assert(ctx);
  36. free(ctx->pktq);
  37. free(ctx);
  38. pkt_close();
  39. }
  40. int
  41. fragroute_process(fragroute_t *ctx, void *buf, size_t len)
  42. {
  43. struct pkt *pkt;
  44. assert(ctx);
  45. assert(buf);
  46. ctx->first_packet = 0;
  47. /* save the l2 header of the original packet for later */
  48. ctx->l2len = get_l2len(buf, (int)len, ctx->dlt);
  49. memcpy(ctx->l2header, buf, ctx->l2len);
  50. if ((pkt = pkt_new(len)) == NULL) {
  51. strcpy(ctx->errbuf, "unable to pkt_new()");
  52. return -1;
  53. }
  54. memcpy(pkt->pkt_data, buf, len);
  55. pkt->pkt_end = pkt->pkt_data + len;
  56. pkt_decorate(pkt);
  57. if (pkt->pkt_ip == NULL) {
  58. strcpy(ctx->errbuf, "skipping non-IP packet");
  59. return -1;
  60. }
  61. /* Don't always checksum packets before being fragged
  62. if (pkt->pkt_eth && htons(pkt->pkt_eth->eth_type) == ETH_TYPE_IP) {
  63. ip_checksum(pkt->pkt_ip, len);
  64. }
  65. */
  66. TAILQ_INIT(ctx->pktq);
  67. TAILQ_INSERT_TAIL(ctx->pktq, pkt, pkt_next);
  68. mod_apply(ctx->pktq);
  69. return 0;
  70. }
  71. /*
  72. * keep calling this after fragroute_process() to get all the fragments.
  73. * Each call returns the fragment length which is stored in **packet.
  74. * Returns 0 when no more fragments remain or -1 on error
  75. */
  76. int
  77. fragroute_getfragment(fragroute_t *ctx, char **packet)
  78. {
  79. static struct pkt *pkt = NULL;
  80. static struct pkt *next = NULL;
  81. char *pkt_data = *packet;
  82. u_int32_t length;
  83. if (ctx->first_packet != 0) {
  84. pkt = next;
  85. } else {
  86. ctx->first_packet = 1;
  87. pkt = TAILQ_FIRST(ctx->pktq);
  88. }
  89. if (pkt != TAILQ_END(&(ctx->pktq))) {
  90. next = TAILQ_NEXT(pkt, pkt_next);
  91. memcpy(pkt_data, pkt->pkt_data, pkt->pkt_end - pkt->pkt_data);
  92. /* return the original L2 header */
  93. memcpy(pkt_data, ctx->l2header, ctx->l2len);
  94. length = pkt->pkt_end - pkt->pkt_data;
  95. pkt = next;
  96. return (int)length;
  97. }
  98. return 0; // nothing
  99. }
  100. fragroute_t *
  101. fragroute_init(const int mtu, const int dlt, const char *config, char *errbuf)
  102. {
  103. fragroute_t *ctx;
  104. if (dlt != DLT_EN10MB) {
  105. sprintf(errbuf, "Fragroute only supports DLT_EN10MB pcap files");
  106. return NULL;
  107. }
  108. ctx = (fragroute_t *)safe_malloc(sizeof(fragroute_t));
  109. ctx->pktq = (struct pktq *)safe_malloc(sizeof(struct pktq));
  110. ctx->dlt = dlt;
  111. pkt_init(128);
  112. ctx->mtu = mtu;
  113. /* parse the config */
  114. if (mod_open(config, errbuf) < 0) {
  115. fragroute_close(ctx);
  116. return NULL;
  117. }
  118. return ctx;
  119. }