iwpriv.c 28 KB

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