P2P: Allow peer to propose channel in invitation process
[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);
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         /*
394          * Fall back to whatever is included in the channel intersection since
395          * no better options seems to be available.
396          */
397         cl = &intersection->reg_class[0];
398         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick another channel "
399                 "(reg_class %u channel %u) from intersection",
400                 cl->reg_class, cl->channel[0]);
401         p2p->op_reg_class = cl->reg_class;
402         p2p->op_channel = cl->channel[0];
403 }
404
405
406 void p2p_process_go_neg_req(struct p2p_data *p2p, const u8 *sa,
407                             const u8 *data, size_t len, int rx_freq)
408 {
409         struct p2p_device *dev = NULL;
410         struct wpabuf *resp;
411         struct p2p_message msg;
412         u8 status = P2P_SC_FAIL_INVALID_PARAMS;
413         int tie_breaker = 0;
414         int freq;
415
416         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
417                 "P2P: Received GO Negotiation Request from " MACSTR
418                 "(freq=%d)", MAC2STR(sa), rx_freq);
419
420         if (p2p_parse(data, len, &msg))
421                 return;
422
423         if (!msg.capability) {
424                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
425                         "P2P: Mandatory Capability attribute missing from GO "
426                         "Negotiation Request");
427 #ifdef CONFIG_P2P_STRICT
428                 goto fail;
429 #endif /* CONFIG_P2P_STRICT */
430         }
431
432         if (msg.go_intent)
433                 tie_breaker = *msg.go_intent & 0x01;
434         else {
435                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
436                         "P2P: Mandatory GO Intent attribute missing from GO "
437                         "Negotiation Request");
438 #ifdef CONFIG_P2P_STRICT
439                 goto fail;
440 #endif /* CONFIG_P2P_STRICT */
441         }
442
443         if (!msg.config_timeout) {
444                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
445                         "P2P: Mandatory Configuration Timeout attribute "
446                         "missing from GO Negotiation Request");
447 #ifdef CONFIG_P2P_STRICT
448                 goto fail;
449 #endif /* CONFIG_P2P_STRICT */
450         }
451
452         if (!msg.listen_channel) {
453                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
454                         "P2P: No Listen Channel attribute received");
455                 goto fail;
456         }
457         if (!msg.operating_channel) {
458                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
459                         "P2P: No Operating Channel attribute received");
460                 goto fail;
461         }
462         if (!msg.channel_list) {
463                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
464                         "P2P: No Channel List attribute received");
465                 goto fail;
466         }
467         if (!msg.intended_addr) {
468                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
469                         "P2P: No Intended P2P Interface Address attribute "
470                         "received");
471                 goto fail;
472         }
473         if (!msg.p2p_device_info) {
474                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
475                         "P2P: No P2P Device Info attribute received");
476                 goto fail;
477         }
478
479         if (os_memcmp(msg.p2p_device_addr, sa, ETH_ALEN) != 0) {
480                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
481                         "P2P: Unexpected GO Negotiation Request SA=" MACSTR
482                         " != dev_addr=" MACSTR,
483                         MAC2STR(sa), MAC2STR(msg.p2p_device_addr));
484                 goto fail;
485         }
486
487         dev = p2p_get_device(p2p, sa);
488
489         if (msg.status && *msg.status) {
490                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
491                         "P2P: Unexpected Status attribute (%d) in GO "
492                         "Negotiation Request", *msg.status);
493                 goto fail;
494         }
495
496         if (dev == NULL)
497                 dev = p2p_add_dev_from_go_neg_req(p2p, sa, &msg);
498         else if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
499                 p2p_add_dev_info(p2p, sa, dev, &msg);
500         if (dev && dev->flags & P2P_DEV_USER_REJECTED) {
501                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
502                         "P2P: User has rejected this peer");
503                 status = P2P_SC_FAIL_REJECTED_BY_USER;
504         } else if (dev == NULL || dev->wps_method == WPS_NOT_READY) {
505                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
506                         "P2P: Not ready for GO negotiation with " MACSTR,
507                         MAC2STR(sa));
508                 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
509                 if (dev)
510                         dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
511                 p2p->cfg->go_neg_req_rx(p2p->cfg->cb_ctx, sa,
512                                         msg.dev_password_id);
513         } else if (p2p->go_neg_peer && p2p->go_neg_peer != dev) {
514                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
515                         "P2P: Already in Group Formation with another peer");
516                 status = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
517         } else {
518                 int go;
519
520                 if (!p2p->go_neg_peer) {
521                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting "
522                                 "GO Negotiation with previously authorized "
523                                 "peer");
524                         if (!(dev->flags & P2P_DEV_FORCE_FREQ)) {
525                                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
526                                         "P2P: Use default channel settings");
527                                 p2p->op_reg_class = p2p->cfg->op_reg_class;
528                                 p2p->op_channel = p2p->cfg->op_channel;
529                                 os_memcpy(&p2p->channels, &p2p->cfg->channels,
530                                           sizeof(struct p2p_channels));
531                         } else {
532                                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
533                                         "P2P: Use previously configured "
534                                         "forced channel settings");
535                         }
536                 }
537
538                 dev->flags &= ~P2P_DEV_NOT_YET_READY;
539
540                 if (!msg.go_intent) {
541                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
542                                 "P2P: No GO Intent attribute received");
543                         goto fail;
544                 }
545                 if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
546                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
547                                 "P2P: Invalid GO Intent value (%u) received",
548                                 *msg.go_intent >> 1);
549                         goto fail;
550                 }
551
552                 if (dev->go_neg_req_sent &&
553                     os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) > 0) {
554                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
555                                 "P2P: Do not reply since peer has higher "
556                                 "address and GO Neg Request already sent");
557                         p2p_parse_free(&msg);
558                         return;
559                 }
560
561                 go = p2p_go_det(p2p->go_intent, *msg.go_intent);
562                 if (go < 0) {
563                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
564                                 "P2P: Incompatible GO Intent");
565                         status = P2P_SC_FAIL_BOTH_GO_INTENT_15;
566                         goto fail;
567                 }
568
569                 if (p2p_peer_channels(p2p, dev, msg.channel_list,
570                                       msg.channel_list_len) < 0) {
571                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
572                                 "P2P: No common channels found");
573                         status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
574                         goto fail;
575                 }
576
577                 switch (msg.dev_password_id) {
578                 case DEV_PW_REGISTRAR_SPECIFIED:
579                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
580                                 "P2P: PIN from peer Display");
581                         if (dev->wps_method != WPS_PIN_KEYPAD) {
582                                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
583                                         "P2P: We have wps_method=%s -> "
584                                         "incompatible",
585                                         p2p_wps_method_str(dev->wps_method));
586                                 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
587                                 goto fail;
588                         }
589                         break;
590                 case DEV_PW_USER_SPECIFIED:
591                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
592                                 "P2P: Peer entered PIN on Keypad");
593                         if (dev->wps_method != WPS_PIN_DISPLAY) {
594                                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
595                                         "P2P: We have wps_method=%s -> "
596                                         "incompatible",
597                                         p2p_wps_method_str(dev->wps_method));
598                                 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
599                                 goto fail;
600                         }
601                         break;
602                 case DEV_PW_PUSHBUTTON:
603                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
604                                 "P2P: Peer using pushbutton");
605                         if (dev->wps_method != WPS_PBC) {
606                                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
607                                         "P2P: We have wps_method=%s -> "
608                                         "incompatible",
609                                         p2p_wps_method_str(dev->wps_method));
610                                 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
611                                 goto fail;
612                         }
613                         break;
614                 default:
615                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
616                                 "P2P: Unsupported Device Password ID %d",
617                                 msg.dev_password_id);
618                         status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
619                         goto fail;
620                 }
621
622                 if (go) {
623                         struct p2p_channels intersection;
624                         size_t i;
625                         p2p_channels_intersect(&p2p->channels, &dev->channels,
626                                                &intersection);
627                         if (intersection.reg_classes == 0 ||
628                             intersection.reg_class[0].channels == 0) {
629                                 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
630                                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
631                                         "P2P: No common channels found");
632                                 goto fail;
633                         }
634                         for (i = 0; i < intersection.reg_classes; i++) {
635                                 struct p2p_reg_class *c;
636                                 c = &intersection.reg_class[i];
637                                 wpa_printf(MSG_DEBUG, "P2P: reg_class %u",
638                                            c->reg_class);
639                                 wpa_hexdump(MSG_DEBUG, "P2P: channels",
640                                             c->channel, c->channels);
641                         }
642                         if (!p2p_channels_includes(&intersection,
643                                                    p2p->op_reg_class,
644                                                    p2p->op_channel))
645                                 p2p_reselect_channel(p2p, &intersection);
646
647                         if (!p2p->ssid_set) {
648                                 p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
649                                 p2p->ssid_set = 1;
650                         }
651                 }
652
653                 dev->go_state = go ? LOCAL_GO : REMOTE_GO;
654                 dev->oper_freq = p2p_channel_to_freq((const char *)
655                                                      msg.operating_channel,
656                                                      msg.operating_channel[3],
657                                                      msg.operating_channel[4]);
658                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
659                         "channel preference: %d MHz", dev->oper_freq);
660
661                 if (msg.config_timeout) {
662                         dev->go_timeout = msg.config_timeout[0];
663                         dev->client_timeout = msg.config_timeout[1];
664                 }
665
666                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
667                         "P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
668                 if (p2p->state != P2P_IDLE)
669                         p2p_stop_find_for_freq(p2p, rx_freq);
670                 p2p_set_state(p2p, P2P_GO_NEG);
671                 p2p_clear_timeout(p2p);
672                 dev->dialog_token = msg.dialog_token;
673                 os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
674                 p2p->go_neg_peer = dev;
675                 status = P2P_SC_SUCCESS;
676         }
677
678 fail:
679         if (dev)
680                 dev->status = status;
681         resp = p2p_build_go_neg_resp(p2p, dev, msg.dialog_token, status,
682                                      !tie_breaker);
683         p2p_parse_free(&msg);
684         if (resp == NULL)
685                 return;
686         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
687                 "P2P: Sending GO Negotiation Response");
688         if (rx_freq > 0)
689                 freq = rx_freq;
690         else
691                 freq = p2p_channel_to_freq(p2p->cfg->country,
692                                            p2p->cfg->reg_class,
693                                            p2p->cfg->channel);
694         if (freq < 0) {
695                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
696                         "P2P: Unknown regulatory class/channel");
697                 wpabuf_free(resp);
698                 return;
699         }
700         if (status == P2P_SC_SUCCESS) {
701                 p2p->pending_action_state = P2P_PENDING_GO_NEG_RESPONSE;
702                 dev->flags |= P2P_DEV_WAIT_GO_NEG_CONFIRM;
703                 if (os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) < 0) {
704                         /*
705                          * Peer has smaller address, so the GO Negotiation
706                          * Response from us is expected to complete
707                          * negotiation. Ignore a GO Negotiation Response from
708                          * the peer if it happens to be received after this
709                          * point due to a race condition in GO Negotiation
710                          * Request transmission and processing.
711                          */
712                         dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
713                 }
714         } else
715                 p2p->pending_action_state =
716                         P2P_PENDING_GO_NEG_RESPONSE_FAILURE;
717         if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
718                             p2p->cfg->dev_addr,
719                             wpabuf_head(resp), wpabuf_len(resp), 250) < 0) {
720                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
721                         "P2P: Failed to send Action frame");
722         }
723
724         wpabuf_free(resp);
725 }
726
727
728 static struct wpabuf * p2p_build_go_neg_conf(struct p2p_data *p2p,
729                                              struct p2p_device *peer,
730                                              u8 dialog_token, u8 status,
731                                              const u8 *resp_chan, int go)
732 {
733         struct wpabuf *buf;
734         u8 *len;
735         struct p2p_channels res;
736         u8 group_capab;
737         size_t extra = 0;
738
739         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
740                 "P2P: Building GO Negotiation Confirm");
741
742 #ifdef CONFIG_WIFI_DISPLAY
743         if (p2p->wfd_ie_go_neg)
744                 extra = wpabuf_len(p2p->wfd_ie_go_neg);
745 #endif /* CONFIG_WIFI_DISPLAY */
746
747         buf = wpabuf_alloc(1000 + extra);
748         if (buf == NULL)
749                 return NULL;
750
751         p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_CONF, dialog_token);
752
753         len = p2p_buf_add_ie_hdr(buf);
754         p2p_buf_add_status(buf, status);
755         group_capab = 0;
756         if (peer->go_state == LOCAL_GO) {
757                 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
758                         group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
759                         if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
760                                 group_capab |=
761                                         P2P_GROUP_CAPAB_PERSISTENT_RECONN;
762                 }
763                 if (p2p->cross_connect)
764                         group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
765                 if (p2p->cfg->p2p_intra_bss)
766                         group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
767         }
768         p2p_buf_add_capability(buf, p2p->dev_capab &
769                                ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
770                                group_capab);
771         if (go || resp_chan == NULL)
772                 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
773                                               p2p->op_reg_class,
774                                               p2p->op_channel);
775         else
776                 p2p_buf_add_operating_channel(buf, (const char *) resp_chan,
777                                               resp_chan[3], resp_chan[4]);
778         p2p_channels_intersect(&p2p->channels, &peer->channels, &res);
779         p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
780         if (go) {
781                 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
782                                      p2p->ssid_len);
783         }
784         p2p_buf_update_ie_hdr(buf, len);
785
786 #ifdef CONFIG_WIFI_DISPLAY
787         if (p2p->wfd_ie_go_neg)
788                 wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
789 #endif /* CONFIG_WIFI_DISPLAY */
790
791         return buf;
792 }
793
794
795 void p2p_process_go_neg_resp(struct p2p_data *p2p, const u8 *sa,
796                              const u8 *data, size_t len, int rx_freq)
797 {
798         struct p2p_device *dev;
799         struct wpabuf *conf;
800         int go = -1;
801         struct p2p_message msg;
802         u8 status = P2P_SC_SUCCESS;
803         int freq;
804
805         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
806                 "P2P: Received GO Negotiation Response from " MACSTR
807                 " (freq=%d)", MAC2STR(sa), rx_freq);
808         dev = p2p_get_device(p2p, sa);
809         if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
810             dev != p2p->go_neg_peer) {
811                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
812                         "P2P: Not ready for GO negotiation with " MACSTR,
813                         MAC2STR(sa));
814                 return;
815         }
816
817         if (p2p_parse(data, len, &msg))
818                 return;
819
820         if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE)) {
821                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
822                         "P2P: Was not expecting GO Negotiation Response - "
823                         "ignore");
824                 p2p_parse_free(&msg);
825                 return;
826         }
827         dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
828
829         if (msg.dialog_token != dev->dialog_token) {
830                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
831                         "P2P: Unexpected Dialog Token %u (expected %u)",
832                         msg.dialog_token, dev->dialog_token);
833                 p2p_parse_free(&msg);
834                 return;
835         }
836
837         if (!msg.status) {
838                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
839                         "P2P: No Status attribute received");
840                 status = P2P_SC_FAIL_INVALID_PARAMS;
841                 goto fail;
842         }
843         if (*msg.status) {
844                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
845                         "P2P: GO Negotiation rejected: status %d",
846                         *msg.status);
847                 dev->go_neg_req_sent = 0;
848                 if (*msg.status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
849                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
850                                 "P2P: Wait for the peer to become ready for "
851                                 "GO Negotiation");
852                         dev->flags |= P2P_DEV_NOT_YET_READY;
853                         dev->wait_count = 0;
854                         p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
855                         p2p_set_timeout(p2p, 0, 0);
856                 } else {
857                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
858                                 "P2P: Stop GO Negotiation attempt");
859                         p2p_go_neg_failed(p2p, dev, *msg.status);
860                 }
861                 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
862                 p2p_parse_free(&msg);
863                 return;
864         }
865
866         if (!msg.capability) {
867                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
868                         "P2P: Mandatory Capability attribute missing from GO "
869                         "Negotiation Response");
870 #ifdef CONFIG_P2P_STRICT
871                 status = P2P_SC_FAIL_INVALID_PARAMS;
872                 goto fail;
873 #endif /* CONFIG_P2P_STRICT */
874         }
875
876         if (!msg.p2p_device_info) {
877                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
878                         "P2P: Mandatory P2P Device Info attribute missing "
879                         "from GO Negotiation Response");
880 #ifdef CONFIG_P2P_STRICT
881                 status = P2P_SC_FAIL_INVALID_PARAMS;
882                 goto fail;
883 #endif /* CONFIG_P2P_STRICT */
884         }
885
886         if (!msg.intended_addr) {
887                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
888                         "P2P: No Intended P2P Interface Address attribute "
889                         "received");
890                 status = P2P_SC_FAIL_INVALID_PARAMS;
891                 goto fail;
892         }
893
894         if (!msg.go_intent) {
895                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
896                         "P2P: No GO Intent attribute received");
897                 status = P2P_SC_FAIL_INVALID_PARAMS;
898                 goto fail;
899         }
900         if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
901                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
902                         "P2P: Invalid GO Intent value (%u) received",
903                         *msg.go_intent >> 1);
904                 status = P2P_SC_FAIL_INVALID_PARAMS;
905                 goto fail;
906         }
907
908         go = p2p_go_det(p2p->go_intent, *msg.go_intent);
909         if (go < 0) {
910                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
911                         "P2P: Incompatible GO Intent");
912                 status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
913                 goto fail;
914         }
915
916         if (!go && msg.group_id) {
917                 /* Store SSID for Provisioning step */
918                 p2p->ssid_len = msg.group_id_len - ETH_ALEN;
919                 os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
920         } else if (!go) {
921                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
922                         "P2P: Mandatory P2P Group ID attribute missing from "
923                         "GO Negotiation Response");
924                 p2p->ssid_len = 0;
925 #ifdef CONFIG_P2P_STRICT
926                 status = P2P_SC_FAIL_INVALID_PARAMS;
927                 goto fail;
928 #endif /* CONFIG_P2P_STRICT */
929         }
930
931         if (!msg.config_timeout) {
932                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
933                         "P2P: Mandatory Configuration Timeout attribute "
934                         "missing from GO Negotiation Response");
935 #ifdef CONFIG_P2P_STRICT
936                 status = P2P_SC_FAIL_INVALID_PARAMS;
937                 goto fail;
938 #endif /* CONFIG_P2P_STRICT */
939         } else {
940                 dev->go_timeout = msg.config_timeout[0];
941                 dev->client_timeout = msg.config_timeout[1];
942         }
943
944         if (!msg.operating_channel && !go) {
945                 /*
946                  * Note: P2P Client may omit Operating Channel attribute to
947                  * indicate it does not have a preference.
948                  */
949                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
950                         "P2P: No Operating Channel attribute received");
951                 status = P2P_SC_FAIL_INVALID_PARAMS;
952                 goto fail;
953         }
954         if (!msg.channel_list) {
955                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
956                         "P2P: No Channel List attribute received");
957                 status = P2P_SC_FAIL_INVALID_PARAMS;
958                 goto fail;
959         }
960
961         if (p2p_peer_channels(p2p, dev, msg.channel_list,
962                               msg.channel_list_len) < 0) {
963                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
964                         "P2P: No common channels found");
965                 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
966                 goto fail;
967         }
968
969         if (msg.operating_channel) {
970                 dev->oper_freq = p2p_channel_to_freq((const char *)
971                                                      msg.operating_channel,
972                                                      msg.operating_channel[3],
973                                                      msg.operating_channel[4]);
974                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
975                         "channel preference: %d MHz", dev->oper_freq);
976         } else
977                 dev->oper_freq = 0;
978
979         switch (msg.dev_password_id) {
980         case DEV_PW_REGISTRAR_SPECIFIED:
981                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
982                         "P2P: PIN from peer Display");
983                 if (dev->wps_method != WPS_PIN_KEYPAD) {
984                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
985                                 "P2P: We have wps_method=%s -> "
986                                 "incompatible",
987                                 p2p_wps_method_str(dev->wps_method));
988                         status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
989                         goto fail;
990                 }
991                 break;
992         case DEV_PW_USER_SPECIFIED:
993                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
994                         "P2P: Peer entered PIN on Keypad");
995                 if (dev->wps_method != WPS_PIN_DISPLAY) {
996                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
997                                 "P2P: We have wps_method=%s -> "
998                                 "incompatible",
999                                 p2p_wps_method_str(dev->wps_method));
1000                         status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1001                         goto fail;
1002                 }
1003                 break;
1004         case DEV_PW_PUSHBUTTON:
1005                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1006                         "P2P: Peer using pushbutton");
1007                 if (dev->wps_method != WPS_PBC) {
1008                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1009                                 "P2P: We have wps_method=%s -> "
1010                                 "incompatible",
1011                                 p2p_wps_method_str(dev->wps_method));
1012                         status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1013                         goto fail;
1014                 }
1015                 break;
1016         default:
1017                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1018                         "P2P: Unsupported Device Password ID %d",
1019                         msg.dev_password_id);
1020                 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1021                 goto fail;
1022         }
1023
1024         if (go) {
1025                 struct p2p_channels intersection;
1026                 size_t i;
1027                 p2p_channels_intersect(&p2p->channels, &dev->channels,
1028                                        &intersection);
1029                 if (intersection.reg_classes == 0 ||
1030                     intersection.reg_class[0].channels == 0) {
1031                         status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
1032                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1033                                 "P2P: No common channels found");
1034                         goto fail;
1035                 }
1036                 for (i = 0; i < intersection.reg_classes; i++) {
1037                         struct p2p_reg_class *c;
1038                         c = &intersection.reg_class[i];
1039                         wpa_printf(MSG_DEBUG, "P2P: reg_class %u",
1040                                    c->reg_class);
1041                         wpa_hexdump(MSG_DEBUG, "P2P: channels",
1042                                     c->channel, c->channels);
1043                 }
1044                 if (!p2p_channels_includes(&intersection, p2p->op_reg_class,
1045                                            p2p->op_channel))
1046                         p2p_reselect_channel(p2p, &intersection);
1047
1048                 if (!p2p->ssid_set) {
1049                         p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
1050                         p2p->ssid_set = 1;
1051                 }
1052         }
1053
1054         p2p_set_state(p2p, P2P_GO_NEG);
1055         p2p_clear_timeout(p2p);
1056
1057         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1058                 "P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
1059         os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
1060
1061 fail:
1062         conf = p2p_build_go_neg_conf(p2p, dev, msg.dialog_token, status,
1063                                      msg.operating_channel, go);
1064         p2p_parse_free(&msg);
1065         if (conf == NULL)
1066                 return;
1067         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1068                 "P2P: Sending GO Negotiation Confirm");
1069         if (status == P2P_SC_SUCCESS) {
1070                 p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
1071                 dev->go_state = go ? LOCAL_GO : REMOTE_GO;
1072         } else
1073                 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1074         if (rx_freq > 0)
1075                 freq = rx_freq;
1076         else
1077                 freq = dev->listen_freq;
1078         if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr, sa,
1079                             wpabuf_head(conf), wpabuf_len(conf), 0) < 0) {
1080                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1081                         "P2P: Failed to send Action frame");
1082                 p2p_go_neg_failed(p2p, dev, -1);
1083         }
1084         wpabuf_free(conf);
1085 }
1086
1087
1088 void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa,
1089                              const u8 *data, size_t len)
1090 {
1091         struct p2p_device *dev;
1092         struct p2p_message msg;
1093
1094         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1095                 "P2P: Received GO Negotiation Confirm from " MACSTR,
1096                 MAC2STR(sa));
1097         dev = p2p_get_device(p2p, sa);
1098         if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
1099             dev != p2p->go_neg_peer) {
1100                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1101                         "P2P: Not ready for GO negotiation with " MACSTR,
1102                         MAC2STR(sa));
1103                 return;
1104         }
1105
1106         if (p2p->pending_action_state == P2P_PENDING_GO_NEG_RESPONSE) {
1107                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopped waiting "
1108                         "for TX status on GO Negotiation Response since we "
1109                         "already received Confirmation");
1110                 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1111         }
1112
1113         if (p2p_parse(data, len, &msg))
1114                 return;
1115
1116         if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
1117                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1118                         "P2P: Was not expecting GO Negotiation Confirm - "
1119                         "ignore");
1120                 return;
1121         }
1122         dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1123
1124         if (msg.dialog_token != dev->dialog_token) {
1125                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1126                         "P2P: Unexpected Dialog Token %u (expected %u)",
1127                         msg.dialog_token, dev->dialog_token);
1128                 p2p_parse_free(&msg);
1129                 return;
1130         }
1131
1132         if (!msg.status) {
1133                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1134                         "P2P: No Status attribute received");
1135                 p2p_parse_free(&msg);
1136                 return;
1137         }
1138         if (*msg.status) {
1139                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1140                         "P2P: GO Negotiation rejected: status %d",
1141                         *msg.status);
1142                 p2p_parse_free(&msg);
1143                 return;
1144         }
1145
1146         if (dev->go_state == REMOTE_GO && msg.group_id) {
1147                 /* Store SSID for Provisioning step */
1148                 p2p->ssid_len = msg.group_id_len - ETH_ALEN;
1149                 os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
1150         } else if (dev->go_state == REMOTE_GO) {
1151                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1152                         "P2P: Mandatory P2P Group ID attribute missing from "
1153                         "GO Negotiation Confirmation");
1154                 p2p->ssid_len = 0;
1155 #ifdef CONFIG_P2P_STRICT
1156                 p2p_parse_free(&msg);
1157                 return;
1158 #endif /* CONFIG_P2P_STRICT */
1159         }
1160
1161         if (!msg.operating_channel) {
1162                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1163                         "P2P: Mandatory Operating Channel attribute missing "
1164                         "from GO Negotiation Confirmation");
1165 #ifdef CONFIG_P2P_STRICT
1166                 p2p_parse_free(&msg);
1167                 return;
1168 #endif /* CONFIG_P2P_STRICT */
1169         }
1170
1171         if (!msg.channel_list) {
1172                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1173                         "P2P: Mandatory Operating Channel attribute missing "
1174                         "from GO Negotiation Confirmation");
1175 #ifdef CONFIG_P2P_STRICT
1176                 p2p_parse_free(&msg);
1177                 return;
1178 #endif /* CONFIG_P2P_STRICT */
1179         }
1180
1181         p2p_parse_free(&msg);
1182
1183         if (dev->go_state == UNKNOWN_GO) {
1184                 /*
1185                  * This should not happen since GO negotiation has already
1186                  * been completed.
1187                  */
1188                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1189                         "P2P: Unexpected GO Neg state - do not know which end "
1190                         "becomes GO");
1191                 return;
1192         }
1193
1194         p2p_go_complete(p2p, dev);
1195 }