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)
138 buf = wpabuf_alloc(1000);
142 peer->dialog_token++;
143 if (peer->dialog_token == 0)
144 peer->dialog_token = 1;
145 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_REQ, peer->dialog_token);
147 len = p2p_buf_add_ie_hdr(buf);
149 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
150 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
151 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
152 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_RECONN;
154 if (p2p->cross_connect)
155 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
156 if (p2p->cfg->p2p_intra_bss)
157 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
158 p2p_buf_add_capability(buf, p2p->dev_capab, group_capab);
159 p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) |
160 p2p->next_tie_breaker);
161 p2p->next_tie_breaker = !p2p->next_tie_breaker;
162 p2p_buf_add_config_timeout(buf, 100, 20);
163 p2p_buf_add_listen_channel(buf, p2p->cfg->country, p2p->cfg->reg_class,
165 if (p2p->ext_listen_interval)
166 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
167 p2p->ext_listen_interval);
168 p2p_buf_add_intended_addr(buf, p2p->intended_addr);
169 p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
170 p2p_buf_add_device_info(buf, p2p, peer);
171 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
172 p2p->op_reg_class, p2p->op_channel);
173 p2p_buf_update_ie_hdr(buf, len);
175 /* WPS IE with Device Password ID attribute */
176 p2p_build_wps_ie(p2p, buf, p2p_wps_method_pw_id(peer->wps_method), 0);
182 int p2p_connect_send(struct p2p_data *p2p, struct p2p_device *dev)
187 if (dev->flags & P2P_DEV_PD_BEFORE_GO_NEG) {
189 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
190 "P2P: Use PD-before-GO-Neg workaround for " MACSTR,
191 MAC2STR(dev->info.p2p_device_addr));
192 if (dev->wps_method == WPS_PIN_DISPLAY)
193 config_method = WPS_CONFIG_KEYPAD;
194 else if (dev->wps_method == WPS_PIN_KEYPAD)
195 config_method = WPS_CONFIG_DISPLAY;
196 else if (dev->wps_method == WPS_PBC)
197 config_method = WPS_CONFIG_PUSHBUTTON;
200 return p2p_prov_disc_req(p2p, dev->info.p2p_device_addr,
201 config_method, 0, 0);
204 freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
206 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
207 "P2P: No Listen/Operating frequency known for the "
208 "peer " MACSTR " to send GO Negotiation Request",
209 MAC2STR(dev->info.p2p_device_addr));
213 req = p2p_build_go_neg_req(p2p, dev);
216 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
217 "P2P: Sending GO Negotiation Request");
218 p2p_set_state(p2p, P2P_CONNECT);
219 p2p->pending_action_state = P2P_PENDING_GO_NEG_REQUEST;
220 p2p->go_neg_peer = dev;
221 dev->flags |= P2P_DEV_WAIT_GO_NEG_RESPONSE;
223 if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
224 p2p->cfg->dev_addr, dev->info.p2p_device_addr,
225 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
226 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
227 "P2P: Failed to send Action frame");
228 /* Use P2P find to recover and retry */
229 p2p_set_timeout(p2p, 0, 0);
238 static struct wpabuf * p2p_build_go_neg_resp(struct p2p_data *p2p,
239 struct p2p_device *peer,
240 u8 dialog_token, u8 status,
247 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
248 "P2P: Building GO Negotiation Response");
249 buf = wpabuf_alloc(1000);
253 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_RESP, dialog_token);
255 len = p2p_buf_add_ie_hdr(buf);
256 p2p_buf_add_status(buf, status);
258 if (peer && peer->go_state == LOCAL_GO) {
259 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
260 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
261 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
263 P2P_GROUP_CAPAB_PERSISTENT_RECONN;
265 if (p2p->cross_connect)
266 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
267 if (p2p->cfg->p2p_intra_bss)
268 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
270 p2p_buf_add_capability(buf, p2p->dev_capab, group_capab);
271 p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | tie_breaker);
272 p2p_buf_add_config_timeout(buf, 100, 20);
273 if (peer && peer->go_state == REMOTE_GO) {
274 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Omit Operating "
275 "Channel attribute");
277 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
281 p2p_buf_add_intended_addr(buf, p2p->intended_addr);
282 if (status || peer == NULL) {
283 p2p_buf_add_channel_list(buf, p2p->cfg->country,
285 } else if (peer->go_state == REMOTE_GO) {
286 p2p_buf_add_channel_list(buf, p2p->cfg->country,
289 struct p2p_channels res;
290 p2p_channels_intersect(&p2p->channels, &peer->channels,
292 p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
294 p2p_buf_add_device_info(buf, p2p, peer);
295 if (peer && peer->go_state == LOCAL_GO) {
296 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
299 p2p_buf_update_ie_hdr(buf, len);
301 /* WPS IE with Device Password ID attribute */
302 p2p_build_wps_ie(p2p, buf,
303 p2p_wps_method_pw_id(peer ? peer->wps_method :
310 static void p2p_reselect_channel(struct p2p_data *p2p,
311 struct p2p_channels *intersection)
313 struct p2p_reg_class *cl;
315 u8 op_reg_class, op_channel;
318 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Selected operating "
319 "channel (reg_class %u channel %u) not acceptable to the "
320 "peer", p2p->op_reg_class, p2p->op_channel);
322 /* First, try to pick the best channel from another band */
323 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->op_reg_class,
325 if (freq >= 2400 && freq < 2500 && p2p->best_freq_5 > 0 &&
326 p2p_freq_to_channel(p2p->cfg->country, p2p->best_freq_5,
327 &op_reg_class, &op_channel) == 0 &&
328 p2p_channels_includes(intersection, op_reg_class, op_channel)) {
329 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick best 5 GHz "
330 "channel (reg_class %u channel %u) from intersection",
331 op_reg_class, op_channel);
332 p2p->op_reg_class = op_reg_class;
333 p2p->op_channel = op_channel;
337 if (freq >= 4900 && freq < 6000 && p2p->best_freq_24 > 0 &&
338 p2p_freq_to_channel(p2p->cfg->country, p2p->best_freq_24,
339 &op_reg_class, &op_channel) == 0 &&
340 p2p_channels_includes(intersection, op_reg_class, op_channel)) {
341 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick best 2.4 GHz "
342 "channel (reg_class %u channel %u) from intersection",
343 op_reg_class, op_channel);
344 p2p->op_reg_class = op_reg_class;
345 p2p->op_channel = op_channel;
349 /* Select channel with highest preference if the peer supports it */
350 for (i = 0; p2p->cfg->pref_chan && i < p2p->cfg->num_pref_chan; i++) {
351 if (p2p_channels_includes(intersection,
352 p2p->cfg->pref_chan[i].op_class,
353 p2p->cfg->pref_chan[i].chan)) {
354 p2p->op_reg_class = p2p->cfg->pref_chan[i].op_class;
355 p2p->op_channel = p2p->cfg->pref_chan[i].chan;
356 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick "
357 "highest preferred chnnel (op_class %u "
358 "channel %u) from intersection",
359 p2p->op_reg_class, p2p->op_channel);
365 * Fall back to whatever is included in the channel intersection since
366 * no better options seems to be available.
368 cl = &intersection->reg_class[0];
369 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick another channel "
370 "(reg_class %u channel %u) from intersection",
371 cl->reg_class, cl->channel[0]);
372 p2p->op_reg_class = cl->reg_class;
373 p2p->op_channel = cl->channel[0];
377 void p2p_process_go_neg_req(struct p2p_data *p2p, const u8 *sa,
378 const u8 *data, size_t len, int rx_freq)
380 struct p2p_device *dev = NULL;
382 struct p2p_message msg;
383 u8 status = P2P_SC_FAIL_INVALID_PARAMS;
387 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
388 "P2P: Received GO Negotiation Request from " MACSTR
389 "(freq=%d)", MAC2STR(sa), rx_freq);
391 if (p2p_parse(data, len, &msg))
394 if (!msg.capability) {
395 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
396 "P2P: Mandatory Capability attribute missing from GO "
397 "Negotiation Request");
398 #ifdef CONFIG_P2P_STRICT
400 #endif /* CONFIG_P2P_STRICT */
404 tie_breaker = *msg.go_intent & 0x01;
406 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
407 "P2P: Mandatory GO Intent attribute missing from GO "
408 "Negotiation Request");
409 #ifdef CONFIG_P2P_STRICT
411 #endif /* CONFIG_P2P_STRICT */
414 if (!msg.config_timeout) {
415 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
416 "P2P: Mandatory Configuration Timeout attribute "
417 "missing from GO Negotiation Request");
418 #ifdef CONFIG_P2P_STRICT
420 #endif /* CONFIG_P2P_STRICT */
423 if (!msg.listen_channel) {
424 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
425 "P2P: No Listen Channel attribute received");
428 if (!msg.operating_channel) {
429 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
430 "P2P: No Operating Channel attribute received");
433 if (!msg.channel_list) {
434 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
435 "P2P: No Channel List attribute received");
438 if (!msg.intended_addr) {
439 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
440 "P2P: No Intended P2P Interface Address attribute "
444 if (!msg.p2p_device_info) {
445 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
446 "P2P: No P2P Device Info attribute received");
450 if (os_memcmp(msg.p2p_device_addr, sa, ETH_ALEN) != 0) {
451 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
452 "P2P: Unexpected GO Negotiation Request SA=" MACSTR
453 " != dev_addr=" MACSTR,
454 MAC2STR(sa), MAC2STR(msg.p2p_device_addr));
458 dev = p2p_get_device(p2p, sa);
460 if (msg.status && *msg.status) {
461 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
462 "P2P: Unexpected Status attribute (%d) in GO "
463 "Negotiation Request", *msg.status);
468 dev = p2p_add_dev_from_go_neg_req(p2p, sa, &msg);
469 else if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
470 p2p_add_dev_info(p2p, sa, dev, &msg);
471 if (dev && dev->flags & P2P_DEV_USER_REJECTED) {
472 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
473 "P2P: User has rejected this peer");
474 status = P2P_SC_FAIL_REJECTED_BY_USER;
475 } else if (dev == NULL || dev->wps_method == WPS_NOT_READY) {
476 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
477 "P2P: Not ready for GO negotiation with " MACSTR,
479 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
481 dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
482 p2p->cfg->go_neg_req_rx(p2p->cfg->cb_ctx, sa,
483 msg.dev_password_id);
484 } else if (p2p->go_neg_peer && p2p->go_neg_peer != dev) {
485 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
486 "P2P: Already in Group Formation with another peer");
487 status = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
491 if (!p2p->go_neg_peer) {
492 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting "
493 "GO Negotiation with previously authorized "
495 if (!(dev->flags & P2P_DEV_FORCE_FREQ)) {
496 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
497 "P2P: Use default channel settings");
498 p2p->op_reg_class = p2p->cfg->op_reg_class;
499 p2p->op_channel = p2p->cfg->op_channel;
500 os_memcpy(&p2p->channels, &p2p->cfg->channels,
501 sizeof(struct p2p_channels));
503 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
504 "P2P: Use previously configured "
505 "forced channel settings");
509 dev->flags &= ~P2P_DEV_NOT_YET_READY;
511 if (!msg.go_intent) {
512 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
513 "P2P: No GO Intent attribute received");
516 if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
517 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
518 "P2P: Invalid GO Intent value (%u) received",
519 *msg.go_intent >> 1);
523 if (dev->go_neg_req_sent &&
524 os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) > 0) {
525 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
526 "P2P: Do not reply since peer has higher "
527 "address and GO Neg Request already sent");
528 p2p_parse_free(&msg);
532 go = p2p_go_det(p2p->go_intent, *msg.go_intent);
534 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
535 "P2P: Incompatible GO Intent");
536 status = P2P_SC_FAIL_BOTH_GO_INTENT_15;
540 if (p2p_peer_channels(p2p, dev, msg.channel_list,
541 msg.channel_list_len) < 0) {
542 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
543 "P2P: No common channels found");
544 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
548 switch (msg.dev_password_id) {
549 case DEV_PW_REGISTRAR_SPECIFIED:
550 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
551 "P2P: PIN from peer Display");
552 if (dev->wps_method != WPS_PIN_KEYPAD) {
553 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
554 "P2P: We have wps_method=%s -> "
556 p2p_wps_method_str(dev->wps_method));
557 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
561 case DEV_PW_USER_SPECIFIED:
562 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
563 "P2P: Peer entered PIN on Keypad");
564 if (dev->wps_method != WPS_PIN_DISPLAY) {
565 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
566 "P2P: We have wps_method=%s -> "
568 p2p_wps_method_str(dev->wps_method));
569 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
573 case DEV_PW_PUSHBUTTON:
574 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
575 "P2P: Peer using pushbutton");
576 if (dev->wps_method != WPS_PBC) {
577 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
578 "P2P: We have wps_method=%s -> "
580 p2p_wps_method_str(dev->wps_method));
581 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
586 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
587 "P2P: Unsupported Device Password ID %d",
588 msg.dev_password_id);
589 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
594 struct p2p_channels intersection;
596 p2p_channels_intersect(&p2p->channels, &dev->channels,
598 if (intersection.reg_classes == 0 ||
599 intersection.reg_class[0].channels == 0) {
600 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
601 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
602 "P2P: No common channels found");
605 for (i = 0; i < intersection.reg_classes; i++) {
606 struct p2p_reg_class *c;
607 c = &intersection.reg_class[i];
608 wpa_printf(MSG_DEBUG, "P2P: reg_class %u",
610 wpa_hexdump(MSG_DEBUG, "P2P: channels",
611 c->channel, c->channels);
613 if (!p2p_channels_includes(&intersection,
616 p2p_reselect_channel(p2p, &intersection);
618 if (!p2p->ssid_set) {
619 p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
624 dev->go_state = go ? LOCAL_GO : REMOTE_GO;
625 dev->oper_freq = p2p_channel_to_freq((const char *)
626 msg.operating_channel,
627 msg.operating_channel[3],
628 msg.operating_channel[4]);
629 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
630 "channel preference: %d MHz", dev->oper_freq);
632 if (msg.config_timeout) {
633 dev->go_timeout = msg.config_timeout[0];
634 dev->client_timeout = msg.config_timeout[1];
637 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
638 "P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
639 if (p2p->state != P2P_IDLE)
640 p2p_stop_find_for_freq(p2p, rx_freq);
641 p2p_set_state(p2p, P2P_GO_NEG);
642 p2p_clear_timeout(p2p);
643 dev->dialog_token = msg.dialog_token;
644 os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
645 p2p->go_neg_peer = dev;
646 status = P2P_SC_SUCCESS;
651 dev->status = status;
652 resp = p2p_build_go_neg_resp(p2p, dev, msg.dialog_token, status,
654 p2p_parse_free(&msg);
657 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
658 "P2P: Sending GO Negotiation Response");
662 freq = p2p_channel_to_freq(p2p->cfg->country,
666 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
667 "P2P: Unknown regulatory class/channel");
671 if (status == P2P_SC_SUCCESS) {
672 p2p->pending_action_state = P2P_PENDING_GO_NEG_RESPONSE;
673 dev->flags |= P2P_DEV_WAIT_GO_NEG_CONFIRM;
675 p2p->pending_action_state =
676 P2P_PENDING_GO_NEG_RESPONSE_FAILURE;
677 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
679 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
680 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
681 "P2P: Failed to send Action frame");
688 static struct wpabuf * p2p_build_go_neg_conf(struct p2p_data *p2p,
689 struct p2p_device *peer,
690 u8 dialog_token, u8 status,
691 const u8 *resp_chan, int go)
695 struct p2p_channels res;
698 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
699 "P2P: Building GO Negotiation Confirm");
700 buf = wpabuf_alloc(1000);
704 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_CONF, dialog_token);
706 len = p2p_buf_add_ie_hdr(buf);
707 p2p_buf_add_status(buf, status);
709 if (peer->go_state == LOCAL_GO) {
710 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
711 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
712 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
714 P2P_GROUP_CAPAB_PERSISTENT_RECONN;
716 if (p2p->cross_connect)
717 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
718 if (p2p->cfg->p2p_intra_bss)
719 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
721 p2p_buf_add_capability(buf, p2p->dev_capab, group_capab);
722 if (go || resp_chan == NULL)
723 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
727 p2p_buf_add_operating_channel(buf, (const char *) resp_chan,
728 resp_chan[3], resp_chan[4]);
729 p2p_channels_intersect(&p2p->channels, &peer->channels, &res);
730 p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
732 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
735 p2p_buf_update_ie_hdr(buf, len);
741 void p2p_process_go_neg_resp(struct p2p_data *p2p, const u8 *sa,
742 const u8 *data, size_t len, int rx_freq)
744 struct p2p_device *dev;
747 struct p2p_message msg;
748 u8 status = P2P_SC_SUCCESS;
751 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
752 "P2P: Received GO Negotiation Response from " MACSTR
753 " (freq=%d)", MAC2STR(sa), rx_freq);
754 dev = p2p_get_device(p2p, sa);
755 if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
756 dev != p2p->go_neg_peer) {
757 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
758 "P2P: Not ready for GO negotiation with " MACSTR,
763 if (p2p_parse(data, len, &msg))
766 if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE)) {
767 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
768 "P2P: Was not expecting GO Negotiation Response - "
770 p2p_parse_free(&msg);
773 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
775 if (msg.dialog_token != dev->dialog_token) {
776 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
777 "P2P: Unexpected Dialog Token %u (expected %u)",
778 msg.dialog_token, dev->dialog_token);
779 p2p_parse_free(&msg);
784 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
785 "P2P: No Status attribute received");
786 status = P2P_SC_FAIL_INVALID_PARAMS;
790 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
791 "P2P: GO Negotiation rejected: status %d",
793 dev->go_neg_req_sent = 0;
794 if (*msg.status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
795 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
796 "P2P: Wait for the peer to become ready for "
798 dev->flags |= P2P_DEV_NOT_YET_READY;
800 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
801 p2p_set_timeout(p2p, 0, 0);
803 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
804 "P2P: Stop GO Negotiation attempt");
805 p2p_go_neg_failed(p2p, dev, *msg.status);
807 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
808 p2p_parse_free(&msg);
812 if (!msg.capability) {
813 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
814 "P2P: Mandatory Capability attribute missing from GO "
815 "Negotiation Response");
816 #ifdef CONFIG_P2P_STRICT
817 status = P2P_SC_FAIL_INVALID_PARAMS;
819 #endif /* CONFIG_P2P_STRICT */
822 if (!msg.p2p_device_info) {
823 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
824 "P2P: Mandatory P2P Device Info attribute missing "
825 "from GO Negotiation Response");
826 #ifdef CONFIG_P2P_STRICT
827 status = P2P_SC_FAIL_INVALID_PARAMS;
829 #endif /* CONFIG_P2P_STRICT */
832 if (!msg.intended_addr) {
833 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
834 "P2P: No Intended P2P Interface Address attribute "
836 status = P2P_SC_FAIL_INVALID_PARAMS;
840 if (!msg.go_intent) {
841 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
842 "P2P: No GO Intent attribute received");
843 status = P2P_SC_FAIL_INVALID_PARAMS;
846 if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
847 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
848 "P2P: Invalid GO Intent value (%u) received",
849 *msg.go_intent >> 1);
850 status = P2P_SC_FAIL_INVALID_PARAMS;
854 go = p2p_go_det(p2p->go_intent, *msg.go_intent);
856 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
857 "P2P: Incompatible GO Intent");
858 status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
862 if (!go && msg.group_id) {
863 /* Store SSID for Provisioning step */
864 p2p->ssid_len = msg.group_id_len - ETH_ALEN;
865 os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
867 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
868 "P2P: Mandatory P2P Group ID attribute missing from "
869 "GO Negotiation Response");
871 #ifdef CONFIG_P2P_STRICT
872 status = P2P_SC_FAIL_INVALID_PARAMS;
874 #endif /* CONFIG_P2P_STRICT */
877 if (!msg.config_timeout) {
878 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
879 "P2P: Mandatory Configuration Timeout attribute "
880 "missing from GO Negotiation Response");
881 #ifdef CONFIG_P2P_STRICT
882 status = P2P_SC_FAIL_INVALID_PARAMS;
884 #endif /* CONFIG_P2P_STRICT */
886 dev->go_timeout = msg.config_timeout[0];
887 dev->client_timeout = msg.config_timeout[1];
890 if (!msg.operating_channel && !go) {
892 * Note: P2P Client may omit Operating Channel attribute to
893 * indicate it does not have a preference.
895 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
896 "P2P: No Operating Channel attribute received");
897 status = P2P_SC_FAIL_INVALID_PARAMS;
900 if (!msg.channel_list) {
901 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
902 "P2P: No Channel List attribute received");
903 status = P2P_SC_FAIL_INVALID_PARAMS;
907 if (p2p_peer_channels(p2p, dev, msg.channel_list,
908 msg.channel_list_len) < 0) {
909 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
910 "P2P: No common channels found");
911 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
915 if (msg.operating_channel) {
916 dev->oper_freq = p2p_channel_to_freq((const char *)
917 msg.operating_channel,
918 msg.operating_channel[3],
919 msg.operating_channel[4]);
920 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
921 "channel preference: %d MHz", dev->oper_freq);
925 switch (msg.dev_password_id) {
926 case DEV_PW_REGISTRAR_SPECIFIED:
927 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
928 "P2P: PIN from peer Display");
929 if (dev->wps_method != WPS_PIN_KEYPAD) {
930 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
931 "P2P: We have wps_method=%s -> "
933 p2p_wps_method_str(dev->wps_method));
934 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
938 case DEV_PW_USER_SPECIFIED:
939 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
940 "P2P: Peer entered PIN on Keypad");
941 if (dev->wps_method != WPS_PIN_DISPLAY) {
942 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
943 "P2P: We have wps_method=%s -> "
945 p2p_wps_method_str(dev->wps_method));
946 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
950 case DEV_PW_PUSHBUTTON:
951 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
952 "P2P: Peer using pushbutton");
953 if (dev->wps_method != WPS_PBC) {
954 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
955 "P2P: We have wps_method=%s -> "
957 p2p_wps_method_str(dev->wps_method));
958 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
963 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
964 "P2P: Unsupported Device Password ID %d",
965 msg.dev_password_id);
966 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
971 struct p2p_channels intersection;
973 p2p_channels_intersect(&p2p->channels, &dev->channels,
975 if (intersection.reg_classes == 0 ||
976 intersection.reg_class[0].channels == 0) {
977 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
978 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
979 "P2P: No common channels found");
982 for (i = 0; i < intersection.reg_classes; i++) {
983 struct p2p_reg_class *c;
984 c = &intersection.reg_class[i];
985 wpa_printf(MSG_DEBUG, "P2P: reg_class %u",
987 wpa_hexdump(MSG_DEBUG, "P2P: channels",
988 c->channel, c->channels);
990 if (!p2p_channels_includes(&intersection, p2p->op_reg_class,
992 p2p_reselect_channel(p2p, &intersection);
994 if (!p2p->ssid_set) {
995 p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
1000 p2p_set_state(p2p, P2P_GO_NEG);
1001 p2p_clear_timeout(p2p);
1003 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1004 "P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
1005 os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
1008 conf = p2p_build_go_neg_conf(p2p, dev, msg.dialog_token, status,
1009 msg.operating_channel, go);
1010 p2p_parse_free(&msg);
1013 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1014 "P2P: Sending GO Negotiation Confirm");
1015 if (status == P2P_SC_SUCCESS) {
1016 p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
1017 dev->go_state = go ? LOCAL_GO : REMOTE_GO;
1019 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1023 freq = dev->listen_freq;
1024 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr, sa,
1025 wpabuf_head(conf), wpabuf_len(conf), 200) < 0) {
1026 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1027 "P2P: Failed to send Action frame");
1028 p2p_go_neg_failed(p2p, dev, -1);
1034 void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa,
1035 const u8 *data, size_t len)
1037 struct p2p_device *dev;
1038 struct p2p_message msg;
1040 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1041 "P2P: Received GO Negotiation Confirm from " MACSTR,
1043 dev = p2p_get_device(p2p, sa);
1044 if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
1045 dev != p2p->go_neg_peer) {
1046 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1047 "P2P: Not ready for GO negotiation with " MACSTR,
1052 if (p2p->pending_action_state == P2P_PENDING_GO_NEG_RESPONSE) {
1053 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopped waiting "
1054 "for TX status on GO Negotiation Response since we "
1055 "already received Confirmation");
1056 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1059 if (p2p_parse(data, len, &msg))
1062 if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
1063 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1064 "P2P: Was not expecting GO Negotiation Confirm - "
1068 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1070 if (msg.dialog_token != dev->dialog_token) {
1071 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1072 "P2P: Unexpected Dialog Token %u (expected %u)",
1073 msg.dialog_token, dev->dialog_token);
1074 p2p_parse_free(&msg);
1079 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1080 "P2P: No Status attribute received");
1081 p2p_parse_free(&msg);
1085 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1086 "P2P: GO Negotiation rejected: status %d",
1088 p2p_parse_free(&msg);
1092 if (dev->go_state == REMOTE_GO && msg.group_id) {
1093 /* Store SSID for Provisioning step */
1094 p2p->ssid_len = msg.group_id_len - ETH_ALEN;
1095 os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
1096 } else if (dev->go_state == REMOTE_GO) {
1097 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1098 "P2P: Mandatory P2P Group ID attribute missing from "
1099 "GO Negotiation Confirmation");
1101 #ifdef CONFIG_P2P_STRICT
1102 p2p_parse_free(&msg);
1104 #endif /* CONFIG_P2P_STRICT */
1107 if (!msg.operating_channel) {
1108 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1109 "P2P: Mandatory Operating Channel attribute missing "
1110 "from GO Negotiation Confirmation");
1111 #ifdef CONFIG_P2P_STRICT
1112 p2p_parse_free(&msg);
1114 #endif /* CONFIG_P2P_STRICT */
1117 if (!msg.channel_list) {
1118 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1119 "P2P: Mandatory Operating Channel attribute missing "
1120 "from GO Negotiation Confirmation");
1121 #ifdef CONFIG_P2P_STRICT
1122 p2p_parse_free(&msg);
1124 #endif /* CONFIG_P2P_STRICT */
1127 p2p_parse_free(&msg);
1129 if (dev->go_state == UNKNOWN_GO) {
1131 * This should not happen since GO negotiation has already
1134 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1135 "P2P: Unexpected GO Neg state - do not know which end "
1140 p2p_go_complete(p2p, dev);