dbus_new_handlers.c 87 KB

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