dbus_new_handlers_p2p.c 71 KB

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