2 * Wi-Fi Direct - P2P Group Owner Negotiation
3 * Copyright (c) 2009-2010, Atheros Communications
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "common/ieee802_11_defs.h"
13 #include "wps/wps_defs.h"
18 static int p2p_go_det(u8 own_intent, u8 peer_value)
20 u8 peer_intent = peer_value >> 1;
21 if (own_intent == peer_intent) {
22 if (own_intent == P2P_MAX_GO_INTENT)
23 return -1; /* both devices want to become GO */
25 /* Use tie breaker bit to determine GO */
26 return (peer_value & 0x01) ? 0 : 1;
29 return own_intent > peer_intent;
33 int p2p_peer_channels_check(struct p2p_data *p2p, struct p2p_channels *own,
34 struct p2p_device *dev,
35 const u8 *channel_list, size_t channel_list_len)
38 struct p2p_channels *ch;
40 struct p2p_channels intersection;
43 os_memset(ch, 0, sizeof(*ch));
45 end = channel_list + channel_list_len;
49 os_memcpy(dev->country, pos, 3);
50 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Peer country", pos, 3);
51 if (pos[2] != 0x04 && os_memcmp(pos, p2p->cfg->country, 2) != 0) {
52 p2p_info(p2p, "Mismatching country (ours=%c%c peer's=%c%c)",
53 p2p->cfg->country[0], p2p->cfg->country[1],
59 while (pos + 2 < end) {
60 struct p2p_reg_class *cl = &ch->reg_class[ch->reg_classes];
61 cl->reg_class = *pos++;
62 if (pos + 1 + pos[0] > end) {
63 p2p_info(p2p, "Invalid peer Channel List");
67 cl->channels = channels > P2P_MAX_REG_CLASS_CHANNELS ?
68 P2P_MAX_REG_CLASS_CHANNELS : channels;
69 os_memcpy(cl->channel, pos, cl->channels);
72 if (ch->reg_classes == P2P_MAX_REG_CLASSES)
76 p2p_channels_intersect(own, &dev->channels, &intersection);
77 p2p_dbg(p2p, "Own reg_classes %d peer reg_classes %d intersection reg_classes %d",
78 (int) own->reg_classes,
79 (int) dev->channels.reg_classes,
80 (int) intersection.reg_classes);
81 if (intersection.reg_classes == 0) {
82 p2p_info(p2p, "No common channels found");
89 static int p2p_peer_channels(struct p2p_data *p2p, struct p2p_device *dev,
90 const u8 *channel_list, size_t channel_list_len)
92 return p2p_peer_channels_check(p2p, &p2p->channels, dev,
93 channel_list, channel_list_len);
97 u16 p2p_wps_method_pw_id(enum p2p_wps_method wps_method)
100 case WPS_PIN_DISPLAY:
101 return DEV_PW_REGISTRAR_SPECIFIED;
103 return DEV_PW_USER_SPECIFIED;
105 return DEV_PW_PUSHBUTTON;
107 return DEV_PW_DEFAULT;
112 static const char * p2p_wps_method_str(enum p2p_wps_method wps_method)
114 switch (wps_method) {
115 case WPS_PIN_DISPLAY:
127 static struct wpabuf * p2p_build_go_neg_req(struct p2p_data *p2p,
128 struct p2p_device *peer)
135 #ifdef CONFIG_WIFI_DISPLAY
136 if (p2p->wfd_ie_go_neg)
137 extra = wpabuf_len(p2p->wfd_ie_go_neg);
138 #endif /* CONFIG_WIFI_DISPLAY */
140 buf = wpabuf_alloc(1000 + extra);
144 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_REQ, peer->dialog_token);
146 len = p2p_buf_add_ie_hdr(buf);
148 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
149 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
150 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
151 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_RECONN;
153 if (p2p->cross_connect)
154 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
155 if (p2p->cfg->p2p_intra_bss)
156 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
157 p2p_buf_add_capability(buf, p2p->dev_capab &
158 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
160 p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | peer->tie_breaker);
161 p2p_buf_add_config_timeout(buf, p2p->go_timeout, p2p->client_timeout);
162 p2p_buf_add_listen_channel(buf, p2p->cfg->country, p2p->cfg->reg_class,
164 if (p2p->ext_listen_interval)
165 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
166 p2p->ext_listen_interval);
167 p2p_buf_add_intended_addr(buf, p2p->intended_addr);
168 p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
169 p2p_buf_add_device_info(buf, p2p, peer);
170 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
171 p2p->op_reg_class, p2p->op_channel);
172 p2p_buf_update_ie_hdr(buf, len);
174 /* WPS IE with Device Password ID attribute */
175 p2p_build_wps_ie(p2p, buf, p2p_wps_method_pw_id(peer->wps_method), 0);
177 #ifdef CONFIG_WIFI_DISPLAY
178 if (p2p->wfd_ie_go_neg)
179 wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
180 #endif /* CONFIG_WIFI_DISPLAY */
186 int p2p_connect_send(struct p2p_data *p2p, struct p2p_device *dev)
191 if (dev->flags & P2P_DEV_PD_BEFORE_GO_NEG) {
193 p2p_dbg(p2p, "Use PD-before-GO-Neg workaround for " MACSTR,
194 MAC2STR(dev->info.p2p_device_addr));
195 if (dev->wps_method == WPS_PIN_DISPLAY)
196 config_method = WPS_CONFIG_KEYPAD;
197 else if (dev->wps_method == WPS_PIN_KEYPAD)
198 config_method = WPS_CONFIG_DISPLAY;
199 else if (dev->wps_method == WPS_PBC)
200 config_method = WPS_CONFIG_PUSHBUTTON;
203 return p2p_prov_disc_req(p2p, dev->info.p2p_device_addr,
204 config_method, 0, 0, 1);
207 freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
209 p2p_dbg(p2p, "No Listen/Operating frequency known for the peer "
210 MACSTR " to send GO Negotiation Request",
211 MAC2STR(dev->info.p2p_device_addr));
215 req = p2p_build_go_neg_req(p2p, dev);
218 p2p_dbg(p2p, "Sending GO Negotiation Request");
219 p2p_set_state(p2p, P2P_CONNECT);
220 p2p->pending_action_state = P2P_PENDING_GO_NEG_REQUEST;
221 p2p->go_neg_peer = dev;
222 dev->flags |= P2P_DEV_WAIT_GO_NEG_RESPONSE;
224 if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
225 p2p->cfg->dev_addr, dev->info.p2p_device_addr,
226 wpabuf_head(req), wpabuf_len(req), 500) < 0) {
227 p2p_dbg(p2p, "Failed to send Action frame");
228 /* Use P2P find to recover and retry */
229 p2p_set_timeout(p2p, 0, 0);
231 dev->go_neg_req_sent++;
239 static struct wpabuf * p2p_build_go_neg_resp(struct p2p_data *p2p,
240 struct p2p_device *peer,
241 u8 dialog_token, u8 status,
249 p2p_dbg(p2p, "Building GO Negotiation Response");
251 #ifdef CONFIG_WIFI_DISPLAY
252 if (p2p->wfd_ie_go_neg)
253 extra = wpabuf_len(p2p->wfd_ie_go_neg);
254 #endif /* CONFIG_WIFI_DISPLAY */
256 buf = wpabuf_alloc(1000 + extra);
260 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_RESP, dialog_token);
262 len = p2p_buf_add_ie_hdr(buf);
263 p2p_buf_add_status(buf, status);
265 if (peer && peer->go_state == LOCAL_GO) {
266 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
267 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
268 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
270 P2P_GROUP_CAPAB_PERSISTENT_RECONN;
272 if (p2p->cross_connect)
273 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
274 if (p2p->cfg->p2p_intra_bss)
275 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
277 p2p_buf_add_capability(buf, p2p->dev_capab &
278 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
280 p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | tie_breaker);
281 p2p_buf_add_config_timeout(buf, p2p->go_timeout, p2p->client_timeout);
282 if (peer && peer->go_state == REMOTE_GO) {
283 p2p_dbg(p2p, "Omit Operating Channel attribute");
285 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
289 p2p_buf_add_intended_addr(buf, p2p->intended_addr);
290 if (status || peer == NULL) {
291 p2p_buf_add_channel_list(buf, p2p->cfg->country,
293 } else if (peer->go_state == REMOTE_GO) {
294 p2p_buf_add_channel_list(buf, p2p->cfg->country,
297 struct p2p_channels res;
298 p2p_channels_intersect(&p2p->channels, &peer->channels,
300 p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
302 p2p_buf_add_device_info(buf, p2p, peer);
303 if (peer && peer->go_state == LOCAL_GO) {
304 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
307 p2p_buf_update_ie_hdr(buf, len);
309 /* WPS IE with Device Password ID attribute */
310 p2p_build_wps_ie(p2p, buf,
311 p2p_wps_method_pw_id(peer ? peer->wps_method :
314 #ifdef CONFIG_WIFI_DISPLAY
315 if (p2p->wfd_ie_go_neg)
316 wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
317 #endif /* CONFIG_WIFI_DISPLAY */
325 * p2p_reselect_channel - Re-select operating channel based on peer information
326 * @p2p: P2P module context from p2p_init()
327 * @intersection: Support channel list intersection from local and peer
329 * This function is used to re-select the best channel after having received
330 * information from the peer to allow supported channel lists to be intersected.
331 * This can be used to improve initial channel selection done in
332 * p2p_prepare_channel() prior to the start of GO Negotiation. In addition, this
333 * can be used for Invitation case.
335 void p2p_reselect_channel(struct p2p_data *p2p,
336 struct p2p_channels *intersection)
338 struct p2p_reg_class *cl;
340 u8 op_reg_class, op_channel;
343 if (p2p->own_freq_preference > 0 &&
344 p2p_freq_to_channel(p2p->own_freq_preference,
345 &op_reg_class, &op_channel) == 0 &&
346 p2p_channels_includes(intersection, op_reg_class, op_channel)) {
347 p2p_dbg(p2p, "Pick own channel preference (reg_class %u channel %u) from intersection",
348 op_reg_class, op_channel);
349 p2p->op_reg_class = op_reg_class;
350 p2p->op_channel = op_channel;
354 if (p2p->best_freq_overall > 0 &&
355 p2p_freq_to_channel(p2p->best_freq_overall,
356 &op_reg_class, &op_channel) == 0 &&
357 p2p_channels_includes(intersection, op_reg_class, op_channel)) {
358 p2p_dbg(p2p, "Pick best overall channel (reg_class %u channel %u) from intersection",
359 op_reg_class, op_channel);
360 p2p->op_reg_class = op_reg_class;
361 p2p->op_channel = op_channel;
365 /* First, try to pick the best channel from another band */
366 freq = p2p_channel_to_freq(p2p->op_reg_class, p2p->op_channel);
367 if (freq >= 2400 && freq < 2500 && p2p->best_freq_5 > 0 &&
368 !p2p_channels_includes(intersection, p2p->op_reg_class,
370 p2p_freq_to_channel(p2p->best_freq_5,
371 &op_reg_class, &op_channel) == 0 &&
372 p2p_channels_includes(intersection, op_reg_class, op_channel)) {
373 p2p_dbg(p2p, "Pick best 5 GHz channel (reg_class %u channel %u) from intersection",
374 op_reg_class, op_channel);
375 p2p->op_reg_class = op_reg_class;
376 p2p->op_channel = op_channel;
380 if (freq >= 4900 && freq < 6000 && p2p->best_freq_24 > 0 &&
381 !p2p_channels_includes(intersection, p2p->op_reg_class,
383 p2p_freq_to_channel(p2p->best_freq_24,
384 &op_reg_class, &op_channel) == 0 &&
385 p2p_channels_includes(intersection, op_reg_class, op_channel)) {
386 p2p_dbg(p2p, "Pick best 2.4 GHz channel (reg_class %u channel %u) from intersection",
387 op_reg_class, op_channel);
388 p2p->op_reg_class = op_reg_class;
389 p2p->op_channel = op_channel;
393 /* Select channel with highest preference if the peer supports it */
394 for (i = 0; p2p->cfg->pref_chan && i < p2p->cfg->num_pref_chan; i++) {
395 if (p2p_channels_includes(intersection,
396 p2p->cfg->pref_chan[i].op_class,
397 p2p->cfg->pref_chan[i].chan)) {
398 p2p->op_reg_class = p2p->cfg->pref_chan[i].op_class;
399 p2p->op_channel = p2p->cfg->pref_chan[i].chan;
400 p2p_dbg(p2p, "Pick highest preferred channel (op_class %u channel %u) from intersection",
401 p2p->op_reg_class, p2p->op_channel);
406 /* Try a channel where we might be able to use HT40 */
407 for (i = 0; i < intersection->reg_classes; i++) {
408 struct p2p_reg_class *c = &intersection->reg_class[i];
409 if (c->reg_class == 116 || c->reg_class == 117 ||
410 c->reg_class == 126 || c->reg_class == 127) {
411 p2p_dbg(p2p, "Pick possible HT40 channel (reg_class %u channel %u) from intersection",
412 c->reg_class, c->channel[0]);
413 p2p->op_reg_class = c->reg_class;
414 p2p->op_channel = c->channel[0];
420 * Try to see if the original channel is in the intersection. If
421 * so, no need to change anything, as it already contains some
424 if (p2p_channels_includes(intersection, p2p->op_reg_class,
426 p2p_dbg(p2p, "Using original operating class and channel (op_class %u channel %u) from intersection",
427 p2p->op_reg_class, p2p->op_channel);
432 * Fall back to whatever is included in the channel intersection since
433 * no better options seems to be available.
435 cl = &intersection->reg_class[0];
436 p2p_dbg(p2p, "Pick another channel (reg_class %u channel %u) from intersection",
437 cl->reg_class, cl->channel[0]);
438 p2p->op_reg_class = cl->reg_class;
439 p2p->op_channel = cl->channel[0];
443 static int p2p_go_select_channel(struct p2p_data *p2p, struct p2p_device *dev,
446 struct p2p_channels intersection;
449 p2p_channels_intersect(&p2p->channels, &dev->channels, &intersection);
450 if (intersection.reg_classes == 0 ||
451 intersection.reg_class[0].channels == 0) {
452 *status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
453 p2p_dbg(p2p, "No common channels found");
457 for (i = 0; i < intersection.reg_classes; i++) {
458 struct p2p_reg_class *c;
459 c = &intersection.reg_class[i];
460 p2p_dbg(p2p, "reg_class %u", c->reg_class);
461 wpa_hexdump(MSG_DEBUG, "P2P: channels",
462 c->channel, c->channels);
465 if (!p2p_channels_includes(&intersection, p2p->op_reg_class,
467 if (dev->flags & P2P_DEV_FORCE_FREQ) {
468 *status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
469 p2p_dbg(p2p, "Peer does not support the forced channel");
473 p2p_dbg(p2p, "Selected operating channel (op_class %u channel %u) not acceptable to the peer",
474 p2p->op_reg_class, p2p->op_channel);
475 p2p_reselect_channel(p2p, &intersection);
476 } else if (!(dev->flags & P2P_DEV_FORCE_FREQ) &&
477 !p2p->cfg->cfg_op_channel) {
478 p2p_dbg(p2p, "Try to optimize channel selection with peer information received; previously selected op_class %u channel %u",
479 p2p->op_reg_class, p2p->op_channel);
480 p2p_reselect_channel(p2p, &intersection);
483 if (!p2p->ssid_set) {
484 p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
492 void p2p_process_go_neg_req(struct p2p_data *p2p, const u8 *sa,
493 const u8 *data, size_t len, int rx_freq)
495 struct p2p_device *dev = NULL;
497 struct p2p_message msg;
498 u8 status = P2P_SC_FAIL_INVALID_PARAMS;
502 p2p_dbg(p2p, "Received GO Negotiation Request from " MACSTR "(freq=%d)",
503 MAC2STR(sa), rx_freq);
505 if (p2p_parse(data, len, &msg))
508 if (!msg.capability) {
509 p2p_dbg(p2p, "Mandatory Capability attribute missing from GO Negotiation Request");
510 #ifdef CONFIG_P2P_STRICT
512 #endif /* CONFIG_P2P_STRICT */
516 tie_breaker = *msg.go_intent & 0x01;
518 p2p_dbg(p2p, "Mandatory GO Intent attribute missing from GO Negotiation Request");
519 #ifdef CONFIG_P2P_STRICT
521 #endif /* CONFIG_P2P_STRICT */
524 if (!msg.config_timeout) {
525 p2p_dbg(p2p, "Mandatory Configuration Timeout attribute missing from GO Negotiation Request");
526 #ifdef CONFIG_P2P_STRICT
528 #endif /* CONFIG_P2P_STRICT */
531 if (!msg.listen_channel) {
532 p2p_dbg(p2p, "No Listen Channel attribute received");
535 if (!msg.operating_channel) {
536 p2p_dbg(p2p, "No Operating Channel attribute received");
539 if (!msg.channel_list) {
540 p2p_dbg(p2p, "No Channel List attribute received");
543 if (!msg.intended_addr) {
544 p2p_dbg(p2p, "No Intended P2P Interface Address attribute received");
547 if (!msg.p2p_device_info) {
548 p2p_dbg(p2p, "No P2P Device Info attribute received");
552 if (os_memcmp(msg.p2p_device_addr, sa, ETH_ALEN) != 0) {
553 p2p_dbg(p2p, "Unexpected GO Negotiation Request SA=" MACSTR
554 " != dev_addr=" MACSTR,
555 MAC2STR(sa), MAC2STR(msg.p2p_device_addr));
559 dev = p2p_get_device(p2p, sa);
561 if (msg.status && *msg.status) {
562 p2p_dbg(p2p, "Unexpected Status attribute (%d) in GO Negotiation Request",
568 dev = p2p_add_dev_from_go_neg_req(p2p, sa, &msg);
569 else if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
570 p2p_add_dev_info(p2p, sa, dev, &msg);
571 if (dev && dev->flags & P2P_DEV_USER_REJECTED) {
572 p2p_dbg(p2p, "User has rejected this peer");
573 status = P2P_SC_FAIL_REJECTED_BY_USER;
574 } else if (dev == NULL || dev->wps_method == WPS_NOT_READY) {
575 p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR,
577 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
578 p2p->cfg->go_neg_req_rx(p2p->cfg->cb_ctx, sa,
579 msg.dev_password_id);
580 } else if (p2p->go_neg_peer && p2p->go_neg_peer != dev) {
581 p2p_dbg(p2p, "Already in Group Formation with another peer");
582 status = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
586 if (!p2p->go_neg_peer) {
587 p2p_dbg(p2p, "Starting GO Negotiation with previously authorized peer");
588 if (!(dev->flags & P2P_DEV_FORCE_FREQ)) {
589 p2p_dbg(p2p, "Use default channel settings");
590 p2p->op_reg_class = p2p->cfg->op_reg_class;
591 p2p->op_channel = p2p->cfg->op_channel;
592 os_memcpy(&p2p->channels, &p2p->cfg->channels,
593 sizeof(struct p2p_channels));
595 p2p_dbg(p2p, "Use previously configured forced channel settings");
599 dev->flags &= ~P2P_DEV_NOT_YET_READY;
601 if (!msg.go_intent) {
602 p2p_dbg(p2p, "No GO Intent attribute received");
605 if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
606 p2p_dbg(p2p, "Invalid GO Intent value (%u) received",
607 *msg.go_intent >> 1);
611 if (dev->go_neg_req_sent &&
612 os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) > 0) {
613 p2p_dbg(p2p, "Do not reply since peer has higher address and GO Neg Request already sent");
614 p2p_parse_free(&msg);
618 go = p2p_go_det(p2p->go_intent, *msg.go_intent);
620 p2p_dbg(p2p, "Incompatible GO Intent");
621 status = P2P_SC_FAIL_BOTH_GO_INTENT_15;
625 if (p2p_peer_channels(p2p, dev, msg.channel_list,
626 msg.channel_list_len) < 0) {
627 p2p_dbg(p2p, "No common channels found");
628 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
632 switch (msg.dev_password_id) {
633 case DEV_PW_REGISTRAR_SPECIFIED:
634 p2p_dbg(p2p, "PIN from peer Display");
635 if (dev->wps_method != WPS_PIN_KEYPAD) {
636 p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
637 p2p_wps_method_str(dev->wps_method));
638 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
642 case DEV_PW_USER_SPECIFIED:
643 p2p_dbg(p2p, "Peer entered PIN on Keypad");
644 if (dev->wps_method != WPS_PIN_DISPLAY) {
645 p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
646 p2p_wps_method_str(dev->wps_method));
647 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
651 case DEV_PW_PUSHBUTTON:
652 p2p_dbg(p2p, "Peer using pushbutton");
653 if (dev->wps_method != WPS_PBC) {
654 p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
655 p2p_wps_method_str(dev->wps_method));
656 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
661 p2p_dbg(p2p, "Unsupported Device Password ID %d",
662 msg.dev_password_id);
663 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
667 if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
670 dev->go_state = go ? LOCAL_GO : REMOTE_GO;
671 dev->oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
672 msg.operating_channel[4]);
673 p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
676 if (msg.config_timeout) {
677 dev->go_timeout = msg.config_timeout[0];
678 dev->client_timeout = msg.config_timeout[1];
681 p2p_dbg(p2p, "GO Negotiation with " MACSTR, MAC2STR(sa));
682 if (p2p->state != P2P_IDLE)
683 p2p_stop_find_for_freq(p2p, rx_freq);
684 p2p_set_state(p2p, P2P_GO_NEG);
685 p2p_clear_timeout(p2p);
686 dev->dialog_token = msg.dialog_token;
687 os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
688 p2p->go_neg_peer = dev;
689 status = P2P_SC_SUCCESS;
694 dev->status = status;
695 resp = p2p_build_go_neg_resp(p2p, dev, msg.dialog_token, status,
697 p2p_parse_free(&msg);
700 p2p_dbg(p2p, "Sending GO Negotiation Response");
704 freq = p2p_channel_to_freq(p2p->cfg->reg_class,
707 p2p_dbg(p2p, "Unknown regulatory class/channel");
711 if (status == P2P_SC_SUCCESS) {
712 p2p->pending_action_state = P2P_PENDING_GO_NEG_RESPONSE;
713 dev->flags |= P2P_DEV_WAIT_GO_NEG_CONFIRM;
714 if (os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) < 0) {
716 * Peer has smaller address, so the GO Negotiation
717 * Response from us is expected to complete
718 * negotiation. Ignore a GO Negotiation Response from
719 * the peer if it happens to be received after this
720 * point due to a race condition in GO Negotiation
721 * Request transmission and processing.
723 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
726 p2p->pending_action_state =
727 P2P_PENDING_GO_NEG_RESPONSE_FAILURE;
728 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
730 wpabuf_head(resp), wpabuf_len(resp), 500) < 0) {
731 p2p_dbg(p2p, "Failed to send Action frame");
738 static struct wpabuf * p2p_build_go_neg_conf(struct p2p_data *p2p,
739 struct p2p_device *peer,
740 u8 dialog_token, u8 status,
741 const u8 *resp_chan, int go)
745 struct p2p_channels res;
749 p2p_dbg(p2p, "Building GO Negotiation Confirm");
751 #ifdef CONFIG_WIFI_DISPLAY
752 if (p2p->wfd_ie_go_neg)
753 extra = wpabuf_len(p2p->wfd_ie_go_neg);
754 #endif /* CONFIG_WIFI_DISPLAY */
756 buf = wpabuf_alloc(1000 + extra);
760 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_CONF, dialog_token);
762 len = p2p_buf_add_ie_hdr(buf);
763 p2p_buf_add_status(buf, status);
765 if (peer->go_state == LOCAL_GO) {
766 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
767 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
768 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
770 P2P_GROUP_CAPAB_PERSISTENT_RECONN;
772 if (p2p->cross_connect)
773 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
774 if (p2p->cfg->p2p_intra_bss)
775 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
777 p2p_buf_add_capability(buf, p2p->dev_capab &
778 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
780 if (go || resp_chan == NULL)
781 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
785 p2p_buf_add_operating_channel(buf, (const char *) resp_chan,
786 resp_chan[3], resp_chan[4]);
787 p2p_channels_intersect(&p2p->channels, &peer->channels, &res);
788 p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
790 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
793 p2p_buf_update_ie_hdr(buf, len);
795 #ifdef CONFIG_WIFI_DISPLAY
796 if (p2p->wfd_ie_go_neg)
797 wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
798 #endif /* CONFIG_WIFI_DISPLAY */
804 void p2p_process_go_neg_resp(struct p2p_data *p2p, const u8 *sa,
805 const u8 *data, size_t len, int rx_freq)
807 struct p2p_device *dev;
810 struct p2p_message msg;
811 u8 status = P2P_SC_SUCCESS;
814 p2p_dbg(p2p, "Received GO Negotiation Response from " MACSTR
815 " (freq=%d)", MAC2STR(sa), rx_freq);
816 dev = p2p_get_device(p2p, sa);
817 if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
818 dev != p2p->go_neg_peer) {
819 p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR,
824 if (p2p_parse(data, len, &msg))
827 if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE)) {
828 p2p_dbg(p2p, "Was not expecting GO Negotiation Response - ignore");
829 p2p_parse_free(&msg);
832 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
834 if (msg.dialog_token != dev->dialog_token) {
835 p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)",
836 msg.dialog_token, dev->dialog_token);
837 p2p_parse_free(&msg);
842 p2p_dbg(p2p, "No Status attribute received");
843 status = P2P_SC_FAIL_INVALID_PARAMS;
847 p2p_dbg(p2p, "GO Negotiation rejected: status %d", *msg.status);
848 dev->go_neg_req_sent = 0;
849 if (*msg.status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
850 p2p_dbg(p2p, "Wait for the peer to become ready for GO Negotiation");
851 dev->flags |= P2P_DEV_NOT_YET_READY;
853 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
854 p2p_set_timeout(p2p, 0, 0);
856 p2p_dbg(p2p, "Stop GO Negotiation attempt");
857 p2p_go_neg_failed(p2p, dev, *msg.status);
859 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
860 p2p_parse_free(&msg);
864 if (!msg.capability) {
865 p2p_dbg(p2p, "Mandatory Capability attribute missing from GO Negotiation Response");
866 #ifdef CONFIG_P2P_STRICT
867 status = P2P_SC_FAIL_INVALID_PARAMS;
869 #endif /* CONFIG_P2P_STRICT */
872 if (!msg.p2p_device_info) {
873 p2p_dbg(p2p, "Mandatory P2P Device Info attribute missing from GO Negotiation Response");
874 #ifdef CONFIG_P2P_STRICT
875 status = P2P_SC_FAIL_INVALID_PARAMS;
877 #endif /* CONFIG_P2P_STRICT */
880 if (!msg.intended_addr) {
881 p2p_dbg(p2p, "No Intended P2P Interface Address attribute received");
882 status = P2P_SC_FAIL_INVALID_PARAMS;
886 if (!msg.go_intent) {
887 p2p_dbg(p2p, "No GO Intent attribute received");
888 status = P2P_SC_FAIL_INVALID_PARAMS;
891 if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
892 p2p_dbg(p2p, "Invalid GO Intent value (%u) received",
893 *msg.go_intent >> 1);
894 status = P2P_SC_FAIL_INVALID_PARAMS;
898 go = p2p_go_det(p2p->go_intent, *msg.go_intent);
900 p2p_dbg(p2p, "Incompatible GO Intent");
901 status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
905 if (!go && msg.group_id) {
906 /* Store SSID for Provisioning step */
907 p2p->ssid_len = msg.group_id_len - ETH_ALEN;
908 os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
910 p2p_dbg(p2p, "Mandatory P2P Group ID attribute missing from GO Negotiation Response");
912 status = P2P_SC_FAIL_INVALID_PARAMS;
916 if (!msg.config_timeout) {
917 p2p_dbg(p2p, "Mandatory Configuration Timeout attribute missing from GO Negotiation Response");
918 #ifdef CONFIG_P2P_STRICT
919 status = P2P_SC_FAIL_INVALID_PARAMS;
921 #endif /* CONFIG_P2P_STRICT */
923 dev->go_timeout = msg.config_timeout[0];
924 dev->client_timeout = msg.config_timeout[1];
927 if (!msg.operating_channel && !go) {
929 * Note: P2P Client may omit Operating Channel attribute to
930 * indicate it does not have a preference.
932 p2p_dbg(p2p, "No Operating Channel attribute received");
933 status = P2P_SC_FAIL_INVALID_PARAMS;
936 if (!msg.channel_list) {
937 p2p_dbg(p2p, "No Channel List attribute received");
938 status = P2P_SC_FAIL_INVALID_PARAMS;
942 if (p2p_peer_channels(p2p, dev, msg.channel_list,
943 msg.channel_list_len) < 0) {
944 p2p_dbg(p2p, "No common channels found");
945 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
949 if (msg.operating_channel) {
950 dev->oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
951 msg.operating_channel[4]);
952 p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
957 switch (msg.dev_password_id) {
958 case DEV_PW_REGISTRAR_SPECIFIED:
959 p2p_dbg(p2p, "PIN from peer Display");
960 if (dev->wps_method != WPS_PIN_KEYPAD) {
961 p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
962 p2p_wps_method_str(dev->wps_method));
963 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
967 case DEV_PW_USER_SPECIFIED:
968 p2p_dbg(p2p, "Peer entered PIN on Keypad");
969 if (dev->wps_method != WPS_PIN_DISPLAY) {
970 p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
971 p2p_wps_method_str(dev->wps_method));
972 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
976 case DEV_PW_PUSHBUTTON:
977 p2p_dbg(p2p, "Peer using pushbutton");
978 if (dev->wps_method != WPS_PBC) {
979 p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
980 p2p_wps_method_str(dev->wps_method));
981 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
986 p2p_dbg(p2p, "Unsupported Device Password ID %d",
987 msg.dev_password_id);
988 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
992 if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
995 p2p_set_state(p2p, P2P_GO_NEG);
996 p2p_clear_timeout(p2p);
998 p2p_dbg(p2p, "GO Negotiation with " MACSTR, MAC2STR(sa));
999 os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
1002 conf = p2p_build_go_neg_conf(p2p, dev, msg.dialog_token, status,
1003 msg.operating_channel, go);
1004 p2p_parse_free(&msg);
1007 p2p_dbg(p2p, "Sending GO Negotiation Confirm");
1008 if (status == P2P_SC_SUCCESS) {
1009 p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
1010 dev->go_state = go ? LOCAL_GO : REMOTE_GO;
1012 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1016 freq = dev->listen_freq;
1017 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr, sa,
1018 wpabuf_head(conf), wpabuf_len(conf), 0) < 0) {
1019 p2p_dbg(p2p, "Failed to send Action frame");
1020 p2p_go_neg_failed(p2p, dev, -1);
1023 if (status != P2P_SC_SUCCESS) {
1024 p2p_dbg(p2p, "GO Negotiation failed");
1025 p2p_go_neg_failed(p2p, dev, status);
1030 void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa,
1031 const u8 *data, size_t len)
1033 struct p2p_device *dev;
1034 struct p2p_message msg;
1036 p2p_dbg(p2p, "Received GO Negotiation Confirm from " MACSTR,
1038 dev = p2p_get_device(p2p, sa);
1039 if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
1040 dev != p2p->go_neg_peer) {
1041 p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR,
1046 if (p2p->pending_action_state == P2P_PENDING_GO_NEG_RESPONSE) {
1047 p2p_dbg(p2p, "Stopped waiting for TX status on GO Negotiation Response since we already received Confirmation");
1048 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1051 if (p2p_parse(data, len, &msg))
1054 if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
1055 p2p_dbg(p2p, "Was not expecting GO Negotiation Confirm - ignore");
1058 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1060 if (msg.dialog_token != dev->dialog_token) {
1061 p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)",
1062 msg.dialog_token, dev->dialog_token);
1063 p2p_parse_free(&msg);
1068 p2p_dbg(p2p, "No Status attribute received");
1069 p2p_parse_free(&msg);
1073 p2p_dbg(p2p, "GO Negotiation rejected: status %d", *msg.status);
1074 p2p_go_neg_failed(p2p, dev, *msg.status);
1075 p2p_parse_free(&msg);
1079 if (dev->go_state == REMOTE_GO && msg.group_id) {
1080 /* Store SSID for Provisioning step */
1081 p2p->ssid_len = msg.group_id_len - ETH_ALEN;
1082 os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
1083 } else if (dev->go_state == REMOTE_GO) {
1084 p2p_dbg(p2p, "Mandatory P2P Group ID attribute missing from GO Negotiation Confirmation");
1086 p2p_go_neg_failed(p2p, dev, P2P_SC_FAIL_INVALID_PARAMS);
1087 p2p_parse_free(&msg);
1091 if (!msg.operating_channel) {
1092 p2p_dbg(p2p, "Mandatory Operating Channel attribute missing from GO Negotiation Confirmation");
1093 #ifdef CONFIG_P2P_STRICT
1094 p2p_parse_free(&msg);
1096 #endif /* CONFIG_P2P_STRICT */
1099 if (!msg.channel_list) {
1100 p2p_dbg(p2p, "Mandatory Operating Channel attribute missing from GO Negotiation Confirmation");
1101 #ifdef CONFIG_P2P_STRICT
1102 p2p_parse_free(&msg);
1104 #endif /* CONFIG_P2P_STRICT */
1107 p2p_parse_free(&msg);
1109 if (dev->go_state == UNKNOWN_GO) {
1111 * This should not happen since GO negotiation has already
1114 p2p_dbg(p2p, "Unexpected GO Neg state - do not know which end becomes GO");
1119 * The peer could have missed our ctrl::ack frame for GO Negotiation
1120 * Confirm and continue retransmitting the frame. To reduce the
1121 * likelihood of the peer not getting successful TX status for the
1122 * GO Negotiation Confirm frame, wait a short time here before starting
1123 * the group so that we will remain on the current channel to
1124 * acknowledge any possible retransmission from the peer.
1126 p2p_dbg(p2p, "20 ms wait on current channel before starting group");
1129 p2p_go_complete(p2p, dev);