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