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