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