dbus_new_handlers_p2p.c 71 KB

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