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