Mirror of the official iwinfo project repository by OpenWrt https://git.openwrt.org/project/iwinfo
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

574 lines
12 KiB

  1. /*
  2. * iwinfo - Wireless Information Library - Shared utility routines
  3. *
  4. * Copyright (C) 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/utils.h"
  23. static int ioctl_socket = -1;
  24. struct uci_context *uci_ctx = NULL;
  25. static int iwinfo_ioctl_socket(void)
  26. {
  27. /* Prepare socket */
  28. if (ioctl_socket == -1)
  29. {
  30. ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0);
  31. fcntl(ioctl_socket, F_SETFD, fcntl(ioctl_socket, F_GETFD) | FD_CLOEXEC);
  32. }
  33. return ioctl_socket;
  34. }
  35. int iwinfo_ioctl(int cmd, void *ifr)
  36. {
  37. int s = iwinfo_ioctl_socket();
  38. return ioctl(s, cmd, ifr);
  39. }
  40. int iwinfo_dbm2mw(int in)
  41. {
  42. double res = 1.0;
  43. int ip = in / 10;
  44. int fp = in % 10;
  45. int k;
  46. for(k = 0; k < ip; k++) res *= 10;
  47. for(k = 0; k < fp; k++) res *= LOG10_MAGIC;
  48. return (int)res;
  49. }
  50. int iwinfo_mw2dbm(int in)
  51. {
  52. double fin = (double) in;
  53. int res = 0;
  54. while(fin > 10.0)
  55. {
  56. res += 10;
  57. fin /= 10.0;
  58. }
  59. while(fin > 1.000001)
  60. {
  61. res += 1;
  62. fin /= LOG10_MAGIC;
  63. }
  64. return (int)res;
  65. }
  66. int iwinfo_ifup(const char *ifname)
  67. {
  68. struct ifreq ifr;
  69. strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
  70. if (iwinfo_ioctl(SIOCGIFFLAGS, &ifr))
  71. return 0;
  72. ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
  73. return !iwinfo_ioctl(SIOCSIFFLAGS, &ifr);
  74. }
  75. int iwinfo_ifdown(const char *ifname)
  76. {
  77. struct ifreq ifr;
  78. strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
  79. if (iwinfo_ioctl(SIOCGIFFLAGS, &ifr))
  80. return 0;
  81. ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
  82. return !iwinfo_ioctl(SIOCSIFFLAGS, &ifr);
  83. }
  84. int iwinfo_ifmac(const char *ifname)
  85. {
  86. struct ifreq ifr;
  87. strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
  88. if (iwinfo_ioctl(SIOCGIFHWADDR, &ifr))
  89. return 0;
  90. ifr.ifr_hwaddr.sa_data[0] |= 0x02;
  91. ifr.ifr_hwaddr.sa_data[1]++;
  92. ifr.ifr_hwaddr.sa_data[2]++;
  93. return !iwinfo_ioctl(SIOCSIFHWADDR, &ifr);
  94. }
  95. void iwinfo_close(void)
  96. {
  97. if (ioctl_socket > -1)
  98. close(ioctl_socket);
  99. ioctl_socket = -1;
  100. }
  101. struct iwinfo_hardware_entry * iwinfo_hardware(struct iwinfo_hardware_id *id)
  102. {
  103. FILE *db;
  104. char buf[256] = { 0 };
  105. static struct iwinfo_hardware_entry e;
  106. struct iwinfo_hardware_entry *rv = NULL;
  107. if (!(db = fopen(IWINFO_HARDWARE_FILE, "r")))
  108. return NULL;
  109. while (fgets(buf, sizeof(buf) - 1, db) != NULL)
  110. {
  111. memset(&e, 0, sizeof(e));
  112. if (sscanf(buf, "%hx %hx %hx %hx %hd %hd \"%63[^\"]\" \"%63[^\"]\"",
  113. &e.vendor_id, &e.device_id,
  114. &e.subsystem_vendor_id, &e.subsystem_device_id,
  115. &e.txpower_offset, &e.frequency_offset,
  116. e.vendor_name, e.device_name) < 8)
  117. continue;
  118. if ((e.vendor_id != 0xffff) && (e.vendor_id != id->vendor_id))
  119. continue;
  120. if ((e.device_id != 0xffff) && (e.device_id != id->device_id))
  121. continue;
  122. if ((e.subsystem_vendor_id != 0xffff) &&
  123. (e.subsystem_vendor_id != id->subsystem_vendor_id))
  124. continue;
  125. if ((e.subsystem_device_id != 0xffff) &&
  126. (e.subsystem_device_id != id->subsystem_device_id))
  127. continue;
  128. rv = &e;
  129. break;
  130. }
  131. fclose(db);
  132. return rv;
  133. }
  134. int iwinfo_hardware_id_from_mtd(struct iwinfo_hardware_id *id)
  135. {
  136. FILE *mtd;
  137. uint16_t *bc;
  138. int fd, off;
  139. unsigned int len;
  140. char buf[128];
  141. if (!(mtd = fopen("/proc/mtd", "r")))
  142. return -1;
  143. while (fgets(buf, sizeof(buf), mtd) != NULL)
  144. {
  145. if (fscanf(mtd, "mtd%d: %x %*x %127s", &off, &len, buf) < 3 ||
  146. (strcmp(buf, "\"boardconfig\"") && strcmp(buf, "\"EEPROM\"") &&
  147. strcmp(buf, "\"factory\"")))
  148. {
  149. off = -1;
  150. continue;
  151. }
  152. break;
  153. }
  154. fclose(mtd);
  155. if (off < 0)
  156. return -1;
  157. snprintf(buf, sizeof(buf), "/dev/mtdblock%d", off);
  158. if ((fd = open(buf, O_RDONLY)) < 0)
  159. return -1;
  160. bc = mmap(NULL, len, PROT_READ, MAP_PRIVATE|MAP_LOCKED, fd, 0);
  161. if ((void *)bc != MAP_FAILED)
  162. {
  163. id->vendor_id = 0;
  164. id->device_id = 0;
  165. for (off = len / 2 - 0x800; off >= 0; off -= 0x800)
  166. {
  167. /* AR531X board data magic */
  168. if ((bc[off] == 0x3533) && (bc[off + 1] == 0x3131))
  169. {
  170. id->vendor_id = bc[off + 0x7d];
  171. id->device_id = bc[off + 0x7c];
  172. id->subsystem_vendor_id = bc[off + 0x84];
  173. id->subsystem_device_id = bc[off + 0x83];
  174. break;
  175. }
  176. /* AR5416 EEPROM magic */
  177. else if ((bc[off] == 0xA55A) || (bc[off] == 0x5AA5))
  178. {
  179. id->vendor_id = bc[off + 0x0D];
  180. id->device_id = bc[off + 0x0E];
  181. id->subsystem_vendor_id = bc[off + 0x13];
  182. id->subsystem_device_id = bc[off + 0x14];
  183. break;
  184. }
  185. /* Rt3xxx SoC */
  186. else if ((bc[off] == 0x3050) || (bc[off] == 0x5030) ||
  187. (bc[off] == 0x3051) || (bc[off] == 0x5130) ||
  188. (bc[off] == 0x3052) || (bc[off] == 0x5230) ||
  189. (bc[off] == 0x3350) || (bc[off] == 0x5033) ||
  190. (bc[off] == 0x3352) || (bc[off] == 0x5233) ||
  191. (bc[off] == 0x3662) || (bc[off] == 0x6236) ||
  192. (bc[off] == 0x3883) || (bc[off] == 0x8338) ||
  193. (bc[off] == 0x5350) || (bc[off] == 0x5053))
  194. {
  195. /* vendor: RaLink */
  196. id->vendor_id = 0x1814;
  197. id->subsystem_vendor_id = 0x1814;
  198. /* device */
  199. if (((bc[off] & 0xf0) == 0x30) ||
  200. ((bc[off] & 0xff) == 0x53))
  201. id->device_id = (bc[off] >> 8) | (bc[off] & 0x00ff) << 8;
  202. else
  203. id->device_id = bc[off];
  204. /* subsystem from EEPROM_NIC_CONF0_RF_TYPE */
  205. id->subsystem_device_id = (bc[off + 0x1a] & 0x0f00) >> 8;
  206. } else if ((bc[off] == 0x7620) || (bc[off] == 0x2076) ||
  207. (bc[off] == 0x7628) || (bc[off] == 0x2876) ||
  208. (bc[off] == 0x7688) || (bc[off] == 0x8876)) {
  209. /* vendor: MediaTek */
  210. id->vendor_id = 0x14c3;
  211. id->subsystem_vendor_id = 0x14c3;
  212. /* device */
  213. if ((bc[off] & 0xff) == 0x76)
  214. id->device_id = (bc[off] >> 8) | (bc[off] & 0x00ff) << 8;
  215. else
  216. id->device_id = bc[off];
  217. /* subsystem from EEPROM_NIC_CONF0_RF_TYPE */
  218. id->subsystem_device_id = (bc[off + 0x1a] & 0x0f00) >> 8;
  219. }
  220. }
  221. munmap(bc, len);
  222. }
  223. close(fd);
  224. return (id->vendor_id && id->device_id) ? 0 : -1;
  225. }
  226. static void iwinfo_parse_rsn_cipher(uint8_t idx, uint8_t *ciphers)
  227. {
  228. switch (idx)
  229. {
  230. case 0:
  231. *ciphers |= IWINFO_CIPHER_NONE;
  232. break;
  233. case 1:
  234. *ciphers |= IWINFO_CIPHER_WEP40;
  235. break;
  236. case 2:
  237. *ciphers |= IWINFO_CIPHER_TKIP;
  238. break;
  239. case 3: /* WRAP */
  240. break;
  241. case 4:
  242. *ciphers |= IWINFO_CIPHER_CCMP;
  243. break;
  244. case 5:
  245. *ciphers |= IWINFO_CIPHER_WEP104;
  246. break;
  247. case 6: /* AES-128-CMAC */
  248. case 7: /* No group addressed */
  249. case 8: /* GCMP */
  250. case 9: /* GCMP-256 */
  251. case 10: /* CCMP-256 */
  252. case 11: /* BIP-GMAC-128 */
  253. case 12: /* BIP-GMAC-256 */
  254. case 13: /* BIP-CMAC-256 */
  255. break;
  256. }
  257. }
  258. void iwinfo_parse_rsn(struct iwinfo_crypto_entry *c, uint8_t *data, uint8_t len,
  259. uint8_t defcipher, uint8_t defauth)
  260. {
  261. uint16_t i, count;
  262. uint8_t wpa_version = 0;
  263. static unsigned char ms_oui[3] = { 0x00, 0x50, 0xf2 };
  264. static unsigned char ieee80211_oui[3] = { 0x00, 0x0f, 0xac };
  265. data += 2;
  266. len -= 2;
  267. if (!memcmp(data, ms_oui, 3))
  268. wpa_version |= 1;
  269. else if (!memcmp(data, ieee80211_oui, 3))
  270. wpa_version |= 2;
  271. if (len < 4)
  272. {
  273. c->group_ciphers |= defcipher;
  274. c->pair_ciphers |= defcipher;
  275. c->auth_suites |= defauth;
  276. return;
  277. }
  278. if (!memcmp(data, ms_oui, 3) || !memcmp(data, ieee80211_oui, 3))
  279. iwinfo_parse_rsn_cipher(data[3], &c->group_ciphers);
  280. data += 4;
  281. len -= 4;
  282. if (len < 2)
  283. {
  284. c->pair_ciphers |= defcipher;
  285. c->auth_suites |= defauth;
  286. return;
  287. }
  288. count = data[0] | (data[1] << 8);
  289. if (2 + (count * 4) > len)
  290. return;
  291. for (i = 0; i < count; i++)
  292. if (!memcmp(data + 2 + (i * 4), ms_oui, 3) ||
  293. !memcmp(data + 2 + (i * 4), ieee80211_oui, 3))
  294. iwinfo_parse_rsn_cipher(data[2 + (i * 4) + 3], &c->pair_ciphers);
  295. data += 2 + (count * 4);
  296. len -= 2 + (count * 4);
  297. if (len < 2)
  298. {
  299. c->auth_suites |= defauth;
  300. return;
  301. }
  302. count = data[0] | (data[1] << 8);
  303. if (2 + (count * 4) > len)
  304. return;
  305. for (i = 0; i < count; i++)
  306. {
  307. if (!memcmp(data + 2 + (i * 4), ms_oui, 3) ||
  308. !memcmp(data + 2 + (i * 4), ieee80211_oui, 3))
  309. {
  310. switch (data[2 + (i * 4) + 3])
  311. {
  312. case 1: /* IEEE 802.1x */
  313. c->wpa_version |= wpa_version;
  314. c->auth_suites |= IWINFO_KMGMT_8021x;
  315. break;
  316. case 2: /* PSK */
  317. c->wpa_version |= wpa_version;
  318. c->auth_suites |= IWINFO_KMGMT_PSK;
  319. break;
  320. case 3: /* FT/IEEE 802.1X */
  321. case 4: /* FT/PSK */
  322. case 5: /* IEEE 802.1X/SHA-256 */
  323. case 6: /* PSK/SHA-256 */
  324. case 7: /* TPK Handshake */
  325. break;
  326. case 8: /* SAE */
  327. c->wpa_version |= 4;
  328. c->auth_suites |= IWINFO_KMGMT_SAE;
  329. break;
  330. case 9: /* FT/SAE */
  331. case 10: /* undefined */
  332. break;
  333. case 11: /* 802.1x Suite-B */
  334. case 12: /* 802.1x Suite-B-192 */
  335. c->wpa_version |= 4;
  336. c->auth_suites |= IWINFO_KMGMT_8021x;
  337. break;
  338. case 13: /* FT/802.1x SHA-384 */
  339. case 14: /* FILS SHA-256 */
  340. case 15: /* FILS SHA-384 */
  341. case 16: /* FT/FILS SHA-256 */
  342. case 17: /* FT/FILS SHA-384 */
  343. break;
  344. case 18: /* OWE */
  345. c->wpa_version |= 4;
  346. c->auth_suites |= IWINFO_KMGMT_OWE;
  347. break;
  348. }
  349. }
  350. }
  351. data += 2 + (count * 4);
  352. len -= 2 + (count * 4);
  353. }
  354. struct uci_section *iwinfo_uci_get_radio(const char *name, const char *type)
  355. {
  356. struct uci_ptr ptr = {
  357. .package = "wireless",
  358. .section = name,
  359. .flags = (name && *name == '@') ? UCI_LOOKUP_EXTENDED : 0,
  360. };
  361. const char *opt;
  362. if (!uci_ctx) {
  363. uci_ctx = uci_alloc_context();
  364. if (!uci_ctx)
  365. return NULL;
  366. }
  367. if (uci_lookup_ptr(uci_ctx, &ptr, NULL, true))
  368. return NULL;
  369. if (!ptr.s || strcmp(ptr.s->type, "wifi-device") != 0)
  370. return NULL;
  371. opt = uci_lookup_option_string(uci_ctx, ptr.s, "type");
  372. if (!opt || strcmp(opt, type) != 0)
  373. return NULL;
  374. return ptr.s;
  375. }
  376. void iwinfo_uci_free(void)
  377. {
  378. if (!uci_ctx)
  379. return;
  380. uci_free_context(uci_ctx);
  381. uci_ctx = NULL;
  382. }
  383. struct iwinfo_ubus_query_state {
  384. const char *ifname;
  385. const char *field;
  386. size_t len;
  387. char *buf;
  388. };
  389. static void iwinfo_ubus_query_cb(struct ubus_request *req, int type,
  390. struct blob_attr *msg)
  391. {
  392. struct iwinfo_ubus_query_state *st = req->priv;
  393. struct blobmsg_policy pol1[2] = {
  394. { "ifname", BLOBMSG_TYPE_STRING },
  395. { "config", BLOBMSG_TYPE_TABLE }
  396. };
  397. struct blobmsg_policy pol2 = { st->field, BLOBMSG_TYPE_STRING };
  398. struct blob_attr *cur, *cur2, *cur3, *cfg[2], *res;
  399. int rem, rem2, rem3;
  400. blobmsg_for_each_attr(cur, msg, rem) {
  401. if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE)
  402. continue;
  403. blobmsg_for_each_attr(cur2, cur, rem2) {
  404. if (blobmsg_type(cur2) != BLOBMSG_TYPE_ARRAY)
  405. continue;
  406. if (strcmp(blobmsg_name(cur2), "interfaces"))
  407. continue;
  408. blobmsg_for_each_attr(cur3, cur2, rem3) {
  409. blobmsg_parse(pol1, sizeof(pol1) / sizeof(pol1[0]), cfg,
  410. blobmsg_data(cur3), blobmsg_len(cur3));
  411. if (!cfg[0] || !cfg[1] ||
  412. strcmp(blobmsg_get_string(cfg[0]), st->ifname))
  413. continue;
  414. blobmsg_parse(&pol2, 1, &res,
  415. blobmsg_data(cfg[1]), blobmsg_len(cfg[1]));
  416. if (!res)
  417. continue;
  418. strncpy(st->buf, blobmsg_get_string(res), st->len);
  419. return;
  420. }
  421. }
  422. }
  423. }
  424. int iwinfo_ubus_query(const char *ifname, const char *field,
  425. char *buf, size_t len)
  426. {
  427. struct iwinfo_ubus_query_state st = {
  428. .ifname = ifname,
  429. .field = field,
  430. .buf = buf,
  431. .len = len
  432. };
  433. struct ubus_context *ctx = NULL;
  434. struct blob_buf b = { };
  435. int rv = -1;
  436. uint32_t id;
  437. blob_buf_init(&b, 0);
  438. ctx = ubus_connect(NULL);
  439. if (!ctx)
  440. goto out;
  441. if (ubus_lookup_id(ctx, "network.wireless", &id))
  442. goto out;
  443. if (ubus_invoke(ctx, id, "status", b.head, iwinfo_ubus_query_cb, &st, 250))
  444. goto out;
  445. rv = 0;
  446. out:
  447. if (ctx)
  448. ubus_free(ctx);
  449. blob_buf_free(&b);
  450. return rv;
  451. }