Home | History | Annotate | Line # | Download | only in net80211
ieee80211_proto.c revision 1.4
      1 /*	$NetBSD: ieee80211_proto.c,v 1.4 2003/09/28 02:35:20 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.4 2003/09/28 02:35:20 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 #endif
    147 }
    148 
    149 void
    150 ieee80211_print_essid(u_int8_t *essid, int len)
    151 {
    152 	int i;
    153 	u_int8_t *p;
    154 
    155 	if (len > IEEE80211_NWID_LEN)
    156 		len = IEEE80211_NWID_LEN;
    157 	/* determine printable or not */
    158 	for (i = 0, p = essid; i < len; i++, p++) {
    159 		if (*p < ' ' || *p > 0x7e)
    160 			break;
    161 	}
    162 	if (i == len) {
    163 		printf("\"");
    164 		for (i = 0, p = essid; i < len; i++, p++)
    165 			printf("%c", *p);
    166 		printf("\"");
    167 	} else {
    168 		printf("0x");
    169 		for (i = 0, p = essid; i < len; i++, p++)
    170 			printf("%02x", *p);
    171 	}
    172 }
    173 
    174 void
    175 ieee80211_dump_pkt(u_int8_t *buf, int len, int rate, int rssi)
    176 {
    177 	struct ieee80211_frame *wh;
    178 	int i;
    179 
    180 	wh = (struct ieee80211_frame *)buf;
    181 	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
    182 	case IEEE80211_FC1_DIR_NODS:
    183 		printf("NODS %s", ether_sprintf(wh->i_addr2));
    184 		printf("->%s", ether_sprintf(wh->i_addr1));
    185 		printf("(%s)", ether_sprintf(wh->i_addr3));
    186 		break;
    187 	case IEEE80211_FC1_DIR_TODS:
    188 		printf("TODS %s", ether_sprintf(wh->i_addr2));
    189 		printf("->%s", ether_sprintf(wh->i_addr3));
    190 		printf("(%s)", ether_sprintf(wh->i_addr1));
    191 		break;
    192 	case IEEE80211_FC1_DIR_FROMDS:
    193 		printf("FRDS %s", ether_sprintf(wh->i_addr3));
    194 		printf("->%s", ether_sprintf(wh->i_addr1));
    195 		printf("(%s)", ether_sprintf(wh->i_addr2));
    196 		break;
    197 	case IEEE80211_FC1_DIR_DSTODS:
    198 		printf("DSDS %s", ether_sprintf((u_int8_t *)&wh[1]));
    199 		printf("->%s", ether_sprintf(wh->i_addr3));
    200 		printf("(%s", ether_sprintf(wh->i_addr2));
    201 		printf("->%s)", ether_sprintf(wh->i_addr1));
    202 		break;
    203 	}
    204 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
    205 	case IEEE80211_FC0_TYPE_DATA:
    206 		printf(" data");
    207 		break;
    208 	case IEEE80211_FC0_TYPE_MGT:
    209 		printf(" %s", ieee80211_mgt_subtype_name[
    210 		    (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
    211 		    >> IEEE80211_FC0_SUBTYPE_SHIFT]);
    212 		break;
    213 	default:
    214 		printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
    215 		break;
    216 	}
    217 	if (wh->i_fc[1] & IEEE80211_FC1_WEP)
    218 		printf(" WEP");
    219 	if (rate >= 0)
    220 		printf(" %dM", rate / 2);
    221 	if (rssi >= 0)
    222 		printf(" +%d", rssi);
    223 	printf("\n");
    224 	if (len > 0) {
    225 		for (i = 0; i < len; i++) {
    226 			if ((i & 1) == 0)
    227 				printf(" ");
    228 			printf("%02x", buf[i]);
    229 		}
    230 		printf("\n");
    231 	}
    232 }
    233 
    234 int
    235 ieee80211_fix_rate(struct ieee80211com *ic, struct ieee80211_node *ni, int flags)
    236 {
    237 #define	RV(v)	((v) & IEEE80211_RATE_VAL)
    238 	int i, j, ignore, error;
    239 	int okrate, badrate;
    240 	struct ieee80211_rateset *srs, *nrs;
    241 	u_int8_t r;
    242 
    243 	error = 0;
    244 	okrate = badrate = 0;
    245 	srs = &ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
    246 	nrs = &ni->ni_rates;
    247 	for (i = 0; i < ni->ni_rates.rs_nrates; ) {
    248 		ignore = 0;
    249 		if (flags & IEEE80211_F_DOSORT) {
    250 			/*
    251 			 * Sort rates.
    252 			 */
    253 			for (j = i + 1; j < nrs->rs_nrates; j++) {
    254 				if (RV(nrs->rs_rates[i]) > RV(nrs->rs_rates[j])) {
    255 					r = nrs->rs_rates[i];
    256 					nrs->rs_rates[i] = nrs->rs_rates[j];
    257 					nrs->rs_rates[j] = r;
    258 				}
    259 			}
    260 		}
    261 		r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
    262 		badrate = r;
    263 		if (flags & IEEE80211_F_DOFRATE) {
    264 			/*
    265 			 * Apply fixed rate constraint.  Note that we do
    266 			 * not apply the constraint to basic rates as
    267 			 * otherwise we may not be able to associate if
    268 			 * the rate set we submit to the AP is invalid
    269 			 * (e.g. fix rate at 36Mb/s which is not a basic
    270 			 * rate for 11a operation).
    271 			 */
    272 			if ((nrs->rs_rates[i] & IEEE80211_RATE_BASIC) == 0 &&
    273 			    ic->ic_fixed_rate >= 0 &&
    274 			    r != RV(srs->rs_rates[ic->ic_fixed_rate]))
    275 				ignore++;
    276 		}
    277 		if (flags & IEEE80211_F_DONEGO) {
    278 			/*
    279 			 * Check against supported rates.
    280 			 */
    281 			for (j = 0; j < srs->rs_nrates; j++) {
    282 				if (r == RV(srs->rs_rates[j]))
    283 					break;
    284 			}
    285 			if (j == srs->rs_nrates) {
    286 				if (nrs->rs_rates[i] & IEEE80211_RATE_BASIC)
    287 					error++;
    288 				ignore++;
    289 			}
    290 		}
    291 		if (flags & IEEE80211_F_DODEL) {
    292 			/*
    293 			 * Delete unacceptable rates.
    294 			 */
    295 			if (ignore) {
    296 				nrs->rs_nrates--;
    297 				for (j = i; j < nrs->rs_nrates; j++)
    298 					nrs->rs_rates[j] = nrs->rs_rates[j + 1];
    299 				nrs->rs_rates[j] = 0;
    300 				continue;
    301 			}
    302 		}
    303 		if (!ignore)
    304 			okrate = nrs->rs_rates[i];
    305 		i++;
    306 	}
    307 	if (okrate == 0 || error != 0)
    308 		return badrate | IEEE80211_RATE_BASIC;
    309 	else
    310 		return RV(okrate);
    311 #undef RV
    312 }
    313 
    314 static int
    315 ieee80211_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int mgt)
    316 {
    317 	struct ifnet *ifp = &ic->ic_if;
    318 	struct ieee80211_node *ni;
    319 	enum ieee80211_state ostate;
    320 	ieee80211_node_critsec_decl(s);
    321 
    322 	ostate = ic->ic_state;
    323 	IEEE80211_DPRINTF(("%s: %s -> %s\n", __func__,
    324 		ieee80211_state_name[ostate], ieee80211_state_name[nstate]));
    325 	ic->ic_state = nstate;			/* state transition */
    326 	ni = ic->ic_bss;			/* NB: no reference held */
    327 	switch (nstate) {
    328 	case IEEE80211_S_INIT:
    329 		switch (ostate) {
    330 		case IEEE80211_S_INIT:
    331 			break;
    332 		case IEEE80211_S_RUN:
    333 			switch (ic->ic_opmode) {
    334 			case IEEE80211_M_STA:
    335 				IEEE80211_SEND_MGMT(ic, ni,
    336 				    IEEE80211_FC0_SUBTYPE_DISASSOC,
    337 				    IEEE80211_REASON_ASSOC_LEAVE);
    338 				break;
    339 			case IEEE80211_M_HOSTAP:
    340 				ieee80211_node_critsec_begin(ic, s);
    341 				TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
    342 					if (ni->ni_associd == 0)
    343 						continue;
    344 					IEEE80211_SEND_MGMT(ic, ni,
    345 					    IEEE80211_FC0_SUBTYPE_DISASSOC,
    346 					    IEEE80211_REASON_ASSOC_LEAVE);
    347 				}
    348 				ieee80211_node_critsec_end(ic, s);
    349 				break;
    350 			default:
    351 				break;
    352 			}
    353 			/* FALLTHRU */
    354 		case IEEE80211_S_ASSOC:
    355 			switch (ic->ic_opmode) {
    356 			case IEEE80211_M_STA:
    357 				IEEE80211_SEND_MGMT(ic, ni,
    358 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
    359 				    IEEE80211_REASON_AUTH_LEAVE);
    360 				break;
    361 			case IEEE80211_M_HOSTAP:
    362 				ieee80211_node_critsec_begin(ic, s);
    363 				TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
    364 					IEEE80211_SEND_MGMT(ic, ni,
    365 					    IEEE80211_FC0_SUBTYPE_DEAUTH,
    366 					    IEEE80211_REASON_AUTH_LEAVE);
    367 				}
    368 				ieee80211_node_critsec_end(ic, s);
    369 				break;
    370 			default:
    371 				break;
    372 			}
    373 			/* FALLTHRU */
    374 		case IEEE80211_S_AUTH:
    375 		case IEEE80211_S_SCAN:
    376 			ic->ic_mgt_timer = 0;
    377 #ifdef __FreeBSD__
    378 			IF_DRAIN(&ic->ic_mgtq);
    379 #else
    380 			IF_PURGE(&ic->ic_mgtq);
    381 #endif
    382 			if (ic->ic_wep_ctx != NULL) {
    383 				free(ic->ic_wep_ctx, M_DEVBUF);
    384 				ic->ic_wep_ctx = NULL;
    385 			}
    386 			ieee80211_free_allnodes(ic);
    387 			break;
    388 		}
    389 		break;
    390 	case IEEE80211_S_SCAN:
    391 		ic->ic_flags &= ~IEEE80211_F_SIBSS;
    392 		/* initialize bss for probe request */
    393 		IEEE80211_ADDR_COPY(ni->ni_macaddr, ifp->if_broadcastaddr);
    394 		IEEE80211_ADDR_COPY(ni->ni_bssid, ifp->if_broadcastaddr);
    395 		ni->ni_rates = ic->ic_sup_rates[
    396 			ieee80211_chan2mode(ic, ni->ni_chan)];
    397 		ni->ni_associd = 0;
    398 		ni->ni_rstamp = 0;
    399 		switch (ostate) {
    400 		case IEEE80211_S_INIT:
    401 			if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
    402 			    ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
    403 				/*
    404 				 * AP operation and we already have a channel;
    405 				 * bypass the scan and startup immediately.
    406 				 */
    407 				ieee80211_create_ibss(ic, ic->ic_des_chan);
    408 			} else {
    409 				ieee80211_begin_scan(ifp);
    410 			}
    411 			break;
    412 		case IEEE80211_S_SCAN:
    413 			/* scan next */
    414 			if (ic->ic_flags & IEEE80211_F_ASCAN) {
    415 				IEEE80211_SEND_MGMT(ic, ni,
    416 				    IEEE80211_FC0_SUBTYPE_PROBE_REQ, 0);
    417 			}
    418 			break;
    419 		case IEEE80211_S_RUN:
    420 			/* beacon miss */
    421 			if (ifp->if_flags & IFF_DEBUG) {
    422 				/* XXX bssid clobbered above */
    423 				if_printf(ifp, "no recent beacons from %s;"
    424 				    " rescanning\n",
    425 				    ether_sprintf(ic->ic_bss->ni_bssid));
    426 			}
    427 			ieee80211_free_allnodes(ic);
    428 			/* FALLTHRU */
    429 		case IEEE80211_S_AUTH:
    430 		case IEEE80211_S_ASSOC:
    431 			/* timeout restart scan */
    432 			ni = ieee80211_find_node(ic, ic->ic_bss->ni_macaddr);
    433 			if (ni != NULL) {
    434 				ni->ni_fails++;
    435 				ieee80211_unref_node(&ni);
    436 			}
    437 			ieee80211_begin_scan(ifp);
    438 			break;
    439 		}
    440 		break;
    441 	case IEEE80211_S_AUTH:
    442 		switch (ostate) {
    443 		case IEEE80211_S_INIT:
    444 			IEEE80211_DPRINTF(("%s: invalid transition\n",
    445 				__func__));
    446 			break;
    447 		case IEEE80211_S_SCAN:
    448 			IEEE80211_SEND_MGMT(ic, ni,
    449 			    IEEE80211_FC0_SUBTYPE_AUTH, 1);
    450 			break;
    451 		case IEEE80211_S_AUTH:
    452 		case IEEE80211_S_ASSOC:
    453 			switch (mgt) {
    454 			case IEEE80211_FC0_SUBTYPE_AUTH:
    455 				/* ??? */
    456 				IEEE80211_SEND_MGMT(ic, ni,
    457 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
    458 				break;
    459 			case IEEE80211_FC0_SUBTYPE_DEAUTH:
    460 				/* ignore and retry scan on timeout */
    461 				break;
    462 			}
    463 			break;
    464 		case IEEE80211_S_RUN:
    465 			switch (mgt) {
    466 			case IEEE80211_FC0_SUBTYPE_AUTH:
    467 				IEEE80211_SEND_MGMT(ic, ni,
    468 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
    469 				ic->ic_state = ostate;	/* stay RUN */
    470 				break;
    471 			case IEEE80211_FC0_SUBTYPE_DEAUTH:
    472 				/* try to reauth */
    473 				IEEE80211_SEND_MGMT(ic, ni,
    474 				    IEEE80211_FC0_SUBTYPE_AUTH, 1);
    475 				break;
    476 			}
    477 			break;
    478 		}
    479 		break;
    480 	case IEEE80211_S_ASSOC:
    481 		switch (ostate) {
    482 		case IEEE80211_S_INIT:
    483 		case IEEE80211_S_SCAN:
    484 		case IEEE80211_S_ASSOC:
    485 			IEEE80211_DPRINTF(("%s: invalid transition\n",
    486 				__func__));
    487 			break;
    488 		case IEEE80211_S_AUTH:
    489 			IEEE80211_SEND_MGMT(ic, ni,
    490 			    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
    491 			break;
    492 		case IEEE80211_S_RUN:
    493 			IEEE80211_SEND_MGMT(ic, ni,
    494 			    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 1);
    495 			break;
    496 		}
    497 		break;
    498 	case IEEE80211_S_RUN:
    499 		switch (ostate) {
    500 		case IEEE80211_S_INIT:
    501 		case IEEE80211_S_AUTH:
    502 		case IEEE80211_S_RUN:
    503 			IEEE80211_DPRINTF(("%s: invalid transition\n",
    504 				__func__));
    505 			break;
    506 		case IEEE80211_S_SCAN:		/* adhoc/hostap mode */
    507 		case IEEE80211_S_ASSOC:		/* infra mode */
    508 			KASSERT(ni->ni_txrate < ni->ni_rates.rs_nrates,
    509 				("%s: bogus xmit rate %u setup\n", __func__,
    510 					ni->ni_txrate));
    511 			if (ifp->if_flags & IFF_DEBUG) {
    512 				if_printf(ifp, " ");
    513 				if (ic->ic_opmode == IEEE80211_M_STA)
    514 					printf("associated ");
    515 				else
    516 					printf("synchronized ");
    517 				printf("with %s ssid ",
    518 				    ether_sprintf(ni->ni_bssid));
    519 				ieee80211_print_essid(ic->ic_bss->ni_essid,
    520 				    ni->ni_esslen);
    521 				printf(" channel %d start %uMb\n",
    522 					ieee80211_chan2ieee(ic, ni->ni_chan),
    523 					IEEE80211_RATE2MBS(ni->ni_rates.rs_rates[ni->ni_txrate]));
    524 			}
    525 			ic->ic_mgt_timer = 0;
    526 			(*ifp->if_start)(ifp);
    527 			break;
    528 		}
    529 		break;
    530 	}
    531 	return 0;
    532 }
    533