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