wpa_cli: Correct return value of wpa_cli_cmd_p2p_peers
[mech_eap.git] / wpa_supplicant / notify.c
1 /*
2  * wpa_supplicant - Event notifications
3  * Copyright (c) 2009-2010, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "utils/includes.h"
16
17 #include "utils/common.h"
18 #include "common/wpa_ctrl.h"
19 #include "config.h"
20 #include "wpa_supplicant_i.h"
21 #include "wps_supplicant.h"
22 #include "dbus/dbus_common.h"
23 #include "dbus/dbus_old.h"
24 #include "dbus/dbus_new.h"
25 #include "rsn_supp/wpa.h"
26 #include "driver_i.h"
27 #include "scan.h"
28 #include "p2p_supplicant.h"
29 #include "sme.h"
30 #include "notify.h"
31
32 int wpas_notify_supplicant_initialized(struct wpa_global *global)
33 {
34 #ifdef CONFIG_DBUS
35         if (global->params.dbus_ctrl_interface) {
36                 global->dbus = wpas_dbus_init(global);
37                 if (global->dbus == NULL)
38                         return -1;
39         }
40 #endif /* CONFIG_DBUS */
41
42         return 0;
43 }
44
45
46 void wpas_notify_supplicant_deinitialized(struct wpa_global *global)
47 {
48 #ifdef CONFIG_DBUS
49         if (global->dbus)
50                 wpas_dbus_deinit(global->dbus);
51 #endif /* CONFIG_DBUS */
52 }
53
54
55 int wpas_notify_iface_added(struct wpa_supplicant *wpa_s)
56 {
57         if (wpas_dbus_register_iface(wpa_s))
58                 return -1;
59
60         if (wpas_dbus_register_interface(wpa_s))
61                 return -1;
62
63         return 0;
64 }
65
66
67 void wpas_notify_iface_removed(struct wpa_supplicant *wpa_s)
68 {
69         /* unregister interface in old DBus ctrl iface */
70         wpas_dbus_unregister_iface(wpa_s);
71
72         /* unregister interface in new DBus ctrl iface */
73         wpas_dbus_unregister_interface(wpa_s);
74 }
75
76
77 void wpas_notify_state_changed(struct wpa_supplicant *wpa_s,
78                                enum wpa_states new_state,
79                                enum wpa_states old_state)
80 {
81         /* notify the old DBus API */
82         wpa_supplicant_dbus_notify_state_change(wpa_s, new_state,
83                                                 old_state);
84
85         /* notify the new DBus API */
86         wpas_dbus_signal_prop_changed(wpa_s, WPAS_DBUS_PROP_STATE);
87
88 #ifdef CONFIG_P2P
89         if (new_state == WPA_COMPLETED)
90                 wpas_p2p_notif_connected(wpa_s);
91         else if (new_state < WPA_ASSOCIATED)
92                 wpas_p2p_notif_disconnected(wpa_s);
93 #endif /* CONFIG_P2P */
94
95         sme_state_changed(wpa_s);
96
97 #ifdef ANDROID
98         wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
99                      "id=%d state=%d BSSID=" MACSTR,
100                      wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
101                      new_state, MAC2STR(wpa_s->pending_bssid));
102 #endif /* ANDROID */
103 }
104
105
106 void wpas_notify_network_changed(struct wpa_supplicant *wpa_s)
107 {
108         wpas_dbus_signal_prop_changed(wpa_s, WPAS_DBUS_PROP_CURRENT_NETWORK);
109 }
110
111
112 void wpas_notify_ap_scan_changed(struct wpa_supplicant *wpa_s)
113 {
114         wpas_dbus_signal_prop_changed(wpa_s, WPAS_DBUS_PROP_AP_SCAN);
115 }
116
117
118 void wpas_notify_bssid_changed(struct wpa_supplicant *wpa_s)
119 {
120         wpas_dbus_signal_prop_changed(wpa_s, WPAS_DBUS_PROP_CURRENT_BSS);
121 }
122
123
124 void wpas_notify_auth_changed(struct wpa_supplicant *wpa_s)
125 {
126         wpas_dbus_signal_prop_changed(wpa_s, WPAS_DBUS_PROP_CURRENT_AUTH_MODE);
127 }
128
129
130 void wpas_notify_network_enabled_changed(struct wpa_supplicant *wpa_s,
131                                          struct wpa_ssid *ssid)
132 {
133         wpas_dbus_signal_network_enabled_changed(wpa_s, ssid);
134 }
135
136
137 void wpas_notify_network_selected(struct wpa_supplicant *wpa_s,
138                                   struct wpa_ssid *ssid)
139 {
140         wpas_dbus_signal_network_selected(wpa_s, ssid->id);
141 }
142
143
144 void wpas_notify_network_request(struct wpa_supplicant *wpa_s,
145                                  struct wpa_ssid *ssid,
146                                  enum wpa_ctrl_req_type rtype,
147                                  const char *default_txt)
148 {
149         wpas_dbus_signal_network_request(wpa_s, ssid, rtype, default_txt);
150 }
151
152
153 void wpas_notify_scanning(struct wpa_supplicant *wpa_s)
154 {
155         /* notify the old DBus API */
156         wpa_supplicant_dbus_notify_scanning(wpa_s);
157
158         /* notify the new DBus API */
159         wpas_dbus_signal_prop_changed(wpa_s, WPAS_DBUS_PROP_SCANNING);
160 }
161
162
163 void wpas_notify_scan_done(struct wpa_supplicant *wpa_s, int success)
164 {
165         wpas_dbus_signal_scan_done(wpa_s, success);
166 }
167
168
169 void wpas_notify_scan_results(struct wpa_supplicant *wpa_s)
170 {
171         /* notify the old DBus API */
172         wpa_supplicant_dbus_notify_scan_results(wpa_s);
173
174         wpas_wps_notify_scan_results(wpa_s);
175 }
176
177
178 void wpas_notify_wps_credential(struct wpa_supplicant *wpa_s,
179                                 const struct wps_credential *cred)
180 {
181 #ifdef CONFIG_WPS
182         /* notify the old DBus API */
183         wpa_supplicant_dbus_notify_wps_cred(wpa_s, cred);
184         /* notify the new DBus API */
185         wpas_dbus_signal_wps_cred(wpa_s, cred);
186 #endif /* CONFIG_WPS */
187 }
188
189
190 void wpas_notify_wps_event_m2d(struct wpa_supplicant *wpa_s,
191                                struct wps_event_m2d *m2d)
192 {
193 #ifdef CONFIG_WPS
194         wpas_dbus_signal_wps_event_m2d(wpa_s, m2d);
195 #endif /* CONFIG_WPS */
196 }
197
198
199 void wpas_notify_wps_event_fail(struct wpa_supplicant *wpa_s,
200                                 struct wps_event_fail *fail)
201 {
202 #ifdef CONFIG_WPS
203         wpas_dbus_signal_wps_event_fail(wpa_s, fail);
204 #endif /* CONFIG_WPS */
205 }
206
207
208 void wpas_notify_wps_event_success(struct wpa_supplicant *wpa_s)
209 {
210 #ifdef CONFIG_WPS
211         wpas_dbus_signal_wps_event_success(wpa_s);
212 #endif /* CONFIG_WPS */
213 }
214
215
216 void wpas_notify_network_added(struct wpa_supplicant *wpa_s,
217                                struct wpa_ssid *ssid)
218 {
219         /*
220          * Networks objects created during any P2P activities should not be
221          * exposed out. They might/will confuse certain non-P2P aware
222          * applications since these network objects won't behave like
223          * regular ones.
224          */
225         if (wpa_s->global->p2p_group_formation != wpa_s)
226                 wpas_dbus_register_network(wpa_s, ssid);
227 }
228
229
230 void wpas_notify_persistent_group_added(struct wpa_supplicant *wpa_s,
231                                         struct wpa_ssid *ssid)
232 {
233 #ifdef CONFIG_P2P
234         wpas_dbus_register_persistent_group(wpa_s, ssid);
235 #endif /* CONFIG_P2P */
236 }
237
238
239 void wpas_notify_persistent_group_removed(struct wpa_supplicant *wpa_s,
240                                           struct wpa_ssid *ssid)
241 {
242 #ifdef CONFIG_P2P
243         wpas_dbus_unregister_persistent_group(wpa_s, ssid->id);
244 #endif /* CONFIG_P2P */
245 }
246
247
248 void wpas_notify_network_removed(struct wpa_supplicant *wpa_s,
249                                  struct wpa_ssid *ssid)
250 {
251         if (wpa_s->wpa)
252                 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
253         if (wpa_s->global->p2p_group_formation != wpa_s)
254                 wpas_dbus_unregister_network(wpa_s, ssid->id);
255 }
256
257
258 void wpas_notify_bss_added(struct wpa_supplicant *wpa_s,
259                            u8 bssid[], unsigned int id)
260 {
261         wpas_dbus_register_bss(wpa_s, bssid, id);
262         wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_BSS_ADDED "%u " MACSTR,
263                      id, MAC2STR(bssid));
264 }
265
266
267 void wpas_notify_bss_removed(struct wpa_supplicant *wpa_s,
268                              u8 bssid[], unsigned int id)
269 {
270         wpas_dbus_unregister_bss(wpa_s, bssid, id);
271         wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_BSS_REMOVED "%u " MACSTR,
272                      id, MAC2STR(bssid));
273 }
274
275
276 void wpas_notify_bss_freq_changed(struct wpa_supplicant *wpa_s,
277                                   unsigned int id)
278 {
279         wpas_dbus_bss_signal_prop_changed(wpa_s, WPAS_DBUS_BSS_PROP_FREQ, id);
280 }
281
282
283 void wpas_notify_bss_signal_changed(struct wpa_supplicant *wpa_s,
284                                     unsigned int id)
285 {
286         wpas_dbus_bss_signal_prop_changed(wpa_s, WPAS_DBUS_BSS_PROP_SIGNAL,
287                                           id);
288 }
289
290
291 void wpas_notify_bss_privacy_changed(struct wpa_supplicant *wpa_s,
292                                      unsigned int id)
293 {
294         wpas_dbus_bss_signal_prop_changed(wpa_s, WPAS_DBUS_BSS_PROP_PRIVACY,
295                                           id);
296 }
297
298
299 void wpas_notify_bss_mode_changed(struct wpa_supplicant *wpa_s,
300                                   unsigned int id)
301 {
302         wpas_dbus_bss_signal_prop_changed(wpa_s, WPAS_DBUS_BSS_PROP_MODE, id);
303 }
304
305
306 void wpas_notify_bss_wpaie_changed(struct wpa_supplicant *wpa_s,
307                                    unsigned int id)
308 {
309         wpas_dbus_bss_signal_prop_changed(wpa_s, WPAS_DBUS_BSS_PROP_WPA, id);
310 }
311
312
313 void wpas_notify_bss_rsnie_changed(struct wpa_supplicant *wpa_s,
314                                    unsigned int id)
315 {
316         wpas_dbus_bss_signal_prop_changed(wpa_s, WPAS_DBUS_BSS_PROP_RSN, id);
317 }
318
319
320 void wpas_notify_bss_wps_changed(struct wpa_supplicant *wpa_s,
321                                  unsigned int id)
322 {
323 }
324
325
326 void wpas_notify_bss_ies_changed(struct wpa_supplicant *wpa_s,
327                                    unsigned int id)
328 {
329         wpas_dbus_bss_signal_prop_changed(wpa_s, WPAS_DBUS_BSS_PROP_IES, id);
330 }
331
332
333 void wpas_notify_bss_rates_changed(struct wpa_supplicant *wpa_s,
334                                    unsigned int id)
335 {
336         wpas_dbus_bss_signal_prop_changed(wpa_s, WPAS_DBUS_BSS_PROP_RATES, id);
337 }
338
339
340 void wpas_notify_blob_added(struct wpa_supplicant *wpa_s, const char *name)
341 {
342         wpas_dbus_signal_blob_added(wpa_s, name);
343 }
344
345
346 void wpas_notify_blob_removed(struct wpa_supplicant *wpa_s, const char *name)
347 {
348         wpas_dbus_signal_blob_removed(wpa_s, name);
349 }
350
351
352 void wpas_notify_debug_level_changed(struct wpa_global *global)
353 {
354         wpas_dbus_signal_debug_level_changed(global);
355 }
356
357
358 void wpas_notify_debug_timestamp_changed(struct wpa_global *global)
359 {
360         wpas_dbus_signal_debug_timestamp_changed(global);
361 }
362
363
364 void wpas_notify_debug_show_keys_changed(struct wpa_global *global)
365 {
366         wpas_dbus_signal_debug_show_keys_changed(global);
367 }
368
369
370 void wpas_notify_suspend(struct wpa_global *global)
371 {
372         struct wpa_supplicant *wpa_s;
373
374         os_get_time(&global->suspend_time);
375         wpa_printf(MSG_DEBUG, "System suspend notification");
376         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
377                 wpa_drv_suspend(wpa_s);
378 }
379
380
381 void wpas_notify_resume(struct wpa_global *global)
382 {
383         struct os_time now;
384         int slept;
385         struct wpa_supplicant *wpa_s;
386
387         if (global->suspend_time.sec == 0)
388                 slept = -1;
389         else {
390                 os_get_time(&now);
391                 slept = now.sec - global->suspend_time.sec;
392         }
393         wpa_printf(MSG_DEBUG, "System resume notification (slept %d seconds)",
394                    slept);
395
396         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
397                 wpa_drv_resume(wpa_s);
398                 if (wpa_s->wpa_state == WPA_DISCONNECTED)
399                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
400         }
401 }
402
403
404 #ifdef CONFIG_P2P
405
406 void wpas_notify_p2p_device_found(struct wpa_supplicant *wpa_s,
407                                   const u8 *dev_addr, int new_device)
408 {
409         if (new_device) {
410                 /* Create the new peer object */
411                 wpas_dbus_register_peer(wpa_s, dev_addr);
412         }
413
414         /* Notify a new peer has been detected*/
415         wpas_dbus_signal_peer_device_found(wpa_s, dev_addr);
416 }
417
418
419 void wpas_notify_p2p_device_lost(struct wpa_supplicant *wpa_s,
420                                  const u8 *dev_addr)
421 {
422         wpas_dbus_unregister_peer(wpa_s, dev_addr);
423
424         /* Create signal on interface object*/
425         wpas_dbus_signal_peer_device_lost(wpa_s, dev_addr);
426 }
427
428
429 void wpas_notify_p2p_group_removed(struct wpa_supplicant *wpa_s,
430                                    const struct wpa_ssid *ssid,
431                                    const char *role)
432 {
433         wpas_dbus_unregister_p2p_group(wpa_s, ssid);
434
435         wpas_dbus_signal_p2p_group_removed(wpa_s, role);
436 }
437
438
439 void wpas_notify_p2p_go_neg_req(struct wpa_supplicant *wpa_s,
440                                 const u8 *src, u16 dev_passwd_id)
441 {
442         wpas_dbus_signal_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
443 }
444
445
446 void wpas_notify_p2p_go_neg_completed(struct wpa_supplicant *wpa_s, int status)
447 {
448         wpas_dbus_signal_p2p_go_neg_resp(wpa_s, status);
449 }
450
451
452 void wpas_notify_p2p_invitation_result(struct wpa_supplicant *wpa_s,
453                                        int status, const u8 *bssid)
454 {
455         wpas_dbus_signal_p2p_invitation_result(wpa_s, status, bssid);
456 }
457
458
459 void wpas_notify_p2p_sd_request(struct wpa_supplicant *wpa_s,
460                                 int freq, const u8 *sa, u8 dialog_token,
461                                 u16 update_indic, const u8 *tlvs,
462                                 size_t tlvs_len)
463 {
464         wpas_dbus_signal_p2p_sd_request(wpa_s, freq, sa, dialog_token,
465                                         update_indic, tlvs, tlvs_len);
466 }
467
468
469 void wpas_notify_p2p_sd_response(struct wpa_supplicant *wpa_s,
470                                  const u8 *sa, u16 update_indic,
471                                  const u8 *tlvs, size_t tlvs_len)
472 {
473         wpas_dbus_signal_p2p_sd_response(wpa_s, sa, update_indic,
474                                          tlvs, tlvs_len);
475 }
476
477
478 /**
479  * wpas_notify_p2p_provision_discovery - Notification of provision discovery
480  * @dev_addr: Who sent the request or responded to our request.
481  * @request: Will be 1 if request, 0 for response.
482  * @status: Valid only in case of response (0 in case of success)
483  * @config_methods: WPS config methods
484  * @generated_pin: PIN to be displayed in case of WPS_CONFIG_DISPLAY method
485  *
486  * This can be used to notify:
487  * - Requests or responses
488  * - Various config methods
489  * - Failure condition in case of response
490  */
491 void wpas_notify_p2p_provision_discovery(struct wpa_supplicant *wpa_s,
492                                          const u8 *dev_addr, int request,
493                                          enum p2p_prov_disc_status status,
494                                          u16 config_methods,
495                                          unsigned int generated_pin)
496 {
497         wpas_dbus_signal_p2p_provision_discovery(wpa_s, dev_addr, request,
498                                                  status, config_methods,
499                                                  generated_pin);
500 }
501
502
503 void wpas_notify_p2p_group_started(struct wpa_supplicant *wpa_s,
504                                    struct wpa_ssid *ssid, int network_id,
505                                    int client)
506 {
507         /* Notify a group has been started */
508         wpas_dbus_register_p2p_group(wpa_s, ssid);
509
510         wpas_dbus_signal_p2p_group_started(wpa_s, ssid, client, network_id);
511 }
512
513
514 void wpas_notify_p2p_wps_failed(struct wpa_supplicant *wpa_s,
515                                 struct wps_event_fail *fail)
516 {
517         wpas_dbus_signal_p2p_wps_failed(wpa_s, fail);
518 }
519
520 #endif /* CONFIG_P2P */
521
522
523 static void wpas_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
524                                           const u8 *sta)
525 {
526 #ifdef CONFIG_P2P
527         /*
528          * Register a group member object corresponding to this peer and
529          * emit a PeerJoined signal. This will check if it really is a
530          * P2P group.
531          */
532         wpas_dbus_register_p2p_groupmember(wpa_s, sta);
533
534         /*
535          * Create 'peer-joined' signal on group object -- will also
536          * check P2P itself.
537          */
538         wpas_dbus_signal_p2p_peer_joined(wpa_s, sta);
539 #endif /* CONFIG_P2P */
540 }
541
542
543 static void wpas_notify_ap_sta_deauthorized(struct wpa_supplicant *wpa_s,
544                                             const u8 *sta)
545 {
546 #ifdef CONFIG_P2P
547         /*
548          * Unregister a group member object corresponding to this peer
549          * if this is a P2P group.
550          */
551         wpas_dbus_unregister_p2p_groupmember(wpa_s, sta);
552
553         /*
554          * Create 'peer-disconnected' signal on group object if this
555          * is a P2P group.
556          */
557         wpas_dbus_signal_p2p_peer_disconnected(wpa_s, sta);
558 #endif /* CONFIG_P2P */
559 }
560
561
562 void wpas_notify_sta_authorized(struct wpa_supplicant *wpa_s,
563                                 const u8 *mac_addr, int authorized)
564 {
565         if (authorized)
566                 wpas_notify_ap_sta_authorized(wpa_s, mac_addr);
567         else
568                 wpas_notify_ap_sta_deauthorized(wpa_s, mac_addr);
569 }
570
571
572 void wpas_notify_certification(struct wpa_supplicant *wpa_s, int depth,
573                                const char *subject, const char *cert_hash,
574                                const struct wpabuf *cert)
575 {
576         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_EAP_PEER_CERT
577                 "depth=%d subject='%s'%s%s",
578                 depth, subject,
579                 cert_hash ? " hash=" : "",
580                 cert_hash ? cert_hash : "");
581
582         if (cert) {
583                 char *cert_hex;
584                 size_t len = wpabuf_len(cert) * 2 + 1;
585                 cert_hex = os_malloc(len);
586                 if (cert_hex) {
587                         wpa_snprintf_hex(cert_hex, len, wpabuf_head(cert),
588                                          wpabuf_len(cert));
589                         wpa_msg_ctrl(wpa_s, MSG_INFO,
590                                      WPA_EVENT_EAP_PEER_CERT
591                                      "depth=%d subject='%s' cert=%s",
592                                      depth, subject, cert_hex);
593                         os_free(cert_hex);
594                 }
595         }
596
597         /* notify the old DBus API */
598         wpa_supplicant_dbus_notify_certification(wpa_s, depth, subject,
599                                                  cert_hash, cert);
600         /* notify the new DBus API */
601         wpas_dbus_signal_certification(wpa_s, depth, subject, cert_hash, cert);
602 }