2 * Wi-Fi Direct - P2P provision discovery
3 * Copyright (c) 2009-2010, Atheros Communications
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "common/ieee802_11_defs.h"
13 #include "wps/wps_defs.h"
19 * Number of retries to attempt for provision discovery requests
20 * in case the peer is not listening.
22 #define MAX_PROV_DISC_REQ_RETRIES 10
25 static void p2p_build_wps_ie_config_methods(struct wpabuf *buf,
29 wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
30 len = wpabuf_put(buf, 1);
31 wpabuf_put_be32(buf, WPS_DEV_OUI_WFA);
34 wpabuf_put_be16(buf, ATTR_CONFIG_METHODS);
35 wpabuf_put_be16(buf, 2);
36 wpabuf_put_be16(buf, config_methods);
38 p2p_buf_update_ie_hdr(buf, len);
42 static struct wpabuf * p2p_build_prov_disc_req(struct p2p_data *p2p,
45 struct p2p_device *go)
51 #ifdef CONFIG_WIFI_DISPLAY
52 if (p2p->wfd_ie_prov_disc_req)
53 extra = wpabuf_len(p2p->wfd_ie_prov_disc_req);
54 #endif /* CONFIG_WIFI_DISPLAY */
56 buf = wpabuf_alloc(1000 + extra);
60 p2p_buf_add_public_action_hdr(buf, P2P_PROV_DISC_REQ, dialog_token);
62 len = p2p_buf_add_ie_hdr(buf);
63 p2p_buf_add_capability(buf, p2p->dev_capab &
64 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
65 p2p_buf_add_device_info(buf, p2p, NULL);
67 p2p_buf_add_group_id(buf, go->info.p2p_device_addr,
68 go->oper_ssid, go->oper_ssid_len);
70 p2p_buf_update_ie_hdr(buf, len);
72 /* WPS IE with Config Methods attribute */
73 p2p_build_wps_ie_config_methods(buf, config_methods);
75 #ifdef CONFIG_WIFI_DISPLAY
76 if (p2p->wfd_ie_prov_disc_req)
77 wpabuf_put_buf(buf, p2p->wfd_ie_prov_disc_req);
78 #endif /* CONFIG_WIFI_DISPLAY */
84 static struct wpabuf * p2p_build_prov_disc_resp(struct p2p_data *p2p,
93 #ifdef CONFIG_WIFI_DISPLAY
94 struct wpabuf *wfd_ie = p2p->wfd_ie_prov_disc_resp;
95 if (wfd_ie && group_id) {
97 for (i = 0; i < p2p->num_groups; i++) {
98 struct p2p_group *g = p2p->groups[i];
100 if (!p2p_group_is_group_id_match(g, group_id,
103 ie = p2p_group_get_wfd_ie(g);
111 extra = wpabuf_len(wfd_ie);
112 #endif /* CONFIG_WIFI_DISPLAY */
114 buf = wpabuf_alloc(100 + extra);
118 p2p_buf_add_public_action_hdr(buf, P2P_PROV_DISC_RESP, dialog_token);
120 /* WPS IE with Config Methods attribute */
121 p2p_build_wps_ie_config_methods(buf, config_methods);
123 #ifdef CONFIG_WIFI_DISPLAY
125 wpabuf_put_buf(buf, wfd_ie);
126 #endif /* CONFIG_WIFI_DISPLAY */
132 void p2p_process_prov_disc_req(struct p2p_data *p2p, const u8 *sa,
133 const u8 *data, size_t len, int rx_freq)
135 struct p2p_message msg;
136 struct p2p_device *dev;
141 if (p2p_parse(data, len, &msg))
144 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
145 "P2P: Received Provision Discovery Request from " MACSTR
146 " with config methods 0x%x (freq=%d)",
147 MAC2STR(sa), msg.wps_config_methods, rx_freq);
149 dev = p2p_get_device(p2p, sa);
150 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
151 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
152 "P2P: Provision Discovery Request from "
153 "unknown peer " MACSTR, MAC2STR(sa));
154 if (p2p_add_device(p2p, sa, rx_freq, 0, data + 1, len - 1, 0))
156 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
157 "P2P: Provision Discovery Request add device "
158 "failed " MACSTR, MAC2STR(sa));
160 } else if (msg.wfd_subelems) {
161 wpabuf_free(dev->info.wfd_subelems);
162 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
165 if (!(msg.wps_config_methods &
166 (WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD |
167 WPS_CONFIG_PUSHBUTTON))) {
168 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unsupported "
169 "Config Methods in Provision Discovery Request");
175 for (i = 0; i < p2p->num_groups; i++) {
176 if (p2p_group_is_group_id_match(p2p->groups[i],
181 if (i == p2p->num_groups) {
182 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: PD "
183 "request for unknown P2P Group ID - reject");
189 dev->flags &= ~(P2P_DEV_PD_PEER_DISPLAY |
190 P2P_DEV_PD_PEER_KEYPAD);
191 if (msg.wps_config_methods & WPS_CONFIG_DISPLAY) {
192 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
193 " requested us to show a PIN on display", MAC2STR(sa));
195 dev->flags |= P2P_DEV_PD_PEER_KEYPAD;
196 } else if (msg.wps_config_methods & WPS_CONFIG_KEYPAD) {
197 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
198 " requested us to write its PIN using keypad",
201 dev->flags |= P2P_DEV_PD_PEER_DISPLAY;
207 resp = p2p_build_prov_disc_resp(p2p, msg.dialog_token,
208 reject ? 0 : msg.wps_config_methods,
209 msg.group_id, msg.group_id_len);
211 p2p_parse_free(&msg);
214 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
215 "P2P: Sending Provision Discovery Response");
219 freq = p2p_channel_to_freq(p2p->cfg->country,
223 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
224 "P2P: Unknown regulatory class/channel");
226 p2p_parse_free(&msg);
229 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
230 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
232 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
233 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
234 "P2P: Failed to send Action frame");
239 if (!reject && p2p->cfg->prov_disc_req) {
240 const u8 *dev_addr = sa;
241 if (msg.p2p_device_addr)
242 dev_addr = msg.p2p_device_addr;
243 p2p->cfg->prov_disc_req(p2p->cfg->cb_ctx, sa,
244 msg.wps_config_methods,
245 dev_addr, msg.pri_dev_type,
246 msg.device_name, msg.config_methods,
247 msg.capability ? msg.capability[0] : 0,
248 msg.capability ? msg.capability[1] :
250 msg.group_id, msg.group_id_len);
252 p2p_parse_free(&msg);
256 void p2p_process_prov_disc_resp(struct p2p_data *p2p, const u8 *sa,
257 const u8 *data, size_t len)
259 struct p2p_message msg;
260 struct p2p_device *dev;
261 u16 report_config_methods = 0;
264 if (p2p_parse(data, len, &msg))
267 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
268 "P2P: Received Provision Discovery Response from " MACSTR
269 " with config methods 0x%x",
270 MAC2STR(sa), msg.wps_config_methods);
272 dev = p2p_get_device(p2p, sa);
273 if (dev == NULL || !dev->req_config_methods) {
274 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
275 "P2P: Ignore Provision Discovery Response from "
276 MACSTR " with no pending request", MAC2STR(sa));
277 p2p_parse_free(&msg);
281 if (p2p->pending_action_state == P2P_PENDING_PD) {
282 os_memset(p2p->pending_pd_devaddr, 0, ETH_ALEN);
283 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
286 if (dev->dialog_token != msg.dialog_token) {
287 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
288 "P2P: Ignore Provision Discovery Response with "
289 "unexpected Dialog Token %u (expected %u)",
290 msg.dialog_token, dev->dialog_token);
291 p2p_parse_free(&msg);
296 * If the response is from the peer to whom a user initiated request
297 * was sent earlier, we reset that state info here.
299 if (p2p->user_initiated_pd &&
300 os_memcmp(p2p->pending_pd_devaddr, sa, ETH_ALEN) == 0)
301 p2p_reset_pending_pd(p2p);
303 if (msg.wps_config_methods != dev->req_config_methods) {
304 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer rejected "
305 "our Provision Discovery Request");
306 if (p2p->cfg->prov_disc_fail)
307 p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx, sa,
308 P2P_PROV_DISC_REJECTED);
309 p2p_parse_free(&msg);
313 report_config_methods = dev->req_config_methods;
314 dev->flags &= ~(P2P_DEV_PD_PEER_DISPLAY |
315 P2P_DEV_PD_PEER_KEYPAD);
316 if (dev->req_config_methods & WPS_CONFIG_DISPLAY) {
317 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
318 " accepted to show a PIN on display", MAC2STR(sa));
319 dev->flags |= P2P_DEV_PD_PEER_DISPLAY;
320 } else if (msg.wps_config_methods & WPS_CONFIG_KEYPAD) {
321 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
322 " accepted to write our PIN using keypad",
324 dev->flags |= P2P_DEV_PD_PEER_KEYPAD;
327 /* Store the provisioning info */
328 dev->wps_prov_info = msg.wps_config_methods;
330 p2p_parse_free(&msg);
334 dev->req_config_methods = 0;
335 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
336 if (dev->flags & P2P_DEV_PD_BEFORE_GO_NEG) {
337 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
338 "P2P: Start GO Neg after the PD-before-GO-Neg "
339 "workaround with " MACSTR,
340 MAC2STR(dev->info.p2p_device_addr));
341 dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
342 p2p_connect_send(p2p, dev);
345 if (success && p2p->cfg->prov_disc_resp)
346 p2p->cfg->prov_disc_resp(p2p->cfg->cb_ctx, sa,
347 report_config_methods);
351 int p2p_send_prov_disc_req(struct p2p_data *p2p, struct p2p_device *dev,
352 int join, int force_freq)
360 freq = dev->listen_freq > 0 ? dev->listen_freq :
363 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
364 "P2P: No Listen/Operating frequency known for the "
365 "peer " MACSTR " to send Provision Discovery Request",
366 MAC2STR(dev->info.p2p_device_addr));
370 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
371 if (!(dev->info.dev_capab &
372 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
373 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
374 "P2P: Cannot use PD with P2P Device " MACSTR
375 " that is in a group and is not discoverable",
376 MAC2STR(dev->info.p2p_device_addr));
379 /* TODO: use device discoverability request through GO */
383 if (dev->dialog_token == 0)
384 dev->dialog_token = 1;
385 req = p2p_build_prov_disc_req(p2p, dev->dialog_token,
386 dev->req_config_methods,
391 if (p2p->state != P2P_IDLE)
392 p2p_stop_listen_for_freq(p2p, freq);
393 p2p->pending_action_state = P2P_PENDING_PD;
394 if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
395 p2p->cfg->dev_addr, dev->info.p2p_device_addr,
396 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
397 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
398 "P2P: Failed to send Action frame");
403 os_memcpy(p2p->pending_pd_devaddr, dev->info.p2p_device_addr, ETH_ALEN);
410 int p2p_prov_disc_req(struct p2p_data *p2p, const u8 *peer_addr,
411 u16 config_methods, int join, int force_freq)
413 struct p2p_device *dev;
415 dev = p2p_get_device(p2p, peer_addr);
417 dev = p2p_get_device_interface(p2p, peer_addr);
418 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
419 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Provision "
420 "Discovery Request destination " MACSTR
421 " not yet known", MAC2STR(peer_addr));
425 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Provision Discovery "
426 "Request with " MACSTR " (config methods 0x%x)",
427 MAC2STR(peer_addr), config_methods);
428 if (config_methods == 0)
431 /* Reset provisioning info */
432 dev->wps_prov_info = 0;
434 dev->req_config_methods = config_methods;
436 dev->flags |= P2P_DEV_PD_FOR_JOIN;
438 dev->flags &= ~P2P_DEV_PD_FOR_JOIN;
440 if (p2p->state != P2P_IDLE && p2p->state != P2P_SEARCH &&
441 p2p->state != P2P_LISTEN_ONLY) {
442 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Busy with other "
443 "operations; postpone Provision Discovery Request "
444 "with " MACSTR " (config methods 0x%x)",
445 MAC2STR(peer_addr), config_methods);
450 * We use the join param as a cue to differentiate between user
451 * initiated PD request and one issued during finds (internal).
453 p2p->user_initiated_pd = !join;
455 if (p2p->user_initiated_pd)
456 p2p->pd_retries = MAX_PROV_DISC_REQ_RETRIES;
458 return p2p_send_prov_disc_req(p2p, dev, join, force_freq);
462 void p2p_reset_pending_pd(struct p2p_data *p2p)
464 struct p2p_device *dev;
466 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
467 if (os_memcmp(p2p->pending_pd_devaddr,
468 dev->info.p2p_device_addr, ETH_ALEN))
470 if (!dev->req_config_methods)
472 if (dev->flags & P2P_DEV_PD_FOR_JOIN)
474 /* Reset the config methods of the device */
475 dev->req_config_methods = 0;
478 p2p->user_initiated_pd = 0;
479 os_memset(p2p->pending_pd_devaddr, 0, ETH_ALEN);