1/*- 2 * Copyright (c) 2010-2011 Monthadar Al Jaberi, TerraNet AB 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification. 11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 13 * redistribution must be conditioned upon including a substantially 14 * similar Disclaimer requirement for further binary redistribution. 15 * 16 * NO WARRANTY 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27 * THE POSSIBILITY OF SUCH DAMAGES. 28 * 29 * $FreeBSD$ 30 */ 31#include <sys/param.h> 32#include <sys/module.h> 33#include <sys/kernel.h> 34#include <sys/systm.h> 35#include <sys/sysctl.h> 36#include <sys/mbuf.h> 37#include <sys/malloc.h> 38#include <sys/lock.h> 39#include <sys/mutex.h> 40#include <sys/proc.h> 41#include <sys/ucred.h> 42#include <sys/jail.h> 43 44#include <sys/types.h> 45#include <sys/sockio.h> 46#include <sys/socket.h> 47#include <sys/socketvar.h> 48#include <sys/errno.h> 49#include <sys/callout.h> 50#include <sys/endian.h> 51#include <sys/kthread.h> 52#include <sys/taskqueue.h> 53#include <sys/priv.h> 54#include <sys/sysctl.h> 55 56#include <machine/bus.h> 57 58#include <net/if.h> 59#include <net/if_dl.h> 60#include <net/if_media.h> 61#include <net/if_types.h> 62#include <net/if_arp.h> 63#include <net/ethernet.h> 64#include <net/if_llc.h> 65#include <net/vnet.h> 66 67#include <net80211/ieee80211_var.h> 68#include <net80211/ieee80211_regdomain.h> 69 70#include <net/bpf.h> 71 72 73#include <sys/errno.h> 74#include <sys/conf.h> /* cdevsw struct */ 75#include <sys/uio.h> /* uio struct */ 76 77 78#include <netinet/in.h> 79#include <netinet/if_ether.h> 80 81#include "if_wtapvar.h" 82#include "if_wtapioctl.h" 83#include "if_medium.h" 84#include "wtap_hal/hal.h" 85 86/* WTAP PLUGINS */ 87#include "plugins/visibility.h" 88 89MALLOC_DEFINE(M_WTAP, "wtap", "wtap wireless simulator"); 90MALLOC_DEFINE(M_WTAP_PACKET, "wtap packet", "wtap wireless simulator packet"); 91MALLOC_DEFINE(M_WTAP_RXBUF, "wtap rxbuf", 92 "wtap wireless simulator recieve buffer"); 93MALLOC_DEFINE(M_WTAP_PLUGIN, "wtap plugin", "wtap wireless simulator plugin"); 94 95static struct wtap_hal *hal; 96 97/* Function prototypes */ 98static d_ioctl_t wtap_ioctl; 99 100static struct cdev *sdev; 101static struct cdevsw wtap_cdevsw = { 102 .d_version = D_VERSION, 103 .d_flags = 0, 104 .d_ioctl = wtap_ioctl, 105 .d_name = "wtapctl", 106}; 107 108int 109wtap_ioctl(struct cdev *dev, u_long cmd, caddr_t data, 110 int fflag, struct thread *td) 111{ 112 int error = 0; 113 114 CURVNET_SET(CRED_TO_VNET(curthread->td_ucred)); 115 116 switch(cmd) { 117 case WTAPIOCTLCRT: 118 if(new_wtap(hal, *(int *)data)) 119 error = EINVAL; 120 break; 121 case WTAPIOCTLDEL: 122 if(free_wtap(hal, *(int *)data)) 123 error = EINVAL; 124 break; 125 default: 126 DWTAP_PRINTF("Unkown WTAP IOCTL\n"); 127 error = EINVAL; 128 } 129 130 CURVNET_RESTORE(); 131 return error; 132} 133 134 135/* The function called at load/unload. */ 136static int 137event_handler(module_t module, int event, void *arg) 138{ 139 struct visibility_plugin *plugin; 140 int e = 0; /* Error, 0 for normal return status */ 141 142 switch (event) { 143 case MOD_LOAD: 144 sdev = make_dev(&wtap_cdevsw,0,UID_ROOT, 145 GID_WHEEL,0600,(const char *)"wtapctl"); 146 hal = (struct wtap_hal *)malloc(sizeof(struct wtap_hal), 147 M_WTAP, M_NOWAIT | M_ZERO); 148 bzero(hal, sizeof(struct wtap_hal)); 149 150 init_hal(hal); 151 152 /* Setting up a simple plugin */ 153 plugin = (struct visibility_plugin *)malloc 154 (sizeof(struct visibility_plugin), M_WTAP_PLUGIN, 155 M_NOWAIT | M_ZERO); 156 plugin->base.wp_hal = hal; 157 plugin->base.init = visibility_init; 158 plugin->base.deinit = visibility_deinit; 159 plugin->base.work = visibility_work; 160 register_plugin(hal, (struct wtap_plugin *)plugin); 161 162 printf("Loaded wtap wireless simulator\n"); 163 break; 164 case MOD_UNLOAD: 165 destroy_dev(sdev); 166 deregister_plugin(hal); 167 deinit_hal(hal); 168 free(hal, M_WTAP); 169 printf("Unloading wtap wireless simulator\n"); 170 break; 171 default: 172 e = EOPNOTSUPP; /* Error, Operation Not Supported */ 173 break; 174 } 175 176 return(e); 177} 178 179/* The second argument of DECLARE_MODULE. */ 180static moduledata_t wtap_conf = { 181 "wtap", /* module name */ 182 event_handler, /* event handler */ 183 NULL /* extra data */ 184}; 185 186DECLARE_MODULE(wtap, wtap_conf, SI_SUB_DRIVERS, SI_ORDER_MIDDLE); 187MODULE_DEPEND(wtap, wlan, 1, 1, 1); /* 802.11 media layer */ 188