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