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