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 $
29 * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
30 * All rights reserved.
32 * This code is derived from software contributed to The NetBSD Foundation
33 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
34 * NASA Ames Research Center.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the NetBSD
47 * Foundation, Inc. and its contributors.
48 * 4. Neither the name of The NetBSD Foundation nor the names of its
49 * contributors may be used to endorse or promote products derived
50 * from this software without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
53 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
54 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
55 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
56 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
57 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
58 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
59 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
60 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
61 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
62 * POSSIBILITY OF SUCH DAMAGE.
65 #include <sys/param.h>
66 #include <sys/ioctl.h>
67 #include <sys/socket.h>
68 #include <sys/sysctl.h>
71 #include <net/ethernet.h>
73 #include <net/if_dl.h>
74 #include <net/if_types.h>
75 #include <net/route.h>
76 #include <net/if_ieee80211.h>
89 static void set80211(int s, int type, int val, int len, u_int8_t *data);
90 static const char *get_string(const char *val, const char *sep,
91 u_int8_t *buf, int *lenp);
92 static void print_string(const u_int8_t *buf, int len);
95 set80211ssid(const char *val, int d, int s, const struct afswtch *rafp)
103 if (len > 2 && isdigit(val[0]) && val[1] == ':') {
108 bzero(data, sizeof(data));
110 get_string(val, NULL, data, &len);
112 set80211(s, IEEE80211_IOC_SSID, ssid, len, data);
116 set80211stationname(const char *val, int d, int s, const struct afswtch *rafp)
121 bzero(data, sizeof(data));
123 get_string(val, NULL, data, &len);
125 set80211(s, IEEE80211_IOC_STATIONNAME, 0, len, data);
129 set80211channel(const char *val, int d, int s, const struct afswtch *rafp)
131 set80211(s, IEEE80211_IOC_CHANNEL, atoi(val), 0, NULL);
135 set80211authmode(const char *val, int d, int s, const struct afswtch *rafp)
139 if(strcasecmp(val, "none") == 0) {
140 mode = IEEE80211_AUTH_NONE;
141 } else if(strcasecmp(val, "open") == 0) {
142 mode = IEEE80211_AUTH_OPEN;
143 } else if(strcasecmp(val, "shared") == 0) {
144 mode = IEEE80211_AUTH_SHARED;
146 err(1, "unknown authmode");
149 set80211(s, IEEE80211_IOC_AUTHMODE, mode, 0, NULL);
153 set80211powersavemode(const char *val, int d, int s, const struct afswtch *rafp)
157 if(strcasecmp(val, "off") == 0) {
158 mode = IEEE80211_POWERSAVE_OFF;
159 } else if(strcasecmp(val, "on") == 0) {
160 mode = IEEE80211_POWERSAVE_ON;
161 } else if(strcasecmp(val, "cam") == 0) {
162 mode = IEEE80211_POWERSAVE_CAM;
163 } else if(strcasecmp(val, "psp") == 0) {
164 mode = IEEE80211_POWERSAVE_PSP;
165 } else if(strcasecmp(val, "psp-cam") == 0) {
166 mode = IEEE80211_POWERSAVE_PSP_CAM;
168 err(1, "unknown powersavemode");
171 set80211(s, IEEE80211_IOC_POWERSAVE, mode, 0, NULL);
175 set80211powersave(const char *val, int d, int s, const struct afswtch *rafp)
178 set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_OFF,
181 set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_ON,
186 set80211powersavesleep(const char *val, int d, int s, const struct afswtch *rafp)
188 set80211(s, IEEE80211_IOC_POWERSAVESLEEP, atoi(val), 0, NULL);
192 set80211wepmode(const char *val, int d, int s, const struct afswtch *rafp)
196 if(strcasecmp(val, "off") == 0) {
197 mode = IEEE80211_WEP_OFF;
198 } else if(strcasecmp(val, "on") == 0) {
199 mode = IEEE80211_WEP_ON;
200 } else if(strcasecmp(val, "mixed") == 0) {
201 mode = IEEE80211_WEP_MIXED;
203 err(1, "unknown wep mode");
206 set80211(s, IEEE80211_IOC_WEP, mode, 0, NULL);
210 set80211wep(const char *val, int d, int s, const struct afswtch *rafp)
212 set80211(s, IEEE80211_IOC_WEP, d, 0, NULL);
216 set80211weptxkey(const char *val, int d, int s, const struct afswtch *rafp)
218 set80211(s, IEEE80211_IOC_WEPTXKEY, atoi(val)-1, 0, NULL);
222 set80211wepkey(const char *val, int d, int s, const struct afswtch *rafp)
228 if(isdigit(val[0]) && val[1] == ':') {
233 bzero(data, sizeof(data));
235 get_string(val, NULL, data, &len);
237 set80211(s, IEEE80211_IOC_WEPKEY, key, len, data);
241 * This function is purly a NetBSD compatability interface. The NetBSD
242 * iterface is too inflexable, but it's there so we'll support it since
243 * it's not all that hard.
246 set80211nwkey(const char *val, int d, int s, const struct afswtch *rafp)
252 set80211(s, IEEE80211_IOC_WEP, IEEE80211_WEP_ON, 0, NULL);
254 if(isdigit(val[0]) && val[1] == ':') {
255 txkey = val[0]-'0'-1;
258 for(i = 0; i < 4; i++) {
259 bzero(data, sizeof(data));
261 val = get_string(val, ",", data, &len);
263 set80211(s, IEEE80211_IOC_WEPKEY, i, len, data);
266 bzero(data, sizeof(data));
268 get_string(val, NULL, data, &len);
271 set80211(s, IEEE80211_IOC_WEPKEY, 0, len, data);
273 bzero(data, sizeof(data));
274 for(i = 1; i < 4; i++)
275 set80211(s, IEEE80211_IOC_WEPKEY, i, 0, data);
278 set80211(s, IEEE80211_IOC_WEPTXKEY, txkey, 0, NULL);
282 ieee80211_status (s, info)
284 struct rt_addrinfo *info __unused;
288 struct ieee80211req ireq;
292 (void) memset(&ireq, 0, sizeof(ireq));
293 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
296 ireq.i_type = IEEE80211_IOC_SSID;
298 if (ioctl(s, SIOCG80211, &ireq) < 0) {
299 /* If we can't get the SSID, the this isn't an 802.11 device. */
303 print_string(data, ireq.i_len);
305 ireq.i_type = IEEE80211_IOC_NUMSSIDS;
306 if (ioctl(s, SIOCG80211, &ireq) >= 0) {
309 ireq.i_type = IEEE80211_IOC_SSID;
310 for (ireq.i_val = 0; ireq.i_val < num; ireq.i_val++) {
311 if (ioctl(s, SIOCG80211, &ireq) >= 0 && ireq.i_len > 0) {
312 printf(" %d:", ireq.i_val + 1);
313 print_string(data, ireq.i_len);
318 ireq.i_type = IEEE80211_IOC_STATIONNAME;
319 if (ioctl(s, SIOCG80211, &ireq) != -1) {
320 printf("\tstationname ");
321 print_string(data, ireq.i_len);
325 ireq.i_type = IEEE80211_IOC_CHANNEL;
326 if (ioctl(s, SIOCG80211, &ireq) < 0) {
329 printf("\tchannel %d", ireq.i_val);
331 ireq.i_type = IEEE80211_IOC_AUTHMODE;
332 if (ioctl(s, SIOCG80211, &ireq) != -1) {
334 switch (ireq.i_val) {
335 case IEEE80211_AUTH_NONE:
338 case IEEE80211_AUTH_OPEN:
341 case IEEE80211_AUTH_SHARED:
350 ireq.i_type = IEEE80211_IOC_POWERSAVE;
351 if (ioctl(s, SIOCG80211, &ireq) != -1 &&
352 ireq.i_val != IEEE80211_POWERSAVE_NOSUP ) {
353 printf(" powersavemode");
354 switch (ireq.i_val) {
355 case IEEE80211_POWERSAVE_OFF:
358 case IEEE80211_POWERSAVE_CAM:
361 case IEEE80211_POWERSAVE_PSP:
364 case IEEE80211_POWERSAVE_PSP_CAM:
369 ireq.i_type = IEEE80211_IOC_POWERSAVESLEEP;
370 if (ioctl(s, SIOCG80211, &ireq) != -1) {
372 printf(" powersavesleep %d", ireq.i_val);
378 ireq.i_type = IEEE80211_IOC_WEP;
379 if (ioctl(s, SIOCG80211, &ireq) != -1 &&
380 ireq.i_val != IEEE80211_WEP_NOSUP) {
382 switch (ireq.i_val) {
383 case IEEE80211_WEP_OFF:
386 case IEEE80211_WEP_ON:
389 case IEEE80211_WEP_MIXED:
398 * If we get here then we've got WEP support so we need
399 * to print WEP status.
402 ireq.i_type = IEEE80211_IOC_WEPTXKEY;
403 if (ioctl(s, SIOCG80211, &ireq) < 0) {
404 warn("WEP support, but no tx key!");
407 printf(" weptxkey %d", ireq.i_val+1);
409 ireq.i_type = IEEE80211_IOC_NUMWEPKEYS;
410 if (ioctl(s, SIOCG80211, &ireq) < 0) {
411 warn("WEP support, but no NUMWEPKEYS support!");
418 ireq.i_type = IEEE80211_IOC_WEPKEY;
420 for(i = 0; i < num; i++) {
422 if (ioctl(s, SIOCG80211, &ireq) < 0) {
423 warn("WEP support, but can get keys!");
426 if(ireq.i_len == 0 || ireq.i_len > 13)
428 printf("%cwepkey %d:%s", spacer, i+1,
429 ireq.i_len <= 5 ? "64-bit" : "128-bit");
442 set80211(int s, int type, int val, int len, u_int8_t *data)
444 struct ieee80211req ireq;
446 (void) memset(&ireq, 0, sizeof(ireq));
447 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
452 if(ioctl(s, SIOCS80211, &ireq) < 0)
453 err(1, "SIOCS80211");
457 get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
465 hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
471 if (sep != NULL && strchr(sep, *val) != NULL) {
476 if (!isxdigit((u_char)val[0]) ||
477 !isxdigit((u_char)val[1])) {
478 warnx("bad hexadecimal digits");
484 warnx("hexadecimal digits too long");
486 warnx("strings too long");
490 #define tohex(x) (isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
491 *p++ = (tohex((u_char)val[0]) << 4) |
492 tohex((u_char)val[1]);
499 /* The string "-" is treated as the empty string. */
500 if (!hexstr && len == 1 && buf[0] == '-')
503 memset(p, 0, *lenp - len);
509 print_string(const u_int8_t *buf, int len)
516 for(; i < len; i++) {
517 if (!isprint(buf[i]) && buf[i] != '\0')
523 if (hasspc || len == 0 || buf[0] == '\0')
524 printf("\"%.*s\"", len, buf);
526 printf("%.*s", len, buf);
529 for (i = 0; i < len; i++)
530 printf("%02x", buf[i]);