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