ctrl_iface.c 130 KB

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