Line data Source code
1 : /*
2 : * Wi-Fi Direct - P2P Group Owner Negotiation
3 : * Copyright (c) 2009-2010, Atheros Communications
4 : *
5 : * This software may be distributed under the terms of the BSD license.
6 : * See README for more details.
7 : */
8 :
9 : #include "includes.h"
10 :
11 : #include "common.h"
12 : #include "utils/eloop.h"
13 : #include "common/ieee802_11_defs.h"
14 : #include "common/wpa_ctrl.h"
15 : #include "wps/wps_defs.h"
16 : #include "p2p_i.h"
17 : #include "p2p.h"
18 :
19 :
20 215 : static int p2p_go_det(u8 own_intent, u8 peer_value)
21 : {
22 215 : u8 peer_intent = peer_value >> 1;
23 215 : if (own_intent == peer_intent) {
24 32 : if (own_intent == P2P_MAX_GO_INTENT)
25 2 : return -1; /* both devices want to become GO */
26 :
27 : /* Use tie breaker bit to determine GO */
28 30 : return (peer_value & 0x01) ? 0 : 1;
29 : }
30 :
31 183 : return own_intent > peer_intent;
32 : }
33 :
34 :
35 289 : int p2p_peer_channels_check(struct p2p_data *p2p, struct p2p_channels *own,
36 : struct p2p_device *dev,
37 : const u8 *channel_list, size_t channel_list_len)
38 : {
39 : const u8 *pos, *end;
40 : struct p2p_channels *ch;
41 : size_t channels;
42 : struct p2p_channels intersection;
43 :
44 289 : ch = &dev->channels;
45 289 : os_memset(ch, 0, sizeof(*ch));
46 289 : pos = channel_list;
47 289 : end = channel_list + channel_list_len;
48 :
49 289 : if (end - pos < 3)
50 0 : return -1;
51 289 : os_memcpy(dev->country, pos, 3);
52 289 : wpa_hexdump_ascii(MSG_DEBUG, "P2P: Peer country", pos, 3);
53 289 : if (pos[2] != 0x04 && os_memcmp(pos, p2p->cfg->country, 2) != 0) {
54 0 : p2p_info(p2p, "Mismatching country (ours=%c%c peer's=%c%c)",
55 0 : p2p->cfg->country[0], p2p->cfg->country[1],
56 0 : pos[0], pos[1]);
57 0 : return -1;
58 : }
59 289 : pos += 3;
60 :
61 966 : while (pos + 2 < end) {
62 390 : struct p2p_reg_class *cl = &ch->reg_class[ch->reg_classes];
63 390 : cl->reg_class = *pos++;
64 390 : if (pos + 1 + pos[0] > end) {
65 1 : p2p_info(p2p, "Invalid peer Channel List");
66 1 : return -1;
67 : }
68 389 : channels = *pos++;
69 389 : cl->channels = channels > P2P_MAX_REG_CLASS_CHANNELS ?
70 389 : P2P_MAX_REG_CLASS_CHANNELS : channels;
71 389 : os_memcpy(cl->channel, pos, cl->channels);
72 389 : pos += channels;
73 389 : ch->reg_classes++;
74 389 : if (ch->reg_classes == P2P_MAX_REG_CLASSES)
75 1 : break;
76 : }
77 :
78 288 : p2p_channels_intersect(own, &dev->channels, &intersection);
79 864 : p2p_dbg(p2p, "Own reg_classes %d peer reg_classes %d intersection reg_classes %d",
80 288 : (int) own->reg_classes,
81 288 : (int) dev->channels.reg_classes,
82 288 : (int) intersection.reg_classes);
83 288 : if (intersection.reg_classes == 0) {
84 5 : p2p_info(p2p, "No common channels found");
85 5 : return -1;
86 : }
87 283 : return 0;
88 : }
89 :
90 :
91 210 : static int p2p_peer_channels(struct p2p_data *p2p, struct p2p_device *dev,
92 : const u8 *channel_list, size_t channel_list_len)
93 : {
94 210 : return p2p_peer_channels_check(p2p, &p2p->channels, dev,
95 : channel_list, channel_list_len);
96 : }
97 :
98 :
99 1152 : u16 p2p_wps_method_pw_id(enum p2p_wps_method wps_method)
100 : {
101 1152 : switch (wps_method) {
102 : case WPS_PIN_DISPLAY:
103 817 : return DEV_PW_REGISTRAR_SPECIFIED;
104 : case WPS_PIN_KEYPAD:
105 106 : return DEV_PW_USER_SPECIFIED;
106 : case WPS_PBC:
107 157 : return DEV_PW_PUSHBUTTON;
108 : case WPS_NFC:
109 24 : return DEV_PW_NFC_CONNECTION_HANDOVER;
110 : case WPS_P2PS:
111 6 : return DEV_PW_P2PS_DEFAULT;
112 : default:
113 42 : return DEV_PW_DEFAULT;
114 : }
115 : }
116 :
117 :
118 4 : static const char * p2p_wps_method_str(enum p2p_wps_method wps_method)
119 : {
120 4 : switch (wps_method) {
121 : case WPS_PIN_DISPLAY:
122 2 : return "Display";
123 : case WPS_PIN_KEYPAD:
124 1 : return "Keypad";
125 : case WPS_PBC:
126 1 : return "PBC";
127 : case WPS_NFC:
128 0 : return "NFC";
129 : case WPS_P2PS:
130 0 : return "P2PS";
131 : default:
132 0 : return "??";
133 : }
134 : }
135 :
136 :
137 204 : 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 204 : size_t extra = 0;
144 : u16 pw_id;
145 :
146 : #ifdef CONFIG_WIFI_DISPLAY
147 204 : if (p2p->wfd_ie_go_neg)
148 2 : extra = wpabuf_len(p2p->wfd_ie_go_neg);
149 : #endif /* CONFIG_WIFI_DISPLAY */
150 :
151 204 : if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ])
152 0 : extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ]);
153 :
154 204 : buf = wpabuf_alloc(1000 + extra);
155 204 : if (buf == NULL)
156 0 : return NULL;
157 :
158 204 : p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_REQ, peer->dialog_token);
159 :
160 204 : len = p2p_buf_add_ie_hdr(buf);
161 204 : group_capab = 0;
162 204 : if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
163 36 : group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
164 36 : if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
165 24 : group_capab |= P2P_GROUP_CAPAB_PERSISTENT_RECONN;
166 : }
167 204 : if (p2p->cross_connect)
168 0 : group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
169 204 : if (p2p->cfg->p2p_intra_bss)
170 204 : group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
171 204 : p2p_buf_add_capability(buf, p2p->dev_capab &
172 : ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
173 : group_capab);
174 204 : p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | peer->tie_breaker);
175 204 : p2p_buf_add_config_timeout(buf, p2p->go_timeout, p2p->client_timeout);
176 204 : p2p_buf_add_listen_channel(buf, p2p->cfg->country, p2p->cfg->reg_class,
177 204 : p2p->cfg->channel);
178 204 : if (p2p->ext_listen_interval)
179 2 : p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
180 2 : p2p->ext_listen_interval);
181 204 : p2p_buf_add_intended_addr(buf, p2p->intended_addr);
182 204 : p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
183 204 : p2p_buf_add_device_info(buf, p2p, peer);
184 408 : p2p_buf_add_operating_channel(buf, p2p->cfg->country,
185 408 : p2p->op_reg_class, p2p->op_channel);
186 204 : p2p_buf_update_ie_hdr(buf, len);
187 :
188 : /* WPS IE with Device Password ID attribute */
189 204 : pw_id = p2p_wps_method_pw_id(peer->wps_method);
190 204 : if (peer->oob_pw_id)
191 8 : pw_id = peer->oob_pw_id;
192 204 : if (p2p_build_wps_ie(p2p, buf, pw_id, 0) < 0) {
193 0 : p2p_dbg(p2p, "Failed to build WPS IE for GO Negotiation Request");
194 0 : wpabuf_free(buf);
195 0 : return NULL;
196 : }
197 :
198 : #ifdef CONFIG_WIFI_DISPLAY
199 204 : if (p2p->wfd_ie_go_neg)
200 2 : wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
201 : #endif /* CONFIG_WIFI_DISPLAY */
202 :
203 204 : if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ])
204 0 : wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ]);
205 :
206 204 : return buf;
207 : }
208 :
209 :
210 206 : int p2p_connect_send(struct p2p_data *p2p, struct p2p_device *dev)
211 : {
212 : struct wpabuf *req;
213 : int freq;
214 :
215 206 : if (dev->flags & P2P_DEV_PD_BEFORE_GO_NEG) {
216 : u16 config_method;
217 12 : p2p_dbg(p2p, "Use PD-before-GO-Neg workaround for " MACSTR,
218 12 : MAC2STR(dev->info.p2p_device_addr));
219 2 : if (dev->wps_method == WPS_PIN_DISPLAY)
220 0 : config_method = WPS_CONFIG_KEYPAD;
221 2 : else if (dev->wps_method == WPS_PIN_KEYPAD)
222 0 : config_method = WPS_CONFIG_DISPLAY;
223 2 : else if (dev->wps_method == WPS_PBC)
224 2 : config_method = WPS_CONFIG_PUSHBUTTON;
225 0 : else if (dev->wps_method == WPS_P2PS)
226 0 : config_method = WPS_CONFIG_P2PS;
227 : else
228 0 : return -1;
229 2 : return p2p_prov_disc_req(p2p, dev->info.p2p_device_addr,
230 : NULL, config_method, 0, 0, 1);
231 : }
232 :
233 204 : freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
234 204 : if (dev->oob_go_neg_freq > 0)
235 8 : freq = dev->oob_go_neg_freq;
236 204 : if (freq <= 0) {
237 0 : p2p_dbg(p2p, "No Listen/Operating frequency known for the peer "
238 : MACSTR " to send GO Negotiation Request",
239 0 : MAC2STR(dev->info.p2p_device_addr));
240 0 : return -1;
241 : }
242 :
243 204 : req = p2p_build_go_neg_req(p2p, dev);
244 204 : if (req == NULL)
245 0 : return -1;
246 204 : p2p_dbg(p2p, "Sending GO Negotiation Request");
247 204 : p2p_set_state(p2p, P2P_CONNECT);
248 204 : p2p->pending_action_state = P2P_PENDING_GO_NEG_REQUEST;
249 204 : p2p->go_neg_peer = dev;
250 204 : eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
251 204 : dev->flags |= P2P_DEV_WAIT_GO_NEG_RESPONSE;
252 204 : dev->connect_reqs++;
253 408 : if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
254 204 : p2p->cfg->dev_addr, dev->info.p2p_device_addr,
255 204 : wpabuf_head(req), wpabuf_len(req), 500) < 0) {
256 4 : p2p_dbg(p2p, "Failed to send Action frame");
257 : /* Use P2P find to recover and retry */
258 4 : p2p_set_timeout(p2p, 0, 0);
259 : } else
260 200 : dev->go_neg_req_sent++;
261 :
262 204 : wpabuf_free(req);
263 :
264 204 : return 0;
265 : }
266 :
267 :
268 155 : static struct wpabuf * p2p_build_go_neg_resp(struct p2p_data *p2p,
269 : struct p2p_device *peer,
270 : u8 dialog_token, u8 status,
271 : u8 tie_breaker)
272 : {
273 : struct wpabuf *buf;
274 : u8 *len;
275 : u8 group_capab;
276 155 : size_t extra = 0;
277 : u16 pw_id;
278 :
279 155 : p2p_dbg(p2p, "Building GO Negotiation Response");
280 :
281 : #ifdef CONFIG_WIFI_DISPLAY
282 155 : if (p2p->wfd_ie_go_neg)
283 2 : extra = wpabuf_len(p2p->wfd_ie_go_neg);
284 : #endif /* CONFIG_WIFI_DISPLAY */
285 :
286 155 : if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP])
287 0 : extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP]);
288 :
289 155 : buf = wpabuf_alloc(1000 + extra);
290 155 : if (buf == NULL)
291 0 : return NULL;
292 :
293 155 : p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_RESP, dialog_token);
294 :
295 155 : len = p2p_buf_add_ie_hdr(buf);
296 155 : p2p_buf_add_status(buf, status);
297 155 : group_capab = 0;
298 155 : if (peer && peer->go_state == LOCAL_GO) {
299 35 : if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
300 5 : group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
301 5 : if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
302 4 : group_capab |=
303 : P2P_GROUP_CAPAB_PERSISTENT_RECONN;
304 : }
305 35 : if (p2p->cross_connect)
306 0 : group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
307 35 : if (p2p->cfg->p2p_intra_bss)
308 35 : group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
309 : }
310 155 : p2p_buf_add_capability(buf, p2p->dev_capab &
311 : ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
312 : group_capab);
313 155 : p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | tie_breaker);
314 155 : p2p_buf_add_config_timeout(buf, p2p->go_timeout, p2p->client_timeout);
315 155 : if (peer && peer->go_state == REMOTE_GO) {
316 69 : p2p_dbg(p2p, "Omit Operating Channel attribute");
317 : } else {
318 172 : p2p_buf_add_operating_channel(buf, p2p->cfg->country,
319 86 : p2p->op_reg_class,
320 86 : p2p->op_channel);
321 : }
322 155 : p2p_buf_add_intended_addr(buf, p2p->intended_addr);
323 155 : if (status || peer == NULL) {
324 55 : p2p_buf_add_channel_list(buf, p2p->cfg->country,
325 : &p2p->channels);
326 100 : } else if (peer->go_state == REMOTE_GO) {
327 67 : p2p_buf_add_channel_list(buf, p2p->cfg->country,
328 : &p2p->channels);
329 : } else {
330 : struct p2p_channels res;
331 33 : p2p_channels_intersect(&p2p->channels, &peer->channels,
332 : &res);
333 33 : p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
334 : }
335 155 : p2p_buf_add_device_info(buf, p2p, peer);
336 155 : if (peer && peer->go_state == LOCAL_GO) {
337 35 : p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
338 : p2p->ssid_len);
339 : }
340 155 : p2p_buf_update_ie_hdr(buf, len);
341 :
342 : /* WPS IE with Device Password ID attribute */
343 155 : pw_id = p2p_wps_method_pw_id(peer ? peer->wps_method : WPS_NOT_READY);
344 155 : if (peer && peer->oob_pw_id)
345 8 : pw_id = peer->oob_pw_id;
346 155 : if (p2p_build_wps_ie(p2p, buf, pw_id, 0) < 0) {
347 0 : p2p_dbg(p2p, "Failed to build WPS IE for GO Negotiation Response");
348 0 : wpabuf_free(buf);
349 0 : return NULL;
350 : }
351 :
352 : #ifdef CONFIG_WIFI_DISPLAY
353 155 : if (p2p->wfd_ie_go_neg)
354 2 : wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
355 : #endif /* CONFIG_WIFI_DISPLAY */
356 :
357 155 : if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP])
358 0 : wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP]);
359 :
360 155 : return buf;
361 : }
362 :
363 :
364 : /**
365 : * p2p_reselect_channel - Re-select operating channel based on peer information
366 : * @p2p: P2P module context from p2p_init()
367 : * @intersection: Support channel list intersection from local and peer
368 : *
369 : * This function is used to re-select the best channel after having received
370 : * information from the peer to allow supported channel lists to be intersected.
371 : * This can be used to improve initial channel selection done in
372 : * p2p_prepare_channel() prior to the start of GO Negotiation. In addition, this
373 : * can be used for Invitation case.
374 : */
375 93 : void p2p_reselect_channel(struct p2p_data *p2p,
376 : struct p2p_channels *intersection)
377 : {
378 : struct p2p_reg_class *cl;
379 : int freq;
380 : u8 op_reg_class, op_channel;
381 : unsigned int i;
382 93 : const int op_classes_5ghz[] = { 124, 115, 0 };
383 93 : const int op_classes_ht40[] = { 126, 127, 116, 117, 0 };
384 93 : const int op_classes_vht[] = { 128, 0 };
385 :
386 94 : if (p2p->own_freq_preference > 0 &&
387 1 : p2p_freq_to_channel(p2p->own_freq_preference,
388 1 : &op_reg_class, &op_channel) == 0 &&
389 1 : p2p_channels_includes(intersection, op_reg_class, op_channel)) {
390 1 : p2p_dbg(p2p, "Pick own channel preference (reg_class %u channel %u) from intersection",
391 : op_reg_class, op_channel);
392 1 : p2p->op_reg_class = op_reg_class;
393 1 : p2p->op_channel = op_channel;
394 89 : return;
395 : }
396 :
397 92 : if (p2p->best_freq_overall > 0 &&
398 0 : p2p_freq_to_channel(p2p->best_freq_overall,
399 0 : &op_reg_class, &op_channel) == 0 &&
400 0 : p2p_channels_includes(intersection, op_reg_class, op_channel)) {
401 0 : p2p_dbg(p2p, "Pick best overall channel (reg_class %u channel %u) from intersection",
402 : op_reg_class, op_channel);
403 0 : p2p->op_reg_class = op_reg_class;
404 0 : p2p->op_channel = op_channel;
405 0 : return;
406 : }
407 :
408 : /* First, try to pick the best channel from another band */
409 92 : freq = p2p_channel_to_freq(p2p->op_reg_class, p2p->op_channel);
410 92 : if (freq >= 2400 && freq < 2500 && p2p->best_freq_5 > 0 &&
411 0 : !p2p_channels_includes(intersection, p2p->op_reg_class,
412 0 : p2p->op_channel) &&
413 0 : p2p_freq_to_channel(p2p->best_freq_5,
414 0 : &op_reg_class, &op_channel) == 0 &&
415 0 : p2p_channels_includes(intersection, op_reg_class, op_channel)) {
416 0 : p2p_dbg(p2p, "Pick best 5 GHz channel (reg_class %u channel %u) from intersection",
417 : op_reg_class, op_channel);
418 0 : p2p->op_reg_class = op_reg_class;
419 0 : p2p->op_channel = op_channel;
420 0 : return;
421 : }
422 :
423 92 : if (freq >= 4900 && freq < 6000 && p2p->best_freq_24 > 0 &&
424 0 : !p2p_channels_includes(intersection, p2p->op_reg_class,
425 0 : p2p->op_channel) &&
426 0 : p2p_freq_to_channel(p2p->best_freq_24,
427 0 : &op_reg_class, &op_channel) == 0 &&
428 0 : p2p_channels_includes(intersection, op_reg_class, op_channel)) {
429 0 : p2p_dbg(p2p, "Pick best 2.4 GHz channel (reg_class %u channel %u) from intersection",
430 : op_reg_class, op_channel);
431 0 : p2p->op_reg_class = op_reg_class;
432 0 : p2p->op_channel = op_channel;
433 0 : return;
434 : }
435 :
436 : /* Select channel with highest preference if the peer supports it */
437 93 : for (i = 0; p2p->cfg->pref_chan && i < p2p->cfg->num_pref_chan; i++) {
438 4 : if (p2p_channels_includes(intersection,
439 2 : p2p->cfg->pref_chan[i].op_class,
440 2 : p2p->cfg->pref_chan[i].chan)) {
441 1 : p2p->op_reg_class = p2p->cfg->pref_chan[i].op_class;
442 1 : p2p->op_channel = p2p->cfg->pref_chan[i].chan;
443 2 : p2p_dbg(p2p, "Pick highest preferred channel (op_class %u channel %u) from intersection",
444 2 : p2p->op_reg_class, p2p->op_channel);
445 1 : return;
446 : }
447 : }
448 :
449 : /* Try a channel where we might be able to use VHT */
450 91 : if (p2p_channel_select(intersection, op_classes_vht,
451 : &p2p->op_reg_class, &p2p->op_channel) == 0) {
452 4 : p2p_dbg(p2p, "Pick possible VHT channel (op_class %u channel %u) from intersection",
453 4 : p2p->op_reg_class, p2p->op_channel);
454 2 : return;
455 : }
456 :
457 : /* Try a channel where we might be able to use HT40 */
458 89 : if (p2p_channel_select(intersection, op_classes_ht40,
459 : &p2p->op_reg_class, &p2p->op_channel) == 0) {
460 2 : p2p_dbg(p2p, "Pick possible HT40 channel (op_class %u channel %u) from intersection",
461 2 : p2p->op_reg_class, p2p->op_channel);
462 1 : return;
463 : }
464 :
465 : /* Prefer a 5 GHz channel */
466 88 : if (p2p_channel_select(intersection, op_classes_5ghz,
467 : &p2p->op_reg_class, &p2p->op_channel) == 0) {
468 0 : p2p_dbg(p2p, "Pick possible 5 GHz channel (op_class %u channel %u) from intersection",
469 0 : p2p->op_reg_class, p2p->op_channel);
470 0 : return;
471 : }
472 :
473 : /*
474 : * Try to see if the original channel is in the intersection. If
475 : * so, no need to change anything, as it already contains some
476 : * randomness.
477 : */
478 88 : if (p2p_channels_includes(intersection, p2p->op_reg_class,
479 88 : p2p->op_channel)) {
480 166 : p2p_dbg(p2p, "Using original operating class and channel (op_class %u channel %u) from intersection",
481 166 : p2p->op_reg_class, p2p->op_channel);
482 83 : return;
483 : }
484 :
485 : /*
486 : * Fall back to whatever is included in the channel intersection since
487 : * no better options seems to be available.
488 : */
489 5 : cl = &intersection->reg_class[0];
490 10 : p2p_dbg(p2p, "Pick another channel (reg_class %u channel %u) from intersection",
491 10 : cl->reg_class, cl->channel[0]);
492 5 : p2p->op_reg_class = cl->reg_class;
493 5 : p2p->op_channel = cl->channel[0];
494 : }
495 :
496 :
497 123 : int p2p_go_select_channel(struct p2p_data *p2p, struct p2p_device *dev,
498 : u8 *status)
499 : {
500 : struct p2p_channels tmp, intersection;
501 :
502 123 : p2p_channels_dump(p2p, "own channels", &p2p->channels);
503 123 : p2p_channels_dump(p2p, "peer channels", &dev->channels);
504 123 : p2p_channels_intersect(&p2p->channels, &dev->channels, &tmp);
505 123 : p2p_channels_dump(p2p, "intersection", &tmp);
506 123 : p2p_channels_remove_freqs(&tmp, &p2p->no_go_freq);
507 123 : p2p_channels_dump(p2p, "intersection after no-GO removal", &tmp);
508 123 : p2p_channels_intersect(&tmp, &p2p->cfg->channels, &intersection);
509 123 : p2p_channels_dump(p2p, "intersection with local channel list",
510 : &intersection);
511 225 : if (intersection.reg_classes == 0 ||
512 102 : intersection.reg_class[0].channels == 0) {
513 21 : *status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
514 21 : p2p_dbg(p2p, "No common channels found");
515 21 : return -1;
516 : }
517 :
518 102 : if (!p2p_channels_includes(&intersection, p2p->op_reg_class,
519 102 : p2p->op_channel)) {
520 5 : if (dev->flags & P2P_DEV_FORCE_FREQ) {
521 0 : *status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
522 0 : p2p_dbg(p2p, "Peer does not support the forced channel");
523 0 : return -1;
524 : }
525 :
526 10 : p2p_dbg(p2p, "Selected operating channel (op_class %u channel %u) not acceptable to the peer",
527 10 : p2p->op_reg_class, p2p->op_channel);
528 5 : p2p_reselect_channel(p2p, &intersection);
529 178 : } else if (!(dev->flags & P2P_DEV_FORCE_FREQ) &&
530 81 : !p2p->cfg->cfg_op_channel) {
531 160 : p2p_dbg(p2p, "Try to optimize channel selection with peer information received; previously selected op_class %u channel %u",
532 160 : p2p->op_reg_class, p2p->op_channel);
533 80 : p2p_reselect_channel(p2p, &intersection);
534 : }
535 :
536 102 : if (!p2p->ssid_set) {
537 100 : p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
538 100 : p2p->ssid_set = 1;
539 : }
540 :
541 102 : return 0;
542 : }
543 :
544 :
545 159 : void p2p_process_go_neg_req(struct p2p_data *p2p, const u8 *sa,
546 : const u8 *data, size_t len, int rx_freq)
547 : {
548 159 : struct p2p_device *dev = NULL;
549 : struct wpabuf *resp;
550 : struct p2p_message msg;
551 159 : u8 status = P2P_SC_FAIL_INVALID_PARAMS;
552 159 : int tie_breaker = 0;
553 : int freq;
554 :
555 954 : p2p_dbg(p2p, "Received GO Negotiation Request from " MACSTR "(freq=%d)",
556 954 : MAC2STR(sa), rx_freq);
557 :
558 159 : if (p2p_parse(data, len, &msg))
559 5 : return;
560 :
561 158 : if (!msg.capability) {
562 3 : p2p_dbg(p2p, "Mandatory Capability attribute missing from GO Negotiation Request");
563 : #ifdef CONFIG_P2P_STRICT
564 : goto fail;
565 : #endif /* CONFIG_P2P_STRICT */
566 : }
567 :
568 158 : if (msg.go_intent)
569 156 : tie_breaker = *msg.go_intent & 0x01;
570 : else {
571 2 : p2p_dbg(p2p, "Mandatory GO Intent attribute missing from GO Negotiation Request");
572 : #ifdef CONFIG_P2P_STRICT
573 : goto fail;
574 : #endif /* CONFIG_P2P_STRICT */
575 : }
576 :
577 158 : if (!msg.config_timeout) {
578 3 : p2p_dbg(p2p, "Mandatory Configuration Timeout attribute missing from GO Negotiation Request");
579 : #ifdef CONFIG_P2P_STRICT
580 : goto fail;
581 : #endif /* CONFIG_P2P_STRICT */
582 : }
583 :
584 158 : if (!msg.listen_channel) {
585 1 : p2p_dbg(p2p, "No Listen Channel attribute received");
586 1 : goto fail;
587 : }
588 157 : if (!msg.operating_channel) {
589 1 : p2p_dbg(p2p, "No Operating Channel attribute received");
590 1 : goto fail;
591 : }
592 156 : if (!msg.channel_list) {
593 1 : p2p_dbg(p2p, "No Channel List attribute received");
594 1 : goto fail;
595 : }
596 155 : if (!msg.intended_addr) {
597 1 : p2p_dbg(p2p, "No Intended P2P Interface Address attribute received");
598 1 : goto fail;
599 : }
600 154 : if (!msg.p2p_device_info) {
601 1 : p2p_dbg(p2p, "No P2P Device Info attribute received");
602 1 : goto fail;
603 : }
604 :
605 153 : if (os_memcmp(msg.p2p_device_addr, sa, ETH_ALEN) != 0) {
606 12 : p2p_dbg(p2p, "Unexpected GO Negotiation Request SA=" MACSTR
607 : " != dev_addr=" MACSTR,
608 12 : MAC2STR(sa), MAC2STR(msg.p2p_device_addr));
609 1 : goto fail;
610 : }
611 :
612 152 : dev = p2p_get_device(p2p, sa);
613 :
614 152 : if (msg.status && *msg.status) {
615 2 : p2p_dbg(p2p, "Unexpected Status attribute (%d) in GO Negotiation Request",
616 2 : *msg.status);
617 3 : if (dev && p2p->go_neg_peer == dev &&
618 1 : *msg.status == P2P_SC_FAIL_REJECTED_BY_USER) {
619 : /*
620 : * This mechanism for using Status attribute in GO
621 : * Negotiation Request is not compliant with the P2P
622 : * specification, but some deployed devices use it to
623 : * indicate rejection of GO Negotiation in a case where
624 : * they have sent out GO Negotiation Response with
625 : * status 1. The P2P specification explicitly disallows
626 : * this. To avoid unnecessary interoperability issues
627 : * and extra frames, mark the pending negotiation as
628 : * failed and do not reply to this GO Negotiation
629 : * Request frame.
630 : */
631 1 : p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
632 1 : p2p_go_neg_failed(p2p, *msg.status);
633 1 : p2p_parse_free(&msg);
634 1 : return;
635 : }
636 1 : goto fail;
637 : }
638 :
639 150 : if (dev == NULL)
640 6 : dev = p2p_add_dev_from_go_neg_req(p2p, sa, &msg);
641 271 : else if ((dev->flags & P2P_DEV_PROBE_REQ_ONLY) ||
642 127 : !(dev->flags & P2P_DEV_REPORTED))
643 24 : p2p_add_dev_info(p2p, sa, dev, &msg);
644 120 : else if (!dev->listen_freq && !dev->oper_freq) {
645 : /*
646 : * This may happen if the peer entry was added based on PD
647 : * Request and no Probe Request/Response frame has been received
648 : * from this peer (or that information has timed out).
649 : */
650 30 : p2p_dbg(p2p, "Update peer " MACSTR
651 : " based on GO Neg Req since listen/oper freq not known",
652 30 : MAC2STR(dev->info.p2p_device_addr));
653 5 : p2p_add_dev_info(p2p, sa, dev, &msg);
654 : }
655 :
656 150 : if (p2p->go_neg_peer && p2p->go_neg_peer == dev)
657 27 : eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
658 :
659 150 : if (dev && dev->flags & P2P_DEV_USER_REJECTED) {
660 2 : p2p_dbg(p2p, "User has rejected this peer");
661 2 : status = P2P_SC_FAIL_REJECTED_BY_USER;
662 296 : } else if (dev == NULL ||
663 185 : (dev->wps_method == WPS_NOT_READY &&
664 49 : (p2p->authorized_oob_dev_pw_id == 0 ||
665 12 : p2p->authorized_oob_dev_pw_id !=
666 12 : msg.dev_password_id))) {
667 198 : p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR,
668 198 : MAC2STR(sa));
669 33 : status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
670 66 : p2p->cfg->go_neg_req_rx(p2p->cfg->cb_ctx, sa,
671 33 : msg.dev_password_id);
672 115 : } else if (p2p->go_neg_peer && p2p->go_neg_peer != dev) {
673 1 : p2p_dbg(p2p, "Already in Group Formation with another peer");
674 1 : status = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
675 : } else {
676 : int go;
677 :
678 114 : if (!p2p->go_neg_peer) {
679 87 : p2p_dbg(p2p, "Starting GO Negotiation with previously authorized peer");
680 87 : if (!(dev->flags & P2P_DEV_FORCE_FREQ)) {
681 82 : p2p_dbg(p2p, "Use default channel settings");
682 82 : p2p->op_reg_class = p2p->cfg->op_reg_class;
683 82 : p2p->op_channel = p2p->cfg->op_channel;
684 82 : os_memcpy(&p2p->channels, &p2p->cfg->channels,
685 : sizeof(struct p2p_channels));
686 : } else {
687 5 : p2p_dbg(p2p, "Use previously configured forced channel settings");
688 : }
689 : }
690 :
691 114 : dev->flags &= ~P2P_DEV_NOT_YET_READY;
692 :
693 114 : if (!msg.go_intent) {
694 1 : p2p_dbg(p2p, "No GO Intent attribute received");
695 1 : goto fail;
696 : }
697 113 : if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
698 1 : p2p_dbg(p2p, "Invalid GO Intent value (%u) received",
699 1 : *msg.go_intent >> 1);
700 1 : goto fail;
701 : }
702 :
703 115 : if (dev->go_neg_req_sent &&
704 3 : os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) > 0) {
705 2 : p2p_dbg(p2p, "Do not reply since peer has higher address and GO Neg Request already sent");
706 2 : p2p_parse_free(&msg);
707 2 : return;
708 : }
709 :
710 110 : go = p2p_go_det(p2p->go_intent, *msg.go_intent);
711 110 : if (go < 0) {
712 1 : p2p_dbg(p2p, "Incompatible GO Intent");
713 1 : status = P2P_SC_FAIL_BOTH_GO_INTENT_15;
714 1 : goto fail;
715 : }
716 :
717 109 : if (p2p_peer_channels(p2p, dev, msg.channel_list,
718 109 : msg.channel_list_len) < 0) {
719 2 : p2p_dbg(p2p, "No common channels found");
720 2 : status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
721 2 : goto fail;
722 : }
723 :
724 107 : switch (msg.dev_password_id) {
725 : case DEV_PW_REGISTRAR_SPECIFIED:
726 11 : p2p_dbg(p2p, "PIN from peer Display");
727 11 : if (dev->wps_method != WPS_PIN_KEYPAD) {
728 2 : p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
729 : p2p_wps_method_str(dev->wps_method));
730 2 : status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
731 2 : goto fail;
732 : }
733 9 : break;
734 : case DEV_PW_USER_SPECIFIED:
735 63 : p2p_dbg(p2p, "Peer entered PIN on Keypad");
736 63 : if (dev->wps_method != WPS_PIN_DISPLAY) {
737 1 : p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
738 : p2p_wps_method_str(dev->wps_method));
739 1 : status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
740 1 : goto fail;
741 : }
742 62 : break;
743 : case DEV_PW_PUSHBUTTON:
744 22 : p2p_dbg(p2p, "Peer using pushbutton");
745 22 : if (dev->wps_method != WPS_PBC) {
746 1 : p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
747 : p2p_wps_method_str(dev->wps_method));
748 1 : status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
749 1 : goto fail;
750 : }
751 21 : break;
752 : case DEV_PW_P2PS_DEFAULT:
753 2 : p2p_dbg(p2p, "Peer using P2PS pin");
754 2 : if (dev->wps_method != WPS_P2PS) {
755 0 : p2p_dbg(p2p,
756 : "We have wps_method=%s -> incompatible",
757 : p2p_wps_method_str(dev->wps_method));
758 0 : status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
759 0 : goto fail;
760 : }
761 2 : break;
762 : default:
763 17 : if (msg.dev_password_id &&
764 8 : msg.dev_password_id == dev->oob_pw_id) {
765 4 : p2p_dbg(p2p, "Peer using NFC");
766 4 : if (dev->wps_method != WPS_NFC) {
767 0 : p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
768 : p2p_wps_method_str(
769 : dev->wps_method));
770 0 : status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
771 0 : goto fail;
772 : }
773 4 : break;
774 : }
775 : #ifdef CONFIG_WPS_NFC
776 9 : if (p2p->authorized_oob_dev_pw_id &&
777 4 : msg.dev_password_id ==
778 4 : p2p->authorized_oob_dev_pw_id) {
779 4 : p2p_dbg(p2p, "Using static handover with our device password from NFC Tag");
780 4 : dev->wps_method = WPS_NFC;
781 4 : dev->oob_pw_id = p2p->authorized_oob_dev_pw_id;
782 4 : break;
783 : }
784 : #endif /* CONFIG_WPS_NFC */
785 1 : p2p_dbg(p2p, "Unsupported Device Password ID %d",
786 1 : msg.dev_password_id);
787 1 : status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
788 1 : goto fail;
789 : }
790 :
791 102 : if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
792 2 : goto fail;
793 :
794 100 : dev->go_state = go ? LOCAL_GO : REMOTE_GO;
795 100 : dev->oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
796 100 : msg.operating_channel[4]);
797 100 : p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
798 : dev->oper_freq);
799 :
800 100 : if (msg.config_timeout) {
801 100 : dev->go_timeout = msg.config_timeout[0];
802 100 : dev->client_timeout = msg.config_timeout[1];
803 : }
804 :
805 100 : p2p_dbg(p2p, "GO Negotiation with " MACSTR, MAC2STR(sa));
806 100 : if (p2p->state != P2P_IDLE)
807 100 : p2p_stop_find_for_freq(p2p, rx_freq);
808 100 : p2p_set_state(p2p, P2P_GO_NEG);
809 100 : p2p_clear_timeout(p2p);
810 100 : dev->dialog_token = msg.dialog_token;
811 100 : os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
812 100 : p2p->go_neg_peer = dev;
813 100 : eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
814 100 : status = P2P_SC_SUCCESS;
815 : }
816 :
817 : fail:
818 155 : if (dev)
819 148 : dev->status = status;
820 155 : resp = p2p_build_go_neg_resp(p2p, dev, msg.dialog_token, status,
821 : !tie_breaker);
822 155 : p2p_parse_free(&msg);
823 155 : if (resp == NULL)
824 0 : return;
825 155 : p2p_dbg(p2p, "Sending GO Negotiation Response");
826 155 : if (rx_freq > 0)
827 155 : freq = rx_freq;
828 : else
829 0 : freq = p2p_channel_to_freq(p2p->cfg->reg_class,
830 0 : p2p->cfg->channel);
831 155 : if (freq < 0) {
832 0 : p2p_dbg(p2p, "Unknown regulatory class/channel");
833 0 : wpabuf_free(resp);
834 0 : return;
835 : }
836 155 : if (status == P2P_SC_SUCCESS) {
837 100 : p2p->pending_action_state = P2P_PENDING_GO_NEG_RESPONSE;
838 100 : dev->flags |= P2P_DEV_WAIT_GO_NEG_CONFIRM;
839 100 : if (os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) < 0) {
840 : /*
841 : * Peer has smaller address, so the GO Negotiation
842 : * Response from us is expected to complete
843 : * negotiation. Ignore a GO Negotiation Response from
844 : * the peer if it happens to be received after this
845 : * point due to a race condition in GO Negotiation
846 : * Request transmission and processing.
847 : */
848 77 : dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
849 : }
850 : } else
851 55 : p2p->pending_action_state =
852 : P2P_PENDING_GO_NEG_RESPONSE_FAILURE;
853 310 : if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
854 155 : p2p->cfg->dev_addr,
855 155 : wpabuf_head(resp), wpabuf_len(resp), 500) < 0) {
856 0 : p2p_dbg(p2p, "Failed to send Action frame");
857 : }
858 :
859 155 : wpabuf_free(resp);
860 : }
861 :
862 :
863 109 : static struct wpabuf * p2p_build_go_neg_conf(struct p2p_data *p2p,
864 : struct p2p_device *peer,
865 : u8 dialog_token, u8 status,
866 : const u8 *resp_chan, int go)
867 : {
868 : struct wpabuf *buf;
869 : u8 *len;
870 : struct p2p_channels res;
871 : u8 group_capab;
872 109 : size_t extra = 0;
873 :
874 109 : p2p_dbg(p2p, "Building GO Negotiation Confirm");
875 :
876 : #ifdef CONFIG_WIFI_DISPLAY
877 109 : if (p2p->wfd_ie_go_neg)
878 2 : extra = wpabuf_len(p2p->wfd_ie_go_neg);
879 : #endif /* CONFIG_WIFI_DISPLAY */
880 :
881 109 : if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF])
882 0 : extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF]);
883 :
884 109 : buf = wpabuf_alloc(1000 + extra);
885 109 : if (buf == NULL)
886 0 : return NULL;
887 :
888 109 : p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_CONF, dialog_token);
889 :
890 109 : len = p2p_buf_add_ie_hdr(buf);
891 109 : p2p_buf_add_status(buf, status);
892 109 : group_capab = 0;
893 109 : if (peer->go_state == LOCAL_GO) {
894 0 : if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
895 0 : group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
896 0 : if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
897 0 : group_capab |=
898 : P2P_GROUP_CAPAB_PERSISTENT_RECONN;
899 : }
900 0 : if (p2p->cross_connect)
901 0 : group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
902 0 : if (p2p->cfg->p2p_intra_bss)
903 0 : group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
904 : }
905 109 : p2p_buf_add_capability(buf, p2p->dev_capab &
906 : ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
907 : group_capab);
908 109 : if (go || resp_chan == NULL)
909 146 : p2p_buf_add_operating_channel(buf, p2p->cfg->country,
910 73 : p2p->op_reg_class,
911 73 : p2p->op_channel);
912 : else
913 72 : p2p_buf_add_operating_channel(buf, (const char *) resp_chan,
914 72 : resp_chan[3], resp_chan[4]);
915 109 : p2p_channels_intersect(&p2p->channels, &peer->channels, &res);
916 109 : p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
917 109 : if (go) {
918 72 : p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
919 : p2p->ssid_len);
920 : }
921 109 : p2p_buf_update_ie_hdr(buf, len);
922 :
923 : #ifdef CONFIG_WIFI_DISPLAY
924 109 : if (p2p->wfd_ie_go_neg)
925 2 : wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
926 : #endif /* CONFIG_WIFI_DISPLAY */
927 :
928 109 : if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF])
929 0 : wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF]);
930 :
931 109 : return buf;
932 : }
933 :
934 :
935 156 : void p2p_process_go_neg_resp(struct p2p_data *p2p, const u8 *sa,
936 : const u8 *data, size_t len, int rx_freq)
937 : {
938 : struct p2p_device *dev;
939 156 : int go = -1;
940 : struct p2p_message msg;
941 156 : u8 status = P2P_SC_SUCCESS;
942 : int freq;
943 :
944 936 : p2p_dbg(p2p, "Received GO Negotiation Response from " MACSTR
945 936 : " (freq=%d)", MAC2STR(sa), rx_freq);
946 156 : dev = p2p_get_device(p2p, sa);
947 311 : if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
948 155 : dev != p2p->go_neg_peer) {
949 6 : p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR,
950 6 : MAC2STR(sa));
951 1 : return;
952 : }
953 :
954 155 : if (p2p_parse(data, len, &msg))
955 1 : return;
956 :
957 154 : if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE)) {
958 1 : p2p_dbg(p2p, "Was not expecting GO Negotiation Response - ignore");
959 1 : p2p_parse_free(&msg);
960 1 : return;
961 : }
962 153 : dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
963 :
964 153 : if (msg.dialog_token != dev->dialog_token) {
965 2 : p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)",
966 2 : msg.dialog_token, dev->dialog_token);
967 1 : p2p_parse_free(&msg);
968 1 : return;
969 : }
970 :
971 152 : if (!msg.status) {
972 1 : p2p_dbg(p2p, "No Status attribute received");
973 1 : status = P2P_SC_FAIL_INVALID_PARAMS;
974 1 : goto fail;
975 : }
976 151 : if (*msg.status) {
977 43 : p2p_dbg(p2p, "GO Negotiation rejected: status %d", *msg.status);
978 43 : dev->go_neg_req_sent = 0;
979 43 : if (*msg.status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
980 32 : p2p_dbg(p2p, "Wait for the peer to become ready for GO Negotiation");
981 32 : dev->flags |= P2P_DEV_NOT_YET_READY;
982 32 : eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p,
983 : NULL);
984 32 : eloop_register_timeout(120, 0, p2p_go_neg_wait_timeout,
985 : p2p, NULL);
986 32 : if (p2p->state == P2P_CONNECT_LISTEN)
987 0 : p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
988 : else
989 32 : p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
990 32 : p2p_set_timeout(p2p, 0, 0);
991 : } else {
992 11 : p2p_dbg(p2p, "Stop GO Negotiation attempt");
993 11 : p2p_go_neg_failed(p2p, *msg.status);
994 : }
995 43 : p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
996 43 : p2p_parse_free(&msg);
997 43 : return;
998 : }
999 :
1000 108 : if (!msg.capability) {
1001 1 : p2p_dbg(p2p, "Mandatory Capability attribute missing from GO Negotiation Response");
1002 : #ifdef CONFIG_P2P_STRICT
1003 : status = P2P_SC_FAIL_INVALID_PARAMS;
1004 : goto fail;
1005 : #endif /* CONFIG_P2P_STRICT */
1006 : }
1007 :
1008 108 : if (!msg.p2p_device_info) {
1009 1 : p2p_dbg(p2p, "Mandatory P2P Device Info attribute missing from GO Negotiation Response");
1010 : #ifdef CONFIG_P2P_STRICT
1011 : status = P2P_SC_FAIL_INVALID_PARAMS;
1012 : goto fail;
1013 : #endif /* CONFIG_P2P_STRICT */
1014 : }
1015 :
1016 108 : if (!msg.intended_addr) {
1017 1 : p2p_dbg(p2p, "No Intended P2P Interface Address attribute received");
1018 1 : status = P2P_SC_FAIL_INVALID_PARAMS;
1019 1 : goto fail;
1020 : }
1021 :
1022 107 : if (!msg.go_intent) {
1023 1 : p2p_dbg(p2p, "No GO Intent attribute received");
1024 1 : status = P2P_SC_FAIL_INVALID_PARAMS;
1025 1 : goto fail;
1026 : }
1027 106 : if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
1028 1 : p2p_dbg(p2p, "Invalid GO Intent value (%u) received",
1029 1 : *msg.go_intent >> 1);
1030 1 : status = P2P_SC_FAIL_INVALID_PARAMS;
1031 1 : goto fail;
1032 : }
1033 :
1034 105 : go = p2p_go_det(p2p->go_intent, *msg.go_intent);
1035 105 : if (go < 0) {
1036 1 : p2p_dbg(p2p, "Incompatible GO Intent");
1037 1 : status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
1038 1 : goto fail;
1039 : }
1040 :
1041 104 : if (!go && msg.group_id) {
1042 : /* Store SSID for Provisioning step */
1043 36 : p2p->ssid_len = msg.group_id_len - ETH_ALEN;
1044 36 : os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
1045 68 : } else if (!go) {
1046 1 : p2p_dbg(p2p, "Mandatory P2P Group ID attribute missing from GO Negotiation Response");
1047 1 : p2p->ssid_len = 0;
1048 1 : status = P2P_SC_FAIL_INVALID_PARAMS;
1049 1 : goto fail;
1050 : }
1051 :
1052 103 : if (!msg.config_timeout) {
1053 1 : p2p_dbg(p2p, "Mandatory Configuration Timeout attribute missing from GO Negotiation Response");
1054 : #ifdef CONFIG_P2P_STRICT
1055 : status = P2P_SC_FAIL_INVALID_PARAMS;
1056 : goto fail;
1057 : #endif /* CONFIG_P2P_STRICT */
1058 : } else {
1059 102 : dev->go_timeout = msg.config_timeout[0];
1060 102 : dev->client_timeout = msg.config_timeout[1];
1061 : }
1062 :
1063 103 : if (!msg.operating_channel && !go) {
1064 : /*
1065 : * Note: P2P Client may omit Operating Channel attribute to
1066 : * indicate it does not have a preference.
1067 : */
1068 1 : p2p_dbg(p2p, "No Operating Channel attribute received");
1069 1 : status = P2P_SC_FAIL_INVALID_PARAMS;
1070 1 : goto fail;
1071 : }
1072 102 : if (!msg.channel_list) {
1073 1 : p2p_dbg(p2p, "No Channel List attribute received");
1074 1 : status = P2P_SC_FAIL_INVALID_PARAMS;
1075 1 : goto fail;
1076 : }
1077 :
1078 101 : if (p2p_peer_channels(p2p, dev, msg.channel_list,
1079 101 : msg.channel_list_len) < 0) {
1080 1 : p2p_dbg(p2p, "No common channels found");
1081 1 : status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
1082 1 : goto fail;
1083 : }
1084 :
1085 100 : if (msg.operating_channel) {
1086 33 : dev->oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
1087 33 : msg.operating_channel[4]);
1088 33 : p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
1089 : dev->oper_freq);
1090 : } else
1091 67 : dev->oper_freq = 0;
1092 :
1093 100 : switch (msg.dev_password_id) {
1094 : case DEV_PW_REGISTRAR_SPECIFIED:
1095 61 : p2p_dbg(p2p, "PIN from peer Display");
1096 61 : if (dev->wps_method != WPS_PIN_KEYPAD) {
1097 0 : p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1098 : p2p_wps_method_str(dev->wps_method));
1099 0 : status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1100 0 : goto fail;
1101 : }
1102 61 : break;
1103 : case DEV_PW_USER_SPECIFIED:
1104 9 : p2p_dbg(p2p, "Peer entered PIN on Keypad");
1105 9 : if (dev->wps_method != WPS_PIN_DISPLAY) {
1106 0 : p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1107 : p2p_wps_method_str(dev->wps_method));
1108 0 : status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1109 0 : goto fail;
1110 : }
1111 9 : break;
1112 : case DEV_PW_PUSHBUTTON:
1113 20 : p2p_dbg(p2p, "Peer using pushbutton");
1114 20 : if (dev->wps_method != WPS_PBC) {
1115 0 : p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1116 : p2p_wps_method_str(dev->wps_method));
1117 0 : status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1118 0 : goto fail;
1119 : }
1120 20 : break;
1121 : case DEV_PW_P2PS_DEFAULT:
1122 2 : p2p_dbg(p2p, "P2P: Peer using P2PS default pin");
1123 2 : if (dev->wps_method != WPS_P2PS) {
1124 0 : p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1125 : p2p_wps_method_str(dev->wps_method));
1126 0 : status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1127 0 : goto fail;
1128 : }
1129 2 : break;
1130 : default:
1131 16 : if (msg.dev_password_id &&
1132 8 : msg.dev_password_id == dev->oob_pw_id) {
1133 8 : p2p_dbg(p2p, "Peer using NFC");
1134 8 : if (dev->wps_method != WPS_NFC) {
1135 0 : p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1136 : p2p_wps_method_str(dev->wps_method));
1137 0 : status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1138 0 : goto fail;
1139 : }
1140 8 : break;
1141 : }
1142 0 : p2p_dbg(p2p, "Unsupported Device Password ID %d",
1143 0 : msg.dev_password_id);
1144 0 : status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1145 0 : goto fail;
1146 : }
1147 :
1148 100 : if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
1149 0 : goto fail;
1150 :
1151 100 : p2p_set_state(p2p, P2P_GO_NEG);
1152 100 : p2p_clear_timeout(p2p);
1153 :
1154 100 : p2p_dbg(p2p, "GO Negotiation with " MACSTR, MAC2STR(sa));
1155 100 : os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
1156 :
1157 : fail:
1158 : /* Store GO Negotiation Confirmation to allow retransmission */
1159 109 : wpabuf_free(dev->go_neg_conf);
1160 109 : dev->go_neg_conf = p2p_build_go_neg_conf(p2p, dev, msg.dialog_token,
1161 : status, msg.operating_channel,
1162 : go);
1163 109 : p2p_parse_free(&msg);
1164 109 : if (dev->go_neg_conf == NULL)
1165 0 : return;
1166 109 : p2p_dbg(p2p, "Sending GO Negotiation Confirm");
1167 109 : if (status == P2P_SC_SUCCESS) {
1168 100 : p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
1169 100 : dev->go_state = go ? LOCAL_GO : REMOTE_GO;
1170 : } else
1171 9 : p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1172 109 : if (rx_freq > 0)
1173 109 : freq = rx_freq;
1174 : else
1175 0 : freq = dev->listen_freq;
1176 :
1177 109 : dev->go_neg_conf_freq = freq;
1178 109 : dev->go_neg_conf_sent = 0;
1179 :
1180 218 : if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr, sa,
1181 109 : wpabuf_head(dev->go_neg_conf),
1182 109 : wpabuf_len(dev->go_neg_conf), 200) < 0) {
1183 0 : p2p_dbg(p2p, "Failed to send Action frame");
1184 0 : p2p_go_neg_failed(p2p, -1);
1185 0 : p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
1186 : } else
1187 109 : dev->go_neg_conf_sent++;
1188 109 : if (status != P2P_SC_SUCCESS) {
1189 9 : p2p_dbg(p2p, "GO Negotiation failed");
1190 9 : p2p_go_neg_failed(p2p, status);
1191 : }
1192 : }
1193 :
1194 :
1195 100 : void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa,
1196 : const u8 *data, size_t len)
1197 : {
1198 : struct p2p_device *dev;
1199 : struct p2p_message msg;
1200 :
1201 600 : p2p_dbg(p2p, "Received GO Negotiation Confirm from " MACSTR,
1202 600 : MAC2STR(sa));
1203 100 : dev = p2p_get_device(p2p, sa);
1204 200 : if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
1205 100 : dev != p2p->go_neg_peer) {
1206 0 : p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR,
1207 0 : MAC2STR(sa));
1208 0 : return;
1209 : }
1210 :
1211 100 : if (p2p->pending_action_state == P2P_PENDING_GO_NEG_RESPONSE) {
1212 0 : p2p_dbg(p2p, "Stopped waiting for TX status on GO Negotiation Response since we already received Confirmation");
1213 0 : p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1214 : }
1215 :
1216 100 : if (p2p_parse(data, len, &msg))
1217 0 : return;
1218 :
1219 100 : if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
1220 0 : p2p_dbg(p2p, "Was not expecting GO Negotiation Confirm - ignore");
1221 0 : p2p_parse_free(&msg);
1222 0 : return;
1223 : }
1224 100 : dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1225 100 : p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
1226 :
1227 100 : if (msg.dialog_token != dev->dialog_token) {
1228 0 : p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)",
1229 0 : msg.dialog_token, dev->dialog_token);
1230 0 : p2p_parse_free(&msg);
1231 0 : return;
1232 : }
1233 :
1234 100 : if (!msg.status) {
1235 0 : p2p_dbg(p2p, "No Status attribute received");
1236 0 : p2p_parse_free(&msg);
1237 0 : return;
1238 : }
1239 100 : if (*msg.status) {
1240 0 : p2p_dbg(p2p, "GO Negotiation rejected: status %d", *msg.status);
1241 0 : p2p_go_neg_failed(p2p, *msg.status);
1242 0 : p2p_parse_free(&msg);
1243 0 : return;
1244 : }
1245 :
1246 100 : if (dev->go_state == REMOTE_GO && msg.group_id) {
1247 : /* Store SSID for Provisioning step */
1248 67 : p2p->ssid_len = msg.group_id_len - ETH_ALEN;
1249 67 : os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
1250 33 : } else if (dev->go_state == REMOTE_GO) {
1251 0 : p2p_dbg(p2p, "Mandatory P2P Group ID attribute missing from GO Negotiation Confirmation");
1252 0 : p2p->ssid_len = 0;
1253 0 : p2p_go_neg_failed(p2p, P2P_SC_FAIL_INVALID_PARAMS);
1254 0 : p2p_parse_free(&msg);
1255 0 : return;
1256 : }
1257 :
1258 100 : if (!msg.operating_channel) {
1259 0 : p2p_dbg(p2p, "Mandatory Operating Channel attribute missing from GO Negotiation Confirmation");
1260 : #ifdef CONFIG_P2P_STRICT
1261 : p2p_parse_free(&msg);
1262 : return;
1263 : #endif /* CONFIG_P2P_STRICT */
1264 100 : } else if (dev->go_state == REMOTE_GO) {
1265 67 : int oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
1266 67 : msg.operating_channel[4]);
1267 67 : if (oper_freq != dev->oper_freq) {
1268 2 : p2p_dbg(p2p, "Updated peer (GO) operating channel preference from %d MHz to %d MHz",
1269 : dev->oper_freq, oper_freq);
1270 2 : dev->oper_freq = oper_freq;
1271 : }
1272 : }
1273 :
1274 100 : if (!msg.channel_list) {
1275 0 : p2p_dbg(p2p, "Mandatory Operating Channel attribute missing from GO Negotiation Confirmation");
1276 : #ifdef CONFIG_P2P_STRICT
1277 : p2p_parse_free(&msg);
1278 : return;
1279 : #endif /* CONFIG_P2P_STRICT */
1280 : }
1281 :
1282 100 : p2p_parse_free(&msg);
1283 :
1284 100 : if (dev->go_state == UNKNOWN_GO) {
1285 : /*
1286 : * This should not happen since GO negotiation has already
1287 : * been completed.
1288 : */
1289 0 : p2p_dbg(p2p, "Unexpected GO Neg state - do not know which end becomes GO");
1290 0 : return;
1291 : }
1292 :
1293 : /*
1294 : * The peer could have missed our ctrl::ack frame for GO Negotiation
1295 : * Confirm and continue retransmitting the frame. To reduce the
1296 : * likelihood of the peer not getting successful TX status for the
1297 : * GO Negotiation Confirm frame, wait a short time here before starting
1298 : * the group so that we will remain on the current channel to
1299 : * acknowledge any possible retransmission from the peer.
1300 : */
1301 100 : p2p_dbg(p2p, "20 ms wait on current channel before starting group");
1302 100 : os_sleep(0, 20000);
1303 :
1304 100 : p2p_go_complete(p2p, dev);
1305 : }
|