1 /* 2 * Copyright (c) 2003-2005 Sam Leffler, Errno Consulting 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 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 * $FreeBSD: src/sys/net80211/ieee80211_freebsd.c,v 1.7.2.2 2005/12/22 19:22:51 sam Exp $ 28 * $DragonFly: src/sys/netproto/802_11/wlan/ieee80211_dragonfly.c,v 1.1 2006/05/18 13:51:46 sephe Exp $ 29 */ 30 31 /* 32 * IEEE 802.11 support (DragonFlyBSD-specific code) 33 */ 34 #include <sys/param.h> 35 #include <sys/kernel.h> 36 #include <sys/systm.h> 37 #include <sys/linker.h> 38 #include <sys/mbuf.h> 39 #include <sys/module.h> 40 #include <sys/proc.h> 41 #include <sys/sysctl.h> 42 43 #include <sys/socket.h> 44 45 #include <net/if.h> 46 #include <net/if_arp.h> 47 #include <net/if_media.h> 48 #include <net/ethernet.h> 49 #include <net/route.h> 50 51 #include <netproto/802_11/ieee80211_var.h> 52 53 #define if_link_state_change(ifp, state) ((void)0) 54 55 SYSCTL_NODE(_net, OID_AUTO, wlan, CTLFLAG_RD, 0, "IEEE 80211 parameters"); 56 57 #ifdef IEEE80211_DEBUG 58 int ieee80211_debug = 0; 59 SYSCTL_INT(_net_wlan, OID_AUTO, debug, CTLFLAG_RW, &ieee80211_debug, 60 0, "debugging printfs"); 61 #endif 62 63 static int 64 ieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS) 65 { 66 int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT; 67 int error; 68 69 error = sysctl_handle_int(oidp, &inact, 0, req); 70 if (error || !req->newptr) 71 return error; 72 *(int *)arg1 = inact / IEEE80211_INACT_WAIT; 73 return 0; 74 } 75 76 static int 77 ieee80211_sysctl_parent(SYSCTL_HANDLER_ARGS) 78 { 79 struct ieee80211com *ic = arg1; 80 const char *name = ic->ic_ifp->if_xname; 81 82 return SYSCTL_OUT(req, name, strlen(name)); 83 } 84 85 void 86 ieee80211_sysctl_attach(struct ieee80211com *ic) 87 { 88 struct sysctl_ctx_list *ctx; 89 struct sysctl_oid *oid; 90 char num[14]; /* sufficient for 32 bits */ 91 92 ctx = malloc(sizeof(struct sysctl_ctx_list), M_DEVBUF, 93 M_WAITOK | M_ZERO); 94 sysctl_ctx_init(ctx); 95 96 snprintf(num, sizeof(num), "%u", ic->ic_vap); 97 oid = SYSCTL_ADD_NODE(ctx, &SYSCTL_NODE_CHILDREN(_net, wlan), 98 OID_AUTO, num, CTLFLAG_RD, NULL, ""); 99 if (oid == NULL) 100 return; 101 102 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 103 "%parent", CTLFLAG_RD, ic, 0, ieee80211_sysctl_parent, "A", 104 "parent device"); 105 #ifdef IEEE80211_DEBUG 106 ic->ic_debug = ieee80211_debug; 107 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 108 "debug", CTLFLAG_RW, &ic->ic_debug, 0, 109 "control debugging printfs"); 110 #endif 111 /* XXX inherit from tunables */ 112 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 113 "inact_run", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_run, 0, 114 ieee80211_sysctl_inact, "I", 115 "station inactivity timeout (sec)"); 116 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 117 "inact_probe", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_probe, 0, 118 ieee80211_sysctl_inact, "I", 119 "station inactivity probe timeout (sec)"); 120 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 121 "inact_auth", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_auth, 0, 122 ieee80211_sysctl_inact, "I", 123 "station authentication timeout (sec)"); 124 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 125 "inact_init", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_init, 0, 126 ieee80211_sysctl_inact, "I", 127 "station initial state timeout (sec)"); 128 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 129 "driver_caps", CTLFLAG_RW, &ic->ic_caps, 0, 130 "driver capabilities"); 131 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 132 "bmiss_max", CTLFLAG_RW, &ic->ic_bmiss_max, 0, 133 "consecutive beacon misses before scanning"); 134 135 ic->ic_sysctl = ctx; 136 } 137 138 void 139 ieee80211_sysctl_detach(struct ieee80211com *ic) 140 { 141 if (ic->ic_sysctl != NULL) { 142 sysctl_ctx_free(ic->ic_sysctl); 143 free(ic->ic_sysctl, M_DEVBUF); 144 ic->ic_sysctl = NULL; 145 } 146 } 147 148 int 149 ieee80211_node_dectestref(struct ieee80211_node *ni) 150 { 151 /* XXX need equivalent of atomic_dec_and_test */ 152 atomic_subtract_int(&ni->ni_refcnt, 1); 153 return atomic_cmpset_int(&ni->ni_refcnt, 0, 1); 154 } 155 156 /* 157 * Allocate and setup a management frame of the specified 158 * size. We return the mbuf and a pointer to the start 159 * of the contiguous data area that's been reserved based 160 * on the packet length. The data area is forced to 32-bit 161 * alignment and the buffer length to a multiple of 4 bytes. 162 * This is done mainly so beacon frames (that require this) 163 * can use this interface too. 164 */ 165 struct mbuf * 166 ieee80211_getmgtframe(uint8_t **frm, u_int pktlen) 167 { 168 struct mbuf *m; 169 u_int len; 170 171 /* 172 * NB: we know the mbuf routines will align the data area 173 * so we don't need to do anything special. 174 */ 175 /* XXX 4-address frame? */ 176 len = roundup(sizeof(struct ieee80211_frame) + pktlen, 4); 177 KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len)); 178 if (len < MINCLSIZE) { 179 m = m_gethdr(M_NOWAIT, MT_HEADER); 180 /* 181 * Align the data in case additional headers are added. 182 * This should only happen when a WEP header is added 183 * which only happens for shared key authentication mgt 184 * frames which all fit in MHLEN. 185 */ 186 if (m != NULL) 187 MH_ALIGN(m, len); 188 } else 189 m = m_getcl(M_NOWAIT, MT_HEADER, M_PKTHDR); 190 if (m != NULL) { 191 m->m_data += sizeof(struct ieee80211_frame); 192 *frm = m->m_data; 193 } 194 return m; 195 } 196 197 #include <sys/libkern.h> 198 199 void 200 get_random_bytes(void *p, size_t n) 201 { 202 uint8_t *dp = p; 203 204 while (n > 0) { 205 uint32_t v = arc4random(); 206 size_t nb = n > sizeof(uint32_t) ? sizeof(uint32_t) : n; 207 208 bcopy(&v, dp, n > sizeof(uint32_t) ? sizeof(uint32_t) : n); 209 dp += sizeof(uint32_t), n -= nb; 210 } 211 } 212 213 void 214 ieee80211_notify_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, 215 int newassoc) 216 { 217 struct ifnet *ifp = ic->ic_ifp; 218 struct ieee80211_join_event iev; 219 220 memset(&iev, 0, sizeof(iev)); 221 if (ni == ic->ic_bss) { 222 IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_bssid); 223 rt_ieee80211msg(ifp, newassoc ? 224 RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC, 225 &iev, sizeof(iev)); 226 if_link_state_change(ifp, LINK_STATE_UP); 227 } else { 228 IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr); 229 rt_ieee80211msg(ifp, newassoc ? 230 RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN, 231 &iev, sizeof(iev)); 232 } 233 } 234 235 void 236 ieee80211_notify_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni) 237 { 238 struct ifnet *ifp = ic->ic_ifp; 239 struct ieee80211_leave_event iev; 240 241 if (ni == ic->ic_bss) { 242 rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0); 243 if_link_state_change(ifp, LINK_STATE_DOWN); 244 } else { 245 /* fire off wireless event station leaving */ 246 memset(&iev, 0, sizeof(iev)); 247 IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr); 248 rt_ieee80211msg(ifp, RTM_IEEE80211_LEAVE, &iev, sizeof(iev)); 249 } 250 } 251 252 void 253 ieee80211_notify_scan_done(struct ieee80211com *ic) 254 { 255 struct ifnet *ifp = ic->ic_ifp; 256 257 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 258 "%s: notify scan done\n", ic->ic_ifp->if_xname); 259 260 /* dispatch wireless event indicating scan completed */ 261 rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0); 262 } 263 264 void 265 ieee80211_notify_replay_failure(struct ieee80211com *ic, 266 const struct ieee80211_frame *wh, const struct ieee80211_key *k, 267 uint64_t rsc) 268 { 269 struct ifnet *ifp = ic->ic_ifp; 270 271 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 272 "[%6D] %s replay detected <rsc %ju, csc %ju, keyix %u rxkeyix %u>\n", 273 wh->i_addr2, ":", k->wk_cipher->ic_name, 274 (intmax_t) rsc, (intmax_t) k->wk_keyrsc, 275 k->wk_keyix, k->wk_rxkeyix); 276 277 if (ifp != NULL) { /* NB: for cipher test modules */ 278 struct ieee80211_replay_event iev; 279 280 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1); 281 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2); 282 iev.iev_cipher = k->wk_cipher->ic_cipher; 283 if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE) 284 iev.iev_keyix = k->wk_rxkeyix; 285 else 286 iev.iev_keyix = k->wk_keyix; 287 iev.iev_keyrsc = k->wk_keyrsc; 288 iev.iev_rsc = rsc; 289 rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev)); 290 } 291 } 292 293 void 294 ieee80211_notify_michael_failure(struct ieee80211com *ic, 295 const struct ieee80211_frame *wh, u_int keyix) 296 { 297 struct ifnet *ifp = ic->ic_ifp; 298 299 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 300 "[%6D] michael MIC verification failed <keyix %u>\n", 301 wh->i_addr2, ":", keyix); 302 ic->ic_stats.is_rx_tkipmic++; 303 304 if (ifp != NULL) { /* NB: for cipher test modules */ 305 struct ieee80211_michael_event iev; 306 307 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1); 308 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2); 309 iev.iev_cipher = IEEE80211_CIPHER_TKIP; 310 iev.iev_keyix = keyix; 311 rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev)); 312 } 313 } 314 315 void 316 ieee80211_load_module(const char *modname) 317 { 318 #ifdef notyet 319 struct thread *td = curthread; 320 321 if (suser(td) == 0 && securelevel_gt(td->td_ucred, 0) == 0) { 322 crit_enter(); /* NB: need BGL here */ 323 linker_load_module(modname, NULL, NULL, NULL, NULL); 324 crit_exit(); 325 } 326 #else 327 printf("%s: load the %s module by hand for now.\n", __func__, modname); 328 #endif 329 } 330 331 /* 332 * Append the specified data to the indicated mbuf chain, 333 * Extend the mbuf chain if the new data does not fit in 334 * existing space. 335 * 336 * Return 1 if able to complete the job; otherwise 0. 337 */ 338 int 339 ieee80211_mbuf_append(struct mbuf *m0, int len, const uint8_t *cp) 340 { 341 struct mbuf *m, *n; 342 int remainder, space; 343 344 for (m = m0; m->m_next != NULL; m = m->m_next) 345 ; 346 remainder = len; 347 space = M_TRAILINGSPACE(m); 348 if (space > 0) { 349 /* 350 * Copy into available space. 351 */ 352 if (space > remainder) 353 space = remainder; 354 bcopy(cp, mtod(m, caddr_t) + m->m_len, space); 355 m->m_len += space; 356 cp += space, remainder -= space; 357 } 358 while (remainder > 0) { 359 /* 360 * Allocate a new mbuf; could check space 361 * and allocate a cluster instead. 362 */ 363 n = m_get(MB_DONTWAIT, m->m_type); 364 if (n == NULL) 365 break; 366 n->m_len = min(MLEN, remainder); 367 bcopy(cp, mtod(n, caddr_t), n->m_len); 368 cp += n->m_len, remainder -= n->m_len; 369 m->m_next = n; 370 m = n; 371 } 372 if (m0->m_flags & M_PKTHDR) 373 m0->m_pkthdr.len += len - remainder; 374 return (remainder == 0); 375 } 376 377 /* 378 * Create a writable copy of the mbuf chain. While doing this 379 * we compact the chain with a goal of producing a chain with 380 * at most two mbufs. The second mbuf in this chain is likely 381 * to be a cluster. The primary purpose of this work is to create 382 * a writable packet for encryption, compression, etc. The 383 * secondary goal is to linearize the data so the data can be 384 * passed to crypto hardware in the most efficient manner possible. 385 */ 386 struct mbuf * 387 ieee80211_mbuf_clone(struct mbuf *m0, int how) 388 { 389 struct mbuf *m, *mprev; 390 struct mbuf *n, *mfirst, *mlast; 391 int len, off; 392 393 mprev = NULL; 394 for (m = m0; m != NULL; m = mprev->m_next) { 395 /* 396 * Regular mbufs are ignored unless there's a cluster 397 * in front of it that we can use to coalesce. We do 398 * the latter mainly so later clusters can be coalesced 399 * also w/o having to handle them specially (i.e. convert 400 * mbuf+cluster -> cluster). This optimization is heavily 401 * influenced by the assumption that we're running over 402 * Ethernet where MCLBYTES is large enough that the max 403 * packet size will permit lots of coalescing into a 404 * single cluster. This in turn permits efficient 405 * crypto operations, especially when using hardware. 406 */ 407 if ((m->m_flags & M_EXT) == 0) { 408 if (mprev && (mprev->m_flags & M_EXT) && 409 m->m_len <= M_TRAILINGSPACE(mprev)) { 410 /* XXX: this ignores mbuf types */ 411 memcpy(mtod(mprev, caddr_t) + mprev->m_len, 412 mtod(m, caddr_t), m->m_len); 413 mprev->m_len += m->m_len; 414 mprev->m_next = m->m_next; /* unlink from chain */ 415 m_free(m); /* reclaim mbuf */ 416 } else { 417 mprev = m; 418 } 419 continue; 420 } 421 /* 422 * Writable mbufs are left alone (for now). 423 */ 424 if (M_WRITABLE(m)) { 425 mprev = m; 426 continue; 427 } 428 429 /* 430 * Not writable, replace with a copy or coalesce with 431 * the previous mbuf if possible (since we have to copy 432 * it anyway, we try to reduce the number of mbufs and 433 * clusters so that future work is easier). 434 */ 435 KASSERT(m->m_flags & M_EXT, ("m_flags 0x%x", m->m_flags)); 436 /* NB: we only coalesce into a cluster or larger */ 437 if (mprev != NULL && (mprev->m_flags & M_EXT) && 438 m->m_len <= M_TRAILINGSPACE(mprev)) { 439 /* XXX: this ignores mbuf types */ 440 memcpy(mtod(mprev, caddr_t) + mprev->m_len, 441 mtod(m, caddr_t), m->m_len); 442 mprev->m_len += m->m_len; 443 mprev->m_next = m->m_next; /* unlink from chain */ 444 m_free(m); /* reclaim mbuf */ 445 continue; 446 } 447 448 /* 449 * Allocate new space to hold the copy... 450 */ 451 /* XXX why can M_PKTHDR be set past the first mbuf? */ 452 if (mprev == NULL && (m->m_flags & M_PKTHDR)) { 453 /* 454 * NB: if a packet header is present we must 455 * allocate the mbuf separately from any cluster 456 * because M_MOVE_PKTHDR will smash the data 457 * pointer and drop the M_EXT marker. 458 */ 459 MGETHDR(n, how, m->m_type); 460 if (n == NULL) { 461 m_freem(m0); 462 return (NULL); 463 } 464 M_MOVE_PKTHDR(n, m); 465 MCLGET(n, how); 466 if ((n->m_flags & M_EXT) == 0) { 467 m_free(n); 468 m_freem(m0); 469 return (NULL); 470 } 471 } else { 472 n = m_getcl(how, m->m_type, m->m_flags); 473 if (n == NULL) { 474 m_freem(m0); 475 return (NULL); 476 } 477 } 478 /* 479 * ... and copy the data. We deal with jumbo mbufs 480 * (i.e. m_len > MCLBYTES) by splitting them into 481 * clusters. We could just malloc a buffer and make 482 * it external but too many device drivers don't know 483 * how to break up the non-contiguous memory when 484 * doing DMA. 485 */ 486 len = m->m_len; 487 off = 0; 488 mfirst = n; 489 mlast = NULL; 490 for (;;) { 491 int cc = min(len, MCLBYTES); 492 memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + off, cc); 493 n->m_len = cc; 494 if (mlast != NULL) 495 mlast->m_next = n; 496 mlast = n; 497 498 len -= cc; 499 if (len <= 0) 500 break; 501 off += cc; 502 503 n = m_getcl(how, m->m_type, m->m_flags); 504 if (n == NULL) { 505 m_freem(mfirst); 506 m_freem(m0); 507 return (NULL); 508 } 509 } 510 n->m_next = m->m_next; 511 if (mprev == NULL) 512 m0 = mfirst; /* new head of chain */ 513 else 514 mprev->m_next = mfirst; /* replace old mbuf */ 515 m_free(m); /* release old mbuf */ 516 mprev = mfirst; 517 } 518 return (m0); 519 } 520 521 /* 522 * Module glue. 523 * 524 * NB: the module name is "wlan" for compatibility with NetBSD. 525 */ 526 static int 527 wlan_modevent(module_t mod, int type, void *unused) 528 { 529 switch (type) { 530 case MOD_LOAD: 531 if (bootverbose) 532 printf("wlan: <802.11 Link Layer>\n"); 533 return 0; 534 case MOD_UNLOAD: 535 return 0; 536 } 537 return EINVAL; 538 } 539 540 static moduledata_t wlan_mod = { 541 "wlan", 542 wlan_modevent, 543 0 544 }; 545 DECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 546 MODULE_VERSION(wlan, 1); 547 MODULE_DEPEND(wlan, crypto, 1, 1, 1); 548