dbus_new_handlers_p2p.c 75 KB

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