iwinfo_nl80211.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385
  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_center_freq1_cb(struct nl_msg *msg, void *arg)
  970. {
  971. int *freq = arg;
  972. struct nlattr **tb = nl80211_parse(msg);
  973. if (tb[NL80211_ATTR_CENTER_FREQ1])
  974. *freq = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
  975. return NL_SKIP;
  976. }
  977. static int nl80211_get_center_freq1(const char *ifname, int *buf)
  978. {
  979. char *res;
  980. /* try to find frequency from interface info */
  981. res = nl80211_phy2ifname(ifname);
  982. *buf = 0;
  983. nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0,
  984. nl80211_get_center_freq1_cb, buf);
  985. return (*buf == 0) ? -1 : 0;
  986. }
  987. static int nl80211_get_center_freq2_cb(struct nl_msg *msg, void *arg)
  988. {
  989. int *freq = arg;
  990. struct nlattr **tb = nl80211_parse(msg);
  991. if (tb[NL80211_ATTR_CENTER_FREQ2])
  992. *freq = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
  993. return NL_SKIP;
  994. }
  995. static int nl80211_get_center_freq2(const char *ifname, int *buf)
  996. {
  997. char *res;
  998. /* try to find frequency from interface info */
  999. res = nl80211_phy2ifname(ifname);
  1000. *buf = 0;
  1001. nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0,
  1002. nl80211_get_center_freq2_cb, buf);
  1003. return (*buf == 0) ? -1 : 0;
  1004. }
  1005. static int nl80211_get_channel(const char *ifname, int *buf)
  1006. {
  1007. if (!nl80211_get_frequency(ifname, buf))
  1008. {
  1009. *buf = nl80211_freq2channel(*buf);
  1010. return 0;
  1011. }
  1012. return -1;
  1013. }
  1014. static int nl80211_get_center_chan1(const char *ifname, int *buf)
  1015. {
  1016. if (!nl80211_get_center_freq1(ifname, buf))
  1017. {
  1018. *buf = nl80211_freq2channel(*buf);
  1019. return 0;
  1020. }
  1021. return -1;
  1022. }
  1023. static int nl80211_get_center_chan2(const char *ifname, int *buf)
  1024. {
  1025. if (!nl80211_get_center_freq2(ifname, buf))
  1026. {
  1027. *buf = nl80211_freq2channel(*buf);
  1028. return 0;
  1029. }
  1030. return -1;
  1031. }
  1032. static int nl80211_get_txpower_cb(struct nl_msg *msg, void *arg)
  1033. {
  1034. int *buf = arg;
  1035. struct nlattr **tb = nl80211_parse(msg);
  1036. if (tb[NL80211_ATTR_WIPHY_TX_POWER_LEVEL])
  1037. *buf = iwinfo_mbm2dbm(nla_get_u32(tb[NL80211_ATTR_WIPHY_TX_POWER_LEVEL]));
  1038. return NL_SKIP;
  1039. }
  1040. static int nl80211_get_txpower(const char *ifname, int *buf)
  1041. {
  1042. char *res;
  1043. res = nl80211_phy2ifname(ifname);
  1044. *buf = 0;
  1045. if (nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0,
  1046. nl80211_get_txpower_cb, buf))
  1047. return -1;
  1048. return 0;
  1049. }
  1050. static int nl80211_fill_signal_cb(struct nl_msg *msg, void *arg)
  1051. {
  1052. int8_t dbm;
  1053. int16_t mbit;
  1054. struct nl80211_rssi_rate *rr = arg;
  1055. struct nlattr **attr = nl80211_parse(msg);
  1056. struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
  1057. struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
  1058. static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
  1059. [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
  1060. [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
  1061. [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
  1062. [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
  1063. [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
  1064. [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
  1065. [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
  1066. [NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
  1067. [NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
  1068. [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
  1069. };
  1070. static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
  1071. [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
  1072. [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
  1073. [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
  1074. [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
  1075. };
  1076. if (attr[NL80211_ATTR_STA_INFO])
  1077. {
  1078. if (!nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
  1079. attr[NL80211_ATTR_STA_INFO], stats_policy))
  1080. {
  1081. if (sinfo[NL80211_STA_INFO_SIGNAL])
  1082. {
  1083. dbm = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
  1084. rr->rssi = (rr->rssi * rr->rssi_samples + dbm) / (rr->rssi_samples + 1);
  1085. rr->rssi_samples++;
  1086. }
  1087. if (sinfo[NL80211_STA_INFO_TX_BITRATE])
  1088. {
  1089. if (!nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
  1090. sinfo[NL80211_STA_INFO_TX_BITRATE],
  1091. rate_policy))
  1092. {
  1093. if (rinfo[NL80211_RATE_INFO_BITRATE])
  1094. {
  1095. mbit = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
  1096. rr->rate = (rr->rate * rr->rate_samples + mbit) / (rr->rate_samples + 1);
  1097. rr->rate_samples++;
  1098. }
  1099. }
  1100. }
  1101. }
  1102. }
  1103. return NL_SKIP;
  1104. }
  1105. static void nl80211_fill_signal(const char *ifname, struct nl80211_rssi_rate *r)
  1106. {
  1107. DIR *d;
  1108. struct dirent *de;
  1109. memset(r, 0, sizeof(*r));
  1110. if ((d = opendir("/sys/class/net")) != NULL)
  1111. {
  1112. while ((de = readdir(d)) != NULL)
  1113. {
  1114. if (!strncmp(de->d_name, ifname, strlen(ifname)) &&
  1115. (!de->d_name[strlen(ifname)] ||
  1116. !strncmp(&de->d_name[strlen(ifname)], ".sta", 4)))
  1117. {
  1118. nl80211_request(de->d_name, NL80211_CMD_GET_STATION,
  1119. NLM_F_DUMP, nl80211_fill_signal_cb, r);
  1120. }
  1121. }
  1122. closedir(d);
  1123. }
  1124. }
  1125. static int nl80211_get_bitrate(const char *ifname, int *buf)
  1126. {
  1127. struct nl80211_rssi_rate rr;
  1128. nl80211_fill_signal(ifname, &rr);
  1129. if (rr.rate_samples)
  1130. {
  1131. *buf = (rr.rate * 100);
  1132. return 0;
  1133. }
  1134. return -1;
  1135. }
  1136. static int nl80211_get_signal(const char *ifname, int *buf)
  1137. {
  1138. struct nl80211_rssi_rate rr;
  1139. nl80211_fill_signal(ifname, &rr);
  1140. if (rr.rssi_samples)
  1141. {
  1142. *buf = rr.rssi;
  1143. return 0;
  1144. }
  1145. return -1;
  1146. }
  1147. static int nl80211_get_noise_cb(struct nl_msg *msg, void *arg)
  1148. {
  1149. int8_t *noise = arg;
  1150. struct nlattr **tb = nl80211_parse(msg);
  1151. struct nlattr *si[NL80211_SURVEY_INFO_MAX + 1];
  1152. static struct nla_policy sp[NL80211_SURVEY_INFO_MAX + 1] = {
  1153. [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
  1154. [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
  1155. };
  1156. if (!tb[NL80211_ATTR_SURVEY_INFO])
  1157. return NL_SKIP;
  1158. if (nla_parse_nested(si, NL80211_SURVEY_INFO_MAX,
  1159. tb[NL80211_ATTR_SURVEY_INFO], sp))
  1160. return NL_SKIP;
  1161. if (!si[NL80211_SURVEY_INFO_NOISE])
  1162. return NL_SKIP;
  1163. if (!*noise || si[NL80211_SURVEY_INFO_IN_USE])
  1164. *noise = (int8_t)nla_get_u8(si[NL80211_SURVEY_INFO_NOISE]);
  1165. return NL_SKIP;
  1166. }
  1167. static int nl80211_get_noise(const char *ifname, int *buf)
  1168. {
  1169. int8_t noise = 0;
  1170. if (nl80211_request(ifname, NL80211_CMD_GET_SURVEY, NLM_F_DUMP,
  1171. nl80211_get_noise_cb, &noise))
  1172. goto out;
  1173. *buf = noise;
  1174. return 0;
  1175. out:
  1176. *buf = 0;
  1177. return -1;
  1178. }
  1179. static int nl80211_get_quality(const char *ifname, int *buf)
  1180. {
  1181. int signal;
  1182. if (!nl80211_get_signal(ifname, &signal))
  1183. {
  1184. /* A positive signal level is usually just a quality
  1185. * value, pass through as-is */
  1186. if (signal >= 0)
  1187. {
  1188. *buf = signal;
  1189. }
  1190. /* The cfg80211 wext compat layer assumes a signal range
  1191. * of -110 dBm to -40 dBm, the quality value is derived
  1192. * by adding 110 to the signal level */
  1193. else
  1194. {
  1195. if (signal < -110)
  1196. signal = -110;
  1197. else if (signal > -40)
  1198. signal = -40;
  1199. *buf = (signal + 110);
  1200. }
  1201. return 0;
  1202. }
  1203. return -1;
  1204. }
  1205. static int nl80211_get_quality_max(const char *ifname, int *buf)
  1206. {
  1207. /* The cfg80211 wext compat layer assumes a maximum
  1208. * quality of 70 */
  1209. *buf = 70;
  1210. return 0;
  1211. }
  1212. static int nl80211_check_wepkey(const char *key)
  1213. {
  1214. if (key && *key)
  1215. {
  1216. switch (strlen(key))
  1217. {
  1218. case 5:
  1219. case 10:
  1220. return IWINFO_CIPHER_WEP40;
  1221. case 13:
  1222. case 26:
  1223. return IWINFO_CIPHER_WEP104;
  1224. }
  1225. }
  1226. return 0;
  1227. }
  1228. static struct {
  1229. const char *match;
  1230. int version;
  1231. int suite;
  1232. } wpa_key_mgmt_strings[] = {
  1233. { "IEEE 802.1X/EAP", 0, IWINFO_KMGMT_8021x },
  1234. { "EAP-SUITE-B-192", 4, IWINFO_KMGMT_8021x },
  1235. { "EAP-SUITE-B", 4, IWINFO_KMGMT_8021x },
  1236. { "EAP-SHA256", 0, IWINFO_KMGMT_8021x },
  1237. { "PSK-SHA256", 0, IWINFO_KMGMT_PSK },
  1238. { "NONE", 0, IWINFO_KMGMT_NONE },
  1239. { "None", 0, IWINFO_KMGMT_NONE },
  1240. { "PSK", 0, IWINFO_KMGMT_PSK },
  1241. { "EAP", 0, IWINFO_KMGMT_8021x },
  1242. { "SAE", 4, IWINFO_KMGMT_SAE },
  1243. { "OWE", 4, IWINFO_KMGMT_OWE }
  1244. };
  1245. static void parse_wpa_suites(const char *str, int defversion,
  1246. uint8_t *versions, uint8_t *suites)
  1247. {
  1248. size_t l;
  1249. int i, version;
  1250. const char *p, *q, *m, *sep = " \t\n,-+/";
  1251. for (p = str; *p; )
  1252. {
  1253. q = p;
  1254. for (i = 0; i < ARRAY_SIZE(wpa_key_mgmt_strings); i++)
  1255. {
  1256. m = wpa_key_mgmt_strings[i].match;
  1257. l = strlen(m);
  1258. if (!strncmp(q, m, l) && (!q[l] || strchr(sep, q[l])))
  1259. {
  1260. if (wpa_key_mgmt_strings[i].version != 0)
  1261. version = wpa_key_mgmt_strings[i].version;
  1262. else
  1263. version = defversion;
  1264. *versions |= version;
  1265. *suites |= wpa_key_mgmt_strings[i].suite;
  1266. q += l;
  1267. break;
  1268. }
  1269. }
  1270. if (q == p)
  1271. q += strcspn(q, sep);
  1272. p = q + strspn(q, sep);
  1273. }
  1274. }
  1275. static struct {
  1276. const char *match;
  1277. int cipher;
  1278. } wpa_cipher_strings[] = {
  1279. { "WEP-104", IWINFO_CIPHER_WEP104 },
  1280. { "WEP-40", IWINFO_CIPHER_WEP40 },
  1281. { "NONE", IWINFO_CIPHER_NONE },
  1282. { "TKIP", IWINFO_CIPHER_TKIP },
  1283. { "CCMP", IWINFO_CIPHER_CCMP },
  1284. { "GCMP", IWINFO_CIPHER_GCMP }
  1285. };
  1286. static void parse_wpa_ciphers(const char *str, uint16_t *ciphers)
  1287. {
  1288. int i;
  1289. size_t l;
  1290. const char *m, *p, *q, *sep = " \t\n,-+/";
  1291. for (p = str; *p; )
  1292. {
  1293. q = p;
  1294. for (i = 0; i < ARRAY_SIZE(wpa_cipher_strings); i++)
  1295. {
  1296. m = wpa_cipher_strings[i].match;
  1297. l = strlen(m);
  1298. if (!strncmp(q, m, l) && (!q[l] || strchr(sep, q[l])))
  1299. {
  1300. *ciphers |= wpa_cipher_strings[i].cipher;
  1301. q += l;
  1302. break;
  1303. }
  1304. }
  1305. if (q == p)
  1306. q += strcspn(q, sep);
  1307. p = q + strspn(q, sep);
  1308. }
  1309. }
  1310. static int nl80211_get_encryption(const char *ifname, char *buf)
  1311. {
  1312. char *p;
  1313. int opmode;
  1314. uint8_t wpa_version = 0;
  1315. char wpa[2], wpa_key_mgmt[64], wpa_pairwise[16], wpa_groupwise[16];
  1316. char auth_algs[2], wep_key0[27], wep_key1[27], wep_key2[27], wep_key3[27];
  1317. char mode[16];
  1318. struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf;
  1319. /* WPA supplicant */
  1320. if (nl80211_wpactl_query(ifname,
  1321. "pairwise_cipher", wpa_pairwise, sizeof(wpa_pairwise),
  1322. "group_cipher", wpa_groupwise, sizeof(wpa_groupwise),
  1323. "key_mgmt", wpa_key_mgmt, sizeof(wpa_key_mgmt),
  1324. "mode", mode, sizeof(mode)))
  1325. {
  1326. /* WEP or Open */
  1327. if (!strcmp(wpa_key_mgmt, "NONE"))
  1328. {
  1329. parse_wpa_ciphers(wpa_pairwise, &c->pair_ciphers);
  1330. parse_wpa_ciphers(wpa_groupwise, &c->group_ciphers);
  1331. if (c->pair_ciphers != 0 && c->pair_ciphers != IWINFO_CIPHER_NONE) {
  1332. c->enabled = 1;
  1333. c->auth_suites = IWINFO_KMGMT_NONE;
  1334. c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
  1335. }
  1336. else {
  1337. c->pair_ciphers = 0;
  1338. c->group_ciphers = 0;
  1339. }
  1340. }
  1341. /* MESH with SAE */
  1342. else if (!strcmp(mode, "mesh") && !strcmp(wpa_key_mgmt, "UNKNOWN"))
  1343. {
  1344. c->enabled = 1;
  1345. c->wpa_version = 4;
  1346. c->auth_suites = IWINFO_KMGMT_SAE;
  1347. c->pair_ciphers = IWINFO_CIPHER_CCMP;
  1348. c->group_ciphers = IWINFO_CIPHER_CCMP;
  1349. }
  1350. /* WPA */
  1351. else
  1352. {
  1353. parse_wpa_ciphers(wpa_pairwise, &c->pair_ciphers);
  1354. parse_wpa_ciphers(wpa_groupwise, &c->group_ciphers);
  1355. p = wpa_key_mgmt;
  1356. if (!strncmp(p, "WPA2-", 5) || !strncmp(p, "WPA2/", 5))
  1357. {
  1358. p += 5;
  1359. wpa_version = 2;
  1360. }
  1361. else if (!strncmp(p, "WPA-", 4))
  1362. {
  1363. p += 4;
  1364. wpa_version = 1;
  1365. }
  1366. parse_wpa_suites(p, wpa_version, &c->wpa_version, &c->auth_suites);
  1367. c->enabled = !!(c->wpa_version && c->auth_suites);
  1368. }
  1369. return 0;
  1370. }
  1371. /* Hostapd */
  1372. else if (nl80211_hostapd_query(ifname,
  1373. "wpa", wpa, sizeof(wpa),
  1374. "wpa_key_mgmt", wpa_key_mgmt, sizeof(wpa_key_mgmt),
  1375. "wpa_pairwise", wpa_pairwise, sizeof(wpa_pairwise),
  1376. "auth_algs", auth_algs, sizeof(auth_algs),
  1377. "wep_key0", wep_key0, sizeof(wep_key0),
  1378. "wep_key1", wep_key1, sizeof(wep_key1),
  1379. "wep_key2", wep_key2, sizeof(wep_key2),
  1380. "wep_key3", wep_key3, sizeof(wep_key3)))
  1381. {
  1382. c->wpa_version = 0;
  1383. if (wpa_key_mgmt[0])
  1384. {
  1385. for (p = strtok(wpa_key_mgmt, " \t"); p != NULL; p = strtok(NULL, " \t"))
  1386. {
  1387. if (!strncmp(p, "WPA-", 4))
  1388. p += 4;
  1389. parse_wpa_suites(p, atoi(wpa), &c->wpa_version, &c->auth_suites);
  1390. }
  1391. c->enabled = c->wpa_version ? 1 : 0;
  1392. }
  1393. if (wpa_pairwise[0])
  1394. parse_wpa_ciphers(wpa_pairwise, &c->pair_ciphers);
  1395. if (auth_algs[0])
  1396. {
  1397. switch (atoi(auth_algs))
  1398. {
  1399. case 1:
  1400. c->auth_algs |= IWINFO_AUTH_OPEN;
  1401. break;
  1402. case 2:
  1403. c->auth_algs |= IWINFO_AUTH_SHARED;
  1404. break;
  1405. case 3:
  1406. c->auth_algs |= IWINFO_AUTH_OPEN;
  1407. c->auth_algs |= IWINFO_AUTH_SHARED;
  1408. break;
  1409. }
  1410. c->pair_ciphers |= nl80211_check_wepkey(wep_key0);
  1411. c->pair_ciphers |= nl80211_check_wepkey(wep_key1);
  1412. c->pair_ciphers |= nl80211_check_wepkey(wep_key2);
  1413. c->pair_ciphers |= nl80211_check_wepkey(wep_key3);
  1414. c->enabled = (c->auth_algs && c->pair_ciphers) ? 1 : 0;
  1415. }
  1416. c->group_ciphers = c->pair_ciphers;
  1417. return 0;
  1418. }
  1419. /* Ad-Hoc or Mesh interfaces without wpa_supplicant are open */
  1420. else if (!nl80211_get_mode(ifname, &opmode) &&
  1421. (opmode == IWINFO_OPMODE_ADHOC ||
  1422. opmode == IWINFO_OPMODE_MESHPOINT))
  1423. {
  1424. c->enabled = 0;
  1425. return 0;
  1426. }
  1427. return -1;
  1428. }
  1429. static int nl80211_get_phyname(const char *ifname, char *buf)
  1430. {
  1431. const char *name;
  1432. name = nl80211_ifname2phy(ifname);
  1433. if (name)
  1434. {
  1435. strcpy(buf, name);
  1436. return 0;
  1437. }
  1438. else if ((name = nl80211_phy2ifname(ifname)) != NULL)
  1439. {
  1440. name = nl80211_ifname2phy(name);
  1441. if (name)
  1442. {
  1443. strcpy(buf, ifname);
  1444. return 0;
  1445. }
  1446. }
  1447. return -1;
  1448. }
  1449. static void nl80211_parse_rateinfo(struct nlattr **ri,
  1450. struct iwinfo_rate_entry *re)
  1451. {
  1452. if (ri[NL80211_RATE_INFO_BITRATE32])
  1453. re->rate = nla_get_u32(ri[NL80211_RATE_INFO_BITRATE32]) * 100;
  1454. else if (ri[NL80211_RATE_INFO_BITRATE])
  1455. re->rate = nla_get_u16(ri[NL80211_RATE_INFO_BITRATE]) * 100;
  1456. if (ri[NL80211_RATE_INFO_VHT_MCS])
  1457. {
  1458. re->is_vht = 1;
  1459. re->mcs = nla_get_u8(ri[NL80211_RATE_INFO_VHT_MCS]);
  1460. if (ri[NL80211_RATE_INFO_VHT_NSS])
  1461. re->nss = nla_get_u8(ri[NL80211_RATE_INFO_VHT_NSS]);
  1462. }
  1463. else if (ri[NL80211_RATE_INFO_MCS])
  1464. {
  1465. re->is_ht = 1;
  1466. re->mcs = nla_get_u8(ri[NL80211_RATE_INFO_MCS]);
  1467. }
  1468. if (ri[NL80211_RATE_INFO_5_MHZ_WIDTH])
  1469. re->mhz = 5;
  1470. else if (ri[NL80211_RATE_INFO_10_MHZ_WIDTH])
  1471. re->mhz = 10;
  1472. else if (ri[NL80211_RATE_INFO_40_MHZ_WIDTH])
  1473. re->mhz = 40;
  1474. else if (ri[NL80211_RATE_INFO_80_MHZ_WIDTH])
  1475. re->mhz = 80;
  1476. else if (ri[NL80211_RATE_INFO_80P80_MHZ_WIDTH] ||
  1477. ri[NL80211_RATE_INFO_160_MHZ_WIDTH])
  1478. re->mhz = 160;
  1479. else
  1480. re->mhz = 20;
  1481. if (ri[NL80211_RATE_INFO_SHORT_GI])
  1482. re->is_short_gi = 1;
  1483. re->is_40mhz = (re->mhz == 40);
  1484. }
  1485. static int nl80211_get_survey_cb(struct nl_msg *msg, void *arg)
  1486. {
  1487. struct nl80211_array_buf *arr = arg;
  1488. struct iwinfo_survey_entry *e = arr->buf;
  1489. struct nlattr **attr = nl80211_parse(msg);
  1490. struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
  1491. int rc;
  1492. static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
  1493. [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
  1494. [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
  1495. [NL80211_SURVEY_INFO_TIME] = { .type = NLA_U64 },
  1496. [NL80211_SURVEY_INFO_TIME_BUSY] = { .type = NLA_U64 },
  1497. [NL80211_SURVEY_INFO_TIME_EXT_BUSY] = { .type = NLA_U64 },
  1498. [NL80211_SURVEY_INFO_TIME_RX] = { .type = NLA_U64 },
  1499. [NL80211_SURVEY_INFO_TIME_TX] = { .type = NLA_U64 },
  1500. };
  1501. rc = nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
  1502. attr[NL80211_ATTR_SURVEY_INFO],
  1503. survey_policy);
  1504. if (rc)
  1505. return NL_SKIP;
  1506. /* advance to end of array */
  1507. e += arr->count;
  1508. memset(e, 0, sizeof(*e));
  1509. if (sinfo[NL80211_SURVEY_INFO_FREQUENCY])
  1510. e->mhz = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
  1511. if (sinfo[NL80211_SURVEY_INFO_NOISE])
  1512. e->noise = nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
  1513. if (sinfo[NL80211_SURVEY_INFO_TIME])
  1514. e->active_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME]);
  1515. if (sinfo[NL80211_SURVEY_INFO_TIME_BUSY])
  1516. e->busy_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_BUSY]);
  1517. if (sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY])
  1518. e->busy_time_ext = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY]);
  1519. if (sinfo[NL80211_SURVEY_INFO_TIME_RX])
  1520. e->rxtime = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_RX]);
  1521. if (sinfo[NL80211_SURVEY_INFO_TIME_TX])
  1522. e->txtime = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_TX]);
  1523. arr->count++;
  1524. return NL_SKIP;
  1525. }
  1526. static void plink_state_to_str(char *dst, unsigned state)
  1527. {
  1528. switch (state) {
  1529. case NL80211_PLINK_LISTEN:
  1530. strcpy(dst, "LISTEN");
  1531. break;
  1532. case NL80211_PLINK_OPN_SNT:
  1533. strcpy(dst, "OPN_SNT");
  1534. break;
  1535. case NL80211_PLINK_OPN_RCVD:
  1536. strcpy(dst, "OPN_RCVD");
  1537. break;
  1538. case NL80211_PLINK_CNF_RCVD:
  1539. strcpy(dst, "CNF_RCVD");
  1540. break;
  1541. case NL80211_PLINK_ESTAB:
  1542. strcpy(dst, "ESTAB");
  1543. break;
  1544. case NL80211_PLINK_HOLDING:
  1545. strcpy(dst, "HOLDING");
  1546. break;
  1547. case NL80211_PLINK_BLOCKED:
  1548. strcpy(dst, "BLOCKED");
  1549. break;
  1550. default:
  1551. strcpy(dst, "UNKNOWN");
  1552. break;
  1553. }
  1554. }
  1555. static void power_mode_to_str(char *dst, struct nlattr *a)
  1556. {
  1557. enum nl80211_mesh_power_mode pm = nla_get_u32(a);
  1558. switch (pm) {
  1559. case NL80211_MESH_POWER_ACTIVE:
  1560. strcpy(dst, "ACTIVE");
  1561. break;
  1562. case NL80211_MESH_POWER_LIGHT_SLEEP:
  1563. strcpy(dst, "LIGHT SLEEP");
  1564. break;
  1565. case NL80211_MESH_POWER_DEEP_SLEEP:
  1566. strcpy(dst, "DEEP SLEEP");
  1567. break;
  1568. default:
  1569. strcpy(dst, "UNKNOWN");
  1570. break;
  1571. }
  1572. }
  1573. static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
  1574. {
  1575. struct nl80211_array_buf *arr = arg;
  1576. struct iwinfo_assoclist_entry *e = arr->buf;
  1577. struct nlattr **attr = nl80211_parse(msg);
  1578. struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
  1579. struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
  1580. struct nl80211_sta_flag_update *sta_flags;
  1581. static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
  1582. [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
  1583. [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
  1584. [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
  1585. [NL80211_STA_INFO_RX_BITRATE] = { .type = NLA_NESTED },
  1586. [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
  1587. [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
  1588. [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
  1589. [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
  1590. [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
  1591. [NL80211_STA_INFO_TX_RETRIES] = { .type = NLA_U32 },
  1592. [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
  1593. [NL80211_STA_INFO_CONNECTED_TIME]= { .type = NLA_U32 },
  1594. [NL80211_STA_INFO_RX_DROP_MISC] = { .type = NLA_U64 },
  1595. [NL80211_STA_INFO_T_OFFSET] = { .type = NLA_U64 },
  1596. [NL80211_STA_INFO_STA_FLAGS] =
  1597. { .minlen = sizeof(struct nl80211_sta_flag_update) },
  1598. [NL80211_STA_INFO_EXPECTED_THROUGHPUT] = { .type = NLA_U32 },
  1599. /* mesh */
  1600. [NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
  1601. [NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
  1602. [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
  1603. [NL80211_STA_INFO_LOCAL_PM] = { .type = NLA_U32 },
  1604. [NL80211_STA_INFO_PEER_PM] = { .type = NLA_U32 },
  1605. [NL80211_STA_INFO_NONPEER_PM] = { .type = NLA_U32 },
  1606. };
  1607. static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
  1608. [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
  1609. [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
  1610. [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
  1611. [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
  1612. };
  1613. /* advance to end of array */
  1614. e += arr->count;
  1615. memset(e, 0, sizeof(*e));
  1616. if (attr[NL80211_ATTR_MAC])
  1617. memcpy(e->mac, nla_data(attr[NL80211_ATTR_MAC]), 6);
  1618. if (attr[NL80211_ATTR_STA_INFO] &&
  1619. !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
  1620. attr[NL80211_ATTR_STA_INFO], stats_policy))
  1621. {
  1622. if (sinfo[NL80211_STA_INFO_SIGNAL])
  1623. e->signal = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
  1624. if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
  1625. e->signal_avg = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
  1626. if (sinfo[NL80211_STA_INFO_INACTIVE_TIME])
  1627. e->inactive = nla_get_u32(sinfo[NL80211_STA_INFO_INACTIVE_TIME]);
  1628. if (sinfo[NL80211_STA_INFO_CONNECTED_TIME])
  1629. e->connected_time = nla_get_u32(sinfo[NL80211_STA_INFO_CONNECTED_TIME]);
  1630. if (sinfo[NL80211_STA_INFO_RX_PACKETS])
  1631. e->rx_packets = nla_get_u32(sinfo[NL80211_STA_INFO_RX_PACKETS]);
  1632. if (sinfo[NL80211_STA_INFO_TX_PACKETS])
  1633. e->tx_packets = nla_get_u32(sinfo[NL80211_STA_INFO_TX_PACKETS]);
  1634. if (sinfo[NL80211_STA_INFO_RX_BITRATE] &&
  1635. !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
  1636. sinfo[NL80211_STA_INFO_RX_BITRATE], rate_policy))
  1637. nl80211_parse_rateinfo(rinfo, &e->rx_rate);
  1638. if (sinfo[NL80211_STA_INFO_TX_BITRATE] &&
  1639. !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
  1640. sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy))
  1641. nl80211_parse_rateinfo(rinfo, &e->tx_rate);
  1642. if (sinfo[NL80211_STA_INFO_RX_BYTES])
  1643. e->rx_bytes = nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES]);
  1644. if (sinfo[NL80211_STA_INFO_TX_BYTES])
  1645. e->tx_bytes = nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES]);
  1646. if (sinfo[NL80211_STA_INFO_TX_RETRIES])
  1647. e->tx_retries = nla_get_u32(sinfo[NL80211_STA_INFO_TX_RETRIES]);
  1648. if (sinfo[NL80211_STA_INFO_TX_FAILED])
  1649. e->tx_failed = nla_get_u32(sinfo[NL80211_STA_INFO_TX_FAILED]);
  1650. if (sinfo[NL80211_STA_INFO_T_OFFSET])
  1651. e->t_offset = nla_get_u64(sinfo[NL80211_STA_INFO_T_OFFSET]);
  1652. if (sinfo[NL80211_STA_INFO_RX_DROP_MISC])
  1653. e->rx_drop_misc = nla_get_u64(sinfo[NL80211_STA_INFO_RX_DROP_MISC]);
  1654. if (sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT])
  1655. e->thr = nla_get_u32(sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT]);
  1656. /* mesh */
  1657. if (sinfo[NL80211_STA_INFO_LLID])
  1658. e->llid = nla_get_u16(sinfo[NL80211_STA_INFO_LLID]);
  1659. if (sinfo[NL80211_STA_INFO_PLID])
  1660. e->plid = nla_get_u16(sinfo[NL80211_STA_INFO_PLID]);
  1661. if (sinfo[NL80211_STA_INFO_PLINK_STATE])
  1662. plink_state_to_str(e->plink_state,
  1663. nla_get_u8(sinfo[NL80211_STA_INFO_PLINK_STATE]));
  1664. if (sinfo[NL80211_STA_INFO_LOCAL_PM])
  1665. power_mode_to_str(e->local_ps, sinfo[NL80211_STA_INFO_LOCAL_PM]);
  1666. if (sinfo[NL80211_STA_INFO_PEER_PM])
  1667. power_mode_to_str(e->peer_ps, sinfo[NL80211_STA_INFO_PEER_PM]);
  1668. if (sinfo[NL80211_STA_INFO_NONPEER_PM])
  1669. power_mode_to_str(e->nonpeer_ps, sinfo[NL80211_STA_INFO_NONPEER_PM]);
  1670. /* Station flags */
  1671. if (sinfo[NL80211_STA_INFO_STA_FLAGS])
  1672. {
  1673. sta_flags = (struct nl80211_sta_flag_update *)
  1674. nla_data(sinfo[NL80211_STA_INFO_STA_FLAGS]);
  1675. if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
  1676. sta_flags->set & BIT(NL80211_STA_FLAG_AUTHORIZED))
  1677. e->is_authorized = 1;
  1678. if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHENTICATED) &&
  1679. sta_flags->set & BIT(NL80211_STA_FLAG_AUTHENTICATED))
  1680. e->is_authenticated = 1;
  1681. if (sta_flags->mask & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) &&
  1682. sta_flags->set & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE))
  1683. e->is_preamble_short = 1;
  1684. if (sta_flags->mask & BIT(NL80211_STA_FLAG_WME) &&
  1685. sta_flags->set & BIT(NL80211_STA_FLAG_WME))
  1686. e->is_wme = 1;
  1687. if (sta_flags->mask & BIT(NL80211_STA_FLAG_MFP) &&
  1688. sta_flags->set & BIT(NL80211_STA_FLAG_MFP))
  1689. e->is_mfp = 1;
  1690. if (sta_flags->mask & BIT(NL80211_STA_FLAG_TDLS_PEER) &&
  1691. sta_flags->set & BIT(NL80211_STA_FLAG_TDLS_PEER))
  1692. e->is_tdls = 1;
  1693. }
  1694. }
  1695. e->noise = 0; /* filled in by caller */
  1696. arr->count++;
  1697. return NL_SKIP;
  1698. }
  1699. static int nl80211_get_survey(const char *ifname, char *buf, int *len)
  1700. {
  1701. struct nl80211_array_buf arr = { .buf = buf, .count = 0 };
  1702. int rc;
  1703. rc = nl80211_request(ifname, NL80211_CMD_GET_SURVEY,
  1704. NLM_F_DUMP, nl80211_get_survey_cb, &arr);
  1705. if (!rc)
  1706. *len = (arr.count * sizeof(struct iwinfo_survey_entry));
  1707. else
  1708. *len = 0;
  1709. return 0;
  1710. }
  1711. static int nl80211_get_assoclist(const char *ifname, char *buf, int *len)
  1712. {
  1713. DIR *d;
  1714. int i, noise = 0;
  1715. struct dirent *de;
  1716. struct nl80211_array_buf arr = { .buf = buf, .count = 0 };
  1717. struct iwinfo_assoclist_entry *e;
  1718. if ((d = opendir("/sys/class/net")) != NULL)
  1719. {
  1720. while ((de = readdir(d)) != NULL)
  1721. {
  1722. if (!strncmp(de->d_name, ifname, strlen(ifname)) &&
  1723. (!de->d_name[strlen(ifname)] ||
  1724. !strncmp(&de->d_name[strlen(ifname)], ".sta", 4)))
  1725. {
  1726. nl80211_request(de->d_name, NL80211_CMD_GET_STATION,
  1727. NLM_F_DUMP, nl80211_get_assoclist_cb, &arr);
  1728. }
  1729. }
  1730. closedir(d);
  1731. if (!nl80211_get_noise(ifname, &noise))
  1732. for (i = 0, e = arr.buf; i < arr.count; i++, e++)
  1733. e->noise = noise;
  1734. *len = (arr.count * sizeof(struct iwinfo_assoclist_entry));
  1735. return 0;
  1736. }
  1737. return -1;
  1738. }
  1739. static int nl80211_get_txpwrlist_cb(struct nl_msg *msg, void *arg)
  1740. {
  1741. int *dbm_max = arg;
  1742. int ch_cur, ch_cmp, bands_remain, freqs_remain;
  1743. struct nlattr **attr = nl80211_parse(msg);
  1744. struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
  1745. struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
  1746. struct nlattr *band, *freq;
  1747. static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
  1748. [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
  1749. [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
  1750. [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
  1751. [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
  1752. [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
  1753. [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
  1754. };
  1755. ch_cur = *dbm_max; /* value int* is initialized with channel by caller */
  1756. *dbm_max = -1;
  1757. nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
  1758. {
  1759. nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
  1760. nla_len(band), NULL);
  1761. nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
  1762. {
  1763. nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
  1764. nla_data(freq), nla_len(freq), freq_policy);
  1765. ch_cmp = nl80211_freq2channel(nla_get_u32(
  1766. freqs[NL80211_FREQUENCY_ATTR_FREQ]));
  1767. if ((!ch_cur || (ch_cmp == ch_cur)) &&
  1768. freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER])
  1769. {
  1770. *dbm_max = (int)(0.01 * nla_get_u32(
  1771. freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]));
  1772. break;
  1773. }
  1774. }
  1775. }
  1776. return NL_SKIP;
  1777. }
  1778. static int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len)
  1779. {
  1780. int err, ch_cur;
  1781. int dbm_max = -1, dbm_cur, dbm_cnt;
  1782. struct nl80211_msg_conveyor *req;
  1783. struct iwinfo_txpwrlist_entry entry;
  1784. if (nl80211_get_channel(ifname, &ch_cur))
  1785. ch_cur = 0;
  1786. /* initialize the value pointer with channel for callback */
  1787. dbm_max = ch_cur;
  1788. err = nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
  1789. nl80211_get_txpwrlist_cb, &dbm_max);
  1790. if (!err)
  1791. {
  1792. for (dbm_cur = 0, dbm_cnt = 0;
  1793. dbm_cur < dbm_max;
  1794. dbm_cur++, dbm_cnt++)
  1795. {
  1796. entry.dbm = dbm_cur;
  1797. entry.mw = iwinfo_dbm2mw(dbm_cur);
  1798. memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
  1799. }
  1800. entry.dbm = dbm_max;
  1801. entry.mw = iwinfo_dbm2mw(dbm_max);
  1802. memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
  1803. dbm_cnt++;
  1804. *len = dbm_cnt * sizeof(entry);
  1805. return 0;
  1806. }
  1807. return -1;
  1808. }
  1809. static void nl80211_get_scancrypto(char *spec, struct iwinfo_crypto_entry *c)
  1810. {
  1811. int wpa_version = 0;
  1812. char *p, *q, *proto, *suites;
  1813. c->enabled = 0;
  1814. for (p = strtok_r(spec, "[]", &q); p; p = strtok_r(NULL, "[]", &q)) {
  1815. if (!strcmp(p, "WEP")) {
  1816. c->enabled = 1;
  1817. c->auth_suites = IWINFO_KMGMT_NONE;
  1818. c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
  1819. c->pair_ciphers = IWINFO_CIPHER_WEP40 | IWINFO_CIPHER_WEP104;
  1820. break;
  1821. }
  1822. proto = strtok(p, "-");
  1823. suites = strtok(NULL, "]");
  1824. if (!proto || !suites)
  1825. continue;
  1826. if (!strcmp(proto, "WPA2") || !strcmp(proto, "RSN"))
  1827. wpa_version = 2;
  1828. else if (!strcmp(proto, "WPA"))
  1829. wpa_version = 1;
  1830. else
  1831. continue;
  1832. c->enabled = 1;
  1833. parse_wpa_suites(suites, wpa_version, &c->wpa_version, &c->auth_suites);
  1834. parse_wpa_ciphers(suites, &c->pair_ciphers);
  1835. }
  1836. }
  1837. struct nl80211_scanlist {
  1838. struct iwinfo_scanlist_entry *e;
  1839. int len;
  1840. };
  1841. static void nl80211_get_scanlist_ie(struct nlattr **bss,
  1842. struct iwinfo_scanlist_entry *e)
  1843. {
  1844. int ielen = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
  1845. unsigned char *ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
  1846. static unsigned char ms_oui[3] = { 0x00, 0x50, 0xf2 };
  1847. int len;
  1848. while (ielen >= 2 && ielen >= ie[1])
  1849. {
  1850. switch (ie[0])
  1851. {
  1852. case 0: /* SSID */
  1853. case 114: /* Mesh ID */
  1854. if (e->ssid[0] == 0) {
  1855. len = min(ie[1], IWINFO_ESSID_MAX_SIZE);
  1856. memcpy(e->ssid, ie + 2, len);
  1857. e->ssid[len] = 0;
  1858. }
  1859. break;
  1860. case 48: /* RSN */
  1861. iwinfo_parse_rsn(&e->crypto, ie + 2, ie[1],
  1862. IWINFO_CIPHER_CCMP, IWINFO_KMGMT_8021x);
  1863. break;
  1864. case 221: /* Vendor */
  1865. if (ie[1] >= 4 && !memcmp(ie + 2, ms_oui, 3) && ie[5] == 1)
  1866. iwinfo_parse_rsn(&e->crypto, ie + 6, ie[1] - 4,
  1867. IWINFO_CIPHER_TKIP, IWINFO_KMGMT_PSK);
  1868. break;
  1869. case 61: /* HT oeration */
  1870. if (ie[1] >= 3) {
  1871. e->ht_chan_info.primary_chan = ie[2];
  1872. e->ht_chan_info.secondary_chan_off = ie[3] & 0x3;
  1873. e->ht_chan_info.chan_width = (ie[4] & 0x4)>>2;
  1874. }
  1875. break;
  1876. case 192: /* VHT operation */
  1877. if (ie[1] >= 3) {
  1878. e->vht_chan_info.chan_width = ie[2];
  1879. e->vht_chan_info.center_chan_1 = ie[3];
  1880. e->vht_chan_info.center_chan_2 = ie[4];
  1881. }
  1882. break;
  1883. }
  1884. ielen -= ie[1] + 2;
  1885. ie += ie[1] + 2;
  1886. }
  1887. }
  1888. static int nl80211_get_scanlist_cb(struct nl_msg *msg, void *arg)
  1889. {
  1890. int8_t rssi;
  1891. uint16_t caps;
  1892. struct nl80211_scanlist *sl = arg;
  1893. struct nlattr **tb = nl80211_parse(msg);
  1894. struct nlattr *bss[NL80211_BSS_MAX + 1];
  1895. static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
  1896. [NL80211_BSS_TSF] = { .type = NLA_U64 },
  1897. [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
  1898. [NL80211_BSS_BSSID] = { 0 },
  1899. [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
  1900. [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
  1901. [NL80211_BSS_INFORMATION_ELEMENTS] = { 0 },
  1902. [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
  1903. [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
  1904. [NL80211_BSS_STATUS] = { .type = NLA_U32 },
  1905. [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
  1906. [NL80211_BSS_BEACON_IES] = { 0 },
  1907. };
  1908. if (!tb[NL80211_ATTR_BSS] ||
  1909. nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
  1910. bss_policy) ||
  1911. !bss[NL80211_BSS_BSSID])
  1912. {
  1913. return NL_SKIP;
  1914. }
  1915. if (bss[NL80211_BSS_CAPABILITY])
  1916. caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
  1917. else
  1918. caps = 0;
  1919. memset(sl->e, 0, sizeof(*sl->e));
  1920. memcpy(sl->e->mac, nla_data(bss[NL80211_BSS_BSSID]), 6);
  1921. if (caps & (1<<1))
  1922. sl->e->mode = IWINFO_OPMODE_ADHOC;
  1923. else if (caps & (1<<0))
  1924. sl->e->mode = IWINFO_OPMODE_MASTER;
  1925. else
  1926. sl->e->mode = IWINFO_OPMODE_MESHPOINT;
  1927. if (caps & (1<<4))
  1928. sl->e->crypto.enabled = 1;
  1929. if (bss[NL80211_BSS_FREQUENCY])
  1930. sl->e->channel = nl80211_freq2channel(nla_get_u32(
  1931. bss[NL80211_BSS_FREQUENCY]));
  1932. if (bss[NL80211_BSS_INFORMATION_ELEMENTS])
  1933. nl80211_get_scanlist_ie(bss, sl->e);
  1934. if (bss[NL80211_BSS_SIGNAL_MBM])
  1935. {
  1936. sl->e->signal =
  1937. (uint8_t)((int32_t)nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]) / 100);
  1938. rssi = sl->e->signal - 0x100;
  1939. if (rssi < -110)
  1940. rssi = -110;
  1941. else if (rssi > -40)
  1942. rssi = -40;
  1943. sl->e->quality = (rssi + 110);
  1944. sl->e->quality_max = 70;
  1945. }
  1946. if (sl->e->crypto.enabled && !sl->e->crypto.wpa_version)
  1947. {
  1948. sl->e->crypto.auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
  1949. sl->e->crypto.pair_ciphers = IWINFO_CIPHER_WEP40 | IWINFO_CIPHER_WEP104;
  1950. }
  1951. sl->e++;
  1952. sl->len++;
  1953. return NL_SKIP;
  1954. }
  1955. static int nl80211_get_scanlist_nl(const char *ifname, char *buf, int *len)
  1956. {
  1957. struct nl80211_scanlist sl = { .e = (struct iwinfo_scanlist_entry *)buf };
  1958. if (nl80211_request(ifname, NL80211_CMD_TRIGGER_SCAN, 0, NULL, NULL))
  1959. goto out;
  1960. if (nl80211_wait("nl80211", "scan",
  1961. NL80211_CMD_NEW_SCAN_RESULTS, NL80211_CMD_SCAN_ABORTED))
  1962. goto out;
  1963. if (nl80211_request(ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP,
  1964. nl80211_get_scanlist_cb, &sl))
  1965. goto out;
  1966. *len = sl.len * sizeof(struct iwinfo_scanlist_entry);
  1967. return 0;
  1968. out:
  1969. *len = 0;
  1970. return -1;
  1971. }
  1972. static int wpasupp_ssid_decode(const char *in, char *out, int outlen)
  1973. {
  1974. #define hex(x) \
  1975. (((x) >= 'a') ? ((x) - 'a' + 10) : \
  1976. (((x) >= 'A') ? ((x) - 'A' + 10) : ((x) - '0')))
  1977. int len = 0;
  1978. while (*in)
  1979. {
  1980. if (len + 1 >= outlen)
  1981. break;
  1982. switch (*in)
  1983. {
  1984. case '\\':
  1985. in++;
  1986. switch (*in)
  1987. {
  1988. case 'n':
  1989. out[len++] = '\n'; in++;
  1990. break;
  1991. case 'r':
  1992. out[len++] = '\r'; in++;
  1993. break;
  1994. case 't':
  1995. out[len++] = '\t'; in++;
  1996. break;
  1997. case 'e':
  1998. out[len++] = '\033'; in++;
  1999. break;
  2000. case 'x':
  2001. if (isxdigit(*(in+1)) && isxdigit(*(in+2)))
  2002. out[len++] = hex(*(in+1)) * 16 + hex(*(in+2));
  2003. in += 3;
  2004. break;
  2005. default:
  2006. out[len++] = *in++;
  2007. break;
  2008. }
  2009. break;
  2010. default:
  2011. out[len++] = *in++;
  2012. break;
  2013. }
  2014. }
  2015. if (outlen > len)
  2016. out[len] = '\0';
  2017. return len;
  2018. }
  2019. static int nl80211_get_scanlist_wpactl(const char *ifname, char *buf, int *len)
  2020. {
  2021. int sock, qmax, rssi, tries, count = -1, ready = 0;
  2022. char *pos, *line, *bssid, *freq, *signal, *flags, *ssid, reply[4096];
  2023. struct sockaddr_un local = { 0 };
  2024. struct iwinfo_scanlist_entry *e = (struct iwinfo_scanlist_entry *)buf;
  2025. sock = nl80211_wpactl_connect(ifname, &local);
  2026. if (sock < 0)
  2027. return sock;
  2028. send(sock, "ATTACH", 6, 0);
  2029. send(sock, "SCAN", 4, 0);
  2030. /*
  2031. * wait for scan results:
  2032. * nl80211_wpactl_recv() will use a timeout of 256ms and we need to scan
  2033. * 72 channels at most. We'll also receive two "OK" messages acknowledging
  2034. * the "ATTACH" and "SCAN" commands and the driver might need a bit extra
  2035. * time to process the results, so try 72 + 2 + 1 times.
  2036. */
  2037. for (tries = 0; tries < 75; tries++)
  2038. {
  2039. if (nl80211_wpactl_recv(sock, reply, sizeof(reply)) <= 0)
  2040. continue;
  2041. /* got an event notification */
  2042. if (reply[0] == '<')
  2043. {
  2044. /* scan results are ready */
  2045. if (strstr(reply, "CTRL-EVENT-SCAN-RESULTS"))
  2046. {
  2047. /* send "SCAN_RESULTS" command */
  2048. ready = (send(sock, "SCAN_RESULTS", 12, 0) == 12);
  2049. break;
  2050. }
  2051. /* is another unrelated event, retry */
  2052. tries--;
  2053. }
  2054. /* scanning already in progress, keep awaiting results */
  2055. else if (!strcmp(reply, "FAIL-BUSY\n"))
  2056. {
  2057. tries--;
  2058. }
  2059. /* another failure, abort */
  2060. else if (!strncmp(reply, "FAIL-", 5))
  2061. {
  2062. break;
  2063. }
  2064. }
  2065. /* receive and parse scan results if the wait above didn't time out */
  2066. while (ready && nl80211_wpactl_recv(sock, reply, sizeof(reply)) > 0)
  2067. {
  2068. /* received an event notification, receive again */
  2069. if (reply[0] == '<')
  2070. continue;
  2071. nl80211_get_quality_max(ifname, &qmax);
  2072. for (line = strtok_r(reply, "\n", &pos);
  2073. line != NULL;
  2074. line = strtok_r(NULL, "\n", &pos))
  2075. {
  2076. /* skip header line */
  2077. if (count < 0)
  2078. {
  2079. count++;
  2080. continue;
  2081. }
  2082. bssid = strtok(line, "\t");
  2083. freq = strtok(NULL, "\t");
  2084. signal = strtok(NULL, "\t");
  2085. flags = strtok(NULL, "\t");
  2086. ssid = strtok(NULL, "\n");
  2087. if (!bssid || !freq || !signal || !flags)
  2088. continue;
  2089. /* BSSID */
  2090. e->mac[0] = strtol(&bssid[0], NULL, 16);
  2091. e->mac[1] = strtol(&bssid[3], NULL, 16);
  2092. e->mac[2] = strtol(&bssid[6], NULL, 16);
  2093. e->mac[3] = strtol(&bssid[9], NULL, 16);
  2094. e->mac[4] = strtol(&bssid[12], NULL, 16);
  2095. e->mac[5] = strtol(&bssid[15], NULL, 16);
  2096. /* SSID */
  2097. if (ssid)
  2098. wpasupp_ssid_decode(ssid, e->ssid, sizeof(e->ssid));
  2099. else
  2100. e->ssid[0] = 0;
  2101. /* Mode */
  2102. if (strstr(flags, "[MESH]"))
  2103. e->mode = IWINFO_OPMODE_MESHPOINT;
  2104. else if (strstr(flags, "[IBSS]"))
  2105. e->mode = IWINFO_OPMODE_ADHOC;
  2106. else
  2107. e->mode = IWINFO_OPMODE_MASTER;
  2108. /* Channel */
  2109. e->channel = nl80211_freq2channel(atoi(freq));
  2110. /* Signal */
  2111. rssi = atoi(signal);
  2112. e->signal = rssi;
  2113. /* Quality */
  2114. if (rssi < 0)
  2115. {
  2116. /* The cfg80211 wext compat layer assumes a signal range
  2117. * of -110 dBm to -40 dBm, the quality value is derived
  2118. * by adding 110 to the signal level */
  2119. if (rssi < -110)
  2120. rssi = -110;
  2121. else if (rssi > -40)
  2122. rssi = -40;
  2123. e->quality = (rssi + 110);
  2124. }
  2125. else
  2126. {
  2127. e->quality = rssi;
  2128. }
  2129. /* Max. Quality */
  2130. e->quality_max = qmax;
  2131. /* Crypto */
  2132. nl80211_get_scancrypto(flags, &e->crypto);
  2133. count++;
  2134. e++;
  2135. }
  2136. *len = count * sizeof(struct iwinfo_scanlist_entry);
  2137. break;
  2138. }
  2139. close(sock);
  2140. unlink(local.sun_path);
  2141. return (count >= 0) ? 0 : -1;
  2142. }
  2143. static int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
  2144. {
  2145. char *res;
  2146. int rv, mode;
  2147. *len = 0;
  2148. /* Got a radioX pseudo interface, find some interface on it or create one */
  2149. if (!strncmp(ifname, "radio", 5))
  2150. {
  2151. /* Reuse existing interface */
  2152. if ((res = nl80211_phy2ifname(ifname)) != NULL)
  2153. {
  2154. return nl80211_get_scanlist(res, buf, len);
  2155. }
  2156. /* Need to spawn a temporary iface for scanning */
  2157. else if ((res = nl80211_ifadd(ifname)) != NULL)
  2158. {
  2159. rv = nl80211_get_scanlist(res, buf, len);
  2160. nl80211_ifdel(res);
  2161. return rv;
  2162. }
  2163. }
  2164. /* WPA supplicant */
  2165. if (!nl80211_get_scanlist_wpactl(ifname, buf, len))
  2166. {
  2167. return 0;
  2168. }
  2169. /* station / ad-hoc / monitor scan */
  2170. else if (!nl80211_get_mode(ifname, &mode) &&
  2171. (mode == IWINFO_OPMODE_ADHOC ||
  2172. mode == IWINFO_OPMODE_MASTER ||
  2173. mode == IWINFO_OPMODE_CLIENT ||
  2174. mode == IWINFO_OPMODE_MONITOR) &&
  2175. iwinfo_ifup(ifname))
  2176. {
  2177. return nl80211_get_scanlist_nl(ifname, buf, len);
  2178. }
  2179. /* AP scan */
  2180. else
  2181. {
  2182. /* Got a temp interface, don't create yet another one */
  2183. if (!strncmp(ifname, "tmp.", 4))
  2184. {
  2185. if (!iwinfo_ifup(ifname))
  2186. return -1;
  2187. rv = nl80211_get_scanlist_nl(ifname, buf, len);
  2188. iwinfo_ifdown(ifname);
  2189. return rv;
  2190. }
  2191. /* Spawn a new scan interface */
  2192. else
  2193. {
  2194. if (!(res = nl80211_ifadd(ifname)))
  2195. return -1;
  2196. iwinfo_ifmac(res);
  2197. /* if we can take the new interface up, the driver supports an
  2198. * additional interface and there's no need to tear down the ap */
  2199. if (iwinfo_ifup(res))
  2200. {
  2201. rv = nl80211_get_scanlist_nl(res, buf, len);
  2202. iwinfo_ifdown(res);
  2203. }
  2204. /* driver cannot create secondary interface, take down ap
  2205. * during scan */
  2206. else if (iwinfo_ifdown(ifname) && iwinfo_ifup(res))
  2207. {
  2208. rv = nl80211_get_scanlist_nl(res, buf, len);
  2209. iwinfo_ifdown(res);
  2210. iwinfo_ifup(ifname);
  2211. nl80211_hostapd_hup(ifname);
  2212. }
  2213. nl80211_ifdel(res);
  2214. return rv;
  2215. }
  2216. }
  2217. return -1;
  2218. }
  2219. static int nl80211_get_freqlist_cb(struct nl_msg *msg, void *arg)
  2220. {
  2221. int bands_remain, freqs_remain;
  2222. struct nl80211_array_buf *arr = arg;
  2223. struct iwinfo_freqlist_entry *e;
  2224. struct nlattr **attr = nl80211_parse(msg);
  2225. struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
  2226. struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
  2227. struct nlattr *band, *freq;
  2228. e = arr->buf;
  2229. e += arr->count;
  2230. if (attr[NL80211_ATTR_WIPHY_BANDS]) {
  2231. nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
  2232. {
  2233. nla_parse(bands, NL80211_BAND_ATTR_MAX,
  2234. nla_data(band), nla_len(band), NULL);
  2235. if (bands[NL80211_BAND_ATTR_FREQS]) {
  2236. nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
  2237. {
  2238. nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
  2239. nla_data(freq), nla_len(freq), NULL);
  2240. if (!freqs[NL80211_FREQUENCY_ATTR_FREQ] ||
  2241. freqs[NL80211_FREQUENCY_ATTR_DISABLED])
  2242. continue;
  2243. e->mhz = nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]);
  2244. e->channel = nl80211_freq2channel(e->mhz);
  2245. e->restricted = (
  2246. freqs[NL80211_FREQUENCY_ATTR_NO_IR] &&
  2247. !freqs[NL80211_FREQUENCY_ATTR_RADAR]
  2248. ) ? 1 : 0;
  2249. if (freqs[NL80211_FREQUENCY_ATTR_NO_HT40_MINUS])
  2250. e->flags |= IWINFO_FREQ_NO_HT40MINUS;
  2251. if (freqs[NL80211_FREQUENCY_ATTR_NO_HT40_PLUS])
  2252. e->flags |= IWINFO_FREQ_NO_HT40PLUS;
  2253. if (freqs[NL80211_FREQUENCY_ATTR_NO_80MHZ])
  2254. e->flags |= IWINFO_FREQ_NO_80MHZ;
  2255. if (freqs[NL80211_FREQUENCY_ATTR_NO_160MHZ])
  2256. e->flags |= IWINFO_FREQ_NO_160MHZ;
  2257. if (freqs[NL80211_FREQUENCY_ATTR_NO_20MHZ])
  2258. e->flags |= IWINFO_FREQ_NO_20MHZ;
  2259. if (freqs[NL80211_FREQUENCY_ATTR_NO_10MHZ])
  2260. e->flags |= IWINFO_FREQ_NO_10MHZ;
  2261. e++;
  2262. arr->count++;
  2263. }
  2264. }
  2265. }
  2266. }
  2267. return NL_SKIP;
  2268. }
  2269. static int nl80211_get_freqlist(const char *ifname, char *buf, int *len)
  2270. {
  2271. struct nl80211_msg_conveyor *cv;
  2272. struct nl80211_array_buf arr = { .buf = buf, .count = 0 };
  2273. uint32_t features = nl80211_get_protocol_features(ifname);
  2274. int flags;
  2275. flags = features & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP ? NLM_F_DUMP : 0;
  2276. cv = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, flags);
  2277. if (!cv)
  2278. goto out;
  2279. NLA_PUT_FLAG(cv->msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
  2280. if (nl80211_send(cv, nl80211_get_freqlist_cb, &arr))
  2281. goto out;
  2282. *len = arr.count * sizeof(struct iwinfo_freqlist_entry);
  2283. return 0;
  2284. nla_put_failure:
  2285. nl80211_free(cv);
  2286. out:
  2287. *len = 0;
  2288. return -1;
  2289. }
  2290. static int nl80211_get_country_cb(struct nl_msg *msg, void *arg)
  2291. {
  2292. char *buf = arg;
  2293. struct nlattr **attr = nl80211_parse(msg);
  2294. if (attr[NL80211_ATTR_REG_ALPHA2])
  2295. memcpy(buf, nla_data(attr[NL80211_ATTR_REG_ALPHA2]), 2);
  2296. else
  2297. buf[0] = 0;
  2298. return NL_SKIP;
  2299. }
  2300. static int nl80211_get_country(const char *ifname, char *buf)
  2301. {
  2302. if (nl80211_request(ifname, NL80211_CMD_GET_REG, 0,
  2303. nl80211_get_country_cb, buf))
  2304. return -1;
  2305. return 0;
  2306. }
  2307. static int nl80211_get_countrylist(const char *ifname, char *buf, int *len)
  2308. {
  2309. int count;
  2310. struct iwinfo_country_entry *e = (struct iwinfo_country_entry *)buf;
  2311. const struct iwinfo_iso3166_label *l;
  2312. for (l = IWINFO_ISO3166_NAMES, count = 0; l->iso3166; l++, e++, count++)
  2313. {
  2314. e->iso3166 = l->iso3166;
  2315. e->ccode[0] = (l->iso3166 / 256);
  2316. e->ccode[1] = (l->iso3166 % 256);
  2317. e->ccode[2] = 0;
  2318. }
  2319. *len = (count * sizeof(struct iwinfo_country_entry));
  2320. return 0;
  2321. }
  2322. struct nl80211_modes
  2323. {
  2324. bool ok;
  2325. uint32_t hw;
  2326. uint32_t ht;
  2327. };
  2328. static int nl80211_get_modelist_cb(struct nl_msg *msg, void *arg)
  2329. {
  2330. struct nl80211_modes *m = arg;
  2331. int bands_remain, freqs_remain;
  2332. uint16_t caps = 0;
  2333. uint32_t vht_caps = 0;
  2334. struct nlattr **attr = nl80211_parse(msg);
  2335. struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
  2336. struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
  2337. struct nlattr *band, *freq;
  2338. if (attr[NL80211_ATTR_WIPHY_BANDS])
  2339. {
  2340. nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
  2341. {
  2342. nla_parse(bands, NL80211_BAND_ATTR_MAX,
  2343. nla_data(band), nla_len(band), NULL);
  2344. if (bands[NL80211_BAND_ATTR_HT_CAPA])
  2345. caps = nla_get_u16(bands[NL80211_BAND_ATTR_HT_CAPA]);
  2346. /* Treat any nonzero capability as 11n */
  2347. if (caps > 0)
  2348. {
  2349. m->hw |= IWINFO_80211_N;
  2350. m->ht |= IWINFO_HTMODE_HT20;
  2351. if (caps & (1 << 1))
  2352. m->ht |= IWINFO_HTMODE_HT40;
  2353. }
  2354. nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS],
  2355. freqs_remain)
  2356. {
  2357. nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
  2358. nla_data(freq), nla_len(freq), NULL);
  2359. if (!freqs[NL80211_FREQUENCY_ATTR_FREQ])
  2360. continue;
  2361. if (nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]) < 2485)
  2362. {
  2363. m->hw |= IWINFO_80211_B;
  2364. m->hw |= IWINFO_80211_G;
  2365. }
  2366. else if (bands[NL80211_BAND_ATTR_VHT_CAPA])
  2367. {
  2368. vht_caps = nla_get_u32(bands[NL80211_BAND_ATTR_VHT_CAPA]);
  2369. /* Treat any nonzero capability as 11ac */
  2370. if (vht_caps > 0)
  2371. {
  2372. m->hw |= IWINFO_80211_AC;
  2373. m->ht |= IWINFO_HTMODE_VHT20 | IWINFO_HTMODE_VHT40 | IWINFO_HTMODE_VHT80;
  2374. switch ((vht_caps >> 2) & 3)
  2375. {
  2376. case 2:
  2377. m->ht |= IWINFO_HTMODE_VHT80_80;
  2378. /* fall through */
  2379. case 1:
  2380. m->ht |= IWINFO_HTMODE_VHT160;
  2381. }
  2382. }
  2383. }
  2384. else if (nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]) >= 56160)
  2385. {
  2386. m->hw |= IWINFO_80211_AD;
  2387. }
  2388. else if (!(m->hw & IWINFO_80211_AC))
  2389. {
  2390. m->hw |= IWINFO_80211_A;
  2391. }
  2392. }
  2393. }
  2394. m->ok = 1;
  2395. }
  2396. return NL_SKIP;
  2397. }
  2398. static int nl80211_get_hwmodelist(const char *ifname, int *buf)
  2399. {
  2400. struct nl80211_modes m = { 0 };
  2401. if (nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
  2402. nl80211_get_modelist_cb, &m))
  2403. goto out;
  2404. if (!m.ok)
  2405. goto out;
  2406. *buf = m.hw;
  2407. return 0;
  2408. out:
  2409. *buf = 0;
  2410. return -1;
  2411. }
  2412. struct chan_info {
  2413. int width;
  2414. int mode;
  2415. };
  2416. static int nl80211_get_htmode_cb(struct nl_msg *msg, void *arg)
  2417. {
  2418. struct nlattr **tb = nl80211_parse(msg);
  2419. struct nlattr *cur;
  2420. struct chan_info *chn = arg;
  2421. if ((cur = tb[NL80211_ATTR_CHANNEL_WIDTH]))
  2422. chn->width = nla_get_u32(cur);
  2423. if ((cur = tb[NL80211_ATTR_BSS_HT_OPMODE]))
  2424. chn->mode = nla_get_u32(cur);
  2425. return NL_SKIP;
  2426. }
  2427. static int nl80211_get_htmode(const char *ifname, int *buf)
  2428. {
  2429. struct chan_info chn = { .width = 0, .mode = 0 };
  2430. char *res;
  2431. int err;
  2432. res = nl80211_phy2ifname(ifname);
  2433. *buf = 0;
  2434. err = nl80211_request(res ? res : ifname,
  2435. NL80211_CMD_GET_INTERFACE, 0,
  2436. nl80211_get_htmode_cb, &chn);
  2437. if (err)
  2438. return -1;
  2439. switch (chn.width) {
  2440. case NL80211_CHAN_WIDTH_20:
  2441. if (chn.mode == -1)
  2442. *buf = IWINFO_HTMODE_VHT20;
  2443. else
  2444. *buf = IWINFO_HTMODE_HT20;
  2445. break;
  2446. case NL80211_CHAN_WIDTH_40:
  2447. if (chn.mode == -1)
  2448. *buf = IWINFO_HTMODE_VHT40;
  2449. else
  2450. *buf = IWINFO_HTMODE_HT40;
  2451. break;
  2452. case NL80211_CHAN_WIDTH_80:
  2453. *buf = IWINFO_HTMODE_VHT80;
  2454. break;
  2455. case NL80211_CHAN_WIDTH_80P80:
  2456. *buf = IWINFO_HTMODE_VHT80_80;
  2457. break;
  2458. case NL80211_CHAN_WIDTH_160:
  2459. *buf = IWINFO_HTMODE_VHT160;
  2460. break;
  2461. case NL80211_CHAN_WIDTH_5:
  2462. case NL80211_CHAN_WIDTH_10:
  2463. case NL80211_CHAN_WIDTH_20_NOHT:
  2464. *buf = IWINFO_HTMODE_NOHT;
  2465. break;
  2466. default:
  2467. return -1;
  2468. }
  2469. return 0;
  2470. }
  2471. static int nl80211_get_htmodelist(const char *ifname, int *buf)
  2472. {
  2473. struct nl80211_modes m = { 0 };
  2474. if (nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
  2475. nl80211_get_modelist_cb, &m))
  2476. goto out;
  2477. if (!m.ok)
  2478. goto out;
  2479. *buf = m.ht;
  2480. return 0;
  2481. out:
  2482. *buf = 0;
  2483. return -1;
  2484. }
  2485. static int nl80211_get_ifcomb_cb(struct nl_msg *msg, void *arg)
  2486. {
  2487. struct nlattr **attr = nl80211_parse(msg);
  2488. struct nlattr *comb;
  2489. int *ret = arg;
  2490. int comb_rem, limit_rem, mode_rem;
  2491. *ret = 0;
  2492. if (!attr[NL80211_ATTR_INTERFACE_COMBINATIONS])
  2493. return NL_SKIP;
  2494. nla_for_each_nested(comb, attr[NL80211_ATTR_INTERFACE_COMBINATIONS], comb_rem)
  2495. {
  2496. static struct nla_policy iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
  2497. [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
  2498. [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
  2499. };
  2500. struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB+1];
  2501. static struct nla_policy iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
  2502. [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
  2503. [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
  2504. };
  2505. struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT+1];
  2506. struct nlattr *limit;
  2507. nla_parse_nested(tb_comb, NUM_NL80211_IFACE_COMB, comb, iface_combination_policy);
  2508. if (!tb_comb[NL80211_IFACE_COMB_LIMITS])
  2509. continue;
  2510. nla_for_each_nested(limit, tb_comb[NL80211_IFACE_COMB_LIMITS], limit_rem)
  2511. {
  2512. struct nlattr *mode;
  2513. nla_parse_nested(tb_limit, NUM_NL80211_IFACE_LIMIT, limit, iface_limit_policy);
  2514. if (!tb_limit[NL80211_IFACE_LIMIT_TYPES] ||
  2515. !tb_limit[NL80211_IFACE_LIMIT_MAX])
  2516. continue;
  2517. if (nla_get_u32(tb_limit[NL80211_IFACE_LIMIT_MAX]) < 2)
  2518. continue;
  2519. nla_for_each_nested(mode, tb_limit[NL80211_IFACE_LIMIT_TYPES], mode_rem) {
  2520. if (nla_type(mode) == NL80211_IFTYPE_AP)
  2521. *ret = 1;
  2522. }
  2523. }
  2524. }
  2525. return NL_SKIP;
  2526. }
  2527. static int nl80211_get_mbssid_support(const char *ifname, int *buf)
  2528. {
  2529. if (nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
  2530. nl80211_get_ifcomb_cb, buf))
  2531. return -1;
  2532. return 0;
  2533. }
  2534. static int nl80211_hardware_id_from_fdt(struct iwinfo_hardware_id *id, const char *ifname)
  2535. {
  2536. char *phy, compat[64], path[PATH_MAX];
  2537. int i;
  2538. /* Try to determine the phy name from the given interface */
  2539. phy = nl80211_ifname2phy(ifname);
  2540. snprintf(path, sizeof(path), "/sys/class/%s/%s/device/of_node/compatible",
  2541. phy ? "ieee80211" : "net", phy ? phy : ifname);
  2542. if (nl80211_readstr(path, compat, sizeof(compat)) <= 0)
  2543. return -1;
  2544. if (!strcmp(compat, "qca,ar9130-wmac")) {
  2545. id->vendor_id = 0x168c;
  2546. id->device_id = 0x0029;
  2547. id->subsystem_vendor_id = 0x168c;
  2548. id->subsystem_device_id = 0x9130;
  2549. } else if (!strcmp(compat, "qca,ar9330-wmac")) {
  2550. id->vendor_id = 0x168c;
  2551. id->device_id = 0x0030;
  2552. id->subsystem_vendor_id = 0x168c;
  2553. id->subsystem_device_id = 0x9330;
  2554. } else if (!strcmp(compat, "qca,ar9340-wmac")) {
  2555. id->vendor_id = 0x168c;
  2556. id->device_id = 0x0030;
  2557. id->subsystem_vendor_id = 0x168c;
  2558. id->subsystem_device_id = 0x9340;
  2559. } else if (!strcmp(compat, "qca,qca9530-wmac")) {
  2560. id->vendor_id = 0x168c;
  2561. id->device_id = 0x0033;
  2562. id->subsystem_vendor_id = 0x168c;
  2563. id->subsystem_device_id = 0x9530;
  2564. } else if (!strcmp(compat, "qca,qca9550-wmac")) {
  2565. id->vendor_id = 0x168c;
  2566. id->device_id = 0x0033;
  2567. id->subsystem_vendor_id = 0x168c;
  2568. id->subsystem_device_id = 0x9550;
  2569. } else if (!strcmp(compat, "qca,qca9560-wmac")) {
  2570. id->vendor_id = 0x168c;
  2571. id->device_id = 0x0033;
  2572. id->subsystem_vendor_id = 0x168c;
  2573. id->subsystem_device_id = 0x9560;
  2574. } else if (!strcmp(compat, "qcom,ipq4019-wifi")) {
  2575. id->vendor_id = 0x168c;
  2576. id->device_id = 0x003c;
  2577. id->subsystem_vendor_id = 0x168c;
  2578. id->subsystem_device_id = 0x4019;
  2579. }
  2580. return (id->vendor_id && id->device_id) ? 0 : -1;
  2581. }
  2582. static int nl80211_get_hardware_id(const char *ifname, char *buf)
  2583. {
  2584. struct iwinfo_hardware_id *id = (struct iwinfo_hardware_id *)buf;
  2585. char *phy, num[8], path[PATH_MAX];
  2586. int i;
  2587. struct { const char *path; uint16_t *dest; } lookup[] = {
  2588. { "vendor", &id->vendor_id },
  2589. { "device", &id->device_id },
  2590. { "subsystem_vendor", &id->subsystem_vendor_id },
  2591. { "subsystem_device", &id->subsystem_device_id }
  2592. };
  2593. memset(id, 0, sizeof(*id));
  2594. /* Try to determine the phy name from the given interface */
  2595. phy = nl80211_ifname2phy(ifname);
  2596. for (i = 0; i < ARRAY_SIZE(lookup); i++)
  2597. {
  2598. snprintf(path, sizeof(path), "/sys/class/%s/%s/device/%s",
  2599. phy ? "ieee80211" : "net",
  2600. phy ? phy : ifname, lookup[i].path);
  2601. if (nl80211_readstr(path, num, sizeof(num)) > 0)
  2602. *lookup[i].dest = strtoul(num, NULL, 16);
  2603. }
  2604. /* Failed to obtain hardware IDs, try FDT */
  2605. if (id->vendor_id == 0 || id->device_id == 0)
  2606. if (!nl80211_hardware_id_from_fdt(id, ifname))
  2607. return 0;
  2608. /* Failed to obtain hardware IDs, search board config */
  2609. if (id->vendor_id == 0 || id->device_id == 0)
  2610. return iwinfo_hardware_id_from_mtd(id);
  2611. return 0;
  2612. }
  2613. static const struct iwinfo_hardware_entry *
  2614. nl80211_get_hardware_entry(const char *ifname)
  2615. {
  2616. struct iwinfo_hardware_id id;
  2617. if (nl80211_get_hardware_id(ifname, (char *)&id))
  2618. return NULL;
  2619. return iwinfo_hardware(&id);
  2620. }
  2621. static int nl80211_get_hardware_name(const char *ifname, char *buf)
  2622. {
  2623. const struct iwinfo_hardware_entry *hw;
  2624. if (!(hw = nl80211_get_hardware_entry(ifname)))
  2625. sprintf(buf, "Generic MAC80211");
  2626. else
  2627. sprintf(buf, "%s %s", hw->vendor_name, hw->device_name);
  2628. return 0;
  2629. }
  2630. static int nl80211_get_txpower_offset(const char *ifname, int *buf)
  2631. {
  2632. const struct iwinfo_hardware_entry *hw;
  2633. if (!(hw = nl80211_get_hardware_entry(ifname)))
  2634. return -1;
  2635. *buf = hw->txpower_offset;
  2636. return 0;
  2637. }
  2638. static int nl80211_get_frequency_offset(const char *ifname, int *buf)
  2639. {
  2640. const struct iwinfo_hardware_entry *hw;
  2641. if (!(hw = nl80211_get_hardware_entry(ifname)))
  2642. return -1;
  2643. *buf = hw->frequency_offset;
  2644. return 0;
  2645. }
  2646. static int nl80211_lookup_phyname(const char *section, char *buf)
  2647. {
  2648. int idx;
  2649. if ((idx = nl80211_phy_idx_from_uci(section)) < 0)
  2650. return -1;
  2651. sprintf(buf, "phy%d", idx);
  2652. return 0;
  2653. }
  2654. const struct iwinfo_ops nl80211_ops = {
  2655. .name = "nl80211",
  2656. .probe = nl80211_probe,
  2657. .channel = nl80211_get_channel,
  2658. .center_chan1 = nl80211_get_center_chan1,
  2659. .center_chan2 = nl80211_get_center_chan2,
  2660. .frequency = nl80211_get_frequency,
  2661. .frequency_offset = nl80211_get_frequency_offset,
  2662. .txpower = nl80211_get_txpower,
  2663. .txpower_offset = nl80211_get_txpower_offset,
  2664. .bitrate = nl80211_get_bitrate,
  2665. .signal = nl80211_get_signal,
  2666. .noise = nl80211_get_noise,
  2667. .quality = nl80211_get_quality,
  2668. .quality_max = nl80211_get_quality_max,
  2669. .mbssid_support = nl80211_get_mbssid_support,
  2670. .hwmodelist = nl80211_get_hwmodelist,
  2671. .htmodelist = nl80211_get_htmodelist,
  2672. .htmode = nl80211_get_htmode,
  2673. .mode = nl80211_get_mode,
  2674. .ssid = nl80211_get_ssid,
  2675. .bssid = nl80211_get_bssid,
  2676. .country = nl80211_get_country,
  2677. .hardware_id = nl80211_get_hardware_id,
  2678. .hardware_name = nl80211_get_hardware_name,
  2679. .encryption = nl80211_get_encryption,
  2680. .phyname = nl80211_get_phyname,
  2681. .assoclist = nl80211_get_assoclist,
  2682. .txpwrlist = nl80211_get_txpwrlist,
  2683. .scanlist = nl80211_get_scanlist,
  2684. .freqlist = nl80211_get_freqlist,
  2685. .countrylist = nl80211_get_countrylist,
  2686. .survey = nl80211_get_survey,
  2687. .lookup_phy = nl80211_lookup_phyname,
  2688. .close = nl80211_close
  2689. };