sm_pf.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* $Id: sm_pf.c,v 1.9 2005/01/15 17:31: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(char *s)
  64. {
  65. fatal("pf support not available");
  66. }
  67. int
  68. get_pf(char *symon_buf, int maxlen, char *s)
  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. /* Priviledged init, called before priviledges are dropped */
  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. /* Prepare if module for first use */
  84. void
  85. init_pf(char *s)
  86. {
  87. if (pf_dev == -1)
  88. privinit_pf();
  89. info("started module pf(%.200s)", s);
  90. }
  91. /* Get pf statistics */
  92. int
  93. get_pf(char *symon_buf, int maxlen, char *arg)
  94. {
  95. struct pf_status s;
  96. u_int64_t n;
  97. if (pf_dev == -1) {
  98. warning("pf(%.200s) failed (dev == -1)", arg);
  99. return 0;
  100. }
  101. if (ioctl(pf_dev, DIOCGETSTATUS, &s)) {
  102. warning("pf(%.200s) failed (ioctl error)", arg);
  103. return 0;
  104. }
  105. if (!s.running)
  106. return 0;
  107. n = s.states;
  108. return snpack(symon_buf, maxlen, arg, MT_PF,
  109. s.bcounters[0][0],
  110. s.bcounters[0][1],
  111. s.bcounters[1][0],
  112. s.bcounters[1][1],
  113. s.pcounters[0][0][PF_PASS],
  114. s.pcounters[0][0][PF_DROP],
  115. s.pcounters[0][1][PF_PASS],
  116. s.pcounters[0][1][PF_DROP],
  117. s.pcounters[1][0][PF_PASS],
  118. s.pcounters[1][0][PF_DROP],
  119. s.pcounters[1][1][PF_PASS],
  120. s.pcounters[1][1][PF_DROP],
  121. n,
  122. s.fcounters[0],
  123. s.fcounters[1],
  124. s.fcounters[2],
  125. s.counters[0],
  126. s.counters[1],
  127. s.counters[2],
  128. s.counters[3],
  129. s.counters[4],
  130. s.counters[5]
  131. );
  132. }
  133. #endif