ctrl_iface.c 130 KB

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