6efefe44054ded5d9ca8de122ca439a037283844
[mech_eap.git] / src / p2p / p2p_go_neg.c
1 /*
2  * Wi-Fi Direct - P2P Group Owner Negotiation
3  * Copyright (c) 2009-2010, Atheros Communications
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "includes.h"
10
11 #include "common.h"
12 #include "common/ieee802_11_defs.h"
13 #include "wps/wps_defs.h"
14 #include "p2p_i.h"
15 #include "p2p.h"
16
17
18 static int p2p_go_det(u8 own_intent, u8 peer_value)
19 {
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 */
24
25                 /* Use tie breaker bit to determine GO */
26                 return (peer_value & 0x01) ? 0 : 1;
27         }
28
29         return own_intent > peer_intent;
30 }
31
32
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)
36 {
37         const u8 *pos, *end;
38         struct p2p_channels *ch;
39         size_t channels;
40         struct p2p_channels intersection;
41
42         ch = &dev->channels;
43         os_memset(ch, 0, sizeof(*ch));
44         pos = channel_list;
45         end = channel_list + channel_list_len;
46
47         if (end - pos < 3)
48                 return -1;
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],
55                         pos[0], pos[1]);
56                 return -1;
57         }
58         pos += 3;
59
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");
66                         return -1;
67                 }
68                 channels = *pos++;
69                 cl->channels = channels > P2P_MAX_REG_CLASS_CHANNELS ?
70                         P2P_MAX_REG_CLASS_CHANNELS : channels;
71                 os_memcpy(cl->channel, pos, cl->channels);
72                 pos += channels;
73                 ch->reg_classes++;
74                 if (ch->reg_classes == P2P_MAX_REG_CLASSES)
75                         break;
76         }
77
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");
87                 return -1;
88         }
89         return 0;
90 }
91
92
93 static int p2p_peer_channels(struct p2p_data *p2p, struct p2p_device *dev,
94                              const u8 *channel_list, size_t channel_list_len)
95 {
96         return p2p_peer_channels_check(p2p, &p2p->channels, dev,
97                                        channel_list, channel_list_len);
98 }
99
100
101 u16 p2p_wps_method_pw_id(enum p2p_wps_method wps_method)
102 {
103         switch (wps_method) {
104         case WPS_PIN_DISPLAY:
105                 return DEV_PW_REGISTRAR_SPECIFIED;
106         case WPS_PIN_KEYPAD:
107                 return DEV_PW_USER_SPECIFIED;
108         case WPS_PBC:
109                 return DEV_PW_PUSHBUTTON;
110         default:
111                 return DEV_PW_DEFAULT;
112         }
113 }
114
115
116 static const char * p2p_wps_method_str(enum p2p_wps_method wps_method)
117 {
118         switch (wps_method) {
119         case WPS_PIN_DISPLAY:
120                 return "Display";
121         case WPS_PIN_KEYPAD:
122                 return "Keypad";
123         case WPS_PBC:
124                 return "PBC";
125         default:
126                 return "??";
127         }
128 }
129
130
131 static struct wpabuf * p2p_build_go_neg_req(struct p2p_data *p2p,
132                                             struct p2p_device *peer)
133 {
134         struct wpabuf *buf;
135         u8 *len;
136         u8 group_capab;
137         size_t extra = 0;
138
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 */
143
144         buf = wpabuf_alloc(1000 + extra);
145         if (buf == NULL)
146                 return NULL;
147
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);
152
153         len = p2p_buf_add_ie_hdr(buf);
154         group_capab = 0;
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;
159         }
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,
166                                group_capab);
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,
172                                    p2p->cfg->channel);
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);
182
183         /* WPS IE with Device Password ID attribute */
184         p2p_build_wps_ie(p2p, buf, p2p_wps_method_pw_id(peer->wps_method), 0);
185
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 */
190
191         return buf;
192 }
193
194
195 int p2p_connect_send(struct p2p_data *p2p, struct p2p_device *dev)
196 {
197         struct wpabuf *req;
198         int freq;
199
200         if (dev->flags & P2P_DEV_PD_BEFORE_GO_NEG) {
201                 u16 config_method;
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;
211                 else
212                         return -1;
213                 return p2p_prov_disc_req(p2p, dev->info.p2p_device_addr,
214                                          config_method, 0, 0, 1);
215         }
216
217         freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
218         if (freq <= 0) {
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));
223                 return -1;
224         }
225
226         req = p2p_build_go_neg_req(p2p, dev);
227         if (req == NULL)
228                 return -1;
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;
235         dev->connect_reqs++;
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);
243         } else
244                 dev->go_neg_req_sent++;
245
246         wpabuf_free(req);
247
248         return 0;
249 }
250
251
252 static struct wpabuf * p2p_build_go_neg_resp(struct p2p_data *p2p,
253                                              struct p2p_device *peer,
254                                              u8 dialog_token, u8 status,
255                                              u8 tie_breaker)
256 {
257         struct wpabuf *buf;
258         u8 *len;
259         u8 group_capab;
260         size_t extra = 0;
261
262         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
263                 "P2P: Building GO Negotiation Response");
264
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 */
269
270         buf = wpabuf_alloc(1000 + extra);
271         if (buf == NULL)
272                 return NULL;
273
274         p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_RESP, dialog_token);
275
276         len = p2p_buf_add_ie_hdr(buf);
277         p2p_buf_add_status(buf, status);
278         group_capab = 0;
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)
283                                 group_capab |=
284                                         P2P_GROUP_CAPAB_PERSISTENT_RECONN;
285                 }
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;
290         }
291         p2p_buf_add_capability(buf, p2p->dev_capab &
292                                ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
293                                group_capab);
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");
299         } else {
300                 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
301                                               p2p->op_reg_class,
302                                               p2p->op_channel);
303         }
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,
307                                          &p2p->channels);
308         } else if (peer->go_state == REMOTE_GO) {
309                 p2p_buf_add_channel_list(buf, p2p->cfg->country,
310                                          &p2p->channels);
311         } else {
312                 struct p2p_channels res;
313                 p2p_channels_intersect(&p2p->channels, &peer->channels,
314                                        &res);
315                 p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
316         }
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,
320                                      p2p->ssid_len);
321         }
322         p2p_buf_update_ie_hdr(buf, len);
323
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 :
327                                               WPS_NOT_READY), 0);
328
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 */
333
334
335         return buf;
336 }
337
338
339 void p2p_reselect_channel(struct p2p_data *p2p,
340                           struct p2p_channels *intersection)
341 {
342         struct p2p_reg_class *cl;
343         int freq;
344         u8 op_reg_class, op_channel;
345         unsigned int i;
346
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);
350
351         /* First, try to pick the best channel from another band */
352         freq = p2p_channel_to_freq(p2p->cfg->country, p2p->op_reg_class,
353                                    p2p->op_channel);
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;
363                 return;
364         }
365
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;
375                 return;
376         }
377
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);
389                         return;
390                 }
391         }
392
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];
404                         return;
405                 }
406         }
407
408         /*
409          * Fall back to whatever is included in the channel intersection since
410          * no better options seems to be available.
411          */
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];
418 }
419
420
421 static int p2p_go_select_channel(struct p2p_data *p2p, struct p2p_device *dev,
422                                  u8 *status)
423 {
424         struct p2p_channels intersection;
425         size_t i;
426
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");
433                 return -1;
434         }
435
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);
442         }
443
444         if (!p2p_channels_includes(&intersection, p2p->op_reg_class,
445                                    p2p->op_channel)) {
446                 if (dev->flags & P2P_DEV_FORCE_FREQ) {
447                         *status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
448                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer does "
449                                 "not support the forced channel");
450                         return -1;
451                 }
452                 p2p_reselect_channel(p2p, &intersection);
453         }
454
455         if (!p2p->ssid_set) {
456                 p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
457                 p2p->ssid_set = 1;
458         }
459
460         return 0;
461 }
462
463
464 void p2p_process_go_neg_req(struct p2p_data *p2p, const u8 *sa,
465                             const u8 *data, size_t len, int rx_freq)
466 {
467         struct p2p_device *dev = NULL;
468         struct wpabuf *resp;
469         struct p2p_message msg;
470         u8 status = P2P_SC_FAIL_INVALID_PARAMS;
471         int tie_breaker = 0;
472         int freq;
473
474         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
475                 "P2P: Received GO Negotiation Request from " MACSTR
476                 "(freq=%d)", MAC2STR(sa), rx_freq);
477
478         if (p2p_parse(data, len, &msg))
479                 return;
480
481         if (!msg.capability) {
482                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
483                         "P2P: Mandatory Capability attribute missing from GO "
484                         "Negotiation Request");
485 #ifdef CONFIG_P2P_STRICT
486                 goto fail;
487 #endif /* CONFIG_P2P_STRICT */
488         }
489
490         if (msg.go_intent)
491                 tie_breaker = *msg.go_intent & 0x01;
492         else {
493                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
494                         "P2P: Mandatory GO Intent attribute missing from GO "
495                         "Negotiation Request");
496 #ifdef CONFIG_P2P_STRICT
497                 goto fail;
498 #endif /* CONFIG_P2P_STRICT */
499         }
500
501         if (!msg.config_timeout) {
502                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
503                         "P2P: Mandatory Configuration Timeout attribute "
504                         "missing from GO Negotiation Request");
505 #ifdef CONFIG_P2P_STRICT
506                 goto fail;
507 #endif /* CONFIG_P2P_STRICT */
508         }
509
510         if (!msg.listen_channel) {
511                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
512                         "P2P: No Listen Channel attribute received");
513                 goto fail;
514         }
515         if (!msg.operating_channel) {
516                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
517                         "P2P: No Operating Channel attribute received");
518                 goto fail;
519         }
520         if (!msg.channel_list) {
521                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
522                         "P2P: No Channel List attribute received");
523                 goto fail;
524         }
525         if (!msg.intended_addr) {
526                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
527                         "P2P: No Intended P2P Interface Address attribute "
528                         "received");
529                 goto fail;
530         }
531         if (!msg.p2p_device_info) {
532                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
533                         "P2P: No P2P Device Info attribute received");
534                 goto fail;
535         }
536
537         if (os_memcmp(msg.p2p_device_addr, sa, ETH_ALEN) != 0) {
538                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
539                         "P2P: Unexpected GO Negotiation Request SA=" MACSTR
540                         " != dev_addr=" MACSTR,
541                         MAC2STR(sa), MAC2STR(msg.p2p_device_addr));
542                 goto fail;
543         }
544
545         dev = p2p_get_device(p2p, sa);
546
547         if (msg.status && *msg.status) {
548                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
549                         "P2P: Unexpected Status attribute (%d) in GO "
550                         "Negotiation Request", *msg.status);
551                 goto fail;
552         }
553
554         if (dev == NULL)
555                 dev = p2p_add_dev_from_go_neg_req(p2p, sa, &msg);
556         else if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
557                 p2p_add_dev_info(p2p, sa, dev, &msg);
558         if (dev && dev->flags & P2P_DEV_USER_REJECTED) {
559                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
560                         "P2P: User has rejected this peer");
561                 status = P2P_SC_FAIL_REJECTED_BY_USER;
562         } else if (dev == NULL || dev->wps_method == WPS_NOT_READY) {
563                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
564                         "P2P: Not ready for GO negotiation with " MACSTR,
565                         MAC2STR(sa));
566                 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
567                 if (dev)
568                         dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
569                 p2p->cfg->go_neg_req_rx(p2p->cfg->cb_ctx, sa,
570                                         msg.dev_password_id);
571         } else if (p2p->go_neg_peer && p2p->go_neg_peer != dev) {
572                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
573                         "P2P: Already in Group Formation with another peer");
574                 status = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
575         } else {
576                 int go;
577
578                 if (!p2p->go_neg_peer) {
579                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting "
580                                 "GO Negotiation with previously authorized "
581                                 "peer");
582                         if (!(dev->flags & P2P_DEV_FORCE_FREQ)) {
583                                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
584                                         "P2P: Use default channel settings");
585                                 p2p->op_reg_class = p2p->cfg->op_reg_class;
586                                 p2p->op_channel = p2p->cfg->op_channel;
587                                 os_memcpy(&p2p->channels, &p2p->cfg->channels,
588                                           sizeof(struct p2p_channels));
589                         } else {
590                                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
591                                         "P2P: Use previously configured "
592                                         "forced channel settings");
593                         }
594                 }
595
596                 dev->flags &= ~P2P_DEV_NOT_YET_READY;
597
598                 if (!msg.go_intent) {
599                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
600                                 "P2P: No GO Intent attribute received");
601                         goto fail;
602                 }
603                 if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
604                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
605                                 "P2P: Invalid GO Intent value (%u) received",
606                                 *msg.go_intent >> 1);
607                         goto fail;
608                 }
609
610                 if (dev->go_neg_req_sent &&
611                     os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) > 0) {
612                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
613                                 "P2P: Do not reply since peer has higher "
614                                 "address and GO Neg Request already sent");
615                         p2p_parse_free(&msg);
616                         return;
617                 }
618
619                 go = p2p_go_det(p2p->go_intent, *msg.go_intent);
620                 if (go < 0) {
621                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
622                                 "P2P: Incompatible GO Intent");
623                         status = P2P_SC_FAIL_BOTH_GO_INTENT_15;
624                         goto fail;
625                 }
626
627                 if (p2p_peer_channels(p2p, dev, msg.channel_list,
628                                       msg.channel_list_len) < 0) {
629                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
630                                 "P2P: No common channels found");
631                         status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
632                         goto fail;
633                 }
634
635                 switch (msg.dev_password_id) {
636                 case DEV_PW_REGISTRAR_SPECIFIED:
637                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
638                                 "P2P: PIN from peer Display");
639                         if (dev->wps_method != WPS_PIN_KEYPAD) {
640                                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
641                                         "P2P: We have wps_method=%s -> "
642                                         "incompatible",
643                                         p2p_wps_method_str(dev->wps_method));
644                                 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
645                                 goto fail;
646                         }
647                         break;
648                 case DEV_PW_USER_SPECIFIED:
649                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
650                                 "P2P: Peer entered PIN on Keypad");
651                         if (dev->wps_method != WPS_PIN_DISPLAY) {
652                                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
653                                         "P2P: We have wps_method=%s -> "
654                                         "incompatible",
655                                         p2p_wps_method_str(dev->wps_method));
656                                 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
657                                 goto fail;
658                         }
659                         break;
660                 case DEV_PW_PUSHBUTTON:
661                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
662                                 "P2P: Peer using pushbutton");
663                         if (dev->wps_method != WPS_PBC) {
664                                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
665                                         "P2P: We have wps_method=%s -> "
666                                         "incompatible",
667                                         p2p_wps_method_str(dev->wps_method));
668                                 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
669                                 goto fail;
670                         }
671                         break;
672                 default:
673                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
674                                 "P2P: Unsupported Device Password ID %d",
675                                 msg.dev_password_id);
676                         status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
677                         goto fail;
678                 }
679
680                 if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
681                         goto fail;
682
683                 dev->go_state = go ? LOCAL_GO : REMOTE_GO;
684                 dev->oper_freq = p2p_channel_to_freq((const char *)
685                                                      msg.operating_channel,
686                                                      msg.operating_channel[3],
687                                                      msg.operating_channel[4]);
688                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
689                         "channel preference: %d MHz", dev->oper_freq);
690
691                 if (msg.config_timeout) {
692                         dev->go_timeout = msg.config_timeout[0];
693                         dev->client_timeout = msg.config_timeout[1];
694                 }
695
696                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
697                         "P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
698                 if (p2p->state != P2P_IDLE)
699                         p2p_stop_find_for_freq(p2p, rx_freq);
700                 p2p_set_state(p2p, P2P_GO_NEG);
701                 p2p_clear_timeout(p2p);
702                 dev->dialog_token = msg.dialog_token;
703                 os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
704                 p2p->go_neg_peer = dev;
705                 status = P2P_SC_SUCCESS;
706         }
707
708 fail:
709         if (dev)
710                 dev->status = status;
711         resp = p2p_build_go_neg_resp(p2p, dev, msg.dialog_token, status,
712                                      !tie_breaker);
713         p2p_parse_free(&msg);
714         if (resp == NULL)
715                 return;
716         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
717                 "P2P: Sending GO Negotiation Response");
718         if (rx_freq > 0)
719                 freq = rx_freq;
720         else
721                 freq = p2p_channel_to_freq(p2p->cfg->country,
722                                            p2p->cfg->reg_class,
723                                            p2p->cfg->channel);
724         if (freq < 0) {
725                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
726                         "P2P: Unknown regulatory class/channel");
727                 wpabuf_free(resp);
728                 return;
729         }
730         if (status == P2P_SC_SUCCESS) {
731                 p2p->pending_action_state = P2P_PENDING_GO_NEG_RESPONSE;
732                 dev->flags |= P2P_DEV_WAIT_GO_NEG_CONFIRM;
733                 if (os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) < 0) {
734                         /*
735                          * Peer has smaller address, so the GO Negotiation
736                          * Response from us is expected to complete
737                          * negotiation. Ignore a GO Negotiation Response from
738                          * the peer if it happens to be received after this
739                          * point due to a race condition in GO Negotiation
740                          * Request transmission and processing.
741                          */
742                         dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
743                 }
744         } else
745                 p2p->pending_action_state =
746                         P2P_PENDING_GO_NEG_RESPONSE_FAILURE;
747         if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
748                             p2p->cfg->dev_addr,
749                             wpabuf_head(resp), wpabuf_len(resp), 250) < 0) {
750                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
751                         "P2P: Failed to send Action frame");
752         }
753
754         wpabuf_free(resp);
755 }
756
757
758 static struct wpabuf * p2p_build_go_neg_conf(struct p2p_data *p2p,
759                                              struct p2p_device *peer,
760                                              u8 dialog_token, u8 status,
761                                              const u8 *resp_chan, int go)
762 {
763         struct wpabuf *buf;
764         u8 *len;
765         struct p2p_channels res;
766         u8 group_capab;
767         size_t extra = 0;
768
769         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
770                 "P2P: Building GO Negotiation Confirm");
771
772 #ifdef CONFIG_WIFI_DISPLAY
773         if (p2p->wfd_ie_go_neg)
774                 extra = wpabuf_len(p2p->wfd_ie_go_neg);
775 #endif /* CONFIG_WIFI_DISPLAY */
776
777         buf = wpabuf_alloc(1000 + extra);
778         if (buf == NULL)
779                 return NULL;
780
781         p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_CONF, dialog_token);
782
783         len = p2p_buf_add_ie_hdr(buf);
784         p2p_buf_add_status(buf, status);
785         group_capab = 0;
786         if (peer->go_state == LOCAL_GO) {
787                 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
788                         group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
789                         if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
790                                 group_capab |=
791                                         P2P_GROUP_CAPAB_PERSISTENT_RECONN;
792                 }
793                 if (p2p->cross_connect)
794                         group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
795                 if (p2p->cfg->p2p_intra_bss)
796                         group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
797         }
798         p2p_buf_add_capability(buf, p2p->dev_capab &
799                                ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
800                                group_capab);
801         if (go || resp_chan == NULL)
802                 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
803                                               p2p->op_reg_class,
804                                               p2p->op_channel);
805         else
806                 p2p_buf_add_operating_channel(buf, (const char *) resp_chan,
807                                               resp_chan[3], resp_chan[4]);
808         p2p_channels_intersect(&p2p->channels, &peer->channels, &res);
809         p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
810         if (go) {
811                 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
812                                      p2p->ssid_len);
813         }
814         p2p_buf_update_ie_hdr(buf, len);
815
816 #ifdef CONFIG_WIFI_DISPLAY
817         if (p2p->wfd_ie_go_neg)
818                 wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
819 #endif /* CONFIG_WIFI_DISPLAY */
820
821         return buf;
822 }
823
824
825 void p2p_process_go_neg_resp(struct p2p_data *p2p, const u8 *sa,
826                              const u8 *data, size_t len, int rx_freq)
827 {
828         struct p2p_device *dev;
829         struct wpabuf *conf;
830         int go = -1;
831         struct p2p_message msg;
832         u8 status = P2P_SC_SUCCESS;
833         int freq;
834
835         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
836                 "P2P: Received GO Negotiation Response from " MACSTR
837                 " (freq=%d)", MAC2STR(sa), rx_freq);
838         dev = p2p_get_device(p2p, sa);
839         if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
840             dev != p2p->go_neg_peer) {
841                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
842                         "P2P: Not ready for GO negotiation with " MACSTR,
843                         MAC2STR(sa));
844                 return;
845         }
846
847         if (p2p_parse(data, len, &msg))
848                 return;
849
850         if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE)) {
851                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
852                         "P2P: Was not expecting GO Negotiation Response - "
853                         "ignore");
854                 p2p_parse_free(&msg);
855                 return;
856         }
857         dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
858
859         if (msg.dialog_token != dev->dialog_token) {
860                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
861                         "P2P: Unexpected Dialog Token %u (expected %u)",
862                         msg.dialog_token, dev->dialog_token);
863                 p2p_parse_free(&msg);
864                 return;
865         }
866
867         if (!msg.status) {
868                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
869                         "P2P: No Status attribute received");
870                 status = P2P_SC_FAIL_INVALID_PARAMS;
871                 goto fail;
872         }
873         if (*msg.status) {
874                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
875                         "P2P: GO Negotiation rejected: status %d",
876                         *msg.status);
877                 dev->go_neg_req_sent = 0;
878                 if (*msg.status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
879                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
880                                 "P2P: Wait for the peer to become ready for "
881                                 "GO Negotiation");
882                         dev->flags |= P2P_DEV_NOT_YET_READY;
883                         dev->wait_count = 0;
884                         p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
885                         p2p_set_timeout(p2p, 0, 0);
886                 } else {
887                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
888                                 "P2P: Stop GO Negotiation attempt");
889                         p2p_go_neg_failed(p2p, dev, *msg.status);
890                 }
891                 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
892                 p2p_parse_free(&msg);
893                 return;
894         }
895
896         if (!msg.capability) {
897                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
898                         "P2P: Mandatory Capability attribute missing from GO "
899                         "Negotiation Response");
900 #ifdef CONFIG_P2P_STRICT
901                 status = P2P_SC_FAIL_INVALID_PARAMS;
902                 goto fail;
903 #endif /* CONFIG_P2P_STRICT */
904         }
905
906         if (!msg.p2p_device_info) {
907                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
908                         "P2P: Mandatory P2P Device Info attribute missing "
909                         "from GO Negotiation Response");
910 #ifdef CONFIG_P2P_STRICT
911                 status = P2P_SC_FAIL_INVALID_PARAMS;
912                 goto fail;
913 #endif /* CONFIG_P2P_STRICT */
914         }
915
916         if (!msg.intended_addr) {
917                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
918                         "P2P: No Intended P2P Interface Address attribute "
919                         "received");
920                 status = P2P_SC_FAIL_INVALID_PARAMS;
921                 goto fail;
922         }
923
924         if (!msg.go_intent) {
925                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
926                         "P2P: No GO Intent attribute received");
927                 status = P2P_SC_FAIL_INVALID_PARAMS;
928                 goto fail;
929         }
930         if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
931                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
932                         "P2P: Invalid GO Intent value (%u) received",
933                         *msg.go_intent >> 1);
934                 status = P2P_SC_FAIL_INVALID_PARAMS;
935                 goto fail;
936         }
937
938         go = p2p_go_det(p2p->go_intent, *msg.go_intent);
939         if (go < 0) {
940                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
941                         "P2P: Incompatible GO Intent");
942                 status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
943                 goto fail;
944         }
945
946         if (!go && msg.group_id) {
947                 /* Store SSID for Provisioning step */
948                 p2p->ssid_len = msg.group_id_len - ETH_ALEN;
949                 os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
950         } else if (!go) {
951                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
952                         "P2P: Mandatory P2P Group ID attribute missing from "
953                         "GO Negotiation Response");
954                 p2p->ssid_len = 0;
955 #ifdef CONFIG_P2P_STRICT
956                 status = P2P_SC_FAIL_INVALID_PARAMS;
957                 goto fail;
958 #endif /* CONFIG_P2P_STRICT */
959         }
960
961         if (!msg.config_timeout) {
962                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
963                         "P2P: Mandatory Configuration Timeout attribute "
964                         "missing from GO Negotiation Response");
965 #ifdef CONFIG_P2P_STRICT
966                 status = P2P_SC_FAIL_INVALID_PARAMS;
967                 goto fail;
968 #endif /* CONFIG_P2P_STRICT */
969         } else {
970                 dev->go_timeout = msg.config_timeout[0];
971                 dev->client_timeout = msg.config_timeout[1];
972         }
973
974         if (!msg.operating_channel && !go) {
975                 /*
976                  * Note: P2P Client may omit Operating Channel attribute to
977                  * indicate it does not have a preference.
978                  */
979                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
980                         "P2P: No Operating Channel attribute received");
981                 status = P2P_SC_FAIL_INVALID_PARAMS;
982                 goto fail;
983         }
984         if (!msg.channel_list) {
985                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
986                         "P2P: No Channel List attribute received");
987                 status = P2P_SC_FAIL_INVALID_PARAMS;
988                 goto fail;
989         }
990
991         if (p2p_peer_channels(p2p, dev, msg.channel_list,
992                               msg.channel_list_len) < 0) {
993                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
994                         "P2P: No common channels found");
995                 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
996                 goto fail;
997         }
998
999         if (msg.operating_channel) {
1000                 dev->oper_freq = p2p_channel_to_freq((const char *)
1001                                                      msg.operating_channel,
1002                                                      msg.operating_channel[3],
1003                                                      msg.operating_channel[4]);
1004                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
1005                         "channel preference: %d MHz", dev->oper_freq);
1006         } else
1007                 dev->oper_freq = 0;
1008
1009         switch (msg.dev_password_id) {
1010         case DEV_PW_REGISTRAR_SPECIFIED:
1011                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1012                         "P2P: PIN from peer Display");
1013                 if (dev->wps_method != WPS_PIN_KEYPAD) {
1014                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1015                                 "P2P: We have wps_method=%s -> "
1016                                 "incompatible",
1017                                 p2p_wps_method_str(dev->wps_method));
1018                         status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1019                         goto fail;
1020                 }
1021                 break;
1022         case DEV_PW_USER_SPECIFIED:
1023                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1024                         "P2P: Peer entered PIN on Keypad");
1025                 if (dev->wps_method != WPS_PIN_DISPLAY) {
1026                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1027                                 "P2P: We have wps_method=%s -> "
1028                                 "incompatible",
1029                                 p2p_wps_method_str(dev->wps_method));
1030                         status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1031                         goto fail;
1032                 }
1033                 break;
1034         case DEV_PW_PUSHBUTTON:
1035                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1036                         "P2P: Peer using pushbutton");
1037                 if (dev->wps_method != WPS_PBC) {
1038                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1039                                 "P2P: We have wps_method=%s -> "
1040                                 "incompatible",
1041                                 p2p_wps_method_str(dev->wps_method));
1042                         status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1043                         goto fail;
1044                 }
1045                 break;
1046         default:
1047                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1048                         "P2P: Unsupported Device Password ID %d",
1049                         msg.dev_password_id);
1050                 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1051                 goto fail;
1052         }
1053
1054         if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
1055                 goto fail;
1056
1057         p2p_set_state(p2p, P2P_GO_NEG);
1058         p2p_clear_timeout(p2p);
1059
1060         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1061                 "P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
1062         os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
1063
1064 fail:
1065         conf = p2p_build_go_neg_conf(p2p, dev, msg.dialog_token, status,
1066                                      msg.operating_channel, go);
1067         p2p_parse_free(&msg);
1068         if (conf == NULL)
1069                 return;
1070         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1071                 "P2P: Sending GO Negotiation Confirm");
1072         if (status == P2P_SC_SUCCESS) {
1073                 p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
1074                 dev->go_state = go ? LOCAL_GO : REMOTE_GO;
1075         } else
1076                 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1077         if (rx_freq > 0)
1078                 freq = rx_freq;
1079         else
1080                 freq = dev->listen_freq;
1081         if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr, sa,
1082                             wpabuf_head(conf), wpabuf_len(conf), 0) < 0) {
1083                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1084                         "P2P: Failed to send Action frame");
1085                 p2p_go_neg_failed(p2p, dev, -1);
1086         }
1087         wpabuf_free(conf);
1088 }
1089
1090
1091 void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa,
1092                              const u8 *data, size_t len)
1093 {
1094         struct p2p_device *dev;
1095         struct p2p_message msg;
1096
1097         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1098                 "P2P: Received GO Negotiation Confirm from " MACSTR,
1099                 MAC2STR(sa));
1100         dev = p2p_get_device(p2p, sa);
1101         if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
1102             dev != p2p->go_neg_peer) {
1103                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1104                         "P2P: Not ready for GO negotiation with " MACSTR,
1105                         MAC2STR(sa));
1106                 return;
1107         }
1108
1109         if (p2p->pending_action_state == P2P_PENDING_GO_NEG_RESPONSE) {
1110                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopped waiting "
1111                         "for TX status on GO Negotiation Response since we "
1112                         "already received Confirmation");
1113                 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1114         }
1115
1116         if (p2p_parse(data, len, &msg))
1117                 return;
1118
1119         if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
1120                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1121                         "P2P: Was not expecting GO Negotiation Confirm - "
1122                         "ignore");
1123                 return;
1124         }
1125         dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1126
1127         if (msg.dialog_token != dev->dialog_token) {
1128                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1129                         "P2P: Unexpected Dialog Token %u (expected %u)",
1130                         msg.dialog_token, dev->dialog_token);
1131                 p2p_parse_free(&msg);
1132                 return;
1133         }
1134
1135         if (!msg.status) {
1136                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1137                         "P2P: No Status attribute received");
1138                 p2p_parse_free(&msg);
1139                 return;
1140         }
1141         if (*msg.status) {
1142                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1143                         "P2P: GO Negotiation rejected: status %d",
1144                         *msg.status);
1145                 p2p_parse_free(&msg);
1146                 return;
1147         }
1148
1149         if (dev->go_state == REMOTE_GO && msg.group_id) {
1150                 /* Store SSID for Provisioning step */
1151                 p2p->ssid_len = msg.group_id_len - ETH_ALEN;
1152                 os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
1153         } else if (dev->go_state == REMOTE_GO) {
1154                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1155                         "P2P: Mandatory P2P Group ID attribute missing from "
1156                         "GO Negotiation Confirmation");
1157                 p2p->ssid_len = 0;
1158 #ifdef CONFIG_P2P_STRICT
1159                 p2p_parse_free(&msg);
1160                 return;
1161 #endif /* CONFIG_P2P_STRICT */
1162         }
1163
1164         if (!msg.operating_channel) {
1165                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1166                         "P2P: Mandatory Operating Channel attribute missing "
1167                         "from GO Negotiation Confirmation");
1168 #ifdef CONFIG_P2P_STRICT
1169                 p2p_parse_free(&msg);
1170                 return;
1171 #endif /* CONFIG_P2P_STRICT */
1172         }
1173
1174         if (!msg.channel_list) {
1175                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1176                         "P2P: Mandatory Operating Channel attribute missing "
1177                         "from GO Negotiation Confirmation");
1178 #ifdef CONFIG_P2P_STRICT
1179                 p2p_parse_free(&msg);
1180                 return;
1181 #endif /* CONFIG_P2P_STRICT */
1182         }
1183
1184         p2p_parse_free(&msg);
1185
1186         if (dev->go_state == UNKNOWN_GO) {
1187                 /*
1188                  * This should not happen since GO negotiation has already
1189                  * been completed.
1190                  */
1191                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1192                         "P2P: Unexpected GO Neg state - do not know which end "
1193                         "becomes GO");
1194                 return;
1195         }
1196
1197         /*
1198          * The peer could have missed our ctrl::ack frame for GO Negotiation
1199          * Confirm and continue retransmitting the frame. To reduce the
1200          * likelihood of the peer not getting successful TX status for the
1201          * GO Negotiation Confirm frame, wait a short time here before starting
1202          * the group so that we will remain on the current channel to
1203          * acknowledge any possible retransmission from the peer.
1204          */
1205         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: 20 ms wait on current "
1206                 "channel before starting group");
1207         os_sleep(0, 20000);
1208
1209         p2p_go_complete(p2p, dev);
1210 }