softflowd.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 "freelist.h"
  29. #include "treetype.h"
  30. /* User to setuid to and directory to chroot to when we drop privs */
  31. #ifndef PRIVDROP_USER
  32. # define PRIVDROP_USER "nobody"
  33. #endif
  34. #ifndef PRIVDROP_CHROOT_DIR
  35. # define PRIVDROP_CHROOT_DIR "/var/empty"
  36. #endif
  37. /*
  38. * Capture length for libpcap: Must fit the link layer header, plus
  39. * a maximally sized ip/ipv6 header and most of a TCP header
  40. */
  41. #define LIBPCAP_SNAPLEN_V4 96
  42. #define LIBPCAP_SNAPLEN_V6 160
  43. /*
  44. * Timeouts
  45. */
  46. #define DEFAULT_TCP_TIMEOUT 3600
  47. #define DEFAULT_TCP_RST_TIMEOUT 120
  48. #define DEFAULT_TCP_FIN_TIMEOUT 300
  49. #define DEFAULT_UDP_TIMEOUT 300
  50. #define DEFAULT_ICMP_TIMEOUT 300
  51. #define DEFAULT_GENERAL_TIMEOUT 3600
  52. #define DEFAULT_MAXIMUM_LIFETIME (3600*24*7)
  53. #define DEFAULT_EXPIRY_INTERVAL 60
  54. /*
  55. * Default maximum number of flow to track simultaneously
  56. * 8192 corresponds to just under 1Mb of flow data
  57. */
  58. #define DEFAULT_MAX_FLOWS 8192
  59. /* Store a couple of statistics, maybe more in the future */
  60. struct STATISTIC {
  61. double min, mean, max;
  62. };
  63. /* Flow tracking levels */
  64. #define TRACK_FULL 1 /* src/dst/addr/port/proto 5-tuple */
  65. #define TRACK_IP_PROTO 2 /* src/dst/proto 3-tuple */
  66. #define TRACK_IP_ONLY 3 /* src/dst tuple */
  67. /*
  68. * This structure is the root of the flow tracking system.
  69. * It holds the root of the tree of active flows and the head of the
  70. * tree of expiry events. It also collects miscellaneous statistics
  71. */
  72. struct FLOWTRACK {
  73. /* The flows and their expiry events */
  74. FLOW_HEAD(FLOWS, FLOW) flows; /* Top of flow tree */
  75. EXPIRY_HEAD(EXPIRIES, EXPIRY) expiries; /* Top of expiries tree */
  76. struct freelist flow_freelist; /* Freelist for flows */
  77. struct freelist expiry_freelist; /* Freelist for expiry events */
  78. unsigned int num_flows; /* # of active flows */
  79. unsigned int max_flows; /* Max # of active flows */
  80. u_int64_t next_flow_seq; /* Next flow ID */
  81. /* Stuff related to flow export */
  82. struct timeval system_boot_time; /* SysUptime */
  83. int track_level; /* See TRACK_* above */
  84. /* Flow timeouts */
  85. int tcp_timeout; /* Open TCP connections */
  86. int tcp_rst_timeout; /* TCP flows after RST */
  87. int tcp_fin_timeout; /* TCP flows after bidi FIN */
  88. int udp_timeout; /* UDP flows */
  89. int icmp_timeout; /* ICMP flows */
  90. int general_timeout; /* Everything else */
  91. int maximum_lifetime; /* Maximum life for flows */
  92. int expiry_interval; /* Interval between expiries */
  93. /* Statistics */
  94. u_int64_t total_packets; /* # of good packets */
  95. u_int64_t frag_packets; /* # of fragmented packets */
  96. u_int64_t non_ip_packets; /* # of not-IP packets */
  97. u_int64_t bad_packets; /* # of bad packets */
  98. u_int64_t flows_expired; /* # expired */
  99. u_int64_t flows_exported; /* # of flows sent */
  100. u_int64_t flows_dropped; /* # of flows dropped */
  101. u_int64_t flows_force_expired; /* # of flows forced out */
  102. u_int64_t packets_sent; /* # netflow packets sent */
  103. struct STATISTIC duration; /* Flow duration */
  104. struct STATISTIC octets; /* Bytes (bidir) */
  105. struct STATISTIC packets; /* Packets (bidir) */
  106. /* Per protocol statistics */
  107. u_int64_t flows_pp[256];
  108. u_int64_t octets_pp[256];
  109. u_int64_t packets_pp[256];
  110. struct STATISTIC duration_pp[256];
  111. /* Timeout statistics */
  112. u_int64_t expired_general;
  113. u_int64_t expired_tcp;
  114. u_int64_t expired_tcp_rst;
  115. u_int64_t expired_tcp_fin;
  116. u_int64_t expired_udp;
  117. u_int64_t expired_icmp;
  118. u_int64_t expired_maxlife;
  119. u_int64_t expired_overbytes;
  120. u_int64_t expired_maxflows;
  121. u_int64_t expired_flush;
  122. };
  123. /*
  124. * This structure is an entry in the tree of flows that we are
  125. * currently tracking.
  126. *
  127. * Because flows are matched _bi-directionally_, they must be stored in
  128. * a canonical format: the numerically lowest address and port number must
  129. * be stored in the first address and port array slot respectively.
  130. */
  131. struct FLOW {
  132. /* Housekeeping */
  133. struct EXPIRY *expiry; /* Pointer to expiry record */
  134. FLOW_ENTRY(FLOW) trp; /* Tree pointer */
  135. /* Flow identity (all are in network byte order) */
  136. int af; /* Address family of flow */
  137. u_int32_t ip6_flowlabel[2]; /* IPv6 Flowlabel */
  138. union {
  139. struct in_addr v4;
  140. struct in6_addr v6;
  141. } addr[2]; /* Endpoint addresses */
  142. u_int16_t port[2]; /* Endpoint ports */
  143. u_int8_t tcp_flags[2]; /* Cumulative OR of flags */
  144. u_int8_t protocol; /* Protocol */
  145. /* Per-flow statistics (all in _host_ byte order) */
  146. u_int64_t flow_seq; /* Flow ID */
  147. struct timeval flow_start; /* Time of creation */
  148. struct timeval flow_last; /* Time of last traffic */
  149. /* Per-endpoint statistics (all in _host_ byte order) */
  150. u_int32_t octets[2]; /* Octets so far */
  151. u_int32_t packets[2]; /* Packets so far */
  152. };
  153. /*
  154. * This is an entry in the tree of expiry events. The tree is used to
  155. * avoid traversion the whole tree of active flows looking for ones to
  156. * expire. "expires_at" is the time at which the flow should be discarded,
  157. * or zero if it is scheduled for immediate disposal.
  158. *
  159. * When a flow which hasn't been scheduled for immediate expiry registers
  160. * traffic, it is deleted from its current position in the tree and
  161. * re-inserted (subject to its updated timeout).
  162. *
  163. * Expiry scans operate by starting at the head of the tree and expiring
  164. * each entry with expires_at < now
  165. *
  166. */
  167. struct EXPIRY {
  168. EXPIRY_ENTRY(EXPIRY) trp; /* Tree pointer */
  169. struct FLOW *flow; /* pointer to flow */
  170. u_int32_t expires_at; /* time_t */
  171. enum {
  172. R_GENERAL, R_TCP, R_TCP_RST, R_TCP_FIN, R_UDP, R_ICMP,
  173. R_MAXLIFE, R_OVERBYTES, R_OVERFLOWS, R_FLUSH
  174. } reason;
  175. };
  176. /* Prototype for functions shared from softflowd.c */
  177. u_int32_t timeval_sub_ms(const struct timeval *t1, const struct timeval *t2);
  178. /* Prototypes for functions to send NetFlow packets, from netflow*.c */
  179. int send_netflow_v1(struct FLOW **flows, int num_flows, int nfsock,
  180. u_int16_t ifidx, u_int64_t *flows_exported, struct timeval *system_boot_time,
  181. int verbose_flag);
  182. int send_netflow_v5(struct FLOW **flows, int num_flows, int nfsock,
  183. u_int16_t ifidx, u_int64_t *flows_exported, struct timeval *system_boot_time,
  184. int verbose_flag);
  185. int send_netflow_v9(struct FLOW **flows, int num_flows, int nfsock,
  186. u_int16_t ifidx, u_int64_t *flows_exported, struct timeval *system_boot_time,
  187. int verbose_flag);
  188. /* Force a resend of the flow template */
  189. void netflow9_resend_template(void);
  190. #endif /* _SOFTFLOWD_H */