ctrl_iface.c 91 KB

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