iwinfo_madwifi.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. /*
  2. * iwinfo - Wireless Information Library - Madwifi Backend
  3. *
  4. * Copyright (C) 2009-2010 Jo-Philipp Wich <xm@subsignal.org>
  5. *
  6. * The iwinfo library is free software: you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation.
  9. *
  10. * The iwinfo library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with the iwinfo library. If not, see http://www.gnu.org/licenses/.
  17. *
  18. * The signal handling code is derived from the official madwifi tools,
  19. * wlanconfig.c in particular. The encryption property handling was
  20. * inspired by the hostapd madwifi driver.
  21. */
  22. #include "iwinfo_wext.h"
  23. #include "api/madwifi.h"
  24. /*
  25. * Madwifi ISO 3166 to Country/Region Code mapping.
  26. */
  27. static struct ISO3166_to_CCode
  28. {
  29. u_int16_t iso3166;
  30. u_int16_t ccode;
  31. } CountryCodes[] = {
  32. { 0x3030 /* 00 */, 0 }, /* World */
  33. { 0x4145 /* AE */, 784 }, /* U.A.E. */
  34. { 0x414C /* AL */, 8 }, /* Albania */
  35. { 0x414D /* AM */, 51 }, /* Armenia */
  36. { 0x4152 /* AR */, 32 }, /* Argentina */
  37. { 0x4154 /* AT */, 40 }, /* Austria */
  38. { 0x4155 /* AU */, 36 }, /* Australia */
  39. { 0x415A /* AZ */, 31 }, /* Azerbaijan */
  40. { 0x4245 /* BE */, 56 }, /* Belgium */
  41. { 0x4247 /* BG */, 100 }, /* Bulgaria */
  42. { 0x4248 /* BH */, 48 }, /* Bahrain */
  43. { 0x424E /* BN */, 96 }, /* Brunei Darussalam */
  44. { 0x424F /* BO */, 68 }, /* Bolivia */
  45. { 0x4252 /* BR */, 76 }, /* Brazil */
  46. { 0x4259 /* BY */, 112 }, /* Belarus */
  47. { 0x425A /* BZ */, 84 }, /* Belize */
  48. { 0x4341 /* CA */, 124 }, /* Canada */
  49. { 0x4348 /* CH */, 756 }, /* Switzerland */
  50. { 0x434C /* CL */, 152 }, /* Chile */
  51. { 0x434E /* CN */, 156 }, /* People's Republic of China */
  52. { 0x434F /* CO */, 170 }, /* Colombia */
  53. { 0x4352 /* CR */, 188 }, /* Costa Rica */
  54. { 0x4359 /* CY */, 196 }, /* Cyprus */
  55. { 0x435A /* CZ */, 203 }, /* Czech Republic */
  56. { 0x4445 /* DE */, 276 }, /* Germany */
  57. { 0x444B /* DK */, 208 }, /* Denmark */
  58. { 0x444F /* DO */, 214 }, /* Dominican Republic */
  59. { 0x445A /* DZ */, 12 }, /* Algeria */
  60. { 0x4543 /* EC */, 218 }, /* Ecuador */
  61. { 0x4545 /* EE */, 233 }, /* Estonia */
  62. { 0x4547 /* EG */, 818 }, /* Egypt */
  63. { 0x4553 /* ES */, 724 }, /* Spain */
  64. { 0x4649 /* FI */, 246 }, /* Finland */
  65. { 0x464F /* FO */, 234 }, /* Faeroe Islands */
  66. { 0x4652 /* FR */, 250 }, /* France */
  67. { 0x4652 /* FR */, 255 }, /* France2 */
  68. { 0x4742 /* GB */, 826 }, /* United Kingdom */
  69. { 0x4745 /* GE */, 268 }, /* Georgia */
  70. { 0x4752 /* GR */, 300 }, /* Greece */
  71. { 0x4754 /* GT */, 320 }, /* Guatemala */
  72. { 0x484B /* HK */, 344 }, /* Hong Kong S.A.R., P.R.C. */
  73. { 0x484E /* HN */, 340 }, /* Honduras */
  74. { 0x4852 /* HR */, 191 }, /* Croatia */
  75. { 0x4855 /* HU */, 348 }, /* Hungary */
  76. { 0x4944 /* ID */, 360 }, /* Indonesia */
  77. { 0x4945 /* IE */, 372 }, /* Ireland */
  78. { 0x494C /* IL */, 376 }, /* Israel */
  79. { 0x494E /* IN */, 356 }, /* India */
  80. { 0x4951 /* IQ */, 368 }, /* Iraq */
  81. { 0x4952 /* IR */, 364 }, /* Iran */
  82. { 0x4953 /* IS */, 352 }, /* Iceland */
  83. { 0x4954 /* IT */, 380 }, /* Italy */
  84. { 0x4A4D /* JM */, 388 }, /* Jamaica */
  85. { 0x4A4F /* JO */, 400 }, /* Jordan */
  86. { 0x4A50 /* JP */, 392 }, /* Japan */
  87. { 0x4A50 /* JP */, 393 }, /* Japan (JP1) */
  88. { 0x4A50 /* JP */, 394 }, /* Japan (JP0) */
  89. { 0x4A50 /* JP */, 395 }, /* Japan (JP1-1) */
  90. { 0x4A50 /* JP */, 396 }, /* Japan (JE1) */
  91. { 0x4A50 /* JP */, 397 }, /* Japan (JE2) */
  92. { 0x4A50 /* JP */, 399 }, /* Japan (JP6) */
  93. { 0x4A50 /* JP */, 900 }, /* Japan */
  94. { 0x4A50 /* JP */, 901 }, /* Japan */
  95. { 0x4A50 /* JP */, 902 }, /* Japan */
  96. { 0x4A50 /* JP */, 903 }, /* Japan */
  97. { 0x4A50 /* JP */, 904 }, /* Japan */
  98. { 0x4A50 /* JP */, 905 }, /* Japan */
  99. { 0x4A50 /* JP */, 906 }, /* Japan */
  100. { 0x4A50 /* JP */, 907 }, /* Japan */
  101. { 0x4A50 /* JP */, 908 }, /* Japan */
  102. { 0x4A50 /* JP */, 909 }, /* Japan */
  103. { 0x4A50 /* JP */, 910 }, /* Japan */
  104. { 0x4A50 /* JP */, 911 }, /* Japan */
  105. { 0x4A50 /* JP */, 912 }, /* Japan */
  106. { 0x4A50 /* JP */, 913 }, /* Japan */
  107. { 0x4A50 /* JP */, 914 }, /* Japan */
  108. { 0x4A50 /* JP */, 915 }, /* Japan */
  109. { 0x4A50 /* JP */, 916 }, /* Japan */
  110. { 0x4A50 /* JP */, 917 }, /* Japan */
  111. { 0x4A50 /* JP */, 918 }, /* Japan */
  112. { 0x4A50 /* JP */, 919 }, /* Japan */
  113. { 0x4A50 /* JP */, 920 }, /* Japan */
  114. { 0x4A50 /* JP */, 921 }, /* Japan */
  115. { 0x4A50 /* JP */, 922 }, /* Japan */
  116. { 0x4A50 /* JP */, 923 }, /* Japan */
  117. { 0x4A50 /* JP */, 924 }, /* Japan */
  118. { 0x4A50 /* JP */, 925 }, /* Japan */
  119. { 0x4A50 /* JP */, 926 }, /* Japan */
  120. { 0x4A50 /* JP */, 927 }, /* Japan */
  121. { 0x4A50 /* JP */, 928 }, /* Japan */
  122. { 0x4A50 /* JP */, 929 }, /* Japan */
  123. { 0x4A50 /* JP */, 930 }, /* Japan */
  124. { 0x4A50 /* JP */, 931 }, /* Japan */
  125. { 0x4A50 /* JP */, 932 }, /* Japan */
  126. { 0x4A50 /* JP */, 933 }, /* Japan */
  127. { 0x4A50 /* JP */, 934 }, /* Japan */
  128. { 0x4A50 /* JP */, 935 }, /* Japan */
  129. { 0x4A50 /* JP */, 936 }, /* Japan */
  130. { 0x4A50 /* JP */, 937 }, /* Japan */
  131. { 0x4A50 /* JP */, 938 }, /* Japan */
  132. { 0x4A50 /* JP */, 939 }, /* Japan */
  133. { 0x4A50 /* JP */, 940 }, /* Japan */
  134. { 0x4A50 /* JP */, 941 }, /* Japan */
  135. { 0x4B45 /* KE */, 404 }, /* Kenya */
  136. { 0x4B50 /* KP */, 408 }, /* North Korea */
  137. { 0x4B52 /* KR */, 410 }, /* South Korea */
  138. { 0x4B52 /* KR */, 411 }, /* South Korea */
  139. { 0x4B57 /* KW */, 414 }, /* Kuwait */
  140. { 0x4B5A /* KZ */, 398 }, /* Kazakhstan */
  141. { 0x4C42 /* LB */, 422 }, /* Lebanon */
  142. { 0x4C49 /* LI */, 438 }, /* Liechtenstein */
  143. { 0x4C54 /* LT */, 440 }, /* Lithuania */
  144. { 0x4C55 /* LU */, 442 }, /* Luxembourg */
  145. { 0x4C56 /* LV */, 428 }, /* Latvia */
  146. { 0x4C59 /* LY */, 434 }, /* Libya */
  147. { 0x4D41 /* MA */, 504 }, /* Morocco */
  148. { 0x4D43 /* MC */, 492 }, /* Principality of Monaco */
  149. { 0x4D4B /* MK */, 807 }, /* the Former Yugoslav Republic of Macedonia */
  150. { 0x4D4F /* MO */, 446 }, /* Macau */
  151. { 0x4D58 /* MX */, 484 }, /* Mexico */
  152. { 0x4D59 /* MY */, 458 }, /* Malaysia */
  153. { 0x4E49 /* NI */, 558 }, /* Nicaragua */
  154. { 0x4E4C /* NL */, 528 }, /* Netherlands */
  155. { 0x4E4F /* NO */, 578 }, /* Norway */
  156. { 0x4E5A /* NZ */, 554 }, /* New Zealand */
  157. { 0x4F4D /* OM */, 512 }, /* Oman */
  158. { 0x5041 /* PA */, 591 }, /* Panama */
  159. { 0x5045 /* PE */, 604 }, /* Peru */
  160. { 0x5048 /* PH */, 608 }, /* Republic of the Philippines */
  161. { 0x504B /* PK */, 586 }, /* Islamic Republic of Pakistan */
  162. { 0x504C /* PL */, 616 }, /* Poland */
  163. { 0x5052 /* PR */, 630 }, /* Puerto Rico */
  164. { 0x5054 /* PT */, 620 }, /* Portugal */
  165. { 0x5059 /* PY */, 600 }, /* Paraguay */
  166. { 0x5141 /* QA */, 634 }, /* Qatar */
  167. { 0x524F /* RO */, 642 }, /* Romania */
  168. { 0x5255 /* RU */, 643 }, /* Russia */
  169. { 0x5341 /* SA */, 682 }, /* Saudi Arabia */
  170. { 0x5345 /* SE */, 752 }, /* Sweden */
  171. { 0x5347 /* SG */, 702 }, /* Singapore */
  172. { 0x5349 /* SI */, 705 }, /* Slovenia */
  173. { 0x534B /* SK */, 703 }, /* Slovak Republic */
  174. { 0x5356 /* SV */, 222 }, /* El Salvador */
  175. { 0x5359 /* SY */, 760 }, /* Syria */
  176. { 0x5448 /* TH */, 764 }, /* Thailand */
  177. { 0x544E /* TN */, 788 }, /* Tunisia */
  178. { 0x5452 /* TR */, 792 }, /* Turkey */
  179. { 0x5454 /* TT */, 780 }, /* Trinidad y Tobago */
  180. { 0x5457 /* TW */, 158 }, /* Taiwan */
  181. { 0x5541 /* UA */, 804 }, /* Ukraine */
  182. { 0x554B /* UK */, 826 }, /* United Kingdom */
  183. { 0x5553 /* US */, 840 }, /* United States */
  184. { 0x5553 /* US */, 842 }, /* United States (Public Safety)*/
  185. { 0x5559 /* UY */, 858 }, /* Uruguay */
  186. { 0x555A /* UZ */, 860 }, /* Uzbekistan */
  187. { 0x5645 /* VE */, 862 }, /* Venezuela */
  188. { 0x564E /* VN */, 704 }, /* Viet Nam */
  189. { 0x5945 /* YE */, 887 }, /* Yemen */
  190. { 0x5A41 /* ZA */, 710 }, /* South Africa */
  191. { 0x5A57 /* ZW */, 716 }, /* Zimbabwe */
  192. };
  193. static const char * madwifi_phyname(const char *ifname)
  194. {
  195. static char phyname[IFNAMSIZ];
  196. if (strlen(ifname) > 5 && !strncmp(ifname, "radio", 5))
  197. snprintf(phyname, sizeof(phyname), "wifi%s", ifname + 5);
  198. else
  199. snprintf(phyname, sizeof(phyname), "%s", ifname);
  200. return (const char *)phyname;
  201. }
  202. static int madwifi_wrq(struct iwreq *wrq, const char *ifname, int cmd, void *data, size_t len)
  203. {
  204. strncpy(wrq->ifr_name, ifname, IFNAMSIZ);
  205. if( data != NULL )
  206. {
  207. if( len < IFNAMSIZ )
  208. {
  209. memcpy(wrq->u.name, data, len);
  210. }
  211. else
  212. {
  213. wrq->u.data.pointer = data;
  214. wrq->u.data.length = len;
  215. }
  216. }
  217. return iwinfo_ioctl(cmd, wrq);
  218. }
  219. static int get80211priv(const char *ifname, int op, void *data, size_t len)
  220. {
  221. struct iwreq iwr;
  222. if( madwifi_wrq(&iwr, ifname, op, data, len) < 0 )
  223. return -1;
  224. return iwr.u.data.length;
  225. }
  226. static char * madwifi_isvap(const char *ifname, const char *wifiname)
  227. {
  228. int fd, ln;
  229. char path[32];
  230. char *ret = NULL;
  231. static char name[IFNAMSIZ];
  232. if( strlen(ifname) <= 9 )
  233. {
  234. sprintf(path, "/proc/sys/net/%s/%%parent", ifname);
  235. if( (fd = open(path, O_RDONLY)) > -1 )
  236. {
  237. if( wifiname != NULL )
  238. {
  239. if( read(fd, name, strlen(wifiname)) == strlen(wifiname) )
  240. ret = strncmp(name, wifiname, strlen(wifiname))
  241. ? NULL : name;
  242. }
  243. else if( (ln = read(fd, name, IFNAMSIZ)) >= 4 )
  244. {
  245. name[ln-1] = 0;
  246. ret = name;
  247. }
  248. (void) close(fd);
  249. }
  250. }
  251. return ret;
  252. }
  253. static int madwifi_iswifi(const char *ifname)
  254. {
  255. int ret;
  256. char path[32];
  257. struct stat s;
  258. const char *phy;
  259. ret = 0;
  260. phy = madwifi_phyname(ifname);
  261. if( strlen(phy) <= 7 )
  262. {
  263. sprintf(path, "/proc/sys/dev/%s/diversity", phy);
  264. if( ! stat(path, &s) )
  265. ret = (s.st_mode & S_IFREG);
  266. }
  267. return ret;
  268. }
  269. static char * madwifi_ifadd(const char *ifname)
  270. {
  271. const char *wifidev = NULL;
  272. struct ifreq ifr = { 0 };
  273. struct ieee80211_clone_params cp = { 0 };
  274. static char nif[IFNAMSIZ] = { 0 };
  275. if( !(wifidev = madwifi_isvap(ifname, NULL)) && madwifi_iswifi(ifname) )
  276. wifidev = madwifi_phyname(ifname);
  277. if( wifidev )
  278. {
  279. snprintf(nif, sizeof(nif), "tmp.%s", ifname);
  280. strncpy(cp.icp_name, nif, IFNAMSIZ);
  281. cp.icp_opmode = IEEE80211_M_STA;
  282. cp.icp_flags = IEEE80211_CLONE_BSSID;
  283. strncpy(ifr.ifr_name, wifidev, IFNAMSIZ);
  284. ifr.ifr_data = (void *)&cp;
  285. if( !iwinfo_ioctl(SIOC80211IFCREATE, &ifr) )
  286. {
  287. return nif;
  288. }
  289. else
  290. {
  291. cp.icp_opmode = IEEE80211_M_MONITOR;
  292. if( !iwinfo_ioctl(SIOC80211IFCREATE, &ifr) )
  293. return nif;
  294. }
  295. }
  296. return NULL;
  297. }
  298. static void madwifi_ifdel(const char *ifname)
  299. {
  300. struct ifreq ifr = { 0 };
  301. strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
  302. iwinfo_ioctl(SIOC80211IFDESTROY, &ifr);
  303. }
  304. static int madwifi_probe(const char *ifname)
  305. {
  306. return ( !!madwifi_isvap(ifname, NULL) || madwifi_iswifi(ifname) );
  307. }
  308. static void madwifi_close(void)
  309. {
  310. /* Nop */
  311. }
  312. static int madwifi_get_mode(const char *ifname, int *buf)
  313. {
  314. return wext_ops.mode(ifname, buf);
  315. }
  316. static int madwifi_get_ssid(const char *ifname, char *buf)
  317. {
  318. return wext_ops.ssid(ifname, buf);
  319. }
  320. static int madwifi_get_bssid(const char *ifname, char *buf)
  321. {
  322. return wext_ops.bssid(ifname, buf);
  323. }
  324. static int madwifi_get_channel(const char *ifname, int *buf)
  325. {
  326. int i;
  327. uint16_t freq;
  328. struct iwreq wrq;
  329. struct ieee80211req_chaninfo chans;
  330. if( madwifi_wrq(&wrq, ifname, SIOCGIWFREQ, NULL, 0) >= 0 )
  331. {
  332. /* Madwifi returns a Hz frequency, get it's freq list to find channel index */
  333. freq = (uint16_t)(wrq.u.freq.m / 100000);
  334. if( get80211priv(ifname, IEEE80211_IOCTL_GETCHANINFO, &chans, sizeof(chans)) >= 0 )
  335. {
  336. *buf = 0;
  337. for( i = 0; i < chans.ic_nchans; i++ )
  338. {
  339. if( freq == chans.ic_chans[i].ic_freq )
  340. {
  341. *buf = chans.ic_chans[i].ic_ieee;
  342. break;
  343. }
  344. }
  345. return 0;
  346. }
  347. }
  348. return -1;
  349. }
  350. static int madwifi_get_frequency(const char *ifname, int *buf)
  351. {
  352. struct iwreq wrq;
  353. if( madwifi_wrq(&wrq, ifname, SIOCGIWFREQ, NULL, 0) >= 0 )
  354. {
  355. *buf = (uint16_t)(wrq.u.freq.m / 100000);
  356. return 0;
  357. }
  358. return -1;
  359. }
  360. static int madwifi_get_txpower(const char *ifname, int *buf)
  361. {
  362. return wext_ops.txpower(ifname, buf);
  363. }
  364. static int madwifi_get_bitrate(const char *ifname, int *buf)
  365. {
  366. unsigned int mode, len, rate, rate_count;
  367. uint8_t tmp[24*1024];
  368. uint8_t *cp;
  369. struct iwreq wrq;
  370. struct ieee80211req_sta_info *si;
  371. if( madwifi_wrq(&wrq, ifname, SIOCGIWMODE, NULL, 0) >= 0 )
  372. {
  373. mode = wrq.u.mode;
  374. /* Calculate bitrate average from associated stations in ad-hoc mode */
  375. if( mode == 1 )
  376. {
  377. rate = rate_count = 0;
  378. if( (len = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
  379. {
  380. cp = tmp;
  381. do {
  382. si = (struct ieee80211req_sta_info *) cp;
  383. if( si->isi_rssi > 0 )
  384. {
  385. rate_count++;
  386. rate += ((si->isi_rates[si->isi_txrate] & IEEE80211_RATE_VAL) / 2);
  387. }
  388. cp += si->isi_len;
  389. len -= si->isi_len;
  390. } while (len >= sizeof(struct ieee80211req_sta_info));
  391. }
  392. *buf = (rate == 0 || rate_count == 0) ? 0 : (rate / rate_count) * 1000;
  393. return 0;
  394. }
  395. /* Return whatever wext tells us ... */
  396. return wext_ops.bitrate(ifname, buf);
  397. }
  398. return -1;
  399. }
  400. static int madwifi_get_signal(const char *ifname, int *buf)
  401. {
  402. unsigned int mode, len, rssi, rssi_count;
  403. uint8_t tmp[24*1024];
  404. uint8_t *cp;
  405. struct iwreq wrq;
  406. struct ieee80211req_sta_info *si;
  407. if( madwifi_wrq(&wrq, ifname, SIOCGIWMODE, NULL, 0) >= 0 )
  408. {
  409. mode = wrq.u.mode;
  410. /* Calculate signal average from associated stations in ap or ad-hoc mode */
  411. if( mode == 1 )
  412. {
  413. rssi = rssi_count = 0;
  414. if( (len = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
  415. {
  416. cp = tmp;
  417. do {
  418. si = (struct ieee80211req_sta_info *) cp;
  419. if( si->isi_rssi > 0 )
  420. {
  421. rssi_count++;
  422. rssi -= (si->isi_rssi - 95);
  423. }
  424. cp += si->isi_len;
  425. len -= si->isi_len;
  426. } while (len >= sizeof(struct ieee80211req_sta_info));
  427. }
  428. *buf = (rssi == 0 || rssi_count == 0) ? 1 : -(rssi / rssi_count);
  429. return 0;
  430. }
  431. /* Return whatever wext tells us ... */
  432. return wext_ops.signal(ifname, buf);
  433. }
  434. return -1;
  435. }
  436. static int madwifi_get_noise(const char *ifname, int *buf)
  437. {
  438. return wext_ops.noise(ifname, buf);
  439. }
  440. static int madwifi_get_quality(const char *ifname, int *buf)
  441. {
  442. unsigned int mode, len, quality, quality_count;
  443. uint8_t tmp[24*1024];
  444. uint8_t *cp;
  445. struct iwreq wrq;
  446. struct ieee80211req_sta_info *si;
  447. if( madwifi_wrq(&wrq, ifname, SIOCGIWMODE, NULL, 0) >= 0 )
  448. {
  449. mode = wrq.u.mode;
  450. /* Calculate signal average from associated stations in ad-hoc mode */
  451. if( mode == 1 )
  452. {
  453. quality = quality_count = 0;
  454. if( (len = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
  455. {
  456. cp = tmp;
  457. do {
  458. si = (struct ieee80211req_sta_info *) cp;
  459. if( si->isi_rssi > 0 )
  460. {
  461. quality_count++;
  462. quality += si->isi_rssi;
  463. }
  464. cp += si->isi_len;
  465. len -= si->isi_len;
  466. } while (len >= sizeof(struct ieee80211req_sta_info));
  467. }
  468. *buf = (quality == 0 || quality_count == 0) ? 0 : (quality / quality_count);
  469. return 0;
  470. }
  471. /* Return whatever wext tells us ... */
  472. return wext_ops.quality(ifname, buf);
  473. }
  474. return -1;
  475. }
  476. static int madwifi_get_quality_max(const char *ifname, int *buf)
  477. {
  478. return wext_ops.quality_max(ifname, buf);
  479. }
  480. static int madwifi_get_encryption(const char *ifname, char *buf)
  481. {
  482. int ciphers = 0, key_len = 0;
  483. char keybuf[IW_ENCODING_TOKEN_MAX];
  484. struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf;
  485. struct iwreq wrq;
  486. struct ieee80211req_key wk;
  487. memset(&wrq, 0, sizeof(wrq));
  488. /* Obtain key info */
  489. if( madwifi_wrq(&wrq, ifname, SIOCGIWENCODE, keybuf, sizeof(keybuf)) < 0 )
  490. return -1;
  491. #if 0
  492. /* Have any encryption? */
  493. if( (wrq.u.data.flags & IW_ENCODE_DISABLED) || (wrq.u.data.length == 0) )
  494. return 0;
  495. #endif
  496. /* Save key len */
  497. key_len = wrq.u.data.length;
  498. /* Get wpa protocol version */
  499. wrq.u.mode = IEEE80211_PARAM_WPA;
  500. if( madwifi_wrq(&wrq, ifname, IEEE80211_IOCTL_GETPARAM, NULL, 0) >= 0 )
  501. c->wpa_version = wrq.u.mode;
  502. /* Get authentication suites */
  503. wrq.u.mode = IEEE80211_PARAM_AUTHMODE;
  504. if( madwifi_wrq(&wrq, ifname, IEEE80211_IOCTL_GETPARAM, NULL, 0) >= 0 )
  505. {
  506. switch(wrq.u.mode) {
  507. case IEEE80211_AUTH_8021X:
  508. c->auth_suites |= IWINFO_KMGMT_8021x;
  509. break;
  510. case IEEE80211_AUTH_WPA:
  511. c->auth_suites |= IWINFO_KMGMT_PSK;
  512. break;
  513. case IEEE80211_AUTH_OPEN:
  514. c->auth_algs |= IWINFO_AUTH_OPEN;
  515. break;
  516. case IEEE80211_AUTH_SHARED:
  517. c->auth_algs |= IWINFO_AUTH_SHARED;
  518. break;
  519. default:
  520. c->auth_suites |= IWINFO_KMGMT_NONE;
  521. break;
  522. }
  523. }
  524. memset(&wk, 0, sizeof(wk));
  525. memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
  526. /* Get key information */
  527. if( get80211priv(ifname, IEEE80211_IOCTL_GETKEY, &wk, sizeof(wk)) >= 0 )
  528. {
  529. /* Type 0 == WEP */
  530. if( (wk.ik_type == 0) && (c->auth_algs == 0) )
  531. c->auth_algs = (IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED);
  532. }
  533. /* Get used pairwise ciphers */
  534. wrq.u.mode = IEEE80211_PARAM_UCASTCIPHERS;
  535. if( madwifi_wrq(&wrq, ifname, IEEE80211_IOCTL_GETPARAM, NULL, 0) >= 0 )
  536. {
  537. ciphers = wrq.u.mode;
  538. if( c->wpa_version && ciphers & (1 << IEEE80211_CIPHER_TKIP) )
  539. c->pair_ciphers |= IWINFO_CIPHER_TKIP;
  540. if( c->wpa_version && ciphers & (1 << IEEE80211_CIPHER_AES_CCM) )
  541. c->pair_ciphers |= IWINFO_CIPHER_CCMP;
  542. if( c->wpa_version && ciphers & (1 << IEEE80211_CIPHER_AES_OCB) )
  543. c->pair_ciphers |= IWINFO_CIPHER_AESOCB;
  544. if( c->wpa_version && ciphers & (1 << IEEE80211_CIPHER_CKIP) )
  545. c->pair_ciphers |= IWINFO_CIPHER_CKIP;
  546. if( !c->pair_ciphers && ciphers & (1 << IEEE80211_CIPHER_WEP) )
  547. {
  548. switch(key_len) {
  549. case 13:
  550. c->pair_ciphers |= IWINFO_CIPHER_WEP104;
  551. break;
  552. case 5:
  553. c->pair_ciphers |= IWINFO_CIPHER_WEP40;
  554. break;
  555. case 0:
  556. break;
  557. default:
  558. c->pair_ciphers = IWINFO_CIPHER_WEP40 |
  559. IWINFO_CIPHER_WEP104;
  560. break;
  561. }
  562. }
  563. if( ciphers & (1 << IEEE80211_CIPHER_NONE) )
  564. c->pair_ciphers |= IWINFO_CIPHER_NONE;
  565. }
  566. /* Get used group cipher */
  567. wrq.u.mode = IEEE80211_PARAM_MCASTCIPHER;
  568. if( madwifi_wrq(&wrq, ifname, IEEE80211_IOCTL_GETPARAM, NULL, 0) >= 0 )
  569. {
  570. ciphers = c->wpa_version ? wrq.u.mode : IEEE80211_CIPHER_WEP;
  571. switch(ciphers) {
  572. case IEEE80211_CIPHER_TKIP:
  573. c->group_ciphers |= IWINFO_CIPHER_TKIP;
  574. break;
  575. case IEEE80211_CIPHER_AES_CCM:
  576. c->group_ciphers |= IWINFO_CIPHER_CCMP;
  577. break;
  578. case IEEE80211_CIPHER_AES_OCB:
  579. c->group_ciphers |= IWINFO_CIPHER_AESOCB;
  580. break;
  581. case IEEE80211_CIPHER_CKIP:
  582. c->group_ciphers |= IWINFO_CIPHER_CKIP;
  583. break;
  584. case IEEE80211_CIPHER_WEP:
  585. switch(key_len) {
  586. case 13:
  587. c->group_ciphers |= IWINFO_CIPHER_WEP104;
  588. break;
  589. case 5:
  590. c->group_ciphers |= IWINFO_CIPHER_WEP40;
  591. break;
  592. default:
  593. break;
  594. }
  595. break;
  596. case IEEE80211_CIPHER_NONE:
  597. c->group_ciphers |= IWINFO_CIPHER_NONE;
  598. break;
  599. default:
  600. break;
  601. }
  602. }
  603. c->enabled = (c->wpa_version || (c->auth_algs && c->pair_ciphers)) ? 1 : 0;
  604. return 0;
  605. }
  606. static int madwifi_get_phyname(const char *ifname, char *buf)
  607. {
  608. const char *wifidev;
  609. wifidev = madwifi_isvap(ifname, NULL);
  610. if (wifidev)
  611. {
  612. strcpy(buf, wifidev);
  613. return 0;
  614. }
  615. else if (madwifi_iswifi(ifname))
  616. {
  617. strcpy(buf, madwifi_phyname(ifname));
  618. return 0;
  619. }
  620. return -1;
  621. }
  622. static int madwifi_get_assoclist(const char *ifname, char *buf, int *len)
  623. {
  624. int bl, tl, noise;
  625. uint8_t *cp;
  626. uint8_t tmp[24*1024];
  627. struct ieee80211req_sta_info *si;
  628. struct iwinfo_assoclist_entry entry;
  629. if( (tl = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
  630. {
  631. cp = tmp;
  632. bl = 0;
  633. if( madwifi_get_noise(ifname, &noise) )
  634. noise = 0;
  635. do {
  636. si = (struct ieee80211req_sta_info *) cp;
  637. memset(&entry, 0, sizeof(entry));
  638. entry.signal = (si->isi_rssi - 95);
  639. entry.noise = noise;
  640. memcpy(entry.mac, &si->isi_macaddr, 6);
  641. entry.inactive = si->isi_inact * 1000;
  642. entry.tx_packets = (si->isi_txseqs[0] & IEEE80211_SEQ_SEQ_MASK)
  643. >> IEEE80211_SEQ_SEQ_SHIFT;
  644. entry.rx_packets = (si->isi_rxseqs[0] & IEEE80211_SEQ_SEQ_MASK)
  645. >> IEEE80211_SEQ_SEQ_SHIFT;
  646. entry.tx_rate.rate =
  647. (si->isi_rates[si->isi_txrate] & IEEE80211_RATE_VAL) * 500;
  648. /* XXX: this is just a guess */
  649. entry.rx_rate.rate = entry.tx_rate.rate;
  650. entry.rx_rate.mcs = -1;
  651. entry.tx_rate.mcs = -1;
  652. memcpy(&buf[bl], &entry, sizeof(struct iwinfo_assoclist_entry));
  653. bl += sizeof(struct iwinfo_assoclist_entry);
  654. cp += si->isi_len;
  655. tl -= si->isi_len;
  656. } while (tl >= sizeof(struct ieee80211req_sta_info));
  657. *len = bl;
  658. return 0;
  659. }
  660. return -1;
  661. }
  662. static int madwifi_get_txpwrlist(const char *ifname, char *buf, int *len)
  663. {
  664. int rc = -1;
  665. char *res;
  666. /* A wifiX device? */
  667. if( madwifi_iswifi(ifname) )
  668. {
  669. if( (res = madwifi_ifadd(ifname)) != NULL )
  670. {
  671. rc = wext_ops.txpwrlist(res, buf, len);
  672. madwifi_ifdel(res);
  673. }
  674. }
  675. /* Its an athX ... */
  676. else if( !!madwifi_isvap(ifname, NULL) )
  677. {
  678. rc = wext_ops.txpwrlist(ifname, buf, len);
  679. }
  680. return rc;
  681. }
  682. static int madwifi_get_scanlist(const char *ifname, char *buf, int *len)
  683. {
  684. int ret;
  685. char *res;
  686. DIR *proc;
  687. struct dirent *e;
  688. ret = -1;
  689. /* We got a wifiX device passed, try to lookup a vap on it */
  690. if( madwifi_iswifi(ifname) )
  691. {
  692. if( (proc = opendir("/proc/sys/net/")) != NULL )
  693. {
  694. while( (e = readdir(proc)) != NULL )
  695. {
  696. if( !!madwifi_isvap(e->d_name, ifname) )
  697. {
  698. if( iwinfo_ifup(e->d_name) )
  699. {
  700. ret = wext_ops.scanlist(e->d_name, buf, len);
  701. break;
  702. }
  703. }
  704. }
  705. closedir(proc);
  706. }
  707. /* Still nothing found, try to create a vap */
  708. if( ret == -1 )
  709. {
  710. if( (res = madwifi_ifadd(ifname)) != NULL )
  711. {
  712. if( iwinfo_ifup(res) )
  713. {
  714. wext_ops.scanlist(res, buf, len);
  715. sleep(1);
  716. wext_ops.scanlist(res, buf, len);
  717. sleep(1);
  718. ret = wext_ops.scanlist(res, buf, len);
  719. }
  720. iwinfo_ifdown(res);
  721. madwifi_ifdel(res);
  722. }
  723. }
  724. }
  725. /* Got athX device? */
  726. else if( !!madwifi_isvap(ifname, NULL) )
  727. {
  728. ret = wext_ops.scanlist(ifname, buf, len);
  729. }
  730. return ret;
  731. }
  732. static int madwifi_get_freqlist(const char *ifname, char *buf, int *len)
  733. {
  734. int i, bl;
  735. int rc = -1;
  736. char *res;
  737. struct ieee80211req_chaninfo chans;
  738. struct iwinfo_freqlist_entry entry;
  739. /* A wifiX device? */
  740. if( madwifi_iswifi(ifname) )
  741. {
  742. if( (res = madwifi_ifadd(ifname)) != NULL )
  743. {
  744. rc = get80211priv(res, IEEE80211_IOCTL_GETCHANINFO,
  745. &chans, sizeof(chans));
  746. madwifi_ifdel(res);
  747. }
  748. }
  749. /* Its an athX ... */
  750. else if( !!madwifi_isvap(ifname, NULL) )
  751. {
  752. rc = get80211priv(ifname, IEEE80211_IOCTL_GETCHANINFO,
  753. &chans, sizeof(chans));
  754. }
  755. /* Got chaninfo? */
  756. if( rc >= 0 )
  757. {
  758. bl = 0;
  759. for( i = 0; i < chans.ic_nchans; i++ )
  760. {
  761. entry.mhz = chans.ic_chans[i].ic_freq;
  762. entry.channel = chans.ic_chans[i].ic_ieee;
  763. entry.restricted = 0;
  764. memcpy(&buf[bl], &entry, sizeof(struct iwinfo_freqlist_entry));
  765. bl += sizeof(struct iwinfo_freqlist_entry);
  766. }
  767. *len = bl;
  768. return 0;
  769. }
  770. return -1;
  771. }
  772. static int madwifi_get_country(const char *ifname, char *buf)
  773. {
  774. int i, fd, ccode = -1;
  775. char buffer[34];
  776. char *wifi = madwifi_iswifi(ifname)
  777. ? (char *)ifname : madwifi_isvap(ifname, NULL);
  778. struct ISO3166_to_CCode *e;
  779. if( wifi )
  780. {
  781. snprintf(buffer, sizeof(buffer), "/proc/sys/dev/%s/countrycode", wifi);
  782. if( (fd = open(buffer, O_RDONLY)) > -1 )
  783. {
  784. memset(buffer, 0, sizeof(buffer));
  785. if( read(fd, buffer, sizeof(buffer)-1) > 0 )
  786. ccode = atoi(buffer);
  787. close(fd);
  788. }
  789. }
  790. for( i = 0; i < (sizeof(CountryCodes)/sizeof(CountryCodes[0])); i++ )
  791. {
  792. e = &CountryCodes[i];
  793. if( e->ccode == ccode )
  794. {
  795. sprintf(buf, "%c%c", e->iso3166 / 256, e->iso3166 % 256);
  796. return 0;
  797. }
  798. }
  799. return -1;
  800. }
  801. static int madwifi_get_countrylist(const char *ifname, char *buf, int *len)
  802. {
  803. int i, count;
  804. struct ISO3166_to_CCode *e, *p = NULL;
  805. struct iwinfo_country_entry *c = (struct iwinfo_country_entry *)buf;
  806. count = 0;
  807. for( int i = 0; i < (sizeof(CountryCodes)/sizeof(CountryCodes[0])); i++ )
  808. {
  809. e = &CountryCodes[i];
  810. if( !p || (e->iso3166 != p->iso3166) )
  811. {
  812. c->iso3166 = e->iso3166;
  813. snprintf(c->ccode, sizeof(c->ccode), "%i", e->ccode);
  814. c++;
  815. count++;
  816. }
  817. p = e;
  818. }
  819. *len = (count * sizeof(struct iwinfo_country_entry));
  820. return 0;
  821. }
  822. static int madwifi_get_hwmodelist(const char *ifname, int *buf)
  823. {
  824. char chans[IWINFO_BUFSIZE] = { 0 };
  825. struct iwinfo_freqlist_entry *e = NULL;
  826. int len = 0;
  827. if( !madwifi_get_freqlist(ifname, chans, &len) )
  828. {
  829. for( e = (struct iwinfo_freqlist_entry *)chans; e->channel; e++ )
  830. {
  831. if( e->channel <= 14 )
  832. {
  833. *buf |= IWINFO_80211_B;
  834. *buf |= IWINFO_80211_G;
  835. }
  836. else
  837. {
  838. *buf |= IWINFO_80211_A;
  839. }
  840. }
  841. return 0;
  842. }
  843. return -1;
  844. }
  845. static int madwifi_get_htmodelist(const char *ifname, int *buf)
  846. {
  847. /* OpenWrt's madwifi did never support any HT rates */
  848. return -1;
  849. }
  850. static int madwifi_get_mbssid_support(const char *ifname, int *buf)
  851. {
  852. /* Test whether we can create another interface */
  853. char *nif = madwifi_ifadd(ifname);
  854. if( nif )
  855. {
  856. *buf = iwinfo_ifup(nif);
  857. iwinfo_ifdown(nif);
  858. madwifi_ifdel(nif);
  859. return 0;
  860. }
  861. return -1;
  862. }
  863. static int madwifi_get_hardware_id(const char *ifname, char *buf)
  864. {
  865. char vendor[64];
  866. char device[64];
  867. struct iwinfo_hardware_id *ids;
  868. struct iwinfo_hardware_entry *e;
  869. const char *phy = madwifi_phyname(ifname);
  870. if (wext_ops.hardware_id(phy, buf))
  871. return iwinfo_hardware_id_from_mtd((struct iwinfo_hardware_id *)buf);
  872. return 0;
  873. }
  874. static const struct iwinfo_hardware_entry *
  875. madwifi_get_hardware_entry(const char *ifname)
  876. {
  877. struct iwinfo_hardware_id id;
  878. if (madwifi_get_hardware_id(ifname, (char *)&id))
  879. return NULL;
  880. return iwinfo_hardware(&id);
  881. }
  882. static int madwifi_get_hardware_name(const char *ifname, char *buf)
  883. {
  884. const struct iwinfo_hardware_entry *hw;
  885. if (!(hw = madwifi_get_hardware_entry(ifname)))
  886. sprintf(buf, "Generic Atheros");
  887. else
  888. sprintf(buf, "%s %s", hw->vendor_name, hw->device_name);
  889. return 0;
  890. }
  891. static int madwifi_get_txpower_offset(const char *ifname, int *buf)
  892. {
  893. const struct iwinfo_hardware_entry *hw;
  894. if (!(hw = madwifi_get_hardware_entry(ifname)))
  895. return -1;
  896. *buf = hw->txpower_offset;
  897. return 0;
  898. }
  899. static int madwifi_get_frequency_offset(const char *ifname, int *buf)
  900. {
  901. const struct iwinfo_hardware_entry *hw;
  902. if (!(hw = madwifi_get_hardware_entry(ifname)))
  903. return -1;
  904. *buf = hw->frequency_offset;
  905. return 0;
  906. }
  907. const struct iwinfo_ops madwifi_ops = {
  908. .name = "madwifi",
  909. .probe = madwifi_probe,
  910. .channel = madwifi_get_channel,
  911. .frequency = madwifi_get_frequency,
  912. .frequency_offset = madwifi_get_frequency_offset,
  913. .txpower = madwifi_get_txpower,
  914. .txpower_offset = madwifi_get_txpower_offset,
  915. .bitrate = madwifi_get_bitrate,
  916. .signal = madwifi_get_signal,
  917. .noise = madwifi_get_noise,
  918. .quality = madwifi_get_quality,
  919. .quality_max = madwifi_get_quality_max,
  920. .mbssid_support = madwifi_get_mbssid_support,
  921. .hwmodelist = madwifi_get_hwmodelist,
  922. .htmodelist = madwifi_get_htmodelist,
  923. .mode = madwifi_get_mode,
  924. .ssid = madwifi_get_ssid,
  925. .bssid = madwifi_get_bssid,
  926. .country = madwifi_get_country,
  927. .hardware_id = madwifi_get_hardware_id,
  928. .hardware_name = madwifi_get_hardware_name,
  929. .encryption = madwifi_get_encryption,
  930. .phyname = madwifi_get_phyname,
  931. .assoclist = madwifi_get_assoclist,
  932. .txpwrlist = madwifi_get_txpwrlist,
  933. .scanlist = madwifi_get_scanlist,
  934. .freqlist = madwifi_get_freqlist,
  935. .countrylist = madwifi_get_countrylist,
  936. .close = madwifi_close
  937. };