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 if (p2p->cfg->p2p_intra_bss)
164 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
165 p2p_buf_add_capability(buf, p2p->dev_capab, group_capab);
166 p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) |
167 p2p->next_tie_breaker);
168 p2p->next_tie_breaker = !p2p->next_tie_breaker;
169 p2p_buf_add_config_timeout(buf, 100, 20);
170 p2p_buf_add_listen_channel(buf, p2p->cfg->country, p2p->cfg->reg_class,
172 if (p2p->ext_listen_interval)
173 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
174 p2p->ext_listen_interval);
175 p2p_buf_add_intended_addr(buf, p2p->intended_addr);
176 p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
177 p2p_buf_add_device_info(buf, p2p, peer);
178 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
179 p2p->op_reg_class, p2p->op_channel);
180 p2p_buf_update_ie_hdr(buf, len);
182 /* WPS IE with Device Password ID attribute */
183 p2p_build_wps_ie(p2p, buf, p2p_wps_method_pw_id(peer->wps_method), 0);
189 int p2p_connect_send(struct p2p_data *p2p, struct p2p_device *dev)
194 freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
196 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
197 "P2P: No Listen/Operating frequency known for the "
198 "peer " MACSTR " to send GO Negotiation Request",
199 MAC2STR(dev->p2p_device_addr));
203 req = p2p_build_go_neg_req(p2p, dev);
206 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
207 "P2P: Sending GO Negotiation Request");
208 p2p_set_state(p2p, P2P_CONNECT);
209 p2p->pending_action_state = P2P_PENDING_GO_NEG_REQUEST;
210 p2p->go_neg_peer = dev;
211 dev->flags |= P2P_DEV_WAIT_GO_NEG_RESPONSE;
212 if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq,
213 dev->p2p_device_addr, p2p->cfg->dev_addr,
214 dev->p2p_device_addr,
215 wpabuf_head(req), wpabuf_len(req), 200) < 0)
217 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
218 "P2P: Failed to send Action frame");
219 /* Use P2P find to recover and retry */
220 p2p_set_timeout(p2p, 0, 0);
229 static struct wpabuf * p2p_build_go_neg_resp(struct p2p_data *p2p,
230 struct p2p_device *peer,
231 u8 dialog_token, u8 status,
238 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
239 "P2P: Building GO Negotiation Response");
240 buf = wpabuf_alloc(1000);
244 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_RESP, dialog_token);
246 len = p2p_buf_add_ie_hdr(buf);
247 p2p_buf_add_status(buf, status);
249 if (peer && peer->go_state == LOCAL_GO) {
250 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP)
251 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
252 if (p2p->cross_connect)
253 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
254 if (p2p->cfg->p2p_intra_bss)
255 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
257 p2p_buf_add_capability(buf, p2p->dev_capab, group_capab);
258 p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | tie_breaker);
259 p2p_buf_add_config_timeout(buf, 100, 20);
260 if (peer && peer->go_state == REMOTE_GO) {
261 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Omit Operating "
262 "Channel attribute");
264 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
268 p2p_buf_add_intended_addr(buf, p2p->intended_addr);
269 if (status || peer == NULL) {
270 p2p_buf_add_channel_list(buf, p2p->cfg->country,
272 } else if (peer->go_state == REMOTE_GO) {
273 p2p_buf_add_channel_list(buf, p2p->cfg->country,
276 struct p2p_channels res;
277 p2p_channels_intersect(&p2p->channels, &peer->channels,
279 p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
281 p2p_buf_add_device_info(buf, p2p, peer);
282 if (peer && peer->go_state == LOCAL_GO) {
283 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
286 p2p_buf_update_ie_hdr(buf, len);
288 /* WPS IE with Device Password ID attribute */
289 p2p_build_wps_ie(p2p, buf,
290 p2p_wps_method_pw_id(peer ? peer->wps_method :
297 void p2p_process_go_neg_req(struct p2p_data *p2p, const u8 *sa,
298 const u8 *data, size_t len, int rx_freq)
300 struct p2p_device *dev = NULL;
302 struct p2p_message msg;
303 u8 status = P2P_SC_FAIL_INVALID_PARAMS;
307 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
308 "P2P: Received GO Negotiation Request from " MACSTR
309 "(freq=%d)", MAC2STR(sa), rx_freq);
311 if (p2p_parse(data, len, &msg))
314 if (!msg.capability) {
315 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
316 "P2P: Mandatory Capability attribute missing from GO "
317 "Negotiation Request");
318 #ifdef CONFIG_P2P_STRICT
320 #endif /* CONFIG_P2P_STRICT */
324 tie_breaker = *msg.go_intent & 0x01;
326 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
327 "P2P: Mandatory GO Intent attribute missing from GO "
328 "Negotiation Request");
329 #ifdef CONFIG_P2P_STRICT
331 #endif /* CONFIG_P2P_STRICT */
334 if (!msg.config_timeout) {
335 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
336 "P2P: Mandatory Configuration Timeout attribute "
337 "missing from GO Negotiation Request");
338 #ifdef CONFIG_P2P_STRICT
340 #endif /* CONFIG_P2P_STRICT */
343 if (!msg.listen_channel) {
344 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
345 "P2P: No Listen Channel attribute received");
348 if (!msg.operating_channel) {
349 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
350 "P2P: No Operating Channel attribute received");
353 if (!msg.channel_list) {
354 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
355 "P2P: No Channel List attribute received");
358 if (!msg.intended_addr) {
359 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
360 "P2P: No Intended P2P Interface Address attribute "
364 if (!msg.p2p_device_info) {
365 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
366 "P2P: No P2P Device Info attribute received");
370 if (os_memcmp(msg.p2p_device_addr, sa, ETH_ALEN) != 0) {
371 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
372 "P2P: Unexpected GO Negotiation Request SA=" MACSTR
373 " != dev_addr=" MACSTR,
374 MAC2STR(sa), MAC2STR(msg.p2p_device_addr));
378 dev = p2p_get_device(p2p, sa);
380 if (msg.status && *msg.status) {
381 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
382 "P2P: Unexpected Status attribute (%d) in GO "
383 "Negotiation Request", *msg.status);
388 dev = p2p_add_dev_from_go_neg_req(p2p, sa, &msg);
389 else if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
390 p2p_add_dev_info(p2p, sa, dev, &msg);
391 if (dev && dev->flags & P2P_DEV_USER_REJECTED) {
392 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
393 "P2P: User has rejected this peer");
394 status = P2P_SC_FAIL_REJECTED_BY_USER;
395 } else if (dev == NULL || dev->wps_method == WPS_NOT_READY) {
396 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
397 "P2P: Not ready for GO negotiation with " MACSTR,
399 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
401 dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
402 p2p->cfg->go_neg_req_rx(p2p->cfg->cb_ctx, sa,
403 msg.dev_password_id);
404 } else if (p2p->go_neg_peer && p2p->go_neg_peer != dev) {
405 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
406 "P2P: Already in Group Formation with another peer");
407 status = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
411 if (!p2p->go_neg_peer) {
412 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting "
413 "GO Negotiation with previously authorized "
415 if (!(dev->flags & P2P_DEV_FORCE_FREQ)) {
416 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
417 "P2P: Use default channel settings");
418 p2p->op_reg_class = p2p->cfg->op_reg_class;
419 p2p->op_channel = p2p->cfg->op_channel;
420 os_memcpy(&p2p->channels, &p2p->cfg->channels,
421 sizeof(struct p2p_channels));
423 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
424 "P2P: Use previously configured "
425 "forced channel settings");
429 dev->flags &= ~P2P_DEV_NOT_YET_READY;
431 if (!msg.go_intent) {
432 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
433 "P2P: No GO Intent attribute received");
436 if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
437 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
438 "P2P: Invalid GO Intent value (%u) received",
439 *msg.go_intent >> 1);
443 if (dev->go_neg_req_sent &&
444 os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) > 0) {
445 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
446 "P2P: Do not reply since peer has higher "
447 "address and GO Neg Request already sent");
448 p2p_parse_free(&msg);
452 go = p2p_go_det(p2p->go_intent, *msg.go_intent);
454 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
455 "P2P: Incompatible GO Intent");
456 status = P2P_SC_FAIL_BOTH_GO_INTENT_15;
460 if (p2p_peer_channels(p2p, dev, msg.channel_list,
461 msg.channel_list_len) < 0) {
462 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
463 "P2P: No common channels found");
464 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
468 switch (msg.dev_password_id) {
470 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
471 "P2P: PIN from peer Label");
472 if (dev->wps_method != WPS_PIN_KEYPAD) {
473 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
474 "P2P: We have wps_method=%s -> "
476 p2p_wps_method_str(dev->wps_method));
477 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
481 case DEV_PW_REGISTRAR_SPECIFIED:
482 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
483 "P2P: PIN from peer Display");
484 if (dev->wps_method != WPS_PIN_KEYPAD) {
485 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
486 "P2P: We have wps_method=%s -> "
488 p2p_wps_method_str(dev->wps_method));
489 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
493 case DEV_PW_USER_SPECIFIED:
494 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
495 "P2P: Peer entered PIN on Keypad");
496 if (dev->wps_method != WPS_PIN_LABEL &&
497 dev->wps_method != WPS_PIN_DISPLAY) {
498 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
499 "P2P: We have wps_method=%s -> "
501 p2p_wps_method_str(dev->wps_method));
502 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
506 case DEV_PW_PUSHBUTTON:
507 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
508 "P2P: Peer using pushbutton");
509 if (dev->wps_method != WPS_PBC) {
510 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
511 "P2P: We have wps_method=%s -> "
513 p2p_wps_method_str(dev->wps_method));
514 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
519 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
520 "P2P: Unsupported Device Password ID %d",
521 msg.dev_password_id);
522 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
527 struct p2p_channels intersection;
529 p2p_channels_intersect(&p2p->channels, &dev->channels,
531 if (intersection.reg_classes == 0 ||
532 intersection.reg_class[0].channels == 0) {
533 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
534 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
535 "P2P: No common channels found");
538 for (i = 0; i < intersection.reg_classes; i++) {
539 struct p2p_reg_class *c;
540 c = &intersection.reg_class[i];
541 wpa_printf(MSG_DEBUG, "P2P: reg_class %u",
543 wpa_hexdump(MSG_DEBUG, "P2P: channels",
544 c->channel, c->channels);
546 if (!p2p_channels_includes(&intersection,
549 struct p2p_reg_class *cl;
550 cl = &intersection.reg_class[0];
551 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
552 "P2P: Selected operating channel "
553 "(reg_class %u channel %u) not "
554 "acceptable to the peer - pick "
555 "another channel (reg_class %u "
557 p2p->op_reg_class, p2p->op_channel,
558 cl->reg_class, cl->channel[0]);
559 p2p->op_reg_class = cl->reg_class;
560 p2p->op_channel = cl->channel[0];
563 p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
566 dev->go_state = go ? LOCAL_GO : REMOTE_GO;
567 dev->oper_freq = p2p_channel_to_freq((const char *)
568 msg.operating_channel,
569 msg.operating_channel[3],
570 msg.operating_channel[4]);
571 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
572 "channel preference: %d MHz", dev->oper_freq);
574 if (msg.config_timeout) {
575 dev->go_timeout = msg.config_timeout[0];
576 dev->client_timeout = msg.config_timeout[1];
579 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
580 "P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
581 if (p2p->state != P2P_IDLE)
583 p2p_set_state(p2p, P2P_GO_NEG);
584 p2p_clear_timeout(p2p);
585 dev->dialog_token = msg.dialog_token;
586 os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
587 p2p->go_neg_peer = dev;
588 status = P2P_SC_SUCCESS;
593 dev->status = status;
594 resp = p2p_build_go_neg_resp(p2p, dev, msg.dialog_token, status,
596 p2p_parse_free(&msg);
599 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
600 "P2P: Sending GO Negotiation Response");
604 freq = p2p_channel_to_freq(p2p->cfg->country,
608 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
609 "P2P: Unknown regulatory class/channel");
613 if (status == P2P_SC_SUCCESS) {
614 p2p->pending_action_state = P2P_PENDING_GO_NEG_RESPONSE;
615 dev->flags |= P2P_DEV_WAIT_GO_NEG_CONFIRM;
617 p2p->pending_action_state =
618 P2P_PENDING_GO_NEG_RESPONSE_FAILURE;
619 if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, sa,
620 p2p->cfg->dev_addr, p2p->cfg->dev_addr,
621 wpabuf_head(resp), wpabuf_len(resp), 200) <
623 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
624 "P2P: Failed to send Action frame");
631 static struct wpabuf * p2p_build_go_neg_conf(struct p2p_data *p2p,
632 struct p2p_device *peer,
633 u8 dialog_token, u8 status,
634 const u8 *resp_chan, int go)
638 struct p2p_channels res;
641 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
642 "P2P: Building GO Negotiation Confirm");
643 buf = wpabuf_alloc(1000);
647 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_CONF, dialog_token);
649 len = p2p_buf_add_ie_hdr(buf);
650 p2p_buf_add_status(buf, status);
652 if (peer->go_state == LOCAL_GO) {
653 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP)
654 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
655 if (p2p->cross_connect)
656 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
657 if (p2p->cfg->p2p_intra_bss)
658 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
660 p2p_buf_add_capability(buf, p2p->dev_capab, group_capab);
661 if (go || resp_chan == NULL)
662 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
666 p2p_buf_add_operating_channel(buf, (const char *) resp_chan,
667 resp_chan[3], resp_chan[4]);
668 p2p_channels_intersect(&p2p->channels, &peer->channels, &res);
669 p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
671 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
674 p2p_buf_update_ie_hdr(buf, len);
680 void p2p_process_go_neg_resp(struct p2p_data *p2p, const u8 *sa,
681 const u8 *data, size_t len, int rx_freq)
683 struct p2p_device *dev;
686 struct p2p_message msg;
687 u8 status = P2P_SC_SUCCESS;
690 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
691 "P2P: Received GO Negotiation Response from " MACSTR
692 " (freq=%d)", MAC2STR(sa), rx_freq);
693 dev = p2p_get_device(p2p, sa);
694 if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
695 dev != p2p->go_neg_peer) {
696 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
697 "P2P: Not ready for GO negotiation with " MACSTR,
702 if (p2p_parse(data, len, &msg))
705 if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE)) {
706 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
707 "P2P: Was not expecting GO Negotiation Response - "
709 p2p_parse_free(&msg);
712 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
714 if (msg.dialog_token != dev->dialog_token) {
715 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
716 "P2P: Unexpected Dialog Token %u (expected %u)",
717 msg.dialog_token, dev->dialog_token);
718 p2p_parse_free(&msg);
723 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
724 "P2P: No Status attribute received");
725 status = P2P_SC_FAIL_INVALID_PARAMS;
729 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
730 "P2P: GO Negotiation rejected: status %d",
732 dev->go_neg_req_sent = 0;
733 if (*msg.status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
734 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
735 "P2P: Wait for the peer to become ready for "
737 dev->flags |= P2P_DEV_NOT_YET_READY;
739 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
740 p2p_set_timeout(p2p, 0, 0);
742 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
743 "P2P: Stop GO Negotiation attempt");
744 p2p_go_neg_failed(p2p, dev, *msg.status);
746 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
747 p2p_parse_free(&msg);
751 if (!msg.capability) {
752 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
753 "P2P: Mandatory Capability attribute missing from GO "
754 "Negotiation Response");
755 #ifdef CONFIG_P2P_STRICT
756 status = P2P_SC_FAIL_INVALID_PARAMS;
758 #endif /* CONFIG_P2P_STRICT */
761 if (!msg.p2p_device_info) {
762 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
763 "P2P: Mandatory P2P Device Info attribute missing "
764 "from GO Negotiation Response");
765 #ifdef CONFIG_P2P_STRICT
766 status = P2P_SC_FAIL_INVALID_PARAMS;
768 #endif /* CONFIG_P2P_STRICT */
771 if (!msg.intended_addr) {
772 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
773 "P2P: No Intended P2P Interface Address attribute "
775 status = P2P_SC_FAIL_INVALID_PARAMS;
779 if (!msg.go_intent) {
780 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
781 "P2P: No GO Intent attribute received");
782 status = P2P_SC_FAIL_INVALID_PARAMS;
785 if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
786 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
787 "P2P: Invalid GO Intent value (%u) received",
788 *msg.go_intent >> 1);
789 status = P2P_SC_FAIL_INVALID_PARAMS;
793 go = p2p_go_det(p2p->go_intent, *msg.go_intent);
795 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
796 "P2P: Incompatible GO Intent");
797 status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
801 if (!go && msg.group_id) {
802 /* Store SSID for Provisioning step */
803 p2p->ssid_len = msg.group_id_len - ETH_ALEN;
804 os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
806 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
807 "P2P: Mandatory P2P Group ID attribute missing from "
808 "GO Negotiation Response");
810 #ifdef CONFIG_P2P_STRICT
811 status = P2P_SC_FAIL_INVALID_PARAMS;
813 #endif /* CONFIG_P2P_STRICT */
816 if (!msg.config_timeout) {
817 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
818 "P2P: Mandatory Configuration Timeout attribute "
819 "missing from GO Negotiation Response");
820 #ifdef CONFIG_P2P_STRICT
821 status = P2P_SC_FAIL_INVALID_PARAMS;
823 #endif /* CONFIG_P2P_STRICT */
825 dev->go_timeout = msg.config_timeout[0];
826 dev->client_timeout = msg.config_timeout[1];
829 if (!msg.operating_channel && !go) {
831 * Note: P2P Client may omit Operating Channel attribute to
832 * indicate it does not have a preference.
834 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
835 "P2P: No Operating Channel attribute received");
836 status = P2P_SC_FAIL_INVALID_PARAMS;
839 if (!msg.channel_list) {
840 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
841 "P2P: No Channel List attribute received");
842 status = P2P_SC_FAIL_INVALID_PARAMS;
846 if (p2p_peer_channels(p2p, dev, msg.channel_list,
847 msg.channel_list_len) < 0) {
848 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
849 "P2P: No common channels found");
850 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
854 if (msg.operating_channel) {
855 dev->oper_freq = p2p_channel_to_freq((const char *)
856 msg.operating_channel,
857 msg.operating_channel[3],
858 msg.operating_channel[4]);
859 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
860 "channel preference: %d MHz", dev->oper_freq);
864 switch (msg.dev_password_id) {
866 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
867 "P2P: PIN from peer Label");
868 if (dev->wps_method != WPS_PIN_KEYPAD) {
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;
877 case DEV_PW_REGISTRAR_SPECIFIED:
878 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
879 "P2P: PIN from peer Display");
880 if (dev->wps_method != WPS_PIN_KEYPAD) {
881 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
882 "P2P: We have wps_method=%s -> "
884 p2p_wps_method_str(dev->wps_method));
885 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
889 case DEV_PW_USER_SPECIFIED:
890 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
891 "P2P: Peer entered PIN on Keypad");
892 if (dev->wps_method != WPS_PIN_LABEL &&
893 dev->wps_method != WPS_PIN_DISPLAY) {
894 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
895 "P2P: We have wps_method=%s -> "
897 p2p_wps_method_str(dev->wps_method));
898 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
902 case DEV_PW_PUSHBUTTON:
903 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
904 "P2P: Peer using pushbutton");
905 if (dev->wps_method != WPS_PBC) {
906 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
907 "P2P: We have wps_method=%s -> "
909 p2p_wps_method_str(dev->wps_method));
910 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
915 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
916 "P2P: Unsupported Device Password ID %d",
917 msg.dev_password_id);
918 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
923 struct p2p_channels intersection;
925 p2p_channels_intersect(&p2p->channels, &dev->channels,
927 if (intersection.reg_classes == 0 ||
928 intersection.reg_class[0].channels == 0) {
929 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
930 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
931 "P2P: No common channels found");
934 for (i = 0; i < intersection.reg_classes; i++) {
935 struct p2p_reg_class *c;
936 c = &intersection.reg_class[i];
937 wpa_printf(MSG_DEBUG, "P2P: reg_class %u",
939 wpa_hexdump(MSG_DEBUG, "P2P: channels",
940 c->channel, c->channels);
942 if (!p2p_channels_includes(&intersection, p2p->op_reg_class,
944 struct p2p_reg_class *cl;
945 cl = &intersection.reg_class[0];
946 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
947 "P2P: Selected operating channel "
948 "(reg_class %u channel %u) not "
949 "acceptable to the peer - pick "
950 "another channel (reg_class %u "
952 p2p->op_reg_class, p2p->op_channel,
953 cl->reg_class, cl->channel[0]);
954 p2p->op_reg_class = cl->reg_class;
955 p2p->op_channel = cl->channel[0];
958 p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
961 p2p_set_state(p2p, P2P_GO_NEG);
962 p2p_clear_timeout(p2p);
964 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
965 "P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
966 os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
969 conf = p2p_build_go_neg_conf(p2p, dev, msg.dialog_token, status,
970 msg.operating_channel, go);
971 p2p_parse_free(&msg);
974 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
975 "P2P: Sending GO Negotiation Confirm");
976 if (status == P2P_SC_SUCCESS) {
977 p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
978 dev->go_state = go ? LOCAL_GO : REMOTE_GO;
980 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
984 freq = dev->listen_freq;
985 if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, sa,
986 p2p->cfg->dev_addr, sa,
987 wpabuf_head(conf), wpabuf_len(conf), 200) <
989 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
990 "P2P: Failed to send Action frame");
991 p2p_go_neg_failed(p2p, dev, -1);
997 void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa,
998 const u8 *data, size_t len)
1000 struct p2p_device *dev;
1001 struct p2p_message msg;
1003 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1004 "P2P: Received GO Negotiation Confirm from " MACSTR,
1006 dev = p2p_get_device(p2p, sa);
1007 if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
1008 dev != p2p->go_neg_peer) {
1009 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1010 "P2P: Not ready for GO negotiation with " MACSTR,
1015 if (p2p->pending_action_state == P2P_PENDING_GO_NEG_RESPONSE) {
1016 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopped waiting "
1017 "for TX status on GO Negotiation Response since we "
1018 "already received Confirmation");
1019 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1022 if (p2p_parse(data, len, &msg))
1025 if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
1026 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1027 "P2P: Was not expecting GO Negotiation Confirm - "
1031 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1033 if (msg.dialog_token != dev->dialog_token) {
1034 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1035 "P2P: Unexpected Dialog Token %u (expected %u)",
1036 msg.dialog_token, dev->dialog_token);
1037 p2p_parse_free(&msg);
1042 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1043 "P2P: No Status attribute received");
1044 p2p_parse_free(&msg);
1048 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1049 "P2P: GO Negotiation rejected: status %d",
1051 p2p_parse_free(&msg);
1055 if (dev->go_state == REMOTE_GO && msg.group_id) {
1056 /* Store SSID for Provisioning step */
1057 p2p->ssid_len = msg.group_id_len - ETH_ALEN;
1058 os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
1059 } else if (dev->go_state == REMOTE_GO) {
1060 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1061 "P2P: Mandatory P2P Group ID attribute missing from "
1062 "GO Negotiation Confirmation");
1064 #ifdef CONFIG_P2P_STRICT
1065 p2p_parse_free(&msg);
1067 #endif /* CONFIG_P2P_STRICT */
1070 if (!msg.operating_channel) {
1071 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1072 "P2P: Mandatory Operating Channel attribute missing "
1073 "from GO Negotiation Confirmation");
1074 #ifdef CONFIG_P2P_STRICT
1075 p2p_parse_free(&msg);
1077 #endif /* CONFIG_P2P_STRICT */
1080 if (!msg.channel_list) {
1081 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1082 "P2P: Mandatory Operating Channel attribute missing "
1083 "from GO Negotiation Confirmation");
1084 #ifdef CONFIG_P2P_STRICT
1085 p2p_parse_free(&msg);
1087 #endif /* CONFIG_P2P_STRICT */
1090 p2p_parse_free(&msg);
1092 if (dev->go_state == UNKNOWN_GO) {
1094 * This should not happen since GO negotiation has already
1097 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1098 "P2P: Unexpected GO Neg state - do not know which end "
1103 p2p_go_complete(p2p, dev);