ctrl_iface.c 124 KB

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