p2p_build.c revision 8d520ff1dc2da35cdca849e982051b86468016d8
1/*
2 * P2P - IE builder
3 * Copyright (c) 2009-2010, Atheros Communications
4 *
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.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15#include "includes.h"
16
17#include "common.h"
18#include "common/ieee802_11_defs.h"
19#include "wps/wps_i.h"
20#include "p2p_i.h"
21
22
23void p2p_buf_add_action_hdr(struct wpabuf *buf, u8 subtype, u8 dialog_token)
24{
25	wpabuf_put_u8(buf, WLAN_ACTION_VENDOR_SPECIFIC);
26	wpabuf_put_be24(buf, OUI_WFA);
27	wpabuf_put_u8(buf, P2P_OUI_TYPE);
28
29	wpabuf_put_u8(buf, subtype); /* OUI Subtype */
30	wpabuf_put_u8(buf, dialog_token);
31	wpa_printf(MSG_DEBUG, "P2P: * Dialog Token: %d", dialog_token);
32}
33
34
35void p2p_buf_add_public_action_hdr(struct wpabuf *buf, u8 subtype,
36				   u8 dialog_token)
37{
38	wpabuf_put_u8(buf, WLAN_ACTION_PUBLIC);
39	wpabuf_put_u8(buf, WLAN_PA_VENDOR_SPECIFIC);
40	wpabuf_put_be24(buf, OUI_WFA);
41	wpabuf_put_u8(buf, P2P_OUI_TYPE);
42
43	wpabuf_put_u8(buf, subtype); /* OUI Subtype */
44	wpabuf_put_u8(buf, dialog_token);
45	wpa_printf(MSG_DEBUG, "P2P: * Dialog Token: %d", dialog_token);
46}
47
48
49u8 * p2p_buf_add_ie_hdr(struct wpabuf *buf)
50{
51	u8 *len;
52
53	/* P2P IE header */
54	wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
55	len = wpabuf_put(buf, 1); /* IE length to be filled */
56	wpabuf_put_be24(buf, OUI_WFA);
57	wpabuf_put_u8(buf, P2P_OUI_TYPE);
58	wpa_printf(MSG_DEBUG, "P2P: * P2P IE header");
59	return len;
60}
61
62
63void p2p_buf_update_ie_hdr(struct wpabuf *buf, u8 *len)
64{
65	/* Update P2P IE Length */
66	*len = (u8 *) wpabuf_put(buf, 0) - len - 1;
67}
68
69
70void p2p_buf_add_capability(struct wpabuf *buf, u8 dev_capab, u8 group_capab)
71{
72	/* P2P Capability */
73	wpabuf_put_u8(buf, P2P_ATTR_CAPABILITY);
74	wpabuf_put_le16(buf, 2);
75	wpabuf_put_u8(buf, dev_capab); /* Device Capabilities */
76	wpabuf_put_u8(buf, group_capab); /* Group Capabilities */
77	wpa_printf(MSG_DEBUG, "P2P: * Capability dev=%02x group=%02x",
78		   dev_capab, group_capab);
79}
80
81
82void p2p_buf_add_go_intent(struct wpabuf *buf, u8 go_intent)
83{
84	/* Group Owner Intent */
85	wpabuf_put_u8(buf, P2P_ATTR_GROUP_OWNER_INTENT);
86	wpabuf_put_le16(buf, 1);
87	wpabuf_put_u8(buf, go_intent);
88	wpa_printf(MSG_DEBUG, "P2P: * GO Intent: Intent %u Tie breaker %u",
89		   go_intent >> 1, go_intent & 0x01);
90}
91
92
93void p2p_buf_add_listen_channel(struct wpabuf *buf, const char *country,
94				u8 reg_class, u8 channel)
95{
96	/* Listen Channel */
97	wpabuf_put_u8(buf, P2P_ATTR_LISTEN_CHANNEL);
98	wpabuf_put_le16(buf, 5);
99	wpabuf_put_data(buf, country, 3);
100	wpabuf_put_u8(buf, reg_class); /* Regulatory Class */
101	wpabuf_put_u8(buf, channel); /* Channel Number */
102	wpa_printf(MSG_DEBUG, "P2P: * Listen Channel: Regulatory Class %u "
103		   "Channel %u", reg_class, channel);
104}
105
106
107void p2p_buf_add_operating_channel(struct wpabuf *buf, const char *country,
108				   u8 reg_class, u8 channel)
109{
110	/* Operating Channel */
111	wpabuf_put_u8(buf, P2P_ATTR_OPERATING_CHANNEL);
112	wpabuf_put_le16(buf, 5);
113	wpabuf_put_data(buf, country, 3);
114	wpabuf_put_u8(buf, reg_class); /* Regulatory Class */
115	wpabuf_put_u8(buf, channel); /* Channel Number */
116	wpa_printf(MSG_DEBUG, "P2P: * Operating Channel: Regulatory Class %u "
117		   "Channel %u", reg_class, channel);
118}
119
120
121void p2p_buf_add_channel_list(struct wpabuf *buf, const char *country,
122			      struct p2p_channels *chan)
123{
124	u8 *len;
125	size_t i;
126
127	/* Channel List */
128	wpabuf_put_u8(buf, P2P_ATTR_CHANNEL_LIST);
129	len = wpabuf_put(buf, 2); /* IE length to be filled */
130	wpabuf_put_data(buf, country, 3); /* Country String */
131
132	for (i = 0; i < chan->reg_classes; i++) {
133		struct p2p_reg_class *c = &chan->reg_class[i];
134		wpabuf_put_u8(buf, c->reg_class);
135		wpabuf_put_u8(buf, c->channels);
136		wpabuf_put_data(buf, c->channel, c->channels);
137	}
138
139	/* Update attribute length */
140	WPA_PUT_LE16(len, (u8 *) wpabuf_put(buf, 0) - len - 2);
141	wpa_printf(MSG_DEBUG, "P2P: * Channel List");
142}
143
144
145void p2p_buf_add_status(struct wpabuf *buf, u8 status)
146{
147	/* Status */
148	wpabuf_put_u8(buf, P2P_ATTR_STATUS);
149	wpabuf_put_le16(buf, 1);
150	wpabuf_put_u8(buf, status);
151	wpa_printf(MSG_DEBUG, "P2P: * Status: %d", status);
152}
153
154
155void p2p_buf_add_device_info(struct wpabuf *buf, struct p2p_data *p2p,
156			     struct p2p_device *peer)
157{
158	u8 *len;
159	u16 methods;
160	size_t nlen, i;
161
162	/* P2P Device Info */
163	wpabuf_put_u8(buf, P2P_ATTR_DEVICE_INFO);
164	len = wpabuf_put(buf, 2); /* IE length to be filled */
165
166	/* P2P Device address */
167	wpabuf_put_data(buf, p2p->cfg->dev_addr, ETH_ALEN);
168
169	/* Config Methods */
170	methods = 0;
171	if (peer && peer->wps_method != WPS_NOT_READY) {
172		if (peer->wps_method == WPS_PBC)
173			methods |= WPS_CONFIG_PUSHBUTTON;
174		else if (peer->wps_method == WPS_PIN_LABEL)
175			methods |= WPS_CONFIG_LABEL;
176		else if (peer->wps_method == WPS_PIN_DISPLAY ||
177			 peer->wps_method == WPS_PIN_KEYPAD)
178			methods |= WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
179	} else {
180		methods |= WPS_CONFIG_PUSHBUTTON;
181		methods |= WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
182	}
183	wpabuf_put_be16(buf, methods);
184
185	/* Primary Device Type */
186	wpabuf_put_data(buf, p2p->cfg->pri_dev_type,
187			sizeof(p2p->cfg->pri_dev_type));
188
189	/* Number of Secondary Device Types */
190	wpabuf_put_u8(buf, p2p->cfg->num_sec_dev_types);
191
192	/* Secondary Device Type List */
193	for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
194		wpabuf_put_data(buf, p2p->cfg->sec_dev_type[i],
195				WPS_DEV_TYPE_LEN);
196
197	/* Device Name */
198	nlen = p2p->cfg->dev_name ? os_strlen(p2p->cfg->dev_name) : 0;
199	wpabuf_put_be16(buf, ATTR_DEV_NAME);
200	wpabuf_put_be16(buf, nlen);
201	wpabuf_put_data(buf, p2p->cfg->dev_name, nlen);
202
203	/* Update attribute length */
204	WPA_PUT_LE16(len, (u8 *) wpabuf_put(buf, 0) - len - 2);
205	wpa_printf(MSG_DEBUG, "P2P: * Device Info");
206}
207
208
209void p2p_buf_add_device_id(struct wpabuf *buf, const u8 *dev_addr)
210{
211	/* P2P Device ID */
212	wpabuf_put_u8(buf, P2P_ATTR_DEVICE_ID);
213	wpabuf_put_le16(buf, ETH_ALEN);
214	wpabuf_put_data(buf, dev_addr, ETH_ALEN);
215	wpa_printf(MSG_DEBUG, "P2P: * Device ID: " MACSTR, MAC2STR(dev_addr));
216}
217
218
219void p2p_buf_add_config_timeout(struct wpabuf *buf, u8 go_timeout,
220				u8 client_timeout)
221{
222	/* Configuration Timeout */
223	wpabuf_put_u8(buf, P2P_ATTR_CONFIGURATION_TIMEOUT);
224	wpabuf_put_le16(buf, 2);
225	wpabuf_put_u8(buf, go_timeout);
226	wpabuf_put_u8(buf, client_timeout);
227	wpa_printf(MSG_DEBUG, "P2P: * Configuration Timeout: GO %d (*10ms)  "
228		   "client %d (*10ms)", go_timeout, client_timeout);
229}
230
231
232void p2p_buf_add_intended_addr(struct wpabuf *buf, const u8 *interface_addr)
233{
234	/* Intended P2P Interface Address */
235	wpabuf_put_u8(buf, P2P_ATTR_INTENDED_INTERFACE_ADDR);
236	wpabuf_put_le16(buf, ETH_ALEN);
237	wpabuf_put_data(buf, interface_addr, ETH_ALEN);
238	wpa_printf(MSG_DEBUG, "P2P: * Intended P2P Interface Address " MACSTR,
239		   MAC2STR(interface_addr));
240}
241
242
243void p2p_buf_add_group_bssid(struct wpabuf *buf, const u8 *bssid)
244{
245	/* P2P Group BSSID */
246	wpabuf_put_u8(buf, P2P_ATTR_GROUP_BSSID);
247	wpabuf_put_le16(buf, ETH_ALEN);
248	wpabuf_put_data(buf, bssid, ETH_ALEN);
249	wpa_printf(MSG_DEBUG, "P2P: * P2P Group BSSID " MACSTR,
250		   MAC2STR(bssid));
251}
252
253
254void p2p_buf_add_group_id(struct wpabuf *buf, const u8 *dev_addr,
255			  const u8 *ssid, size_t ssid_len)
256{
257	/* P2P Group ID */
258	wpabuf_put_u8(buf, P2P_ATTR_GROUP_ID);
259	wpabuf_put_le16(buf, ETH_ALEN + ssid_len);
260	wpabuf_put_data(buf, dev_addr, ETH_ALEN);
261	wpabuf_put_data(buf, ssid, ssid_len);
262	wpa_printf(MSG_DEBUG, "P2P: * P2P Group ID " MACSTR,
263		   MAC2STR(dev_addr));
264}
265
266
267void p2p_buf_add_invitation_flags(struct wpabuf *buf, u8 flags)
268{
269	/* Invitation Flags */
270	wpabuf_put_u8(buf, P2P_ATTR_INVITATION_FLAGS);
271	wpabuf_put_le16(buf, 1);
272	wpabuf_put_u8(buf, flags);
273	wpa_printf(MSG_DEBUG, "P2P: * Invitation Flags: bitmap 0x%x", flags);
274}
275
276
277static void p2p_buf_add_noa_desc(struct wpabuf *buf, struct p2p_noa_desc *desc)
278{
279	if (desc == NULL)
280		return;
281
282	wpabuf_put_u8(buf, desc->count_type);
283	wpabuf_put_le32(buf, desc->duration);
284	wpabuf_put_le32(buf, desc->interval);
285	wpabuf_put_le32(buf, desc->start_time);
286}
287
288
289void p2p_buf_add_noa(struct wpabuf *buf, u8 noa_index, u8 opp_ps, u8 ctwindow,
290		     struct p2p_noa_desc *desc1, struct p2p_noa_desc *desc2)
291{
292	/* Notice of Absence */
293	wpabuf_put_u8(buf, P2P_ATTR_NOTICE_OF_ABSENCE);
294	wpabuf_put_le16(buf, 2 + (desc1 ? 13 : 0) + (desc2 ? 13 : 0));
295	wpabuf_put_u8(buf, noa_index);
296	wpabuf_put_u8(buf, (opp_ps ? 0x80 : 0) | (ctwindow & 0x7f));
297	p2p_buf_add_noa_desc(buf, desc1);
298	p2p_buf_add_noa_desc(buf, desc2);
299	wpa_printf(MSG_DEBUG, "P2P: * Notice of Absence");
300}
301
302
303void p2p_buf_add_ext_listen_timing(struct wpabuf *buf, u16 period,
304				   u16 interval)
305{
306	/* Extended Listen Timing */
307	wpabuf_put_u8(buf, P2P_ATTR_EXT_LISTEN_TIMING);
308	wpabuf_put_le16(buf, 4);
309	wpabuf_put_le16(buf, period);
310	wpabuf_put_le16(buf, interval);
311	wpa_printf(MSG_DEBUG, "P2P: * Extended Listen Timing (period %u msec  "
312		   "interval %u msec)", period, interval);
313}
314
315
316void p2p_buf_add_p2p_interface(struct wpabuf *buf, struct p2p_data *p2p)
317{
318	/* P2P Interface */
319	wpabuf_put_u8(buf, P2P_ATTR_INTERFACE);
320	wpabuf_put_le16(buf, ETH_ALEN + 1 + ETH_ALEN);
321	/* P2P Device address */
322	wpabuf_put_data(buf, p2p->cfg->dev_addr, ETH_ALEN);
323	/*
324	 * FIX: Fetch interface address list from driver. Do not include
325	 * the P2P Device address if it is never used as interface address.
326	 */
327	/* P2P Interface Address Count */
328	wpabuf_put_u8(buf, 1);
329	wpabuf_put_data(buf, p2p->cfg->dev_addr, ETH_ALEN);
330}
331
332
333static void p2p_add_wps_string(struct wpabuf *buf, enum wps_attribute attr,
334			       const char *val)
335{
336	size_t len;
337
338	wpabuf_put_be16(buf, attr);
339	len = val ? os_strlen(val) : 0;
340#ifndef CONFIG_WPS_STRICT
341	if (len == 0) {
342		/*
343		 * Some deployed WPS implementations fail to parse zeor-length
344		 * attributes. As a workaround, send a space character if the
345		 * device attribute string is empty.
346		 */
347		wpabuf_put_be16(buf, 1);
348		wpabuf_put_u8(buf, ' ');
349		return;
350	}
351#endif /* CONFIG_WPS_STRICT */
352	wpabuf_put_be16(buf, len);
353	if (val)
354		wpabuf_put_data(buf, val, len);
355}
356
357
358void p2p_build_wps_ie(struct p2p_data *p2p, struct wpabuf *buf, u16 pw_id,
359		      int all_attr)
360{
361	u8 *len;
362	int i;
363
364	wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
365	len = wpabuf_put(buf, 1);
366	wpabuf_put_be32(buf, WPS_DEV_OUI_WFA);
367
368	wps_build_version(buf);
369
370	if (all_attr) {
371		wpabuf_put_be16(buf, ATTR_WPS_STATE);
372		wpabuf_put_be16(buf, 1);
373		wpabuf_put_u8(buf, WPS_STATE_NOT_CONFIGURED);
374	}
375
376	/* Device Password ID */
377	wpabuf_put_be16(buf, ATTR_DEV_PASSWORD_ID);
378	wpabuf_put_be16(buf, 2);
379	wpa_printf(MSG_DEBUG, "P2P: WPS IE Device Password ID: %d", pw_id);
380	wpabuf_put_be16(buf, pw_id);
381
382	if (all_attr) {
383		wpabuf_put_be16(buf, ATTR_RESPONSE_TYPE);
384		wpabuf_put_be16(buf, 1);
385		wpabuf_put_u8(buf, WPS_RESP_ENROLLEE_INFO);
386
387		wps_build_uuid_e(buf, p2p->cfg->uuid);
388		p2p_add_wps_string(buf, ATTR_MANUFACTURER,
389				   p2p->cfg->manufacturer);
390		p2p_add_wps_string(buf, ATTR_MODEL_NAME, p2p->cfg->model_name);
391		p2p_add_wps_string(buf, ATTR_MODEL_NUMBER,
392				   p2p->cfg->model_number);
393		p2p_add_wps_string(buf, ATTR_SERIAL_NUMBER,
394				   p2p->cfg->serial_number);
395
396		wpabuf_put_be16(buf, ATTR_PRIMARY_DEV_TYPE);
397		wpabuf_put_be16(buf, WPS_DEV_TYPE_LEN);
398		wpabuf_put_data(buf, p2p->cfg->pri_dev_type, WPS_DEV_TYPE_LEN);
399
400		p2p_add_wps_string(buf, ATTR_DEV_NAME, p2p->cfg->dev_name);
401
402		wpabuf_put_be16(buf, ATTR_CONFIG_METHODS);
403		wpabuf_put_be16(buf, 2);
404		wpabuf_put_be16(buf, p2p->cfg->config_methods);
405	}
406
407	wps_build_wfa_ext(buf, 0, NULL, 0);
408
409	if (all_attr && p2p->cfg->num_sec_dev_types) {
410		wpabuf_put_be16(buf, ATTR_SECONDARY_DEV_TYPE_LIST);
411		wpabuf_put_be16(buf, WPS_DEV_TYPE_LEN *
412				p2p->cfg->num_sec_dev_types);
413		wpabuf_put_data(buf, p2p->cfg->sec_dev_type,
414				WPS_DEV_TYPE_LEN *
415				p2p->cfg->num_sec_dev_types);
416	}
417
418	/* Add the WPS vendor extensions */
419	for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
420		if (p2p->wps_vendor_ext[i] == NULL)
421			break;
422		if (wpabuf_tailroom(buf) <
423		    4 + wpabuf_len(p2p->wps_vendor_ext[i]))
424			continue;
425		wpabuf_put_be16(buf, ATTR_VENDOR_EXT);
426		wpabuf_put_be16(buf, wpabuf_len(p2p->wps_vendor_ext[i]));
427		wpabuf_put_buf(buf, p2p->wps_vendor_ext[i]);
428	}
429
430	p2p_buf_update_ie_hdr(buf, len);
431}
432