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