if_wtap_module.c revision 256281
1156952Sume/*-
2156952Sume * Copyright (c) 2010-2011 Monthadar Al Jaberi, TerraNet AB
3156952Sume * All rights reserved.
4156952Sume *
5156952Sume * Redistribution and use in source and binary forms, with or without
6156952Sume * modification, are permitted provided that the following conditions
7156952Sume * are met:
8156952Sume * 1. Redistributions of source code must retain the above copyright
9156952Sume *    notice, this list of conditions and the following disclaimer,
10156952Sume *    without modification.
11156952Sume * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12156952Sume *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13156952Sume *    redistribution must be conditioned upon including a substantially
14156952Sume *    similar Disclaimer requirement for further binary redistribution.
15156952Sume *
16156952Sume * NO WARRANTY
17156952Sume * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18156952Sume * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19270838Sume * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20156952Sume * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21156956Sume * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22156956Sume * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23156952Sume * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24156952Sume * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25156952Sume * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26156952Sume * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27156952Sume * THE POSSIBILITY OF SUCH DAMAGES.
28156952Sume *
29156952Sume * $FreeBSD: stable/10/sys/dev/wtap/if_wtap_module.c 231828 2012-02-16 16:48:12Z adrian $
30156952Sume */
31156952Sume#include <sys/param.h>
32156952Sume#include <sys/module.h>
33170244Sume#include <sys/kernel.h>
34156952Sume#include <sys/systm.h>
35170244Sume#include <sys/sysctl.h>
36156952Sume#include <sys/mbuf.h>
37170244Sume#include <sys/malloc.h>
38170244Sume#include <sys/lock.h>
39170244Sume#include <sys/mutex.h>
40156952Sume#include <sys/proc.h>
41170244Sume#include <sys/ucred.h>
42170244Sume#include <sys/jail.h>
43156952Sume
44170244Sume#include <sys/types.h>
45156952Sume#include <sys/sockio.h>
46156952Sume#include <sys/socket.h>
47156952Sume#include <sys/socketvar.h>
48170244Sume#include <sys/errno.h>
49156952Sume#include <sys/callout.h>
50156952Sume#include <sys/endian.h>
51156952Sume#include <sys/kthread.h>
52156952Sume#include <sys/taskqueue.h>
53156952Sume#include <sys/priv.h>
54156952Sume#include <sys/sysctl.h>
55156952Sume
56156952Sume#include <machine/bus.h>
57156952Sume
58156952Sume#include <net/if.h>
59156952Sume#include <net/if_dl.h>
60156952Sume#include <net/if_media.h>
61156952Sume#include <net/if_types.h>
62156952Sume#include <net/if_arp.h>
63156952Sume#include <net/ethernet.h>
64156952Sume#include <net/if_llc.h>
65156952Sume#include <net/vnet.h>
66156952Sume
67156952Sume#include <net80211/ieee80211_var.h>
68156952Sume#include <net80211/ieee80211_regdomain.h>
69156952Sume
70156952Sume#include <net/bpf.h>
71156952Sume
72156952Sume
73156952Sume#include <sys/errno.h>
74156952Sume#include <sys/conf.h>   /* cdevsw struct */
75156952Sume#include <sys/uio.h>    /* uio struct */
76156952Sume
77156952Sume
78156952Sume#include <netinet/in.h>
79156952Sume#include <netinet/if_ether.h>
80156952Sume
81156952Sume#include "if_wtapvar.h"
82156952Sume#include "if_wtapioctl.h"
83156952Sume#include "if_medium.h"
84156952Sume#include "wtap_hal/hal.h"
85156952Sume
86156952Sume/* WTAP PLUGINS */
87156952Sume#include "plugins/visibility.h"
88156952Sume
89156952SumeMALLOC_DEFINE(M_WTAP, "wtap", "wtap wireless simulator");
90156952SumeMALLOC_DEFINE(M_WTAP_PACKET, "wtap packet", "wtap wireless simulator packet");
91156952SumeMALLOC_DEFINE(M_WTAP_RXBUF, "wtap rxbuf",
92156952Sume    "wtap wireless simulator recieve buffer");
93156952SumeMALLOC_DEFINE(M_WTAP_PLUGIN, "wtap plugin", "wtap wireless simulator plugin");
94156952Sume
95156952Sumestatic struct wtap_hal		*hal;
96156952Sume
97156952Sume/* Function prototypes */
98156952Sumestatic d_ioctl_t	wtap_ioctl;
99156952Sume
100156952Sumestatic struct cdev *sdev;
101156952Sumestatic struct cdevsw wtap_cdevsw = {
102156952Sume	.d_version =	D_VERSION,
103156952Sume	.d_flags =	0,
104156952Sume	.d_ioctl =	wtap_ioctl,
105156952Sume	.d_name =	"wtapctl",
106156952Sume};
107156952Sume
108156952Sumeint
109156952Sumewtap_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
110156952Sume    int fflag, struct thread *td)
111156952Sume{
112156952Sume	int error = 0;
113156952Sume
114156952Sume	CURVNET_SET(CRED_TO_VNET(curthread->td_ucred));
115156952Sume
116156952Sume	switch(cmd) {
117156952Sume	case WTAPIOCTLCRT:
118156952Sume		if(new_wtap(hal, *(int *)data))
119156952Sume			error = EINVAL;
120156952Sume		break;
121156952Sume	case WTAPIOCTLDEL:
122156952Sume		if(free_wtap(hal, *(int *)data))
123156952Sume			error = EINVAL;
124156952Sume		break;
125156952Sume	default:
126156952Sume		DWTAP_PRINTF("Unkown WTAP IOCTL\n");
127156952Sume		error = EINVAL;
128156952Sume	}
129156952Sume
130156952Sume	CURVNET_RESTORE();
131156952Sume	return error;
132156952Sume}
133156952Sume
134156952Sume
135156952Sume/* The function called at load/unload. */
136156952Sumestatic int
137156952Sumeevent_handler(module_t module, int event, void *arg)
138156952Sume{
139156952Sume	struct visibility_plugin *plugin;
140156952Sume	int e = 0; /* Error, 0 for normal return status */
141156952Sume
142156952Sume	switch (event) {
143156952Sume	case MOD_LOAD:
144156952Sume		sdev = make_dev(&wtap_cdevsw,0,UID_ROOT,
145156952Sume		    GID_WHEEL,0600,(const char *)"wtapctl");
146158787Sume		hal = (struct wtap_hal *)malloc(sizeof(struct wtap_hal),
147170244Sume		    M_WTAP, M_NOWAIT | M_ZERO);
148156952Sume		bzero(hal, sizeof(struct wtap_hal));
149156952Sume
150156952Sume		init_hal(hal);
151156952Sume
152156952Sume		/* Setting up a simple plugin */
153156952Sume		plugin = (struct visibility_plugin *)malloc
154156956Sume		    (sizeof(struct visibility_plugin), M_WTAP_PLUGIN,
155156952Sume		    M_NOWAIT | M_ZERO);
156170244Sume		plugin->base.wp_hal  = hal;
157156952Sume		plugin->base.init = visibility_init;
158170244Sume		plugin->base.deinit = visibility_deinit;
159156952Sume		plugin->base.work = visibility_work;
160170244Sume		register_plugin(hal, (struct wtap_plugin *)plugin);
161156952Sume
162156952Sume                printf("Loaded wtap wireless simulator\n");
163156952Sume                break;
164156952Sume	case MOD_UNLOAD:
165156952Sume		destroy_dev(sdev);
166170244Sume		deregister_plugin(hal);
167156952Sume		deinit_hal(hal);
168156952Sume		free(hal, M_WTAP);
169156952Sume		printf("Unloading wtap wireless simulator\n");
170156952Sume		break;
171156952Sume	default:
172156952Sume		e = EOPNOTSUPP; /* Error, Operation Not Supported */
173170244Sume		break;
174156952Sume	}
175156952Sume
176156952Sume	return(e);
177156952Sume}
178170244Sume
179170244Sume/* The second argument of DECLARE_MODULE. */
180170244Sumestatic moduledata_t wtap_conf = {
181156952Sume	"wtap",		/* module name */
182156952Sume	event_handler,	/* event handler */
183156952Sume	NULL		/* extra data */
184156952Sume};
185156952Sume
186156952SumeDECLARE_MODULE(wtap, wtap_conf, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
187156952SumeMODULE_DEPEND(wtap, wlan, 1, 1, 1);	/* 802.11 media layer */
188156952Sume