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