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 (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
, group_capab
);
165 p2p_buf_add_go_intent(buf
, (p2p
->go_intent
<< 1) |
166 p2p
->next_tie_breaker
);
167 p2p
->next_tie_breaker
= !p2p
->next_tie_breaker
;
168 p2p_buf_add_config_timeout(buf
, 100, 20);
169 p2p_buf_add_listen_channel(buf
, p2p
->cfg
->country
, p2p
->cfg
->reg_class
,
171 if (p2p
->ext_listen_interval
)
172 p2p_buf_add_ext_listen_timing(buf
, p2p
->ext_listen_period
,
173 p2p
->ext_listen_interval
);
174 p2p_buf_add_intended_addr(buf
, p2p
->intended_addr
);
175 p2p_buf_add_channel_list(buf
, p2p
->cfg
->country
, &p2p
->channels
);
176 p2p_buf_add_device_info(buf
, p2p
, peer
);
177 p2p_buf_add_operating_channel(buf
, p2p
->cfg
->country
,
178 p2p
->op_reg_class
, p2p
->op_channel
);
179 p2p_buf_update_ie_hdr(buf
, len
);
181 /* WPS IE with Device Password ID attribute */
182 p2p_build_wps_ie(p2p
, buf
, p2p_wps_method_pw_id(peer
->wps_method
), 0);
188 int p2p_connect_send(struct p2p_data
*p2p
, struct p2p_device
*dev
)
193 freq
= dev
->listen_freq
> 0 ? dev
->listen_freq
: dev
->oper_freq
;
195 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
196 "P2P: No Listen/Operating frequency known for the "
197 "peer " MACSTR
" to send GO Negotiation Request",
198 MAC2STR(dev
->info
.p2p_device_addr
));
202 req
= p2p_build_go_neg_req(p2p
, dev
);
205 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
206 "P2P: Sending GO Negotiation Request");
207 p2p_set_state(p2p
, P2P_CONNECT
);
208 p2p
->pending_action_state
= P2P_PENDING_GO_NEG_REQUEST
;
209 p2p
->go_neg_peer
= dev
;
210 dev
->flags
|= P2P_DEV_WAIT_GO_NEG_RESPONSE
;
212 if (p2p_send_action(p2p
, freq
, dev
->info
.p2p_device_addr
,
213 p2p
->cfg
->dev_addr
, dev
->info
.p2p_device_addr
,
214 wpabuf_head(req
), wpabuf_len(req
), 200) < 0) {
215 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
216 "P2P: Failed to send Action frame");
217 /* Use P2P find to recover and retry */
218 p2p_set_timeout(p2p
, 0, 0);
227 static struct wpabuf
* p2p_build_go_neg_resp(struct p2p_data
*p2p
,
228 struct p2p_device
*peer
,
229 u8 dialog_token
, u8 status
,
236 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
237 "P2P: Building GO Negotiation Response");
238 buf
= wpabuf_alloc(1000);
242 p2p_buf_add_public_action_hdr(buf
, P2P_GO_NEG_RESP
, dialog_token
);
244 len
= p2p_buf_add_ie_hdr(buf
);
245 p2p_buf_add_status(buf
, status
);
247 if (peer
&& peer
->go_state
== LOCAL_GO
) {
248 if (peer
->flags
& P2P_DEV_PREFER_PERSISTENT_GROUP
) {
249 group_capab
|= P2P_GROUP_CAPAB_PERSISTENT_GROUP
;
250 if (peer
->flags
& P2P_DEV_PREFER_PERSISTENT_RECONN
)
252 P2P_GROUP_CAPAB_PERSISTENT_RECONN
;
254 if (p2p
->cross_connect
)
255 group_capab
|= P2P_GROUP_CAPAB_CROSS_CONN
;
256 if (p2p
->cfg
->p2p_intra_bss
)
257 group_capab
|= P2P_GROUP_CAPAB_INTRA_BSS_DIST
;
259 p2p_buf_add_capability(buf
, p2p
->dev_capab
, group_capab
);
260 p2p_buf_add_go_intent(buf
, (p2p
->go_intent
<< 1) | tie_breaker
);
261 p2p_buf_add_config_timeout(buf
, 100, 20);
262 if (peer
&& peer
->go_state
== REMOTE_GO
) {
263 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Omit Operating "
264 "Channel attribute");
266 p2p_buf_add_operating_channel(buf
, p2p
->cfg
->country
,
270 p2p_buf_add_intended_addr(buf
, p2p
->intended_addr
);
271 if (status
|| peer
== NULL
) {
272 p2p_buf_add_channel_list(buf
, p2p
->cfg
->country
,
274 } else if (peer
->go_state
== REMOTE_GO
) {
275 p2p_buf_add_channel_list(buf
, p2p
->cfg
->country
,
278 struct p2p_channels res
;
279 p2p_channels_intersect(&p2p
->channels
, &peer
->channels
,
281 p2p_buf_add_channel_list(buf
, p2p
->cfg
->country
, &res
);
283 p2p_buf_add_device_info(buf
, p2p
, peer
);
284 if (peer
&& peer
->go_state
== LOCAL_GO
) {
285 p2p_buf_add_group_id(buf
, p2p
->cfg
->dev_addr
, p2p
->ssid
,
288 p2p_buf_update_ie_hdr(buf
, len
);
290 /* WPS IE with Device Password ID attribute */
291 p2p_build_wps_ie(p2p
, buf
,
292 p2p_wps_method_pw_id(peer
? peer
->wps_method
:
299 static void p2p_reselect_channel(struct p2p_data
*p2p
,
300 struct p2p_channels
*intersection
)
302 struct p2p_reg_class
*cl
;
304 u8 op_reg_class
, op_channel
;
306 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Selected operating "
307 "channel (reg_class %u channel %u) not acceptable to the "
308 "peer", p2p
->op_reg_class
, p2p
->op_channel
);
310 /* First, try to pick the best channel from another band */
311 freq
= p2p_channel_to_freq(p2p
->cfg
->country
, p2p
->op_reg_class
,
313 if (freq
>= 2400 && freq
< 2500 && p2p
->best_freq_5
> 0 &&
314 p2p_freq_to_channel(p2p
->cfg
->country
, p2p
->best_freq_5
,
315 &op_reg_class
, &op_channel
) == 0 &&
316 p2p_channels_includes(intersection
, op_reg_class
, op_channel
)) {
317 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Pick best 5 GHz "
318 "channel (reg_class %u channel %u) from intersection",
319 op_reg_class
, op_channel
);
320 p2p
->op_reg_class
= op_reg_class
;
321 p2p
->op_channel
= op_channel
;
325 if (freq
>= 4900 && freq
< 6000 && p2p
->best_freq_24
> 0 &&
326 p2p_freq_to_channel(p2p
->cfg
->country
, p2p
->best_freq_24
,
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 2.4 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
;
338 * Fall back to whatever is included in the channel intersection since
339 * no better options seems to be available.
341 cl
= &intersection
->reg_class
[0];
342 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Pick another channel "
343 "(reg_class %u channel %u) from intersection",
344 cl
->reg_class
, cl
->channel
[0]);
345 p2p
->op_reg_class
= cl
->reg_class
;
346 p2p
->op_channel
= cl
->channel
[0];
350 void p2p_process_go_neg_req(struct p2p_data
*p2p
, const u8
*sa
,
351 const u8
*data
, size_t len
, int rx_freq
)
353 struct p2p_device
*dev
= NULL
;
355 struct p2p_message msg
;
356 u8 status
= P2P_SC_FAIL_INVALID_PARAMS
;
360 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
361 "P2P: Received GO Negotiation Request from " MACSTR
362 "(freq=%d)", MAC2STR(sa
), rx_freq
);
364 if (p2p_parse(data
, len
, &msg
))
367 if (!msg
.capability
) {
368 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
369 "P2P: Mandatory Capability attribute missing from GO "
370 "Negotiation Request");
371 #ifdef CONFIG_P2P_STRICT
373 #endif /* CONFIG_P2P_STRICT */
377 tie_breaker
= *msg
.go_intent
& 0x01;
379 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
380 "P2P: Mandatory GO Intent attribute missing from GO "
381 "Negotiation Request");
382 #ifdef CONFIG_P2P_STRICT
384 #endif /* CONFIG_P2P_STRICT */
387 if (!msg
.config_timeout
) {
388 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
389 "P2P: Mandatory Configuration Timeout attribute "
390 "missing from GO Negotiation Request");
391 #ifdef CONFIG_P2P_STRICT
393 #endif /* CONFIG_P2P_STRICT */
396 if (!msg
.listen_channel
) {
397 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
398 "P2P: No Listen Channel attribute received");
401 if (!msg
.operating_channel
) {
402 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
403 "P2P: No Operating Channel attribute received");
406 if (!msg
.channel_list
) {
407 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
408 "P2P: No Channel List attribute received");
411 if (!msg
.intended_addr
) {
412 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
413 "P2P: No Intended P2P Interface Address attribute "
417 if (!msg
.p2p_device_info
) {
418 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
419 "P2P: No P2P Device Info attribute received");
423 if (os_memcmp(msg
.p2p_device_addr
, sa
, ETH_ALEN
) != 0) {
424 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
425 "P2P: Unexpected GO Negotiation Request SA=" MACSTR
426 " != dev_addr=" MACSTR
,
427 MAC2STR(sa
), MAC2STR(msg
.p2p_device_addr
));
431 dev
= p2p_get_device(p2p
, sa
);
433 if (msg
.status
&& *msg
.status
) {
434 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
435 "P2P: Unexpected Status attribute (%d) in GO "
436 "Negotiation Request", *msg
.status
);
441 dev
= p2p_add_dev_from_go_neg_req(p2p
, sa
, &msg
);
442 else if (dev
->flags
& P2P_DEV_PROBE_REQ_ONLY
)
443 p2p_add_dev_info(p2p
, sa
, dev
, &msg
);
444 if (dev
&& dev
->flags
& P2P_DEV_USER_REJECTED
) {
445 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
446 "P2P: User has rejected this peer");
447 status
= P2P_SC_FAIL_REJECTED_BY_USER
;
448 } else if (dev
== NULL
|| dev
->wps_method
== WPS_NOT_READY
) {
449 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
450 "P2P: Not ready for GO negotiation with " MACSTR
,
452 status
= P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
454 dev
->flags
|= P2P_DEV_PEER_WAITING_RESPONSE
;
455 p2p
->cfg
->go_neg_req_rx(p2p
->cfg
->cb_ctx
, sa
,
456 msg
.dev_password_id
);
457 } else if (p2p
->go_neg_peer
&& p2p
->go_neg_peer
!= dev
) {
458 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
459 "P2P: Already in Group Formation with another peer");
460 status
= P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
464 if (!p2p
->go_neg_peer
) {
465 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Starting "
466 "GO Negotiation with previously authorized "
468 if (!(dev
->flags
& P2P_DEV_FORCE_FREQ
)) {
469 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
470 "P2P: Use default channel settings");
471 p2p
->op_reg_class
= p2p
->cfg
->op_reg_class
;
472 p2p
->op_channel
= p2p
->cfg
->op_channel
;
473 os_memcpy(&p2p
->channels
, &p2p
->cfg
->channels
,
474 sizeof(struct p2p_channels
));
476 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
477 "P2P: Use previously configured "
478 "forced channel settings");
482 dev
->flags
&= ~P2P_DEV_NOT_YET_READY
;
484 if (!msg
.go_intent
) {
485 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
486 "P2P: No GO Intent attribute received");
489 if ((*msg
.go_intent
>> 1) > P2P_MAX_GO_INTENT
) {
490 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
491 "P2P: Invalid GO Intent value (%u) received",
492 *msg
.go_intent
>> 1);
496 if (dev
->go_neg_req_sent
&&
497 os_memcmp(sa
, p2p
->cfg
->dev_addr
, ETH_ALEN
) > 0) {
498 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
499 "P2P: Do not reply since peer has higher "
500 "address and GO Neg Request already sent");
501 p2p_parse_free(&msg
);
505 go
= p2p_go_det(p2p
->go_intent
, *msg
.go_intent
);
507 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
508 "P2P: Incompatible GO Intent");
509 status
= P2P_SC_FAIL_BOTH_GO_INTENT_15
;
513 if (p2p_peer_channels(p2p
, dev
, msg
.channel_list
,
514 msg
.channel_list_len
) < 0) {
515 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
516 "P2P: No common channels found");
517 status
= P2P_SC_FAIL_NO_COMMON_CHANNELS
;
521 switch (msg
.dev_password_id
) {
522 case DEV_PW_REGISTRAR_SPECIFIED
:
523 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
524 "P2P: PIN from peer Display");
525 if (dev
->wps_method
!= WPS_PIN_KEYPAD
) {
526 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
527 "P2P: We have wps_method=%s -> "
529 p2p_wps_method_str(dev
->wps_method
));
530 status
= P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD
;
534 case DEV_PW_USER_SPECIFIED
:
535 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
536 "P2P: Peer entered PIN on Keypad");
537 if (dev
->wps_method
!= WPS_PIN_DISPLAY
) {
538 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
539 "P2P: We have wps_method=%s -> "
541 p2p_wps_method_str(dev
->wps_method
));
542 status
= P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD
;
546 case DEV_PW_PUSHBUTTON
:
547 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
548 "P2P: Peer using pushbutton");
549 if (dev
->wps_method
!= WPS_PBC
) {
550 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
551 "P2P: We have wps_method=%s -> "
553 p2p_wps_method_str(dev
->wps_method
));
554 status
= P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD
;
559 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
560 "P2P: Unsupported Device Password ID %d",
561 msg
.dev_password_id
);
562 status
= P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD
;
567 struct p2p_channels intersection
;
569 p2p_channels_intersect(&p2p
->channels
, &dev
->channels
,
571 if (intersection
.reg_classes
== 0 ||
572 intersection
.reg_class
[0].channels
== 0) {
573 status
= P2P_SC_FAIL_NO_COMMON_CHANNELS
;
574 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
575 "P2P: No common channels found");
578 for (i
= 0; i
< intersection
.reg_classes
; i
++) {
579 struct p2p_reg_class
*c
;
580 c
= &intersection
.reg_class
[i
];
581 wpa_printf(MSG_DEBUG
, "P2P: reg_class %u",
583 wpa_hexdump(MSG_DEBUG
, "P2P: channels",
584 c
->channel
, c
->channels
);
586 if (!p2p_channels_includes(&intersection
,
589 p2p_reselect_channel(p2p
, &intersection
);
591 if (!p2p
->ssid_set
) {
592 p2p_build_ssid(p2p
, p2p
->ssid
, &p2p
->ssid_len
);
597 dev
->go_state
= go
? LOCAL_GO
: REMOTE_GO
;
598 dev
->oper_freq
= p2p_channel_to_freq((const char *)
599 msg
.operating_channel
,
600 msg
.operating_channel
[3],
601 msg
.operating_channel
[4]);
602 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Peer operating "
603 "channel preference: %d MHz", dev
->oper_freq
);
605 if (msg
.config_timeout
) {
606 dev
->go_timeout
= msg
.config_timeout
[0];
607 dev
->client_timeout
= msg
.config_timeout
[1];
610 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
611 "P2P: GO Negotiation with " MACSTR
, MAC2STR(sa
));
612 if (p2p
->state
!= P2P_IDLE
)
613 p2p_stop_find_for_freq(p2p
, rx_freq
);
614 p2p_set_state(p2p
, P2P_GO_NEG
);
615 p2p_clear_timeout(p2p
);
616 dev
->dialog_token
= msg
.dialog_token
;
617 os_memcpy(dev
->intended_addr
, msg
.intended_addr
, ETH_ALEN
);
618 p2p
->go_neg_peer
= dev
;
619 status
= P2P_SC_SUCCESS
;
624 dev
->status
= status
;
625 resp
= p2p_build_go_neg_resp(p2p
, dev
, msg
.dialog_token
, status
,
627 p2p_parse_free(&msg
);
630 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
631 "P2P: Sending GO Negotiation Response");
635 freq
= p2p_channel_to_freq(p2p
->cfg
->country
,
639 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
640 "P2P: Unknown regulatory class/channel");
644 if (status
== P2P_SC_SUCCESS
) {
645 p2p
->pending_action_state
= P2P_PENDING_GO_NEG_RESPONSE
;
646 dev
->flags
|= P2P_DEV_WAIT_GO_NEG_CONFIRM
;
648 p2p
->pending_action_state
=
649 P2P_PENDING_GO_NEG_RESPONSE_FAILURE
;
650 if (p2p_send_action(p2p
, freq
, sa
, p2p
->cfg
->dev_addr
,
652 wpabuf_head(resp
), wpabuf_len(resp
), 200) < 0) {
653 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
654 "P2P: Failed to send Action frame");
661 static struct wpabuf
* p2p_build_go_neg_conf(struct p2p_data
*p2p
,
662 struct p2p_device
*peer
,
663 u8 dialog_token
, u8 status
,
664 const u8
*resp_chan
, int go
)
668 struct p2p_channels res
;
671 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
672 "P2P: Building GO Negotiation Confirm");
673 buf
= wpabuf_alloc(1000);
677 p2p_buf_add_public_action_hdr(buf
, P2P_GO_NEG_CONF
, dialog_token
);
679 len
= p2p_buf_add_ie_hdr(buf
);
680 p2p_buf_add_status(buf
, status
);
682 if (peer
->go_state
== LOCAL_GO
) {
683 if (peer
->flags
& P2P_DEV_PREFER_PERSISTENT_GROUP
) {
684 group_capab
|= P2P_GROUP_CAPAB_PERSISTENT_GROUP
;
685 if (peer
->flags
& P2P_DEV_PREFER_PERSISTENT_RECONN
)
687 P2P_GROUP_CAPAB_PERSISTENT_RECONN
;
689 if (p2p
->cross_connect
)
690 group_capab
|= P2P_GROUP_CAPAB_CROSS_CONN
;
691 if (p2p
->cfg
->p2p_intra_bss
)
692 group_capab
|= P2P_GROUP_CAPAB_INTRA_BSS_DIST
;
694 p2p_buf_add_capability(buf
, p2p
->dev_capab
, group_capab
);
695 if (go
|| resp_chan
== NULL
)
696 p2p_buf_add_operating_channel(buf
, p2p
->cfg
->country
,
700 p2p_buf_add_operating_channel(buf
, (const char *) resp_chan
,
701 resp_chan
[3], resp_chan
[4]);
702 p2p_channels_intersect(&p2p
->channels
, &peer
->channels
, &res
);
703 p2p_buf_add_channel_list(buf
, p2p
->cfg
->country
, &res
);
705 p2p_buf_add_group_id(buf
, p2p
->cfg
->dev_addr
, p2p
->ssid
,
708 p2p_buf_update_ie_hdr(buf
, len
);
714 void p2p_process_go_neg_resp(struct p2p_data
*p2p
, const u8
*sa
,
715 const u8
*data
, size_t len
, int rx_freq
)
717 struct p2p_device
*dev
;
720 struct p2p_message msg
;
721 u8 status
= P2P_SC_SUCCESS
;
724 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
725 "P2P: Received GO Negotiation Response from " MACSTR
726 " (freq=%d)", MAC2STR(sa
), rx_freq
);
727 dev
= p2p_get_device(p2p
, sa
);
728 if (dev
== NULL
|| dev
->wps_method
== WPS_NOT_READY
||
729 dev
!= p2p
->go_neg_peer
) {
730 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
731 "P2P: Not ready for GO negotiation with " MACSTR
,
736 if (p2p_parse(data
, len
, &msg
))
739 if (!(dev
->flags
& P2P_DEV_WAIT_GO_NEG_RESPONSE
)) {
740 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
741 "P2P: Was not expecting GO Negotiation Response - "
743 p2p_parse_free(&msg
);
746 dev
->flags
&= ~P2P_DEV_WAIT_GO_NEG_RESPONSE
;
748 if (msg
.dialog_token
!= dev
->dialog_token
) {
749 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
750 "P2P: Unexpected Dialog Token %u (expected %u)",
751 msg
.dialog_token
, dev
->dialog_token
);
752 p2p_parse_free(&msg
);
757 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
758 "P2P: No Status attribute received");
759 status
= P2P_SC_FAIL_INVALID_PARAMS
;
763 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
764 "P2P: GO Negotiation rejected: status %d",
766 dev
->go_neg_req_sent
= 0;
767 if (*msg
.status
== P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
) {
768 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
769 "P2P: Wait for the peer to become ready for "
771 dev
->flags
|= P2P_DEV_NOT_YET_READY
;
773 p2p_set_state(p2p
, P2P_WAIT_PEER_IDLE
);
774 p2p_set_timeout(p2p
, 0, 0);
776 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
777 "P2P: Stop GO Negotiation attempt");
778 p2p_go_neg_failed(p2p
, dev
, *msg
.status
);
780 p2p
->cfg
->send_action_done(p2p
->cfg
->cb_ctx
);
781 p2p_parse_free(&msg
);
785 if (!msg
.capability
) {
786 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
787 "P2P: Mandatory Capability attribute missing from GO "
788 "Negotiation Response");
789 #ifdef CONFIG_P2P_STRICT
790 status
= P2P_SC_FAIL_INVALID_PARAMS
;
792 #endif /* CONFIG_P2P_STRICT */
795 if (!msg
.p2p_device_info
) {
796 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
797 "P2P: Mandatory P2P Device Info attribute missing "
798 "from GO Negotiation Response");
799 #ifdef CONFIG_P2P_STRICT
800 status
= P2P_SC_FAIL_INVALID_PARAMS
;
802 #endif /* CONFIG_P2P_STRICT */
805 if (!msg
.intended_addr
) {
806 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
807 "P2P: No Intended P2P Interface Address attribute "
809 status
= P2P_SC_FAIL_INVALID_PARAMS
;
813 if (!msg
.go_intent
) {
814 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
815 "P2P: No GO Intent attribute received");
816 status
= P2P_SC_FAIL_INVALID_PARAMS
;
819 if ((*msg
.go_intent
>> 1) > P2P_MAX_GO_INTENT
) {
820 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
821 "P2P: Invalid GO Intent value (%u) received",
822 *msg
.go_intent
>> 1);
823 status
= P2P_SC_FAIL_INVALID_PARAMS
;
827 go
= p2p_go_det(p2p
->go_intent
, *msg
.go_intent
);
829 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
830 "P2P: Incompatible GO Intent");
831 status
= P2P_SC_FAIL_INCOMPATIBLE_PARAMS
;
835 if (!go
&& msg
.group_id
) {
836 /* Store SSID for Provisioning step */
837 p2p
->ssid_len
= msg
.group_id_len
- ETH_ALEN
;
838 os_memcpy(p2p
->ssid
, msg
.group_id
+ ETH_ALEN
, p2p
->ssid_len
);
840 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
841 "P2P: Mandatory P2P Group ID attribute missing from "
842 "GO Negotiation Response");
844 #ifdef CONFIG_P2P_STRICT
845 status
= P2P_SC_FAIL_INVALID_PARAMS
;
847 #endif /* CONFIG_P2P_STRICT */
850 if (!msg
.config_timeout
) {
851 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
852 "P2P: Mandatory Configuration Timeout attribute "
853 "missing from GO Negotiation Response");
854 #ifdef CONFIG_P2P_STRICT
855 status
= P2P_SC_FAIL_INVALID_PARAMS
;
857 #endif /* CONFIG_P2P_STRICT */
859 dev
->go_timeout
= msg
.config_timeout
[0];
860 dev
->client_timeout
= msg
.config_timeout
[1];
863 if (!msg
.operating_channel
&& !go
) {
865 * Note: P2P Client may omit Operating Channel attribute to
866 * indicate it does not have a preference.
868 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
869 "P2P: No Operating Channel attribute received");
870 status
= P2P_SC_FAIL_INVALID_PARAMS
;
873 if (!msg
.channel_list
) {
874 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
875 "P2P: No Channel List attribute received");
876 status
= P2P_SC_FAIL_INVALID_PARAMS
;
880 if (p2p_peer_channels(p2p
, dev
, msg
.channel_list
,
881 msg
.channel_list_len
) < 0) {
882 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
883 "P2P: No common channels found");
884 status
= P2P_SC_FAIL_NO_COMMON_CHANNELS
;
888 if (msg
.operating_channel
) {
889 dev
->oper_freq
= p2p_channel_to_freq((const char *)
890 msg
.operating_channel
,
891 msg
.operating_channel
[3],
892 msg
.operating_channel
[4]);
893 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Peer operating "
894 "channel preference: %d MHz", dev
->oper_freq
);
898 switch (msg
.dev_password_id
) {
899 case DEV_PW_REGISTRAR_SPECIFIED
:
900 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
901 "P2P: PIN from peer Display");
902 if (dev
->wps_method
!= WPS_PIN_KEYPAD
) {
903 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
904 "P2P: We have wps_method=%s -> "
906 p2p_wps_method_str(dev
->wps_method
));
907 status
= P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD
;
911 case DEV_PW_USER_SPECIFIED
:
912 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
913 "P2P: Peer entered PIN on Keypad");
914 if (dev
->wps_method
!= WPS_PIN_DISPLAY
) {
915 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
916 "P2P: We have wps_method=%s -> "
918 p2p_wps_method_str(dev
->wps_method
));
919 status
= P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD
;
923 case DEV_PW_PUSHBUTTON
:
924 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
925 "P2P: Peer using pushbutton");
926 if (dev
->wps_method
!= WPS_PBC
) {
927 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
928 "P2P: We have wps_method=%s -> "
930 p2p_wps_method_str(dev
->wps_method
));
931 status
= P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD
;
936 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
937 "P2P: Unsupported Device Password ID %d",
938 msg
.dev_password_id
);
939 status
= P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD
;
944 struct p2p_channels intersection
;
946 p2p_channels_intersect(&p2p
->channels
, &dev
->channels
,
948 if (intersection
.reg_classes
== 0 ||
949 intersection
.reg_class
[0].channels
== 0) {
950 status
= P2P_SC_FAIL_NO_COMMON_CHANNELS
;
951 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
952 "P2P: No common channels found");
955 for (i
= 0; i
< intersection
.reg_classes
; i
++) {
956 struct p2p_reg_class
*c
;
957 c
= &intersection
.reg_class
[i
];
958 wpa_printf(MSG_DEBUG
, "P2P: reg_class %u",
960 wpa_hexdump(MSG_DEBUG
, "P2P: channels",
961 c
->channel
, c
->channels
);
963 if (!p2p_channels_includes(&intersection
, p2p
->op_reg_class
,
965 p2p_reselect_channel(p2p
, &intersection
);
967 if (!p2p
->ssid_set
) {
968 p2p_build_ssid(p2p
, p2p
->ssid
, &p2p
->ssid_len
);
973 p2p_set_state(p2p
, P2P_GO_NEG
);
974 p2p_clear_timeout(p2p
);
976 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
977 "P2P: GO Negotiation with " MACSTR
, MAC2STR(sa
));
978 os_memcpy(dev
->intended_addr
, msg
.intended_addr
, ETH_ALEN
);
981 conf
= p2p_build_go_neg_conf(p2p
, dev
, msg
.dialog_token
, status
,
982 msg
.operating_channel
, go
);
983 p2p_parse_free(&msg
);
986 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
987 "P2P: Sending GO Negotiation Confirm");
988 if (status
== P2P_SC_SUCCESS
) {
989 p2p
->pending_action_state
= P2P_PENDING_GO_NEG_CONFIRM
;
990 dev
->go_state
= go
? LOCAL_GO
: REMOTE_GO
;
992 p2p
->pending_action_state
= P2P_NO_PENDING_ACTION
;
996 freq
= dev
->listen_freq
;
997 if (p2p_send_action(p2p
, freq
, sa
, p2p
->cfg
->dev_addr
, sa
,
998 wpabuf_head(conf
), wpabuf_len(conf
), 200) < 0) {
999 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1000 "P2P: Failed to send Action frame");
1001 p2p_go_neg_failed(p2p
, dev
, -1);
1007 void p2p_process_go_neg_conf(struct p2p_data
*p2p
, const u8
*sa
,
1008 const u8
*data
, size_t len
)
1010 struct p2p_device
*dev
;
1011 struct p2p_message msg
;
1013 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1014 "P2P: Received GO Negotiation Confirm from " MACSTR
,
1016 dev
= p2p_get_device(p2p
, sa
);
1017 if (dev
== NULL
|| dev
->wps_method
== WPS_NOT_READY
||
1018 dev
!= p2p
->go_neg_peer
) {
1019 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1020 "P2P: Not ready for GO negotiation with " MACSTR
,
1025 if (p2p
->pending_action_state
== P2P_PENDING_GO_NEG_RESPONSE
) {
1026 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Stopped waiting "
1027 "for TX status on GO Negotiation Response since we "
1028 "already received Confirmation");
1029 p2p
->pending_action_state
= P2P_NO_PENDING_ACTION
;
1032 if (p2p_parse(data
, len
, &msg
))
1035 if (!(dev
->flags
& P2P_DEV_WAIT_GO_NEG_CONFIRM
)) {
1036 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1037 "P2P: Was not expecting GO Negotiation Confirm - "
1041 dev
->flags
&= ~P2P_DEV_WAIT_GO_NEG_CONFIRM
;
1043 if (msg
.dialog_token
!= dev
->dialog_token
) {
1044 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1045 "P2P: Unexpected Dialog Token %u (expected %u)",
1046 msg
.dialog_token
, dev
->dialog_token
);
1047 p2p_parse_free(&msg
);
1052 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1053 "P2P: No Status attribute received");
1054 p2p_parse_free(&msg
);
1058 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1059 "P2P: GO Negotiation rejected: status %d",
1061 p2p_parse_free(&msg
);
1065 if (dev
->go_state
== REMOTE_GO
&& msg
.group_id
) {
1066 /* Store SSID for Provisioning step */
1067 p2p
->ssid_len
= msg
.group_id_len
- ETH_ALEN
;
1068 os_memcpy(p2p
->ssid
, msg
.group_id
+ ETH_ALEN
, p2p
->ssid_len
);
1069 } else if (dev
->go_state
== REMOTE_GO
) {
1070 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1071 "P2P: Mandatory P2P Group ID attribute missing from "
1072 "GO Negotiation Confirmation");
1074 #ifdef CONFIG_P2P_STRICT
1075 p2p_parse_free(&msg
);
1077 #endif /* CONFIG_P2P_STRICT */
1080 if (!msg
.operating_channel
) {
1081 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1082 "P2P: Mandatory Operating Channel attribute missing "
1083 "from GO Negotiation Confirmation");
1084 #ifdef CONFIG_P2P_STRICT
1085 p2p_parse_free(&msg
);
1087 #endif /* CONFIG_P2P_STRICT */
1090 if (!msg
.channel_list
) {
1091 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1092 "P2P: Mandatory Operating Channel attribute missing "
1093 "from GO Negotiation Confirmation");
1094 #ifdef CONFIG_P2P_STRICT
1095 p2p_parse_free(&msg
);
1097 #endif /* CONFIG_P2P_STRICT */
1100 p2p_parse_free(&msg
);
1102 if (dev
->go_state
== UNKNOWN_GO
) {
1104 * This should not happen since GO negotiation has already
1107 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1108 "P2P: Unexpected GO Neg state - do not know which end "
1113 p2p_go_complete(p2p
, dev
);