2004-09-16 20:24:23 +00:00
|
|
|
/*-
|
2017-11-27 15:23:17 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2011-03-02 16:15:11 +00:00
|
|
|
* Copyright (c) 2010-2011 Alexander V. Chernikov <melifaro@ipfw.ru>
|
2005-03-21 15:34:03 +00:00
|
|
|
* Copyright (c) 2004-2005 Gleb Smirnoff <glebius@FreeBSD.org>
|
2004-09-16 20:24:23 +00:00
|
|
|
* Copyright (c) 2001-2003 Roman V. Palagin <romanp@unshadow.net>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $SourceForge: netflow.c,v 1.41 2004/09/05 11:41:10 glebius Exp $
|
|
|
|
*/
|
|
|
|
|
2013-12-29 18:53:45 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2011-03-02 16:15:11 +00:00
|
|
|
#include "opt_inet6.h"
|
|
|
|
#include "opt_route.h"
|
2004-09-16 20:24:23 +00:00
|
|
|
#include <sys/param.h>
|
2016-05-26 18:57:26 +00:00
|
|
|
#include <sys/bitstring.h>
|
2014-01-01 21:48:04 +00:00
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/counter.h>
|
2004-09-16 20:24:23 +00:00
|
|
|
#include <sys/kernel.h>
|
2016-01-09 09:34:39 +00:00
|
|
|
#include <sys/ktr.h>
|
2004-09-16 20:24:23 +00:00
|
|
|
#include <sys/limits.h>
|
|
|
|
#include <sys/mbuf.h>
|
2005-01-20 13:28:39 +00:00
|
|
|
#include <sys/syslog.h>
|
2004-09-16 20:24:23 +00:00
|
|
|
#include <sys/socket.h>
|
2016-02-01 17:41:21 +00:00
|
|
|
#include <vm/uma.h>
|
2005-05-11 11:26:24 +00:00
|
|
|
|
2004-09-16 20:24:23 +00:00
|
|
|
#include <net/if.h>
|
2016-01-14 13:14:12 +00:00
|
|
|
#include <net/if_dl.h>
|
2013-10-26 17:58:36 +00:00
|
|
|
#include <net/if_var.h>
|
2004-09-16 20:24:23 +00:00
|
|
|
#include <net/route.h>
|
2011-03-02 16:15:11 +00:00
|
|
|
#include <net/ethernet.h>
|
2004-09-16 20:24:23 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
2011-03-02 16:15:11 +00:00
|
|
|
#include <netinet/ip6.h>
|
2004-09-16 20:24:23 +00:00
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <netinet/udp.h>
|
|
|
|
|
|
|
|
#include <netgraph/ng_message.h>
|
|
|
|
#include <netgraph/netgraph.h>
|
|
|
|
|
|
|
|
#include <netgraph/netflow/netflow.h>
|
2011-03-02 16:15:11 +00:00
|
|
|
#include <netgraph/netflow/netflow_v9.h>
|
2004-09-16 20:24:23 +00:00
|
|
|
#include <netgraph/netflow/ng_netflow.h>
|
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
#define NBUCKETS (65536) /* must be power of 2 */
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2006-10-11 12:31:14 +00:00
|
|
|
/* This hash is for TCP or UDP packets. */
|
|
|
|
#define FULL_HASH(addr1, addr2, port1, port2) \
|
|
|
|
(((addr1 ^ (addr1 >> 16) ^ \
|
|
|
|
htons(addr2 ^ (addr2 >> 16))) ^ \
|
2006-10-11 13:28:37 +00:00
|
|
|
port1 ^ htons(port2)) & \
|
2006-10-11 12:31:14 +00:00
|
|
|
(NBUCKETS - 1))
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2006-10-11 12:31:14 +00:00
|
|
|
/* This hash is for all other IP packets. */
|
|
|
|
#define ADDR_HASH(addr1, addr2) \
|
|
|
|
((addr1 ^ (addr1 >> 16) ^ \
|
|
|
|
htons(addr2 ^ (addr2 >> 16))) & \
|
|
|
|
(NBUCKETS - 1))
|
2004-09-16 20:24:23 +00:00
|
|
|
|
|
|
|
/* Macros to shorten logical constructions */
|
|
|
|
/* XXX: priv must exist in namespace */
|
2014-01-01 21:48:04 +00:00
|
|
|
#define INACTIVE(fle) (time_uptime - fle->f.last > priv->nfinfo_inact_t)
|
|
|
|
#define AGED(fle) (time_uptime - fle->f.first > priv->nfinfo_act_t)
|
2004-09-16 20:24:23 +00:00
|
|
|
#define ISFREE(fle) (fle->f.packets == 0)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 4 is a magical number: statistically number of 4-packet flows is
|
|
|
|
* bigger than 5,6,7...-packet flows by an order of magnitude. Most UDP/ICMP
|
|
|
|
* scans are 1 packet (~ 90% of flow cache). TCP scans are 2-packet in case
|
|
|
|
* of reachable host and 4-packet otherwise.
|
|
|
|
*/
|
|
|
|
#define SMALL(fle) (fle->f.packets <= 4)
|
2005-03-03 11:01:05 +00:00
|
|
|
|
2005-10-31 15:41:29 +00:00
|
|
|
MALLOC_DEFINE(M_NETFLOW_HASH, "netflow_hash", "NetFlow hash");
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
static int export_add(item_p, struct flow_entry *);
|
2011-03-02 16:15:11 +00:00
|
|
|
static int export_send(priv_p, fib_export_p, item_p, int);
|
|
|
|
|
2013-03-26 14:05:37 +00:00
|
|
|
static int hash_insert(priv_p, struct flow_hash_entry *, struct flow_rec *,
|
|
|
|
int, uint8_t, uint8_t);
|
2011-03-03 16:16:49 +00:00
|
|
|
#ifdef INET6
|
2013-03-26 14:05:37 +00:00
|
|
|
static int hash6_insert(priv_p, struct flow_hash_entry *, struct flow6_rec *,
|
|
|
|
int, uint8_t, uint8_t);
|
2011-03-03 16:16:49 +00:00
|
|
|
#endif
|
2011-03-02 16:15:11 +00:00
|
|
|
|
2013-03-26 14:05:37 +00:00
|
|
|
static void expire_flow(priv_p, fib_export_p, struct flow_entry *, int);
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2011-03-02 16:15:11 +00:00
|
|
|
/*
|
|
|
|
* Generate hash for a given flow record.
|
|
|
|
*
|
|
|
|
* FIB is not used here, because:
|
|
|
|
* most VRFS will carry public IPv4 addresses which are unique even
|
|
|
|
* without FIB private addresses can overlap, but this is worked out
|
|
|
|
* via flow_rec bcmp() containing fib id. In IPv6 world addresses are
|
|
|
|
* all globally unique (it's not fully true, there is FC00::/7 for example,
|
|
|
|
* but chances of address overlap are MUCH smaller)
|
|
|
|
*/
|
2013-03-26 14:05:37 +00:00
|
|
|
static inline uint32_t
|
2004-09-16 20:24:23 +00:00
|
|
|
ip_hash(struct flow_rec *r)
|
|
|
|
{
|
2013-03-26 14:05:37 +00:00
|
|
|
|
2004-09-16 20:24:23 +00:00
|
|
|
switch (r->r_ip_p) {
|
|
|
|
case IPPROTO_TCP:
|
|
|
|
case IPPROTO_UDP:
|
|
|
|
return FULL_HASH(r->r_src.s_addr, r->r_dst.s_addr,
|
|
|
|
r->r_sport, r->r_dport);
|
|
|
|
default:
|
|
|
|
return ADDR_HASH(r->r_src.s_addr, r->r_dst.s_addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-02 16:15:11 +00:00
|
|
|
#ifdef INET6
|
|
|
|
/* Generate hash for a given flow6 record. Use lower 4 octets from v6 addresses */
|
2013-03-26 14:05:37 +00:00
|
|
|
static inline uint32_t
|
2011-03-02 16:15:11 +00:00
|
|
|
ip6_hash(struct flow6_rec *r)
|
|
|
|
{
|
2013-03-26 14:05:37 +00:00
|
|
|
|
2011-03-02 16:15:11 +00:00
|
|
|
switch (r->r_ip_p) {
|
|
|
|
case IPPROTO_TCP:
|
|
|
|
case IPPROTO_UDP:
|
|
|
|
return FULL_HASH(r->src.r_src6.__u6_addr.__u6_addr32[3],
|
|
|
|
r->dst.r_dst6.__u6_addr.__u6_addr32[3], r->r_sport,
|
|
|
|
r->r_dport);
|
|
|
|
default:
|
|
|
|
return ADDR_HASH(r->src.r_src6.__u6_addr.__u6_addr32[3],
|
|
|
|
r->dst.r_dst6.__u6_addr.__u6_addr32[3]);
|
|
|
|
}
|
|
|
|
}
|
2016-05-26 18:57:26 +00:00
|
|
|
|
|
|
|
static inline int
|
|
|
|
ip6_masklen(struct in6_addr *saddr, struct rt_addrinfo *info)
|
|
|
|
{
|
|
|
|
const int nbits = sizeof(*saddr) * NBBY;
|
|
|
|
int mlen;
|
|
|
|
|
|
|
|
if (info->rti_addrs & RTA_NETMASK)
|
|
|
|
bit_count((bitstr_t *)saddr, 0, nbits, &mlen);
|
|
|
|
else
|
|
|
|
mlen = nbits;
|
|
|
|
return (mlen);
|
|
|
|
}
|
2011-03-02 16:15:11 +00:00
|
|
|
#endif
|
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
/*
|
|
|
|
* Detach export datagram from priv, if there is any.
|
|
|
|
* If there is no, allocate a new one.
|
|
|
|
*/
|
|
|
|
static item_p
|
2011-03-02 16:15:11 +00:00
|
|
|
get_export_dgram(priv_p priv, fib_export_p fe)
|
2005-05-11 11:26:24 +00:00
|
|
|
{
|
|
|
|
item_p item = NULL;
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2011-03-02 16:15:11 +00:00
|
|
|
mtx_lock(&fe->export_mtx);
|
|
|
|
if (fe->exp.item != NULL) {
|
|
|
|
item = fe->exp.item;
|
|
|
|
fe->exp.item = NULL;
|
2005-05-11 11:26:24 +00:00
|
|
|
}
|
2011-03-02 16:15:11 +00:00
|
|
|
mtx_unlock(&fe->export_mtx);
|
2005-05-11 11:26:24 +00:00
|
|
|
|
|
|
|
if (item == NULL) {
|
|
|
|
struct netflow_v5_export_dgram *dgram;
|
|
|
|
struct mbuf *m;
|
|
|
|
|
2012-12-05 08:04:20 +00:00
|
|
|
m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
|
2005-05-11 11:26:24 +00:00
|
|
|
if (m == NULL)
|
|
|
|
return (NULL);
|
2005-05-16 17:10:08 +00:00
|
|
|
item = ng_package_data(m, NG_NOFLAGS);
|
2005-05-11 11:26:24 +00:00
|
|
|
if (item == NULL)
|
|
|
|
return (NULL);
|
|
|
|
dgram = mtod(m, struct netflow_v5_export_dgram *);
|
|
|
|
dgram->header.count = 0;
|
|
|
|
dgram->header.version = htons(NETFLOW_V5);
|
2010-07-26 13:48:35 +00:00
|
|
|
dgram->header.pad = 0;
|
2005-05-11 11:26:24 +00:00
|
|
|
}
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
return (item);
|
2004-09-16 20:24:23 +00:00
|
|
|
}
|
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
/*
|
|
|
|
* Re-attach incomplete datagram back to priv.
|
|
|
|
* If there is already another one, then send incomplete. */
|
|
|
|
static void
|
2011-03-02 16:15:11 +00:00
|
|
|
return_export_dgram(priv_p priv, fib_export_p fe, item_p item, int flags)
|
2004-09-16 20:24:23 +00:00
|
|
|
{
|
2013-03-26 14:05:37 +00:00
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
/*
|
|
|
|
* It may happen on SMP, that some thread has already
|
|
|
|
* put its item there, in this case we bail out and
|
|
|
|
* send what we have to collector.
|
|
|
|
*/
|
2011-03-02 16:15:11 +00:00
|
|
|
mtx_lock(&fe->export_mtx);
|
|
|
|
if (fe->exp.item == NULL) {
|
|
|
|
fe->exp.item = item;
|
|
|
|
mtx_unlock(&fe->export_mtx);
|
2005-05-11 11:26:24 +00:00
|
|
|
} else {
|
2011-03-02 16:15:11 +00:00
|
|
|
mtx_unlock(&fe->export_mtx);
|
|
|
|
export_send(priv, fe, item, flags);
|
2005-05-11 11:26:24 +00:00
|
|
|
}
|
2004-09-16 20:24:23 +00:00
|
|
|
}
|
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
/*
|
|
|
|
* The flow is over. Call export_add() and free it. If datagram is
|
|
|
|
* full, then call export_send().
|
|
|
|
*/
|
2013-03-26 14:05:37 +00:00
|
|
|
static void
|
2011-03-02 16:15:11 +00:00
|
|
|
expire_flow(priv_p priv, fib_export_p fe, struct flow_entry *fle, int flags)
|
2004-09-16 20:24:23 +00:00
|
|
|
{
|
2011-03-02 16:15:11 +00:00
|
|
|
struct netflow_export_item exp;
|
|
|
|
uint16_t version = fle->f.version;
|
|
|
|
|
|
|
|
if ((priv->export != NULL) && (version == IPVERSION)) {
|
|
|
|
exp.item = get_export_dgram(priv, fe);
|
|
|
|
if (exp.item == NULL) {
|
2014-01-01 21:48:04 +00:00
|
|
|
priv->nfinfo_export_failed++;
|
2011-03-02 16:15:11 +00:00
|
|
|
if (priv->export9 != NULL)
|
2014-01-01 21:48:04 +00:00
|
|
|
priv->nfinfo_export9_failed++;
|
2013-03-26 14:05:37 +00:00
|
|
|
/* fle definitely contains IPv4 flow. */
|
2011-03-02 16:15:11 +00:00
|
|
|
uma_zfree_arg(priv->zone, fle, priv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (export_add(exp.item, fle) > 0)
|
|
|
|
export_send(priv, fe, exp.item, flags);
|
|
|
|
else
|
|
|
|
return_export_dgram(priv, fe, exp.item, NG_QUEUE);
|
2005-05-11 11:26:24 +00:00
|
|
|
}
|
2011-03-02 16:15:11 +00:00
|
|
|
|
|
|
|
if (priv->export9 != NULL) {
|
|
|
|
exp.item9 = get_export9_dgram(priv, fe, &exp.item9_opt);
|
|
|
|
if (exp.item9 == NULL) {
|
2014-01-01 21:48:04 +00:00
|
|
|
priv->nfinfo_export9_failed++;
|
2011-03-02 16:15:11 +00:00
|
|
|
if (version == IPVERSION)
|
|
|
|
uma_zfree_arg(priv->zone, fle, priv);
|
2011-03-03 16:16:49 +00:00
|
|
|
#ifdef INET6
|
2011-03-02 16:15:11 +00:00
|
|
|
else if (version == IP6VERSION)
|
|
|
|
uma_zfree_arg(priv->zone6, fle, priv);
|
2011-03-03 16:16:49 +00:00
|
|
|
#endif
|
2011-03-02 16:15:11 +00:00
|
|
|
else
|
2013-03-26 14:05:37 +00:00
|
|
|
panic("ng_netflow: Unknown IP proto: %d",
|
|
|
|
version);
|
2011-03-02 16:15:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (export9_add(exp.item9, exp.item9_opt, fle) > 0)
|
|
|
|
export9_send(priv, fe, exp.item9, exp.item9_opt, flags);
|
|
|
|
else
|
2013-03-26 14:05:37 +00:00
|
|
|
return_export9_dgram(priv, fe, exp.item9,
|
|
|
|
exp.item9_opt, NG_QUEUE);
|
2005-05-11 11:26:24 +00:00
|
|
|
}
|
2011-03-02 16:15:11 +00:00
|
|
|
|
|
|
|
if (version == IPVERSION)
|
|
|
|
uma_zfree_arg(priv->zone, fle, priv);
|
2011-03-03 16:16:49 +00:00
|
|
|
#ifdef INET6
|
2011-03-02 16:15:11 +00:00
|
|
|
else if (version == IP6VERSION)
|
|
|
|
uma_zfree_arg(priv->zone6, fle, priv);
|
2011-03-03 16:16:49 +00:00
|
|
|
#endif
|
2004-09-16 20:24:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a snapshot of node statistics */
|
|
|
|
void
|
|
|
|
ng_netflow_copyinfo(priv_p priv, struct ng_netflow_info *i)
|
|
|
|
{
|
2013-03-26 14:05:37 +00:00
|
|
|
|
2014-01-01 21:48:04 +00:00
|
|
|
i->nfinfo_bytes = counter_u64_fetch(priv->nfinfo_bytes);
|
|
|
|
i->nfinfo_packets = counter_u64_fetch(priv->nfinfo_packets);
|
|
|
|
i->nfinfo_bytes6 = counter_u64_fetch(priv->nfinfo_bytes6);
|
|
|
|
i->nfinfo_packets6 = counter_u64_fetch(priv->nfinfo_packets6);
|
|
|
|
i->nfinfo_sbytes = counter_u64_fetch(priv->nfinfo_sbytes);
|
|
|
|
i->nfinfo_spackets = counter_u64_fetch(priv->nfinfo_spackets);
|
|
|
|
i->nfinfo_sbytes6 = counter_u64_fetch(priv->nfinfo_sbytes6);
|
|
|
|
i->nfinfo_spackets6 = counter_u64_fetch(priv->nfinfo_spackets6);
|
|
|
|
i->nfinfo_act_exp = counter_u64_fetch(priv->nfinfo_act_exp);
|
|
|
|
i->nfinfo_inact_exp = counter_u64_fetch(priv->nfinfo_inact_exp);
|
|
|
|
|
|
|
|
i->nfinfo_used = uma_zone_get_cur(priv->zone);
|
2014-01-02 08:39:47 +00:00
|
|
|
#ifdef INET6
|
2014-01-01 21:48:04 +00:00
|
|
|
i->nfinfo_used6 = uma_zone_get_cur(priv->zone6);
|
2014-01-02 08:39:47 +00:00
|
|
|
#endif
|
2014-01-01 21:48:04 +00:00
|
|
|
|
|
|
|
i->nfinfo_alloc_failed = priv->nfinfo_alloc_failed;
|
|
|
|
i->nfinfo_export_failed = priv->nfinfo_export_failed;
|
|
|
|
i->nfinfo_export9_failed = priv->nfinfo_export9_failed;
|
|
|
|
i->nfinfo_realloc_mbuf = priv->nfinfo_realloc_mbuf;
|
|
|
|
i->nfinfo_alloc_fibs = priv->nfinfo_alloc_fibs;
|
|
|
|
i->nfinfo_inact_t = priv->nfinfo_inact_t;
|
|
|
|
i->nfinfo_act_t = priv->nfinfo_act_t;
|
2004-09-16 20:24:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert a record into defined slot.
|
|
|
|
*
|
|
|
|
* First we get for us a free flow entry, then fill in all
|
2005-05-11 11:26:24 +00:00
|
|
|
* possible fields in it.
|
|
|
|
*
|
|
|
|
* TODO: consider dropping hash mutex while filling in datagram,
|
|
|
|
* as this was done in previous version. Need to test & profile
|
|
|
|
* to be sure.
|
2004-09-16 20:24:23 +00:00
|
|
|
*/
|
2012-06-18 13:56:36 +00:00
|
|
|
static int
|
2011-03-02 16:15:11 +00:00
|
|
|
hash_insert(priv_p priv, struct flow_hash_entry *hsh, struct flow_rec *r,
|
2012-06-18 13:56:36 +00:00
|
|
|
int plen, uint8_t flags, uint8_t tcp_flags)
|
2004-09-16 20:24:23 +00:00
|
|
|
{
|
2008-02-07 11:10:17 +00:00
|
|
|
struct flow_entry *fle;
|
2016-01-14 13:14:12 +00:00
|
|
|
struct sockaddr_in sin, sin_mask;
|
|
|
|
struct sockaddr_dl rt_gateway;
|
|
|
|
struct rt_addrinfo info;
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
mtx_assert(&hsh->mtx, MA_OWNED);
|
|
|
|
|
|
|
|
fle = uma_zalloc_arg(priv->zone, priv, M_NOWAIT);
|
|
|
|
if (fle == NULL) {
|
2014-01-01 21:48:04 +00:00
|
|
|
priv->nfinfo_alloc_failed++;
|
2004-09-16 20:24:23 +00:00
|
|
|
return (ENOMEM);
|
2005-05-11 11:26:24 +00:00
|
|
|
}
|
2004-09-16 20:24:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now fle is totally ours. It is detached from all lists,
|
|
|
|
* we can safely edit it.
|
|
|
|
*/
|
2011-03-02 16:15:11 +00:00
|
|
|
fle->f.version = IPVERSION;
|
2004-09-16 20:24:23 +00:00
|
|
|
bcopy(r, &fle->f.r, sizeof(struct flow_rec));
|
|
|
|
fle->f.bytes = plen;
|
|
|
|
fle->f.packets = 1;
|
2005-03-20 21:03:43 +00:00
|
|
|
fle->f.tcp_flags = tcp_flags;
|
2004-09-16 20:24:23 +00:00
|
|
|
|
|
|
|
fle->f.first = fle->f.last = time_uptime;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First we do route table lookup on destination address. So we can
|
|
|
|
* fill in out_ifx, dst_mask, nexthop, and dst_as in future releases.
|
|
|
|
*/
|
2012-06-18 13:56:36 +00:00
|
|
|
if ((flags & NG_NETFLOW_CONF_NODSTLOOKUP) == 0) {
|
|
|
|
bzero(&sin, sizeof(sin));
|
|
|
|
sin.sin_len = sizeof(struct sockaddr_in);
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_addr = fle->f.r.r_dst;
|
|
|
|
|
2016-01-14 13:14:12 +00:00
|
|
|
rt_gateway.sdl_len = sizeof(rt_gateway);
|
|
|
|
sin_mask.sin_len = sizeof(struct sockaddr_in);
|
|
|
|
bzero(&info, sizeof(info));
|
|
|
|
|
|
|
|
info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&rt_gateway;
|
|
|
|
info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&sin_mask;
|
|
|
|
|
|
|
|
if (rib_lookup_info(r->fib, (struct sockaddr *)&sin, NHR_REF, 0,
|
|
|
|
&info) == 0) {
|
|
|
|
fle->f.fle_o_ifx = info.rti_ifp->if_index;
|
|
|
|
|
|
|
|
if (info.rti_flags & RTF_GATEWAY &&
|
|
|
|
rt_gateway.sdl_family == AF_INET)
|
2012-06-18 13:56:36 +00:00
|
|
|
fle->f.next_hop =
|
2016-01-14 13:14:12 +00:00
|
|
|
((struct sockaddr_in *)&rt_gateway)->sin_addr;
|
2012-06-18 13:56:36 +00:00
|
|
|
|
2016-01-14 13:14:12 +00:00
|
|
|
if (info.rti_addrs & RTA_NETMASK)
|
|
|
|
fle->f.dst_mask = bitcount32(sin_mask.sin_addr.s_addr);
|
|
|
|
else if (info.rti_flags & RTF_HOST)
|
2012-06-18 13:56:36 +00:00
|
|
|
/* Give up. We can't determine mask :( */
|
|
|
|
fle->f.dst_mask = 32;
|
|
|
|
|
2016-01-14 13:14:12 +00:00
|
|
|
rib_free_info(&info);
|
2012-06-18 13:56:36 +00:00
|
|
|
}
|
2004-09-16 20:24:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Do route lookup on source address, to fill in src_mask. */
|
2012-06-18 13:56:36 +00:00
|
|
|
if ((flags & NG_NETFLOW_CONF_NOSRCLOOKUP) == 0) {
|
|
|
|
bzero(&sin, sizeof(sin));
|
|
|
|
sin.sin_len = sizeof(struct sockaddr_in);
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_addr = fle->f.r.r_src;
|
2016-01-14 13:14:12 +00:00
|
|
|
|
|
|
|
sin_mask.sin_len = sizeof(struct sockaddr_in);
|
|
|
|
bzero(&info, sizeof(info));
|
|
|
|
|
|
|
|
info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&sin_mask;
|
|
|
|
|
|
|
|
if (rib_lookup_info(r->fib, (struct sockaddr *)&sin, 0, 0,
|
|
|
|
&info) == 0) {
|
|
|
|
if (info.rti_addrs & RTA_NETMASK)
|
2013-03-26 14:05:37 +00:00
|
|
|
fle->f.src_mask =
|
2016-01-14 13:14:12 +00:00
|
|
|
bitcount32(sin_mask.sin_addr.s_addr);
|
|
|
|
else if (info.rti_flags & RTF_HOST)
|
2012-06-18 13:56:36 +00:00
|
|
|
/* Give up. We can't determine mask :( */
|
|
|
|
fle->f.src_mask = 32;
|
|
|
|
}
|
2004-09-16 20:24:23 +00:00
|
|
|
}
|
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
/* Push new flow at the and of hash. */
|
|
|
|
TAILQ_INSERT_TAIL(&hsh->head, fle, fle_hash);
|
2004-09-16 20:24:23 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2011-03-02 16:15:11 +00:00
|
|
|
#ifdef INET6
|
2012-06-18 13:56:36 +00:00
|
|
|
static int
|
2011-07-05 14:48:39 +00:00
|
|
|
hash6_insert(priv_p priv, struct flow_hash_entry *hsh6, struct flow6_rec *r,
|
2012-06-18 13:56:36 +00:00
|
|
|
int plen, uint8_t flags, uint8_t tcp_flags)
|
2011-03-02 16:15:11 +00:00
|
|
|
{
|
|
|
|
struct flow6_entry *fle6;
|
2016-01-14 13:14:12 +00:00
|
|
|
struct sockaddr_in6 sin6, sin6_mask;
|
|
|
|
struct sockaddr_dl rt_gateway;
|
|
|
|
struct rt_addrinfo info;
|
2011-03-02 16:15:11 +00:00
|
|
|
|
|
|
|
mtx_assert(&hsh6->mtx, MA_OWNED);
|
|
|
|
|
|
|
|
fle6 = uma_zalloc_arg(priv->zone6, priv, M_NOWAIT);
|
|
|
|
if (fle6 == NULL) {
|
2014-01-01 21:48:04 +00:00
|
|
|
priv->nfinfo_alloc_failed++;
|
2011-03-02 16:15:11 +00:00
|
|
|
return (ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now fle is totally ours. It is detached from all lists,
|
|
|
|
* we can safely edit it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
fle6->f.version = IP6VERSION;
|
|
|
|
bcopy(r, &fle6->f.r, sizeof(struct flow6_rec));
|
|
|
|
fle6->f.bytes = plen;
|
|
|
|
fle6->f.packets = 1;
|
|
|
|
fle6->f.tcp_flags = tcp_flags;
|
|
|
|
|
|
|
|
fle6->f.first = fle6->f.last = time_uptime;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First we do route table lookup on destination address. So we can
|
|
|
|
* fill in out_ifx, dst_mask, nexthop, and dst_as in future releases.
|
|
|
|
*/
|
2013-03-26 14:05:37 +00:00
|
|
|
if ((flags & NG_NETFLOW_CONF_NODSTLOOKUP) == 0) {
|
2016-01-07 12:22:29 +00:00
|
|
|
bzero(&sin6, sizeof(struct sockaddr_in6));
|
|
|
|
sin6.sin6_len = sizeof(struct sockaddr_in6);
|
|
|
|
sin6.sin6_family = AF_INET6;
|
|
|
|
sin6.sin6_addr = r->dst.r_dst6;
|
2012-06-18 13:56:36 +00:00
|
|
|
|
2016-01-14 13:14:12 +00:00
|
|
|
rt_gateway.sdl_len = sizeof(rt_gateway);
|
|
|
|
sin6_mask.sin6_len = sizeof(struct sockaddr_in6);
|
|
|
|
bzero(&info, sizeof(info));
|
|
|
|
|
|
|
|
info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&rt_gateway;
|
|
|
|
info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&sin6_mask;
|
2012-06-18 13:56:36 +00:00
|
|
|
|
2016-01-14 13:14:12 +00:00
|
|
|
if (rib_lookup_info(r->fib, (struct sockaddr *)&sin6, NHR_REF,
|
|
|
|
0, &info) == 0) {
|
|
|
|
fle6->f.fle_o_ifx = info.rti_ifp->if_index;
|
2012-06-18 13:56:36 +00:00
|
|
|
|
2016-01-14 13:14:12 +00:00
|
|
|
if (info.rti_flags & RTF_GATEWAY &&
|
|
|
|
rt_gateway.sdl_family == AF_INET6)
|
2012-06-18 13:56:36 +00:00
|
|
|
fle6->f.n.next_hop6 =
|
2016-01-14 13:14:12 +00:00
|
|
|
((struct sockaddr_in6 *)&rt_gateway)->sin6_addr;
|
2012-06-18 13:56:36 +00:00
|
|
|
|
2016-05-26 19:17:51 +00:00
|
|
|
fle6->f.dst_mask =
|
|
|
|
ip6_masklen(&sin6_mask.sin6_addr, &info);
|
2011-03-02 16:15:11 +00:00
|
|
|
|
2016-01-14 13:14:12 +00:00
|
|
|
rib_free_info(&info);
|
2012-06-18 13:56:36 +00:00
|
|
|
}
|
2011-03-02 16:15:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-04 17:25:32 +00:00
|
|
|
if ((flags & NG_NETFLOW_CONF_NOSRCLOOKUP) == 0) {
|
2012-06-18 13:56:36 +00:00
|
|
|
/* Do route lookup on source address, to fill in src_mask. */
|
2016-01-07 12:22:29 +00:00
|
|
|
bzero(&sin6, sizeof(struct sockaddr_in6));
|
|
|
|
sin6.sin6_len = sizeof(struct sockaddr_in6);
|
|
|
|
sin6.sin6_family = AF_INET6;
|
|
|
|
sin6.sin6_addr = r->src.r_src6;
|
2011-03-02 16:15:11 +00:00
|
|
|
|
2016-01-14 13:14:12 +00:00
|
|
|
sin6_mask.sin6_len = sizeof(struct sockaddr_in6);
|
|
|
|
bzero(&info, sizeof(info));
|
2011-03-02 16:15:11 +00:00
|
|
|
|
2016-01-14 13:14:12 +00:00
|
|
|
info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&sin6_mask;
|
|
|
|
|
|
|
|
if (rib_lookup_info(r->fib, (struct sockaddr *)&sin6, 0, 0,
|
2016-05-26 18:57:26 +00:00
|
|
|
&info) == 0)
|
2016-05-26 19:17:51 +00:00
|
|
|
fle6->f.src_mask =
|
|
|
|
ip6_masklen(&sin6_mask.sin6_addr, &info);
|
2011-03-02 16:15:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Push new flow at the and of hash. */
|
2011-07-05 14:48:39 +00:00
|
|
|
TAILQ_INSERT_TAIL(&hsh6->head, (struct flow_entry *)fle6, fle_hash);
|
2011-03-02 16:15:11 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-09-16 20:24:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Non-static functions called from ng_netflow.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Allocate memory and set up flow cache */
|
2011-04-18 09:14:23 +00:00
|
|
|
void
|
2004-09-16 20:24:23 +00:00
|
|
|
ng_netflow_cache_init(priv_p priv)
|
|
|
|
{
|
2011-03-02 16:15:11 +00:00
|
|
|
struct flow_hash_entry *hsh;
|
2004-09-16 20:24:23 +00:00
|
|
|
int i;
|
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
/* Initialize cache UMA zone. */
|
2013-03-26 14:05:37 +00:00
|
|
|
priv->zone = uma_zcreate("NetFlow IPv4 cache",
|
2014-01-01 21:48:04 +00:00
|
|
|
sizeof(struct flow_entry), NULL, NULL, NULL, NULL,
|
|
|
|
UMA_ALIGN_CACHE, 0);
|
2005-05-11 11:26:24 +00:00
|
|
|
uma_zone_set_max(priv->zone, CACHESIZE);
|
2011-03-02 16:15:11 +00:00
|
|
|
#ifdef INET6
|
2013-03-26 14:05:37 +00:00
|
|
|
priv->zone6 = uma_zcreate("NetFlow IPv6 cache",
|
2014-01-01 21:48:04 +00:00
|
|
|
sizeof(struct flow6_entry), NULL, NULL, NULL, NULL,
|
|
|
|
UMA_ALIGN_CACHE, 0);
|
2011-03-02 16:15:11 +00:00
|
|
|
uma_zone_set_max(priv->zone6, CACHESIZE);
|
|
|
|
#endif
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
/* Allocate hash. */
|
2008-10-23 20:26:15 +00:00
|
|
|
priv->hash = malloc(NBUCKETS * sizeof(struct flow_hash_entry),
|
2005-05-11 11:26:24 +00:00
|
|
|
M_NETFLOW_HASH, M_WAITOK | M_ZERO);
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
/* Initialize hash. */
|
|
|
|
for (i = 0, hsh = priv->hash; i < NBUCKETS; i++, hsh++) {
|
|
|
|
mtx_init(&hsh->mtx, "hash mutex", NULL, MTX_DEF);
|
|
|
|
TAILQ_INIT(&hsh->head);
|
|
|
|
}
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2011-03-02 16:15:11 +00:00
|
|
|
#ifdef INET6
|
|
|
|
/* Allocate hash. */
|
2011-07-05 14:48:39 +00:00
|
|
|
priv->hash6 = malloc(NBUCKETS * sizeof(struct flow_hash_entry),
|
2011-03-02 16:15:11 +00:00
|
|
|
M_NETFLOW_HASH, M_WAITOK | M_ZERO);
|
|
|
|
|
|
|
|
/* Initialize hash. */
|
2011-07-05 14:48:39 +00:00
|
|
|
for (i = 0, hsh = priv->hash6; i < NBUCKETS; i++, hsh++) {
|
|
|
|
mtx_init(&hsh->mtx, "hash mutex", NULL, MTX_DEF);
|
|
|
|
TAILQ_INIT(&hsh->head);
|
2011-03-02 16:15:11 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-01-01 21:48:04 +00:00
|
|
|
priv->nfinfo_bytes = counter_u64_alloc(M_WAITOK);
|
|
|
|
priv->nfinfo_packets = counter_u64_alloc(M_WAITOK);
|
|
|
|
priv->nfinfo_bytes6 = counter_u64_alloc(M_WAITOK);
|
|
|
|
priv->nfinfo_packets6 = counter_u64_alloc(M_WAITOK);
|
|
|
|
priv->nfinfo_sbytes = counter_u64_alloc(M_WAITOK);
|
|
|
|
priv->nfinfo_spackets = counter_u64_alloc(M_WAITOK);
|
|
|
|
priv->nfinfo_sbytes6 = counter_u64_alloc(M_WAITOK);
|
|
|
|
priv->nfinfo_spackets6 = counter_u64_alloc(M_WAITOK);
|
|
|
|
priv->nfinfo_act_exp = counter_u64_alloc(M_WAITOK);
|
|
|
|
priv->nfinfo_inact_exp = counter_u64_alloc(M_WAITOK);
|
|
|
|
|
2011-03-02 16:15:11 +00:00
|
|
|
ng_netflow_v9_cache_init(priv);
|
|
|
|
CTR0(KTR_NET, "ng_netflow startup()");
|
2004-09-16 20:24:23 +00:00
|
|
|
}
|
|
|
|
|
2011-03-02 16:15:11 +00:00
|
|
|
/* Initialize new FIB table for v5 and v9 */
|
|
|
|
int
|
|
|
|
ng_netflow_fib_init(priv_p priv, int fib)
|
|
|
|
{
|
|
|
|
fib_export_p fe = priv_to_fib(priv, fib);
|
|
|
|
|
|
|
|
CTR1(KTR_NET, "ng_netflow(): fib init: %d", fib);
|
|
|
|
|
|
|
|
if (fe != NULL)
|
|
|
|
return (0);
|
|
|
|
|
2013-03-26 14:05:37 +00:00
|
|
|
if ((fe = malloc(sizeof(struct fib_export), M_NETGRAPH,
|
|
|
|
M_NOWAIT | M_ZERO)) == NULL)
|
2013-03-26 14:08:14 +00:00
|
|
|
return (ENOMEM);
|
2011-03-02 16:15:11 +00:00
|
|
|
|
|
|
|
mtx_init(&fe->export_mtx, "export dgram lock", NULL, MTX_DEF);
|
|
|
|
mtx_init(&fe->export9_mtx, "export9 dgram lock", NULL, MTX_DEF);
|
|
|
|
fe->fib = fib;
|
|
|
|
fe->domain_id = fib;
|
|
|
|
|
2013-03-26 14:05:37 +00:00
|
|
|
if (atomic_cmpset_ptr((volatile uintptr_t *)&priv->fib_data[fib],
|
|
|
|
(uintptr_t)NULL, (uintptr_t)fe) == 0) {
|
2011-03-02 16:15:11 +00:00
|
|
|
/* FIB already set up by other ISR */
|
2013-03-26 14:05:37 +00:00
|
|
|
CTR3(KTR_NET, "ng_netflow(): fib init: %d setup %p but got %p",
|
|
|
|
fib, fe, priv_to_fib(priv, fib));
|
2011-03-02 16:15:11 +00:00
|
|
|
mtx_destroy(&fe->export_mtx);
|
|
|
|
mtx_destroy(&fe->export9_mtx);
|
|
|
|
free(fe, M_NETGRAPH);
|
|
|
|
} else {
|
|
|
|
/* Increase counter for statistics */
|
2013-03-26 14:05:37 +00:00
|
|
|
CTR3(KTR_NET, "ng_netflow(): fib %d setup to %p (%p)",
|
|
|
|
fib, fe, priv_to_fib(priv, fib));
|
2014-01-01 21:48:04 +00:00
|
|
|
priv->nfinfo_alloc_fibs++;
|
2011-03-02 16:15:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2004-09-16 20:24:23 +00:00
|
|
|
/* Free all flow cache memory. Called from node close method. */
|
|
|
|
void
|
|
|
|
ng_netflow_cache_flush(priv_p priv)
|
|
|
|
{
|
2005-05-11 11:26:24 +00:00
|
|
|
struct flow_entry *fle, *fle1;
|
|
|
|
struct flow_hash_entry *hsh;
|
2011-03-02 16:15:11 +00:00
|
|
|
struct netflow_export_item exp;
|
|
|
|
fib_export_p fe;
|
2004-09-16 20:24:23 +00:00
|
|
|
int i;
|
|
|
|
|
2011-03-02 16:15:11 +00:00
|
|
|
bzero(&exp, sizeof(exp));
|
|
|
|
|
2004-09-16 20:24:23 +00:00
|
|
|
/*
|
|
|
|
* We are going to free probably billable data.
|
|
|
|
* Expire everything before freeing it.
|
|
|
|
* No locking is required since callout is already drained.
|
|
|
|
*/
|
2005-05-11 11:26:24 +00:00
|
|
|
for (hsh = priv->hash, i = 0; i < NBUCKETS; hsh++, i++)
|
|
|
|
TAILQ_FOREACH_SAFE(fle, &hsh->head, fle_hash, fle1) {
|
|
|
|
TAILQ_REMOVE(&hsh->head, fle, fle_hash);
|
2011-03-02 16:15:11 +00:00
|
|
|
fe = priv_to_fib(priv, fle->f.r.fib);
|
|
|
|
expire_flow(priv, fe, fle, NG_QUEUE);
|
2005-05-11 11:26:24 +00:00
|
|
|
}
|
2011-03-02 16:15:11 +00:00
|
|
|
#ifdef INET6
|
2011-07-05 14:48:39 +00:00
|
|
|
for (hsh = priv->hash6, i = 0; i < NBUCKETS; hsh++, i++)
|
|
|
|
TAILQ_FOREACH_SAFE(fle, &hsh->head, fle_hash, fle1) {
|
|
|
|
TAILQ_REMOVE(&hsh->head, fle, fle_hash);
|
|
|
|
fe = priv_to_fib(priv, fle->f.r.fib);
|
|
|
|
expire_flow(priv, fe, fle, NG_QUEUE);
|
2011-03-02 16:15:11 +00:00
|
|
|
}
|
|
|
|
#endif
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
uma_zdestroy(priv->zone);
|
|
|
|
/* Destroy hash mutexes. */
|
|
|
|
for (i = 0, hsh = priv->hash; i < NBUCKETS; i++, hsh++)
|
|
|
|
mtx_destroy(&hsh->mtx);
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
/* Free hash memory. */
|
2011-03-02 16:15:11 +00:00
|
|
|
if (priv->hash != NULL)
|
2008-10-23 15:53:51 +00:00
|
|
|
free(priv->hash, M_NETFLOW_HASH);
|
2011-03-02 16:15:11 +00:00
|
|
|
#ifdef INET6
|
|
|
|
uma_zdestroy(priv->zone6);
|
|
|
|
/* Destroy hash mutexes. */
|
2011-07-05 14:48:39 +00:00
|
|
|
for (i = 0, hsh = priv->hash6; i < NBUCKETS; i++, hsh++)
|
|
|
|
mtx_destroy(&hsh->mtx);
|
2011-03-02 16:15:11 +00:00
|
|
|
|
|
|
|
/* Free hash memory. */
|
|
|
|
if (priv->hash6 != NULL)
|
|
|
|
free(priv->hash6, M_NETFLOW_HASH);
|
|
|
|
#endif
|
|
|
|
|
2012-03-13 11:08:40 +00:00
|
|
|
for (i = 0; i < priv->maxfibs; i++) {
|
2011-03-02 16:15:11 +00:00
|
|
|
if ((fe = priv_to_fib(priv, i)) == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (fe->exp.item != NULL)
|
|
|
|
export_send(priv, fe, fe->exp.item, NG_QUEUE);
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2011-03-02 16:15:11 +00:00
|
|
|
if (fe->exp.item9 != NULL)
|
2013-03-26 14:05:37 +00:00
|
|
|
export9_send(priv, fe, fe->exp.item9,
|
|
|
|
fe->exp.item9_opt, NG_QUEUE);
|
2011-03-02 16:15:11 +00:00
|
|
|
|
|
|
|
mtx_destroy(&fe->export_mtx);
|
|
|
|
mtx_destroy(&fe->export9_mtx);
|
|
|
|
free(fe, M_NETGRAPH);
|
|
|
|
}
|
|
|
|
|
2014-01-01 21:48:04 +00:00
|
|
|
counter_u64_free(priv->nfinfo_bytes);
|
|
|
|
counter_u64_free(priv->nfinfo_packets);
|
|
|
|
counter_u64_free(priv->nfinfo_bytes6);
|
|
|
|
counter_u64_free(priv->nfinfo_packets6);
|
|
|
|
counter_u64_free(priv->nfinfo_sbytes);
|
|
|
|
counter_u64_free(priv->nfinfo_spackets);
|
|
|
|
counter_u64_free(priv->nfinfo_sbytes6);
|
|
|
|
counter_u64_free(priv->nfinfo_spackets6);
|
|
|
|
counter_u64_free(priv->nfinfo_act_exp);
|
|
|
|
counter_u64_free(priv->nfinfo_inact_exp);
|
|
|
|
|
2011-03-02 16:15:11 +00:00
|
|
|
ng_netflow_v9_cache_flush(priv);
|
2004-09-16 20:24:23 +00:00
|
|
|
}
|
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
/* Insert packet from into flow cache. */
|
2004-09-16 20:24:23 +00:00
|
|
|
int
|
2013-03-26 14:05:37 +00:00
|
|
|
ng_netflow_flow_add(priv_p priv, fib_export_p fe, struct ip *ip,
|
|
|
|
caddr_t upper_ptr, uint8_t upper_proto, uint8_t flags,
|
|
|
|
unsigned int src_if_index)
|
2004-09-16 20:24:23 +00:00
|
|
|
{
|
2013-03-26 14:05:37 +00:00
|
|
|
struct flow_entry *fle, *fle1;
|
2011-03-02 16:15:11 +00:00
|
|
|
struct flow_hash_entry *hsh;
|
2004-09-16 20:24:23 +00:00
|
|
|
struct flow_rec r;
|
2005-03-21 15:34:03 +00:00
|
|
|
int hlen, plen;
|
2005-05-11 11:26:24 +00:00
|
|
|
int error = 0;
|
2011-03-02 16:15:11 +00:00
|
|
|
uint16_t eproto;
|
2013-03-26 14:05:37 +00:00
|
|
|
uint8_t tcp_flags = 0;
|
2004-09-16 20:24:23 +00:00
|
|
|
|
|
|
|
bzero(&r, sizeof(r));
|
2013-03-26 14:05:37 +00:00
|
|
|
|
2005-03-21 15:34:03 +00:00
|
|
|
if (ip->ip_v != IPVERSION)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
hlen = ip->ip_hl << 2;
|
|
|
|
if (hlen < sizeof(struct ip))
|
|
|
|
return (EINVAL);
|
|
|
|
|
2011-03-02 16:15:11 +00:00
|
|
|
eproto = ETHERTYPE_IP;
|
|
|
|
/* Assume L4 template by default */
|
|
|
|
r.flow_type = NETFLOW_V9_FLOW_V4_L4;
|
|
|
|
|
2005-03-21 15:34:03 +00:00
|
|
|
r.r_src = ip->ip_src;
|
|
|
|
r.r_dst = ip->ip_dst;
|
2011-03-02 16:15:11 +00:00
|
|
|
r.fib = fe->fib;
|
2005-03-21 15:34:03 +00:00
|
|
|
|
|
|
|
plen = ntohs(ip->ip_len);
|
|
|
|
|
|
|
|
r.r_ip_p = ip->ip_p;
|
|
|
|
r.r_tos = ip->ip_tos;
|
|
|
|
|
2008-10-08 10:37:07 +00:00
|
|
|
r.r_i_ifx = src_if_index;
|
2005-03-21 15:34:03 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX NOTE: only first fragment of fragmented TCP, UDP and
|
|
|
|
* ICMP packet will be recorded with proper s_port and d_port.
|
|
|
|
* Following fragments will be recorded simply as IP packet with
|
|
|
|
* ip_proto = ip->ip_p and s_port, d_port set to zero.
|
|
|
|
* I know, it looks like bug. But I don't want to re-implement
|
|
|
|
* ip packet assebmling here. Anyway, (in)famous trafd works this way -
|
|
|
|
* and nobody complains yet :)
|
|
|
|
*/
|
2005-04-11 10:16:17 +00:00
|
|
|
if ((ip->ip_off & htons(IP_OFFMASK)) == 0)
|
|
|
|
switch(r.r_ip_p) {
|
|
|
|
case IPPROTO_TCP:
|
2013-03-26 14:05:37 +00:00
|
|
|
{
|
|
|
|
struct tcphdr *tcp;
|
2005-04-11 10:16:17 +00:00
|
|
|
|
|
|
|
tcp = (struct tcphdr *)((caddr_t )ip + hlen);
|
|
|
|
r.r_sport = tcp->th_sport;
|
|
|
|
r.r_dport = tcp->th_dport;
|
|
|
|
tcp_flags = tcp->th_flags;
|
|
|
|
break;
|
2013-03-26 14:05:37 +00:00
|
|
|
}
|
|
|
|
case IPPROTO_UDP:
|
2005-04-11 10:16:17 +00:00
|
|
|
r.r_ports = *(uint32_t *)((caddr_t )ip + hlen);
|
|
|
|
break;
|
|
|
|
}
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2014-01-01 21:48:04 +00:00
|
|
|
counter_u64_add(priv->nfinfo_packets, 1);
|
|
|
|
counter_u64_add(priv->nfinfo_bytes, plen);
|
2004-12-28 12:11:32 +00:00
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
/* Find hash slot. */
|
|
|
|
hsh = &priv->hash[ip_hash(&r)];
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
mtx_lock(&hsh->mtx);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Go through hash and find our entry. If we encounter an
|
|
|
|
* entry, that should be expired, purge it. We do a reverse
|
|
|
|
* search since most active entries are first, and most
|
|
|
|
* searches are done on most active entries.
|
|
|
|
*/
|
|
|
|
TAILQ_FOREACH_REVERSE_SAFE(fle, &hsh->head, fhead, fle_hash, fle1) {
|
|
|
|
if (bcmp(&r, &fle->f.r, sizeof(struct flow_rec)) == 0)
|
|
|
|
break;
|
|
|
|
if ((INACTIVE(fle) && SMALL(fle)) || AGED(fle)) {
|
|
|
|
TAILQ_REMOVE(&hsh->head, fle, fle_hash);
|
2013-03-26 14:05:37 +00:00
|
|
|
expire_flow(priv, priv_to_fib(priv, fle->f.r.fib),
|
|
|
|
fle, NG_QUEUE);
|
2014-01-01 21:48:04 +00:00
|
|
|
counter_u64_add(priv->nfinfo_act_exp, 1);
|
2005-05-11 11:26:24 +00:00
|
|
|
}
|
|
|
|
}
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
if (fle) { /* An existent entry. */
|
2004-09-16 20:24:23 +00:00
|
|
|
|
|
|
|
fle->f.bytes += plen;
|
|
|
|
fle->f.packets ++;
|
|
|
|
fle->f.tcp_flags |= tcp_flags;
|
|
|
|
fle->f.last = time_uptime;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We have the following reasons to expire flow in active way:
|
|
|
|
* - it hit active timeout
|
|
|
|
* - a TCP connection closed
|
|
|
|
* - it is going to overflow counter
|
|
|
|
*/
|
|
|
|
if (tcp_flags & TH_FIN || tcp_flags & TH_RST || AGED(fle) ||
|
2011-03-02 16:15:11 +00:00
|
|
|
(fle->f.bytes >= (CNTR_MAX - IF_MAXMTU)) ) {
|
2005-05-11 11:26:24 +00:00
|
|
|
TAILQ_REMOVE(&hsh->head, fle, fle_hash);
|
2013-03-26 14:05:37 +00:00
|
|
|
expire_flow(priv, priv_to_fib(priv, fle->f.r.fib),
|
|
|
|
fle, NG_QUEUE);
|
2014-01-01 21:48:04 +00:00
|
|
|
counter_u64_add(priv->nfinfo_act_exp, 1);
|
2005-05-11 11:26:24 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* It is the newest, move it to the tail,
|
|
|
|
* if it isn't there already. Next search will
|
|
|
|
* locate it quicker.
|
|
|
|
*/
|
|
|
|
if (fle != TAILQ_LAST(&hsh->head, fhead)) {
|
|
|
|
TAILQ_REMOVE(&hsh->head, fle, fle_hash);
|
|
|
|
TAILQ_INSERT_TAIL(&hsh->head, fle, fle_hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else /* A new flow entry. */
|
2012-06-18 13:56:36 +00:00
|
|
|
error = hash_insert(priv, hsh, &r, plen, flags, tcp_flags);
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
mtx_unlock(&hsh->mtx);
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2011-03-02 16:15:11 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef INET6
|
|
|
|
/* Insert IPv6 packet from into flow cache. */
|
|
|
|
int
|
2013-03-26 14:05:37 +00:00
|
|
|
ng_netflow_flow6_add(priv_p priv, fib_export_p fe, struct ip6_hdr *ip6,
|
|
|
|
caddr_t upper_ptr, uint8_t upper_proto, uint8_t flags,
|
|
|
|
unsigned int src_if_index)
|
2011-03-02 16:15:11 +00:00
|
|
|
{
|
2013-03-26 14:05:37 +00:00
|
|
|
struct flow_entry *fle = NULL, *fle1;
|
|
|
|
struct flow6_entry *fle6;
|
|
|
|
struct flow_hash_entry *hsh;
|
|
|
|
struct flow6_rec r;
|
2011-03-02 16:15:11 +00:00
|
|
|
int plen;
|
|
|
|
int error = 0;
|
|
|
|
uint8_t tcp_flags = 0;
|
|
|
|
|
|
|
|
/* check version */
|
|
|
|
if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
bzero(&r, sizeof(r));
|
|
|
|
|
|
|
|
r.src.r_src6 = ip6->ip6_src;
|
|
|
|
r.dst.r_dst6 = ip6->ip6_dst;
|
|
|
|
r.fib = fe->fib;
|
|
|
|
|
|
|
|
/* Assume L4 template by default */
|
|
|
|
r.flow_type = NETFLOW_V9_FLOW_V6_L4;
|
|
|
|
|
|
|
|
plen = ntohs(ip6->ip6_plen) + sizeof(struct ip6_hdr);
|
|
|
|
|
|
|
|
#if 0
|
2013-03-26 14:05:37 +00:00
|
|
|
/* XXX: set DSCP/CoS value */
|
2011-03-02 16:15:11 +00:00
|
|
|
r.r_tos = ip->ip_tos;
|
|
|
|
#endif
|
2012-06-18 13:56:36 +00:00
|
|
|
if ((flags & NG_NETFLOW_IS_FRAG) == 0) {
|
2011-03-02 16:15:11 +00:00
|
|
|
switch(upper_proto) {
|
|
|
|
case IPPROTO_TCP:
|
2013-03-26 14:05:37 +00:00
|
|
|
{
|
|
|
|
struct tcphdr *tcp;
|
2011-03-02 16:15:11 +00:00
|
|
|
|
|
|
|
tcp = (struct tcphdr *)upper_ptr;
|
|
|
|
r.r_ports = *(uint32_t *)upper_ptr;
|
|
|
|
tcp_flags = tcp->th_flags;
|
|
|
|
break;
|
2013-03-26 14:05:37 +00:00
|
|
|
}
|
2011-03-02 16:15:11 +00:00
|
|
|
case IPPROTO_UDP:
|
|
|
|
case IPPROTO_SCTP:
|
|
|
|
r.r_ports = *(uint32_t *)upper_ptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r.r_ip_p = upper_proto;
|
|
|
|
r.r_i_ifx = src_if_index;
|
|
|
|
|
2014-01-01 21:48:04 +00:00
|
|
|
counter_u64_add(priv->nfinfo_packets6, 1);
|
|
|
|
counter_u64_add(priv->nfinfo_bytes6, plen);
|
2011-03-02 16:15:11 +00:00
|
|
|
|
|
|
|
/* Find hash slot. */
|
2011-07-05 14:48:39 +00:00
|
|
|
hsh = &priv->hash6[ip6_hash(&r)];
|
2011-03-02 16:15:11 +00:00
|
|
|
|
2011-07-05 14:48:39 +00:00
|
|
|
mtx_lock(&hsh->mtx);
|
2011-03-02 16:15:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Go through hash and find our entry. If we encounter an
|
|
|
|
* entry, that should be expired, purge it. We do a reverse
|
|
|
|
* search since most active entries are first, and most
|
|
|
|
* searches are done on most active entries.
|
|
|
|
*/
|
2011-07-05 14:48:39 +00:00
|
|
|
TAILQ_FOREACH_REVERSE_SAFE(fle, &hsh->head, fhead, fle_hash, fle1) {
|
|
|
|
if (fle->f.version != IP6VERSION)
|
2011-03-02 16:15:11 +00:00
|
|
|
continue;
|
2011-07-05 14:48:39 +00:00
|
|
|
fle6 = (struct flow6_entry *)fle;
|
2011-03-02 16:15:11 +00:00
|
|
|
if (bcmp(&r, &fle6->f.r, sizeof(struct flow6_rec)) == 0)
|
|
|
|
break;
|
|
|
|
if ((INACTIVE(fle6) && SMALL(fle6)) || AGED(fle6)) {
|
2011-07-05 14:48:39 +00:00
|
|
|
TAILQ_REMOVE(&hsh->head, fle, fle_hash);
|
|
|
|
expire_flow(priv, priv_to_fib(priv, fle->f.r.fib), fle,
|
|
|
|
NG_QUEUE);
|
2014-01-01 21:48:04 +00:00
|
|
|
counter_u64_add(priv->nfinfo_act_exp, 1);
|
2011-03-02 16:15:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-05 14:48:39 +00:00
|
|
|
if (fle != NULL) { /* An existent entry. */
|
|
|
|
fle6 = (struct flow6_entry *)fle;
|
2011-03-02 16:15:11 +00:00
|
|
|
|
|
|
|
fle6->f.bytes += plen;
|
|
|
|
fle6->f.packets ++;
|
|
|
|
fle6->f.tcp_flags |= tcp_flags;
|
|
|
|
fle6->f.last = time_uptime;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We have the following reasons to expire flow in active way:
|
|
|
|
* - it hit active timeout
|
|
|
|
* - a TCP connection closed
|
|
|
|
* - it is going to overflow counter
|
|
|
|
*/
|
|
|
|
if (tcp_flags & TH_FIN || tcp_flags & TH_RST || AGED(fle6) ||
|
|
|
|
(fle6->f.bytes >= (CNTR_MAX - IF_MAXMTU)) ) {
|
2011-07-05 14:48:39 +00:00
|
|
|
TAILQ_REMOVE(&hsh->head, fle, fle_hash);
|
|
|
|
expire_flow(priv, priv_to_fib(priv, fle->f.r.fib), fle,
|
|
|
|
NG_QUEUE);
|
2014-01-01 21:48:04 +00:00
|
|
|
counter_u64_add(priv->nfinfo_act_exp, 1);
|
2011-03-02 16:15:11 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* It is the newest, move it to the tail,
|
|
|
|
* if it isn't there already. Next search will
|
|
|
|
* locate it quicker.
|
|
|
|
*/
|
2011-07-05 14:48:39 +00:00
|
|
|
if (fle != TAILQ_LAST(&hsh->head, fhead)) {
|
|
|
|
TAILQ_REMOVE(&hsh->head, fle, fle_hash);
|
|
|
|
TAILQ_INSERT_TAIL(&hsh->head, fle, fle_hash);
|
2011-03-02 16:15:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else /* A new flow entry. */
|
2012-06-18 13:56:36 +00:00
|
|
|
error = hash6_insert(priv, hsh, &r, plen, flags, tcp_flags);
|
2011-03-02 16:15:11 +00:00
|
|
|
|
2011-07-05 14:48:39 +00:00
|
|
|
mtx_unlock(&hsh->mtx);
|
2004-12-28 12:11:32 +00:00
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
return (error);
|
2004-09-16 20:24:23 +00:00
|
|
|
}
|
2011-03-02 16:15:11 +00:00
|
|
|
#endif
|
2004-09-16 20:24:23 +00:00
|
|
|
|
|
|
|
/*
|
2005-05-11 11:26:24 +00:00
|
|
|
* Return records from cache to userland.
|
2004-09-16 20:24:23 +00:00
|
|
|
*
|
|
|
|
* TODO: matching particular IP should be done in kernel, here.
|
|
|
|
*/
|
|
|
|
int
|
2011-07-05 14:48:39 +00:00
|
|
|
ng_netflow_flow_show(priv_p priv, struct ngnf_show_header *req,
|
|
|
|
struct ngnf_show_header *resp)
|
2004-09-16 20:24:23 +00:00
|
|
|
{
|
2011-03-02 16:15:11 +00:00
|
|
|
struct flow_hash_entry *hsh;
|
|
|
|
struct flow_entry *fle;
|
2011-07-05 14:48:39 +00:00
|
|
|
struct flow_entry_data *data = (struct flow_entry_data *)(resp + 1);
|
|
|
|
#ifdef INET6
|
|
|
|
struct flow6_entry_data *data6 = (struct flow6_entry_data *)(resp + 1);
|
|
|
|
#endif
|
|
|
|
int i, max;
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2011-07-05 14:48:39 +00:00
|
|
|
i = req->hash_id;
|
|
|
|
if (i > NBUCKETS-1)
|
|
|
|
return (EINVAL);
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2011-07-05 14:48:39 +00:00
|
|
|
#ifdef INET6
|
|
|
|
if (req->version == 6) {
|
|
|
|
resp->version = 6;
|
|
|
|
hsh = priv->hash6 + i;
|
|
|
|
max = NREC6_AT_ONCE;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
if (req->version == 4) {
|
|
|
|
resp->version = 4;
|
|
|
|
hsh = priv->hash + i;
|
|
|
|
max = NREC_AT_ONCE;
|
|
|
|
} else
|
|
|
|
return (EINVAL);
|
2004-09-16 20:24:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We will transfer not more than NREC_AT_ONCE. More data
|
|
|
|
* will come in next message.
|
2011-07-05 14:48:39 +00:00
|
|
|
* We send current hash index and current record number in list
|
|
|
|
* to userland, and userland should return it back to us.
|
|
|
|
* Then, we will restart with new entry.
|
2005-05-11 11:26:24 +00:00
|
|
|
*
|
2011-07-05 14:48:39 +00:00
|
|
|
* The resulting cache snapshot can be inaccurate if flow expiration
|
|
|
|
* is taking place on hash item between userland data requests for
|
|
|
|
* this hash item id.
|
2004-09-16 20:24:23 +00:00
|
|
|
*/
|
2011-07-05 14:48:39 +00:00
|
|
|
resp->nentries = 0;
|
2005-05-11 11:26:24 +00:00
|
|
|
for (; i < NBUCKETS; hsh++, i++) {
|
2011-07-05 14:48:39 +00:00
|
|
|
int list_id;
|
|
|
|
|
|
|
|
if (mtx_trylock(&hsh->mtx) == 0) {
|
|
|
|
/*
|
|
|
|
* Requested hash index is not available,
|
|
|
|
* relay decision to skip or re-request data
|
|
|
|
* to userland.
|
|
|
|
*/
|
|
|
|
resp->hash_id = i;
|
|
|
|
resp->list_id = 0;
|
|
|
|
return (0);
|
|
|
|
}
|
2005-05-11 11:26:24 +00:00
|
|
|
|
2011-07-05 14:48:39 +00:00
|
|
|
list_id = 0;
|
2005-05-11 11:26:24 +00:00
|
|
|
TAILQ_FOREACH(fle, &hsh->head, fle_hash) {
|
2011-07-05 14:48:39 +00:00
|
|
|
if (hsh->mtx.mtx_lock & MTX_CONTESTED) {
|
|
|
|
resp->hash_id = i;
|
|
|
|
resp->list_id = list_id;
|
2011-07-06 09:43:25 +00:00
|
|
|
mtx_unlock(&hsh->mtx);
|
2011-07-05 14:48:39 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
list_id++;
|
|
|
|
/* Search for particular record in list. */
|
|
|
|
if (req->list_id > 0) {
|
|
|
|
if (list_id < req->list_id)
|
|
|
|
continue;
|
2005-05-11 11:26:24 +00:00
|
|
|
|
2011-07-05 14:48:39 +00:00
|
|
|
/* Requested list position found. */
|
|
|
|
req->list_id = 0;
|
|
|
|
}
|
|
|
|
#ifdef INET6
|
|
|
|
if (req->version == 6) {
|
|
|
|
struct flow6_entry *fle6;
|
|
|
|
|
|
|
|
fle6 = (struct flow6_entry *)fle;
|
|
|
|
bcopy(&fle6->f, data6 + resp->nentries,
|
|
|
|
sizeof(fle6->f));
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
bcopy(&fle->f, data + resp->nentries,
|
|
|
|
sizeof(fle->f));
|
|
|
|
resp->nentries++;
|
|
|
|
if (resp->nentries == max) {
|
|
|
|
resp->hash_id = i;
|
|
|
|
/*
|
|
|
|
* If it was the last item in list
|
|
|
|
* we simply skip to next hash_id.
|
|
|
|
*/
|
|
|
|
resp->list_id = list_id + 1;
|
2011-07-06 09:43:25 +00:00
|
|
|
mtx_unlock(&hsh->mtx);
|
2005-05-11 11:26:24 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2004-09-16 20:24:23 +00:00
|
|
|
}
|
2005-05-11 11:26:24 +00:00
|
|
|
mtx_unlock(&hsh->mtx);
|
|
|
|
}
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2011-07-05 14:48:39 +00:00
|
|
|
resp->hash_id = resp->list_id = 0;
|
|
|
|
|
2004-09-16 20:24:23 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have full datagram in privdata. Send it to export hook. */
|
|
|
|
static int
|
2011-03-02 16:15:11 +00:00
|
|
|
export_send(priv_p priv, fib_export_p fe, item_p item, int flags)
|
2004-09-16 20:24:23 +00:00
|
|
|
{
|
2005-05-11 11:26:24 +00:00
|
|
|
struct mbuf *m = NGI_M(item);
|
|
|
|
struct netflow_v5_export_dgram *dgram = mtod(m,
|
|
|
|
struct netflow_v5_export_dgram *);
|
|
|
|
struct netflow_v5_header *header = &dgram->header;
|
2004-09-16 20:24:23 +00:00
|
|
|
struct timespec ts;
|
|
|
|
int error = 0;
|
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
/* Fill mbuf header. */
|
|
|
|
m->m_len = m->m_pkthdr.len = sizeof(struct netflow_v5_record) *
|
|
|
|
header->count + sizeof(struct netflow_v5_header);
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
/* Fill export header. */
|
|
|
|
header->sys_uptime = htonl(MILLIUPTIME(time_uptime));
|
2004-09-16 20:24:23 +00:00
|
|
|
getnanotime(&ts);
|
|
|
|
header->unix_secs = htonl(ts.tv_sec);
|
|
|
|
header->unix_nsecs = htonl(ts.tv_nsec);
|
2006-04-25 19:56:53 +00:00
|
|
|
header->engine_type = 0;
|
2011-03-02 16:15:11 +00:00
|
|
|
header->engine_id = fe->domain_id;
|
2006-04-25 19:56:53 +00:00
|
|
|
header->pad = 0;
|
2011-03-02 16:15:11 +00:00
|
|
|
header->flow_seq = htonl(atomic_fetchadd_32(&fe->flow_seq,
|
2005-11-27 02:43:08 +00:00
|
|
|
header->count));
|
2004-09-16 20:24:23 +00:00
|
|
|
header->count = htons(header->count);
|
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
if (priv->export != NULL)
|
2006-01-12 22:48:12 +00:00
|
|
|
NG_FWD_ITEM_HOOK_FLAGS(error, item, priv->export, flags);
|
2008-01-27 09:22:10 +00:00
|
|
|
else
|
|
|
|
NG_FREE_ITEM(item);
|
2004-09-16 20:24:23 +00:00
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
/* Add export record to dgram. */
|
2004-09-16 20:24:23 +00:00
|
|
|
static int
|
2005-05-11 11:26:24 +00:00
|
|
|
export_add(item_p item, struct flow_entry *fle)
|
2004-09-16 20:24:23 +00:00
|
|
|
{
|
2005-05-11 11:26:24 +00:00
|
|
|
struct netflow_v5_export_dgram *dgram = mtod(NGI_M(item),
|
|
|
|
struct netflow_v5_export_dgram *);
|
|
|
|
struct netflow_v5_header *header = &dgram->header;
|
2004-09-16 20:24:23 +00:00
|
|
|
struct netflow_v5_record *rec;
|
|
|
|
|
2008-01-27 08:52:41 +00:00
|
|
|
rec = &dgram->r[header->count];
|
|
|
|
header->count ++;
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
KASSERT(header->count <= NETFLOW_V5_MAX_RECORDS,
|
|
|
|
("ng_netflow: export too big"));
|
|
|
|
|
|
|
|
/* Fill in export record. */
|
2004-09-16 20:24:23 +00:00
|
|
|
rec->src_addr = fle->f.r.r_src.s_addr;
|
|
|
|
rec->dst_addr = fle->f.r.r_dst.s_addr;
|
|
|
|
rec->next_hop = fle->f.next_hop.s_addr;
|
|
|
|
rec->i_ifx = htons(fle->f.fle_i_ifx);
|
|
|
|
rec->o_ifx = htons(fle->f.fle_o_ifx);
|
|
|
|
rec->packets = htonl(fle->f.packets);
|
|
|
|
rec->octets = htonl(fle->f.bytes);
|
2005-03-03 11:01:05 +00:00
|
|
|
rec->first = htonl(MILLIUPTIME(fle->f.first));
|
|
|
|
rec->last = htonl(MILLIUPTIME(fle->f.last));
|
2004-09-16 20:24:23 +00:00
|
|
|
rec->s_port = fle->f.r.r_sport;
|
|
|
|
rec->d_port = fle->f.r.r_dport;
|
|
|
|
rec->flags = fle->f.tcp_flags;
|
|
|
|
rec->prot = fle->f.r.r_ip_p;
|
|
|
|
rec->tos = fle->f.r.r_tos;
|
|
|
|
rec->dst_mask = fle->f.dst_mask;
|
|
|
|
rec->src_mask = fle->f.src_mask;
|
2010-07-26 13:48:35 +00:00
|
|
|
rec->pad1 = 0;
|
|
|
|
rec->pad2 = 0;
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
/* Not supported fields. */
|
|
|
|
rec->src_as = rec->dst_as = 0;
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
if (header->count == NETFLOW_V5_MAX_RECORDS)
|
|
|
|
return (1); /* end of datagram */
|
|
|
|
else
|
|
|
|
return (0);
|
2004-09-16 20:24:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Periodic flow expiry run. */
|
|
|
|
void
|
|
|
|
ng_netflow_expire(void *arg)
|
|
|
|
{
|
2005-05-11 11:26:24 +00:00
|
|
|
struct flow_entry *fle, *fle1;
|
|
|
|
struct flow_hash_entry *hsh;
|
|
|
|
priv_p priv = (priv_p )arg;
|
2014-01-01 21:48:04 +00:00
|
|
|
int used, i;
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
/*
|
|
|
|
* Going through all the cache.
|
|
|
|
*/
|
2014-01-01 21:48:04 +00:00
|
|
|
used = uma_zone_get_cur(priv->zone);
|
2005-05-11 11:26:24 +00:00
|
|
|
for (hsh = priv->hash, i = 0; i < NBUCKETS; hsh++, i++) {
|
2004-09-16 20:24:23 +00:00
|
|
|
/*
|
2005-05-11 11:26:24 +00:00
|
|
|
* Skip entries, that are already being worked on.
|
2004-09-16 20:24:23 +00:00
|
|
|
*/
|
2005-05-11 11:26:24 +00:00
|
|
|
if (mtx_trylock(&hsh->mtx) == 0)
|
|
|
|
continue;
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
TAILQ_FOREACH_SAFE(fle, &hsh->head, fle_hash, fle1) {
|
2004-09-16 20:24:23 +00:00
|
|
|
/*
|
2005-05-11 11:26:24 +00:00
|
|
|
* Interrupt thread wants this entry!
|
|
|
|
* Quick! Quick! Bail out!
|
2004-09-16 20:24:23 +00:00
|
|
|
*/
|
2005-05-11 11:26:24 +00:00
|
|
|
if (hsh->mtx.mtx_lock & MTX_CONTESTED)
|
|
|
|
break;
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
/*
|
|
|
|
* Don't expire aggressively while hash collision
|
|
|
|
* ratio is predicted small.
|
|
|
|
*/
|
|
|
|
if (used <= (NBUCKETS*2) && !INACTIVE(fle))
|
|
|
|
break;
|
|
|
|
|
2006-10-11 12:32:53 +00:00
|
|
|
if ((INACTIVE(fle) && (SMALL(fle) ||
|
2006-10-11 12:39:21 +00:00
|
|
|
(used > (NBUCKETS*2)))) || AGED(fle)) {
|
2005-05-11 11:26:24 +00:00
|
|
|
TAILQ_REMOVE(&hsh->head, fle, fle_hash);
|
2013-03-26 14:05:37 +00:00
|
|
|
expire_flow(priv, priv_to_fib(priv,
|
|
|
|
fle->f.r.fib), fle, NG_NOFLAGS);
|
2005-05-11 11:26:24 +00:00
|
|
|
used--;
|
2014-01-01 21:48:04 +00:00
|
|
|
counter_u64_add(priv->nfinfo_inact_exp, 1);
|
2005-05-11 11:26:24 +00:00
|
|
|
}
|
2004-09-16 20:24:23 +00:00
|
|
|
}
|
2005-05-11 11:26:24 +00:00
|
|
|
mtx_unlock(&hsh->mtx);
|
|
|
|
}
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2011-03-02 16:15:11 +00:00
|
|
|
#ifdef INET6
|
2014-01-01 21:48:04 +00:00
|
|
|
used = uma_zone_get_cur(priv->zone6);
|
2011-07-05 14:48:39 +00:00
|
|
|
for (hsh = priv->hash6, i = 0; i < NBUCKETS; hsh++, i++) {
|
|
|
|
struct flow6_entry *fle6;
|
|
|
|
|
2011-03-02 16:15:11 +00:00
|
|
|
/*
|
|
|
|
* Skip entries, that are already being worked on.
|
|
|
|
*/
|
2011-07-05 14:48:39 +00:00
|
|
|
if (mtx_trylock(&hsh->mtx) == 0)
|
2011-03-02 16:15:11 +00:00
|
|
|
continue;
|
|
|
|
|
2011-07-05 14:48:39 +00:00
|
|
|
TAILQ_FOREACH_SAFE(fle, &hsh->head, fle_hash, fle1) {
|
|
|
|
fle6 = (struct flow6_entry *)fle;
|
2011-03-02 16:15:11 +00:00
|
|
|
/*
|
|
|
|
* Interrupt thread wants this entry!
|
|
|
|
* Quick! Quick! Bail out!
|
|
|
|
*/
|
2011-07-05 14:48:39 +00:00
|
|
|
if (hsh->mtx.mtx_lock & MTX_CONTESTED)
|
2011-03-02 16:15:11 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't expire aggressively while hash collision
|
|
|
|
* ratio is predicted small.
|
|
|
|
*/
|
|
|
|
if (used <= (NBUCKETS*2) && !INACTIVE(fle6))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ((INACTIVE(fle6) && (SMALL(fle6) ||
|
|
|
|
(used > (NBUCKETS*2)))) || AGED(fle6)) {
|
2011-07-05 14:48:39 +00:00
|
|
|
TAILQ_REMOVE(&hsh->head, fle, fle_hash);
|
|
|
|
expire_flow(priv, priv_to_fib(priv,
|
|
|
|
fle->f.r.fib), fle, NG_NOFLAGS);
|
2011-03-02 16:15:11 +00:00
|
|
|
used--;
|
2014-01-01 21:48:04 +00:00
|
|
|
counter_u64_add(priv->nfinfo_inact_exp, 1);
|
2011-03-02 16:15:11 +00:00
|
|
|
}
|
|
|
|
}
|
2011-07-05 14:48:39 +00:00
|
|
|
mtx_unlock(&hsh->mtx);
|
2011-03-02 16:15:11 +00:00
|
|
|
}
|
|
|
|
#endif
|
2004-09-16 20:24:23 +00:00
|
|
|
|
2005-05-11 11:26:24 +00:00
|
|
|
/* Schedule next expire. */
|
2004-09-16 20:24:23 +00:00
|
|
|
callout_reset(&priv->exp_callout, (1*hz), &ng_netflow_expire,
|
|
|
|
(void *)priv);
|
|
|
|
}
|