2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003-2009 Sam Leffler, Errno Consulting
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * IEEE 802.11 support (FreeBSD-specific code)
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>
43 #include <sys/module.h>
45 #include <sys/sysctl.h>
47 #include <sys/socket.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>
60 #include <net80211/ieee80211_var.h>
61 #include <net80211/ieee80211_input.h>
63 SYSCTL_NODE(_net, OID_AUTO, wlan, CTLFLAG_RD, 0, "IEEE 80211 parameters");
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");
71 static MALLOC_DEFINE(M_80211_COM, "80211com", "802.11 com state");
73 static const char wlanname[] = "wlan";
74 static struct if_clone *wlan_cloner;
77 wlan_clone_create(struct if_clone *ifc, int unit, caddr_t params)
79 struct ieee80211_clone_params cp;
80 struct ieee80211vap *vap;
81 struct ieee80211com *ic;
84 error = copyin(params, &cp, sizeof(cp));
87 ic = ieee80211_find_com(cp.icp_parent);
90 if (cp.icp_opmode >= IEEE80211_OPMODE_MAX) {
91 ic_printf(ic, "%s: invalid opmode %d\n", __func__,
95 if ((ic->ic_caps & ieee80211_opcap[cp.icp_opmode]) == 0) {
96 ic_printf(ic, "%s mode not supported\n",
97 ieee80211_opmode_name[cp.icp_opmode]);
100 if ((cp.icp_flags & IEEE80211_CLONE_TDMA) &&
101 #ifdef IEEE80211_SUPPORT_TDMA
102 (ic->ic_caps & IEEE80211_C_TDMA) == 0
107 ic_printf(ic, "TDMA not supported\n");
110 vap = ic->ic_vap_create(ic, wlanname, unit,
111 cp.icp_opmode, cp.icp_flags, cp.icp_bssid,
112 cp.icp_flags & IEEE80211_CLONE_MACADDR ?
113 cp.icp_macaddr : ic->ic_macaddr);
115 return (vap == NULL ? EIO : 0);
119 wlan_clone_destroy(struct ifnet *ifp)
121 struct ieee80211vap *vap = ifp->if_softc;
122 struct ieee80211com *ic = vap->iv_ic;
124 ic->ic_vap_delete(vap);
128 ieee80211_vap_destroy(struct ieee80211vap *vap)
130 CURVNET_SET(vap->iv_ifp->if_vnet);
131 if_clone_destroyif(wlan_cloner, vap->iv_ifp);
136 ieee80211_sysctl_msecs_ticks(SYSCTL_HANDLER_ARGS)
138 int msecs = ticks_to_msecs(*(int *)arg1);
141 error = sysctl_handle_int(oidp, &msecs, 0, req);
142 if (error || !req->newptr)
144 *(int *)arg1 = msecs_to_ticks(msecs);
149 ieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS)
151 int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT;
154 error = sysctl_handle_int(oidp, &inact, 0, req);
155 if (error || !req->newptr)
157 *(int *)arg1 = inact / IEEE80211_INACT_WAIT;
162 ieee80211_sysctl_parent(SYSCTL_HANDLER_ARGS)
164 struct ieee80211com *ic = arg1;
166 return SYSCTL_OUT_STR(req, ic->ic_name);
170 ieee80211_sysctl_radar(SYSCTL_HANDLER_ARGS)
172 struct ieee80211com *ic = arg1;
175 error = sysctl_handle_int(oidp, &t, 0, req);
176 if (error || !req->newptr)
179 ieee80211_dfs_notify_radar(ic, ic->ic_curchan);
180 IEEE80211_UNLOCK(ic);
185 * For now, just restart everything.
187 * Later on, it'd be nice to have a separate VAP restart to
188 * full-device restart.
191 ieee80211_sysctl_vap_restart(SYSCTL_HANDLER_ARGS)
193 struct ieee80211vap *vap = arg1;
196 error = sysctl_handle_int(oidp, &t, 0, req);
197 if (error || !req->newptr)
200 ieee80211_restart_all(vap->iv_ic);
205 ieee80211_sysctl_attach(struct ieee80211com *ic)
210 ieee80211_sysctl_detach(struct ieee80211com *ic)
215 ieee80211_sysctl_vattach(struct ieee80211vap *vap)
217 struct ifnet *ifp = vap->iv_ifp;
218 struct sysctl_ctx_list *ctx;
219 struct sysctl_oid *oid;
220 char num[14]; /* sufficient for 32 bits */
222 ctx = (struct sysctl_ctx_list *) IEEE80211_MALLOC(sizeof(struct sysctl_ctx_list),
223 M_DEVBUF, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
225 if_printf(ifp, "%s: cannot allocate sysctl context!\n",
229 sysctl_ctx_init(ctx);
230 snprintf(num, sizeof(num), "%u", ifp->if_dunit);
231 oid = SYSCTL_ADD_NODE(ctx, &SYSCTL_NODE_CHILDREN(_net, wlan),
232 OID_AUTO, num, CTLFLAG_RD, NULL, "");
233 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
234 "%parent", CTLTYPE_STRING | CTLFLAG_RD, vap->iv_ic, 0,
235 ieee80211_sysctl_parent, "A", "parent device");
236 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
237 "driver_caps", CTLFLAG_RW, &vap->iv_caps, 0,
238 "driver capabilities");
239 #ifdef IEEE80211_DEBUG
240 vap->iv_debug = ieee80211_debug;
241 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
242 "debug", CTLFLAG_RW, &vap->iv_debug, 0,
243 "control debugging printfs");
245 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
246 "bmiss_max", CTLFLAG_RW, &vap->iv_bmiss_max, 0,
247 "consecutive beacon misses before scanning");
248 /* XXX inherit from tunables */
249 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
250 "inact_run", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_run, 0,
251 ieee80211_sysctl_inact, "I",
252 "station inactivity timeout (sec)");
253 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
254 "inact_probe", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_probe, 0,
255 ieee80211_sysctl_inact, "I",
256 "station inactivity probe timeout (sec)");
257 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
258 "inact_auth", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_auth, 0,
259 ieee80211_sysctl_inact, "I",
260 "station authentication timeout (sec)");
261 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
262 "inact_init", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_init, 0,
263 ieee80211_sysctl_inact, "I",
264 "station initial state timeout (sec)");
265 if (vap->iv_htcaps & IEEE80211_HTC_HT) {
266 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
267 "ampdu_mintraffic_bk", CTLFLAG_RW,
268 &vap->iv_ampdu_mintraffic[WME_AC_BK], 0,
269 "BK traffic tx aggr threshold (pps)");
270 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
271 "ampdu_mintraffic_be", CTLFLAG_RW,
272 &vap->iv_ampdu_mintraffic[WME_AC_BE], 0,
273 "BE traffic tx aggr threshold (pps)");
274 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
275 "ampdu_mintraffic_vo", CTLFLAG_RW,
276 &vap->iv_ampdu_mintraffic[WME_AC_VO], 0,
277 "VO traffic tx aggr threshold (pps)");
278 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
279 "ampdu_mintraffic_vi", CTLFLAG_RW,
280 &vap->iv_ampdu_mintraffic[WME_AC_VI], 0,
281 "VI traffic tx aggr threshold (pps)");
284 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
285 "force_restart", CTLTYPE_INT | CTLFLAG_RW, vap, 0,
286 ieee80211_sysctl_vap_restart, "I",
287 "force a VAP restart");
289 if (vap->iv_caps & IEEE80211_C_DFS) {
290 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
291 "radar", CTLTYPE_INT | CTLFLAG_RW, vap->iv_ic, 0,
292 ieee80211_sysctl_radar, "I", "simulate radar event");
294 vap->iv_sysctl = ctx;
299 ieee80211_sysctl_vdetach(struct ieee80211vap *vap)
302 if (vap->iv_sysctl != NULL) {
303 sysctl_ctx_free(vap->iv_sysctl);
304 IEEE80211_FREE(vap->iv_sysctl, M_DEVBUF);
305 vap->iv_sysctl = NULL;
309 #define MS(_v, _f) (((_v) & _f##_M) >> _f##_S)
311 ieee80211_com_vincref(struct ieee80211vap *vap)
315 ostate = atomic_fetchadd_32(&vap->iv_com_state, IEEE80211_COM_REF_ADD);
317 if (ostate & IEEE80211_COM_DETACHED) {
318 atomic_subtract_32(&vap->iv_com_state, IEEE80211_COM_REF_ADD);
322 if (MS(ostate, IEEE80211_COM_REF) == IEEE80211_COM_REF_MAX) {
323 atomic_subtract_32(&vap->iv_com_state, IEEE80211_COM_REF_ADD);
331 ieee80211_com_vdecref(struct ieee80211vap *vap)
335 ostate = atomic_fetchadd_32(&vap->iv_com_state, -IEEE80211_COM_REF_ADD);
337 KASSERT(MS(ostate, IEEE80211_COM_REF) != 0,
338 ("com reference counter underflow"));
344 ieee80211_com_vdetach(struct ieee80211vap *vap)
348 sleep_time = msecs_to_ticks(250);
349 atomic_set_32(&vap->iv_com_state, IEEE80211_COM_DETACHED);
350 while (MS(atomic_load_32(&vap->iv_com_state), IEEE80211_COM_REF) != 0)
351 pause("comref", sleep_time);
356 ieee80211_node_dectestref(struct ieee80211_node *ni)
358 /* XXX need equivalent of atomic_dec_and_test */
359 atomic_subtract_int(&ni->ni_refcnt, 1);
360 return atomic_cmpset_int(&ni->ni_refcnt, 0, 1);
364 ieee80211_drain_ifq(struct ifqueue *ifq)
366 struct ieee80211_node *ni;
374 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
375 KASSERT(ni != NULL, ("frame w/o node"));
376 ieee80211_free_node(ni);
377 m->m_pkthdr.rcvif = NULL;
384 ieee80211_flush_ifq(struct ifqueue *ifq, struct ieee80211vap *vap)
386 struct ieee80211_node *ni;
387 struct mbuf *m, **mprev;
390 mprev = &ifq->ifq_head;
391 while ((m = *mprev) != NULL) {
392 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
393 if (ni != NULL && ni->ni_vap == vap) {
394 *mprev = m->m_nextpkt; /* remove from list */
398 ieee80211_free_node(ni); /* reclaim ref */
400 mprev = &m->m_nextpkt;
402 /* recalculate tail ptr */
404 for (; m != NULL && m->m_nextpkt != NULL; m = m->m_nextpkt)
411 * As above, for mbufs allocated with m_gethdr/MGETHDR
412 * or initialized by M_COPY_PKTHDR.
414 #define MC_ALIGN(m, len) \
416 (m)->m_data += rounddown2(MCLBYTES - (len), sizeof(long)); \
417 } while (/* CONSTCOND */ 0)
420 * Allocate and setup a management frame of the specified
421 * size. We return the mbuf and a pointer to the start
422 * of the contiguous data area that's been reserved based
423 * on the packet length. The data area is forced to 32-bit
424 * alignment and the buffer length to a multiple of 4 bytes.
425 * This is done mainly so beacon frames (that require this)
426 * can use this interface too.
429 ieee80211_getmgtframe(uint8_t **frm, int headroom, int pktlen)
435 * NB: we know the mbuf routines will align the data area
436 * so we don't need to do anything special.
438 len = roundup2(headroom + pktlen, 4);
439 KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len));
440 if (len < MINCLSIZE) {
441 m = m_gethdr(M_NOWAIT, MT_DATA);
443 * Align the data in case additional headers are added.
444 * This should only happen when a WEP header is added
445 * which only happens for shared key authentication mgt
446 * frames which all fit in MHLEN.
451 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
456 m->m_data += headroom;
462 #ifndef __NO_STRICT_ALIGNMENT
464 * Re-align the payload in the mbuf. This is mainly used (right now)
465 * to handle IP header alignment requirements on certain architectures.
468 ieee80211_realign(struct ieee80211vap *vap, struct mbuf *m, size_t align)
473 pktlen = m->m_pkthdr.len;
474 space = pktlen + align;
475 if (space < MINCLSIZE)
476 n = m_gethdr(M_NOWAIT, MT_DATA);
478 n = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR,
479 space <= MCLBYTES ? MCLBYTES :
480 #if MJUMPAGESIZE != MCLBYTES
481 space <= MJUMPAGESIZE ? MJUMPAGESIZE :
483 space <= MJUM9BYTES ? MJUM9BYTES : MJUM16BYTES);
485 if (__predict_true(n != NULL)) {
487 n->m_data = (caddr_t)(ALIGN(n->m_data + align) - align);
488 m_copydata(m, 0, pktlen, mtod(n, caddr_t));
491 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
492 mtod(m, const struct ieee80211_frame *), NULL,
493 "%s", "no mbuf to realign");
494 vap->iv_stats.is_rx_badalign++;
499 #endif /* !__NO_STRICT_ALIGNMENT */
502 ieee80211_add_callback(struct mbuf *m,
503 void (*func)(struct ieee80211_node *, void *, int), void *arg)
506 struct ieee80211_cb *cb;
508 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_CALLBACK,
509 sizeof(struct ieee80211_cb), M_NOWAIT);
513 cb = (struct ieee80211_cb *)(mtag+1);
516 m_tag_prepend(m, mtag);
517 m->m_flags |= M_TXCB;
522 ieee80211_add_xmit_params(struct mbuf *m,
523 const struct ieee80211_bpf_params *params)
526 struct ieee80211_tx_params *tx;
528 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_XMIT_PARAMS,
529 sizeof(struct ieee80211_tx_params), M_NOWAIT);
533 tx = (struct ieee80211_tx_params *)(mtag+1);
534 memcpy(&tx->params, params, sizeof(struct ieee80211_bpf_params));
535 m_tag_prepend(m, mtag);
540 ieee80211_get_xmit_params(struct mbuf *m,
541 struct ieee80211_bpf_params *params)
544 struct ieee80211_tx_params *tx;
546 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_XMIT_PARAMS,
550 tx = (struct ieee80211_tx_params *)(mtag + 1);
551 memcpy(params, &tx->params, sizeof(struct ieee80211_bpf_params));
556 ieee80211_process_callback(struct ieee80211_node *ni,
557 struct mbuf *m, int status)
561 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_CALLBACK, NULL);
563 struct ieee80211_cb *cb = (struct ieee80211_cb *)(mtag+1);
564 cb->func(ni, cb->arg, status);
569 * Add RX parameters to the given mbuf.
571 * Returns 1 if OK, 0 on error.
574 ieee80211_add_rx_params(struct mbuf *m, const struct ieee80211_rx_stats *rxs)
577 struct ieee80211_rx_params *rx;
579 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_RECV_PARAMS,
580 sizeof(struct ieee80211_rx_stats), M_NOWAIT);
584 rx = (struct ieee80211_rx_params *)(mtag + 1);
585 memcpy(&rx->params, rxs, sizeof(*rxs));
586 m_tag_prepend(m, mtag);
591 ieee80211_get_rx_params(struct mbuf *m, struct ieee80211_rx_stats *rxs)
594 struct ieee80211_rx_params *rx;
596 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_RECV_PARAMS,
600 rx = (struct ieee80211_rx_params *)(mtag + 1);
601 memcpy(rxs, &rx->params, sizeof(*rxs));
605 const struct ieee80211_rx_stats *
606 ieee80211_get_rx_params_ptr(struct mbuf *m)
609 struct ieee80211_rx_params *rx;
611 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_RECV_PARAMS,
615 rx = (struct ieee80211_rx_params *)(mtag + 1);
616 return (&rx->params);
621 * Add TOA parameters to the given mbuf.
624 ieee80211_add_toa_params(struct mbuf *m, const struct ieee80211_toa_params *p)
627 struct ieee80211_toa_params *rp;
629 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_TOA_PARAMS,
630 sizeof(struct ieee80211_toa_params), M_NOWAIT);
634 rp = (struct ieee80211_toa_params *)(mtag + 1);
635 memcpy(rp, p, sizeof(*rp));
636 m_tag_prepend(m, mtag);
641 ieee80211_get_toa_params(struct mbuf *m, struct ieee80211_toa_params *p)
644 struct ieee80211_toa_params *rp;
646 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_TOA_PARAMS,
650 rp = (struct ieee80211_toa_params *)(mtag + 1);
652 memcpy(p, rp, sizeof(*p));
657 * Transmit a frame to the parent interface.
660 ieee80211_parent_xmitpkt(struct ieee80211com *ic, struct mbuf *m)
665 * Assert the IC TX lock is held - this enforces the
666 * processing -> queuing order is maintained
668 IEEE80211_TX_LOCK_ASSERT(ic);
669 error = ic->ic_transmit(ic, m);
671 struct ieee80211_node *ni;
673 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
675 /* XXX number of fragments */
676 if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 1);
677 ieee80211_free_node(ni);
678 ieee80211_free_mbuf(m);
684 * Transmit a frame to the VAP interface.
687 ieee80211_vap_xmitpkt(struct ieee80211vap *vap, struct mbuf *m)
689 struct ifnet *ifp = vap->iv_ifp;
692 * When transmitting via the VAP, we shouldn't hold
693 * any IC TX lock as the VAP TX path will acquire it.
695 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
697 return (ifp->if_transmit(ifp, m));
701 #include <sys/libkern.h>
704 get_random_bytes(void *p, size_t n)
709 uint32_t v = arc4random();
710 size_t nb = n > sizeof(uint32_t) ? sizeof(uint32_t) : n;
711 bcopy(&v, dp, n > sizeof(uint32_t) ? sizeof(uint32_t) : n);
712 dp += sizeof(uint32_t), n -= nb;
717 * Helper function for events that pass just a single mac address.
720 notify_macaddr(struct ifnet *ifp, int op, const uint8_t mac[IEEE80211_ADDR_LEN])
722 struct ieee80211_join_event iev;
724 CURVNET_SET(ifp->if_vnet);
725 memset(&iev, 0, sizeof(iev));
726 IEEE80211_ADDR_COPY(iev.iev_addr, mac);
727 rt_ieee80211msg(ifp, op, &iev, sizeof(iev));
732 ieee80211_notify_node_join(struct ieee80211_node *ni, int newassoc)
734 struct ieee80211vap *vap = ni->ni_vap;
735 struct ifnet *ifp = vap->iv_ifp;
737 CURVNET_SET_QUIET(ifp->if_vnet);
738 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode join",
739 (ni == vap->iv_bss) ? "bss " : "");
741 if (ni == vap->iv_bss) {
742 notify_macaddr(ifp, newassoc ?
743 RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC, ni->ni_bssid);
744 if_link_state_change(ifp, LINK_STATE_UP);
746 notify_macaddr(ifp, newassoc ?
747 RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN, ni->ni_macaddr);
753 ieee80211_notify_node_leave(struct ieee80211_node *ni)
755 struct ieee80211vap *vap = ni->ni_vap;
756 struct ifnet *ifp = vap->iv_ifp;
758 CURVNET_SET_QUIET(ifp->if_vnet);
759 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode leave",
760 (ni == vap->iv_bss) ? "bss " : "");
762 if (ni == vap->iv_bss) {
763 rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
764 if_link_state_change(ifp, LINK_STATE_DOWN);
766 /* fire off wireless event station leaving */
767 notify_macaddr(ifp, RTM_IEEE80211_LEAVE, ni->ni_macaddr);
773 ieee80211_notify_scan_done(struct ieee80211vap *vap)
775 struct ifnet *ifp = vap->iv_ifp;
777 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n", "notify scan done");
779 /* dispatch wireless event indicating scan completed */
780 CURVNET_SET(ifp->if_vnet);
781 rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
786 ieee80211_notify_replay_failure(struct ieee80211vap *vap,
787 const struct ieee80211_frame *wh, const struct ieee80211_key *k,
788 u_int64_t rsc, int tid)
790 struct ifnet *ifp = vap->iv_ifp;
792 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
793 "%s replay detected tid %d <rsc %ju, csc %ju, keyix %u rxkeyix %u>",
794 k->wk_cipher->ic_name, tid, (intmax_t) rsc,
795 (intmax_t) k->wk_keyrsc[tid],
796 k->wk_keyix, k->wk_rxkeyix);
798 if (ifp != NULL) { /* NB: for cipher test modules */
799 struct ieee80211_replay_event iev;
801 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
802 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
803 iev.iev_cipher = k->wk_cipher->ic_cipher;
804 if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE)
805 iev.iev_keyix = k->wk_rxkeyix;
807 iev.iev_keyix = k->wk_keyix;
808 iev.iev_keyrsc = k->wk_keyrsc[tid];
810 CURVNET_SET(ifp->if_vnet);
811 rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
817 ieee80211_notify_michael_failure(struct ieee80211vap *vap,
818 const struct ieee80211_frame *wh, u_int keyix)
820 struct ifnet *ifp = vap->iv_ifp;
822 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
823 "michael MIC verification failed <keyix %u>", keyix);
824 vap->iv_stats.is_rx_tkipmic++;
826 if (ifp != NULL) { /* NB: for cipher test modules */
827 struct ieee80211_michael_event iev;
829 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
830 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
831 iev.iev_cipher = IEEE80211_CIPHER_TKIP;
832 iev.iev_keyix = keyix;
833 CURVNET_SET(ifp->if_vnet);
834 rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev));
840 ieee80211_notify_wds_discover(struct ieee80211_node *ni)
842 struct ieee80211vap *vap = ni->ni_vap;
843 struct ifnet *ifp = vap->iv_ifp;
845 notify_macaddr(ifp, RTM_IEEE80211_WDS, ni->ni_macaddr);
849 ieee80211_notify_csa(struct ieee80211com *ic,
850 const struct ieee80211_channel *c, int mode, int count)
852 struct ieee80211_csa_event iev;
853 struct ieee80211vap *vap;
856 memset(&iev, 0, sizeof(iev));
857 iev.iev_flags = c->ic_flags;
858 iev.iev_freq = c->ic_freq;
859 iev.iev_ieee = c->ic_ieee;
861 iev.iev_count = count;
862 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
864 CURVNET_SET(ifp->if_vnet);
865 rt_ieee80211msg(ifp, RTM_IEEE80211_CSA, &iev, sizeof(iev));
871 ieee80211_notify_radar(struct ieee80211com *ic,
872 const struct ieee80211_channel *c)
874 struct ieee80211_radar_event iev;
875 struct ieee80211vap *vap;
878 memset(&iev, 0, sizeof(iev));
879 iev.iev_flags = c->ic_flags;
880 iev.iev_freq = c->ic_freq;
881 iev.iev_ieee = c->ic_ieee;
882 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
884 CURVNET_SET(ifp->if_vnet);
885 rt_ieee80211msg(ifp, RTM_IEEE80211_RADAR, &iev, sizeof(iev));
891 ieee80211_notify_cac(struct ieee80211com *ic,
892 const struct ieee80211_channel *c, enum ieee80211_notify_cac_event type)
894 struct ieee80211_cac_event iev;
895 struct ieee80211vap *vap;
898 memset(&iev, 0, sizeof(iev));
899 iev.iev_flags = c->ic_flags;
900 iev.iev_freq = c->ic_freq;
901 iev.iev_ieee = c->ic_ieee;
903 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
905 CURVNET_SET(ifp->if_vnet);
906 rt_ieee80211msg(ifp, RTM_IEEE80211_CAC, &iev, sizeof(iev));
912 ieee80211_notify_node_deauth(struct ieee80211_node *ni)
914 struct ieee80211vap *vap = ni->ni_vap;
915 struct ifnet *ifp = vap->iv_ifp;
917 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node deauth");
919 notify_macaddr(ifp, RTM_IEEE80211_DEAUTH, ni->ni_macaddr);
923 ieee80211_notify_node_auth(struct ieee80211_node *ni)
925 struct ieee80211vap *vap = ni->ni_vap;
926 struct ifnet *ifp = vap->iv_ifp;
928 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node auth");
930 notify_macaddr(ifp, RTM_IEEE80211_AUTH, ni->ni_macaddr);
934 ieee80211_notify_country(struct ieee80211vap *vap,
935 const uint8_t bssid[IEEE80211_ADDR_LEN], const uint8_t cc[2])
937 struct ifnet *ifp = vap->iv_ifp;
938 struct ieee80211_country_event iev;
940 memset(&iev, 0, sizeof(iev));
941 IEEE80211_ADDR_COPY(iev.iev_addr, bssid);
942 iev.iev_cc[0] = cc[0];
943 iev.iev_cc[1] = cc[1];
944 CURVNET_SET(ifp->if_vnet);
945 rt_ieee80211msg(ifp, RTM_IEEE80211_COUNTRY, &iev, sizeof(iev));
950 ieee80211_notify_radio(struct ieee80211com *ic, int state)
952 struct ieee80211_radio_event iev;
953 struct ieee80211vap *vap;
956 memset(&iev, 0, sizeof(iev));
957 iev.iev_state = state;
958 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
960 CURVNET_SET(ifp->if_vnet);
961 rt_ieee80211msg(ifp, RTM_IEEE80211_RADIO, &iev, sizeof(iev));
967 ieee80211_load_module(const char *modname)
971 (void)kern_kldload(curthread, modname, NULL);
973 printf("%s: load the %s module by hand for now.\n", __func__, modname);
977 static eventhandler_tag wlan_bpfevent;
978 static eventhandler_tag wlan_ifllevent;
981 bpf_track(void *arg, struct ifnet *ifp, int dlt, int attach)
983 /* NB: identify vap's by if_init */
984 if (dlt == DLT_IEEE802_11_RADIO &&
985 ifp->if_init == ieee80211_init) {
986 struct ieee80211vap *vap = ifp->if_softc;
988 * Track bpf radiotap listener state. We mark the vap
989 * to indicate if any listener is present and the com
990 * to indicate if any listener exists on any associated
991 * vap. This flag is used by drivers to prepare radiotap
992 * state only when needed.
995 ieee80211_syncflag_ext(vap, IEEE80211_FEXT_BPF);
996 if (vap->iv_opmode == IEEE80211_M_MONITOR)
997 atomic_add_int(&vap->iv_ic->ic_montaps, 1);
998 } else if (!bpf_peers_present(vap->iv_rawbpf)) {
999 ieee80211_syncflag_ext(vap, -IEEE80211_FEXT_BPF);
1000 if (vap->iv_opmode == IEEE80211_M_MONITOR)
1001 atomic_subtract_int(&vap->iv_ic->ic_montaps, 1);
1007 * Change MAC address on the vap (if was not started).
1010 wlan_iflladdr(void *arg __unused, struct ifnet *ifp)
1012 /* NB: identify vap's by if_init */
1013 if (ifp->if_init == ieee80211_init &&
1014 (ifp->if_flags & IFF_UP) == 0) {
1015 struct ieee80211vap *vap = ifp->if_softc;
1017 IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp));
1024 * NB: the module name is "wlan" for compatibility with NetBSD.
1027 wlan_modevent(module_t mod, int type, void *unused)
1032 printf("wlan: <802.11 Link Layer>\n");
1033 wlan_bpfevent = EVENTHANDLER_REGISTER(bpf_track,
1034 bpf_track, 0, EVENTHANDLER_PRI_ANY);
1035 wlan_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
1036 wlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
1037 wlan_cloner = if_clone_simple(wlanname, wlan_clone_create,
1038 wlan_clone_destroy, 0);
1041 if_clone_detach(wlan_cloner);
1042 EVENTHANDLER_DEREGISTER(bpf_track, wlan_bpfevent);
1043 EVENTHANDLER_DEREGISTER(iflladdr_event, wlan_ifllevent);
1049 static moduledata_t wlan_mod = {
1054 DECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
1055 MODULE_VERSION(wlan, 1);
1056 MODULE_DEPEND(wlan, ether, 1, 1, 1);
1057 #ifdef IEEE80211_ALQ
1058 MODULE_DEPEND(wlan, alq, 1, 1, 1);
1059 #endif /* IEEE80211_ALQ */