1 /* 2 * Wi-Fi Direct - P2P group operations 3 * Copyright (c) 2009-2010, Atheros Communications 4 * 5 * This software may be distributed under the terms of the BSD license. 6 * See README for more details. 7 */ 8 9 #include "includes.h" 10 11 #include "common.h" 12 #include "common/ieee802_11_defs.h" 13 #include "common/ieee802_11_common.h" 14 #include "wps/wps_defs.h" 15 #include "wps/wps_i.h" 16 #include "p2p_i.h" 17 #include "p2p.h" 18 19 20 struct p2p_group_member { 21 struct p2p_group_member *next; 22 u8 addr[ETH_ALEN]; /* P2P Interface Address */ 23 u8 dev_addr[ETH_ALEN]; /* P2P Device Address */ 24 struct wpabuf *p2p_ie; 25 struct wpabuf *client_info; 26 u8 dev_capab; 27 }; 28 29 /** 30 * struct p2p_group - Internal P2P module per-group data 31 */ 32 struct p2p_group { 33 struct p2p_data *p2p; 34 struct p2p_group_config *cfg; 35 struct p2p_group_member *members; 36 unsigned int num_members; 37 int group_formation; 38 int beacon_update; 39 struct wpabuf *noa; 40 }; 41 42 43 static void p2p_group_update_ies(struct p2p_group *group); 44 45 46 struct p2p_group * p2p_group_init(struct p2p_data *p2p, 47 struct p2p_group_config *config) 48 { 49 struct p2p_group *group, **groups; 50 51 group = os_zalloc(sizeof(*group)); 52 if (group == NULL) 53 return NULL; 54 55 groups = os_realloc(p2p->groups, (p2p->num_groups + 1) * 56 sizeof(struct p2p_group *)); 57 if (groups == NULL) { 58 os_free(group); 59 return NULL; 60 } 61 groups[p2p->num_groups++] = group; 62 p2p->groups = groups; 63 64 group->p2p = p2p; 65 group->cfg = config; 66 group->group_formation = 1; 67 group->beacon_update = 1; 68 p2p_group_update_ies(group); 69 group->cfg->idle_update(group->cfg->cb_ctx, 1); 70 71 return group; 72 } 73 74 75 static void p2p_group_free_member(struct p2p_group_member *m) 76 { 77 wpabuf_free(m->p2p_ie); 78 wpabuf_free(m->client_info); 79 os_free(m); 80 } 81 82 83 static void p2p_group_free_members(struct p2p_group *group) 84 { 85 struct p2p_group_member *m, *prev; 86 m = group->members; 87 group->members = NULL; 88 group->num_members = 0; 89 while (m) { 90 prev = m; 91 m = m->next; 92 p2p_group_free_member(prev); 93 } 94 } 95 96 97 void p2p_group_deinit(struct p2p_group *group) 98 { 99 size_t g; 100 struct p2p_data *p2p; 101 102 if (group == NULL) 103 return; 104 105 p2p = group->p2p; 106 107 for (g = 0; g < p2p->num_groups; g++) { 108 if (p2p->groups[g] == group) { 109 while (g + 1 < p2p->num_groups) { 110 p2p->groups[g] = p2p->groups[g + 1]; 111 g++; 112 } 113 p2p->num_groups--; 114 break; 115 } 116 } 117 118 p2p_group_free_members(group); 119 os_free(group->cfg); 120 wpabuf_free(group->noa); 121 os_free(group); 122 } 123 124 125 static void p2p_client_info(struct wpabuf *ie, struct p2p_group_member *m) 126 { 127 if (m->client_info == NULL) 128 return; 129 if (wpabuf_tailroom(ie) < wpabuf_len(m->client_info) + 1) 130 return; 131 wpabuf_put_buf(ie, m->client_info); 132 } 133 134 135 static void p2p_group_add_common_ies(struct p2p_group *group, 136 struct wpabuf *ie) 137 { 138 u8 dev_capab = 0, group_capab = 0; 139 140 /* P2P Capability */ 141 dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY; 142 dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE; 143 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER; 144 if (group->cfg->persistent_group) { 145 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP; 146 if (group->cfg->persistent_group == 2) 147 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_RECONN; 148 } 149 if (group->p2p->cfg->p2p_intra_bss) 150 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST; 151 if (group->group_formation) 152 group_capab |= P2P_GROUP_CAPAB_GROUP_FORMATION; 153 if (group->p2p->cross_connect) 154 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN; 155 if (group->num_members >= group->cfg->max_clients) 156 group_capab |= P2P_GROUP_CAPAB_GROUP_LIMIT; 157 p2p_buf_add_capability(ie, dev_capab, group_capab); 158 } 159 160 161 static void p2p_group_add_noa(struct wpabuf *ie, struct wpabuf *noa) 162 { 163 if (noa == NULL) 164 return; 165 /* Notice of Absence */ 166 wpabuf_put_u8(ie, P2P_ATTR_NOTICE_OF_ABSENCE); 167 wpabuf_put_le16(ie, wpabuf_len(noa)); 168 wpabuf_put_buf(ie, noa); 169 } 170 171 172 static struct wpabuf * p2p_group_build_beacon_ie(struct p2p_group *group) 173 { 174 struct wpabuf *ie; 175 u8 *len; 176 177 ie = wpabuf_alloc(257); 178 if (ie == NULL) 179 return NULL; 180 181 len = p2p_buf_add_ie_hdr(ie); 182 p2p_group_add_common_ies(group, ie); 183 p2p_buf_add_device_id(ie, group->p2p->cfg->dev_addr); 184 p2p_group_add_noa(ie, group->noa); 185 p2p_buf_update_ie_hdr(ie, len); 186 187 return ie; 188 } 189 190 191 static struct wpabuf * p2p_group_build_probe_resp_ie(struct p2p_group *group) 192 { 193 u8 *group_info; 194 struct wpabuf *ie; 195 struct p2p_group_member *m; 196 u8 *len; 197 198 ie = wpabuf_alloc(257); 199 if (ie == NULL) 200 return NULL; 201 202 len = p2p_buf_add_ie_hdr(ie); 203 204 p2p_group_add_common_ies(group, ie); 205 p2p_group_add_noa(ie, group->noa); 206 207 /* P2P Device Info */ 208 p2p_buf_add_device_info(ie, group->p2p, NULL); 209 210 /* P2P Group Info */ 211 group_info = wpabuf_put(ie, 0); 212 wpabuf_put_u8(ie, P2P_ATTR_GROUP_INFO); 213 wpabuf_put_le16(ie, 0); /* Length to be filled */ 214 for (m = group->members; m; m = m->next) 215 p2p_client_info(ie, m); 216 WPA_PUT_LE16(group_info + 1, 217 (u8 *) wpabuf_put(ie, 0) - group_info - 3); 218 219 p2p_buf_update_ie_hdr(ie, len); 220 return ie; 221 } 222 223 224 static void p2p_group_update_ies(struct p2p_group *group) 225 { 226 struct wpabuf *beacon_ie; 227 struct wpabuf *probe_resp_ie; 228 229 probe_resp_ie = p2p_group_build_probe_resp_ie(group); 230 if (probe_resp_ie == NULL) 231 return; 232 wpa_hexdump_buf(MSG_MSGDUMP, "P2P: Update GO Probe Response P2P IE", 233 probe_resp_ie); 234 235 if (group->beacon_update) { 236 beacon_ie = p2p_group_build_beacon_ie(group); 237 if (beacon_ie) 238 group->beacon_update = 0; 239 wpa_hexdump_buf(MSG_MSGDUMP, "P2P: Update GO Beacon P2P IE", 240 beacon_ie); 241 } else 242 beacon_ie = NULL; 243 244 group->cfg->ie_update(group->cfg->cb_ctx, beacon_ie, probe_resp_ie); 245 } 246 247 248 /** 249 * p2p_build_client_info - Build P2P Client Info Descriptor 250 * @addr: MAC address of the peer device 251 * @p2p_ie: P2P IE from (Re)Association Request 252 * @dev_capab: Buffer for returning Device Capability 253 * @dev_addr: Buffer for returning P2P Device Address 254 * Returns: P2P Client Info Descriptor or %NULL on failure 255 * 256 * This function builds P2P Client Info Descriptor based on the information 257 * available from (Re)Association Request frame. Group owner can use this to 258 * build the P2P Group Info attribute for Probe Response frames. 259 */ 260 static struct wpabuf * p2p_build_client_info(const u8 *addr, 261 struct wpabuf *p2p_ie, 262 u8 *dev_capab, u8 *dev_addr) 263 { 264 const u8 *spos; 265 struct p2p_message msg; 266 u8 *len_pos; 267 struct wpabuf *buf; 268 269 if (p2p_ie == NULL) 270 return NULL; 271 272 os_memset(&msg, 0, sizeof(msg)); 273 if (p2p_parse_p2p_ie(p2p_ie, &msg) || 274 msg.capability == NULL || msg.p2p_device_info == NULL) 275 return NULL; 276 277 buf = wpabuf_alloc(ETH_ALEN + 1 + 1 + msg.p2p_device_info_len); 278 if (buf == NULL) 279 return NULL; 280 281 *dev_capab = msg.capability[0]; 282 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN); 283 284 spos = msg.p2p_device_info; /* P2P Device address */ 285 286 /* P2P Client Info Descriptor */ 287 /* Length to be set */ 288 len_pos = wpabuf_put(buf, 1); 289 /* P2P Device address */ 290 wpabuf_put_data(buf, spos, ETH_ALEN); 291 /* P2P Interface address */ 292 wpabuf_put_data(buf, addr, ETH_ALEN); 293 /* Device Capability Bitmap */ 294 wpabuf_put_u8(buf, msg.capability[0]); 295 /* 296 * Config Methods, Primary Device Type, Number of Secondary Device 297 * Types, Secondary Device Type List, Device Name copied from 298 * Device Info 299 */ 300 wpabuf_put_data(buf, spos + ETH_ALEN, 301 msg.p2p_device_info_len - ETH_ALEN); 302 303 *len_pos = wpabuf_len(buf) - 1; 304 305 306 return buf; 307 } 308 309 310 static int p2p_group_remove_member(struct p2p_group *group, const u8 *addr) 311 { 312 struct p2p_group_member *m, *prev; 313 314 if (group == NULL) 315 return 0; 316 317 m = group->members; 318 prev = NULL; 319 while (m) { 320 if (os_memcmp(m->addr, addr, ETH_ALEN) == 0) 321 break; 322 prev = m; 323 m = m->next; 324 } 325 326 if (m == NULL) 327 return 0; 328 329 if (prev) 330 prev->next = m->next; 331 else 332 group->members = m->next; 333 p2p_group_free_member(m); 334 group->num_members--; 335 336 return 1; 337 } 338 339 340 int p2p_group_notif_assoc(struct p2p_group *group, const u8 *addr, 341 const u8 *ie, size_t len) 342 { 343 struct p2p_group_member *m; 344 345 if (group == NULL) 346 return -1; 347 348 m = os_zalloc(sizeof(*m)); 349 if (m == NULL) 350 return -1; 351 os_memcpy(m->addr, addr, ETH_ALEN); 352 m->p2p_ie = ieee802_11_vendor_ie_concat(ie, len, P2P_IE_VENDOR_TYPE); 353 if (m->p2p_ie) { 354 m->client_info = p2p_build_client_info(addr, m->p2p_ie, 355 &m->dev_capab, 356 m->dev_addr); 357 } 358 359 p2p_group_remove_member(group, addr); 360 361 m->next = group->members; 362 group->members = m; 363 group->num_members++; 364 wpa_msg(group->p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Add client " MACSTR 365 " to group (p2p=%d client_info=%d); num_members=%u/%u", 366 MAC2STR(addr), m->p2p_ie ? 1 : 0, m->client_info ? 1 : 0, 367 group->num_members, group->cfg->max_clients); 368 if (group->num_members == group->cfg->max_clients) 369 group->beacon_update = 1; 370 p2p_group_update_ies(group); 371 if (group->num_members == 1) 372 group->cfg->idle_update(group->cfg->cb_ctx, 0); 373 374 return 0; 375 } 376 377 378 struct wpabuf * p2p_group_assoc_resp_ie(struct p2p_group *group, u8 status) 379 { 380 struct wpabuf *resp; 381 u8 *rlen; 382 383 /* 384 * (Re)Association Response - P2P IE 385 * Status attribute (shall be present when association request is 386 * denied) 387 * Extended Listen Timing (may be present) 388 */ 389 resp = wpabuf_alloc(20); 390 if (resp == NULL) 391 return NULL; 392 rlen = p2p_buf_add_ie_hdr(resp); 393 if (status != P2P_SC_SUCCESS) 394 p2p_buf_add_status(resp, status); 395 p2p_buf_update_ie_hdr(resp, rlen); 396 397 return resp; 398 } 399 400 401 void p2p_group_notif_disassoc(struct p2p_group *group, const u8 *addr) 402 { 403 if (p2p_group_remove_member(group, addr)) { 404 wpa_msg(group->p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Remove " 405 "client " MACSTR " from group; num_members=%u/%u", 406 MAC2STR(addr), group->num_members, 407 group->cfg->max_clients); 408 if (group->num_members == group->cfg->max_clients - 1) 409 group->beacon_update = 1; 410 p2p_group_update_ies(group); 411 if (group->num_members == 0) 412 group->cfg->idle_update(group->cfg->cb_ctx, 1); 413 } 414 } 415 416 417 /** 418 * p2p_match_dev_type_member - Match client device type with requested type 419 * @m: Group member 420 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs) 421 * Returns: 1 on match, 0 on mismatch 422 * 423 * This function can be used to match the Requested Device Type attribute in 424 * WPS IE with the device types of a group member for deciding whether a GO 425 * should reply to a Probe Request frame. 426 */ 427 static int p2p_match_dev_type_member(struct p2p_group_member *m, 428 struct wpabuf *wps) 429 { 430 const u8 *pos, *end; 431 struct wps_parse_attr attr; 432 u8 num_sec; 433 434 if (m->client_info == NULL || wps == NULL) 435 return 0; 436 437 pos = wpabuf_head(m->client_info); 438 end = pos + wpabuf_len(m->client_info); 439 440 pos += 1 + 2 * ETH_ALEN + 1 + 2; 441 if (end - pos < WPS_DEV_TYPE_LEN + 1) 442 return 0; 443 444 if (wps_parse_msg(wps, &attr)) 445 return 1; /* assume no Requested Device Type attributes */ 446 447 if (attr.num_req_dev_type == 0) 448 return 1; /* no Requested Device Type attributes -> match */ 449 450 if (dev_type_list_match(pos, attr.req_dev_type, attr.num_req_dev_type)) 451 return 1; /* Match with client Primary Device Type */ 452 453 pos += WPS_DEV_TYPE_LEN; 454 num_sec = *pos++; 455 if (end - pos < num_sec * WPS_DEV_TYPE_LEN) 456 return 0; 457 while (num_sec > 0) { 458 num_sec--; 459 if (dev_type_list_match(pos, attr.req_dev_type, 460 attr.num_req_dev_type)) 461 return 1; /* Match with client Secondary Device Type */ 462 pos += WPS_DEV_TYPE_LEN; 463 } 464 465 /* No matching device type found */ 466 return 0; 467 } 468 469 470 int p2p_group_match_dev_type(struct p2p_group *group, struct wpabuf *wps) 471 { 472 struct p2p_group_member *m; 473 474 if (p2p_match_dev_type(group->p2p, wps)) 475 return 1; /* Match with own device type */ 476 477 for (m = group->members; m; m = m->next) { 478 if (p2p_match_dev_type_member(m, wps)) 479 return 1; /* Match with group client device type */ 480 } 481 482 /* No match with Requested Device Type */ 483 return 0; 484 } 485 486 487 int p2p_group_match_dev_id(struct p2p_group *group, struct wpabuf *p2p) 488 { 489 struct p2p_group_member *m; 490 struct p2p_message msg; 491 492 os_memset(&msg, 0, sizeof(msg)); 493 if (p2p_parse_p2p_ie(p2p, &msg)) 494 return 1; /* Failed to parse - assume no filter on Device ID */ 495 496 if (!msg.device_id) 497 return 1; /* No filter on Device ID */ 498 499 if (os_memcmp(msg.device_id, group->p2p->cfg->dev_addr, ETH_ALEN) == 0) 500 return 1; /* Match with our P2P Device Address */ 501 502 for (m = group->members; m; m = m->next) { 503 if (os_memcmp(msg.device_id, m->dev_addr, ETH_ALEN) == 0) 504 return 1; /* Match with group client P2P Device Address */ 505 } 506 507 /* No match with Device ID */ 508 return 0; 509 } 510 511 512 void p2p_group_notif_formation_done(struct p2p_group *group) 513 { 514 if (group == NULL) 515 return; 516 group->group_formation = 0; 517 group->beacon_update = 1; 518 p2p_group_update_ies(group); 519 } 520 521 522 int p2p_group_notif_noa(struct p2p_group *group, const u8 *noa, 523 size_t noa_len) 524 { 525 if (noa == NULL) { 526 wpabuf_free(group->noa); 527 group->noa = NULL; 528 } else { 529 if (group->noa) { 530 if (wpabuf_size(group->noa) >= noa_len) { 531 group->noa->used = 0; 532 wpabuf_put_data(group->noa, noa, noa_len); 533 } else { 534 wpabuf_free(group->noa); 535 group->noa = NULL; 536 } 537 } 538 539 if (!group->noa) { 540 group->noa = wpabuf_alloc_copy(noa, noa_len); 541 if (group->noa == NULL) 542 return -1; 543 } 544 } 545 546 group->beacon_update = 1; 547 p2p_group_update_ies(group); 548 return 0; 549 } 550 551 552 static struct p2p_group_member * p2p_group_get_client(struct p2p_group *group, 553 const u8 *dev_id) 554 { 555 struct p2p_group_member *m; 556 557 for (m = group->members; m; m = m->next) { 558 if (os_memcmp(dev_id, m->dev_addr, ETH_ALEN) == 0) 559 return m; 560 } 561 562 return NULL; 563 } 564 565 566 static struct p2p_group_member * p2p_group_get_client_iface( 567 struct p2p_group *group, const u8 *interface_addr) 568 { 569 struct p2p_group_member *m; 570 571 for (m = group->members; m; m = m->next) { 572 if (os_memcmp(interface_addr, m->addr, ETH_ALEN) == 0) 573 return m; 574 } 575 576 return NULL; 577 } 578 579 580 const u8 * p2p_group_get_dev_addr(struct p2p_group *group, const u8 *addr) 581 { 582 struct p2p_group_member *m; 583 584 if (group == NULL) 585 return NULL; 586 m = p2p_group_get_client_iface(group, addr); 587 if (m && !is_zero_ether_addr(m->dev_addr)) 588 return m->dev_addr; 589 return NULL; 590 } 591 592 593 static struct wpabuf * p2p_build_go_disc_req(void) 594 { 595 struct wpabuf *buf; 596 597 buf = wpabuf_alloc(100); 598 if (buf == NULL) 599 return NULL; 600 601 p2p_buf_add_action_hdr(buf, P2P_GO_DISC_REQ, 0); 602 603 return buf; 604 } 605 606 607 int p2p_group_go_discover(struct p2p_group *group, const u8 *dev_id, 608 const u8 *searching_dev, int rx_freq) 609 { 610 struct p2p_group_member *m; 611 struct wpabuf *req; 612 struct p2p_data *p2p = group->p2p; 613 int freq; 614 615 m = p2p_group_get_client(group, dev_id); 616 if (m == NULL || m->client_info == NULL) { 617 wpa_printf(MSG_DEBUG, "P2P: Requested client was not in this " 618 "group " MACSTR, 619 MAC2STR(group->cfg->interface_addr)); 620 return -1; 621 } 622 623 if (!(m->dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) { 624 wpa_printf(MSG_DEBUG, "P2P: Requested client does not support " 625 "client discoverability"); 626 return -1; 627 } 628 629 wpa_printf(MSG_DEBUG, "P2P: Schedule GO Discoverability Request to be " 630 "sent to " MACSTR, MAC2STR(dev_id)); 631 632 req = p2p_build_go_disc_req(); 633 if (req == NULL) 634 return -1; 635 636 /* TODO: Should really use group operating frequency here */ 637 freq = rx_freq; 638 639 p2p->pending_action_state = P2P_PENDING_GO_DISC_REQ; 640 if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, m->addr, 641 group->cfg->interface_addr, 642 group->cfg->interface_addr, 643 wpabuf_head(req), wpabuf_len(req), 200) < 0) 644 { 645 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 646 "P2P: Failed to send Action frame"); 647 } 648 649 wpabuf_free(req); 650 651 return 0; 652 } 653 654 655 const u8 * p2p_group_get_interface_addr(struct p2p_group *group) 656 { 657 return group->cfg->interface_addr; 658 } 659 660 661 u8 p2p_group_presence_req(struct p2p_group *group, 662 const u8 *client_interface_addr, 663 const u8 *noa, size_t noa_len) 664 { 665 struct p2p_group_member *m; 666 u8 curr_noa[50]; 667 int curr_noa_len; 668 669 m = p2p_group_get_client_iface(group, client_interface_addr); 670 if (m == NULL || m->client_info == NULL) { 671 wpa_printf(MSG_DEBUG, "P2P: Client was not in this group"); 672 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE; 673 } 674 675 wpa_hexdump(MSG_DEBUG, "P2P: Presence Request NoA", noa, noa_len); 676 677 if (group->p2p->cfg->get_noa) 678 curr_noa_len = group->p2p->cfg->get_noa( 679 group->p2p->cfg->cb_ctx, group->cfg->interface_addr, 680 curr_noa, sizeof(curr_noa)); 681 else 682 curr_noa_len = -1; 683 if (curr_noa_len < 0) 684 wpa_printf(MSG_DEBUG, "P2P: Failed to fetch current NoA"); 685 else if (curr_noa_len == 0) 686 wpa_printf(MSG_DEBUG, "P2P: No NoA being advertized"); 687 else 688 wpa_hexdump(MSG_DEBUG, "P2P: Current NoA", curr_noa, 689 curr_noa_len); 690 691 /* TODO: properly process request and store copy */ 692 if (curr_noa_len > 0 || curr_noa_len == -1) 693 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE; 694 695 return P2P_SC_SUCCESS; 696 } 697 698 699 unsigned int p2p_get_group_num_members(struct p2p_group *group) 700 { 701 return group->num_members; 702 } 703 704 705 const u8 * p2p_iterate_group_members(struct p2p_group *group, void **next) 706 { 707 struct p2p_group_member *iter = *next; 708 709 if (!iter) 710 iter = group->members; 711 else 712 iter = iter->next; 713 714 *next = iter; 715 716 if (!iter) 717 return NULL; 718 719 return iter->addr; 720 } 721 722 723 int p2p_group_is_client_connected(struct p2p_group *group, const u8 *dev_addr) 724 { 725 struct p2p_group_member *m; 726 727 for (m = group->members; m; m = m->next) { 728 if (os_memcmp(m->dev_addr, dev_addr, ETH_ALEN) == 0) 729 return 1; 730 } 731 732 return 0; 733 } 734