1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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 #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/sockio.h> 45 #include <sys/socket.h> 46 #include <sys/socketvar.h> 47 #include <sys/errno.h> 48 #include <sys/callout.h> 49 #include <sys/endian.h> 50 #include <sys/kthread.h> 51 #include <sys/taskqueue.h> 52 #include <sys/priv.h> 53 #include <sys/sysctl.h> 54 55 #include <machine/bus.h> 56 57 #include <net/if.h> 58 #include <net/if_dl.h> 59 #include <net/if_media.h> 60 #include <net/if_types.h> 61 #include <net/if_arp.h> 62 #include <net/ethernet.h> 63 #include <net/if_llc.h> 64 #include <net/vnet.h> 65 66 #include <net80211/ieee80211_var.h> 67 #include <net80211/ieee80211_regdomain.h> 68 69 #include <net/bpf.h> 70 71 #include <sys/errno.h> 72 #include <sys/conf.h> /* cdevsw struct */ 73 #include <sys/uio.h> /* uio struct */ 74 75 #include <netinet/in.h> 76 #include <netinet/if_ether.h> 77 78 #include "visibility.h" 79 80 /* Function prototypes */ 81 static d_ioctl_t vis_ioctl; 82 83 static struct cdevsw vis_cdevsw = { 84 .d_version = D_VERSION, 85 .d_flags = 0, 86 .d_ioctl = vis_ioctl, 87 .d_name = "visctl", 88 }; 89 90 void 91 visibility_init(struct wtap_plugin *plugin) 92 { 93 struct visibility_plugin *vis_plugin; 94 95 vis_plugin = (struct visibility_plugin *) plugin; 96 plugin->wp_sdev = make_dev(&vis_cdevsw,0,UID_ROOT,GID_WHEEL,0600, 97 (const char *)"visctl"); 98 plugin->wp_sdev->si_drv1 = vis_plugin; 99 mtx_init(&vis_plugin->pl_mtx, "visibility_plugin mtx", 100 NULL, MTX_DEF | MTX_RECURSE); 101 printf("Using visibility wtap plugin...\n"); 102 } 103 104 void 105 visibility_deinit(struct wtap_plugin *plugin) 106 { 107 struct visibility_plugin *vis_plugin; 108 109 vis_plugin = (struct visibility_plugin *) plugin; 110 destroy_dev(plugin->wp_sdev); 111 mtx_destroy(&vis_plugin->pl_mtx); 112 free(vis_plugin, M_WTAP_PLUGIN); 113 printf("Removing visibility wtap plugin...\n"); 114 } 115 116 /* We need to use a mutex lock when we read out a visibility map 117 * and when we change visibility map from user space through IOCTL 118 */ 119 void 120 visibility_work(struct wtap_plugin *plugin, struct packet *p) 121 { 122 struct visibility_plugin *vis_plugin = 123 (struct visibility_plugin *) plugin; 124 struct wtap_hal *hal = (struct wtap_hal *)vis_plugin->base.wp_hal; 125 struct vis_map *map; 126 127 KASSERT(mtod(p->m, const char *) != (const char *) 0xdeadc0de || 128 mtod(p->m, const char *) != NULL, 129 ("[%s] got a corrupt packet from master queue, p->m=%p, p->id=%d\n", 130 __func__, p->m, p->id)); 131 DWTAP_PRINTF("[%d] BROADCASTING m=%p\n", p->id, p->m); 132 mtx_lock(&vis_plugin->pl_mtx); 133 map = &vis_plugin->pl_node[p->id]; 134 mtx_unlock(&vis_plugin->pl_mtx); 135 136 /* This is O(n*n) which is not optimal for large 137 * number of nodes. Another way of doing it is 138 * creating groups of nodes that hear each other. 139 * Atleast for this simple static node plugin. 140 */ 141 for(int i=0; i<ARRAY_SIZE; ++i){ 142 uint32_t index = map->map[i]; 143 for(int j=0; j<32; ++j){ 144 int vis = index & 0x01; 145 if(vis){ 146 int k = i*ARRAY_SIZE + j; 147 if(hal->hal_devs[k] != NULL 148 && hal->hal_devs[k]->up == 1){ 149 struct wtap_softc *sc = 150 hal->hal_devs[k]; 151 struct mbuf *m = 152 m_dup(p->m, M_NOWAIT); 153 DWTAP_PRINTF("[%d] duplicated old_m=%p" 154 "to new_m=%p\n", p->id, p->m, m); 155 #if 0 156 printf("[%d] sending to %d\n", 157 p->id, k); 158 #endif 159 wtap_inject(sc, m); 160 } 161 } 162 index = index >> 1; 163 } 164 } 165 } 166 167 static void 168 add_link(struct visibility_plugin *vis_plugin, struct link *l) 169 { 170 171 mtx_lock(&vis_plugin->pl_mtx); 172 struct vis_map *map = &vis_plugin->pl_node[l->id1]; 173 int index = l->id2/ARRAY_SIZE; 174 int bit = l->id2 % ARRAY_SIZE; 175 uint32_t value = 1 << bit; 176 map->map[index] = map->map[index] | value; 177 mtx_unlock(&vis_plugin->pl_mtx); 178 #if 0 179 printf("l->id1=%d, l->id2=%d, map->map[%d] = %u, bit=%d\n", 180 l->id1, l->id2, index, map->map[index], bit); 181 #endif 182 } 183 184 static void 185 del_link(struct visibility_plugin *vis_plugin, struct link *l) 186 { 187 188 mtx_lock(&vis_plugin->pl_mtx); 189 struct vis_map *map = &vis_plugin->pl_node[l->id1]; 190 int index = l->id2/ARRAY_SIZE; 191 int bit = l->id2 % ARRAY_SIZE; 192 uint32_t value = 1 << bit; 193 map->map[index] = map->map[index] & ~value; 194 mtx_unlock(&vis_plugin->pl_mtx); 195 #if 0 196 printf("map->map[index] = %u\n", map->map[index]); 197 #endif 198 } 199 200 int 201 vis_ioctl(struct cdev *sdev, u_long cmd, caddr_t data, 202 int fflag, struct thread *td) 203 { 204 struct visibility_plugin *vis_plugin = 205 (struct visibility_plugin *) sdev->si_drv1; 206 struct wtap_hal *hal = vis_plugin->base.wp_hal; 207 struct link l; 208 int op; 209 int error = 0; 210 211 CURVNET_SET(CRED_TO_VNET(curthread->td_ucred)); 212 switch(cmd) { 213 case VISIOCTLOPEN: 214 op = *(int *)data; 215 if(op == 0) 216 medium_close(hal->hal_md); 217 else 218 medium_open(hal->hal_md); 219 break; 220 case VISIOCTLLINK: 221 l = *(struct link *)data; 222 if(l.op == 0) 223 del_link(vis_plugin, &l); 224 else 225 add_link(vis_plugin, &l); 226 #if 0 227 printf("op=%d, id1=%d, id2=%d\n", l.op, l.id1, l.id2); 228 #endif 229 break; 230 default: 231 DWTAP_PRINTF("Unknown WTAP IOCTL\n"); 232 error = EINVAL; 233 } 234 235 CURVNET_RESTORE(); 236 return error; 237 } 238