ieee80211_proto.c revision 1.5 1 /* $NetBSD: ieee80211_proto.c,v 1.5 2003/10/13 04:23:56 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.3 2003/07/20 21:36:08 sam Exp $");
37 #else
38 __KERNEL_RCSID(0, "$NetBSD: ieee80211_proto.c,v 1.5 2003/10/13 04:23:56 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_name, "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 < ni->ni_rates.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 if (nrs->rs_rates[i] & IEEE80211_RATE_BASIC)
288 error++;
289 ignore++;
290 }
291 }
292 if (flags & IEEE80211_F_DODEL) {
293 /*
294 * Delete unacceptable rates.
295 */
296 if (ignore) {
297 nrs->rs_nrates--;
298 for (j = i; j < nrs->rs_nrates; j++)
299 nrs->rs_rates[j] = nrs->rs_rates[j + 1];
300 nrs->rs_rates[j] = 0;
301 continue;
302 }
303 }
304 if (!ignore)
305 okrate = nrs->rs_rates[i];
306 i++;
307 }
308 if (okrate == 0 || error != 0)
309 return badrate | IEEE80211_RATE_BASIC;
310 else
311 return RV(okrate);
312 #undef RV
313 }
314
315 static int
316 ieee80211_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int mgt)
317 {
318 struct ifnet *ifp = &ic->ic_if;
319 struct ieee80211_node *ni;
320 enum ieee80211_state ostate;
321 ieee80211_node_critsec_decl(s);
322
323 ostate = ic->ic_state;
324 IEEE80211_DPRINTF(("%s: %s -> %s\n", __func__,
325 ieee80211_state_name[ostate], ieee80211_state_name[nstate]));
326 ic->ic_state = nstate; /* state transition */
327 ni = ic->ic_bss; /* NB: no reference held */
328 switch (nstate) {
329 case IEEE80211_S_INIT:
330 switch (ostate) {
331 case IEEE80211_S_INIT:
332 break;
333 case IEEE80211_S_RUN:
334 switch (ic->ic_opmode) {
335 case IEEE80211_M_STA:
336 IEEE80211_SEND_MGMT(ic, ni,
337 IEEE80211_FC0_SUBTYPE_DISASSOC,
338 IEEE80211_REASON_ASSOC_LEAVE);
339 break;
340 case IEEE80211_M_HOSTAP:
341 ieee80211_node_critsec_begin(ic, s);
342 TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
343 if (ni->ni_associd == 0)
344 continue;
345 IEEE80211_SEND_MGMT(ic, ni,
346 IEEE80211_FC0_SUBTYPE_DISASSOC,
347 IEEE80211_REASON_ASSOC_LEAVE);
348 }
349 ieee80211_node_critsec_end(ic, s);
350 break;
351 default:
352 break;
353 }
354 /* FALLTHRU */
355 case IEEE80211_S_ASSOC:
356 switch (ic->ic_opmode) {
357 case IEEE80211_M_STA:
358 IEEE80211_SEND_MGMT(ic, ni,
359 IEEE80211_FC0_SUBTYPE_DEAUTH,
360 IEEE80211_REASON_AUTH_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 IEEE80211_SEND_MGMT(ic, ni,
366 IEEE80211_FC0_SUBTYPE_DEAUTH,
367 IEEE80211_REASON_AUTH_LEAVE);
368 }
369 ieee80211_node_critsec_end(ic, s);
370 break;
371 default:
372 break;
373 }
374 /* FALLTHRU */
375 case IEEE80211_S_AUTH:
376 case IEEE80211_S_SCAN:
377 ic->ic_mgt_timer = 0;
378 #ifdef __FreeBSD__
379 IF_DRAIN(&ic->ic_mgtq);
380 #else
381 IF_PURGE(&ic->ic_mgtq);
382 IF_PURGE(&ic->ic_pwrsaveq);
383 #endif
384 if (ic->ic_wep_ctx != NULL) {
385 free(ic->ic_wep_ctx, M_DEVBUF);
386 ic->ic_wep_ctx = NULL;
387 }
388 ieee80211_free_allnodes(ic);
389 break;
390 }
391 break;
392 case IEEE80211_S_SCAN:
393 ic->ic_flags &= ~IEEE80211_F_SIBSS;
394 /* initialize bss for probe request */
395 IEEE80211_ADDR_COPY(ni->ni_macaddr, ifp->if_broadcastaddr);
396 IEEE80211_ADDR_COPY(ni->ni_bssid, ifp->if_broadcastaddr);
397 ni->ni_rates = ic->ic_sup_rates[
398 ieee80211_chan2mode(ic, ni->ni_chan)];
399 ni->ni_associd = 0;
400 ni->ni_rstamp = 0;
401 switch (ostate) {
402 case IEEE80211_S_INIT:
403 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
404 ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
405 /*
406 * AP operation and we already have a channel;
407 * bypass the scan and startup immediately.
408 */
409 ieee80211_create_ibss(ic, ic->ic_des_chan);
410 } else {
411 ieee80211_begin_scan(ifp);
412 }
413 break;
414 case IEEE80211_S_SCAN:
415 /* scan next */
416 if (ic->ic_flags & IEEE80211_F_ASCAN) {
417 IEEE80211_SEND_MGMT(ic, ni,
418 IEEE80211_FC0_SUBTYPE_PROBE_REQ, 0);
419 }
420 break;
421 case IEEE80211_S_RUN:
422 /* beacon miss */
423 if (ifp->if_flags & IFF_DEBUG) {
424 /* XXX bssid clobbered above */
425 if_printf(ifp, "no recent beacons from %s;"
426 " rescanning\n",
427 ether_sprintf(ic->ic_bss->ni_bssid));
428 }
429 ieee80211_free_allnodes(ic);
430 /* FALLTHRU */
431 case IEEE80211_S_AUTH:
432 case IEEE80211_S_ASSOC:
433 /* timeout restart scan */
434 ni = ieee80211_find_node(ic, ic->ic_bss->ni_macaddr);
435 if (ni != NULL) {
436 ni->ni_fails++;
437 ieee80211_unref_node(&ni);
438 }
439 ieee80211_begin_scan(ifp);
440 break;
441 }
442 break;
443 case IEEE80211_S_AUTH:
444 switch (ostate) {
445 case IEEE80211_S_INIT:
446 IEEE80211_DPRINTF(("%s: invalid transition\n",
447 __func__));
448 break;
449 case IEEE80211_S_SCAN:
450 IEEE80211_SEND_MGMT(ic, ni,
451 IEEE80211_FC0_SUBTYPE_AUTH, 1);
452 break;
453 case IEEE80211_S_AUTH:
454 case IEEE80211_S_ASSOC:
455 switch (mgt) {
456 case IEEE80211_FC0_SUBTYPE_AUTH:
457 /* ??? */
458 IEEE80211_SEND_MGMT(ic, ni,
459 IEEE80211_FC0_SUBTYPE_AUTH, 2);
460 break;
461 case IEEE80211_FC0_SUBTYPE_DEAUTH:
462 /* ignore and retry scan on timeout */
463 break;
464 }
465 break;
466 case IEEE80211_S_RUN:
467 switch (mgt) {
468 case IEEE80211_FC0_SUBTYPE_AUTH:
469 IEEE80211_SEND_MGMT(ic, ni,
470 IEEE80211_FC0_SUBTYPE_AUTH, 2);
471 ic->ic_state = ostate; /* stay RUN */
472 break;
473 case IEEE80211_FC0_SUBTYPE_DEAUTH:
474 /* try to reauth */
475 IEEE80211_SEND_MGMT(ic, ni,
476 IEEE80211_FC0_SUBTYPE_AUTH, 1);
477 break;
478 }
479 break;
480 }
481 break;
482 case IEEE80211_S_ASSOC:
483 switch (ostate) {
484 case IEEE80211_S_INIT:
485 case IEEE80211_S_SCAN:
486 case IEEE80211_S_ASSOC:
487 IEEE80211_DPRINTF(("%s: invalid transition\n",
488 __func__));
489 break;
490 case IEEE80211_S_AUTH:
491 IEEE80211_SEND_MGMT(ic, ni,
492 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
493 break;
494 case IEEE80211_S_RUN:
495 IEEE80211_SEND_MGMT(ic, ni,
496 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 1);
497 break;
498 }
499 break;
500 case IEEE80211_S_RUN:
501 switch (ostate) {
502 case IEEE80211_S_INIT:
503 case IEEE80211_S_AUTH:
504 case IEEE80211_S_RUN:
505 IEEE80211_DPRINTF(("%s: invalid transition\n",
506 __func__));
507 break;
508 case IEEE80211_S_SCAN: /* adhoc/hostap mode */
509 case IEEE80211_S_ASSOC: /* infra mode */
510 KASSERT(ni->ni_txrate < ni->ni_rates.rs_nrates,
511 ("%s: bogus xmit rate %u setup\n", __func__,
512 ni->ni_txrate));
513 if (ifp->if_flags & IFF_DEBUG) {
514 if_printf(ifp, " ");
515 if (ic->ic_opmode == IEEE80211_M_STA)
516 printf("associated ");
517 else
518 printf("synchronized ");
519 printf("with %s ssid ",
520 ether_sprintf(ni->ni_bssid));
521 ieee80211_print_essid(ic->ic_bss->ni_essid,
522 ni->ni_esslen);
523 printf(" channel %d start %uMb\n",
524 ieee80211_chan2ieee(ic, ni->ni_chan),
525 IEEE80211_RATE2MBS(ni->ni_rates.rs_rates[ni->ni_txrate]));
526 }
527 ic->ic_mgt_timer = 0;
528 (*ifp->if_start)(ifp);
529 break;
530 }
531 break;
532 }
533 return 0;
534 }
535