tcpedit.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* $Id: tcpedit.h 1550 2006-07-31 02:58:32Z aturner $ */
  2. /*
  3. * Copyright (c) 2001-2006 Aaron Turner.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "defines.h"
  32. #include "dlt.h"
  33. #include "../common.h"
  34. #ifndef _TCPEDIT_H_
  35. #define _TCPEDIT_H_
  36. #define L2DATALEN 255 /* Max size of the L2 data file */
  37. struct l2_s {
  38. int enabled; /* are we rewritting the L2 header ? */
  39. int len; /* user data length */
  40. u_char data1[L2DATALEN];
  41. u_char data2[L2DATALEN];
  42. /*
  43. * we need to store the *new* linktype which we will then use to
  44. * select the correct union slice. set to LINKTYPE_USER to
  45. * use the user specified data (data1[] & data2[])
  46. * other valid options are LINKTYPE_VLAN and LINKTYPE_ETHER for
  47. * 802.1q and standard ethernet frames respectively.
  48. */
  49. int dlt;
  50. #define DLT_USER DLT_USER0
  51. #define DLT_VLAN DLT_USER1
  52. u_int16_t vlan_tag;
  53. u_int8_t vlan_pri;
  54. u_int8_t vlan_cfi;
  55. };
  56. typedef struct l2_s l2_t;
  57. #define TCPEDIT_ERRSTR_LEN 1024
  58. struct tcpedit_runtime_s {
  59. COUNTER packetnum;
  60. COUNTER total_bytes;
  61. COUNTER pkts_edited;
  62. pcap_t *pcap1;
  63. pcap_t *pcap2;
  64. char errstr[TCPEDIT_ERRSTR_LEN];
  65. #ifdef FORCE_ALIGN
  66. u_char *ipbuff = NULL; /* IP header and above buffer */
  67. #endif
  68. };
  69. typedef struct tcpedit_runtime_s tcpedit_runtime_t;
  70. /*
  71. * portmap data struct
  72. */
  73. struct tcpedit_portmap_s {
  74. long from;
  75. long to;
  76. struct tcpedit_portmap_s *next;
  77. };
  78. typedef struct tcpedit_portmap_s tcpedit_portmap_t;
  79. /*
  80. * all the arguments that the packet editing library supports
  81. */
  82. struct tcpedit_s {
  83. int validated; /* have we run tcpedit_validate()? */
  84. /* runtime variables, don't mess with these */
  85. tcpedit_runtime_t runtime;
  86. /* skip rewriting IP/MAC's which are broadcast or multicast? */
  87. int skip_broadcast;
  88. /* we use the mask to say which are valid values */
  89. int mac_mask;
  90. #define TCPEDIT_MAC_MASK_SMAC1 0x1
  91. #define TCPEDIT_MAC_MASK_SMAC2 0x2
  92. #define TCPEDIT_MAC_MASK_DMAC1 0x4
  93. #define TCPEDIT_MAC_MASK_DMAC2 0x8
  94. /* rewrite traffic bi-directionally */
  95. int bidir;
  96. #define TCPEDIT_BIDIR_OFF 0x0
  97. #define TCPEDIT_BIDIR_ON 0x1
  98. /* 802.1q VLAN tag stuff */
  99. int vlan;
  100. #define TCPEDIT_VLAN_OFF 0x0
  101. #define TCPEDIT_VLAN_DEL 0x1 /* strip 802.1q and rewrite as standard
  102. * 802.3 Ethernet */
  103. #define TCPEDIT_VLAN_ADD 0x2 /* add/replace 802.1q vlan tag */
  104. /* pad or truncate packets */
  105. int fixlen;
  106. #define TCPEDIT_FIXLEN_OFF 0x0
  107. #define TCPEDIT_FIXLEN_PAD 0x1
  108. #define TCPEDIT_FIXLEN_TRUNC 0x2
  109. #define TCPEDIT_FIXLEN_DEL 0x3
  110. /* rewrite ip? */
  111. int rewrite_ip;
  112. #define TCPEDIT_REWRITE_IP_OFF 0x0
  113. #define TCPEDIT_REWRITE_IP_ON 0x1
  114. /* fix IP/TCP/UDP checksums */
  115. int fixcsum;
  116. #define TCPEDIT_FIXCSUM_OFF 0x0
  117. #define TCPEDIT_FIXCSUM_ON 0x1
  118. /* remove ethernet FCS */
  119. int efcs;
  120. #define TCPEDIT_EFCS_OFF 0x0
  121. #define TCPEDIT_EFCS_ON 0x1
  122. // char padding1; /* keep things 4 byte aligned */
  123. /* values to rewrite src/dst MAC addresses */
  124. tcpr_macaddr_t intf1_dmac;
  125. tcpr_macaddr_t intf1_smac;
  126. tcpr_macaddr_t intf2_dmac;
  127. tcpr_macaddr_t intf2_smac;
  128. /* other L2 editing options */
  129. u_int16_t l2proto;
  130. u_int16_t l2_mem_align; /* keep things 4 byte aligned */
  131. /* rewrite L2 data in full */
  132. l2_t l2;
  133. /* rewrite end-point IP addresses between cidrmap1 & cidrmap2 */
  134. tcpr_cidrmap_t *cidrmap1; /* tcpprep cache data */
  135. tcpr_cidrmap_t *cidrmap2;
  136. /* pseudo-randomize IP addresses using a seed */
  137. int seed;
  138. /* rewrite tcp/udp ports */
  139. tcpedit_portmap_t *portmap;
  140. int mtu; /* Deal with different MTU's */
  141. int maxpacket; /* L2 header + MTU */
  142. };
  143. typedef struct tcpedit_s tcpedit_t;
  144. int tcpedit_init(tcpedit_t *tcpedit, pcap_t *pcap1, pcap_t *pcap2);
  145. char *tcpedit_geterr(tcpedit_t *tcpedit);
  146. void tcpedit_seterr(tcpedit_t *tcpedit, const char *fmt, ...);
  147. int tcpedit_validate(tcpedit_t *tcpedit, int srcdlt, int dstdlt);
  148. int tcpedit_packet(tcpedit_t *tcpedit, struct pcap_pkthdr **pkthdr,
  149. u_char **pktdata, int direction);
  150. int tcpedit_close(tcpedit_t *tcpedit);
  151. COUNTER tcpedit_get_total_bytes(tcpedit_t *tcpedit);
  152. COUNTER tcpedit_get_pkts_edited(tcpedit_t *tcpedit);
  153. #endif