if_npflog.c revision 1.5 1 /* $NetBSD: if_npflog.c,v 1.5 2017/01/29 00:15:54 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: if_npflog.c,v 1.5 2017/01/29 00:15:54 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 #include <sys/sockio.h>
49
50 #include <net/if.h>
51 #include <net/if_types.h>
52 #include <net/bpf.h>
53 #endif
54
55 #include "npf_impl.h"
56 #include "if_npflog.h"
57
58 MODULE(MODULE_CLASS_DRIVER, if_npflog, NULL);
59
60 typedef struct npflog_softc {
61 LIST_ENTRY(npflog_softc) sc_entry;
62 kmutex_t sc_lock;
63 ifnet_t sc_if;
64 int sc_unit;
65 } npflog_softc_t;
66
67 static int npflog_clone_create(struct if_clone *, int);
68 static int npflog_clone_destroy(ifnet_t *);
69
70 static LIST_HEAD(, npflog_softc) npflog_if_list __cacheline_aligned;
71 static struct if_clone npflog_cloner =
72 IF_CLONE_INITIALIZER("npflog", npflog_clone_create, npflog_clone_destroy);
73
74 static void
75 npflogattach(int nunits)
76 {
77
78 LIST_INIT(&npflog_if_list);
79 if_clone_attach(&npflog_cloner);
80 }
81
82 static void
83 npflogdetach(void)
84 {
85 npflog_softc_t *sc;
86
87 while ((sc = LIST_FIRST(&npflog_if_list)) != NULL) {
88 npflog_clone_destroy(&sc->sc_if);
89 }
90 if_clone_detach(&npflog_cloner);
91 }
92
93 static int
94 npflog_ioctl(ifnet_t *ifp, u_long cmd, void *data)
95 {
96 npflog_softc_t *sc = ifp->if_softc;
97 int error = 0;
98
99 mutex_enter(&sc->sc_lock);
100 switch (cmd) {
101 case SIOCINITIFADDR:
102 ifp->if_flags |= (IFF_UP | IFF_RUNNING);
103 break;
104 default:
105 error = ifioctl_common(ifp, cmd, data);
106 break;
107 }
108 mutex_exit(&sc->sc_lock);
109 return error;
110 }
111
112 static int
113 npflog_clone_create(struct if_clone *ifc, int unit)
114 {
115 npflog_softc_t *sc;
116 ifnet_t *ifp;
117
118 sc = kmem_zalloc(sizeof(npflog_softc_t), KM_SLEEP);
119 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTNET);
120
121 ifp = &sc->sc_if;
122 ifp->if_softc = sc;
123
124 if_initname(ifp, "npflog", unit);
125 ifp->if_type = IFT_OTHER;
126 ifp->if_dlt = DLT_NULL;
127 ifp->if_ioctl = npflog_ioctl;
128
129 KERNEL_LOCK(1, NULL);
130 if_attach(ifp);
131 if_alloc_sadl(ifp);
132 bpf_attach(ifp, DLT_NPFLOG, NPFLOG_HDRLEN);
133 LIST_INSERT_HEAD(&npflog_if_list, sc, sc_entry);
134 KERNEL_UNLOCK_ONE(NULL);
135
136 return 0;
137 }
138
139 static int
140 npflog_clone_destroy(ifnet_t *ifp)
141 {
142 npflog_softc_t *sc = ifp->if_softc;
143
144 KERNEL_LOCK(1, NULL);
145 LIST_REMOVE(sc, sc_entry);
146 bpf_detach(ifp);
147 if_detach(ifp);
148 KERNEL_UNLOCK_ONE(NULL);
149
150 mutex_destroy(&sc->sc_lock);
151 kmem_free(sc, sizeof(npflog_softc_t));
152 return 0;
153 }
154
155 /*
156 * Module interface.
157 */
158 static int
159 if_npflog_modcmd(modcmd_t cmd, void *arg)
160 {
161 switch (cmd) {
162 case MODULE_CMD_INIT:
163 npflogattach(1);
164 break;
165
166 case MODULE_CMD_FINI:
167 npflogdetach();
168 break;
169
170 case MODULE_CMD_AUTOUNLOAD:
171 return EBUSY;
172
173 default:
174 return ENOTTY;
175 }
176 return 0;
177 }
178