tcpedit.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* $Id: tcpedit.h 1505 2006-07-17 02:56:37Z 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. libnet_t *lnet;
  65. char errstr[TCPEDIT_ERRSTR_LEN];
  66. #ifdef FORCE_ALIGN
  67. u_char *ipbuff = NULL; /* IP header and above buffer */
  68. #endif
  69. };
  70. typedef struct tcpedit_runtime_s tcpedit_runtime_t;
  71. /*
  72. * portmap data struct
  73. */
  74. struct tcpedit_portmap_s {
  75. long from;
  76. long to;
  77. struct tcpedit_portmap_s *next;
  78. };
  79. typedef struct tcpedit_portmap_s tcpedit_portmap_t;
  80. /*
  81. * all the arguments that the packet editing library supports
  82. */
  83. struct tcpedit_s {
  84. int validated; /* have we run tcpedit_validate()? */
  85. /* runtime variables, don't mess with these */
  86. tcpedit_runtime_t runtime;
  87. /* we use the mask to say which are valid values */
  88. char mac_mask;
  89. #define TCPEDIT_MAC_MASK_SMAC1 0x1
  90. #define TCPEDIT_MAC_MASK_SMAC2 0x2
  91. #define TCPEDIT_MAC_MASK_DMAC1 0x4
  92. #define TCPEDIT_MAC_MASK_DMAC2 0x8
  93. /* rewrite traffic bi-directionally */
  94. char bidir;
  95. #define TCPEDIT_BIDIR_OFF 0x0
  96. #define TCPEDIT_BIDIR_ON 0x1
  97. /* 802.1q VLAN tag stuff */
  98. char vlan;
  99. #define TCPEDIT_VLAN_OFF 0x0
  100. #define TCPEDIT_VLAN_DEL 0x1 /* strip 802.1q and rewrite as standard
  101. * 802.3 Ethernet */
  102. #define TCPEDIT_VLAN_ADD 0x2 /* add/replace 802.1q vlan tag */
  103. /* pad or truncate packets */
  104. char fixlen;
  105. #define TCPEDIT_FIXLEN_OFF 0x0
  106. #define TCPEDIT_FIXLEN_PAD 0x1
  107. #define TCPEDIT_FIXLEN_TRUNC 0x2
  108. #define TCPEDIT_FIXLEN_DEL 0x3
  109. /* rewrite ip? */
  110. char rewrite_ip;
  111. #define TCPEDIT_REWRITE_IP_OFF 0x0
  112. #define TCPEDIT_REWRITE_IP_ON 0x1
  113. /* fix IP/TCP/UDP checksums */
  114. char fixcsum;
  115. #define TCPEDIT_FIXCSUM_OFF 0x0
  116. #define TCPEDIT_FIXCSUM_ON 0x1
  117. /* remove ethernet FCS */
  118. char efcs;
  119. #define TCPEDIT_EFCS_OFF 0x0
  120. #define TCPEDIT_EFCS_ON 0x1
  121. char padding1; /* keep things 4 byte aligned */
  122. /* values to rewrite src/dst MAC addresses */
  123. tcpr_macaddr_t intf1_dmac;
  124. tcpr_macaddr_t intf1_smac;
  125. tcpr_macaddr_t intf2_dmac;
  126. tcpr_macaddr_t intf2_smac;
  127. /* other L2 editing options */
  128. u_int16_t l2proto;
  129. u_int16_t l2_mem_align; /* keep things 4 byte aligned */
  130. /* rewrite L2 data in full */
  131. l2_t l2;
  132. /* rewrite end-point IP addresses between cidrmap1 & cidrmap2 */
  133. tcpr_cidrmap_t *cidrmap1; /* tcpprep cache data */
  134. tcpr_cidrmap_t *cidrmap2;
  135. /* pseudo-randomize IP addresses using a seed */
  136. int seed;
  137. /* rewrite tcp/udp ports */
  138. tcpedit_portmap_t *portmap;
  139. int mtu; /* Deal with different MTU's */
  140. int maxpacket; /* L2 header + MTU */
  141. };
  142. typedef struct tcpedit_s tcpedit_t;
  143. int tcpedit_init(tcpedit_t *tcpedit, pcap_t *pcap1, pcap_t *pcap2);
  144. char *tcpedit_geterr(tcpedit_t *tcpedit);
  145. void tcpedit_seterr(tcpedit_t *tcpedit, const char *fmt, ...);
  146. int tcpedit_validate(tcpedit_t *tcpedit, int srcdlt, int dstdlt);
  147. int tcpedit_packet(tcpedit_t *tcpedit, struct pcap_pkthdr **pkthdr,
  148. u_char **pktdata, int direction);
  149. int tcpedit_close(tcpedit_t *tcpedit);
  150. COUNTER tcpedit_get_total_bytes(tcpedit_t *tcpedit);
  151. COUNTER tcpedit_get_pkts_edited(tcpedit_t *tcpedit);
  152. #endif