iwinfo_nl80211.c 77 KB

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