npf_ext_log.c revision 1.1.2.4 1 1.1.2.4 yamt /* $NetBSD: npf_ext_log.c,v 1.1.2.4 2014/05/22 11:41:09 yamt Exp $ */
2 1.1.2.2 yamt
3 1.1.2.2 yamt /*-
4 1.1.2.2 yamt * Copyright (c) 2010-2012 The NetBSD Foundation, Inc.
5 1.1.2.2 yamt * All rights reserved.
6 1.1.2.2 yamt *
7 1.1.2.2 yamt * This material is based upon work partially supported by The
8 1.1.2.2 yamt * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
9 1.1.2.2 yamt *
10 1.1.2.2 yamt * Redistribution and use in source and binary forms, with or without
11 1.1.2.2 yamt * modification, are permitted provided that the following conditions
12 1.1.2.2 yamt * are met:
13 1.1.2.2 yamt * 1. Redistributions of source code must retain the above copyright
14 1.1.2.2 yamt * notice, this list of conditions and the following disclaimer.
15 1.1.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.2 yamt * notice, this list of conditions and the following disclaimer in the
17 1.1.2.2 yamt * documentation and/or other materials provided with the distribution.
18 1.1.2.2 yamt *
19 1.1.2.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.2.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.2.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.2.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.2.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.2.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.2.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.2.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.2.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.2.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.2.2 yamt * POSSIBILITY OF SUCH DAMAGE.
30 1.1.2.2 yamt */
31 1.1.2.2 yamt
32 1.1.2.2 yamt /*
33 1.1.2.2 yamt * NPF logging extension.
34 1.1.2.2 yamt */
35 1.1.2.2 yamt
36 1.1.2.2 yamt #include <sys/cdefs.h>
37 1.1.2.4 yamt __KERNEL_RCSID(0, "$NetBSD: npf_ext_log.c,v 1.1.2.4 2014/05/22 11:41:09 yamt Exp $");
38 1.1.2.2 yamt
39 1.1.2.2 yamt #include <sys/types.h>
40 1.1.2.2 yamt #include <sys/module.h>
41 1.1.2.2 yamt
42 1.1.2.2 yamt #include <sys/conf.h>
43 1.1.2.2 yamt #include <sys/kmem.h>
44 1.1.2.2 yamt #include <sys/mbuf.h>
45 1.1.2.2 yamt #include <sys/mutex.h>
46 1.1.2.2 yamt #include <sys/queue.h>
47 1.1.2.2 yamt
48 1.1.2.2 yamt #include <net/if.h>
49 1.1.2.2 yamt #include <net/if_types.h>
50 1.1.2.2 yamt #include <net/bpf.h>
51 1.1.2.2 yamt
52 1.1.2.2 yamt #include "npf_impl.h"
53 1.1.2.2 yamt
54 1.1.2.2 yamt NPF_EXT_MODULE(npf_ext_log, "");
55 1.1.2.2 yamt
56 1.1.2.2 yamt #define NPFEXT_LOG_VER 1
57 1.1.2.2 yamt
58 1.1.2.2 yamt static void * npf_ext_log_id;
59 1.1.2.2 yamt
60 1.1.2.2 yamt typedef struct {
61 1.1.2.2 yamt unsigned int if_idx;
62 1.1.2.2 yamt } npf_ext_log_t;
63 1.1.2.2 yamt
64 1.1.2.2 yamt static int
65 1.1.2.2 yamt npf_log_ctor(npf_rproc_t *rp, prop_dictionary_t params)
66 1.1.2.2 yamt {
67 1.1.2.2 yamt npf_ext_log_t *meta;
68 1.1.2.2 yamt
69 1.1.2.2 yamt meta = kmem_zalloc(sizeof(npf_ext_log_t), KM_SLEEP);
70 1.1.2.2 yamt prop_dictionary_get_uint32(params, "log-interface", &meta->if_idx);
71 1.1.2.2 yamt npf_rproc_assign(rp, meta);
72 1.1.2.2 yamt return 0;
73 1.1.2.2 yamt }
74 1.1.2.2 yamt
75 1.1.2.2 yamt static void
76 1.1.2.2 yamt npf_log_dtor(npf_rproc_t *rp, void *meta)
77 1.1.2.2 yamt {
78 1.1.2.2 yamt kmem_free(meta, sizeof(npf_ext_log_t));
79 1.1.2.2 yamt }
80 1.1.2.2 yamt
81 1.1.2.2 yamt static void
82 1.1.2.2 yamt npf_log(npf_cache_t *npc, nbuf_t *nbuf, void *meta, int *decision)
83 1.1.2.2 yamt {
84 1.1.2.3 yamt struct mbuf *m = nbuf_head_mbuf(nbuf);
85 1.1.2.2 yamt const npf_ext_log_t *log = meta;
86 1.1.2.2 yamt ifnet_t *ifp;
87 1.1.2.2 yamt int family;
88 1.1.2.2 yamt
89 1.1.2.2 yamt /* Set the address family. */
90 1.1.2.2 yamt if (npf_iscached(npc, NPC_IP4)) {
91 1.1.2.2 yamt family = AF_INET;
92 1.1.2.2 yamt } else if (npf_iscached(npc, NPC_IP6)) {
93 1.1.2.2 yamt family = AF_INET6;
94 1.1.2.2 yamt } else {
95 1.1.2.2 yamt family = AF_UNSPEC;
96 1.1.2.2 yamt }
97 1.1.2.2 yamt
98 1.1.2.2 yamt KERNEL_LOCK(1, NULL);
99 1.1.2.2 yamt
100 1.1.2.2 yamt /* Find a pseudo-interface to log. */
101 1.1.2.2 yamt ifp = if_byindex(log->if_idx);
102 1.1.2.2 yamt if (ifp == NULL) {
103 1.1.2.2 yamt /* No interface. */
104 1.1.2.2 yamt KERNEL_UNLOCK_ONE(NULL);
105 1.1.2.2 yamt return;
106 1.1.2.2 yamt }
107 1.1.2.2 yamt
108 1.1.2.2 yamt /* Pass through BPF. */
109 1.1.2.2 yamt ifp->if_opackets++;
110 1.1.2.2 yamt ifp->if_obytes += m->m_pkthdr.len;
111 1.1.2.2 yamt bpf_mtap_af(ifp, family, m);
112 1.1.2.2 yamt KERNEL_UNLOCK_ONE(NULL);
113 1.1.2.2 yamt }
114 1.1.2.2 yamt
115 1.1.2.2 yamt /*
116 1.1.2.2 yamt * Module interface.
117 1.1.2.2 yamt */
118 1.1.2.2 yamt static int
119 1.1.2.2 yamt npf_ext_log_modcmd(modcmd_t cmd, void *arg)
120 1.1.2.2 yamt {
121 1.1.2.2 yamt static const npf_ext_ops_t npf_log_ops = {
122 1.1.2.2 yamt .version = NPFEXT_LOG_VER,
123 1.1.2.2 yamt .ctx = NULL,
124 1.1.2.2 yamt .ctor = npf_log_ctor,
125 1.1.2.2 yamt .dtor = npf_log_dtor,
126 1.1.2.2 yamt .proc = npf_log
127 1.1.2.2 yamt };
128 1.1.2.2 yamt int error;
129 1.1.2.2 yamt
130 1.1.2.2 yamt switch (cmd) {
131 1.1.2.2 yamt case MODULE_CMD_INIT:
132 1.1.2.2 yamt /*
133 1.1.2.2 yamt * Initialise the NPF logging extension.
134 1.1.2.2 yamt */
135 1.1.2.2 yamt npf_ext_log_id = npf_ext_register("log", &npf_log_ops);
136 1.1.2.2 yamt if (!npf_ext_log_id) {
137 1.1.2.2 yamt return EEXIST;
138 1.1.2.2 yamt }
139 1.1.2.2 yamt break;
140 1.1.2.2 yamt
141 1.1.2.2 yamt case MODULE_CMD_FINI:
142 1.1.2.2 yamt error = npf_ext_unregister(npf_ext_log_id);
143 1.1.2.2 yamt if (error) {
144 1.1.2.2 yamt return error;
145 1.1.2.2 yamt }
146 1.1.2.2 yamt break;
147 1.1.2.2 yamt
148 1.1.2.2 yamt case MODULE_CMD_AUTOUNLOAD:
149 1.1.2.2 yamt /* Allow auto-unload only if NPF permits it. */
150 1.1.2.2 yamt return npf_autounload_p() ? 0 : EBUSY;
151 1.1.2.2 yamt
152 1.1.2.2 yamt default:
153 1.1.2.2 yamt return ENOTTY;
154 1.1.2.2 yamt }
155 1.1.2.2 yamt return 0;
156 1.1.2.2 yamt }
157