softflowd.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 contains optional information carried by Option Data
  69. * Record.
  70. */
  71. struct OPTION {
  72. uint32_t sample;
  73. };
  74. /*
  75. * This structure is the root of the flow tracking system.
  76. * It holds the root of the tree of active flows and the head of the
  77. * tree of expiry events. It also collects miscellaneous statistics
  78. */
  79. struct FLOWTRACK {
  80. /* The flows and their expiry events */
  81. FLOW_HEAD(FLOWS, FLOW) flows; /* Top of flow tree */
  82. EXPIRY_HEAD(EXPIRIES, EXPIRY) expiries; /* Top of expiries tree */
  83. struct freelist flow_freelist; /* Freelist for flows */
  84. struct freelist expiry_freelist; /* Freelist for expiry events */
  85. unsigned int num_flows; /* # of active flows */
  86. unsigned int max_flows; /* Max # of active flows */
  87. u_int64_t next_flow_seq; /* Next flow ID */
  88. /* Stuff related to flow export */
  89. struct timeval system_boot_time; /* SysUptime */
  90. int track_level; /* See TRACK_* above */
  91. /* Flow timeouts */
  92. int tcp_timeout; /* Open TCP connections */
  93. int tcp_rst_timeout; /* TCP flows after RST */
  94. int tcp_fin_timeout; /* TCP flows after bidi FIN */
  95. int udp_timeout; /* UDP flows */
  96. int icmp_timeout; /* ICMP flows */
  97. int general_timeout; /* Everything else */
  98. int maximum_lifetime; /* Maximum life for flows */
  99. int expiry_interval; /* Interval between expiries */
  100. /* Statistics */
  101. u_int64_t total_packets; /* # of good packets */
  102. u_int64_t non_sampled_packets; /* # of not sampled packets */
  103. u_int64_t frag_packets; /* # of fragmented packets */
  104. u_int64_t non_ip_packets; /* # of not-IP packets */
  105. u_int64_t bad_packets; /* # of bad packets */
  106. u_int64_t flows_expired; /* # expired */
  107. u_int64_t flows_exported; /* # of flows sent */
  108. u_int64_t flows_dropped; /* # of flows dropped */
  109. u_int64_t flows_force_expired; /* # of flows forced out */
  110. u_int64_t packets_sent; /* # netflow packets sent */
  111. struct STATISTIC duration; /* Flow duration */
  112. struct STATISTIC octets; /* Bytes (bidir) */
  113. struct STATISTIC packets; /* Packets (bidir) */
  114. /* Per protocol statistics */
  115. u_int64_t flows_pp[256];
  116. u_int64_t octets_pp[256];
  117. u_int64_t packets_pp[256];
  118. struct STATISTIC duration_pp[256];
  119. /* Timeout statistics */
  120. u_int64_t expired_general;
  121. u_int64_t expired_tcp;
  122. u_int64_t expired_tcp_rst;
  123. u_int64_t expired_tcp_fin;
  124. u_int64_t expired_udp;
  125. u_int64_t expired_icmp;
  126. u_int64_t expired_maxlife;
  127. u_int64_t expired_overbytes;
  128. u_int64_t expired_maxflows;
  129. u_int64_t expired_flush;
  130. /* Optional information */
  131. struct OPTION option;
  132. };
  133. /*
  134. * This structure is an entry in the tree of flows that we are
  135. * currently tracking.
  136. *
  137. * Because flows are matched _bi-directionally_, they must be stored in
  138. * a canonical format: the numerically lowest address and port number must
  139. * be stored in the first address and port array slot respectively.
  140. */
  141. struct FLOW {
  142. /* Housekeeping */
  143. struct EXPIRY *expiry; /* Pointer to expiry record */
  144. FLOW_ENTRY(FLOW) trp; /* Tree pointer */
  145. /* Flow identity (all are in network byte order) */
  146. int af; /* Address family of flow */
  147. u_int32_t ip6_flowlabel[2]; /* IPv6 Flowlabel */
  148. union {
  149. struct in_addr v4;
  150. struct in6_addr v6;
  151. } addr[2]; /* Endpoint addresses */
  152. u_int16_t port[2]; /* Endpoint ports */
  153. u_int8_t tcp_flags[2]; /* Cumulative OR of flags */
  154. u_int8_t protocol; /* Protocol */
  155. /* Per-flow statistics (all in _host_ byte order) */
  156. u_int64_t flow_seq; /* Flow ID */
  157. struct timeval flow_start; /* Time of creation */
  158. struct timeval flow_last; /* Time of last traffic */
  159. /* Per-endpoint statistics (all in _host_ byte order) */
  160. u_int32_t octets[2]; /* Octets so far */
  161. u_int32_t packets[2]; /* Packets so far */
  162. };
  163. /*
  164. * This is an entry in the tree of expiry events. The tree is used to
  165. * avoid traversion the whole tree of active flows looking for ones to
  166. * expire. "expires_at" is the time at which the flow should be discarded,
  167. * or zero if it is scheduled for immediate disposal.
  168. *
  169. * When a flow which hasn't been scheduled for immediate expiry registers
  170. * traffic, it is deleted from its current position in the tree and
  171. * re-inserted (subject to its updated timeout).
  172. *
  173. * Expiry scans operate by starting at the head of the tree and expiring
  174. * each entry with expires_at < now
  175. *
  176. */
  177. struct EXPIRY {
  178. EXPIRY_ENTRY(EXPIRY) trp; /* Tree pointer */
  179. struct FLOW *flow; /* pointer to flow */
  180. u_int32_t expires_at; /* time_t */
  181. enum {
  182. R_GENERAL, R_TCP, R_TCP_RST, R_TCP_FIN, R_UDP, R_ICMP,
  183. R_MAXLIFE, R_OVERBYTES, R_OVERFLOWS, R_FLUSH
  184. } reason;
  185. };
  186. /* Prototype for functions shared from softflowd.c */
  187. u_int32_t timeval_sub_ms(const struct timeval *t1, const struct timeval *t2);
  188. /* Prototypes for functions to send NetFlow packets, from netflow*.c */
  189. int send_netflow_v1(struct FLOW **flows, int num_flows, int nfsock,
  190. u_int16_t ifidx, u_int64_t *flows_exported, struct timeval *system_boot_time,
  191. int verbose_flag, struct OPTION *option);
  192. int send_netflow_v5(struct FLOW **flows, int num_flows, int nfsock,
  193. u_int16_t ifidx, u_int64_t *flows_exported, struct timeval *system_boot_time,
  194. int verbose_flag, struct OPTION *option);
  195. int send_netflow_v9(struct FLOW **flows, int num_flows, int nfsock,
  196. u_int16_t ifidx, u_int64_t *flows_exported, struct timeval *system_boot_time,
  197. int verbose_flag, struct OPTION *option);
  198. /* Force a resend of the flow template */
  199. void netflow9_resend_template(void);
  200. #endif /* _SOFTFLOWD_H */