dbus_new_handlers.c 119 KB

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