1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2003-2009 Sam Leffler, Errno Consulting 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 /* 32 * IEEE 802.11 support (FreeBSD-specific code) 33 */ 34 #include "opt_wlan.h" 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/eventhandler.h> 39 #include <sys/kernel.h> 40 #include <sys/linker.h> 41 #include <sys/malloc.h> 42 #include <sys/mbuf.h> 43 #include <sys/module.h> 44 #include <sys/priv.h> 45 #include <sys/proc.h> 46 #include <sys/sysctl.h> 47 48 #include <sys/socket.h> 49 50 #include <net/bpf.h> 51 #include <net/debugnet.h> 52 #include <net/if.h> 53 #include <net/if_var.h> 54 #include <net/if_dl.h> 55 #include <net/if_clone.h> 56 #include <net/if_media.h> 57 #include <net/if_types.h> 58 #include <net/ethernet.h> 59 #include <net/route.h> 60 #include <net/vnet.h> 61 62 #include <net80211/ieee80211_var.h> 63 #include <net80211/ieee80211_input.h> 64 65 DEBUGNET_DEFINE(ieee80211); 66 SYSCTL_NODE(_net, OID_AUTO, wlan, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 67 "IEEE 80211 parameters"); 68 69 #ifdef IEEE80211_DEBUG 70 static int ieee80211_debug = 0; 71 SYSCTL_INT(_net_wlan, OID_AUTO, debug, CTLFLAG_RW, &ieee80211_debug, 72 0, "debugging printfs"); 73 #endif 74 75 static const char wlanname[] = "wlan"; 76 static struct if_clone *wlan_cloner; 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 = priv_check(curthread, PRIV_NET80211_CREATE_VAP); 87 if (error) 88 return error; 89 90 error = copyin(params, &cp, sizeof(cp)); 91 if (error) 92 return error; 93 ic = ieee80211_find_com(cp.icp_parent); 94 if (ic == NULL) 95 return ENXIO; 96 if (cp.icp_opmode >= IEEE80211_OPMODE_MAX) { 97 ic_printf(ic, "%s: invalid opmode %d\n", __func__, 98 cp.icp_opmode); 99 return EINVAL; 100 } 101 if ((ic->ic_caps & ieee80211_opcap[cp.icp_opmode]) == 0) { 102 ic_printf(ic, "%s mode not supported\n", 103 ieee80211_opmode_name[cp.icp_opmode]); 104 return EOPNOTSUPP; 105 } 106 if ((cp.icp_flags & IEEE80211_CLONE_TDMA) && 107 #ifdef IEEE80211_SUPPORT_TDMA 108 (ic->ic_caps & IEEE80211_C_TDMA) == 0 109 #else 110 (1) 111 #endif 112 ) { 113 ic_printf(ic, "TDMA not supported\n"); 114 return EOPNOTSUPP; 115 } 116 vap = ic->ic_vap_create(ic, wlanname, unit, 117 cp.icp_opmode, cp.icp_flags, cp.icp_bssid, 118 cp.icp_flags & IEEE80211_CLONE_MACADDR ? 119 cp.icp_macaddr : ic->ic_macaddr); 120 121 if (vap == NULL) 122 return (EIO); 123 124 #ifdef DEBUGNET 125 if (ic->ic_debugnet_meth != NULL) 126 DEBUGNET_SET(vap->iv_ifp, ieee80211); 127 #endif 128 return (0); 129 } 130 131 static void 132 wlan_clone_destroy(struct ifnet *ifp) 133 { 134 struct ieee80211vap *vap = ifp->if_softc; 135 struct ieee80211com *ic = vap->iv_ic; 136 137 ic->ic_vap_delete(vap); 138 } 139 140 void 141 ieee80211_vap_destroy(struct ieee80211vap *vap) 142 { 143 CURVNET_SET(vap->iv_ifp->if_vnet); 144 if_clone_destroyif(wlan_cloner, vap->iv_ifp); 145 CURVNET_RESTORE(); 146 } 147 148 int 149 ieee80211_sysctl_msecs_ticks(SYSCTL_HANDLER_ARGS) 150 { 151 int msecs = ticks_to_msecs(*(int *)arg1); 152 int error; 153 154 error = sysctl_handle_int(oidp, &msecs, 0, req); 155 if (error || !req->newptr) 156 return error; 157 *(int *)arg1 = msecs_to_ticks(msecs); 158 return 0; 159 } 160 161 static int 162 ieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS) 163 { 164 int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT; 165 int error; 166 167 error = sysctl_handle_int(oidp, &inact, 0, req); 168 if (error || !req->newptr) 169 return error; 170 *(int *)arg1 = inact / IEEE80211_INACT_WAIT; 171 return 0; 172 } 173 174 static int 175 ieee80211_sysctl_parent(SYSCTL_HANDLER_ARGS) 176 { 177 struct ieee80211com *ic = arg1; 178 179 return SYSCTL_OUT_STR(req, ic->ic_name); 180 } 181 182 static int 183 ieee80211_sysctl_radar(SYSCTL_HANDLER_ARGS) 184 { 185 struct ieee80211com *ic = arg1; 186 int t = 0, error; 187 188 error = sysctl_handle_int(oidp, &t, 0, req); 189 if (error || !req->newptr) 190 return error; 191 IEEE80211_LOCK(ic); 192 ieee80211_dfs_notify_radar(ic, ic->ic_curchan); 193 IEEE80211_UNLOCK(ic); 194 return 0; 195 } 196 197 /* 198 * For now, just restart everything. 199 * 200 * Later on, it'd be nice to have a separate VAP restart to 201 * full-device restart. 202 */ 203 static int 204 ieee80211_sysctl_vap_restart(SYSCTL_HANDLER_ARGS) 205 { 206 struct ieee80211vap *vap = arg1; 207 int t = 0, error; 208 209 error = sysctl_handle_int(oidp, &t, 0, req); 210 if (error || !req->newptr) 211 return error; 212 213 ieee80211_restart_all(vap->iv_ic); 214 return 0; 215 } 216 217 void 218 ieee80211_sysctl_attach(struct ieee80211com *ic) 219 { 220 } 221 222 void 223 ieee80211_sysctl_detach(struct ieee80211com *ic) 224 { 225 } 226 227 void 228 ieee80211_sysctl_vattach(struct ieee80211vap *vap) 229 { 230 struct ifnet *ifp = vap->iv_ifp; 231 struct sysctl_ctx_list *ctx; 232 struct sysctl_oid *oid; 233 char num[14]; /* sufficient for 32 bits */ 234 235 ctx = (struct sysctl_ctx_list *) IEEE80211_MALLOC(sizeof(struct sysctl_ctx_list), 236 M_DEVBUF, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 237 if (ctx == NULL) { 238 if_printf(ifp, "%s: cannot allocate sysctl context!\n", 239 __func__); 240 return; 241 } 242 sysctl_ctx_init(ctx); 243 snprintf(num, sizeof(num), "%u", ifp->if_dunit); 244 oid = SYSCTL_ADD_NODE(ctx, &SYSCTL_NODE_CHILDREN(_net, wlan), 245 OID_AUTO, num, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 246 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 247 "%parent", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 248 vap->iv_ic, 0, ieee80211_sysctl_parent, "A", "parent device"); 249 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 250 "driver_caps", CTLFLAG_RW, &vap->iv_caps, 0, 251 "driver capabilities"); 252 #ifdef IEEE80211_DEBUG 253 vap->iv_debug = ieee80211_debug; 254 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 255 "debug", CTLFLAG_RW, &vap->iv_debug, 0, 256 "control debugging printfs"); 257 #endif 258 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 259 "bmiss_max", CTLFLAG_RW, &vap->iv_bmiss_max, 0, 260 "consecutive beacon misses before scanning"); 261 /* XXX inherit from tunables */ 262 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 263 "inact_run", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 264 &vap->iv_inact_run, 0, ieee80211_sysctl_inact, "I", 265 "station inactivity timeout (sec)"); 266 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 267 "inact_probe", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 268 &vap->iv_inact_probe, 0, ieee80211_sysctl_inact, "I", 269 "station inactivity probe timeout (sec)"); 270 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 271 "inact_auth", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 272 &vap->iv_inact_auth, 0, ieee80211_sysctl_inact, "I", 273 "station authentication timeout (sec)"); 274 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 275 "inact_init", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 276 &vap->iv_inact_init, 0, ieee80211_sysctl_inact, "I", 277 "station initial state timeout (sec)"); 278 if (vap->iv_htcaps & IEEE80211_HTC_HT) { 279 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 280 "ampdu_mintraffic_bk", CTLFLAG_RW, 281 &vap->iv_ampdu_mintraffic[WME_AC_BK], 0, 282 "BK traffic tx aggr threshold (pps)"); 283 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 284 "ampdu_mintraffic_be", CTLFLAG_RW, 285 &vap->iv_ampdu_mintraffic[WME_AC_BE], 0, 286 "BE traffic tx aggr threshold (pps)"); 287 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 288 "ampdu_mintraffic_vo", CTLFLAG_RW, 289 &vap->iv_ampdu_mintraffic[WME_AC_VO], 0, 290 "VO traffic tx aggr threshold (pps)"); 291 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 292 "ampdu_mintraffic_vi", CTLFLAG_RW, 293 &vap->iv_ampdu_mintraffic[WME_AC_VI], 0, 294 "VI traffic tx aggr threshold (pps)"); 295 } 296 297 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 298 "force_restart", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 299 vap, 0, ieee80211_sysctl_vap_restart, "I", "force a VAP restart"); 300 301 if (vap->iv_caps & IEEE80211_C_DFS) { 302 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 303 "radar", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 304 vap->iv_ic, 0, ieee80211_sysctl_radar, "I", 305 "simulate radar event"); 306 } 307 vap->iv_sysctl = ctx; 308 vap->iv_oid = oid; 309 } 310 311 void 312 ieee80211_sysctl_vdetach(struct ieee80211vap *vap) 313 { 314 315 if (vap->iv_sysctl != NULL) { 316 sysctl_ctx_free(vap->iv_sysctl); 317 IEEE80211_FREE(vap->iv_sysctl, M_DEVBUF); 318 vap->iv_sysctl = NULL; 319 } 320 } 321 322 int 323 ieee80211_com_vincref(struct ieee80211vap *vap) 324 { 325 uint32_t ostate; 326 327 ostate = atomic_fetchadd_32(&vap->iv_com_state, IEEE80211_COM_REF_ADD); 328 329 if (ostate & IEEE80211_COM_DETACHED) { 330 atomic_subtract_32(&vap->iv_com_state, IEEE80211_COM_REF_ADD); 331 return (ENETDOWN); 332 } 333 334 if (_IEEE80211_MASKSHIFT(ostate, IEEE80211_COM_REF) == 335 IEEE80211_COM_REF_MAX) { 336 atomic_subtract_32(&vap->iv_com_state, IEEE80211_COM_REF_ADD); 337 return (EOVERFLOW); 338 } 339 340 return (0); 341 } 342 343 void 344 ieee80211_com_vdecref(struct ieee80211vap *vap) 345 { 346 uint32_t ostate; 347 348 ostate = atomic_fetchadd_32(&vap->iv_com_state, -IEEE80211_COM_REF_ADD); 349 350 KASSERT(_IEEE80211_MASKSHIFT(ostate, IEEE80211_COM_REF) != 0, 351 ("com reference counter underflow")); 352 353 (void) ostate; 354 } 355 356 void 357 ieee80211_com_vdetach(struct ieee80211vap *vap) 358 { 359 int sleep_time; 360 361 sleep_time = msecs_to_ticks(250); 362 atomic_set_32(&vap->iv_com_state, IEEE80211_COM_DETACHED); 363 while (_IEEE80211_MASKSHIFT(atomic_load_32(&vap->iv_com_state), 364 IEEE80211_COM_REF) != 0) 365 pause("comref", sleep_time); 366 } 367 368 int 369 ieee80211_node_dectestref(struct ieee80211_node *ni) 370 { 371 /* XXX need equivalent of atomic_dec_and_test */ 372 atomic_subtract_int(&ni->ni_refcnt, 1); 373 return atomic_cmpset_int(&ni->ni_refcnt, 0, 1); 374 } 375 376 void 377 ieee80211_drain_ifq(struct ifqueue *ifq) 378 { 379 struct ieee80211_node *ni; 380 struct mbuf *m; 381 382 for (;;) { 383 IF_DEQUEUE(ifq, m); 384 if (m == NULL) 385 break; 386 387 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 388 KASSERT(ni != NULL, ("frame w/o node")); 389 ieee80211_free_node(ni); 390 m->m_pkthdr.rcvif = NULL; 391 392 m_freem(m); 393 } 394 } 395 396 void 397 ieee80211_flush_ifq(struct ifqueue *ifq, struct ieee80211vap *vap) 398 { 399 struct ieee80211_node *ni; 400 struct mbuf *m, **mprev; 401 402 IF_LOCK(ifq); 403 mprev = &ifq->ifq_head; 404 while ((m = *mprev) != NULL) { 405 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 406 if (ni != NULL && ni->ni_vap == vap) { 407 *mprev = m->m_nextpkt; /* remove from list */ 408 ifq->ifq_len--; 409 410 m_freem(m); 411 ieee80211_free_node(ni); /* reclaim ref */ 412 } else 413 mprev = &m->m_nextpkt; 414 } 415 /* recalculate tail ptr */ 416 m = ifq->ifq_head; 417 for (; m != NULL && m->m_nextpkt != NULL; m = m->m_nextpkt) 418 ; 419 ifq->ifq_tail = m; 420 IF_UNLOCK(ifq); 421 } 422 423 /* 424 * As above, for mbufs allocated with m_gethdr/MGETHDR 425 * or initialized by M_COPY_PKTHDR. 426 */ 427 #define MC_ALIGN(m, len) \ 428 do { \ 429 (m)->m_data += rounddown2(MCLBYTES - (len), sizeof(long)); \ 430 } while (/* CONSTCOND */ 0) 431 432 /* 433 * Allocate and setup a management frame of the specified 434 * size. We return the mbuf and a pointer to the start 435 * of the contiguous data area that's been reserved based 436 * on the packet length. The data area is forced to 32-bit 437 * alignment and the buffer length to a multiple of 4 bytes. 438 * This is done mainly so beacon frames (that require this) 439 * can use this interface too. 440 */ 441 struct mbuf * 442 ieee80211_getmgtframe(uint8_t **frm, int headroom, int pktlen) 443 { 444 struct mbuf *m; 445 u_int len; 446 447 /* 448 * NB: we know the mbuf routines will align the data area 449 * so we don't need to do anything special. 450 */ 451 len = roundup2(headroom + pktlen, 4); 452 KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len)); 453 if (len < MINCLSIZE) { 454 m = m_gethdr(M_NOWAIT, MT_DATA); 455 /* 456 * Align the data in case additional headers are added. 457 * This should only happen when a WEP header is added 458 * which only happens for shared key authentication mgt 459 * frames which all fit in MHLEN. 460 */ 461 if (m != NULL) 462 M_ALIGN(m, len); 463 } else { 464 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 465 if (m != NULL) 466 MC_ALIGN(m, len); 467 } 468 if (m != NULL) { 469 m->m_data += headroom; 470 *frm = m->m_data; 471 } 472 return m; 473 } 474 475 #ifndef __NO_STRICT_ALIGNMENT 476 /* 477 * Re-align the payload in the mbuf. This is mainly used (right now) 478 * to handle IP header alignment requirements on certain architectures. 479 */ 480 struct mbuf * 481 ieee80211_realign(struct ieee80211vap *vap, struct mbuf *m, size_t align) 482 { 483 int pktlen, space; 484 struct mbuf *n; 485 486 pktlen = m->m_pkthdr.len; 487 space = pktlen + align; 488 if (space < MINCLSIZE) 489 n = m_gethdr(M_NOWAIT, MT_DATA); 490 else { 491 n = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, 492 space <= MCLBYTES ? MCLBYTES : 493 #if MJUMPAGESIZE != MCLBYTES 494 space <= MJUMPAGESIZE ? MJUMPAGESIZE : 495 #endif 496 space <= MJUM9BYTES ? MJUM9BYTES : MJUM16BYTES); 497 } 498 if (__predict_true(n != NULL)) { 499 m_move_pkthdr(n, m); 500 n->m_data = (caddr_t)(ALIGN(n->m_data + align) - align); 501 m_copydata(m, 0, pktlen, mtod(n, caddr_t)); 502 n->m_len = pktlen; 503 } else { 504 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 505 mtod(m, const struct ieee80211_frame *), NULL, 506 "%s", "no mbuf to realign"); 507 vap->iv_stats.is_rx_badalign++; 508 } 509 m_freem(m); 510 return n; 511 } 512 #endif /* !__NO_STRICT_ALIGNMENT */ 513 514 int 515 ieee80211_add_callback(struct mbuf *m, 516 void (*func)(struct ieee80211_node *, void *, int), void *arg) 517 { 518 struct m_tag *mtag; 519 struct ieee80211_cb *cb; 520 521 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_CALLBACK, 522 sizeof(struct ieee80211_cb), M_NOWAIT); 523 if (mtag == NULL) 524 return 0; 525 526 cb = (struct ieee80211_cb *)(mtag+1); 527 cb->func = func; 528 cb->arg = arg; 529 m_tag_prepend(m, mtag); 530 m->m_flags |= M_TXCB; 531 return 1; 532 } 533 534 int 535 ieee80211_add_xmit_params(struct mbuf *m, 536 const struct ieee80211_bpf_params *params) 537 { 538 struct m_tag *mtag; 539 struct ieee80211_tx_params *tx; 540 541 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_XMIT_PARAMS, 542 sizeof(struct ieee80211_tx_params), M_NOWAIT); 543 if (mtag == NULL) 544 return (0); 545 546 tx = (struct ieee80211_tx_params *)(mtag+1); 547 memcpy(&tx->params, params, sizeof(struct ieee80211_bpf_params)); 548 m_tag_prepend(m, mtag); 549 return (1); 550 } 551 552 int 553 ieee80211_get_xmit_params(struct mbuf *m, 554 struct ieee80211_bpf_params *params) 555 { 556 struct m_tag *mtag; 557 struct ieee80211_tx_params *tx; 558 559 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_XMIT_PARAMS, 560 NULL); 561 if (mtag == NULL) 562 return (-1); 563 tx = (struct ieee80211_tx_params *)(mtag + 1); 564 memcpy(params, &tx->params, sizeof(struct ieee80211_bpf_params)); 565 return (0); 566 } 567 568 void 569 ieee80211_process_callback(struct ieee80211_node *ni, 570 struct mbuf *m, int status) 571 { 572 struct m_tag *mtag; 573 574 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_CALLBACK, NULL); 575 if (mtag != NULL) { 576 struct ieee80211_cb *cb = (struct ieee80211_cb *)(mtag+1); 577 cb->func(ni, cb->arg, status); 578 } 579 } 580 581 /* 582 * Add RX parameters to the given mbuf. 583 * 584 * Returns 1 if OK, 0 on error. 585 */ 586 int 587 ieee80211_add_rx_params(struct mbuf *m, const struct ieee80211_rx_stats *rxs) 588 { 589 struct m_tag *mtag; 590 struct ieee80211_rx_params *rx; 591 592 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_RECV_PARAMS, 593 sizeof(struct ieee80211_rx_stats), M_NOWAIT); 594 if (mtag == NULL) 595 return (0); 596 597 rx = (struct ieee80211_rx_params *)(mtag + 1); 598 memcpy(&rx->params, rxs, sizeof(*rxs)); 599 m_tag_prepend(m, mtag); 600 return (1); 601 } 602 603 int 604 ieee80211_get_rx_params(struct mbuf *m, struct ieee80211_rx_stats *rxs) 605 { 606 struct m_tag *mtag; 607 struct ieee80211_rx_params *rx; 608 609 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_RECV_PARAMS, 610 NULL); 611 if (mtag == NULL) 612 return (-1); 613 rx = (struct ieee80211_rx_params *)(mtag + 1); 614 memcpy(rxs, &rx->params, sizeof(*rxs)); 615 return (0); 616 } 617 618 const struct ieee80211_rx_stats * 619 ieee80211_get_rx_params_ptr(struct mbuf *m) 620 { 621 struct m_tag *mtag; 622 struct ieee80211_rx_params *rx; 623 624 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_RECV_PARAMS, 625 NULL); 626 if (mtag == NULL) 627 return (NULL); 628 rx = (struct ieee80211_rx_params *)(mtag + 1); 629 return (&rx->params); 630 } 631 632 /* 633 * Add TOA parameters to the given mbuf. 634 */ 635 int 636 ieee80211_add_toa_params(struct mbuf *m, const struct ieee80211_toa_params *p) 637 { 638 struct m_tag *mtag; 639 struct ieee80211_toa_params *rp; 640 641 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_TOA_PARAMS, 642 sizeof(struct ieee80211_toa_params), M_NOWAIT); 643 if (mtag == NULL) 644 return (0); 645 646 rp = (struct ieee80211_toa_params *)(mtag + 1); 647 memcpy(rp, p, sizeof(*rp)); 648 m_tag_prepend(m, mtag); 649 return (1); 650 } 651 652 int 653 ieee80211_get_toa_params(struct mbuf *m, struct ieee80211_toa_params *p) 654 { 655 struct m_tag *mtag; 656 struct ieee80211_toa_params *rp; 657 658 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_TOA_PARAMS, 659 NULL); 660 if (mtag == NULL) 661 return (0); 662 rp = (struct ieee80211_toa_params *)(mtag + 1); 663 if (p != NULL) 664 memcpy(p, rp, sizeof(*p)); 665 return (1); 666 } 667 668 /* 669 * Transmit a frame to the parent interface. 670 */ 671 int 672 ieee80211_parent_xmitpkt(struct ieee80211com *ic, struct mbuf *m) 673 { 674 int error; 675 676 /* 677 * Assert the IC TX lock is held - this enforces the 678 * processing -> queuing order is maintained 679 */ 680 IEEE80211_TX_LOCK_ASSERT(ic); 681 error = ic->ic_transmit(ic, m); 682 if (error) { 683 struct ieee80211_node *ni; 684 685 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 686 687 /* XXX number of fragments */ 688 if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 1); 689 ieee80211_free_node(ni); 690 ieee80211_free_mbuf(m); 691 } 692 return (error); 693 } 694 695 /* 696 * Transmit a frame to the VAP interface. 697 */ 698 int 699 ieee80211_vap_xmitpkt(struct ieee80211vap *vap, struct mbuf *m) 700 { 701 struct ifnet *ifp = vap->iv_ifp; 702 703 /* 704 * When transmitting via the VAP, we shouldn't hold 705 * any IC TX lock as the VAP TX path will acquire it. 706 */ 707 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic); 708 709 return (ifp->if_transmit(ifp, m)); 710 711 } 712 713 #include <sys/libkern.h> 714 715 void 716 get_random_bytes(void *p, size_t n) 717 { 718 uint8_t *dp = p; 719 720 while (n > 0) { 721 uint32_t v = arc4random(); 722 size_t nb = n > sizeof(uint32_t) ? sizeof(uint32_t) : n; 723 bcopy(&v, dp, n > sizeof(uint32_t) ? sizeof(uint32_t) : n); 724 dp += sizeof(uint32_t), n -= nb; 725 } 726 } 727 728 /* 729 * Helper function for events that pass just a single mac address. 730 */ 731 static void 732 notify_macaddr(struct ifnet *ifp, int op, const uint8_t mac[IEEE80211_ADDR_LEN]) 733 { 734 struct ieee80211_join_event iev; 735 736 CURVNET_SET(ifp->if_vnet); 737 memset(&iev, 0, sizeof(iev)); 738 IEEE80211_ADDR_COPY(iev.iev_addr, mac); 739 rt_ieee80211msg(ifp, op, &iev, sizeof(iev)); 740 CURVNET_RESTORE(); 741 } 742 743 void 744 ieee80211_notify_node_join(struct ieee80211_node *ni, int newassoc) 745 { 746 struct ieee80211vap *vap = ni->ni_vap; 747 struct ifnet *ifp = vap->iv_ifp; 748 749 CURVNET_SET_QUIET(ifp->if_vnet); 750 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode join", 751 (ni == vap->iv_bss) ? "bss " : ""); 752 753 if (ni == vap->iv_bss) { 754 notify_macaddr(ifp, newassoc ? 755 RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC, ni->ni_bssid); 756 if_link_state_change(ifp, LINK_STATE_UP); 757 } else { 758 notify_macaddr(ifp, newassoc ? 759 RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN, ni->ni_macaddr); 760 } 761 CURVNET_RESTORE(); 762 } 763 764 void 765 ieee80211_notify_node_leave(struct ieee80211_node *ni) 766 { 767 struct ieee80211vap *vap = ni->ni_vap; 768 struct ifnet *ifp = vap->iv_ifp; 769 770 CURVNET_SET_QUIET(ifp->if_vnet); 771 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode leave", 772 (ni == vap->iv_bss) ? "bss " : ""); 773 774 if (ni == vap->iv_bss) { 775 rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0); 776 if_link_state_change(ifp, LINK_STATE_DOWN); 777 } else { 778 /* fire off wireless event station leaving */ 779 notify_macaddr(ifp, RTM_IEEE80211_LEAVE, ni->ni_macaddr); 780 } 781 CURVNET_RESTORE(); 782 } 783 784 void 785 ieee80211_notify_scan_done(struct ieee80211vap *vap) 786 { 787 struct ifnet *ifp = vap->iv_ifp; 788 789 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n", "notify scan done"); 790 791 /* dispatch wireless event indicating scan completed */ 792 CURVNET_SET(ifp->if_vnet); 793 rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0); 794 CURVNET_RESTORE(); 795 } 796 797 void 798 ieee80211_notify_replay_failure(struct ieee80211vap *vap, 799 const struct ieee80211_frame *wh, const struct ieee80211_key *k, 800 u_int64_t rsc, int tid) 801 { 802 struct ifnet *ifp = vap->iv_ifp; 803 804 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2, 805 "%s replay detected tid %d <rsc %ju (%jx), csc %ju (%jx), keyix %u rxkeyix %u>", 806 k->wk_cipher->ic_name, tid, 807 (intmax_t) rsc, 808 (intmax_t) rsc, 809 (intmax_t) k->wk_keyrsc[tid], 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 CURVNET_SET(ifp->if_vnet); 826 rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev)); 827 CURVNET_RESTORE(); 828 } 829 } 830 831 void 832 ieee80211_notify_michael_failure(struct ieee80211vap *vap, 833 const struct ieee80211_frame *wh, u_int keyix) 834 { 835 struct ifnet *ifp = vap->iv_ifp; 836 837 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2, 838 "michael MIC verification failed <keyix %u>", keyix); 839 vap->iv_stats.is_rx_tkipmic++; 840 841 if (ifp != NULL) { /* NB: for cipher test modules */ 842 struct ieee80211_michael_event iev; 843 844 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1); 845 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2); 846 iev.iev_cipher = IEEE80211_CIPHER_TKIP; 847 iev.iev_keyix = keyix; 848 CURVNET_SET(ifp->if_vnet); 849 rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev)); 850 CURVNET_RESTORE(); 851 } 852 } 853 854 void 855 ieee80211_notify_wds_discover(struct ieee80211_node *ni) 856 { 857 struct ieee80211vap *vap = ni->ni_vap; 858 struct ifnet *ifp = vap->iv_ifp; 859 860 notify_macaddr(ifp, RTM_IEEE80211_WDS, ni->ni_macaddr); 861 } 862 863 void 864 ieee80211_notify_csa(struct ieee80211com *ic, 865 const struct ieee80211_channel *c, int mode, int count) 866 { 867 struct ieee80211_csa_event iev; 868 struct ieee80211vap *vap; 869 struct ifnet *ifp; 870 871 memset(&iev, 0, sizeof(iev)); 872 iev.iev_flags = c->ic_flags; 873 iev.iev_freq = c->ic_freq; 874 iev.iev_ieee = c->ic_ieee; 875 iev.iev_mode = mode; 876 iev.iev_count = count; 877 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 878 ifp = vap->iv_ifp; 879 CURVNET_SET(ifp->if_vnet); 880 rt_ieee80211msg(ifp, RTM_IEEE80211_CSA, &iev, sizeof(iev)); 881 CURVNET_RESTORE(); 882 } 883 } 884 885 void 886 ieee80211_notify_radar(struct ieee80211com *ic, 887 const struct ieee80211_channel *c) 888 { 889 struct ieee80211_radar_event iev; 890 struct ieee80211vap *vap; 891 struct ifnet *ifp; 892 893 memset(&iev, 0, sizeof(iev)); 894 iev.iev_flags = c->ic_flags; 895 iev.iev_freq = c->ic_freq; 896 iev.iev_ieee = c->ic_ieee; 897 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 898 ifp = vap->iv_ifp; 899 CURVNET_SET(ifp->if_vnet); 900 rt_ieee80211msg(ifp, RTM_IEEE80211_RADAR, &iev, sizeof(iev)); 901 CURVNET_RESTORE(); 902 } 903 } 904 905 void 906 ieee80211_notify_cac(struct ieee80211com *ic, 907 const struct ieee80211_channel *c, enum ieee80211_notify_cac_event type) 908 { 909 struct ieee80211_cac_event iev; 910 struct ieee80211vap *vap; 911 struct ifnet *ifp; 912 913 memset(&iev, 0, sizeof(iev)); 914 iev.iev_flags = c->ic_flags; 915 iev.iev_freq = c->ic_freq; 916 iev.iev_ieee = c->ic_ieee; 917 iev.iev_type = type; 918 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 919 ifp = vap->iv_ifp; 920 CURVNET_SET(ifp->if_vnet); 921 rt_ieee80211msg(ifp, RTM_IEEE80211_CAC, &iev, sizeof(iev)); 922 CURVNET_RESTORE(); 923 } 924 } 925 926 void 927 ieee80211_notify_node_deauth(struct ieee80211_node *ni) 928 { 929 struct ieee80211vap *vap = ni->ni_vap; 930 struct ifnet *ifp = vap->iv_ifp; 931 932 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node deauth"); 933 934 notify_macaddr(ifp, RTM_IEEE80211_DEAUTH, ni->ni_macaddr); 935 } 936 937 void 938 ieee80211_notify_node_auth(struct ieee80211_node *ni) 939 { 940 struct ieee80211vap *vap = ni->ni_vap; 941 struct ifnet *ifp = vap->iv_ifp; 942 943 IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node auth"); 944 945 notify_macaddr(ifp, RTM_IEEE80211_AUTH, ni->ni_macaddr); 946 } 947 948 void 949 ieee80211_notify_country(struct ieee80211vap *vap, 950 const uint8_t bssid[IEEE80211_ADDR_LEN], const uint8_t cc[2]) 951 { 952 struct ifnet *ifp = vap->iv_ifp; 953 struct ieee80211_country_event iev; 954 955 memset(&iev, 0, sizeof(iev)); 956 IEEE80211_ADDR_COPY(iev.iev_addr, bssid); 957 iev.iev_cc[0] = cc[0]; 958 iev.iev_cc[1] = cc[1]; 959 CURVNET_SET(ifp->if_vnet); 960 rt_ieee80211msg(ifp, RTM_IEEE80211_COUNTRY, &iev, sizeof(iev)); 961 CURVNET_RESTORE(); 962 } 963 964 void 965 ieee80211_notify_radio(struct ieee80211com *ic, int state) 966 { 967 struct ieee80211_radio_event iev; 968 struct ieee80211vap *vap; 969 struct ifnet *ifp; 970 971 memset(&iev, 0, sizeof(iev)); 972 iev.iev_state = state; 973 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 974 ifp = vap->iv_ifp; 975 CURVNET_SET(ifp->if_vnet); 976 rt_ieee80211msg(ifp, RTM_IEEE80211_RADIO, &iev, sizeof(iev)); 977 CURVNET_RESTORE(); 978 } 979 } 980 981 void 982 ieee80211_notify_ifnet_change(struct ieee80211vap *vap) 983 { 984 struct ifnet *ifp = vap->iv_ifp; 985 986 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG, "%s\n", 987 "interface state change"); 988 989 CURVNET_SET(ifp->if_vnet); 990 rt_ifmsg(ifp); 991 CURVNET_RESTORE(); 992 } 993 994 void 995 ieee80211_load_module(const char *modname) 996 { 997 998 #ifdef notyet 999 (void)kern_kldload(curthread, modname, NULL); 1000 #else 1001 printf("%s: load the %s module by hand for now.\n", __func__, modname); 1002 #endif 1003 } 1004 1005 static eventhandler_tag wlan_bpfevent; 1006 static eventhandler_tag wlan_ifllevent; 1007 1008 static void 1009 bpf_track(void *arg, struct ifnet *ifp, int dlt, int attach) 1010 { 1011 /* NB: identify vap's by if_init */ 1012 if (dlt == DLT_IEEE802_11_RADIO && 1013 ifp->if_init == ieee80211_init) { 1014 struct ieee80211vap *vap = ifp->if_softc; 1015 /* 1016 * Track bpf radiotap listener state. We mark the vap 1017 * to indicate if any listener is present and the com 1018 * to indicate if any listener exists on any associated 1019 * vap. This flag is used by drivers to prepare radiotap 1020 * state only when needed. 1021 */ 1022 if (attach) { 1023 ieee80211_syncflag_ext(vap, IEEE80211_FEXT_BPF); 1024 if (vap->iv_opmode == IEEE80211_M_MONITOR) 1025 atomic_add_int(&vap->iv_ic->ic_montaps, 1); 1026 } else if (!bpf_peers_present(vap->iv_rawbpf)) { 1027 ieee80211_syncflag_ext(vap, -IEEE80211_FEXT_BPF); 1028 if (vap->iv_opmode == IEEE80211_M_MONITOR) 1029 atomic_subtract_int(&vap->iv_ic->ic_montaps, 1); 1030 } 1031 } 1032 } 1033 1034 /* 1035 * Change MAC address on the vap (if was not started). 1036 */ 1037 static void 1038 wlan_iflladdr(void *arg __unused, struct ifnet *ifp) 1039 { 1040 /* NB: identify vap's by if_init */ 1041 if (ifp->if_init == ieee80211_init && 1042 (ifp->if_flags & IFF_UP) == 0) { 1043 struct ieee80211vap *vap = ifp->if_softc; 1044 1045 IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp)); 1046 } 1047 } 1048 1049 /* 1050 * Fetch the VAP name. 1051 * 1052 * This returns a const char pointer suitable for debugging, 1053 * but don't expect it to stick around for much longer. 1054 */ 1055 const char * 1056 ieee80211_get_vap_ifname(struct ieee80211vap *vap) 1057 { 1058 if (vap->iv_ifp == NULL) 1059 return "(none)"; 1060 return vap->iv_ifp->if_xname; 1061 } 1062 1063 #ifdef DEBUGNET 1064 static void 1065 ieee80211_debugnet_init(struct ifnet *ifp, int *nrxr, int *ncl, int *clsize) 1066 { 1067 struct ieee80211vap *vap; 1068 struct ieee80211com *ic; 1069 1070 vap = if_getsoftc(ifp); 1071 ic = vap->iv_ic; 1072 1073 IEEE80211_LOCK(ic); 1074 ic->ic_debugnet_meth->dn8_init(ic, nrxr, ncl, clsize); 1075 IEEE80211_UNLOCK(ic); 1076 } 1077 1078 static void 1079 ieee80211_debugnet_event(struct ifnet *ifp, enum debugnet_ev ev) 1080 { 1081 struct ieee80211vap *vap; 1082 struct ieee80211com *ic; 1083 1084 vap = if_getsoftc(ifp); 1085 ic = vap->iv_ic; 1086 1087 IEEE80211_LOCK(ic); 1088 ic->ic_debugnet_meth->dn8_event(ic, ev); 1089 IEEE80211_UNLOCK(ic); 1090 } 1091 1092 static int 1093 ieee80211_debugnet_transmit(struct ifnet *ifp, struct mbuf *m) 1094 { 1095 return (ieee80211_vap_transmit(ifp, m)); 1096 } 1097 1098 static int 1099 ieee80211_debugnet_poll(struct ifnet *ifp, int count) 1100 { 1101 struct ieee80211vap *vap; 1102 struct ieee80211com *ic; 1103 1104 vap = if_getsoftc(ifp); 1105 ic = vap->iv_ic; 1106 1107 return (ic->ic_debugnet_meth->dn8_poll(ic, count)); 1108 } 1109 #endif 1110 1111 /* 1112 * Module glue. 1113 * 1114 * NB: the module name is "wlan" for compatibility with NetBSD. 1115 */ 1116 static int 1117 wlan_modevent(module_t mod, int type, void *unused) 1118 { 1119 switch (type) { 1120 case MOD_LOAD: 1121 if (bootverbose) 1122 printf("wlan: <802.11 Link Layer>\n"); 1123 wlan_bpfevent = EVENTHANDLER_REGISTER(bpf_track, 1124 bpf_track, 0, EVENTHANDLER_PRI_ANY); 1125 wlan_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event, 1126 wlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY); 1127 wlan_cloner = if_clone_simple(wlanname, wlan_clone_create, 1128 wlan_clone_destroy, 0); 1129 return 0; 1130 case MOD_UNLOAD: 1131 if_clone_detach(wlan_cloner); 1132 EVENTHANDLER_DEREGISTER(bpf_track, wlan_bpfevent); 1133 EVENTHANDLER_DEREGISTER(iflladdr_event, wlan_ifllevent); 1134 return 0; 1135 } 1136 return EINVAL; 1137 } 1138 1139 static moduledata_t wlan_mod = { 1140 wlanname, 1141 wlan_modevent, 1142 0 1143 }; 1144 DECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 1145 MODULE_VERSION(wlan, 1); 1146 MODULE_DEPEND(wlan, ether, 1, 1, 1); 1147 #ifdef IEEE80211_ALQ 1148 MODULE_DEPEND(wlan, alq, 1, 1, 1); 1149 #endif /* IEEE80211_ALQ */ 1150