iwinfo_utils.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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. static int iwinfo_bit(int value, int max)
  67. {
  68. int i;
  69. if (max > 31 || !(value & ((1 << max) - 1)))
  70. return -1;
  71. for (i = 0; i < max; i++)
  72. {
  73. if (value & 1)
  74. break;
  75. value >>= 1;
  76. }
  77. return i;
  78. }
  79. static const char * const iwinfo_name(int mask, int max, const char * const names[])
  80. {
  81. int index = iwinfo_bit(mask, max);
  82. if (index < 0)
  83. return NULL;
  84. return names[index];
  85. }
  86. const char * const iwinfo_band_name(int mask)
  87. {
  88. return iwinfo_name(mask, IWINFO_BAND_COUNT, IWINFO_BAND_NAMES);
  89. }
  90. const char * const iwinfo_htmode_name(int mask)
  91. {
  92. return iwinfo_name(mask, IWINFO_HTMODE_COUNT, IWINFO_HTMODE_NAMES);
  93. }
  94. uint32_t iwinfo_band2ghz(uint8_t band)
  95. {
  96. switch (band)
  97. {
  98. case IWINFO_BAND_24:
  99. return 2;
  100. case IWINFO_BAND_5:
  101. return 5;
  102. case IWINFO_BAND_6:
  103. return 6;
  104. case IWINFO_BAND_60:
  105. return 60;
  106. }
  107. return 0;
  108. }
  109. uint8_t iwinfo_ghz2band(uint32_t ghz)
  110. {
  111. switch (ghz)
  112. {
  113. case 2:
  114. return IWINFO_BAND_24;
  115. case 5:
  116. return IWINFO_BAND_5;
  117. case 6:
  118. return IWINFO_BAND_6;
  119. case 60:
  120. return IWINFO_BAND_60;
  121. }
  122. return 0;
  123. }
  124. size_t iwinfo_format_hwmodes(int modes, char *buf, size_t len)
  125. {
  126. // bit numbers as per IWINFO_80211_*: ad ac ax a b g n
  127. const int order[IWINFO_80211_COUNT] = { 5, 4, 6, 0, 1, 2, 3 };
  128. size_t res = 0;
  129. int i;
  130. *buf = 0;
  131. if (!(modes & ((1 << IWINFO_80211_COUNT) - 1)))
  132. return 0;
  133. for (i = 0; i < IWINFO_80211_COUNT; i++)
  134. if (modes & 1 << order[i])
  135. res += snprintf(buf + res, len - res, "%s/", IWINFO_80211_NAMES[order[i]]);
  136. if (res > 0)
  137. {
  138. res--;
  139. buf[res] = 0;
  140. }
  141. return res;
  142. }
  143. int iwinfo_htmode_is_ht(int htmode)
  144. {
  145. switch (htmode)
  146. {
  147. case IWINFO_HTMODE_HT20:
  148. case IWINFO_HTMODE_HT40:
  149. return 1;
  150. }
  151. return 0;
  152. }
  153. int iwinfo_htmode_is_vht(int htmode)
  154. {
  155. switch (htmode)
  156. {
  157. case IWINFO_HTMODE_VHT20:
  158. case IWINFO_HTMODE_VHT40:
  159. case IWINFO_HTMODE_VHT80:
  160. case IWINFO_HTMODE_VHT80_80:
  161. case IWINFO_HTMODE_VHT160:
  162. return 1;
  163. }
  164. return 0;
  165. }
  166. int iwinfo_htmode_is_he(int htmode)
  167. {
  168. switch (htmode)
  169. {
  170. case IWINFO_HTMODE_HE20:
  171. case IWINFO_HTMODE_HE40:
  172. case IWINFO_HTMODE_HE80:
  173. case IWINFO_HTMODE_HE80_80:
  174. case IWINFO_HTMODE_HE160:
  175. return 1;
  176. }
  177. return 0;
  178. }
  179. int iwinfo_ifup(const char *ifname)
  180. {
  181. struct ifreq ifr;
  182. strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
  183. if (iwinfo_ioctl(SIOCGIFFLAGS, &ifr))
  184. return 0;
  185. ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
  186. return !iwinfo_ioctl(SIOCSIFFLAGS, &ifr);
  187. }
  188. int iwinfo_ifdown(const char *ifname)
  189. {
  190. struct ifreq ifr;
  191. strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
  192. if (iwinfo_ioctl(SIOCGIFFLAGS, &ifr))
  193. return 0;
  194. ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
  195. return !iwinfo_ioctl(SIOCSIFFLAGS, &ifr);
  196. }
  197. int iwinfo_ifmac(const char *ifname)
  198. {
  199. struct ifreq ifr;
  200. strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
  201. if (iwinfo_ioctl(SIOCGIFHWADDR, &ifr))
  202. return 0;
  203. ifr.ifr_hwaddr.sa_data[0] |= 0x02;
  204. ifr.ifr_hwaddr.sa_data[1]++;
  205. ifr.ifr_hwaddr.sa_data[2]++;
  206. return !iwinfo_ioctl(SIOCSIFHWADDR, &ifr);
  207. }
  208. void iwinfo_close(void)
  209. {
  210. if (ioctl_socket > -1)
  211. close(ioctl_socket);
  212. ioctl_socket = -1;
  213. }
  214. struct iwinfo_hardware_entry * iwinfo_hardware(struct iwinfo_hardware_id *id)
  215. {
  216. FILE *db;
  217. char buf[256] = { 0 };
  218. static struct iwinfo_hardware_entry e;
  219. struct iwinfo_hardware_entry *rv = NULL;
  220. if (!(db = fopen(IWINFO_HARDWARE_FILE, "r")))
  221. return NULL;
  222. while (fgets(buf, sizeof(buf) - 1, db) != NULL)
  223. {
  224. if (buf[0] == '#')
  225. continue;
  226. memset(&e, 0, sizeof(e));
  227. if (sscanf(buf, "%hx %hx %hx %hx %hd %hd \"%63[^\"]\" \"%63[^\"]\"",
  228. &e.vendor_id, &e.device_id,
  229. &e.subsystem_vendor_id, &e.subsystem_device_id,
  230. &e.txpower_offset, &e.frequency_offset,
  231. e.vendor_name, e.device_name) != 8 &&
  232. sscanf(buf, "\"%127[^\"]\" %hd %hd \"%63[^\"]\" \"%63[^\"]\"",
  233. e.compatible, &e.txpower_offset, &e.frequency_offset,
  234. e.vendor_name, e.device_name) != 5)
  235. continue;
  236. if ((e.vendor_id != 0xffff) && (e.vendor_id != id->vendor_id))
  237. continue;
  238. if ((e.device_id != 0xffff) && (e.device_id != id->device_id))
  239. continue;
  240. if ((e.subsystem_vendor_id != 0xffff) &&
  241. (e.subsystem_vendor_id != id->subsystem_vendor_id))
  242. continue;
  243. if ((e.subsystem_device_id != 0xffff) &&
  244. (e.subsystem_device_id != id->subsystem_device_id))
  245. continue;
  246. if (strcmp(e.compatible, id->compatible))
  247. continue;
  248. rv = &e;
  249. break;
  250. }
  251. fclose(db);
  252. return rv;
  253. }
  254. int iwinfo_hardware_id_from_mtd(struct iwinfo_hardware_id *id)
  255. {
  256. FILE *mtd;
  257. uint16_t *bc;
  258. int fd, off;
  259. unsigned int len;
  260. char buf[128];
  261. if (!(mtd = fopen("/proc/mtd", "r")))
  262. return -1;
  263. while (fgets(buf, sizeof(buf), mtd) != NULL)
  264. {
  265. if (fscanf(mtd, "mtd%d: %x %*x %127s", &off, &len, buf) < 3 ||
  266. (strcmp(buf, "\"boardconfig\"") && strcmp(buf, "\"EEPROM\"") &&
  267. strcmp(buf, "\"factory\"")))
  268. {
  269. off = -1;
  270. continue;
  271. }
  272. break;
  273. }
  274. fclose(mtd);
  275. if (off < 0)
  276. return -1;
  277. snprintf(buf, sizeof(buf), "/dev/mtdblock%d", off);
  278. if ((fd = open(buf, O_RDONLY)) < 0)
  279. return -1;
  280. bc = mmap(NULL, len, PROT_READ, MAP_PRIVATE|MAP_LOCKED, fd, 0);
  281. if ((void *)bc != MAP_FAILED)
  282. {
  283. id->vendor_id = 0;
  284. id->device_id = 0;
  285. for (off = len / 2 - 0x800; off >= 0; off -= 0x800)
  286. {
  287. /* AR531X board data magic */
  288. if ((bc[off] == 0x3533) && (bc[off + 1] == 0x3131))
  289. {
  290. id->vendor_id = bc[off + 0x7d];
  291. id->device_id = bc[off + 0x7c];
  292. id->subsystem_vendor_id = bc[off + 0x84];
  293. id->subsystem_device_id = bc[off + 0x83];
  294. break;
  295. }
  296. /* AR5416 EEPROM magic */
  297. else if ((bc[off] == 0xA55A) || (bc[off] == 0x5AA5))
  298. {
  299. id->vendor_id = bc[off + 0x0D];
  300. id->device_id = bc[off + 0x0E];
  301. id->subsystem_vendor_id = bc[off + 0x13];
  302. id->subsystem_device_id = bc[off + 0x14];
  303. break;
  304. }
  305. /* Rt3xxx SoC */
  306. else if ((bc[off] == 0x3050) || (bc[off] == 0x5030) ||
  307. (bc[off] == 0x3051) || (bc[off] == 0x5130) ||
  308. (bc[off] == 0x3052) || (bc[off] == 0x5230) ||
  309. (bc[off] == 0x3350) || (bc[off] == 0x5033) ||
  310. (bc[off] == 0x3352) || (bc[off] == 0x5233) ||
  311. (bc[off] == 0x3662) || (bc[off] == 0x6236) ||
  312. (bc[off] == 0x3883) || (bc[off] == 0x8338) ||
  313. (bc[off] == 0x5350) || (bc[off] == 0x5053))
  314. {
  315. /* vendor: RaLink */
  316. id->vendor_id = 0x1814;
  317. id->subsystem_vendor_id = 0x1814;
  318. /* device */
  319. if (((bc[off] & 0xf0) == 0x30) ||
  320. ((bc[off] & 0xff) == 0x53))
  321. id->device_id = (bc[off] >> 8) | (bc[off] & 0x00ff) << 8;
  322. else
  323. id->device_id = bc[off];
  324. /* subsystem from EEPROM_NIC_CONF0_RF_TYPE */
  325. id->subsystem_device_id = (bc[off + 0x1a] & 0x0f00) >> 8;
  326. } else if ((bc[off] == 0x7620) || (bc[off] == 0x2076) ||
  327. (bc[off] == 0x7628) || (bc[off] == 0x2876) ||
  328. (bc[off] == 0x7688) || (bc[off] == 0x8876)) {
  329. /* vendor: MediaTek */
  330. id->vendor_id = 0x14c3;
  331. id->subsystem_vendor_id = 0x14c3;
  332. /* device */
  333. if ((bc[off] & 0xff) == 0x76)
  334. id->device_id = (bc[off] >> 8) | (bc[off] & 0x00ff) << 8;
  335. else
  336. id->device_id = bc[off];
  337. /* subsystem from EEPROM_NIC_CONF0_RF_TYPE */
  338. id->subsystem_device_id = (bc[off + 0x1a] & 0x0f00) >> 8;
  339. }
  340. }
  341. munmap(bc, len);
  342. }
  343. close(fd);
  344. return (id->vendor_id && id->device_id) ? 0 : -1;
  345. }
  346. static void iwinfo_parse_rsn_cipher(uint8_t idx, uint16_t *ciphers)
  347. {
  348. switch (idx)
  349. {
  350. case 0:
  351. *ciphers |= IWINFO_CIPHER_NONE;
  352. break;
  353. case 1:
  354. *ciphers |= IWINFO_CIPHER_WEP40;
  355. break;
  356. case 2:
  357. *ciphers |= IWINFO_CIPHER_TKIP;
  358. break;
  359. case 3: /* WRAP */
  360. break;
  361. case 4:
  362. *ciphers |= IWINFO_CIPHER_CCMP;
  363. break;
  364. case 5:
  365. *ciphers |= IWINFO_CIPHER_WEP104;
  366. break;
  367. case 8:
  368. *ciphers |= IWINFO_CIPHER_GCMP;
  369. break;
  370. case 9:
  371. *ciphers |= IWINFO_CIPHER_GCMP256;
  372. break;
  373. case 10:
  374. *ciphers |= IWINFO_CIPHER_CCMP256;
  375. break;
  376. case 6: /* AES-128-CMAC */
  377. case 7: /* No group addressed */
  378. case 11: /* BIP-GMAC-128 */
  379. case 12: /* BIP-GMAC-256 */
  380. case 13: /* BIP-CMAC-256 */
  381. break;
  382. }
  383. }
  384. void iwinfo_parse_rsn(struct iwinfo_crypto_entry *c, uint8_t *data, uint8_t len,
  385. uint16_t defcipher, uint8_t defauth)
  386. {
  387. uint16_t i, count;
  388. uint8_t wpa_version = 0;
  389. static unsigned char ms_oui[3] = { 0x00, 0x50, 0xf2 };
  390. static unsigned char ieee80211_oui[3] = { 0x00, 0x0f, 0xac };
  391. data += 2;
  392. len -= 2;
  393. if (!memcmp(data, ms_oui, 3))
  394. wpa_version |= 1;
  395. else if (!memcmp(data, ieee80211_oui, 3))
  396. wpa_version |= 2;
  397. if (len < 4)
  398. {
  399. c->group_ciphers |= defcipher;
  400. c->pair_ciphers |= defcipher;
  401. c->auth_suites |= defauth;
  402. return;
  403. }
  404. if (!memcmp(data, ms_oui, 3) || !memcmp(data, ieee80211_oui, 3))
  405. iwinfo_parse_rsn_cipher(data[3], &c->group_ciphers);
  406. data += 4;
  407. len -= 4;
  408. if (len < 2)
  409. {
  410. c->pair_ciphers |= defcipher;
  411. c->auth_suites |= defauth;
  412. return;
  413. }
  414. count = data[0] | (data[1] << 8);
  415. if (2 + (count * 4) > len)
  416. return;
  417. for (i = 0; i < count; i++)
  418. if (!memcmp(data + 2 + (i * 4), ms_oui, 3) ||
  419. !memcmp(data + 2 + (i * 4), ieee80211_oui, 3))
  420. iwinfo_parse_rsn_cipher(data[2 + (i * 4) + 3], &c->pair_ciphers);
  421. data += 2 + (count * 4);
  422. len -= 2 + (count * 4);
  423. if (len < 2)
  424. {
  425. c->auth_suites |= defauth;
  426. return;
  427. }
  428. count = data[0] | (data[1] << 8);
  429. if (2 + (count * 4) > len)
  430. return;
  431. for (i = 0; i < count; i++)
  432. {
  433. if (!memcmp(data + 2 + (i * 4), ms_oui, 3) ||
  434. !memcmp(data + 2 + (i * 4), ieee80211_oui, 3))
  435. {
  436. switch (data[2 + (i * 4) + 3])
  437. {
  438. case 1: /* IEEE 802.1x */
  439. c->wpa_version |= wpa_version;
  440. c->auth_suites |= IWINFO_KMGMT_8021x;
  441. break;
  442. case 2: /* PSK */
  443. c->wpa_version |= wpa_version;
  444. c->auth_suites |= IWINFO_KMGMT_PSK;
  445. break;
  446. case 3: /* FT/IEEE 802.1X */
  447. case 4: /* FT/PSK */
  448. case 5: /* IEEE 802.1X/SHA-256 */
  449. case 6: /* PSK/SHA-256 */
  450. case 7: /* TPK Handshake */
  451. break;
  452. case 8: /* SAE */
  453. c->wpa_version |= 4;
  454. c->auth_suites |= IWINFO_KMGMT_SAE;
  455. break;
  456. case 9: /* FT/SAE */
  457. case 10: /* undefined */
  458. break;
  459. case 11: /* 802.1x Suite-B */
  460. case 12: /* 802.1x Suite-B-192 */
  461. case 13: /* FT/802.1x SHA-384 */
  462. c->wpa_version |= 4;
  463. c->auth_suites |= IWINFO_KMGMT_8021x;
  464. break;
  465. case 14: /* FILS SHA-256 */
  466. case 15: /* FILS SHA-384 */
  467. case 16: /* FT/FILS SHA-256 */
  468. case 17: /* FT/FILS SHA-384 */
  469. break;
  470. case 18: /* OWE */
  471. c->wpa_version |= 4;
  472. c->auth_suites |= IWINFO_KMGMT_OWE;
  473. break;
  474. }
  475. }
  476. }
  477. data += 2 + (count * 4);
  478. len -= 2 + (count * 4);
  479. }
  480. struct uci_section *iwinfo_uci_get_radio(const char *name, const char *type)
  481. {
  482. struct uci_ptr ptr = {
  483. .package = "wireless",
  484. .section = name,
  485. .flags = (name && *name == '@') ? UCI_LOOKUP_EXTENDED : 0,
  486. };
  487. const char *opt;
  488. if (!uci_ctx) {
  489. uci_ctx = uci_alloc_context();
  490. if (!uci_ctx)
  491. return NULL;
  492. }
  493. if (uci_lookup_ptr(uci_ctx, &ptr, NULL, true))
  494. return NULL;
  495. if (!ptr.s || strcmp(ptr.s->type, "wifi-device") != 0)
  496. return NULL;
  497. opt = uci_lookup_option_string(uci_ctx, ptr.s, "type");
  498. if (!opt || strcmp(opt, type) != 0)
  499. return NULL;
  500. return ptr.s;
  501. }
  502. void iwinfo_uci_free(void)
  503. {
  504. if (!uci_ctx)
  505. return;
  506. uci_free_context(uci_ctx);
  507. uci_ctx = NULL;
  508. }
  509. struct iwinfo_ubus_query_state {
  510. const char *ifname;
  511. const char *field;
  512. size_t len;
  513. char *buf;
  514. };
  515. static void iwinfo_ubus_query_cb(struct ubus_request *req, int type,
  516. struct blob_attr *msg)
  517. {
  518. struct iwinfo_ubus_query_state *st = req->priv;
  519. struct blobmsg_policy pol1[2] = {
  520. { "ifname", BLOBMSG_TYPE_STRING },
  521. { "config", BLOBMSG_TYPE_TABLE }
  522. };
  523. struct blobmsg_policy pol2 = { st->field, BLOBMSG_TYPE_STRING };
  524. struct blob_attr *cur, *cur2, *cur3, *cfg[2], *res;
  525. int rem, rem2, rem3;
  526. blobmsg_for_each_attr(cur, msg, rem) {
  527. if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE)
  528. continue;
  529. blobmsg_for_each_attr(cur2, cur, rem2) {
  530. if (blobmsg_type(cur2) != BLOBMSG_TYPE_ARRAY)
  531. continue;
  532. if (strcmp(blobmsg_name(cur2), "interfaces"))
  533. continue;
  534. blobmsg_for_each_attr(cur3, cur2, rem3) {
  535. blobmsg_parse(pol1, sizeof(pol1) / sizeof(pol1[0]), cfg,
  536. blobmsg_data(cur3), blobmsg_len(cur3));
  537. if (!cfg[0] || !cfg[1] ||
  538. strcmp(blobmsg_get_string(cfg[0]), st->ifname))
  539. continue;
  540. blobmsg_parse(&pol2, 1, &res,
  541. blobmsg_data(cfg[1]), blobmsg_len(cfg[1]));
  542. if (!res)
  543. continue;
  544. strncpy(st->buf, blobmsg_get_string(res), st->len);
  545. return;
  546. }
  547. }
  548. }
  549. }
  550. int iwinfo_ubus_query(const char *ifname, const char *field,
  551. char *buf, size_t len)
  552. {
  553. struct iwinfo_ubus_query_state st = {
  554. .ifname = ifname,
  555. .field = field,
  556. .buf = buf,
  557. .len = len
  558. };
  559. struct ubus_context *ctx = NULL;
  560. struct blob_buf b = { };
  561. int rv = -1;
  562. uint32_t id;
  563. blob_buf_init(&b, 0);
  564. ctx = ubus_connect(NULL);
  565. if (!ctx)
  566. goto out;
  567. if (ubus_lookup_id(ctx, "network.wireless", &id))
  568. goto out;
  569. if (ubus_invoke(ctx, id, "status", b.head, iwinfo_ubus_query_cb, &st, 250))
  570. goto out;
  571. rv = 0;
  572. out:
  573. if (ctx)
  574. ubus_free(ctx);
  575. blob_buf_free(&b);
  576. return rv;
  577. }