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