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.
 
 
 

3237 lines
74 KiB

  1. /*
  2. * iwinfo - Wireless Information Library - NL80211 Backend
  3. *
  4. * Copyright (C) 2010-2013 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. * Parts of this code are derived from the Linux iw utility.
  23. */
  24. #include <limits.h>
  25. #include <glob.h>
  26. #include <fnmatch.h>
  27. #include <stdarg.h>
  28. #include <stdlib.h>
  29. #include "iwinfo_nl80211.h"
  30. #define min(x, y) ((x) < (y)) ? (x) : (y)
  31. #define BIT(x) (1ULL<<(x))
  32. static struct nl80211_state *nls = NULL;
  33. static void nl80211_close(void)
  34. {
  35. if (nls)
  36. {
  37. if (nls->nlctrl)
  38. genl_family_put(nls->nlctrl);
  39. if (nls->nl80211)
  40. genl_family_put(nls->nl80211);
  41. if (nls->nl_sock)
  42. nl_socket_free(nls->nl_sock);
  43. if (nls->nl_cache)
  44. nl_cache_free(nls->nl_cache);
  45. free(nls);
  46. nls = NULL;
  47. }
  48. }
  49. static int nl80211_init(void)
  50. {
  51. int err, fd;
  52. if (!nls)
  53. {
  54. nls = malloc(sizeof(struct nl80211_state));
  55. if (!nls) {
  56. err = -ENOMEM;
  57. goto err;
  58. }
  59. memset(nls, 0, sizeof(*nls));
  60. nls->nl_sock = nl_socket_alloc();
  61. if (!nls->nl_sock) {
  62. err = -ENOMEM;
  63. goto err;
  64. }
  65. if (genl_connect(nls->nl_sock)) {
  66. err = -ENOLINK;
  67. goto err;
  68. }
  69. fd = nl_socket_get_fd(nls->nl_sock);
  70. if (fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC) < 0) {
  71. err = -EINVAL;
  72. goto err;
  73. }
  74. if (genl_ctrl_alloc_cache(nls->nl_sock, &nls->nl_cache)) {
  75. err = -ENOMEM;
  76. goto err;
  77. }
  78. nls->nl80211 = genl_ctrl_search_by_name(nls->nl_cache, "nl80211");
  79. if (!nls->nl80211) {
  80. err = -ENOENT;
  81. goto err;
  82. }
  83. nls->nlctrl = genl_ctrl_search_by_name(nls->nl_cache, "nlctrl");
  84. if (!nls->nlctrl) {
  85. err = -ENOENT;
  86. goto err;
  87. }
  88. }
  89. return 0;
  90. err:
  91. nl80211_close();
  92. return err;
  93. }
  94. static int nl80211_readint(const char *path)
  95. {
  96. int fd;
  97. int rv = -1;
  98. char buffer[16];
  99. if ((fd = open(path, O_RDONLY)) > -1)
  100. {
  101. if (read(fd, buffer, sizeof(buffer)) > 0)
  102. rv = atoi(buffer);
  103. close(fd);
  104. }
  105. return rv;
  106. }
  107. static int nl80211_readstr(const char *path, char *buffer, int length)
  108. {
  109. int fd;
  110. int rv = -1;
  111. if ((fd = open(path, O_RDONLY)) > -1)
  112. {
  113. if ((rv = read(fd, buffer, length - 1)) > 0)
  114. {
  115. if (buffer[rv - 1] == '\n')
  116. rv--;
  117. buffer[rv] = 0;
  118. }
  119. close(fd);
  120. }
  121. return rv;
  122. }
  123. static int nl80211_msg_error(struct sockaddr_nl *nla,
  124. struct nlmsgerr *err, void *arg)
  125. {
  126. int *ret = arg;
  127. *ret = err->error;
  128. return NL_STOP;
  129. }
  130. static int nl80211_msg_finish(struct nl_msg *msg, void *arg)
  131. {
  132. int *ret = arg;
  133. *ret = 0;
  134. return NL_SKIP;
  135. }
  136. static int nl80211_msg_ack(struct nl_msg *msg, void *arg)
  137. {
  138. int *ret = arg;
  139. *ret = 0;
  140. return NL_STOP;
  141. }
  142. static int nl80211_msg_response(struct nl_msg *msg, void *arg)
  143. {
  144. return NL_SKIP;
  145. }
  146. static void nl80211_free(struct nl80211_msg_conveyor *cv)
  147. {
  148. if (cv)
  149. {
  150. if (cv->cb)
  151. nl_cb_put(cv->cb);
  152. if (cv->msg)
  153. nlmsg_free(cv->msg);
  154. cv->cb = NULL;
  155. cv->msg = NULL;
  156. }
  157. }
  158. static struct nl80211_msg_conveyor * nl80211_new(struct genl_family *family,
  159. int cmd, int flags)
  160. {
  161. static struct nl80211_msg_conveyor cv;
  162. struct nl_msg *req = NULL;
  163. struct nl_cb *cb = NULL;
  164. req = nlmsg_alloc();
  165. if (!req)
  166. goto err;
  167. cb = nl_cb_alloc(NL_CB_DEFAULT);
  168. if (!cb)
  169. goto err;
  170. genlmsg_put(req, 0, 0, genl_family_get_id(family), 0, flags, cmd, 0);
  171. cv.msg = req;
  172. cv.cb = cb;
  173. return &cv;
  174. err:
  175. if (req)
  176. nlmsg_free(req);
  177. return NULL;
  178. }
  179. static struct nl80211_msg_conveyor * nl80211_ctl(int cmd, int flags)
  180. {
  181. if (nl80211_init() < 0)
  182. return NULL;
  183. return nl80211_new(nls->nlctrl, cmd, flags);
  184. }
  185. static int nl80211_phy_idx_from_uci_path(struct uci_section *s)
  186. {
  187. size_t linklen, pathlen;
  188. char buf[128], *link;
  189. struct dirent *e;
  190. const char *path;
  191. int idx = -1;
  192. DIR *d;
  193. path = uci_lookup_option_string(uci_ctx, s, "path");
  194. if (!path)
  195. return -1;
  196. if ((d = opendir("/sys/class/ieee80211")) != NULL)
  197. {
  198. while ((e = readdir(d)) != NULL)
  199. {
  200. snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/device", e->d_name);
  201. link = realpath(buf, NULL);
  202. if (link == NULL)
  203. continue;
  204. linklen = strlen(link);
  205. pathlen = strlen(path);
  206. if (pathlen >= linklen || strcmp(link + (linklen - pathlen), path))
  207. linklen = 0;
  208. free(link);
  209. if (linklen == 0)
  210. continue;
  211. snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", e->d_name);
  212. idx = nl80211_readint(buf);
  213. if (idx >= 0)
  214. break;
  215. }
  216. closedir(d);
  217. }
  218. return idx;
  219. }
  220. static int nl80211_phy_idx_from_uci_macaddr(struct uci_section *s)
  221. {
  222. const char *opt;
  223. char buf[128];
  224. int i, idx = -1;
  225. glob_t gl;
  226. opt = uci_lookup_option_string(uci_ctx, s, "macaddr");
  227. if (!opt)
  228. return -1;
  229. snprintf(buf, sizeof(buf), "/sys/class/ieee80211/*"); /**/
  230. if (glob(buf, 0, NULL, &gl))
  231. return -1;
  232. for (i = 0; i < gl.gl_pathc; i++)
  233. {
  234. snprintf(buf, sizeof(buf), "%s/macaddress", gl.gl_pathv[i]);
  235. if (nl80211_readstr(buf, buf, sizeof(buf)) <= 0)
  236. continue;
  237. if (fnmatch(opt, buf, FNM_CASEFOLD))
  238. continue;
  239. snprintf(buf, sizeof(buf), "%s/index", gl.gl_pathv[i]);
  240. if ((idx = nl80211_readint(buf)) > -1)
  241. break;
  242. }
  243. globfree(&gl);
  244. return idx;
  245. }
  246. static int nl80211_phy_idx_from_uci_phy(struct uci_section *s)
  247. {
  248. const char *opt;
  249. char buf[128];
  250. opt = uci_lookup_option_string(uci_ctx, s, "phy");
  251. if (!opt)
  252. return -1;
  253. snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", opt);
  254. return nl80211_readint(buf);
  255. }
  256. static int nl80211_phy_idx_from_uci(const char *name)
  257. {
  258. struct uci_section *s;
  259. int idx = -1;
  260. s = iwinfo_uci_get_radio(name, "mac80211");
  261. if (!s)
  262. goto free;
  263. idx = nl80211_phy_idx_from_uci_path(s);
  264. if (idx < 0)
  265. idx = nl80211_phy_idx_from_uci_macaddr(s);
  266. if (idx < 0)
  267. idx = nl80211_phy_idx_from_uci_phy(s);
  268. free:
  269. iwinfo_uci_free();
  270. return idx;
  271. }
  272. static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname,
  273. int cmd, int flags)
  274. {
  275. int ifidx = -1, phyidx = -1;
  276. struct nl80211_msg_conveyor *cv;
  277. if (ifname == NULL)
  278. return NULL;
  279. if (nl80211_init() < 0)
  280. return NULL;
  281. if (!strncmp(ifname, "phy", 3))
  282. phyidx = atoi(&ifname[3]);
  283. else if (!strncmp(ifname, "radio", 5))
  284. phyidx = nl80211_phy_idx_from_uci(ifname);
  285. if (!strncmp(ifname, "mon.", 4))
  286. ifidx = if_nametoindex(&ifname[4]);
  287. else
  288. ifidx = if_nametoindex(ifname);
  289. /* Valid ifidx must be greater than 0 */
  290. if ((ifidx <= 0) && (phyidx < 0))
  291. return NULL;
  292. cv = nl80211_new(nls->nl80211, cmd, flags);
  293. if (!cv)
  294. return NULL;
  295. if (ifidx > 0)
  296. NLA_PUT_U32(cv->msg, NL80211_ATTR_IFINDEX, ifidx);
  297. else if (phyidx > -1)
  298. NLA_PUT_U32(cv->msg, NL80211_ATTR_WIPHY, phyidx);
  299. return cv;
  300. nla_put_failure:
  301. nl80211_free(cv);
  302. return NULL;
  303. }
  304. static int nl80211_send(struct nl80211_msg_conveyor *cv,
  305. int (*cb_func)(struct nl_msg *, void *),
  306. void *cb_arg)
  307. {
  308. static struct nl80211_msg_conveyor rcv;
  309. int err;
  310. if (cb_func)
  311. nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, cb_func, cb_arg);
  312. else
  313. nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, nl80211_msg_response, &rcv);
  314. err = nl_send_auto_complete(nls->nl_sock, cv->msg);
  315. if (err < 0)
  316. goto out;
  317. err = 1;
  318. nl_cb_err(cv->cb, NL_CB_CUSTOM, nl80211_msg_error, &err);
  319. nl_cb_set(cv->cb, NL_CB_FINISH, NL_CB_CUSTOM, nl80211_msg_finish, &err);
  320. nl_cb_set(cv->cb, NL_CB_ACK, NL_CB_CUSTOM, nl80211_msg_ack, &err);
  321. while (err > 0)
  322. nl_recvmsgs(nls->nl_sock, cv->cb);
  323. out:
  324. nl80211_free(cv);
  325. return err;
  326. }
  327. static int nl80211_request(const char *ifname, int cmd, int flags,
  328. int (*cb_func)(struct nl_msg *, void *),
  329. void *cb_arg)
  330. {
  331. struct nl80211_msg_conveyor *cv;
  332. cv = nl80211_msg(ifname, cmd, flags);
  333. if (!cv)
  334. return -ENOMEM;
  335. return nl80211_send(cv, cb_func, cb_arg);
  336. }
  337. static struct nlattr ** nl80211_parse(struct nl_msg *msg)
  338. {
  339. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  340. static struct nlattr *attr[NL80211_ATTR_MAX + 1];
  341. nla_parse(attr, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  342. genlmsg_attrlen(gnlh, 0), NULL);
  343. return attr;
  344. }
  345. static int nl80211_get_protocol_features_cb(struct nl_msg *msg, void *arg)
  346. {
  347. uint32_t *features = arg;
  348. struct nlattr **attr = nl80211_parse(msg);
  349. if (attr[NL80211_ATTR_PROTOCOL_FEATURES])
  350. *features = nla_get_u32(attr[NL80211_ATTR_PROTOCOL_FEATURES]);
  351. return NL_SKIP;
  352. }
  353. static int nl80211_get_protocol_features(const char *ifname)
  354. {
  355. struct nl80211_msg_conveyor *req;
  356. uint32_t features = 0;
  357. req = nl80211_msg(ifname, NL80211_CMD_GET_PROTOCOL_FEATURES, 0);
  358. if (req) {
  359. nl80211_send(req, nl80211_get_protocol_features_cb, &features);
  360. nl80211_free(req);
  361. }
  362. return features;
  363. }
  364. static int nl80211_subscribe_cb(struct nl_msg *msg, void *arg)
  365. {
  366. struct nl80211_group_conveyor *cv = arg;
  367. struct nlattr **attr = nl80211_parse(msg);
  368. struct nlattr *mgrpinfo[CTRL_ATTR_MCAST_GRP_MAX + 1];
  369. struct nlattr *mgrp;
  370. int mgrpidx;
  371. if (!attr[CTRL_ATTR_MCAST_GROUPS])
  372. return NL_SKIP;
  373. nla_for_each_nested(mgrp, attr[CTRL_ATTR_MCAST_GROUPS], mgrpidx)
  374. {
  375. nla_parse(mgrpinfo, CTRL_ATTR_MCAST_GRP_MAX,
  376. nla_data(mgrp), nla_len(mgrp), NULL);
  377. if (mgrpinfo[CTRL_ATTR_MCAST_GRP_ID] &&
  378. mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME] &&
  379. !strncmp(nla_data(mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME]),
  380. cv->name, nla_len(mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME])))
  381. {
  382. cv->id = nla_get_u32(mgrpinfo[CTRL_ATTR_MCAST_GRP_ID]);
  383. break;
  384. }
  385. }
  386. return NL_SKIP;
  387. }
  388. static int nl80211_subscribe(const char *family, const char *group)
  389. {
  390. struct nl80211_group_conveyor cv = { .name = group, .id = -ENOENT };
  391. struct nl80211_msg_conveyor *req;
  392. int err;
  393. req = nl80211_ctl(CTRL_CMD_GETFAMILY, 0);
  394. if (req)
  395. {
  396. NLA_PUT_STRING(req->msg, CTRL_ATTR_FAMILY_NAME, family);
  397. err = nl80211_send(req, nl80211_subscribe_cb, &cv);
  398. if (err)
  399. return err;
  400. return nl_socket_add_membership(nls->nl_sock, cv.id);
  401. nla_put_failure:
  402. nl80211_free(req);
  403. }
  404. return -ENOMEM;
  405. }
  406. static int nl80211_wait_cb(struct nl_msg *msg, void *arg)
  407. {
  408. struct nl80211_event_conveyor *cv = arg;
  409. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  410. if (cv->wait[gnlh->cmd / 32] & (1 << (gnlh->cmd % 32)))
  411. cv->recv = gnlh->cmd;
  412. return NL_SKIP;
  413. }
  414. static int nl80211_wait_seq_check(struct nl_msg *msg, void *arg)
  415. {
  416. return NL_OK;
  417. }
  418. static int __nl80211_wait(const char *family, const char *group, ...)
  419. {
  420. struct nl80211_event_conveyor cv = { };
  421. struct nl_cb *cb;
  422. int err = 0;
  423. int cmd;
  424. va_list ap;
  425. if (nl80211_subscribe(family, group))
  426. return -ENOENT;
  427. cb = nl_cb_alloc(NL_CB_DEFAULT);
  428. if (!cb)
  429. return -ENOMEM;
  430. nl_cb_err(cb, NL_CB_CUSTOM, nl80211_msg_error, &err);
  431. nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, nl80211_wait_seq_check, NULL);
  432. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, nl80211_wait_cb, &cv );
  433. va_start(ap, group);
  434. for (cmd = va_arg(ap, int); cmd != 0; cmd = va_arg(ap, int))
  435. cv.wait[cmd / 32] |= (1 << (cmd % 32));
  436. va_end(ap);
  437. while (!cv.recv && !err)
  438. nl_recvmsgs(nls->nl_sock, cb);
  439. nl_cb_put(cb);
  440. return err;
  441. }
  442. #define nl80211_wait(family, group, ...) \
  443. __nl80211_wait(family, group, __VA_ARGS__, 0)
  444. static int nl80211_freq2channel(int freq)
  445. {
  446. if (freq == 2484)
  447. return 14;
  448. else if (freq < 2484)
  449. return (freq - 2407) / 5;
  450. else if (freq >= 4910 && freq <= 4980)
  451. return (freq - 4000) / 5;
  452. else if(freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6)
  453. return (freq - 56160) / 2160;
  454. else
  455. return (freq - 5000) / 5;
  456. }
  457. static int nl80211_channel2freq(int channel, const char *band)
  458. {
  459. if (!band || band[0] != 'a')
  460. {
  461. if (channel == 14)
  462. return 2484;
  463. else if (channel < 14)
  464. return (channel * 5) + 2407;
  465. }
  466. else if ( strcmp(band, "ad") == 0)
  467. {
  468. return 56160 + 2160 * channel;
  469. }
  470. else
  471. {
  472. if (channel >= 182 && channel <= 196)
  473. return (channel * 5) + 4000;
  474. else
  475. return (channel * 5) + 5000;
  476. }
  477. return 0;
  478. }
  479. static int nl80211_ifname2phy_cb(struct nl_msg *msg, void *arg)
  480. {
  481. char *buf = arg;
  482. struct nlattr **attr = nl80211_parse(msg);
  483. if (attr[NL80211_ATTR_WIPHY_NAME])
  484. memcpy(buf, nla_data(attr[NL80211_ATTR_WIPHY_NAME]),
  485. nla_len(attr[NL80211_ATTR_WIPHY_NAME]));
  486. else
  487. buf[0] = 0;
  488. return NL_SKIP;
  489. }
  490. static char * nl80211_ifname2phy(const char *ifname)
  491. {
  492. static char phy[32] = { 0 };
  493. memset(phy, 0, sizeof(phy));
  494. nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
  495. nl80211_ifname2phy_cb, phy);
  496. return phy[0] ? phy : NULL;
  497. }
  498. static char * nl80211_phy2ifname(const char *ifname)
  499. {
  500. int ifidx = -1, cifidx = -1, phyidx = -1;
  501. char buffer[64];
  502. static char nif[IFNAMSIZ] = { 0 };
  503. DIR *d;
  504. struct dirent *e;
  505. /* Only accept phy name of the form phy%d or radio%d */
  506. if (!ifname)
  507. return NULL;
  508. else if (!strncmp(ifname, "phy", 3))
  509. phyidx = atoi(&ifname[3]);
  510. else if (!strncmp(ifname, "radio", 5))
  511. phyidx = nl80211_phy_idx_from_uci(ifname);
  512. else
  513. return NULL;
  514. memset(nif, 0, sizeof(nif));
  515. if (phyidx > -1)
  516. {
  517. if ((d = opendir("/sys/class/net")) != NULL)
  518. {
  519. while ((e = readdir(d)) != NULL)
  520. {
  521. snprintf(buffer, sizeof(buffer),
  522. "/sys/class/net/%s/phy80211/index", e->d_name);
  523. if (nl80211_readint(buffer) == phyidx)
  524. {
  525. snprintf(buffer, sizeof(buffer),
  526. "/sys/class/net/%s/ifindex", e->d_name);
  527. if ((cifidx = nl80211_readint(buffer)) >= 0 &&
  528. ((ifidx < 0) || (cifidx < ifidx)))
  529. {
  530. ifidx = cifidx;
  531. strncpy(nif, e->d_name, sizeof(nif) - 1);
  532. }
  533. }
  534. }
  535. closedir(d);
  536. }
  537. }
  538. return nif[0] ? nif : NULL;
  539. }
  540. static int nl80211_get_mode_cb(struct nl_msg *msg, void *arg)
  541. {
  542. int *mode = arg;
  543. struct nlattr **tb = nl80211_parse(msg);
  544. const int ifmodes[NL80211_IFTYPE_MAX + 1] = {
  545. IWINFO_OPMODE_UNKNOWN, /* unspecified */
  546. IWINFO_OPMODE_ADHOC, /* IBSS */
  547. IWINFO_OPMODE_CLIENT, /* managed */
  548. IWINFO_OPMODE_MASTER, /* AP */
  549. IWINFO_OPMODE_AP_VLAN, /* AP/VLAN */
  550. IWINFO_OPMODE_WDS, /* WDS */
  551. IWINFO_OPMODE_MONITOR, /* monitor */
  552. IWINFO_OPMODE_MESHPOINT, /* mesh point */
  553. IWINFO_OPMODE_P2P_CLIENT, /* P2P-client */
  554. IWINFO_OPMODE_P2P_GO, /* P2P-GO */
  555. };
  556. if (tb[NL80211_ATTR_IFTYPE])
  557. *mode = ifmodes[nla_get_u32(tb[NL80211_ATTR_IFTYPE])];
  558. return NL_SKIP;
  559. }
  560. static int nl80211_get_mode(const char *ifname, int *buf)
  561. {
  562. char *res;
  563. *buf = IWINFO_OPMODE_UNKNOWN;
  564. res = nl80211_phy2ifname(ifname);
  565. nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0,
  566. nl80211_get_mode_cb, buf);
  567. return (*buf == IWINFO_OPMODE_UNKNOWN) ? -1 : 0;
  568. }
  569. static int __nl80211_hostapd_query(const char *ifname, ...)
  570. {
  571. va_list ap, ap_cur;
  572. char *phy, *search, *dest, *key, *val, buf[128];
  573. int len, mode, found = 0, match = 1;
  574. FILE *fp;
  575. if (nl80211_get_mode(ifname, &mode))
  576. return 0;
  577. if (mode != IWINFO_OPMODE_MASTER && mode != IWINFO_OPMODE_AP_VLAN)
  578. return 0;
  579. phy = nl80211_ifname2phy(ifname);
  580. if (!phy)
  581. return 0;
  582. snprintf(buf, sizeof(buf), "/var/run/hostapd-%s.conf", phy);
  583. fp = fopen(buf, "r");
  584. if (!fp)
  585. return 0;
  586. va_start(ap, ifname);
  587. /* clear all destination buffers */
  588. va_copy(ap_cur, ap);
  589. while ((search = va_arg(ap_cur, char *)) != NULL)
  590. {
  591. dest = va_arg(ap_cur, char *);
  592. len = va_arg(ap_cur, int);
  593. memset(dest, 0, len);
  594. }
  595. va_end(ap_cur);
  596. /* iterate applicable lines and copy found values into dest buffers */
  597. while (fgets(buf, sizeof(buf), fp))
  598. {
  599. key = strtok(buf, " =\t\n");
  600. val = strtok(NULL, "\n");
  601. if (!key || !val || !*key || *key == '#')
  602. continue;
  603. if (!strcmp(key, "interface") || !strcmp(key, "bss"))
  604. match = !strcmp(ifname, val);
  605. if (!match)
  606. continue;
  607. va_copy(ap_cur, ap);
  608. while ((search = va_arg(ap_cur, char *)) != NULL)
  609. {
  610. dest = va_arg(ap_cur, char *);
  611. len = va_arg(ap_cur, int);
  612. if (!strcmp(search, key))
  613. {
  614. strncpy(dest, val, len - 1);
  615. found++;
  616. break;
  617. }
  618. }
  619. va_end(ap_cur);
  620. }
  621. fclose(fp);
  622. va_end(ap);
  623. return found;
  624. }
  625. #define nl80211_hostapd_query(ifname, ...) \
  626. __nl80211_hostapd_query(ifname, ##__VA_ARGS__, NULL)
  627. static inline int nl80211_wpactl_recv(int sock, char *buf, int blen)
  628. {
  629. fd_set rfds;
  630. struct timeval tv = { 0, 256000 };
  631. FD_ZERO(&rfds);
  632. FD_SET(sock, &rfds);
  633. memset(buf, 0, blen);
  634. if (select(sock + 1, &rfds, NULL, NULL, &tv) < 0)
  635. return -1;
  636. if (!FD_ISSET(sock, &rfds))
  637. return -1;
  638. return recv(sock, buf, blen - 1, 0);
  639. }
  640. static int nl80211_wpactl_connect(const char *ifname, struct sockaddr_un *local)
  641. {
  642. struct sockaddr_un remote = { 0 };
  643. size_t remote_length, local_length;
  644. int sock = socket(PF_UNIX, SOCK_DGRAM, 0);
  645. if (sock < 0)
  646. return sock;
  647. remote.sun_family = AF_UNIX;
  648. remote_length = sizeof(remote.sun_family) +
  649. sprintf(remote.sun_path, "/var/run/wpa_supplicant-%s/%s",
  650. ifname, ifname);
  651. if (fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC) < 0)
  652. {
  653. close(sock);
  654. return -1;
  655. }
  656. if (connect(sock, (struct sockaddr *)&remote, remote_length))
  657. {
  658. remote_length = sizeof(remote.sun_family) +
  659. sprintf(remote.sun_path, "/var/run/wpa_supplicant/%s", ifname);
  660. if (connect(sock, (struct sockaddr *)&remote, remote_length))
  661. {
  662. close(sock);
  663. return -1;
  664. }
  665. }
  666. local->sun_family = AF_UNIX;
  667. local_length = sizeof(local->sun_family) +
  668. sprintf(local->sun_path, "/var/run/iwinfo-%s-%d", ifname, getpid());
  669. if (bind(sock, (struct sockaddr *)local, local_length) < 0)
  670. {
  671. close(sock);
  672. return -1;
  673. }
  674. return sock;
  675. }
  676. static int __nl80211_wpactl_query(const char *ifname, ...)
  677. {
  678. va_list ap, ap_cur;
  679. struct sockaddr_un local = { 0 };
  680. int len, mode, found = 0, sock = -1;
  681. char *search, *dest, *key, *val, *line, *pos, buf[512];
  682. if (nl80211_get_mode(ifname, &mode))
  683. return 0;
  684. if (mode != IWINFO_OPMODE_CLIENT &&
  685. mode != IWINFO_OPMODE_ADHOC &&
  686. mode != IWINFO_OPMODE_MESHPOINT)
  687. return 0;
  688. sock = nl80211_wpactl_connect(ifname, &local);
  689. if (sock < 0)
  690. return 0;
  691. va_start(ap, ifname);
  692. /* clear all destination buffers */
  693. va_copy(ap_cur, ap);
  694. while ((search = va_arg(ap_cur, char *)) != NULL)
  695. {
  696. dest = va_arg(ap_cur, char *);
  697. len = va_arg(ap_cur, int);
  698. memset(dest, 0, len);
  699. }
  700. va_end(ap_cur);
  701. send(sock, "STATUS", 6, 0);
  702. while (true)
  703. {
  704. if (nl80211_wpactl_recv(sock, buf, sizeof(buf)) <= 0)
  705. break;
  706. if (buf[0] == '<')
  707. continue;
  708. for (line = strtok_r(buf, "\n", &pos);
  709. line != NULL;
  710. line = strtok_r(NULL, "\n", &pos))
  711. {
  712. key = strtok(line, "=");
  713. val = strtok(NULL, "\n");
  714. if (!key || !val)
  715. continue;
  716. va_copy(ap_cur, ap);
  717. while ((search = va_arg(ap_cur, char *)) != NULL)
  718. {
  719. dest = va_arg(ap_cur, char *);
  720. len = va_arg(ap_cur, int);
  721. if (!strcmp(search, key))
  722. {
  723. strncpy(dest, val, len - 1);
  724. found++;
  725. break;
  726. }
  727. }
  728. va_end(ap_cur);
  729. }
  730. break;
  731. }
  732. va_end(ap);
  733. close(sock);
  734. unlink(local.sun_path);
  735. return found;
  736. }
  737. #define nl80211_wpactl_query(ifname, ...) \
  738. __nl80211_wpactl_query(ifname, ##__VA_ARGS__, NULL)
  739. static char * nl80211_ifadd(const char *ifname)
  740. {
  741. char path[PATH_MAX];
  742. static char nif[IFNAMSIZ] = { 0 };
  743. struct nl80211_msg_conveyor *req;
  744. FILE *sysfs;
  745. req = nl80211_msg(ifname, NL80211_CMD_NEW_INTERFACE, 0);
  746. if (req)
  747. {
  748. snprintf(nif, sizeof(nif), "tmp.%s", ifname);
  749. NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, nif);
  750. NLA_PUT_U32(req->msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_STATION);
  751. nl80211_send(req, NULL, NULL);
  752. snprintf(path, sizeof(path) - 1,
  753. "/proc/sys/net/ipv6/conf/%s/disable_ipv6", nif);
  754. if ((sysfs = fopen(path, "w")) != NULL)
  755. {
  756. fwrite("0\n", 1, 2, sysfs);
  757. fclose(sysfs);
  758. }
  759. return nif;
  760. nla_put_failure:
  761. nl80211_free(req);
  762. }
  763. return NULL;
  764. }
  765. static void nl80211_ifdel(const char *ifname)
  766. {
  767. struct nl80211_msg_conveyor *req;
  768. int err;
  769. req = nl80211_msg(ifname, NL80211_CMD_DEL_INTERFACE, 0);
  770. if (req)
  771. {
  772. NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, ifname);
  773. nl80211_send(req, NULL, NULL);
  774. return;
  775. nla_put_failure:
  776. nl80211_free(req);
  777. }
  778. }
  779. static void nl80211_hostapd_hup(const char *ifname)
  780. {
  781. int fd, pid = 0;
  782. char buf[32];
  783. char *phy = nl80211_ifname2phy(ifname);
  784. if (phy)
  785. {
  786. snprintf(buf, sizeof(buf), "/var/run/wifi-%s.pid", phy);
  787. if ((fd = open(buf, O_RDONLY)) >= 0)
  788. {
  789. if (read(fd, buf, sizeof(buf)) > 0)
  790. pid = atoi(buf);
  791. close(fd);
  792. }
  793. if (pid > 0)
  794. kill(pid, 1);
  795. }
  796. }
  797. static int nl80211_probe(const char *ifname)
  798. {
  799. return !!nl80211_ifname2phy(ifname);
  800. }
  801. struct nl80211_ssid_bssid {
  802. unsigned char *ssid;
  803. unsigned char bssid[7];
  804. };
  805. static int nl80211_get_macaddr_cb(struct nl_msg *msg, void *arg)
  806. {
  807. struct nl80211_ssid_bssid *sb = arg;
  808. struct nlattr **tb = nl80211_parse(msg);
  809. if (tb[NL80211_ATTR_MAC]) {
  810. sb->bssid[0] = 1;
  811. memcpy(sb->bssid + 1, nla_data(tb[NL80211_ATTR_MAC]),
  812. sizeof(sb->bssid) - 1);
  813. }
  814. return NL_SKIP;
  815. }
  816. static int nl80211_get_ssid_bssid_cb(struct nl_msg *msg, void *arg)
  817. {
  818. int ielen;
  819. unsigned char *ie;
  820. struct nl80211_ssid_bssid *sb = arg;
  821. struct nlattr **tb = nl80211_parse(msg);
  822. struct nlattr *bss[NL80211_BSS_MAX + 1];
  823. static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
  824. [NL80211_BSS_INFORMATION_ELEMENTS] = { 0 },
  825. [NL80211_BSS_STATUS] = { .type = NLA_U32 },
  826. };
  827. if (!tb[NL80211_ATTR_BSS] ||
  828. nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
  829. bss_policy) ||
  830. !bss[NL80211_BSS_BSSID] ||
  831. !bss[NL80211_BSS_STATUS] ||
  832. !bss[NL80211_BSS_INFORMATION_ELEMENTS])
  833. {
  834. return NL_SKIP;
  835. }
  836. switch (nla_get_u32(bss[NL80211_BSS_STATUS]))
  837. {
  838. case NL80211_BSS_STATUS_ASSOCIATED:
  839. case NL80211_BSS_STATUS_AUTHENTICATED:
  840. case NL80211_BSS_STATUS_IBSS_JOINED:
  841. if (sb->ssid)
  842. {
  843. ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
  844. ielen = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
  845. while (ielen >= 2 && ielen >= ie[1])
  846. {
  847. if (ie[0] == 0)
  848. {
  849. memcpy(sb->ssid, ie + 2, min(ie[1], IWINFO_ESSID_MAX_SIZE));
  850. return NL_SKIP;
  851. }
  852. ielen -= ie[1] + 2;
  853. ie += ie[1] + 2;
  854. }
  855. }
  856. else
  857. {
  858. sb->bssid[0] = 1;
  859. memcpy(sb->bssid + 1, nla_data(bss[NL80211_BSS_BSSID]), 6);
  860. return NL_SKIP;
  861. }
  862. default:
  863. return NL_SKIP;
  864. }
  865. }
  866. static int nl80211_get_ssid(const char *ifname, char *buf)
  867. {
  868. char *res;
  869. struct nl80211_ssid_bssid sb = { .ssid = (unsigned char *)buf };
  870. /* try to find ssid from scan dump results */
  871. res = nl80211_phy2ifname(ifname);
  872. sb.ssid[0] = 0;
  873. nl80211_request(res ? res : ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP,
  874. nl80211_get_ssid_bssid_cb, &sb);
  875. /* failed, try to find from hostapd info */
  876. if (sb.ssid[0] == 0)
  877. nl80211_hostapd_query(ifname, "ssid", sb.ssid,
  878. IWINFO_ESSID_MAX_SIZE + 1);
  879. /* failed, try to obtain Mesh ID */
  880. if (sb.ssid[0] == 0)
  881. iwinfo_ubus_query(res ? res : ifname, "mesh_id",
  882. sb.ssid, IWINFO_ESSID_MAX_SIZE + 1);
  883. return (sb.ssid[0] == 0) ? -1 : 0;
  884. }
  885. static int nl80211_get_bssid(const char *ifname, char *buf)
  886. {
  887. char *res, bssid[sizeof("FF:FF:FF:FF:FF:FF\0")];
  888. struct nl80211_ssid_bssid sb = { };
  889. res = nl80211_phy2ifname(ifname);
  890. /* try to obtain mac address via NL80211_CMD_GET_INTERFACE */
  891. nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0,
  892. nl80211_get_macaddr_cb, &sb);
  893. /* failed, try to find bssid from scan dump results */
  894. if (sb.bssid[0] == 0)
  895. nl80211_request(res ? res : ifname,
  896. NL80211_CMD_GET_SCAN, NLM_F_DUMP,
  897. nl80211_get_ssid_bssid_cb, &sb);
  898. /* failed, try to find mac from hostapd info */
  899. if ((sb.bssid[0] == 0) &&
  900. nl80211_hostapd_query(ifname, "bssid", bssid, sizeof(bssid)))
  901. {
  902. sb.bssid[0] = 1;
  903. sb.bssid[1] = strtol(&bssid[0], NULL, 16);
  904. sb.bssid[2] = strtol(&bssid[3], NULL, 16);
  905. sb.bssid[3] = strtol(&bssid[6], NULL, 16);
  906. sb.bssid[4] = strtol(&bssid[9], NULL, 16);
  907. sb.bssid[5] = strtol(&bssid[12], NULL, 16);
  908. sb.bssid[6] = strtol(&bssid[15], NULL, 16);
  909. }
  910. if (sb.bssid[0])
  911. {
  912. sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
  913. sb.bssid[1], sb.bssid[2], sb.bssid[3],
  914. sb.bssid[4], sb.bssid[5], sb.bssid[6]);
  915. return 0;
  916. }
  917. return -1;
  918. }
  919. static int nl80211_get_frequency_scan_cb(struct nl_msg *msg, void *arg)
  920. {
  921. int *freq = arg;
  922. struct nlattr **attr = nl80211_parse(msg);
  923. struct nlattr *binfo[NL80211_BSS_MAX + 1];
  924. static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
  925. [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
  926. [NL80211_BSS_STATUS] = { .type = NLA_U32 },
  927. };
  928. if (attr[NL80211_ATTR_BSS] &&
  929. !nla_parse_nested(binfo, NL80211_BSS_MAX,
  930. attr[NL80211_ATTR_BSS], bss_policy))
  931. {
  932. if (binfo[NL80211_BSS_STATUS] && binfo[NL80211_BSS_FREQUENCY])
  933. *freq = nla_get_u32(binfo[NL80211_BSS_FREQUENCY]);
  934. }
  935. return NL_SKIP;
  936. }
  937. static int nl80211_get_frequency_info_cb(struct nl_msg *msg, void *arg)
  938. {
  939. int *freq = arg;
  940. struct nlattr **tb = nl80211_parse(msg);
  941. if (tb[NL80211_ATTR_WIPHY_FREQ])
  942. *freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
  943. return NL_SKIP;
  944. }
  945. static int nl80211_get_frequency(const char *ifname, int *buf)
  946. {
  947. char *res, channel[4], hwmode[3];
  948. /* try to find frequency from interface info */
  949. res = nl80211_phy2ifname(ifname);
  950. *buf = 0;
  951. nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0,
  952. nl80211_get_frequency_info_cb, buf);
  953. /* failed, try to find frequency from hostapd info */
  954. if ((*buf == 0) &&
  955. nl80211_hostapd_query(ifname, "hw_mode", hwmode, sizeof(hwmode),
  956. "channel", channel, sizeof(channel)) == 2)
  957. {
  958. *buf = nl80211_channel2freq(atoi(channel), hwmode);
  959. }
  960. /* failed, try to find frequency from scan results */
  961. if (*buf == 0)
  962. {
  963. res = nl80211_phy2ifname(ifname);
  964. nl80211_request(res ? res : ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP,
  965. nl80211_get_frequency_scan_cb, buf);
  966. }
  967. return (*buf == 0) ? -1 : 0;
  968. }
  969. static int nl80211_get_channel(const char *ifname, int *buf)
  970. {
  971. if (!nl80211_get_frequency(ifname, buf))
  972. {
  973. *buf = nl80211_freq2channel(*buf);
  974. return 0;
  975. }
  976. return -1;
  977. }
  978. static int nl80211_get_txpower_cb(struct nl_msg *msg, void *arg)
  979. {
  980. int *buf = arg;
  981. struct nlattr **tb = nl80211_parse(msg);
  982. if (tb[NL80211_ATTR_WIPHY_TX_POWER_LEVEL])
  983. *buf = iwinfo_mbm2dbm(nla_get_u32(tb[NL80211_ATTR_WIPHY_TX_POWER_LEVEL]));
  984. return NL_SKIP;
  985. }
  986. static int nl80211_get_txpower(const char *ifname, int *buf)
  987. {
  988. char *res;
  989. res = nl80211_phy2ifname(ifname);
  990. *buf = 0;
  991. if (nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0,
  992. nl80211_get_txpower_cb, buf))
  993. return -1;
  994. return 0;
  995. }
  996. static int nl80211_fill_signal_cb(struct nl_msg *msg, void *arg)
  997. {
  998. int8_t dbm;
  999. int16_t mbit;
  1000. struct nl80211_rssi_rate *rr = arg;
  1001. struct nlattr **attr = nl80211_parse(msg);
  1002. struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
  1003. struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
  1004. static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
  1005. [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
  1006. [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
  1007. [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
  1008. [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
  1009. [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
  1010. [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
  1011. [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
  1012. [NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
  1013. [NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
  1014. [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
  1015. };
  1016. static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
  1017. [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
  1018. [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
  1019. [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
  1020. [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
  1021. };
  1022. if (attr[NL80211_ATTR_STA_INFO])
  1023. {
  1024. if (!nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
  1025. attr[NL80211_ATTR_STA_INFO], stats_policy))
  1026. {
  1027. if (sinfo[NL80211_STA_INFO_SIGNAL])
  1028. {
  1029. dbm = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
  1030. rr->rssi = (rr->rssi * rr->rssi_samples + dbm) / (rr->rssi_samples + 1);
  1031. rr->rssi_samples++;
  1032. }
  1033. if (sinfo[NL80211_STA_INFO_TX_BITRATE])
  1034. {
  1035. if (!nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
  1036. sinfo[NL80211_STA_INFO_TX_BITRATE],
  1037. rate_policy))
  1038. {
  1039. if (rinfo[NL80211_RATE_INFO_BITRATE])
  1040. {
  1041. mbit = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
  1042. rr->rate = (rr->rate * rr->rate_samples + mbit) / (rr->rate_samples + 1);
  1043. rr->rate_samples++;
  1044. }
  1045. }
  1046. }
  1047. }
  1048. }
  1049. return NL_SKIP;
  1050. }
  1051. static void nl80211_fill_signal(const char *ifname, struct nl80211_rssi_rate *r)
  1052. {
  1053. DIR *d;
  1054. struct dirent *de;
  1055. memset(r, 0, sizeof(*r));
  1056. if ((d = opendir("/sys/class/net")) != NULL)
  1057. {
  1058. while ((de = readdir(d)) != NULL)
  1059. {
  1060. if (!strncmp(de->d_name, ifname, strlen(ifname)) &&
  1061. (!de->d_name[strlen(ifname)] ||
  1062. !strncmp(&de->d_name[strlen(ifname)], ".sta", 4)))
  1063. {
  1064. nl80211_request(de->d_name, NL80211_CMD_GET_STATION,
  1065. NLM_F_DUMP, nl80211_fill_signal_cb, r);
  1066. }
  1067. }
  1068. closedir(d);
  1069. }
  1070. }
  1071. static int nl80211_get_bitrate(const char *ifname, int *buf)
  1072. {
  1073. struct nl80211_rssi_rate rr;
  1074. nl80211_fill_signal(ifname, &rr);
  1075. if (rr.rate_samples)
  1076. {
  1077. *buf = (rr.rate * 100);
  1078. return 0;
  1079. }
  1080. return -1;
  1081. }
  1082. static int nl80211_get_signal(const char *ifname, int *buf)
  1083. {
  1084. struct nl80211_rssi_rate rr;
  1085. nl80211_fill_signal(ifname, &rr);
  1086. if (rr.rssi_samples)
  1087. {
  1088. *buf = rr.rssi;
  1089. return 0;
  1090. }
  1091. return -1;
  1092. }
  1093. static int nl80211_get_noise_cb(struct nl_msg *msg, void *arg)
  1094. {
  1095. int8_t *noise = arg;
  1096. struct nlattr **tb = nl80211_parse(msg);
  1097. struct nlattr *si[NL80211_SURVEY_INFO_MAX + 1];
  1098. static struct nla_policy sp[NL80211_SURVEY_INFO_MAX + 1] = {
  1099. [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
  1100. [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
  1101. };
  1102. if (!tb[NL80211_ATTR_SURVEY_INFO])
  1103. return NL_SKIP;
  1104. if (nla_parse_nested(si, NL80211_SURVEY_INFO_MAX,
  1105. tb[NL80211_ATTR_SURVEY_INFO], sp))
  1106. return NL_SKIP;
  1107. if (!si[NL80211_SURVEY_INFO_NOISE])
  1108. return NL_SKIP;
  1109. if (!*noise || si[NL80211_SURVEY_INFO_IN_USE])
  1110. *noise = (int8_t)nla_get_u8(si[NL80211_SURVEY_INFO_NOISE]);
  1111. return NL_SKIP;
  1112. }
  1113. static int nl80211_get_noise(const char *ifname, int *buf)
  1114. {
  1115. int8_t noise = 0;
  1116. if (nl80211_request(ifname, NL80211_CMD_GET_SURVEY, NLM_F_DUMP,
  1117. nl80211_get_noise_cb, &noise))
  1118. goto out;
  1119. *buf = noise;
  1120. return 0;
  1121. out:
  1122. *buf = 0;
  1123. return -1;
  1124. }
  1125. static int nl80211_get_quality(const char *ifname, int *buf)
  1126. {
  1127. int signal;
  1128. if (!nl80211_get_signal(ifname, &signal))
  1129. {
  1130. /* A positive signal level is usually just a quality
  1131. * value, pass through as-is */
  1132. if (signal >= 0)
  1133. {
  1134. *buf = signal;
  1135. }
  1136. /* The cfg80211 wext compat layer assumes a signal range
  1137. * of -110 dBm to -40 dBm, the quality value is derived
  1138. * by adding 110 to the signal level */
  1139. else
  1140. {
  1141. if (signal < -110)
  1142. signal = -110;
  1143. else if (signal > -40)
  1144. signal = -40;
  1145. *buf = (signal + 110);
  1146. }
  1147. return 0;
  1148. }
  1149. return -1;
  1150. }
  1151. static int nl80211_get_quality_max(const char *ifname, int *buf)
  1152. {
  1153. /* The cfg80211 wext compat layer assumes a maximum
  1154. * quality of 70 */
  1155. *buf = 70;
  1156. return 0;
  1157. }
  1158. static int nl80211_check_wepkey(const char *key)
  1159. {
  1160. if (key && *key)
  1161. {
  1162. switch (strlen(key))
  1163. {
  1164. case 5:
  1165. case 10:
  1166. return IWINFO_CIPHER_WEP40;
  1167. case 13:
  1168. case 26:
  1169. return IWINFO_CIPHER_WEP104;
  1170. }
  1171. }
  1172. return 0;
  1173. }
  1174. static struct {
  1175. const char *match;
  1176. int version;
  1177. int suite;
  1178. } wpa_key_mgmt_strings[] = {
  1179. { "IEEE 802.1X/EAP", 0, IWINFO_KMGMT_8021x },
  1180. { "EAP-SUITE-B-192", 4, IWINFO_KMGMT_8021x },
  1181. { "EAP-SUITE-B", 4, IWINFO_KMGMT_8021x },
  1182. { "EAP-SHA256", 0, IWINFO_KMGMT_8021x },
  1183. { "PSK-SHA256", 0, IWINFO_KMGMT_PSK },
  1184. { "NONE", 0, IWINFO_KMGMT_NONE },
  1185. { "None", 0, IWINFO_KMGMT_NONE },
  1186. { "PSK", 0, IWINFO_KMGMT_PSK },
  1187. { "EAP", 0, IWINFO_KMGMT_8021x },
  1188. { "SAE", 4, IWINFO_KMGMT_SAE },
  1189. { "OWE", 4, IWINFO_KMGMT_OWE }
  1190. };
  1191. static void parse_wpa_suites(const char *str, int defversion,
  1192. uint8_t *versions, uint8_t *suites)
  1193. {
  1194. size_t l;
  1195. int i, version;
  1196. const char *p, *q, *m, *sep = " \t\n,-+/";
  1197. for (p = str; *p; )
  1198. {
  1199. q = p;
  1200. for (i = 0; i < ARRAY_SIZE(wpa_key_mgmt_strings); i++)
  1201. {
  1202. m = wpa_key_mgmt_strings[i].match;
  1203. l = strlen(m);
  1204. if (!strncmp(q, m, l) && (!q[l] || strchr(sep, q[l])))
  1205. {
  1206. if (wpa_key_mgmt_strings[i].version != 0)
  1207. version = wpa_key_mgmt_strings[i].version;
  1208. else
  1209. version = defversion;
  1210. *versions |= version;
  1211. *suites |= wpa_key_mgmt_strings[i].suite;
  1212. q += l;
  1213. break;
  1214. }
  1215. }
  1216. if (q == p)
  1217. q += strcspn(q, sep);
  1218. p = q + strspn(q, sep);
  1219. }
  1220. }
  1221. static struct {
  1222. const char *match;
  1223. int cipher;
  1224. } wpa_cipher_strings[] = {
  1225. { "WEP-104", IWINFO_CIPHER_WEP104 },
  1226. { "WEP-40", IWINFO_CIPHER_WEP40 },
  1227. { "NONE", IWINFO_CIPHER_NONE },
  1228. { "TKIP", IWINFO_CIPHER_TKIP },
  1229. { "CCMP", IWINFO_CIPHER_CCMP }
  1230. };
  1231. static void parse_wpa_ciphers(const char *str, uint8_t *ciphers)
  1232. {
  1233. int i;
  1234. size_t l;
  1235. const char *m, *p, *q, *sep = " \t\n,-+/";
  1236. for (p = str; *p; )
  1237. {
  1238. q = p;
  1239. for (i = 0; i < ARRAY_SIZE(wpa_cipher_strings); i++)
  1240. {
  1241. m = wpa_cipher_strings[i].match;
  1242. l = strlen(m);
  1243. if (!strncmp(q, m, l) && (!q[l] || strchr(sep, q[l])))
  1244. {
  1245. *ciphers |= wpa_cipher_strings[i].cipher;
  1246. q += l;
  1247. break;
  1248. }
  1249. }
  1250. if (q == p)
  1251. q += strcspn(q, sep);
  1252. p = q + strspn(q, sep);
  1253. }
  1254. }
  1255. static int nl80211_get_encryption(const char *ifname, char *buf)
  1256. {
  1257. char *p;
  1258. int opmode;
  1259. uint8_t wpa_version = 0;
  1260. char wpa[2], wpa_key_mgmt[64], wpa_pairwise[16], wpa_groupwise[16];
  1261. char auth_algs[2], wep_key0[27], wep_key1[27], wep_key2[27], wep_key3[27];
  1262. char mode[16];
  1263. struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf;
  1264. /* WPA supplicant */
  1265. if (nl80211_wpactl_query(ifname,
  1266. "pairwise_cipher", wpa_pairwise, sizeof(wpa_pairwise),
  1267. "group_cipher", wpa_groupwise, sizeof(wpa_groupwise),
  1268. "key_mgmt", wpa_key_mgmt, sizeof(wpa_key_mgmt),
  1269. "mode", mode, sizeof(mode)))
  1270. {
  1271. /* WEP or Open */
  1272. if (!strcmp(wpa_key_mgmt, "NONE"))
  1273. {
  1274. parse_wpa_ciphers(wpa_pairwise, &c->pair_ciphers);
  1275. parse_wpa_ciphers(wpa_groupwise, &c->group_ciphers);
  1276. if (c->pair_ciphers != 0 && c->pair_ciphers != IWINFO_CIPHER_NONE) {
  1277. c->enabled = 1;
  1278. c->auth_suites = IWINFO_KMGMT_NONE;
  1279. c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
  1280. }
  1281. else {
  1282. c->pair_ciphers = 0;
  1283. c->group_ciphers = 0;
  1284. }
  1285. }
  1286. /* MESH with SAE */
  1287. else if (!strcmp(mode, "mesh") && !strcmp(wpa_key_mgmt, "UNKNOWN"))
  1288. {
  1289. c->enabled = 1;
  1290. c->wpa_version = 4;
  1291. c->auth_suites = IWINFO_KMGMT_SAE;
  1292. c->pair_ciphers = IWINFO_CIPHER_CCMP;
  1293. c->group_ciphers = IWINFO_CIPHER_CCMP;
  1294. }
  1295. /* WPA */
  1296. else
  1297. {
  1298. parse_wpa_ciphers(wpa_pairwise, &c->pair_ciphers);
  1299. parse_wpa_ciphers(wpa_groupwise, &c->group_ciphers);
  1300. p = wpa_key_mgmt;
  1301. if (!strncmp(p, "WPA2-", 5) || !strncmp(p, "WPA2/", 5))
  1302. {
  1303. p += 5;
  1304. wpa_version = 2;
  1305. }
  1306. else if (!strncmp(p, "WPA-", 4))
  1307. {
  1308. p += 4;
  1309. wpa_version = 1;
  1310. }
  1311. parse_wpa_suites(p, wpa_version, &c->wpa_version, &c->auth_suites);
  1312. c->enabled = !!(c->wpa_version && c->auth_suites);
  1313. }
  1314. return 0;
  1315. }
  1316. /* Hostapd */
  1317. else if (nl80211_hostapd_query(ifname,
  1318. "wpa", wpa, sizeof(wpa),
  1319. "wpa_key_mgmt", wpa_key_mgmt, sizeof(wpa_key_mgmt),
  1320. "wpa_pairwise", wpa_pairwise, sizeof(wpa_pairwise),
  1321. "auth_algs", auth_algs, sizeof(auth_algs),
  1322. "wep_key0", wep_key0, sizeof(wep_key0),
  1323. "wep_key1", wep_key1, sizeof(wep_key1),
  1324. "wep_key2", wep_key2, sizeof(wep_key2),
  1325. "wep_key3", wep_key3, sizeof(wep_key3)))
  1326. {
  1327. c->wpa_version = 0;
  1328. if (wpa_key_mgmt[0])
  1329. {
  1330. for (p = strtok(wpa_key_mgmt, " \t"); p != NULL; p = strtok(NULL, " \t"))
  1331. {
  1332. if (!strncmp(p, "WPA-", 4))
  1333. p += 4;
  1334. parse_wpa_suites(p, atoi(wpa), &c->wpa_version, &c->auth_suites);
  1335. }
  1336. c->enabled = c->wpa_version ? 1 : 0;
  1337. }
  1338. if (wpa_pairwise[0])
  1339. parse_wpa_ciphers(wpa_pairwise, &c->pair_ciphers);
  1340. if (auth_algs[0])
  1341. {
  1342. switch (atoi(auth_algs))
  1343. {
  1344. case 1:
  1345. c->auth_algs |= IWINFO_AUTH_OPEN;
  1346. break;
  1347. case 2:
  1348. c->auth_algs |= IWINFO_AUTH_SHARED;
  1349. break;
  1350. case 3:
  1351. c->auth_algs |= IWINFO_AUTH_OPEN;
  1352. c->auth_algs |= IWINFO_AUTH_SHARED;
  1353. break;
  1354. }
  1355. c->pair_ciphers |= nl80211_check_wepkey(wep_key0);
  1356. c->pair_ciphers |= nl80211_check_wepkey(wep_key1);
  1357. c->pair_ciphers |= nl80211_check_wepkey(wep_key2);
  1358. c->pair_ciphers |= nl80211_check_wepkey(wep_key3);
  1359. c->enabled = (c->auth_algs && c->pair_ciphers) ? 1 : 0;
  1360. }
  1361. c->group_ciphers = c->pair_ciphers;
  1362. return 0;
  1363. }
  1364. /* Ad-Hoc or Mesh interfaces without wpa_supplicant are open */
  1365. else if (!nl80211_get_mode(ifname, &opmode) &&
  1366. (opmode == IWINFO_OPMODE_ADHOC ||
  1367. opmode == IWINFO_OPMODE_MESHPOINT))
  1368. {
  1369. c->enabled = 0;
  1370. return 0;
  1371. }
  1372. return -1;
  1373. }
  1374. static int nl80211_get_phyname(const char *ifname, char *buf)
  1375. {
  1376. const char *name;
  1377. name = nl80211_ifname2phy(ifname);
  1378. if (name)
  1379. {
  1380. strcpy(buf, name);
  1381. return 0;
  1382. }
  1383. else if ((name = nl80211_phy2ifname(ifname)) != NULL)
  1384. {
  1385. name = nl80211_ifname2phy(name);
  1386. if (name)
  1387. {
  1388. strcpy(buf, ifname);
  1389. return 0;
  1390. }
  1391. }
  1392. return -1;
  1393. }
  1394. static void nl80211_parse_rateinfo(struct nlattr **ri,
  1395. struct iwinfo_rate_entry *re)
  1396. {
  1397. if (ri[NL80211_RATE_INFO_BITRATE32])
  1398. re->rate = nla_get_u32(ri[NL80211_RATE_INFO_BITRATE32]) * 100;
  1399. else if (ri[NL80211_RATE_INFO_BITRATE])
  1400. re->rate = nla_get_u16(ri[NL80211_RATE_INFO_BITRATE]) * 100;
  1401. if (ri[NL80211_RATE_INFO_VHT_MCS])
  1402. {
  1403. re->is_vht = 1;
  1404. re->mcs = nla_get_u8(ri[NL80211_RATE_INFO_VHT_MCS]);
  1405. if (ri[NL80211_RATE_INFO_VHT_NSS])
  1406. re->nss = nla_get_u8(ri[NL80211_RATE_INFO_VHT_NSS]);
  1407. }
  1408. else if (ri[NL80211_RATE_INFO_MCS])
  1409. {
  1410. re->is_ht = 1;
  1411. re->mcs = nla_get_u8(ri[NL80211_RATE_INFO_MCS]);
  1412. }
  1413. if (ri[NL80211_RATE_INFO_5_MHZ_WIDTH])
  1414. re->mhz = 5;
  1415. else if (ri[NL80211_RATE_INFO_10_MHZ_WIDTH])
  1416. re->mhz = 10;
  1417. else if (ri[NL80211_RATE_INFO_40_MHZ_WIDTH])
  1418. re->mhz = 40;
  1419. else if (ri[NL80211_RATE_INFO_80_MHZ_WIDTH])
  1420. re->mhz = 80;
  1421. else if (ri[NL80211_RATE_INFO_80P80_MHZ_WIDTH] ||
  1422. ri[NL80211_RATE_INFO_160_MHZ_WIDTH])
  1423. re->mhz = 160;
  1424. else
  1425. re->mhz = 20;
  1426. if (ri[NL80211_RATE_INFO_SHORT_GI])
  1427. re->is_short_gi = 1;
  1428. re->is_40mhz = (re->mhz == 40);
  1429. }
  1430. static int nl80211_get_survey_cb(struct nl_msg *msg, void *arg)
  1431. {
  1432. struct nl80211_array_buf *arr = arg;
  1433. struct iwinfo_survey_entry *e = arr->buf;
  1434. struct nlattr **attr = nl80211_parse(msg);
  1435. struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
  1436. int rc;
  1437. static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
  1438. [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
  1439. [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
  1440. [NL80211_SURVEY_INFO_TIME] = { .type = NLA_U64 },
  1441. [NL80211_SURVEY_INFO_TIME_BUSY] = { .type = NLA_U64 },
  1442. [NL80211_SURVEY_INFO_TIME_EXT_BUSY] = { .type = NLA_U64 },
  1443. [NL80211_SURVEY_INFO_TIME_RX] = { .type = NLA_U64 },
  1444. [NL80211_SURVEY_INFO_TIME_TX] = { .type = NLA_U64 },
  1445. };
  1446. rc = nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
  1447. attr[NL80211_ATTR_SURVEY_INFO],
  1448. survey_policy);
  1449. if (rc)
  1450. return NL_SKIP;
  1451. /* advance to end of array */
  1452. e += arr->count;
  1453. memset(e, 0, sizeof(*e));
  1454. if (sinfo[NL80211_SURVEY_INFO_FREQUENCY])
  1455. e->mhz = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
  1456. if (sinfo[NL80211_SURVEY_INFO_NOISE])
  1457. e->noise = nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
  1458. if (sinfo[NL80211_SURVEY_INFO_TIME])
  1459. e->active_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME]);
  1460. if (sinfo[NL80211_SURVEY_INFO_TIME_BUSY])
  1461. e->busy_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_BUSY]);
  1462. if (sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY])
  1463. e->busy_time_ext = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY]);
  1464. if (sinfo[NL80211_SURVEY_INFO_TIME_RX])
  1465. e->rxtime = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_RX]);
  1466. if (sinfo[NL80211_SURVEY_INFO_TIME_TX])
  1467. e->txtime = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_TX]);
  1468. arr->count++;
  1469. return NL_SKIP;
  1470. }
  1471. static void plink_state_to_str(char *dst, unsigned state)
  1472. {
  1473. switch (state) {
  1474. case NL80211_PLINK_LISTEN:
  1475. strcpy(dst, "LISTEN");
  1476. break;
  1477. case NL80211_PLINK_OPN_SNT:
  1478. strcpy(dst, "OPN_SNT");
  1479. break;
  1480. case NL80211_PLINK_OPN_RCVD:
  1481. strcpy(dst, "OPN_RCVD");
  1482. break;
  1483. case NL80211_PLINK_CNF_RCVD:
  1484. strcpy(dst, "CNF_RCVD");
  1485. break;
  1486. case NL80211_PLINK_ESTAB:
  1487. strcpy(dst, "ESTAB");
  1488. break;
  1489. case NL80211_PLINK_HOLDING:
  1490. strcpy(dst, "HOLDING");
  1491. break;
  1492. case NL80211_PLINK_BLOCKED:
  1493. strcpy(dst, "BLOCKED");
  1494. break;
  1495. default:
  1496. strcpy(dst, "UNKNOWN");
  1497. break;
  1498. }
  1499. }
  1500. static void power_mode_to_str(char *dst, struct nlattr *a)
  1501. {
  1502. enum nl80211_mesh_power_mode pm = nla_get_u32(a);
  1503. switch (pm) {
  1504. case NL80211_MESH_POWER_ACTIVE:
  1505. strcpy(dst, "ACTIVE");
  1506. break;
  1507. case NL80211_MESH_POWER_LIGHT_SLEEP:
  1508. strcpy(dst, "LIGHT SLEEP");
  1509. break;
  1510. case NL80211_MESH_POWER_DEEP_SLEEP:
  1511. strcpy(dst, "DEEP SLEEP");
  1512. break;
  1513. default:
  1514. strcpy(dst, "UNKNOWN");
  1515. break;
  1516. }
  1517. }
  1518. static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
  1519. {
  1520. struct nl80211_array_buf *arr = arg;
  1521. struct iwinfo_assoclist_entry *e = arr->buf;
  1522. struct nlattr **attr = nl80211_parse(msg);
  1523. struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
  1524. struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
  1525. struct nl80211_sta_flag_update *sta_flags;
  1526. static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
  1527. [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
  1528. [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
  1529. [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
  1530. [NL80211_STA_INFO_RX_BITRATE] = { .type = NLA_NESTED },
  1531. [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
  1532. [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
  1533. [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
  1534. [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
  1535. [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
  1536. [NL80211_STA_INFO_TX_RETRIES] = { .type = NLA_U32 },
  1537. [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
  1538. [NL80211_STA_INFO_CONNECTED_TIME]= { .type = NLA_U32 },
  1539. [NL80211_STA_INFO_RX_DROP_MISC] = { .type = NLA_U64 },
  1540. [NL80211_STA_INFO_T_OFFSET] = { .type = NLA_U64 },
  1541. [NL80211_STA_INFO_STA_FLAGS] =
  1542. { .minlen = sizeof(struct nl80211_sta_flag_update) },
  1543. [NL80211_STA_INFO_EXPECTED_THROUGHPUT] = { .type = NLA_U32 },
  1544. /* mesh */
  1545. [NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
  1546. [NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
  1547. [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
  1548. [NL80211_STA_INFO_LOCAL_PM] = { .type = NLA_U32 },
  1549. [NL80211_STA_INFO_PEER_PM] = { .type = NLA_U32 },
  1550. [NL80211_STA_INFO_NONPEER_PM] = { .type = NLA_U32 },
  1551. };
  1552. static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
  1553. [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
  1554. [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
  1555. [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
  1556. [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
  1557. };
  1558. /* advance to end of array */
  1559. e += arr->count;
  1560. memset(e, 0, sizeof(*e));
  1561. if (attr[NL80211_ATTR_MAC])
  1562. memcpy(e->mac, nla_data(attr[NL80211_ATTR_MAC]), 6);
  1563. if (attr[NL80211_ATTR_STA_INFO] &&
  1564. !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
  1565. attr[NL80211_ATTR_STA_INFO], stats_policy))
  1566. {
  1567. if (sinfo[NL80211_STA_INFO_SIGNAL])
  1568. e->signal = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
  1569. if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
  1570. e->signal_avg = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
  1571. if (sinfo[NL80211_STA_INFO_INACTIVE_TIME])
  1572. e->inactive = nla_get_u32(sinfo[NL80211_STA_INFO_INACTIVE_TIME]);
  1573. if (sinfo[NL80211_STA_INFO_CONNECTED_TIME])
  1574. e->connected_time = nla_get_u32(sinfo[NL80211_STA_INFO_CONNECTED_TIME]);
  1575. if (sinfo[NL80211_STA_INFO_RX_PACKETS])
  1576. e->rx_packets = nla_get_u32(sinfo[NL80211_STA_INFO_RX_PACKETS]);
  1577. if (sinfo[NL80211_STA_INFO_TX_PACKETS])
  1578. e->tx_packets = nla_get_u32(sinfo[NL80211_STA_INFO_TX_PACKETS]);
  1579. if (sinfo[NL80211_STA_INFO_RX_BITRATE] &&
  1580. !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
  1581. sinfo[NL80211_STA_INFO_RX_BITRATE], rate_policy))
  1582. nl80211_parse_rateinfo(rinfo, &e->rx_rate);
  1583. if (sinfo[NL80211_STA_INFO_TX_BITRATE] &&
  1584. !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
  1585. sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy))
  1586. nl80211_parse_rateinfo(rinfo, &e->tx_rate);
  1587. if (sinfo[NL80211_STA_INFO_RX_BYTES])
  1588. e->rx_bytes = nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES]);
  1589. if (sinfo[NL80211_STA_INFO_TX_BYTES])
  1590. e->tx_bytes = nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES]);
  1591. if (sinfo[NL80211_STA_INFO_TX_RETRIES])
  1592. e->tx_retries = nla_get_u32(sinfo[NL80211_STA_INFO_TX_RETRIES]);
  1593. if (sinfo[NL80211_STA_INFO_TX_FAILED])
  1594. e->tx_failed = nla_get_u32(sinfo[NL80211_STA_INFO_TX_FAILED]);
  1595. if (sinfo[NL80211_STA_INFO_T_OFFSET])
  1596. e->t_offset = nla_get_u64(sinfo[NL80211_STA_INFO_T_OFFSET]);
  1597. if (sinfo[NL80211_STA_INFO_RX_DROP_MISC])
  1598. e->rx_drop_misc = nla_get_u64(sinfo[NL80211_STA_INFO_RX_DROP_MISC]);
  1599. if (sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT])
  1600. e->thr = nla_get_u32(sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT]);
  1601. /* mesh */
  1602. if (sinfo[NL80211_STA_INFO_LLID])
  1603. e->llid = nla_get_u16(sinfo[NL80211_STA_INFO_LLID]);
  1604. if (sinfo[NL80211_STA_INFO_PLID])
  1605. e->plid = nla_get_u16(sinfo[NL80211_STA_INFO_PLID]);
  1606. if (sinfo[NL80211_STA_INFO_PLINK_STATE])
  1607. plink_state_to_str(e->plink_state,
  1608. nla_get_u8(sinfo[NL80211_STA_INFO_PLINK_STATE]));
  1609. if (sinfo[NL80211_STA_INFO_LOCAL_PM])
  1610. power_mode_to_str(e->local_ps, sinfo[NL80211_STA_INFO_LOCAL_PM]);
  1611. if (sinfo[NL80211_STA_INFO_PEER_PM])
  1612. power_mode_to_str(e->peer_ps, sinfo[NL80211_STA_INFO_PEER_PM]);
  1613. if (sinfo[NL80211_STA_INFO_NONPEER_PM])
  1614. power_mode_to_str(e->nonpeer_ps, sinfo[NL80211_STA_INFO_NONPEER_PM]);
  1615. /* Station flags */
  1616. if (sinfo[NL80211_STA_INFO_STA_FLAGS])
  1617. {
  1618. sta_flags = (struct nl80211_sta_flag_update *)
  1619. nla_data(sinfo[NL80211_STA_INFO_STA_FLAGS]);
  1620. if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
  1621. sta_flags->set & BIT(NL80211_STA_FLAG_AUTHORIZED))
  1622. e->is_authorized = 1;
  1623. if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHENTICATED) &&
  1624. sta_flags->set & BIT(NL80211_STA_FLAG_AUTHENTICATED))
  1625. e->is_authenticated = 1;
  1626. if (sta_flags->mask & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) &&
  1627. sta_flags->set & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE))
  1628. e->is_preamble_short = 1;
  1629. if (sta_flags->mask & BIT(NL80211_STA_FLAG_WME) &&
  1630. sta_flags->set & BIT(NL80211_STA_FLAG_WME))
  1631. e->is_wme = 1;
  1632. if (sta_flags->mask & BIT(NL80211_STA_FLAG_MFP) &&
  1633. sta_flags->set & BIT(NL80211_STA_FLAG_MFP))
  1634. e->is_mfp = 1;
  1635. if (sta_flags->mask & BIT(NL80211_STA_FLAG_TDLS_PEER) &&
  1636. sta_flags->set & BIT(NL80211_STA_FLAG_TDLS_PEER))
  1637. e->is_tdls = 1;
  1638. }
  1639. }
  1640. e->noise = 0; /* filled in by caller */
  1641. arr->count++;
  1642. return NL_SKIP;
  1643. }
  1644. static int nl80211_get_survey(const char *ifname, char *buf, int *len)
  1645. {
  1646. struct nl80211_array_buf arr = { .buf = buf, .count = 0 };
  1647. int rc;
  1648. rc = nl80211_request(ifname, NL80211_CMD_GET_SURVEY,
  1649. NLM_F_DUMP, nl80211_get_survey_cb, &arr);
  1650. if (!rc)
  1651. *len = (arr.count * sizeof(struct iwinfo_survey_entry));
  1652. else
  1653. *len = 0;
  1654. return 0;
  1655. }
  1656. static int nl80211_get_assoclist(const char *ifname, char *buf, int *len)
  1657. {
  1658. DIR *d;
  1659. int i, noise = 0;
  1660. struct dirent *de;
  1661. struct nl80211_array_buf arr = { .buf = buf, .count = 0 };
  1662. struct iwinfo_assoclist_entry *e;
  1663. if ((d = opendir("/sys/class/net")) != NULL)
  1664. {
  1665. while ((de = readdir(d)) != NULL)
  1666. {
  1667. if (!strncmp(de->d_name, ifname, strlen(ifname)) &&
  1668. (!de->d_name[strlen(ifname)] ||
  1669. !strncmp(&de->d_name[strlen(ifname)], ".sta", 4)))
  1670. {
  1671. nl80211_request(de->d_name, NL80211_CMD_GET_STATION,
  1672. NLM_F_DUMP, nl80211_get_assoclist_cb, &arr);
  1673. }
  1674. }
  1675. closedir(d);
  1676. if (!nl80211_get_noise(ifname, &noise))
  1677. for (i = 0, e = arr.buf; i < arr.count; i++, e++)
  1678. e->noise = noise;
  1679. *len = (arr.count * sizeof(struct iwinfo_assoclist_entry));
  1680. return 0;
  1681. }
  1682. return -1;
  1683. }
  1684. static int nl80211_get_txpwrlist_cb(struct nl_msg *msg, void *arg)
  1685. {
  1686. int *dbm_max = arg;
  1687. int ch_cur, ch_cmp, bands_remain, freqs_remain;
  1688. struct nlattr **attr = nl80211_parse(msg);
  1689. struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
  1690. struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
  1691. struct nlattr *band, *freq;
  1692. static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
  1693. [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
  1694. [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
  1695. [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
  1696. [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
  1697. [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
  1698. [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
  1699. };
  1700. ch_cur = *dbm_max; /* value int* is initialized with channel by caller */
  1701. *dbm_max = -1;
  1702. nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
  1703. {
  1704. nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
  1705. nla_len(band), NULL);
  1706. nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
  1707. {
  1708. nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
  1709. nla_data(freq), nla_len(freq), freq_policy);
  1710. ch_cmp = nl80211_freq2channel(nla_get_u32(
  1711. freqs[NL80211_FREQUENCY_ATTR_FREQ]));
  1712. if ((!ch_cur || (ch_cmp == ch_cur)) &&
  1713. freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER])
  1714. {
  1715. *dbm_max = (int)(0.01 * nla_get_u32(
  1716. freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]));
  1717. break;
  1718. }
  1719. }
  1720. }
  1721. return NL_SKIP;
  1722. }
  1723. static int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len)
  1724. {
  1725. int err, ch_cur;
  1726. int dbm_max = -1, dbm_cur, dbm_cnt;
  1727. struct nl80211_msg_conveyor *req;
  1728. struct iwinfo_txpwrlist_entry entry;
  1729. if (nl80211_get_channel(ifname, &ch_cur))
  1730. ch_cur = 0;
  1731. /* initialize the value pointer with channel for callback */
  1732. dbm_max = ch_cur;
  1733. err = nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
  1734. nl80211_get_txpwrlist_cb, &dbm_max);
  1735. if (!err)
  1736. {
  1737. for (dbm_cur = 0, dbm_cnt = 0;
  1738. dbm_cur < dbm_max;
  1739. dbm_cur++, dbm_cnt++)
  1740. {
  1741. entry.dbm = dbm_cur;
  1742. entry.mw = iwinfo_dbm2mw(dbm_cur);
  1743. memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
  1744. }
  1745. entry.dbm = dbm_max;
  1746. entry.mw = iwinfo_dbm2mw(dbm_max);
  1747. memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
  1748. dbm_cnt++;
  1749. *len = dbm_cnt * sizeof(entry);
  1750. return 0;
  1751. }
  1752. return -1;
  1753. }
  1754. static void nl80211_get_scancrypto(char *spec, struct iwinfo_crypto_entry *c)
  1755. {
  1756. int wpa_version = 0;
  1757. char *p, *q, *proto, *suites;
  1758. c->enabled = 0;
  1759. for (p = strtok_r(spec, "[]", &q); p; p = strtok_r(NULL, "[]", &q)) {
  1760. if (!strcmp(p, "WEP")) {
  1761. c->enabled = 1;
  1762. c->auth_suites = IWINFO_KMGMT_NONE;
  1763. c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
  1764. c->pair_ciphers = IWINFO_CIPHER_WEP40 | IWINFO_CIPHER_WEP104;
  1765. break;
  1766. }
  1767. proto = strtok(p, "-");
  1768. suites = strtok(NULL, "]");
  1769. if (!proto || !suites)
  1770. continue;
  1771. if (!strcmp(proto, "WPA2") || !strcmp(proto, "RSN"))
  1772. wpa_version = 2;
  1773. else if (!strcmp(proto, "WPA"))
  1774. wpa_version = 1;
  1775. else
  1776. continue;
  1777. c->enabled = 1;
  1778. parse_wpa_suites(suites, wpa_version, &c->wpa_version, &c->auth_suites);
  1779. parse_wpa_ciphers(suites, &c->pair_ciphers);
  1780. }
  1781. }
  1782. struct nl80211_scanlist {
  1783. struct iwinfo_scanlist_entry *e;
  1784. int len;
  1785. };
  1786. static void nl80211_get_scanlist_ie(struct nlattr **bss,
  1787. struct iwinfo_scanlist_entry *e)
  1788. {
  1789. int ielen = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
  1790. unsigned char *ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
  1791. static unsigned char ms_oui[3] = { 0x00, 0x50, 0xf2 };
  1792. int len;
  1793. while (ielen >= 2 && ielen >= ie[1])
  1794. {
  1795. switch (ie[0])
  1796. {
  1797. case 0: /* SSID */
  1798. case 114: /* Mesh ID */
  1799. if (e->ssid[0] == 0) {
  1800. len = min(ie[1], IWINFO_ESSID_MAX_SIZE);
  1801. memcpy(e->ssid, ie + 2, len);
  1802. e->ssid[len] = 0;
  1803. }
  1804. break;
  1805. case 48: /* RSN */
  1806. iwinfo_parse_rsn(&e->crypto, ie + 2, ie[1],
  1807. IWINFO_CIPHER_CCMP, IWINFO_KMGMT_8021x);
  1808. break;
  1809. case 221: /* Vendor */
  1810. if (ie[1] >= 4 && !memcmp(ie + 2, ms_oui, 3) && ie[5] == 1)
  1811. iwinfo_parse_rsn(&e->crypto, ie + 6, ie[1] - 4,
  1812. IWINFO_CIPHER_TKIP, IWINFO_KMGMT_PSK);
  1813. break;
  1814. }
  1815. ielen -= ie[1] + 2;
  1816. ie += ie[1] + 2;
  1817. }
  1818. }
  1819. static int nl80211_get_scanlist_cb(struct nl_msg *msg, void *arg)
  1820. {
  1821. int8_t rssi;
  1822. uint16_t caps;
  1823. struct nl80211_scanlist *sl = arg;
  1824. struct nlattr **tb = nl80211_parse(msg);
  1825. struct nlattr *bss[NL80211_BSS_MAX + 1];
  1826. static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
  1827. [NL80211_BSS_TSF] = { .type = NLA_U64 },
  1828. [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
  1829. [NL80211_BSS_BSSID] = { 0 },
  1830. [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
  1831. [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
  1832. [NL80211_BSS_INFORMATION_ELEMENTS] = { 0 },
  1833. [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
  1834. [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
  1835. [NL80211_BSS_STATUS] = { .type = NLA_U32 },
  1836. [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
  1837. [NL80211_BSS_BEACON_IES] = { 0 },
  1838. };
  1839. if (!tb[NL80211_ATTR_BSS] ||
  1840. nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
  1841. bss_policy) ||
  1842. !bss[NL80211_BSS_BSSID])
  1843. {
  1844. return NL_SKIP;
  1845. }
  1846. if (bss[NL80211_BSS_CAPABILITY])
  1847. caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
  1848. else
  1849. caps = 0;
  1850. memset(sl->e, 0, sizeof(*sl->e));
  1851. memcpy(sl->e->mac, nla_data(bss[NL80211_BSS_BSSID]), 6);
  1852. if (caps & (1<<1))
  1853. sl->e->mode = IWINFO_OPMODE_ADHOC;
  1854. else if (caps & (1<<0))
  1855. sl->e->mode = IWINFO_OPMODE_MASTER;
  1856. else
  1857. sl->e->mode = IWINFO_OPMODE_MESHPOINT;
  1858. if (caps & (1<<4))
  1859. sl->e->crypto.enabled = 1;
  1860. if (bss[NL80211_BSS_FREQUENCY])
  1861. sl->e->channel = nl80211_freq2channel(nla_get_u32(
  1862. bss[NL80211_BSS_FREQUENCY]));
  1863. if (bss[NL80211_BSS_INFORMATION_ELEMENTS])
  1864. nl80211_get_scanlist_ie(bss, sl->e);
  1865. if (bss[NL80211_BSS_SIGNAL_MBM])
  1866. {
  1867. sl->e->signal =
  1868. (uint8_t)((int32_t)nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]) / 100);
  1869. rssi = sl->e->signal - 0x100;
  1870. if (rssi < -110)
  1871. rssi = -110;
  1872. else if (rssi > -40)
  1873. rssi = -40;
  1874. sl->e->quality = (rssi + 110);
  1875. sl->e->quality_max = 70;
  1876. }
  1877. if (sl->e->crypto.enabled && !sl->e->crypto.wpa_version)
  1878. {
  1879. sl->e->crypto.auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
  1880. sl->e->crypto.pair_ciphers = IWINFO_CIPHER_WEP40 | IWINFO_CIPHER_WEP104;
  1881. }
  1882. sl->e++;
  1883. sl->len++;
  1884. return NL_SKIP;
  1885. }
  1886. static int nl80211_get_scanlist_nl(const char *ifname, char *buf, int *len)
  1887. {
  1888. struct nl80211_scanlist sl = { .e = (struct iwinfo_scanlist_entry *)buf };
  1889. if (nl80211_request(ifname, NL80211_CMD_TRIGGER_SCAN, 0, NULL, NULL))
  1890. goto out;
  1891. if (nl80211_wait("nl80211", "scan",
  1892. NL80211_CMD_NEW_SCAN_RESULTS, NL80211_CMD_SCAN_ABORTED))
  1893. goto out;
  1894. if (nl80211_request(ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP,
  1895. nl80211_get_scanlist_cb, &sl))
  1896. goto out;
  1897. *len = sl.len * sizeof(struct iwinfo_scanlist_entry);
  1898. return 0;
  1899. out:
  1900. *len = 0;
  1901. return -1;
  1902. }
  1903. static int wpasupp_ssid_decode(const char *in, char *out, int outlen)
  1904. {
  1905. #define hex(x) \
  1906. (((x) >= 'a') ? ((x) - 'a' + 10) : \
  1907. (((x) >= 'A') ? ((x) - 'A' + 10) : ((x) - '0')))
  1908. int len = 0;
  1909. while (*in)
  1910. {
  1911. if (len + 1 >= outlen)
  1912. break;
  1913. switch (*in)
  1914. {
  1915. case '\\':
  1916. in++;
  1917. switch (*in)
  1918. {
  1919. case 'n':
  1920. out[len++] = '\n'; in++;
  1921. break;
  1922. case 'r':
  1923. out[len++] = '\r'; in++;
  1924. break;
  1925. case 't':
  1926. out[len++] = '\t'; in++;
  1927. break;
  1928. case 'e':
  1929. out[len++] = '\033'; in++;
  1930. break;
  1931. case 'x':
  1932. if (isxdigit(*(in+1)) && isxdigit(*(in+2)))
  1933. out[len++] = hex(*(in+1)) * 16 + hex(*(in+2));
  1934. in += 3;
  1935. break;
  1936. default:
  1937. out[len++] = *in++;
  1938. break;
  1939. }
  1940. break;
  1941. default:
  1942. out[len++] = *in++;
  1943. break;
  1944. }
  1945. }
  1946. if (outlen > len)
  1947. out[len] = '\0';
  1948. return len;
  1949. }
  1950. static int nl80211_get_scanlist_wpactl(const char *ifname, char *buf, int *len)
  1951. {
  1952. int sock, qmax, rssi, tries, count = -1, ready = 0;
  1953. char *pos, *line, *bssid, *freq, *signal, *flags, *ssid, reply[4096];
  1954. struct sockaddr_un local = { 0 };
  1955. struct iwinfo_scanlist_entry *e = (struct iwinfo_scanlist_entry *)buf;
  1956. sock = nl80211_wpactl_connect(ifname, &local);
  1957. if (sock < 0)
  1958. return sock;
  1959. send(sock, "ATTACH", 6, 0);
  1960. send(sock, "SCAN", 4, 0);
  1961. /*
  1962. * wait for scan results:
  1963. * nl80211_wpactl_recv() will use a timeout of 256ms and we need to scan
  1964. * 72 channels at most. We'll also receive two "OK" messages acknowledging
  1965. * the "ATTACH" and "SCAN" commands and the driver might need a bit extra
  1966. * time to process the results, so try 72 + 2 + 1 times.
  1967. */
  1968. for (tries = 0; tries < 75; tries++)
  1969. {
  1970. if (nl80211_wpactl_recv(sock, reply, sizeof(reply)) <= 0)
  1971. continue;
  1972. /* got an event notification */
  1973. if (reply[0] == '<')
  1974. {
  1975. /* scan results are ready */
  1976. if (strstr(reply, "CTRL-EVENT-SCAN-RESULTS"))
  1977. {
  1978. /* send "SCAN_RESULTS" command */
  1979. ready = (send(sock, "SCAN_RESULTS", 12, 0) == 12);
  1980. break;
  1981. }
  1982. /* is another unrelated event, retry */
  1983. tries--;
  1984. }
  1985. /* scanning already in progress, keep awaiting results */
  1986. else if (!strcmp(reply, "FAIL-BUSY\n"))
  1987. {
  1988. tries--;
  1989. }
  1990. /* another failure, abort */
  1991. else if (!strncmp(reply, "FAIL-", 5))
  1992. {
  1993. break;
  1994. }
  1995. }
  1996. /* receive and parse scan results if the wait above didn't time out */
  1997. while (ready && nl80211_wpactl_recv(sock, reply, sizeof(reply)) > 0)
  1998. {
  1999. /* received an event notification, receive again */
  2000. if (reply[0] == '<')
  2001. continue;
  2002. nl80211_get_quality_max(ifname, &qmax);
  2003. for (line = strtok_r(reply, "\n", &pos);
  2004. line != NULL;
  2005. line = strtok_r(NULL, "\n", &pos))
  2006. {
  2007. /* skip header line */
  2008. if (count < 0)
  2009. {
  2010. count++;
  2011. continue;
  2012. }
  2013. bssid = strtok(line, "\t");
  2014. freq = strtok(NULL, "\t");
  2015. signal = strtok(NULL, "\t");
  2016. flags = strtok(NULL, "\t");
  2017. ssid = strtok(NULL, "\n");
  2018. if (!bssid || !freq || !signal || !flags)
  2019. continue;
  2020. /* BSSID */
  2021. e->mac[0] = strtol(&bssid[0], NULL, 16);
  2022. e->mac[1] = strtol(&bssid[3], NULL, 16);
  2023. e->mac[2] = strtol(&bssid[6], NULL, 16);
  2024. e->mac[3] = strtol(&bssid[9], NULL, 16);
  2025. e->mac[4] = strtol(&bssid[12], NULL, 16);
  2026. e->mac[5] = strtol(&bssid[15], NULL, 16);
  2027. /* SSID */
  2028. if (ssid)
  2029. wpasupp_ssid_decode(ssid, e->ssid, sizeof(e->ssid));
  2030. else
  2031. e->ssid[0] = 0;
  2032. /* Mode */
  2033. if (strstr(flags, "[MESH]"))
  2034. e->mode = IWINFO_OPMODE_MESHPOINT;
  2035. else if (strstr(flags, "[IBSS]"))
  2036. e->mode = IWINFO_OPMODE_ADHOC;
  2037. else
  2038. e->mode = IWINFO_OPMODE_MASTER;
  2039. /* Channel */
  2040. e->channel = nl80211_freq2channel(atoi(freq));
  2041. /* Signal */
  2042. rssi = atoi(signal);
  2043. e->signal = rssi;
  2044. /* Quality */
  2045. if (rssi < 0)
  2046. {
  2047. /* The cfg80211 wext compat layer assumes a signal range
  2048. * of -110 dBm to -40 dBm, the quality value is derived
  2049. * by adding 110 to the signal level */
  2050. if (rssi < -110)
  2051. rssi = -110;
  2052. else if (rssi > -40)
  2053. rssi = -40;
  2054. e->quality = (rssi + 110);
  2055. }
  2056. else
  2057. {
  2058. e->quality = rssi;
  2059. }
  2060. /* Max. Quality */
  2061. e->quality_max = qmax;
  2062. /* Crypto */
  2063. nl80211_get_scancrypto(flags, &e->crypto);
  2064. count++;
  2065. e++;
  2066. }
  2067. *len = count * sizeof(struct iwinfo_scanlist_entry);
  2068. break;
  2069. }
  2070. close(sock);
  2071. unlink(local.sun_path);
  2072. return (count >= 0) ? 0 : -1;
  2073. }
  2074. static int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
  2075. {
  2076. char *res;
  2077. int rv, mode;
  2078. *len = 0;
  2079. /* Got a radioX pseudo interface, find some interface on it or create one */
  2080. if (!strncmp(ifname, "radio", 5))
  2081. {
  2082. /* Reuse existing interface */
  2083. if ((res = nl80211_phy2ifname(ifname)) != NULL)
  2084. {
  2085. return nl80211_get_scanlist(res, buf, len);
  2086. }
  2087. /* Need to spawn a temporary iface for scanning */
  2088. else if ((res = nl80211_ifadd(ifname)) != NULL)
  2089. {
  2090. rv = nl80211_get_scanlist(res, buf, len);
  2091. nl80211_ifdel(res);
  2092. return rv;
  2093. }
  2094. }
  2095. /* WPA supplicant */
  2096. if (!nl80211_get_scanlist_wpactl(ifname, buf, len))
  2097. {
  2098. return 0;
  2099. }
  2100. /* station / ad-hoc / monitor scan */
  2101. else if (!nl80211_get_mode(ifname, &mode) &&
  2102. (mode == IWINFO_OPMODE_ADHOC ||
  2103. mode == IWINFO_OPMODE_MASTER ||
  2104. mode == IWINFO_OPMODE_CLIENT ||
  2105. mode == IWINFO_OPMODE_MONITOR) &&
  2106. iwinfo_ifup(ifname))
  2107. {
  2108. return nl80211_get_scanlist_nl(ifname, buf, len);
  2109. }
  2110. /* AP scan */
  2111. else
  2112. {
  2113. /* Got a temp interface, don't create yet another one */
  2114. if (!strncmp(ifname, "tmp.", 4))
  2115. {
  2116. if (!iwinfo_ifup(ifname))
  2117. return -1;
  2118. rv = nl80211_get_scanlist_nl(ifname, buf, len);
  2119. iwinfo_ifdown(ifname);
  2120. return rv;
  2121. }
  2122. /* Spawn a new scan interface */
  2123. else
  2124. {
  2125. if (!(res = nl80211_ifadd(ifname)))
  2126. return -1;
  2127. iwinfo_ifmac(res);
  2128. /* if we can take the new interface up, the driver supports an
  2129. * additional interface and there's no need to tear down the ap */
  2130. if (iwinfo_ifup(res))
  2131. {
  2132. rv = nl80211_get_scanlist_nl(res, buf, len);
  2133. iwinfo_ifdown(res);
  2134. }
  2135. /* driver cannot create secondary interface, take down ap
  2136. * during scan */
  2137. else if (iwinfo_ifdown(ifname) && iwinfo_ifup(res))
  2138. {
  2139. rv = nl80211_get_scanlist_nl(res, buf, len);
  2140. iwinfo_ifdown(res);
  2141. iwinfo_ifup(ifname);
  2142. nl80211_hostapd_hup(ifname);
  2143. }
  2144. nl80211_ifdel(res);
  2145. return rv;
  2146. }
  2147. }
  2148. return -1;
  2149. }
  2150. static int nl80211_get_freqlist_cb(struct nl_msg *msg, void *arg)
  2151. {
  2152. int bands_remain, freqs_remain;
  2153. struct nl80211_array_buf *arr = arg;
  2154. struct iwinfo_freqlist_entry *e;
  2155. struct nlattr **attr = nl80211_parse(msg);
  2156. struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
  2157. struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
  2158. struct nlattr *band, *freq;
  2159. e = arr->buf;
  2160. e += arr->count;
  2161. if (attr[NL80211_ATTR_WIPHY_BANDS]) {
  2162. nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
  2163. {
  2164. nla_parse(bands, NL80211_BAND_ATTR_MAX,
  2165. nla_data(band), nla_len(band), NULL);
  2166. if (bands[NL80211_BAND_ATTR_FREQS]) {
  2167. nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
  2168. {
  2169. nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
  2170. nla_data(freq), nla_len(freq), NULL);
  2171. if (!freqs[NL80211_FREQUENCY_ATTR_FREQ] ||
  2172. freqs[NL80211_FREQUENCY_ATTR_DISABLED])
  2173. continue;
  2174. e->mhz = nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]);
  2175. e->channel = nl80211_freq2channel(e->mhz);
  2176. e->restricted = (
  2177. freqs[NL80211_FREQUENCY_ATTR_NO_IR] &&
  2178. !freqs[NL80211_FREQUENCY_ATTR_RADAR]
  2179. ) ? 1 : 0;
  2180. if (freqs[NL80211_FREQUENCY_ATTR_NO_HT40_MINUS])
  2181. e->flags |= IWINFO_FREQ_NO_HT40MINUS;
  2182. if (freqs[NL80211_FREQUENCY_ATTR_NO_HT40_PLUS])
  2183. e->flags |= IWINFO_FREQ_NO_HT40PLUS;
  2184. if (freqs[NL80211_FREQUENCY_ATTR_NO_80MHZ])
  2185. e->flags |= IWINFO_FREQ_NO_80MHZ;
  2186. if (freqs[NL80211_FREQUENCY_ATTR_NO_160MHZ])
  2187. e->flags |= IWINFO_FREQ_NO_160MHZ;
  2188. if (freqs[NL80211_FREQUENCY_ATTR_NO_20MHZ])
  2189. e->flags |= IWINFO_FREQ_NO_20MHZ;
  2190. if (freqs[NL80211_FREQUENCY_ATTR_NO_10MHZ])
  2191. e->flags |= IWINFO_FREQ_NO_10MHZ;
  2192. e++;
  2193. arr->count++;
  2194. }
  2195. }
  2196. }
  2197. }
  2198. return NL_SKIP;
  2199. }
  2200. static int nl80211_get_freqlist(const char *ifname, char *buf, int *len)
  2201. {
  2202. struct nl80211_msg_conveyor *cv;
  2203. struct nl80211_array_buf arr = { .buf = buf, .count = 0 };
  2204. uint32_t features = nl80211_get_protocol_features(ifname);
  2205. int flags;
  2206. flags = features & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP ? NLM_F_DUMP : 0;
  2207. cv = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, flags);
  2208. if (!cv)
  2209. goto out;
  2210. NLA_PUT_FLAG(cv->msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
  2211. if (nl80211_send(cv, nl80211_get_freqlist_cb, &arr))
  2212. goto out;
  2213. *len = arr.count * sizeof(struct iwinfo_freqlist_entry);
  2214. return 0;
  2215. nla_put_failure:
  2216. nl80211_free(cv);
  2217. out:
  2218. *len = 0;
  2219. return -1;
  2220. }
  2221. static int nl80211_get_country_cb(struct nl_msg *msg, void *arg)
  2222. {
  2223. char *buf = arg;
  2224. struct nlattr **attr = nl80211_parse(msg);
  2225. if (attr[NL80211_ATTR_REG_ALPHA2])
  2226. memcpy(buf, nla_data(attr[NL80211_ATTR_REG_ALPHA2]), 2);
  2227. else
  2228. buf[0] = 0;
  2229. return NL_SKIP;
  2230. }
  2231. static int nl80211_get_country(const char *ifname, char *buf)
  2232. {
  2233. if (nl80211_request(ifname, NL80211_CMD_GET_REG, 0,
  2234. nl80211_get_country_cb, buf))
  2235. return -1;
  2236. return 0;
  2237. }
  2238. static int nl80211_get_countrylist(const char *ifname, char *buf, int *len)
  2239. {
  2240. int count;
  2241. struct iwinfo_country_entry *e = (struct iwinfo_country_entry *)buf;
  2242. const struct iwinfo_iso3166_label *l;
  2243. for (l = IWINFO_ISO3166_NAMES, count = 0; l->iso3166; l++, e++, count++)
  2244. {
  2245. e->iso3166 = l->iso3166;
  2246. e->ccode[0] = (l->iso3166 / 256);
  2247. e->ccode[1] = (l->iso3166 % 256);
  2248. e->ccode[2] = 0;
  2249. }
  2250. *len = (count * sizeof(struct iwinfo_country_entry));
  2251. return 0;
  2252. }
  2253. struct nl80211_modes
  2254. {
  2255. bool ok;
  2256. uint32_t hw;
  2257. uint32_t ht;
  2258. };
  2259. static int nl80211_get_modelist_cb(struct nl_msg *msg, void *arg)
  2260. {
  2261. struct nl80211_modes *m = arg;
  2262. int bands_remain, freqs_remain;
  2263. uint16_t caps = 0;
  2264. uint32_t vht_caps = 0;
  2265. struct nlattr **attr = nl80211_parse(msg);
  2266. struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
  2267. struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
  2268. struct nlattr *band, *freq;
  2269. if (attr[NL80211_ATTR_WIPHY_BANDS])
  2270. {
  2271. nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
  2272. {
  2273. nla_parse(bands, NL80211_BAND_ATTR_MAX,
  2274. nla_data(band), nla_len(band), NULL);
  2275. if (bands[NL80211_BAND_ATTR_HT_CAPA])
  2276. caps = nla_get_u16(bands[NL80211_BAND_ATTR_HT_CAPA]);
  2277. /* Treat any nonzero capability as 11n */
  2278. if (caps > 0)
  2279. {
  2280. m->hw |= IWINFO_80211_N;
  2281. m->ht |= IWINFO_HTMODE_HT20;
  2282. if (caps & (1 << 1))
  2283. m->ht |= IWINFO_HTMODE_HT40;
  2284. }
  2285. nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS],
  2286. freqs_remain)
  2287. {
  2288. nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
  2289. nla_data(freq), nla_len(freq), NULL);
  2290. if (!freqs[NL80211_FREQUENCY_ATTR_FREQ])
  2291. continue;
  2292. if (nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]) < 2485)
  2293. {
  2294. m->hw |= IWINFO_80211_B;
  2295. m->hw |= IWINFO_80211_G;
  2296. }
  2297. else if (bands[NL80211_BAND_ATTR_VHT_CAPA])
  2298. {
  2299. vht_caps = nla_get_u32(bands[NL80211_BAND_ATTR_VHT_CAPA]);
  2300. /* Treat any nonzero capability as 11ac */
  2301. if (vht_caps > 0)
  2302. {
  2303. m->hw |= IWINFO_80211_AC;
  2304. m->ht |= IWINFO_HTMODE_VHT20 | IWINFO_HTMODE_VHT40 | IWINFO_HTMODE_VHT80;
  2305. switch ((vht_caps >> 2) & 3)
  2306. {
  2307. case 2:
  2308. m->ht |= IWINFO_HTMODE_VHT80_80;
  2309. /* fall through */
  2310. case 1:
  2311. m->ht |= IWINFO_HTMODE_VHT160;
  2312. }
  2313. }
  2314. }
  2315. else if (nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]) >= 56160)
  2316. {
  2317. m->hw |= IWINFO_80211_AD;
  2318. }
  2319. else if (!(m->hw & IWINFO_80211_AC))
  2320. {
  2321. m->hw |= IWINFO_80211_A;
  2322. }
  2323. }
  2324. }
  2325. m->ok = 1;
  2326. }
  2327. return NL_SKIP;
  2328. }
  2329. static int nl80211_get_hwmodelist(const char *ifname, int *buf)
  2330. {
  2331. struct nl80211_modes m = { 0 };
  2332. if (nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
  2333. nl80211_get_modelist_cb, &m))
  2334. goto out;
  2335. if (!m.ok)
  2336. goto out;
  2337. *buf = m.hw;
  2338. return 0;
  2339. out:
  2340. *buf = 0;
  2341. return -1;
  2342. }
  2343. struct chan_info {
  2344. int width;
  2345. int mode;
  2346. };
  2347. static int nl80211_get_htmode_cb(struct nl_msg *msg, void *arg)
  2348. {
  2349. struct nlattr **tb = nl80211_parse(msg);
  2350. struct nlattr *cur;
  2351. struct chan_info *chn = arg;
  2352. if ((cur = tb[NL80211_ATTR_CHANNEL_WIDTH]))
  2353. chn->width = nla_get_u32(cur);
  2354. if ((cur = tb[NL80211_ATTR_BSS_HT_OPMODE]))
  2355. chn->mode = nla_get_u32(cur);
  2356. return NL_SKIP;
  2357. }
  2358. static int nl80211_get_htmode(const char *ifname, int *buf)
  2359. {
  2360. struct chan_info chn = { .width = 0, .mode = 0 };
  2361. char *res;
  2362. int err;
  2363. res = nl80211_phy2ifname(ifname);
  2364. *buf = 0;
  2365. err = nl80211_request(res ? res : ifname,
  2366. NL80211_CMD_GET_INTERFACE, 0,
  2367. nl80211_get_htmode_cb, &chn);
  2368. if (err)
  2369. return -1;
  2370. switch (chn.width) {
  2371. case NL80211_CHAN_WIDTH_20:
  2372. if (chn.mode == -1)
  2373. *buf = IWINFO_HTMODE_VHT20;
  2374. else
  2375. *buf = IWINFO_HTMODE_HT20;
  2376. break;
  2377. case NL80211_CHAN_WIDTH_40:
  2378. if (chn.mode == -1)
  2379. *buf = IWINFO_HTMODE_VHT40;
  2380. else
  2381. *buf = IWINFO_HTMODE_HT40;
  2382. break;
  2383. case NL80211_CHAN_WIDTH_80:
  2384. *buf = IWINFO_HTMODE_VHT80;
  2385. break;
  2386. case NL80211_CHAN_WIDTH_80P80:
  2387. *buf = IWINFO_HTMODE_VHT80_80;
  2388. break;
  2389. case NL80211_CHAN_WIDTH_160:
  2390. *buf = IWINFO_HTMODE_VHT160;
  2391. break;
  2392. case NL80211_CHAN_WIDTH_5:
  2393. case NL80211_CHAN_WIDTH_10:
  2394. case NL80211_CHAN_WIDTH_20_NOHT:
  2395. *buf = IWINFO_HTMODE_NOHT;
  2396. break;
  2397. default:
  2398. return -1;
  2399. }
  2400. return 0;
  2401. }
  2402. static int nl80211_get_htmodelist(const char *ifname, int *buf)
  2403. {
  2404. struct nl80211_modes m = { 0 };
  2405. if (nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
  2406. nl80211_get_modelist_cb, &m))
  2407. goto out;
  2408. if (!m.ok)
  2409. goto out;
  2410. *buf = m.ht;
  2411. return 0;
  2412. out:
  2413. *buf = 0;
  2414. return -1;
  2415. }
  2416. static int nl80211_get_ifcomb_cb(struct nl_msg *msg, void *arg)
  2417. {
  2418. struct nlattr **attr = nl80211_parse(msg);
  2419. struct nlattr *comb;
  2420. int *ret = arg;
  2421. int comb_rem, limit_rem, mode_rem;
  2422. *ret = 0;
  2423. if (!attr[NL80211_ATTR_INTERFACE_COMBINATIONS])
  2424. return NL_SKIP;
  2425. nla_for_each_nested(comb, attr[NL80211_ATTR_INTERFACE_COMBINATIONS], comb_rem)
  2426. {
  2427. static struct nla_policy iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
  2428. [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
  2429. [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
  2430. };
  2431. struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB+1];
  2432. static struct nla_policy iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
  2433. [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
  2434. [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
  2435. };
  2436. struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT+1];
  2437. struct nlattr *limit;
  2438. nla_parse_nested(tb_comb, NUM_NL80211_IFACE_COMB, comb, iface_combination_policy);
  2439. if (!tb_comb[NL80211_IFACE_COMB_LIMITS])
  2440. continue;
  2441. nla_for_each_nested(limit, tb_comb[NL80211_IFACE_COMB_LIMITS], limit_rem)
  2442. {
  2443. struct nlattr *mode;
  2444. nla_parse_nested(tb_limit, NUM_NL80211_IFACE_LIMIT, limit, iface_limit_policy);
  2445. if (!tb_limit[NL80211_IFACE_LIMIT_TYPES] ||
  2446. !tb_limit[NL80211_IFACE_LIMIT_MAX])
  2447. continue;
  2448. if (nla_get_u32(tb_limit[NL80211_IFACE_LIMIT_MAX]) < 2)
  2449. continue;
  2450. nla_for_each_nested(mode, tb_limit[NL80211_IFACE_LIMIT_TYPES], mode_rem) {
  2451. if (nla_type(mode) == NL80211_IFTYPE_AP)
  2452. *ret = 1;
  2453. }
  2454. }
  2455. }
  2456. return NL_SKIP;
  2457. }
  2458. static int nl80211_get_mbssid_support(const char *ifname, int *buf)
  2459. {
  2460. if (nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
  2461. nl80211_get_ifcomb_cb, buf))
  2462. return -1;
  2463. return 0;
  2464. }
  2465. static int nl80211_get_hardware_id(const char *ifname, char *buf)
  2466. {
  2467. struct iwinfo_hardware_id *id = (struct iwinfo_hardware_id *)buf;
  2468. char *phy, num[8], path[PATH_MAX];
  2469. int i;
  2470. struct { const char *path; uint16_t *dest; } lookup[] = {
  2471. { "vendor", &id->vendor_id },
  2472. { "device", &id->device_id },
  2473. { "subsystem_vendor", &id->subsystem_vendor_id },
  2474. { "subsystem_device", &id->subsystem_device_id }
  2475. };
  2476. memset(id, 0, sizeof(*id));
  2477. /* Try to determine the phy name from the given interface */
  2478. phy = nl80211_ifname2phy(ifname);
  2479. for (i = 0; i < ARRAY_SIZE(lookup); i++)
  2480. {
  2481. snprintf(path, sizeof(path), "/sys/class/%s/%s/device/%s",
  2482. phy ? "ieee80211" : "net",
  2483. phy ? phy : ifname, lookup[i].path);
  2484. if (nl80211_readstr(path, num, sizeof(num)) > 0)
  2485. *lookup[i].dest = strtoul(num, NULL, 16);
  2486. }
  2487. /* Failed to obtain hardware IDs, search board config */
  2488. if (id->vendor_id == 0 || id->device_id == 0)
  2489. return iwinfo_hardware_id_from_mtd(id);
  2490. return 0;
  2491. }
  2492. static const struct iwinfo_hardware_entry *
  2493. nl80211_get_hardware_entry(const char *ifname)
  2494. {
  2495. struct iwinfo_hardware_id id;
  2496. if (nl80211_get_hardware_id(ifname, (char *)&id))
  2497. return NULL;
  2498. return iwinfo_hardware(&id);
  2499. }
  2500. static int nl80211_get_hardware_name(const char *ifname, char *buf)
  2501. {
  2502. const struct iwinfo_hardware_entry *hw;
  2503. if (!(hw = nl80211_get_hardware_entry(ifname)))
  2504. sprintf(buf, "Generic MAC80211");
  2505. else
  2506. sprintf(buf, "%s %s", hw->vendor_name, hw->device_name);
  2507. return 0;
  2508. }
  2509. static int nl80211_get_txpower_offset(const char *ifname, int *buf)
  2510. {
  2511. const struct iwinfo_hardware_entry *hw;
  2512. if (!(hw = nl80211_get_hardware_entry(ifname)))
  2513. return -1;
  2514. *buf = hw->txpower_offset;
  2515. return 0;
  2516. }
  2517. static int nl80211_get_frequency_offset(const char *ifname, int *buf)
  2518. {
  2519. const struct iwinfo_hardware_entry *hw;
  2520. if (!(hw = nl80211_get_hardware_entry(ifname)))
  2521. return -1;
  2522. *buf = hw->frequency_offset;
  2523. return 0;
  2524. }
  2525. static int nl80211_lookup_phyname(const char *section, char *buf)
  2526. {
  2527. int idx;
  2528. if ((idx = nl80211_phy_idx_from_uci(section)) < 0)
  2529. return -1;
  2530. sprintf(buf, "phy%d", idx);
  2531. return 0;
  2532. }
  2533. const struct iwinfo_ops nl80211_ops = {
  2534. .name = "nl80211",
  2535. .probe = nl80211_probe,
  2536. .channel = nl80211_get_channel,
  2537. .frequency = nl80211_get_frequency,
  2538. .frequency_offset = nl80211_get_frequency_offset,
  2539. .txpower = nl80211_get_txpower,
  2540. .txpower_offset = nl80211_get_txpower_offset,
  2541. .bitrate = nl80211_get_bitrate,
  2542. .signal = nl80211_get_signal,
  2543. .noise = nl80211_get_noise,
  2544. .quality = nl80211_get_quality,
  2545. .quality_max = nl80211_get_quality_max,
  2546. .mbssid_support = nl80211_get_mbssid_support,
  2547. .hwmodelist = nl80211_get_hwmodelist,
  2548. .htmodelist = nl80211_get_htmodelist,
  2549. .htmode = nl80211_get_htmode,
  2550. .mode = nl80211_get_mode,
  2551. .ssid = nl80211_get_ssid,
  2552. .bssid = nl80211_get_bssid,
  2553. .country = nl80211_get_country,
  2554. .hardware_id = nl80211_get_hardware_id,
  2555. .hardware_name = nl80211_get_hardware_name,
  2556. .encryption = nl80211_get_encryption,
  2557. .phyname = nl80211_get_phyname,
  2558. .assoclist = nl80211_get_assoclist,
  2559. .txpwrlist = nl80211_get_txpwrlist,
  2560. .scanlist = nl80211_get_scanlist,
  2561. .freqlist = nl80211_get_freqlist,
  2562. .countrylist = nl80211_get_countrylist,
  2563. .survey = nl80211_get_survey,
  2564. .lookup_phy = nl80211_lookup_phyname,
  2565. .close = nl80211_close
  2566. };