sm_pf.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2002 Daniel Hartmeier
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following
  13. * disclaimer in the documentation and/or other materials provided
  14. * with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  26. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. */
  30. /*
  31. * Get current pf statistics and return them in symon_buf as
  32. *
  33. * bytes_v4_in : bytes_v4_out : bytes_v6_in : bytes_v6_out :
  34. * packets_v4_in_pass : * packets_v4_in_drop : packets_v4_out_pass :
  35. * packets_v4_out_drop : * packets_v6_in_pass : packets_v6_in_drop :
  36. * packets_v6_out_pass : * packets_v6_out_drop : states_entries :
  37. * states_searches : states_inserts : * states_removals : counters_match :
  38. * counters_badoffset : counters_fragment : * counters_short :
  39. * counters_normalize : counters_memory
  40. *
  41. */
  42. #include "conf.h"
  43. #include <sys/types.h>
  44. #include <sys/ioctl.h>
  45. #include <sys/socket.h>
  46. #include <netinet/in.h>
  47. #include <net/if.h>
  48. #ifdef HAS_PFVAR_H
  49. #include <net/pfvar.h>
  50. #endif
  51. #include <errno.h>
  52. #include <fcntl.h>
  53. #include <string.h>
  54. #include "error.h"
  55. #include "symon.h"
  56. #ifndef HAS_PFVAR_H
  57. void
  58. privinit_pf()
  59. {
  60. }
  61. void
  62. init_pf(struct stream *st)
  63. {
  64. fatal("pf support not available");
  65. }
  66. void
  67. gets_pf()
  68. {
  69. fatal("pf support not available");
  70. }
  71. int
  72. get_pf(char *symon_buf, int maxlen, struct stream *st)
  73. {
  74. fatal("pf support not available");
  75. return 0;
  76. }
  77. #else
  78. /* Globals for this module start with pf_ */
  79. int pf_dev = -1;
  80. struct pf_status pf_stat;
  81. void
  82. privinit_pf()
  83. {
  84. if ((pf_dev = open("/dev/pf", O_RDONLY)) == -1) {
  85. warning("could not open \"/dev/pf\", %.200s", strerror(errno));
  86. }
  87. }
  88. void
  89. init_pf(struct stream *st)
  90. {
  91. if (pf_dev == -1) {
  92. privinit_pf();
  93. }
  94. info("started module pf()");
  95. }
  96. void
  97. gets_pf()
  98. {
  99. if (pf_dev == -1) {
  100. warning("could not get pf stats (dev == -1)");
  101. pf_stat.running = 0;
  102. return;
  103. }
  104. if (ioctl(pf_dev, DIOCGETSTATUS, &pf_stat)) {
  105. warning("could not get pf stats (ioctl error)");
  106. pf_stat.running = 0;
  107. return;
  108. }
  109. }
  110. int
  111. get_pf(char *symon_buf, int maxlen, struct stream *st)
  112. {
  113. u_int64_t n;
  114. if (!pf_stat.running) {
  115. return 0;
  116. }
  117. n = pf_stat.states;
  118. return snpack(symon_buf, maxlen, st->arg, MT_PF,
  119. pf_stat.bcounters[0][0],
  120. pf_stat.bcounters[0][1],
  121. pf_stat.bcounters[1][0],
  122. pf_stat.bcounters[1][1],
  123. pf_stat.pcounters[0][0][PF_PASS],
  124. pf_stat.pcounters[0][0][PF_DROP],
  125. pf_stat.pcounters[0][1][PF_PASS],
  126. pf_stat.pcounters[0][1][PF_DROP],
  127. pf_stat.pcounters[1][0][PF_PASS],
  128. pf_stat.pcounters[1][0][PF_DROP],
  129. pf_stat.pcounters[1][1][PF_PASS],
  130. pf_stat.pcounters[1][1][PF_DROP],
  131. n,
  132. pf_stat.fcounters[0],
  133. pf_stat.fcounters[1],
  134. pf_stat.fcounters[2],
  135. pf_stat.counters[0],
  136. pf_stat.counters[1],
  137. pf_stat.counters[2],
  138. pf_stat.counters[3],
  139. pf_stat.counters[4],
  140. pf_stat.counters[5]
  141. );
  142. }
  143. #endif /* HAS_PFVAR_H */