ieee80211_netbsd.c revision 1.8 1 /* $NetBSD: ieee80211_netbsd.c,v 1.8 2005/11/18 16:40:09 skrll Exp $ */
2 /*-
3 * Copyright (c) 2003-2005 Sam Leffler, Errno Consulting
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #ifdef __FreeBSD__
31 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_freebsd.c,v 1.8 2005/08/08 18:46:35 sam Exp $");
32 #else
33 __KERNEL_RCSID(0, "$NetBSD: ieee80211_netbsd.c,v 1.8 2005/11/18 16:40:09 skrll Exp $");
34 #endif
35
36 /*
37 * IEEE 802.11 support (FreeBSD-specific code)
38 */
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/systm.h>
42 #include <sys/mbuf.h>
43 #include <sys/proc.h>
44 #include <sys/sysctl.h>
45
46 #include <machine/stdarg.h>
47
48 #include <sys/socket.h>
49
50 #include <net/if.h>
51 #include <net/if_media.h>
52 #include <net/if_ether.h>
53 #include <net/route.h>
54
55 #include <net80211/ieee80211_netbsd.h>
56 #include <net80211/ieee80211_var.h>
57 #include <net80211/ieee80211_sysctl.h>
58
59 #define LOGICALLY_EQUAL(x, y) (!(x) == !(y))
60
61 static void ieee80211_sysctl_fill_node(struct ieee80211_node *,
62 struct ieee80211_node_sysctl *, int, struct ieee80211_channel *, int);
63 static struct ieee80211_node *ieee80211_node_walknext(
64 struct ieee80211_node_walk *);
65 static struct ieee80211_node *ieee80211_node_walkfirst(
66 struct ieee80211_node_walk *, u_short);
67 static int ieee80211_sysctl_node(SYSCTLFN_ARGS);
68
69 #ifdef IEEE80211_DEBUG
70 int ieee80211_debug = 0;
71 #endif
72
73 typedef void (*ieee80211_setup_func)(void);
74
75 __link_set_decl(ieee80211_funcs, ieee80211_setup_func);
76
77 void
78 ieee80211_init(void)
79 {
80 ieee80211_setup_func * const *ieee80211_setup, f;
81
82 __link_set_foreach(ieee80211_setup, ieee80211_funcs) {
83 f = (void*)*ieee80211_setup;
84 (*f)();
85 }
86 }
87
88 static int
89 ieee80211_sysctl_inact(SYSCTLFN_ARGS)
90 {
91 int error, t;
92 struct sysctlnode node;
93
94 node = *rnode;
95 /* sysctl_lookup copies the product from t. Then, it
96 * copies the new value onto t.
97 */
98 t = *(int*)rnode->sysctl_data * IEEE80211_INACT_WAIT;
99 node.sysctl_data = &t;
100 error = sysctl_lookup(SYSCTLFN_CALL(&node));
101 if (error || newp == NULL)
102 return (error);
103
104 /* The new value was in seconds. Convert to inactivity-wait
105 * intervals. There are IEEE80211_INACT_WAIT seconds per
106 * interval.
107 */
108 *(int*)rnode->sysctl_data = t / IEEE80211_INACT_WAIT;
109
110 return (0);
111 }
112
113 static int
114 ieee80211_sysctl_parent(SYSCTLFN_ARGS)
115 {
116 struct ieee80211com *ic;
117 char pname[IFNAMSIZ];
118 struct sysctlnode node;
119
120 node = *rnode;
121 ic = node.sysctl_data;
122 strncpy(pname, ic->ic_ifp->if_xname, IFNAMSIZ);
123 node.sysctl_data = pname;
124 return sysctl_lookup(SYSCTLFN_CALL(&node));
125 }
126
127 /*
128 * Create or get top of sysctl tree net.link.ieee80211.
129 */
130 static const struct sysctlnode *
131 ieee80211_sysctl_treetop(struct sysctllog **log)
132 {
133 int rc;
134 const struct sysctlnode *rnode;
135
136 if ((rc = sysctl_createv(log, 0, NULL, &rnode,
137 CTLFLAG_PERMANENT, CTLTYPE_NODE, "net", NULL,
138 NULL, 0, NULL, 0, CTL_NET, CTL_EOL)) != 0)
139 goto err;
140
141 if ((rc = sysctl_createv(log, 0, &rnode, &rnode,
142 CTLFLAG_PERMANENT, CTLTYPE_NODE, "link",
143 "link-layer statistics and controls",
144 NULL, 0, NULL, 0, PF_LINK, CTL_EOL)) != 0)
145 goto err;
146
147 if ((rc = sysctl_createv(log, 0, &rnode, &rnode,
148 CTLFLAG_PERMANENT, CTLTYPE_NODE, "ieee80211",
149 "IEEE 802.11 WLAN statistics and controls",
150 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
151 goto err;
152
153 return rnode;
154 err:
155 printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc);
156 return NULL;
157 }
158
159 void
160 ieee80211_sysctl_attach(struct ieee80211com *ic)
161 {
162 int rc;
163 const struct sysctlnode *cnode, *rnode;
164 char num[sizeof("vap") + 14]; /* sufficient for 32 bits */
165
166 if ((rnode = ieee80211_sysctl_treetop(NULL)) == NULL)
167 return;
168
169 snprintf(num, sizeof(num), "vap%u", ic->ic_vap);
170
171 if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &rnode,
172 CTLFLAG_PERMANENT, CTLTYPE_NODE, num, SYSCTL_DESCR("virtual AP"),
173 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
174 goto err;
175
176 /* control debugging printfs */
177 if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode,
178 CTLFLAG_PERMANENT|CTLFLAG_READONLY, CTLTYPE_STRING,
179 "parent", SYSCTL_DESCR("parent device"),
180 ieee80211_sysctl_parent, 0, ic, IFNAMSIZ, CTL_CREATE,
181 CTL_EOL)) != 0)
182 goto err;
183
184 #ifdef IEEE80211_DEBUG
185 /* control debugging printfs */
186 if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode,
187 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
188 "debug", SYSCTL_DESCR("control debugging printfs"),
189 NULL, ieee80211_debug, &ic->ic_debug, 0,
190 CTL_CREATE, CTL_EOL)) != 0)
191 goto err;
192 #endif
193 /* XXX inherit from tunables */
194 if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode,
195 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
196 "inact_run", SYSCTL_DESCR("station inactivity timeout (sec)"),
197 ieee80211_sysctl_inact, 0, &ic->ic_inact_run, 0,
198 CTL_CREATE, CTL_EOL)) != 0)
199 goto err;
200 if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode,
201 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
202 "inact_probe",
203 SYSCTL_DESCR("station inactivity probe timeout (sec)"),
204 ieee80211_sysctl_inact, 0, &ic->ic_inact_probe, 0,
205 CTL_CREATE, CTL_EOL)) != 0)
206 goto err;
207 if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode,
208 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
209 "inact_auth",
210 SYSCTL_DESCR("station authentication timeout (sec)"),
211 ieee80211_sysctl_inact, 0, &ic->ic_inact_auth, 0,
212 CTL_CREATE, CTL_EOL)) != 0)
213 goto err;
214 if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode,
215 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
216 "inact_init",
217 SYSCTL_DESCR("station initial state timeout (sec)"),
218 ieee80211_sysctl_inact, 0, &ic->ic_inact_init, 0,
219 CTL_CREATE, CTL_EOL)) != 0)
220 goto err;
221 if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode,
222 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
223 "driver_caps", SYSCTL_DESCR("driver capabilities"),
224 NULL, 0, &ic->ic_caps, 0, CTL_CREATE, CTL_EOL)) != 0)
225 goto err;
226
227 return;
228 err:
229 printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc);
230 }
231
232 void
233 ieee80211_sysctl_detach(struct ieee80211com *ic)
234 {
235 sysctl_teardown(&ic->ic_sysctllog);
236 }
237
238 /*
239 * Pointers for testing:
240 *
241 * If there are no interfaces, or else no 802.11 interfaces,
242 * ieee80211_node_walkfirst must return NULL.
243 *
244 * If there is any single 802.11 interface, ieee80211_node_walkfirst
245 * must not return NULL.
246 */
247 static struct ieee80211_node *
248 ieee80211_node_walkfirst(struct ieee80211_node_walk *nw, u_short if_index)
249 {
250 (void)memset(nw, 0, sizeof(*nw));
251
252 nw->nw_ifindex = if_index;
253
254 LIST_FOREACH(nw->nw_ic, &ieee80211com_head, ic_list) {
255 if (if_index != 0 && nw->nw_ic->ic_ifp->if_index != if_index)
256 continue;
257 if (!TAILQ_EMPTY(&nw->nw_ic->ic_sta.nt_node))
258 nw->nw_nt = &nw->nw_ic->ic_sta;
259 else if (!TAILQ_EMPTY(&nw->nw_ic->ic_scan.nt_node))
260 nw->nw_nt = &nw->nw_ic->ic_scan;
261 else if (nw->nw_ic->ic_bss == NULL)
262 continue;
263 break;
264 }
265
266 if (nw->nw_ic == NULL)
267 return NULL;
268
269 if (nw->nw_nt == NULL)
270 nw->nw_ni = nw->nw_ic->ic_bss;
271 else
272 nw->nw_ni = TAILQ_FIRST(&nw->nw_nt->nt_node);
273
274 return nw->nw_ni;
275 }
276
277 static struct ieee80211_node *
278 ieee80211_node_walknext(struct ieee80211_node_walk *nw)
279 {
280 if (nw->nw_nt != NULL)
281 nw->nw_ni = TAILQ_NEXT(nw->nw_ni, ni_list);
282 else
283 nw->nw_ni = NULL;
284
285 while (nw->nw_ni == NULL) {
286 if (nw->nw_nt == &nw->nw_ic->ic_sta) {
287 nw->nw_nt = &nw->nw_ic->ic_scan;
288 nw->nw_ni = TAILQ_FIRST(&nw->nw_nt->nt_node);
289 continue;
290 } else if (nw->nw_nt == &nw->nw_ic->ic_scan) {
291 nw->nw_nt = NULL;
292 nw->nw_ni = nw->nw_ic->ic_bss;
293 continue;
294 }
295 KASSERT(nw->nw_nt == NULL);
296 if (nw->nw_ifindex != 0)
297 return NULL;
298
299 nw->nw_ic = LIST_NEXT(nw->nw_ic, ic_list);
300 if (nw->nw_ic == NULL)
301 return NULL;
302
303 nw->nw_nt = &nw->nw_ic->ic_sta;
304 nw->nw_ni = TAILQ_FIRST(&nw->nw_nt->nt_node);
305 }
306
307 return nw->nw_ni;
308 }
309
310 static void
311 ieee80211_sysctl_fill_node(struct ieee80211_node *ni,
312 struct ieee80211_node_sysctl *ns, int ifindex,
313 struct ieee80211_channel *chan0, int is_bss)
314 {
315 ns->ns_ifindex = ifindex;
316 ns->ns_capinfo = ni->ni_capinfo;
317 ns->ns_flags = (is_bss) ? IEEE80211_NODE_SYSCTL_F_BSS : 0;
318 (void)memcpy(ns->ns_macaddr, ni->ni_macaddr, sizeof(ns->ns_macaddr));
319 (void)memcpy(ns->ns_bssid, ni->ni_bssid, sizeof(ns->ns_bssid));
320 if (ni->ni_chan != IEEE80211_CHAN_ANYC) {
321 ns->ns_freq = ni->ni_chan->ic_freq;
322 ns->ns_chanflags = ni->ni_chan->ic_flags;
323 ns->ns_chanidx = ni->ni_chan - chan0;
324 } else {
325 ns->ns_freq = ns->ns_chanflags = 0;
326 ns->ns_chanidx = 0;
327 }
328 ns->ns_rssi = ni->ni_rssi;
329 ns->ns_esslen = ni->ni_esslen;
330 (void)memcpy(ns->ns_essid, ni->ni_essid, sizeof(ns->ns_essid));
331 ns->ns_erp = ni->ni_erp;
332 ns->ns_associd = ni->ni_associd;
333 ns->ns_inact = ni->ni_inact * IEEE80211_INACT_WAIT;
334 ns->ns_rstamp = ni->ni_rstamp;
335 ns->ns_rates = ni->ni_rates;
336 ns->ns_txrate = ni->ni_txrate;
337 ns->ns_intval = ni->ni_intval;
338 (void)memcpy(ns->ns_tstamp, &ni->ni_tstamp, sizeof(ns->ns_tstamp));
339 ns->ns_txseq = ni->ni_txseqs[0];
340 ns->ns_rxseq = ni->ni_rxseqs[0];
341 ns->ns_fhdwell = ni->ni_fhdwell;
342 ns->ns_fhindex = ni->ni_fhindex;
343 ns->ns_fails = ni->ni_fails;
344 }
345
346 /* Between two examinations of the sysctl tree, I expect each
347 * interface to add no more than 5 nodes.
348 */
349 #define IEEE80211_SYSCTL_NODE_GROWTH 5
350
351 static int
352 ieee80211_sysctl_node(SYSCTLFN_ARGS)
353 {
354 struct ieee80211_node_walk nw;
355 struct ieee80211_node *ni;
356 struct ieee80211_node_sysctl ns;
357 char *dp;
358 u_int cur_ifindex, ifcount, ifindex, last_ifindex, op, arg, hdr_type;
359 size_t len, needed, eltsize, out_size;
360 int error, s, saw_bss = 0, nelt;
361
362 if (namelen == 1 && name[0] == CTL_QUERY)
363 return (sysctl_query(SYSCTLFN_CALL(rnode)));
364
365 if (namelen != IEEE80211_SYSCTL_NODENAMELEN)
366 return (EINVAL);
367
368 /* ifindex.op.arg.header-type.eltsize.nelt */
369 dp = oldp;
370 len = (oldp != NULL) ? *oldlenp : 0;
371 ifindex = name[IEEE80211_SYSCTL_NODENAME_IF];
372 op = name[IEEE80211_SYSCTL_NODENAME_OP];
373 arg = name[IEEE80211_SYSCTL_NODENAME_ARG];
374 hdr_type = name[IEEE80211_SYSCTL_NODENAME_TYPE];
375 eltsize = name[IEEE80211_SYSCTL_NODENAME_ELTSIZE];
376 nelt = name[IEEE80211_SYSCTL_NODENAME_ELTCOUNT];
377 out_size = MIN(sizeof(ns), eltsize);
378
379 if (op != IEEE80211_SYSCTL_OP_ALL || arg != 0 ||
380 hdr_type != IEEE80211_SYSCTL_T_NODE || eltsize < 1 || nelt < 0)
381 return (EINVAL);
382
383 error = 0;
384 needed = 0;
385 ifcount = 0;
386 last_ifindex = 0;
387
388 s = splnet();
389
390 for (ni = ieee80211_node_walkfirst(&nw, ifindex); ni != NULL;
391 ni = ieee80211_node_walknext(&nw)) {
392 struct ieee80211com *ic;
393
394 ic = nw.nw_ic;
395 cur_ifindex = ic->ic_ifp->if_index;
396
397 if (cur_ifindex != last_ifindex) {
398 saw_bss = 0;
399 ifcount++;
400 last_ifindex = cur_ifindex;
401 }
402
403 if (nelt <= 0)
404 continue;
405
406 if (saw_bss && ni == ic->ic_bss)
407 continue;
408 else if (ni == ic->ic_bss)
409 saw_bss = 1;
410 if (len >= eltsize) {
411 ieee80211_sysctl_fill_node(ni, &ns, cur_ifindex,
412 &ic->ic_channels[0], ni == ic->ic_bss);
413 error = copyout(&ns, dp, out_size);
414 if (error)
415 goto cleanup;
416 dp += eltsize;
417 len -= eltsize;
418 }
419 needed += eltsize;
420 if (nelt != INT_MAX)
421 nelt--;
422 }
423 cleanup:
424 splx(s);
425
426 *oldlenp = needed;
427 if (oldp == NULL)
428 *oldlenp += ifcount * IEEE80211_SYSCTL_NODE_GROWTH * eltsize;
429
430 return (error);
431 }
432
433 /*
434 * Setup sysctl(3) MIB, net.ieee80211.*
435 *
436 * TBD condition CTLFLAG_PERMANENT on being an LKM or not
437 */
438 SYSCTL_SETUP(sysctl_ieee80211, "sysctl ieee80211 subtree setup")
439 {
440 int rc;
441 const struct sysctlnode *cnode, *rnode;
442
443 if ((rnode = ieee80211_sysctl_treetop(clog)) == NULL)
444 return;
445
446 if ((rc = sysctl_createv(clog, 0, &rnode, NULL,
447 CTLFLAG_PERMANENT, CTLTYPE_NODE, "nodes", "client/peer stations",
448 ieee80211_sysctl_node, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
449 goto err;
450
451 #ifdef IEEE80211_DEBUG
452 /* control debugging printfs */
453 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
454 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
455 "debug", SYSCTL_DESCR("control debugging printfs"),
456 NULL, 0, &ieee80211_debug, 0, CTL_CREATE, CTL_EOL)) != 0)
457 goto err;
458 #endif /* IEEE80211_DEBUG */
459
460 return;
461 err:
462 printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
463 }
464
465 int
466 ieee80211_node_dectestref(struct ieee80211_node *ni)
467 {
468 int rc, s;
469 s = splnet();
470 if (--ni->ni_refcnt == 0) {
471 rc = 1;
472 ni->ni_refcnt = 1;
473 } else
474 rc = 0;
475 splx(s);
476 return rc;
477 }
478
479 void
480 if_printf(struct ifnet *ifp, const char *fmt, ...)
481 {
482 va_list ap;
483 va_start(ap, fmt);
484
485 printf("%s: ", ifp->if_xname);
486 vprintf(fmt, ap);
487
488 va_end(ap);
489 return;
490 }
491
492 struct mbuf *
493 m_getcl(int how, int type, int flags)
494 {
495 struct mbuf *m;
496
497 if ((flags & M_PKTHDR) != 0)
498 MGETHDR(m, how, type);
499 else
500 MGET(m, how, type);
501
502 if (m == NULL)
503 return NULL;
504
505 MCLGET(m, flags);
506
507 if ((m->m_flags & M_EXT) == 0) {
508 m_free(m);
509 return NULL;
510 }
511 return m;
512 }
513
514 /*
515 * Append the specified data to the indicated mbuf chain,
516 * Extend the mbuf chain if the new data does not fit in
517 * existing space.
518 *
519 * Return 1 if able to complete the job; otherwise 0.
520 */
521 int
522 m_append(struct mbuf *m0, int len, caddr_t cp)
523 {
524 struct mbuf *m, *n;
525 int remainder, space;
526
527 for (m = m0; m->m_next != NULL; m = m->m_next)
528 ;
529 remainder = len;
530 space = M_TRAILINGSPACE(m);
531 if (space > 0) {
532 /*
533 * Copy into available space.
534 */
535 if (space > remainder)
536 space = remainder;
537 memmove(mtod(m, caddr_t) + m->m_len, cp, space);
538 m->m_len += space;
539 cp += space, remainder -= space;
540 }
541 while (remainder > 0) {
542 /*
543 * Allocate a new mbuf; could check space
544 * and allocate a cluster instead.
545 */
546 n = m_get(M_DONTWAIT, m->m_type);
547 if (n == NULL)
548 break;
549 n->m_len = min(MLEN, remainder);
550 memmove(mtod(n, caddr_t), cp, n->m_len);
551 cp += n->m_len, remainder -= n->m_len;
552 m->m_next = n;
553 m = n;
554 }
555 if (m0->m_flags & M_PKTHDR)
556 m0->m_pkthdr.len += len - remainder;
557 return (remainder == 0);
558 }
559
560 /*
561 * Allocate and setup a management frame of the specified
562 * size. We return the mbuf and a pointer to the start
563 * of the contiguous data area that's been reserved based
564 * on the packet length. The data area is forced to 32-bit
565 * alignment and the buffer length to a multiple of 4 bytes.
566 * This is done mainly so beacon frames (that require this)
567 * can use this interface too.
568 */
569 struct mbuf *
570 ieee80211_getmgtframe(u_int8_t **frm, u_int pktlen)
571 {
572 struct mbuf *m;
573 u_int len;
574
575 /*
576 * NB: we know the mbuf routines will align the data area
577 * so we don't need to do anything special.
578 */
579 /* XXX 4-address frame? */
580 len = roundup(sizeof(struct ieee80211_frame) + pktlen, 4);
581 IASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len));
582 if (len <= MHLEN) {
583 m = m_gethdr(M_NOWAIT, MT_HEADER);
584 /*
585 * Align the data in case additional headers are added.
586 * This should only happen when a WEP header is added
587 * which only happens for shared key authentication mgt
588 * frames which all fit in MHLEN.
589 */
590 if (m != NULL)
591 MH_ALIGN(m, len);
592 } else
593 m = m_getcl(M_NOWAIT, MT_HEADER, M_PKTHDR);
594 if (m != NULL) {
595 m->m_data += sizeof(struct ieee80211_frame);
596 *frm = m->m_data;
597 }
598 return m;
599 }
600
601 void
602 get_random_bytes(void *p, size_t n)
603 {
604 u_int8_t *dp = p;
605
606 while (n > 0) {
607 u_int32_t v = arc4random();
608 size_t nb = n > sizeof(u_int32_t) ? sizeof(u_int32_t) : n;
609 (void)memcpy(dp, &v, nb);
610 dp += sizeof(u_int32_t), n -= nb;
611 }
612 }
613
614 void
615 ieee80211_notify_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int newassoc)
616 {
617 struct ifnet *ifp = ic->ic_ifp;
618 struct ieee80211_join_event iev;
619
620 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, "%s: %snode %s join\n",
621 ifp->if_xname, (ni == ic->ic_bss) ? "bss " : "",
622 ether_sprintf(ni->ni_macaddr));
623
624 memset(&iev, 0, sizeof(iev));
625 if (ni == ic->ic_bss) {
626 IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_bssid);
627 rt_ieee80211msg(ifp, newassoc ?
628 RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC,
629 &iev, sizeof(iev));
630 if_link_state_change(ifp, LINK_STATE_UP);
631 } else if (newassoc) {
632 /* fire off wireless event only for new station */
633 IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
634 rt_ieee80211msg(ifp, RTM_IEEE80211_JOIN, &iev, sizeof(iev));
635 }
636 }
637
638 void
639 ieee80211_notify_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
640 {
641 struct ifnet *ifp = ic->ic_ifp;
642 struct ieee80211_leave_event iev;
643
644 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, "%s: %snode %s leave\n",
645 ifp->if_xname, (ni == ic->ic_bss) ? "bss " : "",
646 ether_sprintf(ni->ni_macaddr));
647
648 if (ni == ic->ic_bss) {
649 rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
650 if_link_state_change(ifp, LINK_STATE_DOWN);
651 } else {
652 /* fire off wireless event station leaving */
653 memset(&iev, 0, sizeof(iev));
654 IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
655 rt_ieee80211msg(ifp, RTM_IEEE80211_LEAVE, &iev, sizeof(iev));
656 }
657 }
658
659 void
660 ieee80211_notify_scan_done(struct ieee80211com *ic)
661 {
662 struct ifnet *ifp = ic->ic_ifp;
663
664 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
665 "%s: notify scan done\n", ic->ic_ifp->if_xname);
666
667 /* dispatch wireless event indicating scan completed */
668 rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
669 }
670
671 void
672 ieee80211_notify_replay_failure(struct ieee80211com *ic,
673 const struct ieee80211_frame *wh, const struct ieee80211_key *k,
674 u_int64_t rsc)
675 {
676 struct ifnet *ifp = ic->ic_ifp;
677
678 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
679 "[%s] %s replay detected <rsc %ju, csc %ju, keyix %u rxkeyix %u>\n",
680 ether_sprintf(wh->i_addr2), k->wk_cipher->ic_name,
681 (intmax_t) rsc, (intmax_t) k->wk_keyrsc,
682 k->wk_keyix, k->wk_rxkeyix);
683
684 if (ifp != NULL) { /* NB: for cipher test modules */
685 struct ieee80211_replay_event iev;
686
687 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
688 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
689 iev.iev_cipher = k->wk_cipher->ic_cipher;
690 if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE)
691 iev.iev_keyix = k->wk_rxkeyix;
692 else
693 iev.iev_keyix = k->wk_keyix;
694 iev.iev_keyrsc = k->wk_keyrsc;
695 iev.iev_rsc = rsc;
696 rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
697 }
698 }
699
700 void
701 ieee80211_notify_michael_failure(struct ieee80211com *ic,
702 const struct ieee80211_frame *wh, u_int keyix)
703 {
704 struct ifnet *ifp = ic->ic_ifp;
705
706 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
707 "[%s] michael MIC verification failed <keyix %u>\n",
708 ether_sprintf(wh->i_addr2), keyix);
709 ic->ic_stats.is_rx_tkipmic++;
710
711 if (ifp != NULL) { /* NB: for cipher test modules */
712 struct ieee80211_michael_event iev;
713
714 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
715 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
716 iev.iev_cipher = IEEE80211_CIPHER_TKIP;
717 iev.iev_keyix = keyix;
718 rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev));
719 }
720 }
721
722 void
723 ieee80211_load_module(const char *modname)
724 {
725 #ifdef notyet
726 struct thread *td = curthread;
727
728 if (suser(td) == 0 && securelevel_gt(td->td_ucred, 0) == 0) {
729 mtx_lock(&Giant);
730 (void) linker_load_module(modname, NULL, NULL, NULL, NULL);
731 mtx_unlock(&Giant);
732 }
733 #else
734 printf("%s: load the %s module by hand for now.\n", __func__, modname);
735 #endif
736 }
737