xref: /freebsd/sys/dev/wtap/wtap_hal/hal.c (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
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 "hal.h"
32 #include "../if_medium.h"
33 #include "handler.h"
34 
35 static void
36 hal_tx_proc(void *arg, int npending)
37 {
38 	struct wtap_hal *hal = (struct wtap_hal *)arg;
39 	struct packet *p;
40 
41 #if 0
42 	DWTAP_PRINTF("%s\n", __func__);
43 #endif
44 
45 	hal = (struct wtap_hal *)arg;
46 	for(;;){
47 		p = medium_get_next_packet(hal->hal_md);
48 		if(p == NULL)
49 		return;
50 
51 		hal->plugin->work(hal->plugin, p);
52 
53 #if 0
54 		DWTAP_PRINTF("[%d] freeing m=%p\n", p->id, p->m);
55 #endif
56 		m_free(p->m);
57 		free(p, M_WTAP_PACKET);
58 	}
59 }
60 
61 void
62 init_hal(struct wtap_hal *hal)
63 {
64 
65 	DWTAP_PRINTF("%s\n", __func__);
66 	mtx_init(&hal->hal_mtx, "wtap_hal mtx", NULL, MTX_DEF | MTX_RECURSE);
67 
68 	hal->hal_md = (struct wtap_medium *)malloc(sizeof(struct wtap_medium),
69 	    M_WTAP, M_NOWAIT | M_ZERO);
70 	bzero(hal->hal_md, sizeof(struct wtap_medium));
71 
72 	init_medium(hal->hal_md);
73 	/* register event handler for packets */
74 	TASK_INIT(&hal->hal_md->tx_handler->proc, 0, hal_tx_proc, hal);
75 }
76 
77 void
78 register_plugin(struct wtap_hal *hal, struct wtap_plugin *plugin)
79 {
80 
81 	plugin->init(plugin);
82 	hal->plugin = plugin;
83 }
84 
85 void
86 deregister_plugin(struct wtap_hal *hal)
87 {
88 
89 	hal->plugin->deinit(hal->plugin);
90 	hal->plugin = NULL; /* catch illegal usages */
91 }
92 
93 void
94 deinit_hal(struct wtap_hal *hal)
95 {
96 
97 	DWTAP_PRINTF("%s\n", __func__);
98 	deinit_medium(hal->hal_md);
99 	free(hal->hal_md, M_WTAP);
100 	mtx_destroy(&hal->hal_mtx);
101 }
102 
103 int32_t
104 new_wtap(struct wtap_hal *hal, int32_t id)
105 {
106 	static const uint8_t mac_pool[64][IEEE80211_ADDR_LEN] = {
107 	    {0,152,154,152,150,151},
108 	    {0,152,154,152,150,152},
109 	    {0,152,154,152,150,153},
110 	    {0,152,154,152,150,154},
111 	    {0,152,154,152,150,155},
112 	    {0,152,154,152,150,156},
113 	    {0,152,154,152,150,157},
114 	    {0,152,154,152,150,158},
115 	    {0,152,154,152,151,151},
116 	    {0,152,154,152,151,152},
117 	    {0,152,154,152,151,153},
118 	    {0,152,154,152,151,154},
119 	    {0,152,154,152,151,155},
120 	    {0,152,154,152,151,156},
121 	    {0,152,154,152,151,157},
122 	    {0,152,154,152,151,158},
123 	    {0,152,154,152,152,151},
124 	    {0,152,154,152,152,152},
125 	    {0,152,154,152,152,153},
126 	    {0,152,154,152,152,154},
127 	    {0,152,154,152,152,155},
128 	    {0,152,154,152,152,156},
129 	    {0,152,154,152,152,157},
130 	    {0,152,154,152,152,158},
131 	    {0,152,154,152,153,151},
132 	    {0,152,154,152,153,152},
133 	    {0,152,154,152,153,153},
134 	    {0,152,154,152,153,154},
135 	    {0,152,154,152,153,155},
136 	    {0,152,154,152,153,156},
137 	    {0,152,154,152,153,157},
138 	    {0,152,154,152,153,158},
139 	    {0,152,154,152,154,151},
140 	    {0,152,154,152,154,152},
141 	    {0,152,154,152,154,153},
142 	    {0,152,154,152,154,154},
143 	    {0,152,154,152,154,155},
144 	    {0,152,154,152,154,156},
145 	    {0,152,154,152,154,157},
146 	    {0,152,154,152,154,158},
147 	    {0,152,154,152,155,151},
148 	    {0,152,154,152,155,152},
149 	    {0,152,154,152,155,153},
150 	    {0,152,154,152,155,154},
151 	    {0,152,154,152,155,155},
152 	    {0,152,154,152,155,156},
153 	    {0,152,154,152,155,157},
154 	    {0,152,154,152,155,158},
155 	    {0,152,154,152,156,151},
156 	    {0,152,154,152,156,152},
157 	    {0,152,154,152,156,153},
158 	    {0,152,154,152,156,154},
159 	    {0,152,154,152,156,155},
160 	    {0,152,154,152,156,156},
161 	    {0,152,154,152,156,157},
162 	    {0,152,154,152,156,158},
163 	    {0,152,154,152,157,151},
164 	    {0,152,154,152,157,152},
165 	    {0,152,154,152,157,153},
166 	    {0,152,154,152,157,154},
167 	    {0,152,154,152,157,155},
168 	    {0,152,154,152,157,156},
169 	    {0,152,154,152,157,157},
170 	    {0,152,154,152,157,158}
171 	    };
172 
173 	DWTAP_PRINTF("%s\n", __func__);
174 	uint8_t const *macaddr = mac_pool[id];
175 	if(hal->hal_devs[id] != NULL){
176 		printf("error, wtap_id=%d already created\n", id);
177 		return -1;
178 	}
179 
180 	hal->hal_devs[id] = (struct wtap_softc *)malloc(
181 	    sizeof(struct wtap_softc), M_WTAP, M_NOWAIT | M_ZERO);
182 	bzero(hal->hal_devs[id], sizeof(struct wtap_softc));
183 	hal->hal_devs[id]->sc_md = hal->hal_md;
184 	hal->hal_devs[id]->id = id;
185 	snprintf(hal->hal_devs[id]->name, sizeof(hal->hal_devs[id]->name),
186 	    "wlan%d", id);
187 	mtx_init(&hal->hal_devs[id]->sc_mtx, "wtap_softc mtx", NULL,
188 	    MTX_DEF | MTX_RECURSE);
189 
190 	if(wtap_attach(hal->hal_devs[id], macaddr)){
191 		printf("%s, cant alloc new wtap\n", __func__);
192 		return -1;
193 	}
194 
195 	return 0;
196 }
197 
198 int32_t
199 free_wtap(struct wtap_hal *hal, int32_t id)
200 {
201 
202 	DWTAP_PRINTF("%s\n", __func__);
203 	if(hal->hal_devs[id] == NULL){
204 		printf("error, wtap_id=%d never created\n", id);
205 		return -1;
206 	}
207 
208 	if(wtap_detach(hal->hal_devs[id]))
209 		printf("%s, cant alloc new wtap\n", __func__);
210 	mtx_destroy(&hal->hal_devs[id]->sc_mtx);
211 	free(hal->hal_devs[id], M_WTAP);
212 	hal->hal_devs[id] = NULL;
213 	return 0;
214 }
215 
216