e232c51f0de6d7139bc6c59eebc63cb7e42365ba
[dragonfly.git] / sys / netproto / 802_11 / wlan / ieee80211_dragonfly.c
1 /*-
2  * Copyright (c) 2003-2009 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * $FreeBSD: head/sys/net80211/ieee80211_freebsd.c 202612 2010-01-19 05:00:57Z thompsa $
26  * $DragonFly$
27  */
28
29 /*
30  * IEEE 802.11 support (DragonFlyBSD-specific code)
31  */
32 #include "opt_wlan.h"
33
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h> 
37 #include <sys/linker.h>
38 #include <sys/mbuf.h>   
39 #include <sys/module.h>
40 #include <sys/proc.h>
41 #include <sys/sysctl.h>
42
43 #include <sys/socket.h>
44
45 #include <net/bpf.h>
46 #include <net/if.h>
47 #include <net/if_dl.h>
48 #include <net/if_clone.h>
49 #include <net/if_media.h>
50 #include <net/if_types.h>
51 #include <net/ethernet.h>
52 #include <net/route.h>
53 #include <net/ifq_var.h>
54
55 #include <netproto/802_11/ieee80211_var.h>
56 #include <netproto/802_11/ieee80211_input.h>
57
58 SYSCTL_NODE(_net, OID_AUTO, wlan, CTLFLAG_RD, 0, "IEEE 80211 parameters");
59
60 #ifdef IEEE80211_DEBUG
61 int     ieee80211_debug = 0;
62 SYSCTL_INT(_net_wlan, OID_AUTO, debug, CTLFLAG_RW, &ieee80211_debug,
63             0, "debugging printfs");
64 #endif
65
66 MALLOC_DEFINE(M_80211_COM, "80211com", "802.11 com state");
67
68
69 static void     wlan_clone_destroy(struct ifnet *);
70 static int      wlan_clone_create(struct if_clone *, int, caddr_t);
71
72 static struct if_clone wlan_cloner = 
73         IF_CLONE_INITIALIZER("wlan", wlan_clone_create, wlan_clone_destroy,
74             0, IF_MAXUNIT);
75
76 struct lwkt_serialize wlan_global_serializer = LWKT_SERIALIZE_INITIALIZER;
77
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  */
84 static void *
85 wlan_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
95 static void
96 wlan_free(void *ic, u_char type)
97 {
98         kfree(ic, M_80211_COM);
99 }
100
101 static int
102 wlan_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
149 static void
150 wlan_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 }
157
158 /*
159  * These serializer functions are used by wlan and all drivers.
160  */
161 void
162 wlan_serialize_enter(void)
163 {
164         lwkt_serialize_enter(&wlan_global_serializer);
165 }
166
167 void
168 wlan_serialize_exit(void)
169 {
170         lwkt_serialize_exit(&wlan_global_serializer);
171 }
172
173 int
174 wlan_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  */
183 void
184 wlan_cv_init(struct cv *cv, const char *desc)
185 {
186         cv->cv_desc = desc;
187         cv->cv_waiters = 0;
188 }
189
190 int
191 wlan_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
200 void
201 wlan_cv_wait(struct cv *cv)
202 {
203         ++cv->cv_waiters;
204         wlan_serialize_sleep(cv, 0, cv->cv_desc, 0);
205 }
206
207 void
208 wlan_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  */
224 void
225 ieee80211_vap_destroy(struct ieee80211vap *vap)
226 {
227         if_clone_destroy(vap->iv_ifp->if_xname);
228 }
229
230 int
231 ieee80211_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
244 static int
245 ieee80211_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
257 static int
258 ieee80211_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
266 static int
267 ieee80211_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;
275         ieee80211_dfs_notify_radar(ic, ic->ic_curchan);
276         return 0;
277 }
278
279 void
280 ieee80211_sysctl_attach(struct ieee80211com *ic)
281 {
282 }
283
284 void
285 ieee80211_sysctl_detach(struct ieee80211com *ic)
286 {
287 }
288
289 void
290 ieee80211_sysctl_vattach(struct ieee80211vap *vap)
291 {
292         struct ifnet *ifp = vap->iv_ifp;
293         struct sysctl_ctx_list *ctx;
294         struct sysctl_oid *oid;
295         char num[14];                   /* sufficient for 32 bits */
296
297         ctx = (struct sysctl_ctx_list *) kmalloc(sizeof(struct sysctl_ctx_list),
298                 M_DEVBUF, M_INTWAIT | M_ZERO);
299         if (ctx == NULL) {
300                 if_printf(ifp, "%s: cannot allocate sysctl context!\n",
301                         __func__);
302                 return;
303         }
304         sysctl_ctx_init(ctx);
305         ksnprintf(num, sizeof(num), "%u", ifp->if_dunit);
306         oid = SYSCTL_ADD_NODE(ctx, &SYSCTL_NODE_CHILDREN(_net, wlan),
307                 OID_AUTO, num, CTLFLAG_RD, NULL, "");
308         SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
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");
314 #ifdef IEEE80211_DEBUG
315         vap->iv_debug = ieee80211_debug;
316         SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
317                 "debug", CTLFLAG_RW, &vap->iv_debug, 0,
318                 "control debugging printfs");
319 #endif
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");
323         /* XXX inherit from tunables */
324         SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
325                 "inact_run", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_run, 0,
326                 ieee80211_sysctl_inact, "I",
327                 "station inactivity timeout (sec)");
328         SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
329                 "inact_probe", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_probe, 0,
330                 ieee80211_sysctl_inact, "I",
331                 "station inactivity probe timeout (sec)");
332         SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
333                 "inact_auth", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_auth, 0,
334                 ieee80211_sysctl_inact, "I",
335                 "station authentication timeout (sec)");
336         SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
337                 "inact_init", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_init, 0,
338                 ieee80211_sysctl_inact, "I",
339                 "station initial state timeout (sec)");
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;
365 }
366
367 void
368 ieee80211_sysctl_vdetach(struct ieee80211vap *vap)
369 {
370
371         if (vap->iv_sysctl != NULL) {
372                 sysctl_ctx_free(vap->iv_sysctl);
373                 kfree(vap->iv_sysctl, M_DEVBUF);
374                 vap->iv_sysctl = NULL;
375         }
376 }
377
378 int
379 ieee80211_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
386 void
387 ieee80211_drain_ifq(struct ifqueue *ifq)
388 {
389         struct ieee80211_node *ni;
390         struct mbuf *m;
391
392         wlan_assert_serialized();
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
407 void
408 ieee80211_flush_ifq(struct ifqueue *ifq, struct ieee80211vap *vap)
409 {
410         struct ieee80211_node *ni;
411         struct mbuf *m, **mprev;
412
413         wlan_assert_serialized();
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;
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)                                                \
438 do {                                                                    \
439         (m)->m_data += (MCLBYTES - (len)) &~ (sizeof(long) - 1);        \
440 } while (/* CONSTCOND */ 0)
441
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  */
451 struct mbuf *
452 ieee80211_getmgtframe(uint8_t **frm, int headroom, int pktlen)
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          */
461         len = roundup2(headroom + pktlen, 4);
462         KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len));
463         if (len < MINCLSIZE) {
464                 m = m_gethdr(MB_DONTWAIT, MT_DATA);
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);
473         } else {
474                 m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
475                 if (m != NULL)
476                         MC_ALIGN(m, len);
477         }
478         if (m != NULL) {
479                 m->m_data += headroom;
480                 *frm = m->m_data;
481         }
482         return m;
483 }
484
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  */
489 struct mbuf *
490 ieee80211_realign(struct ieee80211vap *vap, struct mbuf *m, size_t align)
491 {
492         int pktlen, space;
493         struct mbuf *n = NULL;
494
495         pktlen = m->m_pkthdr.len;
496         space = pktlen + align;
497         if (space < MINCLSIZE)
498                 n = m_gethdr(MB_DONTWAIT, MT_DATA);
499 #ifdef notyet
500         else {
501                 n = m_getjcl(MB_DONTWAIT, MT_DATA, M_PKTHDR,
502                     space <= MCLBYTES ?     MCLBYTES :
503 #if MJUMPAGESIZE != MCLBYTES
504                     space <= MJUMPAGESIZE ? MJUMPAGESIZE :
505 #endif
506                     space <= MJUM9BYTES ?   MJUM9BYTES : MJUM16BYTES);
507         }
508 #endif
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
524 int
525 ieee80211_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,
532                         sizeof(struct ieee80211_cb), M_INTWAIT);
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
544 void
545 ieee80211_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
557 #include <sys/libkern.h>
558
559 void
560 get_random_bytes(void *p, size_t n)
561 {
562         uint8_t *dp = p;
563
564         while (n > 0) {
565                 uint32_t v = karc4random();
566                 size_t nb = n > sizeof(uint32_t) ? sizeof(uint32_t) : n;
567                 bcopy(&v, dp, n > sizeof(uint32_t) ? sizeof(uint32_t) : n);
568                 dp += sizeof(uint32_t), n -= nb;
569         }
570 }
571
572 /*
573  * Helper function for events that pass just a single mac address.
574  */
575 static void
576 notify_macaddr(struct ifnet *ifp, int op, const uint8_t mac[IEEE80211_ADDR_LEN])
577 {
578         struct ieee80211_join_event iev;
579
580         memset(&iev, 0, sizeof(iev));
581         IEEE80211_ADDR_COPY(iev.iev_addr, mac);
582         rt_ieee80211msg(ifp, op, &iev, sizeof(iev));
583 }
584
585 void
586 ieee80211_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
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);
597                 if_link_state_change(ifp);
598         } else {
599                 notify_macaddr(ifp, newassoc ?
600                     RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN, ni->ni_macaddr);
601         }
602 }
603
604 void
605 ieee80211_notify_node_leave(struct ieee80211_node *ni)
606 {
607         struct ieee80211vap *vap = ni->ni_vap;
608         struct ifnet *ifp = vap->iv_ifp;
609
610         IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode leave",
611             (ni == vap->iv_bss) ? "bss " : "");
612
613         if (ni == vap->iv_bss) {
614                 rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
615                 if_link_state_change(ifp);
616         } else {
617                 /* fire off wireless event station leaving */
618                 notify_macaddr(ifp, RTM_IEEE80211_LEAVE, ni->ni_macaddr);
619         }
620 }
621
622 void
623 ieee80211_notify_scan_done(struct ieee80211vap *vap)
624 {
625         struct ifnet *ifp = vap->iv_ifp;
626
627         IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n", "notify scan done");
628
629         /* dispatch wireless event indicating scan completed */
630         rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
631 }
632
633 void
634 ieee80211_notify_replay_failure(struct ieee80211vap *vap,
635         const struct ieee80211_frame *wh, const struct ieee80211_key *k,
636         u_int64_t rsc, int tid)
637 {
638         struct ifnet *ifp = vap->iv_ifp;
639
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],
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;
656                 iev.iev_keyrsc = k->wk_keyrsc[tid];
657                 iev.iev_rsc = rsc;
658                 rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
659         }
660 }
661
662 void
663 ieee80211_notify_michael_failure(struct ieee80211vap *vap,
664         const struct ieee80211_frame *wh, u_int keyix)
665 {
666         struct ifnet *ifp = vap->iv_ifp;
667
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++;
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
683 void
684 ieee80211_notify_wds_discover(struct ieee80211_node *ni)
685 {
686         struct ieee80211vap *vap = ni->ni_vap;
687         struct ifnet *ifp = vap->iv_ifp;
688
689         notify_macaddr(ifp, RTM_IEEE80211_WDS, ni->ni_macaddr);
690 }
691
692 void
693 ieee80211_notify_csa(struct ieee80211com *ic,
694         const struct ieee80211_channel *c, int mode, int count)
695 {
696         struct ifnet *ifp = ic->ic_ifp;
697         struct ieee80211_csa_event iev;
698
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));
706 }
707
708 void
709 ieee80211_notify_radar(struct ieee80211com *ic,
710         const struct ieee80211_channel *c)
711 {
712         struct ifnet *ifp = ic->ic_ifp;
713         struct ieee80211_radar_event iev;
714
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 }
721
722 void
723 ieee80211_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;
728
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
737 void
738 ieee80211_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
748 void
749 ieee80211_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
759 void
760 ieee80211_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
773 void
774 ieee80211_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
784 int
785 ieee80211_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
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
843 uint16_t
844 ieee80211_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
885 void
886 ieee80211_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
896 static eventhandler_tag wlan_bpfevent;
897 static eventhandler_tag wlan_ifllevent;
898
899 static void
900 bpf_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;
905                 /*
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.
911                  */
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);
916                 } else if (!vap->iv_rawbpf) {
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);
920                 }
921         }
922 }
923
924 static void
925 wlan_iflladdr(void *arg __unused, struct ifnet *ifp)
926 {
927         struct ieee80211com *ic = ifp->if_l2com;
928         struct ieee80211vap *vap, *next;
929
930         if (ifp->if_type != IFT_IEEE80211 || ic == NULL)
931                 return;
932
933         TAILQ_FOREACH_MUTABLE(vap, &ic->ic_vaps, iv_next, next) {
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));
941                         wlan_serialize_exit();
942                         if_setlladdr(vap->iv_ifp, IF_LLADDR(ifp),
943                                      IEEE80211_ADDR_LEN);
944                         wlan_serialize_enter();
945                 }
946         }
947 }
948
949 /*
950  * Module glue.
951  *
952  * NB: the module name is "wlan" for compatibility with NetBSD.
953  */
954 static int
955 wlan_modevent(module_t mod, int type, void *unused)
956 {
957         switch (type) {
958         case MOD_LOAD:
959                 if (bootverbose)
960                         kprintf("wlan: <802.11 Link Layer>\n");
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);
972                 if_register_com_alloc(IFT_IEEE80211, wlan_alloc, wlan_free);
973                 return 0;
974         case MOD_UNLOAD:
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);
979                 return 0;
980         }
981         return EINVAL;
982 }
983
984 static moduledata_t wlan_mod = {
985         "wlan",
986         wlan_modevent,
987         0
988 };
989 DECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
990 MODULE_VERSION(wlan, 1);
991 MODULE_DEPEND(wlan, ether, 1, 1, 1);