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