ctrl_iface.c 92 KB

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