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