dbus_new_handlers_p2p.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448
  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. /**
  1241. * wpas_dbus_getter_persistent_groups - Get array of persistent group objects
  1242. * @iter: Pointer to incoming dbus message iter
  1243. * @error: Location to store error on failure
  1244. * @user_data: Function specific data
  1245. * Returns: TRUE on success, FALSE on failure
  1246. *
  1247. * Getter for "PersistentGroups" property.
  1248. */
  1249. dbus_bool_t wpas_dbus_getter_persistent_groups(DBusMessageIter *iter,
  1250. DBusError *error,
  1251. void *user_data)
  1252. {
  1253. struct wpa_supplicant *wpa_s = user_data;
  1254. struct wpa_ssid *ssid;
  1255. char **paths;
  1256. unsigned int i = 0, num = 0;
  1257. dbus_bool_t success = FALSE;
  1258. if (wpa_s->conf == NULL) {
  1259. wpa_printf(MSG_ERROR, "dbus: %s: "
  1260. "An error occurred getting persistent groups list",
  1261. __func__);
  1262. dbus_set_error_const(error, DBUS_ERROR_FAILED, "an error "
  1263. "occurred getting persistent groups list");
  1264. return FALSE;
  1265. }
  1266. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  1267. if (network_is_persistent_group(ssid))
  1268. num++;
  1269. paths = os_calloc(num, sizeof(char *));
  1270. if (!paths) {
  1271. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  1272. return FALSE;
  1273. }
  1274. /* Loop through configured networks and append object path of each */
  1275. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1276. if (!network_is_persistent_group(ssid))
  1277. continue;
  1278. paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  1279. if (paths[i] == NULL) {
  1280. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
  1281. "no memory");
  1282. goto out;
  1283. }
  1284. /* Construct the object path for this network. */
  1285. os_snprintf(paths[i++], WPAS_DBUS_OBJECT_PATH_MAX,
  1286. "%s/" WPAS_DBUS_NEW_PERSISTENT_GROUPS_PART "/%d",
  1287. wpa_s->dbus_new_path, ssid->id);
  1288. }
  1289. success = wpas_dbus_simple_array_property_getter(iter,
  1290. DBUS_TYPE_OBJECT_PATH,
  1291. paths, num, error);
  1292. out:
  1293. while (i)
  1294. os_free(paths[--i]);
  1295. os_free(paths);
  1296. return success;
  1297. }
  1298. /**
  1299. * wpas_dbus_getter_persistent_group_properties - Get options for a persistent
  1300. * group
  1301. * @iter: Pointer to incoming dbus message iter
  1302. * @error: Location to store error on failure
  1303. * @user_data: Function specific data
  1304. * Returns: TRUE on success, FALSE on failure
  1305. *
  1306. * Getter for "Properties" property of a persistent group.
  1307. */
  1308. dbus_bool_t wpas_dbus_getter_persistent_group_properties(DBusMessageIter *iter,
  1309. DBusError *error,
  1310. void *user_data)
  1311. {
  1312. struct network_handler_args *net = user_data;
  1313. /* Leveraging the fact that persistent group object is still
  1314. * represented in same manner as network within.
  1315. */
  1316. return wpas_dbus_getter_network_properties(iter, error, net);
  1317. }
  1318. /**
  1319. * wpas_dbus_setter_persistent_group_properties - Get options for a persistent
  1320. * group
  1321. * @iter: Pointer to incoming dbus message iter
  1322. * @error: Location to store error on failure
  1323. * @user_data: Function specific data
  1324. * Returns: TRUE on success, FALSE on failure
  1325. *
  1326. * Setter for "Properties" property of a persistent group.
  1327. */
  1328. dbus_bool_t wpas_dbus_setter_persistent_group_properties(DBusMessageIter *iter,
  1329. DBusError *error,
  1330. void *user_data)
  1331. {
  1332. struct network_handler_args *net = user_data;
  1333. struct wpa_ssid *ssid = net->ssid;
  1334. DBusMessageIter variant_iter;
  1335. /*
  1336. * Leveraging the fact that persistent group object is still
  1337. * represented in same manner as network within.
  1338. */
  1339. dbus_message_iter_recurse(iter, &variant_iter);
  1340. return set_network_properties(net->wpa_s, ssid, &variant_iter, error);
  1341. }
  1342. /**
  1343. * wpas_dbus_new_iface_add_persistent_group - Add a new configured
  1344. * persistent_group
  1345. * @message: Pointer to incoming dbus message
  1346. * @wpa_s: wpa_supplicant structure for a network interface
  1347. * Returns: A dbus message containing the object path of the new
  1348. * persistent group
  1349. *
  1350. * Handler function for "AddPersistentGroup" method call of a P2P Device
  1351. * interface.
  1352. */
  1353. DBusMessage * wpas_dbus_handler_add_persistent_group(
  1354. DBusMessage *message, struct wpa_supplicant *wpa_s)
  1355. {
  1356. DBusMessage *reply = NULL;
  1357. DBusMessageIter iter;
  1358. struct wpa_ssid *ssid = NULL;
  1359. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *path = path_buf;
  1360. DBusError error;
  1361. dbus_message_iter_init(message, &iter);
  1362. ssid = wpa_config_add_network(wpa_s->conf);
  1363. if (ssid == NULL) {
  1364. wpa_printf(MSG_ERROR, "dbus: %s: "
  1365. "Cannot add new persistent group", __func__);
  1366. reply = wpas_dbus_error_unknown_error(
  1367. message,
  1368. "wpa_supplicant could not add "
  1369. "a persistent group on this interface.");
  1370. goto err;
  1371. }
  1372. /* Mark the ssid as being a persistent group before the notification */
  1373. ssid->disabled = 2;
  1374. ssid->p2p_persistent_group = 1;
  1375. wpas_notify_persistent_group_added(wpa_s, ssid);
  1376. wpa_config_set_network_defaults(ssid);
  1377. dbus_error_init(&error);
  1378. if (!set_network_properties(wpa_s, ssid, &iter, &error)) {
  1379. wpa_printf(MSG_DEBUG, "dbus: %s: "
  1380. "Control interface could not set persistent group "
  1381. "properties", __func__);
  1382. reply = wpas_dbus_reply_new_from_error(message, &error,
  1383. DBUS_ERROR_INVALID_ARGS,
  1384. "Failed to set network "
  1385. "properties");
  1386. dbus_error_free(&error);
  1387. goto err;
  1388. }
  1389. /* Construct the object path for this network. */
  1390. os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
  1391. "%s/" WPAS_DBUS_NEW_PERSISTENT_GROUPS_PART "/%d",
  1392. wpa_s->dbus_new_path, ssid->id);
  1393. reply = dbus_message_new_method_return(message);
  1394. if (reply == NULL) {
  1395. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1396. NULL);
  1397. goto err;
  1398. }
  1399. if (!dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
  1400. DBUS_TYPE_INVALID)) {
  1401. dbus_message_unref(reply);
  1402. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1403. NULL);
  1404. goto err;
  1405. }
  1406. return reply;
  1407. err:
  1408. if (ssid) {
  1409. wpas_notify_persistent_group_removed(wpa_s, ssid);
  1410. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1411. }
  1412. return reply;
  1413. }
  1414. /**
  1415. * wpas_dbus_handler_remove_persistent_group - Remove a configured persistent
  1416. * group
  1417. * @message: Pointer to incoming dbus message
  1418. * @wpa_s: wpa_supplicant structure for a network interface
  1419. * Returns: NULL on success or dbus error on failure
  1420. *
  1421. * Handler function for "RemovePersistentGroup" method call of a P2P Device
  1422. * interface.
  1423. */
  1424. DBusMessage * wpas_dbus_handler_remove_persistent_group(
  1425. DBusMessage *message, struct wpa_supplicant *wpa_s)
  1426. {
  1427. DBusMessage *reply = NULL;
  1428. const char *op;
  1429. char *iface = NULL, *persistent_group_id = NULL;
  1430. int id;
  1431. struct wpa_ssid *ssid;
  1432. dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &op,
  1433. DBUS_TYPE_INVALID);
  1434. /*
  1435. * Extract the network ID and ensure the network is actually a child of
  1436. * this interface.
  1437. */
  1438. iface = wpas_dbus_new_decompose_object_path(op, 1,
  1439. &persistent_group_id,
  1440. NULL);
  1441. if (iface == NULL || os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
  1442. reply = wpas_dbus_error_invalid_args(message, op);
  1443. goto out;
  1444. }
  1445. id = strtoul(persistent_group_id, NULL, 10);
  1446. if (errno == EINVAL) {
  1447. reply = wpas_dbus_error_invalid_args(message, op);
  1448. goto out;
  1449. }
  1450. ssid = wpa_config_get_network(wpa_s->conf, id);
  1451. if (ssid == NULL) {
  1452. reply = wpas_dbus_error_persistent_group_unknown(message);
  1453. goto out;
  1454. }
  1455. wpas_notify_persistent_group_removed(wpa_s, ssid);
  1456. if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
  1457. wpa_printf(MSG_ERROR, "dbus: %s: "
  1458. "error occurred when removing persistent group %d",
  1459. __func__, id);
  1460. reply = wpas_dbus_error_unknown_error(
  1461. message,
  1462. "error removing the specified persistent group on "
  1463. "this interface.");
  1464. goto out;
  1465. }
  1466. out:
  1467. os_free(iface);
  1468. os_free(persistent_group_id);
  1469. return reply;
  1470. }
  1471. static void remove_persistent_group(struct wpa_supplicant *wpa_s,
  1472. struct wpa_ssid *ssid)
  1473. {
  1474. wpas_notify_persistent_group_removed(wpa_s, ssid);
  1475. if (wpa_config_remove_network(wpa_s->conf, ssid->id) < 0) {
  1476. wpa_printf(MSG_ERROR, "dbus: %s: "
  1477. "error occurred when removing persistent group %d",
  1478. __func__, ssid->id);
  1479. return;
  1480. }
  1481. }
  1482. /**
  1483. * wpas_dbus_handler_remove_all_persistent_groups - Remove all configured
  1484. * persistent groups
  1485. * @message: Pointer to incoming dbus message
  1486. * @wpa_s: wpa_supplicant structure for a network interface
  1487. * Returns: NULL on success or dbus error on failure
  1488. *
  1489. * Handler function for "RemoveAllPersistentGroups" method call of a
  1490. * P2P Device interface.
  1491. */
  1492. DBusMessage * wpas_dbus_handler_remove_all_persistent_groups(
  1493. DBusMessage *message, struct wpa_supplicant *wpa_s)
  1494. {
  1495. struct wpa_ssid *ssid, *next;
  1496. struct wpa_config *config;
  1497. config = wpa_s->conf;
  1498. ssid = config->ssid;
  1499. while (ssid) {
  1500. next = ssid->next;
  1501. if (network_is_persistent_group(ssid))
  1502. remove_persistent_group(wpa_s, ssid);
  1503. ssid = next;
  1504. }
  1505. return NULL;
  1506. }
  1507. /*
  1508. * Group object properties accessor methods
  1509. */
  1510. dbus_bool_t wpas_dbus_getter_p2p_group_members(DBusMessageIter *iter,
  1511. DBusError *error,
  1512. void *user_data)
  1513. {
  1514. struct wpa_supplicant *wpa_s = user_data;
  1515. struct wpa_ssid *ssid;
  1516. unsigned int num_members;
  1517. char **paths;
  1518. unsigned int i;
  1519. void *next = NULL;
  1520. const u8 *addr;
  1521. dbus_bool_t success = FALSE;
  1522. /* Verify correct role for this property */
  1523. if (wpas_get_p2p_role(wpa_s) != WPAS_P2P_ROLE_GO) {
  1524. return wpas_dbus_simple_array_property_getter(
  1525. iter, DBUS_TYPE_OBJECT_PATH, NULL, 0, error);
  1526. }
  1527. ssid = wpa_s->conf->ssid;
  1528. /* At present WPAS P2P_GO mode only applicable for p2p_go */
  1529. if (ssid->mode != WPAS_MODE_P2P_GO &&
  1530. ssid->mode != WPAS_MODE_AP &&
  1531. ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
  1532. return FALSE;
  1533. num_members = p2p_get_group_num_members(wpa_s->p2p_group);
  1534. paths = os_calloc(num_members, sizeof(char *));
  1535. if (!paths)
  1536. goto out_of_memory;
  1537. i = 0;
  1538. while ((addr = p2p_iterate_group_members(wpa_s->p2p_group, &next))) {
  1539. paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  1540. if (!paths[i])
  1541. goto out_of_memory;
  1542. os_snprintf(paths[i], WPAS_DBUS_OBJECT_PATH_MAX,
  1543. "%s/" WPAS_DBUS_NEW_P2P_GROUPMEMBERS_PART
  1544. "/" COMPACT_MACSTR,
  1545. wpa_s->dbus_groupobj_path, MAC2STR(addr));
  1546. i++;
  1547. }
  1548. success = wpas_dbus_simple_array_property_getter(iter,
  1549. DBUS_TYPE_OBJECT_PATH,
  1550. paths, num_members,
  1551. error);
  1552. for (i = 0; i < num_members; i++)
  1553. os_free(paths[i]);
  1554. os_free(paths);
  1555. return success;
  1556. out_of_memory:
  1557. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  1558. if (paths) {
  1559. for (i = 0; i < num_members; i++)
  1560. os_free(paths[i]);
  1561. os_free(paths);
  1562. }
  1563. return FALSE;
  1564. }
  1565. dbus_bool_t wpas_dbus_getter_p2p_group_ssid(DBusMessageIter *iter,
  1566. DBusError *error, void *user_data)
  1567. {
  1568. struct wpa_supplicant *wpa_s = user_data;
  1569. if (wpa_s->current_ssid == NULL)
  1570. return FALSE;
  1571. return wpas_dbus_simple_array_property_getter(
  1572. iter, DBUS_TYPE_BYTE, wpa_s->current_ssid->ssid,
  1573. wpa_s->current_ssid->ssid_len, error);
  1574. }
  1575. dbus_bool_t wpas_dbus_getter_p2p_group_bssid(DBusMessageIter *iter,
  1576. DBusError *error,
  1577. void *user_data)
  1578. {
  1579. struct wpa_supplicant *wpa_s = user_data;
  1580. u8 role = wpas_get_p2p_role(wpa_s);
  1581. u8 *p_bssid;
  1582. if (role == WPAS_P2P_ROLE_CLIENT) {
  1583. if (wpa_s->current_ssid == NULL)
  1584. return FALSE;
  1585. p_bssid = wpa_s->current_ssid->bssid;
  1586. } else {
  1587. if (wpa_s->ap_iface == NULL)
  1588. return FALSE;
  1589. p_bssid = wpa_s->ap_iface->bss[0]->own_addr;
  1590. }
  1591. return wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
  1592. p_bssid, ETH_ALEN,
  1593. error);
  1594. }
  1595. dbus_bool_t wpas_dbus_getter_p2p_group_frequency(DBusMessageIter *iter,
  1596. DBusError *error,
  1597. void *user_data)
  1598. {
  1599. struct wpa_supplicant *wpa_s = user_data;
  1600. u16 op_freq;
  1601. u8 role = wpas_get_p2p_role(wpa_s);
  1602. if (role == WPAS_P2P_ROLE_CLIENT) {
  1603. if (wpa_s->go_params == NULL)
  1604. return FALSE;
  1605. op_freq = wpa_s->go_params->freq;
  1606. } else {
  1607. if (wpa_s->ap_iface == NULL)
  1608. return FALSE;
  1609. op_freq = wpa_s->ap_iface->freq;
  1610. }
  1611. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT16,
  1612. &op_freq, error);
  1613. }
  1614. dbus_bool_t wpas_dbus_getter_p2p_group_passphrase(DBusMessageIter *iter,
  1615. DBusError *error,
  1616. void *user_data)
  1617. {
  1618. struct wpa_supplicant *wpa_s = user_data;
  1619. u8 role = wpas_get_p2p_role(wpa_s);
  1620. char *p_pass = NULL;
  1621. /* Verify correct role for this property */
  1622. if (role == WPAS_P2P_ROLE_GO) {
  1623. if (wpa_s->current_ssid == NULL)
  1624. return FALSE;
  1625. p_pass = wpa_s->current_ssid->passphrase;
  1626. } else
  1627. p_pass = "";
  1628. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  1629. &p_pass, error);
  1630. }
  1631. dbus_bool_t wpas_dbus_getter_p2p_group_psk(DBusMessageIter *iter,
  1632. DBusError *error, void *user_data)
  1633. {
  1634. struct wpa_supplicant *wpa_s = user_data;
  1635. u8 role = wpas_get_p2p_role(wpa_s);
  1636. u8 *p_psk = NULL;
  1637. u8 psk_len = 0;
  1638. /* Verify correct role for this property */
  1639. if (role == WPAS_P2P_ROLE_CLIENT) {
  1640. if (wpa_s->current_ssid == NULL)
  1641. return FALSE;
  1642. p_psk = wpa_s->current_ssid->psk;
  1643. psk_len = 32;
  1644. }
  1645. return wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
  1646. &p_psk, psk_len, error);
  1647. }
  1648. dbus_bool_t wpas_dbus_getter_p2p_group_vendor_ext(DBusMessageIter *iter,
  1649. DBusError *error,
  1650. void *user_data)
  1651. {
  1652. struct wpa_supplicant *wpa_s = user_data;
  1653. struct hostapd_data *hapd;
  1654. struct wpabuf *vendor_ext[MAX_WPS_VENDOR_EXTENSIONS];
  1655. int num_vendor_ext = 0;
  1656. int i;
  1657. /* Verify correct role for this property */
  1658. if (wpas_get_p2p_role(wpa_s) == WPAS_P2P_ROLE_GO) {
  1659. if (wpa_s->ap_iface == NULL)
  1660. return FALSE;
  1661. hapd = wpa_s->ap_iface->bss[0];
  1662. /* Parse WPS Vendor Extensions sent in Beacon/Probe Response */
  1663. for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
  1664. if (hapd->conf->wps_vendor_ext[i] == NULL)
  1665. vendor_ext[i] = NULL;
  1666. else {
  1667. vendor_ext[num_vendor_ext++] =
  1668. hapd->conf->wps_vendor_ext[i];
  1669. }
  1670. }
  1671. }
  1672. /* Return vendor extensions or no data */
  1673. return wpas_dbus_simple_array_array_property_getter(iter,
  1674. DBUS_TYPE_BYTE,
  1675. vendor_ext,
  1676. num_vendor_ext,
  1677. error);
  1678. }
  1679. dbus_bool_t wpas_dbus_setter_p2p_group_vendor_ext(DBusMessageIter *iter,
  1680. DBusError *error,
  1681. void *user_data)
  1682. {
  1683. struct wpa_supplicant *wpa_s = user_data;
  1684. DBusMessageIter variant_iter, iter_dict;
  1685. struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
  1686. unsigned int i;
  1687. struct hostapd_data *hapd = NULL;
  1688. if (wpas_get_p2p_role(wpa_s) == WPAS_P2P_ROLE_GO &&
  1689. wpa_s->ap_iface != NULL)
  1690. hapd = wpa_s->ap_iface->bss[0];
  1691. else
  1692. return FALSE;
  1693. dbus_message_iter_recurse(iter, &variant_iter);
  1694. if (!wpa_dbus_dict_open_read(&variant_iter, &iter_dict, error))
  1695. return FALSE;
  1696. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  1697. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
  1698. dbus_set_error_const(error, DBUS_ERROR_INVALID_ARGS,
  1699. "invalid message format");
  1700. return FALSE;
  1701. }
  1702. if (os_strcmp(entry.key, "WPSVendorExtensions") == 0) {
  1703. if (entry.type != DBUS_TYPE_ARRAY ||
  1704. entry.array_type != WPAS_DBUS_TYPE_BINARRAY ||
  1705. entry.array_len > MAX_WPS_VENDOR_EXTENSIONS)
  1706. goto error;
  1707. for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
  1708. if (i < entry.array_len) {
  1709. hapd->conf->wps_vendor_ext[i] =
  1710. entry.binarray_value[i];
  1711. entry.binarray_value[i] = NULL;
  1712. } else
  1713. hapd->conf->wps_vendor_ext[i] = NULL;
  1714. }
  1715. hostapd_update_wps(hapd);
  1716. } else
  1717. goto error;
  1718. wpa_dbus_dict_entry_clear(&entry);
  1719. }
  1720. return TRUE;
  1721. error:
  1722. wpa_dbus_dict_entry_clear(&entry);
  1723. dbus_set_error_const(error, DBUS_ERROR_INVALID_ARGS,
  1724. "invalid message format");
  1725. return FALSE;
  1726. }
  1727. DBusMessage * wpas_dbus_handler_p2p_add_service(DBusMessage *message,
  1728. struct wpa_supplicant *wpa_s)
  1729. {
  1730. DBusMessageIter iter_dict;
  1731. DBusMessage *reply = NULL;
  1732. DBusMessageIter iter;
  1733. struct wpa_dbus_dict_entry entry;
  1734. int upnp = 0;
  1735. int bonjour = 0;
  1736. char *service = NULL;
  1737. struct wpabuf *query = NULL;
  1738. struct wpabuf *resp = NULL;
  1739. u8 version = 0;
  1740. dbus_message_iter_init(message, &iter);
  1741. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  1742. goto error;
  1743. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  1744. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  1745. goto error;
  1746. if (!os_strcmp(entry.key, "service_type") &&
  1747. (entry.type == DBUS_TYPE_STRING)) {
  1748. if (!os_strcmp(entry.str_value, "upnp"))
  1749. upnp = 1;
  1750. else if (!os_strcmp(entry.str_value, "bonjour"))
  1751. bonjour = 1;
  1752. else
  1753. goto error_clear;
  1754. } else if (!os_strcmp(entry.key, "version") &&
  1755. entry.type == DBUS_TYPE_INT32) {
  1756. version = entry.uint32_value;
  1757. } else if (!os_strcmp(entry.key, "service") &&
  1758. (entry.type == DBUS_TYPE_STRING)) {
  1759. service = os_strdup(entry.str_value);
  1760. } else if (!os_strcmp(entry.key, "query")) {
  1761. if ((entry.type != DBUS_TYPE_ARRAY) ||
  1762. (entry.array_type != DBUS_TYPE_BYTE))
  1763. goto error_clear;
  1764. query = wpabuf_alloc_copy(
  1765. entry.bytearray_value,
  1766. entry.array_len);
  1767. } else if (!os_strcmp(entry.key, "response")) {
  1768. if ((entry.type != DBUS_TYPE_ARRAY) ||
  1769. (entry.array_type != DBUS_TYPE_BYTE))
  1770. goto error_clear;
  1771. resp = wpabuf_alloc_copy(entry.bytearray_value,
  1772. entry.array_len);
  1773. }
  1774. wpa_dbus_dict_entry_clear(&entry);
  1775. }
  1776. if (upnp == 1) {
  1777. if (version <= 0 || service == NULL)
  1778. goto error;
  1779. if (wpas_p2p_service_add_upnp(wpa_s, version, service) != 0)
  1780. goto error;
  1781. os_free(service);
  1782. service = NULL;
  1783. } else if (bonjour == 1) {
  1784. if (query == NULL || resp == NULL)
  1785. goto error;
  1786. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0)
  1787. goto error;
  1788. query = NULL;
  1789. resp = NULL;
  1790. } else
  1791. goto error;
  1792. return reply;
  1793. error_clear:
  1794. wpa_dbus_dict_entry_clear(&entry);
  1795. error:
  1796. os_free(service);
  1797. wpabuf_free(query);
  1798. wpabuf_free(resp);
  1799. return wpas_dbus_error_invalid_args(message, NULL);
  1800. }
  1801. DBusMessage * wpas_dbus_handler_p2p_delete_service(
  1802. DBusMessage *message, struct wpa_supplicant *wpa_s)
  1803. {
  1804. DBusMessageIter iter_dict;
  1805. DBusMessage *reply = NULL;
  1806. DBusMessageIter iter;
  1807. struct wpa_dbus_dict_entry entry;
  1808. int upnp = 0;
  1809. int bonjour = 0;
  1810. int ret = 0;
  1811. char *service = NULL;
  1812. struct wpabuf *query = NULL;
  1813. u8 version = 0;
  1814. dbus_message_iter_init(message, &iter);
  1815. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  1816. goto error;
  1817. if (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  1818. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  1819. goto error;
  1820. if (!os_strcmp(entry.key, "service_type") &&
  1821. (entry.type == DBUS_TYPE_STRING)) {
  1822. if (!os_strcmp(entry.str_value, "upnp"))
  1823. upnp = 1;
  1824. else if (!os_strcmp(entry.str_value, "bonjour"))
  1825. bonjour = 1;
  1826. else
  1827. goto error_clear;
  1828. wpa_dbus_dict_entry_clear(&entry);
  1829. }
  1830. }
  1831. if (upnp == 1) {
  1832. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  1833. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  1834. goto error;
  1835. if (!os_strcmp(entry.key, "version") &&
  1836. entry.type == DBUS_TYPE_INT32)
  1837. version = entry.uint32_value;
  1838. else if (!os_strcmp(entry.key, "service") &&
  1839. entry.type == DBUS_TYPE_STRING)
  1840. service = os_strdup(entry.str_value);
  1841. else
  1842. goto error_clear;
  1843. wpa_dbus_dict_entry_clear(&entry);
  1844. }
  1845. if (version <= 0 || service == NULL)
  1846. goto error;
  1847. ret = wpas_p2p_service_del_upnp(wpa_s, version, service);
  1848. os_free(service);
  1849. if (ret != 0)
  1850. goto error;
  1851. } else if (bonjour == 1) {
  1852. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  1853. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  1854. goto error;
  1855. if (!os_strcmp(entry.key, "query")) {
  1856. if ((entry.type != DBUS_TYPE_ARRAY) ||
  1857. (entry.array_type != DBUS_TYPE_BYTE))
  1858. goto error_clear;
  1859. query = wpabuf_alloc_copy(
  1860. entry.bytearray_value,
  1861. entry.array_len);
  1862. } else
  1863. goto error_clear;
  1864. wpa_dbus_dict_entry_clear(&entry);
  1865. }
  1866. if (query == NULL)
  1867. goto error;
  1868. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  1869. if (ret != 0)
  1870. goto error;
  1871. wpabuf_free(query);
  1872. } else
  1873. goto error;
  1874. return reply;
  1875. error_clear:
  1876. wpa_dbus_dict_entry_clear(&entry);
  1877. error:
  1878. return wpas_dbus_error_invalid_args(message, NULL);
  1879. }
  1880. DBusMessage * wpas_dbus_handler_p2p_flush_service(DBusMessage *message,
  1881. struct wpa_supplicant *wpa_s)
  1882. {
  1883. wpas_p2p_service_flush(wpa_s);
  1884. return NULL;
  1885. }
  1886. DBusMessage * wpas_dbus_handler_p2p_service_sd_req(
  1887. DBusMessage *message, struct wpa_supplicant *wpa_s)
  1888. {
  1889. DBusMessageIter iter_dict;
  1890. DBusMessage *reply = NULL;
  1891. DBusMessageIter iter;
  1892. struct wpa_dbus_dict_entry entry;
  1893. int upnp = 0;
  1894. char *service = NULL;
  1895. char *peer_object_path = NULL;
  1896. struct wpabuf *tlv = NULL;
  1897. u8 version = 0;
  1898. u64 ref = 0;
  1899. u8 addr_buf[ETH_ALEN], *addr;
  1900. dbus_message_iter_init(message, &iter);
  1901. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  1902. goto error;
  1903. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  1904. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  1905. goto error;
  1906. if (!os_strcmp(entry.key, "peer_object") &&
  1907. entry.type == DBUS_TYPE_OBJECT_PATH) {
  1908. peer_object_path = os_strdup(entry.str_value);
  1909. } else if (!os_strcmp(entry.key, "service_type") &&
  1910. entry.type == DBUS_TYPE_STRING) {
  1911. if (!os_strcmp(entry.str_value, "upnp"))
  1912. upnp = 1;
  1913. else
  1914. goto error_clear;
  1915. } else if (!os_strcmp(entry.key, "version") &&
  1916. entry.type == DBUS_TYPE_INT32) {
  1917. version = entry.uint32_value;
  1918. } else if (!os_strcmp(entry.key, "service") &&
  1919. entry.type == DBUS_TYPE_STRING) {
  1920. service = os_strdup(entry.str_value);
  1921. } else if (!os_strcmp(entry.key, "tlv")) {
  1922. if (entry.type != DBUS_TYPE_ARRAY ||
  1923. entry.array_type != DBUS_TYPE_BYTE)
  1924. goto error_clear;
  1925. tlv = wpabuf_alloc_copy(entry.bytearray_value,
  1926. entry.array_len);
  1927. } else
  1928. goto error_clear;
  1929. wpa_dbus_dict_entry_clear(&entry);
  1930. }
  1931. if (!peer_object_path) {
  1932. addr = NULL;
  1933. } else {
  1934. if (parse_peer_object_path(peer_object_path, addr_buf) < 0 ||
  1935. !p2p_peer_known(wpa_s->global->p2p, addr_buf))
  1936. goto error;
  1937. addr = addr_buf;
  1938. }
  1939. if (upnp == 1) {
  1940. if (version <= 0 || service == NULL)
  1941. goto error;
  1942. ref = wpas_p2p_sd_request_upnp(wpa_s, addr, version, service);
  1943. } else {
  1944. if (tlv == NULL)
  1945. goto error;
  1946. ref = wpas_p2p_sd_request(wpa_s, addr, tlv);
  1947. wpabuf_free(tlv);
  1948. }
  1949. if (ref != 0) {
  1950. reply = dbus_message_new_method_return(message);
  1951. dbus_message_append_args(reply, DBUS_TYPE_UINT64,
  1952. &ref, DBUS_TYPE_INVALID);
  1953. } else {
  1954. reply = wpas_dbus_error_unknown_error(
  1955. message, "Unable to send SD request");
  1956. }
  1957. out:
  1958. os_free(service);
  1959. os_free(peer_object_path);
  1960. return reply;
  1961. error_clear:
  1962. wpa_dbus_dict_entry_clear(&entry);
  1963. error:
  1964. if (tlv)
  1965. wpabuf_free(tlv);
  1966. reply = wpas_dbus_error_invalid_args(message, NULL);
  1967. goto out;
  1968. }
  1969. DBusMessage * wpas_dbus_handler_p2p_service_sd_res(
  1970. DBusMessage *message, struct wpa_supplicant *wpa_s)
  1971. {
  1972. DBusMessageIter iter_dict;
  1973. DBusMessage *reply = NULL;
  1974. DBusMessageIter iter;
  1975. struct wpa_dbus_dict_entry entry;
  1976. char *peer_object_path = NULL;
  1977. struct wpabuf *tlv = NULL;
  1978. int freq = 0;
  1979. int dlg_tok = 0;
  1980. u8 addr[ETH_ALEN];
  1981. dbus_message_iter_init(message, &iter);
  1982. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  1983. goto error;
  1984. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  1985. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  1986. goto error;
  1987. if (!os_strcmp(entry.key, "peer_object") &&
  1988. entry.type == DBUS_TYPE_OBJECT_PATH) {
  1989. peer_object_path = os_strdup(entry.str_value);
  1990. } else if (!os_strcmp(entry.key, "frequency") &&
  1991. entry.type == DBUS_TYPE_INT32) {
  1992. freq = entry.uint32_value;
  1993. } else if (!os_strcmp(entry.key, "dialog_token") &&
  1994. entry.type == DBUS_TYPE_UINT32) {
  1995. dlg_tok = entry.uint32_value;
  1996. } else if (!os_strcmp(entry.key, "tlvs")) {
  1997. if (entry.type != DBUS_TYPE_ARRAY ||
  1998. entry.array_type != DBUS_TYPE_BYTE)
  1999. goto error_clear;
  2000. tlv = wpabuf_alloc_copy(entry.bytearray_value,
  2001. entry.array_len);
  2002. } else
  2003. goto error_clear;
  2004. wpa_dbus_dict_entry_clear(&entry);
  2005. }
  2006. if (!peer_object_path ||
  2007. (parse_peer_object_path(peer_object_path, addr) < 0) ||
  2008. !p2p_peer_known(wpa_s->global->p2p, addr))
  2009. goto error;
  2010. if (tlv == NULL)
  2011. goto error;
  2012. wpas_p2p_sd_response(wpa_s, freq, addr, (u8) dlg_tok, tlv);
  2013. wpabuf_free(tlv);
  2014. out:
  2015. os_free(peer_object_path);
  2016. return reply;
  2017. error_clear:
  2018. wpa_dbus_dict_entry_clear(&entry);
  2019. error:
  2020. reply = wpas_dbus_error_invalid_args(message, NULL);
  2021. goto out;
  2022. }
  2023. DBusMessage * wpas_dbus_handler_p2p_service_sd_cancel_req(
  2024. DBusMessage *message, struct wpa_supplicant *wpa_s)
  2025. {
  2026. DBusMessageIter iter;
  2027. u64 req = 0;
  2028. dbus_message_iter_init(message, &iter);
  2029. dbus_message_iter_get_basic(&iter, &req);
  2030. if (req == 0)
  2031. goto error;
  2032. if (!wpas_p2p_sd_cancel_request(wpa_s, req))
  2033. goto error;
  2034. return NULL;
  2035. error:
  2036. return wpas_dbus_error_invalid_args(message, NULL);
  2037. }
  2038. DBusMessage * wpas_dbus_handler_p2p_service_update(
  2039. DBusMessage *message, struct wpa_supplicant *wpa_s)
  2040. {
  2041. wpas_p2p_sd_service_update(wpa_s);
  2042. return NULL;
  2043. }
  2044. DBusMessage * wpas_dbus_handler_p2p_serv_disc_external(
  2045. DBusMessage *message, struct wpa_supplicant *wpa_s)
  2046. {
  2047. DBusMessageIter iter;
  2048. int ext = 0;
  2049. dbus_message_iter_init(message, &iter);
  2050. dbus_message_iter_get_basic(&iter, &ext);
  2051. wpa_s->p2p_sd_over_ctrl_iface = ext;
  2052. return NULL;
  2053. }