ifieee80211.c revision 153405
1/*
2 * Copyright 2001 The Aerospace Corporation.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 * 3. The name of The Aerospace Corporation may not be used to endorse or
13 *    promote products derived from this software.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AEROSPACE CORPORATION ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AEROSPACE CORPORATION BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: head/sbin/ifconfig/ifieee80211.c 153405 2005-12-14 01:22:26Z sam $
28 */
29
30/*-
31 * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
32 * All rights reserved.
33 *
34 * This code is derived from software contributed to The NetBSD Foundation
35 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
36 * NASA Ames Research Center.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 *    notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 *    notice, this list of conditions and the following disclaimer in the
45 *    documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 *    must display the following acknowledgement:
48 *	This product includes software developed by the NetBSD
49 *	Foundation, Inc. and its contributors.
50 * 4. Neither the name of The NetBSD Foundation nor the names of its
51 *    contributors may be used to endorse or promote products derived
52 *    from this software without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
55 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
58 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
61 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
62 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64 * POSSIBILITY OF SUCH DAMAGE.
65 */
66
67#include <sys/param.h>
68#include <sys/ioctl.h>
69#include <sys/socket.h>
70#include <sys/sysctl.h>
71#include <sys/time.h>
72
73#include <net/ethernet.h>
74#include <net/if.h>
75#include <net/if_dl.h>
76#include <net/if_types.h>
77#include <net/if_media.h>
78#include <net/route.h>
79
80#include <net80211/ieee80211.h>
81#include <net80211/ieee80211_crypto.h>
82#include <net80211/ieee80211_ioctl.h>
83
84#include <ctype.h>
85#include <err.h>
86#include <errno.h>
87#include <fcntl.h>
88#include <inttypes.h>
89#include <stdio.h>
90#include <stdlib.h>
91#include <string.h>
92#include <unistd.h>
93
94#include "ifconfig.h"
95
96static void set80211(int s, int type, int val, int len, u_int8_t *data);
97static const char *get_string(const char *val, const char *sep,
98    u_int8_t *buf, int *lenp);
99static void print_string(const u_int8_t *buf, int len);
100
101static int
102isanyarg(const char *arg)
103{
104	return (strcmp(arg, "-") == 0 ||
105	    strcasecmp(arg, "any") == 0 || strcasecmp(arg, "off") == 0);
106}
107
108static void
109set80211ssid(const char *val, int d, int s, const struct afswtch *rafp)
110{
111	int		ssid;
112	int		len;
113	u_int8_t	data[IEEE80211_NWID_LEN];
114
115	ssid = 0;
116	len = strlen(val);
117	if (len > 2 && isdigit(val[0]) && val[1] == ':') {
118		ssid = atoi(val)-1;
119		val += 2;
120	}
121
122	bzero(data, sizeof(data));
123	len = sizeof(data);
124	if (get_string(val, NULL, data, &len) == NULL)
125		exit(1);
126
127	set80211(s, IEEE80211_IOC_SSID, ssid, len, data);
128}
129
130static void
131set80211stationname(const char *val, int d, int s, const struct afswtch *rafp)
132{
133	int			len;
134	u_int8_t		data[33];
135
136	bzero(data, sizeof(data));
137	len = sizeof(data);
138	get_string(val, NULL, data, &len);
139
140	set80211(s, IEEE80211_IOC_STATIONNAME, 0, len, data);
141}
142
143/*
144 * Convert IEEE channel number to MHz frequency.
145 */
146static u_int
147ieee80211_ieee2mhz(u_int chan)
148{
149	if (chan == 14)
150		return 2484;
151	if (chan < 14)			/* 0-13 */
152		return 2407 + chan*5;
153	if (chan < 27)			/* 15-26 */
154		return 2512 + ((chan-15)*20);
155	return 5000 + (chan*5);
156}
157
158/*
159 * Convert MHz frequency to IEEE channel number.
160 */
161static u_int
162ieee80211_mhz2ieee(u_int freq)
163{
164	if (freq == 2484)
165		return 14;
166	if (freq < 2484)
167		return (freq - 2407) / 5;
168	if (freq < 5000)
169		return 15 + ((freq - 2512) / 20);
170	return (freq - 5000) / 5;
171}
172
173static void
174set80211channel(const char *val, int d, int s, const struct afswtch *rafp)
175{
176	if (!isanyarg(val)) {
177		int v = atoi(val);
178		if (v > 255)		/* treat as frequency */
179			v = ieee80211_mhz2ieee(v);
180		set80211(s, IEEE80211_IOC_CHANNEL, v, 0, NULL);
181	} else
182		set80211(s, IEEE80211_IOC_CHANNEL, IEEE80211_CHAN_ANY, 0, NULL);
183}
184
185static void
186set80211authmode(const char *val, int d, int s, const struct afswtch *rafp)
187{
188	int	mode;
189
190	if (strcasecmp(val, "none") == 0) {
191		mode = IEEE80211_AUTH_NONE;
192	} else if (strcasecmp(val, "open") == 0) {
193		mode = IEEE80211_AUTH_OPEN;
194	} else if (strcasecmp(val, "shared") == 0) {
195		mode = IEEE80211_AUTH_SHARED;
196	} else if (strcasecmp(val, "8021x") == 0) {
197		mode = IEEE80211_AUTH_8021X;
198	} else if (strcasecmp(val, "wpa") == 0) {
199		mode = IEEE80211_AUTH_WPA;
200	} else {
201		errx(1, "unknown authmode");
202	}
203
204	set80211(s, IEEE80211_IOC_AUTHMODE, mode, 0, NULL);
205}
206
207static void
208set80211powersavemode(const char *val, int d, int s, const struct afswtch *rafp)
209{
210	int	mode;
211
212	if (strcasecmp(val, "off") == 0) {
213		mode = IEEE80211_POWERSAVE_OFF;
214	} else if (strcasecmp(val, "on") == 0) {
215		mode = IEEE80211_POWERSAVE_ON;
216	} else if (strcasecmp(val, "cam") == 0) {
217		mode = IEEE80211_POWERSAVE_CAM;
218	} else if (strcasecmp(val, "psp") == 0) {
219		mode = IEEE80211_POWERSAVE_PSP;
220	} else if (strcasecmp(val, "psp-cam") == 0) {
221		mode = IEEE80211_POWERSAVE_PSP_CAM;
222	} else {
223		errx(1, "unknown powersavemode");
224	}
225
226	set80211(s, IEEE80211_IOC_POWERSAVE, mode, 0, NULL);
227}
228
229static void
230set80211powersave(const char *val, int d, int s, const struct afswtch *rafp)
231{
232	if (d == 0)
233		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_OFF,
234		    0, NULL);
235	else
236		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_ON,
237		    0, NULL);
238}
239
240static void
241set80211powersavesleep(const char *val, int d, int s, const struct afswtch *rafp)
242{
243	set80211(s, IEEE80211_IOC_POWERSAVESLEEP, atoi(val), 0, NULL);
244}
245
246static void
247set80211wepmode(const char *val, int d, int s, const struct afswtch *rafp)
248{
249	int	mode;
250
251	if (strcasecmp(val, "off") == 0) {
252		mode = IEEE80211_WEP_OFF;
253	} else if (strcasecmp(val, "on") == 0) {
254		mode = IEEE80211_WEP_ON;
255	} else if (strcasecmp(val, "mixed") == 0) {
256		mode = IEEE80211_WEP_MIXED;
257	} else {
258		errx(1, "unknown wep mode");
259	}
260
261	set80211(s, IEEE80211_IOC_WEP, mode, 0, NULL);
262}
263
264static void
265set80211wep(const char *val, int d, int s, const struct afswtch *rafp)
266{
267	set80211(s, IEEE80211_IOC_WEP, d, 0, NULL);
268}
269
270static int
271isundefarg(const char *arg)
272{
273	return (strcmp(arg, "-") == 0 || strncasecmp(arg, "undef", 5) == 0);
274}
275
276static void
277set80211weptxkey(const char *val, int d, int s, const struct afswtch *rafp)
278{
279	if (isundefarg(val))
280		set80211(s, IEEE80211_IOC_WEPTXKEY, IEEE80211_KEYIX_NONE, 0, NULL);
281	else
282		set80211(s, IEEE80211_IOC_WEPTXKEY, atoi(val)-1, 0, NULL);
283}
284
285static void
286set80211wepkey(const char *val, int d, int s, const struct afswtch *rafp)
287{
288	int		key = 0;
289	int		len;
290	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
291
292	if (isdigit(val[0]) && val[1] == ':') {
293		key = atoi(val)-1;
294		val += 2;
295	}
296
297	bzero(data, sizeof(data));
298	len = sizeof(data);
299	get_string(val, NULL, data, &len);
300
301	set80211(s, IEEE80211_IOC_WEPKEY, key, len, data);
302}
303
304/*
305 * This function is purely a NetBSD compatability interface.  The NetBSD
306 * interface is too inflexible, but it's there so we'll support it since
307 * it's not all that hard.
308 */
309static void
310set80211nwkey(const char *val, int d, int s, const struct afswtch *rafp)
311{
312	int		txkey;
313	int		i, len;
314	u_int8_t	data[IEEE80211_KEYBUF_SIZE];
315
316	set80211(s, IEEE80211_IOC_WEP, IEEE80211_WEP_ON, 0, NULL);
317
318	if (isdigit(val[0]) && val[1] == ':') {
319		txkey = val[0]-'0'-1;
320		val += 2;
321
322		for (i = 0; i < 4; i++) {
323			bzero(data, sizeof(data));
324			len = sizeof(data);
325			val = get_string(val, ",", data, &len);
326			if (val == NULL)
327				exit(1);
328
329			set80211(s, IEEE80211_IOC_WEPKEY, i, len, data);
330		}
331	} else {
332		bzero(data, sizeof(data));
333		len = sizeof(data);
334		get_string(val, NULL, data, &len);
335		txkey = 0;
336
337		set80211(s, IEEE80211_IOC_WEPKEY, 0, len, data);
338
339		bzero(data, sizeof(data));
340		for (i = 1; i < 4; i++)
341			set80211(s, IEEE80211_IOC_WEPKEY, i, 0, data);
342	}
343
344	set80211(s, IEEE80211_IOC_WEPTXKEY, txkey, 0, NULL);
345}
346
347static void
348set80211rtsthreshold(const char *val, int d, int s, const struct afswtch *rafp)
349{
350	set80211(s, IEEE80211_IOC_RTSTHRESHOLD,
351		isundefarg(val) ? IEEE80211_RTS_MAX : atoi(val), 0, NULL);
352}
353
354static void
355set80211protmode(const char *val, int d, int s, const struct afswtch *rafp)
356{
357	int	mode;
358
359	if (strcasecmp(val, "off") == 0) {
360		mode = IEEE80211_PROTMODE_OFF;
361	} else if (strcasecmp(val, "cts") == 0) {
362		mode = IEEE80211_PROTMODE_CTS;
363	} else if (strcasecmp(val, "rtscts") == 0) {
364		mode = IEEE80211_PROTMODE_RTSCTS;
365	} else {
366		errx(1, "unknown protection mode");
367	}
368
369	set80211(s, IEEE80211_IOC_PROTMODE, mode, 0, NULL);
370}
371
372static void
373set80211txpower(const char *val, int d, int s, const struct afswtch *rafp)
374{
375	set80211(s, IEEE80211_IOC_TXPOWER, atoi(val), 0, NULL);
376}
377
378#define	IEEE80211_ROAMING_DEVICE	0
379#define	IEEE80211_ROAMING_AUTO		1
380#define	IEEE80211_ROAMING_MANUAL	2
381
382static void
383set80211roaming(const char *val, int d, int s, const struct afswtch *rafp)
384{
385	int mode;
386
387	if (strcasecmp(val, "device") == 0) {
388		mode = IEEE80211_ROAMING_DEVICE;
389	} else if (strcasecmp(val, "auto") == 0) {
390		mode = IEEE80211_ROAMING_AUTO;
391	} else if (strcasecmp(val, "manual") == 0) {
392		mode = IEEE80211_ROAMING_MANUAL;
393	} else {
394		errx(1, "unknown roaming mode");
395	}
396	set80211(s, IEEE80211_IOC_ROAMING, mode, 0, NULL);
397}
398
399static void
400set80211wme(const char *val, int d, int s, const struct afswtch *rafp)
401{
402	set80211(s, IEEE80211_IOC_WME, d, 0, NULL);
403}
404
405static void
406set80211hidessid(const char *val, int d, int s, const struct afswtch *rafp)
407{
408	set80211(s, IEEE80211_IOC_HIDESSID, d, 0, NULL);
409}
410
411static void
412set80211apbridge(const char *val, int d, int s, const struct afswtch *rafp)
413{
414	set80211(s, IEEE80211_IOC_APBRIDGE, d, 0, NULL);
415}
416
417static void
418set80211chanlist(const char *val, int d, int s, const struct afswtch *rafp)
419{
420	struct ieee80211req_chanlist chanlist;
421#define	MAXCHAN	(sizeof(chanlist.ic_channels)*NBBY)
422	char *temp, *cp, *tp;
423
424	temp = malloc(strlen(val) + 1);
425	if (temp == NULL)
426		errx(1, "malloc failed");
427	strcpy(temp, val);
428	memset(&chanlist, 0, sizeof(chanlist));
429	cp = temp;
430	for (;;) {
431		int first, last, f;
432
433		tp = strchr(cp, ',');
434		if (tp != NULL)
435			*tp++ = '\0';
436		switch (sscanf(cp, "%u-%u", &first, &last)) {
437		case 1:
438			if (first > MAXCHAN)
439				errx(-1, "channel %u out of range, max %zu",
440					first, MAXCHAN);
441			setbit(chanlist.ic_channels, first);
442			break;
443		case 2:
444			if (first > MAXCHAN)
445				errx(-1, "channel %u out of range, max %zu",
446					first, MAXCHAN);
447			if (last > MAXCHAN)
448				errx(-1, "channel %u out of range, max %zu",
449					last, MAXCHAN);
450			if (first > last)
451				errx(-1, "void channel range, %u > %u",
452					first, last);
453			for (f = first; f <= last; f++)
454				setbit(chanlist.ic_channels, f);
455			break;
456		}
457		if (tp == NULL)
458			break;
459		while (isspace(*tp))
460			tp++;
461		if (!isdigit(*tp))
462			break;
463		cp = tp;
464	}
465	set80211(s, IEEE80211_IOC_CHANLIST, 0,
466		sizeof(chanlist), (uint8_t *) &chanlist);
467#undef MAXCHAN
468}
469
470static void
471set80211bssid(const char *val, int d, int s, const struct afswtch *rafp)
472{
473
474	if (!isanyarg(val)) {
475		char *temp;
476		struct sockaddr_dl sdl;
477
478		temp = malloc(strlen(val) + 1);
479		if (temp == NULL)
480			errx(1, "malloc failed");
481		temp[0] = ':';
482		strcpy(temp + 1, val);
483		sdl.sdl_len = sizeof(sdl);
484		link_addr(temp, &sdl);
485		free(temp);
486		if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
487			errx(1, "malformed link-level address");
488		set80211(s, IEEE80211_IOC_BSSID, 0,
489			IEEE80211_ADDR_LEN, LLADDR(&sdl));
490	} else {
491		uint8_t zerobssid[IEEE80211_ADDR_LEN];
492		memset(zerobssid, 0, sizeof(zerobssid));
493		set80211(s, IEEE80211_IOC_BSSID, 0,
494			IEEE80211_ADDR_LEN, zerobssid);
495	}
496}
497
498static int
499getac(const char *ac)
500{
501	if (strcasecmp(ac, "ac_be") == 0 || strcasecmp(ac, "be") == 0)
502		return WME_AC_BE;
503	if (strcasecmp(ac, "ac_bk") == 0 || strcasecmp(ac, "bk") == 0)
504		return WME_AC_BK;
505	if (strcasecmp(ac, "ac_vi") == 0 || strcasecmp(ac, "vi") == 0)
506		return WME_AC_VI;
507	if (strcasecmp(ac, "ac_vo") == 0 || strcasecmp(ac, "vo") == 0)
508		return WME_AC_VO;
509	errx(1, "unknown wme access class %s", ac);
510}
511
512static
513DECL_CMD_FUNC2(set80211cwmin, ac, val)
514{
515	set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val), getac(ac), NULL);
516}
517
518static
519DECL_CMD_FUNC2(set80211cwmax, ac, val)
520{
521	set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val), getac(ac), NULL);
522}
523
524static
525DECL_CMD_FUNC2(set80211aifs, ac, val)
526{
527	set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val), getac(ac), NULL);
528}
529
530static
531DECL_CMD_FUNC2(set80211txoplimit, ac, val)
532{
533	set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val), getac(ac), NULL);
534}
535
536static
537DECL_CMD_FUNC(set80211acm, ac, d)
538{
539	set80211(s, IEEE80211_IOC_WME_ACM, 1, getac(ac), NULL);
540}
541static
542DECL_CMD_FUNC(set80211noacm, ac, d)
543{
544	set80211(s, IEEE80211_IOC_WME_ACM, 0, getac(ac), NULL);
545}
546
547static
548DECL_CMD_FUNC(set80211ackpolicy, ac, d)
549{
550	set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 1, getac(ac), NULL);
551}
552static
553DECL_CMD_FUNC(set80211noackpolicy, ac, d)
554{
555	set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 0, getac(ac), NULL);
556}
557
558static
559DECL_CMD_FUNC2(set80211bsscwmin, ac, val)
560{
561	set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val),
562		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
563}
564
565static
566DECL_CMD_FUNC2(set80211bsscwmax, ac, val)
567{
568	set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val),
569		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
570}
571
572static
573DECL_CMD_FUNC2(set80211bssaifs, ac, val)
574{
575	set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val),
576		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
577}
578
579static
580DECL_CMD_FUNC2(set80211bsstxoplimit, ac, val)
581{
582	set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val),
583		getac(ac)|IEEE80211_WMEPARAM_BSS, NULL);
584}
585
586static
587DECL_CMD_FUNC(set80211dtimperiod, val, d)
588{
589	set80211(s, IEEE80211_IOC_DTIM_PERIOD, atoi(val), 0, NULL);
590}
591
592static
593DECL_CMD_FUNC(set80211bintval, val, d)
594{
595	set80211(s, IEEE80211_IOC_BEACON_INTERVAL, atoi(val), 0, NULL);
596}
597
598static void
599set80211macmac(int s, int op, const char *val)
600{
601	char *temp;
602	struct sockaddr_dl sdl;
603
604	temp = malloc(strlen(val) + 1);
605	if (temp == NULL)
606		errx(1, "malloc failed");
607	temp[0] = ':';
608	strcpy(temp + 1, val);
609	sdl.sdl_len = sizeof(sdl);
610	link_addr(temp, &sdl);
611	free(temp);
612	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
613		errx(1, "malformed link-level address");
614	set80211(s, op, 0, IEEE80211_ADDR_LEN, LLADDR(&sdl));
615}
616
617static
618DECL_CMD_FUNC(set80211addmac, val, d)
619{
620	set80211macmac(s, IEEE80211_IOC_ADDMAC, val);
621}
622
623static
624DECL_CMD_FUNC(set80211delmac, val, d)
625{
626	set80211macmac(s, IEEE80211_IOC_DELMAC, val);
627}
628
629static
630DECL_CMD_FUNC(set80211kickmac, val, d)
631{
632	char *temp;
633	struct sockaddr_dl sdl;
634	struct ieee80211req_mlme mlme;
635
636	temp = malloc(strlen(val) + 1);
637	if (temp == NULL)
638		errx(1, "malloc failed");
639	temp[0] = ':';
640	strcpy(temp + 1, val);
641	sdl.sdl_len = sizeof(sdl);
642	link_addr(temp, &sdl);
643	free(temp);
644	if (sdl.sdl_alen != IEEE80211_ADDR_LEN)
645		errx(1, "malformed link-level address");
646	memset(&mlme, 0, sizeof(mlme));
647	mlme.im_op = IEEE80211_MLME_DEAUTH;
648	mlme.im_reason = IEEE80211_REASON_AUTH_EXPIRE;
649	memcpy(mlme.im_macaddr, LLADDR(&sdl), IEEE80211_ADDR_LEN);
650	set80211(s, IEEE80211_IOC_MLME, 0, sizeof(mlme), (u_int8_t *) &mlme);
651}
652
653static
654DECL_CMD_FUNC(set80211maccmd, val, d)
655{
656	set80211(s, IEEE80211_IOC_MACCMD, d, 0, NULL);
657}
658
659static void
660set80211pureg(const char *val, int d, int s, const struct afswtch *rafp)
661{
662	set80211(s, IEEE80211_IOC_PUREG, d, 0, NULL);
663}
664
665static
666DECL_CMD_FUNC(set80211mcastrate, val, d)
667{
668	set80211(s, IEEE80211_IOC_MCAST_RATE, (int) 2*atof(val), 0, NULL);
669}
670
671static
672DECL_CMD_FUNC(set80211fragthreshold, val, d)
673{
674	set80211(s, IEEE80211_IOC_FRAGTHRESHOLD,
675		isundefarg(val) ? IEEE80211_FRAG_MAX : atoi(val), 0, NULL);
676}
677
678static int
679getmaxrate(uint8_t rates[15], uint8_t nrates)
680{
681	int i, maxrate = -1;
682
683	for (i = 0; i < nrates; i++) {
684		int rate = rates[i] & IEEE80211_RATE_VAL;
685		if (rate > maxrate)
686			maxrate = rate;
687	}
688	return maxrate / 2;
689}
690
691static const char *
692getcaps(int capinfo)
693{
694	static char capstring[32];
695	char *cp = capstring;
696
697	if (capinfo & IEEE80211_CAPINFO_ESS)
698		*cp++ = 'E';
699	if (capinfo & IEEE80211_CAPINFO_IBSS)
700		*cp++ = 'I';
701	if (capinfo & IEEE80211_CAPINFO_CF_POLLABLE)
702		*cp++ = 'c';
703	if (capinfo & IEEE80211_CAPINFO_CF_POLLREQ)
704		*cp++ = 'C';
705	if (capinfo & IEEE80211_CAPINFO_PRIVACY)
706		*cp++ = 'P';
707	if (capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)
708		*cp++ = 'S';
709	if (capinfo & IEEE80211_CAPINFO_PBCC)
710		*cp++ = 'B';
711	if (capinfo & IEEE80211_CAPINFO_CHNL_AGILITY)
712		*cp++ = 'A';
713	if (capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
714		*cp++ = 's';
715	if (capinfo & IEEE80211_CAPINFO_RSN)
716		*cp++ = 'R';
717	if (capinfo & IEEE80211_CAPINFO_DSSSOFDM)
718		*cp++ = 'D';
719	*cp = '\0';
720	return capstring;
721}
722
723static void
724printie(const char* tag, const uint8_t *ie, size_t ielen, int maxlen)
725{
726	printf("%s", tag);
727	if (verbose) {
728		maxlen -= strlen(tag)+2;
729		if (2*ielen > maxlen)
730			maxlen--;
731		printf("<");
732		for (; ielen > 0; ie++, ielen--) {
733			if (maxlen-- <= 0)
734				break;
735			printf("%02x", *ie);
736		}
737		if (ielen != 0)
738			printf("-");
739		printf(">");
740	}
741}
742
743/*
744 * Copy the ssid string contents into buf, truncating to fit.  If the
745 * ssid is entirely printable then just copy intact.  Otherwise convert
746 * to hexadecimal.  If the result is truncated then replace the last
747 * three characters with "...".
748 */
749static int
750copy_essid(char buf[], size_t bufsize, const u_int8_t *essid, size_t essid_len)
751{
752	const u_int8_t *p;
753	size_t maxlen;
754	int i;
755
756	if (essid_len > bufsize)
757		maxlen = bufsize;
758	else
759		maxlen = essid_len;
760	/* determine printable or not */
761	for (i = 0, p = essid; i < maxlen; i++, p++) {
762		if (*p < ' ' || *p > 0x7e)
763			break;
764	}
765	if (i != maxlen) {		/* not printable, print as hex */
766		if (bufsize < 3)
767			return 0;
768		strlcpy(buf, "0x", bufsize);
769		bufsize -= 2;
770		p = essid;
771		for (i = 0; i < maxlen && bufsize >= 2; i++) {
772			sprintf(&buf[2+2*i], "%02x", p[i]);
773			bufsize -= 2;
774		}
775		if (i != essid_len)
776			memcpy(&buf[2+2*i-3], "...", 3);
777	} else {			/* printable, truncate as needed */
778		memcpy(buf, essid, maxlen);
779		if (maxlen != essid_len)
780			memcpy(&buf[maxlen-3], "...", 3);
781	}
782	return maxlen;
783}
784
785/* unaligned little endian access */
786#define LE_READ_4(p)					\
787	((u_int32_t)					\
788	 ((((const u_int8_t *)(p))[0]      ) |		\
789	  (((const u_int8_t *)(p))[1] <<  8) |		\
790	  (((const u_int8_t *)(p))[2] << 16) |		\
791	  (((const u_int8_t *)(p))[3] << 24)))
792
793static int __inline
794iswpaoui(const u_int8_t *frm)
795{
796	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
797}
798
799static int __inline
800iswmeoui(const u_int8_t *frm)
801{
802	return frm[1] > 3 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI);
803}
804
805static int __inline
806isatherosoui(const u_int8_t *frm)
807{
808	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
809}
810
811static void
812printies(const u_int8_t *vp, int ielen, int maxcols)
813{
814	while (ielen > 0) {
815		switch (vp[0]) {
816		case IEEE80211_ELEMID_VENDOR:
817			if (iswpaoui(vp))
818				printie(" WPA", vp, 2+vp[1], maxcols);
819			else if (iswmeoui(vp))
820				printie(" WME", vp, 2+vp[1], maxcols);
821			else if (isatherosoui(vp))
822				printie(" ATH", vp, 2+vp[1], maxcols);
823			else
824				printie(" VEN", vp, 2+vp[1], maxcols);
825			break;
826		case IEEE80211_ELEMID_RSN:
827			printie(" RSN", vp, 2+vp[1], maxcols);
828			break;
829		default:
830			printie(" ???", vp, 2+vp[1], maxcols);
831			break;
832		}
833		ielen -= 2+vp[1];
834		vp += 2+vp[1];
835	}
836}
837
838static void
839list_scan(int s)
840{
841	uint8_t buf[24*1024];
842	struct ieee80211req ireq;
843	char ssid[14];
844	uint8_t *cp;
845	int len;
846
847	(void) memset(&ireq, 0, sizeof(ireq));
848	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
849	ireq.i_type = IEEE80211_IOC_SCAN_RESULTS;
850	ireq.i_data = buf;
851	ireq.i_len = sizeof(buf);
852	if (ioctl(s, SIOCG80211, &ireq) < 0)
853		errx(1, "unable to get scan results");
854	len = ireq.i_len;
855	if (len < sizeof(struct ieee80211req_scan_result))
856		return;
857
858	printf("%-14.14s  %-17.17s  %4s %4s  %-5s %3s %4s\n"
859		, "SSID"
860		, "BSSID"
861		, "CHAN"
862		, "RATE"
863		, "S:N"
864		, "INT"
865		, "CAPS"
866	);
867	cp = buf;
868	do {
869		struct ieee80211req_scan_result *sr;
870		uint8_t *vp;
871
872		sr = (struct ieee80211req_scan_result *) cp;
873		vp = (u_int8_t *)(sr+1);
874		printf("%-14.*s  %s  %3d  %3dM %2d:%-2d  %3d %-4.4s"
875			, copy_essid(ssid, sizeof(ssid), vp, sr->isr_ssid_len)
876				, ssid
877			, ether_ntoa((const struct ether_addr *) sr->isr_bssid)
878			, ieee80211_mhz2ieee(sr->isr_freq)
879			, getmaxrate(sr->isr_rates, sr->isr_nrates)
880			, sr->isr_rssi, sr->isr_noise
881			, sr->isr_intval
882			, getcaps(sr->isr_capinfo)
883		);
884		printies(vp + sr->isr_ssid_len, sr->isr_ie_len, 24);;
885		printf("\n");
886		cp += sr->isr_len, len -= sr->isr_len;
887	} while (len >= sizeof(struct ieee80211req_scan_result));
888}
889
890#include <net80211/ieee80211_freebsd.h>
891
892static void
893scan_and_wait(int s)
894{
895	struct ieee80211req ireq;
896	int sroute;
897
898	sroute = socket(PF_ROUTE, SOCK_RAW, 0);
899	if (sroute < 0) {
900		perror("socket(PF_ROUTE,SOCK_RAW)");
901		return;
902	}
903	(void) memset(&ireq, 0, sizeof(ireq));
904	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
905	ireq.i_type = IEEE80211_IOC_SCAN_REQ;
906	/* NB: only root can trigger a scan so ignore errors */
907	if (ioctl(s, SIOCS80211, &ireq) >= 0) {
908		char buf[2048];
909		struct if_announcemsghdr *ifan;
910		struct rt_msghdr *rtm;
911
912		do {
913			if (read(sroute, buf, sizeof(buf)) < 0) {
914				perror("read(PF_ROUTE)");
915				break;
916			}
917			rtm = (struct rt_msghdr *) buf;
918			if (rtm->rtm_version != RTM_VERSION)
919				break;
920			ifan = (struct if_announcemsghdr *) rtm;
921		} while (rtm->rtm_type != RTM_IEEE80211 ||
922		    ifan->ifan_what != RTM_IEEE80211_SCAN);
923	}
924	close(sroute);
925}
926
927static
928DECL_CMD_FUNC(set80211scan, val, d)
929{
930	scan_and_wait(s);
931	list_scan(s);
932}
933
934static void
935list_stations(int s)
936{
937	uint8_t buf[24*1024];
938	struct ieee80211req ireq;
939	uint8_t *cp;
940	int len;
941
942	(void) memset(&ireq, 0, sizeof(ireq));
943	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
944	ireq.i_type = IEEE80211_IOC_STA_INFO;
945	ireq.i_data = buf;
946	ireq.i_len = sizeof(buf);
947	if (ioctl(s, SIOCG80211, &ireq) < 0)
948		errx(1, "unable to get station information");
949	len = ireq.i_len;
950	if (len < sizeof(struct ieee80211req_sta_info))
951		return;
952
953	printf("%-17.17s %4s %4s %4s %4s %4s %6s %6s %4s %3s\n"
954		, "ADDR"
955		, "AID"
956		, "CHAN"
957		, "RATE"
958		, "RSSI"
959		, "IDLE"
960		, "TXSEQ"
961		, "RXSEQ"
962		, "CAPS"
963		, "ERP"
964	);
965	cp = buf;
966	do {
967		struct ieee80211req_sta_info *si;
968		uint8_t *vp;
969
970		si = (struct ieee80211req_sta_info *) cp;
971		vp = (u_int8_t *)(si+1);
972		printf("%s %4u %4d %3dM %4d %4d %6d %6d %-4.4s %3x"
973			, ether_ntoa((const struct ether_addr*) si->isi_macaddr)
974			, IEEE80211_AID(si->isi_associd)
975			, ieee80211_mhz2ieee(si->isi_freq)
976			, (si->isi_rates[si->isi_txrate] & IEEE80211_RATE_VAL)/2
977			, si->isi_rssi
978			, si->isi_inact
979			, si->isi_txseqs[0]
980			, si->isi_rxseqs[0]
981			, getcaps(si->isi_capinfo)
982			, si->isi_erp
983		);
984		printies(vp, si->isi_ie_len, 24);
985		printf("\n");
986		cp += si->isi_len, len -= si->isi_len;
987	} while (len >= sizeof(struct ieee80211req_sta_info));
988}
989
990static void
991print_chaninfo(const struct ieee80211_channel *c)
992{
993#define	IEEE80211_IS_CHAN_PASSIVE(_c) \
994	(((_c)->ic_flags & IEEE80211_CHAN_PASSIVE))
995	char buf[14];
996
997	buf[0] = '\0';
998	if (IEEE80211_IS_CHAN_FHSS(c))
999		strlcat(buf, " FHSS", sizeof(buf));
1000	if (IEEE80211_IS_CHAN_A(c))
1001		strlcat(buf, " 11a", sizeof(buf));
1002	/* XXX 11g schizophrenia */
1003	if (IEEE80211_IS_CHAN_G(c) ||
1004	    IEEE80211_IS_CHAN_PUREG(c))
1005		strlcat(buf, " 11g", sizeof(buf));
1006	else if (IEEE80211_IS_CHAN_B(c))
1007		strlcat(buf, " 11b", sizeof(buf));
1008	if (IEEE80211_IS_CHAN_T(c))
1009		strlcat(buf, " Turbo", sizeof(buf));
1010	printf("Channel %3u : %u%c Mhz%-14.14s",
1011		ieee80211_mhz2ieee(c->ic_freq), c->ic_freq,
1012		IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ', buf);
1013#undef IEEE80211_IS_CHAN_PASSIVE
1014}
1015
1016static void
1017list_channels(int s, int allchans)
1018{
1019	struct ieee80211req ireq;
1020	struct ieee80211req_chaninfo chans;
1021	struct ieee80211req_chaninfo achans;
1022	const struct ieee80211_channel *c;
1023	int i, half;
1024
1025	(void) memset(&ireq, 0, sizeof(ireq));
1026	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
1027	ireq.i_type = IEEE80211_IOC_CHANINFO;
1028	ireq.i_data = &chans;
1029	ireq.i_len = sizeof(chans);
1030	if (ioctl(s, SIOCG80211, &ireq) < 0)
1031		errx(1, "unable to get channel information");
1032	if (!allchans) {
1033		struct ieee80211req_chanlist active;
1034
1035		ireq.i_type = IEEE80211_IOC_CHANLIST;
1036		ireq.i_data = &active;
1037		ireq.i_len = sizeof(active);
1038		if (ioctl(s, SIOCG80211, &ireq) < 0)
1039			errx(1, "unable to get active channel list");
1040		memset(&achans, 0, sizeof(achans));
1041		for (i = 0; i < chans.ic_nchans; i++) {
1042			c = &chans.ic_chans[i];
1043			if (isset(active.ic_channels, ieee80211_mhz2ieee(c->ic_freq)) || allchans)
1044				achans.ic_chans[achans.ic_nchans++] = *c;
1045		}
1046	} else
1047		achans = chans;
1048	half = achans.ic_nchans / 2;
1049	if (achans.ic_nchans % 2)
1050		half++;
1051	for (i = 0; i < achans.ic_nchans / 2; i++) {
1052		print_chaninfo(&achans.ic_chans[i]);
1053		print_chaninfo(&achans.ic_chans[half+i]);
1054		printf("\n");
1055	}
1056	if (achans.ic_nchans % 2) {
1057		print_chaninfo(&achans.ic_chans[i]);
1058		printf("\n");
1059	}
1060}
1061
1062static void
1063list_keys(int s)
1064{
1065}
1066
1067#define	IEEE80211_C_BITS \
1068"\020\1WEP\2TKIP\3AES\4AES_CCM\6CKIP\11IBSS\12PMGT\13HOSTAP\14AHDEMO" \
1069"\15SWRETRY\16TXPMGT\17SHSLOT\20SHPREAMBLE\21MONITOR\22TKIPMIC\30WPA1" \
1070"\31WPA2\32BURST\33WME"
1071
1072static void
1073list_capabilities(int s)
1074{
1075	struct ieee80211req ireq;
1076	u_int32_t caps;
1077
1078	(void) memset(&ireq, 0, sizeof(ireq));
1079	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
1080	ireq.i_type = IEEE80211_IOC_DRIVER_CAPS;
1081	if (ioctl(s, SIOCG80211, &ireq) < 0)
1082		errx(1, "unable to get driver capabilities");
1083	caps = (((u_int16_t) ireq.i_val) << 16) | ((u_int16_t) ireq.i_len);
1084	printb(name, caps, IEEE80211_C_BITS);
1085	putchar('\n');
1086}
1087
1088static void
1089list_wme(int s)
1090{
1091	static const char *acnames[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" };
1092	struct ieee80211req ireq;
1093	int ac;
1094
1095	(void) memset(&ireq, 0, sizeof(ireq));
1096	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
1097	ireq.i_len = 0;
1098	for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++) {
1099again:
1100		if (ireq.i_len & IEEE80211_WMEPARAM_BSS)
1101			printf("\t%s", "     ");
1102		else
1103			printf("\t%s", acnames[ac]);
1104
1105		ireq.i_len = (ireq.i_len & IEEE80211_WMEPARAM_BSS) | ac;
1106
1107		/* show WME BSS parameters */
1108		ireq.i_type = IEEE80211_IOC_WME_CWMIN;
1109		if (ioctl(s, SIOCG80211, &ireq) != -1)
1110			printf(" cwmin %2u", ireq.i_val);
1111		ireq.i_type = IEEE80211_IOC_WME_CWMAX;
1112		if (ioctl(s, SIOCG80211, &ireq) != -1)
1113			printf(" cwmax %2u", ireq.i_val);
1114		ireq.i_type = IEEE80211_IOC_WME_AIFS;
1115		if (ioctl(s, SIOCG80211, &ireq) != -1)
1116			printf(" aifs %2u", ireq.i_val);
1117		ireq.i_type = IEEE80211_IOC_WME_TXOPLIMIT;
1118		if (ioctl(s, SIOCG80211, &ireq) != -1)
1119			printf(" txopLimit %3u", ireq.i_val);
1120		ireq.i_type = IEEE80211_IOC_WME_ACM;
1121		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1122			if (ireq.i_val)
1123				printf(" acm");
1124			else if (verbose)
1125				printf(" -acm");
1126		}
1127		/* !BSS only */
1128		if ((ireq.i_len & IEEE80211_WMEPARAM_BSS) == 0) {
1129			ireq.i_type = IEEE80211_IOC_WME_ACKPOLICY;
1130			if (ioctl(s, SIOCG80211, &ireq) != -1) {
1131				if (!ireq.i_val)
1132					printf(" -ack");
1133				else if (verbose)
1134					printf(" ack");
1135			}
1136		}
1137		printf("\n");
1138		if ((ireq.i_len & IEEE80211_WMEPARAM_BSS) == 0) {
1139			ireq.i_len |= IEEE80211_WMEPARAM_BSS;
1140			goto again;
1141		} else
1142			ireq.i_len &= ~IEEE80211_WMEPARAM_BSS;
1143	}
1144}
1145
1146static void
1147list_mac(int s)
1148{
1149	struct ieee80211req ireq;
1150	struct ieee80211req_maclist *acllist;
1151	int i, nacls, policy;
1152	char c;
1153
1154	(void) memset(&ireq, 0, sizeof(ireq));
1155	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); /* XXX ?? */
1156	ireq.i_type = IEEE80211_IOC_MACCMD;
1157	ireq.i_val = IEEE80211_MACCMD_POLICY;
1158	if (ioctl(s, SIOCG80211, &ireq) < 0) {
1159		if (errno == EINVAL) {
1160			printf("No acl policy loaded\n");
1161			return;
1162		}
1163		err(1, "unable to get mac policy");
1164	}
1165	policy = ireq.i_val;
1166
1167	ireq.i_val = IEEE80211_MACCMD_LIST;
1168	ireq.i_len = 0;
1169	if (ioctl(s, SIOCG80211, &ireq) < 0)
1170		err(1, "unable to get mac acl list size");
1171	if (ireq.i_len == 0)		/* NB: no acls */
1172		return;
1173
1174	ireq.i_data = malloc(ireq.i_len);
1175	if (ireq.i_data == NULL)
1176		err(1, "out of memory for acl list");
1177
1178	if (ioctl(s, SIOCG80211, &ireq) < 0)
1179		err(1, "unable to get mac acl list");
1180	if (policy == IEEE80211_MACCMD_POLICY_OPEN) {
1181		if (verbose)
1182			printf("policy: open\n");
1183		c = '*';
1184	} else if (policy == IEEE80211_MACCMD_POLICY_ALLOW) {
1185		if (verbose)
1186			printf("policy: allow\n");
1187		c = '+';
1188	} else if (policy == IEEE80211_MACCMD_POLICY_DENY) {
1189		if (verbose)
1190			printf("policy: deny\n");
1191		c = '-';
1192	} else {
1193		printf("policy: unknown (%u)\n", policy);
1194		c = '?';
1195	}
1196	nacls = ireq.i_len / sizeof(*acllist);
1197	acllist = (struct ieee80211req_maclist *) ireq.i_data;
1198	for (i = 0; i < nacls; i++)
1199		printf("%c%s\n", c, ether_ntoa(
1200			(const struct ether_addr *) acllist[i].ml_macaddr));
1201}
1202
1203static
1204DECL_CMD_FUNC(set80211list, arg, d)
1205{
1206#define	iseq(a,b)	(strncasecmp(a,b,sizeof(b)-1) == 0)
1207
1208	if (iseq(arg, "sta"))
1209		list_stations(s);
1210	else if (iseq(arg, "scan") || iseq(arg, "ap"))
1211		list_scan(s);
1212	else if (iseq(arg, "chan") || iseq(arg, "freq"))
1213		list_channels(s, 1);
1214	else if (iseq(arg, "active"))
1215		list_channels(s, 0);
1216	else if (iseq(arg, "keys"))
1217		list_keys(s);
1218	else if (iseq(arg, "caps"))
1219		list_capabilities(s);
1220	else if (iseq(arg, "wme"))
1221		list_wme(s);
1222	else if (iseq(arg, "mac"))
1223		list_mac(s);
1224	else
1225		errx(1, "Don't know how to list %s for %s", arg, name);
1226#undef iseq
1227}
1228
1229static enum ieee80211_opmode
1230get80211opmode(int s)
1231{
1232	struct ifmediareq ifmr;
1233
1234	(void) memset(&ifmr, 0, sizeof(ifmr));
1235	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
1236
1237	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
1238		if (ifmr.ifm_current & IFM_IEEE80211_ADHOC)
1239			return IEEE80211_M_IBSS;	/* XXX ahdemo */
1240		if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
1241			return IEEE80211_M_HOSTAP;
1242		if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
1243			return IEEE80211_M_MONITOR;
1244	}
1245	return IEEE80211_M_STA;
1246}
1247
1248static const struct ieee80211_channel *
1249getchaninfo(int s, int chan)
1250{
1251	struct ieee80211req ireq;
1252	static struct ieee80211req_chaninfo chans;
1253	static struct ieee80211_channel undef;
1254	const struct ieee80211_channel *c;
1255	int i, freq;
1256
1257	(void) memset(&ireq, 0, sizeof(ireq));
1258	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
1259	ireq.i_type = IEEE80211_IOC_CHANINFO;
1260	ireq.i_data = &chans;
1261	ireq.i_len = sizeof(chans);
1262	if (ioctl(s, SIOCG80211, &ireq) < 0)
1263		errx(1, "unable to get channel information");
1264	freq = ieee80211_ieee2mhz(chan);
1265	for (i = 0; i < chans.ic_nchans; i++) {
1266		c = &chans.ic_chans[i];
1267		if (c->ic_freq == freq)
1268			return c;
1269	}
1270	return &undef;
1271}
1272
1273#if 0
1274static void
1275printcipher(int s, struct ieee80211req *ireq, int keylenop)
1276{
1277	switch (ireq->i_val) {
1278	case IEEE80211_CIPHER_WEP:
1279		ireq->i_type = keylenop;
1280		if (ioctl(s, SIOCG80211, ireq) != -1)
1281			printf("WEP-%s",
1282			    ireq->i_len <= 5 ? "40" :
1283			    ireq->i_len <= 13 ? "104" : "128");
1284		else
1285			printf("WEP");
1286		break;
1287	case IEEE80211_CIPHER_TKIP:
1288		printf("TKIP");
1289		break;
1290	case IEEE80211_CIPHER_AES_OCB:
1291		printf("AES-OCB");
1292		break;
1293	case IEEE80211_CIPHER_AES_CCM:
1294		printf("AES-CCM");
1295		break;
1296	case IEEE80211_CIPHER_CKIP:
1297		printf("CKIP");
1298		break;
1299	case IEEE80211_CIPHER_NONE:
1300		printf("NONE");
1301		break;
1302	default:
1303		printf("UNKNOWN (0x%x)", ireq->i_val);
1304		break;
1305	}
1306}
1307#endif
1308
1309#define	MAXCOL	78
1310int	col;
1311char	spacer;
1312
1313#define	LINE_BREAK() do {			\
1314	if (spacer != '\t') {			\
1315		printf("\n");			\
1316		spacer = '\t';			\
1317	}					\
1318	col = 8;	/* 8-col tab */		\
1319} while (0)
1320#define	LINE_CHECK(fmt, ...) do {		\
1321	col += sizeof(fmt)-2;			\
1322	if (col > MAXCOL) {			\
1323		LINE_BREAK();			\
1324		col += sizeof(fmt)-2;		\
1325	}					\
1326	printf(fmt, __VA_ARGS__);		\
1327	spacer = ' ';				\
1328} while (0)
1329
1330static void
1331printkey(const struct ieee80211req_key *ik)
1332{
1333	static const uint8_t zerodata[IEEE80211_KEYBUF_SIZE];
1334	int keylen = ik->ik_keylen;
1335	int printcontents;
1336
1337	printcontents = printkeys &&
1338		(memcmp(ik->ik_keydata, zerodata, keylen) != 0 || verbose);
1339	if (printcontents)
1340		LINE_BREAK();
1341	switch (ik->ik_type) {
1342	case IEEE80211_CIPHER_WEP:
1343		/* compatibility */
1344		LINE_CHECK("%cwepkey %u:%s", spacer, ik->ik_keyix+1,
1345		    keylen <= 5 ? "40-bit" :
1346		    keylen <= 13 ? "104-bit" : "128-bit");
1347		break;
1348	case IEEE80211_CIPHER_TKIP:
1349		if (keylen > 128/8)
1350			keylen -= 128/8;	/* ignore MIC for now */
1351		LINE_CHECK("%cTKIP %u:%u-bit",
1352			spacer, ik->ik_keyix+1, 8*keylen);
1353		break;
1354	case IEEE80211_CIPHER_AES_OCB:
1355		LINE_CHECK("%cAES-OCB %u:%u-bit",
1356			spacer, ik->ik_keyix+1, 8*keylen);
1357		break;
1358	case IEEE80211_CIPHER_AES_CCM:
1359		LINE_CHECK("%cAES-CCM %u:%u-bit",
1360			spacer, ik->ik_keyix+1, 8*keylen);
1361		break;
1362	case IEEE80211_CIPHER_CKIP:
1363		LINE_CHECK("%cCKIP %u:%u-bit",
1364			spacer, ik->ik_keyix+1, 8*keylen);
1365		break;
1366	case IEEE80211_CIPHER_NONE:
1367		LINE_CHECK("%cNULL %u:%u-bit",
1368			spacer, ik->ik_keyix+1, 8*keylen);
1369		break;
1370	default:
1371		LINE_CHECK("%cUNKNOWN (0x%x) %u:%u-bit", spacer,
1372			ik->ik_type, ik->ik_keyix+1, 8*keylen);
1373		break;
1374	}
1375	if (printcontents) {
1376		int i;
1377
1378		printf(" <");
1379		for (i = 0; i < keylen; i++)
1380			printf("%02x", ik->ik_keydata[i]);
1381		printf(">");
1382		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
1383		    (ik->ik_keyrsc != 0 || verbose))
1384			printf(" rsc %ju", (uintmax_t)ik->ik_keyrsc);
1385		if (ik->ik_type != IEEE80211_CIPHER_WEP &&
1386		    (ik->ik_keytsc != 0 || verbose))
1387			printf(" tsc %ju", (uintmax_t)ik->ik_keytsc);
1388		if (ik->ik_flags != 0 && verbose) {
1389			const char *sep = " ";
1390
1391			if (ik->ik_flags & IEEE80211_KEY_XMIT)
1392				printf("%stx", sep), sep = "+";
1393			if (ik->ik_flags & IEEE80211_KEY_RECV)
1394				printf("%srx", sep), sep = "+";
1395			if (ik->ik_flags & IEEE80211_KEY_DEFAULT)
1396				printf("%sdef", sep), sep = "+";
1397		}
1398		LINE_BREAK();
1399	}
1400}
1401
1402static void
1403ieee80211_status(int s)
1404{
1405	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
1406	enum ieee80211_opmode opmode = get80211opmode(s);
1407	int i, num, wpa, wme;
1408	struct ieee80211req ireq;
1409	u_int8_t data[32];
1410	const struct ieee80211_channel *c;
1411
1412	(void) memset(&ireq, 0, sizeof(ireq));
1413	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
1414	ireq.i_data = &data;
1415
1416	wpa = 0;		/* unknown/not set */
1417
1418	ireq.i_type = IEEE80211_IOC_SSID;
1419	ireq.i_val = -1;
1420	if (ioctl(s, SIOCG80211, &ireq) < 0) {
1421		/* If we can't get the SSID, this isn't an 802.11 device. */
1422		return;
1423	}
1424	num = 0;
1425	ireq.i_type = IEEE80211_IOC_NUMSSIDS;
1426	if (ioctl(s, SIOCG80211, &ireq) >= 0)
1427		num = ireq.i_val;
1428	printf("\tssid ");
1429	if (num > 1) {
1430		ireq.i_type = IEEE80211_IOC_SSID;
1431		for (ireq.i_val = 0; ireq.i_val < num; ireq.i_val++) {
1432			if (ioctl(s, SIOCG80211, &ireq) >= 0 && ireq.i_len > 0) {
1433				printf(" %d:", ireq.i_val + 1);
1434				print_string(data, ireq.i_len);
1435			}
1436		}
1437	} else
1438		print_string(data, ireq.i_len);
1439
1440	ireq.i_type = IEEE80211_IOC_CHANNEL;
1441	if (ioctl(s, SIOCG80211, &ireq) < 0)
1442		goto end;
1443	c = getchaninfo(s, ireq.i_val);
1444	if (ireq.i_val != -1) {
1445		printf(" channel %d", ireq.i_val);
1446		if (verbose)
1447			printf(" (%u)", c->ic_freq);
1448	} else if (verbose)
1449		printf(" channel UNDEF");
1450
1451	ireq.i_type = IEEE80211_IOC_BSSID;
1452	ireq.i_len = IEEE80211_ADDR_LEN;
1453	if (ioctl(s, SIOCG80211, &ireq) >= 0 &&
1454	    (memcmp(ireq.i_data, zerobssid, sizeof(zerobssid)) != 0 || verbose))
1455		printf(" bssid %s", ether_ntoa(ireq.i_data));
1456
1457	ireq.i_type = IEEE80211_IOC_STATIONNAME;
1458	if (ioctl(s, SIOCG80211, &ireq) != -1) {
1459		printf("\n\tstationname ");
1460		print_string(data, ireq.i_len);
1461	}
1462
1463	spacer = ' ';		/* force first break */
1464	LINE_BREAK();
1465
1466	ireq.i_type = IEEE80211_IOC_AUTHMODE;
1467	if (ioctl(s, SIOCG80211, &ireq) != -1) {
1468		switch (ireq.i_val) {
1469			case IEEE80211_AUTH_NONE:
1470				LINE_CHECK("%cauthmode NONE", spacer);
1471				break;
1472			case IEEE80211_AUTH_OPEN:
1473				LINE_CHECK("%cauthmode OPEN", spacer);
1474				break;
1475			case IEEE80211_AUTH_SHARED:
1476				LINE_CHECK("%cauthmode SHARED", spacer);
1477				break;
1478			case IEEE80211_AUTH_8021X:
1479				LINE_CHECK("%cauthmode 802.1x", spacer);
1480				break;
1481			case IEEE80211_AUTH_WPA:
1482				ireq.i_type = IEEE80211_IOC_WPA;
1483				if (ioctl(s, SIOCG80211, &ireq) != -1)
1484					wpa = ireq.i_val;
1485				if (!wpa)
1486					wpa = 1;	/* default to WPA1 */
1487				switch (wpa) {
1488				case 2:
1489					LINE_CHECK("%cauthmode WPA2/802.11i",
1490						spacer);
1491					break;
1492				case 3:
1493					LINE_CHECK("%cauthmode WPA1+WPA2/802.11i",
1494						spacer);
1495					break;
1496				default:
1497					LINE_CHECK("%cauthmode WPA", spacer);
1498					break;
1499				}
1500				break;
1501			case IEEE80211_AUTH_AUTO:
1502				LINE_CHECK("%cauthmode AUTO", spacer);
1503				break;
1504			default:
1505				LINE_CHECK("%cauthmode UNKNOWN (0x%x)",
1506					spacer, ireq.i_val);
1507				break;
1508		}
1509	}
1510
1511	ireq.i_type = IEEE80211_IOC_WEP;
1512	if (ioctl(s, SIOCG80211, &ireq) != -1 &&
1513	    ireq.i_val != IEEE80211_WEP_NOSUP) {
1514		int firstkey, wepmode;
1515
1516		wepmode = ireq.i_val;
1517		switch (wepmode) {
1518			case IEEE80211_WEP_OFF:
1519				LINE_CHECK("%cprivacy OFF", spacer);
1520				break;
1521			case IEEE80211_WEP_ON:
1522				LINE_CHECK("%cprivacy ON", spacer);
1523				break;
1524			case IEEE80211_WEP_MIXED:
1525				LINE_CHECK("%cprivacy MIXED", spacer);
1526				break;
1527			default:
1528				LINE_CHECK("%cprivacy UNKNOWN (0x%x)",
1529					spacer, wepmode);
1530				break;
1531		}
1532
1533		/*
1534		 * If we get here then we've got WEP support so we need
1535		 * to print WEP status.
1536		 */
1537
1538		ireq.i_type = IEEE80211_IOC_WEPTXKEY;
1539		if (ioctl(s, SIOCG80211, &ireq) < 0) {
1540			warn("WEP support, but no tx key!");
1541			goto end;
1542		}
1543		if (ireq.i_val != -1)
1544			LINE_CHECK("%cdeftxkey %d", spacer, ireq.i_val+1);
1545		else if (wepmode != IEEE80211_WEP_OFF || verbose)
1546			LINE_CHECK("%cdeftxkey UNDEF", spacer);
1547
1548		ireq.i_type = IEEE80211_IOC_NUMWEPKEYS;
1549		if (ioctl(s, SIOCG80211, &ireq) < 0) {
1550			warn("WEP support, but no NUMWEPKEYS support!");
1551			goto end;
1552		}
1553		num = ireq.i_val;
1554
1555		firstkey = 1;
1556		for (i = 0; i < num; i++) {
1557			struct ieee80211req_key ik;
1558
1559			memset(&ik, 0, sizeof(ik));
1560			ik.ik_keyix = i;
1561			ireq.i_type = IEEE80211_IOC_WPAKEY;
1562			ireq.i_data = &ik;
1563			ireq.i_len = sizeof(ik);
1564			if (ioctl(s, SIOCG80211, &ireq) < 0) {
1565				warn("WEP support, but can get keys!");
1566				goto end;
1567			}
1568			if (ik.ik_keylen != 0) {
1569				if (verbose)
1570					LINE_BREAK();
1571				printkey(&ik);
1572				firstkey = 0;
1573			}
1574		}
1575	}
1576
1577	ireq.i_type = IEEE80211_IOC_POWERSAVE;
1578	if (ioctl(s, SIOCG80211, &ireq) != -1 &&
1579	    ireq.i_val != IEEE80211_POWERSAVE_NOSUP ) {
1580		if (ireq.i_val != IEEE80211_POWERSAVE_OFF || verbose) {
1581			switch (ireq.i_val) {
1582				case IEEE80211_POWERSAVE_OFF:
1583					LINE_CHECK("%cpowersavemode OFF",
1584						spacer);
1585					break;
1586				case IEEE80211_POWERSAVE_CAM:
1587					LINE_CHECK("%cpowersavemode CAM",
1588						spacer);
1589					break;
1590				case IEEE80211_POWERSAVE_PSP:
1591					LINE_CHECK("%cpowersavemode PSP",
1592						spacer);
1593					break;
1594				case IEEE80211_POWERSAVE_PSP_CAM:
1595					LINE_CHECK("%cpowersavemode PSP-CAM",
1596						spacer);
1597					break;
1598			}
1599			ireq.i_type = IEEE80211_IOC_POWERSAVESLEEP;
1600			if (ioctl(s, SIOCG80211, &ireq) != -1)
1601				LINE_CHECK("%cpowersavesleep %d",
1602					spacer, ireq.i_val);
1603		}
1604	}
1605
1606	ireq.i_type = IEEE80211_IOC_TXPOWMAX;
1607	if (ioctl(s, SIOCG80211, &ireq) != -1)
1608		LINE_CHECK("%ctxpowmax %d", spacer, ireq.i_val);
1609
1610	if (verbose) {
1611		ireq.i_type = IEEE80211_IOC_TXPOWER;
1612		if (ioctl(s, SIOCG80211, &ireq) != -1)
1613			LINE_CHECK("%ctxpower %d", spacer, ireq.i_val);
1614	}
1615
1616	ireq.i_type = IEEE80211_IOC_RTSTHRESHOLD;
1617	if (ioctl(s, SIOCG80211, &ireq) != -1) {
1618		if (ireq.i_val != IEEE80211_RTS_MAX || verbose)
1619			LINE_CHECK("%crtsthreshold %d", spacer, ireq.i_val);
1620	}
1621
1622	ireq.i_type = IEEE80211_IOC_MCAST_RATE;
1623	if (ioctl(s, SIOCG80211, &ireq) != -1) {
1624		if (ireq.i_val != 2*1 || verbose) {
1625			if (ireq.i_val == 11)
1626				LINE_CHECK("%cmcastrate 5.5", spacer);
1627			else
1628				LINE_CHECK("%cmcastrate %d", spacer,
1629					ireq.i_val/2);
1630		}
1631	}
1632
1633	ireq.i_type = IEEE80211_IOC_FRAGTHRESHOLD;
1634	if (ioctl(s, SIOCG80211, &ireq) != -1) {
1635		if (ireq.i_val != IEEE80211_FRAG_MAX || verbose)
1636			LINE_CHECK("%cfragthreshold %d", spacer, ireq.i_val);
1637	}
1638
1639	if (IEEE80211_IS_CHAN_G(c) || IEEE80211_IS_CHAN_PUREG(c) || verbose) {
1640		ireq.i_type = IEEE80211_IOC_PUREG;
1641		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1642			if (ireq.i_val)
1643				LINE_CHECK("%cpureg", spacer);
1644			else if (verbose)
1645				LINE_CHECK("%c-pureg", spacer);
1646		}
1647		ireq.i_type = IEEE80211_IOC_PROTMODE;
1648		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1649			switch (ireq.i_val) {
1650				case IEEE80211_PROTMODE_OFF:
1651					LINE_CHECK("%cprotmode OFF", spacer);
1652					break;
1653				case IEEE80211_PROTMODE_CTS:
1654					LINE_CHECK("%cprotmode CTS", spacer);
1655					break;
1656				case IEEE80211_PROTMODE_RTSCTS:
1657					LINE_CHECK("%cprotmode RTSCTS", spacer);
1658					break;
1659				default:
1660					LINE_CHECK("%cprotmode UNKNOWN (0x%x)",
1661						spacer, ireq.i_val);
1662					break;
1663			}
1664		}
1665	}
1666
1667	ireq.i_type = IEEE80211_IOC_WME;
1668	if (ioctl(s, SIOCG80211, &ireq) != -1) {
1669		wme = ireq.i_val;
1670		if (wme)
1671			LINE_CHECK("%cwme", spacer);
1672		else if (verbose)
1673			LINE_CHECK("%c-wme", spacer);
1674	} else
1675		wme = 0;
1676
1677	if (opmode == IEEE80211_M_HOSTAP) {
1678		ireq.i_type = IEEE80211_IOC_HIDESSID;
1679		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1680			if (ireq.i_val)
1681				LINE_CHECK("%cssid HIDE", spacer);
1682			else if (verbose)
1683				LINE_CHECK("%cssid SHOW", spacer);
1684		}
1685
1686		ireq.i_type = IEEE80211_IOC_APBRIDGE;
1687		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1688			if (!ireq.i_val)
1689				LINE_CHECK("%c-apbridge", spacer);
1690			else if (verbose)
1691				LINE_CHECK("%capbridge", spacer);
1692		}
1693
1694		ireq.i_type = IEEE80211_IOC_DTIM_PERIOD;
1695		if (ioctl(s, SIOCG80211, &ireq) != -1)
1696			LINE_CHECK("%cdtimperiod %u", spacer, ireq.i_val);
1697	} else {
1698		ireq.i_type = IEEE80211_IOC_ROAMING;
1699		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1700			if (ireq.i_val != IEEE80211_ROAMING_AUTO || verbose) {
1701				switch (ireq.i_val) {
1702				case IEEE80211_ROAMING_DEVICE:
1703					LINE_CHECK("%croaming DEVICE", spacer);
1704					break;
1705				case IEEE80211_ROAMING_AUTO:
1706					LINE_CHECK("%croaming AUTO", spacer);
1707					break;
1708				case IEEE80211_ROAMING_MANUAL:
1709					LINE_CHECK("%croaming MANUAL", spacer);
1710					break;
1711				default:
1712					LINE_CHECK("%croaming UNKNOWN (0x%x)",
1713						spacer, ireq.i_val);
1714					break;
1715				}
1716			}
1717		}
1718	}
1719	ireq.i_type = IEEE80211_IOC_BEACON_INTERVAL;
1720	if (ioctl(s, SIOCG80211, &ireq) != -1) {
1721		if (ireq.i_val)
1722			LINE_CHECK("%cbintval %u", spacer, ireq.i_val);
1723		else if (verbose)
1724			LINE_CHECK("%cbintval %u", spacer, ireq.i_val);
1725	}
1726
1727	if (wme && verbose) {
1728		LINE_BREAK();
1729		list_wme(s);
1730	}
1731
1732	if (wpa) {
1733		ireq.i_type = IEEE80211_IOC_COUNTERMEASURES;
1734		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1735			if (ireq.i_val)
1736				LINE_CHECK("%ccountermeasures", spacer);
1737			else if (verbose)
1738				LINE_CHECK("%c-countermeasures", spacer);
1739		}
1740#if 0
1741		/* XXX not interesting with WPA done in user space */
1742		ireq.i_type = IEEE80211_IOC_KEYMGTALGS;
1743		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1744		}
1745
1746		ireq.i_type = IEEE80211_IOC_MCASTCIPHER;
1747		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1748			printf("%cmcastcipher ", spacer);
1749			printcipher(s, &ireq, IEEE80211_IOC_MCASTKEYLEN);
1750			spacer = ' ';
1751		}
1752
1753		ireq.i_type = IEEE80211_IOC_UCASTCIPHER;
1754		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1755			printf("%cucastcipher ", spacer);
1756			printcipher(s, &ireq, IEEE80211_IOC_UCASTKEYLEN);
1757		}
1758
1759		if (wpa & 2) {
1760			ireq.i_type = IEEE80211_IOC_RSNCAPS;
1761			if (ioctl(s, SIOCG80211, &ireq) != -1) {
1762				printf("%cRSN caps 0x%x", spacer, ireq.i_val);
1763				spacer = ' ';
1764			}
1765		}
1766
1767		ireq.i_type = IEEE80211_IOC_UCASTCIPHERS;
1768		if (ioctl(s, SIOCG80211, &ireq) != -1) {
1769		}
1770#endif
1771		LINE_BREAK();
1772	}
1773	LINE_BREAK();
1774
1775end:
1776	return;
1777}
1778
1779static void
1780set80211(int s, int type, int val, int len, u_int8_t *data)
1781{
1782	struct ieee80211req	ireq;
1783
1784	(void) memset(&ireq, 0, sizeof(ireq));
1785	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
1786	ireq.i_type = type;
1787	ireq.i_val = val;
1788	ireq.i_len = len;
1789	ireq.i_data = data;
1790	if (ioctl(s, SIOCS80211, &ireq) < 0)
1791		err(1, "SIOCS80211");
1792}
1793
1794static const char *
1795get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
1796{
1797	int len;
1798	int hexstr;
1799	u_int8_t *p;
1800
1801	len = *lenp;
1802	p = buf;
1803	hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
1804	if (hexstr)
1805		val += 2;
1806	for (;;) {
1807		if (*val == '\0')
1808			break;
1809		if (sep != NULL && strchr(sep, *val) != NULL) {
1810			val++;
1811			break;
1812		}
1813		if (hexstr) {
1814			if (!isxdigit((u_char)val[0])) {
1815				warnx("bad hexadecimal digits");
1816				return NULL;
1817			}
1818			if (!isxdigit((u_char)val[1])) {
1819				warnx("odd count hexadecimal digits");
1820				return NULL;
1821			}
1822		}
1823		if (p >= buf + len) {
1824			if (hexstr)
1825				warnx("hexadecimal digits too long");
1826			else
1827				warnx("string too long");
1828			return NULL;
1829		}
1830		if (hexstr) {
1831#define	tohex(x)	(isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
1832			*p++ = (tohex((u_char)val[0]) << 4) |
1833			    tohex((u_char)val[1]);
1834#undef tohex
1835			val += 2;
1836		} else
1837			*p++ = *val++;
1838	}
1839	len = p - buf;
1840	/* The string "-" is treated as the empty string. */
1841	if (!hexstr && len == 1 && buf[0] == '-')
1842		len = 0;
1843	if (len < *lenp)
1844		memset(p, 0, *lenp - len);
1845	*lenp = len;
1846	return val;
1847}
1848
1849static void
1850print_string(const u_int8_t *buf, int len)
1851{
1852	int i;
1853	int hasspc;
1854
1855	i = 0;
1856	hasspc = 0;
1857	for (; i < len; i++) {
1858		if (!isprint(buf[i]) && buf[i] != '\0')
1859			break;
1860		if (isspace(buf[i]))
1861			hasspc++;
1862	}
1863	if (i == len) {
1864		if (hasspc || len == 0 || buf[0] == '\0')
1865			printf("\"%.*s\"", len, buf);
1866		else
1867			printf("%.*s", len, buf);
1868	} else {
1869		printf("0x");
1870		for (i = 0; i < len; i++)
1871			printf("%02x", buf[i]);
1872	}
1873}
1874
1875static struct cmd ieee80211_cmds[] = {
1876	DEF_CMD_ARG("ssid",		set80211ssid),
1877	DEF_CMD_ARG("nwid",		set80211ssid),
1878	DEF_CMD_ARG("stationname",	set80211stationname),
1879	DEF_CMD_ARG("station",		set80211stationname),	/* BSD/OS */
1880	DEF_CMD_ARG("channel",		set80211channel),
1881	DEF_CMD_ARG("authmode",		set80211authmode),
1882	DEF_CMD_ARG("powersavemode",	set80211powersavemode),
1883	DEF_CMD("powersave",	1,	set80211powersave),
1884	DEF_CMD("-powersave",	0,	set80211powersave),
1885	DEF_CMD_ARG("powersavesleep", 	set80211powersavesleep),
1886	DEF_CMD_ARG("wepmode",		set80211wepmode),
1887	DEF_CMD("wep",		1,	set80211wep),
1888	DEF_CMD("-wep",		0,	set80211wep),
1889	DEF_CMD_ARG("deftxkey",		set80211weptxkey),
1890	DEF_CMD_ARG("weptxkey",		set80211weptxkey),
1891	DEF_CMD_ARG("wepkey",		set80211wepkey),
1892	DEF_CMD_ARG("nwkey",		set80211nwkey),		/* NetBSD */
1893	DEF_CMD("-nwkey",	0,	set80211wep),		/* NetBSD */
1894	DEF_CMD_ARG("rtsthreshold",	set80211rtsthreshold),
1895	DEF_CMD_ARG("protmode",		set80211protmode),
1896	DEF_CMD_ARG("txpower",		set80211txpower),
1897	DEF_CMD_ARG("roaming",		set80211roaming),
1898	DEF_CMD("wme",		1,	set80211wme),
1899	DEF_CMD("-wme",		0,	set80211wme),
1900	DEF_CMD("hidessid",	1,	set80211hidessid),
1901	DEF_CMD("-hidessid",	0,	set80211hidessid),
1902	DEF_CMD("apbridge",	1,	set80211apbridge),
1903	DEF_CMD("-apbridge",	0,	set80211apbridge),
1904	DEF_CMD_ARG("chanlist",		set80211chanlist),
1905	DEF_CMD_ARG("bssid",		set80211bssid),
1906	DEF_CMD_ARG("ap",		set80211bssid),
1907	DEF_CMD("scan",	0,		set80211scan),
1908	DEF_CMD_ARG("list",		set80211list),
1909	DEF_CMD_ARG2("cwmin",		set80211cwmin),
1910	DEF_CMD_ARG2("cwmax",		set80211cwmax),
1911	DEF_CMD_ARG2("aifs",		set80211aifs),
1912	DEF_CMD_ARG2("txoplimit",	set80211txoplimit),
1913	DEF_CMD_ARG("acm",		set80211acm),
1914	DEF_CMD_ARG("-acm",		set80211noacm),
1915	DEF_CMD_ARG("ack",		set80211ackpolicy),
1916	DEF_CMD_ARG("-ack",		set80211noackpolicy),
1917	DEF_CMD_ARG2("bss:cwmin",	set80211bsscwmin),
1918	DEF_CMD_ARG2("bss:cwmax",	set80211bsscwmax),
1919	DEF_CMD_ARG2("bss:aifs",	set80211bssaifs),
1920	DEF_CMD_ARG2("bss:txoplimit",	set80211bsstxoplimit),
1921	DEF_CMD_ARG("dtimperiod",	set80211dtimperiod),
1922	DEF_CMD_ARG("bintval",		set80211bintval),
1923	DEF_CMD("mac:open",	IEEE80211_MACCMD_POLICY_OPEN,	set80211maccmd),
1924	DEF_CMD("mac:allow",	IEEE80211_MACCMD_POLICY_ALLOW,	set80211maccmd),
1925	DEF_CMD("mac:deny",	IEEE80211_MACCMD_POLICY_DENY,	set80211maccmd),
1926	DEF_CMD("mac:flush",	IEEE80211_MACCMD_FLUSH,		set80211maccmd),
1927	DEF_CMD("mac:detach",	IEEE80211_MACCMD_DETACH,	set80211maccmd),
1928	DEF_CMD_ARG("mac:add",		set80211addmac),
1929	DEF_CMD_ARG("mac:del",		set80211delmac),
1930	DEF_CMD_ARG("mac:kick",		set80211kickmac),
1931	DEF_CMD("pureg",	1,	set80211pureg),
1932	DEF_CMD("-pureg",	0,	set80211pureg),
1933	DEF_CMD_ARG("mcastrate",	set80211mcastrate),
1934	DEF_CMD_ARG("fragthreshold",	set80211fragthreshold),
1935};
1936static struct afswtch af_ieee80211 = {
1937	.af_name	= "af_ieee80211",
1938	.af_af		= AF_UNSPEC,
1939	.af_other_status = ieee80211_status,
1940};
1941
1942static __constructor void
1943ieee80211_ctor(void)
1944{
1945#define	N(a)	(sizeof(a) / sizeof(a[0]))
1946	int i;
1947
1948	for (i = 0; i < N(ieee80211_cmds);  i++)
1949		cmd_register(&ieee80211_cmds[i]);
1950	af_register(&af_ieee80211);
1951#undef N
1952}
1953