xref: /freebsd/sys/net80211/ieee80211_freebsd.c (revision b3aaa0cc21c63d388230c7ef2a80abd631ff20d5)
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/if.h>
46 #include <net/if_clone.h>
47 #include <net/if_media.h>
48 #include <net/if_types.h>
49 #include <net/ethernet.h>
50 #include <net/route.h>
51 
52 #include <net80211/ieee80211_var.h>
53 
54 SYSCTL_NODE(_net, OID_AUTO, wlan, CTLFLAG_RD, 0, "IEEE 80211 parameters");
55 
56 #ifdef IEEE80211_DEBUG
57 int	ieee80211_debug = 0;
58 SYSCTL_INT(_net_wlan, OID_AUTO, debug, CTLFLAG_RW, &ieee80211_debug,
59 	    0, "debugging printfs");
60 #endif
61 extern int ieee80211_recv_bar_ena;
62 SYSCTL_INT(_net_wlan, OID_AUTO, recv_bar, CTLFLAG_RW, &ieee80211_recv_bar_ena,
63 	    0, "BAR frame processing (ena/dis)");
64 extern int ieee80211_nol_timeout;
65 SYSCTL_INT(_net_wlan, OID_AUTO, nol_timeout, CTLFLAG_RW,
66 	&ieee80211_nol_timeout, 0, "NOL timeout (secs)");
67 extern int ieee80211_cac_timeout;
68 SYSCTL_INT(_net_wlan, OID_AUTO, cac_timeout, CTLFLAG_RW,
69 	&ieee80211_cac_timeout, 0, "CAC timeout (secs)");
70 
71 MALLOC_DEFINE(M_80211_COM, "80211com", "802.11 com state");
72 
73 /*
74  * Allocate/free com structure in conjunction with ifnet;
75  * these routines are registered with if_register_com_alloc
76  * below and are called automatically by the ifnet code
77  * when the ifnet of the parent device is created.
78  */
79 static void *
80 wlan_alloc(u_char type, struct ifnet *ifp)
81 {
82 	struct ieee80211com *ic;
83 
84 	ic = malloc(sizeof(struct ieee80211com), M_80211_COM, M_WAITOK|M_ZERO);
85 	ic->ic_ifp = ifp;
86 
87 	return (ic);
88 }
89 
90 static void
91 wlan_free(void *ic, u_char type)
92 {
93 	free(ic, M_80211_COM);
94 }
95 
96 static int
97 wlan_clone_create(struct if_clone *ifc, int unit, caddr_t params)
98 {
99 	struct ieee80211_clone_params cp;
100 	struct ieee80211vap *vap;
101 	struct ieee80211com *ic;
102 	struct ifnet *ifp;
103 	int error;
104 
105 	error = copyin(params, &cp, sizeof(cp));
106 	if (error)
107 		return error;
108 	ifp = ifunit(cp.icp_parent);
109 	if (ifp == NULL)
110 		return ENXIO;
111 	/* XXX move printfs to DIAGNOSTIC before release */
112 	if (ifp->if_type != IFT_IEEE80211) {
113 		if_printf(ifp, "%s: reject, not an 802.11 device\n", __func__);
114 		return ENXIO;
115 	}
116 	if (cp.icp_opmode >= IEEE80211_OPMODE_MAX) {
117 		if_printf(ifp, "%s: invalid opmode %d\n",
118 		    __func__, cp.icp_opmode);
119 		return EINVAL;
120 	}
121 	ic = ifp->if_l2com;
122 	if ((ic->ic_caps & ieee80211_opcap[cp.icp_opmode]) == 0) {
123 		if_printf(ifp, "%s mode not supported\n",
124 		    ieee80211_opmode_name[cp.icp_opmode]);
125 		return EOPNOTSUPP;
126 	}
127 	if ((cp.icp_flags & IEEE80211_CLONE_TDMA) &&
128 #ifdef IEEE80211_SUPPORT_TDMA
129 	    (ic->ic_caps & IEEE80211_C_TDMA) == 0
130 #else
131 	    (1)
132 #endif
133 	) {
134 		if_printf(ifp, "TDMA not supported\n");
135 		return EOPNOTSUPP;
136 	}
137 	vap = ic->ic_vap_create(ic, ifc->ifc_name, unit,
138 			cp.icp_opmode, cp.icp_flags, cp.icp_bssid,
139 			cp.icp_flags & IEEE80211_CLONE_MACADDR ?
140 			    cp.icp_macaddr : ic->ic_myaddr);
141 	return (vap == NULL ? EIO : 0);
142 }
143 
144 static void
145 wlan_clone_destroy(struct ifnet *ifp)
146 {
147 	struct ieee80211vap *vap = ifp->if_softc;
148 	struct ieee80211com *ic = vap->iv_ic;
149 
150 	ic->ic_vap_delete(vap);
151 }
152 IFC_SIMPLE_DECLARE(wlan, 0);
153 
154 void
155 ieee80211_vap_destroy(struct ieee80211vap *vap)
156 {
157 	if_clone_destroyif(&wlan_cloner, vap->iv_ifp);
158 }
159 
160 static int
161 ieee80211_sysctl_msecs_ticks(SYSCTL_HANDLER_ARGS)
162 {
163 	int msecs = ticks_to_msecs(*(int *)arg1);
164 	int error, t;
165 
166 	error = sysctl_handle_int(oidp, &msecs, 0, req);
167 	if (error || !req->newptr)
168 		return error;
169 	t = msecs_to_ticks(msecs);
170 	*(int *)arg1 = (t < 1) ? 1 : t;
171 	return 0;
172 }
173 
174 #ifdef IEEE80211_AMPDU_AGE
175 extern int ieee80211_ampdu_age;
176 SYSCTL_PROC(_net_wlan, OID_AUTO, ampdu_age, CTLFLAG_RW,
177 	&ieee80211_ampdu_age, 0, ieee80211_sysctl_msecs_ticks, "I",
178 	"AMPDU max reorder age (ms)");
179 #endif
180 extern int ieee80211_addba_timeout;
181 SYSCTL_PROC(_net_wlan, OID_AUTO, addba_timeout, CTLFLAG_RW,
182 	&ieee80211_addba_timeout, 0, ieee80211_sysctl_msecs_ticks, "I",
183 	"ADDBA request timeout (ms)");
184 extern int ieee80211_addba_backoff;
185 SYSCTL_PROC(_net_wlan, OID_AUTO, addba_backoff, CTLFLAG_RW,
186 	&ieee80211_addba_backoff, 0, ieee80211_sysctl_msecs_ticks, "I",
187 	"ADDBA request backoff (ms)");
188 extern int ieee80211_addba_maxtries;
189 SYSCTL_INT(_net_wlan, OID_AUTO, addba_maxtries, CTLFLAG_RW,
190 	&ieee80211_addba_maxtries, 0, "max ADDBA requests sent before backoff");
191 
192 static int
193 ieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS)
194 {
195 	int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT;
196 	int error;
197 
198 	error = sysctl_handle_int(oidp, &inact, 0, req);
199 	if (error || !req->newptr)
200 		return error;
201 	*(int *)arg1 = inact / IEEE80211_INACT_WAIT;
202 	return 0;
203 }
204 
205 static int
206 ieee80211_sysctl_parent(SYSCTL_HANDLER_ARGS)
207 {
208 	struct ieee80211com *ic = arg1;
209 	const char *name = ic->ic_ifp->if_xname;
210 
211 	return SYSCTL_OUT(req, name, strlen(name));
212 }
213 
214 static int
215 ieee80211_sysctl_radar(SYSCTL_HANDLER_ARGS)
216 {
217 	struct ieee80211com *ic = arg1;
218 	int t = 0, error;
219 
220 	error = sysctl_handle_int(oidp, &t, 0, req);
221 	if (error || !req->newptr)
222 		return error;
223 	IEEE80211_LOCK(ic);
224 	ieee80211_dfs_notify_radar(ic, ic->ic_curchan);
225 	IEEE80211_UNLOCK(ic);
226 	return 0;
227 }
228 
229 void
230 ieee80211_sysctl_attach(struct ieee80211com *ic)
231 {
232 }
233 
234 void
235 ieee80211_sysctl_detach(struct ieee80211com *ic)
236 {
237 }
238 
239 void
240 ieee80211_sysctl_vattach(struct ieee80211vap *vap)
241 {
242 	struct ifnet *ifp = vap->iv_ifp;
243 	struct sysctl_ctx_list *ctx;
244 	struct sysctl_oid *oid;
245 	char num[14];			/* sufficient for 32 bits */
246 
247 	ctx = (struct sysctl_ctx_list *) malloc(sizeof(struct sysctl_ctx_list),
248 		M_DEVBUF, M_NOWAIT | M_ZERO);
249 	if (ctx == NULL) {
250 		if_printf(ifp, "%s: cannot allocate sysctl context!\n",
251 			__func__);
252 		return;
253 	}
254 	sysctl_ctx_init(ctx);
255 	snprintf(num, sizeof(num), "%u", ifp->if_dunit);
256 	oid = SYSCTL_ADD_NODE(ctx, &SYSCTL_NODE_CHILDREN(_net, wlan),
257 		OID_AUTO, num, CTLFLAG_RD, NULL, "");
258 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
259 		"%parent", CTLFLAG_RD, vap->iv_ic, 0,
260 		ieee80211_sysctl_parent, "A", "parent device");
261 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
262 		"driver_caps", CTLFLAG_RW, &vap->iv_caps, 0,
263 		"driver capabilities");
264 #ifdef IEEE80211_DEBUG
265 	vap->iv_debug = ieee80211_debug;
266 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
267 		"debug", CTLFLAG_RW, &vap->iv_debug, 0,
268 		"control debugging printfs");
269 #endif
270 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
271 		"bmiss_max", CTLFLAG_RW, &vap->iv_bmiss_max, 0,
272 		"consecutive beacon misses before scanning");
273 	/* XXX inherit from tunables */
274 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
275 		"inact_run", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_run, 0,
276 		ieee80211_sysctl_inact, "I",
277 		"station inactivity timeout (sec)");
278 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
279 		"inact_probe", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_probe, 0,
280 		ieee80211_sysctl_inact, "I",
281 		"station inactivity probe timeout (sec)");
282 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
283 		"inact_auth", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_auth, 0,
284 		ieee80211_sysctl_inact, "I",
285 		"station authentication timeout (sec)");
286 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
287 		"inact_init", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_init, 0,
288 		ieee80211_sysctl_inact, "I",
289 		"station initial state timeout (sec)");
290 	if (vap->iv_htcaps & IEEE80211_HTC_HT) {
291 		SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
292 			"ampdu_mintraffic_bk", CTLFLAG_RW,
293 			&vap->iv_ampdu_mintraffic[WME_AC_BK], 0,
294 			"BK traffic tx aggr threshold (pps)");
295 		SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
296 			"ampdu_mintraffic_be", CTLFLAG_RW,
297 			&vap->iv_ampdu_mintraffic[WME_AC_BE], 0,
298 			"BE traffic tx aggr threshold (pps)");
299 		SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
300 			"ampdu_mintraffic_vo", CTLFLAG_RW,
301 			&vap->iv_ampdu_mintraffic[WME_AC_VO], 0,
302 			"VO traffic tx aggr threshold (pps)");
303 		SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
304 			"ampdu_mintraffic_vi", CTLFLAG_RW,
305 			&vap->iv_ampdu_mintraffic[WME_AC_VI], 0,
306 			"VI traffic tx aggr threshold (pps)");
307 	}
308 	if (vap->iv_caps & IEEE80211_C_DFS) {
309 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
310 			"radar", CTLTYPE_INT | CTLFLAG_RW, vap->iv_ic, 0,
311 			ieee80211_sysctl_radar, "I", "simulare radar event");
312 	}
313 	vap->iv_sysctl = ctx;
314 	vap->iv_oid = oid;
315 }
316 
317 void
318 ieee80211_sysctl_vdetach(struct ieee80211vap *vap)
319 {
320 
321 	if (vap->iv_sysctl != NULL) {
322 		sysctl_ctx_free(vap->iv_sysctl);
323 		free(vap->iv_sysctl, M_DEVBUF);
324 		vap->iv_sysctl = NULL;
325 	}
326 }
327 
328 int
329 ieee80211_node_dectestref(struct ieee80211_node *ni)
330 {
331 	/* XXX need equivalent of atomic_dec_and_test */
332 	atomic_subtract_int(&ni->ni_refcnt, 1);
333 	return atomic_cmpset_int(&ni->ni_refcnt, 0, 1);
334 }
335 
336 void
337 ieee80211_drain_ifq(struct ifqueue *ifq)
338 {
339 	struct ieee80211_node *ni;
340 	struct mbuf *m;
341 
342 	for (;;) {
343 		IF_DEQUEUE(ifq, m);
344 		if (m == NULL)
345 			break;
346 
347 		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
348 		KASSERT(ni != NULL, ("frame w/o node"));
349 		ieee80211_free_node(ni);
350 		m->m_pkthdr.rcvif = NULL;
351 
352 		m_freem(m);
353 	}
354 }
355 
356 void
357 ieee80211_flush_ifq(struct ifqueue *ifq, struct ieee80211vap *vap)
358 {
359 	struct ieee80211_node *ni;
360 	struct mbuf *m, **mprev;
361 
362 	IF_LOCK(ifq);
363 	mprev = &ifq->ifq_head;
364 	while ((m = *mprev) != NULL) {
365 		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
366 		if (ni != NULL && ni->ni_vap == vap) {
367 			*mprev = m->m_nextpkt;		/* remove from list */
368 			ifq->ifq_len--;
369 
370 			m_freem(m);
371 			ieee80211_free_node(ni);	/* reclaim ref */
372 		} else
373 			mprev = &m->m_nextpkt;
374 	}
375 	/* recalculate tail ptr */
376 	m = ifq->ifq_head;
377 	for (; m != NULL && m->m_nextpkt != NULL; m = m->m_nextpkt)
378 		;
379 	ifq->ifq_tail = m;
380 	IF_UNLOCK(ifq);
381 }
382 
383 /*
384  * As above, for mbufs allocated with m_gethdr/MGETHDR
385  * or initialized by M_COPY_PKTHDR.
386  */
387 #define	MC_ALIGN(m, len)						\
388 do {									\
389 	(m)->m_data += (MCLBYTES - (len)) &~ (sizeof(long) - 1);	\
390 } while (/* CONSTCOND */ 0)
391 
392 /*
393  * Allocate and setup a management frame of the specified
394  * size.  We return the mbuf and a pointer to the start
395  * of the contiguous data area that's been reserved based
396  * on the packet length.  The data area is forced to 32-bit
397  * alignment and the buffer length to a multiple of 4 bytes.
398  * This is done mainly so beacon frames (that require this)
399  * can use this interface too.
400  */
401 struct mbuf *
402 ieee80211_getmgtframe(uint8_t **frm, int headroom, int pktlen)
403 {
404 	struct mbuf *m;
405 	u_int len;
406 
407 	/*
408 	 * NB: we know the mbuf routines will align the data area
409 	 *     so we don't need to do anything special.
410 	 */
411 	len = roundup2(headroom + pktlen, 4);
412 	KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len));
413 	if (len < MINCLSIZE) {
414 		m = m_gethdr(M_NOWAIT, MT_DATA);
415 		/*
416 		 * Align the data in case additional headers are added.
417 		 * This should only happen when a WEP header is added
418 		 * which only happens for shared key authentication mgt
419 		 * frames which all fit in MHLEN.
420 		 */
421 		if (m != NULL)
422 			MH_ALIGN(m, len);
423 	} else {
424 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
425 		if (m != NULL)
426 			MC_ALIGN(m, len);
427 	}
428 	if (m != NULL) {
429 		m->m_data += headroom;
430 		*frm = m->m_data;
431 	}
432 	return m;
433 }
434 
435 int
436 ieee80211_add_callback(struct mbuf *m,
437 	void (*func)(struct ieee80211_node *, void *, int), void *arg)
438 {
439 	struct m_tag *mtag;
440 	struct ieee80211_cb *cb;
441 
442 	mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_CALLBACK,
443 			sizeof(struct ieee80211_cb), M_NOWAIT);
444 	if (mtag == NULL)
445 		return 0;
446 
447 	cb = (struct ieee80211_cb *)(mtag+1);
448 	cb->func = func;
449 	cb->arg = arg;
450 	m_tag_prepend(m, mtag);
451 	m->m_flags |= M_TXCB;
452 	return 1;
453 }
454 
455 void
456 ieee80211_process_callback(struct ieee80211_node *ni,
457 	struct mbuf *m, int status)
458 {
459 	struct m_tag *mtag;
460 
461 	mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_CALLBACK, NULL);
462 	if (mtag != NULL) {
463 		struct ieee80211_cb *cb = (struct ieee80211_cb *)(mtag+1);
464 		cb->func(ni, cb->arg, status);
465 	}
466 }
467 
468 #include <sys/libkern.h>
469 
470 void
471 get_random_bytes(void *p, size_t n)
472 {
473 	uint8_t *dp = p;
474 
475 	while (n > 0) {
476 		uint32_t v = arc4random();
477 		size_t nb = n > sizeof(uint32_t) ? sizeof(uint32_t) : n;
478 		bcopy(&v, dp, n > sizeof(uint32_t) ? sizeof(uint32_t) : n);
479 		dp += sizeof(uint32_t), n -= nb;
480 	}
481 }
482 
483 /*
484  * Helper function for events that pass just a single mac address.
485  */
486 static void
487 notify_macaddr(struct ifnet *ifp, int op, const uint8_t mac[IEEE80211_ADDR_LEN])
488 {
489 	struct ieee80211_join_event iev;
490 
491 	memset(&iev, 0, sizeof(iev));
492 	IEEE80211_ADDR_COPY(iev.iev_addr, mac);
493 	rt_ieee80211msg(ifp, op, &iev, sizeof(iev));
494 }
495 
496 void
497 ieee80211_notify_node_join(struct ieee80211_node *ni, int newassoc)
498 {
499 	struct ieee80211vap *vap = ni->ni_vap;
500 	struct ifnet *ifp = vap->iv_ifp;
501 
502 	IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode join",
503 	    (ni == vap->iv_bss) ? "bss " : "");
504 
505 	if (ni == vap->iv_bss) {
506 		notify_macaddr(ifp, newassoc ?
507 		    RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC, ni->ni_bssid);
508 		if_link_state_change(ifp, LINK_STATE_UP);
509 	} else {
510 		notify_macaddr(ifp, newassoc ?
511 		    RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN, ni->ni_macaddr);
512 	}
513 }
514 
515 void
516 ieee80211_notify_node_leave(struct ieee80211_node *ni)
517 {
518 	struct ieee80211vap *vap = ni->ni_vap;
519 	struct ifnet *ifp = vap->iv_ifp;
520 
521 	IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode leave",
522 	    (ni == vap->iv_bss) ? "bss " : "");
523 
524 	if (ni == vap->iv_bss) {
525 		rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
526 		if_link_state_change(ifp, LINK_STATE_DOWN);
527 	} else {
528 		/* fire off wireless event station leaving */
529 		notify_macaddr(ifp, RTM_IEEE80211_LEAVE, ni->ni_macaddr);
530 	}
531 }
532 
533 void
534 ieee80211_notify_scan_done(struct ieee80211vap *vap)
535 {
536 	struct ifnet *ifp = vap->iv_ifp;
537 
538 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n", "notify scan done");
539 
540 	/* dispatch wireless event indicating scan completed */
541 	rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
542 }
543 
544 void
545 ieee80211_notify_replay_failure(struct ieee80211vap *vap,
546 	const struct ieee80211_frame *wh, const struct ieee80211_key *k,
547 	u_int64_t rsc)
548 {
549 	struct ifnet *ifp = vap->iv_ifp;
550 
551 	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
552 	    "%s replay detected <rsc %ju, csc %ju, keyix %u rxkeyix %u>",
553 	    k->wk_cipher->ic_name, (intmax_t) rsc,
554 	    (intmax_t) k->wk_keyrsc[IEEE80211_NONQOS_TID],
555 	    k->wk_keyix, k->wk_rxkeyix);
556 
557 	if (ifp != NULL) {		/* NB: for cipher test modules */
558 		struct ieee80211_replay_event iev;
559 
560 		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
561 		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
562 		iev.iev_cipher = k->wk_cipher->ic_cipher;
563 		if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE)
564 			iev.iev_keyix = k->wk_rxkeyix;
565 		else
566 			iev.iev_keyix = k->wk_keyix;
567 		iev.iev_keyrsc = k->wk_keyrsc[0];	/* XXX need tid */
568 		iev.iev_rsc = rsc;
569 		rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
570 	}
571 }
572 
573 void
574 ieee80211_notify_michael_failure(struct ieee80211vap *vap,
575 	const struct ieee80211_frame *wh, u_int keyix)
576 {
577 	struct ifnet *ifp = vap->iv_ifp;
578 
579 	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
580 	    "michael MIC verification failed <keyix %u>", keyix);
581 	vap->iv_stats.is_rx_tkipmic++;
582 
583 	if (ifp != NULL) {		/* NB: for cipher test modules */
584 		struct ieee80211_michael_event iev;
585 
586 		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
587 		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
588 		iev.iev_cipher = IEEE80211_CIPHER_TKIP;
589 		iev.iev_keyix = keyix;
590 		rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev));
591 	}
592 }
593 
594 void
595 ieee80211_notify_wds_discover(struct ieee80211_node *ni)
596 {
597 	struct ieee80211vap *vap = ni->ni_vap;
598 	struct ifnet *ifp = vap->iv_ifp;
599 
600 	notify_macaddr(ifp, RTM_IEEE80211_WDS, ni->ni_macaddr);
601 }
602 
603 void
604 ieee80211_notify_csa(struct ieee80211com *ic,
605 	const struct ieee80211_channel *c, int mode, int count)
606 {
607 	struct ifnet *ifp = ic->ic_ifp;
608 	struct ieee80211_csa_event iev;
609 
610 	memset(&iev, 0, sizeof(iev));
611 	iev.iev_flags = c->ic_flags;
612 	iev.iev_freq = c->ic_freq;
613 	iev.iev_ieee = c->ic_ieee;
614 	iev.iev_mode = mode;
615 	iev.iev_count = count;
616 	rt_ieee80211msg(ifp, RTM_IEEE80211_CSA, &iev, sizeof(iev));
617 }
618 
619 void
620 ieee80211_notify_radar(struct ieee80211com *ic,
621 	const struct ieee80211_channel *c)
622 {
623 	struct ifnet *ifp = ic->ic_ifp;
624 	struct ieee80211_radar_event iev;
625 
626 	memset(&iev, 0, sizeof(iev));
627 	iev.iev_flags = c->ic_flags;
628 	iev.iev_freq = c->ic_freq;
629 	iev.iev_ieee = c->ic_ieee;
630 	rt_ieee80211msg(ifp, RTM_IEEE80211_RADAR, &iev, sizeof(iev));
631 }
632 
633 void
634 ieee80211_notify_cac(struct ieee80211com *ic,
635 	const struct ieee80211_channel *c, enum ieee80211_notify_cac_event type)
636 {
637 	struct ifnet *ifp = ic->ic_ifp;
638 	struct ieee80211_cac_event iev;
639 
640 	memset(&iev, 0, sizeof(iev));
641 	iev.iev_flags = c->ic_flags;
642 	iev.iev_freq = c->ic_freq;
643 	iev.iev_ieee = c->ic_ieee;
644 	iev.iev_type = type;
645 	rt_ieee80211msg(ifp, RTM_IEEE80211_CAC, &iev, sizeof(iev));
646 }
647 
648 void
649 ieee80211_notify_node_deauth(struct ieee80211_node *ni)
650 {
651 	struct ieee80211vap *vap = ni->ni_vap;
652 	struct ifnet *ifp = vap->iv_ifp;
653 
654 	IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node deauth");
655 
656 	notify_macaddr(ifp, RTM_IEEE80211_DEAUTH, ni->ni_macaddr);
657 }
658 
659 void
660 ieee80211_notify_node_auth(struct ieee80211_node *ni)
661 {
662 	struct ieee80211vap *vap = ni->ni_vap;
663 	struct ifnet *ifp = vap->iv_ifp;
664 
665 	IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node auth");
666 
667 	notify_macaddr(ifp, RTM_IEEE80211_AUTH, ni->ni_macaddr);
668 }
669 
670 void
671 ieee80211_notify_country(struct ieee80211vap *vap,
672 	const uint8_t bssid[IEEE80211_ADDR_LEN], const uint8_t cc[2])
673 {
674 	struct ifnet *ifp = vap->iv_ifp;
675 	struct ieee80211_country_event iev;
676 
677 	memset(&iev, 0, sizeof(iev));
678 	IEEE80211_ADDR_COPY(iev.iev_addr, bssid);
679 	iev.iev_cc[0] = cc[0];
680 	iev.iev_cc[1] = cc[1];
681 	rt_ieee80211msg(ifp, RTM_IEEE80211_COUNTRY, &iev, sizeof(iev));
682 }
683 
684 void
685 ieee80211_notify_radio(struct ieee80211com *ic, int state)
686 {
687 	struct ifnet *ifp = ic->ic_ifp;
688 	struct ieee80211_radio_event iev;
689 
690 	memset(&iev, 0, sizeof(iev));
691 	iev.iev_state = state;
692 	rt_ieee80211msg(ifp, RTM_IEEE80211_RADIO, &iev, sizeof(iev));
693 }
694 
695 void
696 ieee80211_load_module(const char *modname)
697 {
698 
699 #ifdef notyet
700 	(void)kern_kldload(curthread, modname, NULL);
701 #else
702 	printf("%s: load the %s module by hand for now.\n", __func__, modname);
703 #endif
704 }
705 
706 /*
707  * Module glue.
708  *
709  * NB: the module name is "wlan" for compatibility with NetBSD.
710  */
711 static int
712 wlan_modevent(module_t mod, int type, void *unused)
713 {
714 	switch (type) {
715 	case MOD_LOAD:
716 		if (bootverbose)
717 			printf("wlan: <802.11 Link Layer>\n");
718 		if_clone_attach(&wlan_cloner);
719 		if_register_com_alloc(IFT_IEEE80211, wlan_alloc, wlan_free);
720 		return 0;
721 	case MOD_UNLOAD:
722 		if_deregister_com_alloc(IFT_IEEE80211);
723 		if_clone_detach(&wlan_cloner);
724 		return 0;
725 	}
726 	return EINVAL;
727 }
728 
729 static moduledata_t wlan_mod = {
730 	"wlan",
731 	wlan_modevent,
732 	0
733 };
734 DECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
735 MODULE_VERSION(wlan, 1);
736 MODULE_DEPEND(wlan, ether, 1, 1, 1);
737