2 * SPDX-License-Identifier: BSD-2-Clause
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>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
30 * $SourceForge: ng_netflow.c,v 1.30 2004/09/05 11:37:43 glebius Exp $
33 #include <sys/cdefs.h>
35 #include "opt_inet6.h"
36 #include "opt_route.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/counter.h>
41 #include <sys/kernel.h>
43 #include <sys/limits.h>
44 #include <sys/malloc.h>
46 #include <sys/socket.h>
47 #include <sys/syslog.h>
48 #include <sys/ctype.h>
52 #include <net/ethernet.h>
53 #include <net/route.h>
54 #include <net/if_arp.h>
55 #include <net/if_var.h>
56 #include <net/if_private.h>
57 #include <net/if_vlan_var.h>
59 #include <netinet/in.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 #include <netinet/sctp.h>
67 #include <netgraph/ng_message.h>
68 #include <netgraph/ng_parse.h>
69 #include <netgraph/netgraph.h>
70 #include <netgraph/netflow/netflow.h>
71 #include <netgraph/netflow/netflow_v9.h>
72 #include <netgraph/netflow/ng_netflow.h>
74 /* Netgraph methods */
75 static ng_constructor_t ng_netflow_constructor;
76 static ng_rcvmsg_t ng_netflow_rcvmsg;
77 static ng_close_t ng_netflow_close;
78 static ng_shutdown_t ng_netflow_rmnode;
79 static ng_newhook_t ng_netflow_newhook;
80 static ng_rcvdata_t ng_netflow_rcvdata;
81 static ng_disconnect_t ng_netflow_disconnect;
83 /* Parse type for struct ng_netflow_info */
84 static const struct ng_parse_struct_field ng_netflow_info_type_fields[]
85 = NG_NETFLOW_INFO_TYPE;
86 static const struct ng_parse_type ng_netflow_info_type = {
87 &ng_parse_struct_type,
88 &ng_netflow_info_type_fields
91 /* Parse type for struct ng_netflow_ifinfo */
92 static const struct ng_parse_struct_field ng_netflow_ifinfo_type_fields[]
93 = NG_NETFLOW_IFINFO_TYPE;
94 static const struct ng_parse_type ng_netflow_ifinfo_type = {
95 &ng_parse_struct_type,
96 &ng_netflow_ifinfo_type_fields
99 /* Parse type for struct ng_netflow_setdlt */
100 static const struct ng_parse_struct_field ng_netflow_setdlt_type_fields[]
101 = NG_NETFLOW_SETDLT_TYPE;
102 static const struct ng_parse_type ng_netflow_setdlt_type = {
103 &ng_parse_struct_type,
104 &ng_netflow_setdlt_type_fields
107 /* Parse type for ng_netflow_setifindex */
108 static const struct ng_parse_struct_field ng_netflow_setifindex_type_fields[]
109 = NG_NETFLOW_SETIFINDEX_TYPE;
110 static const struct ng_parse_type ng_netflow_setifindex_type = {
111 &ng_parse_struct_type,
112 &ng_netflow_setifindex_type_fields
115 /* Parse type for ng_netflow_settimeouts */
116 static const struct ng_parse_struct_field ng_netflow_settimeouts_type_fields[]
117 = NG_NETFLOW_SETTIMEOUTS_TYPE;
118 static const struct ng_parse_type ng_netflow_settimeouts_type = {
119 &ng_parse_struct_type,
120 &ng_netflow_settimeouts_type_fields
123 /* Parse type for ng_netflow_setconfig */
124 static const struct ng_parse_struct_field ng_netflow_setconfig_type_fields[]
125 = NG_NETFLOW_SETCONFIG_TYPE;
126 static const struct ng_parse_type ng_netflow_setconfig_type = {
127 &ng_parse_struct_type,
128 &ng_netflow_setconfig_type_fields
131 /* Parse type for ng_netflow_settemplate */
132 static const struct ng_parse_struct_field ng_netflow_settemplate_type_fields[]
133 = NG_NETFLOW_SETTEMPLATE_TYPE;
134 static const struct ng_parse_type ng_netflow_settemplate_type = {
135 &ng_parse_struct_type,
136 &ng_netflow_settemplate_type_fields
139 /* Parse type for ng_netflow_setmtu */
140 static const struct ng_parse_struct_field ng_netflow_setmtu_type_fields[]
141 = NG_NETFLOW_SETMTU_TYPE;
142 static const struct ng_parse_type ng_netflow_setmtu_type = {
143 &ng_parse_struct_type,
144 &ng_netflow_setmtu_type_fields
147 /* Parse type for struct ng_netflow_v9info */
148 static const struct ng_parse_struct_field ng_netflow_v9info_type_fields[]
149 = NG_NETFLOW_V9INFO_TYPE;
150 static const struct ng_parse_type ng_netflow_v9info_type = {
151 &ng_parse_struct_type,
152 &ng_netflow_v9info_type_fields
155 /* List of commands and how to convert arguments to/from ASCII */
156 static const struct ng_cmdlist ng_netflow_cmds[] = {
162 &ng_netflow_info_type
168 &ng_parse_uint16_type,
169 &ng_netflow_ifinfo_type
175 &ng_netflow_setdlt_type,
180 NGM_NETFLOW_SETIFINDEX,
182 &ng_netflow_setifindex_type,
187 NGM_NETFLOW_SETTIMEOUTS,
189 &ng_netflow_settimeouts_type,
194 NGM_NETFLOW_SETCONFIG,
196 &ng_netflow_setconfig_type,
201 NGM_NETFLOW_SETTEMPLATE,
203 &ng_netflow_settemplate_type,
210 &ng_netflow_setmtu_type,
218 &ng_netflow_v9info_type
223 /* Netgraph node type descriptor */
224 static struct ng_type ng_netflow_typestruct = {
225 .version = NG_ABI_VERSION,
226 .name = NG_NETFLOW_NODE_TYPE,
227 .constructor = ng_netflow_constructor,
228 .rcvmsg = ng_netflow_rcvmsg,
229 .close = ng_netflow_close,
230 .shutdown = ng_netflow_rmnode,
231 .newhook = ng_netflow_newhook,
232 .rcvdata = ng_netflow_rcvdata,
233 .disconnect = ng_netflow_disconnect,
234 .cmdlist = ng_netflow_cmds,
236 NETGRAPH_INIT(netflow, &ng_netflow_typestruct);
238 /* Called at node creation */
240 ng_netflow_constructor(node_p node)
245 /* Initialize private data */
246 priv = malloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
248 /* Initialize fib data */
249 priv->maxfibs = rt_numfibs;
250 priv->fib_data = malloc(sizeof(fib_export_p) * priv->maxfibs,
251 M_NETGRAPH, M_WAITOK | M_ZERO);
253 /* Make node and its data point at each other */
254 NG_NODE_SET_PRIVATE(node, priv);
257 /* Initialize timeouts to default values */
258 priv->nfinfo_inact_t = INACTIVE_TIMEOUT;
259 priv->nfinfo_act_t = ACTIVE_TIMEOUT;
261 /* Set default config */
262 for (i = 0; i < NG_NETFLOW_MAXIFACES; i++)
263 priv->ifaces[i].info.conf = NG_NETFLOW_CONF_INGRESS;
265 /* Initialize callout handle */
266 callout_init(&priv->exp_callout, 1);
268 /* Allocate memory and set up flow cache */
269 ng_netflow_cache_init(priv);
275 * ng_netflow supports two hooks: data and export.
276 * Incoming traffic is expected on data, and expired
277 * netflow datagrams are sent to export.
280 ng_netflow_newhook(node_p node, hook_p hook, const char *name)
282 const priv_p priv = NG_NODE_PRIVATE(node);
284 if (strncmp(name, NG_NETFLOW_HOOK_DATA, /* an iface hook? */
285 strlen(NG_NETFLOW_HOOK_DATA)) == 0) {
291 cp = name + strlen(NG_NETFLOW_HOOK_DATA);
292 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
295 ifnum = (int)strtoul(cp, &eptr, 10);
296 if (*eptr != '\0' || ifnum < 0 || ifnum >= NG_NETFLOW_MAXIFACES)
299 /* See if hook is already connected */
300 if (priv->ifaces[ifnum].hook != NULL)
303 iface = &priv->ifaces[ifnum];
305 /* Link private info and hook together */
306 NG_HOOK_SET_PRIVATE(hook, iface);
310 * In most cases traffic accounting is done on an
311 * Ethernet interface, so default data link type
312 * will be DLT_EN10MB.
314 iface->info.ifinfo_dlt = DLT_EN10MB;
316 } else if (strncmp(name, NG_NETFLOW_HOOK_OUT,
317 strlen(NG_NETFLOW_HOOK_OUT)) == 0) {
323 cp = name + strlen(NG_NETFLOW_HOOK_OUT);
324 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
327 ifnum = (int)strtoul(cp, &eptr, 10);
328 if (*eptr != '\0' || ifnum < 0 || ifnum >= NG_NETFLOW_MAXIFACES)
331 /* See if hook is already connected */
332 if (priv->ifaces[ifnum].out != NULL)
335 iface = &priv->ifaces[ifnum];
337 /* Link private info and hook together */
338 NG_HOOK_SET_PRIVATE(hook, iface);
341 } else if (strcmp(name, NG_NETFLOW_HOOK_EXPORT) == 0) {
342 if (priv->export != NULL)
345 /* Netflow version 5 supports 32-bit counters only */
346 if (CNTR_MAX == UINT64_MAX)
351 /* Exporter is ready. Let's schedule expiry. */
352 callout_reset(&priv->exp_callout, (1*hz), &ng_netflow_expire,
354 } else if (strcmp(name, NG_NETFLOW_HOOK_EXPORT9) == 0) {
355 if (priv->export9 != NULL)
358 priv->export9 = hook;
360 /* Exporter is ready. Let's schedule expiry. */
361 callout_reset(&priv->exp_callout, (1*hz), &ng_netflow_expire,
369 /* Get a netgraph control message. */
371 ng_netflow_rcvmsg (node_p node, item_p item, hook_p lasthook)
373 const priv_p priv = NG_NODE_PRIVATE(node);
374 struct ng_mesg *resp = NULL;
378 NGI_GET_MSG(item, msg);
380 /* Deal with message according to cookie and command */
381 switch (msg->header.typecookie) {
382 case NGM_NETFLOW_COOKIE:
383 switch (msg->header.cmd) {
384 case NGM_NETFLOW_INFO:
386 struct ng_netflow_info *i;
388 NG_MKRESPONSE(resp, msg, sizeof(struct ng_netflow_info),
390 i = (struct ng_netflow_info *)resp->data;
391 ng_netflow_copyinfo(priv, i);
395 case NGM_NETFLOW_IFINFO:
397 struct ng_netflow_ifinfo *i;
398 const uint16_t *index;
400 if (msg->header.arglen != sizeof(uint16_t))
403 index = (uint16_t *)msg->data;
404 if (*index >= NG_NETFLOW_MAXIFACES)
407 /* connected iface? */
408 if (priv->ifaces[*index].hook == NULL)
411 NG_MKRESPONSE(resp, msg,
412 sizeof(struct ng_netflow_ifinfo), M_NOWAIT);
413 i = (struct ng_netflow_ifinfo *)resp->data;
414 memcpy((void *)i, (void *)&priv->ifaces[*index].info,
415 sizeof(priv->ifaces[*index].info));
419 case NGM_NETFLOW_SETDLT:
421 struct ng_netflow_setdlt *set;
422 struct ng_netflow_iface *iface;
424 if (msg->header.arglen !=
425 sizeof(struct ng_netflow_setdlt))
428 set = (struct ng_netflow_setdlt *)msg->data;
429 if (set->iface >= NG_NETFLOW_MAXIFACES)
431 iface = &priv->ifaces[set->iface];
433 /* connected iface? */
434 if (iface->hook == NULL)
439 iface->info.ifinfo_dlt = DLT_EN10MB;
442 iface->info.ifinfo_dlt = DLT_RAW;
449 case NGM_NETFLOW_SETIFINDEX:
451 struct ng_netflow_setifindex *set;
452 struct ng_netflow_iface *iface;
454 if (msg->header.arglen !=
455 sizeof(struct ng_netflow_setifindex))
458 set = (struct ng_netflow_setifindex *)msg->data;
459 if (set->iface >= NG_NETFLOW_MAXIFACES)
461 iface = &priv->ifaces[set->iface];
463 /* connected iface? */
464 if (iface->hook == NULL)
467 iface->info.ifinfo_index = set->index;
471 case NGM_NETFLOW_SETTIMEOUTS:
473 struct ng_netflow_settimeouts *set;
475 if (msg->header.arglen !=
476 sizeof(struct ng_netflow_settimeouts))
479 set = (struct ng_netflow_settimeouts *)msg->data;
481 priv->nfinfo_inact_t = set->inactive_timeout;
482 priv->nfinfo_act_t = set->active_timeout;
486 case NGM_NETFLOW_SETCONFIG:
488 struct ng_netflow_setconfig *set;
490 if (msg->header.arglen !=
491 sizeof(struct ng_netflow_setconfig))
494 set = (struct ng_netflow_setconfig *)msg->data;
496 if (set->iface >= NG_NETFLOW_MAXIFACES)
499 priv->ifaces[set->iface].info.conf = set->conf;
503 case NGM_NETFLOW_SETTEMPLATE:
505 struct ng_netflow_settemplate *set;
507 if (msg->header.arglen !=
508 sizeof(struct ng_netflow_settemplate))
511 set = (struct ng_netflow_settemplate *)msg->data;
513 priv->templ_packets = set->packets;
514 priv->templ_time = set->time;
518 case NGM_NETFLOW_SETMTU:
520 struct ng_netflow_setmtu *set;
522 if (msg->header.arglen !=
523 sizeof(struct ng_netflow_setmtu))
526 set = (struct ng_netflow_setmtu *)msg->data;
527 if ((set->mtu < MIN_MTU) || (set->mtu > MAX_MTU))
530 priv->mtu = set->mtu;
534 case NGM_NETFLOW_SHOW:
535 if (msg->header.arglen !=
536 sizeof(struct ngnf_show_header))
539 NG_MKRESPONSE(resp, msg, NGRESP_SIZE, M_NOWAIT);
544 error = ng_netflow_flow_show(priv,
545 (struct ngnf_show_header *)msg->data,
546 (struct ngnf_show_header *)resp->data);
552 case NGM_NETFLOW_V9INFO:
554 struct ng_netflow_v9info *i;
556 NG_MKRESPONSE(resp, msg,
557 sizeof(struct ng_netflow_v9info), M_NOWAIT);
558 i = (struct ng_netflow_v9info *)resp->data;
559 ng_netflow_copyv9info(priv, i);
564 ERROUT(EINVAL); /* unknown command */
569 ERROUT(EINVAL); /* incorrect cookie */
574 * Take care of synchronous response, if any.
575 * Free memory and return.
578 NG_RESPOND_MSG(error, node, item, resp);
584 /* Receive data on hook. */
586 ng_netflow_rcvdata (hook_p hook, item_p item)
588 const node_p node = NG_HOOK_NODE(hook);
589 const priv_p priv = NG_NODE_PRIVATE(node);
590 const iface_p iface = NG_HOOK_PRIVATE(hook);
592 struct mbuf *m = NULL, *m_old = NULL;
593 struct ip *ip = NULL;
594 struct ip6_hdr *ip6 = NULL;
597 uint8_t acct = 0, bypass = 0;
598 int error = 0, l3_off = 0;
599 #if defined(INET) || defined(INET6)
601 uint8_t flags = 0, upper_proto = 0;
602 unsigned int src_if_index;
603 caddr_t upper_ptr = NULL;
608 if ((hook == priv->export) || (hook == priv->export9)) {
610 * Data arrived on export hook.
611 * This must not happen.
613 log(LOG_ERR, "ng_netflow: incoming data on export hook!\n");
617 if (hook == iface->hook) {
618 if ((iface->info.conf & NG_NETFLOW_CONF_INGRESS) == 0)
621 } else if (hook == iface->out) {
622 if ((iface->info.conf & NG_NETFLOW_CONF_EGRESS) == 0)
628 if ((!bypass) && (iface->info.conf &
629 (NG_NETFLOW_CONF_ONCE | NG_NETFLOW_CONF_THISONCE))) {
630 mtag = m_tag_locate(NGI_M(item), MTAG_NETFLOW,
631 MTAG_NETFLOW_CALLED, NULL);
632 while (mtag != NULL) {
633 if ((iface->info.conf & NG_NETFLOW_CONF_ONCE) ||
634 ((ng_ID_t *)(mtag + 1))[0] == NG_NODE_ID(node)) {
638 mtag = m_tag_locate(NGI_M(item), MTAG_NETFLOW,
639 MTAG_NETFLOW_CALLED, mtag);
647 NG_FWD_ITEM_HOOK(error, item, out);
651 if (iface->info.conf &
652 (NG_NETFLOW_CONF_ONCE | NG_NETFLOW_CONF_THISONCE)) {
653 mtag = m_tag_alloc(MTAG_NETFLOW, MTAG_NETFLOW_CALLED,
654 sizeof(ng_ID_t), M_NOWAIT);
656 ((ng_ID_t *)(mtag + 1))[0] = NG_NODE_ID(node);
657 m_tag_prepend(NGI_M(item), mtag);
661 #if defined(INET) || defined(INET6)
662 /* Import configuration flags related to flow creation */
663 flags = iface->info.conf & NG_NETFLOW_FLOW_FLAGS;
669 /* Increase counters. */
670 iface->info.ifinfo_packets++;
673 * Depending on interface data link type and packet contents
674 * we pullup enough data, so that ng_netflow_flow_add() does not
675 * need to know about mbuf at all. We keep current length of data
676 * needed to be contiguous in pullup_len. mtod() is done at the
677 * very end one more time, since m can had changed after pulluping.
679 * In case of unrecognized data we don't return error, but just
680 * pass data to downstream hook, if it is available.
683 #define M_CHECK(length) do { \
684 pullup_len += length; \
685 if (((m)->m_pkthdr.len < (pullup_len)) || \
686 ((pullup_len) > MHLEN)) { \
690 if ((m)->m_len < (pullup_len) && \
691 (((m) = m_pullup((m),(pullup_len))) == NULL)) { \
697 switch (iface->info.ifinfo_dlt) {
698 case DLT_EN10MB: /* Ethernet */
700 struct ether_header *eh;
703 M_CHECK(sizeof(struct ether_header));
704 eh = mtod(m, struct ether_header *);
706 /* Make sure this is IP frame. */
707 etype = ntohs(eh->ether_type);
711 M_CHECK(sizeof(struct ip));
712 eh = mtod(m, struct ether_header *);
713 ip = (struct ip *)(eh + 1);
714 l3_off = sizeof(struct ether_header);
720 * m_pullup() called by M_CHECK() pullups
721 * kern.ipc.max_protohdr (default 60 bytes)
724 M_CHECK(sizeof(struct ip6_hdr));
725 eh = mtod(m, struct ether_header *);
726 ip6 = (struct ip6_hdr *)(eh + 1);
727 l3_off = sizeof(struct ether_header);
732 struct ether_vlan_header *evh;
734 M_CHECK(sizeof(struct ether_vlan_header) -
735 sizeof(struct ether_header));
736 evh = mtod(m, struct ether_vlan_header *);
737 etype = ntohs(evh->evl_proto);
738 l3_off = sizeof(struct ether_vlan_header);
740 if (etype == ETHERTYPE_IP) {
742 M_CHECK(sizeof(struct ip));
743 ip = (struct ip *)(evh + 1);
747 } else if (etype == ETHERTYPE_IPV6) {
748 M_CHECK(sizeof(struct ip6_hdr));
749 ip6 = (struct ip6_hdr *)(evh + 1);
755 goto bypass; /* pass this frame */
759 case DLT_RAW: /* IP packets */
760 M_CHECK(sizeof(struct ip));
761 ip = mtod(m, struct ip *);
762 /* l3_off is already zero */
765 * If INET6 is not defined IPv6 packets
766 * will be discarded in ng_netflow_flow_add().
768 if (ip->ip_v == IP6VERSION) {
770 M_CHECK(sizeof(struct ip6_hdr) - sizeof(struct ip));
771 ip6 = mtod(m, struct ip6_hdr *);
783 #if defined(INET) || defined(INET6)
787 if ((ip != NULL) && ((ip->ip_off & htons(IP_OFFMASK)) == 0)) {
788 if ((ip->ip_v != IPVERSION) ||
789 ((ip->ip_hl << 2) < sizeof(struct ip)))
792 * In case of IPv4 header with options, we haven't pulled
795 M_CHECK((ip->ip_hl << 2) - sizeof(struct ip));
797 #if defined(INET) || defined(INET6)
798 /* Save upper layer offset and proto */
800 upper_proto = ip->ip_p;
804 * XXX: in case of wrong upper layer header we will
805 * forward this packet but skip this record in netflow.
809 M_CHECK(sizeof(struct tcphdr));
812 M_CHECK(sizeof(struct udphdr));
815 M_CHECK(sizeof(struct sctphdr));
818 } else if (ip != NULL) {
820 * Nothing to save except upper layer proto,
821 * since this is a packet fragment.
823 #if defined(INET) || defined(INET6)
824 flags |= NG_NETFLOW_IS_FRAG;
825 upper_proto = ip->ip_p;
827 if ((ip->ip_v != IPVERSION) ||
828 ((ip->ip_hl << 2) < sizeof(struct ip)))
831 } else if (ip6 != NULL) {
832 int cur = ip6->ip6_nxt, hdr_off = 0;
833 struct ip6_ext *ip6e;
834 struct ip6_frag *ip6f;
836 if (priv->export9 == NULL)
839 /* Save upper layer info. */
843 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
847 * Loop through IPv6 extended headers to get upper
848 * layer header / frag.
853 * Same as in IPv4, we can forward a 'bad'
854 * packet without accounting.
857 M_CHECK(sizeof(struct tcphdr));
860 M_CHECK(sizeof(struct udphdr));
863 M_CHECK(sizeof(struct sctphdr));
866 /* Loop until 'real' upper layer headers */
867 case IPPROTO_HOPOPTS:
868 case IPPROTO_ROUTING:
869 case IPPROTO_DSTOPTS:
870 M_CHECK(sizeof(struct ip6_ext));
871 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) +
873 upper_proto = ip6e->ip6e_nxt;
874 hdr_off = (ip6e->ip6e_len + 1) << 3;
877 /* RFC4302, can be before DSTOPTS */
879 M_CHECK(sizeof(struct ip6_ext));
880 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) +
882 upper_proto = ip6e->ip6e_nxt;
883 hdr_off = (ip6e->ip6e_len + 2) << 2;
886 case IPPROTO_FRAGMENT:
887 M_CHECK(sizeof(struct ip6_frag));
888 ip6f = (struct ip6_frag *)(mtod(m, caddr_t) +
890 upper_proto = ip6f->ip6f_nxt;
891 hdr_off = sizeof(struct ip6_frag);
893 flags |= NG_NETFLOW_IS_FRAG;
901 * Any unknown header (new extension or IPv6/IPv4
902 * header for tunnels) ends loop.
918 /* Just in case of real reallocation in M_CHECK() / m_pullup() */
920 priv->nfinfo_realloc_mbuf++;
921 /* Restore ip/ipv6 pointer */
923 ip = (struct ip *)(mtod(m, caddr_t) + l3_off);
924 else if (ip6 != NULL)
925 ip6 = (struct ip6_hdr *)(mtod(m, caddr_t) + l3_off);
928 #if defined(INET) || defined(INET6)
929 upper_ptr = (caddr_t)(mtod(m, caddr_t) + off);
931 /* Determine packet input interface. Prefer configured. */
933 if (hook == iface->out || iface->info.ifinfo_index == 0) {
934 if (m->m_pkthdr.rcvif != NULL)
935 src_if_index = m->m_pkthdr.rcvif->if_index;
937 src_if_index = iface->info.ifinfo_index;
940 /* Check packet FIB */
942 if (fib >= priv->maxfibs) {
943 CTR2(KTR_NET, "ng_netflow_rcvdata(): packet fib %d is out of "
944 "range of available fibs: 0 .. %d",
949 if ((fe = priv_to_fib(priv, fib)) == NULL) {
951 if (ng_netflow_fib_init(priv, fib) != 0) {
952 /* malloc() failed */
956 fe = priv_to_fib(priv, fib);
961 error = ng_netflow_flow_add(priv, fe, ip, upper_ptr,
962 upper_proto, flags, src_if_index);
964 #if defined(INET6) && defined(INET)
969 error = ng_netflow_flow6_add(priv, fe, ip6, upper_ptr,
970 upper_proto, flags, src_if_index);
979 /* Accounting failure */
981 counter_u64_add(priv->nfinfo_spackets, 1);
982 counter_u64_add(priv->nfinfo_sbytes,
984 } else if (ip6 != NULL) {
985 counter_u64_add(priv->nfinfo_spackets6, 1);
986 counter_u64_add(priv->nfinfo_sbytes6,
991 /* XXX: error gets overwritten here */
992 NG_FWD_NEW_DATA(error, item, out, m);
1004 /* We will be shut down in a moment */
1006 ng_netflow_close(node_p node)
1008 const priv_p priv = NG_NODE_PRIVATE(node);
1010 callout_drain(&priv->exp_callout);
1011 ng_netflow_cache_flush(priv);
1016 /* Do local shutdown processing. */
1018 ng_netflow_rmnode(node_p node)
1020 const priv_p priv = NG_NODE_PRIVATE(node);
1022 NG_NODE_SET_PRIVATE(node, NULL);
1023 NG_NODE_UNREF(priv->node);
1025 free(priv->fib_data, M_NETGRAPH);
1026 free(priv, M_NETGRAPH);
1031 /* Hook disconnection. */
1033 ng_netflow_disconnect(hook_p hook)
1035 node_p node = NG_HOOK_NODE(hook);
1036 priv_p priv = NG_NODE_PRIVATE(node);
1037 iface_p iface = NG_HOOK_PRIVATE(hook);
1039 if (iface != NULL) {
1040 if (iface->hook == hook)
1042 if (iface->out == hook)
1046 /* if export hook disconnected stop running expire(). */
1047 if (hook == priv->export) {
1048 if (priv->export9 == NULL)
1049 callout_drain(&priv->exp_callout);
1050 priv->export = NULL;
1053 if (hook == priv->export9) {
1054 if (priv->export == NULL)
1055 callout_drain(&priv->exp_callout);
1056 priv->export9 = NULL;
1059 /* Removal of the last link destroys the node. */
1060 if (NG_NODE_NUMHOOKS(node) == 0)
1061 ng_rmnode_self(node);