ieee80211_freebsd.c revision 138568
1138568Ssam/*-
2138568Ssam * Copyright (c) 2003-2004 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 138568 2004-12-08 17:26:47Z 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,
111138568Ssam		"inact", 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)");
126138568Ssam	ic->ic_sysctl = ctx;
127138568Ssam}
128138568Ssam
129138568Ssamvoid
130138568Ssamieee80211_sysctl_detach(struct ieee80211com *ic)
131138568Ssam{
132138568Ssam
133138568Ssam	if (ic->ic_sysctl != NULL) {
134138568Ssam		sysctl_ctx_free(ic->ic_sysctl);
135138568Ssam		ic->ic_sysctl = NULL;
136138568Ssam	}
137138568Ssam}
138138568Ssam
139138568Ssamint
140138568Ssamieee80211_node_dectestref(struct ieee80211_node *ni)
141138568Ssam{
142138568Ssam	/* XXX need equivalent of atomic_dec_and_test */
143138568Ssam	atomic_subtract_int(&ni->ni_refcnt, 1);
144138568Ssam	return atomic_cmpset_int(&ni->ni_refcnt, 0, 1);
145138568Ssam}
146138568Ssam
147138568Ssam/*
148138568Ssam * Allocate and setup a management frame of the specified
149138568Ssam * size.  We return the mbuf and a pointer to the start
150138568Ssam * of the contiguous data area that's been reserved based
151138568Ssam * on the packet length.  The data area is forced to 32-bit
152138568Ssam * alignment and the buffer length to a multiple of 4 bytes.
153138568Ssam * This is done mainly so beacon frames (that require this)
154138568Ssam * can use this interface too.
155138568Ssam */
156138568Ssamstruct mbuf *
157138568Ssamieee80211_getmgtframe(u_int8_t **frm, u_int pktlen)
158138568Ssam{
159138568Ssam	struct mbuf *m;
160138568Ssam	u_int len;
161138568Ssam
162138568Ssam	/*
163138568Ssam	 * NB: we know the mbuf routines will align the data area
164138568Ssam	 *     so we don't need to do anything special.
165138568Ssam	 */
166138568Ssam	/* XXX 4-address frame? */
167138568Ssam	len = roundup(sizeof(struct ieee80211_frame) + pktlen, 4);
168138568Ssam	KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len));
169138568Ssam	if (len < MINCLSIZE) {
170138568Ssam		m = m_gethdr(M_NOWAIT, MT_HEADER);
171138568Ssam		/*
172138568Ssam		 * Align the data in case additional headers are added.
173138568Ssam		 * This should only happen when a WEP header is added
174138568Ssam		 * which only happens for shared key authentication mgt
175138568Ssam		 * frames which all fit in MHLEN.
176138568Ssam		 */
177138568Ssam		if (m != NULL)
178138568Ssam			MH_ALIGN(m, len);
179138568Ssam	} else
180138568Ssam		m = m_getcl(M_NOWAIT, MT_HEADER, M_PKTHDR);
181138568Ssam	if (m != NULL) {
182138568Ssam		m->m_data += sizeof(struct ieee80211_frame);
183138568Ssam		*frm = m->m_data;
184138568Ssam	}
185138568Ssam	return m;
186138568Ssam}
187138568Ssam
188138568Ssam#include <sys/libkern.h>
189138568Ssam
190138568Ssamvoid
191138568Ssamget_random_bytes(void *p, size_t n)
192138568Ssam{
193138568Ssam	u_int8_t *dp = p;
194138568Ssam
195138568Ssam	while (n > 0) {
196138568Ssam		u_int32_t v = arc4random();
197138568Ssam		size_t nb = n > sizeof(u_int32_t) ? sizeof(u_int32_t) : n;
198138568Ssam		bcopy(&v, dp, n > sizeof(u_int32_t) ? sizeof(u_int32_t) : n);
199138568Ssam		dp += sizeof(u_int32_t), n -= nb;
200138568Ssam	}
201138568Ssam}
202138568Ssam
203138568Ssamvoid
204138568Ssamieee80211_notify_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int newassoc)
205138568Ssam{
206138568Ssam	struct ifnet *ifp = ic->ic_ifp;
207138568Ssam	struct ieee80211_join_event iev;
208138568Ssam
209138568Ssam	if (ni == ic->ic_bss) {
210138568Ssam		memset(&iev, 0, sizeof(iev));
211138568Ssam		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_bssid);
212138568Ssam		rt_ieee80211msg(ifp, newassoc ?
213138568Ssam			RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC,
214138568Ssam			&iev, sizeof(iev));
215138568Ssam		if_link_state_change(ifp, LINK_STATE_UP);
216138568Ssam	} else if (newassoc) {
217138568Ssam		/* fire off wireless event only for new station */
218138568Ssam		memset(&iev, 0, sizeof(iev));
219138568Ssam		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
220138568Ssam		rt_ieee80211msg(ifp, RTM_IEEE80211_JOIN, &iev, sizeof(iev));
221138568Ssam	}
222138568Ssam}
223138568Ssam
224138568Ssamvoid
225138568Ssamieee80211_notify_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
226138568Ssam{
227138568Ssam	struct ifnet *ifp = ic->ic_ifp;
228138568Ssam	struct ieee80211_leave_event iev;
229138568Ssam
230138568Ssam	if (ni == ic->ic_bss) {
231138568Ssam		rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
232138568Ssam		if_link_state_change(ifp, LINK_STATE_DOWN);
233138568Ssam	} else {
234138568Ssam		/* fire off wireless event station leaving */
235138568Ssam		memset(&iev, 0, sizeof(iev));
236138568Ssam		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
237138568Ssam		rt_ieee80211msg(ifp, RTM_IEEE80211_LEAVE, &iev, sizeof(iev));
238138568Ssam	}
239138568Ssam}
240138568Ssam
241138568Ssamvoid
242138568Ssamieee80211_notify_scan_done(struct ieee80211com *ic)
243138568Ssam{
244138568Ssam	struct ifnet *ifp = ic->ic_ifp;
245138568Ssam
246138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
247138568Ssam		"%s: notify scan done\n", ic->ic_ifp->if_xname);
248138568Ssam
249138568Ssam	/* dispatch wireless event indicating scan completed */
250138568Ssam	rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
251138568Ssam}
252138568Ssam
253138568Ssamvoid
254138568Ssamieee80211_notify_replay_failure(struct ieee80211com *ic,
255138568Ssam	const struct ieee80211_frame *wh, const struct ieee80211_key *k,
256138568Ssam	u_int64_t rsc)
257138568Ssam{
258138568Ssam	struct ifnet *ifp = ic->ic_ifp;
259138568Ssam
260138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
261138568Ssam		"[%s] %s replay detected <rsc %llu, csc %llu>\n",
262138568Ssam		ether_sprintf(wh->i_addr2), k->wk_cipher->ic_name,
263138568Ssam		rsc, k->wk_keyrsc);
264138568Ssam
265138568Ssam	if (ifp != NULL) {		/* NB: for cipher test modules */
266138568Ssam		struct ieee80211_replay_event iev;
267138568Ssam
268138568Ssam		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
269138568Ssam		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
270138568Ssam		iev.iev_cipher = k->wk_cipher->ic_cipher;
271138568Ssam		iev.iev_keyix = k->wk_keyix;
272138568Ssam		iev.iev_keyrsc = k->wk_keyrsc;
273138568Ssam		iev.iev_rsc = rsc;
274138568Ssam		rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
275138568Ssam	}
276138568Ssam}
277138568Ssam
278138568Ssamvoid
279138568Ssamieee80211_notify_michael_failure(struct ieee80211com *ic,
280138568Ssam	const struct ieee80211_frame *wh, u_int keyix)
281138568Ssam{
282138568Ssam	struct ifnet *ifp = ic->ic_ifp;
283138568Ssam
284138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
285138568Ssam		"[%s] Michael MIC verification failed <keyidx %d>\n",
286138568Ssam	       ether_sprintf(wh->i_addr2), keyix);
287138568Ssam	ic->ic_stats.is_rx_tkipmic++;
288138568Ssam
289138568Ssam	if (ifp != NULL) {		/* NB: for cipher test modules */
290138568Ssam		struct ieee80211_michael_event iev;
291138568Ssam
292138568Ssam		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
293138568Ssam		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
294138568Ssam		iev.iev_cipher = IEEE80211_CIPHER_TKIP;
295138568Ssam		iev.iev_keyix = keyix;
296138568Ssam		rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev));
297138568Ssam	}
298138568Ssam}
299138568Ssam
300138568Ssamvoid
301138568Ssamieee80211_load_module(const char *modname)
302138568Ssam{
303138568Ssam	struct thread *td = curthread;
304138568Ssam
305138568Ssam	if (suser(td) == 0 && securelevel_gt(td->td_ucred, 0) == 0) {
306138568Ssam		mtx_lock(&Giant);
307138568Ssam		(void) linker_load_module(modname, NULL, NULL, NULL, NULL);
308138568Ssam		mtx_unlock(&Giant);
309138568Ssam	}
310138568Ssam}
311138568Ssam
312138568Ssam/*
313138568Ssam * Module glue.
314138568Ssam *
315138568Ssam * NB: the module name is "wlan" for compatibility with NetBSD.
316138568Ssam */
317138568Ssamstatic int
318138568Ssamwlan_modevent(module_t mod, int type, void *unused)
319138568Ssam{
320138568Ssam	switch (type) {
321138568Ssam	case MOD_LOAD:
322138568Ssam		if (bootverbose)
323138568Ssam			printf("wlan: <802.11 Link Layer>\n");
324138568Ssam		return 0;
325138568Ssam	case MOD_UNLOAD:
326138568Ssam		return 0;
327138568Ssam	}
328138568Ssam	return EINVAL;
329138568Ssam}
330138568Ssam
331138568Ssamstatic moduledata_t wlan_mod = {
332138568Ssam	"wlan",
333138568Ssam	wlan_modevent,
334138568Ssam	0
335138568Ssam};
336138568SsamDECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
337138568SsamMODULE_VERSION(wlan, 1);
338138568SsamMODULE_DEPEND(wlan, ether, 1, 1, 1);
339