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.4 2003/11/22 19:30:55 asmodai 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 compatibility 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 (int s, struct rt_addrinfo *info __unused)
287 struct ieee80211req ireq;
291 (void) memset(&ireq, 0, sizeof(ireq));
292 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
295 ireq.i_type = IEEE80211_IOC_SSID;
297 if (ioctl(s, SIOCG80211, &ireq) < 0) {
298 /* If we can't get the SSID, the this isn't an 802.11 device. */
302 print_string(data, ireq.i_len);
304 ireq.i_type = IEEE80211_IOC_NUMSSIDS;
305 if (ioctl(s, SIOCG80211, &ireq) >= 0) {
308 ireq.i_type = IEEE80211_IOC_SSID;
309 for (ireq.i_val = 0; ireq.i_val < num; ireq.i_val++) {
310 if (ioctl(s, SIOCG80211, &ireq) >= 0 && ireq.i_len > 0) {
311 printf(" %d:", ireq.i_val + 1);
312 print_string(data, ireq.i_len);
317 ireq.i_type = IEEE80211_IOC_STATIONNAME;
318 if (ioctl(s, SIOCG80211, &ireq) != -1) {
319 printf("\tstationname ");
320 print_string(data, ireq.i_len);
324 ireq.i_type = IEEE80211_IOC_CHANNEL;
325 if (ioctl(s, SIOCG80211, &ireq) < 0) {
328 printf("\tchannel %d", ireq.i_val);
330 ireq.i_type = IEEE80211_IOC_AUTHMODE;
331 if (ioctl(s, SIOCG80211, &ireq) != -1) {
333 switch (ireq.i_val) {
334 case IEEE80211_AUTH_NONE:
337 case IEEE80211_AUTH_OPEN:
340 case IEEE80211_AUTH_SHARED:
349 ireq.i_type = IEEE80211_IOC_POWERSAVE;
350 if (ioctl(s, SIOCG80211, &ireq) != -1 &&
351 ireq.i_val != IEEE80211_POWERSAVE_NOSUP ) {
352 printf(" powersavemode");
353 switch (ireq.i_val) {
354 case IEEE80211_POWERSAVE_OFF:
357 case IEEE80211_POWERSAVE_CAM:
360 case IEEE80211_POWERSAVE_PSP:
363 case IEEE80211_POWERSAVE_PSP_CAM:
368 ireq.i_type = IEEE80211_IOC_POWERSAVESLEEP;
369 if (ioctl(s, SIOCG80211, &ireq) != -1) {
371 printf(" powersavesleep %d", ireq.i_val);
377 ireq.i_type = IEEE80211_IOC_WEP;
378 if (ioctl(s, SIOCG80211, &ireq) != -1 &&
379 ireq.i_val != IEEE80211_WEP_NOSUP) {
381 switch (ireq.i_val) {
382 case IEEE80211_WEP_OFF:
385 case IEEE80211_WEP_ON:
388 case IEEE80211_WEP_MIXED:
397 * If we get here then we've got WEP support so we need
398 * to print WEP status.
401 ireq.i_type = IEEE80211_IOC_WEPTXKEY;
402 if (ioctl(s, SIOCG80211, &ireq) < 0) {
403 warn("WEP support, but no tx key!");
406 printf(" weptxkey %d", ireq.i_val+1);
408 ireq.i_type = IEEE80211_IOC_NUMWEPKEYS;
409 if (ioctl(s, SIOCG80211, &ireq) < 0) {
410 warn("WEP support, but no NUMWEPKEYS support!");
417 ireq.i_type = IEEE80211_IOC_WEPKEY;
419 for(i = 0; i < num; i++) {
421 if (ioctl(s, SIOCG80211, &ireq) < 0) {
422 warn("WEP support, but can get keys!");
425 if(ireq.i_len == 0 || ireq.i_len > 13)
427 printf("%cwepkey %d:%s", spacer, i+1,
428 ireq.i_len <= 5 ? "64-bit" : "128-bit");
441 set80211(int s, int type, int val, int len, u_int8_t *data)
443 struct ieee80211req ireq;
445 (void) memset(&ireq, 0, sizeof(ireq));
446 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
451 if(ioctl(s, SIOCS80211, &ireq) < 0)
452 err(1, "SIOCS80211");
456 get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
464 hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
470 if (sep != NULL && strchr(sep, *val) != NULL) {
475 if (!isxdigit((u_char)val[0]) ||
476 !isxdigit((u_char)val[1])) {
477 warnx("bad hexadecimal digits");
483 warnx("hexadecimal digits too long");
485 warnx("strings too long");
489 #define tohex(x) (isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
490 *p++ = (tohex((u_char)val[0]) << 4) |
491 tohex((u_char)val[1]);
498 /* The string "-" is treated as the empty string. */
499 if (!hexstr && len == 1 && buf[0] == '-')
502 memset(p, 0, *lenp - len);
508 print_string(const u_int8_t *buf, int len)
515 for(; i < len; i++) {
516 if (!isprint(buf[i]) && buf[i] != '\0')
522 if (hasspc || len == 0 || buf[0] == '\0')
523 printf("\"%.*s\"", len, buf);
525 printf("%.*s", len, buf);
528 for (i = 0; i < len; i++)
529 printf("%02x", buf[i]);