Home | History | Annotate | Line # | Download | only in ifconfig
ieee80211.c revision 1.1
      1 /*	$NetBSD: ieee80211.c,v 1.1 2005/03/19 23:13:42 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1993
      5  *      The Regents of the University of California.  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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __RCSID("$NetBSD: ieee80211.c,v 1.1 2005/03/19 23:13:42 thorpej Exp $");
     35 #endif /* not lint */
     36 
     37 #include <sys/param.h>
     38 #include <sys/ioctl.h>
     39 #include <sys/socket.h>
     40 
     41 #include <net/if.h>
     42 #include <net/if_ether.h>
     43 #include <net80211/ieee80211.h>
     44 #include <net80211/ieee80211_ioctl.h>
     45 
     46 #include <ctype.h>
     47 #include <err.h>
     48 #include <netdb.h>
     49 #include <string.h>
     50 #include <stdlib.h>
     51 #include <stdio.h>
     52 
     53 #include "extern.h"
     54 #include "ieee80211.h"
     55 
     56 void
     57 setifnwid(const char *val, int d)
     58 {
     59 	struct ieee80211_nwid nwid;
     60 	int len;
     61 
     62 	len = sizeof(nwid.i_nwid);
     63 	if (get_string(val, NULL, nwid.i_nwid, &len) == NULL)
     64 		return;
     65 	nwid.i_len = len;
     66 	(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
     67 	ifr.ifr_data = (void *)&nwid;
     68 	if (ioctl(s, SIOCS80211NWID, &ifr) == -1)
     69 		warn("SIOCS80211NWID");
     70 }
     71 
     72 void
     73 setifbssid(const char *val, int d)
     74 {
     75 	struct ieee80211_bssid bssid;
     76 	struct ether_addr *ea;
     77 
     78 	if (d != 0) {
     79 		/* no BSSID is especially desired */
     80 		memset(&bssid.i_bssid, 0, sizeof(bssid.i_bssid));
     81 	} else {
     82 		ea = ether_aton(val);
     83 		if (ea == NULL) {
     84 			warnx("malformed BSSID: %s", val);
     85 			return;
     86 		}
     87 		memcpy(&bssid.i_bssid, ea->ether_addr_octet,
     88 		    sizeof(bssid.i_bssid));
     89 	}
     90 	(void)strncpy(bssid.i_name, name, sizeof(bssid.i_name));
     91 	if (ioctl(s, SIOCS80211BSSID, &bssid) == -1)
     92 		warn("SIOCS80211BSSID");
     93 }
     94 
     95 void
     96 setifchan(const char *val, int d)
     97 {
     98 	struct ieee80211chanreq channel;
     99 	int chan;
    100 
    101 	if (d != 0)
    102 		chan = IEEE80211_CHAN_ANY;
    103 	else {
    104 		chan = atoi(val);
    105 		if (chan < 0 || chan > 0xffff) {
    106 			warnx("invalid channel: %s", val);
    107 			return;
    108 		}
    109 	}
    110 
    111 	(void)strncpy(channel.i_name, name, sizeof(channel.i_name));
    112 	channel.i_channel = (u_int16_t) chan;
    113 	if (ioctl(s, SIOCS80211CHANNEL, &channel) == -1)
    114 		warn("SIOCS80211CHANNEL");
    115 }
    116 
    117 void
    118 setifnwkey(const char *val, int d)
    119 {
    120 	struct ieee80211_nwkey nwkey;
    121 	int i;
    122 	u_int8_t keybuf[IEEE80211_WEP_NKID][16];
    123 
    124 	nwkey.i_wepon = IEEE80211_NWKEY_WEP;
    125 	nwkey.i_defkid = 1;
    126 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    127 		nwkey.i_key[i].i_keylen = sizeof(keybuf[i]);
    128 		nwkey.i_key[i].i_keydat = keybuf[i];
    129 	}
    130 	if (d != 0) {
    131 		/* disable WEP encryption */
    132 		nwkey.i_wepon = 0;
    133 		i = 0;
    134 	} else if (strcasecmp("persist", val) == 0) {
    135 		/* use all values from persistent memory */
    136 		nwkey.i_wepon |= IEEE80211_NWKEY_PERSIST;
    137 		nwkey.i_defkid = 0;
    138 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
    139 			nwkey.i_key[i].i_keylen = -1;
    140 	} else if (strncasecmp("persist:", val, 8) == 0) {
    141 		val += 8;
    142 		/* program keys in persistent memory */
    143 		nwkey.i_wepon |= IEEE80211_NWKEY_PERSIST;
    144 		goto set_nwkey;
    145 	} else {
    146   set_nwkey:
    147 		if (isdigit((unsigned char)val[0]) && val[1] == ':') {
    148 			/* specifying a full set of four keys */
    149 			nwkey.i_defkid = val[0] - '0';
    150 			val += 2;
    151 			for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    152 				val = get_string(val, ",", keybuf[i],
    153 				    &nwkey.i_key[i].i_keylen);
    154 				if (val == NULL)
    155 					return;
    156 			}
    157 			if (*val != '\0') {
    158 				warnx("SIOCS80211NWKEY: too many keys.");
    159 				return;
    160 			}
    161 		} else {
    162 			val = get_string(val, NULL, keybuf[0],
    163 			    &nwkey.i_key[0].i_keylen);
    164 			if (val == NULL)
    165 				return;
    166 			i = 1;
    167 		}
    168 	}
    169 	for (; i < IEEE80211_WEP_NKID; i++)
    170 		nwkey.i_key[i].i_keylen = 0;
    171 	(void)strncpy(nwkey.i_name, name, sizeof(nwkey.i_name));
    172 	if (ioctl(s, SIOCS80211NWKEY, &nwkey) == -1)
    173 		warn("SIOCS80211NWKEY");
    174 }
    175 
    176 void
    177 setifpowersave(const char *val, int d)
    178 {
    179 	struct ieee80211_power power;
    180 
    181 	(void)strncpy(power.i_name, name, sizeof(power.i_name));
    182 	if (ioctl(s, SIOCG80211POWER, &power) == -1) {
    183 		warn("SIOCG80211POWER");
    184 		return;
    185 	}
    186 
    187 	power.i_enabled = d;
    188 	if (ioctl(s, SIOCS80211POWER, &power) == -1)
    189 		warn("SIOCS80211POWER");
    190 }
    191 
    192 void
    193 setifpowersavesleep(const char *val, int d)
    194 {
    195 	struct ieee80211_power power;
    196 
    197 	(void)strncpy(power.i_name, name, sizeof(power.i_name));
    198 	if (ioctl(s, SIOCG80211POWER, &power) == -1) {
    199 		warn("SIOCG80211POWER");
    200 		return;
    201 	}
    202 
    203 	power.i_maxsleep = atoi(val);
    204 	if (ioctl(s, SIOCS80211POWER, &power) == -1)
    205 		warn("SIOCS80211POWER");
    206 }
    207 
    208 void
    209 ieee80211_statistics(void)
    210 {
    211 	struct ieee80211_stats stats;
    212 
    213 	memset(&ifr, 0, sizeof(ifr));
    214 	ifr.ifr_data = (caddr_t)&stats;
    215 	(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    216 	if (ioctl(s, (zflag) ? SIOCG80211ZSTATS : SIOCG80211STATS,
    217 	    (caddr_t)&ifr) == -1)
    218 		return;
    219 #define	RX_PRINT(desc, member) printf("\trx " desc ": %u\n", stats.member)
    220 #define	TX_PRINT(desc, member) printf("\ttx " desc ": %u\n", stats.member)
    221 
    222 	RX_PRINT("too short", is_rx_tooshort);
    223 	RX_PRINT("bad version", is_rx_badversion);
    224 	RX_PRINT("wrong bss", is_rx_wrongbss);
    225 	RX_PRINT("duplicate", is_rx_dup);
    226 	RX_PRINT("wrong direction", is_rx_wrongdir);
    227 	RX_PRINT("multicast echo", is_rx_mcastecho);
    228 	RX_PRINT("STA not associated", is_rx_notassoc);
    229 	RX_PRINT("WEP-encrypted but WEP not configured", is_rx_nowep);
    230 	RX_PRINT("WEP processing failed", is_rx_wepfail);
    231 #if 0
    232 	RX_PRINT("single (M)MSDU, both WEP/non-WEP fragments", is_rx_wepmix);
    233 	RX_PRINT("non-consecutive fragments", is_rx_fragorder);
    234 #endif
    235 	RX_PRINT("decapsulation failed", is_rx_decap);
    236 	RX_PRINT("management-type discarded", is_rx_mgtdiscard);
    237 	RX_PRINT("control-type discarded", is_rx_ctl);
    238 	RX_PRINT("truncated rate set", is_rx_rstoobig);
    239 	RX_PRINT("beacon/prresp element missing", is_rx_elem_missing);
    240 	RX_PRINT("beacon/prresp element too big", is_rx_elem_toobig);
    241 	RX_PRINT("beacon/prresp element too small", is_rx_elem_toosmall);
    242 	RX_PRINT("beacon/prresp element unknown", is_rx_elem_unknown);
    243 	RX_PRINT("invalid channel", is_rx_badchan);
    244 	RX_PRINT("channel mismatch", is_rx_chanmismatch);
    245 	RX_PRINT("failed node allocation", is_rx_nodealloc);
    246 	RX_PRINT("SSID mismatch", is_rx_ssidmismatch);
    247 	RX_PRINT("unsupported authentication algor.", is_rx_auth_unsupported);
    248 	RX_PRINT("STA authentication failure", is_rx_auth_fail);
    249 	RX_PRINT("association for wrong bss", is_rx_assoc_bss);
    250 	RX_PRINT("association without authenication", is_rx_assoc_notauth);
    251 	RX_PRINT("association capability mismatch", is_rx_assoc_capmismatch);
    252 	RX_PRINT("association without rate match", is_rx_assoc_norate);
    253 	RX_PRINT("deauthentication", is_rx_deauth);
    254 	RX_PRINT("disassocation", is_rx_disassoc);
    255 	RX_PRINT("unknown subtype", is_rx_badsubtype);
    256 	RX_PRINT("failed, mbuf unavailable", is_rx_nombuf);
    257 	RX_PRINT("failed, bad ICV", is_rx_decryptcrc);
    258 	RX_PRINT("discard mgmt frame in ad-hoc demo mode", is_rx_ahdemo_mgt);
    259 	RX_PRINT("bad authentication", is_rx_bad_auth);
    260 	TX_PRINT("failed, mbuf unavailable", is_tx_nombuf);
    261 	TX_PRINT("failed, no node", is_tx_nonode);
    262 	TX_PRINT("unknown mgmt frame", is_tx_unknownmgt);
    263 	printf("\tactive scans: %u\n", stats.is_scan_active);
    264 	printf("\tpassive scans: %u\n", stats.is_scan_passive);
    265 	printf("\tnodes timed-out for inactivity: %u\n",
    266 	    stats.is_node_timeout);
    267 	printf("\tcrypto context memory unavailable: %u\n",
    268 	    stats.is_crypto_nomem);
    269 }
    270 
    271 void
    272 ieee80211_status(void)
    273 {
    274 	int i, nwkey_verbose;
    275 	struct ieee80211_nwid nwid;
    276 	struct ieee80211_nwkey nwkey;
    277 	struct ieee80211_power power;
    278 	u_int8_t keybuf[IEEE80211_WEP_NKID][16];
    279 	struct ieee80211_bssid bssid;
    280 	struct ieee80211chanreq channel;
    281 	struct ether_addr ea;
    282 	static const u_int8_t zero_macaddr[IEEE80211_ADDR_LEN];
    283 
    284 	memset(&ifr, 0, sizeof(ifr));
    285 	ifr.ifr_data = (void *)&nwid;
    286 	(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    287 	if (ioctl(s, SIOCG80211NWID, &ifr) == -1)
    288 		return;
    289 	if (nwid.i_len > IEEE80211_NWID_LEN) {
    290 		warnx("SIOCG80211NWID: wrong length of nwid (%d)", nwid.i_len);
    291 		return;
    292 	}
    293 	printf("\tssid ");
    294 	print_string(nwid.i_nwid, nwid.i_len);
    295 	memset(&nwkey, 0, sizeof(nwkey));
    296 	(void)strncpy(nwkey.i_name, name, sizeof(nwkey.i_name));
    297 	/* show nwkey only when WEP is enabled */
    298 	if (ioctl(s, SIOCG80211NWKEY, &nwkey) == -1 ||
    299 	    nwkey.i_wepon == 0) {
    300 		printf("\n");
    301 		goto skip_wep;
    302 	}
    303 
    304 	printf(" nwkey ");
    305 	/* try to retrieve WEP keys */
    306 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    307 		nwkey.i_key[i].i_keydat = keybuf[i];
    308 		nwkey.i_key[i].i_keylen = sizeof(keybuf[i]);
    309 	}
    310 	if (ioctl(s, SIOCG80211NWKEY, &nwkey) == -1) {
    311 		printf("*****");
    312 	} else {
    313 		nwkey_verbose = 0;
    314 		/* check to see non default key or multiple keys defined */
    315 		if (nwkey.i_defkid != 1) {
    316 			nwkey_verbose = 1;
    317 		} else {
    318 			for (i = 1; i < IEEE80211_WEP_NKID; i++) {
    319 				if (nwkey.i_key[i].i_keylen != 0) {
    320 					nwkey_verbose = 1;
    321 					break;
    322 				}
    323 			}
    324 		}
    325 		/* check extra ambiguity with keywords */
    326 		if (!nwkey_verbose) {
    327 			if (nwkey.i_key[0].i_keylen >= 2 &&
    328 			    isdigit(nwkey.i_key[0].i_keydat[0]) &&
    329 			    nwkey.i_key[0].i_keydat[1] == ':')
    330 				nwkey_verbose = 1;
    331 			else if (nwkey.i_key[0].i_keylen >= 7 &&
    332 			    strncasecmp("persist", nwkey.i_key[0].i_keydat, 7)
    333 			    == 0)
    334 				nwkey_verbose = 1;
    335 		}
    336 		if (nwkey_verbose)
    337 			printf("%d:", nwkey.i_defkid);
    338 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    339 			if (i > 0)
    340 				printf(",");
    341 			if (nwkey.i_key[i].i_keylen < 0)
    342 				printf("persist");
    343 			else
    344 				print_string(nwkey.i_key[i].i_keydat,
    345 				    nwkey.i_key[i].i_keylen);
    346 			if (!nwkey_verbose)
    347 				break;
    348 		}
    349 	}
    350 	printf("\n");
    351 
    352  skip_wep:
    353 	(void)strncpy(power.i_name, name, sizeof(power.i_name));
    354 	if (ioctl(s, SIOCG80211POWER, &power) == -1)
    355 		goto skip_power;
    356 	printf("\tpowersave ");
    357 	if (power.i_enabled)
    358 		printf("on (%dms sleep)", power.i_maxsleep);
    359 	else
    360 		printf("off");
    361 	printf("\n");
    362 
    363  skip_power:
    364 	(void)strncpy(bssid.i_name, name, sizeof(bssid.i_name));
    365 	if (ioctl(s, SIOCG80211BSSID, &bssid) == -1)
    366 		return;
    367 	(void)strncpy(channel.i_name, name, sizeof(channel.i_name));
    368 	if (ioctl(s, SIOCG80211CHANNEL, &channel) == -1)
    369 		return;
    370 	if (memcmp(bssid.i_bssid, zero_macaddr, IEEE80211_ADDR_LEN) == 0) {
    371 		if (channel.i_channel != (u_int16_t)-1)
    372 			printf("\tchan %d\n", channel.i_channel);
    373 	} else {
    374 		memcpy(ea.ether_addr_octet, bssid.i_bssid,
    375 		    sizeof(ea.ether_addr_octet));
    376 		printf("\tbssid %s", ether_ntoa(&ea));
    377 		if (channel.i_channel != IEEE80211_CHAN_ANY)
    378 			printf(" chan %d", channel.i_channel);
    379 		printf("\n");
    380 	}
    381 }
    382