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;
213 if (p2p_send_action(p2p, freq, dev->p2p_device_addr,
214 p2p->cfg->dev_addr, dev->p2p_device_addr,
215 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
216 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
217 "P2P: Failed to send Action frame");
218 /* Use P2P find to recover and retry */
219 p2p_set_timeout(p2p, 0, 0);
228 static struct wpabuf * p2p_build_go_neg_resp(struct p2p_data *p2p,
229 struct p2p_device *peer,
230 u8 dialog_token, u8 status,
237 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
238 "P2P: Building GO Negotiation Response");
239 buf = wpabuf_alloc(1000);
243 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_RESP, dialog_token);
245 len = p2p_buf_add_ie_hdr(buf);
246 p2p_buf_add_status(buf, status);
248 if (peer && peer->go_state == LOCAL_GO) {
249 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP)
250 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
251 if (p2p->cross_connect)
252 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
253 if (p2p->cfg->p2p_intra_bss)
254 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
256 p2p_buf_add_capability(buf, p2p->dev_capab, group_capab);
257 p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | tie_breaker);
258 p2p_buf_add_config_timeout(buf, 100, 20);
259 if (peer && peer->go_state == REMOTE_GO) {
260 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Omit Operating "
261 "Channel attribute");
263 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
267 p2p_buf_add_intended_addr(buf, p2p->intended_addr);
268 if (status || peer == NULL) {
269 p2p_buf_add_channel_list(buf, p2p->cfg->country,
271 } else if (peer->go_state == REMOTE_GO) {
272 p2p_buf_add_channel_list(buf, p2p->cfg->country,
275 struct p2p_channels res;
276 p2p_channels_intersect(&p2p->channels, &peer->channels,
278 p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
280 p2p_buf_add_device_info(buf, p2p, peer);
281 if (peer && peer->go_state == LOCAL_GO) {
282 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
285 p2p_buf_update_ie_hdr(buf, len);
287 /* WPS IE with Device Password ID attribute */
288 p2p_build_wps_ie(p2p, buf,
289 p2p_wps_method_pw_id(peer ? peer->wps_method :
296 static void p2p_reselect_channel(struct p2p_data *p2p,
297 struct p2p_channels *intersection)
299 struct p2p_reg_class *cl;
301 u8 op_reg_class, op_channel;
303 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Selected operating "
304 "channel (reg_class %u channel %u) not acceptable to the "
305 "peer", p2p->op_reg_class, p2p->op_channel);
307 /* First, try to pick the best channel from another band */
308 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->op_reg_class,
310 if (freq >= 2400 && freq < 2500 && p2p->best_freq_5 > 0 &&
311 p2p_freq_to_channel(p2p->cfg->country, p2p->best_freq_5,
312 &op_reg_class, &op_channel) == 0 &&
313 p2p_channels_includes(intersection, op_reg_class, op_channel)) {
314 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick best 5 GHz "
315 "channel (reg_class %u channel %u) from intersection",
316 op_reg_class, op_channel);
317 p2p->op_reg_class = op_reg_class;
318 p2p->op_channel = op_channel;
322 if (freq >= 4900 && freq < 6000 && p2p->best_freq_24 > 0 &&
323 p2p_freq_to_channel(p2p->cfg->country, p2p->best_freq_24,
324 &op_reg_class, &op_channel) == 0 &&
325 p2p_channels_includes(intersection, op_reg_class, op_channel)) {
326 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick best 2.4 GHz "
327 "channel (reg_class %u channel %u) from intersection",
328 op_reg_class, op_channel);
329 p2p->op_reg_class = op_reg_class;
330 p2p->op_channel = op_channel;
335 * Fall back to whatever is included in the channel intersection since
336 * no better options seems to be available.
338 cl = &intersection->reg_class[0];
339 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick another channel "
340 "(reg_class %u channel %u) from intersection",
341 cl->reg_class, cl->channel[0]);
342 p2p->op_reg_class = cl->reg_class;
343 p2p->op_channel = cl->channel[0];
347 void p2p_process_go_neg_req(struct p2p_data *p2p, const u8 *sa,
348 const u8 *data, size_t len, int rx_freq)
350 struct p2p_device *dev = NULL;
352 struct p2p_message msg;
353 u8 status = P2P_SC_FAIL_INVALID_PARAMS;
357 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
358 "P2P: Received GO Negotiation Request from " MACSTR
359 "(freq=%d)", MAC2STR(sa), rx_freq);
361 if (p2p_parse(data, len, &msg))
364 if (!msg.capability) {
365 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
366 "P2P: Mandatory Capability attribute missing from GO "
367 "Negotiation Request");
368 #ifdef CONFIG_P2P_STRICT
370 #endif /* CONFIG_P2P_STRICT */
374 tie_breaker = *msg.go_intent & 0x01;
376 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
377 "P2P: Mandatory GO Intent attribute missing from GO "
378 "Negotiation Request");
379 #ifdef CONFIG_P2P_STRICT
381 #endif /* CONFIG_P2P_STRICT */
384 if (!msg.config_timeout) {
385 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
386 "P2P: Mandatory Configuration Timeout attribute "
387 "missing from GO Negotiation Request");
388 #ifdef CONFIG_P2P_STRICT
390 #endif /* CONFIG_P2P_STRICT */
393 if (!msg.listen_channel) {
394 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
395 "P2P: No Listen Channel attribute received");
398 if (!msg.operating_channel) {
399 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
400 "P2P: No Operating Channel attribute received");
403 if (!msg.channel_list) {
404 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
405 "P2P: No Channel List attribute received");
408 if (!msg.intended_addr) {
409 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
410 "P2P: No Intended P2P Interface Address attribute "
414 if (!msg.p2p_device_info) {
415 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
416 "P2P: No P2P Device Info attribute received");
420 if (os_memcmp(msg.p2p_device_addr, sa, ETH_ALEN) != 0) {
421 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
422 "P2P: Unexpected GO Negotiation Request SA=" MACSTR
423 " != dev_addr=" MACSTR,
424 MAC2STR(sa), MAC2STR(msg.p2p_device_addr));
428 dev = p2p_get_device(p2p, sa);
430 if (msg.status && *msg.status) {
431 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
432 "P2P: Unexpected Status attribute (%d) in GO "
433 "Negotiation Request", *msg.status);
438 dev = p2p_add_dev_from_go_neg_req(p2p, sa, &msg);
439 else if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
440 p2p_add_dev_info(p2p, sa, dev, &msg);
441 if (dev && dev->flags & P2P_DEV_USER_REJECTED) {
442 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
443 "P2P: User has rejected this peer");
444 status = P2P_SC_FAIL_REJECTED_BY_USER;
445 } else if (dev == NULL || dev->wps_method == WPS_NOT_READY) {
446 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
447 "P2P: Not ready for GO negotiation with " MACSTR,
449 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
451 dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
452 p2p->cfg->go_neg_req_rx(p2p->cfg->cb_ctx, sa,
453 msg.dev_password_id);
454 } else if (p2p->go_neg_peer && p2p->go_neg_peer != dev) {
455 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
456 "P2P: Already in Group Formation with another peer");
457 status = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
461 if (!p2p->go_neg_peer) {
462 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting "
463 "GO Negotiation with previously authorized "
465 if (!(dev->flags & P2P_DEV_FORCE_FREQ)) {
466 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
467 "P2P: Use default channel settings");
468 p2p->op_reg_class = p2p->cfg->op_reg_class;
469 p2p->op_channel = p2p->cfg->op_channel;
470 os_memcpy(&p2p->channels, &p2p->cfg->channels,
471 sizeof(struct p2p_channels));
473 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
474 "P2P: Use previously configured "
475 "forced channel settings");
479 dev->flags &= ~P2P_DEV_NOT_YET_READY;
481 if (!msg.go_intent) {
482 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
483 "P2P: No GO Intent attribute received");
486 if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
487 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
488 "P2P: Invalid GO Intent value (%u) received",
489 *msg.go_intent >> 1);
493 if (dev->go_neg_req_sent &&
494 os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) > 0) {
495 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
496 "P2P: Do not reply since peer has higher "
497 "address and GO Neg Request already sent");
498 p2p_parse_free(&msg);
502 go = p2p_go_det(p2p->go_intent, *msg.go_intent);
504 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
505 "P2P: Incompatible GO Intent");
506 status = P2P_SC_FAIL_BOTH_GO_INTENT_15;
510 if (p2p_peer_channels(p2p, dev, msg.channel_list,
511 msg.channel_list_len) < 0) {
512 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
513 "P2P: No common channels found");
514 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
518 switch (msg.dev_password_id) {
520 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
521 "P2P: PIN from peer Label");
522 if (dev->wps_method != WPS_PIN_KEYPAD) {
523 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
524 "P2P: We have wps_method=%s -> "
526 p2p_wps_method_str(dev->wps_method));
527 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
531 case DEV_PW_REGISTRAR_SPECIFIED:
532 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
533 "P2P: PIN from peer Display");
534 if (dev->wps_method != WPS_PIN_KEYPAD) {
535 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
536 "P2P: We have wps_method=%s -> "
538 p2p_wps_method_str(dev->wps_method));
539 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
543 case DEV_PW_USER_SPECIFIED:
544 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
545 "P2P: Peer entered PIN on Keypad");
546 if (dev->wps_method != WPS_PIN_LABEL &&
547 dev->wps_method != WPS_PIN_DISPLAY) {
548 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
549 "P2P: We have wps_method=%s -> "
551 p2p_wps_method_str(dev->wps_method));
552 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
556 case DEV_PW_PUSHBUTTON:
557 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
558 "P2P: Peer using pushbutton");
559 if (dev->wps_method != WPS_PBC) {
560 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
561 "P2P: We have wps_method=%s -> "
563 p2p_wps_method_str(dev->wps_method));
564 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
569 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
570 "P2P: Unsupported Device Password ID %d",
571 msg.dev_password_id);
572 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
577 struct p2p_channels intersection;
579 p2p_channels_intersect(&p2p->channels, &dev->channels,
581 if (intersection.reg_classes == 0 ||
582 intersection.reg_class[0].channels == 0) {
583 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
584 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
585 "P2P: No common channels found");
588 for (i = 0; i < intersection.reg_classes; i++) {
589 struct p2p_reg_class *c;
590 c = &intersection.reg_class[i];
591 wpa_printf(MSG_DEBUG, "P2P: reg_class %u",
593 wpa_hexdump(MSG_DEBUG, "P2P: channels",
594 c->channel, c->channels);
596 if (!p2p_channels_includes(&intersection,
599 p2p_reselect_channel(p2p, &intersection);
601 p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
604 dev->go_state = go ? LOCAL_GO : REMOTE_GO;
605 dev->oper_freq = p2p_channel_to_freq((const char *)
606 msg.operating_channel,
607 msg.operating_channel[3],
608 msg.operating_channel[4]);
609 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
610 "channel preference: %d MHz", dev->oper_freq);
612 if (msg.config_timeout) {
613 dev->go_timeout = msg.config_timeout[0];
614 dev->client_timeout = msg.config_timeout[1];
617 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
618 "P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
619 if (p2p->state != P2P_IDLE)
620 p2p_stop_find_for_freq(p2p, rx_freq);
621 p2p_set_state(p2p, P2P_GO_NEG);
622 p2p_clear_timeout(p2p);
623 dev->dialog_token = msg.dialog_token;
624 os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
625 p2p->go_neg_peer = dev;
626 status = P2P_SC_SUCCESS;
631 dev->status = status;
632 resp = p2p_build_go_neg_resp(p2p, dev, msg.dialog_token, status,
634 p2p_parse_free(&msg);
637 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
638 "P2P: Sending GO Negotiation Response");
642 freq = p2p_channel_to_freq(p2p->cfg->country,
646 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
647 "P2P: Unknown regulatory class/channel");
651 if (status == P2P_SC_SUCCESS) {
652 p2p->pending_action_state = P2P_PENDING_GO_NEG_RESPONSE;
653 dev->flags |= P2P_DEV_WAIT_GO_NEG_CONFIRM;
655 p2p->pending_action_state =
656 P2P_PENDING_GO_NEG_RESPONSE_FAILURE;
657 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
659 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
660 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
661 "P2P: Failed to send Action frame");
668 static struct wpabuf * p2p_build_go_neg_conf(struct p2p_data *p2p,
669 struct p2p_device *peer,
670 u8 dialog_token, u8 status,
671 const u8 *resp_chan, int go)
675 struct p2p_channels res;
678 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
679 "P2P: Building GO Negotiation Confirm");
680 buf = wpabuf_alloc(1000);
684 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_CONF, dialog_token);
686 len = p2p_buf_add_ie_hdr(buf);
687 p2p_buf_add_status(buf, status);
689 if (peer->go_state == LOCAL_GO) {
690 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP)
691 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
692 if (p2p->cross_connect)
693 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
694 if (p2p->cfg->p2p_intra_bss)
695 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
697 p2p_buf_add_capability(buf, p2p->dev_capab, group_capab);
698 if (go || resp_chan == NULL)
699 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
703 p2p_buf_add_operating_channel(buf, (const char *) resp_chan,
704 resp_chan[3], resp_chan[4]);
705 p2p_channels_intersect(&p2p->channels, &peer->channels, &res);
706 p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
708 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
711 p2p_buf_update_ie_hdr(buf, len);
717 void p2p_process_go_neg_resp(struct p2p_data *p2p, const u8 *sa,
718 const u8 *data, size_t len, int rx_freq)
720 struct p2p_device *dev;
723 struct p2p_message msg;
724 u8 status = P2P_SC_SUCCESS;
727 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
728 "P2P: Received GO Negotiation Response from " MACSTR
729 " (freq=%d)", MAC2STR(sa), rx_freq);
730 dev = p2p_get_device(p2p, sa);
731 if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
732 dev != p2p->go_neg_peer) {
733 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
734 "P2P: Not ready for GO negotiation with " MACSTR,
739 if (p2p_parse(data, len, &msg))
742 if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE)) {
743 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
744 "P2P: Was not expecting GO Negotiation Response - "
746 p2p_parse_free(&msg);
749 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
751 if (msg.dialog_token != dev->dialog_token) {
752 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
753 "P2P: Unexpected Dialog Token %u (expected %u)",
754 msg.dialog_token, dev->dialog_token);
755 p2p_parse_free(&msg);
760 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
761 "P2P: No Status attribute received");
762 status = P2P_SC_FAIL_INVALID_PARAMS;
766 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
767 "P2P: GO Negotiation rejected: status %d",
769 dev->go_neg_req_sent = 0;
770 if (*msg.status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
771 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
772 "P2P: Wait for the peer to become ready for "
774 dev->flags |= P2P_DEV_NOT_YET_READY;
776 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
777 p2p_set_timeout(p2p, 0, 0);
779 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
780 "P2P: Stop GO Negotiation attempt");
781 p2p_go_neg_failed(p2p, dev, *msg.status);
783 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
784 p2p_parse_free(&msg);
788 if (!msg.capability) {
789 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
790 "P2P: Mandatory Capability attribute missing from GO "
791 "Negotiation Response");
792 #ifdef CONFIG_P2P_STRICT
793 status = P2P_SC_FAIL_INVALID_PARAMS;
795 #endif /* CONFIG_P2P_STRICT */
798 if (!msg.p2p_device_info) {
799 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
800 "P2P: Mandatory P2P Device Info attribute missing "
801 "from GO Negotiation Response");
802 #ifdef CONFIG_P2P_STRICT
803 status = P2P_SC_FAIL_INVALID_PARAMS;
805 #endif /* CONFIG_P2P_STRICT */
808 if (!msg.intended_addr) {
809 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
810 "P2P: No Intended P2P Interface Address attribute "
812 status = P2P_SC_FAIL_INVALID_PARAMS;
816 if (!msg.go_intent) {
817 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
818 "P2P: No GO Intent attribute received");
819 status = P2P_SC_FAIL_INVALID_PARAMS;
822 if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
823 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
824 "P2P: Invalid GO Intent value (%u) received",
825 *msg.go_intent >> 1);
826 status = P2P_SC_FAIL_INVALID_PARAMS;
830 go = p2p_go_det(p2p->go_intent, *msg.go_intent);
832 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
833 "P2P: Incompatible GO Intent");
834 status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
838 if (!go && msg.group_id) {
839 /* Store SSID for Provisioning step */
840 p2p->ssid_len = msg.group_id_len - ETH_ALEN;
841 os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
843 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
844 "P2P: Mandatory P2P Group ID attribute missing from "
845 "GO Negotiation Response");
847 #ifdef CONFIG_P2P_STRICT
848 status = P2P_SC_FAIL_INVALID_PARAMS;
850 #endif /* CONFIG_P2P_STRICT */
853 if (!msg.config_timeout) {
854 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
855 "P2P: Mandatory Configuration Timeout attribute "
856 "missing from GO Negotiation Response");
857 #ifdef CONFIG_P2P_STRICT
858 status = P2P_SC_FAIL_INVALID_PARAMS;
860 #endif /* CONFIG_P2P_STRICT */
862 dev->go_timeout = msg.config_timeout[0];
863 dev->client_timeout = msg.config_timeout[1];
866 if (!msg.operating_channel && !go) {
868 * Note: P2P Client may omit Operating Channel attribute to
869 * indicate it does not have a preference.
871 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
872 "P2P: No Operating Channel attribute received");
873 status = P2P_SC_FAIL_INVALID_PARAMS;
876 if (!msg.channel_list) {
877 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
878 "P2P: No Channel List attribute received");
879 status = P2P_SC_FAIL_INVALID_PARAMS;
883 if (p2p_peer_channels(p2p, dev, msg.channel_list,
884 msg.channel_list_len) < 0) {
885 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
886 "P2P: No common channels found");
887 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
891 if (msg.operating_channel) {
892 dev->oper_freq = p2p_channel_to_freq((const char *)
893 msg.operating_channel,
894 msg.operating_channel[3],
895 msg.operating_channel[4]);
896 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
897 "channel preference: %d MHz", dev->oper_freq);
901 switch (msg.dev_password_id) {
903 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
904 "P2P: PIN from peer Label");
905 if (dev->wps_method != WPS_PIN_KEYPAD) {
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;
914 case DEV_PW_REGISTRAR_SPECIFIED:
915 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
916 "P2P: PIN from peer Display");
917 if (dev->wps_method != WPS_PIN_KEYPAD) {
918 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
919 "P2P: We have wps_method=%s -> "
921 p2p_wps_method_str(dev->wps_method));
922 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
926 case DEV_PW_USER_SPECIFIED:
927 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
928 "P2P: Peer entered PIN on Keypad");
929 if (dev->wps_method != WPS_PIN_LABEL &&
930 dev->wps_method != WPS_PIN_DISPLAY) {
931 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
932 "P2P: We have wps_method=%s -> "
934 p2p_wps_method_str(dev->wps_method));
935 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
939 case DEV_PW_PUSHBUTTON:
940 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
941 "P2P: Peer using pushbutton");
942 if (dev->wps_method != WPS_PBC) {
943 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
944 "P2P: We have wps_method=%s -> "
946 p2p_wps_method_str(dev->wps_method));
947 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
952 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
953 "P2P: Unsupported Device Password ID %d",
954 msg.dev_password_id);
955 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
960 struct p2p_channels intersection;
962 p2p_channels_intersect(&p2p->channels, &dev->channels,
964 if (intersection.reg_classes == 0 ||
965 intersection.reg_class[0].channels == 0) {
966 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
967 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
968 "P2P: No common channels found");
971 for (i = 0; i < intersection.reg_classes; i++) {
972 struct p2p_reg_class *c;
973 c = &intersection.reg_class[i];
974 wpa_printf(MSG_DEBUG, "P2P: reg_class %u",
976 wpa_hexdump(MSG_DEBUG, "P2P: channels",
977 c->channel, c->channels);
979 if (!p2p_channels_includes(&intersection, p2p->op_reg_class,
981 p2p_reselect_channel(p2p, &intersection);
983 p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
986 p2p_set_state(p2p, P2P_GO_NEG);
987 p2p_clear_timeout(p2p);
989 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
990 "P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
991 os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
994 conf = p2p_build_go_neg_conf(p2p, dev, msg.dialog_token, status,
995 msg.operating_channel, go);
996 p2p_parse_free(&msg);
999 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1000 "P2P: Sending GO Negotiation Confirm");
1001 if (status == P2P_SC_SUCCESS) {
1002 p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
1003 dev->go_state = go ? LOCAL_GO : REMOTE_GO;
1005 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1009 freq = dev->listen_freq;
1010 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr, sa,
1011 wpabuf_head(conf), wpabuf_len(conf), 200) < 0) {
1012 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1013 "P2P: Failed to send Action frame");
1014 p2p_go_neg_failed(p2p, dev, -1);
1020 void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa,
1021 const u8 *data, size_t len)
1023 struct p2p_device *dev;
1024 struct p2p_message msg;
1026 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1027 "P2P: Received GO Negotiation Confirm from " MACSTR,
1029 dev = p2p_get_device(p2p, sa);
1030 if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
1031 dev != p2p->go_neg_peer) {
1032 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1033 "P2P: Not ready for GO negotiation with " MACSTR,
1038 if (p2p->pending_action_state == P2P_PENDING_GO_NEG_RESPONSE) {
1039 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopped waiting "
1040 "for TX status on GO Negotiation Response since we "
1041 "already received Confirmation");
1042 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1045 if (p2p_parse(data, len, &msg))
1048 if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
1049 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1050 "P2P: Was not expecting GO Negotiation Confirm - "
1054 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1056 if (msg.dialog_token != dev->dialog_token) {
1057 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1058 "P2P: Unexpected Dialog Token %u (expected %u)",
1059 msg.dialog_token, dev->dialog_token);
1060 p2p_parse_free(&msg);
1065 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1066 "P2P: No Status attribute received");
1067 p2p_parse_free(&msg);
1071 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1072 "P2P: GO Negotiation rejected: status %d",
1074 p2p_parse_free(&msg);
1078 if (dev->go_state == REMOTE_GO && msg.group_id) {
1079 /* Store SSID for Provisioning step */
1080 p2p->ssid_len = msg.group_id_len - ETH_ALEN;
1081 os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
1082 } else if (dev->go_state == REMOTE_GO) {
1083 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1084 "P2P: Mandatory P2P Group ID attribute missing from "
1085 "GO Negotiation Confirmation");
1087 #ifdef CONFIG_P2P_STRICT
1088 p2p_parse_free(&msg);
1090 #endif /* CONFIG_P2P_STRICT */
1093 if (!msg.operating_channel) {
1094 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1095 "P2P: Mandatory Operating Channel attribute missing "
1096 "from GO Negotiation Confirmation");
1097 #ifdef CONFIG_P2P_STRICT
1098 p2p_parse_free(&msg);
1100 #endif /* CONFIG_P2P_STRICT */
1103 if (!msg.channel_list) {
1104 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1105 "P2P: Mandatory Operating Channel attribute missing "
1106 "from GO Negotiation Confirmation");
1107 #ifdef CONFIG_P2P_STRICT
1108 p2p_parse_free(&msg);
1110 #endif /* CONFIG_P2P_STRICT */
1113 p2p_parse_free(&msg);
1115 if (dev->go_state == UNKNOWN_GO) {
1117 * This should not happen since GO negotiation has already
1120 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1121 "P2P: Unexpected GO Neg state - do not know which end "
1126 p2p_go_complete(p2p, dev);