iwinfo_nl80211.c 63 KB

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