ctrl_iface.c 111 KB

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