iwconfig.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954
  1. /*
  2. * Wireless Tools
  3. *
  4. * Jean II - HPLB 97->99 - HPL 99->07
  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-2007 Jean Tourrilhes <jt@hpl.hp.com>
  12. */
  13. #include "iwlib.h" /* Header */
  14. /**************************** CONSTANTS ****************************/
  15. /*
  16. * Error codes defined for setting args
  17. */
  18. #define IWERR_ARG_NUM -2
  19. #define IWERR_ARG_TYPE -3
  20. #define IWERR_ARG_SIZE -4
  21. #define IWERR_ARG_CONFLICT -5
  22. #define IWERR_SET_EXT -6
  23. #define IWERR_GET_EXT -7
  24. /**************************** VARIABLES ****************************/
  25. /*
  26. * Ugly, but deal with errors in set_info() efficiently...
  27. */
  28. static int errarg;
  29. static int errmax;
  30. /************************* DISPLAY ROUTINES **************************/
  31. /*------------------------------------------------------------------*/
  32. /*
  33. * Get wireless informations & config from the device driver
  34. * We will call all the classical wireless ioctl on the driver through
  35. * the socket to know what is supported and to get the settings...
  36. */
  37. static int
  38. get_info(int skfd,
  39. char * ifname,
  40. struct wireless_info * info)
  41. {
  42. struct iwreq wrq;
  43. memset((char *) info, 0, sizeof(struct wireless_info));
  44. /* Get basic information */
  45. if(iw_get_basic_config(skfd, ifname, &(info->b)) < 0)
  46. {
  47. /* If no wireless name : no wireless extensions */
  48. /* But let's check if the interface exists at all */
  49. struct ifreq ifr;
  50. strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
  51. if(ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
  52. return(-ENODEV);
  53. else
  54. return(-ENOTSUP);
  55. }
  56. /* Get ranges */
  57. if(iw_get_range_info(skfd, ifname, &(info->range)) >= 0)
  58. info->has_range = 1;
  59. /* Get AP address */
  60. if(iw_get_ext(skfd, ifname, SIOCGIWAP, &wrq) >= 0)
  61. {
  62. info->has_ap_addr = 1;
  63. memcpy(&(info->ap_addr), &(wrq.u.ap_addr), sizeof (sockaddr));
  64. }
  65. /* Get bit rate */
  66. if(iw_get_ext(skfd, ifname, SIOCGIWRATE, &wrq) >= 0)
  67. {
  68. info->has_bitrate = 1;
  69. memcpy(&(info->bitrate), &(wrq.u.bitrate), sizeof(iwparam));
  70. }
  71. /* Get Power Management settings */
  72. wrq.u.power.flags = 0;
  73. if(iw_get_ext(skfd, ifname, SIOCGIWPOWER, &wrq) >= 0)
  74. {
  75. info->has_power = 1;
  76. memcpy(&(info->power), &(wrq.u.power), sizeof(iwparam));
  77. }
  78. /* Get stats */
  79. if(iw_get_stats(skfd, ifname, &(info->stats),
  80. &info->range, info->has_range) >= 0)
  81. {
  82. info->has_stats = 1;
  83. }
  84. #ifndef WE_ESSENTIAL
  85. /* Get NickName */
  86. wrq.u.essid.pointer = (caddr_t) info->nickname;
  87. wrq.u.essid.length = IW_ESSID_MAX_SIZE + 1;
  88. wrq.u.essid.flags = 0;
  89. if(iw_get_ext(skfd, ifname, SIOCGIWNICKN, &wrq) >= 0)
  90. if(wrq.u.data.length > 1)
  91. info->has_nickname = 1;
  92. if((info->has_range) && (info->range.we_version_compiled > 9))
  93. {
  94. /* Get Transmit Power */
  95. if(iw_get_ext(skfd, ifname, SIOCGIWTXPOW, &wrq) >= 0)
  96. {
  97. info->has_txpower = 1;
  98. memcpy(&(info->txpower), &(wrq.u.txpower), sizeof(iwparam));
  99. }
  100. }
  101. /* Get sensitivity */
  102. if(iw_get_ext(skfd, ifname, SIOCGIWSENS, &wrq) >= 0)
  103. {
  104. info->has_sens = 1;
  105. memcpy(&(info->sens), &(wrq.u.sens), sizeof(iwparam));
  106. }
  107. if((info->has_range) && (info->range.we_version_compiled > 10))
  108. {
  109. /* Get retry limit/lifetime */
  110. if(iw_get_ext(skfd, ifname, SIOCGIWRETRY, &wrq) >= 0)
  111. {
  112. info->has_retry = 1;
  113. memcpy(&(info->retry), &(wrq.u.retry), sizeof(iwparam));
  114. }
  115. }
  116. /* Get RTS threshold */
  117. if(iw_get_ext(skfd, ifname, SIOCGIWRTS, &wrq) >= 0)
  118. {
  119. info->has_rts = 1;
  120. memcpy(&(info->rts), &(wrq.u.rts), sizeof(iwparam));
  121. }
  122. /* Get fragmentation threshold */
  123. if(iw_get_ext(skfd, ifname, SIOCGIWFRAG, &wrq) >= 0)
  124. {
  125. info->has_frag = 1;
  126. memcpy(&(info->frag), &(wrq.u.frag), sizeof(iwparam));
  127. }
  128. #endif /* WE_ESSENTIAL */
  129. return(0);
  130. }
  131. /*------------------------------------------------------------------*/
  132. /*
  133. * Print on the screen in a neat fashion all the info we have collected
  134. * on a device.
  135. */
  136. static void
  137. display_info(struct wireless_info * info,
  138. char * ifname)
  139. {
  140. char buffer[128]; /* Temporary buffer */
  141. /* One token is more of less 5 characters, 14 tokens per line */
  142. int tokens = 3; /* For name */
  143. /* Display device name and wireless name (name of the protocol used) */
  144. printf("%-8.16s %s ", ifname, info->b.name);
  145. /* Display ESSID (extended network), if any */
  146. if(info->b.has_essid)
  147. {
  148. if(info->b.essid_on)
  149. {
  150. /* Does it have an ESSID index ? */
  151. if((info->b.essid_on & IW_ENCODE_INDEX) > 1)
  152. printf("ESSID:\"%s\" [%d] ", info->b.essid,
  153. (info->b.essid_on & IW_ENCODE_INDEX));
  154. else
  155. printf("ESSID:\"%s\" ", info->b.essid);
  156. }
  157. else
  158. printf("ESSID:off/any ");
  159. }
  160. #ifndef WE_ESSENTIAL
  161. /* Display NickName (station name), if any */
  162. if(info->has_nickname)
  163. printf("Nickname:\"%s\"", info->nickname);
  164. #endif /* WE_ESSENTIAL */
  165. /* Formatting */
  166. if(info->b.has_essid || info->has_nickname)
  167. {
  168. printf("\n ");
  169. tokens = 0;
  170. }
  171. #ifndef WE_ESSENTIAL
  172. /* Display Network ID */
  173. if(info->b.has_nwid)
  174. {
  175. /* Note : should display proper number of digits according to info
  176. * in range structure */
  177. if(info->b.nwid.disabled)
  178. printf("NWID:off/any ");
  179. else
  180. printf("NWID:%X ", info->b.nwid.value);
  181. tokens +=2;
  182. }
  183. #endif /* WE_ESSENTIAL */
  184. /* Display the current mode of operation */
  185. if(info->b.has_mode)
  186. {
  187. printf("Mode:%s ", iw_operation_mode[info->b.mode]);
  188. tokens +=3;
  189. }
  190. /* Display frequency / channel */
  191. if(info->b.has_freq)
  192. {
  193. double freq = info->b.freq; /* Frequency/channel */
  194. int channel = -1; /* Converted to channel */
  195. /* Some drivers insist of returning channel instead of frequency.
  196. * This fixes them up. Note that, driver should still return
  197. * frequency, because other tools depend on it. */
  198. if(info->has_range && (freq < KILO))
  199. channel = iw_channel_to_freq((int) freq, &freq, &info->range);
  200. /* Display */
  201. iw_print_freq(buffer, sizeof(buffer), freq, -1, info->b.freq_flags);
  202. printf("%s ", buffer);
  203. tokens +=4;
  204. }
  205. /* Display the address of the current Access Point */
  206. if(info->has_ap_addr)
  207. {
  208. /* A bit of clever formatting */
  209. if(tokens > 8)
  210. {
  211. printf("\n ");
  212. tokens = 0;
  213. }
  214. tokens +=6;
  215. /* Oups ! No Access Point in Ad-Hoc mode */
  216. if((info->b.has_mode) && (info->b.mode == IW_MODE_ADHOC))
  217. printf("Cell:");
  218. else
  219. printf("Access Point:");
  220. printf(" %s ", iw_sawap_ntop(&info->ap_addr, buffer));
  221. }
  222. /* Display the currently used/set bit-rate */
  223. if(info->has_bitrate)
  224. {
  225. /* A bit of clever formatting */
  226. if(tokens > 11)
  227. {
  228. printf("\n ");
  229. tokens = 0;
  230. }
  231. tokens +=3;
  232. /* Display it */
  233. iw_print_bitrate(buffer, sizeof(buffer), info->bitrate.value);
  234. printf("Bit Rate%c%s ", (info->bitrate.fixed ? '=' : ':'), buffer);
  235. }
  236. #ifndef WE_ESSENTIAL
  237. /* Display the Transmit Power */
  238. if(info->has_txpower)
  239. {
  240. /* A bit of clever formatting */
  241. if(tokens > 11)
  242. {
  243. printf("\n ");
  244. tokens = 0;
  245. }
  246. tokens +=3;
  247. /* Display it */
  248. iw_print_txpower(buffer, sizeof(buffer), &info->txpower);
  249. printf("Tx-Power%c%s ", (info->txpower.fixed ? '=' : ':'), buffer);
  250. }
  251. /* Display sensitivity */
  252. if(info->has_sens)
  253. {
  254. /* A bit of clever formatting */
  255. if(tokens > 10)
  256. {
  257. printf("\n ");
  258. tokens = 0;
  259. }
  260. tokens +=4;
  261. /* Fixed ? */
  262. printf("Sensitivity%c", info->sens.fixed ? '=' : ':');
  263. if(info->has_range)
  264. /* Display in dBm ? */
  265. if(info->sens.value < 0)
  266. printf("%d dBm ", info->sens.value);
  267. else
  268. printf("%d/%d ", info->sens.value, info->range.sensitivity);
  269. else
  270. printf("%d ", info->sens.value);
  271. }
  272. #endif /* WE_ESSENTIAL */
  273. printf("\n ");
  274. tokens = 0;
  275. #ifndef WE_ESSENTIAL
  276. /* Display retry limit/lifetime information */
  277. if(info->has_retry)
  278. {
  279. printf("Retry");
  280. /* Disabled ? */
  281. if(info->retry.disabled)
  282. printf(":off");
  283. else
  284. {
  285. /* Let's check the value and its type */
  286. if(info->retry.flags & IW_RETRY_TYPE)
  287. {
  288. iw_print_retry_value(buffer, sizeof(buffer),
  289. info->retry.value, info->retry.flags,
  290. info->range.we_version_compiled);
  291. printf("%s", buffer);
  292. }
  293. /* Let's check if nothing (simply on) */
  294. if(info->retry.flags == IW_RETRY_ON)
  295. printf(":on");
  296. }
  297. printf(" ");
  298. tokens += 5; /* Between 3 and 5, depend on flags */
  299. }
  300. /* Display the RTS threshold */
  301. if(info->has_rts)
  302. {
  303. /* Disabled ? */
  304. if(info->rts.disabled)
  305. printf("RTS thr:off ");
  306. else
  307. {
  308. /* Fixed ? */
  309. printf("RTS thr%c%d B ",
  310. info->rts.fixed ? '=' : ':',
  311. info->rts.value);
  312. }
  313. tokens += 3;
  314. }
  315. /* Display the fragmentation threshold */
  316. if(info->has_frag)
  317. {
  318. /* A bit of clever formatting */
  319. if(tokens > 10)
  320. {
  321. printf("\n ");
  322. tokens = 0;
  323. }
  324. tokens +=4;
  325. /* Disabled ? */
  326. if(info->frag.disabled)
  327. printf("Fragment thr:off");
  328. else
  329. {
  330. /* Fixed ? */
  331. printf("Fragment thr%c%d B ",
  332. info->frag.fixed ? '=' : ':',
  333. info->frag.value);
  334. }
  335. }
  336. /* Formating */
  337. if(tokens > 0)
  338. printf("\n ");
  339. #endif /* WE_ESSENTIAL */
  340. /* Display encryption information */
  341. /* Note : we display only the "current" key, use iwlist to list all keys */
  342. if(info->b.has_key)
  343. {
  344. printf("Encryption key:");
  345. if((info->b.key_flags & IW_ENCODE_DISABLED) || (info->b.key_size == 0))
  346. printf("off");
  347. else
  348. {
  349. /* Display the key */
  350. iw_print_key(buffer, sizeof(buffer),
  351. info->b.key, info->b.key_size, info->b.key_flags);
  352. printf("%s", buffer);
  353. /* Other info... */
  354. if((info->b.key_flags & IW_ENCODE_INDEX) > 1)
  355. printf(" [%d]", info->b.key_flags & IW_ENCODE_INDEX);
  356. if(info->b.key_flags & IW_ENCODE_RESTRICTED)
  357. printf(" Security mode:restricted");
  358. if(info->b.key_flags & IW_ENCODE_OPEN)
  359. printf(" Security mode:open");
  360. }
  361. printf("\n ");
  362. }
  363. /* Display Power Management information */
  364. /* Note : we display only one parameter, period or timeout. If a device
  365. * (such as HiperLan) has both, the user need to use iwlist... */
  366. if(info->has_power) /* I hope the device has power ;-) */
  367. {
  368. printf("Power Management");
  369. /* Disabled ? */
  370. if(info->power.disabled)
  371. printf(":off");
  372. else
  373. {
  374. /* Let's check the value and its type */
  375. if(info->power.flags & IW_POWER_TYPE)
  376. {
  377. iw_print_pm_value(buffer, sizeof(buffer),
  378. info->power.value, info->power.flags,
  379. info->range.we_version_compiled);
  380. printf("%s ", buffer);
  381. }
  382. /* Let's check the mode */
  383. iw_print_pm_mode(buffer, sizeof(buffer), info->power.flags);
  384. printf("%s", buffer);
  385. /* Let's check if nothing (simply on) */
  386. if(info->power.flags == IW_POWER_ON)
  387. printf(":on");
  388. }
  389. printf("\n ");
  390. }
  391. /* Display statistics */
  392. if(info->has_stats)
  393. {
  394. iw_print_stats(buffer, sizeof(buffer),
  395. &info->stats.qual, &info->range, info->has_range);
  396. printf("Link %s\n", buffer);
  397. if(info->range.we_version_compiled > 11)
  398. 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",
  399. info->stats.discard.nwid,
  400. info->stats.discard.code,
  401. info->stats.discard.fragment,
  402. info->stats.discard.retries,
  403. info->stats.discard.misc,
  404. info->stats.miss.beacon);
  405. else
  406. printf(" Rx invalid nwid:%d invalid crypt:%d invalid misc:%d\n",
  407. info->stats.discard.nwid,
  408. info->stats.discard.code,
  409. info->stats.discard.misc);
  410. }
  411. printf("\n");
  412. }
  413. /*------------------------------------------------------------------*/
  414. /*
  415. * Print on the screen in a neat fashion all the info we have collected
  416. * on a device.
  417. */
  418. static int
  419. print_info(int skfd,
  420. char * ifname,
  421. char * args[],
  422. int count)
  423. {
  424. struct wireless_info info;
  425. int rc;
  426. /* Avoid "Unused parameter" warning */
  427. args = args; count = count;
  428. rc = get_info(skfd, ifname, &info);
  429. switch(rc)
  430. {
  431. case 0: /* Success */
  432. /* Display it ! */
  433. display_info(&info, ifname);
  434. break;
  435. case -ENOTSUP:
  436. fprintf(stderr, "%-8.16s no wireless extensions.\n\n",
  437. ifname);
  438. break;
  439. default:
  440. fprintf(stderr, "%-8.16s %s\n\n", ifname, strerror(-rc));
  441. }
  442. return(rc);
  443. }
  444. /****************** COMMAND LINE MODIFIERS PARSING ******************/
  445. /*
  446. * Factor out the parsing of command line modifiers.
  447. */
  448. /*------------------------------------------------------------------*/
  449. /*
  450. * Map command line modifiers to the proper flags...
  451. */
  452. typedef struct iwconfig_modifier {
  453. const char * cmd; /* Command line shorthand */
  454. __u16 flag; /* Flags to add */
  455. __u16 exclude; /* Modifiers to exclude */
  456. } iwconfig_modifier;
  457. /*------------------------------------------------------------------*/
  458. /*
  459. * Modifiers for Power
  460. */
  461. static const struct iwconfig_modifier iwmod_power[] = {
  462. { "min", IW_POWER_MIN, IW_POWER_MAX },
  463. { "max", IW_POWER_MAX, IW_POWER_MIN },
  464. { "period", IW_POWER_PERIOD, IW_POWER_TIMEOUT | IW_POWER_SAVING },
  465. { "timeout", IW_POWER_TIMEOUT, IW_POWER_PERIOD | IW_POWER_SAVING },
  466. { "saving", IW_POWER_SAVING, IW_POWER_TIMEOUT | IW_POWER_PERIOD },
  467. };
  468. #define IWMOD_POWER_NUM (sizeof(iwmod_power)/sizeof(iwmod_power[0]))
  469. /*------------------------------------------------------------------*/
  470. /*
  471. * Modifiers for Retry
  472. */
  473. #ifndef WE_ESSENTIAL
  474. static const struct iwconfig_modifier iwmod_retry[] = {
  475. { "min", IW_RETRY_MIN, IW_RETRY_MAX },
  476. { "max", IW_RETRY_MAX, IW_RETRY_MIN },
  477. { "short", IW_RETRY_SHORT, IW_RETRY_LONG },
  478. { "long", IW_RETRY_LONG, IW_RETRY_SHORT },
  479. { "limit", IW_RETRY_LIMIT, IW_RETRY_LIFETIME },
  480. { "lifetime", IW_RETRY_LIFETIME, IW_RETRY_LIMIT },
  481. };
  482. #define IWMOD_RETRY_NUM (sizeof(iwmod_retry)/sizeof(iwmod_retry[0]))
  483. #endif /* WE_ESSENTIAL */
  484. /*------------------------------------------------------------------*/
  485. /*
  486. * Parse command line modifiers.
  487. * Return error or number arg parsed.
  488. * Modifiers must be at the beggining of command line.
  489. */
  490. static int
  491. parse_modifiers(char * args[], /* Command line args */
  492. int count, /* Args count */
  493. __u16 * pout, /* Flags to write */
  494. const struct iwconfig_modifier modifier[],
  495. int modnum)
  496. {
  497. int i = 0;
  498. int k = 0;
  499. __u16 result = 0; /* Default : no flag set */
  500. /* Get all modifiers and value types on the command line */
  501. do
  502. {
  503. for(k = 0; k < modnum; k++)
  504. {
  505. /* Check if matches */
  506. if(!strcasecmp(args[i], modifier[k].cmd))
  507. {
  508. /* Check for conflicting flags */
  509. if(result & modifier[k].exclude)
  510. {
  511. errarg = i;
  512. return(IWERR_ARG_CONFLICT);
  513. }
  514. /* Just add it */
  515. result |= modifier[k].flag;
  516. ++i;
  517. break;
  518. }
  519. }
  520. }
  521. /* For as long as current arg matched and not out of args */
  522. while((i < count) && (k < modnum));
  523. /* Check there remains one arg for value */
  524. if(i >= count)
  525. return(IWERR_ARG_NUM);
  526. /* Return result */
  527. *pout = result;
  528. return(i);
  529. }
  530. /*********************** SETTING SUB-ROUTINES ***********************/
  531. /*
  532. * The following functions are use to set some wireless parameters and
  533. * are called by the set dispatcher set_info().
  534. * They take as arguments the remaining of the command line, with
  535. * arguments processed already removed.
  536. * An error is indicated by a negative return value.
  537. * 0 and positive return values indicate the number of args consumed.
  538. */
  539. /*------------------------------------------------------------------*/
  540. /*
  541. * Set ESSID
  542. */
  543. static int
  544. set_essid_info(int skfd,
  545. char * ifname,
  546. char * args[], /* Command line args */
  547. int count) /* Args count */
  548. {
  549. struct iwreq wrq;
  550. int i = 1;
  551. char essid[IW_ESSID_MAX_SIZE + 1];
  552. int we_kernel_version;
  553. if((!strcasecmp(args[0], "off")) ||
  554. (!strcasecmp(args[0], "any")))
  555. {
  556. wrq.u.essid.flags = 0;
  557. essid[0] = '\0';
  558. }
  559. else
  560. if(!strcasecmp(args[0], "on"))
  561. {
  562. /* Get old essid */
  563. memset(essid, '\0', sizeof(essid));
  564. wrq.u.essid.pointer = (caddr_t) essid;
  565. wrq.u.essid.length = IW_ESSID_MAX_SIZE + 1;
  566. wrq.u.essid.flags = 0;
  567. if(iw_get_ext(skfd, ifname, SIOCGIWESSID, &wrq) < 0)
  568. return(IWERR_GET_EXT);
  569. wrq.u.essid.flags = 1;
  570. }
  571. else
  572. {
  573. i = 0;
  574. /* '-' or '--' allow to escape the ESSID string, allowing
  575. * to set it to the string "any" or "off".
  576. * This is a big ugly, but it will do for now */
  577. if((!strcmp(args[0], "-")) || (!strcmp(args[0], "--")))
  578. {
  579. if(++i >= count)
  580. return(IWERR_ARG_NUM);
  581. }
  582. /* Check the size of what the user passed us to avoid
  583. * buffer overflows */
  584. if(strlen(args[i]) > IW_ESSID_MAX_SIZE)
  585. {
  586. errmax = IW_ESSID_MAX_SIZE;
  587. return(IWERR_ARG_SIZE);
  588. }
  589. else
  590. {
  591. int temp;
  592. wrq.u.essid.flags = 1;
  593. strcpy(essid, args[i]); /* Size checked, all clear */
  594. i++;
  595. /* Check for ESSID index */
  596. if((i < count) &&
  597. (sscanf(args[i], "[%i]", &temp) == 1) &&
  598. (temp > 0) && (temp < IW_ENCODE_INDEX))
  599. {
  600. wrq.u.essid.flags = temp;
  601. ++i;
  602. }
  603. }
  604. }
  605. /* Get version from kernel, device may not have range... */
  606. we_kernel_version = iw_get_kernel_we_version();
  607. /* Finally set the ESSID value */
  608. wrq.u.essid.pointer = (caddr_t) essid;
  609. wrq.u.essid.length = strlen(essid);
  610. if(we_kernel_version < 21)
  611. wrq.u.essid.length++;
  612. if(iw_set_ext(skfd, ifname, SIOCSIWESSID, &wrq) < 0)
  613. return(IWERR_SET_EXT);
  614. /* Var args */
  615. return(i);
  616. }
  617. /*------------------------------------------------------------------*/
  618. /*
  619. * Set Mode
  620. */
  621. static int
  622. set_mode_info(int skfd,
  623. char * ifname,
  624. char * args[], /* Command line args */
  625. int count) /* Args count */
  626. {
  627. struct iwreq wrq;
  628. unsigned int k; /* Must be unsigned */
  629. /* Avoid "Unused parameter" warning */
  630. count = count;
  631. /* Check if it is a uint, otherwise get is as a string */
  632. if(sscanf(args[0], "%i", &k) != 1)
  633. {
  634. k = 0;
  635. while((k < IW_NUM_OPER_MODE) &&
  636. strncasecmp(args[0], iw_operation_mode[k], 3))
  637. k++;
  638. }
  639. if(k >= IW_NUM_OPER_MODE)
  640. {
  641. errarg = 0;
  642. return(IWERR_ARG_TYPE);
  643. }
  644. wrq.u.mode = k;
  645. if(iw_set_ext(skfd, ifname, SIOCSIWMODE, &wrq) < 0)
  646. return(IWERR_SET_EXT);
  647. /* 1 arg */
  648. return(1);
  649. }
  650. /*------------------------------------------------------------------*/
  651. /*
  652. * Set frequency/channel
  653. */
  654. static int
  655. set_freq_info(int skfd,
  656. char * ifname,
  657. char * args[], /* Command line args */
  658. int count) /* Args count */
  659. {
  660. struct iwreq wrq;
  661. int i = 1;
  662. if(!strcasecmp(args[0], "auto"))
  663. {
  664. wrq.u.freq.m = -1;
  665. wrq.u.freq.e = 0;
  666. wrq.u.freq.flags = 0;
  667. }
  668. else
  669. {
  670. if(!strcasecmp(args[0], "fixed"))
  671. {
  672. /* Get old frequency */
  673. if(iw_get_ext(skfd, ifname, SIOCGIWFREQ, &wrq) < 0)
  674. return(IWERR_GET_EXT);
  675. wrq.u.freq.flags = IW_FREQ_FIXED;
  676. }
  677. else /* Should be a numeric value */
  678. {
  679. double freq;
  680. char * unit;
  681. freq = strtod(args[0], &unit);
  682. if(unit == args[0])
  683. {
  684. errarg = 0;
  685. return(IWERR_ARG_TYPE);
  686. }
  687. if(unit != NULL)
  688. {
  689. if(unit[0] == 'G') freq *= GIGA;
  690. if(unit[0] == 'M') freq *= MEGA;
  691. if(unit[0] == 'k') freq *= KILO;
  692. }
  693. iw_float2freq(freq, &(wrq.u.freq));
  694. wrq.u.freq.flags = IW_FREQ_FIXED;
  695. /* Check for an additional argument */
  696. if((i < count) && (!strcasecmp(args[i], "auto")))
  697. {
  698. wrq.u.freq.flags = 0;
  699. ++i;
  700. }
  701. if((i < count) && (!strcasecmp(args[i], "fixed")))
  702. {
  703. wrq.u.freq.flags = IW_FREQ_FIXED;
  704. ++i;
  705. }
  706. }
  707. }
  708. if(iw_set_ext(skfd, ifname, SIOCSIWFREQ, &wrq) < 0)
  709. return(IWERR_SET_EXT);
  710. /* Var args */
  711. return(i);
  712. }
  713. /*------------------------------------------------------------------*/
  714. /*
  715. * Set Bit Rate
  716. */
  717. static int
  718. set_bitrate_info(int skfd,
  719. char * ifname,
  720. char * args[], /* Command line args */
  721. int count) /* Args count */
  722. {
  723. struct iwreq wrq;
  724. int i = 1;
  725. wrq.u.bitrate.flags = 0;
  726. if(!strcasecmp(args[0], "auto"))
  727. {
  728. wrq.u.bitrate.value = -1;
  729. wrq.u.bitrate.fixed = 0;
  730. }
  731. else
  732. {
  733. if(!strcasecmp(args[0], "fixed"))
  734. {
  735. /* Get old bitrate */
  736. if(iw_get_ext(skfd, ifname, SIOCGIWRATE, &wrq) < 0)
  737. return(IWERR_GET_EXT);
  738. wrq.u.bitrate.fixed = 1;
  739. }
  740. else /* Should be a numeric value */
  741. {
  742. double brate;
  743. char * unit;
  744. brate = strtod(args[0], &unit);
  745. if(unit == args[0])
  746. {
  747. errarg = 0;
  748. return(IWERR_ARG_TYPE);
  749. }
  750. if(unit != NULL)
  751. {
  752. if(unit[0] == 'G') brate *= GIGA;
  753. if(unit[0] == 'M') brate *= MEGA;
  754. if(unit[0] == 'k') brate *= KILO;
  755. }
  756. wrq.u.bitrate.value = (long) brate;
  757. wrq.u.bitrate.fixed = 1;
  758. /* Check for an additional argument */
  759. if((i < count) && (!strcasecmp(args[i], "auto")))
  760. {
  761. wrq.u.bitrate.fixed = 0;
  762. ++i;
  763. }
  764. if((i < count) && (!strcasecmp(args[i], "fixed")))
  765. {
  766. wrq.u.bitrate.fixed = 1;
  767. ++i;
  768. }
  769. if((i < count) && (!strcasecmp(args[i], "unicast")))
  770. {
  771. wrq.u.bitrate.flags |= IW_BITRATE_UNICAST;
  772. ++i;
  773. }
  774. if((i < count) && (!strcasecmp(args[i], "broadcast")))
  775. {
  776. wrq.u.bitrate.flags |= IW_BITRATE_BROADCAST;
  777. ++i;
  778. }
  779. }
  780. }
  781. if(iw_set_ext(skfd, ifname, SIOCSIWRATE, &wrq) < 0)
  782. return(IWERR_SET_EXT);
  783. /* Var args */
  784. return(i);
  785. }
  786. /*------------------------------------------------------------------*/
  787. /*
  788. * Set encryption
  789. */
  790. static int
  791. set_enc_info(int skfd,
  792. char * ifname,
  793. char * args[], /* Command line args */
  794. int count) /* Args count */
  795. {
  796. struct iwreq wrq;
  797. int i = 1;
  798. unsigned char key[IW_ENCODING_TOKEN_MAX];
  799. if(!strcasecmp(args[0], "on"))
  800. {
  801. /* Get old encryption information */
  802. wrq.u.data.pointer = (caddr_t) key;
  803. wrq.u.data.length = IW_ENCODING_TOKEN_MAX;
  804. wrq.u.data.flags = 0;
  805. if(iw_get_ext(skfd, ifname, SIOCGIWENCODE, &wrq) < 0)
  806. return(IWERR_GET_EXT);
  807. wrq.u.data.flags &= ~IW_ENCODE_DISABLED; /* Enable */
  808. }
  809. else
  810. {
  811. int gotone = 0;
  812. int oldone;
  813. int keylen;
  814. int temp;
  815. wrq.u.data.pointer = (caddr_t) NULL;
  816. wrq.u.data.flags = 0;
  817. wrq.u.data.length = 0;
  818. i = 0;
  819. /* Allow arguments in any order (it's safe) */
  820. do
  821. {
  822. oldone = gotone;
  823. /* -- Check for the key -- */
  824. if(i < count)
  825. {
  826. keylen = iw_in_key_full(skfd, ifname,
  827. args[i], key, &wrq.u.data.flags);
  828. if(keylen > 0)
  829. {
  830. wrq.u.data.length = keylen;
  831. wrq.u.data.pointer = (caddr_t) key;
  832. ++i;
  833. gotone++;
  834. }
  835. }
  836. /* -- Check for token index -- */
  837. if((i < count) &&
  838. (sscanf(args[i], "[%i]", &temp) == 1) &&
  839. (temp > 0) && (temp < IW_ENCODE_INDEX))
  840. {
  841. wrq.u.encoding.flags |= temp;
  842. ++i;
  843. gotone++;
  844. }
  845. /* -- Check the various flags -- */
  846. if((i < count) && (!strcasecmp(args[i], "off")))
  847. {
  848. wrq.u.data.flags |= IW_ENCODE_DISABLED;
  849. ++i;
  850. gotone++;
  851. }
  852. if((i < count) && (!strcasecmp(args[i], "open")))
  853. {
  854. wrq.u.data.flags |= IW_ENCODE_OPEN;
  855. ++i;
  856. gotone++;
  857. }
  858. if((i < count) && (!strncasecmp(args[i], "restricted", 5)))
  859. {
  860. wrq.u.data.flags |= IW_ENCODE_RESTRICTED;
  861. ++i;
  862. gotone++;
  863. }
  864. if((i < count) && (!strncasecmp(args[i], "temporary", 4)))
  865. {
  866. wrq.u.data.flags |= IW_ENCODE_TEMP;
  867. ++i;
  868. gotone++;
  869. }
  870. }
  871. while(gotone != oldone);
  872. /* Pointer is absent in new API */
  873. if(wrq.u.data.pointer == NULL)
  874. wrq.u.data.flags |= IW_ENCODE_NOKEY;
  875. /* Check if we have any invalid argument */
  876. if(!gotone)
  877. {
  878. errarg = 0;
  879. return(IWERR_ARG_TYPE);
  880. }
  881. }
  882. if(iw_set_ext(skfd, ifname, SIOCSIWENCODE, &wrq) < 0)
  883. return(IWERR_SET_EXT);
  884. /* Var arg */
  885. return(i);
  886. }
  887. /*------------------------------------------------------------------*/
  888. /*
  889. * Set Power Management
  890. */
  891. static int
  892. set_power_info(int skfd,
  893. char * ifname,
  894. char * args[], /* Command line args */
  895. int count) /* Args count */
  896. {
  897. struct iwreq wrq;
  898. int i = 1;
  899. if(!strcasecmp(args[0], "off"))
  900. wrq.u.power.disabled = 1; /* i.e. max size */
  901. else
  902. if(!strcasecmp(args[0], "on"))
  903. {
  904. /* Get old Power info */
  905. wrq.u.power.flags = 0;
  906. if(iw_get_ext(skfd, ifname, SIOCGIWPOWER, &wrq) < 0)
  907. return(IWERR_GET_EXT);
  908. wrq.u.power.disabled = 0;
  909. }
  910. else
  911. {
  912. double value;
  913. char * unit;
  914. int gotone = 0;
  915. /* Parse modifiers */
  916. i = parse_modifiers(args, count, &wrq.u.power.flags,
  917. iwmod_power, IWMOD_POWER_NUM);
  918. if(i < 0)
  919. return(i);
  920. wrq.u.power.disabled = 0;
  921. /* Is there any value to grab ? */
  922. value = strtod(args[0], &unit);
  923. if(unit != args[0])
  924. {
  925. struct iw_range range;
  926. int flags;
  927. /* Extract range info to handle properly 'relative' */
  928. if(iw_get_range_info(skfd, ifname, &range) < 0)
  929. memset(&range, 0, sizeof(range));
  930. /* Get the flags to be able to do the proper conversion */
  931. switch(wrq.u.power.flags & IW_POWER_TYPE)
  932. {
  933. case IW_POWER_SAVING:
  934. flags = range.pms_flags;
  935. break;
  936. case IW_POWER_TIMEOUT:
  937. flags = range.pmt_flags;
  938. break;
  939. default:
  940. flags = range.pmp_flags;
  941. break;
  942. }
  943. /* Check if time or relative */
  944. if(flags & IW_POWER_RELATIVE)
  945. {
  946. if(range.we_version_compiled < 21)
  947. value *= MEGA;
  948. else
  949. wrq.u.power.flags |= IW_POWER_RELATIVE;
  950. }
  951. else
  952. {
  953. value *= MEGA; /* default = s */
  954. if(unit[0] == 'u') value /= MEGA;
  955. if(unit[0] == 'm') value /= KILO;
  956. }
  957. wrq.u.power.value = (long) value;
  958. /* Set some default type if none */
  959. if((wrq.u.power.flags & IW_POWER_TYPE) == 0)
  960. wrq.u.power.flags |= IW_POWER_PERIOD;
  961. ++i;
  962. gotone = 1;
  963. }
  964. /* Now, check the mode */
  965. if(i < count)
  966. {
  967. if(!strcasecmp(args[i], "all"))
  968. wrq.u.power.flags |= IW_POWER_ALL_R;
  969. if(!strncasecmp(args[i], "unicast", 4))
  970. wrq.u.power.flags |= IW_POWER_UNICAST_R;
  971. if(!strncasecmp(args[i], "multicast", 5))
  972. wrq.u.power.flags |= IW_POWER_MULTICAST_R;
  973. if(!strncasecmp(args[i], "force", 5))
  974. wrq.u.power.flags |= IW_POWER_FORCE_S;
  975. if(!strcasecmp(args[i], "repeat"))
  976. wrq.u.power.flags |= IW_POWER_REPEATER;
  977. if(wrq.u.power.flags & IW_POWER_MODE)
  978. {
  979. ++i;
  980. gotone = 1;
  981. }
  982. }
  983. if(!gotone)
  984. {
  985. errarg = i;
  986. return(IWERR_ARG_TYPE);
  987. }
  988. }
  989. if(iw_set_ext(skfd, ifname, SIOCSIWPOWER, &wrq) < 0)
  990. return(IWERR_SET_EXT);
  991. /* Var args */
  992. return(i);
  993. }
  994. #ifndef WE_ESSENTIAL
  995. /*------------------------------------------------------------------*/
  996. /*
  997. * Set Nickname
  998. */
  999. static int
  1000. set_nick_info(int skfd,
  1001. char * ifname,
  1002. char * args[], /* Command line args */
  1003. int count) /* Args count */
  1004. {
  1005. struct iwreq wrq;
  1006. int we_kernel_version;
  1007. /* Avoid "Unused parameter" warning */
  1008. count = count;
  1009. if(strlen(args[0]) > IW_ESSID_MAX_SIZE)
  1010. {
  1011. errmax = IW_ESSID_MAX_SIZE;
  1012. return(IWERR_ARG_SIZE);
  1013. }
  1014. we_kernel_version = iw_get_kernel_we_version();
  1015. wrq.u.essid.pointer = (caddr_t) args[0];
  1016. wrq.u.essid.length = strlen(args[0]);
  1017. if(we_kernel_version < 21)
  1018. wrq.u.essid.length++;
  1019. if(iw_set_ext(skfd, ifname, SIOCSIWNICKN, &wrq) < 0)
  1020. return(IWERR_SET_EXT);
  1021. /* 1 args */
  1022. return(1);
  1023. }
  1024. /*------------------------------------------------------------------*/
  1025. /*
  1026. * Set commit
  1027. */
  1028. static int
  1029. set_nwid_info(int skfd,
  1030. char * ifname,
  1031. char * args[], /* Command line args */
  1032. int count) /* Args count */
  1033. {
  1034. struct iwreq wrq;
  1035. unsigned long temp;
  1036. /* Avoid "Unused parameter" warning */
  1037. count = count;
  1038. if((!strcasecmp(args[0], "off")) ||
  1039. (!strcasecmp(args[0], "any")))
  1040. wrq.u.nwid.disabled = 1;
  1041. else
  1042. if(!strcasecmp(args[0], "on"))
  1043. {
  1044. /* Get old nwid */
  1045. if(iw_get_ext(skfd, ifname, SIOCGIWNWID, &wrq) < 0)
  1046. return(IWERR_GET_EXT);
  1047. wrq.u.nwid.disabled = 0;
  1048. }
  1049. else
  1050. if(sscanf(args[0], "%lX", &(temp)) != 1)
  1051. {
  1052. errarg = 0;
  1053. return(IWERR_ARG_TYPE);
  1054. }
  1055. else
  1056. {
  1057. wrq.u.nwid.value = temp;
  1058. wrq.u.nwid.disabled = 0;
  1059. }
  1060. wrq.u.nwid.fixed = 1;
  1061. /* Set new nwid */
  1062. if(iw_set_ext(skfd, ifname, SIOCSIWNWID, &wrq) < 0)
  1063. return(IWERR_SET_EXT);
  1064. /* 1 arg */
  1065. return(1);
  1066. }
  1067. /*------------------------------------------------------------------*/
  1068. /*
  1069. * Set AP Address
  1070. */
  1071. static int
  1072. set_apaddr_info(int skfd,
  1073. char * ifname,
  1074. char * args[], /* Command line args */
  1075. int count) /* Args count */
  1076. {
  1077. struct iwreq wrq;
  1078. /* Avoid "Unused parameter" warning */
  1079. count = count;
  1080. if((!strcasecmp(args[0], "auto")) ||
  1081. (!strcasecmp(args[0], "any")))
  1082. {
  1083. /* Send a broadcast address */
  1084. iw_broad_ether(&(wrq.u.ap_addr));
  1085. }
  1086. else
  1087. {
  1088. if(!strcasecmp(args[0], "off"))
  1089. {
  1090. /* Send a NULL address */
  1091. iw_null_ether(&(wrq.u.ap_addr));
  1092. }
  1093. else
  1094. {
  1095. /* Get the address and check if the interface supports it */
  1096. if(iw_in_addr(skfd, ifname, args[0], &(wrq.u.ap_addr)) < 0)
  1097. {
  1098. errarg = 0;
  1099. return(IWERR_ARG_TYPE);
  1100. }
  1101. }
  1102. }
  1103. if(iw_set_ext(skfd, ifname, SIOCSIWAP, &wrq) < 0)
  1104. return(IWERR_SET_EXT);
  1105. /* 1 args */
  1106. return(1);
  1107. }
  1108. /*------------------------------------------------------------------*/
  1109. /*
  1110. * Set Tx Power
  1111. */
  1112. static int
  1113. set_txpower_info(int skfd,
  1114. char * ifname,
  1115. char * args[], /* Command line args */
  1116. int count) /* Args count */
  1117. {
  1118. struct iwreq wrq;
  1119. int i = 1;
  1120. /* Avoid "Unused parameter" warning */
  1121. args = args; count = count;
  1122. /* Prepare the request */
  1123. wrq.u.txpower.value = -1;
  1124. wrq.u.txpower.fixed = 1;
  1125. wrq.u.txpower.disabled = 0;
  1126. wrq.u.txpower.flags = IW_TXPOW_DBM;
  1127. if(!strcasecmp(args[0], "off"))
  1128. wrq.u.txpower.disabled = 1; /* i.e. turn radio off */
  1129. else
  1130. if(!strcasecmp(args[0], "auto"))
  1131. wrq.u.txpower.fixed = 0; /* i.e. use power control */
  1132. else
  1133. {
  1134. if(!strcasecmp(args[0], "on"))
  1135. {
  1136. /* Get old tx-power */
  1137. if(iw_get_ext(skfd, ifname, SIOCGIWTXPOW, &wrq) < 0)
  1138. return(IWERR_GET_EXT);
  1139. wrq.u.txpower.disabled = 0;
  1140. }
  1141. else
  1142. {
  1143. if(!strcasecmp(args[0], "fixed"))
  1144. {
  1145. /* Get old tx-power */
  1146. if(iw_get_ext(skfd, ifname, SIOCGIWTXPOW, &wrq) < 0)
  1147. return(IWERR_GET_EXT);
  1148. wrq.u.txpower.fixed = 1;
  1149. wrq.u.txpower.disabled = 0;
  1150. }
  1151. else /* Should be a numeric value */
  1152. {
  1153. int power;
  1154. int ismwatt = 0;
  1155. struct iw_range range;
  1156. /* Extract range info to do proper conversion */
  1157. if(iw_get_range_info(skfd, ifname, &range) < 0)
  1158. memset(&range, 0, sizeof(range));
  1159. /* Get the value */
  1160. if(sscanf(args[0], "%i", &(power)) != 1)
  1161. {
  1162. errarg = 0;
  1163. return(IWERR_ARG_TYPE);
  1164. }
  1165. /* Check if milliWatt
  1166. * We authorise a single 'm' as a shorthand for 'mW',
  1167. * on the other hand a 'd' probably means 'dBm'... */
  1168. ismwatt = ((strchr(args[0], 'm') != NULL)
  1169. && (strchr(args[0], 'd') == NULL));
  1170. /* We could check 'W' alone... Another time... */
  1171. /* Convert */
  1172. if(range.txpower_capa & IW_TXPOW_RELATIVE)
  1173. {
  1174. /* Can't convert */
  1175. if(ismwatt)
  1176. {
  1177. errarg = 0;
  1178. return(IWERR_ARG_TYPE);
  1179. }
  1180. wrq.u.txpower.flags = IW_TXPOW_RELATIVE;
  1181. }
  1182. else
  1183. if(range.txpower_capa & IW_TXPOW_MWATT)
  1184. {
  1185. if(!ismwatt)
  1186. power = iw_dbm2mwatt(power);
  1187. wrq.u.txpower.flags = IW_TXPOW_MWATT;
  1188. }
  1189. else
  1190. {
  1191. if(ismwatt)
  1192. power = iw_mwatt2dbm(power);
  1193. wrq.u.txpower.flags = IW_TXPOW_DBM;
  1194. }
  1195. wrq.u.txpower.value = power;
  1196. /* Check for an additional argument */
  1197. if((i < count) && (!strcasecmp(args[i], "auto")))
  1198. {
  1199. wrq.u.txpower.fixed = 0;
  1200. ++i;
  1201. }
  1202. if((i < count) && (!strcasecmp(args[i], "fixed")))
  1203. {
  1204. wrq.u.txpower.fixed = 1;
  1205. ++i;
  1206. }
  1207. }
  1208. }
  1209. }
  1210. if(iw_set_ext(skfd, ifname, SIOCSIWTXPOW, &wrq) < 0)
  1211. return(IWERR_SET_EXT);
  1212. /* Var args */
  1213. return(i);
  1214. }
  1215. /*------------------------------------------------------------------*/
  1216. /*
  1217. * Set Sensitivity
  1218. */
  1219. static int
  1220. set_sens_info(int skfd,
  1221. char * ifname,
  1222. char * args[], /* Command line args */
  1223. int count) /* Args count */
  1224. {
  1225. struct iwreq wrq;
  1226. int temp;
  1227. /* Avoid "Unused parameter" warning */
  1228. count = count;
  1229. if(sscanf(args[0], "%i", &(temp)) != 1)
  1230. {
  1231. errarg = 0;
  1232. return(IWERR_ARG_TYPE);
  1233. }
  1234. wrq.u.sens.value = temp;
  1235. if(iw_set_ext(skfd, ifname, SIOCSIWSENS, &wrq) < 0)
  1236. return(IWERR_SET_EXT);
  1237. /* 1 arg */
  1238. return(1);
  1239. }
  1240. /*------------------------------------------------------------------*/
  1241. /*
  1242. * Set Retry Limit
  1243. */
  1244. static int
  1245. set_retry_info(int skfd,
  1246. char * ifname,
  1247. char * args[], /* Command line args */
  1248. int count) /* Args count */
  1249. {
  1250. struct iwreq wrq;
  1251. int i = 0;
  1252. double value;
  1253. char * unit;
  1254. /* Parse modifiers */
  1255. i = parse_modifiers(args, count, &wrq.u.retry.flags,
  1256. iwmod_retry, IWMOD_RETRY_NUM);
  1257. if(i < 0)
  1258. return(i);
  1259. /* Add default type if none */
  1260. if((wrq.u.retry.flags & IW_RETRY_TYPE) == 0)
  1261. wrq.u.retry.flags |= IW_RETRY_LIMIT;
  1262. wrq.u.retry.disabled = 0;
  1263. /* Is there any value to grab ? */
  1264. value = strtod(args[0], &unit);
  1265. if(unit == args[0])
  1266. {
  1267. errarg = i;
  1268. return(IWERR_ARG_TYPE);
  1269. }
  1270. /* Limit is absolute, on the other hand lifetime is seconds */
  1271. if(wrq.u.retry.flags & IW_RETRY_LIFETIME)
  1272. {
  1273. struct iw_range range;
  1274. /* Extract range info to handle properly 'relative' */
  1275. if(iw_get_range_info(skfd, ifname, &range) < 0)
  1276. memset(&range, 0, sizeof(range));
  1277. if(range.r_time_flags & IW_RETRY_RELATIVE)
  1278. {
  1279. if(range.we_version_compiled < 21)
  1280. value *= MEGA;
  1281. else
  1282. wrq.u.retry.flags |= IW_RETRY_RELATIVE;
  1283. }
  1284. else
  1285. {
  1286. /* Normalise lifetime */
  1287. value *= MEGA; /* default = s */
  1288. if(unit[0] == 'u') value /= MEGA;
  1289. if(unit[0] == 'm') value /= KILO;
  1290. }
  1291. }
  1292. wrq.u.retry.value = (long) value;
  1293. ++i;
  1294. if(iw_set_ext(skfd, ifname, SIOCSIWRETRY, &wrq) < 0)
  1295. return(IWERR_SET_EXT);
  1296. /* Var args */
  1297. return(i);
  1298. }
  1299. /*------------------------------------------------------------------*/
  1300. /*
  1301. * Set RTS Threshold
  1302. */
  1303. static int
  1304. set_rts_info(int skfd,
  1305. char * ifname,
  1306. char * args[], /* Command line args */
  1307. int count) /* Args count */
  1308. {
  1309. struct iwreq wrq;
  1310. /* Avoid "Unused parameter" warning */
  1311. count = count;
  1312. wrq.u.rts.value = -1;
  1313. wrq.u.rts.fixed = 1;
  1314. wrq.u.rts.disabled = 0;
  1315. if(!strcasecmp(args[0], "off"))
  1316. wrq.u.rts.disabled = 1; /* i.e. max size */
  1317. else
  1318. if(!strcasecmp(args[0], "auto"))
  1319. wrq.u.rts.fixed = 0;
  1320. else
  1321. {
  1322. if(!strcasecmp(args[0], "fixed"))
  1323. {
  1324. /* Get old RTS threshold */
  1325. if(iw_get_ext(skfd, ifname, SIOCGIWRTS, &wrq) < 0)
  1326. return(IWERR_GET_EXT);
  1327. wrq.u.rts.fixed = 1;
  1328. }
  1329. else
  1330. { /* Should be a numeric value */
  1331. long temp;
  1332. if(sscanf(args[0], "%li", (unsigned long *) &(temp)) != 1)
  1333. {
  1334. errarg = 0;
  1335. return(IWERR_ARG_TYPE);
  1336. }
  1337. wrq.u.rts.value = temp;
  1338. }
  1339. }
  1340. if(iw_set_ext(skfd, ifname, SIOCSIWRTS, &wrq) < 0)
  1341. return(IWERR_SET_EXT);
  1342. /* 1 arg */
  1343. return(1);
  1344. }
  1345. /*------------------------------------------------------------------*/
  1346. /*
  1347. * Set Fragmentation Threshold
  1348. */
  1349. static int
  1350. set_frag_info(int skfd,
  1351. char * ifname,
  1352. char * args[], /* Command line args */
  1353. int count) /* Args count */
  1354. {
  1355. struct iwreq wrq;
  1356. /* Avoid "Unused parameter" warning */
  1357. count = count;
  1358. wrq.u.frag.value = -1;
  1359. wrq.u.frag.fixed = 1;
  1360. wrq.u.frag.disabled = 0;
  1361. if(!strcasecmp(args[0], "off"))
  1362. wrq.u.frag.disabled = 1; /* i.e. max size */
  1363. else
  1364. if(!strcasecmp(args[0], "auto"))
  1365. wrq.u.frag.fixed = 0;
  1366. else
  1367. {
  1368. if(!strcasecmp(args[0], "fixed"))
  1369. {
  1370. /* Get old fragmentation threshold */
  1371. if(iw_get_ext(skfd, ifname, SIOCGIWFRAG, &wrq) < 0)
  1372. return(IWERR_GET_EXT);
  1373. wrq.u.frag.fixed = 1;
  1374. }
  1375. else
  1376. { /* Should be a numeric value */
  1377. long temp;
  1378. if(sscanf(args[0], "%li", &(temp))
  1379. != 1)
  1380. {
  1381. errarg = 0;
  1382. return(IWERR_ARG_TYPE);
  1383. }
  1384. wrq.u.frag.value = temp;
  1385. }
  1386. }
  1387. if(iw_set_ext(skfd, ifname, SIOCSIWFRAG, &wrq) < 0)
  1388. return(IWERR_SET_EXT);
  1389. /* 1 arg */
  1390. return(1);
  1391. }
  1392. /*------------------------------------------------------------------*/
  1393. /*
  1394. * Set Modulation
  1395. */
  1396. static int
  1397. set_modulation_info(int skfd,
  1398. char * ifname,
  1399. char * args[], /* Command line args */
  1400. int count) /* Args count */
  1401. {
  1402. struct iwreq wrq;
  1403. int i = 1;
  1404. /* Avoid "Unused parameter" warning */
  1405. args = args; count = count;
  1406. if(!strcasecmp(args[0], "auto"))
  1407. wrq.u.param.fixed = 0; /* i.e. use any modulation */
  1408. else
  1409. {
  1410. if(!strcasecmp(args[0], "fixed"))
  1411. {
  1412. /* Get old modulation */
  1413. if(iw_get_ext(skfd, ifname, SIOCGIWMODUL, &wrq) < 0)
  1414. return(IWERR_GET_EXT);
  1415. wrq.u.param.fixed = 1;
  1416. }
  1417. else
  1418. {
  1419. int k;
  1420. /* Allow multiple modulations, combine them together */
  1421. wrq.u.param.value = 0x0;
  1422. i = 0;
  1423. do
  1424. {
  1425. for(k = 0; k < IW_SIZE_MODUL_LIST; k++)
  1426. {
  1427. if(!strcasecmp(args[i], iw_modul_list[k].cmd))
  1428. {
  1429. wrq.u.param.value |= iw_modul_list[k].mask;
  1430. ++i;
  1431. break;
  1432. }
  1433. }
  1434. }
  1435. /* For as long as current arg matched and not out of args */
  1436. while((i < count) && (k < IW_SIZE_MODUL_LIST));
  1437. /* Check we got something */
  1438. if(i == 0)
  1439. {
  1440. errarg = 0;
  1441. return(IWERR_ARG_TYPE);
  1442. }
  1443. /* Check for an additional argument */
  1444. if((i < count) && (!strcasecmp(args[i], "auto")))
  1445. {
  1446. wrq.u.param.fixed = 0;
  1447. ++i;
  1448. }
  1449. if((i < count) && (!strcasecmp(args[i], "fixed")))
  1450. {
  1451. wrq.u.param.fixed = 1;
  1452. ++i;
  1453. }
  1454. }
  1455. }
  1456. if(iw_set_ext(skfd, ifname, SIOCSIWMODUL, &wrq) < 0)
  1457. return(IWERR_SET_EXT);
  1458. /* Var args */
  1459. return(i);
  1460. }
  1461. #endif /* WE_ESSENTIAL */
  1462. /*------------------------------------------------------------------*/
  1463. /*
  1464. * Set commit
  1465. */
  1466. static int
  1467. set_commit_info(int skfd,
  1468. char * ifname,
  1469. char * args[], /* Command line args */
  1470. int count) /* Args count */
  1471. {
  1472. struct iwreq wrq;
  1473. /* Avoid "Unused parameter" warning */
  1474. args = args; count = count;
  1475. if(iw_set_ext(skfd, ifname, SIOCSIWCOMMIT, &wrq) < 0)
  1476. return(IWERR_SET_EXT);
  1477. /* No args */
  1478. return(0);
  1479. }
  1480. /************************** SET DISPATCHER **************************/
  1481. /*
  1482. * This is a modified version of the dispatcher in iwlist.
  1483. * The main difference is that here we may have multiple commands per
  1484. * line. Also, most commands here do take arguments, and most often
  1485. * a variable number of them.
  1486. * Therefore, the handler *must* return how many args were consumed...
  1487. *
  1488. * Note that the use of multiple commands per line is not advised
  1489. * in scripts, as it makes error management hard. All commands before
  1490. * the error are executed, but commands after the error are not
  1491. * processed.
  1492. * We also try to give as much clue as possible via stderr to the caller
  1493. * on which command did fail, but if there are two time the same command,
  1494. * you don't know which one failed...
  1495. */
  1496. /*------------------------------------------------------------------*/
  1497. /*
  1498. * Map command line arguments to the proper procedure...
  1499. */
  1500. typedef struct iwconfig_entry {
  1501. const char * cmd; /* Command line shorthand */
  1502. iw_enum_handler fn; /* Subroutine */
  1503. int min_count;
  1504. int request; /* WE numerical ID */
  1505. const char * name; /* Human readable string */
  1506. const char * argsname; /* Args as human readable string */
  1507. } iwconfig_cmd;
  1508. static const struct iwconfig_entry iwconfig_cmds[] = {
  1509. { "essid", set_essid_info, 1, SIOCSIWESSID,
  1510. "Set ESSID", "{NNN|any|on|off}" },
  1511. { "mode", set_mode_info, 1, SIOCSIWMODE,
  1512. "Set Mode", "{managed|ad-hoc|master|...}" },
  1513. { "freq", set_freq_info, 1, SIOCSIWFREQ,
  1514. "Set Frequency", "N.NNN[k|M|G]" },
  1515. { "channel", set_freq_info, 1, SIOCSIWFREQ,
  1516. "Set Frequency", "N" },
  1517. { "bit", set_bitrate_info, 1, SIOCSIWRATE,
  1518. "Set Bit Rate", "{N[k|M|G]|auto|fixed}" },
  1519. { "rate", set_bitrate_info, 1, SIOCSIWRATE,
  1520. "Set Bit Rate", "{N[k|M|G]|auto|fixed}" },
  1521. { "enc", set_enc_info, 1, SIOCSIWENCODE,
  1522. "Set Encode", "{NNNN-NNNN|off}" },
  1523. { "key", set_enc_info, 1, SIOCSIWENCODE,
  1524. "Set Encode", "{NNNN-NNNN|off}" },
  1525. { "power", set_power_info, 1, SIOCSIWPOWER,
  1526. "Set Power Management", "{period N|timeout N|saving N|off}" },
  1527. #ifndef WE_ESSENTIAL
  1528. { "nickname", set_nick_info, 1, SIOCSIWNICKN,
  1529. "Set Nickname", "NNN" },
  1530. { "nwid", set_nwid_info, 1, SIOCSIWNWID,
  1531. "Set NWID", "{NN|on|off}" },
  1532. { "ap", set_apaddr_info, 1, SIOCSIWAP,
  1533. "Set AP Address", "{N|off|auto}" },
  1534. { "txpower", set_txpower_info, 1, SIOCSIWTXPOW,
  1535. "Set Tx Power", "{NmW|NdBm|off|auto}" },
  1536. { "sens", set_sens_info, 1, SIOCSIWSENS,
  1537. "Set Sensitivity", "N" },
  1538. { "retry", set_retry_info, 1, SIOCSIWRETRY,
  1539. "Set Retry Limit", "{limit N|lifetime N}" },
  1540. { "rts", set_rts_info, 1, SIOCSIWRTS,
  1541. "Set RTS Threshold", "{N|auto|fixed|off}" },
  1542. { "frag", set_frag_info, 1, SIOCSIWFRAG,
  1543. "Set Fragmentation Threshold", "{N|auto|fixed|off}" },
  1544. { "modulation", set_modulation_info, 1, SIOCGIWMODUL,
  1545. "Set Modulation", "{11g|11a|CCK|OFDMg|...}" },
  1546. #endif /* WE_ESSENTIAL */
  1547. { "commit", set_commit_info, 0, SIOCSIWCOMMIT,
  1548. "Commit changes", "" },
  1549. { NULL, NULL, 0, 0, NULL, NULL },
  1550. };
  1551. /*------------------------------------------------------------------*/
  1552. /*
  1553. * Find the most appropriate command matching the command line
  1554. */
  1555. static inline const iwconfig_cmd *
  1556. find_command(const char * cmd)
  1557. {
  1558. const iwconfig_cmd * found = NULL;
  1559. int ambig = 0;
  1560. unsigned int len = strlen(cmd);
  1561. int i;
  1562. /* Go through all commands */
  1563. for(i = 0; iwconfig_cmds[i].cmd != NULL; ++i)
  1564. {
  1565. /* No match -> next one */
  1566. if(strncasecmp(iwconfig_cmds[i].cmd, cmd, len) != 0)
  1567. continue;
  1568. /* Exact match -> perfect */
  1569. if(len == strlen(iwconfig_cmds[i].cmd))
  1570. return &iwconfig_cmds[i];
  1571. /* Partial match */
  1572. if(found == NULL)
  1573. /* First time */
  1574. found = &iwconfig_cmds[i];
  1575. else
  1576. /* Another time */
  1577. if (iwconfig_cmds[i].fn != found->fn)
  1578. ambig = 1;
  1579. }
  1580. if(found == NULL)
  1581. {
  1582. fprintf(stderr, "iwconfig: unknown command \"%s\"\n", cmd);
  1583. return NULL;
  1584. }
  1585. if(ambig)
  1586. {
  1587. fprintf(stderr, "iwconfig: command \"%s\" is ambiguous\n", cmd);
  1588. return NULL;
  1589. }
  1590. return found;
  1591. }
  1592. /*------------------------------------------------------------------*/
  1593. /*
  1594. * Set the wireless options requested on command line
  1595. * Find the individual commands and call the appropriate subroutine
  1596. */
  1597. static int
  1598. set_info(int skfd, /* The socket */
  1599. char * args[], /* Command line args */
  1600. int count, /* Args count */
  1601. char * ifname) /* Dev name */
  1602. {
  1603. const iwconfig_cmd * iwcmd;
  1604. int ret;
  1605. /* Loop until we run out of args... */
  1606. while(count > 0)
  1607. {
  1608. /* find the command matching the keyword */
  1609. iwcmd = find_command(args[0]);
  1610. if(iwcmd == NULL)
  1611. {
  1612. /* Here we have an unrecognised arg... Error already printed out. */
  1613. return(-1);
  1614. }
  1615. /* One arg is consumed (the command name) */
  1616. args++;
  1617. count--;
  1618. /* Check arg numbers */
  1619. if(count < iwcmd->min_count)
  1620. ret = IWERR_ARG_NUM;
  1621. else
  1622. ret = 0;
  1623. /* Call the command */
  1624. if(!ret)
  1625. ret = (*iwcmd->fn)(skfd, ifname, args, count);
  1626. /* Deal with various errors */
  1627. if(ret < 0)
  1628. {
  1629. int request = iwcmd->request;
  1630. if(ret == IWERR_GET_EXT)
  1631. request++; /* Transform the SET into GET */
  1632. fprintf(stderr, "Error for wireless request \"%s\" (%X) :\n",
  1633. iwcmd->name, request);
  1634. switch(ret)
  1635. {
  1636. case IWERR_ARG_NUM:
  1637. fprintf(stderr, " too few arguments.\n");
  1638. break;
  1639. case IWERR_ARG_TYPE:
  1640. if(errarg < 0)
  1641. errarg = 0;
  1642. if(errarg >= count)
  1643. errarg = count - 1;
  1644. fprintf(stderr, " invalid argument \"%s\".\n", args[errarg]);
  1645. break;
  1646. case IWERR_ARG_SIZE:
  1647. fprintf(stderr, " argument too big (max %d)\n", errmax);
  1648. break;
  1649. case IWERR_ARG_CONFLICT:
  1650. if(errarg < 0)
  1651. errarg = 0;
  1652. if(errarg >= count)
  1653. errarg = count - 1;
  1654. fprintf(stderr, " conflicting argument \"%s\".\n", args[errarg]);
  1655. break;
  1656. case IWERR_SET_EXT:
  1657. fprintf(stderr, " SET failed on device %-1.16s ; %s.\n",
  1658. ifname, strerror(errno));
  1659. break;
  1660. case IWERR_GET_EXT:
  1661. fprintf(stderr, " GET failed on device %-1.16s ; %s.\n",
  1662. ifname, strerror(errno));
  1663. break;
  1664. }
  1665. /* Stop processing, we don't know if we are in a consistent state
  1666. * in reading the command line */
  1667. return(ret);
  1668. }
  1669. /* Substract consumed args from command line */
  1670. args += ret;
  1671. count -= ret;
  1672. /* Loop back */
  1673. }
  1674. /* Done, all done */
  1675. return(0);
  1676. }
  1677. /*------------------------------------------------------------------*/
  1678. /*
  1679. * Display help
  1680. */
  1681. static inline void
  1682. iw_usage(void)
  1683. {
  1684. int i;
  1685. fprintf(stderr, "Usage: iwconfig [interface]\n");
  1686. for(i = 0; iwconfig_cmds[i].cmd != NULL; ++i)
  1687. fprintf(stderr, " interface %s %s\n",
  1688. iwconfig_cmds[i].cmd, iwconfig_cmds[i].argsname);
  1689. fprintf(stderr, " Check man pages for more details.\n");
  1690. }
  1691. /******************************* MAIN ********************************/
  1692. /*------------------------------------------------------------------*/
  1693. /*
  1694. * The main !
  1695. */
  1696. int
  1697. main(int argc,
  1698. char ** argv)
  1699. {
  1700. int skfd; /* generic raw socket desc. */
  1701. int goterr = 0;
  1702. /* Create a channel to the NET kernel. */
  1703. if((skfd = iw_sockets_open()) < 0)
  1704. {
  1705. perror("socket");
  1706. exit(-1);
  1707. }
  1708. /* No argument : show the list of all device + info */
  1709. if(argc == 1)
  1710. iw_enum_devices(skfd, &print_info, NULL, 0);
  1711. else
  1712. /* Special case for help... */
  1713. if((!strcmp(argv[1], "-h")) || (!strcmp(argv[1], "--help")))
  1714. iw_usage();
  1715. else
  1716. /* Special case for version... */
  1717. if(!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version"))
  1718. goterr = iw_print_version_info("iwconfig");
  1719. else
  1720. {
  1721. /* '--' escape device name */
  1722. if((argc > 2) && !strcmp(argv[1], "--"))
  1723. {
  1724. argv++;
  1725. argc--;
  1726. }
  1727. /* The device name must be the first argument */
  1728. if(argc == 2)
  1729. print_info(skfd, argv[1], NULL, 0);
  1730. else
  1731. /* The other args on the line specify options to be set... */
  1732. goterr = set_info(skfd, argv + 2, argc - 2, argv[1]);
  1733. }
  1734. /* Close the socket. */
  1735. iw_sockets_close(skfd);
  1736. return(goterr);
  1737. }