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$");
37 #include "opt_inet6.h"
38 #include "opt_route.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/counter.h>
43 #include <sys/kernel.h>
45 #include <sys/limits.h>
46 #include <sys/malloc.h>
48 #include <sys/socket.h>
49 #include <sys/syslog.h>
50 #include <sys/ctype.h>
54 #include <net/ethernet.h>
55 #include <net/route.h>
56 #include <net/if_arp.h>
57 #include <net/if_var.h>
58 #include <net/if_private.h>
59 #include <net/if_vlan_var.h>
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64 #include <netinet/ip6.h>
65 #include <netinet/tcp.h>
66 #include <netinet/udp.h>
67 #include <netinet/sctp.h>
69 #include <netgraph/ng_message.h>
70 #include <netgraph/ng_parse.h>
71 #include <netgraph/netgraph.h>
72 #include <netgraph/netflow/netflow.h>
73 #include <netgraph/netflow/netflow_v9.h>
74 #include <netgraph/netflow/ng_netflow.h>
76 /* Netgraph methods */
77 static ng_constructor_t ng_netflow_constructor;
78 static ng_rcvmsg_t ng_netflow_rcvmsg;
79 static ng_close_t ng_netflow_close;
80 static ng_shutdown_t ng_netflow_rmnode;
81 static ng_newhook_t ng_netflow_newhook;
82 static ng_rcvdata_t ng_netflow_rcvdata;
83 static ng_disconnect_t ng_netflow_disconnect;
85 /* Parse type for struct ng_netflow_info */
86 static const struct ng_parse_struct_field ng_netflow_info_type_fields[]
87 = NG_NETFLOW_INFO_TYPE;
88 static const struct ng_parse_type ng_netflow_info_type = {
89 &ng_parse_struct_type,
90 &ng_netflow_info_type_fields
93 /* Parse type for struct ng_netflow_ifinfo */
94 static const struct ng_parse_struct_field ng_netflow_ifinfo_type_fields[]
95 = NG_NETFLOW_IFINFO_TYPE;
96 static const struct ng_parse_type ng_netflow_ifinfo_type = {
97 &ng_parse_struct_type,
98 &ng_netflow_ifinfo_type_fields
101 /* Parse type for struct ng_netflow_setdlt */
102 static const struct ng_parse_struct_field ng_netflow_setdlt_type_fields[]
103 = NG_NETFLOW_SETDLT_TYPE;
104 static const struct ng_parse_type ng_netflow_setdlt_type = {
105 &ng_parse_struct_type,
106 &ng_netflow_setdlt_type_fields
109 /* Parse type for ng_netflow_setifindex */
110 static const struct ng_parse_struct_field ng_netflow_setifindex_type_fields[]
111 = NG_NETFLOW_SETIFINDEX_TYPE;
112 static const struct ng_parse_type ng_netflow_setifindex_type = {
113 &ng_parse_struct_type,
114 &ng_netflow_setifindex_type_fields
117 /* Parse type for ng_netflow_settimeouts */
118 static const struct ng_parse_struct_field ng_netflow_settimeouts_type_fields[]
119 = NG_NETFLOW_SETTIMEOUTS_TYPE;
120 static const struct ng_parse_type ng_netflow_settimeouts_type = {
121 &ng_parse_struct_type,
122 &ng_netflow_settimeouts_type_fields
125 /* Parse type for ng_netflow_setconfig */
126 static const struct ng_parse_struct_field ng_netflow_setconfig_type_fields[]
127 = NG_NETFLOW_SETCONFIG_TYPE;
128 static const struct ng_parse_type ng_netflow_setconfig_type = {
129 &ng_parse_struct_type,
130 &ng_netflow_setconfig_type_fields
133 /* Parse type for ng_netflow_settemplate */
134 static const struct ng_parse_struct_field ng_netflow_settemplate_type_fields[]
135 = NG_NETFLOW_SETTEMPLATE_TYPE;
136 static const struct ng_parse_type ng_netflow_settemplate_type = {
137 &ng_parse_struct_type,
138 &ng_netflow_settemplate_type_fields
141 /* Parse type for ng_netflow_setmtu */
142 static const struct ng_parse_struct_field ng_netflow_setmtu_type_fields[]
143 = NG_NETFLOW_SETMTU_TYPE;
144 static const struct ng_parse_type ng_netflow_setmtu_type = {
145 &ng_parse_struct_type,
146 &ng_netflow_setmtu_type_fields
149 /* Parse type for struct ng_netflow_v9info */
150 static const struct ng_parse_struct_field ng_netflow_v9info_type_fields[]
151 = NG_NETFLOW_V9INFO_TYPE;
152 static const struct ng_parse_type ng_netflow_v9info_type = {
153 &ng_parse_struct_type,
154 &ng_netflow_v9info_type_fields
157 /* List of commands and how to convert arguments to/from ASCII */
158 static const struct ng_cmdlist ng_netflow_cmds[] = {
164 &ng_netflow_info_type
170 &ng_parse_uint16_type,
171 &ng_netflow_ifinfo_type
177 &ng_netflow_setdlt_type,
182 NGM_NETFLOW_SETIFINDEX,
184 &ng_netflow_setifindex_type,
189 NGM_NETFLOW_SETTIMEOUTS,
191 &ng_netflow_settimeouts_type,
196 NGM_NETFLOW_SETCONFIG,
198 &ng_netflow_setconfig_type,
203 NGM_NETFLOW_SETTEMPLATE,
205 &ng_netflow_settemplate_type,
212 &ng_netflow_setmtu_type,
220 &ng_netflow_v9info_type
225 /* Netgraph node type descriptor */
226 static struct ng_type ng_netflow_typestruct = {
227 .version = NG_ABI_VERSION,
228 .name = NG_NETFLOW_NODE_TYPE,
229 .constructor = ng_netflow_constructor,
230 .rcvmsg = ng_netflow_rcvmsg,
231 .close = ng_netflow_close,
232 .shutdown = ng_netflow_rmnode,
233 .newhook = ng_netflow_newhook,
234 .rcvdata = ng_netflow_rcvdata,
235 .disconnect = ng_netflow_disconnect,
236 .cmdlist = ng_netflow_cmds,
238 NETGRAPH_INIT(netflow, &ng_netflow_typestruct);
240 /* Called at node creation */
242 ng_netflow_constructor(node_p node)
247 /* Initialize private data */
248 priv = malloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
250 /* Initialize fib data */
251 priv->maxfibs = rt_numfibs;
252 priv->fib_data = malloc(sizeof(fib_export_p) * priv->maxfibs,
253 M_NETGRAPH, M_WAITOK | M_ZERO);
255 /* Make node and its data point at each other */
256 NG_NODE_SET_PRIVATE(node, priv);
259 /* Initialize timeouts to default values */
260 priv->nfinfo_inact_t = INACTIVE_TIMEOUT;
261 priv->nfinfo_act_t = ACTIVE_TIMEOUT;
263 /* Set default config */
264 for (i = 0; i < NG_NETFLOW_MAXIFACES; i++)
265 priv->ifaces[i].info.conf = NG_NETFLOW_CONF_INGRESS;
267 /* Initialize callout handle */
268 callout_init(&priv->exp_callout, 1);
270 /* Allocate memory and set up flow cache */
271 ng_netflow_cache_init(priv);
277 * ng_netflow supports two hooks: data and export.
278 * Incoming traffic is expected on data, and expired
279 * netflow datagrams are sent to export.
282 ng_netflow_newhook(node_p node, hook_p hook, const char *name)
284 const priv_p priv = NG_NODE_PRIVATE(node);
286 if (strncmp(name, NG_NETFLOW_HOOK_DATA, /* an iface hook? */
287 strlen(NG_NETFLOW_HOOK_DATA)) == 0) {
293 cp = name + strlen(NG_NETFLOW_HOOK_DATA);
294 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
297 ifnum = (int)strtoul(cp, &eptr, 10);
298 if (*eptr != '\0' || ifnum < 0 || ifnum >= NG_NETFLOW_MAXIFACES)
301 /* See if hook is already connected */
302 if (priv->ifaces[ifnum].hook != NULL)
305 iface = &priv->ifaces[ifnum];
307 /* Link private info and hook together */
308 NG_HOOK_SET_PRIVATE(hook, iface);
312 * In most cases traffic accounting is done on an
313 * Ethernet interface, so default data link type
314 * will be DLT_EN10MB.
316 iface->info.ifinfo_dlt = DLT_EN10MB;
318 } else if (strncmp(name, NG_NETFLOW_HOOK_OUT,
319 strlen(NG_NETFLOW_HOOK_OUT)) == 0) {
325 cp = name + strlen(NG_NETFLOW_HOOK_OUT);
326 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
329 ifnum = (int)strtoul(cp, &eptr, 10);
330 if (*eptr != '\0' || ifnum < 0 || ifnum >= NG_NETFLOW_MAXIFACES)
333 /* See if hook is already connected */
334 if (priv->ifaces[ifnum].out != NULL)
337 iface = &priv->ifaces[ifnum];
339 /* Link private info and hook together */
340 NG_HOOK_SET_PRIVATE(hook, iface);
343 } 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) {
357 if (priv->export9 != NULL)
360 priv->export9 = hook;
362 /* Exporter is ready. Let's schedule expiry. */
363 callout_reset(&priv->exp_callout, (1*hz), &ng_netflow_expire,
371 /* Get a netgraph control message. */
373 ng_netflow_rcvmsg (node_p node, item_p item, hook_p lasthook)
375 const priv_p priv = NG_NODE_PRIVATE(node);
376 struct ng_mesg *resp = NULL;
380 NGI_GET_MSG(item, msg);
382 /* Deal with message according to cookie and command */
383 switch (msg->header.typecookie) {
384 case NGM_NETFLOW_COOKIE:
385 switch (msg->header.cmd) {
386 case NGM_NETFLOW_INFO:
388 struct ng_netflow_info *i;
390 NG_MKRESPONSE(resp, msg, sizeof(struct ng_netflow_info),
392 i = (struct ng_netflow_info *)resp->data;
393 ng_netflow_copyinfo(priv, i);
397 case NGM_NETFLOW_IFINFO:
399 struct ng_netflow_ifinfo *i;
400 const uint16_t *index;
402 if (msg->header.arglen != sizeof(uint16_t))
405 index = (uint16_t *)msg->data;
406 if (*index >= NG_NETFLOW_MAXIFACES)
409 /* connected iface? */
410 if (priv->ifaces[*index].hook == NULL)
413 NG_MKRESPONSE(resp, msg,
414 sizeof(struct ng_netflow_ifinfo), M_NOWAIT);
415 i = (struct ng_netflow_ifinfo *)resp->data;
416 memcpy((void *)i, (void *)&priv->ifaces[*index].info,
417 sizeof(priv->ifaces[*index].info));
421 case NGM_NETFLOW_SETDLT:
423 struct ng_netflow_setdlt *set;
424 struct ng_netflow_iface *iface;
426 if (msg->header.arglen !=
427 sizeof(struct ng_netflow_setdlt))
430 set = (struct ng_netflow_setdlt *)msg->data;
431 if (set->iface >= NG_NETFLOW_MAXIFACES)
433 iface = &priv->ifaces[set->iface];
435 /* connected iface? */
436 if (iface->hook == NULL)
441 iface->info.ifinfo_dlt = DLT_EN10MB;
444 iface->info.ifinfo_dlt = DLT_RAW;
451 case NGM_NETFLOW_SETIFINDEX:
453 struct ng_netflow_setifindex *set;
454 struct ng_netflow_iface *iface;
456 if (msg->header.arglen !=
457 sizeof(struct ng_netflow_setifindex))
460 set = (struct ng_netflow_setifindex *)msg->data;
461 if (set->iface >= NG_NETFLOW_MAXIFACES)
463 iface = &priv->ifaces[set->iface];
465 /* connected iface? */
466 if (iface->hook == NULL)
469 iface->info.ifinfo_index = set->index;
473 case NGM_NETFLOW_SETTIMEOUTS:
475 struct ng_netflow_settimeouts *set;
477 if (msg->header.arglen !=
478 sizeof(struct ng_netflow_settimeouts))
481 set = (struct ng_netflow_settimeouts *)msg->data;
483 priv->nfinfo_inact_t = set->inactive_timeout;
484 priv->nfinfo_act_t = set->active_timeout;
488 case NGM_NETFLOW_SETCONFIG:
490 struct ng_netflow_setconfig *set;
492 if (msg->header.arglen !=
493 sizeof(struct ng_netflow_setconfig))
496 set = (struct ng_netflow_setconfig *)msg->data;
498 if (set->iface >= NG_NETFLOW_MAXIFACES)
501 priv->ifaces[set->iface].info.conf = set->conf;
505 case NGM_NETFLOW_SETTEMPLATE:
507 struct ng_netflow_settemplate *set;
509 if (msg->header.arglen !=
510 sizeof(struct ng_netflow_settemplate))
513 set = (struct ng_netflow_settemplate *)msg->data;
515 priv->templ_packets = set->packets;
516 priv->templ_time = set->time;
520 case NGM_NETFLOW_SETMTU:
522 struct ng_netflow_setmtu *set;
524 if (msg->header.arglen !=
525 sizeof(struct ng_netflow_setmtu))
528 set = (struct ng_netflow_setmtu *)msg->data;
529 if ((set->mtu < MIN_MTU) || (set->mtu > MAX_MTU))
532 priv->mtu = set->mtu;
536 case NGM_NETFLOW_SHOW:
537 if (msg->header.arglen !=
538 sizeof(struct ngnf_show_header))
541 NG_MKRESPONSE(resp, msg, NGRESP_SIZE, M_NOWAIT);
546 error = ng_netflow_flow_show(priv,
547 (struct ngnf_show_header *)msg->data,
548 (struct ngnf_show_header *)resp->data);
554 case NGM_NETFLOW_V9INFO:
556 struct ng_netflow_v9info *i;
558 NG_MKRESPONSE(resp, msg,
559 sizeof(struct ng_netflow_v9info), M_NOWAIT);
560 i = (struct ng_netflow_v9info *)resp->data;
561 ng_netflow_copyv9info(priv, i);
566 ERROUT(EINVAL); /* unknown command */
571 ERROUT(EINVAL); /* incorrect cookie */
576 * Take care of synchronous response, if any.
577 * Free memory and return.
580 NG_RESPOND_MSG(error, node, item, resp);
586 /* Receive data on hook. */
588 ng_netflow_rcvdata (hook_p hook, item_p item)
590 const node_p node = NG_HOOK_NODE(hook);
591 const priv_p priv = NG_NODE_PRIVATE(node);
592 const iface_p iface = NG_HOOK_PRIVATE(hook);
594 struct mbuf *m = NULL, *m_old = NULL;
595 struct ip *ip = NULL;
596 struct ip6_hdr *ip6 = NULL;
599 uint8_t acct = 0, bypass = 0;
600 int error = 0, l3_off = 0;
601 #if defined(INET) || defined(INET6)
603 uint8_t flags = 0, upper_proto = 0;
604 unsigned int src_if_index;
605 caddr_t upper_ptr = NULL;
610 if ((hook == priv->export) || (hook == priv->export9)) {
612 * Data arrived on export hook.
613 * This must not happen.
615 log(LOG_ERR, "ng_netflow: incoming data on export hook!\n");
619 if (hook == iface->hook) {
620 if ((iface->info.conf & NG_NETFLOW_CONF_INGRESS) == 0)
623 } else if (hook == iface->out) {
624 if ((iface->info.conf & NG_NETFLOW_CONF_EGRESS) == 0)
630 if ((!bypass) && (iface->info.conf &
631 (NG_NETFLOW_CONF_ONCE | NG_NETFLOW_CONF_THISONCE))) {
632 mtag = m_tag_locate(NGI_M(item), MTAG_NETFLOW,
633 MTAG_NETFLOW_CALLED, NULL);
634 while (mtag != NULL) {
635 if ((iface->info.conf & NG_NETFLOW_CONF_ONCE) ||
636 ((ng_ID_t *)(mtag + 1))[0] == NG_NODE_ID(node)) {
640 mtag = m_tag_locate(NGI_M(item), MTAG_NETFLOW,
641 MTAG_NETFLOW_CALLED, mtag);
649 NG_FWD_ITEM_HOOK(error, item, out);
653 if (iface->info.conf &
654 (NG_NETFLOW_CONF_ONCE | NG_NETFLOW_CONF_THISONCE)) {
655 mtag = m_tag_alloc(MTAG_NETFLOW, MTAG_NETFLOW_CALLED,
656 sizeof(ng_ID_t), M_NOWAIT);
658 ((ng_ID_t *)(mtag + 1))[0] = NG_NODE_ID(node);
659 m_tag_prepend(NGI_M(item), mtag);
663 #if defined(INET) || defined(INET6)
664 /* Import configuration flags related to flow creation */
665 flags = iface->info.conf & NG_NETFLOW_FLOW_FLAGS;
671 /* Increase counters. */
672 iface->info.ifinfo_packets++;
675 * Depending on interface data link type and packet contents
676 * we pullup enough data, so that ng_netflow_flow_add() does not
677 * need to know about mbuf at all. We keep current length of data
678 * needed to be contiguous in pullup_len. mtod() is done at the
679 * very end one more time, since m can had changed after pulluping.
681 * In case of unrecognized data we don't return error, but just
682 * pass data to downstream hook, if it is available.
685 #define M_CHECK(length) do { \
686 pullup_len += length; \
687 if (((m)->m_pkthdr.len < (pullup_len)) || \
688 ((pullup_len) > MHLEN)) { \
692 if ((m)->m_len < (pullup_len) && \
693 (((m) = m_pullup((m),(pullup_len))) == NULL)) { \
699 switch (iface->info.ifinfo_dlt) {
700 case DLT_EN10MB: /* Ethernet */
702 struct ether_header *eh;
705 M_CHECK(sizeof(struct ether_header));
706 eh = mtod(m, struct ether_header *);
708 /* Make sure this is IP frame. */
709 etype = ntohs(eh->ether_type);
713 M_CHECK(sizeof(struct ip));
714 eh = mtod(m, struct ether_header *);
715 ip = (struct ip *)(eh + 1);
716 l3_off = sizeof(struct ether_header);
722 * m_pullup() called by M_CHECK() pullups
723 * kern.ipc.max_protohdr (default 60 bytes)
726 M_CHECK(sizeof(struct ip6_hdr));
727 eh = mtod(m, struct ether_header *);
728 ip6 = (struct ip6_hdr *)(eh + 1);
729 l3_off = sizeof(struct ether_header);
734 struct ether_vlan_header *evh;
736 M_CHECK(sizeof(struct ether_vlan_header) -
737 sizeof(struct ether_header));
738 evh = mtod(m, struct ether_vlan_header *);
739 etype = ntohs(evh->evl_proto);
740 l3_off = sizeof(struct ether_vlan_header);
742 if (etype == ETHERTYPE_IP) {
744 M_CHECK(sizeof(struct ip));
745 ip = (struct ip *)(evh + 1);
749 } else if (etype == ETHERTYPE_IPV6) {
750 M_CHECK(sizeof(struct ip6_hdr));
751 ip6 = (struct ip6_hdr *)(evh + 1);
757 goto bypass; /* pass this frame */
761 case DLT_RAW: /* IP packets */
762 M_CHECK(sizeof(struct ip));
763 ip = mtod(m, struct ip *);
764 /* l3_off is already zero */
767 * If INET6 is not defined IPv6 packets
768 * will be discarded in ng_netflow_flow_add().
770 if (ip->ip_v == IP6VERSION) {
772 M_CHECK(sizeof(struct ip6_hdr) - sizeof(struct ip));
773 ip6 = mtod(m, struct ip6_hdr *);
785 #if defined(INET) || defined(INET6)
789 if ((ip != NULL) && ((ip->ip_off & htons(IP_OFFMASK)) == 0)) {
790 if ((ip->ip_v != IPVERSION) ||
791 ((ip->ip_hl << 2) < sizeof(struct ip)))
794 * In case of IPv4 header with options, we haven't pulled
797 M_CHECK((ip->ip_hl << 2) - sizeof(struct ip));
799 #if defined(INET) || defined(INET6)
800 /* Save upper layer offset and proto */
802 upper_proto = ip->ip_p;
806 * XXX: in case of wrong upper layer header we will
807 * forward this packet but skip this record in netflow.
811 M_CHECK(sizeof(struct tcphdr));
814 M_CHECK(sizeof(struct udphdr));
817 M_CHECK(sizeof(struct sctphdr));
820 } else if (ip != NULL) {
822 * Nothing to save except upper layer proto,
823 * since this is a packet fragment.
825 #if defined(INET) || defined(INET6)
826 flags |= NG_NETFLOW_IS_FRAG;
827 upper_proto = ip->ip_p;
829 if ((ip->ip_v != IPVERSION) ||
830 ((ip->ip_hl << 2) < sizeof(struct ip)))
833 } else if (ip6 != NULL) {
834 int cur = ip6->ip6_nxt, hdr_off = 0;
835 struct ip6_ext *ip6e;
836 struct ip6_frag *ip6f;
838 if (priv->export9 == NULL)
841 /* Save upper layer info. */
845 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
849 * Loop through IPv6 extended headers to get upper
850 * layer header / frag.
855 * Same as in IPv4, we can forward a 'bad'
856 * packet without accounting.
859 M_CHECK(sizeof(struct tcphdr));
862 M_CHECK(sizeof(struct udphdr));
865 M_CHECK(sizeof(struct sctphdr));
868 /* Loop until 'real' upper layer headers */
869 case IPPROTO_HOPOPTS:
870 case IPPROTO_ROUTING:
871 case IPPROTO_DSTOPTS:
872 M_CHECK(sizeof(struct ip6_ext));
873 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) +
875 upper_proto = ip6e->ip6e_nxt;
876 hdr_off = (ip6e->ip6e_len + 1) << 3;
879 /* RFC4302, can be before DSTOPTS */
881 M_CHECK(sizeof(struct ip6_ext));
882 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) +
884 upper_proto = ip6e->ip6e_nxt;
885 hdr_off = (ip6e->ip6e_len + 2) << 2;
888 case IPPROTO_FRAGMENT:
889 M_CHECK(sizeof(struct ip6_frag));
890 ip6f = (struct ip6_frag *)(mtod(m, caddr_t) +
892 upper_proto = ip6f->ip6f_nxt;
893 hdr_off = sizeof(struct ip6_frag);
895 flags |= NG_NETFLOW_IS_FRAG;
903 * Any unknown header (new extension or IPv6/IPv4
904 * header for tunnels) ends loop.
920 /* Just in case of real reallocation in M_CHECK() / m_pullup() */
922 priv->nfinfo_realloc_mbuf++;
923 /* Restore ip/ipv6 pointer */
925 ip = (struct ip *)(mtod(m, caddr_t) + l3_off);
926 else if (ip6 != NULL)
927 ip6 = (struct ip6_hdr *)(mtod(m, caddr_t) + l3_off);
930 #if defined(INET) || defined(INET6)
931 upper_ptr = (caddr_t)(mtod(m, caddr_t) + off);
933 /* Determine packet input interface. Prefer configured. */
935 if (hook == iface->out || iface->info.ifinfo_index == 0) {
936 if (m->m_pkthdr.rcvif != NULL)
937 src_if_index = m->m_pkthdr.rcvif->if_index;
939 src_if_index = iface->info.ifinfo_index;
942 /* Check packet FIB */
944 if (fib >= priv->maxfibs) {
945 CTR2(KTR_NET, "ng_netflow_rcvdata(): packet fib %d is out of "
946 "range of available fibs: 0 .. %d",
951 if ((fe = priv_to_fib(priv, fib)) == NULL) {
953 if (ng_netflow_fib_init(priv, fib) != 0) {
954 /* malloc() failed */
958 fe = priv_to_fib(priv, fib);
963 error = ng_netflow_flow_add(priv, fe, ip, upper_ptr,
964 upper_proto, flags, src_if_index);
966 #if defined(INET6) && defined(INET)
971 error = ng_netflow_flow6_add(priv, fe, ip6, upper_ptr,
972 upper_proto, flags, src_if_index);
981 /* Accounting failure */
983 counter_u64_add(priv->nfinfo_spackets, 1);
984 counter_u64_add(priv->nfinfo_sbytes,
986 } else if (ip6 != NULL) {
987 counter_u64_add(priv->nfinfo_spackets6, 1);
988 counter_u64_add(priv->nfinfo_sbytes6,
993 /* XXX: error gets overwritten here */
994 NG_FWD_NEW_DATA(error, item, out, m);
1006 /* We will be shut down in a moment */
1008 ng_netflow_close(node_p node)
1010 const priv_p priv = NG_NODE_PRIVATE(node);
1012 callout_drain(&priv->exp_callout);
1013 ng_netflow_cache_flush(priv);
1018 /* Do local shutdown processing. */
1020 ng_netflow_rmnode(node_p node)
1022 const priv_p priv = NG_NODE_PRIVATE(node);
1024 NG_NODE_SET_PRIVATE(node, NULL);
1025 NG_NODE_UNREF(priv->node);
1027 free(priv->fib_data, M_NETGRAPH);
1028 free(priv, M_NETGRAPH);
1033 /* Hook disconnection. */
1035 ng_netflow_disconnect(hook_p hook)
1037 node_p node = NG_HOOK_NODE(hook);
1038 priv_p priv = NG_NODE_PRIVATE(node);
1039 iface_p iface = NG_HOOK_PRIVATE(hook);
1041 if (iface != NULL) {
1042 if (iface->hook == hook)
1044 if (iface->out == hook)
1048 /* if export hook disconnected stop running expire(). */
1049 if (hook == priv->export) {
1050 if (priv->export9 == NULL)
1051 callout_drain(&priv->exp_callout);
1052 priv->export = NULL;
1055 if (hook == priv->export9) {
1056 if (priv->export == NULL)
1057 callout_drain(&priv->exp_callout);
1058 priv->export9 = NULL;
1061 /* Removal of the last link destroys the node. */
1062 if (NG_NODE_NUMHOOKS(node) == 0)
1063 ng_rmnode_self(node);