OSDN Git Service

v26
[android-x86/external-wireless-tools.git] / wireless_tools / iwpriv.c
1 /*
2  *      Wireless Tools
3  *
4  *              Jean II - HPLB 97->99 - HPL 99->00
5  *
6  * Main code for "iwconfig". This is the generic tool for most
7  * manipulations...
8  * You need to link this code against "iwlib.c" and "-lm".
9  *
10  * This file is released under the GPL license.
11  *     Copyright (c) 1997-2002 Jean Tourrilhes <jt@hpl.hp.com>
12  */
13
14 #include "iwlib.h"              /* Header */
15
16 /************************** DOCUMENTATION **************************/
17
18 /*
19  * IOCTL RANGES :
20  * ------------
21  *      The initial implementation of iwpriv was using the SIOCDEVPRIVATE
22  * ioctl range (up to 16 ioctls - driver specific). However, this was
23  * causing some compatibility problems with other usages of those
24  * ioctls, and those ioctls are supposed to be removed.
25  *      Therefore, I created a new ioctl range, at SIOCIWFIRSTPRIV. Those
26  * ioctls are specific to Wireless Extensions, so you don't have to
27  * worry about collisions with other usages. On the other hand, in the
28  * new range, the SET convention is enforced (see below).
29  *      The differences are :           SIOCDEVPRIVATE  SIOCIWFIRSTPRIV
30  *              o availability :        <= 2.5.X        WE > 11 (>= 2.4.13)
31  *              o collisions            yes             no
32  *              o SET convention        optional        enforced
33  *              o number                16              32
34  *
35  * NEW DRIVER API :
36  * --------------
37  *      Wireless Extension 13 introduce a new driver API. Wireless
38  * Extensions requests can be handled via a iw_handler table instead
39  * of through the regular ioctl handler.
40  *      The new driver API can be handled only with the new ioctl range
41  * and enforce the GET convention (see below).
42  *      The differences are :           old API         new API
43  *              o handler               do_ioctl()      struct iw_handler_def
44  *              o SIOCIWFIRSTPRIV       WE > 11         yes
45  *              o SIOCDEVPRIVATE        yes             no
46  *              o GET convention        optional        enforced
47  *      Note that the new API before Wireless Extension 15 contains bugs
48  * with regards to handling sub-ioctls and addr/float data types.
49  *
50  * SET/GET CONVENTION :
51  * ------------------
52  *      The regular Wireless Extensions use a SET/GET convention, where
53  * the low order bit identify a SET (0) or a GET (1) request.
54  *      The new ioctl range enforce the SET convention : SET request will
55  * be available to root only and can't return any arguments. If you don't
56  * like that, just use every other two ioctl.
57  *      The new driver API enforce the GET convention : GET request won't
58  * be able to accept any arguments (except if its fits within (union
59  * iwreq_data)). If you don't like that, just use the old API (aka the
60  * ioctl handler).
61  *      In any case, it's a good idea to not have ioctl with both SET
62  * and GET arguments. If the GET arguments doesn't fit within
63  * (union iwreq_data) and SET do, or vice versa, the current code in iwpriv
64  * won't work. One exception is if both SET and GET arguments fit within
65  * (union iwreq_data), this case should be handled safely in a GET
66  * request.
67  *
68  * SUB-IOCTLS :
69  * ----------
70  *      Wireless Extension 15 introduce sub-ioctls. For some applications,
71  * 32 ioctl is not enough, and this simple mechanism allow to increase
72  * the number of ioctls by adding a sub-ioctl index to some of the ioctl
73  * (so basically a two level addressing).
74  *      One might argue that at the point, some other mechanisms might be
75  * better, like using a real filesystem abstraction (/proc, driverfs, ...),
76  * but sub-ioctls are simple enough to not have much drawbacks (which means
77  * that it's a quick and dirty hack ;-).
78  *
79  *      There is two slightly different variation of the sub-ioctl scheme :
80  *      If the payload fit within (union iwreq_data), the first int (4 bytes)
81  * is reserved as the sub-ioctl number and the regular payload shifted by
82  * 4 bytes.
83  *      If the ioctl use (struct iw_point), the sub-ioctl number is in the
84  * flags member of the structure.
85  *      Then, in your handler you would just extract the sub-ioctl number
86  * and do the appropriate processing.
87  *
88  *      Sub-ioctls are declared normally in the private definition table,
89  * with cmd (first arg) beeing the sub-ioctl number. Then, you need to
90  * declare the real ioctl which will process the sub-ioctls with the
91  * SAME ARGUMENTS and a NULL NAME.
92  *      It could look like, for example :
93  * --------------------------------------------
94         // --- Raw access to sub-ioctl handlers ---
95         { 0x8BE0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 2, 0, "set_paramN" },
96         { 0x8BE1, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
97           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "get_paramN" },
98         // --- sub-ioctls handlers ---
99         { 0x8BE0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "" },
100         { 0x8BE1, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "" },
101         // --- sub-ioctls definitions ---
102         { 1, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "set_param1" },
103         { 1, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "get_param1" },
104         { 2, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "set_param2" },
105         { 2, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "get_param2" },
106  * --------------------------------------------
107  *      And iwpriv should do the rest for you ;-)
108  *
109  *      Note that version of iwpriv up to v24 (included) expect at most
110  * 16 ioctls definitions and will likely crash when given more.
111  *      There is no fix that I can see, apart from recommending an upgrade
112  * of Wireless Tools. Wireless Extensions 15 will check this condition, so
113  * another workaround is restricting those extra definitions to WE-15.
114  *
115  *      Another problem is that new API before Wireless Extension 15
116  * will get it wrong when passing fixed arguments of 12-15 bytes. It will
117  * try to get them inline instead of by pointer. You can fool the new API
118  * to do the right thing using fake ioctl definitions (but remember that
119  * you will get more likely to hit the limit of 16 ioctl definitions).
120  *      For safety, use the ioctl handler before v15.
121  *
122  * NEW DATA TYPES (ADDR/FLOAT) :
123  * ---------------------------
124  *      Wireless Tools 25 introduce two new data types, addr and float,
125  * corresponding to struct sockaddr and struct iwfreq.
126  *      Those types are properly handled with Wireless Extensions 15.
127  * However, the new API before v15 won't handle them properly.
128  *
129  *      The first problem is that the new API won't know their size, so
130  * won't copy them. This can be workaround with a fake ioctl definition.
131  *      The second problem is that a fixed single addr won't be inlined
132  * in struct iwreq and will be passed as a pointer. This is due to an
133  * off-by-one error, where all fixed data of 16 bytes is considered too
134  * big to fit in struct iwreq.
135  *
136  *      For those reasons, I would recommend to use the ioctl handler
137  * before v15 when manipulating those data.
138  *
139  * TOKEN INDEX :
140  * -----------
141  *      Token index is very similar to sub-ioctl. It allow the user
142  * to specify an integer index in front of a bunch of other arguments
143  * (addresses, strings, ...).
144  *      Token index works only with data passed as pointer, and is
145  * otherwise ignored. If your data would fit within struct iwreq, you
146  * need to declare the command *without* IW_PRIV_SIZE_FIXED to force
147  * this to happen (and check arg number yourself).
148  * --------------------------------------------
149         // --- Commands that would fit in struct iwreq ---
150         { 0x8BE0, IW_PRIV_TYPE_ADDR | 1, 0, "set_param_with_token" },
151         // --- No problem here ---
152         { 0x8BE1, IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 2, 0, "again" },
153  * --------------------------------------------
154  *      The token index feature is pretty transparent, the token index
155  * will just be in the flags member of (struct iw_point). Default value
156  * (if the user doesn't specify it) will be 0. Token index itself will
157  * work with any version of Wireless Extensions.
158  *      Token index is not compatible with sub-ioctl (both use the same
159  * field of struct iw_point). However, token index can be use to offer
160  * raw access to the sub-ioctl handlers (if it uses struct iw_point) :
161  * --------------------------------------------
162         // --- sub-ioctls handler ---
163         { 0x8BE0, IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1, 0, "" },
164         // --- sub-ioctls definitions ---
165         { 0, IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1, 0, "setaddr" },
166         { 1, IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1, 0, "deladdr" },
167         // --- raw access with token index (+ iwreq workaround) ---
168         { 0x8BE0, IW_PRIV_TYPE_ADDR | 1, 0, "rawaddr" },
169  * --------------------------------------------
170  *
171  * Jean II
172  */
173
174 /**************************** CONSTANTS ****************************/
175
176 static const char *     argtype[] = {
177   "     ", "byte ", "char ", "", "int  ", "float", "addr " };
178
179 #define IW_MAX_PRIV_DEF 128
180
181 /* Backward compatibility */
182 #ifndef IW_PRIV_TYPE_ADDR
183 #define IW_PRIV_TYPE_ADDR       0x6000
184 #endif  /* IW_PRIV_TYPE_ADDR */
185
186 /************************* MISC SUBROUTINES **************************/
187
188 /*------------------------------------------------------------------*/
189 /*
190  * Print usage string
191  */
192 static void
193 iw_usage(void)
194 {
195   fprintf(stderr, "Usage: iwpriv interface [private-command [private-arguments]]\n");
196   fprintf(stderr, "              interface [roam {on|off}]\n");
197   fprintf(stderr, "              interface [port {ad-hoc|managed|N}]\n");
198 }
199
200 /************************* SETTING ROUTINES **************************/
201
202 /*------------------------------------------------------------------*/
203 /*
204  * Execute a private command on the interface
205  */
206 static int
207 set_private_cmd(int             skfd,           /* Socket */
208                 char *          args[],         /* Command line args */
209                 int             count,          /* Args count */
210                 char *          ifname,         /* Dev name */
211                 char *          cmdname,        /* Command name */
212                 iwprivargs *    priv,           /* Private ioctl description */
213                 int             priv_num)       /* Number of descriptions */
214 {
215   struct iwreq  wrq;
216   u_char        buffer[4096];   /* Only that big in v25 and later */
217   int           i = 0;          /* Start with first command arg */
218   int           k;              /* Index in private description table */
219   int           temp;
220   int           subcmd = 0;     /* sub-ioctl index */
221   int           offset = 0;     /* Space for sub-ioctl index */
222
223   /* Check if we have a token index.
224    * Do it know so that sub-ioctl takes precendence, and so that we
225    * don't have to bother with it later on... */
226   if((count > 1) && (sscanf(args[0], "[%i]", &temp) == 1))
227     {
228       subcmd = temp;
229       args++;
230       count--;
231     }
232
233   /* Search the correct ioctl */
234   k = -1;
235   while((++k < priv_num) && strcmp(priv[k].name, cmdname));
236
237   /* If not found... */
238   if(k == priv_num)
239     {
240       fprintf(stderr, "Invalid command : %s\n", cmdname);
241       return(-1);
242     }
243           
244   /* Watch out for sub-ioctls ! */
245   if(priv[k].cmd < SIOCDEVPRIVATE)
246     {
247       int       j = -1;
248
249       /* Find the matching *real* ioctl */
250       while((++j < priv_num) && ((priv[j].name[0] != '\0') ||
251                                  (priv[j].set_args != priv[k].set_args) ||
252                                  (priv[j].get_args != priv[k].get_args)));
253
254       /* If not found... */
255       if(j == priv_num)
256         {
257           fprintf(stderr, "Invalid private ioctl definition for : %s\n",
258                   cmdname);
259           return(-1);
260         }
261
262       /* Save sub-ioctl number */
263       subcmd = priv[k].cmd;
264       /* Reserve one int (simplify alignement issues) */
265       offset = sizeof(__u32);
266       /* Use real ioctl definition from now on */
267       k = j;
268
269       printf("<mapping sub-ioctl %s to cmd 0x%X-%d>\n", cmdname,
270              priv[k].cmd, subcmd);
271     }
272
273   /* If we have to set some data */
274   if((priv[k].set_args & IW_PRIV_TYPE_MASK) &&
275      (priv[k].set_args & IW_PRIV_SIZE_MASK))
276     {
277       switch(priv[k].set_args & IW_PRIV_TYPE_MASK)
278         {
279         case IW_PRIV_TYPE_BYTE:
280           /* Number of args to fetch */
281           wrq.u.data.length = count;
282           if(wrq.u.data.length > (priv[k].set_args & IW_PRIV_SIZE_MASK))
283             wrq.u.data.length = priv[k].set_args & IW_PRIV_SIZE_MASK;
284
285           /* Fetch args */
286           for(; i < wrq.u.data.length; i++) {
287             sscanf(args[i], "%i", &temp);
288             buffer[i] = (char) temp;
289           }
290           break;
291
292         case IW_PRIV_TYPE_INT:
293           /* Number of args to fetch */
294           wrq.u.data.length = count;
295           if(wrq.u.data.length > (priv[k].set_args & IW_PRIV_SIZE_MASK))
296             wrq.u.data.length = priv[k].set_args & IW_PRIV_SIZE_MASK;
297
298           /* Fetch args */
299           for(; i < wrq.u.data.length; i++) {
300             sscanf(args[i], "%i", &temp);
301             ((__s32 *) buffer)[i] = (__s32) temp;
302           }
303           break;
304
305         case IW_PRIV_TYPE_CHAR:
306           if(i < count)
307             {
308               /* Size of the string to fetch */
309               wrq.u.data.length = strlen(args[i]) + 1;
310               if(wrq.u.data.length > (priv[k].set_args & IW_PRIV_SIZE_MASK))
311                 wrq.u.data.length = priv[k].set_args & IW_PRIV_SIZE_MASK;
312
313               /* Fetch string */
314               memcpy(buffer, args[i], wrq.u.data.length);
315               buffer[sizeof(buffer) - 1] = '\0';
316               i++;
317             }
318           else
319             {
320               wrq.u.data.length = 1;
321               buffer[0] = '\0';
322             }
323           break;
324
325         case IW_PRIV_TYPE_FLOAT:
326           /* Number of args to fetch */
327           wrq.u.data.length = count;
328           if(wrq.u.data.length > (priv[k].set_args & IW_PRIV_SIZE_MASK))
329             wrq.u.data.length = priv[k].set_args & IW_PRIV_SIZE_MASK;
330
331           /* Fetch args */
332           for(; i < wrq.u.data.length; i++) {
333             double              freq;
334             if(sscanf(args[i], "%lg", &(freq)) != 1)
335               {
336                 printf("Invalid float [%s]...\n", args[i]);
337                 return(-1);
338               }    
339             if(index(args[i], 'G')) freq *= GIGA;
340             if(index(args[i], 'M')) freq *= MEGA;
341             if(index(args[i], 'k')) freq *= KILO;
342             sscanf(args[i], "%i", &temp);
343             iw_float2freq(freq, ((struct iw_freq *) buffer) + i);
344           }
345           break;
346
347         case IW_PRIV_TYPE_ADDR:
348           /* Number of args to fetch */
349           wrq.u.data.length = count;
350           if(wrq.u.data.length > (priv[k].set_args & IW_PRIV_SIZE_MASK))
351             wrq.u.data.length = priv[k].set_args & IW_PRIV_SIZE_MASK;
352
353           /* Fetch args */
354           for(; i < wrq.u.data.length; i++) {
355             if(iw_in_addr(skfd, ifname, args[i],
356                           ((struct sockaddr *) buffer) + i) < 0)
357               {
358                 printf("Invalid address [%s]...\n", args[i]);
359                 return(-1);
360               }
361           }
362           break;
363
364         default:
365           fprintf(stderr, "Not yet implemented...\n");
366           return(-1);
367         }
368           
369       if((priv[k].set_args & IW_PRIV_SIZE_FIXED) &&
370          (wrq.u.data.length != (priv[k].set_args & IW_PRIV_SIZE_MASK)))
371         {
372           printf("The command %s need exactly %d argument...\n",
373                  cmdname, priv[k].set_args & IW_PRIV_SIZE_MASK);
374           return(-1);
375         }
376     }   /* if args to set */
377   else
378     {
379       wrq.u.data.length = 0L;
380     }
381
382   strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
383
384   /* Those two tests are important. They define how the driver
385    * will have to handle the data */
386   if((priv[k].set_args & IW_PRIV_SIZE_FIXED) &&
387       ((iw_get_priv_size(priv[k].set_args) + offset) <= IFNAMSIZ))
388     {
389       /* First case : all SET args fit within wrq */
390       if(offset)
391         wrq.u.mode = subcmd;
392       memcpy(wrq.u.name + offset, buffer, IFNAMSIZ - offset);
393     }
394   else
395     {
396       if((priv[k].set_args == 0) &&
397          (priv[k].get_args & IW_PRIV_SIZE_FIXED) &&
398          (iw_get_priv_size(priv[k].get_args) <= IFNAMSIZ))
399         {
400           /* Second case : no SET args, GET args fit within wrq */
401           if(offset)
402             wrq.u.mode = subcmd;
403         }
404       else
405         {
406           /* Thirst case : args won't fit in wrq, or variable number of args */
407           wrq.u.data.pointer = (caddr_t) buffer;
408           wrq.u.data.flags = subcmd;
409         }
410     }
411
412   /* Perform the private ioctl */
413   if(ioctl(skfd, priv[k].cmd, &wrq) < 0)
414     {
415       fprintf(stderr, "Interface doesn't accept private ioctl...\n");
416       fprintf(stderr, "%s (%X): %s\n", cmdname, priv[k].cmd, strerror(errno));
417       return(-1);
418     }
419
420   /* If we have to get some data */
421   if((priv[k].get_args & IW_PRIV_TYPE_MASK) &&
422      (priv[k].get_args & IW_PRIV_SIZE_MASK))
423     {
424       int       j;
425       int       n = 0;          /* number of args */
426
427       printf("%-8.8s  %s:", ifname, cmdname);
428
429       /* Check where is the returned data */
430       if((priv[k].get_args & IW_PRIV_SIZE_FIXED) &&
431          (iw_get_priv_size(priv[k].get_args) <= IFNAMSIZ))
432         {
433           memcpy(buffer, wrq.u.name, IFNAMSIZ);
434           n = priv[k].get_args & IW_PRIV_SIZE_MASK;
435         }
436       else
437         n = wrq.u.data.length;
438
439       switch(priv[k].get_args & IW_PRIV_TYPE_MASK)
440         {
441         case IW_PRIV_TYPE_BYTE:
442           /* Display args */
443           for(j = 0; j < n; j++)
444             printf("%d  ", buffer[j]);
445           printf("\n");
446           break;
447
448         case IW_PRIV_TYPE_INT:
449           /* Display args */
450           for(j = 0; j < n; j++)
451             printf("%d  ", ((__s32 *) buffer)[j]);
452           printf("\n");
453           break;
454
455         case IW_PRIV_TYPE_CHAR:
456           /* Display args */
457           buffer[wrq.u.data.length - 1] = '\0';
458           printf("%s\n", buffer);
459           break;
460
461         case IW_PRIV_TYPE_FLOAT:
462           {
463             double              freq;
464             /* Display args */
465             for(j = 0; j < n; j++)
466               {
467                 freq = iw_freq2float(((struct iw_freq *) buffer) + j);
468                 if(freq >= GIGA)
469                   printf("%gG  ", freq / GIGA);
470                 else
471                   if(freq >= MEGA)
472                   printf("%gM  ", freq / MEGA);
473                 else
474                   printf("%gk  ", freq / KILO);
475               }
476             printf("\n");
477           }
478           break;
479
480         case IW_PRIV_TYPE_ADDR:
481           {
482             char                scratch[128];
483             struct sockaddr *   hwa;
484             /* Display args */
485             for(j = 0; j < n; j++)
486               {
487                 hwa = ((struct sockaddr *) buffer) + j;
488                 if(j)
489                   printf("           %.*s", 
490                          (int) strlen(cmdname), "                ");
491                 printf("%s\n", iw_pr_ether(scratch, hwa->sa_data));
492               }
493           }
494           break;
495
496         default:
497           fprintf(stderr, "Not yet implemented...\n");
498           return(-1);
499         }
500     }   /* if args to set */
501
502   return(0);
503 }
504
505 /*------------------------------------------------------------------*/
506 /*
507  * Execute a private command on the interface
508  */
509 static inline int
510 set_private(int         skfd,           /* Socket */
511             char *      args[],         /* Command line args */
512             int         count,          /* Args count */
513             char *      ifname)         /* Dev name */
514 {
515   iwprivargs    priv[IW_MAX_PRIV_DEF];
516   int           number;         /* Max of private ioctl */
517
518   /* Read the private ioctls */
519   number = iw_get_priv_info(skfd, ifname, priv, IW_MAX_PRIV_DEF);
520
521   /* Is there any ? */
522   if(number <= 0)
523     {
524       /* Could skip this message ? */
525       fprintf(stderr, "%-8.8s  no private ioctls.\n\n",
526               ifname);
527       return(-1);
528     }
529
530   return(set_private_cmd(skfd, args + 1, count - 1, ifname, args[0],
531                          priv, number));
532 }
533
534 /************************ CATALOG FUNCTIONS ************************/
535
536 /*------------------------------------------------------------------*/
537 /*
538  * Print on the screen in a neat fashion all the info we have collected
539  * on a device.
540  */
541 static int
542 print_priv_info(int             skfd,
543                 char *          ifname,
544                 char *          args[],
545                 int             count)
546 {
547   int           k;
548   iwprivargs    priv[IW_MAX_PRIV_DEF];
549   int           n;
550
551   /* Avoid "Unused parameter" warning */
552   args = args; count = count;
553
554   /* Read the private ioctls */
555   n = iw_get_priv_info(skfd, ifname, priv, IW_MAX_PRIV_DEF);
556
557   /* Is there any ? */
558   if(n <= 0)
559     {
560       /* Could skip this message ? */
561       fprintf(stderr, "%-8.8s  no private ioctls.\n\n",
562               ifname);
563     }
564   else
565     {
566       printf("%-8.8s  Available private ioctl :\n", ifname);
567       /* Print them all */
568       for(k = 0; k < n; k++)
569         if(priv[k].name[0] != '\0')
570           printf("          %-16.16s (%.4X) : set %3d %s & get %3d %s\n",
571                  priv[k].name, priv[k].cmd,
572                  priv[k].set_args & IW_PRIV_SIZE_MASK,
573                  argtype[(priv[k].set_args & IW_PRIV_TYPE_MASK) >> 12],
574                  priv[k].get_args & IW_PRIV_SIZE_MASK,
575                  argtype[(priv[k].get_args & IW_PRIV_TYPE_MASK) >> 12]);
576       printf("\n");
577     }
578   return(0);
579 }
580
581 /*------------------------------------------------------------------*/
582 /*
583  * Print on the screen in a neat fashion all the info we have collected
584  * on a device.
585  */
586 static int
587 print_priv_all(int              skfd,
588                char *           ifname,
589                char *           args[],
590                int              count)
591 {
592   int           k;
593   iwprivargs    priv[IW_MAX_PRIV_DEF];
594   int           n;
595
596   /* Avoid "Unused parameter" warning */
597   args = args; count = count;
598
599   /* Read the private ioctls */
600   n = iw_get_priv_info(skfd, ifname, priv, IW_MAX_PRIV_DEF);
601
602   /* Is there any ? */
603   if(n <= 0)
604     {
605       /* Could skip this message ? */
606       fprintf(stderr, "%-8.8s  no private ioctls.\n\n",
607               ifname);
608     }
609   else
610     {
611       printf("%-8.8s  Available read-only private ioctl :\n", ifname);
612       /* Print them all */
613       for(k = 0; k < n; k++)
614         /* We call all ioctl that don't have a null name, don't require
615          * args and return some (avoid triggering "reset" commands) */
616         if((priv[k].name[0] != '\0') && (priv[k].set_args == 0) &&
617            (priv[k].get_args != 0))
618           set_private_cmd(skfd, NULL, 0, ifname, priv[k].name,
619                           priv, n);
620       printf("\n");
621     }
622 #if 0
623   // Debug
624   printf("struct ifreq = %d ; struct iwreq = %d ; IFNAMSIZ = %d\n",
625          sizeof(struct ifreq), sizeof(struct iwreq), IFNAMSIZ);
626   printf("struct iw_freq = %d ; struct sockaddr = %d\n",
627          sizeof(struct iw_freq), sizeof(struct sockaddr));
628 #endif
629   return(0);
630 }
631
632 /********************** PRIVATE IOCTLS MANIPS ***********************/
633 /*
634  * Convenient access to some private ioctls of some devices
635  */
636
637 /*------------------------------------------------------------------*/
638 /*
639  * Set roaming mode on and off
640  * Found in wavelan_cs driver
641  */
642 static int
643 set_roaming(int         skfd,           /* Socket */
644             char *      args[],         /* Command line args */
645             int         count,          /* Args count */
646             char *      ifname)         /* Dev name */
647 {
648   u_char        buffer[1024];
649   struct iwreq          wrq;
650   int           i = 0;          /* Start with first arg */
651   int           k;
652   iwprivargs    priv[IW_MAX_PRIV_DEF];
653   int           number;
654   char          RoamState;              /* buffer to hold new roam state */
655   char          ChangeRoamState=0;      /* whether or not we are going to
656                                            change roam states */
657
658   /* Read the private ioctls */
659   number = iw_get_priv_info(skfd, ifname, priv, IW_MAX_PRIV_DEF);
660
661   /* Is there any ? */
662   if(number <= 0)
663     {
664       /* Could skip this message ? */
665       fprintf(stderr, "%-8.8s  no private ioctls.\n\n",
666               ifname);
667       return(-1);
668     }
669
670   if(count != 1)
671     {
672       iw_usage();
673       return(-1);
674     }
675
676   if(!strcasecmp(args[i], "on"))
677     {
678       printf("%-8.8s  enable roaming\n", ifname);
679       if(!number)
680         {
681           fprintf(stderr, "This device doesn't support roaming\n");
682           return(-1);
683         }
684       ChangeRoamState=1;
685       RoamState=1;
686     }
687   else
688     if(!strcasecmp(args[i], "off"))
689       {
690         i++;
691         printf("%-8.8s  disable roaming\n",  ifname);
692         if(!number)
693           {
694             fprintf(stderr, "This device doesn't support roaming\n");
695             return(-1);
696           }
697         ChangeRoamState=1;
698         RoamState=0;
699       }
700     else
701       {
702         iw_usage();
703         return(-1);
704       }
705
706   if(ChangeRoamState)
707     {
708       k = -1;
709       while((++k < number) && strcmp(priv[k].name, "setroam"));
710       if(k == number)
711         {
712           fprintf(stderr, "This device doesn't support roaming\n");
713           return(-1);
714         }
715       strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
716
717       buffer[0]=RoamState;
718
719       memcpy(wrq.u.name, &buffer, IFNAMSIZ);
720
721       if(ioctl(skfd, priv[k].cmd, &wrq) < 0)
722         {
723           fprintf(stderr, "Roaming support is broken.\n");
724           return(-1);
725         }
726     }
727
728   return(0);
729 }
730
731 /*------------------------------------------------------------------*/
732 /*
733  * Get and set the port type
734  * Found in wavelan2_cs and wvlan_cs drivers
735  */
736 static int
737 port_type(int           skfd,           /* Socket */
738           char *        args[],         /* Command line args */
739           int           count,          /* Args count */
740           char *        ifname)         /* Dev name */
741 {
742   struct iwreq  wrq;
743   int           i = 0;          /* Start with first arg */
744   int           k;
745   iwprivargs    priv[IW_MAX_PRIV_DEF];
746   int           number;
747   char          ptype = 0;
748   char *        modes[] = { "invalid", "managed (BSS)", "reserved", "ad-hoc" };
749
750   /* Read the private ioctls */
751   number = iw_get_priv_info(skfd, ifname, priv, IW_MAX_PRIV_DEF);
752
753   /* Is there any ? */
754   if(number <= 0)
755     {
756       /* Could skip this message ? */
757       fprintf(stderr, "%-8.8s  no private ioctls.\n\n", ifname);
758       return(-1);
759     }
760
761   /* Arguments ? */
762   if(count == 0)
763     {
764       /* So, we just want to see the current value... */
765       k = -1;
766       while((++k < number) && strcmp(priv[k].name, "gport_type") &&
767              strcmp(priv[k].name, "get_port"));
768       if(k == number)
769         {
770           fprintf(stderr, "This device doesn't support getting port type\n");
771           return(-1);
772         }
773       strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
774
775       /* Get it */
776       if(ioctl(skfd, priv[k].cmd, &wrq) < 0)
777         {
778           fprintf(stderr, "Port type support is broken.\n");
779           exit(0);
780         }
781       ptype = *wrq.u.name;
782
783       /* Display it */
784       printf("%-8.8s  Current port mode is %s <port type is %d>.\n\n",
785              ifname, modes[(int) ptype], ptype);
786
787       return(0);
788     }
789
790   if(count != 1)
791     {
792       iw_usage();
793       return(-1);
794     }
795
796   /* Read it */
797   /* As a string... */
798   k = 0;
799   while((k < 4) && strncasecmp(args[i], modes[k], 2))
800     k++;
801   if(k < 4)
802     ptype = k;
803   else
804     /* ...or as an integer */
805     if(sscanf(args[i], "%i", (int *) &ptype) != 1)
806       {
807         iw_usage();
808         return(-1);
809       }
810   
811   k = -1;
812   while((++k < number) && strcmp(priv[k].name, "sport_type") &&
813         strcmp(priv[k].name, "set_port"));
814   if(k == number)
815     {
816       fprintf(stderr, "This device doesn't support setting port type\n");
817       return(-1);
818     }
819   strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
820
821   *(wrq.u.name) = ptype;
822
823   if(ioctl(skfd, priv[k].cmd, &wrq) < 0)
824     {
825       fprintf(stderr, "Invalid port type (or setting not allowed)\n");
826       return(-1);
827     }
828
829   return(0);
830 }
831
832 /******************************* MAIN ********************************/
833
834 /*------------------------------------------------------------------*/
835 /*
836  * The main !
837  */
838 int
839 main(int        argc,
840      char **    argv)
841 {
842   int skfd;             /* generic raw socket desc.     */
843   int goterr = 0;
844
845   /* Create a channel to the NET kernel. */
846   if((skfd = iw_sockets_open()) < 0)
847     {
848       perror("socket");
849       return(-1);
850     }
851
852   /* No argument : show the list of all device + info */
853   if(argc == 1)
854     iw_enum_devices(skfd, &print_priv_info, NULL, 0);
855   else
856     /* Special cases take one... */
857     /* All */
858     if((!strncmp(argv[1], "-a", 2)) || (!strcmp(argv[1], "--all")))
859       iw_enum_devices(skfd, &print_priv_all, NULL, 0);
860     else
861       /* Help */
862       if((!strncmp(argv[1], "-h", 2)) || (!strcmp(argv[1], "--help")))
863         iw_usage();
864       else
865         /* Version */
866         if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version"))
867           goterr = iw_print_version_info("iwpriv");
868         else
869           /* The device name must be the first argument */
870           /* Name only : show for that device only */
871           if(argc == 2)
872             print_priv_info(skfd, argv[1], NULL, 0);
873           else
874             /* Special cases take two... */
875             /* All */
876             if((!strncmp(argv[2], "-a", 2)) ||
877                (!strcmp(argv[2], "--all")))
878               print_priv_all(skfd, argv[1], NULL, 0);
879             else
880               /* Roaming */
881               if(!strncmp(argv[2], "roam", 4))
882                 goterr = set_roaming(skfd, argv + 3, argc - 3, argv[1]);
883               else
884                 /* Port type */
885                 if(!strncmp(argv[2], "port", 4))
886                   goterr = port_type(skfd, argv + 3, argc - 3, argv[1]);
887                 else
888                   /*-------------*/
889                   /* Otherwise, it's a private ioctl */
890                   goterr = set_private(skfd, argv + 2, argc - 2, argv[1]);
891
892   /* Close the socket. */
893   close(skfd);
894
895   return(goterr);
896 }