ifieee80211.c revision 160687
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 160687 2006-07-26 03:08:45Z 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>
93155931Ssam#include <stdarg.h>
9477218Sphk
9577218Sphk#include "ifconfig.h"
9677218Sphk
9777218Sphkstatic void set80211(int s, int type, int val, int len, u_int8_t *data);
9877218Sphkstatic const char *get_string(const char *val, const char *sep,
9977218Sphk    u_int8_t *buf, int *lenp);
10077218Sphkstatic void print_string(const u_int8_t *buf, int len);
10177218Sphk
102138593Ssamstatic int
103138593Ssamisanyarg(const char *arg)
104138593Ssam{
105138593Ssam	return (strcmp(arg, "-") == 0 ||
106138593Ssam	    strcasecmp(arg, "any") == 0 || strcasecmp(arg, "off") == 0);
107138593Ssam}
108138593Ssam
109138593Ssamstatic void
11077218Sphkset80211ssid(const char *val, int d, int s, const struct afswtch *rafp)
11177218Sphk{
11277218Sphk	int		ssid;
11377218Sphk	int		len;
114151883Sbrooks	u_int8_t	data[IEEE80211_NWID_LEN];
11577218Sphk
11677218Sphk	ssid = 0;
117121827Sbrooks	len = strlen(val);
11888748Sambrisko	if (len > 2 && isdigit(val[0]) && val[1] == ':') {
11988748Sambrisko		ssid = atoi(val)-1;
12088748Sambrisko		val += 2;
12188748Sambrisko	}
12277218Sphk
12377218Sphk	bzero(data, sizeof(data));
12477218Sphk	len = sizeof(data);
125151883Sbrooks	if (get_string(val, NULL, data, &len) == NULL)
126151883Sbrooks		exit(1);
12777218Sphk
12877218Sphk	set80211(s, IEEE80211_IOC_SSID, ssid, len, data);
12977218Sphk}
13077218Sphk
131138593Ssamstatic void
13277218Sphkset80211stationname(const char *val, int d, int s, const struct afswtch *rafp)
13377218Sphk{
13477218Sphk	int			len;
13577218Sphk	u_int8_t		data[33];
13677218Sphk
13777218Sphk	bzero(data, sizeof(data));
13877218Sphk	len = sizeof(data);
13977218Sphk	get_string(val, NULL, data, &len);
14077218Sphk
14177218Sphk	set80211(s, IEEE80211_IOC_STATIONNAME, 0, len, data);
14277218Sphk}
14377218Sphk
144138593Ssam/*
145138593Ssam * Convert IEEE channel number to MHz frequency.
146138593Ssam */
147138593Ssamstatic u_int
148138593Ssamieee80211_ieee2mhz(u_int chan)
149138593Ssam{
150138593Ssam	if (chan == 14)
151138593Ssam		return 2484;
152138593Ssam	if (chan < 14)			/* 0-13 */
153138593Ssam		return 2407 + chan*5;
154138593Ssam	if (chan < 27)			/* 15-26 */
155138593Ssam		return 2512 + ((chan-15)*20);
156138593Ssam	return 5000 + (chan*5);
157138593Ssam}
158138593Ssam
159138593Ssam/*
160138593Ssam * Convert MHz frequency to IEEE channel number.
161138593Ssam */
162138593Ssamstatic u_int
163138593Ssamieee80211_mhz2ieee(u_int freq)
164138593Ssam{
165138593Ssam	if (freq == 2484)
166138593Ssam		return 14;
167138593Ssam	if (freq < 2484)
168138593Ssam		return (freq - 2407) / 5;
169138593Ssam	if (freq < 5000)
170138593Ssam		return 15 + ((freq - 2512) / 20);
171138593Ssam	return (freq - 5000) / 5;
172138593Ssam}
173138593Ssam
174138593Ssamstatic void
17577218Sphkset80211channel(const char *val, int d, int s, const struct afswtch *rafp)
17677218Sphk{
177138593Ssam	if (!isanyarg(val)) {
178138593Ssam		int v = atoi(val);
179138593Ssam		if (v > 255)		/* treat as frequency */
180138593Ssam			v = ieee80211_mhz2ieee(v);
181138593Ssam		set80211(s, IEEE80211_IOC_CHANNEL, v, 0, NULL);
182138593Ssam	} else
183116957Ssam		set80211(s, IEEE80211_IOC_CHANNEL, IEEE80211_CHAN_ANY, 0, NULL);
18477218Sphk}
18577218Sphk
186138593Ssamstatic void
18777218Sphkset80211authmode(const char *val, int d, int s, const struct afswtch *rafp)
18877218Sphk{
18977218Sphk	int	mode;
19077218Sphk
19191454Sbrooks	if (strcasecmp(val, "none") == 0) {
19277218Sphk		mode = IEEE80211_AUTH_NONE;
19391454Sbrooks	} else if (strcasecmp(val, "open") == 0) {
19477218Sphk		mode = IEEE80211_AUTH_OPEN;
19591454Sbrooks	} else if (strcasecmp(val, "shared") == 0) {
19677218Sphk		mode = IEEE80211_AUTH_SHARED;
197138593Ssam	} else if (strcasecmp(val, "8021x") == 0) {
198138593Ssam		mode = IEEE80211_AUTH_8021X;
199138593Ssam	} else if (strcasecmp(val, "wpa") == 0) {
200138593Ssam		mode = IEEE80211_AUTH_WPA;
20177218Sphk	} else {
202150708Sru		errx(1, "unknown authmode");
20377218Sphk	}
20477218Sphk
20577218Sphk	set80211(s, IEEE80211_IOC_AUTHMODE, mode, 0, NULL);
20677218Sphk}
20777218Sphk
208138593Ssamstatic void
20977218Sphkset80211powersavemode(const char *val, int d, int s, const struct afswtch *rafp)
21077218Sphk{
21177218Sphk	int	mode;
21277218Sphk
21391454Sbrooks	if (strcasecmp(val, "off") == 0) {
21477218Sphk		mode = IEEE80211_POWERSAVE_OFF;
21591454Sbrooks	} else if (strcasecmp(val, "on") == 0) {
21677218Sphk		mode = IEEE80211_POWERSAVE_ON;
21791454Sbrooks	} else if (strcasecmp(val, "cam") == 0) {
21877218Sphk		mode = IEEE80211_POWERSAVE_CAM;
21991454Sbrooks	} else if (strcasecmp(val, "psp") == 0) {
22077218Sphk		mode = IEEE80211_POWERSAVE_PSP;
22191454Sbrooks	} else if (strcasecmp(val, "psp-cam") == 0) {
22277218Sphk		mode = IEEE80211_POWERSAVE_PSP_CAM;
22377218Sphk	} else {
224150708Sru		errx(1, "unknown powersavemode");
22577218Sphk	}
22677218Sphk
22777218Sphk	set80211(s, IEEE80211_IOC_POWERSAVE, mode, 0, NULL);
22877218Sphk}
22977218Sphk
230138593Ssamstatic void
23177218Sphkset80211powersave(const char *val, int d, int s, const struct afswtch *rafp)
23277218Sphk{
23377218Sphk	if (d == 0)
23477218Sphk		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_OFF,
23577218Sphk		    0, NULL);
23677218Sphk	else
23777218Sphk		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_ON,
23877218Sphk		    0, NULL);
23977218Sphk}
24077218Sphk
241138593Ssamstatic void
24277218Sphkset80211powersavesleep(const char *val, int d, int s, const struct afswtch *rafp)
24377218Sphk{
24477218Sphk	set80211(s, IEEE80211_IOC_POWERSAVESLEEP, atoi(val), 0, NULL);
24577218Sphk}
24677218Sphk
247138593Ssamstatic void
24877218Sphkset80211wepmode(const char *val, int d, int s, const struct afswtch *rafp)
24977218Sphk{
25077218Sphk	int	mode;
25177218Sphk
25291454Sbrooks	if (strcasecmp(val, "off") == 0) {
25377218Sphk		mode = IEEE80211_WEP_OFF;
25491454Sbrooks	} else if (strcasecmp(val, "on") == 0) {
25577218Sphk		mode = IEEE80211_WEP_ON;
25691454Sbrooks	} else if (strcasecmp(val, "mixed") == 0) {
25777218Sphk		mode = IEEE80211_WEP_MIXED;
25877218Sphk	} else {
259150708Sru		errx(1, "unknown wep mode");
26077218Sphk	}
26177218Sphk
26277218Sphk	set80211(s, IEEE80211_IOC_WEP, mode, 0, NULL);
26377218Sphk}
26477218Sphk
265138593Ssamstatic void
26677218Sphkset80211wep(const char *val, int d, int s, const struct afswtch *rafp)
26777218Sphk{
26877218Sphk	set80211(s, IEEE80211_IOC_WEP, d, 0, NULL);
26977218Sphk}
27077218Sphk
271139493Ssamstatic int
272139493Ssamisundefarg(const char *arg)
273139493Ssam{
274139493Ssam	return (strcmp(arg, "-") == 0 || strncasecmp(arg, "undef", 5) == 0);
275139493Ssam}
276139493Ssam
277138593Ssamstatic void
27877218Sphkset80211weptxkey(const char *val, int d, int s, const struct afswtch *rafp)
27977218Sphk{
280139493Ssam	if (isundefarg(val))
281139493Ssam		set80211(s, IEEE80211_IOC_WEPTXKEY, IEEE80211_KEYIX_NONE, 0, NULL);
282139493Ssam	else
283139493Ssam		set80211(s, IEEE80211_IOC_WEPTXKEY, atoi(val)-1, 0, NULL);
28477218Sphk}
28577218Sphk
286138593Ssamstatic void
28777218Sphkset80211wepkey(const char *val, int d, int s, const struct afswtch *rafp)
28877218Sphk{
28977218Sphk	int		key = 0;
29077218Sphk	int		len;
291120178Ssam	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
29277218Sphk
29391454Sbrooks	if (isdigit(val[0]) && val[1] == ':') {
29477218Sphk		key = atoi(val)-1;
29577218Sphk		val += 2;
29677218Sphk	}
29777218Sphk
29877218Sphk	bzero(data, sizeof(data));
29977218Sphk	len = sizeof(data);
30077218Sphk	get_string(val, NULL, data, &len);
30177218Sphk
30277218Sphk	set80211(s, IEEE80211_IOC_WEPKEY, key, len, data);
30377218Sphk}
30477218Sphk
30577218Sphk/*
306148686Sstefanf * This function is purely a NetBSD compatability interface.  The NetBSD
307148686Sstefanf * interface is too inflexible, but it's there so we'll support it since
30877218Sphk * it's not all that hard.
30977218Sphk */
310138593Ssamstatic void
31177218Sphkset80211nwkey(const char *val, int d, int s, const struct afswtch *rafp)
31277218Sphk{
31377218Sphk	int		txkey;
31477218Sphk	int		i, len;
315120178Ssam	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
31677218Sphk
31777218Sphk	set80211(s, IEEE80211_IOC_WEP, IEEE80211_WEP_ON, 0, NULL);
31877218Sphk
31991454Sbrooks	if (isdigit(val[0]) && val[1] == ':') {
32077218Sphk		txkey = val[0]-'0'-1;
32177218Sphk		val += 2;
32277218Sphk
32391454Sbrooks		for (i = 0; i < 4; i++) {
32477218Sphk			bzero(data, sizeof(data));
32577218Sphk			len = sizeof(data);
32677218Sphk			val = get_string(val, ",", data, &len);
327151827Sbrooks			if (val == NULL)
328151827Sbrooks				exit(1);
32977218Sphk
33077218Sphk			set80211(s, IEEE80211_IOC_WEPKEY, i, len, data);
33177218Sphk		}
33277218Sphk	} else {
33377218Sphk		bzero(data, sizeof(data));
33477218Sphk		len = sizeof(data);
33577218Sphk		get_string(val, NULL, data, &len);
33677218Sphk		txkey = 0;
33777218Sphk
33877218Sphk		set80211(s, IEEE80211_IOC_WEPKEY, 0, len, data);
33977218Sphk
34077218Sphk		bzero(data, sizeof(data));
34191454Sbrooks		for (i = 1; i < 4; i++)
34277218Sphk			set80211(s, IEEE80211_IOC_WEPKEY, i, 0, data);
34377218Sphk	}
34477218Sphk
34577218Sphk	set80211(s, IEEE80211_IOC_WEPTXKEY, txkey, 0, NULL);
34677218Sphk}
34777218Sphk
348138593Ssamstatic void
349127649Ssamset80211rtsthreshold(const char *val, int d, int s, const struct afswtch *rafp)
350127649Ssam{
351148416Ssam	set80211(s, IEEE80211_IOC_RTSTHRESHOLD,
352148416Ssam		isundefarg(val) ? IEEE80211_RTS_MAX : atoi(val), 0, NULL);
353127649Ssam}
354127649Ssam
355138593Ssamstatic void
356127649Ssamset80211protmode(const char *val, int d, int s, const struct afswtch *rafp)
357127649Ssam{
358127649Ssam	int	mode;
359127649Ssam
360127649Ssam	if (strcasecmp(val, "off") == 0) {
361127649Ssam		mode = IEEE80211_PROTMODE_OFF;
362127649Ssam	} else if (strcasecmp(val, "cts") == 0) {
363127649Ssam		mode = IEEE80211_PROTMODE_CTS;
364127649Ssam	} else if (strcasecmp(val, "rtscts") == 0) {
365127649Ssam		mode = IEEE80211_PROTMODE_RTSCTS;
366127649Ssam	} else {
367150708Sru		errx(1, "unknown protection mode");
368127649Ssam	}
369127649Ssam
370127649Ssam	set80211(s, IEEE80211_IOC_PROTMODE, mode, 0, NULL);
371127649Ssam}
372127649Ssam
373138593Ssamstatic void
374127649Ssamset80211txpower(const char *val, int d, int s, const struct afswtch *rafp)
375127649Ssam{
376127649Ssam	set80211(s, IEEE80211_IOC_TXPOWER, atoi(val), 0, NULL);
377127649Ssam}
378127649Ssam
379138593Ssam#define	IEEE80211_ROAMING_DEVICE	0
380138593Ssam#define	IEEE80211_ROAMING_AUTO		1
381138593Ssam#define	IEEE80211_ROAMING_MANUAL	2
382138593Ssam
383138593Ssamstatic void
384138593Ssamset80211roaming(const char *val, int d, int s, const struct afswtch *rafp)
38577218Sphk{
386138593Ssam	int mode;
38777218Sphk
388138593Ssam	if (strcasecmp(val, "device") == 0) {
389138593Ssam		mode = IEEE80211_ROAMING_DEVICE;
390138593Ssam	} else if (strcasecmp(val, "auto") == 0) {
391138593Ssam		mode = IEEE80211_ROAMING_AUTO;
392138593Ssam	} else if (strcasecmp(val, "manual") == 0) {
393138593Ssam		mode = IEEE80211_ROAMING_MANUAL;
394138593Ssam	} else {
395150708Sru		errx(1, "unknown roaming mode");
396138593Ssam	}
397138593Ssam	set80211(s, IEEE80211_IOC_ROAMING, mode, 0, NULL);
398138593Ssam}
399138593Ssam
400138593Ssamstatic void
401138593Ssamset80211wme(const char *val, int d, int s, const struct afswtch *rafp)
402138593Ssam{
403138593Ssam	set80211(s, IEEE80211_IOC_WME, d, 0, NULL);
404138593Ssam}
405138593Ssam
406138593Ssamstatic void
407138593Ssamset80211hidessid(const char *val, int d, int s, const struct afswtch *rafp)
408138593Ssam{
409138593Ssam	set80211(s, IEEE80211_IOC_HIDESSID, d, 0, NULL);
410138593Ssam}
411138593Ssam
412138593Ssamstatic void
413138593Ssamset80211apbridge(const char *val, int d, int s, const struct afswtch *rafp)
414138593Ssam{
415138593Ssam	set80211(s, IEEE80211_IOC_APBRIDGE, d, 0, NULL);
416138593Ssam}
417138593Ssam
418138593Ssamstatic void
419138593Ssamset80211chanlist(const char *val, int d, int s, const struct afswtch *rafp)
420138593Ssam{
421138593Ssam	struct ieee80211req_chanlist chanlist;
422138593Ssam#define	MAXCHAN	(sizeof(chanlist.ic_channels)*NBBY)
423138593Ssam	char *temp, *cp, *tp;
424138593Ssam
425138593Ssam	temp = malloc(strlen(val) + 1);
426138593Ssam	if (temp == NULL)
427138593Ssam		errx(1, "malloc failed");
428138593Ssam	strcpy(temp, val);
429138593Ssam	memset(&chanlist, 0, sizeof(chanlist));
430138593Ssam	cp = temp;
431138593Ssam	for (;;) {
432138593Ssam		int first, last, f;
433138593Ssam
434138593Ssam		tp = strchr(cp, ',');
435138593Ssam		if (tp != NULL)
436138593Ssam			*tp++ = '\0';
437138593Ssam		switch (sscanf(cp, "%u-%u", &first, &last)) {
438138593Ssam		case 1:
439138593Ssam			if (first > MAXCHAN)
440146873Sjhb				errx(-1, "channel %u out of range, max %zu",
441138593Ssam					first, MAXCHAN);
442138593Ssam			setbit(chanlist.ic_channels, first);
443138593Ssam			break;
444138593Ssam		case 2:
445138593Ssam			if (first > MAXCHAN)
446146873Sjhb				errx(-1, "channel %u out of range, max %zu",
447138593Ssam					first, MAXCHAN);
448138593Ssam			if (last > MAXCHAN)
449146873Sjhb				errx(-1, "channel %u out of range, max %zu",
450138593Ssam					last, MAXCHAN);
451138593Ssam			if (first > last)
452138593Ssam				errx(-1, "void channel range, %u > %u",
453138593Ssam					first, last);
454138593Ssam			for (f = first; f <= last; f++)
455138593Ssam				setbit(chanlist.ic_channels, f);
456138593Ssam			break;
457138593Ssam		}
458138593Ssam		if (tp == NULL)
459138593Ssam			break;
460138593Ssam		while (isspace(*tp))
461138593Ssam			tp++;
462138593Ssam		if (!isdigit(*tp))
463138593Ssam			break;
464138593Ssam		cp = tp;
465138593Ssam	}
466138593Ssam	set80211(s, IEEE80211_IOC_CHANLIST, 0,
467138593Ssam		sizeof(chanlist), (uint8_t *) &chanlist);
468138593Ssam#undef MAXCHAN
469138593Ssam}
470138593Ssam
471138593Ssamstatic void
472138593Ssamset80211bssid(const char *val, int d, int s, const struct afswtch *rafp)
473138593Ssam{
474138593Ssam
475138593Ssam	if (!isanyarg(val)) {
476138593Ssam		char *temp;
477138593Ssam		struct sockaddr_dl sdl;
478138593Ssam
479155702Ssam		temp = malloc(strlen(val) + 2); /* ':' and '\0' */
480138593Ssam		if (temp == NULL)
481138593Ssam			errx(1, "malloc failed");
482138593Ssam		temp[0] = ':';
483138593Ssam		strcpy(temp + 1, val);
484138593Ssam		sdl.sdl_len = sizeof(sdl);
485138593Ssam		link_addr(temp, &sdl);
486138593Ssam		free(temp);
487138593Ssam		if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
488138593Ssam			errx(1, "malformed link-level address");
489138593Ssam		set80211(s, IEEE80211_IOC_BSSID, 0,
490138593Ssam			IEEE80211_ADDR_LEN, LLADDR(&sdl));
491138593Ssam	} else {
492138593Ssam		uint8_t zerobssid[IEEE80211_ADDR_LEN];
493138593Ssam		memset(zerobssid, 0, sizeof(zerobssid));
494138593Ssam		set80211(s, IEEE80211_IOC_BSSID, 0,
495138593Ssam			IEEE80211_ADDR_LEN, zerobssid);
496138593Ssam	}
497138593Ssam}
498138593Ssam
499138593Ssamstatic int
500138593Ssamgetac(const char *ac)
501138593Ssam{
502138593Ssam	if (strcasecmp(ac, "ac_be") == 0 || strcasecmp(ac, "be") == 0)
503138593Ssam		return WME_AC_BE;
504138593Ssam	if (strcasecmp(ac, "ac_bk") == 0 || strcasecmp(ac, "bk") == 0)
505138593Ssam		return WME_AC_BK;
506138593Ssam	if (strcasecmp(ac, "ac_vi") == 0 || strcasecmp(ac, "vi") == 0)
507138593Ssam		return WME_AC_VI;
508138593Ssam	if (strcasecmp(ac, "ac_vo") == 0 || strcasecmp(ac, "vo") == 0)
509138593Ssam		return WME_AC_VO;
510138593Ssam	errx(1, "unknown wme access class %s", ac);
511138593Ssam}
512138593Ssam
513138593Ssamstatic
514138593SsamDECL_CMD_FUNC2(set80211cwmin, ac, val)
515138593Ssam{
516138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val), getac(ac), NULL);
517138593Ssam}
518138593Ssam
519138593Ssamstatic
520138593SsamDECL_CMD_FUNC2(set80211cwmax, ac, val)
521138593Ssam{
522138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val), getac(ac), NULL);
523138593Ssam}
524138593Ssam
525138593Ssamstatic
526138593SsamDECL_CMD_FUNC2(set80211aifs, ac, val)
527138593Ssam{
528138593Ssam	set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val), getac(ac), NULL);
529138593Ssam}
530138593Ssam
531138593Ssamstatic
532138593SsamDECL_CMD_FUNC2(set80211txoplimit, ac, val)
533138593Ssam{
534138593Ssam	set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val), getac(ac), NULL);
535138593Ssam}
536138593Ssam
537138593Ssamstatic
538148621SsamDECL_CMD_FUNC(set80211acm, ac, d)
539138593Ssam{
540148621Ssam	set80211(s, IEEE80211_IOC_WME_ACM, 1, getac(ac), NULL);
541138593Ssam}
542148621Ssamstatic
543148621SsamDECL_CMD_FUNC(set80211noacm, ac, d)
544148621Ssam{
545148621Ssam	set80211(s, IEEE80211_IOC_WME_ACM, 0, getac(ac), NULL);
546148621Ssam}
547138593Ssam
548138593Ssamstatic
549148621SsamDECL_CMD_FUNC(set80211ackpolicy, ac, d)
550138593Ssam{
551148621Ssam	set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 1, getac(ac), NULL);
552138593Ssam}
553148621Ssamstatic
554148621SsamDECL_CMD_FUNC(set80211noackpolicy, ac, d)
555148621Ssam{
556148621Ssam	set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 0, getac(ac), NULL);
557148621Ssam}
558138593Ssam
559138593Ssamstatic
560138593SsamDECL_CMD_FUNC2(set80211bsscwmin, ac, val)
561138593Ssam{
562138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val),
563138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
564138593Ssam}
565138593Ssam
566138593Ssamstatic
567138593SsamDECL_CMD_FUNC2(set80211bsscwmax, ac, val)
568138593Ssam{
569138593Ssam	set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val),
570138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
571138593Ssam}
572138593Ssam
573138593Ssamstatic
574138593SsamDECL_CMD_FUNC2(set80211bssaifs, ac, val)
575138593Ssam{
576138593Ssam	set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val),
577138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
578138593Ssam}
579138593Ssam
580138593Ssamstatic
581138593SsamDECL_CMD_FUNC2(set80211bsstxoplimit, ac, val)
582138593Ssam{
583138593Ssam	set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val),
584138593Ssam		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
585138593Ssam}
586138593Ssam
587138593Ssamstatic
588138593SsamDECL_CMD_FUNC(set80211dtimperiod, val, d)
589138593Ssam{
590138593Ssam	set80211(s, IEEE80211_IOC_DTIM_PERIOD, atoi(val), 0, NULL);
591138593Ssam}
592138593Ssam
593138593Ssamstatic
594138593SsamDECL_CMD_FUNC(set80211bintval, val, d)
595138593Ssam{
596138593Ssam	set80211(s, IEEE80211_IOC_BEACON_INTERVAL, atoi(val), 0, NULL);
597138593Ssam}
598138593Ssam
599138593Ssamstatic void
600138593Ssamset80211macmac(int s, int op, const char *val)
601138593Ssam{
602138593Ssam	char *temp;
603138593Ssam	struct sockaddr_dl sdl;
604138593Ssam
605155702Ssam	temp = malloc(strlen(val) + 2); /* ':' and '\0' */
606138593Ssam	if (temp == NULL)
607138593Ssam		errx(1, "malloc failed");
608138593Ssam	temp[0] = ':';
609138593Ssam	strcpy(temp + 1, val);
610138593Ssam	sdl.sdl_len = sizeof(sdl);
611138593Ssam	link_addr(temp, &sdl);
612138593Ssam	free(temp);
613138593Ssam	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
614138593Ssam		errx(1, "malformed link-level address");
615138593Ssam	set80211(s, op, 0, IEEE80211_ADDR_LEN, LLADDR(&sdl));
616138593Ssam}
617138593Ssam
618138593Ssamstatic
619138593SsamDECL_CMD_FUNC(set80211addmac, val, d)
620138593Ssam{
621138593Ssam	set80211macmac(s, IEEE80211_IOC_ADDMAC, val);
622138593Ssam}
623138593Ssam
624138593Ssamstatic
625138593SsamDECL_CMD_FUNC(set80211delmac, val, d)
626138593Ssam{
627138593Ssam	set80211macmac(s, IEEE80211_IOC_DELMAC, val);
628138593Ssam}
629138593Ssam
630138593Ssamstatic
631149029SsamDECL_CMD_FUNC(set80211kickmac, val, d)
632149029Ssam{
633149029Ssam	char *temp;
634149029Ssam	struct sockaddr_dl sdl;
635149029Ssam	struct ieee80211req_mlme mlme;
636149029Ssam
637155702Ssam	temp = malloc(strlen(val) + 2); /* ':' and '\0' */
638149029Ssam	if (temp == NULL)
639149029Ssam		errx(1, "malloc failed");
640149029Ssam	temp[0] = ':';
641149029Ssam	strcpy(temp + 1, val);
642149029Ssam	sdl.sdl_len = sizeof(sdl);
643149029Ssam	link_addr(temp, &sdl);
644149029Ssam	free(temp);
645149029Ssam	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
646149029Ssam		errx(1, "malformed link-level address");
647149029Ssam	memset(&mlme, 0, sizeof(mlme));
648149029Ssam	mlme.im_op = IEEE80211_MLME_DEAUTH;
649149029Ssam	mlme.im_reason = IEEE80211_REASON_AUTH_EXPIRE;
650149029Ssam	memcpy(mlme.im_macaddr, LLADDR(&sdl), IEEE80211_ADDR_LEN);
651149029Ssam	set80211(s, IEEE80211_IOC_MLME, 0, sizeof(mlme), (u_int8_t *) &mlme);
652149029Ssam}
653149029Ssam
654149029Ssamstatic
655138593SsamDECL_CMD_FUNC(set80211maccmd, val, d)
656138593Ssam{
657138593Ssam	set80211(s, IEEE80211_IOC_MACCMD, d, 0, NULL);
658138593Ssam}
659138593Ssam
660147795Ssamstatic void
661147795Ssamset80211pureg(const char *val, int d, int s, const struct afswtch *rafp)
662147795Ssam{
663147795Ssam	set80211(s, IEEE80211_IOC_PUREG, d, 0, NULL);
664147795Ssam}
665147795Ssam
666153422Ssamstatic void
667153422Ssamset80211burst(const char *val, int d, int s, const struct afswtch *rafp)
668153422Ssam{
669153422Ssam	set80211(s, IEEE80211_IOC_BURST, d, 0, NULL);
670153422Ssam}
671153422Ssam
672148416Ssamstatic
673153354SsamDECL_CMD_FUNC(set80211mcastrate, val, d)
674153354Ssam{
675153354Ssam	set80211(s, IEEE80211_IOC_MCAST_RATE, (int) 2*atof(val), 0, NULL);
676153354Ssam}
677153354Ssam
678153354Ssamstatic
679148416SsamDECL_CMD_FUNC(set80211fragthreshold, val, d)
680148416Ssam{
681148416Ssam	set80211(s, IEEE80211_IOC_FRAGTHRESHOLD,
682148416Ssam		isundefarg(val) ? IEEE80211_FRAG_MAX : atoi(val), 0, NULL);
683148416Ssam}
684148416Ssam
685160687Ssamstatic
686160687SsamDECL_CMD_FUNC(set80211bmissthreshold, val, d)
687160687Ssam{
688160687Ssam	set80211(s, IEEE80211_IOC_BMISSTHRESHOLD,
689160687Ssam		isundefarg(val) ? IEEE80211_HWBMISS_MAX : atoi(val), 0, NULL);
690160687Ssam}
691160687Ssam
692138593Ssamstatic int
693138593Ssamgetmaxrate(uint8_t rates[15], uint8_t nrates)
694138593Ssam{
695138593Ssam	int i, maxrate = -1;
696138593Ssam
697138593Ssam	for (i = 0; i < nrates; i++) {
698138593Ssam		int rate = rates[i] & IEEE80211_RATE_VAL;
699138593Ssam		if (rate > maxrate)
700138593Ssam			maxrate = rate;
701138593Ssam	}
702138593Ssam	return maxrate / 2;
703138593Ssam}
704138593Ssam
705138593Ssamstatic const char *
706138593Ssamgetcaps(int capinfo)
707138593Ssam{
708138593Ssam	static char capstring[32];
709138593Ssam	char *cp = capstring;
710138593Ssam
711138593Ssam	if (capinfo & IEEE80211_CAPINFO_ESS)
712138593Ssam		*cp++ = 'E';
713138593Ssam	if (capinfo & IEEE80211_CAPINFO_IBSS)
714138593Ssam		*cp++ = 'I';
715138593Ssam	if (capinfo & IEEE80211_CAPINFO_CF_POLLABLE)
716138593Ssam		*cp++ = 'c';
717138593Ssam	if (capinfo & IEEE80211_CAPINFO_CF_POLLREQ)
718138593Ssam		*cp++ = 'C';
719138593Ssam	if (capinfo & IEEE80211_CAPINFO_PRIVACY)
720138593Ssam		*cp++ = 'P';
721138593Ssam	if (capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)
722138593Ssam		*cp++ = 'S';
723138593Ssam	if (capinfo & IEEE80211_CAPINFO_PBCC)
724138593Ssam		*cp++ = 'B';
725138593Ssam	if (capinfo & IEEE80211_CAPINFO_CHNL_AGILITY)
726138593Ssam		*cp++ = 'A';
727138593Ssam	if (capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
728138593Ssam		*cp++ = 's';
729138593Ssam	if (capinfo & IEEE80211_CAPINFO_RSN)
730138593Ssam		*cp++ = 'R';
731138593Ssam	if (capinfo & IEEE80211_CAPINFO_DSSSOFDM)
732138593Ssam		*cp++ = 'D';
733138593Ssam	*cp = '\0';
734138593Ssam	return capstring;
735138593Ssam}
736138593Ssam
737159885Ssamstatic const char *
738159885Ssamgetflags(int flags)
739159885Ssam{
740159885Ssam/* XXX need these publicly defined or similar */
741159885Ssam#define	IEEE80211_NODE_AUTH	0x0001		/* authorized for data */
742159885Ssam#define	IEEE80211_NODE_QOS	0x0002		/* QoS enabled */
743159885Ssam#define	IEEE80211_NODE_ERP	0x0004		/* ERP enabled */
744159885Ssam#define	IEEE80211_NODE_PWR_MGT	0x0010		/* power save mode enabled */
745159885Ssam	static char flagstring[32];
746159885Ssam	char *cp = flagstring;
747159885Ssam
748159885Ssam	if (flags & IEEE80211_NODE_AUTH)
749159885Ssam		*cp++ = 'A';
750159885Ssam	if (flags & IEEE80211_NODE_QOS)
751159885Ssam		*cp++ = 'Q';
752159885Ssam	if (flags & IEEE80211_NODE_ERP)
753159885Ssam		*cp++ = 'E';
754159885Ssam	if (flags & IEEE80211_NODE_PWR_MGT)
755159885Ssam		*cp++ = 'P';
756159885Ssam	*cp = '\0';
757159885Ssam	return flagstring;
758159885Ssam#undef IEEE80211_NODE_AUTH
759159885Ssam#undef IEEE80211_NODE_QOS
760159885Ssam#undef IEEE80211_NODE_ERP
761159885Ssam#undef IEEE80211_NODE_PWR_MGT
762159885Ssam}
763159885Ssam
764138593Ssamstatic void
765138593Ssamprintie(const char* tag, const uint8_t *ie, size_t ielen, int maxlen)
766138593Ssam{
767138593Ssam	printf("%s", tag);
768138593Ssam	if (verbose) {
769138593Ssam		maxlen -= strlen(tag)+2;
770138593Ssam		if (2*ielen > maxlen)
771138593Ssam			maxlen--;
772138593Ssam		printf("<");
773138593Ssam		for (; ielen > 0; ie++, ielen--) {
774138593Ssam			if (maxlen-- <= 0)
775138593Ssam				break;
776138593Ssam			printf("%02x", *ie);
777138593Ssam		}
778138593Ssam		if (ielen != 0)
779138593Ssam			printf("-");
780138593Ssam		printf(">");
781138593Ssam	}
782138593Ssam}
783138593Ssam
784138593Ssam/*
785138593Ssam * Copy the ssid string contents into buf, truncating to fit.  If the
786138593Ssam * ssid is entirely printable then just copy intact.  Otherwise convert
787138593Ssam * to hexadecimal.  If the result is truncated then replace the last
788138593Ssam * three characters with "...".
789138593Ssam */
790146873Sjhbstatic int
791138593Ssamcopy_essid(char buf[], size_t bufsize, const u_int8_t *essid, size_t essid_len)
792138593Ssam{
793138593Ssam	const u_int8_t *p;
794138593Ssam	size_t maxlen;
795138593Ssam	int i;
796138593Ssam
797138593Ssam	if (essid_len > bufsize)
798138593Ssam		maxlen = bufsize;
799138593Ssam	else
800138593Ssam		maxlen = essid_len;
801138593Ssam	/* determine printable or not */
802138593Ssam	for (i = 0, p = essid; i < maxlen; i++, p++) {
803138593Ssam		if (*p < ' ' || *p > 0x7e)
804138593Ssam			break;
805138593Ssam	}
806138593Ssam	if (i != maxlen) {		/* not printable, print as hex */
807138593Ssam		if (bufsize < 3)
808138593Ssam			return 0;
809138593Ssam		strlcpy(buf, "0x", bufsize);
810138593Ssam		bufsize -= 2;
811138593Ssam		p = essid;
812138593Ssam		for (i = 0; i < maxlen && bufsize >= 2; i++) {
813147489Savatar			sprintf(&buf[2+2*i], "%02x", p[i]);
814138593Ssam			bufsize -= 2;
815138593Ssam		}
816147489Savatar		if (i != essid_len)
817147489Savatar			memcpy(&buf[2+2*i-3], "...", 3);
818138593Ssam	} else {			/* printable, truncate as needed */
819138593Ssam		memcpy(buf, essid, maxlen);
820147489Savatar		if (maxlen != essid_len)
821147489Savatar			memcpy(&buf[maxlen-3], "...", 3);
822138593Ssam	}
823138593Ssam	return maxlen;
824138593Ssam}
825138593Ssam
826148686Sstefanf/* unaligned little endian access */
827138593Ssam#define LE_READ_4(p)					\
828138593Ssam	((u_int32_t)					\
829138593Ssam	 ((((const u_int8_t *)(p))[0]      ) |		\
830138593Ssam	  (((const u_int8_t *)(p))[1] <<  8) |		\
831138593Ssam	  (((const u_int8_t *)(p))[2] << 16) |		\
832138593Ssam	  (((const u_int8_t *)(p))[3] << 24)))
833138593Ssam
834138593Ssamstatic int __inline
835138593Ssamiswpaoui(const u_int8_t *frm)
836138593Ssam{
837138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
838138593Ssam}
839138593Ssam
840138593Ssamstatic int __inline
841138593Ssamiswmeoui(const u_int8_t *frm)
842138593Ssam{
843138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI);
844138593Ssam}
845138593Ssam
846138593Ssamstatic int __inline
847138593Ssamisatherosoui(const u_int8_t *frm)
848138593Ssam{
849138593Ssam	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
850138593Ssam}
851138593Ssam
852138593Ssamstatic void
853138593Ssamprinties(const u_int8_t *vp, int ielen, int maxcols)
854138593Ssam{
855138593Ssam	while (ielen > 0) {
856138593Ssam		switch (vp[0]) {
857138593Ssam		case IEEE80211_ELEMID_VENDOR:
858138593Ssam			if (iswpaoui(vp))
859138593Ssam				printie(" WPA", vp, 2+vp[1], maxcols);
860138593Ssam			else if (iswmeoui(vp))
861138593Ssam				printie(" WME", vp, 2+vp[1], maxcols);
862139492Ssam			else if (isatherosoui(vp))
863139492Ssam				printie(" ATH", vp, 2+vp[1], maxcols);
864138593Ssam			else
865138593Ssam				printie(" VEN", vp, 2+vp[1], maxcols);
866138593Ssam			break;
867138593Ssam		case IEEE80211_ELEMID_RSN:
868138593Ssam			printie(" RSN", vp, 2+vp[1], maxcols);
869138593Ssam			break;
870138593Ssam		default:
871138593Ssam			printie(" ???", vp, 2+vp[1], maxcols);
872138593Ssam			break;
873138593Ssam		}
874138593Ssam		ielen -= 2+vp[1];
875138593Ssam		vp += 2+vp[1];
876138593Ssam	}
877138593Ssam}
878138593Ssam
879138593Ssamstatic void
880138593Ssamlist_scan(int s)
881138593Ssam{
882138593Ssam	uint8_t buf[24*1024];
883138593Ssam	struct ieee80211req ireq;
884153892Srwatson	char ssid[IEEE80211_NWID_LEN+1];
885138593Ssam	uint8_t *cp;
886154522Ssam	int len, ssidmax;
887138593Ssam
88877218Sphk	(void) memset(&ireq, 0, sizeof(ireq));
88977218Sphk	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
890138593Ssam	ireq.i_type = IEEE80211_IOC_SCAN_RESULTS;
891138593Ssam	ireq.i_data = buf;
892138593Ssam	ireq.i_len = sizeof(buf);
893138593Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
894138593Ssam		errx(1, "unable to get scan results");
895138593Ssam	len = ireq.i_len;
896138593Ssam	if (len < sizeof(struct ieee80211req_scan_result))
897138593Ssam		return;
898138593Ssam
899154522Ssam	ssidmax = verbose ? IEEE80211_NWID_LEN : 14;
900154522Ssam	printf("%-*.*s  %-17.17s  %4s %4s  %-5s %3s %4s\n"
901154522Ssam		, ssidmax, ssidmax, "SSID"
902138593Ssam		, "BSSID"
903138593Ssam		, "CHAN"
904138593Ssam		, "RATE"
905138593Ssam		, "S:N"
906138593Ssam		, "INT"
907138593Ssam		, "CAPS"
908138593Ssam	);
909138593Ssam	cp = buf;
910138593Ssam	do {
911138593Ssam		struct ieee80211req_scan_result *sr;
912138593Ssam		uint8_t *vp;
913138593Ssam
914138593Ssam		sr = (struct ieee80211req_scan_result *) cp;
915138593Ssam		vp = (u_int8_t *)(sr+1);
916154522Ssam		printf("%-*.*s  %s  %3d  %3dM %2d:%-2d  %3d %-4.4s"
917154522Ssam			, ssidmax
918155461Ssam			  , copy_essid(ssid, ssidmax, vp, sr->isr_ssid_len)
919154522Ssam			  , ssid
920138593Ssam			, ether_ntoa((const struct ether_addr *) sr->isr_bssid)
921138593Ssam			, ieee80211_mhz2ieee(sr->isr_freq)
922138593Ssam			, getmaxrate(sr->isr_rates, sr->isr_nrates)
923138593Ssam			, sr->isr_rssi, sr->isr_noise
924138593Ssam			, sr->isr_intval
925138593Ssam			, getcaps(sr->isr_capinfo)
926138593Ssam		);
927138593Ssam		printies(vp + sr->isr_ssid_len, sr->isr_ie_len, 24);;
928138593Ssam		printf("\n");
929138593Ssam		cp += sr->isr_len, len -= sr->isr_len;
930138593Ssam	} while (len >= sizeof(struct ieee80211req_scan_result));
931138593Ssam}
932138593Ssam
933138593Ssam#include <net80211/ieee80211_freebsd.h>
934138593Ssam
935138593Ssamstatic void
936138593Ssamscan_and_wait(int s)
937138593Ssam{
938138593Ssam	struct ieee80211req ireq;
939138593Ssam	int sroute;
940138593Ssam
941138593Ssam	sroute = socket(PF_ROUTE, SOCK_RAW, 0);
942138593Ssam	if (sroute < 0) {
943138593Ssam		perror("socket(PF_ROUTE,SOCK_RAW)");
944138593Ssam		return;
945138593Ssam	}
946138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
947138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
948138593Ssam	ireq.i_type = IEEE80211_IOC_SCAN_REQ;
949138593Ssam	/* NB: only root can trigger a scan so ignore errors */
950138593Ssam	if (ioctl(s, SIOCS80211, &ireq) >= 0) {
951138593Ssam		char buf[2048];
952138593Ssam		struct if_announcemsghdr *ifan;
953138593Ssam		struct rt_msghdr *rtm;
954138593Ssam
955138593Ssam		do {
956138593Ssam			if (read(sroute, buf, sizeof(buf)) < 0) {
957138593Ssam				perror("read(PF_ROUTE)");
958138593Ssam				break;
959138593Ssam			}
960138593Ssam			rtm = (struct rt_msghdr *) buf;
961138593Ssam			if (rtm->rtm_version != RTM_VERSION)
962138593Ssam				break;
963138593Ssam			ifan = (struct if_announcemsghdr *) rtm;
964138593Ssam		} while (rtm->rtm_type != RTM_IEEE80211 ||
965138593Ssam		    ifan->ifan_what != RTM_IEEE80211_SCAN);
966138593Ssam	}
967138593Ssam	close(sroute);
968138593Ssam}
969138593Ssam
970138593Ssamstatic
971138593SsamDECL_CMD_FUNC(set80211scan, val, d)
972138593Ssam{
973138593Ssam	scan_and_wait(s);
974138593Ssam	list_scan(s);
975138593Ssam}
976138593Ssam
977138593Ssamstatic void
978138593Ssamlist_stations(int s)
979138593Ssam{
980138593Ssam	uint8_t buf[24*1024];
981138593Ssam	struct ieee80211req ireq;
982138593Ssam	uint8_t *cp;
983138593Ssam	int len;
984138593Ssam
985138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
986138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
987138593Ssam	ireq.i_type = IEEE80211_IOC_STA_INFO;
988138593Ssam	ireq.i_data = buf;
989138593Ssam	ireq.i_len = sizeof(buf);
990138593Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
991138593Ssam		errx(1, "unable to get station information");
992138593Ssam	len = ireq.i_len;
993138593Ssam	if (len < sizeof(struct ieee80211req_sta_info))
994138593Ssam		return;
995138593Ssam
996159885Ssam	printf("%-17.17s %4s %4s %4s %4s %4s %6s %6s %4s %4s\n"
997138593Ssam		, "ADDR"
998138593Ssam		, "AID"
999138593Ssam		, "CHAN"
1000138593Ssam		, "RATE"
1001138593Ssam		, "RSSI"
1002138593Ssam		, "IDLE"
1003138593Ssam		, "TXSEQ"
1004138593Ssam		, "RXSEQ"
1005138593Ssam		, "CAPS"
1006159885Ssam		, "FLAG"
1007138593Ssam	);
1008138593Ssam	cp = buf;
1009138593Ssam	do {
1010138593Ssam		struct ieee80211req_sta_info *si;
1011138593Ssam		uint8_t *vp;
1012138593Ssam
1013138593Ssam		si = (struct ieee80211req_sta_info *) cp;
1014138593Ssam		vp = (u_int8_t *)(si+1);
1015159885Ssam		printf("%s %4u %4d %3dM %4d %4d %6d %6d %-4.4s %-4.4s"
1016138593Ssam			, ether_ntoa((const struct ether_addr*) si->isi_macaddr)
1017138593Ssam			, IEEE80211_AID(si->isi_associd)
1018138593Ssam			, ieee80211_mhz2ieee(si->isi_freq)
1019138593Ssam			, (si->isi_rates[si->isi_txrate] & IEEE80211_RATE_VAL)/2
1020138593Ssam			, si->isi_rssi
1021138593Ssam			, si->isi_inact
1022138593Ssam			, si->isi_txseqs[0]
1023138593Ssam			, si->isi_rxseqs[0]
1024138593Ssam			, getcaps(si->isi_capinfo)
1025159885Ssam			, getflags(si->isi_state)
1026138593Ssam		);
1027138593Ssam		printies(vp, si->isi_ie_len, 24);
1028138593Ssam		printf("\n");
1029138593Ssam		cp += si->isi_len, len -= si->isi_len;
1030138593Ssam	} while (len >= sizeof(struct ieee80211req_sta_info));
1031138593Ssam}
1032138593Ssam
1033138593Ssamstatic void
1034138593Ssamprint_chaninfo(const struct ieee80211_channel *c)
1035138593Ssam{
1036138593Ssam#define	IEEE80211_IS_CHAN_PASSIVE(_c) \
1037138593Ssam	(((_c)->ic_flags & IEEE80211_CHAN_PASSIVE))
1038138593Ssam	char buf[14];
1039138593Ssam
1040138593Ssam	buf[0] = '\0';
1041138593Ssam	if (IEEE80211_IS_CHAN_FHSS(c))
1042138593Ssam		strlcat(buf, " FHSS", sizeof(buf));
1043138593Ssam	if (IEEE80211_IS_CHAN_A(c))
1044138593Ssam		strlcat(buf, " 11a", sizeof(buf));
1045138593Ssam	/* XXX 11g schizophrenia */
1046138593Ssam	if (IEEE80211_IS_CHAN_G(c) ||
1047138593Ssam	    IEEE80211_IS_CHAN_PUREG(c))
1048138593Ssam		strlcat(buf, " 11g", sizeof(buf));
1049138593Ssam	else if (IEEE80211_IS_CHAN_B(c))
1050138593Ssam		strlcat(buf, " 11b", sizeof(buf));
1051138593Ssam	if (IEEE80211_IS_CHAN_T(c))
1052138593Ssam		strlcat(buf, " Turbo", sizeof(buf));
1053138593Ssam	printf("Channel %3u : %u%c Mhz%-14.14s",
1054138593Ssam		ieee80211_mhz2ieee(c->ic_freq), c->ic_freq,
1055138593Ssam		IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ', buf);
1056138593Ssam#undef IEEE80211_IS_CHAN_PASSIVE
1057138593Ssam}
1058138593Ssam
1059138593Ssamstatic void
1060138593Ssamlist_channels(int s, int allchans)
1061138593Ssam{
1062138593Ssam	struct ieee80211req ireq;
1063138593Ssam	struct ieee80211req_chaninfo chans;
1064138593Ssam	struct ieee80211req_chaninfo achans;
1065138593Ssam	const struct ieee80211_channel *c;
1066138593Ssam	int i, half;
1067138593Ssam
1068138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
1069138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
1070138593Ssam	ireq.i_type = IEEE80211_IOC_CHANINFO;
1071138593Ssam	ireq.i_data = &chans;
1072138593Ssam	ireq.i_len = sizeof(chans);
1073138593Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
1074138593Ssam		errx(1, "unable to get channel information");
1075138593Ssam	if (!allchans) {
1076138593Ssam		struct ieee80211req_chanlist active;
1077138593Ssam
1078138593Ssam		ireq.i_type = IEEE80211_IOC_CHANLIST;
1079138593Ssam		ireq.i_data = &active;
1080138593Ssam		ireq.i_len = sizeof(active);
1081138593Ssam		if (ioctl(s, SIOCG80211, &ireq) < 0)
1082138593Ssam			errx(1, "unable to get active channel list");
1083138593Ssam		memset(&achans, 0, sizeof(achans));
1084138593Ssam		for (i = 0; i < chans.ic_nchans; i++) {
1085138593Ssam			c = &chans.ic_chans[i];
1086138593Ssam			if (isset(active.ic_channels, ieee80211_mhz2ieee(c->ic_freq)) || allchans)
1087138593Ssam				achans.ic_chans[achans.ic_nchans++] = *c;
1088138593Ssam		}
1089138593Ssam	} else
1090138593Ssam		achans = chans;
1091138593Ssam	half = achans.ic_nchans / 2;
1092138593Ssam	if (achans.ic_nchans % 2)
1093138593Ssam		half++;
1094138593Ssam	for (i = 0; i < achans.ic_nchans / 2; i++) {
1095138593Ssam		print_chaninfo(&achans.ic_chans[i]);
1096138593Ssam		print_chaninfo(&achans.ic_chans[half+i]);
1097138593Ssam		printf("\n");
1098138593Ssam	}
1099138593Ssam	if (achans.ic_nchans % 2) {
1100138593Ssam		print_chaninfo(&achans.ic_chans[i]);
1101138593Ssam		printf("\n");
1102138593Ssam	}
1103138593Ssam}
1104138593Ssam
1105138593Ssamstatic void
1106138593Ssamlist_keys(int s)
1107138593Ssam{
1108138593Ssam}
1109138593Ssam
1110138593Ssam#define	IEEE80211_C_BITS \
1111138593Ssam"\020\1WEP\2TKIP\3AES\4AES_CCM\6CKIP\11IBSS\12PMGT\13HOSTAP\14AHDEMO" \
1112138593Ssam"\15SWRETRY\16TXPMGT\17SHSLOT\20SHPREAMBLE\21MONITOR\22TKIPMIC\30WPA1" \
1113138593Ssam"\31WPA2\32BURST\33WME"
1114138593Ssam
1115138593Ssamstatic void
1116138593Ssamlist_capabilities(int s)
1117138593Ssam{
1118138593Ssam	struct ieee80211req ireq;
1119138593Ssam	u_int32_t caps;
1120138593Ssam
1121138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
1122138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
1123138593Ssam	ireq.i_type = IEEE80211_IOC_DRIVER_CAPS;
1124138593Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
1125138593Ssam		errx(1, "unable to get driver capabilities");
1126138593Ssam	caps = (((u_int16_t) ireq.i_val) << 16) | ((u_int16_t) ireq.i_len);
1127138593Ssam	printb(name, caps, IEEE80211_C_BITS);
1128138593Ssam	putchar('\n');
1129138593Ssam}
1130138593Ssam
1131138593Ssamstatic void
1132138593Ssamlist_wme(int s)
1133138593Ssam{
1134138593Ssam	static const char *acnames[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" };
1135138593Ssam	struct ieee80211req ireq;
1136138593Ssam	int ac;
1137138593Ssam
1138138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
1139138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
1140138593Ssam	ireq.i_len = 0;
1141138593Ssam	for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++) {
1142138593Ssamagain:
1143138593Ssam		if (ireq.i_len & IEEE80211_WMEPARAM_BSS)
1144138593Ssam			printf("\t%s", "     ");
1145138593Ssam		else
1146138593Ssam			printf("\t%s", acnames[ac]);
1147138593Ssam
1148138593Ssam		ireq.i_len = (ireq.i_len & IEEE80211_WMEPARAM_BSS) | ac;
1149138593Ssam
1150138593Ssam		/* show WME BSS parameters */
1151138593Ssam		ireq.i_type = IEEE80211_IOC_WME_CWMIN;
1152138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1)
1153138593Ssam			printf(" cwmin %2u", ireq.i_val);
1154138593Ssam		ireq.i_type = IEEE80211_IOC_WME_CWMAX;
1155138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1)
1156138593Ssam			printf(" cwmax %2u", ireq.i_val);
1157138593Ssam		ireq.i_type = IEEE80211_IOC_WME_AIFS;
1158138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1)
1159138593Ssam			printf(" aifs %2u", ireq.i_val);
1160138593Ssam		ireq.i_type = IEEE80211_IOC_WME_TXOPLIMIT;
1161138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1)
1162138593Ssam			printf(" txopLimit %3u", ireq.i_val);
1163138593Ssam		ireq.i_type = IEEE80211_IOC_WME_ACM;
1164138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1165138593Ssam			if (ireq.i_val)
1166138593Ssam				printf(" acm");
1167138593Ssam			else if (verbose)
1168138593Ssam				printf(" -acm");
1169138593Ssam		}
1170138593Ssam		/* !BSS only */
1171138593Ssam		if ((ireq.i_len & IEEE80211_WMEPARAM_BSS) == 0) {
1172138593Ssam			ireq.i_type = IEEE80211_IOC_WME_ACKPOLICY;
1173138593Ssam			if (ioctl(s, SIOCG80211, &ireq) != -1) {
1174138593Ssam				if (!ireq.i_val)
1175138593Ssam					printf(" -ack");
1176138593Ssam				else if (verbose)
1177138593Ssam					printf(" ack");
1178138593Ssam			}
1179138593Ssam		}
1180138593Ssam		printf("\n");
1181138593Ssam		if ((ireq.i_len & IEEE80211_WMEPARAM_BSS) == 0) {
1182138593Ssam			ireq.i_len |= IEEE80211_WMEPARAM_BSS;
1183138593Ssam			goto again;
1184138593Ssam		} else
1185138593Ssam			ireq.i_len &= ~IEEE80211_WMEPARAM_BSS;
1186138593Ssam	}
1187138593Ssam}
1188138593Ssam
1189149029Ssamstatic void
1190149029Ssamlist_mac(int s)
1191149029Ssam{
1192149029Ssam	struct ieee80211req ireq;
1193149029Ssam	struct ieee80211req_maclist *acllist;
1194149029Ssam	int i, nacls, policy;
1195149029Ssam	char c;
1196149029Ssam
1197149029Ssam	(void) memset(&ireq, 0, sizeof(ireq));
1198149029Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); /* XXX ?? */
1199149029Ssam	ireq.i_type = IEEE80211_IOC_MACCMD;
1200149029Ssam	ireq.i_val = IEEE80211_MACCMD_POLICY;
1201149029Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0) {
1202149029Ssam		if (errno == EINVAL) {
1203149029Ssam			printf("No acl policy loaded\n");
1204149029Ssam			return;
1205149029Ssam		}
1206149029Ssam		err(1, "unable to get mac policy");
1207149029Ssam	}
1208149029Ssam	policy = ireq.i_val;
1209149029Ssam
1210149029Ssam	ireq.i_val = IEEE80211_MACCMD_LIST;
1211149029Ssam	ireq.i_len = 0;
1212149029Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
1213149029Ssam		err(1, "unable to get mac acl list size");
1214149029Ssam	if (ireq.i_len == 0)		/* NB: no acls */
1215149029Ssam		return;
1216149029Ssam
1217149029Ssam	ireq.i_data = malloc(ireq.i_len);
1218149029Ssam	if (ireq.i_data == NULL)
1219149029Ssam		err(1, "out of memory for acl list");
1220149029Ssam
1221149029Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
1222149029Ssam		err(1, "unable to get mac acl list");
1223149029Ssam	if (policy == IEEE80211_MACCMD_POLICY_OPEN) {
1224149029Ssam		if (verbose)
1225149029Ssam			printf("policy: open\n");
1226149029Ssam		c = '*';
1227149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_ALLOW) {
1228149029Ssam		if (verbose)
1229149029Ssam			printf("policy: allow\n");
1230149029Ssam		c = '+';
1231149029Ssam	} else if (policy == IEEE80211_MACCMD_POLICY_DENY) {
1232149029Ssam		if (verbose)
1233149029Ssam			printf("policy: deny\n");
1234149029Ssam		c = '-';
1235149029Ssam	} else {
1236149029Ssam		printf("policy: unknown (%u)\n", policy);
1237149029Ssam		c = '?';
1238149029Ssam	}
1239149029Ssam	nacls = ireq.i_len / sizeof(*acllist);
1240149029Ssam	acllist = (struct ieee80211req_maclist *) ireq.i_data;
1241149029Ssam	for (i = 0; i < nacls; i++)
1242149029Ssam		printf("%c%s\n", c, ether_ntoa(
1243149029Ssam			(const struct ether_addr *) acllist[i].ml_macaddr));
1244149029Ssam}
1245149029Ssam
1246138593Ssamstatic
1247138593SsamDECL_CMD_FUNC(set80211list, arg, d)
1248138593Ssam{
1249138593Ssam#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
1250138593Ssam
1251138593Ssam	if (iseq(arg, "sta"))
1252138593Ssam		list_stations(s);
1253138593Ssam	else if (iseq(arg, "scan") || iseq(arg, "ap"))
1254138593Ssam		list_scan(s);
1255138593Ssam	else if (iseq(arg, "chan") || iseq(arg, "freq"))
1256138593Ssam		list_channels(s, 1);
1257138593Ssam	else if (iseq(arg, "active"))
1258138593Ssam		list_channels(s, 0);
1259138593Ssam	else if (iseq(arg, "keys"))
1260138593Ssam		list_keys(s);
1261138593Ssam	else if (iseq(arg, "caps"))
1262138593Ssam		list_capabilities(s);
1263138593Ssam	else if (iseq(arg, "wme"))
1264138593Ssam		list_wme(s);
1265149029Ssam	else if (iseq(arg, "mac"))
1266149029Ssam		list_mac(s);
1267138593Ssam	else
1268138593Ssam		errx(1, "Don't know how to list %s for %s", arg, name);
1269138593Ssam#undef iseq
1270138593Ssam}
1271138593Ssam
1272138593Ssamstatic enum ieee80211_opmode
1273138593Ssamget80211opmode(int s)
1274138593Ssam{
1275138593Ssam	struct ifmediareq ifmr;
1276138593Ssam
1277138593Ssam	(void) memset(&ifmr, 0, sizeof(ifmr));
1278138593Ssam	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
1279138593Ssam
1280138593Ssam	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
1281138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_ADHOC)
1282138593Ssam			return IEEE80211_M_IBSS;	/* XXX ahdemo */
1283138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
1284138593Ssam			return IEEE80211_M_HOSTAP;
1285138593Ssam		if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
1286138593Ssam			return IEEE80211_M_MONITOR;
1287138593Ssam	}
1288138593Ssam	return IEEE80211_M_STA;
1289138593Ssam}
1290138593Ssam
1291138593Ssamstatic const struct ieee80211_channel *
1292138593Ssamgetchaninfo(int s, int chan)
1293138593Ssam{
1294138593Ssam	struct ieee80211req ireq;
1295138593Ssam	static struct ieee80211req_chaninfo chans;
1296138593Ssam	static struct ieee80211_channel undef;
1297138593Ssam	const struct ieee80211_channel *c;
1298138593Ssam	int i, freq;
1299138593Ssam
1300138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
1301138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
1302138593Ssam	ireq.i_type = IEEE80211_IOC_CHANINFO;
1303138593Ssam	ireq.i_data = &chans;
1304138593Ssam	ireq.i_len = sizeof(chans);
1305138593Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
1306138593Ssam		errx(1, "unable to get channel information");
1307138593Ssam	freq = ieee80211_ieee2mhz(chan);
1308138593Ssam	for (i = 0; i < chans.ic_nchans; i++) {
1309138593Ssam		c = &chans.ic_chans[i];
1310138593Ssam		if (c->ic_freq == freq)
1311138593Ssam			return c;
1312138593Ssam	}
1313138593Ssam	return &undef;
1314138593Ssam}
1315138593Ssam
1316138593Ssam#if 0
1317138593Ssamstatic void
1318138593Ssamprintcipher(int s, struct ieee80211req *ireq, int keylenop)
1319138593Ssam{
1320138593Ssam	switch (ireq->i_val) {
1321138593Ssam	case IEEE80211_CIPHER_WEP:
1322138593Ssam		ireq->i_type = keylenop;
1323138593Ssam		if (ioctl(s, SIOCG80211, ireq) != -1)
1324138593Ssam			printf("WEP-%s",
1325138593Ssam			    ireq->i_len <= 5 ? "40" :
1326138593Ssam			    ireq->i_len <= 13 ? "104" : "128");
1327138593Ssam		else
1328138593Ssam			printf("WEP");
1329138593Ssam		break;
1330138593Ssam	case IEEE80211_CIPHER_TKIP:
1331138593Ssam		printf("TKIP");
1332138593Ssam		break;
1333138593Ssam	case IEEE80211_CIPHER_AES_OCB:
1334138593Ssam		printf("AES-OCB");
1335138593Ssam		break;
1336138593Ssam	case IEEE80211_CIPHER_AES_CCM:
1337138593Ssam		printf("AES-CCM");
1338138593Ssam		break;
1339138593Ssam	case IEEE80211_CIPHER_CKIP:
1340138593Ssam		printf("CKIP");
1341138593Ssam		break;
1342138593Ssam	case IEEE80211_CIPHER_NONE:
1343138593Ssam		printf("NONE");
1344138593Ssam		break;
1345138593Ssam	default:
1346138593Ssam		printf("UNKNOWN (0x%x)", ireq->i_val);
1347138593Ssam		break;
1348138593Ssam	}
1349138593Ssam}
1350138593Ssam#endif
1351138593Ssam
1352138593Ssam#define	MAXCOL	78
1353155931Ssamstatic	int col;
1354155931Ssamstatic	char spacer;
1355138593Ssam
1356155931Ssamstatic void
1357155931SsamLINE_BREAK(void)
1358155931Ssam{
1359155931Ssam	if (spacer != '\t') {
1360155931Ssam		printf("\n");
1361155931Ssam		spacer = '\t';
1362155931Ssam	}
1363155931Ssam	col = 8;	/* 8-col tab */
1364155931Ssam}
1365138593Ssam
1366138593Ssamstatic void
1367155931SsamLINE_CHECK(const char *fmt, ...)
1368155931Ssam{
1369155931Ssam	char buf[80];
1370155931Ssam	va_list ap;
1371155931Ssam	int n;
1372155931Ssam
1373155931Ssam	va_start(ap, fmt);
1374155931Ssam	n = vsnprintf(buf+1, sizeof(buf)-1, fmt, ap);
1375155931Ssam	va_end(ap);
1376155931Ssam	col += 1+n;
1377155931Ssam	if (col > MAXCOL) {
1378155931Ssam		LINE_BREAK();
1379155931Ssam		col += n;
1380155931Ssam	}
1381155931Ssam	buf[0] = spacer;
1382155931Ssam	printf("%s", buf);
1383155931Ssam	spacer = ' ';
1384155931Ssam}
1385155931Ssam
1386155931Ssamstatic void
1387138593Ssamprintkey(const struct ieee80211req_key *ik)
1388138593Ssam{
1389138593Ssam	static const uint8_t zerodata[IEEE80211_KEYBUF_SIZE];
1390138593Ssam	int keylen = ik->ik_keylen;
1391138593Ssam	int printcontents;
1392138593Ssam
1393148001Srwatson	printcontents = printkeys &&
1394138593Ssam		(memcmp(ik->ik_keydata, zerodata, keylen) != 0 || verbose);
1395138593Ssam	if (printcontents)
1396138593Ssam		LINE_BREAK();
1397138593Ssam	switch (ik->ik_type) {
1398138593Ssam	case IEEE80211_CIPHER_WEP:
1399138593Ssam		/* compatibility */
1400155931Ssam		LINE_CHECK("wepkey %u:%s", ik->ik_keyix+1,
1401138593Ssam		    keylen <= 5 ? "40-bit" :
1402138593Ssam		    keylen <= 13 ? "104-bit" : "128-bit");
1403138593Ssam		break;
1404138593Ssam	case IEEE80211_CIPHER_TKIP:
1405138593Ssam		if (keylen > 128/8)
1406138593Ssam			keylen -= 128/8;	/* ignore MIC for now */
1407155931Ssam		LINE_CHECK("TKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
1408138593Ssam		break;
1409138593Ssam	case IEEE80211_CIPHER_AES_OCB:
1410155931Ssam		LINE_CHECK("AES-OCB %u:%u-bit", ik->ik_keyix+1, 8*keylen);
1411138593Ssam		break;
1412138593Ssam	case IEEE80211_CIPHER_AES_CCM:
1413155931Ssam		LINE_CHECK("AES-CCM %u:%u-bit", ik->ik_keyix+1, 8*keylen);
1414138593Ssam		break;
1415138593Ssam	case IEEE80211_CIPHER_CKIP:
1416155931Ssam		LINE_CHECK("CKIP %u:%u-bit", ik->ik_keyix+1, 8*keylen);
1417138593Ssam		break;
1418138593Ssam	case IEEE80211_CIPHER_NONE:
1419155931Ssam		LINE_CHECK("NULL %u:%u-bit", ik->ik_keyix+1, 8*keylen);
1420138593Ssam		break;
1421138593Ssam	default:
1422155931Ssam		LINE_CHECK("UNKNOWN (0x%x) %u:%u-bit",
1423138593Ssam			ik->ik_type, ik->ik_keyix+1, 8*keylen);
1424138593Ssam		break;
1425138593Ssam	}
1426138593Ssam	if (printcontents) {
1427138593Ssam		int i;
1428138593Ssam
1429138593Ssam		printf(" <");
1430138593Ssam		for (i = 0; i < keylen; i++)
1431138593Ssam			printf("%02x", ik->ik_keydata[i]);
1432138593Ssam		printf(">");
1433138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
1434138593Ssam		    (ik->ik_keyrsc != 0 || verbose))
1435146873Sjhb			printf(" rsc %ju", (uintmax_t)ik->ik_keyrsc);
1436138593Ssam		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
1437138593Ssam		    (ik->ik_keytsc != 0 || verbose))
1438146873Sjhb			printf(" tsc %ju", (uintmax_t)ik->ik_keytsc);
1439138593Ssam		if (ik->ik_flags != 0 && verbose) {
1440138593Ssam			const char *sep = " ";
1441138593Ssam
1442138593Ssam			if (ik->ik_flags & IEEE80211_KEY_XMIT)
1443138593Ssam				printf("%stx", sep), sep = "+";
1444138593Ssam			if (ik->ik_flags & IEEE80211_KEY_RECV)
1445138593Ssam				printf("%srx", sep), sep = "+";
1446138593Ssam			if (ik->ik_flags & IEEE80211_KEY_DEFAULT)
1447138593Ssam				printf("%sdef", sep), sep = "+";
1448138593Ssam		}
1449138593Ssam		LINE_BREAK();
1450138593Ssam	}
1451138593Ssam}
1452138593Ssam
1453138593Ssamstatic void
1454139494Ssamieee80211_status(int s)
1455138593Ssam{
1456138593Ssam	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
1457138593Ssam	enum ieee80211_opmode opmode = get80211opmode(s);
1458138593Ssam	int i, num, wpa, wme;
1459138593Ssam	struct ieee80211req ireq;
1460138593Ssam	u_int8_t data[32];
1461138593Ssam	const struct ieee80211_channel *c;
1462138593Ssam
1463138593Ssam	(void) memset(&ireq, 0, sizeof(ireq));
1464138593Ssam	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
146577218Sphk	ireq.i_data = &data;
146677218Sphk
1467138593Ssam	wpa = 0;		/* unknown/not set */
1468138593Ssam
146977218Sphk	ireq.i_type = IEEE80211_IOC_SSID;
147077218Sphk	ireq.i_val = -1;
147177218Sphk	if (ioctl(s, SIOCG80211, &ireq) < 0) {
1472148686Sstefanf		/* If we can't get the SSID, this isn't an 802.11 device. */
147377218Sphk		return;
147477218Sphk	}
147588748Sambrisko	num = 0;
147688748Sambrisko	ireq.i_type = IEEE80211_IOC_NUMSSIDS;
1477138593Ssam	if (ioctl(s, SIOCG80211, &ireq) >= 0)
147888748Sambrisko		num = ireq.i_val;
1479138593Ssam	printf("\tssid ");
1480138593Ssam	if (num > 1) {
1481138593Ssam		ireq.i_type = IEEE80211_IOC_SSID;
1482138593Ssam		for (ireq.i_val = 0; ireq.i_val < num; ireq.i_val++) {
1483138593Ssam			if (ioctl(s, SIOCG80211, &ireq) >= 0 && ireq.i_len > 0) {
1484138593Ssam				printf(" %d:", ireq.i_val + 1);
1485138593Ssam				print_string(data, ireq.i_len);
1486138593Ssam			}
148788748Sambrisko		}
1488138593Ssam	} else
1489138593Ssam		print_string(data, ireq.i_len);
149077218Sphk
1491138593Ssam	ireq.i_type = IEEE80211_IOC_CHANNEL;
1492138593Ssam	if (ioctl(s, SIOCG80211, &ireq) < 0)
1493138593Ssam		goto end;
1494138593Ssam	c = getchaninfo(s, ireq.i_val);
1495138593Ssam	if (ireq.i_val != -1) {
1496138593Ssam		printf(" channel %d", ireq.i_val);
1497138593Ssam		if (verbose)
1498138593Ssam			printf(" (%u)", c->ic_freq);
1499138593Ssam	} else if (verbose)
1500138593Ssam		printf(" channel UNDEF");
1501138593Ssam
1502138593Ssam	ireq.i_type = IEEE80211_IOC_BSSID;
1503138593Ssam	ireq.i_len = IEEE80211_ADDR_LEN;
1504138593Ssam	if (ioctl(s, SIOCG80211, &ireq) >= 0 &&
1505153405Ssam	    (memcmp(ireq.i_data, zerobssid, sizeof(zerobssid)) != 0 || verbose))
1506138593Ssam		printf(" bssid %s", ether_ntoa(ireq.i_data));
1507138593Ssam
150877218Sphk	ireq.i_type = IEEE80211_IOC_STATIONNAME;
150977218Sphk	if (ioctl(s, SIOCG80211, &ireq) != -1) {
1510138593Ssam		printf("\n\tstationname ");
151177218Sphk		print_string(data, ireq.i_len);
151277218Sphk	}
151377218Sphk
1514138593Ssam	spacer = ' ';		/* force first break */
1515138593Ssam	LINE_BREAK();
151677218Sphk
151777218Sphk	ireq.i_type = IEEE80211_IOC_AUTHMODE;
151877218Sphk	if (ioctl(s, SIOCG80211, &ireq) != -1) {
151977218Sphk		switch (ireq.i_val) {
152077218Sphk			case IEEE80211_AUTH_NONE:
1521155931Ssam				LINE_CHECK("authmode NONE");
152277218Sphk				break;
152377218Sphk			case IEEE80211_AUTH_OPEN:
1524155931Ssam				LINE_CHECK("authmode OPEN");
152577218Sphk				break;
152677218Sphk			case IEEE80211_AUTH_SHARED:
1527155931Ssam				LINE_CHECK("authmode SHARED");
152877218Sphk				break;
1529138593Ssam			case IEEE80211_AUTH_8021X:
1530155931Ssam				LINE_CHECK("authmode 802.1x");
153177218Sphk				break;
1532138593Ssam			case IEEE80211_AUTH_WPA:
1533138593Ssam				ireq.i_type = IEEE80211_IOC_WPA;
1534138593Ssam				if (ioctl(s, SIOCG80211, &ireq) != -1)
1535138593Ssam					wpa = ireq.i_val;
1536138593Ssam				if (!wpa)
1537138593Ssam					wpa = 1;	/* default to WPA1 */
1538138593Ssam				switch (wpa) {
1539138593Ssam				case 2:
1540155931Ssam					LINE_CHECK("authmode WPA2/802.11i");
1541138593Ssam					break;
1542138593Ssam				case 3:
1543155931Ssam					LINE_CHECK("authmode WPA1+WPA2/802.11i");
1544138593Ssam					break;
1545138593Ssam				default:
1546155931Ssam					LINE_CHECK("authmode WPA");
1547138593Ssam					break;
1548138593Ssam				}
154977218Sphk				break;
1550138593Ssam			case IEEE80211_AUTH_AUTO:
1551155931Ssam				LINE_CHECK("authmode AUTO");
155277218Sphk				break;
1553127649Ssam			default:
1554155931Ssam				LINE_CHECK("authmode UNKNOWN (0x%x)",
1555155931Ssam					ireq.i_val);
1556127649Ssam				break;
1557127649Ssam		}
1558127649Ssam	}
1559127649Ssam
156077218Sphk	ireq.i_type = IEEE80211_IOC_WEP;
156180315Sbrooks	if (ioctl(s, SIOCG80211, &ireq) != -1 &&
156280315Sbrooks	    ireq.i_val != IEEE80211_WEP_NOSUP) {
1563138718Ssam		int firstkey, wepmode;
1564138593Ssam
1565138718Ssam		wepmode = ireq.i_val;
1566138718Ssam		switch (wepmode) {
156777218Sphk			case IEEE80211_WEP_OFF:
1568155931Ssam				LINE_CHECK("privacy OFF");
156977218Sphk				break;
157077218Sphk			case IEEE80211_WEP_ON:
1571155931Ssam				LINE_CHECK("privacy ON");
157277218Sphk				break;
157377218Sphk			case IEEE80211_WEP_MIXED:
1574155931Ssam				LINE_CHECK("privacy MIXED");
157577218Sphk				break;
157677218Sphk			default:
1577155931Ssam				LINE_CHECK("privacy UNKNOWN (0x%x)", wepmode);
157877218Sphk				break;
157977218Sphk		}
158077218Sphk
158177218Sphk		/*
158277218Sphk		 * If we get here then we've got WEP support so we need
158377218Sphk		 * to print WEP status.
158491454Sbrooks		 */
158577218Sphk
158677218Sphk		ireq.i_type = IEEE80211_IOC_WEPTXKEY;
158777218Sphk		if (ioctl(s, SIOCG80211, &ireq) < 0) {
158877218Sphk			warn("WEP support, but no tx key!");
158977218Sphk			goto end;
159077218Sphk		}
1591138593Ssam		if (ireq.i_val != -1)
1592155931Ssam			LINE_CHECK("deftxkey %d", ireq.i_val+1);
1593138718Ssam		else if (wepmode != IEEE80211_WEP_OFF || verbose)
1594155931Ssam			LINE_CHECK("deftxkey UNDEF");
159577218Sphk
159677218Sphk		ireq.i_type = IEEE80211_IOC_NUMWEPKEYS;
159777218Sphk		if (ioctl(s, SIOCG80211, &ireq) < 0) {
159877218Sphk			warn("WEP support, but no NUMWEPKEYS support!");
159977218Sphk			goto end;
160077218Sphk		}
160177218Sphk		num = ireq.i_val;
160277218Sphk
1603138593Ssam		firstkey = 1;
1604138593Ssam		for (i = 0; i < num; i++) {
1605138593Ssam			struct ieee80211req_key ik;
160677218Sphk
1607138593Ssam			memset(&ik, 0, sizeof(ik));
1608138593Ssam			ik.ik_keyix = i;
1609138593Ssam			ireq.i_type = IEEE80211_IOC_WPAKEY;
1610138593Ssam			ireq.i_data = &ik;
1611138593Ssam			ireq.i_len = sizeof(ik);
161277218Sphk			if (ioctl(s, SIOCG80211, &ireq) < 0) {
161377218Sphk				warn("WEP support, but can get keys!");
161477218Sphk				goto end;
161577218Sphk			}
1616138593Ssam			if (ik.ik_keylen != 0) {
1617138593Ssam				if (verbose)
1618138593Ssam					LINE_BREAK();
1619138593Ssam				printkey(&ik);
1620138593Ssam				firstkey = 0;
1621138593Ssam			}
1622138593Ssam		}
1623138593Ssam	}
1624138593Ssam
1625138593Ssam	ireq.i_type = IEEE80211_IOC_POWERSAVE;
1626138593Ssam	if (ioctl(s, SIOCG80211, &ireq) != -1 &&
1627138593Ssam	    ireq.i_val != IEEE80211_POWERSAVE_NOSUP ) {
1628138593Ssam		if (ireq.i_val != IEEE80211_POWERSAVE_OFF || verbose) {
1629138593Ssam			switch (ireq.i_val) {
1630138593Ssam				case IEEE80211_POWERSAVE_OFF:
1631155931Ssam					LINE_CHECK("powersavemode OFF");
1632138593Ssam					break;
1633138593Ssam				case IEEE80211_POWERSAVE_CAM:
1634155931Ssam					LINE_CHECK("powersavemode CAM");
1635138593Ssam					break;
1636138593Ssam				case IEEE80211_POWERSAVE_PSP:
1637155931Ssam					LINE_CHECK("powersavemode PSP");
1638138593Ssam					break;
1639138593Ssam				case IEEE80211_POWERSAVE_PSP_CAM:
1640155931Ssam					LINE_CHECK("powersavemode PSP-CAM");
1641138593Ssam					break;
1642138593Ssam			}
1643138593Ssam			ireq.i_type = IEEE80211_IOC_POWERSAVESLEEP;
1644138593Ssam			if (ioctl(s, SIOCG80211, &ireq) != -1)
1645155931Ssam				LINE_CHECK("powersavesleep %d", ireq.i_val);
1646138593Ssam		}
1647138593Ssam	}
1648138593Ssam
1649138593Ssam	ireq.i_type = IEEE80211_IOC_TXPOWMAX;
1650138593Ssam	if (ioctl(s, SIOCG80211, &ireq) != -1)
1651155931Ssam		LINE_CHECK("txpowmax %d", ireq.i_val);
1652138593Ssam
1653138593Ssam	if (verbose) {
1654138593Ssam		ireq.i_type = IEEE80211_IOC_TXPOWER;
1655138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1)
1656155931Ssam			LINE_CHECK("txpower %d", ireq.i_val);
1657138593Ssam	}
1658138593Ssam
1659138593Ssam	ireq.i_type = IEEE80211_IOC_RTSTHRESHOLD;
1660138593Ssam	if (ioctl(s, SIOCG80211, &ireq) != -1) {
1661138593Ssam		if (ireq.i_val != IEEE80211_RTS_MAX || verbose)
1662155931Ssam			LINE_CHECK("rtsthreshold %d", ireq.i_val);
1663138593Ssam	}
1664138593Ssam
1665153354Ssam	ireq.i_type = IEEE80211_IOC_MCAST_RATE;
1666153354Ssam	if (ioctl(s, SIOCG80211, &ireq) != -1) {
1667153354Ssam		if (ireq.i_val != 2*1 || verbose) {
1668153354Ssam			if (ireq.i_val == 11)
1669155931Ssam				LINE_CHECK("mcastrate 5.5");
1670153354Ssam			else
1671155931Ssam				LINE_CHECK("mcastrate %d", ireq.i_val/2);
1672153354Ssam		}
1673153354Ssam	}
1674153354Ssam
1675148416Ssam	ireq.i_type = IEEE80211_IOC_FRAGTHRESHOLD;
1676148416Ssam	if (ioctl(s, SIOCG80211, &ireq) != -1) {
1677148416Ssam		if (ireq.i_val != IEEE80211_FRAG_MAX || verbose)
1678155931Ssam			LINE_CHECK("fragthreshold %d", ireq.i_val);
1679148416Ssam	}
1680148416Ssam
1681160687Ssam	ireq.i_type = IEEE80211_IOC_BMISSTHRESHOLD;
1682160687Ssam	if (ioctl(s, SIOCG80211, &ireq) != -1) {
1683160687Ssam		if (ireq.i_val != IEEE80211_HWBMISS_MAX || verbose)
1684160687Ssam			LINE_CHECK("bmiss %d", ireq.i_val);
1685160687Ssam	}
1686160687Ssam
1687138593Ssam	if (IEEE80211_IS_CHAN_G(c) || IEEE80211_IS_CHAN_PUREG(c) || verbose) {
1688147795Ssam		ireq.i_type = IEEE80211_IOC_PUREG;
1689147795Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1690147795Ssam			if (ireq.i_val)
1691155931Ssam				LINE_CHECK("pureg");
1692147795Ssam			else if (verbose)
1693155931Ssam				LINE_CHECK("-pureg");
1694147795Ssam		}
1695138593Ssam		ireq.i_type = IEEE80211_IOC_PROTMODE;
1696138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1697138593Ssam			switch (ireq.i_val) {
1698138593Ssam				case IEEE80211_PROTMODE_OFF:
1699155931Ssam					LINE_CHECK("protmode OFF");
1700138593Ssam					break;
1701138593Ssam				case IEEE80211_PROTMODE_CTS:
1702155931Ssam					LINE_CHECK("protmode CTS");
1703138593Ssam					break;
1704138593Ssam				case IEEE80211_PROTMODE_RTSCTS:
1705155931Ssam					LINE_CHECK("protmode RTSCTS");
1706138593Ssam					break;
1707138593Ssam				default:
1708155931Ssam					LINE_CHECK("protmode UNKNOWN (0x%x)",
1709155931Ssam						ireq.i_val);
1710138593Ssam					break;
1711138593Ssam			}
1712138593Ssam		}
1713138593Ssam	}
1714138593Ssam
1715138593Ssam	ireq.i_type = IEEE80211_IOC_WME;
1716138593Ssam	if (ioctl(s, SIOCG80211, &ireq) != -1) {
1717138593Ssam		wme = ireq.i_val;
1718138593Ssam		if (wme)
1719155931Ssam			LINE_CHECK("wme");
1720138593Ssam		else if (verbose)
1721155931Ssam			LINE_CHECK("-wme");
1722138593Ssam	} else
1723138593Ssam		wme = 0;
1724138593Ssam
1725153422Ssam	ireq.i_type = IEEE80211_IOC_BURST;
1726153422Ssam	if (ioctl(s, SIOCG80211, &ireq) != -1) {
1727153422Ssam		if (ireq.i_val)
1728155931Ssam			LINE_CHECK("burst");
1729153422Ssam		else if (verbose)
1730155931Ssam			LINE_CHECK("-burst");
1731153422Ssam	}
1732153422Ssam
1733138593Ssam	if (opmode == IEEE80211_M_HOSTAP) {
1734138593Ssam		ireq.i_type = IEEE80211_IOC_HIDESSID;
1735138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1736138593Ssam			if (ireq.i_val)
1737155931Ssam				LINE_CHECK("ssid HIDE");
1738138593Ssam			else if (verbose)
1739155931Ssam				LINE_CHECK("ssid SHOW");
1740138593Ssam		}
1741138593Ssam
1742138593Ssam		ireq.i_type = IEEE80211_IOC_APBRIDGE;
1743138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1744138593Ssam			if (!ireq.i_val)
1745155931Ssam				LINE_CHECK("-apbridge");
1746138593Ssam			else if (verbose)
1747155931Ssam				LINE_CHECK("apbridge");
1748138593Ssam		}
1749138593Ssam
1750138593Ssam		ireq.i_type = IEEE80211_IOC_DTIM_PERIOD;
1751138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1)
1752155931Ssam			LINE_CHECK("dtimperiod %u", ireq.i_val);
1753138593Ssam	} else {
1754138593Ssam		ireq.i_type = IEEE80211_IOC_ROAMING;
1755138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1756138593Ssam			if (ireq.i_val != IEEE80211_ROAMING_AUTO || verbose) {
1757138593Ssam				switch (ireq.i_val) {
1758138593Ssam				case IEEE80211_ROAMING_DEVICE:
1759155931Ssam					LINE_CHECK("roaming DEVICE");
1760138593Ssam					break;
1761138593Ssam				case IEEE80211_ROAMING_AUTO:
1762155931Ssam					LINE_CHECK("roaming AUTO");
1763138593Ssam					break;
1764138593Ssam				case IEEE80211_ROAMING_MANUAL:
1765155931Ssam					LINE_CHECK("roaming MANUAL");
1766138593Ssam					break;
1767138593Ssam				default:
1768155931Ssam					LINE_CHECK("roaming UNKNOWN (0x%x)",
1769155931Ssam						ireq.i_val);
1770138593Ssam					break;
1771138593Ssam				}
1772138593Ssam			}
1773138593Ssam		}
1774138593Ssam	}
1775138593Ssam	ireq.i_type = IEEE80211_IOC_BEACON_INTERVAL;
1776138593Ssam	if (ioctl(s, SIOCG80211, &ireq) != -1) {
1777138593Ssam		if (ireq.i_val)
1778155931Ssam			LINE_CHECK("bintval %u", ireq.i_val);
1779138593Ssam		else if (verbose)
1780155931Ssam			LINE_CHECK("bintval %u", ireq.i_val);
1781138593Ssam	}
1782138593Ssam
1783138593Ssam	if (wme && verbose) {
1784138593Ssam		LINE_BREAK();
1785138593Ssam		list_wme(s);
1786138593Ssam	}
1787138593Ssam
1788138593Ssam	if (wpa) {
1789138593Ssam		ireq.i_type = IEEE80211_IOC_COUNTERMEASURES;
1790138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1791138593Ssam			if (ireq.i_val)
1792155931Ssam				LINE_CHECK("countermeasures");
1793138593Ssam			else if (verbose)
1794155931Ssam				LINE_CHECK("-countermeasures");
1795138593Ssam		}
1796138593Ssam#if 0
1797138593Ssam		/* XXX not interesting with WPA done in user space */
1798138593Ssam		ireq.i_type = IEEE80211_IOC_KEYMGTALGS;
1799138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1800138593Ssam		}
1801138593Ssam
1802138593Ssam		ireq.i_type = IEEE80211_IOC_MCASTCIPHER;
1803138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1804155931Ssam			LINE_CHECK("mcastcipher ");
1805138593Ssam			printcipher(s, &ireq, IEEE80211_IOC_MCASTKEYLEN);
1806138593Ssam			spacer = ' ';
1807138593Ssam		}
1808138593Ssam
1809138593Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHER;
1810138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1811155931Ssam			LINE_CHECK("ucastcipher ");
1812138593Ssam			printcipher(s, &ireq, IEEE80211_IOC_UCASTKEYLEN);
1813138593Ssam		}
1814138593Ssam
1815138593Ssam		if (wpa & 2) {
1816138593Ssam			ireq.i_type = IEEE80211_IOC_RSNCAPS;
1817138593Ssam			if (ioctl(s, SIOCG80211, &ireq) != -1) {
1818155931Ssam				LINE_CHECK("RSN caps 0x%x", ireq.i_val);
181977218Sphk				spacer = ' ';
1820138593Ssam			}
182177218Sphk		}
1822138593Ssam
1823138593Ssam		ireq.i_type = IEEE80211_IOC_UCASTCIPHERS;
1824138593Ssam		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1825138593Ssam		}
1826138593Ssam#endif
1827138593Ssam		LINE_BREAK();
182877218Sphk	}
1829138593Ssam	LINE_BREAK();
183077218Sphk
183177218Sphkend:
183277218Sphk	return;
183377218Sphk}
183477218Sphk
183577218Sphkstatic void
183677218Sphkset80211(int s, int type, int val, int len, u_int8_t *data)
183777218Sphk{
183877218Sphk	struct ieee80211req	ireq;
183977218Sphk
184077218Sphk	(void) memset(&ireq, 0, sizeof(ireq));
184177218Sphk	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
184277218Sphk	ireq.i_type = type;
184377218Sphk	ireq.i_val = val;
184477218Sphk	ireq.i_len = len;
184577218Sphk	ireq.i_data = data;
184691454Sbrooks	if (ioctl(s, SIOCS80211, &ireq) < 0)
184777218Sphk		err(1, "SIOCS80211");
184877218Sphk}
184977218Sphk
185077218Sphkstatic const char *
185177218Sphkget_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
185277218Sphk{
185377218Sphk	int len;
185477218Sphk	int hexstr;
185577218Sphk	u_int8_t *p;
185677218Sphk
185777218Sphk	len = *lenp;
185877218Sphk	p = buf;
185977218Sphk	hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
186077218Sphk	if (hexstr)
186177218Sphk		val += 2;
186277218Sphk	for (;;) {
186377218Sphk		if (*val == '\0')
186477218Sphk			break;
186577218Sphk		if (sep != NULL && strchr(sep, *val) != NULL) {
186677218Sphk			val++;
186777218Sphk			break;
186877218Sphk		}
186977218Sphk		if (hexstr) {
1870127831Sphk			if (!isxdigit((u_char)val[0])) {
187177218Sphk				warnx("bad hexadecimal digits");
187277218Sphk				return NULL;
187377218Sphk			}
1874127831Sphk			if (!isxdigit((u_char)val[1])) {
1875127831Sphk				warnx("odd count hexadecimal digits");
1876127831Sphk				return NULL;
1877127831Sphk			}
187877218Sphk		}
1879127831Sphk		if (p >= buf + len) {
188077218Sphk			if (hexstr)
188177218Sphk				warnx("hexadecimal digits too long");
188277218Sphk			else
1883127831Sphk				warnx("string too long");
188477218Sphk			return NULL;
188577218Sphk		}
188677218Sphk		if (hexstr) {
188777218Sphk#define	tohex(x)	(isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
188877218Sphk			*p++ = (tohex((u_char)val[0]) << 4) |
188977218Sphk			    tohex((u_char)val[1]);
189077218Sphk#undef tohex
189177218Sphk			val += 2;
189277218Sphk		} else
189377218Sphk			*p++ = *val++;
189477218Sphk	}
189577218Sphk	len = p - buf;
189677218Sphk	/* The string "-" is treated as the empty string. */
189777218Sphk	if (!hexstr && len == 1 && buf[0] == '-')
189877218Sphk		len = 0;
189977218Sphk	if (len < *lenp)
190077218Sphk		memset(p, 0, *lenp - len);
190177218Sphk	*lenp = len;
190277218Sphk	return val;
190377218Sphk}
190477218Sphk
190577218Sphkstatic void
190677218Sphkprint_string(const u_int8_t *buf, int len)
190777218Sphk{
190877218Sphk	int i;
190977218Sphk	int hasspc;
191077218Sphk
191177218Sphk	i = 0;
191277218Sphk	hasspc = 0;
191391454Sbrooks	for (; i < len; i++) {
191477218Sphk		if (!isprint(buf[i]) && buf[i] != '\0')
191577218Sphk			break;
191677218Sphk		if (isspace(buf[i]))
191777218Sphk			hasspc++;
191877218Sphk	}
191977218Sphk	if (i == len) {
192077218Sphk		if (hasspc || len == 0 || buf[0] == '\0')
192177218Sphk			printf("\"%.*s\"", len, buf);
192277218Sphk		else
192377218Sphk			printf("%.*s", len, buf);
192477218Sphk	} else {
192577218Sphk		printf("0x");
192677218Sphk		for (i = 0; i < len; i++)
192777218Sphk			printf("%02x", buf[i]);
192877218Sphk	}
192977218Sphk}
193077218Sphk
1931138593Ssamstatic struct cmd ieee80211_cmds[] = {
1932138593Ssam	DEF_CMD_ARG("ssid",		set80211ssid),
1933138593Ssam	DEF_CMD_ARG("nwid",		set80211ssid),
1934138593Ssam	DEF_CMD_ARG("stationname",	set80211stationname),
1935138593Ssam	DEF_CMD_ARG("station",		set80211stationname),	/* BSD/OS */
1936138593Ssam	DEF_CMD_ARG("channel",		set80211channel),
1937138593Ssam	DEF_CMD_ARG("authmode",		set80211authmode),
1938138593Ssam	DEF_CMD_ARG("powersavemode",	set80211powersavemode),
1939138593Ssam	DEF_CMD("powersave",	1,	set80211powersave),
1940138593Ssam	DEF_CMD("-powersave",	0,	set80211powersave),
1941138593Ssam	DEF_CMD_ARG("powersavesleep", 	set80211powersavesleep),
1942138593Ssam	DEF_CMD_ARG("wepmode",		set80211wepmode),
1943138593Ssam	DEF_CMD("wep",		1,	set80211wep),
1944138593Ssam	DEF_CMD("-wep",		0,	set80211wep),
1945139493Ssam	DEF_CMD_ARG("deftxkey",		set80211weptxkey),
1946138593Ssam	DEF_CMD_ARG("weptxkey",		set80211weptxkey),
1947138593Ssam	DEF_CMD_ARG("wepkey",		set80211wepkey),
1948138593Ssam	DEF_CMD_ARG("nwkey",		set80211nwkey),		/* NetBSD */
1949138593Ssam	DEF_CMD("-nwkey",	0,	set80211wep),		/* NetBSD */
1950138593Ssam	DEF_CMD_ARG("rtsthreshold",	set80211rtsthreshold),
1951138593Ssam	DEF_CMD_ARG("protmode",		set80211protmode),
1952138593Ssam	DEF_CMD_ARG("txpower",		set80211txpower),
1953138593Ssam	DEF_CMD_ARG("roaming",		set80211roaming),
1954138593Ssam	DEF_CMD("wme",		1,	set80211wme),
1955138593Ssam	DEF_CMD("-wme",		0,	set80211wme),
1956138593Ssam	DEF_CMD("hidessid",	1,	set80211hidessid),
1957138593Ssam	DEF_CMD("-hidessid",	0,	set80211hidessid),
1958138593Ssam	DEF_CMD("apbridge",	1,	set80211apbridge),
1959138593Ssam	DEF_CMD("-apbridge",	0,	set80211apbridge),
1960138593Ssam	DEF_CMD_ARG("chanlist",		set80211chanlist),
1961138593Ssam	DEF_CMD_ARG("bssid",		set80211bssid),
1962138593Ssam	DEF_CMD_ARG("ap",		set80211bssid),
1963138593Ssam	DEF_CMD("scan",	0,		set80211scan),
1964138593Ssam	DEF_CMD_ARG("list",		set80211list),
1965138593Ssam	DEF_CMD_ARG2("cwmin",		set80211cwmin),
1966138593Ssam	DEF_CMD_ARG2("cwmax",		set80211cwmax),
1967138593Ssam	DEF_CMD_ARG2("aifs",		set80211aifs),
1968138593Ssam	DEF_CMD_ARG2("txoplimit",	set80211txoplimit),
1969148621Ssam	DEF_CMD_ARG("acm",		set80211acm),
1970148621Ssam	DEF_CMD_ARG("-acm",		set80211noacm),
1971148621Ssam	DEF_CMD_ARG("ack",		set80211ackpolicy),
1972148621Ssam	DEF_CMD_ARG("-ack",		set80211noackpolicy),
1973138593Ssam	DEF_CMD_ARG2("bss:cwmin",	set80211bsscwmin),
1974138593Ssam	DEF_CMD_ARG2("bss:cwmax",	set80211bsscwmax),
1975138593Ssam	DEF_CMD_ARG2("bss:aifs",	set80211bssaifs),
1976138593Ssam	DEF_CMD_ARG2("bss:txoplimit",	set80211bsstxoplimit),
1977138593Ssam	DEF_CMD_ARG("dtimperiod",	set80211dtimperiod),
1978138593Ssam	DEF_CMD_ARG("bintval",		set80211bintval),
1979138593Ssam	DEF_CMD("mac:open",	IEEE80211_MACCMD_POLICY_OPEN,	set80211maccmd),
1980138593Ssam	DEF_CMD("mac:allow",	IEEE80211_MACCMD_POLICY_ALLOW,	set80211maccmd),
1981138593Ssam	DEF_CMD("mac:deny",	IEEE80211_MACCMD_POLICY_DENY,	set80211maccmd),
1982138593Ssam	DEF_CMD("mac:flush",	IEEE80211_MACCMD_FLUSH,		set80211maccmd),
1983138593Ssam	DEF_CMD("mac:detach",	IEEE80211_MACCMD_DETACH,	set80211maccmd),
1984138593Ssam	DEF_CMD_ARG("mac:add",		set80211addmac),
1985138593Ssam	DEF_CMD_ARG("mac:del",		set80211delmac),
1986138593Ssam	DEF_CMD_ARG("mac:kick",		set80211kickmac),
1987147795Ssam	DEF_CMD("pureg",	1,	set80211pureg),
1988147795Ssam	DEF_CMD("-pureg",	0,	set80211pureg),
1989153354Ssam	DEF_CMD_ARG("mcastrate",	set80211mcastrate),
1990148416Ssam	DEF_CMD_ARG("fragthreshold",	set80211fragthreshold),
1991153422Ssam	DEF_CMD("burst",	1,	set80211burst),
1992153422Ssam	DEF_CMD("-burst",	0,	set80211burst),
1993160687Ssam	DEF_CMD_ARG("bmiss",		set80211bmissthreshold),
1994160687Ssam	DEF_CMD_ARG("bmissthreshold",	set80211bmissthreshold),
1995138593Ssam};
1996138593Ssamstatic struct afswtch af_ieee80211 = {
1997138593Ssam	.af_name	= "af_ieee80211",
1998138593Ssam	.af_af		= AF_UNSPEC,
1999139494Ssam	.af_other_status = ieee80211_status,
2000138593Ssam};
2001138593Ssam
2002138593Ssamstatic __constructor void
2003138593Ssamieee80211_ctor(void)
2004138593Ssam{
2005138593Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
2006138593Ssam	int i;
2007138593Ssam
2008138593Ssam	for (i = 0; i < N(ieee80211_cmds);  i++)
2009138593Ssam		cmd_register(&ieee80211_cmds[i]);
2010138593Ssam	af_register(&af_ieee80211);
2011138593Ssam#undef N
2012138593Ssam}
2013