Home | History | Annotate | Line # | Download | only in net80211
ieee80211_proto.c revision 1.2
      1 /*-
      2  * Copyright (c) 2001 Atsushi Onoe
      3  * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      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.
     16  *
     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.
     20  *
     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.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_proto.c,v 1.3 2003/07/20 21:36:08 sam Exp $");
     35 
     36 /*
     37  * IEEE 802.11 protocol support.
     38  */
     39 
     40 #include "opt_inet.h"
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/mbuf.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>
     51 #include <sys/bus.h>
     52 #include <sys/proc.h>
     53 #include <sys/sysctl.h>
     54 
     55 #ifdef __FreeBSD__
     56 #include <machine/atomic.h>
     57 #endif
     58 
     59 #include <net/if.h>
     60 #include <net/if_dl.h>
     61 #include <net/if_media.h>
     62 #include <net/if_arp.h>
     63 #ifdef __FreeBSD__
     64 #include <net/ethernet.h>
     65 #endif
     66 #include <net/if_llc.h>
     67 
     68 #include <net80211/ieee80211_var.h>
     69 
     70 #include <net/bpf.h>
     71 
     72 #ifdef INET
     73 #include <netinet/in.h>
     74 #include <netinet/if_ether.h>
     75 #endif
     76 
     77 #define	IEEE80211_RATE2MBS(r)	(((r) & IEEE80211_RATE_VAL) / 2)
     78 
     79 const char *ieee80211_mgt_subtype_name[] = {
     80 	"assoc_req",	"assoc_resp",	"reassoc_req",	"reassoc_resp",
     81 	"probe_req",	"probe_resp",	"reserved#6",	"reserved#7",
     82 	"beacon",	"atim",		"disassoc",	"auth",
     83 	"deauth",	"reserved#13",	"reserved#14",	"reserved#15"
     84 };
     85 const char *ieee80211_state_name[IEEE80211_S_MAX] = {
     86 	"INIT",		/* IEEE80211_S_INIT */
     87 	"SCAN",		/* IEEE80211_S_SCAN */
     88 	"AUTH",		/* IEEE80211_S_AUTH */
     89 	"ASSOC",	/* IEEE80211_S_ASSOC */
     90 	"RUN"		/* IEEE80211_S_RUN */
     91 };
     92 
     93 static int ieee80211_newstate(struct ieee80211com *, enum ieee80211_state, int);
     94 
     95 void
     96 ieee80211_proto_attach(struct ifnet *ifp)
     97 {
     98 	struct ieee80211com *ic = (void *)ifp;
     99 
    100 	ifp->if_hdrlen = sizeof(struct ieee80211_frame);
    101 
    102 #ifdef notdef
    103 	ic->ic_rtsthreshold = IEEE80211_RTS_DEFAULT;
    104 #else
    105 	ic->ic_rtsthreshold = IEEE80211_RTS_MAX;
    106 #endif
    107 	ic->ic_fragthreshold = 2346;		/* XXX not used yet */
    108 	ic->ic_fixed_rate = -1;			/* no fixed rate */
    109 
    110 #ifdef __FreeBSD__
    111 	mtx_init(&ic->ic_mgtq.ifq_mtx, ifp->if_name, "mgmt send q", MTX_DEF);
    112 #endif
    113 
    114 	/* protocol state change handler */
    115 	ic->ic_newstate = ieee80211_newstate;
    116 
    117 	/* initialize management frame handlers */
    118 	ic->ic_recv_mgmt = ieee80211_recv_mgmt;
    119 	ic->ic_send_mgmt = ieee80211_send_mgmt;
    120 }
    121 
    122 void
    123 ieee80211_proto_detach(struct ifnet *ifp)
    124 {
    125 	struct ieee80211com *ic = (void *)ifp;
    126 
    127 	IF_DRAIN(&ic->ic_mgtq);
    128 	mtx_destroy(&ic->ic_mgtq.ifq_mtx);
    129 }
    130 
    131 void
    132 ieee80211_print_essid(u_int8_t *essid, int len)
    133 {
    134 	int i;
    135 	u_int8_t *p;
    136 
    137 	if (len > IEEE80211_NWID_LEN)
    138 		len = IEEE80211_NWID_LEN;
    139 	/* determine printable or not */
    140 	for (i = 0, p = essid; i < len; i++, p++) {
    141 		if (*p < ' ' || *p > 0x7e)
    142 			break;
    143 	}
    144 	if (i == len) {
    145 		printf("\"");
    146 		for (i = 0, p = essid; i < len; i++, p++)
    147 			printf("%c", *p);
    148 		printf("\"");
    149 	} else {
    150 		printf("0x");
    151 		for (i = 0, p = essid; i < len; i++, p++)
    152 			printf("%02x", *p);
    153 	}
    154 }
    155 
    156 void
    157 ieee80211_dump_pkt(u_int8_t *buf, int len, int rate, int rssi)
    158 {
    159 	struct ieee80211_frame *wh;
    160 	int i;
    161 
    162 	wh = (struct ieee80211_frame *)buf;
    163 	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
    164 	case IEEE80211_FC1_DIR_NODS:
    165 		printf("NODS %s", ether_sprintf(wh->i_addr2));
    166 		printf("->%s", ether_sprintf(wh->i_addr1));
    167 		printf("(%s)", ether_sprintf(wh->i_addr3));
    168 		break;
    169 	case IEEE80211_FC1_DIR_TODS:
    170 		printf("TODS %s", ether_sprintf(wh->i_addr2));
    171 		printf("->%s", ether_sprintf(wh->i_addr3));
    172 		printf("(%s)", ether_sprintf(wh->i_addr1));
    173 		break;
    174 	case IEEE80211_FC1_DIR_FROMDS:
    175 		printf("FRDS %s", ether_sprintf(wh->i_addr3));
    176 		printf("->%s", ether_sprintf(wh->i_addr1));
    177 		printf("(%s)", ether_sprintf(wh->i_addr2));
    178 		break;
    179 	case IEEE80211_FC1_DIR_DSTODS:
    180 		printf("DSDS %s", ether_sprintf((u_int8_t *)&wh[1]));
    181 		printf("->%s", ether_sprintf(wh->i_addr3));
    182 		printf("(%s", ether_sprintf(wh->i_addr2));
    183 		printf("->%s)", ether_sprintf(wh->i_addr1));
    184 		break;
    185 	}
    186 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
    187 	case IEEE80211_FC0_TYPE_DATA:
    188 		printf(" data");
    189 		break;
    190 	case IEEE80211_FC0_TYPE_MGT:
    191 		printf(" %s", ieee80211_mgt_subtype_name[
    192 		    (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
    193 		    >> IEEE80211_FC0_SUBTYPE_SHIFT]);
    194 		break;
    195 	default:
    196 		printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
    197 		break;
    198 	}
    199 	if (wh->i_fc[1] & IEEE80211_FC1_WEP)
    200 		printf(" WEP");
    201 	if (rate >= 0)
    202 		printf(" %dM", rate / 2);
    203 	if (rssi >= 0)
    204 		printf(" +%d", rssi);
    205 	printf("\n");
    206 	if (len > 0) {
    207 		for (i = 0; i < len; i++) {
    208 			if ((i & 1) == 0)
    209 				printf(" ");
    210 			printf("%02x", buf[i]);
    211 		}
    212 		printf("\n");
    213 	}
    214 }
    215 
    216 int
    217 ieee80211_fix_rate(struct ieee80211com *ic, struct ieee80211_node *ni, int flags)
    218 {
    219 #define	RV(v)	((v) & IEEE80211_RATE_VAL)
    220 	int i, j, ignore, error;
    221 	int okrate, badrate;
    222 	struct ieee80211_rateset *srs, *nrs;
    223 	u_int8_t r;
    224 
    225 	error = 0;
    226 	okrate = badrate = 0;
    227 	srs = &ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
    228 	nrs = &ni->ni_rates;
    229 	for (i = 0; i < ni->ni_rates.rs_nrates; ) {
    230 		ignore = 0;
    231 		if (flags & IEEE80211_F_DOSORT) {
    232 			/*
    233 			 * Sort rates.
    234 			 */
    235 			for (j = i + 1; j < nrs->rs_nrates; j++) {
    236 				if (RV(nrs->rs_rates[i]) > RV(nrs->rs_rates[j])) {
    237 					r = nrs->rs_rates[i];
    238 					nrs->rs_rates[i] = nrs->rs_rates[j];
    239 					nrs->rs_rates[j] = r;
    240 				}
    241 			}
    242 		}
    243 		r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
    244 		badrate = r;
    245 		if (flags & IEEE80211_F_DOFRATE) {
    246 			/*
    247 			 * Apply fixed rate constraint.  Note that we do
    248 			 * not apply the constraint to basic rates as
    249 			 * otherwise we may not be able to associate if
    250 			 * the rate set we submit to the AP is invalid
    251 			 * (e.g. fix rate at 36Mb/s which is not a basic
    252 			 * rate for 11a operation).
    253 			 */
    254 			if ((nrs->rs_rates[i] & IEEE80211_RATE_BASIC) == 0 &&
    255 			    ic->ic_fixed_rate >= 0 &&
    256 			    r != RV(srs->rs_rates[ic->ic_fixed_rate]))
    257 				ignore++;
    258 		}
    259 		if (flags & IEEE80211_F_DONEGO) {
    260 			/*
    261 			 * Check against supported rates.
    262 			 */
    263 			for (j = 0; j < srs->rs_nrates; j++) {
    264 				if (r == RV(srs->rs_rates[j]))
    265 					break;
    266 			}
    267 			if (j == srs->rs_nrates) {
    268 				if (nrs->rs_rates[i] & IEEE80211_RATE_BASIC)
    269 					error++;
    270 				ignore++;
    271 			}
    272 		}
    273 		if (flags & IEEE80211_F_DODEL) {
    274 			/*
    275 			 * Delete unacceptable rates.
    276 			 */
    277 			if (ignore) {
    278 				nrs->rs_nrates--;
    279 				for (j = i; j < nrs->rs_nrates; j++)
    280 					nrs->rs_rates[j] = nrs->rs_rates[j + 1];
    281 				nrs->rs_rates[j] = 0;
    282 				continue;
    283 			}
    284 		}
    285 		if (!ignore)
    286 			okrate = nrs->rs_rates[i];
    287 		i++;
    288 	}
    289 	if (okrate == 0 || error != 0)
    290 		return badrate | IEEE80211_RATE_BASIC;
    291 	else
    292 		return RV(okrate);
    293 #undef RV
    294 }
    295 
    296 static int
    297 ieee80211_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int mgt)
    298 {
    299 	struct ifnet *ifp = &ic->ic_if;
    300 	struct ieee80211_node *ni;
    301 	enum ieee80211_state ostate;
    302 	ieee80211_node_critsec_decl(s);
    303 
    304 	ostate = ic->ic_state;
    305 	IEEE80211_DPRINTF(("%s: %s -> %s\n", __func__,
    306 		ieee80211_state_name[ostate], ieee80211_state_name[nstate]));
    307 	ic->ic_state = nstate;			/* state transition */
    308 	ni = ic->ic_bss;			/* NB: no reference held */
    309 	switch (nstate) {
    310 	case IEEE80211_S_INIT:
    311 		switch (ostate) {
    312 		case IEEE80211_S_INIT:
    313 			break;
    314 		case IEEE80211_S_RUN:
    315 			switch (ic->ic_opmode) {
    316 			case IEEE80211_M_STA:
    317 				IEEE80211_SEND_MGMT(ic, ni,
    318 				    IEEE80211_FC0_SUBTYPE_DISASSOC,
    319 				    IEEE80211_REASON_ASSOC_LEAVE);
    320 				break;
    321 			case IEEE80211_M_HOSTAP:
    322 				ieee80211_node_critsec_begin(ic, s);
    323 				TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
    324 					if (ni->ni_associd == 0)
    325 						continue;
    326 					IEEE80211_SEND_MGMT(ic, ni,
    327 					    IEEE80211_FC0_SUBTYPE_DISASSOC,
    328 					    IEEE80211_REASON_ASSOC_LEAVE);
    329 				}
    330 				ieee80211_node_critsec_end(ic, s);
    331 				break;
    332 			default:
    333 				break;
    334 			}
    335 			/* FALLTHRU */
    336 		case IEEE80211_S_ASSOC:
    337 			switch (ic->ic_opmode) {
    338 			case IEEE80211_M_STA:
    339 				IEEE80211_SEND_MGMT(ic, ni,
    340 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
    341 				    IEEE80211_REASON_AUTH_LEAVE);
    342 				break;
    343 			case IEEE80211_M_HOSTAP:
    344 				ieee80211_node_critsec_begin(ic, s);
    345 				TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
    346 					IEEE80211_SEND_MGMT(ic, ni,
    347 					    IEEE80211_FC0_SUBTYPE_DEAUTH,
    348 					    IEEE80211_REASON_AUTH_LEAVE);
    349 				}
    350 				ieee80211_node_critsec_end(ic, s);
    351 				break;
    352 			default:
    353 				break;
    354 			}
    355 			/* FALLTHRU */
    356 		case IEEE80211_S_AUTH:
    357 		case IEEE80211_S_SCAN:
    358 			ic->ic_mgt_timer = 0;
    359 			IF_DRAIN(&ic->ic_mgtq);
    360 			if (ic->ic_wep_ctx != NULL) {
    361 				free(ic->ic_wep_ctx, M_DEVBUF);
    362 				ic->ic_wep_ctx = NULL;
    363 			}
    364 			ieee80211_free_allnodes(ic);
    365 			break;
    366 		}
    367 		break;
    368 	case IEEE80211_S_SCAN:
    369 		ic->ic_flags &= ~IEEE80211_F_SIBSS;
    370 		/* initialize bss for probe request */
    371 		IEEE80211_ADDR_COPY(ni->ni_macaddr, ifp->if_broadcastaddr);
    372 		IEEE80211_ADDR_COPY(ni->ni_bssid, ifp->if_broadcastaddr);
    373 		ni->ni_rates = ic->ic_sup_rates[
    374 			ieee80211_chan2mode(ic, ni->ni_chan)];
    375 		ni->ni_associd = 0;
    376 		ni->ni_rstamp = 0;
    377 		switch (ostate) {
    378 		case IEEE80211_S_INIT:
    379 			if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
    380 			    ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
    381 				/*
    382 				 * AP operation and we already have a channel;
    383 				 * bypass the scan and startup immediately.
    384 				 */
    385 				ieee80211_create_ibss(ic, ic->ic_des_chan);
    386 			} else {
    387 				ieee80211_begin_scan(ifp);
    388 			}
    389 			break;
    390 		case IEEE80211_S_SCAN:
    391 			/* scan next */
    392 			if (ic->ic_flags & IEEE80211_F_ASCAN) {
    393 				IEEE80211_SEND_MGMT(ic, ni,
    394 				    IEEE80211_FC0_SUBTYPE_PROBE_REQ, 0);
    395 			}
    396 			break;
    397 		case IEEE80211_S_RUN:
    398 			/* beacon miss */
    399 			if (ifp->if_flags & IFF_DEBUG) {
    400 				/* XXX bssid clobbered above */
    401 				if_printf(ifp, "no recent beacons from %s;"
    402 				    " rescanning\n",
    403 				    ether_sprintf(ic->ic_bss->ni_bssid));
    404 			}
    405 			ieee80211_free_allnodes(ic);
    406 			/* FALLTHRU */
    407 		case IEEE80211_S_AUTH:
    408 		case IEEE80211_S_ASSOC:
    409 			/* timeout restart scan */
    410 			ni = ieee80211_find_node(ic, ic->ic_bss->ni_macaddr);
    411 			if (ni != NULL) {
    412 				ni->ni_fails++;
    413 				ieee80211_unref_node(&ni);
    414 			}
    415 			ieee80211_begin_scan(ifp);
    416 			break;
    417 		}
    418 		break;
    419 	case IEEE80211_S_AUTH:
    420 		switch (ostate) {
    421 		case IEEE80211_S_INIT:
    422 			IEEE80211_DPRINTF(("%s: invalid transition\n",
    423 				__func__));
    424 			break;
    425 		case IEEE80211_S_SCAN:
    426 			IEEE80211_SEND_MGMT(ic, ni,
    427 			    IEEE80211_FC0_SUBTYPE_AUTH, 1);
    428 			break;
    429 		case IEEE80211_S_AUTH:
    430 		case IEEE80211_S_ASSOC:
    431 			switch (mgt) {
    432 			case IEEE80211_FC0_SUBTYPE_AUTH:
    433 				/* ??? */
    434 				IEEE80211_SEND_MGMT(ic, ni,
    435 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
    436 				break;
    437 			case IEEE80211_FC0_SUBTYPE_DEAUTH:
    438 				/* ignore and retry scan on timeout */
    439 				break;
    440 			}
    441 			break;
    442 		case IEEE80211_S_RUN:
    443 			switch (mgt) {
    444 			case IEEE80211_FC0_SUBTYPE_AUTH:
    445 				IEEE80211_SEND_MGMT(ic, ni,
    446 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
    447 				ic->ic_state = ostate;	/* stay RUN */
    448 				break;
    449 			case IEEE80211_FC0_SUBTYPE_DEAUTH:
    450 				/* try to reauth */
    451 				IEEE80211_SEND_MGMT(ic, ni,
    452 				    IEEE80211_FC0_SUBTYPE_AUTH, 1);
    453 				break;
    454 			}
    455 			break;
    456 		}
    457 		break;
    458 	case IEEE80211_S_ASSOC:
    459 		switch (ostate) {
    460 		case IEEE80211_S_INIT:
    461 		case IEEE80211_S_SCAN:
    462 		case IEEE80211_S_ASSOC:
    463 			IEEE80211_DPRINTF(("%s: invalid transition\n",
    464 				__func__));
    465 			break;
    466 		case IEEE80211_S_AUTH:
    467 			IEEE80211_SEND_MGMT(ic, ni,
    468 			    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
    469 			break;
    470 		case IEEE80211_S_RUN:
    471 			IEEE80211_SEND_MGMT(ic, ni,
    472 			    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 1);
    473 			break;
    474 		}
    475 		break;
    476 	case IEEE80211_S_RUN:
    477 		switch (ostate) {
    478 		case IEEE80211_S_INIT:
    479 		case IEEE80211_S_AUTH:
    480 		case IEEE80211_S_RUN:
    481 			IEEE80211_DPRINTF(("%s: invalid transition\n",
    482 				__func__));
    483 			break;
    484 		case IEEE80211_S_SCAN:		/* adhoc/hostap mode */
    485 		case IEEE80211_S_ASSOC:		/* infra mode */
    486 			KASSERT(ni->ni_txrate < ni->ni_rates.rs_nrates,
    487 				("%s: bogus xmit rate %u setup\n", __func__,
    488 					ni->ni_txrate));
    489 			if (ifp->if_flags & IFF_DEBUG) {
    490 				if_printf(ifp, " ");
    491 				if (ic->ic_opmode == IEEE80211_M_STA)
    492 					printf("associated ");
    493 				else
    494 					printf("synchronized ");
    495 				printf("with %s ssid ",
    496 				    ether_sprintf(ni->ni_bssid));
    497 				ieee80211_print_essid(ic->ic_bss->ni_essid,
    498 				    ni->ni_esslen);
    499 				printf(" channel %d start %uMb\n",
    500 					ieee80211_chan2ieee(ic, ni->ni_chan),
    501 					IEEE80211_RATE2MBS(ni->ni_rates.rs_rates[ni->ni_txrate]));
    502 			}
    503 			ic->ic_mgt_timer = 0;
    504 			(*ifp->if_start)(ifp);
    505 			break;
    506 		}
    507 		break;
    508 	}
    509 	return 0;
    510 }
    511