ctrl_iface.c 119 KB

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