tcpedit_api.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2024 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. *
  6. * The Tcpreplay Suite of tools is free software: you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or with the authors permission any later version.
  10. *
  11. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "defines.h"
  20. #include "config.h"
  21. #include "dlt_utils.h"
  22. #include "portmap.h"
  23. #include "tcpedit.h"
  24. #include <sys/types.h>
  25. /**
  26. * Set output DLT plugin by using it's DLT_<type>. Note that the user plugin
  27. * is DLT_USER0.
  28. */
  29. int
  30. tcpedit_set_encoder_dltplugin_byid(tcpedit_t *tcpedit, int dlt)
  31. {
  32. tcpeditdlt_plugin_t *plugin;
  33. tcpeditdlt_t *ctx;
  34. assert(tcpedit);
  35. ctx = tcpedit->dlt_ctx;
  36. assert(ctx);
  37. if (ctx->encoder) {
  38. tcpedit_seterr(tcpedit, "You have already selected a DLT encoder: %s", ctx->encoder->name);
  39. return TCPEDIT_ERROR;
  40. }
  41. plugin = tcpedit_dlt_getplugin(ctx, dlt);
  42. if (plugin == NULL) {
  43. tcpedit_seterr(tcpedit, "No output DLT plugin decoder with DLT type: 0x%04x", dlt);
  44. return TCPEDIT_ERROR;
  45. }
  46. ctx->encoder = plugin;
  47. /* init the encoder plugin if it's not the decoder plugin too */
  48. if (ctx->encoder->dlt != ctx->decoder->dlt) {
  49. if (ctx->encoder->plugin_init(ctx) != TCPEDIT_OK) {
  50. /* plugin should generate the error */
  51. return TCPEDIT_ERROR;
  52. }
  53. }
  54. return TCPEDIT_OK;
  55. }
  56. /**
  57. * same as tcpedit_set_encoder_plugin_byid() except we take the DLT_<name>
  58. * as a string to select it
  59. */
  60. int
  61. tcpedit_set_encoder_dltplugin_byname(tcpedit_t *tcpedit, const char *name)
  62. {
  63. tcpeditdlt_plugin_t *plugin;
  64. tcpeditdlt_t *ctx;
  65. assert(tcpedit);
  66. ctx = tcpedit->dlt_ctx;
  67. assert(ctx);
  68. if (ctx->encoder) {
  69. tcpedit_seterr(tcpedit, "You have already selected a DLT encoder: %s", ctx->encoder->name);
  70. return TCPEDIT_ERROR;
  71. }
  72. plugin = tcpedit_dlt_getplugin_byname(ctx, name);
  73. if (plugin == NULL) {
  74. tcpedit_seterr(tcpedit, "No output DLT plugin available for: %s", name);
  75. return TCPEDIT_ERROR;
  76. }
  77. ctx->encoder = plugin;
  78. /* init the encoder plugin if it's not the decoder plugin too */
  79. if (ctx->encoder->dlt != ctx->decoder->dlt) {
  80. if (ctx->encoder->plugin_init(ctx) != TCPEDIT_OK) {
  81. /* plugin should generate the error */
  82. return TCPEDIT_ERROR;
  83. }
  84. }
  85. return TCPEDIT_OK;
  86. }
  87. /**
  88. * Set whether we should edit broadcast & multicast IP addresses
  89. */
  90. int
  91. tcpedit_set_skip_broadcast(tcpedit_t *tcpedit, bool value)
  92. {
  93. assert(tcpedit);
  94. tcpedit->skip_broadcast = value;
  95. return TCPEDIT_OK;
  96. }
  97. /**
  98. * \brief force fixing L3 & L4 data by padding or truncating packets
  99. */
  100. int
  101. tcpedit_set_fixlen(tcpedit_t *tcpedit, tcpedit_fixlen value)
  102. {
  103. assert(tcpedit);
  104. tcpedit->fixlen = value;
  105. return TCPEDIT_OK;
  106. }
  107. /**
  108. * \brief should we always recalculate L3 & L4 checksums?
  109. */
  110. int
  111. tcpedit_set_fixcsum(tcpedit_t *tcpedit, bool value)
  112. {
  113. assert(tcpedit);
  114. tcpedit->fixcsum = value;
  115. return TCPEDIT_OK;
  116. }
  117. /**
  118. * \brief should we fix(?) header length?
  119. */
  120. int
  121. tcpedit_set_fixhdrlen(tcpedit_t *tcpedit, bool value)
  122. {
  123. assert(tcpedit);
  124. tcpedit->fixhdrlen = value;
  125. return TCPEDIT_OK;
  126. }
  127. /**
  128. * \brief should we remove the EFCS from the frame?
  129. */
  130. int
  131. tcpedit_set_efcs(tcpedit_t *tcpedit, bool value)
  132. {
  133. assert(tcpedit);
  134. tcpedit->efcs = value;
  135. return TCPEDIT_OK;
  136. }
  137. /**
  138. * \brief set the IPv4 TTL mode
  139. */
  140. int
  141. tcpedit_set_ttl_mode(tcpedit_t *tcpedit, tcpedit_ttl_mode value)
  142. {
  143. assert(tcpedit);
  144. tcpedit->ttl_mode = value;
  145. return TCPEDIT_OK;
  146. }
  147. /**
  148. * \brief set the IPv4 ttl value
  149. */
  150. int
  151. tcpedit_set_ttl_value(tcpedit_t *tcpedit, uint8_t value)
  152. {
  153. assert(tcpedit);
  154. tcpedit->ttl_value = value;
  155. return TCPEDIT_OK;
  156. }
  157. /**
  158. * \brief set the IPv4 TOS/DiffServ/ECN byte value
  159. */
  160. int
  161. tcpedit_set_tos(tcpedit_t *tcpedit, uint8_t value)
  162. {
  163. assert(tcpedit);
  164. tcpedit->tos = value;
  165. return TCPEDIT_OK;
  166. }
  167. /**
  168. * \brief set the IPv6 Traffic Class byte value
  169. */
  170. int
  171. tcpedit_set_tclass(tcpedit_t *tcpedit, uint8_t value)
  172. {
  173. assert(tcpedit);
  174. tcpedit->tclass = value;
  175. return TCPEDIT_OK;
  176. }
  177. /**
  178. * \brief set the IPv6 Flow Label 20bit value
  179. */
  180. int
  181. tcpedit_set_flowlabel(tcpedit_t *tcpedit, uint32_t value)
  182. {
  183. assert(tcpedit);
  184. tcpedit->flowlabel = (int)value;
  185. return TCPEDIT_OK;
  186. }
  187. /**
  188. * Set the IPv4 IP address randomization seed
  189. */
  190. int
  191. tcpedit_set_seed(tcpedit_t *tcpedit)
  192. {
  193. assert(tcpedit);
  194. tcpedit->rewrite_ip = true;
  195. tcpedit->seed = 1;
  196. tcpedit->seed = tcpr_random(&tcpedit->seed);
  197. return TCPEDIT_OK;
  198. }
  199. /**
  200. * Set the MTU of the frames
  201. */
  202. int
  203. tcpedit_set_mtu(tcpedit_t *tcpedit, int value)
  204. {
  205. assert(tcpedit);
  206. tcpedit->mtu = value;
  207. return TCPEDIT_OK;
  208. }
  209. /**
  210. * Enable truncating packets to the MTU length
  211. */
  212. int
  213. tcpedit_set_mtu_truncate(tcpedit_t *tcpedit, bool value)
  214. {
  215. assert(tcpedit);
  216. tcpedit->mtu_truncate = value;
  217. return TCPEDIT_OK;
  218. }
  219. /**
  220. * Set the maxpacket- currently not supported
  221. */
  222. int
  223. tcpedit_set_maxpacket(tcpedit_t *tcpedit, int value)
  224. {
  225. assert(tcpedit);
  226. tcpedit->maxpacket = value;
  227. return TCPEDIT_OK;
  228. }
  229. /**
  230. * \brief Set the server to client (primary) CIDR map (Pseudo NAT)
  231. *
  232. * Set the server to client (primary) CIDR map using the given string
  233. * which is in the format of:
  234. * <match cidr>:<target cidr>,...
  235. * 192.168.0.0/16:10.77.0.0/16,172.16.0.0/12:10.1.0.0/24
  236. */
  237. int
  238. tcpedit_set_cidrmap_s2c(tcpedit_t *tcpedit, char *value)
  239. {
  240. assert(tcpedit);
  241. tcpedit->rewrite_ip = true;
  242. if (!parse_cidr_map(&tcpedit->cidrmap1, value)) {
  243. tcpedit_seterr(tcpedit, "Unable to parse: %s", value);
  244. return TCPEDIT_ERROR;
  245. }
  246. return TCPEDIT_OK;
  247. }
  248. /**
  249. * \brief Set the client to server (secondary) CIDR map (Pseudo NAT)
  250. *
  251. * Set the client to server (secondary) CIDR map using the given string
  252. * which is in the format of:
  253. * <match cidr>:<target cidr>,...
  254. * 192.168.0.0/16:10.77.0.0/16,172.16.0.0/12:10.1.0.0/24
  255. */
  256. int
  257. tcpedit_set_cidrmap_c2s(tcpedit_t *tcpedit, char *value)
  258. {
  259. assert(tcpedit);
  260. tcpedit->rewrite_ip = true;
  261. if (!parse_cidr_map(&tcpedit->cidrmap2, value)) {
  262. tcpedit_seterr(tcpedit, "Unable to parse: %s", value);
  263. return TCPEDIT_ERROR;
  264. }
  265. return TCPEDIT_OK;
  266. }
  267. /**
  268. * Rewrite the Source IP of any packet
  269. */
  270. int
  271. tcpedit_set_srcip_map(tcpedit_t *tcpedit, char *value)
  272. {
  273. assert(tcpedit);
  274. tcpedit->rewrite_ip = true;
  275. if (!parse_cidr_map(&tcpedit->srcipmap, value)) {
  276. tcpedit_seterr(tcpedit, "Unable to parse source ip map: %s", value);
  277. return TCPEDIT_ERROR;
  278. }
  279. return TCPEDIT_OK;
  280. }
  281. /**
  282. * Rewrite the Destination IP of any packet
  283. */
  284. int
  285. tcpedit_set_dstip_map(tcpedit_t *tcpedit, char *value)
  286. {
  287. assert(tcpedit);
  288. tcpedit->rewrite_ip = true;
  289. if (!parse_cidr_map(&tcpedit->dstipmap, value)) {
  290. tcpedit_seterr(tcpedit, "Unable to parse destination ip map: %s", value);
  291. return TCPEDIT_ERROR;
  292. }
  293. return TCPEDIT_OK;
  294. }
  295. /**
  296. * Rewrite TCP/UDP ports using the following format:
  297. * <src>:<dst>,...
  298. */
  299. int
  300. tcpedit_set_port_map(tcpedit_t *tcpedit, char *value)
  301. {
  302. assert(tcpedit);
  303. if (!parse_portmap(&tcpedit->portmap, value)) {
  304. tcpedit_seterr(tcpedit, "Unable to parse portmap: %s", value);
  305. return TCPEDIT_ERROR;
  306. }
  307. return TCPEDIT_OK;
  308. }