dbus_new_handlers.c 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311
  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 = NULL, *net_id = NULL;
  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, 0, &net_id, NULL);
  1433. if (iface == NULL || net_id == NULL ||
  1434. os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
  1435. reply = wpas_dbus_error_invalid_args(message, op);
  1436. goto out;
  1437. }
  1438. errno = 0;
  1439. id = strtoul(net_id, NULL, 10);
  1440. if (errno != 0) {
  1441. reply = wpas_dbus_error_invalid_args(message, op);
  1442. goto out;
  1443. }
  1444. ssid = wpa_config_get_network(wpa_s->conf, id);
  1445. if (ssid == NULL) {
  1446. reply = wpas_dbus_error_network_unknown(message);
  1447. goto out;
  1448. }
  1449. was_disabled = ssid->disabled;
  1450. wpas_notify_network_removed(wpa_s, ssid);
  1451. if (ssid == wpa_s->current_ssid)
  1452. wpa_supplicant_deauthenticate(wpa_s,
  1453. WLAN_REASON_DEAUTH_LEAVING);
  1454. else if (!was_disabled && wpa_s->sched_scanning) {
  1455. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
  1456. "network from filters");
  1457. wpa_supplicant_cancel_sched_scan(wpa_s);
  1458. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1459. }
  1460. if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
  1461. wpa_printf(MSG_ERROR,
  1462. "wpas_dbus_handler_remove_network[dbus]: "
  1463. "error occurred when removing network %d", id);
  1464. reply = wpas_dbus_error_unknown_error(
  1465. message, "error removing the specified network on "
  1466. "this interface.");
  1467. goto out;
  1468. }
  1469. out:
  1470. os_free(iface);
  1471. os_free(net_id);
  1472. return reply;
  1473. }
  1474. static void remove_network(void *arg, struct wpa_ssid *ssid)
  1475. {
  1476. struct wpa_supplicant *wpa_s = arg;
  1477. wpas_notify_network_removed(wpa_s, ssid);
  1478. if (wpa_config_remove_network(wpa_s->conf, ssid->id) < 0) {
  1479. wpa_printf(MSG_ERROR,
  1480. "wpas_dbus_handler_remove_all_networks[dbus]: "
  1481. "error occurred when removing network %d",
  1482. ssid->id);
  1483. return;
  1484. }
  1485. if (ssid == wpa_s->current_ssid)
  1486. wpa_supplicant_deauthenticate(wpa_s,
  1487. WLAN_REASON_DEAUTH_LEAVING);
  1488. }
  1489. /**
  1490. * wpas_dbus_handler_remove_all_networks - Remove all configured networks
  1491. * @message: Pointer to incoming dbus message
  1492. * @wpa_s: wpa_supplicant structure for a network interface
  1493. * Returns: NULL on success or dbus error on failure
  1494. *
  1495. * Handler function for "RemoveAllNetworks" method call of a network interface.
  1496. */
  1497. DBusMessage * wpas_dbus_handler_remove_all_networks(
  1498. DBusMessage *message, struct wpa_supplicant *wpa_s)
  1499. {
  1500. if (wpa_s->sched_scanning)
  1501. wpa_supplicant_cancel_sched_scan(wpa_s);
  1502. /* NB: could check for failure and return an error */
  1503. wpa_config_foreach_network(wpa_s->conf, remove_network, wpa_s);
  1504. return NULL;
  1505. }
  1506. /**
  1507. * wpas_dbus_handler_select_network - Attempt association with a network
  1508. * @message: Pointer to incoming dbus message
  1509. * @wpa_s: wpa_supplicant structure for a network interface
  1510. * Returns: NULL on success or dbus error on failure
  1511. *
  1512. * Handler function for "SelectNetwork" method call of network interface.
  1513. */
  1514. DBusMessage * wpas_dbus_handler_select_network(DBusMessage *message,
  1515. struct wpa_supplicant *wpa_s)
  1516. {
  1517. DBusMessage *reply = NULL;
  1518. const char *op;
  1519. char *iface = NULL, *net_id = NULL;
  1520. int id;
  1521. struct wpa_ssid *ssid;
  1522. dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &op,
  1523. DBUS_TYPE_INVALID);
  1524. /* Extract the network ID and ensure the network */
  1525. /* is actually a child of this interface */
  1526. iface = wpas_dbus_new_decompose_object_path(op, 0, &net_id, NULL);
  1527. if (iface == NULL || net_id == NULL ||
  1528. os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
  1529. reply = wpas_dbus_error_invalid_args(message, op);
  1530. goto out;
  1531. }
  1532. errno = 0;
  1533. id = strtoul(net_id, NULL, 10);
  1534. if (errno != 0) {
  1535. reply = wpas_dbus_error_invalid_args(message, op);
  1536. goto out;
  1537. }
  1538. ssid = wpa_config_get_network(wpa_s->conf, id);
  1539. if (ssid == NULL) {
  1540. reply = wpas_dbus_error_network_unknown(message);
  1541. goto out;
  1542. }
  1543. /* Finally, associate with the network */
  1544. wpa_supplicant_select_network(wpa_s, ssid);
  1545. out:
  1546. os_free(iface);
  1547. os_free(net_id);
  1548. return reply;
  1549. }
  1550. /**
  1551. * wpas_dbus_handler_network_reply - Reply to a NetworkRequest signal
  1552. * @message: Pointer to incoming dbus message
  1553. * @wpa_s: wpa_supplicant structure for a network interface
  1554. * Returns: NULL on success or dbus error on failure
  1555. *
  1556. * Handler function for "NetworkReply" method call of network interface.
  1557. */
  1558. DBusMessage * wpas_dbus_handler_network_reply(DBusMessage *message,
  1559. struct wpa_supplicant *wpa_s)
  1560. {
  1561. #ifdef IEEE8021X_EAPOL
  1562. DBusMessage *reply = NULL;
  1563. const char *op, *field, *value;
  1564. char *iface = NULL, *net_id = NULL;
  1565. int id;
  1566. struct wpa_ssid *ssid;
  1567. if (!dbus_message_get_args(message, NULL,
  1568. DBUS_TYPE_OBJECT_PATH, &op,
  1569. DBUS_TYPE_STRING, &field,
  1570. DBUS_TYPE_STRING, &value,
  1571. DBUS_TYPE_INVALID))
  1572. return wpas_dbus_error_invalid_args(message, NULL);
  1573. /* Extract the network ID and ensure the network */
  1574. /* is actually a child of this interface */
  1575. iface = wpas_dbus_new_decompose_object_path(op, 0, &net_id, NULL);
  1576. if (iface == NULL || net_id == NULL ||
  1577. os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
  1578. reply = wpas_dbus_error_invalid_args(message, op);
  1579. goto out;
  1580. }
  1581. errno = 0;
  1582. id = strtoul(net_id, NULL, 10);
  1583. if (errno != 0) {
  1584. reply = wpas_dbus_error_invalid_args(message, net_id);
  1585. goto out;
  1586. }
  1587. ssid = wpa_config_get_network(wpa_s->conf, id);
  1588. if (ssid == NULL) {
  1589. reply = wpas_dbus_error_network_unknown(message);
  1590. goto out;
  1591. }
  1592. if (wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid,
  1593. field, value) < 0)
  1594. reply = wpas_dbus_error_invalid_args(message, field);
  1595. else {
  1596. /* Tell EAP to retry immediately */
  1597. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  1598. }
  1599. out:
  1600. os_free(iface);
  1601. os_free(net_id);
  1602. return reply;
  1603. #else /* IEEE8021X_EAPOL */
  1604. wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
  1605. return wpas_dbus_error_unknown_error(message, "802.1X not included");
  1606. #endif /* IEEE8021X_EAPOL */
  1607. }
  1608. #ifndef CONFIG_NO_CONFIG_BLOBS
  1609. /**
  1610. * wpas_dbus_handler_add_blob - Store named binary blob (ie, for certificates)
  1611. * @message: Pointer to incoming dbus message
  1612. * @wpa_s: %wpa_supplicant data structure
  1613. * Returns: A dbus message containing an error on failure or NULL on success
  1614. *
  1615. * Asks wpa_supplicant to internally store a binary blobs.
  1616. */
  1617. DBusMessage * wpas_dbus_handler_add_blob(DBusMessage *message,
  1618. struct wpa_supplicant *wpa_s)
  1619. {
  1620. DBusMessage *reply = NULL;
  1621. DBusMessageIter iter, array_iter;
  1622. char *blob_name;
  1623. u8 *blob_data;
  1624. int blob_len;
  1625. struct wpa_config_blob *blob = NULL;
  1626. dbus_message_iter_init(message, &iter);
  1627. dbus_message_iter_get_basic(&iter, &blob_name);
  1628. if (wpa_config_get_blob(wpa_s->conf, blob_name)) {
  1629. return dbus_message_new_error(message,
  1630. WPAS_DBUS_ERROR_BLOB_EXISTS,
  1631. NULL);
  1632. }
  1633. dbus_message_iter_next(&iter);
  1634. dbus_message_iter_recurse(&iter, &array_iter);
  1635. dbus_message_iter_get_fixed_array(&array_iter, &blob_data, &blob_len);
  1636. blob = os_zalloc(sizeof(*blob));
  1637. if (!blob) {
  1638. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1639. NULL);
  1640. goto err;
  1641. }
  1642. blob->data = os_malloc(blob_len);
  1643. if (!blob->data) {
  1644. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1645. NULL);
  1646. goto err;
  1647. }
  1648. os_memcpy(blob->data, blob_data, blob_len);
  1649. blob->len = blob_len;
  1650. blob->name = os_strdup(blob_name);
  1651. if (!blob->name) {
  1652. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1653. NULL);
  1654. goto err;
  1655. }
  1656. wpa_config_set_blob(wpa_s->conf, blob);
  1657. wpas_notify_blob_added(wpa_s, blob->name);
  1658. return reply;
  1659. err:
  1660. if (blob) {
  1661. os_free(blob->name);
  1662. os_free(blob->data);
  1663. os_free(blob);
  1664. }
  1665. return reply;
  1666. }
  1667. /**
  1668. * wpas_dbus_handler_get_blob - Get named binary blob (ie, for certificates)
  1669. * @message: Pointer to incoming dbus message
  1670. * @wpa_s: %wpa_supplicant data structure
  1671. * Returns: A dbus message containing array of bytes (blob)
  1672. *
  1673. * Gets one wpa_supplicant's binary blobs.
  1674. */
  1675. DBusMessage * wpas_dbus_handler_get_blob(DBusMessage *message,
  1676. struct wpa_supplicant *wpa_s)
  1677. {
  1678. DBusMessage *reply = NULL;
  1679. DBusMessageIter iter, array_iter;
  1680. char *blob_name;
  1681. const struct wpa_config_blob *blob;
  1682. dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &blob_name,
  1683. DBUS_TYPE_INVALID);
  1684. blob = wpa_config_get_blob(wpa_s->conf, blob_name);
  1685. if (!blob) {
  1686. return dbus_message_new_error(message,
  1687. WPAS_DBUS_ERROR_BLOB_UNKNOWN,
  1688. "Blob id not set");
  1689. }
  1690. reply = dbus_message_new_method_return(message);
  1691. if (!reply) {
  1692. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1693. NULL);
  1694. goto out;
  1695. }
  1696. dbus_message_iter_init_append(reply, &iter);
  1697. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
  1698. DBUS_TYPE_BYTE_AS_STRING,
  1699. &array_iter)) {
  1700. dbus_message_unref(reply);
  1701. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1702. NULL);
  1703. goto out;
  1704. }
  1705. if (!dbus_message_iter_append_fixed_array(&array_iter, DBUS_TYPE_BYTE,
  1706. &(blob->data), blob->len)) {
  1707. dbus_message_unref(reply);
  1708. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1709. NULL);
  1710. goto out;
  1711. }
  1712. if (!dbus_message_iter_close_container(&iter, &array_iter)) {
  1713. dbus_message_unref(reply);
  1714. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1715. NULL);
  1716. goto out;
  1717. }
  1718. out:
  1719. return reply;
  1720. }
  1721. /**
  1722. * wpas_remove_handler_remove_blob - Remove named binary blob
  1723. * @message: Pointer to incoming dbus message
  1724. * @wpa_s: %wpa_supplicant data structure
  1725. * Returns: NULL on success or dbus error
  1726. *
  1727. * Asks wpa_supplicant to internally remove a binary blobs.
  1728. */
  1729. DBusMessage * wpas_dbus_handler_remove_blob(DBusMessage *message,
  1730. struct wpa_supplicant *wpa_s)
  1731. {
  1732. DBusMessage *reply = NULL;
  1733. char *blob_name;
  1734. dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &blob_name,
  1735. DBUS_TYPE_INVALID);
  1736. if (wpa_config_remove_blob(wpa_s->conf, blob_name)) {
  1737. return dbus_message_new_error(message,
  1738. WPAS_DBUS_ERROR_BLOB_UNKNOWN,
  1739. "Blob id not set");
  1740. }
  1741. wpas_notify_blob_removed(wpa_s, blob_name);
  1742. return reply;
  1743. }
  1744. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1745. /*
  1746. * wpas_dbus_handler_flush_bss - Flush the BSS cache
  1747. * @message: Pointer to incoming dbus message
  1748. * @wpa_s: wpa_supplicant structure for a network interface
  1749. * Returns: NULL
  1750. *
  1751. * Handler function for "FlushBSS" method call of network interface.
  1752. */
  1753. DBusMessage * wpas_dbus_handler_flush_bss(DBusMessage *message,
  1754. struct wpa_supplicant *wpa_s)
  1755. {
  1756. dbus_uint32_t age;
  1757. dbus_message_get_args(message, NULL, DBUS_TYPE_UINT32, &age,
  1758. DBUS_TYPE_INVALID);
  1759. if (age == 0)
  1760. wpa_bss_flush(wpa_s);
  1761. else
  1762. wpa_bss_flush_by_age(wpa_s, age);
  1763. return NULL;
  1764. }
  1765. #ifdef CONFIG_AUTOSCAN
  1766. /**
  1767. * wpas_dbus_handler_autoscan - Set autoscan parameters for the interface
  1768. * @message: Pointer to incoming dbus message
  1769. * @wpa_s: wpa_supplicant structure for a network interface
  1770. * Returns: NULL
  1771. *
  1772. * Handler function for "AutoScan" method call of network interface.
  1773. */
  1774. DBusMessage * wpas_dbus_handler_autoscan(DBusMessage *message,
  1775. struct wpa_supplicant *wpa_s)
  1776. {
  1777. DBusMessage *reply = NULL;
  1778. enum wpa_states state = wpa_s->wpa_state;
  1779. char *arg;
  1780. dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg,
  1781. DBUS_TYPE_INVALID);
  1782. if (arg != NULL && os_strlen(arg) > 0) {
  1783. char *tmp;
  1784. tmp = os_strdup(arg);
  1785. if (tmp == NULL) {
  1786. reply = dbus_message_new_error(message,
  1787. DBUS_ERROR_NO_MEMORY,
  1788. NULL);
  1789. } else {
  1790. os_free(wpa_s->conf->autoscan);
  1791. wpa_s->conf->autoscan = tmp;
  1792. if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  1793. autoscan_init(wpa_s, 1);
  1794. else if (state == WPA_SCANNING)
  1795. wpa_supplicant_reinit_autoscan(wpa_s);
  1796. }
  1797. } else if (arg != NULL && os_strlen(arg) == 0) {
  1798. os_free(wpa_s->conf->autoscan);
  1799. wpa_s->conf->autoscan = NULL;
  1800. autoscan_deinit(wpa_s);
  1801. } else
  1802. reply = dbus_message_new_error(message,
  1803. DBUS_ERROR_INVALID_ARGS,
  1804. NULL);
  1805. return reply;
  1806. }
  1807. #endif /* CONFIG_AUTOSCAN */
  1808. /*
  1809. * wpas_dbus_handler_eap_logoff - IEEE 802.1X EAPOL state machine logoff
  1810. * @message: Pointer to incoming dbus message
  1811. * @wpa_s: wpa_supplicant structure for a network interface
  1812. * Returns: NULL
  1813. *
  1814. * Handler function for "EAPLogoff" method call of network interface.
  1815. */
  1816. DBusMessage * wpas_dbus_handler_eap_logoff(DBusMessage *message,
  1817. struct wpa_supplicant *wpa_s)
  1818. {
  1819. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  1820. return NULL;
  1821. }
  1822. /*
  1823. * wpas_dbus_handler_eap_logon - IEEE 802.1X EAPOL state machine logon
  1824. * @message: Pointer to incoming dbus message
  1825. * @wpa_s: wpa_supplicant structure for a network interface
  1826. * Returns: NULL
  1827. *
  1828. * Handler function for "EAPLogin" method call of network interface.
  1829. */
  1830. DBusMessage * wpas_dbus_handler_eap_logon(DBusMessage *message,
  1831. struct wpa_supplicant *wpa_s)
  1832. {
  1833. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  1834. return NULL;
  1835. }
  1836. #ifdef CONFIG_TDLS
  1837. static int get_peer_hwaddr_helper(DBusMessage *message, const char *func_name,
  1838. u8 *peer_address, DBusMessage **error)
  1839. {
  1840. const char *peer_string;
  1841. *error = NULL;
  1842. if (!dbus_message_get_args(message, NULL,
  1843. DBUS_TYPE_STRING, &peer_string,
  1844. DBUS_TYPE_INVALID)) {
  1845. *error = wpas_dbus_error_invalid_args(message, NULL);
  1846. return -1;
  1847. }
  1848. if (hwaddr_aton(peer_string, peer_address)) {
  1849. wpa_printf(MSG_DEBUG, "%s: invalid address '%s'",
  1850. func_name, peer_string);
  1851. *error = wpas_dbus_error_invalid_args(
  1852. message, "Invalid hardware address format");
  1853. return -1;
  1854. }
  1855. return 0;
  1856. }
  1857. /*
  1858. * wpas_dbus_handler_tdls_discover - Discover TDLS peer
  1859. * @message: Pointer to incoming dbus message
  1860. * @wpa_s: wpa_supplicant structure for a network interface
  1861. * Returns: NULL indicating success or DBus error message on failure
  1862. *
  1863. * Handler function for "TDLSDiscover" method call of network interface.
  1864. */
  1865. DBusMessage * wpas_dbus_handler_tdls_discover(DBusMessage *message,
  1866. struct wpa_supplicant *wpa_s)
  1867. {
  1868. u8 peer[ETH_ALEN];
  1869. DBusMessage *error_reply;
  1870. int ret;
  1871. if (get_peer_hwaddr_helper(message, __func__, peer, &error_reply) < 0)
  1872. return error_reply;
  1873. wpa_printf(MSG_DEBUG, "DBUS TDLS_DISCOVER " MACSTR, MAC2STR(peer));
  1874. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  1875. ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
  1876. else
  1877. ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
  1878. if (ret) {
  1879. return wpas_dbus_error_unknown_error(
  1880. message, "error performing TDLS discovery");
  1881. }
  1882. return NULL;
  1883. }
  1884. /*
  1885. * wpas_dbus_handler_tdls_setup - Setup TDLS session
  1886. * @message: Pointer to incoming dbus message
  1887. * @wpa_s: wpa_supplicant structure for a network interface
  1888. * Returns: NULL indicating success or DBus error message on failure
  1889. *
  1890. * Handler function for "TDLSSetup" method call of network interface.
  1891. */
  1892. DBusMessage * wpas_dbus_handler_tdls_setup(DBusMessage *message,
  1893. struct wpa_supplicant *wpa_s)
  1894. {
  1895. u8 peer[ETH_ALEN];
  1896. DBusMessage *error_reply;
  1897. int ret;
  1898. if (get_peer_hwaddr_helper(message, __func__, peer, &error_reply) < 0)
  1899. return error_reply;
  1900. wpa_printf(MSG_DEBUG, "DBUS TDLS_SETUP " MACSTR, MAC2STR(peer));
  1901. wpa_tdls_remove(wpa_s->wpa, peer);
  1902. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  1903. ret = wpa_tdls_start(wpa_s->wpa, peer);
  1904. else
  1905. ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  1906. if (ret) {
  1907. return wpas_dbus_error_unknown_error(
  1908. message, "error performing TDLS setup");
  1909. }
  1910. return NULL;
  1911. }
  1912. /*
  1913. * wpas_dbus_handler_tdls_status - Return TDLS session status
  1914. * @message: Pointer to incoming dbus message
  1915. * @wpa_s: wpa_supplicant structure for a network interface
  1916. * Returns: A string representing the state of the link to this TDLS peer
  1917. *
  1918. * Handler function for "TDLSStatus" method call of network interface.
  1919. */
  1920. DBusMessage * wpas_dbus_handler_tdls_status(DBusMessage *message,
  1921. struct wpa_supplicant *wpa_s)
  1922. {
  1923. u8 peer[ETH_ALEN];
  1924. DBusMessage *reply;
  1925. const char *tdls_status;
  1926. if (get_peer_hwaddr_helper(message, __func__, peer, &reply) < 0)
  1927. return reply;
  1928. wpa_printf(MSG_DEBUG, "DBUS TDLS_STATUS " MACSTR, MAC2STR(peer));
  1929. tdls_status = wpa_tdls_get_link_status(wpa_s->wpa, peer);
  1930. reply = dbus_message_new_method_return(message);
  1931. dbus_message_append_args(reply, DBUS_TYPE_STRING,
  1932. &tdls_status, DBUS_TYPE_INVALID);
  1933. return reply;
  1934. }
  1935. /*
  1936. * wpas_dbus_handler_tdls_teardown - Teardown TDLS session
  1937. * @message: Pointer to incoming dbus message
  1938. * @wpa_s: wpa_supplicant structure for a network interface
  1939. * Returns: NULL indicating success or DBus error message on failure
  1940. *
  1941. * Handler function for "TDLSTeardown" method call of network interface.
  1942. */
  1943. DBusMessage * wpas_dbus_handler_tdls_teardown(DBusMessage *message,
  1944. struct wpa_supplicant *wpa_s)
  1945. {
  1946. u8 peer[ETH_ALEN];
  1947. DBusMessage *error_reply;
  1948. int ret;
  1949. if (get_peer_hwaddr_helper(message, __func__, peer, &error_reply) < 0)
  1950. return error_reply;
  1951. wpa_printf(MSG_DEBUG, "DBUS TDLS_TEARDOWN " MACSTR, MAC2STR(peer));
  1952. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  1953. ret = wpa_tdls_teardown_link(
  1954. wpa_s->wpa, peer,
  1955. WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
  1956. else
  1957. ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  1958. if (ret) {
  1959. return wpas_dbus_error_unknown_error(
  1960. message, "error performing TDLS teardown");
  1961. }
  1962. return NULL;
  1963. }
  1964. #endif /* CONFIG_TDLS */
  1965. /**
  1966. * wpas_dbus_handler_set_pkcs11_engine_and_module_path - Set PKCS #11 engine and module path
  1967. * @message: Pointer to incoming dbus message
  1968. * @wpa_s: %wpa_supplicant data structure
  1969. * Returns: A dbus message containing an error on failure or NULL on success
  1970. *
  1971. * Sets the PKCS #11 engine and module path.
  1972. */
  1973. DBusMessage * wpas_dbus_handler_set_pkcs11_engine_and_module_path(
  1974. DBusMessage *message, struct wpa_supplicant *wpa_s)
  1975. {
  1976. DBusMessageIter iter;
  1977. char *value = NULL;
  1978. char *pkcs11_engine_path = NULL;
  1979. char *pkcs11_module_path = NULL;
  1980. dbus_message_iter_init(message, &iter);
  1981. dbus_message_iter_get_basic(&iter, &value);
  1982. if (value == NULL) {
  1983. return dbus_message_new_error(
  1984. message, DBUS_ERROR_INVALID_ARGS,
  1985. "Invalid pkcs11_engine_path argument");
  1986. }
  1987. /* Empty path defaults to NULL */
  1988. if (os_strlen(value))
  1989. pkcs11_engine_path = value;
  1990. dbus_message_iter_next(&iter);
  1991. dbus_message_iter_get_basic(&iter, &value);
  1992. if (value == NULL) {
  1993. os_free(pkcs11_engine_path);
  1994. return dbus_message_new_error(
  1995. message, DBUS_ERROR_INVALID_ARGS,
  1996. "Invalid pkcs11_module_path argument");
  1997. }
  1998. /* Empty path defaults to NULL */
  1999. if (os_strlen(value))
  2000. pkcs11_module_path = value;
  2001. if (wpas_set_pkcs11_engine_and_module_path(wpa_s, pkcs11_engine_path,
  2002. pkcs11_module_path))
  2003. return dbus_message_new_error(
  2004. message, DBUS_ERROR_FAILED,
  2005. "Reinit of the EAPOL state machine with the new PKCS "
  2006. "#11 engine and module path failed.");
  2007. wpa_dbus_mark_property_changed(
  2008. wpa_s->global->dbus, wpa_s->dbus_new_path,
  2009. WPAS_DBUS_NEW_IFACE_INTERFACE, "PKCS11EnginePath");
  2010. wpa_dbus_mark_property_changed(
  2011. wpa_s->global->dbus, wpa_s->dbus_new_path,
  2012. WPAS_DBUS_NEW_IFACE_INTERFACE, "PKCS11ModulePath");
  2013. return NULL;
  2014. }
  2015. /**
  2016. * wpas_dbus_getter_capabilities - Return interface capabilities
  2017. * @iter: Pointer to incoming dbus message iter
  2018. * @error: Location to store error on failure
  2019. * @user_data: Function specific data
  2020. * Returns: TRUE on success, FALSE on failure
  2021. *
  2022. * Getter for "Capabilities" property of an interface.
  2023. */
  2024. dbus_bool_t wpas_dbus_getter_capabilities(DBusMessageIter *iter,
  2025. DBusError *error, void *user_data)
  2026. {
  2027. struct wpa_supplicant *wpa_s = user_data;
  2028. struct wpa_driver_capa capa;
  2029. int res;
  2030. DBusMessageIter iter_dict, iter_dict_entry, iter_dict_val, iter_array,
  2031. variant_iter;
  2032. const char *scans[] = { "active", "passive", "ssid" };
  2033. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  2034. "a{sv}", &variant_iter))
  2035. goto nomem;
  2036. if (!wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
  2037. goto nomem;
  2038. res = wpa_drv_get_capa(wpa_s, &capa);
  2039. /***** pairwise cipher */
  2040. if (res < 0) {
  2041. const char *args[] = {"ccmp", "tkip", "none"};
  2042. if (!wpa_dbus_dict_append_string_array(
  2043. &iter_dict, "Pairwise", args,
  2044. ARRAY_SIZE(args)))
  2045. goto nomem;
  2046. } else {
  2047. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Pairwise",
  2048. &iter_dict_entry,
  2049. &iter_dict_val,
  2050. &iter_array))
  2051. goto nomem;
  2052. if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP_256) {
  2053. if (!wpa_dbus_dict_string_array_add_element(
  2054. &iter_array, "ccmp-256"))
  2055. goto nomem;
  2056. }
  2057. if (capa.enc & WPA_DRIVER_CAPA_ENC_GCMP_256) {
  2058. if (!wpa_dbus_dict_string_array_add_element(
  2059. &iter_array, "gcmp-256"))
  2060. goto nomem;
  2061. }
  2062. if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  2063. if (!wpa_dbus_dict_string_array_add_element(
  2064. &iter_array, "ccmp"))
  2065. goto nomem;
  2066. }
  2067. if (capa.enc & WPA_DRIVER_CAPA_ENC_GCMP) {
  2068. if (!wpa_dbus_dict_string_array_add_element(
  2069. &iter_array, "gcmp"))
  2070. goto nomem;
  2071. }
  2072. if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  2073. if (!wpa_dbus_dict_string_array_add_element(
  2074. &iter_array, "tkip"))
  2075. goto nomem;
  2076. }
  2077. if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  2078. if (!wpa_dbus_dict_string_array_add_element(
  2079. &iter_array, "none"))
  2080. goto nomem;
  2081. }
  2082. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  2083. &iter_dict_entry,
  2084. &iter_dict_val,
  2085. &iter_array))
  2086. goto nomem;
  2087. }
  2088. /***** group cipher */
  2089. if (res < 0) {
  2090. const char *args[] = {
  2091. "ccmp", "tkip", "wep104", "wep40"
  2092. };
  2093. if (!wpa_dbus_dict_append_string_array(
  2094. &iter_dict, "Group", args,
  2095. ARRAY_SIZE(args)))
  2096. goto nomem;
  2097. } else {
  2098. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Group",
  2099. &iter_dict_entry,
  2100. &iter_dict_val,
  2101. &iter_array))
  2102. goto nomem;
  2103. if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP_256) {
  2104. if (!wpa_dbus_dict_string_array_add_element(
  2105. &iter_array, "ccmp-256"))
  2106. goto nomem;
  2107. }
  2108. if (capa.enc & WPA_DRIVER_CAPA_ENC_GCMP_256) {
  2109. if (!wpa_dbus_dict_string_array_add_element(
  2110. &iter_array, "gcmp-256"))
  2111. goto nomem;
  2112. }
  2113. if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  2114. if (!wpa_dbus_dict_string_array_add_element(
  2115. &iter_array, "ccmp"))
  2116. goto nomem;
  2117. }
  2118. if (capa.enc & WPA_DRIVER_CAPA_ENC_GCMP) {
  2119. if (!wpa_dbus_dict_string_array_add_element(
  2120. &iter_array, "gcmp"))
  2121. goto nomem;
  2122. }
  2123. if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  2124. if (!wpa_dbus_dict_string_array_add_element(
  2125. &iter_array, "tkip"))
  2126. goto nomem;
  2127. }
  2128. if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) {
  2129. if (!wpa_dbus_dict_string_array_add_element(
  2130. &iter_array, "wep104"))
  2131. goto nomem;
  2132. }
  2133. if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) {
  2134. if (!wpa_dbus_dict_string_array_add_element(
  2135. &iter_array, "wep40"))
  2136. goto nomem;
  2137. }
  2138. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  2139. &iter_dict_entry,
  2140. &iter_dict_val,
  2141. &iter_array))
  2142. goto nomem;
  2143. }
  2144. /***** key management */
  2145. if (res < 0) {
  2146. const char *args[] = {
  2147. "wpa-psk", "wpa-eap", "ieee8021x", "wpa-none",
  2148. #ifdef CONFIG_WPS
  2149. "wps",
  2150. #endif /* CONFIG_WPS */
  2151. "none"
  2152. };
  2153. if (!wpa_dbus_dict_append_string_array(
  2154. &iter_dict, "KeyMgmt", args,
  2155. ARRAY_SIZE(args)))
  2156. goto nomem;
  2157. } else {
  2158. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "KeyMgmt",
  2159. &iter_dict_entry,
  2160. &iter_dict_val,
  2161. &iter_array))
  2162. goto nomem;
  2163. if (!wpa_dbus_dict_string_array_add_element(&iter_array,
  2164. "none"))
  2165. goto nomem;
  2166. if (!wpa_dbus_dict_string_array_add_element(&iter_array,
  2167. "ieee8021x"))
  2168. goto nomem;
  2169. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2170. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  2171. if (!wpa_dbus_dict_string_array_add_element(
  2172. &iter_array, "wpa-eap"))
  2173. goto nomem;
  2174. if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT)
  2175. if (!wpa_dbus_dict_string_array_add_element(
  2176. &iter_array, "wpa-ft-eap"))
  2177. goto nomem;
  2178. /* TODO: Ensure that driver actually supports sha256 encryption. */
  2179. #ifdef CONFIG_IEEE80211W
  2180. if (!wpa_dbus_dict_string_array_add_element(
  2181. &iter_array, "wpa-eap-sha256"))
  2182. goto nomem;
  2183. #endif /* CONFIG_IEEE80211W */
  2184. }
  2185. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  2186. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2187. if (!wpa_dbus_dict_string_array_add_element(
  2188. &iter_array, "wpa-psk"))
  2189. goto nomem;
  2190. if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK)
  2191. if (!wpa_dbus_dict_string_array_add_element(
  2192. &iter_array, "wpa-ft-psk"))
  2193. goto nomem;
  2194. /* TODO: Ensure that driver actually supports sha256 encryption. */
  2195. #ifdef CONFIG_IEEE80211W
  2196. if (!wpa_dbus_dict_string_array_add_element(
  2197. &iter_array, "wpa-psk-sha256"))
  2198. goto nomem;
  2199. #endif /* CONFIG_IEEE80211W */
  2200. }
  2201. if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  2202. if (!wpa_dbus_dict_string_array_add_element(
  2203. &iter_array, "wpa-none"))
  2204. goto nomem;
  2205. }
  2206. #ifdef CONFIG_WPS
  2207. if (!wpa_dbus_dict_string_array_add_element(&iter_array,
  2208. "wps"))
  2209. goto nomem;
  2210. #endif /* CONFIG_WPS */
  2211. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  2212. &iter_dict_entry,
  2213. &iter_dict_val,
  2214. &iter_array))
  2215. goto nomem;
  2216. }
  2217. /***** WPA protocol */
  2218. if (res < 0) {
  2219. const char *args[] = { "rsn", "wpa" };
  2220. if (!wpa_dbus_dict_append_string_array(
  2221. &iter_dict, "Protocol", args,
  2222. ARRAY_SIZE(args)))
  2223. goto nomem;
  2224. } else {
  2225. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Protocol",
  2226. &iter_dict_entry,
  2227. &iter_dict_val,
  2228. &iter_array))
  2229. goto nomem;
  2230. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  2231. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2232. if (!wpa_dbus_dict_string_array_add_element(
  2233. &iter_array, "rsn"))
  2234. goto nomem;
  2235. }
  2236. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2237. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  2238. if (!wpa_dbus_dict_string_array_add_element(
  2239. &iter_array, "wpa"))
  2240. goto nomem;
  2241. }
  2242. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  2243. &iter_dict_entry,
  2244. &iter_dict_val,
  2245. &iter_array))
  2246. goto nomem;
  2247. }
  2248. /***** auth alg */
  2249. if (res < 0) {
  2250. const char *args[] = { "open", "shared", "leap" };
  2251. if (!wpa_dbus_dict_append_string_array(
  2252. &iter_dict, "AuthAlg", args,
  2253. ARRAY_SIZE(args)))
  2254. goto nomem;
  2255. } else {
  2256. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "AuthAlg",
  2257. &iter_dict_entry,
  2258. &iter_dict_val,
  2259. &iter_array))
  2260. goto nomem;
  2261. if (capa.auth & (WPA_DRIVER_AUTH_OPEN)) {
  2262. if (!wpa_dbus_dict_string_array_add_element(
  2263. &iter_array, "open"))
  2264. goto nomem;
  2265. }
  2266. if (capa.auth & (WPA_DRIVER_AUTH_SHARED)) {
  2267. if (!wpa_dbus_dict_string_array_add_element(
  2268. &iter_array, "shared"))
  2269. goto nomem;
  2270. }
  2271. if (capa.auth & (WPA_DRIVER_AUTH_LEAP)) {
  2272. if (!wpa_dbus_dict_string_array_add_element(
  2273. &iter_array, "leap"))
  2274. goto nomem;
  2275. }
  2276. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  2277. &iter_dict_entry,
  2278. &iter_dict_val,
  2279. &iter_array))
  2280. goto nomem;
  2281. }
  2282. /***** Scan */
  2283. if (!wpa_dbus_dict_append_string_array(&iter_dict, "Scan", scans,
  2284. ARRAY_SIZE(scans)))
  2285. goto nomem;
  2286. /***** Modes */
  2287. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Modes",
  2288. &iter_dict_entry,
  2289. &iter_dict_val,
  2290. &iter_array))
  2291. goto nomem;
  2292. if (!wpa_dbus_dict_string_array_add_element(
  2293. &iter_array, "infrastructure"))
  2294. goto nomem;
  2295. if (!wpa_dbus_dict_string_array_add_element(
  2296. &iter_array, "ad-hoc"))
  2297. goto nomem;
  2298. if (res >= 0) {
  2299. if (capa.flags & (WPA_DRIVER_FLAGS_AP)) {
  2300. if (!wpa_dbus_dict_string_array_add_element(
  2301. &iter_array, "ap"))
  2302. goto nomem;
  2303. }
  2304. if (capa.flags & (WPA_DRIVER_FLAGS_P2P_CAPABLE)) {
  2305. if (!wpa_dbus_dict_string_array_add_element(
  2306. &iter_array, "p2p"))
  2307. goto nomem;
  2308. }
  2309. }
  2310. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  2311. &iter_dict_entry,
  2312. &iter_dict_val,
  2313. &iter_array))
  2314. goto nomem;
  2315. /***** Modes end */
  2316. if (res >= 0) {
  2317. dbus_int32_t max_scan_ssid = capa.max_scan_ssids;
  2318. if (!wpa_dbus_dict_append_int32(&iter_dict, "MaxScanSSID",
  2319. max_scan_ssid))
  2320. goto nomem;
  2321. }
  2322. if (!wpa_dbus_dict_close_write(&variant_iter, &iter_dict))
  2323. goto nomem;
  2324. if (!dbus_message_iter_close_container(iter, &variant_iter))
  2325. goto nomem;
  2326. return TRUE;
  2327. nomem:
  2328. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2329. return FALSE;
  2330. }
  2331. /**
  2332. * wpas_dbus_getter_state - Get interface state
  2333. * @iter: Pointer to incoming dbus message iter
  2334. * @error: Location to store error on failure
  2335. * @user_data: Function specific data
  2336. * Returns: TRUE on success, FALSE on failure
  2337. *
  2338. * Getter for "State" property.
  2339. */
  2340. dbus_bool_t wpas_dbus_getter_state(DBusMessageIter *iter, DBusError *error,
  2341. void *user_data)
  2342. {
  2343. struct wpa_supplicant *wpa_s = user_data;
  2344. const char *str_state;
  2345. char *state_ls, *tmp;
  2346. dbus_bool_t success = FALSE;
  2347. str_state = wpa_supplicant_state_txt(wpa_s->wpa_state);
  2348. /* make state string lowercase to fit new DBus API convention
  2349. */
  2350. state_ls = tmp = os_strdup(str_state);
  2351. if (!tmp) {
  2352. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2353. return FALSE;
  2354. }
  2355. while (*tmp) {
  2356. *tmp = tolower(*tmp);
  2357. tmp++;
  2358. }
  2359. success = wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2360. &state_ls, error);
  2361. os_free(state_ls);
  2362. return success;
  2363. }
  2364. /**
  2365. * wpas_dbus_new_iface_get_scanning - Get interface scanning state
  2366. * @iter: Pointer to incoming dbus message iter
  2367. * @error: Location to store error on failure
  2368. * @user_data: Function specific data
  2369. * Returns: TRUE on success, FALSE on failure
  2370. *
  2371. * Getter for "scanning" property.
  2372. */
  2373. dbus_bool_t wpas_dbus_getter_scanning(DBusMessageIter *iter, DBusError *error,
  2374. void *user_data)
  2375. {
  2376. struct wpa_supplicant *wpa_s = user_data;
  2377. dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
  2378. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  2379. &scanning, error);
  2380. }
  2381. /**
  2382. * wpas_dbus_getter_ap_scan - Control roaming mode
  2383. * @iter: Pointer to incoming dbus message iter
  2384. * @error: Location to store error on failure
  2385. * @user_data: Function specific data
  2386. * Returns: TRUE on success, FALSE on failure
  2387. *
  2388. * Getter function for "ApScan" property.
  2389. */
  2390. dbus_bool_t wpas_dbus_getter_ap_scan(DBusMessageIter *iter, DBusError *error,
  2391. void *user_data)
  2392. {
  2393. struct wpa_supplicant *wpa_s = user_data;
  2394. dbus_uint32_t ap_scan = wpa_s->conf->ap_scan;
  2395. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT32,
  2396. &ap_scan, error);
  2397. }
  2398. /**
  2399. * wpas_dbus_setter_ap_scan - Control roaming mode
  2400. * @iter: Pointer to incoming dbus message iter
  2401. * @error: Location to store error on failure
  2402. * @user_data: Function specific data
  2403. * Returns: TRUE on success, FALSE on failure
  2404. *
  2405. * Setter function for "ApScan" property.
  2406. */
  2407. dbus_bool_t wpas_dbus_setter_ap_scan(DBusMessageIter *iter, DBusError *error,
  2408. void *user_data)
  2409. {
  2410. struct wpa_supplicant *wpa_s = user_data;
  2411. dbus_uint32_t ap_scan;
  2412. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_UINT32,
  2413. &ap_scan))
  2414. return FALSE;
  2415. if (wpa_supplicant_set_ap_scan(wpa_s, ap_scan)) {
  2416. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2417. "ap_scan must be 0, 1, or 2");
  2418. return FALSE;
  2419. }
  2420. return TRUE;
  2421. }
  2422. /**
  2423. * wpas_dbus_getter_fast_reauth - Control fast
  2424. * reauthentication (TLS session resumption)
  2425. * @iter: Pointer to incoming dbus message iter
  2426. * @error: Location to store error on failure
  2427. * @user_data: Function specific data
  2428. * Returns: TRUE on success, FALSE on failure
  2429. *
  2430. * Getter function for "FastReauth" property.
  2431. */
  2432. dbus_bool_t wpas_dbus_getter_fast_reauth(DBusMessageIter *iter,
  2433. DBusError *error,
  2434. void *user_data)
  2435. {
  2436. struct wpa_supplicant *wpa_s = user_data;
  2437. dbus_bool_t fast_reauth = wpa_s->conf->fast_reauth ? TRUE : FALSE;
  2438. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  2439. &fast_reauth, error);
  2440. }
  2441. /**
  2442. * wpas_dbus_setter_fast_reauth - Control fast
  2443. * reauthentication (TLS session resumption)
  2444. * @iter: Pointer to incoming dbus message iter
  2445. * @error: Location to store error on failure
  2446. * @user_data: Function specific data
  2447. * Returns: TRUE on success, FALSE on failure
  2448. *
  2449. * Setter function for "FastReauth" property.
  2450. */
  2451. dbus_bool_t wpas_dbus_setter_fast_reauth(DBusMessageIter *iter,
  2452. DBusError *error,
  2453. void *user_data)
  2454. {
  2455. struct wpa_supplicant *wpa_s = user_data;
  2456. dbus_bool_t fast_reauth;
  2457. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_BOOLEAN,
  2458. &fast_reauth))
  2459. return FALSE;
  2460. wpa_s->conf->fast_reauth = fast_reauth;
  2461. return TRUE;
  2462. }
  2463. /**
  2464. * wpas_dbus_getter_disconnect_reason - Get most recent reason for disconnect
  2465. * @iter: Pointer to incoming dbus message iter
  2466. * @error: Location to store error on failure
  2467. * @user_data: Function specific data
  2468. * Returns: TRUE on success, FALSE on failure
  2469. *
  2470. * Getter for "DisconnectReason" property. The reason is negative if it is
  2471. * locally generated.
  2472. */
  2473. dbus_bool_t wpas_dbus_getter_disconnect_reason(DBusMessageIter *iter,
  2474. DBusError *error,
  2475. void *user_data)
  2476. {
  2477. struct wpa_supplicant *wpa_s = user_data;
  2478. dbus_int32_t reason = wpa_s->disconnect_reason;
  2479. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_INT32,
  2480. &reason, error);
  2481. }
  2482. /**
  2483. * wpas_dbus_getter_bss_expire_age - Get BSS entry expiration age
  2484. * @iter: Pointer to incoming dbus message iter
  2485. * @error: Location to store error on failure
  2486. * @user_data: Function specific data
  2487. * Returns: TRUE on success, FALSE on failure
  2488. *
  2489. * Getter function for "BSSExpireAge" property.
  2490. */
  2491. dbus_bool_t wpas_dbus_getter_bss_expire_age(DBusMessageIter *iter,
  2492. DBusError *error,
  2493. void *user_data)
  2494. {
  2495. struct wpa_supplicant *wpa_s = user_data;
  2496. dbus_uint32_t expire_age = wpa_s->conf->bss_expiration_age;
  2497. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT32,
  2498. &expire_age, error);
  2499. }
  2500. /**
  2501. * wpas_dbus_setter_bss_expire_age - Control BSS entry expiration age
  2502. * @iter: Pointer to incoming dbus message iter
  2503. * @error: Location to store error on failure
  2504. * @user_data: Function specific data
  2505. * Returns: TRUE on success, FALSE on failure
  2506. *
  2507. * Setter function for "BSSExpireAge" property.
  2508. */
  2509. dbus_bool_t wpas_dbus_setter_bss_expire_age(DBusMessageIter *iter,
  2510. DBusError *error,
  2511. void *user_data)
  2512. {
  2513. struct wpa_supplicant *wpa_s = user_data;
  2514. dbus_uint32_t expire_age;
  2515. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_UINT32,
  2516. &expire_age))
  2517. return FALSE;
  2518. if (wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age)) {
  2519. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2520. "BSSExpireAge must be >= 10");
  2521. return FALSE;
  2522. }
  2523. return TRUE;
  2524. }
  2525. /**
  2526. * wpas_dbus_getter_bss_expire_count - Get BSS entry expiration scan count
  2527. * @iter: Pointer to incoming dbus message iter
  2528. * @error: Location to store error on failure
  2529. * @user_data: Function specific data
  2530. * Returns: TRUE on success, FALSE on failure
  2531. *
  2532. * Getter function for "BSSExpireCount" property.
  2533. */
  2534. dbus_bool_t wpas_dbus_getter_bss_expire_count(DBusMessageIter *iter,
  2535. DBusError *error,
  2536. void *user_data)
  2537. {
  2538. struct wpa_supplicant *wpa_s = user_data;
  2539. dbus_uint32_t expire_count = wpa_s->conf->bss_expiration_scan_count;
  2540. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT32,
  2541. &expire_count, error);
  2542. }
  2543. /**
  2544. * wpas_dbus_setter_bss_expire_count - Control BSS entry expiration scan count
  2545. * @iter: Pointer to incoming dbus message iter
  2546. * @error: Location to store error on failure
  2547. * @user_data: Function specific data
  2548. * Returns: TRUE on success, FALSE on failure
  2549. *
  2550. * Setter function for "BSSExpireCount" property.
  2551. */
  2552. dbus_bool_t wpas_dbus_setter_bss_expire_count(DBusMessageIter *iter,
  2553. DBusError *error,
  2554. void *user_data)
  2555. {
  2556. struct wpa_supplicant *wpa_s = user_data;
  2557. dbus_uint32_t expire_count;
  2558. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_UINT32,
  2559. &expire_count))
  2560. return FALSE;
  2561. if (wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count)) {
  2562. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2563. "BSSExpireCount must be > 0");
  2564. return FALSE;
  2565. }
  2566. return TRUE;
  2567. }
  2568. /**
  2569. * wpas_dbus_getter_country - Control country code
  2570. * @iter: Pointer to incoming dbus message iter
  2571. * @error: Location to store error on failure
  2572. * @user_data: Function specific data
  2573. * Returns: TRUE on success, FALSE on failure
  2574. *
  2575. * Getter function for "Country" property.
  2576. */
  2577. dbus_bool_t wpas_dbus_getter_country(DBusMessageIter *iter, DBusError *error,
  2578. void *user_data)
  2579. {
  2580. struct wpa_supplicant *wpa_s = user_data;
  2581. char country[3];
  2582. char *str = country;
  2583. country[0] = wpa_s->conf->country[0];
  2584. country[1] = wpa_s->conf->country[1];
  2585. country[2] = '\0';
  2586. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2587. &str, error);
  2588. }
  2589. /**
  2590. * wpas_dbus_setter_country - Control country code
  2591. * @iter: Pointer to incoming dbus message iter
  2592. * @error: Location to store error on failure
  2593. * @user_data: Function specific data
  2594. * Returns: TRUE on success, FALSE on failure
  2595. *
  2596. * Setter function for "Country" property.
  2597. */
  2598. dbus_bool_t wpas_dbus_setter_country(DBusMessageIter *iter, DBusError *error,
  2599. void *user_data)
  2600. {
  2601. struct wpa_supplicant *wpa_s = user_data;
  2602. const char *country;
  2603. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_STRING,
  2604. &country))
  2605. return FALSE;
  2606. if (!country[0] || !country[1]) {
  2607. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2608. "invalid country code");
  2609. return FALSE;
  2610. }
  2611. if (wpa_s->drv_priv != NULL && wpa_drv_set_country(wpa_s, country)) {
  2612. wpa_printf(MSG_DEBUG, "Failed to set country");
  2613. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2614. "failed to set country code");
  2615. return FALSE;
  2616. }
  2617. wpa_s->conf->country[0] = country[0];
  2618. wpa_s->conf->country[1] = country[1];
  2619. return TRUE;
  2620. }
  2621. /**
  2622. * wpas_dbus_getter_scan_interval - Get scan interval
  2623. * @iter: Pointer to incoming dbus message iter
  2624. * @error: Location to store error on failure
  2625. * @user_data: Function specific data
  2626. * Returns: TRUE on success, FALSE on failure
  2627. *
  2628. * Getter function for "ScanInterval" property.
  2629. */
  2630. dbus_bool_t wpas_dbus_getter_scan_interval(DBusMessageIter *iter,
  2631. DBusError *error,
  2632. void *user_data)
  2633. {
  2634. struct wpa_supplicant *wpa_s = user_data;
  2635. dbus_int32_t scan_interval = wpa_s->scan_interval;
  2636. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_INT32,
  2637. &scan_interval, error);
  2638. }
  2639. /**
  2640. * wpas_dbus_setter_scan_interval - Control scan interval
  2641. * @iter: Pointer to incoming dbus message iter
  2642. * @error: Location to store error on failure
  2643. * @user_data: Function specific data
  2644. * Returns: TRUE on success, FALSE on failure
  2645. *
  2646. * Setter function for "ScanInterval" property.
  2647. */
  2648. dbus_bool_t wpas_dbus_setter_scan_interval(DBusMessageIter *iter,
  2649. DBusError *error,
  2650. void *user_data)
  2651. {
  2652. struct wpa_supplicant *wpa_s = user_data;
  2653. dbus_int32_t scan_interval;
  2654. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_INT32,
  2655. &scan_interval))
  2656. return FALSE;
  2657. if (wpa_supplicant_set_scan_interval(wpa_s, scan_interval)) {
  2658. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2659. "scan_interval must be >= 0");
  2660. return FALSE;
  2661. }
  2662. return TRUE;
  2663. }
  2664. /**
  2665. * wpas_dbus_getter_ifname - Get interface name
  2666. * @iter: Pointer to incoming dbus message iter
  2667. * @error: Location to store error on failure
  2668. * @user_data: Function specific data
  2669. * Returns: TRUE on success, FALSE on failure
  2670. *
  2671. * Getter for "Ifname" property.
  2672. */
  2673. dbus_bool_t wpas_dbus_getter_ifname(DBusMessageIter *iter, DBusError *error,
  2674. void *user_data)
  2675. {
  2676. struct wpa_supplicant *wpa_s = user_data;
  2677. const char *ifname = wpa_s->ifname;
  2678. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2679. &ifname, error);
  2680. }
  2681. /**
  2682. * wpas_dbus_getter_driver - Get interface name
  2683. * @iter: Pointer to incoming dbus message iter
  2684. * @error: Location to store error on failure
  2685. * @user_data: Function specific data
  2686. * Returns: TRUE on success, FALSE on failure
  2687. *
  2688. * Getter for "Driver" property.
  2689. */
  2690. dbus_bool_t wpas_dbus_getter_driver(DBusMessageIter *iter, DBusError *error,
  2691. void *user_data)
  2692. {
  2693. struct wpa_supplicant *wpa_s = user_data;
  2694. const char *driver;
  2695. if (wpa_s->driver == NULL || wpa_s->driver->name == NULL) {
  2696. wpa_printf(MSG_DEBUG, "wpas_dbus_getter_driver[dbus]: "
  2697. "wpa_s has no driver set");
  2698. dbus_set_error(error, DBUS_ERROR_FAILED, "%s: no driver set",
  2699. __func__);
  2700. return FALSE;
  2701. }
  2702. driver = wpa_s->driver->name;
  2703. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2704. &driver, error);
  2705. }
  2706. /**
  2707. * wpas_dbus_getter_current_bss - Get current bss object path
  2708. * @iter: Pointer to incoming dbus message iter
  2709. * @error: Location to store error on failure
  2710. * @user_data: Function specific data
  2711. * Returns: TRUE on success, FALSE on failure
  2712. *
  2713. * Getter for "CurrentBSS" property.
  2714. */
  2715. dbus_bool_t wpas_dbus_getter_current_bss(DBusMessageIter *iter,
  2716. DBusError *error,
  2717. void *user_data)
  2718. {
  2719. struct wpa_supplicant *wpa_s = user_data;
  2720. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *bss_obj_path = path_buf;
  2721. if (wpa_s->current_bss)
  2722. os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  2723. "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
  2724. wpa_s->dbus_new_path, wpa_s->current_bss->id);
  2725. else
  2726. os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX, "/");
  2727. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_OBJECT_PATH,
  2728. &bss_obj_path, error);
  2729. }
  2730. /**
  2731. * wpas_dbus_getter_current_network - Get current network object path
  2732. * @iter: Pointer to incoming dbus message iter
  2733. * @error: Location to store error on failure
  2734. * @user_data: Function specific data
  2735. * Returns: TRUE on success, FALSE on failure
  2736. *
  2737. * Getter for "CurrentNetwork" property.
  2738. */
  2739. dbus_bool_t wpas_dbus_getter_current_network(DBusMessageIter *iter,
  2740. DBusError *error,
  2741. void *user_data)
  2742. {
  2743. struct wpa_supplicant *wpa_s = user_data;
  2744. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *net_obj_path = path_buf;
  2745. if (wpa_s->current_ssid)
  2746. os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  2747. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%u",
  2748. wpa_s->dbus_new_path, wpa_s->current_ssid->id);
  2749. else
  2750. os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX, "/");
  2751. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_OBJECT_PATH,
  2752. &net_obj_path, error);
  2753. }
  2754. /**
  2755. * wpas_dbus_getter_current_auth_mode - Get current authentication type
  2756. * @iter: Pointer to incoming dbus message iter
  2757. * @error: Location to store error on failure
  2758. * @user_data: Function specific data
  2759. * Returns: TRUE on success, FALSE on failure
  2760. *
  2761. * Getter for "CurrentAuthMode" property.
  2762. */
  2763. dbus_bool_t wpas_dbus_getter_current_auth_mode(DBusMessageIter *iter,
  2764. DBusError *error,
  2765. void *user_data)
  2766. {
  2767. struct wpa_supplicant *wpa_s = user_data;
  2768. const char *eap_mode;
  2769. const char *auth_mode;
  2770. char eap_mode_buf[WPAS_DBUS_AUTH_MODE_MAX];
  2771. if (wpa_s->wpa_state != WPA_COMPLETED) {
  2772. auth_mode = "INACTIVE";
  2773. } else if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X ||
  2774. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  2775. eap_mode = wpa_supplicant_get_eap_mode(wpa_s);
  2776. os_snprintf(eap_mode_buf, WPAS_DBUS_AUTH_MODE_MAX,
  2777. "EAP-%s", eap_mode);
  2778. auth_mode = eap_mode_buf;
  2779. } else {
  2780. auth_mode = wpa_key_mgmt_txt(wpa_s->key_mgmt,
  2781. wpa_s->current_ssid->proto);
  2782. }
  2783. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2784. &auth_mode, error);
  2785. }
  2786. /**
  2787. * wpas_dbus_getter_bridge_ifname - Get interface name
  2788. * @iter: Pointer to incoming dbus message iter
  2789. * @error: Location to store error on failure
  2790. * @user_data: Function specific data
  2791. * Returns: TRUE on success, FALSE on failure
  2792. *
  2793. * Getter for "BridgeIfname" property.
  2794. */
  2795. dbus_bool_t wpas_dbus_getter_bridge_ifname(DBusMessageIter *iter,
  2796. DBusError *error,
  2797. void *user_data)
  2798. {
  2799. struct wpa_supplicant *wpa_s = user_data;
  2800. const char *bridge_ifname = wpa_s->bridge_ifname;
  2801. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2802. &bridge_ifname, error);
  2803. }
  2804. /**
  2805. * wpas_dbus_getter_bsss - Get array of BSSs objects
  2806. * @iter: Pointer to incoming dbus message iter
  2807. * @error: Location to store error on failure
  2808. * @user_data: Function specific data
  2809. * Returns: TRUE on success, FALSE on failure
  2810. *
  2811. * Getter for "BSSs" property.
  2812. */
  2813. dbus_bool_t wpas_dbus_getter_bsss(DBusMessageIter *iter, DBusError *error,
  2814. void *user_data)
  2815. {
  2816. struct wpa_supplicant *wpa_s = user_data;
  2817. struct wpa_bss *bss;
  2818. char **paths;
  2819. unsigned int i = 0;
  2820. dbus_bool_t success = FALSE;
  2821. paths = os_calloc(wpa_s->num_bss, sizeof(char *));
  2822. if (!paths) {
  2823. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2824. return FALSE;
  2825. }
  2826. /* Loop through scan results and append each result's object path */
  2827. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  2828. paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  2829. if (paths[i] == NULL) {
  2830. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
  2831. "no memory");
  2832. goto out;
  2833. }
  2834. /* Construct the object path for this BSS. */
  2835. os_snprintf(paths[i++], WPAS_DBUS_OBJECT_PATH_MAX,
  2836. "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
  2837. wpa_s->dbus_new_path, bss->id);
  2838. }
  2839. success = wpas_dbus_simple_array_property_getter(iter,
  2840. DBUS_TYPE_OBJECT_PATH,
  2841. paths, wpa_s->num_bss,
  2842. error);
  2843. out:
  2844. while (i)
  2845. os_free(paths[--i]);
  2846. os_free(paths);
  2847. return success;
  2848. }
  2849. /**
  2850. * wpas_dbus_getter_networks - Get array of networks objects
  2851. * @iter: Pointer to incoming dbus message iter
  2852. * @error: Location to store error on failure
  2853. * @user_data: Function specific data
  2854. * Returns: TRUE on success, FALSE on failure
  2855. *
  2856. * Getter for "Networks" property.
  2857. */
  2858. dbus_bool_t wpas_dbus_getter_networks(DBusMessageIter *iter, DBusError *error,
  2859. void *user_data)
  2860. {
  2861. struct wpa_supplicant *wpa_s = user_data;
  2862. struct wpa_ssid *ssid;
  2863. char **paths;
  2864. unsigned int i = 0, num = 0;
  2865. dbus_bool_t success = FALSE;
  2866. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  2867. if (!network_is_persistent_group(ssid))
  2868. num++;
  2869. paths = os_calloc(num, sizeof(char *));
  2870. if (!paths) {
  2871. dbus_set_error(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2872. return FALSE;
  2873. }
  2874. /* Loop through configured networks and append object path of each */
  2875. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  2876. if (network_is_persistent_group(ssid))
  2877. continue;
  2878. paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  2879. if (paths[i] == NULL) {
  2880. dbus_set_error(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2881. goto out;
  2882. }
  2883. /* Construct the object path for this network. */
  2884. os_snprintf(paths[i++], WPAS_DBUS_OBJECT_PATH_MAX,
  2885. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%d",
  2886. wpa_s->dbus_new_path, ssid->id);
  2887. }
  2888. success = wpas_dbus_simple_array_property_getter(iter,
  2889. DBUS_TYPE_OBJECT_PATH,
  2890. paths, num, error);
  2891. out:
  2892. while (i)
  2893. os_free(paths[--i]);
  2894. os_free(paths);
  2895. return success;
  2896. }
  2897. /**
  2898. * wpas_dbus_getter_pkcs11_engine_path - Get PKCS #11 engine path
  2899. * @iter: Pointer to incoming dbus message iter
  2900. * @error: Location to store error on failure
  2901. * @user_data: Function specific data
  2902. * Returns: A dbus message containing the PKCS #11 engine path
  2903. *
  2904. * Getter for "PKCS11EnginePath" property.
  2905. */
  2906. dbus_bool_t wpas_dbus_getter_pkcs11_engine_path(DBusMessageIter *iter,
  2907. DBusError *error,
  2908. void *user_data)
  2909. {
  2910. struct wpa_supplicant *wpa_s = user_data;
  2911. const char *pkcs11_engine_path;
  2912. if (wpa_s->conf->pkcs11_engine_path == NULL)
  2913. pkcs11_engine_path = "";
  2914. else
  2915. pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
  2916. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2917. &pkcs11_engine_path, error);
  2918. }
  2919. /**
  2920. * wpas_dbus_getter_pkcs11_module_path - Get PKCS #11 module path
  2921. * @iter: Pointer to incoming dbus message iter
  2922. * @error: Location to store error on failure
  2923. * @user_data: Function specific data
  2924. * Returns: A dbus message containing the PKCS #11 module path
  2925. *
  2926. * Getter for "PKCS11ModulePath" property.
  2927. */
  2928. dbus_bool_t wpas_dbus_getter_pkcs11_module_path(DBusMessageIter *iter,
  2929. DBusError *error,
  2930. void *user_data)
  2931. {
  2932. struct wpa_supplicant *wpa_s = user_data;
  2933. const char *pkcs11_module_path;
  2934. if (wpa_s->conf->pkcs11_module_path == NULL)
  2935. pkcs11_module_path = "";
  2936. else
  2937. pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
  2938. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2939. &pkcs11_module_path, error);
  2940. }
  2941. /**
  2942. * wpas_dbus_getter_blobs - Get all blobs defined for this interface
  2943. * @iter: Pointer to incoming dbus message iter
  2944. * @error: Location to store error on failure
  2945. * @user_data: Function specific data
  2946. * Returns: TRUE on success, FALSE on failure
  2947. *
  2948. * Getter for "Blobs" property.
  2949. */
  2950. dbus_bool_t wpas_dbus_getter_blobs(DBusMessageIter *iter, DBusError *error,
  2951. void *user_data)
  2952. {
  2953. struct wpa_supplicant *wpa_s = user_data;
  2954. DBusMessageIter variant_iter, dict_iter, entry_iter, array_iter;
  2955. struct wpa_config_blob *blob;
  2956. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  2957. "a{say}", &variant_iter) ||
  2958. !dbus_message_iter_open_container(&variant_iter, DBUS_TYPE_ARRAY,
  2959. "{say}", &dict_iter)) {
  2960. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2961. return FALSE;
  2962. }
  2963. blob = wpa_s->conf->blobs;
  2964. while (blob) {
  2965. if (!dbus_message_iter_open_container(&dict_iter,
  2966. DBUS_TYPE_DICT_ENTRY,
  2967. NULL, &entry_iter) ||
  2968. !dbus_message_iter_append_basic(&entry_iter,
  2969. DBUS_TYPE_STRING,
  2970. &(blob->name)) ||
  2971. !dbus_message_iter_open_container(&entry_iter,
  2972. DBUS_TYPE_ARRAY,
  2973. DBUS_TYPE_BYTE_AS_STRING,
  2974. &array_iter) ||
  2975. !dbus_message_iter_append_fixed_array(&array_iter,
  2976. DBUS_TYPE_BYTE,
  2977. &(blob->data),
  2978. blob->len) ||
  2979. !dbus_message_iter_close_container(&entry_iter,
  2980. &array_iter) ||
  2981. !dbus_message_iter_close_container(&dict_iter,
  2982. &entry_iter)) {
  2983. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
  2984. "no memory");
  2985. return FALSE;
  2986. }
  2987. blob = blob->next;
  2988. }
  2989. if (!dbus_message_iter_close_container(&variant_iter, &dict_iter) ||
  2990. !dbus_message_iter_close_container(iter, &variant_iter)) {
  2991. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2992. return FALSE;
  2993. }
  2994. return TRUE;
  2995. }
  2996. static struct wpa_bss * get_bss_helper(struct bss_handler_args *args,
  2997. DBusError *error, const char *func_name)
  2998. {
  2999. struct wpa_bss *res = wpa_bss_get_id(args->wpa_s, args->id);
  3000. if (!res) {
  3001. wpa_printf(MSG_ERROR, "%s[dbus]: no bss with id %d found",
  3002. func_name, args->id);
  3003. dbus_set_error(error, DBUS_ERROR_FAILED,
  3004. "%s: BSS %d not found",
  3005. func_name, args->id);
  3006. }
  3007. return res;
  3008. }
  3009. /**
  3010. * wpas_dbus_getter_bss_bssid - Return the BSSID of a BSS
  3011. * @iter: Pointer to incoming dbus message iter
  3012. * @error: Location to store error on failure
  3013. * @user_data: Function specific data
  3014. * Returns: TRUE on success, FALSE on failure
  3015. *
  3016. * Getter for "BSSID" property.
  3017. */
  3018. dbus_bool_t wpas_dbus_getter_bss_bssid(DBusMessageIter *iter, DBusError *error,
  3019. void *user_data)
  3020. {
  3021. struct bss_handler_args *args = user_data;
  3022. struct wpa_bss *res;
  3023. res = get_bss_helper(args, error, __func__);
  3024. if (!res)
  3025. return FALSE;
  3026. return wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
  3027. res->bssid, ETH_ALEN,
  3028. error);
  3029. }
  3030. /**
  3031. * wpas_dbus_getter_bss_ssid - Return the SSID of a BSS
  3032. * @iter: Pointer to incoming dbus message iter
  3033. * @error: Location to store error on failure
  3034. * @user_data: Function specific data
  3035. * Returns: TRUE on success, FALSE on failure
  3036. *
  3037. * Getter for "SSID" property.
  3038. */
  3039. dbus_bool_t wpas_dbus_getter_bss_ssid(DBusMessageIter *iter, DBusError *error,
  3040. void *user_data)
  3041. {
  3042. struct bss_handler_args *args = user_data;
  3043. struct wpa_bss *res;
  3044. res = get_bss_helper(args, error, __func__);
  3045. if (!res)
  3046. return FALSE;
  3047. return wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
  3048. res->ssid, res->ssid_len,
  3049. error);
  3050. }
  3051. /**
  3052. * wpas_dbus_getter_bss_privacy - Return the privacy flag of a BSS
  3053. * @iter: Pointer to incoming dbus message iter
  3054. * @error: Location to store error on failure
  3055. * @user_data: Function specific data
  3056. * Returns: TRUE on success, FALSE on failure
  3057. *
  3058. * Getter for "Privacy" property.
  3059. */
  3060. dbus_bool_t wpas_dbus_getter_bss_privacy(DBusMessageIter *iter,
  3061. DBusError *error, void *user_data)
  3062. {
  3063. struct bss_handler_args *args = user_data;
  3064. struct wpa_bss *res;
  3065. dbus_bool_t privacy;
  3066. res = get_bss_helper(args, error, __func__);
  3067. if (!res)
  3068. return FALSE;
  3069. privacy = (res->caps & IEEE80211_CAP_PRIVACY) ? TRUE : FALSE;
  3070. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  3071. &privacy, error);
  3072. }
  3073. /**
  3074. * wpas_dbus_getter_bss_mode - Return the mode of a BSS
  3075. * @iter: Pointer to incoming dbus message iter
  3076. * @error: Location to store error on failure
  3077. * @user_data: Function specific data
  3078. * Returns: TRUE on success, FALSE on failure
  3079. *
  3080. * Getter for "Mode" property.
  3081. */
  3082. dbus_bool_t wpas_dbus_getter_bss_mode(DBusMessageIter *iter, DBusError *error,
  3083. void *user_data)
  3084. {
  3085. struct bss_handler_args *args = user_data;
  3086. struct wpa_bss *res;
  3087. const char *mode;
  3088. res = get_bss_helper(args, error, __func__);
  3089. if (!res)
  3090. return FALSE;
  3091. if (bss_is_dmg(res)) {
  3092. switch (res->caps & IEEE80211_CAP_DMG_MASK) {
  3093. case IEEE80211_CAP_DMG_PBSS:
  3094. case IEEE80211_CAP_DMG_IBSS:
  3095. mode = "ad-hoc";
  3096. break;
  3097. case IEEE80211_CAP_DMG_AP:
  3098. mode = "infrastructure";
  3099. break;
  3100. }
  3101. } else {
  3102. if (res->caps & IEEE80211_CAP_IBSS)
  3103. mode = "ad-hoc";
  3104. else
  3105. mode = "infrastructure";
  3106. }
  3107. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  3108. &mode, error);
  3109. }
  3110. /**
  3111. * wpas_dbus_getter_bss_level - Return the signal strength of a BSS
  3112. * @iter: Pointer to incoming dbus message iter
  3113. * @error: Location to store error on failure
  3114. * @user_data: Function specific data
  3115. * Returns: TRUE on success, FALSE on failure
  3116. *
  3117. * Getter for "Level" property.
  3118. */
  3119. dbus_bool_t wpas_dbus_getter_bss_signal(DBusMessageIter *iter,
  3120. DBusError *error, void *user_data)
  3121. {
  3122. struct bss_handler_args *args = user_data;
  3123. struct wpa_bss *res;
  3124. s16 level;
  3125. res = get_bss_helper(args, error, __func__);
  3126. if (!res)
  3127. return FALSE;
  3128. level = (s16) res->level;
  3129. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_INT16,
  3130. &level, error);
  3131. }
  3132. /**
  3133. * wpas_dbus_getter_bss_frequency - Return the frequency of a BSS
  3134. * @iter: Pointer to incoming dbus message iter
  3135. * @error: Location to store error on failure
  3136. * @user_data: Function specific data
  3137. * Returns: TRUE on success, FALSE on failure
  3138. *
  3139. * Getter for "Frequency" property.
  3140. */
  3141. dbus_bool_t wpas_dbus_getter_bss_frequency(DBusMessageIter *iter,
  3142. DBusError *error, void *user_data)
  3143. {
  3144. struct bss_handler_args *args = user_data;
  3145. struct wpa_bss *res;
  3146. u16 freq;
  3147. res = get_bss_helper(args, error, __func__);
  3148. if (!res)
  3149. return FALSE;
  3150. freq = (u16) res->freq;
  3151. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT16,
  3152. &freq, error);
  3153. }
  3154. static int cmp_u8s_desc(const void *a, const void *b)
  3155. {
  3156. return (*(u8 *) b - *(u8 *) a);
  3157. }
  3158. /**
  3159. * wpas_dbus_getter_bss_rates - Return available bit rates of a BSS
  3160. * @iter: Pointer to incoming dbus message iter
  3161. * @error: Location to store error on failure
  3162. * @user_data: Function specific data
  3163. * Returns: TRUE on success, FALSE on failure
  3164. *
  3165. * Getter for "Rates" property.
  3166. */
  3167. dbus_bool_t wpas_dbus_getter_bss_rates(DBusMessageIter *iter,
  3168. DBusError *error, void *user_data)
  3169. {
  3170. struct bss_handler_args *args = user_data;
  3171. struct wpa_bss *res;
  3172. u8 *ie_rates = NULL;
  3173. u32 *real_rates;
  3174. int rates_num, i;
  3175. dbus_bool_t success = FALSE;
  3176. res = get_bss_helper(args, error, __func__);
  3177. if (!res)
  3178. return FALSE;
  3179. rates_num = wpa_bss_get_bit_rates(res, &ie_rates);
  3180. if (rates_num < 0)
  3181. return FALSE;
  3182. qsort(ie_rates, rates_num, 1, cmp_u8s_desc);
  3183. real_rates = os_malloc(sizeof(u32) * rates_num);
  3184. if (!real_rates) {
  3185. os_free(ie_rates);
  3186. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3187. return FALSE;
  3188. }
  3189. for (i = 0; i < rates_num; i++)
  3190. real_rates[i] = ie_rates[i] * 500000;
  3191. success = wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_UINT32,
  3192. real_rates, rates_num,
  3193. error);
  3194. os_free(ie_rates);
  3195. os_free(real_rates);
  3196. return success;
  3197. }
  3198. static dbus_bool_t wpas_dbus_get_bss_security_prop(DBusMessageIter *iter,
  3199. struct wpa_ie_data *ie_data,
  3200. DBusError *error)
  3201. {
  3202. DBusMessageIter iter_dict, variant_iter;
  3203. const char *group;
  3204. const char *pairwise[5]; /* max 5 pairwise ciphers is supported */
  3205. const char *key_mgmt[8]; /* max 8 key managements may be supported */
  3206. int n;
  3207. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  3208. "a{sv}", &variant_iter))
  3209. goto nomem;
  3210. if (!wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
  3211. goto nomem;
  3212. /* KeyMgmt */
  3213. n = 0;
  3214. if (ie_data->key_mgmt & WPA_KEY_MGMT_PSK)
  3215. key_mgmt[n++] = "wpa-psk";
  3216. if (ie_data->key_mgmt & WPA_KEY_MGMT_FT_PSK)
  3217. key_mgmt[n++] = "wpa-ft-psk";
  3218. if (ie_data->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
  3219. key_mgmt[n++] = "wpa-psk-sha256";
  3220. if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  3221. key_mgmt[n++] = "wpa-eap";
  3222. if (ie_data->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
  3223. key_mgmt[n++] = "wpa-ft-eap";
  3224. if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
  3225. key_mgmt[n++] = "wpa-eap-sha256";
  3226. if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B)
  3227. key_mgmt[n++] = "wpa-eap-suite-b";
  3228. if (ie_data->key_mgmt & WPA_KEY_MGMT_NONE)
  3229. key_mgmt[n++] = "wpa-none";
  3230. if (!wpa_dbus_dict_append_string_array(&iter_dict, "KeyMgmt",
  3231. key_mgmt, n))
  3232. goto nomem;
  3233. /* Group */
  3234. switch (ie_data->group_cipher) {
  3235. case WPA_CIPHER_WEP40:
  3236. group = "wep40";
  3237. break;
  3238. case WPA_CIPHER_TKIP:
  3239. group = "tkip";
  3240. break;
  3241. case WPA_CIPHER_CCMP:
  3242. group = "ccmp";
  3243. break;
  3244. case WPA_CIPHER_GCMP:
  3245. group = "gcmp";
  3246. break;
  3247. case WPA_CIPHER_WEP104:
  3248. group = "wep104";
  3249. break;
  3250. case WPA_CIPHER_CCMP_256:
  3251. group = "ccmp-256";
  3252. break;
  3253. case WPA_CIPHER_GCMP_256:
  3254. group = "gcmp-256";
  3255. break;
  3256. default:
  3257. group = "";
  3258. break;
  3259. }
  3260. if (!wpa_dbus_dict_append_string(&iter_dict, "Group", group))
  3261. goto nomem;
  3262. /* Pairwise */
  3263. n = 0;
  3264. if (ie_data->pairwise_cipher & WPA_CIPHER_TKIP)
  3265. pairwise[n++] = "tkip";
  3266. if (ie_data->pairwise_cipher & WPA_CIPHER_CCMP)
  3267. pairwise[n++] = "ccmp";
  3268. if (ie_data->pairwise_cipher & WPA_CIPHER_GCMP)
  3269. pairwise[n++] = "gcmp";
  3270. if (ie_data->pairwise_cipher & WPA_CIPHER_CCMP_256)
  3271. pairwise[n++] = "ccmp-256";
  3272. if (ie_data->pairwise_cipher & WPA_CIPHER_GCMP_256)
  3273. pairwise[n++] = "gcmp-256";
  3274. if (!wpa_dbus_dict_append_string_array(&iter_dict, "Pairwise",
  3275. pairwise, n))
  3276. goto nomem;
  3277. /* Management group (RSN only) */
  3278. if (ie_data->proto == WPA_PROTO_RSN) {
  3279. switch (ie_data->mgmt_group_cipher) {
  3280. #ifdef CONFIG_IEEE80211W
  3281. case WPA_CIPHER_AES_128_CMAC:
  3282. group = "aes128cmac";
  3283. break;
  3284. #endif /* CONFIG_IEEE80211W */
  3285. default:
  3286. group = "";
  3287. break;
  3288. }
  3289. if (!wpa_dbus_dict_append_string(&iter_dict, "MgmtGroup",
  3290. group))
  3291. goto nomem;
  3292. }
  3293. if (!wpa_dbus_dict_close_write(&variant_iter, &iter_dict))
  3294. goto nomem;
  3295. if (!dbus_message_iter_close_container(iter, &variant_iter))
  3296. goto nomem;
  3297. return TRUE;
  3298. nomem:
  3299. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3300. return FALSE;
  3301. }
  3302. /**
  3303. * wpas_dbus_getter_bss_wpa - Return the WPA options of a BSS
  3304. * @iter: Pointer to incoming dbus message iter
  3305. * @error: Location to store error on failure
  3306. * @user_data: Function specific data
  3307. * Returns: TRUE on success, FALSE on failure
  3308. *
  3309. * Getter for "WPA" property.
  3310. */
  3311. dbus_bool_t wpas_dbus_getter_bss_wpa(DBusMessageIter *iter, DBusError *error,
  3312. void *user_data)
  3313. {
  3314. struct bss_handler_args *args = user_data;
  3315. struct wpa_bss *res;
  3316. struct wpa_ie_data wpa_data;
  3317. const u8 *ie;
  3318. res = get_bss_helper(args, error, __func__);
  3319. if (!res)
  3320. return FALSE;
  3321. os_memset(&wpa_data, 0, sizeof(wpa_data));
  3322. ie = wpa_bss_get_vendor_ie(res, WPA_IE_VENDOR_TYPE);
  3323. if (ie) {
  3324. if (wpa_parse_wpa_ie(ie, 2 + ie[1], &wpa_data) < 0) {
  3325. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  3326. "failed to parse WPA IE");
  3327. return FALSE;
  3328. }
  3329. }
  3330. return wpas_dbus_get_bss_security_prop(iter, &wpa_data, error);
  3331. }
  3332. /**
  3333. * wpas_dbus_getter_bss_rsn - Return the RSN options of a BSS
  3334. * @iter: Pointer to incoming dbus message iter
  3335. * @error: Location to store error on failure
  3336. * @user_data: Function specific data
  3337. * Returns: TRUE on success, FALSE on failure
  3338. *
  3339. * Getter for "RSN" property.
  3340. */
  3341. dbus_bool_t wpas_dbus_getter_bss_rsn(DBusMessageIter *iter, DBusError *error,
  3342. void *user_data)
  3343. {
  3344. struct bss_handler_args *args = user_data;
  3345. struct wpa_bss *res;
  3346. struct wpa_ie_data wpa_data;
  3347. const u8 *ie;
  3348. res = get_bss_helper(args, error, __func__);
  3349. if (!res)
  3350. return FALSE;
  3351. os_memset(&wpa_data, 0, sizeof(wpa_data));
  3352. ie = wpa_bss_get_ie(res, WLAN_EID_RSN);
  3353. if (ie) {
  3354. if (wpa_parse_wpa_ie(ie, 2 + ie[1], &wpa_data) < 0) {
  3355. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  3356. "failed to parse RSN IE");
  3357. return FALSE;
  3358. }
  3359. }
  3360. return wpas_dbus_get_bss_security_prop(iter, &wpa_data, error);
  3361. }
  3362. /**
  3363. * wpas_dbus_getter_bss_wps - Return the WPS options of a BSS
  3364. * @iter: Pointer to incoming dbus message iter
  3365. * @error: Location to store error on failure
  3366. * @user_data: Function specific data
  3367. * Returns: TRUE on success, FALSE on failure
  3368. *
  3369. * Getter for "WPS" property.
  3370. */
  3371. dbus_bool_t wpas_dbus_getter_bss_wps(DBusMessageIter *iter, DBusError *error,
  3372. void *user_data)
  3373. {
  3374. struct bss_handler_args *args = user_data;
  3375. struct wpa_bss *res;
  3376. #ifdef CONFIG_WPS
  3377. struct wpabuf *wps_ie;
  3378. #endif /* CONFIG_WPS */
  3379. DBusMessageIter iter_dict, variant_iter;
  3380. const char *type = "";
  3381. res = get_bss_helper(args, error, __func__);
  3382. if (!res)
  3383. return FALSE;
  3384. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  3385. "a{sv}", &variant_iter))
  3386. goto nomem;
  3387. if (!wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
  3388. goto nomem;
  3389. #ifdef CONFIG_WPS
  3390. wps_ie = wpa_bss_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
  3391. if (wps_ie) {
  3392. if (wps_is_selected_pbc_registrar(wps_ie))
  3393. type = "pbc";
  3394. else if (wps_is_selected_pin_registrar(wps_ie))
  3395. type = "pin";
  3396. wpabuf_free(wps_ie);
  3397. }
  3398. #endif /* CONFIG_WPS */
  3399. if (!wpa_dbus_dict_append_string(&iter_dict, "Type", type))
  3400. goto nomem;
  3401. if (!wpa_dbus_dict_close_write(&variant_iter, &iter_dict))
  3402. goto nomem;
  3403. if (!dbus_message_iter_close_container(iter, &variant_iter))
  3404. goto nomem;
  3405. return TRUE;
  3406. nomem:
  3407. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3408. return FALSE;
  3409. }
  3410. /**
  3411. * wpas_dbus_getter_bss_ies - Return all IEs of a BSS
  3412. * @iter: Pointer to incoming dbus message iter
  3413. * @error: Location to store error on failure
  3414. * @user_data: Function specific data
  3415. * Returns: TRUE on success, FALSE on failure
  3416. *
  3417. * Getter for "IEs" property.
  3418. */
  3419. dbus_bool_t wpas_dbus_getter_bss_ies(DBusMessageIter *iter, DBusError *error,
  3420. void *user_data)
  3421. {
  3422. struct bss_handler_args *args = user_data;
  3423. struct wpa_bss *res;
  3424. res = get_bss_helper(args, error, __func__);
  3425. if (!res)
  3426. return FALSE;
  3427. return wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
  3428. res + 1, res->ie_len,
  3429. error);
  3430. }
  3431. /**
  3432. * wpas_dbus_getter_bss_age - Return time in seconds since BSS was last seen
  3433. * @iter: Pointer to incoming dbus message iter
  3434. * @error: Location to store error on failure
  3435. * @user_data: Function specific data
  3436. * Returns: TRUE on success, FALSE on failure
  3437. *
  3438. * Getter for BSS age
  3439. */
  3440. dbus_bool_t wpas_dbus_getter_bss_age(DBusMessageIter *iter, DBusError *error,
  3441. void *user_data)
  3442. {
  3443. struct bss_handler_args *args = user_data;
  3444. struct wpa_bss *res;
  3445. struct os_reltime now, diff = { 0, 0 };
  3446. u32 age;
  3447. res = get_bss_helper(args, error, __func__);
  3448. if (!res)
  3449. return FALSE;
  3450. os_get_reltime(&now);
  3451. os_reltime_sub(&now, &res->last_update, &diff);
  3452. age = diff.sec > 0 ? diff.sec : 0;
  3453. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT32, &age,
  3454. error);
  3455. }
  3456. /**
  3457. * wpas_dbus_getter_enabled - Check whether network is enabled or disabled
  3458. * @iter: Pointer to incoming dbus message iter
  3459. * @error: Location to store error on failure
  3460. * @user_data: Function specific data
  3461. * Returns: TRUE on success, FALSE on failure
  3462. *
  3463. * Getter for "enabled" property of a configured network.
  3464. */
  3465. dbus_bool_t wpas_dbus_getter_enabled(DBusMessageIter *iter, DBusError *error,
  3466. void *user_data)
  3467. {
  3468. struct network_handler_args *net = user_data;
  3469. dbus_bool_t enabled = net->ssid->disabled ? FALSE : TRUE;
  3470. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  3471. &enabled, error);
  3472. }
  3473. /**
  3474. * wpas_dbus_setter_enabled - Mark a configured network as enabled or disabled
  3475. * @iter: Pointer to incoming dbus message iter
  3476. * @error: Location to store error on failure
  3477. * @user_data: Function specific data
  3478. * Returns: TRUE on success, FALSE on failure
  3479. *
  3480. * Setter for "Enabled" property of a configured network.
  3481. */
  3482. dbus_bool_t wpas_dbus_setter_enabled(DBusMessageIter *iter, DBusError *error,
  3483. void *user_data)
  3484. {
  3485. struct network_handler_args *net = user_data;
  3486. struct wpa_supplicant *wpa_s;
  3487. struct wpa_ssid *ssid;
  3488. dbus_bool_t enable;
  3489. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_BOOLEAN,
  3490. &enable))
  3491. return FALSE;
  3492. wpa_s = net->wpa_s;
  3493. ssid = net->ssid;
  3494. if (enable)
  3495. wpa_supplicant_enable_network(wpa_s, ssid);
  3496. else
  3497. wpa_supplicant_disable_network(wpa_s, ssid);
  3498. return TRUE;
  3499. }
  3500. /**
  3501. * wpas_dbus_getter_network_properties - Get options for a configured network
  3502. * @iter: Pointer to incoming dbus message iter
  3503. * @error: Location to store error on failure
  3504. * @user_data: Function specific data
  3505. * Returns: TRUE on success, FALSE on failure
  3506. *
  3507. * Getter for "Properties" property of a configured network.
  3508. */
  3509. dbus_bool_t wpas_dbus_getter_network_properties(DBusMessageIter *iter,
  3510. DBusError *error,
  3511. void *user_data)
  3512. {
  3513. struct network_handler_args *net = user_data;
  3514. DBusMessageIter variant_iter, dict_iter;
  3515. char **iterator;
  3516. char **props = wpa_config_get_all(net->ssid, 1);
  3517. dbus_bool_t success = FALSE;
  3518. if (!props) {
  3519. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3520. return FALSE;
  3521. }
  3522. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "a{sv}",
  3523. &variant_iter) ||
  3524. !wpa_dbus_dict_open_write(&variant_iter, &dict_iter)) {
  3525. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3526. goto out;
  3527. }
  3528. iterator = props;
  3529. while (*iterator) {
  3530. if (!wpa_dbus_dict_append_string(&dict_iter, *iterator,
  3531. *(iterator + 1))) {
  3532. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
  3533. "no memory");
  3534. goto out;
  3535. }
  3536. iterator += 2;
  3537. }
  3538. if (!wpa_dbus_dict_close_write(&variant_iter, &dict_iter) ||
  3539. !dbus_message_iter_close_container(iter, &variant_iter)) {
  3540. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3541. goto out;
  3542. }
  3543. success = TRUE;
  3544. out:
  3545. iterator = props;
  3546. while (*iterator) {
  3547. os_free(*iterator);
  3548. iterator++;
  3549. }
  3550. os_free(props);
  3551. return success;
  3552. }
  3553. /**
  3554. * wpas_dbus_setter_network_properties - Set options for a configured network
  3555. * @iter: Pointer to incoming dbus message iter
  3556. * @error: Location to store error on failure
  3557. * @user_data: Function specific data
  3558. * Returns: TRUE on success, FALSE on failure
  3559. *
  3560. * Setter for "Properties" property of a configured network.
  3561. */
  3562. dbus_bool_t wpas_dbus_setter_network_properties(DBusMessageIter *iter,
  3563. DBusError *error,
  3564. void *user_data)
  3565. {
  3566. struct network_handler_args *net = user_data;
  3567. struct wpa_ssid *ssid = net->ssid;
  3568. DBusMessageIter variant_iter;
  3569. dbus_message_iter_recurse(iter, &variant_iter);
  3570. return set_network_properties(net->wpa_s, ssid, &variant_iter, error);
  3571. }
  3572. #ifdef CONFIG_AP
  3573. DBusMessage * wpas_dbus_handler_subscribe_preq(
  3574. DBusMessage *message, struct wpa_supplicant *wpa_s)
  3575. {
  3576. struct wpas_dbus_priv *priv = wpa_s->global->dbus;
  3577. char *name;
  3578. if (wpa_s->preq_notify_peer != NULL) {
  3579. if (os_strcmp(dbus_message_get_sender(message),
  3580. wpa_s->preq_notify_peer) == 0)
  3581. return NULL;
  3582. return dbus_message_new_error(message,
  3583. WPAS_DBUS_ERROR_SUBSCRIPTION_IN_USE,
  3584. "Another application is already subscribed");
  3585. }
  3586. name = os_strdup(dbus_message_get_sender(message));
  3587. if (!name)
  3588. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  3589. "out of memory");
  3590. wpa_s->preq_notify_peer = name;
  3591. /* Subscribe to clean up if application closes socket */
  3592. wpas_dbus_subscribe_noc(priv);
  3593. /*
  3594. * Double-check it's still alive to make sure that we didn't
  3595. * miss the NameOwnerChanged signal, e.g. while strdup'ing.
  3596. */
  3597. if (!dbus_bus_name_has_owner(priv->con, name, NULL)) {
  3598. /*
  3599. * Application no longer exists, clean up.
  3600. * The return value is irrelevant now.
  3601. *
  3602. * Need to check if the NameOwnerChanged handling
  3603. * already cleaned up because we have processed
  3604. * DBus messages while checking if the name still
  3605. * has an owner.
  3606. */
  3607. if (!wpa_s->preq_notify_peer)
  3608. return NULL;
  3609. os_free(wpa_s->preq_notify_peer);
  3610. wpa_s->preq_notify_peer = NULL;
  3611. wpas_dbus_unsubscribe_noc(priv);
  3612. }
  3613. return NULL;
  3614. }
  3615. DBusMessage * wpas_dbus_handler_unsubscribe_preq(
  3616. DBusMessage *message, struct wpa_supplicant *wpa_s)
  3617. {
  3618. struct wpas_dbus_priv *priv = wpa_s->global->dbus;
  3619. if (!wpa_s->preq_notify_peer)
  3620. return dbus_message_new_error(message,
  3621. WPAS_DBUS_ERROR_NO_SUBSCRIPTION,
  3622. "Not subscribed");
  3623. if (os_strcmp(wpa_s->preq_notify_peer,
  3624. dbus_message_get_sender(message)))
  3625. return dbus_message_new_error(message,
  3626. WPAS_DBUS_ERROR_SUBSCRIPTION_EPERM,
  3627. "Can't unsubscribe others");
  3628. os_free(wpa_s->preq_notify_peer);
  3629. wpa_s->preq_notify_peer = NULL;
  3630. wpas_dbus_unsubscribe_noc(priv);
  3631. return NULL;
  3632. }
  3633. void wpas_dbus_signal_preq(struct wpa_supplicant *wpa_s,
  3634. const u8 *addr, const u8 *dst, const u8 *bssid,
  3635. const u8 *ie, size_t ie_len, u32 ssi_signal)
  3636. {
  3637. DBusMessage *msg;
  3638. DBusMessageIter iter, dict_iter;
  3639. struct wpas_dbus_priv *priv = wpa_s->global->dbus;
  3640. /* Do nothing if the control interface is not turned on */
  3641. if (priv == NULL)
  3642. return;
  3643. if (wpa_s->preq_notify_peer == NULL)
  3644. return;
  3645. msg = dbus_message_new_signal(wpa_s->dbus_new_path,
  3646. WPAS_DBUS_NEW_IFACE_INTERFACE,
  3647. "ProbeRequest");
  3648. if (msg == NULL)
  3649. return;
  3650. dbus_message_set_destination(msg, wpa_s->preq_notify_peer);
  3651. dbus_message_iter_init_append(msg, &iter);
  3652. if (!wpa_dbus_dict_open_write(&iter, &dict_iter))
  3653. goto fail;
  3654. if (addr && !wpa_dbus_dict_append_byte_array(&dict_iter, "addr",
  3655. (const char *) addr,
  3656. ETH_ALEN))
  3657. goto fail;
  3658. if (dst && !wpa_dbus_dict_append_byte_array(&dict_iter, "dst",
  3659. (const char *) dst,
  3660. ETH_ALEN))
  3661. goto fail;
  3662. if (bssid && !wpa_dbus_dict_append_byte_array(&dict_iter, "bssid",
  3663. (const char *) bssid,
  3664. ETH_ALEN))
  3665. goto fail;
  3666. if (ie && ie_len && !wpa_dbus_dict_append_byte_array(&dict_iter, "ies",
  3667. (const char *) ie,
  3668. ie_len))
  3669. goto fail;
  3670. if (ssi_signal && !wpa_dbus_dict_append_int32(&dict_iter, "signal",
  3671. ssi_signal))
  3672. goto fail;
  3673. if (!wpa_dbus_dict_close_write(&iter, &dict_iter))
  3674. goto fail;
  3675. dbus_connection_send(priv->con, msg, NULL);
  3676. goto out;
  3677. fail:
  3678. wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
  3679. out:
  3680. dbus_message_unref(msg);
  3681. }
  3682. #endif /* CONFIG_AP */