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