1 /* $NetBSD: ieee80211_netbsd.c,v 1.21 2012/06/02 21:36:47 dsl Exp $ */ 2 /*- 3 * Copyright (c) 2003-2005 Sam Leffler, Errno Consulting 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 #ifdef __FreeBSD__ 31 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_freebsd.c,v 1.8 2005/08/08 18:46:35 sam Exp $"); 32 #else 33 __KERNEL_RCSID(0, "$NetBSD: ieee80211_netbsd.c,v 1.21 2012/06/02 21:36:47 dsl Exp $"); 34 #endif 35 36 /* 37 * IEEE 802.11 support (NetBSD-specific code) 38 */ 39 #include <sys/param.h> 40 #include <sys/kernel.h> 41 #include <sys/systm.h> 42 #include <sys/mbuf.h> 43 #include <sys/proc.h> 44 #include <sys/sysctl.h> 45 #include <sys/once.h> 46 47 #include <sys/socket.h> 48 49 #include <sys/cprng.h> 50 51 #include <net/if.h> 52 #include <net/if_media.h> 53 #include <net/if_ether.h> 54 #include <net/route.h> 55 56 #include <net80211/ieee80211_netbsd.h> 57 #include <net80211/ieee80211_var.h> 58 #include <net80211/ieee80211_sysctl.h> 59 60 #define LOGICALLY_EQUAL(x, y) (!(x) == !(y)) 61 62 static void ieee80211_sysctl_fill_node(struct ieee80211_node *, 63 struct ieee80211_node_sysctl *, int, const struct ieee80211_channel *, 64 uint32_t); 65 static struct ieee80211_node *ieee80211_node_walknext( 66 struct ieee80211_node_walk *); 67 static struct ieee80211_node *ieee80211_node_walkfirst( 68 struct ieee80211_node_walk *, u_short); 69 static int ieee80211_sysctl_node(SYSCTLFN_ARGS); 70 71 #ifdef IEEE80211_DEBUG 72 int ieee80211_debug = 0; 73 #endif 74 75 typedef void (*ieee80211_setup_func)(void); 76 77 __link_set_decl(ieee80211_funcs, ieee80211_setup_func); 78 79 static int 80 ieee80211_init0(void) 81 { 82 ieee80211_setup_func * const *ieee80211_setup, f; 83 84 __link_set_foreach(ieee80211_setup, ieee80211_funcs) { 85 f = (void*)*ieee80211_setup; 86 (*f)(); 87 } 88 89 return 0; 90 } 91 92 void 93 ieee80211_init(void) 94 { 95 static ONCE_DECL(ieee80211_init_once); 96 97 RUN_ONCE(&ieee80211_init_once, ieee80211_init0); 98 } 99 100 static int 101 ieee80211_sysctl_inact(SYSCTLFN_ARGS) 102 { 103 int error, t; 104 struct sysctlnode node; 105 106 node = *rnode; 107 /* sysctl_lookup copies the product from t. Then, it 108 * copies the new value onto t. 109 */ 110 t = *(int*)rnode->sysctl_data * IEEE80211_INACT_WAIT; 111 node.sysctl_data = &t; 112 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 113 if (error || newp == NULL) 114 return (error); 115 116 /* The new value was in seconds. Convert to inactivity-wait 117 * intervals. There are IEEE80211_INACT_WAIT seconds per 118 * interval. 119 */ 120 *(int*)rnode->sysctl_data = t / IEEE80211_INACT_WAIT; 121 122 return (0); 123 } 124 125 static int 126 ieee80211_sysctl_parent(SYSCTLFN_ARGS) 127 { 128 struct ieee80211com *ic; 129 char pname[IFNAMSIZ]; 130 struct sysctlnode node; 131 132 node = *rnode; 133 ic = node.sysctl_data; 134 strncpy(pname, ic->ic_ifp->if_xname, IFNAMSIZ); 135 node.sysctl_data = pname; 136 return sysctl_lookup(SYSCTLFN_CALL(&node)); 137 } 138 139 /* 140 * Create or get top of sysctl tree net.link.ieee80211. 141 */ 142 static const struct sysctlnode * 143 ieee80211_sysctl_treetop(struct sysctllog **log) 144 { 145 int rc; 146 const struct sysctlnode *rnode; 147 148 if ((rc = sysctl_createv(log, 0, NULL, &rnode, 149 CTLFLAG_PERMANENT, CTLTYPE_NODE, "net", NULL, 150 NULL, 0, NULL, 0, CTL_NET, CTL_EOL)) != 0) 151 goto err; 152 153 if ((rc = sysctl_createv(log, 0, &rnode, &rnode, 154 CTLFLAG_PERMANENT, CTLTYPE_NODE, "link", 155 "link-layer statistics and controls", 156 NULL, 0, NULL, 0, PF_LINK, CTL_EOL)) != 0) 157 goto err; 158 159 if ((rc = sysctl_createv(log, 0, &rnode, &rnode, 160 CTLFLAG_PERMANENT, CTLTYPE_NODE, "ieee80211", 161 "IEEE 802.11 WLAN statistics and controls", 162 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0) 163 goto err; 164 165 return rnode; 166 err: 167 printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc); 168 return NULL; 169 } 170 171 void 172 ieee80211_sysctl_attach(struct ieee80211com *ic) 173 { 174 int rc; 175 const struct sysctlnode *cnode, *rnode; 176 char num[sizeof("vap") + 14]; /* sufficient for 32 bits */ 177 178 if ((rnode = ieee80211_sysctl_treetop(NULL)) == NULL) 179 return; 180 181 snprintf(num, sizeof(num), "vap%u", ic->ic_vap); 182 183 if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &rnode, 184 CTLFLAG_PERMANENT, CTLTYPE_NODE, num, SYSCTL_DESCR("virtual AP"), 185 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0) 186 goto err; 187 188 /* control debugging printfs */ 189 if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode, 190 CTLFLAG_PERMANENT|CTLFLAG_READONLY, CTLTYPE_STRING, 191 "parent", SYSCTL_DESCR("parent device"), 192 ieee80211_sysctl_parent, 0, (void *)ic, IFNAMSIZ, CTL_CREATE, 193 CTL_EOL)) != 0) 194 goto err; 195 196 #ifdef IEEE80211_DEBUG 197 /* control debugging printfs */ 198 if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode, 199 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, 200 "debug", SYSCTL_DESCR("control debugging printfs"), 201 NULL, ieee80211_debug, &ic->ic_debug, 0, 202 CTL_CREATE, CTL_EOL)) != 0) 203 goto err; 204 #endif 205 /* XXX inherit from tunables */ 206 if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode, 207 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, 208 "inact_run", SYSCTL_DESCR("station inactivity timeout (sec)"), 209 ieee80211_sysctl_inact, 0, &ic->ic_inact_run, 0, 210 CTL_CREATE, CTL_EOL)) != 0) 211 goto err; 212 if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode, 213 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, 214 "inact_probe", 215 SYSCTL_DESCR("station inactivity probe timeout (sec)"), 216 ieee80211_sysctl_inact, 0, &ic->ic_inact_probe, 0, 217 CTL_CREATE, CTL_EOL)) != 0) 218 goto err; 219 if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode, 220 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, 221 "inact_auth", 222 SYSCTL_DESCR("station authentication timeout (sec)"), 223 ieee80211_sysctl_inact, 0, &ic->ic_inact_auth, 0, 224 CTL_CREATE, CTL_EOL)) != 0) 225 goto err; 226 if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode, 227 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, 228 "inact_init", 229 SYSCTL_DESCR("station initial state timeout (sec)"), 230 ieee80211_sysctl_inact, 0, &ic->ic_inact_init, 0, 231 CTL_CREATE, CTL_EOL)) != 0) 232 goto err; 233 if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode, 234 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, 235 "driver_caps", SYSCTL_DESCR("driver capabilities"), 236 NULL, 0, &ic->ic_caps, 0, CTL_CREATE, CTL_EOL)) != 0) 237 goto err; 238 if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode, 239 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, 240 "bmiss_max", SYSCTL_DESCR("consecutive beacon misses before scanning"), 241 NULL, 0, &ic->ic_bmiss_max, 0, CTL_CREATE, CTL_EOL)) != 0) 242 goto err; 243 244 return; 245 err: 246 printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc); 247 } 248 249 void 250 ieee80211_sysctl_detach(struct ieee80211com *ic) 251 { 252 sysctl_teardown(&ic->ic_sysctllog); 253 } 254 255 /* 256 * Pointers for testing: 257 * 258 * If there are no interfaces, or else no 802.11 interfaces, 259 * ieee80211_node_walkfirst must return NULL. 260 * 261 * If there is any single 802.11 interface, ieee80211_node_walkfirst 262 * must not return NULL. 263 */ 264 static struct ieee80211_node * 265 ieee80211_node_walkfirst(struct ieee80211_node_walk *nw, u_short if_index) 266 { 267 (void)memset(nw, 0, sizeof(*nw)); 268 269 nw->nw_ifindex = if_index; 270 271 LIST_FOREACH(nw->nw_ic, &ieee80211com_head, ic_list) { 272 if (if_index != 0 && nw->nw_ic->ic_ifp->if_index != if_index) 273 continue; 274 if (!TAILQ_EMPTY(&nw->nw_ic->ic_sta.nt_node)) 275 nw->nw_nt = &nw->nw_ic->ic_sta; 276 else if (!TAILQ_EMPTY(&nw->nw_ic->ic_scan.nt_node)) 277 nw->nw_nt = &nw->nw_ic->ic_scan; 278 else if (nw->nw_ic->ic_bss == NULL) 279 continue; 280 break; 281 } 282 283 if (nw->nw_ic == NULL) 284 return NULL; 285 286 if (nw->nw_nt == NULL) 287 nw->nw_ni = nw->nw_ic->ic_bss; 288 else 289 nw->nw_ni = TAILQ_FIRST(&nw->nw_nt->nt_node); 290 291 return nw->nw_ni; 292 } 293 294 static struct ieee80211_node * 295 ieee80211_node_walknext(struct ieee80211_node_walk *nw) 296 { 297 if (nw->nw_nt != NULL) 298 nw->nw_ni = TAILQ_NEXT(nw->nw_ni, ni_list); 299 else 300 nw->nw_ni = NULL; 301 302 while (nw->nw_ni == NULL) { 303 if (nw->nw_nt == &nw->nw_ic->ic_sta) { 304 nw->nw_nt = &nw->nw_ic->ic_scan; 305 nw->nw_ni = TAILQ_FIRST(&nw->nw_nt->nt_node); 306 continue; 307 } else if (nw->nw_nt == &nw->nw_ic->ic_scan) { 308 nw->nw_nt = NULL; 309 nw->nw_ni = nw->nw_ic->ic_bss; 310 continue; 311 } 312 KASSERT(nw->nw_nt == NULL); 313 if (nw->nw_ifindex != 0) 314 return NULL; 315 316 nw->nw_ic = LIST_NEXT(nw->nw_ic, ic_list); 317 if (nw->nw_ic == NULL) 318 return NULL; 319 320 nw->nw_nt = &nw->nw_ic->ic_sta; 321 nw->nw_ni = TAILQ_FIRST(&nw->nw_nt->nt_node); 322 } 323 324 return nw->nw_ni; 325 } 326 327 static void 328 ieee80211_sysctl_fill_node(struct ieee80211_node *ni, 329 struct ieee80211_node_sysctl *ns, int ifindex, 330 const struct ieee80211_channel *chan0, uint32_t flags) 331 { 332 ns->ns_ifindex = ifindex; 333 ns->ns_capinfo = ni->ni_capinfo; 334 ns->ns_flags = flags; 335 (void)memcpy(ns->ns_macaddr, ni->ni_macaddr, sizeof(ns->ns_macaddr)); 336 (void)memcpy(ns->ns_bssid, ni->ni_bssid, sizeof(ns->ns_bssid)); 337 if (ni->ni_chan != IEEE80211_CHAN_ANYC) { 338 ns->ns_freq = ni->ni_chan->ic_freq; 339 ns->ns_chanflags = ni->ni_chan->ic_flags; 340 ns->ns_chanidx = ni->ni_chan - chan0; 341 } else { 342 ns->ns_freq = ns->ns_chanflags = 0; 343 ns->ns_chanidx = 0; 344 } 345 ns->ns_rssi = ni->ni_rssi; 346 ns->ns_esslen = ni->ni_esslen; 347 (void)memcpy(ns->ns_essid, ni->ni_essid, sizeof(ns->ns_essid)); 348 ns->ns_erp = ni->ni_erp; 349 ns->ns_associd = ni->ni_associd; 350 ns->ns_inact = ni->ni_inact * IEEE80211_INACT_WAIT; 351 ns->ns_rstamp = ni->ni_rstamp; 352 ns->ns_rates = ni->ni_rates; 353 ns->ns_txrate = ni->ni_txrate; 354 ns->ns_intval = ni->ni_intval; 355 (void)memcpy(ns->ns_tstamp, &ni->ni_tstamp, sizeof(ns->ns_tstamp)); 356 ns->ns_txseq = ni->ni_txseqs[0]; 357 ns->ns_rxseq = ni->ni_rxseqs[0]; 358 ns->ns_fhdwell = ni->ni_fhdwell; 359 ns->ns_fhindex = ni->ni_fhindex; 360 ns->ns_fails = ni->ni_fails; 361 } 362 363 /* Between two examinations of the sysctl tree, I expect each 364 * interface to add no more than 5 nodes. 365 */ 366 #define IEEE80211_SYSCTL_NODE_GROWTH 5 367 368 static int 369 ieee80211_sysctl_node(SYSCTLFN_ARGS) 370 { 371 struct ieee80211_node_walk nw; 372 struct ieee80211_node *ni; 373 struct ieee80211_node_sysctl ns; 374 char *dp; 375 u_int cur_ifindex, ifcount, ifindex, last_ifindex, op, arg, hdr_type; 376 uint32_t flags; 377 size_t len, needed, eltsize, out_size; 378 int error, s, saw_bss = 0, nelt; 379 380 if (namelen == 1 && name[0] == CTL_QUERY) 381 return (sysctl_query(SYSCTLFN_CALL(rnode))); 382 383 if (namelen != IEEE80211_SYSCTL_NODENAMELEN) 384 return (EINVAL); 385 386 /* ifindex.op.arg.header-type.eltsize.nelt */ 387 dp = oldp; 388 len = (oldp != NULL) ? *oldlenp : 0; 389 ifindex = name[IEEE80211_SYSCTL_NODENAME_IF]; 390 op = name[IEEE80211_SYSCTL_NODENAME_OP]; 391 arg = name[IEEE80211_SYSCTL_NODENAME_ARG]; 392 hdr_type = name[IEEE80211_SYSCTL_NODENAME_TYPE]; 393 eltsize = name[IEEE80211_SYSCTL_NODENAME_ELTSIZE]; 394 nelt = name[IEEE80211_SYSCTL_NODENAME_ELTCOUNT]; 395 out_size = MIN(sizeof(ns), eltsize); 396 397 if (op != IEEE80211_SYSCTL_OP_ALL || arg != 0 || 398 hdr_type != IEEE80211_SYSCTL_T_NODE || eltsize < 1 || nelt < 0) 399 return (EINVAL); 400 401 error = 0; 402 needed = 0; 403 ifcount = 0; 404 last_ifindex = 0; 405 406 s = splnet(); 407 408 for (ni = ieee80211_node_walkfirst(&nw, ifindex); ni != NULL; 409 ni = ieee80211_node_walknext(&nw)) { 410 struct ieee80211com *ic; 411 412 ic = nw.nw_ic; 413 cur_ifindex = ic->ic_ifp->if_index; 414 415 if (cur_ifindex != last_ifindex) { 416 saw_bss = 0; 417 ifcount++; 418 last_ifindex = cur_ifindex; 419 } 420 421 if (nelt <= 0) 422 continue; 423 424 if (saw_bss && ni == ic->ic_bss) 425 continue; 426 else if (ni == ic->ic_bss) { 427 saw_bss = 1; 428 flags = IEEE80211_NODE_SYSCTL_F_BSS; 429 } else 430 flags = 0; 431 if (ni->ni_table == &ic->ic_scan) 432 flags |= IEEE80211_NODE_SYSCTL_F_SCAN; 433 else if (ni->ni_table == &ic->ic_sta) 434 flags |= IEEE80211_NODE_SYSCTL_F_STA; 435 if (len >= eltsize) { 436 ieee80211_sysctl_fill_node(ni, &ns, cur_ifindex, 437 &ic->ic_channels[0], flags); 438 error = copyout(&ns, dp, out_size); 439 if (error) 440 goto cleanup; 441 dp += eltsize; 442 len -= eltsize; 443 } 444 needed += eltsize; 445 if (nelt != INT_MAX) 446 nelt--; 447 } 448 cleanup: 449 splx(s); 450 451 *oldlenp = needed; 452 if (oldp == NULL) 453 *oldlenp += ifcount * IEEE80211_SYSCTL_NODE_GROWTH * eltsize; 454 455 return (error); 456 } 457 458 /* 459 * Setup sysctl(3) MIB, net.ieee80211.* 460 * 461 * TBD condition CTLFLAG_PERMANENT on being a module or not 462 */ 463 SYSCTL_SETUP(sysctl_ieee80211, "sysctl ieee80211 subtree setup") 464 { 465 int rc; 466 const struct sysctlnode *cnode, *rnode; 467 468 if ((rnode = ieee80211_sysctl_treetop(clog)) == NULL) 469 return; 470 471 if ((rc = sysctl_createv(clog, 0, &rnode, NULL, 472 CTLFLAG_PERMANENT, CTLTYPE_NODE, "nodes", "client/peer stations", 473 ieee80211_sysctl_node, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0) 474 goto err; 475 476 #ifdef IEEE80211_DEBUG 477 /* control debugging printfs */ 478 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode, 479 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, 480 "debug", SYSCTL_DESCR("control debugging printfs"), 481 NULL, 0, &ieee80211_debug, 0, CTL_CREATE, CTL_EOL)) != 0) 482 goto err; 483 #endif /* IEEE80211_DEBUG */ 484 485 return; 486 err: 487 printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc); 488 } 489 490 int 491 ieee80211_node_dectestref(struct ieee80211_node *ni) 492 { 493 if (atomic_dec_uint_nv(&ni->ni_refcnt) == 0) { 494 atomic_inc_uint(&ni->ni_refcnt); 495 return 1; 496 } else 497 return 0; 498 } 499 500 void 501 ieee80211_drain_ifq(struct ifqueue *ifq) 502 { 503 struct ieee80211_node *ni; 504 struct mbuf *m; 505 506 for (;;) { 507 IF_DEQUEUE(ifq, m); 508 if (m == NULL) 509 break; 510 511 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 512 KASSERT(ni != NULL); 513 ieee80211_free_node(ni); 514 m->m_pkthdr.rcvif = NULL; 515 516 m_freem(m); 517 } 518 } 519 520 521 void 522 if_printf(struct ifnet *ifp, const char *fmt, ...) 523 { 524 va_list ap; 525 va_start(ap, fmt); 526 527 printf("%s: ", ifp->if_xname); 528 vprintf(fmt, ap); 529 530 va_end(ap); 531 return; 532 } 533 534 /* 535 * Set the m_data pointer of a newly-allocated mbuf 536 * to place an object of the specified size at the 537 * end of the mbuf, longword aligned. 538 */ 539 void 540 m_align(struct mbuf *m, int len) 541 { 542 int adjust; 543 544 if (m->m_flags & M_EXT) 545 adjust = m->m_ext.ext_size - len; 546 else if (m->m_flags & M_PKTHDR) 547 adjust = MHLEN - len; 548 else 549 adjust = MLEN - len; 550 m->m_data += adjust &~ (sizeof(long)-1); 551 } 552 553 /* 554 * Append the specified data to the indicated mbuf chain, 555 * Extend the mbuf chain if the new data does not fit in 556 * existing space. 557 * 558 * Return 1 if able to complete the job; otherwise 0. 559 */ 560 int 561 m_append(struct mbuf *m0, int len, const void *cpv) 562 { 563 struct mbuf *m, *n; 564 int remainder, space; 565 const char *cp = cpv; 566 567 for (m = m0; m->m_next != NULL; m = m->m_next) 568 continue; 569 remainder = len; 570 space = M_TRAILINGSPACE(m); 571 if (space > 0) { 572 /* 573 * Copy into available space. 574 */ 575 if (space > remainder) 576 space = remainder; 577 memmove(mtod(m, char *) + m->m_len, cp, space); 578 m->m_len += space; 579 cp = cp + space, remainder -= space; 580 } 581 while (remainder > 0) { 582 /* 583 * Allocate a new mbuf; could check space 584 * and allocate a cluster instead. 585 */ 586 n = m_get(M_DONTWAIT, m->m_type); 587 if (n == NULL) 588 break; 589 n->m_len = min(MLEN, remainder); 590 memmove(mtod(n, void *), cp, n->m_len); 591 cp += n->m_len, remainder -= n->m_len; 592 m->m_next = n; 593 m = n; 594 } 595 if (m0->m_flags & M_PKTHDR) 596 m0->m_pkthdr.len += len - remainder; 597 return (remainder == 0); 598 } 599 600 /* 601 * Allocate and setup a management frame of the specified 602 * size. We return the mbuf and a pointer to the start 603 * of the contiguous data area that's been reserved based 604 * on the packet length. The data area is forced to 32-bit 605 * alignment and the buffer length to a multiple of 4 bytes. 606 * This is done mainly so beacon frames (that require this) 607 * can use this interface too. 608 */ 609 struct mbuf * 610 ieee80211_getmgtframe(u_int8_t **frm, u_int pktlen) 611 { 612 struct mbuf *m; 613 u_int len; 614 615 /* 616 * NB: we know the mbuf routines will align the data area 617 * so we don't need to do anything special. 618 */ 619 /* XXX 4-address frame? */ 620 len = roundup(sizeof(struct ieee80211_frame) + pktlen, 4); 621 IASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len)); 622 if (len <= MHLEN) { 623 m = m_gethdr(M_NOWAIT, MT_HEADER); 624 /* 625 * Align the data in case additional headers are added. 626 * This should only happen when a WEP header is added 627 * which only happens for shared key authentication mgt 628 * frames which all fit in MHLEN. 629 */ 630 if (m != NULL) 631 MH_ALIGN(m, len); 632 } else 633 m = m_getcl(M_NOWAIT, MT_HEADER, M_PKTHDR); 634 if (m != NULL) { 635 m->m_data += sizeof(struct ieee80211_frame); 636 *frm = m->m_data; 637 IASSERT((uintptr_t)*frm % 4 == 0, ("bad beacon boundary")); 638 } 639 return m; 640 } 641 642 void 643 get_random_bytes(void *p, size_t n) 644 { 645 cprng_fast(p, n); 646 } 647 648 void 649 ieee80211_notify_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int newassoc) 650 { 651 struct ifnet *ifp = ic->ic_ifp; 652 struct ieee80211_join_event iev; 653 654 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, "%s: %snode %s join\n", 655 ifp->if_xname, (ni == ic->ic_bss) ? "bss " : "", 656 ether_sprintf(ni->ni_macaddr)); 657 658 memset(&iev, 0, sizeof(iev)); 659 if (ni == ic->ic_bss) { 660 IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_bssid); 661 rt_ieee80211msg(ifp, newassoc ? 662 RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC, 663 &iev, sizeof(iev)); 664 if_link_state_change(ifp, LINK_STATE_UP); 665 } else { 666 IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr); 667 rt_ieee80211msg(ifp, newassoc ? 668 RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN, 669 &iev, sizeof(iev)); 670 } 671 } 672 673 void 674 ieee80211_notify_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni) 675 { 676 struct ifnet *ifp = ic->ic_ifp; 677 struct ieee80211_leave_event iev; 678 679 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, "%s: %snode %s leave\n", 680 ifp->if_xname, (ni == ic->ic_bss) ? "bss " : "", 681 ether_sprintf(ni->ni_macaddr)); 682 683 if (ni == ic->ic_bss) { 684 rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0); 685 if_link_state_change(ifp, LINK_STATE_DOWN); 686 } else { 687 /* fire off wireless event station leaving */ 688 memset(&iev, 0, sizeof(iev)); 689 IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr); 690 rt_ieee80211msg(ifp, RTM_IEEE80211_LEAVE, &iev, sizeof(iev)); 691 } 692 } 693 694 void 695 ieee80211_notify_scan_done(struct ieee80211com *ic) 696 { 697 struct ifnet *ifp = ic->ic_ifp; 698 699 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 700 "%s: notify scan done\n", ic->ic_ifp->if_xname); 701 702 /* dispatch wireless event indicating scan completed */ 703 rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0); 704 } 705 706 void 707 ieee80211_notify_replay_failure(struct ieee80211com *ic, 708 const struct ieee80211_frame *wh, const struct ieee80211_key *k, 709 u_int64_t rsc) 710 { 711 struct ifnet *ifp = ic->ic_ifp; 712 713 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 714 "[%s] %s replay detected <rsc %ju, csc %ju, keyix %u rxkeyix %u>\n", 715 ether_sprintf(wh->i_addr2), k->wk_cipher->ic_name, 716 (intmax_t) rsc, (intmax_t) k->wk_keyrsc, 717 k->wk_keyix, k->wk_rxkeyix); 718 719 if (ifp != NULL) { /* NB: for cipher test modules */ 720 struct ieee80211_replay_event iev; 721 722 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1); 723 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2); 724 iev.iev_cipher = k->wk_cipher->ic_cipher; 725 if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE) 726 iev.iev_keyix = k->wk_rxkeyix; 727 else 728 iev.iev_keyix = k->wk_keyix; 729 iev.iev_keyrsc = k->wk_keyrsc; 730 iev.iev_rsc = rsc; 731 rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev)); 732 } 733 } 734 735 void 736 ieee80211_notify_michael_failure(struct ieee80211com *ic, 737 const struct ieee80211_frame *wh, u_int keyix) 738 { 739 struct ifnet *ifp = ic->ic_ifp; 740 741 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 742 "[%s] michael MIC verification failed <keyix %u>\n", 743 ether_sprintf(wh->i_addr2), keyix); 744 ic->ic_stats.is_rx_tkipmic++; 745 746 if (ifp != NULL) { /* NB: for cipher test modules */ 747 struct ieee80211_michael_event iev; 748 749 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1); 750 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2); 751 iev.iev_cipher = IEEE80211_CIPHER_TKIP; 752 iev.iev_keyix = keyix; 753 rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev)); 754 } 755 } 756 757 void 758 ieee80211_load_module(const char *modname) 759 { 760 #ifdef notyet 761 struct thread *td = curthread; 762 763 if (suser(td) == 0 && securelevel_gt(td->td_ucred, 0) == 0) { 764 mtx_lock(&Giant); 765 (void) linker_load_module(modname, NULL, NULL, NULL, NULL); 766 mtx_unlock(&Giant); 767 } 768 #else 769 printf("%s: load the %s module by hand for now.\n", __func__, modname); 770 #endif 771 } 772