iwinfo_nl80211.c 84 KB

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