1 /* 2 * hostapd / IEEE 802.11 Management 3 * Copyright (c) 2002-2012, Jouni Malinen <j (at) 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 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; 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 76 static 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 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 */ 165 166 167 static void hostapd_ext_capab_byte(struct hostapd_data *hapd, u8 *pos, int idx) 168 { 169 *pos = 0x00; 170 171 switch (idx) { 172 case 0: /* Bits 0-7 */ 173 break; 174 case 1: /* Bits 8-15 */ 175 break; 176 case 2: /* Bits 16-23 */ 177 if (hapd->conf->wnm_sleep_mode) 178 *pos |= 0x02; /* Bit 17 - WNM-Sleep Mode */ 179 if (hapd->conf->bss_transition) 180 *pos |= 0x08; /* Bit 19 - BSS Transition */ 181 break; 182 case 3: /* Bits 24-31 */ 183 #ifdef CONFIG_WNM 184 *pos |= 0x02; /* Bit 25 - SSID List */ 185 #endif /* CONFIG_WNM */ 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 break; 191 case 4: /* Bits 32-39 */ 192 if (hapd->conf->tdls & TDLS_PROHIBIT) 193 *pos |= 0x40; /* Bit 38 - TDLS Prohibited */ 194 if (hapd->conf->tdls & TDLS_PROHIBIT_CHAN_SWITCH) { 195 /* Bit 39 - TDLS Channel Switching Prohibited */ 196 *pos |= 0x80; 197 } 198 break; 199 case 5: /* Bits 40-47 */ 200 break; 201 case 6: /* Bits 48-55 */ 202 if (hapd->conf->ssid.utf8_ssid) 203 *pos |= 0x01; /* Bit 48 - UTF-8 SSID */ 204 break; 205 } 206 } 207 208 209 u8 * hostapd_eid_ext_capab(struct hostapd_data *hapd, u8 *eid) 210 { 211 u8 *pos = eid; 212 u8 len = 0, i; 213 214 if (hapd->conf->tdls & (TDLS_PROHIBIT | TDLS_PROHIBIT_CHAN_SWITCH)) 215 len = 5; 216 if (len < 4 && hapd->conf->interworking) 217 len = 4; 218 if (len < 3 && hapd->conf->wnm_sleep_mode) 219 len = 3; 220 if (len < 7 && hapd->conf->ssid.utf8_ssid) 221 len = 7; 222 #ifdef CONFIG_WNM 223 if (len < 4) 224 len = 4; 225 #endif /* CONFIG_WNM */ 226 if (len < hapd->iface->extended_capa_len) 227 len = hapd->iface->extended_capa_len; 228 if (len == 0) 229 return eid; 230 231 *pos++ = WLAN_EID_EXT_CAPAB; 232 *pos++ = len; 233 for (i = 0; i < len; i++, pos++) { 234 hostapd_ext_capab_byte(hapd, pos, i); 235 236 if (i < hapd->iface->extended_capa_len) { 237 *pos &= ~hapd->iface->extended_capa_mask[i]; 238 *pos |= hapd->iface->extended_capa[i]; 239 } 240 } 241 242 while (len > 0 && eid[1 + len] == 0) { 243 len--; 244 eid[1] = len; 245 } 246 if (len == 0) 247 return eid; 248 249 return eid + 2 + len; 250 } 251 252 253 u8 * hostapd_eid_interworking(struct hostapd_data *hapd, u8 *eid) 254 { 255 u8 *pos = eid; 256 #ifdef CONFIG_INTERWORKING 257 u8 *len; 258 259 if (!hapd->conf->interworking) 260 return eid; 261 262 *pos++ = WLAN_EID_INTERWORKING; 263 len = pos++; 264 265 *pos = hapd->conf->access_network_type; 266 if (hapd->conf->internet) 267 *pos |= INTERWORKING_ANO_INTERNET; 268 if (hapd->conf->asra) 269 *pos |= INTERWORKING_ANO_ASRA; 270 if (hapd->conf->esr) 271 *pos |= INTERWORKING_ANO_ESR; 272 if (hapd->conf->uesa) 273 *pos |= INTERWORKING_ANO_UESA; 274 pos++; 275 276 if (hapd->conf->venue_info_set) { 277 *pos++ = hapd->conf->venue_group; 278 *pos++ = hapd->conf->venue_type; 279 } 280 281 if (!is_zero_ether_addr(hapd->conf->hessid)) { 282 os_memcpy(pos, hapd->conf->hessid, ETH_ALEN); 283 pos += ETH_ALEN; 284 } 285 286 *len = pos - len - 1; 287 #endif /* CONFIG_INTERWORKING */ 288 289 return pos; 290 } 291 292 293 u8 * hostapd_eid_adv_proto(struct hostapd_data *hapd, u8 *eid) 294 { 295 u8 *pos = eid; 296 #ifdef CONFIG_INTERWORKING 297 298 /* TODO: Separate configuration for ANQP? */ 299 if (!hapd->conf->interworking) 300 return eid; 301 302 *pos++ = WLAN_EID_ADV_PROTO; 303 *pos++ = 2; 304 *pos++ = 0x7F; /* Query Response Length Limit | PAME-BI */ 305 *pos++ = ACCESS_NETWORK_QUERY_PROTOCOL; 306 #endif /* CONFIG_INTERWORKING */ 307 308 return pos; 309 } 310 311 312 u8 * hostapd_eid_roaming_consortium(struct hostapd_data *hapd, u8 *eid) 313 { 314 u8 *pos = eid; 315 #ifdef CONFIG_INTERWORKING 316 u8 *len; 317 unsigned int i, count; 318 319 if (!hapd->conf->interworking || 320 hapd->conf->roaming_consortium == NULL || 321 hapd->conf->roaming_consortium_count == 0) 322 return eid; 323 324 *pos++ = WLAN_EID_ROAMING_CONSORTIUM; 325 len = pos++; 326 327 /* Number of ANQP OIs (in addition to the max 3 listed here) */ 328 if (hapd->conf->roaming_consortium_count > 3 + 255) 329 *pos++ = 255; 330 else if (hapd->conf->roaming_consortium_count > 3) 331 *pos++ = hapd->conf->roaming_consortium_count - 3; 332 else 333 *pos++ = 0; 334 335 /* OU #1 and #2 Lengths */ 336 *pos = hapd->conf->roaming_consortium[0].len; 337 if (hapd->conf->roaming_consortium_count > 1) 338 *pos |= hapd->conf->roaming_consortium[1].len << 4; 339 pos++; 340 341 if (hapd->conf->roaming_consortium_count > 3) 342 count = 3; 343 else 344 count = hapd->conf->roaming_consortium_count; 345 346 for (i = 0; i < count; i++) { 347 os_memcpy(pos, hapd->conf->roaming_consortium[i].oi, 348 hapd->conf->roaming_consortium[i].len); 349 pos += hapd->conf->roaming_consortium[i].len; 350 } 351 352 *len = pos - len - 1; 353 #endif /* CONFIG_INTERWORKING */ 354 355 return pos; 356 } 357 358 359 u8 * hostapd_eid_time_adv(struct hostapd_data *hapd, u8 *eid) 360 { 361 if (hapd->conf->time_advertisement != 2) 362 return eid; 363 364 if (hapd->time_adv == NULL && 365 hostapd_update_time_adv(hapd) < 0) 366 return eid; 367 368 if (hapd->time_adv == NULL) 369 return eid; 370 371 os_memcpy(eid, wpabuf_head(hapd->time_adv), 372 wpabuf_len(hapd->time_adv)); 373 eid += wpabuf_len(hapd->time_adv); 374 375 return eid; 376 } 377 378 379 u8 * hostapd_eid_time_zone(struct hostapd_data *hapd, u8 *eid) 380 { 381 size_t len; 382 383 if (hapd->conf->time_advertisement != 2) 384 return eid; 385 386 len = os_strlen(hapd->conf->time_zone); 387 388 *eid++ = WLAN_EID_TIME_ZONE; 389 *eid++ = len; 390 os_memcpy(eid, hapd->conf->time_zone, len); 391 eid += len; 392 393 return eid; 394 } 395 396 397 int hostapd_update_time_adv(struct hostapd_data *hapd) 398 { 399 const int elen = 2 + 1 + 10 + 5 + 1; 400 struct os_time t; 401 struct os_tm tm; 402 u8 *pos; 403 404 if (hapd->conf->time_advertisement != 2) 405 return 0; 406 407 if (os_get_time(&t) < 0 || os_gmtime(t.sec, &tm) < 0) 408 return -1; 409 410 if (!hapd->time_adv) { 411 hapd->time_adv = wpabuf_alloc(elen); 412 if (hapd->time_adv == NULL) 413 return -1; 414 pos = wpabuf_put(hapd->time_adv, elen); 415 } else 416 pos = wpabuf_mhead_u8(hapd->time_adv); 417 418 *pos++ = WLAN_EID_TIME_ADVERTISEMENT; 419 *pos++ = 1 + 10 + 5 + 1; 420 421 *pos++ = 2; /* UTC time at which the TSF timer is 0 */ 422 423 /* Time Value at TSF 0 */ 424 /* FIX: need to calculate this based on the current TSF value */ 425 WPA_PUT_LE16(pos, tm.year); /* Year */ 426 pos += 2; 427 *pos++ = tm.month; /* Month */ 428 *pos++ = tm.day; /* Day of month */ 429 *pos++ = tm.hour; /* Hours */ 430 *pos++ = tm.min; /* Minutes */ 431 *pos++ = tm.sec; /* Seconds */ 432 WPA_PUT_LE16(pos, 0); /* Milliseconds (not used) */ 433 pos += 2; 434 *pos++ = 0; /* Reserved */ 435 436 /* Time Error */ 437 /* TODO: fill in an estimate on the error */ 438 *pos++ = 0; 439 *pos++ = 0; 440 *pos++ = 0; 441 *pos++ = 0; 442 *pos++ = 0; 443 444 *pos++ = hapd->time_update_counter++; 445 446 return 0; 447 } 448 449 450 u8 * hostapd_eid_bss_max_idle_period(struct hostapd_data *hapd, u8 *eid) 451 { 452 u8 *pos = eid; 453 454 #ifdef CONFIG_WNM 455 if (hapd->conf->ap_max_inactivity > 0) { 456 unsigned int val; 457 *pos++ = WLAN_EID_BSS_MAX_IDLE_PERIOD; 458 *pos++ = 3; 459 val = hapd->conf->ap_max_inactivity; 460 if (val > 68000) 461 val = 68000; 462 val *= 1000; 463 val /= 1024; 464 if (val == 0) 465 val = 1; 466 if (val > 65535) 467 val = 65535; 468 WPA_PUT_LE16(pos, val); 469 pos += 2; 470 *pos++ = 0x00; /* TODO: Protected Keep-Alive Required */ 471 } 472 #endif /* CONFIG_WNM */ 473 474 return pos; 475 } 476