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