dbus_new_handlers_p2p.c 65 KB

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