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