2 * Wi-Fi Direct - P2P provision discovery
3 * Copyright (c) 2009-2010, Atheros Communications
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
18 #include "common/ieee802_11_defs.h"
19 #include "wps/wps_defs.h"
24 static void p2p_build_wps_ie_config_methods(struct wpabuf *buf,
28 wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
29 len = wpabuf_put(buf, 1);
30 wpabuf_put_be32(buf, WPS_DEV_OUI_WFA);
33 wpabuf_put_be16(buf, ATTR_CONFIG_METHODS);
34 wpabuf_put_be16(buf, 2);
35 wpabuf_put_be16(buf, config_methods);
37 p2p_buf_update_ie_hdr(buf, len);
41 static struct wpabuf * p2p_build_prov_disc_req(struct p2p_data *p2p,
44 struct p2p_device *go)
49 buf = wpabuf_alloc(1000);
53 p2p_buf_add_public_action_hdr(buf, P2P_PROV_DISC_REQ, dialog_token);
55 len = p2p_buf_add_ie_hdr(buf);
56 p2p_buf_add_capability(buf, p2p->dev_capab, 0);
57 p2p_buf_add_device_info(buf, p2p, NULL);
59 p2p_buf_add_group_id(buf, go->p2p_device_addr, go->oper_ssid,
62 p2p_buf_update_ie_hdr(buf, len);
64 /* WPS IE with Config Methods attribute */
65 p2p_build_wps_ie_config_methods(buf, config_methods);
71 static struct wpabuf * p2p_build_prov_disc_resp(struct p2p_data *p2p,
77 buf = wpabuf_alloc(100);
81 p2p_buf_add_public_action_hdr(buf, P2P_PROV_DISC_RESP, dialog_token);
83 /* WPS IE with Config Methods attribute */
84 p2p_build_wps_ie_config_methods(buf, config_methods);
90 void p2p_process_prov_disc_req(struct p2p_data *p2p, const u8 *sa,
91 const u8 *data, size_t len, int rx_freq)
93 struct p2p_message msg;
94 struct p2p_device *dev;
99 if (p2p_parse(data, len, &msg))
102 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
103 "P2P: Received Provision Discovery Request from " MACSTR
104 " with config methods 0x%x (freq=%d)",
105 MAC2STR(sa), msg.wps_config_methods, rx_freq);
107 dev = p2p_get_device(p2p, sa);
109 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
110 "P2P: Provision Discovery Request from "
111 "unknown peer " MACSTR, MAC2STR(sa));
114 if (!(msg.wps_config_methods &
115 (WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD |
116 WPS_CONFIG_PUSHBUTTON))) {
117 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unsupported "
118 "Config Methods in Provision Discovery Request");
123 dev->flags &= ~(P2P_DEV_PD_PEER_DISPLAY |
124 P2P_DEV_PD_PEER_KEYPAD);
125 if (msg.wps_config_methods & WPS_CONFIG_DISPLAY) {
126 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
127 " requested us to show a PIN on display", MAC2STR(sa));
129 dev->flags |= P2P_DEV_PD_PEER_KEYPAD;
130 } else if (msg.wps_config_methods & WPS_CONFIG_KEYPAD) {
131 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
132 " requested us to write its PIN using keypad",
135 dev->flags |= P2P_DEV_PD_PEER_DISPLAY;
141 resp = p2p_build_prov_disc_resp(p2p, msg.dialog_token,
142 reject ? 0 : msg.wps_config_methods);
144 p2p_parse_free(&msg);
147 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
148 "P2P: Sending Provision Discovery Response");
152 freq = p2p_channel_to_freq(p2p->cfg->country,
156 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
157 "P2P: Unknown regulatory class/channel");
159 p2p_parse_free(&msg);
162 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
163 if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, sa,
164 p2p->cfg->dev_addr, p2p->cfg->dev_addr,
165 wpabuf_head(resp), wpabuf_len(resp), 200) <
167 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
168 "P2P: Failed to send Action frame");
173 if (!reject && p2p->cfg->prov_disc_req) {
174 const u8 *dev_addr = sa;
175 if (msg.p2p_device_addr)
176 dev_addr = msg.p2p_device_addr;
177 p2p->cfg->prov_disc_req(p2p->cfg->cb_ctx, sa,
178 msg.wps_config_methods,
179 dev_addr, msg.pri_dev_type,
180 msg.device_name, msg.config_methods,
181 msg.capability ? msg.capability[0] : 0,
182 msg.capability ? msg.capability[1] :
186 p2p_parse_free(&msg);
190 void p2p_process_prov_disc_resp(struct p2p_data *p2p, const u8 *sa,
191 const u8 *data, size_t len)
193 struct p2p_message msg;
194 struct p2p_device *dev;
195 u16 report_config_methods = 0;
197 if (p2p_parse(data, len, &msg))
200 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
201 "P2P: Received Provisioning Discovery Response from " MACSTR
202 " with config methods 0x%x",
203 MAC2STR(sa), msg.wps_config_methods);
205 dev = p2p_get_device(p2p, sa);
206 if (dev == NULL || !dev->req_config_methods) {
207 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
208 "P2P: Ignore Provisioning Discovery Response from "
209 MACSTR " with no pending request", MAC2STR(sa));
210 p2p_parse_free(&msg);
214 if (dev->dialog_token != msg.dialog_token) {
215 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
216 "P2P: Ignore Provisioning Discovery Response with "
217 "unexpected Dialog Token %u (expected %u)",
218 msg.dialog_token, dev->dialog_token);
219 p2p_parse_free(&msg);
223 if (msg.wps_config_methods != dev->req_config_methods) {
224 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer rejected "
225 "our Provisioning Discovery Request");
226 p2p_parse_free(&msg);
230 report_config_methods = dev->req_config_methods;
231 dev->flags &= ~(P2P_DEV_PD_PEER_DISPLAY |
232 P2P_DEV_PD_PEER_KEYPAD);
233 if (dev->req_config_methods & WPS_CONFIG_DISPLAY) {
234 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
235 " accepted to show a PIN on display", MAC2STR(sa));
236 dev->flags |= P2P_DEV_PD_PEER_DISPLAY;
237 } else if (msg.wps_config_methods & WPS_CONFIG_KEYPAD) {
238 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
239 " accepted to write our PIN using keypad",
241 dev->flags |= P2P_DEV_PD_PEER_KEYPAD;
243 p2p_parse_free(&msg);
246 dev->req_config_methods = 0;
247 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
248 if (p2p->cfg->prov_disc_resp)
249 p2p->cfg->prov_disc_resp(p2p->cfg->cb_ctx, sa,
250 report_config_methods);
254 int p2p_send_prov_disc_req(struct p2p_data *p2p, struct p2p_device *dev,
260 freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
262 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
263 "P2P: No Listen/Operating frequency known for the "
264 "peer " MACSTR " to send Provision Discovery Request",
265 MAC2STR(dev->p2p_device_addr));
269 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
270 if (!(dev->dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
271 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
272 "P2P: Cannot use PD with P2P Device " MACSTR
273 " that is in a group and is not discoverable",
274 MAC2STR(dev->p2p_device_addr));
277 /* TODO: use device discoverability request through GO */
281 if (dev->dialog_token == 0)
282 dev->dialog_token = 1;
283 req = p2p_build_prov_disc_req(p2p, dev->dialog_token,
284 dev->req_config_methods,
289 p2p->pending_action_state = P2P_PENDING_PD;
290 if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq,
291 dev->p2p_device_addr, p2p->cfg->dev_addr,
292 dev->p2p_device_addr,
293 wpabuf_head(req), wpabuf_len(req), 200) < 0)
295 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
296 "P2P: Failed to send Action frame");
306 int p2p_prov_disc_req(struct p2p_data *p2p, const u8 *peer_addr,
307 u16 config_methods, int join)
309 struct p2p_device *dev;
311 dev = p2p_get_device(p2p, peer_addr);
313 dev = p2p_get_device_interface(p2p, peer_addr);
314 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
315 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Provision "
316 "Discovery Request destination " MACSTR
317 " not yet known", MAC2STR(peer_addr));
321 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Provision Discovery "
322 "Request with " MACSTR " (config methods 0x%x)",
323 MAC2STR(peer_addr), config_methods);
324 if (config_methods == 0)
327 dev->req_config_methods = config_methods;
329 if (p2p->go_neg_peer ||
330 (p2p->state != P2P_IDLE && p2p->state != P2P_SEARCH &&
331 p2p->state != P2P_LISTEN_ONLY)) {
332 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Busy with other "
333 "operations; postpone Provision Discovery Request "
334 "with " MACSTR " (config methods 0x%x)",
335 MAC2STR(peer_addr), config_methods);
339 return p2p_send_prov_disc_req(p2p, dev, join);