]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/netgraph/netflow/ng_netflow.h
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / netgraph / netflow / ng_netflow.h
1 /*-
2  * Copyright (c) 2010-2011 Alexander V. Chernikov <melifaro@ipfw.ru>
3  * Copyright (c) 2004-2005 Gleb Smirnoff <glebius@FreeBSD.org>
4  * Copyright (c) 2001-2003 Roman V. Palagin <romanp@unshadow.net>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  *       $SourceForge: ng_netflow.h,v 1.26 2004/09/04 15:44:55 glebius Exp $
29  *       $FreeBSD$
30  */
31
32 #ifndef _NG_NETFLOW_H_
33 #define _NG_NETFLOW_H_
34
35 #define NG_NETFLOW_NODE_TYPE    "netflow"
36 #define NGM_NETFLOW_COOKIE      1309868867
37 #define NGM_NETFLOW_V9_COOKIE   1349865386
38
39 #define NG_NETFLOW_MAXIFACES    USHRT_MAX
40
41 /* Hook names */
42
43 #define NG_NETFLOW_HOOK_DATA    "iface"
44 #define NG_NETFLOW_HOOK_OUT     "out"
45 #define NG_NETFLOW_HOOK_EXPORT  "export"
46 #define NG_NETFLOW_HOOK_EXPORT9 "export9"
47
48 /* This define effectively disable (v5) netflow export hook! */
49 /* #define COUNTERS_64 */
50
51 /* Netgraph commands understood by netflow node */
52 enum {
53     NGM_NETFLOW_INFO = 1|NGM_READONLY|NGM_HASREPLY,     /* get node info */
54     NGM_NETFLOW_IFINFO = 2|NGM_READONLY|NGM_HASREPLY,   /* get iface info */
55     NGM_NETFLOW_SHOW = 3|NGM_READONLY|NGM_HASREPLY,     /* show ip cache flow */
56     NGM_NETFLOW_SETDLT          = 4,    /* set data-link type */        
57     NGM_NETFLOW_SETIFINDEX      = 5,    /* set interface index */
58     NGM_NETFLOW_SETTIMEOUTS     = 6,    /* set active/inactive flow timeouts */
59     NGM_NETFLOW_SETCONFIG       = 7,    /* set flow generation options */
60     NGM_NETFLOW_SETTEMPLATE     = 8,    /* set v9 flow template periodic */
61     NGM_NETFLOW_SETMTU          = 9,    /* set outgoing interface MTU */
62     NGM_NETFLOW_V9INFO = 10|NGM_READONLY|NGM_HASREPLY,  /* get v9 info */
63 };
64
65 /* This structure is returned by the NGM_NETFLOW_INFO message */
66 struct ng_netflow_info {
67         uint64_t        nfinfo_bytes;           /* accounted IPv4 bytes */
68         uint32_t        nfinfo_packets;         /* accounted IPv4 packets */
69         uint64_t        nfinfo_bytes6;          /* accounted IPv6 bytes */
70         uint32_t        nfinfo_packets6;        /* accounted IPv6 packets */
71         uint64_t        nfinfo_sbytes;          /* skipped IPv4 bytes */
72         uint32_t        nfinfo_spackets;        /* skipped IPv4 packets */
73         uint64_t        nfinfo_sbytes6;         /* skipped IPv6 bytes */
74         uint32_t        nfinfo_spackets6;       /* skipped IPv6 packets */
75         uint32_t        nfinfo_used;            /* used cache records */
76         uint32_t        nfinfo_used6;           /* used IPv6 cache records */
77         uint32_t        nfinfo_alloc_failed;    /* failed allocations */
78         uint32_t        nfinfo_export_failed;   /* failed exports */
79         uint32_t        nfinfo_export9_failed;  /* failed exports */
80         uint32_t        nfinfo_realloc_mbuf;    /* reallocated mbufs */
81         uint32_t        nfinfo_alloc_fibs;      /* fibs allocated */
82         uint32_t        nfinfo_act_exp;         /* active expiries */
83         uint32_t        nfinfo_inact_exp;       /* inactive expiries */
84         uint32_t        nfinfo_inact_t;         /* flow inactive timeout */
85         uint32_t        nfinfo_act_t;           /* flow active timeout */
86 };
87
88 /* This structure is returned by the NGM_NETFLOW_IFINFO message */
89 struct ng_netflow_ifinfo {
90         uint32_t        ifinfo_packets; /* number of packets for this iface */
91         uint8_t         ifinfo_dlt;     /* Data Link Type, DLT_XXX */
92 #define MAXDLTNAMELEN   20
93         u_int16_t       ifinfo_index;   /* connected iface index */
94         uint32_t        conf;
95 };
96
97
98 /* This structure is passed to NGM_NETFLOW_SETDLT message */
99 struct ng_netflow_setdlt {
100         uint16_t iface;         /* which iface dlt change */
101         uint8_t  dlt;                   /* DLT_XXX from bpf.h */
102 };
103
104 /* This structure is passed to NGM_NETFLOW_SETIFINDEX */
105 struct ng_netflow_setifindex {
106         u_int16_t iface;                /* which iface index change */
107         u_int16_t index;                /* new index */
108 };
109
110 /* This structure is passed to NGM_NETFLOW_SETTIMEOUTS */
111 struct ng_netflow_settimeouts {
112         uint32_t        inactive_timeout;       /* flow inactive timeout */
113         uint32_t        active_timeout;         /* flow active timeout */
114 };
115
116 #define NG_NETFLOW_CONF_INGRESS         1
117 #define NG_NETFLOW_CONF_EGRESS          2
118 #define NG_NETFLOW_CONF_ONCE            4
119 #define NG_NETFLOW_CONF_THISONCE        8
120
121 /* This structure is passed to NGM_NETFLOW_SETCONFIG */
122 struct ng_netflow_setconfig {
123         u_int16_t iface;                /* which iface config change */
124         u_int32_t conf;                 /* new config */
125 };
126
127 /* This structure is passed to NGM_NETFLOW_SETTEMPLATE */
128 struct ng_netflow_settemplate {
129         uint16_t time;          /* max time between announce */
130         uint16_t packets;       /* max packets between announce */
131 };
132
133 /* This structure is passed to NGM_NETFLOW_SETMTU */
134 struct ng_netflow_setmtu {
135         uint16_t mtu;           /* MTU for packet */
136 };
137
138 /* This structure is used in NGM_NETFLOW_SHOW request/responce */
139 struct ngnf_show_header {
140         u_char          version;        /* IPv4 or IPv6 */
141         uint32_t        hash_id;        /* current hash index */
142         uint32_t        list_id;        /* current record number in given hash */
143         uint32_t        nentries;       /* number of records in response */
144 };
145
146 /* This structure is used in NGM_NETFLOW_V9INFO message */
147 struct ng_netflow_v9info {
148         uint16_t        templ_packets;  /* v9 template packets */
149         uint16_t        templ_time;     /* v9 template time */
150         uint16_t        mtu;            /* v9 MTU */
151 };
152
153 /* XXXGL
154  * Somewhere flow_rec6 is casted to flow_rec, and flow6_entry_data is
155  * casted to flow_entry_data. After casting, fle->r.fib is accessed.
156  * So beginning of these structs up to fib should be kept common.
157  */
158
159 /* This is unique data, which identifies flow */
160 struct flow_rec {
161         uint16_t        flow_type; /* IPv4 L4/L3 flow, see NETFLOW_V9_FLOW* */
162         uint16_t        fib;
163         struct in_addr  r_src;
164         struct in_addr  r_dst;
165         union {
166                 struct {
167                         uint16_t        s_port; /* source TCP/UDP port */
168                         uint16_t        d_port; /* destination TCP/UDP port */
169                 } dir;
170                 uint32_t both;
171         } ports;
172         union {
173                 struct {
174                         u_char          prot;   /* IP protocol */
175                         u_char          tos;    /* IP TOS */
176                         uint16_t        i_ifx;  /* input interface index */
177                 } i;
178                 uint32_t all;
179         } misc;
180 };
181
182 /* This is unique data, which identifies flow */
183 struct flow6_rec {
184         uint16_t        flow_type; /* IPv4 L4/L3 Ipv6 L4/L3 flow, see NETFLOW_V9_FLOW* */
185         uint16_t        fib;
186         union {
187                 struct in_addr  r_src;
188                 struct in6_addr r_src6;
189         } src;
190         union {
191                 struct in_addr  r_dst;
192                 struct in6_addr r_dst6;
193         } dst;
194         union {
195                 struct {
196                         uint16_t        s_port; /* source TCP/UDP port */
197                         uint16_t        d_port; /* destination TCP/UDP port */
198                 } dir;
199                 uint32_t both;
200         } ports;
201         union {
202                 struct {
203                         u_char          prot;   /* IP protocol */
204                         u_char          tos;    /* IP TOS */
205                         uint16_t        i_ifx;  /* input interface index */
206                 } i;
207                 uint32_t all;
208         } misc;
209 };
210
211 #define r_ip_p  misc.i.prot
212 #define r_tos   misc.i.tos
213 #define r_i_ifx misc.i.i_ifx
214 #define r_misc  misc.all
215 #define r_ports ports.both
216 #define r_sport ports.dir.s_port
217 #define r_dport ports.dir.d_port
218         
219 /* A flow entry which accumulates statistics */
220 struct flow_entry_data {
221         uint16_t                version;        /* Protocol version */
222         struct flow_rec         r;
223         struct in_addr          next_hop;
224         uint16_t                fle_o_ifx;      /* output interface index */
225 #define                         fle_i_ifx       r.misc.i.i_ifx
226         uint8_t         dst_mask;       /* destination route mask bits */
227         uint8_t         src_mask;       /* source route mask bits */
228         u_long                  packets;
229         u_long                  bytes;
230         long                    first;  /* uptime on first packet */
231         long                    last;   /* uptime on last packet */
232         u_char                  tcp_flags;      /* cumulative OR */
233 };
234
235 struct flow6_entry_data {
236         uint16_t                version;        /* Protocol version */
237         struct flow6_rec        r;
238         union {
239                 struct in_addr          next_hop;
240                 struct in6_addr         next_hop6;
241         } n;
242         uint16_t                fle_o_ifx;      /* output interface index */
243 #define                         fle_i_ifx       r.misc.i.i_ifx
244         uint8_t         dst_mask;       /* destination route mask bits */
245         uint8_t         src_mask;       /* source route mask bits */
246         u_long                  packets;
247         u_long                  bytes;
248         long                    first;  /* uptime on first packet */
249         long                    last;   /* uptime on last packet */
250         u_char                  tcp_flags;      /* cumulative OR */
251 };
252
253 /*
254  * How many flow records we will transfer at once
255  * without overflowing socket receive buffer
256  */
257 #define NREC_AT_ONCE            1000
258 #define NREC6_AT_ONCE           (NREC_AT_ONCE * sizeof(struct flow_entry_data) / \
259                                 sizeof(struct flow6_entry_data))
260 #define NGRESP_SIZE             (sizeof(struct ngnf_show_header) + (NREC_AT_ONCE * \
261                                 sizeof(struct flow_entry_data)))
262 #define SORCVBUF_SIZE           (NGRESP_SIZE + 2 * sizeof(struct ng_mesg))
263
264 /* Everything below is for kernel */
265
266 #ifdef _KERNEL
267
268 struct flow_entry {
269         TAILQ_ENTRY(flow_entry) fle_hash;       /* entries in hash slot */
270         struct flow_entry_data  f;
271 };
272
273 struct flow6_entry {
274         TAILQ_ENTRY(flow_entry) fle_hash;       /* entries in hash slot */
275         struct flow6_entry_data f;
276 };
277 /* Parsing declarations */
278
279 /* Parse the info structure */
280 #define NG_NETFLOW_INFO_TYPE    {                       \
281         { "IPv4 bytes",         &ng_parse_uint64_type },        \
282         { "IPv4 packets",       &ng_parse_uint32_type },        \
283         { "IPv6 bytes",         &ng_parse_uint64_type },        \
284         { "IPv6 packets",       &ng_parse_uint32_type },        \
285         { "IPv4 skipped bytes",         &ng_parse_uint64_type },        \
286         { "IPv4 skipped packets",       &ng_parse_uint32_type },        \
287         { "IPv6 skipped bytes",         &ng_parse_uint64_type },        \
288         { "IPv6 skipped packets",       &ng_parse_uint32_type },        \
289         { "IPv4 records used",  &ng_parse_uint32_type },\
290         { "IPv6 records used",  &ng_parse_uint32_type },\
291         { "Failed allocations", &ng_parse_uint32_type },\
292         { "V5 failed exports",  &ng_parse_uint32_type },\
293         { "V9 failed exports",  &ng_parse_uint32_type },\
294         { "mbuf reallocations", &ng_parse_uint32_type },\
295         { "fibs allocated",     &ng_parse_uint32_type },\
296         { "Active expiries",    &ng_parse_uint32_type },\
297         { "Inactive expiries",  &ng_parse_uint32_type },\
298         { "Inactive timeout",   &ng_parse_uint32_type },\
299         { "Active timeout",     &ng_parse_uint32_type },\
300         { NULL }                                        \
301 }
302
303 /* Parse the ifinfo structure */
304 #define NG_NETFLOW_IFINFO_TYPE  {                       \
305         { "packets",    &ng_parse_uint32_type },        \
306         { "data link type", &ng_parse_uint8_type },     \
307         { "index", &ng_parse_uint16_type },             \
308         { "conf", &ng_parse_uint32_type },              \
309         { NULL }                                        \
310 }
311
312 /* Parse the setdlt structure */
313 #define NG_NETFLOW_SETDLT_TYPE {                        \
314         { "iface",      &ng_parse_uint16_type },        \
315         { "dlt",        &ng_parse_uint8_type  },        \
316         { NULL }                                        \
317 }
318
319 /* Parse the setifindex structure */
320 #define NG_NETFLOW_SETIFINDEX_TYPE {                    \
321         { "iface",      &ng_parse_uint16_type },        \
322         { "index",      &ng_parse_uint16_type },        \
323         { NULL }                                        \
324 }
325
326 /* Parse the settimeouts structure */
327 #define NG_NETFLOW_SETTIMEOUTS_TYPE {                   \
328         { "inactive",   &ng_parse_uint32_type },        \
329         { "active",     &ng_parse_uint32_type },        \
330         { NULL }                                        \
331 }
332
333 /* Parse the setifindex structure */
334 #define NG_NETFLOW_SETCONFIG_TYPE {                     \
335         { "iface",      &ng_parse_uint16_type },        \
336         { "conf",       &ng_parse_uint32_type },        \
337         { NULL }                                        \
338 }
339
340 /* Parse the settemplate structure */
341 #define NG_NETFLOW_SETTEMPLATE_TYPE {           \
342         { "time",       &ng_parse_uint16_type },        \
343         { "packets",    &ng_parse_uint16_type },        \
344         { NULL }                                        \
345 }
346
347 /* Parse the setmtu structure */
348 #define NG_NETFLOW_SETMTU_TYPE {                        \
349         { "mtu",        &ng_parse_uint16_type },        \
350         { NULL }                                        \
351 }
352
353 /* Parse the v9info structure */
354 #define NG_NETFLOW_V9INFO_TYPE {                        \
355         { "v9 template packets",        &ng_parse_uint16_type },\
356         { "v9 template time",   &ng_parse_uint16_type },\
357         { "v9 MTU",             &ng_parse_uint16_type },\
358         { NULL }                                        \
359 }
360
361 /* Private hook data */
362 struct ng_netflow_iface {
363         hook_p          hook;           /* NULL when disconnected */
364         hook_p          out;            /* NULL when no bypass hook */
365         struct ng_netflow_ifinfo        info;
366 };
367
368 typedef struct ng_netflow_iface *iface_p;
369 typedef struct ng_netflow_ifinfo *ifinfo_p;
370
371 struct netflow_export_item {
372         item_p          item;
373         item_p          item9;
374         struct netflow_v9_packet_opt    *item9_opt;
375 };
376
377 /* Structure contatining fib-specific data */
378 struct fib_export {
379         uint32_t                        fib;            /* kernel fib id */
380         struct netflow_export_item      exp;            /* Various data used for export */
381         struct mtx                      export_mtx;     /* exp.item mutex */
382         struct mtx                      export9_mtx;    /* exp.item9 mutex */
383         uint32_t                        flow_seq;       /* current V5 flow sequence */
384         uint32_t                        flow9_seq;      /* current V9 flow sequence */
385         uint32_t                        domain_id;      /* Observartion domain id */
386         /* Netflow V9 counters */
387         uint32_t                        templ_last_ts;  /* unixtime of last template announce */
388         uint32_t                        templ_last_pkt; /* packets count on last template announce */
389         uint32_t                        sent_packets;   /* packets sent by exporter; */
390         struct netflow_v9_packet_opt    *export9_opt;   /* current packet specific options */
391 };
392
393 typedef struct fib_export *fib_export_p;
394
395 /* Structure describing our flow engine */
396 struct netflow {
397         node_p                  node;           /* link to the node itself */
398         hook_p                  export;         /* export data goes there */
399         hook_p                  export9;        /* Netflow V9 export data goes there */
400
401         struct ng_netflow_info  info;
402         struct callout          exp_callout;    /* expiry periodic job */
403
404         /*
405          * Flow entries are allocated in uma(9) zone zone. They are
406          * indexed by hash hash. Each hash element consist of tailqueue
407          * head and mutex to protect this element.
408          */
409 #define CACHESIZE                       (65536*4)
410 #define CACHELOWAT                      (CACHESIZE * 3/4)
411 #define CACHEHIGHWAT                    (CACHESIZE * 9/10)
412         uma_zone_t              zone;
413         struct flow_hash_entry  *hash;
414
415         /*
416          * NetFlow data export
417          *
418          * export_item is a data item, it has an mbuf with cluster
419          * attached to it. A thread detaches export_item from priv
420          * and works with it. If the export is full it is sent, and
421          * a new one is allocated. Before exiting thread re-attaches
422          * its current item back to priv. If there is item already,
423          * current incomplete datagram is sent.
424          * export_mtx is used for attaching/detaching.
425          */
426
427         /* IPv6 support */
428 #ifdef INET6
429         uma_zone_t              zone6;
430         struct flow_hash_entry  *hash6;
431 #endif
432         /* Multiple FIB support */
433         fib_export_p            *fib_data; /* array of pointers to per-fib data */
434         uint16_t                maxfibs; /* number of allocated fibs */
435
436         /* Netflow v9 configuration options */
437         /*
438          * RFC 3954 clause 7.3
439          * "Both options MUST be configurable by the user on the Exporter."
440          */
441         uint16_t                templ_time;     /* time between sending templates */
442         uint16_t                templ_packets;  /* packets between sending templates */
443 #define NETFLOW_V9_MAX_FLOWSETS 2
444         u_char                  flowsets_count; /* current flowsets used */
445         u_char                  flowset_records[NETFLOW_V9_MAX_FLOWSETS - 1]; /* Count of records in each flowset */
446         uint16_t                mtu;            /* export interface MTU */
447         struct netflow_v9_flowset_header        *v9_flowsets[NETFLOW_V9_MAX_FLOWSETS - 1]; /* Pointers to pre-compiled flowsets */
448
449         struct ng_netflow_iface ifaces[NG_NETFLOW_MAXIFACES];
450 };
451
452 typedef struct netflow *priv_p;
453
454 /* Header of a small list in hash cell */
455 struct flow_hash_entry {
456         struct mtx              mtx;
457         TAILQ_HEAD(fhead, flow_entry) head;
458 };
459
460 #define ERROUT(x)       { error = (x); goto done; }
461
462 #define MTAG_NETFLOW            1221656444
463 #define MTAG_NETFLOW_CALLED     0
464
465 #define m_pktlen(m)     ((m)->m_pkthdr.len)
466 #define IP6VERSION      6
467
468 #define priv_to_fib(priv, fib)  (priv)->fib_data[(fib)]
469
470 /*
471  * Cisco uses milliseconds for uptime. Bad idea, since it overflows
472  * every 48+ days. But we will do same to keep compatibility. This macro
473  * does overflowable multiplication to 1000.
474  */
475 #define MILLIUPTIME(t)  (((t) << 9) +   /* 512 */       \
476                          ((t) << 8) +   /* 256 */       \
477                          ((t) << 7) +   /* 128 */       \
478                          ((t) << 6) +   /* 64  */       \
479                          ((t) << 5) +   /* 32  */       \
480                          ((t) << 3))    /* 8   */
481
482 /* Prototypes for netflow.c */
483 void    ng_netflow_cache_init(priv_p);
484 void    ng_netflow_cache_flush(priv_p);
485 int     ng_netflow_fib_init(priv_p priv, int fib);
486 void    ng_netflow_copyinfo(priv_p, struct ng_netflow_info *);
487 void    ng_netflow_copyv9info(priv_p, struct ng_netflow_v9info *);
488 timeout_t ng_netflow_expire;
489 int     ng_netflow_flow_add(priv_p, fib_export_p, struct ip *, caddr_t, uint8_t, uint8_t, unsigned int);
490 int     ng_netflow_flow6_add(priv_p, fib_export_p, struct ip6_hdr *, caddr_t , uint8_t, uint8_t, unsigned int);
491 int     ng_netflow_flow_show(priv_p, struct ngnf_show_header *req, struct ngnf_show_header *resp);
492
493 void    ng_netflow_v9_cache_init(priv_p);
494 void    ng_netflow_v9_cache_flush(priv_p);
495 item_p  get_export9_dgram(priv_p, fib_export_p, struct netflow_v9_packet_opt **);
496 void    return_export9_dgram(priv_p, fib_export_p, item_p,
497             struct netflow_v9_packet_opt *, int);
498 int     export9_add(item_p, struct netflow_v9_packet_opt *, struct flow_entry *);
499 int     export9_send(priv_p, fib_export_p, item_p, struct netflow_v9_packet_opt *,
500             int);
501
502 #endif  /* _KERNEL */
503 #endif  /* _NG_NETFLOW_H_ */