parse_args.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2017 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 "config.h"
  20. #include "defines.h"
  21. #include "common.h"
  22. #include "tcpedit.h"
  23. #include "tcpedit_stub.h"
  24. #include "parse_args.h"
  25. #include "portmap.h"
  26. #include <string.h>
  27. #include <stdlib.h>
  28. /**
  29. * returns 0 for sucess w/o errors
  30. * returns 1 for sucess w/ warnings
  31. * returns -1 for error
  32. */
  33. int
  34. tcpedit_post_args(tcpedit_t *tcpedit) {
  35. int rcode = 0;
  36. int i;
  37. long ttl;
  38. uint32_t seed = 1;
  39. assert(tcpedit);
  40. /* --pnat */
  41. if (HAVE_OPT(PNAT)) {
  42. int ct = STACKCT_OPT(PNAT);
  43. char **list = (char**)STACKLST_OPT(PNAT);
  44. int first = 1;
  45. tcpedit->rewrite_ip ++;
  46. do {
  47. char *p = *list++;
  48. if (first) {
  49. if (! parse_cidr_map(&tcpedit->cidrmap1, p)) {
  50. tcpedit_seterr(tcpedit,
  51. "Unable to parse first --pnat=%s", p);
  52. return -1;
  53. }
  54. } else {
  55. if (! parse_cidr_map(&tcpedit->cidrmap2, p)) {
  56. tcpedit_seterr(tcpedit,
  57. "Unable to parse second --pnat=%s", p);
  58. return -1;
  59. }
  60. }
  61. first = 0;
  62. } while (--ct > 0);
  63. }
  64. /* --srcipmap */
  65. if (HAVE_OPT(SRCIPMAP)) {
  66. tcpedit->rewrite_ip ++;
  67. if (! parse_cidr_map(&tcpedit->srcipmap, OPT_ARG(SRCIPMAP))) {
  68. tcpedit_seterr(tcpedit,
  69. "Unable to parse --srcipmap=%s", OPT_ARG(SRCIPMAP));
  70. return -1;
  71. }
  72. }
  73. /* --dstipmap */
  74. if (HAVE_OPT(DSTIPMAP)) {
  75. tcpedit->rewrite_ip ++;
  76. if (! parse_cidr_map(&tcpedit->dstipmap, OPT_ARG(DSTIPMAP))) {
  77. tcpedit_seterr(tcpedit,
  78. "Unable to parse --dstipmap=%s", OPT_ARG(DSTIPMAP));
  79. return -1;
  80. }
  81. }
  82. /*
  83. * If we have one and only one -N, then use the same map data
  84. * for both interfaces/files
  85. */
  86. if ((tcpedit->cidrmap1 != NULL) && (tcpedit->cidrmap2 == NULL))
  87. tcpedit->cidrmap2 = tcpedit->cidrmap1;
  88. /* --fixcsum */
  89. if (HAVE_OPT(FIXCSUM))
  90. tcpedit->fixcsum = true;
  91. /* --efcs */
  92. if (HAVE_OPT(EFCS))
  93. tcpedit->efcs = true;
  94. /* --ttl */
  95. if (HAVE_OPT(TTL)) {
  96. if (strchr(OPT_ARG(TTL), '+')) {
  97. tcpedit->ttl_mode = TCPEDIT_TTL_MODE_ADD;
  98. } else if (strchr(OPT_ARG(TTL), '-')) {
  99. tcpedit->ttl_mode = TCPEDIT_TTL_MODE_SUB;
  100. } else {
  101. tcpedit->ttl_mode = TCPEDIT_TTL_MODE_SET;
  102. }
  103. ttl = strtol(OPT_ARG(TTL), (char **)NULL, 10);
  104. if (ttl < 0)
  105. ttl *= -1; /* convert to positive value */
  106. if (ttl > 255) {
  107. tcpedit_seterr(tcpedit, "Invalid --ttl value (must be 0-255): %ld", ttl);
  108. return -1;
  109. }
  110. tcpedit->ttl_value = (u_int8_t)ttl;
  111. }
  112. /* --tos */
  113. if (HAVE_OPT(TOS))
  114. tcpedit->tos = OPT_VALUE_TOS;
  115. /* --tclass */
  116. if (HAVE_OPT(TCLASS))
  117. tcpedit->tclass = OPT_VALUE_TCLASS;
  118. /* --flowlabel */
  119. if (HAVE_OPT(FLOWLABEL))
  120. tcpedit->flowlabel = OPT_VALUE_FLOWLABEL;
  121. /* --mtu */
  122. if (HAVE_OPT(MTU))
  123. tcpedit->mtu = OPT_VALUE_MTU;
  124. /* --mtu-trunc */
  125. if (HAVE_OPT(MTU_TRUNC))
  126. tcpedit->mtu_truncate = true;
  127. /* --skipbroadcast */
  128. if (HAVE_OPT(SKIPBROADCAST))
  129. tcpedit->skip_broadcast = true;
  130. /* --fixlen */
  131. if (HAVE_OPT(FIXLEN)) {
  132. if (strcmp(OPT_ARG(FIXLEN), "pad") == 0) {
  133. tcpedit->fixlen = TCPEDIT_FIXLEN_PAD;
  134. } else if (strcmp(OPT_ARG(FIXLEN), "trunc") == 0) {
  135. tcpedit->fixlen = TCPEDIT_FIXLEN_TRUNC;
  136. } else if (strcmp(OPT_ARG(FIXLEN), "del") == 0) {
  137. tcpedit->fixlen = TCPEDIT_FIXLEN_DEL;
  138. } else {
  139. tcpedit_seterr(tcpedit, "Invalid --fixlen=%s", OPT_ARG(FIXLEN));
  140. return -1;
  141. }
  142. }
  143. /* TCP/UDP port rewriting */
  144. if (HAVE_OPT(PORTMAP)) {
  145. int ct = STACKCT_OPT(PORTMAP);
  146. char **list = (char**)STACKLST_OPT(PORTMAP);
  147. int first = 1;
  148. tcpedit_portmap_t *portmap_head, *portmap;
  149. do {
  150. char *p = *list++;
  151. if (first) {
  152. if (! parse_portmap(&tcpedit->portmap, p)) {
  153. tcpedit_seterr(tcpedit, "Unable to parse --portmap=%s", p);
  154. return -1;
  155. }
  156. } else {
  157. if (! parse_portmap(&portmap, p)) {
  158. tcpedit_seterr(tcpedit, "Unable to parse --portmap=%s", p);
  159. return -1;
  160. }
  161. /* append to end of tcpedit->portmap linked list */
  162. portmap_head = tcpedit->portmap;
  163. while (portmap_head->next != NULL)
  164. portmap_head = portmap_head->next;
  165. portmap_head->next = portmap;
  166. }
  167. first = 0;
  168. } while (--ct > 0);
  169. }
  170. /*
  171. * IP address rewriting processing. Update seed by making
  172. * 5 calls to tcpr_random() as our mixer for randomizing. This should
  173. * work better since most people aren't going to write out values
  174. * close to 32bit integers.
  175. */
  176. if (HAVE_OPT(SEED)) {
  177. tcpedit->rewrite_ip = true;
  178. seed = OPT_VALUE_SEED;
  179. } else if (HAVE_OPT(FUZZ_SEED)) {
  180. /* --fuzz-seed */
  181. seed = OPT_VALUE_FUZZ_SEED;
  182. tcpedit->fuzz_factor = OPT_VALUE_FUZZ_FACTOR;
  183. }
  184. for (i = 0; i < 5; ++i) {
  185. seed = tcpr_random(&seed);
  186. }
  187. srandom(seed);
  188. if (HAVE_OPT(SEED)) {
  189. tcpedit->rewrite_ip = true;
  190. tcpedit->seed = seed;
  191. }
  192. if (HAVE_OPT(FUZZ_SEED)) {
  193. /* --fuzz-seed */
  194. tcpedit->fuzz_seed = seed;
  195. }
  196. if (HAVE_OPT(ENDPOINTS)) {
  197. tcpedit->rewrite_ip = true;
  198. if (! parse_endpoints(&tcpedit->cidrmap1, &tcpedit->cidrmap2,
  199. OPT_ARG(ENDPOINTS))) {
  200. tcpedit_seterr(tcpedit,
  201. "Unable to parse --endpoints=%s", OPT_ARG(ENDPOINTS));
  202. return -1;
  203. }
  204. }
  205. /*
  206. * figure out the max packet len
  207. if (tcpedit->l2.enabled) {
  208. // custom l2 header
  209. dbg(1, "Using custom L2 header to calculate max frame size\n");
  210. tcpedit->maxpacket = tcpedit->mtu + tcpedit->l2.len;
  211. }
  212. else if (tcpedit->l2.dlt == DLT_EN10MB || tcpedit->l2.dlt == DLT_VLAN) {
  213. // ethernet
  214. dbg(1, "Using Ethernet to calculate max frame size\n");
  215. tcpedit->maxpacket = tcpedit->mtu + TCPR_ETH_H;
  216. } else {
  217. // uh, wtf is this now? we'll just assume ethernet and hope things work
  218. tcpedit->maxpacket = tcpedit->mtu + TCPR_ETH_H;
  219. tcpedit_seterr(tcpedit,
  220. "Unsupported DLT type: %s. We'll just treat it as ethernet.\n"
  221. "You may need to increase the MTU (-t <size>) if you get errors\n",
  222. pcap_datalink_val_to_name(tcpedit->l2.dlt));
  223. rcode = 1;
  224. }
  225. */
  226. /* parse the tcpedit dlt args */
  227. rcode = tcpedit_dlt_post_args(tcpedit);
  228. if (rcode < 0) {
  229. errx(-1, "Unable to parse args: %s", tcpedit_geterr(tcpedit));
  230. } else if (rcode == 1) {
  231. warnx("%s", tcpedit_geterr(tcpedit));
  232. }
  233. return 0;
  234. }