iwinfo_nl80211.c 81 KB

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