ieee80211_freebsd.c revision 217322
155682Smarkm/*- 2233294Sstas * Copyright (c) 2003-2009 Sam Leffler, Errno Consulting 3233294Sstas * All rights reserved. 4233294Sstas * 555682Smarkm * Redistribution and use in source and binary forms, with or without 6233294Sstas * modification, are permitted provided that the following conditions 7233294Sstas * are met: 8233294Sstas * 1. Redistributions of source code must retain the above copyright 955682Smarkm * notice, this list of conditions and the following disclaimer. 10233294Sstas * 2. Redistributions in binary form must reproduce the above copyright 11233294Sstas * notice, this list of conditions and the following disclaimer in the 1255682Smarkm * documentation and/or other materials provided with the distribution. 13233294Sstas * 14233294Sstas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15233294Sstas * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1655682Smarkm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17233294Sstas * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18233294Sstas * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19233294Sstas * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2055682Smarkm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21233294Sstas * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22233294Sstas * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23233294Sstas * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24233294Sstas */ 25233294Sstas 26233294Sstas#include <sys/cdefs.h> 27233294Sstas__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_freebsd.c 217322 2011-01-12 19:53:50Z mdf $"); 28233294Sstas 29233294Sstas/* 30233294Sstas * IEEE 802.11 support (FreeBSD-specific code) 31233294Sstas */ 3255682Smarkm#include "opt_wlan.h" 3355682Smarkm 3455682Smarkm#include <sys/param.h> 3555682Smarkm#include <sys/kernel.h> 36233294Sstas#include <sys/systm.h> 3755682Smarkm#include <sys/linker.h> 3890926Snectar#include <sys/mbuf.h> 3990926Snectar#include <sys/module.h> 40233294Sstas#include <sys/proc.h> 41233294Sstas#include <sys/sysctl.h> 4290926Snectar 4390926Snectar#include <sys/socket.h> 4490926Snectar 4590926Snectar#include <net/bpf.h> 4690926Snectar#include <net/if.h> 4790926Snectar#include <net/if_dl.h> 4890926Snectar#include <net/if_clone.h> 4990926Snectar#include <net/if_media.h> 50178825Sdfr#include <net/if_types.h> 51178825Sdfr#include <net/ethernet.h> 52233294Sstas#include <net/route.h> 53178825Sdfr#include <net/vnet.h> 54178825Sdfr 5590926Snectar#include <net80211/ieee80211_var.h> 5690926Snectar#include <net80211/ieee80211_input.h> 57233294Sstas 5890926SnectarSYSCTL_NODE(_net, OID_AUTO, wlan, CTLFLAG_RD, 0, "IEEE 80211 parameters"); 5990926Snectar 6090926Snectar#ifdef IEEE80211_DEBUG 6190926Snectarint ieee80211_debug = 0; 6290926SnectarSYSCTL_INT(_net_wlan, OID_AUTO, debug, CTLFLAG_RW, &ieee80211_debug, 6390926Snectar 0, "debugging printfs"); 6490926Snectar#endif 6590926Snectar 6690926SnectarMALLOC_DEFINE(M_80211_COM, "80211com", "802.11 com state"); 6790926Snectar 6890926Snectar/* 6990926Snectar * Allocate/free com structure in conjunction with ifnet; 7090926Snectar * these routines are registered with if_register_com_alloc 71233294Sstas * below and are called automatically by the ifnet code 7290926Snectar * when the ifnet of the parent device is created. 7390926Snectar */ 7490926Snectarstatic void * 75233294Sstaswlan_alloc(u_char type, struct ifnet *ifp) 7690926Snectar{ 77178825Sdfr struct ieee80211com *ic; 7890926Snectar 7990926Snectar ic = malloc(sizeof(struct ieee80211com), M_80211_COM, M_WAITOK|M_ZERO); 8090926Snectar ic->ic_ifp = ifp; 8190926Snectar 8290926Snectar return (ic); 8390926Snectar} 8490926Snectar 8555682Smarkmstatic void 86178825Sdfrwlan_free(void *ic, u_char type) 8755682Smarkm{ 8878527Sassar free(ic, M_80211_COM); 8978527Sassar} 9078527Sassar 9178527Sassarstatic int 9278527Sassarwlan_clone_create(struct if_clone *ifc, int unit, caddr_t params) 93233294Sstas{ 94233294Sstas struct ieee80211_clone_params cp; 95178825Sdfr struct ieee80211vap *vap; 96233294Sstas struct ieee80211com *ic; 9790926Snectar struct ifnet *ifp; 9878527Sassar int error; 9978527Sassar 100178825Sdfr error = copyin(params, &cp, sizeof(cp)); 101178825Sdfr if (error) 10290926Snectar return error; 103178825Sdfr ifp = ifunit(cp.icp_parent); 10478527Sassar if (ifp == NULL) 105178825Sdfr return ENXIO; 10678527Sassar /* XXX move printfs to DIAGNOSTIC before release */ 10778527Sassar if (ifp->if_type != IFT_IEEE80211) { 10878527Sassar if_printf(ifp, "%s: reject, not an 802.11 device\n", __func__); 10978527Sassar return ENXIO; 110178825Sdfr } 11178527Sassar if (cp.icp_opmode >= IEEE80211_OPMODE_MAX) { 112233294Sstas if_printf(ifp, "%s: invalid opmode %d\n", 113233294Sstas __func__, cp.icp_opmode); 11478527Sassar return EINVAL; 11578527Sassar } 11678527Sassar ic = ifp->if_l2com; 117178825Sdfr if ((ic->ic_caps & ieee80211_opcap[cp.icp_opmode]) == 0) { 11878527Sassar if_printf(ifp, "%s mode not supported\n", 11978527Sassar ieee80211_opmode_name[cp.icp_opmode]); 12078527Sassar return EOPNOTSUPP; 12178527Sassar } 12278527Sassar if ((cp.icp_flags & IEEE80211_CLONE_TDMA) && 123233294Sstas#ifdef IEEE80211_SUPPORT_TDMA 124233294Sstas (ic->ic_caps & IEEE80211_C_TDMA) == 0 12555682Smarkm#else 12655682Smarkm (1) 12755682Smarkm#endif 12855682Smarkm ) { 12955682Smarkm if_printf(ifp, "TDMA not supported\n"); 13055682Smarkm return EOPNOTSUPP; 13155682Smarkm } 13255682Smarkm vap = ic->ic_vap_create(ic, ifc->ifc_name, unit, 133233294Sstas cp.icp_opmode, cp.icp_flags, cp.icp_bssid, 134142403Snectar cp.icp_flags & IEEE80211_CLONE_MACADDR ? 135233294Sstas cp.icp_macaddr : (const uint8_t *)IF_LLADDR(ifp)); 136178825Sdfr return (vap == NULL ? EIO : 0); 137142403Snectar} 138142403Snectar 13955682Smarkmstatic void 14055682Smarkmwlan_clone_destroy(struct ifnet *ifp) 14155682Smarkm{ 14255682Smarkm struct ieee80211vap *vap = ifp->if_softc; 14355682Smarkm struct ieee80211com *ic = vap->iv_ic; 14455682Smarkm 14555682Smarkm ic->ic_vap_delete(vap); 14690926Snectar} 14790926SnectarIFC_SIMPLE_DECLARE(wlan, 0); 14890926Snectar 149178825Sdfrvoid 150178825Sdfrieee80211_vap_destroy(struct ieee80211vap *vap) 15190926Snectar{ 15290926Snectar if_clone_destroyif(&wlan_cloner, vap->iv_ifp); 153233294Sstas} 154233294Sstas 155233294Sstasint 156178825Sdfrieee80211_sysctl_msecs_ticks(SYSCTL_HANDLER_ARGS) 157178825Sdfr{ 15890926Snectar int msecs = ticks_to_msecs(*(int *)arg1); 15990926Snectar int error, t; 160233294Sstas 16155682Smarkm error = sysctl_handle_int(oidp, &msecs, 0, req); 16255682Smarkm if (error || !req->newptr) 163178825Sdfr return error; 16455682Smarkm t = msecs_to_ticks(msecs); 165233294Sstas *(int *)arg1 = (t < 1) ? 1 : t; 16655682Smarkm return 0; 167178825Sdfr} 16855682Smarkm 16955682Smarkmstatic int 17055682Smarkmieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS) 17172445Sassar{ 172233294Sstas int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT; 17372445Sassar int error; 174178825Sdfr 17572445Sassar error = sysctl_handle_int(oidp, &inact, 0, req); 17672445Sassar if (error || !req->newptr) 177233294Sstas return error; 178233294Sstas *(int *)arg1 = inact / IEEE80211_INACT_WAIT; 17955682Smarkm return 0; 18072445Sassar} 181233294Sstas 18272445Sassarstatic int 18372445Sassarieee80211_sysctl_parent(SYSCTL_HANDLER_ARGS) 18472445Sassar{ 185178825Sdfr struct ieee80211com *ic = arg1; 18672445Sassar const char *name = ic->ic_ifp->if_xname; 18772445Sassar 188178825Sdfr return SYSCTL_OUT(req, name, strlen(name)); 189233294Sstas} 19055682Smarkm 19155682Smarkmstatic int 19255682Smarkmieee80211_sysctl_radar(SYSCTL_HANDLER_ARGS) 19355682Smarkm{ 19455682Smarkm struct ieee80211com *ic = arg1; 19555682Smarkm int t = 0, error; 19655682Smarkm 19772445Sassar error = sysctl_handle_int(oidp, &t, 0, req); 198233294Sstas if (error || !req->newptr) 19972445Sassar return error; 20072445Sassar IEEE80211_LOCK(ic); 20172445Sassar ieee80211_dfs_notify_radar(ic, ic->ic_curchan); 202178825Sdfr IEEE80211_UNLOCK(ic); 20372445Sassar return 0; 20472445Sassar} 20555682Smarkm 20678527Sassarvoid 20778527Sassarieee80211_sysctl_attach(struct ieee80211com *ic) 20878527Sassar{ 209233294Sstas} 21078527Sassar 21178527Sassarvoid 212178825Sdfrieee80211_sysctl_detach(struct ieee80211com *ic) 213178825Sdfr{ 21478527Sassar} 21578527Sassar 21678527Sassarvoid 21778527Sassarieee80211_sysctl_vattach(struct ieee80211vap *vap) 21878527Sassar{ 21978527Sassar struct ifnet *ifp = vap->iv_ifp; 22078527Sassar struct sysctl_ctx_list *ctx; 22178527Sassar struct sysctl_oid *oid; 22278527Sassar char num[14]; /* sufficient for 32 bits */ 22378527Sassar 22478527Sassar ctx = (struct sysctl_ctx_list *) malloc(sizeof(struct sysctl_ctx_list), 22578527Sassar M_DEVBUF, M_NOWAIT | M_ZERO); 22678527Sassar if (ctx == NULL) { 22755682Smarkm if_printf(ifp, "%s: cannot allocate sysctl context!\n", 22855682Smarkm __func__); 229233294Sstas return; 23055682Smarkm } 23155682Smarkm sysctl_ctx_init(ctx); 23255682Smarkm snprintf(num, sizeof(num), "%u", ifp->if_dunit); 23378527Sassar oid = SYSCTL_ADD_NODE(ctx, &SYSCTL_NODE_CHILDREN(_net, wlan), 23478527Sassar OID_AUTO, num, CTLFLAG_RD, NULL, ""); 23578527Sassar SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 23678527Sassar "%parent", CTLFLAG_RD, vap->iv_ic, 0, 23778527Sassar ieee80211_sysctl_parent, "A", "parent device"); 238178825Sdfr SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 23955682Smarkm "driver_caps", CTLFLAG_RW, &vap->iv_caps, 0, 240 "driver capabilities"); 241#ifdef IEEE80211_DEBUG 242 vap->iv_debug = ieee80211_debug; 243 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 244 "debug", CTLFLAG_RW, &vap->iv_debug, 0, 245 "control debugging printfs"); 246#endif 247 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 248 "bmiss_max", CTLFLAG_RW, &vap->iv_bmiss_max, 0, 249 "consecutive beacon misses before scanning"); 250 /* XXX inherit from tunables */ 251 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 252 "inact_run", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_run, 0, 253 ieee80211_sysctl_inact, "I", 254 "station inactivity timeout (sec)"); 255 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 256 "inact_probe", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_probe, 0, 257 ieee80211_sysctl_inact, "I", 258 "station inactivity probe timeout (sec)"); 259 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 260 "inact_auth", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_auth, 0, 261 ieee80211_sysctl_inact, "I", 262 "station authentication timeout (sec)"); 263 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 264 "inact_init", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_init, 0, 265 ieee80211_sysctl_inact, "I", 266 "station initial state timeout (sec)"); 267 if (vap->iv_htcaps & IEEE80211_HTC_HT) { 268 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 269 "ampdu_mintraffic_bk", CTLFLAG_RW, 270 &vap->iv_ampdu_mintraffic[WME_AC_BK], 0, 271 "BK traffic tx aggr threshold (pps)"); 272 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 273 "ampdu_mintraffic_be", CTLFLAG_RW, 274 &vap->iv_ampdu_mintraffic[WME_AC_BE], 0, 275 "BE traffic tx aggr threshold (pps)"); 276 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 277 "ampdu_mintraffic_vo", CTLFLAG_RW, 278 &vap->iv_ampdu_mintraffic[WME_AC_VO], 0, 279 "VO traffic tx aggr threshold (pps)"); 280 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 281 "ampdu_mintraffic_vi", CTLFLAG_RW, 282 &vap->iv_ampdu_mintraffic[WME_AC_VI], 0, 283 "VI traffic tx aggr threshold (pps)"); 284 } 285 if (vap->iv_caps & IEEE80211_C_DFS) { 286 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 287 "radar", CTLTYPE_INT | CTLFLAG_RW, vap->iv_ic, 0, 288 ieee80211_sysctl_radar, "I", "simulate radar event"); 289 } 290 vap->iv_sysctl = ctx; 291 vap->iv_oid = oid; 292} 293 294void 295ieee80211_sysctl_vdetach(struct ieee80211vap *vap) 296{ 297 298 if (vap->iv_sysctl != NULL) { 299 sysctl_ctx_free(vap->iv_sysctl); 300 free(vap->iv_sysctl, M_DEVBUF); 301 vap->iv_sysctl = NULL; 302 } 303} 304 305int 306ieee80211_node_dectestref(struct ieee80211_node *ni) 307{ 308 /* XXX need equivalent of atomic_dec_and_test */ 309 atomic_subtract_int(&ni->ni_refcnt, 1); 310 return atomic_cmpset_int(&ni->ni_refcnt, 0, 1); 311} 312 313void 314ieee80211_drain_ifq(struct ifqueue *ifq) 315{ 316 struct ieee80211_node *ni; 317 struct mbuf *m; 318 319 for (;;) { 320 IF_DEQUEUE(ifq, m); 321 if (m == NULL) 322 break; 323 324 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 325 KASSERT(ni != NULL, ("frame w/o node")); 326 ieee80211_free_node(ni); 327 m->m_pkthdr.rcvif = NULL; 328 329 m_freem(m); 330 } 331} 332 333void 334ieee80211_flush_ifq(struct ifqueue *ifq, struct ieee80211vap *vap) 335{ 336 struct ieee80211_node *ni; 337 struct mbuf *m, **mprev; 338 339 IF_LOCK(ifq); 340 mprev = &ifq->ifq_head; 341 while ((m = *mprev) != NULL) { 342 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 343 if (ni != NULL && ni->ni_vap == vap) { 344 *mprev = m->m_nextpkt; /* remove from list */ 345 ifq->ifq_len--; 346 347 m_freem(m); 348 ieee80211_free_node(ni); /* reclaim ref */ 349 } else 350 mprev = &m->m_nextpkt; 351 } 352 /* recalculate tail ptr */ 353 m = ifq->ifq_head; 354 for (; m != NULL && m->m_nextpkt != NULL; m = m->m_nextpkt) 355 ; 356 ifq->ifq_tail = m; 357 IF_UNLOCK(ifq); 358} 359 360/* 361 * As above, for mbufs allocated with m_gethdr/MGETHDR 362 * or initialized by M_COPY_PKTHDR. 363 */ 364#define MC_ALIGN(m, len) \ 365do { \ 366 (m)->m_data += (MCLBYTES - (len)) &~ (sizeof(long) - 1); \ 367} while (/* CONSTCOND */ 0) 368 369/* 370 * Allocate and setup a management frame of the specified 371 * size. We return the mbuf and a pointer to the start 372 * of the contiguous data area that's been reserved based 373 * on the packet length. The data area is forced to 32-bit 374 * alignment and the buffer length to a multiple of 4 bytes. 375 * This is done mainly so beacon frames (that require this) 376 * can use this interface too. 377 */ 378struct mbuf * 379ieee80211_getmgtframe(uint8_t **frm, int headroom, int pktlen) 380{ 381 struct mbuf *m; 382 u_int len; 383 384 /* 385 * NB: we know the mbuf routines will align the data area 386 * so we don't need to do anything special. 387 */ 388 len = roundup2(headroom + pktlen, 4); 389 KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len)); 390 if (len < MINCLSIZE) { 391 m = m_gethdr(M_NOWAIT, MT_DATA); 392 /* 393 * Align the data in case additional headers are added. 394 * This should only happen when a WEP header is added 395 * which only happens for shared key authentication mgt 396 * frames which all fit in MHLEN. 397 */ 398 if (m != NULL) 399 MH_ALIGN(m, len); 400 } else { 401 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 402 if (m != NULL) 403 MC_ALIGN(m, len); 404 } 405 if (m != NULL) { 406 m->m_data += headroom; 407 *frm = m->m_data; 408 } 409 return m; 410} 411 412/* 413 * Re-align the payload in the mbuf. This is mainly used (right now) 414 * to handle IP header alignment requirements on certain architectures. 415 */ 416struct mbuf * 417ieee80211_realign(struct ieee80211vap *vap, struct mbuf *m, size_t align) 418{ 419 int pktlen, space; 420 struct mbuf *n; 421 422 pktlen = m->m_pkthdr.len; 423 space = pktlen + align; 424 if (space < MINCLSIZE) 425 n = m_gethdr(M_DONTWAIT, MT_DATA); 426 else { 427 n = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, 428 space <= MCLBYTES ? MCLBYTES : 429#if MJUMPAGESIZE != MCLBYTES 430 space <= MJUMPAGESIZE ? MJUMPAGESIZE : 431#endif 432 space <= MJUM9BYTES ? MJUM9BYTES : MJUM16BYTES); 433 } 434 if (__predict_true(n != NULL)) { 435 m_move_pkthdr(n, m); 436 n->m_data = (caddr_t)(ALIGN(n->m_data + align) - align); 437 m_copydata(m, 0, pktlen, mtod(n, caddr_t)); 438 n->m_len = pktlen; 439 } else { 440 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 441 mtod(m, const struct ieee80211_frame *), NULL, 442 "%s", "no mbuf to realign"); 443 vap->iv_stats.is_rx_badalign++; 444 } 445 m_freem(m); 446 return n; 447} 448 449int 450ieee80211_add_callback(struct mbuf *m, 451 void (*func)(struct ieee80211_node *, void *, int), void *arg) 452{ 453 struct m_tag *mtag; 454 struct ieee80211_cb *cb; 455 456 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_CALLBACK, 457 sizeof(struct ieee80211_cb), M_NOWAIT); 458 if (mtag == NULL) 459 return 0; 460 461 cb = (struct ieee80211_cb *)(mtag+1); 462 cb->func = func; 463 cb->arg = arg; 464 m_tag_prepend(m, mtag); 465 m->m_flags |= M_TXCB; 466 return 1; 467} 468 469void 470ieee80211_process_callback(struct ieee80211_node *ni, 471 struct mbuf *m, int status) 472{ 473 struct m_tag *mtag; 474 475 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_CALLBACK, NULL); 476 if (mtag != NULL) { 477 struct ieee80211_cb *cb = (struct ieee80211_cb *)(mtag+1); 478 cb->func(ni, cb->arg, status); 479 } 480} 481 482#include <sys/libkern.h> 483 484void 485get_random_bytes(void *p, size_t n) 486{ 487 uint8_t *dp = p; 488 489 while (n > 0) { 490 uint32_t v = arc4random(); 491 size_t nb = n > sizeof(uint32_t) ? sizeof(uint32_t) : n; 492 bcopy(&v, dp, n > sizeof(uint32_t) ? sizeof(uint32_t) : n); 493 dp += sizeof(uint32_t), n -= nb; 494 } 495} 496 497/* 498 * Helper function for events that pass just a single mac address. 499 */ 500static void 501notify_macaddr(struct ifnet *ifp, int op, const uint8_t mac[IEEE80211_ADDR_LEN]) 502{ 503 struct ieee80211_join_event iev; 504 505 CURVNET_SET(ifp->if_vnet); 506 memset(&iev, 0, sizeof(iev)); 507 IEEE80211_ADDR_COPY(iev.iev_addr, mac); 508 rt_ieee80211msg(ifp, op, &iev, sizeof(iev)); 509 CURVNET_RESTORE(); 510} 511 512void 513ieee80211_notify_node_join(struct ieee80211_node *ni, int newassoc) 514{ 515 struct ieee80211vap *vap = ni->ni_vap; 516 struct ifnet *ifp = vap->iv_ifp; 517 518 CURVNET_SET_QUIET(ifp->if_vnet); 519 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode join", 520 (ni == vap->iv_bss) ? "bss " : ""); 521 522 if (ni == vap->iv_bss) { 523 notify_macaddr(ifp, newassoc ? 524 RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC, ni->ni_bssid); 525 if_link_state_change(ifp, LINK_STATE_UP); 526 } else { 527 notify_macaddr(ifp, newassoc ? 528 RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN, ni->ni_macaddr); 529 } 530 CURVNET_RESTORE(); 531} 532 533void 534ieee80211_notify_node_leave(struct ieee80211_node *ni) 535{ 536 struct ieee80211vap *vap = ni->ni_vap; 537 struct ifnet *ifp = vap->iv_ifp; 538 539 CURVNET_SET_QUIET(ifp->if_vnet); 540 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode leave", 541 (ni == vap->iv_bss) ? "bss " : ""); 542 543 if (ni == vap->iv_bss) { 544 rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0); 545 if_link_state_change(ifp, LINK_STATE_DOWN); 546 } else { 547 /* fire off wireless event station leaving */ 548 notify_macaddr(ifp, RTM_IEEE80211_LEAVE, ni->ni_macaddr); 549 } 550 CURVNET_RESTORE(); 551} 552 553void 554ieee80211_notify_scan_done(struct ieee80211vap *vap) 555{ 556 struct ifnet *ifp = vap->iv_ifp; 557 558 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n", "notify scan done"); 559 560 /* dispatch wireless event indicating scan completed */ 561 CURVNET_SET(ifp->if_vnet); 562 rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0); 563 CURVNET_RESTORE(); 564} 565 566void 567ieee80211_notify_replay_failure(struct ieee80211vap *vap, 568 const struct ieee80211_frame *wh, const struct ieee80211_key *k, 569 u_int64_t rsc, int tid) 570{ 571 struct ifnet *ifp = vap->iv_ifp; 572 573 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2, 574 "%s replay detected <rsc %ju, csc %ju, keyix %u rxkeyix %u>", 575 k->wk_cipher->ic_name, (intmax_t) rsc, 576 (intmax_t) k->wk_keyrsc[tid], 577 k->wk_keyix, k->wk_rxkeyix); 578 579 if (ifp != NULL) { /* NB: for cipher test modules */ 580 struct ieee80211_replay_event iev; 581 582 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1); 583 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2); 584 iev.iev_cipher = k->wk_cipher->ic_cipher; 585 if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE) 586 iev.iev_keyix = k->wk_rxkeyix; 587 else 588 iev.iev_keyix = k->wk_keyix; 589 iev.iev_keyrsc = k->wk_keyrsc[tid]; 590 iev.iev_rsc = rsc; 591 CURVNET_SET(ifp->if_vnet); 592 rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev)); 593 CURVNET_RESTORE(); 594 } 595} 596 597void 598ieee80211_notify_michael_failure(struct ieee80211vap *vap, 599 const struct ieee80211_frame *wh, u_int keyix) 600{ 601 struct ifnet *ifp = vap->iv_ifp; 602 603 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2, 604 "michael MIC verification failed <keyix %u>", keyix); 605 vap->iv_stats.is_rx_tkipmic++; 606 607 if (ifp != NULL) { /* NB: for cipher test modules */ 608 struct ieee80211_michael_event iev; 609 610 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1); 611 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2); 612 iev.iev_cipher = IEEE80211_CIPHER_TKIP; 613 iev.iev_keyix = keyix; 614 CURVNET_SET(ifp->if_vnet); 615 rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev)); 616 CURVNET_RESTORE(); 617 } 618} 619 620void 621ieee80211_notify_wds_discover(struct ieee80211_node *ni) 622{ 623 struct ieee80211vap *vap = ni->ni_vap; 624 struct ifnet *ifp = vap->iv_ifp; 625 626 notify_macaddr(ifp, RTM_IEEE80211_WDS, ni->ni_macaddr); 627} 628 629void 630ieee80211_notify_csa(struct ieee80211com *ic, 631 const struct ieee80211_channel *c, int mode, int count) 632{ 633 struct ifnet *ifp = ic->ic_ifp; 634 struct ieee80211_csa_event iev; 635 636 memset(&iev, 0, sizeof(iev)); 637 iev.iev_flags = c->ic_flags; 638 iev.iev_freq = c->ic_freq; 639 iev.iev_ieee = c->ic_ieee; 640 iev.iev_mode = mode; 641 iev.iev_count = count; 642 rt_ieee80211msg(ifp, RTM_IEEE80211_CSA, &iev, sizeof(iev)); 643} 644 645void 646ieee80211_notify_radar(struct ieee80211com *ic, 647 const struct ieee80211_channel *c) 648{ 649 struct ifnet *ifp = ic->ic_ifp; 650 struct ieee80211_radar_event iev; 651 652 memset(&iev, 0, sizeof(iev)); 653 iev.iev_flags = c->ic_flags; 654 iev.iev_freq = c->ic_freq; 655 iev.iev_ieee = c->ic_ieee; 656 rt_ieee80211msg(ifp, RTM_IEEE80211_RADAR, &iev, sizeof(iev)); 657} 658 659void 660ieee80211_notify_cac(struct ieee80211com *ic, 661 const struct ieee80211_channel *c, enum ieee80211_notify_cac_event type) 662{ 663 struct ifnet *ifp = ic->ic_ifp; 664 struct ieee80211_cac_event iev; 665 666 memset(&iev, 0, sizeof(iev)); 667 iev.iev_flags = c->ic_flags; 668 iev.iev_freq = c->ic_freq; 669 iev.iev_ieee = c->ic_ieee; 670 iev.iev_type = type; 671 rt_ieee80211msg(ifp, RTM_IEEE80211_CAC, &iev, sizeof(iev)); 672} 673 674void 675ieee80211_notify_node_deauth(struct ieee80211_node *ni) 676{ 677 struct ieee80211vap *vap = ni->ni_vap; 678 struct ifnet *ifp = vap->iv_ifp; 679 680 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node deauth"); 681 682 notify_macaddr(ifp, RTM_IEEE80211_DEAUTH, ni->ni_macaddr); 683} 684 685void 686ieee80211_notify_node_auth(struct ieee80211_node *ni) 687{ 688 struct ieee80211vap *vap = ni->ni_vap; 689 struct ifnet *ifp = vap->iv_ifp; 690 691 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node auth"); 692 693 notify_macaddr(ifp, RTM_IEEE80211_AUTH, ni->ni_macaddr); 694} 695 696void 697ieee80211_notify_country(struct ieee80211vap *vap, 698 const uint8_t bssid[IEEE80211_ADDR_LEN], const uint8_t cc[2]) 699{ 700 struct ifnet *ifp = vap->iv_ifp; 701 struct ieee80211_country_event iev; 702 703 memset(&iev, 0, sizeof(iev)); 704 IEEE80211_ADDR_COPY(iev.iev_addr, bssid); 705 iev.iev_cc[0] = cc[0]; 706 iev.iev_cc[1] = cc[1]; 707 rt_ieee80211msg(ifp, RTM_IEEE80211_COUNTRY, &iev, sizeof(iev)); 708} 709 710void 711ieee80211_notify_radio(struct ieee80211com *ic, int state) 712{ 713 struct ifnet *ifp = ic->ic_ifp; 714 struct ieee80211_radio_event iev; 715 716 memset(&iev, 0, sizeof(iev)); 717 iev.iev_state = state; 718 rt_ieee80211msg(ifp, RTM_IEEE80211_RADIO, &iev, sizeof(iev)); 719} 720 721void 722ieee80211_load_module(const char *modname) 723{ 724 725#ifdef notyet 726 (void)kern_kldload(curthread, modname, NULL); 727#else 728 printf("%s: load the %s module by hand for now.\n", __func__, modname); 729#endif 730} 731 732static eventhandler_tag wlan_bpfevent; 733static eventhandler_tag wlan_ifllevent; 734 735static void 736bpf_track(void *arg, struct ifnet *ifp, int dlt, int attach) 737{ 738 /* NB: identify vap's by if_start */ 739 if (dlt == DLT_IEEE802_11_RADIO && ifp->if_start == ieee80211_start) { 740 struct ieee80211vap *vap = ifp->if_softc; 741 /* 742 * Track bpf radiotap listener state. We mark the vap 743 * to indicate if any listener is present and the com 744 * to indicate if any listener exists on any associated 745 * vap. This flag is used by drivers to prepare radiotap 746 * state only when needed. 747 */ 748 if (attach) { 749 ieee80211_syncflag_ext(vap, IEEE80211_FEXT_BPF); 750 if (vap->iv_opmode == IEEE80211_M_MONITOR) 751 atomic_add_int(&vap->iv_ic->ic_montaps, 1); 752 } else if (!bpf_peers_present(vap->iv_rawbpf)) { 753 ieee80211_syncflag_ext(vap, -IEEE80211_FEXT_BPF); 754 if (vap->iv_opmode == IEEE80211_M_MONITOR) 755 atomic_subtract_int(&vap->iv_ic->ic_montaps, 1); 756 } 757 } 758} 759 760static void 761wlan_iflladdr(void *arg __unused, struct ifnet *ifp) 762{ 763 struct ieee80211com *ic = ifp->if_l2com; 764 struct ieee80211vap *vap, *next; 765 766 if (ifp->if_type != IFT_IEEE80211 || ic == NULL) 767 return; 768 769 IEEE80211_LOCK(ic); 770 TAILQ_FOREACH_SAFE(vap, &ic->ic_vaps, iv_next, next) { 771 /* 772 * If the MAC address has changed on the parent and it was 773 * copied to the vap on creation then re-sync. 774 */ 775 if (vap->iv_ic == ic && 776 (vap->iv_flags_ext & IEEE80211_FEXT_UNIQMAC) == 0) { 777 IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp)); 778 IEEE80211_UNLOCK(ic); 779 if_setlladdr(vap->iv_ifp, IF_LLADDR(ifp), 780 IEEE80211_ADDR_LEN); 781 IEEE80211_LOCK(ic); 782 } 783 } 784 IEEE80211_UNLOCK(ic); 785} 786 787/* 788 * Module glue. 789 * 790 * NB: the module name is "wlan" for compatibility with NetBSD. 791 */ 792static int 793wlan_modevent(module_t mod, int type, void *unused) 794{ 795 switch (type) { 796 case MOD_LOAD: 797 if (bootverbose) 798 printf("wlan: <802.11 Link Layer>\n"); 799 wlan_bpfevent = EVENTHANDLER_REGISTER(bpf_track, 800 bpf_track, 0, EVENTHANDLER_PRI_ANY); 801 if (wlan_bpfevent == NULL) 802 return ENOMEM; 803 wlan_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event, 804 wlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY); 805 if (wlan_ifllevent == NULL) { 806 EVENTHANDLER_DEREGISTER(bpf_track, wlan_bpfevent); 807 return ENOMEM; 808 } 809 if_clone_attach(&wlan_cloner); 810 if_register_com_alloc(IFT_IEEE80211, wlan_alloc, wlan_free); 811 return 0; 812 case MOD_UNLOAD: 813 if_deregister_com_alloc(IFT_IEEE80211); 814 if_clone_detach(&wlan_cloner); 815 EVENTHANDLER_DEREGISTER(bpf_track, wlan_bpfevent); 816 EVENTHANDLER_DEREGISTER(iflladdr_event, wlan_ifllevent); 817 return 0; 818 } 819 return EINVAL; 820} 821 822static moduledata_t wlan_mod = { 823 "wlan", 824 wlan_modevent, 825 0 826}; 827DECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 828MODULE_VERSION(wlan, 1); 829MODULE_DEPEND(wlan, ether, 1, 1, 1); 830