1 /*-
2  * Copyright (c) 2003-2009 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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * $FreeBSD: head/sys/net80211/ieee80211_freebsd.c 202612 2010-01-19 05:00:57Z thompsa $
26  * $DragonFly$
27  */
28 
29 /*
30  * IEEE 802.11 support (DragonFlyBSD-specific code)
31  */
32 #include "opt_wlan.h"
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/bpf.h>
46 #include <net/if.h>
47 #include <net/if_dl.h>
48 #include <net/if_clone.h>
49 #include <net/if_media.h>
50 #include <net/if_types.h>
51 #include <net/ethernet.h>
52 #include <net/route.h>
53 #include <net/ifq_var.h>
54 
55 #include <netproto/802_11/ieee80211_var.h>
56 #include <netproto/802_11/ieee80211_input.h>
57 
58 SYSCTL_NODE(_net, OID_AUTO, wlan, CTLFLAG_RD, 0, "IEEE 80211 parameters");
59 
60 #ifdef IEEE80211_DEBUG
61 int	ieee80211_debug = 0;
62 SYSCTL_INT(_net_wlan, OID_AUTO, debug, CTLFLAG_RW, &ieee80211_debug,
63 	    0, "debugging printfs");
64 #endif
65 
66 int	ieee80211_force_swcrypto = 0;
67 SYSCTL_INT(_net_wlan, OID_AUTO, force_swcrypto, CTLFLAG_RW,
68 	    &ieee80211_force_swcrypto, 0, "force software crypto");
69 
70 MALLOC_DEFINE(M_80211_COM, "80211com", "802.11 com state");
71 
72 
73 static int	wlan_clone_destroy(struct ifnet *);
74 static int	wlan_clone_create(struct if_clone *, int, caddr_t);
75 
76 static struct if_clone wlan_cloner =
77 	IF_CLONE_INITIALIZER("wlan", wlan_clone_create, wlan_clone_destroy,
78 	    0, IF_MAXUNIT);
79 
80 struct lwkt_serialize wlan_global_serializer = LWKT_SERIALIZE_INITIALIZER;
81 
82 /*
83  * Allocate/free com structure in conjunction with ifnet;
84  * these routines are registered with if_register_com_alloc
85  * below and are called automatically by the ifnet code
86  * when the ifnet of the parent device is created.
87  */
88 static void *
89 wlan_alloc(u_char type, struct ifnet *ifp)
90 {
91 	struct ieee80211com *ic;
92 
93 	ic = kmalloc(sizeof(struct ieee80211com), M_80211_COM, M_WAITOK|M_ZERO);
94 	ic->ic_ifp = ifp;
95 
96 	return (ic);
97 }
98 
99 static void
100 wlan_free(void *ic, u_char type)
101 {
102 	kfree(ic, M_80211_COM);
103 }
104 
105 static int
106 wlan_clone_create(struct if_clone *ifc, int unit, caddr_t params)
107 {
108 	struct ieee80211_clone_params cp;
109 	struct ieee80211vap *vap;
110 	struct ieee80211com *ic;
111 	struct ifnet *ifp;
112 	int error;
113 
114 	error = copyin(params, &cp, sizeof(cp));
115 	if (error)
116 		return error;
117 	ifp = ifunit(cp.icp_parent);
118 	if (ifp == NULL)
119 		return ENXIO;
120 	/* XXX move printfs to DIAGNOSTIC before release */
121 	if (ifp->if_type != IFT_IEEE80211) {
122 		if_printf(ifp, "%s: reject, not an 802.11 device\n", __func__);
123 		return ENXIO;
124 	}
125 	if (cp.icp_opmode >= IEEE80211_OPMODE_MAX) {
126 		if_printf(ifp, "%s: invalid opmode %d\n",
127 		    __func__, cp.icp_opmode);
128 		return EINVAL;
129 	}
130 	ic = ifp->if_l2com;
131 	if ((ic->ic_caps & ieee80211_opcap[cp.icp_opmode]) == 0) {
132 		if_printf(ifp, "%s mode not supported\n",
133 		    ieee80211_opmode_name[cp.icp_opmode]);
134 		return EOPNOTSUPP;
135 	}
136 	if ((cp.icp_flags & IEEE80211_CLONE_TDMA) &&
137 #ifdef IEEE80211_SUPPORT_TDMA
138 	    (ic->ic_caps & IEEE80211_C_TDMA) == 0
139 #else
140 	    (1)
141 #endif
142 	) {
143 		if_printf(ifp, "TDMA not supported\n");
144 		return EOPNOTSUPP;
145 	}
146 	vap = ic->ic_vap_create(ic, ifc->ifc_name, unit,
147 			cp.icp_opmode, cp.icp_flags, cp.icp_bssid,
148 			cp.icp_flags & IEEE80211_CLONE_MACADDR ?
149 			    cp.icp_macaddr : (const uint8_t *)IF_LLADDR(ifp));
150 	return (vap == NULL ? EIO : 0);
151 }
152 
153 static int
154 wlan_clone_destroy(struct ifnet *ifp)
155 {
156 	struct ieee80211vap *vap = ifp->if_softc;
157 	struct ieee80211com *ic = vap->iv_ic;
158 
159 	wlan_serialize_enter();	/* WARNING must be global serializer */
160 	ic->ic_vap_delete(vap);
161 	wlan_serialize_exit();
162 
163 	return 0;
164 }
165 
166 const char *wlan_last_enter_func;
167 const char *wlan_last_exit_func;
168 /*
169  * These serializer functions are used by wlan and all drivers.
170  */
171 void
172 _wlan_serialize_enter(const char *funcname)
173 {
174 	lwkt_serialize_enter(&wlan_global_serializer);
175 	wlan_last_enter_func = funcname;
176 }
177 
178 void
179 _wlan_serialize_exit(const char *funcname)
180 {
181 	lwkt_serialize_exit(&wlan_global_serializer);
182 	wlan_last_exit_func = funcname;
183 }
184 
185 int
186 wlan_serialize_sleep(void *ident, int flags, const char *wmesg, int timo)
187 {
188 	return(zsleep(ident, &wlan_global_serializer, flags, wmesg, timo));
189 }
190 
191 /*
192  * condition-var functions which interlock the ic lock (which is now
193  * just wlan_global_serializer)
194  */
195 void
196 wlan_cv_init(struct cv *cv, const char *desc)
197 {
198 	cv->cv_desc = desc;
199 	cv->cv_waiters = 0;
200 }
201 
202 int
203 wlan_cv_timedwait(struct cv *cv, int ticks)
204 {
205 	int error;
206 
207 	++cv->cv_waiters;
208 	error = wlan_serialize_sleep(cv, 0, cv->cv_desc, ticks);
209 	return (error);
210 }
211 
212 void
213 wlan_cv_wait(struct cv *cv)
214 {
215 	++cv->cv_waiters;
216 	wlan_serialize_sleep(cv, 0, cv->cv_desc, 0);
217 }
218 
219 void
220 wlan_cv_signal(struct cv *cv, int broadcast)
221 {
222 	if (cv->cv_waiters) {
223 		if (broadcast) {
224 			cv->cv_waiters = 0;
225 			wakeup(cv);
226 		} else {
227 			--cv->cv_waiters;
228 			wakeup_one(cv);
229 		}
230 	}
231 }
232 
233 /*
234  * Misc
235  */
236 void
237 ieee80211_vap_destroy(struct ieee80211vap *vap)
238 {
239 	wlan_assert_serialized();
240 	wlan_serialize_exit();
241 	if_clone_destroy(vap->iv_ifp->if_xname);
242 	wlan_serialize_enter();
243 }
244 
245 /*
246  * NOTE: This handler is used generally to convert milliseconds
247  *	 to ticks for various simple sysctl variables and does not
248  *	 need to be serialized.
249  */
250 int
251 ieee80211_sysctl_msecs_ticks(SYSCTL_HANDLER_ARGS)
252 {
253 	int msecs = ticks_to_msecs(*(int *)arg1);
254 	int error, t;
255 
256 	error = sysctl_handle_int(oidp, &msecs, 0, req);
257 	if (error == 0 && req->newptr) {
258 		t = msecs_to_ticks(msecs);
259 		*(int *)arg1 = (t < 1) ? 1 : t;
260 	}
261 
262 	return error;
263 }
264 
265 static int
266 ieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS)
267 {
268 	int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT;
269 	int error;
270 
271 	error = sysctl_handle_int(oidp, &inact, 0, req);
272 	wlan_serialize_enter();
273 	if (error == 0 && req->newptr)
274 		*(int *)arg1 = inact / IEEE80211_INACT_WAIT;
275 	wlan_serialize_exit();
276 
277 	return error;
278 }
279 
280 static int
281 ieee80211_sysctl_parent(SYSCTL_HANDLER_ARGS)
282 {
283 	struct ieee80211com *ic = arg1;
284 	const char *name = ic->ic_ifp->if_xname;
285 
286 	return SYSCTL_OUT(req, name, strlen(name));
287 }
288 
289 static int
290 ieee80211_sysctl_radar(SYSCTL_HANDLER_ARGS)
291 {
292 	struct ieee80211com *ic = arg1;
293 	int t = 0, error;
294 
295 	error = sysctl_handle_int(oidp, &t, 0, req);
296 	wlan_serialize_enter();
297 	if (error == 0 && req->newptr)
298 		ieee80211_dfs_notify_radar(ic, ic->ic_curchan);
299 	wlan_serialize_exit();
300 
301 	return error;
302 }
303 
304 void
305 ieee80211_sysctl_attach(struct ieee80211com *ic)
306 {
307 }
308 
309 void
310 ieee80211_sysctl_detach(struct ieee80211com *ic)
311 {
312 }
313 
314 void
315 ieee80211_sysctl_vattach(struct ieee80211vap *vap)
316 {
317 	struct ifnet *ifp = vap->iv_ifp;
318 	struct sysctl_ctx_list *ctx;
319 	struct sysctl_oid *oid;
320 	char num[14];			/* sufficient for 32 bits */
321 
322 	ctx = (struct sysctl_ctx_list *) kmalloc(sizeof(struct sysctl_ctx_list),
323 		M_DEVBUF, M_INTWAIT | M_ZERO);
324 	if (ctx == NULL) {
325 		if_printf(ifp, "%s: cannot allocate sysctl context!\n",
326 			__func__);
327 		return;
328 	}
329 	sysctl_ctx_init(ctx);
330 	ksnprintf(num, sizeof(num), "%u", ifp->if_dunit);
331 	oid = SYSCTL_ADD_NODE(ctx, &SYSCTL_NODE_CHILDREN(_net, wlan),
332 		OID_AUTO, num, CTLFLAG_RD, NULL, "");
333 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
334 		"%parent", CTLFLAG_RD, vap->iv_ic, 0,
335 		ieee80211_sysctl_parent, "A", "parent device");
336 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
337 		"driver_caps", CTLFLAG_RW, &vap->iv_caps, 0,
338 		"driver capabilities");
339 #ifdef IEEE80211_DEBUG
340 	vap->iv_debug = ieee80211_debug;
341 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
342 		"debug", CTLFLAG_RW, &vap->iv_debug, 0,
343 		"control debugging printfs");
344 #endif
345 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
346 		"bmiss_max", CTLFLAG_RW, &vap->iv_bmiss_max, 0,
347 		"consecutive beacon misses before scanning");
348 	/* XXX inherit from tunables */
349 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
350 		"inact_run", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_run, 0,
351 		ieee80211_sysctl_inact, "I",
352 		"station inactivity timeout (sec)");
353 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
354 		"inact_probe", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_probe, 0,
355 		ieee80211_sysctl_inact, "I",
356 		"station inactivity probe timeout (sec)");
357 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
358 		"inact_auth", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_auth, 0,
359 		ieee80211_sysctl_inact, "I",
360 		"station authentication timeout (sec)");
361 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
362 		"inact_init", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_init, 0,
363 		ieee80211_sysctl_inact, "I",
364 		"station initial state timeout (sec)");
365 	if (vap->iv_htcaps & IEEE80211_HTC_HT) {
366 		SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
367 			"ampdu_mintraffic_bk", CTLFLAG_RW,
368 			&vap->iv_ampdu_mintraffic[WME_AC_BK], 0,
369 			"BK traffic tx aggr threshold (pps)");
370 		SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
371 			"ampdu_mintraffic_be", CTLFLAG_RW,
372 			&vap->iv_ampdu_mintraffic[WME_AC_BE], 0,
373 			"BE traffic tx aggr threshold (pps)");
374 		SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
375 			"ampdu_mintraffic_vo", CTLFLAG_RW,
376 			&vap->iv_ampdu_mintraffic[WME_AC_VO], 0,
377 			"VO traffic tx aggr threshold (pps)");
378 		SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
379 			"ampdu_mintraffic_vi", CTLFLAG_RW,
380 			&vap->iv_ampdu_mintraffic[WME_AC_VI], 0,
381 			"VI traffic tx aggr threshold (pps)");
382 	}
383 	if (vap->iv_caps & IEEE80211_C_DFS) {
384 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
385 			"radar", CTLTYPE_INT | CTLFLAG_RW, vap->iv_ic, 0,
386 			ieee80211_sysctl_radar, "I", "simulate radar event");
387 	}
388 	vap->iv_sysctl = ctx;
389 	vap->iv_oid = oid;
390 }
391 
392 void
393 ieee80211_sysctl_vdetach(struct ieee80211vap *vap)
394 {
395 
396 	if (vap->iv_sysctl != NULL) {
397 		sysctl_ctx_free(vap->iv_sysctl);
398 		kfree(vap->iv_sysctl, M_DEVBUF);
399 		vap->iv_sysctl = NULL;
400 	}
401 }
402 
403 int
404 ieee80211_node_dectestref(struct ieee80211_node *ni)
405 {
406 	/* XXX need equivalent of atomic_dec_and_test */
407 	atomic_subtract_int(&ni->ni_refcnt, 1);
408 	return atomic_cmpset_int(&ni->ni_refcnt, 0, 1);
409 }
410 
411 void
412 ieee80211_drain_ifq(struct ifqueue *ifq)
413 {
414 	struct ieee80211_node *ni;
415 	struct mbuf *m;
416 
417 	wlan_assert_serialized();
418 	for (;;) {
419 		IF_DEQUEUE(ifq, m);
420 		if (m == NULL)
421 			break;
422 
423 		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
424 		KASSERT(ni != NULL, ("frame w/o node"));
425 		ieee80211_free_node(ni);
426 		m->m_pkthdr.rcvif = NULL;
427 
428 		m_freem(m);
429 	}
430 }
431 
432 void
433 ieee80211_flush_ifq(struct ifqueue *ifq, struct ieee80211vap *vap)
434 {
435 	struct ieee80211_node *ni;
436 	struct mbuf *m, **mprev;
437 
438 	wlan_assert_serialized();
439 	mprev = &ifq->ifq_head;
440 	while ((m = *mprev) != NULL) {
441 		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
442 		if (ni != NULL && ni->ni_vap == vap) {
443 			*mprev = m->m_nextpkt;		/* remove from list */
444 			ifq->ifq_len--;
445 
446 			m_freem(m);
447 			ieee80211_free_node(ni);	/* reclaim ref */
448 		} else
449 			mprev = &m->m_nextpkt;
450 	}
451 	/* recalculate tail ptr */
452 	m = ifq->ifq_head;
453 	for (; m != NULL && m->m_nextpkt != NULL; m = m->m_nextpkt)
454 		;
455 	ifq->ifq_tail = m;
456 }
457 
458 /*
459  * As above, for mbufs allocated with m_gethdr/MGETHDR
460  * or initialized by M_COPY_PKTHDR.
461  */
462 #define	MC_ALIGN(m, len)						\
463 do {									\
464 	(m)->m_data += (MCLBYTES - (len)) &~ (sizeof(long) - 1);	\
465 } while (/* CONSTCOND */ 0)
466 
467 /*
468  * Allocate and setup a management frame of the specified
469  * size.  We return the mbuf and a pointer to the start
470  * of the contiguous data area that's been reserved based
471  * on the packet length.  The data area is forced to 32-bit
472  * alignment and the buffer length to a multiple of 4 bytes.
473  * This is done mainly so beacon frames (that require this)
474  * can use this interface too.
475  */
476 struct mbuf *
477 ieee80211_getmgtframe(uint8_t **frm, int headroom, int pktlen)
478 {
479 	struct mbuf *m;
480 	u_int len;
481 
482 	/*
483 	 * NB: we know the mbuf routines will align the data area
484 	 *     so we don't need to do anything special.
485 	 */
486 	len = roundup2(headroom + pktlen, 4);
487 	KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len));
488 	if (len < MINCLSIZE) {
489 		m = m_gethdr(MB_DONTWAIT, MT_DATA);
490 		/*
491 		 * Align the data in case additional headers are added.
492 		 * This should only happen when a WEP header is added
493 		 * which only happens for shared key authentication mgt
494 		 * frames which all fit in MHLEN.
495 		 */
496 		if (m != NULL)
497 			MH_ALIGN(m, len);
498 	} else {
499 		m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
500 		if (m != NULL)
501 			MC_ALIGN(m, len);
502 	}
503 	if (m != NULL) {
504 		m->m_data += headroom;
505 		*frm = m->m_data;
506 	}
507 	return m;
508 }
509 
510 /*
511  * Re-align the payload in the mbuf.  This is mainly used (right now)
512  * to handle IP header alignment requirements on certain architectures.
513  */
514 struct mbuf *
515 ieee80211_realign(struct ieee80211vap *vap, struct mbuf *m, size_t align)
516 {
517 	int pktlen, space;
518 	struct mbuf *n = NULL;
519 
520 	pktlen = m->m_pkthdr.len;
521 	space = pktlen + align;
522 	if (space < MINCLSIZE)
523 		n = m_gethdr(MB_DONTWAIT, MT_DATA);
524 #ifdef notyet
525 	else {
526 		n = m_getjcl(MB_DONTWAIT, MT_DATA, M_PKTHDR,
527 		    space <= MCLBYTES ?     MCLBYTES :
528 #if MJUMPAGESIZE != MCLBYTES
529 		    space <= MJUMPAGESIZE ? MJUMPAGESIZE :
530 #endif
531 		    space <= MJUM9BYTES ?   MJUM9BYTES : MJUM16BYTES);
532 	}
533 #endif
534 	if (__predict_true(n != NULL)) {
535 		m_move_pkthdr(n, m);
536 		n->m_data = (caddr_t)(ALIGN(n->m_data + align) - align);
537 		m_copydata(m, 0, pktlen, mtod(n, caddr_t));
538 		n->m_len = pktlen;
539 	} else {
540 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
541 		    mtod(m, const struct ieee80211_frame *), NULL,
542 		    "%s", "no mbuf to realign");
543 		vap->iv_stats.is_rx_badalign++;
544 	}
545 	m_freem(m);
546 	return n;
547 }
548 
549 int
550 ieee80211_add_callback(struct mbuf *m,
551 	void (*func)(struct ieee80211_node *, void *, int), void *arg)
552 {
553 	struct m_tag *mtag;
554 	struct ieee80211_cb *cb;
555 
556 	mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_CALLBACK,
557 			sizeof(struct ieee80211_cb), M_INTWAIT);
558 	if (mtag == NULL)
559 		return 0;
560 
561 	cb = (struct ieee80211_cb *)(mtag+1);
562 	cb->func = func;
563 	cb->arg = arg;
564 	m_tag_prepend(m, mtag);
565 	m->m_flags |= M_TXCB;
566 	return 1;
567 }
568 
569 void
570 ieee80211_process_callback(struct ieee80211_node *ni,
571 	struct mbuf *m, int status)
572 {
573 	struct m_tag *mtag;
574 
575 	mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_CALLBACK, NULL);
576 	if (mtag != NULL) {
577 		struct ieee80211_cb *cb = (struct ieee80211_cb *)(mtag+1);
578 		cb->func(ni, cb->arg, status);
579 	}
580 }
581 
582 #include <sys/libkern.h>
583 
584 void
585 get_random_bytes(void *p, size_t n)
586 {
587 	uint8_t *dp = p;
588 
589 	while (n > 0) {
590 		uint32_t v = karc4random();
591 		size_t nb = n > sizeof(uint32_t) ? sizeof(uint32_t) : n;
592 		bcopy(&v, dp, n > sizeof(uint32_t) ? sizeof(uint32_t) : n);
593 		dp += sizeof(uint32_t), n -= nb;
594 	}
595 }
596 
597 /*
598  * Helper function for events that pass just a single mac address.
599  */
600 static void
601 notify_macaddr(struct ifnet *ifp, int op, const uint8_t mac[IEEE80211_ADDR_LEN])
602 {
603 	struct ieee80211_join_event iev;
604 
605 	memset(&iev, 0, sizeof(iev));
606 	IEEE80211_ADDR_COPY(iev.iev_addr, mac);
607 	rt_ieee80211msg(ifp, op, &iev, sizeof(iev));
608 }
609 
610 void
611 ieee80211_notify_node_join(struct ieee80211_node *ni, int newassoc)
612 {
613 	struct ieee80211vap *vap = ni->ni_vap;
614 	struct ifnet *ifp = vap->iv_ifp;
615 
616 	IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode join",
617 	    (ni == vap->iv_bss) ? "bss " : "");
618 
619 	if (ni == vap->iv_bss) {
620 		notify_macaddr(ifp, newassoc ?
621 		    RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC, ni->ni_bssid);
622 		if_link_state_change(ifp);
623 	} else {
624 		notify_macaddr(ifp, newassoc ?
625 		    RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN, ni->ni_macaddr);
626 	}
627 }
628 
629 void
630 ieee80211_notify_node_leave(struct ieee80211_node *ni)
631 {
632 	struct ieee80211vap *vap = ni->ni_vap;
633 	struct ifnet *ifp = vap->iv_ifp;
634 
635 	IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode leave",
636 	    (ni == vap->iv_bss) ? "bss " : "");
637 
638 	if (ni == vap->iv_bss) {
639 		rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
640 		if_link_state_change(ifp);
641 	} else {
642 		/* fire off wireless event station leaving */
643 		notify_macaddr(ifp, RTM_IEEE80211_LEAVE, ni->ni_macaddr);
644 	}
645 }
646 
647 void
648 ieee80211_notify_scan_done(struct ieee80211vap *vap)
649 {
650 	struct ifnet *ifp = vap->iv_ifp;
651 
652 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n", "notify scan done");
653 
654 	/* dispatch wireless event indicating scan completed */
655 	rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
656 }
657 
658 void
659 ieee80211_notify_replay_failure(struct ieee80211vap *vap,
660 	const struct ieee80211_frame *wh, const struct ieee80211_key *k,
661 	u_int64_t rsc, int tid)
662 {
663 	struct ifnet *ifp = vap->iv_ifp;
664 
665 	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
666 	    "%s replay detected <rsc %ju, csc %ju, keyix %u rxkeyix %u>",
667 	    k->wk_cipher->ic_name, (intmax_t) rsc,
668 	    (intmax_t) k->wk_keyrsc[tid],
669 	    k->wk_keyix, k->wk_rxkeyix);
670 
671 	if (ifp != NULL) {		/* NB: for cipher test modules */
672 		struct ieee80211_replay_event iev;
673 
674 		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
675 		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
676 		iev.iev_cipher = k->wk_cipher->ic_cipher;
677 		if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE)
678 			iev.iev_keyix = k->wk_rxkeyix;
679 		else
680 			iev.iev_keyix = k->wk_keyix;
681 		iev.iev_keyrsc = k->wk_keyrsc[tid];
682 		iev.iev_rsc = rsc;
683 		rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
684 	}
685 }
686 
687 void
688 ieee80211_notify_michael_failure(struct ieee80211vap *vap,
689 	const struct ieee80211_frame *wh, u_int keyix)
690 {
691 	struct ifnet *ifp = vap->iv_ifp;
692 
693 	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
694 	    "michael MIC verification failed <keyix %u>", keyix);
695 	vap->iv_stats.is_rx_tkipmic++;
696 
697 	if (ifp != NULL) {		/* NB: for cipher test modules */
698 		struct ieee80211_michael_event iev;
699 
700 		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
701 		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
702 		iev.iev_cipher = IEEE80211_CIPHER_TKIP;
703 		iev.iev_keyix = keyix;
704 		rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev));
705 	}
706 }
707 
708 void
709 ieee80211_notify_wds_discover(struct ieee80211_node *ni)
710 {
711 	struct ieee80211vap *vap = ni->ni_vap;
712 	struct ifnet *ifp = vap->iv_ifp;
713 
714 	notify_macaddr(ifp, RTM_IEEE80211_WDS, ni->ni_macaddr);
715 }
716 
717 void
718 ieee80211_notify_csa(struct ieee80211com *ic,
719 	const struct ieee80211_channel *c, int mode, int count)
720 {
721 	struct ifnet *ifp = ic->ic_ifp;
722 	struct ieee80211_csa_event iev;
723 
724 	memset(&iev, 0, sizeof(iev));
725 	iev.iev_flags = c->ic_flags;
726 	iev.iev_freq = c->ic_freq;
727 	iev.iev_ieee = c->ic_ieee;
728 	iev.iev_mode = mode;
729 	iev.iev_count = count;
730 	rt_ieee80211msg(ifp, RTM_IEEE80211_CSA, &iev, sizeof(iev));
731 }
732 
733 void
734 ieee80211_notify_radar(struct ieee80211com *ic,
735 	const struct ieee80211_channel *c)
736 {
737 	struct ifnet *ifp = ic->ic_ifp;
738 	struct ieee80211_radar_event iev;
739 
740 	memset(&iev, 0, sizeof(iev));
741 	iev.iev_flags = c->ic_flags;
742 	iev.iev_freq = c->ic_freq;
743 	iev.iev_ieee = c->ic_ieee;
744 	rt_ieee80211msg(ifp, RTM_IEEE80211_RADAR, &iev, sizeof(iev));
745 }
746 
747 void
748 ieee80211_notify_cac(struct ieee80211com *ic,
749 	const struct ieee80211_channel *c, enum ieee80211_notify_cac_event type)
750 {
751 	struct ifnet *ifp = ic->ic_ifp;
752 	struct ieee80211_cac_event iev;
753 
754 	memset(&iev, 0, sizeof(iev));
755 	iev.iev_flags = c->ic_flags;
756 	iev.iev_freq = c->ic_freq;
757 	iev.iev_ieee = c->ic_ieee;
758 	iev.iev_type = type;
759 	rt_ieee80211msg(ifp, RTM_IEEE80211_CAC, &iev, sizeof(iev));
760 }
761 
762 void
763 ieee80211_notify_node_deauth(struct ieee80211_node *ni)
764 {
765 	struct ieee80211vap *vap = ni->ni_vap;
766 	struct ifnet *ifp = vap->iv_ifp;
767 
768 	IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node deauth");
769 
770 	notify_macaddr(ifp, RTM_IEEE80211_DEAUTH, ni->ni_macaddr);
771 }
772 
773 void
774 ieee80211_notify_node_auth(struct ieee80211_node *ni)
775 {
776 	struct ieee80211vap *vap = ni->ni_vap;
777 	struct ifnet *ifp = vap->iv_ifp;
778 
779 	IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node auth");
780 
781 	notify_macaddr(ifp, RTM_IEEE80211_AUTH, ni->ni_macaddr);
782 }
783 
784 void
785 ieee80211_notify_country(struct ieee80211vap *vap,
786 	const uint8_t bssid[IEEE80211_ADDR_LEN], const uint8_t cc[2])
787 {
788 	struct ifnet *ifp = vap->iv_ifp;
789 	struct ieee80211_country_event iev;
790 
791 	memset(&iev, 0, sizeof(iev));
792 	IEEE80211_ADDR_COPY(iev.iev_addr, bssid);
793 	iev.iev_cc[0] = cc[0];
794 	iev.iev_cc[1] = cc[1];
795 	rt_ieee80211msg(ifp, RTM_IEEE80211_COUNTRY, &iev, sizeof(iev));
796 }
797 
798 void
799 ieee80211_notify_radio(struct ieee80211com *ic, int state)
800 {
801 	struct ifnet *ifp = ic->ic_ifp;
802 	struct ieee80211_radio_event iev;
803 
804 	memset(&iev, 0, sizeof(iev));
805 	iev.iev_state = state;
806 	rt_ieee80211msg(ifp, RTM_IEEE80211_RADIO, &iev, sizeof(iev));
807 }
808 
809 int
810 ieee80211_handoff(struct ifnet *dst_ifp, struct mbuf *m)
811 {
812         struct mbuf *m0;
813 
814 	/* We may be sending a fragment so traverse the mbuf */
815 	wlan_assert_serialized();
816 	wlan_serialize_exit();
817 	for (; m; m = m0) {
818 		struct altq_pktattr pktattr;
819 
820 		m0 = m->m_nextpkt;
821 		m->m_nextpkt = NULL;
822 
823 		if (ifq_is_enabled(&dst_ifp->if_snd))
824 			altq_etherclassify(&dst_ifp->if_snd, m, &pktattr);
825 
826 		ifq_dispatch(dst_ifp, m, &pktattr);
827 	}
828 	wlan_serialize_enter();
829 
830 	return (0);
831 }
832 
833 /* IEEE Std 802.11a-1999, page 9, table 79 */
834 #define IEEE80211_OFDM_SYM_TIME                 4
835 #define IEEE80211_OFDM_PREAMBLE_TIME            16
836 #define IEEE80211_OFDM_SIGNAL_TIME              4
837 /* IEEE Std 802.11g-2003, page 44 */
838 #define IEEE80211_OFDM_SIGNAL_EXT_TIME          6
839 
840 /* IEEE Std 802.11a-1999, page 7, figure 107 */
841 #define IEEE80211_OFDM_PLCP_SERVICE_NBITS       16
842 #define IEEE80211_OFDM_TAIL_NBITS               6
843 
844 #define IEEE80211_OFDM_NBITS(frmlen) \
845 	(IEEE80211_OFDM_PLCP_SERVICE_NBITS + \
846 	((frmlen) * NBBY) + \
847 	IEEE80211_OFDM_TAIL_NBITS)
848 
849 #define IEEE80211_OFDM_NBITS_PER_SYM(kbps) \
850 	(((kbps) * IEEE80211_OFDM_SYM_TIME) / 1000)
851 
852 #define IEEE80211_OFDM_NSYMS(kbps, frmlen) \
853 	howmany(IEEE80211_OFDM_NBITS((frmlen)), \
854 	IEEE80211_OFDM_NBITS_PER_SYM((kbps)))
855 
856 #define IEEE80211_OFDM_TXTIME(kbps, frmlen) \
857 	(IEEE80211_OFDM_PREAMBLE_TIME + \
858 	IEEE80211_OFDM_SIGNAL_TIME + \
859 	(IEEE80211_OFDM_NSYMS((kbps), (frmlen)) * IEEE80211_OFDM_SYM_TIME))
860 
861 /* IEEE Std 802.11b-1999, page 28, subclause 18.3.4 */
862 #define IEEE80211_CCK_PREAMBLE_LEN      144
863 #define IEEE80211_CCK_PLCP_HDR_TIME     48
864 #define IEEE80211_CCK_SHPREAMBLE_LEN    72
865 #define IEEE80211_CCK_SHPLCP_HDR_TIME   24
866 
867 #define IEEE80211_CCK_NBITS(frmlen)     ((frmlen) * NBBY)
868 #define IEEE80211_CCK_TXTIME(kbps, frmlen) \
869 	(((IEEE80211_CCK_NBITS((frmlen)) * 1000) + (kbps) - 1) / (kbps))
870 
871 uint16_t
872 ieee80211_txtime(struct ieee80211_node *ni, u_int len, uint8_t rs_rate,
873 		uint32_t flags)
874 {
875 	struct ieee80211vap *vap = ni->ni_vap;
876 	uint16_t txtime;
877 	int rate;
878 
879 	rs_rate &= IEEE80211_RATE_VAL;
880 	rate = rs_rate * 500;   /* ieee80211 rate -> kbps */
881 
882 	if (vap->iv_ic->ic_phytype == IEEE80211_T_OFDM) {
883 		/*
884 		 * IEEE Std 802.11a-1999, page 37, equation (29)
885 		 * IEEE Std 802.11g-2003, page 44, equation (42)
886 		 */
887 		txtime = IEEE80211_OFDM_TXTIME(rate, len);
888 		if (vap->iv_ic->ic_curmode == IEEE80211_MODE_11G)
889 			txtime += IEEE80211_OFDM_SIGNAL_EXT_TIME;
890 	} else {
891 		/*
892 		 * IEEE Std 802.11b-1999, page 28, subclause 18.3.4
893 		 * IEEE Std 802.11g-2003, page 45, equation (43)
894 		 */
895 		if (vap->iv_ic->ic_phytype == IEEE80211_T_OFDM_QUARTER+1)
896 			++len;
897 		txtime = IEEE80211_CCK_TXTIME(rate, len);
898 
899 		/*
900 		 * Short preamble is not applicable for DS 1Mbits/s
901 		 */
902 		if (rs_rate != 2 && (flags & IEEE80211_F_SHPREAMBLE)) {
903 			txtime += IEEE80211_CCK_SHPREAMBLE_LEN +
904 				IEEE80211_CCK_SHPLCP_HDR_TIME;
905 		} else {
906 			txtime += IEEE80211_CCK_PREAMBLE_LEN +
907 			IEEE80211_CCK_PLCP_HDR_TIME;
908 		}
909 	}
910 	return txtime;
911 }
912 
913 void
914 ieee80211_load_module(const char *modname)
915 {
916 
917 #ifdef notyet
918 	(void)kern_kldload(curthread, modname, NULL);
919 #else
920 	kprintf("%s: load the %s module by hand for now.\n", __func__, modname);
921 #endif
922 }
923 
924 static eventhandler_tag wlan_bpfevent;
925 static eventhandler_tag wlan_ifllevent;
926 
927 static void
928 bpf_track_event(void *arg, struct ifnet *ifp, int dlt, int attach)
929 {
930 	/* NB: identify vap's by if_start */
931 
932 	wlan_serialize_enter();
933 	if (dlt == DLT_IEEE802_11_RADIO && ifp->if_start == ieee80211_start) {
934 		struct ieee80211vap *vap = ifp->if_softc;
935 		/*
936 		 * Track bpf radiotap listener state.  We mark the vap
937 		 * to indicate if any listener is present and the com
938 		 * to indicate if any listener exists on any associated
939 		 * vap.  This flag is used by drivers to prepare radiotap
940 		 * state only when needed.
941 		 */
942 		if (attach) {
943 			ieee80211_syncflag_ext(vap, IEEE80211_FEXT_BPF);
944 			if (vap->iv_opmode == IEEE80211_M_MONITOR)
945 				atomic_add_int(&vap->iv_ic->ic_montaps, 1);
946 		} else if (!vap->iv_rawbpf) {
947 			ieee80211_syncflag_ext(vap, -IEEE80211_FEXT_BPF);
948 			if (vap->iv_opmode == IEEE80211_M_MONITOR)
949 				atomic_subtract_int(&vap->iv_ic->ic_montaps, 1);
950 		}
951 	}
952 	wlan_serialize_exit();
953 }
954 
955 static void
956 wlan_iflladdr_event(void *arg __unused, struct ifnet *ifp)
957 {
958 	struct ieee80211com *ic = ifp->if_l2com;
959 	struct ieee80211vap *vap, *next;
960 
961 	wlan_serialize_enter();
962 	if (ifp->if_type != IFT_IEEE80211 || ic == NULL) {
963 		wlan_serialize_exit();
964 		return;
965 	}
966 
967 	TAILQ_FOREACH_MUTABLE(vap, &ic->ic_vaps, iv_next, next) {
968 		/*
969 		 * If the MAC address has changed on the parent and it was
970 		 * copied to the vap on creation then re-sync.
971 		 */
972 		if (vap->iv_ic == ic &&
973 		    (vap->iv_flags_ext & IEEE80211_FEXT_UNIQMAC) == 0) {
974 			IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp));
975 			wlan_serialize_exit();
976 			if_setlladdr(vap->iv_ifp, IF_LLADDR(ifp),
977 				     IEEE80211_ADDR_LEN);
978 			wlan_serialize_enter();
979 		}
980 	}
981 	wlan_serialize_exit();
982 }
983 
984 /*
985  * Module glue.
986  *
987  * NB: the module name is "wlan" for compatibility with NetBSD.
988  */
989 static int
990 wlan_modevent(module_t mod, int type, void *unused)
991 {
992 	int error;
993 
994 	wlan_serialize_enter();
995 
996 	switch (type) {
997 	case MOD_LOAD:
998 		if (bootverbose)
999 			kprintf("wlan: <802.11 Link Layer>\n");
1000 		wlan_bpfevent = EVENTHANDLER_REGISTER(bpf_track,
1001 					bpf_track_event, 0,
1002 					EVENTHANDLER_PRI_ANY);
1003 		if (wlan_bpfevent == NULL) {
1004 			error = ENOMEM;
1005 			break;
1006 		}
1007 		wlan_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
1008 					wlan_iflladdr_event, NULL,
1009 					EVENTHANDLER_PRI_ANY);
1010 		if (wlan_ifllevent == NULL) {
1011 			EVENTHANDLER_DEREGISTER(bpf_track, wlan_bpfevent);
1012 			error = ENOMEM;
1013 			break;
1014 		}
1015 		if_clone_attach(&wlan_cloner);
1016 		if_register_com_alloc(IFT_IEEE80211, wlan_alloc, wlan_free);
1017 		error = 0;
1018 		break;
1019 	case MOD_UNLOAD:
1020 		if_deregister_com_alloc(IFT_IEEE80211);
1021 		if_clone_detach(&wlan_cloner);
1022 		EVENTHANDLER_DEREGISTER(bpf_track, wlan_bpfevent);
1023 		EVENTHANDLER_DEREGISTER(iflladdr_event, wlan_ifllevent);
1024 		error = 0;
1025 		break;
1026 	default:
1027 		error = EINVAL;
1028 		break;
1029 	}
1030 	wlan_serialize_exit();
1031 
1032 	return error;
1033 }
1034 
1035 static moduledata_t wlan_mod = {
1036 	"wlan",
1037 	wlan_modevent,
1038 	0
1039 };
1040 DECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
1041 MODULE_VERSION(wlan, 1);
1042 MODULE_DEPEND(wlan, ether, 1, 1, 1);
1043