ctrl_iface.c 120 KB

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