2 * Wi-Fi Direct - P2P Group Owner Negotiation
3 * Copyright (c) 2009-2010, Atheros Communications
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "common/ieee802_11_defs.h"
13 #include "wps/wps_defs.h"
18 static int p2p_go_det(u8 own_intent, u8 peer_value)
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 */
25 /* Use tie breaker bit to determine GO */
26 return (peer_value & 0x01) ? 0 : 1;
29 return own_intent > peer_intent;
33 int 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)
38 struct p2p_channels *ch;
40 struct p2p_channels intersection;
43 os_memset(ch, 0, sizeof(*ch));
45 end = channel_list + channel_list_len;
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],
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");
69 cl->channels = channels > P2P_MAX_REG_CLASS_CHANNELS ?
70 P2P_MAX_REG_CLASS_CHANNELS : channels;
71 os_memcpy(cl->channel, pos, cl->channels);
74 if (ch->reg_classes == P2P_MAX_REG_CLASSES)
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");
93 static int p2p_peer_channels(struct p2p_data *p2p, struct p2p_device *dev,
94 const u8 *channel_list, size_t channel_list_len)
96 return p2p_peer_channels_check(p2p, &p2p->channels, dev,
97 channel_list, channel_list_len);
101 u16 p2p_wps_method_pw_id(enum p2p_wps_method wps_method)
103 switch (wps_method) {
104 case WPS_PIN_DISPLAY:
105 return DEV_PW_REGISTRAR_SPECIFIED;
107 return DEV_PW_USER_SPECIFIED;
109 return DEV_PW_PUSHBUTTON;
111 return DEV_PW_DEFAULT;
116 static const char * p2p_wps_method_str(enum p2p_wps_method wps_method)
118 switch (wps_method) {
119 case WPS_PIN_DISPLAY:
131 static struct wpabuf * p2p_build_go_neg_req(struct p2p_data *p2p,
132 struct p2p_device *peer)
139 #ifdef CONFIG_WIFI_DISPLAY
140 if (p2p->wfd_ie_go_neg)
141 extra = wpabuf_len(p2p->wfd_ie_go_neg);
142 #endif /* CONFIG_WIFI_DISPLAY */
144 buf = wpabuf_alloc(1000 + extra);
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);
153 len = p2p_buf_add_ie_hdr(buf);
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;
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 &
165 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
167 p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) |
168 p2p->next_tie_breaker);
169 p2p->next_tie_breaker = !p2p->next_tie_breaker;
170 p2p_buf_add_config_timeout(buf, p2p->go_timeout, p2p->client_timeout);
171 p2p_buf_add_listen_channel(buf, p2p->cfg->country, p2p->cfg->reg_class,
173 if (p2p->ext_listen_interval)
174 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
175 p2p->ext_listen_interval);
176 p2p_buf_add_intended_addr(buf, p2p->intended_addr);
177 p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
178 p2p_buf_add_device_info(buf, p2p, peer);
179 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
180 p2p->op_reg_class, p2p->op_channel);
181 p2p_buf_update_ie_hdr(buf, len);
183 /* WPS IE with Device Password ID attribute */
184 p2p_build_wps_ie(p2p, buf, p2p_wps_method_pw_id(peer->wps_method), 0);
186 #ifdef CONFIG_WIFI_DISPLAY
187 if (p2p->wfd_ie_go_neg)
188 wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
189 #endif /* CONFIG_WIFI_DISPLAY */
195 int p2p_connect_send(struct p2p_data *p2p, struct p2p_device *dev)
200 if (dev->flags & P2P_DEV_PD_BEFORE_GO_NEG) {
202 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
203 "P2P: Use PD-before-GO-Neg workaround for " MACSTR,
204 MAC2STR(dev->info.p2p_device_addr));
205 if (dev->wps_method == WPS_PIN_DISPLAY)
206 config_method = WPS_CONFIG_KEYPAD;
207 else if (dev->wps_method == WPS_PIN_KEYPAD)
208 config_method = WPS_CONFIG_DISPLAY;
209 else if (dev->wps_method == WPS_PBC)
210 config_method = WPS_CONFIG_PUSHBUTTON;
213 return p2p_prov_disc_req(p2p, dev->info.p2p_device_addr,
214 config_method, 0, 0, 1);
217 freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
219 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
220 "P2P: No Listen/Operating frequency known for the "
221 "peer " MACSTR " to send GO Negotiation Request",
222 MAC2STR(dev->info.p2p_device_addr));
226 req = p2p_build_go_neg_req(p2p, dev);
229 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
230 "P2P: Sending GO Negotiation Request");
231 p2p_set_state(p2p, P2P_CONNECT);
232 p2p->pending_action_state = P2P_PENDING_GO_NEG_REQUEST;
233 p2p->go_neg_peer = dev;
234 dev->flags |= P2P_DEV_WAIT_GO_NEG_RESPONSE;
236 if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
237 p2p->cfg->dev_addr, dev->info.p2p_device_addr,
238 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
239 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
240 "P2P: Failed to send Action frame");
241 /* Use P2P find to recover and retry */
242 p2p_set_timeout(p2p, 0, 0);
244 dev->go_neg_req_sent++;
252 static struct wpabuf * p2p_build_go_neg_resp(struct p2p_data *p2p,
253 struct p2p_device *peer,
254 u8 dialog_token, u8 status,
262 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
263 "P2P: Building GO Negotiation Response");
265 #ifdef CONFIG_WIFI_DISPLAY
266 if (p2p->wfd_ie_go_neg)
267 extra = wpabuf_len(p2p->wfd_ie_go_neg);
268 #endif /* CONFIG_WIFI_DISPLAY */
270 buf = wpabuf_alloc(1000 + extra);
274 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_RESP, dialog_token);
276 len = p2p_buf_add_ie_hdr(buf);
277 p2p_buf_add_status(buf, status);
279 if (peer && peer->go_state == LOCAL_GO) {
280 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
281 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
282 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
284 P2P_GROUP_CAPAB_PERSISTENT_RECONN;
286 if (p2p->cross_connect)
287 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
288 if (p2p->cfg->p2p_intra_bss)
289 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
291 p2p_buf_add_capability(buf, p2p->dev_capab &
292 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
294 p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | tie_breaker);
295 p2p_buf_add_config_timeout(buf, p2p->go_timeout, p2p->client_timeout);
296 if (peer && peer->go_state == REMOTE_GO) {
297 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Omit Operating "
298 "Channel attribute");
300 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
304 p2p_buf_add_intended_addr(buf, p2p->intended_addr);
305 if (status || peer == NULL) {
306 p2p_buf_add_channel_list(buf, p2p->cfg->country,
308 } else if (peer->go_state == REMOTE_GO) {
309 p2p_buf_add_channel_list(buf, p2p->cfg->country,
312 struct p2p_channels res;
313 p2p_channels_intersect(&p2p->channels, &peer->channels,
315 p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
317 p2p_buf_add_device_info(buf, p2p, peer);
318 if (peer && peer->go_state == LOCAL_GO) {
319 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
322 p2p_buf_update_ie_hdr(buf, len);
324 /* WPS IE with Device Password ID attribute */
325 p2p_build_wps_ie(p2p, buf,
326 p2p_wps_method_pw_id(peer ? peer->wps_method :
329 #ifdef CONFIG_WIFI_DISPLAY
330 if (p2p->wfd_ie_go_neg)
331 wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
332 #endif /* CONFIG_WIFI_DISPLAY */
340 * p2p_reselect_channel - Re-select operating channel based on peer information
341 * @p2p: P2P module context from p2p_init()
342 * @intersection: Support channel list intersection from local and peer
344 * This function is used to re-select the best channel after having received
345 * information from the peer to allow supported channel lists to be intersected.
346 * This can be used to improve initial channel selection done in
347 * p2p_prepare_channel() prior to the start of GO Negotiation. In addition, this
348 * can be used for Invitation case.
350 void p2p_reselect_channel(struct p2p_data *p2p,
351 struct p2p_channels *intersection)
353 struct p2p_reg_class *cl;
355 u8 op_reg_class, op_channel;
358 /* First, try to pick the best channel from another band */
359 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->op_reg_class,
361 if (freq >= 2400 && freq < 2500 && p2p->best_freq_5 > 0 &&
362 p2p_freq_to_channel(p2p->cfg->country, p2p->best_freq_5,
363 &op_reg_class, &op_channel) == 0 &&
364 p2p_channels_includes(intersection, op_reg_class, op_channel)) {
365 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick best 5 GHz "
366 "channel (reg_class %u channel %u) from intersection",
367 op_reg_class, op_channel);
368 p2p->op_reg_class = op_reg_class;
369 p2p->op_channel = op_channel;
373 if (freq >= 4900 && freq < 6000 && p2p->best_freq_24 > 0 &&
374 p2p_freq_to_channel(p2p->cfg->country, p2p->best_freq_24,
375 &op_reg_class, &op_channel) == 0 &&
376 p2p_channels_includes(intersection, op_reg_class, op_channel)) {
377 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick best 2.4 GHz "
378 "channel (reg_class %u channel %u) from intersection",
379 op_reg_class, op_channel);
380 p2p->op_reg_class = op_reg_class;
381 p2p->op_channel = op_channel;
385 /* Select channel with highest preference if the peer supports it */
386 for (i = 0; p2p->cfg->pref_chan && i < p2p->cfg->num_pref_chan; i++) {
387 if (p2p_channels_includes(intersection,
388 p2p->cfg->pref_chan[i].op_class,
389 p2p->cfg->pref_chan[i].chan)) {
390 p2p->op_reg_class = p2p->cfg->pref_chan[i].op_class;
391 p2p->op_channel = p2p->cfg->pref_chan[i].chan;
392 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick "
393 "highest preferred chnnel (op_class %u "
394 "channel %u) from intersection",
395 p2p->op_reg_class, p2p->op_channel);
400 /* Try a channel where we might be able to use HT40 */
401 for (i = 0; i < intersection->reg_classes; i++) {
402 struct p2p_reg_class *c = &intersection->reg_class[i];
403 if (c->reg_class == 116 || c->reg_class == 117 ||
404 c->reg_class == 126 || c->reg_class == 127) {
405 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
406 "P2P: Pick possible HT40 channel (reg_class "
407 "%u channel %u) from intersection",
408 c->reg_class, c->channel[0]);
409 p2p->op_reg_class = c->reg_class;
410 p2p->op_channel = c->channel[0];
416 * Try to see if the original channel is in the intersection. If
417 * so, no need to change anything, as it already contains some
420 if (p2p_channels_includes(intersection, p2p->op_reg_class,
422 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
423 "P2P: Using original operating class and channel "
424 "(op_class %u channel %u) from intersection",
425 p2p->op_reg_class, p2p->op_channel);
430 * Fall back to whatever is included in the channel intersection since
431 * no better options seems to be available.
433 cl = &intersection->reg_class[0];
434 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick another channel "
435 "(reg_class %u channel %u) from intersection",
436 cl->reg_class, cl->channel[0]);
437 p2p->op_reg_class = cl->reg_class;
438 p2p->op_channel = cl->channel[0];
442 static int p2p_go_select_channel(struct p2p_data *p2p, struct p2p_device *dev,
445 struct p2p_channels intersection;
448 p2p_channels_intersect(&p2p->channels, &dev->channels, &intersection);
449 if (intersection.reg_classes == 0 ||
450 intersection.reg_class[0].channels == 0) {
451 *status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
452 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
453 "P2P: No common channels found");
457 for (i = 0; i < intersection.reg_classes; i++) {
458 struct p2p_reg_class *c;
459 c = &intersection.reg_class[i];
460 wpa_printf(MSG_DEBUG, "P2P: reg_class %u", c->reg_class);
461 wpa_hexdump(MSG_DEBUG, "P2P: channels",
462 c->channel, c->channels);
465 if (!p2p_channels_includes(&intersection, p2p->op_reg_class,
467 if (dev->flags & P2P_DEV_FORCE_FREQ) {
468 *status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
469 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer does "
470 "not support the forced channel");
474 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Selected operating "
475 "channel (op_class %u channel %u) not acceptable to "
476 "the peer", p2p->op_reg_class, p2p->op_channel);
477 p2p_reselect_channel(p2p, &intersection);
478 } else if (!(dev->flags & P2P_DEV_FORCE_FREQ) &&
479 !p2p->cfg->cfg_op_channel) {
480 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Try to optimize "
481 "channel selection with peer information received; "
482 "previously selected op_class %u channel %u",
483 p2p->op_reg_class, p2p->op_channel);
484 p2p_reselect_channel(p2p, &intersection);
487 if (!p2p->ssid_set) {
488 p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
496 void p2p_process_go_neg_req(struct p2p_data *p2p, const u8 *sa,
497 const u8 *data, size_t len, int rx_freq)
499 struct p2p_device *dev = NULL;
501 struct p2p_message msg;
502 u8 status = P2P_SC_FAIL_INVALID_PARAMS;
506 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
507 "P2P: Received GO Negotiation Request from " MACSTR
508 "(freq=%d)", MAC2STR(sa), rx_freq);
510 if (p2p_parse(data, len, &msg))
513 if (!msg.capability) {
514 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
515 "P2P: Mandatory Capability attribute missing from GO "
516 "Negotiation Request");
517 #ifdef CONFIG_P2P_STRICT
519 #endif /* CONFIG_P2P_STRICT */
523 tie_breaker = *msg.go_intent & 0x01;
525 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
526 "P2P: Mandatory GO Intent attribute missing from GO "
527 "Negotiation Request");
528 #ifdef CONFIG_P2P_STRICT
530 #endif /* CONFIG_P2P_STRICT */
533 if (!msg.config_timeout) {
534 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
535 "P2P: Mandatory Configuration Timeout attribute "
536 "missing from GO Negotiation Request");
537 #ifdef CONFIG_P2P_STRICT
539 #endif /* CONFIG_P2P_STRICT */
542 if (!msg.listen_channel) {
543 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
544 "P2P: No Listen Channel attribute received");
547 if (!msg.operating_channel) {
548 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
549 "P2P: No Operating Channel attribute received");
552 if (!msg.channel_list) {
553 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
554 "P2P: No Channel List attribute received");
557 if (!msg.intended_addr) {
558 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
559 "P2P: No Intended P2P Interface Address attribute "
563 if (!msg.p2p_device_info) {
564 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
565 "P2P: No P2P Device Info attribute received");
569 if (os_memcmp(msg.p2p_device_addr, sa, ETH_ALEN) != 0) {
570 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
571 "P2P: Unexpected GO Negotiation Request SA=" MACSTR
572 " != dev_addr=" MACSTR,
573 MAC2STR(sa), MAC2STR(msg.p2p_device_addr));
577 dev = p2p_get_device(p2p, sa);
579 if (msg.status && *msg.status) {
580 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
581 "P2P: Unexpected Status attribute (%d) in GO "
582 "Negotiation Request", *msg.status);
587 dev = p2p_add_dev_from_go_neg_req(p2p, sa, &msg);
588 else if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
589 p2p_add_dev_info(p2p, sa, dev, &msg);
590 if (dev && dev->flags & P2P_DEV_USER_REJECTED) {
591 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
592 "P2P: User has rejected this peer");
593 status = P2P_SC_FAIL_REJECTED_BY_USER;
594 } else if (dev == NULL || dev->wps_method == WPS_NOT_READY) {
595 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
596 "P2P: Not ready for GO negotiation with " MACSTR,
598 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
600 dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
601 p2p->cfg->go_neg_req_rx(p2p->cfg->cb_ctx, sa,
602 msg.dev_password_id);
603 } else if (p2p->go_neg_peer && p2p->go_neg_peer != dev) {
604 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
605 "P2P: Already in Group Formation with another peer");
606 status = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
610 if (!p2p->go_neg_peer) {
611 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting "
612 "GO Negotiation with previously authorized "
614 if (!(dev->flags & P2P_DEV_FORCE_FREQ)) {
615 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
616 "P2P: Use default channel settings");
617 p2p->op_reg_class = p2p->cfg->op_reg_class;
618 p2p->op_channel = p2p->cfg->op_channel;
619 os_memcpy(&p2p->channels, &p2p->cfg->channels,
620 sizeof(struct p2p_channels));
622 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
623 "P2P: Use previously configured "
624 "forced channel settings");
628 dev->flags &= ~P2P_DEV_NOT_YET_READY;
630 if (!msg.go_intent) {
631 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
632 "P2P: No GO Intent attribute received");
635 if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
636 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
637 "P2P: Invalid GO Intent value (%u) received",
638 *msg.go_intent >> 1);
642 if (dev->go_neg_req_sent &&
643 os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) > 0) {
644 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
645 "P2P: Do not reply since peer has higher "
646 "address and GO Neg Request already sent");
647 p2p_parse_free(&msg);
651 go = p2p_go_det(p2p->go_intent, *msg.go_intent);
653 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
654 "P2P: Incompatible GO Intent");
655 status = P2P_SC_FAIL_BOTH_GO_INTENT_15;
659 if (p2p_peer_channels(p2p, dev, msg.channel_list,
660 msg.channel_list_len) < 0) {
661 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
662 "P2P: No common channels found");
663 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
667 switch (msg.dev_password_id) {
668 case DEV_PW_REGISTRAR_SPECIFIED:
669 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
670 "P2P: PIN from peer Display");
671 if (dev->wps_method != WPS_PIN_KEYPAD) {
672 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
673 "P2P: We have wps_method=%s -> "
675 p2p_wps_method_str(dev->wps_method));
676 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
680 case DEV_PW_USER_SPECIFIED:
681 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
682 "P2P: Peer entered PIN on Keypad");
683 if (dev->wps_method != WPS_PIN_DISPLAY) {
684 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
685 "P2P: We have wps_method=%s -> "
687 p2p_wps_method_str(dev->wps_method));
688 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
692 case DEV_PW_PUSHBUTTON:
693 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
694 "P2P: Peer using pushbutton");
695 if (dev->wps_method != WPS_PBC) {
696 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
697 "P2P: We have wps_method=%s -> "
699 p2p_wps_method_str(dev->wps_method));
700 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
705 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
706 "P2P: Unsupported Device Password ID %d",
707 msg.dev_password_id);
708 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
712 if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
715 dev->go_state = go ? LOCAL_GO : REMOTE_GO;
716 dev->oper_freq = p2p_channel_to_freq((const char *)
717 msg.operating_channel,
718 msg.operating_channel[3],
719 msg.operating_channel[4]);
720 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
721 "channel preference: %d MHz", dev->oper_freq);
723 if (msg.config_timeout) {
724 dev->go_timeout = msg.config_timeout[0];
725 dev->client_timeout = msg.config_timeout[1];
728 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
729 "P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
730 if (p2p->state != P2P_IDLE)
731 p2p_stop_find_for_freq(p2p, rx_freq);
732 p2p_set_state(p2p, P2P_GO_NEG);
733 p2p_clear_timeout(p2p);
734 dev->dialog_token = msg.dialog_token;
735 os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
736 p2p->go_neg_peer = dev;
737 status = P2P_SC_SUCCESS;
742 dev->status = status;
743 resp = p2p_build_go_neg_resp(p2p, dev, msg.dialog_token, status,
745 p2p_parse_free(&msg);
748 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
749 "P2P: Sending GO Negotiation Response");
753 freq = p2p_channel_to_freq(p2p->cfg->country,
757 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
758 "P2P: Unknown regulatory class/channel");
762 if (status == P2P_SC_SUCCESS) {
763 p2p->pending_action_state = P2P_PENDING_GO_NEG_RESPONSE;
764 dev->flags |= P2P_DEV_WAIT_GO_NEG_CONFIRM;
765 if (os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) < 0) {
767 * Peer has smaller address, so the GO Negotiation
768 * Response from us is expected to complete
769 * negotiation. Ignore a GO Negotiation Response from
770 * the peer if it happens to be received after this
771 * point due to a race condition in GO Negotiation
772 * Request transmission and processing.
774 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
777 p2p->pending_action_state =
778 P2P_PENDING_GO_NEG_RESPONSE_FAILURE;
779 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
781 wpabuf_head(resp), wpabuf_len(resp), 250) < 0) {
782 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
783 "P2P: Failed to send Action frame");
790 static struct wpabuf * p2p_build_go_neg_conf(struct p2p_data *p2p,
791 struct p2p_device *peer,
792 u8 dialog_token, u8 status,
793 const u8 *resp_chan, int go)
797 struct p2p_channels res;
801 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
802 "P2P: Building GO Negotiation Confirm");
804 #ifdef CONFIG_WIFI_DISPLAY
805 if (p2p->wfd_ie_go_neg)
806 extra = wpabuf_len(p2p->wfd_ie_go_neg);
807 #endif /* CONFIG_WIFI_DISPLAY */
809 buf = wpabuf_alloc(1000 + extra);
813 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_CONF, dialog_token);
815 len = p2p_buf_add_ie_hdr(buf);
816 p2p_buf_add_status(buf, status);
818 if (peer->go_state == LOCAL_GO) {
819 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
820 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
821 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
823 P2P_GROUP_CAPAB_PERSISTENT_RECONN;
825 if (p2p->cross_connect)
826 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
827 if (p2p->cfg->p2p_intra_bss)
828 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
830 p2p_buf_add_capability(buf, p2p->dev_capab &
831 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
833 if (go || resp_chan == NULL)
834 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
838 p2p_buf_add_operating_channel(buf, (const char *) resp_chan,
839 resp_chan[3], resp_chan[4]);
840 p2p_channels_intersect(&p2p->channels, &peer->channels, &res);
841 p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
843 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
846 p2p_buf_update_ie_hdr(buf, len);
848 #ifdef CONFIG_WIFI_DISPLAY
849 if (p2p->wfd_ie_go_neg)
850 wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
851 #endif /* CONFIG_WIFI_DISPLAY */
857 void p2p_process_go_neg_resp(struct p2p_data *p2p, const u8 *sa,
858 const u8 *data, size_t len, int rx_freq)
860 struct p2p_device *dev;
863 struct p2p_message msg;
864 u8 status = P2P_SC_SUCCESS;
867 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
868 "P2P: Received GO Negotiation Response from " MACSTR
869 " (freq=%d)", MAC2STR(sa), rx_freq);
870 dev = p2p_get_device(p2p, sa);
871 if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
872 dev != p2p->go_neg_peer) {
873 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
874 "P2P: Not ready for GO negotiation with " MACSTR,
879 if (p2p_parse(data, len, &msg))
882 if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE)) {
883 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
884 "P2P: Was not expecting GO Negotiation Response - "
886 p2p_parse_free(&msg);
889 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
891 if (msg.dialog_token != dev->dialog_token) {
892 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
893 "P2P: Unexpected Dialog Token %u (expected %u)",
894 msg.dialog_token, dev->dialog_token);
895 p2p_parse_free(&msg);
900 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
901 "P2P: No Status attribute received");
902 status = P2P_SC_FAIL_INVALID_PARAMS;
906 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
907 "P2P: GO Negotiation rejected: status %d",
909 dev->go_neg_req_sent = 0;
910 if (*msg.status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
911 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
912 "P2P: Wait for the peer to become ready for "
914 dev->flags |= P2P_DEV_NOT_YET_READY;
916 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
917 p2p_set_timeout(p2p, 0, 0);
919 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
920 "P2P: Stop GO Negotiation attempt");
921 p2p_go_neg_failed(p2p, dev, *msg.status);
923 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
924 p2p_parse_free(&msg);
928 if (!msg.capability) {
929 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
930 "P2P: Mandatory Capability attribute missing from GO "
931 "Negotiation Response");
932 #ifdef CONFIG_P2P_STRICT
933 status = P2P_SC_FAIL_INVALID_PARAMS;
935 #endif /* CONFIG_P2P_STRICT */
938 if (!msg.p2p_device_info) {
939 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
940 "P2P: Mandatory P2P Device Info attribute missing "
941 "from GO Negotiation Response");
942 #ifdef CONFIG_P2P_STRICT
943 status = P2P_SC_FAIL_INVALID_PARAMS;
945 #endif /* CONFIG_P2P_STRICT */
948 if (!msg.intended_addr) {
949 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
950 "P2P: No Intended P2P Interface Address attribute "
952 status = P2P_SC_FAIL_INVALID_PARAMS;
956 if (!msg.go_intent) {
957 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
958 "P2P: No GO Intent attribute received");
959 status = P2P_SC_FAIL_INVALID_PARAMS;
962 if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
963 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
964 "P2P: Invalid GO Intent value (%u) received",
965 *msg.go_intent >> 1);
966 status = P2P_SC_FAIL_INVALID_PARAMS;
970 go = p2p_go_det(p2p->go_intent, *msg.go_intent);
972 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
973 "P2P: Incompatible GO Intent");
974 status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
978 if (!go && msg.group_id) {
979 /* Store SSID for Provisioning step */
980 p2p->ssid_len = msg.group_id_len - ETH_ALEN;
981 os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
983 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
984 "P2P: Mandatory P2P Group ID attribute missing from "
985 "GO Negotiation Response");
987 #ifdef CONFIG_P2P_STRICT
988 status = P2P_SC_FAIL_INVALID_PARAMS;
990 #endif /* CONFIG_P2P_STRICT */
993 if (!msg.config_timeout) {
994 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
995 "P2P: Mandatory Configuration Timeout attribute "
996 "missing from GO Negotiation Response");
997 #ifdef CONFIG_P2P_STRICT
998 status = P2P_SC_FAIL_INVALID_PARAMS;
1000 #endif /* CONFIG_P2P_STRICT */
1002 dev->go_timeout = msg.config_timeout[0];
1003 dev->client_timeout = msg.config_timeout[1];
1006 if (!msg.operating_channel && !go) {
1008 * Note: P2P Client may omit Operating Channel attribute to
1009 * indicate it does not have a preference.
1011 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1012 "P2P: No Operating Channel attribute received");
1013 status = P2P_SC_FAIL_INVALID_PARAMS;
1016 if (!msg.channel_list) {
1017 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1018 "P2P: No Channel List attribute received");
1019 status = P2P_SC_FAIL_INVALID_PARAMS;
1023 if (p2p_peer_channels(p2p, dev, msg.channel_list,
1024 msg.channel_list_len) < 0) {
1025 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1026 "P2P: No common channels found");
1027 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
1031 if (msg.operating_channel) {
1032 dev->oper_freq = p2p_channel_to_freq((const char *)
1033 msg.operating_channel,
1034 msg.operating_channel[3],
1035 msg.operating_channel[4]);
1036 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
1037 "channel preference: %d MHz", dev->oper_freq);
1041 switch (msg.dev_password_id) {
1042 case DEV_PW_REGISTRAR_SPECIFIED:
1043 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1044 "P2P: PIN from peer Display");
1045 if (dev->wps_method != WPS_PIN_KEYPAD) {
1046 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1047 "P2P: We have wps_method=%s -> "
1049 p2p_wps_method_str(dev->wps_method));
1050 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1054 case DEV_PW_USER_SPECIFIED:
1055 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1056 "P2P: Peer entered PIN on Keypad");
1057 if (dev->wps_method != WPS_PIN_DISPLAY) {
1058 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1059 "P2P: We have wps_method=%s -> "
1061 p2p_wps_method_str(dev->wps_method));
1062 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1066 case DEV_PW_PUSHBUTTON:
1067 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1068 "P2P: Peer using pushbutton");
1069 if (dev->wps_method != WPS_PBC) {
1070 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1071 "P2P: We have wps_method=%s -> "
1073 p2p_wps_method_str(dev->wps_method));
1074 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1079 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1080 "P2P: Unsupported Device Password ID %d",
1081 msg.dev_password_id);
1082 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1086 if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
1089 p2p_set_state(p2p, P2P_GO_NEG);
1090 p2p_clear_timeout(p2p);
1092 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1093 "P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
1094 os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
1097 conf = p2p_build_go_neg_conf(p2p, dev, msg.dialog_token, status,
1098 msg.operating_channel, go);
1099 p2p_parse_free(&msg);
1102 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1103 "P2P: Sending GO Negotiation Confirm");
1104 if (status == P2P_SC_SUCCESS) {
1105 p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
1106 dev->go_state = go ? LOCAL_GO : REMOTE_GO;
1108 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1112 freq = dev->listen_freq;
1113 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr, sa,
1114 wpabuf_head(conf), wpabuf_len(conf), 0) < 0) {
1115 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1116 "P2P: Failed to send Action frame");
1117 p2p_go_neg_failed(p2p, dev, -1);
1123 void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa,
1124 const u8 *data, size_t len)
1126 struct p2p_device *dev;
1127 struct p2p_message msg;
1129 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1130 "P2P: Received GO Negotiation Confirm from " MACSTR,
1132 dev = p2p_get_device(p2p, sa);
1133 if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
1134 dev != p2p->go_neg_peer) {
1135 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1136 "P2P: Not ready for GO negotiation with " MACSTR,
1141 if (p2p->pending_action_state == P2P_PENDING_GO_NEG_RESPONSE) {
1142 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopped waiting "
1143 "for TX status on GO Negotiation Response since we "
1144 "already received Confirmation");
1145 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1148 if (p2p_parse(data, len, &msg))
1151 if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
1152 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1153 "P2P: Was not expecting GO Negotiation Confirm - "
1157 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1159 if (msg.dialog_token != dev->dialog_token) {
1160 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1161 "P2P: Unexpected Dialog Token %u (expected %u)",
1162 msg.dialog_token, dev->dialog_token);
1163 p2p_parse_free(&msg);
1168 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1169 "P2P: No Status attribute received");
1170 p2p_parse_free(&msg);
1174 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1175 "P2P: GO Negotiation rejected: status %d",
1177 p2p_parse_free(&msg);
1181 if (dev->go_state == REMOTE_GO && msg.group_id) {
1182 /* Store SSID for Provisioning step */
1183 p2p->ssid_len = msg.group_id_len - ETH_ALEN;
1184 os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
1185 } else if (dev->go_state == REMOTE_GO) {
1186 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1187 "P2P: Mandatory P2P Group ID attribute missing from "
1188 "GO Negotiation Confirmation");
1190 #ifdef CONFIG_P2P_STRICT
1191 p2p_parse_free(&msg);
1193 #endif /* CONFIG_P2P_STRICT */
1196 if (!msg.operating_channel) {
1197 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1198 "P2P: Mandatory Operating Channel attribute missing "
1199 "from GO Negotiation Confirmation");
1200 #ifdef CONFIG_P2P_STRICT
1201 p2p_parse_free(&msg);
1203 #endif /* CONFIG_P2P_STRICT */
1206 if (!msg.channel_list) {
1207 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1208 "P2P: Mandatory Operating Channel attribute missing "
1209 "from GO Negotiation Confirmation");
1210 #ifdef CONFIG_P2P_STRICT
1211 p2p_parse_free(&msg);
1213 #endif /* CONFIG_P2P_STRICT */
1216 p2p_parse_free(&msg);
1218 if (dev->go_state == UNKNOWN_GO) {
1220 * This should not happen since GO negotiation has already
1223 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1224 "P2P: Unexpected GO Neg state - do not know which end "
1230 * The peer could have missed our ctrl::ack frame for GO Negotiation
1231 * Confirm and continue retransmitting the frame. To reduce the
1232 * likelihood of the peer not getting successful TX status for the
1233 * GO Negotiation Confirm frame, wait a short time here before starting
1234 * the group so that we will remain on the current channel to
1235 * acknowledge any possible retransmission from the peer.
1237 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: 20 ms wait on current "
1238 "channel before starting group");
1241 p2p_go_complete(p2p, dev);