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