ctrl_iface.c 76 KB

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