dbus_new_handlers.c 114 KB

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