ctrl_iface.c 97 KB

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