dbus_new_handlers_p2p.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779
  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. /**
  480. * wpas_dbus_handler_p2p_cancel - Cancel P2P group formation
  481. * @message: Pointer to incoming dbus message
  482. * @wpa_s: %wpa_supplicant data structure
  483. * Returns: NULL on success or DBus error on failure
  484. *
  485. * Handler for "Cancel" method call. Returns NULL if P2P cancel succeeds or DBus
  486. * error on P2P cancel failure
  487. */
  488. DBusMessage * wpas_dbus_handler_p2p_cancel(DBusMessage *message,
  489. struct wpa_supplicant *wpa_s)
  490. {
  491. if (wpas_p2p_cancel(wpa_s))
  492. return wpas_dbus_error_unknown_error(message,
  493. "P2P cancel failed");
  494. return NULL;
  495. }
  496. DBusMessage * wpas_dbus_handler_p2p_invite(DBusMessage *message,
  497. struct wpa_supplicant *wpa_s)
  498. {
  499. DBusMessageIter iter_dict;
  500. DBusMessage *reply = NULL;
  501. DBusMessageIter iter;
  502. struct wpa_dbus_dict_entry entry;
  503. char *peer_object_path = NULL;
  504. char *pg_object_path = NULL;
  505. char *iface = NULL;
  506. u8 peer_addr[ETH_ALEN];
  507. unsigned int group_id = 0;
  508. int persistent = 0;
  509. struct wpa_ssid *ssid;
  510. if (!wpa_dbus_p2p_check_enabled(wpa_s, message, &reply, NULL))
  511. return reply;
  512. dbus_message_iter_init(message, &iter);
  513. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  514. goto err;
  515. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  516. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  517. goto err;
  518. if (os_strcmp(entry.key, "peer") == 0 &&
  519. entry.type == DBUS_TYPE_OBJECT_PATH) {
  520. peer_object_path = os_strdup(entry.str_value);
  521. wpa_dbus_dict_entry_clear(&entry);
  522. } else if (os_strcmp(entry.key, "persistent_group_object") ==
  523. 0 &&
  524. entry.type == DBUS_TYPE_OBJECT_PATH) {
  525. pg_object_path = os_strdup(entry.str_value);
  526. persistent = 1;
  527. wpa_dbus_dict_entry_clear(&entry);
  528. } else {
  529. wpa_dbus_dict_entry_clear(&entry);
  530. goto err;
  531. }
  532. }
  533. if (parse_peer_object_path(peer_object_path, peer_addr) < 0 ||
  534. !p2p_peer_known(wpa_s->global->p2p, peer_addr))
  535. goto err;
  536. wpa_s = wpa_s->global->p2p_init_wpa_s;
  537. if (persistent) {
  538. char *net_id_str;
  539. /*
  540. * A group ID is defined meaning we want to re-invoke a
  541. * persistent group
  542. */
  543. iface = wpas_dbus_new_decompose_object_path(
  544. pg_object_path,
  545. WPAS_DBUS_NEW_PERSISTENT_GROUPS_PART,
  546. &net_id_str);
  547. if (iface == NULL || net_id_str == NULL ||
  548. !wpa_s->parent->dbus_new_path ||
  549. os_strcmp(iface, wpa_s->parent->dbus_new_path) != 0) {
  550. reply = wpas_dbus_error_invalid_args(message,
  551. pg_object_path);
  552. goto out;
  553. }
  554. group_id = strtoul(net_id_str, NULL, 10);
  555. if (errno == EINVAL) {
  556. reply = wpas_dbus_error_invalid_args(
  557. message, pg_object_path);
  558. goto out;
  559. }
  560. /* Get the SSID structure from the persistent group id */
  561. ssid = wpa_config_get_network(wpa_s->conf, group_id);
  562. if (ssid == NULL || ssid->disabled != 2)
  563. goto err;
  564. if (wpas_p2p_invite(wpa_s, peer_addr, ssid, NULL, 0, 0, 0, 0) <
  565. 0) {
  566. reply = wpas_dbus_error_unknown_error(
  567. message,
  568. "Failed to reinvoke a persistent group");
  569. goto out;
  570. }
  571. } else {
  572. /*
  573. * No group ID means propose to a peer to join my active group
  574. */
  575. if (wpas_p2p_invite_group(wpa_s, wpa_s->ifname,
  576. peer_addr, NULL)) {
  577. reply = wpas_dbus_error_unknown_error(
  578. message, "Failed to join to an active group");
  579. goto out;
  580. }
  581. }
  582. out:
  583. os_free(iface);
  584. os_free(pg_object_path);
  585. os_free(peer_object_path);
  586. return reply;
  587. err:
  588. reply = wpas_dbus_error_invalid_args(message, NULL);
  589. goto out;
  590. }
  591. DBusMessage * wpas_dbus_handler_p2p_prov_disc_req(DBusMessage *message,
  592. struct wpa_supplicant *wpa_s)
  593. {
  594. DBusMessageIter iter;
  595. char *peer_object_path = NULL;
  596. char *config_method = NULL;
  597. u8 peer_addr[ETH_ALEN];
  598. dbus_message_iter_init(message, &iter);
  599. dbus_message_iter_get_basic(&iter, &peer_object_path);
  600. if (parse_peer_object_path(peer_object_path, peer_addr) < 0)
  601. return wpas_dbus_error_invalid_args(message, NULL);
  602. dbus_message_iter_next(&iter);
  603. dbus_message_iter_get_basic(&iter, &config_method);
  604. /*
  605. * Validation checks on config_method are being duplicated here
  606. * to be able to return invalid args reply since the error code
  607. * from p2p module are not granular enough (yet).
  608. */
  609. if (os_strcmp(config_method, "display") &&
  610. os_strcmp(config_method, "keypad") &&
  611. os_strcmp(config_method, "pbc") &&
  612. os_strcmp(config_method, "pushbutton"))
  613. return wpas_dbus_error_invalid_args(message, NULL);
  614. wpa_s = wpa_s->global->p2p_init_wpa_s;
  615. if (wpas_p2p_prov_disc(wpa_s, peer_addr, config_method,
  616. WPAS_P2P_PD_FOR_GO_NEG, NULL) < 0)
  617. return wpas_dbus_error_unknown_error(message,
  618. "Failed to send provision discovery request");
  619. return NULL;
  620. }
  621. /*
  622. * P2P Device property accessor methods.
  623. */
  624. dbus_bool_t wpas_dbus_getter_p2p_device_config(DBusMessageIter *iter,
  625. DBusError *error,
  626. void *user_data)
  627. {
  628. struct wpa_supplicant *wpa_s = user_data;
  629. DBusMessageIter variant_iter, dict_iter;
  630. DBusMessageIter iter_secdev_dict_entry, iter_secdev_dict_val,
  631. iter_secdev_dict_array;
  632. const char *dev_name;
  633. int num_vendor_extensions = 0;
  634. int i;
  635. const struct wpabuf *vendor_ext[P2P_MAX_WPS_VENDOR_EXT];
  636. if (!wpa_dbus_p2p_check_enabled(wpa_s, NULL, NULL, error))
  637. return FALSE;
  638. wpa_s = wpa_s->global->p2p_init_wpa_s;
  639. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  640. "a{sv}", &variant_iter) ||
  641. !wpa_dbus_dict_open_write(&variant_iter, &dict_iter))
  642. goto err_no_mem;
  643. /* DeviceName */
  644. dev_name = wpa_s->conf->device_name;
  645. if (dev_name &&
  646. !wpa_dbus_dict_append_string(&dict_iter, "DeviceName", dev_name))
  647. goto err_no_mem;
  648. /* Primary device type */
  649. if (!wpa_dbus_dict_append_byte_array(&dict_iter, "PrimaryDeviceType",
  650. (char *) wpa_s->conf->device_type,
  651. WPS_DEV_TYPE_LEN))
  652. goto err_no_mem;
  653. /* Secondary device types */
  654. if (wpa_s->conf->num_sec_device_types) {
  655. if (!wpa_dbus_dict_begin_array(&dict_iter,
  656. "SecondaryDeviceTypes",
  657. DBUS_TYPE_ARRAY_AS_STRING
  658. DBUS_TYPE_BYTE_AS_STRING,
  659. &iter_secdev_dict_entry,
  660. &iter_secdev_dict_val,
  661. &iter_secdev_dict_array))
  662. goto err_no_mem;
  663. for (i = 0; i < wpa_s->conf->num_sec_device_types; i++)
  664. wpa_dbus_dict_bin_array_add_element(
  665. &iter_secdev_dict_array,
  666. wpa_s->conf->sec_device_type[i],
  667. WPS_DEV_TYPE_LEN);
  668. if (!wpa_dbus_dict_end_array(&dict_iter,
  669. &iter_secdev_dict_entry,
  670. &iter_secdev_dict_val,
  671. &iter_secdev_dict_array))
  672. goto err_no_mem;
  673. }
  674. /* Vendor Extensions */
  675. for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
  676. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  677. continue;
  678. vendor_ext[num_vendor_extensions++] =
  679. wpa_s->conf->wps_vendor_ext[i];
  680. }
  681. if ((num_vendor_extensions &&
  682. !wpa_dbus_dict_append_wpabuf_array(&dict_iter,
  683. "VendorExtension",
  684. vendor_ext,
  685. num_vendor_extensions)) ||
  686. !wpa_dbus_dict_append_uint32(&dict_iter, "GOIntent",
  687. wpa_s->conf->p2p_go_intent) ||
  688. !wpa_dbus_dict_append_bool(&dict_iter, "PersistentReconnect",
  689. wpa_s->conf->persistent_reconnect) ||
  690. !wpa_dbus_dict_append_uint32(&dict_iter, "ListenRegClass",
  691. wpa_s->conf->p2p_listen_reg_class) ||
  692. !wpa_dbus_dict_append_uint32(&dict_iter, "ListenChannel",
  693. wpa_s->conf->p2p_listen_channel) ||
  694. !wpa_dbus_dict_append_uint32(&dict_iter, "OperRegClass",
  695. wpa_s->conf->p2p_oper_reg_class) ||
  696. !wpa_dbus_dict_append_uint32(&dict_iter, "OperChannel",
  697. wpa_s->conf->p2p_oper_channel) ||
  698. (wpa_s->conf->p2p_ssid_postfix &&
  699. !wpa_dbus_dict_append_string(&dict_iter, "SsidPostfix",
  700. wpa_s->conf->p2p_ssid_postfix)) ||
  701. !wpa_dbus_dict_append_bool(&dict_iter, "IntraBss",
  702. wpa_s->conf->p2p_intra_bss) ||
  703. !wpa_dbus_dict_append_uint32(&dict_iter, "GroupIdle",
  704. wpa_s->conf->p2p_group_idle) ||
  705. !wpa_dbus_dict_append_uint32(&dict_iter, "disassoc_low_ack",
  706. wpa_s->conf->disassoc_low_ack) ||
  707. !wpa_dbus_dict_append_bool(&dict_iter, "NoGroupIface",
  708. wpa_s->conf->p2p_no_group_iface) ||
  709. !wpa_dbus_dict_append_uint32(&dict_iter, "p2p_search_delay",
  710. wpa_s->conf->p2p_search_delay) ||
  711. !wpa_dbus_dict_close_write(&variant_iter, &dict_iter) ||
  712. !dbus_message_iter_close_container(iter, &variant_iter))
  713. goto err_no_mem;
  714. return TRUE;
  715. err_no_mem:
  716. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  717. return FALSE;
  718. }
  719. dbus_bool_t wpas_dbus_setter_p2p_device_config(DBusMessageIter *iter,
  720. DBusError *error,
  721. void *user_data)
  722. {
  723. struct wpa_supplicant *wpa_s = user_data;
  724. DBusMessageIter variant_iter, iter_dict;
  725. struct wpa_dbus_dict_entry entry = {.type = DBUS_TYPE_STRING };
  726. unsigned int i;
  727. if (!wpa_dbus_p2p_check_enabled(wpa_s, NULL, NULL, error))
  728. return FALSE;
  729. wpa_s = wpa_s->global->p2p_init_wpa_s;
  730. dbus_message_iter_recurse(iter, &variant_iter);
  731. if (!wpa_dbus_dict_open_read(&variant_iter, &iter_dict, error))
  732. return FALSE;
  733. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  734. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
  735. dbus_set_error_const(error, DBUS_ERROR_INVALID_ARGS,
  736. "invalid message format");
  737. return FALSE;
  738. }
  739. if (os_strcmp(entry.key, "DeviceName") == 0) {
  740. char *devname;
  741. if (entry.type != DBUS_TYPE_STRING)
  742. goto error;
  743. devname = os_strdup(entry.str_value);
  744. if (devname == NULL)
  745. goto err_no_mem_clear;
  746. os_free(wpa_s->conf->device_name);
  747. wpa_s->conf->device_name = devname;
  748. wpa_s->conf->changed_parameters |=
  749. CFG_CHANGED_DEVICE_NAME;
  750. } else if (os_strcmp(entry.key, "PrimaryDeviceType") == 0) {
  751. if (entry.type != DBUS_TYPE_ARRAY ||
  752. entry.array_type != DBUS_TYPE_BYTE ||
  753. entry.array_len != WPS_DEV_TYPE_LEN)
  754. goto error;
  755. os_memcpy(wpa_s->conf->device_type,
  756. entry.bytearray_value,
  757. WPS_DEV_TYPE_LEN);
  758. wpa_s->conf->changed_parameters |=
  759. CFG_CHANGED_DEVICE_TYPE;
  760. } else if (os_strcmp(entry.key, "SecondaryDeviceTypes") == 0) {
  761. if (entry.type != DBUS_TYPE_ARRAY ||
  762. entry.array_type != WPAS_DBUS_TYPE_BINARRAY ||
  763. entry.array_len > MAX_SEC_DEVICE_TYPES)
  764. goto error;
  765. for (i = 0; i < entry.array_len; i++)
  766. if (wpabuf_len(entry.binarray_value[i]) !=
  767. WPS_DEV_TYPE_LEN)
  768. goto err_no_mem_clear;
  769. for (i = 0; i < entry.array_len; i++)
  770. os_memcpy(wpa_s->conf->sec_device_type[i],
  771. wpabuf_head(entry.binarray_value[i]),
  772. WPS_DEV_TYPE_LEN);
  773. wpa_s->conf->num_sec_device_types = entry.array_len;
  774. wpa_s->conf->changed_parameters |=
  775. CFG_CHANGED_SEC_DEVICE_TYPE;
  776. } else if (os_strcmp(entry.key, "VendorExtension") == 0) {
  777. if (entry.type != DBUS_TYPE_ARRAY ||
  778. entry.array_type != WPAS_DBUS_TYPE_BINARRAY ||
  779. (entry.array_len > P2P_MAX_WPS_VENDOR_EXT))
  780. goto error;
  781. wpa_s->conf->changed_parameters |=
  782. CFG_CHANGED_VENDOR_EXTENSION;
  783. for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
  784. wpabuf_free(wpa_s->conf->wps_vendor_ext[i]);
  785. if (i < entry.array_len) {
  786. wpa_s->conf->wps_vendor_ext[i] =
  787. entry.binarray_value[i];
  788. entry.binarray_value[i] = NULL;
  789. } else
  790. wpa_s->conf->wps_vendor_ext[i] = NULL;
  791. }
  792. } else if (os_strcmp(entry.key, "GOIntent") == 0 &&
  793. entry.type == DBUS_TYPE_UINT32 &&
  794. (entry.uint32_value <= 15))
  795. wpa_s->conf->p2p_go_intent = entry.uint32_value;
  796. else if (os_strcmp(entry.key, "PersistentReconnect") == 0 &&
  797. entry.type == DBUS_TYPE_BOOLEAN)
  798. wpa_s->conf->persistent_reconnect = entry.bool_value;
  799. else if (os_strcmp(entry.key, "ListenRegClass") == 0 &&
  800. entry.type == DBUS_TYPE_UINT32) {
  801. wpa_s->conf->p2p_listen_reg_class = entry.uint32_value;
  802. wpa_s->conf->changed_parameters |=
  803. CFG_CHANGED_P2P_LISTEN_CHANNEL;
  804. } else if (os_strcmp(entry.key, "ListenChannel") == 0 &&
  805. entry.type == DBUS_TYPE_UINT32) {
  806. wpa_s->conf->p2p_listen_channel = entry.uint32_value;
  807. wpa_s->conf->changed_parameters |=
  808. CFG_CHANGED_P2P_LISTEN_CHANNEL;
  809. } else if (os_strcmp(entry.key, "OperRegClass") == 0 &&
  810. entry.type == DBUS_TYPE_UINT32) {
  811. wpa_s->conf->p2p_oper_reg_class = entry.uint32_value;
  812. wpa_s->conf->changed_parameters |=
  813. CFG_CHANGED_P2P_OPER_CHANNEL;
  814. } else if (os_strcmp(entry.key, "OperChannel") == 0 &&
  815. entry.type == DBUS_TYPE_UINT32) {
  816. wpa_s->conf->p2p_oper_channel = entry.uint32_value;
  817. wpa_s->conf->changed_parameters |=
  818. CFG_CHANGED_P2P_OPER_CHANNEL;
  819. } else if (os_strcmp(entry.key, "SsidPostfix") == 0) {
  820. char *postfix;
  821. if (entry.type != DBUS_TYPE_STRING)
  822. goto error;
  823. postfix = os_strdup(entry.str_value);
  824. if (!postfix)
  825. goto err_no_mem_clear;
  826. os_free(wpa_s->conf->p2p_ssid_postfix);
  827. wpa_s->conf->p2p_ssid_postfix = postfix;
  828. wpa_s->conf->changed_parameters |=
  829. CFG_CHANGED_P2P_SSID_POSTFIX;
  830. } else if (os_strcmp(entry.key, "IntraBss") == 0 &&
  831. entry.type == DBUS_TYPE_BOOLEAN) {
  832. wpa_s->conf->p2p_intra_bss = entry.bool_value;
  833. wpa_s->conf->changed_parameters |=
  834. CFG_CHANGED_P2P_INTRA_BSS;
  835. } else if (os_strcmp(entry.key, "GroupIdle") == 0 &&
  836. entry.type == DBUS_TYPE_UINT32)
  837. wpa_s->conf->p2p_group_idle = entry.uint32_value;
  838. else if (os_strcmp(entry.key, "disassoc_low_ack") == 0 &&
  839. entry.type == DBUS_TYPE_UINT32)
  840. wpa_s->conf->disassoc_low_ack = entry.uint32_value;
  841. else if (os_strcmp(entry.key, "NoGroupIface") == 0 &&
  842. entry.type == DBUS_TYPE_BOOLEAN)
  843. wpa_s->conf->p2p_no_group_iface = entry.bool_value;
  844. else if (os_strcmp(entry.key, "p2p_search_delay") == 0 &&
  845. entry.type == DBUS_TYPE_UINT32)
  846. wpa_s->conf->p2p_search_delay = entry.uint32_value;
  847. else
  848. goto error;
  849. wpa_dbus_dict_entry_clear(&entry);
  850. }
  851. if (wpa_s->conf->changed_parameters) {
  852. /* Some changed parameters requires to update config*/
  853. wpa_supplicant_update_config(wpa_s);
  854. }
  855. return TRUE;
  856. error:
  857. dbus_set_error_const(error, DBUS_ERROR_INVALID_ARGS,
  858. "invalid message format");
  859. wpa_dbus_dict_entry_clear(&entry);
  860. return FALSE;
  861. err_no_mem_clear:
  862. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  863. wpa_dbus_dict_entry_clear(&entry);
  864. return FALSE;
  865. }
  866. dbus_bool_t wpas_dbus_getter_p2p_peers(DBusMessageIter *iter, DBusError *error,
  867. void *user_data)
  868. {
  869. struct wpa_supplicant *wpa_s = user_data;
  870. struct p2p_data *p2p = wpa_s->global->p2p;
  871. int next = 0, i = 0;
  872. int num = 0, out_of_mem = 0;
  873. const u8 *addr;
  874. const struct p2p_peer_info *peer_info = NULL;
  875. dbus_bool_t success = FALSE;
  876. struct dl_list peer_objpath_list;
  877. struct peer_objpath_node {
  878. struct dl_list list;
  879. char path[WPAS_DBUS_OBJECT_PATH_MAX];
  880. } *node, *tmp;
  881. char **peer_obj_paths = NULL;
  882. if (!wpa_dbus_p2p_check_enabled(wpa_s, NULL, NULL, error) ||
  883. !wpa_s->parent->parent->dbus_new_path)
  884. return FALSE;
  885. dl_list_init(&peer_objpath_list);
  886. /* Get the first peer info */
  887. peer_info = p2p_get_peer_found(p2p, NULL, next);
  888. /* Get next and accumulate them */
  889. next = 1;
  890. while (peer_info != NULL) {
  891. node = os_zalloc(sizeof(struct peer_objpath_node));
  892. if (!node) {
  893. out_of_mem = 1;
  894. goto error;
  895. }
  896. addr = peer_info->p2p_device_addr;
  897. os_snprintf(node->path, WPAS_DBUS_OBJECT_PATH_MAX,
  898. "%s/" WPAS_DBUS_NEW_P2P_PEERS_PART
  899. "/" COMPACT_MACSTR,
  900. wpa_s->parent->parent->dbus_new_path,
  901. MAC2STR(addr));
  902. dl_list_add_tail(&peer_objpath_list, &node->list);
  903. num++;
  904. peer_info = p2p_get_peer_found(p2p, addr, next);
  905. }
  906. /*
  907. * Now construct the peer object paths in a form suitable for
  908. * array_property_getter helper below.
  909. */
  910. peer_obj_paths = os_calloc(num, sizeof(char *));
  911. if (!peer_obj_paths) {
  912. out_of_mem = 1;
  913. goto error;
  914. }
  915. dl_list_for_each_safe(node, tmp, &peer_objpath_list,
  916. struct peer_objpath_node, list)
  917. peer_obj_paths[i++] = node->path;
  918. success = wpas_dbus_simple_array_property_getter(iter,
  919. DBUS_TYPE_OBJECT_PATH,
  920. peer_obj_paths, num,
  921. error);
  922. error:
  923. if (peer_obj_paths)
  924. os_free(peer_obj_paths);
  925. dl_list_for_each_safe(node, tmp, &peer_objpath_list,
  926. struct peer_objpath_node, list) {
  927. dl_list_del(&node->list);
  928. os_free(node);
  929. }
  930. if (out_of_mem)
  931. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  932. return success;
  933. }
  934. enum wpas_p2p_role {
  935. WPAS_P2P_ROLE_DEVICE,
  936. WPAS_P2P_ROLE_GO,
  937. WPAS_P2P_ROLE_CLIENT,
  938. };
  939. static enum wpas_p2p_role wpas_get_p2p_role(struct wpa_supplicant *wpa_s)
  940. {
  941. struct wpa_ssid *ssid = wpa_s->current_ssid;
  942. if (!ssid)
  943. return WPAS_P2P_ROLE_DEVICE;
  944. if (wpa_s->wpa_state != WPA_COMPLETED)
  945. return WPAS_P2P_ROLE_DEVICE;
  946. switch (ssid->mode) {
  947. case WPAS_MODE_P2P_GO:
  948. case WPAS_MODE_P2P_GROUP_FORMATION:
  949. return WPAS_P2P_ROLE_GO;
  950. case WPAS_MODE_INFRA:
  951. if (ssid->p2p_group)
  952. return WPAS_P2P_ROLE_CLIENT;
  953. return WPAS_P2P_ROLE_DEVICE;
  954. default:
  955. return WPAS_P2P_ROLE_DEVICE;
  956. }
  957. }
  958. dbus_bool_t wpas_dbus_getter_p2p_role(DBusMessageIter *iter, DBusError *error,
  959. void *user_data)
  960. {
  961. struct wpa_supplicant *wpa_s = user_data;
  962. char *str;
  963. switch (wpas_get_p2p_role(wpa_s)) {
  964. case WPAS_P2P_ROLE_GO:
  965. str = "GO";
  966. break;
  967. case WPAS_P2P_ROLE_CLIENT:
  968. str = "client";
  969. break;
  970. default:
  971. str = "device";
  972. break;
  973. }
  974. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING, &str,
  975. error);
  976. }
  977. dbus_bool_t wpas_dbus_getter_p2p_group(DBusMessageIter *iter, DBusError *error,
  978. void *user_data)
  979. {
  980. struct wpa_supplicant *wpa_s = user_data;
  981. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX];
  982. char *dbus_groupobj_path = path_buf;
  983. if (wpa_s->dbus_groupobj_path == NULL)
  984. os_snprintf(dbus_groupobj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  985. "/");
  986. else
  987. os_snprintf(dbus_groupobj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  988. "%s", wpa_s->dbus_groupobj_path);
  989. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_OBJECT_PATH,
  990. &dbus_groupobj_path, error);
  991. }
  992. dbus_bool_t wpas_dbus_getter_p2p_peergo(DBusMessageIter *iter,
  993. DBusError *error, void *user_data)
  994. {
  995. struct wpa_supplicant *wpa_s = user_data;
  996. char go_peer_obj_path[WPAS_DBUS_OBJECT_PATH_MAX], *path;
  997. if (!wpa_s->parent->parent->dbus_new_path)
  998. return FALSE;
  999. if (wpas_get_p2p_role(wpa_s) != WPAS_P2P_ROLE_CLIENT)
  1000. os_snprintf(go_peer_obj_path, WPAS_DBUS_OBJECT_PATH_MAX, "/");
  1001. else
  1002. os_snprintf(go_peer_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  1003. "%s/" WPAS_DBUS_NEW_P2P_PEERS_PART "/"
  1004. COMPACT_MACSTR,
  1005. wpa_s->parent->parent->dbus_new_path,
  1006. MAC2STR(wpa_s->go_dev_addr));
  1007. path = go_peer_obj_path;
  1008. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_OBJECT_PATH,
  1009. &path, error);
  1010. }
  1011. /*
  1012. * Peer object properties accessor methods
  1013. */
  1014. dbus_bool_t wpas_dbus_getter_p2p_peer_device_name(DBusMessageIter *iter,
  1015. DBusError *error,
  1016. void *user_data)
  1017. {
  1018. struct peer_handler_args *peer_args = user_data;
  1019. const struct p2p_peer_info *info;
  1020. char *tmp;
  1021. if (!wpa_dbus_p2p_check_enabled(peer_args->wpa_s, NULL, NULL, error))
  1022. return FALSE;
  1023. /* get the peer info */
  1024. info = p2p_get_peer_found(peer_args->wpa_s->global->p2p,
  1025. peer_args->p2p_device_addr, 0);
  1026. if (info == NULL) {
  1027. dbus_set_error(error, DBUS_ERROR_FAILED,
  1028. "failed to find peer");
  1029. return FALSE;
  1030. }
  1031. tmp = os_strdup(info->device_name);
  1032. if (!tmp) {
  1033. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  1034. return FALSE;
  1035. }
  1036. if (!wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING, &tmp,
  1037. error)) {
  1038. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  1039. os_free(tmp);
  1040. return FALSE;
  1041. }
  1042. os_free(tmp);
  1043. return TRUE;
  1044. }
  1045. dbus_bool_t wpas_dbus_getter_p2p_peer_manufacturer(DBusMessageIter *iter,
  1046. DBusError *error,
  1047. void *user_data)
  1048. {
  1049. struct peer_handler_args *peer_args = user_data;
  1050. const struct p2p_peer_info *info;
  1051. char *tmp;
  1052. if (!wpa_dbus_p2p_check_enabled(peer_args->wpa_s, NULL, NULL, error))
  1053. return FALSE;
  1054. /* get the peer 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, "failed to find peer");
  1059. return FALSE;
  1060. }
  1061. tmp = os_strdup(info->manufacturer);
  1062. if (!tmp) {
  1063. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  1064. return FALSE;
  1065. }
  1066. if (!wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING, &tmp,
  1067. error)) {
  1068. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  1069. os_free(tmp);
  1070. return FALSE;
  1071. }
  1072. os_free(tmp);
  1073. return TRUE;
  1074. }
  1075. dbus_bool_t wpas_dbus_getter_p2p_peer_primary_device_type(
  1076. DBusMessageIter *iter, DBusError *error, void *user_data)
  1077. {
  1078. struct peer_handler_args *peer_args = user_data;
  1079. const struct p2p_peer_info *info;
  1080. info = p2p_get_peer_found(peer_args->wpa_s->global->p2p,
  1081. peer_args->p2p_device_addr, 0);
  1082. if (info == NULL) {
  1083. dbus_set_error(error, DBUS_ERROR_FAILED,
  1084. "failed to find peer");
  1085. return FALSE;
  1086. }
  1087. if (!wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
  1088. (char *)
  1089. info->pri_dev_type,
  1090. WPS_DEV_TYPE_LEN, error)) {
  1091. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  1092. return FALSE;
  1093. }
  1094. return TRUE;
  1095. }
  1096. dbus_bool_t wpas_dbus_getter_p2p_peer_config_method(DBusMessageIter *iter,
  1097. DBusError *error,
  1098. void *user_data)
  1099. {
  1100. struct peer_handler_args *peer_args = user_data;
  1101. const struct p2p_peer_info *info;
  1102. info = p2p_get_peer_found(peer_args->wpa_s->global->p2p,
  1103. peer_args->p2p_device_addr, 0);
  1104. if (info == NULL) {
  1105. dbus_set_error(error, DBUS_ERROR_FAILED,
  1106. "failed to find peer");
  1107. return FALSE;
  1108. }
  1109. if (!wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT16,
  1110. &info->config_methods, error)) {
  1111. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  1112. return FALSE;
  1113. }
  1114. return TRUE;
  1115. }
  1116. dbus_bool_t wpas_dbus_getter_p2p_peer_level(DBusMessageIter *iter,
  1117. DBusError *error,
  1118. void *user_data)
  1119. {
  1120. struct peer_handler_args *peer_args = user_data;
  1121. const struct p2p_peer_info *info;
  1122. info = p2p_get_peer_found(peer_args->wpa_s->global->p2p,
  1123. peer_args->p2p_device_addr, 0);
  1124. if (info == NULL) {
  1125. dbus_set_error(error, DBUS_ERROR_FAILED,
  1126. "failed to find peer");
  1127. return FALSE;
  1128. }
  1129. if (!wpas_dbus_simple_property_getter(iter, DBUS_TYPE_INT32,
  1130. &info->level, error)) {
  1131. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  1132. return FALSE;
  1133. }
  1134. return TRUE;
  1135. }
  1136. dbus_bool_t wpas_dbus_getter_p2p_peer_device_capability(DBusMessageIter *iter,
  1137. DBusError *error,
  1138. void *user_data)
  1139. {
  1140. struct peer_handler_args *peer_args = user_data;
  1141. const struct p2p_peer_info *info;
  1142. info = p2p_get_peer_found(peer_args->wpa_s->global->p2p,
  1143. peer_args->p2p_device_addr, 0);
  1144. if (info == NULL) {
  1145. dbus_set_error(error, DBUS_ERROR_FAILED,
  1146. "failed to find peer");
  1147. return FALSE;
  1148. }
  1149. if (!wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BYTE,
  1150. &info->dev_capab, error)) {
  1151. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  1152. return FALSE;
  1153. }
  1154. return TRUE;
  1155. }
  1156. dbus_bool_t wpas_dbus_getter_p2p_peer_group_capability(DBusMessageIter *iter,
  1157. DBusError *error,
  1158. void *user_data)
  1159. {
  1160. struct peer_handler_args *peer_args = user_data;
  1161. const struct p2p_peer_info *info;
  1162. info = p2p_get_peer_found(peer_args->wpa_s->global->p2p,
  1163. peer_args->p2p_device_addr, 0);
  1164. if (info == NULL) {
  1165. dbus_set_error(error, DBUS_ERROR_FAILED,
  1166. "failed to find peer");
  1167. return FALSE;
  1168. }
  1169. if (!wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BYTE,
  1170. &info->group_capab, error)) {
  1171. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  1172. return FALSE;
  1173. }
  1174. return TRUE;
  1175. }
  1176. dbus_bool_t wpas_dbus_getter_p2p_peer_secondary_device_types(
  1177. DBusMessageIter *iter, DBusError *error, void *user_data)
  1178. {
  1179. struct peer_handler_args *peer_args = user_data;
  1180. const struct p2p_peer_info *info;
  1181. DBusMessageIter variant_iter, array_iter;
  1182. info = p2p_get_peer_found(peer_args->wpa_s->global->p2p,
  1183. peer_args->p2p_device_addr, 0);
  1184. if (info == NULL) {
  1185. dbus_set_error(error, DBUS_ERROR_FAILED, "failed to find peer");
  1186. return FALSE;
  1187. }
  1188. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  1189. DBUS_TYPE_ARRAY_AS_STRING
  1190. DBUS_TYPE_ARRAY_AS_STRING
  1191. DBUS_TYPE_BYTE_AS_STRING,
  1192. &variant_iter) ||
  1193. !dbus_message_iter_open_container(&variant_iter, DBUS_TYPE_ARRAY,
  1194. DBUS_TYPE_ARRAY_AS_STRING
  1195. DBUS_TYPE_BYTE_AS_STRING,
  1196. &array_iter)) {
  1197. dbus_set_error(error, DBUS_ERROR_FAILED,
  1198. "%s: failed to construct message 1", __func__);
  1199. return FALSE;
  1200. }
  1201. if (info->wps_sec_dev_type_list_len) {
  1202. const u8 *sec_dev_type_list = info->wps_sec_dev_type_list;
  1203. int num_sec_device_types =
  1204. info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN;
  1205. int i;
  1206. DBusMessageIter inner_array_iter;
  1207. for (i = 0; i < num_sec_device_types; i++) {
  1208. if (!dbus_message_iter_open_container(
  1209. &array_iter, DBUS_TYPE_ARRAY,
  1210. DBUS_TYPE_BYTE_AS_STRING,
  1211. &inner_array_iter) ||
  1212. !dbus_message_iter_append_fixed_array(
  1213. &inner_array_iter, DBUS_TYPE_BYTE,
  1214. &sec_dev_type_list, WPS_DEV_TYPE_LEN) ||
  1215. !dbus_message_iter_close_container(
  1216. &array_iter, &inner_array_iter)) {
  1217. dbus_set_error(error, DBUS_ERROR_FAILED,
  1218. "%s: failed to construct message 2 (%d)",
  1219. __func__, i);
  1220. return FALSE;
  1221. }
  1222. sec_dev_type_list += WPS_DEV_TYPE_LEN;
  1223. }
  1224. }
  1225. if (!dbus_message_iter_close_container(&variant_iter, &array_iter) ||
  1226. !dbus_message_iter_close_container(iter, &variant_iter)) {
  1227. dbus_set_error(error, DBUS_ERROR_FAILED,
  1228. "%s: failed to construct message 3", __func__);
  1229. return FALSE;
  1230. }
  1231. return TRUE;
  1232. }
  1233. dbus_bool_t wpas_dbus_getter_p2p_peer_vendor_extension(DBusMessageIter *iter,
  1234. DBusError *error,
  1235. void *user_data)
  1236. {
  1237. struct wpabuf *vendor_extension[P2P_MAX_WPS_VENDOR_EXT];
  1238. unsigned int i, num = 0;
  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. /* Add WPS vendor extensions attribute */
  1249. os_memset(vendor_extension, 0, sizeof(vendor_extension));
  1250. for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
  1251. if (info->wps_vendor_ext[i] == NULL)
  1252. continue;
  1253. vendor_extension[num] = info->wps_vendor_ext[i];
  1254. num++;
  1255. }
  1256. if (!wpas_dbus_simple_array_array_property_getter(iter, DBUS_TYPE_BYTE,
  1257. vendor_extension,
  1258. num, error))
  1259. return FALSE;
  1260. return TRUE;
  1261. }
  1262. dbus_bool_t wpas_dbus_getter_p2p_peer_ies(DBusMessageIter *iter,
  1263. DBusError *error, void *user_data)
  1264. {
  1265. struct peer_handler_args *peer_args = user_data;
  1266. const struct p2p_peer_info *info;
  1267. info = p2p_get_peer_found(peer_args->wpa_s->global->p2p,
  1268. peer_args->p2p_device_addr, 0);
  1269. if (info == NULL) {
  1270. dbus_set_error(error, DBUS_ERROR_FAILED,
  1271. "failed to find peer");
  1272. return FALSE;
  1273. }
  1274. if (info->wfd_subelems == NULL)
  1275. return wpas_dbus_simple_array_property_getter(iter,
  1276. DBUS_TYPE_BYTE,
  1277. NULL, 0, error);
  1278. return wpas_dbus_simple_array_property_getter(
  1279. iter, DBUS_TYPE_BYTE, (char *) info->wfd_subelems->buf,
  1280. info->wfd_subelems->used, error);
  1281. }
  1282. dbus_bool_t wpas_dbus_getter_p2p_peer_device_address(DBusMessageIter *iter,
  1283. DBusError *error,
  1284. void *user_data)
  1285. {
  1286. struct peer_handler_args *peer_args = user_data;
  1287. const struct p2p_peer_info *info;
  1288. info = p2p_get_peer_found(peer_args->wpa_s->global->p2p,
  1289. peer_args->p2p_device_addr, 0);
  1290. if (info == NULL) {
  1291. dbus_set_error(error, DBUS_ERROR_FAILED,
  1292. "failed to find peer");
  1293. return FALSE;
  1294. }
  1295. return wpas_dbus_simple_array_property_getter(
  1296. iter, DBUS_TYPE_BYTE, (char *) info->p2p_device_addr,
  1297. ETH_ALEN, error);
  1298. }
  1299. struct peer_group_data {
  1300. struct wpa_supplicant *wpa_s;
  1301. const struct p2p_peer_info *info;
  1302. char **paths;
  1303. unsigned int nb_paths;
  1304. int error;
  1305. };
  1306. static int match_group_where_peer_is_client(struct p2p_group *group,
  1307. void *user_data)
  1308. {
  1309. struct peer_group_data *data = user_data;
  1310. const struct p2p_group_config *cfg;
  1311. struct wpa_supplicant *wpa_s_go;
  1312. char **paths;
  1313. if (!p2p_group_is_client_connected(group, data->info->p2p_device_addr))
  1314. return 1;
  1315. cfg = p2p_group_get_config(group);
  1316. wpa_s_go = wpas_get_p2p_go_iface(data->wpa_s, cfg->ssid,
  1317. cfg->ssid_len);
  1318. if (wpa_s_go == NULL)
  1319. return 1;
  1320. paths = os_realloc_array(data->paths, data->nb_paths + 1,
  1321. sizeof(char *));
  1322. if (paths == NULL)
  1323. goto out_of_memory;
  1324. data->paths = paths;
  1325. data->paths[data->nb_paths] = wpa_s_go->dbus_groupobj_path;
  1326. data->nb_paths++;
  1327. return 1;
  1328. out_of_memory:
  1329. data->error = ENOMEM;
  1330. return 0;
  1331. }
  1332. dbus_bool_t wpas_dbus_getter_p2p_peer_groups(DBusMessageIter *iter,
  1333. DBusError *error,
  1334. void *user_data)
  1335. {
  1336. struct peer_handler_args *peer_args = user_data;
  1337. const struct p2p_peer_info *info;
  1338. struct peer_group_data data;
  1339. struct wpa_supplicant *wpa_s, *wpa_s_go;
  1340. dbus_bool_t success = FALSE;
  1341. info = p2p_get_peer_found(peer_args->wpa_s->global->p2p,
  1342. peer_args->p2p_device_addr, 0);
  1343. if (info == NULL) {
  1344. dbus_set_error(error, DBUS_ERROR_FAILED,
  1345. "failed to find peer");
  1346. return FALSE;
  1347. }
  1348. os_memset(&data, 0, sizeof(data));
  1349. wpa_s = peer_args->wpa_s;
  1350. wpa_s = wpa_s->global->p2p_init_wpa_s;
  1351. wpa_s_go = wpas_get_p2p_client_iface(wpa_s, info->p2p_device_addr);
  1352. if (wpa_s_go) {
  1353. data.paths = os_calloc(1, sizeof(char *));
  1354. if (data.paths == NULL)
  1355. goto out_of_memory;
  1356. data.paths[0] = wpa_s_go->dbus_groupobj_path;
  1357. data.nb_paths = 1;
  1358. }
  1359. data.wpa_s = peer_args->wpa_s;
  1360. data.info = info;
  1361. p2p_loop_on_all_groups(peer_args->wpa_s->global->p2p,
  1362. match_group_where_peer_is_client, &data);
  1363. if (data.error)
  1364. goto out_of_memory;
  1365. if (data.paths == NULL) {
  1366. return wpas_dbus_simple_array_property_getter(
  1367. iter, DBUS_TYPE_OBJECT_PATH, NULL, 0, error);
  1368. }
  1369. success = wpas_dbus_simple_array_property_getter(iter,
  1370. DBUS_TYPE_OBJECT_PATH,
  1371. data.paths,
  1372. data.nb_paths, error);
  1373. goto out;
  1374. out_of_memory:
  1375. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  1376. out:
  1377. os_free(data.paths);
  1378. return success;
  1379. }
  1380. /**
  1381. * wpas_dbus_getter_persistent_groups - Get array of persistent group objects
  1382. * @iter: Pointer to incoming dbus message iter
  1383. * @error: Location to store error on failure
  1384. * @user_data: Function specific data
  1385. * Returns: TRUE on success, FALSE on failure
  1386. *
  1387. * Getter for "PersistentGroups" property.
  1388. */
  1389. dbus_bool_t wpas_dbus_getter_persistent_groups(DBusMessageIter *iter,
  1390. DBusError *error,
  1391. void *user_data)
  1392. {
  1393. struct wpa_supplicant *wpa_s = user_data;
  1394. struct wpa_ssid *ssid;
  1395. char **paths;
  1396. unsigned int i = 0, num = 0;
  1397. dbus_bool_t success = FALSE;
  1398. wpa_s = wpa_s->global->p2p_init_wpa_s;
  1399. if (!wpa_s->parent->dbus_new_path)
  1400. return FALSE;
  1401. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  1402. if (network_is_persistent_group(ssid))
  1403. num++;
  1404. paths = os_calloc(num, sizeof(char *));
  1405. if (!paths) {
  1406. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  1407. return FALSE;
  1408. }
  1409. /* Loop through configured networks and append object path of each */
  1410. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1411. if (!network_is_persistent_group(ssid))
  1412. continue;
  1413. paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  1414. if (paths[i] == NULL) {
  1415. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
  1416. "no memory");
  1417. goto out;
  1418. }
  1419. /* Construct the object path for this network. */
  1420. os_snprintf(paths[i++], WPAS_DBUS_OBJECT_PATH_MAX,
  1421. "%s/" WPAS_DBUS_NEW_PERSISTENT_GROUPS_PART "/%d",
  1422. wpa_s->parent->dbus_new_path, ssid->id);
  1423. }
  1424. success = wpas_dbus_simple_array_property_getter(iter,
  1425. DBUS_TYPE_OBJECT_PATH,
  1426. paths, num, error);
  1427. out:
  1428. while (i)
  1429. os_free(paths[--i]);
  1430. os_free(paths);
  1431. return success;
  1432. }
  1433. /**
  1434. * wpas_dbus_getter_persistent_group_properties - Get options for a persistent
  1435. * group
  1436. * @iter: Pointer to incoming dbus message iter
  1437. * @error: Location to store error on failure
  1438. * @user_data: Function specific data
  1439. * Returns: TRUE on success, FALSE on failure
  1440. *
  1441. * Getter for "Properties" property of a persistent group.
  1442. */
  1443. dbus_bool_t wpas_dbus_getter_persistent_group_properties(DBusMessageIter *iter,
  1444. DBusError *error,
  1445. void *user_data)
  1446. {
  1447. struct network_handler_args *net = user_data;
  1448. /* Leveraging the fact that persistent group object is still
  1449. * represented in same manner as network within.
  1450. */
  1451. return wpas_dbus_getter_network_properties(iter, error, net);
  1452. }
  1453. /**
  1454. * wpas_dbus_setter_persistent_group_properties - Get options for a persistent
  1455. * group
  1456. * @iter: Pointer to incoming dbus message iter
  1457. * @error: Location to store error on failure
  1458. * @user_data: Function specific data
  1459. * Returns: TRUE on success, FALSE on failure
  1460. *
  1461. * Setter for "Properties" property of a persistent group.
  1462. */
  1463. dbus_bool_t wpas_dbus_setter_persistent_group_properties(DBusMessageIter *iter,
  1464. DBusError *error,
  1465. void *user_data)
  1466. {
  1467. struct network_handler_args *net = user_data;
  1468. struct wpa_ssid *ssid = net->ssid;
  1469. DBusMessageIter variant_iter;
  1470. /*
  1471. * Leveraging the fact that persistent group object is still
  1472. * represented in same manner as network within.
  1473. */
  1474. dbus_message_iter_recurse(iter, &variant_iter);
  1475. return set_network_properties(net->wpa_s, ssid, &variant_iter, error);
  1476. }
  1477. /**
  1478. * wpas_dbus_new_iface_add_persistent_group - Add a new configured
  1479. * persistent_group
  1480. * @message: Pointer to incoming dbus message
  1481. * @wpa_s: wpa_supplicant structure for a network interface
  1482. * Returns: A dbus message containing the object path of the new
  1483. * persistent group
  1484. *
  1485. * Handler function for "AddPersistentGroup" method call of a P2P Device
  1486. * interface.
  1487. */
  1488. DBusMessage * wpas_dbus_handler_add_persistent_group(
  1489. DBusMessage *message, struct wpa_supplicant *wpa_s)
  1490. {
  1491. DBusMessage *reply = NULL;
  1492. DBusMessageIter iter;
  1493. struct wpa_ssid *ssid = NULL;
  1494. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *path = path_buf;
  1495. DBusError error;
  1496. dbus_message_iter_init(message, &iter);
  1497. wpa_s = wpa_s->global->p2p_init_wpa_s;
  1498. if (wpa_s->parent->dbus_new_path)
  1499. ssid = wpa_config_add_network(wpa_s->conf);
  1500. if (ssid == NULL) {
  1501. wpa_printf(MSG_ERROR,
  1502. "dbus: %s: Cannot add new persistent group",
  1503. __func__);
  1504. reply = wpas_dbus_error_unknown_error(
  1505. message,
  1506. "wpa_supplicant could not add a persistent group on this interface.");
  1507. goto err;
  1508. }
  1509. /* Mark the ssid as being a persistent group before the notification */
  1510. ssid->disabled = 2;
  1511. ssid->p2p_persistent_group = 1;
  1512. wpas_notify_persistent_group_added(wpa_s, ssid);
  1513. wpa_config_set_network_defaults(ssid);
  1514. dbus_error_init(&error);
  1515. if (!set_network_properties(wpa_s, ssid, &iter, &error)) {
  1516. wpa_printf(MSG_DEBUG,
  1517. "dbus: %s: Control interface could not set persistent group properties",
  1518. __func__);
  1519. reply = wpas_dbus_reply_new_from_error(
  1520. message, &error, DBUS_ERROR_INVALID_ARGS,
  1521. "Failed to set network properties");
  1522. dbus_error_free(&error);
  1523. goto err;
  1524. }
  1525. /* Construct the object path for this network. */
  1526. os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
  1527. "%s/" WPAS_DBUS_NEW_PERSISTENT_GROUPS_PART "/%d",
  1528. wpa_s->parent->dbus_new_path, ssid->id);
  1529. reply = dbus_message_new_method_return(message);
  1530. if (reply == NULL) {
  1531. reply = wpas_dbus_error_no_memory(message);
  1532. goto err;
  1533. }
  1534. if (!dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
  1535. DBUS_TYPE_INVALID)) {
  1536. dbus_message_unref(reply);
  1537. reply = wpas_dbus_error_no_memory(message);
  1538. goto err;
  1539. }
  1540. return reply;
  1541. err:
  1542. if (ssid) {
  1543. wpas_notify_persistent_group_removed(wpa_s, ssid);
  1544. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1545. }
  1546. return reply;
  1547. }
  1548. /**
  1549. * wpas_dbus_handler_remove_persistent_group - Remove a configured persistent
  1550. * group
  1551. * @message: Pointer to incoming dbus message
  1552. * @wpa_s: wpa_supplicant structure for a network interface
  1553. * Returns: NULL on success or dbus error on failure
  1554. *
  1555. * Handler function for "RemovePersistentGroup" method call of a P2P Device
  1556. * interface.
  1557. */
  1558. DBusMessage * wpas_dbus_handler_remove_persistent_group(
  1559. DBusMessage *message, struct wpa_supplicant *wpa_s)
  1560. {
  1561. DBusMessage *reply = NULL;
  1562. const char *op;
  1563. char *iface = NULL, *persistent_group_id;
  1564. int id;
  1565. struct wpa_ssid *ssid;
  1566. dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &op,
  1567. DBUS_TYPE_INVALID);
  1568. wpa_s = wpa_s->global->p2p_init_wpa_s;
  1569. /*
  1570. * Extract the network ID and ensure the network is actually a child of
  1571. * this interface.
  1572. */
  1573. iface = wpas_dbus_new_decompose_object_path(
  1574. op, WPAS_DBUS_NEW_PERSISTENT_GROUPS_PART,
  1575. &persistent_group_id);
  1576. if (iface == NULL || persistent_group_id == NULL ||
  1577. !wpa_s->parent->dbus_new_path ||
  1578. os_strcmp(iface, wpa_s->parent->dbus_new_path) != 0) {
  1579. reply = wpas_dbus_error_invalid_args(message, op);
  1580. goto out;
  1581. }
  1582. id = strtoul(persistent_group_id, NULL, 10);
  1583. if (errno == EINVAL) {
  1584. reply = wpas_dbus_error_invalid_args(message, op);
  1585. goto out;
  1586. }
  1587. ssid = wpa_config_get_network(wpa_s->conf, id);
  1588. if (ssid == NULL) {
  1589. reply = wpas_dbus_error_persistent_group_unknown(message);
  1590. goto out;
  1591. }
  1592. wpas_notify_persistent_group_removed(wpa_s, ssid);
  1593. if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
  1594. wpa_printf(MSG_ERROR,
  1595. "dbus: %s: error occurred when removing persistent group %d",
  1596. __func__, id);
  1597. reply = wpas_dbus_error_unknown_error(
  1598. message,
  1599. "error removing the specified persistent group on this interface.");
  1600. goto out;
  1601. }
  1602. out:
  1603. os_free(iface);
  1604. return reply;
  1605. }
  1606. static void remove_persistent_group(struct wpa_supplicant *wpa_s,
  1607. struct wpa_ssid *ssid)
  1608. {
  1609. wpas_notify_persistent_group_removed(wpa_s, ssid);
  1610. if (wpa_config_remove_network(wpa_s->conf, ssid->id) < 0) {
  1611. wpa_printf(MSG_ERROR,
  1612. "dbus: %s: error occurred when removing persistent group %d",
  1613. __func__, ssid->id);
  1614. return;
  1615. }
  1616. }
  1617. /**
  1618. * wpas_dbus_handler_remove_all_persistent_groups - Remove all configured
  1619. * persistent groups
  1620. * @message: Pointer to incoming dbus message
  1621. * @wpa_s: wpa_supplicant structure for a network interface
  1622. * Returns: NULL on success or dbus error on failure
  1623. *
  1624. * Handler function for "RemoveAllPersistentGroups" method call of a
  1625. * P2P Device interface.
  1626. */
  1627. DBusMessage * wpas_dbus_handler_remove_all_persistent_groups(
  1628. DBusMessage *message, struct wpa_supplicant *wpa_s)
  1629. {
  1630. struct wpa_ssid *ssid, *next;
  1631. struct wpa_config *config;
  1632. wpa_s = wpa_s->global->p2p_init_wpa_s;
  1633. config = wpa_s->conf;
  1634. ssid = config->ssid;
  1635. while (ssid) {
  1636. next = ssid->next;
  1637. if (network_is_persistent_group(ssid))
  1638. remove_persistent_group(wpa_s, ssid);
  1639. ssid = next;
  1640. }
  1641. return NULL;
  1642. }
  1643. /*
  1644. * Group object properties accessor methods
  1645. */
  1646. dbus_bool_t wpas_dbus_getter_p2p_group_members(DBusMessageIter *iter,
  1647. DBusError *error,
  1648. void *user_data)
  1649. {
  1650. struct wpa_supplicant *wpa_s = user_data;
  1651. struct wpa_ssid *ssid;
  1652. unsigned int num_members;
  1653. char **paths;
  1654. unsigned int i;
  1655. void *next = NULL;
  1656. const u8 *addr;
  1657. dbus_bool_t success = FALSE;
  1658. if (!wpa_s->parent->parent->dbus_new_path)
  1659. return FALSE;
  1660. /* Verify correct role for this property */
  1661. if (wpas_get_p2p_role(wpa_s) != WPAS_P2P_ROLE_GO) {
  1662. return wpas_dbus_simple_array_property_getter(
  1663. iter, DBUS_TYPE_OBJECT_PATH, NULL, 0, error);
  1664. }
  1665. ssid = wpa_s->conf->ssid;
  1666. /* At present WPAS P2P_GO mode only applicable for p2p_go */
  1667. if (ssid->mode != WPAS_MODE_P2P_GO &&
  1668. ssid->mode != WPAS_MODE_AP &&
  1669. ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
  1670. return FALSE;
  1671. num_members = p2p_get_group_num_members(wpa_s->p2p_group);
  1672. paths = os_calloc(num_members, sizeof(char *));
  1673. if (!paths)
  1674. goto out_of_memory;
  1675. i = 0;
  1676. while ((addr = p2p_iterate_group_members(wpa_s->p2p_group, &next))) {
  1677. paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  1678. if (!paths[i])
  1679. goto out_of_memory;
  1680. os_snprintf(paths[i], WPAS_DBUS_OBJECT_PATH_MAX,
  1681. "%s/" WPAS_DBUS_NEW_P2P_PEERS_PART
  1682. "/" COMPACT_MACSTR,
  1683. wpa_s->parent->parent->dbus_new_path,
  1684. MAC2STR(addr));
  1685. i++;
  1686. }
  1687. success = wpas_dbus_simple_array_property_getter(iter,
  1688. DBUS_TYPE_OBJECT_PATH,
  1689. paths, num_members,
  1690. error);
  1691. for (i = 0; i < num_members; i++)
  1692. os_free(paths[i]);
  1693. os_free(paths);
  1694. return success;
  1695. out_of_memory:
  1696. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  1697. if (paths) {
  1698. for (i = 0; i < num_members; i++)
  1699. os_free(paths[i]);
  1700. os_free(paths);
  1701. }
  1702. return FALSE;
  1703. }
  1704. dbus_bool_t wpas_dbus_getter_p2p_group_ssid(DBusMessageIter *iter,
  1705. DBusError *error, void *user_data)
  1706. {
  1707. struct wpa_supplicant *wpa_s = user_data;
  1708. if (wpa_s->current_ssid == NULL)
  1709. return FALSE;
  1710. return wpas_dbus_simple_array_property_getter(
  1711. iter, DBUS_TYPE_BYTE, wpa_s->current_ssid->ssid,
  1712. wpa_s->current_ssid->ssid_len, error);
  1713. }
  1714. dbus_bool_t wpas_dbus_getter_p2p_group_bssid(DBusMessageIter *iter,
  1715. DBusError *error,
  1716. void *user_data)
  1717. {
  1718. struct wpa_supplicant *wpa_s = user_data;
  1719. u8 role = wpas_get_p2p_role(wpa_s);
  1720. u8 *p_bssid;
  1721. if (role == WPAS_P2P_ROLE_CLIENT) {
  1722. if (wpa_s->current_ssid == NULL)
  1723. return FALSE;
  1724. p_bssid = wpa_s->current_ssid->bssid;
  1725. } else {
  1726. if (wpa_s->ap_iface == NULL)
  1727. return FALSE;
  1728. p_bssid = wpa_s->ap_iface->bss[0]->own_addr;
  1729. }
  1730. return wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
  1731. p_bssid, ETH_ALEN,
  1732. error);
  1733. }
  1734. dbus_bool_t wpas_dbus_getter_p2p_group_frequency(DBusMessageIter *iter,
  1735. DBusError *error,
  1736. void *user_data)
  1737. {
  1738. struct wpa_supplicant *wpa_s = user_data;
  1739. u16 op_freq;
  1740. u8 role = wpas_get_p2p_role(wpa_s);
  1741. if (role == WPAS_P2P_ROLE_CLIENT) {
  1742. if (wpa_s->go_params == NULL)
  1743. return FALSE;
  1744. op_freq = wpa_s->go_params->freq;
  1745. } else {
  1746. if (wpa_s->ap_iface == NULL)
  1747. return FALSE;
  1748. op_freq = wpa_s->ap_iface->freq;
  1749. }
  1750. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT16,
  1751. &op_freq, error);
  1752. }
  1753. dbus_bool_t wpas_dbus_getter_p2p_group_passphrase(DBusMessageIter *iter,
  1754. DBusError *error,
  1755. void *user_data)
  1756. {
  1757. struct wpa_supplicant *wpa_s = user_data;
  1758. char *p_pass;
  1759. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1760. if (ssid == NULL)
  1761. return FALSE;
  1762. p_pass = ssid->passphrase;
  1763. if (!p_pass)
  1764. p_pass = "";
  1765. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  1766. &p_pass, error);
  1767. }
  1768. dbus_bool_t wpas_dbus_getter_p2p_group_psk(DBusMessageIter *iter,
  1769. DBusError *error, void *user_data)
  1770. {
  1771. struct wpa_supplicant *wpa_s = user_data;
  1772. u8 *p_psk = NULL;
  1773. u8 psk_len = 0;
  1774. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1775. if (ssid == NULL)
  1776. return FALSE;
  1777. if (ssid->psk_set) {
  1778. p_psk = ssid->psk;
  1779. psk_len = sizeof(ssid->psk);
  1780. }
  1781. return wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
  1782. p_psk, psk_len, error);
  1783. }
  1784. dbus_bool_t wpas_dbus_getter_p2p_group_vendor_ext(DBusMessageIter *iter,
  1785. DBusError *error,
  1786. void *user_data)
  1787. {
  1788. struct wpa_supplicant *wpa_s = user_data;
  1789. struct hostapd_data *hapd;
  1790. struct wpabuf *vendor_ext[MAX_WPS_VENDOR_EXTENSIONS];
  1791. unsigned int i, num_vendor_ext = 0;
  1792. os_memset(vendor_ext, 0, sizeof(vendor_ext));
  1793. /* Verify correct role for this property */
  1794. if (wpas_get_p2p_role(wpa_s) == WPAS_P2P_ROLE_GO) {
  1795. if (wpa_s->ap_iface == NULL)
  1796. return FALSE;
  1797. hapd = wpa_s->ap_iface->bss[0];
  1798. /* Parse WPS Vendor Extensions sent in Beacon/Probe Response */
  1799. for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
  1800. if (hapd->conf->wps_vendor_ext[i] == NULL)
  1801. continue;
  1802. vendor_ext[num_vendor_ext++] =
  1803. hapd->conf->wps_vendor_ext[i];
  1804. }
  1805. }
  1806. /* Return vendor extensions or no data */
  1807. return wpas_dbus_simple_array_array_property_getter(iter,
  1808. DBUS_TYPE_BYTE,
  1809. vendor_ext,
  1810. num_vendor_ext,
  1811. error);
  1812. }
  1813. dbus_bool_t wpas_dbus_setter_p2p_group_vendor_ext(DBusMessageIter *iter,
  1814. DBusError *error,
  1815. void *user_data)
  1816. {
  1817. struct wpa_supplicant *wpa_s = user_data;
  1818. DBusMessageIter variant_iter, iter_dict, array_iter, sub;
  1819. struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
  1820. unsigned int i;
  1821. struct hostapd_data *hapd = NULL;
  1822. if (wpas_get_p2p_role(wpa_s) == WPAS_P2P_ROLE_GO &&
  1823. wpa_s->ap_iface != NULL)
  1824. hapd = wpa_s->ap_iface->bss[0];
  1825. else
  1826. return FALSE;
  1827. dbus_message_iter_recurse(iter, &variant_iter);
  1828. if (dbus_message_iter_get_arg_type(&variant_iter) != DBUS_TYPE_ARRAY)
  1829. return FALSE;
  1830. /*
  1831. * This is supposed to be array of bytearrays (aay), but the earlier
  1832. * implementation used a dict with "WPSVendorExtensions" as the key in
  1833. * this setter function which does not match the format used by the
  1834. * getter function. For backwards compatibility, allow both formats to
  1835. * be used in the setter.
  1836. */
  1837. if (dbus_message_iter_get_element_type(&variant_iter) ==
  1838. DBUS_TYPE_ARRAY) {
  1839. /* This is the proper format matching the getter */
  1840. struct wpabuf *vals[MAX_WPS_VENDOR_EXTENSIONS];
  1841. dbus_message_iter_recurse(&variant_iter, &array_iter);
  1842. if (dbus_message_iter_get_arg_type(&array_iter) !=
  1843. DBUS_TYPE_ARRAY ||
  1844. dbus_message_iter_get_element_type(&array_iter) !=
  1845. DBUS_TYPE_BYTE) {
  1846. wpa_printf(MSG_DEBUG,
  1847. "dbus: Not an array of array of bytes");
  1848. return FALSE;
  1849. }
  1850. i = 0;
  1851. os_memset(vals, 0, sizeof(vals));
  1852. while (dbus_message_iter_get_arg_type(&array_iter) ==
  1853. DBUS_TYPE_ARRAY) {
  1854. char *val;
  1855. int len;
  1856. if (i == MAX_WPS_VENDOR_EXTENSIONS) {
  1857. wpa_printf(MSG_DEBUG,
  1858. "dbus: Too many WPSVendorExtensions values");
  1859. i = MAX_WPS_VENDOR_EXTENSIONS + 1;
  1860. break;
  1861. }
  1862. dbus_message_iter_recurse(&array_iter, &sub);
  1863. dbus_message_iter_get_fixed_array(&sub, &val, &len);
  1864. wpa_hexdump(MSG_DEBUG, "dbus: WPSVendorExtentions[]",
  1865. val, len);
  1866. vals[i] = wpabuf_alloc_copy(val, len);
  1867. if (vals[i] == NULL) {
  1868. i = MAX_WPS_VENDOR_EXTENSIONS + 1;
  1869. break;
  1870. }
  1871. i++;
  1872. dbus_message_iter_next(&array_iter);
  1873. }
  1874. if (i > MAX_WPS_VENDOR_EXTENSIONS) {
  1875. for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++)
  1876. wpabuf_free(vals[i]);
  1877. return FALSE;
  1878. }
  1879. for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
  1880. wpabuf_free(hapd->conf->wps_vendor_ext[i]);
  1881. hapd->conf->wps_vendor_ext[i] = vals[i];
  1882. }
  1883. hostapd_update_wps(hapd);
  1884. return TRUE;
  1885. }
  1886. if (dbus_message_iter_get_element_type(&variant_iter) !=
  1887. DBUS_TYPE_DICT_ENTRY)
  1888. return FALSE;
  1889. wpa_printf(MSG_DEBUG,
  1890. "dbus: Try to use backwards compatibility version of WPSVendorExtensions setter");
  1891. if (!wpa_dbus_dict_open_read(&variant_iter, &iter_dict, error))
  1892. return FALSE;
  1893. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  1894. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
  1895. dbus_set_error_const(error, DBUS_ERROR_INVALID_ARGS,
  1896. "invalid message format");
  1897. return FALSE;
  1898. }
  1899. if (os_strcmp(entry.key, "WPSVendorExtensions") == 0) {
  1900. if (entry.type != DBUS_TYPE_ARRAY ||
  1901. entry.array_type != WPAS_DBUS_TYPE_BINARRAY ||
  1902. entry.array_len > MAX_WPS_VENDOR_EXTENSIONS)
  1903. goto error;
  1904. for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
  1905. wpabuf_free(hapd->conf->wps_vendor_ext[i]);
  1906. if (i < entry.array_len) {
  1907. hapd->conf->wps_vendor_ext[i] =
  1908. entry.binarray_value[i];
  1909. entry.binarray_value[i] = NULL;
  1910. } else
  1911. hapd->conf->wps_vendor_ext[i] = NULL;
  1912. }
  1913. hostapd_update_wps(hapd);
  1914. } else
  1915. goto error;
  1916. wpa_dbus_dict_entry_clear(&entry);
  1917. }
  1918. return TRUE;
  1919. error:
  1920. wpa_dbus_dict_entry_clear(&entry);
  1921. dbus_set_error_const(error, DBUS_ERROR_INVALID_ARGS,
  1922. "invalid message format");
  1923. return FALSE;
  1924. }
  1925. DBusMessage * wpas_dbus_handler_p2p_add_service(DBusMessage *message,
  1926. struct wpa_supplicant *wpa_s)
  1927. {
  1928. DBusMessageIter iter_dict;
  1929. DBusMessage *reply = NULL;
  1930. DBusMessageIter iter;
  1931. struct wpa_dbus_dict_entry entry;
  1932. int upnp = 0;
  1933. int bonjour = 0;
  1934. char *service = NULL;
  1935. struct wpabuf *query = NULL;
  1936. struct wpabuf *resp = NULL;
  1937. u8 version = 0;
  1938. dbus_message_iter_init(message, &iter);
  1939. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  1940. goto error;
  1941. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  1942. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  1943. goto error;
  1944. if (os_strcmp(entry.key, "service_type") == 0 &&
  1945. entry.type == DBUS_TYPE_STRING) {
  1946. if (os_strcmp(entry.str_value, "upnp") == 0)
  1947. upnp = 1;
  1948. else if (os_strcmp(entry.str_value, "bonjour") == 0)
  1949. bonjour = 1;
  1950. else
  1951. goto error_clear;
  1952. } else if (os_strcmp(entry.key, "version") == 0 &&
  1953. entry.type == DBUS_TYPE_INT32) {
  1954. version = entry.uint32_value;
  1955. } else if (os_strcmp(entry.key, "service") == 0 &&
  1956. entry.type == DBUS_TYPE_STRING) {
  1957. os_free(service);
  1958. service = os_strdup(entry.str_value);
  1959. } else if (os_strcmp(entry.key, "query") == 0) {
  1960. if (entry.type != DBUS_TYPE_ARRAY ||
  1961. entry.array_type != DBUS_TYPE_BYTE)
  1962. goto error_clear;
  1963. query = wpabuf_alloc_copy(
  1964. entry.bytearray_value,
  1965. entry.array_len);
  1966. } else if (os_strcmp(entry.key, "response") == 0) {
  1967. if (entry.type != DBUS_TYPE_ARRAY ||
  1968. entry.array_type != DBUS_TYPE_BYTE)
  1969. goto error_clear;
  1970. resp = wpabuf_alloc_copy(entry.bytearray_value,
  1971. entry.array_len);
  1972. }
  1973. wpa_dbus_dict_entry_clear(&entry);
  1974. }
  1975. if (upnp == 1) {
  1976. if (version <= 0 || service == NULL)
  1977. goto error;
  1978. if (wpas_p2p_service_add_upnp(wpa_s, version, service) != 0)
  1979. goto error;
  1980. } else if (bonjour == 1) {
  1981. if (query == NULL || resp == NULL)
  1982. goto error;
  1983. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0)
  1984. goto error;
  1985. query = NULL;
  1986. resp = NULL;
  1987. } else
  1988. goto error;
  1989. os_free(service);
  1990. return reply;
  1991. error_clear:
  1992. wpa_dbus_dict_entry_clear(&entry);
  1993. error:
  1994. os_free(service);
  1995. wpabuf_free(query);
  1996. wpabuf_free(resp);
  1997. return wpas_dbus_error_invalid_args(message, NULL);
  1998. }
  1999. DBusMessage * wpas_dbus_handler_p2p_delete_service(
  2000. DBusMessage *message, struct wpa_supplicant *wpa_s)
  2001. {
  2002. DBusMessageIter iter_dict;
  2003. DBusMessage *reply = NULL;
  2004. DBusMessageIter iter;
  2005. struct wpa_dbus_dict_entry entry;
  2006. int upnp = 0;
  2007. int bonjour = 0;
  2008. int ret = 0;
  2009. char *service = NULL;
  2010. struct wpabuf *query = NULL;
  2011. u8 version = 0;
  2012. dbus_message_iter_init(message, &iter);
  2013. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  2014. goto error;
  2015. if (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  2016. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  2017. goto error;
  2018. if (os_strcmp(entry.key, "service_type") == 0 &&
  2019. entry.type == DBUS_TYPE_STRING) {
  2020. if (os_strcmp(entry.str_value, "upnp") == 0)
  2021. upnp = 1;
  2022. else if (os_strcmp(entry.str_value, "bonjour") == 0)
  2023. bonjour = 1;
  2024. else
  2025. goto error_clear;
  2026. wpa_dbus_dict_entry_clear(&entry);
  2027. }
  2028. }
  2029. if (upnp == 1) {
  2030. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  2031. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  2032. goto error;
  2033. if (os_strcmp(entry.key, "version") == 0 &&
  2034. entry.type == DBUS_TYPE_INT32)
  2035. version = entry.uint32_value;
  2036. else if (os_strcmp(entry.key, "service") == 0 &&
  2037. entry.type == DBUS_TYPE_STRING) {
  2038. os_free(service);
  2039. service = os_strdup(entry.str_value);
  2040. } else
  2041. goto error_clear;
  2042. wpa_dbus_dict_entry_clear(&entry);
  2043. }
  2044. if (version <= 0 || service == NULL)
  2045. goto error;
  2046. ret = wpas_p2p_service_del_upnp(wpa_s, version, service);
  2047. if (ret != 0)
  2048. goto error;
  2049. } else if (bonjour == 1) {
  2050. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  2051. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  2052. goto error;
  2053. if (os_strcmp(entry.key, "query") == 0) {
  2054. if (entry.type != DBUS_TYPE_ARRAY ||
  2055. entry.array_type != DBUS_TYPE_BYTE)
  2056. goto error_clear;
  2057. wpabuf_free(query);
  2058. query = wpabuf_alloc_copy(
  2059. entry.bytearray_value,
  2060. entry.array_len);
  2061. } else
  2062. goto error_clear;
  2063. wpa_dbus_dict_entry_clear(&entry);
  2064. }
  2065. if (query == NULL)
  2066. goto error;
  2067. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  2068. if (ret != 0)
  2069. goto error;
  2070. } else
  2071. goto error;
  2072. wpabuf_free(query);
  2073. os_free(service);
  2074. return reply;
  2075. error_clear:
  2076. wpa_dbus_dict_entry_clear(&entry);
  2077. error:
  2078. wpabuf_free(query);
  2079. os_free(service);
  2080. return wpas_dbus_error_invalid_args(message, NULL);
  2081. }
  2082. DBusMessage * wpas_dbus_handler_p2p_flush_service(DBusMessage *message,
  2083. struct wpa_supplicant *wpa_s)
  2084. {
  2085. wpas_p2p_service_flush(wpa_s);
  2086. return NULL;
  2087. }
  2088. DBusMessage * wpas_dbus_handler_p2p_service_sd_req(
  2089. DBusMessage *message, struct wpa_supplicant *wpa_s)
  2090. {
  2091. DBusMessageIter iter_dict;
  2092. DBusMessage *reply = NULL;
  2093. DBusMessageIter iter;
  2094. struct wpa_dbus_dict_entry entry;
  2095. int upnp = 0;
  2096. char *service = NULL;
  2097. char *peer_object_path = NULL;
  2098. struct wpabuf *tlv = NULL;
  2099. u8 version = 0;
  2100. u64 ref = 0;
  2101. u8 addr_buf[ETH_ALEN], *addr;
  2102. dbus_message_iter_init(message, &iter);
  2103. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  2104. goto error;
  2105. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  2106. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  2107. goto error;
  2108. if (os_strcmp(entry.key, "peer_object") == 0 &&
  2109. entry.type == DBUS_TYPE_OBJECT_PATH) {
  2110. peer_object_path = os_strdup(entry.str_value);
  2111. } else if (os_strcmp(entry.key, "service_type") == 0 &&
  2112. entry.type == DBUS_TYPE_STRING) {
  2113. if (os_strcmp(entry.str_value, "upnp") == 0)
  2114. upnp = 1;
  2115. else
  2116. goto error_clear;
  2117. } else if (os_strcmp(entry.key, "version") == 0 &&
  2118. entry.type == DBUS_TYPE_INT32) {
  2119. version = entry.uint32_value;
  2120. } else if (os_strcmp(entry.key, "service") == 0 &&
  2121. entry.type == DBUS_TYPE_STRING) {
  2122. service = os_strdup(entry.str_value);
  2123. } else if (os_strcmp(entry.key, "tlv") == 0) {
  2124. if (entry.type != DBUS_TYPE_ARRAY ||
  2125. entry.array_type != DBUS_TYPE_BYTE)
  2126. goto error_clear;
  2127. tlv = wpabuf_alloc_copy(entry.bytearray_value,
  2128. entry.array_len);
  2129. } else
  2130. goto error_clear;
  2131. wpa_dbus_dict_entry_clear(&entry);
  2132. }
  2133. if (!peer_object_path) {
  2134. addr = NULL;
  2135. } else {
  2136. if (parse_peer_object_path(peer_object_path, addr_buf) < 0 ||
  2137. !p2p_peer_known(wpa_s->global->p2p, addr_buf))
  2138. goto error;
  2139. addr = addr_buf;
  2140. }
  2141. if (upnp == 1) {
  2142. if (version <= 0 || service == NULL)
  2143. goto error;
  2144. ref = wpas_p2p_sd_request_upnp(wpa_s, addr, version, service);
  2145. } else {
  2146. if (tlv == NULL)
  2147. goto error;
  2148. ref = wpas_p2p_sd_request(wpa_s, addr, tlv);
  2149. wpabuf_free(tlv);
  2150. }
  2151. if (ref != 0) {
  2152. reply = dbus_message_new_method_return(message);
  2153. dbus_message_append_args(reply, DBUS_TYPE_UINT64,
  2154. &ref, DBUS_TYPE_INVALID);
  2155. } else {
  2156. reply = wpas_dbus_error_unknown_error(
  2157. message, "Unable to send SD request");
  2158. }
  2159. out:
  2160. os_free(service);
  2161. os_free(peer_object_path);
  2162. return reply;
  2163. error_clear:
  2164. wpa_dbus_dict_entry_clear(&entry);
  2165. error:
  2166. if (tlv)
  2167. wpabuf_free(tlv);
  2168. reply = wpas_dbus_error_invalid_args(message, NULL);
  2169. goto out;
  2170. }
  2171. DBusMessage * wpas_dbus_handler_p2p_service_sd_res(
  2172. DBusMessage *message, struct wpa_supplicant *wpa_s)
  2173. {
  2174. DBusMessageIter iter_dict;
  2175. DBusMessage *reply = NULL;
  2176. DBusMessageIter iter;
  2177. struct wpa_dbus_dict_entry entry;
  2178. char *peer_object_path = NULL;
  2179. struct wpabuf *tlv = NULL;
  2180. int freq = 0;
  2181. int dlg_tok = 0;
  2182. u8 addr[ETH_ALEN];
  2183. dbus_message_iter_init(message, &iter);
  2184. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  2185. goto error;
  2186. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  2187. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  2188. goto error;
  2189. if (os_strcmp(entry.key, "peer_object") == 0 &&
  2190. entry.type == DBUS_TYPE_OBJECT_PATH) {
  2191. peer_object_path = os_strdup(entry.str_value);
  2192. } else if (os_strcmp(entry.key, "frequency") == 0 &&
  2193. entry.type == DBUS_TYPE_INT32) {
  2194. freq = entry.uint32_value;
  2195. } else if (os_strcmp(entry.key, "dialog_token") == 0 &&
  2196. (entry.type == DBUS_TYPE_UINT32 ||
  2197. entry.type == DBUS_TYPE_INT32)) {
  2198. dlg_tok = entry.uint32_value;
  2199. } else if (os_strcmp(entry.key, "tlvs") == 0) {
  2200. if (entry.type != DBUS_TYPE_ARRAY ||
  2201. entry.array_type != DBUS_TYPE_BYTE)
  2202. goto error_clear;
  2203. tlv = wpabuf_alloc_copy(entry.bytearray_value,
  2204. entry.array_len);
  2205. } else
  2206. goto error_clear;
  2207. wpa_dbus_dict_entry_clear(&entry);
  2208. }
  2209. if (parse_peer_object_path(peer_object_path, addr) < 0 ||
  2210. !p2p_peer_known(wpa_s->global->p2p, addr) ||
  2211. tlv == NULL)
  2212. goto error;
  2213. wpas_p2p_sd_response(wpa_s, freq, addr, (u8) dlg_tok, tlv);
  2214. wpabuf_free(tlv);
  2215. out:
  2216. os_free(peer_object_path);
  2217. return reply;
  2218. error_clear:
  2219. wpa_dbus_dict_entry_clear(&entry);
  2220. error:
  2221. reply = wpas_dbus_error_invalid_args(message, NULL);
  2222. goto out;
  2223. }
  2224. DBusMessage * wpas_dbus_handler_p2p_service_sd_cancel_req(
  2225. DBusMessage *message, struct wpa_supplicant *wpa_s)
  2226. {
  2227. DBusMessageIter iter;
  2228. u64 req = 0;
  2229. dbus_message_iter_init(message, &iter);
  2230. dbus_message_iter_get_basic(&iter, &req);
  2231. if (req == 0)
  2232. goto error;
  2233. if (wpas_p2p_sd_cancel_request(wpa_s, req) < 0)
  2234. goto error;
  2235. return NULL;
  2236. error:
  2237. return wpas_dbus_error_invalid_args(message, NULL);
  2238. }
  2239. DBusMessage * wpas_dbus_handler_p2p_service_update(
  2240. DBusMessage *message, struct wpa_supplicant *wpa_s)
  2241. {
  2242. wpas_p2p_sd_service_update(wpa_s);
  2243. return NULL;
  2244. }
  2245. DBusMessage * wpas_dbus_handler_p2p_serv_disc_external(
  2246. DBusMessage *message, struct wpa_supplicant *wpa_s)
  2247. {
  2248. DBusMessageIter iter;
  2249. int ext = 0;
  2250. dbus_message_iter_init(message, &iter);
  2251. dbus_message_iter_get_basic(&iter, &ext);
  2252. wpa_s->p2p_sd_over_ctrl_iface = ext;
  2253. return NULL;
  2254. }
  2255. #ifdef CONFIG_WIFI_DISPLAY
  2256. dbus_bool_t wpas_dbus_getter_global_wfd_ies(DBusMessageIter *iter,
  2257. DBusError *error, void *user_data)
  2258. {
  2259. struct wpa_global *global = user_data;
  2260. struct wpabuf *ie;
  2261. dbus_bool_t ret;
  2262. ie = wifi_display_get_wfd_ie(global);
  2263. if (ie == NULL)
  2264. return wpas_dbus_simple_array_property_getter(iter,
  2265. DBUS_TYPE_BYTE,
  2266. NULL, 0, error);
  2267. ret = wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
  2268. wpabuf_head(ie),
  2269. wpabuf_len(ie), error);
  2270. wpabuf_free(ie);
  2271. return ret;
  2272. }
  2273. dbus_bool_t wpas_dbus_setter_global_wfd_ies(DBusMessageIter *iter,
  2274. DBusError *error, void *user_data)
  2275. {
  2276. struct wpa_global *global = user_data;
  2277. DBusMessageIter variant, array;
  2278. struct wpabuf *ie = NULL;
  2279. const u8 *data;
  2280. int len;
  2281. if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_VARIANT)
  2282. goto err;
  2283. dbus_message_iter_recurse(iter, &variant);
  2284. if (dbus_message_iter_get_arg_type(&variant) != DBUS_TYPE_ARRAY)
  2285. goto err;
  2286. dbus_message_iter_recurse(&variant, &array);
  2287. dbus_message_iter_get_fixed_array(&array, &data, &len);
  2288. if (len == 0) {
  2289. wifi_display_enable(global, 0);
  2290. wifi_display_deinit(global);
  2291. return TRUE;
  2292. }
  2293. ie = wpabuf_alloc(len);
  2294. if (ie == NULL)
  2295. goto err;
  2296. wpabuf_put_data(ie, data, len);
  2297. if (wifi_display_subelem_set_from_ies(global, ie) != 0)
  2298. goto err;
  2299. if (global->wifi_display == 0)
  2300. wifi_display_enable(global, 1);
  2301. wpabuf_free(ie);
  2302. return TRUE;
  2303. err:
  2304. wpabuf_free(ie);
  2305. dbus_set_error_const(error, DBUS_ERROR_INVALID_ARGS,
  2306. "invalid message format");
  2307. return FALSE;
  2308. }
  2309. #endif /* CONFIG_WIFI_DISPLAY */