xref: /freebsd/sys/netgraph/netflow/netflow.c (revision cc68614da8232d8baaca0ae0d0dd8f890f06623e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010-2011 Alexander V. Chernikov <melifaro@ipfw.ru>
5  * Copyright (c) 2004-2005 Gleb Smirnoff <glebius@FreeBSD.org>
6  * Copyright (c) 2001-2003 Roman V. Palagin <romanp@unshadow.net>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $SourceForge: netflow.c,v 1.41 2004/09/05 11:41:10 glebius Exp $
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include "opt_inet.h"
37 #include "opt_inet6.h"
38 #include "opt_route.h"
39 #include <sys/param.h>
40 #include <sys/bitstring.h>
41 #include <sys/systm.h>
42 #include <sys/counter.h>
43 #include <sys/kernel.h>
44 #include <sys/ktr.h>
45 #include <sys/limits.h>
46 #include <sys/mbuf.h>
47 #include <sys/syslog.h>
48 #include <sys/socket.h>
49 #include <vm/uma.h>
50 
51 #include <net/if.h>
52 #include <net/if_dl.h>
53 #include <net/if_var.h>
54 #include <net/route.h>
55 #include <net/route/nhop.h>
56 #include <net/route/route_ctl.h>
57 #include <net/ethernet.h>
58 #include <netinet/in.h>
59 #include <netinet/in_fib.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/ip.h>
62 #include <netinet/ip6.h>
63 #include <netinet/tcp.h>
64 #include <netinet/udp.h>
65 
66 #include <netinet6/in6_fib.h>
67 
68 #include <netgraph/ng_message.h>
69 #include <netgraph/netgraph.h>
70 
71 #include <netgraph/netflow/netflow.h>
72 #include <netgraph/netflow/netflow_v9.h>
73 #include <netgraph/netflow/ng_netflow.h>
74 
75 #define	NBUCKETS	(65536)		/* must be power of 2 */
76 
77 /* This hash is for TCP or UDP packets. */
78 #define FULL_HASH(addr1, addr2, port1, port2)	\
79 	(((addr1 ^ (addr1 >> 16) ^ 		\
80 	htons(addr2 ^ (addr2 >> 16))) ^ 	\
81 	port1 ^ htons(port2)) &			\
82 	(NBUCKETS - 1))
83 
84 /* This hash is for all other IP packets. */
85 #define ADDR_HASH(addr1, addr2)			\
86 	((addr1 ^ (addr1 >> 16) ^ 		\
87 	htons(addr2 ^ (addr2 >> 16))) &		\
88 	(NBUCKETS - 1))
89 
90 /* Macros to shorten logical constructions */
91 /* XXX: priv must exist in namespace */
92 #define	INACTIVE(fle)	(time_uptime - fle->f.last > priv->nfinfo_inact_t)
93 #define	AGED(fle)	(time_uptime - fle->f.first > priv->nfinfo_act_t)
94 #define	ISFREE(fle)	(fle->f.packets == 0)
95 
96 /*
97  * 4 is a magical number: statistically number of 4-packet flows is
98  * bigger than 5,6,7...-packet flows by an order of magnitude. Most UDP/ICMP
99  * scans are 1 packet (~ 90% of flow cache). TCP scans are 2-packet in case
100  * of reachable host and 4-packet otherwise.
101  */
102 #define	SMALL(fle)	(fle->f.packets <= 4)
103 
104 MALLOC_DEFINE(M_NETFLOW_HASH, "netflow_hash", "NetFlow hash");
105 
106 static int export_add(item_p, struct flow_entry *);
107 static int export_send(priv_p, fib_export_p, item_p, int);
108 
109 #ifdef INET
110 static int hash_insert(priv_p, struct flow_hash_entry *, struct flow_rec *,
111     int, uint8_t, uint8_t);
112 #endif
113 #ifdef INET6
114 static int hash6_insert(priv_p, struct flow_hash_entry *, struct flow6_rec *,
115     int, uint8_t, uint8_t);
116 #endif
117 
118 static void expire_flow(priv_p, fib_export_p, struct flow_entry *, int);
119 
120 #ifdef INET
121 /*
122  * Generate hash for a given flow record.
123  *
124  * FIB is not used here, because:
125  * most VRFS will carry public IPv4 addresses which are unique even
126  * without FIB private addresses can overlap, but this is worked out
127  * via flow_rec bcmp() containing fib id. In IPv6 world addresses are
128  * all globally unique (it's not fully true, there is FC00::/7 for example,
129  * but chances of address overlap are MUCH smaller)
130  */
131 static inline uint32_t
132 ip_hash(struct flow_rec *r)
133 {
134 
135 	switch (r->r_ip_p) {
136 	case IPPROTO_TCP:
137 	case IPPROTO_UDP:
138 		return FULL_HASH(r->r_src.s_addr, r->r_dst.s_addr,
139 		    r->r_sport, r->r_dport);
140 	default:
141 		return ADDR_HASH(r->r_src.s_addr, r->r_dst.s_addr);
142 	}
143 }
144 #endif
145 
146 #ifdef INET6
147 /* Generate hash for a given flow6 record. Use lower 4 octets from v6 addresses */
148 static inline uint32_t
149 ip6_hash(struct flow6_rec *r)
150 {
151 
152 	switch (r->r_ip_p) {
153 	case IPPROTO_TCP:
154 	case IPPROTO_UDP:
155 		return FULL_HASH(r->src.r_src6.__u6_addr.__u6_addr32[3],
156 		    r->dst.r_dst6.__u6_addr.__u6_addr32[3], r->r_sport,
157 		    r->r_dport);
158 	default:
159 		return ADDR_HASH(r->src.r_src6.__u6_addr.__u6_addr32[3],
160 		    r->dst.r_dst6.__u6_addr.__u6_addr32[3]);
161  	}
162 }
163 
164 #endif
165 
166 /*
167  * Detach export datagram from priv, if there is any.
168  * If there is no, allocate a new one.
169  */
170 static item_p
171 get_export_dgram(priv_p priv, fib_export_p fe)
172 {
173 	item_p	item = NULL;
174 
175 	mtx_lock(&fe->export_mtx);
176 	if (fe->exp.item != NULL) {
177 		item = fe->exp.item;
178 		fe->exp.item = NULL;
179 	}
180 	mtx_unlock(&fe->export_mtx);
181 
182 	if (item == NULL) {
183 		struct netflow_v5_export_dgram *dgram;
184 		struct mbuf *m;
185 
186 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
187 		if (m == NULL)
188 			return (NULL);
189 		item = ng_package_data(m, NG_NOFLAGS);
190 		if (item == NULL)
191 			return (NULL);
192 		dgram = mtod(m, struct netflow_v5_export_dgram *);
193 		dgram->header.count = 0;
194 		dgram->header.version = htons(NETFLOW_V5);
195 		dgram->header.pad = 0;
196 	}
197 
198 	return (item);
199 }
200 
201 /*
202  * Re-attach incomplete datagram back to priv.
203  * If there is already another one, then send incomplete. */
204 static void
205 return_export_dgram(priv_p priv, fib_export_p fe, item_p item, int flags)
206 {
207 
208 	/*
209 	 * It may happen on SMP, that some thread has already
210 	 * put its item there, in this case we bail out and
211 	 * send what we have to collector.
212 	 */
213 	mtx_lock(&fe->export_mtx);
214 	if (fe->exp.item == NULL) {
215 		fe->exp.item = item;
216 		mtx_unlock(&fe->export_mtx);
217 	} else {
218 		mtx_unlock(&fe->export_mtx);
219 		export_send(priv, fe, item, flags);
220 	}
221 }
222 
223 /*
224  * The flow is over. Call export_add() and free it. If datagram is
225  * full, then call export_send().
226  */
227 static void
228 expire_flow(priv_p priv, fib_export_p fe, struct flow_entry *fle, int flags)
229 {
230 	struct netflow_export_item exp;
231 	uint16_t version = fle->f.version;
232 
233 	if ((priv->export != NULL) && (version == IPVERSION)) {
234 		exp.item = get_export_dgram(priv, fe);
235 		if (exp.item == NULL) {
236 			priv->nfinfo_export_failed++;
237 			if (priv->export9 != NULL)
238 				priv->nfinfo_export9_failed++;
239 			/* fle definitely contains IPv4 flow. */
240 			uma_zfree_arg(priv->zone, fle, priv);
241 			return;
242 		}
243 
244 		if (export_add(exp.item, fle) > 0)
245 			export_send(priv, fe, exp.item, flags);
246 		else
247 			return_export_dgram(priv, fe, exp.item, NG_QUEUE);
248 	}
249 
250 	if (priv->export9 != NULL) {
251 		exp.item9 = get_export9_dgram(priv, fe, &exp.item9_opt);
252 		if (exp.item9 == NULL) {
253 			priv->nfinfo_export9_failed++;
254 			if (version == IPVERSION)
255 				uma_zfree_arg(priv->zone, fle, priv);
256 #ifdef INET6
257 			else if (version == IP6VERSION)
258 				uma_zfree_arg(priv->zone6, fle, priv);
259 #endif
260 			else
261 				panic("ng_netflow: Unknown IP proto: %d",
262 				    version);
263 			return;
264 		}
265 
266 		if (export9_add(exp.item9, exp.item9_opt, fle) > 0)
267 			export9_send(priv, fe, exp.item9, exp.item9_opt, flags);
268 		else
269 			return_export9_dgram(priv, fe, exp.item9,
270 			    exp.item9_opt, NG_QUEUE);
271 	}
272 
273 	if (version == IPVERSION)
274 		uma_zfree_arg(priv->zone, fle, priv);
275 #ifdef INET6
276 	else if (version == IP6VERSION)
277 		uma_zfree_arg(priv->zone6, fle, priv);
278 #endif
279 }
280 
281 /* Get a snapshot of node statistics */
282 void
283 ng_netflow_copyinfo(priv_p priv, struct ng_netflow_info *i)
284 {
285 
286 	i->nfinfo_bytes = counter_u64_fetch(priv->nfinfo_bytes);
287 	i->nfinfo_packets = counter_u64_fetch(priv->nfinfo_packets);
288 	i->nfinfo_bytes6 = counter_u64_fetch(priv->nfinfo_bytes6);
289 	i->nfinfo_packets6 = counter_u64_fetch(priv->nfinfo_packets6);
290 	i->nfinfo_sbytes = counter_u64_fetch(priv->nfinfo_sbytes);
291 	i->nfinfo_spackets = counter_u64_fetch(priv->nfinfo_spackets);
292 	i->nfinfo_sbytes6 = counter_u64_fetch(priv->nfinfo_sbytes6);
293 	i->nfinfo_spackets6 = counter_u64_fetch(priv->nfinfo_spackets6);
294 	i->nfinfo_act_exp = counter_u64_fetch(priv->nfinfo_act_exp);
295 	i->nfinfo_inact_exp = counter_u64_fetch(priv->nfinfo_inact_exp);
296 
297 	i->nfinfo_used = uma_zone_get_cur(priv->zone);
298 #ifdef INET6
299 	i->nfinfo_used6 = uma_zone_get_cur(priv->zone6);
300 #endif
301 
302 	i->nfinfo_alloc_failed = priv->nfinfo_alloc_failed;
303 	i->nfinfo_export_failed = priv->nfinfo_export_failed;
304 	i->nfinfo_export9_failed = priv->nfinfo_export9_failed;
305 	i->nfinfo_realloc_mbuf = priv->nfinfo_realloc_mbuf;
306 	i->nfinfo_alloc_fibs = priv->nfinfo_alloc_fibs;
307 	i->nfinfo_inact_t = priv->nfinfo_inact_t;
308 	i->nfinfo_act_t = priv->nfinfo_act_t;
309 }
310 
311 /*
312  * Insert a record into defined slot.
313  *
314  * First we get for us a free flow entry, then fill in all
315  * possible fields in it.
316  *
317  * TODO: consider dropping hash mutex while filling in datagram,
318  * as this was done in previous version. Need to test & profile
319  * to be sure.
320  */
321 #ifdef INET
322 static int
323 hash_insert(priv_p priv, struct flow_hash_entry *hsh, struct flow_rec *r,
324 	int plen, uint8_t flags, uint8_t tcp_flags)
325 {
326 	struct flow_entry *fle;
327 
328 	mtx_assert(&hsh->mtx, MA_OWNED);
329 
330 	fle = uma_zalloc_arg(priv->zone, priv, M_NOWAIT);
331 	if (fle == NULL) {
332 		priv->nfinfo_alloc_failed++;
333 		return (ENOMEM);
334 	}
335 
336 	/*
337 	 * Now fle is totally ours. It is detached from all lists,
338 	 * we can safely edit it.
339 	 */
340 	fle->f.version = IPVERSION;
341 	bcopy(r, &fle->f.r, sizeof(struct flow_rec));
342 	fle->f.bytes = plen;
343 	fle->f.packets = 1;
344 	fle->f.tcp_flags = tcp_flags;
345 
346 	fle->f.first = fle->f.last = time_uptime;
347 
348 	/*
349 	 * First we do route table lookup on destination address. So we can
350 	 * fill in out_ifx, dst_mask, nexthop, and dst_as in future releases.
351 	 */
352 	if ((flags & NG_NETFLOW_CONF_NODSTLOOKUP) == 0) {
353 		struct rtentry *rt;
354 		struct route_nhop_data rnd;
355 
356 		rt = fib4_lookup_rt(r->fib, fle->f.r.r_dst, 0, NHR_NONE, &rnd);
357 		if (rt != NULL) {
358 			struct in_addr addr;
359 			uint32_t scopeid;
360 			struct nhop_object *nh = nhop_select_func(rnd.rnd_nhop, 0);
361 			int plen;
362 
363 			rt_get_inet_prefix_plen(rt, &addr, &plen, &scopeid);
364 			fle->f.fle_o_ifx = nh->nh_ifp->if_index;
365 			if (nh->gw_sa.sa_family == AF_INET)
366 				fle->f.next_hop = nh->gw4_sa.sin_addr;
367 			/*
368 			 * XXX we're leaving an empty gateway here for
369 			 * IPv6 nexthops.
370 			 */
371 			fle->f.dst_mask = plen;
372 		}
373 	}
374 
375 	/* Do route lookup on source address, to fill in src_mask. */
376 	if ((flags & NG_NETFLOW_CONF_NOSRCLOOKUP) == 0) {
377 		struct rtentry *rt;
378 		struct route_nhop_data rnd;
379 
380 		rt = fib4_lookup_rt(r->fib, fle->f.r.r_src, 0, NHR_NONE, &rnd);
381 		if (rt != NULL) {
382 			struct in_addr addr;
383 			uint32_t scopeid;
384 			int plen;
385 
386 			rt_get_inet_prefix_plen(rt, &addr, &plen, &scopeid);
387 			fle->f.src_mask = plen;
388 		}
389 	}
390 
391 	/* Push new flow at the and of hash. */
392 	TAILQ_INSERT_TAIL(&hsh->head, fle, fle_hash);
393 
394 	return (0);
395 }
396 #endif
397 
398 #ifdef INET6
399 static int
400 hash6_insert(priv_p priv, struct flow_hash_entry *hsh6, struct flow6_rec *r,
401 	int plen, uint8_t flags, uint8_t tcp_flags)
402 {
403 	struct flow6_entry *fle6;
404 
405 	mtx_assert(&hsh6->mtx, MA_OWNED);
406 
407 	fle6 = uma_zalloc_arg(priv->zone6, priv, M_NOWAIT);
408 	if (fle6 == NULL) {
409 		priv->nfinfo_alloc_failed++;
410 		return (ENOMEM);
411 	}
412 
413 	/*
414 	 * Now fle is totally ours. It is detached from all lists,
415 	 * we can safely edit it.
416 	 */
417 
418 	fle6->f.version = IP6VERSION;
419 	bcopy(r, &fle6->f.r, sizeof(struct flow6_rec));
420 	fle6->f.bytes = plen;
421 	fle6->f.packets = 1;
422 	fle6->f.tcp_flags = tcp_flags;
423 
424 	fle6->f.first = fle6->f.last = time_uptime;
425 
426 	/*
427 	 * First we do route table lookup on destination address. So we can
428 	 * fill in out_ifx, dst_mask, nexthop, and dst_as in future releases.
429 	 */
430 	if ((flags & NG_NETFLOW_CONF_NODSTLOOKUP) == 0) {
431 		struct rtentry *rt;
432 		struct route_nhop_data rnd;
433 
434 		rt = fib6_lookup_rt(r->fib, &fle6->f.r.dst.r_dst6, 0, NHR_NONE, &rnd);
435 		if (rt != NULL) {
436 			struct in6_addr addr;
437 			uint32_t scopeid;
438 			struct nhop_object *nh = nhop_select_func(rnd.rnd_nhop, 0);
439 			int plen;
440 
441 			rt_get_inet6_prefix_plen(rt, &addr, &plen, &scopeid);
442 			fle6->f.fle_o_ifx = nh->nh_ifp->if_index;
443 			if (nh->gw_sa.sa_family == AF_INET6)
444 				fle6->f.n.next_hop6 = nh->gw6_sa.sin6_addr;
445 			fle6->f.dst_mask = plen;
446 		}
447 	}
448 
449 	if ((flags & NG_NETFLOW_CONF_NOSRCLOOKUP) == 0) {
450 		/* Do route lookup on source address, to fill in src_mask. */
451 		struct rtentry *rt;
452 		struct route_nhop_data rnd;
453 
454 		rt = fib6_lookup_rt(r->fib, &fle6->f.r.src.r_src6, 0, NHR_NONE, &rnd);
455 		if (rt != NULL) {
456 			struct in6_addr addr;
457 			uint32_t scopeid;
458 			int plen;
459 
460 			rt_get_inet6_prefix_plen(rt, &addr, &plen, &scopeid);
461 			fle6->f.src_mask = plen;
462 		}
463 	}
464 
465 	/* Push new flow at the and of hash. */
466 	TAILQ_INSERT_TAIL(&hsh6->head, (struct flow_entry *)fle6, fle_hash);
467 
468 	return (0);
469 }
470 #endif
471 
472 /*
473  * Non-static functions called from ng_netflow.c
474  */
475 
476 /* Allocate memory and set up flow cache */
477 void
478 ng_netflow_cache_init(priv_p priv)
479 {
480 	struct flow_hash_entry *hsh;
481 	int i;
482 
483 	/* Initialize cache UMA zone. */
484 	priv->zone = uma_zcreate("NetFlow IPv4 cache",
485 	    sizeof(struct flow_entry), NULL, NULL, NULL, NULL,
486 	    UMA_ALIGN_CACHE, 0);
487 	uma_zone_set_max(priv->zone, CACHESIZE);
488 #ifdef INET6
489 	priv->zone6 = uma_zcreate("NetFlow IPv6 cache",
490 	    sizeof(struct flow6_entry), NULL, NULL, NULL, NULL,
491 	    UMA_ALIGN_CACHE, 0);
492 	uma_zone_set_max(priv->zone6, CACHESIZE);
493 #endif
494 
495 	/* Allocate hash. */
496 	priv->hash = malloc(NBUCKETS * sizeof(struct flow_hash_entry),
497 	    M_NETFLOW_HASH, M_WAITOK | M_ZERO);
498 
499 	/* Initialize hash. */
500 	for (i = 0, hsh = priv->hash; i < NBUCKETS; i++, hsh++) {
501 		mtx_init(&hsh->mtx, "hash mutex", NULL, MTX_DEF);
502 		TAILQ_INIT(&hsh->head);
503 	}
504 
505 #ifdef INET6
506 	/* Allocate hash. */
507 	priv->hash6 = malloc(NBUCKETS * sizeof(struct flow_hash_entry),
508 	    M_NETFLOW_HASH, M_WAITOK | M_ZERO);
509 
510 	/* Initialize hash. */
511 	for (i = 0, hsh = priv->hash6; i < NBUCKETS; i++, hsh++) {
512 		mtx_init(&hsh->mtx, "hash mutex", NULL, MTX_DEF);
513 		TAILQ_INIT(&hsh->head);
514 	}
515 #endif
516 
517 	priv->nfinfo_bytes = counter_u64_alloc(M_WAITOK);
518 	priv->nfinfo_packets = counter_u64_alloc(M_WAITOK);
519 	priv->nfinfo_bytes6 = counter_u64_alloc(M_WAITOK);
520 	priv->nfinfo_packets6 = counter_u64_alloc(M_WAITOK);
521 	priv->nfinfo_sbytes = counter_u64_alloc(M_WAITOK);
522 	priv->nfinfo_spackets = counter_u64_alloc(M_WAITOK);
523 	priv->nfinfo_sbytes6 = counter_u64_alloc(M_WAITOK);
524 	priv->nfinfo_spackets6 = counter_u64_alloc(M_WAITOK);
525 	priv->nfinfo_act_exp = counter_u64_alloc(M_WAITOK);
526 	priv->nfinfo_inact_exp = counter_u64_alloc(M_WAITOK);
527 
528 	ng_netflow_v9_cache_init(priv);
529 	CTR0(KTR_NET, "ng_netflow startup()");
530 }
531 
532 /* Initialize new FIB table for v5 and v9 */
533 int
534 ng_netflow_fib_init(priv_p priv, int fib)
535 {
536 	fib_export_p	fe = priv_to_fib(priv, fib);
537 
538 	CTR1(KTR_NET, "ng_netflow(): fib init: %d", fib);
539 
540 	if (fe != NULL)
541 		return (0);
542 
543 	if ((fe = malloc(sizeof(struct fib_export), M_NETGRAPH,
544 	    M_NOWAIT | M_ZERO)) == NULL)
545 		return (ENOMEM);
546 
547 	mtx_init(&fe->export_mtx, "export dgram lock", NULL, MTX_DEF);
548 	mtx_init(&fe->export9_mtx, "export9 dgram lock", NULL, MTX_DEF);
549 	fe->fib = fib;
550 	fe->domain_id = fib;
551 
552 	if (atomic_cmpset_ptr((volatile uintptr_t *)&priv->fib_data[fib],
553 	    (uintptr_t)NULL, (uintptr_t)fe) == 0) {
554 		/* FIB already set up by other ISR */
555 		CTR3(KTR_NET, "ng_netflow(): fib init: %d setup %p but got %p",
556 		    fib, fe, priv_to_fib(priv, fib));
557 		mtx_destroy(&fe->export_mtx);
558 		mtx_destroy(&fe->export9_mtx);
559 		free(fe, M_NETGRAPH);
560 	} else {
561 		/* Increase counter for statistics */
562 		CTR3(KTR_NET, "ng_netflow(): fib %d setup to %p (%p)",
563 		    fib, fe, priv_to_fib(priv, fib));
564 		priv->nfinfo_alloc_fibs++;
565 	}
566 
567 	return (0);
568 }
569 
570 /* Free all flow cache memory. Called from node close method. */
571 void
572 ng_netflow_cache_flush(priv_p priv)
573 {
574 	struct flow_entry	*fle, *fle1;
575 	struct flow_hash_entry	*hsh;
576 	struct netflow_export_item exp;
577 	fib_export_p fe;
578 	int i;
579 
580 	bzero(&exp, sizeof(exp));
581 
582 	/*
583 	 * We are going to free probably billable data.
584 	 * Expire everything before freeing it.
585 	 * No locking is required since callout is already drained.
586 	 */
587 	for (hsh = priv->hash, i = 0; i < NBUCKETS; hsh++, i++)
588 		TAILQ_FOREACH_SAFE(fle, &hsh->head, fle_hash, fle1) {
589 			TAILQ_REMOVE(&hsh->head, fle, fle_hash);
590 			fe = priv_to_fib(priv, fle->f.r.fib);
591 			expire_flow(priv, fe, fle, NG_QUEUE);
592 		}
593 #ifdef INET6
594 	for (hsh = priv->hash6, i = 0; i < NBUCKETS; hsh++, i++)
595 		TAILQ_FOREACH_SAFE(fle, &hsh->head, fle_hash, fle1) {
596 			TAILQ_REMOVE(&hsh->head, fle, fle_hash);
597 			fe = priv_to_fib(priv, fle->f.r.fib);
598 			expire_flow(priv, fe, fle, NG_QUEUE);
599 		}
600 #endif
601 
602 	uma_zdestroy(priv->zone);
603 	/* Destroy hash mutexes. */
604 	for (i = 0, hsh = priv->hash; i < NBUCKETS; i++, hsh++)
605 		mtx_destroy(&hsh->mtx);
606 
607 	/* Free hash memory. */
608 	if (priv->hash != NULL)
609 		free(priv->hash, M_NETFLOW_HASH);
610 #ifdef INET6
611 	uma_zdestroy(priv->zone6);
612 	/* Destroy hash mutexes. */
613 	for (i = 0, hsh = priv->hash6; i < NBUCKETS; i++, hsh++)
614 		mtx_destroy(&hsh->mtx);
615 
616 	/* Free hash memory. */
617 	if (priv->hash6 != NULL)
618 		free(priv->hash6, M_NETFLOW_HASH);
619 #endif
620 
621 	for (i = 0; i < priv->maxfibs; i++) {
622 		if ((fe = priv_to_fib(priv, i)) == NULL)
623 			continue;
624 
625 		if (fe->exp.item != NULL)
626 			export_send(priv, fe, fe->exp.item, NG_QUEUE);
627 
628 		if (fe->exp.item9 != NULL)
629 			export9_send(priv, fe, fe->exp.item9,
630 			    fe->exp.item9_opt, NG_QUEUE);
631 
632 		mtx_destroy(&fe->export_mtx);
633 		mtx_destroy(&fe->export9_mtx);
634 		free(fe, M_NETGRAPH);
635 	}
636 
637 	counter_u64_free(priv->nfinfo_bytes);
638 	counter_u64_free(priv->nfinfo_packets);
639 	counter_u64_free(priv->nfinfo_bytes6);
640 	counter_u64_free(priv->nfinfo_packets6);
641 	counter_u64_free(priv->nfinfo_sbytes);
642 	counter_u64_free(priv->nfinfo_spackets);
643 	counter_u64_free(priv->nfinfo_sbytes6);
644 	counter_u64_free(priv->nfinfo_spackets6);
645 	counter_u64_free(priv->nfinfo_act_exp);
646 	counter_u64_free(priv->nfinfo_inact_exp);
647 
648 	ng_netflow_v9_cache_flush(priv);
649 }
650 
651 #ifdef INET
652 /* Insert packet from into flow cache. */
653 int
654 ng_netflow_flow_add(priv_p priv, fib_export_p fe, struct ip *ip,
655     caddr_t upper_ptr, uint8_t upper_proto, uint8_t flags,
656     unsigned int src_if_index)
657 {
658 	struct flow_entry	*fle, *fle1;
659 	struct flow_hash_entry	*hsh;
660 	struct flow_rec		r;
661 	int			hlen, plen;
662 	int			error = 0;
663 	uint16_t		eproto;
664 	uint8_t			tcp_flags = 0;
665 
666 	bzero(&r, sizeof(r));
667 
668 	if (ip->ip_v != IPVERSION)
669 		return (EINVAL);
670 
671 	hlen = ip->ip_hl << 2;
672 	if (hlen < sizeof(struct ip))
673 		return (EINVAL);
674 
675 	eproto = ETHERTYPE_IP;
676 	/* Assume L4 template by default */
677 	r.flow_type = NETFLOW_V9_FLOW_V4_L4;
678 
679 	r.r_src = ip->ip_src;
680 	r.r_dst = ip->ip_dst;
681 	r.fib = fe->fib;
682 
683 	plen = ntohs(ip->ip_len);
684 
685 	r.r_ip_p = ip->ip_p;
686 	r.r_tos = ip->ip_tos;
687 
688 	r.r_i_ifx = src_if_index;
689 
690 	/*
691 	 * XXX NOTE: only first fragment of fragmented TCP, UDP and
692 	 * ICMP packet will be recorded with proper s_port and d_port.
693 	 * Following fragments will be recorded simply as IP packet with
694 	 * ip_proto = ip->ip_p and s_port, d_port set to zero.
695 	 * I know, it looks like bug. But I don't want to re-implement
696 	 * ip packet assebmling here. Anyway, (in)famous trafd works this way -
697 	 * and nobody complains yet :)
698 	 */
699 	if ((ip->ip_off & htons(IP_OFFMASK)) == 0)
700 		switch(r.r_ip_p) {
701 		case IPPROTO_TCP:
702 		    {
703 			struct tcphdr *tcp;
704 
705 			tcp = (struct tcphdr *)((caddr_t )ip + hlen);
706 			r.r_sport = tcp->th_sport;
707 			r.r_dport = tcp->th_dport;
708 			tcp_flags = tcp->th_flags;
709 			break;
710 		    }
711 		case IPPROTO_UDP:
712 			r.r_ports = *(uint32_t *)((caddr_t )ip + hlen);
713 			break;
714 		}
715 
716 	counter_u64_add(priv->nfinfo_packets, 1);
717 	counter_u64_add(priv->nfinfo_bytes, plen);
718 
719 	/* Find hash slot. */
720 	hsh = &priv->hash[ip_hash(&r)];
721 
722 	mtx_lock(&hsh->mtx);
723 
724 	/*
725 	 * Go through hash and find our entry. If we encounter an
726 	 * entry, that should be expired, purge it. We do a reverse
727 	 * search since most active entries are first, and most
728 	 * searches are done on most active entries.
729 	 */
730 	TAILQ_FOREACH_REVERSE_SAFE(fle, &hsh->head, fhead, fle_hash, fle1) {
731 		if (bcmp(&r, &fle->f.r, sizeof(struct flow_rec)) == 0)
732 			break;
733 		if ((INACTIVE(fle) && SMALL(fle)) || AGED(fle)) {
734 			TAILQ_REMOVE(&hsh->head, fle, fle_hash);
735 			expire_flow(priv, priv_to_fib(priv, fle->f.r.fib),
736 			    fle, NG_QUEUE);
737 			counter_u64_add(priv->nfinfo_act_exp, 1);
738 		}
739 	}
740 
741 	if (fle) {			/* An existent entry. */
742 
743 		fle->f.bytes += plen;
744 		fle->f.packets ++;
745 		fle->f.tcp_flags |= tcp_flags;
746 		fle->f.last = time_uptime;
747 
748 		/*
749 		 * We have the following reasons to expire flow in active way:
750 		 * - it hit active timeout
751 		 * - a TCP connection closed
752 		 * - it is going to overflow counter
753 		 */
754 		if (tcp_flags & TH_FIN || tcp_flags & TH_RST || AGED(fle) ||
755 		    (fle->f.bytes >= (CNTR_MAX - IF_MAXMTU)) ) {
756 			TAILQ_REMOVE(&hsh->head, fle, fle_hash);
757 			expire_flow(priv, priv_to_fib(priv, fle->f.r.fib),
758 			    fle, NG_QUEUE);
759 			counter_u64_add(priv->nfinfo_act_exp, 1);
760 		} else {
761 			/*
762 			 * It is the newest, move it to the tail,
763 			 * if it isn't there already. Next search will
764 			 * locate it quicker.
765 			 */
766 			if (fle != TAILQ_LAST(&hsh->head, fhead)) {
767 				TAILQ_REMOVE(&hsh->head, fle, fle_hash);
768 				TAILQ_INSERT_TAIL(&hsh->head, fle, fle_hash);
769 			}
770 		}
771 	} else				/* A new flow entry. */
772 		error = hash_insert(priv, hsh, &r, plen, flags, tcp_flags);
773 
774 	mtx_unlock(&hsh->mtx);
775 
776 	return (error);
777 }
778 #endif
779 
780 #ifdef INET6
781 /* Insert IPv6 packet from into flow cache. */
782 int
783 ng_netflow_flow6_add(priv_p priv, fib_export_p fe, struct ip6_hdr *ip6,
784     caddr_t upper_ptr, uint8_t upper_proto, uint8_t flags,
785     unsigned int src_if_index)
786 {
787 	struct flow_entry	*fle = NULL, *fle1;
788 	struct flow6_entry	*fle6;
789 	struct flow_hash_entry	*hsh;
790 	struct flow6_rec	r;
791 	int			plen;
792 	int			error = 0;
793 	uint8_t			tcp_flags = 0;
794 
795 	/* check version */
796 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
797 		return (EINVAL);
798 
799 	bzero(&r, sizeof(r));
800 
801 	r.src.r_src6 = ip6->ip6_src;
802 	r.dst.r_dst6 = ip6->ip6_dst;
803 	r.fib = fe->fib;
804 
805 	/* Assume L4 template by default */
806 	r.flow_type = NETFLOW_V9_FLOW_V6_L4;
807 
808 	plen = ntohs(ip6->ip6_plen) + sizeof(struct ip6_hdr);
809 
810 #if 0
811 	/* XXX: set DSCP/CoS value */
812 	r.r_tos = ip->ip_tos;
813 #endif
814 	if ((flags & NG_NETFLOW_IS_FRAG) == 0) {
815 		switch(upper_proto) {
816 		case IPPROTO_TCP:
817 		    {
818 			struct tcphdr *tcp;
819 
820 			tcp = (struct tcphdr *)upper_ptr;
821 			r.r_ports = *(uint32_t *)upper_ptr;
822 			tcp_flags = tcp->th_flags;
823 			break;
824 		    }
825  		case IPPROTO_UDP:
826 		case IPPROTO_SCTP:
827 			r.r_ports = *(uint32_t *)upper_ptr;
828 			break;
829 		}
830 	}
831 
832 	r.r_ip_p = upper_proto;
833 	r.r_i_ifx = src_if_index;
834 
835 	counter_u64_add(priv->nfinfo_packets6, 1);
836 	counter_u64_add(priv->nfinfo_bytes6, plen);
837 
838 	/* Find hash slot. */
839 	hsh = &priv->hash6[ip6_hash(&r)];
840 
841 	mtx_lock(&hsh->mtx);
842 
843 	/*
844 	 * Go through hash and find our entry. If we encounter an
845 	 * entry, that should be expired, purge it. We do a reverse
846 	 * search since most active entries are first, and most
847 	 * searches are done on most active entries.
848 	 */
849 	TAILQ_FOREACH_REVERSE_SAFE(fle, &hsh->head, fhead, fle_hash, fle1) {
850 		if (fle->f.version != IP6VERSION)
851 			continue;
852 		fle6 = (struct flow6_entry *)fle;
853 		if (bcmp(&r, &fle6->f.r, sizeof(struct flow6_rec)) == 0)
854 			break;
855 		if ((INACTIVE(fle6) && SMALL(fle6)) || AGED(fle6)) {
856 			TAILQ_REMOVE(&hsh->head, fle, fle_hash);
857 			expire_flow(priv, priv_to_fib(priv, fle->f.r.fib), fle,
858 			    NG_QUEUE);
859 			counter_u64_add(priv->nfinfo_act_exp, 1);
860 		}
861 	}
862 
863 	if (fle != NULL) {			/* An existent entry. */
864 		fle6 = (struct flow6_entry *)fle;
865 
866 		fle6->f.bytes += plen;
867 		fle6->f.packets ++;
868 		fle6->f.tcp_flags |= tcp_flags;
869 		fle6->f.last = time_uptime;
870 
871 		/*
872 		 * We have the following reasons to expire flow in active way:
873 		 * - it hit active timeout
874 		 * - a TCP connection closed
875 		 * - it is going to overflow counter
876 		 */
877 		if (tcp_flags & TH_FIN || tcp_flags & TH_RST || AGED(fle6) ||
878 		    (fle6->f.bytes >= (CNTR_MAX - IF_MAXMTU)) ) {
879 			TAILQ_REMOVE(&hsh->head, fle, fle_hash);
880 			expire_flow(priv, priv_to_fib(priv, fle->f.r.fib), fle,
881 			    NG_QUEUE);
882 			counter_u64_add(priv->nfinfo_act_exp, 1);
883 		} else {
884 			/*
885 			 * It is the newest, move it to the tail,
886 			 * if it isn't there already. Next search will
887 			 * locate it quicker.
888 			 */
889 			if (fle != TAILQ_LAST(&hsh->head, fhead)) {
890 				TAILQ_REMOVE(&hsh->head, fle, fle_hash);
891 				TAILQ_INSERT_TAIL(&hsh->head, fle, fle_hash);
892 			}
893 		}
894 	} else				/* A new flow entry. */
895 		error = hash6_insert(priv, hsh, &r, plen, flags, tcp_flags);
896 
897 	mtx_unlock(&hsh->mtx);
898 
899 	return (error);
900 }
901 #endif
902 
903 /*
904  * Return records from cache to userland.
905  *
906  * TODO: matching particular IP should be done in kernel, here.
907  */
908 int
909 ng_netflow_flow_show(priv_p priv, struct ngnf_show_header *req,
910 struct ngnf_show_header *resp)
911 {
912 	struct flow_hash_entry	*hsh;
913 	struct flow_entry	*fle;
914 	struct flow_entry_data	*data = (struct flow_entry_data *)(resp + 1);
915 #ifdef INET6
916 	struct flow6_entry_data	*data6 = (struct flow6_entry_data *)(resp + 1);
917 #endif
918 	int	i, max;
919 
920 	i = req->hash_id;
921 	if (i > NBUCKETS-1)
922 		return (EINVAL);
923 
924 #ifdef INET6
925 	if (req->version == 6) {
926 		resp->version = 6;
927 		hsh = priv->hash6 + i;
928 		max = NREC6_AT_ONCE;
929 	} else
930 #endif
931 	if (req->version == 4) {
932 		resp->version = 4;
933 		hsh = priv->hash + i;
934 		max = NREC_AT_ONCE;
935 	} else
936 		return (EINVAL);
937 
938 	/*
939 	 * We will transfer not more than NREC_AT_ONCE. More data
940 	 * will come in next message.
941 	 * We send current hash index and current record number in list
942 	 * to userland, and userland should return it back to us.
943 	 * Then, we will restart with new entry.
944 	 *
945 	 * The resulting cache snapshot can be inaccurate if flow expiration
946 	 * is taking place on hash item between userland data requests for
947 	 * this hash item id.
948 	 */
949 	resp->nentries = 0;
950 	for (; i < NBUCKETS; hsh++, i++) {
951 		int list_id;
952 
953 		if (mtx_trylock(&hsh->mtx) == 0) {
954 			/*
955 			 * Requested hash index is not available,
956 			 * relay decision to skip or re-request data
957 			 * to userland.
958 			 */
959 			resp->hash_id = i;
960 			resp->list_id = 0;
961 			return (0);
962 		}
963 
964 		list_id = 0;
965 		TAILQ_FOREACH(fle, &hsh->head, fle_hash) {
966 			if (hsh->mtx.mtx_lock & MTX_CONTESTED) {
967 				resp->hash_id = i;
968 				resp->list_id = list_id;
969 				mtx_unlock(&hsh->mtx);
970 				return (0);
971 			}
972 
973 			list_id++;
974 			/* Search for particular record in list. */
975 			if (req->list_id > 0) {
976 				if (list_id < req->list_id)
977 					continue;
978 
979 				/* Requested list position found. */
980 				req->list_id = 0;
981 			}
982 #ifdef INET6
983 			if (req->version == 6) {
984 				struct flow6_entry *fle6;
985 
986 				fle6 = (struct flow6_entry *)fle;
987 				bcopy(&fle6->f, data6 + resp->nentries,
988 				    sizeof(fle6->f));
989 			} else
990 #endif
991 				bcopy(&fle->f, data + resp->nentries,
992 				    sizeof(fle->f));
993 			resp->nentries++;
994 			if (resp->nentries == max) {
995 				resp->hash_id = i;
996 				/*
997 				 * If it was the last item in list
998 				 * we simply skip to next hash_id.
999 				 */
1000 				resp->list_id = list_id + 1;
1001 				mtx_unlock(&hsh->mtx);
1002 				return (0);
1003 			}
1004 		}
1005 		mtx_unlock(&hsh->mtx);
1006 	}
1007 
1008 	resp->hash_id = resp->list_id = 0;
1009 
1010 	return (0);
1011 }
1012 
1013 /* We have full datagram in privdata. Send it to export hook. */
1014 static int
1015 export_send(priv_p priv, fib_export_p fe, item_p item, int flags)
1016 {
1017 	struct mbuf *m = NGI_M(item);
1018 	struct netflow_v5_export_dgram *dgram = mtod(m,
1019 					struct netflow_v5_export_dgram *);
1020 	struct netflow_v5_header *header = &dgram->header;
1021 	struct timespec ts;
1022 	int error = 0;
1023 
1024 	/* Fill mbuf header. */
1025 	m->m_len = m->m_pkthdr.len = sizeof(struct netflow_v5_record) *
1026 	   header->count + sizeof(struct netflow_v5_header);
1027 
1028 	/* Fill export header. */
1029 	header->sys_uptime = htonl(MILLIUPTIME(time_uptime));
1030 	getnanotime(&ts);
1031 	header->unix_secs  = htonl(ts.tv_sec);
1032 	header->unix_nsecs = htonl(ts.tv_nsec);
1033 	header->engine_type = 0;
1034 	header->engine_id = fe->domain_id;
1035 	header->pad = 0;
1036 	header->flow_seq = htonl(atomic_fetchadd_32(&fe->flow_seq,
1037 	    header->count));
1038 	header->count = htons(header->count);
1039 
1040 	if (priv->export != NULL)
1041 		NG_FWD_ITEM_HOOK_FLAGS(error, item, priv->export, flags);
1042 	else
1043 		NG_FREE_ITEM(item);
1044 
1045 	return (error);
1046 }
1047 
1048 /* Add export record to dgram. */
1049 static int
1050 export_add(item_p item, struct flow_entry *fle)
1051 {
1052 	struct netflow_v5_export_dgram *dgram = mtod(NGI_M(item),
1053 					struct netflow_v5_export_dgram *);
1054 	struct netflow_v5_header *header = &dgram->header;
1055 	struct netflow_v5_record *rec;
1056 
1057 	rec = &dgram->r[header->count];
1058 	header->count ++;
1059 
1060 	KASSERT(header->count <= NETFLOW_V5_MAX_RECORDS,
1061 	    ("ng_netflow: export too big"));
1062 
1063 	/* Fill in export record. */
1064 	rec->src_addr = fle->f.r.r_src.s_addr;
1065 	rec->dst_addr = fle->f.r.r_dst.s_addr;
1066 	rec->next_hop = fle->f.next_hop.s_addr;
1067 	rec->i_ifx    = htons(fle->f.fle_i_ifx);
1068 	rec->o_ifx    = htons(fle->f.fle_o_ifx);
1069 	rec->packets  = htonl(fle->f.packets);
1070 	rec->octets   = htonl(fle->f.bytes);
1071 	rec->first    = htonl(MILLIUPTIME(fle->f.first));
1072 	rec->last     = htonl(MILLIUPTIME(fle->f.last));
1073 	rec->s_port   = fle->f.r.r_sport;
1074 	rec->d_port   = fle->f.r.r_dport;
1075 	rec->flags    = fle->f.tcp_flags;
1076 	rec->prot     = fle->f.r.r_ip_p;
1077 	rec->tos      = fle->f.r.r_tos;
1078 	rec->dst_mask = fle->f.dst_mask;
1079 	rec->src_mask = fle->f.src_mask;
1080 	rec->pad1     = 0;
1081 	rec->pad2     = 0;
1082 
1083 	/* Not supported fields. */
1084 	rec->src_as = rec->dst_as = 0;
1085 
1086 	if (header->count == NETFLOW_V5_MAX_RECORDS)
1087 		return (1); /* end of datagram */
1088 	else
1089 		return (0);
1090 }
1091 
1092 /* Periodic flow expiry run. */
1093 void
1094 ng_netflow_expire(void *arg)
1095 {
1096 	struct flow_entry	*fle, *fle1;
1097 	struct flow_hash_entry	*hsh;
1098 	priv_p			priv = (priv_p )arg;
1099 	int			used, i;
1100 
1101 	/*
1102 	 * Going through all the cache.
1103 	 */
1104 	used = uma_zone_get_cur(priv->zone);
1105 	for (hsh = priv->hash, i = 0; i < NBUCKETS; hsh++, i++) {
1106 		/*
1107 		 * Skip entries, that are already being worked on.
1108 		 */
1109 		if (mtx_trylock(&hsh->mtx) == 0)
1110 			continue;
1111 
1112 		TAILQ_FOREACH_SAFE(fle, &hsh->head, fle_hash, fle1) {
1113 			/*
1114 			 * Interrupt thread wants this entry!
1115 			 * Quick! Quick! Bail out!
1116 			 */
1117 			if (hsh->mtx.mtx_lock & MTX_CONTESTED)
1118 				break;
1119 
1120 			/*
1121 			 * Don't expire aggressively while hash collision
1122 			 * ratio is predicted small.
1123 			 */
1124 			if (used <= (NBUCKETS*2) && !INACTIVE(fle))
1125 				break;
1126 
1127 			if ((INACTIVE(fle) && (SMALL(fle) ||
1128 			    (used > (NBUCKETS*2)))) || AGED(fle)) {
1129 				TAILQ_REMOVE(&hsh->head, fle, fle_hash);
1130 				expire_flow(priv, priv_to_fib(priv,
1131 				    fle->f.r.fib), fle, NG_NOFLAGS);
1132 				used--;
1133 				counter_u64_add(priv->nfinfo_inact_exp, 1);
1134 			}
1135 		}
1136 		mtx_unlock(&hsh->mtx);
1137 	}
1138 
1139 #ifdef INET6
1140 	used = uma_zone_get_cur(priv->zone6);
1141 	for (hsh = priv->hash6, i = 0; i < NBUCKETS; hsh++, i++) {
1142 		struct flow6_entry	*fle6;
1143 
1144 		/*
1145 		 * Skip entries, that are already being worked on.
1146 		 */
1147 		if (mtx_trylock(&hsh->mtx) == 0)
1148 			continue;
1149 
1150 		TAILQ_FOREACH_SAFE(fle, &hsh->head, fle_hash, fle1) {
1151 			fle6 = (struct flow6_entry *)fle;
1152 			/*
1153 			 * Interrupt thread wants this entry!
1154 			 * Quick! Quick! Bail out!
1155 			 */
1156 			if (hsh->mtx.mtx_lock & MTX_CONTESTED)
1157 				break;
1158 
1159 			/*
1160 			 * Don't expire aggressively while hash collision
1161 			 * ratio is predicted small.
1162 			 */
1163 			if (used <= (NBUCKETS*2) && !INACTIVE(fle6))
1164 				break;
1165 
1166 			if ((INACTIVE(fle6) && (SMALL(fle6) ||
1167 			    (used > (NBUCKETS*2)))) || AGED(fle6)) {
1168 				TAILQ_REMOVE(&hsh->head, fle, fle_hash);
1169 				expire_flow(priv, priv_to_fib(priv,
1170 				    fle->f.r.fib), fle, NG_NOFLAGS);
1171 				used--;
1172 				counter_u64_add(priv->nfinfo_inact_exp, 1);
1173 			}
1174 		}
1175 		mtx_unlock(&hsh->mtx);
1176 	}
1177 #endif
1178 
1179 	/* Schedule next expire. */
1180 	callout_reset(&priv->exp_callout, (1*hz), &ng_netflow_expire,
1181 	    (void *)priv);
1182 }
1183