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