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>
51 #include <net/debugnet.h>
53 #include <net/if_var.h>
54 #include <net/if_dl.h>
55 #include <net/if_clone.h>
56 #include <net/if_media.h>
57 #include <net/if_private.h>
58 #include <net/if_types.h>
59 #include <net/ethernet.h>
60 #include <net/route.h>
63 #include <net80211/ieee80211_var.h>
64 #include <net80211/ieee80211_input.h>
66 DEBUGNET_DEFINE(ieee80211);
67 SYSCTL_NODE(_net, OID_AUTO, wlan, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
68 "IEEE 80211 parameters");
70 #ifdef IEEE80211_DEBUG
71 static int ieee80211_debug = 0;
72 SYSCTL_INT(_net_wlan, OID_AUTO, debug, CTLFLAG_RW, &ieee80211_debug,
73 0, "debugging printfs");
76 static const char wlanname[] = "wlan";
77 static struct if_clone *wlan_cloner;
80 * priv(9) NET80211 checks.
81 * Return 0 if operation is allowed, E* (usually EPERM) otherwise.
84 ieee80211_priv_check_vap_getkey(u_long cmd __unused,
85 struct ieee80211vap *vap __unused, struct ifnet *ifp __unused)
88 return (priv_check(curthread, PRIV_NET80211_VAP_GETKEY));
92 ieee80211_priv_check_vap_manage(u_long cmd __unused,
93 struct ieee80211vap *vap __unused, struct ifnet *ifp __unused)
96 return (priv_check(curthread, PRIV_NET80211_VAP_MANAGE));
100 ieee80211_priv_check_vap_setmac(u_long cmd __unused,
101 struct ieee80211vap *vap __unused, struct ifnet *ifp __unused)
104 return (priv_check(curthread, PRIV_NET80211_VAP_SETMAC));
108 ieee80211_priv_check_create_vap(u_long cmd __unused,
109 struct ieee80211vap *vap __unused, struct ifnet *ifp __unused)
112 return (priv_check(curthread, PRIV_NET80211_CREATE_VAP));
116 wlan_clone_create(struct if_clone *ifc, char *name, size_t len,
117 struct ifc_data *ifd, struct ifnet **ifpp)
119 struct ieee80211_clone_params cp;
120 struct ieee80211vap *vap;
121 struct ieee80211com *ic;
124 error = ieee80211_priv_check_create_vap(0, NULL, NULL);
128 error = ifc_copyin(ifd, &cp, sizeof(cp));
131 ic = ieee80211_find_com(cp.icp_parent);
134 if (cp.icp_opmode >= IEEE80211_OPMODE_MAX) {
135 ic_printf(ic, "%s: invalid opmode %d\n", __func__,
139 if ((ic->ic_caps & ieee80211_opcap[cp.icp_opmode]) == 0) {
140 ic_printf(ic, "%s mode not supported\n",
141 ieee80211_opmode_name[cp.icp_opmode]);
144 if ((cp.icp_flags & IEEE80211_CLONE_TDMA) &&
145 #ifdef IEEE80211_SUPPORT_TDMA
146 (ic->ic_caps & IEEE80211_C_TDMA) == 0
151 ic_printf(ic, "TDMA not supported\n");
154 vap = ic->ic_vap_create(ic, wlanname, ifd->unit,
155 cp.icp_opmode, cp.icp_flags, cp.icp_bssid,
156 cp.icp_flags & IEEE80211_CLONE_MACADDR ?
157 cp.icp_macaddr : ic->ic_macaddr);
163 if (ic->ic_debugnet_meth != NULL)
164 DEBUGNET_SET(vap->iv_ifp, ieee80211);
172 wlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
174 struct ieee80211vap *vap = ifp->if_softc;
175 struct ieee80211com *ic = vap->iv_ic;
177 ic->ic_vap_delete(vap);
183 ieee80211_vap_destroy(struct ieee80211vap *vap)
185 CURVNET_SET(vap->iv_ifp->if_vnet);
186 if_clone_destroyif(wlan_cloner, vap->iv_ifp);
191 ieee80211_sysctl_msecs_ticks(SYSCTL_HANDLER_ARGS)
193 int msecs = ticks_to_msecs(*(int *)arg1);
196 error = sysctl_handle_int(oidp, &msecs, 0, req);
197 if (error || !req->newptr)
199 *(int *)arg1 = msecs_to_ticks(msecs);
204 ieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS)
206 int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT;
209 error = sysctl_handle_int(oidp, &inact, 0, req);
210 if (error || !req->newptr)
212 *(int *)arg1 = inact / IEEE80211_INACT_WAIT;
217 ieee80211_sysctl_parent(SYSCTL_HANDLER_ARGS)
219 struct ieee80211com *ic = arg1;
221 return SYSCTL_OUT_STR(req, ic->ic_name);
225 ieee80211_sysctl_radar(SYSCTL_HANDLER_ARGS)
227 struct ieee80211com *ic = arg1;
230 error = sysctl_handle_int(oidp, &t, 0, req);
231 if (error || !req->newptr)
234 ieee80211_dfs_notify_radar(ic, ic->ic_curchan);
235 IEEE80211_UNLOCK(ic);
240 * For now, just restart everything.
242 * Later on, it'd be nice to have a separate VAP restart to
243 * full-device restart.
246 ieee80211_sysctl_vap_restart(SYSCTL_HANDLER_ARGS)
248 struct ieee80211vap *vap = arg1;
251 error = sysctl_handle_int(oidp, &t, 0, req);
252 if (error || !req->newptr)
255 ieee80211_restart_all(vap->iv_ic);
260 ieee80211_sysctl_attach(struct ieee80211com *ic)
265 ieee80211_sysctl_detach(struct ieee80211com *ic)
270 ieee80211_sysctl_vattach(struct ieee80211vap *vap)
272 struct ifnet *ifp = vap->iv_ifp;
273 struct sysctl_ctx_list *ctx;
274 struct sysctl_oid *oid;
275 char num[14]; /* sufficient for 32 bits */
277 ctx = (struct sysctl_ctx_list *) IEEE80211_MALLOC(sizeof(struct sysctl_ctx_list),
278 M_DEVBUF, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
280 if_printf(ifp, "%s: cannot allocate sysctl context!\n",
284 sysctl_ctx_init(ctx);
285 snprintf(num, sizeof(num), "%u", ifp->if_dunit);
286 oid = SYSCTL_ADD_NODE(ctx, &SYSCTL_NODE_CHILDREN(_net, wlan),
287 OID_AUTO, num, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
288 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
289 "%parent", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
290 vap->iv_ic, 0, ieee80211_sysctl_parent, "A", "parent device");
291 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
292 "driver_caps", CTLFLAG_RW, &vap->iv_caps, 0,
293 "driver capabilities");
294 #ifdef IEEE80211_DEBUG
295 vap->iv_debug = ieee80211_debug;
296 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
297 "debug", CTLFLAG_RW, &vap->iv_debug, 0,
298 "control debugging printfs");
300 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
301 "bmiss_max", CTLFLAG_RW, &vap->iv_bmiss_max, 0,
302 "consecutive beacon misses before scanning");
303 /* XXX inherit from tunables */
304 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
305 "inact_run", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
306 &vap->iv_inact_run, 0, ieee80211_sysctl_inact, "I",
307 "station inactivity timeout (sec)");
308 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
309 "inact_probe", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
310 &vap->iv_inact_probe, 0, ieee80211_sysctl_inact, "I",
311 "station inactivity probe timeout (sec)");
312 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
313 "inact_auth", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
314 &vap->iv_inact_auth, 0, ieee80211_sysctl_inact, "I",
315 "station authentication timeout (sec)");
316 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
317 "inact_init", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
318 &vap->iv_inact_init, 0, ieee80211_sysctl_inact, "I",
319 "station initial state timeout (sec)");
320 if (vap->iv_htcaps & IEEE80211_HTC_HT) {
321 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
322 "ampdu_mintraffic_bk", CTLFLAG_RW,
323 &vap->iv_ampdu_mintraffic[WME_AC_BK], 0,
324 "BK traffic tx aggr threshold (pps)");
325 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
326 "ampdu_mintraffic_be", CTLFLAG_RW,
327 &vap->iv_ampdu_mintraffic[WME_AC_BE], 0,
328 "BE traffic tx aggr threshold (pps)");
329 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
330 "ampdu_mintraffic_vo", CTLFLAG_RW,
331 &vap->iv_ampdu_mintraffic[WME_AC_VO], 0,
332 "VO traffic tx aggr threshold (pps)");
333 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
334 "ampdu_mintraffic_vi", CTLFLAG_RW,
335 &vap->iv_ampdu_mintraffic[WME_AC_VI], 0,
336 "VI traffic tx aggr threshold (pps)");
339 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
340 "force_restart", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
341 vap, 0, ieee80211_sysctl_vap_restart, "I", "force a VAP restart");
343 if (vap->iv_caps & IEEE80211_C_DFS) {
344 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
345 "radar", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
346 vap->iv_ic, 0, ieee80211_sysctl_radar, "I",
347 "simulate radar event");
349 vap->iv_sysctl = ctx;
354 ieee80211_sysctl_vdetach(struct ieee80211vap *vap)
357 if (vap->iv_sysctl != NULL) {
358 sysctl_ctx_free(vap->iv_sysctl);
359 IEEE80211_FREE(vap->iv_sysctl, M_DEVBUF);
360 vap->iv_sysctl = NULL;
365 ieee80211_com_vincref(struct ieee80211vap *vap)
369 ostate = atomic_fetchadd_32(&vap->iv_com_state, IEEE80211_COM_REF_ADD);
371 if (ostate & IEEE80211_COM_DETACHED) {
372 atomic_subtract_32(&vap->iv_com_state, IEEE80211_COM_REF_ADD);
376 if (_IEEE80211_MASKSHIFT(ostate, IEEE80211_COM_REF) ==
377 IEEE80211_COM_REF_MAX) {
378 atomic_subtract_32(&vap->iv_com_state, IEEE80211_COM_REF_ADD);
386 ieee80211_com_vdecref(struct ieee80211vap *vap)
390 ostate = atomic_fetchadd_32(&vap->iv_com_state, -IEEE80211_COM_REF_ADD);
392 KASSERT(_IEEE80211_MASKSHIFT(ostate, IEEE80211_COM_REF) != 0,
393 ("com reference counter underflow"));
399 ieee80211_com_vdetach(struct ieee80211vap *vap)
403 sleep_time = msecs_to_ticks(250);
404 atomic_set_32(&vap->iv_com_state, IEEE80211_COM_DETACHED);
405 while (_IEEE80211_MASKSHIFT(atomic_load_32(&vap->iv_com_state),
406 IEEE80211_COM_REF) != 0)
407 pause("comref", sleep_time);
411 ieee80211_node_dectestref(struct ieee80211_node *ni)
413 /* XXX need equivalent of atomic_dec_and_test */
414 atomic_subtract_int(&ni->ni_refcnt, 1);
415 return atomic_cmpset_int(&ni->ni_refcnt, 0, 1);
419 ieee80211_drain_ifq(struct ifqueue *ifq)
421 struct ieee80211_node *ni;
429 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
430 KASSERT(ni != NULL, ("frame w/o node"));
431 ieee80211_free_node(ni);
432 m->m_pkthdr.rcvif = NULL;
439 ieee80211_flush_ifq(struct ifqueue *ifq, struct ieee80211vap *vap)
441 struct ieee80211_node *ni;
442 struct mbuf *m, **mprev;
445 mprev = &ifq->ifq_head;
446 while ((m = *mprev) != NULL) {
447 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
448 if (ni != NULL && ni->ni_vap == vap) {
449 *mprev = m->m_nextpkt; /* remove from list */
453 ieee80211_free_node(ni); /* reclaim ref */
455 mprev = &m->m_nextpkt;
457 /* recalculate tail ptr */
459 for (; m != NULL && m->m_nextpkt != NULL; m = m->m_nextpkt)
466 * As above, for mbufs allocated with m_gethdr/MGETHDR
467 * or initialized by M_COPY_PKTHDR.
469 #define MC_ALIGN(m, len) \
471 (m)->m_data += rounddown2(MCLBYTES - (len), sizeof(long)); \
472 } while (/* CONSTCOND */ 0)
475 * Allocate and setup a management frame of the specified
476 * size. We return the mbuf and a pointer to the start
477 * of the contiguous data area that's been reserved based
478 * on the packet length. The data area is forced to 32-bit
479 * alignment and the buffer length to a multiple of 4 bytes.
480 * This is done mainly so beacon frames (that require this)
481 * can use this interface too.
484 ieee80211_getmgtframe(uint8_t **frm, int headroom, int pktlen)
490 * NB: we know the mbuf routines will align the data area
491 * so we don't need to do anything special.
493 len = roundup2(headroom + pktlen, 4);
494 KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len));
495 if (len < MINCLSIZE) {
496 m = m_gethdr(IEEE80211_M_NOWAIT, MT_DATA);
498 * Align the data in case additional headers are added.
499 * This should only happen when a WEP header is added
500 * which only happens for shared key authentication mgt
501 * frames which all fit in MHLEN.
506 m = m_getcl(IEEE80211_M_NOWAIT, MT_DATA, M_PKTHDR);
511 m->m_data += headroom;
517 #ifndef __NO_STRICT_ALIGNMENT
519 * Re-align the payload in the mbuf. This is mainly used (right now)
520 * to handle IP header alignment requirements on certain architectures.
523 ieee80211_realign(struct ieee80211vap *vap, struct mbuf *m, size_t align)
528 pktlen = m->m_pkthdr.len;
529 space = pktlen + align;
530 if (space < MINCLSIZE)
531 n = m_gethdr(IEEE80211_M_NOWAIT, MT_DATA);
533 n = m_getjcl(IEEE80211_M_NOWAIT, MT_DATA, M_PKTHDR,
534 space <= MCLBYTES ? MCLBYTES :
535 #if MJUMPAGESIZE != MCLBYTES
536 space <= MJUMPAGESIZE ? MJUMPAGESIZE :
538 space <= MJUM9BYTES ? MJUM9BYTES : MJUM16BYTES);
540 if (__predict_true(n != NULL)) {
542 n->m_data = (caddr_t)(ALIGN(n->m_data + align) - align);
543 m_copydata(m, 0, pktlen, mtod(n, caddr_t));
546 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
547 mtod(m, const struct ieee80211_frame *), NULL,
548 "%s", "no mbuf to realign");
549 vap->iv_stats.is_rx_badalign++;
554 #endif /* !__NO_STRICT_ALIGNMENT */
557 ieee80211_add_callback(struct mbuf *m,
558 void (*func)(struct ieee80211_node *, void *, int), void *arg)
561 struct ieee80211_cb *cb;
563 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_CALLBACK,
564 sizeof(struct ieee80211_cb), IEEE80211_M_NOWAIT);
568 cb = (struct ieee80211_cb *)(mtag+1);
571 m_tag_prepend(m, mtag);
572 m->m_flags |= M_TXCB;
577 ieee80211_add_xmit_params(struct mbuf *m,
578 const struct ieee80211_bpf_params *params)
581 struct ieee80211_tx_params *tx;
583 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_XMIT_PARAMS,
584 sizeof(struct ieee80211_tx_params), IEEE80211_M_NOWAIT);
588 tx = (struct ieee80211_tx_params *)(mtag+1);
589 memcpy(&tx->params, params, sizeof(struct ieee80211_bpf_params));
590 m_tag_prepend(m, mtag);
595 ieee80211_get_xmit_params(struct mbuf *m,
596 struct ieee80211_bpf_params *params)
599 struct ieee80211_tx_params *tx;
601 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_XMIT_PARAMS,
605 tx = (struct ieee80211_tx_params *)(mtag + 1);
606 memcpy(params, &tx->params, sizeof(struct ieee80211_bpf_params));
611 ieee80211_process_callback(struct ieee80211_node *ni,
612 struct mbuf *m, int status)
616 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_CALLBACK, NULL);
618 struct ieee80211_cb *cb = (struct ieee80211_cb *)(mtag+1);
619 cb->func(ni, cb->arg, status);
624 * Add RX parameters to the given mbuf.
626 * Returns 1 if OK, 0 on error.
629 ieee80211_add_rx_params(struct mbuf *m, const struct ieee80211_rx_stats *rxs)
632 struct ieee80211_rx_params *rx;
634 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_RECV_PARAMS,
635 sizeof(struct ieee80211_rx_stats), IEEE80211_M_NOWAIT);
639 rx = (struct ieee80211_rx_params *)(mtag + 1);
640 memcpy(&rx->params, rxs, sizeof(*rxs));
641 m_tag_prepend(m, mtag);
646 ieee80211_get_rx_params(struct mbuf *m, struct ieee80211_rx_stats *rxs)
649 struct ieee80211_rx_params *rx;
651 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_RECV_PARAMS,
655 rx = (struct ieee80211_rx_params *)(mtag + 1);
656 memcpy(rxs, &rx->params, sizeof(*rxs));
660 const struct ieee80211_rx_stats *
661 ieee80211_get_rx_params_ptr(struct mbuf *m)
664 struct ieee80211_rx_params *rx;
666 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_RECV_PARAMS,
670 rx = (struct ieee80211_rx_params *)(mtag + 1);
671 return (&rx->params);
675 * Add TOA parameters to the given mbuf.
678 ieee80211_add_toa_params(struct mbuf *m, const struct ieee80211_toa_params *p)
681 struct ieee80211_toa_params *rp;
683 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_TOA_PARAMS,
684 sizeof(struct ieee80211_toa_params), IEEE80211_M_NOWAIT);
688 rp = (struct ieee80211_toa_params *)(mtag + 1);
689 memcpy(rp, p, sizeof(*rp));
690 m_tag_prepend(m, mtag);
695 ieee80211_get_toa_params(struct mbuf *m, struct ieee80211_toa_params *p)
698 struct ieee80211_toa_params *rp;
700 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_TOA_PARAMS,
704 rp = (struct ieee80211_toa_params *)(mtag + 1);
706 memcpy(p, rp, sizeof(*p));
711 * Transmit a frame to the parent interface.
714 ieee80211_parent_xmitpkt(struct ieee80211com *ic, struct mbuf *m)
719 * Assert the IC TX lock is held - this enforces the
720 * processing -> queuing order is maintained
722 IEEE80211_TX_LOCK_ASSERT(ic);
723 error = ic->ic_transmit(ic, m);
725 struct ieee80211_node *ni;
727 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
729 /* XXX number of fragments */
730 if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 1);
731 ieee80211_free_node(ni);
732 ieee80211_free_mbuf(m);
738 * Transmit a frame to the VAP interface.
741 ieee80211_vap_xmitpkt(struct ieee80211vap *vap, struct mbuf *m)
743 struct ifnet *ifp = vap->iv_ifp;
746 * When transmitting via the VAP, we shouldn't hold
747 * any IC TX lock as the VAP TX path will acquire it.
749 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
751 return (ifp->if_transmit(ifp, m));
755 #include <sys/libkern.h>
758 net80211_get_random_bytes(void *p, size_t n)
763 uint32_t v = arc4random();
764 size_t nb = n > sizeof(uint32_t) ? sizeof(uint32_t) : n;
765 bcopy(&v, dp, n > sizeof(uint32_t) ? sizeof(uint32_t) : n);
766 dp += sizeof(uint32_t), n -= nb;
771 * Helper function for events that pass just a single mac address.
774 notify_macaddr(struct ifnet *ifp, int op, const uint8_t mac[IEEE80211_ADDR_LEN])
776 struct ieee80211_join_event iev;
778 CURVNET_SET(ifp->if_vnet);
779 memset(&iev, 0, sizeof(iev));
780 IEEE80211_ADDR_COPY(iev.iev_addr, mac);
781 rt_ieee80211msg(ifp, op, &iev, sizeof(iev));
786 ieee80211_notify_node_join(struct ieee80211_node *ni, int newassoc)
788 struct ieee80211vap *vap = ni->ni_vap;
789 struct ifnet *ifp = vap->iv_ifp;
791 CURVNET_SET_QUIET(ifp->if_vnet);
792 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode join",
793 (ni == vap->iv_bss) ? "bss " : "");
795 if (ni == vap->iv_bss) {
796 notify_macaddr(ifp, newassoc ?
797 RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC, ni->ni_bssid);
798 if_link_state_change(ifp, LINK_STATE_UP);
800 notify_macaddr(ifp, newassoc ?
801 RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN, ni->ni_macaddr);
807 ieee80211_notify_node_leave(struct ieee80211_node *ni)
809 struct ieee80211vap *vap = ni->ni_vap;
810 struct ifnet *ifp = vap->iv_ifp;
812 CURVNET_SET_QUIET(ifp->if_vnet);
813 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode leave",
814 (ni == vap->iv_bss) ? "bss " : "");
816 if (ni == vap->iv_bss) {
817 rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
818 if_link_state_change(ifp, LINK_STATE_DOWN);
820 /* fire off wireless event station leaving */
821 notify_macaddr(ifp, RTM_IEEE80211_LEAVE, ni->ni_macaddr);
827 ieee80211_notify_scan_done(struct ieee80211vap *vap)
829 struct ifnet *ifp = vap->iv_ifp;
831 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n", "notify scan done");
833 /* dispatch wireless event indicating scan completed */
834 CURVNET_SET(ifp->if_vnet);
835 rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
840 ieee80211_notify_replay_failure(struct ieee80211vap *vap,
841 const struct ieee80211_frame *wh, const struct ieee80211_key *k,
842 u_int64_t rsc, int tid)
844 struct ifnet *ifp = vap->iv_ifp;
846 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
847 "%s replay detected tid %d <rsc %ju (%jx), csc %ju (%jx), keyix %u rxkeyix %u>",
848 k->wk_cipher->ic_name, tid,
851 (intmax_t) k->wk_keyrsc[tid],
852 (intmax_t) k->wk_keyrsc[tid],
853 k->wk_keyix, k->wk_rxkeyix);
855 if (ifp != NULL) { /* NB: for cipher test modules */
856 struct ieee80211_replay_event iev;
858 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
859 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
860 iev.iev_cipher = k->wk_cipher->ic_cipher;
861 if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE)
862 iev.iev_keyix = k->wk_rxkeyix;
864 iev.iev_keyix = k->wk_keyix;
865 iev.iev_keyrsc = k->wk_keyrsc[tid];
867 CURVNET_SET(ifp->if_vnet);
868 rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
874 ieee80211_notify_michael_failure(struct ieee80211vap *vap,
875 const struct ieee80211_frame *wh, u_int keyix)
877 struct ifnet *ifp = vap->iv_ifp;
879 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
880 "michael MIC verification failed <keyix %u>", keyix);
881 vap->iv_stats.is_rx_tkipmic++;
883 if (ifp != NULL) { /* NB: for cipher test modules */
884 struct ieee80211_michael_event iev;
886 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
887 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
888 iev.iev_cipher = IEEE80211_CIPHER_TKIP;
889 iev.iev_keyix = keyix;
890 CURVNET_SET(ifp->if_vnet);
891 rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev));
897 ieee80211_notify_wds_discover(struct ieee80211_node *ni)
899 struct ieee80211vap *vap = ni->ni_vap;
900 struct ifnet *ifp = vap->iv_ifp;
902 notify_macaddr(ifp, RTM_IEEE80211_WDS, ni->ni_macaddr);
906 ieee80211_notify_csa(struct ieee80211com *ic,
907 const struct ieee80211_channel *c, int mode, int count)
909 struct ieee80211_csa_event iev;
910 struct ieee80211vap *vap;
913 memset(&iev, 0, sizeof(iev));
914 iev.iev_flags = c->ic_flags;
915 iev.iev_freq = c->ic_freq;
916 iev.iev_ieee = c->ic_ieee;
918 iev.iev_count = count;
919 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
921 CURVNET_SET(ifp->if_vnet);
922 rt_ieee80211msg(ifp, RTM_IEEE80211_CSA, &iev, sizeof(iev));
928 ieee80211_notify_radar(struct ieee80211com *ic,
929 const struct ieee80211_channel *c)
931 struct ieee80211_radar_event iev;
932 struct ieee80211vap *vap;
935 memset(&iev, 0, sizeof(iev));
936 iev.iev_flags = c->ic_flags;
937 iev.iev_freq = c->ic_freq;
938 iev.iev_ieee = c->ic_ieee;
939 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
941 CURVNET_SET(ifp->if_vnet);
942 rt_ieee80211msg(ifp, RTM_IEEE80211_RADAR, &iev, sizeof(iev));
948 ieee80211_notify_cac(struct ieee80211com *ic,
949 const struct ieee80211_channel *c, enum ieee80211_notify_cac_event type)
951 struct ieee80211_cac_event iev;
952 struct ieee80211vap *vap;
955 memset(&iev, 0, sizeof(iev));
956 iev.iev_flags = c->ic_flags;
957 iev.iev_freq = c->ic_freq;
958 iev.iev_ieee = c->ic_ieee;
960 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
962 CURVNET_SET(ifp->if_vnet);
963 rt_ieee80211msg(ifp, RTM_IEEE80211_CAC, &iev, sizeof(iev));
969 ieee80211_notify_node_deauth(struct ieee80211_node *ni)
971 struct ieee80211vap *vap = ni->ni_vap;
972 struct ifnet *ifp = vap->iv_ifp;
974 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node deauth");
976 notify_macaddr(ifp, RTM_IEEE80211_DEAUTH, ni->ni_macaddr);
980 ieee80211_notify_node_auth(struct ieee80211_node *ni)
982 struct ieee80211vap *vap = ni->ni_vap;
983 struct ifnet *ifp = vap->iv_ifp;
985 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node auth");
987 notify_macaddr(ifp, RTM_IEEE80211_AUTH, ni->ni_macaddr);
991 ieee80211_notify_country(struct ieee80211vap *vap,
992 const uint8_t bssid[IEEE80211_ADDR_LEN], const uint8_t cc[2])
994 struct ifnet *ifp = vap->iv_ifp;
995 struct ieee80211_country_event iev;
997 memset(&iev, 0, sizeof(iev));
998 IEEE80211_ADDR_COPY(iev.iev_addr, bssid);
999 iev.iev_cc[0] = cc[0];
1000 iev.iev_cc[1] = cc[1];
1001 CURVNET_SET(ifp->if_vnet);
1002 rt_ieee80211msg(ifp, RTM_IEEE80211_COUNTRY, &iev, sizeof(iev));
1007 ieee80211_notify_radio(struct ieee80211com *ic, int state)
1009 struct ieee80211_radio_event iev;
1010 struct ieee80211vap *vap;
1013 memset(&iev, 0, sizeof(iev));
1014 iev.iev_state = state;
1015 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1017 CURVNET_SET(ifp->if_vnet);
1018 rt_ieee80211msg(ifp, RTM_IEEE80211_RADIO, &iev, sizeof(iev));
1024 ieee80211_notify_ifnet_change(struct ieee80211vap *vap, int if_flags_mask)
1026 struct ifnet *ifp = vap->iv_ifp;
1028 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG, "%s\n",
1029 "interface state change");
1031 CURVNET_SET(ifp->if_vnet);
1032 rt_ifmsg(ifp, if_flags_mask);
1037 ieee80211_load_module(const char *modname)
1041 (void)kern_kldload(curthread, modname, NULL);
1043 printf("%s: load the %s module by hand for now.\n", __func__, modname);
1047 static eventhandler_tag wlan_bpfevent;
1048 static eventhandler_tag wlan_ifllevent;
1051 bpf_track(void *arg, struct ifnet *ifp, int dlt, int attach)
1053 /* NB: identify vap's by if_init */
1054 if (dlt == DLT_IEEE802_11_RADIO &&
1055 ifp->if_init == ieee80211_init) {
1056 struct ieee80211vap *vap = ifp->if_softc;
1058 * Track bpf radiotap listener state. We mark the vap
1059 * to indicate if any listener is present and the com
1060 * to indicate if any listener exists on any associated
1061 * vap. This flag is used by drivers to prepare radiotap
1062 * state only when needed.
1065 ieee80211_syncflag_ext(vap, IEEE80211_FEXT_BPF);
1066 if (vap->iv_opmode == IEEE80211_M_MONITOR)
1067 atomic_add_int(&vap->iv_ic->ic_montaps, 1);
1068 } else if (!bpf_peers_present(vap->iv_rawbpf)) {
1069 ieee80211_syncflag_ext(vap, -IEEE80211_FEXT_BPF);
1070 if (vap->iv_opmode == IEEE80211_M_MONITOR)
1071 atomic_subtract_int(&vap->iv_ic->ic_montaps, 1);
1077 * Change MAC address on the vap (if was not started).
1080 wlan_iflladdr(void *arg __unused, struct ifnet *ifp)
1082 /* NB: identify vap's by if_init */
1083 if (ifp->if_init == ieee80211_init &&
1084 (ifp->if_flags & IFF_UP) == 0) {
1085 struct ieee80211vap *vap = ifp->if_softc;
1087 IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp));
1092 * Fetch the VAP name.
1094 * This returns a const char pointer suitable for debugging,
1095 * but don't expect it to stick around for much longer.
1098 ieee80211_get_vap_ifname(struct ieee80211vap *vap)
1100 if (vap->iv_ifp == NULL)
1102 return vap->iv_ifp->if_xname;
1107 ieee80211_debugnet_init(struct ifnet *ifp, int *nrxr, int *ncl, int *clsize)
1109 struct ieee80211vap *vap;
1110 struct ieee80211com *ic;
1112 vap = if_getsoftc(ifp);
1116 ic->ic_debugnet_meth->dn8_init(ic, nrxr, ncl, clsize);
1117 IEEE80211_UNLOCK(ic);
1121 ieee80211_debugnet_event(struct ifnet *ifp, enum debugnet_ev ev)
1123 struct ieee80211vap *vap;
1124 struct ieee80211com *ic;
1126 vap = if_getsoftc(ifp);
1130 ic->ic_debugnet_meth->dn8_event(ic, ev);
1131 IEEE80211_UNLOCK(ic);
1135 ieee80211_debugnet_transmit(struct ifnet *ifp, struct mbuf *m)
1137 return (ieee80211_vap_transmit(ifp, m));
1141 ieee80211_debugnet_poll(struct ifnet *ifp, int count)
1143 struct ieee80211vap *vap;
1144 struct ieee80211com *ic;
1146 vap = if_getsoftc(ifp);
1149 return (ic->ic_debugnet_meth->dn8_poll(ic, count));
1156 * NB: the module name is "wlan" for compatibility with NetBSD.
1159 wlan_modevent(module_t mod, int type, void *unused)
1164 printf("wlan: <802.11 Link Layer>\n");
1165 wlan_bpfevent = EVENTHANDLER_REGISTER(bpf_track,
1166 bpf_track, 0, EVENTHANDLER_PRI_ANY);
1167 wlan_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
1168 wlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
1169 struct if_clone_addreq req = {
1170 .create_f = wlan_clone_create,
1171 .destroy_f = wlan_clone_destroy,
1172 .flags = IFC_F_AUTOUNIT,
1174 wlan_cloner = ifc_attach_cloner(wlanname, &req);
1177 ifc_detach_cloner(wlan_cloner);
1178 EVENTHANDLER_DEREGISTER(bpf_track, wlan_bpfevent);
1179 EVENTHANDLER_DEREGISTER(iflladdr_event, wlan_ifllevent);
1185 static moduledata_t wlan_mod = {
1190 DECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
1191 MODULE_VERSION(wlan, 1);
1192 MODULE_DEPEND(wlan, ether, 1, 1, 1);
1193 #ifdef IEEE80211_ALQ
1194 MODULE_DEPEND(wlan, alq, 1, 1, 1);
1195 #endif /* IEEE80211_ALQ */