sm_pf.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* $Id: sm_pf.c,v 1.6 2007/02/11 20:07:32 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. void
  68. gets_pf()
  69. {
  70. fatal("pf support not available");
  71. }
  72. int
  73. get_pf(char *symon_buf, int maxlen, struct stream *st)
  74. {
  75. fatal("pf support not available");
  76. return 0;
  77. }
  78. #else
  79. /* Globals for this module start with pf_ */
  80. int pf_dev = -1;
  81. struct pf_status pf_stat;
  82. void
  83. privinit_pf()
  84. {
  85. if ((pf_dev = open("/dev/pf", O_RDONLY)) == -1) {
  86. warning("could not open \"/dev/pf\", %.200s", strerror(errno));
  87. }
  88. }
  89. void
  90. init_pf(struct stream *st)
  91. {
  92. if (pf_dev == -1) {
  93. privinit_pf();
  94. }
  95. info("started module pf()");
  96. }
  97. void
  98. gets_pf()
  99. {
  100. if (pf_dev == -1) {
  101. warning("could not get pf stats (dev == -1)");
  102. pf_stat.running = 0;
  103. return;
  104. }
  105. if (ioctl(pf_dev, DIOCGETSTATUS, &pf_stat)) {
  106. warning("could not get pf stats (ioctl error)");
  107. pf_stat.running = 0;
  108. return;
  109. }
  110. }
  111. int
  112. get_pf(char *symon_buf, int maxlen, struct stream *st)
  113. {
  114. u_int64_t n;
  115. if (!pf_stat.running) {
  116. return 0;
  117. }
  118. n = pf_stat.states;
  119. return snpack(symon_buf, maxlen, st->arg, MT_PF,
  120. pf_stat.bcounters[0][0],
  121. pf_stat.bcounters[0][1],
  122. pf_stat.bcounters[1][0],
  123. pf_stat.bcounters[1][1],
  124. pf_stat.pcounters[0][0][PF_PASS],
  125. pf_stat.pcounters[0][0][PF_DROP],
  126. pf_stat.pcounters[0][1][PF_PASS],
  127. pf_stat.pcounters[0][1][PF_DROP],
  128. pf_stat.pcounters[1][0][PF_PASS],
  129. pf_stat.pcounters[1][0][PF_DROP],
  130. pf_stat.pcounters[1][1][PF_PASS],
  131. pf_stat.pcounters[1][1][PF_DROP],
  132. n,
  133. pf_stat.fcounters[0],
  134. pf_stat.fcounters[1],
  135. pf_stat.fcounters[2],
  136. pf_stat.counters[0],
  137. pf_stat.counters[1],
  138. pf_stat.counters[2],
  139. pf_stat.counters[3],
  140. pf_stat.counters[4],
  141. pf_stat.counters[5]
  142. );
  143. }
  144. #endif /* HAS_PFVAR_H */