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