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 * $FreeBSD: head/sys/net80211/ieee80211_freebsd.c 202612 2010-01-19 05:00:57Z thompsa $ 26 */ 27 28 /* 29 * IEEE 802.11 support (DragonFlyBSD-specific code) 30 */ 31 #include "opt_wlan.h" 32 33 #include <sys/param.h> 34 #include <sys/kernel.h> 35 #include <sys/systm.h> 36 #include <sys/linker.h> 37 #include <sys/mbuf.h> 38 #include <sys/module.h> 39 #include <sys/proc.h> 40 #include <sys/sysctl.h> 41 42 #include <sys/socket.h> 43 44 #include <net/bpf.h> 45 #include <net/if.h> 46 #include <net/if_dl.h> 47 #include <net/if_clone.h> 48 #include <net/if_media.h> 49 #include <net/if_types.h> 50 #include <net/ethernet.h> 51 #include <net/route.h> 52 #include <net/ifq_var.h> 53 54 #include <netproto/802_11/ieee80211_var.h> 55 #include <netproto/802_11/ieee80211_input.h> 56 57 SYSCTL_NODE(_net, OID_AUTO, wlan, CTLFLAG_RD, 0, "IEEE 80211 parameters"); 58 59 #ifdef IEEE80211_DEBUG 60 int ieee80211_debug = 0; 61 SYSCTL_INT(_net_wlan, OID_AUTO, debug, CTLFLAG_RW, &ieee80211_debug, 62 0, "debugging printfs"); 63 #endif 64 65 int ieee80211_force_swcrypto = 0; 66 SYSCTL_INT(_net_wlan, OID_AUTO, force_swcrypto, CTLFLAG_RW, 67 &ieee80211_force_swcrypto, 0, "force software crypto"); 68 69 MALLOC_DEFINE(M_80211_COM, "80211com", "802.11 com state"); 70 71 72 static int wlan_clone_destroy(struct ifnet *); 73 static int wlan_clone_create(struct if_clone *, int, caddr_t); 74 75 static struct if_clone wlan_cloner = 76 IF_CLONE_INITIALIZER("wlan", wlan_clone_create, wlan_clone_destroy, 77 0, IF_MAXUNIT); 78 79 struct lwkt_serialize wlan_global_serializer = LWKT_SERIALIZE_INITIALIZER; 80 81 /* 82 * Allocate/free com structure in conjunction with ifnet; 83 * these routines are registered with if_register_com_alloc 84 * below and are called automatically by the ifnet code 85 * when the ifnet of the parent device is created. 86 */ 87 static void * 88 wlan_alloc(u_char type, struct ifnet *ifp) 89 { 90 struct ieee80211com *ic; 91 92 ic = kmalloc(sizeof(struct ieee80211com), M_80211_COM, M_WAITOK|M_ZERO); 93 ic->ic_ifp = ifp; 94 95 return (ic); 96 } 97 98 static void 99 wlan_free(void *ic, u_char type) 100 { 101 kfree(ic, M_80211_COM); 102 } 103 104 static int 105 wlan_clone_create(struct if_clone *ifc, int unit, caddr_t params) 106 { 107 struct ieee80211_clone_params cp; 108 struct ieee80211vap *vap; 109 struct ieee80211com *ic; 110 struct ifnet *ifp; 111 int error; 112 113 error = copyin(params, &cp, sizeof(cp)); 114 if (error) 115 return error; 116 ifp = ifunit(cp.icp_parent); 117 if (ifp == NULL) 118 return ENXIO; 119 /* XXX move printfs to DIAGNOSTIC before release */ 120 if (ifp->if_type != IFT_IEEE80211) { 121 if_printf(ifp, "%s: reject, not an 802.11 device\n", __func__); 122 return ENXIO; 123 } 124 if (cp.icp_opmode >= IEEE80211_OPMODE_MAX) { 125 if_printf(ifp, "%s: invalid opmode %d\n", 126 __func__, cp.icp_opmode); 127 return EINVAL; 128 } 129 ic = ifp->if_l2com; 130 if ((ic->ic_caps & ieee80211_opcap[cp.icp_opmode]) == 0) { 131 if_printf(ifp, "%s mode not supported\n", 132 ieee80211_opmode_name[cp.icp_opmode]); 133 return EOPNOTSUPP; 134 } 135 if ((cp.icp_flags & IEEE80211_CLONE_TDMA) && 136 #ifdef IEEE80211_SUPPORT_TDMA 137 (ic->ic_caps & IEEE80211_C_TDMA) == 0 138 #else 139 (1) 140 #endif 141 ) { 142 if_printf(ifp, "TDMA not supported\n"); 143 return EOPNOTSUPP; 144 } 145 vap = ic->ic_vap_create(ic, ifc->ifc_name, unit, 146 cp.icp_opmode, cp.icp_flags, cp.icp_bssid, 147 cp.icp_flags & IEEE80211_CLONE_MACADDR ? 148 cp.icp_macaddr : (const uint8_t *)IF_LLADDR(ifp)); 149 return (vap == NULL ? EIO : 0); 150 } 151 152 static int 153 wlan_clone_destroy(struct ifnet *ifp) 154 { 155 struct ieee80211vap *vap = ifp->if_softc; 156 struct ieee80211com *ic = vap->iv_ic; 157 158 ic->ic_vap_delete(vap); 159 160 return 0; 161 } 162 163 const char *wlan_last_enter_func; 164 const char *wlan_last_exit_func; 165 166 /* 167 * These serializer functions are used by wlan and all drivers. 168 * They are not recursive. The serializer must be held on 169 * any OACTIVE interactions. Dragonfly automatically holds 170 * the serializer on most ifp->if_*() calls but calls made 171 * from wlan into ath might not. 172 */ 173 void 174 _wlan_serialize_enter(const char *funcname) 175 { 176 lwkt_serialize_enter(&wlan_global_serializer); 177 wlan_last_enter_func = funcname; 178 } 179 180 void 181 _wlan_serialize_exit(const char *funcname) 182 { 183 lwkt_serialize_exit(&wlan_global_serializer); 184 wlan_last_exit_func = funcname; 185 } 186 187 int 188 _wlan_is_serialized(void) 189 { 190 return (IS_SERIALIZED(&wlan_global_serializer)); 191 } 192 193 /* 194 * Push/pop allows the wlan serializer to be entered recursively. 195 */ 196 int 197 _wlan_serialize_push(const char *funcname) 198 { 199 if (IS_SERIALIZED(&wlan_global_serializer)) { 200 return 0; 201 } else { 202 _wlan_serialize_enter(funcname); 203 return 1; 204 } 205 } 206 207 void 208 _wlan_serialize_pop(const char *funcname, int wst) 209 { 210 if (wst) { 211 _wlan_serialize_exit(funcname); 212 } 213 } 214 215 #if 0 216 217 int 218 wlan_serialize_sleep(void *ident, int flags, const char *wmesg, int timo) 219 { 220 return(zsleep(ident, &wlan_global_serializer, flags, wmesg, timo)); 221 } 222 223 /* 224 * condition-var functions which interlock the ic lock (which is now 225 * just wlan_global_serializer) 226 */ 227 void 228 wlan_cv_init(struct cv *cv, const char *desc) 229 { 230 cv->cv_desc = desc; 231 cv->cv_waiters = 0; 232 } 233 234 int 235 wlan_cv_timedwait(struct cv *cv, int ticks) 236 { 237 int error; 238 239 ++cv->cv_waiters; 240 error = wlan_serialize_sleep(cv, 0, cv->cv_desc, ticks); 241 return (error); 242 } 243 244 void 245 wlan_cv_wait(struct cv *cv) 246 { 247 ++cv->cv_waiters; 248 wlan_serialize_sleep(cv, 0, cv->cv_desc, 0); 249 } 250 251 void 252 wlan_cv_signal(struct cv *cv, int broadcast) 253 { 254 if (cv->cv_waiters) { 255 if (broadcast) { 256 cv->cv_waiters = 0; 257 wakeup(cv); 258 } else { 259 --cv->cv_waiters; 260 wakeup_one(cv); 261 } 262 } 263 } 264 265 #endif 266 267 /* 268 * Misc 269 */ 270 int 271 ieee80211_vap_xmitpkt(struct ieee80211vap *vap, struct mbuf *m) 272 { 273 struct ifnet *ifp = vap->iv_ifp; 274 struct ifaltq_subque *ifsq = ifq_get_subq_default(&ifp->if_snd); 275 int error; 276 int wst; 277 278 /* 279 * When transmitting via the VAP, we shouldn't hold 280 * any IC TX lock as the VAP TX path will acquire it. 281 */ 282 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic); 283 284 error = ifsq_enqueue(ifsq, m, NULL); 285 wst = wlan_serialize_push(); 286 ifp->if_start(ifp, ifsq); 287 wlan_serialize_pop(wst); 288 289 return error; 290 } 291 292 int 293 ieee80211_parent_xmitpkt(struct ieee80211com *ic, struct mbuf *m) 294 { 295 struct ifnet *parent = ic->ic_ifp; 296 struct ifaltq_subque *ifsq = ifq_get_subq_default(&parent->if_snd); 297 int error; 298 int wst; 299 300 /* 301 * Assert the IC TX lock is held - this enforces the 302 * processing -> queuing order is maintained 303 */ 304 IEEE80211_TX_LOCK_ASSERT(ic); 305 306 error = ifsq_enqueue(ifsq, m, NULL); 307 wst = wlan_serialize_push(); 308 parent->if_start(parent, ifsq); 309 wlan_serialize_pop(wst); 310 311 return error; 312 } 313 314 void 315 ieee80211_vap_destroy(struct ieee80211vap *vap) 316 { 317 if_clone_destroy(vap->iv_ifp->if_xname); 318 } 319 320 /* 321 * NOTE: This handler is used generally to convert milliseconds 322 * to ticks for various simple sysctl variables and does not 323 * need to be serialized. 324 */ 325 int 326 ieee80211_sysctl_msecs_ticks(SYSCTL_HANDLER_ARGS) 327 { 328 int msecs = ticks_to_msecs(*(int *)arg1); 329 int error, t; 330 331 error = sysctl_handle_int(oidp, &msecs, 0, req); 332 if (error == 0 && req->newptr) { 333 t = msecs_to_ticks(msecs); 334 *(int *)arg1 = (t < 1) ? 1 : t; 335 } 336 337 return error; 338 } 339 340 static int 341 ieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS) 342 { 343 int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT; 344 int error; 345 346 error = sysctl_handle_int(oidp, &inact, 0, req); 347 if (error == 0 && req->newptr) 348 *(int *)arg1 = inact / IEEE80211_INACT_WAIT; 349 350 return error; 351 } 352 353 static int 354 ieee80211_sysctl_parent(SYSCTL_HANDLER_ARGS) 355 { 356 struct ieee80211com *ic = arg1; 357 const char *name = ic->ic_ifp->if_xname; 358 359 return SYSCTL_OUT(req, name, strlen(name)); 360 } 361 362 static int 363 ieee80211_sysctl_radar(SYSCTL_HANDLER_ARGS) 364 { 365 struct ieee80211com *ic = arg1; 366 int t = 0, error; 367 368 error = sysctl_handle_int(oidp, &t, 0, req); 369 if (error == 0 && req->newptr) 370 ieee80211_dfs_notify_radar(ic, ic->ic_curchan); 371 372 return error; 373 } 374 375 void 376 ieee80211_sysctl_attach(struct ieee80211com *ic) 377 { 378 } 379 380 void 381 ieee80211_sysctl_detach(struct ieee80211com *ic) 382 { 383 } 384 385 void 386 ieee80211_sysctl_vattach(struct ieee80211vap *vap) 387 { 388 struct ifnet *ifp = vap->iv_ifp; 389 struct sysctl_ctx_list *ctx; 390 struct sysctl_oid *oid; 391 char num[14]; /* sufficient for 32 bits */ 392 393 ctx = (struct sysctl_ctx_list *) kmalloc(sizeof(struct sysctl_ctx_list), 394 M_DEVBUF, M_INTWAIT | M_ZERO); 395 if (ctx == NULL) { 396 if_printf(ifp, "%s: cannot allocate sysctl context!\n", 397 __func__); 398 return; 399 } 400 sysctl_ctx_init(ctx); 401 ksnprintf(num, sizeof(num), "%u", ifp->if_dunit); 402 oid = SYSCTL_ADD_NODE(ctx, &SYSCTL_NODE_CHILDREN(_net, wlan), 403 OID_AUTO, num, CTLFLAG_RD, NULL, ""); 404 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 405 "%parent", CTLFLAG_RD, vap->iv_ic, 0, 406 ieee80211_sysctl_parent, "A", "parent device"); 407 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 408 "driver_caps", CTLFLAG_RW, &vap->iv_caps, 0, 409 "driver capabilities"); 410 #ifdef IEEE80211_DEBUG 411 vap->iv_debug = ieee80211_debug; 412 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 413 "debug", CTLFLAG_RW, &vap->iv_debug, 0, 414 "control debugging printfs"); 415 #endif 416 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 417 "bmiss_max", CTLFLAG_RW, &vap->iv_bmiss_max, 0, 418 "consecutive beacon misses before scanning"); 419 /* XXX inherit from tunables */ 420 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 421 "inact_run", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_run, 0, 422 ieee80211_sysctl_inact, "I", 423 "station inactivity timeout (sec)"); 424 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 425 "inact_probe", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_probe, 0, 426 ieee80211_sysctl_inact, "I", 427 "station inactivity probe timeout (sec)"); 428 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 429 "inact_auth", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_auth, 0, 430 ieee80211_sysctl_inact, "I", 431 "station authentication timeout (sec)"); 432 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 433 "inact_init", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_init, 0, 434 ieee80211_sysctl_inact, "I", 435 "station initial state timeout (sec)"); 436 if (vap->iv_htcaps & IEEE80211_HTC_HT) { 437 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 438 "ampdu_mintraffic_bk", CTLFLAG_RW, 439 &vap->iv_ampdu_mintraffic[WME_AC_BK], 0, 440 "BK traffic tx aggr threshold (pps)"); 441 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 442 "ampdu_mintraffic_be", CTLFLAG_RW, 443 &vap->iv_ampdu_mintraffic[WME_AC_BE], 0, 444 "BE traffic tx aggr threshold (pps)"); 445 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 446 "ampdu_mintraffic_vo", CTLFLAG_RW, 447 &vap->iv_ampdu_mintraffic[WME_AC_VO], 0, 448 "VO traffic tx aggr threshold (pps)"); 449 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 450 "ampdu_mintraffic_vi", CTLFLAG_RW, 451 &vap->iv_ampdu_mintraffic[WME_AC_VI], 0, 452 "VI traffic tx aggr threshold (pps)"); 453 } 454 if (vap->iv_caps & IEEE80211_C_DFS) { 455 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 456 "radar", CTLTYPE_INT | CTLFLAG_RW, vap->iv_ic, 0, 457 ieee80211_sysctl_radar, "I", "simulate radar event"); 458 } 459 vap->iv_sysctl = ctx; 460 vap->iv_oid = oid; 461 } 462 463 void 464 ieee80211_sysctl_vdetach(struct ieee80211vap *vap) 465 { 466 467 if (vap->iv_sysctl != NULL) { 468 sysctl_ctx_free(vap->iv_sysctl); 469 kfree(vap->iv_sysctl, M_DEVBUF); 470 vap->iv_sysctl = NULL; 471 } 472 } 473 474 int 475 ieee80211_node_dectestref(struct ieee80211_node *ni) 476 { 477 /* XXX need equivalent of atomic_dec_and_test */ 478 atomic_subtract_int(&ni->ni_refcnt, 1); 479 return atomic_cmpset_int(&ni->ni_refcnt, 0, 1); 480 } 481 482 #if 0 483 /* XXX this breaks ALTQ's packet scheduler */ 484 void 485 ieee80211_flush_ifq(struct ifaltq *ifq, struct ieee80211vap *vap) 486 { 487 struct ieee80211_node *ni; 488 struct mbuf *m, **mprev; 489 struct ifaltq_subque *ifsq = ifq_get_subq_default(ifq); 490 491 wlan_assert_serialized(); 492 493 ALTQ_SQ_LOCK(ifsq); 494 495 /* 496 * Fix normal queue 497 */ 498 mprev = &ifsq->ifsq_norm_head; 499 while ((m = *mprev) != NULL) { 500 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 501 if (ni != NULL && ni->ni_vap == vap) { 502 *mprev = m->m_nextpkt; /* remove from list */ 503 ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len); 504 505 m_freem(m); 506 ieee80211_free_node(ni); /* reclaim ref */ 507 } else 508 mprev = &m->m_nextpkt; 509 } 510 /* recalculate tail ptr */ 511 m = ifsq->ifsq_norm_head; 512 for (; m != NULL && m->m_nextpkt != NULL; m = m->m_nextpkt) 513 ; 514 ifsq->ifsq_norm_tail = m; 515 516 /* 517 * Fix priority queue 518 */ 519 mprev = &ifsq->ifsq_prio_head; 520 while ((m = *mprev) != NULL) { 521 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 522 if (ni != NULL && ni->ni_vap == vap) { 523 *mprev = m->m_nextpkt; /* remove from list */ 524 ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len); 525 ALTQ_SQ_PRIO_CNTR_DEC(ifsq, m->m_pkthdr.len); 526 527 m_freem(m); 528 ieee80211_free_node(ni); /* reclaim ref */ 529 } else 530 mprev = &m->m_nextpkt; 531 } 532 /* recalculate tail ptr */ 533 m = ifsq->ifsq_prio_head; 534 for (; m != NULL && m->m_nextpkt != NULL; m = m->m_nextpkt) 535 ; 536 ifsq->ifsq_prio_tail = m; 537 538 ALTQ_SQ_UNLOCK(ifsq); 539 } 540 #endif 541 542 /* 543 * As above, for mbufs allocated with m_gethdr/MGETHDR 544 * or initialized by M_COPY_PKTHDR. 545 */ 546 #define MC_ALIGN(m, len) \ 547 do { \ 548 (m)->m_data += (MCLBYTES - (len)) &~ (sizeof(long) - 1); \ 549 } while (/* CONSTCOND */ 0) 550 551 /* 552 * Allocate and setup a management frame of the specified 553 * size. We return the mbuf and a pointer to the start 554 * of the contiguous data area that's been reserved based 555 * on the packet length. The data area is forced to 32-bit 556 * alignment and the buffer length to a multiple of 4 bytes. 557 * This is done mainly so beacon frames (that require this) 558 * can use this interface too. 559 */ 560 struct mbuf * 561 ieee80211_getmgtframe(uint8_t **frm, int headroom, int pktlen) 562 { 563 struct mbuf *m; 564 u_int len; 565 566 /* 567 * NB: we know the mbuf routines will align the data area 568 * so we don't need to do anything special. 569 */ 570 len = roundup2(headroom + pktlen, 4); 571 KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len)); 572 if (len < MINCLSIZE) { 573 m = m_gethdr(MB_DONTWAIT, MT_DATA); 574 /* 575 * Align the data in case additional headers are added. 576 * This should only happen when a WEP header is added 577 * which only happens for shared key authentication mgt 578 * frames which all fit in MHLEN. 579 */ 580 if (m != NULL) 581 MH_ALIGN(m, len); 582 } else { 583 m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR); 584 if (m != NULL) 585 MC_ALIGN(m, len); 586 } 587 if (m != NULL) { 588 m->m_data += headroom; 589 *frm = m->m_data; 590 } 591 return m; 592 } 593 594 /* 595 * Re-align the payload in the mbuf. This is mainly used (right now) 596 * to handle IP header alignment requirements on certain architectures. 597 */ 598 struct mbuf * 599 ieee80211_realign(struct ieee80211vap *vap, struct mbuf *m, size_t align) 600 { 601 int pktlen, space; 602 struct mbuf *n = NULL; 603 604 pktlen = m->m_pkthdr.len; 605 space = pktlen + align; 606 if (space < MINCLSIZE) { 607 n = m_gethdr(MB_DONTWAIT, MT_DATA); 608 } else { 609 if (space <= MCLBYTES) 610 space = MCLBYTES; 611 else if (space <= MJUMPAGESIZE) 612 space = MJUMPAGESIZE; 613 else if (space <= MJUM9BYTES) 614 space = MJUM9BYTES; 615 else 616 space = MJUM16BYTES; 617 n = m_getjcl(MB_DONTWAIT, MT_DATA, M_PKTHDR, space); 618 } 619 if (__predict_true(n != NULL)) { 620 m_move_pkthdr(n, m); 621 n->m_data = (caddr_t)(ALIGN(n->m_data + align) - align); 622 m_copydata(m, 0, pktlen, mtod(n, caddr_t)); 623 n->m_len = pktlen; 624 } else { 625 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 626 mtod(m, const struct ieee80211_frame *), NULL, 627 "%s", "no mbuf to realign"); 628 vap->iv_stats.is_rx_badalign++; 629 } 630 m_freem(m); 631 return n; 632 } 633 634 int 635 ieee80211_add_callback(struct mbuf *m, 636 void (*func)(struct ieee80211_node *, void *, int), void *arg) 637 { 638 struct m_tag *mtag; 639 struct ieee80211_cb *cb; 640 641 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_CALLBACK, 642 sizeof(struct ieee80211_cb), M_INTWAIT); 643 if (mtag == NULL) 644 return 0; 645 646 cb = (struct ieee80211_cb *)(mtag+1); 647 cb->func = func; 648 cb->arg = arg; 649 m_tag_prepend(m, mtag); 650 m->m_flags |= M_TXCB; 651 return 1; 652 } 653 654 void 655 ieee80211_process_callback(struct ieee80211_node *ni, 656 struct mbuf *m, int status) 657 { 658 struct m_tag *mtag; 659 660 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_CALLBACK, NULL); 661 if (mtag != NULL) { 662 struct ieee80211_cb *cb = (struct ieee80211_cb *)(mtag+1); 663 cb->func(ni, cb->arg, status); 664 } 665 } 666 667 #include <sys/libkern.h> 668 669 void 670 get_random_bytes(void *p, size_t n) 671 { 672 uint8_t *dp = p; 673 674 while (n > 0) { 675 uint32_t v = karc4random(); 676 size_t nb = n > sizeof(uint32_t) ? sizeof(uint32_t) : n; 677 bcopy(&v, dp, n > sizeof(uint32_t) ? sizeof(uint32_t) : n); 678 dp += sizeof(uint32_t), n -= nb; 679 } 680 } 681 682 /* 683 * Helper function for events that pass just a single mac address. 684 */ 685 static void 686 notify_macaddr(struct ifnet *ifp, int op, const uint8_t mac[IEEE80211_ADDR_LEN]) 687 { 688 struct ieee80211_join_event iev; 689 690 memset(&iev, 0, sizeof(iev)); 691 IEEE80211_ADDR_COPY(iev.iev_addr, mac); 692 rt_ieee80211msg(ifp, op, &iev, sizeof(iev)); 693 } 694 695 void 696 ieee80211_notify_node_join(struct ieee80211_node *ni, int newassoc) 697 { 698 struct ieee80211vap *vap = ni->ni_vap; 699 struct ifnet *ifp = vap->iv_ifp; 700 701 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode join", 702 (ni == vap->iv_bss) ? "bss " : ""); 703 704 if (ni == vap->iv_bss) { 705 notify_macaddr(ifp, newassoc ? 706 RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC, ni->ni_bssid); 707 if_link_state_change(ifp); 708 } else { 709 notify_macaddr(ifp, newassoc ? 710 RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN, ni->ni_macaddr); 711 } 712 } 713 714 void 715 ieee80211_notify_node_leave(struct ieee80211_node *ni) 716 { 717 struct ieee80211vap *vap = ni->ni_vap; 718 struct ifnet *ifp = vap->iv_ifp; 719 720 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode leave", 721 (ni == vap->iv_bss) ? "bss " : ""); 722 723 if (ni == vap->iv_bss) { 724 rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0); 725 if_link_state_change(ifp); 726 } else { 727 /* fire off wireless event station leaving */ 728 notify_macaddr(ifp, RTM_IEEE80211_LEAVE, ni->ni_macaddr); 729 } 730 } 731 732 void 733 ieee80211_notify_scan_done(struct ieee80211vap *vap) 734 { 735 struct ifnet *ifp = vap->iv_ifp; 736 737 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n", "notify scan done"); 738 739 /* dispatch wireless event indicating scan completed */ 740 rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0); 741 } 742 743 void 744 ieee80211_notify_replay_failure(struct ieee80211vap *vap, 745 const struct ieee80211_frame *wh, const struct ieee80211_key *k, 746 u_int64_t rsc, int tid) 747 { 748 struct ifnet *ifp = vap->iv_ifp; 749 750 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2, 751 "%s replay detected <rsc %ju, csc %ju, keyix %u rxkeyix %u>", 752 k->wk_cipher->ic_name, (intmax_t) rsc, 753 (intmax_t) k->wk_keyrsc[tid], 754 k->wk_keyix, k->wk_rxkeyix); 755 756 if (ifp != NULL) { /* NB: for cipher test modules */ 757 struct ieee80211_replay_event iev; 758 759 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1); 760 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2); 761 iev.iev_cipher = k->wk_cipher->ic_cipher; 762 if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE) 763 iev.iev_keyix = k->wk_rxkeyix; 764 else 765 iev.iev_keyix = k->wk_keyix; 766 iev.iev_keyrsc = k->wk_keyrsc[tid]; 767 iev.iev_rsc = rsc; 768 rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev)); 769 } 770 } 771 772 void 773 ieee80211_notify_michael_failure(struct ieee80211vap *vap, 774 const struct ieee80211_frame *wh, u_int keyix) 775 { 776 struct ifnet *ifp = vap->iv_ifp; 777 778 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2, 779 "michael MIC verification failed <keyix %u>", keyix); 780 vap->iv_stats.is_rx_tkipmic++; 781 782 if (ifp != NULL) { /* NB: for cipher test modules */ 783 struct ieee80211_michael_event iev; 784 785 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1); 786 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2); 787 iev.iev_cipher = IEEE80211_CIPHER_TKIP; 788 iev.iev_keyix = keyix; 789 rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev)); 790 } 791 } 792 793 void 794 ieee80211_notify_wds_discover(struct ieee80211_node *ni) 795 { 796 struct ieee80211vap *vap = ni->ni_vap; 797 struct ifnet *ifp = vap->iv_ifp; 798 799 notify_macaddr(ifp, RTM_IEEE80211_WDS, ni->ni_macaddr); 800 } 801 802 void 803 ieee80211_notify_csa(struct ieee80211com *ic, 804 const struct ieee80211_channel *c, int mode, int count) 805 { 806 struct ifnet *ifp = ic->ic_ifp; 807 struct ieee80211_csa_event iev; 808 809 memset(&iev, 0, sizeof(iev)); 810 iev.iev_flags = c->ic_flags; 811 iev.iev_freq = c->ic_freq; 812 iev.iev_ieee = c->ic_ieee; 813 iev.iev_mode = mode; 814 iev.iev_count = count; 815 rt_ieee80211msg(ifp, RTM_IEEE80211_CSA, &iev, sizeof(iev)); 816 } 817 818 void 819 ieee80211_notify_radar(struct ieee80211com *ic, 820 const struct ieee80211_channel *c) 821 { 822 struct ifnet *ifp = ic->ic_ifp; 823 struct ieee80211_radar_event iev; 824 825 memset(&iev, 0, sizeof(iev)); 826 iev.iev_flags = c->ic_flags; 827 iev.iev_freq = c->ic_freq; 828 iev.iev_ieee = c->ic_ieee; 829 rt_ieee80211msg(ifp, RTM_IEEE80211_RADAR, &iev, sizeof(iev)); 830 } 831 832 void 833 ieee80211_notify_cac(struct ieee80211com *ic, 834 const struct ieee80211_channel *c, enum ieee80211_notify_cac_event type) 835 { 836 struct ifnet *ifp = ic->ic_ifp; 837 struct ieee80211_cac_event iev; 838 839 memset(&iev, 0, sizeof(iev)); 840 iev.iev_flags = c->ic_flags; 841 iev.iev_freq = c->ic_freq; 842 iev.iev_ieee = c->ic_ieee; 843 iev.iev_type = type; 844 rt_ieee80211msg(ifp, RTM_IEEE80211_CAC, &iev, sizeof(iev)); 845 } 846 847 void 848 ieee80211_notify_node_deauth(struct ieee80211_node *ni) 849 { 850 struct ieee80211vap *vap = ni->ni_vap; 851 struct ifnet *ifp = vap->iv_ifp; 852 853 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node deauth"); 854 855 notify_macaddr(ifp, RTM_IEEE80211_DEAUTH, ni->ni_macaddr); 856 } 857 858 void 859 ieee80211_notify_node_auth(struct ieee80211_node *ni) 860 { 861 struct ieee80211vap *vap = ni->ni_vap; 862 struct ifnet *ifp = vap->iv_ifp; 863 864 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node auth"); 865 866 notify_macaddr(ifp, RTM_IEEE80211_AUTH, ni->ni_macaddr); 867 } 868 869 void 870 ieee80211_notify_country(struct ieee80211vap *vap, 871 const uint8_t bssid[IEEE80211_ADDR_LEN], const uint8_t cc[2]) 872 { 873 struct ifnet *ifp = vap->iv_ifp; 874 struct ieee80211_country_event iev; 875 876 memset(&iev, 0, sizeof(iev)); 877 IEEE80211_ADDR_COPY(iev.iev_addr, bssid); 878 iev.iev_cc[0] = cc[0]; 879 iev.iev_cc[1] = cc[1]; 880 rt_ieee80211msg(ifp, RTM_IEEE80211_COUNTRY, &iev, sizeof(iev)); 881 } 882 883 void 884 ieee80211_notify_radio(struct ieee80211com *ic, int state) 885 { 886 struct ifnet *ifp = ic->ic_ifp; 887 struct ieee80211_radio_event iev; 888 889 memset(&iev, 0, sizeof(iev)); 890 iev.iev_state = state; 891 rt_ieee80211msg(ifp, RTM_IEEE80211_RADIO, &iev, sizeof(iev)); 892 } 893 894 /* IEEE Std 802.11a-1999, page 9, table 79 */ 895 #define IEEE80211_OFDM_SYM_TIME 4 896 #define IEEE80211_OFDM_PREAMBLE_TIME 16 897 #define IEEE80211_OFDM_SIGNAL_TIME 4 898 /* IEEE Std 802.11g-2003, page 44 */ 899 #define IEEE80211_OFDM_SIGNAL_EXT_TIME 6 900 901 /* IEEE Std 802.11a-1999, page 7, figure 107 */ 902 #define IEEE80211_OFDM_PLCP_SERVICE_NBITS 16 903 #define IEEE80211_OFDM_TAIL_NBITS 6 904 905 #define IEEE80211_OFDM_NBITS(frmlen) \ 906 (IEEE80211_OFDM_PLCP_SERVICE_NBITS + \ 907 ((frmlen) * NBBY) + \ 908 IEEE80211_OFDM_TAIL_NBITS) 909 910 #define IEEE80211_OFDM_NBITS_PER_SYM(kbps) \ 911 (((kbps) * IEEE80211_OFDM_SYM_TIME) / 1000) 912 913 #define IEEE80211_OFDM_NSYMS(kbps, frmlen) \ 914 howmany(IEEE80211_OFDM_NBITS((frmlen)), \ 915 IEEE80211_OFDM_NBITS_PER_SYM((kbps))) 916 917 #define IEEE80211_OFDM_TXTIME(kbps, frmlen) \ 918 (IEEE80211_OFDM_PREAMBLE_TIME + \ 919 IEEE80211_OFDM_SIGNAL_TIME + \ 920 (IEEE80211_OFDM_NSYMS((kbps), (frmlen)) * IEEE80211_OFDM_SYM_TIME)) 921 922 /* IEEE Std 802.11b-1999, page 28, subclause 18.3.4 */ 923 #define IEEE80211_CCK_PREAMBLE_LEN 144 924 #define IEEE80211_CCK_PLCP_HDR_TIME 48 925 #define IEEE80211_CCK_SHPREAMBLE_LEN 72 926 #define IEEE80211_CCK_SHPLCP_HDR_TIME 24 927 928 #define IEEE80211_CCK_NBITS(frmlen) ((frmlen) * NBBY) 929 #define IEEE80211_CCK_TXTIME(kbps, frmlen) \ 930 (((IEEE80211_CCK_NBITS((frmlen)) * 1000) + (kbps) - 1) / (kbps)) 931 932 uint16_t 933 ieee80211_txtime(struct ieee80211_node *ni, u_int len, uint8_t rs_rate, 934 uint32_t flags) 935 { 936 struct ieee80211vap *vap = ni->ni_vap; 937 uint16_t txtime; 938 int rate; 939 940 rs_rate &= IEEE80211_RATE_VAL; 941 rate = rs_rate * 500; /* ieee80211 rate -> kbps */ 942 943 if (vap->iv_ic->ic_phytype == IEEE80211_T_OFDM) { 944 /* 945 * IEEE Std 802.11a-1999, page 37, equation (29) 946 * IEEE Std 802.11g-2003, page 44, equation (42) 947 */ 948 txtime = IEEE80211_OFDM_TXTIME(rate, len); 949 if (vap->iv_ic->ic_curmode == IEEE80211_MODE_11G) 950 txtime += IEEE80211_OFDM_SIGNAL_EXT_TIME; 951 } else { 952 /* 953 * IEEE Std 802.11b-1999, page 28, subclause 18.3.4 954 * IEEE Std 802.11g-2003, page 45, equation (43) 955 */ 956 if (vap->iv_ic->ic_phytype == IEEE80211_T_OFDM_QUARTER+1) 957 ++len; 958 txtime = IEEE80211_CCK_TXTIME(rate, len); 959 960 /* 961 * Short preamble is not applicable for DS 1Mbits/s 962 */ 963 if (rs_rate != 2 && (flags & IEEE80211_F_SHPREAMBLE)) { 964 txtime += IEEE80211_CCK_SHPREAMBLE_LEN + 965 IEEE80211_CCK_SHPLCP_HDR_TIME; 966 } else { 967 txtime += IEEE80211_CCK_PREAMBLE_LEN + 968 IEEE80211_CCK_PLCP_HDR_TIME; 969 } 970 } 971 return txtime; 972 } 973 974 void 975 ieee80211_load_module(const char *modname) 976 { 977 978 #ifdef notyet 979 (void)kern_kldload(curthread, modname, NULL); 980 #else 981 kprintf("%s: load the %s module by hand for now.\n", __func__, modname); 982 #endif 983 } 984 985 static eventhandler_tag wlan_bpfevent; 986 static eventhandler_tag wlan_ifllevent; 987 988 static void 989 bpf_track_event(void *arg, struct ifnet *ifp, int dlt, int attach) 990 { 991 /* NB: identify vap's by if_start */ 992 993 if (dlt == DLT_IEEE802_11_RADIO && 994 ifp->if_start == ieee80211_vap_start) { 995 struct ieee80211vap *vap = ifp->if_softc; 996 /* 997 * Track bpf radiotap listener state. We mark the vap 998 * to indicate if any listener is present and the com 999 * to indicate if any listener exists on any associated 1000 * vap. This flag is used by drivers to prepare radiotap 1001 * state only when needed. 1002 */ 1003 if (attach) { 1004 ieee80211_syncflag_ext(vap, IEEE80211_FEXT_BPF); 1005 if (vap->iv_opmode == IEEE80211_M_MONITOR) 1006 atomic_add_int(&vap->iv_ic->ic_montaps, 1); 1007 } else if (!vap->iv_rawbpf) { 1008 ieee80211_syncflag_ext(vap, -IEEE80211_FEXT_BPF); 1009 if (vap->iv_opmode == IEEE80211_M_MONITOR) 1010 atomic_subtract_int(&vap->iv_ic->ic_montaps, 1); 1011 } 1012 } 1013 } 1014 1015 const char * 1016 ether_sprintf(const u_char *buf) 1017 { 1018 static char ethstr[MAXCPU][ETHER_ADDRSTRLEN + 1]; 1019 char *ptr = ethstr[mycpu->gd_cpuid]; 1020 1021 kether_ntoa(buf, ptr); 1022 return (ptr); 1023 } 1024 1025 static void 1026 wlan_iflladdr_event(void *arg __unused, struct ifnet *ifp) 1027 { 1028 struct ieee80211com *ic = ifp->if_l2com; 1029 struct ieee80211vap *vap, *next; 1030 1031 if (ifp->if_type != IFT_IEEE80211 || ic == NULL) { 1032 return; 1033 } 1034 1035 TAILQ_FOREACH_MUTABLE(vap, &ic->ic_vaps, iv_next, next) { 1036 /* 1037 * If the MAC address has changed on the parent and it was 1038 * copied to the vap on creation then re-sync. 1039 */ 1040 if (vap->iv_ic == ic && 1041 (vap->iv_flags_ext & IEEE80211_FEXT_UNIQMAC) == 0) { 1042 IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp)); 1043 if_setlladdr(vap->iv_ifp, IF_LLADDR(ifp), 1044 IEEE80211_ADDR_LEN); 1045 } 1046 } 1047 } 1048 1049 /* 1050 * Module glue. 1051 * 1052 * NB: the module name is "wlan" for compatibility with NetBSD. 1053 */ 1054 static int 1055 wlan_modevent(module_t mod, int type, void *unused) 1056 { 1057 int error; 1058 1059 switch (type) { 1060 case MOD_LOAD: 1061 if (bootverbose) 1062 kprintf("wlan: <802.11 Link Layer>\n"); 1063 wlan_bpfevent = EVENTHANDLER_REGISTER(bpf_track, 1064 bpf_track_event, 0, 1065 EVENTHANDLER_PRI_ANY); 1066 if (wlan_bpfevent == NULL) { 1067 error = ENOMEM; 1068 break; 1069 } 1070 wlan_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event, 1071 wlan_iflladdr_event, NULL, 1072 EVENTHANDLER_PRI_ANY); 1073 if (wlan_ifllevent == NULL) { 1074 EVENTHANDLER_DEREGISTER(bpf_track, wlan_bpfevent); 1075 error = ENOMEM; 1076 break; 1077 } 1078 if_clone_attach(&wlan_cloner); 1079 if_register_com_alloc(IFT_IEEE80211, wlan_alloc, wlan_free); 1080 error = 0; 1081 break; 1082 case MOD_UNLOAD: 1083 if_deregister_com_alloc(IFT_IEEE80211); 1084 if_clone_detach(&wlan_cloner); 1085 EVENTHANDLER_DEREGISTER(bpf_track, wlan_bpfevent); 1086 EVENTHANDLER_DEREGISTER(iflladdr_event, wlan_ifllevent); 1087 error = 0; 1088 break; 1089 default: 1090 error = EINVAL; 1091 break; 1092 } 1093 return error; 1094 } 1095 1096 static moduledata_t wlan_mod = { 1097 "wlan", 1098 wlan_modevent, 1099 0 1100 }; 1101 DECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 1102 MODULE_VERSION(wlan, 1); 1103 MODULE_DEPEND(wlan, ether, 1, 1, 1); 1104