1 | #include <machine/rtems-bsd-kernel-space.h> |
---|
2 | |
---|
3 | /*- |
---|
4 | * Copyright (c) 2003-2009 Sam Leffler, Errno Consulting |
---|
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 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
13 | * notice, this list of conditions and the following disclaimer in the |
---|
14 | * documentation and/or other materials provided with the distribution. |
---|
15 | * |
---|
16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
---|
17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
---|
18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
---|
19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
---|
20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
---|
21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
---|
25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
26 | */ |
---|
27 | |
---|
28 | #include <sys/cdefs.h> |
---|
29 | __FBSDID("$FreeBSD$"); |
---|
30 | |
---|
31 | /* |
---|
32 | * IEEE 802.11 support (FreeBSD-specific code) |
---|
33 | */ |
---|
34 | #include <rtems/bsd/local/opt_wlan.h> |
---|
35 | |
---|
36 | #include <rtems/bsd/sys/param.h> |
---|
37 | #include <sys/kernel.h> |
---|
38 | #include <sys/systm.h> |
---|
39 | #include <sys/linker.h> |
---|
40 | #include <sys/mbuf.h> |
---|
41 | #include <sys/module.h> |
---|
42 | #include <sys/proc.h> |
---|
43 | #include <sys/sysctl.h> |
---|
44 | |
---|
45 | #include <sys/socket.h> |
---|
46 | |
---|
47 | #include <net/bpf.h> |
---|
48 | #include <net/if.h> |
---|
49 | #include <net/if_dl.h> |
---|
50 | #include <net/if_clone.h> |
---|
51 | #include <net/if_media.h> |
---|
52 | #include <net/if_types.h> |
---|
53 | #include <net/ethernet.h> |
---|
54 | #include <net/route.h> |
---|
55 | #include <net/vnet.h> |
---|
56 | |
---|
57 | #include <net80211/ieee80211_var.h> |
---|
58 | #include <net80211/ieee80211_input.h> |
---|
59 | |
---|
60 | SYSCTL_NODE(_net, OID_AUTO, wlan, CTLFLAG_RD, 0, "IEEE 80211 parameters"); |
---|
61 | |
---|
62 | #ifdef IEEE80211_DEBUG |
---|
63 | int ieee80211_debug = 0; |
---|
64 | SYSCTL_INT(_net_wlan, OID_AUTO, debug, CTLFLAG_RW, &ieee80211_debug, |
---|
65 | 0, "debugging printfs"); |
---|
66 | #endif |
---|
67 | |
---|
68 | static MALLOC_DEFINE(M_80211_COM, "80211com", "802.11 com state"); |
---|
69 | |
---|
70 | /* |
---|
71 | * Allocate/free com structure in conjunction with ifnet; |
---|
72 | * these routines are registered with if_register_com_alloc |
---|
73 | * below and are called automatically by the ifnet code |
---|
74 | * when the ifnet of the parent device is created. |
---|
75 | */ |
---|
76 | static void * |
---|
77 | wlan_alloc(u_char type, struct ifnet *ifp) |
---|
78 | { |
---|
79 | struct ieee80211com *ic; |
---|
80 | |
---|
81 | ic = malloc(sizeof(struct ieee80211com), M_80211_COM, M_WAITOK|M_ZERO); |
---|
82 | ic->ic_ifp = ifp; |
---|
83 | |
---|
84 | return (ic); |
---|
85 | } |
---|
86 | |
---|
87 | static void |
---|
88 | wlan_free(void *ic, u_char type) |
---|
89 | { |
---|
90 | free(ic, M_80211_COM); |
---|
91 | } |
---|
92 | |
---|
93 | static int |
---|
94 | wlan_clone_create(struct if_clone *ifc, int unit, caddr_t params) |
---|
95 | { |
---|
96 | struct ieee80211_clone_params cp; |
---|
97 | struct ieee80211vap *vap; |
---|
98 | struct ieee80211com *ic; |
---|
99 | struct ifnet *ifp; |
---|
100 | int error; |
---|
101 | |
---|
102 | error = copyin(params, &cp, sizeof(cp)); |
---|
103 | if (error) |
---|
104 | return error; |
---|
105 | ifp = ifunit(cp.icp_parent); |
---|
106 | if (ifp == NULL) |
---|
107 | return ENXIO; |
---|
108 | /* XXX move printfs to DIAGNOSTIC before release */ |
---|
109 | if (ifp->if_type != IFT_IEEE80211) { |
---|
110 | if_printf(ifp, "%s: reject, not an 802.11 device\n", __func__); |
---|
111 | return ENXIO; |
---|
112 | } |
---|
113 | if (cp.icp_opmode >= IEEE80211_OPMODE_MAX) { |
---|
114 | if_printf(ifp, "%s: invalid opmode %d\n", |
---|
115 | __func__, cp.icp_opmode); |
---|
116 | return EINVAL; |
---|
117 | } |
---|
118 | ic = ifp->if_l2com; |
---|
119 | if ((ic->ic_caps & ieee80211_opcap[cp.icp_opmode]) == 0) { |
---|
120 | if_printf(ifp, "%s mode not supported\n", |
---|
121 | ieee80211_opmode_name[cp.icp_opmode]); |
---|
122 | return EOPNOTSUPP; |
---|
123 | } |
---|
124 | if ((cp.icp_flags & IEEE80211_CLONE_TDMA) && |
---|
125 | #ifdef IEEE80211_SUPPORT_TDMA |
---|
126 | (ic->ic_caps & IEEE80211_C_TDMA) == 0 |
---|
127 | #else |
---|
128 | (1) |
---|
129 | #endif |
---|
130 | ) { |
---|
131 | if_printf(ifp, "TDMA not supported\n"); |
---|
132 | return EOPNOTSUPP; |
---|
133 | } |
---|
134 | vap = ic->ic_vap_create(ic, ifc->ifc_name, unit, |
---|
135 | cp.icp_opmode, cp.icp_flags, cp.icp_bssid, |
---|
136 | cp.icp_flags & IEEE80211_CLONE_MACADDR ? |
---|
137 | cp.icp_macaddr : (const uint8_t *)IF_LLADDR(ifp)); |
---|
138 | return (vap == NULL ? EIO : 0); |
---|
139 | } |
---|
140 | |
---|
141 | static void |
---|
142 | wlan_clone_destroy(struct ifnet *ifp) |
---|
143 | { |
---|
144 | struct ieee80211vap *vap = ifp->if_softc; |
---|
145 | struct ieee80211com *ic = vap->iv_ic; |
---|
146 | |
---|
147 | ic->ic_vap_delete(vap); |
---|
148 | } |
---|
149 | IFC_SIMPLE_DECLARE(wlan, 0); |
---|
150 | |
---|
151 | void |
---|
152 | ieee80211_vap_destroy(struct ieee80211vap *vap) |
---|
153 | { |
---|
154 | if_clone_destroyif(&wlan_cloner, vap->iv_ifp); |
---|
155 | } |
---|
156 | |
---|
157 | int |
---|
158 | ieee80211_sysctl_msecs_ticks(SYSCTL_HANDLER_ARGS) |
---|
159 | { |
---|
160 | int msecs = ticks_to_msecs(*(int *)arg1); |
---|
161 | int error, t; |
---|
162 | |
---|
163 | error = sysctl_handle_int(oidp, &msecs, 0, req); |
---|
164 | if (error || !req->newptr) |
---|
165 | return error; |
---|
166 | t = msecs_to_ticks(msecs); |
---|
167 | *(int *)arg1 = (t < 1) ? 1 : t; |
---|
168 | return 0; |
---|
169 | } |
---|
170 | |
---|
171 | static int |
---|
172 | ieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS) |
---|
173 | { |
---|
174 | int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT; |
---|
175 | int error; |
---|
176 | |
---|
177 | error = sysctl_handle_int(oidp, &inact, 0, req); |
---|
178 | if (error || !req->newptr) |
---|
179 | return error; |
---|
180 | *(int *)arg1 = inact / IEEE80211_INACT_WAIT; |
---|
181 | return 0; |
---|
182 | } |
---|
183 | |
---|
184 | static int |
---|
185 | ieee80211_sysctl_parent(SYSCTL_HANDLER_ARGS) |
---|
186 | { |
---|
187 | struct ieee80211com *ic = arg1; |
---|
188 | const char *name = ic->ic_ifp->if_xname; |
---|
189 | |
---|
190 | return SYSCTL_OUT(req, name, strlen(name)); |
---|
191 | } |
---|
192 | |
---|
193 | static int |
---|
194 | ieee80211_sysctl_radar(SYSCTL_HANDLER_ARGS) |
---|
195 | { |
---|
196 | struct ieee80211com *ic = arg1; |
---|
197 | int t = 0, error; |
---|
198 | |
---|
199 | error = sysctl_handle_int(oidp, &t, 0, req); |
---|
200 | if (error || !req->newptr) |
---|
201 | return error; |
---|
202 | IEEE80211_LOCK(ic); |
---|
203 | ieee80211_dfs_notify_radar(ic, ic->ic_curchan); |
---|
204 | IEEE80211_UNLOCK(ic); |
---|
205 | return 0; |
---|
206 | } |
---|
207 | |
---|
208 | void |
---|
209 | ieee80211_sysctl_attach(struct ieee80211com *ic) |
---|
210 | { |
---|
211 | } |
---|
212 | |
---|
213 | void |
---|
214 | ieee80211_sysctl_detach(struct ieee80211com *ic) |
---|
215 | { |
---|
216 | } |
---|
217 | |
---|
218 | void |
---|
219 | ieee80211_sysctl_vattach(struct ieee80211vap *vap) |
---|
220 | { |
---|
221 | struct ifnet *ifp = vap->iv_ifp; |
---|
222 | struct sysctl_ctx_list *ctx; |
---|
223 | struct sysctl_oid *oid; |
---|
224 | char num[14]; /* sufficient for 32 bits */ |
---|
225 | |
---|
226 | ctx = (struct sysctl_ctx_list *) malloc(sizeof(struct sysctl_ctx_list), |
---|
227 | M_DEVBUF, M_NOWAIT | M_ZERO); |
---|
228 | if (ctx == NULL) { |
---|
229 | if_printf(ifp, "%s: cannot allocate sysctl context!\n", |
---|
230 | __func__); |
---|
231 | return; |
---|
232 | } |
---|
233 | sysctl_ctx_init(ctx); |
---|
234 | snprintf(num, sizeof(num), "%u", ifp->if_dunit); |
---|
235 | oid = SYSCTL_ADD_NODE(ctx, &SYSCTL_NODE_CHILDREN(_net, wlan), |
---|
236 | OID_AUTO, num, CTLFLAG_RD, NULL, ""); |
---|
237 | SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, |
---|
238 | "%parent", CTLTYPE_STRING | CTLFLAG_RD, vap->iv_ic, 0, |
---|
239 | ieee80211_sysctl_parent, "A", "parent device"); |
---|
240 | SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, |
---|
241 | "driver_caps", CTLFLAG_RW, &vap->iv_caps, 0, |
---|
242 | "driver capabilities"); |
---|
243 | #ifdef IEEE80211_DEBUG |
---|
244 | vap->iv_debug = ieee80211_debug; |
---|
245 | SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, |
---|
246 | "debug", CTLFLAG_RW, &vap->iv_debug, 0, |
---|
247 | "control debugging printfs"); |
---|
248 | #endif |
---|
249 | SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, |
---|
250 | "bmiss_max", CTLFLAG_RW, &vap->iv_bmiss_max, 0, |
---|
251 | "consecutive beacon misses before scanning"); |
---|
252 | /* XXX inherit from tunables */ |
---|
253 | SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, |
---|
254 | "inact_run", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_run, 0, |
---|
255 | ieee80211_sysctl_inact, "I", |
---|
256 | "station inactivity timeout (sec)"); |
---|
257 | SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, |
---|
258 | "inact_probe", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_probe, 0, |
---|
259 | ieee80211_sysctl_inact, "I", |
---|
260 | "station inactivity probe timeout (sec)"); |
---|
261 | SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, |
---|
262 | "inact_auth", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_auth, 0, |
---|
263 | ieee80211_sysctl_inact, "I", |
---|
264 | "station authentication timeout (sec)"); |
---|
265 | SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, |
---|
266 | "inact_init", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_init, 0, |
---|
267 | ieee80211_sysctl_inact, "I", |
---|
268 | "station initial state timeout (sec)"); |
---|
269 | if (vap->iv_htcaps & IEEE80211_HTC_HT) { |
---|
270 | SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, |
---|
271 | "ampdu_mintraffic_bk", CTLFLAG_RW, |
---|
272 | &vap->iv_ampdu_mintraffic[WME_AC_BK], 0, |
---|
273 | "BK traffic tx aggr threshold (pps)"); |
---|
274 | SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, |
---|
275 | "ampdu_mintraffic_be", CTLFLAG_RW, |
---|
276 | &vap->iv_ampdu_mintraffic[WME_AC_BE], 0, |
---|
277 | "BE traffic tx aggr threshold (pps)"); |
---|
278 | SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, |
---|
279 | "ampdu_mintraffic_vo", CTLFLAG_RW, |
---|
280 | &vap->iv_ampdu_mintraffic[WME_AC_VO], 0, |
---|
281 | "VO traffic tx aggr threshold (pps)"); |
---|
282 | SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, |
---|
283 | "ampdu_mintraffic_vi", CTLFLAG_RW, |
---|
284 | &vap->iv_ampdu_mintraffic[WME_AC_VI], 0, |
---|
285 | "VI traffic tx aggr threshold (pps)"); |
---|
286 | } |
---|
287 | if (vap->iv_caps & IEEE80211_C_DFS) { |
---|
288 | SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, |
---|
289 | "radar", CTLTYPE_INT | CTLFLAG_RW, vap->iv_ic, 0, |
---|
290 | ieee80211_sysctl_radar, "I", "simulate radar event"); |
---|
291 | } |
---|
292 | vap->iv_sysctl = ctx; |
---|
293 | vap->iv_oid = oid; |
---|
294 | } |
---|
295 | |
---|
296 | void |
---|
297 | ieee80211_sysctl_vdetach(struct ieee80211vap *vap) |
---|
298 | { |
---|
299 | |
---|
300 | if (vap->iv_sysctl != NULL) { |
---|
301 | sysctl_ctx_free(vap->iv_sysctl); |
---|
302 | free(vap->iv_sysctl, M_DEVBUF); |
---|
303 | vap->iv_sysctl = NULL; |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | int |
---|
308 | ieee80211_node_dectestref(struct ieee80211_node *ni) |
---|
309 | { |
---|
310 | /* XXX need equivalent of atomic_dec_and_test */ |
---|
311 | atomic_subtract_int(&ni->ni_refcnt, 1); |
---|
312 | return atomic_cmpset_int(&ni->ni_refcnt, 0, 1); |
---|
313 | } |
---|
314 | |
---|
315 | void |
---|
316 | ieee80211_drain_ifq(struct ifqueue *ifq) |
---|
317 | { |
---|
318 | struct ieee80211_node *ni; |
---|
319 | struct mbuf *m; |
---|
320 | |
---|
321 | for (;;) { |
---|
322 | IF_DEQUEUE(ifq, m); |
---|
323 | if (m == NULL) |
---|
324 | break; |
---|
325 | |
---|
326 | ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; |
---|
327 | KASSERT(ni != NULL, ("frame w/o node")); |
---|
328 | ieee80211_free_node(ni); |
---|
329 | m->m_pkthdr.rcvif = NULL; |
---|
330 | |
---|
331 | m_freem(m); |
---|
332 | } |
---|
333 | } |
---|
334 | |
---|
335 | void |
---|
336 | ieee80211_flush_ifq(struct ifqueue *ifq, struct ieee80211vap *vap) |
---|
337 | { |
---|
338 | struct ieee80211_node *ni; |
---|
339 | struct mbuf *m, **mprev; |
---|
340 | |
---|
341 | IF_LOCK(ifq); |
---|
342 | mprev = &ifq->ifq_head; |
---|
343 | while ((m = *mprev) != NULL) { |
---|
344 | ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; |
---|
345 | if (ni != NULL && ni->ni_vap == vap) { |
---|
346 | *mprev = m->m_nextpkt; /* remove from list */ |
---|
347 | ifq->ifq_len--; |
---|
348 | |
---|
349 | m_freem(m); |
---|
350 | ieee80211_free_node(ni); /* reclaim ref */ |
---|
351 | } else |
---|
352 | mprev = &m->m_nextpkt; |
---|
353 | } |
---|
354 | /* recalculate tail ptr */ |
---|
355 | m = ifq->ifq_head; |
---|
356 | for (; m != NULL && m->m_nextpkt != NULL; m = m->m_nextpkt) |
---|
357 | ; |
---|
358 | ifq->ifq_tail = m; |
---|
359 | IF_UNLOCK(ifq); |
---|
360 | } |
---|
361 | |
---|
362 | /* |
---|
363 | * As above, for mbufs allocated with m_gethdr/MGETHDR |
---|
364 | * or initialized by M_COPY_PKTHDR. |
---|
365 | */ |
---|
366 | #define MC_ALIGN(m, len) \ |
---|
367 | do { \ |
---|
368 | (m)->m_data += (MCLBYTES - (len)) &~ (sizeof(long) - 1); \ |
---|
369 | } while (/* CONSTCOND */ 0) |
---|
370 | |
---|
371 | /* |
---|
372 | * Allocate and setup a management frame of the specified |
---|
373 | * size. We return the mbuf and a pointer to the start |
---|
374 | * of the contiguous data area that's been reserved based |
---|
375 | * on the packet length. The data area is forced to 32-bit |
---|
376 | * alignment and the buffer length to a multiple of 4 bytes. |
---|
377 | * This is done mainly so beacon frames (that require this) |
---|
378 | * can use this interface too. |
---|
379 | */ |
---|
380 | struct mbuf * |
---|
381 | ieee80211_getmgtframe(uint8_t **frm, int headroom, int pktlen) |
---|
382 | { |
---|
383 | struct mbuf *m; |
---|
384 | u_int len; |
---|
385 | |
---|
386 | /* |
---|
387 | * NB: we know the mbuf routines will align the data area |
---|
388 | * so we don't need to do anything special. |
---|
389 | */ |
---|
390 | len = roundup2(headroom + pktlen, 4); |
---|
391 | KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len)); |
---|
392 | if (len < MINCLSIZE) { |
---|
393 | m = m_gethdr(M_NOWAIT, MT_DATA); |
---|
394 | /* |
---|
395 | * Align the data in case additional headers are added. |
---|
396 | * This should only happen when a WEP header is added |
---|
397 | * which only happens for shared key authentication mgt |
---|
398 | * frames which all fit in MHLEN. |
---|
399 | */ |
---|
400 | if (m != NULL) |
---|
401 | MH_ALIGN(m, len); |
---|
402 | } else { |
---|
403 | m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); |
---|
404 | if (m != NULL) |
---|
405 | MC_ALIGN(m, len); |
---|
406 | } |
---|
407 | if (m != NULL) { |
---|
408 | m->m_data += headroom; |
---|
409 | *frm = m->m_data; |
---|
410 | } |
---|
411 | return m; |
---|
412 | } |
---|
413 | |
---|
414 | /* |
---|
415 | * Re-align the payload in the mbuf. This is mainly used (right now) |
---|
416 | * to handle IP header alignment requirements on certain architectures. |
---|
417 | */ |
---|
418 | struct mbuf * |
---|
419 | ieee80211_realign(struct ieee80211vap *vap, struct mbuf *m, size_t align) |
---|
420 | { |
---|
421 | int pktlen, space; |
---|
422 | struct mbuf *n; |
---|
423 | |
---|
424 | pktlen = m->m_pkthdr.len; |
---|
425 | space = pktlen + align; |
---|
426 | if (space < MINCLSIZE) |
---|
427 | n = m_gethdr(M_DONTWAIT, MT_DATA); |
---|
428 | else { |
---|
429 | n = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, |
---|
430 | space <= MCLBYTES ? MCLBYTES : |
---|
431 | #if MJUMPAGESIZE != MCLBYTES |
---|
432 | space <= MJUMPAGESIZE ? MJUMPAGESIZE : |
---|
433 | #endif |
---|
434 | space <= MJUM9BYTES ? MJUM9BYTES : MJUM16BYTES); |
---|
435 | } |
---|
436 | if (__predict_true(n != NULL)) { |
---|
437 | m_move_pkthdr(n, m); |
---|
438 | n->m_data = (caddr_t)(ALIGN(n->m_data + align) - align); |
---|
439 | m_copydata(m, 0, pktlen, mtod(n, caddr_t)); |
---|
440 | n->m_len = pktlen; |
---|
441 | } else { |
---|
442 | IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, |
---|
443 | mtod(m, const struct ieee80211_frame *), NULL, |
---|
444 | "%s", "no mbuf to realign"); |
---|
445 | vap->iv_stats.is_rx_badalign++; |
---|
446 | } |
---|
447 | m_freem(m); |
---|
448 | return n; |
---|
449 | } |
---|
450 | |
---|
451 | int |
---|
452 | ieee80211_add_callback(struct mbuf *m, |
---|
453 | void (*func)(struct ieee80211_node *, void *, int), void *arg) |
---|
454 | { |
---|
455 | struct m_tag *mtag; |
---|
456 | struct ieee80211_cb *cb; |
---|
457 | |
---|
458 | mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_CALLBACK, |
---|
459 | sizeof(struct ieee80211_cb), M_NOWAIT); |
---|
460 | if (mtag == NULL) |
---|
461 | return 0; |
---|
462 | |
---|
463 | cb = (struct ieee80211_cb *)(mtag+1); |
---|
464 | cb->func = func; |
---|
465 | cb->arg = arg; |
---|
466 | m_tag_prepend(m, mtag); |
---|
467 | m->m_flags |= M_TXCB; |
---|
468 | return 1; |
---|
469 | } |
---|
470 | |
---|
471 | void |
---|
472 | ieee80211_process_callback(struct ieee80211_node *ni, |
---|
473 | struct mbuf *m, int status) |
---|
474 | { |
---|
475 | struct m_tag *mtag; |
---|
476 | |
---|
477 | mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_CALLBACK, NULL); |
---|
478 | if (mtag != NULL) { |
---|
479 | struct ieee80211_cb *cb = (struct ieee80211_cb *)(mtag+1); |
---|
480 | cb->func(ni, cb->arg, status); |
---|
481 | } |
---|
482 | } |
---|
483 | |
---|
484 | #include <sys/libkern.h> |
---|
485 | |
---|
486 | void |
---|
487 | get_random_bytes(void *p, size_t n) |
---|
488 | { |
---|
489 | uint8_t *dp = p; |
---|
490 | |
---|
491 | while (n > 0) { |
---|
492 | uint32_t v = arc4random(); |
---|
493 | size_t nb = n > sizeof(uint32_t) ? sizeof(uint32_t) : n; |
---|
494 | bcopy(&v, dp, n > sizeof(uint32_t) ? sizeof(uint32_t) : n); |
---|
495 | dp += sizeof(uint32_t), n -= nb; |
---|
496 | } |
---|
497 | } |
---|
498 | |
---|
499 | /* |
---|
500 | * Helper function for events that pass just a single mac address. |
---|
501 | */ |
---|
502 | static void |
---|
503 | notify_macaddr(struct ifnet *ifp, int op, const uint8_t mac[IEEE80211_ADDR_LEN]) |
---|
504 | { |
---|
505 | struct ieee80211_join_event iev; |
---|
506 | |
---|
507 | CURVNET_SET(ifp->if_vnet); |
---|
508 | memset(&iev, 0, sizeof(iev)); |
---|
509 | IEEE80211_ADDR_COPY(iev.iev_addr, mac); |
---|
510 | rt_ieee80211msg(ifp, op, &iev, sizeof(iev)); |
---|
511 | CURVNET_RESTORE(); |
---|
512 | } |
---|
513 | |
---|
514 | void |
---|
515 | ieee80211_notify_node_join(struct ieee80211_node *ni, int newassoc) |
---|
516 | { |
---|
517 | struct ieee80211vap *vap = ni->ni_vap; |
---|
518 | struct ifnet *ifp = vap->iv_ifp; |
---|
519 | |
---|
520 | CURVNET_SET_QUIET(ifp->if_vnet); |
---|
521 | IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode join", |
---|
522 | (ni == vap->iv_bss) ? "bss " : ""); |
---|
523 | |
---|
524 | if (ni == vap->iv_bss) { |
---|
525 | notify_macaddr(ifp, newassoc ? |
---|
526 | RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC, ni->ni_bssid); |
---|
527 | if_link_state_change(ifp, LINK_STATE_UP); |
---|
528 | } else { |
---|
529 | notify_macaddr(ifp, newassoc ? |
---|
530 | RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN, ni->ni_macaddr); |
---|
531 | } |
---|
532 | CURVNET_RESTORE(); |
---|
533 | } |
---|
534 | |
---|
535 | void |
---|
536 | ieee80211_notify_node_leave(struct ieee80211_node *ni) |
---|
537 | { |
---|
538 | struct ieee80211vap *vap = ni->ni_vap; |
---|
539 | struct ifnet *ifp = vap->iv_ifp; |
---|
540 | |
---|
541 | CURVNET_SET_QUIET(ifp->if_vnet); |
---|
542 | IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode leave", |
---|
543 | (ni == vap->iv_bss) ? "bss " : ""); |
---|
544 | |
---|
545 | if (ni == vap->iv_bss) { |
---|
546 | rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0); |
---|
547 | if_link_state_change(ifp, LINK_STATE_DOWN); |
---|
548 | } else { |
---|
549 | /* fire off wireless event station leaving */ |
---|
550 | notify_macaddr(ifp, RTM_IEEE80211_LEAVE, ni->ni_macaddr); |
---|
551 | } |
---|
552 | CURVNET_RESTORE(); |
---|
553 | } |
---|
554 | |
---|
555 | void |
---|
556 | ieee80211_notify_scan_done(struct ieee80211vap *vap) |
---|
557 | { |
---|
558 | struct ifnet *ifp = vap->iv_ifp; |
---|
559 | |
---|
560 | IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n", "notify scan done"); |
---|
561 | |
---|
562 | /* dispatch wireless event indicating scan completed */ |
---|
563 | CURVNET_SET(ifp->if_vnet); |
---|
564 | rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0); |
---|
565 | CURVNET_RESTORE(); |
---|
566 | } |
---|
567 | |
---|
568 | void |
---|
569 | ieee80211_notify_replay_failure(struct ieee80211vap *vap, |
---|
570 | const struct ieee80211_frame *wh, const struct ieee80211_key *k, |
---|
571 | u_int64_t rsc, int tid) |
---|
572 | { |
---|
573 | struct ifnet *ifp = vap->iv_ifp; |
---|
574 | |
---|
575 | IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2, |
---|
576 | "%s replay detected <rsc %ju, csc %ju, keyix %u rxkeyix %u>", |
---|
577 | k->wk_cipher->ic_name, (intmax_t) rsc, |
---|
578 | (intmax_t) k->wk_keyrsc[tid], |
---|
579 | k->wk_keyix, k->wk_rxkeyix); |
---|
580 | |
---|
581 | if (ifp != NULL) { /* NB: for cipher test modules */ |
---|
582 | struct ieee80211_replay_event iev; |
---|
583 | |
---|
584 | IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1); |
---|
585 | IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2); |
---|
586 | iev.iev_cipher = k->wk_cipher->ic_cipher; |
---|
587 | if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE) |
---|
588 | iev.iev_keyix = k->wk_rxkeyix; |
---|
589 | else |
---|
590 | iev.iev_keyix = k->wk_keyix; |
---|
591 | iev.iev_keyrsc = k->wk_keyrsc[tid]; |
---|
592 | iev.iev_rsc = rsc; |
---|
593 | CURVNET_SET(ifp->if_vnet); |
---|
594 | rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev)); |
---|
595 | CURVNET_RESTORE(); |
---|
596 | } |
---|
597 | } |
---|
598 | |
---|
599 | void |
---|
600 | ieee80211_notify_michael_failure(struct ieee80211vap *vap, |
---|
601 | const struct ieee80211_frame *wh, u_int keyix) |
---|
602 | { |
---|
603 | struct ifnet *ifp = vap->iv_ifp; |
---|
604 | |
---|
605 | IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2, |
---|
606 | "michael MIC verification failed <keyix %u>", keyix); |
---|
607 | vap->iv_stats.is_rx_tkipmic++; |
---|
608 | |
---|
609 | if (ifp != NULL) { /* NB: for cipher test modules */ |
---|
610 | struct ieee80211_michael_event iev; |
---|
611 | |
---|
612 | IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1); |
---|
613 | IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2); |
---|
614 | iev.iev_cipher = IEEE80211_CIPHER_TKIP; |
---|
615 | iev.iev_keyix = keyix; |
---|
616 | CURVNET_SET(ifp->if_vnet); |
---|
617 | rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev)); |
---|
618 | CURVNET_RESTORE(); |
---|
619 | } |
---|
620 | } |
---|
621 | |
---|
622 | void |
---|
623 | ieee80211_notify_wds_discover(struct ieee80211_node *ni) |
---|
624 | { |
---|
625 | struct ieee80211vap *vap = ni->ni_vap; |
---|
626 | struct ifnet *ifp = vap->iv_ifp; |
---|
627 | |
---|
628 | notify_macaddr(ifp, RTM_IEEE80211_WDS, ni->ni_macaddr); |
---|
629 | } |
---|
630 | |
---|
631 | void |
---|
632 | ieee80211_notify_csa(struct ieee80211com *ic, |
---|
633 | const struct ieee80211_channel *c, int mode, int count) |
---|
634 | { |
---|
635 | struct ifnet *ifp = ic->ic_ifp; |
---|
636 | struct ieee80211_csa_event iev; |
---|
637 | |
---|
638 | memset(&iev, 0, sizeof(iev)); |
---|
639 | iev.iev_flags = c->ic_flags; |
---|
640 | iev.iev_freq = c->ic_freq; |
---|
641 | iev.iev_ieee = c->ic_ieee; |
---|
642 | iev.iev_mode = mode; |
---|
643 | iev.iev_count = count; |
---|
644 | rt_ieee80211msg(ifp, RTM_IEEE80211_CSA, &iev, sizeof(iev)); |
---|
645 | } |
---|
646 | |
---|
647 | void |
---|
648 | ieee80211_notify_radar(struct ieee80211com *ic, |
---|
649 | const struct ieee80211_channel *c) |
---|
650 | { |
---|
651 | struct ifnet *ifp = ic->ic_ifp; |
---|
652 | struct ieee80211_radar_event iev; |
---|
653 | |
---|
654 | memset(&iev, 0, sizeof(iev)); |
---|
655 | iev.iev_flags = c->ic_flags; |
---|
656 | iev.iev_freq = c->ic_freq; |
---|
657 | iev.iev_ieee = c->ic_ieee; |
---|
658 | rt_ieee80211msg(ifp, RTM_IEEE80211_RADAR, &iev, sizeof(iev)); |
---|
659 | } |
---|
660 | |
---|
661 | void |
---|
662 | ieee80211_notify_cac(struct ieee80211com *ic, |
---|
663 | const struct ieee80211_channel *c, enum ieee80211_notify_cac_event type) |
---|
664 | { |
---|
665 | struct ifnet *ifp = ic->ic_ifp; |
---|
666 | struct ieee80211_cac_event iev; |
---|
667 | |
---|
668 | memset(&iev, 0, sizeof(iev)); |
---|
669 | iev.iev_flags = c->ic_flags; |
---|
670 | iev.iev_freq = c->ic_freq; |
---|
671 | iev.iev_ieee = c->ic_ieee; |
---|
672 | iev.iev_type = type; |
---|
673 | rt_ieee80211msg(ifp, RTM_IEEE80211_CAC, &iev, sizeof(iev)); |
---|
674 | } |
---|
675 | |
---|
676 | void |
---|
677 | ieee80211_notify_node_deauth(struct ieee80211_node *ni) |
---|
678 | { |
---|
679 | struct ieee80211vap *vap = ni->ni_vap; |
---|
680 | struct ifnet *ifp = vap->iv_ifp; |
---|
681 | |
---|
682 | IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node deauth"); |
---|
683 | |
---|
684 | notify_macaddr(ifp, RTM_IEEE80211_DEAUTH, ni->ni_macaddr); |
---|
685 | } |
---|
686 | |
---|
687 | void |
---|
688 | ieee80211_notify_node_auth(struct ieee80211_node *ni) |
---|
689 | { |
---|
690 | struct ieee80211vap *vap = ni->ni_vap; |
---|
691 | struct ifnet *ifp = vap->iv_ifp; |
---|
692 | |
---|
693 | IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node auth"); |
---|
694 | |
---|
695 | notify_macaddr(ifp, RTM_IEEE80211_AUTH, ni->ni_macaddr); |
---|
696 | } |
---|
697 | |
---|
698 | void |
---|
699 | ieee80211_notify_country(struct ieee80211vap *vap, |
---|
700 | const uint8_t bssid[IEEE80211_ADDR_LEN], const uint8_t cc[2]) |
---|
701 | { |
---|
702 | struct ifnet *ifp = vap->iv_ifp; |
---|
703 | struct ieee80211_country_event iev; |
---|
704 | |
---|
705 | memset(&iev, 0, sizeof(iev)); |
---|
706 | IEEE80211_ADDR_COPY(iev.iev_addr, bssid); |
---|
707 | iev.iev_cc[0] = cc[0]; |
---|
708 | iev.iev_cc[1] = cc[1]; |
---|
709 | rt_ieee80211msg(ifp, RTM_IEEE80211_COUNTRY, &iev, sizeof(iev)); |
---|
710 | } |
---|
711 | |
---|
712 | void |
---|
713 | ieee80211_notify_radio(struct ieee80211com *ic, int state) |
---|
714 | { |
---|
715 | struct ifnet *ifp = ic->ic_ifp; |
---|
716 | struct ieee80211_radio_event iev; |
---|
717 | |
---|
718 | memset(&iev, 0, sizeof(iev)); |
---|
719 | iev.iev_state = state; |
---|
720 | rt_ieee80211msg(ifp, RTM_IEEE80211_RADIO, &iev, sizeof(iev)); |
---|
721 | } |
---|
722 | |
---|
723 | void |
---|
724 | ieee80211_load_module(const char *modname) |
---|
725 | { |
---|
726 | |
---|
727 | #ifdef notyet |
---|
728 | (void)kern_kldload(curthread, modname, NULL); |
---|
729 | #else |
---|
730 | printf("%s: load the %s module by hand for now.\n", __func__, modname); |
---|
731 | #endif |
---|
732 | } |
---|
733 | |
---|
734 | static eventhandler_tag wlan_bpfevent; |
---|
735 | static eventhandler_tag wlan_ifllevent; |
---|
736 | |
---|
737 | static void |
---|
738 | bpf_track(void *arg, struct ifnet *ifp, int dlt, int attach) |
---|
739 | { |
---|
740 | /* NB: identify vap's by if_start */ |
---|
741 | if (dlt == DLT_IEEE802_11_RADIO && ifp->if_start == ieee80211_start) { |
---|
742 | struct ieee80211vap *vap = ifp->if_softc; |
---|
743 | /* |
---|
744 | * Track bpf radiotap listener state. We mark the vap |
---|
745 | * to indicate if any listener is present and the com |
---|
746 | * to indicate if any listener exists on any associated |
---|
747 | * vap. This flag is used by drivers to prepare radiotap |
---|
748 | * state only when needed. |
---|
749 | */ |
---|
750 | if (attach) { |
---|
751 | ieee80211_syncflag_ext(vap, IEEE80211_FEXT_BPF); |
---|
752 | if (vap->iv_opmode == IEEE80211_M_MONITOR) |
---|
753 | atomic_add_int(&vap->iv_ic->ic_montaps, 1); |
---|
754 | } else if (!bpf_peers_present(vap->iv_rawbpf)) { |
---|
755 | ieee80211_syncflag_ext(vap, -IEEE80211_FEXT_BPF); |
---|
756 | if (vap->iv_opmode == IEEE80211_M_MONITOR) |
---|
757 | atomic_subtract_int(&vap->iv_ic->ic_montaps, 1); |
---|
758 | } |
---|
759 | } |
---|
760 | } |
---|
761 | |
---|
762 | static void |
---|
763 | wlan_iflladdr(void *arg __unused, struct ifnet *ifp) |
---|
764 | { |
---|
765 | struct ieee80211com *ic = ifp->if_l2com; |
---|
766 | struct ieee80211vap *vap, *next; |
---|
767 | |
---|
768 | if (ifp->if_type != IFT_IEEE80211 || ic == NULL) |
---|
769 | return; |
---|
770 | |
---|
771 | IEEE80211_LOCK(ic); |
---|
772 | TAILQ_FOREACH_SAFE(vap, &ic->ic_vaps, iv_next, next) { |
---|
773 | /* |
---|
774 | * If the MAC address has changed on the parent and it was |
---|
775 | * copied to the vap on creation then re-sync. |
---|
776 | */ |
---|
777 | if (vap->iv_ic == ic && |
---|
778 | (vap->iv_flags_ext & IEEE80211_FEXT_UNIQMAC) == 0) { |
---|
779 | IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp)); |
---|
780 | IEEE80211_UNLOCK(ic); |
---|
781 | if_setlladdr(vap->iv_ifp, IF_LLADDR(ifp), |
---|
782 | IEEE80211_ADDR_LEN); |
---|
783 | IEEE80211_LOCK(ic); |
---|
784 | } |
---|
785 | } |
---|
786 | IEEE80211_UNLOCK(ic); |
---|
787 | } |
---|
788 | |
---|
789 | /* |
---|
790 | * Module glue. |
---|
791 | * |
---|
792 | * NB: the module name is "wlan" for compatibility with NetBSD. |
---|
793 | */ |
---|
794 | static int |
---|
795 | wlan_modevent(module_t mod, int type, void *unused) |
---|
796 | { |
---|
797 | switch (type) { |
---|
798 | case MOD_LOAD: |
---|
799 | if (bootverbose) |
---|
800 | printf("wlan: <802.11 Link Layer>\n"); |
---|
801 | wlan_bpfevent = EVENTHANDLER_REGISTER(bpf_track, |
---|
802 | bpf_track, 0, EVENTHANDLER_PRI_ANY); |
---|
803 | if (wlan_bpfevent == NULL) |
---|
804 | return ENOMEM; |
---|
805 | wlan_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event, |
---|
806 | wlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY); |
---|
807 | if (wlan_ifllevent == NULL) { |
---|
808 | EVENTHANDLER_DEREGISTER(bpf_track, wlan_bpfevent); |
---|
809 | return ENOMEM; |
---|
810 | } |
---|
811 | if_clone_attach(&wlan_cloner); |
---|
812 | if_register_com_alloc(IFT_IEEE80211, wlan_alloc, wlan_free); |
---|
813 | return 0; |
---|
814 | case MOD_UNLOAD: |
---|
815 | if_deregister_com_alloc(IFT_IEEE80211); |
---|
816 | if_clone_detach(&wlan_cloner); |
---|
817 | EVENTHANDLER_DEREGISTER(bpf_track, wlan_bpfevent); |
---|
818 | EVENTHANDLER_DEREGISTER(iflladdr_event, wlan_ifllevent); |
---|
819 | return 0; |
---|
820 | } |
---|
821 | return EINVAL; |
---|
822 | } |
---|
823 | |
---|
824 | static moduledata_t wlan_mod = { |
---|
825 | "wlan", |
---|
826 | wlan_modevent, |
---|
827 | 0 |
---|
828 | }; |
---|
829 | DECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); |
---|
830 | MODULE_VERSION(wlan, 1); |
---|
831 | MODULE_DEPEND(wlan, ether, 1, 1, 1); |
---|