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