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
32 #include <sys/param.h>
33 #include <sys/module.h>
34 #include <sys/malloc.h>
35 #include <sys/jail.h>
36
37 #include <sys/socket.h>
38
39 #include <net/if.h>
40 #include <net/if_media.h>
41 #include <net/ethernet.h>
42
43 #include <netinet/in.h>
44
45 #include "if_wtapvar.h"
46 #include "if_wtapioctl.h"
47 #include "if_medium.h"
48 #include "wtap_hal/hal.h"
49
50 /* WTAP PLUGINS */
51 #include "plugins/visibility.h"
52
53 MALLOC_DEFINE(M_WTAP, "wtap", "wtap wireless simulator");
54 MALLOC_DEFINE(M_WTAP_PACKET, "wtap packet", "wtap wireless simulator packet");
55 MALLOC_DEFINE(M_WTAP_RXBUF, "wtap rxbuf",
56 "wtap wireless simulator receive buffer");
57 MALLOC_DEFINE(M_WTAP_PLUGIN, "wtap plugin", "wtap wireless simulator plugin");
58
59 static struct wtap_hal *hal;
60
61 /* Function prototypes */
62 static d_ioctl_t wtap_ioctl;
63
64 static struct cdev *sdev;
65 static struct cdevsw wtap_cdevsw = {
66 .d_version = D_VERSION,
67 .d_flags = 0,
68 .d_ioctl = wtap_ioctl,
69 .d_name = "wtapctl",
70 };
71
72 int
wtap_ioctl(struct cdev * dev,u_long cmd,caddr_t data,int fflag,struct thread * td)73 wtap_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
74 int fflag, struct thread *td)
75 {
76 int error = 0;
77
78 CURVNET_SET(CRED_TO_VNET(curthread->td_ucred));
79
80 switch(cmd) {
81 case WTAPIOCTLCRT:
82 if(new_wtap(hal, *(int *)data))
83 error = EINVAL;
84 break;
85 case WTAPIOCTLDEL:
86 if(free_wtap(hal, *(int *)data))
87 error = EINVAL;
88 break;
89 default:
90 DWTAP_PRINTF("Unknown WTAP IOCTL\n");
91 error = EINVAL;
92 }
93
94 CURVNET_RESTORE();
95 return error;
96 }
97
98 /* The function called at load/unload. */
99 static int
event_handler(module_t module,int event,void * arg)100 event_handler(module_t module, int event, void *arg)
101 {
102 struct visibility_plugin *plugin;
103 int e = 0; /* Error, 0 for normal return status */
104
105 switch (event) {
106 case MOD_LOAD:
107 sdev = make_dev(&wtap_cdevsw,0,UID_ROOT,
108 GID_WHEEL,0600,(const char *)"wtapctl");
109 hal = (struct wtap_hal *)malloc(sizeof(struct wtap_hal),
110 M_WTAP, M_NOWAIT | M_ZERO);
111
112 init_hal(hal);
113
114 /* Setting up a simple plugin */
115 plugin = (struct visibility_plugin *)malloc
116 (sizeof(struct visibility_plugin), M_WTAP_PLUGIN,
117 M_NOWAIT | M_ZERO);
118 plugin->base.wp_hal = hal;
119 plugin->base.init = visibility_init;
120 plugin->base.deinit = visibility_deinit;
121 plugin->base.work = visibility_work;
122 register_plugin(hal, (struct wtap_plugin *)plugin);
123
124 printf("Loaded wtap wireless simulator\n");
125 break;
126 case MOD_UNLOAD:
127 destroy_dev(sdev);
128 deregister_plugin(hal);
129 deinit_hal(hal);
130 free(hal, M_WTAP);
131 printf("Unloading wtap wireless simulator\n");
132 break;
133 default:
134 e = EOPNOTSUPP; /* Error, Operation Not Supported */
135 break;
136 }
137
138 return(e);
139 }
140
141 /* The second argument of DECLARE_MODULE. */
142 static moduledata_t wtap_conf = {
143 "wtap", /* module name */
144 event_handler, /* event handler */
145 NULL /* extra data */
146 };
147
148 DECLARE_MODULE(wtap, wtap_conf, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
149 MODULE_DEPEND(wtap, wlan, 1, 1, 1); /* 802.11 media layer */
150