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