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) {
110 case WPS_PIN_DISPLAY:
111 return DEV_PW_REGISTRAR_SPECIFIED;
113 return DEV_PW_USER_SPECIFIED;
115 return DEV_PW_PUSHBUTTON;
117 return DEV_PW_DEFAULT;
122 static const char * p2p_wps_method_str(enum p2p_wps_method wps_method)
124 switch (wps_method) {
125 case WPS_PIN_DISPLAY:
137 static struct wpabuf * p2p_build_go_neg_req(struct p2p_data *p2p,
138 struct p2p_device *peer)
144 buf = wpabuf_alloc(1000);
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 (p2p->cross_connect)
158 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
159 if (p2p->cfg->p2p_intra_bss)
160 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
161 p2p_buf_add_capability(buf, p2p->dev_capab, group_capab);
162 p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) |
163 p2p->next_tie_breaker);
164 p2p->next_tie_breaker = !p2p->next_tie_breaker;
165 p2p_buf_add_config_timeout(buf, 100, 20);
166 p2p_buf_add_listen_channel(buf, p2p->cfg->country, p2p->cfg->reg_class,
168 if (p2p->ext_listen_interval)
169 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
170 p2p->ext_listen_interval);
171 p2p_buf_add_intended_addr(buf, p2p->intended_addr);
172 p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
173 p2p_buf_add_device_info(buf, p2p, peer);
174 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
175 p2p->op_reg_class, p2p->op_channel);
176 p2p_buf_update_ie_hdr(buf, len);
178 /* WPS IE with Device Password ID attribute */
179 p2p_build_wps_ie(p2p, buf, p2p_wps_method_pw_id(peer->wps_method), 0);
185 int p2p_connect_send(struct p2p_data *p2p, struct p2p_device *dev)
190 freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
192 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
193 "P2P: No Listen/Operating frequency known for the "
194 "peer " MACSTR " to send GO Negotiation Request",
195 MAC2STR(dev->info.p2p_device_addr));
199 req = p2p_build_go_neg_req(p2p, dev);
202 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
203 "P2P: Sending GO Negotiation Request");
204 p2p_set_state(p2p, P2P_CONNECT);
205 p2p->pending_action_state = P2P_PENDING_GO_NEG_REQUEST;
206 p2p->go_neg_peer = dev;
207 dev->flags |= P2P_DEV_WAIT_GO_NEG_RESPONSE;
209 if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
210 p2p->cfg->dev_addr, dev->info.p2p_device_addr,
211 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
212 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
213 "P2P: Failed to send Action frame");
214 /* Use P2P find to recover and retry */
215 p2p_set_timeout(p2p, 0, 0);
224 static struct wpabuf * p2p_build_go_neg_resp(struct p2p_data *p2p,
225 struct p2p_device *peer,
226 u8 dialog_token, u8 status,
233 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
234 "P2P: Building GO Negotiation Response");
235 buf = wpabuf_alloc(1000);
239 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_RESP, dialog_token);
241 len = p2p_buf_add_ie_hdr(buf);
242 p2p_buf_add_status(buf, status);
244 if (peer && peer->go_state == LOCAL_GO) {
245 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP)
246 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
247 if (p2p->cross_connect)
248 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
249 if (p2p->cfg->p2p_intra_bss)
250 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
252 p2p_buf_add_capability(buf, p2p->dev_capab, group_capab);
253 p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | tie_breaker);
254 p2p_buf_add_config_timeout(buf, 100, 20);
255 if (peer && peer->go_state == REMOTE_GO) {
256 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Omit Operating "
257 "Channel attribute");
259 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
263 p2p_buf_add_intended_addr(buf, p2p->intended_addr);
264 if (status || peer == NULL) {
265 p2p_buf_add_channel_list(buf, p2p->cfg->country,
267 } else if (peer->go_state == REMOTE_GO) {
268 p2p_buf_add_channel_list(buf, p2p->cfg->country,
271 struct p2p_channels res;
272 p2p_channels_intersect(&p2p->channels, &peer->channels,
274 p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
276 p2p_buf_add_device_info(buf, p2p, peer);
277 if (peer && peer->go_state == LOCAL_GO) {
278 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
281 p2p_buf_update_ie_hdr(buf, len);
283 /* WPS IE with Device Password ID attribute */
284 p2p_build_wps_ie(p2p, buf,
285 p2p_wps_method_pw_id(peer ? peer->wps_method :
292 static void p2p_reselect_channel(struct p2p_data *p2p,
293 struct p2p_channels *intersection)
295 struct p2p_reg_class *cl;
297 u8 op_reg_class, op_channel;
299 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Selected operating "
300 "channel (reg_class %u channel %u) not acceptable to the "
301 "peer", p2p->op_reg_class, p2p->op_channel);
303 /* First, try to pick the best channel from another band */
304 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->op_reg_class,
306 if (freq >= 2400 && freq < 2500 && p2p->best_freq_5 > 0 &&
307 p2p_freq_to_channel(p2p->cfg->country, p2p->best_freq_5,
308 &op_reg_class, &op_channel) == 0 &&
309 p2p_channels_includes(intersection, op_reg_class, op_channel)) {
310 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick best 5 GHz "
311 "channel (reg_class %u channel %u) from intersection",
312 op_reg_class, op_channel);
313 p2p->op_reg_class = op_reg_class;
314 p2p->op_channel = op_channel;
318 if (freq >= 4900 && freq < 6000 && p2p->best_freq_24 > 0 &&
319 p2p_freq_to_channel(p2p->cfg->country, p2p->best_freq_24,
320 &op_reg_class, &op_channel) == 0 &&
321 p2p_channels_includes(intersection, op_reg_class, op_channel)) {
322 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick best 2.4 GHz "
323 "channel (reg_class %u channel %u) from intersection",
324 op_reg_class, op_channel);
325 p2p->op_reg_class = op_reg_class;
326 p2p->op_channel = op_channel;
331 * Fall back to whatever is included in the channel intersection since
332 * no better options seems to be available.
334 cl = &intersection->reg_class[0];
335 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick another channel "
336 "(reg_class %u channel %u) from intersection",
337 cl->reg_class, cl->channel[0]);
338 p2p->op_reg_class = cl->reg_class;
339 p2p->op_channel = cl->channel[0];
343 void p2p_process_go_neg_req(struct p2p_data *p2p, const u8 *sa,
344 const u8 *data, size_t len, int rx_freq)
346 struct p2p_device *dev = NULL;
348 struct p2p_message msg;
349 u8 status = P2P_SC_FAIL_INVALID_PARAMS;
353 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
354 "P2P: Received GO Negotiation Request from " MACSTR
355 "(freq=%d)", MAC2STR(sa), rx_freq);
357 if (p2p_parse(data, len, &msg))
360 if (!msg.capability) {
361 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
362 "P2P: Mandatory Capability attribute missing from GO "
363 "Negotiation Request");
364 #ifdef CONFIG_P2P_STRICT
366 #endif /* CONFIG_P2P_STRICT */
370 tie_breaker = *msg.go_intent & 0x01;
372 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
373 "P2P: Mandatory GO Intent attribute missing from GO "
374 "Negotiation Request");
375 #ifdef CONFIG_P2P_STRICT
377 #endif /* CONFIG_P2P_STRICT */
380 if (!msg.config_timeout) {
381 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
382 "P2P: Mandatory Configuration Timeout attribute "
383 "missing from GO Negotiation Request");
384 #ifdef CONFIG_P2P_STRICT
386 #endif /* CONFIG_P2P_STRICT */
389 if (!msg.listen_channel) {
390 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
391 "P2P: No Listen Channel attribute received");
394 if (!msg.operating_channel) {
395 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
396 "P2P: No Operating Channel attribute received");
399 if (!msg.channel_list) {
400 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
401 "P2P: No Channel List attribute received");
404 if (!msg.intended_addr) {
405 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
406 "P2P: No Intended P2P Interface Address attribute "
410 if (!msg.p2p_device_info) {
411 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
412 "P2P: No P2P Device Info attribute received");
416 if (os_memcmp(msg.p2p_device_addr, sa, ETH_ALEN) != 0) {
417 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
418 "P2P: Unexpected GO Negotiation Request SA=" MACSTR
419 " != dev_addr=" MACSTR,
420 MAC2STR(sa), MAC2STR(msg.p2p_device_addr));
424 dev = p2p_get_device(p2p, sa);
426 if (msg.status && *msg.status) {
427 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
428 "P2P: Unexpected Status attribute (%d) in GO "
429 "Negotiation Request", *msg.status);
434 dev = p2p_add_dev_from_go_neg_req(p2p, sa, &msg);
435 else if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
436 p2p_add_dev_info(p2p, sa, dev, &msg);
437 if (dev && dev->flags & P2P_DEV_USER_REJECTED) {
438 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
439 "P2P: User has rejected this peer");
440 status = P2P_SC_FAIL_REJECTED_BY_USER;
441 } else if (dev == NULL || dev->wps_method == WPS_NOT_READY) {
442 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
443 "P2P: Not ready for GO negotiation with " MACSTR,
445 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
447 dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
448 p2p->cfg->go_neg_req_rx(p2p->cfg->cb_ctx, sa,
449 msg.dev_password_id);
450 } else if (p2p->go_neg_peer && p2p->go_neg_peer != dev) {
451 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
452 "P2P: Already in Group Formation with another peer");
453 status = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
457 if (!p2p->go_neg_peer) {
458 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting "
459 "GO Negotiation with previously authorized "
461 if (!(dev->flags & P2P_DEV_FORCE_FREQ)) {
462 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
463 "P2P: Use default channel settings");
464 p2p->op_reg_class = p2p->cfg->op_reg_class;
465 p2p->op_channel = p2p->cfg->op_channel;
466 os_memcpy(&p2p->channels, &p2p->cfg->channels,
467 sizeof(struct p2p_channels));
469 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
470 "P2P: Use previously configured "
471 "forced channel settings");
475 dev->flags &= ~P2P_DEV_NOT_YET_READY;
477 if (!msg.go_intent) {
478 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
479 "P2P: No GO Intent attribute received");
482 if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
483 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
484 "P2P: Invalid GO Intent value (%u) received",
485 *msg.go_intent >> 1);
489 if (dev->go_neg_req_sent &&
490 os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) > 0) {
491 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
492 "P2P: Do not reply since peer has higher "
493 "address and GO Neg Request already sent");
494 p2p_parse_free(&msg);
498 go = p2p_go_det(p2p->go_intent, *msg.go_intent);
500 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
501 "P2P: Incompatible GO Intent");
502 status = P2P_SC_FAIL_BOTH_GO_INTENT_15;
506 if (p2p_peer_channels(p2p, dev, msg.channel_list,
507 msg.channel_list_len) < 0) {
508 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
509 "P2P: No common channels found");
510 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
514 switch (msg.dev_password_id) {
515 case DEV_PW_REGISTRAR_SPECIFIED:
516 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
517 "P2P: PIN from peer Display");
518 if (dev->wps_method != WPS_PIN_KEYPAD) {
519 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
520 "P2P: We have wps_method=%s -> "
522 p2p_wps_method_str(dev->wps_method));
523 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
527 case DEV_PW_USER_SPECIFIED:
528 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
529 "P2P: Peer entered PIN on Keypad");
530 if (dev->wps_method != WPS_PIN_DISPLAY) {
531 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
532 "P2P: We have wps_method=%s -> "
534 p2p_wps_method_str(dev->wps_method));
535 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
539 case DEV_PW_PUSHBUTTON:
540 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
541 "P2P: Peer using pushbutton");
542 if (dev->wps_method != WPS_PBC) {
543 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
544 "P2P: We have wps_method=%s -> "
546 p2p_wps_method_str(dev->wps_method));
547 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
552 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
553 "P2P: Unsupported Device Password ID %d",
554 msg.dev_password_id);
555 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
560 struct p2p_channels intersection;
562 p2p_channels_intersect(&p2p->channels, &dev->channels,
564 if (intersection.reg_classes == 0 ||
565 intersection.reg_class[0].channels == 0) {
566 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
567 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
568 "P2P: No common channels found");
571 for (i = 0; i < intersection.reg_classes; i++) {
572 struct p2p_reg_class *c;
573 c = &intersection.reg_class[i];
574 wpa_printf(MSG_DEBUG, "P2P: reg_class %u",
576 wpa_hexdump(MSG_DEBUG, "P2P: channels",
577 c->channel, c->channels);
579 if (!p2p_channels_includes(&intersection,
582 p2p_reselect_channel(p2p, &intersection);
584 p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
587 dev->go_state = go ? LOCAL_GO : REMOTE_GO;
588 dev->oper_freq = p2p_channel_to_freq((const char *)
589 msg.operating_channel,
590 msg.operating_channel[3],
591 msg.operating_channel[4]);
592 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
593 "channel preference: %d MHz", dev->oper_freq);
595 if (msg.config_timeout) {
596 dev->go_timeout = msg.config_timeout[0];
597 dev->client_timeout = msg.config_timeout[1];
600 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
601 "P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
602 if (p2p->state != P2P_IDLE)
603 p2p_stop_find_for_freq(p2p, rx_freq);
604 p2p_set_state(p2p, P2P_GO_NEG);
605 p2p_clear_timeout(p2p);
606 dev->dialog_token = msg.dialog_token;
607 os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
608 p2p->go_neg_peer = dev;
609 status = P2P_SC_SUCCESS;
614 dev->status = status;
615 resp = p2p_build_go_neg_resp(p2p, dev, msg.dialog_token, status,
617 p2p_parse_free(&msg);
620 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
621 "P2P: Sending GO Negotiation Response");
625 freq = p2p_channel_to_freq(p2p->cfg->country,
629 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
630 "P2P: Unknown regulatory class/channel");
634 if (status == P2P_SC_SUCCESS) {
635 p2p->pending_action_state = P2P_PENDING_GO_NEG_RESPONSE;
636 dev->flags |= P2P_DEV_WAIT_GO_NEG_CONFIRM;
638 p2p->pending_action_state =
639 P2P_PENDING_GO_NEG_RESPONSE_FAILURE;
640 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
642 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
643 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
644 "P2P: Failed to send Action frame");
651 static struct wpabuf * p2p_build_go_neg_conf(struct p2p_data *p2p,
652 struct p2p_device *peer,
653 u8 dialog_token, u8 status,
654 const u8 *resp_chan, int go)
658 struct p2p_channels res;
661 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
662 "P2P: Building GO Negotiation Confirm");
663 buf = wpabuf_alloc(1000);
667 p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_CONF, dialog_token);
669 len = p2p_buf_add_ie_hdr(buf);
670 p2p_buf_add_status(buf, status);
672 if (peer->go_state == LOCAL_GO) {
673 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP)
674 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
675 if (p2p->cross_connect)
676 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
677 if (p2p->cfg->p2p_intra_bss)
678 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
680 p2p_buf_add_capability(buf, p2p->dev_capab, group_capab);
681 if (go || resp_chan == NULL)
682 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
686 p2p_buf_add_operating_channel(buf, (const char *) resp_chan,
687 resp_chan[3], resp_chan[4]);
688 p2p_channels_intersect(&p2p->channels, &peer->channels, &res);
689 p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
691 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
694 p2p_buf_update_ie_hdr(buf, len);
700 void p2p_process_go_neg_resp(struct p2p_data *p2p, const u8 *sa,
701 const u8 *data, size_t len, int rx_freq)
703 struct p2p_device *dev;
706 struct p2p_message msg;
707 u8 status = P2P_SC_SUCCESS;
710 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
711 "P2P: Received GO Negotiation Response from " MACSTR
712 " (freq=%d)", MAC2STR(sa), rx_freq);
713 dev = p2p_get_device(p2p, sa);
714 if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
715 dev != p2p->go_neg_peer) {
716 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
717 "P2P: Not ready for GO negotiation with " MACSTR,
722 if (p2p_parse(data, len, &msg))
725 if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE)) {
726 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
727 "P2P: Was not expecting GO Negotiation Response - "
729 p2p_parse_free(&msg);
732 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
734 if (msg.dialog_token != dev->dialog_token) {
735 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
736 "P2P: Unexpected Dialog Token %u (expected %u)",
737 msg.dialog_token, dev->dialog_token);
738 p2p_parse_free(&msg);
743 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
744 "P2P: No Status attribute received");
745 status = P2P_SC_FAIL_INVALID_PARAMS;
749 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
750 "P2P: GO Negotiation rejected: status %d",
752 dev->go_neg_req_sent = 0;
753 if (*msg.status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
754 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
755 "P2P: Wait for the peer to become ready for "
757 dev->flags |= P2P_DEV_NOT_YET_READY;
759 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
760 p2p_set_timeout(p2p, 0, 0);
762 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
763 "P2P: Stop GO Negotiation attempt");
764 p2p_go_neg_failed(p2p, dev, *msg.status);
766 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
767 p2p_parse_free(&msg);
771 if (!msg.capability) {
772 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
773 "P2P: Mandatory Capability attribute missing from GO "
774 "Negotiation Response");
775 #ifdef CONFIG_P2P_STRICT
776 status = P2P_SC_FAIL_INVALID_PARAMS;
778 #endif /* CONFIG_P2P_STRICT */
781 if (!msg.p2p_device_info) {
782 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
783 "P2P: Mandatory P2P Device Info attribute missing "
784 "from GO Negotiation Response");
785 #ifdef CONFIG_P2P_STRICT
786 status = P2P_SC_FAIL_INVALID_PARAMS;
788 #endif /* CONFIG_P2P_STRICT */
791 if (!msg.intended_addr) {
792 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
793 "P2P: No Intended P2P Interface Address attribute "
795 status = P2P_SC_FAIL_INVALID_PARAMS;
799 if (!msg.go_intent) {
800 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
801 "P2P: No GO Intent attribute received");
802 status = P2P_SC_FAIL_INVALID_PARAMS;
805 if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
806 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
807 "P2P: Invalid GO Intent value (%u) received",
808 *msg.go_intent >> 1);
809 status = P2P_SC_FAIL_INVALID_PARAMS;
813 go = p2p_go_det(p2p->go_intent, *msg.go_intent);
815 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
816 "P2P: Incompatible GO Intent");
817 status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
821 if (!go && msg.group_id) {
822 /* Store SSID for Provisioning step */
823 p2p->ssid_len = msg.group_id_len - ETH_ALEN;
824 os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
826 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
827 "P2P: Mandatory P2P Group ID attribute missing from "
828 "GO Negotiation Response");
830 #ifdef CONFIG_P2P_STRICT
831 status = P2P_SC_FAIL_INVALID_PARAMS;
833 #endif /* CONFIG_P2P_STRICT */
836 if (!msg.config_timeout) {
837 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
838 "P2P: Mandatory Configuration Timeout attribute "
839 "missing from GO Negotiation Response");
840 #ifdef CONFIG_P2P_STRICT
841 status = P2P_SC_FAIL_INVALID_PARAMS;
843 #endif /* CONFIG_P2P_STRICT */
845 dev->go_timeout = msg.config_timeout[0];
846 dev->client_timeout = msg.config_timeout[1];
849 if (!msg.operating_channel && !go) {
851 * Note: P2P Client may omit Operating Channel attribute to
852 * indicate it does not have a preference.
854 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
855 "P2P: No Operating Channel attribute received");
856 status = P2P_SC_FAIL_INVALID_PARAMS;
859 if (!msg.channel_list) {
860 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
861 "P2P: No Channel List attribute received");
862 status = P2P_SC_FAIL_INVALID_PARAMS;
866 if (p2p_peer_channels(p2p, dev, msg.channel_list,
867 msg.channel_list_len) < 0) {
868 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
869 "P2P: No common channels found");
870 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
874 if (msg.operating_channel) {
875 dev->oper_freq = p2p_channel_to_freq((const char *)
876 msg.operating_channel,
877 msg.operating_channel[3],
878 msg.operating_channel[4]);
879 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
880 "channel preference: %d MHz", dev->oper_freq);
884 switch (msg.dev_password_id) {
885 case DEV_PW_REGISTRAR_SPECIFIED:
886 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
887 "P2P: PIN from peer Display");
888 if (dev->wps_method != WPS_PIN_KEYPAD) {
889 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
890 "P2P: We have wps_method=%s -> "
892 p2p_wps_method_str(dev->wps_method));
893 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
897 case DEV_PW_USER_SPECIFIED:
898 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
899 "P2P: Peer entered PIN on Keypad");
900 if (dev->wps_method != WPS_PIN_DISPLAY) {
901 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
902 "P2P: We have wps_method=%s -> "
904 p2p_wps_method_str(dev->wps_method));
905 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
909 case DEV_PW_PUSHBUTTON:
910 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
911 "P2P: Peer using pushbutton");
912 if (dev->wps_method != WPS_PBC) {
913 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
914 "P2P: We have wps_method=%s -> "
916 p2p_wps_method_str(dev->wps_method));
917 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
922 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
923 "P2P: Unsupported Device Password ID %d",
924 msg.dev_password_id);
925 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
930 struct p2p_channels intersection;
932 p2p_channels_intersect(&p2p->channels, &dev->channels,
934 if (intersection.reg_classes == 0 ||
935 intersection.reg_class[0].channels == 0) {
936 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
937 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
938 "P2P: No common channels found");
941 for (i = 0; i < intersection.reg_classes; i++) {
942 struct p2p_reg_class *c;
943 c = &intersection.reg_class[i];
944 wpa_printf(MSG_DEBUG, "P2P: reg_class %u",
946 wpa_hexdump(MSG_DEBUG, "P2P: channels",
947 c->channel, c->channels);
949 if (!p2p_channels_includes(&intersection, p2p->op_reg_class,
951 p2p_reselect_channel(p2p, &intersection);
953 p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
956 p2p_set_state(p2p, P2P_GO_NEG);
957 p2p_clear_timeout(p2p);
959 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
960 "P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
961 os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
964 conf = p2p_build_go_neg_conf(p2p, dev, msg.dialog_token, status,
965 msg.operating_channel, go);
966 p2p_parse_free(&msg);
969 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
970 "P2P: Sending GO Negotiation Confirm");
971 if (status == P2P_SC_SUCCESS) {
972 p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
973 dev->go_state = go ? LOCAL_GO : REMOTE_GO;
975 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
979 freq = dev->listen_freq;
980 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr, sa,
981 wpabuf_head(conf), wpabuf_len(conf), 200) < 0) {
982 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
983 "P2P: Failed to send Action frame");
984 p2p_go_neg_failed(p2p, dev, -1);
990 void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa,
991 const u8 *data, size_t len)
993 struct p2p_device *dev;
994 struct p2p_message msg;
996 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
997 "P2P: Received GO Negotiation Confirm from " MACSTR,
999 dev = p2p_get_device(p2p, sa);
1000 if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
1001 dev != p2p->go_neg_peer) {
1002 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1003 "P2P: Not ready for GO negotiation with " MACSTR,
1008 if (p2p->pending_action_state == P2P_PENDING_GO_NEG_RESPONSE) {
1009 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopped waiting "
1010 "for TX status on GO Negotiation Response since we "
1011 "already received Confirmation");
1012 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1015 if (p2p_parse(data, len, &msg))
1018 if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
1019 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1020 "P2P: Was not expecting GO Negotiation Confirm - "
1024 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1026 if (msg.dialog_token != dev->dialog_token) {
1027 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1028 "P2P: Unexpected Dialog Token %u (expected %u)",
1029 msg.dialog_token, dev->dialog_token);
1030 p2p_parse_free(&msg);
1035 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1036 "P2P: No Status attribute received");
1037 p2p_parse_free(&msg);
1041 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1042 "P2P: GO Negotiation rejected: status %d",
1044 p2p_parse_free(&msg);
1048 if (dev->go_state == REMOTE_GO && msg.group_id) {
1049 /* Store SSID for Provisioning step */
1050 p2p->ssid_len = msg.group_id_len - ETH_ALEN;
1051 os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
1052 } else if (dev->go_state == REMOTE_GO) {
1053 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1054 "P2P: Mandatory P2P Group ID attribute missing from "
1055 "GO Negotiation Confirmation");
1057 #ifdef CONFIG_P2P_STRICT
1058 p2p_parse_free(&msg);
1060 #endif /* CONFIG_P2P_STRICT */
1063 if (!msg.operating_channel) {
1064 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1065 "P2P: Mandatory Operating Channel attribute missing "
1066 "from GO Negotiation Confirmation");
1067 #ifdef CONFIG_P2P_STRICT
1068 p2p_parse_free(&msg);
1070 #endif /* CONFIG_P2P_STRICT */
1073 if (!msg.channel_list) {
1074 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1075 "P2P: Mandatory Operating Channel attribute missing "
1076 "from GO Negotiation Confirmation");
1077 #ifdef CONFIG_P2P_STRICT
1078 p2p_parse_free(&msg);
1080 #endif /* CONFIG_P2P_STRICT */
1083 p2p_parse_free(&msg);
1085 if (dev->go_state == UNKNOWN_GO) {
1087 * This should not happen since GO negotiation has already
1090 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1091 "P2P: Unexpected GO Neg state - do not know which end "
1096 p2p_go_complete(p2p, dev);