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