dbus_new_handlers_p2p.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442
  1. /*
  2. * WPA Supplicant / dbus-based control interface (P2P)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * Alternatively, this software may be distributed under the terms of BSD
  9. * license.
  10. *
  11. * See README and COPYING for more details.
  12. */
  13. #include "includes.h"
  14. #include "utils/includes.h"
  15. #include "common.h"
  16. #include "../config.h"
  17. #include "../wpa_supplicant_i.h"
  18. #include "../wps_supplicant.h"
  19. #include "../notify.h"
  20. #include "dbus_new_helpers.h"
  21. #include "dbus_new.h"
  22. #include "dbus_new_handlers.h"
  23. #include "dbus_new_handlers_p2p.h"
  24. #include "dbus_dict_helpers.h"
  25. #include "p2p/p2p.h"
  26. #include "common/ieee802_11_defs.h"
  27. #include "ap/hostapd.h"
  28. #include "ap/ap_config.h"
  29. #include "ap/wps_hostapd.h"
  30. #include "../p2p_supplicant.h"
  31. /**
  32. * Parses out the mac address from the peer object path.
  33. * @peer_path - object path of the form
  34. * /fi/w1/wpa_supplicant1/Interfaces/n/Peers/00112233445566 (no colons)
  35. * @addr - out param must be of ETH_ALEN size
  36. * Returns 0 if valid (including MAC), -1 otherwise
  37. */
  38. static int parse_peer_object_path(char *peer_path, u8 addr[ETH_ALEN])
  39. {
  40. char *p;
  41. if (!peer_path)
  42. return -1;
  43. p = os_strrchr(peer_path, '/');
  44. if (!p)
  45. return -1;
  46. p++;
  47. return hwaddr_compact_aton(p, addr);
  48. }
  49. /**
  50. * wpas_dbus_error_persistent_group_unknown - Return a new PersistentGroupUnknown
  51. * error message
  52. * @message: Pointer to incoming dbus message this error refers to
  53. * Returns: a dbus error message
  54. *
  55. * Convenience function to create and return an invalid persistent group error.
  56. */
  57. static DBusMessage * wpas_dbus_error_persistent_group_unknown(
  58. DBusMessage *message)
  59. {
  60. return dbus_message_new_error(message, WPAS_DBUS_ERROR_NETWORK_UNKNOWN,
  61. "There is no such persistent group in "
  62. "this P2P device.");
  63. }
  64. DBusMessage * wpas_dbus_handler_p2p_find(DBusMessage *message,
  65. struct wpa_supplicant *wpa_s)
  66. {
  67. struct wpa_dbus_dict_entry entry;
  68. DBusMessage *reply = NULL;
  69. DBusMessageIter iter;
  70. DBusMessageIter iter_dict;
  71. unsigned int timeout = 0;
  72. enum p2p_discovery_type type = P2P_FIND_ONLY_SOCIAL;
  73. int num_req_dev_types = 0;
  74. unsigned int i;
  75. u8 *req_dev_types = NULL;
  76. dbus_message_iter_init(message, &iter);
  77. entry.key = NULL;
  78. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  79. goto error;
  80. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  81. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  82. goto error;
  83. if (!os_strcmp(entry.key, "Timeout") &&
  84. (entry.type == DBUS_TYPE_INT32)) {
  85. timeout = entry.uint32_value;
  86. } else if (os_strcmp(entry.key, "RequestedDeviceTypes") == 0) {
  87. if ((entry.type != DBUS_TYPE_ARRAY) ||
  88. (entry.array_type != WPAS_DBUS_TYPE_BINARRAY))
  89. goto error_clear;
  90. os_free(req_dev_types);
  91. req_dev_types =
  92. os_malloc(WPS_DEV_TYPE_LEN * entry.array_len);
  93. if (!req_dev_types)
  94. goto error_clear;
  95. for (i = 0; i < entry.array_len; i++) {
  96. if (wpabuf_len(entry.binarray_value[i]) !=
  97. WPS_DEV_TYPE_LEN)
  98. goto error_clear;
  99. os_memcpy(req_dev_types + i * WPS_DEV_TYPE_LEN,
  100. wpabuf_head(entry.binarray_value[i]),
  101. WPS_DEV_TYPE_LEN);
  102. }
  103. num_req_dev_types = entry.array_len;
  104. } else if (!os_strcmp(entry.key, "DiscoveryType") &&
  105. (entry.type == DBUS_TYPE_STRING)) {
  106. if (!os_strcmp(entry.str_value, "start_with_full"))
  107. type = P2P_FIND_START_WITH_FULL;
  108. else if (!os_strcmp(entry.str_value, "social"))
  109. type = P2P_FIND_ONLY_SOCIAL;
  110. else if (!os_strcmp(entry.str_value, "progressive"))
  111. type = P2P_FIND_PROGRESSIVE;
  112. else
  113. goto error_clear;
  114. } else
  115. goto error_clear;
  116. wpa_dbus_dict_entry_clear(&entry);
  117. }
  118. wpas_p2p_find(wpa_s, timeout, type, num_req_dev_types, req_dev_types,
  119. NULL);
  120. os_free(req_dev_types);
  121. return reply;
  122. error_clear:
  123. wpa_dbus_dict_entry_clear(&entry);
  124. error:
  125. os_free(req_dev_types);
  126. reply = wpas_dbus_error_invalid_args(message, entry.key);
  127. return reply;
  128. }
  129. DBusMessage * wpas_dbus_handler_p2p_stop_find(DBusMessage *message,
  130. struct wpa_supplicant *wpa_s)
  131. {
  132. wpas_p2p_stop_find(wpa_s);
  133. return NULL;
  134. }
  135. DBusMessage * wpas_dbus_handler_p2p_rejectpeer(DBusMessage *message,
  136. struct wpa_supplicant *wpa_s)
  137. {
  138. DBusMessageIter iter;
  139. char *peer_object_path = NULL;
  140. u8 peer_addr[ETH_ALEN];
  141. dbus_message_iter_init(message, &iter);
  142. dbus_message_iter_get_basic(&iter, &peer_object_path);
  143. if (parse_peer_object_path(peer_object_path, peer_addr) < 0)
  144. return wpas_dbus_error_invalid_args(message, NULL);
  145. if (wpas_p2p_reject(wpa_s, peer_addr) < 0)
  146. return wpas_dbus_error_unknown_error(message,
  147. "Failed to call wpas_p2p_reject method.");
  148. return NULL;
  149. }
  150. DBusMessage * wpas_dbus_handler_p2p_listen(DBusMessage *message,
  151. struct wpa_supplicant *wpa_s)
  152. {
  153. dbus_int32_t timeout = 0;
  154. if (!dbus_message_get_args(message, NULL, DBUS_TYPE_INT32, &timeout,
  155. DBUS_TYPE_INVALID))
  156. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  157. NULL);
  158. if (wpas_p2p_listen(wpa_s, (unsigned int)timeout))
  159. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  160. NULL);
  161. return NULL;
  162. }
  163. DBusMessage * wpas_dbus_handler_p2p_extendedlisten(
  164. DBusMessage *message, struct wpa_supplicant *wpa_s)
  165. {
  166. unsigned int period = 0, interval = 0;
  167. struct wpa_dbus_dict_entry entry;
  168. DBusMessageIter iter;
  169. DBusMessageIter iter_dict;
  170. dbus_message_iter_init(message, &iter);
  171. entry.key = NULL;
  172. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  173. goto error;
  174. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  175. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  176. goto error;
  177. if (!os_strcmp(entry.key, "period") &&
  178. (entry.type == DBUS_TYPE_INT32))
  179. period = entry.uint32_value;
  180. else if (!os_strcmp(entry.key, "interval") &&
  181. (entry.type == DBUS_TYPE_INT32))
  182. interval = entry.uint32_value;
  183. else
  184. goto error_clear;
  185. wpa_dbus_dict_entry_clear(&entry);
  186. }
  187. if (wpas_p2p_ext_listen(wpa_s, period, interval))
  188. return wpas_dbus_error_unknown_error(
  189. message, "failed to initiate a p2p_ext_listen.");
  190. return NULL;
  191. error_clear:
  192. wpa_dbus_dict_entry_clear(&entry);
  193. error:
  194. return wpas_dbus_error_invalid_args(message, entry.key);
  195. }
  196. DBusMessage * wpas_dbus_handler_p2p_presence_request(
  197. DBusMessage *message, struct wpa_supplicant *wpa_s)
  198. {
  199. unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
  200. struct wpa_dbus_dict_entry entry;
  201. DBusMessageIter iter;
  202. DBusMessageIter iter_dict;
  203. dbus_message_iter_init(message, &iter);
  204. entry.key = NULL;
  205. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  206. goto error;
  207. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  208. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  209. goto error;
  210. if (!os_strcmp(entry.key, "duration1") &&
  211. (entry.type == DBUS_TYPE_INT32))
  212. dur1 = entry.uint32_value;
  213. else if (!os_strcmp(entry.key, "interval1") &&
  214. entry.type == DBUS_TYPE_INT32)
  215. int1 = entry.uint32_value;
  216. else if (!os_strcmp(entry.key, "duration2") &&
  217. entry.type == DBUS_TYPE_INT32)
  218. dur2 = entry.uint32_value;
  219. else if (!os_strcmp(entry.key, "interval2") &&
  220. entry.type == DBUS_TYPE_INT32)
  221. int2 = entry.uint32_value;
  222. else
  223. goto error_clear;
  224. wpa_dbus_dict_entry_clear(&entry);
  225. }
  226. if (wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2) < 0)
  227. return wpas_dbus_error_unknown_error(message,
  228. "Failed to invoke presence request.");
  229. return NULL;
  230. error_clear:
  231. wpa_dbus_dict_entry_clear(&entry);
  232. error:
  233. return wpas_dbus_error_invalid_args(message, entry.key);
  234. }
  235. DBusMessage * wpas_dbus_handler_p2p_group_add(DBusMessage *message,
  236. struct wpa_supplicant *wpa_s)
  237. {
  238. DBusMessageIter iter_dict;
  239. DBusMessage *reply = NULL;
  240. DBusMessageIter iter;
  241. struct wpa_dbus_dict_entry entry;
  242. char *pg_object_path = NULL;
  243. int persistent_group = 0;
  244. int freq = 0;
  245. char *iface = NULL;
  246. char *net_id_str = NULL;
  247. unsigned int group_id = 0;
  248. struct wpa_ssid *ssid;
  249. dbus_message_iter_init(message, &iter);
  250. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  251. goto inv_args;
  252. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  253. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  254. goto inv_args;
  255. if (!os_strcmp(entry.key, "persistent") &&
  256. (entry.type == DBUS_TYPE_BOOLEAN)) {
  257. persistent_group = (entry.bool_value == TRUE) ? 1 : 0;
  258. } else if (!os_strcmp(entry.key, "frequency") &&
  259. (entry.type == DBUS_TYPE_INT32)) {
  260. freq = entry.int32_value;
  261. if (freq <= 0)
  262. goto inv_args_clear;
  263. } else if (!os_strcmp(entry.key, "persistent_group_object") &&
  264. entry.type == DBUS_TYPE_OBJECT_PATH)
  265. pg_object_path = os_strdup(entry.str_value);
  266. else
  267. goto inv_args_clear;
  268. wpa_dbus_dict_entry_clear(&entry);
  269. }
  270. if (pg_object_path != NULL) {
  271. /*
  272. * A persistent group Object Path is defined meaning we want
  273. * to re-invoke a persistent group.
  274. */
  275. iface = wpas_dbus_new_decompose_object_path(pg_object_path, 1,
  276. &net_id_str, NULL);
  277. if (iface == NULL ||
  278. os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
  279. reply =
  280. wpas_dbus_error_invalid_args(message,
  281. pg_object_path);
  282. goto out;
  283. }
  284. group_id = strtoul(net_id_str, NULL, 10);
  285. if (errno == EINVAL) {
  286. reply = wpas_dbus_error_invalid_args(
  287. message, pg_object_path);
  288. goto out;
  289. }
  290. /* Get the SSID structure from the persistent group id */
  291. ssid = wpa_config_get_network(wpa_s->conf, group_id);
  292. if (ssid == NULL || ssid->disabled != 2)
  293. goto inv_args;
  294. if (wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq)) {
  295. reply = wpas_dbus_error_unknown_error(
  296. message,
  297. "Failed to reinvoke a persistent group");
  298. goto out;
  299. }
  300. } else if (wpas_p2p_group_add(wpa_s, persistent_group, freq))
  301. goto inv_args;
  302. out:
  303. os_free(pg_object_path);
  304. os_free(net_id_str);
  305. os_free(iface);
  306. return reply;
  307. inv_args_clear:
  308. wpa_dbus_dict_entry_clear(&entry);
  309. inv_args:
  310. reply = wpas_dbus_error_invalid_args(message, NULL);
  311. goto out;
  312. }
  313. DBusMessage * wpas_dbus_handler_p2p_disconnect(DBusMessage *message,
  314. struct wpa_supplicant *wpa_s)
  315. {
  316. if (wpas_p2p_disconnect(wpa_s))
  317. return wpas_dbus_error_unknown_error(message,
  318. "failed to disconnect");
  319. return NULL;
  320. }
  321. static dbus_bool_t wpa_dbus_p2p_check_enabled(struct wpa_supplicant *wpa_s,
  322. DBusMessage *message,
  323. DBusMessage **out_reply,
  324. DBusError *error)
  325. {
  326. /* Return an error message or an error if P2P isn't available */
  327. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
  328. if (out_reply) {
  329. *out_reply = dbus_message_new_error(
  330. message, DBUS_ERROR_FAILED,
  331. "P2P is not available for this interface");
  332. }
  333. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  334. "P2P is not available for this "
  335. "interface");
  336. return FALSE;
  337. }
  338. return TRUE;
  339. }
  340. DBusMessage * wpas_dbus_handler_p2p_flush(DBusMessage *message,
  341. struct wpa_supplicant *wpa_s)
  342. {
  343. DBusMessage *reply = NULL;
  344. if (!wpa_dbus_p2p_check_enabled(wpa_s, message, &reply, NULL))
  345. return reply;
  346. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  347. wpa_s->force_long_sd = 0;
  348. p2p_flush(wpa_s->global->p2p);
  349. return NULL;
  350. }
  351. DBusMessage * wpas_dbus_handler_p2p_connect(DBusMessage *message,
  352. struct wpa_supplicant *wpa_s)
  353. {
  354. DBusMessageIter iter_dict;
  355. DBusMessage *reply = NULL;
  356. DBusMessageIter iter;
  357. struct wpa_dbus_dict_entry entry;
  358. char *peer_object_path = NULL;
  359. int persistent_group = 0;
  360. int join = 0;
  361. int authorize_only = 0;
  362. int go_intent = -1;
  363. int freq = 0;
  364. u8 addr[ETH_ALEN];
  365. char *pin = NULL;
  366. enum p2p_wps_method wps_method = WPS_NOT_READY;
  367. int new_pin;
  368. char *err_msg = NULL;
  369. char *iface = NULL;
  370. if (!wpa_dbus_p2p_check_enabled(wpa_s, message, &reply, NULL))
  371. return reply;
  372. dbus_message_iter_init(message, &iter);
  373. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  374. goto inv_args;
  375. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  376. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  377. goto inv_args;
  378. if (!os_strcmp(entry.key, "peer") &&
  379. (entry.type == DBUS_TYPE_OBJECT_PATH)) {
  380. peer_object_path = os_strdup(entry.str_value);
  381. } else if (!os_strcmp(entry.key, "persistent") &&
  382. (entry.type == DBUS_TYPE_BOOLEAN)) {
  383. persistent_group = (entry.bool_value == TRUE) ? 1 : 0;
  384. } else if (!os_strcmp(entry.key, "join") &&
  385. (entry.type == DBUS_TYPE_BOOLEAN)) {
  386. join = (entry.bool_value == TRUE) ? 1 : 0;
  387. } else if (!os_strcmp(entry.key, "authorize_only") &&
  388. (entry.type == DBUS_TYPE_BOOLEAN)) {
  389. authorize_only = (entry.bool_value == TRUE) ? 1 : 0;
  390. } else if (!os_strcmp(entry.key, "frequency") &&
  391. (entry.type == DBUS_TYPE_INT32)) {
  392. freq = entry.int32_value;
  393. if (freq <= 0)
  394. goto inv_args_clear;
  395. } else if (!os_strcmp(entry.key, "go_intent") &&
  396. (entry.type == DBUS_TYPE_INT32)) {
  397. go_intent = entry.int32_value;
  398. if ((go_intent < 0) || (go_intent > 15))
  399. goto inv_args_clear;
  400. } else if (!os_strcmp(entry.key, "wps_method") &&
  401. (entry.type == DBUS_TYPE_STRING)) {
  402. if (!os_strcmp(entry.str_value, "pbc"))
  403. wps_method = WPS_PBC;
  404. else if (!os_strcmp(entry.str_value, "pin"))
  405. wps_method = WPS_PIN_DISPLAY;
  406. else if (!os_strcmp(entry.str_value, "display"))
  407. wps_method = WPS_PIN_DISPLAY;
  408. else if (!os_strcmp(entry.str_value, "keypad"))
  409. wps_method = WPS_PIN_KEYPAD;
  410. else
  411. goto inv_args_clear;
  412. } else if (!os_strcmp(entry.key, "pin") &&
  413. (entry.type == DBUS_TYPE_STRING)) {
  414. pin = os_strdup(entry.str_value);
  415. } else
  416. goto inv_args_clear;
  417. wpa_dbus_dict_entry_clear(&entry);
  418. }
  419. if (!peer_object_path || (wps_method == WPS_NOT_READY) ||
  420. (parse_peer_object_path(peer_object_path, addr) < 0) ||
  421. !p2p_peer_known(wpa_s->global->p2p, addr))
  422. goto inv_args;
  423. /*
  424. * Validate the wps_method specified and the pin value.
  425. */
  426. if ((!pin || !pin[0]) && (wps_method == WPS_PIN_KEYPAD))
  427. goto inv_args;
  428. new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
  429. persistent_group, join, authorize_only,
  430. go_intent, freq);
  431. if (new_pin >= 0) {
  432. char npin[9];
  433. char *generated_pin;
  434. os_snprintf(npin, sizeof(npin), "%08d", new_pin);
  435. generated_pin = npin;
  436. reply = dbus_message_new_method_return(message);
  437. dbus_message_append_args(reply, DBUS_TYPE_STRING,
  438. &generated_pin, DBUS_TYPE_INVALID);
  439. } else {
  440. switch (new_pin) {
  441. case -2:
  442. err_msg = "connect failed due to channel "
  443. "unavailability.";
  444. iface = WPAS_DBUS_ERROR_CONNECT_CHANNEL_UNAVAILABLE;
  445. break;
  446. case -3:
  447. err_msg = "connect failed due to unsupported channel.";
  448. iface = WPAS_DBUS_ERROR_CONNECT_CHANNEL_UNSUPPORTED;
  449. break;
  450. default:
  451. err_msg = "connect failed due to unspecified error.";
  452. iface = WPAS_DBUS_ERROR_CONNECT_UNSPECIFIED_ERROR;
  453. break;
  454. }
  455. /*
  456. * TODO:
  457. * Do we need specialized errors corresponding to above
  458. * error conditions as against just returning a different
  459. * error message?
  460. */
  461. reply = dbus_message_new_error(message, iface, err_msg);
  462. }
  463. out:
  464. os_free(peer_object_path);
  465. os_free(pin);
  466. return reply;
  467. inv_args_clear:
  468. wpa_dbus_dict_entry_clear(&entry);
  469. inv_args:
  470. reply = wpas_dbus_error_invalid_args(message, NULL);
  471. goto out;
  472. }
  473. DBusMessage * wpas_dbus_handler_p2p_invite(DBusMessage *message,
  474. struct wpa_supplicant *wpa_s)
  475. {
  476. DBusMessageIter iter_dict;
  477. DBusMessage *reply = NULL;
  478. DBusMessageIter iter;
  479. struct wpa_dbus_dict_entry entry;
  480. char *peer_object_path = NULL;
  481. char *pg_object_path = NULL;
  482. char *iface = NULL;
  483. char *net_id_str = NULL;
  484. u8 peer_addr[ETH_ALEN];
  485. unsigned int group_id = 0;
  486. int persistent = 0;
  487. struct wpa_ssid *ssid;
  488. if (!wpa_dbus_p2p_check_enabled(wpa_s, message, &reply, NULL))
  489. return reply;
  490. dbus_message_iter_init(message, &iter);
  491. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  492. goto err;
  493. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  494. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  495. goto err;
  496. if (!os_strcmp(entry.key, "peer") &&
  497. (entry.type == DBUS_TYPE_OBJECT_PATH)) {
  498. peer_object_path = os_strdup(entry.str_value);
  499. wpa_dbus_dict_entry_clear(&entry);
  500. } else if (!os_strcmp(entry.key, "persistent_group_object") &&
  501. (entry.type == DBUS_TYPE_OBJECT_PATH)) {
  502. pg_object_path = os_strdup(entry.str_value);
  503. persistent = 1;
  504. wpa_dbus_dict_entry_clear(&entry);
  505. } else {
  506. wpa_dbus_dict_entry_clear(&entry);
  507. goto err;
  508. }
  509. }
  510. if (!peer_object_path ||
  511. (parse_peer_object_path(peer_object_path, peer_addr) < 0) ||
  512. !p2p_peer_known(wpa_s->global->p2p, peer_addr)) {
  513. goto err;
  514. }
  515. if (persistent) {
  516. /*
  517. * A group ID is defined meaning we want to re-invoke a
  518. * persistent group
  519. */
  520. iface = wpas_dbus_new_decompose_object_path(pg_object_path, 1,
  521. &net_id_str, NULL);
  522. if (iface == NULL ||
  523. os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
  524. reply = wpas_dbus_error_invalid_args(message,
  525. pg_object_path);
  526. goto out;
  527. }
  528. group_id = strtoul(net_id_str, NULL, 10);
  529. if (errno == EINVAL) {
  530. reply = wpas_dbus_error_invalid_args(
  531. message, pg_object_path);
  532. goto out;
  533. }
  534. /* Get the SSID structure from the persistent group id */
  535. ssid = wpa_config_get_network(wpa_s->conf, group_id);
  536. if (ssid == NULL || ssid->disabled != 2)
  537. goto err;
  538. if (wpas_p2p_invite(wpa_s, peer_addr, ssid, NULL) < 0) {
  539. reply = wpas_dbus_error_unknown_error(
  540. message,
  541. "Failed to reinvoke a persistent group");
  542. goto out;
  543. }
  544. } else {
  545. /*
  546. * No group ID means propose to a peer to join my active group
  547. */
  548. if (wpas_p2p_invite_group(wpa_s, wpa_s->ifname,
  549. peer_addr, NULL)) {
  550. reply = wpas_dbus_error_unknown_error(
  551. message, "Failed to join to an active group");
  552. goto out;
  553. }
  554. }
  555. out:
  556. os_free(pg_object_path);
  557. os_free(peer_object_path);
  558. return reply;
  559. err:
  560. reply = wpas_dbus_error_invalid_args(message, NULL);
  561. goto out;
  562. }
  563. DBusMessage * wpas_dbus_handler_p2p_prov_disc_req(DBusMessage *message,
  564. struct wpa_supplicant *wpa_s)
  565. {
  566. DBusMessageIter iter;
  567. char *peer_object_path = NULL;
  568. char *config_method = NULL;
  569. u8 peer_addr[ETH_ALEN];
  570. dbus_message_iter_init(message, &iter);
  571. dbus_message_iter_get_basic(&iter, &peer_object_path);
  572. if (parse_peer_object_path(peer_object_path, peer_addr) < 0)
  573. return wpas_dbus_error_invalid_args(message, NULL);
  574. dbus_message_iter_next(&iter);
  575. dbus_message_iter_get_basic(&iter, &config_method);
  576. /*
  577. * Validation checks on config_method are being duplicated here
  578. * to be able to return invalid args reply since the error code
  579. * from p2p module are not granular enough (yet).
  580. */
  581. if (os_strcmp(config_method, "display") &&
  582. os_strcmp(config_method, "keypad") &&
  583. os_strcmp(config_method, "pbc") &&
  584. os_strcmp(config_method, "pushbutton"))
  585. return wpas_dbus_error_invalid_args(message, NULL);
  586. if (wpas_p2p_prov_disc(wpa_s, peer_addr, config_method, 0) < 0)
  587. return wpas_dbus_error_unknown_error(message,
  588. "Failed to send provision discovery request");
  589. return NULL;
  590. }
  591. /*
  592. * P2P Device property accessor methods.
  593. */
  594. dbus_bool_t wpas_dbus_getter_p2p_device_properties(DBusMessageIter *iter,
  595. DBusError *error,
  596. void *user_data)
  597. {
  598. struct wpa_supplicant *wpa_s = user_data;
  599. DBusMessageIter variant_iter, dict_iter;
  600. DBusMessageIter iter_secdev_dict_entry, iter_secdev_dict_val,
  601. iter_secdev_dict_array;
  602. const char *dev_name;
  603. int num_vendor_extensions = 0;
  604. int i;
  605. const struct wpabuf *vendor_ext[P2P_MAX_WPS_VENDOR_EXT];
  606. if (!wpa_dbus_p2p_check_enabled(wpa_s, NULL, NULL, error))
  607. return FALSE;
  608. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  609. "a{sv}", &variant_iter) ||
  610. !wpa_dbus_dict_open_write(&variant_iter, &dict_iter))
  611. goto err_no_mem;
  612. /* DeviceName */
  613. dev_name = wpa_s->conf->device_name;
  614. if (dev_name &&
  615. !wpa_dbus_dict_append_string(&dict_iter, "DeviceName", dev_name))
  616. goto err_no_mem;
  617. /* Primary device type */
  618. if (!wpa_dbus_dict_append_byte_array(&dict_iter, "PrimaryDeviceType",
  619. (char *)wpa_s->conf->device_type,
  620. WPS_DEV_TYPE_LEN))
  621. goto err_no_mem;
  622. /* Secondary device types */
  623. if (wpa_s->conf->num_sec_device_types) {
  624. if (!wpa_dbus_dict_begin_array(&dict_iter,
  625. "SecondaryDeviceTypes",
  626. DBUS_TYPE_ARRAY_AS_STRING
  627. DBUS_TYPE_BYTE_AS_STRING,
  628. &iter_secdev_dict_entry,
  629. &iter_secdev_dict_val,
  630. &iter_secdev_dict_array))
  631. goto err_no_mem;
  632. for (i = 0; i < wpa_s->conf->num_sec_device_types; i++)
  633. wpa_dbus_dict_bin_array_add_element(
  634. &iter_secdev_dict_array,
  635. wpa_s->conf->sec_device_type[i],
  636. WPS_DEV_TYPE_LEN);
  637. if (!wpa_dbus_dict_end_array(&dict_iter,
  638. &iter_secdev_dict_entry,
  639. &iter_secdev_dict_val,
  640. &iter_secdev_dict_array))
  641. goto err_no_mem;
  642. }
  643. /* Vendor Extensions */
  644. for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
  645. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  646. continue;
  647. vendor_ext[num_vendor_extensions++] =
  648. wpa_s->conf->wps_vendor_ext[i];
  649. }
  650. if (num_vendor_extensions &&
  651. !wpa_dbus_dict_append_wpabuf_array(&dict_iter,
  652. "VendorExtension",
  653. vendor_ext,
  654. num_vendor_extensions))
  655. goto err_no_mem;
  656. /* GO Intent */
  657. if (!wpa_dbus_dict_append_uint32(&dict_iter, "GOIntent",
  658. wpa_s->conf->p2p_go_intent))
  659. goto err_no_mem;
  660. /* Persistent Reconnect */
  661. if (!wpa_dbus_dict_append_bool(&dict_iter, "PersistantReconnect",
  662. wpa_s->conf->persistent_reconnect))
  663. goto err_no_mem;
  664. /* Listen Reg Class */
  665. if (!wpa_dbus_dict_append_uint32(&dict_iter, "ListenRegClass",
  666. wpa_s->conf->p2p_listen_reg_class))
  667. goto err_no_mem;
  668. /* Listen Channel */
  669. if (!wpa_dbus_dict_append_uint32(&dict_iter, "ListenChannel",
  670. wpa_s->conf->p2p_listen_channel))
  671. goto err_no_mem;
  672. /* Oper Reg Class */
  673. if (!wpa_dbus_dict_append_uint32(&dict_iter, "OperRegClass",
  674. wpa_s->conf->p2p_oper_reg_class))
  675. goto err_no_mem;
  676. /* Oper Channel */
  677. if (!wpa_dbus_dict_append_uint32(&dict_iter, "OperChannel",
  678. wpa_s->conf->p2p_oper_channel))
  679. goto err_no_mem;
  680. /* SSID Postfix */
  681. if (wpa_s->conf->p2p_ssid_postfix &&
  682. !wpa_dbus_dict_append_string(&dict_iter, "SsidPostfix",
  683. wpa_s->conf->p2p_ssid_postfix))
  684. goto err_no_mem;
  685. /* Intra Bss */
  686. if (!wpa_dbus_dict_append_bool(&dict_iter, "IntraBss",
  687. wpa_s->conf->p2p_intra_bss))
  688. goto err_no_mem;
  689. /* Group Idle */
  690. if (!wpa_dbus_dict_append_uint32(&dict_iter, "GroupIdle",
  691. wpa_s->conf->p2p_group_idle))
  692. goto err_no_mem;
  693. /* Dissasociation low ack */
  694. if (!wpa_dbus_dict_append_uint32(&dict_iter, "disassoc_low_ack",
  695. wpa_s->conf->disassoc_low_ack))
  696. goto err_no_mem;
  697. if (!wpa_dbus_dict_close_write(&variant_iter, &dict_iter) ||
  698. !dbus_message_iter_close_container(iter, &variant_iter))
  699. goto err_no_mem;
  700. return TRUE;
  701. err_no_mem:
  702. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  703. return FALSE;
  704. }
  705. dbus_bool_t wpas_dbus_setter_p2p_device_properties(DBusMessageIter *iter,
  706. DBusError *error,
  707. void *user_data)
  708. {
  709. struct wpa_supplicant *wpa_s = user_data;
  710. DBusMessageIter variant_iter, iter_dict;
  711. struct wpa_dbus_dict_entry entry = {.type = DBUS_TYPE_STRING };
  712. unsigned int i;
  713. if (!wpa_dbus_p2p_check_enabled(wpa_s, NULL, NULL, error))
  714. return FALSE;
  715. dbus_message_iter_recurse(iter, &variant_iter);
  716. if (!wpa_dbus_dict_open_read(&variant_iter, &iter_dict, error))
  717. return FALSE;
  718. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  719. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
  720. dbus_set_error_const(error, DBUS_ERROR_INVALID_ARGS,
  721. "invalid message format");
  722. return FALSE;
  723. }
  724. if (os_strcmp(entry.key, "DeviceName") == 0) {
  725. char *devname;
  726. if (entry.type != DBUS_TYPE_STRING)
  727. goto error;
  728. devname = os_strdup(entry.str_value);
  729. if (devname == NULL)
  730. goto err_no_mem_clear;
  731. os_free(wpa_s->conf->device_name);
  732. wpa_s->conf->device_name = devname;
  733. wpa_s->conf->changed_parameters |=
  734. CFG_CHANGED_DEVICE_NAME;
  735. } else if (os_strcmp(entry.key, "PrimaryDeviceType") == 0) {
  736. if (entry.type != DBUS_TYPE_ARRAY ||
  737. entry.array_type != DBUS_TYPE_BYTE ||
  738. entry.array_len != WPS_DEV_TYPE_LEN)
  739. goto error;
  740. os_memcpy(wpa_s->conf->device_type,
  741. entry.bytearray_value,
  742. WPS_DEV_TYPE_LEN);
  743. wpa_s->conf->changed_parameters |=
  744. CFG_CHANGED_DEVICE_TYPE;
  745. } else if (os_strcmp(entry.key, "SecondaryDeviceTypes") == 0) {
  746. if (entry.type != DBUS_TYPE_ARRAY ||
  747. entry.array_type != WPAS_DBUS_TYPE_BINARRAY ||
  748. entry.array_len > MAX_SEC_DEVICE_TYPES)
  749. goto error;
  750. for (i = 0; i < entry.array_len; i++)
  751. if (wpabuf_len(entry.binarray_value[i]) !=
  752. WPS_DEV_TYPE_LEN)
  753. goto err_no_mem_clear;
  754. for (i = 0; i < entry.array_len; i++)
  755. os_memcpy(wpa_s->conf->sec_device_type[i],
  756. wpabuf_head(entry.binarray_value[i]),
  757. WPS_DEV_TYPE_LEN);
  758. wpa_s->conf->num_sec_device_types = entry.array_len;
  759. wpa_s->conf->changed_parameters |=
  760. CFG_CHANGED_SEC_DEVICE_TYPE;
  761. } else if (os_strcmp(entry.key, "VendorExtension") == 0) {
  762. if ((entry.type != DBUS_TYPE_ARRAY) ||
  763. (entry.array_type != WPAS_DBUS_TYPE_BINARRAY) ||
  764. (entry.array_len > P2P_MAX_WPS_VENDOR_EXT))
  765. goto error;
  766. wpa_s->conf->changed_parameters |=
  767. CFG_CHANGED_VENDOR_EXTENSION;
  768. for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
  769. wpabuf_free(wpa_s->conf->wps_vendor_ext[i]);
  770. if (i < entry.array_len) {
  771. wpa_s->conf->wps_vendor_ext[i] =
  772. entry.binarray_value[i];
  773. entry.binarray_value[i] = NULL;
  774. } else
  775. wpa_s->conf->wps_vendor_ext[i] = NULL;
  776. }
  777. } else if ((os_strcmp(entry.key, "GOIntent") == 0) &&
  778. (entry.type == DBUS_TYPE_UINT32) &&
  779. (entry.uint32_value <= 15))
  780. wpa_s->conf->p2p_go_intent = entry.uint32_value;
  781. else if ((os_strcmp(entry.key, "PersistantReconnect") == 0) &&
  782. (entry.type == DBUS_TYPE_BOOLEAN))
  783. wpa_s->conf->persistent_reconnect = entry.bool_value;
  784. else if ((os_strcmp(entry.key, "ListenRegClass") == 0) &&
  785. (entry.type == DBUS_TYPE_UINT32)) {
  786. wpa_s->conf->p2p_listen_reg_class = entry.uint32_value;
  787. wpa_s->conf->changed_parameters |=
  788. CFG_CHANGED_P2P_LISTEN_CHANNEL;
  789. } else if ((os_strcmp(entry.key, "ListenChannel") == 0) &&
  790. (entry.type == DBUS_TYPE_UINT32)) {
  791. wpa_s->conf->p2p_listen_channel = entry.uint32_value;
  792. wpa_s->conf->changed_parameters |=
  793. CFG_CHANGED_P2P_LISTEN_CHANNEL;
  794. } else if ((os_strcmp(entry.key, "OperRegClass") == 0) &&
  795. (entry.type == DBUS_TYPE_UINT32)) {
  796. wpa_s->conf->p2p_oper_reg_class = entry.uint32_value;
  797. wpa_s->conf->changed_parameters |=
  798. CFG_CHANGED_P2P_OPER_CHANNEL;
  799. } else if ((os_strcmp(entry.key, "OperChannel") == 0) &&
  800. (entry.type == DBUS_TYPE_UINT32)) {
  801. wpa_s->conf->p2p_oper_channel = entry.uint32_value;
  802. wpa_s->conf->changed_parameters |=
  803. CFG_CHANGED_P2P_OPER_CHANNEL;
  804. } else if (os_strcmp(entry.key, "SsidPostfix") == 0) {
  805. char *postfix;
  806. if (entry.type != DBUS_TYPE_STRING)
  807. goto error;
  808. postfix = os_strdup(entry.str_value);
  809. if (!postfix)
  810. goto err_no_mem_clear;
  811. os_free(wpa_s->conf->p2p_ssid_postfix);
  812. wpa_s->conf->p2p_ssid_postfix = postfix;
  813. wpa_s->conf->changed_parameters |=
  814. CFG_CHANGED_P2P_SSID_POSTFIX;
  815. } else if ((os_strcmp(entry.key, "IntraBss") == 0) &&
  816. (entry.type == DBUS_TYPE_BOOLEAN)) {
  817. wpa_s->conf->p2p_intra_bss = entry.bool_value;
  818. wpa_s->conf->changed_parameters |=
  819. CFG_CHANGED_P2P_INTRA_BSS;
  820. } else if ((os_strcmp(entry.key, "GroupIdle") == 0) &&
  821. (entry.type == DBUS_TYPE_UINT32))
  822. wpa_s->conf->p2p_group_idle = entry.uint32_value;
  823. else if (os_strcmp(entry.key, "disassoc_low_ack") == 0 &&
  824. entry.type == DBUS_TYPE_UINT32)
  825. wpa_s->conf->disassoc_low_ack = entry.uint32_value;
  826. else
  827. goto error;
  828. wpa_dbus_dict_entry_clear(&entry);
  829. }
  830. if (wpa_s->conf->changed_parameters) {
  831. /* Some changed parameters requires to update config*/
  832. wpa_supplicant_update_config(wpa_s);
  833. }
  834. return TRUE;
  835. error:
  836. dbus_set_error_const(error, DBUS_ERROR_INVALID_ARGS,
  837. "invalid message format");
  838. wpa_dbus_dict_entry_clear(&entry);
  839. return FALSE;
  840. err_no_mem_clear:
  841. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  842. wpa_dbus_dict_entry_clear(&entry);
  843. return FALSE;
  844. }
  845. dbus_bool_t wpas_dbus_getter_p2p_peers(DBusMessageIter *iter, DBusError *error,
  846. void *user_data)
  847. {
  848. struct wpa_supplicant *wpa_s = user_data;
  849. struct p2p_data *p2p = wpa_s->global->p2p;
  850. int next = 0, i = 0;
  851. int num = 0, out_of_mem = 0;
  852. const u8 *addr;
  853. const struct p2p_peer_info *peer_info = NULL;
  854. dbus_bool_t success = FALSE;
  855. struct dl_list peer_objpath_list;
  856. struct peer_objpath_node {
  857. struct dl_list list;
  858. char path[WPAS_DBUS_OBJECT_PATH_MAX];
  859. } *node, *tmp;
  860. char **peer_obj_paths = NULL;
  861. if (!wpa_dbus_p2p_check_enabled(wpa_s, NULL, NULL, error))
  862. return FALSE;
  863. dl_list_init(&peer_objpath_list);
  864. /* Get the first peer info */
  865. peer_info = p2p_get_peer_found(p2p, NULL, next);
  866. /* Get next and accumulate them */
  867. next = 1;
  868. while (peer_info != NULL) {
  869. node = os_zalloc(sizeof(struct peer_objpath_node));
  870. if (!node) {
  871. out_of_mem = 1;
  872. goto error;
  873. }
  874. addr = peer_info->p2p_device_addr;
  875. os_snprintf(node->path, WPAS_DBUS_OBJECT_PATH_MAX,
  876. "%s/" WPAS_DBUS_NEW_P2P_PEERS_PART
  877. "/" COMPACT_MACSTR,
  878. wpa_s->dbus_new_path, MAC2STR(addr));
  879. dl_list_add_tail(&peer_objpath_list, &node->list);
  880. num++;
  881. peer_info = p2p_get_peer_found(p2p, addr, next);
  882. }
  883. /*
  884. * Now construct the peer object paths in a form suitable for
  885. * array_property_getter helper below.
  886. */
  887. peer_obj_paths = os_zalloc(num * sizeof(char *));
  888. if (!peer_obj_paths) {
  889. out_of_mem = 1;
  890. goto error;
  891. }
  892. dl_list_for_each_safe(node, tmp, &peer_objpath_list,
  893. struct peer_objpath_node, list)
  894. peer_obj_paths[i++] = node->path;
  895. success = wpas_dbus_simple_array_property_getter(iter,
  896. DBUS_TYPE_OBJECT_PATH,
  897. peer_obj_paths, num,
  898. error);
  899. error:
  900. if (peer_obj_paths)
  901. os_free(peer_obj_paths);
  902. dl_list_for_each_safe(node, tmp, &peer_objpath_list,
  903. struct peer_objpath_node, list) {
  904. dl_list_del(&node->list);
  905. os_free(node);
  906. }
  907. if (out_of_mem)
  908. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  909. return success;
  910. }
  911. enum wpas_p2p_role {
  912. WPAS_P2P_ROLE_DEVICE,
  913. WPAS_P2P_ROLE_GO,
  914. WPAS_P2P_ROLE_CLIENT,
  915. };
  916. static enum wpas_p2p_role wpas_get_p2p_role(struct wpa_supplicant *wpa_s)
  917. {
  918. struct wpa_ssid *ssid = wpa_s->current_ssid;
  919. if (!ssid)
  920. return WPAS_P2P_ROLE_DEVICE;
  921. if (wpa_s->wpa_state != WPA_COMPLETED)
  922. return WPAS_P2P_ROLE_DEVICE;
  923. switch (ssid->mode) {
  924. case WPAS_MODE_P2P_GO:
  925. case WPAS_MODE_P2P_GROUP_FORMATION:
  926. return WPAS_P2P_ROLE_GO;
  927. case WPAS_MODE_INFRA:
  928. if (ssid->p2p_group)
  929. return WPAS_P2P_ROLE_CLIENT;
  930. return WPAS_P2P_ROLE_DEVICE;
  931. default:
  932. return WPAS_P2P_ROLE_DEVICE;
  933. }
  934. }
  935. dbus_bool_t wpas_dbus_getter_p2p_role(DBusMessageIter *iter, DBusError *error,
  936. void *user_data)
  937. {
  938. struct wpa_supplicant *wpa_s = user_data;
  939. char *str;
  940. switch (wpas_get_p2p_role(wpa_s)) {
  941. case WPAS_P2P_ROLE_GO:
  942. str = "GO";
  943. break;
  944. case WPAS_P2P_ROLE_CLIENT:
  945. str = "client";
  946. break;
  947. default:
  948. str = "device";
  949. }
  950. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING, &str,
  951. error);
  952. }
  953. dbus_bool_t wpas_dbus_getter_p2p_group(DBusMessageIter *iter, DBusError *error,
  954. void *user_data)
  955. {
  956. struct wpa_supplicant *wpa_s = user_data;
  957. if (wpa_s->dbus_groupobj_path == NULL)
  958. return FALSE;
  959. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_OBJECT_PATH,
  960. &wpa_s->dbus_groupobj_path,
  961. error);
  962. }
  963. dbus_bool_t wpas_dbus_getter_p2p_peergo(DBusMessageIter *iter,
  964. DBusError *error, void *user_data)
  965. {
  966. struct wpa_supplicant *wpa_s = user_data;
  967. char go_peer_obj_path[WPAS_DBUS_OBJECT_PATH_MAX], *path;
  968. if (wpas_get_p2p_role(wpa_s) != WPAS_P2P_ROLE_CLIENT)
  969. return FALSE;
  970. os_snprintf(go_peer_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  971. "%s/" WPAS_DBUS_NEW_P2P_PEERS_PART "/" COMPACT_MACSTR,
  972. wpa_s->dbus_new_path, MAC2STR(wpa_s->go_dev_addr));
  973. path = go_peer_obj_path;
  974. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_OBJECT_PATH,
  975. &path, error);
  976. }
  977. /*
  978. * Peer object properties accessor methods
  979. */
  980. dbus_bool_t wpas_dbus_getter_p2p_peer_device_name(DBusMessageIter *iter,
  981. DBusError *error,
  982. void *user_data)
  983. {
  984. struct peer_handler_args *peer_args = user_data;
  985. const struct p2p_peer_info *info;
  986. char *tmp;
  987. if (!wpa_dbus_p2p_check_enabled(peer_args->wpa_s, NULL, NULL, error))
  988. return FALSE;
  989. /* get the peer info */
  990. info = p2p_get_peer_found(peer_args->wpa_s->global->p2p,
  991. peer_args->p2p_device_addr, 0);
  992. if (info == NULL) {
  993. dbus_set_error(error, DBUS_ERROR_FAILED,
  994. "failed to find peer");
  995. return FALSE;
  996. }
  997. tmp = os_strdup(info->device_name);
  998. if (!tmp) {
  999. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  1000. return FALSE;
  1001. }
  1002. if (!wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING, &tmp,
  1003. error)) {
  1004. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  1005. os_free(tmp);
  1006. return FALSE;
  1007. }
  1008. os_free(tmp);
  1009. return TRUE;
  1010. }
  1011. dbus_bool_t wpas_dbus_getter_p2p_peer_primary_device_type(
  1012. DBusMessageIter *iter, DBusError *error, void *user_data)
  1013. {
  1014. struct peer_handler_args *peer_args = user_data;
  1015. const struct p2p_peer_info *info;
  1016. info = p2p_get_peer_found(peer_args->wpa_s->global->p2p,
  1017. peer_args->p2p_device_addr, 0);
  1018. if (info == NULL) {
  1019. dbus_set_error(error, DBUS_ERROR_FAILED,
  1020. "failed to find peer");
  1021. return FALSE;
  1022. }
  1023. if (!wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
  1024. (char *)
  1025. info->pri_dev_type,
  1026. WPS_DEV_TYPE_LEN, error)) {
  1027. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  1028. return FALSE;
  1029. }
  1030. return TRUE;
  1031. }
  1032. dbus_bool_t wpas_dbus_getter_p2p_peer_config_method(DBusMessageIter *iter,
  1033. DBusError *error,
  1034. void *user_data)
  1035. {
  1036. struct peer_handler_args *peer_args = user_data;
  1037. const struct p2p_peer_info *info;
  1038. info = p2p_get_peer_found(peer_args->wpa_s->global->p2p,
  1039. peer_args->p2p_device_addr, 0);
  1040. if (info == NULL) {
  1041. dbus_set_error(error, DBUS_ERROR_FAILED,
  1042. "failed to find peer");
  1043. return FALSE;
  1044. }
  1045. if (!wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT16,
  1046. &info->config_methods, error)) {
  1047. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  1048. return FALSE;
  1049. }
  1050. return TRUE;
  1051. }
  1052. dbus_bool_t wpas_dbus_getter_p2p_peer_level(DBusMessageIter *iter,
  1053. DBusError *error,
  1054. void *user_data)
  1055. {
  1056. struct peer_handler_args *peer_args = user_data;
  1057. const struct p2p_peer_info *info;
  1058. info = p2p_get_peer_found(peer_args->wpa_s->global->p2p,
  1059. peer_args->p2p_device_addr, 0);
  1060. if (info == NULL) {
  1061. dbus_set_error(error, DBUS_ERROR_FAILED,
  1062. "failed to find peer");
  1063. return FALSE;
  1064. }
  1065. if (!wpas_dbus_simple_property_getter(iter, DBUS_TYPE_INT32,
  1066. &info->level, error)) {
  1067. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  1068. return FALSE;
  1069. }
  1070. return TRUE;
  1071. }
  1072. dbus_bool_t wpas_dbus_getter_p2p_peer_device_capability(DBusMessageIter *iter,
  1073. DBusError *error,
  1074. void *user_data)
  1075. {
  1076. struct peer_handler_args *peer_args = user_data;
  1077. const struct p2p_peer_info *info;
  1078. info = p2p_get_peer_found(peer_args->wpa_s->global->p2p,
  1079. peer_args->p2p_device_addr, 0);
  1080. if (info == NULL) {
  1081. dbus_set_error(error, DBUS_ERROR_FAILED,
  1082. "failed to find peer");
  1083. return FALSE;
  1084. }
  1085. if (!wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BYTE,
  1086. &info->dev_capab, error)) {
  1087. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  1088. return FALSE;
  1089. }
  1090. return TRUE;
  1091. }
  1092. dbus_bool_t wpas_dbus_getter_p2p_peer_group_capability(DBusMessageIter *iter,
  1093. DBusError *error,
  1094. void *user_data)
  1095. {
  1096. struct peer_handler_args *peer_args = user_data;
  1097. const struct p2p_peer_info *info;
  1098. info = p2p_get_peer_found(peer_args->wpa_s->global->p2p,
  1099. peer_args->p2p_device_addr, 0);
  1100. if (info == NULL) {
  1101. dbus_set_error(error, DBUS_ERROR_FAILED,
  1102. "failed to find peer");
  1103. return FALSE;
  1104. }
  1105. if (!wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BYTE,
  1106. &info->group_capab, error)) {
  1107. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  1108. return FALSE;
  1109. }
  1110. return TRUE;
  1111. }
  1112. dbus_bool_t wpas_dbus_getter_p2p_peer_secondary_device_types(
  1113. DBusMessageIter *iter, DBusError *error, void *user_data)
  1114. {
  1115. struct peer_handler_args *peer_args = user_data;
  1116. const struct p2p_peer_info *info;
  1117. DBusMessageIter variant_iter, array_iter;
  1118. info = p2p_get_peer_found(peer_args->wpa_s->global->p2p,
  1119. peer_args->p2p_device_addr, 0);
  1120. if (info == NULL) {
  1121. dbus_set_error(error, DBUS_ERROR_FAILED,
  1122. "failed to find peer");
  1123. return FALSE;
  1124. }
  1125. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  1126. DBUS_TYPE_ARRAY_AS_STRING
  1127. DBUS_TYPE_ARRAY_AS_STRING
  1128. DBUS_TYPE_BYTE_AS_STRING,
  1129. &variant_iter)) {
  1130. dbus_set_error(error, DBUS_ERROR_FAILED,
  1131. "%s: failed to construct message 1", __func__);
  1132. return FALSE;
  1133. }
  1134. if (!dbus_message_iter_open_container(&variant_iter, DBUS_TYPE_ARRAY,
  1135. DBUS_TYPE_ARRAY_AS_STRING
  1136. DBUS_TYPE_BYTE_AS_STRING,
  1137. &array_iter)) {
  1138. dbus_set_error(error, DBUS_ERROR_FAILED,
  1139. "%s: failed to construct message 2", __func__);
  1140. return FALSE;
  1141. }
  1142. if (info->wps_sec_dev_type_list_len) {
  1143. const u8 *sec_dev_type_list = info->wps_sec_dev_type_list;
  1144. int num_sec_device_types =
  1145. info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN;
  1146. int i;
  1147. DBusMessageIter inner_array_iter;
  1148. for (i = 0; i < num_sec_device_types; i++) {
  1149. if (!dbus_message_iter_open_container(
  1150. &array_iter, DBUS_TYPE_ARRAY,
  1151. DBUS_TYPE_BYTE_AS_STRING,
  1152. &inner_array_iter)) {
  1153. dbus_set_error(error, DBUS_ERROR_FAILED,
  1154. "%s: failed to construct "
  1155. "message 3 (%d)",
  1156. __func__, i);
  1157. return FALSE;
  1158. }
  1159. if (!dbus_message_iter_append_fixed_array(
  1160. &inner_array_iter, DBUS_TYPE_BYTE,
  1161. &sec_dev_type_list, WPS_DEV_TYPE_LEN)) {
  1162. dbus_set_error(error, DBUS_ERROR_FAILED,
  1163. "%s: failed to construct "
  1164. "message 4 (%d)",
  1165. __func__, i);
  1166. return FALSE;
  1167. }
  1168. if (!dbus_message_iter_close_container(
  1169. &array_iter, &inner_array_iter)) {
  1170. dbus_set_error(error, DBUS_ERROR_FAILED,
  1171. "%s: failed to construct "
  1172. "message 5 (%d)",
  1173. __func__, i);
  1174. return FALSE;
  1175. }
  1176. sec_dev_type_list += WPS_DEV_TYPE_LEN;
  1177. }
  1178. }
  1179. if (!dbus_message_iter_close_container(&variant_iter, &array_iter)) {
  1180. dbus_set_error(error, DBUS_ERROR_FAILED,
  1181. "%s: failed to construct message 6", __func__);
  1182. return FALSE;
  1183. }
  1184. if (!dbus_message_iter_close_container(iter, &variant_iter)) {
  1185. dbus_set_error(error, DBUS_ERROR_FAILED,
  1186. "%s: failed to construct message 7", __func__);
  1187. return FALSE;
  1188. }
  1189. return TRUE;
  1190. }
  1191. dbus_bool_t wpas_dbus_getter_p2p_peer_vendor_extension(DBusMessageIter *iter,
  1192. DBusError *error,
  1193. void *user_data)
  1194. {
  1195. struct wpabuf *vendor_extension[P2P_MAX_WPS_VENDOR_EXT];
  1196. int i, num;
  1197. struct peer_handler_args *peer_args = user_data;
  1198. const struct p2p_peer_info *info;
  1199. info = p2p_get_peer_found(peer_args->wpa_s->global->p2p,
  1200. peer_args->p2p_device_addr, 0);
  1201. if (info == NULL) {
  1202. dbus_set_error(error, DBUS_ERROR_FAILED,
  1203. "failed to find peer");
  1204. return FALSE;
  1205. }
  1206. /* Add WPS vendor extensions attribute */
  1207. for (i = 0, num = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
  1208. if (info->wps_vendor_ext[i] == NULL)
  1209. continue;
  1210. vendor_extension[num] = info->wps_vendor_ext[i];
  1211. num++;
  1212. }
  1213. if (!wpas_dbus_simple_array_array_property_getter(iter, DBUS_TYPE_BYTE,
  1214. vendor_extension,
  1215. num, error))
  1216. return FALSE;
  1217. return TRUE;
  1218. }
  1219. dbus_bool_t wpas_dbus_getter_p2p_peer_ies(DBusMessageIter *iter,
  1220. DBusError *error, void *user_data)
  1221. {
  1222. dbus_bool_t success;
  1223. /* struct peer_handler_args *peer_args = user_data; */
  1224. success = wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
  1225. NULL, 0, error);
  1226. return success;
  1227. }
  1228. /**
  1229. * wpas_dbus_getter_persistent_groups - Get array of persistent group objects
  1230. * @iter: Pointer to incoming dbus message iter
  1231. * @error: Location to store error on failure
  1232. * @user_data: Function specific data
  1233. * Returns: TRUE on success, FALSE on failure
  1234. *
  1235. * Getter for "PersistentGroups" property.
  1236. */
  1237. dbus_bool_t wpas_dbus_getter_persistent_groups(DBusMessageIter *iter,
  1238. DBusError *error,
  1239. void *user_data)
  1240. {
  1241. struct wpa_supplicant *wpa_s = user_data;
  1242. struct wpa_ssid *ssid;
  1243. char **paths;
  1244. unsigned int i = 0, num = 0;
  1245. dbus_bool_t success = FALSE;
  1246. if (wpa_s->conf == NULL) {
  1247. wpa_printf(MSG_ERROR, "dbus: %s: "
  1248. "An error occurred getting persistent groups list",
  1249. __func__);
  1250. dbus_set_error_const(error, DBUS_ERROR_FAILED, "an error "
  1251. "occurred getting persistent groups list");
  1252. return FALSE;
  1253. }
  1254. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  1255. if (network_is_persistent_group(ssid))
  1256. num++;
  1257. paths = os_zalloc(num * sizeof(char *));
  1258. if (!paths) {
  1259. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  1260. return FALSE;
  1261. }
  1262. /* Loop through configured networks and append object path of each */
  1263. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1264. if (!network_is_persistent_group(ssid))
  1265. continue;
  1266. paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  1267. if (paths[i] == NULL) {
  1268. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
  1269. "no memory");
  1270. goto out;
  1271. }
  1272. /* Construct the object path for this network. */
  1273. os_snprintf(paths[i++], WPAS_DBUS_OBJECT_PATH_MAX,
  1274. "%s/" WPAS_DBUS_NEW_PERSISTENT_GROUPS_PART "/%d",
  1275. wpa_s->dbus_new_path, ssid->id);
  1276. }
  1277. success = wpas_dbus_simple_array_property_getter(iter,
  1278. DBUS_TYPE_OBJECT_PATH,
  1279. paths, num, error);
  1280. out:
  1281. while (i)
  1282. os_free(paths[--i]);
  1283. os_free(paths);
  1284. return success;
  1285. }
  1286. /**
  1287. * wpas_dbus_getter_persistent_group_properties - Get options for a persistent
  1288. * group
  1289. * @iter: Pointer to incoming dbus message iter
  1290. * @error: Location to store error on failure
  1291. * @user_data: Function specific data
  1292. * Returns: TRUE on success, FALSE on failure
  1293. *
  1294. * Getter for "Properties" property of a persistent group.
  1295. */
  1296. dbus_bool_t wpas_dbus_getter_persistent_group_properties(DBusMessageIter *iter,
  1297. DBusError *error,
  1298. void *user_data)
  1299. {
  1300. struct network_handler_args *net = user_data;
  1301. /* Leveraging the fact that persistent group object is still
  1302. * represented in same manner as network within.
  1303. */
  1304. return wpas_dbus_getter_network_properties(iter, error, net);
  1305. }
  1306. /**
  1307. * wpas_dbus_setter_persistent_group_properties - Get options for a persistent
  1308. * group
  1309. * @iter: Pointer to incoming dbus message iter
  1310. * @error: Location to store error on failure
  1311. * @user_data: Function specific data
  1312. * Returns: TRUE on success, FALSE on failure
  1313. *
  1314. * Setter for "Properties" property of a persistent group.
  1315. */
  1316. dbus_bool_t wpas_dbus_setter_persistent_group_properties(DBusMessageIter *iter,
  1317. DBusError *error,
  1318. void *user_data)
  1319. {
  1320. struct network_handler_args *net = user_data;
  1321. struct wpa_ssid *ssid = net->ssid;
  1322. DBusMessageIter variant_iter;
  1323. /*
  1324. * Leveraging the fact that persistent group object is still
  1325. * represented in same manner as network within.
  1326. */
  1327. dbus_message_iter_recurse(iter, &variant_iter);
  1328. return set_network_properties(net->wpa_s, ssid, &variant_iter, error);
  1329. }
  1330. /**
  1331. * wpas_dbus_new_iface_add_persistent_group - Add a new configured
  1332. * persistent_group
  1333. * @message: Pointer to incoming dbus message
  1334. * @wpa_s: wpa_supplicant structure for a network interface
  1335. * Returns: A dbus message containing the object path of the new
  1336. * persistent group
  1337. *
  1338. * Handler function for "AddPersistentGroup" method call of a P2P Device
  1339. * interface.
  1340. */
  1341. DBusMessage * wpas_dbus_handler_add_persistent_group(
  1342. DBusMessage *message, struct wpa_supplicant *wpa_s)
  1343. {
  1344. DBusMessage *reply = NULL;
  1345. DBusMessageIter iter;
  1346. struct wpa_ssid *ssid = NULL;
  1347. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *path = path_buf;
  1348. DBusError error;
  1349. dbus_message_iter_init(message, &iter);
  1350. ssid = wpa_config_add_network(wpa_s->conf);
  1351. if (ssid == NULL) {
  1352. wpa_printf(MSG_ERROR, "dbus: %s: "
  1353. "Cannot add new persistent group", __func__);
  1354. reply = wpas_dbus_error_unknown_error(
  1355. message,
  1356. "wpa_supplicant could not add "
  1357. "a persistent group on this interface.");
  1358. goto err;
  1359. }
  1360. /* Mark the ssid as being a persistent group before the notification */
  1361. ssid->disabled = 2;
  1362. ssid->p2p_persistent_group = 1;
  1363. wpas_notify_persistent_group_added(wpa_s, ssid);
  1364. wpa_config_set_network_defaults(ssid);
  1365. dbus_error_init(&error);
  1366. if (!set_network_properties(wpa_s, ssid, &iter, &error)) {
  1367. wpa_printf(MSG_DEBUG, "dbus: %s: "
  1368. "Control interface could not set persistent group "
  1369. "properties", __func__);
  1370. reply = wpas_dbus_reply_new_from_error(message, &error,
  1371. DBUS_ERROR_INVALID_ARGS,
  1372. "Failed to set network "
  1373. "properties");
  1374. dbus_error_free(&error);
  1375. goto err;
  1376. }
  1377. /* Construct the object path for this network. */
  1378. os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
  1379. "%s/" WPAS_DBUS_NEW_PERSISTENT_GROUPS_PART "/%d",
  1380. wpa_s->dbus_new_path, ssid->id);
  1381. reply = dbus_message_new_method_return(message);
  1382. if (reply == NULL) {
  1383. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1384. NULL);
  1385. goto err;
  1386. }
  1387. if (!dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
  1388. DBUS_TYPE_INVALID)) {
  1389. dbus_message_unref(reply);
  1390. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1391. NULL);
  1392. goto err;
  1393. }
  1394. return reply;
  1395. err:
  1396. if (ssid) {
  1397. wpas_notify_persistent_group_removed(wpa_s, ssid);
  1398. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1399. }
  1400. return reply;
  1401. }
  1402. /**
  1403. * wpas_dbus_handler_remove_persistent_group - Remove a configured persistent
  1404. * group
  1405. * @message: Pointer to incoming dbus message
  1406. * @wpa_s: wpa_supplicant structure for a network interface
  1407. * Returns: NULL on success or dbus error on failure
  1408. *
  1409. * Handler function for "RemovePersistentGroup" method call of a P2P Device
  1410. * interface.
  1411. */
  1412. DBusMessage * wpas_dbus_handler_remove_persistent_group(
  1413. DBusMessage *message, struct wpa_supplicant *wpa_s)
  1414. {
  1415. DBusMessage *reply = NULL;
  1416. const char *op;
  1417. char *iface = NULL, *persistent_group_id = NULL;
  1418. int id;
  1419. struct wpa_ssid *ssid;
  1420. dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &op,
  1421. DBUS_TYPE_INVALID);
  1422. /*
  1423. * Extract the network ID and ensure the network is actually a child of
  1424. * this interface.
  1425. */
  1426. iface = wpas_dbus_new_decompose_object_path(op, 1,
  1427. &persistent_group_id,
  1428. NULL);
  1429. if (iface == NULL || os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
  1430. reply = wpas_dbus_error_invalid_args(message, op);
  1431. goto out;
  1432. }
  1433. id = strtoul(persistent_group_id, NULL, 10);
  1434. if (errno == EINVAL) {
  1435. reply = wpas_dbus_error_invalid_args(message, op);
  1436. goto out;
  1437. }
  1438. ssid = wpa_config_get_network(wpa_s->conf, id);
  1439. if (ssid == NULL) {
  1440. reply = wpas_dbus_error_persistent_group_unknown(message);
  1441. goto out;
  1442. }
  1443. wpas_notify_persistent_group_removed(wpa_s, ssid);
  1444. if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
  1445. wpa_printf(MSG_ERROR, "dbus: %s: "
  1446. "error occurred when removing persistent group %d",
  1447. __func__, id);
  1448. reply = wpas_dbus_error_unknown_error(
  1449. message,
  1450. "error removing the specified persistent group on "
  1451. "this interface.");
  1452. goto out;
  1453. }
  1454. out:
  1455. os_free(iface);
  1456. os_free(persistent_group_id);
  1457. return reply;
  1458. }
  1459. static void remove_persistent_group(struct wpa_supplicant *wpa_s,
  1460. struct wpa_ssid *ssid)
  1461. {
  1462. wpas_notify_persistent_group_removed(wpa_s, ssid);
  1463. if (wpa_config_remove_network(wpa_s->conf, ssid->id) < 0) {
  1464. wpa_printf(MSG_ERROR, "dbus: %s: "
  1465. "error occurred when removing persistent group %d",
  1466. __func__, ssid->id);
  1467. return;
  1468. }
  1469. }
  1470. /**
  1471. * wpas_dbus_handler_remove_all_persistent_groups - Remove all configured
  1472. * persistent groups
  1473. * @message: Pointer to incoming dbus message
  1474. * @wpa_s: wpa_supplicant structure for a network interface
  1475. * Returns: NULL on success or dbus error on failure
  1476. *
  1477. * Handler function for "RemoveAllPersistentGroups" method call of a
  1478. * P2P Device interface.
  1479. */
  1480. DBusMessage * wpas_dbus_handler_remove_all_persistent_groups(
  1481. DBusMessage *message, struct wpa_supplicant *wpa_s)
  1482. {
  1483. struct wpa_ssid *ssid, *next;
  1484. struct wpa_config *config;
  1485. config = wpa_s->conf;
  1486. ssid = config->ssid;
  1487. while (ssid) {
  1488. next = ssid->next;
  1489. if (network_is_persistent_group(ssid))
  1490. remove_persistent_group(wpa_s, ssid);
  1491. ssid = next;
  1492. }
  1493. return NULL;
  1494. }
  1495. /*
  1496. * Group object properties accessor methods
  1497. */
  1498. dbus_bool_t wpas_dbus_getter_p2p_group_members(DBusMessageIter *iter,
  1499. DBusError *error,
  1500. void *user_data)
  1501. {
  1502. struct wpa_supplicant *wpa_s = user_data;
  1503. struct wpa_ssid *ssid;
  1504. unsigned int num_members;
  1505. char **paths;
  1506. unsigned int i;
  1507. void *next = NULL;
  1508. const u8 *addr;
  1509. dbus_bool_t success = FALSE;
  1510. /* Verify correct role for this property */
  1511. if (wpas_get_p2p_role(wpa_s) != WPAS_P2P_ROLE_GO) {
  1512. return wpas_dbus_simple_array_property_getter(
  1513. iter, DBUS_TYPE_OBJECT_PATH, NULL, 0, error);
  1514. }
  1515. ssid = wpa_s->conf->ssid;
  1516. /* At present WPAS P2P_GO mode only applicable for p2p_go */
  1517. if (ssid->mode != WPAS_MODE_P2P_GO &&
  1518. ssid->mode != WPAS_MODE_AP &&
  1519. ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
  1520. return FALSE;
  1521. num_members = p2p_get_group_num_members(wpa_s->p2p_group);
  1522. paths = os_zalloc(num_members * sizeof(char *));
  1523. if (!paths)
  1524. goto out_of_memory;
  1525. i = 0;
  1526. while ((addr = p2p_iterate_group_members(wpa_s->p2p_group, &next))) {
  1527. paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  1528. if (!paths[i])
  1529. goto out_of_memory;
  1530. os_snprintf(paths[i], WPAS_DBUS_OBJECT_PATH_MAX,
  1531. "%s/" WPAS_DBUS_NEW_P2P_GROUPMEMBERS_PART
  1532. "/" COMPACT_MACSTR,
  1533. wpa_s->dbus_groupobj_path, MAC2STR(addr));
  1534. i++;
  1535. }
  1536. success = wpas_dbus_simple_array_property_getter(iter,
  1537. DBUS_TYPE_OBJECT_PATH,
  1538. paths, num_members,
  1539. error);
  1540. for (i = 0; i < num_members; i++)
  1541. os_free(paths[i]);
  1542. os_free(paths);
  1543. return success;
  1544. out_of_memory:
  1545. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  1546. if (paths) {
  1547. for (i = 0; i < num_members; i++)
  1548. os_free(paths[i]);
  1549. os_free(paths);
  1550. }
  1551. return FALSE;
  1552. }
  1553. dbus_bool_t wpas_dbus_getter_p2p_group_ssid(DBusMessageIter *iter,
  1554. DBusError *error, void *user_data)
  1555. {
  1556. struct wpa_supplicant *wpa_s = user_data;
  1557. if (wpa_s->current_ssid == NULL)
  1558. return FALSE;
  1559. return wpas_dbus_simple_array_property_getter(
  1560. iter, DBUS_TYPE_BYTE, wpa_s->current_ssid->ssid,
  1561. wpa_s->current_ssid->ssid_len, error);
  1562. }
  1563. dbus_bool_t wpas_dbus_getter_p2p_group_bssid(DBusMessageIter *iter,
  1564. DBusError *error,
  1565. void *user_data)
  1566. {
  1567. struct wpa_supplicant *wpa_s = user_data;
  1568. u8 role = wpas_get_p2p_role(wpa_s);
  1569. u8 *p_bssid;
  1570. if (role == WPAS_P2P_ROLE_CLIENT) {
  1571. if (wpa_s->current_ssid == NULL)
  1572. return FALSE;
  1573. p_bssid = wpa_s->current_ssid->bssid;
  1574. } else {
  1575. if (wpa_s->ap_iface == NULL)
  1576. return FALSE;
  1577. p_bssid = wpa_s->ap_iface->bss[0]->own_addr;
  1578. }
  1579. return wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
  1580. p_bssid, ETH_ALEN,
  1581. error);
  1582. }
  1583. dbus_bool_t wpas_dbus_getter_p2p_group_frequency(DBusMessageIter *iter,
  1584. DBusError *error,
  1585. void *user_data)
  1586. {
  1587. struct wpa_supplicant *wpa_s = user_data;
  1588. u16 op_freq;
  1589. u8 role = wpas_get_p2p_role(wpa_s);
  1590. if (role == WPAS_P2P_ROLE_CLIENT) {
  1591. if (wpa_s->go_params == NULL)
  1592. return FALSE;
  1593. op_freq = wpa_s->go_params->freq;
  1594. } else {
  1595. if (wpa_s->ap_iface == NULL)
  1596. return FALSE;
  1597. op_freq = wpa_s->ap_iface->freq;
  1598. }
  1599. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT16,
  1600. &op_freq, error);
  1601. }
  1602. dbus_bool_t wpas_dbus_getter_p2p_group_passphrase(DBusMessageIter *iter,
  1603. DBusError *error,
  1604. void *user_data)
  1605. {
  1606. struct wpa_supplicant *wpa_s = user_data;
  1607. u8 role = wpas_get_p2p_role(wpa_s);
  1608. char *p_pass = NULL;
  1609. /* Verify correct role for this property */
  1610. if (role == WPAS_P2P_ROLE_GO) {
  1611. if (wpa_s->current_ssid == NULL)
  1612. return FALSE;
  1613. p_pass = wpa_s->current_ssid->passphrase;
  1614. } else
  1615. p_pass = "";
  1616. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  1617. &p_pass, error);
  1618. }
  1619. dbus_bool_t wpas_dbus_getter_p2p_group_psk(DBusMessageIter *iter,
  1620. DBusError *error, void *user_data)
  1621. {
  1622. struct wpa_supplicant *wpa_s = user_data;
  1623. u8 role = wpas_get_p2p_role(wpa_s);
  1624. u8 *p_psk = NULL;
  1625. u8 psk_len = 0;
  1626. /* Verify correct role for this property */
  1627. if (role == WPAS_P2P_ROLE_CLIENT) {
  1628. if (wpa_s->current_ssid == NULL)
  1629. return FALSE;
  1630. p_psk = wpa_s->current_ssid->psk;
  1631. psk_len = 32;
  1632. }
  1633. return wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
  1634. &p_psk, psk_len, error);
  1635. }
  1636. dbus_bool_t wpas_dbus_getter_p2p_group_vendor_ext(DBusMessageIter *iter,
  1637. DBusError *error,
  1638. void *user_data)
  1639. {
  1640. struct wpa_supplicant *wpa_s = user_data;
  1641. struct hostapd_data *hapd;
  1642. struct wpabuf *vendor_ext[MAX_WPS_VENDOR_EXTENSIONS];
  1643. int num_vendor_ext = 0;
  1644. int i;
  1645. /* Verify correct role for this property */
  1646. if (wpas_get_p2p_role(wpa_s) == WPAS_P2P_ROLE_GO) {
  1647. if (wpa_s->ap_iface == NULL)
  1648. return FALSE;
  1649. hapd = wpa_s->ap_iface->bss[0];
  1650. /* Parse WPS Vendor Extensions sent in Beacon/Probe Response */
  1651. for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
  1652. if (hapd->conf->wps_vendor_ext[i] == NULL)
  1653. vendor_ext[i] = NULL;
  1654. else {
  1655. vendor_ext[num_vendor_ext++] =
  1656. hapd->conf->wps_vendor_ext[i];
  1657. }
  1658. }
  1659. }
  1660. /* Return vendor extensions or no data */
  1661. return wpas_dbus_simple_array_array_property_getter(iter,
  1662. DBUS_TYPE_BYTE,
  1663. vendor_ext,
  1664. num_vendor_ext,
  1665. error);
  1666. }
  1667. dbus_bool_t wpas_dbus_setter_p2p_group_vendor_ext(DBusMessageIter *iter,
  1668. DBusError *error,
  1669. void *user_data)
  1670. {
  1671. struct wpa_supplicant *wpa_s = user_data;
  1672. DBusMessageIter variant_iter, iter_dict;
  1673. struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
  1674. unsigned int i;
  1675. struct hostapd_data *hapd = NULL;
  1676. if (wpas_get_p2p_role(wpa_s) == WPAS_P2P_ROLE_GO &&
  1677. wpa_s->ap_iface != NULL)
  1678. hapd = wpa_s->ap_iface->bss[0];
  1679. else
  1680. return FALSE;
  1681. dbus_message_iter_recurse(iter, &variant_iter);
  1682. if (!wpa_dbus_dict_open_read(&variant_iter, &iter_dict, error))
  1683. return FALSE;
  1684. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  1685. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
  1686. dbus_set_error_const(error, DBUS_ERROR_INVALID_ARGS,
  1687. "invalid message format");
  1688. return FALSE;
  1689. }
  1690. if (os_strcmp(entry.key, "WPSVendorExtensions") == 0) {
  1691. if (entry.type != DBUS_TYPE_ARRAY ||
  1692. entry.array_type != WPAS_DBUS_TYPE_BINARRAY ||
  1693. entry.array_len > MAX_WPS_VENDOR_EXTENSIONS)
  1694. goto error;
  1695. for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
  1696. if (i < entry.array_len) {
  1697. hapd->conf->wps_vendor_ext[i] =
  1698. entry.binarray_value[i];
  1699. entry.binarray_value[i] = NULL;
  1700. } else
  1701. hapd->conf->wps_vendor_ext[i] = NULL;
  1702. }
  1703. hostapd_update_wps(hapd);
  1704. } else
  1705. goto error;
  1706. wpa_dbus_dict_entry_clear(&entry);
  1707. }
  1708. return TRUE;
  1709. error:
  1710. wpa_dbus_dict_entry_clear(&entry);
  1711. dbus_set_error_const(error, DBUS_ERROR_INVALID_ARGS,
  1712. "invalid message format");
  1713. return FALSE;
  1714. }
  1715. DBusMessage * wpas_dbus_handler_p2p_add_service(DBusMessage *message,
  1716. struct wpa_supplicant *wpa_s)
  1717. {
  1718. DBusMessageIter iter_dict;
  1719. DBusMessage *reply = NULL;
  1720. DBusMessageIter iter;
  1721. struct wpa_dbus_dict_entry entry;
  1722. int upnp = 0;
  1723. int bonjour = 0;
  1724. char *service = NULL;
  1725. struct wpabuf *query = NULL;
  1726. struct wpabuf *resp = NULL;
  1727. u8 version = 0;
  1728. dbus_message_iter_init(message, &iter);
  1729. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  1730. goto error;
  1731. if (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  1732. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  1733. goto error;
  1734. if (!os_strcmp(entry.key, "service_type") &&
  1735. (entry.type == DBUS_TYPE_STRING)) {
  1736. if (!os_strcmp(entry.str_value, "upnp"))
  1737. upnp = 1;
  1738. else if (!os_strcmp(entry.str_value, "bonjour"))
  1739. bonjour = 1;
  1740. else
  1741. goto error_clear;
  1742. wpa_dbus_dict_entry_clear(&entry);
  1743. }
  1744. }
  1745. if (upnp == 1) {
  1746. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  1747. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  1748. goto error;
  1749. if (!os_strcmp(entry.key, "version") &&
  1750. entry.type == DBUS_TYPE_INT32)
  1751. version = entry.uint32_value;
  1752. else if (!os_strcmp(entry.key, "service") &&
  1753. entry.type == DBUS_TYPE_STRING)
  1754. service = os_strdup(entry.str_value);
  1755. wpa_dbus_dict_entry_clear(&entry);
  1756. }
  1757. if (version <= 0 || service == NULL)
  1758. goto error;
  1759. if (wpas_p2p_service_add_upnp(wpa_s, version, service) != 0)
  1760. goto error;
  1761. os_free(service);
  1762. } else if (bonjour == 1) {
  1763. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  1764. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  1765. goto error;
  1766. if (!os_strcmp(entry.key, "query")) {
  1767. if ((entry.type != DBUS_TYPE_ARRAY) ||
  1768. (entry.array_type != DBUS_TYPE_BYTE))
  1769. goto error_clear;
  1770. query = wpabuf_alloc_copy(
  1771. entry.bytearray_value,
  1772. entry.array_len);
  1773. } else if (!os_strcmp(entry.key, "response")) {
  1774. if ((entry.type != DBUS_TYPE_ARRAY) ||
  1775. (entry.array_type != DBUS_TYPE_BYTE))
  1776. goto error_clear;
  1777. resp = wpabuf_alloc_copy(entry.bytearray_value,
  1778. entry.array_len);
  1779. }
  1780. wpa_dbus_dict_entry_clear(&entry);
  1781. }
  1782. if (query == NULL || resp == NULL)
  1783. goto error;
  1784. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
  1785. wpabuf_free(query);
  1786. wpabuf_free(resp);
  1787. goto error;
  1788. }
  1789. } else
  1790. goto error;
  1791. return reply;
  1792. error_clear:
  1793. wpa_dbus_dict_entry_clear(&entry);
  1794. error:
  1795. return wpas_dbus_error_invalid_args(message, NULL);
  1796. }
  1797. DBusMessage * wpas_dbus_handler_p2p_delete_service(
  1798. DBusMessage *message, struct wpa_supplicant *wpa_s)
  1799. {
  1800. DBusMessageIter iter_dict;
  1801. DBusMessage *reply = NULL;
  1802. DBusMessageIter iter;
  1803. struct wpa_dbus_dict_entry entry;
  1804. int upnp = 0;
  1805. int bonjour = 0;
  1806. int ret = 0;
  1807. char *service = NULL;
  1808. struct wpabuf *query = NULL;
  1809. u8 version = 0;
  1810. dbus_message_iter_init(message, &iter);
  1811. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  1812. goto error;
  1813. if (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  1814. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  1815. goto error;
  1816. if (!os_strcmp(entry.key, "service_type") &&
  1817. (entry.type == DBUS_TYPE_STRING)) {
  1818. if (!os_strcmp(entry.str_value, "upnp"))
  1819. upnp = 1;
  1820. else if (!os_strcmp(entry.str_value, "bonjour"))
  1821. bonjour = 1;
  1822. else
  1823. goto error_clear;
  1824. wpa_dbus_dict_entry_clear(&entry);
  1825. }
  1826. }
  1827. if (upnp == 1) {
  1828. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  1829. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  1830. goto error;
  1831. if (!os_strcmp(entry.key, "version") &&
  1832. entry.type == DBUS_TYPE_INT32)
  1833. version = entry.uint32_value;
  1834. else if (!os_strcmp(entry.key, "service") &&
  1835. entry.type == DBUS_TYPE_STRING)
  1836. service = os_strdup(entry.str_value);
  1837. else
  1838. goto error_clear;
  1839. wpa_dbus_dict_entry_clear(&entry);
  1840. }
  1841. if (version <= 0 || service == NULL)
  1842. goto error;
  1843. ret = wpas_p2p_service_del_upnp(wpa_s, version, service);
  1844. os_free(service);
  1845. if (ret != 0)
  1846. goto error;
  1847. } else if (bonjour == 1) {
  1848. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  1849. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  1850. goto error;
  1851. if (!os_strcmp(entry.key, "query")) {
  1852. if ((entry.type != DBUS_TYPE_ARRAY) ||
  1853. (entry.array_type != DBUS_TYPE_BYTE))
  1854. goto error_clear;
  1855. query = wpabuf_alloc_copy(
  1856. entry.bytearray_value,
  1857. entry.array_len);
  1858. } else
  1859. goto error_clear;
  1860. wpa_dbus_dict_entry_clear(&entry);
  1861. }
  1862. if (query == NULL)
  1863. goto error;
  1864. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  1865. if (ret != 0)
  1866. goto error;
  1867. wpabuf_free(query);
  1868. } else
  1869. goto error;
  1870. return reply;
  1871. error_clear:
  1872. wpa_dbus_dict_entry_clear(&entry);
  1873. error:
  1874. return wpas_dbus_error_invalid_args(message, NULL);
  1875. }
  1876. DBusMessage * wpas_dbus_handler_p2p_flush_service(DBusMessage *message,
  1877. struct wpa_supplicant *wpa_s)
  1878. {
  1879. wpas_p2p_service_flush(wpa_s);
  1880. return NULL;
  1881. }
  1882. DBusMessage * wpas_dbus_handler_p2p_service_sd_req(
  1883. DBusMessage *message, struct wpa_supplicant *wpa_s)
  1884. {
  1885. DBusMessageIter iter_dict;
  1886. DBusMessage *reply = NULL;
  1887. DBusMessageIter iter;
  1888. struct wpa_dbus_dict_entry entry;
  1889. int upnp = 0;
  1890. char *service = NULL;
  1891. char *peer_object_path = NULL;
  1892. struct wpabuf *tlv = NULL;
  1893. u8 version = 0;
  1894. u64 ref = 0;
  1895. u8 addr[ETH_ALEN];
  1896. dbus_message_iter_init(message, &iter);
  1897. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  1898. goto error;
  1899. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  1900. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  1901. goto error;
  1902. if (!os_strcmp(entry.key, "peer_object") &&
  1903. entry.type == DBUS_TYPE_OBJECT_PATH) {
  1904. peer_object_path = os_strdup(entry.str_value);
  1905. } else if (!os_strcmp(entry.key, "service_type") &&
  1906. entry.type == DBUS_TYPE_STRING) {
  1907. if (!os_strcmp(entry.str_value, "upnp"))
  1908. upnp = 1;
  1909. else
  1910. goto error_clear;
  1911. } else if (!os_strcmp(entry.key, "version") &&
  1912. entry.type == DBUS_TYPE_INT32) {
  1913. version = entry.uint32_value;
  1914. } else if (!os_strcmp(entry.key, "service") &&
  1915. entry.type == DBUS_TYPE_STRING) {
  1916. service = os_strdup(entry.str_value);
  1917. } else if (!os_strcmp(entry.key, "tlv")) {
  1918. if (entry.type != DBUS_TYPE_ARRAY ||
  1919. entry.array_type != DBUS_TYPE_BYTE)
  1920. goto error_clear;
  1921. tlv = wpabuf_alloc_copy(entry.bytearray_value,
  1922. entry.array_len);
  1923. } else
  1924. goto error_clear;
  1925. wpa_dbus_dict_entry_clear(&entry);
  1926. }
  1927. if (!peer_object_path ||
  1928. (parse_peer_object_path(peer_object_path, addr) < 0) ||
  1929. !p2p_peer_known(wpa_s->global->p2p, addr))
  1930. goto error;
  1931. if (upnp == 1) {
  1932. if (version <= 0 || service == NULL)
  1933. goto error;
  1934. ref = wpas_p2p_sd_request_upnp(wpa_s, addr, version, service);
  1935. } else {
  1936. if (tlv == NULL)
  1937. goto error;
  1938. ref = wpas_p2p_sd_request(wpa_s, addr, tlv);
  1939. wpabuf_free(tlv);
  1940. }
  1941. if (ref != 0) {
  1942. reply = dbus_message_new_method_return(message);
  1943. dbus_message_append_args(reply, DBUS_TYPE_UINT64,
  1944. &ref, DBUS_TYPE_INVALID);
  1945. } else {
  1946. reply = wpas_dbus_error_unknown_error(
  1947. message, "Unable to send SD request");
  1948. }
  1949. out:
  1950. os_free(service);
  1951. os_free(peer_object_path);
  1952. return reply;
  1953. error_clear:
  1954. wpa_dbus_dict_entry_clear(&entry);
  1955. error:
  1956. if (tlv)
  1957. wpabuf_free(tlv);
  1958. reply = wpas_dbus_error_invalid_args(message, NULL);
  1959. goto out;
  1960. }
  1961. DBusMessage * wpas_dbus_handler_p2p_service_sd_res(
  1962. DBusMessage *message, struct wpa_supplicant *wpa_s)
  1963. {
  1964. DBusMessageIter iter_dict;
  1965. DBusMessage *reply = NULL;
  1966. DBusMessageIter iter;
  1967. struct wpa_dbus_dict_entry entry;
  1968. char *peer_object_path = NULL;
  1969. struct wpabuf *tlv = NULL;
  1970. int freq = 0;
  1971. int dlg_tok = 0;
  1972. u8 addr[ETH_ALEN];
  1973. dbus_message_iter_init(message, &iter);
  1974. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  1975. goto error;
  1976. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  1977. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  1978. goto error;
  1979. if (!os_strcmp(entry.key, "peer_object") &&
  1980. entry.type == DBUS_TYPE_OBJECT_PATH) {
  1981. peer_object_path = os_strdup(entry.str_value);
  1982. } else if (!os_strcmp(entry.key, "frequency") &&
  1983. entry.type == DBUS_TYPE_INT32) {
  1984. freq = entry.uint32_value;
  1985. } else if (!os_strcmp(entry.key, "dialog_token") &&
  1986. entry.type == DBUS_TYPE_UINT32) {
  1987. dlg_tok = entry.uint32_value;
  1988. } else if (!os_strcmp(entry.key, "tlvs")) {
  1989. if (entry.type != DBUS_TYPE_ARRAY ||
  1990. entry.array_type != DBUS_TYPE_BYTE)
  1991. goto error_clear;
  1992. tlv = wpabuf_alloc_copy(entry.bytearray_value,
  1993. entry.array_len);
  1994. } else
  1995. goto error_clear;
  1996. wpa_dbus_dict_entry_clear(&entry);
  1997. }
  1998. if (!peer_object_path ||
  1999. (parse_peer_object_path(peer_object_path, addr) < 0) ||
  2000. !p2p_peer_known(wpa_s->global->p2p, addr))
  2001. goto error;
  2002. if (tlv == NULL)
  2003. goto error;
  2004. wpas_p2p_sd_response(wpa_s, freq, addr, (u8) dlg_tok, tlv);
  2005. wpabuf_free(tlv);
  2006. out:
  2007. os_free(peer_object_path);
  2008. return reply;
  2009. error_clear:
  2010. wpa_dbus_dict_entry_clear(&entry);
  2011. error:
  2012. reply = wpas_dbus_error_invalid_args(message, NULL);
  2013. goto out;
  2014. }
  2015. DBusMessage * wpas_dbus_handler_p2p_service_sd_cancel_req(
  2016. DBusMessage *message, struct wpa_supplicant *wpa_s)
  2017. {
  2018. DBusMessageIter iter;
  2019. u64 req = 0;
  2020. dbus_message_iter_init(message, &iter);
  2021. dbus_message_iter_get_basic(&iter, &req);
  2022. if (req == 0)
  2023. goto error;
  2024. if (!wpas_p2p_sd_cancel_request(wpa_s, req))
  2025. goto error;
  2026. return NULL;
  2027. error:
  2028. return wpas_dbus_error_invalid_args(message, NULL);
  2029. }
  2030. DBusMessage * wpas_dbus_handler_p2p_service_update(
  2031. DBusMessage *message, struct wpa_supplicant *wpa_s)
  2032. {
  2033. wpas_p2p_sd_service_update(wpa_s);
  2034. return NULL;
  2035. }
  2036. DBusMessage * wpas_dbus_handler_p2p_serv_disc_external(
  2037. DBusMessage *message, struct wpa_supplicant *wpa_s)
  2038. {
  2039. DBusMessageIter iter;
  2040. int ext = 0;
  2041. dbus_message_iter_init(message, &iter);
  2042. dbus_message_iter_get_basic(&iter, &ext);
  2043. wpa_s->p2p_sd_over_ctrl_iface = ext;
  2044. return NULL;
  2045. }