iwinfo_madwifi.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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_center_chan1(const char *ifname, int *buf)
  351. {
  352. /* Not Supported */
  353. return -1;
  354. }
  355. static int madwifi_get_center_chan2(const char *ifname, int *buf)
  356. {
  357. /* Not Supported */
  358. return -1;
  359. }
  360. static int madwifi_get_frequency(const char *ifname, int *buf)
  361. {
  362. struct iwreq wrq;
  363. if( madwifi_wrq(&wrq, ifname, SIOCGIWFREQ, NULL, 0) >= 0 )
  364. {
  365. *buf = (uint16_t)(wrq.u.freq.m / 100000);
  366. return 0;
  367. }
  368. return -1;
  369. }
  370. static int madwifi_get_txpower(const char *ifname, int *buf)
  371. {
  372. return wext_ops.txpower(ifname, buf);
  373. }
  374. static int madwifi_get_bitrate(const char *ifname, int *buf)
  375. {
  376. unsigned int mode, len, rate, rate_count;
  377. uint8_t tmp[24*1024];
  378. uint8_t *cp;
  379. struct iwreq wrq;
  380. struct ieee80211req_sta_info *si;
  381. if( madwifi_wrq(&wrq, ifname, SIOCGIWMODE, NULL, 0) >= 0 )
  382. {
  383. mode = wrq.u.mode;
  384. /* Calculate bitrate average from associated stations in ad-hoc mode */
  385. if( mode == 1 )
  386. {
  387. rate = rate_count = 0;
  388. if( (len = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
  389. {
  390. cp = tmp;
  391. do {
  392. si = (struct ieee80211req_sta_info *) cp;
  393. if( si->isi_rssi > 0 )
  394. {
  395. rate_count++;
  396. rate += ((si->isi_rates[si->isi_txrate] & IEEE80211_RATE_VAL) / 2);
  397. }
  398. cp += si->isi_len;
  399. len -= si->isi_len;
  400. } while (len >= sizeof(struct ieee80211req_sta_info));
  401. }
  402. *buf = (rate == 0 || rate_count == 0) ? 0 : (rate / rate_count) * 1000;
  403. return 0;
  404. }
  405. /* Return whatever wext tells us ... */
  406. return wext_ops.bitrate(ifname, buf);
  407. }
  408. return -1;
  409. }
  410. static int madwifi_get_signal(const char *ifname, int *buf)
  411. {
  412. unsigned int mode, len, rssi, rssi_count;
  413. uint8_t tmp[24*1024];
  414. uint8_t *cp;
  415. struct iwreq wrq;
  416. struct ieee80211req_sta_info *si;
  417. if( madwifi_wrq(&wrq, ifname, SIOCGIWMODE, NULL, 0) >= 0 )
  418. {
  419. mode = wrq.u.mode;
  420. /* Calculate signal average from associated stations in ap or ad-hoc mode */
  421. if( mode == 1 )
  422. {
  423. rssi = rssi_count = 0;
  424. if( (len = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
  425. {
  426. cp = tmp;
  427. do {
  428. si = (struct ieee80211req_sta_info *) cp;
  429. if( si->isi_rssi > 0 )
  430. {
  431. rssi_count++;
  432. rssi -= (si->isi_rssi - 95);
  433. }
  434. cp += si->isi_len;
  435. len -= si->isi_len;
  436. } while (len >= sizeof(struct ieee80211req_sta_info));
  437. }
  438. *buf = (rssi == 0 || rssi_count == 0) ? 1 : -(rssi / rssi_count);
  439. return 0;
  440. }
  441. /* Return whatever wext tells us ... */
  442. return wext_ops.signal(ifname, buf);
  443. }
  444. return -1;
  445. }
  446. static int madwifi_get_noise(const char *ifname, int *buf)
  447. {
  448. return wext_ops.noise(ifname, buf);
  449. }
  450. static int madwifi_get_quality(const char *ifname, int *buf)
  451. {
  452. unsigned int mode, len, quality, quality_count;
  453. uint8_t tmp[24*1024];
  454. uint8_t *cp;
  455. struct iwreq wrq;
  456. struct ieee80211req_sta_info *si;
  457. if( madwifi_wrq(&wrq, ifname, SIOCGIWMODE, NULL, 0) >= 0 )
  458. {
  459. mode = wrq.u.mode;
  460. /* Calculate signal average from associated stations in ad-hoc mode */
  461. if( mode == 1 )
  462. {
  463. quality = quality_count = 0;
  464. if( (len = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
  465. {
  466. cp = tmp;
  467. do {
  468. si = (struct ieee80211req_sta_info *) cp;
  469. if( si->isi_rssi > 0 )
  470. {
  471. quality_count++;
  472. quality += si->isi_rssi;
  473. }
  474. cp += si->isi_len;
  475. len -= si->isi_len;
  476. } while (len >= sizeof(struct ieee80211req_sta_info));
  477. }
  478. *buf = (quality == 0 || quality_count == 0) ? 0 : (quality / quality_count);
  479. return 0;
  480. }
  481. /* Return whatever wext tells us ... */
  482. return wext_ops.quality(ifname, buf);
  483. }
  484. return -1;
  485. }
  486. static int madwifi_get_quality_max(const char *ifname, int *buf)
  487. {
  488. return wext_ops.quality_max(ifname, buf);
  489. }
  490. static int madwifi_get_encryption(const char *ifname, char *buf)
  491. {
  492. int ciphers = 0, key_len = 0;
  493. char keybuf[IW_ENCODING_TOKEN_MAX];
  494. struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf;
  495. struct iwreq wrq;
  496. struct ieee80211req_key wk;
  497. memset(&wrq, 0, sizeof(wrq));
  498. /* Obtain key info */
  499. if( madwifi_wrq(&wrq, ifname, SIOCGIWENCODE, keybuf, sizeof(keybuf)) < 0 )
  500. return -1;
  501. #if 0
  502. /* Have any encryption? */
  503. if( (wrq.u.data.flags & IW_ENCODE_DISABLED) || (wrq.u.data.length == 0) )
  504. return 0;
  505. #endif
  506. /* Save key len */
  507. key_len = wrq.u.data.length;
  508. /* Get wpa protocol version */
  509. wrq.u.mode = IEEE80211_PARAM_WPA;
  510. if( madwifi_wrq(&wrq, ifname, IEEE80211_IOCTL_GETPARAM, NULL, 0) >= 0 )
  511. c->wpa_version = wrq.u.mode;
  512. /* Get authentication suites */
  513. wrq.u.mode = IEEE80211_PARAM_AUTHMODE;
  514. if( madwifi_wrq(&wrq, ifname, IEEE80211_IOCTL_GETPARAM, NULL, 0) >= 0 )
  515. {
  516. switch(wrq.u.mode) {
  517. case IEEE80211_AUTH_8021X:
  518. c->auth_suites |= IWINFO_KMGMT_8021x;
  519. break;
  520. case IEEE80211_AUTH_WPA:
  521. c->auth_suites |= IWINFO_KMGMT_PSK;
  522. break;
  523. case IEEE80211_AUTH_OPEN:
  524. c->auth_algs |= IWINFO_AUTH_OPEN;
  525. break;
  526. case IEEE80211_AUTH_SHARED:
  527. c->auth_algs |= IWINFO_AUTH_SHARED;
  528. break;
  529. default:
  530. c->auth_suites |= IWINFO_KMGMT_NONE;
  531. break;
  532. }
  533. }
  534. memset(&wk, 0, sizeof(wk));
  535. memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
  536. /* Get key information */
  537. if( get80211priv(ifname, IEEE80211_IOCTL_GETKEY, &wk, sizeof(wk)) >= 0 )
  538. {
  539. /* Type 0 == WEP */
  540. if( (wk.ik_type == 0) && (c->auth_algs == 0) )
  541. c->auth_algs = (IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED);
  542. }
  543. /* Get used pairwise ciphers */
  544. wrq.u.mode = IEEE80211_PARAM_UCASTCIPHERS;
  545. if( madwifi_wrq(&wrq, ifname, IEEE80211_IOCTL_GETPARAM, NULL, 0) >= 0 )
  546. {
  547. ciphers = wrq.u.mode;
  548. if( c->wpa_version && ciphers & (1 << IEEE80211_CIPHER_TKIP) )
  549. c->pair_ciphers |= IWINFO_CIPHER_TKIP;
  550. if( c->wpa_version && ciphers & (1 << IEEE80211_CIPHER_AES_CCM) )
  551. c->pair_ciphers |= IWINFO_CIPHER_CCMP;
  552. if( c->wpa_version && ciphers & (1 << IEEE80211_CIPHER_AES_OCB) )
  553. c->pair_ciphers |= IWINFO_CIPHER_AESOCB;
  554. if( c->wpa_version && ciphers & (1 << IEEE80211_CIPHER_CKIP) )
  555. c->pair_ciphers |= IWINFO_CIPHER_CKIP;
  556. if( !c->pair_ciphers && ciphers & (1 << IEEE80211_CIPHER_WEP) )
  557. {
  558. switch(key_len) {
  559. case 13:
  560. c->pair_ciphers |= IWINFO_CIPHER_WEP104;
  561. break;
  562. case 5:
  563. c->pair_ciphers |= IWINFO_CIPHER_WEP40;
  564. break;
  565. case 0:
  566. break;
  567. default:
  568. c->pair_ciphers = IWINFO_CIPHER_WEP40 |
  569. IWINFO_CIPHER_WEP104;
  570. break;
  571. }
  572. }
  573. if( ciphers & (1 << IEEE80211_CIPHER_NONE) )
  574. c->pair_ciphers |= IWINFO_CIPHER_NONE;
  575. }
  576. /* Get used group cipher */
  577. wrq.u.mode = IEEE80211_PARAM_MCASTCIPHER;
  578. if( madwifi_wrq(&wrq, ifname, IEEE80211_IOCTL_GETPARAM, NULL, 0) >= 0 )
  579. {
  580. ciphers = c->wpa_version ? wrq.u.mode : IEEE80211_CIPHER_WEP;
  581. switch(ciphers) {
  582. case IEEE80211_CIPHER_TKIP:
  583. c->group_ciphers |= IWINFO_CIPHER_TKIP;
  584. break;
  585. case IEEE80211_CIPHER_AES_CCM:
  586. c->group_ciphers |= IWINFO_CIPHER_CCMP;
  587. break;
  588. case IEEE80211_CIPHER_AES_OCB:
  589. c->group_ciphers |= IWINFO_CIPHER_AESOCB;
  590. break;
  591. case IEEE80211_CIPHER_CKIP:
  592. c->group_ciphers |= IWINFO_CIPHER_CKIP;
  593. break;
  594. case IEEE80211_CIPHER_WEP:
  595. switch(key_len) {
  596. case 13:
  597. c->group_ciphers |= IWINFO_CIPHER_WEP104;
  598. break;
  599. case 5:
  600. c->group_ciphers |= IWINFO_CIPHER_WEP40;
  601. break;
  602. default:
  603. break;
  604. }
  605. break;
  606. case IEEE80211_CIPHER_NONE:
  607. c->group_ciphers |= IWINFO_CIPHER_NONE;
  608. break;
  609. default:
  610. break;
  611. }
  612. }
  613. c->enabled = (c->wpa_version || (c->auth_algs && c->pair_ciphers)) ? 1 : 0;
  614. return 0;
  615. }
  616. static int madwifi_get_phyname(const char *ifname, char *buf)
  617. {
  618. const char *wifidev;
  619. wifidev = madwifi_isvap(ifname, NULL);
  620. if (wifidev)
  621. {
  622. strcpy(buf, wifidev);
  623. return 0;
  624. }
  625. else if (madwifi_iswifi(ifname))
  626. {
  627. strcpy(buf, madwifi_phyname(ifname));
  628. return 0;
  629. }
  630. return -1;
  631. }
  632. static int madwifi_get_assoclist(const char *ifname, char *buf, int *len)
  633. {
  634. int bl, tl, noise;
  635. uint8_t *cp;
  636. uint8_t tmp[24*1024];
  637. struct ieee80211req_sta_info *si;
  638. struct iwinfo_assoclist_entry entry;
  639. if( (tl = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
  640. {
  641. cp = tmp;
  642. bl = 0;
  643. if( madwifi_get_noise(ifname, &noise) )
  644. noise = 0;
  645. do {
  646. si = (struct ieee80211req_sta_info *) cp;
  647. memset(&entry, 0, sizeof(entry));
  648. entry.signal = (si->isi_rssi - 95);
  649. entry.noise = noise;
  650. memcpy(entry.mac, &si->isi_macaddr, 6);
  651. entry.inactive = si->isi_inact * 1000;
  652. entry.tx_packets = (si->isi_txseqs[0] & IEEE80211_SEQ_SEQ_MASK)
  653. >> IEEE80211_SEQ_SEQ_SHIFT;
  654. entry.rx_packets = (si->isi_rxseqs[0] & IEEE80211_SEQ_SEQ_MASK)
  655. >> IEEE80211_SEQ_SEQ_SHIFT;
  656. entry.tx_rate.rate =
  657. (si->isi_rates[si->isi_txrate] & IEEE80211_RATE_VAL) * 500;
  658. /* XXX: this is just a guess */
  659. entry.rx_rate.rate = entry.tx_rate.rate;
  660. entry.rx_rate.mcs = -1;
  661. entry.tx_rate.mcs = -1;
  662. memcpy(&buf[bl], &entry, sizeof(struct iwinfo_assoclist_entry));
  663. bl += sizeof(struct iwinfo_assoclist_entry);
  664. cp += si->isi_len;
  665. tl -= si->isi_len;
  666. } while (tl >= sizeof(struct ieee80211req_sta_info));
  667. *len = bl;
  668. return 0;
  669. }
  670. return -1;
  671. }
  672. static int madwifi_get_txpwrlist(const char *ifname, char *buf, int *len)
  673. {
  674. int rc = -1;
  675. char *res;
  676. /* A wifiX device? */
  677. if( madwifi_iswifi(ifname) )
  678. {
  679. if( (res = madwifi_ifadd(ifname)) != NULL )
  680. {
  681. rc = wext_ops.txpwrlist(res, buf, len);
  682. madwifi_ifdel(res);
  683. }
  684. }
  685. /* Its an athX ... */
  686. else if( !!madwifi_isvap(ifname, NULL) )
  687. {
  688. rc = wext_ops.txpwrlist(ifname, buf, len);
  689. }
  690. return rc;
  691. }
  692. static int madwifi_get_scanlist(const char *ifname, char *buf, int *len)
  693. {
  694. int ret;
  695. char *res;
  696. DIR *proc;
  697. struct dirent *e;
  698. ret = -1;
  699. /* We got a wifiX device passed, try to lookup a vap on it */
  700. if( madwifi_iswifi(ifname) )
  701. {
  702. if( (proc = opendir("/proc/sys/net/")) != NULL )
  703. {
  704. while( (e = readdir(proc)) != NULL )
  705. {
  706. if( !!madwifi_isvap(e->d_name, ifname) )
  707. {
  708. if( iwinfo_ifup(e->d_name) )
  709. {
  710. ret = wext_ops.scanlist(e->d_name, buf, len);
  711. break;
  712. }
  713. }
  714. }
  715. closedir(proc);
  716. }
  717. /* Still nothing found, try to create a vap */
  718. if( ret == -1 )
  719. {
  720. if( (res = madwifi_ifadd(ifname)) != NULL )
  721. {
  722. if( iwinfo_ifup(res) )
  723. {
  724. wext_ops.scanlist(res, buf, len);
  725. sleep(1);
  726. wext_ops.scanlist(res, buf, len);
  727. sleep(1);
  728. ret = wext_ops.scanlist(res, buf, len);
  729. }
  730. iwinfo_ifdown(res);
  731. madwifi_ifdel(res);
  732. }
  733. }
  734. }
  735. /* Got athX device? */
  736. else if( !!madwifi_isvap(ifname, NULL) )
  737. {
  738. ret = wext_ops.scanlist(ifname, buf, len);
  739. }
  740. return ret;
  741. }
  742. static int madwifi_get_freqlist(const char *ifname, char *buf, int *len)
  743. {
  744. int i, bl;
  745. int rc = -1;
  746. char *res;
  747. struct ieee80211req_chaninfo chans;
  748. struct iwinfo_freqlist_entry entry;
  749. /* A wifiX device? */
  750. if( madwifi_iswifi(ifname) )
  751. {
  752. if( (res = madwifi_ifadd(ifname)) != NULL )
  753. {
  754. rc = get80211priv(res, IEEE80211_IOCTL_GETCHANINFO,
  755. &chans, sizeof(chans));
  756. madwifi_ifdel(res);
  757. }
  758. }
  759. /* Its an athX ... */
  760. else if( !!madwifi_isvap(ifname, NULL) )
  761. {
  762. rc = get80211priv(ifname, IEEE80211_IOCTL_GETCHANINFO,
  763. &chans, sizeof(chans));
  764. }
  765. /* Got chaninfo? */
  766. if( rc >= 0 )
  767. {
  768. bl = 0;
  769. for( i = 0; i < chans.ic_nchans; i++ )
  770. {
  771. entry.mhz = chans.ic_chans[i].ic_freq;
  772. entry.channel = chans.ic_chans[i].ic_ieee;
  773. entry.restricted = 0;
  774. memcpy(&buf[bl], &entry, sizeof(struct iwinfo_freqlist_entry));
  775. bl += sizeof(struct iwinfo_freqlist_entry);
  776. }
  777. *len = bl;
  778. return 0;
  779. }
  780. return -1;
  781. }
  782. static int madwifi_get_country(const char *ifname, char *buf)
  783. {
  784. int i, fd, ccode = -1;
  785. char buffer[34];
  786. char *wifi = madwifi_iswifi(ifname)
  787. ? (char *)ifname : madwifi_isvap(ifname, NULL);
  788. struct ISO3166_to_CCode *e;
  789. if( wifi )
  790. {
  791. snprintf(buffer, sizeof(buffer), "/proc/sys/dev/%s/countrycode", wifi);
  792. if( (fd = open(buffer, O_RDONLY)) > -1 )
  793. {
  794. memset(buffer, 0, sizeof(buffer));
  795. if( read(fd, buffer, sizeof(buffer)-1) > 0 )
  796. ccode = atoi(buffer);
  797. close(fd);
  798. }
  799. }
  800. for( i = 0; i < (sizeof(CountryCodes)/sizeof(CountryCodes[0])); i++ )
  801. {
  802. e = &CountryCodes[i];
  803. if( e->ccode == ccode )
  804. {
  805. sprintf(buf, "%c%c", e->iso3166 / 256, e->iso3166 % 256);
  806. return 0;
  807. }
  808. }
  809. return -1;
  810. }
  811. static int madwifi_get_countrylist(const char *ifname, char *buf, int *len)
  812. {
  813. int i, count;
  814. struct ISO3166_to_CCode *e, *p = NULL;
  815. struct iwinfo_country_entry *c = (struct iwinfo_country_entry *)buf;
  816. count = 0;
  817. for( int i = 0; i < (sizeof(CountryCodes)/sizeof(CountryCodes[0])); i++ )
  818. {
  819. e = &CountryCodes[i];
  820. if( !p || (e->iso3166 != p->iso3166) )
  821. {
  822. c->iso3166 = e->iso3166;
  823. snprintf(c->ccode, sizeof(c->ccode), "%i", e->ccode);
  824. c++;
  825. count++;
  826. }
  827. p = e;
  828. }
  829. *len = (count * sizeof(struct iwinfo_country_entry));
  830. return 0;
  831. }
  832. static int madwifi_get_hwmodelist(const char *ifname, int *buf)
  833. {
  834. char chans[IWINFO_BUFSIZE] = { 0 };
  835. struct iwinfo_freqlist_entry *e = NULL;
  836. int len = 0;
  837. if( !madwifi_get_freqlist(ifname, chans, &len) )
  838. {
  839. for( e = (struct iwinfo_freqlist_entry *)chans; e->channel; e++ )
  840. {
  841. if( e->channel <= 14 )
  842. {
  843. *buf |= IWINFO_80211_B;
  844. *buf |= IWINFO_80211_G;
  845. }
  846. else
  847. {
  848. *buf |= IWINFO_80211_A;
  849. }
  850. }
  851. return 0;
  852. }
  853. return -1;
  854. }
  855. static int madwifi_get_htmodelist(const char *ifname, int *buf)
  856. {
  857. /* OpenWrt's madwifi did never support any HT rates */
  858. return -1;
  859. }
  860. static int madwifi_get_mbssid_support(const char *ifname, int *buf)
  861. {
  862. /* Test whether we can create another interface */
  863. char *nif = madwifi_ifadd(ifname);
  864. if( nif )
  865. {
  866. *buf = iwinfo_ifup(nif);
  867. iwinfo_ifdown(nif);
  868. madwifi_ifdel(nif);
  869. return 0;
  870. }
  871. return -1;
  872. }
  873. static int madwifi_get_hardware_id(const char *ifname, char *buf)
  874. {
  875. char vendor[64];
  876. char device[64];
  877. struct iwinfo_hardware_id *ids;
  878. struct iwinfo_hardware_entry *e;
  879. const char *phy = madwifi_phyname(ifname);
  880. if (wext_ops.hardware_id(phy, buf))
  881. return iwinfo_hardware_id_from_mtd((struct iwinfo_hardware_id *)buf);
  882. return 0;
  883. }
  884. static const struct iwinfo_hardware_entry *
  885. madwifi_get_hardware_entry(const char *ifname)
  886. {
  887. struct iwinfo_hardware_id id;
  888. if (madwifi_get_hardware_id(ifname, (char *)&id))
  889. return NULL;
  890. return iwinfo_hardware(&id);
  891. }
  892. static int madwifi_get_hardware_name(const char *ifname, char *buf)
  893. {
  894. const struct iwinfo_hardware_entry *hw;
  895. if (!(hw = madwifi_get_hardware_entry(ifname)))
  896. sprintf(buf, "Generic Atheros");
  897. else
  898. sprintf(buf, "%s %s", hw->vendor_name, hw->device_name);
  899. return 0;
  900. }
  901. static int madwifi_get_txpower_offset(const char *ifname, int *buf)
  902. {
  903. const struct iwinfo_hardware_entry *hw;
  904. if (!(hw = madwifi_get_hardware_entry(ifname)))
  905. return -1;
  906. *buf = hw->txpower_offset;
  907. return 0;
  908. }
  909. static int madwifi_get_frequency_offset(const char *ifname, int *buf)
  910. {
  911. const struct iwinfo_hardware_entry *hw;
  912. if (!(hw = madwifi_get_hardware_entry(ifname)))
  913. return -1;
  914. *buf = hw->frequency_offset;
  915. return 0;
  916. }
  917. const struct iwinfo_ops madwifi_ops = {
  918. .name = "madwifi",
  919. .probe = madwifi_probe,
  920. .channel = madwifi_get_channel,
  921. .center_chan1 = madwifi_get_center_chan1,
  922. .center_chan2 = madwifi_get_center_chan2,
  923. .frequency = madwifi_get_frequency,
  924. .frequency_offset = madwifi_get_frequency_offset,
  925. .txpower = madwifi_get_txpower,
  926. .txpower_offset = madwifi_get_txpower_offset,
  927. .bitrate = madwifi_get_bitrate,
  928. .signal = madwifi_get_signal,
  929. .noise = madwifi_get_noise,
  930. .quality = madwifi_get_quality,
  931. .quality_max = madwifi_get_quality_max,
  932. .mbssid_support = madwifi_get_mbssid_support,
  933. .hwmodelist = madwifi_get_hwmodelist,
  934. .htmodelist = madwifi_get_htmodelist,
  935. .mode = madwifi_get_mode,
  936. .ssid = madwifi_get_ssid,
  937. .bssid = madwifi_get_bssid,
  938. .country = madwifi_get_country,
  939. .hardware_id = madwifi_get_hardware_id,
  940. .hardware_name = madwifi_get_hardware_name,
  941. .encryption = madwifi_get_encryption,
  942. .phyname = madwifi_get_phyname,
  943. .assoclist = madwifi_get_assoclist,
  944. .txpwrlist = madwifi_get_txpwrlist,
  945. .scanlist = madwifi_get_scanlist,
  946. .freqlist = madwifi_get_freqlist,
  947. .countrylist = madwifi_get_countrylist,
  948. .close = madwifi_close
  949. };