dbus_new_handlers_p2p.c 71 KB

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