1 /* $NetBSD: npf_ext_rndblock.c,v 1.5 2014/07/20 00:37:41 rmind Exp $ */ 2 3 /*- 4 * Copyright (c) 2012 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /* 30 * NPF random blocking extension - kernel module. 31 * This is also a demo extension. 32 */ 33 34 #include <sys/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: npf_ext_rndblock.c,v 1.5 2014/07/20 00:37:41 rmind Exp $"); 36 37 #include <sys/types.h> 38 #include <sys/cprng.h> 39 #include <sys/atomic.h> 40 #include <sys/module.h> 41 #include <sys/kmem.h> 42 43 #include "npf.h" 44 45 /* 46 * NPF extension module definition and the identifier. 47 */ 48 NPF_EXT_MODULE(npf_ext_rndblock, ""); 49 50 #define NPFEXT_RNDBLOCK_VER 1 51 52 static void * npf_ext_rndblock_id; 53 54 #define PERCENTAGE_BASE 10000 55 56 /* 57 * Meta-data structure, containing parameters. 58 */ 59 typedef struct { 60 unsigned int mod; 61 unsigned long counter; 62 unsigned int percentage; 63 } npf_ext_rndblock_t; 64 65 /* 66 * npf_ext_rndblock_ctor: a constructor to parse and store any parameters 67 * associated with a rule procedure, which is being newly created. 68 */ 69 static int 70 npf_ext_rndblock_ctor(npf_rproc_t *rp, prop_dictionary_t params) 71 { 72 npf_ext_rndblock_t *meta; 73 74 /* 75 * Allocate and a associate a structure for the parameter 76 * and our meta-data. 77 */ 78 meta = kmem_zalloc(sizeof(npf_ext_rndblock_t), KM_SLEEP); 79 prop_dictionary_get_uint32(params, "mod", &meta->mod); 80 prop_dictionary_get_uint32(params, "percentage", &meta->percentage); 81 npf_rproc_assign(rp, meta); 82 83 return 0; 84 } 85 86 /* 87 * npf_ext_rndblock_dtor: a destructor for our rule procedure. 88 */ 89 static void 90 npf_ext_rndblock_dtor(npf_rproc_t *rp, void *meta) 91 { 92 /* Free our meta-data, associated with the procedure. */ 93 kmem_free(meta, sizeof(npf_ext_rndblock_t)); 94 } 95 96 /* 97 * npf_ext_rndblock: main routine implementing the extension functionality. 98 */ 99 static bool 100 npf_ext_rndblock(npf_cache_t *npc, void *meta, int *decision) 101 { 102 npf_ext_rndblock_t *rndblock = meta; 103 unsigned long c; 104 105 /* Skip, if already blocking. */ 106 if (*decision == NPF_DECISION_BLOCK) { 107 return true; 108 } 109 110 /* 111 * Sample demo: 112 * 113 * Drop the packets according to the given module or percentage. 114 * 115 * Rule procedures may be executed concurrently in an SMP system. 116 * Use atomic operation to increment the counter. 117 */ 118 c = atomic_inc_ulong_nv(&rndblock->counter); 119 120 if (rndblock->mod) { 121 if ((c % rndblock->mod) == 0) { 122 *decision = NPF_DECISION_BLOCK; 123 } 124 } 125 126 if (rndblock->percentage) { 127 uint32_t w = cprng_fast32() % PERCENTAGE_BASE; 128 if (w <= rndblock->percentage) { 129 *decision = NPF_DECISION_BLOCK; 130 } 131 } 132 133 return true; 134 } 135 136 /* 137 * Module interface. 138 */ 139 static int 140 npf_ext_rndblock_modcmd(modcmd_t cmd, void *arg) 141 { 142 static const npf_ext_ops_t npf_rndblock_ops = { 143 .version = NPFEXT_RNDBLOCK_VER, 144 .ctx = NULL, 145 .ctor = npf_ext_rndblock_ctor, 146 .dtor = npf_ext_rndblock_dtor, 147 .proc = npf_ext_rndblock 148 }; 149 150 switch (cmd) { 151 case MODULE_CMD_INIT: 152 /* 153 * Initialise the NPF extension module. Register the 154 * "rndblock" extensions calls (constructor, destructor, 155 * the processing * routine, etc). 156 */ 157 npf_ext_rndblock_id = npf_ext_register("rndblock", 158 &npf_rndblock_ops); 159 return npf_ext_rndblock_id ? 0 : EEXIST; 160 161 case MODULE_CMD_FINI: 162 /* 163 * Unregister our rndblock extension. NPF may return an 164 * if there are references and it cannot drain them. 165 */ 166 return npf_ext_unregister(npf_ext_rndblock_id); 167 168 case MODULE_CMD_AUTOUNLOAD: 169 /* Allow auto-unload only if NPF permits it. */ 170 return npf_autounload_p() ? 0 : EBUSY; 171 172 default: 173 return ENOTTY; 174 } 175 return 0; 176 } 177