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_i.h"
17 void p2p_buf_add_action_hdr(struct wpabuf *buf, u8 subtype, u8 dialog_token)
19 wpabuf_put_u8(buf, WLAN_ACTION_VENDOR_SPECIFIC);
20 wpabuf_put_be24(buf, OUI_WFA);
21 wpabuf_put_u8(buf, P2P_OUI_TYPE);
23 wpabuf_put_u8(buf, subtype); /* OUI Subtype */
24 wpabuf_put_u8(buf, dialog_token);
25 wpa_printf(MSG_DEBUG, "P2P: * Dialog Token: %d", dialog_token);
29 void p2p_buf_add_public_action_hdr(struct wpabuf *buf, u8 subtype,
32 wpabuf_put_u8(buf, WLAN_ACTION_PUBLIC);
33 wpabuf_put_u8(buf, WLAN_PA_VENDOR_SPECIFIC);
34 wpabuf_put_be24(buf, OUI_WFA);
35 wpabuf_put_u8(buf, P2P_OUI_TYPE);
37 wpabuf_put_u8(buf, subtype); /* OUI Subtype */
38 wpabuf_put_u8(buf, dialog_token);
39 wpa_printf(MSG_DEBUG, "P2P: * Dialog Token: %d", dialog_token);
43 u8 * p2p_buf_add_ie_hdr(struct wpabuf *buf)
48 wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
49 len = wpabuf_put(buf, 1); /* IE length to be filled */
50 wpabuf_put_be24(buf, OUI_WFA);
51 wpabuf_put_u8(buf, P2P_OUI_TYPE);
52 wpa_printf(MSG_DEBUG, "P2P: * P2P IE header");
57 void p2p_buf_update_ie_hdr(struct wpabuf *buf, u8 *len)
59 /* Update P2P IE Length */
60 *len = (u8 *) wpabuf_put(buf, 0) - len - 1;
64 void p2p_buf_add_capability(struct wpabuf *buf, u8 dev_capab, u8 group_capab)
67 wpabuf_put_u8(buf, P2P_ATTR_CAPABILITY);
68 wpabuf_put_le16(buf, 2);
69 wpabuf_put_u8(buf, dev_capab); /* Device Capabilities */
70 wpabuf_put_u8(buf, group_capab); /* Group Capabilities */
71 wpa_printf(MSG_DEBUG, "P2P: * Capability dev=%02x group=%02x",
72 dev_capab, group_capab);
76 void p2p_buf_add_go_intent(struct wpabuf *buf, u8 go_intent)
78 /* Group Owner Intent */
79 wpabuf_put_u8(buf, P2P_ATTR_GROUP_OWNER_INTENT);
80 wpabuf_put_le16(buf, 1);
81 wpabuf_put_u8(buf, go_intent);
82 wpa_printf(MSG_DEBUG, "P2P: * GO Intent: Intent %u Tie breaker %u",
83 go_intent >> 1, go_intent & 0x01);
87 void p2p_buf_add_listen_channel(struct wpabuf *buf, const char *country,
88 u8 reg_class, u8 channel)
91 wpabuf_put_u8(buf, P2P_ATTR_LISTEN_CHANNEL);
92 wpabuf_put_le16(buf, 5);
93 wpabuf_put_data(buf, country, 3);
94 wpabuf_put_u8(buf, reg_class); /* Regulatory Class */
95 wpabuf_put_u8(buf, channel); /* Channel Number */
96 wpa_printf(MSG_DEBUG, "P2P: * Listen Channel: Regulatory Class %u "
97 "Channel %u", reg_class, channel);
101 void p2p_buf_add_operating_channel(struct wpabuf *buf, const char *country,
102 u8 reg_class, u8 channel)
104 /* Operating Channel */
105 wpabuf_put_u8(buf, P2P_ATTR_OPERATING_CHANNEL);
106 wpabuf_put_le16(buf, 5);
107 wpabuf_put_data(buf, country, 3);
108 wpabuf_put_u8(buf, reg_class); /* Regulatory Class */
109 wpabuf_put_u8(buf, channel); /* Channel Number */
110 wpa_printf(MSG_DEBUG, "P2P: * Operating Channel: Regulatory Class %u "
111 "Channel %u", reg_class, channel);
115 void p2p_buf_add_channel_list(struct wpabuf *buf, const char *country,
116 struct p2p_channels *chan)
122 wpabuf_put_u8(buf, P2P_ATTR_CHANNEL_LIST);
123 len = wpabuf_put(buf, 2); /* IE length to be filled */
124 wpabuf_put_data(buf, country, 3); /* Country String */
126 for (i = 0; i < chan->reg_classes; i++) {
127 struct p2p_reg_class *c = &chan->reg_class[i];
128 wpabuf_put_u8(buf, c->reg_class);
129 wpabuf_put_u8(buf, c->channels);
130 wpabuf_put_data(buf, c->channel, c->channels);
133 /* Update attribute length */
134 WPA_PUT_LE16(len, (u8 *) wpabuf_put(buf, 0) - len - 2);
135 wpa_printf(MSG_DEBUG, "P2P: * Channel List");
139 void p2p_buf_add_status(struct wpabuf *buf, u8 status)
142 wpabuf_put_u8(buf, P2P_ATTR_STATUS);
143 wpabuf_put_le16(buf, 1);
144 wpabuf_put_u8(buf, status);
145 wpa_printf(MSG_DEBUG, "P2P: * Status: %d", status);
149 void p2p_buf_add_device_info(struct wpabuf *buf, struct p2p_data *p2p,
150 struct p2p_device *peer)
156 /* P2P Device Info */
157 wpabuf_put_u8(buf, P2P_ATTR_DEVICE_INFO);
158 len = wpabuf_put(buf, 2); /* IE length to be filled */
160 /* P2P Device address */
161 wpabuf_put_data(buf, p2p->cfg->dev_addr, ETH_ALEN);
165 if (peer && peer->wps_method != WPS_NOT_READY) {
166 if (peer->wps_method == WPS_PBC)
167 methods |= WPS_CONFIG_PUSHBUTTON;
168 else if (peer->wps_method == WPS_PIN_DISPLAY ||
169 peer->wps_method == WPS_PIN_KEYPAD)
170 methods |= WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
171 } else if (p2p->cfg->config_methods) {
172 methods |= p2p->cfg->config_methods &
173 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_DISPLAY |
176 methods |= WPS_CONFIG_PUSHBUTTON;
177 methods |= WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
179 wpabuf_put_be16(buf, methods);
181 /* Primary Device Type */
182 wpabuf_put_data(buf, p2p->cfg->pri_dev_type,
183 sizeof(p2p->cfg->pri_dev_type));
185 /* Number of Secondary Device Types */
186 wpabuf_put_u8(buf, p2p->cfg->num_sec_dev_types);
188 /* Secondary Device Type List */
189 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
190 wpabuf_put_data(buf, p2p->cfg->sec_dev_type[i],
194 nlen = p2p->cfg->dev_name ? os_strlen(p2p->cfg->dev_name) : 0;
195 wpabuf_put_be16(buf, ATTR_DEV_NAME);
196 wpabuf_put_be16(buf, nlen);
197 wpabuf_put_data(buf, p2p->cfg->dev_name, nlen);
199 /* Update attribute length */
200 WPA_PUT_LE16(len, (u8 *) wpabuf_put(buf, 0) - len - 2);
201 wpa_printf(MSG_DEBUG, "P2P: * Device Info");
205 void p2p_buf_add_device_id(struct wpabuf *buf, const u8 *dev_addr)
208 wpabuf_put_u8(buf, P2P_ATTR_DEVICE_ID);
209 wpabuf_put_le16(buf, ETH_ALEN);
210 wpabuf_put_data(buf, dev_addr, ETH_ALEN);
211 wpa_printf(MSG_DEBUG, "P2P: * Device ID: " MACSTR, MAC2STR(dev_addr));
215 void p2p_buf_add_config_timeout(struct wpabuf *buf, u8 go_timeout,
218 /* Configuration Timeout */
219 wpabuf_put_u8(buf, P2P_ATTR_CONFIGURATION_TIMEOUT);
220 wpabuf_put_le16(buf, 2);
221 wpabuf_put_u8(buf, go_timeout);
222 wpabuf_put_u8(buf, client_timeout);
223 wpa_printf(MSG_DEBUG, "P2P: * Configuration Timeout: GO %d (*10ms) "
224 "client %d (*10ms)", go_timeout, client_timeout);
228 void p2p_buf_add_intended_addr(struct wpabuf *buf, const u8 *interface_addr)
230 /* Intended P2P Interface Address */
231 wpabuf_put_u8(buf, P2P_ATTR_INTENDED_INTERFACE_ADDR);
232 wpabuf_put_le16(buf, ETH_ALEN);
233 wpabuf_put_data(buf, interface_addr, ETH_ALEN);
234 wpa_printf(MSG_DEBUG, "P2P: * Intended P2P Interface Address " MACSTR,
235 MAC2STR(interface_addr));
239 void p2p_buf_add_group_bssid(struct wpabuf *buf, const u8 *bssid)
241 /* P2P Group BSSID */
242 wpabuf_put_u8(buf, P2P_ATTR_GROUP_BSSID);
243 wpabuf_put_le16(buf, ETH_ALEN);
244 wpabuf_put_data(buf, bssid, ETH_ALEN);
245 wpa_printf(MSG_DEBUG, "P2P: * P2P Group BSSID " MACSTR,
250 void p2p_buf_add_group_id(struct wpabuf *buf, const u8 *dev_addr,
251 const u8 *ssid, size_t ssid_len)
254 wpabuf_put_u8(buf, P2P_ATTR_GROUP_ID);
255 wpabuf_put_le16(buf, ETH_ALEN + ssid_len);
256 wpabuf_put_data(buf, dev_addr, ETH_ALEN);
257 wpabuf_put_data(buf, ssid, ssid_len);
258 wpa_printf(MSG_DEBUG, "P2P: * P2P Group ID " MACSTR,
263 void p2p_buf_add_invitation_flags(struct wpabuf *buf, u8 flags)
265 /* Invitation Flags */
266 wpabuf_put_u8(buf, P2P_ATTR_INVITATION_FLAGS);
267 wpabuf_put_le16(buf, 1);
268 wpabuf_put_u8(buf, flags);
269 wpa_printf(MSG_DEBUG, "P2P: * Invitation Flags: bitmap 0x%x", flags);
273 static void p2p_buf_add_noa_desc(struct wpabuf *buf, struct p2p_noa_desc *desc)
278 wpabuf_put_u8(buf, desc->count_type);
279 wpabuf_put_le32(buf, desc->duration);
280 wpabuf_put_le32(buf, desc->interval);
281 wpabuf_put_le32(buf, desc->start_time);
285 void p2p_buf_add_noa(struct wpabuf *buf, u8 noa_index, u8 opp_ps, u8 ctwindow,
286 struct p2p_noa_desc *desc1, struct p2p_noa_desc *desc2)
288 /* Notice of Absence */
289 wpabuf_put_u8(buf, P2P_ATTR_NOTICE_OF_ABSENCE);
290 wpabuf_put_le16(buf, 2 + (desc1 ? 13 : 0) + (desc2 ? 13 : 0));
291 wpabuf_put_u8(buf, noa_index);
292 wpabuf_put_u8(buf, (opp_ps ? 0x80 : 0) | (ctwindow & 0x7f));
293 p2p_buf_add_noa_desc(buf, desc1);
294 p2p_buf_add_noa_desc(buf, desc2);
295 wpa_printf(MSG_DEBUG, "P2P: * Notice of Absence");
299 void p2p_buf_add_ext_listen_timing(struct wpabuf *buf, u16 period,
302 /* Extended Listen Timing */
303 wpabuf_put_u8(buf, P2P_ATTR_EXT_LISTEN_TIMING);
304 wpabuf_put_le16(buf, 4);
305 wpabuf_put_le16(buf, period);
306 wpabuf_put_le16(buf, interval);
307 wpa_printf(MSG_DEBUG, "P2P: * Extended Listen Timing (period %u msec "
308 "interval %u msec)", period, interval);
312 void p2p_buf_add_p2p_interface(struct wpabuf *buf, struct p2p_data *p2p)
315 wpabuf_put_u8(buf, P2P_ATTR_INTERFACE);
316 wpabuf_put_le16(buf, ETH_ALEN + 1 + ETH_ALEN);
317 /* P2P Device address */
318 wpabuf_put_data(buf, p2p->cfg->dev_addr, ETH_ALEN);
320 * FIX: Fetch interface address list from driver. Do not include
321 * the P2P Device address if it is never used as interface address.
323 /* P2P Interface Address Count */
324 wpabuf_put_u8(buf, 1);
325 wpabuf_put_data(buf, p2p->cfg->dev_addr, ETH_ALEN);
329 static void p2p_add_wps_string(struct wpabuf *buf, enum wps_attribute attr,
334 wpabuf_put_be16(buf, attr);
335 len = val ? os_strlen(val) : 0;
336 #ifndef CONFIG_WPS_STRICT
339 * Some deployed WPS implementations fail to parse zeor-length
340 * attributes. As a workaround, send a space character if the
341 * device attribute string is empty.
343 wpabuf_put_be16(buf, 1);
344 wpabuf_put_u8(buf, ' ');
347 #endif /* CONFIG_WPS_STRICT */
348 wpabuf_put_be16(buf, len);
350 wpabuf_put_data(buf, val, len);
354 void p2p_build_wps_ie(struct p2p_data *p2p, struct wpabuf *buf, int pw_id,
360 wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
361 len = wpabuf_put(buf, 1);
362 wpabuf_put_be32(buf, WPS_DEV_OUI_WFA);
364 wps_build_version(buf);
367 wpabuf_put_be16(buf, ATTR_WPS_STATE);
368 wpabuf_put_be16(buf, 1);
369 wpabuf_put_u8(buf, WPS_STATE_NOT_CONFIGURED);
373 /* Device Password ID */
374 wpabuf_put_be16(buf, ATTR_DEV_PASSWORD_ID);
375 wpabuf_put_be16(buf, 2);
376 wpa_printf(MSG_DEBUG, "P2P: WPS IE Device Password ID: %d",
378 wpabuf_put_be16(buf, pw_id);
382 wpabuf_put_be16(buf, ATTR_RESPONSE_TYPE);
383 wpabuf_put_be16(buf, 1);
384 wpabuf_put_u8(buf, WPS_RESP_ENROLLEE_INFO);
386 wps_build_uuid_e(buf, p2p->cfg->uuid);
387 p2p_add_wps_string(buf, ATTR_MANUFACTURER,
388 p2p->cfg->manufacturer);
389 p2p_add_wps_string(buf, ATTR_MODEL_NAME, p2p->cfg->model_name);
390 p2p_add_wps_string(buf, ATTR_MODEL_NUMBER,
391 p2p->cfg->model_number);
392 p2p_add_wps_string(buf, ATTR_SERIAL_NUMBER,
393 p2p->cfg->serial_number);
395 wpabuf_put_be16(buf, ATTR_PRIMARY_DEV_TYPE);
396 wpabuf_put_be16(buf, WPS_DEV_TYPE_LEN);
397 wpabuf_put_data(buf, p2p->cfg->pri_dev_type, WPS_DEV_TYPE_LEN);
399 p2p_add_wps_string(buf, ATTR_DEV_NAME, p2p->cfg->dev_name);
401 wpabuf_put_be16(buf, ATTR_CONFIG_METHODS);
402 wpabuf_put_be16(buf, 2);
403 wpabuf_put_be16(buf, p2p->cfg->config_methods);
406 wps_build_wfa_ext(buf, 0, NULL, 0);
408 if (all_attr && p2p->cfg->num_sec_dev_types) {
409 wpabuf_put_be16(buf, ATTR_SECONDARY_DEV_TYPE_LIST);
410 wpabuf_put_be16(buf, WPS_DEV_TYPE_LEN *
411 p2p->cfg->num_sec_dev_types);
412 wpabuf_put_data(buf, p2p->cfg->sec_dev_type,
414 p2p->cfg->num_sec_dev_types);
417 /* Add the WPS vendor extensions */
418 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
419 if (p2p->wps_vendor_ext[i] == NULL)
421 if (wpabuf_tailroom(buf) <
422 4 + wpabuf_len(p2p->wps_vendor_ext[i]))
424 wpabuf_put_be16(buf, ATTR_VENDOR_EXT);
425 wpabuf_put_be16(buf, wpabuf_len(p2p->wps_vendor_ext[i]));
426 wpabuf_put_buf(buf, p2p->wps_vendor_ext[i]);
429 p2p_buf_update_ie_hdr(buf, len);