npf_ext_log.c revision 1.6
1/*	$NetBSD: npf_ext_log.c,v 1.6 2013/03/11 17:03:55 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#include <sys/cdefs.h>
37__KERNEL_RCSID(0, "$NetBSD: npf_ext_log.c,v 1.6 2013/03/11 17:03:55 christos 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
54NPF_EXT_MODULE(npf_ext_log, "");
55
56#define	NPFEXT_LOG_VER		1
57
58static void *		npf_ext_log_id;
59
60typedef struct {
61	unsigned int	if_idx;
62} npf_ext_log_t;
63
64static int
65npf_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
75static void
76npf_log_dtor(npf_rproc_t *rp, void *meta)
77{
78	kmem_free(meta, sizeof(npf_ext_log_t));
79}
80
81static void
82npf_log(npf_cache_t *npc, nbuf_t *nbuf, void *meta, int *decision)
83{
84	struct mbuf *m = nbuf_head_mbuf(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;
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
115/*
116 * Module interface.
117 */
118static int
119npf_ext_log_modcmd(modcmd_t cmd, void *arg)
120{
121	static const npf_ext_ops_t npf_log_ops = {
122		.version	= NPFEXT_LOG_VER,
123		.ctx		= NULL,
124		.ctor		= npf_log_ctor,
125		.dtor		= npf_log_dtor,
126		.proc		= npf_log
127	};
128	int error;
129
130	switch (cmd) {
131	case MODULE_CMD_INIT:
132		/*
133		 * Initialise the NPF logging extension.
134		 */
135		npf_ext_log_id = npf_ext_register("log", &npf_log_ops);
136		if (!npf_ext_log_id) {
137			return EEXIST;
138		}
139		break;
140
141	case MODULE_CMD_FINI:
142		error = npf_ext_unregister(npf_ext_log_id);
143		if (error) {
144			return error;
145		}
146		break;
147
148	case MODULE_CMD_AUTOUNLOAD:
149		/* Allow auto-unload only if NPF permits it. */
150		return npf_autounload_p() ? 0 : EBUSY;
151
152	default:
153		return ENOTTY;
154	}
155	return 0;
156}
157