iwinfo_nl80211.c 81 KB

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