P2P: Fix device discoverability to not wait before sending GO Neg Req
[libeap.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 program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "common/ieee802_11_defs.h"
19 #include "wps/wps_defs.h"
20 #include "p2p_i.h"
21 #include "p2p.h"
22
23
24 static int p2p_go_det(u8 own_intent, u8 peer_value)
25 {
26         u8 peer_intent = peer_value >> 1;
27         if (own_intent == peer_intent) {
28                 if (own_intent == P2P_MAX_GO_INTENT)
29                         return -1; /* both devices want to become GO */
30
31                 /* Use tie breaker bit to determine GO */
32                 return (peer_value & 0x01) ? 0 : 1;
33         }
34
35         return own_intent > peer_intent;
36 }
37
38
39 int p2p_peer_channels_check(struct p2p_data *p2p, struct p2p_channels *own,
40                             struct p2p_device *dev,
41                             const u8 *channel_list, size_t channel_list_len)
42 {
43         const u8 *pos, *end;
44         struct p2p_channels *ch;
45         size_t channels;
46         struct p2p_channels intersection;
47
48         ch = &dev->channels;
49         os_memset(ch, 0, sizeof(*ch));
50         pos = channel_list;
51         end = channel_list + channel_list_len;
52
53         if (end - pos < 3)
54                 return -1;
55         os_memcpy(dev->country, pos, 3);
56         wpa_hexdump_ascii(MSG_DEBUG, "P2P: Peer country", pos, 3);
57         if (pos[2] != 0x04 && os_memcmp(pos, p2p->cfg->country, 2) != 0) {
58                 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
59                         "P2P: Mismatching country (ours=%c%c peer's=%c%c)",
60                         p2p->cfg->country[0], p2p->cfg->country[1],
61                         pos[0], pos[1]);
62                 return -1;
63         }
64         pos += 3;
65
66         while (pos + 2 < end) {
67                 struct p2p_reg_class *cl = &ch->reg_class[ch->reg_classes];
68                 cl->reg_class = *pos++;
69                 if (pos + 1 + pos[0] > end) {
70                         wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
71                                 "P2P: Invalid peer Channel List");
72                         return -1;
73                 }
74                 channels = *pos++;
75                 cl->channels = channels > P2P_MAX_REG_CLASS_CHANNELS ?
76                         P2P_MAX_REG_CLASS_CHANNELS : channels;
77                 os_memcpy(cl->channel, pos, cl->channels);
78                 pos += channels;
79                 ch->reg_classes++;
80                 if (ch->reg_classes == P2P_MAX_REG_CLASSES)
81                         break;
82         }
83
84         p2p_channels_intersect(own, &dev->channels, &intersection);
85         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Own reg_classes %d "
86                 "peer reg_classes %d intersection reg_classes %d",
87                 (int) own->reg_classes,
88                 (int) dev->channels.reg_classes,
89                 (int) intersection.reg_classes);
90         if (intersection.reg_classes == 0) {
91                 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
92                         "P2P: No common channels found");
93                 return -1;
94         }
95         return 0;
96 }
97
98
99 static int p2p_peer_channels(struct p2p_data *p2p, struct p2p_device *dev,
100                              const u8 *channel_list, size_t channel_list_len)
101 {
102         return p2p_peer_channels_check(p2p, &p2p->channels, dev,
103                                        channel_list, channel_list_len);
104 }
105
106
107 static u16 p2p_wps_method_pw_id(enum p2p_wps_method wps_method)
108 {
109         switch (wps_method) {
110         case WPS_PIN_LABEL:
111                 return DEV_PW_DEFAULT;
112         case WPS_PIN_DISPLAY:
113                 return DEV_PW_REGISTRAR_SPECIFIED;
114         case WPS_PIN_KEYPAD:
115                 return DEV_PW_USER_SPECIFIED;
116         case WPS_PBC:
117                 return DEV_PW_PUSHBUTTON;
118         default:
119                 return DEV_PW_DEFAULT;
120         }
121 }
122
123
124 static const char * p2p_wps_method_str(enum p2p_wps_method wps_method)
125 {
126         switch (wps_method) {
127         case WPS_PIN_LABEL:
128                 return "Label";
129         case WPS_PIN_DISPLAY:
130                 return "Display";
131         case WPS_PIN_KEYPAD:
132                 return "Keypad";
133         case WPS_PBC:
134                 return "PBC";
135         default:
136                 return "??";
137         }
138 }
139
140
141 static struct wpabuf * p2p_build_go_neg_req(struct p2p_data *p2p,
142                                             struct p2p_device *peer)
143 {
144         struct wpabuf *buf;
145         u8 *len;
146         u8 group_capab;
147
148         buf = wpabuf_alloc(1000);
149         if (buf == NULL)
150                 return NULL;
151
152         peer->dialog_token++;
153         if (peer->dialog_token == 0)
154                 peer->dialog_token = 1;
155         p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_REQ, peer->dialog_token);
156
157         len = p2p_buf_add_ie_hdr(buf);
158         group_capab = 0;
159         if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP)
160                 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
161         p2p_buf_add_capability(buf, p2p->dev_capab, group_capab);
162         p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) |
163                               p2p->next_tie_breaker);
164         p2p->next_tie_breaker = !p2p->next_tie_breaker;
165         p2p_buf_add_config_timeout(buf, 100, 20);
166         p2p_buf_add_listen_channel(buf, p2p->cfg->country, p2p->cfg->reg_class,
167                                    p2p->cfg->channel);
168         if (p2p->ext_listen_interval)
169                 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
170                                               p2p->ext_listen_interval);
171         p2p_buf_add_intended_addr(buf, p2p->intended_addr);
172         p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
173         p2p_buf_add_device_info(buf, p2p, peer);
174         p2p_buf_add_operating_channel(buf, p2p->cfg->country,
175                                       p2p->op_reg_class, p2p->op_channel);
176         p2p_buf_update_ie_hdr(buf, len);
177
178         /* WPS IE with Device Password ID attribute */
179         p2p_build_wps_ie(p2p, buf, p2p_wps_method_pw_id(peer->wps_method), 0);
180
181         return buf;
182 }
183
184
185 int p2p_connect_send(struct p2p_data *p2p, struct p2p_device *dev)
186 {
187         struct wpabuf *req;
188         int freq;
189
190         freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
191         if (freq <= 0) {
192                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
193                         "P2P: No Listen/Operating frequency known for the "
194                         "peer " MACSTR " to send GO Negotiation Request",
195                         MAC2STR(dev->p2p_device_addr));
196                 return -1;
197         }
198
199         req = p2p_build_go_neg_req(p2p, dev);
200         if (req == NULL)
201                 return -1;
202         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
203                 "P2P: Sending GO Negotiation Request");
204         p2p_set_state(p2p, P2P_CONNECT);
205         p2p->pending_action_state = P2P_PENDING_GO_NEG_REQUEST;
206         p2p->go_neg_peer = dev;
207         dev->flags |= P2P_DEV_WAIT_GO_NEG_RESPONSE;
208         if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq,
209                                   dev->p2p_device_addr, p2p->cfg->dev_addr,
210                                   dev->p2p_device_addr,
211                                   wpabuf_head(req), wpabuf_len(req), 200) < 0)
212         {
213                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
214                         "P2P: Failed to send Action frame");
215                 /* Use P2P find to recover and retry */
216                 p2p_set_timeout(p2p, 0, 0);
217         }
218
219         wpabuf_free(req);
220
221         return 0;
222 }
223
224
225 static struct wpabuf * p2p_build_go_neg_resp(struct p2p_data *p2p,
226                                              struct p2p_device *peer,
227                                              u8 dialog_token, u8 status,
228                                              u8 tie_breaker)
229 {
230         struct wpabuf *buf;
231         u8 *len;
232         u8 group_capab;
233
234         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
235                 "P2P: Building GO Negotiation Response");
236         buf = wpabuf_alloc(1000);
237         if (buf == NULL)
238                 return NULL;
239
240         p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_RESP, dialog_token);
241
242         len = p2p_buf_add_ie_hdr(buf);
243         p2p_buf_add_status(buf, status);
244         group_capab = 0;
245         if (peer && (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP))
246                 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
247         p2p_buf_add_capability(buf, p2p->dev_capab, group_capab);
248         p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | tie_breaker);
249         p2p_buf_add_config_timeout(buf, 100, 20);
250         if (peer && peer->go_state == REMOTE_GO) {
251                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Omit Operating "
252                         "Channel attribute");
253         } else {
254                 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
255                                               p2p->op_reg_class,
256                                               p2p->op_channel);
257         }
258         p2p_buf_add_intended_addr(buf, p2p->intended_addr);
259         if (status || peer == NULL) {
260                 p2p_buf_add_channel_list(buf, p2p->cfg->country,
261                                          &p2p->channels);
262         } else if (peer->go_state == REMOTE_GO) {
263                 p2p_buf_add_channel_list(buf, p2p->cfg->country,
264                                          &p2p->channels);
265         } else {
266                 struct p2p_channels res;
267                 p2p_channels_intersect(&p2p->channels, &peer->channels,
268                                        &res);
269                 p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
270         }
271         p2p_buf_add_device_info(buf, p2p, peer);
272         if (peer && peer->go_state == LOCAL_GO) {
273                 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
274                                      p2p->ssid_len);
275         }
276         p2p_buf_update_ie_hdr(buf, len);
277
278         /* WPS IE with Device Password ID attribute */
279         p2p_build_wps_ie(p2p, buf,
280                          p2p_wps_method_pw_id(peer ? peer->wps_method :
281                                               WPS_NOT_READY), 0);
282
283         return buf;
284 }
285
286
287 void p2p_process_go_neg_req(struct p2p_data *p2p, const u8 *sa,
288                             const u8 *data, size_t len, int rx_freq)
289 {
290         struct p2p_device *dev = NULL;
291         struct wpabuf *resp;
292         struct p2p_message msg;
293         u8 status = P2P_SC_FAIL_INVALID_PARAMS;
294         int tie_breaker = 0;
295         int freq;
296
297         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
298                 "P2P: Received GO Negotiation Request from " MACSTR
299                 "(freq=%d)", MAC2STR(sa), rx_freq);
300
301         if (p2p_parse(data, len, &msg))
302                 return;
303
304         if (!msg.capability) {
305                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
306                         "P2P: Mandatory Capability attribute missing from GO "
307                         "Negotiation Request");
308 #ifdef CONFIG_P2P_STRICT
309                 goto fail;
310 #endif /* CONFIG_P2P_STRICT */
311         }
312
313         if (msg.go_intent)
314                 tie_breaker = *msg.go_intent & 0x01;
315         else {
316                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
317                         "P2P: Mandatory GO Intent attribute missing from GO "
318                         "Negotiation Request");
319 #ifdef CONFIG_P2P_STRICT
320                 goto fail;
321 #endif /* CONFIG_P2P_STRICT */
322         }
323
324         if (!msg.config_timeout) {
325                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
326                         "P2P: Mandatory Configuration Timeout attribute "
327                         "missing from GO Negotiation Request");
328 #ifdef CONFIG_P2P_STRICT
329                 goto fail;
330 #endif /* CONFIG_P2P_STRICT */
331         }
332
333         if (!msg.listen_channel) {
334                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
335                         "P2P: No Listen Channel attribute received");
336                 goto fail;
337         }
338         if (!msg.operating_channel) {
339                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
340                         "P2P: No Operating Channel attribute received");
341                 goto fail;
342         }
343         if (!msg.channel_list) {
344                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
345                         "P2P: No Channel List attribute received");
346                 goto fail;
347         }
348         if (!msg.intended_addr) {
349                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
350                         "P2P: No Intended P2P Interface Address attribute "
351                         "received");
352                 goto fail;
353         }
354         if (!msg.p2p_device_info) {
355                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
356                         "P2P: No P2P Device Info attribute received");
357                 goto fail;
358         }
359
360         if (os_memcmp(msg.p2p_device_addr, sa, ETH_ALEN) != 0) {
361                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
362                         "P2P: Unexpected GO Negotiation Request SA=" MACSTR
363                         " != dev_addr=" MACSTR,
364                         MAC2STR(sa), MAC2STR(msg.p2p_device_addr));
365                 goto fail;
366         }
367
368         dev = p2p_get_device(p2p, sa);
369
370         if (msg.status && *msg.status) {
371                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
372                         "P2P: Unexpected Status attribute (%d) in GO "
373                         "Negotiation Request", *msg.status);
374                 goto fail;
375         }
376
377         if (dev == NULL)
378                 dev = p2p_add_dev_from_go_neg_req(p2p, sa, &msg);
379         else if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
380                 p2p_add_dev_info(p2p, sa, dev, &msg);
381         if (dev && dev->flags & P2P_DEV_USER_REJECTED) {
382                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
383                         "P2P: User has rejected this peer");
384                 status = P2P_SC_FAIL_REJECTED_BY_USER;
385         } else if (dev == NULL || dev->wps_method == WPS_NOT_READY) {
386                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
387                         "P2P: Not ready for GO negotiation with " MACSTR,
388                         MAC2STR(sa));
389                 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
390                 if (dev)
391                         dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
392                 p2p->cfg->go_neg_req_rx(p2p->cfg->cb_ctx, sa);
393         } else if (p2p->go_neg_peer && p2p->go_neg_peer != dev) {
394                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
395                         "P2P: Already in Group Formation with another peer");
396                 status = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
397         } else {
398                 int go;
399
400                 if (!p2p->go_neg_peer) {
401                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting "
402                                 "GO Negotiation with previously authorized "
403                                 "peer");
404                         if (!(dev->flags & P2P_DEV_FORCE_FREQ)) {
405                                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
406                                         "P2P: Use default channel settings");
407                                 p2p->op_reg_class = p2p->cfg->op_reg_class;
408                                 p2p->op_channel = p2p->cfg->op_channel;
409                                 os_memcpy(&p2p->channels, &p2p->cfg->channels,
410                                           sizeof(struct p2p_channels));
411                         } else {
412                                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
413                                         "P2P: Use previously configured "
414                                         "forced channel settings");
415                         }
416                 }
417
418                 dev->flags &= ~P2P_DEV_NOT_YET_READY;
419
420                 if (!msg.go_intent) {
421                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
422                                 "P2P: No GO Intent attribute received");
423                         goto fail;
424                 }
425                 if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
426                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
427                                 "P2P: Invalid GO Intent value (%u) received",
428                                 *msg.go_intent >> 1);
429                         goto fail;
430                 }
431
432                 if (dev->go_neg_req_sent &&
433                     os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) > 0) {
434                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
435                                 "P2P: Do not reply since peer has higher "
436                                 "address and GO Neg Request already sent");
437                         p2p_parse_free(&msg);
438                         return;
439                 }
440
441                 go = p2p_go_det(p2p->go_intent, *msg.go_intent);
442                 if (go < 0) {
443                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
444                                 "P2P: Incompatible GO Intent");
445                         status = P2P_SC_FAIL_BOTH_GO_INTENT_15;
446                         goto fail;
447                 }
448
449                 if (p2p_peer_channels(p2p, dev, msg.channel_list,
450                                       msg.channel_list_len) < 0) {
451                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
452                                 "P2P: No common channels found");
453                         status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
454                         goto fail;
455                 }
456
457                 switch (msg.dev_password_id) {
458                 case DEV_PW_DEFAULT:
459                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
460                                 "P2P: PIN from peer Label");
461                         if (dev->wps_method != WPS_PIN_KEYPAD) {
462                                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
463                                         "P2P: We have wps_method=%s -> "
464                                         "incompatible",
465                                         p2p_wps_method_str(dev->wps_method));
466                                 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
467                                 goto fail;
468                         }
469                         break;
470                 case DEV_PW_REGISTRAR_SPECIFIED:
471                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
472                                 "P2P: PIN from peer Display");
473                         if (dev->wps_method != WPS_PIN_KEYPAD) {
474                                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
475                                         "P2P: We have wps_method=%s -> "
476                                         "incompatible",
477                                         p2p_wps_method_str(dev->wps_method));
478                                 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
479                                 goto fail;
480                         }
481                         break;
482                 case DEV_PW_USER_SPECIFIED:
483                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
484                                 "P2P: Peer entered PIN on Keypad");
485                         if (dev->wps_method != WPS_PIN_LABEL &&
486                             dev->wps_method != WPS_PIN_DISPLAY) {
487                                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
488                                         "P2P: We have wps_method=%s -> "
489                                         "incompatible",
490                                         p2p_wps_method_str(dev->wps_method));
491                                 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
492                                 goto fail;
493                         }
494                         break;
495                 case DEV_PW_PUSHBUTTON:
496                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
497                                 "P2P: Peer using pushbutton");
498                         if (dev->wps_method != WPS_PBC) {
499                                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
500                                         "P2P: We have wps_method=%s -> "
501                                         "incompatible",
502                                         p2p_wps_method_str(dev->wps_method));
503                                 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
504                                 goto fail;
505                         }
506                         break;
507                 default:
508                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
509                                 "P2P: Unsupported Device Password ID %d",
510                                 msg.dev_password_id);
511                         status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
512                         goto fail;
513                 }
514
515                 if (go) {
516                         struct p2p_channels intersection;
517                         size_t i;
518                         p2p_channels_intersect(&p2p->channels, &dev->channels,
519                                                &intersection);
520                         if (intersection.reg_classes == 0 ||
521                             intersection.reg_class[0].channels == 0) {
522                                 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
523                                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
524                                         "P2P: No common channels found");
525                                 goto fail;
526                         }
527                         for (i = 0; i < intersection.reg_classes; i++) {
528                                 struct p2p_reg_class *c;
529                                 c = &intersection.reg_class[i];
530                                 wpa_printf(MSG_DEBUG, "P2P: reg_class %u",
531                                            c->reg_class);
532                                 wpa_hexdump(MSG_DEBUG, "P2P: channels",
533                                             c->channel, c->channels);
534                         }
535                         if (!p2p_channels_includes(&intersection,
536                                                    p2p->op_reg_class,
537                                                    p2p->op_channel)) {
538                                 struct p2p_reg_class *cl;
539                                 cl = &intersection.reg_class[0];
540                                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
541                                         "P2P: Selected operating channel "
542                                         "(reg_class %u channel %u) not "
543                                         "acceptable to the peer - pick "
544                                         "another channel (reg_class %u "
545                                         "channel %u)",
546                                         p2p->op_reg_class, p2p->op_channel,
547                                         cl->reg_class, cl->channel[0]);
548                                 p2p->op_reg_class = cl->reg_class;
549                                 p2p->op_channel = cl->channel[0];
550                         }
551
552                         p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
553                 }
554
555                 dev->go_state = go ? LOCAL_GO : REMOTE_GO;
556                 dev->oper_freq = p2p_channel_to_freq((const char *)
557                                                      msg.operating_channel,
558                                                      msg.operating_channel[3],
559                                                      msg.operating_channel[4]);
560                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
561                         "channel preference: %d MHz", dev->oper_freq);
562
563                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
564                         "P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
565                 if (p2p->state != P2P_IDLE)
566                         p2p_stop_find(p2p);
567                 p2p_set_state(p2p, P2P_GO_NEG);
568                 p2p_clear_timeout(p2p);
569                 dev->dialog_token = msg.dialog_token;
570                 os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
571                 p2p->go_neg_peer = dev;
572                 status = P2P_SC_SUCCESS;
573         }
574
575 fail:
576         resp = p2p_build_go_neg_resp(p2p, dev, msg.dialog_token, status,
577                                      !tie_breaker);
578         p2p_parse_free(&msg);
579         if (resp == NULL)
580                 return;
581         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
582                 "P2P: Sending GO Negotiation Response");
583         if (rx_freq > 0)
584                 freq = rx_freq;
585         else
586                 freq = p2p_channel_to_freq(p2p->cfg->country,
587                                            p2p->cfg->reg_class,
588                                            p2p->cfg->channel);
589         if (freq < 0) {
590                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
591                         "P2P: Unknown regulatory class/channel");
592                 wpabuf_free(resp);
593                 return;
594         }
595         if (status == P2P_SC_SUCCESS) {
596                 p2p->pending_action_state = P2P_PENDING_GO_NEG_RESPONSE;
597                 dev->flags |= P2P_DEV_WAIT_GO_NEG_CONFIRM;
598         } else
599                 p2p->pending_action_state =
600                         P2P_PENDING_GO_NEG_RESPONSE_FAILURE;
601         if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, sa,
602                                   p2p->cfg->dev_addr, p2p->cfg->dev_addr,
603                                   wpabuf_head(resp), wpabuf_len(resp), 200) <
604             0) {
605                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
606                         "P2P: Failed to send Action frame");
607         }
608
609         wpabuf_free(resp);
610 }
611
612
613 static struct wpabuf * p2p_build_go_neg_conf(struct p2p_data *p2p,
614                                              struct p2p_device *peer,
615                                              u8 dialog_token, u8 status,
616                                              const u8 *resp_chan, int go)
617 {
618         struct wpabuf *buf;
619         u8 *len;
620         struct p2p_channels res;
621         u8 group_capab;
622
623         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
624                 "P2P: Building GO Negotiation Confirm");
625         buf = wpabuf_alloc(1000);
626         if (buf == NULL)
627                 return NULL;
628
629         p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_CONF, dialog_token);
630
631         len = p2p_buf_add_ie_hdr(buf);
632         p2p_buf_add_status(buf, status);
633         group_capab = 0;
634         if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP)
635                 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
636         p2p_buf_add_capability(buf, p2p->dev_capab, group_capab);
637         if (go || resp_chan == NULL)
638                 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
639                                               p2p->op_reg_class,
640                                               p2p->op_channel);
641         else
642                 p2p_buf_add_operating_channel(buf, (const char *) resp_chan,
643                                               resp_chan[3], resp_chan[4]);
644         p2p_channels_intersect(&p2p->channels, &peer->channels, &res);
645         p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
646         if (go) {
647                 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
648                                      p2p->ssid_len);
649         }
650         p2p_buf_update_ie_hdr(buf, len);
651
652         return buf;
653 }
654
655
656 void p2p_process_go_neg_resp(struct p2p_data *p2p, const u8 *sa,
657                              const u8 *data, size_t len, int rx_freq)
658 {
659         struct p2p_device *dev;
660         struct wpabuf *conf;
661         int go = -1;
662         struct p2p_message msg;
663         u8 status = P2P_SC_SUCCESS;
664         int freq;
665
666         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
667                 "P2P: Received GO Negotiation Response from " MACSTR
668                 " (freq=%d)", MAC2STR(sa), rx_freq);
669         dev = p2p_get_device(p2p, sa);
670         if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
671             dev != p2p->go_neg_peer) {
672                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
673                         "P2P: Not ready for GO negotiation with " MACSTR,
674                         MAC2STR(sa));
675                 return;
676         }
677
678         if (p2p_parse(data, len, &msg))
679                 return;
680
681         if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE)) {
682                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
683                         "P2P: Was not expecting GO Negotiation Response - "
684                         "ignore");
685                 p2p_parse_free(&msg);
686                 return;
687         }
688         dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
689
690         if (msg.dialog_token != dev->dialog_token) {
691                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
692                         "P2P: Unexpected Dialog Token %u (expected %u)",
693                         msg.dialog_token, dev->dialog_token);
694                 p2p_parse_free(&msg);
695                 return;
696         }
697
698         if (!msg.status) {
699                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
700                         "P2P: No Status attribute received");
701                 status = P2P_SC_FAIL_INVALID_PARAMS;
702                 goto fail;
703         }
704         if (*msg.status) {
705                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
706                         "P2P: GO Negotiation rejected: status %d",
707                         *msg.status);
708                 dev->go_neg_req_sent = 0;
709                 if (*msg.status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
710                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
711                                 "P2P: Wait for the peer to become ready for "
712                                 "GO Negotiation");
713                         dev->flags |= P2P_DEV_NOT_YET_READY;
714                         dev->wait_count = 0;
715                         p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
716                         p2p_set_timeout(p2p, 0, 0);
717                 } else {
718                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
719                                 "P2P: Stop GO Negotiation attempt");
720                         p2p_go_neg_failed(p2p, dev, *msg.status);
721                 }
722                 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
723                 p2p_parse_free(&msg);
724                 return;
725         }
726
727         if (!msg.capability) {
728                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
729                         "P2P: Mandatory Capability attribute missing from GO "
730                         "Negotiation Response");
731 #ifdef CONFIG_P2P_STRICT
732                 status = P2P_SC_FAIL_INVALID_PARAMS;
733                 goto fail;
734 #endif /* CONFIG_P2P_STRICT */
735         }
736
737         if (!msg.p2p_device_info) {
738                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
739                         "P2P: Mandatory P2P Device Info attribute missing "
740                         "from GO Negotiation Response");
741 #ifdef CONFIG_P2P_STRICT
742                 status = P2P_SC_FAIL_INVALID_PARAMS;
743                 goto fail;
744 #endif /* CONFIG_P2P_STRICT */
745         }
746
747         if (!msg.intended_addr) {
748                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
749                         "P2P: No Intended P2P Interface Address attribute "
750                         "received");
751                 status = P2P_SC_FAIL_INVALID_PARAMS;
752                 goto fail;
753         }
754
755         if (!msg.go_intent) {
756                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
757                         "P2P: No GO Intent attribute received");
758                 status = P2P_SC_FAIL_INVALID_PARAMS;
759                 goto fail;
760         }
761         if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
762                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
763                         "P2P: Invalid GO Intent value (%u) received",
764                         *msg.go_intent >> 1);
765                 status = P2P_SC_FAIL_INVALID_PARAMS;
766                 goto fail;
767         }
768
769         go = p2p_go_det(p2p->go_intent, *msg.go_intent);
770         if (go < 0) {
771                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
772                         "P2P: Incompatible GO Intent");
773                 status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
774                 goto fail;
775         }
776
777         if (!go && msg.group_id) {
778                 /* TODO: Store SSID for Provisioning step */
779         } else if (!go) {
780                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
781                         "P2P: Mandatory P2P Group ID attribute missing from "
782                         "GO Negotiation Response");
783 #ifdef CONFIG_P2P_STRICT
784                 status = P2P_SC_FAIL_INVALID_PARAMS;
785                 goto fail;
786 #endif /* CONFIG_P2P_STRICT */
787         }
788
789         if (!msg.config_timeout) {
790                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
791                         "P2P: Mandatory Configuration Timeout attribute "
792                         "missing from GO Negotiation Response");
793 #ifdef CONFIG_P2P_STRICT
794                 status = P2P_SC_FAIL_INVALID_PARAMS;
795                 goto fail;
796 #endif /* CONFIG_P2P_STRICT */
797         }
798
799         if (!msg.operating_channel && !go) {
800                 /*
801                  * Note: P2P Client may omit Operating Channel attribute to
802                  * indicate it does not have a preference.
803                  */
804                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
805                         "P2P: No Operating Channel attribute received");
806                 status = P2P_SC_FAIL_INVALID_PARAMS;
807                 goto fail;
808         }
809         if (!msg.channel_list) {
810                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
811                         "P2P: No Channel List attribute received");
812                 status = P2P_SC_FAIL_INVALID_PARAMS;
813                 goto fail;
814         }
815
816         if (p2p_peer_channels(p2p, dev, msg.channel_list,
817                               msg.channel_list_len) < 0) {
818                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
819                         "P2P: No common channels found");
820                 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
821                 goto fail;
822         }
823
824         if (msg.operating_channel) {
825                 dev->oper_freq = p2p_channel_to_freq((const char *)
826                                                      msg.operating_channel,
827                                                      msg.operating_channel[3],
828                                                      msg.operating_channel[4]);
829                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
830                         "channel preference: %d MHz", dev->oper_freq);
831         } else
832                 dev->oper_freq = 0;
833
834         switch (msg.dev_password_id) {
835         case DEV_PW_DEFAULT:
836                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
837                         "P2P: PIN from peer Label");
838                 if (dev->wps_method != WPS_PIN_KEYPAD) {
839                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
840                                 "P2P: We have wps_method=%s -> "
841                                 "incompatible",
842                                 p2p_wps_method_str(dev->wps_method));
843                         status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
844                         goto fail;
845                 }
846                 break;
847         case DEV_PW_REGISTRAR_SPECIFIED:
848                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
849                         "P2P: PIN from peer Display");
850                 if (dev->wps_method != WPS_PIN_KEYPAD) {
851                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
852                                 "P2P: We have wps_method=%s -> "
853                                 "incompatible",
854                                 p2p_wps_method_str(dev->wps_method));
855                         status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
856                         goto fail;
857                 }
858                 break;
859         case DEV_PW_USER_SPECIFIED:
860                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
861                         "P2P: Peer entered PIN on Keypad");
862                 if (dev->wps_method != WPS_PIN_LABEL &&
863                     dev->wps_method != WPS_PIN_DISPLAY) {
864                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
865                                 "P2P: We have wps_method=%s -> "
866                                 "incompatible",
867                                 p2p_wps_method_str(dev->wps_method));
868                         status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
869                         goto fail;
870                 }
871                 break;
872         case DEV_PW_PUSHBUTTON:
873                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
874                         "P2P: Peer using pushbutton");
875                 if (dev->wps_method != WPS_PBC) {
876                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
877                                 "P2P: We have wps_method=%s -> "
878                                 "incompatible",
879                                 p2p_wps_method_str(dev->wps_method));
880                         status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
881                         goto fail;
882                 }
883                 break;
884         default:
885                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
886                         "P2P: Unsupported Device Password ID %d",
887                         msg.dev_password_id);
888                 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
889                 goto fail;
890         }
891
892         if (go) {
893                 struct p2p_channels intersection;
894                 size_t i;
895                 p2p_channels_intersect(&p2p->channels, &dev->channels,
896                                        &intersection);
897                 if (intersection.reg_classes == 0 ||
898                     intersection.reg_class[0].channels == 0) {
899                         status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
900                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
901                                 "P2P: No common channels found");
902                         goto fail;
903                 }
904                 for (i = 0; i < intersection.reg_classes; i++) {
905                         struct p2p_reg_class *c;
906                         c = &intersection.reg_class[i];
907                         wpa_printf(MSG_DEBUG, "P2P: reg_class %u",
908                                    c->reg_class);
909                         wpa_hexdump(MSG_DEBUG, "P2P: channels",
910                                     c->channel, c->channels);
911                 }
912                 if (!p2p_channels_includes(&intersection, p2p->op_reg_class,
913                                            p2p->op_channel)) {
914                         struct p2p_reg_class *cl;
915                         cl = &intersection.reg_class[0];
916                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
917                                 "P2P: Selected operating channel "
918                                 "(reg_class %u channel %u) not "
919                                 "acceptable to the peer - pick "
920                                 "another channel (reg_class %u "
921                                 "channel %u)",
922                                 p2p->op_reg_class, p2p->op_channel,
923                                 cl->reg_class, cl->channel[0]);
924                         p2p->op_reg_class = cl->reg_class;
925                         p2p->op_channel = cl->channel[0];
926                 }
927
928                 p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
929         }
930
931         p2p_set_state(p2p, P2P_GO_NEG);
932         p2p_clear_timeout(p2p);
933
934         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
935                 "P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
936         os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
937
938 fail:
939         conf = p2p_build_go_neg_conf(p2p, dev, msg.dialog_token, status,
940                                      msg.operating_channel, go);
941         p2p_parse_free(&msg);
942         if (conf == NULL)
943                 return;
944         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
945                 "P2P: Sending GO Negotiation Confirm");
946         if (status == P2P_SC_SUCCESS) {
947                 p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
948                 dev->go_state = go ? LOCAL_GO : REMOTE_GO;
949         } else
950                 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
951         if (rx_freq > 0)
952                 freq = rx_freq;
953         else
954                 freq = dev->listen_freq;
955         if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, sa,
956                                   p2p->cfg->dev_addr, sa,
957                                   wpabuf_head(conf), wpabuf_len(conf), 200) <
958             0) {
959                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
960                         "P2P: Failed to send Action frame");
961                 p2p_go_neg_failed(p2p, dev, -1);
962         }
963         wpabuf_free(conf);
964 }
965
966
967 void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa,
968                              const u8 *data, size_t len)
969 {
970         struct p2p_device *dev;
971         struct p2p_message msg;
972
973         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
974                 "P2P: Received GO Negotiation Confirm from " MACSTR,
975                 MAC2STR(sa));
976         dev = p2p_get_device(p2p, sa);
977         if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
978             dev != p2p->go_neg_peer) {
979                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
980                         "P2P: Not ready for GO negotiation with " MACSTR,
981                         MAC2STR(sa));
982                 return;
983         }
984
985         if (p2p->pending_action_state == P2P_PENDING_GO_NEG_RESPONSE) {
986                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopped waiting "
987                         "for TX status on GO Negotiation Response since we "
988                         "already received Confirmation");
989                 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
990         }
991
992         if (p2p_parse(data, len, &msg))
993                 return;
994
995         if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
996                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
997                         "P2P: Was not expecting GO Negotiation Confirm - "
998                         "ignore");
999                 return;
1000         }
1001         dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1002
1003         if (msg.dialog_token != dev->dialog_token) {
1004                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1005                         "P2P: Unexpected Dialog Token %u (expected %u)",
1006                         msg.dialog_token, dev->dialog_token);
1007                 p2p_parse_free(&msg);
1008                 return;
1009         }
1010
1011         if (!msg.status) {
1012                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1013                         "P2P: No Status attribute received");
1014                 p2p_parse_free(&msg);
1015                 return;
1016         }
1017         if (*msg.status) {
1018                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1019                         "P2P: GO Negotiation rejected: status %d",
1020                         *msg.status);
1021                 p2p_parse_free(&msg);
1022                 return;
1023         }
1024
1025         if (dev->go_state == REMOTE_GO && msg.group_id) {
1026                 /* TODO: Store SSID for Provisioning step */
1027         } else if (dev->go_state == REMOTE_GO) {
1028                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1029                         "P2P: Mandatory P2P Group ID attribute missing from "
1030                         "GO Negotiation Confirmation");
1031 #ifdef CONFIG_P2P_STRICT
1032                 p2p_parse_free(&msg);
1033                 return;
1034 #endif /* CONFIG_P2P_STRICT */
1035         }
1036
1037         if (!msg.operating_channel) {
1038                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1039                         "P2P: Mandatory Operating Channel attribute missing "
1040                         "from GO Negotiation Confirmation");
1041 #ifdef CONFIG_P2P_STRICT
1042                 p2p_parse_free(&msg);
1043                 return;
1044 #endif /* CONFIG_P2P_STRICT */
1045         }
1046
1047         if (!msg.channel_list) {
1048                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1049                         "P2P: Mandatory Operating Channel attribute missing "
1050                         "from GO Negotiation Confirmation");
1051 #ifdef CONFIG_P2P_STRICT
1052                 p2p_parse_free(&msg);
1053                 return;
1054 #endif /* CONFIG_P2P_STRICT */
1055         }
1056
1057         p2p_parse_free(&msg);
1058
1059         if (dev->go_state == UNKNOWN_GO) {
1060                 /*
1061                  * This should not happen since GO negotiation has already
1062                  * been completed.
1063                  */
1064                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1065                         "P2P: Unexpected GO Neg state - do not know which end "
1066                         "becomes GO");
1067                 return;
1068         }
1069
1070         p2p_go_complete(p2p, dev);
1071 }