1 /* $NetBSD: ieee80211_proto.c,v 1.7 2004/01/13 23:37:30 dyoung Exp $ */ 2 /*- 3 * Copyright (c) 2001 Atsushi Onoe 4 * Copyright (c) 2002, 2003 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 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * Alternatively, this software may be distributed under the terms of the 19 * GNU General Public License ("GPL") version 2 as published by the Free 20 * Software Foundation. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 #ifdef __FreeBSD__ 36 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_proto.c,v 1.6 2003/10/31 18:32:09 brooks Exp $"); 37 #else 38 __KERNEL_RCSID(0, "$NetBSD: ieee80211_proto.c,v 1.7 2004/01/13 23:37:30 dyoung Exp $"); 39 #endif 40 41 /* 42 * IEEE 802.11 protocol support. 43 */ 44 45 #include "opt_inet.h" 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/mbuf.h> 50 #include <sys/malloc.h> 51 #include <sys/kernel.h> 52 #include <sys/socket.h> 53 #include <sys/sockio.h> 54 #include <sys/endian.h> 55 #include <sys/errno.h> 56 #ifdef __FreeBSD__ 57 #include <sys/bus.h> 58 #endif 59 #include <sys/proc.h> 60 #include <sys/sysctl.h> 61 62 #ifdef __FreeBSD__ 63 #include <machine/atomic.h> 64 #endif 65 66 #include <net/if.h> 67 #include <net/if_dl.h> 68 #include <net/if_media.h> 69 #include <net/if_arp.h> 70 #ifdef __FreeBSD__ 71 #include <net/ethernet.h> 72 #else 73 #include <net/if_ether.h> 74 #endif 75 #include <net/if_llc.h> 76 77 #include <net80211/ieee80211_var.h> 78 #include <net80211/ieee80211_compat.h> 79 80 #include <net/bpf.h> 81 82 #ifdef INET 83 #include <netinet/in.h> 84 #ifdef __FreeBSD__ 85 #include <netinet/if_ether.h> 86 #else 87 #include <net/if_ether.h> 88 #endif 89 #endif 90 91 #define IEEE80211_RATE2MBS(r) (((r) & IEEE80211_RATE_VAL) / 2) 92 93 const char *ieee80211_mgt_subtype_name[] = { 94 "assoc_req", "assoc_resp", "reassoc_req", "reassoc_resp", 95 "probe_req", "probe_resp", "reserved#6", "reserved#7", 96 "beacon", "atim", "disassoc", "auth", 97 "deauth", "reserved#13", "reserved#14", "reserved#15" 98 }; 99 const char *ieee80211_state_name[IEEE80211_S_MAX] = { 100 "INIT", /* IEEE80211_S_INIT */ 101 "SCAN", /* IEEE80211_S_SCAN */ 102 "AUTH", /* IEEE80211_S_AUTH */ 103 "ASSOC", /* IEEE80211_S_ASSOC */ 104 "RUN" /* IEEE80211_S_RUN */ 105 }; 106 107 static int ieee80211_newstate(struct ieee80211com *, enum ieee80211_state, int); 108 109 void 110 ieee80211_proto_attach(struct ifnet *ifp) 111 { 112 struct ieee80211com *ic = (void *)ifp; 113 114 ifp->if_hdrlen = sizeof(struct ieee80211_frame); 115 116 #ifdef notdef 117 ic->ic_rtsthreshold = IEEE80211_RTS_DEFAULT; 118 #else 119 ic->ic_rtsthreshold = IEEE80211_RTS_MAX; 120 #endif 121 ic->ic_fragthreshold = 2346; /* XXX not used yet */ 122 ic->ic_fixed_rate = -1; /* no fixed rate */ 123 124 #ifdef __FreeBSD__ 125 mtx_init(&ic->ic_mgtq.ifq_mtx, ifp->if_xname, "mgmt send q", MTX_DEF); 126 #endif 127 128 /* protocol state change handler */ 129 ic->ic_newstate = ieee80211_newstate; 130 131 /* initialize management frame handlers */ 132 ic->ic_recv_mgmt = ieee80211_recv_mgmt; 133 ic->ic_send_mgmt = ieee80211_send_mgmt; 134 } 135 136 void 137 ieee80211_proto_detach(struct ifnet *ifp) 138 { 139 struct ieee80211com *ic = (void *)ifp; 140 141 #ifdef __FreeBSD__ 142 IF_DRAIN(&ic->ic_mgtq); 143 mtx_destroy(&ic->ic_mgtq.ifq_mtx); 144 #else 145 IF_PURGE(&ic->ic_mgtq); 146 IF_PURGE(&ic->ic_pwrsaveq); 147 #endif 148 } 149 150 void 151 ieee80211_print_essid(u_int8_t *essid, int len) 152 { 153 int i; 154 u_int8_t *p; 155 156 if (len > IEEE80211_NWID_LEN) 157 len = IEEE80211_NWID_LEN; 158 /* determine printable or not */ 159 for (i = 0, p = essid; i < len; i++, p++) { 160 if (*p < ' ' || *p > 0x7e) 161 break; 162 } 163 if (i == len) { 164 printf("\""); 165 for (i = 0, p = essid; i < len; i++, p++) 166 printf("%c", *p); 167 printf("\""); 168 } else { 169 printf("0x"); 170 for (i = 0, p = essid; i < len; i++, p++) 171 printf("%02x", *p); 172 } 173 } 174 175 void 176 ieee80211_dump_pkt(u_int8_t *buf, int len, int rate, int rssi) 177 { 178 struct ieee80211_frame *wh; 179 int i; 180 181 wh = (struct ieee80211_frame *)buf; 182 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) { 183 case IEEE80211_FC1_DIR_NODS: 184 printf("NODS %s", ether_sprintf(wh->i_addr2)); 185 printf("->%s", ether_sprintf(wh->i_addr1)); 186 printf("(%s)", ether_sprintf(wh->i_addr3)); 187 break; 188 case IEEE80211_FC1_DIR_TODS: 189 printf("TODS %s", ether_sprintf(wh->i_addr2)); 190 printf("->%s", ether_sprintf(wh->i_addr3)); 191 printf("(%s)", ether_sprintf(wh->i_addr1)); 192 break; 193 case IEEE80211_FC1_DIR_FROMDS: 194 printf("FRDS %s", ether_sprintf(wh->i_addr3)); 195 printf("->%s", ether_sprintf(wh->i_addr1)); 196 printf("(%s)", ether_sprintf(wh->i_addr2)); 197 break; 198 case IEEE80211_FC1_DIR_DSTODS: 199 printf("DSDS %s", ether_sprintf((u_int8_t *)&wh[1])); 200 printf("->%s", ether_sprintf(wh->i_addr3)); 201 printf("(%s", ether_sprintf(wh->i_addr2)); 202 printf("->%s)", ether_sprintf(wh->i_addr1)); 203 break; 204 } 205 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) { 206 case IEEE80211_FC0_TYPE_DATA: 207 printf(" data"); 208 break; 209 case IEEE80211_FC0_TYPE_MGT: 210 printf(" %s", ieee80211_mgt_subtype_name[ 211 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) 212 >> IEEE80211_FC0_SUBTYPE_SHIFT]); 213 break; 214 default: 215 printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK); 216 break; 217 } 218 if (wh->i_fc[1] & IEEE80211_FC1_WEP) 219 printf(" WEP"); 220 if (rate >= 0) 221 printf(" %dM", rate / 2); 222 if (rssi >= 0) 223 printf(" +%d", rssi); 224 printf("\n"); 225 if (len > 0) { 226 for (i = 0; i < len; i++) { 227 if ((i & 1) == 0) 228 printf(" "); 229 printf("%02x", buf[i]); 230 } 231 printf("\n"); 232 } 233 } 234 235 int 236 ieee80211_fix_rate(struct ieee80211com *ic, struct ieee80211_node *ni, int flags) 237 { 238 #define RV(v) ((v) & IEEE80211_RATE_VAL) 239 int i, j, ignore, error; 240 int okrate, badrate; 241 struct ieee80211_rateset *srs, *nrs; 242 u_int8_t r; 243 244 error = 0; 245 okrate = badrate = 0; 246 srs = &ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)]; 247 nrs = &ni->ni_rates; 248 for (i = 0; i < nrs->rs_nrates; ) { 249 ignore = 0; 250 if (flags & IEEE80211_F_DOSORT) { 251 /* 252 * Sort rates. 253 */ 254 for (j = i + 1; j < nrs->rs_nrates; j++) { 255 if (RV(nrs->rs_rates[i]) > RV(nrs->rs_rates[j])) { 256 r = nrs->rs_rates[i]; 257 nrs->rs_rates[i] = nrs->rs_rates[j]; 258 nrs->rs_rates[j] = r; 259 } 260 } 261 } 262 r = nrs->rs_rates[i] & IEEE80211_RATE_VAL; 263 badrate = r; 264 if (flags & IEEE80211_F_DOFRATE) { 265 /* 266 * Apply fixed rate constraint. Note that we do 267 * not apply the constraint to basic rates as 268 * otherwise we may not be able to associate if 269 * the rate set we submit to the AP is invalid 270 * (e.g. fix rate at 36Mb/s which is not a basic 271 * rate for 11a operation). 272 */ 273 if ((nrs->rs_rates[i] & IEEE80211_RATE_BASIC) == 0 && 274 ic->ic_fixed_rate >= 0 && 275 r != RV(srs->rs_rates[ic->ic_fixed_rate])) 276 ignore++; 277 } 278 if (flags & IEEE80211_F_DONEGO) { 279 /* 280 * Check against supported rates. 281 */ 282 for (j = 0; j < srs->rs_nrates; j++) { 283 if (r == RV(srs->rs_rates[j])) 284 break; 285 } 286 if (j == srs->rs_nrates) { 287 /* 288 * A rate in the node's rate set is not 289 * supported. If this is a basic rate and we 290 * are operating as an AP then this is an error. 291 * Otherwise we just discard/ignore the rate. 292 * Note that this is important for 11b stations 293 * when they want to associate with an 11g AP. 294 */ 295 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 296 (nrs->rs_rates[i] & IEEE80211_RATE_BASIC)) 297 error++; 298 ignore++; 299 } 300 } 301 if (flags & IEEE80211_F_DODEL) { 302 /* 303 * Delete unacceptable rates. 304 */ 305 if (ignore) { 306 nrs->rs_nrates--; 307 for (j = i; j < nrs->rs_nrates; j++) 308 nrs->rs_rates[j] = nrs->rs_rates[j + 1]; 309 nrs->rs_rates[j] = 0; 310 continue; 311 } 312 } 313 if (!ignore) 314 okrate = nrs->rs_rates[i]; 315 i++; 316 } 317 if (okrate == 0 || error != 0) 318 return badrate | IEEE80211_RATE_BASIC; 319 else 320 return RV(okrate); 321 #undef RV 322 } 323 324 static int 325 ieee80211_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int mgt) 326 { 327 struct ifnet *ifp = &ic->ic_if; 328 struct ieee80211_node *ni; 329 enum ieee80211_state ostate; 330 ieee80211_node_critsec_decl(s); 331 332 ostate = ic->ic_state; 333 IEEE80211_DPRINTF(("%s: %s -> %s\n", __func__, 334 ieee80211_state_name[ostate], ieee80211_state_name[nstate])); 335 ic->ic_state = nstate; /* state transition */ 336 ni = ic->ic_bss; /* NB: no reference held */ 337 switch (nstate) { 338 case IEEE80211_S_INIT: 339 switch (ostate) { 340 case IEEE80211_S_INIT: 341 break; 342 case IEEE80211_S_RUN: 343 switch (ic->ic_opmode) { 344 case IEEE80211_M_STA: 345 IEEE80211_SEND_MGMT(ic, ni, 346 IEEE80211_FC0_SUBTYPE_DISASSOC, 347 IEEE80211_REASON_ASSOC_LEAVE); 348 break; 349 case IEEE80211_M_HOSTAP: 350 ieee80211_node_critsec_begin(ic, s); 351 TAILQ_FOREACH(ni, &ic->ic_node, ni_list) { 352 if (ni->ni_associd == 0) 353 continue; 354 IEEE80211_SEND_MGMT(ic, ni, 355 IEEE80211_FC0_SUBTYPE_DISASSOC, 356 IEEE80211_REASON_ASSOC_LEAVE); 357 } 358 ieee80211_node_critsec_end(ic, s); 359 break; 360 default: 361 break; 362 } 363 /* FALLTHRU */ 364 case IEEE80211_S_ASSOC: 365 switch (ic->ic_opmode) { 366 case IEEE80211_M_STA: 367 IEEE80211_SEND_MGMT(ic, ni, 368 IEEE80211_FC0_SUBTYPE_DEAUTH, 369 IEEE80211_REASON_AUTH_LEAVE); 370 break; 371 case IEEE80211_M_HOSTAP: 372 ieee80211_node_critsec_begin(ic, s); 373 TAILQ_FOREACH(ni, &ic->ic_node, ni_list) { 374 IEEE80211_SEND_MGMT(ic, ni, 375 IEEE80211_FC0_SUBTYPE_DEAUTH, 376 IEEE80211_REASON_AUTH_LEAVE); 377 } 378 ieee80211_node_critsec_end(ic, s); 379 break; 380 default: 381 break; 382 } 383 /* FALLTHRU */ 384 case IEEE80211_S_AUTH: 385 case IEEE80211_S_SCAN: 386 ic->ic_mgt_timer = 0; 387 #ifdef __FreeBSD__ 388 IF_DRAIN(&ic->ic_mgtq); 389 #else 390 IF_PURGE(&ic->ic_mgtq); 391 IF_PURGE(&ic->ic_pwrsaveq); 392 #endif 393 if (ic->ic_wep_ctx != NULL) { 394 free(ic->ic_wep_ctx, M_DEVBUF); 395 ic->ic_wep_ctx = NULL; 396 } 397 ieee80211_free_allnodes(ic); 398 break; 399 } 400 break; 401 case IEEE80211_S_SCAN: 402 ic->ic_flags &= ~IEEE80211_F_SIBSS; 403 /* initialize bss for probe request */ 404 IEEE80211_ADDR_COPY(ni->ni_macaddr, ifp->if_broadcastaddr); 405 IEEE80211_ADDR_COPY(ni->ni_bssid, ifp->if_broadcastaddr); 406 ni->ni_rates = ic->ic_sup_rates[ 407 ieee80211_chan2mode(ic, ni->ni_chan)]; 408 ni->ni_associd = 0; 409 ni->ni_rstamp = 0; 410 switch (ostate) { 411 case IEEE80211_S_INIT: 412 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 413 ic->ic_des_chan != IEEE80211_CHAN_ANYC) { 414 /* 415 * AP operation and we already have a channel; 416 * bypass the scan and startup immediately. 417 */ 418 ieee80211_create_ibss(ic, ic->ic_des_chan); 419 } else { 420 ieee80211_begin_scan(ifp); 421 } 422 break; 423 case IEEE80211_S_SCAN: 424 /* scan next */ 425 if (ic->ic_flags & IEEE80211_F_ASCAN) { 426 IEEE80211_SEND_MGMT(ic, ni, 427 IEEE80211_FC0_SUBTYPE_PROBE_REQ, 0); 428 } 429 break; 430 case IEEE80211_S_RUN: 431 /* beacon miss */ 432 if (ifp->if_flags & IFF_DEBUG) { 433 /* XXX bssid clobbered above */ 434 if_printf(ifp, "no recent beacons from %s;" 435 " rescanning\n", 436 ether_sprintf(ic->ic_bss->ni_bssid)); 437 } 438 ieee80211_free_allnodes(ic); 439 /* FALLTHRU */ 440 case IEEE80211_S_AUTH: 441 case IEEE80211_S_ASSOC: 442 /* timeout restart scan */ 443 ni = ieee80211_find_node(ic, ic->ic_bss->ni_macaddr); 444 if (ni != NULL) { 445 ni->ni_fails++; 446 ieee80211_unref_node(&ni); 447 } 448 ieee80211_begin_scan(ifp); 449 break; 450 } 451 break; 452 case IEEE80211_S_AUTH: 453 switch (ostate) { 454 case IEEE80211_S_INIT: 455 IEEE80211_DPRINTF(("%s: invalid transition\n", 456 __func__)); 457 break; 458 case IEEE80211_S_SCAN: 459 IEEE80211_SEND_MGMT(ic, ni, 460 IEEE80211_FC0_SUBTYPE_AUTH, 1); 461 break; 462 case IEEE80211_S_AUTH: 463 case IEEE80211_S_ASSOC: 464 switch (mgt) { 465 case IEEE80211_FC0_SUBTYPE_AUTH: 466 /* ??? */ 467 IEEE80211_SEND_MGMT(ic, ni, 468 IEEE80211_FC0_SUBTYPE_AUTH, 2); 469 break; 470 case IEEE80211_FC0_SUBTYPE_DEAUTH: 471 /* ignore and retry scan on timeout */ 472 break; 473 } 474 break; 475 case IEEE80211_S_RUN: 476 switch (mgt) { 477 case IEEE80211_FC0_SUBTYPE_AUTH: 478 IEEE80211_SEND_MGMT(ic, ni, 479 IEEE80211_FC0_SUBTYPE_AUTH, 2); 480 ic->ic_state = ostate; /* stay RUN */ 481 break; 482 case IEEE80211_FC0_SUBTYPE_DEAUTH: 483 /* try to reauth */ 484 IEEE80211_SEND_MGMT(ic, ni, 485 IEEE80211_FC0_SUBTYPE_AUTH, 1); 486 break; 487 } 488 break; 489 } 490 break; 491 case IEEE80211_S_ASSOC: 492 switch (ostate) { 493 case IEEE80211_S_INIT: 494 case IEEE80211_S_SCAN: 495 case IEEE80211_S_ASSOC: 496 IEEE80211_DPRINTF(("%s: invalid transition\n", 497 __func__)); 498 break; 499 case IEEE80211_S_AUTH: 500 IEEE80211_SEND_MGMT(ic, ni, 501 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0); 502 break; 503 case IEEE80211_S_RUN: 504 IEEE80211_SEND_MGMT(ic, ni, 505 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 1); 506 break; 507 } 508 break; 509 case IEEE80211_S_RUN: 510 switch (ostate) { 511 case IEEE80211_S_INIT: 512 case IEEE80211_S_AUTH: 513 case IEEE80211_S_RUN: 514 IEEE80211_DPRINTF(("%s: invalid transition\n", 515 __func__)); 516 break; 517 case IEEE80211_S_SCAN: /* adhoc/hostap mode */ 518 case IEEE80211_S_ASSOC: /* infra mode */ 519 IASSERT(ni->ni_txrate < ni->ni_rates.rs_nrates, 520 ("%s: bogus xmit rate %u setup\n", __func__, 521 ni->ni_txrate)); 522 if (ifp->if_flags & IFF_DEBUG) { 523 if_printf(ifp, " "); 524 if (ic->ic_opmode == IEEE80211_M_STA) 525 printf("associated "); 526 else 527 printf("synchronized "); 528 printf("with %s ssid ", 529 ether_sprintf(ni->ni_bssid)); 530 ieee80211_print_essid(ic->ic_bss->ni_essid, 531 ni->ni_esslen); 532 printf(" channel %d start %uMb\n", 533 ieee80211_chan2ieee(ic, ni->ni_chan), 534 IEEE80211_RATE2MBS(ni->ni_rates.rs_rates[ni->ni_txrate])); 535 } 536 ic->ic_mgt_timer = 0; 537 (*ifp->if_start)(ifp); 538 break; 539 } 540 break; 541 } 542 return 0; 543 } 544