1 /*-
2 * Copyright (c) 2004-2005 Gleb Smirnoff <glebius@FreeBSD.org>
3 * Copyright (c) 2001-2003 Roman V. Palagin <romanp@unshadow.net>
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 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $SourceForge: ng_netflow.h,v 1.26 2004/09/04 15:44:55 glebius Exp $
28 * $FreeBSD: src/sys/netgraph/netflow/ng_netflow.h,v 1.2.2.3 2005/05/11 10:45:31 glebius Exp $
29 */
30
31 #ifndef _NG_NETFLOW_H_
32 #define _NG_NETFLOW_H_
33
34 #define NG_NETFLOW_NODE_TYPE "netflow"
35 #define NGM_NETFLOW_COOKIE 1101814790
36
37 #define NG_NETFLOW_MAXIFACES 512
38
39 /* Hook names */
40
41 #define NG_NETFLOW_HOOK_DATA "iface"
42 #define NG_NETFLOW_HOOK_OUT "out"
43 #define NG_NETFLOW_HOOK_EXPORT "export"
44
45 /* Netgraph commands understood by netflow node */
46 enum {
47 NGM_NETFLOW_INFO = 1, /* get node info */
48 NGM_NETFLOW_IFINFO, /* get iface info */
49 NGM_NETFLOW_SHOW, /* show ip cache flow */
50 NGM_NETFLOW_SETDLT, /* set data-link type */
51 NGM_NETFLOW_SETIFINDEX, /* set interface index */
52 NGM_NETFLOW_SETTIMEOUTS, /* set active/inactive flow timeouts */
53 };
54
55 /* This structure is returned by the NGM_NETFLOW_INFO message */
56 struct ng_netflow_info {
57 uint64_t nfinfo_bytes; /* total number of accounted bytes */
58 uint32_t nfinfo_packets; /* total number of accounted packets */
59 uint32_t nfinfo_used; /* number of used cache records */
60 uint32_t nfinfo_free; /* number of free records */
61 uint32_t nfinfo_inact_t; /* flow inactive timeout */
62 uint32_t nfinfo_act_t; /* flow active timeout */
63 };
64
65 /* This structure is returned by the NGM_NETFLOW_IFINFO message */
66 struct ng_netflow_ifinfo {
67 uint32_t ifinfo_packets; /* number of packets for this iface */
68 uint8_t ifinfo_dlt; /* Data Link Type, DLT_XXX */
69 #define MAXDLTNAMELEN 20
70 u_int16_t ifinfo_index; /* connected iface index */
71 };
72
73
74 /* This structure is passed to NGM_NETFLOW_SETDLT message */
75 struct ng_netflow_setdlt {
76 uint16_t iface; /* which iface dlt change */
77 uint8_t dlt; /* DLT_XXX from bpf.h */
78 };
79
80 /* This structure is passed to NGM_NETFLOW_SETIFINDEX */
81 struct ng_netflow_setifindex {
82 u_int16_t iface; /* which iface index change */
83 u_int16_t index; /* new index */
84 };
85
86 /* This structure is passed to NGM_NETFLOW_SETTIMEOUTS */
87 struct ng_netflow_settimeouts {
88 uint32_t inactive_timeout; /* flow inactive timeout */
89 uint32_t active_timeout; /* flow active timeout */
90 };
91
92 /* This is unique data, which identifies flow */
93 struct flow_rec {
94 struct in_addr r_src;
95 struct in_addr r_dst;
96 union {
97 struct {
98 uint16_t s_port; /* source TCP/UDP port */
99 uint16_t d_port; /* destination TCP/UDP port */
100 } dir;
101 uint32_t both;
102 } ports;
103 union {
104 struct {
105 u_char prot; /* IP protocol */
106 u_char tos; /* IP TOS */
107 uint16_t i_ifx; /* input interface index */
108 } i;
109 uint32_t all;
110 } misc;
111 };
112
113 #define r_ip_p misc.i.prot
114 #define r_tos misc.i.tos
115 #define r_i_ifx misc.i.i_ifx
116 #define r_misc misc.all
117 #define r_ports ports.both
118 #define r_sport ports.dir.s_port
119 #define r_dport ports.dir.d_port
120
121 /* A flow entry which accumulates statistics */
122 struct flow_entry_data {
123 struct flow_rec r;
124 struct in_addr next_hop;
125 uint16_t fle_o_ifx; /* output interface index */
126 #define fle_i_ifx r.misc.i.i_ifx
127 uint8_t dst_mask; /* destination route mask bits */
128 uint8_t src_mask; /* source route mask bits */
129 u_long packets;
130 u_long bytes;
131 long first; /* uptime on first packet */
132 long last; /* uptime on last packet */
133 u_char tcp_flags; /* cumulative OR */
134 };
135
136 /*
137 * How many flow records we will transfer at once
138 * without overflowing socket receive buffer
139 */
140 #define NREC_AT_ONCE 1000
141 #define NGRESP_SIZE (sizeof(struct ngnf_flows) + (NREC_AT_ONCE * \
142 sizeof(struct flow_entry_data)))
143 #define SORCVBUF_SIZE (NGRESP_SIZE + 2 * sizeof(struct ng_mesg))
144
145 /* This struct is returned to userland, when "show cache ip flow" */
146 struct ngnf_flows {
147 uint32_t nentries;
148 uint32_t last;
149 struct flow_entry_data entries[0];
150 };
151
152 /* Everything below is for kernel */
153
154 #ifdef _KERNEL
155
156 struct flow_entry {
157 struct flow_entry_data f;
158
159 LIST_ENTRY(flow_entry) fle_hash; /* entries in one hash item */
160 TAILQ_ENTRY(flow_entry) fle_work; /* entries in work queue*/
161 SLIST_ENTRY(flow_entry) fle_free; /* entries in free stack */
162 };
163
164 /* Parsing declarations */
165
166 /* Parse the info structure */
167 #define NG_NETFLOW_INFO_TYPE { \
168 { "Bytes", &ng_parse_uint64_type }, \
169 { "Packets", &ng_parse_uint32_type }, \
170 { "Records used", &ng_parse_uint32_type },\
171 { "Records free", &ng_parse_uint32_type },\
172 { "Inactive timeout", &ng_parse_uint32_type },\
173 { "Active timeout", &ng_parse_uint32_type },\
174 { NULL } \
175 }
176
177 /* Parse the ifinfo structure */
178 #define NG_NETFLOW_IFINFO_TYPE { \
179 { "packets", &ng_parse_uint32_type }, \
180 { "data link type", &ng_parse_uint8_type }, \
181 { "index", &ng_parse_uint16_type }, \
182 { NULL } \
183 }
184
185 /* Parse the setdlt structure */
186 #define NG_NETFLOW_SETDLT_TYPE { \
187 { "iface", &ng_parse_uint16_type }, \
188 { "dlt", &ng_parse_uint8_type }, \
189 { NULL } \
190 }
191
192 /* Parse the setifindex structure */
193 #define NG_NETFLOW_SETIFINDEX_TYPE { \
194 { "iface", &ng_parse_uint16_type }, \
195 { "index", &ng_parse_uint16_type }, \
196 { NULL } \
197 }
198
199 /* Parse the settimeouts structure */
200 #define NG_NETFLOW_SETTIMEOUTS_TYPE { \
201 { "inactive", &ng_parse_uint32_type }, \
202 { "active", &ng_parse_uint32_type }, \
203 { NULL } \
204 }
205
206 /* Private hook data */
207 struct ng_netflow_iface {
208 hook_p hook; /* NULL when disconnected */
209 hook_p out; /* NULL when no bypass hook */
210 struct ng_netflow_ifinfo info;
211 };
212
213 typedef struct ng_netflow_iface *iface_p;
214 typedef struct ng_netflow_ifinfo *ifinfo_p;
215
216 /* Structure describing our flow engine */
217 struct netflow {
218 node_p node; /* link to the node itself */
219
220 struct ng_netflow_iface ifaces[NG_NETFLOW_MAXIFACES]; /* incoming */
221 hook_p export; /* export data goes there */
222
223 struct ng_netflow_info info;
224 uint32_t flow_seq; /* current flow sequence */
225
226 struct callout exp_callout;
227
228 /* Flow cache is a big chunk of memory referenced by 'cache'.
229 * Accounting engine searches for its record using hashing index
230 * 'hash'. Expiry engine searches for its record from begining of
231 * tail queue 'expire_q'. Allocation is performed using last free
232 * stack held in singly linked list 'free_l' */
233 #define CACHESIZE 65536
234 #define CACHELOWAT (CACHESIZE * 3/4)
235 #define CACHEHIGHWAT (CACHESIZE * 9/10)
236 struct flow_entry *cache;
237 struct flow_hash_entry *hash;
238 TAILQ_HEAD( , flow_entry) work_queue;
239 SLIST_HEAD( , flow_entry) free_list;
240 SLIST_HEAD( , flow_entry) expire_list;
241
242 /* Mutexes to protect above lists */
243 struct mtx work_mtx;
244 struct mtx free_mtx;
245 struct mtx expire_mtx;
246
247 /* ng_netflow_export_send() forms its datagram here. */
248 struct netflow_export_dgram {
249 struct netflow_v5_header header;
250 struct netflow_v5_record r[NETFLOW_V5_MAX_RECORDS];
251 } __attribute__((__packed__)) dgram;
252 };
253
254 typedef struct netflow *priv_p;
255
256 /* Header of a small list in hash cell */
257 struct flow_hash_entry {
258 LIST_HEAD( ,flow_entry) head;
259 };
260
261 #define ERROUT(x) { error = (x); goto done; }
262
263 /* Prototypes for netflow.c */
264 int ng_netflow_cache_init(priv_p);
265 void ng_netflow_cache_flush(priv_p);
266 void ng_netflow_copyinfo(priv_p, struct ng_netflow_info *);
267 timeout_t ng_netflow_expire;
268 int ng_netflow_flow_add(priv_p, struct ip *, iface_p, struct ifnet *);
269 int ng_netflow_flow_show(priv_p, uint32_t last, struct ng_mesg *);
270
271 #endif /* _KERNEL */
272 #endif /* _NG_NETFLOW_H_ */
Cache object: 35d0cd4f05ace3a902986b3c3362a8fc
|