xref: /netbsd-src/sys/net/npf/npf_ext_log.c (revision 9616dacfef448e70e3fbbd865bddf60d54b656c5)
1 /*	$NetBSD: npf_ext_log.c,v 1.10 2016/12/26 23:05:06 christos 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 #ifdef _KERNEL
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: npf_ext_log.c,v 1.10 2016/12/26 23:05:06 christos Exp $");
39 
40 #include <sys/types.h>
41 #include <sys/module.h>
42 
43 #include <sys/conf.h>
44 #include <sys/kmem.h>
45 #include <sys/mbuf.h>
46 #include <sys/mutex.h>
47 #include <sys/queue.h>
48 
49 #include <net/if.h>
50 #include <net/if_types.h>
51 #include <net/bpf.h>
52 #endif
53 
54 #include "npf_impl.h"
55 
56 NPF_EXT_MODULE(npf_ext_log, "");
57 
58 #define	NPFEXT_LOG_VER		1
59 
60 static void *		npf_ext_log_id;
61 
62 typedef struct {
63 	unsigned int	if_idx;
64 } npf_ext_log_t;
65 
66 static int
67 npf_log_ctor(npf_rproc_t *rp, prop_dictionary_t params)
68 {
69 	npf_ext_log_t *meta;
70 
71 	meta = kmem_zalloc(sizeof(npf_ext_log_t), KM_SLEEP);
72 	prop_dictionary_get_uint32(params, "log-interface", &meta->if_idx);
73 	npf_rproc_assign(rp, meta);
74 	return 0;
75 }
76 
77 static void
78 npf_log_dtor(npf_rproc_t *rp, void *meta)
79 {
80 	kmem_free(meta, sizeof(npf_ext_log_t));
81 }
82 
83 static bool
84 npf_log(npf_cache_t *npc, void *meta, int *decision)
85 {
86 	struct mbuf *m = nbuf_head_mbuf(npc->npc_nbuf);
87 	const npf_ext_log_t *log = meta;
88 	struct psref psref;
89 	ifnet_t *ifp;
90 	int family;
91 
92 	/* Set the address family. */
93 	if (npf_iscached(npc, NPC_IP4)) {
94 		family = AF_INET;
95 	} else if (npf_iscached(npc, NPC_IP6)) {
96 		family = AF_INET6;
97 	} else {
98 		family = AF_UNSPEC;
99 	}
100 
101 	KERNEL_LOCK(1, NULL);
102 
103 	/* Find a pseudo-interface to log. */
104 	ifp = if_get_byindex(log->if_idx, &psref);
105 	if (ifp == NULL) {
106 		/* No interface. */
107 		KERNEL_UNLOCK_ONE(NULL);
108 		return true;
109 	}
110 
111 	/* Pass through BPF. */
112 	ifp->if_opackets++;
113 	ifp->if_obytes += m->m_pkthdr.len;
114 	bpf_mtap_af(ifp, family, m);
115 	if_put(ifp, &psref);
116 
117 	KERNEL_UNLOCK_ONE(NULL);
118 
119 	return true;
120 }
121 
122 /*
123  * Module interface.
124  */
125 static int
126 npf_ext_log_modcmd(modcmd_t cmd, void *arg)
127 {
128 	static const npf_ext_ops_t npf_log_ops = {
129 		.version	= NPFEXT_LOG_VER,
130 		.ctx		= NULL,
131 		.ctor		= npf_log_ctor,
132 		.dtor		= npf_log_dtor,
133 		.proc		= npf_log
134 	};
135 	npf_t *npf = npf_getkernctx();
136 	int error;
137 
138 	switch (cmd) {
139 	case MODULE_CMD_INIT:
140 		/*
141 		 * Initialise the NPF logging extension.
142 		 */
143 		npf_ext_log_id = npf_ext_register(npf, "log", &npf_log_ops);
144 		if (!npf_ext_log_id) {
145 			return EEXIST;
146 		}
147 		break;
148 
149 	case MODULE_CMD_FINI:
150 		error = npf_ext_unregister(npf, npf_ext_log_id);
151 		if (error) {
152 			return error;
153 		}
154 		break;
155 
156 	case MODULE_CMD_AUTOUNLOAD:
157 		/* Allow auto-unload only if NPF permits it. */
158 		return npf_autounload_p() ? 0 : EBUSY;
159 
160 	default:
161 		return ENOTTY;
162 	}
163 	return 0;
164 }
165