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