xX.c 7.7 KB

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