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