dbus_new_handlers.c 133 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800
  1. /*
  2. * WPA Supplicant / dbus-based control interface
  3. * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
  4. * Copyright (c) 2009-2010, Witold Sowa <witold.sowa@gmail.com>
  5. * Copyright (c) 2009-2015, Jouni Malinen <j@w1.fi>
  6. *
  7. * This software may be distributed under the terms of the BSD license.
  8. * See README for more details.
  9. */
  10. #include "includes.h"
  11. #include "common.h"
  12. #include "common/ieee802_11_defs.h"
  13. #include "eap_peer/eap_methods.h"
  14. #include "eapol_supp/eapol_supp_sm.h"
  15. #include "rsn_supp/wpa.h"
  16. #include "../config.h"
  17. #include "../wpa_supplicant_i.h"
  18. #include "../driver_i.h"
  19. #include "../notify.h"
  20. #include "../bss.h"
  21. #include "../scan.h"
  22. #include "../autoscan.h"
  23. #include "dbus_new_helpers.h"
  24. #include "dbus_new.h"
  25. #include "dbus_new_handlers.h"
  26. #include "dbus_dict_helpers.h"
  27. #include "dbus_common_i.h"
  28. #include "drivers/driver.h"
  29. static const char * const debug_strings[] = {
  30. "excessive", "msgdump", "debug", "info", "warning", "error", NULL
  31. };
  32. /**
  33. * wpas_dbus_error_unknown_error - Return a new UnknownError error message
  34. * @message: Pointer to incoming dbus message this error refers to
  35. * @arg: Optional string appended to error message
  36. * Returns: a dbus error message
  37. *
  38. * Convenience function to create and return an UnknownError
  39. */
  40. DBusMessage * wpas_dbus_error_unknown_error(DBusMessage *message,
  41. const char *arg)
  42. {
  43. return dbus_message_new_error(message, WPAS_DBUS_ERROR_UNKNOWN_ERROR,
  44. arg);
  45. }
  46. /**
  47. * wpas_dbus_error_iface_unknown - Return a new invalid interface error message
  48. * @message: Pointer to incoming dbus message this error refers to
  49. * Returns: A dbus error message
  50. *
  51. * Convenience function to create and return an invalid interface error
  52. */
  53. static DBusMessage * wpas_dbus_error_iface_unknown(DBusMessage *message)
  54. {
  55. return dbus_message_new_error(
  56. message, WPAS_DBUS_ERROR_IFACE_UNKNOWN,
  57. "wpa_supplicant knows nothing about this interface.");
  58. }
  59. /**
  60. * wpas_dbus_error_network_unknown - Return a new NetworkUnknown error message
  61. * @message: Pointer to incoming dbus message this error refers to
  62. * Returns: a dbus error message
  63. *
  64. * Convenience function to create and return an invalid network error
  65. */
  66. static DBusMessage * wpas_dbus_error_network_unknown(DBusMessage *message)
  67. {
  68. return dbus_message_new_error(
  69. message, WPAS_DBUS_ERROR_NETWORK_UNKNOWN,
  70. "There is no such a network in this interface.");
  71. }
  72. /**
  73. * wpas_dbus_error_invalid_args - Return a new InvalidArgs error message
  74. * @message: Pointer to incoming dbus message this error refers to
  75. * Returns: a dbus error message
  76. *
  77. * Convenience function to create and return an invalid options error
  78. */
  79. DBusMessage * wpas_dbus_error_invalid_args(DBusMessage *message,
  80. const char *arg)
  81. {
  82. DBusMessage *reply;
  83. reply = dbus_message_new_error(
  84. message, WPAS_DBUS_ERROR_INVALID_ARGS,
  85. "Did not receive correct message arguments.");
  86. if (arg != NULL)
  87. dbus_message_append_args(reply, DBUS_TYPE_STRING, &arg,
  88. DBUS_TYPE_INVALID);
  89. return reply;
  90. }
  91. /**
  92. * wpas_dbus_error_scan_error - Return a new ScanError error message
  93. * @message: Pointer to incoming dbus message this error refers to
  94. * @error: Optional string to be used as the error message
  95. * Returns: a dbus error message
  96. *
  97. * Convenience function to create and return a scan error
  98. */
  99. static DBusMessage * wpas_dbus_error_scan_error(DBusMessage *message,
  100. const char *error)
  101. {
  102. return dbus_message_new_error(message,
  103. WPAS_DBUS_ERROR_IFACE_SCAN_ERROR,
  104. error);
  105. }
  106. DBusMessage * wpas_dbus_error_no_memory(DBusMessage *message)
  107. {
  108. wpa_printf(MSG_DEBUG, "dbus: Failed to allocate memory");
  109. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY, NULL);
  110. }
  111. static const char * const dont_quote[] = {
  112. "key_mgmt", "proto", "pairwise", "auth_alg", "group", "eap",
  113. "opensc_engine_path", "pkcs11_engine_path", "pkcs11_module_path",
  114. "bssid", "scan_freq", "freq_list", NULL
  115. };
  116. static dbus_bool_t should_quote_opt(const char *key)
  117. {
  118. int i = 0;
  119. while (dont_quote[i] != NULL) {
  120. if (os_strcmp(key, dont_quote[i]) == 0)
  121. return FALSE;
  122. i++;
  123. }
  124. return TRUE;
  125. }
  126. /**
  127. * get_iface_by_dbus_path - Get a new network interface
  128. * @global: Pointer to global data from wpa_supplicant_init()
  129. * @path: Pointer to a dbus object path representing an interface
  130. * Returns: Pointer to the interface or %NULL if not found
  131. */
  132. static struct wpa_supplicant * get_iface_by_dbus_path(
  133. struct wpa_global *global, const char *path)
  134. {
  135. struct wpa_supplicant *wpa_s;
  136. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  137. if (wpa_s->dbus_new_path &&
  138. os_strcmp(wpa_s->dbus_new_path, path) == 0)
  139. return wpa_s;
  140. }
  141. return NULL;
  142. }
  143. /**
  144. * set_network_properties - Set properties of a configured network
  145. * @wpa_s: wpa_supplicant structure for a network interface
  146. * @ssid: wpa_ssid structure for a configured network
  147. * @iter: DBus message iterator containing dictionary of network
  148. * properties to set.
  149. * @error: On failure, an error describing the failure
  150. * Returns: TRUE if the request succeeds, FALSE if it failed
  151. *
  152. * Sets network configuration with parameters given id DBus dictionary
  153. */
  154. dbus_bool_t set_network_properties(struct wpa_supplicant *wpa_s,
  155. struct wpa_ssid *ssid,
  156. DBusMessageIter *iter,
  157. DBusError *error)
  158. {
  159. struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
  160. DBusMessageIter iter_dict;
  161. char *value = NULL;
  162. if (!wpa_dbus_dict_open_read(iter, &iter_dict, error))
  163. return FALSE;
  164. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  165. size_t size = 50;
  166. int ret;
  167. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  168. goto error;
  169. value = NULL;
  170. if (entry.type == DBUS_TYPE_ARRAY &&
  171. entry.array_type == DBUS_TYPE_BYTE) {
  172. if (entry.array_len <= 0)
  173. goto error;
  174. size = entry.array_len * 2 + 1;
  175. value = os_zalloc(size);
  176. if (value == NULL)
  177. goto error;
  178. ret = wpa_snprintf_hex(value, size,
  179. (u8 *) entry.bytearray_value,
  180. entry.array_len);
  181. if (ret <= 0)
  182. goto error;
  183. } else if (entry.type == DBUS_TYPE_STRING) {
  184. if (should_quote_opt(entry.key)) {
  185. size = os_strlen(entry.str_value);
  186. if (size == 0)
  187. goto error;
  188. size += 3;
  189. value = os_zalloc(size);
  190. if (value == NULL)
  191. goto error;
  192. ret = os_snprintf(value, size, "\"%s\"",
  193. entry.str_value);
  194. if (os_snprintf_error(size, ret))
  195. goto error;
  196. } else {
  197. value = os_strdup(entry.str_value);
  198. if (value == NULL)
  199. goto error;
  200. }
  201. } else if (entry.type == DBUS_TYPE_UINT32) {
  202. value = os_zalloc(size);
  203. if (value == NULL)
  204. goto error;
  205. ret = os_snprintf(value, size, "%u",
  206. entry.uint32_value);
  207. if (os_snprintf_error(size, ret))
  208. goto error;
  209. } else if (entry.type == DBUS_TYPE_INT32) {
  210. value = os_zalloc(size);
  211. if (value == NULL)
  212. goto error;
  213. ret = os_snprintf(value, size, "%d",
  214. entry.int32_value);
  215. if (os_snprintf_error(size, ret))
  216. goto error;
  217. } else
  218. goto error;
  219. if (wpa_config_set(ssid, entry.key, value, 0) < 0)
  220. goto error;
  221. if (os_strcmp(entry.key, "bssid") != 0 &&
  222. os_strcmp(entry.key, "priority") != 0)
  223. wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
  224. if (wpa_s->current_ssid == ssid ||
  225. wpa_s->current_ssid == NULL) {
  226. /*
  227. * Invalidate the EAP session cache if anything in the
  228. * current or previously used configuration changes.
  229. */
  230. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  231. }
  232. if ((os_strcmp(entry.key, "psk") == 0 &&
  233. value[0] == '"' && ssid->ssid_len) ||
  234. (os_strcmp(entry.key, "ssid") == 0 && ssid->passphrase))
  235. wpa_config_update_psk(ssid);
  236. else if (os_strcmp(entry.key, "priority") == 0)
  237. wpa_config_update_prio_list(wpa_s->conf);
  238. os_free(value);
  239. value = NULL;
  240. wpa_dbus_dict_entry_clear(&entry);
  241. }
  242. return TRUE;
  243. error:
  244. os_free(value);
  245. wpa_dbus_dict_entry_clear(&entry);
  246. dbus_set_error_const(error, DBUS_ERROR_INVALID_ARGS,
  247. "invalid message format");
  248. return FALSE;
  249. }
  250. /**
  251. * wpas_dbus_simple_property_getter - Get basic type property
  252. * @iter: Message iter to use when appending arguments
  253. * @type: DBus type of property (must be basic type)
  254. * @val: pointer to place holding property value
  255. * @error: On failure an error describing the failure
  256. * Returns: TRUE if the request was successful, FALSE if it failed
  257. *
  258. * Generic getter for basic type properties. Type is required to be basic.
  259. */
  260. dbus_bool_t wpas_dbus_simple_property_getter(DBusMessageIter *iter,
  261. const int type,
  262. const void *val,
  263. DBusError *error)
  264. {
  265. DBusMessageIter variant_iter;
  266. if (!dbus_type_is_basic(type)) {
  267. dbus_set_error(error, DBUS_ERROR_FAILED,
  268. "%s: given type is not basic", __func__);
  269. return FALSE;
  270. }
  271. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  272. wpa_dbus_type_as_string(type),
  273. &variant_iter) ||
  274. !dbus_message_iter_append_basic(&variant_iter, type, val) ||
  275. !dbus_message_iter_close_container(iter, &variant_iter)) {
  276. dbus_set_error(error, DBUS_ERROR_FAILED,
  277. "%s: error constructing reply", __func__);
  278. return FALSE;
  279. }
  280. return TRUE;
  281. }
  282. /**
  283. * wpas_dbus_simple_property_setter - Set basic type property
  284. * @message: Pointer to incoming dbus message
  285. * @type: DBus type of property (must be basic type)
  286. * @val: pointer to place where value being set will be stored
  287. * Returns: TRUE if the request was successful, FALSE if it failed
  288. *
  289. * Generic setter for basic type properties. Type is required to be basic.
  290. */
  291. dbus_bool_t wpas_dbus_simple_property_setter(DBusMessageIter *iter,
  292. DBusError *error,
  293. const int type, void *val)
  294. {
  295. DBusMessageIter variant_iter;
  296. if (!dbus_type_is_basic(type)) {
  297. dbus_set_error(error, DBUS_ERROR_FAILED,
  298. "%s: given type is not basic", __func__);
  299. return FALSE;
  300. }
  301. /* Look at the new value */
  302. dbus_message_iter_recurse(iter, &variant_iter);
  303. if (dbus_message_iter_get_arg_type(&variant_iter) != type) {
  304. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  305. "wrong property type");
  306. return FALSE;
  307. }
  308. dbus_message_iter_get_basic(&variant_iter, val);
  309. return TRUE;
  310. }
  311. /**
  312. * wpas_dbus_simple_array_property_getter - Get array type property
  313. * @iter: Pointer to incoming dbus message iterator
  314. * @type: DBus type of property array elements (must be basic type)
  315. * @array: pointer to array of elements to put into response message
  316. * @array_len: length of above array
  317. * @error: a pointer to an error to fill on failure
  318. * Returns: TRUE if the request succeeded, FALSE if it failed
  319. *
  320. * Generic getter for array type properties. Array elements type is
  321. * required to be basic.
  322. */
  323. dbus_bool_t wpas_dbus_simple_array_property_getter(DBusMessageIter *iter,
  324. const int type,
  325. const void *array,
  326. size_t array_len,
  327. DBusError *error)
  328. {
  329. DBusMessageIter variant_iter, array_iter;
  330. char type_str[] = "a?"; /* ? will be replaced with subtype letter; */
  331. const char *sub_type_str;
  332. size_t element_size, i;
  333. if (!dbus_type_is_basic(type)) {
  334. dbus_set_error(error, DBUS_ERROR_FAILED,
  335. "%s: given type is not basic", __func__);
  336. return FALSE;
  337. }
  338. sub_type_str = wpa_dbus_type_as_string(type);
  339. type_str[1] = sub_type_str[0];
  340. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  341. type_str, &variant_iter) ||
  342. !dbus_message_iter_open_container(&variant_iter, DBUS_TYPE_ARRAY,
  343. sub_type_str, &array_iter)) {
  344. dbus_set_error(error, DBUS_ERROR_FAILED,
  345. "%s: failed to construct message", __func__);
  346. return FALSE;
  347. }
  348. switch (type) {
  349. case DBUS_TYPE_BYTE:
  350. case DBUS_TYPE_BOOLEAN:
  351. element_size = 1;
  352. break;
  353. case DBUS_TYPE_INT16:
  354. case DBUS_TYPE_UINT16:
  355. element_size = sizeof(uint16_t);
  356. break;
  357. case DBUS_TYPE_INT32:
  358. case DBUS_TYPE_UINT32:
  359. element_size = sizeof(uint32_t);
  360. break;
  361. case DBUS_TYPE_INT64:
  362. case DBUS_TYPE_UINT64:
  363. element_size = sizeof(uint64_t);
  364. break;
  365. case DBUS_TYPE_DOUBLE:
  366. element_size = sizeof(double);
  367. break;
  368. case DBUS_TYPE_STRING:
  369. case DBUS_TYPE_OBJECT_PATH:
  370. element_size = sizeof(char *);
  371. break;
  372. default:
  373. dbus_set_error(error, DBUS_ERROR_FAILED,
  374. "%s: unknown element type %d", __func__, type);
  375. return FALSE;
  376. }
  377. for (i = 0; i < array_len; i++) {
  378. if (!dbus_message_iter_append_basic(&array_iter, type,
  379. (const char *) array +
  380. i * element_size)) {
  381. dbus_set_error(error, DBUS_ERROR_FAILED,
  382. "%s: failed to construct message 2.5",
  383. __func__);
  384. return FALSE;
  385. }
  386. }
  387. if (!dbus_message_iter_close_container(&variant_iter, &array_iter) ||
  388. !dbus_message_iter_close_container(iter, &variant_iter)) {
  389. dbus_set_error(error, DBUS_ERROR_FAILED,
  390. "%s: failed to construct message 3", __func__);
  391. return FALSE;
  392. }
  393. return TRUE;
  394. }
  395. /**
  396. * wpas_dbus_simple_array_array_property_getter - Get array array type property
  397. * @iter: Pointer to incoming dbus message iterator
  398. * @type: DBus type of property array elements (must be basic type)
  399. * @array: pointer to array of elements to put into response message
  400. * @array_len: length of above array
  401. * @error: a pointer to an error to fill on failure
  402. * Returns: TRUE if the request succeeded, FALSE if it failed
  403. *
  404. * Generic getter for array type properties. Array elements type is
  405. * required to be basic.
  406. */
  407. dbus_bool_t wpas_dbus_simple_array_array_property_getter(DBusMessageIter *iter,
  408. const int type,
  409. struct wpabuf **array,
  410. size_t array_len,
  411. DBusError *error)
  412. {
  413. DBusMessageIter variant_iter, array_iter;
  414. char type_str[] = "aa?";
  415. char inner_type_str[] = "a?";
  416. const char *sub_type_str;
  417. size_t i;
  418. if (!dbus_type_is_basic(type)) {
  419. dbus_set_error(error, DBUS_ERROR_FAILED,
  420. "%s: given type is not basic", __func__);
  421. return FALSE;
  422. }
  423. sub_type_str = wpa_dbus_type_as_string(type);
  424. type_str[2] = sub_type_str[0];
  425. inner_type_str[1] = sub_type_str[0];
  426. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  427. type_str, &variant_iter) ||
  428. !dbus_message_iter_open_container(&variant_iter, DBUS_TYPE_ARRAY,
  429. inner_type_str, &array_iter)) {
  430. dbus_set_error(error, DBUS_ERROR_FAILED,
  431. "%s: failed to construct message", __func__);
  432. return FALSE;
  433. }
  434. for (i = 0; i < array_len && array[i]; i++) {
  435. wpa_dbus_dict_bin_array_add_element(&array_iter,
  436. wpabuf_head(array[i]),
  437. wpabuf_len(array[i]));
  438. }
  439. if (!dbus_message_iter_close_container(&variant_iter, &array_iter) ||
  440. !dbus_message_iter_close_container(iter, &variant_iter)) {
  441. dbus_set_error(error, DBUS_ERROR_FAILED,
  442. "%s: failed to close message", __func__);
  443. return FALSE;
  444. }
  445. return TRUE;
  446. }
  447. /**
  448. * wpas_dbus_string_property_getter - Get string type property
  449. * @iter: Message iter to use when appending arguments
  450. * @val: Pointer to place holding property value, can be %NULL
  451. * @error: On failure an error describing the failure
  452. * Returns: TRUE if the request was successful, FALSE if it failed
  453. *
  454. * Generic getter for string type properties. %NULL is converted to an empty
  455. * string.
  456. */
  457. dbus_bool_t wpas_dbus_string_property_getter(DBusMessageIter *iter,
  458. const void *val,
  459. DBusError *error)
  460. {
  461. if (!val)
  462. val = "";
  463. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  464. &val, error);
  465. }
  466. /**
  467. * wpas_dbus_handler_create_interface - Request registration of a network iface
  468. * @message: Pointer to incoming dbus message
  469. * @global: %wpa_supplicant global data structure
  470. * Returns: The object path of the new interface object,
  471. * or a dbus error message with more information
  472. *
  473. * Handler function for "CreateInterface" method call. Handles requests
  474. * by dbus clients to register a network interface that wpa_supplicant
  475. * will manage.
  476. */
  477. DBusMessage * wpas_dbus_handler_create_interface(DBusMessage *message,
  478. struct wpa_global *global)
  479. {
  480. DBusMessageIter iter_dict;
  481. DBusMessage *reply = NULL;
  482. DBusMessageIter iter;
  483. struct wpa_dbus_dict_entry entry;
  484. char *driver = NULL;
  485. char *ifname = NULL;
  486. char *confname = NULL;
  487. char *bridge_ifname = NULL;
  488. dbus_message_iter_init(message, &iter);
  489. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  490. goto error;
  491. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  492. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  493. goto error;
  494. if (os_strcmp(entry.key, "Driver") == 0 &&
  495. entry.type == DBUS_TYPE_STRING) {
  496. os_free(driver);
  497. driver = os_strdup(entry.str_value);
  498. wpa_dbus_dict_entry_clear(&entry);
  499. if (driver == NULL)
  500. goto oom;
  501. } else if (os_strcmp(entry.key, "Ifname") == 0 &&
  502. entry.type == DBUS_TYPE_STRING) {
  503. os_free(ifname);
  504. ifname = os_strdup(entry.str_value);
  505. wpa_dbus_dict_entry_clear(&entry);
  506. if (ifname == NULL)
  507. goto oom;
  508. } else if (os_strcmp(entry.key, "ConfigFile") == 0 &&
  509. entry.type == DBUS_TYPE_STRING) {
  510. os_free(confname);
  511. confname = os_strdup(entry.str_value);
  512. wpa_dbus_dict_entry_clear(&entry);
  513. if (confname == NULL)
  514. goto oom;
  515. } else if (os_strcmp(entry.key, "BridgeIfname") == 0 &&
  516. entry.type == DBUS_TYPE_STRING) {
  517. os_free(bridge_ifname);
  518. bridge_ifname = os_strdup(entry.str_value);
  519. wpa_dbus_dict_entry_clear(&entry);
  520. if (bridge_ifname == NULL)
  521. goto oom;
  522. } else {
  523. wpa_dbus_dict_entry_clear(&entry);
  524. goto error;
  525. }
  526. }
  527. if (ifname == NULL)
  528. goto error; /* Required Ifname argument missing */
  529. /*
  530. * Try to get the wpa_supplicant record for this iface, return
  531. * an error if we already control it.
  532. */
  533. if (wpa_supplicant_get_iface(global, ifname) != NULL) {
  534. reply = dbus_message_new_error(
  535. message, WPAS_DBUS_ERROR_IFACE_EXISTS,
  536. "wpa_supplicant already controls this interface.");
  537. } else {
  538. struct wpa_supplicant *wpa_s;
  539. struct wpa_interface iface;
  540. os_memset(&iface, 0, sizeof(iface));
  541. iface.driver = driver;
  542. iface.ifname = ifname;
  543. iface.confname = confname;
  544. iface.bridge_ifname = bridge_ifname;
  545. /* Otherwise, have wpa_supplicant attach to it. */
  546. wpa_s = wpa_supplicant_add_iface(global, &iface, NULL);
  547. if (wpa_s && wpa_s->dbus_new_path) {
  548. const char *path = wpa_s->dbus_new_path;
  549. reply = dbus_message_new_method_return(message);
  550. dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
  551. &path, DBUS_TYPE_INVALID);
  552. } else {
  553. reply = wpas_dbus_error_unknown_error(
  554. message,
  555. "wpa_supplicant couldn't grab this interface.");
  556. }
  557. }
  558. out:
  559. os_free(driver);
  560. os_free(ifname);
  561. os_free(confname);
  562. os_free(bridge_ifname);
  563. return reply;
  564. error:
  565. reply = wpas_dbus_error_invalid_args(message, NULL);
  566. goto out;
  567. oom:
  568. reply = wpas_dbus_error_no_memory(message);
  569. goto out;
  570. }
  571. /**
  572. * wpas_dbus_handler_remove_interface - Request deregistration of an interface
  573. * @message: Pointer to incoming dbus message
  574. * @global: wpa_supplicant global data structure
  575. * Returns: a dbus message containing a UINT32 indicating success (1) or
  576. * failure (0), or returns a dbus error message with more information
  577. *
  578. * Handler function for "removeInterface" method call. Handles requests
  579. * by dbus clients to deregister a network interface that wpa_supplicant
  580. * currently manages.
  581. */
  582. DBusMessage * wpas_dbus_handler_remove_interface(DBusMessage *message,
  583. struct wpa_global *global)
  584. {
  585. struct wpa_supplicant *wpa_s;
  586. char *path;
  587. DBusMessage *reply = NULL;
  588. dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
  589. DBUS_TYPE_INVALID);
  590. wpa_s = get_iface_by_dbus_path(global, path);
  591. if (wpa_s == NULL)
  592. reply = wpas_dbus_error_iface_unknown(message);
  593. else if (wpa_supplicant_remove_iface(global, wpa_s, 0)) {
  594. reply = wpas_dbus_error_unknown_error(
  595. message,
  596. "wpa_supplicant couldn't remove this interface.");
  597. }
  598. return reply;
  599. }
  600. /**
  601. * wpas_dbus_handler_get_interface - Get the object path for an interface name
  602. * @message: Pointer to incoming dbus message
  603. * @global: %wpa_supplicant global data structure
  604. * Returns: The object path of the interface object,
  605. * or a dbus error message with more information
  606. *
  607. * Handler function for "getInterface" method call.
  608. */
  609. DBusMessage * wpas_dbus_handler_get_interface(DBusMessage *message,
  610. struct wpa_global *global)
  611. {
  612. DBusMessage *reply = NULL;
  613. const char *ifname;
  614. const char *path;
  615. struct wpa_supplicant *wpa_s;
  616. dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &ifname,
  617. DBUS_TYPE_INVALID);
  618. wpa_s = wpa_supplicant_get_iface(global, ifname);
  619. if (wpa_s == NULL || wpa_s->dbus_new_path == NULL)
  620. return wpas_dbus_error_iface_unknown(message);
  621. path = wpa_s->dbus_new_path;
  622. reply = dbus_message_new_method_return(message);
  623. if (reply == NULL)
  624. return wpas_dbus_error_no_memory(message);
  625. if (!dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
  626. DBUS_TYPE_INVALID)) {
  627. dbus_message_unref(reply);
  628. return wpas_dbus_error_no_memory(message);
  629. }
  630. return reply;
  631. }
  632. /**
  633. * wpas_dbus_getter_debug_level - Get debug level
  634. * @iter: Pointer to incoming dbus message iter
  635. * @error: Location to store error on failure
  636. * @user_data: Function specific data
  637. * Returns: TRUE on success, FALSE on failure
  638. *
  639. * Getter for "DebugLevel" property.
  640. */
  641. dbus_bool_t wpas_dbus_getter_debug_level(
  642. const struct wpa_dbus_property_desc *property_desc,
  643. DBusMessageIter *iter, DBusError *error, void *user_data)
  644. {
  645. const char *str;
  646. int idx = wpa_debug_level;
  647. if (idx < 0)
  648. idx = 0;
  649. if (idx > 5)
  650. idx = 5;
  651. str = debug_strings[idx];
  652. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  653. &str, error);
  654. }
  655. /**
  656. * wpas_dbus_getter_debug_timestamp - Get debug timestamp
  657. * @iter: Pointer to incoming dbus message iter
  658. * @error: Location to store error on failure
  659. * @user_data: Function specific data
  660. * Returns: TRUE on success, FALSE on failure
  661. *
  662. * Getter for "DebugTimestamp" property.
  663. */
  664. dbus_bool_t wpas_dbus_getter_debug_timestamp(
  665. const struct wpa_dbus_property_desc *property_desc,
  666. DBusMessageIter *iter, DBusError *error, void *user_data)
  667. {
  668. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  669. &wpa_debug_timestamp, error);
  670. }
  671. /**
  672. * wpas_dbus_getter_debug_show_keys - Get debug show keys
  673. * @iter: Pointer to incoming dbus message iter
  674. * @error: Location to store error on failure
  675. * @user_data: Function specific data
  676. * Returns: TRUE on success, FALSE on failure
  677. *
  678. * Getter for "DebugShowKeys" property.
  679. */
  680. dbus_bool_t wpas_dbus_getter_debug_show_keys(
  681. const struct wpa_dbus_property_desc *property_desc,
  682. DBusMessageIter *iter, DBusError *error, void *user_data)
  683. {
  684. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  685. &wpa_debug_show_keys, error);
  686. }
  687. /**
  688. * wpas_dbus_setter_debug_level - Set debug level
  689. * @iter: Pointer to incoming dbus message iter
  690. * @error: Location to store error on failure
  691. * @user_data: Function specific data
  692. * Returns: TRUE on success, FALSE on failure
  693. *
  694. * Setter for "DebugLevel" property.
  695. */
  696. dbus_bool_t wpas_dbus_setter_debug_level(
  697. const struct wpa_dbus_property_desc *property_desc,
  698. DBusMessageIter *iter, DBusError *error, void *user_data)
  699. {
  700. struct wpa_global *global = user_data;
  701. const char *str = NULL;
  702. int i, val = -1;
  703. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_STRING,
  704. &str))
  705. return FALSE;
  706. for (i = 0; debug_strings[i]; i++)
  707. if (os_strcmp(debug_strings[i], str) == 0) {
  708. val = i;
  709. break;
  710. }
  711. if (val < 0 ||
  712. wpa_supplicant_set_debug_params(global, val, wpa_debug_timestamp,
  713. wpa_debug_show_keys)) {
  714. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  715. "wrong debug level value");
  716. return FALSE;
  717. }
  718. return TRUE;
  719. }
  720. /**
  721. * wpas_dbus_setter_debug_timestamp - Set debug timestamp
  722. * @iter: Pointer to incoming dbus message iter
  723. * @error: Location to store error on failure
  724. * @user_data: Function specific data
  725. * Returns: TRUE on success, FALSE on failure
  726. *
  727. * Setter for "DebugTimestamp" property.
  728. */
  729. dbus_bool_t wpas_dbus_setter_debug_timestamp(
  730. const struct wpa_dbus_property_desc *property_desc,
  731. DBusMessageIter *iter, DBusError *error, void *user_data)
  732. {
  733. struct wpa_global *global = user_data;
  734. dbus_bool_t val;
  735. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_BOOLEAN,
  736. &val))
  737. return FALSE;
  738. wpa_supplicant_set_debug_params(global, wpa_debug_level, val ? 1 : 0,
  739. wpa_debug_show_keys);
  740. return TRUE;
  741. }
  742. /**
  743. * wpas_dbus_setter_debug_show_keys - Set debug show keys
  744. * @iter: Pointer to incoming dbus message iter
  745. * @error: Location to store error on failure
  746. * @user_data: Function specific data
  747. * Returns: TRUE on success, FALSE on failure
  748. *
  749. * Setter for "DebugShowKeys" property.
  750. */
  751. dbus_bool_t wpas_dbus_setter_debug_show_keys(
  752. const struct wpa_dbus_property_desc *property_desc,
  753. DBusMessageIter *iter, DBusError *error, void *user_data)
  754. {
  755. struct wpa_global *global = user_data;
  756. dbus_bool_t val;
  757. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_BOOLEAN,
  758. &val))
  759. return FALSE;
  760. wpa_supplicant_set_debug_params(global, wpa_debug_level,
  761. wpa_debug_timestamp,
  762. val ? 1 : 0);
  763. return TRUE;
  764. }
  765. /**
  766. * wpas_dbus_getter_interfaces - Request registered interfaces list
  767. * @iter: Pointer to incoming dbus message iter
  768. * @error: Location to store error on failure
  769. * @user_data: Function specific data
  770. * Returns: TRUE on success, FALSE on failure
  771. *
  772. * Getter for "Interfaces" property. Handles requests
  773. * by dbus clients to return list of registered interfaces objects
  774. * paths
  775. */
  776. dbus_bool_t wpas_dbus_getter_interfaces(
  777. const struct wpa_dbus_property_desc *property_desc,
  778. DBusMessageIter *iter, DBusError *error, void *user_data)
  779. {
  780. struct wpa_global *global = user_data;
  781. struct wpa_supplicant *wpa_s;
  782. const char **paths;
  783. unsigned int i = 0, num = 0;
  784. dbus_bool_t success;
  785. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  786. if (wpa_s->dbus_new_path)
  787. num++;
  788. }
  789. paths = os_calloc(num, sizeof(char *));
  790. if (!paths) {
  791. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  792. return FALSE;
  793. }
  794. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  795. if (wpa_s->dbus_new_path)
  796. paths[i++] = wpa_s->dbus_new_path;
  797. }
  798. success = wpas_dbus_simple_array_property_getter(iter,
  799. DBUS_TYPE_OBJECT_PATH,
  800. paths, num, error);
  801. os_free(paths);
  802. return success;
  803. }
  804. /**
  805. * wpas_dbus_getter_eap_methods - Request supported EAP methods list
  806. * @iter: Pointer to incoming dbus message iter
  807. * @error: Location to store error on failure
  808. * @user_data: Function specific data
  809. * Returns: TRUE on success, FALSE on failure
  810. *
  811. * Getter for "EapMethods" property. Handles requests
  812. * by dbus clients to return list of strings with supported EAP methods
  813. */
  814. dbus_bool_t wpas_dbus_getter_eap_methods(
  815. const struct wpa_dbus_property_desc *property_desc,
  816. DBusMessageIter *iter, DBusError *error, void *user_data)
  817. {
  818. char **eap_methods;
  819. size_t num_items = 0;
  820. dbus_bool_t success;
  821. eap_methods = eap_get_names_as_string_array(&num_items);
  822. if (!eap_methods) {
  823. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  824. return FALSE;
  825. }
  826. success = wpas_dbus_simple_array_property_getter(iter,
  827. DBUS_TYPE_STRING,
  828. eap_methods,
  829. num_items, error);
  830. while (num_items)
  831. os_free(eap_methods[--num_items]);
  832. os_free(eap_methods);
  833. return success;
  834. }
  835. /**
  836. * wpas_dbus_getter_global_capabilities - Request supported global capabilities
  837. * @iter: Pointer to incoming dbus message iter
  838. * @error: Location to store error on failure
  839. * @user_data: Function specific data
  840. * Returns: TRUE on success, FALSE on failure
  841. *
  842. * Getter for "Capabilities" property. Handles requests by dbus clients to
  843. * return a list of strings with supported capabilities like AP, RSN IBSS,
  844. * and P2P that are determined at compile time.
  845. */
  846. dbus_bool_t wpas_dbus_getter_global_capabilities(
  847. const struct wpa_dbus_property_desc *property_desc,
  848. DBusMessageIter *iter, DBusError *error, void *user_data)
  849. {
  850. const char *capabilities[5] = { NULL, NULL, NULL, NULL, NULL };
  851. size_t num_items = 0;
  852. #ifdef CONFIG_AP
  853. capabilities[num_items++] = "ap";
  854. #endif /* CONFIG_AP */
  855. #ifdef CONFIG_IBSS_RSN
  856. capabilities[num_items++] = "ibss-rsn";
  857. #endif /* CONFIG_IBSS_RSN */
  858. #ifdef CONFIG_P2P
  859. capabilities[num_items++] = "p2p";
  860. #endif /* CONFIG_P2P */
  861. #ifdef CONFIG_INTERWORKING
  862. capabilities[num_items++] = "interworking";
  863. #endif /* CONFIG_INTERWORKING */
  864. #ifdef CONFIG_IEEE80211W
  865. capabilities[num_items++] = "pmf";
  866. #endif /* CONFIG_IEEE80211W */
  867. return wpas_dbus_simple_array_property_getter(iter,
  868. DBUS_TYPE_STRING,
  869. capabilities,
  870. num_items, error);
  871. }
  872. static int wpas_dbus_get_scan_type(DBusMessage *message, DBusMessageIter *var,
  873. char **type, DBusMessage **reply)
  874. {
  875. if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_STRING) {
  876. wpa_printf(MSG_DEBUG, "%s[dbus]: Type must be a string",
  877. __func__);
  878. *reply = wpas_dbus_error_invalid_args(
  879. message, "Wrong Type value type. String required");
  880. return -1;
  881. }
  882. dbus_message_iter_get_basic(var, type);
  883. return 0;
  884. }
  885. static int wpas_dbus_get_scan_ssids(DBusMessage *message, DBusMessageIter *var,
  886. struct wpa_driver_scan_params *params,
  887. DBusMessage **reply)
  888. {
  889. struct wpa_driver_scan_ssid *ssids = params->ssids;
  890. size_t ssids_num = 0;
  891. u8 *ssid;
  892. DBusMessageIter array_iter, sub_array_iter;
  893. char *val;
  894. int len;
  895. if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_ARRAY) {
  896. wpa_printf(MSG_DEBUG,
  897. "%s[dbus]: ssids must be an array of arrays of bytes",
  898. __func__);
  899. *reply = wpas_dbus_error_invalid_args(
  900. message,
  901. "Wrong SSIDs value type. Array of arrays of bytes required");
  902. return -1;
  903. }
  904. dbus_message_iter_recurse(var, &array_iter);
  905. if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_ARRAY ||
  906. dbus_message_iter_get_element_type(&array_iter) != DBUS_TYPE_BYTE) {
  907. wpa_printf(MSG_DEBUG,
  908. "%s[dbus]: ssids must be an array of arrays of bytes",
  909. __func__);
  910. *reply = wpas_dbus_error_invalid_args(
  911. message,
  912. "Wrong SSIDs value type. Array of arrays of bytes required");
  913. return -1;
  914. }
  915. while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_ARRAY) {
  916. if (ssids_num >= WPAS_MAX_SCAN_SSIDS) {
  917. wpa_printf(MSG_DEBUG,
  918. "%s[dbus]: Too many ssids specified on scan dbus call",
  919. __func__);
  920. *reply = wpas_dbus_error_invalid_args(
  921. message,
  922. "Too many ssids specified. Specify at most four");
  923. return -1;
  924. }
  925. dbus_message_iter_recurse(&array_iter, &sub_array_iter);
  926. dbus_message_iter_get_fixed_array(&sub_array_iter, &val, &len);
  927. if (len > SSID_MAX_LEN) {
  928. wpa_printf(MSG_DEBUG,
  929. "%s[dbus]: SSID too long (len=%d max_len=%d)",
  930. __func__, len, SSID_MAX_LEN);
  931. *reply = wpas_dbus_error_invalid_args(
  932. message, "Invalid SSID: too long");
  933. return -1;
  934. }
  935. if (len != 0) {
  936. ssid = os_memdup(val, len);
  937. if (ssid == NULL) {
  938. *reply = wpas_dbus_error_no_memory(message);
  939. return -1;
  940. }
  941. } else {
  942. /* Allow zero-length SSIDs */
  943. ssid = NULL;
  944. }
  945. ssids[ssids_num].ssid = ssid;
  946. ssids[ssids_num].ssid_len = len;
  947. dbus_message_iter_next(&array_iter);
  948. ssids_num++;
  949. }
  950. params->num_ssids = ssids_num;
  951. return 0;
  952. }
  953. static int wpas_dbus_get_scan_ies(DBusMessage *message, DBusMessageIter *var,
  954. struct wpa_driver_scan_params *params,
  955. DBusMessage **reply)
  956. {
  957. u8 *ies = NULL, *nies;
  958. int ies_len = 0;
  959. DBusMessageIter array_iter, sub_array_iter;
  960. char *val;
  961. int len;
  962. if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_ARRAY) {
  963. wpa_printf(MSG_DEBUG,
  964. "%s[dbus]: ies must be an array of arrays of bytes",
  965. __func__);
  966. *reply = wpas_dbus_error_invalid_args(
  967. message,
  968. "Wrong IEs value type. Array of arrays of bytes required");
  969. return -1;
  970. }
  971. dbus_message_iter_recurse(var, &array_iter);
  972. if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_ARRAY ||
  973. dbus_message_iter_get_element_type(&array_iter) != DBUS_TYPE_BYTE) {
  974. wpa_printf(MSG_DEBUG,
  975. "%s[dbus]: ies must be an array of arrays of bytes",
  976. __func__);
  977. *reply = wpas_dbus_error_invalid_args(
  978. message, "Wrong IEs value type. Array required");
  979. return -1;
  980. }
  981. while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_ARRAY) {
  982. dbus_message_iter_recurse(&array_iter, &sub_array_iter);
  983. dbus_message_iter_get_fixed_array(&sub_array_iter, &val, &len);
  984. if (len == 0) {
  985. dbus_message_iter_next(&array_iter);
  986. continue;
  987. }
  988. nies = os_realloc(ies, ies_len + len);
  989. if (nies == NULL) {
  990. os_free(ies);
  991. *reply = wpas_dbus_error_no_memory(message);
  992. return -1;
  993. }
  994. ies = nies;
  995. os_memcpy(ies + ies_len, val, len);
  996. ies_len += len;
  997. dbus_message_iter_next(&array_iter);
  998. }
  999. params->extra_ies = ies;
  1000. params->extra_ies_len = ies_len;
  1001. return 0;
  1002. }
  1003. static int wpas_dbus_get_scan_channels(DBusMessage *message,
  1004. DBusMessageIter *var,
  1005. struct wpa_driver_scan_params *params,
  1006. DBusMessage **reply)
  1007. {
  1008. DBusMessageIter array_iter, sub_array_iter;
  1009. int *freqs = NULL, *nfreqs;
  1010. int freqs_num = 0;
  1011. if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_ARRAY) {
  1012. wpa_printf(MSG_DEBUG,
  1013. "%s[dbus]: Channels must be an array of structs",
  1014. __func__);
  1015. *reply = wpas_dbus_error_invalid_args(
  1016. message,
  1017. "Wrong Channels value type. Array of structs required");
  1018. return -1;
  1019. }
  1020. dbus_message_iter_recurse(var, &array_iter);
  1021. if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_STRUCT) {
  1022. wpa_printf(MSG_DEBUG,
  1023. "%s[dbus]: Channels must be an array of structs",
  1024. __func__);
  1025. *reply = wpas_dbus_error_invalid_args(
  1026. message,
  1027. "Wrong Channels value type. Array of structs required");
  1028. return -1;
  1029. }
  1030. while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_STRUCT)
  1031. {
  1032. int freq, width;
  1033. dbus_message_iter_recurse(&array_iter, &sub_array_iter);
  1034. if (dbus_message_iter_get_arg_type(&sub_array_iter) !=
  1035. DBUS_TYPE_UINT32) {
  1036. wpa_printf(MSG_DEBUG,
  1037. "%s[dbus]: Channel must by specified by struct of two UINT32s %c",
  1038. __func__,
  1039. dbus_message_iter_get_arg_type(
  1040. &sub_array_iter));
  1041. *reply = wpas_dbus_error_invalid_args(
  1042. message,
  1043. "Wrong Channel struct. Two UINT32s required");
  1044. os_free(freqs);
  1045. return -1;
  1046. }
  1047. dbus_message_iter_get_basic(&sub_array_iter, &freq);
  1048. if (!dbus_message_iter_next(&sub_array_iter) ||
  1049. dbus_message_iter_get_arg_type(&sub_array_iter) !=
  1050. DBUS_TYPE_UINT32) {
  1051. wpa_printf(MSG_DEBUG,
  1052. "%s[dbus]: Channel must by specified by struct of two UINT32s",
  1053. __func__);
  1054. *reply = wpas_dbus_error_invalid_args(
  1055. message,
  1056. "Wrong Channel struct. Two UINT32s required");
  1057. os_free(freqs);
  1058. return -1;
  1059. }
  1060. dbus_message_iter_get_basic(&sub_array_iter, &width);
  1061. #define FREQS_ALLOC_CHUNK 32
  1062. if (freqs_num % FREQS_ALLOC_CHUNK == 0) {
  1063. nfreqs = os_realloc_array(
  1064. freqs, freqs_num + FREQS_ALLOC_CHUNK,
  1065. sizeof(int));
  1066. if (nfreqs == NULL)
  1067. os_free(freqs);
  1068. freqs = nfreqs;
  1069. }
  1070. if (freqs == NULL) {
  1071. *reply = wpas_dbus_error_no_memory(message);
  1072. return -1;
  1073. }
  1074. freqs[freqs_num] = freq;
  1075. freqs_num++;
  1076. dbus_message_iter_next(&array_iter);
  1077. }
  1078. nfreqs = os_realloc_array(freqs, freqs_num + 1, sizeof(int));
  1079. if (nfreqs == NULL)
  1080. os_free(freqs);
  1081. freqs = nfreqs;
  1082. if (freqs == NULL) {
  1083. *reply = wpas_dbus_error_no_memory(message);
  1084. return -1;
  1085. }
  1086. freqs[freqs_num] = 0;
  1087. params->freqs = freqs;
  1088. return 0;
  1089. }
  1090. static int wpas_dbus_get_scan_allow_roam(DBusMessage *message,
  1091. DBusMessageIter *var,
  1092. dbus_bool_t *allow,
  1093. DBusMessage **reply)
  1094. {
  1095. if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_BOOLEAN) {
  1096. wpa_printf(MSG_DEBUG, "%s[dbus]: Type must be a boolean",
  1097. __func__);
  1098. *reply = wpas_dbus_error_invalid_args(
  1099. message, "Wrong Type value type. Boolean required");
  1100. return -1;
  1101. }
  1102. dbus_message_iter_get_basic(var, allow);
  1103. return 0;
  1104. }
  1105. /**
  1106. * wpas_dbus_handler_scan - Request a wireless scan on an interface
  1107. * @message: Pointer to incoming dbus message
  1108. * @wpa_s: wpa_supplicant structure for a network interface
  1109. * Returns: NULL indicating success or DBus error message on failure
  1110. *
  1111. * Handler function for "Scan" method call of a network device. Requests
  1112. * that wpa_supplicant perform a wireless scan as soon as possible
  1113. * on a particular wireless interface.
  1114. */
  1115. DBusMessage * wpas_dbus_handler_scan(DBusMessage *message,
  1116. struct wpa_supplicant *wpa_s)
  1117. {
  1118. DBusMessage *reply = NULL;
  1119. DBusMessageIter iter, dict_iter, entry_iter, variant_iter;
  1120. char *key = NULL, *type = NULL;
  1121. struct wpa_driver_scan_params params;
  1122. size_t i;
  1123. dbus_bool_t allow_roam = 1;
  1124. os_memset(&params, 0, sizeof(params));
  1125. dbus_message_iter_init(message, &iter);
  1126. dbus_message_iter_recurse(&iter, &dict_iter);
  1127. while (dbus_message_iter_get_arg_type(&dict_iter) ==
  1128. DBUS_TYPE_DICT_ENTRY) {
  1129. dbus_message_iter_recurse(&dict_iter, &entry_iter);
  1130. dbus_message_iter_get_basic(&entry_iter, &key);
  1131. dbus_message_iter_next(&entry_iter);
  1132. dbus_message_iter_recurse(&entry_iter, &variant_iter);
  1133. if (os_strcmp(key, "Type") == 0) {
  1134. if (wpas_dbus_get_scan_type(message, &variant_iter,
  1135. &type, &reply) < 0)
  1136. goto out;
  1137. } else if (os_strcmp(key, "SSIDs") == 0) {
  1138. if (wpas_dbus_get_scan_ssids(message, &variant_iter,
  1139. &params, &reply) < 0)
  1140. goto out;
  1141. } else if (os_strcmp(key, "IEs") == 0) {
  1142. if (wpas_dbus_get_scan_ies(message, &variant_iter,
  1143. &params, &reply) < 0)
  1144. goto out;
  1145. } else if (os_strcmp(key, "Channels") == 0) {
  1146. if (wpas_dbus_get_scan_channels(message, &variant_iter,
  1147. &params, &reply) < 0)
  1148. goto out;
  1149. } else if (os_strcmp(key, "AllowRoam") == 0) {
  1150. if (wpas_dbus_get_scan_allow_roam(message,
  1151. &variant_iter,
  1152. &allow_roam,
  1153. &reply) < 0)
  1154. goto out;
  1155. } else {
  1156. wpa_printf(MSG_DEBUG, "%s[dbus]: Unknown argument %s",
  1157. __func__, key);
  1158. reply = wpas_dbus_error_invalid_args(message, key);
  1159. goto out;
  1160. }
  1161. dbus_message_iter_next(&dict_iter);
  1162. }
  1163. if (!type) {
  1164. wpa_printf(MSG_DEBUG, "%s[dbus]: Scan type not specified",
  1165. __func__);
  1166. reply = wpas_dbus_error_invalid_args(message, key);
  1167. goto out;
  1168. }
  1169. if (os_strcmp(type, "passive") == 0) {
  1170. if (params.num_ssids || params.extra_ies_len) {
  1171. wpa_printf(MSG_DEBUG,
  1172. "%s[dbus]: SSIDs or IEs specified for passive scan.",
  1173. __func__);
  1174. reply = wpas_dbus_error_invalid_args(
  1175. message,
  1176. "You can specify only Channels in passive scan");
  1177. goto out;
  1178. } else {
  1179. if (wpa_s->sched_scanning) {
  1180. wpa_printf(MSG_DEBUG,
  1181. "%s[dbus]: Stop ongoing sched_scan to allow requested scan to proceed",
  1182. __func__);
  1183. wpa_supplicant_cancel_sched_scan(wpa_s);
  1184. }
  1185. if (params.freqs && params.freqs[0]) {
  1186. wpa_s->last_scan_req = MANUAL_SCAN_REQ;
  1187. if (wpa_supplicant_trigger_scan(wpa_s,
  1188. &params)) {
  1189. reply = wpas_dbus_error_scan_error(
  1190. message,
  1191. "Scan request rejected");
  1192. }
  1193. } else {
  1194. wpa_s->scan_req = MANUAL_SCAN_REQ;
  1195. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1196. }
  1197. }
  1198. } else if (os_strcmp(type, "active") == 0) {
  1199. if (!params.num_ssids) {
  1200. /* Add wildcard ssid */
  1201. params.num_ssids++;
  1202. }
  1203. #ifdef CONFIG_AUTOSCAN
  1204. autoscan_deinit(wpa_s);
  1205. #endif /* CONFIG_AUTOSCAN */
  1206. if (wpa_s->sched_scanning) {
  1207. wpa_printf(MSG_DEBUG,
  1208. "%s[dbus]: Stop ongoing sched_scan to allow requested scan to proceed",
  1209. __func__);
  1210. wpa_supplicant_cancel_sched_scan(wpa_s);
  1211. }
  1212. wpa_s->last_scan_req = MANUAL_SCAN_REQ;
  1213. if (wpa_supplicant_trigger_scan(wpa_s, &params)) {
  1214. reply = wpas_dbus_error_scan_error(
  1215. message, "Scan request rejected");
  1216. }
  1217. } else {
  1218. wpa_printf(MSG_DEBUG, "%s[dbus]: Unknown scan type: %s",
  1219. __func__, type);
  1220. reply = wpas_dbus_error_invalid_args(message,
  1221. "Wrong scan type");
  1222. goto out;
  1223. }
  1224. if (!allow_roam)
  1225. wpa_s->scan_res_handler = scan_only_handler;
  1226. out:
  1227. for (i = 0; i < WPAS_MAX_SCAN_SSIDS; i++)
  1228. os_free((u8 *) params.ssids[i].ssid);
  1229. os_free((u8 *) params.extra_ies);
  1230. os_free(params.freqs);
  1231. return reply;
  1232. }
  1233. /*
  1234. * wpas_dbus_handler_abort_scan - Request an ongoing scan to be aborted
  1235. * @message: Pointer to incoming dbus message
  1236. * @wpa_s: wpa_supplicant structure for a network interface
  1237. * Returns: Abort failed or no scan in progress DBus error message on failure
  1238. * or NULL otherwise.
  1239. *
  1240. * Handler function for "AbortScan" method call of network interface.
  1241. */
  1242. DBusMessage * wpas_dbus_handler_abort_scan(DBusMessage *message,
  1243. struct wpa_supplicant *wpa_s)
  1244. {
  1245. if (wpas_abort_ongoing_scan(wpa_s) < 0)
  1246. return dbus_message_new_error(
  1247. message, WPAS_DBUS_ERROR_IFACE_SCAN_ERROR,
  1248. "Abort failed or no scan in progress");
  1249. return NULL;
  1250. }
  1251. /**
  1252. * wpas_dbus_handler_signal_poll - Request immediate signal properties
  1253. * @message: Pointer to incoming dbus message
  1254. * @wpa_s: wpa_supplicant structure for a network interface
  1255. * Returns: NULL indicating success or DBus error message on failure
  1256. *
  1257. * Handler function for "SignalPoll" method call of a network device. Requests
  1258. * that wpa_supplicant read signal properties like RSSI, noise, and link
  1259. * speed and return them.
  1260. */
  1261. DBusMessage * wpas_dbus_handler_signal_poll(DBusMessage *message,
  1262. struct wpa_supplicant *wpa_s)
  1263. {
  1264. struct wpa_signal_info si;
  1265. DBusMessage *reply = NULL;
  1266. DBusMessageIter iter, iter_dict, variant_iter;
  1267. int ret;
  1268. ret = wpa_drv_signal_poll(wpa_s, &si);
  1269. if (ret) {
  1270. return dbus_message_new_error(message, DBUS_ERROR_FAILED,
  1271. "Failed to read signal");
  1272. }
  1273. reply = dbus_message_new_method_return(message);
  1274. if (reply == NULL)
  1275. goto nomem;
  1276. dbus_message_iter_init_append(reply, &iter);
  1277. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
  1278. "a{sv}", &variant_iter) ||
  1279. !wpa_dbus_dict_open_write(&variant_iter, &iter_dict) ||
  1280. !wpa_dbus_dict_append_int32(&iter_dict, "rssi",
  1281. si.current_signal) ||
  1282. !wpa_dbus_dict_append_int32(&iter_dict, "linkspeed",
  1283. si.current_txrate / 1000) ||
  1284. !wpa_dbus_dict_append_int32(&iter_dict, "noise",
  1285. si.current_noise) ||
  1286. !wpa_dbus_dict_append_uint32(&iter_dict, "frequency",
  1287. si.frequency) ||
  1288. (si.chanwidth != CHAN_WIDTH_UNKNOWN &&
  1289. !wpa_dbus_dict_append_string(
  1290. &iter_dict, "width",
  1291. channel_width_to_string(si.chanwidth))) ||
  1292. (si.center_frq1 > 0 && si.center_frq2 > 0 &&
  1293. (!wpa_dbus_dict_append_int32(&iter_dict, "center-frq1",
  1294. si.center_frq1) ||
  1295. !wpa_dbus_dict_append_int32(&iter_dict, "center-frq2",
  1296. si.center_frq2))) ||
  1297. (si.avg_signal &&
  1298. !wpa_dbus_dict_append_int32(&iter_dict, "avg-rssi",
  1299. si.avg_signal)) ||
  1300. !wpa_dbus_dict_close_write(&variant_iter, &iter_dict) ||
  1301. !dbus_message_iter_close_container(&iter, &variant_iter))
  1302. goto nomem;
  1303. return reply;
  1304. nomem:
  1305. if (reply)
  1306. dbus_message_unref(reply);
  1307. return wpas_dbus_error_no_memory(message);
  1308. }
  1309. /*
  1310. * wpas_dbus_handler_disconnect - Terminate the current connection
  1311. * @message: Pointer to incoming dbus message
  1312. * @wpa_s: wpa_supplicant structure for a network interface
  1313. * Returns: NotConnected DBus error message if already not connected
  1314. * or NULL otherwise.
  1315. *
  1316. * Handler function for "Disconnect" method call of network interface.
  1317. */
  1318. DBusMessage * wpas_dbus_handler_disconnect(DBusMessage *message,
  1319. struct wpa_supplicant *wpa_s)
  1320. {
  1321. if (wpa_s->current_ssid != NULL) {
  1322. wpas_request_disconnection(wpa_s);
  1323. return NULL;
  1324. }
  1325. return dbus_message_new_error(message, WPAS_DBUS_ERROR_NOT_CONNECTED,
  1326. "This interface is not connected");
  1327. }
  1328. /**
  1329. * wpas_dbus_new_iface_add_network - Add a new configured network
  1330. * @message: Pointer to incoming dbus message
  1331. * @wpa_s: wpa_supplicant structure for a network interface
  1332. * Returns: A dbus message containing the object path of the new network
  1333. *
  1334. * Handler function for "AddNetwork" method call of a network interface.
  1335. */
  1336. DBusMessage * wpas_dbus_handler_add_network(DBusMessage *message,
  1337. struct wpa_supplicant *wpa_s)
  1338. {
  1339. DBusMessage *reply = NULL;
  1340. DBusMessageIter iter;
  1341. struct wpa_ssid *ssid = NULL;
  1342. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *path = path_buf;
  1343. DBusError error;
  1344. dbus_message_iter_init(message, &iter);
  1345. if (wpa_s->dbus_new_path)
  1346. ssid = wpa_supplicant_add_network(wpa_s);
  1347. if (ssid == NULL) {
  1348. wpa_printf(MSG_ERROR, "%s[dbus]: can't add new interface.",
  1349. __func__);
  1350. reply = wpas_dbus_error_unknown_error(
  1351. message,
  1352. "wpa_supplicant could not add a network on this interface.");
  1353. goto err;
  1354. }
  1355. dbus_error_init(&error);
  1356. if (!set_network_properties(wpa_s, ssid, &iter, &error)) {
  1357. wpa_printf(MSG_DEBUG,
  1358. "%s[dbus]: control interface couldn't set network properties",
  1359. __func__);
  1360. reply = wpas_dbus_reply_new_from_error(message, &error,
  1361. DBUS_ERROR_INVALID_ARGS,
  1362. "Failed to add network");
  1363. dbus_error_free(&error);
  1364. goto err;
  1365. }
  1366. /* Construct the object path for this network. */
  1367. os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
  1368. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%d",
  1369. wpa_s->dbus_new_path, ssid->id);
  1370. reply = dbus_message_new_method_return(message);
  1371. if (reply == NULL) {
  1372. reply = wpas_dbus_error_no_memory(message);
  1373. goto err;
  1374. }
  1375. if (!dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
  1376. DBUS_TYPE_INVALID)) {
  1377. dbus_message_unref(reply);
  1378. reply = wpas_dbus_error_no_memory(message);
  1379. goto err;
  1380. }
  1381. return reply;
  1382. err:
  1383. if (ssid) {
  1384. wpas_notify_network_removed(wpa_s, ssid);
  1385. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1386. }
  1387. return reply;
  1388. }
  1389. /**
  1390. * wpas_dbus_handler_reassociate - Reassociate
  1391. * @message: Pointer to incoming dbus message
  1392. * @wpa_s: wpa_supplicant structure for a network interface
  1393. * Returns: InterfaceDisabled DBus error message if disabled
  1394. * or NULL otherwise.
  1395. *
  1396. * Handler function for "Reassociate" method call of network interface.
  1397. */
  1398. DBusMessage * wpas_dbus_handler_reassociate(DBusMessage *message,
  1399. struct wpa_supplicant *wpa_s)
  1400. {
  1401. if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED) {
  1402. wpas_request_connection(wpa_s);
  1403. return NULL;
  1404. }
  1405. return dbus_message_new_error(message, WPAS_DBUS_ERROR_IFACE_DISABLED,
  1406. "This interface is disabled");
  1407. }
  1408. /**
  1409. * wpas_dbus_handler_expect_disconnect - ExpectDisconnect
  1410. * @message: Pointer to incoming dbus message
  1411. * @global: %wpa_supplicant global data structure
  1412. * Returns: NULL
  1413. *
  1414. * Handler function for notifying system there will be a expected disconnect.
  1415. * This will prevent wpa_supplicant from adding blacklists upon next disconnect..
  1416. */
  1417. DBusMessage * wpas_dbus_handler_expect_disconnect(DBusMessage *message,
  1418. struct wpa_global *global)
  1419. {
  1420. struct wpa_supplicant *wpa_s = global->ifaces;
  1421. for (; wpa_s; wpa_s = wpa_s->next)
  1422. if (wpa_s->wpa_state >= WPA_ASSOCIATED)
  1423. wpa_s->own_disconnect_req = 1;
  1424. return NULL;
  1425. }
  1426. /**
  1427. * wpas_dbus_handler_reattach - Reattach to current AP
  1428. * @message: Pointer to incoming dbus message
  1429. * @wpa_s: wpa_supplicant structure for a network interface
  1430. * Returns: NotConnected DBus error message if not connected
  1431. * or NULL otherwise.
  1432. *
  1433. * Handler function for "Reattach" method call of network interface.
  1434. */
  1435. DBusMessage * wpas_dbus_handler_reattach(DBusMessage *message,
  1436. struct wpa_supplicant *wpa_s)
  1437. {
  1438. if (wpa_s->current_ssid != NULL) {
  1439. wpa_s->reattach = 1;
  1440. wpas_request_connection(wpa_s);
  1441. return NULL;
  1442. }
  1443. return dbus_message_new_error(message, WPAS_DBUS_ERROR_NOT_CONNECTED,
  1444. "This interface is not connected");
  1445. }
  1446. /**
  1447. * wpas_dbus_handler_reconnect - Reconnect if disconnected
  1448. * @message: Pointer to incoming dbus message
  1449. * @wpa_s: wpa_supplicant structure for a network interface
  1450. * Returns: InterfaceDisabled DBus error message if disabled
  1451. * or NULL otherwise.
  1452. *
  1453. * Handler function for "Reconnect" method call of network interface.
  1454. */
  1455. DBusMessage * wpas_dbus_handler_reconnect(DBusMessage *message,
  1456. struct wpa_supplicant *wpa_s)
  1457. {
  1458. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  1459. return dbus_message_new_error(message,
  1460. WPAS_DBUS_ERROR_IFACE_DISABLED,
  1461. "This interface is disabled");
  1462. }
  1463. if (wpa_s->disconnected)
  1464. wpas_request_connection(wpa_s);
  1465. return NULL;
  1466. }
  1467. /**
  1468. * wpas_dbus_handler_remove_network - Remove a configured network
  1469. * @message: Pointer to incoming dbus message
  1470. * @wpa_s: wpa_supplicant structure for a network interface
  1471. * Returns: NULL on success or dbus error on failure
  1472. *
  1473. * Handler function for "RemoveNetwork" method call of a network interface.
  1474. */
  1475. DBusMessage * wpas_dbus_handler_remove_network(DBusMessage *message,
  1476. struct wpa_supplicant *wpa_s)
  1477. {
  1478. DBusMessage *reply = NULL;
  1479. const char *op;
  1480. char *iface, *net_id;
  1481. int id;
  1482. int result;
  1483. dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &op,
  1484. DBUS_TYPE_INVALID);
  1485. /* Extract the network ID and ensure the network */
  1486. /* is actually a child of this interface */
  1487. iface = wpas_dbus_new_decompose_object_path(op,
  1488. WPAS_DBUS_NEW_NETWORKS_PART,
  1489. &net_id);
  1490. if (iface == NULL || net_id == NULL || !wpa_s->dbus_new_path ||
  1491. os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
  1492. reply = wpas_dbus_error_invalid_args(message, op);
  1493. goto out;
  1494. }
  1495. errno = 0;
  1496. id = strtoul(net_id, NULL, 10);
  1497. if (errno != 0) {
  1498. reply = wpas_dbus_error_invalid_args(message, op);
  1499. goto out;
  1500. }
  1501. result = wpa_supplicant_remove_network(wpa_s, id);
  1502. if (result == -1) {
  1503. reply = wpas_dbus_error_network_unknown(message);
  1504. goto out;
  1505. }
  1506. if (result == -2) {
  1507. wpa_printf(MSG_ERROR,
  1508. "%s[dbus]: error occurred when removing network %d",
  1509. __func__, id);
  1510. reply = wpas_dbus_error_unknown_error(
  1511. message,
  1512. "error removing the specified network on is interface.");
  1513. goto out;
  1514. }
  1515. out:
  1516. os_free(iface);
  1517. return reply;
  1518. }
  1519. static void remove_network(void *arg, struct wpa_ssid *ssid)
  1520. {
  1521. struct wpa_supplicant *wpa_s = arg;
  1522. wpas_notify_network_removed(wpa_s, ssid);
  1523. if (wpa_config_remove_network(wpa_s->conf, ssid->id) < 0) {
  1524. wpa_printf(MSG_ERROR,
  1525. "%s[dbus]: error occurred when removing network %d",
  1526. __func__, ssid->id);
  1527. return;
  1528. }
  1529. if (ssid == wpa_s->current_ssid)
  1530. wpa_supplicant_deauthenticate(wpa_s,
  1531. WLAN_REASON_DEAUTH_LEAVING);
  1532. }
  1533. /**
  1534. * wpas_dbus_handler_remove_all_networks - Remove all configured networks
  1535. * @message: Pointer to incoming dbus message
  1536. * @wpa_s: wpa_supplicant structure for a network interface
  1537. * Returns: NULL on success or dbus error on failure
  1538. *
  1539. * Handler function for "RemoveAllNetworks" method call of a network interface.
  1540. */
  1541. DBusMessage * wpas_dbus_handler_remove_all_networks(
  1542. DBusMessage *message, struct wpa_supplicant *wpa_s)
  1543. {
  1544. if (wpa_s->sched_scanning)
  1545. wpa_supplicant_cancel_sched_scan(wpa_s);
  1546. /* NB: could check for failure and return an error */
  1547. wpa_config_foreach_network(wpa_s->conf, remove_network, wpa_s);
  1548. return NULL;
  1549. }
  1550. /**
  1551. * wpas_dbus_handler_select_network - Attempt association with a network
  1552. * @message: Pointer to incoming dbus message
  1553. * @wpa_s: wpa_supplicant structure for a network interface
  1554. * Returns: NULL on success or dbus error on failure
  1555. *
  1556. * Handler function for "SelectNetwork" method call of network interface.
  1557. */
  1558. DBusMessage * wpas_dbus_handler_select_network(DBusMessage *message,
  1559. struct wpa_supplicant *wpa_s)
  1560. {
  1561. DBusMessage *reply = NULL;
  1562. const char *op;
  1563. char *iface, *net_id;
  1564. int id;
  1565. struct wpa_ssid *ssid;
  1566. dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &op,
  1567. DBUS_TYPE_INVALID);
  1568. /* Extract the network ID and ensure the network */
  1569. /* is actually a child of this interface */
  1570. iface = wpas_dbus_new_decompose_object_path(op,
  1571. WPAS_DBUS_NEW_NETWORKS_PART,
  1572. &net_id);
  1573. if (iface == NULL || net_id == NULL || !wpa_s->dbus_new_path ||
  1574. os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
  1575. reply = wpas_dbus_error_invalid_args(message, op);
  1576. goto out;
  1577. }
  1578. errno = 0;
  1579. id = strtoul(net_id, NULL, 10);
  1580. if (errno != 0) {
  1581. reply = wpas_dbus_error_invalid_args(message, op);
  1582. goto out;
  1583. }
  1584. ssid = wpa_config_get_network(wpa_s->conf, id);
  1585. if (ssid == NULL) {
  1586. reply = wpas_dbus_error_network_unknown(message);
  1587. goto out;
  1588. }
  1589. /* Finally, associate with the network */
  1590. wpa_supplicant_select_network(wpa_s, ssid);
  1591. out:
  1592. os_free(iface);
  1593. return reply;
  1594. }
  1595. /**
  1596. * wpas_dbus_handler_network_reply - Reply to a NetworkRequest signal
  1597. * @message: Pointer to incoming dbus message
  1598. * @wpa_s: wpa_supplicant structure for a network interface
  1599. * Returns: NULL on success or dbus error on failure
  1600. *
  1601. * Handler function for "NetworkReply" method call of network interface.
  1602. */
  1603. DBusMessage * wpas_dbus_handler_network_reply(DBusMessage *message,
  1604. struct wpa_supplicant *wpa_s)
  1605. {
  1606. #ifdef IEEE8021X_EAPOL
  1607. DBusMessage *reply = NULL;
  1608. const char *op, *field, *value;
  1609. char *iface, *net_id;
  1610. int id;
  1611. struct wpa_ssid *ssid;
  1612. if (!dbus_message_get_args(message, NULL,
  1613. DBUS_TYPE_OBJECT_PATH, &op,
  1614. DBUS_TYPE_STRING, &field,
  1615. DBUS_TYPE_STRING, &value,
  1616. DBUS_TYPE_INVALID))
  1617. return wpas_dbus_error_invalid_args(message, NULL);
  1618. /* Extract the network ID and ensure the network */
  1619. /* is actually a child of this interface */
  1620. iface = wpas_dbus_new_decompose_object_path(op,
  1621. WPAS_DBUS_NEW_NETWORKS_PART,
  1622. &net_id);
  1623. if (iface == NULL || net_id == NULL || !wpa_s->dbus_new_path ||
  1624. os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
  1625. reply = wpas_dbus_error_invalid_args(message, op);
  1626. goto out;
  1627. }
  1628. errno = 0;
  1629. id = strtoul(net_id, NULL, 10);
  1630. if (errno != 0) {
  1631. reply = wpas_dbus_error_invalid_args(message, net_id);
  1632. goto out;
  1633. }
  1634. ssid = wpa_config_get_network(wpa_s->conf, id);
  1635. if (ssid == NULL) {
  1636. reply = wpas_dbus_error_network_unknown(message);
  1637. goto out;
  1638. }
  1639. if (wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid,
  1640. field, value) < 0)
  1641. reply = wpas_dbus_error_invalid_args(message, field);
  1642. else {
  1643. /* Tell EAP to retry immediately */
  1644. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  1645. }
  1646. out:
  1647. os_free(iface);
  1648. return reply;
  1649. #else /* IEEE8021X_EAPOL */
  1650. wpa_printf(MSG_DEBUG, "dbus: 802.1X not included");
  1651. return wpas_dbus_error_unknown_error(message, "802.1X not included");
  1652. #endif /* IEEE8021X_EAPOL */
  1653. }
  1654. #ifndef CONFIG_NO_CONFIG_BLOBS
  1655. /**
  1656. * wpas_dbus_handler_add_blob - Store named binary blob (ie, for certificates)
  1657. * @message: Pointer to incoming dbus message
  1658. * @wpa_s: %wpa_supplicant data structure
  1659. * Returns: A dbus message containing an error on failure or NULL on success
  1660. *
  1661. * Asks wpa_supplicant to internally store a binary blobs.
  1662. */
  1663. DBusMessage * wpas_dbus_handler_add_blob(DBusMessage *message,
  1664. struct wpa_supplicant *wpa_s)
  1665. {
  1666. DBusMessage *reply = NULL;
  1667. DBusMessageIter iter, array_iter;
  1668. char *blob_name;
  1669. u8 *blob_data;
  1670. int blob_len;
  1671. struct wpa_config_blob *blob = NULL;
  1672. dbus_message_iter_init(message, &iter);
  1673. dbus_message_iter_get_basic(&iter, &blob_name);
  1674. if (wpa_config_get_blob(wpa_s->conf, blob_name)) {
  1675. return dbus_message_new_error(message,
  1676. WPAS_DBUS_ERROR_BLOB_EXISTS,
  1677. NULL);
  1678. }
  1679. dbus_message_iter_next(&iter);
  1680. dbus_message_iter_recurse(&iter, &array_iter);
  1681. dbus_message_iter_get_fixed_array(&array_iter, &blob_data, &blob_len);
  1682. blob = os_zalloc(sizeof(*blob));
  1683. if (!blob) {
  1684. reply = wpas_dbus_error_no_memory(message);
  1685. goto err;
  1686. }
  1687. blob->data = os_memdup(blob_data, blob_len);
  1688. blob->name = os_strdup(blob_name);
  1689. if (!blob->data || !blob->name) {
  1690. reply = wpas_dbus_error_no_memory(message);
  1691. goto err;
  1692. }
  1693. blob->len = blob_len;
  1694. wpa_config_set_blob(wpa_s->conf, blob);
  1695. wpas_notify_blob_added(wpa_s, blob->name);
  1696. return reply;
  1697. err:
  1698. if (blob) {
  1699. os_free(blob->name);
  1700. os_free(blob->data);
  1701. os_free(blob);
  1702. }
  1703. return reply;
  1704. }
  1705. /**
  1706. * wpas_dbus_handler_get_blob - Get named binary blob (ie, for certificates)
  1707. * @message: Pointer to incoming dbus message
  1708. * @wpa_s: %wpa_supplicant data structure
  1709. * Returns: A dbus message containing array of bytes (blob)
  1710. *
  1711. * Gets one wpa_supplicant's binary blobs.
  1712. */
  1713. DBusMessage * wpas_dbus_handler_get_blob(DBusMessage *message,
  1714. struct wpa_supplicant *wpa_s)
  1715. {
  1716. DBusMessage *reply = NULL;
  1717. DBusMessageIter iter, array_iter;
  1718. char *blob_name;
  1719. const struct wpa_config_blob *blob;
  1720. dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &blob_name,
  1721. DBUS_TYPE_INVALID);
  1722. blob = wpa_config_get_blob(wpa_s->conf, blob_name);
  1723. if (!blob) {
  1724. return dbus_message_new_error(message,
  1725. WPAS_DBUS_ERROR_BLOB_UNKNOWN,
  1726. "Blob id not set");
  1727. }
  1728. reply = dbus_message_new_method_return(message);
  1729. if (!reply)
  1730. return wpas_dbus_error_no_memory(message);
  1731. dbus_message_iter_init_append(reply, &iter);
  1732. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
  1733. DBUS_TYPE_BYTE_AS_STRING,
  1734. &array_iter) ||
  1735. !dbus_message_iter_append_fixed_array(&array_iter, DBUS_TYPE_BYTE,
  1736. &(blob->data), blob->len) ||
  1737. !dbus_message_iter_close_container(&iter, &array_iter)) {
  1738. dbus_message_unref(reply);
  1739. reply = wpas_dbus_error_no_memory(message);
  1740. }
  1741. return reply;
  1742. }
  1743. /**
  1744. * wpas_remove_handler_remove_blob - Remove named binary blob
  1745. * @message: Pointer to incoming dbus message
  1746. * @wpa_s: %wpa_supplicant data structure
  1747. * Returns: NULL on success or dbus error
  1748. *
  1749. * Asks wpa_supplicant to internally remove a binary blobs.
  1750. */
  1751. DBusMessage * wpas_dbus_handler_remove_blob(DBusMessage *message,
  1752. struct wpa_supplicant *wpa_s)
  1753. {
  1754. DBusMessage *reply = NULL;
  1755. char *blob_name;
  1756. dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &blob_name,
  1757. DBUS_TYPE_INVALID);
  1758. if (wpa_config_remove_blob(wpa_s->conf, blob_name)) {
  1759. return dbus_message_new_error(message,
  1760. WPAS_DBUS_ERROR_BLOB_UNKNOWN,
  1761. "Blob id not set");
  1762. }
  1763. wpas_notify_blob_removed(wpa_s, blob_name);
  1764. return reply;
  1765. }
  1766. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1767. /*
  1768. * wpas_dbus_handler_flush_bss - Flush the BSS cache
  1769. * @message: Pointer to incoming dbus message
  1770. * @wpa_s: wpa_supplicant structure for a network interface
  1771. * Returns: NULL
  1772. *
  1773. * Handler function for "FlushBSS" method call of network interface.
  1774. */
  1775. DBusMessage * wpas_dbus_handler_flush_bss(DBusMessage *message,
  1776. struct wpa_supplicant *wpa_s)
  1777. {
  1778. dbus_uint32_t age;
  1779. dbus_message_get_args(message, NULL, DBUS_TYPE_UINT32, &age,
  1780. DBUS_TYPE_INVALID);
  1781. if (age == 0)
  1782. wpa_bss_flush(wpa_s);
  1783. else
  1784. wpa_bss_flush_by_age(wpa_s, age);
  1785. return NULL;
  1786. }
  1787. #ifdef CONFIG_AUTOSCAN
  1788. /**
  1789. * wpas_dbus_handler_autoscan - Set autoscan parameters for the interface
  1790. * @message: Pointer to incoming dbus message
  1791. * @wpa_s: wpa_supplicant structure for a network interface
  1792. * Returns: NULL
  1793. *
  1794. * Handler function for "AutoScan" method call of network interface.
  1795. */
  1796. DBusMessage * wpas_dbus_handler_autoscan(DBusMessage *message,
  1797. struct wpa_supplicant *wpa_s)
  1798. {
  1799. DBusMessage *reply = NULL;
  1800. enum wpa_states state = wpa_s->wpa_state;
  1801. char *arg;
  1802. dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg,
  1803. DBUS_TYPE_INVALID);
  1804. if (arg != NULL && os_strlen(arg) > 0) {
  1805. char *tmp;
  1806. tmp = os_strdup(arg);
  1807. if (tmp == NULL) {
  1808. reply = wpas_dbus_error_no_memory(message);
  1809. } else {
  1810. os_free(wpa_s->conf->autoscan);
  1811. wpa_s->conf->autoscan = tmp;
  1812. if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  1813. autoscan_init(wpa_s, 1);
  1814. else if (state == WPA_SCANNING)
  1815. wpa_supplicant_reinit_autoscan(wpa_s);
  1816. }
  1817. } else if (arg != NULL && os_strlen(arg) == 0) {
  1818. os_free(wpa_s->conf->autoscan);
  1819. wpa_s->conf->autoscan = NULL;
  1820. autoscan_deinit(wpa_s);
  1821. } else
  1822. reply = dbus_message_new_error(message,
  1823. DBUS_ERROR_INVALID_ARGS,
  1824. NULL);
  1825. return reply;
  1826. }
  1827. #endif /* CONFIG_AUTOSCAN */
  1828. /*
  1829. * wpas_dbus_handler_eap_logoff - IEEE 802.1X EAPOL state machine logoff
  1830. * @message: Pointer to incoming dbus message
  1831. * @wpa_s: wpa_supplicant structure for a network interface
  1832. * Returns: NULL
  1833. *
  1834. * Handler function for "EAPLogoff" method call of network interface.
  1835. */
  1836. DBusMessage * wpas_dbus_handler_eap_logoff(DBusMessage *message,
  1837. struct wpa_supplicant *wpa_s)
  1838. {
  1839. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  1840. return NULL;
  1841. }
  1842. /*
  1843. * wpas_dbus_handler_eap_logon - IEEE 802.1X EAPOL state machine logon
  1844. * @message: Pointer to incoming dbus message
  1845. * @wpa_s: wpa_supplicant structure for a network interface
  1846. * Returns: NULL
  1847. *
  1848. * Handler function for "EAPLogin" method call of network interface.
  1849. */
  1850. DBusMessage * wpas_dbus_handler_eap_logon(DBusMessage *message,
  1851. struct wpa_supplicant *wpa_s)
  1852. {
  1853. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  1854. return NULL;
  1855. }
  1856. #ifdef CONFIG_TDLS
  1857. static int get_peer_hwaddr_helper(DBusMessage *message, const char *func_name,
  1858. u8 *peer_address, DBusMessage **error)
  1859. {
  1860. const char *peer_string;
  1861. *error = NULL;
  1862. if (!dbus_message_get_args(message, NULL,
  1863. DBUS_TYPE_STRING, &peer_string,
  1864. DBUS_TYPE_INVALID)) {
  1865. *error = wpas_dbus_error_invalid_args(message, NULL);
  1866. return -1;
  1867. }
  1868. if (hwaddr_aton(peer_string, peer_address)) {
  1869. wpa_printf(MSG_DEBUG, "%s: invalid address '%s'",
  1870. func_name, peer_string);
  1871. *error = wpas_dbus_error_invalid_args(
  1872. message, "Invalid hardware address format");
  1873. return -1;
  1874. }
  1875. return 0;
  1876. }
  1877. /*
  1878. * wpas_dbus_handler_tdls_discover - Discover TDLS peer
  1879. * @message: Pointer to incoming dbus message
  1880. * @wpa_s: wpa_supplicant structure for a network interface
  1881. * Returns: NULL indicating success or DBus error message on failure
  1882. *
  1883. * Handler function for "TDLSDiscover" method call of network interface.
  1884. */
  1885. DBusMessage * wpas_dbus_handler_tdls_discover(DBusMessage *message,
  1886. struct wpa_supplicant *wpa_s)
  1887. {
  1888. u8 peer[ETH_ALEN];
  1889. DBusMessage *error_reply;
  1890. int ret;
  1891. if (get_peer_hwaddr_helper(message, __func__, peer, &error_reply) < 0)
  1892. return error_reply;
  1893. wpa_printf(MSG_DEBUG, "DBUS TDLS_DISCOVER " MACSTR, MAC2STR(peer));
  1894. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  1895. ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
  1896. else
  1897. ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
  1898. if (ret) {
  1899. return wpas_dbus_error_unknown_error(
  1900. message, "error performing TDLS discovery");
  1901. }
  1902. return NULL;
  1903. }
  1904. /*
  1905. * wpas_dbus_handler_tdls_setup - Setup TDLS session
  1906. * @message: Pointer to incoming dbus message
  1907. * @wpa_s: wpa_supplicant structure for a network interface
  1908. * Returns: NULL indicating success or DBus error message on failure
  1909. *
  1910. * Handler function for "TDLSSetup" method call of network interface.
  1911. */
  1912. DBusMessage * wpas_dbus_handler_tdls_setup(DBusMessage *message,
  1913. struct wpa_supplicant *wpa_s)
  1914. {
  1915. u8 peer[ETH_ALEN];
  1916. DBusMessage *error_reply;
  1917. int ret;
  1918. if (get_peer_hwaddr_helper(message, __func__, peer, &error_reply) < 0)
  1919. return error_reply;
  1920. wpa_printf(MSG_DEBUG, "DBUS TDLS_SETUP " MACSTR, MAC2STR(peer));
  1921. wpa_tdls_remove(wpa_s->wpa, peer);
  1922. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  1923. ret = wpa_tdls_start(wpa_s->wpa, peer);
  1924. else
  1925. ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  1926. if (ret) {
  1927. return wpas_dbus_error_unknown_error(
  1928. message, "error performing TDLS setup");
  1929. }
  1930. return NULL;
  1931. }
  1932. /*
  1933. * wpas_dbus_handler_tdls_status - Return TDLS session status
  1934. * @message: Pointer to incoming dbus message
  1935. * @wpa_s: wpa_supplicant structure for a network interface
  1936. * Returns: A string representing the state of the link to this TDLS peer
  1937. *
  1938. * Handler function for "TDLSStatus" method call of network interface.
  1939. */
  1940. DBusMessage * wpas_dbus_handler_tdls_status(DBusMessage *message,
  1941. struct wpa_supplicant *wpa_s)
  1942. {
  1943. u8 peer[ETH_ALEN];
  1944. DBusMessage *reply;
  1945. const char *tdls_status;
  1946. if (get_peer_hwaddr_helper(message, __func__, peer, &reply) < 0)
  1947. return reply;
  1948. wpa_printf(MSG_DEBUG, "DBUS TDLS_STATUS " MACSTR, MAC2STR(peer));
  1949. tdls_status = wpa_tdls_get_link_status(wpa_s->wpa, peer);
  1950. reply = dbus_message_new_method_return(message);
  1951. dbus_message_append_args(reply, DBUS_TYPE_STRING,
  1952. &tdls_status, DBUS_TYPE_INVALID);
  1953. return reply;
  1954. }
  1955. /*
  1956. * wpas_dbus_handler_tdls_teardown - Teardown TDLS session
  1957. * @message: Pointer to incoming dbus message
  1958. * @wpa_s: wpa_supplicant structure for a network interface
  1959. * Returns: NULL indicating success or DBus error message on failure
  1960. *
  1961. * Handler function for "TDLSTeardown" method call of network interface.
  1962. */
  1963. DBusMessage * wpas_dbus_handler_tdls_teardown(DBusMessage *message,
  1964. struct wpa_supplicant *wpa_s)
  1965. {
  1966. u8 peer[ETH_ALEN];
  1967. DBusMessage *error_reply;
  1968. int ret;
  1969. if (get_peer_hwaddr_helper(message, __func__, peer, &error_reply) < 0)
  1970. return error_reply;
  1971. wpa_printf(MSG_DEBUG, "DBUS TDLS_TEARDOWN " MACSTR, MAC2STR(peer));
  1972. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  1973. ret = wpa_tdls_teardown_link(
  1974. wpa_s->wpa, peer,
  1975. WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
  1976. else
  1977. ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  1978. if (ret) {
  1979. return wpas_dbus_error_unknown_error(
  1980. message, "error performing TDLS teardown");
  1981. }
  1982. return NULL;
  1983. }
  1984. /*
  1985. * wpas_dbus_handler_tdls_channel_switch - Enable channel switching with TDLS peer
  1986. * @message: Pointer to incoming dbus message
  1987. * @wpa_s: wpa_supplicant structure for a network interface
  1988. * Returns: NULL indicating success or DBus error message on failure
  1989. *
  1990. * Handler function for "TDLSChannelSwitch" method call of network interface.
  1991. */
  1992. DBusMessage *
  1993. wpas_dbus_handler_tdls_channel_switch(DBusMessage *message,
  1994. struct wpa_supplicant *wpa_s)
  1995. {
  1996. DBusMessageIter iter, iter_dict;
  1997. struct wpa_dbus_dict_entry entry;
  1998. u8 peer[ETH_ALEN];
  1999. struct hostapd_freq_params freq_params;
  2000. u8 oper_class = 0;
  2001. int ret;
  2002. int is_peer_present = 0;
  2003. if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
  2004. wpa_printf(MSG_INFO,
  2005. "tdls_chanswitch: Only supported with external setup");
  2006. return wpas_dbus_error_unknown_error(message, "TDLS is not using external setup");
  2007. }
  2008. os_memset(&freq_params, 0, sizeof(freq_params));
  2009. dbus_message_iter_init(message, &iter);
  2010. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  2011. return wpas_dbus_error_invalid_args(message, NULL);
  2012. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  2013. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  2014. return wpas_dbus_error_invalid_args(message, NULL);
  2015. if (os_strcmp(entry.key, "PeerAddress") == 0 &&
  2016. entry.type == DBUS_TYPE_STRING) {
  2017. if (hwaddr_aton(entry.str_value, peer)) {
  2018. wpa_printf(MSG_DEBUG,
  2019. "tdls_chanswitch: Invalid address '%s'",
  2020. entry.str_value);
  2021. wpa_dbus_dict_entry_clear(&entry);
  2022. return wpas_dbus_error_invalid_args(message,
  2023. NULL);
  2024. }
  2025. is_peer_present = 1;
  2026. } else if (os_strcmp(entry.key, "OperClass") == 0 &&
  2027. entry.type == DBUS_TYPE_BYTE) {
  2028. oper_class = entry.byte_value;
  2029. } else if (os_strcmp(entry.key, "Frequency") == 0 &&
  2030. entry.type == DBUS_TYPE_UINT32) {
  2031. freq_params.freq = entry.uint32_value;
  2032. } else if (os_strcmp(entry.key, "SecChannelOffset") == 0 &&
  2033. entry.type == DBUS_TYPE_UINT32) {
  2034. freq_params.sec_channel_offset = entry.uint32_value;
  2035. } else if (os_strcmp(entry.key, "CenterFrequency1") == 0 &&
  2036. entry.type == DBUS_TYPE_UINT32) {
  2037. freq_params.center_freq1 = entry.uint32_value;
  2038. } else if (os_strcmp(entry.key, "CenterFrequency2") == 0 &&
  2039. entry.type == DBUS_TYPE_UINT32) {
  2040. freq_params.center_freq2 = entry.uint32_value;
  2041. } else if (os_strcmp(entry.key, "Bandwidth") == 0 &&
  2042. entry.type == DBUS_TYPE_UINT32) {
  2043. freq_params.bandwidth = entry.uint32_value;
  2044. } else if (os_strcmp(entry.key, "HT") == 0 &&
  2045. entry.type == DBUS_TYPE_BOOLEAN) {
  2046. freq_params.ht_enabled = entry.bool_value;
  2047. } else if (os_strcmp(entry.key, "VHT") == 0 &&
  2048. entry.type == DBUS_TYPE_BOOLEAN) {
  2049. freq_params.vht_enabled = entry.bool_value;
  2050. } else {
  2051. wpa_dbus_dict_entry_clear(&entry);
  2052. return wpas_dbus_error_invalid_args(message, NULL);
  2053. }
  2054. wpa_dbus_dict_entry_clear(&entry);
  2055. }
  2056. if (oper_class == 0) {
  2057. wpa_printf(MSG_INFO,
  2058. "tdls_chanswitch: Invalid op class provided");
  2059. return wpas_dbus_error_invalid_args(
  2060. message, "Invalid op class provided");
  2061. }
  2062. if (freq_params.freq == 0) {
  2063. wpa_printf(MSG_INFO,
  2064. "tdls_chanswitch: Invalid freq provided");
  2065. return wpas_dbus_error_invalid_args(message,
  2066. "Invalid freq provided");
  2067. }
  2068. if (is_peer_present == 0) {
  2069. wpa_printf(MSG_DEBUG,
  2070. "tdls_chanswitch: peer address not provided");
  2071. return wpas_dbus_error_invalid_args(
  2072. message, "peer address not provided");
  2073. }
  2074. wpa_printf(MSG_DEBUG, "dbus: TDLS_CHAN_SWITCH " MACSTR
  2075. " OP CLASS %d FREQ %d CENTER1 %d CENTER2 %d BW %d SEC_OFFSET %d%s%s",
  2076. MAC2STR(peer), oper_class, freq_params.freq,
  2077. freq_params.center_freq1, freq_params.center_freq2,
  2078. freq_params.bandwidth, freq_params.sec_channel_offset,
  2079. freq_params.ht_enabled ? " HT" : "",
  2080. freq_params.vht_enabled ? " VHT" : "");
  2081. ret = wpa_tdls_enable_chan_switch(wpa_s->wpa, peer, oper_class,
  2082. &freq_params);
  2083. if (ret)
  2084. return wpas_dbus_error_unknown_error(
  2085. message, "error processing TDLS channel switch");
  2086. return NULL;
  2087. }
  2088. /*
  2089. * wpas_dbus_handler_tdls_cancel_channel_switch - Disable channel switching with TDLS peer
  2090. * @message: Pointer to incoming dbus message
  2091. * @wpa_s: wpa_supplicant structure for a network interface
  2092. * Returns: NULL indicating success or DBus error message on failure
  2093. *
  2094. * Handler function for "TDLSCancelChannelSwitch" method call of network
  2095. * interface.
  2096. */
  2097. DBusMessage *
  2098. wpas_dbus_handler_tdls_cancel_channel_switch(DBusMessage *message,
  2099. struct wpa_supplicant *wpa_s)
  2100. {
  2101. u8 peer[ETH_ALEN];
  2102. DBusMessage *error_reply;
  2103. int ret;
  2104. if (get_peer_hwaddr_helper(message, __func__, peer, &error_reply) < 0)
  2105. return error_reply;
  2106. wpa_printf(MSG_DEBUG, "dbus: TDLS_CANCEL_CHAN_SWITCH " MACSTR,
  2107. MAC2STR(peer));
  2108. ret = wpa_tdls_disable_chan_switch(wpa_s->wpa, peer);
  2109. if (ret)
  2110. return wpas_dbus_error_unknown_error(
  2111. message, "error canceling TDLS channel switch");
  2112. return NULL;
  2113. }
  2114. #endif /* CONFIG_TDLS */
  2115. #ifndef CONFIG_NO_CONFIG_WRITE
  2116. /**
  2117. * wpas_dbus_handler_save_config - Save configuration to configuration file
  2118. * @message: Pointer to incoming dbus message
  2119. * @wpa_s: wpa_supplicant structure for a network interface
  2120. * Returns: NULL on Success, Otherwise errror message
  2121. *
  2122. * Handler function for "SaveConfig" method call of network interface.
  2123. */
  2124. DBusMessage * wpas_dbus_handler_save_config(DBusMessage *message,
  2125. struct wpa_supplicant *wpa_s)
  2126. {
  2127. int ret;
  2128. if (!wpa_s->conf->update_config) {
  2129. return wpas_dbus_error_unknown_error(
  2130. message,
  2131. "Not allowed to update configuration (update_config=0)");
  2132. }
  2133. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  2134. if (ret)
  2135. return wpas_dbus_error_unknown_error(
  2136. message, "Failed to update configuration");
  2137. return NULL;
  2138. }
  2139. #endif /* CONFIG_NO_CONFIG_WRITE */
  2140. /**
  2141. * wpas_dbus_handler_set_pkcs11_engine_and_module_path - Set PKCS #11 engine and module path
  2142. * @message: Pointer to incoming dbus message
  2143. * @wpa_s: %wpa_supplicant data structure
  2144. * Returns: A dbus message containing an error on failure or NULL on success
  2145. *
  2146. * Sets the PKCS #11 engine and module path.
  2147. */
  2148. DBusMessage * wpas_dbus_handler_set_pkcs11_engine_and_module_path(
  2149. DBusMessage *message, struct wpa_supplicant *wpa_s)
  2150. {
  2151. DBusMessageIter iter;
  2152. char *value = NULL;
  2153. char *pkcs11_engine_path = NULL;
  2154. char *pkcs11_module_path = NULL;
  2155. dbus_message_iter_init(message, &iter);
  2156. dbus_message_iter_get_basic(&iter, &value);
  2157. if (value == NULL) {
  2158. return dbus_message_new_error(
  2159. message, DBUS_ERROR_INVALID_ARGS,
  2160. "Invalid pkcs11_engine_path argument");
  2161. }
  2162. /* Empty path defaults to NULL */
  2163. if (os_strlen(value))
  2164. pkcs11_engine_path = value;
  2165. dbus_message_iter_next(&iter);
  2166. dbus_message_iter_get_basic(&iter, &value);
  2167. if (value == NULL) {
  2168. os_free(pkcs11_engine_path);
  2169. return dbus_message_new_error(
  2170. message, DBUS_ERROR_INVALID_ARGS,
  2171. "Invalid pkcs11_module_path argument");
  2172. }
  2173. /* Empty path defaults to NULL */
  2174. if (os_strlen(value))
  2175. pkcs11_module_path = value;
  2176. if (wpas_set_pkcs11_engine_and_module_path(wpa_s, pkcs11_engine_path,
  2177. pkcs11_module_path))
  2178. return dbus_message_new_error(
  2179. message, DBUS_ERROR_FAILED,
  2180. "Reinit of the EAPOL state machine with the new PKCS #11 engine and module path failed.");
  2181. if (wpa_s->dbus_new_path) {
  2182. wpa_dbus_mark_property_changed(
  2183. wpa_s->global->dbus, wpa_s->dbus_new_path,
  2184. WPAS_DBUS_NEW_IFACE_INTERFACE, "PKCS11EnginePath");
  2185. wpa_dbus_mark_property_changed(
  2186. wpa_s->global->dbus, wpa_s->dbus_new_path,
  2187. WPAS_DBUS_NEW_IFACE_INTERFACE, "PKCS11ModulePath");
  2188. }
  2189. return NULL;
  2190. }
  2191. /**
  2192. * wpas_dbus_getter_capabilities - Return interface capabilities
  2193. * @iter: Pointer to incoming dbus message iter
  2194. * @error: Location to store error on failure
  2195. * @user_data: Function specific data
  2196. * Returns: TRUE on success, FALSE on failure
  2197. *
  2198. * Getter for "Capabilities" property of an interface.
  2199. */
  2200. dbus_bool_t wpas_dbus_getter_capabilities(
  2201. const struct wpa_dbus_property_desc *property_desc,
  2202. DBusMessageIter *iter, DBusError *error, void *user_data)
  2203. {
  2204. struct wpa_supplicant *wpa_s = user_data;
  2205. struct wpa_driver_capa capa;
  2206. int res;
  2207. DBusMessageIter iter_dict, iter_dict_entry, iter_dict_val, iter_array,
  2208. variant_iter;
  2209. const char *scans[] = { "active", "passive", "ssid" };
  2210. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  2211. "a{sv}", &variant_iter) ||
  2212. !wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
  2213. goto nomem;
  2214. res = wpa_drv_get_capa(wpa_s, &capa);
  2215. /***** pairwise cipher */
  2216. if (res < 0) {
  2217. const char *args[] = {"ccmp", "tkip", "none"};
  2218. if (!wpa_dbus_dict_append_string_array(
  2219. &iter_dict, "Pairwise", args,
  2220. ARRAY_SIZE(args)))
  2221. goto nomem;
  2222. } else {
  2223. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Pairwise",
  2224. &iter_dict_entry,
  2225. &iter_dict_val,
  2226. &iter_array) ||
  2227. ((capa.enc & WPA_DRIVER_CAPA_ENC_CCMP_256) &&
  2228. !wpa_dbus_dict_string_array_add_element(
  2229. &iter_array, "ccmp-256")) ||
  2230. ((capa.enc & WPA_DRIVER_CAPA_ENC_GCMP_256) &&
  2231. !wpa_dbus_dict_string_array_add_element(
  2232. &iter_array, "gcmp-256")) ||
  2233. ((capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) &&
  2234. !wpa_dbus_dict_string_array_add_element(
  2235. &iter_array, "ccmp")) ||
  2236. ((capa.enc & WPA_DRIVER_CAPA_ENC_GCMP) &&
  2237. !wpa_dbus_dict_string_array_add_element(
  2238. &iter_array, "gcmp")) ||
  2239. ((capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) &&
  2240. !wpa_dbus_dict_string_array_add_element(
  2241. &iter_array, "tkip")) ||
  2242. ((capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) &&
  2243. !wpa_dbus_dict_string_array_add_element(
  2244. &iter_array, "none")) ||
  2245. !wpa_dbus_dict_end_string_array(&iter_dict,
  2246. &iter_dict_entry,
  2247. &iter_dict_val,
  2248. &iter_array))
  2249. goto nomem;
  2250. }
  2251. /***** group cipher */
  2252. if (res < 0) {
  2253. const char *args[] = {
  2254. "ccmp", "tkip", "wep104", "wep40"
  2255. };
  2256. if (!wpa_dbus_dict_append_string_array(
  2257. &iter_dict, "Group", args,
  2258. ARRAY_SIZE(args)))
  2259. goto nomem;
  2260. } else {
  2261. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Group",
  2262. &iter_dict_entry,
  2263. &iter_dict_val,
  2264. &iter_array) ||
  2265. ((capa.enc & WPA_DRIVER_CAPA_ENC_CCMP_256) &&
  2266. !wpa_dbus_dict_string_array_add_element(
  2267. &iter_array, "ccmp-256")) ||
  2268. ((capa.enc & WPA_DRIVER_CAPA_ENC_GCMP_256) &&
  2269. !wpa_dbus_dict_string_array_add_element(
  2270. &iter_array, "gcmp-256")) ||
  2271. ((capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) &&
  2272. !wpa_dbus_dict_string_array_add_element(
  2273. &iter_array, "ccmp")) ||
  2274. ((capa.enc & WPA_DRIVER_CAPA_ENC_GCMP) &&
  2275. !wpa_dbus_dict_string_array_add_element(
  2276. &iter_array, "gcmp")) ||
  2277. ((capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) &&
  2278. !wpa_dbus_dict_string_array_add_element(
  2279. &iter_array, "tkip")) ||
  2280. ((capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) &&
  2281. !wpa_dbus_dict_string_array_add_element(
  2282. &iter_array, "wep104")) ||
  2283. ((capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) &&
  2284. !wpa_dbus_dict_string_array_add_element(
  2285. &iter_array, "wep40")) ||
  2286. !wpa_dbus_dict_end_string_array(&iter_dict,
  2287. &iter_dict_entry,
  2288. &iter_dict_val,
  2289. &iter_array))
  2290. goto nomem;
  2291. }
  2292. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "GroupMgmt",
  2293. &iter_dict_entry,
  2294. &iter_dict_val,
  2295. &iter_array) ||
  2296. (res == 0 && (capa.enc & WPA_DRIVER_CAPA_ENC_BIP) &&
  2297. !wpa_dbus_dict_string_array_add_element(
  2298. &iter_array, "aes-128-cmac")) ||
  2299. (res == 0 && (capa.enc & WPA_DRIVER_CAPA_ENC_BIP_GMAC_128) &&
  2300. !wpa_dbus_dict_string_array_add_element(
  2301. &iter_array, "bip-gmac-128")) ||
  2302. (res == 0 && (capa.enc & WPA_DRIVER_CAPA_ENC_BIP_GMAC_256) &&
  2303. !wpa_dbus_dict_string_array_add_element(
  2304. &iter_array, "bip-gmac-256")) ||
  2305. (res == 0 && (capa.enc & WPA_DRIVER_CAPA_ENC_BIP_CMAC_256) &&
  2306. !wpa_dbus_dict_string_array_add_element(
  2307. &iter_array, "bip-cmac-256")) ||
  2308. !wpa_dbus_dict_end_string_array(&iter_dict,
  2309. &iter_dict_entry,
  2310. &iter_dict_val,
  2311. &iter_array))
  2312. goto nomem;
  2313. /***** key management */
  2314. if (res < 0) {
  2315. const char *args[] = {
  2316. "wpa-psk", "wpa-eap", "ieee8021x", "wpa-none",
  2317. #ifdef CONFIG_WPS
  2318. "wps",
  2319. #endif /* CONFIG_WPS */
  2320. "none"
  2321. };
  2322. if (!wpa_dbus_dict_append_string_array(
  2323. &iter_dict, "KeyMgmt", args,
  2324. ARRAY_SIZE(args)))
  2325. goto nomem;
  2326. } else {
  2327. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "KeyMgmt",
  2328. &iter_dict_entry,
  2329. &iter_dict_val,
  2330. &iter_array) ||
  2331. !wpa_dbus_dict_string_array_add_element(&iter_array,
  2332. "none") ||
  2333. !wpa_dbus_dict_string_array_add_element(&iter_array,
  2334. "ieee8021x"))
  2335. goto nomem;
  2336. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2337. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  2338. if (!wpa_dbus_dict_string_array_add_element(
  2339. &iter_array, "wpa-eap") ||
  2340. ((capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT) &&
  2341. !wpa_dbus_dict_string_array_add_element(
  2342. &iter_array, "wpa-ft-eap")))
  2343. goto nomem;
  2344. /* TODO: Ensure that driver actually supports sha256 encryption. */
  2345. #ifdef CONFIG_IEEE80211W
  2346. if (!wpa_dbus_dict_string_array_add_element(
  2347. &iter_array, "wpa-eap-sha256"))
  2348. goto nomem;
  2349. #endif /* CONFIG_IEEE80211W */
  2350. }
  2351. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  2352. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2353. if (!wpa_dbus_dict_string_array_add_element(
  2354. &iter_array, "wpa-psk") ||
  2355. ((capa.key_mgmt &
  2356. WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK) &&
  2357. !wpa_dbus_dict_string_array_add_element(
  2358. &iter_array, "wpa-ft-psk")))
  2359. goto nomem;
  2360. /* TODO: Ensure that driver actually supports sha256 encryption. */
  2361. #ifdef CONFIG_IEEE80211W
  2362. if (!wpa_dbus_dict_string_array_add_element(
  2363. &iter_array, "wpa-psk-sha256"))
  2364. goto nomem;
  2365. #endif /* CONFIG_IEEE80211W */
  2366. }
  2367. if ((capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) &&
  2368. !wpa_dbus_dict_string_array_add_element(&iter_array,
  2369. "wpa-none"))
  2370. goto nomem;
  2371. #ifdef CONFIG_WPS
  2372. if (!wpa_dbus_dict_string_array_add_element(&iter_array,
  2373. "wps"))
  2374. goto nomem;
  2375. #endif /* CONFIG_WPS */
  2376. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  2377. &iter_dict_entry,
  2378. &iter_dict_val,
  2379. &iter_array))
  2380. goto nomem;
  2381. }
  2382. /***** WPA protocol */
  2383. if (res < 0) {
  2384. const char *args[] = { "rsn", "wpa" };
  2385. if (!wpa_dbus_dict_append_string_array(
  2386. &iter_dict, "Protocol", args,
  2387. ARRAY_SIZE(args)))
  2388. goto nomem;
  2389. } else {
  2390. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Protocol",
  2391. &iter_dict_entry,
  2392. &iter_dict_val,
  2393. &iter_array) ||
  2394. ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  2395. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) &&
  2396. !wpa_dbus_dict_string_array_add_element(
  2397. &iter_array, "rsn")) ||
  2398. ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2399. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) &&
  2400. !wpa_dbus_dict_string_array_add_element(
  2401. &iter_array, "wpa")) ||
  2402. !wpa_dbus_dict_end_string_array(&iter_dict,
  2403. &iter_dict_entry,
  2404. &iter_dict_val,
  2405. &iter_array))
  2406. goto nomem;
  2407. }
  2408. /***** auth alg */
  2409. if (res < 0) {
  2410. const char *args[] = { "open", "shared", "leap" };
  2411. if (!wpa_dbus_dict_append_string_array(
  2412. &iter_dict, "AuthAlg", args,
  2413. ARRAY_SIZE(args)))
  2414. goto nomem;
  2415. } else {
  2416. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "AuthAlg",
  2417. &iter_dict_entry,
  2418. &iter_dict_val,
  2419. &iter_array))
  2420. goto nomem;
  2421. if (((capa.auth & WPA_DRIVER_AUTH_OPEN) &&
  2422. !wpa_dbus_dict_string_array_add_element(
  2423. &iter_array, "open")) ||
  2424. ((capa.auth & WPA_DRIVER_AUTH_SHARED) &&
  2425. !wpa_dbus_dict_string_array_add_element(
  2426. &iter_array, "shared")) ||
  2427. ((capa.auth & WPA_DRIVER_AUTH_LEAP) &&
  2428. !wpa_dbus_dict_string_array_add_element(
  2429. &iter_array, "leap")) ||
  2430. !wpa_dbus_dict_end_string_array(&iter_dict,
  2431. &iter_dict_entry,
  2432. &iter_dict_val,
  2433. &iter_array))
  2434. goto nomem;
  2435. }
  2436. /***** Scan */
  2437. if (!wpa_dbus_dict_append_string_array(&iter_dict, "Scan", scans,
  2438. ARRAY_SIZE(scans)))
  2439. goto nomem;
  2440. /***** Modes */
  2441. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Modes",
  2442. &iter_dict_entry,
  2443. &iter_dict_val,
  2444. &iter_array) ||
  2445. !wpa_dbus_dict_string_array_add_element(
  2446. &iter_array, "infrastructure") ||
  2447. (res >= 0 && (capa.flags & WPA_DRIVER_FLAGS_IBSS) &&
  2448. !wpa_dbus_dict_string_array_add_element(
  2449. &iter_array, "ad-hoc")) ||
  2450. (res >= 0 && (capa.flags & WPA_DRIVER_FLAGS_AP) &&
  2451. !wpa_dbus_dict_string_array_add_element(
  2452. &iter_array, "ap")) ||
  2453. (res >= 0 && (capa.flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
  2454. !wpa_s->conf->p2p_disabled &&
  2455. !wpa_dbus_dict_string_array_add_element(
  2456. &iter_array, "p2p")) ||
  2457. #ifdef CONFIG_MESH
  2458. (res >= 0 && (capa.flags & WPA_DRIVER_FLAGS_MESH) &&
  2459. !wpa_dbus_dict_string_array_add_element(
  2460. &iter_array, "mesh")) ||
  2461. #endif /* CONFIG_MESH */
  2462. !wpa_dbus_dict_end_string_array(&iter_dict,
  2463. &iter_dict_entry,
  2464. &iter_dict_val,
  2465. &iter_array))
  2466. goto nomem;
  2467. /***** Modes end */
  2468. if (res >= 0) {
  2469. dbus_int32_t max_scan_ssid = capa.max_scan_ssids;
  2470. if (!wpa_dbus_dict_append_int32(&iter_dict, "MaxScanSSID",
  2471. max_scan_ssid))
  2472. goto nomem;
  2473. }
  2474. if (!wpa_dbus_dict_close_write(&variant_iter, &iter_dict) ||
  2475. !dbus_message_iter_close_container(iter, &variant_iter))
  2476. goto nomem;
  2477. return TRUE;
  2478. nomem:
  2479. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2480. return FALSE;
  2481. }
  2482. /**
  2483. * wpas_dbus_getter_state - Get interface state
  2484. * @iter: Pointer to incoming dbus message iter
  2485. * @error: Location to store error on failure
  2486. * @user_data: Function specific data
  2487. * Returns: TRUE on success, FALSE on failure
  2488. *
  2489. * Getter for "State" property.
  2490. */
  2491. dbus_bool_t wpas_dbus_getter_state(
  2492. const struct wpa_dbus_property_desc *property_desc,
  2493. DBusMessageIter *iter, DBusError *error, void *user_data)
  2494. {
  2495. struct wpa_supplicant *wpa_s = user_data;
  2496. const char *str_state;
  2497. char *state_ls, *tmp;
  2498. dbus_bool_t success = FALSE;
  2499. str_state = wpa_supplicant_state_txt(wpa_s->wpa_state);
  2500. /* make state string lowercase to fit new DBus API convention
  2501. */
  2502. state_ls = tmp = os_strdup(str_state);
  2503. if (!tmp) {
  2504. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2505. return FALSE;
  2506. }
  2507. while (*tmp) {
  2508. *tmp = tolower(*tmp);
  2509. tmp++;
  2510. }
  2511. success = wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2512. &state_ls, error);
  2513. os_free(state_ls);
  2514. return success;
  2515. }
  2516. /**
  2517. * wpas_dbus_new_iface_get_scanning - Get interface scanning state
  2518. * @iter: Pointer to incoming dbus message iter
  2519. * @error: Location to store error on failure
  2520. * @user_data: Function specific data
  2521. * Returns: TRUE on success, FALSE on failure
  2522. *
  2523. * Getter for "scanning" property.
  2524. */
  2525. dbus_bool_t wpas_dbus_getter_scanning(
  2526. const struct wpa_dbus_property_desc *property_desc,
  2527. DBusMessageIter *iter, DBusError *error, void *user_data)
  2528. {
  2529. struct wpa_supplicant *wpa_s = user_data;
  2530. dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
  2531. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  2532. &scanning, error);
  2533. }
  2534. /**
  2535. * wpas_dbus_getter_ap_scan - Control roaming mode
  2536. * @iter: Pointer to incoming dbus message iter
  2537. * @error: Location to store error on failure
  2538. * @user_data: Function specific data
  2539. * Returns: TRUE on success, FALSE on failure
  2540. *
  2541. * Getter function for "ApScan" property.
  2542. */
  2543. dbus_bool_t wpas_dbus_getter_ap_scan(
  2544. const struct wpa_dbus_property_desc *property_desc,
  2545. DBusMessageIter *iter, DBusError *error, void *user_data)
  2546. {
  2547. struct wpa_supplicant *wpa_s = user_data;
  2548. dbus_uint32_t ap_scan = wpa_s->conf->ap_scan;
  2549. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT32,
  2550. &ap_scan, error);
  2551. }
  2552. /**
  2553. * wpas_dbus_setter_ap_scan - Control roaming mode
  2554. * @iter: Pointer to incoming dbus message iter
  2555. * @error: Location to store error on failure
  2556. * @user_data: Function specific data
  2557. * Returns: TRUE on success, FALSE on failure
  2558. *
  2559. * Setter function for "ApScan" property.
  2560. */
  2561. dbus_bool_t wpas_dbus_setter_ap_scan(
  2562. const struct wpa_dbus_property_desc *property_desc,
  2563. DBusMessageIter *iter, DBusError *error, void *user_data)
  2564. {
  2565. struct wpa_supplicant *wpa_s = user_data;
  2566. dbus_uint32_t ap_scan;
  2567. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_UINT32,
  2568. &ap_scan))
  2569. return FALSE;
  2570. if (wpa_supplicant_set_ap_scan(wpa_s, ap_scan)) {
  2571. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2572. "ap_scan must be 0, 1, or 2");
  2573. return FALSE;
  2574. }
  2575. return TRUE;
  2576. }
  2577. #ifdef CONFIG_IEEE80211W
  2578. /**
  2579. * wpas_dbus_getter_pmf - Control PMF default
  2580. * @iter: Pointer to incoming dbus message iter
  2581. * @error: Location to store error on failure
  2582. * @user_data: Function specific data
  2583. * Returns: TRUE on success, FALSE on failure
  2584. *
  2585. * Getter function for "Pmf" property.
  2586. */
  2587. dbus_bool_t wpas_dbus_getter_pmf(
  2588. const struct wpa_dbus_property_desc *property_desc,
  2589. DBusMessageIter *iter, DBusError *error, void *user_data)
  2590. {
  2591. struct wpa_supplicant *wpa_s = user_data;
  2592. dbus_uint32_t pmf = wpa_s->conf->pmf;
  2593. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT32,
  2594. &pmf, error);
  2595. }
  2596. /**
  2597. * wpas_dbus_setter_pmf - Control PMF default
  2598. * @iter: Pointer to incoming dbus message iter
  2599. * @error: Location to store error on failure
  2600. * @user_data: Function specific data
  2601. * Returns: TRUE on success, FALSE on failure
  2602. *
  2603. * Setter function for "Pmf" property.
  2604. */
  2605. dbus_bool_t wpas_dbus_setter_pmf(
  2606. const struct wpa_dbus_property_desc *property_desc,
  2607. DBusMessageIter *iter, DBusError *error, void *user_data)
  2608. {
  2609. struct wpa_supplicant *wpa_s = user_data;
  2610. dbus_uint32_t pmf;
  2611. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_UINT32,
  2612. &pmf))
  2613. return FALSE;
  2614. if (pmf > 2) {
  2615. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2616. "Pmf must be 0, 1, or 2");
  2617. return FALSE;
  2618. }
  2619. wpa_s->conf->pmf = pmf;
  2620. return TRUE;
  2621. }
  2622. #endif /* CONFIG_IEEE80211W */
  2623. /**
  2624. * wpas_dbus_getter_fast_reauth - Control fast
  2625. * reauthentication (TLS session resumption)
  2626. * @iter: Pointer to incoming dbus message iter
  2627. * @error: Location to store error on failure
  2628. * @user_data: Function specific data
  2629. * Returns: TRUE on success, FALSE on failure
  2630. *
  2631. * Getter function for "FastReauth" property.
  2632. */
  2633. dbus_bool_t wpas_dbus_getter_fast_reauth(
  2634. const struct wpa_dbus_property_desc *property_desc,
  2635. DBusMessageIter *iter, DBusError *error, void *user_data)
  2636. {
  2637. struct wpa_supplicant *wpa_s = user_data;
  2638. dbus_bool_t fast_reauth = wpa_s->conf->fast_reauth ? TRUE : FALSE;
  2639. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  2640. &fast_reauth, error);
  2641. }
  2642. /**
  2643. * wpas_dbus_setter_fast_reauth - Control fast
  2644. * reauthentication (TLS session resumption)
  2645. * @iter: Pointer to incoming dbus message iter
  2646. * @error: Location to store error on failure
  2647. * @user_data: Function specific data
  2648. * Returns: TRUE on success, FALSE on failure
  2649. *
  2650. * Setter function for "FastReauth" property.
  2651. */
  2652. dbus_bool_t wpas_dbus_setter_fast_reauth(
  2653. const struct wpa_dbus_property_desc *property_desc,
  2654. DBusMessageIter *iter, DBusError *error, void *user_data)
  2655. {
  2656. struct wpa_supplicant *wpa_s = user_data;
  2657. dbus_bool_t fast_reauth;
  2658. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_BOOLEAN,
  2659. &fast_reauth))
  2660. return FALSE;
  2661. wpa_s->conf->fast_reauth = fast_reauth;
  2662. return TRUE;
  2663. }
  2664. /**
  2665. * wpas_dbus_getter_disconnect_reason - Get most recent reason for disconnect
  2666. * @iter: Pointer to incoming dbus message iter
  2667. * @error: Location to store error on failure
  2668. * @user_data: Function specific data
  2669. * Returns: TRUE on success, FALSE on failure
  2670. *
  2671. * Getter for "DisconnectReason" property. The reason is negative if it is
  2672. * locally generated.
  2673. */
  2674. dbus_bool_t wpas_dbus_getter_disconnect_reason(
  2675. const struct wpa_dbus_property_desc *property_desc,
  2676. DBusMessageIter *iter, DBusError *error, void *user_data)
  2677. {
  2678. struct wpa_supplicant *wpa_s = user_data;
  2679. dbus_int32_t reason = wpa_s->disconnect_reason;
  2680. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_INT32,
  2681. &reason, error);
  2682. }
  2683. /**
  2684. * wpas_dbus_getter_assoc_status_code - Get most recent failed assoc status code
  2685. * @iter: Pointer to incoming dbus message iter
  2686. * @error: Location to store error on failure
  2687. * @user_data: Function specific data
  2688. * Returns: TRUE on success, FALSE on failure
  2689. *
  2690. * Getter for "AssocStatusCode" property.
  2691. */
  2692. dbus_bool_t wpas_dbus_getter_assoc_status_code(
  2693. const struct wpa_dbus_property_desc *property_desc,
  2694. DBusMessageIter *iter, DBusError *error, void *user_data)
  2695. {
  2696. struct wpa_supplicant *wpa_s = user_data;
  2697. dbus_int32_t status_code = wpa_s->assoc_status_code;
  2698. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_INT32,
  2699. &status_code, error);
  2700. }
  2701. /**
  2702. * wpas_dbus_getter_bss_expire_age - Get BSS entry expiration age
  2703. * @iter: Pointer to incoming dbus message iter
  2704. * @error: Location to store error on failure
  2705. * @user_data: Function specific data
  2706. * Returns: TRUE on success, FALSE on failure
  2707. *
  2708. * Getter function for "BSSExpireAge" property.
  2709. */
  2710. dbus_bool_t wpas_dbus_getter_bss_expire_age(
  2711. const struct wpa_dbus_property_desc *property_desc,
  2712. DBusMessageIter *iter, DBusError *error, void *user_data)
  2713. {
  2714. struct wpa_supplicant *wpa_s = user_data;
  2715. dbus_uint32_t expire_age = wpa_s->conf->bss_expiration_age;
  2716. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT32,
  2717. &expire_age, error);
  2718. }
  2719. /**
  2720. * wpas_dbus_setter_bss_expire_age - Control BSS entry expiration age
  2721. * @iter: Pointer to incoming dbus message iter
  2722. * @error: Location to store error on failure
  2723. * @user_data: Function specific data
  2724. * Returns: TRUE on success, FALSE on failure
  2725. *
  2726. * Setter function for "BSSExpireAge" property.
  2727. */
  2728. dbus_bool_t wpas_dbus_setter_bss_expire_age(
  2729. const struct wpa_dbus_property_desc *property_desc,
  2730. DBusMessageIter *iter, DBusError *error, void *user_data)
  2731. {
  2732. struct wpa_supplicant *wpa_s = user_data;
  2733. dbus_uint32_t expire_age;
  2734. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_UINT32,
  2735. &expire_age))
  2736. return FALSE;
  2737. if (wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age)) {
  2738. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2739. "BSSExpireAge must be >= 10");
  2740. return FALSE;
  2741. }
  2742. return TRUE;
  2743. }
  2744. /**
  2745. * wpas_dbus_getter_bss_expire_count - Get BSS entry expiration scan count
  2746. * @iter: Pointer to incoming dbus message iter
  2747. * @error: Location to store error on failure
  2748. * @user_data: Function specific data
  2749. * Returns: TRUE on success, FALSE on failure
  2750. *
  2751. * Getter function for "BSSExpireCount" property.
  2752. */
  2753. dbus_bool_t wpas_dbus_getter_bss_expire_count(
  2754. const struct wpa_dbus_property_desc *property_desc,
  2755. DBusMessageIter *iter, DBusError *error, void *user_data)
  2756. {
  2757. struct wpa_supplicant *wpa_s = user_data;
  2758. dbus_uint32_t expire_count = wpa_s->conf->bss_expiration_scan_count;
  2759. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT32,
  2760. &expire_count, error);
  2761. }
  2762. /**
  2763. * wpas_dbus_setter_bss_expire_count - Control BSS entry expiration scan count
  2764. * @iter: Pointer to incoming dbus message iter
  2765. * @error: Location to store error on failure
  2766. * @user_data: Function specific data
  2767. * Returns: TRUE on success, FALSE on failure
  2768. *
  2769. * Setter function for "BSSExpireCount" property.
  2770. */
  2771. dbus_bool_t wpas_dbus_setter_bss_expire_count(
  2772. const struct wpa_dbus_property_desc *property_desc,
  2773. DBusMessageIter *iter, DBusError *error, void *user_data)
  2774. {
  2775. struct wpa_supplicant *wpa_s = user_data;
  2776. dbus_uint32_t expire_count;
  2777. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_UINT32,
  2778. &expire_count))
  2779. return FALSE;
  2780. if (wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count)) {
  2781. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2782. "BSSExpireCount must be > 0");
  2783. return FALSE;
  2784. }
  2785. return TRUE;
  2786. }
  2787. /**
  2788. * wpas_dbus_getter_country - Control country code
  2789. * @iter: Pointer to incoming dbus message iter
  2790. * @error: Location to store error on failure
  2791. * @user_data: Function specific data
  2792. * Returns: TRUE on success, FALSE on failure
  2793. *
  2794. * Getter function for "Country" property.
  2795. */
  2796. dbus_bool_t wpas_dbus_getter_country(
  2797. const struct wpa_dbus_property_desc *property_desc,
  2798. DBusMessageIter *iter, DBusError *error, void *user_data)
  2799. {
  2800. struct wpa_supplicant *wpa_s = user_data;
  2801. char country[3];
  2802. char *str = country;
  2803. country[0] = wpa_s->conf->country[0];
  2804. country[1] = wpa_s->conf->country[1];
  2805. country[2] = '\0';
  2806. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2807. &str, error);
  2808. }
  2809. /**
  2810. * wpas_dbus_setter_country - Control country code
  2811. * @iter: Pointer to incoming dbus message iter
  2812. * @error: Location to store error on failure
  2813. * @user_data: Function specific data
  2814. * Returns: TRUE on success, FALSE on failure
  2815. *
  2816. * Setter function for "Country" property.
  2817. */
  2818. dbus_bool_t wpas_dbus_setter_country(
  2819. const struct wpa_dbus_property_desc *property_desc,
  2820. DBusMessageIter *iter, DBusError *error, void *user_data)
  2821. {
  2822. struct wpa_supplicant *wpa_s = user_data;
  2823. const char *country;
  2824. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_STRING,
  2825. &country))
  2826. return FALSE;
  2827. if (!country[0] || !country[1]) {
  2828. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2829. "invalid country code");
  2830. return FALSE;
  2831. }
  2832. if (wpa_s->drv_priv != NULL && wpa_drv_set_country(wpa_s, country)) {
  2833. wpa_printf(MSG_DEBUG, "Failed to set country");
  2834. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2835. "failed to set country code");
  2836. return FALSE;
  2837. }
  2838. wpa_s->conf->country[0] = country[0];
  2839. wpa_s->conf->country[1] = country[1];
  2840. return TRUE;
  2841. }
  2842. /**
  2843. * wpas_dbus_getter_scan_interval - Get scan interval
  2844. * @iter: Pointer to incoming dbus message iter
  2845. * @error: Location to store error on failure
  2846. * @user_data: Function specific data
  2847. * Returns: TRUE on success, FALSE on failure
  2848. *
  2849. * Getter function for "ScanInterval" property.
  2850. */
  2851. dbus_bool_t wpas_dbus_getter_scan_interval(
  2852. const struct wpa_dbus_property_desc *property_desc,
  2853. DBusMessageIter *iter, DBusError *error, void *user_data)
  2854. {
  2855. struct wpa_supplicant *wpa_s = user_data;
  2856. dbus_int32_t scan_interval = wpa_s->scan_interval;
  2857. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_INT32,
  2858. &scan_interval, error);
  2859. }
  2860. /**
  2861. * wpas_dbus_setter_scan_interval - Control scan interval
  2862. * @iter: Pointer to incoming dbus message iter
  2863. * @error: Location to store error on failure
  2864. * @user_data: Function specific data
  2865. * Returns: TRUE on success, FALSE on failure
  2866. *
  2867. * Setter function for "ScanInterval" property.
  2868. */
  2869. dbus_bool_t wpas_dbus_setter_scan_interval(
  2870. const struct wpa_dbus_property_desc *property_desc,
  2871. DBusMessageIter *iter, DBusError *error, void *user_data)
  2872. {
  2873. struct wpa_supplicant *wpa_s = user_data;
  2874. dbus_int32_t scan_interval;
  2875. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_INT32,
  2876. &scan_interval))
  2877. return FALSE;
  2878. if (wpa_supplicant_set_scan_interval(wpa_s, scan_interval)) {
  2879. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2880. "scan_interval must be >= 0");
  2881. return FALSE;
  2882. }
  2883. return TRUE;
  2884. }
  2885. /**
  2886. * wpas_dbus_getter_ifname - Get interface name
  2887. * @iter: Pointer to incoming dbus message iter
  2888. * @error: Location to store error on failure
  2889. * @user_data: Function specific data
  2890. * Returns: TRUE on success, FALSE on failure
  2891. *
  2892. * Getter for "Ifname" property.
  2893. */
  2894. dbus_bool_t wpas_dbus_getter_ifname(
  2895. const struct wpa_dbus_property_desc *property_desc,
  2896. DBusMessageIter *iter, DBusError *error, void *user_data)
  2897. {
  2898. struct wpa_supplicant *wpa_s = user_data;
  2899. return wpas_dbus_string_property_getter(iter, wpa_s->ifname, error);
  2900. }
  2901. /**
  2902. * wpas_dbus_getter_driver - Get interface name
  2903. * @iter: Pointer to incoming dbus message iter
  2904. * @error: Location to store error on failure
  2905. * @user_data: Function specific data
  2906. * Returns: TRUE on success, FALSE on failure
  2907. *
  2908. * Getter for "Driver" property.
  2909. */
  2910. dbus_bool_t wpas_dbus_getter_driver(
  2911. const struct wpa_dbus_property_desc *property_desc,
  2912. DBusMessageIter *iter, DBusError *error, void *user_data)
  2913. {
  2914. struct wpa_supplicant *wpa_s = user_data;
  2915. if (wpa_s->driver == NULL || wpa_s->driver->name == NULL) {
  2916. wpa_printf(MSG_DEBUG, "%s[dbus]: wpa_s has no driver set",
  2917. __func__);
  2918. dbus_set_error(error, DBUS_ERROR_FAILED, "%s: no driver set",
  2919. __func__);
  2920. return FALSE;
  2921. }
  2922. return wpas_dbus_string_property_getter(iter, wpa_s->driver->name,
  2923. error);
  2924. }
  2925. /**
  2926. * wpas_dbus_getter_current_bss - Get current bss object path
  2927. * @iter: Pointer to incoming dbus message iter
  2928. * @error: Location to store error on failure
  2929. * @user_data: Function specific data
  2930. * Returns: TRUE on success, FALSE on failure
  2931. *
  2932. * Getter for "CurrentBSS" property.
  2933. */
  2934. dbus_bool_t wpas_dbus_getter_current_bss(
  2935. const struct wpa_dbus_property_desc *property_desc,
  2936. DBusMessageIter *iter, DBusError *error, void *user_data)
  2937. {
  2938. struct wpa_supplicant *wpa_s = user_data;
  2939. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *bss_obj_path = path_buf;
  2940. if (wpa_s->current_bss && wpa_s->dbus_new_path)
  2941. os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  2942. "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
  2943. wpa_s->dbus_new_path, wpa_s->current_bss->id);
  2944. else
  2945. os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX, "/");
  2946. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_OBJECT_PATH,
  2947. &bss_obj_path, error);
  2948. }
  2949. /**
  2950. * wpas_dbus_getter_current_network - Get current network object path
  2951. * @iter: Pointer to incoming dbus message iter
  2952. * @error: Location to store error on failure
  2953. * @user_data: Function specific data
  2954. * Returns: TRUE on success, FALSE on failure
  2955. *
  2956. * Getter for "CurrentNetwork" property.
  2957. */
  2958. dbus_bool_t wpas_dbus_getter_current_network(
  2959. const struct wpa_dbus_property_desc *property_desc,
  2960. DBusMessageIter *iter, DBusError *error, void *user_data)
  2961. {
  2962. struct wpa_supplicant *wpa_s = user_data;
  2963. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *net_obj_path = path_buf;
  2964. if (wpa_s->current_ssid && wpa_s->dbus_new_path)
  2965. os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  2966. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%u",
  2967. wpa_s->dbus_new_path, wpa_s->current_ssid->id);
  2968. else
  2969. os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX, "/");
  2970. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_OBJECT_PATH,
  2971. &net_obj_path, error);
  2972. }
  2973. /**
  2974. * wpas_dbus_getter_current_auth_mode - Get current authentication type
  2975. * @iter: Pointer to incoming dbus message iter
  2976. * @error: Location to store error on failure
  2977. * @user_data: Function specific data
  2978. * Returns: TRUE on success, FALSE on failure
  2979. *
  2980. * Getter for "CurrentAuthMode" property.
  2981. */
  2982. dbus_bool_t wpas_dbus_getter_current_auth_mode(
  2983. const struct wpa_dbus_property_desc *property_desc,
  2984. DBusMessageIter *iter, DBusError *error, void *user_data)
  2985. {
  2986. struct wpa_supplicant *wpa_s = user_data;
  2987. const char *eap_mode;
  2988. const char *auth_mode;
  2989. char eap_mode_buf[WPAS_DBUS_AUTH_MODE_MAX];
  2990. if (wpa_s->wpa_state != WPA_COMPLETED) {
  2991. auth_mode = "INACTIVE";
  2992. } else if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X ||
  2993. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  2994. eap_mode = wpa_supplicant_get_eap_mode(wpa_s);
  2995. os_snprintf(eap_mode_buf, WPAS_DBUS_AUTH_MODE_MAX,
  2996. "EAP-%s", eap_mode);
  2997. auth_mode = eap_mode_buf;
  2998. } else if (wpa_s->current_ssid) {
  2999. auth_mode = wpa_key_mgmt_txt(wpa_s->key_mgmt,
  3000. wpa_s->current_ssid->proto);
  3001. } else {
  3002. auth_mode = "UNKNOWN";
  3003. }
  3004. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  3005. &auth_mode, error);
  3006. }
  3007. /**
  3008. * wpas_dbus_getter_bridge_ifname - Get interface name
  3009. * @iter: Pointer to incoming dbus message iter
  3010. * @error: Location to store error on failure
  3011. * @user_data: Function specific data
  3012. * Returns: TRUE on success, FALSE on failure
  3013. *
  3014. * Getter for "BridgeIfname" property.
  3015. */
  3016. dbus_bool_t wpas_dbus_getter_bridge_ifname(
  3017. const struct wpa_dbus_property_desc *property_desc,
  3018. DBusMessageIter *iter, DBusError *error, void *user_data)
  3019. {
  3020. struct wpa_supplicant *wpa_s = user_data;
  3021. return wpas_dbus_string_property_getter(iter, wpa_s->bridge_ifname,
  3022. error);
  3023. }
  3024. /**
  3025. * wpas_dbus_getter_config_file - Get interface configuration file path
  3026. * @iter: Pointer to incoming dbus message iter
  3027. * @error: Location to store error on failure
  3028. * @user_data: Function specific data
  3029. * Returns: TRUE on success, FALSE on failure
  3030. *
  3031. * Getter for "ConfigFile" property.
  3032. */
  3033. dbus_bool_t wpas_dbus_getter_config_file(
  3034. const struct wpa_dbus_property_desc *property_desc,
  3035. DBusMessageIter *iter, DBusError *error, void *user_data)
  3036. {
  3037. struct wpa_supplicant *wpa_s = user_data;
  3038. return wpas_dbus_string_property_getter(iter, wpa_s->confname, error);
  3039. }
  3040. /**
  3041. * wpas_dbus_getter_bsss - Get array of BSSs objects
  3042. * @iter: Pointer to incoming dbus message iter
  3043. * @error: Location to store error on failure
  3044. * @user_data: Function specific data
  3045. * Returns: TRUE on success, FALSE on failure
  3046. *
  3047. * Getter for "BSSs" property.
  3048. */
  3049. dbus_bool_t wpas_dbus_getter_bsss(
  3050. const struct wpa_dbus_property_desc *property_desc,
  3051. DBusMessageIter *iter, DBusError *error, void *user_data)
  3052. {
  3053. struct wpa_supplicant *wpa_s = user_data;
  3054. struct wpa_bss *bss;
  3055. char **paths;
  3056. unsigned int i = 0;
  3057. dbus_bool_t success = FALSE;
  3058. if (!wpa_s->dbus_new_path) {
  3059. dbus_set_error(error, DBUS_ERROR_FAILED,
  3060. "%s: no D-Bus interface", __func__);
  3061. return FALSE;
  3062. }
  3063. paths = os_calloc(wpa_s->num_bss, sizeof(char *));
  3064. if (!paths) {
  3065. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3066. return FALSE;
  3067. }
  3068. /* Loop through scan results and append each result's object path */
  3069. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  3070. paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  3071. if (paths[i] == NULL) {
  3072. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
  3073. "no memory");
  3074. goto out;
  3075. }
  3076. /* Construct the object path for this BSS. */
  3077. os_snprintf(paths[i++], WPAS_DBUS_OBJECT_PATH_MAX,
  3078. "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
  3079. wpa_s->dbus_new_path, bss->id);
  3080. }
  3081. success = wpas_dbus_simple_array_property_getter(iter,
  3082. DBUS_TYPE_OBJECT_PATH,
  3083. paths, wpa_s->num_bss,
  3084. error);
  3085. out:
  3086. while (i)
  3087. os_free(paths[--i]);
  3088. os_free(paths);
  3089. return success;
  3090. }
  3091. /**
  3092. * wpas_dbus_getter_networks - Get array of networks objects
  3093. * @iter: Pointer to incoming dbus message iter
  3094. * @error: Location to store error on failure
  3095. * @user_data: Function specific data
  3096. * Returns: TRUE on success, FALSE on failure
  3097. *
  3098. * Getter for "Networks" property.
  3099. */
  3100. dbus_bool_t wpas_dbus_getter_networks(
  3101. const struct wpa_dbus_property_desc *property_desc,
  3102. DBusMessageIter *iter, DBusError *error, void *user_data)
  3103. {
  3104. struct wpa_supplicant *wpa_s = user_data;
  3105. struct wpa_ssid *ssid;
  3106. char **paths;
  3107. unsigned int i = 0, num = 0;
  3108. dbus_bool_t success = FALSE;
  3109. if (!wpa_s->dbus_new_path) {
  3110. dbus_set_error(error, DBUS_ERROR_FAILED,
  3111. "%s: no D-Bus interface", __func__);
  3112. return FALSE;
  3113. }
  3114. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  3115. if (!network_is_persistent_group(ssid))
  3116. num++;
  3117. paths = os_calloc(num, sizeof(char *));
  3118. if (!paths) {
  3119. dbus_set_error(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3120. return FALSE;
  3121. }
  3122. /* Loop through configured networks and append object path of each */
  3123. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  3124. if (network_is_persistent_group(ssid))
  3125. continue;
  3126. paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  3127. if (paths[i] == NULL) {
  3128. dbus_set_error(error, DBUS_ERROR_NO_MEMORY,
  3129. "no memory");
  3130. goto out;
  3131. }
  3132. /* Construct the object path for this network. */
  3133. os_snprintf(paths[i++], WPAS_DBUS_OBJECT_PATH_MAX,
  3134. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%d",
  3135. wpa_s->dbus_new_path, ssid->id);
  3136. }
  3137. success = wpas_dbus_simple_array_property_getter(iter,
  3138. DBUS_TYPE_OBJECT_PATH,
  3139. paths, num, error);
  3140. out:
  3141. while (i)
  3142. os_free(paths[--i]);
  3143. os_free(paths);
  3144. return success;
  3145. }
  3146. /**
  3147. * wpas_dbus_getter_pkcs11_engine_path - Get PKCS #11 engine path
  3148. * @iter: Pointer to incoming dbus message iter
  3149. * @error: Location to store error on failure
  3150. * @user_data: Function specific data
  3151. * Returns: A dbus message containing the PKCS #11 engine path
  3152. *
  3153. * Getter for "PKCS11EnginePath" property.
  3154. */
  3155. dbus_bool_t wpas_dbus_getter_pkcs11_engine_path(
  3156. const struct wpa_dbus_property_desc *property_desc,
  3157. DBusMessageIter *iter, DBusError *error, void *user_data)
  3158. {
  3159. struct wpa_supplicant *wpa_s = user_data;
  3160. return wpas_dbus_string_property_getter(iter,
  3161. wpa_s->conf->pkcs11_engine_path,
  3162. error);
  3163. }
  3164. /**
  3165. * wpas_dbus_getter_pkcs11_module_path - Get PKCS #11 module path
  3166. * @iter: Pointer to incoming dbus message iter
  3167. * @error: Location to store error on failure
  3168. * @user_data: Function specific data
  3169. * Returns: A dbus message containing the PKCS #11 module path
  3170. *
  3171. * Getter for "PKCS11ModulePath" property.
  3172. */
  3173. dbus_bool_t wpas_dbus_getter_pkcs11_module_path(
  3174. const struct wpa_dbus_property_desc *property_desc,
  3175. DBusMessageIter *iter, DBusError *error, void *user_data)
  3176. {
  3177. struct wpa_supplicant *wpa_s = user_data;
  3178. return wpas_dbus_string_property_getter(iter,
  3179. wpa_s->conf->pkcs11_module_path,
  3180. error);
  3181. }
  3182. /**
  3183. * wpas_dbus_getter_blobs - Get all blobs defined for this interface
  3184. * @iter: Pointer to incoming dbus message iter
  3185. * @error: Location to store error on failure
  3186. * @user_data: Function specific data
  3187. * Returns: TRUE on success, FALSE on failure
  3188. *
  3189. * Getter for "Blobs" property.
  3190. */
  3191. dbus_bool_t wpas_dbus_getter_blobs(
  3192. const struct wpa_dbus_property_desc *property_desc,
  3193. DBusMessageIter *iter, DBusError *error, void *user_data)
  3194. {
  3195. struct wpa_supplicant *wpa_s = user_data;
  3196. DBusMessageIter variant_iter, dict_iter, entry_iter, array_iter;
  3197. struct wpa_config_blob *blob;
  3198. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  3199. "a{say}", &variant_iter) ||
  3200. !dbus_message_iter_open_container(&variant_iter, DBUS_TYPE_ARRAY,
  3201. "{say}", &dict_iter)) {
  3202. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3203. return FALSE;
  3204. }
  3205. blob = wpa_s->conf->blobs;
  3206. while (blob) {
  3207. if (!dbus_message_iter_open_container(&dict_iter,
  3208. DBUS_TYPE_DICT_ENTRY,
  3209. NULL, &entry_iter) ||
  3210. !dbus_message_iter_append_basic(&entry_iter,
  3211. DBUS_TYPE_STRING,
  3212. &(blob->name)) ||
  3213. !dbus_message_iter_open_container(&entry_iter,
  3214. DBUS_TYPE_ARRAY,
  3215. DBUS_TYPE_BYTE_AS_STRING,
  3216. &array_iter) ||
  3217. !dbus_message_iter_append_fixed_array(&array_iter,
  3218. DBUS_TYPE_BYTE,
  3219. &(blob->data),
  3220. blob->len) ||
  3221. !dbus_message_iter_close_container(&entry_iter,
  3222. &array_iter) ||
  3223. !dbus_message_iter_close_container(&dict_iter,
  3224. &entry_iter)) {
  3225. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
  3226. "no memory");
  3227. return FALSE;
  3228. }
  3229. blob = blob->next;
  3230. }
  3231. if (!dbus_message_iter_close_container(&variant_iter, &dict_iter) ||
  3232. !dbus_message_iter_close_container(iter, &variant_iter)) {
  3233. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3234. return FALSE;
  3235. }
  3236. return TRUE;
  3237. }
  3238. dbus_bool_t wpas_dbus_getter_iface_global(
  3239. const struct wpa_dbus_property_desc *property_desc,
  3240. DBusMessageIter *iter, DBusError *error, void *user_data)
  3241. {
  3242. struct wpa_supplicant *wpa_s = user_data;
  3243. int ret;
  3244. char buf[250];
  3245. char *p = buf;
  3246. if (!property_desc->data) {
  3247. dbus_set_error(error, DBUS_ERROR_INVALID_ARGS,
  3248. "Unhandled interface property %s",
  3249. property_desc->dbus_property);
  3250. return FALSE;
  3251. }
  3252. ret = wpa_config_get_value(property_desc->data, wpa_s->conf, buf,
  3253. sizeof(buf));
  3254. if (ret < 0)
  3255. *p = '\0';
  3256. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING, &p,
  3257. error);
  3258. }
  3259. dbus_bool_t wpas_dbus_setter_iface_global(
  3260. const struct wpa_dbus_property_desc *property_desc,
  3261. DBusMessageIter *iter, DBusError *error, void *user_data)
  3262. {
  3263. struct wpa_supplicant *wpa_s = user_data;
  3264. const char *new_value = NULL;
  3265. char buf[250];
  3266. size_t combined_len;
  3267. int ret;
  3268. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_STRING,
  3269. &new_value))
  3270. return FALSE;
  3271. combined_len = os_strlen(property_desc->data) + os_strlen(new_value) +
  3272. 3;
  3273. if (combined_len >= sizeof(buf)) {
  3274. dbus_set_error(error, DBUS_ERROR_INVALID_ARGS,
  3275. "Interface property %s value too large",
  3276. property_desc->dbus_property);
  3277. return FALSE;
  3278. }
  3279. if (!new_value[0])
  3280. new_value = "NULL";
  3281. ret = os_snprintf(buf, combined_len, "%s=%s", property_desc->data,
  3282. new_value);
  3283. if (os_snprintf_error(combined_len, ret)) {
  3284. dbus_set_error(error, WPAS_DBUS_ERROR_UNKNOWN_ERROR,
  3285. "Failed to construct new interface property %s",
  3286. property_desc->dbus_property);
  3287. return FALSE;
  3288. }
  3289. if (wpa_config_process_global(wpa_s->conf, buf, -1)) {
  3290. dbus_set_error(error, DBUS_ERROR_INVALID_ARGS,
  3291. "Failed to set interface property %s",
  3292. property_desc->dbus_property);
  3293. return FALSE;
  3294. }
  3295. wpa_supplicant_update_config(wpa_s);
  3296. return TRUE;
  3297. }
  3298. static struct wpa_bss * get_bss_helper(struct bss_handler_args *args,
  3299. DBusError *error, const char *func_name)
  3300. {
  3301. struct wpa_bss *res = wpa_bss_get_id(args->wpa_s, args->id);
  3302. if (!res) {
  3303. wpa_printf(MSG_ERROR, "%s[dbus]: no bss with id %d found",
  3304. func_name, args->id);
  3305. dbus_set_error(error, DBUS_ERROR_FAILED,
  3306. "%s: BSS %d not found",
  3307. func_name, args->id);
  3308. }
  3309. return res;
  3310. }
  3311. /**
  3312. * wpas_dbus_getter_bss_bssid - Return the BSSID of a BSS
  3313. * @iter: Pointer to incoming dbus message iter
  3314. * @error: Location to store error on failure
  3315. * @user_data: Function specific data
  3316. * Returns: TRUE on success, FALSE on failure
  3317. *
  3318. * Getter for "BSSID" property.
  3319. */
  3320. dbus_bool_t wpas_dbus_getter_bss_bssid(
  3321. const struct wpa_dbus_property_desc *property_desc,
  3322. DBusMessageIter *iter, DBusError *error, void *user_data)
  3323. {
  3324. struct bss_handler_args *args = user_data;
  3325. struct wpa_bss *res;
  3326. res = get_bss_helper(args, error, __func__);
  3327. if (!res)
  3328. return FALSE;
  3329. return wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
  3330. res->bssid, ETH_ALEN,
  3331. error);
  3332. }
  3333. /**
  3334. * wpas_dbus_getter_bss_ssid - Return the SSID of a BSS
  3335. * @iter: Pointer to incoming dbus message iter
  3336. * @error: Location to store error on failure
  3337. * @user_data: Function specific data
  3338. * Returns: TRUE on success, FALSE on failure
  3339. *
  3340. * Getter for "SSID" property.
  3341. */
  3342. dbus_bool_t wpas_dbus_getter_bss_ssid(
  3343. const struct wpa_dbus_property_desc *property_desc,
  3344. DBusMessageIter *iter, DBusError *error, void *user_data)
  3345. {
  3346. struct bss_handler_args *args = user_data;
  3347. struct wpa_bss *res;
  3348. res = get_bss_helper(args, error, __func__);
  3349. if (!res)
  3350. return FALSE;
  3351. return wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
  3352. res->ssid, res->ssid_len,
  3353. error);
  3354. }
  3355. /**
  3356. * wpas_dbus_getter_bss_privacy - Return the privacy flag of a BSS
  3357. * @iter: Pointer to incoming dbus message iter
  3358. * @error: Location to store error on failure
  3359. * @user_data: Function specific data
  3360. * Returns: TRUE on success, FALSE on failure
  3361. *
  3362. * Getter for "Privacy" property.
  3363. */
  3364. dbus_bool_t wpas_dbus_getter_bss_privacy(
  3365. const struct wpa_dbus_property_desc *property_desc,
  3366. DBusMessageIter *iter, DBusError *error, void *user_data)
  3367. {
  3368. struct bss_handler_args *args = user_data;
  3369. struct wpa_bss *res;
  3370. dbus_bool_t privacy;
  3371. res = get_bss_helper(args, error, __func__);
  3372. if (!res)
  3373. return FALSE;
  3374. privacy = (res->caps & IEEE80211_CAP_PRIVACY) ? TRUE : FALSE;
  3375. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  3376. &privacy, error);
  3377. }
  3378. /**
  3379. * wpas_dbus_getter_bss_mode - Return the mode of a BSS
  3380. * @iter: Pointer to incoming dbus message iter
  3381. * @error: Location to store error on failure
  3382. * @user_data: Function specific data
  3383. * Returns: TRUE on success, FALSE on failure
  3384. *
  3385. * Getter for "Mode" property.
  3386. */
  3387. dbus_bool_t wpas_dbus_getter_bss_mode(
  3388. const struct wpa_dbus_property_desc *property_desc,
  3389. DBusMessageIter *iter, DBusError *error, void *user_data)
  3390. {
  3391. struct bss_handler_args *args = user_data;
  3392. struct wpa_bss *res;
  3393. const char *mode;
  3394. const u8 *mesh;
  3395. res = get_bss_helper(args, error, __func__);
  3396. if (!res)
  3397. return FALSE;
  3398. if (bss_is_dmg(res)) {
  3399. switch (res->caps & IEEE80211_CAP_DMG_MASK) {
  3400. case IEEE80211_CAP_DMG_PBSS:
  3401. case IEEE80211_CAP_DMG_IBSS:
  3402. mode = "ad-hoc";
  3403. break;
  3404. case IEEE80211_CAP_DMG_AP:
  3405. mode = "infrastructure";
  3406. break;
  3407. default:
  3408. mode = "";
  3409. break;
  3410. }
  3411. } else {
  3412. mesh = wpa_bss_get_ie(res, WLAN_EID_MESH_ID);
  3413. if (mesh)
  3414. mode = "mesh";
  3415. else if (res->caps & IEEE80211_CAP_IBSS)
  3416. mode = "ad-hoc";
  3417. else
  3418. mode = "infrastructure";
  3419. }
  3420. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  3421. &mode, error);
  3422. }
  3423. /**
  3424. * wpas_dbus_getter_bss_level - Return the signal strength of a BSS
  3425. * @iter: Pointer to incoming dbus message iter
  3426. * @error: Location to store error on failure
  3427. * @user_data: Function specific data
  3428. * Returns: TRUE on success, FALSE on failure
  3429. *
  3430. * Getter for "Level" property.
  3431. */
  3432. dbus_bool_t wpas_dbus_getter_bss_signal(
  3433. const struct wpa_dbus_property_desc *property_desc,
  3434. DBusMessageIter *iter, DBusError *error, void *user_data)
  3435. {
  3436. struct bss_handler_args *args = user_data;
  3437. struct wpa_bss *res;
  3438. s16 level;
  3439. res = get_bss_helper(args, error, __func__);
  3440. if (!res)
  3441. return FALSE;
  3442. level = (s16) res->level;
  3443. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_INT16,
  3444. &level, error);
  3445. }
  3446. /**
  3447. * wpas_dbus_getter_bss_frequency - Return the frequency of a BSS
  3448. * @iter: Pointer to incoming dbus message iter
  3449. * @error: Location to store error on failure
  3450. * @user_data: Function specific data
  3451. * Returns: TRUE on success, FALSE on failure
  3452. *
  3453. * Getter for "Frequency" property.
  3454. */
  3455. dbus_bool_t wpas_dbus_getter_bss_frequency(
  3456. const struct wpa_dbus_property_desc *property_desc,
  3457. DBusMessageIter *iter, DBusError *error, void *user_data)
  3458. {
  3459. struct bss_handler_args *args = user_data;
  3460. struct wpa_bss *res;
  3461. u16 freq;
  3462. res = get_bss_helper(args, error, __func__);
  3463. if (!res)
  3464. return FALSE;
  3465. freq = (u16) res->freq;
  3466. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT16,
  3467. &freq, error);
  3468. }
  3469. static int cmp_u8s_desc(const void *a, const void *b)
  3470. {
  3471. return (*(u8 *) b - *(u8 *) a);
  3472. }
  3473. /**
  3474. * wpas_dbus_getter_bss_rates - Return available bit rates of a BSS
  3475. * @iter: Pointer to incoming dbus message iter
  3476. * @error: Location to store error on failure
  3477. * @user_data: Function specific data
  3478. * Returns: TRUE on success, FALSE on failure
  3479. *
  3480. * Getter for "Rates" property.
  3481. */
  3482. dbus_bool_t wpas_dbus_getter_bss_rates(
  3483. const struct wpa_dbus_property_desc *property_desc,
  3484. DBusMessageIter *iter, DBusError *error, void *user_data)
  3485. {
  3486. struct bss_handler_args *args = user_data;
  3487. struct wpa_bss *res;
  3488. u8 *ie_rates = NULL;
  3489. u32 *real_rates;
  3490. int rates_num, i;
  3491. dbus_bool_t success = FALSE;
  3492. res = get_bss_helper(args, error, __func__);
  3493. if (!res)
  3494. return FALSE;
  3495. rates_num = wpa_bss_get_bit_rates(res, &ie_rates);
  3496. if (rates_num < 0)
  3497. return FALSE;
  3498. qsort(ie_rates, rates_num, 1, cmp_u8s_desc);
  3499. real_rates = os_malloc(sizeof(u32) * rates_num);
  3500. if (!real_rates) {
  3501. os_free(ie_rates);
  3502. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3503. return FALSE;
  3504. }
  3505. for (i = 0; i < rates_num; i++)
  3506. real_rates[i] = ie_rates[i] * 500000;
  3507. success = wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_UINT32,
  3508. real_rates, rates_num,
  3509. error);
  3510. os_free(ie_rates);
  3511. os_free(real_rates);
  3512. return success;
  3513. }
  3514. static dbus_bool_t wpas_dbus_get_bss_security_prop(
  3515. const struct wpa_dbus_property_desc *property_desc,
  3516. DBusMessageIter *iter, struct wpa_ie_data *ie_data, DBusError *error)
  3517. {
  3518. DBusMessageIter iter_dict, variant_iter;
  3519. const char *group;
  3520. const char *pairwise[5]; /* max 5 pairwise ciphers is supported */
  3521. const char *key_mgmt[9]; /* max 9 key managements may be supported */
  3522. int n;
  3523. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  3524. "a{sv}", &variant_iter))
  3525. goto nomem;
  3526. if (!wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
  3527. goto nomem;
  3528. /* KeyMgmt */
  3529. n = 0;
  3530. if (ie_data->key_mgmt & WPA_KEY_MGMT_PSK)
  3531. key_mgmt[n++] = "wpa-psk";
  3532. if (ie_data->key_mgmt & WPA_KEY_MGMT_FT_PSK)
  3533. key_mgmt[n++] = "wpa-ft-psk";
  3534. if (ie_data->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
  3535. key_mgmt[n++] = "wpa-psk-sha256";
  3536. if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  3537. key_mgmt[n++] = "wpa-eap";
  3538. if (ie_data->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
  3539. key_mgmt[n++] = "wpa-ft-eap";
  3540. if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
  3541. key_mgmt[n++] = "wpa-eap-sha256";
  3542. #ifdef CONFIG_SUITEB
  3543. if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B)
  3544. key_mgmt[n++] = "wpa-eap-suite-b";
  3545. #endif /* CONFIG_SUITEB */
  3546. #ifdef CONFIG_SUITEB192
  3547. if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
  3548. key_mgmt[n++] = "wpa-eap-suite-b-192";
  3549. #endif /* CONFIG_SUITEB192 */
  3550. if (ie_data->key_mgmt & WPA_KEY_MGMT_NONE)
  3551. key_mgmt[n++] = "wpa-none";
  3552. if (!wpa_dbus_dict_append_string_array(&iter_dict, "KeyMgmt",
  3553. key_mgmt, n))
  3554. goto nomem;
  3555. /* Group */
  3556. switch (ie_data->group_cipher) {
  3557. case WPA_CIPHER_WEP40:
  3558. group = "wep40";
  3559. break;
  3560. case WPA_CIPHER_TKIP:
  3561. group = "tkip";
  3562. break;
  3563. case WPA_CIPHER_CCMP:
  3564. group = "ccmp";
  3565. break;
  3566. case WPA_CIPHER_GCMP:
  3567. group = "gcmp";
  3568. break;
  3569. case WPA_CIPHER_WEP104:
  3570. group = "wep104";
  3571. break;
  3572. case WPA_CIPHER_CCMP_256:
  3573. group = "ccmp-256";
  3574. break;
  3575. case WPA_CIPHER_GCMP_256:
  3576. group = "gcmp-256";
  3577. break;
  3578. default:
  3579. group = "";
  3580. break;
  3581. }
  3582. if (!wpa_dbus_dict_append_string(&iter_dict, "Group", group))
  3583. goto nomem;
  3584. /* Pairwise */
  3585. n = 0;
  3586. if (ie_data->pairwise_cipher & WPA_CIPHER_TKIP)
  3587. pairwise[n++] = "tkip";
  3588. if (ie_data->pairwise_cipher & WPA_CIPHER_CCMP)
  3589. pairwise[n++] = "ccmp";
  3590. if (ie_data->pairwise_cipher & WPA_CIPHER_GCMP)
  3591. pairwise[n++] = "gcmp";
  3592. if (ie_data->pairwise_cipher & WPA_CIPHER_CCMP_256)
  3593. pairwise[n++] = "ccmp-256";
  3594. if (ie_data->pairwise_cipher & WPA_CIPHER_GCMP_256)
  3595. pairwise[n++] = "gcmp-256";
  3596. if (!wpa_dbus_dict_append_string_array(&iter_dict, "Pairwise",
  3597. pairwise, n))
  3598. goto nomem;
  3599. /* Management group (RSN only) */
  3600. if (ie_data->proto == WPA_PROTO_RSN) {
  3601. switch (ie_data->mgmt_group_cipher) {
  3602. #ifdef CONFIG_IEEE80211W
  3603. case WPA_CIPHER_AES_128_CMAC:
  3604. group = "aes128cmac";
  3605. break;
  3606. #endif /* CONFIG_IEEE80211W */
  3607. default:
  3608. group = "";
  3609. break;
  3610. }
  3611. if (!wpa_dbus_dict_append_string(&iter_dict, "MgmtGroup",
  3612. group))
  3613. goto nomem;
  3614. }
  3615. if (!wpa_dbus_dict_close_write(&variant_iter, &iter_dict) ||
  3616. !dbus_message_iter_close_container(iter, &variant_iter))
  3617. goto nomem;
  3618. return TRUE;
  3619. nomem:
  3620. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3621. return FALSE;
  3622. }
  3623. /**
  3624. * wpas_dbus_getter_bss_wpa - Return the WPA options of a BSS
  3625. * @iter: Pointer to incoming dbus message iter
  3626. * @error: Location to store error on failure
  3627. * @user_data: Function specific data
  3628. * Returns: TRUE on success, FALSE on failure
  3629. *
  3630. * Getter for "WPA" property.
  3631. */
  3632. dbus_bool_t wpas_dbus_getter_bss_wpa(
  3633. const struct wpa_dbus_property_desc *property_desc,
  3634. DBusMessageIter *iter, DBusError *error, void *user_data)
  3635. {
  3636. struct bss_handler_args *args = user_data;
  3637. struct wpa_bss *res;
  3638. struct wpa_ie_data wpa_data;
  3639. const u8 *ie;
  3640. res = get_bss_helper(args, error, __func__);
  3641. if (!res)
  3642. return FALSE;
  3643. os_memset(&wpa_data, 0, sizeof(wpa_data));
  3644. ie = wpa_bss_get_vendor_ie(res, WPA_IE_VENDOR_TYPE);
  3645. if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &wpa_data) < 0) {
  3646. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  3647. "failed to parse WPA IE");
  3648. return FALSE;
  3649. }
  3650. return wpas_dbus_get_bss_security_prop(property_desc, iter, &wpa_data, error);
  3651. }
  3652. /**
  3653. * wpas_dbus_getter_bss_rsn - Return the RSN options of a BSS
  3654. * @iter: Pointer to incoming dbus message iter
  3655. * @error: Location to store error on failure
  3656. * @user_data: Function specific data
  3657. * Returns: TRUE on success, FALSE on failure
  3658. *
  3659. * Getter for "RSN" property.
  3660. */
  3661. dbus_bool_t wpas_dbus_getter_bss_rsn(
  3662. const struct wpa_dbus_property_desc *property_desc,
  3663. DBusMessageIter *iter, DBusError *error, void *user_data)
  3664. {
  3665. struct bss_handler_args *args = user_data;
  3666. struct wpa_bss *res;
  3667. struct wpa_ie_data wpa_data;
  3668. const u8 *ie;
  3669. res = get_bss_helper(args, error, __func__);
  3670. if (!res)
  3671. return FALSE;
  3672. os_memset(&wpa_data, 0, sizeof(wpa_data));
  3673. ie = wpa_bss_get_ie(res, WLAN_EID_RSN);
  3674. if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &wpa_data) < 0) {
  3675. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  3676. "failed to parse RSN IE");
  3677. return FALSE;
  3678. }
  3679. return wpas_dbus_get_bss_security_prop(property_desc, iter, &wpa_data, error);
  3680. }
  3681. /**
  3682. * wpas_dbus_getter_bss_wps - Return the WPS options of a BSS
  3683. * @iter: Pointer to incoming dbus message iter
  3684. * @error: Location to store error on failure
  3685. * @user_data: Function specific data
  3686. * Returns: TRUE on success, FALSE on failure
  3687. *
  3688. * Getter for "WPS" property.
  3689. */
  3690. dbus_bool_t wpas_dbus_getter_bss_wps(
  3691. const struct wpa_dbus_property_desc *property_desc,
  3692. DBusMessageIter *iter, DBusError *error, void *user_data)
  3693. {
  3694. struct bss_handler_args *args = user_data;
  3695. struct wpa_bss *res;
  3696. #ifdef CONFIG_WPS
  3697. struct wpabuf *wps_ie;
  3698. #endif /* CONFIG_WPS */
  3699. DBusMessageIter iter_dict, variant_iter;
  3700. int wps_support = 0;
  3701. const char *type = "";
  3702. res = get_bss_helper(args, error, __func__);
  3703. if (!res)
  3704. return FALSE;
  3705. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  3706. "a{sv}", &variant_iter) ||
  3707. !wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
  3708. goto nomem;
  3709. #ifdef CONFIG_WPS
  3710. wps_ie = wpa_bss_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
  3711. if (wps_ie) {
  3712. wps_support = 1;
  3713. if (wps_is_selected_pbc_registrar(wps_ie))
  3714. type = "pbc";
  3715. else if (wps_is_selected_pin_registrar(wps_ie))
  3716. type = "pin";
  3717. wpabuf_free(wps_ie);
  3718. }
  3719. #endif /* CONFIG_WPS */
  3720. if ((wps_support && !wpa_dbus_dict_append_string(&iter_dict, "Type", type)) ||
  3721. !wpa_dbus_dict_close_write(&variant_iter, &iter_dict) ||
  3722. !dbus_message_iter_close_container(iter, &variant_iter))
  3723. goto nomem;
  3724. return TRUE;
  3725. nomem:
  3726. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3727. return FALSE;
  3728. }
  3729. /**
  3730. * wpas_dbus_getter_bss_ies - Return all IEs of a BSS
  3731. * @iter: Pointer to incoming dbus message iter
  3732. * @error: Location to store error on failure
  3733. * @user_data: Function specific data
  3734. * Returns: TRUE on success, FALSE on failure
  3735. *
  3736. * Getter for "IEs" property.
  3737. */
  3738. dbus_bool_t wpas_dbus_getter_bss_ies(
  3739. const struct wpa_dbus_property_desc *property_desc,
  3740. DBusMessageIter *iter, DBusError *error, void *user_data)
  3741. {
  3742. struct bss_handler_args *args = user_data;
  3743. struct wpa_bss *res;
  3744. res = get_bss_helper(args, error, __func__);
  3745. if (!res)
  3746. return FALSE;
  3747. return wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
  3748. res + 1, res->ie_len,
  3749. error);
  3750. }
  3751. /**
  3752. * wpas_dbus_getter_bss_age - Return time in seconds since BSS was last seen
  3753. * @iter: Pointer to incoming dbus message iter
  3754. * @error: Location to store error on failure
  3755. * @user_data: Function specific data
  3756. * Returns: TRUE on success, FALSE on failure
  3757. *
  3758. * Getter for BSS age
  3759. */
  3760. dbus_bool_t wpas_dbus_getter_bss_age(
  3761. const struct wpa_dbus_property_desc *property_desc,
  3762. DBusMessageIter *iter, DBusError *error, void *user_data)
  3763. {
  3764. struct bss_handler_args *args = user_data;
  3765. struct wpa_bss *res;
  3766. struct os_reltime now, diff = { 0, 0 };
  3767. u32 age;
  3768. res = get_bss_helper(args, error, __func__);
  3769. if (!res)
  3770. return FALSE;
  3771. os_get_reltime(&now);
  3772. os_reltime_sub(&now, &res->last_update, &diff);
  3773. age = diff.sec > 0 ? diff.sec : 0;
  3774. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT32, &age,
  3775. error);
  3776. }
  3777. /**
  3778. * wpas_dbus_getter_enabled - Check whether network is enabled or disabled
  3779. * @iter: Pointer to incoming dbus message iter
  3780. * @error: Location to store error on failure
  3781. * @user_data: Function specific data
  3782. * Returns: TRUE on success, FALSE on failure
  3783. *
  3784. * Getter for "enabled" property of a configured network.
  3785. */
  3786. dbus_bool_t wpas_dbus_getter_enabled(
  3787. const struct wpa_dbus_property_desc *property_desc,
  3788. DBusMessageIter *iter, DBusError *error, void *user_data)
  3789. {
  3790. struct network_handler_args *net = user_data;
  3791. dbus_bool_t enabled = net->ssid->disabled ? FALSE : TRUE;
  3792. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  3793. &enabled, error);
  3794. }
  3795. /**
  3796. * wpas_dbus_setter_enabled - Mark a configured network as enabled or disabled
  3797. * @iter: Pointer to incoming dbus message iter
  3798. * @error: Location to store error on failure
  3799. * @user_data: Function specific data
  3800. * Returns: TRUE on success, FALSE on failure
  3801. *
  3802. * Setter for "Enabled" property of a configured network.
  3803. */
  3804. dbus_bool_t wpas_dbus_setter_enabled(
  3805. const struct wpa_dbus_property_desc *property_desc,
  3806. DBusMessageIter *iter, DBusError *error, void *user_data)
  3807. {
  3808. struct network_handler_args *net = user_data;
  3809. struct wpa_supplicant *wpa_s;
  3810. struct wpa_ssid *ssid;
  3811. dbus_bool_t enable;
  3812. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_BOOLEAN,
  3813. &enable))
  3814. return FALSE;
  3815. wpa_s = net->wpa_s;
  3816. ssid = net->ssid;
  3817. if (enable)
  3818. wpa_supplicant_enable_network(wpa_s, ssid);
  3819. else
  3820. wpa_supplicant_disable_network(wpa_s, ssid);
  3821. return TRUE;
  3822. }
  3823. /**
  3824. * wpas_dbus_getter_network_properties - Get options for a configured network
  3825. * @iter: Pointer to incoming dbus message iter
  3826. * @error: Location to store error on failure
  3827. * @user_data: Function specific data
  3828. * Returns: TRUE on success, FALSE on failure
  3829. *
  3830. * Getter for "Properties" property of a configured network.
  3831. */
  3832. dbus_bool_t wpas_dbus_getter_network_properties(
  3833. const struct wpa_dbus_property_desc *property_desc,
  3834. DBusMessageIter *iter, DBusError *error, void *user_data)
  3835. {
  3836. struct network_handler_args *net = user_data;
  3837. DBusMessageIter variant_iter, dict_iter;
  3838. char **iterator;
  3839. char **props = wpa_config_get_all(net->ssid, 1);
  3840. dbus_bool_t success = FALSE;
  3841. if (!props) {
  3842. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3843. return FALSE;
  3844. }
  3845. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "a{sv}",
  3846. &variant_iter) ||
  3847. !wpa_dbus_dict_open_write(&variant_iter, &dict_iter)) {
  3848. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3849. goto out;
  3850. }
  3851. iterator = props;
  3852. while (*iterator) {
  3853. if (!wpa_dbus_dict_append_string(&dict_iter, *iterator,
  3854. *(iterator + 1))) {
  3855. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
  3856. "no memory");
  3857. goto out;
  3858. }
  3859. iterator += 2;
  3860. }
  3861. if (!wpa_dbus_dict_close_write(&variant_iter, &dict_iter) ||
  3862. !dbus_message_iter_close_container(iter, &variant_iter)) {
  3863. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3864. goto out;
  3865. }
  3866. success = TRUE;
  3867. out:
  3868. iterator = props;
  3869. while (*iterator) {
  3870. os_free(*iterator);
  3871. iterator++;
  3872. }
  3873. os_free(props);
  3874. return success;
  3875. }
  3876. /**
  3877. * wpas_dbus_setter_network_properties - Set options for a configured network
  3878. * @iter: Pointer to incoming dbus message iter
  3879. * @error: Location to store error on failure
  3880. * @user_data: Function specific data
  3881. * Returns: TRUE on success, FALSE on failure
  3882. *
  3883. * Setter for "Properties" property of a configured network.
  3884. */
  3885. dbus_bool_t wpas_dbus_setter_network_properties(
  3886. const struct wpa_dbus_property_desc *property_desc,
  3887. DBusMessageIter *iter, DBusError *error, void *user_data)
  3888. {
  3889. struct network_handler_args *net = user_data;
  3890. struct wpa_ssid *ssid = net->ssid;
  3891. DBusMessageIter variant_iter;
  3892. dbus_message_iter_recurse(iter, &variant_iter);
  3893. return set_network_properties(net->wpa_s, ssid, &variant_iter, error);
  3894. }
  3895. #ifdef CONFIG_AP
  3896. DBusMessage * wpas_dbus_handler_subscribe_preq(
  3897. DBusMessage *message, struct wpa_supplicant *wpa_s)
  3898. {
  3899. struct wpas_dbus_priv *priv = wpa_s->global->dbus;
  3900. char *name;
  3901. if (wpa_s->preq_notify_peer != NULL) {
  3902. if (os_strcmp(dbus_message_get_sender(message),
  3903. wpa_s->preq_notify_peer) == 0)
  3904. return NULL;
  3905. return dbus_message_new_error(message,
  3906. WPAS_DBUS_ERROR_SUBSCRIPTION_IN_USE,
  3907. "Another application is already subscribed");
  3908. }
  3909. name = os_strdup(dbus_message_get_sender(message));
  3910. if (!name)
  3911. return wpas_dbus_error_no_memory(message);
  3912. wpa_s->preq_notify_peer = name;
  3913. /* Subscribe to clean up if application closes socket */
  3914. wpas_dbus_subscribe_noc(priv);
  3915. /*
  3916. * Double-check it's still alive to make sure that we didn't
  3917. * miss the NameOwnerChanged signal, e.g. while strdup'ing.
  3918. */
  3919. if (!dbus_bus_name_has_owner(priv->con, name, NULL)) {
  3920. /*
  3921. * Application no longer exists, clean up.
  3922. * The return value is irrelevant now.
  3923. *
  3924. * Need to check if the NameOwnerChanged handling
  3925. * already cleaned up because we have processed
  3926. * DBus messages while checking if the name still
  3927. * has an owner.
  3928. */
  3929. if (!wpa_s->preq_notify_peer)
  3930. return NULL;
  3931. os_free(wpa_s->preq_notify_peer);
  3932. wpa_s->preq_notify_peer = NULL;
  3933. wpas_dbus_unsubscribe_noc(priv);
  3934. }
  3935. return NULL;
  3936. }
  3937. DBusMessage * wpas_dbus_handler_unsubscribe_preq(
  3938. DBusMessage *message, struct wpa_supplicant *wpa_s)
  3939. {
  3940. struct wpas_dbus_priv *priv = wpa_s->global->dbus;
  3941. if (!wpa_s->preq_notify_peer)
  3942. return dbus_message_new_error(message,
  3943. WPAS_DBUS_ERROR_NO_SUBSCRIPTION,
  3944. "Not subscribed");
  3945. if (os_strcmp(wpa_s->preq_notify_peer,
  3946. dbus_message_get_sender(message)))
  3947. return dbus_message_new_error(message,
  3948. WPAS_DBUS_ERROR_SUBSCRIPTION_EPERM,
  3949. "Can't unsubscribe others");
  3950. os_free(wpa_s->preq_notify_peer);
  3951. wpa_s->preq_notify_peer = NULL;
  3952. wpas_dbus_unsubscribe_noc(priv);
  3953. return NULL;
  3954. }
  3955. void wpas_dbus_signal_preq(struct wpa_supplicant *wpa_s,
  3956. const u8 *addr, const u8 *dst, const u8 *bssid,
  3957. const u8 *ie, size_t ie_len, u32 ssi_signal)
  3958. {
  3959. DBusMessage *msg;
  3960. DBusMessageIter iter, dict_iter;
  3961. struct wpas_dbus_priv *priv = wpa_s->global->dbus;
  3962. /* Do nothing if the control interface is not turned on */
  3963. if (priv == NULL || !wpa_s->dbus_new_path)
  3964. return;
  3965. if (wpa_s->preq_notify_peer == NULL)
  3966. return;
  3967. msg = dbus_message_new_signal(wpa_s->dbus_new_path,
  3968. WPAS_DBUS_NEW_IFACE_INTERFACE,
  3969. "ProbeRequest");
  3970. if (msg == NULL)
  3971. return;
  3972. dbus_message_set_destination(msg, wpa_s->preq_notify_peer);
  3973. dbus_message_iter_init_append(msg, &iter);
  3974. if (!wpa_dbus_dict_open_write(&iter, &dict_iter) ||
  3975. (addr && !wpa_dbus_dict_append_byte_array(&dict_iter, "addr",
  3976. (const char *) addr,
  3977. ETH_ALEN)) ||
  3978. (dst && !wpa_dbus_dict_append_byte_array(&dict_iter, "dst",
  3979. (const char *) dst,
  3980. ETH_ALEN)) ||
  3981. (bssid && !wpa_dbus_dict_append_byte_array(&dict_iter, "bssid",
  3982. (const char *) bssid,
  3983. ETH_ALEN)) ||
  3984. (ie && ie_len && !wpa_dbus_dict_append_byte_array(&dict_iter, "ies",
  3985. (const char *) ie,
  3986. ie_len)) ||
  3987. (ssi_signal && !wpa_dbus_dict_append_int32(&dict_iter, "signal",
  3988. ssi_signal)) ||
  3989. !wpa_dbus_dict_close_write(&iter, &dict_iter))
  3990. goto fail;
  3991. dbus_connection_send(priv->con, msg, NULL);
  3992. goto out;
  3993. fail:
  3994. wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
  3995. out:
  3996. dbus_message_unref(msg);
  3997. }
  3998. #endif /* CONFIG_AP */
  3999. DBusMessage * wpas_dbus_handler_vendor_elem_add(DBusMessage *message,
  4000. struct wpa_supplicant *wpa_s)
  4001. {
  4002. u8 *ielems;
  4003. int len;
  4004. struct ieee802_11_elems elems;
  4005. dbus_int32_t frame_id;
  4006. DBusMessageIter iter, array;
  4007. dbus_message_iter_init(message, &iter);
  4008. dbus_message_iter_get_basic(&iter, &frame_id);
  4009. if (frame_id < 0 || frame_id >= NUM_VENDOR_ELEM_FRAMES) {
  4010. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  4011. "Invalid ID");
  4012. }
  4013. dbus_message_iter_next(&iter);
  4014. dbus_message_iter_recurse(&iter, &array);
  4015. dbus_message_iter_get_fixed_array(&array, &ielems, &len);
  4016. if (!ielems || len == 0) {
  4017. return dbus_message_new_error(
  4018. message, DBUS_ERROR_INVALID_ARGS, "Invalid value");
  4019. }
  4020. if (ieee802_11_parse_elems(ielems, len, &elems, 0) == ParseFailed) {
  4021. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  4022. "Parse error");
  4023. }
  4024. wpa_s = wpas_vendor_elem(wpa_s, frame_id);
  4025. if (!wpa_s->vendor_elem[frame_id]) {
  4026. wpa_s->vendor_elem[frame_id] = wpabuf_alloc_copy(ielems, len);
  4027. wpas_vendor_elem_update(wpa_s);
  4028. return NULL;
  4029. }
  4030. if (wpabuf_resize(&wpa_s->vendor_elem[frame_id], len) < 0) {
  4031. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  4032. "Resize error");
  4033. }
  4034. wpabuf_put_data(wpa_s->vendor_elem[frame_id], ielems, len);
  4035. wpas_vendor_elem_update(wpa_s);
  4036. return NULL;
  4037. }
  4038. DBusMessage * wpas_dbus_handler_vendor_elem_get(DBusMessage *message,
  4039. struct wpa_supplicant *wpa_s)
  4040. {
  4041. DBusMessage *reply;
  4042. DBusMessageIter iter, array_iter;
  4043. dbus_int32_t frame_id;
  4044. const u8 *elem;
  4045. size_t elem_len;
  4046. dbus_message_iter_init(message, &iter);
  4047. dbus_message_iter_get_basic(&iter, &frame_id);
  4048. if (frame_id < 0 || frame_id >= NUM_VENDOR_ELEM_FRAMES) {
  4049. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  4050. "Invalid ID");
  4051. }
  4052. wpa_s = wpas_vendor_elem(wpa_s, frame_id);
  4053. if (!wpa_s->vendor_elem[frame_id]) {
  4054. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  4055. "ID value does not exist");
  4056. }
  4057. reply = dbus_message_new_method_return(message);
  4058. if (!reply)
  4059. return wpas_dbus_error_no_memory(message);
  4060. dbus_message_iter_init_append(reply, &iter);
  4061. elem = wpabuf_head_u8(wpa_s->vendor_elem[frame_id]);
  4062. elem_len = wpabuf_len(wpa_s->vendor_elem[frame_id]);
  4063. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
  4064. DBUS_TYPE_BYTE_AS_STRING,
  4065. &array_iter) ||
  4066. !dbus_message_iter_append_fixed_array(&array_iter, DBUS_TYPE_BYTE,
  4067. &elem, elem_len) ||
  4068. !dbus_message_iter_close_container(&iter, &array_iter)) {
  4069. dbus_message_unref(reply);
  4070. reply = wpas_dbus_error_no_memory(message);
  4071. }
  4072. return reply;
  4073. }
  4074. DBusMessage * wpas_dbus_handler_vendor_elem_remove(DBusMessage *message,
  4075. struct wpa_supplicant *wpa_s)
  4076. {
  4077. u8 *ielems;
  4078. int len;
  4079. struct ieee802_11_elems elems;
  4080. DBusMessageIter iter, array;
  4081. dbus_int32_t frame_id;
  4082. dbus_message_iter_init(message, &iter);
  4083. dbus_message_iter_get_basic(&iter, &frame_id);
  4084. if (frame_id < 0 || frame_id >= NUM_VENDOR_ELEM_FRAMES) {
  4085. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  4086. "Invalid ID");
  4087. }
  4088. dbus_message_iter_next(&iter);
  4089. dbus_message_iter_recurse(&iter, &array);
  4090. dbus_message_iter_get_fixed_array(&array, &ielems, &len);
  4091. if (!ielems || len == 0) {
  4092. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  4093. "Invalid value");
  4094. }
  4095. wpa_s = wpas_vendor_elem(wpa_s, frame_id);
  4096. if (len == 1 && *ielems == '*') {
  4097. wpabuf_free(wpa_s->vendor_elem[frame_id]);
  4098. wpa_s->vendor_elem[frame_id] = NULL;
  4099. wpas_vendor_elem_update(wpa_s);
  4100. return NULL;
  4101. }
  4102. if (!wpa_s->vendor_elem[frame_id]) {
  4103. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  4104. "ID value does not exist");
  4105. }
  4106. if (ieee802_11_parse_elems(ielems, len, &elems, 0) == ParseFailed) {
  4107. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  4108. "Parse error");
  4109. }
  4110. if (wpas_vendor_elem_remove(wpa_s, frame_id, ielems, len) == 0)
  4111. return NULL;
  4112. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  4113. "Not found");
  4114. }