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