ctrl_iface.c 123 KB

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