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