ifieee80211.c revision 116957
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 116957 2003-06-28 06:23:40Z 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/route.h>
78#include <net80211/ieee80211.h>
79#include <net80211/ieee80211_ioctl.h>
80
81#include <ctype.h>
82#include <err.h>
83#include <errno.h>
84#include <fcntl.h>
85#include <stdio.h>
86#include <stdlib.h>
87#include <string.h>
88#include <unistd.h>
89
90#include "ifconfig.h"
91
92static void set80211(int s, int type, int val, int len, u_int8_t *data);
93static const char *get_string(const char *val, const char *sep,
94    u_int8_t *buf, int *lenp);
95static void print_string(const u_int8_t *buf, int len);
96
97void
98set80211ssid(const char *val, int d, int s, const struct afswtch *rafp)
99{
100	int		ssid;
101	int		len;
102	u_int8_t	data[33];
103
104	ssid = 0;
105	len = sizeof(val);
106	if (len > 2 && isdigit(val[0]) && val[1] == ':') {
107		ssid = atoi(val)-1;
108		val += 2;
109	}
110
111	bzero(data, sizeof(data));
112	len = sizeof(data);
113	get_string(val, NULL, data, &len);
114
115	set80211(s, IEEE80211_IOC_SSID, ssid, len, data);
116}
117
118void
119set80211stationname(const char *val, int d, int s, const struct afswtch *rafp)
120{
121	int			len;
122	u_int8_t		data[33];
123
124	bzero(data, sizeof(data));
125	len = sizeof(data);
126	get_string(val, NULL, data, &len);
127
128	set80211(s, IEEE80211_IOC_STATIONNAME, 0, len, data);
129}
130
131void
132set80211channel(const char *val, int d, int s, const struct afswtch *rafp)
133{
134	if (strcmp(val, "-") == 0)
135		set80211(s, IEEE80211_IOC_CHANNEL, IEEE80211_CHAN_ANY, 0, NULL);
136	else
137		set80211(s, IEEE80211_IOC_CHANNEL, atoi(val), 0, NULL);
138}
139
140void
141set80211authmode(const char *val, int d, int s, const struct afswtch *rafp)
142{
143	int	mode;
144
145	if (strcasecmp(val, "none") == 0) {
146		mode = IEEE80211_AUTH_NONE;
147	} else if (strcasecmp(val, "open") == 0) {
148		mode = IEEE80211_AUTH_OPEN;
149	} else if (strcasecmp(val, "shared") == 0) {
150		mode = IEEE80211_AUTH_SHARED;
151	} else {
152		err(1, "unknown authmode");
153	}
154
155	set80211(s, IEEE80211_IOC_AUTHMODE, mode, 0, NULL);
156}
157
158void
159set80211powersavemode(const char *val, int d, int s, const struct afswtch *rafp)
160{
161	int	mode;
162
163	if (strcasecmp(val, "off") == 0) {
164		mode = IEEE80211_POWERSAVE_OFF;
165	} else if (strcasecmp(val, "on") == 0) {
166		mode = IEEE80211_POWERSAVE_ON;
167	} else if (strcasecmp(val, "cam") == 0) {
168		mode = IEEE80211_POWERSAVE_CAM;
169	} else if (strcasecmp(val, "psp") == 0) {
170		mode = IEEE80211_POWERSAVE_PSP;
171	} else if (strcasecmp(val, "psp-cam") == 0) {
172		mode = IEEE80211_POWERSAVE_PSP_CAM;
173	} else {
174		err(1, "unknown powersavemode");
175	}
176
177	set80211(s, IEEE80211_IOC_POWERSAVE, mode, 0, NULL);
178}
179
180void
181set80211powersave(const char *val, int d, int s, const struct afswtch *rafp)
182{
183	if (d == 0)
184		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_OFF,
185		    0, NULL);
186	else
187		set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_ON,
188		    0, NULL);
189}
190
191void
192set80211powersavesleep(const char *val, int d, int s, const struct afswtch *rafp)
193{
194	set80211(s, IEEE80211_IOC_POWERSAVESLEEP, atoi(val), 0, NULL);
195}
196
197void
198set80211wepmode(const char *val, int d, int s, const struct afswtch *rafp)
199{
200	int	mode;
201
202	if (strcasecmp(val, "off") == 0) {
203		mode = IEEE80211_WEP_OFF;
204	} else if (strcasecmp(val, "on") == 0) {
205		mode = IEEE80211_WEP_ON;
206	} else if (strcasecmp(val, "mixed") == 0) {
207		mode = IEEE80211_WEP_MIXED;
208	} else {
209		err(1, "unknown wep mode");
210	}
211
212	set80211(s, IEEE80211_IOC_WEP, mode, 0, NULL);
213}
214
215void
216set80211wep(const char *val, int d, int s, const struct afswtch *rafp)
217{
218	set80211(s, IEEE80211_IOC_WEP, d, 0, NULL);
219}
220
221void
222set80211weptxkey(const char *val, int d, int s, const struct afswtch *rafp)
223{
224	set80211(s, IEEE80211_IOC_WEPTXKEY, atoi(val)-1, 0, NULL);
225}
226
227void
228set80211wepkey(const char *val, int d, int s, const struct afswtch *rafp)
229{
230	int		key = 0;
231	int		len;
232	u_int8_t	data[14];
233
234	if (isdigit(val[0]) && val[1] == ':') {
235		key = atoi(val)-1;
236		val += 2;
237	}
238
239	bzero(data, sizeof(data));
240	len = sizeof(data);
241	get_string(val, NULL, data, &len);
242
243	set80211(s, IEEE80211_IOC_WEPKEY, key, len, data);
244}
245
246/*
247 * This function is purly a NetBSD compatability interface.  The NetBSD
248 * iterface is too inflexable, but it's there so we'll support it since
249 * it's not all that hard.
250 */
251void
252set80211nwkey(const char *val, int d, int s, const struct afswtch *rafp)
253{
254	int		txkey;
255	int		i, len;
256	u_int8_t	data[14];
257
258	set80211(s, IEEE80211_IOC_WEP, IEEE80211_WEP_ON, 0, NULL);
259
260	if (isdigit(val[0]) && val[1] == ':') {
261		txkey = val[0]-'0'-1;
262		val += 2;
263
264		for (i = 0; i < 4; i++) {
265			bzero(data, sizeof(data));
266			len = sizeof(data);
267			val = get_string(val, ",", data, &len);
268
269			set80211(s, IEEE80211_IOC_WEPKEY, i, len, data);
270		}
271	} else {
272		bzero(data, sizeof(data));
273		len = sizeof(data);
274		get_string(val, NULL, data, &len);
275		txkey = 0;
276
277		set80211(s, IEEE80211_IOC_WEPKEY, 0, len, data);
278
279		bzero(data, sizeof(data));
280		for (i = 1; i < 4; i++)
281			set80211(s, IEEE80211_IOC_WEPKEY, i, 0, data);
282	}
283
284	set80211(s, IEEE80211_IOC_WEPTXKEY, txkey, 0, NULL);
285}
286
287void
288ieee80211_status (int s, struct rt_addrinfo *info __unused)
289{
290	int			i;
291	int			num;
292	struct ieee80211req	ireq;
293	u_int8_t		data[32];
294	char			spacer;
295
296	(void) memset(&ireq, 0, sizeof(ireq));
297	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
298	ireq.i_data = &data;
299
300	ireq.i_type = IEEE80211_IOC_SSID;
301	ireq.i_val = -1;
302	if (ioctl(s, SIOCG80211, &ireq) < 0) {
303		/* If we can't get the SSID, the this isn't an 802.11 device. */
304		return;
305	}
306	printf("\tssid ");
307	print_string(data, ireq.i_len);
308	num = 0;
309	ireq.i_type = IEEE80211_IOC_NUMSSIDS;
310	if (ioctl(s, SIOCG80211, &ireq) >= 0) {
311		num = ireq.i_val;
312	}
313	ireq.i_type = IEEE80211_IOC_SSID;
314	for (ireq.i_val = 0; ireq.i_val < num; ireq.i_val++) {
315		if (ioctl(s, SIOCG80211, &ireq) >= 0 && ireq.i_len > 0) {
316			printf(" %d:", ireq.i_val + 1);
317			print_string(data, ireq.i_len);
318		}
319	}
320	printf("\n");
321
322	ireq.i_type = IEEE80211_IOC_STATIONNAME;
323	if (ioctl(s, SIOCG80211, &ireq) != -1) {
324		printf("\tstationname ");
325		print_string(data, ireq.i_len);
326		printf("\n");
327	}
328
329	ireq.i_type = IEEE80211_IOC_CHANNEL;
330	if (ioctl(s, SIOCG80211, &ireq) < 0) {
331		goto end;
332	}
333	printf("\tchannel %d", ireq.i_val);
334
335	ireq.i_type = IEEE80211_IOC_AUTHMODE;
336	if (ioctl(s, SIOCG80211, &ireq) != -1) {
337		printf(" authmode");
338		switch (ireq.i_val) {
339			case IEEE80211_AUTH_NONE:
340				printf(" NONE");
341				break;
342			case IEEE80211_AUTH_OPEN:
343				printf(" OPEN");
344				break;
345			case IEEE80211_AUTH_SHARED:
346				printf(" SHARED");
347				break;
348			default:
349				printf(" UNKNOWN");
350				break;
351		}
352	}
353
354	ireq.i_type = IEEE80211_IOC_POWERSAVE;
355	if (ioctl(s, SIOCG80211, &ireq) != -1 &&
356	    ireq.i_val != IEEE80211_POWERSAVE_NOSUP ) {
357		printf(" powersavemode");
358		switch (ireq.i_val) {
359			case IEEE80211_POWERSAVE_OFF:
360				printf(" OFF");
361				break;
362			case IEEE80211_POWERSAVE_CAM:
363				printf(" CAM");
364				break;
365			case IEEE80211_POWERSAVE_PSP:
366				printf(" PSP");
367				break;
368			case IEEE80211_POWERSAVE_PSP_CAM:
369				printf(" PSP-CAM");
370				break;
371		}
372
373		ireq.i_type = IEEE80211_IOC_POWERSAVESLEEP;
374		if (ioctl(s, SIOCG80211, &ireq) != -1) {
375			if (ireq.i_val)
376				printf(" powersavesleep %d", ireq.i_val);
377		}
378	}
379
380	printf("\n");
381
382	ireq.i_type = IEEE80211_IOC_WEP;
383	if (ioctl(s, SIOCG80211, &ireq) != -1 &&
384	    ireq.i_val != IEEE80211_WEP_NOSUP) {
385		printf("\twepmode");
386		switch (ireq.i_val) {
387			case IEEE80211_WEP_OFF:
388				printf(" OFF");
389				break;
390			case IEEE80211_WEP_ON:
391				printf(" ON");
392				break;
393			case IEEE80211_WEP_MIXED:
394				printf(" MIXED");
395				break;
396			default:
397				printf(" UNKNOWN");
398				break;
399		}
400
401		/*
402		 * If we get here then we've got WEP support so we need
403		 * to print WEP status.
404		 */
405
406		ireq.i_type = IEEE80211_IOC_WEPTXKEY;
407		if (ioctl(s, SIOCG80211, &ireq) < 0) {
408			warn("WEP support, but no tx key!");
409			goto end;
410		}
411		printf(" weptxkey %d", ireq.i_val+1);
412
413		ireq.i_type = IEEE80211_IOC_NUMWEPKEYS;
414		if (ioctl(s, SIOCG80211, &ireq) < 0) {
415			warn("WEP support, but no NUMWEPKEYS support!");
416			goto end;
417		}
418		num = ireq.i_val;
419
420		printf("\n");
421
422		ireq.i_type = IEEE80211_IOC_WEPKEY;
423		spacer = '\t';
424		for (i = 0; i < num; i++) {
425			ireq.i_val = i;
426			if (ioctl(s, SIOCG80211, &ireq) < 0) {
427				warn("WEP support, but can get keys!");
428				goto end;
429			}
430			if (ireq.i_len == 0 || ireq.i_len > 13)
431				continue;
432			printf("%cwepkey %d:%s", spacer, i+1,
433			    ireq.i_len <= 5 ? "64-bit" : "128-bit");
434			if (spacer == '\t')
435				spacer = ' ';
436		}
437		if (spacer == ' ')
438			printf("\n");
439	}
440
441end:
442	return;
443}
444
445static void
446set80211(int s, int type, int val, int len, u_int8_t *data)
447{
448	struct ieee80211req	ireq;
449
450	(void) memset(&ireq, 0, sizeof(ireq));
451	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
452	ireq.i_type = type;
453	ireq.i_val = val;
454	ireq.i_len = len;
455	ireq.i_data = data;
456	if (ioctl(s, SIOCS80211, &ireq) < 0)
457		err(1, "SIOCS80211");
458}
459
460static const char *
461get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
462{
463	int len;
464	int hexstr;
465	u_int8_t *p;
466
467	len = *lenp;
468	p = buf;
469	hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
470	if (hexstr)
471		val += 2;
472	for (;;) {
473		if (*val == '\0')
474			break;
475		if (sep != NULL && strchr(sep, *val) != NULL) {
476			val++;
477			break;
478		}
479		if (hexstr) {
480			if (!isxdigit((u_char)val[0]) ||
481			    !isxdigit((u_char)val[1])) {
482				warnx("bad hexadecimal digits");
483				return NULL;
484			}
485		}
486		if (p > buf + len) {
487			if (hexstr)
488				warnx("hexadecimal digits too long");
489			else
490				warnx("strings too long");
491			return NULL;
492		}
493		if (hexstr) {
494#define	tohex(x)	(isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
495			*p++ = (tohex((u_char)val[0]) << 4) |
496			    tohex((u_char)val[1]);
497#undef tohex
498			val += 2;
499		} else
500			*p++ = *val++;
501	}
502	len = p - buf;
503	/* The string "-" is treated as the empty string. */
504	if (!hexstr && len == 1 && buf[0] == '-')
505		len = 0;
506	if (len < *lenp)
507		memset(p, 0, *lenp - len);
508	*lenp = len;
509	return val;
510}
511
512static void
513print_string(const u_int8_t *buf, int len)
514{
515	int i;
516	int hasspc;
517
518	i = 0;
519	hasspc = 0;
520	for (; i < len; i++) {
521		if (!isprint(buf[i]) && buf[i] != '\0')
522			break;
523		if (isspace(buf[i]))
524			hasspc++;
525	}
526	if (i == len) {
527		if (hasspc || len == 0 || buf[0] == '\0')
528			printf("\"%.*s\"", len, buf);
529		else
530			printf("%.*s", len, buf);
531	} else {
532		printf("0x");
533		for (i = 0; i < len; i++)
534			printf("%02x", buf[i]);
535	}
536}
537
538