dbus_new_handlers_p2p.c 80 KB

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