iwinfo_nl80211.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849
  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 "iwinfo_nl80211.h"
  29. #define min(x, y) ((x) < (y)) ? (x) : (y)
  30. #define BIT(x) (1ULL<<(x))
  31. static struct nl80211_state *nls = NULL;
  32. static void nl80211_close(void)
  33. {
  34. if (nls)
  35. {
  36. if (nls->nlctrl)
  37. genl_family_put(nls->nlctrl);
  38. if (nls->nl80211)
  39. genl_family_put(nls->nl80211);
  40. if (nls->nl_sock)
  41. nl_socket_free(nls->nl_sock);
  42. if (nls->nl_cache)
  43. nl_cache_free(nls->nl_cache);
  44. free(nls);
  45. nls = NULL;
  46. }
  47. }
  48. static int nl80211_init(void)
  49. {
  50. int err, fd;
  51. if (!nls)
  52. {
  53. nls = malloc(sizeof(struct nl80211_state));
  54. if (!nls) {
  55. err = -ENOMEM;
  56. goto err;
  57. }
  58. memset(nls, 0, sizeof(*nls));
  59. nls->nl_sock = nl_socket_alloc();
  60. if (!nls->nl_sock) {
  61. err = -ENOMEM;
  62. goto err;
  63. }
  64. if (genl_connect(nls->nl_sock)) {
  65. err = -ENOLINK;
  66. goto err;
  67. }
  68. fd = nl_socket_get_fd(nls->nl_sock);
  69. if (fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC) < 0) {
  70. err = -EINVAL;
  71. goto err;
  72. }
  73. if (genl_ctrl_alloc_cache(nls->nl_sock, &nls->nl_cache)) {
  74. err = -ENOMEM;
  75. goto err;
  76. }
  77. nls->nl80211 = genl_ctrl_search_by_name(nls->nl_cache, "nl80211");
  78. if (!nls->nl80211) {
  79. err = -ENOENT;
  80. goto err;
  81. }
  82. nls->nlctrl = genl_ctrl_search_by_name(nls->nl_cache, "nlctrl");
  83. if (!nls->nlctrl) {
  84. err = -ENOENT;
  85. goto err;
  86. }
  87. }
  88. return 0;
  89. err:
  90. nl80211_close();
  91. return err;
  92. }
  93. static int nl80211_readint(const char *path)
  94. {
  95. int fd;
  96. int rv = -1;
  97. char buffer[16];
  98. if ((fd = open(path, O_RDONLY)) > -1)
  99. {
  100. if (read(fd, buffer, sizeof(buffer)) > 0)
  101. rv = atoi(buffer);
  102. close(fd);
  103. }
  104. return rv;
  105. }
  106. static int nl80211_readstr(const char *path, char *buffer, int length)
  107. {
  108. int fd;
  109. int rv = -1;
  110. if ((fd = open(path, O_RDONLY)) > -1)
  111. {
  112. if ((rv = read(fd, buffer, length - 1)) > 0)
  113. {
  114. if (buffer[rv - 1] == '\n')
  115. rv--;
  116. buffer[rv] = 0;
  117. }
  118. close(fd);
  119. }
  120. return rv;
  121. }
  122. static int nl80211_msg_error(struct sockaddr_nl *nla,
  123. struct nlmsgerr *err, void *arg)
  124. {
  125. int *ret = arg;
  126. *ret = err->error;
  127. return NL_STOP;
  128. }
  129. static int nl80211_msg_finish(struct nl_msg *msg, void *arg)
  130. {
  131. int *ret = arg;
  132. *ret = 0;
  133. return NL_SKIP;
  134. }
  135. static int nl80211_msg_ack(struct nl_msg *msg, void *arg)
  136. {
  137. int *ret = arg;
  138. *ret = 0;
  139. return NL_STOP;
  140. }
  141. static int nl80211_msg_response(struct nl_msg *msg, void *arg)
  142. {
  143. return NL_SKIP;
  144. }
  145. static void nl80211_free(struct nl80211_msg_conveyor *cv)
  146. {
  147. if (cv)
  148. {
  149. if (cv->cb)
  150. nl_cb_put(cv->cb);
  151. if (cv->msg)
  152. nlmsg_free(cv->msg);
  153. cv->cb = NULL;
  154. cv->msg = NULL;
  155. }
  156. }
  157. static struct nl80211_msg_conveyor * nl80211_new(struct genl_family *family,
  158. int cmd, int flags)
  159. {
  160. static struct nl80211_msg_conveyor cv;
  161. struct nl_msg *req = NULL;
  162. struct nl_cb *cb = NULL;
  163. req = nlmsg_alloc();
  164. if (!req)
  165. goto err;
  166. cb = nl_cb_alloc(NL_CB_DEFAULT);
  167. if (!cb)
  168. goto err;
  169. genlmsg_put(req, 0, 0, genl_family_get_id(family), 0, flags, cmd, 0);
  170. cv.msg = req;
  171. cv.cb = cb;
  172. return &cv;
  173. err:
  174. if (req)
  175. nlmsg_free(req);
  176. return NULL;
  177. }
  178. static struct nl80211_msg_conveyor * nl80211_ctl(int cmd, int flags)
  179. {
  180. if (nl80211_init() < 0)
  181. return NULL;
  182. return nl80211_new(nls->nlctrl, cmd, flags);
  183. }
  184. static int nl80211_phy_idx_from_uci_path(struct uci_section *s)
  185. {
  186. const char *opt;
  187. char buf[128];
  188. int idx = -1;
  189. glob_t gl;
  190. opt = uci_lookup_option_string(uci_ctx, s, "path");
  191. if (!opt)
  192. return -1;
  193. snprintf(buf, sizeof(buf), "/sys/devices/%s/ieee80211/*/index", opt); /**/
  194. if (glob(buf, 0, NULL, &gl))
  195. snprintf(buf, sizeof(buf), "/sys/devices/platform/%s/ieee80211/*/index", opt); /**/
  196. if (glob(buf, 0, NULL, &gl))
  197. return -1;
  198. if (gl.gl_pathc > 0)
  199. idx = nl80211_readint(gl.gl_pathv[0]);
  200. globfree(&gl);
  201. return idx;
  202. }
  203. static int nl80211_phy_idx_from_uci_macaddr(struct uci_section *s)
  204. {
  205. const char *opt;
  206. char buf[128];
  207. int i, idx = -1;
  208. glob_t gl;
  209. opt = uci_lookup_option_string(uci_ctx, s, "macaddr");
  210. if (!opt)
  211. return -1;
  212. snprintf(buf, sizeof(buf), "/sys/class/ieee80211/*"); /**/
  213. if (glob(buf, 0, NULL, &gl))
  214. return -1;
  215. for (i = 0; i < gl.gl_pathc; i++)
  216. {
  217. snprintf(buf, sizeof(buf), "%s/macaddress", gl.gl_pathv[i]);
  218. if (nl80211_readstr(buf, buf, sizeof(buf)) <= 0)
  219. continue;
  220. if (fnmatch(opt, buf, FNM_CASEFOLD))
  221. continue;
  222. snprintf(buf, sizeof(buf), "%s/index", gl.gl_pathv[i]);
  223. if ((idx = nl80211_readint(buf)) > -1)
  224. break;
  225. }
  226. globfree(&gl);
  227. return idx;
  228. }
  229. static int nl80211_phy_idx_from_uci_phy(struct uci_section *s)
  230. {
  231. const char *opt;
  232. char buf[128];
  233. opt = uci_lookup_option_string(uci_ctx, s, "phy");
  234. if (!opt)
  235. return -1;
  236. snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", opt);
  237. return nl80211_readint(buf);
  238. }
  239. static int nl80211_phy_idx_from_uci(const char *name)
  240. {
  241. struct uci_section *s;
  242. int idx = -1;
  243. s = iwinfo_uci_get_radio(name, "mac80211");
  244. if (!s)
  245. goto free;
  246. idx = nl80211_phy_idx_from_uci_path(s);
  247. if (idx < 0)
  248. idx = nl80211_phy_idx_from_uci_macaddr(s);
  249. if (idx < 0)
  250. idx = nl80211_phy_idx_from_uci_phy(s);
  251. free:
  252. iwinfo_uci_free();
  253. return idx;
  254. }
  255. static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname,
  256. int cmd, int flags)
  257. {
  258. int ifidx = -1, phyidx = -1;
  259. struct nl80211_msg_conveyor *cv;
  260. if (ifname == NULL)
  261. return NULL;
  262. if (nl80211_init() < 0)
  263. return NULL;
  264. if (!strncmp(ifname, "phy", 3))
  265. phyidx = atoi(&ifname[3]);
  266. else if (!strncmp(ifname, "radio", 5))
  267. phyidx = nl80211_phy_idx_from_uci(ifname);
  268. else if (!strncmp(ifname, "mon.", 4))
  269. ifidx = if_nametoindex(&ifname[4]);
  270. else
  271. ifidx = if_nametoindex(ifname);
  272. /* Valid ifidx must be greater than 0 */
  273. if ((ifidx <= 0) && (phyidx < 0))
  274. return NULL;
  275. cv = nl80211_new(nls->nl80211, cmd, flags);
  276. if (!cv)
  277. return NULL;
  278. if (ifidx > -1)
  279. NLA_PUT_U32(cv->msg, NL80211_ATTR_IFINDEX, ifidx);
  280. if (phyidx > -1)
  281. NLA_PUT_U32(cv->msg, NL80211_ATTR_WIPHY, phyidx);
  282. return cv;
  283. nla_put_failure:
  284. nl80211_free(cv);
  285. return NULL;
  286. }
  287. static int nl80211_send(struct nl80211_msg_conveyor *cv,
  288. int (*cb_func)(struct nl_msg *, void *),
  289. void *cb_arg)
  290. {
  291. static struct nl80211_msg_conveyor rcv;
  292. int err;
  293. if (cb_func)
  294. nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, cb_func, cb_arg);
  295. else
  296. nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, nl80211_msg_response, &rcv);
  297. err = nl_send_auto_complete(nls->nl_sock, cv->msg);
  298. if (err < 0)
  299. goto out;
  300. err = 1;
  301. nl_cb_err(cv->cb, NL_CB_CUSTOM, nl80211_msg_error, &err);
  302. nl_cb_set(cv->cb, NL_CB_FINISH, NL_CB_CUSTOM, nl80211_msg_finish, &err);
  303. nl_cb_set(cv->cb, NL_CB_ACK, NL_CB_CUSTOM, nl80211_msg_ack, &err);
  304. while (err > 0)
  305. nl_recvmsgs(nls->nl_sock, cv->cb);
  306. out:
  307. nl80211_free(cv);
  308. return err;
  309. }
  310. static int nl80211_request(const char *ifname, int cmd, int flags,
  311. int (*cb_func)(struct nl_msg *, void *),
  312. void *cb_arg)
  313. {
  314. struct nl80211_msg_conveyor *cv;
  315. cv = nl80211_msg(ifname, cmd, flags);
  316. if (!cv)
  317. return -ENOMEM;
  318. return nl80211_send(cv, cb_func, cb_arg);
  319. }
  320. static struct nlattr ** nl80211_parse(struct nl_msg *msg)
  321. {
  322. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  323. static struct nlattr *attr[NL80211_ATTR_MAX + 1];
  324. nla_parse(attr, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  325. genlmsg_attrlen(gnlh, 0), NULL);
  326. return attr;
  327. }
  328. static int nl80211_get_protocol_features_cb(struct nl_msg *msg, void *arg)
  329. {
  330. uint32_t *features = arg;
  331. struct nlattr **attr = nl80211_parse(msg);
  332. if (attr[NL80211_ATTR_PROTOCOL_FEATURES])
  333. *features = nla_get_u32(attr[NL80211_ATTR_PROTOCOL_FEATURES]);
  334. return NL_SKIP;
  335. }
  336. static int nl80211_get_protocol_features(const char *ifname)
  337. {
  338. struct nl80211_msg_conveyor *req;
  339. uint32_t features = 0;
  340. req = nl80211_msg(ifname, NL80211_CMD_GET_PROTOCOL_FEATURES, 0);
  341. if (req) {
  342. nl80211_send(req, nl80211_get_protocol_features_cb, &features);
  343. nl80211_free(req);
  344. }
  345. return features;
  346. }
  347. static int nl80211_subscribe_cb(struct nl_msg *msg, void *arg)
  348. {
  349. struct nl80211_group_conveyor *cv = arg;
  350. struct nlattr **attr = nl80211_parse(msg);
  351. struct nlattr *mgrpinfo[CTRL_ATTR_MCAST_GRP_MAX + 1];
  352. struct nlattr *mgrp;
  353. int mgrpidx;
  354. if (!attr[CTRL_ATTR_MCAST_GROUPS])
  355. return NL_SKIP;
  356. nla_for_each_nested(mgrp, attr[CTRL_ATTR_MCAST_GROUPS], mgrpidx)
  357. {
  358. nla_parse(mgrpinfo, CTRL_ATTR_MCAST_GRP_MAX,
  359. nla_data(mgrp), nla_len(mgrp), NULL);
  360. if (mgrpinfo[CTRL_ATTR_MCAST_GRP_ID] &&
  361. mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME] &&
  362. !strncmp(nla_data(mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME]),
  363. cv->name, nla_len(mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME])))
  364. {
  365. cv->id = nla_get_u32(mgrpinfo[CTRL_ATTR_MCAST_GRP_ID]);
  366. break;
  367. }
  368. }
  369. return NL_SKIP;
  370. }
  371. static int nl80211_subscribe(const char *family, const char *group)
  372. {
  373. struct nl80211_group_conveyor cv = { .name = group, .id = -ENOENT };
  374. struct nl80211_msg_conveyor *req;
  375. int err;
  376. req = nl80211_ctl(CTRL_CMD_GETFAMILY, 0);
  377. if (req)
  378. {
  379. NLA_PUT_STRING(req->msg, CTRL_ATTR_FAMILY_NAME, family);
  380. err = nl80211_send(req, nl80211_subscribe_cb, &cv);
  381. if (err)
  382. return err;
  383. return nl_socket_add_membership(nls->nl_sock, cv.id);
  384. nla_put_failure:
  385. nl80211_free(req);
  386. }
  387. return -ENOMEM;
  388. }
  389. static int nl80211_wait_cb(struct nl_msg *msg, void *arg)
  390. {
  391. struct nl80211_event_conveyor *cv = arg;
  392. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  393. if (cv->wait[gnlh->cmd / 32] & (1 << (gnlh->cmd % 32)))
  394. cv->recv = gnlh->cmd;
  395. return NL_SKIP;
  396. }
  397. static int nl80211_wait_seq_check(struct nl_msg *msg, void *arg)
  398. {
  399. return NL_OK;
  400. }
  401. static int __nl80211_wait(const char *family, const char *group, ...)
  402. {
  403. struct nl80211_event_conveyor cv = { };
  404. struct nl_cb *cb;
  405. int err = 0;
  406. int cmd;
  407. va_list ap;
  408. if (nl80211_subscribe(family, group))
  409. return -ENOENT;
  410. cb = nl_cb_alloc(NL_CB_DEFAULT);
  411. if (!cb)
  412. return -ENOMEM;
  413. nl_cb_err(cb, NL_CB_CUSTOM, nl80211_msg_error, &err);
  414. nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, nl80211_wait_seq_check, NULL);
  415. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, nl80211_wait_cb, &cv );
  416. va_start(ap, group);
  417. for (cmd = va_arg(ap, int); cmd != 0; cmd = va_arg(ap, int))
  418. cv.wait[cmd / 32] |= (1 << (cmd % 32));
  419. va_end(ap);
  420. while (!cv.recv && !err)
  421. nl_recvmsgs(nls->nl_sock, cb);
  422. nl_cb_put(cb);
  423. return err;
  424. }
  425. #define nl80211_wait(family, group, ...) \
  426. __nl80211_wait(family, group, __VA_ARGS__, 0)
  427. static int nl80211_freq2channel(int freq)
  428. {
  429. if (freq == 2484)
  430. return 14;
  431. else if (freq < 2484)
  432. return (freq - 2407) / 5;
  433. else if (freq >= 4910 && freq <= 4980)
  434. return (freq - 4000) / 5;
  435. else
  436. return (freq - 5000) / 5;
  437. }
  438. static int nl80211_channel2freq(int channel, const char *band)
  439. {
  440. if (!band || band[0] != 'a')
  441. {
  442. if (channel == 14)
  443. return 2484;
  444. else if (channel < 14)
  445. return (channel * 5) + 2407;
  446. }
  447. else
  448. {
  449. if (channel >= 182 && channel <= 196)
  450. return (channel * 5) + 4000;
  451. else
  452. return (channel * 5) + 5000;
  453. }
  454. return 0;
  455. }
  456. static int nl80211_ifname2phy_cb(struct nl_msg *msg, void *arg)
  457. {
  458. char *buf = arg;
  459. struct nlattr **attr = nl80211_parse(msg);
  460. if (attr[NL80211_ATTR_WIPHY_NAME])
  461. memcpy(buf, nla_data(attr[NL80211_ATTR_WIPHY_NAME]),
  462. nla_len(attr[NL80211_ATTR_WIPHY_NAME]));
  463. else
  464. buf[0] = 0;
  465. return NL_SKIP;
  466. }
  467. static char * nl80211_ifname2phy(const char *ifname)
  468. {
  469. static char phy[32] = { 0 };
  470. memset(phy, 0, sizeof(phy));
  471. nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
  472. nl80211_ifname2phy_cb, phy);
  473. return phy[0] ? phy : NULL;
  474. }
  475. static char * nl80211_phy2ifname(const char *ifname)
  476. {
  477. int ifidx = -1, cifidx = -1, phyidx = -1;
  478. char buffer[64];
  479. static char nif[IFNAMSIZ] = { 0 };
  480. DIR *d;
  481. struct dirent *e;
  482. /* Only accept phy name of the form phy%d or radio%d */
  483. if (!ifname)
  484. return NULL;
  485. else if (!strncmp(ifname, "phy", 3))
  486. phyidx = atoi(&ifname[3]);
  487. else if (!strncmp(ifname, "radio", 5))
  488. phyidx = nl80211_phy_idx_from_uci(ifname);
  489. else
  490. return NULL;
  491. memset(nif, 0, sizeof(nif));
  492. if (phyidx > -1)
  493. {
  494. if ((d = opendir("/sys/class/net")) != NULL)
  495. {
  496. while ((e = readdir(d)) != NULL)
  497. {
  498. snprintf(buffer, sizeof(buffer),
  499. "/sys/class/net/%s/phy80211/index", e->d_name);
  500. if (nl80211_readint(buffer) == phyidx)
  501. {
  502. snprintf(buffer, sizeof(buffer),
  503. "/sys/class/net/%s/ifindex", e->d_name);
  504. if ((cifidx = nl80211_readint(buffer)) >= 0 &&
  505. ((ifidx < 0) || (cifidx < ifidx)))
  506. {
  507. ifidx = cifidx;
  508. strncpy(nif, e->d_name, sizeof(nif) - 1);
  509. }
  510. }
  511. }
  512. closedir(d);
  513. }
  514. }
  515. return nif[0] ? nif : NULL;
  516. }
  517. static int nl80211_get_mode_cb(struct nl_msg *msg, void *arg)
  518. {
  519. int *mode = arg;
  520. struct nlattr **tb = nl80211_parse(msg);
  521. const int ifmodes[NL80211_IFTYPE_MAX + 1] = {
  522. IWINFO_OPMODE_UNKNOWN, /* unspecified */
  523. IWINFO_OPMODE_ADHOC, /* IBSS */
  524. IWINFO_OPMODE_CLIENT, /* managed */
  525. IWINFO_OPMODE_MASTER, /* AP */
  526. IWINFO_OPMODE_AP_VLAN, /* AP/VLAN */
  527. IWINFO_OPMODE_WDS, /* WDS */
  528. IWINFO_OPMODE_MONITOR, /* monitor */
  529. IWINFO_OPMODE_MESHPOINT, /* mesh point */
  530. IWINFO_OPMODE_P2P_CLIENT, /* P2P-client */
  531. IWINFO_OPMODE_P2P_GO, /* P2P-GO */
  532. };
  533. if (tb[NL80211_ATTR_IFTYPE])
  534. *mode = ifmodes[nla_get_u32(tb[NL80211_ATTR_IFTYPE])];
  535. return NL_SKIP;
  536. }
  537. static int nl80211_get_mode(const char *ifname, int *buf)
  538. {
  539. char *res;
  540. *buf = IWINFO_OPMODE_UNKNOWN;
  541. res = nl80211_phy2ifname(ifname);
  542. nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0,
  543. nl80211_get_mode_cb, buf);
  544. return (*buf == IWINFO_OPMODE_UNKNOWN) ? -1 : 0;
  545. }
  546. static int __nl80211_hostapd_query(const char *ifname, ...)
  547. {
  548. va_list ap, ap_cur;
  549. char *phy, *search, *dest, *key, *val, buf[128];
  550. int len, mode, found = 0, match = 1;
  551. FILE *fp;
  552. if (nl80211_get_mode(ifname, &mode))
  553. return 0;
  554. if (mode != IWINFO_OPMODE_MASTER && mode != IWINFO_OPMODE_AP_VLAN)
  555. return 0;
  556. phy = nl80211_ifname2phy(ifname);
  557. if (!phy)
  558. return 0;
  559. snprintf(buf, sizeof(buf), "/var/run/hostapd-%s.conf", phy);
  560. fp = fopen(buf, "r");
  561. if (!fp)
  562. return 0;
  563. va_start(ap, ifname);
  564. /* clear all destination buffers */
  565. va_copy(ap_cur, ap);
  566. while ((search = va_arg(ap_cur, char *)) != NULL)
  567. {
  568. dest = va_arg(ap_cur, char *);
  569. len = va_arg(ap_cur, int);
  570. memset(dest, 0, len);
  571. }
  572. va_end(ap_cur);
  573. /* iterate applicable lines and copy found values into dest buffers */
  574. while (fgets(buf, sizeof(buf), fp))
  575. {
  576. key = strtok(buf, " =\t\n");
  577. val = strtok(NULL, "\n");
  578. if (!key || !val || !*key || *key == '#')
  579. continue;
  580. if (!strcmp(key, "interface") || !strcmp(key, "bss"))
  581. match = !strcmp(ifname, val);
  582. if (!match)
  583. continue;
  584. va_copy(ap_cur, ap);
  585. while ((search = va_arg(ap_cur, char *)) != NULL)
  586. {
  587. dest = va_arg(ap_cur, char *);
  588. len = va_arg(ap_cur, int);
  589. if (!strcmp(search, key))
  590. {
  591. strncpy(dest, val, len - 1);
  592. found++;
  593. break;
  594. }
  595. }
  596. va_end(ap_cur);
  597. }
  598. fclose(fp);
  599. va_end(ap);
  600. return found;
  601. }
  602. #define nl80211_hostapd_query(ifname, ...) \
  603. __nl80211_hostapd_query(ifname, ##__VA_ARGS__, NULL)
  604. static inline int nl80211_wpactl_recv(int sock, char *buf, int blen)
  605. {
  606. fd_set rfds;
  607. struct timeval tv = { 0, 256000 };
  608. FD_ZERO(&rfds);
  609. FD_SET(sock, &rfds);
  610. memset(buf, 0, blen);
  611. if (select(sock + 1, &rfds, NULL, NULL, &tv) < 0)
  612. return -1;
  613. if (!FD_ISSET(sock, &rfds))
  614. return -1;
  615. return recv(sock, buf, blen - 1, 0);
  616. }
  617. static int nl80211_wpactl_connect(const char *ifname, struct sockaddr_un *local)
  618. {
  619. struct sockaddr_un remote = { 0 };
  620. size_t remote_length, local_length;
  621. int sock = socket(PF_UNIX, SOCK_DGRAM, 0);
  622. if (sock < 0)
  623. return sock;
  624. remote.sun_family = AF_UNIX;
  625. remote_length = sizeof(remote.sun_family) +
  626. sprintf(remote.sun_path, "/var/run/wpa_supplicant-%s/%s",
  627. ifname, ifname);
  628. if (fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC) < 0)
  629. {
  630. close(sock);
  631. return -1;
  632. }
  633. if (connect(sock, (struct sockaddr *)&remote, remote_length))
  634. {
  635. remote_length = sizeof(remote.sun_family) +
  636. sprintf(remote.sun_path, "/var/run/wpa_supplicant/%s", ifname);
  637. if (connect(sock, (struct sockaddr *)&remote, remote_length))
  638. {
  639. close(sock);
  640. return -1;
  641. }
  642. }
  643. local->sun_family = AF_UNIX;
  644. local_length = sizeof(local->sun_family) +
  645. sprintf(local->sun_path, "/var/run/iwinfo-%s-%d", ifname, getpid());
  646. if (bind(sock, (struct sockaddr *)local, local_length) < 0)
  647. {
  648. close(sock);
  649. return -1;
  650. }
  651. return sock;
  652. }
  653. static int __nl80211_wpactl_query(const char *ifname, ...)
  654. {
  655. va_list ap, ap_cur;
  656. struct sockaddr_un local = { 0 };
  657. int len, mode, found = 0, sock = -1;
  658. char *search, *dest, *key, *val, *line, *pos, buf[512];
  659. if (nl80211_get_mode(ifname, &mode))
  660. return 0;
  661. if (mode != IWINFO_OPMODE_CLIENT && mode != IWINFO_OPMODE_ADHOC)
  662. return 0;
  663. sock = nl80211_wpactl_connect(ifname, &local);
  664. if (sock < 0)
  665. return 0;
  666. va_start(ap, ifname);
  667. /* clear all destination buffers */
  668. va_copy(ap_cur, ap);
  669. while ((search = va_arg(ap_cur, char *)) != NULL)
  670. {
  671. dest = va_arg(ap_cur, char *);
  672. len = va_arg(ap_cur, int);
  673. memset(dest, 0, len);
  674. }
  675. va_end(ap_cur);
  676. send(sock, "STATUS", 6, 0);
  677. while (true)
  678. {
  679. if (nl80211_wpactl_recv(sock, buf, sizeof(buf)) <= 0)
  680. break;
  681. if (buf[0] == '<')
  682. continue;
  683. for (line = strtok_r(buf, "\n", &pos);
  684. line != NULL;
  685. line = strtok_r(NULL, "\n", &pos))
  686. {
  687. key = strtok(line, "=");
  688. val = strtok(NULL, "\n");
  689. if (!key || !val)
  690. continue;
  691. va_copy(ap_cur, ap);
  692. while ((search = va_arg(ap_cur, char *)) != NULL)
  693. {
  694. dest = va_arg(ap_cur, char *);
  695. len = va_arg(ap_cur, int);
  696. if (!strcmp(search, key))
  697. {
  698. strncpy(dest, val, len - 1);
  699. found++;
  700. break;
  701. }
  702. }
  703. va_end(ap_cur);
  704. }
  705. break;
  706. }
  707. va_end(ap);
  708. close(sock);
  709. unlink(local.sun_path);
  710. return found;
  711. }
  712. #define nl80211_wpactl_query(ifname, ...) \
  713. __nl80211_wpactl_query(ifname, ##__VA_ARGS__, NULL)
  714. static char * nl80211_ifadd(const char *ifname)
  715. {
  716. char path[PATH_MAX];
  717. static char nif[IFNAMSIZ] = { 0 };
  718. struct nl80211_msg_conveyor *req;
  719. FILE *sysfs;
  720. req = nl80211_msg(ifname, NL80211_CMD_NEW_INTERFACE, 0);
  721. if (req)
  722. {
  723. snprintf(nif, sizeof(nif), "tmp.%s", ifname);
  724. NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, nif);
  725. NLA_PUT_U32(req->msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_STATION);
  726. nl80211_send(req, NULL, NULL);
  727. snprintf(path, sizeof(path) - 1,
  728. "/proc/sys/net/ipv6/conf/%s/disable_ipv6", nif);
  729. if ((sysfs = fopen(path, "w")) != NULL)
  730. {
  731. fwrite("0\n", 1, 2, sysfs);
  732. fclose(sysfs);
  733. }
  734. return nif;
  735. nla_put_failure:
  736. nl80211_free(req);
  737. }
  738. return NULL;
  739. }
  740. static void nl80211_ifdel(const char *ifname)
  741. {
  742. struct nl80211_msg_conveyor *req;
  743. int err;
  744. req = nl80211_msg(ifname, NL80211_CMD_DEL_INTERFACE, 0);
  745. if (req)
  746. {
  747. NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, ifname);
  748. nl80211_send(req, NULL, NULL);
  749. return;
  750. nla_put_failure:
  751. nl80211_free(req);
  752. }
  753. }
  754. static void nl80211_hostapd_hup(const char *ifname)
  755. {
  756. int fd, pid = 0;
  757. char buf[32];
  758. char *phy = nl80211_ifname2phy(ifname);
  759. if (phy)
  760. {
  761. snprintf(buf, sizeof(buf), "/var/run/wifi-%s.pid", phy);
  762. if ((fd = open(buf, O_RDONLY)) >= 0)
  763. {
  764. if (read(fd, buf, sizeof(buf)) > 0)
  765. pid = atoi(buf);
  766. close(fd);
  767. }
  768. if (pid > 0)
  769. kill(pid, 1);
  770. }
  771. }
  772. static int nl80211_probe(const char *ifname)
  773. {
  774. return !!nl80211_ifname2phy(ifname);
  775. }
  776. struct nl80211_ssid_bssid {
  777. unsigned char *ssid;
  778. unsigned char bssid[7];
  779. };
  780. static int nl80211_get_ssid_bssid_cb(struct nl_msg *msg, void *arg)
  781. {
  782. int ielen;
  783. unsigned char *ie;
  784. struct nl80211_ssid_bssid *sb = arg;
  785. struct nlattr **tb = nl80211_parse(msg);
  786. struct nlattr *bss[NL80211_BSS_MAX + 1];
  787. static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
  788. [NL80211_BSS_INFORMATION_ELEMENTS] = { 0 },
  789. [NL80211_BSS_STATUS] = { .type = NLA_U32 },
  790. };
  791. if (!tb[NL80211_ATTR_BSS] ||
  792. nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
  793. bss_policy) ||
  794. !bss[NL80211_BSS_BSSID] ||
  795. !bss[NL80211_BSS_STATUS] ||
  796. !bss[NL80211_BSS_INFORMATION_ELEMENTS])
  797. {
  798. return NL_SKIP;
  799. }
  800. switch (nla_get_u32(bss[NL80211_BSS_STATUS]))
  801. {
  802. case NL80211_BSS_STATUS_ASSOCIATED:
  803. case NL80211_BSS_STATUS_AUTHENTICATED:
  804. case NL80211_BSS_STATUS_IBSS_JOINED:
  805. if (sb->ssid)
  806. {
  807. ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
  808. ielen = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
  809. while (ielen >= 2 && ielen >= ie[1])
  810. {
  811. if (ie[0] == 0)
  812. {
  813. memcpy(sb->ssid, ie + 2, min(ie[1], IWINFO_ESSID_MAX_SIZE));
  814. return NL_SKIP;
  815. }
  816. ielen -= ie[1] + 2;
  817. ie += ie[1] + 2;
  818. }
  819. }
  820. else
  821. {
  822. sb->bssid[0] = 1;
  823. memcpy(sb->bssid + 1, nla_data(bss[NL80211_BSS_BSSID]), 6);
  824. return NL_SKIP;
  825. }
  826. default:
  827. return NL_SKIP;
  828. }
  829. }
  830. static int nl80211_get_ssid(const char *ifname, char *buf)
  831. {
  832. char *res;
  833. struct nl80211_ssid_bssid sb = { .ssid = (unsigned char *)buf };
  834. /* try to find ssid from scan dump results */
  835. res = nl80211_phy2ifname(ifname);
  836. sb.ssid[0] = 0;
  837. nl80211_request(res ? res : ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP,
  838. nl80211_get_ssid_bssid_cb, &sb);
  839. /* failed, try to find from hostapd info */
  840. if (sb.ssid[0] == 0)
  841. nl80211_hostapd_query(ifname, "ssid", sb.ssid,
  842. IWINFO_ESSID_MAX_SIZE + 1);
  843. return (sb.ssid[0] == 0) ? -1 : 0;
  844. }
  845. static int nl80211_get_bssid(const char *ifname, char *buf)
  846. {
  847. char *res, bssid[sizeof("FF:FF:FF:FF:FF:FF\0")];
  848. struct nl80211_ssid_bssid sb = { };
  849. /* try to find bssid from scan dump results */
  850. res = nl80211_phy2ifname(ifname);
  851. nl80211_request(res ? res : ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP,
  852. nl80211_get_ssid_bssid_cb, &sb);
  853. /* failed, try to find mac from hostapd info */
  854. if ((sb.bssid[0] == 0) &&
  855. nl80211_hostapd_query(ifname, "bssid", bssid, sizeof(bssid)))
  856. {
  857. sb.bssid[0] = 1;
  858. sb.bssid[1] = strtol(&bssid[0], NULL, 16);
  859. sb.bssid[2] = strtol(&bssid[3], NULL, 16);
  860. sb.bssid[3] = strtol(&bssid[6], NULL, 16);
  861. sb.bssid[4] = strtol(&bssid[9], NULL, 16);
  862. sb.bssid[5] = strtol(&bssid[12], NULL, 16);
  863. sb.bssid[6] = strtol(&bssid[15], NULL, 16);
  864. }
  865. if (sb.bssid[0])
  866. {
  867. sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
  868. sb.bssid[1], sb.bssid[2], sb.bssid[3],
  869. sb.bssid[4], sb.bssid[5], sb.bssid[6]);
  870. return 0;
  871. }
  872. return -1;
  873. }
  874. static int nl80211_get_frequency_scan_cb(struct nl_msg *msg, void *arg)
  875. {
  876. int *freq = arg;
  877. struct nlattr **attr = nl80211_parse(msg);
  878. struct nlattr *binfo[NL80211_BSS_MAX + 1];
  879. static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
  880. [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
  881. [NL80211_BSS_STATUS] = { .type = NLA_U32 },
  882. };
  883. if (attr[NL80211_ATTR_BSS] &&
  884. !nla_parse_nested(binfo, NL80211_BSS_MAX,
  885. attr[NL80211_ATTR_BSS], bss_policy))
  886. {
  887. if (binfo[NL80211_BSS_STATUS] && binfo[NL80211_BSS_FREQUENCY])
  888. *freq = nla_get_u32(binfo[NL80211_BSS_FREQUENCY]);
  889. }
  890. return NL_SKIP;
  891. }
  892. static int nl80211_get_frequency_info_cb(struct nl_msg *msg, void *arg)
  893. {
  894. int *freq = arg;
  895. struct nlattr **tb = nl80211_parse(msg);
  896. if (tb[NL80211_ATTR_WIPHY_FREQ])
  897. *freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
  898. return NL_SKIP;
  899. }
  900. static int nl80211_get_frequency(const char *ifname, int *buf)
  901. {
  902. char *res, channel[4], hwmode[2];
  903. /* try to find frequency from interface info */
  904. res = nl80211_phy2ifname(ifname);
  905. *buf = 0;
  906. nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0,
  907. nl80211_get_frequency_info_cb, buf);
  908. /* failed, try to find frequency from hostapd info */
  909. if ((*buf == 0) &&
  910. nl80211_hostapd_query(ifname, "hw_mode", hwmode, sizeof(hwmode),
  911. "channel", channel, sizeof(channel)) == 2)
  912. {
  913. *buf = nl80211_channel2freq(atoi(channel), hwmode);
  914. }
  915. /* failed, try to find frequency from scan results */
  916. if (*buf == 0)
  917. {
  918. res = nl80211_phy2ifname(ifname);
  919. nl80211_request(res ? res : ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP,
  920. nl80211_get_frequency_scan_cb, buf);
  921. }
  922. return (*buf == 0) ? -1 : 0;
  923. }
  924. static int nl80211_get_channel(const char *ifname, int *buf)
  925. {
  926. if (!nl80211_get_frequency(ifname, buf))
  927. {
  928. *buf = nl80211_freq2channel(*buf);
  929. return 0;
  930. }
  931. return -1;
  932. }
  933. static int nl80211_get_txpower_cb(struct nl_msg *msg, void *arg)
  934. {
  935. int *buf = arg;
  936. struct nlattr **tb = nl80211_parse(msg);
  937. if (tb[NL80211_ATTR_WIPHY_TX_POWER_LEVEL])
  938. *buf = iwinfo_mbm2dbm(nla_get_u32(tb[NL80211_ATTR_WIPHY_TX_POWER_LEVEL]));
  939. return NL_SKIP;
  940. }
  941. static int nl80211_get_txpower(const char *ifname, int *buf)
  942. {
  943. char *res;
  944. res = nl80211_phy2ifname(ifname);
  945. *buf = 0;
  946. if (nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0,
  947. nl80211_get_txpower_cb, buf))
  948. return -1;
  949. return 0;
  950. }
  951. static int nl80211_fill_signal_cb(struct nl_msg *msg, void *arg)
  952. {
  953. int8_t dbm;
  954. int16_t mbit;
  955. struct nl80211_rssi_rate *rr = arg;
  956. struct nlattr **attr = nl80211_parse(msg);
  957. struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
  958. struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
  959. static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
  960. [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
  961. [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
  962. [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
  963. [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
  964. [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
  965. [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
  966. [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
  967. [NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
  968. [NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
  969. [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
  970. };
  971. static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
  972. [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
  973. [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
  974. [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
  975. [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
  976. };
  977. if (attr[NL80211_ATTR_STA_INFO])
  978. {
  979. if (!nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
  980. attr[NL80211_ATTR_STA_INFO], stats_policy))
  981. {
  982. if (sinfo[NL80211_STA_INFO_SIGNAL])
  983. {
  984. dbm = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
  985. rr->rssi = rr->rssi ? (int8_t)((rr->rssi + dbm) / 2) : dbm;
  986. }
  987. if (sinfo[NL80211_STA_INFO_TX_BITRATE])
  988. {
  989. if (!nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
  990. sinfo[NL80211_STA_INFO_TX_BITRATE],
  991. rate_policy))
  992. {
  993. if (rinfo[NL80211_RATE_INFO_BITRATE])
  994. {
  995. mbit = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
  996. rr->rate = rr->rate
  997. ? (int16_t)((rr->rate + mbit) / 2) : mbit;
  998. }
  999. }
  1000. }
  1001. }
  1002. }
  1003. return NL_SKIP;
  1004. }
  1005. static void nl80211_fill_signal(const char *ifname, struct nl80211_rssi_rate *r)
  1006. {
  1007. DIR *d;
  1008. struct dirent *de;
  1009. r->rssi = 0;
  1010. r->rate = 0;
  1011. if ((d = opendir("/sys/class/net")) != NULL)
  1012. {
  1013. while ((de = readdir(d)) != NULL)
  1014. {
  1015. if (!strncmp(de->d_name, ifname, strlen(ifname)) &&
  1016. (!de->d_name[strlen(ifname)] ||
  1017. !strncmp(&de->d_name[strlen(ifname)], ".sta", 4)))
  1018. {
  1019. nl80211_request(de->d_name, NL80211_CMD_GET_STATION,
  1020. NLM_F_DUMP, nl80211_fill_signal_cb, r);
  1021. }
  1022. }
  1023. closedir(d);
  1024. }
  1025. }
  1026. static int nl80211_get_bitrate(const char *ifname, int *buf)
  1027. {
  1028. struct nl80211_rssi_rate rr;
  1029. nl80211_fill_signal(ifname, &rr);
  1030. if (rr.rate)
  1031. {
  1032. *buf = (rr.rate * 100);
  1033. return 0;
  1034. }
  1035. return -1;
  1036. }
  1037. static int nl80211_get_signal(const char *ifname, int *buf)
  1038. {
  1039. struct nl80211_rssi_rate rr;
  1040. nl80211_fill_signal(ifname, &rr);
  1041. if (rr.rssi)
  1042. {
  1043. *buf = rr.rssi;
  1044. return 0;
  1045. }
  1046. return -1;
  1047. }
  1048. static int nl80211_get_noise_cb(struct nl_msg *msg, void *arg)
  1049. {
  1050. int8_t *noise = arg;
  1051. struct nlattr **tb = nl80211_parse(msg);
  1052. struct nlattr *si[NL80211_SURVEY_INFO_MAX + 1];
  1053. static struct nla_policy sp[NL80211_SURVEY_INFO_MAX + 1] = {
  1054. [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
  1055. [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
  1056. };
  1057. if (!tb[NL80211_ATTR_SURVEY_INFO])
  1058. return NL_SKIP;
  1059. if (nla_parse_nested(si, NL80211_SURVEY_INFO_MAX,
  1060. tb[NL80211_ATTR_SURVEY_INFO], sp))
  1061. return NL_SKIP;
  1062. if (!si[NL80211_SURVEY_INFO_NOISE])
  1063. return NL_SKIP;
  1064. if (!*noise || si[NL80211_SURVEY_INFO_IN_USE])
  1065. *noise = (int8_t)nla_get_u8(si[NL80211_SURVEY_INFO_NOISE]);
  1066. return NL_SKIP;
  1067. }
  1068. static int nl80211_get_noise(const char *ifname, int *buf)
  1069. {
  1070. int8_t noise = 0;
  1071. if (nl80211_request(ifname, NL80211_CMD_GET_SURVEY, NLM_F_DUMP,
  1072. nl80211_get_noise_cb, &noise))
  1073. goto out;
  1074. *buf = noise;
  1075. return 0;
  1076. out:
  1077. *buf = 0;
  1078. return -1;
  1079. }
  1080. static int nl80211_get_quality(const char *ifname, int *buf)
  1081. {
  1082. int signal;
  1083. if (!nl80211_get_signal(ifname, &signal))
  1084. {
  1085. /* A positive signal level is usually just a quality
  1086. * value, pass through as-is */
  1087. if (signal >= 0)
  1088. {
  1089. *buf = signal;
  1090. }
  1091. /* The cfg80211 wext compat layer assumes a signal range
  1092. * of -110 dBm to -40 dBm, the quality value is derived
  1093. * by adding 110 to the signal level */
  1094. else
  1095. {
  1096. if (signal < -110)
  1097. signal = -110;
  1098. else if (signal > -40)
  1099. signal = -40;
  1100. *buf = (signal + 110);
  1101. }
  1102. return 0;
  1103. }
  1104. return -1;
  1105. }
  1106. static int nl80211_get_quality_max(const char *ifname, int *buf)
  1107. {
  1108. /* The cfg80211 wext compat layer assumes a maximum
  1109. * quality of 70 */
  1110. *buf = 70;
  1111. return 0;
  1112. }
  1113. static int nl80211_check_wepkey(const char *key)
  1114. {
  1115. if (key && *key)
  1116. {
  1117. switch (strlen(key))
  1118. {
  1119. case 5:
  1120. case 10:
  1121. return IWINFO_CIPHER_WEP40;
  1122. case 13:
  1123. case 26:
  1124. return IWINFO_CIPHER_WEP104;
  1125. }
  1126. }
  1127. return 0;
  1128. }
  1129. static int nl80211_get_encryption(const char *ifname, char *buf)
  1130. {
  1131. char wpa[2], wpa_key_mgmt[16], wpa_pairwise[16], wpa_groupwise[16];
  1132. char auth_algs[2], wep_key0[27], wep_key1[27], wep_key2[27], wep_key3[27];
  1133. struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf;
  1134. /* WPA supplicant */
  1135. if (nl80211_wpactl_query(ifname,
  1136. "pairwise_cipher", wpa_pairwise, sizeof(wpa_pairwise),
  1137. "group_cipher", wpa_groupwise, sizeof(wpa_groupwise),
  1138. "key_mgmt", wpa_key_mgmt, sizeof(wpa_key_mgmt)))
  1139. {
  1140. /* WEP */
  1141. if (!strcmp(wpa_key_mgmt, "NONE"))
  1142. {
  1143. if (strstr(wpa_pairwise, "WEP-40"))
  1144. c->pair_ciphers |= IWINFO_CIPHER_WEP40;
  1145. else if (strstr(wpa_pairwise, "WEP-104"))
  1146. c->pair_ciphers |= IWINFO_CIPHER_WEP104;
  1147. if (strstr(wpa_groupwise, "WEP-40"))
  1148. c->group_ciphers |= IWINFO_CIPHER_WEP40;
  1149. else if (strstr(wpa_groupwise, "WEP-104"))
  1150. c->group_ciphers |= IWINFO_CIPHER_WEP104;
  1151. c->enabled = !!(c->pair_ciphers | c->group_ciphers);
  1152. c->auth_suites |= IWINFO_KMGMT_NONE;
  1153. c->auth_algs |= IWINFO_AUTH_OPEN; /* XXX: assumption */
  1154. }
  1155. /* WPA */
  1156. else if (strstr(wpa_key_mgmt, "WPA"))
  1157. {
  1158. if (strstr(wpa_pairwise, "TKIP"))
  1159. c->pair_ciphers |= IWINFO_CIPHER_TKIP;
  1160. else if (strstr(wpa_pairwise, "CCMP"))
  1161. c->pair_ciphers |= IWINFO_CIPHER_CCMP;
  1162. else if (strstr(wpa_pairwise, "NONE"))
  1163. c->pair_ciphers |= IWINFO_CIPHER_NONE;
  1164. else if (strstr(wpa_pairwise, "WEP-40"))
  1165. c->pair_ciphers |= IWINFO_CIPHER_WEP40;
  1166. else if (strstr(wpa_pairwise, "WEP-104"))
  1167. c->pair_ciphers |= IWINFO_CIPHER_WEP104;
  1168. if (strstr(wpa_groupwise, "TKIP"))
  1169. c->group_ciphers |= IWINFO_CIPHER_TKIP;
  1170. else if (strstr(wpa_groupwise, "CCMP"))
  1171. c->group_ciphers |= IWINFO_CIPHER_CCMP;
  1172. else if (strstr(wpa_groupwise, "NONE"))
  1173. c->group_ciphers |= IWINFO_CIPHER_NONE;
  1174. else if (strstr(wpa_groupwise, "WEP-40"))
  1175. c->group_ciphers |= IWINFO_CIPHER_WEP40;
  1176. else if (strstr(wpa_groupwise, "WEP-104"))
  1177. c->group_ciphers |= IWINFO_CIPHER_WEP104;
  1178. if (strstr(wpa_key_mgmt, "WPA2"))
  1179. c->wpa_version = 2;
  1180. else if (strstr(wpa_key_mgmt, "WPA"))
  1181. c->wpa_version = 1;
  1182. if (strstr(wpa_key_mgmt, "PSK"))
  1183. c->auth_suites |= IWINFO_KMGMT_PSK;
  1184. else if (strstr(wpa_key_mgmt, "EAP") ||
  1185. strstr(wpa_key_mgmt, "802.1X"))
  1186. c->auth_suites |= IWINFO_KMGMT_8021x;
  1187. else if (strstr(wpa_key_mgmt, "NONE"))
  1188. c->auth_suites |= IWINFO_KMGMT_NONE;
  1189. c->enabled = !!(c->wpa_version && c->auth_suites);
  1190. }
  1191. return 0;
  1192. }
  1193. /* Hostapd */
  1194. else if (nl80211_hostapd_query(ifname,
  1195. "wpa", wpa, sizeof(wpa),
  1196. "wpa_key_mgmt", wpa_key_mgmt, sizeof(wpa_key_mgmt),
  1197. "wpa_pairwise", wpa_pairwise, sizeof(wpa_pairwise),
  1198. "auth_algs", auth_algs, sizeof(auth_algs),
  1199. "wep_key0", wep_key0, sizeof(wep_key0),
  1200. "wep_key1", wep_key1, sizeof(wep_key1),
  1201. "wep_key2", wep_key2, sizeof(wep_key2),
  1202. "wep_key3", wep_key3, sizeof(wep_key3)))
  1203. {
  1204. c->wpa_version = wpa[0] ? atoi(wpa) : 0;
  1205. if (wpa_key_mgmt[0])
  1206. {
  1207. if (strstr(wpa_key_mgmt, "PSK"))
  1208. c->auth_suites |= IWINFO_KMGMT_PSK;
  1209. if (strstr(wpa_key_mgmt, "EAP"))
  1210. c->auth_suites |= IWINFO_KMGMT_8021x;
  1211. if (strstr(wpa_key_mgmt, "NONE"))
  1212. c->auth_suites |= IWINFO_KMGMT_NONE;
  1213. }
  1214. else
  1215. {
  1216. c->auth_suites |= IWINFO_KMGMT_PSK;
  1217. }
  1218. if (wpa_pairwise[0])
  1219. {
  1220. if (strstr(wpa_pairwise, "TKIP"))
  1221. c->pair_ciphers |= IWINFO_CIPHER_TKIP;
  1222. if (strstr(wpa_pairwise, "CCMP"))
  1223. c->pair_ciphers |= IWINFO_CIPHER_CCMP;
  1224. if (strstr(wpa_pairwise, "NONE"))
  1225. c->pair_ciphers |= IWINFO_CIPHER_NONE;
  1226. }
  1227. if (auth_algs[0])
  1228. {
  1229. switch(atoi(auth_algs))
  1230. {
  1231. case 1:
  1232. c->auth_algs |= IWINFO_AUTH_OPEN;
  1233. break;
  1234. case 2:
  1235. c->auth_algs |= IWINFO_AUTH_SHARED;
  1236. break;
  1237. case 3:
  1238. c->auth_algs |= IWINFO_AUTH_OPEN;
  1239. c->auth_algs |= IWINFO_AUTH_SHARED;
  1240. break;
  1241. }
  1242. c->pair_ciphers |= nl80211_check_wepkey(wep_key0);
  1243. c->pair_ciphers |= nl80211_check_wepkey(wep_key1);
  1244. c->pair_ciphers |= nl80211_check_wepkey(wep_key2);
  1245. c->pair_ciphers |= nl80211_check_wepkey(wep_key3);
  1246. }
  1247. c->group_ciphers = c->pair_ciphers;
  1248. c->enabled = (c->wpa_version || c->pair_ciphers) ? 1 : 0;
  1249. return 0;
  1250. }
  1251. return -1;
  1252. }
  1253. static int nl80211_get_phyname(const char *ifname, char *buf)
  1254. {
  1255. const char *name;
  1256. name = nl80211_ifname2phy(ifname);
  1257. if (name)
  1258. {
  1259. strcpy(buf, name);
  1260. return 0;
  1261. }
  1262. else if ((name = nl80211_phy2ifname(ifname)) != NULL)
  1263. {
  1264. name = nl80211_ifname2phy(name);
  1265. if (name)
  1266. {
  1267. strcpy(buf, ifname);
  1268. return 0;
  1269. }
  1270. }
  1271. return -1;
  1272. }
  1273. static void nl80211_parse_rateinfo(struct nlattr **ri,
  1274. struct iwinfo_rate_entry *re)
  1275. {
  1276. if (ri[NL80211_RATE_INFO_BITRATE32])
  1277. re->rate = nla_get_u32(ri[NL80211_RATE_INFO_BITRATE32]) * 100;
  1278. else if (ri[NL80211_RATE_INFO_BITRATE])
  1279. re->rate = nla_get_u16(ri[NL80211_RATE_INFO_BITRATE]) * 100;
  1280. if (ri[NL80211_RATE_INFO_VHT_MCS])
  1281. {
  1282. re->is_vht = 1;
  1283. re->mcs = nla_get_u8(ri[NL80211_RATE_INFO_VHT_MCS]);
  1284. if (ri[NL80211_RATE_INFO_VHT_NSS])
  1285. re->nss = nla_get_u8(ri[NL80211_RATE_INFO_VHT_NSS]);
  1286. }
  1287. else if (ri[NL80211_RATE_INFO_MCS])
  1288. {
  1289. re->is_ht = 1;
  1290. re->mcs = nla_get_u8(ri[NL80211_RATE_INFO_MCS]);
  1291. }
  1292. if (ri[NL80211_RATE_INFO_5_MHZ_WIDTH])
  1293. re->mhz = 5;
  1294. else if (ri[NL80211_RATE_INFO_10_MHZ_WIDTH])
  1295. re->mhz = 10;
  1296. else if (ri[NL80211_RATE_INFO_40_MHZ_WIDTH])
  1297. re->mhz = 40;
  1298. else if (ri[NL80211_RATE_INFO_80_MHZ_WIDTH])
  1299. re->mhz = 80;
  1300. else if (ri[NL80211_RATE_INFO_80P80_MHZ_WIDTH] ||
  1301. ri[NL80211_RATE_INFO_160_MHZ_WIDTH])
  1302. re->mhz = 160;
  1303. else
  1304. re->mhz = 20;
  1305. if (ri[NL80211_RATE_INFO_SHORT_GI])
  1306. re->is_short_gi = 1;
  1307. re->is_40mhz = (re->mhz == 40);
  1308. }
  1309. static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
  1310. {
  1311. struct nl80211_array_buf *arr = arg;
  1312. struct iwinfo_assoclist_entry *e = arr->buf;
  1313. struct nlattr **attr = nl80211_parse(msg);
  1314. struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
  1315. struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
  1316. struct nl80211_sta_flag_update *sta_flags;
  1317. static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
  1318. [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
  1319. [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
  1320. [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
  1321. [NL80211_STA_INFO_RX_BITRATE] = { .type = NLA_NESTED },
  1322. [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
  1323. [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
  1324. [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
  1325. [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
  1326. [NL80211_STA_INFO_TX_RETRIES] = { .type = NLA_U32 },
  1327. [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
  1328. [NL80211_STA_INFO_T_OFFSET] = { .type = NLA_U64 },
  1329. [NL80211_STA_INFO_STA_FLAGS] =
  1330. { .minlen = sizeof(struct nl80211_sta_flag_update) },
  1331. [NL80211_STA_INFO_EXPECTED_THROUGHPUT] = { .type = NLA_U32 },
  1332. };
  1333. static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
  1334. [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
  1335. [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
  1336. [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
  1337. [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
  1338. };
  1339. /* advance to end of array */
  1340. e += arr->count;
  1341. memset(e, 0, sizeof(*e));
  1342. if (attr[NL80211_ATTR_MAC])
  1343. memcpy(e->mac, nla_data(attr[NL80211_ATTR_MAC]), 6);
  1344. if (attr[NL80211_ATTR_STA_INFO] &&
  1345. !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
  1346. attr[NL80211_ATTR_STA_INFO], stats_policy))
  1347. {
  1348. if (sinfo[NL80211_STA_INFO_SIGNAL])
  1349. e->signal = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
  1350. if (sinfo[NL80211_STA_INFO_INACTIVE_TIME])
  1351. e->inactive = nla_get_u32(sinfo[NL80211_STA_INFO_INACTIVE_TIME]);
  1352. if (sinfo[NL80211_STA_INFO_RX_PACKETS])
  1353. e->rx_packets = nla_get_u32(sinfo[NL80211_STA_INFO_RX_PACKETS]);
  1354. if (sinfo[NL80211_STA_INFO_TX_PACKETS])
  1355. e->tx_packets = nla_get_u32(sinfo[NL80211_STA_INFO_TX_PACKETS]);
  1356. if (sinfo[NL80211_STA_INFO_RX_BITRATE] &&
  1357. !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
  1358. sinfo[NL80211_STA_INFO_RX_BITRATE], rate_policy))
  1359. nl80211_parse_rateinfo(rinfo, &e->rx_rate);
  1360. if (sinfo[NL80211_STA_INFO_TX_BITRATE] &&
  1361. !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
  1362. sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy))
  1363. nl80211_parse_rateinfo(rinfo, &e->tx_rate);
  1364. if (sinfo[NL80211_STA_INFO_RX_BYTES])
  1365. e->rx_bytes = nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES]);
  1366. if (sinfo[NL80211_STA_INFO_TX_BYTES])
  1367. e->tx_bytes = nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES]);
  1368. if (sinfo[NL80211_STA_INFO_TX_RETRIES])
  1369. e->tx_retries = nla_get_u32(sinfo[NL80211_STA_INFO_TX_RETRIES]);
  1370. if (sinfo[NL80211_STA_INFO_TX_FAILED])
  1371. e->tx_failed = nla_get_u32(sinfo[NL80211_STA_INFO_TX_FAILED]);
  1372. if (sinfo[NL80211_STA_INFO_T_OFFSET])
  1373. e->t_offset = nla_get_u64(sinfo[NL80211_STA_INFO_T_OFFSET]);
  1374. if (sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT])
  1375. e->thr = nla_get_u32(sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT]);
  1376. /* Station flags */
  1377. if (sinfo[NL80211_STA_INFO_STA_FLAGS])
  1378. {
  1379. sta_flags = (struct nl80211_sta_flag_update *)
  1380. nla_data(sinfo[NL80211_STA_INFO_STA_FLAGS]);
  1381. if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
  1382. sta_flags->set & BIT(NL80211_STA_FLAG_AUTHORIZED))
  1383. e->is_authorized = 1;
  1384. if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHENTICATED) &&
  1385. sta_flags->set & BIT(NL80211_STA_FLAG_AUTHENTICATED))
  1386. e->is_authenticated = 1;
  1387. if (sta_flags->mask & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) &&
  1388. sta_flags->set & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE))
  1389. e->is_preamble_short = 1;
  1390. if (sta_flags->mask & BIT(NL80211_STA_FLAG_WME) &&
  1391. sta_flags->set & BIT(NL80211_STA_FLAG_WME))
  1392. e->is_wme = 1;
  1393. if (sta_flags->mask & BIT(NL80211_STA_FLAG_MFP) &&
  1394. sta_flags->set & BIT(NL80211_STA_FLAG_MFP))
  1395. e->is_mfp = 1;
  1396. if (sta_flags->mask & BIT(NL80211_STA_FLAG_TDLS_PEER) &&
  1397. sta_flags->set & BIT(NL80211_STA_FLAG_TDLS_PEER))
  1398. e->is_tdls = 1;
  1399. }
  1400. }
  1401. e->noise = 0; /* filled in by caller */
  1402. arr->count++;
  1403. return NL_SKIP;
  1404. }
  1405. static int nl80211_get_assoclist(const char *ifname, char *buf, int *len)
  1406. {
  1407. DIR *d;
  1408. int i, noise = 0;
  1409. struct dirent *de;
  1410. struct nl80211_array_buf arr = { .buf = buf, .count = 0 };
  1411. struct iwinfo_assoclist_entry *e;
  1412. if ((d = opendir("/sys/class/net")) != NULL)
  1413. {
  1414. while ((de = readdir(d)) != NULL)
  1415. {
  1416. if (!strncmp(de->d_name, ifname, strlen(ifname)) &&
  1417. (!de->d_name[strlen(ifname)] ||
  1418. !strncmp(&de->d_name[strlen(ifname)], ".sta", 4)))
  1419. {
  1420. nl80211_request(de->d_name, NL80211_CMD_GET_STATION,
  1421. NLM_F_DUMP, nl80211_get_assoclist_cb, &arr);
  1422. }
  1423. }
  1424. closedir(d);
  1425. if (!nl80211_get_noise(ifname, &noise))
  1426. for (i = 0, e = arr.buf; i < arr.count; i++, e++)
  1427. e->noise = noise;
  1428. *len = (arr.count * sizeof(struct iwinfo_assoclist_entry));
  1429. return 0;
  1430. }
  1431. return -1;
  1432. }
  1433. static int nl80211_get_txpwrlist_cb(struct nl_msg *msg, void *arg)
  1434. {
  1435. int *dbm_max = arg;
  1436. int ch_cur, ch_cmp, bands_remain, freqs_remain;
  1437. struct nlattr **attr = nl80211_parse(msg);
  1438. struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
  1439. struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
  1440. struct nlattr *band, *freq;
  1441. static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
  1442. [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
  1443. [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
  1444. [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
  1445. [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
  1446. [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
  1447. [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
  1448. };
  1449. ch_cur = *dbm_max; /* value int* is initialized with channel by caller */
  1450. *dbm_max = -1;
  1451. nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
  1452. {
  1453. nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
  1454. nla_len(band), NULL);
  1455. nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
  1456. {
  1457. nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
  1458. nla_data(freq), nla_len(freq), freq_policy);
  1459. ch_cmp = nl80211_freq2channel(nla_get_u32(
  1460. freqs[NL80211_FREQUENCY_ATTR_FREQ]));
  1461. if ((!ch_cur || (ch_cmp == ch_cur)) &&
  1462. freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER])
  1463. {
  1464. *dbm_max = (int)(0.01 * nla_get_u32(
  1465. freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]));
  1466. break;
  1467. }
  1468. }
  1469. }
  1470. return NL_SKIP;
  1471. }
  1472. static int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len)
  1473. {
  1474. int err, ch_cur;
  1475. int dbm_max = -1, dbm_cur, dbm_cnt;
  1476. struct nl80211_msg_conveyor *req;
  1477. struct iwinfo_txpwrlist_entry entry;
  1478. if (nl80211_get_channel(ifname, &ch_cur))
  1479. ch_cur = 0;
  1480. /* initialize the value pointer with channel for callback */
  1481. dbm_max = ch_cur;
  1482. err = nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
  1483. nl80211_get_txpwrlist_cb, &dbm_max);
  1484. if (!err)
  1485. {
  1486. for (dbm_cur = 0, dbm_cnt = 0;
  1487. dbm_cur < dbm_max;
  1488. dbm_cur++, dbm_cnt++)
  1489. {
  1490. entry.dbm = dbm_cur;
  1491. entry.mw = iwinfo_dbm2mw(dbm_cur);
  1492. memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
  1493. }
  1494. entry.dbm = dbm_max;
  1495. entry.mw = iwinfo_dbm2mw(dbm_max);
  1496. memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
  1497. dbm_cnt++;
  1498. *len = dbm_cnt * sizeof(entry);
  1499. return 0;
  1500. }
  1501. return -1;
  1502. }
  1503. static void nl80211_get_scancrypto(const char *spec,
  1504. struct iwinfo_crypto_entry *c)
  1505. {
  1506. if (strstr(spec, "WPA") || strstr(spec, "WEP"))
  1507. {
  1508. c->enabled = 1;
  1509. if (strstr(spec, "WPA2-") && strstr(spec, "WPA-"))
  1510. c->wpa_version = 3;
  1511. else if (strstr(spec, "WPA2"))
  1512. c->wpa_version = 2;
  1513. else if (strstr(spec, "WPA"))
  1514. c->wpa_version = 1;
  1515. else if (strstr(spec, "WEP"))
  1516. c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
  1517. if (strstr(spec, "PSK"))
  1518. c->auth_suites |= IWINFO_KMGMT_PSK;
  1519. if (strstr(spec, "802.1X") || strstr(spec, "EAP"))
  1520. c->auth_suites |= IWINFO_KMGMT_8021x;
  1521. if (strstr(spec, "WPA-NONE"))
  1522. c->auth_suites |= IWINFO_KMGMT_NONE;
  1523. if (strstr(spec, "TKIP"))
  1524. c->pair_ciphers |= IWINFO_CIPHER_TKIP;
  1525. if (strstr(spec, "CCMP"))
  1526. c->pair_ciphers |= IWINFO_CIPHER_CCMP;
  1527. if (strstr(spec, "WEP-40"))
  1528. c->pair_ciphers |= IWINFO_CIPHER_WEP40;
  1529. if (strstr(spec, "WEP-104"))
  1530. c->pair_ciphers |= IWINFO_CIPHER_WEP104;
  1531. c->group_ciphers = c->pair_ciphers;
  1532. }
  1533. else
  1534. {
  1535. c->enabled = 0;
  1536. }
  1537. }
  1538. struct nl80211_scanlist {
  1539. struct iwinfo_scanlist_entry *e;
  1540. int len;
  1541. };
  1542. static void nl80211_get_scanlist_ie(struct nlattr **bss,
  1543. struct iwinfo_scanlist_entry *e)
  1544. {
  1545. int ielen = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
  1546. unsigned char *ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
  1547. static unsigned char ms_oui[3] = { 0x00, 0x50, 0xf2 };
  1548. int len;
  1549. while (ielen >= 2 && ielen >= ie[1])
  1550. {
  1551. switch (ie[0])
  1552. {
  1553. case 0: /* SSID */
  1554. len = min(ie[1], IWINFO_ESSID_MAX_SIZE);
  1555. memcpy(e->ssid, ie + 2, len);
  1556. e->ssid[len] = 0;
  1557. break;
  1558. case 48: /* RSN */
  1559. iwinfo_parse_rsn(&e->crypto, ie + 2, ie[1],
  1560. IWINFO_CIPHER_CCMP, IWINFO_KMGMT_8021x);
  1561. break;
  1562. case 221: /* Vendor */
  1563. if (ie[1] >= 4 && !memcmp(ie + 2, ms_oui, 3) && ie[5] == 1)
  1564. iwinfo_parse_rsn(&e->crypto, ie + 6, ie[1] - 4,
  1565. IWINFO_CIPHER_TKIP, IWINFO_KMGMT_PSK);
  1566. break;
  1567. }
  1568. ielen -= ie[1] + 2;
  1569. ie += ie[1] + 2;
  1570. }
  1571. }
  1572. static int nl80211_get_scanlist_cb(struct nl_msg *msg, void *arg)
  1573. {
  1574. int8_t rssi;
  1575. uint16_t caps;
  1576. struct nl80211_scanlist *sl = arg;
  1577. struct nlattr **tb = nl80211_parse(msg);
  1578. struct nlattr *bss[NL80211_BSS_MAX + 1];
  1579. static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
  1580. [NL80211_BSS_TSF] = { .type = NLA_U64 },
  1581. [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
  1582. [NL80211_BSS_BSSID] = { 0 },
  1583. [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
  1584. [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
  1585. [NL80211_BSS_INFORMATION_ELEMENTS] = { 0 },
  1586. [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
  1587. [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
  1588. [NL80211_BSS_STATUS] = { .type = NLA_U32 },
  1589. [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
  1590. [NL80211_BSS_BEACON_IES] = { 0 },
  1591. };
  1592. if (!tb[NL80211_ATTR_BSS] ||
  1593. nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
  1594. bss_policy) ||
  1595. !bss[NL80211_BSS_BSSID])
  1596. {
  1597. return NL_SKIP;
  1598. }
  1599. if (bss[NL80211_BSS_CAPABILITY])
  1600. caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
  1601. else
  1602. caps = 0;
  1603. memset(sl->e, 0, sizeof(*sl->e));
  1604. memcpy(sl->e->mac, nla_data(bss[NL80211_BSS_BSSID]), 6);
  1605. if (caps & (1<<1))
  1606. sl->e->mode = IWINFO_OPMODE_ADHOC;
  1607. else if (caps & (1<<0))
  1608. sl->e->mode = IWINFO_OPMODE_MASTER;
  1609. else
  1610. sl->e->mode = IWINFO_OPMODE_MESHPOINT;
  1611. if (caps & (1<<4))
  1612. sl->e->crypto.enabled = 1;
  1613. if (bss[NL80211_BSS_FREQUENCY])
  1614. sl->e->channel = nl80211_freq2channel(nla_get_u32(
  1615. bss[NL80211_BSS_FREQUENCY]));
  1616. if (bss[NL80211_BSS_INFORMATION_ELEMENTS])
  1617. nl80211_get_scanlist_ie(bss, sl->e);
  1618. if (bss[NL80211_BSS_SIGNAL_MBM])
  1619. {
  1620. sl->e->signal =
  1621. (uint8_t)((int32_t)nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]) / 100);
  1622. rssi = sl->e->signal - 0x100;
  1623. if (rssi < -110)
  1624. rssi = -110;
  1625. else if (rssi > -40)
  1626. rssi = -40;
  1627. sl->e->quality = (rssi + 110);
  1628. sl->e->quality_max = 70;
  1629. }
  1630. if (sl->e->crypto.enabled && !sl->e->crypto.wpa_version)
  1631. {
  1632. sl->e->crypto.auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
  1633. sl->e->crypto.pair_ciphers = IWINFO_CIPHER_WEP40 | IWINFO_CIPHER_WEP104;
  1634. }
  1635. sl->e++;
  1636. sl->len++;
  1637. return NL_SKIP;
  1638. }
  1639. static int nl80211_get_scanlist_nl(const char *ifname, char *buf, int *len)
  1640. {
  1641. struct nl80211_scanlist sl = { .e = (struct iwinfo_scanlist_entry *)buf };
  1642. if (nl80211_request(ifname, NL80211_CMD_TRIGGER_SCAN, 0, NULL, NULL))
  1643. goto out;
  1644. if (nl80211_wait("nl80211", "scan",
  1645. NL80211_CMD_NEW_SCAN_RESULTS, NL80211_CMD_SCAN_ABORTED))
  1646. goto out;
  1647. if (nl80211_request(ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP,
  1648. nl80211_get_scanlist_cb, &sl))
  1649. goto out;
  1650. *len = sl.len * sizeof(struct iwinfo_scanlist_entry);
  1651. return 0;
  1652. out:
  1653. *len = 0;
  1654. return -1;
  1655. }
  1656. static int wpasupp_ssid_decode(const char *in, char *out, int outlen)
  1657. {
  1658. #define hex(x) \
  1659. (((x) >= 'a') ? ((x) - 'a' + 10) : \
  1660. (((x) >= 'A') ? ((x) - 'A' + 10) : ((x) - '0')))
  1661. int len = 0;
  1662. while (*in)
  1663. {
  1664. if (len + 1 >= outlen)
  1665. break;
  1666. switch (*in)
  1667. {
  1668. case '\\':
  1669. in++;
  1670. switch (*in)
  1671. {
  1672. case 'n':
  1673. out[len++] = '\n'; in++;
  1674. break;
  1675. case 'r':
  1676. out[len++] = '\r'; in++;
  1677. break;
  1678. case 't':
  1679. out[len++] = '\t'; in++;
  1680. break;
  1681. case 'e':
  1682. out[len++] = '\033'; in++;
  1683. break;
  1684. case 'x':
  1685. if (isxdigit(*(in+1)) && isxdigit(*(in+2)))
  1686. out[len++] = hex(*(in+1)) * 16 + hex(*(in+2));
  1687. in += 3;
  1688. break;
  1689. default:
  1690. out[len++] = *in++;
  1691. break;
  1692. }
  1693. break;
  1694. default:
  1695. out[len++] = *in++;
  1696. break;
  1697. }
  1698. }
  1699. if (outlen > len)
  1700. out[len] = '\0';
  1701. return len;
  1702. }
  1703. static int nl80211_get_scanlist_wpactl(const char *ifname, char *buf, int *len)
  1704. {
  1705. int sock, qmax, rssi, tries, count = -1, ready = 0;
  1706. char *pos, *line, *bssid, *freq, *signal, *flags, *ssid, reply[4096];
  1707. struct sockaddr_un local = { 0 };
  1708. struct iwinfo_scanlist_entry *e = (struct iwinfo_scanlist_entry *)buf;
  1709. sock = nl80211_wpactl_connect(ifname, &local);
  1710. if (sock < 0)
  1711. return sock;
  1712. send(sock, "ATTACH", 6, 0);
  1713. send(sock, "SCAN", 4, 0);
  1714. /*
  1715. * wait for scan results:
  1716. * nl80211_wpactl_recv() will use a timeout of 256ms and we need to scan
  1717. * 72 channels at most. We'll also receive two "OK" messages acknowledging
  1718. * the "ATTACH" and "SCAN" commands and the driver might need a bit extra
  1719. * time to process the results, so try 72 + 2 + 1 times.
  1720. */
  1721. for (tries = 0; tries < 75; tries++)
  1722. {
  1723. if (nl80211_wpactl_recv(sock, reply, sizeof(reply)) <= 0)
  1724. continue;
  1725. /* got an event notification */
  1726. if (reply[0] == '<')
  1727. {
  1728. /* scan results are ready */
  1729. if (strstr(reply, "CTRL-EVENT-SCAN-RESULTS"))
  1730. {
  1731. /* send "SCAN_RESULTS" command */
  1732. ready = (send(sock, "SCAN_RESULTS", 12, 0) == 12);
  1733. break;
  1734. }
  1735. /* is another unrelated event, retry */
  1736. tries--;
  1737. }
  1738. }
  1739. /* receive and parse scan results if the wait above didn't time out */
  1740. while (ready && nl80211_wpactl_recv(sock, reply, sizeof(reply)) > 0)
  1741. {
  1742. /* received an event notification, receive again */
  1743. if (reply[0] == '<')
  1744. continue;
  1745. nl80211_get_quality_max(ifname, &qmax);
  1746. for (line = strtok_r(reply, "\n", &pos);
  1747. line != NULL;
  1748. line = strtok_r(NULL, "\n", &pos))
  1749. {
  1750. /* skip header line */
  1751. if (count < 0)
  1752. {
  1753. count++;
  1754. continue;
  1755. }
  1756. bssid = strtok(line, "\t");
  1757. freq = strtok(NULL, "\t");
  1758. signal = strtok(NULL, "\t");
  1759. flags = strtok(NULL, "\t");
  1760. ssid = strtok(NULL, "\n");
  1761. if (!bssid || !freq || !signal || !flags || !ssid)
  1762. continue;
  1763. /* BSSID */
  1764. e->mac[0] = strtol(&bssid[0], NULL, 16);
  1765. e->mac[1] = strtol(&bssid[3], NULL, 16);
  1766. e->mac[2] = strtol(&bssid[6], NULL, 16);
  1767. e->mac[3] = strtol(&bssid[9], NULL, 16);
  1768. e->mac[4] = strtol(&bssid[12], NULL, 16);
  1769. e->mac[5] = strtol(&bssid[15], NULL, 16);
  1770. /* SSID */
  1771. wpasupp_ssid_decode(ssid, e->ssid, sizeof(e->ssid));
  1772. /* Mode */
  1773. if (strstr(flags, "[MESH]"))
  1774. e->mode = IWINFO_OPMODE_MESHPOINT;
  1775. else if (strstr(flags, "[IBSS]"))
  1776. e->mode = IWINFO_OPMODE_ADHOC;
  1777. else
  1778. e->mode = IWINFO_OPMODE_MASTER;
  1779. /* Channel */
  1780. e->channel = nl80211_freq2channel(atoi(freq));
  1781. /* Signal */
  1782. rssi = atoi(signal);
  1783. e->signal = rssi;
  1784. /* Quality */
  1785. if (rssi < 0)
  1786. {
  1787. /* The cfg80211 wext compat layer assumes a signal range
  1788. * of -110 dBm to -40 dBm, the quality value is derived
  1789. * by adding 110 to the signal level */
  1790. if (rssi < -110)
  1791. rssi = -110;
  1792. else if (rssi > -40)
  1793. rssi = -40;
  1794. e->quality = (rssi + 110);
  1795. }
  1796. else
  1797. {
  1798. e->quality = rssi;
  1799. }
  1800. /* Max. Quality */
  1801. e->quality_max = qmax;
  1802. /* Crypto */
  1803. nl80211_get_scancrypto(flags, &e->crypto);
  1804. count++;
  1805. e++;
  1806. }
  1807. *len = count * sizeof(struct iwinfo_scanlist_entry);
  1808. break;
  1809. }
  1810. close(sock);
  1811. unlink(local.sun_path);
  1812. return (count >= 0) ? 0 : -1;
  1813. }
  1814. static int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
  1815. {
  1816. char *res;
  1817. int rv, mode;
  1818. *len = 0;
  1819. /* Got a radioX pseudo interface, find some interface on it or create one */
  1820. if (!strncmp(ifname, "radio", 5))
  1821. {
  1822. /* Reuse existing interface */
  1823. if ((res = nl80211_phy2ifname(ifname)) != NULL)
  1824. {
  1825. return nl80211_get_scanlist(res, buf, len);
  1826. }
  1827. /* Need to spawn a temporary iface for scanning */
  1828. else if ((res = nl80211_ifadd(ifname)) != NULL)
  1829. {
  1830. rv = nl80211_get_scanlist(res, buf, len);
  1831. nl80211_ifdel(res);
  1832. return rv;
  1833. }
  1834. }
  1835. /* WPA supplicant */
  1836. if (!nl80211_get_scanlist_wpactl(ifname, buf, len))
  1837. {
  1838. return 0;
  1839. }
  1840. /* station / ad-hoc / monitor scan */
  1841. else if (!nl80211_get_mode(ifname, &mode) &&
  1842. (mode == IWINFO_OPMODE_ADHOC ||
  1843. mode == IWINFO_OPMODE_MASTER ||
  1844. mode == IWINFO_OPMODE_CLIENT ||
  1845. mode == IWINFO_OPMODE_MONITOR) &&
  1846. iwinfo_ifup(ifname))
  1847. {
  1848. return nl80211_get_scanlist_nl(ifname, buf, len);
  1849. }
  1850. /* AP scan */
  1851. else
  1852. {
  1853. /* Got a temp interface, don't create yet another one */
  1854. if (!strncmp(ifname, "tmp.", 4))
  1855. {
  1856. if (!iwinfo_ifup(ifname))
  1857. return -1;
  1858. rv = nl80211_get_scanlist_nl(ifname, buf, len);
  1859. iwinfo_ifdown(ifname);
  1860. return rv;
  1861. }
  1862. /* Spawn a new scan interface */
  1863. else
  1864. {
  1865. if (!(res = nl80211_ifadd(ifname)))
  1866. return -1;
  1867. iwinfo_ifmac(res);
  1868. /* if we can take the new interface up, the driver supports an
  1869. * additional interface and there's no need to tear down the ap */
  1870. if (iwinfo_ifup(res))
  1871. {
  1872. rv = nl80211_get_scanlist_nl(res, buf, len);
  1873. iwinfo_ifdown(res);
  1874. }
  1875. /* driver cannot create secondary interface, take down ap
  1876. * during scan */
  1877. else if (iwinfo_ifdown(ifname) && iwinfo_ifup(res))
  1878. {
  1879. rv = nl80211_get_scanlist_nl(res, buf, len);
  1880. iwinfo_ifdown(res);
  1881. iwinfo_ifup(ifname);
  1882. nl80211_hostapd_hup(ifname);
  1883. }
  1884. nl80211_ifdel(res);
  1885. return rv;
  1886. }
  1887. }
  1888. return -1;
  1889. }
  1890. static int nl80211_get_freqlist_cb(struct nl_msg *msg, void *arg)
  1891. {
  1892. int bands_remain, freqs_remain;
  1893. struct nl80211_array_buf *arr = arg;
  1894. struct iwinfo_freqlist_entry *e;
  1895. struct nlattr **attr = nl80211_parse(msg);
  1896. struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
  1897. struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
  1898. struct nlattr *band, *freq;
  1899. e = arr->buf;
  1900. e += arr->count;
  1901. if (attr[NL80211_ATTR_WIPHY_BANDS]) {
  1902. nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
  1903. {
  1904. nla_parse(bands, NL80211_BAND_ATTR_MAX,
  1905. nla_data(band), nla_len(band), NULL);
  1906. if (bands[NL80211_BAND_ATTR_FREQS]) {
  1907. nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
  1908. {
  1909. nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
  1910. nla_data(freq), nla_len(freq), NULL);
  1911. if (!freqs[NL80211_FREQUENCY_ATTR_FREQ] ||
  1912. freqs[NL80211_FREQUENCY_ATTR_DISABLED])
  1913. continue;
  1914. e->mhz = nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]);
  1915. e->channel = nl80211_freq2channel(e->mhz);
  1916. e->restricted = (
  1917. freqs[NL80211_FREQUENCY_ATTR_NO_IR] &&
  1918. !freqs[NL80211_FREQUENCY_ATTR_RADAR]
  1919. ) ? 1 : 0;
  1920. if (freqs[NL80211_FREQUENCY_ATTR_NO_HT40_MINUS])
  1921. e->flags |= IWINFO_FREQ_NO_HT40MINUS;
  1922. if (freqs[NL80211_FREQUENCY_ATTR_NO_HT40_PLUS])
  1923. e->flags |= IWINFO_FREQ_NO_HT40PLUS;
  1924. if (freqs[NL80211_FREQUENCY_ATTR_NO_80MHZ])
  1925. e->flags |= IWINFO_FREQ_NO_80MHZ;
  1926. if (freqs[NL80211_FREQUENCY_ATTR_NO_160MHZ])
  1927. e->flags |= IWINFO_FREQ_NO_160MHZ;
  1928. if (freqs[NL80211_FREQUENCY_ATTR_NO_20MHZ])
  1929. e->flags |= IWINFO_FREQ_NO_20MHZ;
  1930. if (freqs[NL80211_FREQUENCY_ATTR_NO_10MHZ])
  1931. e->flags |= IWINFO_FREQ_NO_10MHZ;
  1932. e++;
  1933. arr->count++;
  1934. }
  1935. }
  1936. }
  1937. }
  1938. return NL_SKIP;
  1939. }
  1940. static int nl80211_get_freqlist(const char *ifname, char *buf, int *len)
  1941. {
  1942. struct nl80211_msg_conveyor *cv;
  1943. struct nl80211_array_buf arr = { .buf = buf, .count = 0 };
  1944. uint32_t features = nl80211_get_protocol_features(ifname);
  1945. int flags;
  1946. flags = features & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP ? NLM_F_DUMP : 0;
  1947. cv = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, flags);
  1948. if (!cv)
  1949. goto out;
  1950. NLA_PUT_FLAG(cv->msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
  1951. if (nl80211_send(cv, nl80211_get_freqlist_cb, &arr))
  1952. goto out;
  1953. *len = arr.count * sizeof(struct iwinfo_freqlist_entry);
  1954. return 0;
  1955. nla_put_failure:
  1956. nl80211_free(cv);
  1957. out:
  1958. *len = 0;
  1959. return -1;
  1960. }
  1961. static int nl80211_get_country_cb(struct nl_msg *msg, void *arg)
  1962. {
  1963. char *buf = arg;
  1964. struct nlattr **attr = nl80211_parse(msg);
  1965. if (attr[NL80211_ATTR_REG_ALPHA2])
  1966. memcpy(buf, nla_data(attr[NL80211_ATTR_REG_ALPHA2]), 2);
  1967. else
  1968. buf[0] = 0;
  1969. return NL_SKIP;
  1970. }
  1971. static int nl80211_get_country(const char *ifname, char *buf)
  1972. {
  1973. if (nl80211_request(ifname, NL80211_CMD_GET_REG, 0,
  1974. nl80211_get_country_cb, buf))
  1975. return -1;
  1976. return 0;
  1977. }
  1978. static int nl80211_get_countrylist(const char *ifname, char *buf, int *len)
  1979. {
  1980. int count;
  1981. struct iwinfo_country_entry *e = (struct iwinfo_country_entry *)buf;
  1982. const struct iwinfo_iso3166_label *l;
  1983. for (l = IWINFO_ISO3166_NAMES, count = 0; l->iso3166; l++, e++, count++)
  1984. {
  1985. e->iso3166 = l->iso3166;
  1986. e->ccode[0] = (l->iso3166 / 256);
  1987. e->ccode[1] = (l->iso3166 % 256);
  1988. e->ccode[2] = 0;
  1989. }
  1990. *len = (count * sizeof(struct iwinfo_country_entry));
  1991. return 0;
  1992. }
  1993. struct nl80211_modes
  1994. {
  1995. bool ok;
  1996. uint32_t hw;
  1997. uint32_t ht;
  1998. };
  1999. static int nl80211_get_modelist_cb(struct nl_msg *msg, void *arg)
  2000. {
  2001. struct nl80211_modes *m = arg;
  2002. int bands_remain, freqs_remain;
  2003. uint16_t caps = 0;
  2004. uint32_t vht_caps = 0;
  2005. struct nlattr **attr = nl80211_parse(msg);
  2006. struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
  2007. struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
  2008. struct nlattr *band, *freq;
  2009. if (attr[NL80211_ATTR_WIPHY_BANDS])
  2010. {
  2011. nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
  2012. {
  2013. nla_parse(bands, NL80211_BAND_ATTR_MAX,
  2014. nla_data(band), nla_len(band), NULL);
  2015. if (bands[NL80211_BAND_ATTR_HT_CAPA])
  2016. caps = nla_get_u16(bands[NL80211_BAND_ATTR_HT_CAPA]);
  2017. /* Treat any nonzero capability as 11n */
  2018. if (caps > 0)
  2019. {
  2020. m->hw |= IWINFO_80211_N;
  2021. m->ht |= IWINFO_HTMODE_HT20;
  2022. if (caps & (1 << 1))
  2023. m->ht |= IWINFO_HTMODE_HT40;
  2024. }
  2025. nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS],
  2026. freqs_remain)
  2027. {
  2028. nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
  2029. nla_data(freq), nla_len(freq), NULL);
  2030. if (!freqs[NL80211_FREQUENCY_ATTR_FREQ])
  2031. continue;
  2032. if (nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]) < 2485)
  2033. {
  2034. m->hw |= IWINFO_80211_B;
  2035. m->hw |= IWINFO_80211_G;
  2036. }
  2037. else if (bands[NL80211_BAND_ATTR_VHT_CAPA])
  2038. {
  2039. vht_caps = nla_get_u32(bands[NL80211_BAND_ATTR_VHT_CAPA]);
  2040. /* Treat any nonzero capability as 11ac */
  2041. if (vht_caps > 0)
  2042. {
  2043. m->hw |= IWINFO_80211_AC;
  2044. m->ht |= IWINFO_HTMODE_VHT20 | IWINFO_HTMODE_VHT40 | IWINFO_HTMODE_VHT80;
  2045. switch ((vht_caps >> 2) & 3)
  2046. {
  2047. case 2:
  2048. m->ht |= IWINFO_HTMODE_VHT80_80;
  2049. /* fall through */
  2050. case 1:
  2051. m->ht |= IWINFO_HTMODE_VHT160;
  2052. }
  2053. }
  2054. }
  2055. else if (!(m->hw & IWINFO_80211_AC))
  2056. {
  2057. m->hw |= IWINFO_80211_A;
  2058. }
  2059. }
  2060. }
  2061. m->ok = 1;
  2062. }
  2063. return NL_SKIP;
  2064. }
  2065. static int nl80211_get_hwmodelist(const char *ifname, int *buf)
  2066. {
  2067. struct nl80211_modes m = { 0 };
  2068. if (nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
  2069. nl80211_get_modelist_cb, &m))
  2070. goto out;
  2071. if (!m.ok)
  2072. goto out;
  2073. *buf = m.hw;
  2074. return 0;
  2075. out:
  2076. *buf = 0;
  2077. return -1;
  2078. }
  2079. static int nl80211_get_htmodelist(const char *ifname, int *buf)
  2080. {
  2081. struct nl80211_modes m = { 0 };
  2082. if (nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
  2083. nl80211_get_modelist_cb, &m))
  2084. goto out;
  2085. if (!m.ok)
  2086. goto out;
  2087. *buf = m.ht;
  2088. return 0;
  2089. out:
  2090. *buf = 0;
  2091. return -1;
  2092. }
  2093. static int nl80211_get_ifcomb_cb(struct nl_msg *msg, void *arg)
  2094. {
  2095. struct nlattr **attr = nl80211_parse(msg);
  2096. struct nlattr *comb;
  2097. int *ret = arg;
  2098. int comb_rem, limit_rem, mode_rem;
  2099. *ret = 0;
  2100. if (!attr[NL80211_ATTR_INTERFACE_COMBINATIONS])
  2101. return NL_SKIP;
  2102. nla_for_each_nested(comb, attr[NL80211_ATTR_INTERFACE_COMBINATIONS], comb_rem)
  2103. {
  2104. static struct nla_policy iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
  2105. [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
  2106. [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
  2107. };
  2108. struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB+1];
  2109. static struct nla_policy iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
  2110. [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
  2111. [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
  2112. };
  2113. struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT+1];
  2114. struct nlattr *limit;
  2115. nla_parse_nested(tb_comb, NUM_NL80211_IFACE_COMB, comb, iface_combination_policy);
  2116. if (!tb_comb[NL80211_IFACE_COMB_LIMITS])
  2117. continue;
  2118. nla_for_each_nested(limit, tb_comb[NL80211_IFACE_COMB_LIMITS], limit_rem)
  2119. {
  2120. struct nlattr *mode;
  2121. nla_parse_nested(tb_limit, NUM_NL80211_IFACE_LIMIT, limit, iface_limit_policy);
  2122. if (!tb_limit[NL80211_IFACE_LIMIT_TYPES] ||
  2123. !tb_limit[NL80211_IFACE_LIMIT_MAX])
  2124. continue;
  2125. if (nla_get_u32(tb_limit[NL80211_IFACE_LIMIT_MAX]) < 2)
  2126. continue;
  2127. nla_for_each_nested(mode, tb_limit[NL80211_IFACE_LIMIT_TYPES], mode_rem) {
  2128. if (nla_type(mode) == NL80211_IFTYPE_AP)
  2129. *ret = 1;
  2130. }
  2131. }
  2132. }
  2133. return NL_SKIP;
  2134. }
  2135. static int nl80211_get_mbssid_support(const char *ifname, int *buf)
  2136. {
  2137. if (nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
  2138. nl80211_get_ifcomb_cb, buf))
  2139. return -1;
  2140. return 0;
  2141. }
  2142. static int nl80211_get_hardware_id(const char *ifname, char *buf)
  2143. {
  2144. int rv = -1;
  2145. char *res;
  2146. /* Got a radioX pseudo interface, find some interface on it or create one */
  2147. if (!strncmp(ifname, "radio", 5))
  2148. {
  2149. /* Reuse existing interface */
  2150. if ((res = nl80211_phy2ifname(ifname)) != NULL)
  2151. {
  2152. rv = wext_ops.hardware_id(res, buf);
  2153. }
  2154. /* Need to spawn a temporary iface for finding IDs */
  2155. else if ((res = nl80211_ifadd(ifname)) != NULL)
  2156. {
  2157. rv = wext_ops.hardware_id(res, buf);
  2158. nl80211_ifdel(res);
  2159. }
  2160. }
  2161. else
  2162. {
  2163. rv = wext_ops.hardware_id(ifname, buf);
  2164. }
  2165. /* Failed to obtain hardware IDs, search board config */
  2166. if (rv)
  2167. {
  2168. rv = iwinfo_hardware_id_from_mtd((struct iwinfo_hardware_id *)buf);
  2169. }
  2170. return rv;
  2171. }
  2172. static const struct iwinfo_hardware_entry *
  2173. nl80211_get_hardware_entry(const char *ifname)
  2174. {
  2175. struct iwinfo_hardware_id id;
  2176. if (nl80211_get_hardware_id(ifname, (char *)&id))
  2177. return NULL;
  2178. return iwinfo_hardware(&id);
  2179. }
  2180. static int nl80211_get_hardware_name(const char *ifname, char *buf)
  2181. {
  2182. const struct iwinfo_hardware_entry *hw;
  2183. if (!(hw = nl80211_get_hardware_entry(ifname)))
  2184. sprintf(buf, "Generic MAC80211");
  2185. else
  2186. sprintf(buf, "%s %s", hw->vendor_name, hw->device_name);
  2187. return 0;
  2188. }
  2189. static int nl80211_get_txpower_offset(const char *ifname, int *buf)
  2190. {
  2191. const struct iwinfo_hardware_entry *hw;
  2192. if (!(hw = nl80211_get_hardware_entry(ifname)))
  2193. return -1;
  2194. *buf = hw->txpower_offset;
  2195. return 0;
  2196. }
  2197. static int nl80211_get_frequency_offset(const char *ifname, int *buf)
  2198. {
  2199. const struct iwinfo_hardware_entry *hw;
  2200. if (!(hw = nl80211_get_hardware_entry(ifname)))
  2201. return -1;
  2202. *buf = hw->frequency_offset;
  2203. return 0;
  2204. }
  2205. static int nl80211_lookup_phyname(const char *section, char *buf)
  2206. {
  2207. int idx;
  2208. if ((idx = nl80211_phy_idx_from_uci(section)) < 0)
  2209. return -1;
  2210. sprintf(buf, "phy%d", idx);
  2211. return 0;
  2212. }
  2213. const struct iwinfo_ops nl80211_ops = {
  2214. .name = "nl80211",
  2215. .probe = nl80211_probe,
  2216. .channel = nl80211_get_channel,
  2217. .frequency = nl80211_get_frequency,
  2218. .frequency_offset = nl80211_get_frequency_offset,
  2219. .txpower = nl80211_get_txpower,
  2220. .txpower_offset = nl80211_get_txpower_offset,
  2221. .bitrate = nl80211_get_bitrate,
  2222. .signal = nl80211_get_signal,
  2223. .noise = nl80211_get_noise,
  2224. .quality = nl80211_get_quality,
  2225. .quality_max = nl80211_get_quality_max,
  2226. .mbssid_support = nl80211_get_mbssid_support,
  2227. .hwmodelist = nl80211_get_hwmodelist,
  2228. .htmodelist = nl80211_get_htmodelist,
  2229. .mode = nl80211_get_mode,
  2230. .ssid = nl80211_get_ssid,
  2231. .bssid = nl80211_get_bssid,
  2232. .country = nl80211_get_country,
  2233. .hardware_id = nl80211_get_hardware_id,
  2234. .hardware_name = nl80211_get_hardware_name,
  2235. .encryption = nl80211_get_encryption,
  2236. .phyname = nl80211_get_phyname,
  2237. .assoclist = nl80211_get_assoclist,
  2238. .txpwrlist = nl80211_get_txpwrlist,
  2239. .scanlist = nl80211_get_scanlist,
  2240. .freqlist = nl80211_get_freqlist,
  2241. .countrylist = nl80211_get_countrylist,
  2242. .lookup_phy = nl80211_lookup_phyname,
  2243. .close = nl80211_close
  2244. };