dbus_new_handlers.c 116 KB

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