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