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