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>
46 #include <sys/sysctl.h>
48 #include <sys/socket.h>
52 #include <net/if_var.h>
53 #include <net/if_dl.h>
54 #include <net/if_clone.h>
55 #include <net/if_media.h>
56 #include <net/if_types.h>
57 #include <net/ethernet.h>
58 #include <net/route.h>
61 #include <net80211/ieee80211_var.h>
62 #include <net80211/ieee80211_input.h>
64 SYSCTL_NODE(_net, OID_AUTO, wlan, CTLFLAG_RD, 0, "IEEE 80211 parameters");
66 #ifdef IEEE80211_DEBUG
67 static int ieee80211_debug = 0;
68 SYSCTL_INT(_net_wlan, OID_AUTO, debug, CTLFLAG_RW, &ieee80211_debug,
69 0, "debugging printfs");
72 static const char wlanname[] = "wlan";
73 static struct if_clone *wlan_cloner;
76 wlan_clone_create(struct if_clone *ifc, int unit, caddr_t params)
78 struct ieee80211_clone_params cp;
79 struct ieee80211vap *vap;
80 struct ieee80211com *ic;
83 error = priv_check(curthread, PRIV_NET80211_CREATE_VAP);
87 error = copyin(params, &cp, sizeof(cp));
90 ic = ieee80211_find_com(cp.icp_parent);
93 if (cp.icp_opmode >= IEEE80211_OPMODE_MAX) {
94 ic_printf(ic, "%s: invalid opmode %d\n", __func__,
98 if ((ic->ic_caps & ieee80211_opcap[cp.icp_opmode]) == 0) {
99 ic_printf(ic, "%s mode not supported\n",
100 ieee80211_opmode_name[cp.icp_opmode]);
103 if ((cp.icp_flags & IEEE80211_CLONE_TDMA) &&
104 #ifdef IEEE80211_SUPPORT_TDMA
105 (ic->ic_caps & IEEE80211_C_TDMA) == 0
110 ic_printf(ic, "TDMA not supported\n");
113 vap = ic->ic_vap_create(ic, wlanname, unit,
114 cp.icp_opmode, cp.icp_flags, cp.icp_bssid,
115 cp.icp_flags & IEEE80211_CLONE_MACADDR ?
116 cp.icp_macaddr : ic->ic_macaddr);
118 return (vap == NULL ? EIO : 0);
122 wlan_clone_destroy(struct ifnet *ifp)
124 struct ieee80211vap *vap = ifp->if_softc;
125 struct ieee80211com *ic = vap->iv_ic;
127 ic->ic_vap_delete(vap);
131 ieee80211_vap_destroy(struct ieee80211vap *vap)
133 CURVNET_SET(vap->iv_ifp->if_vnet);
134 if_clone_destroyif(wlan_cloner, vap->iv_ifp);
139 ieee80211_sysctl_msecs_ticks(SYSCTL_HANDLER_ARGS)
141 int msecs = ticks_to_msecs(*(int *)arg1);
144 error = sysctl_handle_int(oidp, &msecs, 0, req);
145 if (error || !req->newptr)
147 *(int *)arg1 = msecs_to_ticks(msecs);
152 ieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS)
154 int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT;
157 error = sysctl_handle_int(oidp, &inact, 0, req);
158 if (error || !req->newptr)
160 *(int *)arg1 = inact / IEEE80211_INACT_WAIT;
165 ieee80211_sysctl_parent(SYSCTL_HANDLER_ARGS)
167 struct ieee80211com *ic = arg1;
169 return SYSCTL_OUT_STR(req, ic->ic_name);
173 ieee80211_sysctl_radar(SYSCTL_HANDLER_ARGS)
175 struct ieee80211com *ic = arg1;
178 error = sysctl_handle_int(oidp, &t, 0, req);
179 if (error || !req->newptr)
182 ieee80211_dfs_notify_radar(ic, ic->ic_curchan);
183 IEEE80211_UNLOCK(ic);
188 * For now, just restart everything.
190 * Later on, it'd be nice to have a separate VAP restart to
191 * full-device restart.
194 ieee80211_sysctl_vap_restart(SYSCTL_HANDLER_ARGS)
196 struct ieee80211vap *vap = arg1;
199 error = sysctl_handle_int(oidp, &t, 0, req);
200 if (error || !req->newptr)
203 ieee80211_restart_all(vap->iv_ic);
208 ieee80211_sysctl_attach(struct ieee80211com *ic)
213 ieee80211_sysctl_detach(struct ieee80211com *ic)
218 ieee80211_sysctl_vattach(struct ieee80211vap *vap)
220 struct ifnet *ifp = vap->iv_ifp;
221 struct sysctl_ctx_list *ctx;
222 struct sysctl_oid *oid;
223 char num[14]; /* sufficient for 32 bits */
225 ctx = (struct sysctl_ctx_list *) IEEE80211_MALLOC(sizeof(struct sysctl_ctx_list),
226 M_DEVBUF, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
228 if_printf(ifp, "%s: cannot allocate sysctl context!\n",
232 sysctl_ctx_init(ctx);
233 snprintf(num, sizeof(num), "%u", ifp->if_dunit);
234 oid = SYSCTL_ADD_NODE(ctx, &SYSCTL_NODE_CHILDREN(_net, wlan),
235 OID_AUTO, num, CTLFLAG_RD, NULL, "");
236 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
237 "%parent", CTLTYPE_STRING | CTLFLAG_RD, vap->iv_ic, 0,
238 ieee80211_sysctl_parent, "A", "parent device");
239 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
240 "driver_caps", CTLFLAG_RW, &vap->iv_caps, 0,
241 "driver capabilities");
242 #ifdef IEEE80211_DEBUG
243 vap->iv_debug = ieee80211_debug;
244 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
245 "debug", CTLFLAG_RW, &vap->iv_debug, 0,
246 "control debugging printfs");
248 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
249 "bmiss_max", CTLFLAG_RW, &vap->iv_bmiss_max, 0,
250 "consecutive beacon misses before scanning");
251 /* XXX inherit from tunables */
252 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
253 "inact_run", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_run, 0,
254 ieee80211_sysctl_inact, "I",
255 "station inactivity timeout (sec)");
256 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
257 "inact_probe", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_probe, 0,
258 ieee80211_sysctl_inact, "I",
259 "station inactivity probe timeout (sec)");
260 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
261 "inact_auth", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_auth, 0,
262 ieee80211_sysctl_inact, "I",
263 "station authentication timeout (sec)");
264 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
265 "inact_init", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_init, 0,
266 ieee80211_sysctl_inact, "I",
267 "station initial state timeout (sec)");
268 if (vap->iv_htcaps & IEEE80211_HTC_HT) {
269 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
270 "ampdu_mintraffic_bk", CTLFLAG_RW,
271 &vap->iv_ampdu_mintraffic[WME_AC_BK], 0,
272 "BK traffic tx aggr threshold (pps)");
273 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
274 "ampdu_mintraffic_be", CTLFLAG_RW,
275 &vap->iv_ampdu_mintraffic[WME_AC_BE], 0,
276 "BE traffic tx aggr threshold (pps)");
277 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
278 "ampdu_mintraffic_vo", CTLFLAG_RW,
279 &vap->iv_ampdu_mintraffic[WME_AC_VO], 0,
280 "VO traffic tx aggr threshold (pps)");
281 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
282 "ampdu_mintraffic_vi", CTLFLAG_RW,
283 &vap->iv_ampdu_mintraffic[WME_AC_VI], 0,
284 "VI traffic tx aggr threshold (pps)");
287 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
288 "force_restart", CTLTYPE_INT | CTLFLAG_RW, vap, 0,
289 ieee80211_sysctl_vap_restart, "I",
290 "force a VAP restart");
292 if (vap->iv_caps & IEEE80211_C_DFS) {
293 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
294 "radar", CTLTYPE_INT | CTLFLAG_RW, vap->iv_ic, 0,
295 ieee80211_sysctl_radar, "I", "simulate radar event");
297 vap->iv_sysctl = ctx;
302 ieee80211_sysctl_vdetach(struct ieee80211vap *vap)
305 if (vap->iv_sysctl != NULL) {
306 sysctl_ctx_free(vap->iv_sysctl);
307 IEEE80211_FREE(vap->iv_sysctl, M_DEVBUF);
308 vap->iv_sysctl = NULL;
312 #define MS(_v, _f) (((_v) & _f##_M) >> _f##_S)
314 ieee80211_com_vincref(struct ieee80211vap *vap)
318 ostate = atomic_fetchadd_32(&vap->iv_com_state, IEEE80211_COM_REF_ADD);
320 if (ostate & IEEE80211_COM_DETACHED) {
321 atomic_subtract_32(&vap->iv_com_state, IEEE80211_COM_REF_ADD);
325 if (MS(ostate, IEEE80211_COM_REF) == IEEE80211_COM_REF_MAX) {
326 atomic_subtract_32(&vap->iv_com_state, IEEE80211_COM_REF_ADD);
334 ieee80211_com_vdecref(struct ieee80211vap *vap)
338 ostate = atomic_fetchadd_32(&vap->iv_com_state, -IEEE80211_COM_REF_ADD);
340 KASSERT(MS(ostate, IEEE80211_COM_REF) != 0,
341 ("com reference counter underflow"));
347 ieee80211_com_vdetach(struct ieee80211vap *vap)
351 sleep_time = msecs_to_ticks(250);
352 atomic_set_32(&vap->iv_com_state, IEEE80211_COM_DETACHED);
353 while (MS(atomic_load_32(&vap->iv_com_state), IEEE80211_COM_REF) != 0)
354 pause("comref", sleep_time);
359 ieee80211_node_dectestref(struct ieee80211_node *ni)
361 /* XXX need equivalent of atomic_dec_and_test */
362 atomic_subtract_int(&ni->ni_refcnt, 1);
363 return atomic_cmpset_int(&ni->ni_refcnt, 0, 1);
367 ieee80211_drain_ifq(struct ifqueue *ifq)
369 struct ieee80211_node *ni;
377 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
378 KASSERT(ni != NULL, ("frame w/o node"));
379 ieee80211_free_node(ni);
380 m->m_pkthdr.rcvif = NULL;
387 ieee80211_flush_ifq(struct ifqueue *ifq, struct ieee80211vap *vap)
389 struct ieee80211_node *ni;
390 struct mbuf *m, **mprev;
393 mprev = &ifq->ifq_head;
394 while ((m = *mprev) != NULL) {
395 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
396 if (ni != NULL && ni->ni_vap == vap) {
397 *mprev = m->m_nextpkt; /* remove from list */
401 ieee80211_free_node(ni); /* reclaim ref */
403 mprev = &m->m_nextpkt;
405 /* recalculate tail ptr */
407 for (; m != NULL && m->m_nextpkt != NULL; m = m->m_nextpkt)
414 * As above, for mbufs allocated with m_gethdr/MGETHDR
415 * or initialized by M_COPY_PKTHDR.
417 #define MC_ALIGN(m, len) \
419 (m)->m_data += rounddown2(MCLBYTES - (len), sizeof(long)); \
420 } while (/* CONSTCOND */ 0)
423 * Allocate and setup a management frame of the specified
424 * size. We return the mbuf and a pointer to the start
425 * of the contiguous data area that's been reserved based
426 * on the packet length. The data area is forced to 32-bit
427 * alignment and the buffer length to a multiple of 4 bytes.
428 * This is done mainly so beacon frames (that require this)
429 * can use this interface too.
432 ieee80211_getmgtframe(uint8_t **frm, int headroom, int pktlen)
438 * NB: we know the mbuf routines will align the data area
439 * so we don't need to do anything special.
441 len = roundup2(headroom + pktlen, 4);
442 KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len));
443 if (len < MINCLSIZE) {
444 m = m_gethdr(M_NOWAIT, MT_DATA);
446 * Align the data in case additional headers are added.
447 * This should only happen when a WEP header is added
448 * which only happens for shared key authentication mgt
449 * frames which all fit in MHLEN.
454 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
459 m->m_data += headroom;
465 #ifndef __NO_STRICT_ALIGNMENT
467 * Re-align the payload in the mbuf. This is mainly used (right now)
468 * to handle IP header alignment requirements on certain architectures.
471 ieee80211_realign(struct ieee80211vap *vap, struct mbuf *m, size_t align)
476 pktlen = m->m_pkthdr.len;
477 space = pktlen + align;
478 if (space < MINCLSIZE)
479 n = m_gethdr(M_NOWAIT, MT_DATA);
481 n = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR,
482 space <= MCLBYTES ? MCLBYTES :
483 #if MJUMPAGESIZE != MCLBYTES
484 space <= MJUMPAGESIZE ? MJUMPAGESIZE :
486 space <= MJUM9BYTES ? MJUM9BYTES : MJUM16BYTES);
488 if (__predict_true(n != NULL)) {
490 n->m_data = (caddr_t)(ALIGN(n->m_data + align) - align);
491 m_copydata(m, 0, pktlen, mtod(n, caddr_t));
494 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
495 mtod(m, const struct ieee80211_frame *), NULL,
496 "%s", "no mbuf to realign");
497 vap->iv_stats.is_rx_badalign++;
502 #endif /* !__NO_STRICT_ALIGNMENT */
505 ieee80211_add_callback(struct mbuf *m,
506 void (*func)(struct ieee80211_node *, void *, int), void *arg)
509 struct ieee80211_cb *cb;
511 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_CALLBACK,
512 sizeof(struct ieee80211_cb), M_NOWAIT);
516 cb = (struct ieee80211_cb *)(mtag+1);
519 m_tag_prepend(m, mtag);
520 m->m_flags |= M_TXCB;
525 ieee80211_add_xmit_params(struct mbuf *m,
526 const struct ieee80211_bpf_params *params)
529 struct ieee80211_tx_params *tx;
531 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_XMIT_PARAMS,
532 sizeof(struct ieee80211_tx_params), M_NOWAIT);
536 tx = (struct ieee80211_tx_params *)(mtag+1);
537 memcpy(&tx->params, params, sizeof(struct ieee80211_bpf_params));
538 m_tag_prepend(m, mtag);
543 ieee80211_get_xmit_params(struct mbuf *m,
544 struct ieee80211_bpf_params *params)
547 struct ieee80211_tx_params *tx;
549 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_XMIT_PARAMS,
553 tx = (struct ieee80211_tx_params *)(mtag + 1);
554 memcpy(params, &tx->params, sizeof(struct ieee80211_bpf_params));
559 ieee80211_process_callback(struct ieee80211_node *ni,
560 struct mbuf *m, int status)
564 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_CALLBACK, NULL);
566 struct ieee80211_cb *cb = (struct ieee80211_cb *)(mtag+1);
567 cb->func(ni, cb->arg, status);
572 * Add RX parameters to the given mbuf.
574 * Returns 1 if OK, 0 on error.
577 ieee80211_add_rx_params(struct mbuf *m, const struct ieee80211_rx_stats *rxs)
580 struct ieee80211_rx_params *rx;
582 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_RECV_PARAMS,
583 sizeof(struct ieee80211_rx_stats), M_NOWAIT);
587 rx = (struct ieee80211_rx_params *)(mtag + 1);
588 memcpy(&rx->params, rxs, sizeof(*rxs));
589 m_tag_prepend(m, mtag);
594 ieee80211_get_rx_params(struct mbuf *m, struct ieee80211_rx_stats *rxs)
597 struct ieee80211_rx_params *rx;
599 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_RECV_PARAMS,
603 rx = (struct ieee80211_rx_params *)(mtag + 1);
604 memcpy(rxs, &rx->params, sizeof(*rxs));
608 const struct ieee80211_rx_stats *
609 ieee80211_get_rx_params_ptr(struct mbuf *m)
612 struct ieee80211_rx_params *rx;
614 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_RECV_PARAMS,
618 rx = (struct ieee80211_rx_params *)(mtag + 1);
619 return (&rx->params);
624 * Add TOA parameters to the given mbuf.
627 ieee80211_add_toa_params(struct mbuf *m, const struct ieee80211_toa_params *p)
630 struct ieee80211_toa_params *rp;
632 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_TOA_PARAMS,
633 sizeof(struct ieee80211_toa_params), M_NOWAIT);
637 rp = (struct ieee80211_toa_params *)(mtag + 1);
638 memcpy(rp, p, sizeof(*rp));
639 m_tag_prepend(m, mtag);
644 ieee80211_get_toa_params(struct mbuf *m, struct ieee80211_toa_params *p)
647 struct ieee80211_toa_params *rp;
649 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_TOA_PARAMS,
653 rp = (struct ieee80211_toa_params *)(mtag + 1);
655 memcpy(p, rp, sizeof(*p));
660 * Transmit a frame to the parent interface.
663 ieee80211_parent_xmitpkt(struct ieee80211com *ic, struct mbuf *m)
668 * Assert the IC TX lock is held - this enforces the
669 * processing -> queuing order is maintained
671 IEEE80211_TX_LOCK_ASSERT(ic);
672 error = ic->ic_transmit(ic, m);
674 struct ieee80211_node *ni;
676 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
678 /* XXX number of fragments */
679 if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 1);
680 ieee80211_free_node(ni);
681 ieee80211_free_mbuf(m);
687 * Transmit a frame to the VAP interface.
690 ieee80211_vap_xmitpkt(struct ieee80211vap *vap, struct mbuf *m)
692 struct ifnet *ifp = vap->iv_ifp;
695 * When transmitting via the VAP, we shouldn't hold
696 * any IC TX lock as the VAP TX path will acquire it.
698 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
700 return (ifp->if_transmit(ifp, m));
704 #include <sys/libkern.h>
707 get_random_bytes(void *p, size_t n)
712 uint32_t v = arc4random();
713 size_t nb = n > sizeof(uint32_t) ? sizeof(uint32_t) : n;
714 bcopy(&v, dp, n > sizeof(uint32_t) ? sizeof(uint32_t) : n);
715 dp += sizeof(uint32_t), n -= nb;
720 * Helper function for events that pass just a single mac address.
723 notify_macaddr(struct ifnet *ifp, int op, const uint8_t mac[IEEE80211_ADDR_LEN])
725 struct ieee80211_join_event iev;
727 CURVNET_SET(ifp->if_vnet);
728 memset(&iev, 0, sizeof(iev));
729 IEEE80211_ADDR_COPY(iev.iev_addr, mac);
730 rt_ieee80211msg(ifp, op, &iev, sizeof(iev));
735 ieee80211_notify_node_join(struct ieee80211_node *ni, int newassoc)
737 struct ieee80211vap *vap = ni->ni_vap;
738 struct ifnet *ifp = vap->iv_ifp;
740 CURVNET_SET_QUIET(ifp->if_vnet);
741 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode join",
742 (ni == vap->iv_bss) ? "bss " : "");
744 if (ni == vap->iv_bss) {
745 notify_macaddr(ifp, newassoc ?
746 RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC, ni->ni_bssid);
747 if_link_state_change(ifp, LINK_STATE_UP);
749 notify_macaddr(ifp, newassoc ?
750 RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN, ni->ni_macaddr);
756 ieee80211_notify_node_leave(struct ieee80211_node *ni)
758 struct ieee80211vap *vap = ni->ni_vap;
759 struct ifnet *ifp = vap->iv_ifp;
761 CURVNET_SET_QUIET(ifp->if_vnet);
762 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode leave",
763 (ni == vap->iv_bss) ? "bss " : "");
765 if (ni == vap->iv_bss) {
766 rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
767 if_link_state_change(ifp, LINK_STATE_DOWN);
769 /* fire off wireless event station leaving */
770 notify_macaddr(ifp, RTM_IEEE80211_LEAVE, ni->ni_macaddr);
776 ieee80211_notify_scan_done(struct ieee80211vap *vap)
778 struct ifnet *ifp = vap->iv_ifp;
780 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n", "notify scan done");
782 /* dispatch wireless event indicating scan completed */
783 CURVNET_SET(ifp->if_vnet);
784 rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
789 ieee80211_notify_replay_failure(struct ieee80211vap *vap,
790 const struct ieee80211_frame *wh, const struct ieee80211_key *k,
791 u_int64_t rsc, int tid)
793 struct ifnet *ifp = vap->iv_ifp;
795 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
796 "%s replay detected tid %d <rsc %ju (%jx), csc %ju (%jx), keyix %u rxkeyix %u>",
797 k->wk_cipher->ic_name, tid,
800 (intmax_t) k->wk_keyrsc[tid],
801 (intmax_t) k->wk_keyrsc[tid],
802 k->wk_keyix, k->wk_rxkeyix);
804 if (ifp != NULL) { /* NB: for cipher test modules */
805 struct ieee80211_replay_event iev;
807 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
808 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
809 iev.iev_cipher = k->wk_cipher->ic_cipher;
810 if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE)
811 iev.iev_keyix = k->wk_rxkeyix;
813 iev.iev_keyix = k->wk_keyix;
814 iev.iev_keyrsc = k->wk_keyrsc[tid];
816 CURVNET_SET(ifp->if_vnet);
817 rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
823 ieee80211_notify_michael_failure(struct ieee80211vap *vap,
824 const struct ieee80211_frame *wh, u_int keyix)
826 struct ifnet *ifp = vap->iv_ifp;
828 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
829 "michael MIC verification failed <keyix %u>", keyix);
830 vap->iv_stats.is_rx_tkipmic++;
832 if (ifp != NULL) { /* NB: for cipher test modules */
833 struct ieee80211_michael_event iev;
835 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
836 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
837 iev.iev_cipher = IEEE80211_CIPHER_TKIP;
838 iev.iev_keyix = keyix;
839 CURVNET_SET(ifp->if_vnet);
840 rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev));
846 ieee80211_notify_wds_discover(struct ieee80211_node *ni)
848 struct ieee80211vap *vap = ni->ni_vap;
849 struct ifnet *ifp = vap->iv_ifp;
851 notify_macaddr(ifp, RTM_IEEE80211_WDS, ni->ni_macaddr);
855 ieee80211_notify_csa(struct ieee80211com *ic,
856 const struct ieee80211_channel *c, int mode, int count)
858 struct ieee80211_csa_event iev;
859 struct ieee80211vap *vap;
862 memset(&iev, 0, sizeof(iev));
863 iev.iev_flags = c->ic_flags;
864 iev.iev_freq = c->ic_freq;
865 iev.iev_ieee = c->ic_ieee;
867 iev.iev_count = count;
868 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
870 CURVNET_SET(ifp->if_vnet);
871 rt_ieee80211msg(ifp, RTM_IEEE80211_CSA, &iev, sizeof(iev));
877 ieee80211_notify_radar(struct ieee80211com *ic,
878 const struct ieee80211_channel *c)
880 struct ieee80211_radar_event iev;
881 struct ieee80211vap *vap;
884 memset(&iev, 0, sizeof(iev));
885 iev.iev_flags = c->ic_flags;
886 iev.iev_freq = c->ic_freq;
887 iev.iev_ieee = c->ic_ieee;
888 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
890 CURVNET_SET(ifp->if_vnet);
891 rt_ieee80211msg(ifp, RTM_IEEE80211_RADAR, &iev, sizeof(iev));
897 ieee80211_notify_cac(struct ieee80211com *ic,
898 const struct ieee80211_channel *c, enum ieee80211_notify_cac_event type)
900 struct ieee80211_cac_event iev;
901 struct ieee80211vap *vap;
904 memset(&iev, 0, sizeof(iev));
905 iev.iev_flags = c->ic_flags;
906 iev.iev_freq = c->ic_freq;
907 iev.iev_ieee = c->ic_ieee;
909 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
911 CURVNET_SET(ifp->if_vnet);
912 rt_ieee80211msg(ifp, RTM_IEEE80211_CAC, &iev, sizeof(iev));
918 ieee80211_notify_node_deauth(struct ieee80211_node *ni)
920 struct ieee80211vap *vap = ni->ni_vap;
921 struct ifnet *ifp = vap->iv_ifp;
923 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node deauth");
925 notify_macaddr(ifp, RTM_IEEE80211_DEAUTH, ni->ni_macaddr);
929 ieee80211_notify_node_auth(struct ieee80211_node *ni)
931 struct ieee80211vap *vap = ni->ni_vap;
932 struct ifnet *ifp = vap->iv_ifp;
934 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node auth");
936 notify_macaddr(ifp, RTM_IEEE80211_AUTH, ni->ni_macaddr);
940 ieee80211_notify_country(struct ieee80211vap *vap,
941 const uint8_t bssid[IEEE80211_ADDR_LEN], const uint8_t cc[2])
943 struct ifnet *ifp = vap->iv_ifp;
944 struct ieee80211_country_event iev;
946 memset(&iev, 0, sizeof(iev));
947 IEEE80211_ADDR_COPY(iev.iev_addr, bssid);
948 iev.iev_cc[0] = cc[0];
949 iev.iev_cc[1] = cc[1];
950 CURVNET_SET(ifp->if_vnet);
951 rt_ieee80211msg(ifp, RTM_IEEE80211_COUNTRY, &iev, sizeof(iev));
956 ieee80211_notify_radio(struct ieee80211com *ic, int state)
958 struct ieee80211_radio_event iev;
959 struct ieee80211vap *vap;
962 memset(&iev, 0, sizeof(iev));
963 iev.iev_state = state;
964 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
966 CURVNET_SET(ifp->if_vnet);
967 rt_ieee80211msg(ifp, RTM_IEEE80211_RADIO, &iev, sizeof(iev));
973 ieee80211_notify_ifnet_change(struct ieee80211vap *vap)
975 struct ifnet *ifp = vap->iv_ifp;
977 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG, "%s\n",
978 "interface state change");
980 CURVNET_SET(ifp->if_vnet);
986 ieee80211_load_module(const char *modname)
990 (void)kern_kldload(curthread, modname, NULL);
992 printf("%s: load the %s module by hand for now.\n", __func__, modname);
996 static eventhandler_tag wlan_bpfevent;
997 static eventhandler_tag wlan_ifllevent;
1000 bpf_track(void *arg, struct ifnet *ifp, int dlt, int attach)
1002 /* NB: identify vap's by if_init */
1003 if (dlt == DLT_IEEE802_11_RADIO &&
1004 ifp->if_init == ieee80211_init) {
1005 struct ieee80211vap *vap = ifp->if_softc;
1007 * Track bpf radiotap listener state. We mark the vap
1008 * to indicate if any listener is present and the com
1009 * to indicate if any listener exists on any associated
1010 * vap. This flag is used by drivers to prepare radiotap
1011 * state only when needed.
1014 ieee80211_syncflag_ext(vap, IEEE80211_FEXT_BPF);
1015 if (vap->iv_opmode == IEEE80211_M_MONITOR)
1016 atomic_add_int(&vap->iv_ic->ic_montaps, 1);
1017 } else if (!bpf_peers_present(vap->iv_rawbpf)) {
1018 ieee80211_syncflag_ext(vap, -IEEE80211_FEXT_BPF);
1019 if (vap->iv_opmode == IEEE80211_M_MONITOR)
1020 atomic_subtract_int(&vap->iv_ic->ic_montaps, 1);
1026 * Change MAC address on the vap (if was not started).
1029 wlan_iflladdr(void *arg __unused, struct ifnet *ifp)
1031 /* NB: identify vap's by if_init */
1032 if (ifp->if_init == ieee80211_init &&
1033 (ifp->if_flags & IFF_UP) == 0) {
1034 struct ieee80211vap *vap = ifp->if_softc;
1036 IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp));
1041 * Fetch the VAP name.
1043 * This returns a const char pointer suitable for debugging,
1044 * but don't expect it to stick around for much longer.
1047 ieee80211_get_vap_ifname(struct ieee80211vap *vap)
1049 if (vap->iv_ifp == NULL)
1051 return vap->iv_ifp->if_xname;
1057 * NB: the module name is "wlan" for compatibility with NetBSD.
1060 wlan_modevent(module_t mod, int type, void *unused)
1065 printf("wlan: <802.11 Link Layer>\n");
1066 wlan_bpfevent = EVENTHANDLER_REGISTER(bpf_track,
1067 bpf_track, 0, EVENTHANDLER_PRI_ANY);
1068 wlan_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
1069 wlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
1070 wlan_cloner = if_clone_simple(wlanname, wlan_clone_create,
1071 wlan_clone_destroy, 0);
1074 if_clone_detach(wlan_cloner);
1075 EVENTHANDLER_DEREGISTER(bpf_track, wlan_bpfevent);
1076 EVENTHANDLER_DEREGISTER(iflladdr_event, wlan_ifllevent);
1082 static moduledata_t wlan_mod = {
1087 DECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
1088 MODULE_VERSION(wlan, 1);
1089 MODULE_DEPEND(wlan, ether, 1, 1, 1);
1090 #ifdef IEEE80211_ALQ
1091 MODULE_DEPEND(wlan, alq, 1, 1, 1);
1092 #endif /* IEEE80211_ALQ */