Reduce warnings as part of WARNS=6 cleaning, stage 2/6 (or so):
[dragonfly.git] / sbin / ifconfig / ifieee80211.c
1 /*
2  * Copyright 2001 The Aerospace Corporation.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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  *
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
23  * SUCH DAMAGE.
24  *
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 $
27  */
28
29 /*-
30  * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
31  * All rights reserved.
32  *
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.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
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.
52  *
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.
64  */
65
66 #include <sys/param.h>
67 #include <sys/ioctl.h>
68 #include <sys/socket.h>
69 #include <sys/sysctl.h>
70 #include <sys/time.h>
71
72 #include <net/ethernet.h>
73 #include <net/if.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>
79
80 #include <ctype.h>
81 #include <err.h>
82 #include <errno.h>
83 #include <fcntl.h>
84 #include <stdio.h>
85 #include <stdlib.h>
86 #include <string.h>
87 #include <unistd.h>
88
89 #include "ifconfig.h"
90
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);
95
96 void
97 set80211ssid(const char *val, int d __unused, int s,
98              const struct afswtch *rafp __unused)
99 {
100         int             ssid;
101         int             len;
102         u_int8_t        data[33];
103
104         ssid = 0;
105         len = sizeof(val);
106         if (len > 2 && isdigit(val[0]) && val[1] == ':') {
107                 ssid = atoi(val)-1;
108                 val += 2;
109         }
110
111         bzero(data, sizeof(data));
112         len = sizeof(data);
113         get_string(val, NULL, data, &len);
114
115         set80211(s, IEEE80211_IOC_SSID, ssid, len, data);
116 }
117
118 void
119 set80211stationname(const char *val, int d __unused, int s,
120                     const struct afswtch *rafp __unused)
121 {
122         int                     len;
123         u_int8_t                data[33];
124
125         bzero(data, sizeof(data));
126         len = sizeof(data);
127         get_string(val, NULL, data, &len);
128
129         set80211(s, IEEE80211_IOC_STATIONNAME, 0, len, data);
130 }
131
132 void
133 set80211channel(const char *val, int d __unused, int s,
134                 const struct afswtch *rafp __unused)
135 {
136         set80211(s, IEEE80211_IOC_CHANNEL, atoi(val), 0, NULL);
137 }
138
139 void
140 set80211authmode(const char *val, int d __unused, int s,
141                  const struct afswtch *rafp __unused)
142 {
143         int     mode;
144
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;
151         } else {
152                 err(1, "unknown authmode");
153         }
154
155         set80211(s, IEEE80211_IOC_AUTHMODE, mode, 0, NULL);
156 }
157
158 void
159 set80211powersavemode(const char *val, int d __unused, int s,
160                       const struct afswtch *rafp __unused)
161 {
162         int     mode;
163
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;
174         } else {
175                 err(1, "unknown powersavemode");
176         }
177
178         set80211(s, IEEE80211_IOC_POWERSAVE, mode, 0, NULL);
179 }
180
181 void
182 set80211powersave(const char *val __unused, int d, int s,
183                   const struct afswtch *rafp __unused)
184 {
185         if (d == 0)
186                 set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_OFF,
187                     0, NULL);
188         else
189                 set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_ON,
190                     0, NULL);
191 }
192
193 void
194 set80211powersavesleep(const char *val, int d __unused, int s,
195                        const struct afswtch *rafp __unused)
196 {
197         set80211(s, IEEE80211_IOC_POWERSAVESLEEP, atoi(val), 0, NULL);
198 }
199
200 void
201 set80211wepmode(const char *val, int d __unused, int s,
202                 const struct afswtch *rafp __unused)
203 {
204         int     mode;
205
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;
212         } else {
213                 err(1, "unknown wep mode");
214         }
215
216         set80211(s, IEEE80211_IOC_WEP, mode, 0, NULL);
217 }
218
219 void
220 set80211wep(const char *val __unused, int d, int s,
221             const struct afswtch *rafp __unused)
222 {
223         set80211(s, IEEE80211_IOC_WEP, d, 0, NULL);
224 }
225
226 void
227 set80211weptxkey(const char *val, int d __unused, int s,
228                  const struct afswtch *rafp __unused)
229 {
230         set80211(s, IEEE80211_IOC_WEPTXKEY, atoi(val)-1, 0, NULL);
231 }
232
233 void
234 set80211wepkey(const char *val, int d __unused, int s,
235                const struct afswtch *rafp __unused)
236 {
237         int             key = 0;
238         int             len;
239         u_int8_t        data[14];
240
241         if(isdigit(val[0]) && val[1] == ':') {
242                 key = atoi(val)-1;
243                 val += 2;
244         }
245
246         bzero(data, sizeof(data));
247         len = sizeof(data);
248         get_string(val, NULL, data, &len);
249
250         set80211(s, IEEE80211_IOC_WEPKEY, key, len, data);
251 }
252
253 /*
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.
257  */
258 void
259 set80211nwkey(const char *val, int d __unused, int s,
260               const struct afswtch *rafp __unused)
261 {
262         int             txkey;
263         int             i, len;
264         u_int8_t        data[14];
265
266         set80211(s, IEEE80211_IOC_WEP, IEEE80211_WEP_ON, 0, NULL);
267
268         if(isdigit(val[0]) && val[1] == ':') {
269                 txkey = val[0]-'0'-1;
270                 val += 2;
271
272                 for(i = 0; i < 4; i++) {
273                         bzero(data, sizeof(data));
274                         len = sizeof(data);
275                         val = get_string(val, ",", data, &len);
276
277                         set80211(s, IEEE80211_IOC_WEPKEY, i, len, data);
278                 }
279         } else {
280                 bzero(data, sizeof(data));
281                 len = sizeof(data);
282                 get_string(val, NULL, data, &len);
283                 txkey = 0;
284
285                 set80211(s, IEEE80211_IOC_WEPKEY, 0, len, data);
286
287                 bzero(data, sizeof(data));
288                 for(i = 1; i < 4; i++)
289                         set80211(s, IEEE80211_IOC_WEPKEY, i, 0, data);
290         }
291
292         set80211(s, IEEE80211_IOC_WEPTXKEY, txkey, 0, NULL);
293 }
294
295 void
296 ieee80211_status (int s, struct rt_addrinfo *info __unused)
297 {
298         int                     i;
299         int                     num;
300         struct ieee80211req     ireq;
301         u_int8_t                data[32];
302         char                    spacer;
303
304         memset(&ireq, 0, sizeof(ireq));
305         strncpy(ireq.i_name, name, sizeof(ireq.i_name));
306         ireq.i_data = &data;
307
308         ireq.i_type = IEEE80211_IOC_SSID;
309         ireq.i_val = -1;
310         if (ioctl(s, SIOCG80211, &ireq) < 0) {
311                 /* If we can't get the SSID, the this isn't an 802.11 device. */
312                 return;
313         }
314         printf("\tssid ");
315         print_string(data, ireq.i_len);
316         num = 0;
317         ireq.i_type = IEEE80211_IOC_NUMSSIDS;
318         if (ioctl(s, SIOCG80211, &ireq) >= 0) {
319                 num = ireq.i_val;
320         }
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);
326                 }
327         }
328         printf("\n");
329
330         ireq.i_type = IEEE80211_IOC_STATIONNAME;
331         if (ioctl(s, SIOCG80211, &ireq) != -1) {
332                 printf("\tstationname ");
333                 print_string(data, ireq.i_len);
334                 printf("\n");
335         }
336
337         ireq.i_type = IEEE80211_IOC_CHANNEL;
338         if (ioctl(s, SIOCG80211, &ireq) < 0) {
339                 goto end;
340         }
341         printf("\tchannel %d", ireq.i_val);
342
343         ireq.i_type = IEEE80211_IOC_AUTHMODE;
344         if (ioctl(s, SIOCG80211, &ireq) != -1) {
345                 printf(" authmode");
346                 switch (ireq.i_val) {
347                         case IEEE80211_AUTH_NONE:
348                                 printf(" NONE");
349                                 break;
350                         case IEEE80211_AUTH_OPEN:
351                                 printf(" OPEN");
352                                 break;
353                         case IEEE80211_AUTH_SHARED:
354                                 printf(" SHARED");
355                                 break;
356                         default:
357                                 printf(" UNKNOWN");
358                                 break;
359                 }
360         }
361
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:
368                                 printf(" OFF");
369                                 break;
370                         case IEEE80211_POWERSAVE_CAM:
371                                 printf(" CAM");
372                                 break;
373                         case IEEE80211_POWERSAVE_PSP:
374                                 printf(" PSP");
375                                 break;
376                         case IEEE80211_POWERSAVE_PSP_CAM:
377                                 printf(" PSP-CAM");
378                                 break;
379                 }
380
381                 ireq.i_type = IEEE80211_IOC_POWERSAVESLEEP;
382                 if (ioctl(s, SIOCG80211, &ireq) != -1) {
383                         if(ireq.i_val)
384                                 printf(" powersavesleep %d", ireq.i_val);
385                 }
386         }
387
388         printf("\n");
389
390         ireq.i_type = IEEE80211_IOC_WEP;
391         if (ioctl(s, SIOCG80211, &ireq) != -1 &&
392             ireq.i_val != IEEE80211_WEP_NOSUP) {
393                 printf("\twepmode");
394                 switch (ireq.i_val) {
395                         case IEEE80211_WEP_OFF:
396                                 printf(" OFF");
397                                 break;
398                         case IEEE80211_WEP_ON:
399                                 printf(" ON");
400                                 break;
401                         case IEEE80211_WEP_MIXED:
402                                 printf(" MIXED");
403                                 break;
404                         default:
405                                 printf(" UNKNOWN");
406                                 break;
407                 }
408
409                 /*
410                  * If we get here then we've got WEP support so we need
411                  * to print WEP status.
412                  */ 
413
414                 ireq.i_type = IEEE80211_IOC_WEPTXKEY;
415                 if (ioctl(s, SIOCG80211, &ireq) < 0) {
416                         warn("WEP support, but no tx key!");
417                         goto end;
418                 }
419                 printf(" weptxkey %d", ireq.i_val+1);
420
421                 ireq.i_type = IEEE80211_IOC_NUMWEPKEYS;
422                 if (ioctl(s, SIOCG80211, &ireq) < 0) {
423                         warn("WEP support, but no NUMWEPKEYS support!");
424                         goto end;
425                 }
426                 num = ireq.i_val;
427
428                 printf("\n");
429
430                 ireq.i_type = IEEE80211_IOC_WEPKEY;
431                 spacer = '\t';
432                 for(i = 0; i < num; i++) {
433                         ireq.i_val = i;
434                         if (ioctl(s, SIOCG80211, &ireq) < 0) {
435                                 warn("WEP support, but can get keys!");
436                                 goto end;
437                         }
438                         if(ireq.i_len == 0 || ireq.i_len > 13)
439                                 continue;
440                         printf("%cwepkey %d:%s", spacer, i+1,
441                             ireq.i_len <= 5 ? "64-bit" : "128-bit");
442                         if(spacer == '\t')
443                                 spacer = ' ';
444                 }
445                 if (spacer == ' ')
446                         printf("\n");
447         }
448
449 end:
450         return;
451 }
452
453 static void
454 set80211(int s, int type, int val, int len, u_int8_t *data)
455 {
456         struct ieee80211req     ireq;
457
458         memset(&ireq, 0, sizeof(ireq));
459         strncpy(ireq.i_name, name, sizeof(ireq.i_name));
460         ireq.i_type = type;
461         ireq.i_val = val;
462         ireq.i_len = len;
463         ireq.i_data = data;
464         if(ioctl(s, SIOCS80211, &ireq) < 0)
465                 err(1, "SIOCS80211");
466 }
467
468 static const char *
469 get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
470 {
471         int len;
472         int hexstr;
473         u_int8_t *p;
474
475         len = *lenp;
476         p = buf;
477         hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
478         if (hexstr)
479                 val += 2;
480         for (;;) {
481                 if (*val == '\0')
482                         break;
483                 if (sep != NULL && strchr(sep, *val) != NULL) {
484                         val++;
485                         break;
486                 }
487                 if (hexstr) {
488                         if (!isxdigit((u_char)val[0]) ||
489                             !isxdigit((u_char)val[1])) {
490                                 warnx("bad hexadecimal digits");
491                                 return NULL;
492                         }
493                 }
494                 if (p > buf + len) {
495                         if (hexstr)
496                                 warnx("hexadecimal digits too long");
497                         else
498                                 warnx("strings too long");
499                         return NULL;
500                 }
501                 if (hexstr) {
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]);
505 #undef tohex
506                         val += 2;
507                 } else
508                         *p++ = *val++;
509         }
510         len = p - buf;
511         /* The string "-" is treated as the empty string. */
512         if (!hexstr && len == 1 && buf[0] == '-')
513                 len = 0;
514         if (len < *lenp)
515                 memset(p, 0, *lenp - len);
516         *lenp = len;
517         return val;
518 }
519
520 static void
521 print_string(const u_int8_t *buf, int len)
522 {
523         int i;
524         int hasspc;
525
526         i = 0;
527         hasspc = 0;
528         for(; i < len; i++) {
529                 if (!isprint(buf[i]) && buf[i] != '\0')
530                         break;
531                 if (isspace(buf[i]))
532                         hasspc++;
533         }
534         if (i == len) {
535                 if (hasspc || len == 0 || buf[0] == '\0')
536                         printf("\"%.*s\"", len, buf);
537                 else
538                         printf("%.*s", len, buf);
539         } else {
540                 printf("0x");
541                 for (i = 0; i < len; i++)
542                         printf("%02x", buf[i]);
543         }
544 }
545