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 "if_wtapvar.h"
79#include "if_wtapioctl.h"
80#include "if_medium.h"
81#include "wtap_hal/hal.h"
82
83/* WTAP PLUGINS */
84#include "plugins/visibility.h"
85
86MALLOC_DEFINE(M_WTAP, "wtap", "wtap wireless simulator");
87MALLOC_DEFINE(M_WTAP_PACKET, "wtap packet", "wtap wireless simulator packet");
88MALLOC_DEFINE(M_WTAP_RXBUF, "wtap rxbuf",
89    "wtap wireless simulator receive buffer");
90MALLOC_DEFINE(M_WTAP_PLUGIN, "wtap plugin", "wtap wireless simulator plugin");
91
92static struct wtap_hal		*hal;
93
94/* Function prototypes */
95static d_ioctl_t	wtap_ioctl;
96
97static struct cdev *sdev;
98static struct cdevsw wtap_cdevsw = {
99	.d_version =	D_VERSION,
100	.d_flags =	0,
101	.d_ioctl =	wtap_ioctl,
102	.d_name =	"wtapctl",
103};
104
105int
106wtap_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
107    int fflag, struct thread *td)
108{
109	int error = 0;
110
111	CURVNET_SET(CRED_TO_VNET(curthread->td_ucred));
112
113	switch(cmd) {
114	case WTAPIOCTLCRT:
115		if(new_wtap(hal, *(int *)data))
116			error = EINVAL;
117		break;
118	case WTAPIOCTLDEL:
119		if(free_wtap(hal, *(int *)data))
120			error = EINVAL;
121		break;
122	default:
123		DWTAP_PRINTF("Unknown WTAP IOCTL\n");
124		error = EINVAL;
125	}
126
127	CURVNET_RESTORE();
128	return error;
129}
130
131/* The function called at load/unload. */
132static int
133event_handler(module_t module, int event, void *arg)
134{
135	struct visibility_plugin *plugin;
136	int e = 0; /* Error, 0 for normal return status */
137
138	switch (event) {
139	case MOD_LOAD:
140		sdev = make_dev(&wtap_cdevsw,0,UID_ROOT,
141		    GID_WHEEL,0600,(const char *)"wtapctl");
142		hal = (struct wtap_hal *)malloc(sizeof(struct wtap_hal),
143		    M_WTAP, M_NOWAIT | M_ZERO);
144
145		init_hal(hal);
146
147		/* Setting up a simple plugin */
148		plugin = (struct visibility_plugin *)malloc
149		    (sizeof(struct visibility_plugin), M_WTAP_PLUGIN,
150		    M_NOWAIT | M_ZERO);
151		plugin->base.wp_hal  = hal;
152		plugin->base.init = visibility_init;
153		plugin->base.deinit = visibility_deinit;
154		plugin->base.work = visibility_work;
155		register_plugin(hal, (struct wtap_plugin *)plugin);
156
157                printf("Loaded wtap wireless simulator\n");
158                break;
159	case MOD_UNLOAD:
160		destroy_dev(sdev);
161		deregister_plugin(hal);
162		deinit_hal(hal);
163		free(hal, M_WTAP);
164		printf("Unloading wtap wireless simulator\n");
165		break;
166	default:
167		e = EOPNOTSUPP; /* Error, Operation Not Supported */
168		break;
169	}
170
171	return(e);
172}
173
174/* The second argument of DECLARE_MODULE. */
175static moduledata_t wtap_conf = {
176	"wtap",		/* module name */
177	event_handler,	/* event handler */
178	NULL		/* extra data */
179};
180
181DECLARE_MODULE(wtap, wtap_conf, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
182MODULE_DEPEND(wtap, wlan, 1, 1, 1);	/* 802.11 media layer */
183