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 "utils/eloop.h" 13 #include "common/ieee802_11_defs.h" 14 #include "common/ieee802_11_common.h" 15 #include "common/wpa_ctrl.h" 16 #include "wps/wps_defs.h" 17 #include "p2p_i.h" 18 #include "p2p.h" 19 20 21 static int p2p_go_det(u8 own_intent, u8 peer_value) 22 { 23 u8 peer_intent = peer_value >> 1; 24 if (own_intent == peer_intent) { 25 if (own_intent == P2P_MAX_GO_INTENT) 26 return -1; /* both devices want to become GO */ 27 28 /* Use tie breaker bit to determine GO */ 29 return (peer_value & 0x01) ? 0 : 1; 30 } 31 32 return own_intent > peer_intent; 33 } 34 35 36 int p2p_peer_channels_check(struct p2p_data *p2p, struct p2p_channels *own, 37 struct p2p_device *dev, 38 const u8 *channel_list, size_t channel_list_len) 39 { 40 const u8 *pos, *end; 41 struct p2p_channels *ch; 42 u8 channels; 43 struct p2p_channels intersection; 44 45 ch = &dev->channels; 46 os_memset(ch, 0, sizeof(*ch)); 47 pos = channel_list; 48 end = channel_list + channel_list_len; 49 50 if (end - pos < 3) 51 return -1; 52 os_memcpy(dev->country, pos, 3); 53 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Peer country", pos, 3); 54 if (pos[2] != 0x04 && os_memcmp(pos, p2p->cfg->country, 2) != 0) { 55 p2p_info(p2p, "Mismatching country (ours=%c%c peer's=%c%c)", 56 p2p->cfg->country[0], p2p->cfg->country[1], 57 pos[0], pos[1]); 58 return -1; 59 } 60 pos += 3; 61 62 while (end - pos > 2) { 63 struct p2p_reg_class *cl = &ch->reg_class[ch->reg_classes]; 64 cl->reg_class = *pos++; 65 channels = *pos++; 66 if (channels > end - pos) { 67 p2p_info(p2p, "Invalid peer Channel List"); 68 return -1; 69 } 70 cl->channels = channels > P2P_MAX_REG_CLASS_CHANNELS ? 71 P2P_MAX_REG_CLASS_CHANNELS : channels; 72 os_memcpy(cl->channel, pos, cl->channels); 73 pos += channels; 74 ch->reg_classes++; 75 if (ch->reg_classes == P2P_MAX_REG_CLASSES) 76 break; 77 } 78 79 p2p_channels_intersect(own, &dev->channels, &intersection); 80 p2p_dbg(p2p, "Own reg_classes %d 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 p2p_info(p2p, "No common channels found"); 86 return -1; 87 } 88 return 0; 89 } 90 91 92 static int p2p_peer_channels(struct p2p_data *p2p, struct p2p_device *dev, 93 const u8 *channel_list, size_t channel_list_len) 94 { 95 return p2p_peer_channels_check(p2p, &p2p->channels, dev, 96 channel_list, channel_list_len); 97 } 98 99 100 u16 p2p_wps_method_pw_id(enum p2p_wps_method wps_method) 101 { 102 switch (wps_method) { 103 case WPS_PIN_DISPLAY: 104 return DEV_PW_REGISTRAR_SPECIFIED; 105 case WPS_PIN_KEYPAD: 106 return DEV_PW_USER_SPECIFIED; 107 case WPS_PBC: 108 return DEV_PW_PUSHBUTTON; 109 case WPS_NFC: 110 return DEV_PW_NFC_CONNECTION_HANDOVER; 111 case WPS_P2PS: 112 return DEV_PW_P2PS_DEFAULT; 113 default: 114 return DEV_PW_DEFAULT; 115 } 116 } 117 118 119 static const char * p2p_wps_method_str(enum p2p_wps_method wps_method) 120 { 121 switch (wps_method) { 122 case WPS_PIN_DISPLAY: 123 return "Display"; 124 case WPS_PIN_KEYPAD: 125 return "Keypad"; 126 case WPS_PBC: 127 return "PBC"; 128 case WPS_NFC: 129 return "NFC"; 130 case WPS_P2PS: 131 return "P2PS"; 132 default: 133 return "??"; 134 } 135 } 136 137 138 static struct wpabuf * p2p_build_go_neg_req(struct p2p_data *p2p, 139 struct p2p_device *peer) 140 { 141 struct wpabuf *buf; 142 u8 *len; 143 u8 group_capab; 144 size_t extra = 0; 145 u16 pw_id; 146 bool is_6ghz_capab; 147 148 #ifdef CONFIG_WIFI_DISPLAY 149 if (p2p->wfd_ie_go_neg) 150 extra = wpabuf_len(p2p->wfd_ie_go_neg); 151 #endif /* CONFIG_WIFI_DISPLAY */ 152 153 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ]) 154 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ]); 155 156 buf = wpabuf_alloc(1000 + extra); 157 if (buf == NULL) 158 return NULL; 159 160 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_REQ, peer->dialog_token); 161 162 len = p2p_buf_add_ie_hdr(buf); 163 group_capab = 0; 164 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) { 165 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP; 166 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN) 167 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_RECONN; 168 } 169 if (p2p->cross_connect) 170 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN; 171 if (p2p->cfg->p2p_intra_bss) 172 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST; 173 p2p_buf_add_capability(buf, p2p->dev_capab & 174 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 175 group_capab); 176 p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | peer->tie_breaker); 177 p2p_buf_add_config_timeout(buf, p2p->go_timeout, p2p->client_timeout); 178 p2p_buf_add_listen_channel(buf, p2p->cfg->country, p2p->cfg->reg_class, 179 p2p->cfg->channel); 180 if (p2p->ext_listen_interval) 181 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period, 182 p2p->ext_listen_interval); 183 p2p_buf_add_intended_addr(buf, p2p->intended_addr); 184 is_6ghz_capab = is_p2p_6ghz_capable(p2p) && 185 p2p_is_peer_6ghz_capab(p2p, peer->info.p2p_device_addr); 186 if (p2p->num_pref_freq) { 187 bool go = p2p->go_intent == 15; 188 struct p2p_channels pref_chanlist; 189 190 p2p_pref_channel_filter(&p2p->channels, p2p->pref_freq_list, 191 p2p->num_pref_freq, &pref_chanlist, go); 192 p2p_channels_dump(p2p, "channel list after filtering", 193 &pref_chanlist); 194 p2p_buf_add_channel_list(buf, p2p->cfg->country, 195 &pref_chanlist, is_6ghz_capab); 196 } else { 197 p2p_buf_add_channel_list(buf, p2p->cfg->country, 198 &p2p->channels, is_6ghz_capab); 199 } 200 p2p_buf_add_device_info(buf, p2p, peer); 201 p2p_buf_add_operating_channel(buf, p2p->cfg->country, 202 p2p->op_reg_class, p2p->op_channel); 203 p2p_buf_update_ie_hdr(buf, len); 204 205 p2p_buf_add_pref_channel_list(buf, p2p->pref_freq_list, 206 p2p->num_pref_freq); 207 208 /* WPS IE with Device Password ID attribute */ 209 pw_id = p2p_wps_method_pw_id(peer->wps_method); 210 if (peer->oob_pw_id) 211 pw_id = peer->oob_pw_id; 212 if (p2p_build_wps_ie(p2p, buf, pw_id, 0) < 0) { 213 p2p_dbg(p2p, "Failed to build WPS IE for GO Negotiation Request"); 214 wpabuf_free(buf); 215 return NULL; 216 } 217 218 #ifdef CONFIG_WIFI_DISPLAY 219 if (p2p->wfd_ie_go_neg) 220 wpabuf_put_buf(buf, p2p->wfd_ie_go_neg); 221 #endif /* CONFIG_WIFI_DISPLAY */ 222 223 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ]) 224 wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ]); 225 226 return buf; 227 } 228 229 230 int p2p_connect_send(struct p2p_data *p2p, struct p2p_device *dev) 231 { 232 struct wpabuf *req; 233 int freq; 234 235 if (dev->flags & P2P_DEV_PD_BEFORE_GO_NEG) { 236 u16 config_method; 237 p2p_dbg(p2p, "Use PD-before-GO-Neg workaround for " MACSTR, 238 MAC2STR(dev->info.p2p_device_addr)); 239 if (dev->wps_method == WPS_PIN_DISPLAY) 240 config_method = WPS_CONFIG_KEYPAD; 241 else if (dev->wps_method == WPS_PIN_KEYPAD) 242 config_method = WPS_CONFIG_DISPLAY; 243 else if (dev->wps_method == WPS_PBC) 244 config_method = WPS_CONFIG_PUSHBUTTON; 245 else if (dev->wps_method == WPS_P2PS) 246 config_method = WPS_CONFIG_P2PS; 247 else 248 return -1; 249 return p2p_prov_disc_req(p2p, dev->info.p2p_device_addr, 250 NULL, config_method, 0, 0, 1); 251 } 252 253 freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq; 254 if (dev->oob_go_neg_freq > 0) 255 freq = dev->oob_go_neg_freq; 256 if (freq <= 0) { 257 p2p_dbg(p2p, "No Listen/Operating frequency known for the peer " 258 MACSTR " to send GO Negotiation Request", 259 MAC2STR(dev->info.p2p_device_addr)); 260 return -1; 261 } 262 263 req = p2p_build_go_neg_req(p2p, dev); 264 if (req == NULL) 265 return -1; 266 p2p_dbg(p2p, "Sending GO Negotiation Request"); 267 p2p_set_state(p2p, P2P_CONNECT); 268 p2p->pending_action_state = P2P_PENDING_GO_NEG_REQUEST; 269 p2p->go_neg_peer = dev; 270 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL); 271 dev->flags |= P2P_DEV_WAIT_GO_NEG_RESPONSE; 272 dev->connect_reqs++; 273 if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr, 274 p2p->cfg->dev_addr, dev->info.p2p_device_addr, 275 wpabuf_head(req), wpabuf_len(req), 500) < 0) { 276 p2p_dbg(p2p, "Failed to send Action frame"); 277 /* Use P2P find to recover and retry */ 278 p2p_set_timeout(p2p, 0, 0); 279 } else 280 dev->go_neg_req_sent++; 281 282 wpabuf_free(req); 283 284 return 0; 285 } 286 287 288 static struct wpabuf * p2p_build_go_neg_resp(struct p2p_data *p2p, 289 struct p2p_device *peer, 290 u8 dialog_token, u8 status, 291 u8 tie_breaker) 292 { 293 struct wpabuf *buf; 294 u8 *len; 295 u8 group_capab; 296 size_t extra = 0; 297 u16 pw_id; 298 bool is_6ghz_capab; 299 struct p2p_channels pref_chanlist; 300 301 p2p_dbg(p2p, "Building GO Negotiation Response"); 302 303 #ifdef CONFIG_WIFI_DISPLAY 304 if (p2p->wfd_ie_go_neg) 305 extra = wpabuf_len(p2p->wfd_ie_go_neg); 306 #endif /* CONFIG_WIFI_DISPLAY */ 307 308 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP]) 309 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP]); 310 311 buf = wpabuf_alloc(1000 + extra); 312 if (buf == NULL) 313 return NULL; 314 315 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_RESP, dialog_token); 316 317 len = p2p_buf_add_ie_hdr(buf); 318 p2p_buf_add_status(buf, status); 319 group_capab = 0; 320 if (peer && peer->go_state == LOCAL_GO) { 321 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) { 322 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP; 323 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN) 324 group_capab |= 325 P2P_GROUP_CAPAB_PERSISTENT_RECONN; 326 } 327 if (p2p->cross_connect) 328 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN; 329 if (p2p->cfg->p2p_intra_bss) 330 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST; 331 } 332 p2p_buf_add_capability(buf, p2p->dev_capab & 333 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 334 group_capab); 335 p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | tie_breaker); 336 p2p_buf_add_config_timeout(buf, p2p->go_timeout, p2p->client_timeout); 337 if (p2p->override_pref_op_class) { 338 p2p_dbg(p2p, "Override operating channel preference"); 339 p2p_buf_add_operating_channel(buf, p2p->cfg->country, 340 p2p->override_pref_op_class, 341 p2p->override_pref_channel); 342 } else if (peer && peer->go_state == REMOTE_GO && !p2p->num_pref_freq) { 343 p2p_dbg(p2p, "Omit Operating Channel attribute"); 344 } else { 345 p2p_buf_add_operating_channel(buf, p2p->cfg->country, 346 p2p->op_reg_class, 347 p2p->op_channel); 348 } 349 p2p_buf_add_intended_addr(buf, p2p->intended_addr); 350 if (p2p->num_pref_freq) { 351 bool go = (peer && peer->go_state == LOCAL_GO) || 352 p2p->go_intent == 15; 353 354 p2p_pref_channel_filter(&p2p->channels, p2p->pref_freq_list, 355 p2p->num_pref_freq, &pref_chanlist, go); 356 p2p_channels_dump(p2p, "channel list after filtering", 357 &pref_chanlist); 358 } else { 359 p2p_copy_channels(&pref_chanlist, &p2p->channels, 360 p2p->allow_6ghz); 361 } 362 if (status || peer == NULL) { 363 p2p_buf_add_channel_list(buf, p2p->cfg->country, 364 &pref_chanlist, false); 365 } else if (peer->go_state == REMOTE_GO) { 366 is_6ghz_capab = is_p2p_6ghz_capable(p2p) && 367 p2p_is_peer_6ghz_capab(p2p, peer->info.p2p_device_addr); 368 p2p_buf_add_channel_list(buf, p2p->cfg->country, 369 &pref_chanlist, is_6ghz_capab); 370 } else { 371 struct p2p_channels res; 372 373 is_6ghz_capab = is_p2p_6ghz_capable(p2p) && 374 p2p_is_peer_6ghz_capab(p2p, peer->info.p2p_device_addr); 375 p2p_channels_intersect(&pref_chanlist, &peer->channels, 376 &res); 377 p2p_buf_add_channel_list(buf, p2p->cfg->country, &res, 378 is_6ghz_capab); 379 } 380 p2p_buf_add_device_info(buf, p2p, peer); 381 if (peer && peer->go_state == LOCAL_GO) { 382 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid, 383 p2p->ssid_len); 384 } 385 p2p_buf_update_ie_hdr(buf, len); 386 387 /* WPS IE with Device Password ID attribute */ 388 pw_id = p2p_wps_method_pw_id(peer ? peer->wps_method : WPS_NOT_READY); 389 if (peer && peer->oob_pw_id) 390 pw_id = peer->oob_pw_id; 391 if (p2p_build_wps_ie(p2p, buf, pw_id, 0) < 0) { 392 p2p_dbg(p2p, "Failed to build WPS IE for GO Negotiation Response"); 393 wpabuf_free(buf); 394 return NULL; 395 } 396 397 #ifdef CONFIG_WIFI_DISPLAY 398 if (p2p->wfd_ie_go_neg) 399 wpabuf_put_buf(buf, p2p->wfd_ie_go_neg); 400 #endif /* CONFIG_WIFI_DISPLAY */ 401 402 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP]) 403 wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP]); 404 405 return buf; 406 } 407 408 409 /** 410 * p2p_reselect_channel - Re-select operating channel based on peer information 411 * @p2p: P2P module context from p2p_init() 412 * @intersection: Support channel list intersection from local and peer 413 * 414 * This function is used to re-select the best channel after having received 415 * information from the peer to allow supported channel lists to be intersected. 416 * This can be used to improve initial channel selection done in 417 * p2p_prepare_channel() prior to the start of GO Negotiation. In addition, this 418 * can be used for Invitation case. 419 */ 420 void p2p_reselect_channel(struct p2p_data *p2p, 421 struct p2p_channels *intersection) 422 { 423 struct p2p_reg_class *cl; 424 int freq; 425 u8 op_reg_class, op_channel; 426 unsigned int i; 427 const int op_classes_5ghz[] = { 124, 125, 115, 0 }; 428 const int op_classes_ht40[] = { 126, 127, 116, 117, 0 }; 429 const int op_classes_vht[] = { 128, 129, 130, 0 }; 430 const int op_classes_edmg[] = { 181, 182, 183, 0 }; 431 432 if (p2p->own_freq_preference > 0 && 433 p2p_freq_to_channel(p2p->own_freq_preference, 434 &op_reg_class, &op_channel) == 0 && 435 p2p_channels_includes(intersection, op_reg_class, op_channel)) { 436 p2p_dbg(p2p, "Pick own channel preference (reg_class %u channel %u) from intersection", 437 op_reg_class, op_channel); 438 p2p->op_reg_class = op_reg_class; 439 p2p->op_channel = op_channel; 440 return; 441 } 442 443 if (p2p->best_freq_overall > 0 && 444 p2p_freq_to_channel(p2p->best_freq_overall, 445 &op_reg_class, &op_channel) == 0 && 446 p2p_channels_includes(intersection, op_reg_class, op_channel)) { 447 p2p_dbg(p2p, "Pick best overall channel (reg_class %u channel %u) from intersection", 448 op_reg_class, op_channel); 449 p2p->op_reg_class = op_reg_class; 450 p2p->op_channel = op_channel; 451 return; 452 } 453 454 /* First, try to pick the best channel from another band */ 455 freq = p2p_channel_to_freq(p2p->op_reg_class, p2p->op_channel); 456 if (freq >= 2400 && freq < 2500 && p2p->best_freq_5 > 0 && 457 !p2p_channels_includes(intersection, p2p->op_reg_class, 458 p2p->op_channel) && 459 p2p_freq_to_channel(p2p->best_freq_5, 460 &op_reg_class, &op_channel) == 0 && 461 p2p_channels_includes(intersection, op_reg_class, op_channel)) { 462 p2p_dbg(p2p, "Pick best 5 GHz channel (reg_class %u channel %u) from intersection", 463 op_reg_class, op_channel); 464 p2p->op_reg_class = op_reg_class; 465 p2p->op_channel = op_channel; 466 return; 467 } 468 469 if (freq >= 4900 && freq < 6000 && p2p->best_freq_24 > 0 && 470 !p2p_channels_includes(intersection, p2p->op_reg_class, 471 p2p->op_channel) && 472 p2p_freq_to_channel(p2p->best_freq_24, 473 &op_reg_class, &op_channel) == 0 && 474 p2p_channels_includes(intersection, op_reg_class, op_channel)) { 475 p2p_dbg(p2p, "Pick best 2.4 GHz channel (reg_class %u channel %u) from intersection", 476 op_reg_class, op_channel); 477 p2p->op_reg_class = op_reg_class; 478 p2p->op_channel = op_channel; 479 return; 480 } 481 482 /* Select channel with highest preference if the peer supports it */ 483 for (i = 0; p2p->cfg->pref_chan && i < p2p->cfg->num_pref_chan; i++) { 484 if (p2p_channels_includes(intersection, 485 p2p->cfg->pref_chan[i].op_class, 486 p2p->cfg->pref_chan[i].chan)) { 487 p2p->op_reg_class = p2p->cfg->pref_chan[i].op_class; 488 p2p->op_channel = p2p->cfg->pref_chan[i].chan; 489 p2p_dbg(p2p, "Pick highest preferred channel (op_class %u channel %u) from intersection", 490 p2p->op_reg_class, p2p->op_channel); 491 return; 492 } 493 } 494 495 /* Try a channel where we might be able to use EDMG */ 496 if (p2p_channel_select(intersection, op_classes_edmg, 497 &p2p->op_reg_class, &p2p->op_channel) == 0) { 498 p2p_dbg(p2p, "Pick possible EDMG channel (op_class %u channel %u) from intersection", 499 p2p->op_reg_class, p2p->op_channel); 500 return; 501 } 502 503 /* Try a channel where we might be able to use VHT */ 504 if (p2p_channel_select(intersection, op_classes_vht, 505 &p2p->op_reg_class, &p2p->op_channel) == 0) { 506 p2p_dbg(p2p, "Pick possible VHT channel (op_class %u channel %u) from intersection", 507 p2p->op_reg_class, p2p->op_channel); 508 return; 509 } 510 511 /* Try a channel where we might be able to use HT40 */ 512 if (p2p_channel_select(intersection, op_classes_ht40, 513 &p2p->op_reg_class, &p2p->op_channel) == 0) { 514 p2p_dbg(p2p, "Pick possible HT40 channel (op_class %u channel %u) from intersection", 515 p2p->op_reg_class, p2p->op_channel); 516 return; 517 } 518 519 /* Prefer a 5 GHz channel */ 520 if (p2p_channel_select(intersection, op_classes_5ghz, 521 &p2p->op_reg_class, &p2p->op_channel) == 0) { 522 p2p_dbg(p2p, "Pick possible 5 GHz channel (op_class %u channel %u) from intersection", 523 p2p->op_reg_class, p2p->op_channel); 524 return; 525 } 526 527 /* 528 * Try to see if the original channel is in the intersection. If 529 * so, no need to change anything, as it already contains some 530 * randomness. 531 */ 532 if (p2p_channels_includes(intersection, p2p->op_reg_class, 533 p2p->op_channel)) { 534 p2p_dbg(p2p, "Using original operating class and channel (op_class %u channel %u) from intersection", 535 p2p->op_reg_class, p2p->op_channel); 536 return; 537 } 538 539 /* 540 * Fall back to whatever is included in the channel intersection since 541 * no better options seems to be available. 542 */ 543 cl = &intersection->reg_class[0]; 544 p2p_dbg(p2p, "Pick another channel (reg_class %u channel %u) from intersection", 545 cl->reg_class, cl->channel[0]); 546 p2p->op_reg_class = cl->reg_class; 547 p2p->op_channel = cl->channel[0]; 548 } 549 550 551 int p2p_go_select_channel(struct p2p_data *p2p, struct p2p_device *dev, 552 u8 *status) 553 { 554 struct p2p_channels tmp, intersection; 555 556 p2p_channels_dump(p2p, "own channels", &p2p->channels); 557 p2p_channels_dump(p2p, "peer channels", &dev->channels); 558 p2p_channels_intersect(&p2p->channels, &dev->channels, &tmp); 559 p2p_channels_dump(p2p, "intersection", &tmp); 560 p2p_channels_remove_freqs(&tmp, &p2p->no_go_freq); 561 p2p_channels_dump(p2p, "intersection after no-GO removal", &tmp); 562 p2p_channels_intersect(&tmp, &p2p->cfg->channels, &intersection); 563 p2p_channels_dump(p2p, "intersection with local channel list", 564 &intersection); 565 if (intersection.reg_classes == 0 || 566 intersection.reg_class[0].channels == 0) { 567 *status = P2P_SC_FAIL_NO_COMMON_CHANNELS; 568 p2p_dbg(p2p, "No common channels found"); 569 return -1; 570 } 571 572 if (!p2p_channels_includes(&intersection, p2p->op_reg_class, 573 p2p->op_channel)) { 574 if (dev->flags & P2P_DEV_FORCE_FREQ) { 575 *status = P2P_SC_FAIL_NO_COMMON_CHANNELS; 576 p2p_dbg(p2p, "Peer does not support the forced channel"); 577 return -1; 578 } 579 580 p2p_dbg(p2p, "Selected operating channel (op_class %u channel %u) not acceptable to the peer", 581 p2p->op_reg_class, p2p->op_channel); 582 p2p_reselect_channel(p2p, &intersection); 583 } else if (!(dev->flags & P2P_DEV_FORCE_FREQ) && 584 !p2p->cfg->cfg_op_channel) { 585 p2p_dbg(p2p, "Try to optimize channel selection with peer information received; previously selected op_class %u channel %u", 586 p2p->op_reg_class, p2p->op_channel); 587 p2p_reselect_channel(p2p, &intersection); 588 } 589 590 if (!p2p->ssid_set) { 591 p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len); 592 p2p->ssid_set = 1; 593 } 594 595 return 0; 596 } 597 598 599 static void p2p_check_pref_chan_no_recv(struct p2p_data *p2p, int go, 600 struct p2p_device *dev, 601 struct p2p_message *msg, 602 const struct weighted_pcl freq_list[], 603 unsigned int size) 604 { 605 u8 op_class, op_channel; 606 unsigned int oper_freq = 0, i, j; 607 int found = 0; 608 609 p2p_dbg(p2p, 610 "Peer didn't provide a preferred frequency list, see if any of our preferred channels are supported by peer device"); 611 612 /* 613 * Search for a common channel in our preferred frequency list which is 614 * also supported by the peer device. 615 */ 616 for (i = 0; i < size && !found; i++) { 617 /* Make sure that the common frequency is supported by peer. */ 618 oper_freq = freq_list[i].freq; 619 if (p2p_freq_to_channel(oper_freq, &op_class, 620 &op_channel) < 0 || 621 !p2p_pref_freq_allowed(&freq_list[i], go)) 622 continue; 623 for (j = 0; j < msg->channel_list_len; j++) { 624 if (!msg->channel_list || 625 op_channel != msg->channel_list[j]) 626 continue; 627 628 p2p->op_reg_class = op_class; 629 p2p->op_channel = op_channel; 630 os_memcpy(&p2p->channels, &p2p->cfg->channels, 631 sizeof(struct p2p_channels)); 632 found = 1; 633 break; 634 } 635 } 636 637 if (found) { 638 p2p_dbg(p2p, 639 "Freq %d MHz is a preferred channel and is also supported by peer, use it as the operating channel", 640 oper_freq); 641 } else { 642 p2p_dbg(p2p, 643 "None of our preferred channels are supported by peer!"); 644 } 645 } 646 647 648 static void p2p_check_pref_chan_recv(struct p2p_data *p2p, int go, 649 struct p2p_device *dev, 650 struct p2p_message *msg, 651 const struct weighted_pcl freq_list[], 652 unsigned int size) 653 { 654 u8 op_class, op_channel; 655 unsigned int oper_freq = 0, i, j; 656 int found = 0; 657 658 /* 659 * Peer device supports a Preferred Frequency List. 660 * Search for a common channel in the preferred frequency lists 661 * of both peer and local devices. 662 */ 663 for (i = 0; i < size && !found; i++) { 664 for (j = 2; j < (msg->pref_freq_list_len / 2); j++) { 665 oper_freq = p2p_channel_to_freq( 666 msg->pref_freq_list[2 * j], 667 msg->pref_freq_list[2 * j + 1]); 668 if (freq_list[i].freq != oper_freq) 669 continue; 670 if (p2p_freq_to_channel(oper_freq, &op_class, 671 &op_channel) < 0) 672 continue; /* cannot happen */ 673 if (!p2p_pref_freq_allowed(&freq_list[i], go)) 674 break; 675 p2p->op_reg_class = op_class; 676 p2p->op_channel = op_channel; 677 os_memcpy(&p2p->channels, &p2p->cfg->channels, 678 sizeof(struct p2p_channels)); 679 found = 1; 680 break; 681 } 682 } 683 684 if (found) { 685 p2p_dbg(p2p, 686 "Freq %d MHz is a common preferred channel for both peer and local, use it as operating channel", 687 oper_freq); 688 } else { 689 p2p_dbg(p2p, "No common preferred channels found!"); 690 } 691 } 692 693 694 void p2p_check_pref_chan(struct p2p_data *p2p, int go, 695 struct p2p_device *dev, struct p2p_message *msg) 696 { 697 unsigned int size; 698 unsigned int i; 699 u8 op_class, op_channel; 700 char txt[100], *pos, *end; 701 bool is_6ghz_capab; 702 int res; 703 704 /* 705 * Use the preferred channel list from the driver only if there is no 706 * forced_freq, e.g., P2P_CONNECT freq=..., and no preferred operating 707 * channel hardcoded in the configuration file. 708 */ 709 if (!p2p->cfg->get_pref_freq_list || p2p->cfg->num_pref_chan || 710 (dev->flags & P2P_DEV_FORCE_FREQ) || p2p->cfg->cfg_op_channel) 711 return; 712 713 /* Obtain our preferred frequency list from driver based on P2P role. */ 714 size = P2P_MAX_PREF_CHANNELS; 715 if (p2p->cfg->get_pref_freq_list(p2p->cfg->cb_ctx, go, 716 &p2p->num_pref_freq, 717 p2p->pref_freq_list)) 718 return; 719 size = p2p->num_pref_freq; 720 if (!size) 721 return; 722 /* Filter out frequencies that are not acceptable for P2P use */ 723 is_6ghz_capab = is_p2p_6ghz_capable(p2p) && 724 p2p_is_peer_6ghz_capab(p2p, dev->info.p2p_device_addr); 725 i = 0; 726 while (i < size) { 727 if (p2p_freq_to_channel(p2p->pref_freq_list[i].freq, 728 &op_class, &op_channel) < 0 || 729 (!p2p_channels_includes(&p2p->cfg->channels, 730 op_class, op_channel) && 731 (go || !p2p_channels_includes(&p2p->cfg->cli_channels, 732 op_class, op_channel))) || 733 (is_6ghz_freq(p2p->pref_freq_list[i].freq) && 734 !is_6ghz_capab)) { 735 p2p_dbg(p2p, 736 "Ignore local driver frequency preference %u MHz since it is not acceptable for P2P use (go=%d)", 737 p2p->pref_freq_list[i].freq, go); 738 if (size - i - 1 > 0) 739 os_memmove(&p2p->pref_freq_list[i], 740 &p2p->pref_freq_list[i + 1], 741 (size - i - 1) * 742 sizeof(struct weighted_pcl)); 743 size--; 744 continue; 745 } 746 747 /* Preferred frequency is acceptable for P2P use */ 748 i++; 749 } 750 751 pos = txt; 752 end = pos + sizeof(txt); 753 for (i = 0; i < size; i++) { 754 res = os_snprintf(pos, end - pos, " %u", 755 p2p->pref_freq_list[i].freq); 756 if (os_snprintf_error(end - pos, res)) 757 break; 758 pos += res; 759 } 760 *pos = '\0'; 761 p2p_dbg(p2p, "Local driver frequency preference (size=%u):%s", 762 size, txt); 763 764 /* 765 * Check if peer's preference of operating channel is in 766 * our preferred channel list. 767 */ 768 for (i = 0; i < size; i++) { 769 if (p2p->pref_freq_list[i].freq == 770 (unsigned int) dev->oper_freq && 771 p2p_pref_freq_allowed(&p2p->pref_freq_list[i], go)) 772 break; 773 } 774 if (i != size && 775 p2p_freq_to_channel(p2p->pref_freq_list[i].freq, &op_class, 776 &op_channel) == 0) { 777 /* Peer operating channel preference matches our preference */ 778 p2p->op_reg_class = op_class; 779 p2p->op_channel = op_channel; 780 os_memcpy(&p2p->channels, &p2p->cfg->channels, 781 sizeof(struct p2p_channels)); 782 return; 783 } 784 785 p2p_dbg(p2p, 786 "Peer operating channel preference: %d MHz is not in our preferred channel list", 787 dev->oper_freq); 788 789 /* 790 Check if peer's preferred channel list is 791 * _not_ included in the GO Negotiation Request or Invitation Request. 792 */ 793 if (msg->pref_freq_list_len == 0) 794 p2p_check_pref_chan_no_recv(p2p, go, dev, msg, 795 p2p->pref_freq_list, size); 796 else 797 p2p_check_pref_chan_recv(p2p, go, dev, msg, 798 p2p->pref_freq_list, size); 799 } 800 801 802 void p2p_process_go_neg_req(struct p2p_data *p2p, const u8 *sa, 803 const u8 *data, size_t len, int rx_freq) 804 { 805 struct p2p_device *dev = NULL; 806 struct wpabuf *resp; 807 struct p2p_message msg; 808 u8 status = P2P_SC_FAIL_INVALID_PARAMS; 809 int tie_breaker = 0; 810 int freq; 811 812 p2p_dbg(p2p, "Received GO Negotiation Request from " MACSTR "(freq=%d)", 813 MAC2STR(sa), rx_freq); 814 815 if (p2p_parse(data, len, &msg)) 816 return; 817 818 if (!msg.capability) { 819 p2p_dbg(p2p, "Mandatory Capability attribute missing from GO Negotiation Request"); 820 #ifdef CONFIG_P2P_STRICT 821 goto fail; 822 #endif /* CONFIG_P2P_STRICT */ 823 } 824 825 if (msg.go_intent) 826 tie_breaker = *msg.go_intent & 0x01; 827 else { 828 p2p_dbg(p2p, "Mandatory GO Intent attribute missing from GO Negotiation Request"); 829 #ifdef CONFIG_P2P_STRICT 830 goto fail; 831 #endif /* CONFIG_P2P_STRICT */ 832 } 833 834 if (!msg.config_timeout) { 835 p2p_dbg(p2p, "Mandatory Configuration Timeout attribute missing from GO Negotiation Request"); 836 #ifdef CONFIG_P2P_STRICT 837 goto fail; 838 #endif /* CONFIG_P2P_STRICT */ 839 } 840 841 if (!msg.listen_channel) { 842 p2p_dbg(p2p, "No Listen Channel attribute received"); 843 goto fail; 844 } 845 if (!msg.operating_channel) { 846 p2p_dbg(p2p, "No Operating Channel attribute received"); 847 goto fail; 848 } 849 if (!msg.channel_list) { 850 p2p_dbg(p2p, "No Channel List attribute received"); 851 goto fail; 852 } 853 if (!msg.intended_addr) { 854 p2p_dbg(p2p, "No Intended P2P Interface Address attribute received"); 855 goto fail; 856 } 857 if (!msg.p2p_device_info) { 858 p2p_dbg(p2p, "No P2P Device Info attribute received"); 859 goto fail; 860 } 861 862 if (!ether_addr_equal(msg.p2p_device_addr, sa)) { 863 p2p_dbg(p2p, "Unexpected GO Negotiation Request SA=" MACSTR 864 " != dev_addr=" MACSTR, 865 MAC2STR(sa), MAC2STR(msg.p2p_device_addr)); 866 goto fail; 867 } 868 869 dev = p2p_get_device(p2p, sa); 870 871 if (msg.status && *msg.status) { 872 p2p_dbg(p2p, "Unexpected Status attribute (%d) in GO Negotiation Request", 873 *msg.status); 874 if (dev && p2p->go_neg_peer == dev && 875 *msg.status == P2P_SC_FAIL_REJECTED_BY_USER) { 876 /* 877 * This mechanism for using Status attribute in GO 878 * Negotiation Request is not compliant with the P2P 879 * specification, but some deployed devices use it to 880 * indicate rejection of GO Negotiation in a case where 881 * they have sent out GO Negotiation Response with 882 * status 1. The P2P specification explicitly disallows 883 * this. To avoid unnecessary interoperability issues 884 * and extra frames, mark the pending negotiation as 885 * failed and do not reply to this GO Negotiation 886 * Request frame. 887 */ 888 p2p->cfg->send_action_done(p2p->cfg->cb_ctx); 889 p2p_go_neg_failed(p2p, *msg.status); 890 p2p_parse_free(&msg); 891 return; 892 } 893 goto fail; 894 } 895 896 if (dev == NULL) 897 dev = p2p_add_dev_from_go_neg_req(p2p, sa, &msg); 898 else if ((dev->flags & P2P_DEV_PROBE_REQ_ONLY) || 899 !(dev->flags & P2P_DEV_REPORTED)) 900 p2p_add_dev_info(p2p, sa, dev, &msg); 901 else if (!dev->listen_freq && !dev->oper_freq) { 902 /* 903 * This may happen if the peer entry was added based on PD 904 * Request and no Probe Request/Response frame has been received 905 * from this peer (or that information has timed out). 906 */ 907 p2p_dbg(p2p, "Update peer " MACSTR 908 " based on GO Neg Req since listen/oper freq not known", 909 MAC2STR(dev->info.p2p_device_addr)); 910 p2p_add_dev_info(p2p, sa, dev, &msg); 911 } 912 913 if (dev) 914 p2p_update_peer_6ghz_capab(dev, &msg); 915 916 if (p2p->go_neg_peer && p2p->go_neg_peer == dev) 917 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL); 918 919 if (dev && dev->flags & P2P_DEV_USER_REJECTED) { 920 p2p_dbg(p2p, "User has rejected this peer"); 921 status = P2P_SC_FAIL_REJECTED_BY_USER; 922 } else if (dev == NULL || 923 (dev->wps_method == WPS_NOT_READY && 924 (p2p->authorized_oob_dev_pw_id == 0 || 925 p2p->authorized_oob_dev_pw_id != 926 msg.dev_password_id))) { 927 p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR, 928 MAC2STR(sa)); 929 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; 930 p2p->cfg->go_neg_req_rx(p2p->cfg->cb_ctx, sa, 931 msg.dev_password_id, 932 msg.go_intent ? (*msg.go_intent >> 1) : 933 0); 934 } else if (p2p->go_neg_peer && p2p->go_neg_peer != dev) { 935 p2p_dbg(p2p, "Already in Group Formation with another peer"); 936 status = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE; 937 } else { 938 int go; 939 940 if (!p2p->go_neg_peer) { 941 p2p_dbg(p2p, "Starting GO Negotiation with previously authorized peer"); 942 if (!(dev->flags & P2P_DEV_FORCE_FREQ)) { 943 p2p_dbg(p2p, "Use default channel settings"); 944 p2p->op_reg_class = p2p->cfg->op_reg_class; 945 p2p->op_channel = p2p->cfg->op_channel; 946 os_memcpy(&p2p->channels, &p2p->cfg->channels, 947 sizeof(struct p2p_channels)); 948 } else { 949 p2p_dbg(p2p, "Use previously configured forced channel settings"); 950 } 951 } 952 953 dev->flags &= ~P2P_DEV_NOT_YET_READY; 954 955 if (!msg.go_intent) { 956 p2p_dbg(p2p, "No GO Intent attribute received"); 957 goto fail; 958 } 959 if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) { 960 p2p_dbg(p2p, "Invalid GO Intent value (%u) received", 961 *msg.go_intent >> 1); 962 goto fail; 963 } 964 965 if (dev->go_neg_req_sent && 966 os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) > 0) { 967 p2p_dbg(p2p, "Do not reply since peer has higher address and GO Neg Request already sent"); 968 p2p_parse_free(&msg); 969 return; 970 } 971 972 if (dev->go_neg_req_sent && 973 (dev->flags & P2P_DEV_PEER_WAITING_RESPONSE)) { 974 p2p_dbg(p2p, 975 "Do not reply since peer is waiting for us to start a new GO Negotiation and GO Neg Request already sent"); 976 p2p_parse_free(&msg); 977 return; 978 } 979 980 go = p2p_go_det(p2p->go_intent, *msg.go_intent); 981 if (go < 0) { 982 p2p_dbg(p2p, "Incompatible GO Intent"); 983 status = P2P_SC_FAIL_BOTH_GO_INTENT_15; 984 goto fail; 985 } 986 987 if (p2p_peer_channels(p2p, dev, msg.channel_list, 988 msg.channel_list_len) < 0) { 989 p2p_dbg(p2p, "No common channels found"); 990 status = P2P_SC_FAIL_NO_COMMON_CHANNELS; 991 goto fail; 992 } 993 994 switch (msg.dev_password_id) { 995 case DEV_PW_REGISTRAR_SPECIFIED: 996 p2p_dbg(p2p, "PIN from peer Display"); 997 if (dev->wps_method != WPS_PIN_KEYPAD) { 998 p2p_dbg(p2p, "We have wps_method=%s -> incompatible", 999 p2p_wps_method_str(dev->wps_method)); 1000 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD; 1001 goto fail; 1002 } 1003 break; 1004 case DEV_PW_USER_SPECIFIED: 1005 p2p_dbg(p2p, "Peer entered PIN on Keypad"); 1006 if (dev->wps_method != WPS_PIN_DISPLAY) { 1007 p2p_dbg(p2p, "We have wps_method=%s -> incompatible", 1008 p2p_wps_method_str(dev->wps_method)); 1009 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD; 1010 goto fail; 1011 } 1012 break; 1013 case DEV_PW_PUSHBUTTON: 1014 p2p_dbg(p2p, "Peer using pushbutton"); 1015 if (dev->wps_method != WPS_PBC) { 1016 p2p_dbg(p2p, "We have wps_method=%s -> incompatible", 1017 p2p_wps_method_str(dev->wps_method)); 1018 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD; 1019 goto fail; 1020 } 1021 break; 1022 case DEV_PW_P2PS_DEFAULT: 1023 p2p_dbg(p2p, "Peer using P2PS pin"); 1024 if (dev->wps_method != WPS_P2PS) { 1025 p2p_dbg(p2p, 1026 "We have wps_method=%s -> incompatible", 1027 p2p_wps_method_str(dev->wps_method)); 1028 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD; 1029 goto fail; 1030 } 1031 break; 1032 default: 1033 if (msg.dev_password_id && 1034 msg.dev_password_id == dev->oob_pw_id) { 1035 p2p_dbg(p2p, "Peer using NFC"); 1036 if (dev->wps_method != WPS_NFC) { 1037 p2p_dbg(p2p, "We have wps_method=%s -> incompatible", 1038 p2p_wps_method_str( 1039 dev->wps_method)); 1040 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD; 1041 goto fail; 1042 } 1043 break; 1044 } 1045 #ifdef CONFIG_WPS_NFC 1046 if (p2p->authorized_oob_dev_pw_id && 1047 msg.dev_password_id == 1048 p2p->authorized_oob_dev_pw_id) { 1049 p2p_dbg(p2p, "Using static handover with our device password from NFC Tag"); 1050 dev->wps_method = WPS_NFC; 1051 dev->oob_pw_id = p2p->authorized_oob_dev_pw_id; 1052 break; 1053 } 1054 #endif /* CONFIG_WPS_NFC */ 1055 p2p_dbg(p2p, "Unsupported Device Password ID %d", 1056 msg.dev_password_id); 1057 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD; 1058 goto fail; 1059 } 1060 1061 if (go && p2p_go_select_channel(p2p, dev, &status) < 0) 1062 goto fail; 1063 1064 dev->go_state = go ? LOCAL_GO : REMOTE_GO; 1065 dev->oper_freq = p2p_channel_to_freq(msg.operating_channel[3], 1066 msg.operating_channel[4]); 1067 p2p_dbg(p2p, "Peer operating channel preference: %d MHz", 1068 dev->oper_freq); 1069 1070 /* 1071 * Use the driver preferred frequency list extension if 1072 * supported. 1073 */ 1074 p2p_check_pref_chan(p2p, go, dev, &msg); 1075 1076 if (msg.config_timeout) { 1077 dev->go_timeout = msg.config_timeout[0]; 1078 dev->client_timeout = msg.config_timeout[1]; 1079 } 1080 1081 p2p_dbg(p2p, "GO Negotiation with " MACSTR, MAC2STR(sa)); 1082 if (p2p->state != P2P_IDLE) 1083 p2p_stop_find_for_freq(p2p, rx_freq); 1084 p2p_set_state(p2p, P2P_GO_NEG); 1085 p2p_clear_timeout(p2p); 1086 dev->dialog_token = msg.dialog_token; 1087 os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN); 1088 p2p->go_neg_peer = dev; 1089 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL); 1090 status = P2P_SC_SUCCESS; 1091 } 1092 1093 fail: 1094 if (dev) 1095 dev->status = status; 1096 resp = p2p_build_go_neg_resp(p2p, dev, msg.dialog_token, status, 1097 !tie_breaker); 1098 p2p_parse_free(&msg); 1099 if (resp == NULL) 1100 return; 1101 p2p_dbg(p2p, "Sending GO Negotiation Response"); 1102 if (rx_freq > 0) 1103 freq = rx_freq; 1104 else 1105 freq = p2p_channel_to_freq(p2p->cfg->reg_class, 1106 p2p->cfg->channel); 1107 if (freq < 0) { 1108 p2p_dbg(p2p, "Unknown regulatory class/channel"); 1109 wpabuf_free(resp); 1110 return; 1111 } 1112 if (status == P2P_SC_SUCCESS) { 1113 p2p->pending_action_state = P2P_PENDING_GO_NEG_RESPONSE; 1114 dev->flags |= P2P_DEV_WAIT_GO_NEG_CONFIRM; 1115 if (os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) < 0) { 1116 /* 1117 * Peer has smaller address, so the GO Negotiation 1118 * Response from us is expected to complete 1119 * negotiation. Ignore a GO Negotiation Response from 1120 * the peer if it happens to be received after this 1121 * point due to a race condition in GO Negotiation 1122 * Request transmission and processing. 1123 */ 1124 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE; 1125 } 1126 } else 1127 p2p->pending_action_state = 1128 P2P_PENDING_GO_NEG_RESPONSE_FAILURE; 1129 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr, 1130 p2p->cfg->dev_addr, 1131 wpabuf_head(resp), wpabuf_len(resp), 100) < 0) { 1132 p2p_dbg(p2p, "Failed to send Action frame"); 1133 } 1134 1135 wpabuf_free(resp); 1136 } 1137 1138 1139 static struct wpabuf * p2p_build_go_neg_conf(struct p2p_data *p2p, 1140 struct p2p_device *peer, 1141 u8 dialog_token, u8 status, 1142 const u8 *resp_chan, int go) 1143 { 1144 struct wpabuf *buf; 1145 u8 *len; 1146 struct p2p_channels res; 1147 u8 group_capab; 1148 size_t extra = 0; 1149 bool is_6ghz_capab; 1150 1151 p2p_dbg(p2p, "Building GO Negotiation Confirm"); 1152 1153 #ifdef CONFIG_WIFI_DISPLAY 1154 if (p2p->wfd_ie_go_neg) 1155 extra = wpabuf_len(p2p->wfd_ie_go_neg); 1156 #endif /* CONFIG_WIFI_DISPLAY */ 1157 1158 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF]) 1159 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF]); 1160 1161 buf = wpabuf_alloc(1000 + extra); 1162 if (buf == NULL) 1163 return NULL; 1164 1165 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_CONF, dialog_token); 1166 1167 len = p2p_buf_add_ie_hdr(buf); 1168 p2p_buf_add_status(buf, status); 1169 group_capab = 0; 1170 if (peer->go_state == LOCAL_GO) { 1171 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) { 1172 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP; 1173 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN) 1174 group_capab |= 1175 P2P_GROUP_CAPAB_PERSISTENT_RECONN; 1176 } 1177 if (p2p->cross_connect) 1178 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN; 1179 if (p2p->cfg->p2p_intra_bss) 1180 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST; 1181 } 1182 p2p_buf_add_capability(buf, p2p->dev_capab & 1183 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 1184 group_capab); 1185 if (go || resp_chan == NULL) 1186 p2p_buf_add_operating_channel(buf, p2p->cfg->country, 1187 p2p->op_reg_class, 1188 p2p->op_channel); 1189 else 1190 p2p_buf_add_operating_channel(buf, (const char *) resp_chan, 1191 resp_chan[3], resp_chan[4]); 1192 p2p_channels_intersect(&p2p->channels, &peer->channels, &res); 1193 is_6ghz_capab = is_p2p_6ghz_capable(p2p) && 1194 p2p_is_peer_6ghz_capab(p2p, peer->info.p2p_device_addr); 1195 p2p_buf_add_channel_list(buf, p2p->cfg->country, &res, is_6ghz_capab); 1196 if (go) { 1197 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid, 1198 p2p->ssid_len); 1199 } 1200 p2p_buf_update_ie_hdr(buf, len); 1201 1202 #ifdef CONFIG_WIFI_DISPLAY 1203 if (p2p->wfd_ie_go_neg) 1204 wpabuf_put_buf(buf, p2p->wfd_ie_go_neg); 1205 #endif /* CONFIG_WIFI_DISPLAY */ 1206 1207 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF]) 1208 wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF]); 1209 1210 return buf; 1211 } 1212 1213 1214 void p2p_process_go_neg_resp(struct p2p_data *p2p, const u8 *sa, 1215 const u8 *data, size_t len, int rx_freq) 1216 { 1217 struct p2p_device *dev; 1218 int go = -1; 1219 struct p2p_message msg; 1220 u8 status = P2P_SC_SUCCESS; 1221 int freq; 1222 1223 p2p_dbg(p2p, "Received GO Negotiation Response from " MACSTR 1224 " (freq=%d)", MAC2STR(sa), rx_freq); 1225 dev = p2p_get_device(p2p, sa); 1226 if (dev == NULL || dev->wps_method == WPS_NOT_READY || 1227 dev != p2p->go_neg_peer) { 1228 p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR, 1229 MAC2STR(sa)); 1230 return; 1231 } 1232 1233 if (p2p_parse(data, len, &msg)) 1234 return; 1235 1236 if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE)) { 1237 p2p_dbg(p2p, "Was not expecting GO Negotiation Response - ignore"); 1238 p2p_parse_free(&msg); 1239 return; 1240 } 1241 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE; 1242 p2p_update_peer_6ghz_capab(dev, &msg); 1243 1244 if (msg.dialog_token != dev->dialog_token) { 1245 p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)", 1246 msg.dialog_token, dev->dialog_token); 1247 p2p_parse_free(&msg); 1248 return; 1249 } 1250 1251 if (!msg.status) { 1252 p2p_dbg(p2p, "No Status attribute received"); 1253 status = P2P_SC_FAIL_INVALID_PARAMS; 1254 goto fail; 1255 } 1256 if (*msg.status) { 1257 p2p_dbg(p2p, "GO Negotiation rejected: status %d", *msg.status); 1258 dev->go_neg_req_sent = 0; 1259 if (*msg.status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) { 1260 p2p_dbg(p2p, "Wait for the peer to become ready for GO Negotiation"); 1261 dev->flags |= P2P_DEV_NOT_YET_READY; 1262 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, 1263 NULL); 1264 eloop_register_timeout(120, 0, p2p_go_neg_wait_timeout, 1265 p2p, NULL); 1266 if (p2p->state == P2P_CONNECT_LISTEN) 1267 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT); 1268 else 1269 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE); 1270 p2p_set_timeout(p2p, 0, 0); 1271 } else { 1272 p2p_dbg(p2p, "Stop GO Negotiation attempt"); 1273 p2p_go_neg_failed(p2p, *msg.status); 1274 } 1275 p2p->cfg->send_action_done(p2p->cfg->cb_ctx); 1276 p2p_parse_free(&msg); 1277 return; 1278 } 1279 1280 if (!msg.capability) { 1281 p2p_dbg(p2p, "Mandatory Capability attribute missing from GO Negotiation Response"); 1282 #ifdef CONFIG_P2P_STRICT 1283 status = P2P_SC_FAIL_INVALID_PARAMS; 1284 goto fail; 1285 #endif /* CONFIG_P2P_STRICT */ 1286 } 1287 1288 if (!msg.p2p_device_info) { 1289 p2p_dbg(p2p, "Mandatory P2P Device Info attribute missing from GO Negotiation Response"); 1290 #ifdef CONFIG_P2P_STRICT 1291 status = P2P_SC_FAIL_INVALID_PARAMS; 1292 goto fail; 1293 #endif /* CONFIG_P2P_STRICT */ 1294 } 1295 1296 if (!msg.intended_addr) { 1297 p2p_dbg(p2p, "No Intended P2P Interface Address attribute received"); 1298 status = P2P_SC_FAIL_INVALID_PARAMS; 1299 goto fail; 1300 } 1301 1302 if (!msg.go_intent) { 1303 p2p_dbg(p2p, "No GO Intent attribute received"); 1304 status = P2P_SC_FAIL_INVALID_PARAMS; 1305 goto fail; 1306 } 1307 if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) { 1308 p2p_dbg(p2p, "Invalid GO Intent value (%u) received", 1309 *msg.go_intent >> 1); 1310 status = P2P_SC_FAIL_INVALID_PARAMS; 1311 goto fail; 1312 } 1313 1314 go = p2p_go_det(p2p->go_intent, *msg.go_intent); 1315 if (go < 0) { 1316 p2p_dbg(p2p, "Incompatible GO Intent"); 1317 status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS; 1318 goto fail; 1319 } 1320 1321 if (!go && msg.group_id) { 1322 /* Store SSID for Provisioning step */ 1323 p2p->ssid_len = msg.group_id_len - ETH_ALEN; 1324 os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len); 1325 } else if (!go) { 1326 p2p_dbg(p2p, "Mandatory P2P Group ID attribute missing from GO Negotiation Response"); 1327 p2p->ssid_len = 0; 1328 status = P2P_SC_FAIL_INVALID_PARAMS; 1329 goto fail; 1330 } 1331 1332 if (!msg.config_timeout) { 1333 p2p_dbg(p2p, "Mandatory Configuration Timeout attribute missing from GO Negotiation Response"); 1334 #ifdef CONFIG_P2P_STRICT 1335 status = P2P_SC_FAIL_INVALID_PARAMS; 1336 goto fail; 1337 #endif /* CONFIG_P2P_STRICT */ 1338 } else { 1339 dev->go_timeout = msg.config_timeout[0]; 1340 dev->client_timeout = msg.config_timeout[1]; 1341 } 1342 1343 if (msg.wfd_subelems) { 1344 wpabuf_free(dev->info.wfd_subelems); 1345 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems); 1346 } 1347 1348 if (!msg.operating_channel && !go) { 1349 /* 1350 * Note: P2P Client may omit Operating Channel attribute to 1351 * indicate it does not have a preference. 1352 */ 1353 p2p_dbg(p2p, "No Operating Channel attribute received"); 1354 status = P2P_SC_FAIL_INVALID_PARAMS; 1355 goto fail; 1356 } 1357 if (!msg.channel_list) { 1358 p2p_dbg(p2p, "No Channel List attribute received"); 1359 status = P2P_SC_FAIL_INVALID_PARAMS; 1360 goto fail; 1361 } 1362 1363 if (p2p_peer_channels(p2p, dev, msg.channel_list, 1364 msg.channel_list_len) < 0) { 1365 p2p_dbg(p2p, "No common channels found"); 1366 status = P2P_SC_FAIL_NO_COMMON_CHANNELS; 1367 goto fail; 1368 } 1369 1370 if (msg.operating_channel) { 1371 dev->oper_freq = p2p_channel_to_freq(msg.operating_channel[3], 1372 msg.operating_channel[4]); 1373 p2p_dbg(p2p, "Peer operating channel preference: %d MHz", 1374 dev->oper_freq); 1375 } else 1376 dev->oper_freq = 0; 1377 1378 switch (msg.dev_password_id) { 1379 case DEV_PW_REGISTRAR_SPECIFIED: 1380 p2p_dbg(p2p, "PIN from peer Display"); 1381 if (dev->wps_method != WPS_PIN_KEYPAD) { 1382 p2p_dbg(p2p, "We have wps_method=%s -> incompatible", 1383 p2p_wps_method_str(dev->wps_method)); 1384 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD; 1385 goto fail; 1386 } 1387 break; 1388 case DEV_PW_USER_SPECIFIED: 1389 p2p_dbg(p2p, "Peer entered PIN on Keypad"); 1390 if (dev->wps_method != WPS_PIN_DISPLAY) { 1391 p2p_dbg(p2p, "We have wps_method=%s -> incompatible", 1392 p2p_wps_method_str(dev->wps_method)); 1393 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD; 1394 goto fail; 1395 } 1396 break; 1397 case DEV_PW_PUSHBUTTON: 1398 p2p_dbg(p2p, "Peer using pushbutton"); 1399 if (dev->wps_method != WPS_PBC) { 1400 p2p_dbg(p2p, "We have wps_method=%s -> incompatible", 1401 p2p_wps_method_str(dev->wps_method)); 1402 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD; 1403 goto fail; 1404 } 1405 break; 1406 case DEV_PW_P2PS_DEFAULT: 1407 p2p_dbg(p2p, "P2P: Peer using P2PS default pin"); 1408 if (dev->wps_method != WPS_P2PS) { 1409 p2p_dbg(p2p, "We have wps_method=%s -> incompatible", 1410 p2p_wps_method_str(dev->wps_method)); 1411 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD; 1412 goto fail; 1413 } 1414 break; 1415 default: 1416 if (msg.dev_password_id && 1417 msg.dev_password_id == dev->oob_pw_id) { 1418 p2p_dbg(p2p, "Peer using NFC"); 1419 if (dev->wps_method != WPS_NFC) { 1420 p2p_dbg(p2p, "We have wps_method=%s -> incompatible", 1421 p2p_wps_method_str(dev->wps_method)); 1422 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD; 1423 goto fail; 1424 } 1425 break; 1426 } 1427 p2p_dbg(p2p, "Unsupported Device Password ID %d", 1428 msg.dev_password_id); 1429 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD; 1430 goto fail; 1431 } 1432 1433 if (go && p2p_go_select_channel(p2p, dev, &status) < 0) 1434 goto fail; 1435 1436 /* 1437 * Use the driver preferred frequency list extension if local device is 1438 * GO. 1439 */ 1440 if (go) 1441 p2p_check_pref_chan(p2p, go, dev, &msg); 1442 1443 p2p_set_state(p2p, P2P_GO_NEG); 1444 p2p_clear_timeout(p2p); 1445 1446 p2p_dbg(p2p, "GO Negotiation with " MACSTR, MAC2STR(sa)); 1447 os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN); 1448 1449 fail: 1450 /* Store GO Negotiation Confirmation to allow retransmission */ 1451 wpabuf_free(dev->go_neg_conf); 1452 dev->go_neg_conf = p2p_build_go_neg_conf(p2p, dev, msg.dialog_token, 1453 status, msg.operating_channel, 1454 go); 1455 p2p_parse_free(&msg); 1456 if (dev->go_neg_conf == NULL) 1457 return; 1458 p2p_dbg(p2p, "Sending GO Negotiation Confirm"); 1459 if (status == P2P_SC_SUCCESS) { 1460 p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM; 1461 dev->go_state = go ? LOCAL_GO : REMOTE_GO; 1462 } else 1463 p2p->pending_action_state = P2P_NO_PENDING_ACTION; 1464 if (rx_freq > 0) 1465 freq = rx_freq; 1466 else 1467 freq = dev->listen_freq; 1468 1469 dev->go_neg_conf_freq = freq; 1470 dev->go_neg_conf_sent = 0; 1471 1472 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr, sa, 1473 wpabuf_head(dev->go_neg_conf), 1474 wpabuf_len(dev->go_neg_conf), 50) < 0) { 1475 p2p_dbg(p2p, "Failed to send Action frame"); 1476 p2p_go_neg_failed(p2p, -1); 1477 p2p->cfg->send_action_done(p2p->cfg->cb_ctx); 1478 } else 1479 dev->go_neg_conf_sent++; 1480 if (status != P2P_SC_SUCCESS) { 1481 p2p_dbg(p2p, "GO Negotiation failed"); 1482 p2p_go_neg_failed(p2p, status); 1483 } 1484 } 1485 1486 1487 void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa, 1488 const u8 *data, size_t len) 1489 { 1490 struct p2p_device *dev; 1491 struct p2p_message msg; 1492 1493 p2p_dbg(p2p, "Received GO Negotiation Confirm from " MACSTR, 1494 MAC2STR(sa)); 1495 dev = p2p_get_device(p2p, sa); 1496 if (dev == NULL || dev->wps_method == WPS_NOT_READY || 1497 dev != p2p->go_neg_peer) { 1498 p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR, 1499 MAC2STR(sa)); 1500 return; 1501 } 1502 1503 if (p2p->pending_action_state == P2P_PENDING_GO_NEG_RESPONSE) { 1504 p2p_dbg(p2p, "Stopped waiting for TX status on GO Negotiation Response since we already received Confirmation"); 1505 p2p->pending_action_state = P2P_NO_PENDING_ACTION; 1506 } 1507 1508 if (p2p_parse(data, len, &msg)) 1509 return; 1510 1511 if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) { 1512 p2p_dbg(p2p, "Was not expecting GO Negotiation Confirm - ignore"); 1513 p2p_parse_free(&msg); 1514 return; 1515 } 1516 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM; 1517 p2p->cfg->send_action_done(p2p->cfg->cb_ctx); 1518 1519 if (msg.dialog_token != dev->dialog_token) { 1520 p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)", 1521 msg.dialog_token, dev->dialog_token); 1522 p2p_parse_free(&msg); 1523 return; 1524 } 1525 1526 if (!msg.status) { 1527 p2p_dbg(p2p, "No Status attribute received"); 1528 p2p_parse_free(&msg); 1529 return; 1530 } 1531 if (*msg.status) { 1532 p2p_dbg(p2p, "GO Negotiation rejected: status %d", *msg.status); 1533 p2p_go_neg_failed(p2p, *msg.status); 1534 p2p_parse_free(&msg); 1535 return; 1536 } 1537 1538 p2p_update_peer_6ghz_capab(dev, &msg); 1539 1540 if (dev->go_state == REMOTE_GO && msg.group_id) { 1541 /* Store SSID for Provisioning step */ 1542 p2p->ssid_len = msg.group_id_len - ETH_ALEN; 1543 os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len); 1544 } else if (dev->go_state == REMOTE_GO) { 1545 p2p_dbg(p2p, "Mandatory P2P Group ID attribute missing from GO Negotiation Confirmation"); 1546 p2p->ssid_len = 0; 1547 p2p_go_neg_failed(p2p, P2P_SC_FAIL_INVALID_PARAMS); 1548 p2p_parse_free(&msg); 1549 return; 1550 } 1551 1552 if (!msg.operating_channel) { 1553 p2p_dbg(p2p, "Mandatory Operating Channel attribute missing from GO Negotiation Confirmation"); 1554 #ifdef CONFIG_P2P_STRICT 1555 p2p_parse_free(&msg); 1556 return; 1557 #endif /* CONFIG_P2P_STRICT */ 1558 } else if (dev->go_state == REMOTE_GO) { 1559 int oper_freq = p2p_channel_to_freq(msg.operating_channel[3], 1560 msg.operating_channel[4]); 1561 if (oper_freq != dev->oper_freq) { 1562 p2p_dbg(p2p, "Updated peer (GO) operating channel preference from %d MHz to %d MHz", 1563 dev->oper_freq, oper_freq); 1564 dev->oper_freq = oper_freq; 1565 } 1566 } 1567 1568 if (!msg.channel_list) { 1569 p2p_dbg(p2p, "Mandatory Operating Channel attribute missing from GO Negotiation Confirmation"); 1570 #ifdef CONFIG_P2P_STRICT 1571 p2p_parse_free(&msg); 1572 return; 1573 #endif /* CONFIG_P2P_STRICT */ 1574 } 1575 1576 p2p_parse_free(&msg); 1577 1578 if (dev->go_state == UNKNOWN_GO) { 1579 /* 1580 * This should not happen since GO negotiation has already 1581 * been completed. 1582 */ 1583 p2p_dbg(p2p, "Unexpected GO Neg state - do not know which end becomes GO"); 1584 return; 1585 } 1586 1587 /* 1588 * The peer could have missed our ctrl::ack frame for GO Negotiation 1589 * Confirm and continue retransmitting the frame. To reduce the 1590 * likelihood of the peer not getting successful TX status for the 1591 * GO Negotiation Confirm frame, wait a short time here before starting 1592 * the group so that we will remain on the current channel to 1593 * acknowledge any possible retransmission from the peer. 1594 */ 1595 p2p_dbg(p2p, "20 ms wait on current channel before starting group"); 1596 os_sleep(0, 20000); 1597 1598 p2p_go_complete(p2p, dev); 1599 } 1600