ifieee80211.c revision 153354
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 153354 2005-12-12 19:23:55Z sam $
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;
113151883Sbrooks	u_int8_t	data[IEEE80211_NWID_LEN];
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);
124151883Sbrooks	if (get_string(val, NULL, data, &len) == NULL)
125151883Sbrooks		exit(1);
12677218Sphk
12777218Sphk	set80211(s, IEEE80211_IOC_SSID, ssid, len, data);
12877218Sphk}
12977218Sphk
130138593Ssamstatic void
13177218Sphkset80211stationname(const char *val, int d, int s, const struct afswtch *rafp)
13277218Sphk{
13377218Sphk	int			len;
13477218Sphk	u_int8_t		data[33];
13577218Sphk
13677218Sphk	bzero(data, sizeof(data));
13777218Sphk	len = sizeof(data);
13877218Sphk	get_string(val, NULL, data, &len);
13977218Sphk
14077218Sphk	set80211(s, IEEE80211_IOC_STATIONNAME, 0, len, data);
14177218Sphk}
14277218Sphk
143138593Ssam/*
144138593Ssam * Convert IEEE channel number to MHz frequency.
145138593Ssam */
146138593Ssamstatic u_int
147138593Ssamieee80211_ieee2mhz(u_int chan)
148138593Ssam{
149138593Ssam	if (chan == 14)
150138593Ssam		return 2484;
151138593Ssam	if (chan < 14)			/* 0-13 */
152138593Ssam		return 2407 + chan*5;
153138593Ssam	if (chan < 27)			/* 15-26 */
154138593Ssam		return 2512 + ((chan-15)*20);
155138593Ssam	return 5000 + (chan*5);
156138593Ssam}
157138593Ssam
158138593Ssam/*
159138593Ssam * Convert MHz frequency to IEEE channel number.
160138593Ssam */
161138593Ssamstatic u_int
162138593Ssamieee80211_mhz2ieee(u_int freq)
163138593Ssam{
164138593Ssam	if (freq == 2484)
165138593Ssam		return 14;
166138593Ssam	if (freq < 2484)
167138593Ssam		return (freq - 2407) / 5;
168138593Ssam	if (freq < 5000)
169138593Ssam		return 15 + ((freq - 2512) / 20);
170138593Ssam	return (freq - 5000) / 5;
171138593Ssam}
172138593Ssam
173138593Ssamstatic void
17477218Sphkset80211channel(const char *val, int d, int s, const struct afswtch *rafp)
17577218Sphk{
176138593Ssam	if (!isanyarg(val)) {
177138593Ssam		int v = atoi(val);
178138593Ssam		if (v > 255)		/* treat as frequency */
179138593Ssam			v = ieee80211_mhz2ieee(v);
180138593Ssam		set80211(s, IEEE80211_IOC_CHANNEL, v, 0, NULL);
181138593Ssam	} else
182116957Ssam		set80211(s, IEEE80211_IOC_CHANNEL, IEEE80211_CHAN_ANY, 0, NULL);
18377218Sphk}
18477218Sphk
185138593Ssamstatic void
18677218Sphkset80211authmode(const char *val, int d, int s, const struct afswtch *rafp)
18777218Sphk{
18877218Sphk	int	mode;
18977218Sphk
19091454Sbrooks	if (strcasecmp(val, "none") == 0) {
19177218Sphk		mode = IEEE80211_AUTH_NONE;
19291454Sbrooks	} else if (strcasecmp(val, "open") == 0) {
19377218Sphk		mode = IEEE80211_AUTH_OPEN;
19491454Sbrooks	} else if (strcasecmp(val, "shared") == 0) {
19577218Sphk		mode = IEEE80211_AUTH_SHARED;
196138593Ssam	} else if (strcasecmp(val, "8021x") == 0) {
197138593Ssam		mode = IEEE80211_AUTH_8021X;
198138593Ssam	} else if (strcasecmp(val, "wpa") == 0) {
199138593Ssam		mode = IEEE80211_AUTH_WPA;
20077218Sphk	} else {
201150708Sru		errx(1, "unknown authmode");
20277218Sphk	}
20377218Sphk
20477218Sphk	set80211(s, IEEE80211_IOC_AUTHMODE, mode, 0, NULL);
20577218Sphk}
20677218Sphk
207138593Ssamstatic void
20877218Sphkset80211powersavemode(const char *val, int d, int s, const struct afswtch *rafp)
20977218Sphk{
21077218Sphk	int	mode;
21177218Sphk
21291454Sbrooks	if (strcasecmp(val, "off") == 0) {
21377218Sphk		mode = IEEE80211_POWERSAVE_OFF;
21491454Sbrooks	} else if (strcasecmp(val, "on") == 0) {
21577218Sphk		mode = IEEE80211_POWERSAVE_ON;
21691454Sbrooks	} else if (strcasecmp(val, "cam") == 0) {
21777218Sphk		mode = IEEE80211_POWERSAVE_CAM;
21891454Sbrooks	} else if (strcasecmp(val, "psp") == 0) {
21977218Sphk		mode = IEEE80211_POWERSAVE_PSP;
22091454Sbrooks	} else if (strcasecmp(val, "psp-cam") == 0) {
22177218Sphk		mode = IEEE80211_POWERSAVE_PSP_CAM;
22277218Sphk	} else {
223150708Sru		errx(1, "unknown powersavemode");
22477218Sphk	}
22577218Sphk
22677218Sphk	set80211(s, IEEE80211_IOC_POWERSAVE, mode, 0, NULL);
22777218Sphk}
22877218Sphk
229138593Ssamstatic void
23077218Sphkset80211powersave(const char *val, int d, int s, const struct afswtch *rafp)
23177218Sphk{
23277218Sphk	if (d == 0)
23377218Sphk		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_OFF,
23477218Sphk		    0, NULL);
23577218Sphk	else
23677218Sphk		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_ON,
23777218Sphk		    0, NULL);
23877218Sphk}
23977218Sphk
240138593Ssamstatic void
24177218Sphkset80211powersavesleep(const char *val, int d, int s, const struct afswtch *rafp)
24277218Sphk{
24377218Sphk	set80211(s, IEEE80211_IOC_POWERSAVESLEEP, atoi(val), 0, NULL);
24477218Sphk}
24577218Sphk
246138593Ssamstatic void
24777218Sphkset80211wepmode(const char *val, int d, int s, const struct afswtch *rafp)
24877218Sphk{
24977218Sphk	int	mode;
25077218Sphk
25191454Sbrooks	if (strcasecmp(val, "off") == 0) {
25277218Sphk		mode = IEEE80211_WEP_OFF;
25391454Sbrooks	} else if (strcasecmp(val, "on") == 0) {
25477218Sphk		mode = IEEE80211_WEP_ON;
25591454Sbrooks	} else if (strcasecmp(val, "mixed") == 0) {
25677218Sphk		mode = IEEE80211_WEP_MIXED;
25777218Sphk	} else {
258150708Sru		errx(1, "unknown wep mode");
25977218Sphk	}
26077218Sphk
26177218Sphk	set80211(s, IEEE80211_IOC_WEP, mode, 0, NULL);
26277218Sphk}
26377218Sphk
264138593Ssamstatic void
26577218Sphkset80211wep(const char *val, int d, int s, const struct afswtch *rafp)
26677218Sphk{
26777218Sphk	set80211(s, IEEE80211_IOC_WEP, d, 0, NULL);
26877218Sphk}
26977218Sphk
270139493Ssamstatic int
271139493Ssamisundefarg(const char *arg)
272139493Ssam{
273139493Ssam	return (strcmp(arg, "-") == 0 || strncasecmp(arg, "undef", 5) == 0);
274139493Ssam}
275139493Ssam
276138593Ssamstatic void
27777218Sphkset80211weptxkey(const char *val, int d, int s, const struct afswtch *rafp)
27877218Sphk{
279139493Ssam	if (isundefarg(val))
280139493Ssam		set80211(s, IEEE80211_IOC_WEPTXKEY, IEEE80211_KEYIX_NONE, 0, NULL);
281139493Ssam	else
282139493Ssam		set80211(s, IEEE80211_IOC_WEPTXKEY, atoi(val)-1, 0, NULL);
28377218Sphk}
28477218Sphk
285138593Ssamstatic void
28677218Sphkset80211wepkey(const char *val, int d, int s, const struct afswtch *rafp)
28777218Sphk{
28877218Sphk	int		key = 0;
28977218Sphk	int		len;
290120178Ssam	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
29177218Sphk
29291454Sbrooks	if (isdigit(val[0]) && val[1] == ':') {
29377218Sphk		key = atoi(val)-1;
29477218Sphk		val += 2;
29577218Sphk	}
29677218Sphk
29777218Sphk	bzero(data, sizeof(data));
29877218Sphk	len = sizeof(data);
29977218Sphk	get_string(val, NULL, data, &len);
30077218Sphk
30177218Sphk	set80211(s, IEEE80211_IOC_WEPKEY, key, len, data);
30277218Sphk}
30377218Sphk
30477218Sphk/*
305148686Sstefanf * This function is purely a NetBSD compatability interface.  The NetBSD
306148686Sstefanf * interface is too inflexible, but it's there so we'll support it since
30777218Sphk * it's not all that hard.
30877218Sphk */
309138593Ssamstatic void
31077218Sphkset80211nwkey(const char *val, int d, int s, const struct afswtch *rafp)
31177218Sphk{
31277218Sphk	int		txkey;
31377218Sphk	int		i, len;
314120178Ssam	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
31577218Sphk
31677218Sphk	set80211(s, IEEE80211_IOC_WEP, IEEE80211_WEP_ON, 0, NULL);
31777218Sphk
31891454Sbrooks	if (isdigit(val[0]) && val[1] == ':') {
31977218Sphk		txkey = val[0]-'0'-1;
32077218Sphk		val += 2;
32177218Sphk
32291454Sbrooks		for (i = 0; i < 4; i++) {
32377218Sphk			bzero(data, sizeof(data));
32477218Sphk			len = sizeof(data);
32577218Sphk			val = get_string(val, ",", data, &len);
326151827Sbrooks			if (val == NULL)
327151827Sbrooks				exit(1);
32877218Sphk
32977218Sphk			set80211(s, IEEE80211_IOC_WEPKEY, i, len, data);
33077218Sphk		}
33177218Sphk	} else {
33277218Sphk		bzero(data, sizeof(data));
33377218Sphk		len = sizeof(data);
33477218Sphk		get_string(val, NULL, data, &len);
33577218Sphk		txkey = 0;
33677218Sphk
33777218Sphk		set80211(s, IEEE80211_IOC_WEPKEY, 0, len, data);
33877218Sphk
33977218Sphk		bzero(data, sizeof(data));
34091454Sbrooks		for (i = 1; i < 4; i++)
34177218Sphk			set80211(s, IEEE80211_IOC_WEPKEY, i, 0, data);
34277218Sphk	}
34377218Sphk
34477218Sphk	set80211(s, IEEE80211_IOC_WEPTXKEY, txkey, 0, NULL);
34577218Sphk}
34677218Sphk
347138593Ssamstatic void
348127649Ssamset80211rtsthreshold(const char *val, int d, int s, const struct afswtch *rafp)
349127649Ssam{
350148416Ssam	set80211(s, IEEE80211_IOC_RTSTHRESHOLD,
351148416Ssam		isundefarg(val) ? IEEE80211_RTS_MAX : atoi(val), 0, NULL);
352127649Ssam}
353127649Ssam
354138593Ssamstatic void
355127649Ssamset80211protmode(const char *val, int d, int s, const struct afswtch *rafp)
356127649Ssam{
357127649Ssam	int	mode;
358127649Ssam
359127649Ssam	if (strcasecmp(val, "off") == 0) {
360127649Ssam		mode = IEEE80211_PROTMODE_OFF;
361127649Ssam	} else if (strcasecmp(val, "cts") == 0) {
362127649Ssam		mode = IEEE80211_PROTMODE_CTS;
363127649Ssam	} else if (strcasecmp(val, "rtscts") == 0) {
364127649Ssam		mode = IEEE80211_PROTMODE_RTSCTS;
365127649Ssam	} else {
366150708Sru		errx(1, "unknown protection mode");
367127649Ssam	}
368127649Ssam
369127649Ssam	set80211(s, IEEE80211_IOC_PROTMODE, mode, 0, NULL);
370127649Ssam}
371127649Ssam
372138593Ssamstatic void
373127649Ssamset80211txpower(const char *val, int d, int s, const struct afswtch *rafp)
374127649Ssam{
375127649Ssam	set80211(s, IEEE80211_IOC_TXPOWER, atoi(val), 0, NULL);
376127649Ssam}
377127649Ssam
378138593Ssam#define	IEEE80211_ROAMING_DEVICE	0
379138593Ssam#define	IEEE80211_ROAMING_AUTO		1
380138593Ssam#define	IEEE80211_ROAMING_MANUAL	2
381138593Ssam
382138593Ssamstatic void
383138593Ssamset80211roaming(const char *val, int d, int s, const struct afswtch *rafp)
38477218Sphk{
385138593Ssam	int mode;
38677218Sphk
387138593Ssam	if (strcasecmp(val, "device") == 0) {
388138593Ssam		mode = IEEE80211_ROAMING_DEVICE;
389138593Ssam	} else if (strcasecmp(val, "auto") == 0) {
390138593Ssam		mode = IEEE80211_ROAMING_AUTO;
391138593Ssam	} else if (strcasecmp(val, "manual") == 0) {
392138593Ssam		mode = IEEE80211_ROAMING_MANUAL;
393138593Ssam	} else {
394150708Sru		errx(1, "unknown roaming mode");
395138593Ssam	}
396138593Ssam	set80211(s, IEEE80211_IOC_ROAMING, mode, 0, NULL);
397138593Ssam}
398138593Ssam
399138593Ssamstatic void
400138593Ssamset80211wme(const char *val, int d, int s, const struct afswtch *rafp)
401138593Ssam{
402138593Ssam	set80211(s, IEEE80211_IOC_WME, d, 0, NULL);
403138593Ssam}
404138593Ssam
405138593Ssamstatic void
406138593Ssamset80211hidessid(const char *val, int d, int s, const struct afswtch *rafp)
407138593Ssam{
408138593Ssam	set80211(s, IEEE80211_IOC_HIDESSID, d, 0, NULL);
409138593Ssam}
410138593Ssam
411138593Ssamstatic void
412138593Ssamset80211apbridge(const char *val, int d, int s, const struct afswtch *rafp)
413138593Ssam{
414138593Ssam	set80211(s, IEEE80211_IOC_APBRIDGE, d, 0, NULL);
415138593Ssam}
416138593Ssam
417138593Ssamstatic void
418138593Ssamset80211chanlist(const char *val, int d, int s, const struct afswtch *rafp)
419138593Ssam{
420138593Ssam	struct ieee80211req_chanlist chanlist;
421138593Ssam#define	MAXCHAN	(sizeof(chanlist.ic_channels)*NBBY)
422138593Ssam	char *temp, *cp, *tp;
423138593Ssam
424138593Ssam	temp = malloc(strlen(val) + 1);
425138593Ssam	if (temp == NULL)
426138593Ssam		errx(1, "malloc failed");
427138593Ssam	strcpy(temp, val);
428138593Ssam	memset(&chanlist, 0, sizeof(chanlist));
429138593Ssam	cp = temp;
430138593Ssam	for (;;) {
431138593Ssam		int first, last, f;
432138593Ssam
433138593Ssam		tp = strchr(cp, ',');
434138593Ssam		if (tp != NULL)
435138593Ssam			*tp++ = '\0';
436138593Ssam		switch (sscanf(cp, "%u-%u", &first, &last)) {
437138593Ssam		case 1:
438138593Ssam			if (first > MAXCHAN)
439146873Sjhb				errx(-1, "channel %u out of range, max %zu",
440138593Ssam					first, MAXCHAN);
441138593Ssam			setbit(chanlist.ic_channels, first);
442138593Ssam			break;
443138593Ssam		case 2:
444138593Ssam			if (first > MAXCHAN)
445146873Sjhb				errx(-1, "channel %u out of range, max %zu",
446138593Ssam					first, MAXCHAN);
447138593Ssam			if (last > MAXCHAN)
448146873Sjhb				errx(-1, "channel %u out of range, max %zu",
449138593Ssam					last, MAXCHAN);
450138593Ssam			if (first > last)
451138593Ssam				errx(-1, "void channel range, %u > %u",
452138593Ssam					first, last);
453138593Ssam			for (f = first; f <= last; f++)
454138593Ssam				setbit(chanlist.ic_channels, f);
455138593Ssam			break;
456138593Ssam		}
457138593Ssam		if (tp == NULL)
458138593Ssam			break;
459138593Ssam		while (isspace(*tp))
460138593Ssam			tp++;
461138593Ssam		if (!isdigit(*tp))
462138593Ssam			break;
463138593Ssam		cp = tp;
464138593Ssam	}
465138593Ssam	set80211(s, IEEE80211_IOC_CHANLIST, 0,
466138593Ssam		sizeof(chanlist), (uint8_t *) &chanlist);
467138593Ssam#undef MAXCHAN
468138593Ssam}
469138593Ssam
470138593Ssamstatic void
471138593Ssamset80211bssid(const char *val, int d, int s, const struct afswtch *rafp)
472138593Ssam{
473138593Ssam
474138593Ssam	if (!isanyarg(val)) {
475138593Ssam		char *temp;
476138593Ssam		struct sockaddr_dl sdl;
477138593Ssam
478138593Ssam		temp = malloc(strlen(val) + 1);
479138593Ssam		if (temp == NULL)
480138593Ssam			errx(1, "malloc failed");
481138593Ssam		temp[0] = ':';
482138593Ssam		strcpy(temp + 1, val);
483138593Ssam		sdl.sdl_len = sizeof(sdl);
484138593Ssam		link_addr(temp, &sdl);
485138593Ssam		free(temp);
486138593Ssam		if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
487138593Ssam			errx(1, "malformed link-level address");
488138593Ssam		set80211(s, IEEE80211_IOC_BSSID, 0,
489138593Ssam			IEEE80211_ADDR_LEN, LLADDR(&sdl));
490138593Ssam	} else {
491138593Ssam		uint8_t zerobssid[IEEE80211_ADDR_LEN];
492138593Ssam		memset(zerobssid, 0, sizeof(zerobssid));
493138593Ssam		set80211(s, IEEE80211_IOC_BSSID, 0,
494138593Ssam			IEEE80211_ADDR_LEN, zerobssid);
495138593Ssam	}
496138593Ssam}
497138593Ssam
498138593Ssamstatic int
499138593Ssamgetac(const char *ac)
500138593Ssam{
501138593Ssam	if (strcasecmp(ac, "ac_be") == 0 || strcasecmp(ac, "be") == 0)
502138593Ssam		return WME_AC_BE;
503138593Ssam	if (strcasecmp(ac, "ac_bk") == 0 || strcasecmp(ac, "bk") == 0)
504138593Ssam		return WME_AC_BK;
505138593Ssam	if (strcasecmp(ac, "ac_vi") == 0 || strcasecmp(ac, "vi") == 0)
506138593Ssam		return WME_AC_VI;
507138593Ssam	if (strcasecmp(ac, "ac_vo") == 0 || strcasecmp(ac, "vo") == 0)
508138593Ssam		return WME_AC_VO;
509138593Ssam	errx(1, "unknown wme access class %s", ac);
510138593Ssam}
511138593Ssam
512138593Ssamstatic
513138593SsamDECL_CMD_FUNC2(set80211cwmin, ac, val)
514138593Ssam{
515138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val), getac(ac), NULL);
516138593Ssam}
517138593Ssam
518138593Ssamstatic
519138593SsamDECL_CMD_FUNC2(set80211cwmax, ac, val)
520138593Ssam{
521138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val), getac(ac), NULL);
522138593Ssam}
523138593Ssam
524138593Ssamstatic
525138593SsamDECL_CMD_FUNC2(set80211aifs, ac, val)
526138593Ssam{
527138593Ssam	set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val), getac(ac), NULL);
528138593Ssam}
529138593Ssam
530138593Ssamstatic
531138593SsamDECL_CMD_FUNC2(set80211txoplimit, ac, val)
532138593Ssam{
533138593Ssam	set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val), getac(ac), NULL);
534138593Ssam}
535138593Ssam
536138593Ssamstatic
537148621SsamDECL_CMD_FUNC(set80211acm, ac, d)
538138593Ssam{
539148621Ssam	set80211(s, IEEE80211_IOC_WME_ACM, 1, getac(ac), NULL);
540138593Ssam}
541148621Ssamstatic
542148621SsamDECL_CMD_FUNC(set80211noacm, ac, d)
543148621Ssam{
544148621Ssam	set80211(s, IEEE80211_IOC_WME_ACM, 0, getac(ac), NULL);
545148621Ssam}
546138593Ssam
547138593Ssamstatic
548148621SsamDECL_CMD_FUNC(set80211ackpolicy, ac, d)
549138593Ssam{
550148621Ssam	set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 1, getac(ac), NULL);
551138593Ssam}
552148621Ssamstatic
553148621SsamDECL_CMD_FUNC(set80211noackpolicy, ac, d)
554148621Ssam{
555148621Ssam	set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 0, getac(ac), NULL);
556148621Ssam}
557138593Ssam
558138593Ssamstatic
559138593SsamDECL_CMD_FUNC2(set80211bsscwmin, ac, val)
560138593Ssam{
561138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val),
562138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
563138593Ssam}
564138593Ssam
565138593Ssamstatic
566138593SsamDECL_CMD_FUNC2(set80211bsscwmax, ac, val)
567138593Ssam{
568138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val),
569138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
570138593Ssam}
571138593Ssam
572138593Ssamstatic
573138593SsamDECL_CMD_FUNC2(set80211bssaifs, ac, val)
574138593Ssam{
575138593Ssam	set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val),
576138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
577138593Ssam}
578138593Ssam
579138593Ssamstatic
580138593SsamDECL_CMD_FUNC2(set80211bsstxoplimit, ac, val)
581138593Ssam{
582138593Ssam	set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val),
583138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
584138593Ssam}
585138593Ssam
586138593Ssamstatic
587138593SsamDECL_CMD_FUNC(set80211dtimperiod, val, d)
588138593Ssam{
589138593Ssam	set80211(s, IEEE80211_IOC_DTIM_PERIOD, atoi(val), 0, NULL);
590138593Ssam}
591138593Ssam
592138593Ssamstatic
593138593SsamDECL_CMD_FUNC(set80211bintval, val, d)
594138593Ssam{
595138593Ssam	set80211(s, IEEE80211_IOC_BEACON_INTERVAL, atoi(val), 0, NULL);
596138593Ssam}
597138593Ssam
598138593Ssamstatic void
599138593Ssamset80211macmac(int s, int op, const char *val)
600138593Ssam{
601138593Ssam	char *temp;
602138593Ssam	struct sockaddr_dl sdl;
603138593Ssam
604138593Ssam	temp = malloc(strlen(val) + 1);
605138593Ssam	if (temp == NULL)
606138593Ssam		errx(1, "malloc failed");
607138593Ssam	temp[0] = ':';
608138593Ssam	strcpy(temp + 1, val);
609138593Ssam	sdl.sdl_len = sizeof(sdl);
610138593Ssam	link_addr(temp, &sdl);
611138593Ssam	free(temp);
612138593Ssam	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
613138593Ssam		errx(1, "malformed link-level address");
614138593Ssam	set80211(s, op, 0, IEEE80211_ADDR_LEN, LLADDR(&sdl));
615138593Ssam}
616138593Ssam
617138593Ssamstatic
618138593SsamDECL_CMD_FUNC(set80211addmac, val, d)
619138593Ssam{
620138593Ssam	set80211macmac(s, IEEE80211_IOC_ADDMAC, val);
621138593Ssam}
622138593Ssam
623138593Ssamstatic
624138593SsamDECL_CMD_FUNC(set80211delmac, val, d)
625138593Ssam{
626138593Ssam	set80211macmac(s, IEEE80211_IOC_DELMAC, val);
627138593Ssam}
628138593Ssam
629138593Ssamstatic
630149029SsamDECL_CMD_FUNC(set80211kickmac, val, d)
631149029Ssam{
632149029Ssam	char *temp;
633149029Ssam	struct sockaddr_dl sdl;
634149029Ssam	struct ieee80211req_mlme mlme;
635149029Ssam
636149029Ssam	temp = malloc(strlen(val) + 1);
637149029Ssam	if (temp == NULL)
638149029Ssam		errx(1, "malloc failed");
639149029Ssam	temp[0] = ':';
640149029Ssam	strcpy(temp + 1, val);
641149029Ssam	sdl.sdl_len = sizeof(sdl);
642149029Ssam	link_addr(temp, &sdl);
643149029Ssam	free(temp);
644149029Ssam	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
645149029Ssam		errx(1, "malformed link-level address");
646149029Ssam	memset(&mlme, 0, sizeof(mlme));
647149029Ssam	mlme.im_op = IEEE80211_MLME_DEAUTH;
648149029Ssam	mlme.im_reason = IEEE80211_REASON_AUTH_EXPIRE;
649149029Ssam	memcpy(mlme.im_macaddr, LLADDR(&sdl), IEEE80211_ADDR_LEN);
650149029Ssam	set80211(s, IEEE80211_IOC_MLME, 0, sizeof(mlme), (u_int8_t *) &mlme);
651149029Ssam}
652149029Ssam
653149029Ssamstatic
654138593SsamDECL_CMD_FUNC(set80211maccmd, val, d)
655138593Ssam{
656138593Ssam	set80211(s, IEEE80211_IOC_MACCMD, d, 0, NULL);
657138593Ssam}
658138593Ssam
659147795Ssamstatic void
660147795Ssamset80211pureg(const char *val, int d, int s, const struct afswtch *rafp)
661147795Ssam{
662147795Ssam	set80211(s, IEEE80211_IOC_PUREG, d, 0, NULL);
663147795Ssam}
664147795Ssam
665148416Ssamstatic
666153354SsamDECL_CMD_FUNC(set80211mcastrate, val, d)
667153354Ssam{
668153354Ssam	set80211(s, IEEE80211_IOC_MCAST_RATE, (int) 2*atof(val), 0, NULL);
669153354Ssam}
670153354Ssam
671153354Ssamstatic
672148416SsamDECL_CMD_FUNC(set80211fragthreshold, val, d)
673148416Ssam{
674148416Ssam	set80211(s, IEEE80211_IOC_FRAGTHRESHOLD,
675148416Ssam		isundefarg(val) ? IEEE80211_FRAG_MAX : atoi(val), 0, NULL);
676148416Ssam}
677148416Ssam
678138593Ssamstatic int
679138593Ssamgetmaxrate(uint8_t rates[15], uint8_t nrates)
680138593Ssam{
681138593Ssam	int i, maxrate = -1;
682138593Ssam
683138593Ssam	for (i = 0; i < nrates; i++) {
684138593Ssam		int rate = rates[i] & IEEE80211_RATE_VAL;
685138593Ssam		if (rate > maxrate)
686138593Ssam			maxrate = rate;
687138593Ssam	}
688138593Ssam	return maxrate / 2;
689138593Ssam}
690138593Ssam
691138593Ssamstatic const char *
692138593Ssamgetcaps(int capinfo)
693138593Ssam{
694138593Ssam	static char capstring[32];
695138593Ssam	char *cp = capstring;
696138593Ssam
697138593Ssam	if (capinfo & IEEE80211_CAPINFO_ESS)
698138593Ssam		*cp++ = 'E';
699138593Ssam	if (capinfo & IEEE80211_CAPINFO_IBSS)
700138593Ssam		*cp++ = 'I';
701138593Ssam	if (capinfo & IEEE80211_CAPINFO_CF_POLLABLE)
702138593Ssam		*cp++ = 'c';
703138593Ssam	if (capinfo & IEEE80211_CAPINFO_CF_POLLREQ)
704138593Ssam		*cp++ = 'C';
705138593Ssam	if (capinfo & IEEE80211_CAPINFO_PRIVACY)
706138593Ssam		*cp++ = 'P';
707138593Ssam	if (capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)
708138593Ssam		*cp++ = 'S';
709138593Ssam	if (capinfo & IEEE80211_CAPINFO_PBCC)
710138593Ssam		*cp++ = 'B';
711138593Ssam	if (capinfo & IEEE80211_CAPINFO_CHNL_AGILITY)
712138593Ssam		*cp++ = 'A';
713138593Ssam	if (capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
714138593Ssam		*cp++ = 's';
715138593Ssam	if (capinfo & IEEE80211_CAPINFO_RSN)
716138593Ssam		*cp++ = 'R';
717138593Ssam	if (capinfo & IEEE80211_CAPINFO_DSSSOFDM)
718138593Ssam		*cp++ = 'D';
719138593Ssam	*cp = '\0';
720138593Ssam	return capstring;
721138593Ssam}
722138593Ssam
723138593Ssamstatic void
724138593Ssamprintie(const char* tag, const uint8_t *ie, size_t ielen, int maxlen)
725138593Ssam{
726138593Ssam	printf("%s", tag);
727138593Ssam	if (verbose) {
728138593Ssam		maxlen -= strlen(tag)+2;
729138593Ssam		if (2*ielen > maxlen)
730138593Ssam			maxlen--;
731138593Ssam		printf("<");
732138593Ssam		for (; ielen > 0; ie++, ielen--) {
733138593Ssam			if (maxlen-- <= 0)
734138593Ssam				break;
735138593Ssam			printf("%02x", *ie);
736138593Ssam		}
737138593Ssam		if (ielen != 0)
738138593Ssam			printf("-");
739138593Ssam		printf(">");
740138593Ssam	}
741138593Ssam}
742138593Ssam
743138593Ssam/*
744138593Ssam * Copy the ssid string contents into buf, truncating to fit.  If the
745138593Ssam * ssid is entirely printable then just copy intact.  Otherwise convert
746138593Ssam * to hexadecimal.  If the result is truncated then replace the last
747138593Ssam * three characters with "...".
748138593Ssam */
749146873Sjhbstatic int
750138593Ssamcopy_essid(char buf[], size_t bufsize, const u_int8_t *essid, size_t essid_len)
751138593Ssam{
752138593Ssam	const u_int8_t *p;
753138593Ssam	size_t maxlen;
754138593Ssam	int i;
755138593Ssam
756138593Ssam	if (essid_len > bufsize)
757138593Ssam		maxlen = bufsize;
758138593Ssam	else
759138593Ssam		maxlen = essid_len;
760138593Ssam	/* determine printable or not */
761138593Ssam	for (i = 0, p = essid; i < maxlen; i++, p++) {
762138593Ssam		if (*p < ' ' || *p > 0x7e)
763138593Ssam			break;
764138593Ssam	}
765138593Ssam	if (i != maxlen) {		/* not printable, print as hex */
766138593Ssam		if (bufsize < 3)
767138593Ssam			return 0;
768138593Ssam		strlcpy(buf, "0x", bufsize);
769138593Ssam		bufsize -= 2;
770138593Ssam		p = essid;
771138593Ssam		for (i = 0; i < maxlen && bufsize >= 2; i++) {
772147489Savatar			sprintf(&buf[2+2*i], "%02x", p[i]);
773138593Ssam			bufsize -= 2;
774138593Ssam		}
775147489Savatar		if (i != essid_len)
776147489Savatar			memcpy(&buf[2+2*i-3], "...", 3);
777138593Ssam	} else {			/* printable, truncate as needed */
778138593Ssam		memcpy(buf, essid, maxlen);
779147489Savatar		if (maxlen != essid_len)
780147489Savatar			memcpy(&buf[maxlen-3], "...", 3);
781138593Ssam	}
782138593Ssam	return maxlen;
783138593Ssam}
784138593Ssam
785148686Sstefanf/* unaligned little endian access */
786138593Ssam#define LE_READ_4(p)					\
787138593Ssam	((u_int32_t)					\
788138593Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
789138593Ssam	  (((const u_int8_t *)(p))[1] <<  8) |		\
790138593Ssam	  (((const u_int8_t *)(p))[2] << 16) |		\
791138593Ssam	  (((const u_int8_t *)(p))[3] << 24)))
792138593Ssam
793138593Ssamstatic int __inline
794138593Ssamiswpaoui(const u_int8_t *frm)
795138593Ssam{
796138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
797138593Ssam}
798138593Ssam
799138593Ssamstatic int __inline
800138593Ssamiswmeoui(const u_int8_t *frm)
801138593Ssam{
802138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI);
803138593Ssam}
804138593Ssam
805138593Ssamstatic int __inline
806138593Ssamisatherosoui(const u_int8_t *frm)
807138593Ssam{
808138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
809138593Ssam}
810138593Ssam
811138593Ssamstatic void
812138593Ssamprinties(const u_int8_t *vp, int ielen, int maxcols)
813138593Ssam{
814138593Ssam	while (ielen > 0) {
815138593Ssam		switch (vp[0]) {
816138593Ssam		case IEEE80211_ELEMID_VENDOR:
817138593Ssam			if (iswpaoui(vp))
818138593Ssam				printie(" WPA", vp, 2+vp[1], maxcols);
819138593Ssam			else if (iswmeoui(vp))
820138593Ssam				printie(" WME", vp, 2+vp[1], maxcols);
821139492Ssam			else if (isatherosoui(vp))
822139492Ssam				printie(" ATH", vp, 2+vp[1], maxcols);
823138593Ssam			else
824138593Ssam				printie(" VEN", vp, 2+vp[1], maxcols);
825138593Ssam			break;
826138593Ssam		case IEEE80211_ELEMID_RSN:
827138593Ssam			printie(" RSN", vp, 2+vp[1], maxcols);
828138593Ssam			break;
829138593Ssam		default:
830138593Ssam			printie(" ???", vp, 2+vp[1], maxcols);
831138593Ssam			break;
832138593Ssam		}
833138593Ssam		ielen -= 2+vp[1];
834138593Ssam		vp += 2+vp[1];
835138593Ssam	}
836138593Ssam}
837138593Ssam
838138593Ssamstatic void
839138593Ssamlist_scan(int s)
840138593Ssam{
841138593Ssam	uint8_t buf[24*1024];
842138593Ssam	struct ieee80211req ireq;
843138593Ssam	char ssid[14];
844138593Ssam	uint8_t *cp;
845138593Ssam	int len;
846138593Ssam
84777218Sphk	(void) memset(&ireq, 0, sizeof(ireq));
84877218Sphk	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
849138593Ssam	ireq.i_type = IEEE80211_IOC_SCAN_RESULTS;
850138593Ssam	ireq.i_data = buf;
851138593Ssam	ireq.i_len = sizeof(buf);
852138593Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
853138593Ssam		errx(1, "unable to get scan results");
854138593Ssam	len = ireq.i_len;
855138593Ssam	if (len < sizeof(struct ieee80211req_scan_result))
856138593Ssam		return;
857138593Ssam
858138593Ssam	printf("%-14.14s  %-17.17s  %4s %4s  %-5s %3s %4s\n"
859138593Ssam		, "SSID"
860138593Ssam		, "BSSID"
861138593Ssam		, "CHAN"
862138593Ssam		, "RATE"
863138593Ssam		, "S:N"
864138593Ssam		, "INT"
865138593Ssam		, "CAPS"
866138593Ssam	);
867138593Ssam	cp = buf;
868138593Ssam	do {
869138593Ssam		struct ieee80211req_scan_result *sr;
870138593Ssam		uint8_t *vp;
871138593Ssam
872138593Ssam		sr = (struct ieee80211req_scan_result *) cp;
873138593Ssam		vp = (u_int8_t *)(sr+1);
874138593Ssam		printf("%-14.*s  %s  %3d  %3dM %2d:%-2d  %3d %-4.4s"
875138593Ssam			, copy_essid(ssid, sizeof(ssid), vp, sr->isr_ssid_len)
876138593Ssam				, ssid
877138593Ssam			, ether_ntoa((const struct ether_addr *) sr->isr_bssid)
878138593Ssam			, ieee80211_mhz2ieee(sr->isr_freq)
879138593Ssam			, getmaxrate(sr->isr_rates, sr->isr_nrates)
880138593Ssam			, sr->isr_rssi, sr->isr_noise
881138593Ssam			, sr->isr_intval
882138593Ssam			, getcaps(sr->isr_capinfo)
883138593Ssam		);
884138593Ssam		printies(vp + sr->isr_ssid_len, sr->isr_ie_len, 24);;
885138593Ssam		printf("\n");
886138593Ssam		cp += sr->isr_len, len -= sr->isr_len;
887138593Ssam	} while (len >= sizeof(struct ieee80211req_scan_result));
888138593Ssam}
889138593Ssam
890138593Ssam#include <net80211/ieee80211_freebsd.h>
891138593Ssam
892138593Ssamstatic void
893138593Ssamscan_and_wait(int s)
894138593Ssam{
895138593Ssam	struct ieee80211req ireq;
896138593Ssam	int sroute;
897138593Ssam
898138593Ssam	sroute = socket(PF_ROUTE, SOCK_RAW, 0);
899138593Ssam	if (sroute < 0) {
900138593Ssam		perror("socket(PF_ROUTE,SOCK_RAW)");
901138593Ssam		return;
902138593Ssam	}
903138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
904138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
905138593Ssam	ireq.i_type = IEEE80211_IOC_SCAN_REQ;
906138593Ssam	/* NB: only root can trigger a scan so ignore errors */
907138593Ssam	if (ioctl(s, SIOCS80211, &ireq) >= 0) {
908138593Ssam		char buf[2048];
909138593Ssam		struct if_announcemsghdr *ifan;
910138593Ssam		struct rt_msghdr *rtm;
911138593Ssam
912138593Ssam		do {
913138593Ssam			if (read(sroute, buf, sizeof(buf)) < 0) {
914138593Ssam				perror("read(PF_ROUTE)");
915138593Ssam				break;
916138593Ssam			}
917138593Ssam			rtm = (struct rt_msghdr *) buf;
918138593Ssam			if (rtm->rtm_version != RTM_VERSION)
919138593Ssam				break;
920138593Ssam			ifan = (struct if_announcemsghdr *) rtm;
921138593Ssam		} while (rtm->rtm_type != RTM_IEEE80211 ||
922138593Ssam		    ifan->ifan_what != RTM_IEEE80211_SCAN);
923138593Ssam	}
924138593Ssam	close(sroute);
925138593Ssam}
926138593Ssam
927138593Ssamstatic
928138593SsamDECL_CMD_FUNC(set80211scan, val, d)
929138593Ssam{
930138593Ssam	scan_and_wait(s);
931138593Ssam	list_scan(s);
932138593Ssam}
933138593Ssam
934138593Ssamstatic void
935138593Ssamlist_stations(int s)
936138593Ssam{
937138593Ssam	uint8_t buf[24*1024];
938138593Ssam	struct ieee80211req ireq;
939138593Ssam	uint8_t *cp;
940138593Ssam	int len;
941138593Ssam
942138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
943138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
944138593Ssam	ireq.i_type = IEEE80211_IOC_STA_INFO;
945138593Ssam	ireq.i_data = buf;
946138593Ssam	ireq.i_len = sizeof(buf);
947138593Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
948138593Ssam		errx(1, "unable to get station information");
949138593Ssam	len = ireq.i_len;
950138593Ssam	if (len < sizeof(struct ieee80211req_sta_info))
951138593Ssam		return;
952138593Ssam
953138593Ssam	printf("%-17.17s %4s %4s %4s %4s %4s %6s %6s %4s %3s\n"
954138593Ssam		, "ADDR"
955138593Ssam		, "AID"
956138593Ssam		, "CHAN"
957138593Ssam		, "RATE"
958138593Ssam		, "RSSI"
959138593Ssam		, "IDLE"
960138593Ssam		, "TXSEQ"
961138593Ssam		, "RXSEQ"
962138593Ssam		, "CAPS"
963138593Ssam		, "ERP"
964138593Ssam	);
965138593Ssam	cp = buf;
966138593Ssam	do {
967138593Ssam		struct ieee80211req_sta_info *si;
968138593Ssam		uint8_t *vp;
969138593Ssam
970138593Ssam		si = (struct ieee80211req_sta_info *) cp;
971138593Ssam		vp = (u_int8_t *)(si+1);
972138593Ssam		printf("%s %4u %4d %3dM %4d %4d %6d %6d %-4.4s %3x"
973138593Ssam			, ether_ntoa((const struct ether_addr*) si->isi_macaddr)
974138593Ssam			, IEEE80211_AID(si->isi_associd)
975138593Ssam			, ieee80211_mhz2ieee(si->isi_freq)
976138593Ssam			, (si->isi_rates[si->isi_txrate] & IEEE80211_RATE_VAL)/2
977138593Ssam			, si->isi_rssi
978138593Ssam			, si->isi_inact
979138593Ssam			, si->isi_txseqs[0]
980138593Ssam			, si->isi_rxseqs[0]
981138593Ssam			, getcaps(si->isi_capinfo)
982138593Ssam			, si->isi_erp
983138593Ssam		);
984138593Ssam		printies(vp, si->isi_ie_len, 24);
985138593Ssam		printf("\n");
986138593Ssam		cp += si->isi_len, len -= si->isi_len;
987138593Ssam	} while (len >= sizeof(struct ieee80211req_sta_info));
988138593Ssam}
989138593Ssam
990138593Ssamstatic void
991138593Ssamprint_chaninfo(const struct ieee80211_channel *c)
992138593Ssam{
993138593Ssam#define	IEEE80211_IS_CHAN_PASSIVE(_c) \
994138593Ssam	(((_c)->ic_flags & IEEE80211_CHAN_PASSIVE))
995138593Ssam	char buf[14];
996138593Ssam
997138593Ssam	buf[0] = '\0';
998138593Ssam	if (IEEE80211_IS_CHAN_FHSS(c))
999138593Ssam		strlcat(buf, " FHSS", sizeof(buf));
1000138593Ssam	if (IEEE80211_IS_CHAN_A(c))
1001138593Ssam		strlcat(buf, " 11a", sizeof(buf));
1002138593Ssam	/* XXX 11g schizophrenia */
1003138593Ssam	if (IEEE80211_IS_CHAN_G(c) ||
1004138593Ssam	    IEEE80211_IS_CHAN_PUREG(c))
1005138593Ssam		strlcat(buf, " 11g", sizeof(buf));
1006138593Ssam	else if (IEEE80211_IS_CHAN_B(c))
1007138593Ssam		strlcat(buf, " 11b", sizeof(buf));
1008138593Ssam	if (IEEE80211_IS_CHAN_T(c))
1009138593Ssam		strlcat(buf, " Turbo", sizeof(buf));
1010138593Ssam	printf("Channel %3u : %u%c Mhz%-14.14s",
1011138593Ssam		ieee80211_mhz2ieee(c->ic_freq), c->ic_freq,
1012138593Ssam		IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ', buf);
1013138593Ssam#undef IEEE80211_IS_CHAN_PASSIVE
1014138593Ssam}
1015138593Ssam
1016138593Ssamstatic void
1017138593Ssamlist_channels(int s, int allchans)
1018138593Ssam{
1019138593Ssam	struct ieee80211req ireq;
1020138593Ssam	struct ieee80211req_chaninfo chans;
1021138593Ssam	struct ieee80211req_chaninfo achans;
1022138593Ssam	const struct ieee80211_channel *c;
1023138593Ssam	int i, half;
1024138593Ssam
1025138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
1026138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
1027138593Ssam	ireq.i_type = IEEE80211_IOC_CHANINFO;
1028138593Ssam	ireq.i_data = &chans;
1029138593Ssam	ireq.i_len = sizeof(chans);
1030138593Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
1031138593Ssam		errx(1, "unable to get channel information");
1032138593Ssam	if (!allchans) {
1033138593Ssam		struct ieee80211req_chanlist active;
1034138593Ssam
1035138593Ssam		ireq.i_type = IEEE80211_IOC_CHANLIST;
1036138593Ssam		ireq.i_data = &active;
1037138593Ssam		ireq.i_len = sizeof(active);
1038138593Ssam		if (ioctl(s, SIOCG80211, &ireq) < 0)
1039138593Ssam			errx(1, "unable to get active channel list");
1040138593Ssam		memset(&achans, 0, sizeof(achans));
1041138593Ssam		for (i = 0; i < chans.ic_nchans; i++) {
1042138593Ssam			c = &chans.ic_chans[i];
1043138593Ssam			if (isset(active.ic_channels, ieee80211_mhz2ieee(c->ic_freq)) || allchans)
1044138593Ssam				achans.ic_chans[achans.ic_nchans++] = *c;
1045138593Ssam		}
1046138593Ssam	} else
1047138593Ssam		achans = chans;
1048138593Ssam	half = achans.ic_nchans / 2;
1049138593Ssam	if (achans.ic_nchans % 2)
1050138593Ssam		half++;
1051138593Ssam	for (i = 0; i < achans.ic_nchans / 2; i++) {
1052138593Ssam		print_chaninfo(&achans.ic_chans[i]);
1053138593Ssam		print_chaninfo(&achans.ic_chans[half+i]);
1054138593Ssam		printf("\n");
1055138593Ssam	}
1056138593Ssam	if (achans.ic_nchans % 2) {
1057138593Ssam		print_chaninfo(&achans.ic_chans[i]);
1058138593Ssam		printf("\n");
1059138593Ssam	}
1060138593Ssam}
1061138593Ssam
1062138593Ssamstatic void
1063138593Ssamlist_keys(int s)
1064138593Ssam{
1065138593Ssam}
1066138593Ssam
1067138593Ssam#define	IEEE80211_C_BITS \
1068138593Ssam"\020\1WEP\2TKIP\3AES\4AES_CCM\6CKIP\11IBSS\12PMGT\13HOSTAP\14AHDEMO" \
1069138593Ssam"\15SWRETRY\16TXPMGT\17SHSLOT\20SHPREAMBLE\21MONITOR\22TKIPMIC\30WPA1" \
1070138593Ssam"\31WPA2\32BURST\33WME"
1071138593Ssam
1072138593Ssamstatic void
1073138593Ssamlist_capabilities(int s)
1074138593Ssam{
1075138593Ssam	struct ieee80211req ireq;
1076138593Ssam	u_int32_t caps;
1077138593Ssam
1078138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
1079138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
1080138593Ssam	ireq.i_type = IEEE80211_IOC_DRIVER_CAPS;
1081138593Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
1082138593Ssam		errx(1, "unable to get driver capabilities");
1083138593Ssam	caps = (((u_int16_t) ireq.i_val) << 16) | ((u_int16_t) ireq.i_len);
1084138593Ssam	printb(name, caps, IEEE80211_C_BITS);
1085138593Ssam	putchar('\n');
1086138593Ssam}
1087138593Ssam
1088138593Ssamstatic void
1089138593Ssamlist_wme(int s)
1090138593Ssam{
1091138593Ssam	static const char *acnames[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" };
1092138593Ssam	struct ieee80211req ireq;
1093138593Ssam	int ac;
1094138593Ssam
1095138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
1096138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
1097138593Ssam	ireq.i_len = 0;
1098138593Ssam	for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++) {
1099138593Ssamagain:
1100138593Ssam		if (ireq.i_len & IEEE80211_WMEPARAM_BSS)
1101138593Ssam			printf("\t%s", "     ");
1102138593Ssam		else
1103138593Ssam			printf("\t%s", acnames[ac]);
1104138593Ssam
1105138593Ssam		ireq.i_len = (ireq.i_len & IEEE80211_WMEPARAM_BSS) | ac;
1106138593Ssam
1107138593Ssam		/* show WME BSS parameters */
1108138593Ssam		ireq.i_type = IEEE80211_IOC_WME_CWMIN;
1109138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1)
1110138593Ssam			printf(" cwmin %2u", ireq.i_val);
1111138593Ssam		ireq.i_type = IEEE80211_IOC_WME_CWMAX;
1112138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1)
1113138593Ssam			printf(" cwmax %2u", ireq.i_val);
1114138593Ssam		ireq.i_type = IEEE80211_IOC_WME_AIFS;
1115138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1)
1116138593Ssam			printf(" aifs %2u", ireq.i_val);
1117138593Ssam		ireq.i_type = IEEE80211_IOC_WME_TXOPLIMIT;
1118138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1)
1119138593Ssam			printf(" txopLimit %3u", ireq.i_val);
1120138593Ssam		ireq.i_type = IEEE80211_IOC_WME_ACM;
1121138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1122138593Ssam			if (ireq.i_val)
1123138593Ssam				printf(" acm");
1124138593Ssam			else if (verbose)
1125138593Ssam				printf(" -acm");
1126138593Ssam		}
1127138593Ssam		/* !BSS only */
1128138593Ssam		if ((ireq.i_len & IEEE80211_WMEPARAM_BSS) == 0) {
1129138593Ssam			ireq.i_type = IEEE80211_IOC_WME_ACKPOLICY;
1130138593Ssam			if (ioctl(s, SIOCG80211, &ireq) != -1) {
1131138593Ssam				if (!ireq.i_val)
1132138593Ssam					printf(" -ack");
1133138593Ssam				else if (verbose)
1134138593Ssam					printf(" ack");
1135138593Ssam			}
1136138593Ssam		}
1137138593Ssam		printf("\n");
1138138593Ssam		if ((ireq.i_len & IEEE80211_WMEPARAM_BSS) == 0) {
1139138593Ssam			ireq.i_len |= IEEE80211_WMEPARAM_BSS;
1140138593Ssam			goto again;
1141138593Ssam		} else
1142138593Ssam			ireq.i_len &= ~IEEE80211_WMEPARAM_BSS;
1143138593Ssam	}
1144138593Ssam}
1145138593Ssam
1146149029Ssamstatic void
1147149029Ssamlist_mac(int s)
1148149029Ssam{
1149149029Ssam	struct ieee80211req ireq;
1150149029Ssam	struct ieee80211req_maclist *acllist;
1151149029Ssam	int i, nacls, policy;
1152149029Ssam	char c;
1153149029Ssam
1154149029Ssam	(void) memset(&ireq, 0, sizeof(ireq));
1155149029Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); /* XXX ?? */
1156149029Ssam	ireq.i_type = IEEE80211_IOC_MACCMD;
1157149029Ssam	ireq.i_val = IEEE80211_MACCMD_POLICY;
1158149029Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0) {
1159149029Ssam		if (errno == EINVAL) {
1160149029Ssam			printf("No acl policy loaded\n");
1161149029Ssam			return;
1162149029Ssam		}
1163149029Ssam		err(1, "unable to get mac policy");
1164149029Ssam	}
1165149029Ssam	policy = ireq.i_val;
1166149029Ssam
1167149029Ssam	ireq.i_val = IEEE80211_MACCMD_LIST;
1168149029Ssam	ireq.i_len = 0;
1169149029Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
1170149029Ssam		err(1, "unable to get mac acl list size");
1171149029Ssam	if (ireq.i_len == 0)		/* NB: no acls */
1172149029Ssam		return;
1173149029Ssam
1174149029Ssam	ireq.i_data = malloc(ireq.i_len);
1175149029Ssam	if (ireq.i_data == NULL)
1176149029Ssam		err(1, "out of memory for acl list");
1177149029Ssam
1178149029Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
1179149029Ssam		err(1, "unable to get mac acl list");
1180149029Ssam	if (policy == IEEE80211_MACCMD_POLICY_OPEN) {
1181149029Ssam		if (verbose)
1182149029Ssam			printf("policy: open\n");
1183149029Ssam		c = '*';
1184149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_ALLOW) {
1185149029Ssam		if (verbose)
1186149029Ssam			printf("policy: allow\n");
1187149029Ssam		c = '+';
1188149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_DENY) {
1189149029Ssam		if (verbose)
1190149029Ssam			printf("policy: deny\n");
1191149029Ssam		c = '-';
1192149029Ssam	} else {
1193149029Ssam		printf("policy: unknown (%u)\n", policy);
1194149029Ssam		c = '?';
1195149029Ssam	}
1196149029Ssam	nacls = ireq.i_len / sizeof(*acllist);
1197149029Ssam	acllist = (struct ieee80211req_maclist *) ireq.i_data;
1198149029Ssam	for (i = 0; i < nacls; i++)
1199149029Ssam		printf("%c%s\n", c, ether_ntoa(
1200149029Ssam			(const struct ether_addr *) acllist[i].ml_macaddr));
1201149029Ssam}
1202149029Ssam
1203138593Ssamstatic
1204138593SsamDECL_CMD_FUNC(set80211list, arg, d)
1205138593Ssam{
1206138593Ssam#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
1207138593Ssam
1208138593Ssam	if (iseq(arg, "sta"))
1209138593Ssam		list_stations(s);
1210138593Ssam	else if (iseq(arg, "scan") || iseq(arg, "ap"))
1211138593Ssam		list_scan(s);
1212138593Ssam	else if (iseq(arg, "chan") || iseq(arg, "freq"))
1213138593Ssam		list_channels(s, 1);
1214138593Ssam	else if (iseq(arg, "active"))
1215138593Ssam		list_channels(s, 0);
1216138593Ssam	else if (iseq(arg, "keys"))
1217138593Ssam		list_keys(s);
1218138593Ssam	else if (iseq(arg, "caps"))
1219138593Ssam		list_capabilities(s);
1220138593Ssam	else if (iseq(arg, "wme"))
1221138593Ssam		list_wme(s);
1222149029Ssam	else if (iseq(arg, "mac"))
1223149029Ssam		list_mac(s);
1224138593Ssam	else
1225138593Ssam		errx(1, "Don't know how to list %s for %s", arg, name);
1226138593Ssam#undef iseq
1227138593Ssam}
1228138593Ssam
1229138593Ssamstatic enum ieee80211_opmode
1230138593Ssamget80211opmode(int s)
1231138593Ssam{
1232138593Ssam	struct ifmediareq ifmr;
1233138593Ssam
1234138593Ssam	(void) memset(&ifmr, 0, sizeof(ifmr));
1235138593Ssam	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
1236138593Ssam
1237138593Ssam	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
1238138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_ADHOC)
1239138593Ssam			return IEEE80211_M_IBSS;	/* XXX ahdemo */
1240138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
1241138593Ssam			return IEEE80211_M_HOSTAP;
1242138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
1243138593Ssam			return IEEE80211_M_MONITOR;
1244138593Ssam	}
1245138593Ssam	return IEEE80211_M_STA;
1246138593Ssam}
1247138593Ssam
1248138593Ssamstatic const struct ieee80211_channel *
1249138593Ssamgetchaninfo(int s, int chan)
1250138593Ssam{
1251138593Ssam	struct ieee80211req ireq;
1252138593Ssam	static struct ieee80211req_chaninfo chans;
1253138593Ssam	static struct ieee80211_channel undef;
1254138593Ssam	const struct ieee80211_channel *c;
1255138593Ssam	int i, freq;
1256138593Ssam
1257138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
1258138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
1259138593Ssam	ireq.i_type = IEEE80211_IOC_CHANINFO;
1260138593Ssam	ireq.i_data = &chans;
1261138593Ssam	ireq.i_len = sizeof(chans);
1262138593Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
1263138593Ssam		errx(1, "unable to get channel information");
1264138593Ssam	freq = ieee80211_ieee2mhz(chan);
1265138593Ssam	for (i = 0; i < chans.ic_nchans; i++) {
1266138593Ssam		c = &chans.ic_chans[i];
1267138593Ssam		if (c->ic_freq == freq)
1268138593Ssam			return c;
1269138593Ssam	}
1270138593Ssam	return &undef;
1271138593Ssam}
1272138593Ssam
1273138593Ssam#if 0
1274138593Ssamstatic void
1275138593Ssamprintcipher(int s, struct ieee80211req *ireq, int keylenop)
1276138593Ssam{
1277138593Ssam	switch (ireq->i_val) {
1278138593Ssam	case IEEE80211_CIPHER_WEP:
1279138593Ssam		ireq->i_type = keylenop;
1280138593Ssam		if (ioctl(s, SIOCG80211, ireq) != -1)
1281138593Ssam			printf("WEP-%s",
1282138593Ssam			    ireq->i_len <= 5 ? "40" :
1283138593Ssam			    ireq->i_len <= 13 ? "104" : "128");
1284138593Ssam		else
1285138593Ssam			printf("WEP");
1286138593Ssam		break;
1287138593Ssam	case IEEE80211_CIPHER_TKIP:
1288138593Ssam		printf("TKIP");
1289138593Ssam		break;
1290138593Ssam	case IEEE80211_CIPHER_AES_OCB:
1291138593Ssam		printf("AES-OCB");
1292138593Ssam		break;
1293138593Ssam	case IEEE80211_CIPHER_AES_CCM:
1294138593Ssam		printf("AES-CCM");
1295138593Ssam		break;
1296138593Ssam	case IEEE80211_CIPHER_CKIP:
1297138593Ssam		printf("CKIP");
1298138593Ssam		break;
1299138593Ssam	case IEEE80211_CIPHER_NONE:
1300138593Ssam		printf("NONE");
1301138593Ssam		break;
1302138593Ssam	default:
1303138593Ssam		printf("UNKNOWN (0x%x)", ireq->i_val);
1304138593Ssam		break;
1305138593Ssam	}
1306138593Ssam}
1307138593Ssam#endif
1308138593Ssam
1309138593Ssam#define	MAXCOL	78
1310138593Ssamint	col;
1311138593Ssamchar	spacer;
1312138593Ssam
1313138593Ssam#define	LINE_BREAK() do {			\
1314138593Ssam	if (spacer != '\t') {			\
1315138593Ssam		printf("\n");			\
1316138593Ssam		spacer = '\t';			\
1317138593Ssam	}					\
1318138593Ssam	col = 8;	/* 8-col tab */		\
1319138593Ssam} while (0)
1320138593Ssam#define	LINE_CHECK(fmt, ...) do {		\
1321138593Ssam	col += sizeof(fmt)-2;			\
1322138593Ssam	if (col > MAXCOL) {			\
1323138593Ssam		LINE_BREAK();			\
1324138593Ssam		col += sizeof(fmt)-2;		\
1325138593Ssam	}					\
1326138593Ssam	printf(fmt, __VA_ARGS__);		\
1327138593Ssam	spacer = ' ';				\
1328138593Ssam} while (0)
1329138593Ssam
1330138593Ssamstatic void
1331138593Ssamprintkey(const struct ieee80211req_key *ik)
1332138593Ssam{
1333138593Ssam	static const uint8_t zerodata[IEEE80211_KEYBUF_SIZE];
1334138593Ssam	int keylen = ik->ik_keylen;
1335138593Ssam	int printcontents;
1336138593Ssam
1337148001Srwatson	printcontents = printkeys &&
1338138593Ssam		(memcmp(ik->ik_keydata, zerodata, keylen) != 0 || verbose);
1339138593Ssam	if (printcontents)
1340138593Ssam		LINE_BREAK();
1341138593Ssam	switch (ik->ik_type) {
1342138593Ssam	case IEEE80211_CIPHER_WEP:
1343138593Ssam		/* compatibility */
1344138593Ssam		LINE_CHECK("%cwepkey %u:%s", spacer, ik->ik_keyix+1,
1345138593Ssam		    keylen <= 5 ? "40-bit" :
1346138593Ssam		    keylen <= 13 ? "104-bit" : "128-bit");
1347138593Ssam		break;
1348138593Ssam	case IEEE80211_CIPHER_TKIP:
1349138593Ssam		if (keylen > 128/8)
1350138593Ssam			keylen -= 128/8;	/* ignore MIC for now */
1351138593Ssam		LINE_CHECK("%cTKIP %u:%u-bit",
1352138593Ssam			spacer, ik->ik_keyix+1, 8*keylen);
1353138593Ssam		break;
1354138593Ssam	case IEEE80211_CIPHER_AES_OCB:
1355138593Ssam		LINE_CHECK("%cAES-OCB %u:%u-bit",
1356138593Ssam			spacer, ik->ik_keyix+1, 8*keylen);
1357138593Ssam		break;
1358138593Ssam	case IEEE80211_CIPHER_AES_CCM:
1359138593Ssam		LINE_CHECK("%cAES-CCM %u:%u-bit",
1360138593Ssam			spacer, ik->ik_keyix+1, 8*keylen);
1361138593Ssam		break;
1362138593Ssam	case IEEE80211_CIPHER_CKIP:
1363138593Ssam		LINE_CHECK("%cCKIP %u:%u-bit",
1364138593Ssam			spacer, ik->ik_keyix+1, 8*keylen);
1365138593Ssam		break;
1366138593Ssam	case IEEE80211_CIPHER_NONE:
1367138593Ssam		LINE_CHECK("%cNULL %u:%u-bit",
1368138593Ssam			spacer, ik->ik_keyix+1, 8*keylen);
1369138593Ssam		break;
1370138593Ssam	default:
1371138593Ssam		LINE_CHECK("%cUNKNOWN (0x%x) %u:%u-bit", spacer,
1372138593Ssam			ik->ik_type, ik->ik_keyix+1, 8*keylen);
1373138593Ssam		break;
1374138593Ssam	}
1375138593Ssam	if (printcontents) {
1376138593Ssam		int i;
1377138593Ssam
1378138593Ssam		printf(" <");
1379138593Ssam		for (i = 0; i < keylen; i++)
1380138593Ssam			printf("%02x", ik->ik_keydata[i]);
1381138593Ssam		printf(">");
1382138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
1383138593Ssam		    (ik->ik_keyrsc != 0 || verbose))
1384146873Sjhb			printf(" rsc %ju", (uintmax_t)ik->ik_keyrsc);
1385138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
1386138593Ssam		    (ik->ik_keytsc != 0 || verbose))
1387146873Sjhb			printf(" tsc %ju", (uintmax_t)ik->ik_keytsc);
1388138593Ssam		if (ik->ik_flags != 0 && verbose) {
1389138593Ssam			const char *sep = " ";
1390138593Ssam
1391138593Ssam			if (ik->ik_flags & IEEE80211_KEY_XMIT)
1392138593Ssam				printf("%stx", sep), sep = "+";
1393138593Ssam			if (ik->ik_flags & IEEE80211_KEY_RECV)
1394138593Ssam				printf("%srx", sep), sep = "+";
1395138593Ssam			if (ik->ik_flags & IEEE80211_KEY_DEFAULT)
1396138593Ssam				printf("%sdef", sep), sep = "+";
1397138593Ssam		}
1398138593Ssam		LINE_BREAK();
1399138593Ssam	}
1400138593Ssam}
1401138593Ssam
1402138593Ssamstatic void
1403139494Ssamieee80211_status(int s)
1404138593Ssam{
1405138593Ssam	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
1406138593Ssam	enum ieee80211_opmode opmode = get80211opmode(s);
1407138593Ssam	int i, num, wpa, wme;
1408138593Ssam	struct ieee80211req ireq;
1409138593Ssam	u_int8_t data[32];
1410138593Ssam	const struct ieee80211_channel *c;
1411138593Ssam
1412138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
1413138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
141477218Sphk	ireq.i_data = &data;
141577218Sphk
1416138593Ssam	wpa = 0;		/* unknown/not set */
1417138593Ssam
141877218Sphk	ireq.i_type = IEEE80211_IOC_SSID;
141977218Sphk	ireq.i_val = -1;
142077218Sphk	if (ioctl(s, SIOCG80211, &ireq) < 0) {
1421148686Sstefanf		/* If we can't get the SSID, this isn't an 802.11 device. */
142277218Sphk		return;
142377218Sphk	}
142488748Sambrisko	num = 0;
142588748Sambrisko	ireq.i_type = IEEE80211_IOC_NUMSSIDS;
1426138593Ssam	if (ioctl(s, SIOCG80211, &ireq) >= 0)
142788748Sambrisko		num = ireq.i_val;
1428138593Ssam	printf("\tssid ");
1429138593Ssam	if (num > 1) {
1430138593Ssam		ireq.i_type = IEEE80211_IOC_SSID;
1431138593Ssam		for (ireq.i_val = 0; ireq.i_val < num; ireq.i_val++) {
1432138593Ssam			if (ioctl(s, SIOCG80211, &ireq) >= 0 && ireq.i_len > 0) {
1433138593Ssam				printf(" %d:", ireq.i_val + 1);
1434138593Ssam				print_string(data, ireq.i_len);
1435138593Ssam			}
143688748Sambrisko		}
1437138593Ssam	} else
1438138593Ssam		print_string(data, ireq.i_len);
143977218Sphk
1440138593Ssam	ireq.i_type = IEEE80211_IOC_CHANNEL;
1441138593Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
1442138593Ssam		goto end;
1443138593Ssam	c = getchaninfo(s, ireq.i_val);
1444138593Ssam	if (ireq.i_val != -1) {
1445138593Ssam		printf(" channel %d", ireq.i_val);
1446138593Ssam		if (verbose)
1447138593Ssam			printf(" (%u)", c->ic_freq);
1448138593Ssam	} else if (verbose)
1449138593Ssam		printf(" channel UNDEF");
1450138593Ssam
1451138593Ssam	ireq.i_type = IEEE80211_IOC_BSSID;
1452138593Ssam	ireq.i_len = IEEE80211_ADDR_LEN;
1453138593Ssam	if (ioctl(s, SIOCG80211, &ireq) >= 0 &&
1454138593Ssam	    memcmp(ireq.i_data, zerobssid, sizeof(zerobssid)) != 0)
1455138593Ssam		printf(" bssid %s", ether_ntoa(ireq.i_data));
1456138593Ssam
145777218Sphk	ireq.i_type = IEEE80211_IOC_STATIONNAME;
145877218Sphk	if (ioctl(s, SIOCG80211, &ireq) != -1) {
1459138593Ssam		printf("\n\tstationname ");
146077218Sphk		print_string(data, ireq.i_len);
146177218Sphk	}
146277218Sphk
1463138593Ssam	spacer = ' ';		/* force first break */
1464138593Ssam	LINE_BREAK();
146577218Sphk
146677218Sphk	ireq.i_type = IEEE80211_IOC_AUTHMODE;
146777218Sphk	if (ioctl(s, SIOCG80211, &ireq) != -1) {
146877218Sphk		switch (ireq.i_val) {
146977218Sphk			case IEEE80211_AUTH_NONE:
1470138593Ssam				LINE_CHECK("%cauthmode NONE", spacer);
147177218Sphk				break;
147277218Sphk			case IEEE80211_AUTH_OPEN:
1473138593Ssam				LINE_CHECK("%cauthmode OPEN", spacer);
147477218Sphk				break;
147577218Sphk			case IEEE80211_AUTH_SHARED:
1476138593Ssam				LINE_CHECK("%cauthmode SHARED", spacer);
147777218Sphk				break;
1478138593Ssam			case IEEE80211_AUTH_8021X:
1479138593Ssam				LINE_CHECK("%cauthmode 802.1x", spacer);
148077218Sphk				break;
1481138593Ssam			case IEEE80211_AUTH_WPA:
1482138593Ssam				ireq.i_type = IEEE80211_IOC_WPA;
1483138593Ssam				if (ioctl(s, SIOCG80211, &ireq) != -1)
1484138593Ssam					wpa = ireq.i_val;
1485138593Ssam				if (!wpa)
1486138593Ssam					wpa = 1;	/* default to WPA1 */
1487138593Ssam				switch (wpa) {
1488138593Ssam				case 2:
1489138593Ssam					LINE_CHECK("%cauthmode WPA2/802.11i",
1490138593Ssam						spacer);
1491138593Ssam					break;
1492138593Ssam				case 3:
1493138593Ssam					LINE_CHECK("%cauthmode WPA1+WPA2/802.11i",
1494138593Ssam						spacer);
1495138593Ssam					break;
1496138593Ssam				default:
1497138593Ssam					LINE_CHECK("%cauthmode WPA", spacer);
1498138593Ssam					break;
1499138593Ssam				}
150077218Sphk				break;
1501138593Ssam			case IEEE80211_AUTH_AUTO:
1502138593Ssam				LINE_CHECK("%cauthmode AUTO", spacer);
150377218Sphk				break;
1504127649Ssam			default:
1505138593Ssam				LINE_CHECK("%cauthmode UNKNOWN (0x%x)",
1506138593Ssam					spacer, ireq.i_val);
1507127649Ssam				break;
1508127649Ssam		}
1509127649Ssam	}
1510127649Ssam
151177218Sphk	ireq.i_type = IEEE80211_IOC_WEP;
151280315Sbrooks	if (ioctl(s, SIOCG80211, &ireq) != -1 &&
151380315Sbrooks	    ireq.i_val != IEEE80211_WEP_NOSUP) {
1514138718Ssam		int firstkey, wepmode;
1515138593Ssam
1516138718Ssam		wepmode = ireq.i_val;
1517138718Ssam		switch (wepmode) {
151877218Sphk			case IEEE80211_WEP_OFF:
1519138593Ssam				LINE_CHECK("%cprivacy OFF", spacer);
152077218Sphk				break;
152177218Sphk			case IEEE80211_WEP_ON:
1522138593Ssam				LINE_CHECK("%cprivacy ON", spacer);
152377218Sphk				break;
152477218Sphk			case IEEE80211_WEP_MIXED:
1525138593Ssam				LINE_CHECK("%cprivacy MIXED", spacer);
152677218Sphk				break;
152777218Sphk			default:
1528138593Ssam				LINE_CHECK("%cprivacy UNKNOWN (0x%x)",
1529138718Ssam					spacer, wepmode);
153077218Sphk				break;
153177218Sphk		}
153277218Sphk
153377218Sphk		/*
153477218Sphk		 * If we get here then we've got WEP support so we need
153577218Sphk		 * to print WEP status.
153691454Sbrooks		 */
153777218Sphk
153877218Sphk		ireq.i_type = IEEE80211_IOC_WEPTXKEY;
153977218Sphk		if (ioctl(s, SIOCG80211, &ireq) < 0) {
154077218Sphk			warn("WEP support, but no tx key!");
154177218Sphk			goto end;
154277218Sphk		}
1543138593Ssam		if (ireq.i_val != -1)
1544138593Ssam			LINE_CHECK("%cdeftxkey %d", spacer, ireq.i_val+1);
1545138718Ssam		else if (wepmode != IEEE80211_WEP_OFF || verbose)
1546138593Ssam			LINE_CHECK("%cdeftxkey UNDEF", spacer);
154777218Sphk
154877218Sphk		ireq.i_type = IEEE80211_IOC_NUMWEPKEYS;
154977218Sphk		if (ioctl(s, SIOCG80211, &ireq) < 0) {
155077218Sphk			warn("WEP support, but no NUMWEPKEYS support!");
155177218Sphk			goto end;
155277218Sphk		}
155377218Sphk		num = ireq.i_val;
155477218Sphk
1555138593Ssam		firstkey = 1;
1556138593Ssam		for (i = 0; i < num; i++) {
1557138593Ssam			struct ieee80211req_key ik;
155877218Sphk
1559138593Ssam			memset(&ik, 0, sizeof(ik));
1560138593Ssam			ik.ik_keyix = i;
1561138593Ssam			ireq.i_type = IEEE80211_IOC_WPAKEY;
1562138593Ssam			ireq.i_data = &ik;
1563138593Ssam			ireq.i_len = sizeof(ik);
156477218Sphk			if (ioctl(s, SIOCG80211, &ireq) < 0) {
156577218Sphk				warn("WEP support, but can get keys!");
156677218Sphk				goto end;
156777218Sphk			}
1568138593Ssam			if (ik.ik_keylen != 0) {
1569138593Ssam				if (verbose)
1570138593Ssam					LINE_BREAK();
1571138593Ssam				printkey(&ik);
1572138593Ssam				firstkey = 0;
1573138593Ssam			}
1574138593Ssam		}
1575138593Ssam	}
1576138593Ssam
1577138593Ssam	ireq.i_type = IEEE80211_IOC_POWERSAVE;
1578138593Ssam	if (ioctl(s, SIOCG80211, &ireq) != -1 &&
1579138593Ssam	    ireq.i_val != IEEE80211_POWERSAVE_NOSUP ) {
1580138593Ssam		if (ireq.i_val != IEEE80211_POWERSAVE_OFF || verbose) {
1581138593Ssam			switch (ireq.i_val) {
1582138593Ssam				case IEEE80211_POWERSAVE_OFF:
1583138593Ssam					LINE_CHECK("%cpowersavemode OFF",
1584138593Ssam						spacer);
1585138593Ssam					break;
1586138593Ssam				case IEEE80211_POWERSAVE_CAM:
1587138593Ssam					LINE_CHECK("%cpowersavemode CAM",
1588138593Ssam						spacer);
1589138593Ssam					break;
1590138593Ssam				case IEEE80211_POWERSAVE_PSP:
1591138593Ssam					LINE_CHECK("%cpowersavemode PSP",
1592138593Ssam						spacer);
1593138593Ssam					break;
1594138593Ssam				case IEEE80211_POWERSAVE_PSP_CAM:
1595138593Ssam					LINE_CHECK("%cpowersavemode PSP-CAM",
1596138593Ssam						spacer);
1597138593Ssam					break;
1598138593Ssam			}
1599138593Ssam			ireq.i_type = IEEE80211_IOC_POWERSAVESLEEP;
1600138593Ssam			if (ioctl(s, SIOCG80211, &ireq) != -1)
1601138593Ssam				LINE_CHECK("%cpowersavesleep %d",
1602138593Ssam					spacer, ireq.i_val);
1603138593Ssam		}
1604138593Ssam	}
1605138593Ssam
1606138593Ssam	ireq.i_type = IEEE80211_IOC_TXPOWMAX;
1607138593Ssam	if (ioctl(s, SIOCG80211, &ireq) != -1)
1608138593Ssam		LINE_CHECK("%ctxpowmax %d", spacer, ireq.i_val);
1609138593Ssam
1610138593Ssam	if (verbose) {
1611138593Ssam		ireq.i_type = IEEE80211_IOC_TXPOWER;
1612138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1)
1613138593Ssam			LINE_CHECK("%ctxpower %d", spacer, ireq.i_val);
1614138593Ssam	}
1615138593Ssam
1616138593Ssam	ireq.i_type = IEEE80211_IOC_RTSTHRESHOLD;
1617138593Ssam	if (ioctl(s, SIOCG80211, &ireq) != -1) {
1618138593Ssam		if (ireq.i_val != IEEE80211_RTS_MAX || verbose)
1619138593Ssam			LINE_CHECK("%crtsthreshold %d", spacer, ireq.i_val);
1620138593Ssam	}
1621138593Ssam
1622153354Ssam	ireq.i_type = IEEE80211_IOC_MCAST_RATE;
1623153354Ssam	if (ioctl(s, SIOCG80211, &ireq) != -1) {
1624153354Ssam		if (ireq.i_val != 2*1 || verbose) {
1625153354Ssam			if (ireq.i_val == 11)
1626153354Ssam				LINE_CHECK("%cmcastrate 5.5", spacer);
1627153354Ssam			else
1628153354Ssam				LINE_CHECK("%cmcastrate %d", spacer,
1629153354Ssam					ireq.i_val/2);
1630153354Ssam		}
1631153354Ssam	}
1632153354Ssam
1633148416Ssam	ireq.i_type = IEEE80211_IOC_FRAGTHRESHOLD;
1634148416Ssam	if (ioctl(s, SIOCG80211, &ireq) != -1) {
1635148416Ssam		if (ireq.i_val != IEEE80211_FRAG_MAX || verbose)
1636148416Ssam			LINE_CHECK("%cfragthreshold %d", spacer, ireq.i_val);
1637148416Ssam	}
1638148416Ssam
1639138593Ssam	if (IEEE80211_IS_CHAN_G(c) || IEEE80211_IS_CHAN_PUREG(c) || verbose) {
1640147795Ssam		ireq.i_type = IEEE80211_IOC_PUREG;
1641147795Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1642147795Ssam			if (ireq.i_val)
1643147795Ssam				LINE_CHECK("%cpureg", spacer);
1644147795Ssam			else if (verbose)
1645147795Ssam				LINE_CHECK("%c-pureg", spacer);
1646147795Ssam		}
1647138593Ssam		ireq.i_type = IEEE80211_IOC_PROTMODE;
1648138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1649138593Ssam			switch (ireq.i_val) {
1650138593Ssam				case IEEE80211_PROTMODE_OFF:
1651138593Ssam					LINE_CHECK("%cprotmode OFF", spacer);
1652138593Ssam					break;
1653138593Ssam				case IEEE80211_PROTMODE_CTS:
1654138593Ssam					LINE_CHECK("%cprotmode CTS", spacer);
1655138593Ssam					break;
1656138593Ssam				case IEEE80211_PROTMODE_RTSCTS:
1657138593Ssam					LINE_CHECK("%cprotmode RTSCTS", spacer);
1658138593Ssam					break;
1659138593Ssam				default:
1660138593Ssam					LINE_CHECK("%cprotmode UNKNOWN (0x%x)",
1661138593Ssam						spacer, ireq.i_val);
1662138593Ssam					break;
1663138593Ssam			}
1664138593Ssam		}
1665138593Ssam	}
1666138593Ssam
1667138593Ssam	ireq.i_type = IEEE80211_IOC_WME;
1668138593Ssam	if (ioctl(s, SIOCG80211, &ireq) != -1) {
1669138593Ssam		wme = ireq.i_val;
1670138593Ssam		if (wme)
1671138593Ssam			LINE_CHECK("%cwme", spacer);
1672138593Ssam		else if (verbose)
1673138593Ssam			LINE_CHECK("%c-wme", spacer);
1674138593Ssam	} else
1675138593Ssam		wme = 0;
1676138593Ssam
1677138593Ssam	if (opmode == IEEE80211_M_HOSTAP) {
1678138593Ssam		ireq.i_type = IEEE80211_IOC_HIDESSID;
1679138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1680138593Ssam			if (ireq.i_val)
1681138593Ssam				LINE_CHECK("%cssid HIDE", spacer);
1682138593Ssam			else if (verbose)
1683138593Ssam				LINE_CHECK("%cssid SHOW", spacer);
1684138593Ssam		}
1685138593Ssam
1686138593Ssam		ireq.i_type = IEEE80211_IOC_APBRIDGE;
1687138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1688138593Ssam			if (!ireq.i_val)
1689138593Ssam				LINE_CHECK("%c-apbridge", spacer);
1690138593Ssam			else if (verbose)
1691138593Ssam				LINE_CHECK("%capbridge", spacer);
1692138593Ssam		}
1693138593Ssam
1694138593Ssam		ireq.i_type = IEEE80211_IOC_DTIM_PERIOD;
1695138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1)
1696138593Ssam			LINE_CHECK("%cdtimperiod %u", spacer, ireq.i_val);
1697138593Ssam	} else {
1698138593Ssam		ireq.i_type = IEEE80211_IOC_ROAMING;
1699138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1700138593Ssam			if (ireq.i_val != IEEE80211_ROAMING_AUTO || verbose) {
1701138593Ssam				switch (ireq.i_val) {
1702138593Ssam				case IEEE80211_ROAMING_DEVICE:
1703138593Ssam					LINE_CHECK("%croaming DEVICE", spacer);
1704138593Ssam					break;
1705138593Ssam				case IEEE80211_ROAMING_AUTO:
1706138593Ssam					LINE_CHECK("%croaming AUTO", spacer);
1707138593Ssam					break;
1708138593Ssam				case IEEE80211_ROAMING_MANUAL:
1709138593Ssam					LINE_CHECK("%croaming MANUAL", spacer);
1710138593Ssam					break;
1711138593Ssam				default:
1712138593Ssam					LINE_CHECK("%croaming UNKNOWN (0x%x)",
1713138593Ssam						spacer, ireq.i_val);
1714138593Ssam					break;
1715138593Ssam				}
1716138593Ssam			}
1717138593Ssam		}
1718138593Ssam	}
1719138593Ssam	ireq.i_type = IEEE80211_IOC_BEACON_INTERVAL;
1720138593Ssam	if (ioctl(s, SIOCG80211, &ireq) != -1) {
1721138593Ssam		if (ireq.i_val)
1722138593Ssam			LINE_CHECK("%cbintval %u", spacer, ireq.i_val);
1723138593Ssam		else if (verbose)
1724138593Ssam			LINE_CHECK("%cbintval %u", spacer, ireq.i_val);
1725138593Ssam	}
1726138593Ssam
1727138593Ssam	if (wme && verbose) {
1728138593Ssam		LINE_BREAK();
1729138593Ssam		list_wme(s);
1730138593Ssam	}
1731138593Ssam
1732138593Ssam	if (wpa) {
1733138593Ssam		ireq.i_type = IEEE80211_IOC_COUNTERMEASURES;
1734138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1735138593Ssam			if (ireq.i_val)
1736138593Ssam				LINE_CHECK("%ccountermeasures", spacer);
1737138593Ssam			else if (verbose)
1738138593Ssam				LINE_CHECK("%c-countermeasures", spacer);
1739138593Ssam		}
1740138593Ssam#if 0
1741138593Ssam		/* XXX not interesting with WPA done in user space */
1742138593Ssam		ireq.i_type = IEEE80211_IOC_KEYMGTALGS;
1743138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1744138593Ssam		}
1745138593Ssam
1746138593Ssam		ireq.i_type = IEEE80211_IOC_MCASTCIPHER;
1747138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1748138593Ssam			printf("%cmcastcipher ", spacer);
1749138593Ssam			printcipher(s, &ireq, IEEE80211_IOC_MCASTKEYLEN);
1750138593Ssam			spacer = ' ';
1751138593Ssam		}
1752138593Ssam
1753138593Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHER;
1754138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1755138593Ssam			printf("%cucastcipher ", spacer);
1756138593Ssam			printcipher(s, &ireq, IEEE80211_IOC_UCASTKEYLEN);
1757138593Ssam		}
1758138593Ssam
1759138593Ssam		if (wpa & 2) {
1760138593Ssam			ireq.i_type = IEEE80211_IOC_RSNCAPS;
1761138593Ssam			if (ioctl(s, SIOCG80211, &ireq) != -1) {
1762138593Ssam				printf("%cRSN caps 0x%x", spacer, ireq.i_val);
176377218Sphk				spacer = ' ';
1764138593Ssam			}
176577218Sphk		}
1766138593Ssam
1767138593Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHERS;
1768138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1769138593Ssam		}
1770138593Ssam#endif
1771138593Ssam		LINE_BREAK();
177277218Sphk	}
1773138593Ssam	LINE_BREAK();
177477218Sphk
177577218Sphkend:
177677218Sphk	return;
177777218Sphk}
177877218Sphk
177977218Sphkstatic void
178077218Sphkset80211(int s, int type, int val, int len, u_int8_t *data)
178177218Sphk{
178277218Sphk	struct ieee80211req	ireq;
178377218Sphk
178477218Sphk	(void) memset(&ireq, 0, sizeof(ireq));
178577218Sphk	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
178677218Sphk	ireq.i_type = type;
178777218Sphk	ireq.i_val = val;
178877218Sphk	ireq.i_len = len;
178977218Sphk	ireq.i_data = data;
179091454Sbrooks	if (ioctl(s, SIOCS80211, &ireq) < 0)
179177218Sphk		err(1, "SIOCS80211");
179277218Sphk}
179377218Sphk
179477218Sphkstatic const char *
179577218Sphkget_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
179677218Sphk{
179777218Sphk	int len;
179877218Sphk	int hexstr;
179977218Sphk	u_int8_t *p;
180077218Sphk
180177218Sphk	len = *lenp;
180277218Sphk	p = buf;
180377218Sphk	hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
180477218Sphk	if (hexstr)
180577218Sphk		val += 2;
180677218Sphk	for (;;) {
180777218Sphk		if (*val == '\0')
180877218Sphk			break;
180977218Sphk		if (sep != NULL && strchr(sep, *val) != NULL) {
181077218Sphk			val++;
181177218Sphk			break;
181277218Sphk		}
181377218Sphk		if (hexstr) {
1814127831Sphk			if (!isxdigit((u_char)val[0])) {
181577218Sphk				warnx("bad hexadecimal digits");
181677218Sphk				return NULL;
181777218Sphk			}
1818127831Sphk			if (!isxdigit((u_char)val[1])) {
1819127831Sphk				warnx("odd count hexadecimal digits");
1820127831Sphk				return NULL;
1821127831Sphk			}
182277218Sphk		}
1823127831Sphk		if (p >= buf + len) {
182477218Sphk			if (hexstr)
182577218Sphk				warnx("hexadecimal digits too long");
182677218Sphk			else
1827127831Sphk				warnx("string too long");
182877218Sphk			return NULL;
182977218Sphk		}
183077218Sphk		if (hexstr) {
183177218Sphk#define	tohex(x)	(isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
183277218Sphk			*p++ = (tohex((u_char)val[0]) << 4) |
183377218Sphk			    tohex((u_char)val[1]);
183477218Sphk#undef tohex
183577218Sphk			val += 2;
183677218Sphk		} else
183777218Sphk			*p++ = *val++;
183877218Sphk	}
183977218Sphk	len = p - buf;
184077218Sphk	/* The string "-" is treated as the empty string. */
184177218Sphk	if (!hexstr && len == 1 && buf[0] == '-')
184277218Sphk		len = 0;
184377218Sphk	if (len < *lenp)
184477218Sphk		memset(p, 0, *lenp - len);
184577218Sphk	*lenp = len;
184677218Sphk	return val;
184777218Sphk}
184877218Sphk
184977218Sphkstatic void
185077218Sphkprint_string(const u_int8_t *buf, int len)
185177218Sphk{
185277218Sphk	int i;
185377218Sphk	int hasspc;
185477218Sphk
185577218Sphk	i = 0;
185677218Sphk	hasspc = 0;
185791454Sbrooks	for (; i < len; i++) {
185877218Sphk		if (!isprint(buf[i]) && buf[i] != '\0')
185977218Sphk			break;
186077218Sphk		if (isspace(buf[i]))
186177218Sphk			hasspc++;
186277218Sphk	}
186377218Sphk	if (i == len) {
186477218Sphk		if (hasspc || len == 0 || buf[0] == '\0')
186577218Sphk			printf("\"%.*s\"", len, buf);
186677218Sphk		else
186777218Sphk			printf("%.*s", len, buf);
186877218Sphk	} else {
186977218Sphk		printf("0x");
187077218Sphk		for (i = 0; i < len; i++)
187177218Sphk			printf("%02x", buf[i]);
187277218Sphk	}
187377218Sphk}
187477218Sphk
1875138593Ssamstatic struct cmd ieee80211_cmds[] = {
1876138593Ssam	DEF_CMD_ARG("ssid",		set80211ssid),
1877138593Ssam	DEF_CMD_ARG("nwid",		set80211ssid),
1878138593Ssam	DEF_CMD_ARG("stationname",	set80211stationname),
1879138593Ssam	DEF_CMD_ARG("station",		set80211stationname),	/* BSD/OS */
1880138593Ssam	DEF_CMD_ARG("channel",		set80211channel),
1881138593Ssam	DEF_CMD_ARG("authmode",		set80211authmode),
1882138593Ssam	DEF_CMD_ARG("powersavemode",	set80211powersavemode),
1883138593Ssam	DEF_CMD("powersave",	1,	set80211powersave),
1884138593Ssam	DEF_CMD("-powersave",	0,	set80211powersave),
1885138593Ssam	DEF_CMD_ARG("powersavesleep", 	set80211powersavesleep),
1886138593Ssam	DEF_CMD_ARG("wepmode",		set80211wepmode),
1887138593Ssam	DEF_CMD("wep",		1,	set80211wep),
1888138593Ssam	DEF_CMD("-wep",		0,	set80211wep),
1889139493Ssam	DEF_CMD_ARG("deftxkey",		set80211weptxkey),
1890138593Ssam	DEF_CMD_ARG("weptxkey",		set80211weptxkey),
1891138593Ssam	DEF_CMD_ARG("wepkey",		set80211wepkey),
1892138593Ssam	DEF_CMD_ARG("nwkey",		set80211nwkey),		/* NetBSD */
1893138593Ssam	DEF_CMD("-nwkey",	0,	set80211wep),		/* NetBSD */
1894138593Ssam	DEF_CMD_ARG("rtsthreshold",	set80211rtsthreshold),
1895138593Ssam	DEF_CMD_ARG("protmode",		set80211protmode),
1896138593Ssam	DEF_CMD_ARG("txpower",		set80211txpower),
1897138593Ssam	DEF_CMD_ARG("roaming",		set80211roaming),
1898138593Ssam	DEF_CMD("wme",		1,	set80211wme),
1899138593Ssam	DEF_CMD("-wme",		0,	set80211wme),
1900138593Ssam	DEF_CMD("hidessid",	1,	set80211hidessid),
1901138593Ssam	DEF_CMD("-hidessid",	0,	set80211hidessid),
1902138593Ssam	DEF_CMD("apbridge",	1,	set80211apbridge),
1903138593Ssam	DEF_CMD("-apbridge",	0,	set80211apbridge),
1904138593Ssam	DEF_CMD_ARG("chanlist",		set80211chanlist),
1905138593Ssam	DEF_CMD_ARG("bssid",		set80211bssid),
1906138593Ssam	DEF_CMD_ARG("ap",		set80211bssid),
1907138593Ssam	DEF_CMD("scan",	0,		set80211scan),
1908138593Ssam	DEF_CMD_ARG("list",		set80211list),
1909138593Ssam	DEF_CMD_ARG2("cwmin",		set80211cwmin),
1910138593Ssam	DEF_CMD_ARG2("cwmax",		set80211cwmax),
1911138593Ssam	DEF_CMD_ARG2("aifs",		set80211aifs),
1912138593Ssam	DEF_CMD_ARG2("txoplimit",	set80211txoplimit),
1913148621Ssam	DEF_CMD_ARG("acm",		set80211acm),
1914148621Ssam	DEF_CMD_ARG("-acm",		set80211noacm),
1915148621Ssam	DEF_CMD_ARG("ack",		set80211ackpolicy),
1916148621Ssam	DEF_CMD_ARG("-ack",		set80211noackpolicy),
1917138593Ssam	DEF_CMD_ARG2("bss:cwmin",	set80211bsscwmin),
1918138593Ssam	DEF_CMD_ARG2("bss:cwmax",	set80211bsscwmax),
1919138593Ssam	DEF_CMD_ARG2("bss:aifs",	set80211bssaifs),
1920138593Ssam	DEF_CMD_ARG2("bss:txoplimit",	set80211bsstxoplimit),
1921138593Ssam	DEF_CMD_ARG("dtimperiod",	set80211dtimperiod),
1922138593Ssam	DEF_CMD_ARG("bintval",		set80211bintval),
1923138593Ssam	DEF_CMD("mac:open",	IEEE80211_MACCMD_POLICY_OPEN,	set80211maccmd),
1924138593Ssam	DEF_CMD("mac:allow",	IEEE80211_MACCMD_POLICY_ALLOW,	set80211maccmd),
1925138593Ssam	DEF_CMD("mac:deny",	IEEE80211_MACCMD_POLICY_DENY,	set80211maccmd),
1926138593Ssam	DEF_CMD("mac:flush",	IEEE80211_MACCMD_FLUSH,		set80211maccmd),
1927138593Ssam	DEF_CMD("mac:detach",	IEEE80211_MACCMD_DETACH,	set80211maccmd),
1928138593Ssam	DEF_CMD_ARG("mac:add",		set80211addmac),
1929138593Ssam	DEF_CMD_ARG("mac:del",		set80211delmac),
1930138593Ssam	DEF_CMD_ARG("mac:kick",		set80211kickmac),
1931147795Ssam	DEF_CMD("pureg",	1,	set80211pureg),
1932147795Ssam	DEF_CMD("-pureg",	0,	set80211pureg),
1933153354Ssam	DEF_CMD_ARG("mcastrate",	set80211mcastrate),
1934148416Ssam	DEF_CMD_ARG("fragthreshold",	set80211fragthreshold),
1935138593Ssam};
1936138593Ssamstatic struct afswtch af_ieee80211 = {
1937138593Ssam	.af_name	= "af_ieee80211",
1938138593Ssam	.af_af		= AF_UNSPEC,
1939139494Ssam	.af_other_status = ieee80211_status,
1940138593Ssam};
1941138593Ssam
1942138593Ssamstatic __constructor void
1943138593Ssamieee80211_ctor(void)
1944138593Ssam{
1945138593Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
1946138593Ssam	int i;
1947138593Ssam
1948138593Ssam	for (i = 0; i < N(ieee80211_cmds);  i++)
1949138593Ssam		cmd_register(&ieee80211_cmds[i]);
1950138593Ssam	af_register(&af_ieee80211);
1951138593Ssam#undef N
1952138593Ssam}
1953