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 t = msecs_to_ticks(msecs);
145 *(int *)arg1 = (t < 1) ? 1 : t;
150 ieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS)
152 int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT;
155 error = sysctl_handle_int(oidp, &inact, 0, req);
156 if (error || !req->newptr)
158 *(int *)arg1 = inact / IEEE80211_INACT_WAIT;
163 ieee80211_sysctl_parent(SYSCTL_HANDLER_ARGS)
165 struct ieee80211com *ic = arg1;
167 return SYSCTL_OUT_STR(req, ic->ic_name);
171 ieee80211_sysctl_radar(SYSCTL_HANDLER_ARGS)
173 struct ieee80211com *ic = arg1;
176 error = sysctl_handle_int(oidp, &t, 0, req);
177 if (error || !req->newptr)
180 ieee80211_dfs_notify_radar(ic, ic->ic_curchan);
181 IEEE80211_UNLOCK(ic);
186 * For now, just restart everything.
188 * Later on, it'd be nice to have a separate VAP restart to
189 * full-device restart.
192 ieee80211_sysctl_vap_restart(SYSCTL_HANDLER_ARGS)
194 struct ieee80211vap *vap = arg1;
197 error = sysctl_handle_int(oidp, &t, 0, req);
198 if (error || !req->newptr)
201 ieee80211_restart_all(vap->iv_ic);
206 ieee80211_sysctl_attach(struct ieee80211com *ic)
211 ieee80211_sysctl_detach(struct ieee80211com *ic)
216 ieee80211_sysctl_vattach(struct ieee80211vap *vap)
218 struct ifnet *ifp = vap->iv_ifp;
219 struct sysctl_ctx_list *ctx;
220 struct sysctl_oid *oid;
221 char num[14]; /* sufficient for 32 bits */
223 ctx = (struct sysctl_ctx_list *) IEEE80211_MALLOC(sizeof(struct sysctl_ctx_list),
224 M_DEVBUF, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
226 if_printf(ifp, "%s: cannot allocate sysctl context!\n",
230 sysctl_ctx_init(ctx);
231 snprintf(num, sizeof(num), "%u", ifp->if_dunit);
232 oid = SYSCTL_ADD_NODE(ctx, &SYSCTL_NODE_CHILDREN(_net, wlan),
233 OID_AUTO, num, CTLFLAG_RD, NULL, "");
234 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
235 "%parent", CTLTYPE_STRING | CTLFLAG_RD, vap->iv_ic, 0,
236 ieee80211_sysctl_parent, "A", "parent device");
237 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
238 "driver_caps", CTLFLAG_RW, &vap->iv_caps, 0,
239 "driver capabilities");
240 #ifdef IEEE80211_DEBUG
241 vap->iv_debug = ieee80211_debug;
242 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
243 "debug", CTLFLAG_RW, &vap->iv_debug, 0,
244 "control debugging printfs");
246 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
247 "bmiss_max", CTLFLAG_RW, &vap->iv_bmiss_max, 0,
248 "consecutive beacon misses before scanning");
249 /* XXX inherit from tunables */
250 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
251 "inact_run", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_run, 0,
252 ieee80211_sysctl_inact, "I",
253 "station inactivity timeout (sec)");
254 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
255 "inact_probe", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_probe, 0,
256 ieee80211_sysctl_inact, "I",
257 "station inactivity probe timeout (sec)");
258 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
259 "inact_auth", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_auth, 0,
260 ieee80211_sysctl_inact, "I",
261 "station authentication timeout (sec)");
262 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
263 "inact_init", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_init, 0,
264 ieee80211_sysctl_inact, "I",
265 "station initial state timeout (sec)");
266 if (vap->iv_htcaps & IEEE80211_HTC_HT) {
267 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
268 "ampdu_mintraffic_bk", CTLFLAG_RW,
269 &vap->iv_ampdu_mintraffic[WME_AC_BK], 0,
270 "BK traffic tx aggr threshold (pps)");
271 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
272 "ampdu_mintraffic_be", CTLFLAG_RW,
273 &vap->iv_ampdu_mintraffic[WME_AC_BE], 0,
274 "BE traffic tx aggr threshold (pps)");
275 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
276 "ampdu_mintraffic_vo", CTLFLAG_RW,
277 &vap->iv_ampdu_mintraffic[WME_AC_VO], 0,
278 "VO traffic tx aggr threshold (pps)");
279 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
280 "ampdu_mintraffic_vi", CTLFLAG_RW,
281 &vap->iv_ampdu_mintraffic[WME_AC_VI], 0,
282 "VI traffic tx aggr threshold (pps)");
285 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
286 "force_restart", CTLTYPE_INT | CTLFLAG_RW, vap, 0,
287 ieee80211_sysctl_vap_restart, "I",
288 "force a VAP restart");
290 if (vap->iv_caps & IEEE80211_C_DFS) {
291 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
292 "radar", CTLTYPE_INT | CTLFLAG_RW, vap->iv_ic, 0,
293 ieee80211_sysctl_radar, "I", "simulate radar event");
295 vap->iv_sysctl = ctx;
300 ieee80211_sysctl_vdetach(struct ieee80211vap *vap)
303 if (vap->iv_sysctl != NULL) {
304 sysctl_ctx_free(vap->iv_sysctl);
305 IEEE80211_FREE(vap->iv_sysctl, M_DEVBUF);
306 vap->iv_sysctl = NULL;
311 ieee80211_node_dectestref(struct ieee80211_node *ni)
313 /* XXX need equivalent of atomic_dec_and_test */
314 atomic_subtract_int(&ni->ni_refcnt, 1);
315 return atomic_cmpset_int(&ni->ni_refcnt, 0, 1);
319 ieee80211_drain_ifq(struct ifqueue *ifq)
321 struct ieee80211_node *ni;
329 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
330 KASSERT(ni != NULL, ("frame w/o node"));
331 ieee80211_free_node(ni);
332 m->m_pkthdr.rcvif = NULL;
339 ieee80211_flush_ifq(struct ifqueue *ifq, struct ieee80211vap *vap)
341 struct ieee80211_node *ni;
342 struct mbuf *m, **mprev;
345 mprev = &ifq->ifq_head;
346 while ((m = *mprev) != NULL) {
347 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
348 if (ni != NULL && ni->ni_vap == vap) {
349 *mprev = m->m_nextpkt; /* remove from list */
353 ieee80211_free_node(ni); /* reclaim ref */
355 mprev = &m->m_nextpkt;
357 /* recalculate tail ptr */
359 for (; m != NULL && m->m_nextpkt != NULL; m = m->m_nextpkt)
366 * As above, for mbufs allocated with m_gethdr/MGETHDR
367 * or initialized by M_COPY_PKTHDR.
369 #define MC_ALIGN(m, len) \
371 (m)->m_data += rounddown2(MCLBYTES - (len), sizeof(long)); \
372 } while (/* CONSTCOND */ 0)
375 * Allocate and setup a management frame of the specified
376 * size. We return the mbuf and a pointer to the start
377 * of the contiguous data area that's been reserved based
378 * on the packet length. The data area is forced to 32-bit
379 * alignment and the buffer length to a multiple of 4 bytes.
380 * This is done mainly so beacon frames (that require this)
381 * can use this interface too.
384 ieee80211_getmgtframe(uint8_t **frm, int headroom, int pktlen)
390 * NB: we know the mbuf routines will align the data area
391 * so we don't need to do anything special.
393 len = roundup2(headroom + pktlen, 4);
394 KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len));
395 if (len < MINCLSIZE) {
396 m = m_gethdr(M_NOWAIT, MT_DATA);
398 * Align the data in case additional headers are added.
399 * This should only happen when a WEP header is added
400 * which only happens for shared key authentication mgt
401 * frames which all fit in MHLEN.
406 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
411 m->m_data += headroom;
417 #ifndef __NO_STRICT_ALIGNMENT
419 * Re-align the payload in the mbuf. This is mainly used (right now)
420 * to handle IP header alignment requirements on certain architectures.
423 ieee80211_realign(struct ieee80211vap *vap, struct mbuf *m, size_t align)
428 pktlen = m->m_pkthdr.len;
429 space = pktlen + align;
430 if (space < MINCLSIZE)
431 n = m_gethdr(M_NOWAIT, MT_DATA);
433 n = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR,
434 space <= MCLBYTES ? MCLBYTES :
435 #if MJUMPAGESIZE != MCLBYTES
436 space <= MJUMPAGESIZE ? MJUMPAGESIZE :
438 space <= MJUM9BYTES ? MJUM9BYTES : MJUM16BYTES);
440 if (__predict_true(n != NULL)) {
442 n->m_data = (caddr_t)(ALIGN(n->m_data + align) - align);
443 m_copydata(m, 0, pktlen, mtod(n, caddr_t));
446 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
447 mtod(m, const struct ieee80211_frame *), NULL,
448 "%s", "no mbuf to realign");
449 vap->iv_stats.is_rx_badalign++;
454 #endif /* !__NO_STRICT_ALIGNMENT */
457 ieee80211_add_callback(struct mbuf *m,
458 void (*func)(struct ieee80211_node *, void *, int), void *arg)
461 struct ieee80211_cb *cb;
463 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_CALLBACK,
464 sizeof(struct ieee80211_cb), M_NOWAIT);
468 cb = (struct ieee80211_cb *)(mtag+1);
471 m_tag_prepend(m, mtag);
472 m->m_flags |= M_TXCB;
477 ieee80211_add_xmit_params(struct mbuf *m,
478 const struct ieee80211_bpf_params *params)
481 struct ieee80211_tx_params *tx;
483 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_XMIT_PARAMS,
484 sizeof(struct ieee80211_tx_params), M_NOWAIT);
488 tx = (struct ieee80211_tx_params *)(mtag+1);
489 memcpy(&tx->params, params, sizeof(struct ieee80211_bpf_params));
490 m_tag_prepend(m, mtag);
495 ieee80211_get_xmit_params(struct mbuf *m,
496 struct ieee80211_bpf_params *params)
499 struct ieee80211_tx_params *tx;
501 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_XMIT_PARAMS,
505 tx = (struct ieee80211_tx_params *)(mtag + 1);
506 memcpy(params, &tx->params, sizeof(struct ieee80211_bpf_params));
511 ieee80211_process_callback(struct ieee80211_node *ni,
512 struct mbuf *m, int status)
516 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_CALLBACK, NULL);
518 struct ieee80211_cb *cb = (struct ieee80211_cb *)(mtag+1);
519 cb->func(ni, cb->arg, status);
524 * Add RX parameters to the given mbuf.
526 * Returns 1 if OK, 0 on error.
529 ieee80211_add_rx_params(struct mbuf *m, const struct ieee80211_rx_stats *rxs)
532 struct ieee80211_rx_params *rx;
534 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_RECV_PARAMS,
535 sizeof(struct ieee80211_rx_stats), M_NOWAIT);
539 rx = (struct ieee80211_rx_params *)(mtag + 1);
540 memcpy(&rx->params, rxs, sizeof(*rxs));
541 m_tag_prepend(m, mtag);
546 ieee80211_get_rx_params(struct mbuf *m, struct ieee80211_rx_stats *rxs)
549 struct ieee80211_rx_params *rx;
551 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_RECV_PARAMS,
555 rx = (struct ieee80211_rx_params *)(mtag + 1);
556 memcpy(rxs, &rx->params, sizeof(*rxs));
560 const struct ieee80211_rx_stats *
561 ieee80211_get_rx_params_ptr(struct mbuf *m)
564 struct ieee80211_rx_params *rx;
566 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_RECV_PARAMS,
570 rx = (struct ieee80211_rx_params *)(mtag + 1);
571 return (&rx->params);
576 * Add TOA parameters to the given mbuf.
579 ieee80211_add_toa_params(struct mbuf *m, const struct ieee80211_toa_params *p)
582 struct ieee80211_toa_params *rp;
584 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_TOA_PARAMS,
585 sizeof(struct ieee80211_toa_params), M_NOWAIT);
589 rp = (struct ieee80211_toa_params *)(mtag + 1);
590 memcpy(rp, p, sizeof(*rp));
591 m_tag_prepend(m, mtag);
596 ieee80211_get_toa_params(struct mbuf *m, struct ieee80211_toa_params *p)
599 struct ieee80211_toa_params *rp;
601 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_TOA_PARAMS,
605 rp = (struct ieee80211_toa_params *)(mtag + 1);
607 memcpy(p, rp, sizeof(*p));
612 * Transmit a frame to the parent interface.
615 ieee80211_parent_xmitpkt(struct ieee80211com *ic, struct mbuf *m)
620 * Assert the IC TX lock is held - this enforces the
621 * processing -> queuing order is maintained
623 IEEE80211_TX_LOCK_ASSERT(ic);
624 error = ic->ic_transmit(ic, m);
626 struct ieee80211_node *ni;
628 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
630 /* XXX number of fragments */
631 if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 1);
632 ieee80211_free_node(ni);
633 ieee80211_free_mbuf(m);
639 * Transmit a frame to the VAP interface.
642 ieee80211_vap_xmitpkt(struct ieee80211vap *vap, struct mbuf *m)
644 struct ifnet *ifp = vap->iv_ifp;
647 * When transmitting via the VAP, we shouldn't hold
648 * any IC TX lock as the VAP TX path will acquire it.
650 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
652 return (ifp->if_transmit(ifp, m));
656 #include <sys/libkern.h>
659 get_random_bytes(void *p, size_t n)
664 uint32_t v = arc4random();
665 size_t nb = n > sizeof(uint32_t) ? sizeof(uint32_t) : n;
666 bcopy(&v, dp, n > sizeof(uint32_t) ? sizeof(uint32_t) : n);
667 dp += sizeof(uint32_t), n -= nb;
672 * Helper function for events that pass just a single mac address.
675 notify_macaddr(struct ifnet *ifp, int op, const uint8_t mac[IEEE80211_ADDR_LEN])
677 struct ieee80211_join_event iev;
679 CURVNET_SET(ifp->if_vnet);
680 memset(&iev, 0, sizeof(iev));
681 IEEE80211_ADDR_COPY(iev.iev_addr, mac);
682 rt_ieee80211msg(ifp, op, &iev, sizeof(iev));
687 ieee80211_notify_node_join(struct ieee80211_node *ni, int newassoc)
689 struct ieee80211vap *vap = ni->ni_vap;
690 struct ifnet *ifp = vap->iv_ifp;
692 CURVNET_SET_QUIET(ifp->if_vnet);
693 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode join",
694 (ni == vap->iv_bss) ? "bss " : "");
696 if (ni == vap->iv_bss) {
697 notify_macaddr(ifp, newassoc ?
698 RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC, ni->ni_bssid);
699 if_link_state_change(ifp, LINK_STATE_UP);
701 notify_macaddr(ifp, newassoc ?
702 RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN, ni->ni_macaddr);
708 ieee80211_notify_node_leave(struct ieee80211_node *ni)
710 struct ieee80211vap *vap = ni->ni_vap;
711 struct ifnet *ifp = vap->iv_ifp;
713 CURVNET_SET_QUIET(ifp->if_vnet);
714 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode leave",
715 (ni == vap->iv_bss) ? "bss " : "");
717 if (ni == vap->iv_bss) {
718 rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
719 if_link_state_change(ifp, LINK_STATE_DOWN);
721 /* fire off wireless event station leaving */
722 notify_macaddr(ifp, RTM_IEEE80211_LEAVE, ni->ni_macaddr);
728 ieee80211_notify_scan_done(struct ieee80211vap *vap)
730 struct ifnet *ifp = vap->iv_ifp;
732 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n", "notify scan done");
734 /* dispatch wireless event indicating scan completed */
735 CURVNET_SET(ifp->if_vnet);
736 rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
741 ieee80211_notify_replay_failure(struct ieee80211vap *vap,
742 const struct ieee80211_frame *wh, const struct ieee80211_key *k,
743 u_int64_t rsc, int tid)
745 struct ifnet *ifp = vap->iv_ifp;
747 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
748 "%s replay detected tid %d <rsc %ju, csc %ju, keyix %u rxkeyix %u>",
749 k->wk_cipher->ic_name, tid, (intmax_t) rsc,
750 (intmax_t) k->wk_keyrsc[tid],
751 k->wk_keyix, k->wk_rxkeyix);
753 if (ifp != NULL) { /* NB: for cipher test modules */
754 struct ieee80211_replay_event iev;
756 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
757 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
758 iev.iev_cipher = k->wk_cipher->ic_cipher;
759 if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE)
760 iev.iev_keyix = k->wk_rxkeyix;
762 iev.iev_keyix = k->wk_keyix;
763 iev.iev_keyrsc = k->wk_keyrsc[tid];
765 CURVNET_SET(ifp->if_vnet);
766 rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
772 ieee80211_notify_michael_failure(struct ieee80211vap *vap,
773 const struct ieee80211_frame *wh, u_int keyix)
775 struct ifnet *ifp = vap->iv_ifp;
777 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
778 "michael MIC verification failed <keyix %u>", keyix);
779 vap->iv_stats.is_rx_tkipmic++;
781 if (ifp != NULL) { /* NB: for cipher test modules */
782 struct ieee80211_michael_event iev;
784 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
785 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
786 iev.iev_cipher = IEEE80211_CIPHER_TKIP;
787 iev.iev_keyix = keyix;
788 CURVNET_SET(ifp->if_vnet);
789 rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev));
795 ieee80211_notify_wds_discover(struct ieee80211_node *ni)
797 struct ieee80211vap *vap = ni->ni_vap;
798 struct ifnet *ifp = vap->iv_ifp;
800 notify_macaddr(ifp, RTM_IEEE80211_WDS, ni->ni_macaddr);
804 ieee80211_notify_csa(struct ieee80211com *ic,
805 const struct ieee80211_channel *c, int mode, int count)
807 struct ieee80211_csa_event iev;
808 struct ieee80211vap *vap;
811 memset(&iev, 0, sizeof(iev));
812 iev.iev_flags = c->ic_flags;
813 iev.iev_freq = c->ic_freq;
814 iev.iev_ieee = c->ic_ieee;
816 iev.iev_count = count;
817 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
819 CURVNET_SET(ifp->if_vnet);
820 rt_ieee80211msg(ifp, RTM_IEEE80211_CSA, &iev, sizeof(iev));
826 ieee80211_notify_radar(struct ieee80211com *ic,
827 const struct ieee80211_channel *c)
829 struct ieee80211_radar_event iev;
830 struct ieee80211vap *vap;
833 memset(&iev, 0, sizeof(iev));
834 iev.iev_flags = c->ic_flags;
835 iev.iev_freq = c->ic_freq;
836 iev.iev_ieee = c->ic_ieee;
837 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
839 CURVNET_SET(ifp->if_vnet);
840 rt_ieee80211msg(ifp, RTM_IEEE80211_RADAR, &iev, sizeof(iev));
846 ieee80211_notify_cac(struct ieee80211com *ic,
847 const struct ieee80211_channel *c, enum ieee80211_notify_cac_event type)
849 struct ieee80211_cac_event iev;
850 struct ieee80211vap *vap;
853 memset(&iev, 0, sizeof(iev));
854 iev.iev_flags = c->ic_flags;
855 iev.iev_freq = c->ic_freq;
856 iev.iev_ieee = c->ic_ieee;
858 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
860 CURVNET_SET(ifp->if_vnet);
861 rt_ieee80211msg(ifp, RTM_IEEE80211_CAC, &iev, sizeof(iev));
867 ieee80211_notify_node_deauth(struct ieee80211_node *ni)
869 struct ieee80211vap *vap = ni->ni_vap;
870 struct ifnet *ifp = vap->iv_ifp;
872 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node deauth");
874 notify_macaddr(ifp, RTM_IEEE80211_DEAUTH, ni->ni_macaddr);
878 ieee80211_notify_node_auth(struct ieee80211_node *ni)
880 struct ieee80211vap *vap = ni->ni_vap;
881 struct ifnet *ifp = vap->iv_ifp;
883 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node auth");
885 notify_macaddr(ifp, RTM_IEEE80211_AUTH, ni->ni_macaddr);
889 ieee80211_notify_country(struct ieee80211vap *vap,
890 const uint8_t bssid[IEEE80211_ADDR_LEN], const uint8_t cc[2])
892 struct ifnet *ifp = vap->iv_ifp;
893 struct ieee80211_country_event iev;
895 memset(&iev, 0, sizeof(iev));
896 IEEE80211_ADDR_COPY(iev.iev_addr, bssid);
897 iev.iev_cc[0] = cc[0];
898 iev.iev_cc[1] = cc[1];
899 CURVNET_SET(ifp->if_vnet);
900 rt_ieee80211msg(ifp, RTM_IEEE80211_COUNTRY, &iev, sizeof(iev));
905 ieee80211_notify_radio(struct ieee80211com *ic, int state)
907 struct ieee80211_radio_event iev;
908 struct ieee80211vap *vap;
911 memset(&iev, 0, sizeof(iev));
912 iev.iev_state = state;
913 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
915 CURVNET_SET(ifp->if_vnet);
916 rt_ieee80211msg(ifp, RTM_IEEE80211_RADIO, &iev, sizeof(iev));
922 ieee80211_load_module(const char *modname)
926 (void)kern_kldload(curthread, modname, NULL);
928 printf("%s: load the %s module by hand for now.\n", __func__, modname);
932 static eventhandler_tag wlan_bpfevent;
933 static eventhandler_tag wlan_ifllevent;
936 bpf_track(void *arg, struct ifnet *ifp, int dlt, int attach)
938 /* NB: identify vap's by if_init */
939 if (dlt == DLT_IEEE802_11_RADIO &&
940 ifp->if_init == ieee80211_init) {
941 struct ieee80211vap *vap = ifp->if_softc;
943 * Track bpf radiotap listener state. We mark the vap
944 * to indicate if any listener is present and the com
945 * to indicate if any listener exists on any associated
946 * vap. This flag is used by drivers to prepare radiotap
947 * state only when needed.
950 ieee80211_syncflag_ext(vap, IEEE80211_FEXT_BPF);
951 if (vap->iv_opmode == IEEE80211_M_MONITOR)
952 atomic_add_int(&vap->iv_ic->ic_montaps, 1);
953 } else if (!bpf_peers_present(vap->iv_rawbpf)) {
954 ieee80211_syncflag_ext(vap, -IEEE80211_FEXT_BPF);
955 if (vap->iv_opmode == IEEE80211_M_MONITOR)
956 atomic_subtract_int(&vap->iv_ic->ic_montaps, 1);
962 * Change MAC address on the vap (if was not started).
965 wlan_iflladdr(void *arg __unused, struct ifnet *ifp)
967 /* NB: identify vap's by if_init */
968 if (ifp->if_init == ieee80211_init &&
969 (ifp->if_flags & IFF_UP) == 0) {
970 struct ieee80211vap *vap = ifp->if_softc;
972 IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp));
979 * NB: the module name is "wlan" for compatibility with NetBSD.
982 wlan_modevent(module_t mod, int type, void *unused)
987 printf("wlan: <802.11 Link Layer>\n");
988 wlan_bpfevent = EVENTHANDLER_REGISTER(bpf_track,
989 bpf_track, 0, EVENTHANDLER_PRI_ANY);
990 wlan_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
991 wlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
992 wlan_cloner = if_clone_simple(wlanname, wlan_clone_create,
993 wlan_clone_destroy, 0);
996 if_clone_detach(wlan_cloner);
997 EVENTHANDLER_DEREGISTER(bpf_track, wlan_bpfevent);
998 EVENTHANDLER_DEREGISTER(iflladdr_event, wlan_ifllevent);
1004 static moduledata_t wlan_mod = {
1009 DECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
1010 MODULE_VERSION(wlan, 1);
1011 MODULE_DEPEND(wlan, ether, 1, 1, 1);
1012 #ifdef IEEE80211_ALQ
1013 MODULE_DEPEND(wlan, alq, 1, 1, 1);
1014 #endif /* IEEE80211_ALQ */