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 1345 info = p2p_get_peer_found(peer_args->wpa_s->global->p2p, 1346 peer_args->p2p_device_addr, 0); 1347 if (info == NULL) { 1348 dbus_set_error(error, DBUS_ERROR_FAILED, 1349 "failed to find peer"); 1350 return FALSE; 1351 } 1352 1353 if (info->wps_sec_dev_type_list_len) { 1354 const u8 *sec_dev_type_list = info->wps_sec_dev_type_list; 1355 int num_sec_dev_types = info->wps_sec_dev_type_list_len; 1356 1357 if (!wpas_dbus_simple_array_property_getter(iter, 1358 DBUS_TYPE_BYTE, 1359 sec_dev_type_list, 1360 num_sec_dev_types, 1361 error)) 1362 goto err_no_mem; 1363 else 1364 return TRUE; 1365 } 1366 1367 if (!wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE, NULL, 1368 0, error)) 1369 goto err_no_mem; 1370 1371 return TRUE; 1372 1373 err_no_mem: 1374 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory"); 1375 return FALSE; 1376 } 1377 1378 1379 dbus_bool_t wpas_dbus_getter_p2p_peer_vendor_extension(DBusMessageIter *iter, 1380 DBusError *error, 1381 void *user_data) 1382 { 1383 const struct wpabuf *vendor_extension[P2P_MAX_WPS_VENDOR_EXT]; 1384 int i, num; 1385 struct peer_handler_args *peer_args = user_data; 1386 const struct p2p_peer_info *info; 1387 1388 info = p2p_get_peer_found(peer_args->wpa_s->global->p2p, 1389 peer_args->p2p_device_addr, 0); 1390 if (info == NULL) { 1391 dbus_set_error(error, DBUS_ERROR_FAILED, 1392 "failed to find peer"); 1393 return FALSE; 1394 } 1395 1396 /* Add WPS vendor extensions attribute */ 1397 for (i = 0, num = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { 1398 if (info->wps_vendor_ext[i] == NULL) 1399 continue; 1400 vendor_extension[num] = info->wps_vendor_ext[i]; 1401 num++; 1402 } 1403 1404 if (!wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_STRING, 1405 vendor_extension, num, 1406 error)) { 1407 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory"); 1408 return FALSE; 1409 } 1410 1411 return TRUE; 1412 } 1413 1414 1415 dbus_bool_t wpas_dbus_getter_p2p_peer_ies(DBusMessageIter *iter, 1416 DBusError *error, void *user_data) 1417 { 1418 /* struct peer_handler_args *peer_args = user_data; */ 1419 1420 dbus_set_error_const(error, DBUS_ERROR_FAILED, "not implemented"); 1421 return FALSE; 1422 } 1423 1424 1425 /** 1426 * wpas_dbus_getter_persistent_groups - Get array of persistent group objects 1427 * @iter: Pointer to incoming dbus message iter 1428 * @error: Location to store error on failure 1429 * @user_data: Function specific data 1430 * Returns: TRUE on success, FALSE on failure 1431 * 1432 * Getter for "PersistentGroups" property. 1433 */ 1434 dbus_bool_t wpas_dbus_getter_persistent_groups(DBusMessageIter *iter, 1435 DBusError *error, 1436 void *user_data) 1437 { 1438 struct wpa_supplicant *wpa_s = user_data; 1439 struct wpa_ssid *ssid; 1440 char **paths; 1441 unsigned int i = 0, num = 0; 1442 dbus_bool_t success = FALSE; 1443 1444 if (wpa_s->conf == NULL) { 1445 wpa_printf(MSG_ERROR, "dbus: %s: " 1446 "An error occurred getting persistent groups list", 1447 __func__); 1448 dbus_set_error_const(error, DBUS_ERROR_FAILED, "an error " 1449 "occurred getting persistent groups list"); 1450 return FALSE; 1451 } 1452 1453 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) 1454 if (network_is_persistent_group(ssid)) 1455 num++; 1456 1457 paths = os_zalloc(num * sizeof(char *)); 1458 if (!paths) { 1459 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory"); 1460 return FALSE; 1461 } 1462 1463 /* Loop through configured networks and append object path of each */ 1464 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) { 1465 if (!network_is_persistent_group(ssid)) 1466 continue; 1467 paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX); 1468 if (paths[i] == NULL) { 1469 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, 1470 "no memory"); 1471 goto out; 1472 } 1473 /* Construct the object path for this network. */ 1474 os_snprintf(paths[i++], WPAS_DBUS_OBJECT_PATH_MAX, 1475 "%s/" WPAS_DBUS_NEW_PERSISTENT_GROUPS_PART "/%d", 1476 wpa_s->dbus_new_path, ssid->id); 1477 } 1478 1479 success = wpas_dbus_simple_array_property_getter(iter, 1480 DBUS_TYPE_OBJECT_PATH, 1481 paths, num, error); 1482 1483 out: 1484 while (i) 1485 os_free(paths[--i]); 1486 os_free(paths); 1487 return success; 1488 } 1489 1490 1491 /** 1492 * wpas_dbus_getter_persistent_group_properties - Get options for a persistent 1493 * group 1494 * @iter: Pointer to incoming dbus message iter 1495 * @error: Location to store error on failure 1496 * @user_data: Function specific data 1497 * Returns: TRUE on success, FALSE on failure 1498 * 1499 * Getter for "Properties" property of a persistent group. 1500 */ 1501 dbus_bool_t wpas_dbus_getter_persistent_group_properties(DBusMessageIter *iter, 1502 DBusError *error, 1503 void *user_data) 1504 { 1505 struct network_handler_args *net = user_data; 1506 1507 /* Leveraging the fact that persistent group object is still 1508 * represented in same manner as network within. 1509 */ 1510 return wpas_dbus_getter_network_properties(iter, error, net); 1511 } 1512 1513 1514 /** 1515 * wpas_dbus_setter_persistent_group_properties - Get options for a persistent 1516 * group 1517 * @iter: Pointer to incoming dbus message iter 1518 * @error: Location to store error on failure 1519 * @user_data: Function specific data 1520 * Returns: TRUE on success, FALSE on failure 1521 * 1522 * Setter for "Properties" property of a persistent group. 1523 */ 1524 dbus_bool_t wpas_dbus_setter_persistent_group_properties(DBusMessageIter *iter, 1525 DBusError *error, 1526 void *user_data) 1527 { 1528 struct network_handler_args *net = user_data; 1529 struct wpa_ssid *ssid = net->ssid; 1530 DBusMessageIter variant_iter; 1531 1532 /* 1533 * Leveraging the fact that persistent group object is still 1534 * represented in same manner as network within. 1535 */ 1536 dbus_message_iter_recurse(iter, &variant_iter); 1537 return set_network_properties(net->wpa_s, ssid, &variant_iter, error); 1538 } 1539 1540 1541 /** 1542 * wpas_dbus_new_iface_add_persistent_group - Add a new configured 1543 * persistent_group 1544 * @message: Pointer to incoming dbus message 1545 * @wpa_s: wpa_supplicant structure for a network interface 1546 * Returns: A dbus message containing the object path of the new 1547 * persistent group 1548 * 1549 * Handler function for "AddPersistentGroup" method call of a P2P Device 1550 * interface. 1551 */ 1552 DBusMessage * wpas_dbus_handler_add_persistent_group( 1553 DBusMessage *message, struct wpa_supplicant *wpa_s) 1554 { 1555 DBusMessage *reply = NULL; 1556 DBusMessageIter iter; 1557 struct wpa_ssid *ssid = NULL; 1558 char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *path = path_buf; 1559 DBusError error; 1560 1561 dbus_message_iter_init(message, &iter); 1562 1563 ssid = wpa_config_add_network(wpa_s->conf); 1564 if (ssid == NULL) { 1565 wpa_printf(MSG_ERROR, "dbus: %s: " 1566 "Cannot add new persistent group", __func__); 1567 reply = wpas_dbus_error_unknown_error( 1568 message, 1569 "wpa_supplicant could not add " 1570 "a persistent group on this interface."); 1571 goto err; 1572 } 1573 1574 /* Mark the ssid as being a persistent group before the notification */ 1575 ssid->disabled = 2; 1576 ssid->p2p_persistent_group = 1; 1577 wpas_notify_persistent_group_added(wpa_s, ssid); 1578 1579 wpa_config_set_network_defaults(ssid); 1580 1581 dbus_error_init(&error); 1582 if (!set_network_properties(wpa_s, ssid, &iter, &error)) { 1583 wpa_printf(MSG_DEBUG, "dbus: %s: " 1584 "Control interface could not set persistent group " 1585 "properties", __func__); 1586 reply = wpas_dbus_reply_new_from_error(message, &error, 1587 DBUS_ERROR_INVALID_ARGS, 1588 "Failed to set network " 1589 "properties"); 1590 dbus_error_free(&error); 1591 goto err; 1592 } 1593 1594 /* Construct the object path for this network. */ 1595 os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX, 1596 "%s/" WPAS_DBUS_NEW_PERSISTENT_GROUPS_PART "/%d", 1597 wpa_s->dbus_new_path, ssid->id); 1598 1599 reply = dbus_message_new_method_return(message); 1600 if (reply == NULL) { 1601 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY, 1602 NULL); 1603 goto err; 1604 } 1605 if (!dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path, 1606 DBUS_TYPE_INVALID)) { 1607 dbus_message_unref(reply); 1608 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY, 1609 NULL); 1610 goto err; 1611 } 1612 1613 return reply; 1614 1615 err: 1616 if (ssid) { 1617 wpas_notify_persistent_group_removed(wpa_s, ssid); 1618 wpa_config_remove_network(wpa_s->conf, ssid->id); 1619 } 1620 return reply; 1621 } 1622 1623 1624 /** 1625 * wpas_dbus_handler_remove_persistent_group - Remove a configured persistent 1626 * group 1627 * @message: Pointer to incoming dbus message 1628 * @wpa_s: wpa_supplicant structure for a network interface 1629 * Returns: NULL on success or dbus error on failure 1630 * 1631 * Handler function for "RemovePersistentGroup" method call of a P2P Device 1632 * interface. 1633 */ 1634 DBusMessage * wpas_dbus_handler_remove_persistent_group( 1635 DBusMessage *message, struct wpa_supplicant *wpa_s) 1636 { 1637 DBusMessage *reply = NULL; 1638 const char *op; 1639 char *iface = NULL, *persistent_group_id = NULL; 1640 int id; 1641 struct wpa_ssid *ssid; 1642 1643 dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &op, 1644 DBUS_TYPE_INVALID); 1645 1646 /* 1647 * Extract the network ID and ensure the network is actually a child of 1648 * this interface. 1649 */ 1650 iface = wpas_dbus_new_decompose_object_path(op, 1, 1651 &persistent_group_id, 1652 NULL); 1653 if (iface == NULL || os_strcmp(iface, wpa_s->dbus_new_path) != 0) { 1654 reply = wpas_dbus_error_invalid_args(message, op); 1655 goto out; 1656 } 1657 1658 id = strtoul(persistent_group_id, NULL, 10); 1659 if (errno == EINVAL) { 1660 reply = wpas_dbus_error_invalid_args(message, op); 1661 goto out; 1662 } 1663 1664 ssid = wpa_config_get_network(wpa_s->conf, id); 1665 if (ssid == NULL) { 1666 reply = wpas_dbus_error_persistent_group_unknown(message); 1667 goto out; 1668 } 1669 1670 wpas_notify_persistent_group_removed(wpa_s, ssid); 1671 1672 if (wpa_config_remove_network(wpa_s->conf, id) < 0) { 1673 wpa_printf(MSG_ERROR, "dbus: %s: " 1674 "error occurred when removing persistent group %d", 1675 __func__, id); 1676 reply = wpas_dbus_error_unknown_error( 1677 message, 1678 "error removing the specified persistent group on " 1679 "this interface."); 1680 goto out; 1681 } 1682 1683 out: 1684 os_free(iface); 1685 os_free(persistent_group_id); 1686 return reply; 1687 } 1688 1689 1690 static void remove_persistent_group(struct wpa_supplicant *wpa_s, 1691 struct wpa_ssid *ssid) 1692 { 1693 wpas_notify_persistent_group_removed(wpa_s, ssid); 1694 1695 if (wpa_config_remove_network(wpa_s->conf, ssid->id) < 0) { 1696 wpa_printf(MSG_ERROR, "dbus: %s: " 1697 "error occurred when removing persistent group %d", 1698 __func__, ssid->id); 1699 return; 1700 } 1701 } 1702 1703 1704 /** 1705 * wpas_dbus_handler_remove_all_persistent_groups - Remove all configured 1706 * persistent groups 1707 * @message: Pointer to incoming dbus message 1708 * @wpa_s: wpa_supplicant structure for a network interface 1709 * Returns: NULL on success or dbus error on failure 1710 * 1711 * Handler function for "RemoveAllPersistentGroups" method call of a 1712 * P2P Device interface. 1713 */ 1714 DBusMessage * wpas_dbus_handler_remove_all_persistent_groups( 1715 DBusMessage *message, struct wpa_supplicant *wpa_s) 1716 { 1717 struct wpa_ssid *ssid, *next; 1718 struct wpa_config *config; 1719 1720 config = wpa_s->conf; 1721 ssid = config->ssid; 1722 while (ssid) { 1723 next = ssid->next; 1724 if (network_is_persistent_group(ssid)) 1725 remove_persistent_group(wpa_s, ssid); 1726 ssid = next; 1727 } 1728 return NULL; 1729 } 1730 1731 1732 /* 1733 * Group object properties accessor methods 1734 */ 1735 1736 dbus_bool_t wpas_dbus_getter_p2p_group_members(DBusMessageIter *iter, 1737 DBusError *error, 1738 void *user_data) 1739 { 1740 struct wpa_supplicant *wpa_s = user_data; 1741 struct wpa_ssid *ssid; 1742 unsigned int num_members; 1743 char **paths; 1744 unsigned int i; 1745 void *next = NULL; 1746 const u8 *addr; 1747 dbus_bool_t success = FALSE; 1748 1749 /* Ensure we are a GO */ 1750 if (wpa_s->wpa_state != WPA_COMPLETED) 1751 return FALSE; 1752 1753 ssid = wpa_s->conf->ssid; 1754 /* At present WPAS P2P_GO mode only applicable for p2p_go */ 1755 if (ssid->mode != WPAS_MODE_P2P_GO && 1756 ssid->mode != WPAS_MODE_AP && 1757 ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) 1758 return FALSE; 1759 1760 num_members = p2p_get_group_num_members(wpa_s->p2p_group); 1761 1762 paths = os_zalloc(num_members * sizeof(char *)); 1763 if (!paths) 1764 goto out_of_memory; 1765 1766 i = 0; 1767 while ((addr = p2p_iterate_group_members(wpa_s->p2p_group, &next))) { 1768 paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX); 1769 if (!paths[i]) 1770 goto out_of_memory; 1771 os_snprintf(paths[i], WPAS_DBUS_OBJECT_PATH_MAX, 1772 "%s/" WPAS_DBUS_NEW_P2P_GROUPMEMBERS_PART 1773 "/" COMPACT_MACSTR, 1774 wpa_s->dbus_groupobj_path, MAC2STR(addr)); 1775 i++; 1776 } 1777 1778 success = wpas_dbus_simple_array_property_getter(iter, 1779 DBUS_TYPE_OBJECT_PATH, 1780 paths, num_members, 1781 error); 1782 1783 for (i = 0; i < num_members; i++) 1784 os_free(paths[i]); 1785 os_free(paths); 1786 return success; 1787 1788 out_of_memory: 1789 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory"); 1790 if (paths) { 1791 for (i = 0; i < num_members; i++) 1792 os_free(paths[i]); 1793 os_free(paths); 1794 } 1795 return FALSE; 1796 } 1797 1798 1799 dbus_bool_t wpas_dbus_getter_p2p_group_properties(DBusMessageIter *iter, 1800 DBusError *error, 1801 void *user_data) 1802 { 1803 struct wpa_supplicant *wpa_s = user_data; 1804 DBusMessageIter variant_iter, dict_iter; 1805 struct hostapd_data *hapd = NULL; 1806 const struct wpabuf *vendor_ext[MAX_WPS_VENDOR_EXTENSIONS]; 1807 int num_vendor_ext = 0; 1808 int i; 1809 u8 role = wpas_get_p2p_role(wpa_s); 1810 u16 op_freq = 0; 1811 u8 *p_bssid = NULL; 1812 char *role_name = NULL; 1813 1814 if (!wpa_s->current_ssid) 1815 return FALSE; 1816 1817 /* Check current role and adjust information accordingly */ 1818 switch (role) { 1819 case WPAS_P2P_ROLE_CLIENT: 1820 /* go_params is only valid for a client */ 1821 if (wpa_s->go_params) { 1822 op_freq = wpa_s->go_params->freq; 1823 p_bssid = wpa_s->current_ssid->bssid; 1824 role_name = "client"; 1825 } else 1826 return FALSE; 1827 break; 1828 case WPAS_P2P_ROLE_GO: 1829 /* ap_iface is only valid for a GO */ 1830 if (wpa_s->ap_iface) { 1831 hapd = wpa_s->ap_iface->bss[0]; 1832 p_bssid = hapd->own_addr; 1833 op_freq = wpa_s->ap_iface->freq; 1834 role_name = "GO"; 1835 } else 1836 return FALSE; 1837 break; 1838 default: 1839 /* Error condition; this should NEVER occur */ 1840 return FALSE; 1841 } 1842 1843 if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, 1844 "a{sv}", &variant_iter) || 1845 !wpa_dbus_dict_open_write(&variant_iter, &dict_iter)) 1846 goto err_no_mem; 1847 /* Provide the SSID */ 1848 if (!wpa_dbus_dict_append_byte_array( 1849 &dict_iter, "SSID", 1850 (const char *) wpa_s->current_ssid->ssid, 1851 wpa_s->current_ssid->ssid_len)) 1852 goto err_no_mem; 1853 /* Provide the BSSID */ 1854 if (p_bssid && 1855 !wpa_dbus_dict_append_byte_array(&dict_iter, "BSSID", 1856 (const char *) p_bssid, ETH_ALEN)) 1857 goto err_no_mem; 1858 /* Provide the role within the group */ 1859 if (role_name && 1860 !wpa_dbus_dict_append_string(&dict_iter, "Role", role_name)) 1861 goto err_no_mem; 1862 /* Provide the operational frequency */ 1863 if (!wpa_dbus_dict_append_uint16(&dict_iter, "Frequency", op_freq)) 1864 goto err_no_mem; 1865 1866 /* Additional information for group owners */ 1867 if (role == WPAS_P2P_ROLE_GO) { 1868 /* Provide the passphrase */ 1869 if (!wpa_dbus_dict_append_string(&dict_iter, "Passphrase", 1870 wpa_s->current_ssid->passphrase)) 1871 goto err_no_mem; 1872 /* Parse WPS Vendor Extensions sent in Beacon/Probe Response */ 1873 for (i = 0; hapd && i < MAX_WPS_VENDOR_EXTENSIONS; i++) { 1874 if (hapd->conf->wps_vendor_ext[i] == NULL) 1875 continue; 1876 vendor_ext[num_vendor_ext++] = 1877 hapd->conf->wps_vendor_ext[i]; 1878 } 1879 if (!wpa_dbus_dict_append_wpabuf_array(&dict_iter, 1880 "WPSVendorExtensions", 1881 vendor_ext, num_vendor_ext)) 1882 goto err_no_mem; 1883 } else { 1884 /* If not a GO, provide the PSK */ 1885 if (!wpa_dbus_dict_append_byte_array( 1886 &dict_iter, "PSK", 1887 (const char *) wpa_s->current_ssid->psk, 32)) 1888 goto err_no_mem; 1889 } 1890 1891 if (!wpa_dbus_dict_close_write(&variant_iter, &dict_iter) || 1892 !dbus_message_iter_close_container(iter, &variant_iter)) 1893 goto err_no_mem; 1894 1895 return TRUE; 1896 1897 err_no_mem: 1898 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory"); 1899 return FALSE; 1900 } 1901 1902 1903 dbus_bool_t wpas_dbus_setter_p2p_group_properties(DBusMessageIter *iter, 1904 DBusError *error, 1905 void *user_data) 1906 { 1907 struct wpa_supplicant *wpa_s = user_data; 1908 DBusMessageIter variant_iter, iter_dict; 1909 struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING }; 1910 unsigned int i; 1911 struct hostapd_data *hapd = NULL; 1912 1913 if (wpas_get_p2p_role(wpa_s) == WPAS_P2P_ROLE_GO && 1914 wpa_s->ap_iface != NULL) 1915 hapd = wpa_s->ap_iface->bss[0]; 1916 else 1917 return FALSE; 1918 1919 dbus_message_iter_recurse(iter, &variant_iter); 1920 if (!wpa_dbus_dict_open_read(&variant_iter, &iter_dict, error)) 1921 return FALSE; 1922 1923 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) { 1924 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) { 1925 dbus_set_error_const(error, DBUS_ERROR_INVALID_ARGS, 1926 "invalid message format"); 1927 return FALSE; 1928 } 1929 1930 if (os_strcmp(entry.key, "WPSVendorExtensions") == 0) { 1931 if (entry.type != DBUS_TYPE_ARRAY || 1932 entry.array_type != WPAS_DBUS_TYPE_BINARRAY || 1933 entry.array_len > MAX_WPS_VENDOR_EXTENSIONS) 1934 goto error; 1935 1936 for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) { 1937 if (i < entry.array_len) { 1938 hapd->conf->wps_vendor_ext[i] = 1939 entry.binarray_value[i]; 1940 entry.binarray_value[i] = NULL; 1941 } else 1942 hapd->conf->wps_vendor_ext[i] = NULL; 1943 } 1944 1945 hostapd_update_wps(hapd); 1946 } else 1947 goto error; 1948 1949 wpa_dbus_dict_entry_clear(&entry); 1950 } 1951 1952 return TRUE; 1953 1954 error: 1955 wpa_dbus_dict_entry_clear(&entry); 1956 dbus_set_error_const(error, DBUS_ERROR_INVALID_ARGS, 1957 "invalid message format"); 1958 return FALSE; 1959 } 1960 1961 1962 DBusMessage * wpas_dbus_handler_p2p_add_service(DBusMessage *message, 1963 struct wpa_supplicant *wpa_s) 1964 { 1965 DBusMessageIter iter_dict; 1966 DBusMessage *reply = NULL; 1967 DBusMessageIter iter; 1968 struct wpa_dbus_dict_entry entry; 1969 int upnp = 0; 1970 int bonjour = 0; 1971 char *service = NULL; 1972 struct wpabuf *query = NULL; 1973 struct wpabuf *resp = NULL; 1974 u8 version = 0; 1975 1976 dbus_message_iter_init(message, &iter); 1977 1978 if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL)) 1979 goto error; 1980 1981 if (wpa_dbus_dict_has_dict_entry(&iter_dict)) { 1982 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) 1983 goto error; 1984 1985 if (!os_strcmp(entry.key, "service_type") && 1986 (entry.type == DBUS_TYPE_STRING)) { 1987 if (!os_strcmp(entry.str_value, "upnp")) 1988 upnp = 1; 1989 else if (!os_strcmp(entry.str_value, "bonjour")) 1990 bonjour = 1; 1991 else 1992 goto error_clear; 1993 wpa_dbus_dict_entry_clear(&entry); 1994 } 1995 } 1996 1997 if (upnp == 1) { 1998 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) { 1999 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) 2000 goto error; 2001 2002 if (!os_strcmp(entry.key, "version") && 2003 entry.type == DBUS_TYPE_INT32) 2004 version = entry.uint32_value; 2005 else if (!os_strcmp(entry.key, "service") && 2006 entry.type == DBUS_TYPE_STRING) 2007 service = os_strdup(entry.str_value); 2008 wpa_dbus_dict_entry_clear(&entry); 2009 } 2010 if (version <= 0 || service == NULL) 2011 goto error; 2012 2013 if (wpas_p2p_service_add_upnp(wpa_s, version, service) != 0) 2014 goto error; 2015 2016 os_free(service); 2017 } else if (bonjour == 1) { 2018 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) { 2019 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) 2020 goto error; 2021 2022 if (!os_strcmp(entry.key, "query")) { 2023 if ((entry.type != DBUS_TYPE_ARRAY) || 2024 (entry.array_type != DBUS_TYPE_BYTE)) 2025 goto error_clear; 2026 query = wpabuf_alloc_copy( 2027 entry.bytearray_value, 2028 entry.array_len); 2029 } else if (!os_strcmp(entry.key, "response")) { 2030 if ((entry.type != DBUS_TYPE_ARRAY) || 2031 (entry.array_type != DBUS_TYPE_BYTE)) 2032 goto error_clear; 2033 resp = wpabuf_alloc_copy(entry.bytearray_value, 2034 entry.array_len); 2035 } 2036 2037 wpa_dbus_dict_entry_clear(&entry); 2038 } 2039 2040 if (query == NULL || resp == NULL) 2041 goto error; 2042 2043 if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) { 2044 wpabuf_free(query); 2045 wpabuf_free(resp); 2046 goto error; 2047 } 2048 } else 2049 goto error; 2050 2051 return reply; 2052 error_clear: 2053 wpa_dbus_dict_entry_clear(&entry); 2054 error: 2055 return wpas_dbus_error_invalid_args(message, NULL); 2056 } 2057 2058 2059 DBusMessage * wpas_dbus_handler_p2p_delete_service( 2060 DBusMessage *message, struct wpa_supplicant *wpa_s) 2061 { 2062 DBusMessageIter iter_dict; 2063 DBusMessage *reply = NULL; 2064 DBusMessageIter iter; 2065 struct wpa_dbus_dict_entry entry; 2066 int upnp = 0; 2067 int bonjour = 0; 2068 int ret = 0; 2069 char *service = NULL; 2070 struct wpabuf *query = NULL; 2071 u8 version = 0; 2072 2073 dbus_message_iter_init(message, &iter); 2074 2075 if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL)) 2076 goto error; 2077 2078 if (wpa_dbus_dict_has_dict_entry(&iter_dict)) { 2079 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) 2080 goto error; 2081 2082 if (!os_strcmp(entry.key, "service_type") && 2083 (entry.type == DBUS_TYPE_STRING)) { 2084 if (!os_strcmp(entry.str_value, "upnp")) 2085 upnp = 1; 2086 else if (!os_strcmp(entry.str_value, "bonjour")) 2087 bonjour = 1; 2088 else 2089 goto error_clear; 2090 wpa_dbus_dict_entry_clear(&entry); 2091 } 2092 } 2093 if (upnp == 1) { 2094 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) { 2095 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) 2096 goto error; 2097 if (!os_strcmp(entry.key, "version") && 2098 entry.type == DBUS_TYPE_INT32) 2099 version = entry.uint32_value; 2100 else if (!os_strcmp(entry.key, "service") && 2101 entry.type == DBUS_TYPE_STRING) 2102 service = os_strdup(entry.str_value); 2103 else 2104 goto error_clear; 2105 2106 wpa_dbus_dict_entry_clear(&entry); 2107 } 2108 2109 if (version <= 0 || service == NULL) 2110 goto error; 2111 2112 ret = wpas_p2p_service_del_upnp(wpa_s, version, service); 2113 os_free(service); 2114 if (ret != 0) 2115 goto error; 2116 } else if (bonjour == 1) { 2117 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) { 2118 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) 2119 goto error; 2120 2121 if (!os_strcmp(entry.key, "query")) { 2122 if ((entry.type != DBUS_TYPE_ARRAY) || 2123 (entry.array_type != DBUS_TYPE_BYTE)) 2124 goto error_clear; 2125 query = wpabuf_alloc_copy( 2126 entry.bytearray_value, 2127 entry.array_len); 2128 } else 2129 goto error_clear; 2130 2131 wpa_dbus_dict_entry_clear(&entry); 2132 } 2133 2134 if (query == NULL) 2135 goto error; 2136 2137 ret = wpas_p2p_service_del_bonjour(wpa_s, query); 2138 if (ret != 0) 2139 goto error; 2140 wpabuf_free(query); 2141 } else 2142 goto error; 2143 2144 return reply; 2145 error_clear: 2146 wpa_dbus_dict_entry_clear(&entry); 2147 error: 2148 return wpas_dbus_error_invalid_args(message, NULL); 2149 } 2150 2151 2152 DBusMessage * wpas_dbus_handler_p2p_flush_service(DBusMessage *message, 2153 struct wpa_supplicant *wpa_s) 2154 { 2155 wpas_p2p_service_flush(wpa_s); 2156 return NULL; 2157 } 2158 2159 2160 DBusMessage * wpas_dbus_handler_p2p_service_sd_req( 2161 DBusMessage *message, struct wpa_supplicant *wpa_s) 2162 { 2163 DBusMessageIter iter_dict; 2164 DBusMessage *reply = NULL; 2165 DBusMessageIter iter; 2166 struct wpa_dbus_dict_entry entry; 2167 int upnp = 0; 2168 char *service = NULL; 2169 char *peer_object_path = NULL; 2170 struct wpabuf *tlv = NULL; 2171 u8 version = 0; 2172 u64 ref = 0; 2173 u8 addr[ETH_ALEN]; 2174 2175 dbus_message_iter_init(message, &iter); 2176 2177 if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL)) 2178 goto error; 2179 2180 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) { 2181 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) 2182 goto error; 2183 if (!os_strcmp(entry.key, "peer_object") && 2184 entry.type == DBUS_TYPE_OBJECT_PATH) { 2185 peer_object_path = os_strdup(entry.str_value); 2186 } else if (!os_strcmp(entry.key, "service_type") && 2187 entry.type == DBUS_TYPE_STRING) { 2188 if (!os_strcmp(entry.str_value, "upnp")) 2189 upnp = 1; 2190 else 2191 goto error_clear; 2192 } else if (!os_strcmp(entry.key, "version") && 2193 entry.type == DBUS_TYPE_INT32) { 2194 version = entry.uint32_value; 2195 } else if (!os_strcmp(entry.key, "service") && 2196 entry.type == DBUS_TYPE_STRING) { 2197 service = os_strdup(entry.str_value); 2198 } else if (!os_strcmp(entry.key, "tlv")) { 2199 if (entry.type != DBUS_TYPE_ARRAY || 2200 entry.array_type != DBUS_TYPE_BYTE) 2201 goto error_clear; 2202 tlv = wpabuf_alloc_copy(entry.bytearray_value, 2203 entry.array_len); 2204 } else 2205 goto error_clear; 2206 2207 wpa_dbus_dict_entry_clear(&entry); 2208 } 2209 2210 if (!peer_object_path || 2211 (parse_peer_object_path(peer_object_path, addr) < 0) || 2212 !p2p_peer_known(wpa_s->global->p2p, addr)) 2213 goto error; 2214 2215 if (upnp == 1) { 2216 if (version <= 0 || service == NULL) 2217 goto error; 2218 2219 ref = wpas_p2p_sd_request_upnp(wpa_s, addr, version, service); 2220 } else { 2221 if (tlv == NULL) 2222 goto error; 2223 ref = wpas_p2p_sd_request(wpa_s, addr, tlv); 2224 wpabuf_free(tlv); 2225 } 2226 2227 if (ref != 0) { 2228 reply = dbus_message_new_method_return(message); 2229 dbus_message_append_args(reply, DBUS_TYPE_UINT64, 2230 &ref, DBUS_TYPE_INVALID); 2231 } else { 2232 reply = wpas_dbus_error_unknown_error( 2233 message, "Unable to send SD request"); 2234 } 2235 out: 2236 os_free(service); 2237 os_free(peer_object_path); 2238 return reply; 2239 error_clear: 2240 wpa_dbus_dict_entry_clear(&entry); 2241 error: 2242 if (tlv) 2243 wpabuf_free(tlv); 2244 reply = wpas_dbus_error_invalid_args(message, NULL); 2245 goto out; 2246 } 2247 2248 2249 DBusMessage * wpas_dbus_handler_p2p_service_sd_res( 2250 DBusMessage *message, struct wpa_supplicant *wpa_s) 2251 { 2252 DBusMessageIter iter_dict; 2253 DBusMessage *reply = NULL; 2254 DBusMessageIter iter; 2255 struct wpa_dbus_dict_entry entry; 2256 char *peer_object_path = NULL; 2257 struct wpabuf *tlv = NULL; 2258 int freq = 0; 2259 int dlg_tok = 0; 2260 u8 addr[ETH_ALEN]; 2261 2262 dbus_message_iter_init(message, &iter); 2263 2264 if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL)) 2265 goto error; 2266 2267 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) { 2268 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) 2269 goto error; 2270 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, "frequency") && 2275 entry.type == DBUS_TYPE_INT32) { 2276 freq = entry.uint32_value; 2277 } else if (!os_strcmp(entry.key, "dialog_token") && 2278 entry.type == DBUS_TYPE_UINT32) { 2279 dlg_tok = entry.uint32_value; 2280 } else if (!os_strcmp(entry.key, "tlvs")) { 2281 if (entry.type != DBUS_TYPE_ARRAY || 2282 entry.array_type != DBUS_TYPE_BYTE) 2283 goto error_clear; 2284 tlv = wpabuf_alloc_copy(entry.bytearray_value, 2285 entry.array_len); 2286 } else 2287 goto error_clear; 2288 2289 wpa_dbus_dict_entry_clear(&entry); 2290 } 2291 if (!peer_object_path || 2292 (parse_peer_object_path(peer_object_path, addr) < 0) || 2293 !p2p_peer_known(wpa_s->global->p2p, addr)) 2294 goto error; 2295 2296 if (tlv == NULL) 2297 goto error; 2298 2299 wpas_p2p_sd_response(wpa_s, freq, addr, (u8) dlg_tok, tlv); 2300 wpabuf_free(tlv); 2301 out: 2302 os_free(peer_object_path); 2303 return reply; 2304 error_clear: 2305 wpa_dbus_dict_entry_clear(&entry); 2306 error: 2307 reply = wpas_dbus_error_invalid_args(message, NULL); 2308 goto out; 2309 } 2310 2311 2312 DBusMessage * wpas_dbus_handler_p2p_service_sd_cancel_req( 2313 DBusMessage *message, struct wpa_supplicant *wpa_s) 2314 { 2315 DBusMessageIter iter; 2316 u64 req = 0; 2317 2318 dbus_message_iter_init(message, &iter); 2319 dbus_message_iter_get_basic(&iter, &req); 2320 2321 if (req == 0) 2322 goto error; 2323 2324 if (!wpas_p2p_sd_cancel_request(wpa_s, req)) 2325 goto error; 2326 2327 return NULL; 2328 error: 2329 return wpas_dbus_error_invalid_args(message, NULL); 2330 } 2331 2332 2333 DBusMessage * wpas_dbus_handler_p2p_service_update( 2334 DBusMessage *message, struct wpa_supplicant *wpa_s) 2335 { 2336 wpas_p2p_sd_service_update(wpa_s); 2337 return NULL; 2338 } 2339 2340 2341 DBusMessage * wpas_dbus_handler_p2p_serv_disc_external( 2342 DBusMessage *message, struct wpa_supplicant *wpa_s) 2343 { 2344 DBusMessageIter iter; 2345 int ext = 0; 2346 2347 dbus_message_iter_init(message, &iter); 2348 dbus_message_iter_get_basic(&iter, &ext); 2349 2350 wpa_s->p2p_sd_over_ctrl_iface = ext; 2351 2352 return NULL; 2353 2354 } 2355