softflowd.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (c) 2002 Damien Miller. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  14. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  15. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  16. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  17. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  18. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  20. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  22. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #ifndef _SOFTFLOWD_H
  25. #define _SOFTFLOWD_H
  26. #include "common.h"
  27. #include "sys-tree.h"
  28. #include "treetype.h"
  29. /* User to setuid to and directory to chroot to when we drop privs */
  30. #ifndef PRIVDROP_USER
  31. # define PRIVDROP_USER "nobody"
  32. #endif
  33. #define PRIVDROP_CHROOT_DIR "/var/run/softflowd/chroot"
  34. /*
  35. * Capture length for libpcap: Must fit the link layer header, plus
  36. * a maximally sized ip/ipv6 header and most of a TCP header
  37. */
  38. #define LIBPCAP_SNAPLEN_V4 96
  39. #define LIBPCAP_SNAPLEN_V6 160
  40. /*
  41. * Timeouts
  42. */
  43. #define DEFAULT_TCP_TIMEOUT 3600
  44. #define DEFAULT_TCP_RST_TIMEOUT 120
  45. #define DEFAULT_TCP_FIN_TIMEOUT 300
  46. #define DEFAULT_UDP_TIMEOUT 300
  47. #define DEFAULT_ICMP_TIMEOUT 300
  48. #define DEFAULT_GENERAL_TIMEOUT 3600
  49. #define DEFAULT_MAXIMUM_LIFETIME (3600*24*7)
  50. #define DEFAULT_EXPIRY_INTERVAL 60
  51. /*
  52. * Default maximum number of flow to track simultaneously
  53. * 8192 corresponds to just under 1Mb of flow data
  54. */
  55. #define DEFAULT_MAX_FLOWS 8192
  56. /* Store a couple of statistics, maybe more in the future */
  57. struct STATISTIC {
  58. double min, mean, max;
  59. };
  60. /* Flow tracking levels */
  61. #define TRACK_FULL 1 /* src/dst/addr/port/proto 5-tuple */
  62. #define TRACK_IP_PROTO 2 /* src/dst/proto 3-tuple */
  63. #define TRACK_IP_ONLY 3 /* src/dst tuple */
  64. /*
  65. * This structure is the root of the flow tracking system.
  66. * It holds the root of the tree of active flows and the head of the
  67. * tree of expiry events. It also collects miscellaneous statistics
  68. */
  69. struct FLOWTRACK {
  70. /* The flows and their expiry events */
  71. FLOW_HEAD(FLOWS, FLOW) flows; /* Top of flow tree */
  72. EXPIRY_HEAD(EXPIRIES, EXPIRY) expiries; /* Top of expiries tree */
  73. unsigned int num_flows; /* # of active flows */
  74. u_int64_t next_flow_seq; /* Next flow ID */
  75. /* Stuff related to flow export */
  76. struct timeval system_boot_time; /* SysUptime */
  77. int track_level; /* See TRACK_* above */
  78. /* Flow timeouts */
  79. int tcp_timeout; /* Open TCP connections */
  80. int tcp_rst_timeout; /* TCP flows after RST */
  81. int tcp_fin_timeout; /* TCP flows after bidi FIN */
  82. int udp_timeout; /* UDP flows */
  83. int icmp_timeout; /* ICMP flows */
  84. int general_timeout; /* Everything else */
  85. int maximum_lifetime; /* Maximum life for flows */
  86. int expiry_interval; /* Interval between expiries */
  87. /* Statistics */
  88. u_int64_t total_packets; /* # of good packets */
  89. u_int64_t frag_packets; /* # of fragmented packets */
  90. u_int64_t non_ip_packets; /* # of not-IP packets */
  91. u_int64_t bad_packets; /* # of bad packets */
  92. u_int64_t flows_expired; /* # expired */
  93. u_int64_t flows_exported; /* # of flows sent */
  94. u_int64_t flows_dropped; /* # of flows dropped */
  95. u_int64_t flows_force_expired; /* # of flows forced out */
  96. u_int64_t packets_sent; /* # netflow packets sent */
  97. struct STATISTIC duration; /* Flow duration */
  98. struct STATISTIC octets; /* Bytes (bidir) */
  99. struct STATISTIC packets; /* Packets (bidir) */
  100. /* Per protocol statistics */
  101. u_int64_t flows_pp[256];
  102. u_int64_t octets_pp[256];
  103. u_int64_t packets_pp[256];
  104. struct STATISTIC duration_pp[256];
  105. /* Timeout statistics */
  106. u_int64_t expired_general;
  107. u_int64_t expired_tcp;
  108. u_int64_t expired_tcp_rst;
  109. u_int64_t expired_tcp_fin;
  110. u_int64_t expired_udp;
  111. u_int64_t expired_icmp;
  112. u_int64_t expired_maxlife;
  113. u_int64_t expired_overbytes;
  114. u_int64_t expired_maxflows;
  115. u_int64_t expired_flush;
  116. };
  117. /*
  118. * This structure is an entry in the tree of flows that we are
  119. * currently tracking.
  120. *
  121. * Because flows are matched _bi-directionally_, they must be stored in
  122. * a canonical format: the numerically lowest address and port number must
  123. * be stored in the first address and port array slot respectively.
  124. */
  125. struct FLOW {
  126. /* Housekeeping */
  127. struct EXPIRY *expiry; /* Pointer to expiry record */
  128. FLOW_ENTRY(FLOW) trp; /* Tree pointer */
  129. /* Flow identity (all are in network byte order) */
  130. int af; /* Address family of flow */
  131. u_int32_t ip6_flowlabel[2]; /* IPv6 Flowlabel */
  132. union {
  133. struct in_addr v4;
  134. struct in6_addr v6;
  135. } addr[2]; /* Endpoint addresses */
  136. u_int16_t port[2]; /* Endpoint ports */
  137. u_int8_t tcp_flags[2]; /* Cumulative OR of flags */
  138. u_int8_t protocol; /* Protocol */
  139. /* Per-flow statistics (all in _host_ byte order) */
  140. u_int64_t flow_seq; /* Flow ID */
  141. struct timeval flow_start; /* Time of creation */
  142. struct timeval flow_last; /* Time of last traffic */
  143. /* Per-endpoint statistics (all in _host_ byte order) */
  144. u_int32_t octets[2]; /* Octets so far */
  145. u_int32_t packets[2]; /* Packets so far */
  146. };
  147. /*
  148. * This is an entry in the tree of expiry events. The tree is used to
  149. * avoid traversion the whole tree of active flows looking for ones to
  150. * expire. "expires_at" is the time at which the flow should be discarded,
  151. * or zero if it is scheduled for immediate disposal.
  152. *
  153. * When a flow which hasn't been scheduled for immediate expiry registers
  154. * traffic, it is deleted from its current position in the tree and
  155. * re-inserted (subject to its updated timeout).
  156. *
  157. * Expiry scans operate by starting at the head of the tree and expiring
  158. * each entry with expires_at < now
  159. *
  160. */
  161. struct EXPIRY {
  162. EXPIRY_ENTRY(EXPIRY) trp; /* Tree pointer */
  163. struct FLOW *flow; /* pointer to flow */
  164. u_int32_t expires_at; /* time_t */
  165. enum {
  166. R_GENERAL, R_TCP, R_TCP_RST, R_TCP_FIN, R_UDP, R_ICMP,
  167. R_MAXLIFE, R_OVERBYTES, R_OVERFLOWS, R_FLUSH
  168. } reason;
  169. };
  170. /* Prototype for functions shared from softflowd.c */
  171. u_int32_t timeval_sub_ms(const struct timeval *t1, const struct timeval *t2);
  172. /* Prototypes for functions to send NetFlow packets, from netflow*.c */
  173. int send_netflow_v1(struct FLOW **flows, int num_flows, int nfsock,
  174. u_int64_t *flows_exported, struct timeval *system_boot_time,
  175. int verbose_flag);
  176. int send_netflow_v5(struct FLOW **flows, int num_flows, int nfsock,
  177. u_int64_t *flows_exported, struct timeval *system_boot_time,
  178. int verbose_flag);
  179. int send_netflow_v9(struct FLOW **flows, int num_flows, int nfsock,
  180. u_int64_t *flows_exported, struct timeval *system_boot_time,
  181. int verbose_flag);
  182. /* Force a resend of the flow template */
  183. void netflow9_resend_template(void);
  184. #endif /* _SOFTFLOWD_H */