xX.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* $Id: xX.c 767 2004-10-06 12:48:49Z 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 "tcpreplay.h"
  40. #include "cidr.h"
  41. #include "list.h"
  42. #include "xX.h"
  43. #include "err.h"
  44. extern struct options options;
  45. /*
  46. * returns the include_exclude_mode on success placing the CIDR or LIST in mybuf
  47. * but on failure, returns 0
  48. */
  49. int
  50. parse_xX_str(char mode, char *str, void **mybuf)
  51. {
  52. int bpf = 0;
  53. int out = 0;
  54. dbg(1, "Parsing string: %s", str);
  55. dbg(1, "Switching on: %c", str[0]);
  56. switch (str[0]) {
  57. case 'B': /* both ip's */
  58. str = str + 2;
  59. out = xXBoth;
  60. if (!parse_cidr((CIDR **)mybuf, str, ","))
  61. return 0;
  62. break;
  63. case 'D': /* dst ip */
  64. str = str + 2;
  65. out = xXDest;
  66. if (!parse_cidr((CIDR **)mybuf, str, ","))
  67. return 0;
  68. break;
  69. case 'E': /* either ip */
  70. str = str + 2;
  71. out = xXEither;
  72. if (!parse_cidr((CIDR **)mybuf, str, ","))
  73. return 0;
  74. break;
  75. case 'F': /* bpf filter */
  76. bpf = 1;
  77. str = str + 2;
  78. out = xXBPF;
  79. options.bpf_filter = str;
  80. /* note: it's temping to compile the BPF here, but we don't
  81. * yet know what the link type is for the file, so we have
  82. * to compile the BPF once we open the pcap file
  83. */
  84. break;
  85. case 'P': /* packet id */
  86. str = str + 2;
  87. out = xXPacket;
  88. if (!parse_list((LIST **)mybuf, str))
  89. return 0;
  90. break;
  91. case 'S': /* source ip */
  92. str = str + 2;
  93. out = xXSource;
  94. if (!parse_cidr((CIDR **)mybuf, str, ","))
  95. return 0;
  96. break;
  97. default:
  98. errx(1, "Invalid -%c option: %c", mode, *str);
  99. break;
  100. }
  101. if (mode == 'X') { /* run in exclude mode */
  102. out += xXExclude;
  103. if (bpf)
  104. errx(1, "Using a BPF filter with -X doesn't work.\n"
  105. "Try using -xF:\"not <filter>\" instead");
  106. }
  107. return out;
  108. }
  109. /*
  110. * compare the source/destination IP address according to the mode
  111. * and return 1 if we should send the packet or 0 if not
  112. */
  113. int
  114. process_xX_by_cidr(int mode, CIDR * cidr, ip_hdr_t * ip_hdr)
  115. {
  116. if (mode & xXExclude) {
  117. /* Exclude mode */
  118. switch (mode ^ xXExclude) {
  119. case xXSource:
  120. return check_ip_CIDR(cidr, ip_hdr->ip_src.s_addr) ? 0 : 1;
  121. break;
  122. case xXDest:
  123. return check_ip_CIDR(cidr, ip_hdr->ip_dst.s_addr) ? 0 : 1;
  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)) ? 0 : 1;
  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)) ? 0 : 1;
  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) ? 1 : 0;
  139. break;
  140. case xXDest:
  141. return check_ip_CIDR(cidr, ip_hdr->ip_dst.s_addr) ? 1 : 0;
  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)) ? 1 : 0;
  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)) ? 1 : 0;
  150. break;
  151. }
  152. }
  153. /* total failure */
  154. warnx("Unable to determine action in CIDR filter mode");
  155. return 0;
  156. }