Home | History | Annotate | Line # | Download | only in npf
npf_ext_log.c revision 1.9
      1 /*	$NetBSD: npf_ext_log.c,v 1.9 2016/06/16 03:03:33 ozaki-r 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: npf_ext_log.c,v 1.9 2016/06/16 03:03:33 ozaki-r 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 
     48 #include <net/if.h>
     49 #include <net/if_types.h>
     50 #include <net/bpf.h>
     51 
     52 #include "npf_impl.h"
     53 
     54 NPF_EXT_MODULE(npf_ext_log, "");
     55 
     56 #define	NPFEXT_LOG_VER		1
     57 
     58 static void *		npf_ext_log_id;
     59 
     60 typedef struct {
     61 	unsigned int	if_idx;
     62 } npf_ext_log_t;
     63 
     64 static int
     65 npf_log_ctor(npf_rproc_t *rp, prop_dictionary_t params)
     66 {
     67 	npf_ext_log_t *meta;
     68 
     69 	meta = kmem_zalloc(sizeof(npf_ext_log_t), KM_SLEEP);
     70 	prop_dictionary_get_uint32(params, "log-interface", &meta->if_idx);
     71 	npf_rproc_assign(rp, meta);
     72 	return 0;
     73 }
     74 
     75 static void
     76 npf_log_dtor(npf_rproc_t *rp, void *meta)
     77 {
     78 	kmem_free(meta, sizeof(npf_ext_log_t));
     79 }
     80 
     81 static bool
     82 npf_log(npf_cache_t *npc, void *meta, int *decision)
     83 {
     84 	struct mbuf *m = nbuf_head_mbuf(npc->npc_nbuf);
     85 	const npf_ext_log_t *log = meta;
     86 	ifnet_t *ifp;
     87 	int family;
     88 	struct psref psref;
     89 
     90 	/* Set the address family. */
     91 	if (npf_iscached(npc, NPC_IP4)) {
     92 		family = AF_INET;
     93 	} else if (npf_iscached(npc, NPC_IP6)) {
     94 		family = AF_INET6;
     95 	} else {
     96 		family = AF_UNSPEC;
     97 	}
     98 
     99 	KERNEL_LOCK(1, NULL);
    100 
    101 	/* Find a pseudo-interface to log. */
    102 	ifp = if_get_byindex(log->if_idx, &psref);
    103 	if (ifp == NULL) {
    104 		/* No interface. */
    105 		KERNEL_UNLOCK_ONE(NULL);
    106 		return true;
    107 	}
    108 
    109 	/* Pass through BPF. */
    110 	ifp->if_opackets++;
    111 	ifp->if_obytes += m->m_pkthdr.len;
    112 	bpf_mtap_af(ifp, family, m);
    113 	if_put(ifp, &psref);
    114 	KERNEL_UNLOCK_ONE(NULL);
    115 
    116 	return true;
    117 }
    118 
    119 /*
    120  * Module interface.
    121  */
    122 static int
    123 npf_ext_log_modcmd(modcmd_t cmd, void *arg)
    124 {
    125 	static const npf_ext_ops_t npf_log_ops = {
    126 		.version	= NPFEXT_LOG_VER,
    127 		.ctx		= NULL,
    128 		.ctor		= npf_log_ctor,
    129 		.dtor		= npf_log_dtor,
    130 		.proc		= npf_log
    131 	};
    132 	int error;
    133 
    134 	switch (cmd) {
    135 	case MODULE_CMD_INIT:
    136 		/*
    137 		 * Initialise the NPF logging extension.
    138 		 */
    139 		npf_ext_log_id = npf_ext_register("log", &npf_log_ops);
    140 		if (!npf_ext_log_id) {
    141 			return EEXIST;
    142 		}
    143 		break;
    144 
    145 	case MODULE_CMD_FINI:
    146 		error = npf_ext_unregister(npf_ext_log_id);
    147 		if (error) {
    148 			return error;
    149 		}
    150 		break;
    151 
    152 	case MODULE_CMD_AUTOUNLOAD:
    153 		/* Allow auto-unload only if NPF permits it. */
    154 		return npf_autounload_p() ? 0 : EBUSY;
    155 
    156 	default:
    157 		return ENOTTY;
    158 	}
    159 	return 0;
    160 }
    161