dbus_new.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714
  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. return;
  385. new_state_str = os_strdup(wpa_supplicant_state_txt(new_state));
  386. old_state_str = os_strdup(wpa_supplicant_state_txt(old_state));
  387. if (new_state_str == NULL || old_state_str == NULL)
  388. goto out;
  389. /* make state string lowercase to fit new DBus API convention */
  390. tmp = new_state_str;
  391. while (*tmp) {
  392. *tmp = tolower(*tmp);
  393. tmp++;
  394. }
  395. tmp = old_state_str;
  396. while (*tmp) {
  397. *tmp = tolower(*tmp);
  398. tmp++;
  399. }
  400. if (!dbus_message_append_args(_signal,
  401. DBUS_TYPE_STRING, &new_state_str,
  402. DBUS_TYPE_STRING, &old_state_str,
  403. DBUS_TYPE_INVALID)) {
  404. wpa_printf(MSG_ERROR,
  405. "dbus: wpas_dbus_signal_state_changed: "
  406. "not enough memory to construct state change "
  407. "signal");
  408. goto out;
  409. }
  410. dbus_connection_send(iface->con, _signal, NULL);
  411. out:
  412. dbus_message_unref(_signal);
  413. os_free(new_state_str);
  414. os_free(old_state_str);
  415. }
  416. /**
  417. * wpas_dbus_signal_network_enabled_changed - Signals Enabled property changes
  418. * @wpa_s: %wpa_supplicant network interface data
  419. * @ssid: configured network which Enabled property has changed
  420. *
  421. * Sends PropertyChanged signals containing new value of Enabled property
  422. * for specified network
  423. */
  424. void wpas_dbus_signal_network_enabled_changed(struct wpa_supplicant *wpa_s,
  425. struct wpa_ssid *ssid)
  426. {
  427. struct network_handler_args args = {wpa_s, ssid};
  428. char path[WPAS_DBUS_OBJECT_PATH_MAX];
  429. os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
  430. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%d",
  431. wpa_s->dbus_new_path, ssid->id);
  432. wpa_dbus_signal_property_changed(wpa_s->global->dbus,
  433. (WPADBusPropertyAccessor)
  434. wpas_dbus_getter_enabled, &args,
  435. path, WPAS_DBUS_NEW_IFACE_NETWORK,
  436. "Enabled");
  437. }
  438. #ifdef CONFIG_WPS
  439. /**
  440. * wpas_dbus_signal_wps_event_success - Signals Success WPS event
  441. * @wpa_s: %wpa_supplicant network interface data
  442. *
  443. * Sends Event dbus signal with name "success" and empty dict as arguments
  444. */
  445. void wpas_dbus_signal_wps_event_success(struct wpa_supplicant *wpa_s)
  446. {
  447. DBusMessage *_signal = NULL;
  448. DBusMessageIter iter, dict_iter;
  449. struct wpas_dbus_priv *iface;
  450. char *key = "success";
  451. iface = wpa_s->global->dbus;
  452. /* Do nothing if the control interface is not turned on */
  453. if (iface == NULL)
  454. return;
  455. _signal = dbus_message_new_signal(wpa_s->dbus_new_path,
  456. WPAS_DBUS_NEW_IFACE_WPS, "Event");
  457. if (!_signal) {
  458. wpa_printf(MSG_ERROR, "wpas_dbus_signal_wps_event_success"
  459. "[dbus]: out of memory when creating a signal");
  460. return;
  461. }
  462. dbus_message_iter_init_append(_signal, &iter);
  463. if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key) ||
  464. !wpa_dbus_dict_open_write(&iter, &dict_iter) ||
  465. !wpa_dbus_dict_close_write(&iter, &dict_iter)) {
  466. wpa_printf(MSG_ERROR, "wpas_dbus_signal_wps_event_success"
  467. "[dbus]: out of memory");
  468. goto out;
  469. }
  470. dbus_connection_send(iface->con, _signal, NULL);
  471. out:
  472. dbus_message_unref(_signal);
  473. }
  474. /**
  475. * wpas_dbus_signal_wps_event_fail - Signals Fail WPS event
  476. * @wpa_s: %wpa_supplicant network interface data
  477. *
  478. * Sends Event dbus signal with name "fail" and dictionary containing
  479. * "msg field with fail message number (int32) as arguments
  480. */
  481. void wpas_dbus_signal_wps_event_fail(struct wpa_supplicant *wpa_s,
  482. struct wps_event_fail *fail)
  483. {
  484. DBusMessage *_signal = NULL;
  485. DBusMessageIter iter, dict_iter;
  486. struct wpas_dbus_priv *iface;
  487. char *key = "fail";
  488. iface = wpa_s->global->dbus;
  489. /* Do nothing if the control interface is not turned on */
  490. if (iface == NULL)
  491. return;
  492. _signal = dbus_message_new_signal(wpa_s->dbus_new_path,
  493. WPAS_DBUS_NEW_IFACE_WPS, "Event");
  494. if (!_signal) {
  495. wpa_printf(MSG_ERROR, "wpas_dbus_signal_wps_event_fail[dbus]: "
  496. "out of memory when creating a signal");
  497. return;
  498. }
  499. dbus_message_iter_init_append(_signal, &iter);
  500. if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key) ||
  501. !wpa_dbus_dict_open_write(&iter, &dict_iter) ||
  502. !wpa_dbus_dict_append_int32(&dict_iter, "msg", fail->msg) ||
  503. !wpa_dbus_dict_close_write(&iter, &dict_iter)) {
  504. wpa_printf(MSG_ERROR, "wpas_dbus_signal_wps_event_fail[dbus]: "
  505. "out of memory");
  506. goto out;
  507. }
  508. dbus_connection_send(iface->con, _signal, NULL);
  509. out:
  510. dbus_message_unref(_signal);
  511. }
  512. /**
  513. * wpas_dbus_signal_wps_event_m2d - Signals M2D WPS event
  514. * @wpa_s: %wpa_supplicant network interface data
  515. *
  516. * Sends Event dbus signal with name "m2d" and dictionary containing
  517. * fields of wps_event_m2d structure.
  518. */
  519. void wpas_dbus_signal_wps_event_m2d(struct wpa_supplicant *wpa_s,
  520. struct wps_event_m2d *m2d)
  521. {
  522. DBusMessage *_signal = NULL;
  523. DBusMessageIter iter, dict_iter;
  524. struct wpas_dbus_priv *iface;
  525. char *key = "m2d";
  526. iface = wpa_s->global->dbus;
  527. /* Do nothing if the control interface is not turned on */
  528. if (iface == NULL)
  529. return;
  530. _signal = dbus_message_new_signal(wpa_s->dbus_new_path,
  531. WPAS_DBUS_NEW_IFACE_WPS, "Event");
  532. if (!_signal) {
  533. wpa_printf(MSG_ERROR, "wpas_dbus_signal_wps_event_m2d[dbus]: "
  534. "out of memory when creating a signal");
  535. return;
  536. }
  537. dbus_message_iter_init_append(_signal, &iter);
  538. if (!(dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key) &&
  539. wpa_dbus_dict_open_write(&iter, &dict_iter) &&
  540. wpa_dbus_dict_append_uint16(&dict_iter, "config_methods",
  541. m2d->config_methods) &&
  542. wpa_dbus_dict_append_byte_array(&dict_iter, "manufacturer",
  543. (const char *) m2d->manufacturer,
  544. m2d->manufacturer_len) &&
  545. wpa_dbus_dict_append_byte_array(&dict_iter, "model_name",
  546. (const char *) m2d->model_name,
  547. m2d->model_name_len) &&
  548. wpa_dbus_dict_append_byte_array(&dict_iter, "model_number",
  549. (const char *) m2d->model_number,
  550. m2d->model_number_len) &&
  551. wpa_dbus_dict_append_byte_array(&dict_iter, "serial_number",
  552. (const char *)
  553. m2d->serial_number,
  554. m2d->serial_number_len) &&
  555. wpa_dbus_dict_append_byte_array(&dict_iter, "dev_name",
  556. (const char *) m2d->dev_name,
  557. m2d->dev_name_len) &&
  558. wpa_dbus_dict_append_byte_array(&dict_iter, "primary_dev_type",
  559. (const char *)
  560. m2d->primary_dev_type, 8) &&
  561. wpa_dbus_dict_append_uint16(&dict_iter, "config_error",
  562. m2d->config_error) &&
  563. wpa_dbus_dict_append_uint16(&dict_iter, "dev_password_id",
  564. m2d->dev_password_id) &&
  565. wpa_dbus_dict_close_write(&iter, &dict_iter))) {
  566. wpa_printf(MSG_ERROR, "wpas_dbus_signal_wps_event_m2d[dbus]: "
  567. "out of memory");
  568. goto out;
  569. }
  570. dbus_connection_send(iface->con, _signal, NULL);
  571. out:
  572. dbus_message_unref(_signal);
  573. }
  574. /**
  575. * wpas_dbus_signal_wps_cred - Signals new credentials
  576. * @wpa_s: %wpa_supplicant network interface data
  577. *
  578. * Sends signal with credentials in directory argument
  579. */
  580. void wpas_dbus_signal_wps_cred(struct wpa_supplicant *wpa_s,
  581. const struct wps_credential *cred)
  582. {
  583. DBusMessage *_signal = NULL;
  584. DBusMessageIter iter, dict_iter;
  585. struct wpas_dbus_priv *iface;
  586. char *auth_type[6]; /* we have six possible authorization types */
  587. int at_num = 0;
  588. char *encr_type[4]; /* we have four possible encryption types */
  589. int et_num = 0;
  590. iface = wpa_s->global->dbus;
  591. /* Do nothing if the control interface is not turned on */
  592. if (iface == NULL)
  593. return;
  594. _signal = dbus_message_new_signal(wpa_s->dbus_new_path,
  595. WPAS_DBUS_NEW_IFACE_WPS,
  596. "Credentials");
  597. if (!_signal)
  598. return;
  599. dbus_message_iter_init_append(_signal, &iter);
  600. if (!wpa_dbus_dict_open_write(&iter, &dict_iter))
  601. goto nomem;
  602. if (cred->auth_type & WPS_AUTH_OPEN)
  603. auth_type[at_num++] = "open";
  604. if (cred->auth_type & WPS_AUTH_WPAPSK)
  605. auth_type[at_num++] = "wpa-psk";
  606. if (cred->auth_type & WPS_AUTH_SHARED)
  607. auth_type[at_num++] = "shared";
  608. if (cred->auth_type & WPS_AUTH_WPA)
  609. auth_type[at_num++] = "wpa-eap";
  610. if (cred->auth_type & WPS_AUTH_WPA2)
  611. auth_type[at_num++] = "wpa2-eap";
  612. if (cred->auth_type & WPS_AUTH_WPA2PSK)
  613. auth_type[at_num++] =
  614. "wpa2-psk";
  615. if (cred->encr_type & WPS_ENCR_NONE)
  616. encr_type[et_num++] = "none";
  617. if (cred->encr_type & WPS_ENCR_WEP)
  618. encr_type[et_num++] = "wep";
  619. if (cred->encr_type & WPS_ENCR_TKIP)
  620. encr_type[et_num++] = "tkip";
  621. if (cred->encr_type & WPS_ENCR_AES)
  622. encr_type[et_num++] = "aes";
  623. if (wpa_s->current_ssid) {
  624. if (!wpa_dbus_dict_append_byte_array(
  625. &dict_iter, "BSSID",
  626. (const char *) wpa_s->current_ssid->bssid,
  627. ETH_ALEN))
  628. goto nomem;
  629. }
  630. if (!(wpa_dbus_dict_append_byte_array(&dict_iter, "SSID",
  631. (const char *) cred->ssid,
  632. cred->ssid_len) &&
  633. wpa_dbus_dict_append_string_array(&dict_iter, "AuthType",
  634. (const char **) auth_type,
  635. at_num) &&
  636. wpa_dbus_dict_append_string_array(&dict_iter, "EncrType",
  637. (const char **) encr_type,
  638. et_num) &&
  639. wpa_dbus_dict_append_byte_array(&dict_iter, "Key",
  640. (const char *) cred->key,
  641. cred->key_len) &&
  642. wpa_dbus_dict_append_uint32(&dict_iter, "KeyIndex",
  643. cred->key_idx)))
  644. goto nomem;
  645. if (!wpa_dbus_dict_close_write(&iter, &dict_iter))
  646. goto nomem;
  647. dbus_connection_send(iface->con, _signal, NULL);
  648. nomem:
  649. dbus_message_unref(_signal);
  650. }
  651. #endif /* CONFIG_WPS */
  652. /**
  653. * wpas_dbus_signal_prop_changed - Signals change of property
  654. * @wpa_s: %wpa_supplicant network interface data
  655. * @property: indicates which property has changed
  656. *
  657. * Sends ProertyChanged signals with path, interface and arguments
  658. * depending on which property has changed.
  659. */
  660. void wpas_dbus_signal_prop_changed(struct wpa_supplicant *wpa_s,
  661. enum wpas_dbus_prop property)
  662. {
  663. WPADBusPropertyAccessor getter;
  664. char *iface;
  665. char *prop;
  666. void *arg;
  667. switch (property) {
  668. case WPAS_DBUS_PROP_AP_SCAN:
  669. getter = (WPADBusPropertyAccessor) wpas_dbus_getter_ap_scan;
  670. arg = wpa_s;
  671. iface = WPAS_DBUS_NEW_IFACE_INTERFACE;
  672. prop = "ApScan";
  673. break;
  674. case WPAS_DBUS_PROP_SCANNING:
  675. getter = (WPADBusPropertyAccessor) wpas_dbus_getter_scanning;
  676. arg = wpa_s;
  677. iface = WPAS_DBUS_NEW_IFACE_INTERFACE;
  678. prop = "Scanning";
  679. break;
  680. case WPAS_DBUS_PROP_CURRENT_BSS:
  681. getter = (WPADBusPropertyAccessor)
  682. wpas_dbus_getter_current_bss;
  683. arg = wpa_s;
  684. iface = WPAS_DBUS_NEW_IFACE_INTERFACE;
  685. prop = "CurrentBSS";
  686. break;
  687. case WPAS_DBUS_PROP_CURRENT_NETWORK:
  688. getter = (WPADBusPropertyAccessor)
  689. wpas_dbus_getter_current_network;
  690. arg = wpa_s;
  691. iface = WPAS_DBUS_NEW_IFACE_INTERFACE;
  692. prop = "CurrentNetwork";
  693. break;
  694. default:
  695. wpa_printf(MSG_ERROR, "wpas_dbus_signal_prop_changed[dbus]: "
  696. "Unknown Property enum value %d", property);
  697. return;
  698. }
  699. wpa_dbus_signal_property_changed(wpa_s->global->dbus,
  700. getter, arg, wpa_s->dbus_new_path,
  701. iface, prop);
  702. }
  703. /**
  704. * wpas_dbus_signal_debug_level_changed - Signals change of debug param
  705. * @global: wpa_global structure
  706. *
  707. * Sends ProertyChanged signals informing that debug level has changed.
  708. */
  709. void wpas_dbus_signal_debug_level_changed(struct wpa_global *global)
  710. {
  711. wpa_dbus_signal_property_changed(global->dbus,
  712. (WPADBusPropertyAccessor)
  713. wpas_dbus_getter_debug_level,
  714. global, WPAS_DBUS_NEW_PATH,
  715. WPAS_DBUS_NEW_INTERFACE,
  716. "DebugLevel");
  717. }
  718. /**
  719. * wpas_dbus_signal_debug_timestamp_changed - Signals change of debug param
  720. * @global: wpa_global structure
  721. *
  722. * Sends ProertyChanged signals informing that debug timestamp has changed.
  723. */
  724. void wpas_dbus_signal_debug_timestamp_changed(struct wpa_global *global)
  725. {
  726. wpa_dbus_signal_property_changed(global->dbus,
  727. (WPADBusPropertyAccessor)
  728. wpas_dbus_getter_debug_timestamp,
  729. global, WPAS_DBUS_NEW_PATH,
  730. WPAS_DBUS_NEW_INTERFACE,
  731. "DebugTimestamp");
  732. }
  733. /**
  734. * wpas_dbus_signal_debug_show_keys_changed - Signals change of debug param
  735. * @global: wpa_global structure
  736. *
  737. * Sends ProertyChanged signals informing that debug show_keys has changed.
  738. */
  739. void wpas_dbus_signal_debug_show_keys_changed(struct wpa_global *global)
  740. {
  741. wpa_dbus_signal_property_changed(global->dbus,
  742. (WPADBusPropertyAccessor)
  743. wpas_dbus_getter_debug_show_keys,
  744. global, WPAS_DBUS_NEW_PATH,
  745. WPAS_DBUS_NEW_INTERFACE,
  746. "DebugShowKeys");
  747. }
  748. struct wpas_dbus_method {
  749. const char *name;
  750. const char *iface;
  751. WPADBusMethodHandler handler;
  752. struct wpa_dbus_argument args[3];
  753. };
  754. struct wpas_dbus_property {
  755. const char *name;
  756. const char *iface;
  757. const char *type;
  758. WPADBusPropertyAccessor getter;
  759. WPADBusPropertyAccessor setter;
  760. enum dbus_prop_access _access;
  761. };
  762. struct wpas_dbus_signal {
  763. const char *name;
  764. const char *iface;
  765. struct wpa_dbus_argument args[3];
  766. };
  767. static void wpas_dbus_register(struct wpa_dbus_object_desc *obj_desc,
  768. void *priv,
  769. WPADBusArgumentFreeFunction priv_free,
  770. const struct wpas_dbus_method *methods,
  771. const struct wpas_dbus_property *properties,
  772. const struct wpas_dbus_signal *signals)
  773. {
  774. int i;
  775. obj_desc->user_data = priv;
  776. obj_desc->user_data_free_func = priv_free;
  777. for (i = 0; methods && methods[i].name; i++) {
  778. wpa_dbus_method_register(obj_desc, methods[i].iface,
  779. methods[i].name, methods[i].handler,
  780. methods[i].args);
  781. }
  782. for (i = 0; properties && properties[i].name; i++) {
  783. wpa_dbus_property_register(obj_desc, properties[i].iface,
  784. properties[i].name,
  785. properties[i].type,
  786. properties[i].getter,
  787. properties[i].setter,
  788. properties[i]._access);
  789. }
  790. for (i = 0; signals && signals[i].name; i++) {
  791. wpa_dbus_signal_register(obj_desc, signals[i].iface,
  792. signals[i].name, signals[i].args);
  793. }
  794. }
  795. static const struct wpas_dbus_method wpas_dbus_global_methods[] = {
  796. { "CreateInterface", WPAS_DBUS_NEW_INTERFACE,
  797. (WPADBusMethodHandler) &wpas_dbus_handler_create_interface,
  798. {
  799. { "args", "a{sv}", ARG_IN },
  800. { "path", "o", ARG_OUT },
  801. END_ARGS
  802. }
  803. },
  804. { "RemoveInterface", WPAS_DBUS_NEW_INTERFACE,
  805. (WPADBusMethodHandler) &wpas_dbus_handler_remove_interface,
  806. {
  807. { "path", "o", ARG_IN },
  808. END_ARGS
  809. }
  810. },
  811. { "GetInterface", WPAS_DBUS_NEW_INTERFACE,
  812. (WPADBusMethodHandler) &wpas_dbus_handler_get_interface,
  813. {
  814. { "ifname", "s", ARG_IN },
  815. { "path", "o", ARG_OUT },
  816. END_ARGS
  817. }
  818. },
  819. { NULL, NULL, NULL, { END_ARGS } }
  820. };
  821. static const struct wpas_dbus_property wpas_dbus_global_properties[] = {
  822. { "DebugLevel", WPAS_DBUS_NEW_INTERFACE, "y",
  823. (WPADBusPropertyAccessor) wpas_dbus_getter_debug_level,
  824. (WPADBusPropertyAccessor) wpas_dbus_setter_debug_level,
  825. RW
  826. },
  827. { "DebugTimestamp", WPAS_DBUS_NEW_INTERFACE, "b",
  828. (WPADBusPropertyAccessor) wpas_dbus_getter_debug_timestamp,
  829. (WPADBusPropertyAccessor) wpas_dbus_setter_debug_timestamp,
  830. RW
  831. },
  832. { "DebugShowKeys", WPAS_DBUS_NEW_INTERFACE, "b",
  833. (WPADBusPropertyAccessor) wpas_dbus_getter_debug_show_keys,
  834. (WPADBusPropertyAccessor) wpas_dbus_setter_debug_show_keys,
  835. RW
  836. },
  837. { "Interfaces", WPAS_DBUS_NEW_INTERFACE, "ao",
  838. (WPADBusPropertyAccessor) &wpas_dbus_getter_interfaces,
  839. NULL,
  840. R
  841. },
  842. { "EapMethods", WPAS_DBUS_NEW_INTERFACE, "as",
  843. (WPADBusPropertyAccessor) wpas_dbus_getter_eap_methods,
  844. NULL,
  845. R
  846. },
  847. { NULL, NULL, NULL, NULL, NULL, 0 }
  848. };
  849. static const struct wpas_dbus_signal wpas_dbus_global_signals[] = {
  850. { "InterfaceAdded", WPAS_DBUS_NEW_INTERFACE,
  851. {
  852. { "path", "o", ARG_OUT },
  853. { "properties", "a{sv}", ARG_OUT },
  854. END_ARGS
  855. }
  856. },
  857. { "InterfaceRemoved", WPAS_DBUS_NEW_INTERFACE,
  858. {
  859. { "path", "o", ARG_OUT },
  860. END_ARGS
  861. }
  862. },
  863. { "PropertiesChanged", WPAS_DBUS_NEW_INTERFACE,
  864. {
  865. { "properties", "a{sv}", ARG_OUT },
  866. END_ARGS
  867. }
  868. },
  869. { NULL, NULL, { END_ARGS } }
  870. };
  871. /**
  872. * wpas_dbus_ctrl_iface_init - Initialize dbus control interface
  873. * @global: Pointer to global data from wpa_supplicant_init()
  874. * Returns: 0 on success or -1 on failure
  875. *
  876. * Initialize the dbus control interface for wpa_supplicantand and start
  877. * receiving commands from external programs over the bus.
  878. */
  879. int wpas_dbus_ctrl_iface_init(struct wpas_dbus_priv *priv)
  880. {
  881. struct wpa_dbus_object_desc *obj_desc;
  882. int ret;
  883. obj_desc = os_zalloc(sizeof(struct wpa_dbus_object_desc));
  884. if (!obj_desc) {
  885. wpa_printf(MSG_ERROR, "Not enough memory "
  886. "to create object description");
  887. return -1;
  888. }
  889. wpas_dbus_register(obj_desc, priv->global, NULL,
  890. wpas_dbus_global_methods,
  891. wpas_dbus_global_properties,
  892. wpas_dbus_global_signals);
  893. wpa_printf(MSG_DEBUG, "dbus: Register D-Bus object '%s'",
  894. WPAS_DBUS_NEW_PATH);
  895. ret = wpa_dbus_ctrl_iface_init(priv, WPAS_DBUS_NEW_PATH,
  896. WPAS_DBUS_NEW_SERVICE,
  897. obj_desc);
  898. if (ret < 0)
  899. free_dbus_object_desc(obj_desc);
  900. else
  901. priv->dbus_new_initialized = 1;
  902. return ret;
  903. }
  904. /**
  905. * wpas_dbus_ctrl_iface_deinit - Deinitialize dbus ctrl interface for
  906. * wpa_supplicant
  907. * @iface: Pointer to dbus private data from wpas_dbus_init()
  908. *
  909. * Deinitialize the dbus control interface that was initialized with
  910. * wpas_dbus_ctrl_iface_init().
  911. */
  912. void wpas_dbus_ctrl_iface_deinit(struct wpas_dbus_priv *iface)
  913. {
  914. if (!iface->dbus_new_initialized)
  915. return;
  916. wpa_printf(MSG_DEBUG, "dbus: Unregister D-Bus object '%s'",
  917. WPAS_DBUS_NEW_PATH);
  918. dbus_connection_unregister_object_path(iface->con,
  919. WPAS_DBUS_NEW_PATH);
  920. }
  921. static void wpa_dbus_free(void *ptr)
  922. {
  923. os_free(ptr);
  924. }
  925. static const struct wpas_dbus_property wpas_dbus_network_properties[] = {
  926. { "Properties", WPAS_DBUS_NEW_IFACE_NETWORK, "a{sv}",
  927. (WPADBusPropertyAccessor) wpas_dbus_getter_network_properties,
  928. (WPADBusPropertyAccessor) wpas_dbus_setter_network_properties,
  929. RW
  930. },
  931. { "Enabled", WPAS_DBUS_NEW_IFACE_NETWORK, "b",
  932. (WPADBusPropertyAccessor) wpas_dbus_getter_enabled,
  933. (WPADBusPropertyAccessor) wpas_dbus_setter_enabled,
  934. RW
  935. },
  936. { NULL, NULL, NULL, NULL, NULL, 0 }
  937. };
  938. static const struct wpas_dbus_signal wpas_dbus_network_signals[] = {
  939. { "PropertiesChanged", WPAS_DBUS_NEW_IFACE_NETWORK,
  940. {
  941. { "properties", "a{sv}", ARG_OUT },
  942. END_ARGS
  943. }
  944. },
  945. { NULL, NULL, { END_ARGS } }
  946. };
  947. /**
  948. * wpas_dbus_register_network - Register a configured network with dbus
  949. * @wpa_s: wpa_supplicant interface structure
  950. * @ssid: network configuration data
  951. * Returns: 0 on success, -1 on failure
  952. *
  953. * Registers network representing object with dbus
  954. */
  955. int wpas_dbus_register_network(struct wpa_supplicant *wpa_s,
  956. struct wpa_ssid *ssid)
  957. {
  958. struct wpas_dbus_priv *ctrl_iface;
  959. struct wpa_dbus_object_desc *obj_desc;
  960. struct network_handler_args *arg = NULL;
  961. char *net_obj_path;
  962. /* Do nothing if the control interface is not turned on */
  963. if (wpa_s == NULL || wpa_s->global == NULL)
  964. return 0;
  965. ctrl_iface = wpa_s->global->dbus;
  966. if (ctrl_iface == NULL)
  967. return 0;
  968. net_obj_path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  969. if (net_obj_path == NULL)
  970. return -1;
  971. os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  972. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%u",
  973. wpa_s->dbus_new_path, ssid->id);
  974. wpa_printf(MSG_DEBUG, "dbus: Register network object '%s'",
  975. net_obj_path);
  976. obj_desc = os_zalloc(sizeof(struct wpa_dbus_object_desc));
  977. if (!obj_desc) {
  978. wpa_printf(MSG_ERROR, "Not enough memory "
  979. "to create object description");
  980. goto err;
  981. }
  982. /* allocate memory for handlers arguments */
  983. arg = os_zalloc(sizeof(struct network_handler_args));
  984. if (!arg) {
  985. wpa_printf(MSG_ERROR, "Not enough memory "
  986. "to create arguments for method");
  987. goto err;
  988. }
  989. arg->wpa_s = wpa_s;
  990. arg->ssid = ssid;
  991. wpas_dbus_register(obj_desc, arg, wpa_dbus_free, NULL,
  992. wpas_dbus_network_properties,
  993. wpas_dbus_network_signals);
  994. if (wpa_dbus_register_object_per_iface(ctrl_iface, net_obj_path,
  995. wpa_s->ifname, obj_desc))
  996. goto err;
  997. wpas_dbus_signal_network_added(wpa_s, ssid->id);
  998. os_free(net_obj_path);
  999. return 0;
  1000. err:
  1001. os_free(net_obj_path);
  1002. os_free(obj_desc);
  1003. os_free(arg);
  1004. return -1;
  1005. }
  1006. /**
  1007. * wpas_dbus_unregister_network - Unregister a configured network from dbus
  1008. * @wpa_s: wpa_supplicant interface structure
  1009. * @nid: network id
  1010. * Returns: 0 on success, -1 on failure
  1011. *
  1012. * Unregisters network representing object from dbus
  1013. */
  1014. int wpas_dbus_unregister_network(struct wpa_supplicant *wpa_s, int nid)
  1015. {
  1016. struct wpas_dbus_priv *ctrl_iface;
  1017. char *net_obj_path;
  1018. int ret;
  1019. /* Do nothing if the control interface is not turned on */
  1020. if (wpa_s == NULL || wpa_s->global == NULL)
  1021. return 0;
  1022. ctrl_iface = wpa_s->global->dbus;
  1023. if (ctrl_iface == NULL)
  1024. return 0;
  1025. net_obj_path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  1026. if (net_obj_path == NULL)
  1027. return -1;
  1028. os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  1029. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%u",
  1030. wpa_s->dbus_new_path, nid);
  1031. wpa_printf(MSG_DEBUG, "dbus: Unregister network object '%s'",
  1032. net_obj_path);
  1033. ret = wpa_dbus_unregister_object_per_iface(ctrl_iface, net_obj_path);
  1034. if (!ret)
  1035. wpas_dbus_signal_network_removed(wpa_s, nid);
  1036. os_free(net_obj_path);
  1037. return ret;
  1038. }
  1039. static const struct wpas_dbus_property wpas_dbus_bss_properties[] = {
  1040. { "SSID", WPAS_DBUS_NEW_IFACE_BSSID, "ay",
  1041. (WPADBusPropertyAccessor) wpas_dbus_getter_bss_ssid,
  1042. NULL,
  1043. R
  1044. },
  1045. { "BSSID", WPAS_DBUS_NEW_IFACE_BSSID, "ay",
  1046. (WPADBusPropertyAccessor) wpas_dbus_getter_bss_bssid,
  1047. NULL,
  1048. R
  1049. },
  1050. { "Privacy", WPAS_DBUS_NEW_IFACE_BSSID, "b",
  1051. (WPADBusPropertyAccessor) wpas_dbus_getter_bss_privacy,
  1052. NULL,
  1053. R
  1054. },
  1055. { "Mode", WPAS_DBUS_NEW_IFACE_BSSID, "s",
  1056. (WPADBusPropertyAccessor) wpas_dbus_getter_bss_mode,
  1057. NULL,
  1058. R
  1059. },
  1060. { "Signal", WPAS_DBUS_NEW_IFACE_BSSID, "n",
  1061. (WPADBusPropertyAccessor) wpas_dbus_getter_bss_signal,
  1062. NULL,
  1063. R
  1064. },
  1065. { "Frequency", WPAS_DBUS_NEW_IFACE_BSSID, "q",
  1066. (WPADBusPropertyAccessor) wpas_dbus_getter_bss_frequency,
  1067. NULL,
  1068. R
  1069. },
  1070. { "MaxRate", WPAS_DBUS_NEW_IFACE_BSSID, "q",
  1071. (WPADBusPropertyAccessor) wpas_dbus_getter_bss_max_rate,
  1072. NULL,
  1073. R
  1074. },
  1075. { "WPAIE", WPAS_DBUS_NEW_IFACE_BSSID, "ay",
  1076. (WPADBusPropertyAccessor) wpas_dbus_getter_bss_wpaie,
  1077. NULL,
  1078. R
  1079. },
  1080. { "RSNIE", WPAS_DBUS_NEW_IFACE_BSSID, "ay",
  1081. (WPADBusPropertyAccessor) wpas_dbus_getter_bss_rsnie,
  1082. NULL,
  1083. R
  1084. },
  1085. { "WPSIE", WPAS_DBUS_NEW_IFACE_BSSID, "ay",
  1086. (WPADBusPropertyAccessor) wpas_dbus_getter_bss_wpsie,
  1087. NULL,
  1088. R
  1089. },
  1090. { NULL, NULL, NULL, NULL, NULL, 0 }
  1091. };
  1092. static const struct wpas_dbus_signal wpas_dbus_bss_signals[] = {
  1093. { "PropertiesChanged", WPAS_DBUS_NEW_IFACE_BSSID,
  1094. {
  1095. { "properties", "a{sv}", ARG_OUT },
  1096. END_ARGS
  1097. }
  1098. },
  1099. { NULL, NULL, { END_ARGS } }
  1100. };
  1101. /**
  1102. * wpas_dbus_unregister_bss - Unregister a scanned BSS from dbus
  1103. * @wpa_s: wpa_supplicant interface structure
  1104. * @bssid: scanned network bssid
  1105. * @id: unique BSS identifier
  1106. * Returns: 0 on success, -1 on failure
  1107. *
  1108. * Unregisters BSS representing object from dbus
  1109. */
  1110. int wpas_dbus_unregister_bss(struct wpa_supplicant *wpa_s,
  1111. u8 bssid[ETH_ALEN], unsigned int id)
  1112. {
  1113. struct wpas_dbus_priv *ctrl_iface;
  1114. char *bss_obj_path;
  1115. /* Do nothing if the control interface is not turned on */
  1116. if (wpa_s == NULL || wpa_s->global == NULL)
  1117. return 0;
  1118. ctrl_iface = wpa_s->global->dbus;
  1119. if (ctrl_iface == NULL)
  1120. return 0;
  1121. bss_obj_path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  1122. if (bss_obj_path == NULL)
  1123. return -1;
  1124. os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  1125. "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
  1126. wpa_s->dbus_new_path, id);
  1127. wpa_printf(MSG_DEBUG, "dbus: Unregister BSS object '%s'",
  1128. bss_obj_path);
  1129. if (wpa_dbus_unregister_object_per_iface(ctrl_iface, bss_obj_path)) {
  1130. wpa_printf(MSG_ERROR,
  1131. "Cannot unregister BSSID dbus object %s.",
  1132. bss_obj_path);
  1133. os_free(bss_obj_path);
  1134. return -1;
  1135. }
  1136. wpas_dbus_signal_bss_removed(wpa_s, bss_obj_path);
  1137. os_free(bss_obj_path);
  1138. return 0;
  1139. }
  1140. /**
  1141. * wpas_dbus_register_bss - Register a scanned BSS with dbus
  1142. * @wpa_s: wpa_supplicant interface structure
  1143. * @bssid: scanned network bssid
  1144. * @id: unique BSS identifier
  1145. * Returns: 0 on success, -1 on failure
  1146. *
  1147. * Registers BSS representing object with dbus
  1148. */
  1149. int wpas_dbus_register_bss(struct wpa_supplicant *wpa_s,
  1150. u8 bssid[ETH_ALEN], unsigned int id)
  1151. {
  1152. struct wpas_dbus_priv *ctrl_iface;
  1153. struct wpa_dbus_object_desc *obj_desc;
  1154. char *bss_obj_path;
  1155. struct bss_handler_args *arg = NULL;
  1156. /* Do nothing if the control interface is not turned on */
  1157. if (wpa_s == NULL || wpa_s->global == NULL)
  1158. return 0;
  1159. ctrl_iface = wpa_s->global->dbus;
  1160. if (ctrl_iface == NULL)
  1161. return 0;
  1162. bss_obj_path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  1163. if (bss_obj_path == NULL)
  1164. return -1;
  1165. os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  1166. "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
  1167. wpa_s->dbus_new_path, id);
  1168. obj_desc = os_zalloc(sizeof(struct wpa_dbus_object_desc));
  1169. if (!obj_desc) {
  1170. wpa_printf(MSG_ERROR, "Not enough memory "
  1171. "to create object description");
  1172. goto err;
  1173. }
  1174. arg = os_zalloc(sizeof(struct bss_handler_args));
  1175. if (!arg) {
  1176. wpa_printf(MSG_ERROR, "Not enough memory "
  1177. "to create arguments for handler");
  1178. goto err;
  1179. }
  1180. arg->wpa_s = wpa_s;
  1181. arg->id = id;
  1182. wpas_dbus_register(obj_desc, arg, wpa_dbus_free, NULL,
  1183. wpas_dbus_bss_properties,
  1184. wpas_dbus_bss_signals);
  1185. wpa_printf(MSG_DEBUG, "dbus: Register BSS object '%s'",
  1186. bss_obj_path);
  1187. if (wpa_dbus_register_object_per_iface(ctrl_iface, bss_obj_path,
  1188. wpa_s->ifname, obj_desc)) {
  1189. wpa_printf(MSG_ERROR,
  1190. "Cannot register BSSID dbus object %s.",
  1191. bss_obj_path);
  1192. goto err;
  1193. }
  1194. wpas_dbus_signal_bss_added(wpa_s, bss_obj_path);
  1195. os_free(bss_obj_path);
  1196. return 0;
  1197. err:
  1198. os_free(bss_obj_path);
  1199. os_free(obj_desc);
  1200. os_free(arg);
  1201. return -1;
  1202. }
  1203. static const struct wpas_dbus_method wpas_dbus_interface_methods[] = {
  1204. { "Scan", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1205. (WPADBusMethodHandler) &wpas_dbus_handler_scan,
  1206. {
  1207. { "args", "a{sv}", ARG_IN },
  1208. END_ARGS
  1209. }
  1210. },
  1211. { "Disconnect", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1212. (WPADBusMethodHandler) &wpas_dbus_handler_disconnect,
  1213. {
  1214. END_ARGS
  1215. }
  1216. },
  1217. { "AddNetwork", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1218. (WPADBusMethodHandler) &wpas_dbus_handler_add_network,
  1219. {
  1220. { "args", "a{sv}", ARG_IN },
  1221. { "path", "o", ARG_OUT },
  1222. END_ARGS
  1223. }
  1224. },
  1225. { "RemoveNetwork", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1226. (WPADBusMethodHandler) &wpas_dbus_handler_remove_network,
  1227. {
  1228. { "path", "o", ARG_IN },
  1229. END_ARGS
  1230. }
  1231. },
  1232. { "SelectNetwork", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1233. (WPADBusMethodHandler) &wpas_dbus_handler_select_network,
  1234. {
  1235. { "path", "o", ARG_IN },
  1236. END_ARGS
  1237. }
  1238. },
  1239. { "AddBlob", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1240. (WPADBusMethodHandler) &wpas_dbus_handler_add_blob,
  1241. {
  1242. { "name", "s", ARG_IN },
  1243. { "data", "ay", ARG_IN },
  1244. END_ARGS
  1245. }
  1246. },
  1247. { "GetBlob", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1248. (WPADBusMethodHandler) &wpas_dbus_handler_get_blob,
  1249. {
  1250. { "name", "s", ARG_IN },
  1251. { "data", "ay", ARG_OUT },
  1252. END_ARGS
  1253. }
  1254. },
  1255. { "RemoveBlob", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1256. (WPADBusMethodHandler) &wpas_dbus_handler_remove_blob,
  1257. {
  1258. { "name", "s", ARG_IN },
  1259. END_ARGS
  1260. }
  1261. },
  1262. #ifdef CONFIG_WPS
  1263. { "Start", WPAS_DBUS_NEW_IFACE_WPS,
  1264. (WPADBusMethodHandler) &wpas_dbus_handler_wps_start,
  1265. {
  1266. { "args", "a{sv}", ARG_IN },
  1267. { "output", "a{sv}", ARG_OUT },
  1268. END_ARGS
  1269. }
  1270. },
  1271. #endif /* CONFIG_WPS */
  1272. { NULL, NULL, NULL, { END_ARGS } }
  1273. };
  1274. static const struct wpas_dbus_property wpas_dbus_interface_properties[] = {
  1275. { "Capabilities", WPAS_DBUS_NEW_IFACE_INTERFACE, "a{sv}",
  1276. (WPADBusPropertyAccessor) wpas_dbus_getter_capabilities,
  1277. NULL, R
  1278. },
  1279. { "State", WPAS_DBUS_NEW_IFACE_INTERFACE, "s",
  1280. (WPADBusPropertyAccessor) wpas_dbus_getter_state,
  1281. NULL, R
  1282. },
  1283. { "Scanning", WPAS_DBUS_NEW_IFACE_INTERFACE, "b",
  1284. (WPADBusPropertyAccessor) wpas_dbus_getter_scanning,
  1285. NULL, R
  1286. },
  1287. { "ApScan", WPAS_DBUS_NEW_IFACE_INTERFACE, "u",
  1288. (WPADBusPropertyAccessor) wpas_dbus_getter_ap_scan,
  1289. (WPADBusPropertyAccessor) wpas_dbus_setter_ap_scan,
  1290. RW
  1291. },
  1292. { "Ifname", WPAS_DBUS_NEW_IFACE_INTERFACE, "s",
  1293. (WPADBusPropertyAccessor) wpas_dbus_getter_ifname,
  1294. NULL, R
  1295. },
  1296. { "Driver", WPAS_DBUS_NEW_IFACE_INTERFACE, "s",
  1297. (WPADBusPropertyAccessor) wpas_dbus_getter_driver,
  1298. NULL, R
  1299. },
  1300. { "BridgeIfname", WPAS_DBUS_NEW_IFACE_INTERFACE, "s",
  1301. (WPADBusPropertyAccessor) wpas_dbus_getter_bridge_ifname,
  1302. NULL, R
  1303. },
  1304. { "CurrentBSS", WPAS_DBUS_NEW_IFACE_INTERFACE, "o",
  1305. (WPADBusPropertyAccessor) wpas_dbus_getter_current_bss,
  1306. NULL, R
  1307. },
  1308. { "CurrentNetwork", WPAS_DBUS_NEW_IFACE_INTERFACE, "o",
  1309. (WPADBusPropertyAccessor) wpas_dbus_getter_current_network,
  1310. NULL, R
  1311. },
  1312. { "Blobs", WPAS_DBUS_NEW_IFACE_INTERFACE, "a{say}",
  1313. (WPADBusPropertyAccessor) wpas_dbus_getter_blobs,
  1314. NULL, R
  1315. },
  1316. { "BSSs", WPAS_DBUS_NEW_IFACE_INTERFACE, "ao",
  1317. (WPADBusPropertyAccessor) wpas_dbus_getter_bsss,
  1318. NULL, R
  1319. },
  1320. { "Networks", WPAS_DBUS_NEW_IFACE_INTERFACE, "ao",
  1321. (WPADBusPropertyAccessor) wpas_dbus_getter_networks,
  1322. NULL, R
  1323. },
  1324. #ifdef CONFIG_WPS
  1325. { "ProcessCredentials", WPAS_DBUS_NEW_IFACE_WPS, "b",
  1326. (WPADBusPropertyAccessor) wpas_dbus_getter_process_credentials,
  1327. (WPADBusPropertyAccessor) wpas_dbus_setter_process_credentials,
  1328. RW
  1329. },
  1330. #endif /* CONFIG_WPS */
  1331. { NULL, NULL, NULL, NULL, NULL, 0 }
  1332. };
  1333. static const struct wpas_dbus_signal wpas_dbus_interface_signals[] = {
  1334. { "ScanDone", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1335. {
  1336. { "success", "b", ARG_OUT },
  1337. END_ARGS
  1338. }
  1339. },
  1340. { "StateChanged", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1341. {
  1342. { "newState", "s", ARG_OUT },
  1343. { "oldState", "s", ARG_OUT },
  1344. END_ARGS
  1345. }
  1346. },
  1347. { "BSSAdded", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1348. {
  1349. { "path", "o", ARG_OUT },
  1350. { "properties", "a{sv}", ARG_OUT },
  1351. END_ARGS
  1352. }
  1353. },
  1354. { "BSSRemoved", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1355. {
  1356. { "path", "o", ARG_OUT },
  1357. END_ARGS
  1358. }
  1359. },
  1360. { "BlobAdded", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1361. {
  1362. { "name", "s", ARG_OUT },
  1363. END_ARGS
  1364. }
  1365. },
  1366. { "BlobRemoved", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1367. {
  1368. { "name", "s", ARG_OUT },
  1369. END_ARGS
  1370. }
  1371. },
  1372. { "NetworkAdded", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1373. {
  1374. { "path", "o", ARG_OUT },
  1375. { "properties", "a{sv}", ARG_OUT },
  1376. END_ARGS
  1377. }
  1378. },
  1379. { "NetworkRemoved", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1380. {
  1381. { "path", "o", ARG_OUT },
  1382. END_ARGS
  1383. }
  1384. },
  1385. { "NetworkSelected", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1386. {
  1387. { "path", "o", ARG_OUT },
  1388. END_ARGS
  1389. }
  1390. },
  1391. { "PropertiesChanged", WPAS_DBUS_NEW_IFACE_INTERFACE,
  1392. {
  1393. { "properties", "a{sv}", ARG_OUT },
  1394. END_ARGS
  1395. }
  1396. },
  1397. #ifdef CONFIG_WPS
  1398. { "Event", WPAS_DBUS_NEW_IFACE_WPS,
  1399. {
  1400. { "name", "s", ARG_OUT },
  1401. { "args", "a{sv}", ARG_OUT },
  1402. END_ARGS
  1403. }
  1404. },
  1405. { "Credentials", WPAS_DBUS_NEW_IFACE_WPS,
  1406. {
  1407. { "credentials", "a{sv}", ARG_OUT },
  1408. END_ARGS
  1409. }
  1410. },
  1411. { "PropertiesChanged", WPAS_DBUS_NEW_IFACE_WPS,
  1412. {
  1413. { "properties", "a{sv}", ARG_OUT },
  1414. END_ARGS
  1415. }
  1416. },
  1417. #endif /* CONFIG_WPS */
  1418. { NULL, NULL, { END_ARGS } }
  1419. };
  1420. int wpas_dbus_register_interface(struct wpa_supplicant *wpa_s)
  1421. {
  1422. struct wpa_dbus_object_desc *obj_desc = NULL;
  1423. struct wpas_dbus_priv *ctrl_iface = wpa_s->global->dbus;
  1424. int next;
  1425. /* Do nothing if the control interface is not turned on */
  1426. if (ctrl_iface == NULL)
  1427. return 0;
  1428. /* Create and set the interface's object path */
  1429. wpa_s->dbus_new_path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  1430. if (wpa_s->dbus_new_path == NULL)
  1431. return -1;
  1432. next = ctrl_iface->next_objid++;
  1433. os_snprintf(wpa_s->dbus_new_path, WPAS_DBUS_OBJECT_PATH_MAX,
  1434. WPAS_DBUS_NEW_PATH_INTERFACES "/%u",
  1435. next);
  1436. obj_desc = os_zalloc(sizeof(struct wpa_dbus_object_desc));
  1437. if (!obj_desc) {
  1438. wpa_printf(MSG_ERROR, "Not enough memory "
  1439. "to create object description");
  1440. goto err;
  1441. }
  1442. wpas_dbus_register(obj_desc, wpa_s, NULL, wpas_dbus_interface_methods,
  1443. wpas_dbus_interface_properties,
  1444. wpas_dbus_interface_signals);
  1445. wpa_printf(MSG_DEBUG, "dbus: Register interface object '%s'",
  1446. wpa_s->dbus_new_path);
  1447. if (wpa_dbus_register_object_per_iface(ctrl_iface,
  1448. wpa_s->dbus_new_path,
  1449. wpa_s->ifname, obj_desc))
  1450. goto err;
  1451. wpas_dbus_signal_interface_added(wpa_s);
  1452. return 0;
  1453. err:
  1454. os_free(wpa_s->dbus_new_path);
  1455. wpa_s->dbus_new_path = NULL;
  1456. os_free(obj_desc);
  1457. return -1;
  1458. }
  1459. int wpas_dbus_unregister_interface(struct wpa_supplicant *wpa_s)
  1460. {
  1461. struct wpas_dbus_priv *ctrl_iface;
  1462. /* Do nothing if the control interface is not turned on */
  1463. if (wpa_s == NULL || wpa_s->global == NULL)
  1464. return 0;
  1465. ctrl_iface = wpa_s->global->dbus;
  1466. if (ctrl_iface == NULL)
  1467. return 0;
  1468. wpa_printf(MSG_DEBUG, "dbus: Unregister interface object '%s'",
  1469. wpa_s->dbus_new_path);
  1470. if (wpa_dbus_unregister_object_per_iface(ctrl_iface,
  1471. wpa_s->dbus_new_path))
  1472. return -1;
  1473. wpas_dbus_signal_interface_removed(wpa_s);
  1474. os_free(wpa_s->dbus_new_path);
  1475. wpa_s->dbus_new_path = NULL;
  1476. return 0;
  1477. }