iwinfo_wext.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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_center_chan1(const char *ifname, int *buf)
  155. {
  156. /* Not Supported */
  157. return -1;
  158. }
  159. static int wext_get_center_chan2(const char *ifname, int *buf)
  160. {
  161. /* Not Supported */
  162. return -1;
  163. }
  164. static int wext_get_frequency(const char *ifname, int *buf)
  165. {
  166. struct iwreq wrq;
  167. struct iw_range range;
  168. int i, channel;
  169. if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
  170. {
  171. /* We got a channel number instead ... */
  172. if( wrq.u.freq.m < 1000 )
  173. {
  174. channel = wrq.u.freq.m;
  175. wrq.u.data.pointer = (caddr_t) &range;
  176. wrq.u.data.length = sizeof(struct iw_range);
  177. wrq.u.data.flags = 0;
  178. if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
  179. {
  180. for(i = 0; i < range.num_frequency; i++)
  181. {
  182. if( range.freq[i].i == channel )
  183. {
  184. *buf = wext_freq2mhz(&range.freq[i]);
  185. return 0;
  186. }
  187. }
  188. }
  189. }
  190. else
  191. {
  192. *buf = wext_freq2mhz(&wrq.u.freq);
  193. return 0;
  194. }
  195. }
  196. return -1;
  197. }
  198. static int wext_get_txpower(const char *ifname, int *buf)
  199. {
  200. struct iwreq wrq;
  201. wrq.u.txpower.flags = 0;
  202. if(wext_ioctl(ifname, SIOCGIWTXPOW, &wrq) >= 0)
  203. {
  204. if(wrq.u.txpower.flags & IW_TXPOW_MWATT)
  205. *buf = iwinfo_mw2dbm(wrq.u.txpower.value);
  206. else
  207. *buf = wrq.u.txpower.value;
  208. return 0;
  209. }
  210. return -1;
  211. }
  212. static int wext_get_signal(const char *ifname, int *buf)
  213. {
  214. struct iwreq wrq;
  215. struct iw_statistics stats;
  216. wrq.u.data.pointer = (caddr_t) &stats;
  217. wrq.u.data.length = sizeof(struct iw_statistics);
  218. wrq.u.data.flags = 1;
  219. if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
  220. {
  221. *buf = (stats.qual.updated & IW_QUAL_DBM)
  222. ? (stats.qual.level - 0x100) : stats.qual.level;
  223. return 0;
  224. }
  225. return -1;
  226. }
  227. static int wext_get_noise(const char *ifname, int *buf)
  228. {
  229. struct iwreq wrq;
  230. struct iw_statistics stats;
  231. wrq.u.data.pointer = (caddr_t) &stats;
  232. wrq.u.data.length = sizeof(struct iw_statistics);
  233. wrq.u.data.flags = 1;
  234. if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
  235. {
  236. *buf = (stats.qual.updated & IW_QUAL_DBM)
  237. ? (stats.qual.noise - 0x100) : stats.qual.noise;
  238. return 0;
  239. }
  240. return -1;
  241. }
  242. static int wext_get_quality(const char *ifname, int *buf)
  243. {
  244. struct iwreq wrq;
  245. struct iw_statistics stats;
  246. wrq.u.data.pointer = (caddr_t) &stats;
  247. wrq.u.data.length = sizeof(struct iw_statistics);
  248. wrq.u.data.flags = 1;
  249. if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
  250. {
  251. *buf = stats.qual.qual;
  252. return 0;
  253. }
  254. return -1;
  255. }
  256. static int wext_get_quality_max(const char *ifname, int *buf)
  257. {
  258. struct iwreq wrq;
  259. struct iw_range range;
  260. wrq.u.data.pointer = (caddr_t) &range;
  261. wrq.u.data.length = sizeof(struct iw_range);
  262. wrq.u.data.flags = 0;
  263. if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
  264. {
  265. *buf = range.max_qual.qual;
  266. return 0;
  267. }
  268. return -1;
  269. }
  270. static int wext_get_assoclist(const char *ifname, char *buf, int *len)
  271. {
  272. /* Stub */
  273. return -1;
  274. }
  275. static int wext_get_txpwrlist(const char *ifname, char *buf, int *len)
  276. {
  277. struct iwreq wrq;
  278. struct iw_range range;
  279. struct iwinfo_txpwrlist_entry entry;
  280. int i;
  281. wrq.u.data.pointer = (caddr_t) &range;
  282. wrq.u.data.length = sizeof(struct iw_range);
  283. wrq.u.data.flags = 0;
  284. if( (wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0) &&
  285. (range.num_txpower > 0) && (range.num_txpower <= IW_MAX_TXPOWER) &&
  286. !(range.txpower_capa & IW_TXPOW_RELATIVE)
  287. ) {
  288. for( i = 0; i < range.num_txpower; i++ )
  289. {
  290. if( range.txpower_capa & IW_TXPOW_MWATT )
  291. {
  292. entry.dbm = iwinfo_mw2dbm(range.txpower[i]);
  293. entry.mw = range.txpower[i];
  294. }
  295. /* Madwifi does neither set mW not dBm caps, also iwlist assumes
  296. * dBm if mW is not set, so don't check here... */
  297. else /* if( range.txpower_capa & IW_TXPOW_DBM ) */
  298. {
  299. entry.dbm = range.txpower[i];
  300. entry.mw = iwinfo_dbm2mw(range.txpower[i]);
  301. }
  302. memcpy(&buf[i*sizeof(entry)], &entry, sizeof(entry));
  303. }
  304. *len = i * sizeof(entry);
  305. return 0;
  306. }
  307. return -1;
  308. }
  309. static int wext_get_freqlist(const char *ifname, char *buf, int *len)
  310. {
  311. struct iwreq wrq;
  312. struct iw_range range;
  313. struct iwinfo_freqlist_entry entry;
  314. int i, bl;
  315. wrq.u.data.pointer = (caddr_t) &range;
  316. wrq.u.data.length = sizeof(struct iw_range);
  317. wrq.u.data.flags = 0;
  318. if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
  319. {
  320. bl = 0;
  321. for(i = 0; i < range.num_frequency; i++)
  322. {
  323. entry.mhz = wext_freq2mhz(&range.freq[i]);
  324. entry.channel = range.freq[i].i;
  325. entry.restricted = 0;
  326. memcpy(&buf[bl], &entry, sizeof(struct iwinfo_freqlist_entry));
  327. bl += sizeof(struct iwinfo_freqlist_entry);
  328. }
  329. *len = bl;
  330. return 0;
  331. }
  332. return -1;
  333. }
  334. static int wext_get_country(const char *ifname, char *buf)
  335. {
  336. sprintf(buf, "00");
  337. return 0;
  338. }
  339. static int wext_get_countrylist(const char *ifname, char *buf, int *len)
  340. {
  341. /* Stub */
  342. return -1;
  343. }
  344. static int wext_get_hwmodelist(const char *ifname, int *buf)
  345. {
  346. char chans[IWINFO_BUFSIZE] = { 0 };
  347. struct iwinfo_freqlist_entry *e = NULL;
  348. int len = 0;
  349. *buf = 0;
  350. if( !wext_get_freqlist(ifname, chans, &len) )
  351. {
  352. for( e = (struct iwinfo_freqlist_entry *)chans; e->channel; e++ )
  353. {
  354. if( e->channel <= 14 )
  355. {
  356. *buf |= IWINFO_80211_B;
  357. *buf |= IWINFO_80211_G;
  358. }
  359. else
  360. {
  361. *buf |= IWINFO_80211_A;
  362. }
  363. }
  364. return 0;
  365. }
  366. return -1;
  367. }
  368. static int wext_get_htmodelist(const char *ifname, int *buf)
  369. {
  370. /* Stub */
  371. return -1;
  372. }
  373. static int wext_get_encryption(const char *ifname, char *buf)
  374. {
  375. /* No reliable crypto info in wext */
  376. return -1;
  377. }
  378. static int wext_get_phyname(const char *ifname, char *buf)
  379. {
  380. /* No suitable api in wext */
  381. strcpy(buf, ifname);
  382. return 0;
  383. }
  384. static int wext_get_mbssid_support(const char *ifname, int *buf)
  385. {
  386. /* No multi bssid support atm */
  387. return -1;
  388. }
  389. static char * wext_sysfs_ifname_file(const char *ifname, const char *path)
  390. {
  391. FILE *f;
  392. static char buf[128];
  393. char *rv = NULL;
  394. snprintf(buf, sizeof(buf), "/sys/class/net/%s/%s", ifname, path);
  395. if ((f = fopen(buf, "r")) != NULL)
  396. {
  397. memset(buf, 0, sizeof(buf));
  398. if (fread(buf, 1, sizeof(buf), f))
  399. rv = buf;
  400. fclose(f);
  401. }
  402. return rv;
  403. }
  404. static int wext_get_hardware_id(const char *ifname, char *buf)
  405. {
  406. char *data;
  407. struct iwinfo_hardware_id *id = (struct iwinfo_hardware_id *)buf;
  408. memset(id, 0, sizeof(struct iwinfo_hardware_id));
  409. data = wext_sysfs_ifname_file(ifname, "device/vendor");
  410. if (data)
  411. id->vendor_id = strtoul(data, NULL, 16);
  412. data = wext_sysfs_ifname_file(ifname, "device/device");
  413. if (data)
  414. id->device_id = strtoul(data, NULL, 16);
  415. data = wext_sysfs_ifname_file(ifname, "device/subsystem_device");
  416. if (data)
  417. id->subsystem_device_id = strtoul(data, NULL, 16);
  418. data = wext_sysfs_ifname_file(ifname, "device/subsystem_vendor");
  419. if (data)
  420. id->subsystem_vendor_id = strtoul(data, NULL, 16);
  421. return (id->vendor_id > 0 && id->device_id > 0) ? 0 : -1;
  422. }
  423. static int wext_get_hardware_name(const char *ifname, char *buf)
  424. {
  425. sprintf(buf, "Generic WEXT");
  426. return 0;
  427. }
  428. static int wext_get_txpower_offset(const char *ifname, int *buf)
  429. {
  430. /* Stub */
  431. *buf = 0;
  432. return -1;
  433. }
  434. static int wext_get_frequency_offset(const char *ifname, int *buf)
  435. {
  436. /* Stub */
  437. *buf = 0;
  438. return -1;
  439. }
  440. const struct iwinfo_ops wext_ops = {
  441. .name = "wext",
  442. .probe = wext_probe,
  443. .channel = wext_get_channel,
  444. .center_chan1 = wext_get_center_chan1,
  445. .center_chan2 = wext_get_center_chan2,
  446. .frequency = wext_get_frequency,
  447. .frequency_offset = wext_get_frequency_offset,
  448. .txpower = wext_get_txpower,
  449. .txpower_offset = wext_get_txpower_offset,
  450. .bitrate = wext_get_bitrate,
  451. .signal = wext_get_signal,
  452. .noise = wext_get_noise,
  453. .quality = wext_get_quality,
  454. .quality_max = wext_get_quality_max,
  455. .mbssid_support = wext_get_mbssid_support,
  456. .hwmodelist = wext_get_hwmodelist,
  457. .htmodelist = wext_get_htmodelist,
  458. .mode = wext_get_mode,
  459. .ssid = wext_get_ssid,
  460. .bssid = wext_get_bssid,
  461. .country = wext_get_country,
  462. .hardware_id = wext_get_hardware_id,
  463. .hardware_name = wext_get_hardware_name,
  464. .encryption = wext_get_encryption,
  465. .phyname = wext_get_phyname,
  466. .assoclist = wext_get_assoclist,
  467. .txpwrlist = wext_get_txpwrlist,
  468. .scanlist = wext_get_scanlist,
  469. .freqlist = wext_get_freqlist,
  470. .countrylist = wext_get_countrylist,
  471. .close = wext_close
  472. };