iwinfo_nl80211.c 84 KB

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