ieee802_11_shared.c revision c5ec7f57ead87efa365800228aa0b09a12d9e6c4
1/*
2 * hostapd / IEEE 802.11 Management
3 * Copyright (c) 2002-2010, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "common/ieee802_11_defs.h"
13#include "hostapd.h"
14#include "sta_info.h"
15#include "ap_config.h"
16#include "ap_drv_ops.h"
17#include "ieee802_11.h"
18
19
20#ifdef CONFIG_IEEE80211W
21
22u8 * hostapd_eid_assoc_comeback_time(struct hostapd_data *hapd,
23				     struct sta_info *sta, u8 *eid)
24{
25	u8 *pos = eid;
26	u32 timeout, tu;
27	struct os_time now, passed;
28
29	*pos++ = WLAN_EID_TIMEOUT_INTERVAL;
30	*pos++ = 5;
31	*pos++ = WLAN_TIMEOUT_ASSOC_COMEBACK;
32	os_get_time(&now);
33	os_time_sub(&now, &sta->sa_query_start, &passed);
34	tu = (passed.sec * 1000000 + passed.usec) / 1024;
35	if (hapd->conf->assoc_sa_query_max_timeout > tu)
36		timeout = hapd->conf->assoc_sa_query_max_timeout - tu;
37	else
38		timeout = 0;
39	if (timeout < hapd->conf->assoc_sa_query_max_timeout)
40		timeout++; /* add some extra time for local timers */
41	WPA_PUT_LE32(pos, timeout);
42	pos += 4;
43
44	return pos;
45}
46
47
48/* MLME-SAQuery.request */
49void ieee802_11_send_sa_query_req(struct hostapd_data *hapd,
50				  const u8 *addr, const u8 *trans_id)
51{
52	struct ieee80211_mgmt mgmt;
53	u8 *end;
54
55	wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Request to "
56		   MACSTR, MAC2STR(addr));
57	wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
58		    trans_id, WLAN_SA_QUERY_TR_ID_LEN);
59
60	os_memset(&mgmt, 0, sizeof(mgmt));
61	mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
62					  WLAN_FC_STYPE_ACTION);
63	os_memcpy(mgmt.da, addr, ETH_ALEN);
64	os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
65	os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
66	mgmt.u.action.category = WLAN_ACTION_SA_QUERY;
67	mgmt.u.action.u.sa_query_req.action = WLAN_SA_QUERY_REQUEST;
68	os_memcpy(mgmt.u.action.u.sa_query_req.trans_id, trans_id,
69		  WLAN_SA_QUERY_TR_ID_LEN);
70	end = mgmt.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
71	if (hostapd_drv_send_mlme(hapd, &mgmt, end - (u8 *) &mgmt, 0) < 0)
72		perror("ieee802_11_send_sa_query_req: send");
73}
74
75
76static void ieee802_11_send_sa_query_resp(struct hostapd_data *hapd,
77					  const u8 *sa, const u8 *trans_id)
78{
79	struct sta_info *sta;
80	struct ieee80211_mgmt resp;
81	u8 *end;
82
83	wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Request from "
84		   MACSTR, MAC2STR(sa));
85	wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
86		    trans_id, WLAN_SA_QUERY_TR_ID_LEN);
87
88	sta = ap_get_sta(hapd, sa);
89	if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
90		wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignore SA Query Request "
91			   "from unassociated STA " MACSTR, MAC2STR(sa));
92		return;
93	}
94
95	wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Response to "
96		   MACSTR, MAC2STR(sa));
97
98	os_memset(&resp, 0, sizeof(resp));
99	resp.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
100					  WLAN_FC_STYPE_ACTION);
101	os_memcpy(resp.da, sa, ETH_ALEN);
102	os_memcpy(resp.sa, hapd->own_addr, ETH_ALEN);
103	os_memcpy(resp.bssid, hapd->own_addr, ETH_ALEN);
104	resp.u.action.category = WLAN_ACTION_SA_QUERY;
105	resp.u.action.u.sa_query_req.action = WLAN_SA_QUERY_RESPONSE;
106	os_memcpy(resp.u.action.u.sa_query_req.trans_id, trans_id,
107		  WLAN_SA_QUERY_TR_ID_LEN);
108	end = resp.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
109	if (hostapd_drv_send_mlme(hapd, &resp, end - (u8 *) &resp, 0) < 0)
110		perror("ieee80211_mgmt_sa_query_request: send");
111}
112
113
114void ieee802_11_sa_query_action(struct hostapd_data *hapd, const u8 *sa,
115				const u8 action_type, const u8 *trans_id)
116{
117	struct sta_info *sta;
118	int i;
119
120	if (action_type == WLAN_SA_QUERY_REQUEST) {
121		ieee802_11_send_sa_query_resp(hapd, sa, trans_id);
122		return;
123	}
124
125	if (action_type != WLAN_SA_QUERY_RESPONSE) {
126		wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected SA Query "
127			   "Action %d", action_type);
128		return;
129	}
130
131	wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Response from "
132		   MACSTR, MAC2STR(sa));
133	wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
134		    trans_id, WLAN_SA_QUERY_TR_ID_LEN);
135
136	/* MLME-SAQuery.confirm */
137
138	sta = ap_get_sta(hapd, sa);
139	if (sta == NULL || sta->sa_query_trans_id == NULL) {
140		wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
141			   "pending SA Query request found");
142		return;
143	}
144
145	for (i = 0; i < sta->sa_query_count; i++) {
146		if (os_memcmp(sta->sa_query_trans_id +
147			      i * WLAN_SA_QUERY_TR_ID_LEN,
148			      trans_id, WLAN_SA_QUERY_TR_ID_LEN) == 0)
149			break;
150	}
151
152	if (i >= sta->sa_query_count) {
153		wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching SA Query "
154			   "transaction identifier found");
155		return;
156	}
157
158	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
159		       HOSTAPD_LEVEL_DEBUG,
160		       "Reply to pending SA Query received");
161	ap_sta_stop_sa_query(hapd, sta);
162}
163
164#endif /* CONFIG_IEEE80211W */
165
166
167u8 * hostapd_eid_ext_capab(struct hostapd_data *hapd, u8 *eid)
168{
169	u8 *pos = eid;
170	u8 len = 0;
171
172	if (hapd->conf->tdls & (TDLS_PROHIBIT | TDLS_PROHIBIT_CHAN_SWITCH))
173		len = 5;
174	if (len < 4 && hapd->conf->interworking)
175		len = 4;
176	if (len == 0)
177		return eid;
178
179	*pos++ = WLAN_EID_EXT_CAPAB;
180	*pos++ = len;
181	*pos++ = 0x00;
182	*pos++ = 0x00;
183	*pos++ = 0x00;
184
185	*pos = 0x00;
186	if (hapd->conf->time_advertisement == 2)
187		*pos |= 0x08; /* Bit 27 - UTC TSF Offset */
188	if (hapd->conf->interworking)
189		*pos |= 0x80; /* Bit 31 - Interworking */
190	pos++;
191
192	if (len < 5)
193		return pos;
194	*pos = 0x00;
195	if (hapd->conf->tdls & TDLS_PROHIBIT)
196		*pos |= 0x40; /* Bit 38 - TDLS Prohibited */
197	if (hapd->conf->tdls & TDLS_PROHIBIT_CHAN_SWITCH)
198		*pos |= 0x80; /* Bit 39 - TDLS Channel Switching Prohibited */
199	pos++;
200
201	return pos;
202}
203
204
205u8 * hostapd_eid_interworking(struct hostapd_data *hapd, u8 *eid)
206{
207	u8 *pos = eid;
208#ifdef CONFIG_INTERWORKING
209	u8 *len;
210
211	if (!hapd->conf->interworking)
212		return eid;
213
214	*pos++ = WLAN_EID_INTERWORKING;
215	len = pos++;
216
217	*pos = hapd->conf->access_network_type;
218	if (hapd->conf->internet)
219		*pos |= INTERWORKING_ANO_INTERNET;
220	if (hapd->conf->asra)
221		*pos |= INTERWORKING_ANO_ASRA;
222	if (hapd->conf->esr)
223		*pos |= INTERWORKING_ANO_ESR;
224	if (hapd->conf->uesa)
225		*pos |= INTERWORKING_ANO_UESA;
226	pos++;
227
228	if (hapd->conf->venue_info_set) {
229		*pos++ = hapd->conf->venue_group;
230		*pos++ = hapd->conf->venue_type;
231	}
232
233	if (!is_zero_ether_addr(hapd->conf->hessid)) {
234		os_memcpy(pos, hapd->conf->hessid, ETH_ALEN);
235		pos += ETH_ALEN;
236	}
237
238	*len = pos - len - 1;
239#endif /* CONFIG_INTERWORKING */
240
241	return pos;
242}
243
244
245u8 * hostapd_eid_adv_proto(struct hostapd_data *hapd, u8 *eid)
246{
247	u8 *pos = eid;
248#ifdef CONFIG_INTERWORKING
249
250	/* TODO: Separate configuration for ANQP? */
251	if (!hapd->conf->interworking)
252		return eid;
253
254	*pos++ = WLAN_EID_ADV_PROTO;
255	*pos++ = 2;
256	*pos++ = 0x7F; /* Query Response Length Limit | PAME-BI */
257	*pos++ = ACCESS_NETWORK_QUERY_PROTOCOL;
258#endif /* CONFIG_INTERWORKING */
259
260	return pos;
261}
262
263
264u8 * hostapd_eid_roaming_consortium(struct hostapd_data *hapd, u8 *eid)
265{
266	u8 *pos = eid;
267#ifdef CONFIG_INTERWORKING
268	u8 *len;
269	unsigned int i, count;
270
271	if (!hapd->conf->interworking ||
272	    hapd->conf->roaming_consortium == NULL ||
273	    hapd->conf->roaming_consortium_count == 0)
274		return eid;
275
276	*pos++ = WLAN_EID_ROAMING_CONSORTIUM;
277	len = pos++;
278
279	/* Number of ANQP OIs (in addition to the max 3 listed here) */
280	if (hapd->conf->roaming_consortium_count > 3 + 255)
281		*pos++ = 255;
282	else if (hapd->conf->roaming_consortium_count > 3)
283		*pos++ = hapd->conf->roaming_consortium_count - 3;
284	else
285		*pos++ = 0;
286
287	/* OU #1 and #2 Lengths */
288	*pos = hapd->conf->roaming_consortium[0].len;
289	if (hapd->conf->roaming_consortium_count > 1)
290		*pos |= hapd->conf->roaming_consortium[1].len << 4;
291	pos++;
292
293	if (hapd->conf->roaming_consortium_count > 3)
294		count = 3;
295	else
296		count = hapd->conf->roaming_consortium_count;
297
298	for (i = 0; i < count; i++) {
299		os_memcpy(pos, hapd->conf->roaming_consortium[i].oi,
300			  hapd->conf->roaming_consortium[i].len);
301		pos += hapd->conf->roaming_consortium[i].len;
302	}
303
304	*len = pos - len - 1;
305#endif /* CONFIG_INTERWORKING */
306
307	return pos;
308}
309
310
311u8 * hostapd_eid_time_adv(struct hostapd_data *hapd, u8 *eid)
312{
313	if (hapd->conf->time_advertisement != 2)
314		return eid;
315
316	if (hapd->time_adv == NULL &&
317	    hostapd_update_time_adv(hapd) < 0)
318		return eid;
319
320	if (hapd->time_adv == NULL)
321		return eid;
322
323	os_memcpy(eid, wpabuf_head(hapd->time_adv),
324		  wpabuf_len(hapd->time_adv));
325	eid += wpabuf_len(hapd->time_adv);
326
327	return eid;
328}
329
330
331u8 * hostapd_eid_time_zone(struct hostapd_data *hapd, u8 *eid)
332{
333	size_t len;
334
335	if (hapd->conf->time_advertisement != 2)
336		return eid;
337
338	len = os_strlen(hapd->conf->time_zone);
339
340	*eid++ = WLAN_EID_TIME_ZONE;
341	*eid++ = len;
342	os_memcpy(eid, hapd->conf->time_zone, len);
343	eid += len;
344
345	return eid;
346}
347
348
349int hostapd_update_time_adv(struct hostapd_data *hapd)
350{
351	const int elen = 2 + 1 + 10 + 5 + 1;
352	struct os_time t;
353	struct os_tm tm;
354	u8 *pos;
355
356	if (hapd->conf->time_advertisement != 2)
357		return 0;
358
359	if (os_get_time(&t) < 0 || os_gmtime(t.sec, &tm) < 0)
360		return -1;
361
362	if (!hapd->time_adv) {
363		hapd->time_adv = wpabuf_alloc(elen);
364		if (hapd->time_adv == NULL)
365			return -1;
366		pos = wpabuf_put(hapd->time_adv, elen);
367	} else
368		pos = wpabuf_mhead_u8(hapd->time_adv);
369
370	*pos++ = WLAN_EID_TIME_ADVERTISEMENT;
371	*pos++ = 1 + 10 + 5 + 1;
372
373	*pos++ = 2; /* UTC time at which the TSF timer is 0 */
374
375	/* Time Value at TSF 0 */
376	/* FIX: need to calculate this based on the current TSF value */
377	WPA_PUT_LE16(pos, tm.year); /* Year */
378	pos += 2;
379	*pos++ = tm.month; /* Month */
380	*pos++ = tm.day; /* Day of month */
381	*pos++ = tm.hour; /* Hours */
382	*pos++ = tm.min; /* Minutes */
383	*pos++ = tm.sec; /* Seconds */
384	WPA_PUT_LE16(pos, 0); /* Milliseconds (not used) */
385	pos += 2;
386	*pos++ = 0; /* Reserved */
387
388	/* Time Error */
389	/* TODO: fill in an estimate on the error */
390	*pos++ = 0;
391	*pos++ = 0;
392	*pos++ = 0;
393	*pos++ = 0;
394	*pos++ = 0;
395
396	*pos++ = hapd->time_update_counter++;
397
398	return 0;
399}
400