xX.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* $Id: xX.c 1897 2007-08-25 04:57: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. /**
  42. * returns the include_exclude_mode on success placing the CIDR or LIST in mybuf
  43. * but on failure, returns xXError
  44. */
  45. int
  46. parse_xX_str(tcpr_xX_t *xX, char *str, tcpr_bpf_t *bpf)
  47. {
  48. int out = 0;
  49. dbgx(1, "Parsing string: %s", str);
  50. dbgx(1, "Switching on: %c", str[0]);
  51. switch (str[0]) {
  52. case 'B': /* both ip's */
  53. str = str + 2;
  54. out = xXBoth;
  55. if (!parse_cidr(&(xX->cidr), str, ","))
  56. return xXError;
  57. break;
  58. case 'D': /* dst ip */
  59. str = str + 2;
  60. out = xXDest;
  61. if (!parse_cidr(&(xX->cidr), str, ","))
  62. return xXError;
  63. break;
  64. case 'E': /* either ip */
  65. str = str + 2;
  66. out = xXEither;
  67. if (!parse_cidr(&(xX->cidr), str, ","))
  68. return xXError;
  69. break;
  70. case 'F': /* bpf filter */
  71. str = str + 2;
  72. out = xXBPF;
  73. bpf->filter = safe_strdup(str);
  74. /*
  75. * note: it's temping to compile the BPF here, but we don't
  76. * yet know what the link type is for the file, so we have
  77. * to compile the BPF once we open the pcap file
  78. */
  79. break;
  80. case 'P': /* packet id */
  81. str = str + 2;
  82. out = xXPacket;
  83. if (!parse_list(&(xX->list), str))
  84. return xXError;
  85. break;
  86. case 'S': /* source ip */
  87. str = str + 2;
  88. out = xXSource;
  89. if (!parse_cidr(&(xX->cidr), str, ","))
  90. return xXError;
  91. break;
  92. default:
  93. errx(1, "Invalid -%c option: %c", xX->mode, *str);
  94. break;
  95. }
  96. if (xX->mode == 'X') { /* run in exclude mode */
  97. out += xXExclude;
  98. if (bpf->filter != NULL)
  99. err(1, "Using a BPF filter with -X doesn't work.\n"
  100. "Try using -xF:\"not <filter>\" instead");
  101. }
  102. xX->mode = out;
  103. return xX->mode;
  104. }
  105. /**
  106. * compare the source/destination IP address according to the mode
  107. * and return 1 if we should send the packet or 0 if not
  108. */
  109. int
  110. process_xX_by_cidr(int mode, tcpr_cidr_t * cidr, ipv4_hdr_t * ip_hdr)
  111. {
  112. if (mode & xXExclude) {
  113. /* Exclude mode */
  114. switch (mode ^ xXExclude) {
  115. case xXSource:
  116. /* note: check_ip_cidr() returns TCPR_DIR_C2S for true, TCPR_DIR_S2C for false
  117. * and NOT true/false or 1/0, etc!
  118. */
  119. return check_ip_cidr(cidr, ip_hdr->ip_src.s_addr) ? DONT_SEND : SEND;
  120. break;
  121. case xXDest:
  122. return check_ip_cidr(cidr, ip_hdr->ip_dst.s_addr) ? DONT_SEND : SEND;
  123. case xXBoth:
  124. return (check_ip_cidr(cidr, ip_hdr->ip_dst.s_addr) &&
  125. check_ip_cidr(cidr, ip_hdr->ip_src.s_addr) ) ? DONT_SEND : SEND;
  126. break;
  127. case xXEither:
  128. return (check_ip_cidr(cidr, ip_hdr->ip_dst.s_addr) ||
  129. check_ip_cidr(cidr, ip_hdr->ip_src.s_addr) ) ? DONT_SEND : SEND;
  130. break;
  131. }
  132. }
  133. else {
  134. /* Include Mode */
  135. switch (mode) {
  136. case xXSource:
  137. return check_ip_cidr(cidr, ip_hdr->ip_src.s_addr) ? SEND : DONT_SEND;
  138. break;
  139. case xXDest:
  140. return check_ip_cidr(cidr, ip_hdr->ip_dst.s_addr) ? SEND : DONT_SEND;
  141. break;
  142. case xXBoth:
  143. return (check_ip_cidr(cidr, ip_hdr->ip_dst.s_addr) &&
  144. check_ip_cidr(cidr, ip_hdr->ip_src.s_addr) ) ? SEND : DONT_SEND;
  145. break;
  146. case xXEither:
  147. return (check_ip_cidr(cidr, ip_hdr->ip_dst.s_addr) ||
  148. check_ip_cidr(cidr, ip_hdr->ip_src.s_addr) ) ? SEND : DONT_SEND;
  149. break;
  150. }
  151. }
  152. /* total failure */
  153. if (mode &xXExclude) {
  154. warn("Unable to determine action in CIDR filter mode. Default: Don't Send.");
  155. return DONT_SEND;
  156. } else {
  157. warn("Unable to determine action in CIDR filter mode. Default: Send.");
  158. return SEND;
  159. }
  160. }
  161. /*
  162. Local Variables:
  163. mode:c
  164. indent-tabs-mode:nil
  165. c-basic-offset:4
  166. End:
  167. */