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