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