Index: head/sys/dev/wtap/if_wtap_module.c =================================================================== --- head/sys/dev/wtap/if_wtap_module.c (revision 292165) +++ head/sys/dev/wtap/if_wtap_module.c (revision 292166) @@ -1,187 +1,186 @@ /*- * Copyright (c) 2010-2011 Monthadar Al Jaberi, TerraNet AB * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer, * without modification. * 2. Redistributions in binary form must reproduce at minimum a disclaimer * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any * redistribution must be conditioned upon including a substantially * similar Disclaimer requirement for further binary redistribution. * * NO WARRANTY * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGES. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include #include #include -#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* cdevsw struct */ #include /* uio struct */ #include #include #include "if_wtapvar.h" #include "if_wtapioctl.h" #include "if_medium.h" #include "wtap_hal/hal.h" /* WTAP PLUGINS */ #include "plugins/visibility.h" MALLOC_DEFINE(M_WTAP, "wtap", "wtap wireless simulator"); MALLOC_DEFINE(M_WTAP_PACKET, "wtap packet", "wtap wireless simulator packet"); MALLOC_DEFINE(M_WTAP_RXBUF, "wtap rxbuf", "wtap wireless simulator recieve buffer"); MALLOC_DEFINE(M_WTAP_PLUGIN, "wtap plugin", "wtap wireless simulator plugin"); static struct wtap_hal *hal; /* Function prototypes */ static d_ioctl_t wtap_ioctl; static struct cdev *sdev; static struct cdevsw wtap_cdevsw = { .d_version = D_VERSION, .d_flags = 0, .d_ioctl = wtap_ioctl, .d_name = "wtapctl", }; int wtap_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) { int error = 0; CURVNET_SET(CRED_TO_VNET(curthread->td_ucred)); switch(cmd) { case WTAPIOCTLCRT: if(new_wtap(hal, *(int *)data)) error = EINVAL; break; case WTAPIOCTLDEL: if(free_wtap(hal, *(int *)data)) error = EINVAL; break; default: DWTAP_PRINTF("Unkown WTAP IOCTL\n"); error = EINVAL; } CURVNET_RESTORE(); return error; } /* The function called at load/unload. */ static int event_handler(module_t module, int event, void *arg) { struct visibility_plugin *plugin; int e = 0; /* Error, 0 for normal return status */ switch (event) { case MOD_LOAD: sdev = make_dev(&wtap_cdevsw,0,UID_ROOT, GID_WHEEL,0600,(const char *)"wtapctl"); hal = (struct wtap_hal *)malloc(sizeof(struct wtap_hal), M_WTAP, M_NOWAIT | M_ZERO); bzero(hal, sizeof(struct wtap_hal)); init_hal(hal); /* Setting up a simple plugin */ plugin = (struct visibility_plugin *)malloc (sizeof(struct visibility_plugin), M_WTAP_PLUGIN, M_NOWAIT | M_ZERO); plugin->base.wp_hal = hal; plugin->base.init = visibility_init; plugin->base.deinit = visibility_deinit; plugin->base.work = visibility_work; register_plugin(hal, (struct wtap_plugin *)plugin); printf("Loaded wtap wireless simulator\n"); break; case MOD_UNLOAD: destroy_dev(sdev); deregister_plugin(hal); deinit_hal(hal); free(hal, M_WTAP); printf("Unloading wtap wireless simulator\n"); break; default: e = EOPNOTSUPP; /* Error, Operation Not Supported */ break; } return(e); } /* The second argument of DECLARE_MODULE. */ static moduledata_t wtap_conf = { "wtap", /* module name */ event_handler, /* event handler */ NULL /* extra data */ }; DECLARE_MODULE(wtap, wtap_conf, SI_SUB_DRIVERS, SI_ORDER_MIDDLE); MODULE_DEPEND(wtap, wlan, 1, 1, 1); /* 802.11 media layer */ Index: head/sys/dev/wtap/if_wtapvar.h =================================================================== --- head/sys/dev/wtap/if_wtapvar.h (revision 292165) +++ head/sys/dev/wtap/if_wtapvar.h (revision 292166) @@ -1,159 +1,157 @@ /*- * Copyright (c) 2010-2011 Monthadar Al Jaberi, TerraNet AB * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer, * without modification. * 2. Redistributions in binary form must reproduce at minimum a disclaimer * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any * redistribution must be conditioned upon including a substantially * similar Disclaimer requirement for further binary redistribution. * * NO WARRANTY * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGES. * * $FreeBSD$ */ #ifndef _DEV_WTAP_WTAPVAR_H #define _DEV_WTAP_WTAPVAR_H -#include #include #include #include #include #include #include #include #include #include #include -#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if 0 #define DWTAP_PRINTF(...) printf(__VA_ARGS__) #else #define DWTAP_PRINTF(...) #endif #include "if_wtapioctl.h" #define MAX_NBR_WTAP (64) #define BEACON_INTRERVAL (1000) MALLOC_DECLARE(M_WTAP); MALLOC_DECLARE(M_WTAP_PACKET); MALLOC_DECLARE(M_WTAP_BEACON); MALLOC_DECLARE(M_WTAP_RXBUF); MALLOC_DECLARE(M_WTAP_PLUGIN); /* driver-specific node state */ struct wtap_node { struct ieee80211_node an_node; /* base class */ /* future addons */ }; #define WTAP_NODE(ni) ((struct ath_node *)(ni)) #define WTAP_NODE_CONST(ni) ((const struct ath_node *)(ni)) struct wtap_buf { STAILQ_ENTRY(wtap_buf) bf_list; struct mbuf *m; /* mbuf for buf */ }; typedef STAILQ_HEAD(, wtap_buf) wtap_bufhead; #define WTAP_BUF_BUSY 0x00000002 /* (tx) desc owned by h/w */ struct wtap_vap { struct ieee80211vap av_vap; /* base class */ int32_t id; /* wtap id */ struct cdev *av_dev; /* userspace injecting frames */ struct wtap_medium *av_md; /* back pointer */ struct mbuf *beacon; /* beacon */ struct ieee80211_node *bf_node; /* pointer to the node */ struct callout av_swba; /* software beacon alert */ uint32_t av_bcinterval; /* beacon interval */ void (*av_recv_mgmt)(struct ieee80211_node *, struct mbuf *, int, const struct ieee80211_rx_stats *, int, int); int (*av_newstate)(struct ieee80211vap *, enum ieee80211_state, int); void (*av_bmiss)(struct ieee80211vap *); }; #define WTAP_VAP(vap) ((struct wtap_vap *)(vap)) struct taskqueue; struct wtap_softc { struct ieee80211com sc_ic; char name[7]; /* wtapXX\0 */ int32_t id; int32_t up; struct wtap_medium *sc_md; /* interface medium */ struct ieee80211_node* (* sc_node_alloc) (struct ieee80211vap *, const uint8_t [IEEE80211_ADDR_LEN]); void (*sc_node_free)(struct ieee80211_node *); struct mtx sc_mtx; /* master lock (recursive) */ struct taskqueue *sc_tq; /* private task queue */ wtap_bufhead sc_rxbuf; /* receive buffer */ struct task sc_rxtask; /* rx int processing */ struct wtap_tx_radiotap_header sc_tx_th; int sc_tx_th_len; struct wtap_rx_radiotap_header sc_rx_th; int sc_rx_th_len; }; int32_t wtap_attach(struct wtap_softc *, const uint8_t *macaddr); int32_t wtap_detach(struct wtap_softc *); void wtap_resume(struct wtap_softc *); void wtap_suspend(struct wtap_softc *); void wtap_shutdown(struct wtap_softc *); void wtap_intr(struct wtap_softc *); void wtap_inject(struct wtap_softc *, struct mbuf *); void wtap_rx_deliver(struct wtap_softc *, struct mbuf *); #endif Index: head/sys/dev/wtap/plugins/visibility.c =================================================================== --- head/sys/dev/wtap/plugins/visibility.c (revision 292165) +++ head/sys/dev/wtap/plugins/visibility.c (revision 292166) @@ -1,241 +1,240 @@ /*- * Copyright (c) 2010-2011 Monthadar Al Jaberi, TerraNet AB * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer, * without modification. * 2. Redistributions in binary form must reproduce at minimum a disclaimer * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any * redistribution must be conditioned upon including a substantially * similar Disclaimer requirement for further binary redistribution. * * NO WARRANTY * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGES. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include #include #include -#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* cdevsw struct */ #include /* uio struct */ #include #include #include "visibility.h" /* Function prototypes */ static d_ioctl_t vis_ioctl; static struct cdevsw vis_cdevsw = { .d_version = D_VERSION, .d_flags = 0, .d_ioctl = vis_ioctl, .d_name = "visctl", }; void visibility_init(struct wtap_plugin *plugin) { struct visibility_plugin *vis_plugin; vis_plugin = (struct visibility_plugin *) plugin; plugin->wp_sdev = make_dev(&vis_cdevsw,0,UID_ROOT,GID_WHEEL,0600, (const char *)"visctl"); plugin->wp_sdev->si_drv1 = vis_plugin; mtx_init(&vis_plugin->pl_mtx, "visibility_plugin mtx", NULL, MTX_DEF | MTX_RECURSE); printf("Using visibility wtap plugin...\n"); } void visibility_deinit(struct wtap_plugin *plugin) { struct visibility_plugin *vis_plugin; vis_plugin = (struct visibility_plugin *) plugin; destroy_dev(plugin->wp_sdev); mtx_destroy(&vis_plugin->pl_mtx); free(vis_plugin, M_WTAP_PLUGIN); printf("Removing visibility wtap plugin...\n"); } /* We need to use a mutex lock when we read out a visibility map * and when we change visibility map from user space through IOCTL */ void visibility_work(struct wtap_plugin *plugin, struct packet *p) { struct visibility_plugin *vis_plugin = (struct visibility_plugin *) plugin; struct wtap_hal *hal = (struct wtap_hal *)vis_plugin->base.wp_hal; struct vis_map *map; KASSERT(mtod(p->m, const char *) != (const char *) 0xdeadc0de || mtod(p->m, const char *) != NULL, ("[%s] got a corrupt packet from master queue, p->m=%p, p->id=%d\n", __func__, p->m, p->id)); DWTAP_PRINTF("[%d] BROADCASTING m=%p\n", p->id, p->m); mtx_lock(&vis_plugin->pl_mtx); map = &vis_plugin->pl_node[p->id]; mtx_unlock(&vis_plugin->pl_mtx); /* This is O(n*n) which is not optimal for large * number of nodes. Another way of doing it is * creating groups of nodes that hear each other. * Atleast for this simple static node plugin. */ for(int i=0; imap[i]; for(int j=0; j<32; ++j){ int vis = index & 0x01; if(vis){ int k = i*ARRAY_SIZE + j; if(hal->hal_devs[k] != NULL && hal->hal_devs[k]->up == 1){ struct wtap_softc *sc = hal->hal_devs[k]; struct mbuf *m = m_dup(p->m, M_NOWAIT); DWTAP_PRINTF("[%d] duplicated old_m=%p" "to new_m=%p\n", p->id, p->m, m); #if 0 printf("[%d] sending to %d\n", p->id, k); #endif wtap_inject(sc, m); } } index = index >> 1; } } } static void add_link(struct visibility_plugin *vis_plugin, struct link *l) { mtx_lock(&vis_plugin->pl_mtx); struct vis_map *map = &vis_plugin->pl_node[l->id1]; int index = l->id2/ARRAY_SIZE; int bit = l->id2 % ARRAY_SIZE; uint32_t value = 1 << bit; map->map[index] = map->map[index] | value; mtx_unlock(&vis_plugin->pl_mtx); #if 0 printf("l->id1=%d, l->id2=%d, map->map[%d] = %u, bit=%d\n", l->id1, l->id2, index, map->map[index], bit); #endif } static void del_link(struct visibility_plugin *vis_plugin, struct link *l) { mtx_lock(&vis_plugin->pl_mtx); struct vis_map *map = &vis_plugin->pl_node[l->id1]; int index = l->id2/ARRAY_SIZE; int bit = l->id2 % ARRAY_SIZE; uint32_t value = 1 << bit; map->map[index] = map->map[index] & ~value; mtx_unlock(&vis_plugin->pl_mtx); #if 0 printf("map->map[index] = %u\n", map->map[index]); #endif } int vis_ioctl(struct cdev *sdev, u_long cmd, caddr_t data, int fflag, struct thread *td) { struct visibility_plugin *vis_plugin = (struct visibility_plugin *) sdev->si_drv1; struct wtap_hal *hal = vis_plugin->base.wp_hal; struct link l; int op; int error = 0; CURVNET_SET(CRED_TO_VNET(curthread->td_ucred)); switch(cmd) { case VISIOCTLOPEN: op = *(int *)data; if(op == 0) medium_close(hal->hal_md); else medium_open(hal->hal_md); break; case VISIOCTLLINK: l = *(struct link *)data; if(l.op == 0) del_link(vis_plugin, &l); else add_link(vis_plugin, &l); #if 0 printf("op=%d, id1=%d, id2=%d\n", l.op, l.id1, l.id2); #endif break; default: DWTAP_PRINTF("Unkown WTAP IOCTL\n"); error = EINVAL; } CURVNET_RESTORE(); return error; }