ieee80211_freebsd.c revision 165894
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 165894 2007-01-08 18:23:43Z sam $");
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
153165894Ssamvoid
154165894Ssamieee80211_drain_ifq(struct ifqueue *ifq)
155165894Ssam{
156165894Ssam	struct ieee80211_node *ni;
157165894Ssam	struct mbuf *m;
158165894Ssam
159165894Ssam	for (;;) {
160165894Ssam		IF_DEQUEUE(ifq, m);
161165894Ssam		if (m == NULL)
162165894Ssam			break;
163165894Ssam
164165894Ssam		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
165165894Ssam		KASSERT(ni != NULL, ("frame w/o node"));
166165894Ssam		ieee80211_free_node(ni);
167165894Ssam		m->m_pkthdr.rcvif = NULL;
168165894Ssam
169165894Ssam		m_freem(m);
170165894Ssam	}
171165894Ssam}
172165894Ssam
173138568Ssam/*
174138568Ssam * Allocate and setup a management frame of the specified
175138568Ssam * size.  We return the mbuf and a pointer to the start
176138568Ssam * of the contiguous data area that's been reserved based
177138568Ssam * on the packet length.  The data area is forced to 32-bit
178138568Ssam * alignment and the buffer length to a multiple of 4 bytes.
179138568Ssam * This is done mainly so beacon frames (that require this)
180138568Ssam * can use this interface too.
181138568Ssam */
182138568Ssamstruct mbuf *
183138568Ssamieee80211_getmgtframe(u_int8_t **frm, u_int pktlen)
184138568Ssam{
185138568Ssam	struct mbuf *m;
186138568Ssam	u_int len;
187138568Ssam
188138568Ssam	/*
189138568Ssam	 * NB: we know the mbuf routines will align the data area
190138568Ssam	 *     so we don't need to do anything special.
191138568Ssam	 */
192138568Ssam	/* XXX 4-address frame? */
193138568Ssam	len = roundup(sizeof(struct ieee80211_frame) + pktlen, 4);
194138568Ssam	KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len));
195138568Ssam	if (len < MINCLSIZE) {
196151967Sandre		m = m_gethdr(M_NOWAIT, MT_DATA);
197138568Ssam		/*
198138568Ssam		 * Align the data in case additional headers are added.
199138568Ssam		 * This should only happen when a WEP header is added
200138568Ssam		 * which only happens for shared key authentication mgt
201138568Ssam		 * frames which all fit in MHLEN.
202138568Ssam		 */
203138568Ssam		if (m != NULL)
204138568Ssam			MH_ALIGN(m, len);
205138568Ssam	} else
206151967Sandre		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
207138568Ssam	if (m != NULL) {
208138568Ssam		m->m_data += sizeof(struct ieee80211_frame);
209138568Ssam		*frm = m->m_data;
210138568Ssam	}
211138568Ssam	return m;
212138568Ssam}
213138568Ssam
214138568Ssam#include <sys/libkern.h>
215138568Ssam
216138568Ssamvoid
217138568Ssamget_random_bytes(void *p, size_t n)
218138568Ssam{
219138568Ssam	u_int8_t *dp = p;
220138568Ssam
221138568Ssam	while (n > 0) {
222138568Ssam		u_int32_t v = arc4random();
223138568Ssam		size_t nb = n > sizeof(u_int32_t) ? sizeof(u_int32_t) : n;
224138568Ssam		bcopy(&v, dp, n > sizeof(u_int32_t) ? sizeof(u_int32_t) : n);
225138568Ssam		dp += sizeof(u_int32_t), n -= nb;
226138568Ssam	}
227138568Ssam}
228138568Ssam
229138568Ssamvoid
230138568Ssamieee80211_notify_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int newassoc)
231138568Ssam{
232138568Ssam	struct ifnet *ifp = ic->ic_ifp;
233138568Ssam	struct ieee80211_join_event iev;
234138568Ssam
235144302Ssam	memset(&iev, 0, sizeof(iev));
236138568Ssam	if (ni == ic->ic_bss) {
237138568Ssam		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_bssid);
238138568Ssam		rt_ieee80211msg(ifp, newassoc ?
239138568Ssam			RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC,
240138568Ssam			&iev, sizeof(iev));
241138568Ssam		if_link_state_change(ifp, LINK_STATE_UP);
242144302Ssam	} else {
243138568Ssam		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
244144302Ssam		rt_ieee80211msg(ifp, newassoc ?
245144302Ssam			RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN,
246144302Ssam			&iev, sizeof(iev));
247138568Ssam	}
248138568Ssam}
249138568Ssam
250138568Ssamvoid
251138568Ssamieee80211_notify_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
252138568Ssam{
253138568Ssam	struct ifnet *ifp = ic->ic_ifp;
254138568Ssam	struct ieee80211_leave_event iev;
255138568Ssam
256138568Ssam	if (ni == ic->ic_bss) {
257138568Ssam		rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
258138568Ssam		if_link_state_change(ifp, LINK_STATE_DOWN);
259138568Ssam	} else {
260138568Ssam		/* fire off wireless event station leaving */
261138568Ssam		memset(&iev, 0, sizeof(iev));
262138568Ssam		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
263138568Ssam		rt_ieee80211msg(ifp, RTM_IEEE80211_LEAVE, &iev, sizeof(iev));
264138568Ssam	}
265138568Ssam}
266138568Ssam
267138568Ssamvoid
268138568Ssamieee80211_notify_scan_done(struct ieee80211com *ic)
269138568Ssam{
270138568Ssam	struct ifnet *ifp = ic->ic_ifp;
271138568Ssam
272159915Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s\n", "notify scan done");
273138568Ssam
274138568Ssam	/* dispatch wireless event indicating scan completed */
275138568Ssam	rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
276138568Ssam}
277138568Ssam
278138568Ssamvoid
279138568Ssamieee80211_notify_replay_failure(struct ieee80211com *ic,
280138568Ssam	const struct ieee80211_frame *wh, const struct ieee80211_key *k,
281138568Ssam	u_int64_t rsc)
282138568Ssam{
283138568Ssam	struct ifnet *ifp = ic->ic_ifp;
284138568Ssam
285138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
286148863Ssam	    "[%s] %s replay detected <rsc %ju, csc %ju, keyix %u rxkeyix %u>\n",
287148863Ssam	    ether_sprintf(wh->i_addr2), k->wk_cipher->ic_name,
288148863Ssam	    (intmax_t) rsc, (intmax_t) k->wk_keyrsc,
289148863Ssam	    k->wk_keyix, k->wk_rxkeyix);
290138568Ssam
291138568Ssam	if (ifp != NULL) {		/* NB: for cipher test modules */
292138568Ssam		struct ieee80211_replay_event iev;
293138568Ssam
294138568Ssam		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
295138568Ssam		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
296138568Ssam		iev.iev_cipher = k->wk_cipher->ic_cipher;
297148863Ssam		if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE)
298148863Ssam			iev.iev_keyix = k->wk_rxkeyix;
299148863Ssam		else
300148863Ssam			iev.iev_keyix = k->wk_keyix;
301138568Ssam		iev.iev_keyrsc = k->wk_keyrsc;
302138568Ssam		iev.iev_rsc = rsc;
303138568Ssam		rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
304138568Ssam	}
305138568Ssam}
306138568Ssam
307138568Ssamvoid
308138568Ssamieee80211_notify_michael_failure(struct ieee80211com *ic,
309138568Ssam	const struct ieee80211_frame *wh, u_int keyix)
310138568Ssam{
311138568Ssam	struct ifnet *ifp = ic->ic_ifp;
312138568Ssam
313138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
314139512Ssam		"[%s] michael MIC verification failed <keyix %u>\n",
315138568Ssam	       ether_sprintf(wh->i_addr2), keyix);
316138568Ssam	ic->ic_stats.is_rx_tkipmic++;
317138568Ssam
318138568Ssam	if (ifp != NULL) {		/* NB: for cipher test modules */
319138568Ssam		struct ieee80211_michael_event iev;
320138568Ssam
321138568Ssam		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
322138568Ssam		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
323138568Ssam		iev.iev_cipher = IEEE80211_CIPHER_TKIP;
324138568Ssam		iev.iev_keyix = keyix;
325138568Ssam		rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev));
326138568Ssam	}
327138568Ssam}
328138568Ssam
329138568Ssamvoid
330138568Ssamieee80211_load_module(const char *modname)
331138568Ssam{
332159590Sjhb
333138777Ssam#ifdef notyet
334159590Sjhb	(void)kern_kldload(curthread, modname, NULL);
335138777Ssam#else
336138777Ssam	printf("%s: load the %s module by hand for now.\n", __func__, modname);
337138777Ssam#endif
338138568Ssam}
339138568Ssam
340138568Ssam/*
341138568Ssam * Module glue.
342138568Ssam *
343138568Ssam * NB: the module name is "wlan" for compatibility with NetBSD.
344138568Ssam */
345138568Ssamstatic int
346138568Ssamwlan_modevent(module_t mod, int type, void *unused)
347138568Ssam{
348138568Ssam	switch (type) {
349138568Ssam	case MOD_LOAD:
350138568Ssam		if (bootverbose)
351138568Ssam			printf("wlan: <802.11 Link Layer>\n");
352138568Ssam		return 0;
353138568Ssam	case MOD_UNLOAD:
354138568Ssam		return 0;
355138568Ssam	}
356138568Ssam	return EINVAL;
357138568Ssam}
358138568Ssam
359138568Ssamstatic moduledata_t wlan_mod = {
360138568Ssam	"wlan",
361138568Ssam	wlan_modevent,
362138568Ssam	0
363138568Ssam};
364138568SsamDECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
365138568SsamMODULE_VERSION(wlan, 1);
366138568SsamMODULE_DEPEND(wlan, ether, 1, 1, 1);
367