dbus_new.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744
  1. /*
  2. * WPA Supplicant / dbus-based control interface
  3. * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
  4. * Copyright (c) 2009, Witold Sowa <witold.sowa@gmail.com>
  5. * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Alternatively, this software may be distributed under the terms of BSD
  12. * license.
  13. *
  14. * See README and COPYING for more details.
  15. */
  16. #include "includes.h"
  17. #include "common.h"
  18. #include "wps/wps.h"
  19. #include "../config.h"
  20. #include "../wpa_supplicant_i.h"
  21. #include "../bss.h"
  22. #include "dbus_new_helpers.h"
  23. #include "dbus_dict_helpers.h"
  24. #include "dbus_new.h"
  25. #include "dbus_new_handlers.h"
  26. #include "dbus_common.h"
  27. #include "dbus_common_i.h"
  28. /**
  29. * wpas_dbus_signal_interface - Send a interface related event signal
  30. * @wpa_s: %wpa_supplicant network interface data
  31. * @sig_name: signal name - InterfaceAdded or InterfaceRemoved
  32. * @properties: determines if add second argument with object properties
  33. *
  34. * Notify listeners about event related with interface
  35. */
  36. static void wpas_dbus_signal_interface(struct wpa_supplicant *wpa_s,
  37. const char *sig_name, int properties)
  38. {
  39. struct wpas_dbus_priv *iface;
  40. DBusMessage *_signal;
  41. DBusMessageIter iter, iter_dict;
  42. const char *path;
  43. iface = wpa_s->global->dbus;
  44. /* Do nothing if the control interface is not turned on */
  45. if (iface == NULL)
  46. return;
  47. _signal = dbus_message_new_signal(WPAS_DBUS_NEW_PATH,
  48. WPAS_DBUS_NEW_INTERFACE, sig_name);
  49. if (_signal == NULL) {
  50. wpa_printf(MSG_ERROR, "wpas_dbus_signal_interface[dbus]: "
  51. "enough memory to send scan results signal.");
  52. return;
  53. }
  54. dbus_message_iter_init_append(_signal, &iter);
  55. path = wpa_s->dbus_new_path;
  56. if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
  57. &path))
  58. goto err;
  59. if (properties) {
  60. if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
  61. goto err;
  62. wpa_dbus_get_object_properties(iface, path,
  63. WPAS_DBUS_NEW_IFACE_INTERFACE,
  64. &iter_dict);
  65. if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
  66. goto err;
  67. }
  68. dbus_connection_send(iface->con, _signal, NULL);
  69. dbus_message_unref(_signal);
  70. return;
  71. err:
  72. wpa_printf(MSG_ERROR, "wpas_dbus_signal_interface[dbus]: "
  73. "not enough memory to construct signal.");
  74. dbus_message_unref(_signal);
  75. }
  76. /**
  77. * wpas_dbus_signal_interface_added - Send a interface created signal
  78. * @wpa_s: %wpa_supplicant network interface data
  79. *
  80. * Notify listeners about creating new interface
  81. */
  82. static void wpas_dbus_signal_interface_added(struct wpa_supplicant *wpa_s)
  83. {
  84. wpas_dbus_signal_interface(wpa_s, "InterfaceAdded", TRUE);
  85. }
  86. /**
  87. * wpas_dbus_signal_interface_removed - Send a interface removed signal
  88. * @wpa_s: %wpa_supplicant network interface data
  89. *
  90. * Notify listeners about removing interface
  91. */
  92. static void wpas_dbus_signal_interface_removed(struct wpa_supplicant *wpa_s)
  93. {
  94. wpas_dbus_signal_interface(wpa_s, "InterfaceRemoved", FALSE);
  95. }
  96. /**
  97. * wpas_dbus_signal_scan_done - send scan done signal
  98. * @wpa_s: %wpa_supplicant network interface data
  99. * @success: indicates if scanning succeed or failed
  100. *
  101. * Notify listeners about finishing a scan
  102. */
  103. void wpas_dbus_signal_scan_done(struct wpa_supplicant *wpa_s, int success)
  104. {
  105. struct wpas_dbus_priv *iface;
  106. DBusMessage *_signal;
  107. dbus_bool_t succ;
  108. iface = wpa_s->global->dbus;
  109. /* Do nothing if the control interface is not turned on */
  110. if (iface == NULL)
  111. return;
  112. _signal = dbus_message_new_signal(wpa_s->dbus_new_path,
  113. WPAS_DBUS_NEW_IFACE_INTERFACE,
  114. "ScanDone");
  115. if (_signal == NULL) {
  116. wpa_printf(MSG_ERROR, "wpas_dbus_signal_scan_done[dbus]: "
  117. "enough memory to send signal.");
  118. return;
  119. }
  120. succ = success ? TRUE : FALSE;
  121. if (dbus_message_append_args(_signal, DBUS_TYPE_BOOLEAN, &succ,
  122. DBUS_TYPE_INVALID)) {
  123. dbus_connection_send(iface->con, _signal, NULL);
  124. } else {
  125. wpa_printf(MSG_ERROR, "wpas_dbus_signal_scan_done[dbus]: "
  126. "not enough memory to construct signal.");
  127. }
  128. dbus_message_unref(_signal);
  129. }
  130. /**
  131. * wpas_dbus_signal_blob - Send a BSS related event signal
  132. * @wpa_s: %wpa_supplicant network interface data
  133. * @bss_obj_path: BSS object path
  134. * @sig_name: signal name - BSSAdded or BSSRemoved
  135. * @properties: determines if add second argument with object properties
  136. *
  137. * Notify listeners about event related with BSS
  138. */
  139. static void wpas_dbus_signal_bss(struct wpa_supplicant *wpa_s,
  140. const char *bss_obj_path,
  141. const char *sig_name, int properties)
  142. {
  143. struct wpas_dbus_priv *iface;
  144. DBusMessage *_signal;
  145. DBusMessageIter iter, iter_dict;
  146. iface = wpa_s->global->dbus;
  147. /* Do nothing if the control interface is not turned on */
  148. if (iface == NULL)
  149. return;
  150. _signal = dbus_message_new_signal(wpa_s->dbus_new_path,
  151. WPAS_DBUS_NEW_IFACE_INTERFACE,
  152. sig_name);
  153. if (_signal == NULL) {
  154. wpa_printf(MSG_ERROR, "wpas_dbus_signal_bss[dbus]: "
  155. "enough memory to send signal.");
  156. return;
  157. }
  158. dbus_message_iter_init_append(_signal, &iter);
  159. if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
  160. &bss_obj_path))
  161. goto err;
  162. if (properties) {
  163. if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
  164. goto err;
  165. wpa_dbus_get_object_properties(iface, bss_obj_path,
  166. WPAS_DBUS_NEW_IFACE_BSSID,
  167. &iter_dict);
  168. if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
  169. goto err;
  170. }
  171. dbus_connection_send(iface->con, _signal, NULL);
  172. dbus_message_unref(_signal);
  173. return;
  174. err:
  175. wpa_printf(MSG_ERROR, "wpas_dbus_signal_bss[dbus]: "
  176. "not enough memory to construct signal.");
  177. dbus_message_unref(_signal);
  178. }
  179. /**
  180. * wpas_dbus_signal_bss_added - Send a BSS added signal
  181. * @wpa_s: %wpa_supplicant network interface data
  182. * @bss_obj_path: new BSS object path
  183. *
  184. * Notify listeners about adding new BSS
  185. */
  186. static void wpas_dbus_signal_bss_added(struct wpa_supplicant *wpa_s,
  187. const char *bss_obj_path)
  188. {
  189. wpas_dbus_signal_bss(wpa_s, bss_obj_path, "BSSAdded", TRUE);
  190. }
  191. /**
  192. * wpas_dbus_signal_bss_removed - Send a BSS removed signal
  193. * @wpa_s: %wpa_supplicant network interface data
  194. * @bss_obj_path: BSS object path
  195. *
  196. * Notify listeners about removing BSS
  197. */
  198. static void wpas_dbus_signal_bss_removed(struct wpa_supplicant *wpa_s,
  199. const char *bss_obj_path)
  200. {
  201. wpas_dbus_signal_bss(wpa_s, bss_obj_path, "BSSRemoved", FALSE);
  202. }
  203. /**
  204. * wpas_dbus_signal_blob - Send a blob related event signal
  205. * @wpa_s: %wpa_supplicant network interface data
  206. * @name: blob name
  207. * @sig_name: signal name - BlobAdded or BlobRemoved
  208. *
  209. * Notify listeners about event related with blob
  210. */
  211. static void wpas_dbus_signal_blob(struct wpa_supplicant *wpa_s,
  212. const char *name, const char *sig_name)
  213. {
  214. struct wpas_dbus_priv *iface;
  215. DBusMessage *_signal;
  216. iface = wpa_s->global->dbus;
  217. /* Do nothing if the control interface is not turned on */
  218. if (iface == NULL)
  219. return;
  220. _signal = dbus_message_new_signal(wpa_s->dbus_new_path,
  221. WPAS_DBUS_NEW_IFACE_INTERFACE,
  222. sig_name);
  223. if (_signal == NULL) {
  224. wpa_printf(MSG_ERROR, "wpas_dbus_signal_blob[dbus]: "
  225. "enough memory to send signal.");
  226. return;
  227. }
  228. if (dbus_message_append_args(_signal, DBUS_TYPE_STRING, &name,
  229. DBUS_TYPE_INVALID)) {
  230. dbus_connection_send(iface->con, _signal, NULL);
  231. } else {
  232. wpa_printf(MSG_ERROR, "wpas_dbus_signal_blob[dbus]: "
  233. "not enough memory to construct signal.");
  234. }
  235. dbus_message_unref(_signal);
  236. }
  237. /**
  238. * wpas_dbus_signal_blob_added - Send a blob added signal
  239. * @wpa_s: %wpa_supplicant network interface data
  240. * @name: blob name
  241. *
  242. * Notify listeners about adding a new blob
  243. */
  244. void wpas_dbus_signal_blob_added(struct wpa_supplicant *wpa_s,
  245. const char *name)
  246. {
  247. wpas_dbus_signal_blob(wpa_s, name, "BlobAdded");
  248. }
  249. /**
  250. * wpas_dbus_signal_blob_removed - Send a blob removed signal
  251. * @wpa_s: %wpa_supplicant network interface data
  252. * @name: blob name
  253. *
  254. * Notify listeners about removing blob
  255. */
  256. void wpas_dbus_signal_blob_removed(struct wpa_supplicant *wpa_s,
  257. const char *name)
  258. {
  259. wpas_dbus_signal_blob(wpa_s, name, "BlobRemoved");
  260. }
  261. /**
  262. * wpas_dbus_signal_network - Send a network related event signal
  263. * @wpa_s: %wpa_supplicant network interface data
  264. * @id: new network id
  265. * @sig_name: signal name - NetworkAdded, NetworkRemoved or NetworkSelected
  266. * @properties: determines if add second argument with object properties
  267. *
  268. * Notify listeners about event related with configured network
  269. */
  270. static void wpas_dbus_signal_network(struct wpa_supplicant *wpa_s,
  271. int id, const char *sig_name,
  272. int properties)
  273. {
  274. struct wpas_dbus_priv *iface;
  275. DBusMessage *_signal;
  276. DBusMessageIter iter, iter_dict;
  277. char *net_obj_path;
  278. iface = wpa_s->global->dbus;
  279. /* Do nothing if the control interface is not turned on */
  280. if (iface == NULL)
  281. return;
  282. net_obj_path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  283. if (net_obj_path == NULL)
  284. return;
  285. os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  286. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%u",
  287. wpa_s->dbus_new_path, id);
  288. _signal = dbus_message_new_signal(wpa_s->dbus_new_path,
  289. WPAS_DBUS_NEW_IFACE_INTERFACE,
  290. sig_name);
  291. if (_signal == NULL) {
  292. wpa_printf(MSG_ERROR, "wpas_dbus_signal_network[dbus]: "
  293. "enough memory to send signal.");
  294. os_free(net_obj_path);
  295. return;
  296. }
  297. dbus_message_iter_init_append(_signal, &iter);
  298. if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
  299. &net_obj_path))
  300. goto err;
  301. if (properties) {
  302. if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
  303. goto err;
  304. wpa_dbus_get_object_properties(iface, net_obj_path,
  305. WPAS_DBUS_NEW_IFACE_NETWORK,
  306. &iter_dict);
  307. if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
  308. goto err;
  309. }
  310. dbus_connection_send(iface->con, _signal, NULL);
  311. os_free(net_obj_path);
  312. dbus_message_unref(_signal);
  313. return;
  314. err:
  315. wpa_printf(MSG_ERROR, "wpas_dbus_signal_network[dbus]: "
  316. "not enough memory to construct signal.");
  317. os_free(net_obj_path);
  318. dbus_message_unref(_signal);
  319. }
  320. /**
  321. * wpas_dbus_signal_network_added - Send a network added signal
  322. * @wpa_s: %wpa_supplicant network interface data
  323. * @id: new network id
  324. *
  325. * Notify listeners about adding new network
  326. */
  327. static void wpas_dbus_signal_network_added(struct wpa_supplicant *wpa_s,
  328. int id)
  329. {
  330. wpas_dbus_signal_network(wpa_s, id, "NetworkAdded", TRUE);
  331. }
  332. /**
  333. * wpas_dbus_signal_network_removed - Send a network removed signal
  334. * @wpa_s: %wpa_supplicant network interface data
  335. * @id: network id
  336. *
  337. * Notify listeners about removing a network
  338. */
  339. static void wpas_dbus_signal_network_removed(struct wpa_supplicant *wpa_s,
  340. int id)
  341. {
  342. wpas_dbus_signal_network(wpa_s, id, "NetworkRemoved", FALSE);
  343. }
  344. /**
  345. * wpas_dbus_signal_network_selected - Send a network selected signal
  346. * @wpa_s: %wpa_supplicant network interface data
  347. * @id: network id
  348. *
  349. * Notify listeners about selecting a network
  350. */
  351. void wpas_dbus_signal_network_selected(struct wpa_supplicant *wpa_s, int id)
  352. {
  353. wpas_dbus_signal_network(wpa_s, id, "NetworkSelected", FALSE);
  354. }
  355. /**
  356. * wpas_dbus_signal_state_changed - Send a state changed signal
  357. * @wpa_s: %wpa_supplicant network interface data
  358. * @new_state: new state wpa_supplicant is entering
  359. * @old_state: old state wpa_supplicant is leaving
  360. *
  361. * Notify listeners that wpa_supplicant has changed state
  362. */
  363. void wpas_dbus_signal_state_changed(struct wpa_supplicant *wpa_s,
  364. enum wpa_states new_state,
  365. enum wpa_states old_state)
  366. {
  367. struct wpas_dbus_priv *iface;
  368. DBusMessage *_signal = NULL;
  369. char *new_state_str, *old_state_str;
  370. char *tmp;
  371. /* Do nothing if the control interface is not turned on */
  372. if (wpa_s->global == NULL)
  373. return;
  374. iface = wpa_s->global->dbus;
  375. if (iface == NULL)
  376. return;
  377. /* Only send signal if state really changed */
  378. if (new_state == old_state)
  379. return;
  380. _signal = dbus_message_new_signal(wpa_s->dbus_new_path,
  381. WPAS_DBUS_NEW_IFACE_INTERFACE,
  382. "StateChanged");
  383. if (_signal == NULL) {
  384. perror("wpas_dbus_signal_state_changed[dbus]: "
  385. "couldn't create dbus signal; likely out of memory");
  386. wpa_printf(MSG_ERROR,
  387. "wpas_dbus_signal_state_changed[dbus]: "
  388. "couldn't create dbus signal; likely out of "
  389. "memory.");
  390. return;
  391. }
  392. new_state_str = os_strdup(wpa_supplicant_state_txt(new_state));
  393. old_state_str = os_strdup(wpa_supplicant_state_txt(old_state));
  394. if (new_state_str == NULL || old_state_str == NULL) {
  395. perror("wpas_dbus_signal_state_changed[dbus]: "
  396. "couldn't convert state strings");
  397. wpa_printf(MSG_ERROR,
  398. "wpas_dbus_signal_state_changed[dbus]: "
  399. "couldn't convert state strings.");
  400. goto out;
  401. }
  402. /* make state string lowercase to fit new DBus API convention */
  403. tmp = new_state_str;
  404. while (*tmp) {
  405. *tmp = tolower(*tmp);
  406. tmp++;
  407. }
  408. tmp = old_state_str;
  409. while (*tmp) {
  410. *tmp = tolower(*tmp);
  411. tmp++;
  412. }
  413. if (!dbus_message_append_args(_signal,
  414. DBUS_TYPE_STRING, &new_state_str,
  415. DBUS_TYPE_STRING, &old_state_str,
  416. DBUS_TYPE_INVALID)) {
  417. perror("wpas_dbus_signal_state_changed[dbus]: "
  418. "not enough memory to construct state change signal.");
  419. wpa_printf(MSG_ERROR,
  420. "wpas_dbus_signal_state_changed[dbus]: "
  421. "not enough memory to construct state change "
  422. "signal.");
  423. goto out;
  424. }
  425. dbus_connection_send(iface->con, _signal, NULL);
  426. out:
  427. dbus_message_unref(_signal);
  428. os_free(new_state_str);
  429. os_free(old_state_str);
  430. }
  431. /**
  432. * wpas_dbus_signal_network_enabled_changed - Signals Enabled property changes
  433. * @wpa_s: %wpa_supplicant network interface data
  434. * @ssid: configured network which Enabled property has changed
  435. *
  436. * Sends PropertyChanged signals containing new value of Enabled property
  437. * for specified network
  438. */
  439. void wpas_dbus_signal_network_enabled_changed(struct wpa_supplicant *wpa_s,
  440. struct wpa_ssid *ssid)
  441. {
  442. struct network_handler_args args = {wpa_s, ssid};
  443. char path[WPAS_DBUS_OBJECT_PATH_MAX];
  444. os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
  445. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%d",
  446. wpa_s->dbus_new_path, ssid->id);
  447. wpa_dbus_signal_property_changed(wpa_s->global->dbus,
  448. (WPADBusPropertyAccessor)
  449. wpas_dbus_getter_enabled, &args,
  450. path, WPAS_DBUS_NEW_IFACE_NETWORK,
  451. "Enabled");
  452. }
  453. #ifdef CONFIG_WPS
  454. /**
  455. * wpas_dbus_signal_wps_event_success - Signals Success WPS event
  456. * @wpa_s: %wpa_supplicant network interface data
  457. *
  458. * Sends Event dbus signal with name "success" and empty dict as arguments
  459. */
  460. void wpas_dbus_signal_wps_event_success(struct wpa_supplicant *wpa_s)
  461. {
  462. DBusMessage *_signal = NULL;
  463. DBusMessageIter iter, dict_iter;
  464. struct wpas_dbus_priv *iface;
  465. char *key = "success";
  466. iface = wpa_s->global->dbus;
  467. /* Do nothing if the control interface is not turned on */
  468. if (iface == NULL)
  469. return;
  470. _signal = dbus_message_new_signal(wpa_s->dbus_new_path,
  471. WPAS_DBUS_NEW_IFACE_WPS, "Event");
  472. if (!_signal) {
  473. wpa_printf(MSG_ERROR, "wpas_dbus_signal_wps_event_success"
  474. "[dbus]: out of memory when creating a signal");
  475. return;
  476. }
  477. dbus_message_iter_init_append(_signal, &iter);
  478. if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key) ||
  479. !wpa_dbus_dict_open_write(&iter, &dict_iter) ||
  480. !wpa_dbus_dict_close_write(&iter, &dict_iter)) {
  481. wpa_printf(MSG_ERROR, "wpas_dbus_signal_wps_event_success"
  482. "[dbus]: out of memory");
  483. goto out;
  484. }
  485. dbus_connection_send(iface->con, _signal, NULL);
  486. out:
  487. dbus_message_unref(_signal);
  488. }
  489. /**
  490. * wpas_dbus_signal_wps_event_fail - Signals Fail WPS event
  491. * @wpa_s: %wpa_supplicant network interface data
  492. *
  493. * Sends Event dbus signal with name "fail" and dictionary containing
  494. * "msg field with fail message number (int32) as arguments
  495. */
  496. void wpas_dbus_signal_wps_event_fail(struct wpa_supplicant *wpa_s,
  497. struct wps_event_fail *fail)
  498. {
  499. DBusMessage *_signal = NULL;
  500. DBusMessageIter iter, dict_iter;
  501. struct wpas_dbus_priv *iface;
  502. char *key = "fail";
  503. iface = wpa_s->global->dbus;
  504. /* Do nothing if the control interface is not turned on */
  505. if (iface == NULL)
  506. return;
  507. _signal = dbus_message_new_signal(wpa_s->dbus_new_path,
  508. WPAS_DBUS_NEW_IFACE_WPS, "Event");
  509. if (!_signal) {
  510. wpa_printf(MSG_ERROR, "wpas_dbus_signal_wps_event_fail[dbus]: "
  511. "out of memory when creating a signal");
  512. return;
  513. }
  514. dbus_message_iter_init_append(_signal, &iter);
  515. if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key) ||
  516. !wpa_dbus_dict_open_write(&iter, &dict_iter) ||
  517. !wpa_dbus_dict_append_int32(&dict_iter, "msg", fail->msg) ||
  518. !wpa_dbus_dict_close_write(&iter, &dict_iter)) {
  519. wpa_printf(MSG_ERROR, "wpas_dbus_signal_wps_event_fail[dbus]: "
  520. "out of memory");
  521. goto out;
  522. }
  523. dbus_connection_send(iface->con, _signal, NULL);
  524. out:
  525. dbus_message_unref(_signal);
  526. }
  527. /**
  528. * wpas_dbus_signal_wps_event_m2d - Signals M2D WPS event
  529. * @wpa_s: %wpa_supplicant network interface data
  530. *
  531. * Sends Event dbus signal with name "m2d" and dictionary containing
  532. * fields of wps_event_m2d structure.
  533. */
  534. void wpas_dbus_signal_wps_event_m2d(struct wpa_supplicant *wpa_s,
  535. struct wps_event_m2d *m2d)
  536. {
  537. DBusMessage *_signal = NULL;
  538. DBusMessageIter iter, dict_iter;
  539. struct wpas_dbus_priv *iface;
  540. char *key = "m2d";
  541. iface = wpa_s->global->dbus;
  542. /* Do nothing if the control interface is not turned on */
  543. if (iface == NULL)
  544. return;
  545. _signal = dbus_message_new_signal(wpa_s->dbus_new_path,
  546. WPAS_DBUS_NEW_IFACE_WPS, "Event");
  547. if (!_signal) {
  548. wpa_printf(MSG_ERROR, "wpas_dbus_signal_wps_event_m2d[dbus]: "
  549. "out of memory when creating a signal");
  550. return;
  551. }
  552. dbus_message_iter_init_append(_signal, &iter);
  553. if (!(dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key) &&
  554. wpa_dbus_dict_open_write(&iter, &dict_iter) &&
  555. wpa_dbus_dict_append_uint16(&dict_iter, "config_methods",
  556. m2d->config_methods) &&
  557. wpa_dbus_dict_append_byte_array(&dict_iter, "manufacturer",
  558. (const char *) m2d->manufacturer,
  559. m2d->manufacturer_len) &&
  560. wpa_dbus_dict_append_byte_array(&dict_iter, "model_name",
  561. (const char *) m2d->model_name,
  562. m2d->model_name_len) &&
  563. wpa_dbus_dict_append_byte_array(&dict_iter, "model_number",
  564. (const char *) m2d->model_number,
  565. m2d->model_number_len) &&
  566. wpa_dbus_dict_append_byte_array(&dict_iter, "serial_number",
  567. (const char *)
  568. m2d->serial_number,
  569. m2d->serial_number_len) &&
  570. wpa_dbus_dict_append_byte_array(&dict_iter, "dev_name",
  571. (const char *) m2d->dev_name,
  572. m2d->dev_name_len) &&
  573. wpa_dbus_dict_append_byte_array(&dict_iter, "primary_dev_type",
  574. (const char *)
  575. m2d->primary_dev_type, 8) &&
  576. wpa_dbus_dict_append_uint16(&dict_iter, "config_error",
  577. m2d->config_error) &&
  578. wpa_dbus_dict_append_uint16(&dict_iter, "dev_password_id",
  579. m2d->dev_password_id) &&
  580. wpa_dbus_dict_close_write(&iter, &dict_iter))) {
  581. wpa_printf(MSG_ERROR, "wpas_dbus_signal_wps_event_m2d[dbus]: "
  582. "out of memory");
  583. goto out;
  584. }
  585. dbus_connection_send(iface->con, _signal, NULL);
  586. out:
  587. dbus_message_unref(_signal);
  588. }
  589. /**
  590. * wpas_dbus_signal_wps_cred - Signals new credentials
  591. * @wpa_s: %wpa_supplicant network interface data
  592. *
  593. * Sends signal with credentials in directory argument
  594. */
  595. void wpas_dbus_signal_wps_cred(struct wpa_supplicant *wpa_s,
  596. const struct wps_credential *cred)
  597. {
  598. DBusMessage *_signal = NULL;
  599. DBusMessageIter iter, dict_iter;
  600. struct wpas_dbus_priv *iface;
  601. char *auth_type[6]; /* we have six possible authorization types */
  602. int at_num = 0;
  603. char *encr_type[4]; /* we have four possible encryption types */
  604. int et_num = 0;
  605. iface = wpa_s->global->dbus;
  606. /* Do nothing if the control interface is not turned on */
  607. if (iface == NULL)
  608. return;
  609. _signal = dbus_message_new_signal(wpa_s->dbus_new_path,
  610. WPAS_DBUS_NEW_IFACE_WPS,
  611. "Credentials");
  612. if (!_signal) {
  613. wpa_printf(MSG_ERROR, "wpas_dbus_signal_wps_cred[dbus]: "
  614. "out of memory when creating a signal");
  615. return;
  616. }
  617. dbus_message_iter_init_append(_signal, &iter);
  618. if (!wpa_dbus_dict_open_write(&iter, &dict_iter)) {
  619. perror("wpas_dbus_signal_wps_cred[dbus]: out of memory "
  620. "when opening a dictionary");
  621. goto nomem;
  622. }
  623. if (cred->auth_type & WPS_AUTH_OPEN)
  624. auth_type[at_num++] = "open";
  625. if (cred->auth_type & WPS_AUTH_WPAPSK)
  626. auth_type[at_num++] = "wpa-psk";
  627. if (cred->auth_type & WPS_AUTH_SHARED)
  628. auth_type[at_num++] = "shared";
  629. if (cred->auth_type & WPS_AUTH_WPA)
  630. auth_type[at_num++] = "wpa-eap";
  631. if (cred->auth_type & WPS_AUTH_WPA2)
  632. auth_type[at_num++] = "wpa2-eap";
  633. if (cred->auth_type & WPS_AUTH_WPA2PSK)
  634. auth_type[at_num++] =
  635. "wpa2-psk";
  636. if (cred->encr_type & WPS_ENCR_NONE)
  637. encr_type[et_num++] = "none";
  638. if (cred->encr_type & WPS_ENCR_WEP)
  639. encr_type[et_num++] = "wep";
  640. if (cred->encr_type & WPS_ENCR_TKIP)
  641. encr_type[et_num++] = "tkip";
  642. if (cred->encr_type & WPS_ENCR_AES)
  643. encr_type[et_num++] = "aes";
  644. if (wpa_s->current_ssid) {
  645. if (!wpa_dbus_dict_append_byte_array(
  646. &dict_iter, "BSSID",
  647. (const char *) wpa_s->current_ssid->bssid,
  648. ETH_ALEN)) {
  649. perror("wpas_dbus_signal_wps_cred[dbus]: out of "
  650. "memory when appending bssid to dictionary");
  651. goto nomem;
  652. }
  653. }
  654. if (!(wpa_dbus_dict_append_byte_array(&dict_iter, "SSID",
  655. (const char *) cred->ssid,
  656. cred->ssid_len) &&
  657. wpa_dbus_dict_append_string_array(&dict_iter, "AuthType",
  658. (const char **) auth_type,
  659. at_num) &&
  660. wpa_dbus_dict_append_string_array(&dict_iter, "EncrType",
  661. (const char **) encr_type,
  662. et_num) &&
  663. wpa_dbus_dict_append_byte_array(&dict_iter, "Key",
  664. (const char *) cred->key,
  665. cred->key_len) &&
  666. wpa_dbus_dict_append_uint32(&dict_iter, "KeyIndex",
  667. cred->key_idx))) {
  668. perror("wpas_dbus_signal_wps_cred[dbus]: out of memory "
  669. "when appending to dictionary");
  670. goto nomem;
  671. }
  672. if (!wpa_dbus_dict_close_write(&iter, &dict_iter)) {
  673. perror("wpas_dbus_signal_wps_cred[dbus]: out of memory "
  674. "when closing a dictionary");
  675. goto nomem;
  676. }
  677. dbus_connection_send(iface->con, _signal, NULL);
  678. nomem:
  679. dbus_message_unref(_signal);
  680. }
  681. #endif /* CONFIG_WPS */
  682. /**
  683. * wpas_dbus_signal_prop_changed - Signals change of property
  684. * @wpa_s: %wpa_supplicant network interface data
  685. * @property: indicates which property has changed
  686. *
  687. * Sends ProertyChanged signals with path, interface and arguments
  688. * depending on which property has changed.
  689. */
  690. void wpas_dbus_signal_prop_changed(struct wpa_supplicant *wpa_s,
  691. enum wpas_dbus_prop property)
  692. {
  693. WPADBusPropertyAccessor getter;
  694. char *iface;
  695. char *prop;
  696. void *arg;
  697. switch (property) {
  698. case WPAS_DBUS_PROP_AP_SCAN:
  699. getter = (WPADBusPropertyAccessor) wpas_dbus_getter_ap_scan;
  700. arg = wpa_s;
  701. iface = WPAS_DBUS_NEW_IFACE_INTERFACE;
  702. prop = "ApScan";
  703. break;
  704. case WPAS_DBUS_PROP_SCANNING:
  705. getter = (WPADBusPropertyAccessor) wpas_dbus_getter_scanning;
  706. arg = wpa_s;
  707. iface = WPAS_DBUS_NEW_IFACE_INTERFACE;
  708. prop = "Scanning";
  709. break;
  710. case WPAS_DBUS_PROP_CURRENT_BSS:
  711. getter = (WPADBusPropertyAccessor)
  712. wpas_dbus_getter_current_bss;
  713. arg = wpa_s;
  714. iface = WPAS_DBUS_NEW_IFACE_INTERFACE;
  715. prop = "CurrentBSS";
  716. break;
  717. case WPAS_DBUS_PROP_CURRENT_NETWORK:
  718. getter = (WPADBusPropertyAccessor)
  719. wpas_dbus_getter_current_network;
  720. arg = wpa_s;
  721. iface = WPAS_DBUS_NEW_IFACE_INTERFACE;
  722. prop = "CurrentNetwork";
  723. break;
  724. default:
  725. wpa_printf(MSG_ERROR, "wpas_dbus_signal_prop_changed[dbus]: "
  726. "Unknown Property enum value %d", property);
  727. return;
  728. }
  729. wpa_dbus_signal_property_changed(wpa_s->global->dbus,
  730. getter, arg, wpa_s->dbus_new_path,
  731. iface, prop);
  732. }
  733. /**
  734. * wpas_dbus_signal_debug_level_changed - Signals change of debug param
  735. * @global: wpa_global structure
  736. *
  737. * Sends ProertyChanged signals informing that debug level has changed.
  738. */
  739. void wpas_dbus_signal_debug_level_changed(struct wpa_global *global)
  740. {
  741. wpa_dbus_signal_property_changed(global->dbus,
  742. (WPADBusPropertyAccessor)
  743. wpas_dbus_getter_debug_level,
  744. global, WPAS_DBUS_NEW_PATH,
  745. WPAS_DBUS_NEW_INTERFACE,
  746. "DebugLevel");
  747. }
  748. /**
  749. * wpas_dbus_signal_debug_timestamp_changed - Signals change of debug param
  750. * @global: wpa_global structure
  751. *
  752. * Sends ProertyChanged signals informing that debug timestamp has changed.
  753. */
  754. void wpas_dbus_signal_debug_timestamp_changed(struct wpa_global *global)
  755. {
  756. wpa_dbus_signal_property_changed(global->dbus,
  757. (WPADBusPropertyAccessor)
  758. wpas_dbus_getter_debug_timestamp,
  759. global, WPAS_DBUS_NEW_PATH,
  760. WPAS_DBUS_NEW_INTERFACE,
  761. "DebugTimestamp");
  762. }
  763. /**
  764. * wpas_dbus_signal_debug_show_keys_changed - Signals change of debug param
  765. * @global: wpa_global structure
  766. *
  767. * Sends ProertyChanged signals informing that debug show_keys has changed.
  768. */
  769. void wpas_dbus_signal_debug_show_keys_changed(struct wpa_global *global)
  770. {
  771. wpa_dbus_signal_property_changed(global->dbus,
  772. (WPADBusPropertyAccessor)
  773. wpas_dbus_getter_debug_show_keys,
  774. global, WPAS_DBUS_NEW_PATH,
  775. WPAS_DBUS_NEW_INTERFACE,
  776. "DebugShowKeys");
  777. }
  778. struct wpas_dbus_method {
  779. const char *name;
  780. const char *iface;
  781. WPADBusMethodHandler handler;
  782. struct wpa_dbus_argument args[3];
  783. };
  784. struct wpas_dbus_property {
  785. const char *name;
  786. const char *iface;
  787. const char *type;
  788. WPADBusPropertyAccessor getter;
  789. WPADBusPropertyAccessor setter;
  790. enum dbus_prop_access _access;
  791. };
  792. struct wpas_dbus_signal {
  793. const char *name;
  794. const char *iface;
  795. struct wpa_dbus_argument args[3];
  796. };
  797. static void wpas_dbus_register(struct wpa_dbus_object_desc *obj_desc,
  798. void *priv,
  799. WPADBusArgumentFreeFunction priv_free,
  800. const struct wpas_dbus_method *methods,
  801. const struct wpas_dbus_property *properties,
  802. const struct wpas_dbus_signal *signals)
  803. {
  804. int i;
  805. obj_desc->user_data = priv;
  806. obj_desc->user_data_free_func = priv_free;
  807. for (i = 0; methods && methods[i].name; i++) {
  808. wpa_dbus_method_register(obj_desc, methods[i].iface,
  809. methods[i].name, methods[i].handler,
  810. methods[i].args);
  811. }
  812. for (i = 0; properties && properties[i].name; i++) {
  813. wpa_dbus_property_register(obj_desc, properties[i].iface,
  814. properties[i].name,
  815. properties[i].type,
  816. properties[i].getter,
  817. properties[i].setter,
  818. properties[i]._access);
  819. }
  820. for (i = 0; signals && signals[i].name; i++) {
  821. wpa_dbus_signal_register(obj_desc, signals[i].iface,
  822. signals[i].name, signals[i].args);
  823. }
  824. }
  825. static const struct wpas_dbus_method wpas_dbus_global_methods[] = {
  826. { "CreateInterface", WPAS_DBUS_NEW_INTERFACE,
  827. (WPADBusMethodHandler) &wpas_dbus_handler_create_interface,
  828. {
  829. { "args", "a{sv}", ARG_IN },
  830. { "path", "o", ARG_OUT },
  831. END_ARGS
  832. }
  833. },
  834. { "RemoveInterface", WPAS_DBUS_NEW_INTERFACE,
  835. (WPADBusMethodHandler) &wpas_dbus_handler_remove_interface,
  836. {
  837. { "path", "o", ARG_IN },
  838. END_ARGS
  839. }
  840. },
  841. { "GetInterface", WPAS_DBUS_NEW_INTERFACE,
  842. (WPADBusMethodHandler) &wpas_dbus_handler_get_interface,
  843. {
  844. { "ifname", "s", ARG_IN },
  845. { "path", "o", ARG_OUT },
  846. END_ARGS
  847. }
  848. },
  849. { NULL, NULL, NULL, { END_ARGS } }
  850. };
  851. static const struct wpas_dbus_property wpas_dbus_global_properties[] = {
  852. { "DebugLevel", WPAS_DBUS_NEW_INTERFACE, "y",
  853. (WPADBusPropertyAccessor) wpas_dbus_getter_debug_level,
  854. (WPADBusPropertyAccessor) wpas_dbus_setter_debug_level,
  855. RW
  856. },
  857. { "DebugTimestamp", WPAS_DBUS_NEW_INTERFACE, "b",
  858. (WPADBusPropertyAccessor) wpas_dbus_getter_debug_timestamp,
  859. (WPADBusPropertyAccessor) wpas_dbus_setter_debug_timestamp,
  860. RW
  861. },
  862. { "DebugShowKeys", WPAS_DBUS_NEW_INTERFACE, "b",
  863. (WPADBusPropertyAccessor) wpas_dbus_getter_debug_show_keys,
  864. (WPADBusPropertyAccessor) wpas_dbus_setter_debug_show_keys,
  865. RW
  866. },
  867. { "Interfaces", WPAS_DBUS_NEW_INTERFACE, "ao",
  868. (WPADBusPropertyAccessor) &wpas_dbus_getter_interfaces,
  869. NULL,
  870. R
  871. },
  872. { "EapMethods", WPAS_DBUS_NEW_INTERFACE, "as",
  873. (WPADBusPropertyAccessor) wpas_dbus_getter_eap_methods,
  874. NULL,
  875. R
  876. },
  877. { NULL, NULL, NULL, NULL, NULL, 0 }
  878. };
  879. static const struct wpas_dbus_signal wpas_dbus_global_signals[] = {
  880. { "InterfaceAdded", WPAS_DBUS_NEW_INTERFACE,
  881. {
  882. { "path", "o", ARG_OUT },
  883. { "properties", "a{sv}", ARG_OUT },
  884. END_ARGS
  885. }
  886. },
  887. { "InterfaceRemoved", WPAS_DBUS_NEW_INTERFACE,
  888. {
  889. { "path", "o", ARG_OUT },
  890. END_ARGS
  891. }
  892. },
  893. { "PropertiesChanged", WPAS_DBUS_NEW_INTERFACE,
  894. {
  895. { "properties", "a{sv}", ARG_OUT },
  896. END_ARGS
  897. }
  898. },
  899. { NULL, NULL, { END_ARGS } }
  900. };
  901. /**
  902. * wpas_dbus_ctrl_iface_init - Initialize dbus control interface
  903. * @global: Pointer to global data from wpa_supplicant_init()
  904. * Returns: 0 on success or -1 on failure
  905. *
  906. * Initialize the dbus control interface for wpa_supplicantand and start
  907. * receiving commands from external programs over the bus.
  908. */
  909. int wpas_dbus_ctrl_iface_init(struct wpas_dbus_priv *priv)
  910. {
  911. struct wpa_dbus_object_desc *obj_desc;
  912. int ret;
  913. obj_desc = os_zalloc(sizeof(struct wpa_dbus_object_desc));
  914. if (!obj_desc) {
  915. wpa_printf(MSG_ERROR, "Not enough memory "
  916. "to create object description");
  917. return -1;
  918. }
  919. wpas_dbus_register(obj_desc, priv->global, NULL,
  920. wpas_dbus_global_methods,
  921. wpas_dbus_global_properties,
  922. wpas_dbus_global_signals);
  923. wpa_printf(MSG_DEBUG, "dbus: Register D-Bus object '%s'",
  924. WPAS_DBUS_NEW_PATH);
  925. ret = wpa_dbus_ctrl_iface_init(priv, WPAS_DBUS_NEW_PATH,
  926. WPAS_DBUS_NEW_SERVICE,
  927. obj_desc);
  928. if (ret < 0)
  929. free_dbus_object_desc(obj_desc);
  930. else
  931. priv->dbus_new_initialized = 1;
  932. return ret;
  933. }
  934. /**
  935. * wpas_dbus_ctrl_iface_deinit - Deinitialize dbus ctrl interface for
  936. * wpa_supplicant
  937. * @iface: Pointer to dbus private data from wpas_dbus_init()
  938. *
  939. * Deinitialize the dbus control interface that was initialized with
  940. * wpas_dbus_ctrl_iface_init().
  941. */
  942. void wpas_dbus_ctrl_iface_deinit(struct wpas_dbus_priv *iface)
  943. {
  944. if (!iface->dbus_new_initialized)
  945. return;
  946. wpa_printf(MSG_DEBUG, "dbus: Unregister D-Bus object '%s'",
  947. WPAS_DBUS_NEW_PATH);
  948. dbus_connection_unregister_object_path(iface->con,
  949. WPAS_DBUS_NEW_PATH);
  950. }
  951. static void wpa_dbus_free(void *ptr)
  952. {
  953. os_free(ptr);
  954. }
  955. static const struct wpas_dbus_property wpas_dbus_network_properties[] = {
  956. { "Properties", WPAS_DBUS_NEW_IFACE_NETWORK, "a{sv}",
  957. (WPADBusPropertyAccessor) wpas_dbus_getter_network_properties,
  958. (WPADBusPropertyAccessor) wpas_dbus_setter_network_properties,
  959. RW
  960. },
  961. { "Enabled", WPAS_DBUS_NEW_IFACE_NETWORK, "b",
  962. (WPADBusPropertyAccessor) wpas_dbus_getter_enabled,
  963. (WPADBusPropertyAccessor) wpas_dbus_setter_enabled,
  964. RW
  965. },
  966. { NULL, NULL, NULL, NULL, NULL, 0 }
  967. };
  968. static const struct wpas_dbus_signal wpas_dbus_network_signals[] = {
  969. { "PropertiesChanged", WPAS_DBUS_NEW_IFACE_NETWORK,
  970. {
  971. { "properties", "a{sv}", ARG_OUT },
  972. END_ARGS
  973. }
  974. },
  975. { NULL, NULL, { END_ARGS } }
  976. };
  977. /**
  978. * wpas_dbus_register_network - Register a configured network with dbus
  979. * @wpa_s: wpa_supplicant interface structure
  980. * @ssid: network configuration data
  981. * Returns: 0 on success, -1 on failure
  982. *
  983. * Registers network representing object with dbus
  984. */
  985. int wpas_dbus_register_network(struct wpa_supplicant *wpa_s,
  986. struct wpa_ssid *ssid)
  987. {
  988. struct wpas_dbus_priv *ctrl_iface;
  989. struct wpa_dbus_object_desc *obj_desc;
  990. struct network_handler_args *arg = NULL;
  991. char *net_obj_path;
  992. /* Do nothing if the control interface is not turned on */
  993. if (wpa_s == NULL || wpa_s->global == NULL)
  994. return 0;
  995. ctrl_iface = wpa_s->global->dbus;
  996. if (ctrl_iface == NULL)
  997. return 0;
  998. net_obj_path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  999. if (net_obj_path == NULL)
  1000. return -1;
  1001. os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  1002. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%u",
  1003. wpa_s->dbus_new_path, ssid->id);
  1004. wpa_printf(MSG_DEBUG, "dbus: Register network object '%s'",
  1005. net_obj_path);
  1006. obj_desc = os_zalloc(sizeof(struct wpa_dbus_object_desc));
  1007. if (!obj_desc) {
  1008. wpa_printf(MSG_ERROR, "Not enough memory "
  1009. "to create object description");
  1010. goto err;
  1011. }
  1012. /* allocate memory for handlers arguments */
  1013. arg = os_zalloc(sizeof(struct network_handler_args));
  1014. if (!arg) {
  1015. wpa_printf(MSG_ERROR, "Not enough memory "
  1016. "to create arguments for method");
  1017. goto err;
  1018. }
  1019. arg->wpa_s = wpa_s;
  1020. arg->ssid = ssid;
  1021. wpas_dbus_register(obj_desc, arg, wpa_dbus_free, NULL,
  1022. wpas_dbus_network_properties,
  1023. wpas_dbus_network_signals);
  1024. if (wpa_dbus_register_object_per_iface(ctrl_iface, net_obj_path,
  1025. wpa_s->ifname, obj_desc))
  1026. goto err;
  1027. wpas_dbus_signal_network_added(wpa_s, ssid->id);
  1028. os_free(net_obj_path);
  1029. return 0;
  1030. err:
  1031. os_free(net_obj_path);
  1032. os_free(obj_desc);
  1033. os_free(arg);
  1034. return -1;
  1035. }
  1036. /**
  1037. * wpas_dbus_unregister_network - Unregister a configured network from dbus
  1038. * @wpa_s: wpa_supplicant interface structure
  1039. * @nid: network id
  1040. * Returns: 0 on success, -1 on failure
  1041. *
  1042. * Unregisters network representing object from dbus
  1043. */
  1044. int wpas_dbus_unregister_network(struct wpa_supplicant *wpa_s, int nid)
  1045. {
  1046. struct wpas_dbus_priv *ctrl_iface;
  1047. char *net_obj_path;
  1048. int ret;
  1049. /* Do nothing if the control interface is not turned on */
  1050. if (wpa_s == NULL || wpa_s->global == NULL)
  1051. return 0;
  1052. ctrl_iface = wpa_s->global->dbus;
  1053. if (ctrl_iface == NULL)
  1054. return 0;
  1055. net_obj_path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  1056. if (net_obj_path == NULL)
  1057. return -1;
  1058. os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  1059. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%u",
  1060. wpa_s->dbus_new_path, nid);
  1061. wpa_printf(MSG_DEBUG, "dbus: Unregister network object '%s'",
  1062. net_obj_path);
  1063. ret = wpa_dbus_unregister_object_per_iface(ctrl_iface, net_obj_path);
  1064. if (!ret)
  1065. wpas_dbus_signal_network_removed(wpa_s, nid);
  1066. os_free(net_obj_path);
  1067. return ret;
  1068. }
  1069. static const struct wpas_dbus_property wpas_dbus_bss_properties[] = {
  1070. { "SSID", WPAS_DBUS_NEW_IFACE_BSSID, "ay",
  1071. (WPADBusPropertyAccessor) wpas_dbus_getter_bss_ssid,
  1072. NULL,
  1073. R
  1074. },
  1075. { "BSSID", WPAS_DBUS_NEW_IFACE_BSSID, "ay",
  1076. (WPADBusPropertyAccessor) wpas_dbus_getter_bss_bssid,
  1077. NULL,
  1078. R
  1079. },
  1080. { "Privacy", WPAS_DBUS_NEW_IFACE_BSSID, "b",
  1081. (WPADBusPropertyAccessor) wpas_dbus_getter_bss_privacy,
  1082. NULL,
  1083. R
  1084. },
  1085. { "Mode", WPAS_DBUS_NEW_IFACE_BSSID, "s",
  1086. (WPADBusPropertyAccessor) wpas_dbus_getter_bss_mode,
  1087. NULL,
  1088. R
  1089. },
  1090. { "Signal", WPAS_DBUS_NEW_IFACE_BSSID, "n",
  1091. (WPADBusPropertyAccessor) wpas_dbus_getter_bss_signal,
  1092. NULL,
  1093. R
  1094. },
  1095. { "Frequency", WPAS_DBUS_NEW_IFACE_BSSID, "q",
  1096. (WPADBusPropertyAccessor) wpas_dbus_getter_bss_frequency,
  1097. NULL,
  1098. R
  1099. },
  1100. { "MaxRate", WPAS_DBUS_NEW_IFACE_BSSID, "q",
  1101. (WPADBusPropertyAccessor) wpas_dbus_getter_bss_max_rate,
  1102. NULL,
  1103. R
  1104. },
  1105. { "WPAIE", WPAS_DBUS_NEW_IFACE_BSSID, "ay",
  1106. (WPADBusPropertyAccessor) wpas_dbus_getter_bss_wpaie,
  1107. NULL,
  1108. R
  1109. },
  1110. { "RSNIE", WPAS_DBUS_NEW_IFACE_BSSID, "ay",
  1111. (WPADBusPropertyAccessor) wpas_dbus_getter_bss_rsnie,
  1112. NULL,
  1113. R
  1114. },
  1115. { "WPSIE", WPAS_DBUS_NEW_IFACE_BSSID, "ay",
  1116. (WPADBusPropertyAccessor) wpas_dbus_getter_bss_wpsie,
  1117. NULL,
  1118. R
  1119. },
  1120. { NULL, NULL, NULL, NULL, NULL, 0 }
  1121. };
  1122. static const struct wpas_dbus_signal wpas_dbus_bss_signals[] = {
  1123. { "PropertiesChanged", WPAS_DBUS_NEW_IFACE_BSSID,
  1124. {
  1125. { "properties", "a{sv}", ARG_OUT },
  1126. END_ARGS
  1127. }
  1128. },
  1129. { NULL, NULL, { END_ARGS } }
  1130. };
  1131. /**
  1132. * wpas_dbus_unregister_bss - Unregister a scanned BSS from dbus
  1133. * @wpa_s: wpa_supplicant interface structure
  1134. * @bssid: scanned network bssid
  1135. * @id: unique BSS identifier
  1136. * Returns: 0 on success, -1 on failure
  1137. *
  1138. * Unregisters BSS representing object from dbus
  1139. */
  1140. int wpas_dbus_unregister_bss(struct wpa_supplicant *wpa_s,
  1141. u8 bssid[ETH_ALEN], unsigned int id)
  1142. {
  1143. struct wpas_dbus_priv *ctrl_iface;
  1144. char *bss_obj_path;
  1145. /* Do nothing if the control interface is not turned on */
  1146. if (wpa_s == NULL || wpa_s->global == NULL)
  1147. return 0;
  1148. ctrl_iface = wpa_s->global->dbus;
  1149. if (ctrl_iface == NULL)
  1150. return 0;
  1151. bss_obj_path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  1152. if (bss_obj_path == NULL)
  1153. return -1;
  1154. os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  1155. "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
  1156. wpa_s->dbus_new_path, id);
  1157. wpa_printf(MSG_DEBUG, "dbus: Unregister BSS object '%s'",
  1158. bss_obj_path);
  1159. if (wpa_dbus_unregister_object_per_iface(ctrl_iface, bss_obj_path)) {
  1160. wpa_printf(MSG_ERROR,
  1161. "Cannot unregister BSSID dbus object %s.",
  1162. bss_obj_path);
  1163. os_free(bss_obj_path);
  1164. return -1;
  1165. }
  1166. wpas_dbus_signal_bss_removed(wpa_s, bss_obj_path);
  1167. os_free(bss_obj_path);
  1168. return 0;
  1169. }
  1170. /**
  1171. * wpas_dbus_register_bss - Register a scanned BSS with dbus
  1172. * @wpa_s: wpa_supplicant interface structure
  1173. * @bssid: scanned network bssid
  1174. * @id: unique BSS identifier
  1175. * Returns: 0 on success, -1 on failure
  1176. *
  1177. * Registers BSS representing object with dbus
  1178. */
  1179. int wpas_dbus_register_bss(struct wpa_supplicant *wpa_s,
  1180. u8 bssid[ETH_ALEN], unsigned int id)
  1181. {
  1182. struct wpas_dbus_priv *ctrl_iface;
  1183. struct wpa_dbus_object_desc *obj_desc;
  1184. char *bss_obj_path;
  1185. struct bss_handler_args *arg = NULL;
  1186. /* Do nothing if the control interface is not turned on */
  1187. if (wpa_s == NULL || wpa_s->global == NULL)
  1188. return 0;
  1189. ctrl_iface = wpa_s->global->dbus;
  1190. if (ctrl_iface == NULL)
  1191. return 0;
  1192. bss_obj_path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  1193. if (bss_obj_path == NULL)
  1194. return -1;
  1195. os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  1196. "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
  1197. wpa_s->dbus_new_path, id);
  1198. obj_desc = os_zalloc(sizeof(struct wpa_dbus_object_desc));
  1199. if (!obj_desc) {
  1200. wpa_printf(MSG_ERROR, "Not enough memory "
  1201. "to create object description");
  1202. goto err;
  1203. }
  1204. arg = os_zalloc(sizeof(struct bss_handler_args));
  1205. if (!arg) {
  1206. wpa_printf(MSG_ERROR, "Not enough memory "
  1207. "to create arguments for handler");
  1208. goto err;
  1209. }
  1210. arg->wpa_s = wpa_s;
  1211. arg->id = id;
  1212. wpas_dbus_register(obj_desc, arg, wpa_dbus_free, NULL,
  1213. wpas_dbus_bss_properties,
  1214. wpas_dbus_bss_signals);
  1215. wpa_printf(MSG_DEBUG, "dbus: Register BSS object '%s'",
  1216. bss_obj_path);
  1217. if (wpa_dbus_register_object_per_iface(ctrl_iface, bss_obj_path,
  1218. wpa_s->ifname, obj_desc)) {
  1219. wpa_printf(MSG_ERROR,
  1220. "Cannot register BSSID dbus object %s.",
  1221. bss_obj_path);
  1222. goto err;
  1223. }
  1224. wpas_dbus_signal_bss_added(wpa_s, bss_obj_path);
  1225. os_free(bss_obj_path);
  1226. return 0;
  1227. err:
  1228. os_free(bss_obj_path);
  1229. os_free(obj_desc);
  1230. os_free(arg);
  1231. return -1;
  1232. }
  1233. static const struct wpas_dbus_method wpas_dbus_interface_methods[] = {
  1234. { "Scan", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1235. (WPADBusMethodHandler) &wpas_dbus_handler_scan,
  1236. {
  1237. { "args", "a{sv}", ARG_IN },
  1238. END_ARGS
  1239. }
  1240. },
  1241. { "Disconnect", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1242. (WPADBusMethodHandler) &wpas_dbus_handler_disconnect,
  1243. {
  1244. END_ARGS
  1245. }
  1246. },
  1247. { "AddNetwork", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1248. (WPADBusMethodHandler) &wpas_dbus_handler_add_network,
  1249. {
  1250. { "args", "a{sv}", ARG_IN },
  1251. { "path", "o", ARG_OUT },
  1252. END_ARGS
  1253. }
  1254. },
  1255. { "RemoveNetwork", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1256. (WPADBusMethodHandler) &wpas_dbus_handler_remove_network,
  1257. {
  1258. { "path", "o", ARG_IN },
  1259. END_ARGS
  1260. }
  1261. },
  1262. { "SelectNetwork", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1263. (WPADBusMethodHandler) &wpas_dbus_handler_select_network,
  1264. {
  1265. { "path", "o", ARG_IN },
  1266. END_ARGS
  1267. }
  1268. },
  1269. { "AddBlob", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1270. (WPADBusMethodHandler) &wpas_dbus_handler_add_blob,
  1271. {
  1272. { "name", "s", ARG_IN },
  1273. { "data", "ay", ARG_IN },
  1274. END_ARGS
  1275. }
  1276. },
  1277. { "GetBlob", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1278. (WPADBusMethodHandler) &wpas_dbus_handler_get_blob,
  1279. {
  1280. { "name", "s", ARG_IN },
  1281. { "data", "ay", ARG_OUT },
  1282. END_ARGS
  1283. }
  1284. },
  1285. { "RemoveBlob", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1286. (WPADBusMethodHandler) &wpas_dbus_handler_remove_blob,
  1287. {
  1288. { "name", "s", ARG_IN },
  1289. END_ARGS
  1290. }
  1291. },
  1292. #ifdef CONFIG_WPS
  1293. { "Start", WPAS_DBUS_NEW_IFACE_WPS,
  1294. (WPADBusMethodHandler) &wpas_dbus_handler_wps_start,
  1295. {
  1296. { "args", "a{sv}", ARG_IN },
  1297. { "output", "a{sv}", ARG_OUT },
  1298. END_ARGS
  1299. }
  1300. },
  1301. #endif /* CONFIG_WPS */
  1302. { NULL, NULL, NULL, { END_ARGS } }
  1303. };
  1304. static const struct wpas_dbus_property wpas_dbus_interface_properties[] = {
  1305. { "Capabilities", WPAS_DBUS_NEW_IFACE_INTERFACE, "a{sv}",
  1306. (WPADBusPropertyAccessor) wpas_dbus_getter_capabilities,
  1307. NULL, R
  1308. },
  1309. { "State", WPAS_DBUS_NEW_IFACE_INTERFACE, "s",
  1310. (WPADBusPropertyAccessor) wpas_dbus_getter_state,
  1311. NULL, R
  1312. },
  1313. { "Scanning", WPAS_DBUS_NEW_IFACE_INTERFACE, "b",
  1314. (WPADBusPropertyAccessor) wpas_dbus_getter_scanning,
  1315. NULL, R
  1316. },
  1317. { "ApScan", WPAS_DBUS_NEW_IFACE_INTERFACE, "u",
  1318. (WPADBusPropertyAccessor) wpas_dbus_getter_ap_scan,
  1319. (WPADBusPropertyAccessor) wpas_dbus_setter_ap_scan,
  1320. RW
  1321. },
  1322. { "Ifname", WPAS_DBUS_NEW_IFACE_INTERFACE, "s",
  1323. (WPADBusPropertyAccessor) wpas_dbus_getter_ifname,
  1324. NULL, R
  1325. },
  1326. { "Driver", WPAS_DBUS_NEW_IFACE_INTERFACE, "s",
  1327. (WPADBusPropertyAccessor) wpas_dbus_getter_driver,
  1328. NULL, R
  1329. },
  1330. { "BridgeIfname", WPAS_DBUS_NEW_IFACE_INTERFACE, "s",
  1331. (WPADBusPropertyAccessor) wpas_dbus_getter_bridge_ifname,
  1332. NULL, R
  1333. },
  1334. { "CurrentBSS", WPAS_DBUS_NEW_IFACE_INTERFACE, "o",
  1335. (WPADBusPropertyAccessor) wpas_dbus_getter_current_bss,
  1336. NULL, R
  1337. },
  1338. { "CurrentNetwork", WPAS_DBUS_NEW_IFACE_INTERFACE, "o",
  1339. (WPADBusPropertyAccessor) wpas_dbus_getter_current_network,
  1340. NULL, R
  1341. },
  1342. { "Blobs", WPAS_DBUS_NEW_IFACE_INTERFACE, "a{say}",
  1343. (WPADBusPropertyAccessor) wpas_dbus_getter_blobs,
  1344. NULL, R
  1345. },
  1346. { "BSSs", WPAS_DBUS_NEW_IFACE_INTERFACE, "ao",
  1347. (WPADBusPropertyAccessor) wpas_dbus_getter_bsss,
  1348. NULL, R
  1349. },
  1350. { "Networks", WPAS_DBUS_NEW_IFACE_INTERFACE, "ao",
  1351. (WPADBusPropertyAccessor) wpas_dbus_getter_networks,
  1352. NULL, R
  1353. },
  1354. #ifdef CONFIG_WPS
  1355. { "ProcessCredentials", WPAS_DBUS_NEW_IFACE_WPS, "b",
  1356. (WPADBusPropertyAccessor) wpas_dbus_getter_process_credentials,
  1357. (WPADBusPropertyAccessor) wpas_dbus_setter_process_credentials,
  1358. RW
  1359. },
  1360. #endif /* CONFIG_WPS */
  1361. { NULL, NULL, NULL, NULL, NULL, 0 }
  1362. };
  1363. static const struct wpas_dbus_signal wpas_dbus_interface_signals[] = {
  1364. { "ScanDone", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1365. {
  1366. { "success", "b", ARG_OUT },
  1367. END_ARGS
  1368. }
  1369. },
  1370. { "StateChanged", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1371. {
  1372. { "newState", "s", ARG_OUT },
  1373. { "oldState", "s", ARG_OUT },
  1374. END_ARGS
  1375. }
  1376. },
  1377. { "BSSAdded", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1378. {
  1379. { "path", "o", ARG_OUT },
  1380. { "properties", "a{sv}", ARG_OUT },
  1381. END_ARGS
  1382. }
  1383. },
  1384. { "BSSRemoved", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1385. {
  1386. { "path", "o", ARG_OUT },
  1387. END_ARGS
  1388. }
  1389. },
  1390. { "BlobAdded", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1391. {
  1392. { "name", "s", ARG_OUT },
  1393. END_ARGS
  1394. }
  1395. },
  1396. { "BlobRemoved", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1397. {
  1398. { "name", "s", ARG_OUT },
  1399. END_ARGS
  1400. }
  1401. },
  1402. { "NetworkAdded", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1403. {
  1404. { "path", "o", ARG_OUT },
  1405. { "properties", "a{sv}", ARG_OUT },
  1406. END_ARGS
  1407. }
  1408. },
  1409. { "NetworkRemoved", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1410. {
  1411. { "path", "o", ARG_OUT },
  1412. END_ARGS
  1413. }
  1414. },
  1415. { "NetworkSelected", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1416. {
  1417. { "path", "o", ARG_OUT },
  1418. END_ARGS
  1419. }
  1420. },
  1421. { "PropertiesChanged", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1422. {
  1423. { "properties", "a{sv}", ARG_OUT },
  1424. END_ARGS
  1425. }
  1426. },
  1427. #ifdef CONFIG_WPS
  1428. { "Event", WPAS_DBUS_NEW_IFACE_WPS,
  1429. {
  1430. { "name", "s", ARG_OUT },
  1431. { "args", "a{sv}", ARG_OUT },
  1432. END_ARGS
  1433. }
  1434. },
  1435. { "Credentials", WPAS_DBUS_NEW_IFACE_WPS,
  1436. {
  1437. { "credentials", "a{sv}", ARG_OUT },
  1438. END_ARGS
  1439. }
  1440. },
  1441. { "PropertiesChanged", WPAS_DBUS_NEW_IFACE_WPS,
  1442. {
  1443. { "properties", "a{sv}", ARG_OUT },
  1444. END_ARGS
  1445. }
  1446. },
  1447. #endif /* CONFIG_WPS */
  1448. { NULL, NULL, { END_ARGS } }
  1449. };
  1450. int wpas_dbus_register_interface(struct wpa_supplicant *wpa_s)
  1451. {
  1452. struct wpa_dbus_object_desc *obj_desc = NULL;
  1453. struct wpas_dbus_priv *ctrl_iface = wpa_s->global->dbus;
  1454. int next;
  1455. /* Do nothing if the control interface is not turned on */
  1456. if (ctrl_iface == NULL)
  1457. return 0;
  1458. /* Create and set the interface's object path */
  1459. wpa_s->dbus_new_path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  1460. if (wpa_s->dbus_new_path == NULL)
  1461. return -1;
  1462. next = ctrl_iface->next_objid++;
  1463. os_snprintf(wpa_s->dbus_new_path, WPAS_DBUS_OBJECT_PATH_MAX,
  1464. WPAS_DBUS_NEW_PATH_INTERFACES "/%u",
  1465. next);
  1466. obj_desc = os_zalloc(sizeof(struct wpa_dbus_object_desc));
  1467. if (!obj_desc) {
  1468. wpa_printf(MSG_ERROR, "Not enough memory "
  1469. "to create object description");
  1470. goto err;
  1471. }
  1472. wpas_dbus_register(obj_desc, wpa_s, NULL, wpas_dbus_interface_methods,
  1473. wpas_dbus_interface_properties,
  1474. wpas_dbus_interface_signals);
  1475. wpa_printf(MSG_DEBUG, "dbus: Register interface object '%s'",
  1476. wpa_s->dbus_new_path);
  1477. if (wpa_dbus_register_object_per_iface(ctrl_iface,
  1478. wpa_s->dbus_new_path,
  1479. wpa_s->ifname, obj_desc))
  1480. goto err;
  1481. wpas_dbus_signal_interface_added(wpa_s);
  1482. return 0;
  1483. err:
  1484. os_free(wpa_s->dbus_new_path);
  1485. wpa_s->dbus_new_path = NULL;
  1486. os_free(obj_desc);
  1487. return -1;
  1488. }
  1489. int wpas_dbus_unregister_interface(struct wpa_supplicant *wpa_s)
  1490. {
  1491. struct wpas_dbus_priv *ctrl_iface;
  1492. /* Do nothing if the control interface is not turned on */
  1493. if (wpa_s == NULL || wpa_s->global == NULL)
  1494. return 0;
  1495. ctrl_iface = wpa_s->global->dbus;
  1496. if (ctrl_iface == NULL)
  1497. return 0;
  1498. wpa_printf(MSG_DEBUG, "dbus: Unregister interface object '%s'",
  1499. wpa_s->dbus_new_path);
  1500. if (wpa_dbus_unregister_object_per_iface(ctrl_iface,
  1501. wpa_s->dbus_new_path))
  1502. return -1;
  1503. wpas_dbus_signal_interface_removed(wpa_s);
  1504. os_free(wpa_s->dbus_new_path);
  1505. wpa_s->dbus_new_path = NULL;
  1506. return 0;
  1507. }