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[3] != 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 p2p_buf_add_capability(buf, p2p->dev_capab, group_capab);
162 p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) |
163 p2p->next_tie_breaker);
164 p2p->next_tie_breaker = !p2p->next_tie_breaker;
165 p2p_buf_add_config_timeout(buf, 100, 20);
166 p2p_buf_add_listen_channel(buf, p2p->cfg->country, p2p->cfg->reg_class,
168 if (p2p->ext_listen_interval)
169 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
170 p2p->ext_listen_interval);
171 p2p_buf_add_intended_addr(buf, p2p->intended_addr);
172 p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
173 p2p_buf_add_device_info(buf, p2p, peer);
174 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
175 p2p->op_reg_class, p2p->op_channel);
176 p2p_buf_update_ie_hdr(buf, len);
178 /* WPS IE with Device Password ID attribute */
179 p2p_build_wps_ie(p2p, buf, p2p_wps_method_pw_id(peer->wps_method), 0);
185 int p2p_connect_send(struct p2p_data *p2p, struct p2p_device *dev)
190 freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
192 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
193 "P2P: No Listen/Operating frequency known for the "
194 "peer " MACSTR " to send GO Negotiation Request",
195 MAC2STR(dev->p2p_device_addr));
199 req = p2p_build_go_neg_req(p2p, dev);
202 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
203 "P2P: Sending GO Negotiation Request");
204 p2p_set_state(p2p, P2P_CONNECT);
205 p2p->pending_action_state = P2P_PENDING_GO_NEG_REQUEST;
206 p2p->go_neg_peer = dev;
207 dev->flags |= P2P_DEV_WAIT_GO_NEG_RESPONSE;
208 if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq,
209 dev->p2p_device_addr, p2p->cfg->dev_addr,
210 dev->p2p_device_addr,
211 wpabuf_head(req), wpabuf_len(req), 200) < 0)
213 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
214 "P2P: Failed to send Action frame");
215 /* Use P2P find to recover and retry */
216 p2p_set_timeout(p2p, 0, 0);
225 static struct wpabuf * p2p_build_go_neg_resp(struct p2p_data *p2p,
226 struct p2p_device *peer,
227 u8 dialog_token, u8 status,
234 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
235 "P2P: Building GO Negotiation Response");
236 buf = wpabuf_alloc(1000);
240 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_RESP, dialog_token);
242 len = p2p_buf_add_ie_hdr(buf);
243 p2p_buf_add_status(buf, status);
245 if (peer && (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP))
246 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
247 p2p_buf_add_capability(buf, p2p->dev_capab, group_capab);
248 p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | tie_breaker);
249 p2p_buf_add_config_timeout(buf, 100, 20);
250 if (peer && peer->go_state == REMOTE_GO) {
251 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Omit Operating "
252 "Channel attribute");
254 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
258 p2p_buf_add_intended_addr(buf, p2p->intended_addr);
259 if (status || peer == NULL) {
260 p2p_buf_add_channel_list(buf, p2p->cfg->country,
262 } else if (peer->go_state == REMOTE_GO) {
263 p2p_buf_add_channel_list(buf, p2p->cfg->country,
266 struct p2p_channels res;
267 p2p_channels_intersect(&p2p->channels, &peer->channels,
269 p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
271 p2p_buf_add_device_info(buf, p2p, peer);
272 if (peer && peer->go_state == LOCAL_GO) {
273 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
276 p2p_buf_update_ie_hdr(buf, len);
278 /* WPS IE with Device Password ID attribute */
279 p2p_build_wps_ie(p2p, buf,
280 p2p_wps_method_pw_id(peer ? peer->wps_method :
287 void p2p_process_go_neg_req(struct p2p_data *p2p, const u8 *sa,
288 const u8 *data, size_t len, int rx_freq)
290 struct p2p_device *dev = NULL;
292 struct p2p_message msg;
293 u8 status = P2P_SC_FAIL_INVALID_PARAMS;
297 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
298 "P2P: Received GO Negotiation Request from " MACSTR
299 "(freq=%d)", MAC2STR(sa), rx_freq);
301 if (p2p_parse(data, len, &msg))
304 if (!msg.capability) {
305 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
306 "P2P: Mandatory Capability attribute missing from GO "
307 "Negotiation Request");
308 #ifdef CONFIG_P2P_STRICT
310 #endif /* CONFIG_P2P_STRICT */
314 tie_breaker = *msg.go_intent & 0x01;
316 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
317 "P2P: Mandatory GO Intent attribute missing from GO "
318 "Negotiation Request");
319 #ifdef CONFIG_P2P_STRICT
321 #endif /* CONFIG_P2P_STRICT */
324 if (!msg.config_timeout) {
325 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
326 "P2P: Mandatory Configuration Timeout attribute "
327 "missing from GO Negotiation Request");
328 #ifdef CONFIG_P2P_STRICT
330 #endif /* CONFIG_P2P_STRICT */
333 if (!msg.listen_channel) {
334 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
335 "P2P: No Listen Channel attribute received");
338 if (!msg.operating_channel) {
339 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
340 "P2P: No Operating Channel attribute received");
343 if (!msg.channel_list) {
344 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
345 "P2P: No Channel List attribute received");
348 if (!msg.intended_addr) {
349 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
350 "P2P: No Intended P2P Interface Address attribute "
354 if (!msg.p2p_device_info) {
355 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
356 "P2P: No P2P Device Info attribute received");
360 if (os_memcmp(msg.p2p_device_addr, sa, ETH_ALEN) != 0) {
361 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
362 "P2P: Unexpected GO Negotiation Request SA=" MACSTR
363 " != dev_addr=" MACSTR,
364 MAC2STR(sa), MAC2STR(msg.p2p_device_addr));
368 dev = p2p_get_device(p2p, sa);
370 if (msg.status && *msg.status) {
371 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
372 "P2P: Unexpected Status attribute (%d) in GO "
373 "Negotiation Request", *msg.status);
378 dev = p2p_add_dev_from_go_neg_req(p2p, sa, &msg);
379 else if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
380 p2p_add_dev_info(p2p, sa, dev, &msg);
381 if (dev && dev->flags & P2P_DEV_USER_REJECTED) {
382 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
383 "P2P: User has rejected this peer");
384 status = P2P_SC_FAIL_REJECTED_BY_USER;
385 } else if (dev == NULL || dev->wps_method == WPS_NOT_READY) {
386 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
387 "P2P: Not ready for GO negotiation with " MACSTR,
389 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
391 dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
392 p2p->cfg->go_neg_req_rx(p2p->cfg->cb_ctx, sa);
393 } else if (p2p->go_neg_peer && p2p->go_neg_peer != dev) {
394 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
395 "P2P: Already in Group Formation with another peer");
396 status = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
400 if (!p2p->go_neg_peer) {
401 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting "
402 "GO Negotiation with previously authorized "
404 /* TODO: check if force_freq is needed */
405 p2p->op_reg_class = p2p->cfg->op_reg_class;
406 p2p->op_channel = p2p->cfg->op_channel;
407 os_memcpy(&p2p->channels, &p2p->cfg->channels,
408 sizeof(struct p2p_channels));
411 dev->flags &= ~P2P_DEV_NOT_YET_READY;
413 if (!msg.go_intent) {
414 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
415 "P2P: No GO Intent attribute received");
418 if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
419 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
420 "P2P: Invalid GO Intent value (%u) received",
421 *msg.go_intent >> 1);
425 if (dev->go_neg_req_sent &&
426 os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) > 0) {
427 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
428 "P2P: Do not reply since peer has higher "
429 "address and GO Neg Request already sent");
430 p2p_parse_free(&msg);
434 go = p2p_go_det(p2p->go_intent, *msg.go_intent);
436 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
437 "P2P: Incompatible GO Intent");
438 status = P2P_SC_FAIL_BOTH_GO_INTENT_15;
442 if (p2p_peer_channels(p2p, dev, msg.channel_list,
443 msg.channel_list_len) < 0) {
444 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
445 "P2P: No common channels found");
446 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
450 switch (msg.dev_password_id) {
452 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
453 "P2P: PIN from peer Label");
454 if (dev->wps_method != WPS_PIN_KEYPAD) {
455 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
456 "P2P: We have wps_method=%s -> "
458 p2p_wps_method_str(dev->wps_method));
459 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
463 case DEV_PW_REGISTRAR_SPECIFIED:
464 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
465 "P2P: PIN from peer Display");
466 if (dev->wps_method != WPS_PIN_KEYPAD) {
467 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
468 "P2P: We have wps_method=%s -> "
470 p2p_wps_method_str(dev->wps_method));
471 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
475 case DEV_PW_USER_SPECIFIED:
476 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
477 "P2P: Peer entered PIN on Keypad");
478 if (dev->wps_method != WPS_PIN_LABEL &&
479 dev->wps_method != WPS_PIN_DISPLAY) {
480 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
481 "P2P: We have wps_method=%s -> "
483 p2p_wps_method_str(dev->wps_method));
484 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
488 case DEV_PW_PUSHBUTTON:
489 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
490 "P2P: Peer using pushbutton");
491 if (dev->wps_method != WPS_PBC) {
492 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
493 "P2P: We have wps_method=%s -> "
495 p2p_wps_method_str(dev->wps_method));
496 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
501 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
502 "P2P: Unsupported Device Password ID %d",
503 msg.dev_password_id);
504 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
509 struct p2p_channels intersection;
511 p2p_channels_intersect(&p2p->channels, &dev->channels,
513 if (intersection.reg_classes == 0 ||
514 intersection.reg_class[0].channels == 0) {
515 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
516 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
517 "P2P: No common channels found");
520 for (i = 0; i < intersection.reg_classes; i++) {
521 struct p2p_reg_class *c;
522 c = &intersection.reg_class[i];
523 wpa_printf(MSG_DEBUG, "P2P: reg_class %u",
525 wpa_hexdump(MSG_DEBUG, "P2P: channels",
526 c->channel, c->channels);
528 if (!p2p_channels_includes(&intersection,
531 struct p2p_reg_class *cl;
532 cl = &intersection.reg_class[0];
533 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
534 "P2P: Selected operating channel "
535 "(reg_class %u channel %u) not "
536 "acceptable to the peer - pick "
537 "another channel (reg_class %u "
539 p2p->op_reg_class, p2p->op_channel,
540 cl->reg_class, cl->channel[0]);
541 p2p->op_reg_class = cl->reg_class;
542 p2p->op_channel = cl->channel[0];
545 p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
548 dev->go_state = go ? LOCAL_GO : REMOTE_GO;
549 dev->oper_freq = p2p_channel_to_freq((const char *)
550 msg.operating_channel,
551 msg.operating_channel[3],
552 msg.operating_channel[4]);
553 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
554 "channel preference: %d MHz", dev->oper_freq);
556 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
557 "P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
558 if (p2p->state != P2P_IDLE)
560 p2p_set_state(p2p, P2P_GO_NEG);
561 p2p_clear_timeout(p2p);
562 dev->dialog_token = msg.dialog_token;
563 os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
564 p2p->go_neg_peer = dev;
565 status = P2P_SC_SUCCESS;
569 resp = p2p_build_go_neg_resp(p2p, dev, msg.dialog_token, status,
571 p2p_parse_free(&msg);
574 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
575 "P2P: Sending GO Negotiation Response");
579 freq = p2p_channel_to_freq(p2p->cfg->country,
583 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
584 "P2P: Unknown regulatory class/channel");
588 if (status == P2P_SC_SUCCESS) {
589 p2p->pending_action_state = P2P_PENDING_GO_NEG_RESPONSE;
590 dev->flags |= P2P_DEV_WAIT_GO_NEG_CONFIRM;
592 p2p->pending_action_state =
593 P2P_PENDING_GO_NEG_RESPONSE_FAILURE;
594 if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, sa,
595 p2p->cfg->dev_addr, p2p->cfg->dev_addr,
596 wpabuf_head(resp), wpabuf_len(resp), 200) <
598 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
599 "P2P: Failed to send Action frame");
606 static struct wpabuf * p2p_build_go_neg_conf(struct p2p_data *p2p,
607 struct p2p_device *peer,
608 u8 dialog_token, u8 status,
609 const u8 *resp_chan, int go)
613 struct p2p_channels res;
616 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
617 "P2P: Building GO Negotiation Confirm");
618 buf = wpabuf_alloc(1000);
622 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_CONF, dialog_token);
624 len = p2p_buf_add_ie_hdr(buf);
625 p2p_buf_add_status(buf, status);
627 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP)
628 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
629 p2p_buf_add_capability(buf, p2p->dev_capab, group_capab);
630 if (go || resp_chan == NULL)
631 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
635 p2p_buf_add_operating_channel(buf, (const char *) resp_chan,
636 resp_chan[3], resp_chan[4]);
637 p2p_channels_intersect(&p2p->channels, &peer->channels, &res);
638 p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
640 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
643 p2p_buf_update_ie_hdr(buf, len);
649 void p2p_process_go_neg_resp(struct p2p_data *p2p, const u8 *sa,
650 const u8 *data, size_t len, int rx_freq)
652 struct p2p_device *dev;
655 struct p2p_message msg;
656 u8 status = P2P_SC_SUCCESS;
659 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
660 "P2P: Received GO Negotiation Response from " MACSTR
661 " (freq=%d)", MAC2STR(sa), rx_freq);
662 dev = p2p_get_device(p2p, sa);
663 if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
664 dev != p2p->go_neg_peer) {
665 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
666 "P2P: Not ready for GO negotiation with " MACSTR,
671 if (p2p_parse(data, len, &msg))
674 if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE)) {
675 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
676 "P2P: Was not expecting GO Negotiation Response - "
678 p2p_parse_free(&msg);
681 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
683 if (msg.dialog_token != dev->dialog_token) {
684 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
685 "P2P: Unexpected Dialog Token %u (expected %u)",
686 msg.dialog_token, dev->dialog_token);
687 p2p_parse_free(&msg);
692 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
693 "P2P: No Status attribute received");
694 status = P2P_SC_FAIL_INVALID_PARAMS;
698 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
699 "P2P: GO Negotiation rejected: status %d",
701 dev->go_neg_req_sent = 0;
702 if (*msg.status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
703 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
704 "P2P: Wait for the peer to become ready for "
706 dev->flags |= P2P_DEV_NOT_YET_READY;
708 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
709 p2p_set_timeout(p2p, 0, 0);
711 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
712 "P2P: Stop GO Negotiation attempt");
713 p2p_go_neg_failed(p2p, dev, *msg.status);
715 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
716 p2p_parse_free(&msg);
720 if (!msg.capability) {
721 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
722 "P2P: Mandatory Capability attribute missing from GO "
723 "Negotiation Response");
724 #ifdef CONFIG_P2P_STRICT
725 status = P2P_SC_FAIL_INVALID_PARAMS;
727 #endif /* CONFIG_P2P_STRICT */
730 if (!msg.p2p_device_info) {
731 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
732 "P2P: Mandatory P2P Device Info attribute missing "
733 "from GO Negotiation Response");
734 #ifdef CONFIG_P2P_STRICT
735 status = P2P_SC_FAIL_INVALID_PARAMS;
737 #endif /* CONFIG_P2P_STRICT */
740 if (!msg.intended_addr) {
741 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
742 "P2P: No Intended P2P Interface Address attribute "
744 status = P2P_SC_FAIL_INVALID_PARAMS;
748 if (!msg.go_intent) {
749 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
750 "P2P: No GO Intent attribute received");
751 status = P2P_SC_FAIL_INVALID_PARAMS;
754 if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
755 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
756 "P2P: Invalid GO Intent value (%u) received",
757 *msg.go_intent >> 1);
758 status = P2P_SC_FAIL_INVALID_PARAMS;
762 go = p2p_go_det(p2p->go_intent, *msg.go_intent);
764 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
765 "P2P: Incompatible GO Intent");
766 status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
770 if (!go && msg.group_id) {
771 /* TODO: Store SSID for Provisioning step */
773 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
774 "P2P: Mandatory P2P Group ID attribute missing from "
775 "GO Negotiation Response");
776 #ifdef CONFIG_P2P_STRICT
777 status = P2P_SC_FAIL_INVALID_PARAMS;
779 #endif /* CONFIG_P2P_STRICT */
782 if (!msg.config_timeout) {
783 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
784 "P2P: Mandatory Configuration Timeout attribute "
785 "missing from GO Negotiation Response");
786 #ifdef CONFIG_P2P_STRICT
787 status = P2P_SC_FAIL_INVALID_PARAMS;
789 #endif /* CONFIG_P2P_STRICT */
792 if (!msg.operating_channel && !go) {
794 * Note: P2P Client may omit Operating Channel attribute to
795 * indicate it does not have a preference.
797 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
798 "P2P: No Operating Channel attribute received");
799 status = P2P_SC_FAIL_INVALID_PARAMS;
802 if (!msg.channel_list) {
803 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
804 "P2P: No Channel List attribute received");
805 status = P2P_SC_FAIL_INVALID_PARAMS;
809 if (p2p_peer_channels(p2p, dev, msg.channel_list,
810 msg.channel_list_len) < 0) {
811 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
812 "P2P: No common channels found");
813 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
817 if (msg.operating_channel) {
818 dev->oper_freq = p2p_channel_to_freq((const char *)
819 msg.operating_channel,
820 msg.operating_channel[3],
821 msg.operating_channel[4]);
822 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
823 "channel preference: %d MHz", dev->oper_freq);
827 switch (msg.dev_password_id) {
829 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
830 "P2P: PIN from peer Label");
831 if (dev->wps_method != WPS_PIN_KEYPAD) {
832 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
833 "P2P: We have wps_method=%s -> "
835 p2p_wps_method_str(dev->wps_method));
836 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
840 case DEV_PW_REGISTRAR_SPECIFIED:
841 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
842 "P2P: PIN from peer Display");
843 if (dev->wps_method != WPS_PIN_KEYPAD) {
844 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
845 "P2P: We have wps_method=%s -> "
847 p2p_wps_method_str(dev->wps_method));
848 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
852 case DEV_PW_USER_SPECIFIED:
853 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
854 "P2P: Peer entered PIN on Keypad");
855 if (dev->wps_method != WPS_PIN_LABEL &&
856 dev->wps_method != WPS_PIN_DISPLAY) {
857 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
858 "P2P: We have wps_method=%s -> "
860 p2p_wps_method_str(dev->wps_method));
861 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
865 case DEV_PW_PUSHBUTTON:
866 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
867 "P2P: Peer using pushbutton");
868 if (dev->wps_method != WPS_PBC) {
869 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
870 "P2P: We have wps_method=%s -> "
872 p2p_wps_method_str(dev->wps_method));
873 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
878 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
879 "P2P: Unsupported Device Password ID %d",
880 msg.dev_password_id);
881 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
886 struct p2p_channels intersection;
888 p2p_channels_intersect(&p2p->channels, &dev->channels,
890 if (intersection.reg_classes == 0 ||
891 intersection.reg_class[0].channels == 0) {
892 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
893 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
894 "P2P: No common channels found");
897 for (i = 0; i < intersection.reg_classes; i++) {
898 struct p2p_reg_class *c;
899 c = &intersection.reg_class[i];
900 wpa_printf(MSG_DEBUG, "P2P: reg_class %u",
902 wpa_hexdump(MSG_DEBUG, "P2P: channels",
903 c->channel, c->channels);
905 if (!p2p_channels_includes(&intersection, p2p->op_reg_class,
907 struct p2p_reg_class *cl;
908 cl = &intersection.reg_class[0];
909 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
910 "P2P: Selected operating channel "
911 "(reg_class %u channel %u) not "
912 "acceptable to the peer - pick "
913 "another channel (reg_class %u "
915 p2p->op_reg_class, p2p->op_channel,
916 cl->reg_class, cl->channel[0]);
917 p2p->op_reg_class = cl->reg_class;
918 p2p->op_channel = cl->channel[0];
921 p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
924 p2p_set_state(p2p, P2P_GO_NEG);
925 p2p_clear_timeout(p2p);
927 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
928 "P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
929 os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
932 conf = p2p_build_go_neg_conf(p2p, dev, msg.dialog_token, status,
933 msg.operating_channel, go);
934 p2p_parse_free(&msg);
937 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
938 "P2P: Sending GO Negotiation Confirm");
939 if (status == P2P_SC_SUCCESS) {
940 p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
941 dev->go_state = go ? LOCAL_GO : REMOTE_GO;
943 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
947 freq = dev->listen_freq;
948 if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, sa,
949 p2p->cfg->dev_addr, sa,
950 wpabuf_head(conf), wpabuf_len(conf), 200) <
952 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
953 "P2P: Failed to send Action frame");
954 p2p_go_neg_failed(p2p, dev, -1);
960 void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa,
961 const u8 *data, size_t len)
963 struct p2p_device *dev;
964 struct p2p_message msg;
966 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
967 "P2P: Received GO Negotiation Confirm from " MACSTR,
969 dev = p2p_get_device(p2p, sa);
970 if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
971 dev != p2p->go_neg_peer) {
972 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
973 "P2P: Not ready for GO negotiation with " MACSTR,
978 if (p2p->pending_action_state == P2P_PENDING_GO_NEG_RESPONSE) {
979 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopped waiting "
980 "for TX status on GO Negotiation Response since we "
981 "already received Confirmation");
982 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
985 if (p2p_parse(data, len, &msg))
988 if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
989 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
990 "P2P: Was not expecting GO Negotiation Confirm - "
994 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
996 if (msg.dialog_token != dev->dialog_token) {
997 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
998 "P2P: Unexpected Dialog Token %u (expected %u)",
999 msg.dialog_token, dev->dialog_token);
1000 p2p_parse_free(&msg);
1005 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1006 "P2P: No Status attribute received");
1007 p2p_parse_free(&msg);
1011 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1012 "P2P: GO Negotiation rejected: status %d",
1014 p2p_parse_free(&msg);
1018 if (dev->go_state == REMOTE_GO && msg.group_id) {
1019 /* TODO: Store SSID for Provisioning step */
1020 } else if (dev->go_state == REMOTE_GO) {
1021 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1022 "P2P: Mandatory P2P Group ID attribute missing from "
1023 "GO Negotiation Confirmation");
1024 #ifdef CONFIG_P2P_STRICT
1025 p2p_parse_free(&msg);
1027 #endif /* CONFIG_P2P_STRICT */
1030 if (!msg.operating_channel) {
1031 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1032 "P2P: Mandatory Operating Channel attribute missing "
1033 "from GO Negotiation Confirmation");
1034 #ifdef CONFIG_P2P_STRICT
1035 p2p_parse_free(&msg);
1037 #endif /* CONFIG_P2P_STRICT */
1040 if (!msg.channel_list) {
1041 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1042 "P2P: Mandatory Operating Channel attribute missing "
1043 "from GO Negotiation Confirmation");
1044 #ifdef CONFIG_P2P_STRICT
1045 p2p_parse_free(&msg);
1047 #endif /* CONFIG_P2P_STRICT */
1050 p2p_parse_free(&msg);
1052 if (dev->go_state == UNKNOWN_GO) {
1054 * This should not happen since GO negotiation has already
1057 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1058 "P2P: Unexpected GO Neg state - do not know which end "
1063 p2p_go_complete(p2p, dev);