ctrl_iface.c 92 KB

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