xref: /freebsd/sys/net80211/ieee80211_freebsd.c (revision f0a75d274af375d15b97b830966b99a02b7db911)
1 /*-
2  * Copyright (c) 2003-2005 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  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 /*
32  * IEEE 802.11 support (FreeBSD-specific code)
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 
45 #include <net/if.h>
46 #include <net/if_media.h>
47 #include <net/ethernet.h>
48 #include <net/route.h>
49 
50 #include <net80211/ieee80211_var.h>
51 
52 SYSCTL_NODE(_net, OID_AUTO, wlan, CTLFLAG_RD, 0, "IEEE 80211 parameters");
53 
54 #ifdef IEEE80211_DEBUG
55 int	ieee80211_debug = 0;
56 SYSCTL_INT(_net_wlan, OID_AUTO, debug, CTLFLAG_RW, &ieee80211_debug,
57 	    0, "debugging printfs");
58 #endif
59 
60 static int
61 ieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS)
62 {
63 	int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT;
64 	int error;
65 
66 	error = sysctl_handle_int(oidp, &inact, 0, req);
67 	if (error || !req->newptr)
68 		return error;
69 	*(int *)arg1 = inact / IEEE80211_INACT_WAIT;
70 	return 0;
71 }
72 
73 static int
74 ieee80211_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 
82 void
83 ieee80211_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 
135 void
136 ieee80211_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 
145 int
146 ieee80211_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 void
154 ieee80211_drain_ifq(struct ifqueue *ifq)
155 {
156 	struct ieee80211_node *ni;
157 	struct mbuf *m;
158 
159 	for (;;) {
160 		IF_DEQUEUE(ifq, m);
161 		if (m == NULL)
162 			break;
163 
164 		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
165 		KASSERT(ni != NULL, ("frame w/o node"));
166 		ieee80211_free_node(ni);
167 		m->m_pkthdr.rcvif = NULL;
168 
169 		m_freem(m);
170 	}
171 }
172 
173 /*
174  * Allocate and setup a management frame of the specified
175  * size.  We return the mbuf and a pointer to the start
176  * of the contiguous data area that's been reserved based
177  * on the packet length.  The data area is forced to 32-bit
178  * alignment and the buffer length to a multiple of 4 bytes.
179  * This is done mainly so beacon frames (that require this)
180  * can use this interface too.
181  */
182 struct mbuf *
183 ieee80211_getmgtframe(u_int8_t **frm, u_int pktlen)
184 {
185 	struct mbuf *m;
186 	u_int len;
187 
188 	/*
189 	 * NB: we know the mbuf routines will align the data area
190 	 *     so we don't need to do anything special.
191 	 */
192 	/* XXX 4-address frame? */
193 	len = roundup(sizeof(struct ieee80211_frame) + pktlen, 4);
194 	KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len));
195 	if (len < MINCLSIZE) {
196 		m = m_gethdr(M_NOWAIT, MT_DATA);
197 		/*
198 		 * Align the data in case additional headers are added.
199 		 * This should only happen when a WEP header is added
200 		 * which only happens for shared key authentication mgt
201 		 * frames which all fit in MHLEN.
202 		 */
203 		if (m != NULL)
204 			MH_ALIGN(m, len);
205 	} else
206 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
207 	if (m != NULL) {
208 		m->m_data += sizeof(struct ieee80211_frame);
209 		*frm = m->m_data;
210 	}
211 	return m;
212 }
213 
214 #include <sys/libkern.h>
215 
216 void
217 get_random_bytes(void *p, size_t n)
218 {
219 	u_int8_t *dp = p;
220 
221 	while (n > 0) {
222 		u_int32_t v = arc4random();
223 		size_t nb = n > sizeof(u_int32_t) ? sizeof(u_int32_t) : n;
224 		bcopy(&v, dp, n > sizeof(u_int32_t) ? sizeof(u_int32_t) : n);
225 		dp += sizeof(u_int32_t), n -= nb;
226 	}
227 }
228 
229 void
230 ieee80211_notify_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int newassoc)
231 {
232 	struct ifnet *ifp = ic->ic_ifp;
233 	struct ieee80211_join_event iev;
234 
235 	memset(&iev, 0, sizeof(iev));
236 	if (ni == ic->ic_bss) {
237 		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_bssid);
238 		rt_ieee80211msg(ifp, newassoc ?
239 			RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC,
240 			&iev, sizeof(iev));
241 		if_link_state_change(ifp, LINK_STATE_UP);
242 	} else {
243 		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
244 		rt_ieee80211msg(ifp, newassoc ?
245 			RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN,
246 			&iev, sizeof(iev));
247 	}
248 }
249 
250 void
251 ieee80211_notify_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
252 {
253 	struct ifnet *ifp = ic->ic_ifp;
254 	struct ieee80211_leave_event iev;
255 
256 	if (ni == ic->ic_bss) {
257 		rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
258 		if_link_state_change(ifp, LINK_STATE_DOWN);
259 	} else {
260 		/* fire off wireless event station leaving */
261 		memset(&iev, 0, sizeof(iev));
262 		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
263 		rt_ieee80211msg(ifp, RTM_IEEE80211_LEAVE, &iev, sizeof(iev));
264 	}
265 }
266 
267 void
268 ieee80211_notify_scan_done(struct ieee80211com *ic)
269 {
270 	struct ifnet *ifp = ic->ic_ifp;
271 
272 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s\n", "notify scan done");
273 
274 	/* dispatch wireless event indicating scan completed */
275 	rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
276 }
277 
278 void
279 ieee80211_notify_replay_failure(struct ieee80211com *ic,
280 	const struct ieee80211_frame *wh, const struct ieee80211_key *k,
281 	u_int64_t rsc)
282 {
283 	struct ifnet *ifp = ic->ic_ifp;
284 
285 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
286 	    "[%s] %s replay detected <rsc %ju, csc %ju, keyix %u rxkeyix %u>\n",
287 	    ether_sprintf(wh->i_addr2), k->wk_cipher->ic_name,
288 	    (intmax_t) rsc, (intmax_t) k->wk_keyrsc,
289 	    k->wk_keyix, k->wk_rxkeyix);
290 
291 	if (ifp != NULL) {		/* NB: for cipher test modules */
292 		struct ieee80211_replay_event iev;
293 
294 		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
295 		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
296 		iev.iev_cipher = k->wk_cipher->ic_cipher;
297 		if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE)
298 			iev.iev_keyix = k->wk_rxkeyix;
299 		else
300 			iev.iev_keyix = k->wk_keyix;
301 		iev.iev_keyrsc = k->wk_keyrsc;
302 		iev.iev_rsc = rsc;
303 		rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
304 	}
305 }
306 
307 void
308 ieee80211_notify_michael_failure(struct ieee80211com *ic,
309 	const struct ieee80211_frame *wh, u_int keyix)
310 {
311 	struct ifnet *ifp = ic->ic_ifp;
312 
313 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
314 		"[%s] michael MIC verification failed <keyix %u>\n",
315 	       ether_sprintf(wh->i_addr2), keyix);
316 	ic->ic_stats.is_rx_tkipmic++;
317 
318 	if (ifp != NULL) {		/* NB: for cipher test modules */
319 		struct ieee80211_michael_event iev;
320 
321 		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
322 		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
323 		iev.iev_cipher = IEEE80211_CIPHER_TKIP;
324 		iev.iev_keyix = keyix;
325 		rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev));
326 	}
327 }
328 
329 void
330 ieee80211_load_module(const char *modname)
331 {
332 
333 #ifdef notyet
334 	(void)kern_kldload(curthread, modname, NULL);
335 #else
336 	printf("%s: load the %s module by hand for now.\n", __func__, modname);
337 #endif
338 }
339 
340 /*
341  * Module glue.
342  *
343  * NB: the module name is "wlan" for compatibility with NetBSD.
344  */
345 static int
346 wlan_modevent(module_t mod, int type, void *unused)
347 {
348 	switch (type) {
349 	case MOD_LOAD:
350 		if (bootverbose)
351 			printf("wlan: <802.11 Link Layer>\n");
352 		return 0;
353 	case MOD_UNLOAD:
354 		return 0;
355 	}
356 	return EINVAL;
357 }
358 
359 static moduledata_t wlan_mod = {
360 	"wlan",
361 	wlan_modevent,
362 	0
363 };
364 DECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
365 MODULE_VERSION(wlan, 1);
366 MODULE_DEPEND(wlan, ether, 1, 1, 1);
367