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