-
Notifications
You must be signed in to change notification settings - Fork 0
/
iwconfig.c
1966 lines (1747 loc) · 46.4 KB
/
iwconfig.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Wireless Tools
*
* Jean II - HPLB 97->99 - HPL 99->07
*
* Main code for "iwconfig". This is the generic tool for most
* manipulations...
* You need to link this code against "iwlib.c" and "-lm".
*
* This file is released under the GPL license.
* Copyright (c) 1997-2007 Jean Tourrilhes <jt@hpl.hp.com>
*/
#include "iwlib.h" /* Header */
/**************************** CONSTANTS ****************************/
/*
* Error codes defined for setting args
*/
#define IWERR_ARG_NUM -2
#define IWERR_ARG_TYPE -3
#define IWERR_ARG_SIZE -4
#define IWERR_ARG_CONFLICT -5
#define IWERR_SET_EXT -6
#define IWERR_GET_EXT -7
/**************************** VARIABLES ****************************/
/*
* Ugly, but deal with errors in set_info() efficiently...
*/
static int errarg;
static int errmax;
/************************* DISPLAY ROUTINES **************************/
/*------------------------------------------------------------------*/
/*
* Get wireless informations & config from the device driver
* We will call all the classical wireless ioctl on the driver through
* the socket to know what is supported and to get the settings...
*/
static int
get_info(int skfd,
char * ifname,
struct wireless_info * info)
{
struct iwreq wrq;
memset((char *) info, 0, sizeof(struct wireless_info));
/* Get basic information */
if(iw_get_basic_config(skfd, ifname, &(info->b)) < 0)
{
/* If no wireless name : no wireless extensions */
/* But let's check if the interface exists at all */
struct ifreq ifr;
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
if(ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
return(-ENODEV);
else
return(-ENOTSUP);
}
/* Get ranges */
if(iw_get_range_info(skfd, ifname, &(info->range)) >= 0)
info->has_range = 1;
/* Get AP address */
if(iw_get_ext(skfd, ifname, SIOCGIWAP, &wrq) >= 0)
{
info->has_ap_addr = 1;
memcpy(&(info->ap_addr), &(wrq.u.ap_addr), sizeof (sockaddr));
}
/* Get bit rate */
if(iw_get_ext(skfd, ifname, SIOCGIWRATE, &wrq) >= 0)
{
info->has_bitrate = 1;
memcpy(&(info->bitrate), &(wrq.u.bitrate), sizeof(iwparam));
}
/* Get Power Management settings */
wrq.u.power.flags = 0;
if(iw_get_ext(skfd, ifname, SIOCGIWPOWER, &wrq) >= 0)
{
info->has_power = 1;
memcpy(&(info->power), &(wrq.u.power), sizeof(iwparam));
}
/* Get stats */
if(iw_get_stats(skfd, ifname, &(info->stats),
&info->range, info->has_range) >= 0)
{
info->has_stats = 1;
}
#ifndef WE_ESSENTIAL
/* Get NickName */
wrq.u.essid.pointer = (caddr_t) info->nickname;
wrq.u.essid.length = IW_ESSID_MAX_SIZE + 2;
wrq.u.essid.flags = 0;
if(iw_get_ext(skfd, ifname, SIOCGIWNICKN, &wrq) >= 0)
if(wrq.u.data.length > 1)
info->has_nickname = 1;
if((info->has_range) && (info->range.we_version_compiled > 9))
{
/* Get Transmit Power */
if(iw_get_ext(skfd, ifname, SIOCGIWTXPOW, &wrq) >= 0)
{
info->has_txpower = 1;
memcpy(&(info->txpower), &(wrq.u.txpower), sizeof(iwparam));
}
}
/* Get sensitivity */
if(iw_get_ext(skfd, ifname, SIOCGIWSENS, &wrq) >= 0)
{
info->has_sens = 1;
memcpy(&(info->sens), &(wrq.u.sens), sizeof(iwparam));
}
if((info->has_range) && (info->range.we_version_compiled > 10))
{
/* Get retry limit/lifetime */
if(iw_get_ext(skfd, ifname, SIOCGIWRETRY, &wrq) >= 0)
{
info->has_retry = 1;
memcpy(&(info->retry), &(wrq.u.retry), sizeof(iwparam));
}
}
/* Get RTS threshold */
if(iw_get_ext(skfd, ifname, SIOCGIWRTS, &wrq) >= 0)
{
info->has_rts = 1;
memcpy(&(info->rts), &(wrq.u.rts), sizeof(iwparam));
}
/* Get fragmentation threshold */
if(iw_get_ext(skfd, ifname, SIOCGIWFRAG, &wrq) >= 0)
{
info->has_frag = 1;
memcpy(&(info->frag), &(wrq.u.frag), sizeof(iwparam));
}
#endif /* WE_ESSENTIAL */
return(0);
}
/*------------------------------------------------------------------*/
/*
* Print on the screen in a neat fashion all the info we have collected
* on a device.
*/
static void
display_info(struct wireless_info * info,
char * ifname)
{
char buffer[256]; /* Temporary buffer */
/* One token is more of less 5 characters, 14 tokens per line */
int tokens = 3; /* For name */
/* Display device name and wireless name (name of the protocol used) */
printf("%-8.16s %s ", ifname, info->b.name);
/* Display ESSID (extended network), if any */
if(info->b.has_essid)
{
if(info->b.essid_on)
{
/* Escape the non-printable characters */
iw_essid_escape(buffer, info->b.essid, info->b.essid_len);
/* Does it have an ESSID index ? */
if((info->b.essid_on & IW_ENCODE_INDEX) > 1)
printf("ESSID:\"%s\" [%d] ", buffer,
(info->b.essid_on & IW_ENCODE_INDEX));
else
printf("ESSID:\"%s\" ", buffer);
}
else
printf("ESSID:off/any ");
}
#ifndef WE_ESSENTIAL
/* Display NickName (station name), if any */
if(info->has_nickname)
printf("Nickname:\"%s\"", info->nickname);
#endif /* WE_ESSENTIAL */
/* Formatting */
if(info->b.has_essid || info->has_nickname)
{
printf("\n ");
tokens = 0;
}
#ifndef WE_ESSENTIAL
/* Display Network ID */
if(info->b.has_nwid)
{
/* Note : should display proper number of digits according to info
* in range structure */
if(info->b.nwid.disabled)
printf("NWID:off/any ");
else
printf("NWID:%X ", info->b.nwid.value);
tokens +=2;
}
#endif /* WE_ESSENTIAL */
/* Display the current mode of operation */
if(info->b.has_mode)
{
printf("Mode:%s ", iw_operation_mode[info->b.mode]);
tokens +=3;
}
/* Display frequency / channel */
if(info->b.has_freq)
{
double freq = info->b.freq; /* Frequency/channel */
int channel = -1; /* Converted to channel */
/* Some drivers insist of returning channel instead of frequency.
* This fixes them up. Note that, driver should still return
* frequency, because other tools depend on it. */
if(info->has_range && (freq < KILO))
channel = iw_channel_to_freq((int) freq, &freq, &info->range);
/* Display */
iw_print_freq(buffer, sizeof(buffer), freq, -1, info->b.freq_flags);
printf("%s ", buffer);
tokens +=4;
}
/* Display the address of the current Access Point */
if(info->has_ap_addr)
{
/* A bit of clever formatting */
if(tokens > 8)
{
printf("\n ");
tokens = 0;
}
tokens +=6;
/* Oups ! No Access Point in Ad-Hoc mode */
if((info->b.has_mode) && (info->b.mode == IW_MODE_ADHOC))
printf("Cell:");
else
printf("Access Point:");
printf(" %s ", iw_sawap_ntop(&info->ap_addr, buffer));
}
/* Display the currently used/set bit-rate */
if(info->has_bitrate)
{
/* A bit of clever formatting */
if(tokens > 11)
{
printf("\n ");
tokens = 0;
}
tokens +=3;
/* Display it */
iw_print_bitrate(buffer, sizeof(buffer), info->bitrate.value);
printf("Bit Rate%c%s ", (info->bitrate.fixed ? '=' : ':'), buffer);
}
#ifndef WE_ESSENTIAL
/* Display the Transmit Power */
if(info->has_txpower)
{
/* A bit of clever formatting */
if(tokens > 11)
{
printf("\n ");
tokens = 0;
}
tokens +=3;
/* Display it */
iw_print_txpower(buffer, sizeof(buffer), &info->txpower);
printf("Tx-Power%c%s ", (info->txpower.fixed ? '=' : ':'), buffer);
}
/* Display sensitivity */
if(info->has_sens)
{
/* A bit of clever formatting */
if(tokens > 10)
{
printf("\n ");
tokens = 0;
}
tokens +=4;
/* Fixed ? */
printf("Sensitivity%c", info->sens.fixed ? '=' : ':');
if(info->has_range)
/* Display in dBm ? */
if(info->sens.value < 0)
printf("%d dBm ", info->sens.value);
else
printf("%d/%d ", info->sens.value, info->range.sensitivity);
else
printf("%d ", info->sens.value);
}
#endif /* WE_ESSENTIAL */
printf("\n ");
tokens = 0;
#ifndef WE_ESSENTIAL
/* Display retry limit/lifetime information */
if(info->has_retry)
{
printf("Retry");
/* Disabled ? */
if(info->retry.disabled)
printf(":off");
else
{
/* Let's check the value and its type */
if(info->retry.flags & IW_RETRY_TYPE)
{
iw_print_retry_value(buffer, sizeof(buffer),
info->retry.value, info->retry.flags,
info->range.we_version_compiled);
printf("%s", buffer);
}
/* Let's check if nothing (simply on) */
if(info->retry.flags == IW_RETRY_ON)
printf(":on");
}
printf(" ");
tokens += 5; /* Between 3 and 5, depend on flags */
}
/* Display the RTS threshold */
if(info->has_rts)
{
/* Disabled ? */
if(info->rts.disabled)
printf("RTS thr:off ");
else
{
/* Fixed ? */
printf("RTS thr%c%d B ",
info->rts.fixed ? '=' : ':',
info->rts.value);
}
tokens += 3;
}
/* Display the fragmentation threshold */
if(info->has_frag)
{
/* A bit of clever formatting */
if(tokens > 10)
{
printf("\n ");
tokens = 0;
}
tokens +=4;
/* Disabled ? */
if(info->frag.disabled)
printf("Fragment thr:off");
else
{
/* Fixed ? */
printf("Fragment thr%c%d B ",
info->frag.fixed ? '=' : ':',
info->frag.value);
}
}
/* Formating */
if(tokens > 0)
printf("\n ");
#endif /* WE_ESSENTIAL */
/* Display encryption information */
/* Note : we display only the "current" key, use iwlist to list all keys */
if(info->b.has_key)
{
printf("Encryption key:");
if((info->b.key_flags & IW_ENCODE_DISABLED) || (info->b.key_size == 0))
printf("off");
else
{
/* Display the key */
iw_print_key(buffer, sizeof(buffer),
info->b.key, info->b.key_size, info->b.key_flags);
printf("%s", buffer);
/* Other info... */
if((info->b.key_flags & IW_ENCODE_INDEX) > 1)
printf(" [%d]", info->b.key_flags & IW_ENCODE_INDEX);
if(info->b.key_flags & IW_ENCODE_RESTRICTED)
printf(" Security mode:restricted");
if(info->b.key_flags & IW_ENCODE_OPEN)
printf(" Security mode:open");
}
printf("\n ");
}
/* Display Power Management information */
/* Note : we display only one parameter, period or timeout. If a device
* (such as HiperLan) has both, the user need to use iwlist... */
if(info->has_power) /* I hope the device has power ;-) */
{
printf("Power Management");
/* Disabled ? */
if(info->power.disabled)
printf(":off");
else
{
/* Let's check the value and its type */
if(info->power.flags & IW_POWER_TYPE)
{
iw_print_pm_value(buffer, sizeof(buffer),
info->power.value, info->power.flags,
info->range.we_version_compiled);
printf("%s ", buffer);
}
/* Let's check the mode */
iw_print_pm_mode(buffer, sizeof(buffer), info->power.flags);
printf("%s", buffer);
/* Let's check if nothing (simply on) */
if(info->power.flags == IW_POWER_ON)
printf(":on");
}
printf("\n ");
}
/* Display statistics */
if(info->has_stats)
{
iw_print_stats(buffer, sizeof(buffer),
&info->stats.qual, &info->range, info->has_range);
printf("Link %s\n", buffer);
if(info->range.we_version_compiled > 11)
printf(" Rx invalid nwid:%d Rx invalid crypt:%d Rx invalid frag:%d\n Tx excessive retries:%d Invalid misc:%d Missed beacon:%d\n",
info->stats.discard.nwid,
info->stats.discard.code,
info->stats.discard.fragment,
info->stats.discard.retries,
info->stats.discard.misc,
info->stats.miss.beacon);
else
printf(" Rx invalid nwid:%d invalid crypt:%d invalid misc:%d\n",
info->stats.discard.nwid,
info->stats.discard.code,
info->stats.discard.misc);
}
printf("\n");
}
/*------------------------------------------------------------------*/
/*
* Print on the screen in a neat fashion all the info we have collected
* on a device.
*/
static int
print_info(int skfd,
char * ifname,
char * args[],
int count)
{
struct wireless_info info;
int rc;
/* Avoid "Unused parameter" warning */
args = args; count = count;
rc = get_info(skfd, ifname, &info);
switch(rc)
{
case 0: /* Success */
/* Display it ! */
display_info(&info, ifname);
break;
case -ENOTSUP:
fprintf(stderr, "%-8.16s no wireless extensions.\n\n",
ifname);
break;
default:
fprintf(stderr, "%-8.16s %s\n\n", ifname, strerror(-rc));
}
return(rc);
}
/****************** COMMAND LINE MODIFIERS PARSING ******************/
/*
* Factor out the parsing of command line modifiers.
*/
/*------------------------------------------------------------------*/
/*
* Map command line modifiers to the proper flags...
*/
typedef struct iwconfig_modifier {
const char * cmd; /* Command line shorthand */
__u16 flag; /* Flags to add */
__u16 exclude; /* Modifiers to exclude */
} iwconfig_modifier;
/*------------------------------------------------------------------*/
/*
* Modifiers for Power
*/
static const struct iwconfig_modifier iwmod_power[] = {
{ "min", IW_POWER_MIN, IW_POWER_MAX },
{ "max", IW_POWER_MAX, IW_POWER_MIN },
{ "period", IW_POWER_PERIOD, IW_POWER_TIMEOUT | IW_POWER_SAVING },
{ "timeout", IW_POWER_TIMEOUT, IW_POWER_PERIOD | IW_POWER_SAVING },
{ "saving", IW_POWER_SAVING, IW_POWER_TIMEOUT | IW_POWER_PERIOD },
};
#define IWMOD_POWER_NUM (sizeof(iwmod_power)/sizeof(iwmod_power[0]))
/*------------------------------------------------------------------*/
/*
* Modifiers for Retry
*/
#ifndef WE_ESSENTIAL
static const struct iwconfig_modifier iwmod_retry[] = {
{ "min", IW_RETRY_MIN, IW_RETRY_MAX },
{ "max", IW_RETRY_MAX, IW_RETRY_MIN },
{ "short", IW_RETRY_SHORT, IW_RETRY_LONG },
{ "long", IW_RETRY_LONG, IW_RETRY_SHORT },
{ "limit", IW_RETRY_LIMIT, IW_RETRY_LIFETIME },
{ "lifetime", IW_RETRY_LIFETIME, IW_RETRY_LIMIT },
};
#define IWMOD_RETRY_NUM (sizeof(iwmod_retry)/sizeof(iwmod_retry[0]))
#endif /* WE_ESSENTIAL */
/*------------------------------------------------------------------*/
/*
* Parse command line modifiers.
* Return error or number arg parsed.
* Modifiers must be at the beggining of command line.
*/
static int
parse_modifiers(char * args[], /* Command line args */
int count, /* Args count */
__u16 * pout, /* Flags to write */
const struct iwconfig_modifier modifier[],
int modnum)
{
int i = 0;
int k = 0;
__u16 result = 0; /* Default : no flag set */
/* Get all modifiers and value types on the command line */
do
{
for(k = 0; k < modnum; k++)
{
/* Check if matches */
if(!strcasecmp(args[i], modifier[k].cmd))
{
/* Check for conflicting flags */
if(result & modifier[k].exclude)
{
errarg = i;
return(IWERR_ARG_CONFLICT);
}
/* Just add it */
result |= modifier[k].flag;
++i;
break;
}
}
}
/* For as long as current arg matched and not out of args */
while((i < count) && (k < modnum));
/* Check there remains one arg for value */
if(i >= count)
return(IWERR_ARG_NUM);
/* Return result */
*pout = result;
return(i);
}
/*********************** SETTING SUB-ROUTINES ***********************/
/*
* The following functions are use to set some wireless parameters and
* are called by the set dispatcher set_info().
* They take as arguments the remaining of the command line, with
* arguments processed already removed.
* An error is indicated by a negative return value.
* 0 and positive return values indicate the number of args consumed.
*/
/*------------------------------------------------------------------*/
/*
* Set ESSID
*/
static int
set_essid_info(int skfd,
char * ifname,
char * args[], /* Command line args */
int count) /* Args count */
{
struct iwreq wrq;
int i = 1;
char essid[4*IW_ESSID_MAX_SIZE + 1];
int essid_len;
int essid_index;
int we_kernel_version;
if((!strcasecmp(args[0], "off")) ||
(!strcasecmp(args[0], "any")))
{
wrq.u.essid.flags = 0;
essid[0] = '\0';
essid_len = 0;
}
else
if(!strcasecmp(args[0], "on"))
{
/* Get old essid */
memset(essid, '\0', sizeof(essid));
wrq.u.essid.pointer = (caddr_t) essid;
wrq.u.essid.length = IW_ESSID_MAX_SIZE + 2;
wrq.u.essid.flags = 0;
if(iw_get_ext(skfd, ifname, SIOCGIWESSID, &wrq) < 0)
return(IWERR_GET_EXT);
wrq.u.essid.flags = 1;
essid_len = wrq.u.essid.length;
}
else
{
i = 0;
/* '-' or '--' allow to escape the ESSID string, allowing
* to set it to the string "any" or "off".
* This is a big ugly, but it will do for now */
if((!strcmp(args[0], "-")) || (!strcmp(args[0], "--")))
{
if(++i >= count)
return(IWERR_ARG_NUM);
essid_len = strlen(args[i]);
}
/* First size check : check if ot fits in our internal buffer.
* We do a two pass size check because of unescaping */
if(strlen(args[i]) > (4*IW_ESSID_MAX_SIZE))
{
errmax = IW_ESSID_MAX_SIZE;
return(IWERR_ARG_SIZE);
}
/* Unescape the ESSID to allow the user to enter weird chars */
essid_len = iw_essid_unescape(essid, args[i]);
/* Check the size to see if it fits the API. */
if(essid_len > IW_ESSID_MAX_SIZE)
{
errmax = IW_ESSID_MAX_SIZE;
return(IWERR_ARG_SIZE);
}
wrq.u.essid.flags = 1;
i++;
/* Check for ESSID index */
if((i < count) &&
(sscanf(args[i], "[%i]", &essid_index) == 1) &&
(essid_index > 0) && (essid_index < IW_ENCODE_INDEX))
{
wrq.u.essid.flags = essid_index;
++i;
}
}
/* Get version from kernel, device may not have range... */
we_kernel_version = iw_get_kernel_we_version();
/* Finally set the ESSID value */
wrq.u.essid.pointer = (caddr_t) essid;
wrq.u.essid.length = essid_len;
if(we_kernel_version < 21)
wrq.u.essid.length++;
if(iw_set_ext(skfd, ifname, SIOCSIWESSID, &wrq) < 0)
return(IWERR_SET_EXT);
/* Var args */
return(i);
}
/*------------------------------------------------------------------*/
/*
* Set Mode
*/
static int
set_mode_info(int skfd,
char * ifname,
char * args[], /* Command line args */
int count) /* Args count */
{
struct iwreq wrq;
unsigned int k; /* Must be unsigned */
/* Avoid "Unused parameter" warning */
count = count;
/* Check if it is a uint, otherwise get is as a string */
if(sscanf(args[0], "%i", &k) != 1)
{
k = 0;
while((k < IW_NUM_OPER_MODE) &&
strncasecmp(args[0], iw_operation_mode[k], 3))
k++;
}
if(k >= IW_NUM_OPER_MODE)
{
errarg = 0;
return(IWERR_ARG_TYPE);
}
wrq.u.mode = k;
if(iw_set_ext(skfd, ifname, SIOCSIWMODE, &wrq) < 0)
return(IWERR_SET_EXT);
/* 1 arg */
return(1);
}
/*------------------------------------------------------------------*/
/*
* Set frequency/channel
*/
static int
set_freq_info(int skfd,
char * ifname,
char * args[], /* Command line args */
int count) /* Args count */
{
struct iwreq wrq;
int i = 1;
if(!strcasecmp(args[0], "auto"))
{
wrq.u.freq.m = -1;
wrq.u.freq.e = 0;
wrq.u.freq.flags = 0;
}
else
{
if(!strcasecmp(args[0], "fixed"))
{
/* Get old frequency */
if(iw_get_ext(skfd, ifname, SIOCGIWFREQ, &wrq) < 0)
return(IWERR_GET_EXT);
wrq.u.freq.flags = IW_FREQ_FIXED;
}
else /* Should be a numeric value */
{
double freq;
char * unit;
freq = strtod(args[0], &unit);
if(unit == args[0])
{
errarg = 0;
return(IWERR_ARG_TYPE);
}
if(unit != NULL)
{
if(unit[0] == 'G') freq *= GIGA;
if(unit[0] == 'M') freq *= MEGA;
if(unit[0] == 'k') freq *= KILO;
}
iw_float2freq(freq, &(wrq.u.freq));
wrq.u.freq.flags = IW_FREQ_FIXED;
/* Check for an additional argument */
if((i < count) && (!strcasecmp(args[i], "auto")))
{
wrq.u.freq.flags = 0;
++i;
}
if((i < count) && (!strcasecmp(args[i], "fixed")))
{
wrq.u.freq.flags = IW_FREQ_FIXED;
++i;
}
}
}
if(iw_set_ext(skfd, ifname, SIOCSIWFREQ, &wrq) < 0)
return(IWERR_SET_EXT);
/* Var args */
return(i);
}
/*------------------------------------------------------------------*/
/*
* Set Bit Rate
*/
static int
set_bitrate_info(int skfd,
char * ifname,
char * args[], /* Command line args */
int count) /* Args count */
{
struct iwreq wrq;
int i = 1;
wrq.u.bitrate.flags = 0;
if(!strcasecmp(args[0], "auto"))
{
wrq.u.bitrate.value = -1;
wrq.u.bitrate.fixed = 0;
}
else
{
if(!strcasecmp(args[0], "fixed"))
{
/* Get old bitrate */
if(iw_get_ext(skfd, ifname, SIOCGIWRATE, &wrq) < 0)
return(IWERR_GET_EXT);
wrq.u.bitrate.fixed = 1;
}
else /* Should be a numeric value */
{
double brate;
char * unit;
brate = strtod(args[0], &unit);
if(unit == args[0])
{
errarg = 0;
return(IWERR_ARG_TYPE);
}
if(unit != NULL)
{
if(unit[0] == 'G') brate *= GIGA;
if(unit[0] == 'M') brate *= MEGA;
if(unit[0] == 'k') brate *= KILO;
}
wrq.u.bitrate.value = (long) brate;
wrq.u.bitrate.fixed = 1;
/* Check for an additional argument */
if((i < count) && (!strcasecmp(args[i], "auto")))
{
wrq.u.bitrate.fixed = 0;
++i;
}
if((i < count) && (!strcasecmp(args[i], "fixed")))
{
wrq.u.bitrate.fixed = 1;
++i;
}
if((i < count) && (!strcasecmp(args[i], "unicast")))
{
wrq.u.bitrate.flags |= IW_BITRATE_UNICAST;
++i;
}
if((i < count) && (!strcasecmp(args[i], "broadcast")))
{
wrq.u.bitrate.flags |= IW_BITRATE_BROADCAST;
++i;
}
}
}
if(iw_set_ext(skfd, ifname, SIOCSIWRATE, &wrq) < 0)
return(IWERR_SET_EXT);
/* Var args */
return(i);
}
/*------------------------------------------------------------------*/
/*
* Set encryption
*/
static int
set_enc_info(int skfd,
char * ifname,
char * args[], /* Command line args */
int count) /* Args count */
{
struct iwreq wrq;
int i = 1;
unsigned char key[IW_ENCODING_TOKEN_MAX];
if(!strcasecmp(args[0], "on"))
{
/* Get old encryption information */
wrq.u.data.pointer = (caddr_t) key;
wrq.u.data.length = IW_ENCODING_TOKEN_MAX;
wrq.u.data.flags = 0;
if(iw_get_ext(skfd, ifname, SIOCGIWENCODE, &wrq) < 0)
return(IWERR_GET_EXT);
wrq.u.data.flags &= ~IW_ENCODE_DISABLED; /* Enable */
}
else
{
int gotone = 0;
int oldone;
int keylen;
int temp;
wrq.u.data.pointer = (caddr_t) NULL;
wrq.u.data.flags = 0;
wrq.u.data.length = 0;
i = 0;
/* Allow arguments in any order (it's safe) */
do
{
oldone = gotone;
/* -- Check for the key -- */
if(i < count)
{
keylen = iw_in_key_full(skfd, ifname,
args[i], key, &wrq.u.data.flags);
if(keylen > 0)
{
wrq.u.data.length = keylen;
wrq.u.data.pointer = (caddr_t) key;
++i;
gotone++;
}
}
/* -- Check for token index -- */
if((i < count) &&
(sscanf(args[i], "[%i]", &temp) == 1) &&
(temp > 0) && (temp < IW_ENCODE_INDEX))
{
wrq.u.encoding.flags |= temp;
++i;
gotone++;
}
/* -- Check the various flags -- */
if((i < count) && (!strcasecmp(args[i], "off")))
{
wrq.u.data.flags |= IW_ENCODE_DISABLED;
++i;
gotone++;
}
if((i < count) && (!strcasecmp(args[i], "open")))
{
wrq.u.data.flags |= IW_ENCODE_OPEN;
++i;
gotone++;
}
if((i < count) && (!strncasecmp(args[i], "restricted", 5)))
{
wrq.u.data.flags |= IW_ENCODE_RESTRICTED;
++i;
gotone++;
}
if((i < count) && (!strncasecmp(args[i], "temporary", 4)))
{
wrq.u.data.flags |= IW_ENCODE_TEMP;
++i;
gotone++;
}
}
while(gotone != oldone);
/* Pointer is absent in new API */
if(wrq.u.data.pointer == NULL)
wrq.u.data.flags |= IW_ENCODE_NOKEY;
/* Check if we have any invalid argument */
if(!gotone)
{
errarg = 0;
return(IWERR_ARG_TYPE);
}