2 * Wi-Fi Direct - P2P Group Owner Negotiation
3 * Copyright (c) 2009-2010, Atheros Communications
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
18 #include "common/ieee802_11_defs.h"
19 #include "wps/wps_defs.h"
24 static int p2p_go_det(u8 own_intent, u8 peer_value)
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 */
31 /* Use tie breaker bit to determine GO */
32 return (peer_value & 0x01) ? 0 : 1;
35 return own_intent > peer_intent;
39 int 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)
44 struct p2p_channels *ch;
46 struct p2p_channels intersection;
49 os_memset(ch, 0, sizeof(*ch));
51 end = channel_list + channel_list_len;
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],
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");
75 cl->channels = channels > P2P_MAX_REG_CLASS_CHANNELS ?
76 P2P_MAX_REG_CLASS_CHANNELS : channels;
77 os_memcpy(cl->channel, pos, cl->channels);
80 if (ch->reg_classes == P2P_MAX_REG_CLASSES)
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");
99 static int p2p_peer_channels(struct p2p_data *p2p, struct p2p_device *dev,
100 const u8 *channel_list, size_t channel_list_len)
102 return p2p_peer_channels_check(p2p, &p2p->channels, dev,
103 channel_list, channel_list_len);
107 static u16 p2p_wps_method_pw_id(enum p2p_wps_method wps_method)
109 switch (wps_method) {
111 return DEV_PW_DEFAULT;
112 case WPS_PIN_DISPLAY:
113 return DEV_PW_REGISTRAR_SPECIFIED;
115 return DEV_PW_USER_SPECIFIED;
117 return DEV_PW_PUSHBUTTON;
119 return DEV_PW_DEFAULT;
124 static const char * p2p_wps_method_str(enum p2p_wps_method wps_method)
126 switch (wps_method) {
129 case WPS_PIN_DISPLAY:
141 static struct wpabuf * p2p_build_go_neg_req(struct p2p_data *p2p,
142 struct p2p_device *peer)
148 buf = wpabuf_alloc(1000);
152 peer->dialog_token++;
153 if (peer->dialog_token == 0)
154 peer->dialog_token = 1;
155 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_REQ, peer->dialog_token);
157 len = p2p_buf_add_ie_hdr(buf);
159 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP)
160 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
161 if (p2p->cross_connect)
162 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
163 p2p_buf_add_capability(buf, p2p->dev_capab, group_capab);
164 p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) |
165 p2p->next_tie_breaker);
166 p2p->next_tie_breaker = !p2p->next_tie_breaker;
167 p2p_buf_add_config_timeout(buf, 100, 20);
168 p2p_buf_add_listen_channel(buf, p2p->cfg->country, p2p->cfg->reg_class,
170 if (p2p->ext_listen_interval)
171 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
172 p2p->ext_listen_interval);
173 p2p_buf_add_intended_addr(buf, p2p->intended_addr);
174 p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
175 p2p_buf_add_device_info(buf, p2p, peer);
176 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
177 p2p->op_reg_class, p2p->op_channel);
178 p2p_buf_update_ie_hdr(buf, len);
180 /* WPS IE with Device Password ID attribute */
181 p2p_build_wps_ie(p2p, buf, p2p_wps_method_pw_id(peer->wps_method), 0);
187 int p2p_connect_send(struct p2p_data *p2p, struct p2p_device *dev)
192 freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
194 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
195 "P2P: No Listen/Operating frequency known for the "
196 "peer " MACSTR " to send GO Negotiation Request",
197 MAC2STR(dev->p2p_device_addr));
201 req = p2p_build_go_neg_req(p2p, dev);
204 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
205 "P2P: Sending GO Negotiation Request");
206 p2p_set_state(p2p, P2P_CONNECT);
207 p2p->pending_action_state = P2P_PENDING_GO_NEG_REQUEST;
208 p2p->go_neg_peer = dev;
209 dev->flags |= P2P_DEV_WAIT_GO_NEG_RESPONSE;
210 if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq,
211 dev->p2p_device_addr, p2p->cfg->dev_addr,
212 dev->p2p_device_addr,
213 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);
227 static struct wpabuf * p2p_build_go_neg_resp(struct p2p_data *p2p,
228 struct p2p_device *peer,
229 u8 dialog_token, u8 status,
236 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
237 "P2P: Building GO Negotiation Response");
238 buf = wpabuf_alloc(1000);
242 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_RESP, dialog_token);
244 len = p2p_buf_add_ie_hdr(buf);
245 p2p_buf_add_status(buf, status);
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 (p2p->cross_connect)
251 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
253 p2p_buf_add_capability(buf, p2p->dev_capab, group_capab);
254 p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | tie_breaker);
255 p2p_buf_add_config_timeout(buf, 100, 20);
256 if (peer && peer->go_state == REMOTE_GO) {
257 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Omit Operating "
258 "Channel attribute");
260 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
264 p2p_buf_add_intended_addr(buf, p2p->intended_addr);
265 if (status || peer == NULL) {
266 p2p_buf_add_channel_list(buf, p2p->cfg->country,
268 } else if (peer->go_state == REMOTE_GO) {
269 p2p_buf_add_channel_list(buf, p2p->cfg->country,
272 struct p2p_channels res;
273 p2p_channels_intersect(&p2p->channels, &peer->channels,
275 p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
277 p2p_buf_add_device_info(buf, p2p, peer);
278 if (peer && peer->go_state == LOCAL_GO) {
279 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
282 p2p_buf_update_ie_hdr(buf, len);
284 /* WPS IE with Device Password ID attribute */
285 p2p_build_wps_ie(p2p, buf,
286 p2p_wps_method_pw_id(peer ? peer->wps_method :
293 void p2p_process_go_neg_req(struct p2p_data *p2p, const u8 *sa,
294 const u8 *data, size_t len, int rx_freq)
296 struct p2p_device *dev = NULL;
298 struct p2p_message msg;
299 u8 status = P2P_SC_FAIL_INVALID_PARAMS;
303 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
304 "P2P: Received GO Negotiation Request from " MACSTR
305 "(freq=%d)", MAC2STR(sa), rx_freq);
307 if (p2p_parse(data, len, &msg))
310 if (!msg.capability) {
311 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
312 "P2P: Mandatory Capability attribute missing from GO "
313 "Negotiation Request");
314 #ifdef CONFIG_P2P_STRICT
316 #endif /* CONFIG_P2P_STRICT */
320 tie_breaker = *msg.go_intent & 0x01;
322 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
323 "P2P: Mandatory GO Intent attribute missing from GO "
324 "Negotiation Request");
325 #ifdef CONFIG_P2P_STRICT
327 #endif /* CONFIG_P2P_STRICT */
330 if (!msg.config_timeout) {
331 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
332 "P2P: Mandatory Configuration Timeout attribute "
333 "missing from GO Negotiation Request");
334 #ifdef CONFIG_P2P_STRICT
336 #endif /* CONFIG_P2P_STRICT */
339 if (!msg.listen_channel) {
340 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
341 "P2P: No Listen Channel attribute received");
344 if (!msg.operating_channel) {
345 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
346 "P2P: No Operating Channel attribute received");
349 if (!msg.channel_list) {
350 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
351 "P2P: No Channel List attribute received");
354 if (!msg.intended_addr) {
355 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
356 "P2P: No Intended P2P Interface Address attribute "
360 if (!msg.p2p_device_info) {
361 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
362 "P2P: No P2P Device Info attribute received");
366 if (os_memcmp(msg.p2p_device_addr, sa, ETH_ALEN) != 0) {
367 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
368 "P2P: Unexpected GO Negotiation Request SA=" MACSTR
369 " != dev_addr=" MACSTR,
370 MAC2STR(sa), MAC2STR(msg.p2p_device_addr));
374 dev = p2p_get_device(p2p, sa);
376 if (msg.status && *msg.status) {
377 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
378 "P2P: Unexpected Status attribute (%d) in GO "
379 "Negotiation Request", *msg.status);
384 dev = p2p_add_dev_from_go_neg_req(p2p, sa, &msg);
385 else if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
386 p2p_add_dev_info(p2p, sa, dev, &msg);
387 if (dev && dev->flags & P2P_DEV_USER_REJECTED) {
388 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
389 "P2P: User has rejected this peer");
390 status = P2P_SC_FAIL_REJECTED_BY_USER;
391 } else if (dev == NULL || dev->wps_method == WPS_NOT_READY) {
392 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
393 "P2P: Not ready for GO negotiation with " MACSTR,
395 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
397 dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
398 p2p->cfg->go_neg_req_rx(p2p->cfg->cb_ctx, sa,
399 msg.dev_password_id);
400 } else if (p2p->go_neg_peer && p2p->go_neg_peer != dev) {
401 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
402 "P2P: Already in Group Formation with another peer");
403 status = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
407 if (!p2p->go_neg_peer) {
408 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting "
409 "GO Negotiation with previously authorized "
411 if (!(dev->flags & P2P_DEV_FORCE_FREQ)) {
412 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
413 "P2P: Use default channel settings");
414 p2p->op_reg_class = p2p->cfg->op_reg_class;
415 p2p->op_channel = p2p->cfg->op_channel;
416 os_memcpy(&p2p->channels, &p2p->cfg->channels,
417 sizeof(struct p2p_channels));
419 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
420 "P2P: Use previously configured "
421 "forced channel settings");
425 dev->flags &= ~P2P_DEV_NOT_YET_READY;
427 if (!msg.go_intent) {
428 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
429 "P2P: No GO Intent attribute received");
432 if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
433 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
434 "P2P: Invalid GO Intent value (%u) received",
435 *msg.go_intent >> 1);
439 if (dev->go_neg_req_sent &&
440 os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) > 0) {
441 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
442 "P2P: Do not reply since peer has higher "
443 "address and GO Neg Request already sent");
444 p2p_parse_free(&msg);
448 go = p2p_go_det(p2p->go_intent, *msg.go_intent);
450 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
451 "P2P: Incompatible GO Intent");
452 status = P2P_SC_FAIL_BOTH_GO_INTENT_15;
456 if (p2p_peer_channels(p2p, dev, msg.channel_list,
457 msg.channel_list_len) < 0) {
458 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
459 "P2P: No common channels found");
460 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
464 switch (msg.dev_password_id) {
466 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
467 "P2P: PIN from peer Label");
468 if (dev->wps_method != WPS_PIN_KEYPAD) {
469 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
470 "P2P: We have wps_method=%s -> "
472 p2p_wps_method_str(dev->wps_method));
473 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
477 case DEV_PW_REGISTRAR_SPECIFIED:
478 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
479 "P2P: PIN from peer Display");
480 if (dev->wps_method != WPS_PIN_KEYPAD) {
481 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
482 "P2P: We have wps_method=%s -> "
484 p2p_wps_method_str(dev->wps_method));
485 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
489 case DEV_PW_USER_SPECIFIED:
490 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
491 "P2P: Peer entered PIN on Keypad");
492 if (dev->wps_method != WPS_PIN_LABEL &&
493 dev->wps_method != WPS_PIN_DISPLAY) {
494 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
495 "P2P: We have wps_method=%s -> "
497 p2p_wps_method_str(dev->wps_method));
498 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
502 case DEV_PW_PUSHBUTTON:
503 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
504 "P2P: Peer using pushbutton");
505 if (dev->wps_method != WPS_PBC) {
506 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
507 "P2P: We have wps_method=%s -> "
509 p2p_wps_method_str(dev->wps_method));
510 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
515 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
516 "P2P: Unsupported Device Password ID %d",
517 msg.dev_password_id);
518 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
523 struct p2p_channels intersection;
525 p2p_channels_intersect(&p2p->channels, &dev->channels,
527 if (intersection.reg_classes == 0 ||
528 intersection.reg_class[0].channels == 0) {
529 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
530 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
531 "P2P: No common channels found");
534 for (i = 0; i < intersection.reg_classes; i++) {
535 struct p2p_reg_class *c;
536 c = &intersection.reg_class[i];
537 wpa_printf(MSG_DEBUG, "P2P: reg_class %u",
539 wpa_hexdump(MSG_DEBUG, "P2P: channels",
540 c->channel, c->channels);
542 if (!p2p_channels_includes(&intersection,
545 struct p2p_reg_class *cl;
546 cl = &intersection.reg_class[0];
547 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
548 "P2P: Selected operating channel "
549 "(reg_class %u channel %u) not "
550 "acceptable to the peer - pick "
551 "another channel (reg_class %u "
553 p2p->op_reg_class, p2p->op_channel,
554 cl->reg_class, cl->channel[0]);
555 p2p->op_reg_class = cl->reg_class;
556 p2p->op_channel = cl->channel[0];
559 p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
562 dev->go_state = go ? LOCAL_GO : REMOTE_GO;
563 dev->oper_freq = p2p_channel_to_freq((const char *)
564 msg.operating_channel,
565 msg.operating_channel[3],
566 msg.operating_channel[4]);
567 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
568 "channel preference: %d MHz", dev->oper_freq);
570 if (msg.config_timeout) {
571 dev->go_timeout = msg.config_timeout[0];
572 dev->client_timeout = msg.config_timeout[1];
575 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
576 "P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
577 if (p2p->state != P2P_IDLE)
579 p2p_set_state(p2p, P2P_GO_NEG);
580 p2p_clear_timeout(p2p);
581 dev->dialog_token = msg.dialog_token;
582 os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
583 p2p->go_neg_peer = dev;
584 status = P2P_SC_SUCCESS;
589 dev->status = status;
590 resp = p2p_build_go_neg_resp(p2p, dev, msg.dialog_token, status,
592 p2p_parse_free(&msg);
595 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
596 "P2P: Sending GO Negotiation Response");
600 freq = p2p_channel_to_freq(p2p->cfg->country,
604 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
605 "P2P: Unknown regulatory class/channel");
609 if (status == P2P_SC_SUCCESS) {
610 p2p->pending_action_state = P2P_PENDING_GO_NEG_RESPONSE;
611 dev->flags |= P2P_DEV_WAIT_GO_NEG_CONFIRM;
613 p2p->pending_action_state =
614 P2P_PENDING_GO_NEG_RESPONSE_FAILURE;
615 if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, sa,
616 p2p->cfg->dev_addr, p2p->cfg->dev_addr,
617 wpabuf_head(resp), wpabuf_len(resp), 200) <
619 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
620 "P2P: Failed to send Action frame");
627 static struct wpabuf * p2p_build_go_neg_conf(struct p2p_data *p2p,
628 struct p2p_device *peer,
629 u8 dialog_token, u8 status,
630 const u8 *resp_chan, int go)
634 struct p2p_channels res;
637 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
638 "P2P: Building GO Negotiation Confirm");
639 buf = wpabuf_alloc(1000);
643 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_CONF, dialog_token);
645 len = p2p_buf_add_ie_hdr(buf);
646 p2p_buf_add_status(buf, status);
648 if (peer->go_state == LOCAL_GO) {
649 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP)
650 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
651 if (p2p->cross_connect)
652 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
654 p2p_buf_add_capability(buf, p2p->dev_capab, group_capab);
655 if (go || resp_chan == NULL)
656 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
660 p2p_buf_add_operating_channel(buf, (const char *) resp_chan,
661 resp_chan[3], resp_chan[4]);
662 p2p_channels_intersect(&p2p->channels, &peer->channels, &res);
663 p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
665 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
668 p2p_buf_update_ie_hdr(buf, len);
674 void p2p_process_go_neg_resp(struct p2p_data *p2p, const u8 *sa,
675 const u8 *data, size_t len, int rx_freq)
677 struct p2p_device *dev;
680 struct p2p_message msg;
681 u8 status = P2P_SC_SUCCESS;
684 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
685 "P2P: Received GO Negotiation Response from " MACSTR
686 " (freq=%d)", MAC2STR(sa), rx_freq);
687 dev = p2p_get_device(p2p, sa);
688 if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
689 dev != p2p->go_neg_peer) {
690 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
691 "P2P: Not ready for GO negotiation with " MACSTR,
696 if (p2p_parse(data, len, &msg))
699 if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE)) {
700 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
701 "P2P: Was not expecting GO Negotiation Response - "
703 p2p_parse_free(&msg);
706 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
708 if (msg.dialog_token != dev->dialog_token) {
709 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
710 "P2P: Unexpected Dialog Token %u (expected %u)",
711 msg.dialog_token, dev->dialog_token);
712 p2p_parse_free(&msg);
717 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
718 "P2P: No Status attribute received");
719 status = P2P_SC_FAIL_INVALID_PARAMS;
723 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
724 "P2P: GO Negotiation rejected: status %d",
726 dev->go_neg_req_sent = 0;
727 if (*msg.status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
728 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
729 "P2P: Wait for the peer to become ready for "
731 dev->flags |= P2P_DEV_NOT_YET_READY;
733 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
734 p2p_set_timeout(p2p, 0, 0);
736 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
737 "P2P: Stop GO Negotiation attempt");
738 p2p_go_neg_failed(p2p, dev, *msg.status);
740 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
741 p2p_parse_free(&msg);
745 if (!msg.capability) {
746 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
747 "P2P: Mandatory Capability attribute missing from GO "
748 "Negotiation Response");
749 #ifdef CONFIG_P2P_STRICT
750 status = P2P_SC_FAIL_INVALID_PARAMS;
752 #endif /* CONFIG_P2P_STRICT */
755 if (!msg.p2p_device_info) {
756 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
757 "P2P: Mandatory P2P Device Info attribute missing "
758 "from GO Negotiation Response");
759 #ifdef CONFIG_P2P_STRICT
760 status = P2P_SC_FAIL_INVALID_PARAMS;
762 #endif /* CONFIG_P2P_STRICT */
765 if (!msg.intended_addr) {
766 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
767 "P2P: No Intended P2P Interface Address attribute "
769 status = P2P_SC_FAIL_INVALID_PARAMS;
773 if (!msg.go_intent) {
774 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
775 "P2P: No GO Intent attribute received");
776 status = P2P_SC_FAIL_INVALID_PARAMS;
779 if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
780 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
781 "P2P: Invalid GO Intent value (%u) received",
782 *msg.go_intent >> 1);
783 status = P2P_SC_FAIL_INVALID_PARAMS;
787 go = p2p_go_det(p2p->go_intent, *msg.go_intent);
789 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
790 "P2P: Incompatible GO Intent");
791 status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
795 if (!go && msg.group_id) {
796 /* Store SSID for Provisioning step */
797 p2p->ssid_len = msg.group_id_len - ETH_ALEN;
798 os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
800 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
801 "P2P: Mandatory P2P Group ID attribute missing from "
802 "GO Negotiation Response");
804 #ifdef CONFIG_P2P_STRICT
805 status = P2P_SC_FAIL_INVALID_PARAMS;
807 #endif /* CONFIG_P2P_STRICT */
810 if (!msg.config_timeout) {
811 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
812 "P2P: Mandatory Configuration Timeout attribute "
813 "missing from GO Negotiation Response");
814 #ifdef CONFIG_P2P_STRICT
815 status = P2P_SC_FAIL_INVALID_PARAMS;
817 #endif /* CONFIG_P2P_STRICT */
819 dev->go_timeout = msg.config_timeout[0];
820 dev->client_timeout = msg.config_timeout[1];
823 if (!msg.operating_channel && !go) {
825 * Note: P2P Client may omit Operating Channel attribute to
826 * indicate it does not have a preference.
828 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
829 "P2P: No Operating Channel attribute received");
830 status = P2P_SC_FAIL_INVALID_PARAMS;
833 if (!msg.channel_list) {
834 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
835 "P2P: No Channel List attribute received");
836 status = P2P_SC_FAIL_INVALID_PARAMS;
840 if (p2p_peer_channels(p2p, dev, msg.channel_list,
841 msg.channel_list_len) < 0) {
842 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
843 "P2P: No common channels found");
844 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
848 if (msg.operating_channel) {
849 dev->oper_freq = p2p_channel_to_freq((const char *)
850 msg.operating_channel,
851 msg.operating_channel[3],
852 msg.operating_channel[4]);
853 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
854 "channel preference: %d MHz", dev->oper_freq);
858 switch (msg.dev_password_id) {
860 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
861 "P2P: PIN from peer Label");
862 if (dev->wps_method != WPS_PIN_KEYPAD) {
863 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
864 "P2P: We have wps_method=%s -> "
866 p2p_wps_method_str(dev->wps_method));
867 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
871 case DEV_PW_REGISTRAR_SPECIFIED:
872 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
873 "P2P: PIN from peer Display");
874 if (dev->wps_method != WPS_PIN_KEYPAD) {
875 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
876 "P2P: We have wps_method=%s -> "
878 p2p_wps_method_str(dev->wps_method));
879 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
883 case DEV_PW_USER_SPECIFIED:
884 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
885 "P2P: Peer entered PIN on Keypad");
886 if (dev->wps_method != WPS_PIN_LABEL &&
887 dev->wps_method != WPS_PIN_DISPLAY) {
888 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
889 "P2P: We have wps_method=%s -> "
891 p2p_wps_method_str(dev->wps_method));
892 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
896 case DEV_PW_PUSHBUTTON:
897 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
898 "P2P: Peer using pushbutton");
899 if (dev->wps_method != WPS_PBC) {
900 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
901 "P2P: We have wps_method=%s -> "
903 p2p_wps_method_str(dev->wps_method));
904 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
909 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
910 "P2P: Unsupported Device Password ID %d",
911 msg.dev_password_id);
912 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
917 struct p2p_channels intersection;
919 p2p_channels_intersect(&p2p->channels, &dev->channels,
921 if (intersection.reg_classes == 0 ||
922 intersection.reg_class[0].channels == 0) {
923 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
924 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
925 "P2P: No common channels found");
928 for (i = 0; i < intersection.reg_classes; i++) {
929 struct p2p_reg_class *c;
930 c = &intersection.reg_class[i];
931 wpa_printf(MSG_DEBUG, "P2P: reg_class %u",
933 wpa_hexdump(MSG_DEBUG, "P2P: channels",
934 c->channel, c->channels);
936 if (!p2p_channels_includes(&intersection, p2p->op_reg_class,
938 struct p2p_reg_class *cl;
939 cl = &intersection.reg_class[0];
940 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
941 "P2P: Selected operating channel "
942 "(reg_class %u channel %u) not "
943 "acceptable to the peer - pick "
944 "another channel (reg_class %u "
946 p2p->op_reg_class, p2p->op_channel,
947 cl->reg_class, cl->channel[0]);
948 p2p->op_reg_class = cl->reg_class;
949 p2p->op_channel = cl->channel[0];
952 p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
955 p2p_set_state(p2p, P2P_GO_NEG);
956 p2p_clear_timeout(p2p);
958 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
959 "P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
960 os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
963 conf = p2p_build_go_neg_conf(p2p, dev, msg.dialog_token, status,
964 msg.operating_channel, go);
965 p2p_parse_free(&msg);
968 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
969 "P2P: Sending GO Negotiation Confirm");
970 if (status == P2P_SC_SUCCESS) {
971 p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
972 dev->go_state = go ? LOCAL_GO : REMOTE_GO;
974 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
978 freq = dev->listen_freq;
979 if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, sa,
980 p2p->cfg->dev_addr, sa,
981 wpabuf_head(conf), wpabuf_len(conf), 200) <
983 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
984 "P2P: Failed to send Action frame");
985 p2p_go_neg_failed(p2p, dev, -1);
991 void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa,
992 const u8 *data, size_t len)
994 struct p2p_device *dev;
995 struct p2p_message msg;
997 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
998 "P2P: Received GO Negotiation Confirm from " MACSTR,
1000 dev = p2p_get_device(p2p, sa);
1001 if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
1002 dev != p2p->go_neg_peer) {
1003 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1004 "P2P: Not ready for GO negotiation with " MACSTR,
1009 if (p2p->pending_action_state == P2P_PENDING_GO_NEG_RESPONSE) {
1010 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopped waiting "
1011 "for TX status on GO Negotiation Response since we "
1012 "already received Confirmation");
1013 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1016 if (p2p_parse(data, len, &msg))
1019 if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
1020 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1021 "P2P: Was not expecting GO Negotiation Confirm - "
1025 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1027 if (msg.dialog_token != dev->dialog_token) {
1028 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1029 "P2P: Unexpected Dialog Token %u (expected %u)",
1030 msg.dialog_token, dev->dialog_token);
1031 p2p_parse_free(&msg);
1036 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1037 "P2P: No Status attribute received");
1038 p2p_parse_free(&msg);
1042 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1043 "P2P: GO Negotiation rejected: status %d",
1045 p2p_parse_free(&msg);
1049 if (dev->go_state == REMOTE_GO && msg.group_id) {
1050 /* Store SSID for Provisioning step */
1051 p2p->ssid_len = msg.group_id_len - ETH_ALEN;
1052 os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
1053 } else if (dev->go_state == REMOTE_GO) {
1054 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1055 "P2P: Mandatory P2P Group ID attribute missing from "
1056 "GO Negotiation Confirmation");
1058 #ifdef CONFIG_P2P_STRICT
1059 p2p_parse_free(&msg);
1061 #endif /* CONFIG_P2P_STRICT */
1064 if (!msg.operating_channel) {
1065 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1066 "P2P: Mandatory Operating Channel attribute missing "
1067 "from GO Negotiation Confirmation");
1068 #ifdef CONFIG_P2P_STRICT
1069 p2p_parse_free(&msg);
1071 #endif /* CONFIG_P2P_STRICT */
1074 if (!msg.channel_list) {
1075 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1076 "P2P: Mandatory Operating Channel attribute missing "
1077 "from GO Negotiation Confirmation");
1078 #ifdef CONFIG_P2P_STRICT
1079 p2p_parse_free(&msg);
1081 #endif /* CONFIG_P2P_STRICT */
1084 p2p_parse_free(&msg);
1086 if (dev->go_state == UNKNOWN_GO) {
1088 * This should not happen since GO negotiation has already
1091 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1092 "P2P: Unexpected GO Neg state - do not know which end "
1097 p2p_go_complete(p2p, dev);