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