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