2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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>
34 __FBSDID("$FreeBSD$");
36 #include "opt_inet6.h"
37 #include "opt_route.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/counter.h>
42 #include <sys/kernel.h>
44 #include <sys/limits.h>
45 #include <sys/malloc.h>
47 #include <sys/socket.h>
48 #include <sys/syslog.h>
49 #include <sys/ctype.h>
53 #include <net/ethernet.h>
54 #include <net/route.h>
55 #include <net/if_arp.h>
56 #include <net/if_var.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
224 /* Netgraph node type descriptor */
225 static struct ng_type ng_netflow_typestruct = {
226 .version = NG_ABI_VERSION,
227 .name = NG_NETFLOW_NODE_TYPE,
228 .constructor = ng_netflow_constructor,
229 .rcvmsg = ng_netflow_rcvmsg,
230 .close = ng_netflow_close,
231 .shutdown = ng_netflow_rmnode,
232 .newhook = ng_netflow_newhook,
233 .rcvdata = ng_netflow_rcvdata,
234 .disconnect = ng_netflow_disconnect,
235 .cmdlist = ng_netflow_cmds,
237 NETGRAPH_INIT(netflow, &ng_netflow_typestruct);
239 /* Called at node creation */
241 ng_netflow_constructor(node_p node)
246 /* Initialize private data */
247 priv = malloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
249 /* Initialize fib data */
250 priv->maxfibs = rt_numfibs;
251 priv->fib_data = malloc(sizeof(fib_export_p) * priv->maxfibs,
252 M_NETGRAPH, M_WAITOK | M_ZERO);
254 /* Make node and its data point at each other */
255 NG_NODE_SET_PRIVATE(node, priv);
258 /* Initialize timeouts to default values */
259 priv->nfinfo_inact_t = INACTIVE_TIMEOUT;
260 priv->nfinfo_act_t = ACTIVE_TIMEOUT;
262 /* Set default config */
263 for (i = 0; i < NG_NETFLOW_MAXIFACES; i++)
264 priv->ifaces[i].info.conf = NG_NETFLOW_CONF_INGRESS;
266 /* Initialize callout handle */
267 callout_init(&priv->exp_callout, 1);
269 /* Allocate memory and set up flow cache */
270 ng_netflow_cache_init(priv);
276 * ng_netflow supports two hooks: data and export.
277 * Incoming traffic is expected on data, and expired
278 * netflow datagrams are sent to export.
281 ng_netflow_newhook(node_p node, hook_p hook, const char *name)
283 const priv_p priv = NG_NODE_PRIVATE(node);
285 if (strncmp(name, NG_NETFLOW_HOOK_DATA, /* an iface hook? */
286 strlen(NG_NETFLOW_HOOK_DATA)) == 0) {
292 cp = name + strlen(NG_NETFLOW_HOOK_DATA);
293 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
296 ifnum = (int)strtoul(cp, &eptr, 10);
297 if (*eptr != '\0' || ifnum < 0 || ifnum >= NG_NETFLOW_MAXIFACES)
300 /* See if hook is already connected */
301 if (priv->ifaces[ifnum].hook != NULL)
304 iface = &priv->ifaces[ifnum];
306 /* Link private info and hook together */
307 NG_HOOK_SET_PRIVATE(hook, iface);
311 * In most cases traffic accounting is done on an
312 * Ethernet interface, so default data link type
313 * will be DLT_EN10MB.
315 iface->info.ifinfo_dlt = DLT_EN10MB;
317 } else if (strncmp(name, NG_NETFLOW_HOOK_OUT,
318 strlen(NG_NETFLOW_HOOK_OUT)) == 0) {
324 cp = name + strlen(NG_NETFLOW_HOOK_OUT);
325 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
328 ifnum = (int)strtoul(cp, &eptr, 10);
329 if (*eptr != '\0' || ifnum < 0 || ifnum >= NG_NETFLOW_MAXIFACES)
332 /* See if hook is already connected */
333 if (priv->ifaces[ifnum].out != NULL)
336 iface = &priv->ifaces[ifnum];
338 /* Link private info and hook together */
339 NG_HOOK_SET_PRIVATE(hook, iface);
342 } else if (strcmp(name, NG_NETFLOW_HOOK_EXPORT) == 0) {
344 if (priv->export != NULL)
347 /* Netflow version 5 supports 32-bit counters only */
348 if (CNTR_MAX == UINT64_MAX)
353 /* Exporter is ready. Let's schedule expiry. */
354 callout_reset(&priv->exp_callout, (1*hz), &ng_netflow_expire,
356 } else if (strcmp(name, NG_NETFLOW_HOOK_EXPORT9) == 0) {
358 if (priv->export9 != NULL)
361 priv->export9 = hook;
363 /* Exporter is ready. Let's schedule expiry. */
364 callout_reset(&priv->exp_callout, (1*hz), &ng_netflow_expire,
372 /* Get a netgraph control message. */
374 ng_netflow_rcvmsg (node_p node, item_p item, hook_p lasthook)
376 const priv_p priv = NG_NODE_PRIVATE(node);
377 struct ng_mesg *resp = NULL;
381 NGI_GET_MSG(item, msg);
383 /* Deal with message according to cookie and command */
384 switch (msg->header.typecookie) {
385 case NGM_NETFLOW_COOKIE:
386 switch (msg->header.cmd) {
387 case NGM_NETFLOW_INFO:
389 struct ng_netflow_info *i;
391 NG_MKRESPONSE(resp, msg, sizeof(struct ng_netflow_info),
393 i = (struct ng_netflow_info *)resp->data;
394 ng_netflow_copyinfo(priv, i);
398 case NGM_NETFLOW_IFINFO:
400 struct ng_netflow_ifinfo *i;
401 const uint16_t *index;
403 if (msg->header.arglen != sizeof(uint16_t))
406 index = (uint16_t *)msg->data;
407 if (*index >= NG_NETFLOW_MAXIFACES)
410 /* connected iface? */
411 if (priv->ifaces[*index].hook == NULL)
414 NG_MKRESPONSE(resp, msg,
415 sizeof(struct ng_netflow_ifinfo), M_NOWAIT);
416 i = (struct ng_netflow_ifinfo *)resp->data;
417 memcpy((void *)i, (void *)&priv->ifaces[*index].info,
418 sizeof(priv->ifaces[*index].info));
422 case NGM_NETFLOW_SETDLT:
424 struct ng_netflow_setdlt *set;
425 struct ng_netflow_iface *iface;
427 if (msg->header.arglen !=
428 sizeof(struct ng_netflow_setdlt))
431 set = (struct ng_netflow_setdlt *)msg->data;
432 if (set->iface >= NG_NETFLOW_MAXIFACES)
434 iface = &priv->ifaces[set->iface];
436 /* connected iface? */
437 if (iface->hook == NULL)
442 iface->info.ifinfo_dlt = DLT_EN10MB;
445 iface->info.ifinfo_dlt = DLT_RAW;
452 case NGM_NETFLOW_SETIFINDEX:
454 struct ng_netflow_setifindex *set;
455 struct ng_netflow_iface *iface;
457 if (msg->header.arglen !=
458 sizeof(struct ng_netflow_setifindex))
461 set = (struct ng_netflow_setifindex *)msg->data;
462 if (set->iface >= NG_NETFLOW_MAXIFACES)
464 iface = &priv->ifaces[set->iface];
466 /* connected iface? */
467 if (iface->hook == NULL)
470 iface->info.ifinfo_index = set->index;
474 case NGM_NETFLOW_SETTIMEOUTS:
476 struct ng_netflow_settimeouts *set;
478 if (msg->header.arglen !=
479 sizeof(struct ng_netflow_settimeouts))
482 set = (struct ng_netflow_settimeouts *)msg->data;
484 priv->nfinfo_inact_t = set->inactive_timeout;
485 priv->nfinfo_act_t = set->active_timeout;
489 case NGM_NETFLOW_SETCONFIG:
491 struct ng_netflow_setconfig *set;
493 if (msg->header.arglen !=
494 sizeof(struct ng_netflow_setconfig))
497 set = (struct ng_netflow_setconfig *)msg->data;
499 if (set->iface >= NG_NETFLOW_MAXIFACES)
502 priv->ifaces[set->iface].info.conf = set->conf;
506 case NGM_NETFLOW_SETTEMPLATE:
508 struct ng_netflow_settemplate *set;
510 if (msg->header.arglen !=
511 sizeof(struct ng_netflow_settemplate))
514 set = (struct ng_netflow_settemplate *)msg->data;
516 priv->templ_packets = set->packets;
517 priv->templ_time = set->time;
521 case NGM_NETFLOW_SETMTU:
523 struct ng_netflow_setmtu *set;
525 if (msg->header.arglen !=
526 sizeof(struct ng_netflow_setmtu))
529 set = (struct ng_netflow_setmtu *)msg->data;
530 if ((set->mtu < MIN_MTU) || (set->mtu > MAX_MTU))
533 priv->mtu = set->mtu;
537 case NGM_NETFLOW_SHOW:
538 if (msg->header.arglen !=
539 sizeof(struct ngnf_show_header))
542 NG_MKRESPONSE(resp, msg, NGRESP_SIZE, M_NOWAIT);
547 error = ng_netflow_flow_show(priv,
548 (struct ngnf_show_header *)msg->data,
549 (struct ngnf_show_header *)resp->data);
555 case NGM_NETFLOW_V9INFO:
557 struct ng_netflow_v9info *i;
559 NG_MKRESPONSE(resp, msg,
560 sizeof(struct ng_netflow_v9info), M_NOWAIT);
561 i = (struct ng_netflow_v9info *)resp->data;
562 ng_netflow_copyv9info(priv, i);
567 ERROUT(EINVAL); /* unknown command */
572 ERROUT(EINVAL); /* incorrect cookie */
577 * Take care of synchronous response, if any.
578 * Free memory and return.
581 NG_RESPOND_MSG(error, node, item, resp);
587 /* Receive data on hook. */
589 ng_netflow_rcvdata (hook_p hook, item_p item)
591 const node_p node = NG_HOOK_NODE(hook);
592 const priv_p priv = NG_NODE_PRIVATE(node);
593 const iface_p iface = NG_HOOK_PRIVATE(hook);
595 struct mbuf *m = NULL, *m_old = NULL;
596 struct ip *ip = NULL;
597 struct ip6_hdr *ip6 = NULL;
599 int pullup_len = 0, off;
600 uint8_t acct = 0, bypass = 0, flags = 0, upper_proto = 0;
601 int error = 0, l3_off = 0;
602 unsigned int src_if_index;
603 caddr_t upper_ptr = NULL;
607 if ((hook == priv->export) || (hook == priv->export9)) {
609 * Data arrived on export hook.
610 * This must not happen.
612 log(LOG_ERR, "ng_netflow: incoming data on export hook!\n");
616 if (hook == iface->hook) {
617 if ((iface->info.conf & NG_NETFLOW_CONF_INGRESS) == 0)
620 } else if (hook == iface->out) {
621 if ((iface->info.conf & NG_NETFLOW_CONF_EGRESS) == 0)
627 if ((!bypass) && (iface->info.conf &
628 (NG_NETFLOW_CONF_ONCE | NG_NETFLOW_CONF_THISONCE))) {
629 mtag = m_tag_locate(NGI_M(item), MTAG_NETFLOW,
630 MTAG_NETFLOW_CALLED, NULL);
631 while (mtag != NULL) {
632 if ((iface->info.conf & NG_NETFLOW_CONF_ONCE) ||
633 ((ng_ID_t *)(mtag + 1))[0] == NG_NODE_ID(node)) {
637 mtag = m_tag_locate(NGI_M(item), MTAG_NETFLOW,
638 MTAG_NETFLOW_CALLED, mtag);
646 NG_FWD_ITEM_HOOK(error, item, out);
650 if (iface->info.conf &
651 (NG_NETFLOW_CONF_ONCE | NG_NETFLOW_CONF_THISONCE)) {
652 mtag = m_tag_alloc(MTAG_NETFLOW, MTAG_NETFLOW_CALLED,
653 sizeof(ng_ID_t), M_NOWAIT);
655 ((ng_ID_t *)(mtag + 1))[0] = NG_NODE_ID(node);
656 m_tag_prepend(NGI_M(item), mtag);
660 /* Import configuration flags related to flow creation */
661 flags = iface->info.conf & NG_NETFLOW_FLOW_FLAGS;
666 /* Increase counters. */
667 iface->info.ifinfo_packets++;
670 * Depending on interface data link type and packet contents
671 * we pullup enough data, so that ng_netflow_flow_add() does not
672 * need to know about mbuf at all. We keep current length of data
673 * needed to be contiguous in pullup_len. mtod() is done at the
674 * very end one more time, since m can had changed after pulluping.
676 * In case of unrecognized data we don't return error, but just
677 * pass data to downstream hook, if it is available.
680 #define M_CHECK(length) do { \
681 pullup_len += length; \
682 if (((m)->m_pkthdr.len < (pullup_len)) || \
683 ((pullup_len) > MHLEN)) { \
687 if ((m)->m_len < (pullup_len) && \
688 (((m) = m_pullup((m),(pullup_len))) == NULL)) { \
694 switch (iface->info.ifinfo_dlt) {
695 case DLT_EN10MB: /* Ethernet */
697 struct ether_header *eh;
700 M_CHECK(sizeof(struct ether_header));
701 eh = mtod(m, struct ether_header *);
703 /* Make sure this is IP frame. */
704 etype = ntohs(eh->ether_type);
707 M_CHECK(sizeof(struct ip));
708 eh = mtod(m, struct ether_header *);
709 ip = (struct ip *)(eh + 1);
710 l3_off = sizeof(struct ether_header);
715 * m_pullup() called by M_CHECK() pullups
716 * kern.ipc.max_protohdr (default 60 bytes)
719 M_CHECK(sizeof(struct ip6_hdr));
720 eh = mtod(m, struct ether_header *);
721 ip6 = (struct ip6_hdr *)(eh + 1);
722 l3_off = sizeof(struct ether_header);
727 struct ether_vlan_header *evh;
729 M_CHECK(sizeof(struct ether_vlan_header) -
730 sizeof(struct ether_header));
731 evh = mtod(m, struct ether_vlan_header *);
732 etype = ntohs(evh->evl_proto);
733 l3_off = sizeof(struct ether_vlan_header);
735 if (etype == ETHERTYPE_IP) {
736 M_CHECK(sizeof(struct ip));
737 ip = (struct ip *)(evh + 1);
740 } else if (etype == ETHERTYPE_IPV6) {
741 M_CHECK(sizeof(struct ip6_hdr));
742 ip6 = (struct ip6_hdr *)(evh + 1);
748 goto bypass; /* pass this frame */
752 case DLT_RAW: /* IP packets */
753 M_CHECK(sizeof(struct ip));
754 ip = mtod(m, struct ip *);
755 /* l3_off is already zero */
758 * If INET6 is not defined IPv6 packets
759 * will be discarded in ng_netflow_flow_add().
761 if (ip->ip_v == IP6VERSION) {
763 M_CHECK(sizeof(struct ip6_hdr) - sizeof(struct ip));
764 ip6 = mtod(m, struct ip6_hdr *);
775 if ((ip != NULL) && ((ip->ip_off & htons(IP_OFFMASK)) == 0)) {
776 if ((ip->ip_v != IPVERSION) ||
777 ((ip->ip_hl << 2) < sizeof(struct ip)))
780 * In case of IPv4 header with options, we haven't pulled
783 M_CHECK((ip->ip_hl << 2) - sizeof(struct ip));
785 /* Save upper layer offset and proto */
787 upper_proto = ip->ip_p;
790 * XXX: in case of wrong upper layer header we will
791 * forward this packet but skip this record in netflow.
795 M_CHECK(sizeof(struct tcphdr));
798 M_CHECK(sizeof(struct udphdr));
801 M_CHECK(sizeof(struct sctphdr));
804 } else if (ip != NULL) {
806 * Nothing to save except upper layer proto,
807 * since this is a packet fragment.
809 flags |= NG_NETFLOW_IS_FRAG;
810 upper_proto = ip->ip_p;
811 if ((ip->ip_v != IPVERSION) ||
812 ((ip->ip_hl << 2) < sizeof(struct ip)))
815 } else if (ip6 != NULL) {
816 int cur = ip6->ip6_nxt, hdr_off = 0;
817 struct ip6_ext *ip6e;
818 struct ip6_frag *ip6f;
820 if (priv->export9 == NULL)
823 /* Save upper layer info. */
827 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
831 * Loop through IPv6 extended headers to get upper
832 * layer header / frag.
837 * Same as in IPv4, we can forward a 'bad'
838 * packet without accounting.
841 M_CHECK(sizeof(struct tcphdr));
844 M_CHECK(sizeof(struct udphdr));
847 M_CHECK(sizeof(struct sctphdr));
850 /* Loop until 'real' upper layer headers */
851 case IPPROTO_HOPOPTS:
852 case IPPROTO_ROUTING:
853 case IPPROTO_DSTOPTS:
854 M_CHECK(sizeof(struct ip6_ext));
855 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) +
857 upper_proto = ip6e->ip6e_nxt;
858 hdr_off = (ip6e->ip6e_len + 1) << 3;
861 /* RFC4302, can be before DSTOPTS */
863 M_CHECK(sizeof(struct ip6_ext));
864 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) +
866 upper_proto = ip6e->ip6e_nxt;
867 hdr_off = (ip6e->ip6e_len + 2) << 2;
870 case IPPROTO_FRAGMENT:
871 M_CHECK(sizeof(struct ip6_frag));
872 ip6f = (struct ip6_frag *)(mtod(m, caddr_t) +
874 upper_proto = ip6f->ip6f_nxt;
875 hdr_off = sizeof(struct ip6_frag);
877 flags |= NG_NETFLOW_IS_FRAG;
885 * Any unknown header (new extension or IPv6/IPv4
886 * header for tunnels) ends loop.
902 /* Just in case of real reallocation in M_CHECK() / m_pullup() */
904 priv->nfinfo_realloc_mbuf++;
905 /* Restore ip/ipv6 pointer */
907 ip = (struct ip *)(mtod(m, caddr_t) + l3_off);
908 else if (ip6 != NULL)
909 ip6 = (struct ip6_hdr *)(mtod(m, caddr_t) + l3_off);
912 upper_ptr = (caddr_t)(mtod(m, caddr_t) + off);
914 /* Determine packet input interface. Prefer configured. */
916 if (hook == iface->out || iface->info.ifinfo_index == 0) {
917 if (m->m_pkthdr.rcvif != NULL)
918 src_if_index = m->m_pkthdr.rcvif->if_index;
920 src_if_index = iface->info.ifinfo_index;
922 /* Check packet FIB */
924 if (fib >= priv->maxfibs) {
925 CTR2(KTR_NET, "ng_netflow_rcvdata(): packet fib %d is out of "
926 "range of available fibs: 0 .. %d",
931 if ((fe = priv_to_fib(priv, fib)) == NULL) {
933 if (ng_netflow_fib_init(priv, fib) != 0) {
934 /* malloc() failed */
938 fe = priv_to_fib(priv, fib);
942 error = ng_netflow_flow_add(priv, fe, ip, upper_ptr,
943 upper_proto, flags, src_if_index);
945 else if (ip6 != NULL)
946 error = ng_netflow_flow6_add(priv, fe, ip6, upper_ptr,
947 upper_proto, flags, src_if_index);
956 /* Accounting failure */
958 counter_u64_add(priv->nfinfo_spackets, 1);
959 counter_u64_add(priv->nfinfo_sbytes,
961 } else if (ip6 != NULL) {
962 counter_u64_add(priv->nfinfo_spackets6, 1);
963 counter_u64_add(priv->nfinfo_sbytes6,
968 /* XXX: error gets overwritten here */
969 NG_FWD_NEW_DATA(error, item, out, m);
981 /* We will be shut down in a moment */
983 ng_netflow_close(node_p node)
985 const priv_p priv = NG_NODE_PRIVATE(node);
987 callout_drain(&priv->exp_callout);
988 ng_netflow_cache_flush(priv);
993 /* Do local shutdown processing. */
995 ng_netflow_rmnode(node_p node)
997 const priv_p priv = NG_NODE_PRIVATE(node);
999 NG_NODE_SET_PRIVATE(node, NULL);
1000 NG_NODE_UNREF(priv->node);
1002 free(priv->fib_data, M_NETGRAPH);
1003 free(priv, M_NETGRAPH);
1008 /* Hook disconnection. */
1010 ng_netflow_disconnect(hook_p hook)
1012 node_p node = NG_HOOK_NODE(hook);
1013 priv_p priv = NG_NODE_PRIVATE(node);
1014 iface_p iface = NG_HOOK_PRIVATE(hook);
1016 if (iface != NULL) {
1017 if (iface->hook == hook)
1019 if (iface->out == hook)
1023 /* if export hook disconnected stop running expire(). */
1024 if (hook == priv->export) {
1025 if (priv->export9 == NULL)
1026 callout_drain(&priv->exp_callout);
1027 priv->export = NULL;
1030 if (hook == priv->export9) {
1031 if (priv->export == NULL)
1032 callout_drain(&priv->exp_callout);
1033 priv->export9 = NULL;
1036 /* Removal of the last link destroys the node. */
1037 if (NG_NODE_NUMHOOKS(node) == 0)
1038 ng_rmnode_self(node);