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.2 2003/06/17 04:27:33 dillon 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 <net/if_ieee80211.h>
90 static void set80211(int s, int type, int val, int len, u_int8_t *data);
91 static const char *get_string(const char *val, const char *sep,
92 u_int8_t *buf, int *lenp);
93 static void print_string(const u_int8_t *buf, int len);
96 set80211ssid(const char *val, int d, int s, const struct afswtch *rafp)
104 if (len > 2 && isdigit(val[0]) && val[1] == ':') {
109 bzero(data, sizeof(data));
111 get_string(val, NULL, data, &len);
113 set80211(s, IEEE80211_IOC_SSID, ssid, len, data);
117 set80211stationname(const char *val, int d, int s, const struct afswtch *rafp)
122 bzero(data, sizeof(data));
124 get_string(val, NULL, data, &len);
126 set80211(s, IEEE80211_IOC_STATIONNAME, 0, len, data);
130 set80211channel(const char *val, int d, int s, const struct afswtch *rafp)
132 set80211(s, IEEE80211_IOC_CHANNEL, atoi(val), 0, NULL);
136 set80211authmode(const char *val, int d, int s, const struct afswtch *rafp)
140 if(strcasecmp(val, "none") == 0) {
141 mode = IEEE80211_AUTH_NONE;
142 } else if(strcasecmp(val, "open") == 0) {
143 mode = IEEE80211_AUTH_OPEN;
144 } else if(strcasecmp(val, "shared") == 0) {
145 mode = IEEE80211_AUTH_SHARED;
147 err(1, "unknown authmode");
150 set80211(s, IEEE80211_IOC_AUTHMODE, mode, 0, NULL);
154 set80211powersavemode(const char *val, int d, int s, const struct afswtch *rafp)
158 if(strcasecmp(val, "off") == 0) {
159 mode = IEEE80211_POWERSAVE_OFF;
160 } else if(strcasecmp(val, "on") == 0) {
161 mode = IEEE80211_POWERSAVE_ON;
162 } else if(strcasecmp(val, "cam") == 0) {
163 mode = IEEE80211_POWERSAVE_CAM;
164 } else if(strcasecmp(val, "psp") == 0) {
165 mode = IEEE80211_POWERSAVE_PSP;
166 } else if(strcasecmp(val, "psp-cam") == 0) {
167 mode = IEEE80211_POWERSAVE_PSP_CAM;
169 err(1, "unknown powersavemode");
172 set80211(s, IEEE80211_IOC_POWERSAVE, mode, 0, NULL);
176 set80211powersave(const char *val, int d, int s, const struct afswtch *rafp)
179 set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_OFF,
182 set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_ON,
187 set80211powersavesleep(const char *val, int d, int s, const struct afswtch *rafp)
189 set80211(s, IEEE80211_IOC_POWERSAVESLEEP, atoi(val), 0, NULL);
193 set80211wepmode(const char *val, int d, int s, const struct afswtch *rafp)
197 if(strcasecmp(val, "off") == 0) {
198 mode = IEEE80211_WEP_OFF;
199 } else if(strcasecmp(val, "on") == 0) {
200 mode = IEEE80211_WEP_ON;
201 } else if(strcasecmp(val, "mixed") == 0) {
202 mode = IEEE80211_WEP_MIXED;
204 err(1, "unknown wep mode");
207 set80211(s, IEEE80211_IOC_WEP, mode, 0, NULL);
211 set80211wep(const char *val, int d, int s, const struct afswtch *rafp)
213 set80211(s, IEEE80211_IOC_WEP, d, 0, NULL);
217 set80211weptxkey(const char *val, int d, int s, const struct afswtch *rafp)
219 set80211(s, IEEE80211_IOC_WEPTXKEY, atoi(val)-1, 0, NULL);
223 set80211wepkey(const char *val, int d, int s, const struct afswtch *rafp)
229 if(isdigit(val[0]) && val[1] == ':') {
234 bzero(data, sizeof(data));
236 get_string(val, NULL, data, &len);
238 set80211(s, IEEE80211_IOC_WEPKEY, key, len, data);
242 * This function is purly a NetBSD compatability interface. The NetBSD
243 * iterface is too inflexable, but it's there so we'll support it since
244 * it's not all that hard.
247 set80211nwkey(const char *val, int d, int s, const struct afswtch *rafp)
253 set80211(s, IEEE80211_IOC_WEP, IEEE80211_WEP_ON, 0, NULL);
255 if(isdigit(val[0]) && val[1] == ':') {
256 txkey = val[0]-'0'-1;
259 for(i = 0; i < 4; i++) {
260 bzero(data, sizeof(data));
262 val = get_string(val, ",", data, &len);
264 set80211(s, IEEE80211_IOC_WEPKEY, i, len, data);
267 bzero(data, sizeof(data));
269 get_string(val, NULL, data, &len);
272 set80211(s, IEEE80211_IOC_WEPKEY, 0, len, data);
274 bzero(data, sizeof(data));
275 for(i = 1; i < 4; i++)
276 set80211(s, IEEE80211_IOC_WEPKEY, i, 0, data);
279 set80211(s, IEEE80211_IOC_WEPTXKEY, txkey, 0, NULL);
283 ieee80211_status (s, info)
285 struct rt_addrinfo *info __unused;
289 struct ieee80211req ireq;
293 (void) memset(&ireq, 0, sizeof(ireq));
294 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
297 ireq.i_type = IEEE80211_IOC_SSID;
299 if (ioctl(s, SIOCG80211, &ireq) < 0) {
300 /* If we can't get the SSID, the this isn't an 802.11 device. */
304 print_string(data, ireq.i_len);
306 ireq.i_type = IEEE80211_IOC_NUMSSIDS;
307 if (ioctl(s, SIOCG80211, &ireq) >= 0) {
310 ireq.i_type = IEEE80211_IOC_SSID;
311 for (ireq.i_val = 0; ireq.i_val < num; ireq.i_val++) {
312 if (ioctl(s, SIOCG80211, &ireq) >= 0 && ireq.i_len > 0) {
313 printf(" %d:", ireq.i_val + 1);
314 print_string(data, ireq.i_len);
319 ireq.i_type = IEEE80211_IOC_STATIONNAME;
320 if (ioctl(s, SIOCG80211, &ireq) != -1) {
321 printf("\tstationname ");
322 print_string(data, ireq.i_len);
326 ireq.i_type = IEEE80211_IOC_CHANNEL;
327 if (ioctl(s, SIOCG80211, &ireq) < 0) {
330 printf("\tchannel %d", ireq.i_val);
332 ireq.i_type = IEEE80211_IOC_AUTHMODE;
333 if (ioctl(s, SIOCG80211, &ireq) != -1) {
335 switch (ireq.i_val) {
336 case IEEE80211_AUTH_NONE:
339 case IEEE80211_AUTH_OPEN:
342 case IEEE80211_AUTH_SHARED:
351 ireq.i_type = IEEE80211_IOC_POWERSAVE;
352 if (ioctl(s, SIOCG80211, &ireq) != -1 &&
353 ireq.i_val != IEEE80211_POWERSAVE_NOSUP ) {
354 printf(" powersavemode");
355 switch (ireq.i_val) {
356 case IEEE80211_POWERSAVE_OFF:
359 case IEEE80211_POWERSAVE_CAM:
362 case IEEE80211_POWERSAVE_PSP:
365 case IEEE80211_POWERSAVE_PSP_CAM:
370 ireq.i_type = IEEE80211_IOC_POWERSAVESLEEP;
371 if (ioctl(s, SIOCG80211, &ireq) != -1) {
373 printf(" powersavesleep %d", ireq.i_val);
379 ireq.i_type = IEEE80211_IOC_WEP;
380 if (ioctl(s, SIOCG80211, &ireq) != -1 &&
381 ireq.i_val != IEEE80211_WEP_NOSUP) {
383 switch (ireq.i_val) {
384 case IEEE80211_WEP_OFF:
387 case IEEE80211_WEP_ON:
390 case IEEE80211_WEP_MIXED:
399 * If we get here then we've got WEP support so we need
400 * to print WEP status.
403 ireq.i_type = IEEE80211_IOC_WEPTXKEY;
404 if (ioctl(s, SIOCG80211, &ireq) < 0) {
405 warn("WEP support, but no tx key!");
408 printf(" weptxkey %d", ireq.i_val+1);
410 ireq.i_type = IEEE80211_IOC_NUMWEPKEYS;
411 if (ioctl(s, SIOCG80211, &ireq) < 0) {
412 warn("WEP support, but no NUMWEPKEYS support!");
419 ireq.i_type = IEEE80211_IOC_WEPKEY;
421 for(i = 0; i < num; i++) {
423 if (ioctl(s, SIOCG80211, &ireq) < 0) {
424 warn("WEP support, but can get keys!");
427 if(ireq.i_len == 0 || ireq.i_len > 13)
429 printf("%cwepkey %d:%s", spacer, i+1,
430 ireq.i_len <= 5 ? "64-bit" : "128-bit");
443 set80211(int s, int type, int val, int len, u_int8_t *data)
445 struct ieee80211req ireq;
447 (void) memset(&ireq, 0, sizeof(ireq));
448 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
453 if(ioctl(s, SIOCS80211, &ireq) < 0)
454 err(1, "SIOCS80211");
458 get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
466 hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
472 if (sep != NULL && strchr(sep, *val) != NULL) {
477 if (!isxdigit((u_char)val[0]) ||
478 !isxdigit((u_char)val[1])) {
479 warnx("bad hexadecimal digits");
485 warnx("hexadecimal digits too long");
487 warnx("strings too long");
491 #define tohex(x) (isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
492 *p++ = (tohex((u_char)val[0]) << 4) |
493 tohex((u_char)val[1]);
500 /* The string "-" is treated as the empty string. */
501 if (!hexstr && len == 1 && buf[0] == '-')
504 memset(p, 0, *lenp - len);
510 print_string(const u_int8_t *buf, int len)
517 for(; i < len; i++) {
518 if (!isprint(buf[i]) && buf[i] != '\0')
524 if (hasspc || len == 0 || buf[0] == '\0')
525 printf("\"%.*s\"", len, buf);
527 printf("%.*s", len, buf);
530 for (i = 0; i < len; i++)
531 printf("%02x", buf[i]);