1 /* 2 * Control interface for shared AP commands 3 * Copyright (c) 2004-2014, 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 "common/sae.h" 14 #include "eapol_auth/eapol_auth_sm.h" 15 #include "hostapd.h" 16 #include "ieee802_1x.h" 17 #include "wpa_auth.h" 18 #include "ieee802_11.h" 19 #include "sta_info.h" 20 #include "wps_hostapd.h" 21 #include "p2p_hostapd.h" 22 #include "ctrl_iface_ap.h" 23 #include "ap_drv_ops.h" 24 25 26 static int hostapd_get_sta_tx_rx(struct hostapd_data *hapd, 27 struct sta_info *sta, 28 char *buf, size_t buflen) 29 { 30 struct hostap_sta_driver_data data; 31 int ret; 32 33 if (hostapd_drv_read_sta_data(hapd, &data, sta->addr) < 0) 34 return 0; 35 36 ret = os_snprintf(buf, buflen, "rx_packets=%lu\ntx_packets=%lu\n" 37 "rx_bytes=%lu\ntx_bytes=%lu\n", 38 data.rx_packets, data.tx_packets, 39 data.rx_bytes, data.tx_bytes); 40 if (os_snprintf_error(buflen, ret)) 41 return 0; 42 return ret; 43 } 44 45 46 static int hostapd_get_sta_conn_time(struct sta_info *sta, 47 char *buf, size_t buflen) 48 { 49 struct os_reltime age; 50 int ret; 51 52 if (!sta->connected_time.sec) 53 return 0; 54 55 os_reltime_age(&sta->connected_time, &age); 56 57 ret = os_snprintf(buf, buflen, "connected_time=%u\n", 58 (unsigned int) age.sec); 59 if (os_snprintf_error(buflen, ret)) 60 return 0; 61 return ret; 62 } 63 64 65 static const char * timeout_next_str(int val) 66 { 67 switch (val) { 68 case STA_NULLFUNC: 69 return "NULLFUNC POLL"; 70 case STA_DISASSOC: 71 return "DISASSOC"; 72 case STA_DEAUTH: 73 return "DEAUTH"; 74 case STA_REMOVE: 75 return "REMOVE"; 76 case STA_DISASSOC_FROM_CLI: 77 return "DISASSOC_FROM_CLI"; 78 } 79 80 return "?"; 81 } 82 83 84 static int hostapd_ctrl_iface_sta_mib(struct hostapd_data *hapd, 85 struct sta_info *sta, 86 char *buf, size_t buflen) 87 { 88 int len, res, ret, i; 89 90 if (!sta) 91 return 0; 92 93 len = 0; 94 ret = os_snprintf(buf + len, buflen - len, MACSTR "\nflags=", 95 MAC2STR(sta->addr)); 96 if (os_snprintf_error(buflen - len, ret)) 97 return len; 98 len += ret; 99 100 ret = ap_sta_flags_txt(sta->flags, buf + len, buflen - len); 101 if (ret < 0) 102 return len; 103 len += ret; 104 105 ret = os_snprintf(buf + len, buflen - len, "\naid=%d\ncapability=0x%x\n" 106 "listen_interval=%d\nsupported_rates=", 107 sta->aid, sta->capability, sta->listen_interval); 108 if (os_snprintf_error(buflen - len, ret)) 109 return len; 110 len += ret; 111 112 for (i = 0; i < sta->supported_rates_len; i++) { 113 ret = os_snprintf(buf + len, buflen - len, "%02x%s", 114 sta->supported_rates[i], 115 i + 1 < sta->supported_rates_len ? " " : ""); 116 if (os_snprintf_error(buflen - len, ret)) 117 return len; 118 len += ret; 119 } 120 121 ret = os_snprintf(buf + len, buflen - len, "\ntimeout_next=%s\n", 122 timeout_next_str(sta->timeout_next)); 123 if (os_snprintf_error(buflen - len, ret)) 124 return len; 125 len += ret; 126 127 res = ieee802_11_get_mib_sta(hapd, sta, buf + len, buflen - len); 128 if (res >= 0) 129 len += res; 130 res = wpa_get_mib_sta(sta->wpa_sm, buf + len, buflen - len); 131 if (res >= 0) 132 len += res; 133 res = ieee802_1x_get_mib_sta(hapd, sta, buf + len, buflen - len); 134 if (res >= 0) 135 len += res; 136 res = hostapd_wps_get_mib_sta(hapd, sta->addr, buf + len, 137 buflen - len); 138 if (res >= 0) 139 len += res; 140 res = hostapd_p2p_get_mib_sta(hapd, sta, buf + len, buflen - len); 141 if (res >= 0) 142 len += res; 143 144 len += hostapd_get_sta_tx_rx(hapd, sta, buf + len, buflen - len); 145 len += hostapd_get_sta_conn_time(sta, buf + len, buflen - len); 146 147 #ifdef CONFIG_SAE 148 if (sta->sae && sta->sae->state == SAE_ACCEPTED) { 149 res = os_snprintf(buf + len, buflen - len, "sae_group=%d\n", 150 sta->sae->group); 151 if (!os_snprintf_error(buflen - len, res)) 152 len += res; 153 } 154 #endif /* CONFIG_SAE */ 155 156 return len; 157 } 158 159 160 int hostapd_ctrl_iface_sta_first(struct hostapd_data *hapd, 161 char *buf, size_t buflen) 162 { 163 return hostapd_ctrl_iface_sta_mib(hapd, hapd->sta_list, buf, buflen); 164 } 165 166 167 int hostapd_ctrl_iface_sta(struct hostapd_data *hapd, const char *txtaddr, 168 char *buf, size_t buflen) 169 { 170 u8 addr[ETH_ALEN]; 171 int ret; 172 const char *pos; 173 struct sta_info *sta; 174 175 if (hwaddr_aton(txtaddr, addr)) { 176 ret = os_snprintf(buf, buflen, "FAIL\n"); 177 if (os_snprintf_error(buflen, ret)) 178 return 0; 179 return ret; 180 } 181 182 sta = ap_get_sta(hapd, addr); 183 if (sta == NULL) 184 return -1; 185 186 pos = os_strchr(txtaddr, ' '); 187 if (pos) { 188 pos++; 189 190 #ifdef HOSTAPD_DUMP_STATE 191 if (os_strcmp(pos, "eapol") == 0) { 192 if (sta->eapol_sm == NULL) 193 return -1; 194 return eapol_auth_dump_state(sta->eapol_sm, buf, 195 buflen); 196 } 197 #endif /* HOSTAPD_DUMP_STATE */ 198 199 return -1; 200 } 201 202 return hostapd_ctrl_iface_sta_mib(hapd, sta, buf, buflen); 203 } 204 205 206 int hostapd_ctrl_iface_sta_next(struct hostapd_data *hapd, const char *txtaddr, 207 char *buf, size_t buflen) 208 { 209 u8 addr[ETH_ALEN]; 210 struct sta_info *sta; 211 int ret; 212 213 if (hwaddr_aton(txtaddr, addr) || 214 (sta = ap_get_sta(hapd, addr)) == NULL) { 215 ret = os_snprintf(buf, buflen, "FAIL\n"); 216 if (os_snprintf_error(buflen, ret)) 217 return 0; 218 return ret; 219 } 220 221 if (!sta->next) 222 return 0; 223 224 return hostapd_ctrl_iface_sta_mib(hapd, sta->next, buf, buflen); 225 } 226 227 228 #ifdef CONFIG_P2P_MANAGER 229 static int p2p_manager_disconnect(struct hostapd_data *hapd, u16 stype, 230 u8 minor_reason_code, const u8 *addr) 231 { 232 struct ieee80211_mgmt *mgmt; 233 int ret; 234 u8 *pos; 235 236 if (hapd->driver->send_frame == NULL) 237 return -1; 238 239 mgmt = os_zalloc(sizeof(*mgmt) + 100); 240 if (mgmt == NULL) 241 return -1; 242 243 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, stype); 244 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "P2P: Disconnect STA " MACSTR 245 " with minor reason code %u (stype=%u (%s))", 246 MAC2STR(addr), minor_reason_code, stype, 247 fc2str(mgmt->frame_control)); 248 249 os_memcpy(mgmt->da, addr, ETH_ALEN); 250 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN); 251 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN); 252 if (stype == WLAN_FC_STYPE_DEAUTH) { 253 mgmt->u.deauth.reason_code = 254 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID); 255 pos = (u8 *) (&mgmt->u.deauth.reason_code + 1); 256 } else { 257 mgmt->u.disassoc.reason_code = 258 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID); 259 pos = (u8 *) (&mgmt->u.disassoc.reason_code + 1); 260 } 261 262 *pos++ = WLAN_EID_VENDOR_SPECIFIC; 263 *pos++ = 4 + 3 + 1; 264 WPA_PUT_BE32(pos, P2P_IE_VENDOR_TYPE); 265 pos += 4; 266 267 *pos++ = P2P_ATTR_MINOR_REASON_CODE; 268 WPA_PUT_LE16(pos, 1); 269 pos += 2; 270 *pos++ = minor_reason_code; 271 272 ret = hapd->driver->send_frame(hapd->drv_priv, (u8 *) mgmt, 273 pos - (u8 *) mgmt, 1); 274 os_free(mgmt); 275 276 return ret < 0 ? -1 : 0; 277 } 278 #endif /* CONFIG_P2P_MANAGER */ 279 280 281 int hostapd_ctrl_iface_deauthenticate(struct hostapd_data *hapd, 282 const char *txtaddr) 283 { 284 u8 addr[ETH_ALEN]; 285 struct sta_info *sta; 286 const char *pos; 287 u16 reason = WLAN_REASON_PREV_AUTH_NOT_VALID; 288 289 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DEAUTHENTICATE %s", 290 txtaddr); 291 292 if (hwaddr_aton(txtaddr, addr)) 293 return -1; 294 295 pos = os_strstr(txtaddr, " reason="); 296 if (pos) 297 reason = atoi(pos + 8); 298 299 pos = os_strstr(txtaddr, " test="); 300 if (pos) { 301 struct ieee80211_mgmt mgmt; 302 int encrypt; 303 if (hapd->driver->send_frame == NULL) 304 return -1; 305 pos += 6; 306 encrypt = atoi(pos); 307 os_memset(&mgmt, 0, sizeof(mgmt)); 308 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, 309 WLAN_FC_STYPE_DEAUTH); 310 os_memcpy(mgmt.da, addr, ETH_ALEN); 311 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN); 312 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN); 313 mgmt.u.deauth.reason_code = host_to_le16(reason); 314 if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt, 315 IEEE80211_HDRLEN + 316 sizeof(mgmt.u.deauth), 317 encrypt) < 0) 318 return -1; 319 return 0; 320 } 321 322 #ifdef CONFIG_P2P_MANAGER 323 pos = os_strstr(txtaddr, " p2p="); 324 if (pos) { 325 return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DEAUTH, 326 atoi(pos + 5), addr); 327 } 328 #endif /* CONFIG_P2P_MANAGER */ 329 330 hostapd_drv_sta_deauth(hapd, addr, reason); 331 sta = ap_get_sta(hapd, addr); 332 if (sta) 333 ap_sta_deauthenticate(hapd, sta, reason); 334 else if (addr[0] == 0xff) 335 hostapd_free_stas(hapd); 336 337 return 0; 338 } 339 340 341 int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd, 342 const char *txtaddr) 343 { 344 u8 addr[ETH_ALEN]; 345 struct sta_info *sta; 346 const char *pos; 347 u16 reason = WLAN_REASON_PREV_AUTH_NOT_VALID; 348 349 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DISASSOCIATE %s", 350 txtaddr); 351 352 if (hwaddr_aton(txtaddr, addr)) 353 return -1; 354 355 pos = os_strstr(txtaddr, " reason="); 356 if (pos) 357 reason = atoi(pos + 8); 358 359 pos = os_strstr(txtaddr, " test="); 360 if (pos) { 361 struct ieee80211_mgmt mgmt; 362 int encrypt; 363 if (hapd->driver->send_frame == NULL) 364 return -1; 365 pos += 6; 366 encrypt = atoi(pos); 367 os_memset(&mgmt, 0, sizeof(mgmt)); 368 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, 369 WLAN_FC_STYPE_DISASSOC); 370 os_memcpy(mgmt.da, addr, ETH_ALEN); 371 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN); 372 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN); 373 mgmt.u.disassoc.reason_code = host_to_le16(reason); 374 if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt, 375 IEEE80211_HDRLEN + 376 sizeof(mgmt.u.deauth), 377 encrypt) < 0) 378 return -1; 379 return 0; 380 } 381 382 #ifdef CONFIG_P2P_MANAGER 383 pos = os_strstr(txtaddr, " p2p="); 384 if (pos) { 385 return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DISASSOC, 386 atoi(pos + 5), addr); 387 } 388 #endif /* CONFIG_P2P_MANAGER */ 389 390 hostapd_drv_sta_disassoc(hapd, addr, reason); 391 sta = ap_get_sta(hapd, addr); 392 if (sta) 393 ap_sta_disassociate(hapd, sta, reason); 394 else if (addr[0] == 0xff) 395 hostapd_free_stas(hapd); 396 397 return 0; 398 } 399 400 401 int hostapd_ctrl_iface_status(struct hostapd_data *hapd, char *buf, 402 size_t buflen) 403 { 404 struct hostapd_iface *iface = hapd->iface; 405 int len = 0, ret; 406 size_t i; 407 408 ret = os_snprintf(buf + len, buflen - len, 409 "state=%s\n" 410 "phy=%s\n" 411 "freq=%d\n" 412 "num_sta_non_erp=%d\n" 413 "num_sta_no_short_slot_time=%d\n" 414 "num_sta_no_short_preamble=%d\n" 415 "olbc=%d\n" 416 "num_sta_ht_no_gf=%d\n" 417 "num_sta_no_ht=%d\n" 418 "num_sta_ht_20_mhz=%d\n" 419 "num_sta_ht40_intolerant=%d\n" 420 "olbc_ht=%d\n" 421 "ht_op_mode=0x%x\n", 422 hostapd_state_text(iface->state), 423 iface->phy, 424 iface->freq, 425 iface->num_sta_non_erp, 426 iface->num_sta_no_short_slot_time, 427 iface->num_sta_no_short_preamble, 428 iface->olbc, 429 iface->num_sta_ht_no_gf, 430 iface->num_sta_no_ht, 431 iface->num_sta_ht_20mhz, 432 iface->num_sta_ht40_intolerant, 433 iface->olbc_ht, 434 iface->ht_op_mode); 435 if (os_snprintf_error(buflen - len, ret)) 436 return len; 437 len += ret; 438 439 if (!iface->cac_started || !iface->dfs_cac_ms) { 440 ret = os_snprintf(buf + len, buflen - len, 441 "cac_time_seconds=%d\n" 442 "cac_time_left_seconds=N/A\n", 443 iface->dfs_cac_ms / 1000); 444 } else { 445 /* CAC started and CAC time set - calculate remaining time */ 446 struct os_reltime now; 447 unsigned int left_time; 448 449 os_reltime_age(&iface->dfs_cac_start, &now); 450 left_time = iface->dfs_cac_ms / 1000 - now.sec; 451 ret = os_snprintf(buf + len, buflen - len, 452 "cac_time_seconds=%u\n" 453 "cac_time_left_seconds=%u\n", 454 iface->dfs_cac_ms / 1000, 455 left_time); 456 } 457 if (os_snprintf_error(buflen - len, ret)) 458 return len; 459 len += ret; 460 461 ret = os_snprintf(buf + len, buflen - len, 462 "channel=%u\n" 463 "secondary_channel=%d\n" 464 "ieee80211n=%d\n" 465 "ieee80211ac=%d\n" 466 "vht_oper_chwidth=%d\n" 467 "vht_oper_centr_freq_seg0_idx=%d\n" 468 "vht_oper_centr_freq_seg1_idx=%d\n", 469 iface->conf->channel, 470 iface->conf->secondary_channel, 471 iface->conf->ieee80211n, 472 iface->conf->ieee80211ac, 473 iface->conf->vht_oper_chwidth, 474 iface->conf->vht_oper_centr_freq_seg0_idx, 475 iface->conf->vht_oper_centr_freq_seg1_idx); 476 if (os_snprintf_error(buflen - len, ret)) 477 return len; 478 len += ret; 479 480 for (i = 0; i < iface->num_bss; i++) { 481 struct hostapd_data *bss = iface->bss[i]; 482 ret = os_snprintf(buf + len, buflen - len, 483 "bss[%d]=%s\n" 484 "bssid[%d]=" MACSTR "\n" 485 "ssid[%d]=%s\n" 486 "num_sta[%d]=%d\n", 487 (int) i, bss->conf->iface, 488 (int) i, MAC2STR(bss->own_addr), 489 (int) i, 490 wpa_ssid_txt(bss->conf->ssid.ssid, 491 bss->conf->ssid.ssid_len), 492 (int) i, bss->num_sta); 493 if (os_snprintf_error(buflen - len, ret)) 494 return len; 495 len += ret; 496 } 497 498 return len; 499 } 500 501 502 int hostapd_parse_csa_settings(const char *pos, 503 struct csa_settings *settings) 504 { 505 char *end; 506 507 os_memset(settings, 0, sizeof(*settings)); 508 settings->cs_count = strtol(pos, &end, 10); 509 if (pos == end) { 510 wpa_printf(MSG_ERROR, "chanswitch: invalid cs_count provided"); 511 return -1; 512 } 513 514 settings->freq_params.freq = atoi(end); 515 if (settings->freq_params.freq == 0) { 516 wpa_printf(MSG_ERROR, "chanswitch: invalid freq provided"); 517 return -1; 518 } 519 520 #define SET_CSA_SETTING(str) \ 521 do { \ 522 const char *pos2 = os_strstr(pos, " " #str "="); \ 523 if (pos2) { \ 524 pos2 += sizeof(" " #str "=") - 1; \ 525 settings->freq_params.str = atoi(pos2); \ 526 } \ 527 } while (0) 528 529 SET_CSA_SETTING(center_freq1); 530 SET_CSA_SETTING(center_freq2); 531 SET_CSA_SETTING(bandwidth); 532 SET_CSA_SETTING(sec_channel_offset); 533 settings->freq_params.ht_enabled = !!os_strstr(pos, " ht"); 534 settings->freq_params.vht_enabled = !!os_strstr(pos, " vht"); 535 settings->block_tx = !!os_strstr(pos, " blocktx"); 536 #undef SET_CSA_SETTING 537 538 return 0; 539 } 540 541 542 int hostapd_ctrl_iface_stop_ap(struct hostapd_data *hapd) 543 { 544 return hostapd_drv_stop_ap(hapd); 545 } 546