ctrl_iface.c 102 KB

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