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