dbus_new_handlers.c 103 KB

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