ctrl_iface.c 94 KB

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