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