iwinfo.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. /*
  2. * rpcd - UBUS RPC server
  3. *
  4. * Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <sys/types.h>
  19. #include <dirent.h>
  20. #include <libubus.h>
  21. #include <iwinfo.h>
  22. #include <iwinfo/utils.h>
  23. #include <net/ethernet.h>
  24. #ifdef linux
  25. #include <netinet/ether.h>
  26. #endif
  27. #include <rpcd/plugin.h>
  28. static struct blob_buf buf;
  29. static const struct iwinfo_ops *iw;
  30. static const char *ifname;
  31. enum {
  32. RPC_D_DEVICE,
  33. __RPC_D_MAX,
  34. };
  35. static const struct blobmsg_policy rpc_device_policy[__RPC_D_MAX] = {
  36. [RPC_D_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
  37. };
  38. enum {
  39. RPC_A_DEVICE,
  40. RPC_A_MACADDR,
  41. __RPC_A_MAX,
  42. };
  43. static const struct blobmsg_policy rpc_assoclist_policy[__RPC_A_MAX] = {
  44. [RPC_A_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
  45. [RPC_A_MACADDR] = { .name = "mac", .type = BLOBMSG_TYPE_STRING },
  46. };
  47. enum {
  48. RPC_U_SECTION,
  49. __RPC_U_MAX
  50. };
  51. static const struct blobmsg_policy rpc_uci_policy[__RPC_U_MAX] = {
  52. [RPC_U_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
  53. };
  54. static int
  55. __rpc_iwinfo_open(struct blob_attr *device)
  56. {
  57. if (!device)
  58. return UBUS_STATUS_INVALID_ARGUMENT;
  59. ifname = blobmsg_data(device);
  60. iw = iwinfo_backend(ifname);
  61. return iw ? UBUS_STATUS_OK : UBUS_STATUS_NOT_FOUND;
  62. }
  63. static int
  64. rpc_iwinfo_open(struct blob_attr *msg)
  65. {
  66. static struct blob_attr *tb[__RPC_D_MAX];
  67. blobmsg_parse(rpc_device_policy, __RPC_D_MAX, tb,
  68. blob_data(msg), blob_len(msg));
  69. return __rpc_iwinfo_open(tb[RPC_D_DEVICE]);
  70. }
  71. static void
  72. rpc_iwinfo_close(void)
  73. {
  74. iw = NULL;
  75. ifname = NULL;
  76. iwinfo_finish();
  77. }
  78. static void
  79. rpc_iwinfo_call_int(const char *name, int (*func)(const char *, int *),
  80. const char **map)
  81. {
  82. int rv;
  83. if (!func(ifname, &rv))
  84. {
  85. if (!map)
  86. blobmsg_add_u32(&buf, name, rv);
  87. else
  88. blobmsg_add_string(&buf, name, map[rv]);
  89. }
  90. }
  91. static void
  92. rpc_iwinfo_call_hardware_id(const char *name)
  93. {
  94. struct iwinfo_hardware_id ids;
  95. void *c;
  96. if (!iw->hardware_id(ifname, (char *)&ids))
  97. {
  98. c = blobmsg_open_array(&buf, name);
  99. blobmsg_add_u32(&buf, NULL, ids.vendor_id);
  100. blobmsg_add_u32(&buf, NULL, ids.device_id);
  101. blobmsg_add_u32(&buf, NULL, ids.subsystem_vendor_id);
  102. blobmsg_add_u32(&buf, NULL, ids.subsystem_device_id);
  103. blobmsg_close_array(&buf, c);
  104. }
  105. }
  106. static void
  107. rpc_iwinfo_add_encryption(const char *name, struct iwinfo_crypto_entry *e)
  108. {
  109. int ciph, wpa_version;
  110. void *c, *d;
  111. c = blobmsg_open_table(&buf, name);
  112. blobmsg_add_u8(&buf, "enabled", e->enabled);
  113. if (e->enabled)
  114. {
  115. if (!e->wpa_version)
  116. {
  117. d = blobmsg_open_array(&buf, "wep");
  118. if (e->auth_algs & IWINFO_AUTH_OPEN)
  119. blobmsg_add_string(&buf, NULL, "open");
  120. if (e->auth_algs & IWINFO_AUTH_SHARED)
  121. blobmsg_add_string(&buf, NULL, "shared");
  122. blobmsg_close_array(&buf, d);
  123. }
  124. else
  125. {
  126. d = blobmsg_open_array(&buf, "wpa");
  127. for (wpa_version = 1; wpa_version <= 3; wpa_version++)
  128. if (e->wpa_version & (1 << (wpa_version - 1)))
  129. blobmsg_add_u32(&buf, NULL, wpa_version);
  130. blobmsg_close_array(&buf, d);
  131. d = blobmsg_open_array(&buf, "authentication");
  132. if (e->auth_suites & IWINFO_KMGMT_PSK)
  133. blobmsg_add_string(&buf, NULL, "psk");
  134. if (e->auth_suites & IWINFO_KMGMT_8021x)
  135. blobmsg_add_string(&buf, NULL, "802.1x");
  136. if (e->auth_suites & IWINFO_KMGMT_SAE)
  137. blobmsg_add_string(&buf, NULL, "sae");
  138. if (e->auth_suites & IWINFO_KMGMT_OWE)
  139. blobmsg_add_string(&buf, NULL, "owe");
  140. if (!e->auth_suites ||
  141. (e->auth_suites & IWINFO_KMGMT_NONE))
  142. blobmsg_add_string(&buf, NULL, "none");
  143. blobmsg_close_array(&buf, d);
  144. }
  145. d = blobmsg_open_array(&buf, "ciphers");
  146. ciph = e->pair_ciphers | e->group_ciphers;
  147. if (ciph & IWINFO_CIPHER_WEP40)
  148. blobmsg_add_string(&buf, NULL, "wep-40");
  149. if (ciph & IWINFO_CIPHER_WEP104)
  150. blobmsg_add_string(&buf, NULL, "wep-104");
  151. if (ciph & IWINFO_CIPHER_TKIP)
  152. blobmsg_add_string(&buf, NULL, "tkip");
  153. if (ciph & IWINFO_CIPHER_CCMP)
  154. blobmsg_add_string(&buf, NULL, "ccmp");
  155. if (ciph & IWINFO_CIPHER_GCMP)
  156. blobmsg_add_string(&buf, NULL, "gcmp");
  157. if (ciph & IWINFO_CIPHER_WRAP)
  158. blobmsg_add_string(&buf, NULL, "wrap");
  159. if (ciph & IWINFO_CIPHER_AESOCB)
  160. blobmsg_add_string(&buf, NULL, "aes-ocb");
  161. if (ciph & IWINFO_CIPHER_CKIP)
  162. blobmsg_add_string(&buf, NULL, "ckip");
  163. if (!ciph || (ciph & IWINFO_CIPHER_NONE))
  164. blobmsg_add_string(&buf, NULL, "none");
  165. blobmsg_close_array(&buf, d);
  166. }
  167. blobmsg_close_table(&buf, c);
  168. }
  169. static void
  170. rpc_iwinfo_call_encryption(const char *name)
  171. {
  172. struct iwinfo_crypto_entry crypto = { 0 };
  173. if (!iw->encryption(ifname, (char *)&crypto))
  174. rpc_iwinfo_add_encryption(name, &crypto);
  175. }
  176. static void
  177. rpc_iwinfo_call_htmodes(const char *name)
  178. {
  179. int modes;
  180. void *c;
  181. if (!iw->htmodelist(ifname, &modes))
  182. {
  183. c = blobmsg_open_array(&buf, name);
  184. if (modes & IWINFO_HTMODE_HT20)
  185. blobmsg_add_string(&buf, NULL, "HT20");
  186. if (modes & IWINFO_HTMODE_HT40)
  187. blobmsg_add_string(&buf, NULL, "HT40");
  188. if (modes & IWINFO_HTMODE_VHT20)
  189. blobmsg_add_string(&buf, NULL, "VHT20");
  190. if (modes & IWINFO_HTMODE_VHT40)
  191. blobmsg_add_string(&buf, NULL, "VHT40");
  192. if (modes & IWINFO_HTMODE_VHT80)
  193. blobmsg_add_string(&buf, NULL, "VHT80");
  194. if (modes & IWINFO_HTMODE_VHT80_80)
  195. blobmsg_add_string(&buf, NULL, "VHT80+80");
  196. if (modes & IWINFO_HTMODE_VHT160)
  197. blobmsg_add_string(&buf, NULL, "VHT160");
  198. if (modes & IWINFO_HTMODE_HE20)
  199. blobmsg_add_string(&buf, NULL, "HE20");
  200. if (modes & IWINFO_HTMODE_HE40)
  201. blobmsg_add_string(&buf, NULL, "HE40");
  202. if (modes & IWINFO_HTMODE_HE80)
  203. blobmsg_add_string(&buf, NULL, "HE80");
  204. if (modes & IWINFO_HTMODE_HE80_80)
  205. blobmsg_add_string(&buf, NULL, "HE80+80");
  206. if (modes & IWINFO_HTMODE_HE160)
  207. blobmsg_add_string(&buf, NULL, "HE160");
  208. blobmsg_close_array(&buf, c);
  209. }
  210. }
  211. static void
  212. rpc_iwinfo_call_hwmodes(const char *name)
  213. {
  214. int modes;
  215. void *c;
  216. if (!iw->hwmodelist(ifname, &modes))
  217. {
  218. c = blobmsg_open_array(&buf, name);
  219. if (modes & IWINFO_80211_AD)
  220. blobmsg_add_string(&buf, NULL, "ad");
  221. if (modes & IWINFO_80211_AC)
  222. blobmsg_add_string(&buf, NULL, "ac");
  223. if (modes & IWINFO_80211_AX)
  224. blobmsg_add_string(&buf, NULL, "ax");
  225. if (modes & IWINFO_80211_A)
  226. blobmsg_add_string(&buf, NULL, "a");
  227. if (modes & IWINFO_80211_B)
  228. blobmsg_add_string(&buf, NULL, "b");
  229. if (modes & IWINFO_80211_G)
  230. blobmsg_add_string(&buf, NULL, "g");
  231. if (modes & IWINFO_80211_N)
  232. blobmsg_add_string(&buf, NULL, "n");
  233. blobmsg_close_array(&buf, c);
  234. }
  235. }
  236. static void rpc_iwinfo_call_hw_ht_mode()
  237. {
  238. const char *hwmode_str;
  239. const char *htmode_str;
  240. int32_t htmode = 0;
  241. int modes;
  242. if (!iw->hwmodelist(ifname, &modes) && (modes == IWINFO_80211_AD)) {
  243. blobmsg_add_string(&buf, "hwmode", "ad");
  244. return;
  245. }
  246. if (iw->htmode(ifname, &htmode))
  247. return;
  248. switch (htmode) {
  249. case IWINFO_HTMODE_HT20:
  250. htmode_str = "HT20";
  251. hwmode_str = "n";
  252. break;
  253. case IWINFO_HTMODE_HT40:
  254. htmode_str = "HT40";
  255. hwmode_str = "n";
  256. break;
  257. case IWINFO_HTMODE_VHT80:
  258. htmode_str = "VHT80";
  259. hwmode_str = "ac";
  260. break;
  261. case IWINFO_HTMODE_VHT80_80:
  262. htmode_str = "VHT80+80";
  263. hwmode_str = "ac";
  264. break;
  265. case IWINFO_HTMODE_VHT160:
  266. htmode_str = "VHT160";
  267. hwmode_str = "ac";
  268. break;
  269. case IWINFO_HTMODE_HE20:
  270. htmode_str = "HE20";
  271. hwmode_str = "ax";
  272. break;
  273. case IWINFO_HTMODE_HE40:
  274. htmode_str = "HE40";
  275. hwmode_str = "ax";
  276. break;
  277. case IWINFO_HTMODE_HE80:
  278. htmode_str = "HE80";
  279. hwmode_str = "ax";
  280. break;
  281. case IWINFO_HTMODE_HE80_80:
  282. htmode_str = "HE80+80";
  283. hwmode_str = "ax";
  284. break;
  285. case IWINFO_HTMODE_HE160:
  286. htmode_str = "HE160";
  287. hwmode_str = "ax";
  288. break;
  289. case IWINFO_HTMODE_NOHT:
  290. htmode_str = "20";
  291. hwmode_str = "a/g";
  292. break;
  293. default:
  294. htmode_str = hwmode_str = "unknown";
  295. break;
  296. }
  297. blobmsg_add_string(&buf, "hwmode", hwmode_str);
  298. blobmsg_add_string(&buf, "htmode", htmode_str);
  299. }
  300. static void
  301. rpc_iwinfo_call_str(const char *name, int (*func)(const char *, char *))
  302. {
  303. char rv[IWINFO_BUFSIZE] = { 0 };
  304. if (!func(ifname, rv))
  305. blobmsg_add_string(&buf, name, rv);
  306. }
  307. static int
  308. rpc_iwinfo_info(struct ubus_context *ctx, struct ubus_object *obj,
  309. struct ubus_request_data *req, const char *method,
  310. struct blob_attr *msg)
  311. {
  312. int rv;
  313. void *c;
  314. rv = rpc_iwinfo_open(msg);
  315. if (rv)
  316. return rv;
  317. blob_buf_init(&buf, 0);
  318. rpc_iwinfo_call_str("phy", iw->phyname);
  319. rpc_iwinfo_call_str("ssid", iw->ssid);
  320. rpc_iwinfo_call_str("bssid", iw->bssid);
  321. rpc_iwinfo_call_str("country", iw->country);
  322. rpc_iwinfo_call_int("mode", iw->mode, IWINFO_OPMODE_NAMES);
  323. rpc_iwinfo_call_int("channel", iw->channel, NULL);
  324. rpc_iwinfo_call_int("center_chan1", iw->center_chan1, NULL);
  325. rpc_iwinfo_call_int("center_chan2", iw->center_chan2, NULL);
  326. rpc_iwinfo_call_int("frequency", iw->frequency, NULL);
  327. rpc_iwinfo_call_int("frequency_offset", iw->frequency_offset, NULL);
  328. rpc_iwinfo_call_int("txpower", iw->txpower, NULL);
  329. rpc_iwinfo_call_int("txpower_offset", iw->txpower_offset, NULL);
  330. rpc_iwinfo_call_int("quality", iw->quality, NULL);
  331. rpc_iwinfo_call_int("quality_max", iw->quality_max, NULL);
  332. rpc_iwinfo_call_int("signal", iw->signal, NULL);
  333. rpc_iwinfo_call_int("noise", iw->noise, NULL);
  334. rpc_iwinfo_call_int("bitrate", iw->bitrate, NULL);
  335. rpc_iwinfo_call_encryption("encryption");
  336. rpc_iwinfo_call_htmodes("htmodes");
  337. rpc_iwinfo_call_hwmodes("hwmodes");
  338. rpc_iwinfo_call_hw_ht_mode();
  339. c = blobmsg_open_table(&buf, "hardware");
  340. rpc_iwinfo_call_hardware_id("id");
  341. rpc_iwinfo_call_str("name", iw->hardware_name);
  342. blobmsg_close_table(&buf, c);
  343. ubus_send_reply(ctx, req, buf.head);
  344. rpc_iwinfo_close();
  345. return UBUS_STATUS_OK;
  346. }
  347. static int
  348. rpc_iwinfo_scan(struct ubus_context *ctx, struct ubus_object *obj,
  349. struct ubus_request_data *req, const char *method,
  350. struct blob_attr *msg)
  351. {
  352. int i, rv, len;
  353. void *c, *d, *t;
  354. char mac[18];
  355. char res[IWINFO_BUFSIZE];
  356. struct iwinfo_scanlist_entry *e;
  357. rv = rpc_iwinfo_open(msg);
  358. if (rv)
  359. return rv;
  360. blob_buf_init(&buf, 0);
  361. c = blobmsg_open_array(&buf, "results");
  362. if (!iw->scanlist(ifname, res, &len) && (len > 0))
  363. {
  364. for (i = 0; i < len; i += sizeof(struct iwinfo_scanlist_entry))
  365. {
  366. e = (struct iwinfo_scanlist_entry *)&res[i];
  367. d = blobmsg_open_table(&buf, NULL);
  368. if (e->ssid[0])
  369. blobmsg_add_string(&buf, "ssid", (const char *)e->ssid);
  370. snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
  371. e->mac[0], e->mac[1], e->mac[2],
  372. e->mac[3], e->mac[4], e->mac[5]);
  373. blobmsg_add_string(&buf, "bssid", mac);
  374. blobmsg_add_string(&buf, "mode", IWINFO_OPMODE_NAMES[e->mode]);
  375. blobmsg_add_u32(&buf, "channel", e->channel);
  376. blobmsg_add_u32(&buf, "signal", (uint32_t)(e->signal - 0x100));
  377. blobmsg_add_u32(&buf, "quality", e->quality);
  378. blobmsg_add_u32(&buf, "quality_max", e->quality_max);
  379. if (e->ht_chan_info.primary_chan) {
  380. t = blobmsg_open_table(&buf, "ht_operation");
  381. blobmsg_add_u32(&buf, "primary_channel", e->ht_chan_info.primary_chan);
  382. blobmsg_add_string(&buf, "secondary_channel_offset", ht_secondary_offset[e->ht_chan_info.secondary_chan_off]);
  383. blobmsg_add_u32(&buf, "channel_width", ht_chan_width[e->ht_chan_info.chan_width]);
  384. blobmsg_close_table(&buf, t);
  385. }
  386. if (e->vht_chan_info.center_chan_1) {
  387. t = blobmsg_open_table(&buf, "vht_operation");
  388. blobmsg_add_u32(&buf, "channel_width", vht_chan_width[e->vht_chan_info.chan_width]);
  389. blobmsg_add_u32(&buf, "center_freq_1", e->vht_chan_info.center_chan_1);
  390. blobmsg_add_u32(&buf, "center_freq_2", e->vht_chan_info.center_chan_2);
  391. blobmsg_close_table(&buf, t);
  392. }
  393. rpc_iwinfo_add_encryption("encryption", &e->crypto);
  394. blobmsg_close_table(&buf, d);
  395. }
  396. }
  397. blobmsg_close_array(&buf, c);
  398. ubus_send_reply(ctx, req, buf.head);
  399. rpc_iwinfo_close();
  400. return UBUS_STATUS_OK;
  401. }
  402. static void
  403. rpc_iwinfo_add_rateinfo(struct iwinfo_rate_entry *r)
  404. {
  405. blobmsg_add_u8(&buf, "ht", r->is_ht);
  406. blobmsg_add_u8(&buf, "vht", r->is_vht);
  407. blobmsg_add_u8(&buf, "he", r->is_he);
  408. blobmsg_add_u32(&buf, "mhz", r->mhz);
  409. blobmsg_add_u32(&buf, "rate", r->rate);
  410. if (r->is_ht) {
  411. blobmsg_add_u32(&buf, "mcs", r->mcs);
  412. blobmsg_add_u8(&buf, "40mhz", r->is_40mhz);
  413. blobmsg_add_u8(&buf, "short_gi", r->is_short_gi);
  414. }
  415. else if (r->is_vht) {
  416. blobmsg_add_u32(&buf, "mcs", r->mcs);
  417. blobmsg_add_u32(&buf, "nss", r->nss);
  418. blobmsg_add_u8(&buf, "short_gi", r->is_short_gi);
  419. }
  420. else if (r->is_he) {
  421. blobmsg_add_u32(&buf, "mcs", r->mcs);
  422. blobmsg_add_u32(&buf, "nss", r->nss);
  423. blobmsg_add_u32(&buf, "he_gi", r->he_gi);
  424. blobmsg_add_u32(&buf, "he_dcm", r->he_dcm);
  425. }
  426. }
  427. static int
  428. rpc_iwinfo_assoclist(struct ubus_context *ctx, struct ubus_object *obj,
  429. struct ubus_request_data *req, const char *method,
  430. struct blob_attr *msg)
  431. {
  432. int i, rv, len;
  433. char mac[18];
  434. char res[IWINFO_BUFSIZE];
  435. struct iwinfo_assoclist_entry *a;
  436. struct ether_addr *macaddr = NULL;
  437. void *c = NULL, *d, *e;
  438. struct blob_attr *tb[__RPC_A_MAX];
  439. bool found = false;
  440. blobmsg_parse(rpc_assoclist_policy, __RPC_A_MAX, tb,
  441. blob_data(msg), blob_len(msg));
  442. rv = __rpc_iwinfo_open(tb[RPC_A_DEVICE]);
  443. if (rv)
  444. return rv;
  445. if (tb[RPC_A_MACADDR])
  446. macaddr = ether_aton(blobmsg_data(tb[RPC_A_MACADDR]));
  447. blob_buf_init(&buf, 0);
  448. if (!macaddr)
  449. c = blobmsg_open_array(&buf, "results");
  450. if (!iw->assoclist(ifname, res, &len) && (len > 0))
  451. {
  452. for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
  453. {
  454. a = (struct iwinfo_assoclist_entry *)&res[i];
  455. if (!macaddr)
  456. d = blobmsg_open_table(&buf, NULL);
  457. else if (memcmp(macaddr, a->mac, ETH_ALEN) != 0)
  458. continue;
  459. snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
  460. a->mac[0], a->mac[1], a->mac[2],
  461. a->mac[3], a->mac[4], a->mac[5]);
  462. blobmsg_add_string(&buf, "mac", mac);
  463. blobmsg_add_u32(&buf, "signal", a->signal);
  464. blobmsg_add_u32(&buf, "signal_avg", a->signal_avg);
  465. blobmsg_add_u32(&buf, "noise", a->noise);
  466. blobmsg_add_u32(&buf, "inactive", a->inactive);
  467. blobmsg_add_u32(&buf, "connected_time", a->connected_time);
  468. blobmsg_add_u32(&buf, "thr", a->thr);
  469. blobmsg_add_u8(&buf, "authorized", a->is_authorized);
  470. blobmsg_add_u8(&buf, "authenticated", a->is_authenticated);
  471. blobmsg_add_string(&buf, "preamble", a->is_preamble_short ? "short" : "long");
  472. blobmsg_add_u8(&buf, "wme", a->is_wme);
  473. blobmsg_add_u8(&buf, "mfp", a->is_mfp);
  474. blobmsg_add_u8(&buf, "tdls", a->is_tdls);
  475. blobmsg_add_u16(&buf, "mesh llid", a->llid);
  476. blobmsg_add_u16(&buf, "mesh plid", a->plid);
  477. blobmsg_add_string(&buf, "mesh plink", a->plink_state);
  478. blobmsg_add_string(&buf, "mesh local PS", a->local_ps);
  479. blobmsg_add_string(&buf, "mesh peer PS", a->peer_ps);
  480. blobmsg_add_string(&buf, "mesh non-peer PS", a->nonpeer_ps);
  481. e = blobmsg_open_table(&buf, "rx");
  482. blobmsg_add_u64(&buf, "drop_misc", a->rx_drop_misc);
  483. blobmsg_add_u32(&buf, "packets", a->rx_packets);
  484. blobmsg_add_u32(&buf, "bytes", a->rx_bytes);
  485. rpc_iwinfo_add_rateinfo(&a->rx_rate);
  486. blobmsg_close_table(&buf, e);
  487. e = blobmsg_open_table(&buf, "tx");
  488. blobmsg_add_u32(&buf, "failed", a->tx_failed);
  489. blobmsg_add_u32(&buf, "retries", a->tx_retries);
  490. blobmsg_add_u32(&buf, "packets", a->tx_packets);
  491. blobmsg_add_u32(&buf, "bytes", a->tx_bytes);
  492. rpc_iwinfo_add_rateinfo(&a->tx_rate);
  493. blobmsg_close_table(&buf, e);
  494. found = true;
  495. if (!macaddr)
  496. blobmsg_close_table(&buf, d);
  497. else
  498. break;
  499. }
  500. }
  501. if (!macaddr)
  502. blobmsg_close_array(&buf, c);
  503. else if (!found)
  504. return UBUS_STATUS_NOT_FOUND;
  505. ubus_send_reply(ctx, req, buf.head);
  506. rpc_iwinfo_close();
  507. return UBUS_STATUS_OK;
  508. }
  509. static int
  510. rpc_iwinfo_survey(struct ubus_context *ctx, struct ubus_object *obj,
  511. struct ubus_request_data *req, const char *method,
  512. struct blob_attr *msg)
  513. {
  514. char res[IWINFO_BUFSIZE];
  515. struct iwinfo_survey_entry *e;
  516. void *c, *d;
  517. int i, rv, len;
  518. blob_buf_init(&buf, 0);
  519. rv = rpc_iwinfo_open(msg);
  520. c = blobmsg_open_array(&buf, "results");
  521. if (rv || iw->survey(ifname, res, &len) || len < 0)
  522. return UBUS_STATUS_OK;
  523. for (i = 0; i < len; i += sizeof(struct iwinfo_survey_entry)) {
  524. e = (struct iwinfo_survey_entry *)&res[i];
  525. d = blobmsg_open_table(&buf, NULL);
  526. blobmsg_add_u32(&buf, "mhz", e->mhz);
  527. blobmsg_add_u32(&buf, "noise", e->noise);
  528. blobmsg_add_u64(&buf, "active_time", e->active_time);
  529. blobmsg_add_u64(&buf, "busy_time", e->busy_time);
  530. blobmsg_add_u64(&buf, "busy_time_ext", e->busy_time_ext);
  531. blobmsg_add_u64(&buf, "rx_time", e->rxtime);
  532. blobmsg_add_u64(&buf, "tx_time", e->txtime);
  533. blobmsg_close_table(&buf, d);
  534. }
  535. blobmsg_close_array(&buf, c);
  536. ubus_send_reply(ctx, req, buf.head);
  537. rpc_iwinfo_close();
  538. return UBUS_STATUS_OK;
  539. }
  540. static int
  541. rpc_iwinfo_freqlist(struct ubus_context *ctx, struct ubus_object *obj,
  542. struct ubus_request_data *req, const char *method,
  543. struct blob_attr *msg)
  544. {
  545. int i, rv, len, ch;
  546. char res[IWINFO_BUFSIZE];
  547. struct iwinfo_freqlist_entry *f;
  548. void *c, *d;
  549. rv = rpc_iwinfo_open(msg);
  550. if (rv)
  551. return rv;
  552. blob_buf_init(&buf, 0);
  553. c = blobmsg_open_array(&buf, "results");
  554. if (!iw->freqlist(ifname, res, &len) && (len > 0))
  555. {
  556. if (iw->channel(ifname, &ch))
  557. ch = -1;
  558. for (i = 0; i < len; i += sizeof(struct iwinfo_freqlist_entry))
  559. {
  560. f = (struct iwinfo_freqlist_entry *)&res[i];
  561. d = blobmsg_open_table(&buf, NULL);
  562. blobmsg_add_u32(&buf, "channel", f->channel);
  563. blobmsg_add_u32(&buf, "mhz", f->mhz);
  564. blobmsg_add_u8(&buf, "restricted", f->restricted);
  565. if (ch > -1)
  566. blobmsg_add_u8(&buf, "active", f->channel == ch);
  567. blobmsg_close_table(&buf, d);
  568. }
  569. }
  570. blobmsg_close_array(&buf, c);
  571. ubus_send_reply(ctx, req, buf.head);
  572. rpc_iwinfo_close();
  573. return UBUS_STATUS_OK;
  574. }
  575. static int
  576. rpc_iwinfo_txpowerlist(struct ubus_context *ctx, struct ubus_object *obj,
  577. struct ubus_request_data *req, const char *method,
  578. struct blob_attr *msg)
  579. {
  580. int i, rv, len, pwr, off;
  581. char res[IWINFO_BUFSIZE];
  582. struct iwinfo_txpwrlist_entry *t;
  583. void *c, *d;
  584. rv = rpc_iwinfo_open(msg);
  585. if (rv)
  586. return rv;
  587. blob_buf_init(&buf, 0);
  588. c = blobmsg_open_array(&buf, "results");
  589. if (!iw->txpwrlist(ifname, res, &len) && (len > 0))
  590. {
  591. if (iw->txpower(ifname, &pwr))
  592. pwr = -1;
  593. if (iw->txpower_offset(ifname, &off))
  594. off = 0;
  595. for (i = 0; i < len; i += sizeof(struct iwinfo_txpwrlist_entry))
  596. {
  597. t = (struct iwinfo_txpwrlist_entry *)&res[i];
  598. d = blobmsg_open_table(&buf, NULL);
  599. blobmsg_add_u32(&buf, "dbm", t->dbm + off);
  600. blobmsg_add_u32(&buf, "mw", iwinfo_dbm2mw(t->dbm + off));
  601. if (pwr > -1)
  602. blobmsg_add_u8(&buf, "active", t->dbm == pwr);
  603. blobmsg_close_table(&buf, d);
  604. }
  605. }
  606. blobmsg_close_array(&buf, c);
  607. ubus_send_reply(ctx, req, buf.head);
  608. rpc_iwinfo_close();
  609. return UBUS_STATUS_OK;
  610. }
  611. static const char *
  612. rpc_iwinfo_lookup_country(char *buf, int len, int iso3166)
  613. {
  614. int i;
  615. static char ccode[5];
  616. struct iwinfo_country_entry *c;
  617. for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
  618. {
  619. c = (struct iwinfo_country_entry *)&buf[i];
  620. if (c->iso3166 == iso3166)
  621. {
  622. snprintf(ccode, sizeof(ccode), "%s", c->ccode);
  623. return ccode;
  624. }
  625. }
  626. return NULL;
  627. }
  628. static int
  629. rpc_iwinfo_countrylist(struct ubus_context *ctx, struct ubus_object *obj,
  630. struct ubus_request_data *req, const char *method,
  631. struct blob_attr *msg)
  632. {
  633. int rv, len;
  634. char cur[3];
  635. char iso3166[3];
  636. char res[IWINFO_BUFSIZE] = {0};
  637. const char *ccode;
  638. const struct iwinfo_iso3166_label *l;
  639. void *c, *d;
  640. rv = rpc_iwinfo_open(msg);
  641. if (rv)
  642. return rv;
  643. blob_buf_init(&buf, 0);
  644. c = blobmsg_open_array(&buf, "results");
  645. if (!iw->countrylist(ifname, res, &len) && (len > 0))
  646. {
  647. if (iw->country(ifname, cur))
  648. memset(cur, 0, sizeof(cur));
  649. for (l = IWINFO_ISO3166_NAMES; l->iso3166; l++)
  650. {
  651. ccode = rpc_iwinfo_lookup_country(res, len, l->iso3166);
  652. if (!ccode)
  653. continue;
  654. d = blobmsg_open_table(&buf, NULL);
  655. blobmsg_add_string(&buf, "code", ccode);
  656. blobmsg_add_string(&buf, "country", (const char *)l->name);
  657. snprintf(iso3166, sizeof(iso3166), "%c%c",
  658. (l->iso3166 / 256), (l->iso3166 % 256));
  659. blobmsg_add_string(&buf, "iso3166", iso3166);
  660. if (cur[0])
  661. blobmsg_add_u8(&buf, "active", !strncmp(ccode, cur, 2));
  662. blobmsg_close_table(&buf, d);
  663. }
  664. }
  665. blobmsg_close_array(&buf, c);
  666. ubus_send_reply(ctx, req, buf.head);
  667. rpc_iwinfo_close();
  668. return UBUS_STATUS_OK;
  669. }
  670. static int
  671. rpc_iwinfo_phyname(struct ubus_context *ctx, struct ubus_object *obj,
  672. struct ubus_request_data *req, const char *method,
  673. struct blob_attr *msg)
  674. {
  675. int i;
  676. bool found = false;
  677. char res[IWINFO_BUFSIZE];
  678. const struct iwinfo_ops *ops;
  679. struct blob_attr *tb[__RPC_U_MAX];
  680. const char *backends[] = {
  681. "nl80211",
  682. "madwifi",
  683. "wl"
  684. };
  685. blobmsg_parse(rpc_uci_policy, __RPC_U_MAX, tb,
  686. blob_data(msg), blob_len(msg));
  687. if (!tb[RPC_U_SECTION])
  688. return UBUS_STATUS_INVALID_ARGUMENT;
  689. for (i = 0; i < ARRAY_SIZE(backends); i++)
  690. {
  691. ops = iwinfo_backend_by_name(backends[i]);
  692. if (!ops || !ops->lookup_phy)
  693. continue;
  694. if (!ops->lookup_phy(blobmsg_get_string(tb[RPC_U_SECTION]), res))
  695. {
  696. found = true;
  697. break;
  698. }
  699. }
  700. if (found)
  701. {
  702. blob_buf_init(&buf, 0);
  703. blobmsg_add_string(&buf, "phyname", res);
  704. ubus_send_reply(ctx, req, buf.head);
  705. }
  706. rpc_iwinfo_close();
  707. return found ? UBUS_STATUS_OK : UBUS_STATUS_NOT_FOUND;
  708. }
  709. static int
  710. rpc_iwinfo_devices(struct ubus_context *ctx, struct ubus_object *obj,
  711. struct ubus_request_data *req, const char *method,
  712. struct blob_attr *msg)
  713. {
  714. void *c;
  715. struct dirent *e;
  716. DIR *d;
  717. d = opendir("/sys/class/net");
  718. if (!d)
  719. return UBUS_STATUS_UNKNOWN_ERROR;
  720. blob_buf_init(&buf, 0);
  721. c = blobmsg_open_array(&buf, "devices");
  722. while ((e = readdir(d)) != NULL)
  723. {
  724. if (e->d_type != DT_LNK)
  725. continue;
  726. if (iwinfo_type(e->d_name))
  727. blobmsg_add_string(&buf, NULL, e->d_name);
  728. }
  729. blobmsg_close_array(&buf, c);
  730. closedir(d);
  731. ubus_send_reply(ctx, req, buf.head);
  732. rpc_iwinfo_close();
  733. return UBUS_STATUS_OK;
  734. }
  735. static int
  736. rpc_iwinfo_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
  737. {
  738. static const struct ubus_method iwinfo_methods[] = {
  739. UBUS_METHOD_NOARG("devices", rpc_iwinfo_devices),
  740. UBUS_METHOD("info", rpc_iwinfo_info, rpc_device_policy),
  741. UBUS_METHOD("scan", rpc_iwinfo_scan, rpc_device_policy),
  742. UBUS_METHOD("assoclist", rpc_iwinfo_assoclist, rpc_assoclist_policy),
  743. UBUS_METHOD("freqlist", rpc_iwinfo_freqlist, rpc_device_policy),
  744. UBUS_METHOD("txpowerlist", rpc_iwinfo_txpowerlist, rpc_device_policy),
  745. UBUS_METHOD("countrylist", rpc_iwinfo_countrylist, rpc_device_policy),
  746. UBUS_METHOD("survey", rpc_iwinfo_survey, rpc_device_policy),
  747. UBUS_METHOD("phyname", rpc_iwinfo_phyname, rpc_uci_policy),
  748. };
  749. static struct ubus_object_type iwinfo_type =
  750. UBUS_OBJECT_TYPE("rpcd-plugin-iwinfo", iwinfo_methods);
  751. static struct ubus_object obj = {
  752. .name = "iwinfo",
  753. .type = &iwinfo_type,
  754. .methods = iwinfo_methods,
  755. .n_methods = ARRAY_SIZE(iwinfo_methods),
  756. };
  757. return ubus_add_object(ctx, &obj);
  758. }
  759. struct rpc_plugin rpc_plugin = {
  760. .init = rpc_iwinfo_api_init
  761. };