ctrl_iface.c 111 KB

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