sm_pf.c 3.9 KB

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