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 const char wlanname[] = "wlan";
72 static struct if_clone *wlan_cloner;
75 wlan_clone_create(struct if_clone *ifc, int unit, caddr_t params)
77 struct ieee80211_clone_params cp;
78 struct ieee80211vap *vap;
79 struct ieee80211com *ic;
82 error = copyin(params, &cp, sizeof(cp));
85 ic = ieee80211_find_com(cp.icp_parent);
88 if (cp.icp_opmode >= IEEE80211_OPMODE_MAX) {
89 ic_printf(ic, "%s: invalid opmode %d\n", __func__,
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]);
98 if ((cp.icp_flags & IEEE80211_CLONE_TDMA) &&
99 #ifdef IEEE80211_SUPPORT_TDMA
100 (ic->ic_caps & IEEE80211_C_TDMA) == 0
105 ic_printf(ic, "TDMA not supported\n");
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);
113 return (vap == NULL ? EIO : 0);
117 wlan_clone_destroy(struct ifnet *ifp)
119 struct ieee80211vap *vap = ifp->if_softc;
120 struct ieee80211com *ic = vap->iv_ic;
122 ic->ic_vap_delete(vap);
126 ieee80211_vap_destroy(struct ieee80211vap *vap)
128 CURVNET_SET(vap->iv_ifp->if_vnet);
129 if_clone_destroyif(wlan_cloner, vap->iv_ifp);
134 ieee80211_sysctl_msecs_ticks(SYSCTL_HANDLER_ARGS)
136 int msecs = ticks_to_msecs(*(int *)arg1);
139 error = sysctl_handle_int(oidp, &msecs, 0, req);
140 if (error || !req->newptr)
142 *(int *)arg1 = msecs_to_ticks(msecs);
147 ieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS)
149 int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT;
152 error = sysctl_handle_int(oidp, &inact, 0, req);
153 if (error || !req->newptr)
155 *(int *)arg1 = inact / IEEE80211_INACT_WAIT;
160 ieee80211_sysctl_parent(SYSCTL_HANDLER_ARGS)
162 struct ieee80211com *ic = arg1;
164 return SYSCTL_OUT_STR(req, ic->ic_name);
168 ieee80211_sysctl_radar(SYSCTL_HANDLER_ARGS)
170 struct ieee80211com *ic = arg1;
173 error = sysctl_handle_int(oidp, &t, 0, req);
174 if (error || !req->newptr)
177 ieee80211_dfs_notify_radar(ic, ic->ic_curchan);
178 IEEE80211_UNLOCK(ic);
183 * For now, just restart everything.
185 * Later on, it'd be nice to have a separate VAP restart to
186 * full-device restart.
189 ieee80211_sysctl_vap_restart(SYSCTL_HANDLER_ARGS)
191 struct ieee80211vap *vap = arg1;
194 error = sysctl_handle_int(oidp, &t, 0, req);
195 if (error || !req->newptr)
198 ieee80211_restart_all(vap->iv_ic);
203 ieee80211_sysctl_attach(struct ieee80211com *ic)
208 ieee80211_sysctl_detach(struct ieee80211com *ic)
213 ieee80211_sysctl_vattach(struct ieee80211vap *vap)
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 */
220 ctx = (struct sysctl_ctx_list *) IEEE80211_MALLOC(sizeof(struct sysctl_ctx_list),
221 M_DEVBUF, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
223 if_printf(ifp, "%s: cannot allocate sysctl context!\n",
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");
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)");
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");
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");
292 vap->iv_sysctl = ctx;
297 ieee80211_sysctl_vdetach(struct ieee80211vap *vap)
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;
307 #define MS(_v, _f) (((_v) & _f##_M) >> _f##_S)
309 ieee80211_com_vincref(struct ieee80211vap *vap)
313 ostate = atomic_fetchadd_32(&vap->iv_com_state, IEEE80211_COM_REF_ADD);
315 if (ostate & IEEE80211_COM_DETACHED) {
316 atomic_subtract_32(&vap->iv_com_state, IEEE80211_COM_REF_ADD);
320 if (MS(ostate, IEEE80211_COM_REF) == IEEE80211_COM_REF_MAX) {
321 atomic_subtract_32(&vap->iv_com_state, IEEE80211_COM_REF_ADD);
329 ieee80211_com_vdecref(struct ieee80211vap *vap)
333 ostate = atomic_fetchadd_32(&vap->iv_com_state, -IEEE80211_COM_REF_ADD);
335 KASSERT(MS(ostate, IEEE80211_COM_REF) != 0,
336 ("com reference counter underflow"));
342 ieee80211_com_vdetach(struct ieee80211vap *vap)
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);
354 ieee80211_node_dectestref(struct ieee80211_node *ni)
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);
362 ieee80211_drain_ifq(struct ifqueue *ifq)
364 struct ieee80211_node *ni;
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;
382 ieee80211_flush_ifq(struct ifqueue *ifq, struct ieee80211vap *vap)
384 struct ieee80211_node *ni;
385 struct mbuf *m, **mprev;
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 */
396 ieee80211_free_node(ni); /* reclaim ref */
398 mprev = &m->m_nextpkt;
400 /* recalculate tail ptr */
402 for (; m != NULL && m->m_nextpkt != NULL; m = m->m_nextpkt)
409 * As above, for mbufs allocated with m_gethdr/MGETHDR
410 * or initialized by M_COPY_PKTHDR.
412 #define MC_ALIGN(m, len) \
414 (m)->m_data += rounddown2(MCLBYTES - (len), sizeof(long)); \
415 } while (/* CONSTCOND */ 0)
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.
427 ieee80211_getmgtframe(uint8_t **frm, int headroom, int pktlen)
433 * NB: we know the mbuf routines will align the data area
434 * so we don't need to do anything special.
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);
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.
449 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
454 m->m_data += headroom;
460 #ifndef __NO_STRICT_ALIGNMENT
462 * Re-align the payload in the mbuf. This is mainly used (right now)
463 * to handle IP header alignment requirements on certain architectures.
466 ieee80211_realign(struct ieee80211vap *vap, struct mbuf *m, size_t align)
471 pktlen = m->m_pkthdr.len;
472 space = pktlen + align;
473 if (space < MINCLSIZE)
474 n = m_gethdr(M_NOWAIT, MT_DATA);
476 n = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR,
477 space <= MCLBYTES ? MCLBYTES :
478 #if MJUMPAGESIZE != MCLBYTES
479 space <= MJUMPAGESIZE ? MJUMPAGESIZE :
481 space <= MJUM9BYTES ? MJUM9BYTES : MJUM16BYTES);
483 if (__predict_true(n != NULL)) {
485 n->m_data = (caddr_t)(ALIGN(n->m_data + align) - align);
486 m_copydata(m, 0, pktlen, mtod(n, caddr_t));
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++;
497 #endif /* !__NO_STRICT_ALIGNMENT */
500 ieee80211_add_callback(struct mbuf *m,
501 void (*func)(struct ieee80211_node *, void *, int), void *arg)
504 struct ieee80211_cb *cb;
506 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_CALLBACK,
507 sizeof(struct ieee80211_cb), M_NOWAIT);
511 cb = (struct ieee80211_cb *)(mtag+1);
514 m_tag_prepend(m, mtag);
515 m->m_flags |= M_TXCB;
520 ieee80211_add_xmit_params(struct mbuf *m,
521 const struct ieee80211_bpf_params *params)
524 struct ieee80211_tx_params *tx;
526 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_XMIT_PARAMS,
527 sizeof(struct ieee80211_tx_params), M_NOWAIT);
531 tx = (struct ieee80211_tx_params *)(mtag+1);
532 memcpy(&tx->params, params, sizeof(struct ieee80211_bpf_params));
533 m_tag_prepend(m, mtag);
538 ieee80211_get_xmit_params(struct mbuf *m,
539 struct ieee80211_bpf_params *params)
542 struct ieee80211_tx_params *tx;
544 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_XMIT_PARAMS,
548 tx = (struct ieee80211_tx_params *)(mtag + 1);
549 memcpy(params, &tx->params, sizeof(struct ieee80211_bpf_params));
554 ieee80211_process_callback(struct ieee80211_node *ni,
555 struct mbuf *m, int status)
559 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_CALLBACK, NULL);
561 struct ieee80211_cb *cb = (struct ieee80211_cb *)(mtag+1);
562 cb->func(ni, cb->arg, status);
567 * Add RX parameters to the given mbuf.
569 * Returns 1 if OK, 0 on error.
572 ieee80211_add_rx_params(struct mbuf *m, const struct ieee80211_rx_stats *rxs)
575 struct ieee80211_rx_params *rx;
577 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_RECV_PARAMS,
578 sizeof(struct ieee80211_rx_stats), M_NOWAIT);
582 rx = (struct ieee80211_rx_params *)(mtag + 1);
583 memcpy(&rx->params, rxs, sizeof(*rxs));
584 m_tag_prepend(m, mtag);
589 ieee80211_get_rx_params(struct mbuf *m, struct ieee80211_rx_stats *rxs)
592 struct ieee80211_rx_params *rx;
594 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_RECV_PARAMS,
598 rx = (struct ieee80211_rx_params *)(mtag + 1);
599 memcpy(rxs, &rx->params, sizeof(*rxs));
603 const struct ieee80211_rx_stats *
604 ieee80211_get_rx_params_ptr(struct mbuf *m)
607 struct ieee80211_rx_params *rx;
609 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_RECV_PARAMS,
613 rx = (struct ieee80211_rx_params *)(mtag + 1);
614 return (&rx->params);
619 * Add TOA parameters to the given mbuf.
622 ieee80211_add_toa_params(struct mbuf *m, const struct ieee80211_toa_params *p)
625 struct ieee80211_toa_params *rp;
627 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_TOA_PARAMS,
628 sizeof(struct ieee80211_toa_params), M_NOWAIT);
632 rp = (struct ieee80211_toa_params *)(mtag + 1);
633 memcpy(rp, p, sizeof(*rp));
634 m_tag_prepend(m, mtag);
639 ieee80211_get_toa_params(struct mbuf *m, struct ieee80211_toa_params *p)
642 struct ieee80211_toa_params *rp;
644 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_TOA_PARAMS,
648 rp = (struct ieee80211_toa_params *)(mtag + 1);
650 memcpy(p, rp, sizeof(*p));
655 * Transmit a frame to the parent interface.
658 ieee80211_parent_xmitpkt(struct ieee80211com *ic, struct mbuf *m)
663 * Assert the IC TX lock is held - this enforces the
664 * processing -> queuing order is maintained
666 IEEE80211_TX_LOCK_ASSERT(ic);
667 error = ic->ic_transmit(ic, m);
669 struct ieee80211_node *ni;
671 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
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);
682 * Transmit a frame to the VAP interface.
685 ieee80211_vap_xmitpkt(struct ieee80211vap *vap, struct mbuf *m)
687 struct ifnet *ifp = vap->iv_ifp;
690 * When transmitting via the VAP, we shouldn't hold
691 * any IC TX lock as the VAP TX path will acquire it.
693 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
695 return (ifp->if_transmit(ifp, m));
699 #include <sys/libkern.h>
702 get_random_bytes(void *p, size_t n)
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;
715 * Helper function for events that pass just a single mac address.
718 notify_macaddr(struct ifnet *ifp, int op, const uint8_t mac[IEEE80211_ADDR_LEN])
720 struct ieee80211_join_event iev;
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));
730 ieee80211_notify_node_join(struct ieee80211_node *ni, int newassoc)
732 struct ieee80211vap *vap = ni->ni_vap;
733 struct ifnet *ifp = vap->iv_ifp;
735 CURVNET_SET_QUIET(ifp->if_vnet);
736 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode join",
737 (ni == vap->iv_bss) ? "bss " : "");
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);
744 notify_macaddr(ifp, newassoc ?
745 RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN, ni->ni_macaddr);
751 ieee80211_notify_node_leave(struct ieee80211_node *ni)
753 struct ieee80211vap *vap = ni->ni_vap;
754 struct ifnet *ifp = vap->iv_ifp;
756 CURVNET_SET_QUIET(ifp->if_vnet);
757 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode leave",
758 (ni == vap->iv_bss) ? "bss " : "");
760 if (ni == vap->iv_bss) {
761 rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
762 if_link_state_change(ifp, LINK_STATE_DOWN);
764 /* fire off wireless event station leaving */
765 notify_macaddr(ifp, RTM_IEEE80211_LEAVE, ni->ni_macaddr);
771 ieee80211_notify_scan_done(struct ieee80211vap *vap)
773 struct ifnet *ifp = vap->iv_ifp;
775 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n", "notify scan done");
777 /* dispatch wireless event indicating scan completed */
778 CURVNET_SET(ifp->if_vnet);
779 rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
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)
788 struct ifnet *ifp = vap->iv_ifp;
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);
796 if (ifp != NULL) { /* NB: for cipher test modules */
797 struct ieee80211_replay_event iev;
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;
805 iev.iev_keyix = k->wk_keyix;
806 iev.iev_keyrsc = k->wk_keyrsc[tid];
808 CURVNET_SET(ifp->if_vnet);
809 rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
815 ieee80211_notify_michael_failure(struct ieee80211vap *vap,
816 const struct ieee80211_frame *wh, u_int keyix)
818 struct ifnet *ifp = vap->iv_ifp;
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++;
824 if (ifp != NULL) { /* NB: for cipher test modules */
825 struct ieee80211_michael_event iev;
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));
838 ieee80211_notify_wds_discover(struct ieee80211_node *ni)
840 struct ieee80211vap *vap = ni->ni_vap;
841 struct ifnet *ifp = vap->iv_ifp;
843 notify_macaddr(ifp, RTM_IEEE80211_WDS, ni->ni_macaddr);
847 ieee80211_notify_csa(struct ieee80211com *ic,
848 const struct ieee80211_channel *c, int mode, int count)
850 struct ieee80211_csa_event iev;
851 struct ieee80211vap *vap;
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;
859 iev.iev_count = count;
860 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
862 CURVNET_SET(ifp->if_vnet);
863 rt_ieee80211msg(ifp, RTM_IEEE80211_CSA, &iev, sizeof(iev));
869 ieee80211_notify_radar(struct ieee80211com *ic,
870 const struct ieee80211_channel *c)
872 struct ieee80211_radar_event iev;
873 struct ieee80211vap *vap;
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) {
882 CURVNET_SET(ifp->if_vnet);
883 rt_ieee80211msg(ifp, RTM_IEEE80211_RADAR, &iev, sizeof(iev));
889 ieee80211_notify_cac(struct ieee80211com *ic,
890 const struct ieee80211_channel *c, enum ieee80211_notify_cac_event type)
892 struct ieee80211_cac_event iev;
893 struct ieee80211vap *vap;
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;
901 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
903 CURVNET_SET(ifp->if_vnet);
904 rt_ieee80211msg(ifp, RTM_IEEE80211_CAC, &iev, sizeof(iev));
910 ieee80211_notify_node_deauth(struct ieee80211_node *ni)
912 struct ieee80211vap *vap = ni->ni_vap;
913 struct ifnet *ifp = vap->iv_ifp;
915 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node deauth");
917 notify_macaddr(ifp, RTM_IEEE80211_DEAUTH, ni->ni_macaddr);
921 ieee80211_notify_node_auth(struct ieee80211_node *ni)
923 struct ieee80211vap *vap = ni->ni_vap;
924 struct ifnet *ifp = vap->iv_ifp;
926 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node auth");
928 notify_macaddr(ifp, RTM_IEEE80211_AUTH, ni->ni_macaddr);
932 ieee80211_notify_country(struct ieee80211vap *vap,
933 const uint8_t bssid[IEEE80211_ADDR_LEN], const uint8_t cc[2])
935 struct ifnet *ifp = vap->iv_ifp;
936 struct ieee80211_country_event iev;
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));
948 ieee80211_notify_radio(struct ieee80211com *ic, int state)
950 struct ieee80211_radio_event iev;
951 struct ieee80211vap *vap;
954 memset(&iev, 0, sizeof(iev));
955 iev.iev_state = state;
956 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
958 CURVNET_SET(ifp->if_vnet);
959 rt_ieee80211msg(ifp, RTM_IEEE80211_RADIO, &iev, sizeof(iev));
965 ieee80211_load_module(const char *modname)
969 (void)kern_kldload(curthread, modname, NULL);
971 printf("%s: load the %s module by hand for now.\n", __func__, modname);
975 static eventhandler_tag wlan_bpfevent;
976 static eventhandler_tag wlan_ifllevent;
979 bpf_track(void *arg, struct ifnet *ifp, int dlt, int attach)
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;
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.
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);
1005 * Change MAC address on the vap (if was not started).
1008 wlan_iflladdr(void *arg __unused, struct ifnet *ifp)
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;
1015 IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp));
1022 * NB: the module name is "wlan" for compatibility with NetBSD.
1025 wlan_modevent(module_t mod, int type, void *unused)
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);
1039 if_clone_detach(wlan_cloner);
1040 EVENTHANDLER_DEREGISTER(bpf_track, wlan_bpfevent);
1041 EVENTHANDLER_DEREGISTER(iflladdr_event, wlan_ifllevent);
1047 static moduledata_t wlan_mod = {
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 */