if_npflog.c revision 1.2 1 1.2 christos /* $NetBSD: if_npflog.c,v 1.2 2013/03/13 02:34:37 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2010-2012 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This material is based upon work partially supported by The
8 1.1 christos * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos *
19 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.1 christos */
31 1.1 christos
32 1.1 christos /*
33 1.1 christos * NPF logging extension.
34 1.1 christos */
35 1.1 christos
36 1.1 christos #include <sys/cdefs.h>
37 1.2 christos __KERNEL_RCSID(0, "$NetBSD: if_npflog.c,v 1.2 2013/03/13 02:34:37 christos Exp $");
38 1.1 christos
39 1.1 christos #include <sys/types.h>
40 1.1 christos #include <sys/module.h>
41 1.1 christos
42 1.1 christos #include <sys/conf.h>
43 1.1 christos #include <sys/kmem.h>
44 1.1 christos #include <sys/mbuf.h>
45 1.1 christos #include <sys/mutex.h>
46 1.1 christos #include <sys/queue.h>
47 1.1 christos #include <sys/sockio.h>
48 1.1 christos
49 1.1 christos #include <net/if.h>
50 1.1 christos #include <net/if_types.h>
51 1.1 christos #include <net/bpf.h>
52 1.1 christos
53 1.2 christos MODULE(MODULE_CLASS_DRIVER, if_npflog);
54 1.1 christos
55 1.1 christos typedef struct npflog_softc {
56 1.1 christos LIST_ENTRY(npflog_softc) sc_entry;
57 1.1 christos kmutex_t sc_lock;
58 1.1 christos ifnet_t sc_if;
59 1.1 christos int sc_unit;
60 1.1 christos } npflog_softc_t;
61 1.1 christos
62 1.1 christos static int npflog_clone_create(struct if_clone *, int);
63 1.1 christos static int npflog_clone_destroy(ifnet_t *);
64 1.1 christos
65 1.1 christos static LIST_HEAD(, npflog_softc) npflog_if_list __cacheline_aligned;
66 1.1 christos static struct if_clone npflog_cloner =
67 1.1 christos IF_CLONE_INITIALIZER("npflog", npflog_clone_create, npflog_clone_destroy);
68 1.1 christos
69 1.1 christos static void
70 1.1 christos npflogattach(int nunits)
71 1.1 christos {
72 1.1 christos
73 1.1 christos LIST_INIT(&npflog_if_list);
74 1.1 christos if_clone_attach(&npflog_cloner);
75 1.1 christos }
76 1.1 christos
77 1.1 christos static void
78 1.1 christos npflogdetach(void)
79 1.1 christos {
80 1.1 christos npflog_softc_t *sc;
81 1.1 christos
82 1.1 christos while ((sc = LIST_FIRST(&npflog_if_list)) != NULL) {
83 1.1 christos npflog_clone_destroy(&sc->sc_if);
84 1.1 christos }
85 1.1 christos if_clone_detach(&npflog_cloner);
86 1.1 christos }
87 1.1 christos
88 1.1 christos static int
89 1.1 christos npflog_ioctl(ifnet_t *ifp, u_long cmd, void *data)
90 1.1 christos {
91 1.1 christos npflog_softc_t *sc = ifp->if_softc;
92 1.1 christos int error = 0;
93 1.1 christos
94 1.1 christos mutex_enter(&sc->sc_lock);
95 1.1 christos switch (cmd) {
96 1.1 christos case SIOCINITIFADDR:
97 1.1 christos ifp->if_flags |= (IFF_UP | IFF_RUNNING);
98 1.1 christos break;
99 1.1 christos default:
100 1.1 christos error = ifioctl_common(ifp, cmd, data);
101 1.1 christos break;
102 1.1 christos }
103 1.1 christos mutex_exit(&sc->sc_lock);
104 1.1 christos return error;
105 1.1 christos }
106 1.1 christos
107 1.1 christos static int
108 1.1 christos npflog_clone_create(struct if_clone *ifc, int unit)
109 1.1 christos {
110 1.1 christos npflog_softc_t *sc;
111 1.1 christos ifnet_t *ifp;
112 1.1 christos
113 1.1 christos sc = kmem_zalloc(sizeof(npflog_softc_t), KM_SLEEP);
114 1.1 christos mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTNET);
115 1.1 christos
116 1.1 christos ifp = &sc->sc_if;
117 1.1 christos ifp->if_softc = sc;
118 1.1 christos
119 1.1 christos if_initname(ifp, "npflog", unit);
120 1.1 christos ifp->if_type = IFT_OTHER;
121 1.1 christos ifp->if_dlt = DLT_NULL;
122 1.1 christos ifp->if_ioctl = npflog_ioctl;
123 1.1 christos
124 1.1 christos KERNEL_LOCK(1, NULL);
125 1.1 christos if_attach(ifp);
126 1.1 christos if_alloc_sadl(ifp);
127 1.1 christos bpf_attach(ifp, DLT_NULL, 0);
128 1.1 christos LIST_INSERT_HEAD(&npflog_if_list, sc, sc_entry);
129 1.1 christos KERNEL_UNLOCK_ONE(NULL);
130 1.1 christos
131 1.1 christos return 0;
132 1.1 christos }
133 1.1 christos
134 1.1 christos static int
135 1.1 christos npflog_clone_destroy(ifnet_t *ifp)
136 1.1 christos {
137 1.1 christos npflog_softc_t *sc = ifp->if_softc;
138 1.1 christos
139 1.1 christos KERNEL_LOCK(1, NULL);
140 1.1 christos LIST_REMOVE(sc, sc_entry);
141 1.1 christos bpf_detach(ifp);
142 1.1 christos if_detach(ifp);
143 1.1 christos KERNEL_UNLOCK_ONE(NULL);
144 1.1 christos
145 1.1 christos mutex_destroy(&sc->sc_lock);
146 1.1 christos kmem_free(sc, sizeof(npflog_softc_t));
147 1.1 christos return 0;
148 1.1 christos }
149 1.1 christos
150 1.1 christos /*
151 1.1 christos * Module interface.
152 1.1 christos */
153 1.1 christos static int
154 1.1 christos if_npflog_modcmd(modcmd_t cmd, void *arg)
155 1.1 christos {
156 1.1 christos switch (cmd) {
157 1.1 christos case MODULE_CMD_INIT:
158 1.1 christos npflogattach(1);
159 1.1 christos break;
160 1.1 christos
161 1.1 christos case MODULE_CMD_FINI:
162 1.1 christos npflogdetach();
163 1.1 christos break;
164 1.1 christos
165 1.2 christos case MODULE_CMD_AUTOUNLOAD:
166 1.2 christos return EBUSY;
167 1.2 christos
168 1.1 christos default:
169 1.1 christos return ENOTTY;
170 1.1 christos }
171 1.1 christos return 0;
172 1.1 christos }
173