ctrl_iface.c 91 KB

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