2 * Copyright 2001 The Aerospace Corporation. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
13 * THIS SOFTWARE IS PROVIDED BY THE AEROSPACE CORPORATION ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AEROSPACE CORPORATION BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * $FreeBSD: src/sbin/ifconfig/ifieee80211.c,v 1.1.2.3 2002/02/07 15:12:37 ambrisko Exp $
26 * $DragonFly: src/sbin/ifconfig/ifieee80211.c,v 1.7 2005/03/04 00:11:11 cpressey Exp $
30 * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
31 * All rights reserved.
33 * This code is derived from software contributed to The NetBSD Foundation
34 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
35 * NASA Ames Research Center.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the NetBSD
48 * Foundation, Inc. and its contributors.
49 * 4. Neither the name of The NetBSD Foundation nor the names of its
50 * contributors may be used to endorse or promote products derived
51 * from this software without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
54 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
57 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
59 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
61 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63 * POSSIBILITY OF SUCH DAMAGE.
66 #include <sys/param.h>
67 #include <sys/ioctl.h>
68 #include <sys/socket.h>
69 #include <sys/sysctl.h>
72 #include <net/ethernet.h>
74 #include <net/if_dl.h>
75 #include <net/if_types.h>
76 #include <net/route.h>
77 #include <netproto/802_11/ieee80211.h>
78 #include <netproto/802_11/ieee80211_ioctl.h>
91 static void set80211(int s, int type, int val, int len, u_int8_t *data);
92 static const char *get_string(const char *val, const char *sep,
93 u_int8_t *buf, int *lenp);
94 static void print_string(const u_int8_t *buf, int len);
97 set80211ssid(const char *val, int d __unused, int s,
98 const struct afswtch *rafp __unused)
106 if (len > 2 && isdigit(val[0]) && val[1] == ':') {
111 bzero(data, sizeof(data));
113 get_string(val, NULL, data, &len);
115 set80211(s, IEEE80211_IOC_SSID, ssid, len, data);
119 set80211stationname(const char *val, int d __unused, int s,
120 const struct afswtch *rafp __unused)
125 bzero(data, sizeof(data));
127 get_string(val, NULL, data, &len);
129 set80211(s, IEEE80211_IOC_STATIONNAME, 0, len, data);
133 set80211channel(const char *val, int d __unused, int s,
134 const struct afswtch *rafp __unused)
136 set80211(s, IEEE80211_IOC_CHANNEL, atoi(val), 0, NULL);
140 set80211authmode(const char *val, int d __unused, int s,
141 const struct afswtch *rafp __unused)
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;
152 err(1, "unknown authmode");
155 set80211(s, IEEE80211_IOC_AUTHMODE, mode, 0, NULL);
159 set80211powersavemode(const char *val, int d __unused, int s,
160 const struct afswtch *rafp __unused)
164 if(strcasecmp(val, "off") == 0) {
165 mode = IEEE80211_POWERSAVE_OFF;
166 } else if(strcasecmp(val, "on") == 0) {
167 mode = IEEE80211_POWERSAVE_ON;
168 } else if(strcasecmp(val, "cam") == 0) {
169 mode = IEEE80211_POWERSAVE_CAM;
170 } else if(strcasecmp(val, "psp") == 0) {
171 mode = IEEE80211_POWERSAVE_PSP;
172 } else if(strcasecmp(val, "psp-cam") == 0) {
173 mode = IEEE80211_POWERSAVE_PSP_CAM;
175 err(1, "unknown powersavemode");
178 set80211(s, IEEE80211_IOC_POWERSAVE, mode, 0, NULL);
182 set80211powersave(const char *val __unused, int d, int s,
183 const struct afswtch *rafp __unused)
186 set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_OFF,
189 set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_ON,
194 set80211powersavesleep(const char *val, int d __unused, int s,
195 const struct afswtch *rafp __unused)
197 set80211(s, IEEE80211_IOC_POWERSAVESLEEP, atoi(val), 0, NULL);
201 set80211wepmode(const char *val, int d __unused, int s,
202 const struct afswtch *rafp __unused)
206 if(strcasecmp(val, "off") == 0) {
207 mode = IEEE80211_WEP_OFF;
208 } else if(strcasecmp(val, "on") == 0) {
209 mode = IEEE80211_WEP_ON;
210 } else if(strcasecmp(val, "mixed") == 0) {
211 mode = IEEE80211_WEP_MIXED;
213 err(1, "unknown wep mode");
216 set80211(s, IEEE80211_IOC_WEP, mode, 0, NULL);
220 set80211wep(const char *val __unused, int d, int s,
221 const struct afswtch *rafp __unused)
223 set80211(s, IEEE80211_IOC_WEP, d, 0, NULL);
227 set80211weptxkey(const char *val, int d __unused, int s,
228 const struct afswtch *rafp __unused)
230 set80211(s, IEEE80211_IOC_WEPTXKEY, atoi(val)-1, 0, NULL);
234 set80211wepkey(const char *val, int d __unused, int s,
235 const struct afswtch *rafp __unused)
241 if(isdigit(val[0]) && val[1] == ':') {
246 bzero(data, sizeof(data));
248 get_string(val, NULL, data, &len);
250 set80211(s, IEEE80211_IOC_WEPKEY, key, len, data);
254 * This function is purly a NetBSD compatibility interface. The NetBSD
255 * iterface is too inflexable, but it's there so we'll support it since
256 * it's not all that hard.
259 set80211nwkey(const char *val, int d __unused, int s,
260 const struct afswtch *rafp __unused)
266 set80211(s, IEEE80211_IOC_WEP, IEEE80211_WEP_ON, 0, NULL);
268 if(isdigit(val[0]) && val[1] == ':') {
269 txkey = val[0]-'0'-1;
272 for(i = 0; i < 4; i++) {
273 bzero(data, sizeof(data));
275 val = get_string(val, ",", data, &len);
277 set80211(s, IEEE80211_IOC_WEPKEY, i, len, data);
280 bzero(data, sizeof(data));
282 get_string(val, NULL, data, &len);
285 set80211(s, IEEE80211_IOC_WEPKEY, 0, len, data);
287 bzero(data, sizeof(data));
288 for(i = 1; i < 4; i++)
289 set80211(s, IEEE80211_IOC_WEPKEY, i, 0, data);
292 set80211(s, IEEE80211_IOC_WEPTXKEY, txkey, 0, NULL);
296 ieee80211_status (int s, struct rt_addrinfo *info __unused)
300 struct ieee80211req ireq;
304 memset(&ireq, 0, sizeof(ireq));
305 strncpy(ireq.i_name, name, sizeof(ireq.i_name));
308 ireq.i_type = IEEE80211_IOC_SSID;
310 if (ioctl(s, SIOCG80211, &ireq) < 0) {
311 /* If we can't get the SSID, the this isn't an 802.11 device. */
315 print_string(data, ireq.i_len);
317 ireq.i_type = IEEE80211_IOC_NUMSSIDS;
318 if (ioctl(s, SIOCG80211, &ireq) >= 0) {
321 ireq.i_type = IEEE80211_IOC_SSID;
322 for (ireq.i_val = 0; ireq.i_val < num; ireq.i_val++) {
323 if (ioctl(s, SIOCG80211, &ireq) >= 0 && ireq.i_len > 0) {
324 printf(" %d:", ireq.i_val + 1);
325 print_string(data, ireq.i_len);
330 ireq.i_type = IEEE80211_IOC_STATIONNAME;
331 if (ioctl(s, SIOCG80211, &ireq) != -1) {
332 printf("\tstationname ");
333 print_string(data, ireq.i_len);
337 ireq.i_type = IEEE80211_IOC_CHANNEL;
338 if (ioctl(s, SIOCG80211, &ireq) < 0) {
341 printf("\tchannel %d", ireq.i_val);
343 ireq.i_type = IEEE80211_IOC_AUTHMODE;
344 if (ioctl(s, SIOCG80211, &ireq) != -1) {
346 switch (ireq.i_val) {
347 case IEEE80211_AUTH_NONE:
350 case IEEE80211_AUTH_OPEN:
353 case IEEE80211_AUTH_SHARED:
362 ireq.i_type = IEEE80211_IOC_POWERSAVE;
363 if (ioctl(s, SIOCG80211, &ireq) != -1 &&
364 ireq.i_val != IEEE80211_POWERSAVE_NOSUP ) {
365 printf(" powersavemode");
366 switch (ireq.i_val) {
367 case IEEE80211_POWERSAVE_OFF:
370 case IEEE80211_POWERSAVE_CAM:
373 case IEEE80211_POWERSAVE_PSP:
376 case IEEE80211_POWERSAVE_PSP_CAM:
381 ireq.i_type = IEEE80211_IOC_POWERSAVESLEEP;
382 if (ioctl(s, SIOCG80211, &ireq) != -1) {
384 printf(" powersavesleep %d", ireq.i_val);
390 ireq.i_type = IEEE80211_IOC_WEP;
391 if (ioctl(s, SIOCG80211, &ireq) != -1 &&
392 ireq.i_val != IEEE80211_WEP_NOSUP) {
394 switch (ireq.i_val) {
395 case IEEE80211_WEP_OFF:
398 case IEEE80211_WEP_ON:
401 case IEEE80211_WEP_MIXED:
410 * If we get here then we've got WEP support so we need
411 * to print WEP status.
414 ireq.i_type = IEEE80211_IOC_WEPTXKEY;
415 if (ioctl(s, SIOCG80211, &ireq) < 0) {
416 warn("WEP support, but no tx key!");
419 printf(" weptxkey %d", ireq.i_val+1);
421 ireq.i_type = IEEE80211_IOC_NUMWEPKEYS;
422 if (ioctl(s, SIOCG80211, &ireq) < 0) {
423 warn("WEP support, but no NUMWEPKEYS support!");
430 ireq.i_type = IEEE80211_IOC_WEPKEY;
432 for(i = 0; i < num; i++) {
434 if (ioctl(s, SIOCG80211, &ireq) < 0) {
435 warn("WEP support, but can get keys!");
438 if(ireq.i_len == 0 || ireq.i_len > 13)
440 printf("%cwepkey %d:%s", spacer, i+1,
441 ireq.i_len <= 5 ? "64-bit" : "128-bit");
454 set80211(int s, int type, int val, int len, u_int8_t *data)
456 struct ieee80211req ireq;
458 memset(&ireq, 0, sizeof(ireq));
459 strncpy(ireq.i_name, name, sizeof(ireq.i_name));
464 if(ioctl(s, SIOCS80211, &ireq) < 0)
465 err(1, "SIOCS80211");
469 get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
477 hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
483 if (sep != NULL && strchr(sep, *val) != NULL) {
488 if (!isxdigit((u_char)val[0]) ||
489 !isxdigit((u_char)val[1])) {
490 warnx("bad hexadecimal digits");
496 warnx("hexadecimal digits too long");
498 warnx("strings too long");
502 #define tohex(x) (isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
503 *p++ = (tohex((u_char)val[0]) << 4) |
504 tohex((u_char)val[1]);
511 /* The string "-" is treated as the empty string. */
512 if (!hexstr && len == 1 && buf[0] == '-')
515 memset(p, 0, *lenp - len);
521 print_string(const u_int8_t *buf, int len)
528 for(; i < len; i++) {
529 if (!isprint(buf[i]) && buf[i] != '\0')
535 if (hasspc || len == 0 || buf[0] == '\0')
536 printf("\"%.*s\"", len, buf);
538 printf("%.*s", len, buf);
541 for (i = 0; i < len; i++)
542 printf("%02x", buf[i]);