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