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 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
53 "P2P: Mismatching country (ours=%c%c peer's=%c%c)",
54 p2p->cfg->country[0], p2p->cfg->country[1],
60 while (pos + 2 < end) {
61 struct p2p_reg_class *cl = &ch->reg_class[ch->reg_classes];
62 cl->reg_class = *pos++;
63 if (pos + 1 + pos[0] > end) {
64 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
65 "P2P: Invalid peer Channel List");
69 cl->channels = channels > P2P_MAX_REG_CLASS_CHANNELS ?
70 P2P_MAX_REG_CLASS_CHANNELS : channels;
71 os_memcpy(cl->channel, pos, cl->channels);
74 if (ch->reg_classes == P2P_MAX_REG_CLASSES)
78 p2p_channels_intersect(own, &dev->channels, &intersection);
79 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Own reg_classes %d "
80 "peer reg_classes %d intersection reg_classes %d",
81 (int) own->reg_classes,
82 (int) dev->channels.reg_classes,
83 (int) intersection.reg_classes);
84 if (intersection.reg_classes == 0) {
85 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
86 "P2P: No common channels found");
93 static int p2p_peer_channels(struct p2p_data *p2p, struct p2p_device *dev,
94 const u8 *channel_list, size_t channel_list_len)
96 return p2p_peer_channels_check(p2p, &p2p->channels, dev,
97 channel_list, channel_list_len);
101 u16 p2p_wps_method_pw_id(enum p2p_wps_method wps_method)
103 switch (wps_method) {
104 case WPS_PIN_DISPLAY:
105 return DEV_PW_REGISTRAR_SPECIFIED;
107 return DEV_PW_USER_SPECIFIED;
109 return DEV_PW_PUSHBUTTON;
111 return DEV_PW_DEFAULT;
116 static const char * p2p_wps_method_str(enum p2p_wps_method wps_method)
118 switch (wps_method) {
119 case WPS_PIN_DISPLAY:
131 static struct wpabuf * p2p_build_go_neg_req(struct p2p_data *p2p,
132 struct p2p_device *peer)
139 #ifdef CONFIG_WIFI_DISPLAY
140 if (p2p->wfd_ie_go_neg)
141 extra = wpabuf_len(p2p->wfd_ie_go_neg);
142 #endif /* CONFIG_WIFI_DISPLAY */
144 buf = wpabuf_alloc(1000 + extra);
148 peer->dialog_token++;
149 if (peer->dialog_token == 0)
150 peer->dialog_token = 1;
151 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_REQ, peer->dialog_token);
153 len = p2p_buf_add_ie_hdr(buf);
155 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
156 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
157 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
158 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_RECONN;
160 if (p2p->cross_connect)
161 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
162 if (p2p->cfg->p2p_intra_bss)
163 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
164 p2p_buf_add_capability(buf, p2p->dev_capab &
165 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
167 p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) |
168 p2p->next_tie_breaker);
169 p2p->next_tie_breaker = !p2p->next_tie_breaker;
170 p2p_buf_add_config_timeout(buf, p2p->go_timeout, p2p->client_timeout);
171 p2p_buf_add_listen_channel(buf, p2p->cfg->country, p2p->cfg->reg_class,
173 if (p2p->ext_listen_interval)
174 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
175 p2p->ext_listen_interval);
176 p2p_buf_add_intended_addr(buf, p2p->intended_addr);
177 p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
178 p2p_buf_add_device_info(buf, p2p, peer);
179 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
180 p2p->op_reg_class, p2p->op_channel);
181 p2p_buf_update_ie_hdr(buf, len);
183 /* WPS IE with Device Password ID attribute */
184 p2p_build_wps_ie(p2p, buf, p2p_wps_method_pw_id(peer->wps_method), 0);
186 #ifdef CONFIG_WIFI_DISPLAY
187 if (p2p->wfd_ie_go_neg)
188 wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
189 #endif /* CONFIG_WIFI_DISPLAY */
195 int p2p_connect_send(struct p2p_data *p2p, struct p2p_device *dev)
200 if (dev->flags & P2P_DEV_PD_BEFORE_GO_NEG) {
202 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
203 "P2P: Use PD-before-GO-Neg workaround for " MACSTR,
204 MAC2STR(dev->info.p2p_device_addr));
205 if (dev->wps_method == WPS_PIN_DISPLAY)
206 config_method = WPS_CONFIG_KEYPAD;
207 else if (dev->wps_method == WPS_PIN_KEYPAD)
208 config_method = WPS_CONFIG_DISPLAY;
209 else if (dev->wps_method == WPS_PBC)
210 config_method = WPS_CONFIG_PUSHBUTTON;
213 return p2p_prov_disc_req(p2p, dev->info.p2p_device_addr,
214 config_method, 0, 0, 1);
217 freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
219 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
220 "P2P: No Listen/Operating frequency known for the "
221 "peer " MACSTR " to send GO Negotiation Request",
222 MAC2STR(dev->info.p2p_device_addr));
226 req = p2p_build_go_neg_req(p2p, dev);
229 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
230 "P2P: Sending GO Negotiation Request");
231 p2p_set_state(p2p, P2P_CONNECT);
232 p2p->pending_action_state = P2P_PENDING_GO_NEG_REQUEST;
233 p2p->go_neg_peer = dev;
234 dev->flags |= P2P_DEV_WAIT_GO_NEG_RESPONSE;
236 if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
237 p2p->cfg->dev_addr, dev->info.p2p_device_addr,
238 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
239 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
240 "P2P: Failed to send Action frame");
241 /* Use P2P find to recover and retry */
242 p2p_set_timeout(p2p, 0, 0);
244 dev->go_neg_req_sent++;
252 static struct wpabuf * p2p_build_go_neg_resp(struct p2p_data *p2p,
253 struct p2p_device *peer,
254 u8 dialog_token, u8 status,
262 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
263 "P2P: Building GO Negotiation Response");
265 #ifdef CONFIG_WIFI_DISPLAY
266 if (p2p->wfd_ie_go_neg)
267 extra = wpabuf_len(p2p->wfd_ie_go_neg);
268 #endif /* CONFIG_WIFI_DISPLAY */
270 buf = wpabuf_alloc(1000 + extra);
274 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_RESP, dialog_token);
276 len = p2p_buf_add_ie_hdr(buf);
277 p2p_buf_add_status(buf, status);
279 if (peer && peer->go_state == LOCAL_GO) {
280 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
281 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
282 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
284 P2P_GROUP_CAPAB_PERSISTENT_RECONN;
286 if (p2p->cross_connect)
287 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
288 if (p2p->cfg->p2p_intra_bss)
289 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
291 p2p_buf_add_capability(buf, p2p->dev_capab &
292 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
294 p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | tie_breaker);
295 p2p_buf_add_config_timeout(buf, p2p->go_timeout, p2p->client_timeout);
296 if (peer && peer->go_state == REMOTE_GO) {
297 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Omit Operating "
298 "Channel attribute");
300 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
304 p2p_buf_add_intended_addr(buf, p2p->intended_addr);
305 if (status || peer == NULL) {
306 p2p_buf_add_channel_list(buf, p2p->cfg->country,
308 } else if (peer->go_state == REMOTE_GO) {
309 p2p_buf_add_channel_list(buf, p2p->cfg->country,
312 struct p2p_channels res;
313 p2p_channels_intersect(&p2p->channels, &peer->channels,
315 p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
317 p2p_buf_add_device_info(buf, p2p, peer);
318 if (peer && peer->go_state == LOCAL_GO) {
319 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
322 p2p_buf_update_ie_hdr(buf, len);
324 /* WPS IE with Device Password ID attribute */
325 p2p_build_wps_ie(p2p, buf,
326 p2p_wps_method_pw_id(peer ? peer->wps_method :
329 #ifdef CONFIG_WIFI_DISPLAY
330 if (p2p->wfd_ie_go_neg)
331 wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
332 #endif /* CONFIG_WIFI_DISPLAY */
339 void p2p_reselect_channel(struct p2p_data *p2p,
340 struct p2p_channels *intersection)
342 struct p2p_reg_class *cl;
344 u8 op_reg_class, op_channel;
347 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Selected operating "
348 "channel (reg_class %u channel %u) not acceptable to the "
349 "peer", p2p->op_reg_class, p2p->op_channel);
351 /* First, try to pick the best channel from another band */
352 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->op_reg_class,
354 if (freq >= 2400 && freq < 2500 && p2p->best_freq_5 > 0 &&
355 p2p_freq_to_channel(p2p->cfg->country, p2p->best_freq_5,
356 &op_reg_class, &op_channel) == 0 &&
357 p2p_channels_includes(intersection, op_reg_class, op_channel)) {
358 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick best 5 GHz "
359 "channel (reg_class %u channel %u) from intersection",
360 op_reg_class, op_channel);
361 p2p->op_reg_class = op_reg_class;
362 p2p->op_channel = op_channel;
366 if (freq >= 4900 && freq < 6000 && p2p->best_freq_24 > 0 &&
367 p2p_freq_to_channel(p2p->cfg->country, p2p->best_freq_24,
368 &op_reg_class, &op_channel) == 0 &&
369 p2p_channels_includes(intersection, op_reg_class, op_channel)) {
370 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick best 2.4 GHz "
371 "channel (reg_class %u channel %u) from intersection",
372 op_reg_class, op_channel);
373 p2p->op_reg_class = op_reg_class;
374 p2p->op_channel = op_channel;
378 /* Select channel with highest preference if the peer supports it */
379 for (i = 0; p2p->cfg->pref_chan && i < p2p->cfg->num_pref_chan; i++) {
380 if (p2p_channels_includes(intersection,
381 p2p->cfg->pref_chan[i].op_class,
382 p2p->cfg->pref_chan[i].chan)) {
383 p2p->op_reg_class = p2p->cfg->pref_chan[i].op_class;
384 p2p->op_channel = p2p->cfg->pref_chan[i].chan;
385 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick "
386 "highest preferred chnnel (op_class %u "
387 "channel %u) from intersection",
388 p2p->op_reg_class, p2p->op_channel);
393 /* Try a channel where we might be able to use HT40 */
394 for (i = 0; i < intersection->reg_classes; i++) {
395 struct p2p_reg_class *c = &intersection->reg_class[i];
396 if (c->reg_class == 116 || c->reg_class == 117 ||
397 c->reg_class == 126 || c->reg_class == 127) {
398 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
399 "P2P: Pick possible HT40 channel (reg_class "
400 "%u channel %u) from intersection",
401 c->reg_class, c->channel[0]);
402 p2p->op_reg_class = c->reg_class;
403 p2p->op_channel = c->channel[0];
409 * Fall back to whatever is included in the channel intersection since
410 * no better options seems to be available.
412 cl = &intersection->reg_class[0];
413 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick another channel "
414 "(reg_class %u channel %u) from intersection",
415 cl->reg_class, cl->channel[0]);
416 p2p->op_reg_class = cl->reg_class;
417 p2p->op_channel = cl->channel[0];
421 static int p2p_go_select_channel(struct p2p_data *p2p, struct p2p_device *dev,
424 struct p2p_channels intersection;
427 p2p_channels_intersect(&p2p->channels, &dev->channels, &intersection);
428 if (intersection.reg_classes == 0 ||
429 intersection.reg_class[0].channels == 0) {
430 *status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
431 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
432 "P2P: No common channels found");
436 for (i = 0; i < intersection.reg_classes; i++) {
437 struct p2p_reg_class *c;
438 c = &intersection.reg_class[i];
439 wpa_printf(MSG_DEBUG, "P2P: reg_class %u", c->reg_class);
440 wpa_hexdump(MSG_DEBUG, "P2P: channels",
441 c->channel, c->channels);
444 if (!p2p_channels_includes(&intersection, p2p->op_reg_class,
446 if (dev->flags & P2P_DEV_FORCE_FREQ) {
447 *status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
448 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer does "
449 "not support the forced channel");
452 p2p_reselect_channel(p2p, &intersection);
455 if (!p2p->ssid_set) {
456 p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
464 void p2p_process_go_neg_req(struct p2p_data *p2p, const u8 *sa,
465 const u8 *data, size_t len, int rx_freq)
467 struct p2p_device *dev = NULL;
469 struct p2p_message msg;
470 u8 status = P2P_SC_FAIL_INVALID_PARAMS;
474 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
475 "P2P: Received GO Negotiation Request from " MACSTR
476 "(freq=%d)", MAC2STR(sa), rx_freq);
478 if (p2p_parse(data, len, &msg))
481 if (!msg.capability) {
482 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
483 "P2P: Mandatory Capability attribute missing from GO "
484 "Negotiation Request");
485 #ifdef CONFIG_P2P_STRICT
487 #endif /* CONFIG_P2P_STRICT */
491 tie_breaker = *msg.go_intent & 0x01;
493 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
494 "P2P: Mandatory GO Intent attribute missing from GO "
495 "Negotiation Request");
496 #ifdef CONFIG_P2P_STRICT
498 #endif /* CONFIG_P2P_STRICT */
501 if (!msg.config_timeout) {
502 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
503 "P2P: Mandatory Configuration Timeout attribute "
504 "missing from GO Negotiation Request");
505 #ifdef CONFIG_P2P_STRICT
507 #endif /* CONFIG_P2P_STRICT */
510 if (!msg.listen_channel) {
511 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
512 "P2P: No Listen Channel attribute received");
515 if (!msg.operating_channel) {
516 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
517 "P2P: No Operating Channel attribute received");
520 if (!msg.channel_list) {
521 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
522 "P2P: No Channel List attribute received");
525 if (!msg.intended_addr) {
526 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
527 "P2P: No Intended P2P Interface Address attribute "
531 if (!msg.p2p_device_info) {
532 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
533 "P2P: No P2P Device Info attribute received");
537 if (os_memcmp(msg.p2p_device_addr, sa, ETH_ALEN) != 0) {
538 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
539 "P2P: Unexpected GO Negotiation Request SA=" MACSTR
540 " != dev_addr=" MACSTR,
541 MAC2STR(sa), MAC2STR(msg.p2p_device_addr));
545 dev = p2p_get_device(p2p, sa);
547 if (msg.status && *msg.status) {
548 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
549 "P2P: Unexpected Status attribute (%d) in GO "
550 "Negotiation Request", *msg.status);
555 dev = p2p_add_dev_from_go_neg_req(p2p, sa, &msg);
556 else if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
557 p2p_add_dev_info(p2p, sa, dev, &msg);
558 if (dev && dev->flags & P2P_DEV_USER_REJECTED) {
559 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
560 "P2P: User has rejected this peer");
561 status = P2P_SC_FAIL_REJECTED_BY_USER;
562 } else if (dev == NULL || dev->wps_method == WPS_NOT_READY) {
563 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
564 "P2P: Not ready for GO negotiation with " MACSTR,
566 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
568 dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
569 p2p->cfg->go_neg_req_rx(p2p->cfg->cb_ctx, sa,
570 msg.dev_password_id);
571 } else if (p2p->go_neg_peer && p2p->go_neg_peer != dev) {
572 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
573 "P2P: Already in Group Formation with another peer");
574 status = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
578 if (!p2p->go_neg_peer) {
579 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting "
580 "GO Negotiation with previously authorized "
582 if (!(dev->flags & P2P_DEV_FORCE_FREQ)) {
583 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
584 "P2P: Use default channel settings");
585 p2p->op_reg_class = p2p->cfg->op_reg_class;
586 p2p->op_channel = p2p->cfg->op_channel;
587 os_memcpy(&p2p->channels, &p2p->cfg->channels,
588 sizeof(struct p2p_channels));
590 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
591 "P2P: Use previously configured "
592 "forced channel settings");
596 dev->flags &= ~P2P_DEV_NOT_YET_READY;
598 if (!msg.go_intent) {
599 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
600 "P2P: No GO Intent attribute received");
603 if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
604 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
605 "P2P: Invalid GO Intent value (%u) received",
606 *msg.go_intent >> 1);
610 if (dev->go_neg_req_sent &&
611 os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) > 0) {
612 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
613 "P2P: Do not reply since peer has higher "
614 "address and GO Neg Request already sent");
615 p2p_parse_free(&msg);
619 go = p2p_go_det(p2p->go_intent, *msg.go_intent);
621 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
622 "P2P: Incompatible GO Intent");
623 status = P2P_SC_FAIL_BOTH_GO_INTENT_15;
627 if (p2p_peer_channels(p2p, dev, msg.channel_list,
628 msg.channel_list_len) < 0) {
629 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
630 "P2P: No common channels found");
631 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
635 switch (msg.dev_password_id) {
636 case DEV_PW_REGISTRAR_SPECIFIED:
637 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
638 "P2P: PIN from peer Display");
639 if (dev->wps_method != WPS_PIN_KEYPAD) {
640 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
641 "P2P: We have wps_method=%s -> "
643 p2p_wps_method_str(dev->wps_method));
644 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
648 case DEV_PW_USER_SPECIFIED:
649 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
650 "P2P: Peer entered PIN on Keypad");
651 if (dev->wps_method != WPS_PIN_DISPLAY) {
652 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
653 "P2P: We have wps_method=%s -> "
655 p2p_wps_method_str(dev->wps_method));
656 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
660 case DEV_PW_PUSHBUTTON:
661 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
662 "P2P: Peer using pushbutton");
663 if (dev->wps_method != WPS_PBC) {
664 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
665 "P2P: We have wps_method=%s -> "
667 p2p_wps_method_str(dev->wps_method));
668 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
673 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
674 "P2P: Unsupported Device Password ID %d",
675 msg.dev_password_id);
676 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
680 if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
683 dev->go_state = go ? LOCAL_GO : REMOTE_GO;
684 dev->oper_freq = p2p_channel_to_freq((const char *)
685 msg.operating_channel,
686 msg.operating_channel[3],
687 msg.operating_channel[4]);
688 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
689 "channel preference: %d MHz", dev->oper_freq);
691 if (msg.config_timeout) {
692 dev->go_timeout = msg.config_timeout[0];
693 dev->client_timeout = msg.config_timeout[1];
696 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
697 "P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
698 if (p2p->state != P2P_IDLE)
699 p2p_stop_find_for_freq(p2p, rx_freq);
700 p2p_set_state(p2p, P2P_GO_NEG);
701 p2p_clear_timeout(p2p);
702 dev->dialog_token = msg.dialog_token;
703 os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
704 p2p->go_neg_peer = dev;
705 status = P2P_SC_SUCCESS;
710 dev->status = status;
711 resp = p2p_build_go_neg_resp(p2p, dev, msg.dialog_token, status,
713 p2p_parse_free(&msg);
716 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
717 "P2P: Sending GO Negotiation Response");
721 freq = p2p_channel_to_freq(p2p->cfg->country,
725 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
726 "P2P: Unknown regulatory class/channel");
730 if (status == P2P_SC_SUCCESS) {
731 p2p->pending_action_state = P2P_PENDING_GO_NEG_RESPONSE;
732 dev->flags |= P2P_DEV_WAIT_GO_NEG_CONFIRM;
733 if (os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) < 0) {
735 * Peer has smaller address, so the GO Negotiation
736 * Response from us is expected to complete
737 * negotiation. Ignore a GO Negotiation Response from
738 * the peer if it happens to be received after this
739 * point due to a race condition in GO Negotiation
740 * Request transmission and processing.
742 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
745 p2p->pending_action_state =
746 P2P_PENDING_GO_NEG_RESPONSE_FAILURE;
747 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
749 wpabuf_head(resp), wpabuf_len(resp), 250) < 0) {
750 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
751 "P2P: Failed to send Action frame");
758 static struct wpabuf * p2p_build_go_neg_conf(struct p2p_data *p2p,
759 struct p2p_device *peer,
760 u8 dialog_token, u8 status,
761 const u8 *resp_chan, int go)
765 struct p2p_channels res;
769 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
770 "P2P: Building GO Negotiation Confirm");
772 #ifdef CONFIG_WIFI_DISPLAY
773 if (p2p->wfd_ie_go_neg)
774 extra = wpabuf_len(p2p->wfd_ie_go_neg);
775 #endif /* CONFIG_WIFI_DISPLAY */
777 buf = wpabuf_alloc(1000 + extra);
781 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_CONF, dialog_token);
783 len = p2p_buf_add_ie_hdr(buf);
784 p2p_buf_add_status(buf, status);
786 if (peer->go_state == LOCAL_GO) {
787 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
788 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
789 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
791 P2P_GROUP_CAPAB_PERSISTENT_RECONN;
793 if (p2p->cross_connect)
794 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
795 if (p2p->cfg->p2p_intra_bss)
796 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
798 p2p_buf_add_capability(buf, p2p->dev_capab &
799 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
801 if (go || resp_chan == NULL)
802 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
806 p2p_buf_add_operating_channel(buf, (const char *) resp_chan,
807 resp_chan[3], resp_chan[4]);
808 p2p_channels_intersect(&p2p->channels, &peer->channels, &res);
809 p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
811 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
814 p2p_buf_update_ie_hdr(buf, len);
816 #ifdef CONFIG_WIFI_DISPLAY
817 if (p2p->wfd_ie_go_neg)
818 wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
819 #endif /* CONFIG_WIFI_DISPLAY */
825 void p2p_process_go_neg_resp(struct p2p_data *p2p, const u8 *sa,
826 const u8 *data, size_t len, int rx_freq)
828 struct p2p_device *dev;
831 struct p2p_message msg;
832 u8 status = P2P_SC_SUCCESS;
835 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
836 "P2P: Received GO Negotiation Response from " MACSTR
837 " (freq=%d)", MAC2STR(sa), rx_freq);
838 dev = p2p_get_device(p2p, sa);
839 if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
840 dev != p2p->go_neg_peer) {
841 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
842 "P2P: Not ready for GO negotiation with " MACSTR,
847 if (p2p_parse(data, len, &msg))
850 if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE)) {
851 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
852 "P2P: Was not expecting GO Negotiation Response - "
854 p2p_parse_free(&msg);
857 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
859 if (msg.dialog_token != dev->dialog_token) {
860 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
861 "P2P: Unexpected Dialog Token %u (expected %u)",
862 msg.dialog_token, dev->dialog_token);
863 p2p_parse_free(&msg);
868 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
869 "P2P: No Status attribute received");
870 status = P2P_SC_FAIL_INVALID_PARAMS;
874 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
875 "P2P: GO Negotiation rejected: status %d",
877 dev->go_neg_req_sent = 0;
878 if (*msg.status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
879 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
880 "P2P: Wait for the peer to become ready for "
882 dev->flags |= P2P_DEV_NOT_YET_READY;
884 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
885 p2p_set_timeout(p2p, 0, 0);
887 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
888 "P2P: Stop GO Negotiation attempt");
889 p2p_go_neg_failed(p2p, dev, *msg.status);
891 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
892 p2p_parse_free(&msg);
896 if (!msg.capability) {
897 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
898 "P2P: Mandatory Capability attribute missing from GO "
899 "Negotiation Response");
900 #ifdef CONFIG_P2P_STRICT
901 status = P2P_SC_FAIL_INVALID_PARAMS;
903 #endif /* CONFIG_P2P_STRICT */
906 if (!msg.p2p_device_info) {
907 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
908 "P2P: Mandatory P2P Device Info attribute missing "
909 "from GO Negotiation Response");
910 #ifdef CONFIG_P2P_STRICT
911 status = P2P_SC_FAIL_INVALID_PARAMS;
913 #endif /* CONFIG_P2P_STRICT */
916 if (!msg.intended_addr) {
917 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
918 "P2P: No Intended P2P Interface Address attribute "
920 status = P2P_SC_FAIL_INVALID_PARAMS;
924 if (!msg.go_intent) {
925 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
926 "P2P: No GO Intent attribute received");
927 status = P2P_SC_FAIL_INVALID_PARAMS;
930 if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
931 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
932 "P2P: Invalid GO Intent value (%u) received",
933 *msg.go_intent >> 1);
934 status = P2P_SC_FAIL_INVALID_PARAMS;
938 go = p2p_go_det(p2p->go_intent, *msg.go_intent);
940 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
941 "P2P: Incompatible GO Intent");
942 status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
946 if (!go && msg.group_id) {
947 /* Store SSID for Provisioning step */
948 p2p->ssid_len = msg.group_id_len - ETH_ALEN;
949 os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
951 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
952 "P2P: Mandatory P2P Group ID attribute missing from "
953 "GO Negotiation Response");
955 #ifdef CONFIG_P2P_STRICT
956 status = P2P_SC_FAIL_INVALID_PARAMS;
958 #endif /* CONFIG_P2P_STRICT */
961 if (!msg.config_timeout) {
962 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
963 "P2P: Mandatory Configuration Timeout attribute "
964 "missing from GO Negotiation Response");
965 #ifdef CONFIG_P2P_STRICT
966 status = P2P_SC_FAIL_INVALID_PARAMS;
968 #endif /* CONFIG_P2P_STRICT */
970 dev->go_timeout = msg.config_timeout[0];
971 dev->client_timeout = msg.config_timeout[1];
974 if (!msg.operating_channel && !go) {
976 * Note: P2P Client may omit Operating Channel attribute to
977 * indicate it does not have a preference.
979 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
980 "P2P: No Operating Channel attribute received");
981 status = P2P_SC_FAIL_INVALID_PARAMS;
984 if (!msg.channel_list) {
985 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
986 "P2P: No Channel List attribute received");
987 status = P2P_SC_FAIL_INVALID_PARAMS;
991 if (p2p_peer_channels(p2p, dev, msg.channel_list,
992 msg.channel_list_len) < 0) {
993 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
994 "P2P: No common channels found");
995 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
999 if (msg.operating_channel) {
1000 dev->oper_freq = p2p_channel_to_freq((const char *)
1001 msg.operating_channel,
1002 msg.operating_channel[3],
1003 msg.operating_channel[4]);
1004 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
1005 "channel preference: %d MHz", dev->oper_freq);
1009 switch (msg.dev_password_id) {
1010 case DEV_PW_REGISTRAR_SPECIFIED:
1011 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1012 "P2P: PIN from peer Display");
1013 if (dev->wps_method != WPS_PIN_KEYPAD) {
1014 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1015 "P2P: We have wps_method=%s -> "
1017 p2p_wps_method_str(dev->wps_method));
1018 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1022 case DEV_PW_USER_SPECIFIED:
1023 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1024 "P2P: Peer entered PIN on Keypad");
1025 if (dev->wps_method != WPS_PIN_DISPLAY) {
1026 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1027 "P2P: We have wps_method=%s -> "
1029 p2p_wps_method_str(dev->wps_method));
1030 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1034 case DEV_PW_PUSHBUTTON:
1035 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1036 "P2P: Peer using pushbutton");
1037 if (dev->wps_method != WPS_PBC) {
1038 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1039 "P2P: We have wps_method=%s -> "
1041 p2p_wps_method_str(dev->wps_method));
1042 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1047 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1048 "P2P: Unsupported Device Password ID %d",
1049 msg.dev_password_id);
1050 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1054 if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
1057 p2p_set_state(p2p, P2P_GO_NEG);
1058 p2p_clear_timeout(p2p);
1060 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1061 "P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
1062 os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
1065 conf = p2p_build_go_neg_conf(p2p, dev, msg.dialog_token, status,
1066 msg.operating_channel, go);
1067 p2p_parse_free(&msg);
1070 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1071 "P2P: Sending GO Negotiation Confirm");
1072 if (status == P2P_SC_SUCCESS) {
1073 p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
1074 dev->go_state = go ? LOCAL_GO : REMOTE_GO;
1076 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1080 freq = dev->listen_freq;
1081 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr, sa,
1082 wpabuf_head(conf), wpabuf_len(conf), 0) < 0) {
1083 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1084 "P2P: Failed to send Action frame");
1085 p2p_go_neg_failed(p2p, dev, -1);
1091 void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa,
1092 const u8 *data, size_t len)
1094 struct p2p_device *dev;
1095 struct p2p_message msg;
1097 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1098 "P2P: Received GO Negotiation Confirm from " MACSTR,
1100 dev = p2p_get_device(p2p, sa);
1101 if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
1102 dev != p2p->go_neg_peer) {
1103 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1104 "P2P: Not ready for GO negotiation with " MACSTR,
1109 if (p2p->pending_action_state == P2P_PENDING_GO_NEG_RESPONSE) {
1110 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopped waiting "
1111 "for TX status on GO Negotiation Response since we "
1112 "already received Confirmation");
1113 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1116 if (p2p_parse(data, len, &msg))
1119 if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
1120 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1121 "P2P: Was not expecting GO Negotiation Confirm - "
1125 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1127 if (msg.dialog_token != dev->dialog_token) {
1128 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1129 "P2P: Unexpected Dialog Token %u (expected %u)",
1130 msg.dialog_token, dev->dialog_token);
1131 p2p_parse_free(&msg);
1136 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1137 "P2P: No Status attribute received");
1138 p2p_parse_free(&msg);
1142 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1143 "P2P: GO Negotiation rejected: status %d",
1145 p2p_parse_free(&msg);
1149 if (dev->go_state == REMOTE_GO && msg.group_id) {
1150 /* Store SSID for Provisioning step */
1151 p2p->ssid_len = msg.group_id_len - ETH_ALEN;
1152 os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
1153 } else if (dev->go_state == REMOTE_GO) {
1154 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1155 "P2P: Mandatory P2P Group ID attribute missing from "
1156 "GO Negotiation Confirmation");
1158 #ifdef CONFIG_P2P_STRICT
1159 p2p_parse_free(&msg);
1161 #endif /* CONFIG_P2P_STRICT */
1164 if (!msg.operating_channel) {
1165 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1166 "P2P: Mandatory Operating Channel attribute missing "
1167 "from GO Negotiation Confirmation");
1168 #ifdef CONFIG_P2P_STRICT
1169 p2p_parse_free(&msg);
1171 #endif /* CONFIG_P2P_STRICT */
1174 if (!msg.channel_list) {
1175 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1176 "P2P: Mandatory Operating Channel attribute missing "
1177 "from GO Negotiation Confirmation");
1178 #ifdef CONFIG_P2P_STRICT
1179 p2p_parse_free(&msg);
1181 #endif /* CONFIG_P2P_STRICT */
1184 p2p_parse_free(&msg);
1186 if (dev->go_state == UNKNOWN_GO) {
1188 * This should not happen since GO negotiation has already
1191 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1192 "P2P: Unexpected GO Neg state - do not know which end "
1198 * The peer could have missed our ctrl::ack frame for GO Negotiation
1199 * Confirm and continue retransmitting the frame. To reduce the
1200 * likelihood of the peer not getting successful TX status for the
1201 * GO Negotiation Confirm frame, wait a short time here before starting
1202 * the group so that we will remain on the current channel to
1203 * acknowledge any possible retransmission from the peer.
1205 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: 20 ms wait on current "
1206 "channel before starting group");
1209 p2p_go_complete(p2p, dev);