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