2 * Copyright (c) 2010-2011 Alexander V. Chernikov <melifaro@ipfw.ru>
3 * Copyright (c) 2004-2005 Gleb Smirnoff <glebius@FreeBSD.org>
4 * Copyright (c) 2001-2003 Roman V. Palagin <romanp@unshadow.net>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $SourceForge: ng_netflow.c,v 1.30 2004/09/05 11:37:43 glebius Exp $
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include "opt_inet6.h"
35 #include "opt_route.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/counter.h>
40 #include <sys/kernel.h>
42 #include <sys/limits.h>
44 #include <sys/socket.h>
45 #include <sys/syslog.h>
46 #include <sys/ctype.h>
49 #include <net/ethernet.h>
50 #include <net/route.h>
51 #include <net/if_arp.h>
52 #include <net/if_var.h>
53 #include <net/if_vlan_var.h>
55 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/ip.h>
58 #include <netinet/ip6.h>
59 #include <netinet/tcp.h>
60 #include <netinet/udp.h>
61 #include <netinet/sctp.h>
63 #include <netgraph/ng_message.h>
64 #include <netgraph/ng_parse.h>
65 #include <netgraph/netgraph.h>
66 #include <netgraph/netflow/netflow.h>
67 #include <netgraph/netflow/netflow_v9.h>
68 #include <netgraph/netflow/ng_netflow.h>
70 /* Netgraph methods */
71 static ng_constructor_t ng_netflow_constructor;
72 static ng_rcvmsg_t ng_netflow_rcvmsg;
73 static ng_close_t ng_netflow_close;
74 static ng_shutdown_t ng_netflow_rmnode;
75 static ng_newhook_t ng_netflow_newhook;
76 static ng_rcvdata_t ng_netflow_rcvdata;
77 static ng_disconnect_t ng_netflow_disconnect;
79 /* Parse type for struct ng_netflow_info */
80 static const struct ng_parse_struct_field ng_netflow_info_type_fields[]
81 = NG_NETFLOW_INFO_TYPE;
82 static const struct ng_parse_type ng_netflow_info_type = {
83 &ng_parse_struct_type,
84 &ng_netflow_info_type_fields
87 /* Parse type for struct ng_netflow_ifinfo */
88 static const struct ng_parse_struct_field ng_netflow_ifinfo_type_fields[]
89 = NG_NETFLOW_IFINFO_TYPE;
90 static const struct ng_parse_type ng_netflow_ifinfo_type = {
91 &ng_parse_struct_type,
92 &ng_netflow_ifinfo_type_fields
95 /* Parse type for struct ng_netflow_setdlt */
96 static const struct ng_parse_struct_field ng_netflow_setdlt_type_fields[]
97 = NG_NETFLOW_SETDLT_TYPE;
98 static const struct ng_parse_type ng_netflow_setdlt_type = {
99 &ng_parse_struct_type,
100 &ng_netflow_setdlt_type_fields
103 /* Parse type for ng_netflow_setifindex */
104 static const struct ng_parse_struct_field ng_netflow_setifindex_type_fields[]
105 = NG_NETFLOW_SETIFINDEX_TYPE;
106 static const struct ng_parse_type ng_netflow_setifindex_type = {
107 &ng_parse_struct_type,
108 &ng_netflow_setifindex_type_fields
111 /* Parse type for ng_netflow_settimeouts */
112 static const struct ng_parse_struct_field ng_netflow_settimeouts_type_fields[]
113 = NG_NETFLOW_SETTIMEOUTS_TYPE;
114 static const struct ng_parse_type ng_netflow_settimeouts_type = {
115 &ng_parse_struct_type,
116 &ng_netflow_settimeouts_type_fields
119 /* Parse type for ng_netflow_setconfig */
120 static const struct ng_parse_struct_field ng_netflow_setconfig_type_fields[]
121 = NG_NETFLOW_SETCONFIG_TYPE;
122 static const struct ng_parse_type ng_netflow_setconfig_type = {
123 &ng_parse_struct_type,
124 &ng_netflow_setconfig_type_fields
127 /* Parse type for ng_netflow_settemplate */
128 static const struct ng_parse_struct_field ng_netflow_settemplate_type_fields[]
129 = NG_NETFLOW_SETTEMPLATE_TYPE;
130 static const struct ng_parse_type ng_netflow_settemplate_type = {
131 &ng_parse_struct_type,
132 &ng_netflow_settemplate_type_fields
135 /* Parse type for ng_netflow_setmtu */
136 static const struct ng_parse_struct_field ng_netflow_setmtu_type_fields[]
137 = NG_NETFLOW_SETMTU_TYPE;
138 static const struct ng_parse_type ng_netflow_setmtu_type = {
139 &ng_parse_struct_type,
140 &ng_netflow_setmtu_type_fields
143 /* Parse type for struct ng_netflow_v9info */
144 static const struct ng_parse_struct_field ng_netflow_v9info_type_fields[]
145 = NG_NETFLOW_V9INFO_TYPE;
146 static const struct ng_parse_type ng_netflow_v9info_type = {
147 &ng_parse_struct_type,
148 &ng_netflow_v9info_type_fields
151 /* List of commands and how to convert arguments to/from ASCII */
152 static const struct ng_cmdlist ng_netflow_cmds[] = {
158 &ng_netflow_info_type
164 &ng_parse_uint16_type,
165 &ng_netflow_ifinfo_type
171 &ng_netflow_setdlt_type,
176 NGM_NETFLOW_SETIFINDEX,
178 &ng_netflow_setifindex_type,
183 NGM_NETFLOW_SETTIMEOUTS,
185 &ng_netflow_settimeouts_type,
190 NGM_NETFLOW_SETCONFIG,
192 &ng_netflow_setconfig_type,
197 NGM_NETFLOW_SETTEMPLATE,
199 &ng_netflow_settemplate_type,
206 &ng_netflow_setmtu_type,
214 &ng_netflow_v9info_type
220 /* Netgraph node type descriptor */
221 static struct ng_type ng_netflow_typestruct = {
222 .version = NG_ABI_VERSION,
223 .name = NG_NETFLOW_NODE_TYPE,
224 .constructor = ng_netflow_constructor,
225 .rcvmsg = ng_netflow_rcvmsg,
226 .close = ng_netflow_close,
227 .shutdown = ng_netflow_rmnode,
228 .newhook = ng_netflow_newhook,
229 .rcvdata = ng_netflow_rcvdata,
230 .disconnect = ng_netflow_disconnect,
231 .cmdlist = ng_netflow_cmds,
233 NETGRAPH_INIT(netflow, &ng_netflow_typestruct);
235 /* Called at node creation */
237 ng_netflow_constructor(node_p node)
242 /* Initialize private data */
243 priv = malloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
245 /* Initialize fib data */
246 priv->maxfibs = rt_numfibs;
247 priv->fib_data = malloc(sizeof(fib_export_p) * priv->maxfibs,
248 M_NETGRAPH, M_WAITOK | M_ZERO);
250 /* Make node and its data point at each other */
251 NG_NODE_SET_PRIVATE(node, priv);
254 /* Initialize timeouts to default values */
255 priv->nfinfo_inact_t = INACTIVE_TIMEOUT;
256 priv->nfinfo_act_t = ACTIVE_TIMEOUT;
258 /* Set default config */
259 for (i = 0; i < NG_NETFLOW_MAXIFACES; i++)
260 priv->ifaces[i].info.conf = NG_NETFLOW_CONF_INGRESS;
262 /* Initialize callout handle */
263 callout_init(&priv->exp_callout, 1);
265 /* Allocate memory and set up flow cache */
266 ng_netflow_cache_init(priv);
272 * ng_netflow supports two hooks: data and export.
273 * Incoming traffic is expected on data, and expired
274 * netflow datagrams are sent to export.
277 ng_netflow_newhook(node_p node, hook_p hook, const char *name)
279 const priv_p priv = NG_NODE_PRIVATE(node);
281 if (strncmp(name, NG_NETFLOW_HOOK_DATA, /* an iface hook? */
282 strlen(NG_NETFLOW_HOOK_DATA)) == 0) {
288 cp = name + strlen(NG_NETFLOW_HOOK_DATA);
289 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
292 ifnum = (int)strtoul(cp, &eptr, 10);
293 if (*eptr != '\0' || ifnum < 0 || ifnum >= NG_NETFLOW_MAXIFACES)
296 /* See if hook is already connected */
297 if (priv->ifaces[ifnum].hook != NULL)
300 iface = &priv->ifaces[ifnum];
302 /* Link private info and hook together */
303 NG_HOOK_SET_PRIVATE(hook, iface);
307 * In most cases traffic accounting is done on an
308 * Ethernet interface, so default data link type
309 * will be DLT_EN10MB.
311 iface->info.ifinfo_dlt = DLT_EN10MB;
313 } else if (strncmp(name, NG_NETFLOW_HOOK_OUT,
314 strlen(NG_NETFLOW_HOOK_OUT)) == 0) {
320 cp = name + strlen(NG_NETFLOW_HOOK_OUT);
321 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
324 ifnum = (int)strtoul(cp, &eptr, 10);
325 if (*eptr != '\0' || ifnum < 0 || ifnum >= NG_NETFLOW_MAXIFACES)
328 /* See if hook is already connected */
329 if (priv->ifaces[ifnum].out != NULL)
332 iface = &priv->ifaces[ifnum];
334 /* Link private info and hook together */
335 NG_HOOK_SET_PRIVATE(hook, iface);
338 } else if (strcmp(name, NG_NETFLOW_HOOK_EXPORT) == 0) {
340 if (priv->export != NULL)
343 /* Netflow version 5 supports 32-bit counters only */
344 if (CNTR_MAX == UINT64_MAX)
349 /* Exporter is ready. Let's schedule expiry. */
350 callout_reset(&priv->exp_callout, (1*hz), &ng_netflow_expire,
352 } else if (strcmp(name, NG_NETFLOW_HOOK_EXPORT9) == 0) {
354 if (priv->export9 != NULL)
357 priv->export9 = hook;
359 /* Exporter is ready. Let's schedule expiry. */
360 callout_reset(&priv->exp_callout, (1*hz), &ng_netflow_expire,
368 /* Get a netgraph control message. */
370 ng_netflow_rcvmsg (node_p node, item_p item, hook_p lasthook)
372 const priv_p priv = NG_NODE_PRIVATE(node);
373 struct ng_mesg *resp = NULL;
377 NGI_GET_MSG(item, msg);
379 /* Deal with message according to cookie and command */
380 switch (msg->header.typecookie) {
381 case NGM_NETFLOW_COOKIE:
382 switch (msg->header.cmd) {
383 case NGM_NETFLOW_INFO:
385 struct ng_netflow_info *i;
387 NG_MKRESPONSE(resp, msg, sizeof(struct ng_netflow_info),
389 i = (struct ng_netflow_info *)resp->data;
390 ng_netflow_copyinfo(priv, i);
394 case NGM_NETFLOW_IFINFO:
396 struct ng_netflow_ifinfo *i;
397 const uint16_t *index;
399 if (msg->header.arglen != sizeof(uint16_t))
402 index = (uint16_t *)msg->data;
403 if (*index >= NG_NETFLOW_MAXIFACES)
406 /* connected iface? */
407 if (priv->ifaces[*index].hook == NULL)
410 NG_MKRESPONSE(resp, msg,
411 sizeof(struct ng_netflow_ifinfo), M_NOWAIT);
412 i = (struct ng_netflow_ifinfo *)resp->data;
413 memcpy((void *)i, (void *)&priv->ifaces[*index].info,
414 sizeof(priv->ifaces[*index].info));
418 case NGM_NETFLOW_SETDLT:
420 struct ng_netflow_setdlt *set;
421 struct ng_netflow_iface *iface;
423 if (msg->header.arglen !=
424 sizeof(struct ng_netflow_setdlt))
427 set = (struct ng_netflow_setdlt *)msg->data;
428 if (set->iface >= NG_NETFLOW_MAXIFACES)
430 iface = &priv->ifaces[set->iface];
432 /* connected iface? */
433 if (iface->hook == NULL)
438 iface->info.ifinfo_dlt = DLT_EN10MB;
441 iface->info.ifinfo_dlt = DLT_RAW;
448 case NGM_NETFLOW_SETIFINDEX:
450 struct ng_netflow_setifindex *set;
451 struct ng_netflow_iface *iface;
453 if (msg->header.arglen !=
454 sizeof(struct ng_netflow_setifindex))
457 set = (struct ng_netflow_setifindex *)msg->data;
458 if (set->iface >= NG_NETFLOW_MAXIFACES)
460 iface = &priv->ifaces[set->iface];
462 /* connected iface? */
463 if (iface->hook == NULL)
466 iface->info.ifinfo_index = set->index;
470 case NGM_NETFLOW_SETTIMEOUTS:
472 struct ng_netflow_settimeouts *set;
474 if (msg->header.arglen !=
475 sizeof(struct ng_netflow_settimeouts))
478 set = (struct ng_netflow_settimeouts *)msg->data;
480 priv->nfinfo_inact_t = set->inactive_timeout;
481 priv->nfinfo_act_t = set->active_timeout;
485 case NGM_NETFLOW_SETCONFIG:
487 struct ng_netflow_setconfig *set;
489 if (msg->header.arglen !=
490 sizeof(struct ng_netflow_setconfig))
493 set = (struct ng_netflow_setconfig *)msg->data;
495 if (set->iface >= NG_NETFLOW_MAXIFACES)
498 priv->ifaces[set->iface].info.conf = set->conf;
502 case NGM_NETFLOW_SETTEMPLATE:
504 struct ng_netflow_settemplate *set;
506 if (msg->header.arglen !=
507 sizeof(struct ng_netflow_settemplate))
510 set = (struct ng_netflow_settemplate *)msg->data;
512 priv->templ_packets = set->packets;
513 priv->templ_time = set->time;
517 case NGM_NETFLOW_SETMTU:
519 struct ng_netflow_setmtu *set;
521 if (msg->header.arglen !=
522 sizeof(struct ng_netflow_setmtu))
525 set = (struct ng_netflow_setmtu *)msg->data;
526 if ((set->mtu < MIN_MTU) || (set->mtu > MAX_MTU))
529 priv->mtu = set->mtu;
533 case NGM_NETFLOW_SHOW:
534 if (msg->header.arglen !=
535 sizeof(struct ngnf_show_header))
538 NG_MKRESPONSE(resp, msg, NGRESP_SIZE, M_NOWAIT);
543 error = ng_netflow_flow_show(priv,
544 (struct ngnf_show_header *)msg->data,
545 (struct ngnf_show_header *)resp->data);
551 case NGM_NETFLOW_V9INFO:
553 struct ng_netflow_v9info *i;
555 NG_MKRESPONSE(resp, msg,
556 sizeof(struct ng_netflow_v9info), M_NOWAIT);
557 i = (struct ng_netflow_v9info *)resp->data;
558 ng_netflow_copyv9info(priv, i);
563 ERROUT(EINVAL); /* unknown command */
568 ERROUT(EINVAL); /* incorrect cookie */
573 * Take care of synchronous response, if any.
574 * Free memory and return.
577 NG_RESPOND_MSG(error, node, item, resp);
583 /* Receive data on hook. */
585 ng_netflow_rcvdata (hook_p hook, item_p item)
587 const node_p node = NG_HOOK_NODE(hook);
588 const priv_p priv = NG_NODE_PRIVATE(node);
589 const iface_p iface = NG_HOOK_PRIVATE(hook);
591 struct mbuf *m = NULL, *m_old = NULL;
592 struct ip *ip = NULL;
593 struct ip6_hdr *ip6 = NULL;
595 int pullup_len = 0, off;
596 uint8_t acct = 0, bypass = 0, flags = 0, upper_proto = 0;
597 int error = 0, l3_off = 0;
598 unsigned int src_if_index;
599 caddr_t upper_ptr = NULL;
603 if ((hook == priv->export) || (hook == priv->export9)) {
605 * Data arrived on export hook.
606 * This must not happen.
608 log(LOG_ERR, "ng_netflow: incoming data on export hook!\n");
612 if (hook == iface->hook) {
613 if ((iface->info.conf & NG_NETFLOW_CONF_INGRESS) == 0)
616 } else if (hook == iface->out) {
617 if ((iface->info.conf & NG_NETFLOW_CONF_EGRESS) == 0)
623 if ((!bypass) && (iface->info.conf &
624 (NG_NETFLOW_CONF_ONCE | NG_NETFLOW_CONF_THISONCE))) {
625 mtag = m_tag_locate(NGI_M(item), MTAG_NETFLOW,
626 MTAG_NETFLOW_CALLED, NULL);
627 while (mtag != NULL) {
628 if ((iface->info.conf & NG_NETFLOW_CONF_ONCE) ||
629 ((ng_ID_t *)(mtag + 1))[0] == NG_NODE_ID(node)) {
633 mtag = m_tag_locate(NGI_M(item), MTAG_NETFLOW,
634 MTAG_NETFLOW_CALLED, mtag);
642 NG_FWD_ITEM_HOOK(error, item, out);
646 if (iface->info.conf &
647 (NG_NETFLOW_CONF_ONCE | NG_NETFLOW_CONF_THISONCE)) {
648 mtag = m_tag_alloc(MTAG_NETFLOW, MTAG_NETFLOW_CALLED,
649 sizeof(ng_ID_t), M_NOWAIT);
651 ((ng_ID_t *)(mtag + 1))[0] = NG_NODE_ID(node);
652 m_tag_prepend(NGI_M(item), mtag);
656 /* Import configuration flags related to flow creation */
657 flags = iface->info.conf & NG_NETFLOW_FLOW_FLAGS;
662 /* Increase counters. */
663 iface->info.ifinfo_packets++;
666 * Depending on interface data link type and packet contents
667 * we pullup enough data, so that ng_netflow_flow_add() does not
668 * need to know about mbuf at all. We keep current length of data
669 * needed to be contiguous in pullup_len. mtod() is done at the
670 * very end one more time, since m can had changed after pulluping.
672 * In case of unrecognized data we don't return error, but just
673 * pass data to downstream hook, if it is available.
676 #define M_CHECK(length) do { \
677 pullup_len += length; \
678 if (((m)->m_pkthdr.len < (pullup_len)) || \
679 ((pullup_len) > MHLEN)) { \
683 if ((m)->m_len < (pullup_len) && \
684 (((m) = m_pullup((m),(pullup_len))) == NULL)) { \
690 switch (iface->info.ifinfo_dlt) {
691 case DLT_EN10MB: /* Ethernet */
693 struct ether_header *eh;
696 M_CHECK(sizeof(struct ether_header));
697 eh = mtod(m, struct ether_header *);
699 /* Make sure this is IP frame. */
700 etype = ntohs(eh->ether_type);
703 M_CHECK(sizeof(struct ip));
704 eh = mtod(m, struct ether_header *);
705 ip = (struct ip *)(eh + 1);
706 l3_off = sizeof(struct ether_header);
711 * m_pullup() called by M_CHECK() pullups
712 * kern.ipc.max_protohdr (default 60 bytes)
715 M_CHECK(sizeof(struct ip6_hdr));
716 eh = mtod(m, struct ether_header *);
717 ip6 = (struct ip6_hdr *)(eh + 1);
718 l3_off = sizeof(struct ether_header);
723 struct ether_vlan_header *evh;
725 M_CHECK(sizeof(struct ether_vlan_header) -
726 sizeof(struct ether_header));
727 evh = mtod(m, struct ether_vlan_header *);
728 etype = ntohs(evh->evl_proto);
729 l3_off = sizeof(struct ether_vlan_header);
731 if (etype == ETHERTYPE_IP) {
732 M_CHECK(sizeof(struct ip));
733 ip = (struct ip *)(evh + 1);
736 } else if (etype == ETHERTYPE_IPV6) {
737 M_CHECK(sizeof(struct ip6_hdr));
738 ip6 = (struct ip6_hdr *)(evh + 1);
744 goto bypass; /* pass this frame */
748 case DLT_RAW: /* IP packets */
749 M_CHECK(sizeof(struct ip));
750 ip = mtod(m, struct ip *);
751 /* l3_off is already zero */
754 * If INET6 is not defined IPv6 packets
755 * will be discarded in ng_netflow_flow_add().
757 if (ip->ip_v == IP6VERSION) {
759 M_CHECK(sizeof(struct ip6_hdr) - sizeof(struct ip));
760 ip6 = mtod(m, struct ip6_hdr *);
771 if ((ip != NULL) && ((ip->ip_off & htons(IP_OFFMASK)) == 0)) {
772 if ((ip->ip_v != IPVERSION) ||
773 ((ip->ip_hl << 2) < sizeof(struct ip)))
776 * In case of IPv4 header with options, we haven't pulled
779 M_CHECK((ip->ip_hl << 2) - sizeof(struct ip));
781 /* Save upper layer offset and proto */
783 upper_proto = ip->ip_p;
786 * XXX: in case of wrong upper layer header we will
787 * forward this packet but skip this record in netflow.
791 M_CHECK(sizeof(struct tcphdr));
794 M_CHECK(sizeof(struct udphdr));
797 M_CHECK(sizeof(struct sctphdr));
800 } else if (ip != NULL) {
802 * Nothing to save except upper layer proto,
803 * since this is a packet fragment.
805 flags |= NG_NETFLOW_IS_FRAG;
806 upper_proto = ip->ip_p;
807 if ((ip->ip_v != IPVERSION) ||
808 ((ip->ip_hl << 2) < sizeof(struct ip)))
811 } else if (ip6 != NULL) {
812 int cur = ip6->ip6_nxt, hdr_off = 0;
813 struct ip6_ext *ip6e;
814 struct ip6_frag *ip6f;
816 if (priv->export9 == NULL)
819 /* Save upper layer info. */
823 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
827 * Loop thru IPv6 extended headers to get upper
828 * layer header / frag.
833 * Same as in IPv4, we can forward a 'bad'
834 * packet without accounting.
837 M_CHECK(sizeof(struct tcphdr));
840 M_CHECK(sizeof(struct udphdr));
843 M_CHECK(sizeof(struct sctphdr));
846 /* Loop until 'real' upper layer headers */
847 case IPPROTO_HOPOPTS:
848 case IPPROTO_ROUTING:
849 case IPPROTO_DSTOPTS:
850 M_CHECK(sizeof(struct ip6_ext));
851 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) +
853 upper_proto = ip6e->ip6e_nxt;
854 hdr_off = (ip6e->ip6e_len + 1) << 3;
857 /* RFC4302, can be before DSTOPTS */
859 M_CHECK(sizeof(struct ip6_ext));
860 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) +
862 upper_proto = ip6e->ip6e_nxt;
863 hdr_off = (ip6e->ip6e_len + 2) << 2;
866 case IPPROTO_FRAGMENT:
867 M_CHECK(sizeof(struct ip6_frag));
868 ip6f = (struct ip6_frag *)(mtod(m, caddr_t) +
870 upper_proto = ip6f->ip6f_nxt;
871 hdr_off = sizeof(struct ip6_frag);
873 flags |= NG_NETFLOW_IS_FRAG;
881 * Any unknown header (new extension or IPv6/IPv4
882 * header for tunnels) ends loop.
898 /* Just in case of real reallocation in M_CHECK() / m_pullup() */
900 priv->nfinfo_realloc_mbuf++;
901 /* Restore ip/ipv6 pointer */
903 ip = (struct ip *)(mtod(m, caddr_t) + l3_off);
904 else if (ip6 != NULL)
905 ip6 = (struct ip6_hdr *)(mtod(m, caddr_t) + l3_off);
908 upper_ptr = (caddr_t)(mtod(m, caddr_t) + off);
910 /* Determine packet input interface. Prefer configured. */
912 if (hook == iface->out || iface->info.ifinfo_index == 0) {
913 if (m->m_pkthdr.rcvif != NULL)
914 src_if_index = m->m_pkthdr.rcvif->if_index;
916 src_if_index = iface->info.ifinfo_index;
918 /* Check packet FIB */
920 if (fib >= priv->maxfibs) {
921 CTR2(KTR_NET, "ng_netflow_rcvdata(): packet fib %d is out of "
922 "range of available fibs: 0 .. %d",
927 if ((fe = priv_to_fib(priv, fib)) == NULL) {
929 if (ng_netflow_fib_init(priv, fib) != 0) {
930 /* malloc() failed */
934 fe = priv_to_fib(priv, fib);
938 error = ng_netflow_flow_add(priv, fe, ip, upper_ptr,
939 upper_proto, flags, src_if_index);
941 else if (ip6 != NULL)
942 error = ng_netflow_flow6_add(priv, fe, ip6, upper_ptr,
943 upper_proto, flags, src_if_index);
952 /* Accounting failure */
954 counter_u64_add(priv->nfinfo_spackets, 1);
955 counter_u64_add(priv->nfinfo_sbytes,
957 } else if (ip6 != NULL) {
958 counter_u64_add(priv->nfinfo_spackets6, 1);
959 counter_u64_add(priv->nfinfo_sbytes6,
964 /* XXX: error gets overwritten here */
965 NG_FWD_NEW_DATA(error, item, out, m);
977 /* We will be shut down in a moment */
979 ng_netflow_close(node_p node)
981 const priv_p priv = NG_NODE_PRIVATE(node);
983 callout_drain(&priv->exp_callout);
984 ng_netflow_cache_flush(priv);
989 /* Do local shutdown processing. */
991 ng_netflow_rmnode(node_p node)
993 const priv_p priv = NG_NODE_PRIVATE(node);
995 NG_NODE_SET_PRIVATE(node, NULL);
996 NG_NODE_UNREF(priv->node);
998 free(priv->fib_data, M_NETGRAPH);
999 free(priv, M_NETGRAPH);
1004 /* Hook disconnection. */
1006 ng_netflow_disconnect(hook_p hook)
1008 node_p node = NG_HOOK_NODE(hook);
1009 priv_p priv = NG_NODE_PRIVATE(node);
1010 iface_p iface = NG_HOOK_PRIVATE(hook);
1012 if (iface != NULL) {
1013 if (iface->hook == hook)
1015 if (iface->out == hook)
1019 /* if export hook disconnected stop running expire(). */
1020 if (hook == priv->export) {
1021 if (priv->export9 == NULL)
1022 callout_drain(&priv->exp_callout);
1023 priv->export = NULL;
1026 if (hook == priv->export9) {
1027 if (priv->export == NULL)
1028 callout_drain(&priv->exp_callout);
1029 priv->export9 = NULL;
1032 /* Removal of the last link destroys the node. */
1033 if (NG_NODE_NUMHOOKS(node) == 0)
1034 ng_rmnode_self(node);