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