fragroute.c 3.3 KB

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