xref: /netbsd-src/sys/net80211/ieee80211_netbsd.c (revision 5b84b3983f71fd20a534cfa5d1556623a8aaa717)
1 /* $NetBSD: ieee80211_netbsd.c,v 1.6 2005/07/26 22:52:48 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.6 2005/07/26 22:52:48 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, saw_bss = 0, 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 			saw_bss = 0;
384 			ifcount++;
385 			last_ifindex = cur_ifindex;
386 		}
387 
388 		if (nelt <= 0)
389 			continue;
390 
391 		if (saw_bss && ni == ic->ic_bss)
392 			continue;
393 		else if (ni == ic->ic_bss)
394 			saw_bss = 1;
395 		if (len >= eltsize) {
396 			ieee80211_sysctl_fill_node(ni, &ns, cur_ifindex,
397 			    &ic->ic_channels[0], ni == ic->ic_bss);
398 			error = copyout(&ns, dp, out_size);
399 			if (error)
400 				goto cleanup;
401 			dp += eltsize;
402 			len -= eltsize;
403 		}
404 		needed += eltsize;
405 		if (nelt != INT_MAX)
406 			nelt--;
407 	}
408 cleanup:
409 	splx(s);
410 
411 	*oldlenp = needed;
412 	if (oldp == NULL)
413 		*oldlenp += ifcount * IEEE80211_SYSCTL_NODE_GROWTH * eltsize;
414 
415 	return (error);
416 }
417 
418 /*
419  * Setup sysctl(3) MIB, net.ieee80211.*
420  *
421  * TBD condition CTLFLAG_PERMANENT on being an LKM or not
422  */
423 SYSCTL_SETUP(sysctl_ieee80211, "sysctl ieee80211 subtree setup")
424 {
425 	int rc;
426 	const struct sysctlnode *cnode, *rnode;
427 
428 	if ((rnode = ieee80211_sysctl_treetop(clog)) == NULL)
429 		return;
430 
431 	if ((rc = sysctl_createv(clog, 0, &rnode, NULL,
432 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "nodes", "client/peer stations",
433 	    ieee80211_sysctl_node, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
434 		goto err;
435 
436 #ifdef IEEE80211_DEBUG
437 	/* control debugging printfs */
438 	if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
439 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
440 	    "debug", SYSCTL_DESCR("control debugging printfs"),
441 	    NULL, 0, &ieee80211_debug, 0, CTL_CREATE, CTL_EOL)) != 0)
442 		goto err;
443 #endif /* IEEE80211_DEBUG */
444 
445 	return;
446 err:
447 	printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
448 }
449 
450 int
451 ieee80211_node_dectestref(struct ieee80211_node *ni)
452 {
453 	int rc, s;
454 	s = splnet();
455 	if (--ni->ni_refcnt == 0) {
456 		rc = 1;
457 		ni->ni_refcnt = 1;
458 	} else
459 		rc = 0;
460 	splx(s);
461 	return rc;
462 }
463 
464 void
465 if_printf(struct ifnet *ifp, const char *fmt, ...)
466 {
467 	va_list ap;
468 	va_start(ap, fmt);
469 
470 	printf("%s: ", ifp->if_xname);
471 	vprintf(fmt, ap);
472 
473 	va_end(ap);
474 	return;
475 }
476 
477 struct mbuf *
478 m_getcl(int how, int type, int flags)
479 {
480 	struct mbuf *m;
481 
482 	if ((flags & M_PKTHDR) != 0)
483 		MGETHDR(m, how, type);
484 	else
485 		MGET(m, how, type);
486 
487 	if (m == NULL)
488 		return NULL;
489 
490 	MCLGET(m, flags);
491 
492 	if ((m->m_flags & M_EXT) == 0) {
493 		m_free(m);
494 		return NULL;
495 	}
496 	return m;
497 }
498 
499 /*
500  * Append the specified data to the indicated mbuf chain,
501  * Extend the mbuf chain if the new data does not fit in
502  * existing space.
503  *
504  * Return 1 if able to complete the job; otherwise 0.
505  */
506 int
507 m_append(struct mbuf *m0, int len, caddr_t cp)
508 {
509 	struct mbuf *m, *n;
510 	int remainder, space;
511 
512 	for (m = m0; m->m_next != NULL; m = m->m_next)
513 		;
514 	remainder = len;
515 	space = M_TRAILINGSPACE(m);
516 	if (space > 0) {
517 		/*
518 		 * Copy into available space.
519 		 */
520 		if (space > remainder)
521 			space = remainder;
522 		memmove(mtod(m, caddr_t) + m->m_len, cp, space);
523 		m->m_len += space;
524 		cp += space, remainder -= space;
525 	}
526 	while (remainder > 0) {
527 		/*
528 		 * Allocate a new mbuf; could check space
529 		 * and allocate a cluster instead.
530 		 */
531 		n = m_get(M_DONTWAIT, m->m_type);
532 		if (n == NULL)
533 			break;
534 		n->m_len = min(MLEN, remainder);
535 		memmove(mtod(n, caddr_t), cp, n->m_len);
536 		cp += n->m_len, remainder -= n->m_len;
537 		m->m_next = n;
538 		m = n;
539 	}
540 	if (m0->m_flags & M_PKTHDR)
541 		m0->m_pkthdr.len += len - remainder;
542 	return (remainder == 0);
543 }
544 
545 /*
546  * Allocate and setup a management frame of the specified
547  * size.  We return the mbuf and a pointer to the start
548  * of the contiguous data area that's been reserved based
549  * on the packet length.  The data area is forced to 32-bit
550  * alignment and the buffer length to a multiple of 4 bytes.
551  * This is done mainly so beacon frames (that require this)
552  * can use this interface too.
553  */
554 struct mbuf *
555 ieee80211_getmgtframe(u_int8_t **frm, u_int pktlen)
556 {
557 	struct mbuf *m;
558 	u_int len;
559 
560 	/*
561 	 * NB: we know the mbuf routines will align the data area
562 	 *     so we don't need to do anything special.
563 	 */
564 	/* XXX 4-address frame? */
565 	len = roundup(sizeof(struct ieee80211_frame) + pktlen, 4);
566 	IASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len));
567 	if (len <= MHLEN) {
568 		m = m_gethdr(M_NOWAIT, MT_HEADER);
569 		/*
570 		 * Align the data in case additional headers are added.
571 		 * This should only happen when a WEP header is added
572 		 * which only happens for shared key authentication mgt
573 		 * frames which all fit in MHLEN.
574 		 */
575 		if (m != NULL)
576 			MH_ALIGN(m, len);
577 	} else
578 		m = m_getcl(M_NOWAIT, MT_HEADER, M_PKTHDR);
579 	if (m != NULL) {
580 		m->m_data += sizeof(struct ieee80211_frame);
581 		*frm = m->m_data;
582 	}
583 	return m;
584 }
585 
586 void
587 get_random_bytes(void *p, size_t n)
588 {
589 	u_int8_t *dp = p;
590 
591 	while (n > 0) {
592 		u_int32_t v = arc4random();
593 		size_t nb = n > sizeof(u_int32_t) ? sizeof(u_int32_t) : n;
594 		(void)memcpy(dp, &v, nb);
595 		dp += sizeof(u_int32_t), n -= nb;
596 	}
597 }
598 
599 void
600 ieee80211_notify_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int newassoc)
601 {
602 	struct ifnet *ifp = ic->ic_ifp;
603 	struct ieee80211_join_event iev;
604 
605 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, "%s: %snode %s join\n",
606 	    ifp->if_xname, (ni == ic->ic_bss) ? "bss " : "",
607 	    ether_sprintf(ni->ni_macaddr));
608 
609 	if (ni == ic->ic_bss) {
610 		memset(&iev, 0, sizeof(iev));
611 		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_bssid);
612 		rt_ieee80211msg(ifp, newassoc ?
613 			RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC,
614 			&iev, sizeof(iev));
615 		if_link_state_change(ifp, LINK_STATE_UP);
616 	} else if (newassoc) {
617 		/* fire off wireless event only for new station */
618 		memset(&iev, 0, sizeof(iev));
619 		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
620 		rt_ieee80211msg(ifp, RTM_IEEE80211_JOIN, &iev, sizeof(iev));
621 	}
622 }
623 
624 void
625 ieee80211_notify_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
626 {
627 	struct ifnet *ifp = ic->ic_ifp;
628 	struct ieee80211_leave_event iev;
629 
630 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, "%s: %snode %s leave\n",
631 	    ifp->if_xname, (ni == ic->ic_bss) ? "bss " : "",
632 	    ether_sprintf(ni->ni_macaddr));
633 
634 	if (ni == ic->ic_bss) {
635 		rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
636 		if_link_state_change(ifp, LINK_STATE_DOWN);
637 	} else {
638 		/* fire off wireless event station leaving */
639 		memset(&iev, 0, sizeof(iev));
640 		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
641 		rt_ieee80211msg(ifp, RTM_IEEE80211_LEAVE, &iev, sizeof(iev));
642 	}
643 }
644 
645 void
646 ieee80211_notify_scan_done(struct ieee80211com *ic)
647 {
648 	struct ifnet *ifp = ic->ic_ifp;
649 
650 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
651 		"%s: notify scan done\n", ic->ic_ifp->if_xname);
652 
653 	/* dispatch wireless event indicating scan completed */
654 	rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
655 }
656 
657 void
658 ieee80211_notify_replay_failure(struct ieee80211com *ic,
659 	const struct ieee80211_frame *wh, const struct ieee80211_key *k,
660 	u_int64_t rsc)
661 {
662 	struct ifnet *ifp = ic->ic_ifp;
663 
664 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
665 		"[%s] %s replay detected <rsc %ju, csc %ju, keyix %u>\n",
666 		ether_sprintf(wh->i_addr2), k->wk_cipher->ic_name,
667 		(intmax_t) rsc, (intmax_t) k->wk_keyrsc, k->wk_keyix);
668 
669 	if (ifp != NULL) {		/* NB: for cipher test modules */
670 		struct ieee80211_replay_event iev;
671 
672 		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
673 		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
674 		iev.iev_cipher = k->wk_cipher->ic_cipher;
675 		iev.iev_keyix = k->wk_keyix;
676 		iev.iev_keyrsc = k->wk_keyrsc;
677 		iev.iev_rsc = rsc;
678 		rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
679 	}
680 }
681 
682 void
683 ieee80211_notify_michael_failure(struct ieee80211com *ic,
684 	const struct ieee80211_frame *wh, u_int keyix)
685 {
686 	struct ifnet *ifp = ic->ic_ifp;
687 
688 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
689 		"[%s] michael MIC verification failed <keyix %u>\n",
690 	       ether_sprintf(wh->i_addr2), keyix);
691 	ic->ic_stats.is_rx_tkipmic++;
692 
693 	if (ifp != NULL) {		/* NB: for cipher test modules */
694 		struct ieee80211_michael_event iev;
695 
696 		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
697 		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
698 		iev.iev_cipher = IEEE80211_CIPHER_TKIP;
699 		iev.iev_keyix = keyix;
700 		rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev));
701 	}
702 }
703 
704 void
705 ieee80211_load_module(const char *modname)
706 {
707 #ifdef notyet
708 	struct thread *td = curthread;
709 
710 	if (suser(td) == 0 && securelevel_gt(td->td_ucred, 0) == 0) {
711 		mtx_lock(&Giant);
712 		(void) linker_load_module(modname, NULL, NULL, NULL, NULL);
713 		mtx_unlock(&Giant);
714 	}
715 #else
716 	printf("%s: load the %s module by hand for now.\n", __func__, modname);
717 #endif
718 }
719