ieee80211_freebsd.c revision 159590
176278Sjoerg/*-
276278Sjoerg * Copyright (c) 2003-2005 Sam Leffler, Errno Consulting
376278Sjoerg * All rights reserved.
476278Sjoerg *
580258Sdd * Redistribution and use in source and binary forms, with or without
680258Sdd * modification, are permitted provided that the following conditions
776278Sjoerg * are met:
876278Sjoerg * 1. Redistributions of source code must retain the above copyright
976278Sjoerg *    notice, this list of conditions and the following disclaimer.
1076278Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1176278Sjoerg *    notice, this list of conditions and the following disclaimer in the
12100709Sru *    documentation and/or other materials provided with the distribution.
13100709Sru * 3. The name of the author may not be used to endorse or promote products
14100709Sru *    derived from this software without specific prior written permission.
15100709Sru *
16100709Sru * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17100709Sru * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18100709Sru * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19100709Sru * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20100709Sru * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21100709Sru * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2276278Sjoerg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2376278Sjoerg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2476278Sjoerg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2581349Sjhay * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2696826Sbmah */
2776278Sjoerg
2876278Sjoerg#include <sys/cdefs.h>
2976278Sjoerg__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_freebsd.c 159590 2006-06-13 21:36:23Z jhb $");
3081250Snik
3181646Sbmah/*
3292506Sjhay * IEEE 802.11 support (FreeBSD-specific code)
3376278Sjoerg */
3476278Sjoerg#include <sys/param.h>
3599918Sbmah#include <sys/kernel.h>
3676278Sjoerg#include <sys/systm.h>
3781646Sbmah#include <sys/linker.h>
3876278Sjoerg#include <sys/mbuf.h>
3981646Sbmah#include <sys/module.h>
4083909Sbmah#include <sys/proc.h>
4176278Sjoerg#include <sys/sysctl.h>
4276278Sjoerg
4376278Sjoerg#include <sys/socket.h>
4476278Sjoerg
4576278Sjoerg#include <net/if.h>
4676278Sjoerg#include <net/if_media.h>
4776278Sjoerg#include <net/ethernet.h>
4876278Sjoerg#include <net/route.h>
4996822Sbmah
5076278Sjoerg#include <net80211/ieee80211_var.h>
5176278Sjoerg
5281250SnikSYSCTL_NODE(_net, OID_AUTO, wlan, CTLFLAG_RD, 0, "IEEE 80211 parameters");
5381250Snik
5476278Sjoerg#ifdef IEEE80211_DEBUG
5576278Sjoergint	ieee80211_debug = 0;
5676278SjoergSYSCTL_INT(_net_wlan, OID_AUTO, debug, CTLFLAG_RW, &ieee80211_debug,
5796826Sbmah	    0, "debugging printfs");
5899918Sbmah#endif
5976278Sjoerg
60100709Srustatic int
61100709Sruieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS)
62100709Sru{
6382344Swilko	int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT;
6482344Swilko	int error;
6582344Swilko
6682344Swilko	error = sysctl_handle_int(oidp, &inact, 0, req);
6782344Swilko	if (error || !req->newptr)
68		return error;
69	*(int *)arg1 = inact / IEEE80211_INACT_WAIT;
70	return 0;
71}
72
73static int
74ieee80211_sysctl_parent(SYSCTL_HANDLER_ARGS)
75{
76	struct ieee80211com *ic = arg1;
77	const char *name = ic->ic_ifp->if_xname;
78
79	return SYSCTL_OUT(req, name, strlen(name));
80}
81
82void
83ieee80211_sysctl_attach(struct ieee80211com *ic)
84{
85	struct sysctl_ctx_list *ctx;
86	struct sysctl_oid *oid;
87	char num[14];			/* sufficient for 32 bits */
88
89	MALLOC(ctx, struct sysctl_ctx_list *, sizeof(struct sysctl_ctx_list),
90		M_DEVBUF, M_NOWAIT | M_ZERO);
91	if (ctx == NULL) {
92		if_printf(ic->ic_ifp, "%s: cannot allocate sysctl context!\n",
93			__func__);
94		return;
95	}
96	sysctl_ctx_init(ctx);
97	snprintf(num, sizeof(num), "%u", ic->ic_vap);
98	oid = SYSCTL_ADD_NODE(ctx, &SYSCTL_NODE_CHILDREN(_net, wlan),
99		OID_AUTO, num, CTLFLAG_RD, NULL, "");
100	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
101		"%parent", CTLFLAG_RD, ic, 0, ieee80211_sysctl_parent, "A",
102		"parent device");
103#ifdef IEEE80211_DEBUG
104	ic->ic_debug = ieee80211_debug;
105	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
106		"debug", CTLFLAG_RW, &ic->ic_debug, 0,
107		"control debugging printfs");
108#endif
109	/* XXX inherit from tunables */
110	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
111		"inact_run", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_run, 0,
112		ieee80211_sysctl_inact, "I",
113		"station inactivity timeout (sec)");
114	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
115		"inact_probe", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_probe, 0,
116		ieee80211_sysctl_inact, "I",
117		"station inactivity probe timeout (sec)");
118	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
119		"inact_auth", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_auth, 0,
120		ieee80211_sysctl_inact, "I",
121		"station authentication timeout (sec)");
122	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
123		"inact_init", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_init, 0,
124		ieee80211_sysctl_inact, "I",
125		"station initial state timeout (sec)");
126	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
127		"driver_caps", CTLFLAG_RW, &ic->ic_caps, 0,
128		"driver capabilities");
129	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
130		"bmiss_max", CTLFLAG_RW, &ic->ic_bmiss_max, 0,
131		"consecutive beacon misses before scanning");
132	ic->ic_sysctl = ctx;
133}
134
135void
136ieee80211_sysctl_detach(struct ieee80211com *ic)
137{
138
139	if (ic->ic_sysctl != NULL) {
140		sysctl_ctx_free(ic->ic_sysctl);
141		ic->ic_sysctl = NULL;
142	}
143}
144
145int
146ieee80211_node_dectestref(struct ieee80211_node *ni)
147{
148	/* XXX need equivalent of atomic_dec_and_test */
149	atomic_subtract_int(&ni->ni_refcnt, 1);
150	return atomic_cmpset_int(&ni->ni_refcnt, 0, 1);
151}
152
153/*
154 * Allocate and setup a management frame of the specified
155 * size.  We return the mbuf and a pointer to the start
156 * of the contiguous data area that's been reserved based
157 * on the packet length.  The data area is forced to 32-bit
158 * alignment and the buffer length to a multiple of 4 bytes.
159 * This is done mainly so beacon frames (that require this)
160 * can use this interface too.
161 */
162struct mbuf *
163ieee80211_getmgtframe(u_int8_t **frm, u_int pktlen)
164{
165	struct mbuf *m;
166	u_int len;
167
168	/*
169	 * NB: we know the mbuf routines will align the data area
170	 *     so we don't need to do anything special.
171	 */
172	/* XXX 4-address frame? */
173	len = roundup(sizeof(struct ieee80211_frame) + pktlen, 4);
174	KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len));
175	if (len < MINCLSIZE) {
176		m = m_gethdr(M_NOWAIT, MT_DATA);
177		/*
178		 * Align the data in case additional headers are added.
179		 * This should only happen when a WEP header is added
180		 * which only happens for shared key authentication mgt
181		 * frames which all fit in MHLEN.
182		 */
183		if (m != NULL)
184			MH_ALIGN(m, len);
185	} else
186		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
187	if (m != NULL) {
188		m->m_data += sizeof(struct ieee80211_frame);
189		*frm = m->m_data;
190	}
191	return m;
192}
193
194#include <sys/libkern.h>
195
196void
197get_random_bytes(void *p, size_t n)
198{
199	u_int8_t *dp = p;
200
201	while (n > 0) {
202		u_int32_t v = arc4random();
203		size_t nb = n > sizeof(u_int32_t) ? sizeof(u_int32_t) : n;
204		bcopy(&v, dp, n > sizeof(u_int32_t) ? sizeof(u_int32_t) : n);
205		dp += sizeof(u_int32_t), n -= nb;
206	}
207}
208
209void
210ieee80211_notify_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int newassoc)
211{
212	struct ifnet *ifp = ic->ic_ifp;
213	struct ieee80211_join_event iev;
214
215	memset(&iev, 0, sizeof(iev));
216	if (ni == ic->ic_bss) {
217		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_bssid);
218		rt_ieee80211msg(ifp, newassoc ?
219			RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC,
220			&iev, sizeof(iev));
221		if_link_state_change(ifp, LINK_STATE_UP);
222	} else {
223		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
224		rt_ieee80211msg(ifp, newassoc ?
225			RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN,
226			&iev, sizeof(iev));
227	}
228}
229
230void
231ieee80211_notify_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
232{
233	struct ifnet *ifp = ic->ic_ifp;
234	struct ieee80211_leave_event iev;
235
236	if (ni == ic->ic_bss) {
237		rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
238		if_link_state_change(ifp, LINK_STATE_DOWN);
239	} else {
240		/* fire off wireless event station leaving */
241		memset(&iev, 0, sizeof(iev));
242		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
243		rt_ieee80211msg(ifp, RTM_IEEE80211_LEAVE, &iev, sizeof(iev));
244	}
245}
246
247void
248ieee80211_notify_scan_done(struct ieee80211com *ic)
249{
250	struct ifnet *ifp = ic->ic_ifp;
251
252	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
253		"%s: notify scan done\n", ic->ic_ifp->if_xname);
254
255	/* dispatch wireless event indicating scan completed */
256	rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
257}
258
259void
260ieee80211_notify_replay_failure(struct ieee80211com *ic,
261	const struct ieee80211_frame *wh, const struct ieee80211_key *k,
262	u_int64_t rsc)
263{
264	struct ifnet *ifp = ic->ic_ifp;
265
266	IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
267	    "[%s] %s replay detected <rsc %ju, csc %ju, keyix %u rxkeyix %u>\n",
268	    ether_sprintf(wh->i_addr2), k->wk_cipher->ic_name,
269	    (intmax_t) rsc, (intmax_t) k->wk_keyrsc,
270	    k->wk_keyix, k->wk_rxkeyix);
271
272	if (ifp != NULL) {		/* NB: for cipher test modules */
273		struct ieee80211_replay_event iev;
274
275		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
276		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
277		iev.iev_cipher = k->wk_cipher->ic_cipher;
278		if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE)
279			iev.iev_keyix = k->wk_rxkeyix;
280		else
281			iev.iev_keyix = k->wk_keyix;
282		iev.iev_keyrsc = k->wk_keyrsc;
283		iev.iev_rsc = rsc;
284		rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
285	}
286}
287
288void
289ieee80211_notify_michael_failure(struct ieee80211com *ic,
290	const struct ieee80211_frame *wh, u_int keyix)
291{
292	struct ifnet *ifp = ic->ic_ifp;
293
294	IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
295		"[%s] michael MIC verification failed <keyix %u>\n",
296	       ether_sprintf(wh->i_addr2), keyix);
297	ic->ic_stats.is_rx_tkipmic++;
298
299	if (ifp != NULL) {		/* NB: for cipher test modules */
300		struct ieee80211_michael_event iev;
301
302		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
303		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
304		iev.iev_cipher = IEEE80211_CIPHER_TKIP;
305		iev.iev_keyix = keyix;
306		rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev));
307	}
308}
309
310void
311ieee80211_load_module(const char *modname)
312{
313
314#ifdef notyet
315	(void)kern_kldload(curthread, modname, NULL);
316#else
317	printf("%s: load the %s module by hand for now.\n", __func__, modname);
318#endif
319}
320
321/*
322 * Module glue.
323 *
324 * NB: the module name is "wlan" for compatibility with NetBSD.
325 */
326static int
327wlan_modevent(module_t mod, int type, void *unused)
328{
329	switch (type) {
330	case MOD_LOAD:
331		if (bootverbose)
332			printf("wlan: <802.11 Link Layer>\n");
333		return 0;
334	case MOD_UNLOAD:
335		return 0;
336	}
337	return EINVAL;
338}
339
340static moduledata_t wlan_mod = {
341	"wlan",
342	wlan_modevent,
343	0
344};
345DECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
346MODULE_VERSION(wlan, 1);
347MODULE_DEPEND(wlan, ether, 1, 1, 1);
348