sm_pf.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* $Id: sm_pf.c,v 1.5 2003/01/26 20:41:00 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. static int pf_dev = -1;
  56. /* Prepare if module for first use */
  57. void
  58. init_pf(char *s)
  59. {
  60. if (pf_dev == -1)
  61. if ((pf_dev = open("/dev/pf", O_RDWR)) == -1)
  62. fatal("%s:%d: open(\"/dev/pf\") failed, %.200s",
  63. __FILE__, __LINE__, strerror(errno));
  64. info("started module pf(%s)", s);
  65. }
  66. /* Get pf statistics */
  67. int
  68. get_pf(char *symon_buf, int maxlen, char *arg)
  69. {
  70. struct pf_status s;
  71. u_int64_t n;
  72. if (pf_dev == -1) {
  73. warning("pf(%s) failed (dev == -1)", arg);
  74. return 0;
  75. }
  76. if (ioctl(pf_dev, DIOCGETSTATUS, &s)) {
  77. warning("pf(%s) failed (ioctl error)", arg);
  78. return 0;
  79. }
  80. if (!s.running)
  81. return 0;
  82. n = s.states;
  83. return snpack(symon_buf, maxlen, arg, MT_PF,
  84. s.bcounters[0][0],
  85. s.bcounters[0][1],
  86. s.bcounters[1][0],
  87. s.bcounters[1][1],
  88. s.pcounters[0][0][PF_PASS],
  89. s.pcounters[0][0][PF_DROP],
  90. s.pcounters[0][1][PF_PASS],
  91. s.pcounters[0][1][PF_DROP],
  92. s.pcounters[1][0][PF_PASS],
  93. s.pcounters[1][0][PF_DROP],
  94. s.pcounters[1][1][PF_PASS],
  95. s.pcounters[1][1][PF_DROP],
  96. n,
  97. s.fcounters[0],
  98. s.fcounters[1],
  99. s.fcounters[2],
  100. s.counters[0],
  101. s.counters[1],
  102. s.counters[2],
  103. s.counters[3],
  104. s.counters[4],
  105. s.counters[5]
  106. );
  107. }