iwinfo_wext.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * iwinfo - Wireless Information Library - Linux Wireless Extension Backend
  3. *
  4. * Copyright (C) 2009 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. * Parts of this code are derived from the Linux wireless tools, iwlib.c,
  19. * iwlist.c and iwconfig.c in particular.
  20. */
  21. #include "iwinfo.h"
  22. #include "iwinfo_wext.h"
  23. static double wext_freq2float(const struct iw_freq *in)
  24. {
  25. int i;
  26. double res = (double) in->m;
  27. for(i = 0; i < in->e; i++) res *= 10;
  28. return res;
  29. }
  30. static inline int wext_freq2mhz(const struct iw_freq *in)
  31. {
  32. if( in->e == 6 )
  33. {
  34. return in->m;
  35. }
  36. else
  37. {
  38. return (int)(wext_freq2float(in) / 1000000);
  39. }
  40. }
  41. static inline int wext_ioctl(const char *ifname, int cmd, struct iwreq *wrq)
  42. {
  43. if( !strncmp(ifname, "mon.", 4) )
  44. strncpy(wrq->ifr_name, &ifname[4], IFNAMSIZ);
  45. else
  46. strncpy(wrq->ifr_name, ifname, IFNAMSIZ);
  47. return iwinfo_ioctl(cmd, wrq);
  48. }
  49. static int wext_probe(const char *ifname)
  50. {
  51. struct iwreq wrq;
  52. if(wext_ioctl(ifname, SIOCGIWNAME, &wrq) >= 0)
  53. return 1;
  54. return 0;
  55. }
  56. static void wext_close(void)
  57. {
  58. /* Nop */
  59. }
  60. static int wext_get_mode(const char *ifname, int *buf)
  61. {
  62. struct iwreq wrq;
  63. if(wext_ioctl(ifname, SIOCGIWMODE, &wrq) >= 0)
  64. {
  65. switch(wrq.u.mode)
  66. {
  67. case 1:
  68. *buf = IWINFO_OPMODE_ADHOC;
  69. break;
  70. case 2:
  71. *buf = IWINFO_OPMODE_CLIENT;
  72. break;
  73. case 3:
  74. *buf = IWINFO_OPMODE_MASTER;
  75. break;
  76. case 6:
  77. *buf = IWINFO_OPMODE_MONITOR;
  78. break;
  79. default:
  80. *buf = IWINFO_OPMODE_UNKNOWN;
  81. break;
  82. }
  83. return 0;
  84. }
  85. return -1;
  86. }
  87. static int wext_get_ssid(const char *ifname, char *buf)
  88. {
  89. struct iwreq wrq;
  90. wrq.u.essid.pointer = (caddr_t) buf;
  91. wrq.u.essid.length = IW_ESSID_MAX_SIZE + 1;
  92. wrq.u.essid.flags = 0;
  93. if(wext_ioctl(ifname, SIOCGIWESSID, &wrq) >= 0)
  94. return 0;
  95. return -1;
  96. }
  97. static int wext_get_bssid(const char *ifname, char *buf)
  98. {
  99. struct iwreq wrq;
  100. if(wext_ioctl(ifname, SIOCGIWAP, &wrq) >= 0)
  101. {
  102. sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
  103. (uint8_t)wrq.u.ap_addr.sa_data[0], (uint8_t)wrq.u.ap_addr.sa_data[1],
  104. (uint8_t)wrq.u.ap_addr.sa_data[2], (uint8_t)wrq.u.ap_addr.sa_data[3],
  105. (uint8_t)wrq.u.ap_addr.sa_data[4], (uint8_t)wrq.u.ap_addr.sa_data[5]);
  106. return 0;
  107. }
  108. return -1;
  109. }
  110. static int wext_get_bitrate(const char *ifname, int *buf)
  111. {
  112. struct iwreq wrq;
  113. if(wext_ioctl(ifname, SIOCGIWRATE, &wrq) >= 0)
  114. {
  115. *buf = (wrq.u.bitrate.value / 1000);
  116. return 0;
  117. }
  118. return -1;
  119. }
  120. static int wext_get_channel(const char *ifname, int *buf)
  121. {
  122. struct iwreq wrq;
  123. struct iw_range range;
  124. double freq;
  125. int i;
  126. if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
  127. {
  128. if( wrq.u.freq.m >= 1000 )
  129. {
  130. freq = wext_freq2float(&wrq.u.freq);
  131. wrq.u.data.pointer = (caddr_t) &range;
  132. wrq.u.data.length = sizeof(struct iw_range);
  133. wrq.u.data.flags = 0;
  134. if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
  135. {
  136. for(i = 0; i < range.num_frequency; i++)
  137. {
  138. if( wext_freq2float(&range.freq[i]) == freq )
  139. {
  140. *buf = range.freq[i].i;
  141. return 0;
  142. }
  143. }
  144. }
  145. }
  146. else
  147. {
  148. *buf = wrq.u.freq.m;
  149. return 0;
  150. }
  151. }
  152. return -1;
  153. }
  154. static int wext_get_frequency(const char *ifname, int *buf)
  155. {
  156. struct iwreq wrq;
  157. struct iw_range range;
  158. int i, channel;
  159. if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
  160. {
  161. /* We got a channel number instead ... */
  162. if( wrq.u.freq.m < 1000 )
  163. {
  164. channel = wrq.u.freq.m;
  165. wrq.u.data.pointer = (caddr_t) &range;
  166. wrq.u.data.length = sizeof(struct iw_range);
  167. wrq.u.data.flags = 0;
  168. if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
  169. {
  170. for(i = 0; i < range.num_frequency; i++)
  171. {
  172. if( range.freq[i].i == channel )
  173. {
  174. *buf = wext_freq2mhz(&range.freq[i]);
  175. return 0;
  176. }
  177. }
  178. }
  179. }
  180. else
  181. {
  182. *buf = wext_freq2mhz(&wrq.u.freq);
  183. return 0;
  184. }
  185. }
  186. return -1;
  187. }
  188. static int wext_get_txpower(const char *ifname, int *buf)
  189. {
  190. struct iwreq wrq;
  191. wrq.u.txpower.flags = 0;
  192. if(wext_ioctl(ifname, SIOCGIWTXPOW, &wrq) >= 0)
  193. {
  194. if(wrq.u.txpower.flags & IW_TXPOW_MWATT)
  195. *buf = iwinfo_mw2dbm(wrq.u.txpower.value);
  196. else
  197. *buf = wrq.u.txpower.value;
  198. return 0;
  199. }
  200. return -1;
  201. }
  202. static int wext_get_signal(const char *ifname, int *buf)
  203. {
  204. struct iwreq wrq;
  205. struct iw_statistics stats;
  206. wrq.u.data.pointer = (caddr_t) &stats;
  207. wrq.u.data.length = sizeof(struct iw_statistics);
  208. wrq.u.data.flags = 1;
  209. if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
  210. {
  211. *buf = (stats.qual.updated & IW_QUAL_DBM)
  212. ? (stats.qual.level - 0x100) : stats.qual.level;
  213. return 0;
  214. }
  215. return -1;
  216. }
  217. static int wext_get_noise(const char *ifname, int *buf)
  218. {
  219. struct iwreq wrq;
  220. struct iw_statistics stats;
  221. wrq.u.data.pointer = (caddr_t) &stats;
  222. wrq.u.data.length = sizeof(struct iw_statistics);
  223. wrq.u.data.flags = 1;
  224. if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
  225. {
  226. *buf = (stats.qual.updated & IW_QUAL_DBM)
  227. ? (stats.qual.noise - 0x100) : stats.qual.noise;
  228. return 0;
  229. }
  230. return -1;
  231. }
  232. static int wext_get_quality(const char *ifname, int *buf)
  233. {
  234. struct iwreq wrq;
  235. struct iw_statistics stats;
  236. wrq.u.data.pointer = (caddr_t) &stats;
  237. wrq.u.data.length = sizeof(struct iw_statistics);
  238. wrq.u.data.flags = 1;
  239. if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
  240. {
  241. *buf = stats.qual.qual;
  242. return 0;
  243. }
  244. return -1;
  245. }
  246. static int wext_get_quality_max(const char *ifname, int *buf)
  247. {
  248. struct iwreq wrq;
  249. struct iw_range range;
  250. wrq.u.data.pointer = (caddr_t) &range;
  251. wrq.u.data.length = sizeof(struct iw_range);
  252. wrq.u.data.flags = 0;
  253. if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
  254. {
  255. *buf = range.max_qual.qual;
  256. return 0;
  257. }
  258. return -1;
  259. }
  260. static int wext_get_assoclist(const char *ifname, char *buf, int *len)
  261. {
  262. /* Stub */
  263. return -1;
  264. }
  265. static int wext_get_txpwrlist(const char *ifname, char *buf, int *len)
  266. {
  267. struct iwreq wrq;
  268. struct iw_range range;
  269. struct iwinfo_txpwrlist_entry entry;
  270. int i;
  271. wrq.u.data.pointer = (caddr_t) &range;
  272. wrq.u.data.length = sizeof(struct iw_range);
  273. wrq.u.data.flags = 0;
  274. if( (wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0) &&
  275. (range.num_txpower > 0) && (range.num_txpower <= IW_MAX_TXPOWER) &&
  276. !(range.txpower_capa & IW_TXPOW_RELATIVE)
  277. ) {
  278. for( i = 0; i < range.num_txpower; i++ )
  279. {
  280. if( range.txpower_capa & IW_TXPOW_MWATT )
  281. {
  282. entry.dbm = iwinfo_mw2dbm(range.txpower[i]);
  283. entry.mw = range.txpower[i];
  284. }
  285. /* Madwifi does neither set mW not dBm caps, also iwlist assumes
  286. * dBm if mW is not set, so don't check here... */
  287. else /* if( range.txpower_capa & IW_TXPOW_DBM ) */
  288. {
  289. entry.dbm = range.txpower[i];
  290. entry.mw = iwinfo_dbm2mw(range.txpower[i]);
  291. }
  292. memcpy(&buf[i*sizeof(entry)], &entry, sizeof(entry));
  293. }
  294. *len = i * sizeof(entry);
  295. return 0;
  296. }
  297. return -1;
  298. }
  299. static int wext_get_freqlist(const char *ifname, char *buf, int *len)
  300. {
  301. struct iwreq wrq;
  302. struct iw_range range;
  303. struct iwinfo_freqlist_entry entry;
  304. int i, bl;
  305. wrq.u.data.pointer = (caddr_t) &range;
  306. wrq.u.data.length = sizeof(struct iw_range);
  307. wrq.u.data.flags = 0;
  308. if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
  309. {
  310. bl = 0;
  311. for(i = 0; i < range.num_frequency; i++)
  312. {
  313. entry.mhz = wext_freq2mhz(&range.freq[i]);
  314. entry.channel = range.freq[i].i;
  315. entry.restricted = 0;
  316. memcpy(&buf[bl], &entry, sizeof(struct iwinfo_freqlist_entry));
  317. bl += sizeof(struct iwinfo_freqlist_entry);
  318. }
  319. *len = bl;
  320. return 0;
  321. }
  322. return -1;
  323. }
  324. static int wext_get_country(const char *ifname, char *buf)
  325. {
  326. sprintf(buf, "00");
  327. return 0;
  328. }
  329. static int wext_get_countrylist(const char *ifname, char *buf, int *len)
  330. {
  331. /* Stub */
  332. return -1;
  333. }
  334. static int wext_get_hwmodelist(const char *ifname, int *buf)
  335. {
  336. char chans[IWINFO_BUFSIZE] = { 0 };
  337. struct iwinfo_freqlist_entry *e = NULL;
  338. int len = 0;
  339. *buf = 0;
  340. if( !wext_get_freqlist(ifname, chans, &len) )
  341. {
  342. for( e = (struct iwinfo_freqlist_entry *)chans; e->channel; e++ )
  343. {
  344. if( e->channel <= 14 )
  345. {
  346. *buf |= IWINFO_80211_B;
  347. *buf |= IWINFO_80211_G;
  348. }
  349. else
  350. {
  351. *buf |= IWINFO_80211_A;
  352. }
  353. }
  354. return 0;
  355. }
  356. return -1;
  357. }
  358. static int wext_get_htmodelist(const char *ifname, int *buf)
  359. {
  360. /* Stub */
  361. return -1;
  362. }
  363. static int wext_get_encryption(const char *ifname, char *buf)
  364. {
  365. /* No reliable crypto info in wext */
  366. return -1;
  367. }
  368. static int wext_get_phyname(const char *ifname, char *buf)
  369. {
  370. /* No suitable api in wext */
  371. strcpy(buf, ifname);
  372. return 0;
  373. }
  374. static int wext_get_mbssid_support(const char *ifname, int *buf)
  375. {
  376. /* No multi bssid support atm */
  377. return -1;
  378. }
  379. static char * wext_sysfs_ifname_file(const char *ifname, const char *path)
  380. {
  381. FILE *f;
  382. static char buf[128];
  383. char *rv = NULL;
  384. snprintf(buf, sizeof(buf), "/sys/class/net/%s/%s", ifname, path);
  385. if ((f = fopen(buf, "r")) != NULL)
  386. {
  387. memset(buf, 0, sizeof(buf));
  388. if (fread(buf, 1, sizeof(buf), f))
  389. rv = buf;
  390. fclose(f);
  391. }
  392. return rv;
  393. }
  394. static int wext_get_hardware_id(const char *ifname, char *buf)
  395. {
  396. char *data;
  397. struct iwinfo_hardware_id *id = (struct iwinfo_hardware_id *)buf;
  398. memset(id, 0, sizeof(struct iwinfo_hardware_id));
  399. data = wext_sysfs_ifname_file(ifname, "device/vendor");
  400. if (data)
  401. id->vendor_id = strtoul(data, NULL, 16);
  402. data = wext_sysfs_ifname_file(ifname, "device/device");
  403. if (data)
  404. id->device_id = strtoul(data, NULL, 16);
  405. data = wext_sysfs_ifname_file(ifname, "device/subsystem_device");
  406. if (data)
  407. id->subsystem_device_id = strtoul(data, NULL, 16);
  408. data = wext_sysfs_ifname_file(ifname, "device/subsystem_vendor");
  409. if (data)
  410. id->subsystem_vendor_id = strtoul(data, NULL, 16);
  411. return (id->vendor_id > 0 && id->device_id > 0) ? 0 : -1;
  412. }
  413. static int wext_get_hardware_name(const char *ifname, char *buf)
  414. {
  415. sprintf(buf, "Generic WEXT");
  416. return 0;
  417. }
  418. static int wext_get_txpower_offset(const char *ifname, int *buf)
  419. {
  420. /* Stub */
  421. *buf = 0;
  422. return -1;
  423. }
  424. static int wext_get_frequency_offset(const char *ifname, int *buf)
  425. {
  426. /* Stub */
  427. *buf = 0;
  428. return -1;
  429. }
  430. const struct iwinfo_ops wext_ops = {
  431. .name = "wext",
  432. .probe = wext_probe,
  433. .channel = wext_get_channel,
  434. .frequency = wext_get_frequency,
  435. .frequency_offset = wext_get_frequency_offset,
  436. .txpower = wext_get_txpower,
  437. .txpower_offset = wext_get_txpower_offset,
  438. .bitrate = wext_get_bitrate,
  439. .signal = wext_get_signal,
  440. .noise = wext_get_noise,
  441. .quality = wext_get_quality,
  442. .quality_max = wext_get_quality_max,
  443. .mbssid_support = wext_get_mbssid_support,
  444. .hwmodelist = wext_get_hwmodelist,
  445. .htmodelist = wext_get_htmodelist,
  446. .mode = wext_get_mode,
  447. .ssid = wext_get_ssid,
  448. .bssid = wext_get_bssid,
  449. .country = wext_get_country,
  450. .hardware_id = wext_get_hardware_id,
  451. .hardware_name = wext_get_hardware_name,
  452. .encryption = wext_get_encryption,
  453. .phyname = wext_get_phyname,
  454. .assoclist = wext_get_assoclist,
  455. .txpwrlist = wext_get_txpwrlist,
  456. .scanlist = wext_get_scanlist,
  457. .freqlist = wext_get_freqlist,
  458. .countrylist = wext_get_countrylist,
  459. .close = wext_close
  460. };