ctrl_iface.c 94 KB

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