ctrl_iface.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032
  1. /*
  2. * WPA Supplicant / Control interface (shared code for all backends)
  3. * Copyright (c) 2004-2010, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "utils/includes.h"
  15. #include "utils/common.h"
  16. #include "utils/eloop.h"
  17. #include "common/ieee802_11_defs.h"
  18. #include "common/wpa_ctrl.h"
  19. #include "eap_peer/eap.h"
  20. #include "eapol_supp/eapol_supp_sm.h"
  21. #include "rsn_supp/wpa.h"
  22. #include "rsn_supp/preauth.h"
  23. #include "rsn_supp/pmksa_cache.h"
  24. #include "l2_packet/l2_packet.h"
  25. #include "wps/wps.h"
  26. #include "config.h"
  27. #include "wpa_supplicant_i.h"
  28. #include "driver_i.h"
  29. #include "wps_supplicant.h"
  30. #include "ibss_rsn.h"
  31. #include "ap.h"
  32. #include "p2p_supplicant.h"
  33. #include "p2p/p2p.h"
  34. #include "notify.h"
  35. #include "bss.h"
  36. #include "scan.h"
  37. #include "ctrl_iface.h"
  38. extern struct wpa_driver_ops *wpa_drivers[];
  39. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  40. char *buf, int len);
  41. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  42. char *buf, int len);
  43. static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
  44. char *cmd)
  45. {
  46. char *value;
  47. int ret = 0;
  48. value = os_strchr(cmd, ' ');
  49. if (value == NULL)
  50. return -1;
  51. *value++ = '\0';
  52. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  53. if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
  54. eapol_sm_configure(wpa_s->eapol,
  55. atoi(value), -1, -1, -1);
  56. } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
  57. eapol_sm_configure(wpa_s->eapol,
  58. -1, atoi(value), -1, -1);
  59. } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
  60. eapol_sm_configure(wpa_s->eapol,
  61. -1, -1, atoi(value), -1);
  62. } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
  63. eapol_sm_configure(wpa_s->eapol,
  64. -1, -1, -1, atoi(value));
  65. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
  66. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  67. atoi(value)))
  68. ret = -1;
  69. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
  70. 0) {
  71. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  72. atoi(value)))
  73. ret = -1;
  74. } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
  75. if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
  76. ret = -1;
  77. } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
  78. wpa_s->wps_fragment_size = atoi(value);
  79. } else {
  80. value[-1] = '=';
  81. ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
  82. if (ret == 0)
  83. wpa_supplicant_update_config(wpa_s);
  84. }
  85. return ret;
  86. }
  87. #ifdef IEEE8021X_EAPOL
  88. static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
  89. char *addr)
  90. {
  91. u8 bssid[ETH_ALEN];
  92. struct wpa_ssid *ssid = wpa_s->current_ssid;
  93. if (hwaddr_aton(addr, bssid)) {
  94. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
  95. "'%s'", addr);
  96. return -1;
  97. }
  98. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
  99. rsn_preauth_deinit(wpa_s->wpa);
  100. if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
  101. return -1;
  102. return 0;
  103. }
  104. #endif /* IEEE8021X_EAPOL */
  105. #ifdef CONFIG_PEERKEY
  106. /* MLME-STKSTART.request(peer) */
  107. static int wpa_supplicant_ctrl_iface_stkstart(
  108. struct wpa_supplicant *wpa_s, char *addr)
  109. {
  110. u8 peer[ETH_ALEN];
  111. if (hwaddr_aton(addr, peer)) {
  112. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
  113. "address '%s'", addr);
  114. return -1;
  115. }
  116. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
  117. MAC2STR(peer));
  118. return wpa_sm_stkstart(wpa_s->wpa, peer);
  119. }
  120. #endif /* CONFIG_PEERKEY */
  121. #ifdef CONFIG_IEEE80211R
  122. static int wpa_supplicant_ctrl_iface_ft_ds(
  123. struct wpa_supplicant *wpa_s, char *addr)
  124. {
  125. u8 target_ap[ETH_ALEN];
  126. struct wpa_bss *bss;
  127. const u8 *mdie;
  128. if (hwaddr_aton(addr, target_ap)) {
  129. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
  130. "address '%s'", addr);
  131. return -1;
  132. }
  133. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
  134. bss = wpa_bss_get_bssid(wpa_s, target_ap);
  135. if (bss)
  136. mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  137. else
  138. mdie = NULL;
  139. return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
  140. }
  141. #endif /* CONFIG_IEEE80211R */
  142. #ifdef CONFIG_WPS
  143. static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
  144. char *cmd)
  145. {
  146. u8 bssid[ETH_ALEN], *_bssid = bssid;
  147. if (cmd == NULL || os_strcmp(cmd, "any") == 0)
  148. _bssid = NULL;
  149. else if (hwaddr_aton(cmd, bssid)) {
  150. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
  151. cmd);
  152. return -1;
  153. }
  154. #ifdef CONFIG_AP
  155. if (wpa_s->ap_iface)
  156. return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid);
  157. #endif /* CONFIG_AP */
  158. return wpas_wps_start_pbc(wpa_s, _bssid, 0);
  159. }
  160. static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
  161. char *cmd, char *buf,
  162. size_t buflen)
  163. {
  164. u8 bssid[ETH_ALEN], *_bssid = bssid;
  165. char *pin;
  166. int ret;
  167. pin = os_strchr(cmd, ' ');
  168. if (pin)
  169. *pin++ = '\0';
  170. if (os_strcmp(cmd, "any") == 0)
  171. _bssid = NULL;
  172. else if (hwaddr_aton(cmd, bssid)) {
  173. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
  174. cmd);
  175. return -1;
  176. }
  177. #ifdef CONFIG_AP
  178. if (wpa_s->ap_iface)
  179. return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
  180. buf, buflen);
  181. #endif /* CONFIG_AP */
  182. if (pin) {
  183. ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0);
  184. if (ret < 0)
  185. return -1;
  186. ret = os_snprintf(buf, buflen, "%s", pin);
  187. if (ret < 0 || (size_t) ret >= buflen)
  188. return -1;
  189. return ret;
  190. }
  191. ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0);
  192. if (ret < 0)
  193. return -1;
  194. /* Return the generated PIN */
  195. ret = os_snprintf(buf, buflen, "%08d", ret);
  196. if (ret < 0 || (size_t) ret >= buflen)
  197. return -1;
  198. return ret;
  199. }
  200. #ifdef CONFIG_WPS_OOB
  201. static int wpa_supplicant_ctrl_iface_wps_oob(struct wpa_supplicant *wpa_s,
  202. char *cmd)
  203. {
  204. char *path, *method, *name;
  205. path = os_strchr(cmd, ' ');
  206. if (path == NULL)
  207. return -1;
  208. *path++ = '\0';
  209. method = os_strchr(path, ' ');
  210. if (method == NULL)
  211. return -1;
  212. *method++ = '\0';
  213. name = os_strchr(method, ' ');
  214. if (name != NULL)
  215. *name++ = '\0';
  216. return wpas_wps_start_oob(wpa_s, cmd, path, method, name);
  217. }
  218. #endif /* CONFIG_WPS_OOB */
  219. static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
  220. char *cmd)
  221. {
  222. u8 bssid[ETH_ALEN], *_bssid = bssid;
  223. char *pin;
  224. char *new_ssid;
  225. char *new_auth;
  226. char *new_encr;
  227. char *new_key;
  228. struct wps_new_ap_settings ap;
  229. pin = os_strchr(cmd, ' ');
  230. if (pin == NULL)
  231. return -1;
  232. *pin++ = '\0';
  233. if (os_strcmp(cmd, "any") == 0)
  234. _bssid = NULL;
  235. else if (hwaddr_aton(cmd, bssid)) {
  236. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
  237. cmd);
  238. return -1;
  239. }
  240. new_ssid = os_strchr(pin, ' ');
  241. if (new_ssid == NULL)
  242. return wpas_wps_start_reg(wpa_s, _bssid, pin, NULL);
  243. *new_ssid++ = '\0';
  244. new_auth = os_strchr(new_ssid, ' ');
  245. if (new_auth == NULL)
  246. return -1;
  247. *new_auth++ = '\0';
  248. new_encr = os_strchr(new_auth, ' ');
  249. if (new_encr == NULL)
  250. return -1;
  251. *new_encr++ = '\0';
  252. new_key = os_strchr(new_encr, ' ');
  253. if (new_key == NULL)
  254. return -1;
  255. *new_key++ = '\0';
  256. os_memset(&ap, 0, sizeof(ap));
  257. ap.ssid_hex = new_ssid;
  258. ap.auth = new_auth;
  259. ap.encr = new_encr;
  260. ap.key_hex = new_key;
  261. return wpas_wps_start_reg(wpa_s, _bssid, pin, &ap);
  262. }
  263. #ifdef CONFIG_WPS_ER
  264. static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
  265. char *cmd)
  266. {
  267. char *uuid = cmd, *pin, *pos;
  268. u8 addr_buf[ETH_ALEN], *addr = NULL;
  269. pin = os_strchr(uuid, ' ');
  270. if (pin == NULL)
  271. return -1;
  272. *pin++ = '\0';
  273. pos = os_strchr(pin, ' ');
  274. if (pos) {
  275. *pos++ = '\0';
  276. if (hwaddr_aton(pos, addr_buf) == 0)
  277. addr = addr_buf;
  278. }
  279. return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
  280. }
  281. static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
  282. char *cmd)
  283. {
  284. char *uuid = cmd, *pin;
  285. pin = os_strchr(uuid, ' ');
  286. if (pin == NULL)
  287. return -1;
  288. *pin++ = '\0';
  289. return wpas_wps_er_learn(wpa_s, uuid, pin);
  290. }
  291. static int wpa_supplicant_ctrl_iface_wps_er_config(
  292. struct wpa_supplicant *wpa_s, char *cmd)
  293. {
  294. char *pin;
  295. char *new_ssid;
  296. char *new_auth;
  297. char *new_encr;
  298. char *new_key;
  299. struct wps_new_ap_settings ap;
  300. pin = os_strchr(cmd, ' ');
  301. if (pin == NULL)
  302. return -1;
  303. *pin++ = '\0';
  304. new_ssid = os_strchr(pin, ' ');
  305. if (new_ssid == NULL)
  306. return -1;
  307. *new_ssid++ = '\0';
  308. new_auth = os_strchr(new_ssid, ' ');
  309. if (new_auth == NULL)
  310. return -1;
  311. *new_auth++ = '\0';
  312. new_encr = os_strchr(new_auth, ' ');
  313. if (new_encr == NULL)
  314. return -1;
  315. *new_encr++ = '\0';
  316. new_key = os_strchr(new_encr, ' ');
  317. if (new_key == NULL)
  318. return -1;
  319. *new_key++ = '\0';
  320. os_memset(&ap, 0, sizeof(ap));
  321. ap.ssid_hex = new_ssid;
  322. ap.auth = new_auth;
  323. ap.encr = new_encr;
  324. ap.key_hex = new_key;
  325. return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
  326. }
  327. #endif /* CONFIG_WPS_ER */
  328. #endif /* CONFIG_WPS */
  329. #ifdef CONFIG_IBSS_RSN
  330. static int wpa_supplicant_ctrl_iface_ibss_rsn(
  331. struct wpa_supplicant *wpa_s, char *addr)
  332. {
  333. u8 peer[ETH_ALEN];
  334. if (hwaddr_aton(addr, peer)) {
  335. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
  336. "address '%s'", addr);
  337. return -1;
  338. }
  339. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
  340. MAC2STR(peer));
  341. return ibss_rsn_start(wpa_s->ibss_rsn, peer);
  342. }
  343. #endif /* CONFIG_IBSS_RSN */
  344. static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
  345. char *rsp)
  346. {
  347. #ifdef IEEE8021X_EAPOL
  348. char *pos, *id_pos;
  349. int id;
  350. struct wpa_ssid *ssid;
  351. struct eap_peer_config *eap;
  352. pos = os_strchr(rsp, '-');
  353. if (pos == NULL)
  354. return -1;
  355. *pos++ = '\0';
  356. id_pos = pos;
  357. pos = os_strchr(pos, ':');
  358. if (pos == NULL)
  359. return -1;
  360. *pos++ = '\0';
  361. id = atoi(id_pos);
  362. wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
  363. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  364. (u8 *) pos, os_strlen(pos));
  365. ssid = wpa_config_get_network(wpa_s->conf, id);
  366. if (ssid == NULL) {
  367. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  368. "to update", id);
  369. return -1;
  370. }
  371. eap = &ssid->eap;
  372. if (os_strcmp(rsp, "IDENTITY") == 0) {
  373. os_free(eap->identity);
  374. eap->identity = (u8 *) os_strdup(pos);
  375. eap->identity_len = os_strlen(pos);
  376. eap->pending_req_identity = 0;
  377. if (ssid == wpa_s->current_ssid)
  378. wpa_s->reassociate = 1;
  379. } else if (os_strcmp(rsp, "PASSWORD") == 0) {
  380. os_free(eap->password);
  381. eap->password = (u8 *) os_strdup(pos);
  382. eap->password_len = os_strlen(pos);
  383. eap->pending_req_password = 0;
  384. if (ssid == wpa_s->current_ssid)
  385. wpa_s->reassociate = 1;
  386. } else if (os_strcmp(rsp, "NEW_PASSWORD") == 0) {
  387. os_free(eap->new_password);
  388. eap->new_password = (u8 *) os_strdup(pos);
  389. eap->new_password_len = os_strlen(pos);
  390. eap->pending_req_new_password = 0;
  391. if (ssid == wpa_s->current_ssid)
  392. wpa_s->reassociate = 1;
  393. } else if (os_strcmp(rsp, "PIN") == 0) {
  394. os_free(eap->pin);
  395. eap->pin = os_strdup(pos);
  396. eap->pending_req_pin = 0;
  397. if (ssid == wpa_s->current_ssid)
  398. wpa_s->reassociate = 1;
  399. } else if (os_strcmp(rsp, "OTP") == 0) {
  400. os_free(eap->otp);
  401. eap->otp = (u8 *) os_strdup(pos);
  402. eap->otp_len = os_strlen(pos);
  403. os_free(eap->pending_req_otp);
  404. eap->pending_req_otp = NULL;
  405. eap->pending_req_otp_len = 0;
  406. } else if (os_strcmp(rsp, "PASSPHRASE") == 0) {
  407. os_free(eap->private_key_passwd);
  408. eap->private_key_passwd = (u8 *) os_strdup(pos);
  409. eap->pending_req_passphrase = 0;
  410. if (ssid == wpa_s->current_ssid)
  411. wpa_s->reassociate = 1;
  412. } else {
  413. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", rsp);
  414. return -1;
  415. }
  416. return 0;
  417. #else /* IEEE8021X_EAPOL */
  418. wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
  419. return -1;
  420. #endif /* IEEE8021X_EAPOL */
  421. }
  422. static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
  423. const char *params,
  424. char *buf, size_t buflen)
  425. {
  426. char *pos, *end, tmp[30];
  427. int res, verbose, ret;
  428. verbose = os_strcmp(params, "-VERBOSE") == 0;
  429. pos = buf;
  430. end = buf + buflen;
  431. if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  432. struct wpa_ssid *ssid = wpa_s->current_ssid;
  433. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  434. MAC2STR(wpa_s->bssid));
  435. if (ret < 0 || ret >= end - pos)
  436. return pos - buf;
  437. pos += ret;
  438. if (ssid) {
  439. u8 *_ssid = ssid->ssid;
  440. size_t ssid_len = ssid->ssid_len;
  441. u8 ssid_buf[MAX_SSID_LEN];
  442. if (ssid_len == 0) {
  443. int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
  444. if (_res < 0)
  445. ssid_len = 0;
  446. else
  447. ssid_len = _res;
  448. _ssid = ssid_buf;
  449. }
  450. ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
  451. wpa_ssid_txt(_ssid, ssid_len),
  452. ssid->id);
  453. if (ret < 0 || ret >= end - pos)
  454. return pos - buf;
  455. pos += ret;
  456. if (ssid->id_str) {
  457. ret = os_snprintf(pos, end - pos,
  458. "id_str=%s\n",
  459. ssid->id_str);
  460. if (ret < 0 || ret >= end - pos)
  461. return pos - buf;
  462. pos += ret;
  463. }
  464. switch (ssid->mode) {
  465. case WPAS_MODE_INFRA:
  466. ret = os_snprintf(pos, end - pos,
  467. "mode=station\n");
  468. break;
  469. case WPAS_MODE_IBSS:
  470. ret = os_snprintf(pos, end - pos,
  471. "mode=IBSS\n");
  472. break;
  473. case WPAS_MODE_AP:
  474. ret = os_snprintf(pos, end - pos,
  475. "mode=AP\n");
  476. break;
  477. case WPAS_MODE_P2P_GO:
  478. ret = os_snprintf(pos, end - pos,
  479. "mode=P2P GO\n");
  480. break;
  481. case WPAS_MODE_P2P_GROUP_FORMATION:
  482. ret = os_snprintf(pos, end - pos,
  483. "mode=P2P GO - group "
  484. "formation\n");
  485. break;
  486. default:
  487. ret = 0;
  488. break;
  489. }
  490. if (ret < 0 || ret >= end - pos)
  491. return pos - buf;
  492. pos += ret;
  493. }
  494. #ifdef CONFIG_AP
  495. if (wpa_s->ap_iface) {
  496. pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
  497. end - pos,
  498. verbose);
  499. } else
  500. #endif /* CONFIG_AP */
  501. pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
  502. }
  503. ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
  504. wpa_supplicant_state_txt(wpa_s->wpa_state));
  505. if (ret < 0 || ret >= end - pos)
  506. return pos - buf;
  507. pos += ret;
  508. if (wpa_s->l2 &&
  509. l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
  510. ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
  511. if (ret < 0 || ret >= end - pos)
  512. return pos - buf;
  513. pos += ret;
  514. }
  515. #ifdef CONFIG_P2P
  516. if (wpa_s->global->p2p) {
  517. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  518. "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
  519. if (ret < 0 || ret >= end - pos)
  520. return pos - buf;
  521. pos += ret;
  522. }
  523. #endif /* CONFIG_P2P */
  524. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  525. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  526. res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
  527. verbose);
  528. if (res >= 0)
  529. pos += res;
  530. }
  531. res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
  532. if (res >= 0)
  533. pos += res;
  534. return pos - buf;
  535. }
  536. static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
  537. char *cmd)
  538. {
  539. char *pos;
  540. int id;
  541. struct wpa_ssid *ssid;
  542. u8 bssid[ETH_ALEN];
  543. /* cmd: "<network id> <BSSID>" */
  544. pos = os_strchr(cmd, ' ');
  545. if (pos == NULL)
  546. return -1;
  547. *pos++ = '\0';
  548. id = atoi(cmd);
  549. wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
  550. if (hwaddr_aton(pos, bssid)) {
  551. wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
  552. return -1;
  553. }
  554. ssid = wpa_config_get_network(wpa_s->conf, id);
  555. if (ssid == NULL) {
  556. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  557. "to update", id);
  558. return -1;
  559. }
  560. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  561. ssid->bssid_set = !is_zero_ether_addr(bssid);
  562. return 0;
  563. }
  564. static int wpa_supplicant_ctrl_iface_list_networks(
  565. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  566. {
  567. char *pos, *end;
  568. struct wpa_ssid *ssid;
  569. int ret;
  570. pos = buf;
  571. end = buf + buflen;
  572. ret = os_snprintf(pos, end - pos,
  573. "network id / ssid / bssid / flags\n");
  574. if (ret < 0 || ret >= end - pos)
  575. return pos - buf;
  576. pos += ret;
  577. ssid = wpa_s->conf->ssid;
  578. while (ssid) {
  579. ret = os_snprintf(pos, end - pos, "%d\t%s",
  580. ssid->id,
  581. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  582. if (ret < 0 || ret >= end - pos)
  583. return pos - buf;
  584. pos += ret;
  585. if (ssid->bssid_set) {
  586. ret = os_snprintf(pos, end - pos, "\t" MACSTR,
  587. MAC2STR(ssid->bssid));
  588. } else {
  589. ret = os_snprintf(pos, end - pos, "\tany");
  590. }
  591. if (ret < 0 || ret >= end - pos)
  592. return pos - buf;
  593. pos += ret;
  594. ret = os_snprintf(pos, end - pos, "\t%s%s%s",
  595. ssid == wpa_s->current_ssid ?
  596. "[CURRENT]" : "",
  597. ssid->disabled ? "[DISABLED]" : "",
  598. ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
  599. "");
  600. if (ret < 0 || ret >= end - pos)
  601. return pos - buf;
  602. pos += ret;
  603. ret = os_snprintf(pos, end - pos, "\n");
  604. if (ret < 0 || ret >= end - pos)
  605. return pos - buf;
  606. pos += ret;
  607. ssid = ssid->next;
  608. }
  609. return pos - buf;
  610. }
  611. static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
  612. {
  613. int first = 1, ret;
  614. ret = os_snprintf(pos, end - pos, "-");
  615. if (ret < 0 || ret >= end - pos)
  616. return pos;
  617. pos += ret;
  618. if (cipher & WPA_CIPHER_NONE) {
  619. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : "+");
  620. if (ret < 0 || ret >= end - pos)
  621. return pos;
  622. pos += ret;
  623. first = 0;
  624. }
  625. if (cipher & WPA_CIPHER_WEP40) {
  626. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : "+");
  627. if (ret < 0 || ret >= end - pos)
  628. return pos;
  629. pos += ret;
  630. first = 0;
  631. }
  632. if (cipher & WPA_CIPHER_WEP104) {
  633. ret = os_snprintf(pos, end - pos, "%sWEP104",
  634. first ? "" : "+");
  635. if (ret < 0 || ret >= end - pos)
  636. return pos;
  637. pos += ret;
  638. first = 0;
  639. }
  640. if (cipher & WPA_CIPHER_TKIP) {
  641. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : "+");
  642. if (ret < 0 || ret >= end - pos)
  643. return pos;
  644. pos += ret;
  645. first = 0;
  646. }
  647. if (cipher & WPA_CIPHER_CCMP) {
  648. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : "+");
  649. if (ret < 0 || ret >= end - pos)
  650. return pos;
  651. pos += ret;
  652. first = 0;
  653. }
  654. return pos;
  655. }
  656. static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
  657. const u8 *ie, size_t ie_len)
  658. {
  659. struct wpa_ie_data data;
  660. int first, ret;
  661. ret = os_snprintf(pos, end - pos, "[%s-", proto);
  662. if (ret < 0 || ret >= end - pos)
  663. return pos;
  664. pos += ret;
  665. if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
  666. ret = os_snprintf(pos, end - pos, "?]");
  667. if (ret < 0 || ret >= end - pos)
  668. return pos;
  669. pos += ret;
  670. return pos;
  671. }
  672. first = 1;
  673. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  674. ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
  675. if (ret < 0 || ret >= end - pos)
  676. return pos;
  677. pos += ret;
  678. first = 0;
  679. }
  680. if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
  681. ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
  682. if (ret < 0 || ret >= end - pos)
  683. return pos;
  684. pos += ret;
  685. first = 0;
  686. }
  687. if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  688. ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
  689. if (ret < 0 || ret >= end - pos)
  690. return pos;
  691. pos += ret;
  692. first = 0;
  693. }
  694. #ifdef CONFIG_IEEE80211R
  695. if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  696. ret = os_snprintf(pos, end - pos, "%sFT/EAP",
  697. first ? "" : "+");
  698. if (ret < 0 || ret >= end - pos)
  699. return pos;
  700. pos += ret;
  701. first = 0;
  702. }
  703. if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  704. ret = os_snprintf(pos, end - pos, "%sFT/PSK",
  705. first ? "" : "+");
  706. if (ret < 0 || ret >= end - pos)
  707. return pos;
  708. pos += ret;
  709. first = 0;
  710. }
  711. #endif /* CONFIG_IEEE80211R */
  712. #ifdef CONFIG_IEEE80211W
  713. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  714. ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
  715. first ? "" : "+");
  716. if (ret < 0 || ret >= end - pos)
  717. return pos;
  718. pos += ret;
  719. first = 0;
  720. }
  721. if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  722. ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
  723. first ? "" : "+");
  724. if (ret < 0 || ret >= end - pos)
  725. return pos;
  726. pos += ret;
  727. first = 0;
  728. }
  729. #endif /* CONFIG_IEEE80211W */
  730. pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
  731. if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
  732. ret = os_snprintf(pos, end - pos, "-preauth");
  733. if (ret < 0 || ret >= end - pos)
  734. return pos;
  735. pos += ret;
  736. }
  737. ret = os_snprintf(pos, end - pos, "]");
  738. if (ret < 0 || ret >= end - pos)
  739. return pos;
  740. pos += ret;
  741. return pos;
  742. }
  743. #ifdef CONFIG_WPS
  744. static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
  745. char *pos, char *end,
  746. struct wpabuf *wps_ie)
  747. {
  748. int ret;
  749. const char *txt;
  750. if (wps_ie == NULL)
  751. return pos;
  752. if (wps_is_selected_pbc_registrar(wps_ie))
  753. txt = "[WPS-PBC]";
  754. #ifdef CONFIG_WPS2
  755. else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
  756. txt = "[WPS-AUTH]";
  757. #endif /* CONFIG_WPS2 */
  758. else if (wps_is_selected_pin_registrar(wps_ie))
  759. txt = "[WPS-PIN]";
  760. else
  761. txt = "[WPS]";
  762. ret = os_snprintf(pos, end - pos, "%s", txt);
  763. if (ret >= 0 && ret < end - pos)
  764. pos += ret;
  765. wpabuf_free(wps_ie);
  766. return pos;
  767. }
  768. #endif /* CONFIG_WPS */
  769. static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
  770. char *pos, char *end,
  771. const struct wpa_bss *bss)
  772. {
  773. #ifdef CONFIG_WPS
  774. struct wpabuf *wps_ie;
  775. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  776. return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
  777. #else /* CONFIG_WPS */
  778. return pos;
  779. #endif /* CONFIG_WPS */
  780. }
  781. /* Format one result on one text line into a buffer. */
  782. static int wpa_supplicant_ctrl_iface_scan_result(
  783. struct wpa_supplicant *wpa_s,
  784. const struct wpa_bss *bss, char *buf, size_t buflen)
  785. {
  786. char *pos, *end;
  787. int ret;
  788. const u8 *ie, *ie2;
  789. pos = buf;
  790. end = buf + buflen;
  791. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  792. MAC2STR(bss->bssid), bss->freq, bss->level);
  793. if (ret < 0 || ret >= end - pos)
  794. return pos - buf;
  795. pos += ret;
  796. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  797. if (ie)
  798. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  799. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  800. if (ie2)
  801. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  802. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  803. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  804. ret = os_snprintf(pos, end - pos, "[WEP]");
  805. if (ret < 0 || ret >= end - pos)
  806. return pos - buf;
  807. pos += ret;
  808. }
  809. if (bss->caps & IEEE80211_CAP_IBSS) {
  810. ret = os_snprintf(pos, end - pos, "[IBSS]");
  811. if (ret < 0 || ret >= end - pos)
  812. return pos - buf;
  813. pos += ret;
  814. }
  815. if (bss->caps & IEEE80211_CAP_ESS) {
  816. ret = os_snprintf(pos, end - pos, "[ESS]");
  817. if (ret < 0 || ret >= end - pos)
  818. return pos - buf;
  819. pos += ret;
  820. }
  821. ret = os_snprintf(pos, end - pos, "\t%s",
  822. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  823. if (ret < 0 || ret >= end - pos)
  824. return pos - buf;
  825. pos += ret;
  826. ret = os_snprintf(pos, end - pos, "\n");
  827. if (ret < 0 || ret >= end - pos)
  828. return pos - buf;
  829. pos += ret;
  830. return pos - buf;
  831. }
  832. static int wpa_supplicant_ctrl_iface_scan_results(
  833. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  834. {
  835. char *pos, *end;
  836. struct wpa_bss *bss;
  837. int ret;
  838. pos = buf;
  839. end = buf + buflen;
  840. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  841. "flags / ssid\n");
  842. if (ret < 0 || ret >= end - pos)
  843. return pos - buf;
  844. pos += ret;
  845. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  846. ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
  847. end - pos);
  848. if (ret < 0 || ret >= end - pos)
  849. return pos - buf;
  850. pos += ret;
  851. }
  852. return pos - buf;
  853. }
  854. static int wpa_supplicant_ctrl_iface_select_network(
  855. struct wpa_supplicant *wpa_s, char *cmd)
  856. {
  857. int id;
  858. struct wpa_ssid *ssid;
  859. /* cmd: "<network id>" or "any" */
  860. if (os_strcmp(cmd, "any") == 0) {
  861. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  862. ssid = NULL;
  863. } else {
  864. id = atoi(cmd);
  865. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  866. ssid = wpa_config_get_network(wpa_s->conf, id);
  867. if (ssid == NULL) {
  868. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  869. "network id=%d", id);
  870. return -1;
  871. }
  872. if (ssid->disabled == 2) {
  873. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  874. "SELECT_NETWORK with persistent P2P group");
  875. return -1;
  876. }
  877. }
  878. wpa_supplicant_select_network(wpa_s, ssid);
  879. return 0;
  880. }
  881. static int wpa_supplicant_ctrl_iface_enable_network(
  882. struct wpa_supplicant *wpa_s, char *cmd)
  883. {
  884. int id;
  885. struct wpa_ssid *ssid;
  886. /* cmd: "<network id>" or "all" */
  887. if (os_strcmp(cmd, "all") == 0) {
  888. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  889. ssid = NULL;
  890. } else {
  891. id = atoi(cmd);
  892. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  893. ssid = wpa_config_get_network(wpa_s->conf, id);
  894. if (ssid == NULL) {
  895. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  896. "network id=%d", id);
  897. return -1;
  898. }
  899. if (ssid->disabled == 2) {
  900. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  901. "ENABLE_NETWORK with persistent P2P group");
  902. return -1;
  903. }
  904. }
  905. wpa_supplicant_enable_network(wpa_s, ssid);
  906. return 0;
  907. }
  908. static int wpa_supplicant_ctrl_iface_disable_network(
  909. struct wpa_supplicant *wpa_s, char *cmd)
  910. {
  911. int id;
  912. struct wpa_ssid *ssid;
  913. /* cmd: "<network id>" or "all" */
  914. if (os_strcmp(cmd, "all") == 0) {
  915. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  916. ssid = NULL;
  917. } else {
  918. id = atoi(cmd);
  919. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  920. ssid = wpa_config_get_network(wpa_s->conf, id);
  921. if (ssid == NULL) {
  922. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  923. "network id=%d", id);
  924. return -1;
  925. }
  926. if (ssid->disabled == 2) {
  927. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  928. "DISABLE_NETWORK with persistent P2P "
  929. "group");
  930. return -1;
  931. }
  932. }
  933. wpa_supplicant_disable_network(wpa_s, ssid);
  934. return 0;
  935. }
  936. static int wpa_supplicant_ctrl_iface_add_network(
  937. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  938. {
  939. struct wpa_ssid *ssid;
  940. int ret;
  941. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  942. ssid = wpa_config_add_network(wpa_s->conf);
  943. if (ssid == NULL)
  944. return -1;
  945. wpas_notify_network_added(wpa_s, ssid);
  946. ssid->disabled = 1;
  947. wpa_config_set_network_defaults(ssid);
  948. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  949. if (ret < 0 || (size_t) ret >= buflen)
  950. return -1;
  951. return ret;
  952. }
  953. static int wpa_supplicant_ctrl_iface_remove_network(
  954. struct wpa_supplicant *wpa_s, char *cmd)
  955. {
  956. int id;
  957. struct wpa_ssid *ssid;
  958. /* cmd: "<network id>" or "all" */
  959. if (os_strcmp(cmd, "all") == 0) {
  960. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  961. ssid = wpa_s->conf->ssid;
  962. while (ssid) {
  963. struct wpa_ssid *remove_ssid = ssid;
  964. id = ssid->id;
  965. ssid = ssid->next;
  966. wpas_notify_network_removed(wpa_s, remove_ssid);
  967. wpa_config_remove_network(wpa_s->conf, id);
  968. }
  969. if (wpa_s->current_ssid) {
  970. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  971. wpa_supplicant_disassociate(wpa_s,
  972. WLAN_REASON_DEAUTH_LEAVING);
  973. }
  974. return 0;
  975. }
  976. id = atoi(cmd);
  977. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  978. ssid = wpa_config_get_network(wpa_s->conf, id);
  979. if (ssid == NULL ||
  980. wpa_config_remove_network(wpa_s->conf, id) < 0) {
  981. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  982. "id=%d", id);
  983. return -1;
  984. }
  985. if (ssid == wpa_s->current_ssid) {
  986. /*
  987. * Invalidate the EAP session cache if the current network is
  988. * removed.
  989. */
  990. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  991. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  992. }
  993. return 0;
  994. }
  995. static int wpa_supplicant_ctrl_iface_set_network(
  996. struct wpa_supplicant *wpa_s, char *cmd)
  997. {
  998. int id;
  999. struct wpa_ssid *ssid;
  1000. char *name, *value;
  1001. /* cmd: "<network id> <variable name> <value>" */
  1002. name = os_strchr(cmd, ' ');
  1003. if (name == NULL)
  1004. return -1;
  1005. *name++ = '\0';
  1006. value = os_strchr(name, ' ');
  1007. if (value == NULL)
  1008. return -1;
  1009. *value++ = '\0';
  1010. id = atoi(cmd);
  1011. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  1012. id, name);
  1013. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  1014. (u8 *) value, os_strlen(value));
  1015. ssid = wpa_config_get_network(wpa_s->conf, id);
  1016. if (ssid == NULL) {
  1017. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1018. "id=%d", id);
  1019. return -1;
  1020. }
  1021. if (wpa_config_set(ssid, name, value, 0) < 0) {
  1022. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  1023. "variable '%s'", name);
  1024. return -1;
  1025. }
  1026. if (wpa_s->current_ssid == ssid) {
  1027. /*
  1028. * Invalidate the EAP session cache if anything in the current
  1029. * configuration changes.
  1030. */
  1031. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1032. }
  1033. if ((os_strcmp(name, "psk") == 0 &&
  1034. value[0] == '"' && ssid->ssid_len) ||
  1035. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  1036. wpa_config_update_psk(ssid);
  1037. else if (os_strcmp(name, "priority") == 0)
  1038. wpa_config_update_prio_list(wpa_s->conf);
  1039. return 0;
  1040. }
  1041. static int wpa_supplicant_ctrl_iface_get_network(
  1042. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  1043. {
  1044. int id;
  1045. size_t res;
  1046. struct wpa_ssid *ssid;
  1047. char *name, *value;
  1048. /* cmd: "<network id> <variable name>" */
  1049. name = os_strchr(cmd, ' ');
  1050. if (name == NULL || buflen == 0)
  1051. return -1;
  1052. *name++ = '\0';
  1053. id = atoi(cmd);
  1054. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  1055. id, name);
  1056. ssid = wpa_config_get_network(wpa_s->conf, id);
  1057. if (ssid == NULL) {
  1058. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1059. "id=%d", id);
  1060. return -1;
  1061. }
  1062. value = wpa_config_get_no_key(ssid, name);
  1063. if (value == NULL) {
  1064. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  1065. "variable '%s'", name);
  1066. return -1;
  1067. }
  1068. res = os_strlcpy(buf, value, buflen);
  1069. if (res >= buflen) {
  1070. os_free(value);
  1071. return -1;
  1072. }
  1073. os_free(value);
  1074. return res;
  1075. }
  1076. #ifndef CONFIG_NO_CONFIG_WRITE
  1077. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  1078. {
  1079. int ret;
  1080. if (!wpa_s->conf->update_config) {
  1081. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  1082. "to update configuration (update_config=0)");
  1083. return -1;
  1084. }
  1085. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  1086. if (ret) {
  1087. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  1088. "update configuration");
  1089. } else {
  1090. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  1091. " updated");
  1092. }
  1093. return ret;
  1094. }
  1095. #endif /* CONFIG_NO_CONFIG_WRITE */
  1096. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  1097. struct wpa_driver_capa *capa,
  1098. char *buf, size_t buflen)
  1099. {
  1100. int ret, first = 1;
  1101. char *pos, *end;
  1102. size_t len;
  1103. pos = buf;
  1104. end = pos + buflen;
  1105. if (res < 0) {
  1106. if (strict)
  1107. return 0;
  1108. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  1109. if (len >= buflen)
  1110. return -1;
  1111. return len;
  1112. }
  1113. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  1114. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  1115. if (ret < 0 || ret >= end - pos)
  1116. return pos - buf;
  1117. pos += ret;
  1118. first = 0;
  1119. }
  1120. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  1121. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  1122. if (ret < 0 || ret >= end - pos)
  1123. return pos - buf;
  1124. pos += ret;
  1125. first = 0;
  1126. }
  1127. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  1128. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
  1129. if (ret < 0 || ret >= end - pos)
  1130. return pos - buf;
  1131. pos += ret;
  1132. first = 0;
  1133. }
  1134. return pos - buf;
  1135. }
  1136. static int ctrl_iface_get_capability_group(int res, char *strict,
  1137. struct wpa_driver_capa *capa,
  1138. char *buf, size_t buflen)
  1139. {
  1140. int ret, first = 1;
  1141. char *pos, *end;
  1142. size_t len;
  1143. pos = buf;
  1144. end = pos + buflen;
  1145. if (res < 0) {
  1146. if (strict)
  1147. return 0;
  1148. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  1149. if (len >= buflen)
  1150. return -1;
  1151. return len;
  1152. }
  1153. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  1154. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  1155. if (ret < 0 || ret >= end - pos)
  1156. return pos - buf;
  1157. pos += ret;
  1158. first = 0;
  1159. }
  1160. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  1161. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  1162. if (ret < 0 || ret >= end - pos)
  1163. return pos - buf;
  1164. pos += ret;
  1165. first = 0;
  1166. }
  1167. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
  1168. ret = os_snprintf(pos, end - pos, "%sWEP104",
  1169. first ? "" : " ");
  1170. if (ret < 0 || ret >= end - pos)
  1171. return pos - buf;
  1172. pos += ret;
  1173. first = 0;
  1174. }
  1175. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
  1176. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
  1177. if (ret < 0 || ret >= end - pos)
  1178. return pos - buf;
  1179. pos += ret;
  1180. first = 0;
  1181. }
  1182. return pos - buf;
  1183. }
  1184. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  1185. struct wpa_driver_capa *capa,
  1186. char *buf, size_t buflen)
  1187. {
  1188. int ret;
  1189. char *pos, *end;
  1190. size_t len;
  1191. pos = buf;
  1192. end = pos + buflen;
  1193. if (res < 0) {
  1194. if (strict)
  1195. return 0;
  1196. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  1197. "NONE", buflen);
  1198. if (len >= buflen)
  1199. return -1;
  1200. return len;
  1201. }
  1202. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  1203. if (ret < 0 || ret >= end - pos)
  1204. return pos - buf;
  1205. pos += ret;
  1206. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1207. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  1208. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  1209. if (ret < 0 || ret >= end - pos)
  1210. return pos - buf;
  1211. pos += ret;
  1212. }
  1213. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  1214. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  1215. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  1216. if (ret < 0 || ret >= end - pos)
  1217. return pos - buf;
  1218. pos += ret;
  1219. }
  1220. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  1221. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  1222. if (ret < 0 || ret >= end - pos)
  1223. return pos - buf;
  1224. pos += ret;
  1225. }
  1226. return pos - buf;
  1227. }
  1228. static int ctrl_iface_get_capability_proto(int res, char *strict,
  1229. struct wpa_driver_capa *capa,
  1230. char *buf, size_t buflen)
  1231. {
  1232. int ret, first = 1;
  1233. char *pos, *end;
  1234. size_t len;
  1235. pos = buf;
  1236. end = pos + buflen;
  1237. if (res < 0) {
  1238. if (strict)
  1239. return 0;
  1240. len = os_strlcpy(buf, "RSN WPA", buflen);
  1241. if (len >= buflen)
  1242. return -1;
  1243. return len;
  1244. }
  1245. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  1246. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  1247. ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
  1248. if (ret < 0 || ret >= end - pos)
  1249. return pos - buf;
  1250. pos += ret;
  1251. first = 0;
  1252. }
  1253. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1254. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  1255. ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
  1256. if (ret < 0 || ret >= end - pos)
  1257. return pos - buf;
  1258. pos += ret;
  1259. first = 0;
  1260. }
  1261. return pos - buf;
  1262. }
  1263. static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
  1264. struct wpa_driver_capa *capa,
  1265. char *buf, size_t buflen)
  1266. {
  1267. int ret, first = 1;
  1268. char *pos, *end;
  1269. size_t len;
  1270. pos = buf;
  1271. end = pos + buflen;
  1272. if (res < 0) {
  1273. if (strict)
  1274. return 0;
  1275. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  1276. if (len >= buflen)
  1277. return -1;
  1278. return len;
  1279. }
  1280. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  1281. ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
  1282. if (ret < 0 || ret >= end - pos)
  1283. return pos - buf;
  1284. pos += ret;
  1285. first = 0;
  1286. }
  1287. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  1288. ret = os_snprintf(pos, end - pos, "%sSHARED",
  1289. first ? "" : " ");
  1290. if (ret < 0 || ret >= end - pos)
  1291. return pos - buf;
  1292. pos += ret;
  1293. first = 0;
  1294. }
  1295. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  1296. ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
  1297. if (ret < 0 || ret >= end - pos)
  1298. return pos - buf;
  1299. pos += ret;
  1300. first = 0;
  1301. }
  1302. return pos - buf;
  1303. }
  1304. static int wpa_supplicant_ctrl_iface_get_capability(
  1305. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  1306. size_t buflen)
  1307. {
  1308. struct wpa_driver_capa capa;
  1309. int res;
  1310. char *strict;
  1311. char field[30];
  1312. size_t len;
  1313. /* Determine whether or not strict checking was requested */
  1314. len = os_strlcpy(field, _field, sizeof(field));
  1315. if (len >= sizeof(field))
  1316. return -1;
  1317. strict = os_strchr(field, ' ');
  1318. if (strict != NULL) {
  1319. *strict++ = '\0';
  1320. if (os_strcmp(strict, "strict") != 0)
  1321. return -1;
  1322. }
  1323. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  1324. field, strict ? strict : "");
  1325. if (os_strcmp(field, "eap") == 0) {
  1326. return eap_get_names(buf, buflen);
  1327. }
  1328. res = wpa_drv_get_capa(wpa_s, &capa);
  1329. if (os_strcmp(field, "pairwise") == 0)
  1330. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  1331. buf, buflen);
  1332. if (os_strcmp(field, "group") == 0)
  1333. return ctrl_iface_get_capability_group(res, strict, &capa,
  1334. buf, buflen);
  1335. if (os_strcmp(field, "key_mgmt") == 0)
  1336. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  1337. buf, buflen);
  1338. if (os_strcmp(field, "proto") == 0)
  1339. return ctrl_iface_get_capability_proto(res, strict, &capa,
  1340. buf, buflen);
  1341. if (os_strcmp(field, "auth_alg") == 0)
  1342. return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
  1343. buf, buflen);
  1344. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  1345. field);
  1346. return -1;
  1347. }
  1348. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  1349. const char *cmd, char *buf,
  1350. size_t buflen)
  1351. {
  1352. u8 bssid[ETH_ALEN];
  1353. size_t i;
  1354. struct wpa_bss *bss;
  1355. int ret;
  1356. char *pos, *end;
  1357. const u8 *ie, *ie2;
  1358. if (os_strcmp(cmd, "FIRST") == 0)
  1359. bss = dl_list_first(&wpa_s->bss, struct wpa_bss, list);
  1360. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  1361. i = atoi(cmd + 3);
  1362. bss = wpa_bss_get_id(wpa_s, i);
  1363. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  1364. i = atoi(cmd + 5);
  1365. bss = wpa_bss_get_id(wpa_s, i);
  1366. if (bss) {
  1367. struct dl_list *next = bss->list_id.next;
  1368. if (next == &wpa_s->bss_id)
  1369. bss = NULL;
  1370. else
  1371. bss = dl_list_entry(next, struct wpa_bss,
  1372. list_id);
  1373. }
  1374. } else if (hwaddr_aton(cmd, bssid) == 0)
  1375. bss = wpa_bss_get_bssid(wpa_s, bssid);
  1376. else {
  1377. struct wpa_bss *tmp;
  1378. i = atoi(cmd);
  1379. bss = NULL;
  1380. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  1381. {
  1382. if (i-- == 0) {
  1383. bss = tmp;
  1384. break;
  1385. }
  1386. }
  1387. }
  1388. if (bss == NULL)
  1389. return 0;
  1390. pos = buf;
  1391. end = buf + buflen;
  1392. ret = os_snprintf(pos, end - pos,
  1393. "id=%u\n"
  1394. "bssid=" MACSTR "\n"
  1395. "freq=%d\n"
  1396. "beacon_int=%d\n"
  1397. "capabilities=0x%04x\n"
  1398. "qual=%d\n"
  1399. "noise=%d\n"
  1400. "level=%d\n"
  1401. "tsf=%016llu\n"
  1402. "ie=",
  1403. bss->id,
  1404. MAC2STR(bss->bssid), bss->freq, bss->beacon_int,
  1405. bss->caps, bss->qual, bss->noise, bss->level,
  1406. (unsigned long long) bss->tsf);
  1407. if (ret < 0 || ret >= end - pos)
  1408. return pos - buf;
  1409. pos += ret;
  1410. ie = (const u8 *) (bss + 1);
  1411. for (i = 0; i < bss->ie_len; i++) {
  1412. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  1413. if (ret < 0 || ret >= end - pos)
  1414. return pos - buf;
  1415. pos += ret;
  1416. }
  1417. ret = os_snprintf(pos, end - pos, "\n");
  1418. if (ret < 0 || ret >= end - pos)
  1419. return pos - buf;
  1420. pos += ret;
  1421. ret = os_snprintf(pos, end - pos, "flags=");
  1422. if (ret < 0 || ret >= end - pos)
  1423. return pos - buf;
  1424. pos += ret;
  1425. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  1426. if (ie)
  1427. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  1428. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1429. if (ie2)
  1430. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  1431. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  1432. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  1433. ret = os_snprintf(pos, end - pos, "[WEP]");
  1434. if (ret < 0 || ret >= end - pos)
  1435. return pos - buf;
  1436. pos += ret;
  1437. }
  1438. if (bss->caps & IEEE80211_CAP_IBSS) {
  1439. ret = os_snprintf(pos, end - pos, "[IBSS]");
  1440. if (ret < 0 || ret >= end - pos)
  1441. return pos - buf;
  1442. pos += ret;
  1443. }
  1444. if (bss->caps & IEEE80211_CAP_ESS) {
  1445. ret = os_snprintf(pos, end - pos, "[ESS]");
  1446. if (ret < 0 || ret >= end - pos)
  1447. return pos - buf;
  1448. pos += ret;
  1449. }
  1450. ret = os_snprintf(pos, end - pos, "\n");
  1451. if (ret < 0 || ret >= end - pos)
  1452. return pos - buf;
  1453. pos += ret;
  1454. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  1455. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  1456. if (ret < 0 || ret >= end - pos)
  1457. return pos - buf;
  1458. pos += ret;
  1459. #ifdef CONFIG_WPS
  1460. ie = (const u8 *) (bss + 1);
  1461. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  1462. if (ret < 0 || ret >= end - pos)
  1463. return pos - buf;
  1464. pos += ret;
  1465. #endif /* CONFIG_WPS */
  1466. return pos - buf;
  1467. }
  1468. static int wpa_supplicant_ctrl_iface_ap_scan(
  1469. struct wpa_supplicant *wpa_s, char *cmd)
  1470. {
  1471. int ap_scan = atoi(cmd);
  1472. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  1473. }
  1474. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  1475. {
  1476. u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
  1477. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  1478. /* MLME-DELETEKEYS.request */
  1479. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
  1480. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
  1481. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
  1482. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
  1483. #ifdef CONFIG_IEEE80211W
  1484. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 4, 0, NULL, 0, NULL, 0);
  1485. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 5, 0, NULL, 0, NULL, 0);
  1486. #endif /* CONFIG_IEEE80211W */
  1487. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  1488. 0);
  1489. /* MLME-SETPROTECTION.request(None) */
  1490. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  1491. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  1492. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  1493. wpa_sm_drop_sa(wpa_s->wpa);
  1494. }
  1495. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  1496. char *addr)
  1497. {
  1498. u8 bssid[ETH_ALEN];
  1499. struct wpa_bss *bss;
  1500. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1501. if (hwaddr_aton(addr, bssid)) {
  1502. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  1503. "address '%s'", addr);
  1504. return -1;
  1505. }
  1506. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  1507. bss = wpa_bss_get_bssid(wpa_s, bssid);
  1508. if (!bss) {
  1509. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  1510. "from BSS table");
  1511. return -1;
  1512. }
  1513. /*
  1514. * TODO: Find best network configuration block from configuration to
  1515. * allow roaming to other networks
  1516. */
  1517. if (!ssid) {
  1518. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  1519. "configuration known for the target AP");
  1520. return -1;
  1521. }
  1522. wpa_s->reassociate = 1;
  1523. wpa_supplicant_connect(wpa_s, bss, ssid);
  1524. return 0;
  1525. }
  1526. #ifdef CONFIG_P2P
  1527. static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
  1528. {
  1529. unsigned int timeout = atoi(cmd);
  1530. enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
  1531. if (os_strstr(cmd, "type=social"))
  1532. type = P2P_FIND_ONLY_SOCIAL;
  1533. else if (os_strstr(cmd, "type=progressive"))
  1534. type = P2P_FIND_PROGRESSIVE;
  1535. wpas_p2p_find(wpa_s, timeout, type);
  1536. return 0;
  1537. }
  1538. static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
  1539. char *buf, size_t buflen)
  1540. {
  1541. u8 addr[ETH_ALEN];
  1542. char *pos, *pos2;
  1543. char *pin = NULL;
  1544. enum p2p_wps_method wps_method;
  1545. int new_pin;
  1546. int ret;
  1547. int persistent_group;
  1548. int join;
  1549. int auth;
  1550. int go_intent = -1;
  1551. int freq = 0;
  1552. /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad] [persistent]
  1553. * [join|auth] [go_intent=<0..15>] [freq=<in MHz>] */
  1554. if (hwaddr_aton(cmd, addr))
  1555. return -1;
  1556. pos = cmd + 17;
  1557. if (*pos != ' ')
  1558. return -1;
  1559. pos++;
  1560. persistent_group = os_strstr(pos, " persistent") != NULL;
  1561. join = os_strstr(pos, " join") != NULL;
  1562. auth = os_strstr(pos, " auth") != NULL;
  1563. pos2 = os_strstr(pos, " go_intent=");
  1564. if (pos2) {
  1565. pos2 += 11;
  1566. go_intent = atoi(pos2);
  1567. if (go_intent < 0 || go_intent > 15)
  1568. return -1;
  1569. }
  1570. pos2 = os_strstr(pos, " freq=");
  1571. if (pos2) {
  1572. pos2 += 6;
  1573. freq = atoi(pos2);
  1574. if (freq <= 0)
  1575. return -1;
  1576. }
  1577. if (os_strncmp(pos, "pin", 3) == 0) {
  1578. /* Request random PIN (to be displayed) and enable the PIN */
  1579. wps_method = WPS_PIN_DISPLAY;
  1580. } else if (os_strncmp(pos, "pbc", 3) == 0) {
  1581. wps_method = WPS_PBC;
  1582. } else {
  1583. pin = pos;
  1584. pos = os_strchr(pin, ' ');
  1585. wps_method = WPS_PIN_KEYPAD;
  1586. if (pos) {
  1587. *pos++ = '\0';
  1588. if (os_strncmp(pos, "label", 5) == 0)
  1589. wps_method = WPS_PIN_LABEL;
  1590. else if (os_strncmp(pos, "display", 7) == 0)
  1591. wps_method = WPS_PIN_DISPLAY;
  1592. }
  1593. }
  1594. new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
  1595. persistent_group, join, auth, go_intent,
  1596. freq);
  1597. if (new_pin < 0)
  1598. return -1;
  1599. if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
  1600. ret = os_snprintf(buf, buflen, "%08d", new_pin);
  1601. if (ret < 0 || (size_t) ret >= buflen)
  1602. return -1;
  1603. return ret;
  1604. }
  1605. os_memcpy(buf, "OK\n", 3);
  1606. return 3;
  1607. }
  1608. static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
  1609. {
  1610. unsigned int timeout = atoi(cmd);
  1611. return wpas_p2p_listen(wpa_s, timeout);
  1612. }
  1613. static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
  1614. {
  1615. u8 addr[ETH_ALEN];
  1616. char *pos;
  1617. /* <addr> <config method> */
  1618. if (hwaddr_aton(cmd, addr))
  1619. return -1;
  1620. pos = cmd + 17;
  1621. if (*pos != ' ')
  1622. return -1;
  1623. pos++;
  1624. return wpas_p2p_prov_disc(wpa_s, addr, pos);
  1625. }
  1626. static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
  1627. size_t buflen)
  1628. {
  1629. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1630. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  1631. ssid->passphrase == NULL)
  1632. return -1;
  1633. os_strlcpy(buf, ssid->passphrase, buflen);
  1634. return os_strlen(buf);
  1635. }
  1636. static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
  1637. char *buf, size_t buflen)
  1638. {
  1639. u64 ref;
  1640. int res;
  1641. u8 dst_buf[ETH_ALEN], *dst;
  1642. struct wpabuf *tlvs;
  1643. char *pos;
  1644. size_t len;
  1645. if (hwaddr_aton(cmd, dst_buf))
  1646. return -1;
  1647. dst = dst_buf;
  1648. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  1649. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  1650. dst = NULL;
  1651. pos = cmd + 17;
  1652. if (*pos != ' ')
  1653. return -1;
  1654. pos++;
  1655. if (os_strncmp(pos, "upnp ", 5) == 0) {
  1656. u8 version;
  1657. pos += 5;
  1658. if (hexstr2bin(pos, &version, 1) < 0)
  1659. return -1;
  1660. pos += 2;
  1661. if (*pos != ' ')
  1662. return -1;
  1663. pos++;
  1664. ref = (u64) wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
  1665. } else {
  1666. len = os_strlen(pos);
  1667. if (len & 1)
  1668. return -1;
  1669. len /= 2;
  1670. tlvs = wpabuf_alloc(len);
  1671. if (tlvs == NULL)
  1672. return -1;
  1673. if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
  1674. wpabuf_free(tlvs);
  1675. return -1;
  1676. }
  1677. ref = (u64) wpas_p2p_sd_request(wpa_s, dst, tlvs);
  1678. wpabuf_free(tlvs);
  1679. }
  1680. res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
  1681. if (res < 0 || (unsigned) res >= buflen)
  1682. return -1;
  1683. return res;
  1684. }
  1685. static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
  1686. char *cmd)
  1687. {
  1688. long long unsigned val;
  1689. u64 req;
  1690. if (sscanf(cmd, "%llx", &val) != 1)
  1691. return -1;
  1692. req = val;
  1693. return wpas_p2p_sd_cancel_request(wpa_s, (void *) req);
  1694. }
  1695. static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
  1696. {
  1697. int freq;
  1698. u8 dst_buf[ETH_ALEN], *dst;
  1699. u8 dialog_token;
  1700. struct wpabuf *resp_tlvs;
  1701. char *pos, *pos2;
  1702. size_t len;
  1703. pos = os_strchr(cmd, ' ');
  1704. if (pos == NULL)
  1705. return -1;
  1706. *pos++ = '\0';
  1707. freq = atoi(cmd);
  1708. if (freq == 0)
  1709. return -1;
  1710. if (hwaddr_aton(pos, dst_buf))
  1711. return -1;
  1712. dst = dst_buf;
  1713. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  1714. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  1715. dst = NULL;
  1716. pos += 17;
  1717. if (*pos != ' ')
  1718. return -1;
  1719. pos++;
  1720. pos2 = os_strchr(pos, ' ');
  1721. if (pos2 == NULL)
  1722. return -1;
  1723. *pos2++ = '\0';
  1724. dialog_token = atoi(pos);
  1725. len = os_strlen(pos2);
  1726. if (len & 1)
  1727. return -1;
  1728. len /= 2;
  1729. resp_tlvs = wpabuf_alloc(len);
  1730. if (resp_tlvs == NULL)
  1731. return -1;
  1732. if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
  1733. wpabuf_free(resp_tlvs);
  1734. return -1;
  1735. }
  1736. wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
  1737. wpabuf_free(resp_tlvs);
  1738. return 0;
  1739. }
  1740. static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
  1741. char *cmd)
  1742. {
  1743. wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
  1744. return 0;
  1745. }
  1746. static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
  1747. char *cmd)
  1748. {
  1749. char *pos;
  1750. size_t len;
  1751. struct wpabuf *query, *resp;
  1752. pos = os_strchr(cmd, ' ');
  1753. if (pos == NULL)
  1754. return -1;
  1755. *pos++ = '\0';
  1756. len = os_strlen(cmd);
  1757. if (len & 1)
  1758. return -1;
  1759. len /= 2;
  1760. query = wpabuf_alloc(len);
  1761. if (query == NULL)
  1762. return -1;
  1763. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  1764. wpabuf_free(query);
  1765. return -1;
  1766. }
  1767. len = os_strlen(pos);
  1768. if (len & 1) {
  1769. wpabuf_free(query);
  1770. return -1;
  1771. }
  1772. len /= 2;
  1773. resp = wpabuf_alloc(len);
  1774. if (resp == NULL) {
  1775. wpabuf_free(query);
  1776. return -1;
  1777. }
  1778. if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
  1779. wpabuf_free(query);
  1780. wpabuf_free(resp);
  1781. return -1;
  1782. }
  1783. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
  1784. wpabuf_free(query);
  1785. wpabuf_free(resp);
  1786. return -1;
  1787. }
  1788. return 0;
  1789. }
  1790. static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  1791. {
  1792. char *pos;
  1793. u8 version;
  1794. pos = os_strchr(cmd, ' ');
  1795. if (pos == NULL)
  1796. return -1;
  1797. *pos++ = '\0';
  1798. if (hexstr2bin(cmd, &version, 1) < 0)
  1799. return -1;
  1800. return wpas_p2p_service_add_upnp(wpa_s, version, pos);
  1801. }
  1802. static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
  1803. {
  1804. char *pos;
  1805. pos = os_strchr(cmd, ' ');
  1806. if (pos == NULL)
  1807. return -1;
  1808. *pos++ = '\0';
  1809. if (os_strcmp(cmd, "bonjour") == 0)
  1810. return p2p_ctrl_service_add_bonjour(wpa_s, pos);
  1811. if (os_strcmp(cmd, "upnp") == 0)
  1812. return p2p_ctrl_service_add_upnp(wpa_s, pos);
  1813. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  1814. return -1;
  1815. }
  1816. static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
  1817. char *cmd)
  1818. {
  1819. size_t len;
  1820. struct wpabuf *query;
  1821. int ret;
  1822. len = os_strlen(cmd);
  1823. if (len & 1)
  1824. return -1;
  1825. len /= 2;
  1826. query = wpabuf_alloc(len);
  1827. if (query == NULL)
  1828. return -1;
  1829. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  1830. wpabuf_free(query);
  1831. return -1;
  1832. }
  1833. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  1834. wpabuf_free(query);
  1835. return ret;
  1836. }
  1837. static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  1838. {
  1839. char *pos;
  1840. u8 version;
  1841. pos = os_strchr(cmd, ' ');
  1842. if (pos == NULL)
  1843. return -1;
  1844. *pos++ = '\0';
  1845. if (hexstr2bin(cmd, &version, 1) < 0)
  1846. return -1;
  1847. return wpas_p2p_service_del_upnp(wpa_s, version, pos);
  1848. }
  1849. static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
  1850. {
  1851. char *pos;
  1852. pos = os_strchr(cmd, ' ');
  1853. if (pos == NULL)
  1854. return -1;
  1855. *pos++ = '\0';
  1856. if (os_strcmp(cmd, "bonjour") == 0)
  1857. return p2p_ctrl_service_del_bonjour(wpa_s, pos);
  1858. if (os_strcmp(cmd, "upnp") == 0)
  1859. return p2p_ctrl_service_del_upnp(wpa_s, pos);
  1860. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  1861. return -1;
  1862. }
  1863. static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
  1864. {
  1865. u8 addr[ETH_ALEN];
  1866. /* <addr> */
  1867. if (hwaddr_aton(cmd, addr))
  1868. return -1;
  1869. return wpas_p2p_reject(wpa_s, addr);
  1870. }
  1871. static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
  1872. {
  1873. char *pos;
  1874. int id;
  1875. struct wpa_ssid *ssid;
  1876. u8 peer[ETH_ALEN];
  1877. id = atoi(cmd);
  1878. pos = os_strstr(cmd, " peer=");
  1879. if (pos) {
  1880. pos += 6;
  1881. if (hwaddr_aton(pos, peer))
  1882. return -1;
  1883. }
  1884. ssid = wpa_config_get_network(wpa_s->conf, id);
  1885. if (ssid == NULL || ssid->disabled != 2) {
  1886. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1887. "for persistent P2P group",
  1888. id);
  1889. return -1;
  1890. }
  1891. return wpas_p2p_invite(wpa_s, pos ? peer : NULL, ssid, NULL);
  1892. }
  1893. static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
  1894. {
  1895. char *pos;
  1896. u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
  1897. pos = os_strstr(cmd, " peer=");
  1898. if (!pos)
  1899. return -1;
  1900. *pos = '\0';
  1901. pos += 6;
  1902. if (hwaddr_aton(pos, peer)) {
  1903. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
  1904. return -1;
  1905. }
  1906. pos = os_strstr(pos, " go_dev_addr=");
  1907. if (pos) {
  1908. pos += 13;
  1909. if (hwaddr_aton(pos, go_dev_addr)) {
  1910. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
  1911. pos);
  1912. return -1;
  1913. }
  1914. go_dev = go_dev_addr;
  1915. }
  1916. return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
  1917. }
  1918. static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
  1919. {
  1920. if (os_strncmp(cmd, "persistent=", 11) == 0)
  1921. return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
  1922. if (os_strncmp(cmd, "group=", 6) == 0)
  1923. return p2p_ctrl_invite_group(wpa_s, cmd + 6);
  1924. return -1;
  1925. }
  1926. static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
  1927. char *cmd, int freq)
  1928. {
  1929. int id;
  1930. struct wpa_ssid *ssid;
  1931. id = atoi(cmd);
  1932. ssid = wpa_config_get_network(wpa_s->conf, id);
  1933. if (ssid == NULL || ssid->disabled != 2) {
  1934. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1935. "for persistent P2P group",
  1936. id);
  1937. return -1;
  1938. }
  1939. return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq);
  1940. }
  1941. static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
  1942. {
  1943. int freq = 0;
  1944. char *pos;
  1945. pos = os_strstr(cmd, "freq=");
  1946. if (pos)
  1947. freq = atoi(pos + 5);
  1948. if (os_strncmp(cmd, "persistent=", 11) == 0)
  1949. return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq);
  1950. if (os_strcmp(cmd, "persistent") == 0 ||
  1951. os_strncmp(cmd, "persistent ", 11) == 0)
  1952. return wpas_p2p_group_add(wpa_s, 1, freq);
  1953. if (os_strncmp(cmd, "freq=", 5) == 0)
  1954. return wpas_p2p_group_add(wpa_s, 0, freq);
  1955. wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
  1956. cmd);
  1957. return -1;
  1958. }
  1959. static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
  1960. char *buf, size_t buflen)
  1961. {
  1962. u8 addr[ETH_ALEN], *addr_ptr;
  1963. int next;
  1964. if (!wpa_s->global->p2p)
  1965. return -1;
  1966. if (os_strcmp(cmd, "FIRST") == 0) {
  1967. addr_ptr = NULL;
  1968. next = 0;
  1969. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  1970. if (hwaddr_aton(cmd + 5, addr) < 0)
  1971. return -1;
  1972. addr_ptr = addr;
  1973. next = 1;
  1974. } else {
  1975. if (hwaddr_aton(cmd, addr) < 0)
  1976. return -1;
  1977. addr_ptr = addr;
  1978. next = 0;
  1979. }
  1980. return p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next,
  1981. buf, buflen);
  1982. }
  1983. static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
  1984. {
  1985. char *param;
  1986. if (wpa_s->global->p2p == NULL)
  1987. return -1;
  1988. param = os_strchr(cmd, ' ');
  1989. if (param == NULL)
  1990. return -1;
  1991. *param++ = '\0';
  1992. if (os_strcmp(cmd, "discoverability") == 0) {
  1993. p2p_set_client_discoverability(wpa_s->global->p2p,
  1994. atoi(param));
  1995. return 0;
  1996. }
  1997. if (os_strcmp(cmd, "managed") == 0) {
  1998. p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
  1999. return 0;
  2000. }
  2001. if (os_strcmp(cmd, "listen_channel") == 0) {
  2002. return p2p_set_listen_channel(wpa_s->global->p2p, 81,
  2003. atoi(param));
  2004. }
  2005. if (os_strcmp(cmd, "ssid_postfix") == 0) {
  2006. return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
  2007. os_strlen(param));
  2008. }
  2009. if (os_strcmp(cmd, "noa") == 0) {
  2010. char *pos;
  2011. int count, start, duration;
  2012. /* GO NoA parameters: count,start_offset(ms),duration(ms) */
  2013. count = atoi(param);
  2014. pos = os_strchr(param, ',');
  2015. if (pos == NULL)
  2016. return -1;
  2017. pos++;
  2018. start = atoi(pos);
  2019. pos = os_strchr(pos, ',');
  2020. if (pos == NULL)
  2021. return -1;
  2022. pos++;
  2023. duration = atoi(pos);
  2024. if (count < 0 || count > 255 || start < 0 || duration < 0)
  2025. return -1;
  2026. if (count == 0 && duration > 0)
  2027. return -1;
  2028. wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
  2029. "start=%d duration=%d", count, start, duration);
  2030. return wpa_drv_set_noa(wpa_s, count, start, duration);
  2031. }
  2032. if (os_strcmp(cmd, "disabled") == 0) {
  2033. wpa_s->global->p2p_disabled = atoi(param);
  2034. wpa_printf(MSG_DEBUG, "P2P functionality %s",
  2035. wpa_s->global->p2p_disabled ?
  2036. "disabled" : "enabled");
  2037. if (wpa_s->global->p2p_disabled) {
  2038. wpas_p2p_stop_find(wpa_s);
  2039. p2p_flush(wpa_s->global->p2p);
  2040. }
  2041. return 0;
  2042. }
  2043. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
  2044. cmd);
  2045. return -1;
  2046. }
  2047. static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
  2048. {
  2049. char *pos, *pos2;
  2050. unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
  2051. if (cmd[0]) {
  2052. pos = os_strchr(cmd, ' ');
  2053. if (pos == NULL)
  2054. return -1;
  2055. *pos++ = '\0';
  2056. dur1 = atoi(cmd);
  2057. pos2 = os_strchr(pos, ' ');
  2058. if (pos2)
  2059. *pos2++ = '\0';
  2060. int1 = atoi(pos);
  2061. } else
  2062. pos2 = NULL;
  2063. if (pos2) {
  2064. pos = os_strchr(pos2, ' ');
  2065. if (pos == NULL)
  2066. return -1;
  2067. *pos++ = '\0';
  2068. dur2 = atoi(pos2);
  2069. int2 = atoi(pos);
  2070. }
  2071. return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
  2072. }
  2073. static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
  2074. {
  2075. char *pos;
  2076. unsigned int period = 0, interval = 0;
  2077. if (cmd[0]) {
  2078. pos = os_strchr(cmd, ' ');
  2079. if (pos == NULL)
  2080. return -1;
  2081. *pos++ = '\0';
  2082. period = atoi(cmd);
  2083. interval = atoi(pos);
  2084. }
  2085. return wpas_p2p_ext_listen(wpa_s, period, interval);
  2086. }
  2087. #endif /* CONFIG_P2P */
  2088. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  2089. char *buf, size_t *resp_len)
  2090. {
  2091. char *reply;
  2092. const int reply_size = 4096;
  2093. int ctrl_rsp = 0;
  2094. int reply_len;
  2095. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  2096. os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  2097. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  2098. (const u8 *) buf, os_strlen(buf));
  2099. } else {
  2100. wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface",
  2101. (const u8 *) buf, os_strlen(buf));
  2102. }
  2103. reply = os_malloc(reply_size);
  2104. if (reply == NULL) {
  2105. *resp_len = 1;
  2106. return NULL;
  2107. }
  2108. os_memcpy(reply, "OK\n", 3);
  2109. reply_len = 3;
  2110. if (os_strcmp(buf, "PING") == 0) {
  2111. os_memcpy(reply, "PONG\n", 5);
  2112. reply_len = 5;
  2113. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  2114. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  2115. } else if (os_strcmp(buf, "MIB") == 0) {
  2116. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  2117. if (reply_len >= 0) {
  2118. int res;
  2119. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  2120. reply_size - reply_len);
  2121. if (res < 0)
  2122. reply_len = -1;
  2123. else
  2124. reply_len += res;
  2125. }
  2126. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  2127. reply_len = wpa_supplicant_ctrl_iface_status(
  2128. wpa_s, buf + 6, reply, reply_size);
  2129. } else if (os_strcmp(buf, "PMKSA") == 0) {
  2130. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
  2131. reply_size);
  2132. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  2133. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  2134. reply_len = -1;
  2135. } else if (os_strcmp(buf, "LOGON") == 0) {
  2136. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  2137. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  2138. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  2139. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  2140. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  2141. reply_len = -1;
  2142. else {
  2143. wpa_s->disconnected = 0;
  2144. wpa_s->reassociate = 1;
  2145. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2146. }
  2147. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  2148. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  2149. reply_len = -1;
  2150. else if (wpa_s->disconnected) {
  2151. wpa_s->disconnected = 0;
  2152. wpa_s->reassociate = 1;
  2153. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2154. }
  2155. #ifdef IEEE8021X_EAPOL
  2156. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  2157. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  2158. reply_len = -1;
  2159. #endif /* IEEE8021X_EAPOL */
  2160. #ifdef CONFIG_PEERKEY
  2161. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  2162. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  2163. reply_len = -1;
  2164. #endif /* CONFIG_PEERKEY */
  2165. #ifdef CONFIG_IEEE80211R
  2166. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  2167. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  2168. reply_len = -1;
  2169. #endif /* CONFIG_IEEE80211R */
  2170. #ifdef CONFIG_WPS
  2171. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  2172. if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL))
  2173. reply_len = -1;
  2174. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  2175. if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8))
  2176. reply_len = -1;
  2177. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  2178. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  2179. reply,
  2180. reply_size);
  2181. #ifdef CONFIG_WPS_OOB
  2182. } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
  2183. if (wpa_supplicant_ctrl_iface_wps_oob(wpa_s, buf + 8))
  2184. reply_len = -1;
  2185. #endif /* CONFIG_WPS_OOB */
  2186. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  2187. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  2188. reply_len = -1;
  2189. #ifdef CONFIG_WPS_ER
  2190. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  2191. if (wpas_wps_er_start(wpa_s, NULL))
  2192. reply_len = -1;
  2193. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  2194. if (wpas_wps_er_start(wpa_s, buf + 13))
  2195. reply_len = -1;
  2196. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  2197. if (wpas_wps_er_stop(wpa_s))
  2198. reply_len = -1;
  2199. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  2200. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  2201. reply_len = -1;
  2202. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  2203. if (wpas_wps_er_pbc(wpa_s, buf + 11))
  2204. reply_len = -1;
  2205. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  2206. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  2207. reply_len = -1;
  2208. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  2209. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  2210. reply_len = -1;
  2211. #endif /* CONFIG_WPS_ER */
  2212. #endif /* CONFIG_WPS */
  2213. #ifdef CONFIG_IBSS_RSN
  2214. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  2215. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  2216. reply_len = -1;
  2217. #endif /* CONFIG_IBSS_RSN */
  2218. #ifdef CONFIG_P2P
  2219. } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
  2220. if (p2p_ctrl_find(wpa_s, buf + 9))
  2221. reply_len = -1;
  2222. } else if (os_strcmp(buf, "P2P_FIND") == 0) {
  2223. if (p2p_ctrl_find(wpa_s, ""))
  2224. reply_len = -1;
  2225. } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
  2226. wpas_p2p_stop_find(wpa_s);
  2227. } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
  2228. reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
  2229. reply_size);
  2230. } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
  2231. if (p2p_ctrl_listen(wpa_s, buf + 11))
  2232. reply_len = -1;
  2233. } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
  2234. if (p2p_ctrl_listen(wpa_s, ""))
  2235. reply_len = -1;
  2236. } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
  2237. if (wpas_p2p_group_remove(wpa_s, buf + 17))
  2238. reply_len = -1;
  2239. } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
  2240. if (wpas_p2p_group_add(wpa_s, 0, 0))
  2241. reply_len = -1;
  2242. } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
  2243. if (p2p_ctrl_group_add(wpa_s, buf + 14))
  2244. reply_len = -1;
  2245. } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
  2246. if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
  2247. reply_len = -1;
  2248. } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
  2249. reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
  2250. } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
  2251. reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
  2252. reply_size);
  2253. } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
  2254. if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
  2255. reply_len = -1;
  2256. } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
  2257. if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
  2258. reply_len = -1;
  2259. } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
  2260. wpas_p2p_sd_service_update(wpa_s);
  2261. } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
  2262. if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
  2263. reply_len = -1;
  2264. } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
  2265. wpas_p2p_service_flush(wpa_s);
  2266. } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
  2267. if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
  2268. reply_len = -1;
  2269. } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
  2270. if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
  2271. reply_len = -1;
  2272. } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
  2273. if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
  2274. reply_len = -1;
  2275. } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
  2276. if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
  2277. reply_len = -1;
  2278. } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
  2279. reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
  2280. reply_size);
  2281. } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
  2282. if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
  2283. reply_len = -1;
  2284. } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
  2285. p2p_flush(wpa_s->global->p2p);
  2286. } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
  2287. if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
  2288. reply_len = -1;
  2289. } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
  2290. if (p2p_ctrl_presence_req(wpa_s, "") < 0)
  2291. reply_len = -1;
  2292. } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
  2293. if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
  2294. reply_len = -1;
  2295. } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
  2296. if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
  2297. reply_len = -1;
  2298. #endif /* CONFIG_P2P */
  2299. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  2300. {
  2301. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  2302. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  2303. reply_len = -1;
  2304. else
  2305. ctrl_rsp = 1;
  2306. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  2307. if (wpa_supplicant_reload_configuration(wpa_s))
  2308. reply_len = -1;
  2309. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  2310. wpa_supplicant_terminate_proc(wpa_s->global);
  2311. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  2312. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  2313. reply_len = -1;
  2314. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  2315. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  2316. wpa_s, reply, reply_size);
  2317. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  2318. wpa_s->reassociate = 0;
  2319. wpa_s->disconnected = 1;
  2320. wpa_supplicant_deauthenticate(wpa_s,
  2321. WLAN_REASON_DEAUTH_LEAVING);
  2322. } else if (os_strcmp(buf, "SCAN") == 0) {
  2323. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  2324. reply_len = -1;
  2325. else {
  2326. wpa_s->scan_req = 2;
  2327. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2328. }
  2329. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  2330. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  2331. wpa_s, reply, reply_size);
  2332. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  2333. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  2334. reply_len = -1;
  2335. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  2336. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  2337. reply_len = -1;
  2338. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  2339. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  2340. reply_len = -1;
  2341. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  2342. reply_len = wpa_supplicant_ctrl_iface_add_network(
  2343. wpa_s, reply, reply_size);
  2344. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  2345. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  2346. reply_len = -1;
  2347. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  2348. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  2349. reply_len = -1;
  2350. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  2351. reply_len = wpa_supplicant_ctrl_iface_get_network(
  2352. wpa_s, buf + 12, reply, reply_size);
  2353. #ifndef CONFIG_NO_CONFIG_WRITE
  2354. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  2355. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  2356. reply_len = -1;
  2357. #endif /* CONFIG_NO_CONFIG_WRITE */
  2358. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  2359. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  2360. wpa_s, buf + 15, reply, reply_size);
  2361. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  2362. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  2363. reply_len = -1;
  2364. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  2365. reply_len = wpa_supplicant_global_iface_list(
  2366. wpa_s->global, reply, reply_size);
  2367. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  2368. reply_len = wpa_supplicant_global_iface_interfaces(
  2369. wpa_s->global, reply, reply_size);
  2370. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  2371. reply_len = wpa_supplicant_ctrl_iface_bss(
  2372. wpa_s, buf + 4, reply, reply_size);
  2373. #ifdef CONFIG_AP
  2374. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  2375. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  2376. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  2377. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  2378. reply_size);
  2379. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  2380. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  2381. reply_size);
  2382. #endif /* CONFIG_AP */
  2383. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  2384. wpas_notify_suspend(wpa_s->global);
  2385. } else if (os_strcmp(buf, "RESUME") == 0) {
  2386. wpas_notify_resume(wpa_s->global);
  2387. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  2388. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  2389. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  2390. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  2391. reply_len = -1;
  2392. } else {
  2393. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  2394. reply_len = 16;
  2395. }
  2396. if (reply_len < 0) {
  2397. os_memcpy(reply, "FAIL\n", 5);
  2398. reply_len = 5;
  2399. }
  2400. if (ctrl_rsp)
  2401. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  2402. *resp_len = reply_len;
  2403. return reply;
  2404. }
  2405. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  2406. char *cmd)
  2407. {
  2408. struct wpa_interface iface;
  2409. char *pos;
  2410. /*
  2411. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  2412. * TAB<bridge_ifname>
  2413. */
  2414. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  2415. os_memset(&iface, 0, sizeof(iface));
  2416. do {
  2417. iface.ifname = pos = cmd;
  2418. pos = os_strchr(pos, '\t');
  2419. if (pos)
  2420. *pos++ = '\0';
  2421. if (iface.ifname[0] == '\0')
  2422. return -1;
  2423. if (pos == NULL)
  2424. break;
  2425. iface.confname = pos;
  2426. pos = os_strchr(pos, '\t');
  2427. if (pos)
  2428. *pos++ = '\0';
  2429. if (iface.confname[0] == '\0')
  2430. iface.confname = NULL;
  2431. if (pos == NULL)
  2432. break;
  2433. iface.driver = pos;
  2434. pos = os_strchr(pos, '\t');
  2435. if (pos)
  2436. *pos++ = '\0';
  2437. if (iface.driver[0] == '\0')
  2438. iface.driver = NULL;
  2439. if (pos == NULL)
  2440. break;
  2441. iface.ctrl_interface = pos;
  2442. pos = os_strchr(pos, '\t');
  2443. if (pos)
  2444. *pos++ = '\0';
  2445. if (iface.ctrl_interface[0] == '\0')
  2446. iface.ctrl_interface = NULL;
  2447. if (pos == NULL)
  2448. break;
  2449. iface.driver_param = pos;
  2450. pos = os_strchr(pos, '\t');
  2451. if (pos)
  2452. *pos++ = '\0';
  2453. if (iface.driver_param[0] == '\0')
  2454. iface.driver_param = NULL;
  2455. if (pos == NULL)
  2456. break;
  2457. iface.bridge_ifname = pos;
  2458. pos = os_strchr(pos, '\t');
  2459. if (pos)
  2460. *pos++ = '\0';
  2461. if (iface.bridge_ifname[0] == '\0')
  2462. iface.bridge_ifname = NULL;
  2463. if (pos == NULL)
  2464. break;
  2465. } while (0);
  2466. if (wpa_supplicant_get_iface(global, iface.ifname))
  2467. return -1;
  2468. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  2469. }
  2470. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  2471. char *cmd)
  2472. {
  2473. struct wpa_supplicant *wpa_s;
  2474. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  2475. wpa_s = wpa_supplicant_get_iface(global, cmd);
  2476. if (wpa_s == NULL)
  2477. return -1;
  2478. return wpa_supplicant_remove_iface(global, wpa_s);
  2479. }
  2480. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  2481. {
  2482. struct wpa_interface_info *prev;
  2483. while (iface) {
  2484. prev = iface;
  2485. iface = iface->next;
  2486. os_free(prev->ifname);
  2487. os_free(prev->desc);
  2488. os_free(prev);
  2489. }
  2490. }
  2491. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  2492. char *buf, int len)
  2493. {
  2494. int i, res;
  2495. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  2496. char *pos, *end;
  2497. for (i = 0; wpa_drivers[i]; i++) {
  2498. struct wpa_driver_ops *drv = wpa_drivers[i];
  2499. if (drv->get_interfaces == NULL)
  2500. continue;
  2501. tmp = drv->get_interfaces(global->drv_priv[i]);
  2502. if (tmp == NULL)
  2503. continue;
  2504. if (last == NULL)
  2505. iface = last = tmp;
  2506. else
  2507. last->next = tmp;
  2508. while (last->next)
  2509. last = last->next;
  2510. }
  2511. pos = buf;
  2512. end = buf + len;
  2513. for (tmp = iface; tmp; tmp = tmp->next) {
  2514. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  2515. tmp->drv_name, tmp->ifname,
  2516. tmp->desc ? tmp->desc : "");
  2517. if (res < 0 || res >= end - pos) {
  2518. *pos = '\0';
  2519. break;
  2520. }
  2521. pos += res;
  2522. }
  2523. wpa_free_iface_info(iface);
  2524. return pos - buf;
  2525. }
  2526. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  2527. char *buf, int len)
  2528. {
  2529. int res;
  2530. char *pos, *end;
  2531. struct wpa_supplicant *wpa_s;
  2532. wpa_s = global->ifaces;
  2533. pos = buf;
  2534. end = buf + len;
  2535. while (wpa_s) {
  2536. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  2537. if (res < 0 || res >= end - pos) {
  2538. *pos = '\0';
  2539. break;
  2540. }
  2541. pos += res;
  2542. wpa_s = wpa_s->next;
  2543. }
  2544. return pos - buf;
  2545. }
  2546. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  2547. char *buf, size_t *resp_len)
  2548. {
  2549. char *reply;
  2550. const int reply_size = 2048;
  2551. int reply_len;
  2552. wpa_hexdump_ascii(MSG_DEBUG, "RX global ctrl_iface",
  2553. (const u8 *) buf, os_strlen(buf));
  2554. reply = os_malloc(reply_size);
  2555. if (reply == NULL) {
  2556. *resp_len = 1;
  2557. return NULL;
  2558. }
  2559. os_memcpy(reply, "OK\n", 3);
  2560. reply_len = 3;
  2561. if (os_strcmp(buf, "PING") == 0) {
  2562. os_memcpy(reply, "PONG\n", 5);
  2563. reply_len = 5;
  2564. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  2565. if (wpa_supplicant_global_iface_add(global, buf + 14))
  2566. reply_len = -1;
  2567. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  2568. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  2569. reply_len = -1;
  2570. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  2571. reply_len = wpa_supplicant_global_iface_list(
  2572. global, reply, reply_size);
  2573. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  2574. reply_len = wpa_supplicant_global_iface_interfaces(
  2575. global, reply, reply_size);
  2576. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  2577. wpa_supplicant_terminate_proc(global);
  2578. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  2579. wpas_notify_suspend(global);
  2580. } else if (os_strcmp(buf, "RESUME") == 0) {
  2581. wpas_notify_resume(global);
  2582. } else {
  2583. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  2584. reply_len = 16;
  2585. }
  2586. if (reply_len < 0) {
  2587. os_memcpy(reply, "FAIL\n", 5);
  2588. reply_len = 5;
  2589. }
  2590. *resp_len = reply_len;
  2591. return reply;
  2592. }