dbus_new_handlers.c 115 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173
  1. /*
  2. * WPA Supplicant / dbus-based control interface
  3. * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
  4. * Copyright (c) 2009-2010, Witold Sowa <witold.sowa@gmail.com>
  5. * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
  6. *
  7. * This software may be distributed under the terms of the BSD license.
  8. * See README for more details.
  9. */
  10. #include "includes.h"
  11. #include "common.h"
  12. #include "common/ieee802_11_defs.h"
  13. #include "eap_peer/eap_methods.h"
  14. #include "eapol_supp/eapol_supp_sm.h"
  15. #include "rsn_supp/wpa.h"
  16. #include "../config.h"
  17. #include "../wpa_supplicant_i.h"
  18. #include "../driver_i.h"
  19. #include "../notify.h"
  20. #include "../bss.h"
  21. #include "../scan.h"
  22. #include "../autoscan.h"
  23. #include "dbus_new_helpers.h"
  24. #include "dbus_new.h"
  25. #include "dbus_new_handlers.h"
  26. #include "dbus_dict_helpers.h"
  27. #include "dbus_common_i.h"
  28. #include "drivers/driver.h"
  29. static const char *debug_strings[] = {
  30. "excessive", "msgdump", "debug", "info", "warning", "error", NULL
  31. };
  32. /**
  33. * wpas_dbus_error_unknown_error - Return a new InvalidArgs error message
  34. * @message: Pointer to incoming dbus message this error refers to
  35. * @arg: Optional string appended to error message
  36. * Returns: a dbus error message
  37. *
  38. * Convenience function to create and return an UnknownError
  39. */
  40. DBusMessage * wpas_dbus_error_unknown_error(DBusMessage *message,
  41. const char *arg)
  42. {
  43. /*
  44. * This function can be called as a result of a failure
  45. * within internal getter calls, which will call this function
  46. * with a NULL message parameter. However, dbus_message_new_error
  47. * looks very unkindly (i.e, abort()) on a NULL message, so
  48. * in this case, we should not call it.
  49. */
  50. if (message == NULL) {
  51. wpa_printf(MSG_INFO, "dbus: wpas_dbus_error_unknown_error "
  52. "called with NULL message (arg=%s)",
  53. arg ? arg : "N/A");
  54. return NULL;
  55. }
  56. return dbus_message_new_error(message, WPAS_DBUS_ERROR_UNKNOWN_ERROR,
  57. arg);
  58. }
  59. /**
  60. * wpas_dbus_error_iface_unknown - Return a new invalid interface error message
  61. * @message: Pointer to incoming dbus message this error refers to
  62. * Returns: A dbus error message
  63. *
  64. * Convenience function to create and return an invalid interface error
  65. */
  66. static DBusMessage * wpas_dbus_error_iface_unknown(DBusMessage *message)
  67. {
  68. return dbus_message_new_error(message, WPAS_DBUS_ERROR_IFACE_UNKNOWN,
  69. "wpa_supplicant knows nothing about "
  70. "this interface.");
  71. }
  72. /**
  73. * wpas_dbus_error_network_unknown - Return a new NetworkUnknown error message
  74. * @message: Pointer to incoming dbus message this error refers to
  75. * Returns: a dbus error message
  76. *
  77. * Convenience function to create and return an invalid network error
  78. */
  79. static DBusMessage * wpas_dbus_error_network_unknown(DBusMessage *message)
  80. {
  81. return dbus_message_new_error(message, WPAS_DBUS_ERROR_NETWORK_UNKNOWN,
  82. "There is no such a network in this "
  83. "interface.");
  84. }
  85. /**
  86. * wpas_dbus_error_invalid_args - Return a new InvalidArgs error message
  87. * @message: Pointer to incoming dbus message this error refers to
  88. * Returns: a dbus error message
  89. *
  90. * Convenience function to create and return an invalid options error
  91. */
  92. DBusMessage * wpas_dbus_error_invalid_args(DBusMessage *message,
  93. const char *arg)
  94. {
  95. DBusMessage *reply;
  96. reply = dbus_message_new_error(message, WPAS_DBUS_ERROR_INVALID_ARGS,
  97. "Did not receive correct message "
  98. "arguments.");
  99. if (arg != NULL)
  100. dbus_message_append_args(reply, DBUS_TYPE_STRING, &arg,
  101. DBUS_TYPE_INVALID);
  102. return reply;
  103. }
  104. /**
  105. * wpas_dbus_error_scan_error - Return a new ScanError error message
  106. * @message: Pointer to incoming dbus message this error refers to
  107. * @error: Optional string to be used as the error message
  108. * Returns: a dbus error message
  109. *
  110. * Convenience function to create and return a scan error
  111. */
  112. static DBusMessage * wpas_dbus_error_scan_error(DBusMessage *message,
  113. const char *error)
  114. {
  115. return dbus_message_new_error(message,
  116. WPAS_DBUS_ERROR_IFACE_SCAN_ERROR,
  117. error);
  118. }
  119. static const char *dont_quote[] = {
  120. "key_mgmt", "proto", "pairwise", "auth_alg", "group", "eap",
  121. "opensc_engine_path", "pkcs11_engine_path", "pkcs11_module_path",
  122. "bssid", "scan_freq", "freq_list", NULL
  123. };
  124. static dbus_bool_t should_quote_opt(const char *key)
  125. {
  126. int i = 0;
  127. while (dont_quote[i] != NULL) {
  128. if (os_strcmp(key, dont_quote[i]) == 0)
  129. return FALSE;
  130. i++;
  131. }
  132. return TRUE;
  133. }
  134. /**
  135. * get_iface_by_dbus_path - Get a new network interface
  136. * @global: Pointer to global data from wpa_supplicant_init()
  137. * @path: Pointer to a dbus object path representing an interface
  138. * Returns: Pointer to the interface or %NULL if not found
  139. */
  140. static struct wpa_supplicant * get_iface_by_dbus_path(
  141. struct wpa_global *global, const char *path)
  142. {
  143. struct wpa_supplicant *wpa_s;
  144. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  145. if (os_strcmp(wpa_s->dbus_new_path, path) == 0)
  146. return wpa_s;
  147. }
  148. return NULL;
  149. }
  150. /**
  151. * set_network_properties - Set properties of a configured network
  152. * @wpa_s: wpa_supplicant structure for a network interface
  153. * @ssid: wpa_ssid structure for a configured network
  154. * @iter: DBus message iterator containing dictionary of network
  155. * properties to set.
  156. * @error: On failure, an error describing the failure
  157. * Returns: TRUE if the request succeeds, FALSE if it failed
  158. *
  159. * Sets network configuration with parameters given id DBus dictionary
  160. */
  161. dbus_bool_t set_network_properties(struct wpa_supplicant *wpa_s,
  162. struct wpa_ssid *ssid,
  163. DBusMessageIter *iter,
  164. DBusError *error)
  165. {
  166. struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
  167. DBusMessageIter iter_dict;
  168. char *value = NULL;
  169. if (!wpa_dbus_dict_open_read(iter, &iter_dict, error))
  170. return FALSE;
  171. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  172. size_t size = 50;
  173. int ret;
  174. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  175. goto error;
  176. value = NULL;
  177. if (entry.type == DBUS_TYPE_ARRAY &&
  178. entry.array_type == DBUS_TYPE_BYTE) {
  179. if (entry.array_len <= 0)
  180. goto error;
  181. size = entry.array_len * 2 + 1;
  182. value = os_zalloc(size);
  183. if (value == NULL)
  184. goto error;
  185. ret = wpa_snprintf_hex(value, size,
  186. (u8 *) entry.bytearray_value,
  187. entry.array_len);
  188. if (ret <= 0)
  189. goto error;
  190. } else if (entry.type == DBUS_TYPE_STRING) {
  191. if (should_quote_opt(entry.key)) {
  192. size = os_strlen(entry.str_value);
  193. if (size <= 0)
  194. goto error;
  195. size += 3;
  196. value = os_zalloc(size);
  197. if (value == NULL)
  198. goto error;
  199. ret = os_snprintf(value, size, "\"%s\"",
  200. entry.str_value);
  201. if (os_snprintf_error(size, ret))
  202. goto error;
  203. } else {
  204. value = os_strdup(entry.str_value);
  205. if (value == NULL)
  206. goto error;
  207. }
  208. } else if (entry.type == DBUS_TYPE_UINT32) {
  209. value = os_zalloc(size);
  210. if (value == NULL)
  211. goto error;
  212. ret = os_snprintf(value, size, "%u",
  213. entry.uint32_value);
  214. if (os_snprintf_error(size, ret))
  215. goto error;
  216. } else if (entry.type == DBUS_TYPE_INT32) {
  217. value = os_zalloc(size);
  218. if (value == NULL)
  219. goto error;
  220. ret = os_snprintf(value, size, "%d",
  221. entry.int32_value);
  222. if (os_snprintf_error(size, ret))
  223. goto error;
  224. } else
  225. goto error;
  226. if (wpa_config_set(ssid, entry.key, value, 0) < 0)
  227. goto error;
  228. if ((os_strcmp(entry.key, "psk") == 0 &&
  229. value[0] == '"' && ssid->ssid_len) ||
  230. (os_strcmp(entry.key, "ssid") == 0 && ssid->passphrase))
  231. wpa_config_update_psk(ssid);
  232. else if (os_strcmp(entry.key, "priority") == 0)
  233. wpa_config_update_prio_list(wpa_s->conf);
  234. os_free(value);
  235. value = NULL;
  236. wpa_dbus_dict_entry_clear(&entry);
  237. }
  238. return TRUE;
  239. error:
  240. os_free(value);
  241. wpa_dbus_dict_entry_clear(&entry);
  242. dbus_set_error_const(error, DBUS_ERROR_INVALID_ARGS,
  243. "invalid message format");
  244. return FALSE;
  245. }
  246. /**
  247. * wpas_dbus_simple_property_getter - Get basic type property
  248. * @iter: Message iter to use when appending arguments
  249. * @type: DBus type of property (must be basic type)
  250. * @val: pointer to place holding property value
  251. * @error: On failure an error describing the failure
  252. * Returns: TRUE if the request was successful, FALSE if it failed
  253. *
  254. * Generic getter for basic type properties. Type is required to be basic.
  255. */
  256. dbus_bool_t wpas_dbus_simple_property_getter(DBusMessageIter *iter,
  257. const int type,
  258. const void *val,
  259. DBusError *error)
  260. {
  261. DBusMessageIter variant_iter;
  262. if (!dbus_type_is_basic(type)) {
  263. dbus_set_error(error, DBUS_ERROR_FAILED,
  264. "%s: given type is not basic", __func__);
  265. return FALSE;
  266. }
  267. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  268. wpa_dbus_type_as_string(type),
  269. &variant_iter) ||
  270. !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") &&
  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") &&
  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") &&
  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") &&
  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(message,
  511. WPAS_DBUS_ERROR_IFACE_EXISTS,
  512. "wpa_supplicant already "
  513. "controls this interface.");
  514. } else {
  515. struct wpa_supplicant *wpa_s;
  516. struct wpa_interface iface;
  517. os_memset(&iface, 0, sizeof(iface));
  518. iface.driver = driver;
  519. iface.ifname = ifname;
  520. iface.confname = confname;
  521. iface.bridge_ifname = bridge_ifname;
  522. /* Otherwise, have wpa_supplicant attach to it. */
  523. if ((wpa_s = wpa_supplicant_add_iface(global, &iface))) {
  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, "wpa_supplicant couldn't grab this "
  531. "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, "wpa_supplicant couldn't remove this "
  569. "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, "wrong debug "
  689. "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, "wpas_dbus_handler_scan[dbus]: "
  843. "Type must be a string");
  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, "wpas_dbus_handler_scan[dbus]: ssids "
  863. "must be an array of arrays of bytes");
  864. *reply = wpas_dbus_error_invalid_args(
  865. message, "Wrong SSIDs value type. Array of arrays of "
  866. "bytes required");
  867. return -1;
  868. }
  869. dbus_message_iter_recurse(var, &array_iter);
  870. if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_ARRAY ||
  871. dbus_message_iter_get_element_type(&array_iter) != DBUS_TYPE_BYTE)
  872. {
  873. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ssids "
  874. "must be an array of arrays of bytes");
  875. *reply = wpas_dbus_error_invalid_args(
  876. message, "Wrong SSIDs value type. Array of arrays of "
  877. "bytes required");
  878. return -1;
  879. }
  880. while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_ARRAY)
  881. {
  882. if (ssids_num >= WPAS_MAX_SCAN_SSIDS) {
  883. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  884. "Too many ssids specified on scan dbus "
  885. "call");
  886. *reply = wpas_dbus_error_invalid_args(
  887. message, "Too many ssids specified. Specify "
  888. "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. "wpas_dbus_handler_scan[dbus]: "
  896. "SSID too long (len=%d max_len=%d)",
  897. len, MAX_SSID_LEN);
  898. *reply = wpas_dbus_error_invalid_args(
  899. message, "Invalid SSID: too long");
  900. return -1;
  901. }
  902. if (len != 0) {
  903. ssid = os_malloc(len);
  904. if (ssid == NULL) {
  905. wpa_printf(MSG_DEBUG,
  906. "wpas_dbus_handler_scan[dbus]: "
  907. "out of memory. Cannot allocate "
  908. "memory for SSID");
  909. *reply = dbus_message_new_error(
  910. message, DBUS_ERROR_NO_MEMORY, NULL);
  911. return -1;
  912. }
  913. os_memcpy(ssid, val, len);
  914. } else {
  915. /* Allow zero-length SSIDs */
  916. ssid = NULL;
  917. }
  918. ssids[ssids_num].ssid = ssid;
  919. ssids[ssids_num].ssid_len = len;
  920. dbus_message_iter_next(&array_iter);
  921. ssids_num++;
  922. }
  923. params->num_ssids = ssids_num;
  924. return 0;
  925. }
  926. static int wpas_dbus_get_scan_ies(DBusMessage *message, DBusMessageIter *var,
  927. struct wpa_driver_scan_params *params,
  928. DBusMessage **reply)
  929. {
  930. u8 *ies = NULL, *nies;
  931. int ies_len = 0;
  932. DBusMessageIter array_iter, sub_array_iter;
  933. char *val;
  934. int len;
  935. if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_ARRAY) {
  936. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ies must "
  937. "be an array of arrays of bytes");
  938. *reply = wpas_dbus_error_invalid_args(
  939. message, "Wrong IEs value type. Array of arrays of "
  940. "bytes required");
  941. return -1;
  942. }
  943. dbus_message_iter_recurse(var, &array_iter);
  944. if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_ARRAY ||
  945. dbus_message_iter_get_element_type(&array_iter) != DBUS_TYPE_BYTE)
  946. {
  947. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ies must "
  948. "be an array of arrays of bytes");
  949. *reply = wpas_dbus_error_invalid_args(
  950. message, "Wrong IEs value type. Array required");
  951. return -1;
  952. }
  953. while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_ARRAY)
  954. {
  955. dbus_message_iter_recurse(&array_iter, &sub_array_iter);
  956. dbus_message_iter_get_fixed_array(&sub_array_iter, &val, &len);
  957. if (len == 0) {
  958. dbus_message_iter_next(&array_iter);
  959. continue;
  960. }
  961. nies = os_realloc(ies, ies_len + len);
  962. if (nies == NULL) {
  963. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  964. "out of memory. Cannot allocate memory for "
  965. "IE");
  966. os_free(ies);
  967. *reply = dbus_message_new_error(
  968. message, DBUS_ERROR_NO_MEMORY, NULL);
  969. return -1;
  970. }
  971. ies = nies;
  972. os_memcpy(ies + ies_len, val, len);
  973. ies_len += len;
  974. dbus_message_iter_next(&array_iter);
  975. }
  976. params->extra_ies = ies;
  977. params->extra_ies_len = ies_len;
  978. return 0;
  979. }
  980. static int wpas_dbus_get_scan_channels(DBusMessage *message,
  981. DBusMessageIter *var,
  982. struct wpa_driver_scan_params *params,
  983. DBusMessage **reply)
  984. {
  985. DBusMessageIter array_iter, sub_array_iter;
  986. int *freqs = NULL, *nfreqs;
  987. int freqs_num = 0;
  988. if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_ARRAY) {
  989. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  990. "Channels must be an array of structs");
  991. *reply = wpas_dbus_error_invalid_args(
  992. message, "Wrong Channels value type. Array of structs "
  993. "required");
  994. return -1;
  995. }
  996. dbus_message_iter_recurse(var, &array_iter);
  997. if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_STRUCT) {
  998. wpa_printf(MSG_DEBUG,
  999. "wpas_dbus_handler_scan[dbus]: Channels must be an "
  1000. "array of structs");
  1001. *reply = wpas_dbus_error_invalid_args(
  1002. message, "Wrong Channels value type. Array of structs "
  1003. "required");
  1004. return -1;
  1005. }
  1006. while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_STRUCT)
  1007. {
  1008. int freq, width;
  1009. dbus_message_iter_recurse(&array_iter, &sub_array_iter);
  1010. if (dbus_message_iter_get_arg_type(&sub_array_iter) !=
  1011. DBUS_TYPE_UINT32) {
  1012. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1013. "Channel must by specified by struct of "
  1014. "two UINT32s %c",
  1015. dbus_message_iter_get_arg_type(
  1016. &sub_array_iter));
  1017. *reply = wpas_dbus_error_invalid_args(
  1018. message, "Wrong Channel struct. Two UINT32s "
  1019. "required");
  1020. os_free(freqs);
  1021. return -1;
  1022. }
  1023. dbus_message_iter_get_basic(&sub_array_iter, &freq);
  1024. if (!dbus_message_iter_next(&sub_array_iter) ||
  1025. dbus_message_iter_get_arg_type(&sub_array_iter) !=
  1026. DBUS_TYPE_UINT32) {
  1027. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1028. "Channel must by specified by struct of "
  1029. "two UINT32s");
  1030. *reply = wpas_dbus_error_invalid_args(
  1031. message,
  1032. "Wrong Channel struct. Two UINT32s required");
  1033. os_free(freqs);
  1034. return -1;
  1035. }
  1036. dbus_message_iter_get_basic(&sub_array_iter, &width);
  1037. #define FREQS_ALLOC_CHUNK 32
  1038. if (freqs_num % FREQS_ALLOC_CHUNK == 0) {
  1039. nfreqs = os_realloc_array(
  1040. freqs, freqs_num + FREQS_ALLOC_CHUNK,
  1041. sizeof(int));
  1042. if (nfreqs == NULL)
  1043. os_free(freqs);
  1044. freqs = nfreqs;
  1045. }
  1046. if (freqs == NULL) {
  1047. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1048. "out of memory. can't allocate memory for "
  1049. "freqs");
  1050. *reply = dbus_message_new_error(
  1051. message, DBUS_ERROR_NO_MEMORY, NULL);
  1052. return -1;
  1053. }
  1054. freqs[freqs_num] = freq;
  1055. freqs_num++;
  1056. dbus_message_iter_next(&array_iter);
  1057. }
  1058. nfreqs = os_realloc_array(freqs, freqs_num + 1, sizeof(int));
  1059. if (nfreqs == NULL)
  1060. os_free(freqs);
  1061. freqs = nfreqs;
  1062. if (freqs == NULL) {
  1063. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1064. "out of memory. Can't allocate memory for freqs");
  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, "wpas_dbus_handler_scan[dbus]: "
  1080. "Type must be a boolean");
  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, "wpas_dbus_handler_scan[dbus]: "
  1140. "Unknown argument %s", 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, "wpas_dbus_handler_scan[dbus]: "
  1148. "Scan type not specified");
  1149. reply = wpas_dbus_error_invalid_args(message, key);
  1150. goto out;
  1151. }
  1152. if (!os_strcmp(type, "passive")) {
  1153. if (params.num_ssids || params.extra_ies_len) {
  1154. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1155. "SSIDs or IEs specified for passive scan.");
  1156. reply = wpas_dbus_error_invalid_args(
  1157. message, "You can specify only Channels in "
  1158. "passive scan");
  1159. goto out;
  1160. } else if (params.freqs && params.freqs[0]) {
  1161. if (wpa_supplicant_trigger_scan(wpa_s, &params)) {
  1162. reply = wpas_dbus_error_scan_error(
  1163. message, "Scan request rejected");
  1164. }
  1165. } else {
  1166. wpa_s->scan_req = MANUAL_SCAN_REQ;
  1167. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1168. }
  1169. } else if (!os_strcmp(type, "active")) {
  1170. if (!params.num_ssids) {
  1171. /* Add wildcard ssid */
  1172. params.num_ssids++;
  1173. }
  1174. #ifdef CONFIG_AUTOSCAN
  1175. autoscan_deinit(wpa_s);
  1176. #endif /* CONFIG_AUTOSCAN */
  1177. if (wpa_supplicant_trigger_scan(wpa_s, &params)) {
  1178. reply = wpas_dbus_error_scan_error(
  1179. message, "Scan request rejected");
  1180. }
  1181. } else {
  1182. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1183. "Unknown scan type: %s", type);
  1184. reply = wpas_dbus_error_invalid_args(message,
  1185. "Wrong scan type");
  1186. goto out;
  1187. }
  1188. if (!allow_roam)
  1189. wpa_s->scan_res_handler = scan_only_handler;
  1190. out:
  1191. for (i = 0; i < WPAS_MAX_SCAN_SSIDS; i++)
  1192. os_free((u8 *) params.ssids[i].ssid);
  1193. os_free((u8 *) params.extra_ies);
  1194. os_free(params.freqs);
  1195. return reply;
  1196. }
  1197. /**
  1198. * wpas_dbus_handler_signal_poll - Request immediate signal properties
  1199. * @message: Pointer to incoming dbus message
  1200. * @wpa_s: wpa_supplicant structure for a network interface
  1201. * Returns: NULL indicating success or DBus error message on failure
  1202. *
  1203. * Handler function for "SignalPoll" method call of a network device. Requests
  1204. * that wpa_supplicant read signal properties like RSSI, noise, and link
  1205. * speed and return them.
  1206. */
  1207. DBusMessage * wpas_dbus_handler_signal_poll(DBusMessage *message,
  1208. struct wpa_supplicant *wpa_s)
  1209. {
  1210. struct wpa_signal_info si;
  1211. DBusMessage *reply = NULL;
  1212. DBusMessageIter iter, iter_dict, variant_iter;
  1213. int ret;
  1214. ret = wpa_drv_signal_poll(wpa_s, &si);
  1215. if (ret) {
  1216. return dbus_message_new_error(message, DBUS_ERROR_FAILED,
  1217. "Failed to read signal");
  1218. }
  1219. reply = dbus_message_new_method_return(message);
  1220. if (reply == NULL)
  1221. goto nomem;
  1222. dbus_message_iter_init_append(reply, &iter);
  1223. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
  1224. "a{sv}", &variant_iter) ||
  1225. !wpa_dbus_dict_open_write(&variant_iter, &iter_dict) ||
  1226. !wpa_dbus_dict_append_int32(&iter_dict, "rssi",
  1227. si.current_signal) ||
  1228. !wpa_dbus_dict_append_int32(&iter_dict, "linkspeed",
  1229. si.current_txrate / 1000) ||
  1230. !wpa_dbus_dict_append_int32(&iter_dict, "noise",
  1231. si.current_noise) ||
  1232. !wpa_dbus_dict_append_uint32(&iter_dict, "frequency",
  1233. si.frequency) ||
  1234. (si.chanwidth != CHAN_WIDTH_UNKNOWN &&
  1235. !wpa_dbus_dict_append_string(
  1236. &iter_dict, "width",
  1237. channel_width_to_string(si.chanwidth))) ||
  1238. (si.center_frq1 > 0 && si.center_frq2 > 0 &&
  1239. (!wpa_dbus_dict_append_int32(&iter_dict, "center-frq1",
  1240. si.center_frq1) ||
  1241. !wpa_dbus_dict_append_int32(&iter_dict, "center-frq2",
  1242. si.center_frq2))) ||
  1243. (si.avg_signal &&
  1244. !wpa_dbus_dict_append_int32(&iter_dict, "avg-rssi",
  1245. si.avg_signal)) ||
  1246. !wpa_dbus_dict_close_write(&variant_iter, &iter_dict) ||
  1247. !dbus_message_iter_close_container(&iter, &variant_iter))
  1248. goto nomem;
  1249. return reply;
  1250. nomem:
  1251. if (reply)
  1252. dbus_message_unref(reply);
  1253. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY, NULL);
  1254. return reply;
  1255. }
  1256. /*
  1257. * wpas_dbus_handler_disconnect - Terminate the current connection
  1258. * @message: Pointer to incoming dbus message
  1259. * @wpa_s: wpa_supplicant structure for a network interface
  1260. * Returns: NotConnected DBus error message if already not connected
  1261. * or NULL otherwise.
  1262. *
  1263. * Handler function for "Disconnect" method call of network interface.
  1264. */
  1265. DBusMessage * wpas_dbus_handler_disconnect(DBusMessage *message,
  1266. struct wpa_supplicant *wpa_s)
  1267. {
  1268. if (wpa_s->current_ssid != NULL) {
  1269. wpa_s->disconnected = 1;
  1270. wpa_supplicant_deauthenticate(wpa_s,
  1271. WLAN_REASON_DEAUTH_LEAVING);
  1272. return NULL;
  1273. }
  1274. return dbus_message_new_error(message, WPAS_DBUS_ERROR_NOT_CONNECTED,
  1275. "This interface is not connected");
  1276. }
  1277. /**
  1278. * wpas_dbus_new_iface_add_network - Add a new configured network
  1279. * @message: Pointer to incoming dbus message
  1280. * @wpa_s: wpa_supplicant structure for a network interface
  1281. * Returns: A dbus message containing the object path of the new network
  1282. *
  1283. * Handler function for "AddNetwork" method call of a network interface.
  1284. */
  1285. DBusMessage * wpas_dbus_handler_add_network(DBusMessage *message,
  1286. struct wpa_supplicant *wpa_s)
  1287. {
  1288. DBusMessage *reply = NULL;
  1289. DBusMessageIter iter;
  1290. struct wpa_ssid *ssid = NULL;
  1291. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *path = path_buf;
  1292. DBusError error;
  1293. dbus_message_iter_init(message, &iter);
  1294. ssid = wpa_config_add_network(wpa_s->conf);
  1295. if (ssid == NULL) {
  1296. wpa_printf(MSG_ERROR, "wpas_dbus_handler_add_network[dbus]: "
  1297. "can't add new interface.");
  1298. reply = wpas_dbus_error_unknown_error(
  1299. message,
  1300. "wpa_supplicant could not add "
  1301. "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, "wpas_dbus_handler_add_network[dbus]:"
  1310. "control interface couldn't set network "
  1311. "properties");
  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, "Stop ongoing sched_scan to remove "
  1429. "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. "wpas_dbus_handler_remove_network[dbus]: "
  1436. "error occurred when removing network %d", id);
  1437. reply = wpas_dbus_error_unknown_error(
  1438. message, "error removing the specified network on "
  1439. "this 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. "wpas_dbus_handler_remove_all_networks[dbus]: "
  1453. "error occurred when removing network %d",
  1454. ssid->id);
  1455. return;
  1456. }
  1457. if (ssid == wpa_s->current_ssid)
  1458. wpa_supplicant_deauthenticate(wpa_s,
  1459. WLAN_REASON_DEAUTH_LEAVING);
  1460. }
  1461. /**
  1462. * wpas_dbus_handler_remove_all_networks - Remove all configured networks
  1463. * @message: Pointer to incoming dbus message
  1464. * @wpa_s: wpa_supplicant structure for a network interface
  1465. * Returns: NULL on success or dbus error on failure
  1466. *
  1467. * Handler function for "RemoveAllNetworks" method call of a network interface.
  1468. */
  1469. DBusMessage * wpas_dbus_handler_remove_all_networks(
  1470. DBusMessage *message, struct wpa_supplicant *wpa_s)
  1471. {
  1472. if (wpa_s->sched_scanning)
  1473. wpa_supplicant_cancel_sched_scan(wpa_s);
  1474. /* NB: could check for failure and return an error */
  1475. wpa_config_foreach_network(wpa_s->conf, remove_network, wpa_s);
  1476. return NULL;
  1477. }
  1478. /**
  1479. * wpas_dbus_handler_select_network - Attempt association with a network
  1480. * @message: Pointer to incoming dbus message
  1481. * @wpa_s: wpa_supplicant structure for a network interface
  1482. * Returns: NULL on success or dbus error on failure
  1483. *
  1484. * Handler function for "SelectNetwork" method call of network interface.
  1485. */
  1486. DBusMessage * wpas_dbus_handler_select_network(DBusMessage *message,
  1487. struct wpa_supplicant *wpa_s)
  1488. {
  1489. DBusMessage *reply = NULL;
  1490. const char *op;
  1491. char *iface, *net_id;
  1492. int id;
  1493. struct wpa_ssid *ssid;
  1494. dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &op,
  1495. DBUS_TYPE_INVALID);
  1496. /* Extract the network ID and ensure the network */
  1497. /* is actually a child of this interface */
  1498. iface = wpas_dbus_new_decompose_object_path(op,
  1499. WPAS_DBUS_NEW_NETWORKS_PART,
  1500. &net_id);
  1501. if (iface == NULL || net_id == NULL ||
  1502. os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
  1503. reply = wpas_dbus_error_invalid_args(message, op);
  1504. goto out;
  1505. }
  1506. errno = 0;
  1507. id = strtoul(net_id, NULL, 10);
  1508. if (errno != 0) {
  1509. reply = wpas_dbus_error_invalid_args(message, op);
  1510. goto out;
  1511. }
  1512. ssid = wpa_config_get_network(wpa_s->conf, id);
  1513. if (ssid == NULL) {
  1514. reply = wpas_dbus_error_network_unknown(message);
  1515. goto out;
  1516. }
  1517. /* Finally, associate with the network */
  1518. wpa_supplicant_select_network(wpa_s, ssid);
  1519. out:
  1520. os_free(iface);
  1521. return reply;
  1522. }
  1523. /**
  1524. * wpas_dbus_handler_network_reply - Reply to a NetworkRequest signal
  1525. * @message: Pointer to incoming dbus message
  1526. * @wpa_s: wpa_supplicant structure for a network interface
  1527. * Returns: NULL on success or dbus error on failure
  1528. *
  1529. * Handler function for "NetworkReply" method call of network interface.
  1530. */
  1531. DBusMessage * wpas_dbus_handler_network_reply(DBusMessage *message,
  1532. struct wpa_supplicant *wpa_s)
  1533. {
  1534. #ifdef IEEE8021X_EAPOL
  1535. DBusMessage *reply = NULL;
  1536. const char *op, *field, *value;
  1537. char *iface, *net_id;
  1538. int id;
  1539. struct wpa_ssid *ssid;
  1540. if (!dbus_message_get_args(message, NULL,
  1541. DBUS_TYPE_OBJECT_PATH, &op,
  1542. DBUS_TYPE_STRING, &field,
  1543. DBUS_TYPE_STRING, &value,
  1544. DBUS_TYPE_INVALID))
  1545. return wpas_dbus_error_invalid_args(message, NULL);
  1546. /* Extract the network ID and ensure the network */
  1547. /* is actually a child of this interface */
  1548. iface = wpas_dbus_new_decompose_object_path(op,
  1549. WPAS_DBUS_NEW_NETWORKS_PART,
  1550. &net_id);
  1551. if (iface == NULL || net_id == NULL ||
  1552. os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
  1553. reply = wpas_dbus_error_invalid_args(message, op);
  1554. goto out;
  1555. }
  1556. errno = 0;
  1557. id = strtoul(net_id, NULL, 10);
  1558. if (errno != 0) {
  1559. reply = wpas_dbus_error_invalid_args(message, net_id);
  1560. goto out;
  1561. }
  1562. ssid = wpa_config_get_network(wpa_s->conf, id);
  1563. if (ssid == NULL) {
  1564. reply = wpas_dbus_error_network_unknown(message);
  1565. goto out;
  1566. }
  1567. if (wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid,
  1568. field, value) < 0)
  1569. reply = wpas_dbus_error_invalid_args(message, field);
  1570. else {
  1571. /* Tell EAP to retry immediately */
  1572. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  1573. }
  1574. out:
  1575. os_free(iface);
  1576. return reply;
  1577. #else /* IEEE8021X_EAPOL */
  1578. wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
  1579. return wpas_dbus_error_unknown_error(message, "802.1X not included");
  1580. #endif /* IEEE8021X_EAPOL */
  1581. }
  1582. #ifndef CONFIG_NO_CONFIG_BLOBS
  1583. /**
  1584. * wpas_dbus_handler_add_blob - Store named binary blob (ie, for certificates)
  1585. * @message: Pointer to incoming dbus message
  1586. * @wpa_s: %wpa_supplicant data structure
  1587. * Returns: A dbus message containing an error on failure or NULL on success
  1588. *
  1589. * Asks wpa_supplicant to internally store a binary blobs.
  1590. */
  1591. DBusMessage * wpas_dbus_handler_add_blob(DBusMessage *message,
  1592. struct wpa_supplicant *wpa_s)
  1593. {
  1594. DBusMessage *reply = NULL;
  1595. DBusMessageIter iter, array_iter;
  1596. char *blob_name;
  1597. u8 *blob_data;
  1598. int blob_len;
  1599. struct wpa_config_blob *blob = NULL;
  1600. dbus_message_iter_init(message, &iter);
  1601. dbus_message_iter_get_basic(&iter, &blob_name);
  1602. if (wpa_config_get_blob(wpa_s->conf, blob_name)) {
  1603. return dbus_message_new_error(message,
  1604. WPAS_DBUS_ERROR_BLOB_EXISTS,
  1605. NULL);
  1606. }
  1607. dbus_message_iter_next(&iter);
  1608. dbus_message_iter_recurse(&iter, &array_iter);
  1609. dbus_message_iter_get_fixed_array(&array_iter, &blob_data, &blob_len);
  1610. blob = os_zalloc(sizeof(*blob));
  1611. if (!blob) {
  1612. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1613. NULL);
  1614. goto err;
  1615. }
  1616. blob->data = os_malloc(blob_len);
  1617. if (!blob->data) {
  1618. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1619. NULL);
  1620. goto err;
  1621. }
  1622. os_memcpy(blob->data, blob_data, blob_len);
  1623. blob->len = blob_len;
  1624. blob->name = os_strdup(blob_name);
  1625. if (!blob->name) {
  1626. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1627. NULL);
  1628. goto err;
  1629. }
  1630. wpa_config_set_blob(wpa_s->conf, blob);
  1631. wpas_notify_blob_added(wpa_s, blob->name);
  1632. return reply;
  1633. err:
  1634. if (blob) {
  1635. os_free(blob->name);
  1636. os_free(blob->data);
  1637. os_free(blob);
  1638. }
  1639. return reply;
  1640. }
  1641. /**
  1642. * wpas_dbus_handler_get_blob - Get named binary blob (ie, for certificates)
  1643. * @message: Pointer to incoming dbus message
  1644. * @wpa_s: %wpa_supplicant data structure
  1645. * Returns: A dbus message containing array of bytes (blob)
  1646. *
  1647. * Gets one wpa_supplicant's binary blobs.
  1648. */
  1649. DBusMessage * wpas_dbus_handler_get_blob(DBusMessage *message,
  1650. struct wpa_supplicant *wpa_s)
  1651. {
  1652. DBusMessage *reply = NULL;
  1653. DBusMessageIter iter, array_iter;
  1654. char *blob_name;
  1655. const struct wpa_config_blob *blob;
  1656. dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &blob_name,
  1657. DBUS_TYPE_INVALID);
  1658. blob = wpa_config_get_blob(wpa_s->conf, blob_name);
  1659. if (!blob) {
  1660. return dbus_message_new_error(message,
  1661. WPAS_DBUS_ERROR_BLOB_UNKNOWN,
  1662. "Blob id not set");
  1663. }
  1664. reply = dbus_message_new_method_return(message);
  1665. if (!reply) {
  1666. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1667. NULL);
  1668. goto out;
  1669. }
  1670. dbus_message_iter_init_append(reply, &iter);
  1671. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
  1672. DBUS_TYPE_BYTE_AS_STRING,
  1673. &array_iter)) {
  1674. dbus_message_unref(reply);
  1675. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1676. NULL);
  1677. goto out;
  1678. }
  1679. if (!dbus_message_iter_append_fixed_array(&array_iter, DBUS_TYPE_BYTE,
  1680. &(blob->data), blob->len)) {
  1681. dbus_message_unref(reply);
  1682. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1683. NULL);
  1684. goto out;
  1685. }
  1686. if (!dbus_message_iter_close_container(&iter, &array_iter)) {
  1687. dbus_message_unref(reply);
  1688. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1689. NULL);
  1690. goto out;
  1691. }
  1692. out:
  1693. return reply;
  1694. }
  1695. /**
  1696. * wpas_remove_handler_remove_blob - Remove named binary blob
  1697. * @message: Pointer to incoming dbus message
  1698. * @wpa_s: %wpa_supplicant data structure
  1699. * Returns: NULL on success or dbus error
  1700. *
  1701. * Asks wpa_supplicant to internally remove a binary blobs.
  1702. */
  1703. DBusMessage * wpas_dbus_handler_remove_blob(DBusMessage *message,
  1704. struct wpa_supplicant *wpa_s)
  1705. {
  1706. DBusMessage *reply = NULL;
  1707. char *blob_name;
  1708. dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &blob_name,
  1709. DBUS_TYPE_INVALID);
  1710. if (wpa_config_remove_blob(wpa_s->conf, blob_name)) {
  1711. return dbus_message_new_error(message,
  1712. WPAS_DBUS_ERROR_BLOB_UNKNOWN,
  1713. "Blob id not set");
  1714. }
  1715. wpas_notify_blob_removed(wpa_s, blob_name);
  1716. return reply;
  1717. }
  1718. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1719. /*
  1720. * wpas_dbus_handler_flush_bss - Flush the BSS cache
  1721. * @message: Pointer to incoming dbus message
  1722. * @wpa_s: wpa_supplicant structure for a network interface
  1723. * Returns: NULL
  1724. *
  1725. * Handler function for "FlushBSS" method call of network interface.
  1726. */
  1727. DBusMessage * wpas_dbus_handler_flush_bss(DBusMessage *message,
  1728. struct wpa_supplicant *wpa_s)
  1729. {
  1730. dbus_uint32_t age;
  1731. dbus_message_get_args(message, NULL, DBUS_TYPE_UINT32, &age,
  1732. DBUS_TYPE_INVALID);
  1733. if (age == 0)
  1734. wpa_bss_flush(wpa_s);
  1735. else
  1736. wpa_bss_flush_by_age(wpa_s, age);
  1737. return NULL;
  1738. }
  1739. #ifdef CONFIG_AUTOSCAN
  1740. /**
  1741. * wpas_dbus_handler_autoscan - Set autoscan parameters for the interface
  1742. * @message: Pointer to incoming dbus message
  1743. * @wpa_s: wpa_supplicant structure for a network interface
  1744. * Returns: NULL
  1745. *
  1746. * Handler function for "AutoScan" method call of network interface.
  1747. */
  1748. DBusMessage * wpas_dbus_handler_autoscan(DBusMessage *message,
  1749. struct wpa_supplicant *wpa_s)
  1750. {
  1751. DBusMessage *reply = NULL;
  1752. enum wpa_states state = wpa_s->wpa_state;
  1753. char *arg;
  1754. dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg,
  1755. DBUS_TYPE_INVALID);
  1756. if (arg != NULL && os_strlen(arg) > 0) {
  1757. char *tmp;
  1758. tmp = os_strdup(arg);
  1759. if (tmp == NULL) {
  1760. reply = dbus_message_new_error(message,
  1761. DBUS_ERROR_NO_MEMORY,
  1762. NULL);
  1763. } else {
  1764. os_free(wpa_s->conf->autoscan);
  1765. wpa_s->conf->autoscan = tmp;
  1766. if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  1767. autoscan_init(wpa_s, 1);
  1768. else if (state == WPA_SCANNING)
  1769. wpa_supplicant_reinit_autoscan(wpa_s);
  1770. }
  1771. } else if (arg != NULL && os_strlen(arg) == 0) {
  1772. os_free(wpa_s->conf->autoscan);
  1773. wpa_s->conf->autoscan = NULL;
  1774. autoscan_deinit(wpa_s);
  1775. } else
  1776. reply = dbus_message_new_error(message,
  1777. DBUS_ERROR_INVALID_ARGS,
  1778. NULL);
  1779. return reply;
  1780. }
  1781. #endif /* CONFIG_AUTOSCAN */
  1782. /*
  1783. * wpas_dbus_handler_eap_logoff - IEEE 802.1X EAPOL state machine logoff
  1784. * @message: Pointer to incoming dbus message
  1785. * @wpa_s: wpa_supplicant structure for a network interface
  1786. * Returns: NULL
  1787. *
  1788. * Handler function for "EAPLogoff" method call of network interface.
  1789. */
  1790. DBusMessage * wpas_dbus_handler_eap_logoff(DBusMessage *message,
  1791. struct wpa_supplicant *wpa_s)
  1792. {
  1793. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  1794. return NULL;
  1795. }
  1796. /*
  1797. * wpas_dbus_handler_eap_logon - IEEE 802.1X EAPOL state machine logon
  1798. * @message: Pointer to incoming dbus message
  1799. * @wpa_s: wpa_supplicant structure for a network interface
  1800. * Returns: NULL
  1801. *
  1802. * Handler function for "EAPLogin" method call of network interface.
  1803. */
  1804. DBusMessage * wpas_dbus_handler_eap_logon(DBusMessage *message,
  1805. struct wpa_supplicant *wpa_s)
  1806. {
  1807. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  1808. return NULL;
  1809. }
  1810. #ifdef CONFIG_TDLS
  1811. static int get_peer_hwaddr_helper(DBusMessage *message, const char *func_name,
  1812. u8 *peer_address, DBusMessage **error)
  1813. {
  1814. const char *peer_string;
  1815. *error = NULL;
  1816. if (!dbus_message_get_args(message, NULL,
  1817. DBUS_TYPE_STRING, &peer_string,
  1818. DBUS_TYPE_INVALID)) {
  1819. *error = wpas_dbus_error_invalid_args(message, NULL);
  1820. return -1;
  1821. }
  1822. if (hwaddr_aton(peer_string, peer_address)) {
  1823. wpa_printf(MSG_DEBUG, "%s: invalid address '%s'",
  1824. func_name, peer_string);
  1825. *error = wpas_dbus_error_invalid_args(
  1826. message, "Invalid hardware address format");
  1827. return -1;
  1828. }
  1829. return 0;
  1830. }
  1831. /*
  1832. * wpas_dbus_handler_tdls_discover - Discover TDLS peer
  1833. * @message: Pointer to incoming dbus message
  1834. * @wpa_s: wpa_supplicant structure for a network interface
  1835. * Returns: NULL indicating success or DBus error message on failure
  1836. *
  1837. * Handler function for "TDLSDiscover" method call of network interface.
  1838. */
  1839. DBusMessage * wpas_dbus_handler_tdls_discover(DBusMessage *message,
  1840. struct wpa_supplicant *wpa_s)
  1841. {
  1842. u8 peer[ETH_ALEN];
  1843. DBusMessage *error_reply;
  1844. int ret;
  1845. if (get_peer_hwaddr_helper(message, __func__, peer, &error_reply) < 0)
  1846. return error_reply;
  1847. wpa_printf(MSG_DEBUG, "DBUS TDLS_DISCOVER " MACSTR, MAC2STR(peer));
  1848. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  1849. ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
  1850. else
  1851. ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
  1852. if (ret) {
  1853. return wpas_dbus_error_unknown_error(
  1854. message, "error performing TDLS discovery");
  1855. }
  1856. return NULL;
  1857. }
  1858. /*
  1859. * wpas_dbus_handler_tdls_setup - Setup TDLS session
  1860. * @message: Pointer to incoming dbus message
  1861. * @wpa_s: wpa_supplicant structure for a network interface
  1862. * Returns: NULL indicating success or DBus error message on failure
  1863. *
  1864. * Handler function for "TDLSSetup" method call of network interface.
  1865. */
  1866. DBusMessage * wpas_dbus_handler_tdls_setup(DBusMessage *message,
  1867. struct wpa_supplicant *wpa_s)
  1868. {
  1869. u8 peer[ETH_ALEN];
  1870. DBusMessage *error_reply;
  1871. int ret;
  1872. if (get_peer_hwaddr_helper(message, __func__, peer, &error_reply) < 0)
  1873. return error_reply;
  1874. wpa_printf(MSG_DEBUG, "DBUS TDLS_SETUP " MACSTR, MAC2STR(peer));
  1875. wpa_tdls_remove(wpa_s->wpa, peer);
  1876. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  1877. ret = wpa_tdls_start(wpa_s->wpa, peer);
  1878. else
  1879. ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  1880. if (ret) {
  1881. return wpas_dbus_error_unknown_error(
  1882. message, "error performing TDLS setup");
  1883. }
  1884. return NULL;
  1885. }
  1886. /*
  1887. * wpas_dbus_handler_tdls_status - Return TDLS session status
  1888. * @message: Pointer to incoming dbus message
  1889. * @wpa_s: wpa_supplicant structure for a network interface
  1890. * Returns: A string representing the state of the link to this TDLS peer
  1891. *
  1892. * Handler function for "TDLSStatus" method call of network interface.
  1893. */
  1894. DBusMessage * wpas_dbus_handler_tdls_status(DBusMessage *message,
  1895. struct wpa_supplicant *wpa_s)
  1896. {
  1897. u8 peer[ETH_ALEN];
  1898. DBusMessage *reply;
  1899. const char *tdls_status;
  1900. if (get_peer_hwaddr_helper(message, __func__, peer, &reply) < 0)
  1901. return reply;
  1902. wpa_printf(MSG_DEBUG, "DBUS TDLS_STATUS " MACSTR, MAC2STR(peer));
  1903. tdls_status = wpa_tdls_get_link_status(wpa_s->wpa, peer);
  1904. reply = dbus_message_new_method_return(message);
  1905. dbus_message_append_args(reply, DBUS_TYPE_STRING,
  1906. &tdls_status, DBUS_TYPE_INVALID);
  1907. return reply;
  1908. }
  1909. /*
  1910. * wpas_dbus_handler_tdls_teardown - Teardown TDLS session
  1911. * @message: Pointer to incoming dbus message
  1912. * @wpa_s: wpa_supplicant structure for a network interface
  1913. * Returns: NULL indicating success or DBus error message on failure
  1914. *
  1915. * Handler function for "TDLSTeardown" method call of network interface.
  1916. */
  1917. DBusMessage * wpas_dbus_handler_tdls_teardown(DBusMessage *message,
  1918. struct wpa_supplicant *wpa_s)
  1919. {
  1920. u8 peer[ETH_ALEN];
  1921. DBusMessage *error_reply;
  1922. int ret;
  1923. if (get_peer_hwaddr_helper(message, __func__, peer, &error_reply) < 0)
  1924. return error_reply;
  1925. wpa_printf(MSG_DEBUG, "DBUS TDLS_TEARDOWN " MACSTR, MAC2STR(peer));
  1926. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  1927. ret = wpa_tdls_teardown_link(
  1928. wpa_s->wpa, peer,
  1929. WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
  1930. else
  1931. ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  1932. if (ret) {
  1933. return wpas_dbus_error_unknown_error(
  1934. message, "error performing TDLS teardown");
  1935. }
  1936. return NULL;
  1937. }
  1938. #endif /* CONFIG_TDLS */
  1939. /**
  1940. * wpas_dbus_handler_set_pkcs11_engine_and_module_path - Set PKCS #11 engine and module path
  1941. * @message: Pointer to incoming dbus message
  1942. * @wpa_s: %wpa_supplicant data structure
  1943. * Returns: A dbus message containing an error on failure or NULL on success
  1944. *
  1945. * Sets the PKCS #11 engine and module path.
  1946. */
  1947. DBusMessage * wpas_dbus_handler_set_pkcs11_engine_and_module_path(
  1948. DBusMessage *message, struct wpa_supplicant *wpa_s)
  1949. {
  1950. DBusMessageIter iter;
  1951. char *value = NULL;
  1952. char *pkcs11_engine_path = NULL;
  1953. char *pkcs11_module_path = NULL;
  1954. dbus_message_iter_init(message, &iter);
  1955. dbus_message_iter_get_basic(&iter, &value);
  1956. if (value == NULL) {
  1957. return dbus_message_new_error(
  1958. message, DBUS_ERROR_INVALID_ARGS,
  1959. "Invalid pkcs11_engine_path argument");
  1960. }
  1961. /* Empty path defaults to NULL */
  1962. if (os_strlen(value))
  1963. pkcs11_engine_path = value;
  1964. dbus_message_iter_next(&iter);
  1965. dbus_message_iter_get_basic(&iter, &value);
  1966. if (value == NULL) {
  1967. os_free(pkcs11_engine_path);
  1968. return dbus_message_new_error(
  1969. message, DBUS_ERROR_INVALID_ARGS,
  1970. "Invalid pkcs11_module_path argument");
  1971. }
  1972. /* Empty path defaults to NULL */
  1973. if (os_strlen(value))
  1974. pkcs11_module_path = value;
  1975. if (wpas_set_pkcs11_engine_and_module_path(wpa_s, pkcs11_engine_path,
  1976. pkcs11_module_path))
  1977. return dbus_message_new_error(
  1978. message, DBUS_ERROR_FAILED,
  1979. "Reinit of the EAPOL state machine with the new PKCS "
  1980. "#11 engine and module path failed.");
  1981. wpa_dbus_mark_property_changed(
  1982. wpa_s->global->dbus, wpa_s->dbus_new_path,
  1983. WPAS_DBUS_NEW_IFACE_INTERFACE, "PKCS11EnginePath");
  1984. wpa_dbus_mark_property_changed(
  1985. wpa_s->global->dbus, wpa_s->dbus_new_path,
  1986. WPAS_DBUS_NEW_IFACE_INTERFACE, "PKCS11ModulePath");
  1987. return NULL;
  1988. }
  1989. /**
  1990. * wpas_dbus_getter_capabilities - Return interface capabilities
  1991. * @iter: Pointer to incoming dbus message iter
  1992. * @error: Location to store error on failure
  1993. * @user_data: Function specific data
  1994. * Returns: TRUE on success, FALSE on failure
  1995. *
  1996. * Getter for "Capabilities" property of an interface.
  1997. */
  1998. dbus_bool_t wpas_dbus_getter_capabilities(DBusMessageIter *iter,
  1999. DBusError *error, void *user_data)
  2000. {
  2001. struct wpa_supplicant *wpa_s = user_data;
  2002. struct wpa_driver_capa capa;
  2003. int res;
  2004. DBusMessageIter iter_dict, iter_dict_entry, iter_dict_val, iter_array,
  2005. variant_iter;
  2006. const char *scans[] = { "active", "passive", "ssid" };
  2007. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  2008. "a{sv}", &variant_iter) ||
  2009. !wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
  2010. goto nomem;
  2011. res = wpa_drv_get_capa(wpa_s, &capa);
  2012. /***** pairwise cipher */
  2013. if (res < 0) {
  2014. const char *args[] = {"ccmp", "tkip", "none"};
  2015. if (!wpa_dbus_dict_append_string_array(
  2016. &iter_dict, "Pairwise", args,
  2017. ARRAY_SIZE(args)))
  2018. goto nomem;
  2019. } else {
  2020. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Pairwise",
  2021. &iter_dict_entry,
  2022. &iter_dict_val,
  2023. &iter_array) ||
  2024. ((capa.enc & WPA_DRIVER_CAPA_ENC_CCMP_256) &&
  2025. !wpa_dbus_dict_string_array_add_element(
  2026. &iter_array, "ccmp-256")) ||
  2027. ((capa.enc & WPA_DRIVER_CAPA_ENC_GCMP_256) &&
  2028. !wpa_dbus_dict_string_array_add_element(
  2029. &iter_array, "gcmp-256")) ||
  2030. ((capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) &&
  2031. !wpa_dbus_dict_string_array_add_element(
  2032. &iter_array, "ccmp")) ||
  2033. ((capa.enc & WPA_DRIVER_CAPA_ENC_GCMP) &&
  2034. !wpa_dbus_dict_string_array_add_element(
  2035. &iter_array, "gcmp")) ||
  2036. ((capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) &&
  2037. !wpa_dbus_dict_string_array_add_element(
  2038. &iter_array, "tkip")) ||
  2039. ((capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) &&
  2040. !wpa_dbus_dict_string_array_add_element(
  2041. &iter_array, "none")) ||
  2042. !wpa_dbus_dict_end_string_array(&iter_dict,
  2043. &iter_dict_entry,
  2044. &iter_dict_val,
  2045. &iter_array))
  2046. goto nomem;
  2047. }
  2048. /***** group cipher */
  2049. if (res < 0) {
  2050. const char *args[] = {
  2051. "ccmp", "tkip", "wep104", "wep40"
  2052. };
  2053. if (!wpa_dbus_dict_append_string_array(
  2054. &iter_dict, "Group", args,
  2055. ARRAY_SIZE(args)))
  2056. goto nomem;
  2057. } else {
  2058. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Group",
  2059. &iter_dict_entry,
  2060. &iter_dict_val,
  2061. &iter_array) ||
  2062. ((capa.enc & WPA_DRIVER_CAPA_ENC_CCMP_256) &&
  2063. !wpa_dbus_dict_string_array_add_element(
  2064. &iter_array, "ccmp-256")) ||
  2065. ((capa.enc & WPA_DRIVER_CAPA_ENC_GCMP_256) &&
  2066. !wpa_dbus_dict_string_array_add_element(
  2067. &iter_array, "gcmp-256")) ||
  2068. ((capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) &&
  2069. !wpa_dbus_dict_string_array_add_element(
  2070. &iter_array, "ccmp")) ||
  2071. ((capa.enc & WPA_DRIVER_CAPA_ENC_GCMP) &&
  2072. !wpa_dbus_dict_string_array_add_element(
  2073. &iter_array, "gcmp")) ||
  2074. ((capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) &&
  2075. !wpa_dbus_dict_string_array_add_element(
  2076. &iter_array, "tkip")) ||
  2077. ((capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) &&
  2078. !wpa_dbus_dict_string_array_add_element(
  2079. &iter_array, "wep104")) ||
  2080. ((capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) &&
  2081. !wpa_dbus_dict_string_array_add_element(
  2082. &iter_array, "wep40")) ||
  2083. !wpa_dbus_dict_end_string_array(&iter_dict,
  2084. &iter_dict_entry,
  2085. &iter_dict_val,
  2086. &iter_array))
  2087. goto nomem;
  2088. }
  2089. /***** key management */
  2090. if (res < 0) {
  2091. const char *args[] = {
  2092. "wpa-psk", "wpa-eap", "ieee8021x", "wpa-none",
  2093. #ifdef CONFIG_WPS
  2094. "wps",
  2095. #endif /* CONFIG_WPS */
  2096. "none"
  2097. };
  2098. if (!wpa_dbus_dict_append_string_array(
  2099. &iter_dict, "KeyMgmt", args,
  2100. ARRAY_SIZE(args)))
  2101. goto nomem;
  2102. } else {
  2103. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "KeyMgmt",
  2104. &iter_dict_entry,
  2105. &iter_dict_val,
  2106. &iter_array) ||
  2107. !wpa_dbus_dict_string_array_add_element(&iter_array,
  2108. "none") ||
  2109. !wpa_dbus_dict_string_array_add_element(&iter_array,
  2110. "ieee8021x"))
  2111. goto nomem;
  2112. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2113. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  2114. if (!wpa_dbus_dict_string_array_add_element(
  2115. &iter_array, "wpa-eap") ||
  2116. ((capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT) &&
  2117. !wpa_dbus_dict_string_array_add_element(
  2118. &iter_array, "wpa-ft-eap")))
  2119. goto nomem;
  2120. /* TODO: Ensure that driver actually supports sha256 encryption. */
  2121. #ifdef CONFIG_IEEE80211W
  2122. if (!wpa_dbus_dict_string_array_add_element(
  2123. &iter_array, "wpa-eap-sha256"))
  2124. goto nomem;
  2125. #endif /* CONFIG_IEEE80211W */
  2126. }
  2127. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  2128. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2129. if (!wpa_dbus_dict_string_array_add_element(
  2130. &iter_array, "wpa-psk") ||
  2131. ((capa.key_mgmt &
  2132. WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK) &&
  2133. !wpa_dbus_dict_string_array_add_element(
  2134. &iter_array, "wpa-ft-psk")))
  2135. goto nomem;
  2136. /* TODO: Ensure that driver actually supports sha256 encryption. */
  2137. #ifdef CONFIG_IEEE80211W
  2138. if (!wpa_dbus_dict_string_array_add_element(
  2139. &iter_array, "wpa-psk-sha256"))
  2140. goto nomem;
  2141. #endif /* CONFIG_IEEE80211W */
  2142. }
  2143. if ((capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) &&
  2144. !wpa_dbus_dict_string_array_add_element(&iter_array,
  2145. "wpa-none"))
  2146. goto nomem;
  2147. #ifdef CONFIG_WPS
  2148. if (!wpa_dbus_dict_string_array_add_element(&iter_array,
  2149. "wps"))
  2150. goto nomem;
  2151. #endif /* CONFIG_WPS */
  2152. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  2153. &iter_dict_entry,
  2154. &iter_dict_val,
  2155. &iter_array))
  2156. goto nomem;
  2157. }
  2158. /***** WPA protocol */
  2159. if (res < 0) {
  2160. const char *args[] = { "rsn", "wpa" };
  2161. if (!wpa_dbus_dict_append_string_array(
  2162. &iter_dict, "Protocol", args,
  2163. ARRAY_SIZE(args)))
  2164. goto nomem;
  2165. } else {
  2166. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Protocol",
  2167. &iter_dict_entry,
  2168. &iter_dict_val,
  2169. &iter_array) ||
  2170. ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  2171. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) &&
  2172. !wpa_dbus_dict_string_array_add_element(
  2173. &iter_array, "rsn")) ||
  2174. ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2175. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) &&
  2176. !wpa_dbus_dict_string_array_add_element(
  2177. &iter_array, "wpa")) ||
  2178. !wpa_dbus_dict_end_string_array(&iter_dict,
  2179. &iter_dict_entry,
  2180. &iter_dict_val,
  2181. &iter_array))
  2182. goto nomem;
  2183. }
  2184. /***** auth alg */
  2185. if (res < 0) {
  2186. const char *args[] = { "open", "shared", "leap" };
  2187. if (!wpa_dbus_dict_append_string_array(
  2188. &iter_dict, "AuthAlg", args,
  2189. ARRAY_SIZE(args)))
  2190. goto nomem;
  2191. } else {
  2192. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "AuthAlg",
  2193. &iter_dict_entry,
  2194. &iter_dict_val,
  2195. &iter_array))
  2196. goto nomem;
  2197. if (((capa.auth & WPA_DRIVER_AUTH_OPEN) &&
  2198. !wpa_dbus_dict_string_array_add_element(
  2199. &iter_array, "open")) ||
  2200. ((capa.auth & WPA_DRIVER_AUTH_SHARED) &&
  2201. !wpa_dbus_dict_string_array_add_element(
  2202. &iter_array, "shared")) ||
  2203. ((capa.auth & WPA_DRIVER_AUTH_LEAP) &&
  2204. !wpa_dbus_dict_string_array_add_element(
  2205. &iter_array, "leap")) ||
  2206. !wpa_dbus_dict_end_string_array(&iter_dict,
  2207. &iter_dict_entry,
  2208. &iter_dict_val,
  2209. &iter_array))
  2210. goto nomem;
  2211. }
  2212. /***** Scan */
  2213. if (!wpa_dbus_dict_append_string_array(&iter_dict, "Scan", scans,
  2214. ARRAY_SIZE(scans)))
  2215. goto nomem;
  2216. /***** Modes */
  2217. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Modes",
  2218. &iter_dict_entry,
  2219. &iter_dict_val,
  2220. &iter_array) ||
  2221. !wpa_dbus_dict_string_array_add_element(
  2222. &iter_array, "infrastructure") ||
  2223. !wpa_dbus_dict_string_array_add_element(
  2224. &iter_array, "ad-hoc") ||
  2225. (res >= 0 && (capa.flags & WPA_DRIVER_FLAGS_AP) &&
  2226. !wpa_dbus_dict_string_array_add_element(
  2227. &iter_array, "ap")) ||
  2228. (res >= 0 && (capa.flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
  2229. !wpa_dbus_dict_string_array_add_element(
  2230. &iter_array, "p2p")) ||
  2231. !wpa_dbus_dict_end_string_array(&iter_dict,
  2232. &iter_dict_entry,
  2233. &iter_dict_val,
  2234. &iter_array))
  2235. goto nomem;
  2236. /***** Modes end */
  2237. if (res >= 0) {
  2238. dbus_int32_t max_scan_ssid = capa.max_scan_ssids;
  2239. if (!wpa_dbus_dict_append_int32(&iter_dict, "MaxScanSSID",
  2240. max_scan_ssid))
  2241. goto nomem;
  2242. }
  2243. if (!wpa_dbus_dict_close_write(&variant_iter, &iter_dict) ||
  2244. !dbus_message_iter_close_container(iter, &variant_iter))
  2245. goto nomem;
  2246. return TRUE;
  2247. nomem:
  2248. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2249. return FALSE;
  2250. }
  2251. /**
  2252. * wpas_dbus_getter_state - Get interface state
  2253. * @iter: Pointer to incoming dbus message iter
  2254. * @error: Location to store error on failure
  2255. * @user_data: Function specific data
  2256. * Returns: TRUE on success, FALSE on failure
  2257. *
  2258. * Getter for "State" property.
  2259. */
  2260. dbus_bool_t wpas_dbus_getter_state(DBusMessageIter *iter, DBusError *error,
  2261. void *user_data)
  2262. {
  2263. struct wpa_supplicant *wpa_s = user_data;
  2264. const char *str_state;
  2265. char *state_ls, *tmp;
  2266. dbus_bool_t success = FALSE;
  2267. str_state = wpa_supplicant_state_txt(wpa_s->wpa_state);
  2268. /* make state string lowercase to fit new DBus API convention
  2269. */
  2270. state_ls = tmp = os_strdup(str_state);
  2271. if (!tmp) {
  2272. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2273. return FALSE;
  2274. }
  2275. while (*tmp) {
  2276. *tmp = tolower(*tmp);
  2277. tmp++;
  2278. }
  2279. success = wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2280. &state_ls, error);
  2281. os_free(state_ls);
  2282. return success;
  2283. }
  2284. /**
  2285. * wpas_dbus_new_iface_get_scanning - Get interface scanning state
  2286. * @iter: Pointer to incoming dbus message iter
  2287. * @error: Location to store error on failure
  2288. * @user_data: Function specific data
  2289. * Returns: TRUE on success, FALSE on failure
  2290. *
  2291. * Getter for "scanning" property.
  2292. */
  2293. dbus_bool_t wpas_dbus_getter_scanning(DBusMessageIter *iter, DBusError *error,
  2294. void *user_data)
  2295. {
  2296. struct wpa_supplicant *wpa_s = user_data;
  2297. dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
  2298. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  2299. &scanning, error);
  2300. }
  2301. /**
  2302. * wpas_dbus_getter_ap_scan - Control roaming mode
  2303. * @iter: Pointer to incoming dbus message iter
  2304. * @error: Location to store error on failure
  2305. * @user_data: Function specific data
  2306. * Returns: TRUE on success, FALSE on failure
  2307. *
  2308. * Getter function for "ApScan" property.
  2309. */
  2310. dbus_bool_t wpas_dbus_getter_ap_scan(DBusMessageIter *iter, DBusError *error,
  2311. void *user_data)
  2312. {
  2313. struct wpa_supplicant *wpa_s = user_data;
  2314. dbus_uint32_t ap_scan = wpa_s->conf->ap_scan;
  2315. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT32,
  2316. &ap_scan, error);
  2317. }
  2318. /**
  2319. * wpas_dbus_setter_ap_scan - Control roaming mode
  2320. * @iter: Pointer to incoming dbus message iter
  2321. * @error: Location to store error on failure
  2322. * @user_data: Function specific data
  2323. * Returns: TRUE on success, FALSE on failure
  2324. *
  2325. * Setter function for "ApScan" property.
  2326. */
  2327. dbus_bool_t wpas_dbus_setter_ap_scan(DBusMessageIter *iter, DBusError *error,
  2328. void *user_data)
  2329. {
  2330. struct wpa_supplicant *wpa_s = user_data;
  2331. dbus_uint32_t ap_scan;
  2332. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_UINT32,
  2333. &ap_scan))
  2334. return FALSE;
  2335. if (wpa_supplicant_set_ap_scan(wpa_s, ap_scan)) {
  2336. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2337. "ap_scan must be 0, 1, or 2");
  2338. return FALSE;
  2339. }
  2340. return TRUE;
  2341. }
  2342. /**
  2343. * wpas_dbus_getter_fast_reauth - Control fast
  2344. * reauthentication (TLS session resumption)
  2345. * @iter: Pointer to incoming dbus message iter
  2346. * @error: Location to store error on failure
  2347. * @user_data: Function specific data
  2348. * Returns: TRUE on success, FALSE on failure
  2349. *
  2350. * Getter function for "FastReauth" property.
  2351. */
  2352. dbus_bool_t wpas_dbus_getter_fast_reauth(DBusMessageIter *iter,
  2353. DBusError *error,
  2354. void *user_data)
  2355. {
  2356. struct wpa_supplicant *wpa_s = user_data;
  2357. dbus_bool_t fast_reauth = wpa_s->conf->fast_reauth ? TRUE : FALSE;
  2358. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  2359. &fast_reauth, error);
  2360. }
  2361. /**
  2362. * wpas_dbus_setter_fast_reauth - Control fast
  2363. * reauthentication (TLS session resumption)
  2364. * @iter: Pointer to incoming dbus message iter
  2365. * @error: Location to store error on failure
  2366. * @user_data: Function specific data
  2367. * Returns: TRUE on success, FALSE on failure
  2368. *
  2369. * Setter function for "FastReauth" property.
  2370. */
  2371. dbus_bool_t wpas_dbus_setter_fast_reauth(DBusMessageIter *iter,
  2372. DBusError *error,
  2373. void *user_data)
  2374. {
  2375. struct wpa_supplicant *wpa_s = user_data;
  2376. dbus_bool_t fast_reauth;
  2377. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_BOOLEAN,
  2378. &fast_reauth))
  2379. return FALSE;
  2380. wpa_s->conf->fast_reauth = fast_reauth;
  2381. return TRUE;
  2382. }
  2383. /**
  2384. * wpas_dbus_getter_disconnect_reason - Get most recent reason for disconnect
  2385. * @iter: Pointer to incoming dbus message iter
  2386. * @error: Location to store error on failure
  2387. * @user_data: Function specific data
  2388. * Returns: TRUE on success, FALSE on failure
  2389. *
  2390. * Getter for "DisconnectReason" property. The reason is negative if it is
  2391. * locally generated.
  2392. */
  2393. dbus_bool_t wpas_dbus_getter_disconnect_reason(DBusMessageIter *iter,
  2394. DBusError *error,
  2395. void *user_data)
  2396. {
  2397. struct wpa_supplicant *wpa_s = user_data;
  2398. dbus_int32_t reason = wpa_s->disconnect_reason;
  2399. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_INT32,
  2400. &reason, error);
  2401. }
  2402. /**
  2403. * wpas_dbus_getter_bss_expire_age - Get BSS entry expiration age
  2404. * @iter: Pointer to incoming dbus message iter
  2405. * @error: Location to store error on failure
  2406. * @user_data: Function specific data
  2407. * Returns: TRUE on success, FALSE on failure
  2408. *
  2409. * Getter function for "BSSExpireAge" property.
  2410. */
  2411. dbus_bool_t wpas_dbus_getter_bss_expire_age(DBusMessageIter *iter,
  2412. DBusError *error,
  2413. void *user_data)
  2414. {
  2415. struct wpa_supplicant *wpa_s = user_data;
  2416. dbus_uint32_t expire_age = wpa_s->conf->bss_expiration_age;
  2417. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT32,
  2418. &expire_age, error);
  2419. }
  2420. /**
  2421. * wpas_dbus_setter_bss_expire_age - Control BSS entry expiration age
  2422. * @iter: Pointer to incoming dbus message iter
  2423. * @error: Location to store error on failure
  2424. * @user_data: Function specific data
  2425. * Returns: TRUE on success, FALSE on failure
  2426. *
  2427. * Setter function for "BSSExpireAge" property.
  2428. */
  2429. dbus_bool_t wpas_dbus_setter_bss_expire_age(DBusMessageIter *iter,
  2430. DBusError *error,
  2431. void *user_data)
  2432. {
  2433. struct wpa_supplicant *wpa_s = user_data;
  2434. dbus_uint32_t expire_age;
  2435. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_UINT32,
  2436. &expire_age))
  2437. return FALSE;
  2438. if (wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age)) {
  2439. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2440. "BSSExpireAge must be >= 10");
  2441. return FALSE;
  2442. }
  2443. return TRUE;
  2444. }
  2445. /**
  2446. * wpas_dbus_getter_bss_expire_count - Get BSS entry expiration scan count
  2447. * @iter: Pointer to incoming dbus message iter
  2448. * @error: Location to store error on failure
  2449. * @user_data: Function specific data
  2450. * Returns: TRUE on success, FALSE on failure
  2451. *
  2452. * Getter function for "BSSExpireCount" property.
  2453. */
  2454. dbus_bool_t wpas_dbus_getter_bss_expire_count(DBusMessageIter *iter,
  2455. DBusError *error,
  2456. void *user_data)
  2457. {
  2458. struct wpa_supplicant *wpa_s = user_data;
  2459. dbus_uint32_t expire_count = wpa_s->conf->bss_expiration_scan_count;
  2460. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT32,
  2461. &expire_count, error);
  2462. }
  2463. /**
  2464. * wpas_dbus_setter_bss_expire_count - Control BSS entry expiration scan count
  2465. * @iter: Pointer to incoming dbus message iter
  2466. * @error: Location to store error on failure
  2467. * @user_data: Function specific data
  2468. * Returns: TRUE on success, FALSE on failure
  2469. *
  2470. * Setter function for "BSSExpireCount" property.
  2471. */
  2472. dbus_bool_t wpas_dbus_setter_bss_expire_count(DBusMessageIter *iter,
  2473. DBusError *error,
  2474. void *user_data)
  2475. {
  2476. struct wpa_supplicant *wpa_s = user_data;
  2477. dbus_uint32_t expire_count;
  2478. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_UINT32,
  2479. &expire_count))
  2480. return FALSE;
  2481. if (wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count)) {
  2482. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2483. "BSSExpireCount must be > 0");
  2484. return FALSE;
  2485. }
  2486. return TRUE;
  2487. }
  2488. /**
  2489. * wpas_dbus_getter_country - Control country code
  2490. * @iter: Pointer to incoming dbus message iter
  2491. * @error: Location to store error on failure
  2492. * @user_data: Function specific data
  2493. * Returns: TRUE on success, FALSE on failure
  2494. *
  2495. * Getter function for "Country" property.
  2496. */
  2497. dbus_bool_t wpas_dbus_getter_country(DBusMessageIter *iter, DBusError *error,
  2498. void *user_data)
  2499. {
  2500. struct wpa_supplicant *wpa_s = user_data;
  2501. char country[3];
  2502. char *str = country;
  2503. country[0] = wpa_s->conf->country[0];
  2504. country[1] = wpa_s->conf->country[1];
  2505. country[2] = '\0';
  2506. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2507. &str, error);
  2508. }
  2509. /**
  2510. * wpas_dbus_setter_country - Control country code
  2511. * @iter: Pointer to incoming dbus message iter
  2512. * @error: Location to store error on failure
  2513. * @user_data: Function specific data
  2514. * Returns: TRUE on success, FALSE on failure
  2515. *
  2516. * Setter function for "Country" property.
  2517. */
  2518. dbus_bool_t wpas_dbus_setter_country(DBusMessageIter *iter, DBusError *error,
  2519. void *user_data)
  2520. {
  2521. struct wpa_supplicant *wpa_s = user_data;
  2522. const char *country;
  2523. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_STRING,
  2524. &country))
  2525. return FALSE;
  2526. if (!country[0] || !country[1]) {
  2527. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2528. "invalid country code");
  2529. return FALSE;
  2530. }
  2531. if (wpa_s->drv_priv != NULL && wpa_drv_set_country(wpa_s, country)) {
  2532. wpa_printf(MSG_DEBUG, "Failed to set country");
  2533. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2534. "failed to set country code");
  2535. return FALSE;
  2536. }
  2537. wpa_s->conf->country[0] = country[0];
  2538. wpa_s->conf->country[1] = country[1];
  2539. return TRUE;
  2540. }
  2541. /**
  2542. * wpas_dbus_getter_scan_interval - Get scan interval
  2543. * @iter: Pointer to incoming dbus message iter
  2544. * @error: Location to store error on failure
  2545. * @user_data: Function specific data
  2546. * Returns: TRUE on success, FALSE on failure
  2547. *
  2548. * Getter function for "ScanInterval" property.
  2549. */
  2550. dbus_bool_t wpas_dbus_getter_scan_interval(DBusMessageIter *iter,
  2551. DBusError *error,
  2552. void *user_data)
  2553. {
  2554. struct wpa_supplicant *wpa_s = user_data;
  2555. dbus_int32_t scan_interval = wpa_s->scan_interval;
  2556. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_INT32,
  2557. &scan_interval, error);
  2558. }
  2559. /**
  2560. * wpas_dbus_setter_scan_interval - Control scan interval
  2561. * @iter: Pointer to incoming dbus message iter
  2562. * @error: Location to store error on failure
  2563. * @user_data: Function specific data
  2564. * Returns: TRUE on success, FALSE on failure
  2565. *
  2566. * Setter function for "ScanInterval" property.
  2567. */
  2568. dbus_bool_t wpas_dbus_setter_scan_interval(DBusMessageIter *iter,
  2569. DBusError *error,
  2570. void *user_data)
  2571. {
  2572. struct wpa_supplicant *wpa_s = user_data;
  2573. dbus_int32_t scan_interval;
  2574. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_INT32,
  2575. &scan_interval))
  2576. return FALSE;
  2577. if (wpa_supplicant_set_scan_interval(wpa_s, scan_interval)) {
  2578. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  2579. "scan_interval must be >= 0");
  2580. return FALSE;
  2581. }
  2582. return TRUE;
  2583. }
  2584. /**
  2585. * wpas_dbus_getter_ifname - Get interface name
  2586. * @iter: Pointer to incoming dbus message iter
  2587. * @error: Location to store error on failure
  2588. * @user_data: Function specific data
  2589. * Returns: TRUE on success, FALSE on failure
  2590. *
  2591. * Getter for "Ifname" property.
  2592. */
  2593. dbus_bool_t wpas_dbus_getter_ifname(DBusMessageIter *iter, DBusError *error,
  2594. void *user_data)
  2595. {
  2596. struct wpa_supplicant *wpa_s = user_data;
  2597. const char *ifname = wpa_s->ifname;
  2598. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2599. &ifname, error);
  2600. }
  2601. /**
  2602. * wpas_dbus_getter_driver - Get interface name
  2603. * @iter: Pointer to incoming dbus message iter
  2604. * @error: Location to store error on failure
  2605. * @user_data: Function specific data
  2606. * Returns: TRUE on success, FALSE on failure
  2607. *
  2608. * Getter for "Driver" property.
  2609. */
  2610. dbus_bool_t wpas_dbus_getter_driver(DBusMessageIter *iter, DBusError *error,
  2611. void *user_data)
  2612. {
  2613. struct wpa_supplicant *wpa_s = user_data;
  2614. const char *driver;
  2615. if (wpa_s->driver == NULL || wpa_s->driver->name == NULL) {
  2616. wpa_printf(MSG_DEBUG, "wpas_dbus_getter_driver[dbus]: "
  2617. "wpa_s has no driver set");
  2618. dbus_set_error(error, DBUS_ERROR_FAILED, "%s: no driver set",
  2619. __func__);
  2620. return FALSE;
  2621. }
  2622. driver = wpa_s->driver->name;
  2623. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2624. &driver, error);
  2625. }
  2626. /**
  2627. * wpas_dbus_getter_current_bss - Get current bss object path
  2628. * @iter: Pointer to incoming dbus message iter
  2629. * @error: Location to store error on failure
  2630. * @user_data: Function specific data
  2631. * Returns: TRUE on success, FALSE on failure
  2632. *
  2633. * Getter for "CurrentBSS" property.
  2634. */
  2635. dbus_bool_t wpas_dbus_getter_current_bss(DBusMessageIter *iter,
  2636. DBusError *error,
  2637. void *user_data)
  2638. {
  2639. struct wpa_supplicant *wpa_s = user_data;
  2640. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *bss_obj_path = path_buf;
  2641. if (wpa_s->current_bss)
  2642. os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  2643. "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
  2644. wpa_s->dbus_new_path, wpa_s->current_bss->id);
  2645. else
  2646. os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX, "/");
  2647. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_OBJECT_PATH,
  2648. &bss_obj_path, error);
  2649. }
  2650. /**
  2651. * wpas_dbus_getter_current_network - Get current network object path
  2652. * @iter: Pointer to incoming dbus message iter
  2653. * @error: Location to store error on failure
  2654. * @user_data: Function specific data
  2655. * Returns: TRUE on success, FALSE on failure
  2656. *
  2657. * Getter for "CurrentNetwork" property.
  2658. */
  2659. dbus_bool_t wpas_dbus_getter_current_network(DBusMessageIter *iter,
  2660. DBusError *error,
  2661. void *user_data)
  2662. {
  2663. struct wpa_supplicant *wpa_s = user_data;
  2664. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *net_obj_path = path_buf;
  2665. if (wpa_s->current_ssid)
  2666. os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  2667. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%u",
  2668. wpa_s->dbus_new_path, wpa_s->current_ssid->id);
  2669. else
  2670. os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX, "/");
  2671. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_OBJECT_PATH,
  2672. &net_obj_path, error);
  2673. }
  2674. /**
  2675. * wpas_dbus_getter_current_auth_mode - Get current authentication type
  2676. * @iter: Pointer to incoming dbus message iter
  2677. * @error: Location to store error on failure
  2678. * @user_data: Function specific data
  2679. * Returns: TRUE on success, FALSE on failure
  2680. *
  2681. * Getter for "CurrentAuthMode" property.
  2682. */
  2683. dbus_bool_t wpas_dbus_getter_current_auth_mode(DBusMessageIter *iter,
  2684. DBusError *error,
  2685. void *user_data)
  2686. {
  2687. struct wpa_supplicant *wpa_s = user_data;
  2688. const char *eap_mode;
  2689. const char *auth_mode;
  2690. char eap_mode_buf[WPAS_DBUS_AUTH_MODE_MAX];
  2691. if (wpa_s->wpa_state != WPA_COMPLETED) {
  2692. auth_mode = "INACTIVE";
  2693. } else if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X ||
  2694. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  2695. eap_mode = wpa_supplicant_get_eap_mode(wpa_s);
  2696. os_snprintf(eap_mode_buf, WPAS_DBUS_AUTH_MODE_MAX,
  2697. "EAP-%s", eap_mode);
  2698. auth_mode = eap_mode_buf;
  2699. } else {
  2700. auth_mode = wpa_key_mgmt_txt(wpa_s->key_mgmt,
  2701. wpa_s->current_ssid->proto);
  2702. }
  2703. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2704. &auth_mode, error);
  2705. }
  2706. /**
  2707. * wpas_dbus_getter_bridge_ifname - Get interface name
  2708. * @iter: Pointer to incoming dbus message iter
  2709. * @error: Location to store error on failure
  2710. * @user_data: Function specific data
  2711. * Returns: TRUE on success, FALSE on failure
  2712. *
  2713. * Getter for "BridgeIfname" property.
  2714. */
  2715. dbus_bool_t wpas_dbus_getter_bridge_ifname(DBusMessageIter *iter,
  2716. DBusError *error,
  2717. void *user_data)
  2718. {
  2719. struct wpa_supplicant *wpa_s = user_data;
  2720. const char *bridge_ifname = wpa_s->bridge_ifname;
  2721. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2722. &bridge_ifname, error);
  2723. }
  2724. /**
  2725. * wpas_dbus_getter_bsss - Get array of BSSs objects
  2726. * @iter: Pointer to incoming dbus message iter
  2727. * @error: Location to store error on failure
  2728. * @user_data: Function specific data
  2729. * Returns: TRUE on success, FALSE on failure
  2730. *
  2731. * Getter for "BSSs" property.
  2732. */
  2733. dbus_bool_t wpas_dbus_getter_bsss(DBusMessageIter *iter, DBusError *error,
  2734. void *user_data)
  2735. {
  2736. struct wpa_supplicant *wpa_s = user_data;
  2737. struct wpa_bss *bss;
  2738. char **paths;
  2739. unsigned int i = 0;
  2740. dbus_bool_t success = FALSE;
  2741. paths = os_calloc(wpa_s->num_bss, sizeof(char *));
  2742. if (!paths) {
  2743. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2744. return FALSE;
  2745. }
  2746. /* Loop through scan results and append each result's object path */
  2747. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  2748. paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  2749. if (paths[i] == NULL) {
  2750. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
  2751. "no memory");
  2752. goto out;
  2753. }
  2754. /* Construct the object path for this BSS. */
  2755. os_snprintf(paths[i++], WPAS_DBUS_OBJECT_PATH_MAX,
  2756. "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
  2757. wpa_s->dbus_new_path, bss->id);
  2758. }
  2759. success = wpas_dbus_simple_array_property_getter(iter,
  2760. DBUS_TYPE_OBJECT_PATH,
  2761. paths, wpa_s->num_bss,
  2762. error);
  2763. out:
  2764. while (i)
  2765. os_free(paths[--i]);
  2766. os_free(paths);
  2767. return success;
  2768. }
  2769. /**
  2770. * wpas_dbus_getter_networks - Get array of networks objects
  2771. * @iter: Pointer to incoming dbus message iter
  2772. * @error: Location to store error on failure
  2773. * @user_data: Function specific data
  2774. * Returns: TRUE on success, FALSE on failure
  2775. *
  2776. * Getter for "Networks" property.
  2777. */
  2778. dbus_bool_t wpas_dbus_getter_networks(DBusMessageIter *iter, DBusError *error,
  2779. void *user_data)
  2780. {
  2781. struct wpa_supplicant *wpa_s = user_data;
  2782. struct wpa_ssid *ssid;
  2783. char **paths;
  2784. unsigned int i = 0, num = 0;
  2785. dbus_bool_t success = FALSE;
  2786. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  2787. if (!network_is_persistent_group(ssid))
  2788. num++;
  2789. paths = os_calloc(num, sizeof(char *));
  2790. if (!paths) {
  2791. dbus_set_error(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2792. return FALSE;
  2793. }
  2794. /* Loop through configured networks and append object path of each */
  2795. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  2796. if (network_is_persistent_group(ssid))
  2797. continue;
  2798. paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  2799. if (paths[i] == NULL) {
  2800. dbus_set_error(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2801. goto out;
  2802. }
  2803. /* Construct the object path for this network. */
  2804. os_snprintf(paths[i++], WPAS_DBUS_OBJECT_PATH_MAX,
  2805. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%d",
  2806. wpa_s->dbus_new_path, ssid->id);
  2807. }
  2808. success = wpas_dbus_simple_array_property_getter(iter,
  2809. DBUS_TYPE_OBJECT_PATH,
  2810. paths, num, error);
  2811. out:
  2812. while (i)
  2813. os_free(paths[--i]);
  2814. os_free(paths);
  2815. return success;
  2816. }
  2817. /**
  2818. * wpas_dbus_getter_pkcs11_engine_path - Get PKCS #11 engine path
  2819. * @iter: Pointer to incoming dbus message iter
  2820. * @error: Location to store error on failure
  2821. * @user_data: Function specific data
  2822. * Returns: A dbus message containing the PKCS #11 engine path
  2823. *
  2824. * Getter for "PKCS11EnginePath" property.
  2825. */
  2826. dbus_bool_t wpas_dbus_getter_pkcs11_engine_path(DBusMessageIter *iter,
  2827. DBusError *error,
  2828. void *user_data)
  2829. {
  2830. struct wpa_supplicant *wpa_s = user_data;
  2831. const char *pkcs11_engine_path;
  2832. if (wpa_s->conf->pkcs11_engine_path == NULL)
  2833. pkcs11_engine_path = "";
  2834. else
  2835. pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
  2836. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2837. &pkcs11_engine_path, error);
  2838. }
  2839. /**
  2840. * wpas_dbus_getter_pkcs11_module_path - Get PKCS #11 module path
  2841. * @iter: Pointer to incoming dbus message iter
  2842. * @error: Location to store error on failure
  2843. * @user_data: Function specific data
  2844. * Returns: A dbus message containing the PKCS #11 module path
  2845. *
  2846. * Getter for "PKCS11ModulePath" property.
  2847. */
  2848. dbus_bool_t wpas_dbus_getter_pkcs11_module_path(DBusMessageIter *iter,
  2849. DBusError *error,
  2850. void *user_data)
  2851. {
  2852. struct wpa_supplicant *wpa_s = user_data;
  2853. const char *pkcs11_module_path;
  2854. if (wpa_s->conf->pkcs11_module_path == NULL)
  2855. pkcs11_module_path = "";
  2856. else
  2857. pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
  2858. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  2859. &pkcs11_module_path, error);
  2860. }
  2861. /**
  2862. * wpas_dbus_getter_blobs - Get all blobs defined for this interface
  2863. * @iter: Pointer to incoming dbus message iter
  2864. * @error: Location to store error on failure
  2865. * @user_data: Function specific data
  2866. * Returns: TRUE on success, FALSE on failure
  2867. *
  2868. * Getter for "Blobs" property.
  2869. */
  2870. dbus_bool_t wpas_dbus_getter_blobs(DBusMessageIter *iter, DBusError *error,
  2871. void *user_data)
  2872. {
  2873. struct wpa_supplicant *wpa_s = user_data;
  2874. DBusMessageIter variant_iter, dict_iter, entry_iter, array_iter;
  2875. struct wpa_config_blob *blob;
  2876. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  2877. "a{say}", &variant_iter) ||
  2878. !dbus_message_iter_open_container(&variant_iter, DBUS_TYPE_ARRAY,
  2879. "{say}", &dict_iter)) {
  2880. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2881. return FALSE;
  2882. }
  2883. blob = wpa_s->conf->blobs;
  2884. while (blob) {
  2885. if (!dbus_message_iter_open_container(&dict_iter,
  2886. DBUS_TYPE_DICT_ENTRY,
  2887. NULL, &entry_iter) ||
  2888. !dbus_message_iter_append_basic(&entry_iter,
  2889. DBUS_TYPE_STRING,
  2890. &(blob->name)) ||
  2891. !dbus_message_iter_open_container(&entry_iter,
  2892. DBUS_TYPE_ARRAY,
  2893. DBUS_TYPE_BYTE_AS_STRING,
  2894. &array_iter) ||
  2895. !dbus_message_iter_append_fixed_array(&array_iter,
  2896. DBUS_TYPE_BYTE,
  2897. &(blob->data),
  2898. blob->len) ||
  2899. !dbus_message_iter_close_container(&entry_iter,
  2900. &array_iter) ||
  2901. !dbus_message_iter_close_container(&dict_iter,
  2902. &entry_iter)) {
  2903. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
  2904. "no memory");
  2905. return FALSE;
  2906. }
  2907. blob = blob->next;
  2908. }
  2909. if (!dbus_message_iter_close_container(&variant_iter, &dict_iter) ||
  2910. !dbus_message_iter_close_container(iter, &variant_iter)) {
  2911. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  2912. return FALSE;
  2913. }
  2914. return TRUE;
  2915. }
  2916. static struct wpa_bss * get_bss_helper(struct bss_handler_args *args,
  2917. DBusError *error, const char *func_name)
  2918. {
  2919. struct wpa_bss *res = wpa_bss_get_id(args->wpa_s, args->id);
  2920. if (!res) {
  2921. wpa_printf(MSG_ERROR, "%s[dbus]: no bss with id %d found",
  2922. func_name, args->id);
  2923. dbus_set_error(error, DBUS_ERROR_FAILED,
  2924. "%s: BSS %d not found",
  2925. func_name, args->id);
  2926. }
  2927. return res;
  2928. }
  2929. /**
  2930. * wpas_dbus_getter_bss_bssid - Return the BSSID of a BSS
  2931. * @iter: Pointer to incoming dbus message iter
  2932. * @error: Location to store error on failure
  2933. * @user_data: Function specific data
  2934. * Returns: TRUE on success, FALSE on failure
  2935. *
  2936. * Getter for "BSSID" property.
  2937. */
  2938. dbus_bool_t wpas_dbus_getter_bss_bssid(DBusMessageIter *iter, DBusError *error,
  2939. void *user_data)
  2940. {
  2941. struct bss_handler_args *args = user_data;
  2942. struct wpa_bss *res;
  2943. res = get_bss_helper(args, error, __func__);
  2944. if (!res)
  2945. return FALSE;
  2946. return wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
  2947. res->bssid, ETH_ALEN,
  2948. error);
  2949. }
  2950. /**
  2951. * wpas_dbus_getter_bss_ssid - Return the SSID of a BSS
  2952. * @iter: Pointer to incoming dbus message iter
  2953. * @error: Location to store error on failure
  2954. * @user_data: Function specific data
  2955. * Returns: TRUE on success, FALSE on failure
  2956. *
  2957. * Getter for "SSID" property.
  2958. */
  2959. dbus_bool_t wpas_dbus_getter_bss_ssid(DBusMessageIter *iter, DBusError *error,
  2960. void *user_data)
  2961. {
  2962. struct bss_handler_args *args = user_data;
  2963. struct wpa_bss *res;
  2964. res = get_bss_helper(args, error, __func__);
  2965. if (!res)
  2966. return FALSE;
  2967. return wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
  2968. res->ssid, res->ssid_len,
  2969. error);
  2970. }
  2971. /**
  2972. * wpas_dbus_getter_bss_privacy - Return the privacy flag of a BSS
  2973. * @iter: Pointer to incoming dbus message iter
  2974. * @error: Location to store error on failure
  2975. * @user_data: Function specific data
  2976. * Returns: TRUE on success, FALSE on failure
  2977. *
  2978. * Getter for "Privacy" property.
  2979. */
  2980. dbus_bool_t wpas_dbus_getter_bss_privacy(DBusMessageIter *iter,
  2981. DBusError *error, void *user_data)
  2982. {
  2983. struct bss_handler_args *args = user_data;
  2984. struct wpa_bss *res;
  2985. dbus_bool_t privacy;
  2986. res = get_bss_helper(args, error, __func__);
  2987. if (!res)
  2988. return FALSE;
  2989. privacy = (res->caps & IEEE80211_CAP_PRIVACY) ? TRUE : FALSE;
  2990. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  2991. &privacy, error);
  2992. }
  2993. /**
  2994. * wpas_dbus_getter_bss_mode - Return the mode of a BSS
  2995. * @iter: Pointer to incoming dbus message iter
  2996. * @error: Location to store error on failure
  2997. * @user_data: Function specific data
  2998. * Returns: TRUE on success, FALSE on failure
  2999. *
  3000. * Getter for "Mode" property.
  3001. */
  3002. dbus_bool_t wpas_dbus_getter_bss_mode(DBusMessageIter *iter, DBusError *error,
  3003. void *user_data)
  3004. {
  3005. struct bss_handler_args *args = user_data;
  3006. struct wpa_bss *res;
  3007. const char *mode;
  3008. res = get_bss_helper(args, error, __func__);
  3009. if (!res)
  3010. return FALSE;
  3011. if (bss_is_dmg(res)) {
  3012. switch (res->caps & IEEE80211_CAP_DMG_MASK) {
  3013. case IEEE80211_CAP_DMG_PBSS:
  3014. case IEEE80211_CAP_DMG_IBSS:
  3015. mode = "ad-hoc";
  3016. break;
  3017. case IEEE80211_CAP_DMG_AP:
  3018. mode = "infrastructure";
  3019. break;
  3020. }
  3021. } else {
  3022. if (res->caps & IEEE80211_CAP_IBSS)
  3023. mode = "ad-hoc";
  3024. else
  3025. mode = "infrastructure";
  3026. }
  3027. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  3028. &mode, error);
  3029. }
  3030. /**
  3031. * wpas_dbus_getter_bss_level - Return the signal strength of a BSS
  3032. * @iter: Pointer to incoming dbus message iter
  3033. * @error: Location to store error on failure
  3034. * @user_data: Function specific data
  3035. * Returns: TRUE on success, FALSE on failure
  3036. *
  3037. * Getter for "Level" property.
  3038. */
  3039. dbus_bool_t wpas_dbus_getter_bss_signal(DBusMessageIter *iter,
  3040. DBusError *error, void *user_data)
  3041. {
  3042. struct bss_handler_args *args = user_data;
  3043. struct wpa_bss *res;
  3044. s16 level;
  3045. res = get_bss_helper(args, error, __func__);
  3046. if (!res)
  3047. return FALSE;
  3048. level = (s16) res->level;
  3049. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_INT16,
  3050. &level, error);
  3051. }
  3052. /**
  3053. * wpas_dbus_getter_bss_frequency - Return the frequency of a BSS
  3054. * @iter: Pointer to incoming dbus message iter
  3055. * @error: Location to store error on failure
  3056. * @user_data: Function specific data
  3057. * Returns: TRUE on success, FALSE on failure
  3058. *
  3059. * Getter for "Frequency" property.
  3060. */
  3061. dbus_bool_t wpas_dbus_getter_bss_frequency(DBusMessageIter *iter,
  3062. DBusError *error, void *user_data)
  3063. {
  3064. struct bss_handler_args *args = user_data;
  3065. struct wpa_bss *res;
  3066. u16 freq;
  3067. res = get_bss_helper(args, error, __func__);
  3068. if (!res)
  3069. return FALSE;
  3070. freq = (u16) res->freq;
  3071. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT16,
  3072. &freq, error);
  3073. }
  3074. static int cmp_u8s_desc(const void *a, const void *b)
  3075. {
  3076. return (*(u8 *) b - *(u8 *) a);
  3077. }
  3078. /**
  3079. * wpas_dbus_getter_bss_rates - Return available bit rates of a BSS
  3080. * @iter: Pointer to incoming dbus message iter
  3081. * @error: Location to store error on failure
  3082. * @user_data: Function specific data
  3083. * Returns: TRUE on success, FALSE on failure
  3084. *
  3085. * Getter for "Rates" property.
  3086. */
  3087. dbus_bool_t wpas_dbus_getter_bss_rates(DBusMessageIter *iter,
  3088. DBusError *error, void *user_data)
  3089. {
  3090. struct bss_handler_args *args = user_data;
  3091. struct wpa_bss *res;
  3092. u8 *ie_rates = NULL;
  3093. u32 *real_rates;
  3094. int rates_num, i;
  3095. dbus_bool_t success = FALSE;
  3096. res = get_bss_helper(args, error, __func__);
  3097. if (!res)
  3098. return FALSE;
  3099. rates_num = wpa_bss_get_bit_rates(res, &ie_rates);
  3100. if (rates_num < 0)
  3101. return FALSE;
  3102. qsort(ie_rates, rates_num, 1, cmp_u8s_desc);
  3103. real_rates = os_malloc(sizeof(u32) * rates_num);
  3104. if (!real_rates) {
  3105. os_free(ie_rates);
  3106. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3107. return FALSE;
  3108. }
  3109. for (i = 0; i < rates_num; i++)
  3110. real_rates[i] = ie_rates[i] * 500000;
  3111. success = wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_UINT32,
  3112. real_rates, rates_num,
  3113. error);
  3114. os_free(ie_rates);
  3115. os_free(real_rates);
  3116. return success;
  3117. }
  3118. static dbus_bool_t wpas_dbus_get_bss_security_prop(DBusMessageIter *iter,
  3119. struct wpa_ie_data *ie_data,
  3120. DBusError *error)
  3121. {
  3122. DBusMessageIter iter_dict, variant_iter;
  3123. const char *group;
  3124. const char *pairwise[5]; /* max 5 pairwise ciphers is supported */
  3125. const char *key_mgmt[8]; /* max 8 key managements may be supported */
  3126. int n;
  3127. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  3128. "a{sv}", &variant_iter))
  3129. goto nomem;
  3130. if (!wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
  3131. goto nomem;
  3132. /* KeyMgmt */
  3133. n = 0;
  3134. if (ie_data->key_mgmt & WPA_KEY_MGMT_PSK)
  3135. key_mgmt[n++] = "wpa-psk";
  3136. if (ie_data->key_mgmt & WPA_KEY_MGMT_FT_PSK)
  3137. key_mgmt[n++] = "wpa-ft-psk";
  3138. if (ie_data->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
  3139. key_mgmt[n++] = "wpa-psk-sha256";
  3140. if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  3141. key_mgmt[n++] = "wpa-eap";
  3142. if (ie_data->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
  3143. key_mgmt[n++] = "wpa-ft-eap";
  3144. if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
  3145. key_mgmt[n++] = "wpa-eap-sha256";
  3146. if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B)
  3147. key_mgmt[n++] = "wpa-eap-suite-b";
  3148. if (ie_data->key_mgmt & WPA_KEY_MGMT_NONE)
  3149. key_mgmt[n++] = "wpa-none";
  3150. if (!wpa_dbus_dict_append_string_array(&iter_dict, "KeyMgmt",
  3151. key_mgmt, n))
  3152. goto nomem;
  3153. /* Group */
  3154. switch (ie_data->group_cipher) {
  3155. case WPA_CIPHER_WEP40:
  3156. group = "wep40";
  3157. break;
  3158. case WPA_CIPHER_TKIP:
  3159. group = "tkip";
  3160. break;
  3161. case WPA_CIPHER_CCMP:
  3162. group = "ccmp";
  3163. break;
  3164. case WPA_CIPHER_GCMP:
  3165. group = "gcmp";
  3166. break;
  3167. case WPA_CIPHER_WEP104:
  3168. group = "wep104";
  3169. break;
  3170. case WPA_CIPHER_CCMP_256:
  3171. group = "ccmp-256";
  3172. break;
  3173. case WPA_CIPHER_GCMP_256:
  3174. group = "gcmp-256";
  3175. break;
  3176. default:
  3177. group = "";
  3178. break;
  3179. }
  3180. if (!wpa_dbus_dict_append_string(&iter_dict, "Group", group))
  3181. goto nomem;
  3182. /* Pairwise */
  3183. n = 0;
  3184. if (ie_data->pairwise_cipher & WPA_CIPHER_TKIP)
  3185. pairwise[n++] = "tkip";
  3186. if (ie_data->pairwise_cipher & WPA_CIPHER_CCMP)
  3187. pairwise[n++] = "ccmp";
  3188. if (ie_data->pairwise_cipher & WPA_CIPHER_GCMP)
  3189. pairwise[n++] = "gcmp";
  3190. if (ie_data->pairwise_cipher & WPA_CIPHER_CCMP_256)
  3191. pairwise[n++] = "ccmp-256";
  3192. if (ie_data->pairwise_cipher & WPA_CIPHER_GCMP_256)
  3193. pairwise[n++] = "gcmp-256";
  3194. if (!wpa_dbus_dict_append_string_array(&iter_dict, "Pairwise",
  3195. pairwise, n))
  3196. goto nomem;
  3197. /* Management group (RSN only) */
  3198. if (ie_data->proto == WPA_PROTO_RSN) {
  3199. switch (ie_data->mgmt_group_cipher) {
  3200. #ifdef CONFIG_IEEE80211W
  3201. case WPA_CIPHER_AES_128_CMAC:
  3202. group = "aes128cmac";
  3203. break;
  3204. #endif /* CONFIG_IEEE80211W */
  3205. default:
  3206. group = "";
  3207. break;
  3208. }
  3209. if (!wpa_dbus_dict_append_string(&iter_dict, "MgmtGroup",
  3210. group))
  3211. goto nomem;
  3212. }
  3213. if (!wpa_dbus_dict_close_write(&variant_iter, &iter_dict) ||
  3214. !dbus_message_iter_close_container(iter, &variant_iter))
  3215. goto nomem;
  3216. return TRUE;
  3217. nomem:
  3218. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3219. return FALSE;
  3220. }
  3221. /**
  3222. * wpas_dbus_getter_bss_wpa - Return the WPA options of a BSS
  3223. * @iter: Pointer to incoming dbus message iter
  3224. * @error: Location to store error on failure
  3225. * @user_data: Function specific data
  3226. * Returns: TRUE on success, FALSE on failure
  3227. *
  3228. * Getter for "WPA" property.
  3229. */
  3230. dbus_bool_t wpas_dbus_getter_bss_wpa(DBusMessageIter *iter, DBusError *error,
  3231. void *user_data)
  3232. {
  3233. struct bss_handler_args *args = user_data;
  3234. struct wpa_bss *res;
  3235. struct wpa_ie_data wpa_data;
  3236. const u8 *ie;
  3237. res = get_bss_helper(args, error, __func__);
  3238. if (!res)
  3239. return FALSE;
  3240. os_memset(&wpa_data, 0, sizeof(wpa_data));
  3241. ie = wpa_bss_get_vendor_ie(res, WPA_IE_VENDOR_TYPE);
  3242. if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &wpa_data) < 0) {
  3243. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  3244. "failed to parse WPA IE");
  3245. return FALSE;
  3246. }
  3247. return wpas_dbus_get_bss_security_prop(iter, &wpa_data, error);
  3248. }
  3249. /**
  3250. * wpas_dbus_getter_bss_rsn - Return the RSN options of a BSS
  3251. * @iter: Pointer to incoming dbus message iter
  3252. * @error: Location to store error on failure
  3253. * @user_data: Function specific data
  3254. * Returns: TRUE on success, FALSE on failure
  3255. *
  3256. * Getter for "RSN" property.
  3257. */
  3258. dbus_bool_t wpas_dbus_getter_bss_rsn(DBusMessageIter *iter, DBusError *error,
  3259. void *user_data)
  3260. {
  3261. struct bss_handler_args *args = user_data;
  3262. struct wpa_bss *res;
  3263. struct wpa_ie_data wpa_data;
  3264. const u8 *ie;
  3265. res = get_bss_helper(args, error, __func__);
  3266. if (!res)
  3267. return FALSE;
  3268. os_memset(&wpa_data, 0, sizeof(wpa_data));
  3269. ie = wpa_bss_get_ie(res, WLAN_EID_RSN);
  3270. if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &wpa_data) < 0) {
  3271. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  3272. "failed to parse RSN IE");
  3273. return FALSE;
  3274. }
  3275. return wpas_dbus_get_bss_security_prop(iter, &wpa_data, error);
  3276. }
  3277. /**
  3278. * wpas_dbus_getter_bss_wps - Return the WPS options of a BSS
  3279. * @iter: Pointer to incoming dbus message iter
  3280. * @error: Location to store error on failure
  3281. * @user_data: Function specific data
  3282. * Returns: TRUE on success, FALSE on failure
  3283. *
  3284. * Getter for "WPS" property.
  3285. */
  3286. dbus_bool_t wpas_dbus_getter_bss_wps(DBusMessageIter *iter, DBusError *error,
  3287. void *user_data)
  3288. {
  3289. struct bss_handler_args *args = user_data;
  3290. struct wpa_bss *res;
  3291. #ifdef CONFIG_WPS
  3292. struct wpabuf *wps_ie;
  3293. #endif /* CONFIG_WPS */
  3294. DBusMessageIter iter_dict, variant_iter;
  3295. const char *type = "";
  3296. res = get_bss_helper(args, error, __func__);
  3297. if (!res)
  3298. return FALSE;
  3299. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  3300. "a{sv}", &variant_iter) ||
  3301. !wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
  3302. goto nomem;
  3303. #ifdef CONFIG_WPS
  3304. wps_ie = wpa_bss_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
  3305. if (wps_ie) {
  3306. if (wps_is_selected_pbc_registrar(wps_ie))
  3307. type = "pbc";
  3308. else if (wps_is_selected_pin_registrar(wps_ie))
  3309. type = "pin";
  3310. wpabuf_free(wps_ie);
  3311. }
  3312. #endif /* CONFIG_WPS */
  3313. if (!wpa_dbus_dict_append_string(&iter_dict, "Type", type) ||
  3314. !wpa_dbus_dict_close_write(&variant_iter, &iter_dict) ||
  3315. !dbus_message_iter_close_container(iter, &variant_iter))
  3316. goto nomem;
  3317. return TRUE;
  3318. nomem:
  3319. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3320. return FALSE;
  3321. }
  3322. /**
  3323. * wpas_dbus_getter_bss_ies - Return all IEs of a BSS
  3324. * @iter: Pointer to incoming dbus message iter
  3325. * @error: Location to store error on failure
  3326. * @user_data: Function specific data
  3327. * Returns: TRUE on success, FALSE on failure
  3328. *
  3329. * Getter for "IEs" property.
  3330. */
  3331. dbus_bool_t wpas_dbus_getter_bss_ies(DBusMessageIter *iter, DBusError *error,
  3332. void *user_data)
  3333. {
  3334. struct bss_handler_args *args = user_data;
  3335. struct wpa_bss *res;
  3336. res = get_bss_helper(args, error, __func__);
  3337. if (!res)
  3338. return FALSE;
  3339. return wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
  3340. res + 1, res->ie_len,
  3341. error);
  3342. }
  3343. /**
  3344. * wpas_dbus_getter_bss_age - Return time in seconds since BSS was last seen
  3345. * @iter: Pointer to incoming dbus message iter
  3346. * @error: Location to store error on failure
  3347. * @user_data: Function specific data
  3348. * Returns: TRUE on success, FALSE on failure
  3349. *
  3350. * Getter for BSS age
  3351. */
  3352. dbus_bool_t wpas_dbus_getter_bss_age(DBusMessageIter *iter, DBusError *error,
  3353. void *user_data)
  3354. {
  3355. struct bss_handler_args *args = user_data;
  3356. struct wpa_bss *res;
  3357. struct os_reltime now, diff = { 0, 0 };
  3358. u32 age;
  3359. res = get_bss_helper(args, error, __func__);
  3360. if (!res)
  3361. return FALSE;
  3362. os_get_reltime(&now);
  3363. os_reltime_sub(&now, &res->last_update, &diff);
  3364. age = diff.sec > 0 ? diff.sec : 0;
  3365. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT32, &age,
  3366. error);
  3367. }
  3368. /**
  3369. * wpas_dbus_getter_enabled - Check whether network is enabled or disabled
  3370. * @iter: Pointer to incoming dbus message iter
  3371. * @error: Location to store error on failure
  3372. * @user_data: Function specific data
  3373. * Returns: TRUE on success, FALSE on failure
  3374. *
  3375. * Getter for "enabled" property of a configured network.
  3376. */
  3377. dbus_bool_t wpas_dbus_getter_enabled(DBusMessageIter *iter, DBusError *error,
  3378. void *user_data)
  3379. {
  3380. struct network_handler_args *net = user_data;
  3381. dbus_bool_t enabled = net->ssid->disabled ? FALSE : TRUE;
  3382. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  3383. &enabled, error);
  3384. }
  3385. /**
  3386. * wpas_dbus_setter_enabled - Mark a configured network as enabled or disabled
  3387. * @iter: Pointer to incoming dbus message iter
  3388. * @error: Location to store error on failure
  3389. * @user_data: Function specific data
  3390. * Returns: TRUE on success, FALSE on failure
  3391. *
  3392. * Setter for "Enabled" property of a configured network.
  3393. */
  3394. dbus_bool_t wpas_dbus_setter_enabled(DBusMessageIter *iter, DBusError *error,
  3395. void *user_data)
  3396. {
  3397. struct network_handler_args *net = user_data;
  3398. struct wpa_supplicant *wpa_s;
  3399. struct wpa_ssid *ssid;
  3400. dbus_bool_t enable;
  3401. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_BOOLEAN,
  3402. &enable))
  3403. return FALSE;
  3404. wpa_s = net->wpa_s;
  3405. ssid = net->ssid;
  3406. if (enable)
  3407. wpa_supplicant_enable_network(wpa_s, ssid);
  3408. else
  3409. wpa_supplicant_disable_network(wpa_s, ssid);
  3410. return TRUE;
  3411. }
  3412. /**
  3413. * wpas_dbus_getter_network_properties - Get options for a configured network
  3414. * @iter: Pointer to incoming dbus message iter
  3415. * @error: Location to store error on failure
  3416. * @user_data: Function specific data
  3417. * Returns: TRUE on success, FALSE on failure
  3418. *
  3419. * Getter for "Properties" property of a configured network.
  3420. */
  3421. dbus_bool_t wpas_dbus_getter_network_properties(DBusMessageIter *iter,
  3422. DBusError *error,
  3423. void *user_data)
  3424. {
  3425. struct network_handler_args *net = user_data;
  3426. DBusMessageIter variant_iter, dict_iter;
  3427. char **iterator;
  3428. char **props = wpa_config_get_all(net->ssid, 1);
  3429. dbus_bool_t success = FALSE;
  3430. if (!props) {
  3431. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3432. return FALSE;
  3433. }
  3434. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "a{sv}",
  3435. &variant_iter) ||
  3436. !wpa_dbus_dict_open_write(&variant_iter, &dict_iter)) {
  3437. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3438. goto out;
  3439. }
  3440. iterator = props;
  3441. while (*iterator) {
  3442. if (!wpa_dbus_dict_append_string(&dict_iter, *iterator,
  3443. *(iterator + 1))) {
  3444. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
  3445. "no memory");
  3446. goto out;
  3447. }
  3448. iterator += 2;
  3449. }
  3450. if (!wpa_dbus_dict_close_write(&variant_iter, &dict_iter) ||
  3451. !dbus_message_iter_close_container(iter, &variant_iter)) {
  3452. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  3453. goto out;
  3454. }
  3455. success = TRUE;
  3456. out:
  3457. iterator = props;
  3458. while (*iterator) {
  3459. os_free(*iterator);
  3460. iterator++;
  3461. }
  3462. os_free(props);
  3463. return success;
  3464. }
  3465. /**
  3466. * wpas_dbus_setter_network_properties - Set options for a configured network
  3467. * @iter: Pointer to incoming dbus message iter
  3468. * @error: Location to store error on failure
  3469. * @user_data: Function specific data
  3470. * Returns: TRUE on success, FALSE on failure
  3471. *
  3472. * Setter for "Properties" property of a configured network.
  3473. */
  3474. dbus_bool_t wpas_dbus_setter_network_properties(DBusMessageIter *iter,
  3475. DBusError *error,
  3476. void *user_data)
  3477. {
  3478. struct network_handler_args *net = user_data;
  3479. struct wpa_ssid *ssid = net->ssid;
  3480. DBusMessageIter variant_iter;
  3481. dbus_message_iter_recurse(iter, &variant_iter);
  3482. return set_network_properties(net->wpa_s, ssid, &variant_iter, error);
  3483. }
  3484. #ifdef CONFIG_AP
  3485. DBusMessage * wpas_dbus_handler_subscribe_preq(
  3486. DBusMessage *message, struct wpa_supplicant *wpa_s)
  3487. {
  3488. struct wpas_dbus_priv *priv = wpa_s->global->dbus;
  3489. char *name;
  3490. if (wpa_s->preq_notify_peer != NULL) {
  3491. if (os_strcmp(dbus_message_get_sender(message),
  3492. wpa_s->preq_notify_peer) == 0)
  3493. return NULL;
  3494. return dbus_message_new_error(message,
  3495. WPAS_DBUS_ERROR_SUBSCRIPTION_IN_USE,
  3496. "Another application is already subscribed");
  3497. }
  3498. name = os_strdup(dbus_message_get_sender(message));
  3499. if (!name)
  3500. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  3501. "out of memory");
  3502. wpa_s->preq_notify_peer = name;
  3503. /* Subscribe to clean up if application closes socket */
  3504. wpas_dbus_subscribe_noc(priv);
  3505. /*
  3506. * Double-check it's still alive to make sure that we didn't
  3507. * miss the NameOwnerChanged signal, e.g. while strdup'ing.
  3508. */
  3509. if (!dbus_bus_name_has_owner(priv->con, name, NULL)) {
  3510. /*
  3511. * Application no longer exists, clean up.
  3512. * The return value is irrelevant now.
  3513. *
  3514. * Need to check if the NameOwnerChanged handling
  3515. * already cleaned up because we have processed
  3516. * DBus messages while checking if the name still
  3517. * has an owner.
  3518. */
  3519. if (!wpa_s->preq_notify_peer)
  3520. return NULL;
  3521. os_free(wpa_s->preq_notify_peer);
  3522. wpa_s->preq_notify_peer = NULL;
  3523. wpas_dbus_unsubscribe_noc(priv);
  3524. }
  3525. return NULL;
  3526. }
  3527. DBusMessage * wpas_dbus_handler_unsubscribe_preq(
  3528. DBusMessage *message, struct wpa_supplicant *wpa_s)
  3529. {
  3530. struct wpas_dbus_priv *priv = wpa_s->global->dbus;
  3531. if (!wpa_s->preq_notify_peer)
  3532. return dbus_message_new_error(message,
  3533. WPAS_DBUS_ERROR_NO_SUBSCRIPTION,
  3534. "Not subscribed");
  3535. if (os_strcmp(wpa_s->preq_notify_peer,
  3536. dbus_message_get_sender(message)))
  3537. return dbus_message_new_error(message,
  3538. WPAS_DBUS_ERROR_SUBSCRIPTION_EPERM,
  3539. "Can't unsubscribe others");
  3540. os_free(wpa_s->preq_notify_peer);
  3541. wpa_s->preq_notify_peer = NULL;
  3542. wpas_dbus_unsubscribe_noc(priv);
  3543. return NULL;
  3544. }
  3545. void wpas_dbus_signal_preq(struct wpa_supplicant *wpa_s,
  3546. const u8 *addr, const u8 *dst, const u8 *bssid,
  3547. const u8 *ie, size_t ie_len, u32 ssi_signal)
  3548. {
  3549. DBusMessage *msg;
  3550. DBusMessageIter iter, dict_iter;
  3551. struct wpas_dbus_priv *priv = wpa_s->global->dbus;
  3552. /* Do nothing if the control interface is not turned on */
  3553. if (priv == NULL)
  3554. return;
  3555. if (wpa_s->preq_notify_peer == NULL)
  3556. return;
  3557. msg = dbus_message_new_signal(wpa_s->dbus_new_path,
  3558. WPAS_DBUS_NEW_IFACE_INTERFACE,
  3559. "ProbeRequest");
  3560. if (msg == NULL)
  3561. return;
  3562. dbus_message_set_destination(msg, wpa_s->preq_notify_peer);
  3563. dbus_message_iter_init_append(msg, &iter);
  3564. if (!wpa_dbus_dict_open_write(&iter, &dict_iter) ||
  3565. (addr && !wpa_dbus_dict_append_byte_array(&dict_iter, "addr",
  3566. (const char *) addr,
  3567. ETH_ALEN)) ||
  3568. (dst && !wpa_dbus_dict_append_byte_array(&dict_iter, "dst",
  3569. (const char *) dst,
  3570. ETH_ALEN)) ||
  3571. (bssid && !wpa_dbus_dict_append_byte_array(&dict_iter, "bssid",
  3572. (const char *) bssid,
  3573. ETH_ALEN)) ||
  3574. (ie && ie_len && !wpa_dbus_dict_append_byte_array(&dict_iter, "ies",
  3575. (const char *) ie,
  3576. ie_len)) ||
  3577. (ssi_signal && !wpa_dbus_dict_append_int32(&dict_iter, "signal",
  3578. ssi_signal)) ||
  3579. !wpa_dbus_dict_close_write(&iter, &dict_iter))
  3580. goto fail;
  3581. dbus_connection_send(priv->con, msg, NULL);
  3582. goto out;
  3583. fail:
  3584. wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
  3585. out:
  3586. dbus_message_unref(msg);
  3587. }
  3588. #endif /* CONFIG_AP */