ieee80211_proto.c revision 1.9 1 /* $NetBSD: ieee80211_proto.c,v 1.9 2004/07/16 02:38:34 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.8 2004/04/02 20:22:25 sam Exp $");
37 #else
38 __KERNEL_RCSID(0, "$NetBSD: ieee80211_proto.c,v 1.9 2004/07/16 02:38:34 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 #include <net/route.h>
92
93 #define IEEE80211_RATE2MBS(r) (((r) & IEEE80211_RATE_VAL) / 2)
94
95 const char *ieee80211_mgt_subtype_name[] = {
96 "assoc_req", "assoc_resp", "reassoc_req", "reassoc_resp",
97 "probe_req", "probe_resp", "reserved#6", "reserved#7",
98 "beacon", "atim", "disassoc", "auth",
99 "deauth", "reserved#13", "reserved#14", "reserved#15"
100 };
101 const char *ieee80211_state_name[IEEE80211_S_MAX] = {
102 "INIT", /* IEEE80211_S_INIT */
103 "SCAN", /* IEEE80211_S_SCAN */
104 "AUTH", /* IEEE80211_S_AUTH */
105 "ASSOC", /* IEEE80211_S_ASSOC */
106 "RUN" /* IEEE80211_S_RUN */
107 };
108
109 static int ieee80211_newstate(struct ieee80211com *, enum ieee80211_state, int);
110
111 void
112 ieee80211_proto_attach(struct ifnet *ifp)
113 {
114 struct ieee80211com *ic = (void *)ifp;
115
116 ifp->if_hdrlen = sizeof(struct ieee80211_frame);
117
118 #ifdef notdef
119 ic->ic_rtsthreshold = IEEE80211_RTS_DEFAULT;
120 #else
121 ic->ic_rtsthreshold = IEEE80211_RTS_MAX;
122 #endif
123 ic->ic_fragthreshold = 2346; /* XXX not used yet */
124 ic->ic_fixed_rate = -1; /* no fixed rate */
125 ic->ic_protmode = IEEE80211_PROT_CTSONLY;
126
127 #ifdef __FreeBSD__
128 mtx_init(&ic->ic_mgtq.ifq_mtx, ifp->if_xname, "mgmt send q", MTX_DEF);
129 #endif
130
131 /* protocol state change handler */
132 ic->ic_newstate = ieee80211_newstate;
133
134 /* initialize management frame handlers */
135 ic->ic_recv_mgmt = ieee80211_recv_mgmt;
136 ic->ic_send_mgmt = ieee80211_send_mgmt;
137 }
138
139 void
140 ieee80211_proto_detach(struct ifnet *ifp)
141 {
142 struct ieee80211com *ic = (void *)ifp;
143
144 #ifdef __FreeBSD__
145 IF_DRAIN(&ic->ic_mgtq);
146 mtx_destroy(&ic->ic_mgtq.ifq_mtx);
147 #else
148 IF_PURGE(&ic->ic_mgtq);
149 IF_PURGE(&ic->ic_pwrsaveq);
150 #endif
151 }
152
153 void
154 ieee80211_print_essid(u_int8_t *essid, int len)
155 {
156 int i;
157 u_int8_t *p;
158
159 if (len > IEEE80211_NWID_LEN)
160 len = IEEE80211_NWID_LEN;
161 /* determine printable or not */
162 for (i = 0, p = essid; i < len; i++, p++) {
163 if (*p < ' ' || *p > 0x7e)
164 break;
165 }
166 if (i == len) {
167 printf("\"");
168 for (i = 0, p = essid; i < len; i++, p++)
169 printf("%c", *p);
170 printf("\"");
171 } else {
172 printf("0x");
173 for (i = 0, p = essid; i < len; i++, p++)
174 printf("%02x", *p);
175 }
176 }
177
178 void
179 ieee80211_dump_pkt(u_int8_t *buf, int len, int rate, int rssi)
180 {
181 struct ieee80211_frame *wh;
182 int i;
183
184 wh = (struct ieee80211_frame *)buf;
185 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
186 case IEEE80211_FC1_DIR_NODS:
187 printf("NODS %s", ether_sprintf(wh->i_addr2));
188 printf("->%s", ether_sprintf(wh->i_addr1));
189 printf("(%s)", ether_sprintf(wh->i_addr3));
190 break;
191 case IEEE80211_FC1_DIR_TODS:
192 printf("TODS %s", ether_sprintf(wh->i_addr2));
193 printf("->%s", ether_sprintf(wh->i_addr3));
194 printf("(%s)", ether_sprintf(wh->i_addr1));
195 break;
196 case IEEE80211_FC1_DIR_FROMDS:
197 printf("FRDS %s", ether_sprintf(wh->i_addr3));
198 printf("->%s", ether_sprintf(wh->i_addr1));
199 printf("(%s)", ether_sprintf(wh->i_addr2));
200 break;
201 case IEEE80211_FC1_DIR_DSTODS:
202 printf("DSDS %s", ether_sprintf((u_int8_t *)&wh[1]));
203 printf("->%s", ether_sprintf(wh->i_addr3));
204 printf("(%s", ether_sprintf(wh->i_addr2));
205 printf("->%s)", ether_sprintf(wh->i_addr1));
206 break;
207 }
208 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
209 case IEEE80211_FC0_TYPE_DATA:
210 printf(" data");
211 break;
212 case IEEE80211_FC0_TYPE_MGT:
213 printf(" %s", ieee80211_mgt_subtype_name[
214 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
215 >> IEEE80211_FC0_SUBTYPE_SHIFT]);
216 break;
217 default:
218 printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
219 break;
220 }
221 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
222 printf(" WEP");
223 if (rate >= 0)
224 printf(" %dM", rate / 2);
225 if (rssi >= 0)
226 printf(" +%d", rssi);
227 printf("\n");
228 if (len > 0) {
229 for (i = 0; i < len; i++) {
230 if ((i & 1) == 0)
231 printf(" ");
232 printf("%02x", buf[i]);
233 }
234 printf("\n");
235 }
236 }
237
238 int
239 ieee80211_fix_rate(struct ieee80211com *ic, struct ieee80211_node *ni, int flags)
240 {
241 #define RV(v) ((v) & IEEE80211_RATE_VAL)
242 int i, j, ignore, error;
243 int okrate, badrate;
244 struct ieee80211_rateset *srs, *nrs;
245 u_int8_t r;
246
247 error = 0;
248 okrate = badrate = 0;
249 srs = &ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
250 nrs = &ni->ni_rates;
251 for (i = 0; i < nrs->rs_nrates; ) {
252 ignore = 0;
253 if (flags & IEEE80211_F_DOSORT) {
254 /*
255 * Sort rates.
256 */
257 for (j = i + 1; j < nrs->rs_nrates; j++) {
258 if (RV(nrs->rs_rates[i]) > RV(nrs->rs_rates[j])) {
259 r = nrs->rs_rates[i];
260 nrs->rs_rates[i] = nrs->rs_rates[j];
261 nrs->rs_rates[j] = r;
262 }
263 }
264 }
265 r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
266 badrate = r;
267 if (flags & IEEE80211_F_DOFRATE) {
268 /*
269 * Apply fixed rate constraint. Note that we do
270 * not apply the constraint to basic rates as
271 * otherwise we may not be able to associate if
272 * the rate set we submit to the AP is invalid
273 * (e.g. fix rate at 36Mb/s which is not a basic
274 * rate for 11a operation).
275 */
276 if ((nrs->rs_rates[i] & IEEE80211_RATE_BASIC) == 0 &&
277 ic->ic_fixed_rate >= 0 &&
278 r != RV(srs->rs_rates[ic->ic_fixed_rate]))
279 ignore++;
280 }
281 if (flags & IEEE80211_F_DONEGO) {
282 /*
283 * Check against supported rates.
284 */
285 for (j = 0; j < srs->rs_nrates; j++) {
286 if (r == RV(srs->rs_rates[j])) {
287 /*
288 * Overwrite with the supported rate
289 * value so any basic rate bit is set.
290 * This insures that response we send
291 * to stations have the necessary basic
292 * rate bit set.
293 */
294 nrs->rs_rates[i] = srs->rs_rates[j];
295 break;
296 }
297 }
298 if (j == srs->rs_nrates) {
299 /*
300 * A rate in the node's rate set is not
301 * supported. If this is a basic rate and we
302 * are operating as an AP then this is an error.
303 * Otherwise we just discard/ignore the rate.
304 * Note that this is important for 11b stations
305 * when they want to associate with an 11g AP.
306 */
307 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
308 (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
309 error++;
310 ignore++;
311 }
312 }
313 if (flags & IEEE80211_F_DODEL) {
314 /*
315 * Delete unacceptable rates.
316 */
317 if (ignore) {
318 nrs->rs_nrates--;
319 for (j = i; j < nrs->rs_nrates; j++)
320 nrs->rs_rates[j] = nrs->rs_rates[j + 1];
321 nrs->rs_rates[j] = 0;
322 continue;
323 }
324 }
325 if (!ignore)
326 okrate = nrs->rs_rates[i];
327 i++;
328 }
329 if (okrate == 0 || error != 0)
330 return badrate | IEEE80211_RATE_BASIC;
331 else
332 return RV(okrate);
333 #undef RV
334 }
335
336 static int
337 ieee80211_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int mgt)
338 {
339 struct ifnet *ifp = &ic->ic_if;
340 struct ieee80211_node *ni;
341 enum ieee80211_state ostate;
342 ieee80211_node_critsec_decl(s);
343 int linkstate = LINK_STATE_DOWN;
344
345 ostate = ic->ic_state;
346 IEEE80211_DPRINTF(("%s: %s -> %s\n", __func__,
347 ieee80211_state_name[ostate], ieee80211_state_name[nstate]));
348 ic->ic_state = nstate; /* state transition */
349 ni = ic->ic_bss; /* NB: no reference held */
350 switch (nstate) {
351 case IEEE80211_S_INIT:
352 switch (ostate) {
353 case IEEE80211_S_INIT:
354 break;
355 case IEEE80211_S_RUN:
356 switch (ic->ic_opmode) {
357 case IEEE80211_M_STA:
358 IEEE80211_SEND_MGMT(ic, ni,
359 IEEE80211_FC0_SUBTYPE_DISASSOC,
360 IEEE80211_REASON_ASSOC_LEAVE);
361 break;
362 case IEEE80211_M_HOSTAP:
363 ieee80211_node_critsec_begin(ic, s);
364 TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
365 if (ni->ni_associd == 0)
366 continue;
367 IEEE80211_SEND_MGMT(ic, ni,
368 IEEE80211_FC0_SUBTYPE_DISASSOC,
369 IEEE80211_REASON_ASSOC_LEAVE);
370 }
371 ieee80211_node_critsec_end(ic, s);
372 break;
373 default:
374 break;
375 }
376 /* FALLTHRU */
377 case IEEE80211_S_ASSOC:
378 switch (ic->ic_opmode) {
379 case IEEE80211_M_STA:
380 IEEE80211_SEND_MGMT(ic, ni,
381 IEEE80211_FC0_SUBTYPE_DEAUTH,
382 IEEE80211_REASON_AUTH_LEAVE);
383 break;
384 case IEEE80211_M_HOSTAP:
385 ieee80211_node_critsec_begin(ic, s);
386 TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
387 IEEE80211_SEND_MGMT(ic, ni,
388 IEEE80211_FC0_SUBTYPE_DEAUTH,
389 IEEE80211_REASON_AUTH_LEAVE);
390 }
391 ieee80211_node_critsec_end(ic, s);
392 break;
393 default:
394 break;
395 }
396 /* FALLTHRU */
397 case IEEE80211_S_AUTH:
398 case IEEE80211_S_SCAN:
399 ic->ic_mgt_timer = 0;
400 #ifdef __FreeBSD__
401 IF_DRAIN(&ic->ic_mgtq);
402 #else
403 IF_PURGE(&ic->ic_mgtq);
404 IF_PURGE(&ic->ic_pwrsaveq);
405 #endif
406 if (ic->ic_wep_ctx != NULL) {
407 free(ic->ic_wep_ctx, M_DEVBUF);
408 ic->ic_wep_ctx = NULL;
409 }
410 ieee80211_free_allnodes(ic);
411 break;
412 }
413 break;
414 case IEEE80211_S_SCAN:
415 ic->ic_flags &= ~IEEE80211_F_SIBSS;
416 /* initialize bss for probe request */
417 IEEE80211_ADDR_COPY(ni->ni_macaddr, ifp->if_broadcastaddr);
418 IEEE80211_ADDR_COPY(ni->ni_bssid, ifp->if_broadcastaddr);
419 ni->ni_rates = ic->ic_sup_rates[
420 ieee80211_chan2mode(ic, ni->ni_chan)];
421 ni->ni_associd = 0;
422 ni->ni_rstamp = 0;
423 switch (ostate) {
424 case IEEE80211_S_INIT:
425 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
426 ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
427 /*
428 * AP operation and we already have a channel;
429 * bypass the scan and startup immediately.
430 */
431 ieee80211_create_ibss(ic, ic->ic_des_chan);
432 } else {
433 ieee80211_begin_scan(ifp);
434 }
435 break;
436 case IEEE80211_S_SCAN:
437 /* scan next */
438 if (ic->ic_flags & IEEE80211_F_ASCAN) {
439 IEEE80211_SEND_MGMT(ic, ni,
440 IEEE80211_FC0_SUBTYPE_PROBE_REQ, 0);
441 }
442 break;
443 case IEEE80211_S_RUN:
444 /* beacon miss */
445 if (ifp->if_flags & IFF_DEBUG) {
446 /* XXX bssid clobbered above */
447 if_printf(ifp, "no recent beacons from %s;"
448 " rescanning\n",
449 ether_sprintf(ic->ic_bss->ni_bssid));
450 }
451 ieee80211_free_allnodes(ic);
452 /* FALLTHRU */
453 case IEEE80211_S_AUTH:
454 case IEEE80211_S_ASSOC:
455 /* timeout restart scan */
456 ni = ieee80211_find_node(ic, ic->ic_bss->ni_macaddr);
457 if (ni != NULL) {
458 ni->ni_fails++;
459 ieee80211_unref_node(&ni);
460 }
461 ieee80211_begin_scan(ifp);
462 break;
463 }
464 break;
465 case IEEE80211_S_AUTH:
466 switch (ostate) {
467 case IEEE80211_S_INIT:
468 IEEE80211_DPRINTF(("%s: invalid transition\n",
469 __func__));
470 break;
471 case IEEE80211_S_SCAN:
472 IEEE80211_SEND_MGMT(ic, ni,
473 IEEE80211_FC0_SUBTYPE_AUTH, 1);
474 break;
475 case IEEE80211_S_AUTH:
476 case IEEE80211_S_ASSOC:
477 switch (mgt) {
478 case IEEE80211_FC0_SUBTYPE_AUTH:
479 /* ??? */
480 IEEE80211_SEND_MGMT(ic, ni,
481 IEEE80211_FC0_SUBTYPE_AUTH, 2);
482 break;
483 case IEEE80211_FC0_SUBTYPE_DEAUTH:
484 /* ignore and retry scan on timeout */
485 break;
486 }
487 break;
488 case IEEE80211_S_RUN:
489 switch (mgt) {
490 case IEEE80211_FC0_SUBTYPE_AUTH:
491 IEEE80211_SEND_MGMT(ic, ni,
492 IEEE80211_FC0_SUBTYPE_AUTH, 2);
493 ic->ic_state = ostate; /* stay RUN */
494 break;
495 case IEEE80211_FC0_SUBTYPE_DEAUTH:
496 /* try to reauth */
497 IEEE80211_SEND_MGMT(ic, ni,
498 IEEE80211_FC0_SUBTYPE_AUTH, 1);
499 break;
500 }
501 break;
502 }
503 break;
504 case IEEE80211_S_ASSOC:
505 switch (ostate) {
506 case IEEE80211_S_INIT:
507 case IEEE80211_S_SCAN:
508 case IEEE80211_S_ASSOC:
509 IEEE80211_DPRINTF(("%s: invalid transition\n",
510 __func__));
511 break;
512 case IEEE80211_S_AUTH:
513 IEEE80211_SEND_MGMT(ic, ni,
514 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
515 break;
516 case IEEE80211_S_RUN:
517 IEEE80211_SEND_MGMT(ic, ni,
518 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 1);
519 break;
520 }
521 break;
522 case IEEE80211_S_RUN:
523 linkstate = LINK_STATE_UP;
524 switch (ostate) {
525 case IEEE80211_S_INIT:
526 case IEEE80211_S_AUTH:
527 case IEEE80211_S_RUN:
528 IEEE80211_DPRINTF(("%s: invalid transition\n",
529 __func__));
530 break;
531 case IEEE80211_S_SCAN: /* adhoc/hostap mode */
532 case IEEE80211_S_ASSOC: /* infra mode */
533 IASSERT(ni->ni_txrate < ni->ni_rates.rs_nrates,
534 ("%s: bogus xmit rate %u setup\n", __func__,
535 ni->ni_txrate));
536 if (ifp->if_flags & IFF_DEBUG) {
537 if_printf(ifp, " ");
538 if (ic->ic_opmode == IEEE80211_M_STA)
539 printf("associated ");
540 else
541 printf("synchronized ");
542 printf("with %s ssid ",
543 ether_sprintf(ni->ni_bssid));
544 ieee80211_print_essid(ic->ic_bss->ni_essid,
545 ni->ni_esslen);
546 printf(" channel %d start %uMb\n",
547 ieee80211_chan2ieee(ic, ni->ni_chan),
548 IEEE80211_RATE2MBS(ni->ni_rates.rs_rates[ni->ni_txrate]));
549 }
550 ic->ic_mgt_timer = 0;
551 (*ifp->if_start)(ifp);
552 break;
553 }
554 break;
555 }
556 if (ifp->if_link_state != linkstate) {
557 ifp->if_link_state = linkstate;
558 s = splnet();
559 rt_ifmsg(ifp);
560 splx(s);
561 }
562 return 0;
563 }
564