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.
12 * 3. The name of The Aerospace Corporation may not be used to endorse or
13 * promote products derived from this software.
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
31 * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
32 * All rights reserved.
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.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
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.
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.
67 #include <sys/param.h>
68 #include <sys/ioctl.h>
69 #include <sys/socket.h>
70 #include <sys/sysctl.h>
73 #include <net/ethernet.h>
75 #include <net/if_dl.h>
76 #include <net/if_types.h>
77 #include <net/route.h>
78 #include <net/if_ieee80211.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, int s, const struct afswtch *rafp)
105 if (len > 2 && isdigit(val[0]) && val[1] == ':') {
110 bzero(data, sizeof(data));
112 get_string(val, NULL, data, &len);
114 set80211(s, IEEE80211_IOC_SSID, ssid, len, data);
118 set80211stationname(const char *val, int d, int s, const struct afswtch *rafp)
123 bzero(data, sizeof(data));
125 get_string(val, NULL, data, &len);
127 set80211(s, IEEE80211_IOC_STATIONNAME, 0, len, data);
131 set80211channel(const char *val, int d, int s, const struct afswtch *rafp)
133 set80211(s, IEEE80211_IOC_CHANNEL, atoi(val), 0, NULL);
137 set80211authmode(const char *val, int d, int s, const struct afswtch *rafp)
141 if (strcasecmp(val, "none") == 0) {
142 mode = IEEE80211_AUTH_NONE;
143 } else if (strcasecmp(val, "open") == 0) {
144 mode = IEEE80211_AUTH_OPEN;
145 } else if (strcasecmp(val, "shared") == 0) {
146 mode = IEEE80211_AUTH_SHARED;
148 err(1, "unknown authmode");
151 set80211(s, IEEE80211_IOC_AUTHMODE, mode, 0, NULL);
155 set80211powersavemode(const char *val, int d, int s, const struct afswtch *rafp)
159 if (strcasecmp(val, "off") == 0) {
160 mode = IEEE80211_POWERSAVE_OFF;
161 } else if (strcasecmp(val, "on") == 0) {
162 mode = IEEE80211_POWERSAVE_ON;
163 } else if (strcasecmp(val, "cam") == 0) {
164 mode = IEEE80211_POWERSAVE_CAM;
165 } else if (strcasecmp(val, "psp") == 0) {
166 mode = IEEE80211_POWERSAVE_PSP;
167 } else if (strcasecmp(val, "psp-cam") == 0) {
168 mode = IEEE80211_POWERSAVE_PSP_CAM;
170 err(1, "unknown powersavemode");
173 set80211(s, IEEE80211_IOC_POWERSAVE, mode, 0, NULL);
177 set80211powersave(const char *val, int d, int s, const struct afswtch *rafp)
180 set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_OFF,
183 set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_ON,
188 set80211powersavesleep(const char *val, int d, int s, const struct afswtch *rafp)
190 set80211(s, IEEE80211_IOC_POWERSAVESLEEP, atoi(val), 0, NULL);
194 set80211wepmode(const char *val, int d, int s, const struct afswtch *rafp)
198 if (strcasecmp(val, "off") == 0) {
199 mode = IEEE80211_WEP_OFF;
200 } else if (strcasecmp(val, "on") == 0) {
201 mode = IEEE80211_WEP_ON;
202 } else if (strcasecmp(val, "mixed") == 0) {
203 mode = IEEE80211_WEP_MIXED;
205 err(1, "unknown wep mode");
208 set80211(s, IEEE80211_IOC_WEP, mode, 0, NULL);
212 set80211wep(const char *val, int d, int s, const struct afswtch *rafp)
214 set80211(s, IEEE80211_IOC_WEP, d, 0, NULL);
218 set80211weptxkey(const char *val, int d, int s, const struct afswtch *rafp)
220 set80211(s, IEEE80211_IOC_WEPTXKEY, atoi(val)-1, 0, NULL);
224 set80211wepkey(const char *val, int d, int s, const struct afswtch *rafp)
230 if (isdigit(val[0]) && val[1] == ':') {
235 bzero(data, sizeof(data));
237 get_string(val, NULL, data, &len);
239 set80211(s, IEEE80211_IOC_WEPKEY, key, len, data);
243 * This function is purly a NetBSD compatability interface. The NetBSD
244 * iterface is too inflexable, but it's there so we'll support it since
245 * it's not all that hard.
248 set80211nwkey(const char *val, int d, int s, const struct afswtch *rafp)
254 set80211(s, IEEE80211_IOC_WEP, IEEE80211_WEP_ON, 0, NULL);
256 if (isdigit(val[0]) && val[1] == ':') {
257 txkey = val[0]-'0'-1;
260 for (i = 0; i < 4; i++) {
261 bzero(data, sizeof(data));
263 val = get_string(val, ",", data, &len);
265 set80211(s, IEEE80211_IOC_WEPKEY, i, len, data);
268 bzero(data, sizeof(data));
270 get_string(val, NULL, data, &len);
273 set80211(s, IEEE80211_IOC_WEPKEY, 0, len, data);
275 bzero(data, sizeof(data));
276 for (i = 1; i < 4; i++)
277 set80211(s, IEEE80211_IOC_WEPKEY, i, 0, data);
280 set80211(s, IEEE80211_IOC_WEPTXKEY, txkey, 0, NULL);
284 ieee80211_status (s, info)
286 struct rt_addrinfo *info __unused;
290 struct ieee80211req ireq;
294 (void) memset(&ireq, 0, sizeof(ireq));
295 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
298 ireq.i_type = IEEE80211_IOC_SSID;
300 if (ioctl(s, SIOCG80211, &ireq) < 0) {
301 /* If we can't get the SSID, the this isn't an 802.11 device. */
305 print_string(data, ireq.i_len);
307 ireq.i_type = IEEE80211_IOC_NUMSSIDS;
308 if (ioctl(s, SIOCG80211, &ireq) >= 0) {
311 ireq.i_type = IEEE80211_IOC_SSID;
312 for (ireq.i_val = 0; ireq.i_val < num; ireq.i_val++) {
313 if (ioctl(s, SIOCG80211, &ireq) >= 0 && ireq.i_len > 0) {
314 printf(" %d:", ireq.i_val + 1);
315 print_string(data, ireq.i_len);
320 ireq.i_type = IEEE80211_IOC_STATIONNAME;
321 if (ioctl(s, SIOCG80211, &ireq) != -1) {
322 printf("\tstationname ");
323 print_string(data, ireq.i_len);
327 ireq.i_type = IEEE80211_IOC_CHANNEL;
328 if (ioctl(s, SIOCG80211, &ireq) < 0) {
331 printf("\tchannel %d", ireq.i_val);
333 ireq.i_type = IEEE80211_IOC_AUTHMODE;
334 if (ioctl(s, SIOCG80211, &ireq) != -1) {
336 switch (ireq.i_val) {
337 case IEEE80211_AUTH_NONE:
340 case IEEE80211_AUTH_OPEN:
343 case IEEE80211_AUTH_SHARED:
352 ireq.i_type = IEEE80211_IOC_POWERSAVE;
353 if (ioctl(s, SIOCG80211, &ireq) != -1 &&
354 ireq.i_val != IEEE80211_POWERSAVE_NOSUP ) {
355 printf(" powersavemode");
356 switch (ireq.i_val) {
357 case IEEE80211_POWERSAVE_OFF:
360 case IEEE80211_POWERSAVE_CAM:
363 case IEEE80211_POWERSAVE_PSP:
366 case IEEE80211_POWERSAVE_PSP_CAM:
371 ireq.i_type = IEEE80211_IOC_POWERSAVESLEEP;
372 if (ioctl(s, SIOCG80211, &ireq) != -1) {
374 printf(" powersavesleep %d", ireq.i_val);
380 ireq.i_type = IEEE80211_IOC_WEP;
381 if (ioctl(s, SIOCG80211, &ireq) != -1 &&
382 ireq.i_val != IEEE80211_WEP_NOSUP) {
384 switch (ireq.i_val) {
385 case IEEE80211_WEP_OFF:
388 case IEEE80211_WEP_ON:
391 case IEEE80211_WEP_MIXED:
400 * If we get here then we've got WEP support so we need
401 * to print WEP status.
404 ireq.i_type = IEEE80211_IOC_WEPTXKEY;
405 if (ioctl(s, SIOCG80211, &ireq) < 0) {
406 warn("WEP support, but no tx key!");
409 printf(" weptxkey %d", ireq.i_val+1);
411 ireq.i_type = IEEE80211_IOC_NUMWEPKEYS;
412 if (ioctl(s, SIOCG80211, &ireq) < 0) {
413 warn("WEP support, but no NUMWEPKEYS support!");
420 ireq.i_type = IEEE80211_IOC_WEPKEY;
422 for (i = 0; i < num; i++) {
424 if (ioctl(s, SIOCG80211, &ireq) < 0) {
425 warn("WEP support, but can get keys!");
428 if (ireq.i_len == 0 || ireq.i_len > 13)
430 printf("%cwepkey %d:%s", spacer, i+1,
431 ireq.i_len <= 5 ? "64-bit" : "128-bit");
444 set80211(int s, int type, int val, int len, u_int8_t *data)
446 struct ieee80211req ireq;
448 (void) memset(&ireq, 0, sizeof(ireq));
449 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
454 if (ioctl(s, SIOCS80211, &ireq) < 0)
455 err(1, "SIOCS80211");
459 get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
467 hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
473 if (sep != NULL && strchr(sep, *val) != NULL) {
478 if (!isxdigit((u_char)val[0]) ||
479 !isxdigit((u_char)val[1])) {
480 warnx("bad hexadecimal digits");
486 warnx("hexadecimal digits too long");
488 warnx("strings too long");
492 #define tohex(x) (isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
493 *p++ = (tohex((u_char)val[0]) << 4) |
494 tohex((u_char)val[1]);
501 /* The string "-" is treated as the empty string. */
502 if (!hexstr && len == 1 && buf[0] == '-')
505 memset(p, 0, *lenp - len);
511 print_string(const u_int8_t *buf, int len)
518 for (; i < len; i++) {
519 if (!isprint(buf[i]) && buf[i] != '\0')
525 if (hasspc || len == 0 || buf[0] == '\0')
526 printf("\"%.*s\"", len, buf);
528 printf("%.*s", len, buf);
531 for (i = 0; i < len; i++)
532 printf("%02x", buf[i]);