ieee80211_freebsd.c revision 159590
1138568Ssam/*-
2139530Ssam * Copyright (c) 2003-2005 Sam Leffler, Errno Consulting
3138568Ssam * All rights reserved.
4138568Ssam *
5138568Ssam * Redistribution and use in source and binary forms, with or without
6138568Ssam * modification, are permitted provided that the following conditions
7138568Ssam * are met:
8138568Ssam * 1. Redistributions of source code must retain the above copyright
9138568Ssam *    notice, this list of conditions and the following disclaimer.
10138568Ssam * 2. Redistributions in binary form must reproduce the above copyright
11138568Ssam *    notice, this list of conditions and the following disclaimer in the
12138568Ssam *    documentation and/or other materials provided with the distribution.
13138568Ssam * 3. The name of the author may not be used to endorse or promote products
14138568Ssam *    derived from this software without specific prior written permission.
15138568Ssam *
16138568Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17138568Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18138568Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19138568Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20138568Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21138568Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22138568Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23138568Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24138568Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25138568Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26138568Ssam */
27138568Ssam
28138568Ssam#include <sys/cdefs.h>
29138568Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_freebsd.c 159590 2006-06-13 21:36:23Z jhb $");
30138568Ssam
31138568Ssam/*
32138568Ssam * IEEE 802.11 support (FreeBSD-specific code)
33138568Ssam */
34138568Ssam#include <sys/param.h>
35138568Ssam#include <sys/kernel.h>
36138568Ssam#include <sys/systm.h>
37138568Ssam#include <sys/linker.h>
38138568Ssam#include <sys/mbuf.h>
39138568Ssam#include <sys/module.h>
40138568Ssam#include <sys/proc.h>
41138568Ssam#include <sys/sysctl.h>
42138568Ssam
43138568Ssam#include <sys/socket.h>
44138568Ssam
45138568Ssam#include <net/if.h>
46138568Ssam#include <net/if_media.h>
47138568Ssam#include <net/ethernet.h>
48138568Ssam#include <net/route.h>
49138568Ssam
50138568Ssam#include <net80211/ieee80211_var.h>
51138568Ssam
52138568SsamSYSCTL_NODE(_net, OID_AUTO, wlan, CTLFLAG_RD, 0, "IEEE 80211 parameters");
53138568Ssam
54138568Ssam#ifdef IEEE80211_DEBUG
55138568Ssamint	ieee80211_debug = 0;
56138568SsamSYSCTL_INT(_net_wlan, OID_AUTO, debug, CTLFLAG_RW, &ieee80211_debug,
57138568Ssam	    0, "debugging printfs");
58138568Ssam#endif
59138568Ssam
60138568Ssamstatic int
61138568Ssamieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS)
62138568Ssam{
63138568Ssam	int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT;
64138568Ssam	int error;
65138568Ssam
66138568Ssam	error = sysctl_handle_int(oidp, &inact, 0, req);
67138568Ssam	if (error || !req->newptr)
68138568Ssam		return error;
69138568Ssam	*(int *)arg1 = inact / IEEE80211_INACT_WAIT;
70138568Ssam	return 0;
71138568Ssam}
72138568Ssam
73138568Ssamstatic int
74138568Ssamieee80211_sysctl_parent(SYSCTL_HANDLER_ARGS)
75138568Ssam{
76138568Ssam	struct ieee80211com *ic = arg1;
77138568Ssam	const char *name = ic->ic_ifp->if_xname;
78138568Ssam
79138568Ssam	return SYSCTL_OUT(req, name, strlen(name));
80138568Ssam}
81138568Ssam
82138568Ssamvoid
83138568Ssamieee80211_sysctl_attach(struct ieee80211com *ic)
84138568Ssam{
85138568Ssam	struct sysctl_ctx_list *ctx;
86138568Ssam	struct sysctl_oid *oid;
87138568Ssam	char num[14];			/* sufficient for 32 bits */
88138568Ssam
89138568Ssam	MALLOC(ctx, struct sysctl_ctx_list *, sizeof(struct sysctl_ctx_list),
90138568Ssam		M_DEVBUF, M_NOWAIT | M_ZERO);
91138568Ssam	if (ctx == NULL) {
92138568Ssam		if_printf(ic->ic_ifp, "%s: cannot allocate sysctl context!\n",
93138568Ssam			__func__);
94138568Ssam		return;
95138568Ssam	}
96138568Ssam	sysctl_ctx_init(ctx);
97138568Ssam	snprintf(num, sizeof(num), "%u", ic->ic_vap);
98138568Ssam	oid = SYSCTL_ADD_NODE(ctx, &SYSCTL_NODE_CHILDREN(_net, wlan),
99138568Ssam		OID_AUTO, num, CTLFLAG_RD, NULL, "");
100138568Ssam	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
101138568Ssam		"%parent", CTLFLAG_RD, ic, 0, ieee80211_sysctl_parent, "A",
102138568Ssam		"parent device");
103138568Ssam#ifdef IEEE80211_DEBUG
104138568Ssam	ic->ic_debug = ieee80211_debug;
105138568Ssam	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
106138568Ssam		"debug", CTLFLAG_RW, &ic->ic_debug, 0,
107138568Ssam		"control debugging printfs");
108138568Ssam#endif
109138568Ssam	/* XXX inherit from tunables */
110138568Ssam	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
111140633Ssam		"inact_run", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_run, 0,
112138568Ssam		ieee80211_sysctl_inact, "I",
113138568Ssam		"station inactivity timeout (sec)");
114138568Ssam	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
115138568Ssam		"inact_probe", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_probe, 0,
116138568Ssam		ieee80211_sysctl_inact, "I",
117138568Ssam		"station inactivity probe timeout (sec)");
118138568Ssam	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
119138568Ssam		"inact_auth", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_auth, 0,
120138568Ssam		ieee80211_sysctl_inact, "I",
121138568Ssam		"station authentication timeout (sec)");
122138568Ssam	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
123138568Ssam		"inact_init", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_init, 0,
124138568Ssam		ieee80211_sysctl_inact, "I",
125138568Ssam		"station initial state timeout (sec)");
126139512Ssam	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
127139512Ssam		"driver_caps", CTLFLAG_RW, &ic->ic_caps, 0,
128139512Ssam		"driver capabilities");
129153349Ssam	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
130153349Ssam		"bmiss_max", CTLFLAG_RW, &ic->ic_bmiss_max, 0,
131153349Ssam		"consecutive beacon misses before scanning");
132138568Ssam	ic->ic_sysctl = ctx;
133138568Ssam}
134138568Ssam
135138568Ssamvoid
136138568Ssamieee80211_sysctl_detach(struct ieee80211com *ic)
137138568Ssam{
138138568Ssam
139138568Ssam	if (ic->ic_sysctl != NULL) {
140138568Ssam		sysctl_ctx_free(ic->ic_sysctl);
141138568Ssam		ic->ic_sysctl = NULL;
142138568Ssam	}
143138568Ssam}
144138568Ssam
145138568Ssamint
146138568Ssamieee80211_node_dectestref(struct ieee80211_node *ni)
147138568Ssam{
148138568Ssam	/* XXX need equivalent of atomic_dec_and_test */
149138568Ssam	atomic_subtract_int(&ni->ni_refcnt, 1);
150138568Ssam	return atomic_cmpset_int(&ni->ni_refcnt, 0, 1);
151138568Ssam}
152138568Ssam
153138568Ssam/*
154138568Ssam * Allocate and setup a management frame of the specified
155138568Ssam * size.  We return the mbuf and a pointer to the start
156138568Ssam * of the contiguous data area that's been reserved based
157138568Ssam * on the packet length.  The data area is forced to 32-bit
158138568Ssam * alignment and the buffer length to a multiple of 4 bytes.
159138568Ssam * This is done mainly so beacon frames (that require this)
160138568Ssam * can use this interface too.
161138568Ssam */
162138568Ssamstruct mbuf *
163138568Ssamieee80211_getmgtframe(u_int8_t **frm, u_int pktlen)
164138568Ssam{
165138568Ssam	struct mbuf *m;
166138568Ssam	u_int len;
167138568Ssam
168138568Ssam	/*
169138568Ssam	 * NB: we know the mbuf routines will align the data area
170138568Ssam	 *     so we don't need to do anything special.
171138568Ssam	 */
172138568Ssam	/* XXX 4-address frame? */
173138568Ssam	len = roundup(sizeof(struct ieee80211_frame) + pktlen, 4);
174138568Ssam	KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len));
175138568Ssam	if (len < MINCLSIZE) {
176151967Sandre		m = m_gethdr(M_NOWAIT, MT_DATA);
177138568Ssam		/*
178138568Ssam		 * Align the data in case additional headers are added.
179138568Ssam		 * This should only happen when a WEP header is added
180138568Ssam		 * which only happens for shared key authentication mgt
181138568Ssam		 * frames which all fit in MHLEN.
182138568Ssam		 */
183138568Ssam		if (m != NULL)
184138568Ssam			MH_ALIGN(m, len);
185138568Ssam	} else
186151967Sandre		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
187138568Ssam	if (m != NULL) {
188138568Ssam		m->m_data += sizeof(struct ieee80211_frame);
189138568Ssam		*frm = m->m_data;
190138568Ssam	}
191138568Ssam	return m;
192138568Ssam}
193138568Ssam
194138568Ssam#include <sys/libkern.h>
195138568Ssam
196138568Ssamvoid
197138568Ssamget_random_bytes(void *p, size_t n)
198138568Ssam{
199138568Ssam	u_int8_t *dp = p;
200138568Ssam
201138568Ssam	while (n > 0) {
202138568Ssam		u_int32_t v = arc4random();
203138568Ssam		size_t nb = n > sizeof(u_int32_t) ? sizeof(u_int32_t) : n;
204138568Ssam		bcopy(&v, dp, n > sizeof(u_int32_t) ? sizeof(u_int32_t) : n);
205138568Ssam		dp += sizeof(u_int32_t), n -= nb;
206138568Ssam	}
207138568Ssam}
208138568Ssam
209138568Ssamvoid
210138568Ssamieee80211_notify_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int newassoc)
211138568Ssam{
212138568Ssam	struct ifnet *ifp = ic->ic_ifp;
213138568Ssam	struct ieee80211_join_event iev;
214138568Ssam
215144302Ssam	memset(&iev, 0, sizeof(iev));
216138568Ssam	if (ni == ic->ic_bss) {
217138568Ssam		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_bssid);
218138568Ssam		rt_ieee80211msg(ifp, newassoc ?
219138568Ssam			RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC,
220138568Ssam			&iev, sizeof(iev));
221138568Ssam		if_link_state_change(ifp, LINK_STATE_UP);
222144302Ssam	} else {
223138568Ssam		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
224144302Ssam		rt_ieee80211msg(ifp, newassoc ?
225144302Ssam			RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN,
226144302Ssam			&iev, sizeof(iev));
227138568Ssam	}
228138568Ssam}
229138568Ssam
230138568Ssamvoid
231138568Ssamieee80211_notify_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
232138568Ssam{
233138568Ssam	struct ifnet *ifp = ic->ic_ifp;
234138568Ssam	struct ieee80211_leave_event iev;
235138568Ssam
236138568Ssam	if (ni == ic->ic_bss) {
237138568Ssam		rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
238138568Ssam		if_link_state_change(ifp, LINK_STATE_DOWN);
239138568Ssam	} else {
240138568Ssam		/* fire off wireless event station leaving */
241138568Ssam		memset(&iev, 0, sizeof(iev));
242138568Ssam		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
243138568Ssam		rt_ieee80211msg(ifp, RTM_IEEE80211_LEAVE, &iev, sizeof(iev));
244138568Ssam	}
245138568Ssam}
246138568Ssam
247138568Ssamvoid
248138568Ssamieee80211_notify_scan_done(struct ieee80211com *ic)
249138568Ssam{
250138568Ssam	struct ifnet *ifp = ic->ic_ifp;
251138568Ssam
252138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
253138568Ssam		"%s: notify scan done\n", ic->ic_ifp->if_xname);
254138568Ssam
255138568Ssam	/* dispatch wireless event indicating scan completed */
256138568Ssam	rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
257138568Ssam}
258138568Ssam
259138568Ssamvoid
260138568Ssamieee80211_notify_replay_failure(struct ieee80211com *ic,
261138568Ssam	const struct ieee80211_frame *wh, const struct ieee80211_key *k,
262138568Ssam	u_int64_t rsc)
263138568Ssam{
264138568Ssam	struct ifnet *ifp = ic->ic_ifp;
265138568Ssam
266138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
267148863Ssam	    "[%s] %s replay detected <rsc %ju, csc %ju, keyix %u rxkeyix %u>\n",
268148863Ssam	    ether_sprintf(wh->i_addr2), k->wk_cipher->ic_name,
269148863Ssam	    (intmax_t) rsc, (intmax_t) k->wk_keyrsc,
270148863Ssam	    k->wk_keyix, k->wk_rxkeyix);
271138568Ssam
272138568Ssam	if (ifp != NULL) {		/* NB: for cipher test modules */
273138568Ssam		struct ieee80211_replay_event iev;
274138568Ssam
275138568Ssam		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
276138568Ssam		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
277138568Ssam		iev.iev_cipher = k->wk_cipher->ic_cipher;
278148863Ssam		if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE)
279148863Ssam			iev.iev_keyix = k->wk_rxkeyix;
280148863Ssam		else
281148863Ssam			iev.iev_keyix = k->wk_keyix;
282138568Ssam		iev.iev_keyrsc = k->wk_keyrsc;
283138568Ssam		iev.iev_rsc = rsc;
284138568Ssam		rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
285138568Ssam	}
286138568Ssam}
287138568Ssam
288138568Ssamvoid
289138568Ssamieee80211_notify_michael_failure(struct ieee80211com *ic,
290138568Ssam	const struct ieee80211_frame *wh, u_int keyix)
291138568Ssam{
292138568Ssam	struct ifnet *ifp = ic->ic_ifp;
293138568Ssam
294138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
295139512Ssam		"[%s] michael MIC verification failed <keyix %u>\n",
296138568Ssam	       ether_sprintf(wh->i_addr2), keyix);
297138568Ssam	ic->ic_stats.is_rx_tkipmic++;
298138568Ssam
299138568Ssam	if (ifp != NULL) {		/* NB: for cipher test modules */
300138568Ssam		struct ieee80211_michael_event iev;
301138568Ssam
302138568Ssam		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
303138568Ssam		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
304138568Ssam		iev.iev_cipher = IEEE80211_CIPHER_TKIP;
305138568Ssam		iev.iev_keyix = keyix;
306138568Ssam		rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev));
307138568Ssam	}
308138568Ssam}
309138568Ssam
310138568Ssamvoid
311138568Ssamieee80211_load_module(const char *modname)
312138568Ssam{
313159590Sjhb
314138777Ssam#ifdef notyet
315159590Sjhb	(void)kern_kldload(curthread, modname, NULL);
316138777Ssam#else
317138777Ssam	printf("%s: load the %s module by hand for now.\n", __func__, modname);
318138777Ssam#endif
319138568Ssam}
320138568Ssam
321138568Ssam/*
322138568Ssam * Module glue.
323138568Ssam *
324138568Ssam * NB: the module name is "wlan" for compatibility with NetBSD.
325138568Ssam */
326138568Ssamstatic int
327138568Ssamwlan_modevent(module_t mod, int type, void *unused)
328138568Ssam{
329138568Ssam	switch (type) {
330138568Ssam	case MOD_LOAD:
331138568Ssam		if (bootverbose)
332138568Ssam			printf("wlan: <802.11 Link Layer>\n");
333138568Ssam		return 0;
334138568Ssam	case MOD_UNLOAD:
335138568Ssam		return 0;
336138568Ssam	}
337138568Ssam	return EINVAL;
338138568Ssam}
339138568Ssam
340138568Ssamstatic moduledata_t wlan_mod = {
341138568Ssam	"wlan",
342138568Ssam	wlan_modevent,
343138568Ssam	0
344138568Ssam};
345138568SsamDECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
346138568SsamMODULE_VERSION(wlan, 1);
347138568SsamMODULE_DEPEND(wlan, ether, 1, 1, 1);
348