ieee80211_proto.c revision 1.29.4.1 1 /* $NetBSD: ieee80211_proto.c,v 1.29.4.1 2008/02/22 16:50:25 skrll Exp $ */
2 /*-
3 * Copyright (c) 2001 Atsushi Onoe
4 * Copyright (c) 2002-2007 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 #ifdef __FreeBSD__
30 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_proto.c,v 1.45 2007/11/23 05:55:06 sam Exp $");
31 #endif
32 #ifdef __NetBSD__
33 __KERNEL_RCSID(0, "$NetBSD: ieee80211_proto.c,v 1.29.4.1 2008/02/22 16:50:25 skrll Exp $");
34 #endif
35
36 /*
37 * IEEE 802.11 protocol support.
38 */
39
40 #include "opt_inet.h"
41
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/systm.h>
45
46 #include <sys/socket.h>
47 #include <sys/sockio.h>
48 #include <sys/endian.h>
49 #include <sys/errno.h>
50 #include <sys/proc.h>
51 #include <sys/sysctl.h>
52
53 #include <net/if.h>
54 #include <net/if_media.h>
55 #include <net/if_arp.h>
56 #include <net/if_ether.h>
57 #include <net/if_llc.h>
58
59 #include <net80211/ieee80211_netbsd.h>
60 #include <net80211/ieee80211_var.h>
61
62 #include <net/bpf.h>
63
64 #ifdef INET
65 #include <netinet/in.h>
66 #include <net/if_ether.h>
67 #endif
68
69 #include <net/route.h>
70 /* XXX tunables */
71 #define AGGRESSIVE_MODE_SWITCH_HYSTERESIS 3 /* pkts / 100ms */
72 #define HIGH_PRI_SWITCH_THRESH 10 /* pkts / 100ms */
73
74 #define IEEE80211_RATE2MBS(r) (((r) & IEEE80211_RATE_VAL) / 2)
75
76 const char *ieee80211_mgt_subtype_name[] = {
77 "assoc_req", "assoc_resp", "reassoc_req", "reassoc_resp",
78 "probe_req", "probe_resp", "reserved#6", "reserved#7",
79 "beacon", "atim", "disassoc", "auth",
80 "deauth", "action", "reserved#14", "reserved#15"
81 };
82 const char *ieee80211_ctl_subtype_name[] = {
83 "reserved#0", "reserved#1", "reserved#2", "reserved#3",
84 "reserved#3", "reserved#5", "reserved#6", "reserved#7",
85 "reserved#8", "reserved#9", "ps_poll", "rts",
86 "cts", "ack", "cf_end", "cf_end_ack"
87 };
88 const char *ieee80211_opmode_name[IEEE80211_OPMODE_MAX] = {
89 "IBSS", /* IEEE80211_M_IBSS */
90 "STA", /* IEEE80211_M_STA */
91 "#2",
92 "AHDEMO", /* IEEE80211_M_AHDEMO */
93 "#4", "#5",
94 "HOSTAP", /* IEEE80211_M_HOSTAP */
95 "#7",
96 "MONITOR" /* IEEE80211_M_MONITOR */
97 };
98 const char *ieee80211_state_name[IEEE80211_S_MAX] = {
99 "INIT", /* IEEE80211_S_INIT */
100 "SCAN", /* IEEE80211_S_SCAN */
101 "AUTH", /* IEEE80211_S_AUTH */
102 "ASSOC", /* IEEE80211_S_ASSOC */
103 "CAC", /* IEEE80211_S_CAC */
104 "RUN", /* IEEE80211_S_RUN */
105 "CSA", /* IEEE80211_S_CSA */
106 "SLEEP", /* IEEE80211_S_SLEEP */
107 };
108 const char *ieee80211_wme_acnames[] = {
109 "WME_AC_BE",
110 "WME_AC_BK",
111 "WME_AC_VI",
112 "WME_AC_VO",
113 "WME_UPSD",
114 };
115
116 static int ieee80211_newstate(struct ieee80211com *, enum ieee80211_state, int);
117
118 static void
119 null_update_beacon(struct ieee80211com *ic, int item)
120 {
121 }
122
123 void
124 ieee80211_proto_attach(struct ieee80211com *ic)
125 {
126 struct ifnet *ifp = ic->ic_ifp;
127
128 /* XXX room for crypto */
129 ifp->if_hdrlen = sizeof(struct ieee80211_qosframe_addr4);
130
131 ic->ic_rtsthreshold = IEEE80211_RTS_DEFAULT;
132 ic->ic_fragthreshold = IEEE80211_FRAG_DEFAULT;
133 ic->ic_fixed_rate = IEEE80211_FIXED_RATE_NONE;
134 ic->ic_bmiss_max = IEEE80211_BMISS_MAX;
135 callout_init(&ic->ic_swbmiss, CALLOUT_MPSAFE);
136 callout_init(&ic->ic_mgtsend, CALLOUT_MPSAFE);
137 ic->ic_mcast_rate = IEEE80211_MCAST_RATE_DEFAULT;
138 ic->ic_protmode = IEEE80211_PROT_CTSONLY;
139 ic->ic_roaming = IEEE80211_ROAMING_AUTO;
140
141 ic->ic_wme.wme_hipri_switch_hysteresis =
142 AGGRESSIVE_MODE_SWITCH_HYSTERESIS;
143
144 /* protocol state change handler */
145 ic->ic_newstate = ieee80211_newstate;
146 ic->ic_update_beacon = null_update_beacon;
147
148 /* initialize management frame handlers */
149 ic->ic_recv_mgmt = ieee80211_recv_mgmt;
150 ic->ic_send_mgmt = ieee80211_send_mgmt;
151 ic->ic_raw_xmit = ieee80211_raw_xmit;
152 }
153
154 void
155 ieee80211_proto_detach(struct ieee80211com *ic)
156 {
157
158 /*
159 * This should not be needed as we detach when reseting
160 * the state but be conservative here since the
161 * authenticator may do things like spawn kernel threads.
162 */
163 if (ic->ic_auth->ia_detach)
164 ic->ic_auth->ia_detach(ic);
165
166 ieee80211_drain_ifq(&ic->ic_mgtq);
167
168 /*
169 * Detach any ACL'ator.
170 */
171 if (ic->ic_acl != NULL)
172 ic->ic_acl->iac_detach(ic);
173 }
174
175 /*
176 * Simple-minded authenticator module support.
177 */
178
179 #define IEEE80211_AUTH_MAX (IEEE80211_AUTH_WPA+1)
180 /* XXX well-known names */
181 static const char *auth_modnames[IEEE80211_AUTH_MAX] = {
182 "wlan_internal", /* IEEE80211_AUTH_NONE */
183 "wlan_internal", /* IEEE80211_AUTH_OPEN */
184 "wlan_internal", /* IEEE80211_AUTH_SHARED */
185 "wlan_xauth", /* IEEE80211_AUTH_8021X */
186 "wlan_internal", /* IEEE80211_AUTH_AUTO */
187 "wlan_xauth", /* IEEE80211_AUTH_WPA */
188 };
189 static const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX];
190
191 static const struct ieee80211_authenticator auth_internal = {
192 .ia_name = "wlan_internal",
193 .ia_attach = NULL,
194 .ia_detach = NULL,
195 .ia_node_join = NULL,
196 .ia_node_leave = NULL,
197 };
198
199 /*
200 * Setup internal authenticators once; they are never unregistered.
201 */
202 static void
203 ieee80211_auth_setup(void)
204 {
205 ieee80211_authenticator_register(IEEE80211_AUTH_OPEN, &auth_internal);
206 ieee80211_authenticator_register(IEEE80211_AUTH_SHARED, &auth_internal);
207 ieee80211_authenticator_register(IEEE80211_AUTH_AUTO, &auth_internal);
208 }
209
210 const struct ieee80211_authenticator *
211 ieee80211_authenticator_get(int auth)
212 {
213 static int initialized = 0;
214 if (!initialized) {
215 ieee80211_auth_setup();
216 initialized = 1;
217 }
218 if (auth >= IEEE80211_AUTH_MAX)
219 return NULL;
220 if (authenticators[auth] == NULL)
221 ieee80211_load_module(auth_modnames[auth]);
222 return authenticators[auth];
223 }
224
225 void
226 ieee80211_authenticator_register(int type,
227 const struct ieee80211_authenticator *auth)
228 {
229 if (type >= IEEE80211_AUTH_MAX)
230 return;
231 authenticators[type] = auth;
232 }
233
234 void
235 ieee80211_authenticator_unregister(int type)
236 {
237
238 if (type >= IEEE80211_AUTH_MAX)
239 return;
240 authenticators[type] = NULL;
241 }
242
243 /*
244 * Very simple-minded ACL module support.
245 */
246 /* XXX just one for now */
247 static const struct ieee80211_aclator *acl = NULL;
248
249 void
250 ieee80211_aclator_register(const struct ieee80211_aclator *iac)
251 {
252 printf("wlan: %s acl policy registered\n", iac->iac_name);
253 acl = iac;
254 }
255
256 void
257 ieee80211_aclator_unregister(const struct ieee80211_aclator *iac)
258 {
259 if (acl == iac)
260 acl = NULL;
261 printf("wlan: %s acl policy unregistered\n", iac->iac_name);
262 }
263
264 const struct ieee80211_aclator *
265 ieee80211_aclator_get(const char *name)
266 {
267 if (acl == NULL)
268 ieee80211_load_module("wlan_acl");
269 return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL;
270 }
271
272 void
273 ieee80211_print_essid(const uint8_t *essid, int len)
274 {
275 const uint8_t *p;
276 int i;
277
278 if (len > IEEE80211_NWID_LEN)
279 len = IEEE80211_NWID_LEN;
280 /* determine printable or not */
281 for (i = 0, p = essid; i < len; i++, p++) {
282 if (*p < ' ' || *p > 0x7e)
283 break;
284 }
285 if (i == len) {
286 printf("\"");
287 for (i = 0, p = essid; i < len; i++, p++)
288 printf("%c", *p);
289 printf("\"");
290 } else {
291 printf("0x");
292 for (i = 0, p = essid; i < len; i++, p++)
293 printf("%02x", *p);
294 }
295 }
296
297 void
298 ieee80211_dump_pkt(struct ieee80211com *ic,
299 const uint8_t *buf, int len, int rate, int rssi)
300 {
301 const struct ieee80211_frame *wh;
302 int i;
303
304 wh = (const struct ieee80211_frame *)buf;
305 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
306 case IEEE80211_FC1_DIR_NODS:
307 printf("NODS %s", ether_sprintf(wh->i_addr2));
308 printf("->%s", ether_sprintf(wh->i_addr1));
309 printf("(%s)", ether_sprintf(wh->i_addr3));
310 break;
311 case IEEE80211_FC1_DIR_TODS:
312 printf("TODS %s", ether_sprintf(wh->i_addr2));
313 printf("->%s", ether_sprintf(wh->i_addr3));
314 printf("(%s)", ether_sprintf(wh->i_addr1));
315 break;
316 case IEEE80211_FC1_DIR_FROMDS:
317 printf("FRDS %s", ether_sprintf(wh->i_addr3));
318 printf("->%s", ether_sprintf(wh->i_addr1));
319 printf("(%s)", ether_sprintf(wh->i_addr2));
320 break;
321 case IEEE80211_FC1_DIR_DSTODS:
322 printf("DSDS %s", ether_sprintf((const uint8_t *)&wh[1]));
323 printf("->%s", ether_sprintf(wh->i_addr3));
324 printf("(%s", ether_sprintf(wh->i_addr2));
325 printf("->%s)", ether_sprintf(wh->i_addr1));
326 break;
327 }
328 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
329 case IEEE80211_FC0_TYPE_DATA:
330 printf(" data");
331 break;
332 case IEEE80211_FC0_TYPE_MGT:
333 printf(" %s", ieee80211_mgt_subtype_name[
334 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
335 >> IEEE80211_FC0_SUBTYPE_SHIFT]);
336 break;
337 default:
338 printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
339 break;
340 }
341 if (IEEE80211_QOS_HAS_SEQ(wh)) {
342 const struct ieee80211_qosframe *qwh =
343 (const struct ieee80211_qosframe *)buf;
344 printf(" QoS [TID %u%s]", qwh->i_qos[0] & IEEE80211_QOS_TID,
345 qwh->i_qos[0] & IEEE80211_QOS_ACKPOLICY ? " ACM" : "");
346 }
347 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
348 int off;
349
350 off = ieee80211_anyhdrspace(ic, wh);
351 printf(" WEP [IV %.02x %.02x %.02x",
352 buf[off+0], buf[off+1], buf[off+2]);
353 if (buf[off+IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV)
354 printf(" %.02x %.02x %.02x",
355 buf[off+4], buf[off+5], buf[off+6]);
356 printf(" KID %u]", buf[off+IEEE80211_WEP_IVLEN] >> 6);
357 }
358 if (rate >= 0)
359 printf(" %dM", rate / 2);
360 if (rssi >= 0)
361 printf(" +%d", rssi);
362 printf("\n");
363 if (len > 0) {
364 for (i = 0; i < len; i++) {
365 if ((i & 1) == 0)
366 printf(" ");
367 printf("%02x", buf[i]);
368 }
369 printf("\n");
370 }
371 }
372
373 static __inline int
374 findrix(const struct ieee80211_rateset *rs, int r)
375 {
376 int i;
377
378 for (i = 0; i < rs->rs_nrates; i++)
379 if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == r)
380 return i;
381 return -1;
382 }
383
384 int
385 ieee80211_fix_rate(struct ieee80211_node *ni,
386 struct ieee80211_rateset *nrs, int flags)
387 {
388 #define RV(v) ((v) & IEEE80211_RATE_VAL)
389 struct ieee80211com *ic = ni->ni_ic;
390 int i, j, rix, error;
391 int okrate, badrate, fixedrate;
392 const struct ieee80211_rateset *srs;
393 uint8_t r;
394
395 error = 0;
396 okrate = badrate = 0;
397 fixedrate = IEEE80211_FIXED_RATE_NONE;
398 srs = ieee80211_get_suprates(ic, ni->ni_chan);
399 for (i = 0; i < nrs->rs_nrates; ) {
400 if (flags & IEEE80211_F_DOSORT) {
401 /*
402 * Sort rates.
403 */
404 for (j = i + 1; j < nrs->rs_nrates; j++) {
405 if (RV(nrs->rs_rates[i]) > RV(nrs->rs_rates[j])) {
406 r = nrs->rs_rates[i];
407 nrs->rs_rates[i] = nrs->rs_rates[j];
408 nrs->rs_rates[j] = r;
409 }
410 }
411 }
412 r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
413 badrate = r;
414 /*
415 * Check for fixed rate.
416 */
417 if (r == ic->ic_fixed_rate)
418 fixedrate = r;
419 /*
420 * Check against supported rates.
421 */
422 rix = findrix(srs, r);
423 if (flags & IEEE80211_F_DONEGO) {
424 if (rix < 0) {
425 /*
426 * A rate in the node's rate set is not
427 * supported. If this is a basic rate and we
428 * are operating as a STA then this is an error.
429 * Otherwise we just discard/ignore the rate.
430 */
431 if ((flags & IEEE80211_F_JOIN) &&
432 (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
433 error++;
434 } else if ((flags & IEEE80211_F_JOIN) == 0) {
435 /*
436 * Overwrite with the supported rate
437 * value so any basic rate bit is set.
438 */
439 nrs->rs_rates[i] = srs->rs_rates[rix];
440 }
441 }
442 if ((flags & IEEE80211_F_DODEL) && rix < 0) {
443 /*
444 * Delete unacceptable rates.
445 */
446 nrs->rs_nrates--;
447 for (j = i; j < nrs->rs_nrates; j++)
448 nrs->rs_rates[j] = nrs->rs_rates[j + 1];
449 nrs->rs_rates[j] = 0;
450 continue;
451 }
452 if (rix >= 0) {
453 okrate = nrs->rs_rates[i];
454 ni->ni_txrate = i; /* XXXNH */
455 }
456 i++;
457 }
458 if (okrate == 0 || error != 0 ||
459 ((flags & IEEE80211_F_DOFRATE) && fixedrate != ic->ic_fixed_rate))
460 return badrate | IEEE80211_RATE_BASIC;
461 else
462 return RV(okrate);
463 #undef RV
464 }
465
466 /*
467 * Reset 11g-related state.
468 */
469 void
470 ieee80211_reset_erp(struct ieee80211com *ic)
471 {
472 ic->ic_flags &= ~IEEE80211_F_USEPROT;
473 ic->ic_nonerpsta = 0;
474 ic->ic_longslotsta = 0;
475 /*
476 * Short slot time is enabled only when operating in 11g
477 * and not in an IBSS. We must also honor whether or not
478 * the driver is capable of doing it.
479 */
480 ieee80211_set_shortslottime(ic,
481 IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
482 IEEE80211_IS_CHAN_HT(ic->ic_curchan) ||
483 (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
484 ic->ic_opmode == IEEE80211_M_HOSTAP &&
485 (ic->ic_caps & IEEE80211_C_SHSLOT)));
486 /*
487 * Set short preamble and ERP barker-preamble flags.
488 */
489 if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
490 (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
491 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
492 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
493 } else {
494 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
495 ic->ic_flags |= IEEE80211_F_USEBARKER;
496 }
497 }
498
499 /*
500 * Set the short slot time state and notify the driver.
501 */
502 void
503 ieee80211_set_shortslottime(struct ieee80211com *ic, int onoff)
504 {
505 if (onoff)
506 ic->ic_flags |= IEEE80211_F_SHSLOT;
507 else
508 ic->ic_flags &= ~IEEE80211_F_SHSLOT;
509 /* notify driver */
510 if (ic->ic_updateslot != NULL)
511 ic->ic_updateslot(ic->ic_ifp);
512 }
513
514 /*
515 * Check if the specified rate set supports ERP.
516 * NB: the rate set is assumed to be sorted.
517 */
518 int
519 ieee80211_iserp_rateset(struct ieee80211com *ic,
520 struct ieee80211_rateset *rs)
521 {
522 #define N(a) (sizeof(a) / sizeof(a[0]))
523 static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
524 int i, j;
525
526 if (rs->rs_nrates < N(rates))
527 return 0;
528 for (i = 0; i < N(rates); i++) {
529 for (j = 0; j < rs->rs_nrates; j++) {
530 int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
531 if (rates[i] == r)
532 goto next;
533 if (r > rates[i])
534 return 0;
535 }
536 return 0;
537 next:
538 ;
539 }
540 return 1;
541 #undef N
542 }
543
544 /*
545 * Mark the basic rates for the 11g rate table based on the
546 * operating mode. For real 11g we mark all the 11b rates
547 * and 6, 12, and 24 OFDM. For 11b compatibility we mark only
548 * 11b rates. There's also a pseudo 11a-mode used to mark only
549 * the basic OFDM rates.
550 */
551 void
552 ieee80211_set11gbasicrates(struct ieee80211_rateset *rs, enum ieee80211_phymode mode)
553 {
554 static const struct ieee80211_rateset basic[IEEE80211_MODE_MAX] = {
555 { .rs_nrates = 0 }, /* IEEE80211_MODE_AUTO */
556 { 3, { 12, 24, 48 } }, /* IEEE80211_MODE_11A */
557 { 2, { 2, 4 } }, /* IEEE80211_MODE_11B */
558 { 4, { 2, 4, 11, 22 } }, /* IEEE80211_MODE_11G (mixed b/g) */
559 { .rs_nrates = 0 }, /* IEEE80211_MODE_FH */
560 /* IEEE80211_MODE_PUREG (not yet) */
561 { 7, { 2, 4, 11, 22, 12, 24, 48 } },
562 { 3, { 12, 24, 48 } }, /* IEEE80211_MODE_11NA */
563 /* IEEE80211_MODE_11NG (mixed b/g) */
564 { 7, { 2, 4, 11, 22, 12, 24, 48 } },
565 };
566 int i, j;
567
568 for (i = 0; i < rs->rs_nrates; i++) {
569 rs->rs_rates[i] &= IEEE80211_RATE_VAL;
570 for (j = 0; j < basic[mode].rs_nrates; j++)
571 if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
572 rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
573 break;
574 }
575 }
576 }
577
578 /*
579 * WME protocol support. The following parameters come from the spec.
580 */
581 typedef struct phyParamType {
582 uint8_t aifsn;
583 uint8_t logcwmin;
584 uint8_t logcwmax;
585 uint16_t txopLimit;
586 uint8_t acm;
587 } paramType;
588
589 static const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
590 { 3, 4, 6, 0, 0 }, /* IEEE80211_MODE_AUTO */
591 { 3, 4, 6, 0, 0 }, /* IEEE80211_MODE_11A */
592 { 3, 4, 6, 0, 0 }, /* IEEE80211_MODE_11B */
593 { 3, 4, 6, 0, 0 }, /* IEEE80211_MODE_11G */
594 { 3, 4, 6, 0, 0 }, /* IEEE80211_MODE_FH */
595 { 2, 3, 5, 0, 0 }, /* IEEE80211_MODE_TURBO_A */
596 { 2, 3, 5, 0, 0 }, /* IEEE80211_MODE_TURBO_G */
597 { 2, 3, 5, 0, 0 }, /* IEEE80211_MODE_STURBO_A */
598 { 3, 4, 6, 0, 0 }, /* IEEE80211_MODE_11NA */
599 { 3, 4, 6, 0, 0 }, /* IEEE80211_MODE_11NG */
600 };
601 static const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
602 { 7, 4, 10, 0, 0 }, /* IEEE80211_MODE_AUTO */
603 { 7, 4, 10, 0, 0 }, /* IEEE80211_MODE_11A */
604 { 7, 4, 10, 0, 0 }, /* IEEE80211_MODE_11B */
605 { 7, 4, 10, 0, 0 }, /* IEEE80211_MODE_11G */
606 { 7, 4, 10, 0, 0 }, /* IEEE80211_MODE_FH */
607 { 7, 3, 10, 0, 0 }, /* IEEE80211_MODE_TURBO_A */
608 { 7, 3, 10, 0, 0 }, /* IEEE80211_MODE_TURBO_G */
609 { 7, 3, 10, 0, 0 }, /* IEEE80211_MODE_STURBO_A */
610 { 7, 4, 10, 0, 0 }, /* IEEE80211_MODE_11NA */
611 { 7, 4, 10, 0, 0 }, /* IEEE80211_MODE_11NG */
612 };
613 static const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
614 { 1, 3, 4, 94, 0 }, /* IEEE80211_MODE_AUTO */
615 { 1, 3, 4, 94, 0 }, /* IEEE80211_MODE_11A */
616 { 1, 3, 4, 188, 0 }, /* IEEE80211_MODE_11B */
617 { 1, 3, 4, 94, 0 }, /* IEEE80211_MODE_11G */
618 { 1, 3, 4, 188, 0 }, /* IEEE80211_MODE_FH */
619 { 1, 2, 3, 94, 0 }, /* IEEE80211_MODE_TURBO_A */
620 { 1, 2, 3, 94, 0 }, /* IEEE80211_MODE_TURBO_G */
621 { 1, 2, 3, 94, 0 }, /* IEEE80211_MODE_STURBO_A */
622 { 1, 3, 4, 94, 0 }, /* IEEE80211_MODE_11NA */
623 { 1, 3, 4, 94, 0 }, /* IEEE80211_MODE_11NG */
624 };
625 static const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
626 { 1, 2, 3, 47, 0 }, /* IEEE80211_MODE_AUTO */
627 { 1, 2, 3, 47, 0 }, /* IEEE80211_MODE_11A */
628 { 1, 2, 3, 102, 0 }, /* IEEE80211_MODE_11B */
629 { 1, 2, 3, 47, 0 }, /* IEEE80211_MODE_11G */
630 { 1, 2, 3, 102, 0 }, /* IEEE80211_MODE_FH */
631 { 1, 2, 2, 47, 0 }, /* IEEE80211_MODE_TURBO_A */
632 { 1, 2, 2, 47, 0 }, /* IEEE80211_MODE_TURBO_G */
633 { 1, 2, 2, 47, 0 }, /* IEEE80211_MODE_STURBO_A */
634 { 1, 2, 3, 47, 0 }, /* IEEE80211_MODE_11NA */
635 { 1, 2, 3, 47, 0 }, /* IEEE80211_MODE_11NG */
636 };
637
638 static const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
639 { 3, 4, 10, 0, 0 }, /* IEEE80211_MODE_AUTO */
640 { 3, 4, 10, 0, 0 }, /* IEEE80211_MODE_11A */
641 { 3, 4, 10, 0, 0 }, /* IEEE80211_MODE_11B */
642 { 3, 4, 10, 0, 0 }, /* IEEE80211_MODE_11G */
643 { 3, 4, 10, 0, 0 }, /* IEEE80211_MODE_FH */
644 { 2, 3, 10, 0, 0 }, /* IEEE80211_MODE_TURBO_A */
645 { 2, 3, 10, 0, 0 }, /* IEEE80211_MODE_TURBO_G */
646 { 2, 3, 10, 0, 0 }, /* IEEE80211_MODE_STURBO_A */
647 { 3, 4, 10, 0, 0 }, /* IEEE80211_MODE_11NA */
648 { 3, 4, 10, 0, 0 }, /* IEEE80211_MODE_11NG */
649 };
650 static const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
651 { 2, 3, 4, 94, 0 }, /* IEEE80211_MODE_AUTO */
652 { 2, 3, 4, 94, 0 }, /* IEEE80211_MODE_11A */
653 { 2, 3, 4, 188, 0 }, /* IEEE80211_MODE_11B */
654 { 2, 3, 4, 94, 0 }, /* IEEE80211_MODE_11G */
655 { 2, 3, 4, 188, 0 }, /* IEEE80211_MODE_FH */
656 { 2, 2, 3, 94, 0 }, /* IEEE80211_MODE_TURBO_A */
657 { 2, 2, 3, 94, 0 }, /* IEEE80211_MODE_TURBO_G */
658 { 2, 2, 3, 94, 0 }, /* IEEE80211_MODE_STURBO_A */
659 { 2, 3, 4, 94, 0 }, /* IEEE80211_MODE_11NA */
660 { 2, 3, 4, 94, 0 }, /* IEEE80211_MODE_11NG */
661 };
662 static const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
663 { 2, 2, 3, 47, 0 }, /* IEEE80211_MODE_AUTO */
664 { 2, 2, 3, 47, 0 }, /* IEEE80211_MODE_11A */
665 { 2, 2, 3, 102, 0 }, /* IEEE80211_MODE_11B */
666 { 2, 2, 3, 47, 0 }, /* IEEE80211_MODE_11G */
667 { 2, 2, 3, 102, 0 }, /* IEEE80211_MODE_FH */
668 { 1, 2, 2, 47, 0 }, /* IEEE80211_MODE_TURBO_A */
669 { 1, 2, 2, 47, 0 }, /* IEEE80211_MODE_TURBO_G */
670 { 1, 2, 2, 47, 0 }, /* IEEE80211_MODE_STURBO_A */
671 { 2, 2, 3, 47, 0 }, /* IEEE80211_MODE_11NA */
672 { 2, 2, 3, 47, 0 }, /* IEEE80211_MODE_11NG */
673 };
674
675 void
676 ieee80211_wme_initparams(struct ieee80211com *ic)
677 {
678 struct ieee80211_wme_state *wme = &ic->ic_wme;
679 const paramType *pPhyParam, *pBssPhyParam;
680 struct wmeParams *wmep;
681 enum ieee80211_phymode mode;
682 int i;
683
684 if ((ic->ic_caps & IEEE80211_C_WME) == 0)
685 return;
686
687 /*
688 * Select mode; we can be called early in which case we
689 * always use auto mode. We know we'll be called when
690 * entering the RUN state with bsschan setup properly
691 * so state will eventually get set correctly
692 */
693 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
694 mode = ieee80211_chan2mode(ic->ic_bsschan);
695 else
696 mode = IEEE80211_MODE_AUTO;
697 for (i = 0; i < WME_NUM_AC; i++) {
698 switch (i) {
699 case WME_AC_BK:
700 pPhyParam = &phyParamForAC_BK[mode];
701 pBssPhyParam = &phyParamForAC_BK[mode];
702 break;
703 case WME_AC_VI:
704 pPhyParam = &phyParamForAC_VI[mode];
705 pBssPhyParam = &bssPhyParamForAC_VI[mode];
706 break;
707 case WME_AC_VO:
708 pPhyParam = &phyParamForAC_VO[mode];
709 pBssPhyParam = &bssPhyParamForAC_VO[mode];
710 break;
711 case WME_AC_BE:
712 default:
713 pPhyParam = &phyParamForAC_BE[mode];
714 pBssPhyParam = &bssPhyParamForAC_BE[mode];
715 break;
716 }
717
718 wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
719 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
720 wmep->wmep_acm = pPhyParam->acm;
721 wmep->wmep_aifsn = pPhyParam->aifsn;
722 wmep->wmep_logcwmin = pPhyParam->logcwmin;
723 wmep->wmep_logcwmax = pPhyParam->logcwmax;
724 wmep->wmep_txopLimit = pPhyParam->txopLimit;
725 } else {
726 wmep->wmep_acm = pBssPhyParam->acm;
727 wmep->wmep_aifsn = pBssPhyParam->aifsn;
728 wmep->wmep_logcwmin = pBssPhyParam->logcwmin;
729 wmep->wmep_logcwmax = pBssPhyParam->logcwmax;
730 wmep->wmep_txopLimit = pBssPhyParam->txopLimit;
731
732 }
733 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
734 "%s: %s chan [acm %u aifsn %u log2(cwmin) %u "
735 "log2(cwmax) %u txpoLimit %u]\n", __func__
736 , ieee80211_wme_acnames[i]
737 , wmep->wmep_acm
738 , wmep->wmep_aifsn
739 , wmep->wmep_logcwmin
740 , wmep->wmep_logcwmax
741 , wmep->wmep_txopLimit
742 );
743
744 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
745 wmep->wmep_acm = pBssPhyParam->acm;
746 wmep->wmep_aifsn = pBssPhyParam->aifsn;
747 wmep->wmep_logcwmin = pBssPhyParam->logcwmin;
748 wmep->wmep_logcwmax = pBssPhyParam->logcwmax;
749 wmep->wmep_txopLimit = pBssPhyParam->txopLimit;
750 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
751 "%s: %s bss [acm %u aifsn %u log2(cwmin) %u "
752 "log2(cwmax) %u txpoLimit %u]\n", __func__
753 , ieee80211_wme_acnames[i]
754 , wmep->wmep_acm
755 , wmep->wmep_aifsn
756 , wmep->wmep_logcwmin
757 , wmep->wmep_logcwmax
758 , wmep->wmep_txopLimit
759 );
760 }
761 /* NB: check ic_bss to avoid NULL deref on initial attach */
762 if (ic->ic_bss != NULL) {
763 /*
764 * Calculate agressive mode switching threshold based
765 * on beacon interval. This doesn't need locking since
766 * we're only called before entering the RUN state at
767 * which point we start sending beacon frames.
768 */
769 wme->wme_hipri_switch_thresh =
770 (HIGH_PRI_SWITCH_THRESH * ic->ic_bss->ni_intval) / 100;
771 ieee80211_wme_updateparams(ic);
772 }
773 }
774
775 /*
776 * Update WME parameters for ourself and the BSS.
777 */
778 void
779 ieee80211_wme_updateparams_locked(struct ieee80211com *ic)
780 {
781 static const paramType phyParam[IEEE80211_MODE_MAX] = {
782 { 2, 4, 10, 64, 0 }, /* IEEE80211_MODE_AUTO */
783 { 2, 4, 10, 64, 0 }, /* IEEE80211_MODE_11A */
784 { 2, 5, 10, 64, 0 }, /* IEEE80211_MODE_11B */
785 { 2, 4, 10, 64, 0 }, /* IEEE80211_MODE_11G */
786 { 2, 5, 10, 64, 0 }, /* IEEE80211_MODE_FH */
787 { 1, 3, 10, 64, 0 }, /* IEEE80211_MODE_TURBO_A */
788 { 1, 3, 10, 64, 0 }, /* IEEE80211_MODE_TURBO_G */
789 { 1, 3, 10, 64, 0 }, /* IEEE80211_MODE_STURBO_A */
790 { 2, 4, 10, 64, 0 }, /* IEEE80211_MODE_11NA */ /*XXXcheck*/
791 { 2, 4, 10, 64, 0 }, /* IEEE80211_MODE_11NG */ /*XXXcheck*/
792 };
793 struct ieee80211_wme_state *wme = &ic->ic_wme;
794 const struct wmeParams *wmep;
795 struct wmeParams *chanp, *bssp;
796 enum ieee80211_phymode mode;
797 int i;
798
799 /* set up the channel access parameters for the physical device */
800 for (i = 0; i < WME_NUM_AC; i++) {
801 chanp = &wme->wme_chanParams.cap_wmeParams[i];
802 wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
803 chanp->wmep_aifsn = wmep->wmep_aifsn;
804 chanp->wmep_logcwmin = wmep->wmep_logcwmin;
805 chanp->wmep_logcwmax = wmep->wmep_logcwmax;
806 chanp->wmep_txopLimit = wmep->wmep_txopLimit;
807
808 chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
809 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
810 chanp->wmep_aifsn = wmep->wmep_aifsn;
811 chanp->wmep_logcwmin = wmep->wmep_logcwmin;
812 chanp->wmep_logcwmax = wmep->wmep_logcwmax;
813 chanp->wmep_txopLimit = wmep->wmep_txopLimit;
814 }
815
816 /*
817 * Select mode; we can be called early in which case we
818 * always use auto mode. We know we'll be called when
819 * entering the RUN state with bsschan setup properly
820 * so state will eventually get set correctly
821 */
822 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
823 mode = ieee80211_chan2mode(ic->ic_bsschan);
824 else
825 mode = IEEE80211_MODE_AUTO;
826
827 /*
828 * This implements agressive mode as found in certain
829 * vendors' AP's. When there is significant high
830 * priority (VI/VO) traffic in the BSS throttle back BE
831 * traffic by using conservative parameters. Otherwise
832 * BE uses agressive params to optimize performance of
833 * legacy/non-QoS traffic.
834 */
835 if ((ic->ic_opmode == IEEE80211_M_HOSTAP &&
836 (wme->wme_flags & WME_F_AGGRMODE) != 0) ||
837 (ic->ic_opmode == IEEE80211_M_STA &&
838 (ic->ic_bss->ni_flags & IEEE80211_NODE_QOS) == 0) ||
839 (ic->ic_flags & IEEE80211_F_WME) == 0) {
840 chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
841 bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
842
843 chanp->wmep_aifsn = bssp->wmep_aifsn = phyParam[mode].aifsn;
844 chanp->wmep_logcwmin = bssp->wmep_logcwmin =
845 phyParam[mode].logcwmin;
846 chanp->wmep_logcwmax = bssp->wmep_logcwmax =
847 phyParam[mode].logcwmax;
848 chanp->wmep_txopLimit = bssp->wmep_txopLimit =
849 (ic->ic_flags & IEEE80211_F_BURST) ?
850 phyParam[mode].txopLimit : 0;
851 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
852 "%s: %s [acm %u aifsn %u log2(cwmin) %u "
853 "log2(cwmax) %u txpoLimit %u]\n", __func__
854 , ieee80211_wme_acnames[WME_AC_BE]
855 , chanp->wmep_acm
856 , chanp->wmep_aifsn
857 , chanp->wmep_logcwmin
858 , chanp->wmep_logcwmax
859 , chanp->wmep_txopLimit
860 );
861 }
862
863 #ifndef IEEE80211_NO_HOSTAP
864 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
865 ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
866 static const uint8_t logCwMin[IEEE80211_MODE_MAX] = {
867 3, /* IEEE80211_MODE_AUTO */
868 3, /* IEEE80211_MODE_11A */
869 4, /* IEEE80211_MODE_11B */
870 3, /* IEEE80211_MODE_11G */
871 4, /* IEEE80211_MODE_FH */
872 3, /* IEEE80211_MODE_TURBO_A */
873 3, /* IEEE80211_MODE_TURBO_G */
874 3, /* IEEE80211_MODE_STURBO_A */
875 3, /* IEEE80211_MODE_11NA */
876 3, /* IEEE80211_MODE_11NG */
877 };
878 chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
879 bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
880
881 chanp->wmep_logcwmin = bssp->wmep_logcwmin = logCwMin[mode];
882 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
883 "%s: %s log2(cwmin) %u\n", __func__
884 , ieee80211_wme_acnames[WME_AC_BE]
885 , chanp->wmep_logcwmin
886 );
887 }
888 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { /* XXX ibss? */
889 /*
890 * Arrange for a beacon update and bump the parameter
891 * set number so associated stations load the new values.
892 */
893 wme->wme_bssChanParams.cap_info =
894 (wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
895 ieee80211_beacon_notify(ic, IEEE80211_BEACON_WME);
896 }
897 #endif /* !IEEE80211_NO_HOSTAP */
898
899 wme->wme_update(ic);
900
901 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
902 "%s: WME params updated, cap_info 0x%x\n", __func__,
903 ic->ic_opmode == IEEE80211_M_STA ?
904 wme->wme_wmeChanParams.cap_info :
905 wme->wme_bssChanParams.cap_info);
906 }
907
908 void
909 ieee80211_wme_updateparams(struct ieee80211com *ic)
910 {
911
912 if (ic->ic_caps & IEEE80211_C_WME) {
913 IEEE80211_BEACON_LOCK(ic);
914 ieee80211_wme_updateparams_locked(ic);
915 IEEE80211_BEACON_UNLOCK(ic);
916 }
917 }
918
919 /*
920 * Start a device. If this is the first vap running on the
921 * underlying device then we first bring it up.
922 */
923 int
924 ieee80211_init(struct ieee80211com *ic, int forcescan)
925 {
926
927 IEEE80211_DPRINTF(ic,
928 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
929 "%s\n", "start running");
930
931 /*
932 * Kick the 802.11 state machine as appropriate.
933 */
934 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL) {
935 if (ic->ic_opmode == IEEE80211_M_STA) {
936 ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
937 } else {
938 /*
939 * For monitor+wds modes there's nothing to do but
940 * start running. Otherwise, if this is the first
941 * vap to be brought up, start a scan which may be
942 * preempted if the station is locked to a particular
943 * channel.
944 */
945 if (ic->ic_opmode == IEEE80211_M_MONITOR ||
946 ic->ic_opmode == IEEE80211_M_WDS) {
947 ic->ic_state = IEEE80211_S_INIT; /* XXX*/
948 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
949 } else
950 ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
951 }
952 }
953 return 0;
954 }
955
956 /*
957 * Switch between turbo and non-turbo operating modes.
958 * Use the specified channel flags to locate the new
959 * channel, update 802.11 state, and then call back into
960 * the driver to effect the change.
961 */
962 void
963 ieee80211_dturbo_switch(struct ieee80211com *ic, int newflags)
964 {
965 struct ieee80211_channel *chan;
966
967 chan = ieee80211_find_channel(ic, ic->ic_bsschan->ic_freq, newflags);
968 if (chan == NULL) { /* XXX should not happen */
969 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SUPERG,
970 "%s: no channel with freq %u flags 0x%x\n",
971 __func__, ic->ic_bsschan->ic_freq, newflags);
972 return;
973 }
974
975 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SUPERG,
976 "%s: %s -> %s (freq %u flags 0x%x)\n", __func__,
977 ieee80211_phymode_name[ieee80211_chan2mode(ic->ic_bsschan)],
978 ieee80211_phymode_name[ieee80211_chan2mode(chan)],
979 chan->ic_freq, chan->ic_flags);
980
981 ic->ic_bsschan = chan;
982 ic->ic_prevchan = ic->ic_curchan;
983 ic->ic_curchan = chan;
984 ic->ic_set_channel(ic);
985 /* NB: do not need to reset ERP state 'cuz we're in sta mode */
986 }
987
988 #ifndef IEEE80211_NO_HOSTAP
989 static void
990 sta_disassoc(void *arg, struct ieee80211_node *ni)
991 {
992 struct ieee80211com *ic = arg;
993
994 if (ni->ni_associd != 0) {
995 IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
996 IEEE80211_REASON_ASSOC_LEAVE);
997 ieee80211_node_leave(ic, ni);
998 }
999 }
1000 #endif /* !IEEE80211_NO_HOSTAP */
1001
1002 void
1003 ieee80211_beacon_miss(struct ieee80211com *ic)
1004 {
1005
1006 if (ic->ic_flags & IEEE80211_F_SCAN) {
1007 /* XXX check ic_curchan != ic_bsschan? */
1008 return;
1009 }
1010 IEEE80211_DPRINTF(ic, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1011 "%s\n", "beacon miss");
1012
1013 /*
1014 * Our handling is only meaningful for stations that are
1015 * associated; any other conditions else will be handled
1016 * through different means (e.g. the tx timeout on mgt frames).
1017 */
1018 if (ic->ic_opmode != IEEE80211_M_STA || ic->ic_state != IEEE80211_S_RUN)
1019 return;
1020
1021 if (++ic->ic_bmiss_count < ic->ic_bmiss_max) {
1022 /*
1023 * Send a directed probe req before falling back to a scan;
1024 * if we receive a response ic_bmiss_count will be reset.
1025 * Some cards mistakenly report beacon miss so this avoids
1026 * the expensive scan if the ap is still there.
1027 */
1028 ieee80211_send_probereq(ic->ic_bss, ic->ic_myaddr,
1029 ic->ic_bss->ni_bssid, ic->ic_bss->ni_bssid,
1030 ic->ic_bss->ni_essid, ic->ic_bss->ni_esslen,
1031 ic->ic_opt_ie, ic->ic_opt_ie_len);
1032 return;
1033 }
1034 ic->ic_bmiss_count = 0;
1035 if (ic->ic_roaming == IEEE80211_ROAMING_AUTO) {
1036 /*
1037 * If we receive a beacon miss interrupt when using
1038 * dynamic turbo, attempt to switch modes before
1039 * reassociating.
1040 */
1041 if (IEEE80211_ATH_CAP(ic, ic->ic_bss, IEEE80211_NODE_TURBOP))
1042 ieee80211_dturbo_switch(ic,
1043 ic->ic_bsschan->ic_flags ^ IEEE80211_CHAN_TURBO);
1044 /*
1045 * Try to reassociate before scanning for a new ap.
1046 */
1047 ieee80211_new_state(ic, IEEE80211_S_ASSOC, 1);
1048 } else {
1049 /*
1050 * Somebody else is controlling state changes (e.g.
1051 * a user-mode app) don't do anything that would
1052 * confuse them; just drop into scan mode so they'll
1053 * notified of the state change and given control.
1054 */
1055 ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
1056 }
1057 }
1058
1059 /*
1060 * Software beacon miss handling. Check if any beacons
1061 * were received in the last period. If not post a
1062 * beacon miss; otherwise reset the counter.
1063 */
1064 static void
1065 ieee80211_swbmiss(void *arg)
1066 {
1067 struct ieee80211com *ic = arg;
1068
1069 if (ic->ic_swbmiss_count == 0) {
1070 ieee80211_beacon_miss(ic);
1071 if (ic->ic_bmiss_count == 0) /* don't re-arm timer */
1072 return;
1073 } else
1074 ic->ic_swbmiss_count = 0;
1075 callout_reset(&ic->ic_swbmiss, ic->ic_swbmiss_period,
1076 ieee80211_swbmiss, ic);
1077 }
1078
1079 #ifndef IEEE80211_NO_HOSTAP
1080 static void
1081 sta_deauth(void *arg, struct ieee80211_node *ni)
1082 {
1083 struct ieee80211com *ic = arg;
1084
1085 IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
1086 IEEE80211_REASON_ASSOC_LEAVE);
1087 }
1088 #endif /* !IEEE80211_NO_HOSTAP */
1089
1090 /*
1091 * Handle deauth with reason. We retry only for
1092 * the cases where we might succeed. Otherwise
1093 * we downgrade the ap and scan.
1094 */
1095 static void
1096 sta_authretry(struct ieee80211com *ic, struct ieee80211_node *ni, int reason)
1097 {
1098 switch (reason) {
1099 case IEEE80211_STATUS_SUCCESS:
1100 case IEEE80211_STATUS_TIMEOUT:
1101 case IEEE80211_REASON_ASSOC_EXPIRE:
1102 case IEEE80211_REASON_NOT_AUTHED:
1103 case IEEE80211_REASON_NOT_ASSOCED:
1104 case IEEE80211_REASON_ASSOC_LEAVE:
1105 case IEEE80211_REASON_ASSOC_NOT_AUTHED:
1106 IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_AUTH, 1);
1107 break;
1108 default:
1109 ieee80211_scan_assoc_fail(ic, ic->ic_bss->ni_macaddr, reason);
1110 if (ic->ic_roaming == IEEE80211_ROAMING_AUTO)
1111 ieee80211_check_scan(ic,
1112 IEEE80211_SCAN_ACTIVE,
1113 IEEE80211_SCAN_FOREVER,
1114 ic->ic_des_nssid, ic->ic_des_ssid);
1115 break;
1116 }
1117 }
1118
1119 static int
1120 ieee80211_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
1121 {
1122 struct ifnet *ifp = ic->ic_ifp;
1123 struct ieee80211_node *ni;
1124 enum ieee80211_state ostate;
1125
1126 ostate = ic->ic_state;
1127 IEEE80211_DPRINTF(ic, IEEE80211_MSG_STATE, "%s: %s -> %s\n", __func__,
1128 ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
1129 ic->ic_state = nstate; /* state transition */
1130 callout_stop(&ic->ic_mgtsend); /* XXX callout_drain */
1131 if (ostate != IEEE80211_S_SCAN)
1132 ieee80211_cancel_scan(ic); /* background scan */
1133 ni = ic->ic_bss; /* NB: no reference held */
1134 if (ic->ic_flags_ext & IEEE80211_FEXT_SWBMISS)
1135 callout_stop(&ic->ic_swbmiss);
1136 switch (nstate) {
1137 case IEEE80211_S_INIT:
1138 switch (ostate) {
1139 case IEEE80211_S_INIT:
1140 break;
1141 case IEEE80211_S_RUN:
1142 switch (ic->ic_opmode) {
1143 case IEEE80211_M_STA:
1144 IEEE80211_SEND_MGMT(ic, ni,
1145 IEEE80211_FC0_SUBTYPE_DISASSOC,
1146 IEEE80211_REASON_ASSOC_LEAVE);
1147 ieee80211_sta_leave(ic, ni);
1148 break;
1149 case IEEE80211_M_HOSTAP:
1150 #ifndef IEEE80211_NO_HOSTAP
1151 ieee80211_iterate_nodes(&ic->ic_sta,
1152 sta_disassoc, ic);
1153 #endif /* !IEEE80211_NO_HOSTAP */
1154 break;
1155 default:
1156 break;
1157 }
1158 break;
1159 case IEEE80211_S_ASSOC:
1160 switch (ic->ic_opmode) {
1161 case IEEE80211_M_STA:
1162 IEEE80211_SEND_MGMT(ic, ni,
1163 IEEE80211_FC0_SUBTYPE_DEAUTH,
1164 IEEE80211_REASON_AUTH_LEAVE);
1165 break;
1166 case IEEE80211_M_HOSTAP:
1167 #ifndef IEEE80211_NO_HOSTAP
1168 ieee80211_iterate_nodes(&ic->ic_sta,
1169 sta_deauth, ic);
1170 #endif /* !IEEE80211_NO_HOSTAP */
1171 break;
1172 default:
1173 break;
1174 }
1175 break;
1176 case IEEE80211_S_SCAN:
1177 ieee80211_cancel_scan(ic);
1178 break;
1179 case IEEE80211_S_AUTH:
1180 break;
1181 default:
1182 break;
1183 }
1184 if (ostate != IEEE80211_S_INIT) {
1185 /* NB: optimize INIT -> INIT case */
1186 ieee80211_drain_ifq(&ic->ic_mgtq);
1187 ieee80211_reset_bss(ic);
1188 ieee80211_scan_flush(ic);
1189 }
1190 if (ic->ic_auth->ia_detach != NULL)
1191 ic->ic_auth->ia_detach(ic);
1192 break;
1193 case IEEE80211_S_SCAN:
1194 switch (ostate) {
1195 case IEEE80211_S_INIT:
1196 createibss:
1197 if ((ic->ic_opmode == IEEE80211_M_HOSTAP ||
1198 ic->ic_opmode == IEEE80211_M_IBSS ||
1199 ic->ic_opmode == IEEE80211_M_AHDEMO) &&
1200 ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
1201 /*
1202 * Already have a channel; bypass the
1203 * scan and startup immediately. Because
1204 * of this explicitly sync the scanner state.
1205 */
1206 ieee80211_scan_update(ic);
1207 ieee80211_create_ibss(ic, ic->ic_des_chan);
1208 } else {
1209 ieee80211_check_scan(ic,
1210 IEEE80211_SCAN_ACTIVE |
1211 IEEE80211_SCAN_FLUSH,
1212 IEEE80211_SCAN_FOREVER,
1213 ic->ic_des_nssid, ic->ic_des_ssid);
1214 }
1215 break;
1216 case IEEE80211_S_SCAN:
1217 case IEEE80211_S_AUTH:
1218 case IEEE80211_S_ASSOC:
1219 /*
1220 * These can happen either because of a timeout
1221 * on an assoc/auth response or because of a
1222 * change in state that requires a reset. For
1223 * the former we're called with a non-zero arg
1224 * that is the cause for the failure; pass this
1225 * to the scan code so it can update state.
1226 * Otherwise trigger a new scan unless we're in
1227 * manual roaming mode in which case an application
1228 * must issue an explicit scan request.
1229 */
1230 if (arg != 0)
1231 ieee80211_scan_assoc_fail(ic,
1232 ic->ic_bss->ni_macaddr, arg);
1233 if (ic->ic_roaming == IEEE80211_ROAMING_AUTO)
1234 ieee80211_check_scan(ic,
1235 IEEE80211_SCAN_ACTIVE,
1236 IEEE80211_SCAN_FOREVER,
1237 ic->ic_des_nssid, ic->ic_des_ssid);
1238 break;
1239 case IEEE80211_S_RUN: /* beacon miss */
1240 if (ic->ic_opmode == IEEE80211_M_STA) {
1241 ieee80211_sta_leave(ic, ni);
1242 ic->ic_flags &= ~IEEE80211_F_SIBSS; /* XXX */
1243 if (ic->ic_roaming == IEEE80211_ROAMING_AUTO)
1244 ieee80211_check_scan(ic,
1245 IEEE80211_SCAN_ACTIVE,
1246 IEEE80211_SCAN_FOREVER,
1247 ic->ic_des_nssid,
1248 ic->ic_des_ssid);
1249 } else {
1250 ieee80211_iterate_nodes(&ic->ic_sta,
1251 sta_disassoc, ic);
1252 goto createibss;
1253 }
1254 break;
1255 default:
1256 break;
1257 }
1258 break;
1259 case IEEE80211_S_AUTH:
1260 IASSERT(ic->ic_opmode == IEEE80211_M_STA,
1261 ("switch to %s state when operating in mode %u",
1262 ieee80211_state_name[nstate], ic->ic_opmode));
1263 switch (ostate) {
1264 case IEEE80211_S_INIT:
1265 case IEEE80211_S_SCAN:
1266 IEEE80211_SEND_MGMT(ic, ni,
1267 IEEE80211_FC0_SUBTYPE_AUTH, 1);
1268 break;
1269 case IEEE80211_S_AUTH:
1270 case IEEE80211_S_ASSOC:
1271 switch (arg & 0xff) {
1272 case IEEE80211_FC0_SUBTYPE_AUTH:
1273 /* ??? */
1274 IEEE80211_SEND_MGMT(ic, ni,
1275 IEEE80211_FC0_SUBTYPE_AUTH, 2);
1276 break;
1277 case IEEE80211_FC0_SUBTYPE_DEAUTH:
1278 sta_authretry(ic, ni, arg>>8);
1279 break;
1280 }
1281 break;
1282 case IEEE80211_S_RUN:
1283 switch (arg & 0xff) {
1284 case IEEE80211_FC0_SUBTYPE_AUTH:
1285 IEEE80211_SEND_MGMT(ic, ni,
1286 IEEE80211_FC0_SUBTYPE_AUTH, 2);
1287 ic->ic_state = ostate; /* stay RUN */
1288 break;
1289 case IEEE80211_FC0_SUBTYPE_DEAUTH:
1290 ieee80211_sta_leave(ic, ni);
1291 if (ic->ic_roaming == IEEE80211_ROAMING_AUTO) {
1292 /* try to reauth */
1293 IEEE80211_SEND_MGMT(ic, ni,
1294 IEEE80211_FC0_SUBTYPE_AUTH, 1);
1295 }
1296 break;
1297 }
1298 break;
1299 default:
1300 break;
1301 }
1302 break;
1303 case IEEE80211_S_ASSOC:
1304 IASSERT(ic->ic_opmode == IEEE80211_M_STA,
1305 ("switch to %s state when operating in mode %u",
1306 ieee80211_state_name[nstate], ic->ic_opmode));
1307 switch (ostate) {
1308 case IEEE80211_S_INIT:
1309 case IEEE80211_S_SCAN:
1310 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1311 "%s: invalid transition\n", __func__);
1312 break;
1313 case IEEE80211_S_AUTH:
1314 case IEEE80211_S_ASSOC:
1315 IEEE80211_SEND_MGMT(ic, ni,
1316 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
1317 break;
1318 case IEEE80211_S_RUN:
1319 ieee80211_sta_leave(ic, ni);
1320 if (ic->ic_roaming == IEEE80211_ROAMING_AUTO) {
1321 IEEE80211_SEND_MGMT(ic, ni, arg ?
1322 IEEE80211_FC0_SUBTYPE_REASSOC_REQ :
1323 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
1324 }
1325 break;
1326 default:
1327 break;
1328 }
1329 break;
1330 case IEEE80211_S_RUN:
1331 if (ic->ic_flags & IEEE80211_F_WPA) {
1332 /* XXX validate prerequisites */
1333 }
1334 switch (ostate) {
1335 case IEEE80211_S_INIT:
1336 if (ic->ic_opmode == IEEE80211_M_MONITOR ||
1337 ic->ic_opmode == IEEE80211_M_WDS ||
1338 ic->ic_opmode == IEEE80211_M_HOSTAP) {
1339 /*
1340 * Already have a channel; bypass the
1341 * scan and startup immediately. Because
1342 * of this explicitly sync the scanner state.
1343 */
1344 ieee80211_scan_update(ic);
1345 ieee80211_create_ibss(ic,
1346 ieee80211_ht_adjust_channel(ic,
1347 ic->ic_curchan, ic->ic_flags_ext));
1348 break;
1349 }
1350 /* fall thru... */
1351 case IEEE80211_S_AUTH:
1352 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1353 "%s: invalid transition\n", __func__);
1354 /* fall thru... */
1355 case IEEE80211_S_RUN:
1356 break;
1357 case IEEE80211_S_SCAN: /* adhoc/hostap mode */
1358 case IEEE80211_S_ASSOC: /* infra mode */
1359 IASSERT(ni->ni_txrate < ni->ni_rates.rs_nrates,
1360 ("%s: bogus xmit rate %u setup\n", __func__,
1361 ni->ni_txrate));
1362 #ifdef IEEE80211_DEBUG
1363 if (ieee80211_msg_debug(ic)) {
1364 if (ic->ic_opmode == IEEE80211_M_STA)
1365 if_printf(ifp, "associated ");
1366 else
1367 if_printf(ifp, "synchronized ");
1368 printf("with %s ssid ",
1369 ether_sprintf(ni->ni_bssid));
1370 ieee80211_print_essid(ic->ic_bss->ni_essid,
1371 ni->ni_esslen);
1372 printf(" channel %d start %uMb\n",
1373 ieee80211_chan2ieee(ic, ic->ic_curchan),
1374 IEEE80211_RATE2MBS(ni->ni_rates.rs_rates[ni->ni_txrate]));
1375 }
1376 #endif
1377 if (ic->ic_opmode == IEEE80211_M_STA) {
1378 ieee80211_scan_assoc_success(ic,
1379 ni->ni_macaddr);
1380 ieee80211_notify_node_join(ic, ni,
1381 arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
1382 }
1383 (*ifp->if_start)(ifp); /* XXX not authorized yet */
1384 break;
1385 default:
1386 break;
1387 }
1388 if (ostate != IEEE80211_S_RUN &&
1389 ic->ic_opmode == IEEE80211_M_STA &&
1390 (ic->ic_flags_ext & IEEE80211_FEXT_SWBMISS)) {
1391 /*
1392 * Start s/w beacon miss timer for devices w/o
1393 * hardware support. We fudge a bit here since
1394 * we're doing this in software.
1395 */
1396 ic->ic_swbmiss_period = IEEE80211_TU_TO_TICKS(
1397 2 * ic->ic_bmissthreshold * ni->ni_intval);
1398 ic->ic_swbmiss_count = 0;
1399 callout_reset(&ic->ic_swbmiss, ic->ic_swbmiss_period,
1400 ieee80211_swbmiss, ic);
1401 }
1402 /*
1403 * Start/stop the authenticator when operating as an
1404 * AP. We delay until here to allow configuration to
1405 * happen out of order.
1406 */
1407 if (ic->ic_opmode == IEEE80211_M_HOSTAP && /* XXX IBSS/AHDEMO */
1408 ic->ic_auth->ia_attach != NULL) {
1409 /* XXX check failure */
1410 ic->ic_auth->ia_attach(ic);
1411 } else if (ic->ic_auth->ia_detach != NULL) {
1412 ic->ic_auth->ia_detach(ic);
1413 }
1414 /*
1415 * When 802.1x is not in use mark the port authorized
1416 * at this point so traffic can flow.
1417 */
1418 if (ni->ni_authmode != IEEE80211_AUTH_8021X)
1419 ieee80211_node_authorize(ni);
1420 /*
1421 * Enable inactivity processing.
1422 * XXX
1423 */
1424 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
1425 ieee80211_node_timeout, ic);
1426 break;
1427 default:
1428 break;
1429 }
1430 return 0;
1431 }
1432