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