ifieee80211.c revision 151827
177218Sphk/*
277218Sphk * Copyright 2001 The Aerospace Corporation.  All rights reserved.
377218Sphk *
477218Sphk * Redistribution and use in source and binary forms, with or without
577218Sphk * modification, are permitted provided that the following conditions
677218Sphk * are met:
777218Sphk * 1. Redistributions of source code must retain the above copyright
877218Sphk *    notice, this list of conditions and the following disclaimer.
977218Sphk * 2. Redistributions in binary form must reproduce the above copyright
1077218Sphk *    notice, this list of conditions and the following disclaimer in the
1177218Sphk *    documentation and/or other materials provided with the distribution.
1291454Sbrooks * 3. The name of The Aerospace Corporation may not be used to endorse or
1391454Sbrooks *    promote products derived from this software.
1477218Sphk *
1577218Sphk * THIS SOFTWARE IS PROVIDED BY THE AEROSPACE CORPORATION ``AS IS'' AND
1677218Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1777218Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1877218Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE AEROSPACE CORPORATION BE LIABLE
1977218Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2077218Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2177218Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2277218Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2377218Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2477218Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2577218Sphk * SUCH DAMAGE.
2677218Sphk *
2777218Sphk * $FreeBSD: head/sbin/ifconfig/ifieee80211.c 151827 2005-10-28 21:57:04Z brooks $
2877218Sphk */
2977218Sphk
3077218Sphk/*-
3177218Sphk * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
3277218Sphk * All rights reserved.
3377218Sphk *
3477218Sphk * This code is derived from software contributed to The NetBSD Foundation
3577218Sphk * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
3677218Sphk * NASA Ames Research Center.
3777218Sphk *
3877218Sphk * Redistribution and use in source and binary forms, with or without
3977218Sphk * modification, are permitted provided that the following conditions
4077218Sphk * are met:
4177218Sphk * 1. Redistributions of source code must retain the above copyright
4277218Sphk *    notice, this list of conditions and the following disclaimer.
4377218Sphk * 2. Redistributions in binary form must reproduce the above copyright
4477218Sphk *    notice, this list of conditions and the following disclaimer in the
4577218Sphk *    documentation and/or other materials provided with the distribution.
4677218Sphk * 3. All advertising materials mentioning features or use of this software
4777218Sphk *    must display the following acknowledgement:
4877218Sphk *	This product includes software developed by the NetBSD
4977218Sphk *	Foundation, Inc. and its contributors.
5077218Sphk * 4. Neither the name of The NetBSD Foundation nor the names of its
5177218Sphk *    contributors may be used to endorse or promote products derived
5277218Sphk *    from this software without specific prior written permission.
5377218Sphk *
5477218Sphk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
5577218Sphk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
5677218Sphk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5777218Sphk * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
5877218Sphk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
5977218Sphk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
6077218Sphk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
6177218Sphk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
6277218Sphk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
6377218Sphk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
6477218Sphk * POSSIBILITY OF SUCH DAMAGE.
6577218Sphk */
6677218Sphk
6777218Sphk#include <sys/param.h>
6877218Sphk#include <sys/ioctl.h>
6977218Sphk#include <sys/socket.h>
7077218Sphk#include <sys/sysctl.h>
7177218Sphk#include <sys/time.h>
7277218Sphk
7377218Sphk#include <net/ethernet.h>
7477218Sphk#include <net/if.h>
7577218Sphk#include <net/if_dl.h>
7677218Sphk#include <net/if_types.h>
77138593Ssam#include <net/if_media.h>
7877218Sphk#include <net/route.h>
79138593Ssam
80116957Ssam#include <net80211/ieee80211.h>
81120178Ssam#include <net80211/ieee80211_crypto.h>
82116957Ssam#include <net80211/ieee80211_ioctl.h>
8377218Sphk
8477218Sphk#include <ctype.h>
8577218Sphk#include <err.h>
8677218Sphk#include <errno.h>
8777218Sphk#include <fcntl.h>
88146873Sjhb#include <inttypes.h>
8977218Sphk#include <stdio.h>
9077218Sphk#include <stdlib.h>
9177218Sphk#include <string.h>
9277218Sphk#include <unistd.h>
9377218Sphk
9477218Sphk#include "ifconfig.h"
9577218Sphk
9677218Sphkstatic void set80211(int s, int type, int val, int len, u_int8_t *data);
9777218Sphkstatic const char *get_string(const char *val, const char *sep,
9877218Sphk    u_int8_t *buf, int *lenp);
9977218Sphkstatic void print_string(const u_int8_t *buf, int len);
10077218Sphk
101138593Ssamstatic int
102138593Ssamisanyarg(const char *arg)
103138593Ssam{
104138593Ssam	return (strcmp(arg, "-") == 0 ||
105138593Ssam	    strcasecmp(arg, "any") == 0 || strcasecmp(arg, "off") == 0);
106138593Ssam}
107138593Ssam
108138593Ssamstatic void
10977218Sphkset80211ssid(const char *val, int d, int s, const struct afswtch *rafp)
11077218Sphk{
11177218Sphk	int		ssid;
11277218Sphk	int		len;
11377218Sphk	u_int8_t	data[33];
11477218Sphk
11577218Sphk	ssid = 0;
116121827Sbrooks	len = strlen(val);
11788748Sambrisko	if (len > 2 && isdigit(val[0]) && val[1] == ':') {
11888748Sambrisko		ssid = atoi(val)-1;
11988748Sambrisko		val += 2;
12088748Sambrisko	}
12177218Sphk
12277218Sphk	bzero(data, sizeof(data));
12377218Sphk	len = sizeof(data);
12477218Sphk	get_string(val, NULL, data, &len);
12577218Sphk
12677218Sphk	set80211(s, IEEE80211_IOC_SSID, ssid, len, data);
12777218Sphk}
12877218Sphk
129138593Ssamstatic void
13077218Sphkset80211stationname(const char *val, int d, int s, const struct afswtch *rafp)
13177218Sphk{
13277218Sphk	int			len;
13377218Sphk	u_int8_t		data[33];
13477218Sphk
13577218Sphk	bzero(data, sizeof(data));
13677218Sphk	len = sizeof(data);
13777218Sphk	get_string(val, NULL, data, &len);
13877218Sphk
13977218Sphk	set80211(s, IEEE80211_IOC_STATIONNAME, 0, len, data);
14077218Sphk}
14177218Sphk
142138593Ssam/*
143138593Ssam * Convert IEEE channel number to MHz frequency.
144138593Ssam */
145138593Ssamstatic u_int
146138593Ssamieee80211_ieee2mhz(u_int chan)
147138593Ssam{
148138593Ssam	if (chan == 14)
149138593Ssam		return 2484;
150138593Ssam	if (chan < 14)			/* 0-13 */
151138593Ssam		return 2407 + chan*5;
152138593Ssam	if (chan < 27)			/* 15-26 */
153138593Ssam		return 2512 + ((chan-15)*20);
154138593Ssam	return 5000 + (chan*5);
155138593Ssam}
156138593Ssam
157138593Ssam/*
158138593Ssam * Convert MHz frequency to IEEE channel number.
159138593Ssam */
160138593Ssamstatic u_int
161138593Ssamieee80211_mhz2ieee(u_int freq)
162138593Ssam{
163138593Ssam	if (freq == 2484)
164138593Ssam		return 14;
165138593Ssam	if (freq < 2484)
166138593Ssam		return (freq - 2407) / 5;
167138593Ssam	if (freq < 5000)
168138593Ssam		return 15 + ((freq - 2512) / 20);
169138593Ssam	return (freq - 5000) / 5;
170138593Ssam}
171138593Ssam
172138593Ssamstatic void
17377218Sphkset80211channel(const char *val, int d, int s, const struct afswtch *rafp)
17477218Sphk{
175138593Ssam	if (!isanyarg(val)) {
176138593Ssam		int v = atoi(val);
177138593Ssam		if (v > 255)		/* treat as frequency */
178138593Ssam			v = ieee80211_mhz2ieee(v);
179138593Ssam		set80211(s, IEEE80211_IOC_CHANNEL, v, 0, NULL);
180138593Ssam	} else
181116957Ssam		set80211(s, IEEE80211_IOC_CHANNEL, IEEE80211_CHAN_ANY, 0, NULL);
18277218Sphk}
18377218Sphk
184138593Ssamstatic void
18577218Sphkset80211authmode(const char *val, int d, int s, const struct afswtch *rafp)
18677218Sphk{
18777218Sphk	int	mode;
18877218Sphk
18991454Sbrooks	if (strcasecmp(val, "none") == 0) {
19077218Sphk		mode = IEEE80211_AUTH_NONE;
19191454Sbrooks	} else if (strcasecmp(val, "open") == 0) {
19277218Sphk		mode = IEEE80211_AUTH_OPEN;
19391454Sbrooks	} else if (strcasecmp(val, "shared") == 0) {
19477218Sphk		mode = IEEE80211_AUTH_SHARED;
195138593Ssam	} else if (strcasecmp(val, "8021x") == 0) {
196138593Ssam		mode = IEEE80211_AUTH_8021X;
197138593Ssam	} else if (strcasecmp(val, "wpa") == 0) {
198138593Ssam		mode = IEEE80211_AUTH_WPA;
19977218Sphk	} else {
200150708Sru		errx(1, "unknown authmode");
20177218Sphk	}
20277218Sphk
20377218Sphk	set80211(s, IEEE80211_IOC_AUTHMODE, mode, 0, NULL);
20477218Sphk}
20577218Sphk
206138593Ssamstatic void
20777218Sphkset80211powersavemode(const char *val, int d, int s, const struct afswtch *rafp)
20877218Sphk{
20977218Sphk	int	mode;
21077218Sphk
21191454Sbrooks	if (strcasecmp(val, "off") == 0) {
21277218Sphk		mode = IEEE80211_POWERSAVE_OFF;
21391454Sbrooks	} else if (strcasecmp(val, "on") == 0) {
21477218Sphk		mode = IEEE80211_POWERSAVE_ON;
21591454Sbrooks	} else if (strcasecmp(val, "cam") == 0) {
21677218Sphk		mode = IEEE80211_POWERSAVE_CAM;
21791454Sbrooks	} else if (strcasecmp(val, "psp") == 0) {
21877218Sphk		mode = IEEE80211_POWERSAVE_PSP;
21991454Sbrooks	} else if (strcasecmp(val, "psp-cam") == 0) {
22077218Sphk		mode = IEEE80211_POWERSAVE_PSP_CAM;
22177218Sphk	} else {
222150708Sru		errx(1, "unknown powersavemode");
22377218Sphk	}
22477218Sphk
22577218Sphk	set80211(s, IEEE80211_IOC_POWERSAVE, mode, 0, NULL);
22677218Sphk}
22777218Sphk
228138593Ssamstatic void
22977218Sphkset80211powersave(const char *val, int d, int s, const struct afswtch *rafp)
23077218Sphk{
23177218Sphk	if (d == 0)
23277218Sphk		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_OFF,
23377218Sphk		    0, NULL);
23477218Sphk	else
23577218Sphk		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_ON,
23677218Sphk		    0, NULL);
23777218Sphk}
23877218Sphk
239138593Ssamstatic void
24077218Sphkset80211powersavesleep(const char *val, int d, int s, const struct afswtch *rafp)
24177218Sphk{
24277218Sphk	set80211(s, IEEE80211_IOC_POWERSAVESLEEP, atoi(val), 0, NULL);
24377218Sphk}
24477218Sphk
245138593Ssamstatic void
24677218Sphkset80211wepmode(const char *val, int d, int s, const struct afswtch *rafp)
24777218Sphk{
24877218Sphk	int	mode;
24977218Sphk
25091454Sbrooks	if (strcasecmp(val, "off") == 0) {
25177218Sphk		mode = IEEE80211_WEP_OFF;
25291454Sbrooks	} else if (strcasecmp(val, "on") == 0) {
25377218Sphk		mode = IEEE80211_WEP_ON;
25491454Sbrooks	} else if (strcasecmp(val, "mixed") == 0) {
25577218Sphk		mode = IEEE80211_WEP_MIXED;
25677218Sphk	} else {
257150708Sru		errx(1, "unknown wep mode");
25877218Sphk	}
25977218Sphk
26077218Sphk	set80211(s, IEEE80211_IOC_WEP, mode, 0, NULL);
26177218Sphk}
26277218Sphk
263138593Ssamstatic void
26477218Sphkset80211wep(const char *val, int d, int s, const struct afswtch *rafp)
26577218Sphk{
26677218Sphk	set80211(s, IEEE80211_IOC_WEP, d, 0, NULL);
26777218Sphk}
26877218Sphk
269139493Ssamstatic int
270139493Ssamisundefarg(const char *arg)
271139493Ssam{
272139493Ssam	return (strcmp(arg, "-") == 0 || strncasecmp(arg, "undef", 5) == 0);
273139493Ssam}
274139493Ssam
275138593Ssamstatic void
27677218Sphkset80211weptxkey(const char *val, int d, int s, const struct afswtch *rafp)
27777218Sphk{
278139493Ssam	if (isundefarg(val))
279139493Ssam		set80211(s, IEEE80211_IOC_WEPTXKEY, IEEE80211_KEYIX_NONE, 0, NULL);
280139493Ssam	else
281139493Ssam		set80211(s, IEEE80211_IOC_WEPTXKEY, atoi(val)-1, 0, NULL);
28277218Sphk}
28377218Sphk
284138593Ssamstatic void
28577218Sphkset80211wepkey(const char *val, int d, int s, const struct afswtch *rafp)
28677218Sphk{
28777218Sphk	int		key = 0;
28877218Sphk	int		len;
289120178Ssam	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
29077218Sphk
29191454Sbrooks	if (isdigit(val[0]) && val[1] == ':') {
29277218Sphk		key = atoi(val)-1;
29377218Sphk		val += 2;
29477218Sphk	}
29577218Sphk
29677218Sphk	bzero(data, sizeof(data));
29777218Sphk	len = sizeof(data);
29877218Sphk	get_string(val, NULL, data, &len);
29977218Sphk
30077218Sphk	set80211(s, IEEE80211_IOC_WEPKEY, key, len, data);
30177218Sphk}
30277218Sphk
30377218Sphk/*
304148686Sstefanf * This function is purely a NetBSD compatability interface.  The NetBSD
305148686Sstefanf * interface is too inflexible, but it's there so we'll support it since
30677218Sphk * it's not all that hard.
30777218Sphk */
308138593Ssamstatic void
30977218Sphkset80211nwkey(const char *val, int d, int s, const struct afswtch *rafp)
31077218Sphk{
31177218Sphk	int		txkey;
31277218Sphk	int		i, len;
313120178Ssam	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
31477218Sphk
31577218Sphk	set80211(s, IEEE80211_IOC_WEP, IEEE80211_WEP_ON, 0, NULL);
31677218Sphk
31791454Sbrooks	if (isdigit(val[0]) && val[1] == ':') {
31877218Sphk		txkey = val[0]-'0'-1;
31977218Sphk		val += 2;
32077218Sphk
32191454Sbrooks		for (i = 0; i < 4; i++) {
32277218Sphk			bzero(data, sizeof(data));
32377218Sphk			len = sizeof(data);
32477218Sphk			val = get_string(val, ",", data, &len);
325151827Sbrooks			if (val == NULL)
326151827Sbrooks				exit(1);
32777218Sphk
32877218Sphk			set80211(s, IEEE80211_IOC_WEPKEY, i, len, data);
32977218Sphk		}
33077218Sphk	} else {
33177218Sphk		bzero(data, sizeof(data));
33277218Sphk		len = sizeof(data);
33377218Sphk		get_string(val, NULL, data, &len);
33477218Sphk		txkey = 0;
33577218Sphk
33677218Sphk		set80211(s, IEEE80211_IOC_WEPKEY, 0, len, data);
33777218Sphk
33877218Sphk		bzero(data, sizeof(data));
33991454Sbrooks		for (i = 1; i < 4; i++)
34077218Sphk			set80211(s, IEEE80211_IOC_WEPKEY, i, 0, data);
34177218Sphk	}
34277218Sphk
34377218Sphk	set80211(s, IEEE80211_IOC_WEPTXKEY, txkey, 0, NULL);
34477218Sphk}
34577218Sphk
346138593Ssamstatic void
347127649Ssamset80211rtsthreshold(const char *val, int d, int s, const struct afswtch *rafp)
348127649Ssam{
349148416Ssam	set80211(s, IEEE80211_IOC_RTSTHRESHOLD,
350148416Ssam		isundefarg(val) ? IEEE80211_RTS_MAX : atoi(val), 0, NULL);
351127649Ssam}
352127649Ssam
353138593Ssamstatic void
354127649Ssamset80211protmode(const char *val, int d, int s, const struct afswtch *rafp)
355127649Ssam{
356127649Ssam	int	mode;
357127649Ssam
358127649Ssam	if (strcasecmp(val, "off") == 0) {
359127649Ssam		mode = IEEE80211_PROTMODE_OFF;
360127649Ssam	} else if (strcasecmp(val, "cts") == 0) {
361127649Ssam		mode = IEEE80211_PROTMODE_CTS;
362127649Ssam	} else if (strcasecmp(val, "rtscts") == 0) {
363127649Ssam		mode = IEEE80211_PROTMODE_RTSCTS;
364127649Ssam	} else {
365150708Sru		errx(1, "unknown protection mode");
366127649Ssam	}
367127649Ssam
368127649Ssam	set80211(s, IEEE80211_IOC_PROTMODE, mode, 0, NULL);
369127649Ssam}
370127649Ssam
371138593Ssamstatic void
372127649Ssamset80211txpower(const char *val, int d, int s, const struct afswtch *rafp)
373127649Ssam{
374127649Ssam	set80211(s, IEEE80211_IOC_TXPOWER, atoi(val), 0, NULL);
375127649Ssam}
376127649Ssam
377138593Ssam#define	IEEE80211_ROAMING_DEVICE	0
378138593Ssam#define	IEEE80211_ROAMING_AUTO		1
379138593Ssam#define	IEEE80211_ROAMING_MANUAL	2
380138593Ssam
381138593Ssamstatic void
382138593Ssamset80211roaming(const char *val, int d, int s, const struct afswtch *rafp)
38377218Sphk{
384138593Ssam	int mode;
38577218Sphk
386138593Ssam	if (strcasecmp(val, "device") == 0) {
387138593Ssam		mode = IEEE80211_ROAMING_DEVICE;
388138593Ssam	} else if (strcasecmp(val, "auto") == 0) {
389138593Ssam		mode = IEEE80211_ROAMING_AUTO;
390138593Ssam	} else if (strcasecmp(val, "manual") == 0) {
391138593Ssam		mode = IEEE80211_ROAMING_MANUAL;
392138593Ssam	} else {
393150708Sru		errx(1, "unknown roaming mode");
394138593Ssam	}
395138593Ssam	set80211(s, IEEE80211_IOC_ROAMING, mode, 0, NULL);
396138593Ssam}
397138593Ssam
398138593Ssamstatic void
399138593Ssamset80211wme(const char *val, int d, int s, const struct afswtch *rafp)
400138593Ssam{
401138593Ssam	set80211(s, IEEE80211_IOC_WME, d, 0, NULL);
402138593Ssam}
403138593Ssam
404138593Ssamstatic void
405138593Ssamset80211hidessid(const char *val, int d, int s, const struct afswtch *rafp)
406138593Ssam{
407138593Ssam	set80211(s, IEEE80211_IOC_HIDESSID, d, 0, NULL);
408138593Ssam}
409138593Ssam
410138593Ssamstatic void
411138593Ssamset80211apbridge(const char *val, int d, int s, const struct afswtch *rafp)
412138593Ssam{
413138593Ssam	set80211(s, IEEE80211_IOC_APBRIDGE, d, 0, NULL);
414138593Ssam}
415138593Ssam
416138593Ssamstatic void
417138593Ssamset80211chanlist(const char *val, int d, int s, const struct afswtch *rafp)
418138593Ssam{
419138593Ssam	struct ieee80211req_chanlist chanlist;
420138593Ssam#define	MAXCHAN	(sizeof(chanlist.ic_channels)*NBBY)
421138593Ssam	char *temp, *cp, *tp;
422138593Ssam
423138593Ssam	temp = malloc(strlen(val) + 1);
424138593Ssam	if (temp == NULL)
425138593Ssam		errx(1, "malloc failed");
426138593Ssam	strcpy(temp, val);
427138593Ssam	memset(&chanlist, 0, sizeof(chanlist));
428138593Ssam	cp = temp;
429138593Ssam	for (;;) {
430138593Ssam		int first, last, f;
431138593Ssam
432138593Ssam		tp = strchr(cp, ',');
433138593Ssam		if (tp != NULL)
434138593Ssam			*tp++ = '\0';
435138593Ssam		switch (sscanf(cp, "%u-%u", &first, &last)) {
436138593Ssam		case 1:
437138593Ssam			if (first > MAXCHAN)
438146873Sjhb				errx(-1, "channel %u out of range, max %zu",
439138593Ssam					first, MAXCHAN);
440138593Ssam			setbit(chanlist.ic_channels, first);
441138593Ssam			break;
442138593Ssam		case 2:
443138593Ssam			if (first > MAXCHAN)
444146873Sjhb				errx(-1, "channel %u out of range, max %zu",
445138593Ssam					first, MAXCHAN);
446138593Ssam			if (last > MAXCHAN)
447146873Sjhb				errx(-1, "channel %u out of range, max %zu",
448138593Ssam					last, MAXCHAN);
449138593Ssam			if (first > last)
450138593Ssam				errx(-1, "void channel range, %u > %u",
451138593Ssam					first, last);
452138593Ssam			for (f = first; f <= last; f++)
453138593Ssam				setbit(chanlist.ic_channels, f);
454138593Ssam			break;
455138593Ssam		}
456138593Ssam		if (tp == NULL)
457138593Ssam			break;
458138593Ssam		while (isspace(*tp))
459138593Ssam			tp++;
460138593Ssam		if (!isdigit(*tp))
461138593Ssam			break;
462138593Ssam		cp = tp;
463138593Ssam	}
464138593Ssam	set80211(s, IEEE80211_IOC_CHANLIST, 0,
465138593Ssam		sizeof(chanlist), (uint8_t *) &chanlist);
466138593Ssam#undef MAXCHAN
467138593Ssam}
468138593Ssam
469138593Ssamstatic void
470138593Ssamset80211bssid(const char *val, int d, int s, const struct afswtch *rafp)
471138593Ssam{
472138593Ssam
473138593Ssam	if (!isanyarg(val)) {
474138593Ssam		char *temp;
475138593Ssam		struct sockaddr_dl sdl;
476138593Ssam
477138593Ssam		temp = malloc(strlen(val) + 1);
478138593Ssam		if (temp == NULL)
479138593Ssam			errx(1, "malloc failed");
480138593Ssam		temp[0] = ':';
481138593Ssam		strcpy(temp + 1, val);
482138593Ssam		sdl.sdl_len = sizeof(sdl);
483138593Ssam		link_addr(temp, &sdl);
484138593Ssam		free(temp);
485138593Ssam		if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
486138593Ssam			errx(1, "malformed link-level address");
487138593Ssam		set80211(s, IEEE80211_IOC_BSSID, 0,
488138593Ssam			IEEE80211_ADDR_LEN, LLADDR(&sdl));
489138593Ssam	} else {
490138593Ssam		uint8_t zerobssid[IEEE80211_ADDR_LEN];
491138593Ssam		memset(zerobssid, 0, sizeof(zerobssid));
492138593Ssam		set80211(s, IEEE80211_IOC_BSSID, 0,
493138593Ssam			IEEE80211_ADDR_LEN, zerobssid);
494138593Ssam	}
495138593Ssam}
496138593Ssam
497138593Ssamstatic int
498138593Ssamgetac(const char *ac)
499138593Ssam{
500138593Ssam	if (strcasecmp(ac, "ac_be") == 0 || strcasecmp(ac, "be") == 0)
501138593Ssam		return WME_AC_BE;
502138593Ssam	if (strcasecmp(ac, "ac_bk") == 0 || strcasecmp(ac, "bk") == 0)
503138593Ssam		return WME_AC_BK;
504138593Ssam	if (strcasecmp(ac, "ac_vi") == 0 || strcasecmp(ac, "vi") == 0)
505138593Ssam		return WME_AC_VI;
506138593Ssam	if (strcasecmp(ac, "ac_vo") == 0 || strcasecmp(ac, "vo") == 0)
507138593Ssam		return WME_AC_VO;
508138593Ssam	errx(1, "unknown wme access class %s", ac);
509138593Ssam}
510138593Ssam
511138593Ssamstatic
512138593SsamDECL_CMD_FUNC2(set80211cwmin, ac, val)
513138593Ssam{
514138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val), getac(ac), NULL);
515138593Ssam}
516138593Ssam
517138593Ssamstatic
518138593SsamDECL_CMD_FUNC2(set80211cwmax, ac, val)
519138593Ssam{
520138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val), getac(ac), NULL);
521138593Ssam}
522138593Ssam
523138593Ssamstatic
524138593SsamDECL_CMD_FUNC2(set80211aifs, ac, val)
525138593Ssam{
526138593Ssam	set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val), getac(ac), NULL);
527138593Ssam}
528138593Ssam
529138593Ssamstatic
530138593SsamDECL_CMD_FUNC2(set80211txoplimit, ac, val)
531138593Ssam{
532138593Ssam	set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val), getac(ac), NULL);
533138593Ssam}
534138593Ssam
535138593Ssamstatic
536148621SsamDECL_CMD_FUNC(set80211acm, ac, d)
537138593Ssam{
538148621Ssam	set80211(s, IEEE80211_IOC_WME_ACM, 1, getac(ac), NULL);
539138593Ssam}
540148621Ssamstatic
541148621SsamDECL_CMD_FUNC(set80211noacm, ac, d)
542148621Ssam{
543148621Ssam	set80211(s, IEEE80211_IOC_WME_ACM, 0, getac(ac), NULL);
544148621Ssam}
545138593Ssam
546138593Ssamstatic
547148621SsamDECL_CMD_FUNC(set80211ackpolicy, ac, d)
548138593Ssam{
549148621Ssam	set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 1, getac(ac), NULL);
550138593Ssam}
551148621Ssamstatic
552148621SsamDECL_CMD_FUNC(set80211noackpolicy, ac, d)
553148621Ssam{
554148621Ssam	set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 0, getac(ac), NULL);
555148621Ssam}
556138593Ssam
557138593Ssamstatic
558138593SsamDECL_CMD_FUNC2(set80211bsscwmin, ac, val)
559138593Ssam{
560138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val),
561138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
562138593Ssam}
563138593Ssam
564138593Ssamstatic
565138593SsamDECL_CMD_FUNC2(set80211bsscwmax, ac, val)
566138593Ssam{
567138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val),
568138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
569138593Ssam}
570138593Ssam
571138593Ssamstatic
572138593SsamDECL_CMD_FUNC2(set80211bssaifs, ac, val)
573138593Ssam{
574138593Ssam	set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val),
575138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
576138593Ssam}
577138593Ssam
578138593Ssamstatic
579138593SsamDECL_CMD_FUNC2(set80211bsstxoplimit, ac, val)
580138593Ssam{
581138593Ssam	set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val),
582138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
583138593Ssam}
584138593Ssam
585138593Ssamstatic
586138593SsamDECL_CMD_FUNC(set80211dtimperiod, val, d)
587138593Ssam{
588138593Ssam	set80211(s, IEEE80211_IOC_DTIM_PERIOD, atoi(val), 0, NULL);
589138593Ssam}
590138593Ssam
591138593Ssamstatic
592138593SsamDECL_CMD_FUNC(set80211bintval, val, d)
593138593Ssam{
594138593Ssam	set80211(s, IEEE80211_IOC_BEACON_INTERVAL, atoi(val), 0, NULL);
595138593Ssam}
596138593Ssam
597138593Ssamstatic void
598138593Ssamset80211macmac(int s, int op, const char *val)
599138593Ssam{
600138593Ssam	char *temp;
601138593Ssam	struct sockaddr_dl sdl;
602138593Ssam
603138593Ssam	temp = malloc(strlen(val) + 1);
604138593Ssam	if (temp == NULL)
605138593Ssam		errx(1, "malloc failed");
606138593Ssam	temp[0] = ':';
607138593Ssam	strcpy(temp + 1, val);
608138593Ssam	sdl.sdl_len = sizeof(sdl);
609138593Ssam	link_addr(temp, &sdl);
610138593Ssam	free(temp);
611138593Ssam	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
612138593Ssam		errx(1, "malformed link-level address");
613138593Ssam	set80211(s, op, 0, IEEE80211_ADDR_LEN, LLADDR(&sdl));
614138593Ssam}
615138593Ssam
616138593Ssamstatic
617138593SsamDECL_CMD_FUNC(set80211addmac, val, d)
618138593Ssam{
619138593Ssam	set80211macmac(s, IEEE80211_IOC_ADDMAC, val);
620138593Ssam}
621138593Ssam
622138593Ssamstatic
623138593SsamDECL_CMD_FUNC(set80211delmac, val, d)
624138593Ssam{
625138593Ssam	set80211macmac(s, IEEE80211_IOC_DELMAC, val);
626138593Ssam}
627138593Ssam
628138593Ssamstatic
629149029SsamDECL_CMD_FUNC(set80211kickmac, val, d)
630149029Ssam{
631149029Ssam	char *temp;
632149029Ssam	struct sockaddr_dl sdl;
633149029Ssam	struct ieee80211req_mlme mlme;
634149029Ssam
635149029Ssam	temp = malloc(strlen(val) + 1);
636149029Ssam	if (temp == NULL)
637149029Ssam		errx(1, "malloc failed");
638149029Ssam	temp[0] = ':';
639149029Ssam	strcpy(temp + 1, val);
640149029Ssam	sdl.sdl_len = sizeof(sdl);
641149029Ssam	link_addr(temp, &sdl);
642149029Ssam	free(temp);
643149029Ssam	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
644149029Ssam		errx(1, "malformed link-level address");
645149029Ssam	memset(&mlme, 0, sizeof(mlme));
646149029Ssam	mlme.im_op = IEEE80211_MLME_DEAUTH;
647149029Ssam	mlme.im_reason = IEEE80211_REASON_AUTH_EXPIRE;
648149029Ssam	memcpy(mlme.im_macaddr, LLADDR(&sdl), IEEE80211_ADDR_LEN);
649149029Ssam	set80211(s, IEEE80211_IOC_MLME, 0, sizeof(mlme), (u_int8_t *) &mlme);
650149029Ssam}
651149029Ssam
652149029Ssamstatic
653138593SsamDECL_CMD_FUNC(set80211maccmd, val, d)
654138593Ssam{
655138593Ssam	set80211(s, IEEE80211_IOC_MACCMD, d, 0, NULL);
656138593Ssam}
657138593Ssam
658147795Ssamstatic void
659147795Ssamset80211pureg(const char *val, int d, int s, const struct afswtch *rafp)
660147795Ssam{
661147795Ssam	set80211(s, IEEE80211_IOC_PUREG, d, 0, NULL);
662147795Ssam}
663147795Ssam
664148416Ssamstatic
665148416SsamDECL_CMD_FUNC(set80211fragthreshold, val, d)
666148416Ssam{
667148416Ssam	set80211(s, IEEE80211_IOC_FRAGTHRESHOLD,
668148416Ssam		isundefarg(val) ? IEEE80211_FRAG_MAX : atoi(val), 0, NULL);
669148416Ssam}
670148416Ssam
671138593Ssamstatic int
672138593Ssamgetmaxrate(uint8_t rates[15], uint8_t nrates)
673138593Ssam{
674138593Ssam	int i, maxrate = -1;
675138593Ssam
676138593Ssam	for (i = 0; i < nrates; i++) {
677138593Ssam		int rate = rates[i] & IEEE80211_RATE_VAL;
678138593Ssam		if (rate > maxrate)
679138593Ssam			maxrate = rate;
680138593Ssam	}
681138593Ssam	return maxrate / 2;
682138593Ssam}
683138593Ssam
684138593Ssamstatic const char *
685138593Ssamgetcaps(int capinfo)
686138593Ssam{
687138593Ssam	static char capstring[32];
688138593Ssam	char *cp = capstring;
689138593Ssam
690138593Ssam	if (capinfo & IEEE80211_CAPINFO_ESS)
691138593Ssam		*cp++ = 'E';
692138593Ssam	if (capinfo & IEEE80211_CAPINFO_IBSS)
693138593Ssam		*cp++ = 'I';
694138593Ssam	if (capinfo & IEEE80211_CAPINFO_CF_POLLABLE)
695138593Ssam		*cp++ = 'c';
696138593Ssam	if (capinfo & IEEE80211_CAPINFO_CF_POLLREQ)
697138593Ssam		*cp++ = 'C';
698138593Ssam	if (capinfo & IEEE80211_CAPINFO_PRIVACY)
699138593Ssam		*cp++ = 'P';
700138593Ssam	if (capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)
701138593Ssam		*cp++ = 'S';
702138593Ssam	if (capinfo & IEEE80211_CAPINFO_PBCC)
703138593Ssam		*cp++ = 'B';
704138593Ssam	if (capinfo & IEEE80211_CAPINFO_CHNL_AGILITY)
705138593Ssam		*cp++ = 'A';
706138593Ssam	if (capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
707138593Ssam		*cp++ = 's';
708138593Ssam	if (capinfo & IEEE80211_CAPINFO_RSN)
709138593Ssam		*cp++ = 'R';
710138593Ssam	if (capinfo & IEEE80211_CAPINFO_DSSSOFDM)
711138593Ssam		*cp++ = 'D';
712138593Ssam	*cp = '\0';
713138593Ssam	return capstring;
714138593Ssam}
715138593Ssam
716138593Ssamstatic void
717138593Ssamprintie(const char* tag, const uint8_t *ie, size_t ielen, int maxlen)
718138593Ssam{
719138593Ssam	printf("%s", tag);
720138593Ssam	if (verbose) {
721138593Ssam		maxlen -= strlen(tag)+2;
722138593Ssam		if (2*ielen > maxlen)
723138593Ssam			maxlen--;
724138593Ssam		printf("<");
725138593Ssam		for (; ielen > 0; ie++, ielen--) {
726138593Ssam			if (maxlen-- <= 0)
727138593Ssam				break;
728138593Ssam			printf("%02x", *ie);
729138593Ssam		}
730138593Ssam		if (ielen != 0)
731138593Ssam			printf("-");
732138593Ssam		printf(">");
733138593Ssam	}
734138593Ssam}
735138593Ssam
736138593Ssam/*
737138593Ssam * Copy the ssid string contents into buf, truncating to fit.  If the
738138593Ssam * ssid is entirely printable then just copy intact.  Otherwise convert
739138593Ssam * to hexadecimal.  If the result is truncated then replace the last
740138593Ssam * three characters with "...".
741138593Ssam */
742146873Sjhbstatic int
743138593Ssamcopy_essid(char buf[], size_t bufsize, const u_int8_t *essid, size_t essid_len)
744138593Ssam{
745138593Ssam	const u_int8_t *p;
746138593Ssam	size_t maxlen;
747138593Ssam	int i;
748138593Ssam
749138593Ssam	if (essid_len > bufsize)
750138593Ssam		maxlen = bufsize;
751138593Ssam	else
752138593Ssam		maxlen = essid_len;
753138593Ssam	/* determine printable or not */
754138593Ssam	for (i = 0, p = essid; i < maxlen; i++, p++) {
755138593Ssam		if (*p < ' ' || *p > 0x7e)
756138593Ssam			break;
757138593Ssam	}
758138593Ssam	if (i != maxlen) {		/* not printable, print as hex */
759138593Ssam		if (bufsize < 3)
760138593Ssam			return 0;
761138593Ssam		strlcpy(buf, "0x", bufsize);
762138593Ssam		bufsize -= 2;
763138593Ssam		p = essid;
764138593Ssam		for (i = 0; i < maxlen && bufsize >= 2; i++) {
765147489Savatar			sprintf(&buf[2+2*i], "%02x", p[i]);
766138593Ssam			bufsize -= 2;
767138593Ssam		}
768147489Savatar		if (i != essid_len)
769147489Savatar			memcpy(&buf[2+2*i-3], "...", 3);
770138593Ssam	} else {			/* printable, truncate as needed */
771138593Ssam		memcpy(buf, essid, maxlen);
772147489Savatar		if (maxlen != essid_len)
773147489Savatar			memcpy(&buf[maxlen-3], "...", 3);
774138593Ssam	}
775138593Ssam	return maxlen;
776138593Ssam}
777138593Ssam
778148686Sstefanf/* unaligned little endian access */
779138593Ssam#define LE_READ_4(p)					\
780138593Ssam	((u_int32_t)					\
781138593Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
782138593Ssam	  (((const u_int8_t *)(p))[1] <<  8) |		\
783138593Ssam	  (((const u_int8_t *)(p))[2] << 16) |		\
784138593Ssam	  (((const u_int8_t *)(p))[3] << 24)))
785138593Ssam
786138593Ssamstatic int __inline
787138593Ssamiswpaoui(const u_int8_t *frm)
788138593Ssam{
789138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
790138593Ssam}
791138593Ssam
792138593Ssamstatic int __inline
793138593Ssamiswmeoui(const u_int8_t *frm)
794138593Ssam{
795138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI);
796138593Ssam}
797138593Ssam
798138593Ssamstatic int __inline
799138593Ssamisatherosoui(const u_int8_t *frm)
800138593Ssam{
801138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
802138593Ssam}
803138593Ssam
804138593Ssamstatic void
805138593Ssamprinties(const u_int8_t *vp, int ielen, int maxcols)
806138593Ssam{
807138593Ssam	while (ielen > 0) {
808138593Ssam		switch (vp[0]) {
809138593Ssam		case IEEE80211_ELEMID_VENDOR:
810138593Ssam			if (iswpaoui(vp))
811138593Ssam				printie(" WPA", vp, 2+vp[1], maxcols);
812138593Ssam			else if (iswmeoui(vp))
813138593Ssam				printie(" WME", vp, 2+vp[1], maxcols);
814139492Ssam			else if (isatherosoui(vp))
815139492Ssam				printie(" ATH", vp, 2+vp[1], maxcols);
816138593Ssam			else
817138593Ssam				printie(" VEN", vp, 2+vp[1], maxcols);
818138593Ssam			break;
819138593Ssam		case IEEE80211_ELEMID_RSN:
820138593Ssam			printie(" RSN", vp, 2+vp[1], maxcols);
821138593Ssam			break;
822138593Ssam		default:
823138593Ssam			printie(" ???", vp, 2+vp[1], maxcols);
824138593Ssam			break;
825138593Ssam		}
826138593Ssam		ielen -= 2+vp[1];
827138593Ssam		vp += 2+vp[1];
828138593Ssam	}
829138593Ssam}
830138593Ssam
831138593Ssamstatic void
832138593Ssamlist_scan(int s)
833138593Ssam{
834138593Ssam	uint8_t buf[24*1024];
835138593Ssam	struct ieee80211req ireq;
836138593Ssam	char ssid[14];
837138593Ssam	uint8_t *cp;
838138593Ssam	int len;
839138593Ssam
84077218Sphk	(void) memset(&ireq, 0, sizeof(ireq));
84177218Sphk	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
842138593Ssam	ireq.i_type = IEEE80211_IOC_SCAN_RESULTS;
843138593Ssam	ireq.i_data = buf;
844138593Ssam	ireq.i_len = sizeof(buf);
845138593Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
846138593Ssam		errx(1, "unable to get scan results");
847138593Ssam	len = ireq.i_len;
848138593Ssam	if (len < sizeof(struct ieee80211req_scan_result))
849138593Ssam		return;
850138593Ssam
851138593Ssam	printf("%-14.14s  %-17.17s  %4s %4s  %-5s %3s %4s\n"
852138593Ssam		, "SSID"
853138593Ssam		, "BSSID"
854138593Ssam		, "CHAN"
855138593Ssam		, "RATE"
856138593Ssam		, "S:N"
857138593Ssam		, "INT"
858138593Ssam		, "CAPS"
859138593Ssam	);
860138593Ssam	cp = buf;
861138593Ssam	do {
862138593Ssam		struct ieee80211req_scan_result *sr;
863138593Ssam		uint8_t *vp;
864138593Ssam
865138593Ssam		sr = (struct ieee80211req_scan_result *) cp;
866138593Ssam		vp = (u_int8_t *)(sr+1);
867138593Ssam		printf("%-14.*s  %s  %3d  %3dM %2d:%-2d  %3d %-4.4s"
868138593Ssam			, copy_essid(ssid, sizeof(ssid), vp, sr->isr_ssid_len)
869138593Ssam				, ssid
870138593Ssam			, ether_ntoa((const struct ether_addr *) sr->isr_bssid)
871138593Ssam			, ieee80211_mhz2ieee(sr->isr_freq)
872138593Ssam			, getmaxrate(sr->isr_rates, sr->isr_nrates)
873138593Ssam			, sr->isr_rssi, sr->isr_noise
874138593Ssam			, sr->isr_intval
875138593Ssam			, getcaps(sr->isr_capinfo)
876138593Ssam		);
877138593Ssam		printies(vp + sr->isr_ssid_len, sr->isr_ie_len, 24);;
878138593Ssam		printf("\n");
879138593Ssam		cp += sr->isr_len, len -= sr->isr_len;
880138593Ssam	} while (len >= sizeof(struct ieee80211req_scan_result));
881138593Ssam}
882138593Ssam
883138593Ssam#include <net80211/ieee80211_freebsd.h>
884138593Ssam
885138593Ssamstatic void
886138593Ssamscan_and_wait(int s)
887138593Ssam{
888138593Ssam	struct ieee80211req ireq;
889138593Ssam	int sroute;
890138593Ssam
891138593Ssam	sroute = socket(PF_ROUTE, SOCK_RAW, 0);
892138593Ssam	if (sroute < 0) {
893138593Ssam		perror("socket(PF_ROUTE,SOCK_RAW)");
894138593Ssam		return;
895138593Ssam	}
896138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
897138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
898138593Ssam	ireq.i_type = IEEE80211_IOC_SCAN_REQ;
899138593Ssam	/* NB: only root can trigger a scan so ignore errors */
900138593Ssam	if (ioctl(s, SIOCS80211, &ireq) >= 0) {
901138593Ssam		char buf[2048];
902138593Ssam		struct if_announcemsghdr *ifan;
903138593Ssam		struct rt_msghdr *rtm;
904138593Ssam
905138593Ssam		do {
906138593Ssam			if (read(sroute, buf, sizeof(buf)) < 0) {
907138593Ssam				perror("read(PF_ROUTE)");
908138593Ssam				break;
909138593Ssam			}
910138593Ssam			rtm = (struct rt_msghdr *) buf;
911138593Ssam			if (rtm->rtm_version != RTM_VERSION)
912138593Ssam				break;
913138593Ssam			ifan = (struct if_announcemsghdr *) rtm;
914138593Ssam		} while (rtm->rtm_type != RTM_IEEE80211 ||
915138593Ssam		    ifan->ifan_what != RTM_IEEE80211_SCAN);
916138593Ssam	}
917138593Ssam	close(sroute);
918138593Ssam}
919138593Ssam
920138593Ssamstatic
921138593SsamDECL_CMD_FUNC(set80211scan, val, d)
922138593Ssam{
923138593Ssam	scan_and_wait(s);
924138593Ssam	list_scan(s);
925138593Ssam}
926138593Ssam
927138593Ssamstatic void
928138593Ssamlist_stations(int s)
929138593Ssam{
930138593Ssam	uint8_t buf[24*1024];
931138593Ssam	struct ieee80211req ireq;
932138593Ssam	uint8_t *cp;
933138593Ssam	int len;
934138593Ssam
935138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
936138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
937138593Ssam	ireq.i_type = IEEE80211_IOC_STA_INFO;
938138593Ssam	ireq.i_data = buf;
939138593Ssam	ireq.i_len = sizeof(buf);
940138593Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
941138593Ssam		errx(1, "unable to get station information");
942138593Ssam	len = ireq.i_len;
943138593Ssam	if (len < sizeof(struct ieee80211req_sta_info))
944138593Ssam		return;
945138593Ssam
946138593Ssam	printf("%-17.17s %4s %4s %4s %4s %4s %6s %6s %4s %3s\n"
947138593Ssam		, "ADDR"
948138593Ssam		, "AID"
949138593Ssam		, "CHAN"
950138593Ssam		, "RATE"
951138593Ssam		, "RSSI"
952138593Ssam		, "IDLE"
953138593Ssam		, "TXSEQ"
954138593Ssam		, "RXSEQ"
955138593Ssam		, "CAPS"
956138593Ssam		, "ERP"
957138593Ssam	);
958138593Ssam	cp = buf;
959138593Ssam	do {
960138593Ssam		struct ieee80211req_sta_info *si;
961138593Ssam		uint8_t *vp;
962138593Ssam
963138593Ssam		si = (struct ieee80211req_sta_info *) cp;
964138593Ssam		vp = (u_int8_t *)(si+1);
965138593Ssam		printf("%s %4u %4d %3dM %4d %4d %6d %6d %-4.4s %3x"
966138593Ssam			, ether_ntoa((const struct ether_addr*) si->isi_macaddr)
967138593Ssam			, IEEE80211_AID(si->isi_associd)
968138593Ssam			, ieee80211_mhz2ieee(si->isi_freq)
969138593Ssam			, (si->isi_rates[si->isi_txrate] & IEEE80211_RATE_VAL)/2
970138593Ssam			, si->isi_rssi
971138593Ssam			, si->isi_inact
972138593Ssam			, si->isi_txseqs[0]
973138593Ssam			, si->isi_rxseqs[0]
974138593Ssam			, getcaps(si->isi_capinfo)
975138593Ssam			, si->isi_erp
976138593Ssam		);
977138593Ssam		printies(vp, si->isi_ie_len, 24);
978138593Ssam		printf("\n");
979138593Ssam		cp += si->isi_len, len -= si->isi_len;
980138593Ssam	} while (len >= sizeof(struct ieee80211req_sta_info));
981138593Ssam}
982138593Ssam
983138593Ssamstatic void
984138593Ssamprint_chaninfo(const struct ieee80211_channel *c)
985138593Ssam{
986138593Ssam#define	IEEE80211_IS_CHAN_PASSIVE(_c) \
987138593Ssam	(((_c)->ic_flags & IEEE80211_CHAN_PASSIVE))
988138593Ssam	char buf[14];
989138593Ssam
990138593Ssam	buf[0] = '\0';
991138593Ssam	if (IEEE80211_IS_CHAN_FHSS(c))
992138593Ssam		strlcat(buf, " FHSS", sizeof(buf));
993138593Ssam	if (IEEE80211_IS_CHAN_A(c))
994138593Ssam		strlcat(buf, " 11a", sizeof(buf));
995138593Ssam	/* XXX 11g schizophrenia */
996138593Ssam	if (IEEE80211_IS_CHAN_G(c) ||
997138593Ssam	    IEEE80211_IS_CHAN_PUREG(c))
998138593Ssam		strlcat(buf, " 11g", sizeof(buf));
999138593Ssam	else if (IEEE80211_IS_CHAN_B(c))
1000138593Ssam		strlcat(buf, " 11b", sizeof(buf));
1001138593Ssam	if (IEEE80211_IS_CHAN_T(c))
1002138593Ssam		strlcat(buf, " Turbo", sizeof(buf));
1003138593Ssam	printf("Channel %3u : %u%c Mhz%-14.14s",
1004138593Ssam		ieee80211_mhz2ieee(c->ic_freq), c->ic_freq,
1005138593Ssam		IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ', buf);
1006138593Ssam#undef IEEE80211_IS_CHAN_PASSIVE
1007138593Ssam}
1008138593Ssam
1009138593Ssamstatic void
1010138593Ssamlist_channels(int s, int allchans)
1011138593Ssam{
1012138593Ssam	struct ieee80211req ireq;
1013138593Ssam	struct ieee80211req_chaninfo chans;
1014138593Ssam	struct ieee80211req_chaninfo achans;
1015138593Ssam	const struct ieee80211_channel *c;
1016138593Ssam	int i, half;
1017138593Ssam
1018138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
1019138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
1020138593Ssam	ireq.i_type = IEEE80211_IOC_CHANINFO;
1021138593Ssam	ireq.i_data = &chans;
1022138593Ssam	ireq.i_len = sizeof(chans);
1023138593Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
1024138593Ssam		errx(1, "unable to get channel information");
1025138593Ssam	if (!allchans) {
1026138593Ssam		struct ieee80211req_chanlist active;
1027138593Ssam
1028138593Ssam		ireq.i_type = IEEE80211_IOC_CHANLIST;
1029138593Ssam		ireq.i_data = &active;
1030138593Ssam		ireq.i_len = sizeof(active);
1031138593Ssam		if (ioctl(s, SIOCG80211, &ireq) < 0)
1032138593Ssam			errx(1, "unable to get active channel list");
1033138593Ssam		memset(&achans, 0, sizeof(achans));
1034138593Ssam		for (i = 0; i < chans.ic_nchans; i++) {
1035138593Ssam			c = &chans.ic_chans[i];
1036138593Ssam			if (isset(active.ic_channels, ieee80211_mhz2ieee(c->ic_freq)) || allchans)
1037138593Ssam				achans.ic_chans[achans.ic_nchans++] = *c;
1038138593Ssam		}
1039138593Ssam	} else
1040138593Ssam		achans = chans;
1041138593Ssam	half = achans.ic_nchans / 2;
1042138593Ssam	if (achans.ic_nchans % 2)
1043138593Ssam		half++;
1044138593Ssam	for (i = 0; i < achans.ic_nchans / 2; i++) {
1045138593Ssam		print_chaninfo(&achans.ic_chans[i]);
1046138593Ssam		print_chaninfo(&achans.ic_chans[half+i]);
1047138593Ssam		printf("\n");
1048138593Ssam	}
1049138593Ssam	if (achans.ic_nchans % 2) {
1050138593Ssam		print_chaninfo(&achans.ic_chans[i]);
1051138593Ssam		printf("\n");
1052138593Ssam	}
1053138593Ssam}
1054138593Ssam
1055138593Ssamstatic void
1056138593Ssamlist_keys(int s)
1057138593Ssam{
1058138593Ssam}
1059138593Ssam
1060138593Ssam#define	IEEE80211_C_BITS \
1061138593Ssam"\020\1WEP\2TKIP\3AES\4AES_CCM\6CKIP\11IBSS\12PMGT\13HOSTAP\14AHDEMO" \
1062138593Ssam"\15SWRETRY\16TXPMGT\17SHSLOT\20SHPREAMBLE\21MONITOR\22TKIPMIC\30WPA1" \
1063138593Ssam"\31WPA2\32BURST\33WME"
1064138593Ssam
1065138593Ssamstatic void
1066138593Ssamlist_capabilities(int s)
1067138593Ssam{
1068138593Ssam	struct ieee80211req ireq;
1069138593Ssam	u_int32_t caps;
1070138593Ssam
1071138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
1072138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
1073138593Ssam	ireq.i_type = IEEE80211_IOC_DRIVER_CAPS;
1074138593Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
1075138593Ssam		errx(1, "unable to get driver capabilities");
1076138593Ssam	caps = (((u_int16_t) ireq.i_val) << 16) | ((u_int16_t) ireq.i_len);
1077138593Ssam	printb(name, caps, IEEE80211_C_BITS);
1078138593Ssam	putchar('\n');
1079138593Ssam}
1080138593Ssam
1081138593Ssamstatic void
1082138593Ssamlist_wme(int s)
1083138593Ssam{
1084138593Ssam	static const char *acnames[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" };
1085138593Ssam	struct ieee80211req ireq;
1086138593Ssam	int ac;
1087138593Ssam
1088138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
1089138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
1090138593Ssam	ireq.i_len = 0;
1091138593Ssam	for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++) {
1092138593Ssamagain:
1093138593Ssam		if (ireq.i_len & IEEE80211_WMEPARAM_BSS)
1094138593Ssam			printf("\t%s", "     ");
1095138593Ssam		else
1096138593Ssam			printf("\t%s", acnames[ac]);
1097138593Ssam
1098138593Ssam		ireq.i_len = (ireq.i_len & IEEE80211_WMEPARAM_BSS) | ac;
1099138593Ssam
1100138593Ssam		/* show WME BSS parameters */
1101138593Ssam		ireq.i_type = IEEE80211_IOC_WME_CWMIN;
1102138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1)
1103138593Ssam			printf(" cwmin %2u", ireq.i_val);
1104138593Ssam		ireq.i_type = IEEE80211_IOC_WME_CWMAX;
1105138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1)
1106138593Ssam			printf(" cwmax %2u", ireq.i_val);
1107138593Ssam		ireq.i_type = IEEE80211_IOC_WME_AIFS;
1108138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1)
1109138593Ssam			printf(" aifs %2u", ireq.i_val);
1110138593Ssam		ireq.i_type = IEEE80211_IOC_WME_TXOPLIMIT;
1111138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1)
1112138593Ssam			printf(" txopLimit %3u", ireq.i_val);
1113138593Ssam		ireq.i_type = IEEE80211_IOC_WME_ACM;
1114138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1115138593Ssam			if (ireq.i_val)
1116138593Ssam				printf(" acm");
1117138593Ssam			else if (verbose)
1118138593Ssam				printf(" -acm");
1119138593Ssam		}
1120138593Ssam		/* !BSS only */
1121138593Ssam		if ((ireq.i_len & IEEE80211_WMEPARAM_BSS) == 0) {
1122138593Ssam			ireq.i_type = IEEE80211_IOC_WME_ACKPOLICY;
1123138593Ssam			if (ioctl(s, SIOCG80211, &ireq) != -1) {
1124138593Ssam				if (!ireq.i_val)
1125138593Ssam					printf(" -ack");
1126138593Ssam				else if (verbose)
1127138593Ssam					printf(" ack");
1128138593Ssam			}
1129138593Ssam		}
1130138593Ssam		printf("\n");
1131138593Ssam		if ((ireq.i_len & IEEE80211_WMEPARAM_BSS) == 0) {
1132138593Ssam			ireq.i_len |= IEEE80211_WMEPARAM_BSS;
1133138593Ssam			goto again;
1134138593Ssam		} else
1135138593Ssam			ireq.i_len &= ~IEEE80211_WMEPARAM_BSS;
1136138593Ssam	}
1137138593Ssam}
1138138593Ssam
1139149029Ssamstatic void
1140149029Ssamlist_mac(int s)
1141149029Ssam{
1142149029Ssam	struct ieee80211req ireq;
1143149029Ssam	struct ieee80211req_maclist *acllist;
1144149029Ssam	int i, nacls, policy;
1145149029Ssam	char c;
1146149029Ssam
1147149029Ssam	(void) memset(&ireq, 0, sizeof(ireq));
1148149029Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); /* XXX ?? */
1149149029Ssam	ireq.i_type = IEEE80211_IOC_MACCMD;
1150149029Ssam	ireq.i_val = IEEE80211_MACCMD_POLICY;
1151149029Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0) {
1152149029Ssam		if (errno == EINVAL) {
1153149029Ssam			printf("No acl policy loaded\n");
1154149029Ssam			return;
1155149029Ssam		}
1156149029Ssam		err(1, "unable to get mac policy");
1157149029Ssam	}
1158149029Ssam	policy = ireq.i_val;
1159149029Ssam
1160149029Ssam	ireq.i_val = IEEE80211_MACCMD_LIST;
1161149029Ssam	ireq.i_len = 0;
1162149029Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
1163149029Ssam		err(1, "unable to get mac acl list size");
1164149029Ssam	if (ireq.i_len == 0)		/* NB: no acls */
1165149029Ssam		return;
1166149029Ssam
1167149029Ssam	ireq.i_data = malloc(ireq.i_len);
1168149029Ssam	if (ireq.i_data == NULL)
1169149029Ssam		err(1, "out of memory for acl list");
1170149029Ssam
1171149029Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
1172149029Ssam		err(1, "unable to get mac acl list");
1173149029Ssam	if (policy == IEEE80211_MACCMD_POLICY_OPEN) {
1174149029Ssam		if (verbose)
1175149029Ssam			printf("policy: open\n");
1176149029Ssam		c = '*';
1177149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_ALLOW) {
1178149029Ssam		if (verbose)
1179149029Ssam			printf("policy: allow\n");
1180149029Ssam		c = '+';
1181149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_DENY) {
1182149029Ssam		if (verbose)
1183149029Ssam			printf("policy: deny\n");
1184149029Ssam		c = '-';
1185149029Ssam	} else {
1186149029Ssam		printf("policy: unknown (%u)\n", policy);
1187149029Ssam		c = '?';
1188149029Ssam	}
1189149029Ssam	nacls = ireq.i_len / sizeof(*acllist);
1190149029Ssam	acllist = (struct ieee80211req_maclist *) ireq.i_data;
1191149029Ssam	for (i = 0; i < nacls; i++)
1192149029Ssam		printf("%c%s\n", c, ether_ntoa(
1193149029Ssam			(const struct ether_addr *) acllist[i].ml_macaddr));
1194149029Ssam}
1195149029Ssam
1196138593Ssamstatic
1197138593SsamDECL_CMD_FUNC(set80211list, arg, d)
1198138593Ssam{
1199138593Ssam#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
1200138593Ssam
1201138593Ssam	if (iseq(arg, "sta"))
1202138593Ssam		list_stations(s);
1203138593Ssam	else if (iseq(arg, "scan") || iseq(arg, "ap"))
1204138593Ssam		list_scan(s);
1205138593Ssam	else if (iseq(arg, "chan") || iseq(arg, "freq"))
1206138593Ssam		list_channels(s, 1);
1207138593Ssam	else if (iseq(arg, "active"))
1208138593Ssam		list_channels(s, 0);
1209138593Ssam	else if (iseq(arg, "keys"))
1210138593Ssam		list_keys(s);
1211138593Ssam	else if (iseq(arg, "caps"))
1212138593Ssam		list_capabilities(s);
1213138593Ssam	else if (iseq(arg, "wme"))
1214138593Ssam		list_wme(s);
1215149029Ssam	else if (iseq(arg, "mac"))
1216149029Ssam		list_mac(s);
1217138593Ssam	else
1218138593Ssam		errx(1, "Don't know how to list %s for %s", arg, name);
1219138593Ssam#undef iseq
1220138593Ssam}
1221138593Ssam
1222138593Ssamstatic enum ieee80211_opmode
1223138593Ssamget80211opmode(int s)
1224138593Ssam{
1225138593Ssam	struct ifmediareq ifmr;
1226138593Ssam
1227138593Ssam	(void) memset(&ifmr, 0, sizeof(ifmr));
1228138593Ssam	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
1229138593Ssam
1230138593Ssam	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
1231138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_ADHOC)
1232138593Ssam			return IEEE80211_M_IBSS;	/* XXX ahdemo */
1233138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
1234138593Ssam			return IEEE80211_M_HOSTAP;
1235138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
1236138593Ssam			return IEEE80211_M_MONITOR;
1237138593Ssam	}
1238138593Ssam	return IEEE80211_M_STA;
1239138593Ssam}
1240138593Ssam
1241138593Ssamstatic const struct ieee80211_channel *
1242138593Ssamgetchaninfo(int s, int chan)
1243138593Ssam{
1244138593Ssam	struct ieee80211req ireq;
1245138593Ssam	static struct ieee80211req_chaninfo chans;
1246138593Ssam	static struct ieee80211_channel undef;
1247138593Ssam	const struct ieee80211_channel *c;
1248138593Ssam	int i, freq;
1249138593Ssam
1250138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
1251138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
1252138593Ssam	ireq.i_type = IEEE80211_IOC_CHANINFO;
1253138593Ssam	ireq.i_data = &chans;
1254138593Ssam	ireq.i_len = sizeof(chans);
1255138593Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
1256138593Ssam		errx(1, "unable to get channel information");
1257138593Ssam	freq = ieee80211_ieee2mhz(chan);
1258138593Ssam	for (i = 0; i < chans.ic_nchans; i++) {
1259138593Ssam		c = &chans.ic_chans[i];
1260138593Ssam		if (c->ic_freq == freq)
1261138593Ssam			return c;
1262138593Ssam	}
1263138593Ssam	return &undef;
1264138593Ssam}
1265138593Ssam
1266138593Ssam#if 0
1267138593Ssamstatic void
1268138593Ssamprintcipher(int s, struct ieee80211req *ireq, int keylenop)
1269138593Ssam{
1270138593Ssam	switch (ireq->i_val) {
1271138593Ssam	case IEEE80211_CIPHER_WEP:
1272138593Ssam		ireq->i_type = keylenop;
1273138593Ssam		if (ioctl(s, SIOCG80211, ireq) != -1)
1274138593Ssam			printf("WEP-%s",
1275138593Ssam			    ireq->i_len <= 5 ? "40" :
1276138593Ssam			    ireq->i_len <= 13 ? "104" : "128");
1277138593Ssam		else
1278138593Ssam			printf("WEP");
1279138593Ssam		break;
1280138593Ssam	case IEEE80211_CIPHER_TKIP:
1281138593Ssam		printf("TKIP");
1282138593Ssam		break;
1283138593Ssam	case IEEE80211_CIPHER_AES_OCB:
1284138593Ssam		printf("AES-OCB");
1285138593Ssam		break;
1286138593Ssam	case IEEE80211_CIPHER_AES_CCM:
1287138593Ssam		printf("AES-CCM");
1288138593Ssam		break;
1289138593Ssam	case IEEE80211_CIPHER_CKIP:
1290138593Ssam		printf("CKIP");
1291138593Ssam		break;
1292138593Ssam	case IEEE80211_CIPHER_NONE:
1293138593Ssam		printf("NONE");
1294138593Ssam		break;
1295138593Ssam	default:
1296138593Ssam		printf("UNKNOWN (0x%x)", ireq->i_val);
1297138593Ssam		break;
1298138593Ssam	}
1299138593Ssam}
1300138593Ssam#endif
1301138593Ssam
1302138593Ssam#define	MAXCOL	78
1303138593Ssamint	col;
1304138593Ssamchar	spacer;
1305138593Ssam
1306138593Ssam#define	LINE_BREAK() do {			\
1307138593Ssam	if (spacer != '\t') {			\
1308138593Ssam		printf("\n");			\
1309138593Ssam		spacer = '\t';			\
1310138593Ssam	}					\
1311138593Ssam	col = 8;	/* 8-col tab */		\
1312138593Ssam} while (0)
1313138593Ssam#define	LINE_CHECK(fmt, ...) do {		\
1314138593Ssam	col += sizeof(fmt)-2;			\
1315138593Ssam	if (col > MAXCOL) {			\
1316138593Ssam		LINE_BREAK();			\
1317138593Ssam		col += sizeof(fmt)-2;		\
1318138593Ssam	}					\
1319138593Ssam	printf(fmt, __VA_ARGS__);		\
1320138593Ssam	spacer = ' ';				\
1321138593Ssam} while (0)
1322138593Ssam
1323138593Ssamstatic void
1324138593Ssamprintkey(const struct ieee80211req_key *ik)
1325138593Ssam{
1326138593Ssam	static const uint8_t zerodata[IEEE80211_KEYBUF_SIZE];
1327138593Ssam	int keylen = ik->ik_keylen;
1328138593Ssam	int printcontents;
1329138593Ssam
1330148001Srwatson	printcontents = printkeys &&
1331138593Ssam		(memcmp(ik->ik_keydata, zerodata, keylen) != 0 || verbose);
1332138593Ssam	if (printcontents)
1333138593Ssam		LINE_BREAK();
1334138593Ssam	switch (ik->ik_type) {
1335138593Ssam	case IEEE80211_CIPHER_WEP:
1336138593Ssam		/* compatibility */
1337138593Ssam		LINE_CHECK("%cwepkey %u:%s", spacer, ik->ik_keyix+1,
1338138593Ssam		    keylen <= 5 ? "40-bit" :
1339138593Ssam		    keylen <= 13 ? "104-bit" : "128-bit");
1340138593Ssam		break;
1341138593Ssam	case IEEE80211_CIPHER_TKIP:
1342138593Ssam		if (keylen > 128/8)
1343138593Ssam			keylen -= 128/8;	/* ignore MIC for now */
1344138593Ssam		LINE_CHECK("%cTKIP %u:%u-bit",
1345138593Ssam			spacer, ik->ik_keyix+1, 8*keylen);
1346138593Ssam		break;
1347138593Ssam	case IEEE80211_CIPHER_AES_OCB:
1348138593Ssam		LINE_CHECK("%cAES-OCB %u:%u-bit",
1349138593Ssam			spacer, ik->ik_keyix+1, 8*keylen);
1350138593Ssam		break;
1351138593Ssam	case IEEE80211_CIPHER_AES_CCM:
1352138593Ssam		LINE_CHECK("%cAES-CCM %u:%u-bit",
1353138593Ssam			spacer, ik->ik_keyix+1, 8*keylen);
1354138593Ssam		break;
1355138593Ssam	case IEEE80211_CIPHER_CKIP:
1356138593Ssam		LINE_CHECK("%cCKIP %u:%u-bit",
1357138593Ssam			spacer, ik->ik_keyix+1, 8*keylen);
1358138593Ssam		break;
1359138593Ssam	case IEEE80211_CIPHER_NONE:
1360138593Ssam		LINE_CHECK("%cNULL %u:%u-bit",
1361138593Ssam			spacer, ik->ik_keyix+1, 8*keylen);
1362138593Ssam		break;
1363138593Ssam	default:
1364138593Ssam		LINE_CHECK("%cUNKNOWN (0x%x) %u:%u-bit", spacer,
1365138593Ssam			ik->ik_type, ik->ik_keyix+1, 8*keylen);
1366138593Ssam		break;
1367138593Ssam	}
1368138593Ssam	if (printcontents) {
1369138593Ssam		int i;
1370138593Ssam
1371138593Ssam		printf(" <");
1372138593Ssam		for (i = 0; i < keylen; i++)
1373138593Ssam			printf("%02x", ik->ik_keydata[i]);
1374138593Ssam		printf(">");
1375138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
1376138593Ssam		    (ik->ik_keyrsc != 0 || verbose))
1377146873Sjhb			printf(" rsc %ju", (uintmax_t)ik->ik_keyrsc);
1378138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
1379138593Ssam		    (ik->ik_keytsc != 0 || verbose))
1380146873Sjhb			printf(" tsc %ju", (uintmax_t)ik->ik_keytsc);
1381138593Ssam		if (ik->ik_flags != 0 && verbose) {
1382138593Ssam			const char *sep = " ";
1383138593Ssam
1384138593Ssam			if (ik->ik_flags & IEEE80211_KEY_XMIT)
1385138593Ssam				printf("%stx", sep), sep = "+";
1386138593Ssam			if (ik->ik_flags & IEEE80211_KEY_RECV)
1387138593Ssam				printf("%srx", sep), sep = "+";
1388138593Ssam			if (ik->ik_flags & IEEE80211_KEY_DEFAULT)
1389138593Ssam				printf("%sdef", sep), sep = "+";
1390138593Ssam		}
1391138593Ssam		LINE_BREAK();
1392138593Ssam	}
1393138593Ssam}
1394138593Ssam
1395138593Ssamstatic void
1396139494Ssamieee80211_status(int s)
1397138593Ssam{
1398138593Ssam	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
1399138593Ssam	enum ieee80211_opmode opmode = get80211opmode(s);
1400138593Ssam	int i, num, wpa, wme;
1401138593Ssam	struct ieee80211req ireq;
1402138593Ssam	u_int8_t data[32];
1403138593Ssam	const struct ieee80211_channel *c;
1404138593Ssam
1405138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
1406138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
140777218Sphk	ireq.i_data = &data;
140877218Sphk
1409138593Ssam	wpa = 0;		/* unknown/not set */
1410138593Ssam
141177218Sphk	ireq.i_type = IEEE80211_IOC_SSID;
141277218Sphk	ireq.i_val = -1;
141377218Sphk	if (ioctl(s, SIOCG80211, &ireq) < 0) {
1414148686Sstefanf		/* If we can't get the SSID, this isn't an 802.11 device. */
141577218Sphk		return;
141677218Sphk	}
141788748Sambrisko	num = 0;
141888748Sambrisko	ireq.i_type = IEEE80211_IOC_NUMSSIDS;
1419138593Ssam	if (ioctl(s, SIOCG80211, &ireq) >= 0)
142088748Sambrisko		num = ireq.i_val;
1421138593Ssam	printf("\tssid ");
1422138593Ssam	if (num > 1) {
1423138593Ssam		ireq.i_type = IEEE80211_IOC_SSID;
1424138593Ssam		for (ireq.i_val = 0; ireq.i_val < num; ireq.i_val++) {
1425138593Ssam			if (ioctl(s, SIOCG80211, &ireq) >= 0 && ireq.i_len > 0) {
1426138593Ssam				printf(" %d:", ireq.i_val + 1);
1427138593Ssam				print_string(data, ireq.i_len);
1428138593Ssam			}
142988748Sambrisko		}
1430138593Ssam	} else
1431138593Ssam		print_string(data, ireq.i_len);
143277218Sphk
1433138593Ssam	ireq.i_type = IEEE80211_IOC_CHANNEL;
1434138593Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
1435138593Ssam		goto end;
1436138593Ssam	c = getchaninfo(s, ireq.i_val);
1437138593Ssam	if (ireq.i_val != -1) {
1438138593Ssam		printf(" channel %d", ireq.i_val);
1439138593Ssam		if (verbose)
1440138593Ssam			printf(" (%u)", c->ic_freq);
1441138593Ssam	} else if (verbose)
1442138593Ssam		printf(" channel UNDEF");
1443138593Ssam
1444138593Ssam	ireq.i_type = IEEE80211_IOC_BSSID;
1445138593Ssam	ireq.i_len = IEEE80211_ADDR_LEN;
1446138593Ssam	if (ioctl(s, SIOCG80211, &ireq) >= 0 &&
1447138593Ssam	    memcmp(ireq.i_data, zerobssid, sizeof(zerobssid)) != 0)
1448138593Ssam		printf(" bssid %s", ether_ntoa(ireq.i_data));
1449138593Ssam
145077218Sphk	ireq.i_type = IEEE80211_IOC_STATIONNAME;
145177218Sphk	if (ioctl(s, SIOCG80211, &ireq) != -1) {
1452138593Ssam		printf("\n\tstationname ");
145377218Sphk		print_string(data, ireq.i_len);
145477218Sphk	}
145577218Sphk
1456138593Ssam	spacer = ' ';		/* force first break */
1457138593Ssam	LINE_BREAK();
145877218Sphk
145977218Sphk	ireq.i_type = IEEE80211_IOC_AUTHMODE;
146077218Sphk	if (ioctl(s, SIOCG80211, &ireq) != -1) {
146177218Sphk		switch (ireq.i_val) {
146277218Sphk			case IEEE80211_AUTH_NONE:
1463138593Ssam				LINE_CHECK("%cauthmode NONE", spacer);
146477218Sphk				break;
146577218Sphk			case IEEE80211_AUTH_OPEN:
1466138593Ssam				LINE_CHECK("%cauthmode OPEN", spacer);
146777218Sphk				break;
146877218Sphk			case IEEE80211_AUTH_SHARED:
1469138593Ssam				LINE_CHECK("%cauthmode SHARED", spacer);
147077218Sphk				break;
1471138593Ssam			case IEEE80211_AUTH_8021X:
1472138593Ssam				LINE_CHECK("%cauthmode 802.1x", spacer);
147377218Sphk				break;
1474138593Ssam			case IEEE80211_AUTH_WPA:
1475138593Ssam				ireq.i_type = IEEE80211_IOC_WPA;
1476138593Ssam				if (ioctl(s, SIOCG80211, &ireq) != -1)
1477138593Ssam					wpa = ireq.i_val;
1478138593Ssam				if (!wpa)
1479138593Ssam					wpa = 1;	/* default to WPA1 */
1480138593Ssam				switch (wpa) {
1481138593Ssam				case 2:
1482138593Ssam					LINE_CHECK("%cauthmode WPA2/802.11i",
1483138593Ssam						spacer);
1484138593Ssam					break;
1485138593Ssam				case 3:
1486138593Ssam					LINE_CHECK("%cauthmode WPA1+WPA2/802.11i",
1487138593Ssam						spacer);
1488138593Ssam					break;
1489138593Ssam				default:
1490138593Ssam					LINE_CHECK("%cauthmode WPA", spacer);
1491138593Ssam					break;
1492138593Ssam				}
149377218Sphk				break;
1494138593Ssam			case IEEE80211_AUTH_AUTO:
1495138593Ssam				LINE_CHECK("%cauthmode AUTO", spacer);
149677218Sphk				break;
1497127649Ssam			default:
1498138593Ssam				LINE_CHECK("%cauthmode UNKNOWN (0x%x)",
1499138593Ssam					spacer, ireq.i_val);
1500127649Ssam				break;
1501127649Ssam		}
1502127649Ssam	}
1503127649Ssam
150477218Sphk	ireq.i_type = IEEE80211_IOC_WEP;
150580315Sbrooks	if (ioctl(s, SIOCG80211, &ireq) != -1 &&
150680315Sbrooks	    ireq.i_val != IEEE80211_WEP_NOSUP) {
1507138718Ssam		int firstkey, wepmode;
1508138593Ssam
1509138718Ssam		wepmode = ireq.i_val;
1510138718Ssam		switch (wepmode) {
151177218Sphk			case IEEE80211_WEP_OFF:
1512138593Ssam				LINE_CHECK("%cprivacy OFF", spacer);
151377218Sphk				break;
151477218Sphk			case IEEE80211_WEP_ON:
1515138593Ssam				LINE_CHECK("%cprivacy ON", spacer);
151677218Sphk				break;
151777218Sphk			case IEEE80211_WEP_MIXED:
1518138593Ssam				LINE_CHECK("%cprivacy MIXED", spacer);
151977218Sphk				break;
152077218Sphk			default:
1521138593Ssam				LINE_CHECK("%cprivacy UNKNOWN (0x%x)",
1522138718Ssam					spacer, wepmode);
152377218Sphk				break;
152477218Sphk		}
152577218Sphk
152677218Sphk		/*
152777218Sphk		 * If we get here then we've got WEP support so we need
152877218Sphk		 * to print WEP status.
152991454Sbrooks		 */
153077218Sphk
153177218Sphk		ireq.i_type = IEEE80211_IOC_WEPTXKEY;
153277218Sphk		if (ioctl(s, SIOCG80211, &ireq) < 0) {
153377218Sphk			warn("WEP support, but no tx key!");
153477218Sphk			goto end;
153577218Sphk		}
1536138593Ssam		if (ireq.i_val != -1)
1537138593Ssam			LINE_CHECK("%cdeftxkey %d", spacer, ireq.i_val+1);
1538138718Ssam		else if (wepmode != IEEE80211_WEP_OFF || verbose)
1539138593Ssam			LINE_CHECK("%cdeftxkey UNDEF", spacer);
154077218Sphk
154177218Sphk		ireq.i_type = IEEE80211_IOC_NUMWEPKEYS;
154277218Sphk		if (ioctl(s, SIOCG80211, &ireq) < 0) {
154377218Sphk			warn("WEP support, but no NUMWEPKEYS support!");
154477218Sphk			goto end;
154577218Sphk		}
154677218Sphk		num = ireq.i_val;
154777218Sphk
1548138593Ssam		firstkey = 1;
1549138593Ssam		for (i = 0; i < num; i++) {
1550138593Ssam			struct ieee80211req_key ik;
155177218Sphk
1552138593Ssam			memset(&ik, 0, sizeof(ik));
1553138593Ssam			ik.ik_keyix = i;
1554138593Ssam			ireq.i_type = IEEE80211_IOC_WPAKEY;
1555138593Ssam			ireq.i_data = &ik;
1556138593Ssam			ireq.i_len = sizeof(ik);
155777218Sphk			if (ioctl(s, SIOCG80211, &ireq) < 0) {
155877218Sphk				warn("WEP support, but can get keys!");
155977218Sphk				goto end;
156077218Sphk			}
1561138593Ssam			if (ik.ik_keylen != 0) {
1562138593Ssam				if (verbose)
1563138593Ssam					LINE_BREAK();
1564138593Ssam				printkey(&ik);
1565138593Ssam				firstkey = 0;
1566138593Ssam			}
1567138593Ssam		}
1568138593Ssam	}
1569138593Ssam
1570138593Ssam	ireq.i_type = IEEE80211_IOC_POWERSAVE;
1571138593Ssam	if (ioctl(s, SIOCG80211, &ireq) != -1 &&
1572138593Ssam	    ireq.i_val != IEEE80211_POWERSAVE_NOSUP ) {
1573138593Ssam		if (ireq.i_val != IEEE80211_POWERSAVE_OFF || verbose) {
1574138593Ssam			switch (ireq.i_val) {
1575138593Ssam				case IEEE80211_POWERSAVE_OFF:
1576138593Ssam					LINE_CHECK("%cpowersavemode OFF",
1577138593Ssam						spacer);
1578138593Ssam					break;
1579138593Ssam				case IEEE80211_POWERSAVE_CAM:
1580138593Ssam					LINE_CHECK("%cpowersavemode CAM",
1581138593Ssam						spacer);
1582138593Ssam					break;
1583138593Ssam				case IEEE80211_POWERSAVE_PSP:
1584138593Ssam					LINE_CHECK("%cpowersavemode PSP",
1585138593Ssam						spacer);
1586138593Ssam					break;
1587138593Ssam				case IEEE80211_POWERSAVE_PSP_CAM:
1588138593Ssam					LINE_CHECK("%cpowersavemode PSP-CAM",
1589138593Ssam						spacer);
1590138593Ssam					break;
1591138593Ssam			}
1592138593Ssam			ireq.i_type = IEEE80211_IOC_POWERSAVESLEEP;
1593138593Ssam			if (ioctl(s, SIOCG80211, &ireq) != -1)
1594138593Ssam				LINE_CHECK("%cpowersavesleep %d",
1595138593Ssam					spacer, ireq.i_val);
1596138593Ssam		}
1597138593Ssam	}
1598138593Ssam
1599138593Ssam	ireq.i_type = IEEE80211_IOC_TXPOWMAX;
1600138593Ssam	if (ioctl(s, SIOCG80211, &ireq) != -1)
1601138593Ssam		LINE_CHECK("%ctxpowmax %d", spacer, ireq.i_val);
1602138593Ssam
1603138593Ssam	if (verbose) {
1604138593Ssam		ireq.i_type = IEEE80211_IOC_TXPOWER;
1605138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1)
1606138593Ssam			LINE_CHECK("%ctxpower %d", spacer, ireq.i_val);
1607138593Ssam	}
1608138593Ssam
1609138593Ssam	ireq.i_type = IEEE80211_IOC_RTSTHRESHOLD;
1610138593Ssam	if (ioctl(s, SIOCG80211, &ireq) != -1) {
1611138593Ssam		if (ireq.i_val != IEEE80211_RTS_MAX || verbose)
1612138593Ssam			LINE_CHECK("%crtsthreshold %d", spacer, ireq.i_val);
1613138593Ssam	}
1614138593Ssam
1615148416Ssam	ireq.i_type = IEEE80211_IOC_FRAGTHRESHOLD;
1616148416Ssam	if (ioctl(s, SIOCG80211, &ireq) != -1) {
1617148416Ssam		if (ireq.i_val != IEEE80211_FRAG_MAX || verbose)
1618148416Ssam			LINE_CHECK("%cfragthreshold %d", spacer, ireq.i_val);
1619148416Ssam	}
1620148416Ssam
1621138593Ssam	if (IEEE80211_IS_CHAN_G(c) || IEEE80211_IS_CHAN_PUREG(c) || verbose) {
1622147795Ssam		ireq.i_type = IEEE80211_IOC_PUREG;
1623147795Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1624147795Ssam			if (ireq.i_val)
1625147795Ssam				LINE_CHECK("%cpureg", spacer);
1626147795Ssam			else if (verbose)
1627147795Ssam				LINE_CHECK("%c-pureg", spacer);
1628147795Ssam		}
1629138593Ssam		ireq.i_type = IEEE80211_IOC_PROTMODE;
1630138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1631138593Ssam			switch (ireq.i_val) {
1632138593Ssam				case IEEE80211_PROTMODE_OFF:
1633138593Ssam					LINE_CHECK("%cprotmode OFF", spacer);
1634138593Ssam					break;
1635138593Ssam				case IEEE80211_PROTMODE_CTS:
1636138593Ssam					LINE_CHECK("%cprotmode CTS", spacer);
1637138593Ssam					break;
1638138593Ssam				case IEEE80211_PROTMODE_RTSCTS:
1639138593Ssam					LINE_CHECK("%cprotmode RTSCTS", spacer);
1640138593Ssam					break;
1641138593Ssam				default:
1642138593Ssam					LINE_CHECK("%cprotmode UNKNOWN (0x%x)",
1643138593Ssam						spacer, ireq.i_val);
1644138593Ssam					break;
1645138593Ssam			}
1646138593Ssam		}
1647138593Ssam	}
1648138593Ssam
1649138593Ssam	ireq.i_type = IEEE80211_IOC_WME;
1650138593Ssam	if (ioctl(s, SIOCG80211, &ireq) != -1) {
1651138593Ssam		wme = ireq.i_val;
1652138593Ssam		if (wme)
1653138593Ssam			LINE_CHECK("%cwme", spacer);
1654138593Ssam		else if (verbose)
1655138593Ssam			LINE_CHECK("%c-wme", spacer);
1656138593Ssam	} else
1657138593Ssam		wme = 0;
1658138593Ssam
1659138593Ssam	if (opmode == IEEE80211_M_HOSTAP) {
1660138593Ssam		ireq.i_type = IEEE80211_IOC_HIDESSID;
1661138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1662138593Ssam			if (ireq.i_val)
1663138593Ssam				LINE_CHECK("%cssid HIDE", spacer);
1664138593Ssam			else if (verbose)
1665138593Ssam				LINE_CHECK("%cssid SHOW", spacer);
1666138593Ssam		}
1667138593Ssam
1668138593Ssam		ireq.i_type = IEEE80211_IOC_APBRIDGE;
1669138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1670138593Ssam			if (!ireq.i_val)
1671138593Ssam				LINE_CHECK("%c-apbridge", spacer);
1672138593Ssam			else if (verbose)
1673138593Ssam				LINE_CHECK("%capbridge", spacer);
1674138593Ssam		}
1675138593Ssam
1676138593Ssam		ireq.i_type = IEEE80211_IOC_DTIM_PERIOD;
1677138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1)
1678138593Ssam			LINE_CHECK("%cdtimperiod %u", spacer, ireq.i_val);
1679138593Ssam	} else {
1680138593Ssam		ireq.i_type = IEEE80211_IOC_ROAMING;
1681138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1682138593Ssam			if (ireq.i_val != IEEE80211_ROAMING_AUTO || verbose) {
1683138593Ssam				switch (ireq.i_val) {
1684138593Ssam				case IEEE80211_ROAMING_DEVICE:
1685138593Ssam					LINE_CHECK("%croaming DEVICE", spacer);
1686138593Ssam					break;
1687138593Ssam				case IEEE80211_ROAMING_AUTO:
1688138593Ssam					LINE_CHECK("%croaming AUTO", spacer);
1689138593Ssam					break;
1690138593Ssam				case IEEE80211_ROAMING_MANUAL:
1691138593Ssam					LINE_CHECK("%croaming MANUAL", spacer);
1692138593Ssam					break;
1693138593Ssam				default:
1694138593Ssam					LINE_CHECK("%croaming UNKNOWN (0x%x)",
1695138593Ssam						spacer, ireq.i_val);
1696138593Ssam					break;
1697138593Ssam				}
1698138593Ssam			}
1699138593Ssam		}
1700138593Ssam	}
1701138593Ssam	ireq.i_type = IEEE80211_IOC_BEACON_INTERVAL;
1702138593Ssam	if (ioctl(s, SIOCG80211, &ireq) != -1) {
1703138593Ssam		if (ireq.i_val)
1704138593Ssam			LINE_CHECK("%cbintval %u", spacer, ireq.i_val);
1705138593Ssam		else if (verbose)
1706138593Ssam			LINE_CHECK("%cbintval %u", spacer, ireq.i_val);
1707138593Ssam	}
1708138593Ssam
1709138593Ssam	if (wme && verbose) {
1710138593Ssam		LINE_BREAK();
1711138593Ssam		list_wme(s);
1712138593Ssam	}
1713138593Ssam
1714138593Ssam	if (wpa) {
1715138593Ssam		ireq.i_type = IEEE80211_IOC_COUNTERMEASURES;
1716138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1717138593Ssam			if (ireq.i_val)
1718138593Ssam				LINE_CHECK("%ccountermeasures", spacer);
1719138593Ssam			else if (verbose)
1720138593Ssam				LINE_CHECK("%c-countermeasures", spacer);
1721138593Ssam		}
1722138593Ssam#if 0
1723138593Ssam		/* XXX not interesting with WPA done in user space */
1724138593Ssam		ireq.i_type = IEEE80211_IOC_KEYMGTALGS;
1725138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1726138593Ssam		}
1727138593Ssam
1728138593Ssam		ireq.i_type = IEEE80211_IOC_MCASTCIPHER;
1729138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1730138593Ssam			printf("%cmcastcipher ", spacer);
1731138593Ssam			printcipher(s, &ireq, IEEE80211_IOC_MCASTKEYLEN);
1732138593Ssam			spacer = ' ';
1733138593Ssam		}
1734138593Ssam
1735138593Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHER;
1736138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1737138593Ssam			printf("%cucastcipher ", spacer);
1738138593Ssam			printcipher(s, &ireq, IEEE80211_IOC_UCASTKEYLEN);
1739138593Ssam		}
1740138593Ssam
1741138593Ssam		if (wpa & 2) {
1742138593Ssam			ireq.i_type = IEEE80211_IOC_RSNCAPS;
1743138593Ssam			if (ioctl(s, SIOCG80211, &ireq) != -1) {
1744138593Ssam				printf("%cRSN caps 0x%x", spacer, ireq.i_val);
174577218Sphk				spacer = ' ';
1746138593Ssam			}
174777218Sphk		}
1748138593Ssam
1749138593Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHERS;
1750138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1751138593Ssam		}
1752138593Ssam#endif
1753138593Ssam		LINE_BREAK();
175477218Sphk	}
1755138593Ssam	LINE_BREAK();
175677218Sphk
175777218Sphkend:
175877218Sphk	return;
175977218Sphk}
176077218Sphk
176177218Sphkstatic void
176277218Sphkset80211(int s, int type, int val, int len, u_int8_t *data)
176377218Sphk{
176477218Sphk	struct ieee80211req	ireq;
176577218Sphk
176677218Sphk	(void) memset(&ireq, 0, sizeof(ireq));
176777218Sphk	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
176877218Sphk	ireq.i_type = type;
176977218Sphk	ireq.i_val = val;
177077218Sphk	ireq.i_len = len;
177177218Sphk	ireq.i_data = data;
177291454Sbrooks	if (ioctl(s, SIOCS80211, &ireq) < 0)
177377218Sphk		err(1, "SIOCS80211");
177477218Sphk}
177577218Sphk
177677218Sphkstatic const char *
177777218Sphkget_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
177877218Sphk{
177977218Sphk	int len;
178077218Sphk	int hexstr;
178177218Sphk	u_int8_t *p;
178277218Sphk
178377218Sphk	len = *lenp;
178477218Sphk	p = buf;
178577218Sphk	hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
178677218Sphk	if (hexstr)
178777218Sphk		val += 2;
178877218Sphk	for (;;) {
178977218Sphk		if (*val == '\0')
179077218Sphk			break;
179177218Sphk		if (sep != NULL && strchr(sep, *val) != NULL) {
179277218Sphk			val++;
179377218Sphk			break;
179477218Sphk		}
179577218Sphk		if (hexstr) {
1796127831Sphk			if (!isxdigit((u_char)val[0])) {
179777218Sphk				warnx("bad hexadecimal digits");
179877218Sphk				return NULL;
179977218Sphk			}
1800127831Sphk			if (!isxdigit((u_char)val[1])) {
1801127831Sphk				warnx("odd count hexadecimal digits");
1802127831Sphk				return NULL;
1803127831Sphk			}
180477218Sphk		}
1805127831Sphk		if (p >= buf + len) {
180677218Sphk			if (hexstr)
180777218Sphk				warnx("hexadecimal digits too long");
180877218Sphk			else
1809127831Sphk				warnx("string too long");
181077218Sphk			return NULL;
181177218Sphk		}
181277218Sphk		if (hexstr) {
181377218Sphk#define	tohex(x)	(isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
181477218Sphk			*p++ = (tohex((u_char)val[0]) << 4) |
181577218Sphk			    tohex((u_char)val[1]);
181677218Sphk#undef tohex
181777218Sphk			val += 2;
181877218Sphk		} else
181977218Sphk			*p++ = *val++;
182077218Sphk	}
182177218Sphk	len = p - buf;
182277218Sphk	/* The string "-" is treated as the empty string. */
182377218Sphk	if (!hexstr && len == 1 && buf[0] == '-')
182477218Sphk		len = 0;
182577218Sphk	if (len < *lenp)
182677218Sphk		memset(p, 0, *lenp - len);
182777218Sphk	*lenp = len;
182877218Sphk	return val;
182977218Sphk}
183077218Sphk
183177218Sphkstatic void
183277218Sphkprint_string(const u_int8_t *buf, int len)
183377218Sphk{
183477218Sphk	int i;
183577218Sphk	int hasspc;
183677218Sphk
183777218Sphk	i = 0;
183877218Sphk	hasspc = 0;
183991454Sbrooks	for (; i < len; i++) {
184077218Sphk		if (!isprint(buf[i]) && buf[i] != '\0')
184177218Sphk			break;
184277218Sphk		if (isspace(buf[i]))
184377218Sphk			hasspc++;
184477218Sphk	}
184577218Sphk	if (i == len) {
184677218Sphk		if (hasspc || len == 0 || buf[0] == '\0')
184777218Sphk			printf("\"%.*s\"", len, buf);
184877218Sphk		else
184977218Sphk			printf("%.*s", len, buf);
185077218Sphk	} else {
185177218Sphk		printf("0x");
185277218Sphk		for (i = 0; i < len; i++)
185377218Sphk			printf("%02x", buf[i]);
185477218Sphk	}
185577218Sphk}
185677218Sphk
1857138593Ssamstatic struct cmd ieee80211_cmds[] = {
1858138593Ssam	DEF_CMD_ARG("ssid",		set80211ssid),
1859138593Ssam	DEF_CMD_ARG("nwid",		set80211ssid),
1860138593Ssam	DEF_CMD_ARG("stationname",	set80211stationname),
1861138593Ssam	DEF_CMD_ARG("station",		set80211stationname),	/* BSD/OS */
1862138593Ssam	DEF_CMD_ARG("channel",		set80211channel),
1863138593Ssam	DEF_CMD_ARG("authmode",		set80211authmode),
1864138593Ssam	DEF_CMD_ARG("powersavemode",	set80211powersavemode),
1865138593Ssam	DEF_CMD("powersave",	1,	set80211powersave),
1866138593Ssam	DEF_CMD("-powersave",	0,	set80211powersave),
1867138593Ssam	DEF_CMD_ARG("powersavesleep", 	set80211powersavesleep),
1868138593Ssam	DEF_CMD_ARG("wepmode",		set80211wepmode),
1869138593Ssam	DEF_CMD("wep",		1,	set80211wep),
1870138593Ssam	DEF_CMD("-wep",		0,	set80211wep),
1871139493Ssam	DEF_CMD_ARG("deftxkey",		set80211weptxkey),
1872138593Ssam	DEF_CMD_ARG("weptxkey",		set80211weptxkey),
1873138593Ssam	DEF_CMD_ARG("wepkey",		set80211wepkey),
1874138593Ssam	DEF_CMD_ARG("nwkey",		set80211nwkey),		/* NetBSD */
1875138593Ssam	DEF_CMD("-nwkey",	0,	set80211wep),		/* NetBSD */
1876138593Ssam	DEF_CMD_ARG("rtsthreshold",	set80211rtsthreshold),
1877138593Ssam	DEF_CMD_ARG("protmode",		set80211protmode),
1878138593Ssam	DEF_CMD_ARG("txpower",		set80211txpower),
1879138593Ssam	DEF_CMD_ARG("roaming",		set80211roaming),
1880138593Ssam	DEF_CMD("wme",		1,	set80211wme),
1881138593Ssam	DEF_CMD("-wme",		0,	set80211wme),
1882138593Ssam	DEF_CMD("hidessid",	1,	set80211hidessid),
1883138593Ssam	DEF_CMD("-hidessid",	0,	set80211hidessid),
1884138593Ssam	DEF_CMD("apbridge",	1,	set80211apbridge),
1885138593Ssam	DEF_CMD("-apbridge",	0,	set80211apbridge),
1886138593Ssam	DEF_CMD_ARG("chanlist",		set80211chanlist),
1887138593Ssam	DEF_CMD_ARG("bssid",		set80211bssid),
1888138593Ssam	DEF_CMD_ARG("ap",		set80211bssid),
1889138593Ssam	DEF_CMD("scan",	0,		set80211scan),
1890138593Ssam	DEF_CMD_ARG("list",		set80211list),
1891138593Ssam	DEF_CMD_ARG2("cwmin",		set80211cwmin),
1892138593Ssam	DEF_CMD_ARG2("cwmax",		set80211cwmax),
1893138593Ssam	DEF_CMD_ARG2("aifs",		set80211aifs),
1894138593Ssam	DEF_CMD_ARG2("txoplimit",	set80211txoplimit),
1895148621Ssam	DEF_CMD_ARG("acm",		set80211acm),
1896148621Ssam	DEF_CMD_ARG("-acm",		set80211noacm),
1897148621Ssam	DEF_CMD_ARG("ack",		set80211ackpolicy),
1898148621Ssam	DEF_CMD_ARG("-ack",		set80211noackpolicy),
1899138593Ssam	DEF_CMD_ARG2("bss:cwmin",	set80211bsscwmin),
1900138593Ssam	DEF_CMD_ARG2("bss:cwmax",	set80211bsscwmax),
1901138593Ssam	DEF_CMD_ARG2("bss:aifs",	set80211bssaifs),
1902138593Ssam	DEF_CMD_ARG2("bss:txoplimit",	set80211bsstxoplimit),
1903138593Ssam	DEF_CMD_ARG("dtimperiod",	set80211dtimperiod),
1904138593Ssam	DEF_CMD_ARG("bintval",		set80211bintval),
1905138593Ssam	DEF_CMD("mac:open",	IEEE80211_MACCMD_POLICY_OPEN,	set80211maccmd),
1906138593Ssam	DEF_CMD("mac:allow",	IEEE80211_MACCMD_POLICY_ALLOW,	set80211maccmd),
1907138593Ssam	DEF_CMD("mac:deny",	IEEE80211_MACCMD_POLICY_DENY,	set80211maccmd),
1908138593Ssam	DEF_CMD("mac:flush",	IEEE80211_MACCMD_FLUSH,		set80211maccmd),
1909138593Ssam	DEF_CMD("mac:detach",	IEEE80211_MACCMD_DETACH,	set80211maccmd),
1910138593Ssam	DEF_CMD_ARG("mac:add",		set80211addmac),
1911138593Ssam	DEF_CMD_ARG("mac:del",		set80211delmac),
1912138593Ssam	DEF_CMD_ARG("mac:kick",		set80211kickmac),
1913147795Ssam	DEF_CMD("pureg",	1,	set80211pureg),
1914147795Ssam	DEF_CMD("-pureg",	0,	set80211pureg),
1915148416Ssam	DEF_CMD_ARG("fragthreshold",	set80211fragthreshold),
1916138593Ssam};
1917138593Ssamstatic struct afswtch af_ieee80211 = {
1918138593Ssam	.af_name	= "af_ieee80211",
1919138593Ssam	.af_af		= AF_UNSPEC,
1920139494Ssam	.af_other_status = ieee80211_status,
1921138593Ssam};
1922138593Ssam
1923138593Ssamstatic __constructor void
1924138593Ssamieee80211_ctor(void)
1925138593Ssam{
1926138593Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
1927138593Ssam	int i;
1928138593Ssam
1929138593Ssam	for (i = 0; i < N(ieee80211_cmds);  i++)
1930138593Ssam		cmd_register(&ieee80211_cmds[i]);
1931138593Ssam	af_register(&af_ieee80211);
1932138593Ssam#undef N
1933138593Ssam}
1934