xref: /freebsd/sys/netgraph/netflow/netflow.c (revision 6af83ee0d2941d18880b6aaa2b4facd1d30c6106)
1 /*-
2  * Copyright (c) 2004 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: netflow.c,v 1.41 2004/09/05 11:41:10 glebius Exp $
28  */
29 
30 static const char rcs_id[] =
31     "@(#) $FreeBSD$";
32 
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/limits.h>
36 #include <sys/mbuf.h>
37 #include <sys/syslog.h>
38 #include <sys/systm.h>
39 #include <sys/socket.h>
40 
41 #include <net/if.h>
42 #include <net/if_var.h>
43 #include <net/if_dl.h>
44 #include <net/route.h>
45 #include <netinet/in.h>
46 #include <netinet/in_systm.h>
47 #include <netinet/ip.h>
48 #include <netinet/tcp.h>
49 #include <netinet/udp.h>
50 
51 #include <netgraph/ng_message.h>
52 #include <netgraph/netgraph.h>
53 
54 #include <netgraph/netflow/netflow.h>
55 #include <netgraph/netflow/ng_netflow.h>
56 
57 #define	NBUCKETS	(4096)	/* must be power of 2 */
58 
59 /* This hash is for TCP or UDP packets */
60 #define FULL_HASH(addr1,addr2,port1,port2)\
61 	(((addr1 >> 16) ^		\
62 	  (addr2 & 0x00FF) ^		\
63 	  ((port1 ^ port2) << 8) )&	\
64 	 (NBUCKETS - 1))
65 
66 /* This hash for all other IP packets */
67 #define ADDR_HASH(addr1,addr2)\
68 	(((addr1 >> 16) ^		\
69 	  (addr2 & 0x00FF) )&		\
70 	 (NBUCKETS - 1))
71 
72 /* Macros to shorten logical constructions */
73 /* XXX: priv must exist in namespace */
74 #define	INACTIVE(fle)	(time_uptime - fle->f.last > priv->info.nfinfo_inact_t)
75 #define	AGED(fle)	(time_uptime - fle->f.first > priv->info.nfinfo_act_t)
76 #define	ISFREE(fle)	(fle->f.packets == 0)
77 
78 /*
79  * 4 is a magical number: statistically number of 4-packet flows is
80  * bigger than 5,6,7...-packet flows by an order of magnitude. Most UDP/ICMP
81  * scans are 1 packet (~ 90% of flow cache). TCP scans are 2-packet in case
82  * of reachable host and 4-packet otherwise.
83  */
84 #define	SMALL(fle)	(fle->f.packets <= 4)
85 
86 MALLOC_DECLARE(M_NETFLOW);
87 MALLOC_DEFINE(M_NETFLOW, "NetFlow", "flow cache");
88 
89 static int export_add(priv_p , struct flow_entry *);
90 static int export_send(priv_p );
91 
92 /* Generate hash for a given flow record */
93 static __inline uint32_t
94 ip_hash(struct flow_rec *r)
95 {
96 	switch (r->r_ip_p) {
97 	case IPPROTO_TCP:
98 	case IPPROTO_UDP:
99 		return FULL_HASH(r->r_src.s_addr, r->r_dst.s_addr,
100 		    r->r_sport, r->r_dport);
101 	default:
102 		return ADDR_HASH(r->r_src.s_addr, r->r_dst.s_addr);
103 	}
104 }
105 
106 /* Lookup for record in given slot */
107 static __inline struct flow_entry *
108 hash_lookup(struct flow_hash_entry *h, int slot, struct flow_rec *r)
109 {
110 	struct flow_entry *fle;
111 
112 	LIST_FOREACH(fle, &(h[slot].head), fle_hash)
113 		if (bcmp(r, &fle->f.r, sizeof(struct flow_rec)) == 0)
114 			return (fle);
115 
116 	return (NULL);
117 }
118 
119 /* Get a flow entry from free list */
120 static __inline struct flow_entry *
121 alloc_flow(priv_p priv, int *flows)
122 {
123 	register struct flow_entry	*fle;
124 
125 	mtx_lock(&priv->free_mtx);
126 
127 	if (SLIST_EMPTY(&priv->free_list)) {
128 		mtx_unlock(&priv->free_mtx);
129 		return(NULL);
130 	}
131 
132 	fle = SLIST_FIRST(&priv->free_list);
133 	SLIST_REMOVE_HEAD(&priv->free_list, fle_free);
134 
135 	priv->info.nfinfo_used++;
136 	priv->info.nfinfo_free--;
137 
138 	if (flows != NULL)
139 		*flows = priv->info.nfinfo_used;
140 
141 	mtx_unlock(&priv->free_mtx);
142 
143 	return (fle);
144 }
145 
146 /* Insert flow entry into a free list. */
147 static __inline int
148 free_flow(priv_p priv, struct flow_entry *fle)
149 {
150 	int flows;
151 
152 	mtx_lock(&priv->free_mtx);
153 	fle->f.packets = 0;
154 	SLIST_INSERT_HEAD(&priv->free_list, fle, fle_free);
155 	flows = priv->info.nfinfo_used--;
156 	priv->info.nfinfo_free++;
157 	mtx_unlock(&priv->free_mtx);
158 
159 	return flows;
160 }
161 
162 #define	NGNF_GETUSED(priv, rval)	do {	\
163 	mtx_lock(&priv->free_mtx);		\
164 	rval = priv->info.nfinfo_used;		\
165 	mtx_unlock(&priv->free_mtx);		\
166 	} while (0)
167 
168 /* Insert flow entry into expire list. */
169 /* XXX: Flow must be detached from work queue, but not from cache */
170 static __inline void
171 expire_flow(priv_p priv, struct flow_entry *fle)
172 {
173 	mtx_assert(&priv->work_mtx, MA_OWNED);
174 	LIST_REMOVE(fle, fle_hash);
175 
176 	mtx_lock(&priv->expire_mtx);
177 	SLIST_INSERT_HEAD(&priv->expire_list, fle, fle_free);
178 	mtx_unlock(&priv->expire_mtx);
179 }
180 
181 /* Get a snapshot of node statistics */
182 void
183 ng_netflow_copyinfo(priv_p priv, struct ng_netflow_info *i)
184 {
185 	mtx_lock(&priv->free_mtx);
186 	memcpy((void *)i, (void *)&priv->info, sizeof(priv->info));
187 	mtx_unlock(&priv->free_mtx);
188 }
189 
190 /* Calculate number of bits in netmask */
191 #define	g21	0x55555555ul	/* = 0101_0101_0101_0101_0101_0101_0101_0101 */
192 #define	g22	0x33333333ul	/* = 0011_0011_0011_0011_0011_0011_0011_0011 */
193 #define	g23	0x0f0f0f0ful	/* = 0000_1111_0000_1111_0000_1111_0000_1111 */
194 static __inline u_char
195 bit_count(uint32_t v)
196 {
197 	v = (v & g21) + ((v >> 1) & g21);
198 	v = (v & g22) + ((v >> 2) & g22);
199 	v = (v + (v >> 4)) & g23;
200 	return (v + (v >> 8) + (v >> 16) + (v >> 24)) & 0x3f;
201 }
202 
203 /*
204  * Insert a record into defined slot.
205  *
206  * First we get for us a free flow entry, then fill in all
207  * possible fields in it. Then obtain lock on flow cache
208  * and insert flow entry.
209  */
210 static __inline int
211 hash_insert(priv_p priv, int slot, struct flow_rec *r, int plen)
212 {
213 	struct flow_hash_entry	*h = priv->hash;
214 	struct flow_entry	*fle;
215 	struct route ro;
216 	struct sockaddr_in *sin;
217 
218 	fle = alloc_flow(priv, NULL);
219 	if (fle == NULL)
220 		return (ENOMEM);
221 
222 	/*
223 	 * Now fle is totally ours. It is detached from all lists,
224 	 * we can safely edit it.
225 	 */
226 
227 	bcopy(r, &fle->f.r, sizeof(struct flow_rec));
228 	fle->f.bytes = plen;
229 	fle->f.packets = 1;
230 
231 	priv->info.nfinfo_bytes += plen;
232 
233 	fle->f.first = fle->f.last = time_uptime;
234 
235 	/*
236 	 * First we do route table lookup on destination address. So we can
237 	 * fill in out_ifx, dst_mask, nexthop, and dst_as in future releases.
238 	 */
239 	bzero((caddr_t)&ro, sizeof(ro));
240 	sin = (struct sockaddr_in *)&ro.ro_dst;
241 	sin->sin_len = sizeof(*sin);
242 	sin->sin_family = AF_INET;
243 	sin->sin_addr = fle->f.r.r_dst;
244 	rtalloc_ign(&ro, RTF_CLONING);
245 	if (ro.ro_rt != NULL) {
246 		struct rtentry *rt = ro.ro_rt;
247 
248 		fle->f.fle_o_ifx = rt->rt_ifp->if_index;
249 
250 		if (rt->rt_flags & RTF_GATEWAY &&
251 		    rt->rt_gateway->sa_family == AF_INET)
252 			fle->f.next_hop =
253 			    ((struct sockaddr_in *)(rt->rt_gateway))->sin_addr;
254 
255 		if (rt_mask(rt))
256 			fle->f.dst_mask =
257 			    bit_count(((struct sockaddr_in *)rt_mask(rt))->sin_addr.s_addr);
258 		else if (rt->rt_flags & RTF_HOST)
259 			/* Give up. We can't determine mask :( */
260 			fle->f.dst_mask = 32;
261 
262 		RTFREE(ro.ro_rt);
263 	}
264 
265 	/* Do route lookup on source address, to fill in src_mask. */
266 
267 	bzero((caddr_t)&ro, sizeof(ro));
268 	sin = (struct sockaddr_in *)&ro.ro_dst;
269 	sin->sin_len = sizeof(*sin);
270 	sin->sin_family = AF_INET;
271 	sin->sin_addr = fle->f.r.r_src;
272 	rtalloc_ign(&ro, RTF_CLONING);
273 	if (ro.ro_rt != NULL) {
274 		struct rtentry *rt = ro.ro_rt;
275 
276 		if (rt_mask(rt))
277 			fle->f.src_mask =
278 			    bit_count(((struct sockaddr_in *)rt_mask(rt))->sin_addr.s_addr);
279 		else if (rt->rt_flags & RTF_HOST)
280 			/* Give up. We can't determine mask :( */
281 			fle->f.src_mask = 32;
282 
283 		RTFREE(ro.ro_rt);
284 	}
285 
286 	/* Push new flow entry into flow cache */
287 	mtx_lock(&priv->work_mtx);
288 	LIST_INSERT_HEAD(&(h[slot].head), fle, fle_hash);
289 	TAILQ_INSERT_TAIL(&priv->work_queue, fle, fle_work);
290 	mtx_unlock(&priv->work_mtx);
291 
292 	return (0);
293 }
294 
295 static __inline int
296 make_flow_rec(struct mbuf **m, int *plen, struct flow_rec *r,
297 	uint8_t *tcp_flags, u_int16_t i_ifx)
298 {
299 	register struct ip *ip;
300 	int hlen;
301 	int error = 0;
302 
303 	ip = mtod(*m, struct ip*);
304 
305 	/* check version */
306 	if (ip->ip_v != IPVERSION)
307 		return (EINVAL);
308 
309 	/* verify min header length */
310 	hlen = ip->ip_hl << 2;
311 
312 	if (hlen < sizeof(struct ip))
313 		return (EINVAL);
314 
315 	r->r_src = ip->ip_src;
316 	r->r_dst = ip->ip_dst;
317 
318 	/* save packet length */
319 	*plen = ntohs(ip->ip_len);
320 
321 	r->r_ip_p = ip->ip_p;
322 	r->r_tos = ip->ip_tos;
323 
324 	/* Configured in_ifx overrides mbuf's */
325 	if (i_ifx == 0) {
326 		if ((*m)->m_pkthdr.rcvif)
327 			r->r_i_ifx = (*m)->m_pkthdr.rcvif->if_index;
328 	} else
329 		r->r_i_ifx = i_ifx;
330 
331 	/*
332 	 * XXX NOTE: only first fragment of fragmented TCP, UDP and
333 	 * ICMP packet will be recorded with proper s_port and d_port.
334 	 * Following fragments will be recorded simply as IP packet with
335 	 * ip_proto = ip->ip_p and s_port, d_port set to zero.
336 	 * I know, it looks like bug. But I don't want to re-implement
337 	 * ip packet assebmling here. Anyway, (in)famous trafd works this way -
338 	 * and nobody complains yet :)
339 	 */
340 	if(ip->ip_off & htons(IP_OFFMASK))
341 		return (0);
342 
343 	/* skip IP header */
344 	m_adj(*m, hlen);
345 
346 	switch(r->r_ip_p) {
347 	case IPPROTO_TCP:
348 	{
349 		register struct tcphdr *tcp;
350 
351 		/* verify that packet is not truncated */
352 		if (CHECK_MLEN(*m, sizeof(struct tcphdr)))
353 			ERROUT(EINVAL);
354 
355 		if (CHECK_PULLUP(*m, sizeof(struct tcphdr)))
356 			ERROUT(ENOBUFS);
357 
358 		tcp = mtod(*m, struct tcphdr*);
359 		r->r_sport = tcp->th_sport;
360 		r->r_dport = tcp->th_dport;
361 		*tcp_flags = tcp->th_flags;
362 		break;
363 	}
364 	case IPPROTO_UDP:
365 		/* verify that packet is not truncated */
366 		if (CHECK_MLEN(*m, sizeof(struct udphdr)))
367 			ERROUT(EINVAL);
368 
369 		if (CHECK_PULLUP(*m, sizeof(struct udphdr)))
370 			ERROUT(ENOBUFS);
371 
372 		r->r_ports = *(mtod(*m, uint32_t *));
373 		break;
374 	}
375 
376 done:
377 	return (error);
378 }
379 
380 /*
381  * Non-static functions called from ng_netflow.c
382  */
383 
384 /* Allocate memory and set up flow cache */
385 int
386 ng_netflow_cache_init(priv_p priv)
387 {
388 	struct flow_entry *fle;
389 	int i;
390 
391 	/* allocate cache */
392 	MALLOC(priv->cache, struct flow_entry *,
393 	    CACHESIZE * sizeof(struct flow_entry),
394 	    M_NETFLOW, M_WAITOK | M_ZERO);
395 
396 	if (priv->cache == NULL)
397 		return (ENOMEM);
398 
399 	/* allocate hash */
400 	MALLOC(priv->hash, struct flow_hash_entry *,
401 	    NBUCKETS * sizeof(struct flow_hash_entry),
402 	    M_NETFLOW, M_WAITOK | M_ZERO);
403 
404 	if (priv->hash == NULL) {
405 		FREE(priv->cache, M_NETFLOW);
406 		return (ENOMEM);
407 	}
408 
409 	TAILQ_INIT(&priv->work_queue);
410 	SLIST_INIT(&priv->free_list);
411 	SLIST_INIT(&priv->expire_list);
412 
413 	mtx_init(&priv->work_mtx, "ng_netflow cache mutex", NULL, MTX_DEF);
414 	mtx_init(&priv->free_mtx, "ng_netflow free mutex", NULL, MTX_DEF);
415 	mtx_init(&priv->expire_mtx, "ng_netflow expire mutex", NULL, MTX_DEF);
416 
417 	/* build free list */
418 	for (i = 0, fle = priv->cache; i < CACHESIZE; i++, fle++)
419 		SLIST_INSERT_HEAD(&priv->free_list, fle, fle_free);
420 
421 	priv->info.nfinfo_free = CACHESIZE;
422 
423 	return (0);
424 }
425 
426 /* Free all flow cache memory. Called from node close method. */
427 void
428 ng_netflow_cache_flush(priv_p priv)
429 {
430 	register struct flow_entry	*fle;
431 	int i;
432 
433 	/*
434 	 * We are going to free probably billable data.
435 	 * Expire everything before freeing it.
436 	 * No locking is required since callout is already drained.
437 	 */
438 
439 	for (i = 0, fle = priv->cache; i < CACHESIZE; i++, fle++)
440 		if (!ISFREE(fle))
441 			/* ignore errors now */
442 			(void )export_add(priv, fle);
443 
444 	mtx_destroy(&priv->work_mtx);
445 	mtx_destroy(&priv->free_mtx);
446 	mtx_destroy(&priv->expire_mtx);
447 
448 	/* free hash memory */
449 	if (priv->hash)
450 		FREE(priv->hash, M_NETFLOW);
451 
452 	/* free flow cache */
453 	if (priv->cache)
454 		FREE(priv->cache, M_NETFLOW);
455 
456 }
457 
458 /* Insert packet from &m into flow cache. */
459 int
460 ng_netflow_flow_add(priv_p priv, struct mbuf **m, iface_p iface)
461 {
462 	struct flow_hash_entry		*h = priv->hash;
463 	register struct flow_entry	*fle;
464 	struct flow_rec		r;
465 	int			plen;
466 	int			error = 0;
467 	uint32_t		slot;
468 	uint8_t			tcp_flags = 0;
469 
470 	/* Try to fill *rec */
471 	bzero(&r, sizeof(r));
472 	if ((error = make_flow_rec(m, &plen, &r, &tcp_flags,
473 	    iface->info.ifinfo_index)))
474 		return (error);
475 
476 	slot = ip_hash(&r);
477 
478 	mtx_lock(&priv->work_mtx);
479 
480 	/* Update node statistics. */
481 	priv->info.nfinfo_packets ++;
482 	priv->info.nfinfo_bytes += plen;
483 
484 	fle = hash_lookup(h, slot, &r); /* New flow entry or existent? */
485 
486 	if (fle) {	/* an existent entry */
487 
488 		TAILQ_REMOVE(&priv->work_queue, fle, fle_work);
489 
490 		fle->f.bytes += plen;
491 		fle->f.packets ++;
492 		fle->f.tcp_flags |= tcp_flags;
493 		fle->f.last = time_uptime;
494 
495 		/*
496 		 * We have the following reasons to expire flow in active way:
497 		 * - it hit active timeout
498 		 * - a TCP connection closed
499 		 * - it is going to overflow counter
500 		 */
501 		if (tcp_flags & TH_FIN || tcp_flags & TH_RST || AGED(fle) ||
502 		    (fle->f.bytes >= (UINT_MAX - IF_MAXMTU)) )
503 			expire_flow(priv, fle);
504 		else
505 			TAILQ_INSERT_TAIL(&priv->work_queue, fle, fle_work);
506 
507 		mtx_unlock(&priv->work_mtx);
508 
509 	} else {	/* a new flow entry */
510 
511 		mtx_unlock(&priv->work_mtx);
512 		return hash_insert(priv, slot, &r, plen);
513 
514 	}
515 
516 	mtx_assert(&priv->work_mtx, MA_NOTOWNED);
517 	mtx_assert(&priv->expire_mtx, MA_NOTOWNED);
518 	mtx_assert(&priv->free_mtx, MA_NOTOWNED);
519 
520 	return (0);
521 }
522 
523 /*
524  * Return records from cache. netgraph(4) guarantees us that we
525  * are locked against ng_netflow_rcvdata(). However we can
526  * work with ng_netflow_expire() in parrallel. XXX: Is it dangerous?
527  *
528  * TODO: matching particular IP should be done in kernel, here.
529  */
530 int
531 ng_netflow_flow_show(priv_p priv, uint32_t last, struct ng_mesg *resp)
532 {
533 	struct flow_entry *fle;
534 	struct ngnf_flows *data;
535 
536 	data = (struct ngnf_flows *)resp->data;
537 	data->last = 0;
538 	data->nentries = 0;
539 
540 	/* Check if this is a first run */
541 	if (last == 0)
542 		fle = priv->cache;
543 	else {
544 		if (last > CACHESIZE-1)
545 			return (EINVAL);
546 		fle = priv->cache + last;
547 	}
548 
549 	/*
550 	 * We will transfer not more than NREC_AT_ONCE. More data
551 	 * will come in next message.
552 	 * We send current stop point to userland, and userland should return
553 	 * it back to us.
554 	 */
555 	for (; last < CACHESIZE; fle++, last++) {
556 		if (ISFREE(fle))
557 			continue;
558 		bcopy(&fle->f, &(data->entries[data->nentries]),
559 		    sizeof(fle->f));
560 		data->nentries ++;
561 		if (data->nentries == NREC_AT_ONCE) {
562 			if (++last < CACHESIZE)
563 				data->last = (++fle - priv->cache);
564 			return (0);
565 		}
566      	}
567 
568 	return (0);
569 }
570 
571 /* We have full datagram in privdata. Send it to export hook. */
572 static int
573 export_send(priv_p priv)
574 {
575 	struct netflow_v5_header *header = &priv->dgram.header;
576 	struct timespec ts;
577 	struct mbuf *m;
578 	int error = 0;
579 	int mlen;
580 
581 	header->sys_uptime = htonl(time_uptime);
582 
583 	getnanotime(&ts);
584 	header->unix_secs  = htonl(ts.tv_sec);
585 	header->unix_nsecs = htonl(ts.tv_nsec);
586 
587 	/* Flow sequence contains number of first record */
588 	header->flow_seq = htonl(priv->flow_seq - header->count);
589 
590 	mlen = sizeof(struct netflow_v5_header) +
591 	    sizeof(struct netflow_v5_record) * header->count;
592 
593 	header->count = htons(header->count);
594 	if ((m = m_devget((caddr_t)header, mlen, 0, NULL, NULL)) == NULL) {
595 		log(LOG_CRIT, "ng_netflow: m_devget() failed, losing export "
596 		    "dgram\n");
597 		header->count = 0;
598 		return(ENOBUFS);
599 	}
600 
601 	header->count = 0;
602 
603 	/* Giant is required in sosend() at this moment. */
604 	NET_LOCK_GIANT();
605 	NG_SEND_DATA_ONLY(error, priv->export, m);
606 	NET_UNLOCK_GIANT();
607 
608 	if (error)
609 		NG_FREE_M(m);
610 
611 	return (error);
612 }
613 
614 
615 /* Create export datagram. */
616 static int
617 export_add(priv_p priv, struct flow_entry *fle)
618 {
619 	struct netflow_v5_header *header = &priv->dgram.header;
620 	struct netflow_v5_record *rec;
621 
622 	if (header->count == 0 ) {	/* first record */
623 		rec = &priv->dgram.r[0];
624 		header->count = 1;
625 	} else {			/* continue filling datagram */
626 		rec = &priv->dgram.r[header->count];
627 		header->count ++;
628 	}
629 
630 	/* Fill in export record */
631 	rec->src_addr = fle->f.r.r_src.s_addr;
632 	rec->dst_addr = fle->f.r.r_dst.s_addr;
633 	rec->next_hop = fle->f.next_hop.s_addr;
634 	rec->i_ifx    = htons(fle->f.fle_i_ifx);
635 	rec->o_ifx    = htons(fle->f.fle_o_ifx);
636 	rec->packets  = htonl(fle->f.packets);
637 	rec->octets   = htonl(fle->f.bytes);
638 	rec->first    = htonl(fle->f.first);
639 	rec->last     = htonl(fle->f.last);
640 	rec->s_port   = fle->f.r.r_sport;
641 	rec->d_port   = fle->f.r.r_dport;
642 	rec->flags    = fle->f.tcp_flags;
643 	rec->prot     = fle->f.r.r_ip_p;
644 	rec->tos      = fle->f.r.r_tos;
645 	rec->dst_mask = fle->f.dst_mask;
646 	rec->src_mask = fle->f.src_mask;
647 
648 	priv->flow_seq++;
649 
650 	if (header->count == NETFLOW_V5_MAX_RECORDS) /* end of datagram */
651 		return export_send(priv);
652 
653 	return (0);
654 }
655 
656 /* Periodic flow expiry run. */
657 void
658 ng_netflow_expire(void *arg)
659 {
660 	register struct flow_entry	*fle, *fle1;
661 	priv_p priv = (priv_p )arg;
662 	uint32_t used;
663 	int error = 0;
664 
665 	/* First pack actively expired entries */
666 	mtx_lock(&priv->expire_mtx);
667 	while (!SLIST_EMPTY(&(priv->expire_list))) {
668 		fle = SLIST_FIRST(&(priv->expire_list));
669 		SLIST_REMOVE_HEAD(&(priv->expire_list), fle_free);
670 		mtx_unlock(&priv->expire_mtx);
671 
672 		/*
673 		 * While we have dropped the lock, expire_flow() may
674 		 * insert another flow into top of the list.
675 		 * This is not harmful for us, since we have already
676 		 * detached our own.
677 		 */
678 
679 		if ((error = export_add(priv, fle)) != 0)
680 			log(LOG_CRIT, "ng_netflow: export_add() failed: %u\n",
681 			    error);
682 		(void )free_flow(priv, fle);
683 
684 		mtx_lock(&priv->expire_mtx);
685 	}
686 	mtx_unlock(&priv->expire_mtx);
687 
688 	NGNF_GETUSED(priv, used);
689 	mtx_lock(&priv->work_mtx);
690 	TAILQ_FOREACH_SAFE(fle, &(priv->work_queue), fle_work, fle1) {
691 		/*
692 		 * When cache size has not reached CACHELOWAT yet, we keep
693 		 * both inactive and active flows in cache. Doing this, we
694 		 * reduce number of exports, since many inactive flows may
695 		 * wake up and continue their life. However, we make an
696 		 * exclusion for scans. It is very rare situation that
697 		 * inactive 1-packet flow will wake up.
698 		 * When cache has reached CACHELOWAT, we expire all inactive
699 		 * flows, until cache gets to a sane size.
700 		 */
701 		if (used <= CACHELOWAT && !INACTIVE(fle))
702 			goto finish;
703 
704 		if ((INACTIVE(fle) && (SMALL(fle) || (used > CACHELOWAT))) ||
705 		    AGED(fle)) {
706 
707 			/* Detach flow entry from cache */
708 			LIST_REMOVE(fle, fle_hash);
709 			TAILQ_REMOVE(&priv->work_queue, fle, fle_work);
710 
711 			/*
712 			 * While we are sending to collector, unlock cache.
713 			 * XXX: it can happen, however with a small probability,
714 			 * that item, we are holding now, can be moved to the
715 			 * top of flow cache by node thread. In this case our
716 			 * expire thread stops checking. Since this is not
717 			 * fatal we will just ignore it now.
718 			 */
719 			mtx_unlock(&priv->work_mtx);
720 
721 			if ((error = export_add(priv, fle)) != 0)
722 				log(LOG_CRIT, "ng_netflow: export_add() "
723 				    "failed: %u\n", error);
724 
725 			used = free_flow(priv, fle);
726 
727 			mtx_lock(&priv->work_mtx);
728 		}
729      	}
730 
731 finish:
732 	mtx_unlock(&priv->work_mtx);
733 
734 	mtx_assert(&priv->expire_mtx, MA_NOTOWNED);
735 	mtx_assert(&priv->free_mtx, MA_NOTOWNED);
736 
737 	/* schedule next expire */
738 	callout_reset(&priv->exp_callout, (1*hz), &ng_netflow_expire,
739 	    (void *)priv);
740 
741 }
742