Home | History | Annotate | Line # | Download | only in npf
npf_ext_log.c revision 1.1.4.2
      1  1.1.4.2  riz /*	$NetBSD: npf_ext_log.c,v 1.1.4.2 2012/11/18 22:38:26 riz Exp $	*/
      2  1.1.4.2  riz 
      3  1.1.4.2  riz /*-
      4  1.1.4.2  riz  * Copyright (c) 2010-2012 The NetBSD Foundation, Inc.
      5  1.1.4.2  riz  * All rights reserved.
      6  1.1.4.2  riz  *
      7  1.1.4.2  riz  * This material is based upon work partially supported by The
      8  1.1.4.2  riz  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
      9  1.1.4.2  riz  *
     10  1.1.4.2  riz  * Redistribution and use in source and binary forms, with or without
     11  1.1.4.2  riz  * modification, are permitted provided that the following conditions
     12  1.1.4.2  riz  * are met:
     13  1.1.4.2  riz  * 1. Redistributions of source code must retain the above copyright
     14  1.1.4.2  riz  *    notice, this list of conditions and the following disclaimer.
     15  1.1.4.2  riz  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1.4.2  riz  *    notice, this list of conditions and the following disclaimer in the
     17  1.1.4.2  riz  *    documentation and/or other materials provided with the distribution.
     18  1.1.4.2  riz  *
     19  1.1.4.2  riz  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1.4.2  riz  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1.4.2  riz  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1.4.2  riz  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1.4.2  riz  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1.4.2  riz  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1.4.2  riz  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1.4.2  riz  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1.4.2  riz  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1.4.2  riz  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1.4.2  riz  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1.4.2  riz  */
     31  1.1.4.2  riz 
     32  1.1.4.2  riz /*
     33  1.1.4.2  riz  * NPF logging extension.
     34  1.1.4.2  riz  */
     35  1.1.4.2  riz 
     36  1.1.4.2  riz #include <sys/cdefs.h>
     37  1.1.4.2  riz __KERNEL_RCSID(0, "$NetBSD: npf_ext_log.c,v 1.1.4.2 2012/11/18 22:38:26 riz Exp $");
     38  1.1.4.2  riz 
     39  1.1.4.2  riz #include <sys/types.h>
     40  1.1.4.2  riz #include <sys/module.h>
     41  1.1.4.2  riz 
     42  1.1.4.2  riz #include <sys/conf.h>
     43  1.1.4.2  riz #include <sys/kmem.h>
     44  1.1.4.2  riz #include <sys/mbuf.h>
     45  1.1.4.2  riz #include <sys/mutex.h>
     46  1.1.4.2  riz #include <sys/queue.h>
     47  1.1.4.2  riz 
     48  1.1.4.2  riz #include <net/if.h>
     49  1.1.4.2  riz #include <net/if_types.h>
     50  1.1.4.2  riz #include <net/bpf.h>
     51  1.1.4.2  riz 
     52  1.1.4.2  riz #include "npf_impl.h"
     53  1.1.4.2  riz 
     54  1.1.4.2  riz NPF_EXT_MODULE(npf_ext_log, "");
     55  1.1.4.2  riz 
     56  1.1.4.2  riz #define	NPFEXT_LOG_VER		1
     57  1.1.4.2  riz 
     58  1.1.4.2  riz static void *		npf_ext_log_id;
     59  1.1.4.2  riz 
     60  1.1.4.2  riz typedef struct {
     61  1.1.4.2  riz 	unsigned int	if_idx;
     62  1.1.4.2  riz } npf_ext_log_t;
     63  1.1.4.2  riz 
     64  1.1.4.2  riz typedef struct npflog_softc {
     65  1.1.4.2  riz 	LIST_ENTRY(npflog_softc)	sc_entry;
     66  1.1.4.2  riz 	kmutex_t			sc_lock;
     67  1.1.4.2  riz 	ifnet_t				sc_if;
     68  1.1.4.2  riz 	int				sc_unit;
     69  1.1.4.2  riz } npflog_softc_t;
     70  1.1.4.2  riz 
     71  1.1.4.2  riz static int	npflog_clone_create(struct if_clone *, int);
     72  1.1.4.2  riz static int	npflog_clone_destroy(ifnet_t *);
     73  1.1.4.2  riz 
     74  1.1.4.2  riz static LIST_HEAD(, npflog_softc)	npflog_if_list	__cacheline_aligned;
     75  1.1.4.2  riz static struct if_clone			npflog_cloner =
     76  1.1.4.2  riz     IF_CLONE_INITIALIZER("npflog", npflog_clone_create, npflog_clone_destroy);
     77  1.1.4.2  riz 
     78  1.1.4.2  riz void
     79  1.1.4.2  riz npflogattach(int nunits)
     80  1.1.4.2  riz {
     81  1.1.4.2  riz 
     82  1.1.4.2  riz 	LIST_INIT(&npflog_if_list);
     83  1.1.4.2  riz 	if_clone_attach(&npflog_cloner);
     84  1.1.4.2  riz }
     85  1.1.4.2  riz 
     86  1.1.4.2  riz void
     87  1.1.4.2  riz npflogdetach(void)
     88  1.1.4.2  riz {
     89  1.1.4.2  riz 	npflog_softc_t *sc;
     90  1.1.4.2  riz 
     91  1.1.4.2  riz 	while ((sc = LIST_FIRST(&npflog_if_list)) != NULL) {
     92  1.1.4.2  riz 		npflog_clone_destroy(&sc->sc_if);
     93  1.1.4.2  riz 	}
     94  1.1.4.2  riz 	if_clone_detach(&npflog_cloner);
     95  1.1.4.2  riz }
     96  1.1.4.2  riz 
     97  1.1.4.2  riz static int
     98  1.1.4.2  riz npflog_ioctl(ifnet_t *ifp, u_long cmd, void *data)
     99  1.1.4.2  riz {
    100  1.1.4.2  riz 	npflog_softc_t *sc = ifp->if_softc;
    101  1.1.4.2  riz 	int error = 0;
    102  1.1.4.2  riz 
    103  1.1.4.2  riz 	mutex_enter(&sc->sc_lock);
    104  1.1.4.2  riz 	switch (cmd) {
    105  1.1.4.2  riz 	case SIOCINITIFADDR:
    106  1.1.4.2  riz 		ifp->if_flags |= (IFF_UP | IFF_RUNNING);
    107  1.1.4.2  riz 		break;
    108  1.1.4.2  riz 	default:
    109  1.1.4.2  riz 		error = ifioctl_common(ifp, cmd, data);
    110  1.1.4.2  riz 		break;
    111  1.1.4.2  riz 	}
    112  1.1.4.2  riz 	mutex_exit(&sc->sc_lock);
    113  1.1.4.2  riz 	return error;
    114  1.1.4.2  riz }
    115  1.1.4.2  riz 
    116  1.1.4.2  riz static int
    117  1.1.4.2  riz npflog_clone_create(struct if_clone *ifc, int unit)
    118  1.1.4.2  riz {
    119  1.1.4.2  riz 	npflog_softc_t *sc;
    120  1.1.4.2  riz 	ifnet_t *ifp;
    121  1.1.4.2  riz 
    122  1.1.4.2  riz 	sc = kmem_zalloc(sizeof(npflog_softc_t), KM_SLEEP);
    123  1.1.4.2  riz 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTNET);
    124  1.1.4.2  riz 
    125  1.1.4.2  riz 	ifp = &sc->sc_if;
    126  1.1.4.2  riz 	ifp->if_softc = sc;
    127  1.1.4.2  riz 
    128  1.1.4.2  riz 	if_initname(ifp, "npflog", unit);
    129  1.1.4.2  riz 	ifp->if_type = IFT_OTHER;
    130  1.1.4.2  riz 	ifp->if_dlt = DLT_NULL;
    131  1.1.4.2  riz 	ifp->if_ioctl = npflog_ioctl;
    132  1.1.4.2  riz 
    133  1.1.4.2  riz 	KERNEL_LOCK(1, NULL);
    134  1.1.4.2  riz 	if_attach(ifp);
    135  1.1.4.2  riz 	if_alloc_sadl(ifp);
    136  1.1.4.2  riz 	bpf_attach(ifp, DLT_NULL, 0);
    137  1.1.4.2  riz 	LIST_INSERT_HEAD(&npflog_if_list, sc, sc_entry);
    138  1.1.4.2  riz 	KERNEL_UNLOCK_ONE(NULL);
    139  1.1.4.2  riz 
    140  1.1.4.2  riz 	return 0;
    141  1.1.4.2  riz }
    142  1.1.4.2  riz 
    143  1.1.4.2  riz static int
    144  1.1.4.2  riz npflog_clone_destroy(ifnet_t *ifp)
    145  1.1.4.2  riz {
    146  1.1.4.2  riz 	npflog_softc_t *sc = ifp->if_softc;
    147  1.1.4.2  riz 
    148  1.1.4.2  riz 	KERNEL_LOCK(1, NULL);
    149  1.1.4.2  riz 	LIST_REMOVE(sc, sc_entry);
    150  1.1.4.2  riz 	bpf_detach(ifp);
    151  1.1.4.2  riz 	if_detach(ifp);
    152  1.1.4.2  riz 	KERNEL_UNLOCK_ONE(NULL);
    153  1.1.4.2  riz 
    154  1.1.4.2  riz 	mutex_destroy(&sc->sc_lock);
    155  1.1.4.2  riz 	kmem_free(sc, sizeof(npflog_softc_t));
    156  1.1.4.2  riz 	return 0;
    157  1.1.4.2  riz }
    158  1.1.4.2  riz 
    159  1.1.4.2  riz static int
    160  1.1.4.2  riz npf_log_ctor(npf_rproc_t *rp, prop_dictionary_t params)
    161  1.1.4.2  riz {
    162  1.1.4.2  riz 	npf_ext_log_t *meta;
    163  1.1.4.2  riz 
    164  1.1.4.2  riz 	meta = kmem_zalloc(sizeof(npf_ext_log_t), KM_SLEEP);
    165  1.1.4.2  riz 	prop_dictionary_get_uint32(params, "log-interface", &meta->if_idx);
    166  1.1.4.2  riz 	npf_rproc_assign(rp, meta);
    167  1.1.4.2  riz 	return 0;
    168  1.1.4.2  riz }
    169  1.1.4.2  riz 
    170  1.1.4.2  riz static void
    171  1.1.4.2  riz npf_log_dtor(npf_rproc_t *rp, void *meta)
    172  1.1.4.2  riz {
    173  1.1.4.2  riz 	kmem_free(meta, sizeof(npf_ext_log_t));
    174  1.1.4.2  riz }
    175  1.1.4.2  riz 
    176  1.1.4.2  riz static void
    177  1.1.4.2  riz npf_log(npf_cache_t *npc, nbuf_t *nbuf, void *meta, int *decision)
    178  1.1.4.2  riz {
    179  1.1.4.2  riz 	const npf_ext_log_t *log = meta;
    180  1.1.4.2  riz 	struct mbuf *m = nbuf;
    181  1.1.4.2  riz 	ifnet_t *ifp;
    182  1.1.4.2  riz 	int family;
    183  1.1.4.2  riz 
    184  1.1.4.2  riz 	/* Set the address family. */
    185  1.1.4.2  riz 	if (npf_iscached(npc, NPC_IP4)) {
    186  1.1.4.2  riz 		family = AF_INET;
    187  1.1.4.2  riz 	} else if (npf_iscached(npc, NPC_IP6)) {
    188  1.1.4.2  riz 		family = AF_INET6;
    189  1.1.4.2  riz 	} else {
    190  1.1.4.2  riz 		family = AF_UNSPEC;
    191  1.1.4.2  riz 	}
    192  1.1.4.2  riz 
    193  1.1.4.2  riz 	KERNEL_LOCK(1, NULL);
    194  1.1.4.2  riz 
    195  1.1.4.2  riz 	/* Find a pseudo-interface to log. */
    196  1.1.4.2  riz 	ifp = if_byindex(log->if_idx);
    197  1.1.4.2  riz 	if (ifp == NULL) {
    198  1.1.4.2  riz 		/* No interface. */
    199  1.1.4.2  riz 		KERNEL_UNLOCK_ONE(NULL);
    200  1.1.4.2  riz 		return;
    201  1.1.4.2  riz 	}
    202  1.1.4.2  riz 
    203  1.1.4.2  riz 	/* Pass through BPF. */
    204  1.1.4.2  riz 	ifp->if_opackets++;
    205  1.1.4.2  riz 	ifp->if_obytes += m->m_pkthdr.len;
    206  1.1.4.2  riz 	bpf_mtap_af(ifp, family, m);
    207  1.1.4.2  riz 	KERNEL_UNLOCK_ONE(NULL);
    208  1.1.4.2  riz }
    209  1.1.4.2  riz 
    210  1.1.4.2  riz /*
    211  1.1.4.2  riz  * Module interface.
    212  1.1.4.2  riz  */
    213  1.1.4.2  riz static int
    214  1.1.4.2  riz npf_ext_log_modcmd(modcmd_t cmd, void *arg)
    215  1.1.4.2  riz {
    216  1.1.4.2  riz 	static const npf_ext_ops_t npf_log_ops = {
    217  1.1.4.2  riz 		.version	= NPFEXT_LOG_VER,
    218  1.1.4.2  riz 		.ctx		= NULL,
    219  1.1.4.2  riz 		.ctor		= npf_log_ctor,
    220  1.1.4.2  riz 		.dtor		= npf_log_dtor,
    221  1.1.4.2  riz 		.proc		= npf_log
    222  1.1.4.2  riz 	};
    223  1.1.4.2  riz 	int error;
    224  1.1.4.2  riz 
    225  1.1.4.2  riz 	switch (cmd) {
    226  1.1.4.2  riz 	case MODULE_CMD_INIT:
    227  1.1.4.2  riz 		/*
    228  1.1.4.2  riz 		 * Initialise the NPF logging extension.
    229  1.1.4.2  riz 		 */
    230  1.1.4.2  riz 		npflogattach(1);
    231  1.1.4.2  riz 		npf_ext_log_id = npf_ext_register("log", &npf_log_ops);
    232  1.1.4.2  riz 		if (!npf_ext_log_id) {
    233  1.1.4.2  riz 			npflogdetach();
    234  1.1.4.2  riz 			return EEXIST;
    235  1.1.4.2  riz 		}
    236  1.1.4.2  riz 		break;
    237  1.1.4.2  riz 
    238  1.1.4.2  riz 	case MODULE_CMD_FINI:
    239  1.1.4.2  riz 		error = npf_ext_unregister(npf_ext_log_id);
    240  1.1.4.2  riz 		if (error) {
    241  1.1.4.2  riz 			return error;
    242  1.1.4.2  riz 		}
    243  1.1.4.2  riz 		npflogdetach();
    244  1.1.4.2  riz 		break;
    245  1.1.4.2  riz 
    246  1.1.4.2  riz 	case MODULE_CMD_AUTOUNLOAD:
    247  1.1.4.2  riz 		/* Allow auto-unload only if NPF permits it. */
    248  1.1.4.2  riz 		return npf_autounload_p() ? 0 : EBUSY;
    249  1.1.4.2  riz 
    250  1.1.4.2  riz 	default:
    251  1.1.4.2  riz 		return ENOTTY;
    252  1.1.4.2  riz 	}
    253  1.1.4.2  riz 	return 0;
    254  1.1.4.2  riz }
    255