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