sm_pf.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* $Id: sm_pf.c,v 1.7 2003/12/20 16:30:44 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 <sys/types.h>
  44. #include <sys/ioctl.h>
  45. #include <sys/socket.h>
  46. #include <netinet/in.h>
  47. #include <net/if.h>
  48. #include <net/pfvar.h>
  49. #include <errno.h>
  50. #include <fcntl.h>
  51. #include <string.h>
  52. #include "error.h"
  53. #include "symon.h"
  54. /* Globals for this module start with pf_ */
  55. int pf_dev = -1;
  56. /* Priviledged init, called before priviledges are dropped */
  57. void
  58. privinit_pf()
  59. {
  60. if ((pf_dev = open("/dev/pf", O_RDONLY)) == -1)
  61. warning("could not open \"/dev/pf\", %.200s", strerror(errno));
  62. }
  63. /* Prepare if module for first use */
  64. void
  65. init_pf(char *s)
  66. {
  67. if (pf_dev == -1)
  68. privinit_pf();
  69. info("started module pf(%.200s)", s);
  70. }
  71. /* Get pf statistics */
  72. int
  73. get_pf(char *symon_buf, int maxlen, char *arg)
  74. {
  75. struct pf_status s;
  76. u_int64_t n;
  77. if (pf_dev == -1) {
  78. warning("pf(%.200s) failed (dev == -1)", arg);
  79. return 0;
  80. }
  81. if (ioctl(pf_dev, DIOCGETSTATUS, &s)) {
  82. warning("pf(%.200s) failed (ioctl error)", arg);
  83. return 0;
  84. }
  85. if (!s.running)
  86. return 0;
  87. n = s.states;
  88. return snpack(symon_buf, maxlen, arg, MT_PF,
  89. s.bcounters[0][0],
  90. s.bcounters[0][1],
  91. s.bcounters[1][0],
  92. s.bcounters[1][1],
  93. s.pcounters[0][0][PF_PASS],
  94. s.pcounters[0][0][PF_DROP],
  95. s.pcounters[0][1][PF_PASS],
  96. s.pcounters[0][1][PF_DROP],
  97. s.pcounters[1][0][PF_PASS],
  98. s.pcounters[1][0][PF_DROP],
  99. s.pcounters[1][1][PF_PASS],
  100. s.pcounters[1][1][PF_DROP],
  101. n,
  102. s.fcounters[0],
  103. s.fcounters[1],
  104. s.fcounters[2],
  105. s.counters[0],
  106. s.counters[1],
  107. s.counters[2],
  108. s.counters[3],
  109. s.counters[4],
  110. s.counters[5]
  111. );
  112. }