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 #ifdef notyet 609 else { 610 n = m_getjcl(MB_DONTWAIT, MT_DATA, M_PKTHDR, 611 space <= MCLBYTES ? MCLBYTES : 612 #if MJUMPAGESIZE != MCLBYTES 613 space <= MJUMPAGESIZE ? MJUMPAGESIZE : 614 #endif 615 space <= MJUM9BYTES ? MJUM9BYTES : MJUM16BYTES); 616 } 617 #endif 618 if (__predict_true(n != NULL)) { 619 m_move_pkthdr(n, m); 620 n->m_data = (caddr_t)(ALIGN(n->m_data + align) - align); 621 m_copydata(m, 0, pktlen, mtod(n, caddr_t)); 622 n->m_len = pktlen; 623 } else { 624 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 625 mtod(m, const struct ieee80211_frame *), NULL, 626 "%s", "no mbuf to realign"); 627 vap->iv_stats.is_rx_badalign++; 628 } 629 m_freem(m); 630 return n; 631 } 632 633 int 634 ieee80211_add_callback(struct mbuf *m, 635 void (*func)(struct ieee80211_node *, void *, int), void *arg) 636 { 637 struct m_tag *mtag; 638 struct ieee80211_cb *cb; 639 640 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_CALLBACK, 641 sizeof(struct ieee80211_cb), M_INTWAIT); 642 if (mtag == NULL) 643 return 0; 644 645 cb = (struct ieee80211_cb *)(mtag+1); 646 cb->func = func; 647 cb->arg = arg; 648 m_tag_prepend(m, mtag); 649 m->m_flags |= M_TXCB; 650 return 1; 651 } 652 653 void 654 ieee80211_process_callback(struct ieee80211_node *ni, 655 struct mbuf *m, int status) 656 { 657 struct m_tag *mtag; 658 659 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_CALLBACK, NULL); 660 if (mtag != NULL) { 661 struct ieee80211_cb *cb = (struct ieee80211_cb *)(mtag+1); 662 cb->func(ni, cb->arg, status); 663 } 664 } 665 666 #include <sys/libkern.h> 667 668 void 669 get_random_bytes(void *p, size_t n) 670 { 671 uint8_t *dp = p; 672 673 while (n > 0) { 674 uint32_t v = karc4random(); 675 size_t nb = n > sizeof(uint32_t) ? sizeof(uint32_t) : n; 676 bcopy(&v, dp, n > sizeof(uint32_t) ? sizeof(uint32_t) : n); 677 dp += sizeof(uint32_t), n -= nb; 678 } 679 } 680 681 /* 682 * Helper function for events that pass just a single mac address. 683 */ 684 static void 685 notify_macaddr(struct ifnet *ifp, int op, const uint8_t mac[IEEE80211_ADDR_LEN]) 686 { 687 struct ieee80211_join_event iev; 688 689 memset(&iev, 0, sizeof(iev)); 690 IEEE80211_ADDR_COPY(iev.iev_addr, mac); 691 rt_ieee80211msg(ifp, op, &iev, sizeof(iev)); 692 } 693 694 void 695 ieee80211_notify_node_join(struct ieee80211_node *ni, int newassoc) 696 { 697 struct ieee80211vap *vap = ni->ni_vap; 698 struct ifnet *ifp = vap->iv_ifp; 699 700 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode join", 701 (ni == vap->iv_bss) ? "bss " : ""); 702 703 if (ni == vap->iv_bss) { 704 notify_macaddr(ifp, newassoc ? 705 RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC, ni->ni_bssid); 706 if_link_state_change(ifp); 707 } else { 708 notify_macaddr(ifp, newassoc ? 709 RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN, ni->ni_macaddr); 710 } 711 } 712 713 void 714 ieee80211_notify_node_leave(struct ieee80211_node *ni) 715 { 716 struct ieee80211vap *vap = ni->ni_vap; 717 struct ifnet *ifp = vap->iv_ifp; 718 719 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode leave", 720 (ni == vap->iv_bss) ? "bss " : ""); 721 722 if (ni == vap->iv_bss) { 723 rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0); 724 if_link_state_change(ifp); 725 } else { 726 /* fire off wireless event station leaving */ 727 notify_macaddr(ifp, RTM_IEEE80211_LEAVE, ni->ni_macaddr); 728 } 729 } 730 731 void 732 ieee80211_notify_scan_done(struct ieee80211vap *vap) 733 { 734 struct ifnet *ifp = vap->iv_ifp; 735 736 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n", "notify scan done"); 737 738 /* dispatch wireless event indicating scan completed */ 739 rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0); 740 } 741 742 void 743 ieee80211_notify_replay_failure(struct ieee80211vap *vap, 744 const struct ieee80211_frame *wh, const struct ieee80211_key *k, 745 u_int64_t rsc, int tid) 746 { 747 struct ifnet *ifp = vap->iv_ifp; 748 749 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2, 750 "%s replay detected <rsc %ju, csc %ju, keyix %u rxkeyix %u>", 751 k->wk_cipher->ic_name, (intmax_t) rsc, 752 (intmax_t) k->wk_keyrsc[tid], 753 k->wk_keyix, k->wk_rxkeyix); 754 755 if (ifp != NULL) { /* NB: for cipher test modules */ 756 struct ieee80211_replay_event iev; 757 758 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1); 759 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2); 760 iev.iev_cipher = k->wk_cipher->ic_cipher; 761 if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE) 762 iev.iev_keyix = k->wk_rxkeyix; 763 else 764 iev.iev_keyix = k->wk_keyix; 765 iev.iev_keyrsc = k->wk_keyrsc[tid]; 766 iev.iev_rsc = rsc; 767 rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev)); 768 } 769 } 770 771 void 772 ieee80211_notify_michael_failure(struct ieee80211vap *vap, 773 const struct ieee80211_frame *wh, u_int keyix) 774 { 775 struct ifnet *ifp = vap->iv_ifp; 776 777 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2, 778 "michael MIC verification failed <keyix %u>", keyix); 779 vap->iv_stats.is_rx_tkipmic++; 780 781 if (ifp != NULL) { /* NB: for cipher test modules */ 782 struct ieee80211_michael_event iev; 783 784 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1); 785 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2); 786 iev.iev_cipher = IEEE80211_CIPHER_TKIP; 787 iev.iev_keyix = keyix; 788 rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev)); 789 } 790 } 791 792 void 793 ieee80211_notify_wds_discover(struct ieee80211_node *ni) 794 { 795 struct ieee80211vap *vap = ni->ni_vap; 796 struct ifnet *ifp = vap->iv_ifp; 797 798 notify_macaddr(ifp, RTM_IEEE80211_WDS, ni->ni_macaddr); 799 } 800 801 void 802 ieee80211_notify_csa(struct ieee80211com *ic, 803 const struct ieee80211_channel *c, int mode, int count) 804 { 805 struct ifnet *ifp = ic->ic_ifp; 806 struct ieee80211_csa_event iev; 807 808 memset(&iev, 0, sizeof(iev)); 809 iev.iev_flags = c->ic_flags; 810 iev.iev_freq = c->ic_freq; 811 iev.iev_ieee = c->ic_ieee; 812 iev.iev_mode = mode; 813 iev.iev_count = count; 814 rt_ieee80211msg(ifp, RTM_IEEE80211_CSA, &iev, sizeof(iev)); 815 } 816 817 void 818 ieee80211_notify_radar(struct ieee80211com *ic, 819 const struct ieee80211_channel *c) 820 { 821 struct ifnet *ifp = ic->ic_ifp; 822 struct ieee80211_radar_event iev; 823 824 memset(&iev, 0, sizeof(iev)); 825 iev.iev_flags = c->ic_flags; 826 iev.iev_freq = c->ic_freq; 827 iev.iev_ieee = c->ic_ieee; 828 rt_ieee80211msg(ifp, RTM_IEEE80211_RADAR, &iev, sizeof(iev)); 829 } 830 831 void 832 ieee80211_notify_cac(struct ieee80211com *ic, 833 const struct ieee80211_channel *c, enum ieee80211_notify_cac_event type) 834 { 835 struct ifnet *ifp = ic->ic_ifp; 836 struct ieee80211_cac_event iev; 837 838 memset(&iev, 0, sizeof(iev)); 839 iev.iev_flags = c->ic_flags; 840 iev.iev_freq = c->ic_freq; 841 iev.iev_ieee = c->ic_ieee; 842 iev.iev_type = type; 843 rt_ieee80211msg(ifp, RTM_IEEE80211_CAC, &iev, sizeof(iev)); 844 } 845 846 void 847 ieee80211_notify_node_deauth(struct ieee80211_node *ni) 848 { 849 struct ieee80211vap *vap = ni->ni_vap; 850 struct ifnet *ifp = vap->iv_ifp; 851 852 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node deauth"); 853 854 notify_macaddr(ifp, RTM_IEEE80211_DEAUTH, ni->ni_macaddr); 855 } 856 857 void 858 ieee80211_notify_node_auth(struct ieee80211_node *ni) 859 { 860 struct ieee80211vap *vap = ni->ni_vap; 861 struct ifnet *ifp = vap->iv_ifp; 862 863 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node auth"); 864 865 notify_macaddr(ifp, RTM_IEEE80211_AUTH, ni->ni_macaddr); 866 } 867 868 void 869 ieee80211_notify_country(struct ieee80211vap *vap, 870 const uint8_t bssid[IEEE80211_ADDR_LEN], const uint8_t cc[2]) 871 { 872 struct ifnet *ifp = vap->iv_ifp; 873 struct ieee80211_country_event iev; 874 875 memset(&iev, 0, sizeof(iev)); 876 IEEE80211_ADDR_COPY(iev.iev_addr, bssid); 877 iev.iev_cc[0] = cc[0]; 878 iev.iev_cc[1] = cc[1]; 879 rt_ieee80211msg(ifp, RTM_IEEE80211_COUNTRY, &iev, sizeof(iev)); 880 } 881 882 void 883 ieee80211_notify_radio(struct ieee80211com *ic, int state) 884 { 885 struct ifnet *ifp = ic->ic_ifp; 886 struct ieee80211_radio_event iev; 887 888 memset(&iev, 0, sizeof(iev)); 889 iev.iev_state = state; 890 rt_ieee80211msg(ifp, RTM_IEEE80211_RADIO, &iev, sizeof(iev)); 891 } 892 893 /* IEEE Std 802.11a-1999, page 9, table 79 */ 894 #define IEEE80211_OFDM_SYM_TIME 4 895 #define IEEE80211_OFDM_PREAMBLE_TIME 16 896 #define IEEE80211_OFDM_SIGNAL_TIME 4 897 /* IEEE Std 802.11g-2003, page 44 */ 898 #define IEEE80211_OFDM_SIGNAL_EXT_TIME 6 899 900 /* IEEE Std 802.11a-1999, page 7, figure 107 */ 901 #define IEEE80211_OFDM_PLCP_SERVICE_NBITS 16 902 #define IEEE80211_OFDM_TAIL_NBITS 6 903 904 #define IEEE80211_OFDM_NBITS(frmlen) \ 905 (IEEE80211_OFDM_PLCP_SERVICE_NBITS + \ 906 ((frmlen) * NBBY) + \ 907 IEEE80211_OFDM_TAIL_NBITS) 908 909 #define IEEE80211_OFDM_NBITS_PER_SYM(kbps) \ 910 (((kbps) * IEEE80211_OFDM_SYM_TIME) / 1000) 911 912 #define IEEE80211_OFDM_NSYMS(kbps, frmlen) \ 913 howmany(IEEE80211_OFDM_NBITS((frmlen)), \ 914 IEEE80211_OFDM_NBITS_PER_SYM((kbps))) 915 916 #define IEEE80211_OFDM_TXTIME(kbps, frmlen) \ 917 (IEEE80211_OFDM_PREAMBLE_TIME + \ 918 IEEE80211_OFDM_SIGNAL_TIME + \ 919 (IEEE80211_OFDM_NSYMS((kbps), (frmlen)) * IEEE80211_OFDM_SYM_TIME)) 920 921 /* IEEE Std 802.11b-1999, page 28, subclause 18.3.4 */ 922 #define IEEE80211_CCK_PREAMBLE_LEN 144 923 #define IEEE80211_CCK_PLCP_HDR_TIME 48 924 #define IEEE80211_CCK_SHPREAMBLE_LEN 72 925 #define IEEE80211_CCK_SHPLCP_HDR_TIME 24 926 927 #define IEEE80211_CCK_NBITS(frmlen) ((frmlen) * NBBY) 928 #define IEEE80211_CCK_TXTIME(kbps, frmlen) \ 929 (((IEEE80211_CCK_NBITS((frmlen)) * 1000) + (kbps) - 1) / (kbps)) 930 931 uint16_t 932 ieee80211_txtime(struct ieee80211_node *ni, u_int len, uint8_t rs_rate, 933 uint32_t flags) 934 { 935 struct ieee80211vap *vap = ni->ni_vap; 936 uint16_t txtime; 937 int rate; 938 939 rs_rate &= IEEE80211_RATE_VAL; 940 rate = rs_rate * 500; /* ieee80211 rate -> kbps */ 941 942 if (vap->iv_ic->ic_phytype == IEEE80211_T_OFDM) { 943 /* 944 * IEEE Std 802.11a-1999, page 37, equation (29) 945 * IEEE Std 802.11g-2003, page 44, equation (42) 946 */ 947 txtime = IEEE80211_OFDM_TXTIME(rate, len); 948 if (vap->iv_ic->ic_curmode == IEEE80211_MODE_11G) 949 txtime += IEEE80211_OFDM_SIGNAL_EXT_TIME; 950 } else { 951 /* 952 * IEEE Std 802.11b-1999, page 28, subclause 18.3.4 953 * IEEE Std 802.11g-2003, page 45, equation (43) 954 */ 955 if (vap->iv_ic->ic_phytype == IEEE80211_T_OFDM_QUARTER+1) 956 ++len; 957 txtime = IEEE80211_CCK_TXTIME(rate, len); 958 959 /* 960 * Short preamble is not applicable for DS 1Mbits/s 961 */ 962 if (rs_rate != 2 && (flags & IEEE80211_F_SHPREAMBLE)) { 963 txtime += IEEE80211_CCK_SHPREAMBLE_LEN + 964 IEEE80211_CCK_SHPLCP_HDR_TIME; 965 } else { 966 txtime += IEEE80211_CCK_PREAMBLE_LEN + 967 IEEE80211_CCK_PLCP_HDR_TIME; 968 } 969 } 970 return txtime; 971 } 972 973 void 974 ieee80211_load_module(const char *modname) 975 { 976 977 #ifdef notyet 978 (void)kern_kldload(curthread, modname, NULL); 979 #else 980 kprintf("%s: load the %s module by hand for now.\n", __func__, modname); 981 #endif 982 } 983 984 static eventhandler_tag wlan_bpfevent; 985 static eventhandler_tag wlan_ifllevent; 986 987 static void 988 bpf_track_event(void *arg, struct ifnet *ifp, int dlt, int attach) 989 { 990 /* NB: identify vap's by if_start */ 991 992 if (dlt == DLT_IEEE802_11_RADIO && 993 ifp->if_start == ieee80211_vap_start) { 994 struct ieee80211vap *vap = ifp->if_softc; 995 /* 996 * Track bpf radiotap listener state. We mark the vap 997 * to indicate if any listener is present and the com 998 * to indicate if any listener exists on any associated 999 * vap. This flag is used by drivers to prepare radiotap 1000 * state only when needed. 1001 */ 1002 if (attach) { 1003 ieee80211_syncflag_ext(vap, IEEE80211_FEXT_BPF); 1004 if (vap->iv_opmode == IEEE80211_M_MONITOR) 1005 atomic_add_int(&vap->iv_ic->ic_montaps, 1); 1006 } else if (!vap->iv_rawbpf) { 1007 ieee80211_syncflag_ext(vap, -IEEE80211_FEXT_BPF); 1008 if (vap->iv_opmode == IEEE80211_M_MONITOR) 1009 atomic_subtract_int(&vap->iv_ic->ic_montaps, 1); 1010 } 1011 } 1012 } 1013 1014 const char * 1015 ether_sprintf(const u_char *buf) 1016 { 1017 static char ethstr[MAXCPU][ETHER_ADDRSTRLEN + 1]; 1018 char *ptr = ethstr[mycpu->gd_cpuid]; 1019 1020 kether_ntoa(buf, ptr); 1021 return (ptr); 1022 } 1023 1024 static void 1025 wlan_iflladdr_event(void *arg __unused, struct ifnet *ifp) 1026 { 1027 struct ieee80211com *ic = ifp->if_l2com; 1028 struct ieee80211vap *vap, *next; 1029 1030 if (ifp->if_type != IFT_IEEE80211 || ic == NULL) { 1031 return; 1032 } 1033 1034 TAILQ_FOREACH_MUTABLE(vap, &ic->ic_vaps, iv_next, next) { 1035 /* 1036 * If the MAC address has changed on the parent and it was 1037 * copied to the vap on creation then re-sync. 1038 */ 1039 if (vap->iv_ic == ic && 1040 (vap->iv_flags_ext & IEEE80211_FEXT_UNIQMAC) == 0) { 1041 IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp)); 1042 if_setlladdr(vap->iv_ifp, IF_LLADDR(ifp), 1043 IEEE80211_ADDR_LEN); 1044 } 1045 } 1046 } 1047 1048 /* 1049 * Module glue. 1050 * 1051 * NB: the module name is "wlan" for compatibility with NetBSD. 1052 */ 1053 static int 1054 wlan_modevent(module_t mod, int type, void *unused) 1055 { 1056 int error; 1057 1058 switch (type) { 1059 case MOD_LOAD: 1060 if (bootverbose) 1061 kprintf("wlan: <802.11 Link Layer>\n"); 1062 wlan_bpfevent = EVENTHANDLER_REGISTER(bpf_track, 1063 bpf_track_event, 0, 1064 EVENTHANDLER_PRI_ANY); 1065 if (wlan_bpfevent == NULL) { 1066 error = ENOMEM; 1067 break; 1068 } 1069 wlan_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event, 1070 wlan_iflladdr_event, NULL, 1071 EVENTHANDLER_PRI_ANY); 1072 if (wlan_ifllevent == NULL) { 1073 EVENTHANDLER_DEREGISTER(bpf_track, wlan_bpfevent); 1074 error = ENOMEM; 1075 break; 1076 } 1077 if_clone_attach(&wlan_cloner); 1078 if_register_com_alloc(IFT_IEEE80211, wlan_alloc, wlan_free); 1079 error = 0; 1080 break; 1081 case MOD_UNLOAD: 1082 if_deregister_com_alloc(IFT_IEEE80211); 1083 if_clone_detach(&wlan_cloner); 1084 EVENTHANDLER_DEREGISTER(bpf_track, wlan_bpfevent); 1085 EVENTHANDLER_DEREGISTER(iflladdr_event, wlan_ifllevent); 1086 error = 0; 1087 break; 1088 default: 1089 error = EINVAL; 1090 break; 1091 } 1092 return error; 1093 } 1094 1095 static moduledata_t wlan_mod = { 1096 "wlan", 1097 wlan_modevent, 1098 0 1099 }; 1100 DECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 1101 MODULE_VERSION(wlan, 1); 1102 MODULE_DEPEND(wlan, ether, 1, 1, 1); 1103