]>
Commit | Line | Data |
---|---|---|
d4370eac MP |
1 | /* |
2 | * hostapd / IEEE 802.11 Management | |
b6668734 | 3 | * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi> |
d4370eac | 4 | * |
0f3d578e JM |
5 | * This software may be distributed under the terms of the BSD license. |
6 | * See README for more details. | |
d4370eac MP |
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" | |
39b97072 | 17 | #include "ieee802_11.h" |
d4370eac MP |
18 | |
19 | ||
20 | #ifdef CONFIG_IEEE80211W | |
21 | ||
22 | u8 * 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 */ | |
49 | void 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; | |
8cfa3527 | 71 | if (hostapd_drv_send_mlme(hapd, &mgmt, end - (u8 *) &mgmt, 0) < 0) |
d4370eac MP |
72 | perror("ieee802_11_send_sa_query_req: send"); |
73 | } | |
74 | ||
75 | ||
19df9b07 JM |
76 | static void ieee802_11_send_sa_query_resp(struct hostapd_data *hapd, |
77 | const u8 *sa, const u8 *trans_id) | |
d4370eac MP |
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; | |
8cfa3527 | 109 | if (hostapd_drv_send_mlme(hapd, &resp, end - (u8 *) &resp, 0) < 0) |
d4370eac MP |
110 | perror("ieee80211_mgmt_sa_query_request: send"); |
111 | } | |
112 | ||
113 | ||
114 | void 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 */ | |
06c4d247 JM |
165 | |
166 | ||
167 | u8 * 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; | |
c79938a5 JM |
176 | if (len < 3 && hapd->conf->wnm_sleep_mode) |
177 | len = 3; | |
b93c8509 JM |
178 | if (len < 7 && hapd->conf->ssid.utf8_ssid) |
179 | len = 7; | |
0a66ce3c JM |
180 | #ifdef CONFIG_WNM |
181 | if (len < 4) | |
182 | len = 4; | |
183 | #endif /* CONFIG_WNM */ | |
06c4d247 JM |
184 | if (len == 0) |
185 | return eid; | |
186 | ||
187 | *pos++ = WLAN_EID_EXT_CAPAB; | |
188 | *pos++ = len; | |
189 | *pos++ = 0x00; | |
190 | *pos++ = 0x00; | |
06c4d247 | 191 | |
c79938a5 JM |
192 | *pos = 0x00; |
193 | if (hapd->conf->wnm_sleep_mode) | |
194 | *pos |= 0x02; /* Bit 17 - WNM-Sleep Mode */ | |
195 | pos++; | |
196 | ||
197 | if (len < 4) | |
198 | return pos; | |
06c4d247 | 199 | *pos = 0x00; |
0a66ce3c JM |
200 | #ifdef CONFIG_WNM |
201 | *pos |= 0x02; /* Bit 25 - SSID List */ | |
202 | #endif /* CONFIG_WNM */ | |
39b97072 JM |
203 | if (hapd->conf->time_advertisement == 2) |
204 | *pos |= 0x08; /* Bit 27 - UTC TSF Offset */ | |
06c4d247 JM |
205 | if (hapd->conf->interworking) |
206 | *pos |= 0x80; /* Bit 31 - Interworking */ | |
207 | pos++; | |
208 | ||
209 | if (len < 5) | |
210 | return pos; | |
211 | *pos = 0x00; | |
212 | if (hapd->conf->tdls & TDLS_PROHIBIT) | |
213 | *pos |= 0x40; /* Bit 38 - TDLS Prohibited */ | |
214 | if (hapd->conf->tdls & TDLS_PROHIBIT_CHAN_SWITCH) | |
215 | *pos |= 0x80; /* Bit 39 - TDLS Channel Switching Prohibited */ | |
216 | pos++; | |
217 | ||
b93c8509 JM |
218 | if (len < 6) |
219 | return pos; | |
220 | *pos = 0x00; | |
221 | pos++; | |
222 | ||
223 | if (len < 7) | |
224 | return pos; | |
225 | *pos = 0x00; | |
226 | if (hapd->conf->ssid.utf8_ssid) | |
227 | *pos |= 0x01; /* Bit 48 - UTF-8 SSID */ | |
228 | pos++; | |
229 | ||
06c4d247 JM |
230 | return pos; |
231 | } | |
232 | ||
233 | ||
234 | u8 * hostapd_eid_interworking(struct hostapd_data *hapd, u8 *eid) | |
235 | { | |
236 | u8 *pos = eid; | |
237 | #ifdef CONFIG_INTERWORKING | |
238 | u8 *len; | |
239 | ||
240 | if (!hapd->conf->interworking) | |
241 | return eid; | |
242 | ||
243 | *pos++ = WLAN_EID_INTERWORKING; | |
244 | len = pos++; | |
245 | ||
246 | *pos = hapd->conf->access_network_type; | |
247 | if (hapd->conf->internet) | |
248 | *pos |= INTERWORKING_ANO_INTERNET; | |
249 | if (hapd->conf->asra) | |
250 | *pos |= INTERWORKING_ANO_ASRA; | |
251 | if (hapd->conf->esr) | |
252 | *pos |= INTERWORKING_ANO_ESR; | |
253 | if (hapd->conf->uesa) | |
254 | *pos |= INTERWORKING_ANO_UESA; | |
255 | pos++; | |
256 | ||
257 | if (hapd->conf->venue_info_set) { | |
258 | *pos++ = hapd->conf->venue_group; | |
259 | *pos++ = hapd->conf->venue_type; | |
260 | } | |
261 | ||
262 | if (!is_zero_ether_addr(hapd->conf->hessid)) { | |
263 | os_memcpy(pos, hapd->conf->hessid, ETH_ALEN); | |
264 | pos += ETH_ALEN; | |
265 | } | |
266 | ||
267 | *len = pos - len - 1; | |
268 | #endif /* CONFIG_INTERWORKING */ | |
269 | ||
270 | return pos; | |
271 | } | |
c7c178e1 JM |
272 | |
273 | ||
274 | u8 * hostapd_eid_adv_proto(struct hostapd_data *hapd, u8 *eid) | |
275 | { | |
276 | u8 *pos = eid; | |
277 | #ifdef CONFIG_INTERWORKING | |
278 | ||
279 | /* TODO: Separate configuration for ANQP? */ | |
280 | if (!hapd->conf->interworking) | |
281 | return eid; | |
282 | ||
283 | *pos++ = WLAN_EID_ADV_PROTO; | |
284 | *pos++ = 2; | |
1d21e9dd | 285 | *pos++ = 0x7F; /* Query Response Length Limit | PAME-BI */ |
c7c178e1 JM |
286 | *pos++ = ACCESS_NETWORK_QUERY_PROTOCOL; |
287 | #endif /* CONFIG_INTERWORKING */ | |
288 | ||
289 | return pos; | |
290 | } | |
4b2a77ab JM |
291 | |
292 | ||
293 | u8 * hostapd_eid_roaming_consortium(struct hostapd_data *hapd, u8 *eid) | |
294 | { | |
295 | u8 *pos = eid; | |
296 | #ifdef CONFIG_INTERWORKING | |
297 | u8 *len; | |
298 | unsigned int i, count; | |
299 | ||
300 | if (!hapd->conf->interworking || | |
301 | hapd->conf->roaming_consortium == NULL || | |
302 | hapd->conf->roaming_consortium_count == 0) | |
303 | return eid; | |
304 | ||
305 | *pos++ = WLAN_EID_ROAMING_CONSORTIUM; | |
306 | len = pos++; | |
307 | ||
308 | /* Number of ANQP OIs (in addition to the max 3 listed here) */ | |
309 | if (hapd->conf->roaming_consortium_count > 3 + 255) | |
310 | *pos++ = 255; | |
311 | else if (hapd->conf->roaming_consortium_count > 3) | |
312 | *pos++ = hapd->conf->roaming_consortium_count - 3; | |
313 | else | |
314 | *pos++ = 0; | |
315 | ||
316 | /* OU #1 and #2 Lengths */ | |
317 | *pos = hapd->conf->roaming_consortium[0].len; | |
318 | if (hapd->conf->roaming_consortium_count > 1) | |
319 | *pos |= hapd->conf->roaming_consortium[1].len << 4; | |
320 | pos++; | |
321 | ||
322 | if (hapd->conf->roaming_consortium_count > 3) | |
323 | count = 3; | |
324 | else | |
325 | count = hapd->conf->roaming_consortium_count; | |
326 | ||
327 | for (i = 0; i < count; i++) { | |
328 | os_memcpy(pos, hapd->conf->roaming_consortium[i].oi, | |
329 | hapd->conf->roaming_consortium[i].len); | |
330 | pos += hapd->conf->roaming_consortium[i].len; | |
331 | } | |
332 | ||
333 | *len = pos - len - 1; | |
334 | #endif /* CONFIG_INTERWORKING */ | |
335 | ||
336 | return pos; | |
337 | } | |
39b97072 JM |
338 | |
339 | ||
340 | u8 * hostapd_eid_time_adv(struct hostapd_data *hapd, u8 *eid) | |
341 | { | |
342 | if (hapd->conf->time_advertisement != 2) | |
343 | return eid; | |
344 | ||
345 | if (hapd->time_adv == NULL && | |
346 | hostapd_update_time_adv(hapd) < 0) | |
347 | return eid; | |
348 | ||
4c8a333b JM |
349 | if (hapd->time_adv == NULL) |
350 | return eid; | |
351 | ||
39b97072 JM |
352 | os_memcpy(eid, wpabuf_head(hapd->time_adv), |
353 | wpabuf_len(hapd->time_adv)); | |
354 | eid += wpabuf_len(hapd->time_adv); | |
355 | ||
356 | return eid; | |
357 | } | |
358 | ||
359 | ||
360 | u8 * hostapd_eid_time_zone(struct hostapd_data *hapd, u8 *eid) | |
361 | { | |
362 | size_t len; | |
363 | ||
364 | if (hapd->conf->time_advertisement != 2) | |
365 | return eid; | |
366 | ||
367 | len = os_strlen(hapd->conf->time_zone); | |
368 | ||
369 | *eid++ = WLAN_EID_TIME_ZONE; | |
370 | *eid++ = len; | |
371 | os_memcpy(eid, hapd->conf->time_zone, len); | |
372 | eid += len; | |
373 | ||
374 | return eid; | |
375 | } | |
376 | ||
377 | ||
378 | int hostapd_update_time_adv(struct hostapd_data *hapd) | |
379 | { | |
380 | const int elen = 2 + 1 + 10 + 5 + 1; | |
381 | struct os_time t; | |
382 | struct os_tm tm; | |
383 | u8 *pos; | |
384 | ||
385 | if (hapd->conf->time_advertisement != 2) | |
386 | return 0; | |
387 | ||
388 | if (os_get_time(&t) < 0 || os_gmtime(t.sec, &tm) < 0) | |
389 | return -1; | |
390 | ||
391 | if (!hapd->time_adv) { | |
392 | hapd->time_adv = wpabuf_alloc(elen); | |
393 | if (hapd->time_adv == NULL) | |
394 | return -1; | |
395 | pos = wpabuf_put(hapd->time_adv, elen); | |
396 | } else | |
397 | pos = wpabuf_mhead_u8(hapd->time_adv); | |
398 | ||
399 | *pos++ = WLAN_EID_TIME_ADVERTISEMENT; | |
400 | *pos++ = 1 + 10 + 5 + 1; | |
401 | ||
402 | *pos++ = 2; /* UTC time at which the TSF timer is 0 */ | |
403 | ||
404 | /* Time Value at TSF 0 */ | |
405 | /* FIX: need to calculate this based on the current TSF value */ | |
406 | WPA_PUT_LE16(pos, tm.year); /* Year */ | |
407 | pos += 2; | |
408 | *pos++ = tm.month; /* Month */ | |
409 | *pos++ = tm.day; /* Day of month */ | |
410 | *pos++ = tm.hour; /* Hours */ | |
411 | *pos++ = tm.min; /* Minutes */ | |
412 | *pos++ = tm.sec; /* Seconds */ | |
413 | WPA_PUT_LE16(pos, 0); /* Milliseconds (not used) */ | |
414 | pos += 2; | |
415 | *pos++ = 0; /* Reserved */ | |
416 | ||
417 | /* Time Error */ | |
418 | /* TODO: fill in an estimate on the error */ | |
419 | *pos++ = 0; | |
420 | *pos++ = 0; | |
421 | *pos++ = 0; | |
422 | *pos++ = 0; | |
423 | *pos++ = 0; | |
424 | ||
425 | *pos++ = hapd->time_update_counter++; | |
426 | ||
427 | return 0; | |
428 | } | |
b6668734 JM |
429 | |
430 | ||
431 | u8 * hostapd_eid_bss_max_idle_period(struct hostapd_data *hapd, u8 *eid) | |
432 | { | |
433 | u8 *pos = eid; | |
434 | ||
435 | #ifdef CONFIG_WNM | |
436 | if (hapd->conf->ap_max_inactivity > 0) { | |
437 | unsigned int val; | |
438 | *pos++ = WLAN_EID_BSS_MAX_IDLE_PERIOD; | |
439 | *pos++ = 3; | |
440 | val = hapd->conf->ap_max_inactivity; | |
441 | if (val > 68000) | |
442 | val = 68000; | |
443 | val *= 1000; | |
444 | val /= 1024; | |
445 | if (val == 0) | |
446 | val = 1; | |
447 | if (val > 65535) | |
448 | val = 65535; | |
449 | WPA_PUT_LE16(pos, val); | |
450 | pos += 2; | |
451 | *pos++ = 0x00; /* TODO: Protected Keep-Alive Required */ | |
452 | } | |
453 | #endif /* CONFIG_WNM */ | |
454 | ||
455 | return pos; | |
456 | } |