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 p2p_reselect_channel(p2p
, &intersection
);
448 if (!p2p
->ssid_set
) {
449 p2p_build_ssid(p2p
, p2p
->ssid
, &p2p
->ssid_len
);
457 void p2p_process_go_neg_req(struct p2p_data
*p2p
, const u8
*sa
,
458 const u8
*data
, size_t len
, int rx_freq
)
460 struct p2p_device
*dev
= NULL
;
462 struct p2p_message msg
;
463 u8 status
= P2P_SC_FAIL_INVALID_PARAMS
;
467 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
468 "P2P: Received GO Negotiation Request from " MACSTR
469 "(freq=%d)", MAC2STR(sa
), rx_freq
);
471 if (p2p_parse(data
, len
, &msg
))
474 if (!msg
.capability
) {
475 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
476 "P2P: Mandatory Capability attribute missing from GO "
477 "Negotiation Request");
478 #ifdef CONFIG_P2P_STRICT
480 #endif /* CONFIG_P2P_STRICT */
484 tie_breaker
= *msg
.go_intent
& 0x01;
486 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
487 "P2P: Mandatory GO Intent attribute missing from GO "
488 "Negotiation Request");
489 #ifdef CONFIG_P2P_STRICT
491 #endif /* CONFIG_P2P_STRICT */
494 if (!msg
.config_timeout
) {
495 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
496 "P2P: Mandatory Configuration Timeout attribute "
497 "missing from GO Negotiation Request");
498 #ifdef CONFIG_P2P_STRICT
500 #endif /* CONFIG_P2P_STRICT */
503 if (!msg
.listen_channel
) {
504 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
505 "P2P: No Listen Channel attribute received");
508 if (!msg
.operating_channel
) {
509 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
510 "P2P: No Operating Channel attribute received");
513 if (!msg
.channel_list
) {
514 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
515 "P2P: No Channel List attribute received");
518 if (!msg
.intended_addr
) {
519 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
520 "P2P: No Intended P2P Interface Address attribute "
524 if (!msg
.p2p_device_info
) {
525 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
526 "P2P: No P2P Device Info attribute received");
530 if (os_memcmp(msg
.p2p_device_addr
, sa
, ETH_ALEN
) != 0) {
531 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
532 "P2P: Unexpected GO Negotiation Request SA=" MACSTR
533 " != dev_addr=" MACSTR
,
534 MAC2STR(sa
), MAC2STR(msg
.p2p_device_addr
));
538 dev
= p2p_get_device(p2p
, sa
);
540 if (msg
.status
&& *msg
.status
) {
541 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
542 "P2P: Unexpected Status attribute (%d) in GO "
543 "Negotiation Request", *msg
.status
);
548 dev
= p2p_add_dev_from_go_neg_req(p2p
, sa
, &msg
);
549 else if (dev
->flags
& P2P_DEV_PROBE_REQ_ONLY
)
550 p2p_add_dev_info(p2p
, sa
, dev
, &msg
);
551 if (dev
&& dev
->flags
& P2P_DEV_USER_REJECTED
) {
552 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
553 "P2P: User has rejected this peer");
554 status
= P2P_SC_FAIL_REJECTED_BY_USER
;
555 } else if (dev
== NULL
|| dev
->wps_method
== WPS_NOT_READY
) {
556 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
557 "P2P: Not ready for GO negotiation with " MACSTR
,
559 status
= P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
561 dev
->flags
|= P2P_DEV_PEER_WAITING_RESPONSE
;
562 p2p
->cfg
->go_neg_req_rx(p2p
->cfg
->cb_ctx
, sa
,
563 msg
.dev_password_id
);
564 } else if (p2p
->go_neg_peer
&& p2p
->go_neg_peer
!= dev
) {
565 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
566 "P2P: Already in Group Formation with another peer");
567 status
= P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
571 if (!p2p
->go_neg_peer
) {
572 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Starting "
573 "GO Negotiation with previously authorized "
575 if (!(dev
->flags
& P2P_DEV_FORCE_FREQ
)) {
576 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
577 "P2P: Use default channel settings");
578 p2p
->op_reg_class
= p2p
->cfg
->op_reg_class
;
579 p2p
->op_channel
= p2p
->cfg
->op_channel
;
580 os_memcpy(&p2p
->channels
, &p2p
->cfg
->channels
,
581 sizeof(struct p2p_channels
));
583 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
584 "P2P: Use previously configured "
585 "forced channel settings");
589 dev
->flags
&= ~P2P_DEV_NOT_YET_READY
;
591 if (!msg
.go_intent
) {
592 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
593 "P2P: No GO Intent attribute received");
596 if ((*msg
.go_intent
>> 1) > P2P_MAX_GO_INTENT
) {
597 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
598 "P2P: Invalid GO Intent value (%u) received",
599 *msg
.go_intent
>> 1);
603 if (dev
->go_neg_req_sent
&&
604 os_memcmp(sa
, p2p
->cfg
->dev_addr
, ETH_ALEN
) > 0) {
605 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
606 "P2P: Do not reply since peer has higher "
607 "address and GO Neg Request already sent");
608 p2p_parse_free(&msg
);
612 go
= p2p_go_det(p2p
->go_intent
, *msg
.go_intent
);
614 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
615 "P2P: Incompatible GO Intent");
616 status
= P2P_SC_FAIL_BOTH_GO_INTENT_15
;
620 if (p2p_peer_channels(p2p
, dev
, msg
.channel_list
,
621 msg
.channel_list_len
) < 0) {
622 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
623 "P2P: No common channels found");
624 status
= P2P_SC_FAIL_NO_COMMON_CHANNELS
;
628 switch (msg
.dev_password_id
) {
629 case DEV_PW_REGISTRAR_SPECIFIED
:
630 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
631 "P2P: PIN from peer Display");
632 if (dev
->wps_method
!= WPS_PIN_KEYPAD
) {
633 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
634 "P2P: We have wps_method=%s -> "
636 p2p_wps_method_str(dev
->wps_method
));
637 status
= P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD
;
641 case DEV_PW_USER_SPECIFIED
:
642 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
643 "P2P: Peer entered PIN on Keypad");
644 if (dev
->wps_method
!= WPS_PIN_DISPLAY
) {
645 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
646 "P2P: We have wps_method=%s -> "
648 p2p_wps_method_str(dev
->wps_method
));
649 status
= P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD
;
653 case DEV_PW_PUSHBUTTON
:
654 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
655 "P2P: Peer using pushbutton");
656 if (dev
->wps_method
!= WPS_PBC
) {
657 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
658 "P2P: We have wps_method=%s -> "
660 p2p_wps_method_str(dev
->wps_method
));
661 status
= P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD
;
666 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
667 "P2P: Unsupported Device Password ID %d",
668 msg
.dev_password_id
);
669 status
= P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD
;
673 if (go
&& p2p_go_select_channel(p2p
, dev
, &status
) < 0)
676 dev
->go_state
= go
? LOCAL_GO
: REMOTE_GO
;
677 dev
->oper_freq
= p2p_channel_to_freq((const char *)
678 msg
.operating_channel
,
679 msg
.operating_channel
[3],
680 msg
.operating_channel
[4]);
681 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Peer operating "
682 "channel preference: %d MHz", dev
->oper_freq
);
684 if (msg
.config_timeout
) {
685 dev
->go_timeout
= msg
.config_timeout
[0];
686 dev
->client_timeout
= msg
.config_timeout
[1];
689 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
690 "P2P: GO Negotiation with " MACSTR
, MAC2STR(sa
));
691 if (p2p
->state
!= P2P_IDLE
)
692 p2p_stop_find_for_freq(p2p
, rx_freq
);
693 p2p_set_state(p2p
, P2P_GO_NEG
);
694 p2p_clear_timeout(p2p
);
695 dev
->dialog_token
= msg
.dialog_token
;
696 os_memcpy(dev
->intended_addr
, msg
.intended_addr
, ETH_ALEN
);
697 p2p
->go_neg_peer
= dev
;
698 status
= P2P_SC_SUCCESS
;
703 dev
->status
= status
;
704 resp
= p2p_build_go_neg_resp(p2p
, dev
, msg
.dialog_token
, status
,
706 p2p_parse_free(&msg
);
709 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
710 "P2P: Sending GO Negotiation Response");
714 freq
= p2p_channel_to_freq(p2p
->cfg
->country
,
718 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
719 "P2P: Unknown regulatory class/channel");
723 if (status
== P2P_SC_SUCCESS
) {
724 p2p
->pending_action_state
= P2P_PENDING_GO_NEG_RESPONSE
;
725 dev
->flags
|= P2P_DEV_WAIT_GO_NEG_CONFIRM
;
726 if (os_memcmp(sa
, p2p
->cfg
->dev_addr
, ETH_ALEN
) < 0) {
728 * Peer has smaller address, so the GO Negotiation
729 * Response from us is expected to complete
730 * negotiation. Ignore a GO Negotiation Response from
731 * the peer if it happens to be received after this
732 * point due to a race condition in GO Negotiation
733 * Request transmission and processing.
735 dev
->flags
&= ~P2P_DEV_WAIT_GO_NEG_RESPONSE
;
738 p2p
->pending_action_state
=
739 P2P_PENDING_GO_NEG_RESPONSE_FAILURE
;
740 if (p2p_send_action(p2p
, freq
, sa
, p2p
->cfg
->dev_addr
,
742 wpabuf_head(resp
), wpabuf_len(resp
), 250) < 0) {
743 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
744 "P2P: Failed to send Action frame");
751 static struct wpabuf
* p2p_build_go_neg_conf(struct p2p_data
*p2p
,
752 struct p2p_device
*peer
,
753 u8 dialog_token
, u8 status
,
754 const u8
*resp_chan
, int go
)
758 struct p2p_channels res
;
762 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
763 "P2P: Building GO Negotiation Confirm");
765 #ifdef CONFIG_WIFI_DISPLAY
766 if (p2p
->wfd_ie_go_neg
)
767 extra
= wpabuf_len(p2p
->wfd_ie_go_neg
);
768 #endif /* CONFIG_WIFI_DISPLAY */
770 buf
= wpabuf_alloc(1000 + extra
);
774 p2p_buf_add_public_action_hdr(buf
, P2P_GO_NEG_CONF
, dialog_token
);
776 len
= p2p_buf_add_ie_hdr(buf
);
777 p2p_buf_add_status(buf
, status
);
779 if (peer
->go_state
== LOCAL_GO
) {
780 if (peer
->flags
& P2P_DEV_PREFER_PERSISTENT_GROUP
) {
781 group_capab
|= P2P_GROUP_CAPAB_PERSISTENT_GROUP
;
782 if (peer
->flags
& P2P_DEV_PREFER_PERSISTENT_RECONN
)
784 P2P_GROUP_CAPAB_PERSISTENT_RECONN
;
786 if (p2p
->cross_connect
)
787 group_capab
|= P2P_GROUP_CAPAB_CROSS_CONN
;
788 if (p2p
->cfg
->p2p_intra_bss
)
789 group_capab
|= P2P_GROUP_CAPAB_INTRA_BSS_DIST
;
791 p2p_buf_add_capability(buf
, p2p
->dev_capab
&
792 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY
,
794 if (go
|| resp_chan
== NULL
)
795 p2p_buf_add_operating_channel(buf
, p2p
->cfg
->country
,
799 p2p_buf_add_operating_channel(buf
, (const char *) resp_chan
,
800 resp_chan
[3], resp_chan
[4]);
801 p2p_channels_intersect(&p2p
->channels
, &peer
->channels
, &res
);
802 p2p_buf_add_channel_list(buf
, p2p
->cfg
->country
, &res
);
804 p2p_buf_add_group_id(buf
, p2p
->cfg
->dev_addr
, p2p
->ssid
,
807 p2p_buf_update_ie_hdr(buf
, len
);
809 #ifdef CONFIG_WIFI_DISPLAY
810 if (p2p
->wfd_ie_go_neg
)
811 wpabuf_put_buf(buf
, p2p
->wfd_ie_go_neg
);
812 #endif /* CONFIG_WIFI_DISPLAY */
818 void p2p_process_go_neg_resp(struct p2p_data
*p2p
, const u8
*sa
,
819 const u8
*data
, size_t len
, int rx_freq
)
821 struct p2p_device
*dev
;
824 struct p2p_message msg
;
825 u8 status
= P2P_SC_SUCCESS
;
828 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
829 "P2P: Received GO Negotiation Response from " MACSTR
830 " (freq=%d)", MAC2STR(sa
), rx_freq
);
831 dev
= p2p_get_device(p2p
, sa
);
832 if (dev
== NULL
|| dev
->wps_method
== WPS_NOT_READY
||
833 dev
!= p2p
->go_neg_peer
) {
834 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
835 "P2P: Not ready for GO negotiation with " MACSTR
,
840 if (p2p_parse(data
, len
, &msg
))
843 if (!(dev
->flags
& P2P_DEV_WAIT_GO_NEG_RESPONSE
)) {
844 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
845 "P2P: Was not expecting GO Negotiation Response - "
847 p2p_parse_free(&msg
);
850 dev
->flags
&= ~P2P_DEV_WAIT_GO_NEG_RESPONSE
;
852 if (msg
.dialog_token
!= dev
->dialog_token
) {
853 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
854 "P2P: Unexpected Dialog Token %u (expected %u)",
855 msg
.dialog_token
, dev
->dialog_token
);
856 p2p_parse_free(&msg
);
861 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
862 "P2P: No Status attribute received");
863 status
= P2P_SC_FAIL_INVALID_PARAMS
;
867 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
868 "P2P: GO Negotiation rejected: status %d",
870 dev
->go_neg_req_sent
= 0;
871 if (*msg
.status
== P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
) {
872 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
873 "P2P: Wait for the peer to become ready for "
875 dev
->flags
|= P2P_DEV_NOT_YET_READY
;
877 p2p_set_state(p2p
, P2P_WAIT_PEER_IDLE
);
878 p2p_set_timeout(p2p
, 0, 0);
880 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
881 "P2P: Stop GO Negotiation attempt");
882 p2p_go_neg_failed(p2p
, dev
, *msg
.status
);
884 p2p
->cfg
->send_action_done(p2p
->cfg
->cb_ctx
);
885 p2p_parse_free(&msg
);
889 if (!msg
.capability
) {
890 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
891 "P2P: Mandatory Capability attribute missing from GO "
892 "Negotiation Response");
893 #ifdef CONFIG_P2P_STRICT
894 status
= P2P_SC_FAIL_INVALID_PARAMS
;
896 #endif /* CONFIG_P2P_STRICT */
899 if (!msg
.p2p_device_info
) {
900 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
901 "P2P: Mandatory P2P Device Info attribute missing "
902 "from GO Negotiation Response");
903 #ifdef CONFIG_P2P_STRICT
904 status
= P2P_SC_FAIL_INVALID_PARAMS
;
906 #endif /* CONFIG_P2P_STRICT */
909 if (!msg
.intended_addr
) {
910 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
911 "P2P: No Intended P2P Interface Address attribute "
913 status
= P2P_SC_FAIL_INVALID_PARAMS
;
917 if (!msg
.go_intent
) {
918 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
919 "P2P: No GO Intent attribute received");
920 status
= P2P_SC_FAIL_INVALID_PARAMS
;
923 if ((*msg
.go_intent
>> 1) > P2P_MAX_GO_INTENT
) {
924 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
925 "P2P: Invalid GO Intent value (%u) received",
926 *msg
.go_intent
>> 1);
927 status
= P2P_SC_FAIL_INVALID_PARAMS
;
931 go
= p2p_go_det(p2p
->go_intent
, *msg
.go_intent
);
933 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
934 "P2P: Incompatible GO Intent");
935 status
= P2P_SC_FAIL_INCOMPATIBLE_PARAMS
;
939 if (!go
&& msg
.group_id
) {
940 /* Store SSID for Provisioning step */
941 p2p
->ssid_len
= msg
.group_id_len
- ETH_ALEN
;
942 os_memcpy(p2p
->ssid
, msg
.group_id
+ ETH_ALEN
, p2p
->ssid_len
);
944 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
945 "P2P: Mandatory P2P Group ID attribute missing from "
946 "GO Negotiation Response");
948 #ifdef CONFIG_P2P_STRICT
949 status
= P2P_SC_FAIL_INVALID_PARAMS
;
951 #endif /* CONFIG_P2P_STRICT */
954 if (!msg
.config_timeout
) {
955 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
956 "P2P: Mandatory Configuration Timeout attribute "
957 "missing from GO Negotiation Response");
958 #ifdef CONFIG_P2P_STRICT
959 status
= P2P_SC_FAIL_INVALID_PARAMS
;
961 #endif /* CONFIG_P2P_STRICT */
963 dev
->go_timeout
= msg
.config_timeout
[0];
964 dev
->client_timeout
= msg
.config_timeout
[1];
967 if (!msg
.operating_channel
&& !go
) {
969 * Note: P2P Client may omit Operating Channel attribute to
970 * indicate it does not have a preference.
972 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
973 "P2P: No Operating Channel attribute received");
974 status
= P2P_SC_FAIL_INVALID_PARAMS
;
977 if (!msg
.channel_list
) {
978 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
979 "P2P: No Channel List attribute received");
980 status
= P2P_SC_FAIL_INVALID_PARAMS
;
984 if (p2p_peer_channels(p2p
, dev
, msg
.channel_list
,
985 msg
.channel_list_len
) < 0) {
986 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
987 "P2P: No common channels found");
988 status
= P2P_SC_FAIL_NO_COMMON_CHANNELS
;
992 if (msg
.operating_channel
) {
993 dev
->oper_freq
= p2p_channel_to_freq((const char *)
994 msg
.operating_channel
,
995 msg
.operating_channel
[3],
996 msg
.operating_channel
[4]);
997 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Peer operating "
998 "channel preference: %d MHz", dev
->oper_freq
);
1002 switch (msg
.dev_password_id
) {
1003 case DEV_PW_REGISTRAR_SPECIFIED
:
1004 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1005 "P2P: PIN from peer Display");
1006 if (dev
->wps_method
!= WPS_PIN_KEYPAD
) {
1007 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1008 "P2P: We have wps_method=%s -> "
1010 p2p_wps_method_str(dev
->wps_method
));
1011 status
= P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD
;
1015 case DEV_PW_USER_SPECIFIED
:
1016 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1017 "P2P: Peer entered PIN on Keypad");
1018 if (dev
->wps_method
!= WPS_PIN_DISPLAY
) {
1019 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1020 "P2P: We have wps_method=%s -> "
1022 p2p_wps_method_str(dev
->wps_method
));
1023 status
= P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD
;
1027 case DEV_PW_PUSHBUTTON
:
1028 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1029 "P2P: Peer using pushbutton");
1030 if (dev
->wps_method
!= WPS_PBC
) {
1031 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1032 "P2P: We have wps_method=%s -> "
1034 p2p_wps_method_str(dev
->wps_method
));
1035 status
= P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD
;
1040 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1041 "P2P: Unsupported Device Password ID %d",
1042 msg
.dev_password_id
);
1043 status
= P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD
;
1047 if (go
&& p2p_go_select_channel(p2p
, dev
, &status
) < 0)
1050 p2p_set_state(p2p
, P2P_GO_NEG
);
1051 p2p_clear_timeout(p2p
);
1053 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1054 "P2P: GO Negotiation with " MACSTR
, MAC2STR(sa
));
1055 os_memcpy(dev
->intended_addr
, msg
.intended_addr
, ETH_ALEN
);
1058 conf
= p2p_build_go_neg_conf(p2p
, dev
, msg
.dialog_token
, status
,
1059 msg
.operating_channel
, go
);
1060 p2p_parse_free(&msg
);
1063 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1064 "P2P: Sending GO Negotiation Confirm");
1065 if (status
== P2P_SC_SUCCESS
) {
1066 p2p
->pending_action_state
= P2P_PENDING_GO_NEG_CONFIRM
;
1067 dev
->go_state
= go
? LOCAL_GO
: REMOTE_GO
;
1069 p2p
->pending_action_state
= P2P_NO_PENDING_ACTION
;
1073 freq
= dev
->listen_freq
;
1074 if (p2p_send_action(p2p
, freq
, sa
, p2p
->cfg
->dev_addr
, sa
,
1075 wpabuf_head(conf
), wpabuf_len(conf
), 0) < 0) {
1076 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1077 "P2P: Failed to send Action frame");
1078 p2p_go_neg_failed(p2p
, dev
, -1);
1084 void p2p_process_go_neg_conf(struct p2p_data
*p2p
, const u8
*sa
,
1085 const u8
*data
, size_t len
)
1087 struct p2p_device
*dev
;
1088 struct p2p_message msg
;
1090 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1091 "P2P: Received GO Negotiation Confirm from " MACSTR
,
1093 dev
= p2p_get_device(p2p
, sa
);
1094 if (dev
== NULL
|| dev
->wps_method
== WPS_NOT_READY
||
1095 dev
!= p2p
->go_neg_peer
) {
1096 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1097 "P2P: Not ready for GO negotiation with " MACSTR
,
1102 if (p2p
->pending_action_state
== P2P_PENDING_GO_NEG_RESPONSE
) {
1103 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Stopped waiting "
1104 "for TX status on GO Negotiation Response since we "
1105 "already received Confirmation");
1106 p2p
->pending_action_state
= P2P_NO_PENDING_ACTION
;
1109 if (p2p_parse(data
, len
, &msg
))
1112 if (!(dev
->flags
& P2P_DEV_WAIT_GO_NEG_CONFIRM
)) {
1113 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1114 "P2P: Was not expecting GO Negotiation Confirm - "
1118 dev
->flags
&= ~P2P_DEV_WAIT_GO_NEG_CONFIRM
;
1120 if (msg
.dialog_token
!= dev
->dialog_token
) {
1121 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1122 "P2P: Unexpected Dialog Token %u (expected %u)",
1123 msg
.dialog_token
, dev
->dialog_token
);
1124 p2p_parse_free(&msg
);
1129 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1130 "P2P: No Status attribute received");
1131 p2p_parse_free(&msg
);
1135 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1136 "P2P: GO Negotiation rejected: status %d",
1138 p2p_parse_free(&msg
);
1142 if (dev
->go_state
== REMOTE_GO
&& msg
.group_id
) {
1143 /* Store SSID for Provisioning step */
1144 p2p
->ssid_len
= msg
.group_id_len
- ETH_ALEN
;
1145 os_memcpy(p2p
->ssid
, msg
.group_id
+ ETH_ALEN
, p2p
->ssid_len
);
1146 } else if (dev
->go_state
== REMOTE_GO
) {
1147 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1148 "P2P: Mandatory P2P Group ID attribute missing from "
1149 "GO Negotiation Confirmation");
1151 #ifdef CONFIG_P2P_STRICT
1152 p2p_parse_free(&msg
);
1154 #endif /* CONFIG_P2P_STRICT */
1157 if (!msg
.operating_channel
) {
1158 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1159 "P2P: Mandatory Operating Channel attribute missing "
1160 "from GO Negotiation Confirmation");
1161 #ifdef CONFIG_P2P_STRICT
1162 p2p_parse_free(&msg
);
1164 #endif /* CONFIG_P2P_STRICT */
1167 if (!msg
.channel_list
) {
1168 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1169 "P2P: Mandatory Operating Channel attribute missing "
1170 "from GO Negotiation Confirmation");
1171 #ifdef CONFIG_P2P_STRICT
1172 p2p_parse_free(&msg
);
1174 #endif /* CONFIG_P2P_STRICT */
1177 p2p_parse_free(&msg
);
1179 if (dev
->go_state
== UNKNOWN_GO
) {
1181 * This should not happen since GO negotiation has already
1184 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1185 "P2P: Unexpected GO Neg state - do not know which end "
1191 * The peer could have missed our ctrl::ack frame for GO Negotiation
1192 * Confirm and continue retransmitting the frame. To reduce the
1193 * likelihood of the peer not getting successful TX status for the
1194 * GO Negotiation Confirm frame, wait a short time here before starting
1195 * the group so that we will remain on the current channel to
1196 * acknowledge any possible retransmission from the peer.
1198 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: 20 ms wait on current "
1199 "channel before starting group");
1202 p2p_go_complete(p2p
, dev
);