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  * $FreeBSD: src/sys/net80211/ieee80211_freebsd.c,v 1.7.2.2 2005/12/22 19:22:51 sam Exp $
28  * $DragonFly: src/sys/netproto/802_11/wlan/ieee80211_dragonfly.c,v 1.12 2007/09/15 07:19:23 sephe Exp $
29  */
30 
31 /*
32  * IEEE 802.11 support (DragonFlyBSD-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/priv.h>
42 #include <sys/sysctl.h>
43 
44 #include <sys/socket.h>
45 
46 #include <net/if.h>
47 #include <net/if_arp.h>
48 #include <net/if_media.h>
49 #include <net/ethernet.h>
50 #include <net/route.h>
51 
52 #include <netproto/802_11/ieee80211_var.h>
53 
54 SYSCTL_NODE(_net, OID_AUTO, wlan, CTLFLAG_RD, 0, "IEEE 80211 parameters");
55 
56 #ifdef IEEE80211_DEBUG
57 int	ieee80211_debug = 0;
58 SYSCTL_INT(_net_wlan, OID_AUTO, debug, CTLFLAG_RW, &ieee80211_debug,
59 	    0, "debugging kprintfs");
60 #endif
61 
62 static int
63 ieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS)
64 {
65 	int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT;
66 	int error;
67 
68 	error = sysctl_handle_int(oidp, &inact, 0, req);
69 	if (error || !req->newptr)
70 		return error;
71 	*(int *)arg1 = inact / IEEE80211_INACT_WAIT;
72 	return 0;
73 }
74 
75 static int
76 ieee80211_sysctl_parent(SYSCTL_HANDLER_ARGS)
77 {
78 	struct ieee80211com *ic = arg1;
79 	const char *name = ic->ic_ifp->if_xname;
80 
81 	return SYSCTL_OUT(req, name, strlen(name));
82 }
83 
84 void
85 ieee80211_sysctl_attach(struct ieee80211com *ic)
86 {
87 	struct sysctl_ctx_list *ctx;
88 	struct sysctl_oid *oid;
89 	char num[14];			/* sufficient for 32 bits */
90 
91 	ctx = kmalloc(sizeof(struct sysctl_ctx_list), M_DEVBUF,
92 		     M_WAITOK | M_ZERO);
93 	sysctl_ctx_init(ctx);
94 
95 	ksnprintf(num, sizeof(num), "%u", ic->ic_vap);
96 	oid = SYSCTL_ADD_NODE(ctx, &SYSCTL_NODE_CHILDREN(_net, wlan),
97 		OID_AUTO, num, CTLFLAG_RD, NULL, "");
98 	if (oid == NULL) {
99 		kprintf("add sysctl node net.wlan.%s failed\n", num);
100 		kfree(ctx, M_DEVBUF);
101 		return;
102 	}
103 
104 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
105 		"%parent", CTLFLAG_RD, ic, 0, ieee80211_sysctl_parent, "A",
106 		"parent device");
107 #ifdef IEEE80211_DEBUG
108 	ic->ic_debug = ieee80211_debug;
109 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
110 		"debug", CTLFLAG_RW, &ic->ic_debug, 0,
111 		"control debugging kprintfs");
112 #endif
113 	/* XXX inherit from tunables */
114 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
115 		"inact_run", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_run, 0,
116 		ieee80211_sysctl_inact, "I",
117 		"station inactivity timeout (sec)");
118 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
119 		"inact_probe", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_probe, 0,
120 		ieee80211_sysctl_inact, "I",
121 		"station inactivity probe timeout (sec)");
122 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
123 		"inact_auth", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_auth, 0,
124 		ieee80211_sysctl_inact, "I",
125 		"station authentication timeout (sec)");
126 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
127 		"inact_init", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_init, 0,
128 		ieee80211_sysctl_inact, "I",
129 		"station initial state timeout (sec)");
130 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
131 		"driver_caps", CTLFLAG_RW, &ic->ic_caps, 0,
132 		"driver capabilities");
133 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
134 		"bmiss_max", CTLFLAG_RW, &ic->ic_bmiss_max, 0,
135 		"consecutive beacon misses before scanning");
136 
137 	ic->ic_sysctl = ctx;
138 	ic->ic_sysctl_oid = oid;
139 }
140 
141 void
142 ieee80211_sysctl_detach(struct ieee80211com *ic)
143 {
144 	if (ic->ic_sysctl != NULL) {
145 		sysctl_ctx_free(ic->ic_sysctl);
146 		kfree(ic->ic_sysctl, M_DEVBUF);
147 		ic->ic_sysctl = NULL;
148 	}
149 }
150 
151 int
152 ieee80211_node_dectestref(struct ieee80211_node *ni)
153 {
154 	/* XXX need equivalent of atomic_dec_and_test */
155 	atomic_subtract_int(&ni->ni_refcnt, 1);
156 	return atomic_cmpset_int(&ni->ni_refcnt, 0, 1);
157 }
158 
159 /*
160  * Allocate and setup a management frame of the specified
161  * size.  We return the mbuf and a pointer to the start
162  * of the contiguous data area that's been reserved based
163  * on the packet length.  The data area is forced to 32-bit
164  * alignment and the buffer length to a multiple of 4 bytes.
165  * This is done mainly so beacon frames (that require this)
166  * can use this interface too.
167  */
168 struct mbuf *
169 ieee80211_getmgtframe(uint8_t **frm, int headroom, u_int pktlen)
170 {
171 	struct mbuf *m;
172 	u_int len;
173 
174 	/*
175 	 * NB: we know the mbuf routines will align the data area
176 	 *     so we don't need to do anything special.
177 	 */
178 	/* XXX 4-address frame? */
179 	len = roundup(headroom + pktlen, 4);
180 	KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len));
181 	if (len < MINCLSIZE) {
182 		m = m_gethdr(MB_DONTWAIT, MT_HEADER);
183 		/*
184 		 * Align the data in case additional headers are added.
185 		 * This should only happen when a WEP header is added
186 		 * which only happens for shared key authentication mgt
187 		 * frames which all fit in MHLEN.
188 		 */
189 		if (m != NULL)
190 			MH_ALIGN(m, len);
191 	} else
192 		m = m_getcl(MB_DONTWAIT, MT_HEADER, M_PKTHDR);
193 	if (m != NULL) {
194 		m->m_data += headroom;
195 		*frm = mtod(m, uint8_t *);
196 	}
197 	return m;
198 }
199 
200 #include <sys/libkern.h>
201 
202 void
203 get_random_bytes(void *p, size_t n)
204 {
205 	uint8_t *dp = p;
206 
207 	while (n > 0) {
208 		uint32_t v = karc4random();
209 		size_t nb = n > sizeof(uint32_t) ? sizeof(uint32_t) : n;
210 
211 		bcopy(&v, dp, n > sizeof(uint32_t) ? sizeof(uint32_t) : n);
212 		dp += sizeof(uint32_t), n -= nb;
213 	}
214 }
215 
216 void
217 ieee80211_notify_node_join(struct ieee80211com *ic, struct ieee80211_node *ni,
218 			   int newassoc)
219 {
220 	struct ifnet *ifp = ic->ic_ifp;
221 	struct ieee80211_join_event iev;
222 
223 	memset(&iev, 0, sizeof(iev));
224 	if (ni == ic->ic_bss) {
225 		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_bssid);
226 		rt_ieee80211msg(ifp, newassoc ?
227 			RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC,
228 			&iev, sizeof(iev));
229 		ifp->if_link_state = LINK_STATE_UP;
230 		if_link_state_change(ifp);
231 	} else {
232 		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
233 		rt_ieee80211msg(ifp, newassoc ?
234 			RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN,
235 			&iev, sizeof(iev));
236 	}
237 }
238 
239 void
240 ieee80211_notify_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
241 {
242 	struct ifnet *ifp = ic->ic_ifp;
243 	struct ieee80211_leave_event iev;
244 
245 	if (ni == ic->ic_bss) {
246 		rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
247 		ifp->if_link_state = LINK_STATE_DOWN;
248 		if_link_state_change(ifp);
249 	} else {
250 		/* fire off wireless event station leaving */
251 		memset(&iev, 0, sizeof(iev));
252 		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
253 		rt_ieee80211msg(ifp, RTM_IEEE80211_LEAVE, &iev, sizeof(iev));
254 	}
255 }
256 
257 void
258 ieee80211_notify_scan_done(struct ieee80211com *ic)
259 {
260 	struct ifnet *ifp = ic->ic_ifp;
261 
262 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s\n", "notify scan done");
263 
264 	/* dispatch wireless event indicating scan completed */
265 	rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
266 }
267 
268 void
269 ieee80211_notify_replay_failure(struct ieee80211com *ic,
270 	const struct ieee80211_frame *wh, const struct ieee80211_key *k,
271 	uint64_t rsc)
272 {
273 	struct ifnet *ifp = ic->ic_ifp;
274 
275 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
276 	    "[%6D] %s replay detected <rsc %ju, csc %ju, keyix %u rxkeyix %u>\n",
277 	    wh->i_addr2, ":", k->wk_cipher->ic_name,
278 	    (intmax_t) rsc, (intmax_t) k->wk_keyrsc,
279 	    k->wk_keyix, k->wk_rxkeyix);
280 
281 	if (ifp != NULL) {		/* NB: for cipher test modules */
282 		struct ieee80211_replay_event iev;
283 
284 		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
285 		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
286 		iev.iev_cipher = k->wk_cipher->ic_cipher;
287 		if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE)
288 			iev.iev_keyix = k->wk_rxkeyix;
289 		else
290 			iev.iev_keyix = k->wk_keyix;
291 		iev.iev_keyrsc = k->wk_keyrsc;
292 		iev.iev_rsc = rsc;
293 		rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
294 	}
295 }
296 
297 void
298 ieee80211_notify_michael_failure(struct ieee80211com *ic,
299 	const struct ieee80211_frame *wh, u_int keyix)
300 {
301 	struct ifnet *ifp = ic->ic_ifp;
302 
303 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
304 		"[%6D] michael MIC verification failed <keyix %u>\n",
305 	       wh->i_addr2, ":", keyix);
306 	ic->ic_stats.is_rx_tkipmic++;
307 
308 	if (ifp != NULL) {		/* NB: for cipher test modules */
309 		struct ieee80211_michael_event iev;
310 
311 		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
312 		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
313 		iev.iev_cipher = IEEE80211_CIPHER_TKIP;
314 		iev.iev_keyix = keyix;
315 		rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev));
316 	}
317 }
318 
319 void
320 ieee80211_load_module(const char *modname)
321 {
322 #ifdef notyet
323 	struct thread *td = curthread;
324 
325 	if (priv_check(td, PRIV_ROOT) == 0 && securelevel_gt(td->td_ucred, 0) == 0) {
326 		crit_enter();	/* NB: need BGL here */
327 		linker_load_module(modname, NULL, NULL, NULL, NULL);
328 		crit_exit();
329 	}
330 #else
331 	kprintf("%s: load the %s module by hand for now.\n", __func__, modname);
332 #endif
333 }
334 
335 /*
336  * Append the specified data to the indicated mbuf chain,
337  * Extend the mbuf chain if the new data does not fit in
338  * existing space.
339  *
340  * Return 1 if able to complete the job; otherwise 0.
341  */
342 int
343 ieee80211_mbuf_append(struct mbuf *m0, int len, const uint8_t *cp)
344 {
345 	struct mbuf *m, *n;
346 	int remainder, space;
347 
348 	for (m = m0; m->m_next != NULL; m = m->m_next)
349 		;
350 	remainder = len;
351 	space = M_TRAILINGSPACE(m);
352 	if (space > 0) {
353 		/*
354 		 * Copy into available space.
355 		 */
356 		if (space > remainder)
357 			space = remainder;
358 		bcopy(cp, mtod(m, caddr_t) + m->m_len, space);
359 		m->m_len += space;
360 		cp += space, remainder -= space;
361 	}
362 	while (remainder > 0) {
363 		/*
364 		 * Allocate a new mbuf; could check space
365 		 * and allocate a cluster instead.
366 		 */
367 		n = m_get(MB_DONTWAIT, m->m_type);
368 		if (n == NULL)
369 			break;
370 		n->m_len = min(MLEN, remainder);
371 		bcopy(cp, mtod(n, caddr_t), n->m_len);
372 		cp += n->m_len, remainder -= n->m_len;
373 		m->m_next = n;
374 		m = n;
375 	}
376 	if (m0->m_flags & M_PKTHDR)
377 		m0->m_pkthdr.len += len - remainder;
378 	return (remainder == 0);
379 }
380 
381 /*
382  * Create a writable copy of the mbuf chain.  While doing this
383  * we compact the chain with a goal of producing a chain with
384  * at most two mbufs.  The second mbuf in this chain is likely
385  * to be a cluster.  The primary purpose of this work is to create
386  * a writable packet for encryption, compression, etc.  The
387  * secondary goal is to linearize the data so the data can be
388  * passed to crypto hardware in the most efficient manner possible.
389  */
390 struct mbuf *
391 ieee80211_mbuf_clone(struct mbuf *m0, int how)
392 {
393 	struct mbuf *m, *mprev;
394 	struct mbuf *n, *mfirst, *mlast;
395 	int len, off;
396 
397 	mprev = NULL;
398 	for (m = m0; m != NULL; m = mprev->m_next) {
399 		/*
400 		 * Regular mbufs are ignored unless there's a cluster
401 		 * in front of it that we can use to coalesce.  We do
402 		 * the latter mainly so later clusters can be coalesced
403 		 * also w/o having to handle them specially (i.e. convert
404 		 * mbuf+cluster -> cluster).  This optimization is heavily
405 		 * influenced by the assumption that we're running over
406 		 * Ethernet where MCLBYTES is large enough that the max
407 		 * packet size will permit lots of coalescing into a
408 		 * single cluster.  This in turn permits efficient
409 		 * crypto operations, especially when using hardware.
410 		 */
411 		if ((m->m_flags & M_EXT) == 0) {
412 			if (mprev && (mprev->m_flags & M_EXT) &&
413 			    m->m_len <= M_TRAILINGSPACE(mprev)) {
414 				/* XXX: this ignores mbuf types */
415 				memcpy(mtod(mprev, caddr_t) + mprev->m_len,
416 				       mtod(m, caddr_t), m->m_len);
417 				mprev->m_len += m->m_len;
418 				mprev->m_next = m->m_next;	/* unlink from chain */
419 				m_free(m);			/* reclaim mbuf */
420 			} else {
421 				mprev = m;
422 			}
423 			continue;
424 		}
425 		/*
426 		 * Writable mbufs are left alone (for now).
427 		 */
428 		if (M_WRITABLE(m)) {
429 			mprev = m;
430 			continue;
431 		}
432 
433 		/*
434 		 * Not writable, replace with a copy or coalesce with
435 		 * the previous mbuf if possible (since we have to copy
436 		 * it anyway, we try to reduce the number of mbufs and
437 		 * clusters so that future work is easier).
438 		 */
439 		KASSERT(m->m_flags & M_EXT, ("m_flags 0x%x", m->m_flags));
440 		/* NB: we only coalesce into a cluster or larger */
441 		if (mprev != NULL && (mprev->m_flags & M_EXT) &&
442 		    m->m_len <= M_TRAILINGSPACE(mprev)) {
443 			/* XXX: this ignores mbuf types */
444 			memcpy(mtod(mprev, caddr_t) + mprev->m_len,
445 			       mtod(m, caddr_t), m->m_len);
446 			mprev->m_len += m->m_len;
447 			mprev->m_next = m->m_next;	/* unlink from chain */
448 			m_free(m);			/* reclaim mbuf */
449 			continue;
450 		}
451 
452 		/*
453 		 * Allocate new space to hold the copy...
454 		 */
455 		/* XXX why can M_PKTHDR be set past the first mbuf? */
456 		if (mprev == NULL && (m->m_flags & M_PKTHDR)) {
457 			/*
458 			 * NB: if a packet header is present we must
459 			 * allocate the mbuf separately from any cluster
460 			 * because M_MOVE_PKTHDR will smash the data
461 			 * pointer and drop the M_EXT marker.
462 			 */
463 			MGETHDR(n, how, m->m_type);
464 			if (n == NULL) {
465 				m_freem(m0);
466 				return (NULL);
467 			}
468 			M_MOVE_PKTHDR(n, m);
469 			MCLGET(n, how);
470 			if ((n->m_flags & M_EXT) == 0) {
471 				m_free(n);
472 				m_freem(m0);
473 				return (NULL);
474 			}
475 		} else {
476 			n = m_getcl(how, m->m_type, m->m_flags);
477 			if (n == NULL) {
478 				m_freem(m0);
479 				return (NULL);
480 			}
481 		}
482 		/*
483 		 * ... and copy the data.  We deal with jumbo mbufs
484 		 * (i.e. m_len > MCLBYTES) by splitting them into
485 		 * clusters.  We could just malloc a buffer and make
486 		 * it external but too many device drivers don't know
487 		 * how to break up the non-contiguous memory when
488 		 * doing DMA.
489 		 */
490 		len = m->m_len;
491 		off = 0;
492 		mfirst = n;
493 		mlast = NULL;
494 		for (;;) {
495 			int cc = min(len, MCLBYTES);
496 			memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + off, cc);
497 			n->m_len = cc;
498 			if (mlast != NULL)
499 				mlast->m_next = n;
500 			mlast = n;
501 
502 			len -= cc;
503 			if (len <= 0)
504 				break;
505 			off += cc;
506 
507 			n = m_getcl(how, m->m_type, m->m_flags);
508 			if (n == NULL) {
509 				m_freem(mfirst);
510 				m_freem(m0);
511 				return (NULL);
512 			}
513 		}
514 		n->m_next = m->m_next;
515 		if (mprev == NULL)
516 			m0 = mfirst;		/* new head of chain */
517 		else
518 			mprev->m_next = mfirst;	/* replace old mbuf */
519 		m_free(m);			/* release old mbuf */
520 		mprev = mfirst;
521 	}
522 	return (m0);
523 }
524 
525 void
526 ieee80211_drain_mgtq(struct ifqueue *ifq)
527 {
528 	for (;;) {
529 		struct ieee80211_node *ni;
530 		struct mbuf *m;
531 
532 		IF_DEQUEUE(ifq, m);
533 		if (m == NULL)
534 			break;
535 
536 		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
537 		KKASSERT(ni != NULL);
538 		ieee80211_free_node(ni);
539 
540 		m->m_pkthdr.rcvif = NULL;
541 		m_freem(m);
542 	}
543 }
544 
545 /*
546  * Module glue.
547  *
548  * NB: the module name is "wlan" for compatibility with NetBSD.
549  */
550 static int
551 wlan_modevent(module_t mod, int type, void *unused)
552 {
553 	switch (type) {
554 	case MOD_LOAD:
555 		if (bootverbose)
556 			kprintf("wlan: <802.11 Link Layer>\n");
557 		return 0;
558 	case MOD_UNLOAD:
559 		return 0;
560 	}
561 	return EINVAL;
562 }
563 
564 static moduledata_t wlan_mod = {
565 	"wlan",
566 	wlan_modevent,
567 	0
568 };
569 DECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
570 MODULE_VERSION(wlan, 1);
571 MODULE_DEPEND(wlan, crypto, 1, 1, 1);
572