Home | History | Annotate | Line # | Download | only in npf
npf_ext_log.c revision 1.9.2.1
      1  1.9.2.1  pgoyette /*	$NetBSD: npf_ext_log.c,v 1.9.2.1 2017/01/07 08:56:50 pgoyette Exp $	*/
      2      1.1     rmind 
      3      1.1     rmind /*-
      4      1.1     rmind  * Copyright (c) 2010-2012 The NetBSD Foundation, Inc.
      5      1.1     rmind  * All rights reserved.
      6      1.1     rmind  *
      7      1.1     rmind  * This material is based upon work partially supported by The
      8      1.1     rmind  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
      9      1.1     rmind  *
     10      1.1     rmind  * Redistribution and use in source and binary forms, with or without
     11      1.1     rmind  * modification, are permitted provided that the following conditions
     12      1.1     rmind  * are met:
     13      1.1     rmind  * 1. Redistributions of source code must retain the above copyright
     14      1.1     rmind  *    notice, this list of conditions and the following disclaimer.
     15      1.1     rmind  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1     rmind  *    notice, this list of conditions and the following disclaimer in the
     17      1.1     rmind  *    documentation and/or other materials provided with the distribution.
     18      1.1     rmind  *
     19      1.1     rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.1     rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.1     rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.1     rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.1     rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.1     rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.1     rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.1     rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.1     rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.1     rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.1     rmind  * POSSIBILITY OF SUCH DAMAGE.
     30      1.1     rmind  */
     31      1.1     rmind 
     32      1.1     rmind /*
     33      1.1     rmind  * NPF logging extension.
     34      1.1     rmind  */
     35      1.1     rmind 
     36  1.9.2.1  pgoyette #ifdef _KERNEL
     37      1.1     rmind #include <sys/cdefs.h>
     38  1.9.2.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: npf_ext_log.c,v 1.9.2.1 2017/01/07 08:56:50 pgoyette Exp $");
     39      1.1     rmind 
     40      1.1     rmind #include <sys/types.h>
     41      1.1     rmind #include <sys/module.h>
     42      1.1     rmind 
     43      1.1     rmind #include <sys/conf.h>
     44      1.1     rmind #include <sys/kmem.h>
     45      1.1     rmind #include <sys/mbuf.h>
     46      1.1     rmind #include <sys/mutex.h>
     47      1.1     rmind #include <sys/queue.h>
     48      1.1     rmind 
     49      1.1     rmind #include <net/if.h>
     50      1.1     rmind #include <net/if_types.h>
     51      1.1     rmind #include <net/bpf.h>
     52  1.9.2.1  pgoyette #endif
     53      1.1     rmind 
     54      1.1     rmind #include "npf_impl.h"
     55      1.1     rmind 
     56      1.6  christos NPF_EXT_MODULE(npf_ext_log, "");
     57      1.1     rmind 
     58      1.1     rmind #define	NPFEXT_LOG_VER		1
     59      1.1     rmind 
     60      1.1     rmind static void *		npf_ext_log_id;
     61      1.1     rmind 
     62      1.1     rmind typedef struct {
     63      1.1     rmind 	unsigned int	if_idx;
     64      1.1     rmind } npf_ext_log_t;
     65      1.1     rmind 
     66      1.1     rmind static int
     67      1.1     rmind npf_log_ctor(npf_rproc_t *rp, prop_dictionary_t params)
     68      1.1     rmind {
     69      1.1     rmind 	npf_ext_log_t *meta;
     70      1.1     rmind 
     71      1.1     rmind 	meta = kmem_zalloc(sizeof(npf_ext_log_t), KM_SLEEP);
     72      1.1     rmind 	prop_dictionary_get_uint32(params, "log-interface", &meta->if_idx);
     73      1.1     rmind 	npf_rproc_assign(rp, meta);
     74      1.1     rmind 	return 0;
     75      1.1     rmind }
     76      1.1     rmind 
     77      1.1     rmind static void
     78      1.1     rmind npf_log_dtor(npf_rproc_t *rp, void *meta)
     79      1.1     rmind {
     80      1.1     rmind 	kmem_free(meta, sizeof(npf_ext_log_t));
     81      1.1     rmind }
     82      1.1     rmind 
     83      1.7  jakllsch static bool
     84      1.8     rmind npf_log(npf_cache_t *npc, void *meta, int *decision)
     85      1.1     rmind {
     86      1.8     rmind 	struct mbuf *m = nbuf_head_mbuf(npc->npc_nbuf);
     87      1.1     rmind 	const npf_ext_log_t *log = meta;
     88  1.9.2.1  pgoyette 	struct psref psref;
     89      1.1     rmind 	ifnet_t *ifp;
     90      1.1     rmind 	int family;
     91      1.1     rmind 
     92      1.1     rmind 	/* Set the address family. */
     93      1.1     rmind 	if (npf_iscached(npc, NPC_IP4)) {
     94      1.1     rmind 		family = AF_INET;
     95      1.1     rmind 	} else if (npf_iscached(npc, NPC_IP6)) {
     96      1.1     rmind 		family = AF_INET6;
     97      1.1     rmind 	} else {
     98      1.1     rmind 		family = AF_UNSPEC;
     99      1.1     rmind 	}
    100      1.1     rmind 
    101      1.1     rmind 	KERNEL_LOCK(1, NULL);
    102      1.1     rmind 
    103      1.1     rmind 	/* Find a pseudo-interface to log. */
    104      1.9     ozaki 	ifp = if_get_byindex(log->if_idx, &psref);
    105      1.1     rmind 	if (ifp == NULL) {
    106      1.1     rmind 		/* No interface. */
    107      1.1     rmind 		KERNEL_UNLOCK_ONE(NULL);
    108      1.7  jakllsch 		return true;
    109      1.1     rmind 	}
    110      1.1     rmind 
    111      1.1     rmind 	/* Pass through BPF. */
    112      1.1     rmind 	ifp->if_opackets++;
    113      1.1     rmind 	ifp->if_obytes += m->m_pkthdr.len;
    114      1.1     rmind 	bpf_mtap_af(ifp, family, m);
    115      1.9     ozaki 	if_put(ifp, &psref);
    116  1.9.2.1  pgoyette 
    117      1.1     rmind 	KERNEL_UNLOCK_ONE(NULL);
    118      1.7  jakllsch 
    119      1.7  jakllsch 	return true;
    120      1.1     rmind }
    121      1.1     rmind 
    122      1.1     rmind /*
    123      1.1     rmind  * Module interface.
    124      1.1     rmind  */
    125      1.1     rmind static int
    126      1.1     rmind npf_ext_log_modcmd(modcmd_t cmd, void *arg)
    127      1.1     rmind {
    128      1.1     rmind 	static const npf_ext_ops_t npf_log_ops = {
    129      1.1     rmind 		.version	= NPFEXT_LOG_VER,
    130      1.1     rmind 		.ctx		= NULL,
    131      1.1     rmind 		.ctor		= npf_log_ctor,
    132      1.1     rmind 		.dtor		= npf_log_dtor,
    133      1.1     rmind 		.proc		= npf_log
    134      1.1     rmind 	};
    135  1.9.2.1  pgoyette 	npf_t *npf = npf_getkernctx();
    136      1.1     rmind 	int error;
    137      1.1     rmind 
    138      1.1     rmind 	switch (cmd) {
    139      1.1     rmind 	case MODULE_CMD_INIT:
    140      1.1     rmind 		/*
    141      1.1     rmind 		 * Initialise the NPF logging extension.
    142      1.1     rmind 		 */
    143  1.9.2.1  pgoyette 		npf_ext_log_id = npf_ext_register(npf, "log", &npf_log_ops);
    144      1.1     rmind 		if (!npf_ext_log_id) {
    145      1.1     rmind 			return EEXIST;
    146      1.1     rmind 		}
    147      1.1     rmind 		break;
    148      1.1     rmind 
    149      1.1     rmind 	case MODULE_CMD_FINI:
    150  1.9.2.1  pgoyette 		error = npf_ext_unregister(npf, npf_ext_log_id);
    151      1.1     rmind 		if (error) {
    152      1.1     rmind 			return error;
    153      1.1     rmind 		}
    154      1.1     rmind 		break;
    155      1.1     rmind 
    156      1.1     rmind 	case MODULE_CMD_AUTOUNLOAD:
    157      1.1     rmind 		/* Allow auto-unload only if NPF permits it. */
    158      1.1     rmind 		return npf_autounload_p() ? 0 : EBUSY;
    159      1.1     rmind 
    160      1.1     rmind 	default:
    161      1.1     rmind 		return ENOTTY;
    162      1.1     rmind 	}
    163      1.1     rmind 	return 0;
    164      1.1     rmind }
    165