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