2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * Alternatively, this software may be distributed under the terms of the
18 * GNU General Public License ("GPL") version 2 as published by the Free
19 * Software Foundation.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * $FreeBSD: src/sys/net80211/ieee80211_proto.c,v 1.8 2004/04/02 20:22:25 sam Exp $
33 * $DragonFly: src/sys/netproto/802_11/Attic/ieee80211_proto.c,v 1.1 2004/07/26 16:30:17 joerg Exp $
37 * IEEE 802.11 protocol support.
42 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <sys/malloc.h>
46 #include <sys/kernel.h>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
49 #include <sys/endian.h>
50 #include <sys/errno.h>
53 #include <sys/sysctl.h>
55 #include <machine/atomic.h>
58 #include <net/if_dl.h>
59 #include <net/if_media.h>
60 #include <net/if_arp.h>
61 #include <net/ethernet.h>
62 #include <net/if_llc.h>
64 #include <netproto/802_11/ieee80211_var.h>
69 #include <netinet/in.h>
70 #include <netinet/if_ether.h>
73 #define IEEE80211_RATE2MBS(r) (((r) & IEEE80211_RATE_VAL) / 2)
75 const char *ieee80211_mgt_subtype_name[] = {
76 "assoc_req", "assoc_resp", "reassoc_req", "reassoc_resp",
77 "probe_req", "probe_resp", "reserved#6", "reserved#7",
78 "beacon", "atim", "disassoc", "auth",
79 "deauth", "reserved#13", "reserved#14", "reserved#15"
81 const char *ieee80211_state_name[IEEE80211_S_MAX] = {
82 "INIT", /* IEEE80211_S_INIT */
83 "SCAN", /* IEEE80211_S_SCAN */
84 "AUTH", /* IEEE80211_S_AUTH */
85 "ASSOC", /* IEEE80211_S_ASSOC */
86 "RUN" /* IEEE80211_S_RUN */
89 static int ieee80211_newstate(struct ieee80211com *, enum ieee80211_state, int);
92 ieee80211_proto_attach(struct ifnet *ifp)
94 struct ieee80211com *ic = (void *)ifp;
96 ifp->if_hdrlen = sizeof(struct ieee80211_frame);
99 ic->ic_rtsthreshold = IEEE80211_RTS_DEFAULT;
101 ic->ic_rtsthreshold = IEEE80211_RTS_MAX;
103 ic->ic_fragthreshold = 2346; /* XXX not used yet */
104 ic->ic_fixed_rate = -1; /* no fixed rate */
105 ic->ic_protmode = IEEE80211_PROT_CTSONLY;
107 /* protocol state change handler */
108 ic->ic_newstate = ieee80211_newstate;
110 /* initialize management frame handlers */
111 ic->ic_recv_mgmt = ieee80211_recv_mgmt;
112 ic->ic_send_mgmt = ieee80211_send_mgmt;
116 ieee80211_proto_detach(struct ifnet *ifp)
118 struct ieee80211com *ic = (void *)ifp;
120 IF_DRAIN(&ic->ic_mgtq);
124 ieee80211_print_essid(uint8_t *essid, int len)
129 if (len > IEEE80211_NWID_LEN)
130 len = IEEE80211_NWID_LEN;
131 /* determine printable or not */
132 for (i = 0, p = essid; i < len; i++, p++) {
133 if (*p < ' ' || *p > 0x7e)
138 for (i = 0, p = essid; i < len; i++, p++)
143 for (i = 0, p = essid; i < len; i++, p++)
149 ieee80211_dump_pkt(uint8_t *buf, int len, int rate, int rssi)
151 struct ieee80211_frame *wh;
154 wh = (struct ieee80211_frame *)buf;
155 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
156 case IEEE80211_FC1_DIR_NODS:
157 printf("NODS %6D->%6D(%6D)", wh->i_addr2, ":",
158 wh->i_addr1, ":", wh->i_addr3, ":");
160 case IEEE80211_FC1_DIR_TODS:
161 printf("TODS %6D->%6D(%6D)", wh->i_addr2, ":",
162 wh->i_addr3, ":", wh->i_addr1, ":");
164 case IEEE80211_FC1_DIR_FROMDS:
165 printf("FRDS %6D->%6D(%6D)", wh->i_addr3, ":",
166 wh->i_addr1, ":", wh->i_addr2, ":");
168 case IEEE80211_FC1_DIR_DSTODS:
169 printf("DSDS %6D->%6D(%6D->%6D)", (uint8_t *)&wh[1], ":",
170 wh->i_addr3, ":", wh->i_addr2, ":", wh->i_addr1, ":");
173 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
174 case IEEE80211_FC0_TYPE_DATA:
177 case IEEE80211_FC0_TYPE_MGT:
178 printf(" %s", ieee80211_mgt_subtype_name[
179 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
180 >> IEEE80211_FC0_SUBTYPE_SHIFT]);
183 printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
186 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
189 printf(" %dM", rate / 2);
191 printf(" +%d", rssi);
194 for (i = 0; i < len; i++) {
197 printf("%02x", buf[i]);
204 ieee80211_fix_rate(struct ieee80211com *ic, struct ieee80211_node *ni, int flags)
206 #define RV(v) ((v) & IEEE80211_RATE_VAL)
207 int i, j, ignore, error;
209 struct ieee80211_rateset *srs, *nrs;
213 okrate = badrate = 0;
214 srs = &ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
216 for (i = 0; i < nrs->rs_nrates; ) {
218 if (flags & IEEE80211_F_DOSORT) {
222 for (j = i + 1; j < nrs->rs_nrates; j++) {
223 if (RV(nrs->rs_rates[i]) > RV(nrs->rs_rates[j])) {
224 r = nrs->rs_rates[i];
225 nrs->rs_rates[i] = nrs->rs_rates[j];
226 nrs->rs_rates[j] = r;
230 r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
232 if (flags & IEEE80211_F_DOFRATE) {
234 * Apply fixed rate constraint. Note that we do
235 * not apply the constraint to basic rates as
236 * otherwise we may not be able to associate if
237 * the rate set we submit to the AP is invalid
238 * (e.g. fix rate at 36Mb/s which is not a basic
239 * rate for 11a operation).
241 if ((nrs->rs_rates[i] & IEEE80211_RATE_BASIC) == 0 &&
242 ic->ic_fixed_rate >= 0 &&
243 r != RV(srs->rs_rates[ic->ic_fixed_rate]))
246 if (flags & IEEE80211_F_DONEGO) {
248 * Check against supported rates.
250 for (j = 0; j < srs->rs_nrates; j++) {
251 if (r == RV(srs->rs_rates[j])) {
253 * Overwrite with the supported rate
254 * value so any basic rate bit is set.
255 * This insures that response we send
256 * to stations have the necessary basic
259 nrs->rs_rates[i] = srs->rs_rates[j];
263 if (j == srs->rs_nrates) {
265 * A rate in the node's rate set is not
266 * supported. If this is a basic rate and we
267 * are operating as an AP then this is an error.
268 * Otherwise we just discard/ignore the rate.
269 * Note that this is important for 11b stations
270 * when they want to associate with an 11g AP.
272 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
273 (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
278 if (flags & IEEE80211_F_DODEL) {
280 * Delete unacceptable rates.
284 for (j = i; j < nrs->rs_nrates; j++)
285 nrs->rs_rates[j] = nrs->rs_rates[j + 1];
286 nrs->rs_rates[j] = 0;
291 okrate = nrs->rs_rates[i];
294 if (okrate == 0 || error != 0)
295 return badrate | IEEE80211_RATE_BASIC;
302 ieee80211_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int mgt)
304 struct ifnet *ifp = &ic->ic_if;
305 struct ieee80211_node *ni;
306 enum ieee80211_state ostate;
307 struct lwkt_tokref ilock;
309 ostate = ic->ic_state;
310 IEEE80211_DPRINTF(("%s: %s -> %s\n", __func__,
311 ieee80211_state_name[ostate], ieee80211_state_name[nstate]));
312 ic->ic_state = nstate; /* state transition */
313 ni = ic->ic_bss; /* NB: no reference held */
315 case IEEE80211_S_INIT:
317 case IEEE80211_S_INIT:
319 case IEEE80211_S_RUN:
320 switch (ic->ic_opmode) {
321 case IEEE80211_M_STA:
322 IEEE80211_SEND_MGMT(ic, ni,
323 IEEE80211_FC0_SUBTYPE_DISASSOC,
324 IEEE80211_REASON_ASSOC_LEAVE);
326 case IEEE80211_M_HOSTAP:
327 lwkt_gettoken(&ilock, &ic->ic_nodetoken);
328 TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
329 if (ni->ni_associd == 0)
331 IEEE80211_SEND_MGMT(ic, ni,
332 IEEE80211_FC0_SUBTYPE_DISASSOC,
333 IEEE80211_REASON_ASSOC_LEAVE);
335 lwkt_reltoken(&ilock);
341 case IEEE80211_S_ASSOC:
342 switch (ic->ic_opmode) {
343 case IEEE80211_M_STA:
344 IEEE80211_SEND_MGMT(ic, ni,
345 IEEE80211_FC0_SUBTYPE_DEAUTH,
346 IEEE80211_REASON_AUTH_LEAVE);
348 case IEEE80211_M_HOSTAP:
349 lwkt_gettoken(&ilock, &ic->ic_nodetoken);
350 TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
351 IEEE80211_SEND_MGMT(ic, ni,
352 IEEE80211_FC0_SUBTYPE_DEAUTH,
353 IEEE80211_REASON_AUTH_LEAVE);
355 lwkt_reltoken(&ilock);
361 case IEEE80211_S_AUTH:
362 case IEEE80211_S_SCAN:
363 ic->ic_mgt_timer = 0;
364 IF_DRAIN(&ic->ic_mgtq);
365 if (ic->ic_wep_ctx != NULL) {
366 free(ic->ic_wep_ctx, M_DEVBUF);
367 ic->ic_wep_ctx = NULL;
369 ieee80211_free_allnodes(ic);
373 case IEEE80211_S_SCAN:
374 ic->ic_flags &= ~IEEE80211_F_SIBSS;
375 /* initialize bss for probe request */
376 IEEE80211_ADDR_COPY(ni->ni_macaddr, ifp->if_broadcastaddr);
377 IEEE80211_ADDR_COPY(ni->ni_bssid, ifp->if_broadcastaddr);
378 ni->ni_rates = ic->ic_sup_rates[
379 ieee80211_chan2mode(ic, ni->ni_chan)];
383 case IEEE80211_S_INIT:
384 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
385 ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
387 * AP operation and we already have a channel;
388 * bypass the scan and startup immediately.
390 ieee80211_create_ibss(ic, ic->ic_des_chan);
392 ieee80211_begin_scan(ifp);
395 case IEEE80211_S_SCAN:
397 if (ic->ic_flags & IEEE80211_F_ASCAN) {
398 IEEE80211_SEND_MGMT(ic, ni,
399 IEEE80211_FC0_SUBTYPE_PROBE_REQ, 0);
402 case IEEE80211_S_RUN:
404 if (ifp->if_flags & IFF_DEBUG) {
405 /* XXX bssid clobbered above */
406 if_printf(ifp, "no recent beacons from %6D;"
408 ic->ic_bss->ni_bssid, ":");
410 ieee80211_free_allnodes(ic);
412 case IEEE80211_S_AUTH:
413 case IEEE80211_S_ASSOC:
414 /* timeout restart scan */
415 ni = ieee80211_find_node(ic, ic->ic_bss->ni_macaddr);
418 ieee80211_unref_node(&ni);
420 ieee80211_begin_scan(ifp);
424 case IEEE80211_S_AUTH:
426 case IEEE80211_S_INIT:
427 IEEE80211_DPRINTF(("%s: invalid transition\n",
430 case IEEE80211_S_SCAN:
431 IEEE80211_SEND_MGMT(ic, ni,
432 IEEE80211_FC0_SUBTYPE_AUTH, 1);
434 case IEEE80211_S_AUTH:
435 case IEEE80211_S_ASSOC:
437 case IEEE80211_FC0_SUBTYPE_AUTH:
439 IEEE80211_SEND_MGMT(ic, ni,
440 IEEE80211_FC0_SUBTYPE_AUTH, 2);
442 case IEEE80211_FC0_SUBTYPE_DEAUTH:
443 /* ignore and retry scan on timeout */
447 case IEEE80211_S_RUN:
449 case IEEE80211_FC0_SUBTYPE_AUTH:
450 IEEE80211_SEND_MGMT(ic, ni,
451 IEEE80211_FC0_SUBTYPE_AUTH, 2);
452 ic->ic_state = ostate; /* stay RUN */
454 case IEEE80211_FC0_SUBTYPE_DEAUTH:
456 IEEE80211_SEND_MGMT(ic, ni,
457 IEEE80211_FC0_SUBTYPE_AUTH, 1);
463 case IEEE80211_S_ASSOC:
465 case IEEE80211_S_INIT:
466 case IEEE80211_S_SCAN:
467 case IEEE80211_S_ASSOC:
468 IEEE80211_DPRINTF(("%s: invalid transition\n",
471 case IEEE80211_S_AUTH:
472 IEEE80211_SEND_MGMT(ic, ni,
473 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
475 case IEEE80211_S_RUN:
476 IEEE80211_SEND_MGMT(ic, ni,
477 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 1);
481 case IEEE80211_S_RUN:
483 case IEEE80211_S_INIT:
484 case IEEE80211_S_AUTH:
485 case IEEE80211_S_RUN:
486 IEEE80211_DPRINTF(("%s: invalid transition\n",
489 case IEEE80211_S_SCAN: /* adhoc/hostap mode */
490 case IEEE80211_S_ASSOC: /* infra mode */
491 KASSERT(ni->ni_txrate < ni->ni_rates.rs_nrates,
492 ("%s: bogus xmit rate %u setup\n", __func__,
494 if (ifp->if_flags & IFF_DEBUG) {
496 if (ic->ic_opmode == IEEE80211_M_STA)
497 printf("associated ");
499 printf("synchronized ");
500 printf("with %6D ssid ", ni->ni_bssid, ":");
501 ieee80211_print_essid(ic->ic_bss->ni_essid,
503 printf(" channel %d start %uMb\n",
504 ieee80211_chan2ieee(ic, ni->ni_chan),
505 IEEE80211_RATE2MBS(ni->ni_rates.rs_rates[ni->ni_txrate]));
507 ic->ic_mgt_timer = 0;
508 (*ifp->if_start)(ifp);