xX.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2018 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. /*
  20. * xX stands for "include or exclude" which is used with the
  21. * -x and -X flags
  22. *
  23. * Functions for use to process args for or check data against in
  24. * tcpreplay/do_packets and tcpprep.
  25. */
  26. #include <stdlib.h>
  27. #include "config.h"
  28. #include "defines.h"
  29. #include "common.h"
  30. #include <stdlib.h>
  31. /**
  32. * returns the include_exclude_mode on success placing the CIDR or LIST in mybuf
  33. * but on failure, returns xXError
  34. */
  35. int
  36. parse_xX_str(tcpr_xX_t *xX, char *str, tcpr_bpf_t *bpf)
  37. {
  38. int out = 0;
  39. dbgx(1, "Parsing string: %s", str);
  40. dbgx(1, "Switching on: %c", str[0]);
  41. switch (str[0]) {
  42. case 'B': /* both ip's */
  43. str = str + 2;
  44. out = xXBoth;
  45. if (!parse_cidr(&(xX->cidr), str, ","))
  46. return xXError;
  47. break;
  48. case 'D': /* dst ip */
  49. str = str + 2;
  50. out = xXDest;
  51. if (!parse_cidr(&(xX->cidr), str, ","))
  52. return xXError;
  53. break;
  54. case 'E': /* either ip */
  55. str = str + 2;
  56. out = xXEither;
  57. if (!parse_cidr(&(xX->cidr), str, ","))
  58. return xXError;
  59. break;
  60. case 'F': /* bpf filter */
  61. str = str + 2;
  62. out = xXBPF;
  63. bpf->filter = safe_strdup(str);
  64. /*
  65. * note: it's temping to compile the BPF here, but we don't
  66. * yet know what the link type is for the file, so we have
  67. * to compile the BPF once we open the pcap file
  68. */
  69. break;
  70. case 'P': /* packet id */
  71. str = str + 2;
  72. out = xXPacket;
  73. if (!parse_list(&(xX->list), str))
  74. return xXError;
  75. break;
  76. case 'S': /* source ip */
  77. str = str + 2;
  78. out = xXSource;
  79. if (!parse_cidr(&(xX->cidr), str, ","))
  80. return xXError;
  81. break;
  82. default:
  83. errx(-1, "Invalid -%c option: %c", xX->mode, *str);
  84. break;
  85. }
  86. if (xX->mode == 'X') { /* run in exclude mode */
  87. out += xXExclude;
  88. if (bpf->filter != NULL)
  89. err(-1, "Using a BPF filter with -X doesn't work.\n"
  90. "Try using -xF:\"not <filter>\" instead");
  91. }
  92. xX->mode = out;
  93. return xX->mode;
  94. }
  95. /**
  96. * compare the source/destination IP address according to the mode
  97. * and return 1 if we should send the packet or 0 if not
  98. */
  99. int
  100. process_xX_by_cidr_ipv4(int mode, tcpr_cidr_t * cidr, ipv4_hdr_t * ip_hdr)
  101. {
  102. if (mode & xXExclude) {
  103. /* Exclude mode */
  104. switch (mode ^ xXExclude) {
  105. case xXSource:
  106. /* note: check_ip_cidr() returns TCPR_DIR_C2S for true, TCPR_DIR_S2C for false
  107. * and NOT true/false or 1/0, etc!
  108. */
  109. return check_ip_cidr(cidr, ip_hdr->ip_src.s_addr) ? DONT_SEND : SEND;
  110. break;
  111. case xXDest:
  112. return check_ip_cidr(cidr, ip_hdr->ip_dst.s_addr) ? DONT_SEND : SEND;
  113. case xXBoth:
  114. return (check_ip_cidr(cidr, ip_hdr->ip_dst.s_addr) &&
  115. check_ip_cidr(cidr, ip_hdr->ip_src.s_addr) ) ? DONT_SEND : SEND;
  116. break;
  117. case xXEither:
  118. return (check_ip_cidr(cidr, ip_hdr->ip_dst.s_addr) ||
  119. check_ip_cidr(cidr, ip_hdr->ip_src.s_addr) ) ? DONT_SEND : SEND;
  120. break;
  121. }
  122. }
  123. else {
  124. /* Include Mode */
  125. switch (mode) {
  126. case xXSource:
  127. return check_ip_cidr(cidr, ip_hdr->ip_src.s_addr) ? SEND : DONT_SEND;
  128. break;
  129. case xXDest:
  130. return check_ip_cidr(cidr, ip_hdr->ip_dst.s_addr) ? SEND : DONT_SEND;
  131. break;
  132. case xXBoth:
  133. return (check_ip_cidr(cidr, ip_hdr->ip_dst.s_addr) &&
  134. check_ip_cidr(cidr, ip_hdr->ip_src.s_addr) ) ? SEND : DONT_SEND;
  135. break;
  136. case xXEither:
  137. return (check_ip_cidr(cidr, ip_hdr->ip_dst.s_addr) ||
  138. check_ip_cidr(cidr, ip_hdr->ip_src.s_addr) ) ? SEND : DONT_SEND;
  139. break;
  140. }
  141. }
  142. /* total failure */
  143. if (mode &xXExclude) {
  144. warn("Unable to determine action in CIDR filter mode. Default: Don't Send.");
  145. return DONT_SEND;
  146. } else {
  147. warn("Unable to determine action in CIDR filter mode. Default: Send.");
  148. return SEND;
  149. }
  150. }
  151. int
  152. process_xX_by_cidr_ipv6(int mode, tcpr_cidr_t * cidr, ipv6_hdr_t * ip6_hdr)
  153. {
  154. if (mode & xXExclude) {
  155. /* Exclude mode */
  156. switch (mode ^ xXExclude) {
  157. case xXSource:
  158. /* note: check_ip_cidr() returns TCPR_DIR_C2S for true, TCPR_DIR_S2C for false
  159. * and NOT true/false or 1/0, etc!
  160. */
  161. return check_ip6_cidr(cidr, &ip6_hdr->ip_src) ? DONT_SEND : SEND;
  162. break;
  163. case xXDest:
  164. return check_ip6_cidr(cidr, &ip6_hdr->ip_dst) ? DONT_SEND : SEND;
  165. case xXBoth:
  166. return (check_ip6_cidr(cidr, &ip6_hdr->ip_dst) &&
  167. check_ip6_cidr(cidr, &ip6_hdr->ip_src) ) ? DONT_SEND : SEND;
  168. break;
  169. case xXEither:
  170. return (check_ip6_cidr(cidr, &ip6_hdr->ip_dst) ||
  171. check_ip6_cidr(cidr, &ip6_hdr->ip_src) ) ? DONT_SEND : SEND;
  172. break;
  173. }
  174. }
  175. else {
  176. /* Include Mode */
  177. switch (mode) {
  178. case xXSource:
  179. return check_ip6_cidr(cidr, &ip6_hdr->ip_src) ? SEND : DONT_SEND;
  180. break;
  181. case xXDest:
  182. return check_ip6_cidr(cidr, &ip6_hdr->ip_dst) ? SEND : DONT_SEND;
  183. break;
  184. case xXBoth:
  185. return (check_ip6_cidr(cidr, &ip6_hdr->ip_dst) &&
  186. check_ip6_cidr(cidr, &ip6_hdr->ip_src) ) ? SEND : DONT_SEND;
  187. break;
  188. case xXEither:
  189. return (check_ip6_cidr(cidr, &ip6_hdr->ip_dst) ||
  190. check_ip6_cidr(cidr, &ip6_hdr->ip_src) ) ? SEND : DONT_SEND;
  191. break;
  192. }
  193. }
  194. /* total failure */
  195. if (mode &xXExclude) {
  196. warn("Unable to determine action in CIDR filter mode. Default: Don't Send.");
  197. return DONT_SEND;
  198. } else {
  199. warn("Unable to determine action in CIDR filter mode. Default: Send.");
  200. return SEND;
  201. }
  202. }