ctrl_iface.c 96 KB

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