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