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