1 /* 2 * Wi-Fi Direct - P2P Group Owner Negotiation 3 * Copyright (c) 2009-2010, Atheros Communications 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 as 7 * published by the Free Software Foundation. 8 * 9 * Alternatively, this software may be distributed under the terms of BSD 10 * license. 11 * 12 * See README and COPYING for more details. 13 */ 14 15 #include "includes.h" 16 17 #include "common.h" 18 #include "common/ieee802_11_defs.h" 19 #include "wps/wps_defs.h" 20 #include "p2p_i.h" 21 #include "p2p.h" 22 23 24 static int p2p_go_det(u8 own_intent, u8 peer_value) 25 { 26 u8 peer_intent = peer_value >> 1; 27 if (own_intent == peer_intent) { 28 if (own_intent == P2P_MAX_GO_INTENT) 29 return -1; /* both devices want to become GO */ 30 31 /* Use tie breaker bit to determine GO */ 32 return (peer_value & 0x01) ? 0 : 1; 33 } 34 35 return own_intent > peer_intent; 36 } 37 38 39 int p2p_peer_channels_check(struct p2p_data *p2p, struct p2p_channels *own, 40 struct p2p_device *dev, 41 const u8 *channel_list, size_t channel_list_len) 42 { 43 const u8 *pos, *end; 44 struct p2p_channels *ch; 45 size_t channels; 46 struct p2p_channels intersection; 47 48 ch = &dev->channels; 49 os_memset(ch, 0, sizeof(*ch)); 50 pos = channel_list; 51 end = channel_list + channel_list_len; 52 53 if (end - pos < 3) 54 return -1; 55 os_memcpy(dev->country, pos, 3); 56 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Peer country", pos, 3); 57 if (pos[2] != 0x04 && os_memcmp(pos, p2p->cfg->country, 2) != 0) { 58 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, 59 "P2P: Mismatching country (ours=%c%c peer's=%c%c)", 60 p2p->cfg->country[0], p2p->cfg->country[1], 61 pos[0], pos[1]); 62 return -1; 63 } 64 pos += 3; 65 66 while (pos + 2 < end) { 67 struct p2p_reg_class *cl = &ch->reg_class[ch->reg_classes]; 68 cl->reg_class = *pos++; 69 if (pos + 1 + pos[0] > end) { 70 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, 71 "P2P: Invalid peer Channel List"); 72 return -1; 73 } 74 channels = *pos++; 75 cl->channels = channels > P2P_MAX_REG_CLASS_CHANNELS ? 76 P2P_MAX_REG_CLASS_CHANNELS : channels; 77 os_memcpy(cl->channel, pos, cl->channels); 78 pos += channels; 79 ch->reg_classes++; 80 if (ch->reg_classes == P2P_MAX_REG_CLASSES) 81 break; 82 } 83 84 p2p_channels_intersect(own, &dev->channels, &intersection); 85 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Own reg_classes %d " 86 "peer reg_classes %d intersection reg_classes %d", 87 (int) own->reg_classes, 88 (int) dev->channels.reg_classes, 89 (int) intersection.reg_classes); 90 if (intersection.reg_classes == 0) { 91 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, 92 "P2P: No common channels found"); 93 return -1; 94 } 95 return 0; 96 } 97 98 99 static int p2p_peer_channels(struct p2p_data *p2p, struct p2p_device *dev, 100 const u8 *channel_list, size_t channel_list_len) 101 { 102 return p2p_peer_channels_check(p2p, &p2p->channels, dev, 103 channel_list, channel_list_len); 104 } 105 106 107 static u16 p2p_wps_method_pw_id(enum p2p_wps_method wps_method) 108 { 109 switch (wps_method) { 110 case WPS_PIN_LABEL: 111 return DEV_PW_DEFAULT; 112 case WPS_PIN_DISPLAY: 113 return DEV_PW_REGISTRAR_SPECIFIED; 114 case WPS_PIN_KEYPAD: 115 return DEV_PW_USER_SPECIFIED; 116 case WPS_PBC: 117 return DEV_PW_PUSHBUTTON; 118 default: 119 return DEV_PW_DEFAULT; 120 } 121 } 122 123 124 static const char * p2p_wps_method_str(enum p2p_wps_method wps_method) 125 { 126 switch (wps_method) { 127 case WPS_PIN_LABEL: 128 return "Label"; 129 case WPS_PIN_DISPLAY: 130 return "Display"; 131 case WPS_PIN_KEYPAD: 132 return "Keypad"; 133 case WPS_PBC: 134 return "PBC"; 135 default: 136 return "??"; 137 } 138 } 139 140 141 static struct wpabuf * p2p_build_go_neg_req(struct p2p_data *p2p, 142 struct p2p_device *peer) 143 { 144 struct wpabuf *buf; 145 u8 *len; 146 u8 group_capab; 147 148 buf = wpabuf_alloc(1000); 149 if (buf == NULL) 150 return NULL; 151 152 peer->dialog_token++; 153 if (peer->dialog_token == 0) 154 peer->dialog_token = 1; 155 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_REQ, peer->dialog_token); 156 157 len = p2p_buf_add_ie_hdr(buf); 158 group_capab = 0; 159 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) 160 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP; 161 if (p2p->cross_connect) 162 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN; 163 if (p2p->cfg->p2p_intra_bss) 164 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST; 165 p2p_buf_add_capability(buf, p2p->dev_capab, group_capab); 166 p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | 167 p2p->next_tie_breaker); 168 p2p->next_tie_breaker = !p2p->next_tie_breaker; 169 p2p_buf_add_config_timeout(buf, 100, 20); 170 p2p_buf_add_listen_channel(buf, p2p->cfg->country, p2p->cfg->reg_class, 171 p2p->cfg->channel); 172 if (p2p->ext_listen_interval) 173 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period, 174 p2p->ext_listen_interval); 175 p2p_buf_add_intended_addr(buf, p2p->intended_addr); 176 p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels); 177 p2p_buf_add_device_info(buf, p2p, peer); 178 p2p_buf_add_operating_channel(buf, p2p->cfg->country, 179 p2p->op_reg_class, p2p->op_channel); 180 p2p_buf_update_ie_hdr(buf, len); 181 182 /* WPS IE with Device Password ID attribute */ 183 p2p_build_wps_ie(p2p, buf, p2p_wps_method_pw_id(peer->wps_method), 0); 184 185 return buf; 186 } 187 188 189 int p2p_connect_send(struct p2p_data *p2p, struct p2p_device *dev) 190 { 191 struct wpabuf *req; 192 int freq; 193 194 freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq; 195 if (freq <= 0) { 196 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 197 "P2P: No Listen/Operating frequency known for the " 198 "peer " MACSTR " to send GO Negotiation Request", 199 MAC2STR(dev->info.p2p_device_addr)); 200 return -1; 201 } 202 203 req = p2p_build_go_neg_req(p2p, dev); 204 if (req == NULL) 205 return -1; 206 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 207 "P2P: Sending GO Negotiation Request"); 208 p2p_set_state(p2p, P2P_CONNECT); 209 p2p->pending_action_state = P2P_PENDING_GO_NEG_REQUEST; 210 p2p->go_neg_peer = dev; 211 dev->flags |= P2P_DEV_WAIT_GO_NEG_RESPONSE; 212 dev->connect_reqs++; 213 if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr, 214 p2p->cfg->dev_addr, dev->info.p2p_device_addr, 215 wpabuf_head(req), wpabuf_len(req), 200) < 0) { 216 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 217 "P2P: Failed to send Action frame"); 218 /* Use P2P find to recover and retry */ 219 p2p_set_timeout(p2p, 0, 0); 220 } 221 222 wpabuf_free(req); 223 224 return 0; 225 } 226 227 228 static struct wpabuf * p2p_build_go_neg_resp(struct p2p_data *p2p, 229 struct p2p_device *peer, 230 u8 dialog_token, u8 status, 231 u8 tie_breaker) 232 { 233 struct wpabuf *buf; 234 u8 *len; 235 u8 group_capab; 236 237 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 238 "P2P: Building GO Negotiation Response"); 239 buf = wpabuf_alloc(1000); 240 if (buf == NULL) 241 return NULL; 242 243 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_RESP, dialog_token); 244 245 len = p2p_buf_add_ie_hdr(buf); 246 p2p_buf_add_status(buf, status); 247 group_capab = 0; 248 if (peer && peer->go_state == LOCAL_GO) { 249 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) 250 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP; 251 if (p2p->cross_connect) 252 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN; 253 if (p2p->cfg->p2p_intra_bss) 254 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST; 255 } 256 p2p_buf_add_capability(buf, p2p->dev_capab, group_capab); 257 p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | tie_breaker); 258 p2p_buf_add_config_timeout(buf, 100, 20); 259 if (peer && peer->go_state == REMOTE_GO) { 260 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Omit Operating " 261 "Channel attribute"); 262 } else { 263 p2p_buf_add_operating_channel(buf, p2p->cfg->country, 264 p2p->op_reg_class, 265 p2p->op_channel); 266 } 267 p2p_buf_add_intended_addr(buf, p2p->intended_addr); 268 if (status || peer == NULL) { 269 p2p_buf_add_channel_list(buf, p2p->cfg->country, 270 &p2p->channels); 271 } else if (peer->go_state == REMOTE_GO) { 272 p2p_buf_add_channel_list(buf, p2p->cfg->country, 273 &p2p->channels); 274 } else { 275 struct p2p_channels res; 276 p2p_channels_intersect(&p2p->channels, &peer->channels, 277 &res); 278 p2p_buf_add_channel_list(buf, p2p->cfg->country, &res); 279 } 280 p2p_buf_add_device_info(buf, p2p, peer); 281 if (peer && peer->go_state == LOCAL_GO) { 282 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid, 283 p2p->ssid_len); 284 } 285 p2p_buf_update_ie_hdr(buf, len); 286 287 /* WPS IE with Device Password ID attribute */ 288 p2p_build_wps_ie(p2p, buf, 289 p2p_wps_method_pw_id(peer ? peer->wps_method : 290 WPS_NOT_READY), 0); 291 292 return buf; 293 } 294 295 296 static void p2p_reselect_channel(struct p2p_data *p2p, 297 struct p2p_channels *intersection) 298 { 299 struct p2p_reg_class *cl; 300 int freq; 301 u8 op_reg_class, op_channel; 302 303 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Selected operating " 304 "channel (reg_class %u channel %u) not acceptable to the " 305 "peer", p2p->op_reg_class, p2p->op_channel); 306 307 /* First, try to pick the best channel from another band */ 308 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->op_reg_class, 309 p2p->op_channel); 310 if (freq >= 2400 && freq < 2500 && p2p->best_freq_5 > 0 && 311 p2p_freq_to_channel(p2p->cfg->country, p2p->best_freq_5, 312 &op_reg_class, &op_channel) == 0 && 313 p2p_channels_includes(intersection, op_reg_class, op_channel)) { 314 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick best 5 GHz " 315 "channel (reg_class %u channel %u) from intersection", 316 op_reg_class, op_channel); 317 p2p->op_reg_class = op_reg_class; 318 p2p->op_channel = op_channel; 319 return; 320 } 321 322 if (freq >= 4900 && freq < 6000 && p2p->best_freq_24 > 0 && 323 p2p_freq_to_channel(p2p->cfg->country, p2p->best_freq_24, 324 &op_reg_class, &op_channel) == 0 && 325 p2p_channels_includes(intersection, op_reg_class, op_channel)) { 326 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick best 2.4 GHz " 327 "channel (reg_class %u channel %u) from intersection", 328 op_reg_class, op_channel); 329 p2p->op_reg_class = op_reg_class; 330 p2p->op_channel = op_channel; 331 return; 332 } 333 334 /* 335 * Fall back to whatever is included in the channel intersection since 336 * no better options seems to be available. 337 */ 338 cl = &intersection->reg_class[0]; 339 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick another channel " 340 "(reg_class %u channel %u) from intersection", 341 cl->reg_class, cl->channel[0]); 342 p2p->op_reg_class = cl->reg_class; 343 p2p->op_channel = cl->channel[0]; 344 } 345 346 347 void p2p_process_go_neg_req(struct p2p_data *p2p, const u8 *sa, 348 const u8 *data, size_t len, int rx_freq) 349 { 350 struct p2p_device *dev = NULL; 351 struct wpabuf *resp; 352 struct p2p_message msg; 353 u8 status = P2P_SC_FAIL_INVALID_PARAMS; 354 int tie_breaker = 0; 355 int freq; 356 357 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 358 "P2P: Received GO Negotiation Request from " MACSTR 359 "(freq=%d)", MAC2STR(sa), rx_freq); 360 361 if (p2p_parse(data, len, &msg)) 362 return; 363 364 if (!msg.capability) { 365 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 366 "P2P: Mandatory Capability attribute missing from GO " 367 "Negotiation Request"); 368 #ifdef CONFIG_P2P_STRICT 369 goto fail; 370 #endif /* CONFIG_P2P_STRICT */ 371 } 372 373 if (msg.go_intent) 374 tie_breaker = *msg.go_intent & 0x01; 375 else { 376 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 377 "P2P: Mandatory GO Intent attribute missing from GO " 378 "Negotiation Request"); 379 #ifdef CONFIG_P2P_STRICT 380 goto fail; 381 #endif /* CONFIG_P2P_STRICT */ 382 } 383 384 if (!msg.config_timeout) { 385 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 386 "P2P: Mandatory Configuration Timeout attribute " 387 "missing from GO Negotiation Request"); 388 #ifdef CONFIG_P2P_STRICT 389 goto fail; 390 #endif /* CONFIG_P2P_STRICT */ 391 } 392 393 if (!msg.listen_channel) { 394 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 395 "P2P: No Listen Channel attribute received"); 396 goto fail; 397 } 398 if (!msg.operating_channel) { 399 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 400 "P2P: No Operating Channel attribute received"); 401 goto fail; 402 } 403 if (!msg.channel_list) { 404 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 405 "P2P: No Channel List attribute received"); 406 goto fail; 407 } 408 if (!msg.intended_addr) { 409 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 410 "P2P: No Intended P2P Interface Address attribute " 411 "received"); 412 goto fail; 413 } 414 if (!msg.p2p_device_info) { 415 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 416 "P2P: No P2P Device Info attribute received"); 417 goto fail; 418 } 419 420 if (os_memcmp(msg.p2p_device_addr, sa, ETH_ALEN) != 0) { 421 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 422 "P2P: Unexpected GO Negotiation Request SA=" MACSTR 423 " != dev_addr=" MACSTR, 424 MAC2STR(sa), MAC2STR(msg.p2p_device_addr)); 425 goto fail; 426 } 427 428 dev = p2p_get_device(p2p, sa); 429 430 if (msg.status && *msg.status) { 431 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 432 "P2P: Unexpected Status attribute (%d) in GO " 433 "Negotiation Request", *msg.status); 434 goto fail; 435 } 436 437 if (dev == NULL) 438 dev = p2p_add_dev_from_go_neg_req(p2p, sa, &msg); 439 else if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) 440 p2p_add_dev_info(p2p, sa, dev, &msg); 441 if (dev && dev->flags & P2P_DEV_USER_REJECTED) { 442 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 443 "P2P: User has rejected this peer"); 444 status = P2P_SC_FAIL_REJECTED_BY_USER; 445 } else if (dev == NULL || dev->wps_method == WPS_NOT_READY) { 446 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 447 "P2P: Not ready for GO negotiation with " MACSTR, 448 MAC2STR(sa)); 449 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; 450 if (dev) 451 dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE; 452 p2p->cfg->go_neg_req_rx(p2p->cfg->cb_ctx, sa, 453 msg.dev_password_id); 454 } else if (p2p->go_neg_peer && p2p->go_neg_peer != dev) { 455 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 456 "P2P: Already in Group Formation with another peer"); 457 status = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE; 458 } else { 459 int go; 460 461 if (!p2p->go_neg_peer) { 462 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting " 463 "GO Negotiation with previously authorized " 464 "peer"); 465 if (!(dev->flags & P2P_DEV_FORCE_FREQ)) { 466 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 467 "P2P: Use default channel settings"); 468 p2p->op_reg_class = p2p->cfg->op_reg_class; 469 p2p->op_channel = p2p->cfg->op_channel; 470 os_memcpy(&p2p->channels, &p2p->cfg->channels, 471 sizeof(struct p2p_channels)); 472 } else { 473 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 474 "P2P: Use previously configured " 475 "forced channel settings"); 476 } 477 } 478 479 dev->flags &= ~P2P_DEV_NOT_YET_READY; 480 481 if (!msg.go_intent) { 482 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 483 "P2P: No GO Intent attribute received"); 484 goto fail; 485 } 486 if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) { 487 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 488 "P2P: Invalid GO Intent value (%u) received", 489 *msg.go_intent >> 1); 490 goto fail; 491 } 492 493 if (dev->go_neg_req_sent && 494 #ifdef ANDROID_BRCM_P2P_PATCH 495 /* P2P_ADDR: compare against the p2p device address. The own mac 496 address may not not be the actual p2p device address, if you 497 are using a virtual interface. 498 */ 499 os_memcmp(sa, p2p->cfg->p2p_dev_addr, ETH_ALEN) > 0) { 500 #else 501 os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) > 0) { 502 #endif 503 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 504 "P2P: Do not reply since peer has higher " 505 "address and GO Neg Request already sent"); 506 p2p_parse_free(&msg); 507 return; 508 } 509 510 go = p2p_go_det(p2p->go_intent, *msg.go_intent); 511 if (go < 0) { 512 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 513 "P2P: Incompatible GO Intent"); 514 status = P2P_SC_FAIL_BOTH_GO_INTENT_15; 515 goto fail; 516 } 517 518 if (p2p_peer_channels(p2p, dev, msg.channel_list, 519 msg.channel_list_len) < 0) { 520 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 521 "P2P: No common channels found"); 522 status = P2P_SC_FAIL_NO_COMMON_CHANNELS; 523 goto fail; 524 } 525 526 switch (msg.dev_password_id) { 527 case DEV_PW_DEFAULT: 528 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 529 "P2P: PIN from peer Label"); 530 if (dev->wps_method != WPS_PIN_KEYPAD) { 531 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 532 "P2P: We have wps_method=%s -> " 533 "incompatible", 534 p2p_wps_method_str(dev->wps_method)); 535 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD; 536 goto fail; 537 } 538 break; 539 case DEV_PW_REGISTRAR_SPECIFIED: 540 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 541 "P2P: PIN from peer Display"); 542 if (dev->wps_method != WPS_PIN_KEYPAD) { 543 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 544 "P2P: We have wps_method=%s -> " 545 "incompatible", 546 p2p_wps_method_str(dev->wps_method)); 547 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD; 548 goto fail; 549 } 550 break; 551 case DEV_PW_USER_SPECIFIED: 552 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 553 "P2P: Peer entered PIN on Keypad"); 554 if (dev->wps_method != WPS_PIN_LABEL && 555 dev->wps_method != WPS_PIN_DISPLAY) { 556 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 557 "P2P: We have wps_method=%s -> " 558 "incompatible", 559 p2p_wps_method_str(dev->wps_method)); 560 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD; 561 goto fail; 562 } 563 break; 564 case DEV_PW_PUSHBUTTON: 565 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 566 "P2P: Peer using pushbutton"); 567 if (dev->wps_method != WPS_PBC) { 568 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 569 "P2P: We have wps_method=%s -> " 570 "incompatible", 571 p2p_wps_method_str(dev->wps_method)); 572 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD; 573 goto fail; 574 } 575 break; 576 default: 577 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 578 "P2P: Unsupported Device Password ID %d", 579 msg.dev_password_id); 580 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD; 581 goto fail; 582 } 583 584 if (go) { 585 struct p2p_channels intersection; 586 size_t i; 587 p2p_channels_intersect(&p2p->channels, &dev->channels, 588 &intersection); 589 if (intersection.reg_classes == 0 || 590 intersection.reg_class[0].channels == 0) { 591 status = P2P_SC_FAIL_NO_COMMON_CHANNELS; 592 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 593 "P2P: No common channels found"); 594 goto fail; 595 } 596 for (i = 0; i < intersection.reg_classes; i++) { 597 struct p2p_reg_class *c; 598 c = &intersection.reg_class[i]; 599 wpa_printf(MSG_DEBUG, "P2P: reg_class %u", 600 c->reg_class); 601 wpa_hexdump(MSG_DEBUG, "P2P: channels", 602 c->channel, c->channels); 603 } 604 if (!p2p_channels_includes(&intersection, 605 p2p->op_reg_class, 606 p2p->op_channel)) 607 p2p_reselect_channel(p2p, &intersection); 608 609 p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len); 610 } 611 612 dev->go_state = go ? LOCAL_GO : REMOTE_GO; 613 dev->oper_freq = p2p_channel_to_freq((const char *) 614 msg.operating_channel, 615 msg.operating_channel[3], 616 msg.operating_channel[4]); 617 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating " 618 "channel preference: %d MHz", dev->oper_freq); 619 620 if (msg.config_timeout) { 621 dev->go_timeout = msg.config_timeout[0]; 622 dev->client_timeout = msg.config_timeout[1]; 623 } 624 625 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 626 "P2P: GO Negotiation with " MACSTR, MAC2STR(sa)); 627 if (p2p->state != P2P_IDLE) 628 p2p_stop_find_for_freq(p2p, rx_freq); 629 p2p_set_state(p2p, P2P_GO_NEG); 630 p2p_clear_timeout(p2p); 631 dev->dialog_token = msg.dialog_token; 632 os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN); 633 p2p->go_neg_peer = dev; 634 status = P2P_SC_SUCCESS; 635 } 636 637 fail: 638 if (dev) 639 dev->status = status; 640 resp = p2p_build_go_neg_resp(p2p, dev, msg.dialog_token, status, 641 !tie_breaker); 642 p2p_parse_free(&msg); 643 if (resp == NULL) 644 return; 645 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 646 "P2P: Sending GO Negotiation Response"); 647 if (rx_freq > 0) 648 freq = rx_freq; 649 else 650 freq = p2p_channel_to_freq(p2p->cfg->country, 651 p2p->cfg->reg_class, 652 p2p->cfg->channel); 653 if (freq < 0) { 654 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 655 "P2P: Unknown regulatory class/channel"); 656 wpabuf_free(resp); 657 return; 658 } 659 if (status == P2P_SC_SUCCESS) { 660 p2p->pending_action_state = P2P_PENDING_GO_NEG_RESPONSE; 661 dev->flags |= P2P_DEV_WAIT_GO_NEG_CONFIRM; 662 } else 663 p2p->pending_action_state = 664 P2P_PENDING_GO_NEG_RESPONSE_FAILURE; 665 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr, 666 p2p->cfg->dev_addr, 667 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) { 668 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 669 "P2P: Failed to send Action frame"); 670 } 671 672 wpabuf_free(resp); 673 } 674 675 676 static struct wpabuf * p2p_build_go_neg_conf(struct p2p_data *p2p, 677 struct p2p_device *peer, 678 u8 dialog_token, u8 status, 679 const u8 *resp_chan, int go) 680 { 681 struct wpabuf *buf; 682 u8 *len; 683 struct p2p_channels res; 684 u8 group_capab; 685 686 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 687 "P2P: Building GO Negotiation Confirm"); 688 buf = wpabuf_alloc(1000); 689 if (buf == NULL) 690 return NULL; 691 692 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_CONF, dialog_token); 693 694 len = p2p_buf_add_ie_hdr(buf); 695 p2p_buf_add_status(buf, status); 696 group_capab = 0; 697 if (peer->go_state == LOCAL_GO) { 698 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) 699 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP; 700 if (p2p->cross_connect) 701 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN; 702 if (p2p->cfg->p2p_intra_bss) 703 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST; 704 } 705 p2p_buf_add_capability(buf, p2p->dev_capab, group_capab); 706 if (go || resp_chan == NULL) 707 p2p_buf_add_operating_channel(buf, p2p->cfg->country, 708 p2p->op_reg_class, 709 p2p->op_channel); 710 else 711 p2p_buf_add_operating_channel(buf, (const char *) resp_chan, 712 resp_chan[3], resp_chan[4]); 713 p2p_channels_intersect(&p2p->channels, &peer->channels, &res); 714 p2p_buf_add_channel_list(buf, p2p->cfg->country, &res); 715 if (go) { 716 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid, 717 p2p->ssid_len); 718 } 719 p2p_buf_update_ie_hdr(buf, len); 720 721 return buf; 722 } 723 724 725 void p2p_process_go_neg_resp(struct p2p_data *p2p, const u8 *sa, 726 const u8 *data, size_t len, int rx_freq) 727 { 728 struct p2p_device *dev; 729 struct wpabuf *conf; 730 int go = -1; 731 struct p2p_message msg; 732 u8 status = P2P_SC_SUCCESS; 733 int freq; 734 735 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 736 "P2P: Received GO Negotiation Response from " MACSTR 737 " (freq=%d)", MAC2STR(sa), rx_freq); 738 dev = p2p_get_device(p2p, sa); 739 if (dev == NULL || dev->wps_method == WPS_NOT_READY || 740 dev != p2p->go_neg_peer) { 741 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 742 "P2P: Not ready for GO negotiation with " MACSTR, 743 MAC2STR(sa)); 744 return; 745 } 746 747 if (p2p_parse(data, len, &msg)) 748 return; 749 750 if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE)) { 751 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 752 "P2P: Was not expecting GO Negotiation Response - " 753 "ignore"); 754 p2p_parse_free(&msg); 755 return; 756 } 757 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE; 758 759 if (msg.dialog_token != dev->dialog_token) { 760 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 761 "P2P: Unexpected Dialog Token %u (expected %u)", 762 msg.dialog_token, dev->dialog_token); 763 p2p_parse_free(&msg); 764 return; 765 } 766 767 if (!msg.status) { 768 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 769 "P2P: No Status attribute received"); 770 status = P2P_SC_FAIL_INVALID_PARAMS; 771 goto fail; 772 } 773 if (*msg.status) { 774 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 775 "P2P: GO Negotiation rejected: status %d", 776 *msg.status); 777 dev->go_neg_req_sent = 0; 778 if (*msg.status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) { 779 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 780 "P2P: Wait for the peer to become ready for " 781 "GO Negotiation"); 782 dev->flags |= P2P_DEV_NOT_YET_READY; 783 dev->wait_count = 0; 784 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE); 785 p2p_set_timeout(p2p, 0, 0); 786 } else { 787 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 788 "P2P: Stop GO Negotiation attempt"); 789 p2p_go_neg_failed(p2p, dev, *msg.status); 790 } 791 p2p->cfg->send_action_done(p2p->cfg->cb_ctx); 792 p2p_parse_free(&msg); 793 return; 794 } 795 796 if (!msg.capability) { 797 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 798 "P2P: Mandatory Capability attribute missing from GO " 799 "Negotiation Response"); 800 #ifdef CONFIG_P2P_STRICT 801 status = P2P_SC_FAIL_INVALID_PARAMS; 802 goto fail; 803 #endif /* CONFIG_P2P_STRICT */ 804 } 805 806 if (!msg.p2p_device_info) { 807 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 808 "P2P: Mandatory P2P Device Info attribute missing " 809 "from GO Negotiation Response"); 810 #ifdef CONFIG_P2P_STRICT 811 status = P2P_SC_FAIL_INVALID_PARAMS; 812 goto fail; 813 #endif /* CONFIG_P2P_STRICT */ 814 } 815 816 if (!msg.intended_addr) { 817 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 818 "P2P: No Intended P2P Interface Address attribute " 819 "received"); 820 status = P2P_SC_FAIL_INVALID_PARAMS; 821 goto fail; 822 } 823 824 if (!msg.go_intent) { 825 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 826 "P2P: No GO Intent attribute received"); 827 status = P2P_SC_FAIL_INVALID_PARAMS; 828 goto fail; 829 } 830 if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) { 831 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 832 "P2P: Invalid GO Intent value (%u) received", 833 *msg.go_intent >> 1); 834 status = P2P_SC_FAIL_INVALID_PARAMS; 835 goto fail; 836 } 837 838 go = p2p_go_det(p2p->go_intent, *msg.go_intent); 839 if (go < 0) { 840 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 841 "P2P: Incompatible GO Intent"); 842 status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS; 843 goto fail; 844 } 845 846 if (!go && msg.group_id) { 847 /* Store SSID for Provisioning step */ 848 p2p->ssid_len = msg.group_id_len - ETH_ALEN; 849 os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len); 850 } else if (!go) { 851 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 852 "P2P: Mandatory P2P Group ID attribute missing from " 853 "GO Negotiation Response"); 854 p2p->ssid_len = 0; 855 #ifdef CONFIG_P2P_STRICT 856 status = P2P_SC_FAIL_INVALID_PARAMS; 857 goto fail; 858 #endif /* CONFIG_P2P_STRICT */ 859 } 860 861 if (!msg.config_timeout) { 862 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 863 "P2P: Mandatory Configuration Timeout attribute " 864 "missing from GO Negotiation Response"); 865 #ifdef CONFIG_P2P_STRICT 866 status = P2P_SC_FAIL_INVALID_PARAMS; 867 goto fail; 868 #endif /* CONFIG_P2P_STRICT */ 869 } else { 870 dev->go_timeout = msg.config_timeout[0]; 871 dev->client_timeout = msg.config_timeout[1]; 872 } 873 874 if (!msg.operating_channel && !go) { 875 /* 876 * Note: P2P Client may omit Operating Channel attribute to 877 * indicate it does not have a preference. 878 */ 879 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 880 "P2P: No Operating Channel attribute received"); 881 status = P2P_SC_FAIL_INVALID_PARAMS; 882 goto fail; 883 } 884 if (!msg.channel_list) { 885 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 886 "P2P: No Channel List attribute received"); 887 status = P2P_SC_FAIL_INVALID_PARAMS; 888 goto fail; 889 } 890 891 if (p2p_peer_channels(p2p, dev, msg.channel_list, 892 msg.channel_list_len) < 0) { 893 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 894 "P2P: No common channels found"); 895 status = P2P_SC_FAIL_NO_COMMON_CHANNELS; 896 goto fail; 897 } 898 899 if (msg.operating_channel) { 900 dev->oper_freq = p2p_channel_to_freq((const char *) 901 msg.operating_channel, 902 msg.operating_channel[3], 903 msg.operating_channel[4]); 904 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating " 905 "channel preference: %d MHz", dev->oper_freq); 906 } else 907 dev->oper_freq = 0; 908 909 switch (msg.dev_password_id) { 910 case DEV_PW_DEFAULT: 911 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 912 "P2P: PIN from peer Label"); 913 if (dev->wps_method != WPS_PIN_KEYPAD) { 914 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 915 "P2P: We have wps_method=%s -> " 916 "incompatible", 917 p2p_wps_method_str(dev->wps_method)); 918 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD; 919 goto fail; 920 } 921 break; 922 case DEV_PW_REGISTRAR_SPECIFIED: 923 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 924 "P2P: PIN from peer Display"); 925 if (dev->wps_method != WPS_PIN_KEYPAD) { 926 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 927 "P2P: We have wps_method=%s -> " 928 "incompatible", 929 p2p_wps_method_str(dev->wps_method)); 930 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD; 931 goto fail; 932 } 933 break; 934 case DEV_PW_USER_SPECIFIED: 935 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 936 "P2P: Peer entered PIN on Keypad"); 937 if (dev->wps_method != WPS_PIN_LABEL && 938 dev->wps_method != WPS_PIN_DISPLAY) { 939 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 940 "P2P: We have wps_method=%s -> " 941 "incompatible", 942 p2p_wps_method_str(dev->wps_method)); 943 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD; 944 goto fail; 945 } 946 break; 947 case DEV_PW_PUSHBUTTON: 948 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 949 "P2P: Peer using pushbutton"); 950 if (dev->wps_method != WPS_PBC) { 951 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 952 "P2P: We have wps_method=%s -> " 953 "incompatible", 954 p2p_wps_method_str(dev->wps_method)); 955 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD; 956 goto fail; 957 } 958 break; 959 default: 960 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 961 "P2P: Unsupported Device Password ID %d", 962 msg.dev_password_id); 963 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD; 964 goto fail; 965 } 966 967 if (go) { 968 struct p2p_channels intersection; 969 size_t i; 970 p2p_channels_intersect(&p2p->channels, &dev->channels, 971 &intersection); 972 if (intersection.reg_classes == 0 || 973 intersection.reg_class[0].channels == 0) { 974 status = P2P_SC_FAIL_NO_COMMON_CHANNELS; 975 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 976 "P2P: No common channels found"); 977 goto fail; 978 } 979 for (i = 0; i < intersection.reg_classes; i++) { 980 struct p2p_reg_class *c; 981 c = &intersection.reg_class[i]; 982 wpa_printf(MSG_DEBUG, "P2P: reg_class %u", 983 c->reg_class); 984 wpa_hexdump(MSG_DEBUG, "P2P: channels", 985 c->channel, c->channels); 986 } 987 if (!p2p_channels_includes(&intersection, p2p->op_reg_class, 988 p2p->op_channel)) 989 p2p_reselect_channel(p2p, &intersection); 990 991 p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len); 992 } 993 994 p2p_set_state(p2p, P2P_GO_NEG); 995 p2p_clear_timeout(p2p); 996 997 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 998 "P2P: GO Negotiation with " MACSTR, MAC2STR(sa)); 999 os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN); 1000 1001 fail: 1002 conf = p2p_build_go_neg_conf(p2p, dev, msg.dialog_token, status, 1003 msg.operating_channel, go); 1004 p2p_parse_free(&msg); 1005 if (conf == NULL) 1006 return; 1007 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 1008 "P2P: Sending GO Negotiation Confirm"); 1009 if (status == P2P_SC_SUCCESS) { 1010 p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM; 1011 dev->go_state = go ? LOCAL_GO : REMOTE_GO; 1012 } else 1013 p2p->pending_action_state = P2P_NO_PENDING_ACTION; 1014 if (rx_freq > 0) 1015 freq = rx_freq; 1016 else 1017 freq = dev->listen_freq; 1018 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr, sa, 1019 wpabuf_head(conf), wpabuf_len(conf), 200) < 0) { 1020 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 1021 "P2P: Failed to send Action frame"); 1022 p2p_go_neg_failed(p2p, dev, -1); 1023 } 1024 wpabuf_free(conf); 1025 } 1026 1027 1028 void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa, 1029 const u8 *data, size_t len) 1030 { 1031 struct p2p_device *dev; 1032 struct p2p_message msg; 1033 1034 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 1035 "P2P: Received GO Negotiation Confirm from " MACSTR, 1036 MAC2STR(sa)); 1037 dev = p2p_get_device(p2p, sa); 1038 if (dev == NULL || dev->wps_method == WPS_NOT_READY || 1039 dev != p2p->go_neg_peer) { 1040 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 1041 "P2P: Not ready for GO negotiation with " MACSTR, 1042 MAC2STR(sa)); 1043 return; 1044 } 1045 1046 if (p2p->pending_action_state == P2P_PENDING_GO_NEG_RESPONSE) { 1047 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopped waiting " 1048 "for TX status on GO Negotiation Response since we " 1049 "already received Confirmation"); 1050 p2p->pending_action_state = P2P_NO_PENDING_ACTION; 1051 } 1052 1053 if (p2p_parse(data, len, &msg)) 1054 return; 1055 1056 if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) { 1057 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 1058 "P2P: Was not expecting GO Negotiation Confirm - " 1059 "ignore"); 1060 return; 1061 } 1062 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM; 1063 1064 if (msg.dialog_token != dev->dialog_token) { 1065 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 1066 "P2P: Unexpected Dialog Token %u (expected %u)", 1067 msg.dialog_token, dev->dialog_token); 1068 p2p_parse_free(&msg); 1069 return; 1070 } 1071 1072 if (!msg.status) { 1073 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 1074 "P2P: No Status attribute received"); 1075 p2p_parse_free(&msg); 1076 return; 1077 } 1078 if (*msg.status) { 1079 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 1080 "P2P: GO Negotiation rejected: status %d", 1081 *msg.status); 1082 p2p_parse_free(&msg); 1083 return; 1084 } 1085 1086 if (dev->go_state == REMOTE_GO && msg.group_id) { 1087 /* Store SSID for Provisioning step */ 1088 p2p->ssid_len = msg.group_id_len - ETH_ALEN; 1089 os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len); 1090 } else if (dev->go_state == REMOTE_GO) { 1091 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 1092 "P2P: Mandatory P2P Group ID attribute missing from " 1093 "GO Negotiation Confirmation"); 1094 p2p->ssid_len = 0; 1095 #ifdef CONFIG_P2P_STRICT 1096 p2p_parse_free(&msg); 1097 return; 1098 #endif /* CONFIG_P2P_STRICT */ 1099 } 1100 1101 if (!msg.operating_channel) { 1102 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 1103 "P2P: Mandatory Operating Channel attribute missing " 1104 "from GO Negotiation Confirmation"); 1105 #ifdef CONFIG_P2P_STRICT 1106 p2p_parse_free(&msg); 1107 return; 1108 #endif /* CONFIG_P2P_STRICT */ 1109 } 1110 1111 if (!msg.channel_list) { 1112 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 1113 "P2P: Mandatory Operating Channel attribute missing " 1114 "from GO Negotiation Confirmation"); 1115 #ifdef CONFIG_P2P_STRICT 1116 p2p_parse_free(&msg); 1117 return; 1118 #endif /* CONFIG_P2P_STRICT */ 1119 } 1120 1121 p2p_parse_free(&msg); 1122 1123 if (dev->go_state == UNKNOWN_GO) { 1124 /* 1125 * This should not happen since GO negotiation has already 1126 * been completed. 1127 */ 1128 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, 1129 "P2P: Unexpected GO Neg state - do not know which end " 1130 "becomes GO"); 1131 return; 1132 } 1133 1134 p2p_go_complete(p2p, dev); 1135 } 1136