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