Home | History | Annotate | Line # | Download | only in net80211
ieee80211.c revision 1.41
      1 /*	$NetBSD: ieee80211.c,v 1.41 2005/11/18 16:40:08 skrll Exp $	*/
      2 /*-
      3  * Copyright (c) 2001 Atsushi Onoe
      4  * Copyright (c) 2002-2005 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.c,v 1.22 2005/08/10 16:22:29 sam Exp $");
     37 #endif
     38 #ifdef __NetBSD__
     39 __KERNEL_RCSID(0, "$NetBSD: ieee80211.c,v 1.41 2005/11/18 16:40:08 skrll Exp $");
     40 #endif
     41 
     42 /*
     43  * IEEE 802.11 generic handler
     44  */
     45 
     46 #include "opt_inet.h"
     47 #include "bpfilter.h"
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/kernel.h>
     52 
     53 #include <sys/socket.h>
     54 #include <sys/sockio.h>
     55 #include <sys/endian.h>
     56 #include <sys/errno.h>
     57 #include <sys/proc.h>
     58 #include <sys/sysctl.h>
     59 
     60 #include <net/if.h>
     61 #include <net/if_media.h>
     62 #include <net/if_arp.h>
     63 #include <net/if_ether.h>
     64 #include <net/if_llc.h>
     65 
     66 #include <net80211/ieee80211_netbsd.h>
     67 #include <net80211/ieee80211_var.h>
     68 #include <net80211/ieee80211_sysctl.h>
     69 
     70 #include <net/bpf.h>
     71 
     72 #ifdef INET
     73 #include <netinet/in.h>
     74 #include <net/if_ether.h>
     75 #endif
     76 
     77 struct ieee80211com_head ieee80211com_head =
     78     LIST_HEAD_INITIALIZER(ieee80211com_head);
     79 
     80 const char *ieee80211_phymode_name[] = {
     81 	"auto",		/* IEEE80211_MODE_AUTO */
     82 	"11a",		/* IEEE80211_MODE_11A */
     83 	"11b",		/* IEEE80211_MODE_11B */
     84 	"11g",		/* IEEE80211_MODE_11G */
     85 	"FH",		/* IEEE80211_MODE_FH */
     86 	"turboA",	/* IEEE80211_MODE_TURBO_A */
     87 	"turboG",	/* IEEE80211_MODE_TURBO_G */
     88 };
     89 
     90 /* list of all instances */
     91 SLIST_HEAD(ieee80211_list, ieee80211com);
     92 static struct ieee80211_list ieee80211_list =
     93 	SLIST_HEAD_INITIALIZER(ieee80211_list);
     94 static u_int8_t ieee80211_vapmap[32];		/* enough for 256 */
     95 
     96 static void
     97 ieee80211_add_vap(struct ieee80211com *ic)
     98 {
     99 #define	N(a)	(sizeof(a)/sizeof(a[0]))
    100 	int i;
    101 	int s;
    102 	u_int8_t b;
    103 
    104 	s = splnet();
    105 	ic->ic_vap = 0;
    106 	for (i = 0; i < N(ieee80211_vapmap) && ieee80211_vapmap[i] == 0xff; i++)
    107 		ic->ic_vap += NBBY;
    108 	if (i == N(ieee80211_vapmap))
    109 		panic("vap table full");
    110 	for (b = ieee80211_vapmap[i]; b & 1; b >>= 1)
    111 		ic->ic_vap++;
    112 	setbit(ieee80211_vapmap, ic->ic_vap);
    113 	SLIST_INSERT_HEAD(&ieee80211_list, ic, ic_next);
    114 	splx(s);
    115 #undef N
    116 }
    117 
    118 static void
    119 ieee80211_remove_vap(struct ieee80211com *ic)
    120 {
    121 	int s;
    122 
    123 	s = splnet();
    124 	SLIST_REMOVE(&ieee80211_list, ic, ieee80211com, ic_next);
    125 	IASSERT(ic->ic_vap < sizeof(ieee80211_vapmap)*NBBY,
    126 		("invalid vap id %d", ic->ic_vap));
    127 	IASSERT(isset(ieee80211_vapmap, ic->ic_vap),
    128 		("vap id %d not allocated", ic->ic_vap));
    129 	clrbit(ieee80211_vapmap, ic->ic_vap);
    130 	splx(s);
    131 }
    132 
    133 /*
    134  * Default reset method for use with the ioctl support.  This
    135  * method is invoked after any state change in the 802.11
    136  * layer that should be propagated to the hardware but not
    137  * require re-initialization of the 802.11 state machine (e.g
    138  * rescanning for an ap).  We always return ENETRESET which
    139  * should cause the driver to re-initialize the device. Drivers
    140  * can override this method to implement more optimized support.
    141  */
    142 static int
    143 ieee80211_default_reset(struct ifnet *ifp)
    144 {
    145 	return ENETRESET;
    146 }
    147 
    148 void
    149 ieee80211_ifattach(struct ieee80211com *ic)
    150 {
    151 	struct ifnet *ifp = ic->ic_ifp;
    152 	struct ieee80211_channel *c;
    153 	int i;
    154 
    155 	ether_ifattach(ifp, ic->ic_myaddr);
    156 #if NBPFILTER > 0
    157 	bpfattach2(ifp, DLT_IEEE802_11,
    158 	    sizeof(struct ieee80211_frame_addr4), &ic->ic_rawbpf);
    159 #endif
    160 
    161 	ieee80211_crypto_attach(ic);
    162 
    163 	/*
    164 	 * Fill in 802.11 available channel set, mark
    165 	 * all available channels as active, and pick
    166 	 * a default channel if not already specified.
    167 	 */
    168 	memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
    169 	ic->ic_modecaps |= 1<<IEEE80211_MODE_AUTO;
    170 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
    171 		c = &ic->ic_channels[i];
    172 		if (c->ic_flags) {
    173 			/*
    174 			 * Verify driver passed us valid data.
    175 			 */
    176 			if (i != ieee80211_chan2ieee(ic, c)) {
    177 				if_printf(ifp, "bad channel ignored; "
    178 					"freq %u flags %x number %u\n",
    179 					c->ic_freq, c->ic_flags, i);
    180 				c->ic_flags = 0;	/* NB: remove */
    181 				continue;
    182 			}
    183 			setbit(ic->ic_chan_avail, i);
    184 			/*
    185 			 * Identify mode capabilities.
    186 			 */
    187 			if (IEEE80211_IS_CHAN_A(c))
    188 				ic->ic_modecaps |= 1<<IEEE80211_MODE_11A;
    189 			if (IEEE80211_IS_CHAN_B(c))
    190 				ic->ic_modecaps |= 1<<IEEE80211_MODE_11B;
    191 			if (IEEE80211_IS_CHAN_PUREG(c))
    192 				ic->ic_modecaps |= 1<<IEEE80211_MODE_11G;
    193 			if (IEEE80211_IS_CHAN_FHSS(c))
    194 				ic->ic_modecaps |= 1<<IEEE80211_MODE_FH;
    195 			if (IEEE80211_IS_CHAN_T(c))
    196 				ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO_A;
    197 			if (IEEE80211_IS_CHAN_108G(c))
    198 				ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO_G;
    199 			if (ic->ic_curchan == NULL) {
    200 				/* arbitrarily pick the first channel */
    201 				ic->ic_curchan = &ic->ic_channels[i];
    202 			}
    203 		}
    204 	}
    205 	/* validate ic->ic_curmode */
    206 	if ((ic->ic_modecaps & (1<<ic->ic_curmode)) == 0)
    207 		ic->ic_curmode = IEEE80211_MODE_AUTO;
    208 	ic->ic_des_chan = IEEE80211_CHAN_ANYC;	/* any channel is ok */
    209 #if 0
    210 	/*
    211 	 * Enable WME by default if we're capable.
    212 	 */
    213 	if (ic->ic_caps & IEEE80211_C_WME)
    214 		ic->ic_flags |= IEEE80211_F_WME;
    215 #endif
    216 	(void) ieee80211_setmode(ic, ic->ic_curmode);
    217 
    218 	if (ic->ic_bintval == 0)
    219 		ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
    220 	ic->ic_bmisstimeout = 7*ic->ic_bintval;	/* default 7 beacons */
    221 	ic->ic_dtim_period = IEEE80211_DTIM_DEFAULT;
    222 	IEEE80211_BEACON_LOCK_INIT(ic, "beacon");
    223 
    224 	if (ic->ic_lintval == 0)
    225 		ic->ic_lintval = ic->ic_bintval;
    226 	ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
    227 
    228 	LIST_INSERT_HEAD(&ieee80211com_head, ic, ic_list);
    229 	ieee80211_node_attach(ic);
    230 	ieee80211_proto_attach(ic);
    231 
    232 	ieee80211_add_vap(ic);
    233 
    234 	ieee80211_sysctl_attach(ic);		/* NB: requires ic_vap */
    235 
    236 	/*
    237 	 * Install a default reset method for the ioctl support.
    238 	 * The driver is expected to fill this in before calling us.
    239 	 */
    240 	if (ic->ic_reset == NULL)
    241 		ic->ic_reset = ieee80211_default_reset;
    242 }
    243 
    244 void
    245 ieee80211_ifdetach(struct ieee80211com *ic)
    246 {
    247 	struct ifnet *ifp = ic->ic_ifp;
    248 
    249 	ieee80211_remove_vap(ic);
    250 
    251 	ieee80211_sysctl_detach(ic);
    252 	ieee80211_proto_detach(ic);
    253 	ieee80211_crypto_detach(ic);
    254 	ieee80211_node_detach(ic);
    255 	LIST_REMOVE(ic, ic_list);
    256 	ifmedia_delete_instance(&ic->ic_media, IFM_INST_ANY);
    257 
    258 	IEEE80211_BEACON_LOCK_DESTROY(ic);
    259 
    260 #if NBPFILTER > 0
    261 	bpfdetach(ifp);
    262 #endif
    263 	ether_ifdetach(ifp);
    264 }
    265 
    266 /*
    267  * Convert MHz frequency to IEEE channel number.
    268  */
    269 u_int
    270 ieee80211_mhz2ieee(u_int freq, u_int flags)
    271 {
    272 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
    273 		if (freq == 2484)
    274 			return 14;
    275 		if (freq < 2484)
    276 			return (freq - 2407) / 5;
    277 		else
    278 			return 15 + ((freq - 2512) / 20);
    279 	} else if (flags & IEEE80211_CHAN_5GHZ) {	/* 5Ghz band */
    280 		return (freq - 5000) / 5;
    281 	} else {				/* either, guess */
    282 		if (freq == 2484)
    283 			return 14;
    284 		if (freq < 2484)
    285 			return (freq - 2407) / 5;
    286 		if (freq < 5000)
    287 			return 15 + ((freq - 2512) / 20);
    288 		return (freq - 5000) / 5;
    289 	}
    290 }
    291 
    292 /*
    293  * Convert channel to IEEE channel number.
    294  */
    295 u_int
    296 ieee80211_chan2ieee(struct ieee80211com *ic, struct ieee80211_channel *c)
    297 {
    298 	if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX])
    299 		return c - ic->ic_channels;
    300 	else if (c == IEEE80211_CHAN_ANYC)
    301 		return IEEE80211_CHAN_ANY;
    302 	else if (c != NULL) {
    303 		if_printf(ic->ic_ifp, "invalid channel freq %u flags %x\n",
    304 			c->ic_freq, c->ic_flags);
    305 		return 0;		/* XXX */
    306 	} else {
    307 		if_printf(ic->ic_ifp, "invalid channel (NULL)\n");
    308 		return 0;		/* XXX */
    309 	}
    310 }
    311 
    312 /*
    313  * Convert IEEE channel number to MHz frequency.
    314  */
    315 u_int
    316 ieee80211_ieee2mhz(u_int chan, u_int flags)
    317 {
    318 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
    319 		if (chan == 14)
    320 			return 2484;
    321 		if (chan < 14)
    322 			return 2407 + chan*5;
    323 		else
    324 			return 2512 + ((chan-15)*20);
    325 	} else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
    326 		return 5000 + (chan*5);
    327 	} else {				/* either, guess */
    328 		if (chan == 14)
    329 			return 2484;
    330 		if (chan < 14)			/* 0-13 */
    331 			return 2407 + chan*5;
    332 		if (chan < 27)			/* 15-26 */
    333 			return 2512 + ((chan-15)*20);
    334 		return 5000 + (chan*5);
    335 	}
    336 }
    337 
    338 /*
    339  * Setup the media data structures according to the channel and
    340  * rate tables.  This must be called by the driver after
    341  * ieee80211_attach and before most anything else.
    342  */
    343 void
    344 ieee80211_media_init(struct ieee80211com *ic,
    345 	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
    346 {
    347 #define	ADD(_ic, _s, _o) \
    348 	ifmedia_add(&(_ic)->ic_media, \
    349 		IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
    350 	struct ifnet *ifp = ic->ic_ifp;
    351 	struct ifmediareq imr;
    352 	int i, j, mode, rate, maxrate, mword, mopt, r;
    353 	struct ieee80211_rateset *rs;
    354 	struct ieee80211_rateset allrates;
    355 
    356 	/*
    357 	 * Do late attach work that must wait for any subclass
    358 	 * (i.e. driver) work such as overriding methods.
    359 	 */
    360 	ieee80211_node_lateattach(ic);
    361 
    362 #ifdef IEEE80211_NO_HOSTAP
    363 	ic->ic_caps &= ~IEEE80211_C_HOSTAP;
    364 #endif /* IEEE80211_NO_HOSTAP */
    365 
    366 	/*
    367 	 * Fill in media characteristics.
    368 	 */
    369 	ifmedia_init(&ic->ic_media, 0, media_change, media_stat);
    370 	maxrate = 0;
    371 	memset(&allrates, 0, sizeof(allrates));
    372 	for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_MAX; mode++) {
    373 		static const u_int mopts[] = {
    374 			IFM_AUTO,
    375 			IFM_IEEE80211_11A,
    376 			IFM_IEEE80211_11B,
    377 			IFM_IEEE80211_11G,
    378 			IFM_IEEE80211_FH,
    379 			IFM_IEEE80211_11A | IFM_IEEE80211_TURBO,
    380 			IFM_IEEE80211_11G | IFM_IEEE80211_TURBO,
    381 		};
    382 		if ((ic->ic_modecaps & (1<<mode)) == 0)
    383 			continue;
    384 		mopt = mopts[mode];
    385 		ADD(ic, IFM_AUTO, mopt);	/* e.g. 11a auto */
    386 		if (ic->ic_caps & IEEE80211_C_IBSS)
    387 			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC);
    388 		if (ic->ic_caps & IEEE80211_C_HOSTAP)
    389 			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP);
    390 		if (ic->ic_caps & IEEE80211_C_AHDEMO)
    391 			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
    392 		if (ic->ic_caps & IEEE80211_C_MONITOR)
    393 			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR);
    394 		if (mode == IEEE80211_MODE_AUTO)
    395 			continue;
    396 		rs = &ic->ic_sup_rates[mode];
    397 		for (i = 0; i < rs->rs_nrates; i++) {
    398 			rate = rs->rs_rates[i];
    399 			mword = ieee80211_rate2media(ic, rate, mode);
    400 			if (mword == 0)
    401 				continue;
    402 			ADD(ic, mword, mopt);
    403 			if (ic->ic_caps & IEEE80211_C_IBSS)
    404 				ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC);
    405 			if (ic->ic_caps & IEEE80211_C_HOSTAP)
    406 				ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP);
    407 			if (ic->ic_caps & IEEE80211_C_AHDEMO)
    408 				ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
    409 			if (ic->ic_caps & IEEE80211_C_MONITOR)
    410 				ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR);
    411 			/*
    412 			 * Add rate to the collection of all rates.
    413 			 */
    414 			r = rate & IEEE80211_RATE_VAL;
    415 			for (j = 0; j < allrates.rs_nrates; j++)
    416 				if (allrates.rs_rates[j] == r)
    417 					break;
    418 			if (j == allrates.rs_nrates) {
    419 				/* unique, add to the set */
    420 				allrates.rs_rates[j] = r;
    421 				allrates.rs_nrates++;
    422 			}
    423 			rate = (rate & IEEE80211_RATE_VAL) / 2;
    424 			if (rate > maxrate)
    425 				maxrate = rate;
    426 		}
    427 	}
    428 	for (i = 0; i < allrates.rs_nrates; i++) {
    429 		mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
    430 				IEEE80211_MODE_AUTO);
    431 		if (mword == 0)
    432 			continue;
    433 		mword = IFM_SUBTYPE(mword);	/* remove media options */
    434 		ADD(ic, mword, 0);
    435 		if (ic->ic_caps & IEEE80211_C_IBSS)
    436 			ADD(ic, mword, IFM_IEEE80211_ADHOC);
    437 		if (ic->ic_caps & IEEE80211_C_HOSTAP)
    438 			ADD(ic, mword, IFM_IEEE80211_HOSTAP);
    439 		if (ic->ic_caps & IEEE80211_C_AHDEMO)
    440 			ADD(ic, mword, IFM_IEEE80211_ADHOC | IFM_FLAG0);
    441 		if (ic->ic_caps & IEEE80211_C_MONITOR)
    442 			ADD(ic, mword, IFM_IEEE80211_MONITOR);
    443 	}
    444 	ieee80211_media_status(ifp, &imr);
    445 	ifmedia_set(&ic->ic_media, imr.ifm_active);
    446 
    447 	if (maxrate)
    448 		ifp->if_baudrate = IF_Mbps(maxrate);
    449 #undef ADD
    450 }
    451 
    452 void
    453 ieee80211_announce(struct ieee80211com *ic)
    454 {
    455 	struct ifnet *ifp = ic->ic_ifp;
    456 	int i, mode, rate, mword;
    457 	struct ieee80211_rateset *rs;
    458 
    459 	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
    460 		if ((ic->ic_modecaps & (1<<mode)) == 0)
    461 			continue;
    462 		if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
    463 		rs = &ic->ic_sup_rates[mode];
    464 		for (i = 0; i < rs->rs_nrates; i++) {
    465 			rate = rs->rs_rates[i];
    466 			mword = ieee80211_rate2media(ic, rate, mode);
    467 			if (mword == 0)
    468 				continue;
    469 			printf("%s%d%sMbps", (i != 0 ? " " : ""),
    470 			    (rate & IEEE80211_RATE_VAL) / 2,
    471 			    ((rate & 0x1) != 0 ? ".5" : ""));
    472 		}
    473 		printf("\n");
    474 	}
    475 }
    476 
    477 static int
    478 findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
    479 {
    480 #define	IEEERATE(_ic,_m,_i) \
    481 	((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
    482 	int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
    483 	for (i = 0; i < nrates; i++)
    484 		if (IEEERATE(ic, mode, i) == rate)
    485 			return i;
    486 	return -1;
    487 #undef IEEERATE
    488 }
    489 
    490 /*
    491  * Find an instance by it's mac address.
    492  */
    493 struct ieee80211com *
    494 ieee80211_find_vap(const u_int8_t mac[IEEE80211_ADDR_LEN])
    495 {
    496 	int s;
    497 	struct ieee80211com *ic;
    498 
    499 	s = splnet();
    500 	SLIST_FOREACH(ic, &ieee80211_list, ic_next)
    501 		if (IEEE80211_ADDR_EQ(mac, ic->ic_myaddr))
    502 			break;
    503 	splx(s);
    504 	return ic;
    505 }
    506 
    507 static struct ieee80211com *
    508 ieee80211_find_instance(struct ifnet *ifp)
    509 {
    510 	int s;
    511 	struct ieee80211com *ic;
    512 
    513 	s = splnet();
    514 	/* XXX not right for multiple instances but works for now */
    515 	SLIST_FOREACH(ic, &ieee80211_list, ic_next)
    516 		if (ic->ic_ifp == ifp)
    517 			break;
    518 	splx(s);
    519 	return ic;
    520 }
    521 
    522 /*
    523  * Handle a media change request.
    524  */
    525 int
    526 ieee80211_media_change(struct ifnet *ifp)
    527 {
    528 	struct ieee80211com *ic;
    529 	struct ifmedia_entry *ime;
    530 	enum ieee80211_opmode newopmode;
    531 	enum ieee80211_phymode newphymode;
    532 	int i, j, newrate, error = 0;
    533 
    534 	ic = ieee80211_find_instance(ifp);
    535 	if (!ic) {
    536 		if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
    537 		return EINVAL;
    538 	}
    539 	ime = ic->ic_media.ifm_cur;
    540 	/*
    541 	 * First, identify the phy mode.
    542 	 */
    543 	switch (IFM_MODE(ime->ifm_media)) {
    544 	case IFM_IEEE80211_11A:
    545 		newphymode = IEEE80211_MODE_11A;
    546 		break;
    547 	case IFM_IEEE80211_11B:
    548 		newphymode = IEEE80211_MODE_11B;
    549 		break;
    550 	case IFM_IEEE80211_11G:
    551 		newphymode = IEEE80211_MODE_11G;
    552 		break;
    553 	case IFM_IEEE80211_FH:
    554 		newphymode = IEEE80211_MODE_FH;
    555 		break;
    556 	case IFM_AUTO:
    557 		newphymode = IEEE80211_MODE_AUTO;
    558 		break;
    559 	default:
    560 		return EINVAL;
    561 	}
    562 	/*
    563 	 * Turbo mode is an ``option''.
    564 	 * XXX does not apply to AUTO
    565 	 */
    566 	if (ime->ifm_media & IFM_IEEE80211_TURBO) {
    567 		if (newphymode == IEEE80211_MODE_11A)
    568 			newphymode = IEEE80211_MODE_TURBO_A;
    569 		else if (newphymode == IEEE80211_MODE_11G)
    570 			newphymode = IEEE80211_MODE_TURBO_G;
    571 		else
    572 			return EINVAL;
    573 	}
    574 	/*
    575 	 * Validate requested mode is available.
    576 	 */
    577 	if ((ic->ic_modecaps & (1<<newphymode)) == 0)
    578 		return EINVAL;
    579 
    580 	/*
    581 	 * Next, the fixed/variable rate.
    582 	 */
    583 	i = -1;
    584 	if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
    585 		/*
    586 		 * Convert media subtype to rate.
    587 		 */
    588 		newrate = ieee80211_media2rate(ime->ifm_media);
    589 		if (newrate == 0)
    590 			return EINVAL;
    591 		/*
    592 		 * Check the rate table for the specified/current phy.
    593 		 */
    594 		if (newphymode == IEEE80211_MODE_AUTO) {
    595 			/*
    596 			 * In autoselect mode search for the rate.
    597 			 */
    598 			for (j = IEEE80211_MODE_11A;
    599 			     j < IEEE80211_MODE_MAX; j++) {
    600 				if ((ic->ic_modecaps & (1<<j)) == 0)
    601 					continue;
    602 				i = findrate(ic, j, newrate);
    603 				if (i != -1) {
    604 					/* lock mode too */
    605 					newphymode = j;
    606 					break;
    607 				}
    608 			}
    609 		} else {
    610 			i = findrate(ic, newphymode, newrate);
    611 		}
    612 		if (i == -1)			/* mode/rate mismatch */
    613 			return EINVAL;
    614 	}
    615 	/* NB: defer rate setting to later */
    616 
    617 	/*
    618 	 * Deduce new operating mode but don't install it just yet.
    619 	 */
    620 	if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) ==
    621 	    (IFM_IEEE80211_ADHOC|IFM_FLAG0))
    622 		newopmode = IEEE80211_M_AHDEMO;
    623 	else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
    624 		newopmode = IEEE80211_M_HOSTAP;
    625 	else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
    626 		newopmode = IEEE80211_M_IBSS;
    627 	else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
    628 		newopmode = IEEE80211_M_MONITOR;
    629 	else
    630 		newopmode = IEEE80211_M_STA;
    631 
    632 #ifndef IEEE80211_NO_HOSTAP
    633 	/*
    634 	 * Autoselect doesn't make sense when operating as an AP.
    635 	 * If no phy mode has been selected, pick one and lock it
    636 	 * down so rate tables can be used in forming beacon frames
    637 	 * and the like.
    638 	 */
    639 	if (newopmode == IEEE80211_M_HOSTAP &&
    640 	    newphymode == IEEE80211_MODE_AUTO) {
    641 		for (j = IEEE80211_MODE_11A; j < IEEE80211_MODE_MAX; j++)
    642 			if (ic->ic_modecaps & (1<<j)) {
    643 				newphymode = j;
    644 				break;
    645 			}
    646 	}
    647 #endif /* !IEEE80211_NO_HOSTAP */
    648 
    649 	/*
    650 	 * Handle phy mode change.
    651 	 */
    652 	if (ic->ic_curmode != newphymode) {		/* change phy mode */
    653 		error = ieee80211_setmode(ic, newphymode);
    654 		if (error != 0)
    655 			return error;
    656 		error = ENETRESET;
    657 	}
    658 
    659 	/*
    660 	 * Committed to changes, install the rate setting.
    661 	 */
    662 	if (ic->ic_fixed_rate != i) {
    663 		ic->ic_fixed_rate = i;			/* set fixed tx rate */
    664 		error = ENETRESET;
    665 	}
    666 
    667 	/*
    668 	 * Handle operating mode change.
    669 	 */
    670 	if (ic->ic_opmode != newopmode) {
    671 		ic->ic_opmode = newopmode;
    672 		switch (newopmode) {
    673 		case IEEE80211_M_AHDEMO:
    674 		case IEEE80211_M_HOSTAP:
    675 		case IEEE80211_M_STA:
    676 		case IEEE80211_M_MONITOR:
    677 			ic->ic_flags &= ~IEEE80211_F_IBSSON;
    678 			break;
    679 		case IEEE80211_M_IBSS:
    680 			ic->ic_flags |= IEEE80211_F_IBSSON;
    681 			break;
    682 		}
    683 		/*
    684 		 * Yech, slot time may change depending on the
    685 		 * operating mode so reset it to be sure everything
    686 		 * is setup appropriately.
    687 		 */
    688 		ieee80211_reset_erp(ic);
    689 		ieee80211_wme_initparams(ic);	/* after opmode change */
    690 		error = ENETRESET;
    691 	}
    692 #ifdef notdef
    693 	if (error == 0)
    694 		ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
    695 #endif
    696 	return error;
    697 }
    698 
    699 void
    700 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
    701 {
    702 	struct ieee80211com *ic;
    703 	struct ieee80211_rateset *rs;
    704 
    705 	ic = ieee80211_find_instance(ifp);
    706 	if (!ic) {
    707 		if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
    708 		return;
    709 	}
    710 	imr->ifm_status = IFM_AVALID;
    711 	imr->ifm_active = IFM_IEEE80211;
    712 	if (ic->ic_state == IEEE80211_S_RUN)
    713 		imr->ifm_status |= IFM_ACTIVE;
    714 	/*
    715 	 * Calculate a current rate if possible.
    716 	 */
    717 	if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
    718 		/*
    719 		 * A fixed rate is set, report that.
    720 		 */
    721 		rs = &ic->ic_sup_rates[ic->ic_curmode];
    722 		imr->ifm_active |= ieee80211_rate2media(ic,
    723 			rs->rs_rates[ic->ic_fixed_rate], ic->ic_curmode);
    724 	} else if (ic->ic_opmode == IEEE80211_M_STA) {
    725 		/*
    726 		 * In station mode report the current transmit rate.
    727 		 */
    728 		rs = &ic->ic_bss->ni_rates;
    729 		imr->ifm_active |= ieee80211_rate2media(ic,
    730 			rs->rs_rates[ic->ic_bss->ni_txrate], ic->ic_curmode);
    731 	} else
    732 		imr->ifm_active |= IFM_AUTO;
    733 	switch (ic->ic_opmode) {
    734 	case IEEE80211_M_STA:
    735 		break;
    736 	case IEEE80211_M_IBSS:
    737 		imr->ifm_active |= IFM_IEEE80211_ADHOC;
    738 		break;
    739 	case IEEE80211_M_AHDEMO:
    740 		/* should not come here */
    741 		break;
    742 	case IEEE80211_M_HOSTAP:
    743 		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
    744 		break;
    745 	case IEEE80211_M_MONITOR:
    746 		imr->ifm_active |= IFM_IEEE80211_MONITOR;
    747 		break;
    748 	}
    749 	switch (ic->ic_curmode) {
    750 	case IEEE80211_MODE_11A:
    751 		imr->ifm_active |= IFM_IEEE80211_11A;
    752 		break;
    753 	case IEEE80211_MODE_11B:
    754 		imr->ifm_active |= IFM_IEEE80211_11B;
    755 		break;
    756 	case IEEE80211_MODE_11G:
    757 		imr->ifm_active |= IFM_IEEE80211_11G;
    758 		break;
    759 	case IEEE80211_MODE_FH:
    760 		imr->ifm_active |= IFM_IEEE80211_FH;
    761 		break;
    762 	case IEEE80211_MODE_TURBO_A:
    763 		imr->ifm_active |= IFM_IEEE80211_11A
    764 				|  IFM_IEEE80211_TURBO;
    765 		break;
    766 	case IEEE80211_MODE_TURBO_G:
    767 		imr->ifm_active |= IFM_IEEE80211_11G
    768 				|  IFM_IEEE80211_TURBO;
    769 		break;
    770 	}
    771 }
    772 
    773 void
    774 ieee80211_watchdog(struct ieee80211com *ic)
    775 {
    776 	struct ieee80211_node_table *nt;
    777 	int need_inact_timer = 0;
    778 
    779 	if (ic->ic_state != IEEE80211_S_INIT) {
    780 		if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0)
    781 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
    782 		nt = &ic->ic_scan;
    783 		if (nt->nt_inact_timer) {
    784 			if (--nt->nt_inact_timer == 0)
    785 				nt->nt_timeout(nt);
    786 			need_inact_timer += nt->nt_inact_timer;
    787 		}
    788 		nt = &ic->ic_sta;
    789 		if (nt->nt_inact_timer) {
    790 			if (--nt->nt_inact_timer == 0)
    791 				nt->nt_timeout(nt);
    792 			need_inact_timer += nt->nt_inact_timer;
    793 		}
    794 	}
    795 	if (ic->ic_mgt_timer != 0 || need_inact_timer)
    796 		ic->ic_ifp->if_timer = 1;
    797 }
    798 
    799 /*
    800  * Set the current phy mode and recalculate the active channel
    801  * set based on the available channels for this mode.  Also
    802  * select a new default/current channel if the current one is
    803  * inappropriate for this mode.
    804  */
    805 int
    806 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
    807 {
    808 #define	N(a)	(sizeof(a) / sizeof(a[0]))
    809 	static const u_int chanflags[] = {
    810 		0,			/* IEEE80211_MODE_AUTO */
    811 		IEEE80211_CHAN_A,	/* IEEE80211_MODE_11A */
    812 		IEEE80211_CHAN_B,	/* IEEE80211_MODE_11B */
    813 		IEEE80211_CHAN_PUREG,	/* IEEE80211_MODE_11G */
    814 		IEEE80211_CHAN_FHSS,	/* IEEE80211_MODE_FH */
    815 		IEEE80211_CHAN_T,	/* IEEE80211_MODE_TURBO_A */
    816 		IEEE80211_CHAN_108G,	/* IEEE80211_MODE_TURBO_G */
    817 	};
    818 	struct ieee80211_channel *c;
    819 	u_int modeflags;
    820 	int i;
    821 
    822 	/* validate new mode */
    823 	if ((ic->ic_modecaps & (1<<mode)) == 0) {
    824 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
    825 			"%s: mode %u not supported (caps 0x%x)\n",
    826 			__func__, mode, ic->ic_modecaps);
    827 		return EINVAL;
    828 	}
    829 
    830 	/*
    831 	 * Verify at least one channel is present in the available
    832 	 * channel list before committing to the new mode.
    833 	 */
    834 	IASSERT(mode < N(chanflags), ("Unexpected mode %u", mode));
    835 	modeflags = chanflags[mode];
    836 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
    837 		c = &ic->ic_channels[i];
    838 		if (mode == IEEE80211_MODE_AUTO) {
    839 			/* ignore turbo channels for autoselect */
    840 			if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
    841 				break;
    842 		} else {
    843 			if ((c->ic_flags & modeflags) == modeflags)
    844 				break;
    845 		}
    846 	}
    847 	if (i > IEEE80211_CHAN_MAX) {
    848 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
    849 			"%s: no channels found for mode %u\n", __func__, mode);
    850 		return EINVAL;
    851 	}
    852 
    853 	/*
    854 	 * Calculate the active channel set.
    855 	 */
    856 	memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active));
    857 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
    858 		c = &ic->ic_channels[i];
    859 		if (mode == IEEE80211_MODE_AUTO) {
    860 			/* take anything but pure turbo channels */
    861 			if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
    862 				setbit(ic->ic_chan_active, i);
    863 		} else {
    864 			if ((c->ic_flags & modeflags) == modeflags)
    865 				setbit(ic->ic_chan_active, i);
    866 		}
    867 	}
    868 	/*
    869 	 * If no current/default channel is setup or the current
    870 	 * channel is wrong for the mode then pick the first
    871 	 * available channel from the active list.  This is likely
    872 	 * not the right one.
    873 	 */
    874 	if (ic->ic_ibss_chan == NULL ||
    875 	    isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
    876 		for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
    877 			if (isset(ic->ic_chan_active, i)) {
    878 				ic->ic_ibss_chan = &ic->ic_channels[i];
    879 				break;
    880 			}
    881 		IASSERT(ic->ic_ibss_chan != NULL &&
    882 		    isset(ic->ic_chan_active,
    883 			ieee80211_chan2ieee(ic, ic->ic_ibss_chan)),
    884 		    ("Bad IBSS channel %u",
    885 		     ieee80211_chan2ieee(ic, ic->ic_ibss_chan)));
    886 	}
    887 	/*
    888 	 * If the desired channel is set but no longer valid then reset it.
    889 	 */
    890 	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
    891 	    isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_des_chan)))
    892 		ic->ic_des_chan = IEEE80211_CHAN_ANYC;
    893 
    894 	/*
    895 	 * Do mode-specific rate setup.
    896 	 */
    897 	if (mode == IEEE80211_MODE_11G) {
    898 		/*
    899 		 * Use a mixed 11b/11g rate set.
    900 		 */
    901 		ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode],
    902 			IEEE80211_MODE_11G);
    903 	} else if (mode == IEEE80211_MODE_11B) {
    904 		/*
    905 		 * Force pure 11b rate set.
    906 		 */
    907 		ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode],
    908 			IEEE80211_MODE_11B);
    909 	}
    910 	/*
    911 	 * Setup an initial rate set according to the
    912 	 * current/default channel selected above.  This
    913 	 * will be changed when scanning but must exist
    914 	 * now so driver have a consistent state of ic_ibss_chan.
    915 	 */
    916 	if (ic->ic_bss)		/* NB: can be called before lateattach */
    917 		ic->ic_bss->ni_rates = ic->ic_sup_rates[mode];
    918 
    919 	ic->ic_curmode = mode;
    920 	ieee80211_reset_erp(ic);	/* reset ERP state */
    921 	ieee80211_wme_initparams(ic);	/* reset WME stat */
    922 
    923 	return 0;
    924 #undef N
    925 }
    926 
    927 /*
    928  * Return the phy mode for with the specified channel so the
    929  * caller can select a rate set.  This is problematic for channels
    930  * where multiple operating modes are possible (e.g. 11g+11b).
    931  * In those cases we defer to the current operating mode when set.
    932  */
    933 enum ieee80211_phymode
    934 ieee80211_chan2mode(struct ieee80211com *ic, struct ieee80211_channel *chan)
    935 {
    936 	if (IEEE80211_IS_CHAN_5GHZ(chan)) {
    937 		/*
    938 		 * This assumes all 11a turbo channels are also
    939 		 * usable withut turbo, which is currently true.
    940 		 */
    941 		if (ic->ic_curmode == IEEE80211_MODE_TURBO_A)
    942 			return IEEE80211_MODE_TURBO_A;
    943 		return IEEE80211_MODE_11A;
    944 	} else if (IEEE80211_IS_CHAN_FHSS(chan))
    945 		return IEEE80211_MODE_FH;
    946 	else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN)) {
    947 		/*
    948 		 * This assumes all 11g channels are also usable
    949 		 * for 11b, which is currently true.
    950 		 */
    951 		if (ic->ic_curmode == IEEE80211_MODE_TURBO_G)
    952 			return IEEE80211_MODE_TURBO_G;
    953 		if (ic->ic_curmode == IEEE80211_MODE_11B)
    954 			return IEEE80211_MODE_11B;
    955 		return IEEE80211_MODE_11G;
    956 	} else
    957 		return IEEE80211_MODE_11B;
    958 }
    959 
    960 /*
    961  * convert IEEE80211 rate value to ifmedia subtype.
    962  * ieee80211 rate is in unit of 0.5Mbps.
    963  */
    964 int
    965 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
    966 {
    967 #define	N(a)	(sizeof(a) / sizeof(a[0]))
    968 	static const struct {
    969 		u_int	m;	/* rate + mode */
    970 		u_int	r;	/* if_media rate */
    971 	} rates[] = {
    972 		{   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
    973 		{   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
    974 		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
    975 		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
    976 		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
    977 		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
    978 		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
    979 		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
    980 		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
    981 		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
    982 		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
    983 		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
    984 		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
    985 		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
    986 		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
    987 		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
    988 		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
    989 		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
    990 		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
    991 		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
    992 		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
    993 		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
    994 		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
    995 		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
    996 		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
    997 		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
    998 		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
    999 		/* NB: OFDM72 doesn't realy exist so we don't handle it */
   1000 	};
   1001 	u_int mask, i;
   1002 
   1003 	mask = rate & IEEE80211_RATE_VAL;
   1004 	switch (mode) {
   1005 	case IEEE80211_MODE_11A:
   1006 	case IEEE80211_MODE_TURBO_A:
   1007 		mask |= IFM_IEEE80211_11A;
   1008 		break;
   1009 	case IEEE80211_MODE_11B:
   1010 		mask |= IFM_IEEE80211_11B;
   1011 		break;
   1012 	case IEEE80211_MODE_FH:
   1013 		mask |= IFM_IEEE80211_FH;
   1014 		break;
   1015 	case IEEE80211_MODE_AUTO:
   1016 		/* NB: ic may be NULL for some drivers */
   1017 		if (ic && ic->ic_phytype == IEEE80211_T_FH) {
   1018 			mask |= IFM_IEEE80211_FH;
   1019 			break;
   1020 		}
   1021 		/* NB: hack, 11g matches both 11b+11a rates */
   1022 		/* fall thru... */
   1023 	case IEEE80211_MODE_11G:
   1024 	case IEEE80211_MODE_TURBO_G:
   1025 		mask |= IFM_IEEE80211_11G;
   1026 		break;
   1027 	}
   1028 	for (i = 0; i < N(rates); i++)
   1029 		if (rates[i].m == mask)
   1030 			return rates[i].r;
   1031 	return IFM_AUTO;
   1032 #undef N
   1033 }
   1034 
   1035 int
   1036 ieee80211_media2rate(int mword)
   1037 {
   1038 #define	N(a)	(sizeof(a) / sizeof(a[0]))
   1039 	static const int ieeerates[] = {
   1040 		-1,		/* IFM_AUTO */
   1041 		0,		/* IFM_MANUAL */
   1042 		0,		/* IFM_NONE */
   1043 		2,		/* IFM_IEEE80211_FH1 */
   1044 		4,		/* IFM_IEEE80211_FH2 */
   1045 		2,		/* IFM_IEEE80211_DS1 */
   1046 		4,		/* IFM_IEEE80211_DS2 */
   1047 		11,		/* IFM_IEEE80211_DS5 */
   1048 		22,		/* IFM_IEEE80211_DS11 */
   1049 		44,		/* IFM_IEEE80211_DS22 */
   1050 		12,		/* IFM_IEEE80211_OFDM6 */
   1051 		18,		/* IFM_IEEE80211_OFDM9 */
   1052 		24,		/* IFM_IEEE80211_OFDM12 */
   1053 		36,		/* IFM_IEEE80211_OFDM18 */
   1054 		48,		/* IFM_IEEE80211_OFDM24 */
   1055 		72,		/* IFM_IEEE80211_OFDM36 */
   1056 		96,		/* IFM_IEEE80211_OFDM48 */
   1057 		108,		/* IFM_IEEE80211_OFDM54 */
   1058 		144,		/* IFM_IEEE80211_OFDM72 */
   1059 	};
   1060 	return IFM_SUBTYPE(mword) < N(ieeerates) ?
   1061 		ieeerates[IFM_SUBTYPE(mword)] : 0;
   1062 #undef N
   1063 }
   1064