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