xref: /freebsd/sys/dev/wtap/plugins/visibility.c (revision d9f0ce31900a48d1a2bfc1c8c86f79d1e831451a)
1 /*-
2  * Copyright (c) 2010-2011 Monthadar Al Jaberi, TerraNet AB
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  *
29  * $FreeBSD$
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 
72 #include <sys/errno.h>
73 #include <sys/conf.h>   /* cdevsw struct */
74 #include <sys/uio.h>    /* uio struct */
75 
76 #include <netinet/in.h>
77 #include <netinet/if_ether.h>
78 
79 #include "visibility.h"
80 
81 /* Function prototypes */
82 static d_ioctl_t	vis_ioctl;
83 
84 static struct cdevsw vis_cdevsw = {
85 	.d_version =	D_VERSION,
86 	.d_flags =	0,
87 	.d_ioctl =	vis_ioctl,
88 	.d_name =	"visctl",
89 };
90 
91 void
92 visibility_init(struct wtap_plugin *plugin)
93 {
94 	struct visibility_plugin *vis_plugin;
95 
96 	vis_plugin = (struct visibility_plugin *) plugin;
97 	plugin->wp_sdev = make_dev(&vis_cdevsw,0,UID_ROOT,GID_WHEEL,0600,
98 	    (const char *)"visctl");
99 	plugin->wp_sdev->si_drv1 = vis_plugin;
100 	mtx_init(&vis_plugin->pl_mtx, "visibility_plugin mtx",
101 	    NULL, MTX_DEF | MTX_RECURSE);
102 	printf("Using visibility wtap plugin...\n");
103 }
104 
105 void
106 visibility_deinit(struct wtap_plugin *plugin)
107 {
108 	struct visibility_plugin *vis_plugin;
109 
110 	vis_plugin = (struct visibility_plugin *) plugin;
111 	destroy_dev(plugin->wp_sdev);
112 	mtx_destroy(&vis_plugin->pl_mtx);
113 	free(vis_plugin, M_WTAP_PLUGIN);
114 	printf("Removing visibility wtap plugin...\n");
115 }
116 
117 /* We need to use a mutex lock when we read out a visibility map
118  * and when we change visibility map from user space through IOCTL
119  */
120 void
121 visibility_work(struct wtap_plugin *plugin, struct packet *p)
122 {
123 	struct visibility_plugin *vis_plugin =
124 	    (struct visibility_plugin *) plugin;
125 	struct wtap_hal *hal = (struct wtap_hal *)vis_plugin->base.wp_hal;
126 	struct vis_map *map;
127 
128 	KASSERT(mtod(p->m, const char *) != (const char *) 0xdeadc0de ||
129 	    mtod(p->m, const char *) != NULL,
130 	    ("[%s] got a corrupt packet from master queue, p->m=%p, p->id=%d\n",
131 	    __func__, p->m, p->id));
132 	DWTAP_PRINTF("[%d] BROADCASTING m=%p\n", p->id, p->m);
133 	mtx_lock(&vis_plugin->pl_mtx);
134 	map = &vis_plugin->pl_node[p->id];
135 	mtx_unlock(&vis_plugin->pl_mtx);
136 
137 	/* This is O(n*n) which is not optimal for large
138 	 * number of nodes. Another way of doing it is
139 	 * creating groups of nodes that hear each other.
140 	 * Atleast for this simple static node plugin.
141 	 */
142 	for(int i=0; i<ARRAY_SIZE; ++i){
143 		uint32_t index = map->map[i];
144 		for(int j=0; j<32; ++j){
145 			int vis = index & 0x01;
146 			if(vis){
147 				int k = i*ARRAY_SIZE + j;
148 				if(hal->hal_devs[k] != NULL
149 				    && hal->hal_devs[k]->up == 1){
150 					struct wtap_softc *sc =
151 					    hal->hal_devs[k];
152 					struct mbuf *m =
153 					    m_dup(p->m, M_NOWAIT);
154 					DWTAP_PRINTF("[%d] duplicated old_m=%p"
155 					    "to new_m=%p\n", p->id, p->m, m);
156 #if 0
157 					printf("[%d] sending to %d\n",
158 					    p->id, k);
159 #endif
160 					wtap_inject(sc, m);
161 				}
162 			}
163 			index = index >> 1;
164 		}
165 	}
166 }
167 
168 static void
169 add_link(struct visibility_plugin *vis_plugin, struct link *l)
170 {
171 
172 	mtx_lock(&vis_plugin->pl_mtx);
173 	struct vis_map *map = &vis_plugin->pl_node[l->id1];
174 	int index = l->id2/ARRAY_SIZE;
175 	int bit = l->id2 % ARRAY_SIZE;
176 	uint32_t value = 1 << bit;
177 	map->map[index] = map->map[index] | value;
178 	mtx_unlock(&vis_plugin->pl_mtx);
179 #if 0
180 	printf("l->id1=%d, l->id2=%d, map->map[%d] = %u, bit=%d\n",
181 	    l->id1, l->id2, index, map->map[index], bit);
182 #endif
183 }
184 
185 static void
186 del_link(struct visibility_plugin *vis_plugin, struct link *l)
187 {
188 
189 	mtx_lock(&vis_plugin->pl_mtx);
190 	struct vis_map *map = &vis_plugin->pl_node[l->id1];
191 	int index = l->id2/ARRAY_SIZE;
192 	int bit = l->id2 % ARRAY_SIZE;
193 	uint32_t value = 1 << bit;
194 	map->map[index] = map->map[index] & ~value;
195 	mtx_unlock(&vis_plugin->pl_mtx);
196 #if 0
197 	printf("map->map[index] = %u\n", map->map[index]);
198 #endif
199 }
200 
201 
202 int
203 vis_ioctl(struct cdev *sdev, u_long cmd, caddr_t data,
204     int fflag, struct thread *td)
205 {
206 	struct visibility_plugin *vis_plugin =
207 	    (struct visibility_plugin *) sdev->si_drv1;
208 	struct wtap_hal *hal = vis_plugin->base.wp_hal;
209 	struct link l;
210 	int op;
211 	int error = 0;
212 
213 	CURVNET_SET(CRED_TO_VNET(curthread->td_ucred));
214 	switch(cmd) {
215 	case VISIOCTLOPEN:
216 		op =  *(int *)data;
217 		if(op == 0)
218 			medium_close(hal->hal_md);
219 		else
220 			medium_open(hal->hal_md);
221 		break;
222 	case VISIOCTLLINK:
223 		l = *(struct link *)data;
224 		if(l.op == 0)
225 			del_link(vis_plugin, &l);
226 		else
227 			add_link(vis_plugin, &l);
228 #if 0
229 		printf("op=%d, id1=%d, id2=%d\n", l.op, l.id1, l.id2);
230 #endif
231 		break;
232 	default:
233 		DWTAP_PRINTF("Unkown WTAP IOCTL\n");
234 		error = EINVAL;
235 	}
236 
237 	CURVNET_RESTORE();
238 	return error;
239 }
240 
241