1 /*- 2 * Copyright (c) 2003-2009 Sam Leffler, Errno Consulting 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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. 24 */ 25 26 #include <sys/cdefs.h> 27 __FBSDID("$FreeBSD$"); 28 29 /* 30 * IEEE 802.11 support (FreeBSD-specific code) 31 */ 32 #include "opt_wlan.h" 33 34 #include <sys/param.h> 35 #include <sys/kernel.h> 36 #include <sys/systm.h> 37 #include <sys/linker.h> 38 #include <sys/mbuf.h> 39 #include <sys/module.h> 40 #include <sys/proc.h> 41 #include <sys/sysctl.h> 42 43 #include <sys/socket.h> 44 45 #include <net/bpf.h> 46 #include <net/if.h> 47 #include <net/if_dl.h> 48 #include <net/if_clone.h> 49 #include <net/if_media.h> 50 #include <net/if_types.h> 51 #include <net/ethernet.h> 52 #include <net/route.h> 53 #include <net/vnet.h> 54 55 #include <net80211/ieee80211_var.h> 56 #include <net80211/ieee80211_input.h> 57 58 SYSCTL_NODE(_net, OID_AUTO, wlan, CTLFLAG_RD, 0, "IEEE 80211 parameters"); 59 60 #ifdef IEEE80211_DEBUG 61 int ieee80211_debug = 0; 62 SYSCTL_INT(_net_wlan, OID_AUTO, debug, CTLFLAG_RW, &ieee80211_debug, 63 0, "debugging printfs"); 64 #endif 65 66 static MALLOC_DEFINE(M_80211_COM, "80211com", "802.11 com state"); 67 68 #if __FreeBSD_version >= 1000020 69 static const char wlanname[] = "wlan"; 70 static struct if_clone *wlan_cloner; 71 #endif 72 73 /* 74 * Allocate/free com structure in conjunction with ifnet; 75 * these routines are registered with if_register_com_alloc 76 * below and are called automatically by the ifnet code 77 * when the ifnet of the parent device is created. 78 */ 79 static void * 80 wlan_alloc(u_char type, struct ifnet *ifp) 81 { 82 struct ieee80211com *ic; 83 84 ic = malloc(sizeof(struct ieee80211com), M_80211_COM, M_WAITOK|M_ZERO); 85 ic->ic_ifp = ifp; 86 87 return (ic); 88 } 89 90 static void 91 wlan_free(void *ic, u_char type) 92 { 93 free(ic, M_80211_COM); 94 } 95 96 static int 97 wlan_clone_create(struct if_clone *ifc, int unit, caddr_t params) 98 { 99 struct ieee80211_clone_params cp; 100 struct ieee80211vap *vap; 101 struct ieee80211com *ic; 102 struct ifnet *ifp; 103 int error; 104 105 error = copyin(params, &cp, sizeof(cp)); 106 if (error) 107 return error; 108 ifp = ifunit(cp.icp_parent); 109 if (ifp == NULL) 110 return ENXIO; 111 /* XXX move printfs to DIAGNOSTIC before release */ 112 if (ifp->if_type != IFT_IEEE80211) { 113 if_printf(ifp, "%s: reject, not an 802.11 device\n", __func__); 114 return ENXIO; 115 } 116 if (cp.icp_opmode >= IEEE80211_OPMODE_MAX) { 117 if_printf(ifp, "%s: invalid opmode %d\n", 118 __func__, cp.icp_opmode); 119 return EINVAL; 120 } 121 ic = ifp->if_l2com; 122 if ((ic->ic_caps & ieee80211_opcap[cp.icp_opmode]) == 0) { 123 if_printf(ifp, "%s mode not supported\n", 124 ieee80211_opmode_name[cp.icp_opmode]); 125 return EOPNOTSUPP; 126 } 127 if ((cp.icp_flags & IEEE80211_CLONE_TDMA) && 128 #ifdef IEEE80211_SUPPORT_TDMA 129 (ic->ic_caps & IEEE80211_C_TDMA) == 0 130 #else 131 (1) 132 #endif 133 ) { 134 if_printf(ifp, "TDMA not supported\n"); 135 return EOPNOTSUPP; 136 } 137 #if __FreeBSD_version >= 1000020 138 vap = ic->ic_vap_create(ic, wlanname, unit, 139 cp.icp_opmode, cp.icp_flags, cp.icp_bssid, 140 cp.icp_flags & IEEE80211_CLONE_MACADDR ? 141 cp.icp_macaddr : (const uint8_t *)IF_LLADDR(ifp)); 142 #else 143 vap = ic->ic_vap_create(ic, ifc->ifc_name, unit, 144 cp.icp_opmode, cp.icp_flags, cp.icp_bssid, 145 cp.icp_flags & IEEE80211_CLONE_MACADDR ? 146 cp.icp_macaddr : (const uint8_t *)IF_LLADDR(ifp)); 147 148 #endif 149 150 return (vap == NULL ? EIO : 0); 151 } 152 153 static void 154 wlan_clone_destroy(struct ifnet *ifp) 155 { 156 struct ieee80211vap *vap = ifp->if_softc; 157 struct ieee80211com *ic = vap->iv_ic; 158 159 ic->ic_vap_delete(vap); 160 } 161 162 #if __FreeBSD_version < 1000020 163 IFC_SIMPLE_DECLARE(wlan, 0); 164 #endif 165 166 void 167 ieee80211_vap_destroy(struct ieee80211vap *vap) 168 { 169 CURVNET_SET(vap->iv_ifp->if_vnet); 170 #if __FreeBSD_version >= 1000020 171 if_clone_destroyif(wlan_cloner, vap->iv_ifp); 172 #else 173 if_clone_destroyif(&wlan_cloner, vap->iv_ifp); 174 #endif 175 CURVNET_RESTORE(); 176 } 177 178 int 179 ieee80211_sysctl_msecs_ticks(SYSCTL_HANDLER_ARGS) 180 { 181 int msecs = ticks_to_msecs(*(int *)arg1); 182 int error, t; 183 184 error = sysctl_handle_int(oidp, &msecs, 0, req); 185 if (error || !req->newptr) 186 return error; 187 t = msecs_to_ticks(msecs); 188 *(int *)arg1 = (t < 1) ? 1 : t; 189 return 0; 190 } 191 192 static int 193 ieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS) 194 { 195 int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT; 196 int error; 197 198 error = sysctl_handle_int(oidp, &inact, 0, req); 199 if (error || !req->newptr) 200 return error; 201 *(int *)arg1 = inact / IEEE80211_INACT_WAIT; 202 return 0; 203 } 204 205 static int 206 ieee80211_sysctl_parent(SYSCTL_HANDLER_ARGS) 207 { 208 struct ieee80211com *ic = arg1; 209 const char *name = ic->ic_ifp->if_xname; 210 211 return SYSCTL_OUT(req, name, strlen(name)); 212 } 213 214 static int 215 ieee80211_sysctl_radar(SYSCTL_HANDLER_ARGS) 216 { 217 struct ieee80211com *ic = arg1; 218 int t = 0, error; 219 220 error = sysctl_handle_int(oidp, &t, 0, req); 221 if (error || !req->newptr) 222 return error; 223 IEEE80211_LOCK(ic); 224 ieee80211_dfs_notify_radar(ic, ic->ic_curchan); 225 IEEE80211_UNLOCK(ic); 226 return 0; 227 } 228 229 void 230 ieee80211_sysctl_attach(struct ieee80211com *ic) 231 { 232 } 233 234 void 235 ieee80211_sysctl_detach(struct ieee80211com *ic) 236 { 237 } 238 239 void 240 ieee80211_sysctl_vattach(struct ieee80211vap *vap) 241 { 242 struct ifnet *ifp = vap->iv_ifp; 243 struct sysctl_ctx_list *ctx; 244 struct sysctl_oid *oid; 245 char num[14]; /* sufficient for 32 bits */ 246 247 ctx = (struct sysctl_ctx_list *) malloc(sizeof(struct sysctl_ctx_list), 248 M_DEVBUF, M_NOWAIT | M_ZERO); 249 if (ctx == NULL) { 250 if_printf(ifp, "%s: cannot allocate sysctl context!\n", 251 __func__); 252 return; 253 } 254 sysctl_ctx_init(ctx); 255 snprintf(num, sizeof(num), "%u", ifp->if_dunit); 256 oid = SYSCTL_ADD_NODE(ctx, &SYSCTL_NODE_CHILDREN(_net, wlan), 257 OID_AUTO, num, CTLFLAG_RD, NULL, ""); 258 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 259 "%parent", CTLTYPE_STRING | CTLFLAG_RD, vap->iv_ic, 0, 260 ieee80211_sysctl_parent, "A", "parent device"); 261 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 262 "driver_caps", CTLFLAG_RW, &vap->iv_caps, 0, 263 "driver capabilities"); 264 #ifdef IEEE80211_DEBUG 265 vap->iv_debug = ieee80211_debug; 266 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 267 "debug", CTLFLAG_RW, &vap->iv_debug, 0, 268 "control debugging printfs"); 269 #endif 270 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 271 "bmiss_max", CTLFLAG_RW, &vap->iv_bmiss_max, 0, 272 "consecutive beacon misses before scanning"); 273 /* XXX inherit from tunables */ 274 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 275 "inact_run", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_run, 0, 276 ieee80211_sysctl_inact, "I", 277 "station inactivity timeout (sec)"); 278 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 279 "inact_probe", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_probe, 0, 280 ieee80211_sysctl_inact, "I", 281 "station inactivity probe timeout (sec)"); 282 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 283 "inact_auth", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_auth, 0, 284 ieee80211_sysctl_inact, "I", 285 "station authentication timeout (sec)"); 286 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 287 "inact_init", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_init, 0, 288 ieee80211_sysctl_inact, "I", 289 "station initial state timeout (sec)"); 290 if (vap->iv_htcaps & IEEE80211_HTC_HT) { 291 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 292 "ampdu_mintraffic_bk", CTLFLAG_RW, 293 &vap->iv_ampdu_mintraffic[WME_AC_BK], 0, 294 "BK traffic tx aggr threshold (pps)"); 295 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 296 "ampdu_mintraffic_be", CTLFLAG_RW, 297 &vap->iv_ampdu_mintraffic[WME_AC_BE], 0, 298 "BE traffic tx aggr threshold (pps)"); 299 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 300 "ampdu_mintraffic_vo", CTLFLAG_RW, 301 &vap->iv_ampdu_mintraffic[WME_AC_VO], 0, 302 "VO traffic tx aggr threshold (pps)"); 303 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 304 "ampdu_mintraffic_vi", CTLFLAG_RW, 305 &vap->iv_ampdu_mintraffic[WME_AC_VI], 0, 306 "VI traffic tx aggr threshold (pps)"); 307 } 308 if (vap->iv_caps & IEEE80211_C_DFS) { 309 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 310 "radar", CTLTYPE_INT | CTLFLAG_RW, vap->iv_ic, 0, 311 ieee80211_sysctl_radar, "I", "simulate radar event"); 312 } 313 vap->iv_sysctl = ctx; 314 vap->iv_oid = oid; 315 } 316 317 void 318 ieee80211_sysctl_vdetach(struct ieee80211vap *vap) 319 { 320 321 if (vap->iv_sysctl != NULL) { 322 sysctl_ctx_free(vap->iv_sysctl); 323 free(vap->iv_sysctl, M_DEVBUF); 324 vap->iv_sysctl = NULL; 325 } 326 } 327 328 int 329 ieee80211_node_dectestref(struct ieee80211_node *ni) 330 { 331 /* XXX need equivalent of atomic_dec_and_test */ 332 atomic_subtract_int(&ni->ni_refcnt, 1); 333 return atomic_cmpset_int(&ni->ni_refcnt, 0, 1); 334 } 335 336 void 337 ieee80211_drain_ifq(struct ifqueue *ifq) 338 { 339 struct ieee80211_node *ni; 340 struct mbuf *m; 341 342 for (;;) { 343 IF_DEQUEUE(ifq, m); 344 if (m == NULL) 345 break; 346 347 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 348 KASSERT(ni != NULL, ("frame w/o node")); 349 ieee80211_free_node(ni); 350 m->m_pkthdr.rcvif = NULL; 351 352 m_freem(m); 353 } 354 } 355 356 void 357 ieee80211_flush_ifq(struct ifqueue *ifq, struct ieee80211vap *vap) 358 { 359 struct ieee80211_node *ni; 360 struct mbuf *m, **mprev; 361 362 IF_LOCK(ifq); 363 mprev = &ifq->ifq_head; 364 while ((m = *mprev) != NULL) { 365 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 366 if (ni != NULL && ni->ni_vap == vap) { 367 *mprev = m->m_nextpkt; /* remove from list */ 368 ifq->ifq_len--; 369 370 m_freem(m); 371 ieee80211_free_node(ni); /* reclaim ref */ 372 } else 373 mprev = &m->m_nextpkt; 374 } 375 /* recalculate tail ptr */ 376 m = ifq->ifq_head; 377 for (; m != NULL && m->m_nextpkt != NULL; m = m->m_nextpkt) 378 ; 379 ifq->ifq_tail = m; 380 IF_UNLOCK(ifq); 381 } 382 383 /* 384 * As above, for mbufs allocated with m_gethdr/MGETHDR 385 * or initialized by M_COPY_PKTHDR. 386 */ 387 #define MC_ALIGN(m, len) \ 388 do { \ 389 (m)->m_data += (MCLBYTES - (len)) &~ (sizeof(long) - 1); \ 390 } while (/* CONSTCOND */ 0) 391 392 /* 393 * Allocate and setup a management frame of the specified 394 * size. We return the mbuf and a pointer to the start 395 * of the contiguous data area that's been reserved based 396 * on the packet length. The data area is forced to 32-bit 397 * alignment and the buffer length to a multiple of 4 bytes. 398 * This is done mainly so beacon frames (that require this) 399 * can use this interface too. 400 */ 401 struct mbuf * 402 ieee80211_getmgtframe(uint8_t **frm, int headroom, int pktlen) 403 { 404 struct mbuf *m; 405 u_int len; 406 407 /* 408 * NB: we know the mbuf routines will align the data area 409 * so we don't need to do anything special. 410 */ 411 len = roundup2(headroom + pktlen, 4); 412 KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len)); 413 if (len < MINCLSIZE) { 414 m = m_gethdr(M_NOWAIT, MT_DATA); 415 /* 416 * Align the data in case additional headers are added. 417 * This should only happen when a WEP header is added 418 * which only happens for shared key authentication mgt 419 * frames which all fit in MHLEN. 420 */ 421 if (m != NULL) 422 MH_ALIGN(m, len); 423 } else { 424 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 425 if (m != NULL) 426 MC_ALIGN(m, len); 427 } 428 if (m != NULL) { 429 m->m_data += headroom; 430 *frm = m->m_data; 431 } 432 return m; 433 } 434 435 #ifndef __NO_STRICT_ALIGNMENT 436 /* 437 * Re-align the payload in the mbuf. This is mainly used (right now) 438 * to handle IP header alignment requirements on certain architectures. 439 */ 440 struct mbuf * 441 ieee80211_realign(struct ieee80211vap *vap, struct mbuf *m, size_t align) 442 { 443 int pktlen, space; 444 struct mbuf *n; 445 446 pktlen = m->m_pkthdr.len; 447 space = pktlen + align; 448 if (space < MINCLSIZE) 449 n = m_gethdr(M_NOWAIT, MT_DATA); 450 else { 451 n = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, 452 space <= MCLBYTES ? MCLBYTES : 453 #if MJUMPAGESIZE != MCLBYTES 454 space <= MJUMPAGESIZE ? MJUMPAGESIZE : 455 #endif 456 space <= MJUM9BYTES ? MJUM9BYTES : MJUM16BYTES); 457 } 458 if (__predict_true(n != NULL)) { 459 m_move_pkthdr(n, m); 460 n->m_data = (caddr_t)(ALIGN(n->m_data + align) - align); 461 m_copydata(m, 0, pktlen, mtod(n, caddr_t)); 462 n->m_len = pktlen; 463 } else { 464 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 465 mtod(m, const struct ieee80211_frame *), NULL, 466 "%s", "no mbuf to realign"); 467 vap->iv_stats.is_rx_badalign++; 468 } 469 m_freem(m); 470 return n; 471 } 472 #endif /* !__NO_STRICT_ALIGNMENT */ 473 474 int 475 ieee80211_add_callback(struct mbuf *m, 476 void (*func)(struct ieee80211_node *, void *, int), void *arg) 477 { 478 struct m_tag *mtag; 479 struct ieee80211_cb *cb; 480 481 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_CALLBACK, 482 sizeof(struct ieee80211_cb), M_NOWAIT); 483 if (mtag == NULL) 484 return 0; 485 486 cb = (struct ieee80211_cb *)(mtag+1); 487 cb->func = func; 488 cb->arg = arg; 489 m_tag_prepend(m, mtag); 490 m->m_flags |= M_TXCB; 491 return 1; 492 } 493 494 void 495 ieee80211_process_callback(struct ieee80211_node *ni, 496 struct mbuf *m, int status) 497 { 498 struct m_tag *mtag; 499 500 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_CALLBACK, NULL); 501 if (mtag != NULL) { 502 struct ieee80211_cb *cb = (struct ieee80211_cb *)(mtag+1); 503 cb->func(ni, cb->arg, status); 504 } 505 } 506 507 /* 508 * Transmit a frame to the parent interface. 509 * 510 * TODO: if the transmission fails, make sure the parent node is freed 511 * (the callers will first need modifying.) 512 */ 513 int 514 ieee80211_parent_transmit(struct ieee80211com *ic, 515 struct mbuf *m) 516 { 517 struct ifnet *parent = ic->ic_ifp; 518 /* 519 * Assert the IC TX lock is held - this enforces the 520 * processing -> queuing order is maintained 521 */ 522 IEEE80211_TX_LOCK_ASSERT(ic); 523 524 return (parent->if_transmit(parent, m)); 525 } 526 527 /* 528 * Transmit a frame to the VAP interface. 529 */ 530 int 531 ieee80211_vap_transmit(struct ieee80211vap *vap, struct mbuf *m) 532 { 533 struct ifnet *ifp = vap->iv_ifp; 534 535 /* 536 * When transmitting via the VAP, we shouldn't hold 537 * any IC TX lock as the VAP TX path will acquire it. 538 */ 539 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic); 540 541 return (ifp->if_transmit(ifp, m)); 542 543 } 544 545 #include <sys/libkern.h> 546 547 void 548 get_random_bytes(void *p, size_t n) 549 { 550 uint8_t *dp = p; 551 552 while (n > 0) { 553 uint32_t v = arc4random(); 554 size_t nb = n > sizeof(uint32_t) ? sizeof(uint32_t) : n; 555 bcopy(&v, dp, n > sizeof(uint32_t) ? sizeof(uint32_t) : n); 556 dp += sizeof(uint32_t), n -= nb; 557 } 558 } 559 560 /* 561 * Helper function for events that pass just a single mac address. 562 */ 563 static void 564 notify_macaddr(struct ifnet *ifp, int op, const uint8_t mac[IEEE80211_ADDR_LEN]) 565 { 566 struct ieee80211_join_event iev; 567 568 CURVNET_SET(ifp->if_vnet); 569 memset(&iev, 0, sizeof(iev)); 570 IEEE80211_ADDR_COPY(iev.iev_addr, mac); 571 rt_ieee80211msg(ifp, op, &iev, sizeof(iev)); 572 CURVNET_RESTORE(); 573 } 574 575 void 576 ieee80211_notify_node_join(struct ieee80211_node *ni, int newassoc) 577 { 578 struct ieee80211vap *vap = ni->ni_vap; 579 struct ifnet *ifp = vap->iv_ifp; 580 581 CURVNET_SET_QUIET(ifp->if_vnet); 582 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode join", 583 (ni == vap->iv_bss) ? "bss " : ""); 584 585 if (ni == vap->iv_bss) { 586 notify_macaddr(ifp, newassoc ? 587 RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC, ni->ni_bssid); 588 if_link_state_change(ifp, LINK_STATE_UP); 589 } else { 590 notify_macaddr(ifp, newassoc ? 591 RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN, ni->ni_macaddr); 592 } 593 CURVNET_RESTORE(); 594 } 595 596 void 597 ieee80211_notify_node_leave(struct ieee80211_node *ni) 598 { 599 struct ieee80211vap *vap = ni->ni_vap; 600 struct ifnet *ifp = vap->iv_ifp; 601 602 CURVNET_SET_QUIET(ifp->if_vnet); 603 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode leave", 604 (ni == vap->iv_bss) ? "bss " : ""); 605 606 if (ni == vap->iv_bss) { 607 rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0); 608 if_link_state_change(ifp, LINK_STATE_DOWN); 609 } else { 610 /* fire off wireless event station leaving */ 611 notify_macaddr(ifp, RTM_IEEE80211_LEAVE, ni->ni_macaddr); 612 } 613 CURVNET_RESTORE(); 614 } 615 616 void 617 ieee80211_notify_scan_done(struct ieee80211vap *vap) 618 { 619 struct ifnet *ifp = vap->iv_ifp; 620 621 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n", "notify scan done"); 622 623 /* dispatch wireless event indicating scan completed */ 624 CURVNET_SET(ifp->if_vnet); 625 rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0); 626 CURVNET_RESTORE(); 627 } 628 629 void 630 ieee80211_notify_replay_failure(struct ieee80211vap *vap, 631 const struct ieee80211_frame *wh, const struct ieee80211_key *k, 632 u_int64_t rsc, int tid) 633 { 634 struct ifnet *ifp = vap->iv_ifp; 635 636 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2, 637 "%s replay detected tid %d <rsc %ju, csc %ju, keyix %u rxkeyix %u>", 638 k->wk_cipher->ic_name, tid, (intmax_t) rsc, 639 (intmax_t) k->wk_keyrsc[tid], 640 k->wk_keyix, k->wk_rxkeyix); 641 642 if (ifp != NULL) { /* NB: for cipher test modules */ 643 struct ieee80211_replay_event iev; 644 645 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1); 646 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2); 647 iev.iev_cipher = k->wk_cipher->ic_cipher; 648 if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE) 649 iev.iev_keyix = k->wk_rxkeyix; 650 else 651 iev.iev_keyix = k->wk_keyix; 652 iev.iev_keyrsc = k->wk_keyrsc[tid]; 653 iev.iev_rsc = rsc; 654 CURVNET_SET(ifp->if_vnet); 655 rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev)); 656 CURVNET_RESTORE(); 657 } 658 } 659 660 void 661 ieee80211_notify_michael_failure(struct ieee80211vap *vap, 662 const struct ieee80211_frame *wh, u_int keyix) 663 { 664 struct ifnet *ifp = vap->iv_ifp; 665 666 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2, 667 "michael MIC verification failed <keyix %u>", keyix); 668 vap->iv_stats.is_rx_tkipmic++; 669 670 if (ifp != NULL) { /* NB: for cipher test modules */ 671 struct ieee80211_michael_event iev; 672 673 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1); 674 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2); 675 iev.iev_cipher = IEEE80211_CIPHER_TKIP; 676 iev.iev_keyix = keyix; 677 CURVNET_SET(ifp->if_vnet); 678 rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev)); 679 CURVNET_RESTORE(); 680 } 681 } 682 683 void 684 ieee80211_notify_wds_discover(struct ieee80211_node *ni) 685 { 686 struct ieee80211vap *vap = ni->ni_vap; 687 struct ifnet *ifp = vap->iv_ifp; 688 689 notify_macaddr(ifp, RTM_IEEE80211_WDS, ni->ni_macaddr); 690 } 691 692 void 693 ieee80211_notify_csa(struct ieee80211com *ic, 694 const struct ieee80211_channel *c, int mode, int count) 695 { 696 struct ifnet *ifp = ic->ic_ifp; 697 struct ieee80211_csa_event iev; 698 699 memset(&iev, 0, sizeof(iev)); 700 iev.iev_flags = c->ic_flags; 701 iev.iev_freq = c->ic_freq; 702 iev.iev_ieee = c->ic_ieee; 703 iev.iev_mode = mode; 704 iev.iev_count = count; 705 CURVNET_SET(ifp->if_vnet); 706 rt_ieee80211msg(ifp, RTM_IEEE80211_CSA, &iev, sizeof(iev)); 707 CURVNET_RESTORE(); 708 } 709 710 void 711 ieee80211_notify_radar(struct ieee80211com *ic, 712 const struct ieee80211_channel *c) 713 { 714 struct ifnet *ifp = ic->ic_ifp; 715 struct ieee80211_radar_event iev; 716 717 memset(&iev, 0, sizeof(iev)); 718 iev.iev_flags = c->ic_flags; 719 iev.iev_freq = c->ic_freq; 720 iev.iev_ieee = c->ic_ieee; 721 CURVNET_SET(ifp->if_vnet); 722 rt_ieee80211msg(ifp, RTM_IEEE80211_RADAR, &iev, sizeof(iev)); 723 CURVNET_RESTORE(); 724 } 725 726 void 727 ieee80211_notify_cac(struct ieee80211com *ic, 728 const struct ieee80211_channel *c, enum ieee80211_notify_cac_event type) 729 { 730 struct ifnet *ifp = ic->ic_ifp; 731 struct ieee80211_cac_event iev; 732 733 memset(&iev, 0, sizeof(iev)); 734 iev.iev_flags = c->ic_flags; 735 iev.iev_freq = c->ic_freq; 736 iev.iev_ieee = c->ic_ieee; 737 iev.iev_type = type; 738 CURVNET_SET(ifp->if_vnet); 739 rt_ieee80211msg(ifp, RTM_IEEE80211_CAC, &iev, sizeof(iev)); 740 CURVNET_RESTORE(); 741 } 742 743 void 744 ieee80211_notify_node_deauth(struct ieee80211_node *ni) 745 { 746 struct ieee80211vap *vap = ni->ni_vap; 747 struct ifnet *ifp = vap->iv_ifp; 748 749 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node deauth"); 750 751 notify_macaddr(ifp, RTM_IEEE80211_DEAUTH, ni->ni_macaddr); 752 } 753 754 void 755 ieee80211_notify_node_auth(struct ieee80211_node *ni) 756 { 757 struct ieee80211vap *vap = ni->ni_vap; 758 struct ifnet *ifp = vap->iv_ifp; 759 760 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node auth"); 761 762 notify_macaddr(ifp, RTM_IEEE80211_AUTH, ni->ni_macaddr); 763 } 764 765 void 766 ieee80211_notify_country(struct ieee80211vap *vap, 767 const uint8_t bssid[IEEE80211_ADDR_LEN], const uint8_t cc[2]) 768 { 769 struct ifnet *ifp = vap->iv_ifp; 770 struct ieee80211_country_event iev; 771 772 memset(&iev, 0, sizeof(iev)); 773 IEEE80211_ADDR_COPY(iev.iev_addr, bssid); 774 iev.iev_cc[0] = cc[0]; 775 iev.iev_cc[1] = cc[1]; 776 CURVNET_SET(ifp->if_vnet); 777 rt_ieee80211msg(ifp, RTM_IEEE80211_COUNTRY, &iev, sizeof(iev)); 778 CURVNET_RESTORE(); 779 } 780 781 void 782 ieee80211_notify_radio(struct ieee80211com *ic, int state) 783 { 784 struct ifnet *ifp = ic->ic_ifp; 785 struct ieee80211_radio_event iev; 786 787 memset(&iev, 0, sizeof(iev)); 788 iev.iev_state = state; 789 CURVNET_SET(ifp->if_vnet); 790 rt_ieee80211msg(ifp, RTM_IEEE80211_RADIO, &iev, sizeof(iev)); 791 CURVNET_RESTORE(); 792 } 793 794 void 795 ieee80211_load_module(const char *modname) 796 { 797 798 #ifdef notyet 799 (void)kern_kldload(curthread, modname, NULL); 800 #else 801 printf("%s: load the %s module by hand for now.\n", __func__, modname); 802 #endif 803 } 804 805 static eventhandler_tag wlan_bpfevent; 806 static eventhandler_tag wlan_ifllevent; 807 808 static void 809 bpf_track(void *arg, struct ifnet *ifp, int dlt, int attach) 810 { 811 /* NB: identify vap's by if_start */ 812 if (dlt == DLT_IEEE802_11_RADIO && ifp->if_start == ieee80211_start) { 813 struct ieee80211vap *vap = ifp->if_softc; 814 /* 815 * Track bpf radiotap listener state. We mark the vap 816 * to indicate if any listener is present and the com 817 * to indicate if any listener exists on any associated 818 * vap. This flag is used by drivers to prepare radiotap 819 * state only when needed. 820 */ 821 if (attach) { 822 ieee80211_syncflag_ext(vap, IEEE80211_FEXT_BPF); 823 if (vap->iv_opmode == IEEE80211_M_MONITOR) 824 atomic_add_int(&vap->iv_ic->ic_montaps, 1); 825 } else if (!bpf_peers_present(vap->iv_rawbpf)) { 826 ieee80211_syncflag_ext(vap, -IEEE80211_FEXT_BPF); 827 if (vap->iv_opmode == IEEE80211_M_MONITOR) 828 atomic_subtract_int(&vap->iv_ic->ic_montaps, 1); 829 } 830 } 831 } 832 833 static void 834 wlan_iflladdr(void *arg __unused, struct ifnet *ifp) 835 { 836 struct ieee80211com *ic = ifp->if_l2com; 837 struct ieee80211vap *vap, *next; 838 839 if (ifp->if_type != IFT_IEEE80211 || ic == NULL) 840 return; 841 842 IEEE80211_LOCK(ic); 843 TAILQ_FOREACH_SAFE(vap, &ic->ic_vaps, iv_next, next) { 844 /* 845 * If the MAC address has changed on the parent and it was 846 * copied to the vap on creation then re-sync. 847 */ 848 if (vap->iv_ic == ic && 849 (vap->iv_flags_ext & IEEE80211_FEXT_UNIQMAC) == 0) { 850 IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp)); 851 IEEE80211_UNLOCK(ic); 852 if_setlladdr(vap->iv_ifp, IF_LLADDR(ifp), 853 IEEE80211_ADDR_LEN); 854 IEEE80211_LOCK(ic); 855 } 856 } 857 IEEE80211_UNLOCK(ic); 858 } 859 860 /* 861 * Module glue. 862 * 863 * NB: the module name is "wlan" for compatibility with NetBSD. 864 */ 865 static int 866 wlan_modevent(module_t mod, int type, void *unused) 867 { 868 switch (type) { 869 case MOD_LOAD: 870 if (bootverbose) 871 printf("wlan: <802.11 Link Layer>\n"); 872 wlan_bpfevent = EVENTHANDLER_REGISTER(bpf_track, 873 bpf_track, 0, EVENTHANDLER_PRI_ANY); 874 if (wlan_bpfevent == NULL) 875 return ENOMEM; 876 wlan_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event, 877 wlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY); 878 if (wlan_ifllevent == NULL) { 879 EVENTHANDLER_DEREGISTER(bpf_track, wlan_bpfevent); 880 return ENOMEM; 881 } 882 #if __FreeBSD_version >= 1000020 883 wlan_cloner = if_clone_simple(wlanname, wlan_clone_create, 884 wlan_clone_destroy, 0); 885 #else 886 if_clone_attach(&wlan_cloner); 887 #endif 888 if_register_com_alloc(IFT_IEEE80211, wlan_alloc, wlan_free); 889 return 0; 890 case MOD_UNLOAD: 891 if_deregister_com_alloc(IFT_IEEE80211); 892 #if __FreeBSD_version >= 1000020 893 if_clone_detach(wlan_cloner); 894 #else 895 if_clone_detach(&wlan_cloner); 896 #endif 897 EVENTHANDLER_DEREGISTER(bpf_track, wlan_bpfevent); 898 EVENTHANDLER_DEREGISTER(iflladdr_event, wlan_ifllevent); 899 return 0; 900 } 901 return EINVAL; 902 } 903 904 static moduledata_t wlan_mod = { 905 #if __FreeBSD_version >= 1000020 906 wlanname, 907 #else 908 "wlan", 909 #endif 910 wlan_modevent, 911 0 912 }; 913 DECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 914 MODULE_VERSION(wlan, 1); 915 MODULE_DEPEND(wlan, ether, 1, 1, 1); 916 #ifdef IEEE80211_ALQ 917 MODULE_DEPEND(wlan, alq, 1, 1, 1); 918 #endif /* IEEE80211_ALQ */ 919 920