ctrl_iface.c 118 KB

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