2 * Copyright (c) 2003-2009 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
30 * IEEE 802.11 support (FreeBSD-specific code)
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 #include <sys/linker.h>
39 #include <sys/module.h>
41 #include <sys/sysctl.h>
43 #include <sys/socket.h>
47 #include <net/if_var.h>
48 #include <net/if_dl.h>
49 #include <net/if_clone.h>
50 #include <net/if_media.h>
51 #include <net/if_types.h>
52 #include <net/ethernet.h>
53 #include <net/route.h>
56 #include <net80211/ieee80211_var.h>
57 #include <net80211/ieee80211_input.h>
59 SYSCTL_NODE(_net, OID_AUTO, wlan, CTLFLAG_RD, 0, "IEEE 80211 parameters");
61 #ifdef IEEE80211_DEBUG
62 int ieee80211_debug = 0;
63 SYSCTL_INT(_net_wlan, OID_AUTO, debug, CTLFLAG_RW, &ieee80211_debug,
64 0, "debugging printfs");
67 static MALLOC_DEFINE(M_80211_COM, "80211com", "802.11 com state");
69 #if __FreeBSD_version >= 1000020
70 static const char wlanname[] = "wlan";
71 static struct if_clone *wlan_cloner;
75 * Allocate/free com structure in conjunction with ifnet;
76 * these routines are registered with if_register_com_alloc
77 * below and are called automatically by the ifnet code
78 * when the ifnet of the parent device is created.
81 wlan_alloc(u_char type, struct ifnet *ifp)
83 struct ieee80211com *ic;
85 ic = malloc(sizeof(struct ieee80211com), M_80211_COM, M_WAITOK|M_ZERO);
92 wlan_free(void *ic, u_char type)
94 free(ic, M_80211_COM);
98 wlan_clone_create(struct if_clone *ifc, int unit, caddr_t params)
100 struct ieee80211_clone_params cp;
101 struct ieee80211vap *vap;
102 struct ieee80211com *ic;
106 error = copyin(params, &cp, sizeof(cp));
109 ifp = ifunit(cp.icp_parent);
112 /* XXX move printfs to DIAGNOSTIC before release */
113 if (ifp->if_type != IFT_IEEE80211) {
114 if_printf(ifp, "%s: reject, not an 802.11 device\n", __func__);
117 if (cp.icp_opmode >= IEEE80211_OPMODE_MAX) {
118 if_printf(ifp, "%s: invalid opmode %d\n",
119 __func__, cp.icp_opmode);
123 if ((ic->ic_caps & ieee80211_opcap[cp.icp_opmode]) == 0) {
124 if_printf(ifp, "%s mode not supported\n",
125 ieee80211_opmode_name[cp.icp_opmode]);
128 if ((cp.icp_flags & IEEE80211_CLONE_TDMA) &&
129 #ifdef IEEE80211_SUPPORT_TDMA
130 (ic->ic_caps & IEEE80211_C_TDMA) == 0
135 if_printf(ifp, "TDMA not supported\n");
138 #if __FreeBSD_version >= 1000020
139 vap = ic->ic_vap_create(ic, wlanname, unit,
140 cp.icp_opmode, cp.icp_flags, cp.icp_bssid,
141 cp.icp_flags & IEEE80211_CLONE_MACADDR ?
142 cp.icp_macaddr : (const uint8_t *)IF_LLADDR(ifp));
144 vap = ic->ic_vap_create(ic, ifc->ifc_name, unit,
145 cp.icp_opmode, cp.icp_flags, cp.icp_bssid,
146 cp.icp_flags & IEEE80211_CLONE_MACADDR ?
147 cp.icp_macaddr : (const uint8_t *)IF_LLADDR(ifp));
151 return (vap == NULL ? EIO : 0);
155 wlan_clone_destroy(struct ifnet *ifp)
157 struct ieee80211vap *vap = ifp->if_softc;
158 struct ieee80211com *ic = vap->iv_ic;
160 ic->ic_vap_delete(vap);
163 #if __FreeBSD_version < 1000020
164 IFC_SIMPLE_DECLARE(wlan, 0);
168 ieee80211_vap_destroy(struct ieee80211vap *vap)
170 CURVNET_SET(vap->iv_ifp->if_vnet);
171 #if __FreeBSD_version >= 1000020
172 if_clone_destroyif(wlan_cloner, vap->iv_ifp);
174 if_clone_destroyif(&wlan_cloner, vap->iv_ifp);
180 ieee80211_sysctl_msecs_ticks(SYSCTL_HANDLER_ARGS)
182 int msecs = ticks_to_msecs(*(int *)arg1);
185 error = sysctl_handle_int(oidp, &msecs, 0, req);
186 if (error || !req->newptr)
188 t = msecs_to_ticks(msecs);
189 *(int *)arg1 = (t < 1) ? 1 : t;
194 ieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS)
196 int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT;
199 error = sysctl_handle_int(oidp, &inact, 0, req);
200 if (error || !req->newptr)
202 *(int *)arg1 = inact / IEEE80211_INACT_WAIT;
207 ieee80211_sysctl_parent(SYSCTL_HANDLER_ARGS)
209 struct ieee80211com *ic = arg1;
210 const char *name = ic->ic_ifp->if_xname;
212 return SYSCTL_OUT(req, name, strlen(name));
216 ieee80211_sysctl_radar(SYSCTL_HANDLER_ARGS)
218 struct ieee80211com *ic = arg1;
221 error = sysctl_handle_int(oidp, &t, 0, req);
222 if (error || !req->newptr)
225 ieee80211_dfs_notify_radar(ic, ic->ic_curchan);
226 IEEE80211_UNLOCK(ic);
231 ieee80211_sysctl_attach(struct ieee80211com *ic)
236 ieee80211_sysctl_detach(struct ieee80211com *ic)
241 ieee80211_sysctl_vattach(struct ieee80211vap *vap)
243 struct ifnet *ifp = vap->iv_ifp;
244 struct sysctl_ctx_list *ctx;
245 struct sysctl_oid *oid;
246 char num[14]; /* sufficient for 32 bits */
248 ctx = (struct sysctl_ctx_list *) malloc(sizeof(struct sysctl_ctx_list),
249 M_DEVBUF, M_NOWAIT | M_ZERO);
251 if_printf(ifp, "%s: cannot allocate sysctl context!\n",
255 sysctl_ctx_init(ctx);
256 snprintf(num, sizeof(num), "%u", ifp->if_dunit);
257 oid = SYSCTL_ADD_NODE(ctx, &SYSCTL_NODE_CHILDREN(_net, wlan),
258 OID_AUTO, num, CTLFLAG_RD, NULL, "");
259 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
260 "%parent", CTLTYPE_STRING | CTLFLAG_RD, vap->iv_ic, 0,
261 ieee80211_sysctl_parent, "A", "parent device");
262 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
263 "driver_caps", CTLFLAG_RW, &vap->iv_caps, 0,
264 "driver capabilities");
265 #ifdef IEEE80211_DEBUG
266 vap->iv_debug = ieee80211_debug;
267 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
268 "debug", CTLFLAG_RW, &vap->iv_debug, 0,
269 "control debugging printfs");
271 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
272 "bmiss_max", CTLFLAG_RW, &vap->iv_bmiss_max, 0,
273 "consecutive beacon misses before scanning");
274 /* XXX inherit from tunables */
275 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
276 "inact_run", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_run, 0,
277 ieee80211_sysctl_inact, "I",
278 "station inactivity timeout (sec)");
279 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
280 "inact_probe", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_probe, 0,
281 ieee80211_sysctl_inact, "I",
282 "station inactivity probe timeout (sec)");
283 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
284 "inact_auth", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_auth, 0,
285 ieee80211_sysctl_inact, "I",
286 "station authentication timeout (sec)");
287 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
288 "inact_init", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_init, 0,
289 ieee80211_sysctl_inact, "I",
290 "station initial state timeout (sec)");
291 if (vap->iv_htcaps & IEEE80211_HTC_HT) {
292 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
293 "ampdu_mintraffic_bk", CTLFLAG_RW,
294 &vap->iv_ampdu_mintraffic[WME_AC_BK], 0,
295 "BK traffic tx aggr threshold (pps)");
296 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
297 "ampdu_mintraffic_be", CTLFLAG_RW,
298 &vap->iv_ampdu_mintraffic[WME_AC_BE], 0,
299 "BE traffic tx aggr threshold (pps)");
300 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
301 "ampdu_mintraffic_vo", CTLFLAG_RW,
302 &vap->iv_ampdu_mintraffic[WME_AC_VO], 0,
303 "VO traffic tx aggr threshold (pps)");
304 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
305 "ampdu_mintraffic_vi", CTLFLAG_RW,
306 &vap->iv_ampdu_mintraffic[WME_AC_VI], 0,
307 "VI traffic tx aggr threshold (pps)");
309 if (vap->iv_caps & IEEE80211_C_DFS) {
310 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
311 "radar", CTLTYPE_INT | CTLFLAG_RW, vap->iv_ic, 0,
312 ieee80211_sysctl_radar, "I", "simulate radar event");
314 vap->iv_sysctl = ctx;
319 ieee80211_sysctl_vdetach(struct ieee80211vap *vap)
322 if (vap->iv_sysctl != NULL) {
323 sysctl_ctx_free(vap->iv_sysctl);
324 free(vap->iv_sysctl, M_DEVBUF);
325 vap->iv_sysctl = NULL;
330 ieee80211_node_dectestref(struct ieee80211_node *ni)
332 /* XXX need equivalent of atomic_dec_and_test */
333 atomic_subtract_int(&ni->ni_refcnt, 1);
334 return atomic_cmpset_int(&ni->ni_refcnt, 0, 1);
338 ieee80211_drain_ifq(struct ifqueue *ifq)
340 struct ieee80211_node *ni;
348 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
349 KASSERT(ni != NULL, ("frame w/o node"));
350 ieee80211_free_node(ni);
351 m->m_pkthdr.rcvif = NULL;
358 ieee80211_flush_ifq(struct ifqueue *ifq, struct ieee80211vap *vap)
360 struct ieee80211_node *ni;
361 struct mbuf *m, **mprev;
364 mprev = &ifq->ifq_head;
365 while ((m = *mprev) != NULL) {
366 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
367 if (ni != NULL && ni->ni_vap == vap) {
368 *mprev = m->m_nextpkt; /* remove from list */
372 ieee80211_free_node(ni); /* reclaim ref */
374 mprev = &m->m_nextpkt;
376 /* recalculate tail ptr */
378 for (; m != NULL && m->m_nextpkt != NULL; m = m->m_nextpkt)
385 * As above, for mbufs allocated with m_gethdr/MGETHDR
386 * or initialized by M_COPY_PKTHDR.
388 #define MC_ALIGN(m, len) \
390 (m)->m_data += (MCLBYTES - (len)) &~ (sizeof(long) - 1); \
391 } while (/* CONSTCOND */ 0)
394 * Allocate and setup a management frame of the specified
395 * size. We return the mbuf and a pointer to the start
396 * of the contiguous data area that's been reserved based
397 * on the packet length. The data area is forced to 32-bit
398 * alignment and the buffer length to a multiple of 4 bytes.
399 * This is done mainly so beacon frames (that require this)
400 * can use this interface too.
403 ieee80211_getmgtframe(uint8_t **frm, int headroom, int pktlen)
409 * NB: we know the mbuf routines will align the data area
410 * so we don't need to do anything special.
412 len = roundup2(headroom + pktlen, 4);
413 KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len));
414 if (len < MINCLSIZE) {
415 m = m_gethdr(M_NOWAIT, MT_DATA);
417 * Align the data in case additional headers are added.
418 * This should only happen when a WEP header is added
419 * which only happens for shared key authentication mgt
420 * frames which all fit in MHLEN.
425 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
430 m->m_data += headroom;
436 #ifndef __NO_STRICT_ALIGNMENT
438 * Re-align the payload in the mbuf. This is mainly used (right now)
439 * to handle IP header alignment requirements on certain architectures.
442 ieee80211_realign(struct ieee80211vap *vap, struct mbuf *m, size_t align)
447 pktlen = m->m_pkthdr.len;
448 space = pktlen + align;
449 if (space < MINCLSIZE)
450 n = m_gethdr(M_NOWAIT, MT_DATA);
452 n = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR,
453 space <= MCLBYTES ? MCLBYTES :
454 #if MJUMPAGESIZE != MCLBYTES
455 space <= MJUMPAGESIZE ? MJUMPAGESIZE :
457 space <= MJUM9BYTES ? MJUM9BYTES : MJUM16BYTES);
459 if (__predict_true(n != NULL)) {
461 n->m_data = (caddr_t)(ALIGN(n->m_data + align) - align);
462 m_copydata(m, 0, pktlen, mtod(n, caddr_t));
465 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
466 mtod(m, const struct ieee80211_frame *), NULL,
467 "%s", "no mbuf to realign");
468 vap->iv_stats.is_rx_badalign++;
473 #endif /* !__NO_STRICT_ALIGNMENT */
476 ieee80211_add_callback(struct mbuf *m,
477 void (*func)(struct ieee80211_node *, void *, int), void *arg)
480 struct ieee80211_cb *cb;
482 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_CALLBACK,
483 sizeof(struct ieee80211_cb), M_NOWAIT);
487 cb = (struct ieee80211_cb *)(mtag+1);
490 m_tag_prepend(m, mtag);
491 m->m_flags |= M_TXCB;
496 ieee80211_process_callback(struct ieee80211_node *ni,
497 struct mbuf *m, int status)
501 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_CALLBACK, NULL);
503 struct ieee80211_cb *cb = (struct ieee80211_cb *)(mtag+1);
504 cb->func(ni, cb->arg, status);
509 * Transmit a frame to the parent interface.
511 * TODO: if the transmission fails, make sure the parent node is freed
512 * (the callers will first need modifying.)
515 ieee80211_parent_xmitpkt(struct ieee80211com *ic,
518 struct ifnet *parent = ic->ic_ifp;
520 * Assert the IC TX lock is held - this enforces the
521 * processing -> queuing order is maintained
523 IEEE80211_TX_LOCK_ASSERT(ic);
525 return (parent->if_transmit(parent, m));
529 * Transmit a frame to the VAP interface.
532 ieee80211_vap_xmitpkt(struct ieee80211vap *vap, struct mbuf *m)
534 struct ifnet *ifp = vap->iv_ifp;
537 * When transmitting via the VAP, we shouldn't hold
538 * any IC TX lock as the VAP TX path will acquire it.
540 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
542 return (ifp->if_transmit(ifp, m));
546 #include <sys/libkern.h>
549 get_random_bytes(void *p, size_t n)
554 uint32_t v = arc4random();
555 size_t nb = n > sizeof(uint32_t) ? sizeof(uint32_t) : n;
556 bcopy(&v, dp, n > sizeof(uint32_t) ? sizeof(uint32_t) : n);
557 dp += sizeof(uint32_t), n -= nb;
562 * Helper function for events that pass just a single mac address.
565 notify_macaddr(struct ifnet *ifp, int op, const uint8_t mac[IEEE80211_ADDR_LEN])
567 struct ieee80211_join_event iev;
569 CURVNET_SET(ifp->if_vnet);
570 memset(&iev, 0, sizeof(iev));
571 IEEE80211_ADDR_COPY(iev.iev_addr, mac);
572 rt_ieee80211msg(ifp, op, &iev, sizeof(iev));
577 ieee80211_notify_node_join(struct ieee80211_node *ni, int newassoc)
579 struct ieee80211vap *vap = ni->ni_vap;
580 struct ifnet *ifp = vap->iv_ifp;
582 CURVNET_SET_QUIET(ifp->if_vnet);
583 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode join",
584 (ni == vap->iv_bss) ? "bss " : "");
586 if (ni == vap->iv_bss) {
587 notify_macaddr(ifp, newassoc ?
588 RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC, ni->ni_bssid);
589 if_link_state_change(ifp, LINK_STATE_UP);
591 notify_macaddr(ifp, newassoc ?
592 RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN, ni->ni_macaddr);
598 ieee80211_notify_node_leave(struct ieee80211_node *ni)
600 struct ieee80211vap *vap = ni->ni_vap;
601 struct ifnet *ifp = vap->iv_ifp;
603 CURVNET_SET_QUIET(ifp->if_vnet);
604 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode leave",
605 (ni == vap->iv_bss) ? "bss " : "");
607 if (ni == vap->iv_bss) {
608 rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
609 if_link_state_change(ifp, LINK_STATE_DOWN);
611 /* fire off wireless event station leaving */
612 notify_macaddr(ifp, RTM_IEEE80211_LEAVE, ni->ni_macaddr);
618 ieee80211_notify_scan_done(struct ieee80211vap *vap)
620 struct ifnet *ifp = vap->iv_ifp;
622 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n", "notify scan done");
624 /* dispatch wireless event indicating scan completed */
625 CURVNET_SET(ifp->if_vnet);
626 rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
631 ieee80211_notify_replay_failure(struct ieee80211vap *vap,
632 const struct ieee80211_frame *wh, const struct ieee80211_key *k,
633 u_int64_t rsc, int tid)
635 struct ifnet *ifp = vap->iv_ifp;
637 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
638 "%s replay detected tid %d <rsc %ju, csc %ju, keyix %u rxkeyix %u>",
639 k->wk_cipher->ic_name, tid, (intmax_t) rsc,
640 (intmax_t) k->wk_keyrsc[tid],
641 k->wk_keyix, k->wk_rxkeyix);
643 if (ifp != NULL) { /* NB: for cipher test modules */
644 struct ieee80211_replay_event iev;
646 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
647 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
648 iev.iev_cipher = k->wk_cipher->ic_cipher;
649 if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE)
650 iev.iev_keyix = k->wk_rxkeyix;
652 iev.iev_keyix = k->wk_keyix;
653 iev.iev_keyrsc = k->wk_keyrsc[tid];
655 CURVNET_SET(ifp->if_vnet);
656 rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
662 ieee80211_notify_michael_failure(struct ieee80211vap *vap,
663 const struct ieee80211_frame *wh, u_int keyix)
665 struct ifnet *ifp = vap->iv_ifp;
667 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
668 "michael MIC verification failed <keyix %u>", keyix);
669 vap->iv_stats.is_rx_tkipmic++;
671 if (ifp != NULL) { /* NB: for cipher test modules */
672 struct ieee80211_michael_event iev;
674 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
675 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
676 iev.iev_cipher = IEEE80211_CIPHER_TKIP;
677 iev.iev_keyix = keyix;
678 CURVNET_SET(ifp->if_vnet);
679 rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev));
685 ieee80211_notify_wds_discover(struct ieee80211_node *ni)
687 struct ieee80211vap *vap = ni->ni_vap;
688 struct ifnet *ifp = vap->iv_ifp;
690 notify_macaddr(ifp, RTM_IEEE80211_WDS, ni->ni_macaddr);
694 ieee80211_notify_csa(struct ieee80211com *ic,
695 const struct ieee80211_channel *c, int mode, int count)
697 struct ifnet *ifp = ic->ic_ifp;
698 struct ieee80211_csa_event iev;
700 memset(&iev, 0, sizeof(iev));
701 iev.iev_flags = c->ic_flags;
702 iev.iev_freq = c->ic_freq;
703 iev.iev_ieee = c->ic_ieee;
705 iev.iev_count = count;
706 CURVNET_SET(ifp->if_vnet);
707 rt_ieee80211msg(ifp, RTM_IEEE80211_CSA, &iev, sizeof(iev));
712 ieee80211_notify_radar(struct ieee80211com *ic,
713 const struct ieee80211_channel *c)
715 struct ifnet *ifp = ic->ic_ifp;
716 struct ieee80211_radar_event iev;
718 memset(&iev, 0, sizeof(iev));
719 iev.iev_flags = c->ic_flags;
720 iev.iev_freq = c->ic_freq;
721 iev.iev_ieee = c->ic_ieee;
722 CURVNET_SET(ifp->if_vnet);
723 rt_ieee80211msg(ifp, RTM_IEEE80211_RADAR, &iev, sizeof(iev));
728 ieee80211_notify_cac(struct ieee80211com *ic,
729 const struct ieee80211_channel *c, enum ieee80211_notify_cac_event type)
731 struct ifnet *ifp = ic->ic_ifp;
732 struct ieee80211_cac_event iev;
734 memset(&iev, 0, sizeof(iev));
735 iev.iev_flags = c->ic_flags;
736 iev.iev_freq = c->ic_freq;
737 iev.iev_ieee = c->ic_ieee;
739 CURVNET_SET(ifp->if_vnet);
740 rt_ieee80211msg(ifp, RTM_IEEE80211_CAC, &iev, sizeof(iev));
745 ieee80211_notify_node_deauth(struct ieee80211_node *ni)
747 struct ieee80211vap *vap = ni->ni_vap;
748 struct ifnet *ifp = vap->iv_ifp;
750 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node deauth");
752 notify_macaddr(ifp, RTM_IEEE80211_DEAUTH, ni->ni_macaddr);
756 ieee80211_notify_node_auth(struct ieee80211_node *ni)
758 struct ieee80211vap *vap = ni->ni_vap;
759 struct ifnet *ifp = vap->iv_ifp;
761 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node auth");
763 notify_macaddr(ifp, RTM_IEEE80211_AUTH, ni->ni_macaddr);
767 ieee80211_notify_country(struct ieee80211vap *vap,
768 const uint8_t bssid[IEEE80211_ADDR_LEN], const uint8_t cc[2])
770 struct ifnet *ifp = vap->iv_ifp;
771 struct ieee80211_country_event iev;
773 memset(&iev, 0, sizeof(iev));
774 IEEE80211_ADDR_COPY(iev.iev_addr, bssid);
775 iev.iev_cc[0] = cc[0];
776 iev.iev_cc[1] = cc[1];
777 CURVNET_SET(ifp->if_vnet);
778 rt_ieee80211msg(ifp, RTM_IEEE80211_COUNTRY, &iev, sizeof(iev));
783 ieee80211_notify_radio(struct ieee80211com *ic, int state)
785 struct ifnet *ifp = ic->ic_ifp;
786 struct ieee80211_radio_event iev;
788 memset(&iev, 0, sizeof(iev));
789 iev.iev_state = state;
790 CURVNET_SET(ifp->if_vnet);
791 rt_ieee80211msg(ifp, RTM_IEEE80211_RADIO, &iev, sizeof(iev));
796 ieee80211_load_module(const char *modname)
800 (void)kern_kldload(curthread, modname, NULL);
802 printf("%s: load the %s module by hand for now.\n", __func__, modname);
806 static eventhandler_tag wlan_bpfevent;
807 static eventhandler_tag wlan_ifllevent;
810 bpf_track(void *arg, struct ifnet *ifp, int dlt, int attach)
812 /* NB: identify vap's by if_init */
813 if (dlt == DLT_IEEE802_11_RADIO &&
814 ifp->if_init == ieee80211_init) {
815 struct ieee80211vap *vap = ifp->if_softc;
817 * Track bpf radiotap listener state. We mark the vap
818 * to indicate if any listener is present and the com
819 * to indicate if any listener exists on any associated
820 * vap. This flag is used by drivers to prepare radiotap
821 * state only when needed.
824 ieee80211_syncflag_ext(vap, IEEE80211_FEXT_BPF);
825 if (vap->iv_opmode == IEEE80211_M_MONITOR)
826 atomic_add_int(&vap->iv_ic->ic_montaps, 1);
827 } else if (!bpf_peers_present(vap->iv_rawbpf)) {
828 ieee80211_syncflag_ext(vap, -IEEE80211_FEXT_BPF);
829 if (vap->iv_opmode == IEEE80211_M_MONITOR)
830 atomic_subtract_int(&vap->iv_ic->ic_montaps, 1);
836 wlan_iflladdr(void *arg __unused, struct ifnet *ifp)
838 struct ieee80211com *ic = ifp->if_l2com;
839 struct ieee80211vap *vap, *next;
841 if (ifp->if_type != IFT_IEEE80211 || ic == NULL)
845 TAILQ_FOREACH_SAFE(vap, &ic->ic_vaps, iv_next, next) {
847 * If the MAC address has changed on the parent and it was
848 * copied to the vap on creation then re-sync.
850 if (vap->iv_ic == ic &&
851 (vap->iv_flags_ext & IEEE80211_FEXT_UNIQMAC) == 0) {
852 IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp));
853 IEEE80211_UNLOCK(ic);
854 if_setlladdr(vap->iv_ifp, IF_LLADDR(ifp),
859 IEEE80211_UNLOCK(ic);
865 * NB: the module name is "wlan" for compatibility with NetBSD.
868 wlan_modevent(module_t mod, int type, void *unused)
873 printf("wlan: <802.11 Link Layer>\n");
874 wlan_bpfevent = EVENTHANDLER_REGISTER(bpf_track,
875 bpf_track, 0, EVENTHANDLER_PRI_ANY);
876 if (wlan_bpfevent == NULL)
878 wlan_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
879 wlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
880 if (wlan_ifllevent == NULL) {
881 EVENTHANDLER_DEREGISTER(bpf_track, wlan_bpfevent);
884 #if __FreeBSD_version >= 1000020
885 wlan_cloner = if_clone_simple(wlanname, wlan_clone_create,
886 wlan_clone_destroy, 0);
888 if_clone_attach(&wlan_cloner);
890 if_register_com_alloc(IFT_IEEE80211, wlan_alloc, wlan_free);
893 if_deregister_com_alloc(IFT_IEEE80211);
894 #if __FreeBSD_version >= 1000020
895 if_clone_detach(wlan_cloner);
897 if_clone_detach(&wlan_cloner);
899 EVENTHANDLER_DEREGISTER(bpf_track, wlan_bpfevent);
900 EVENTHANDLER_DEREGISTER(iflladdr_event, wlan_ifllevent);
906 static moduledata_t wlan_mod = {
907 #if __FreeBSD_version >= 1000020
915 DECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
916 MODULE_VERSION(wlan, 1);
917 MODULE_DEPEND(wlan, ether, 1, 1, 1);
919 MODULE_DEPEND(wlan, alq, 1, 1, 1);
920 #endif /* IEEE80211_ALQ */