2 * Control interface for shared AP commands
3 * Copyright (c) 2004-2014, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
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"
16 #include "ieee802_1x.h"
18 #include "ieee802_11.h"
20 #include "wps_hostapd.h"
21 #include "p2p_hostapd.h"
22 #include "ctrl_iface_ap.h"
23 #include "ap_drv_ops.h"
26 static int hostapd_get_sta_tx_rx(struct hostapd_data *hapd,
28 char *buf, size_t buflen)
30 struct hostap_sta_driver_data data;
33 if (hostapd_drv_read_sta_data(hapd, &data, sta->addr) < 0)
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))
46 static int hostapd_get_sta_conn_time(struct sta_info *sta,
47 char *buf, size_t buflen)
49 struct os_reltime age;
52 if (!sta->connected_time.sec)
55 os_reltime_age(&sta->connected_time, &age);
57 ret = os_snprintf(buf, buflen, "connected_time=%u\n",
58 (unsigned int) age.sec);
59 if (os_snprintf_error(buflen, ret))
65 static const char * timeout_next_str(int val)
69 return "NULLFUNC POLL";
76 case STA_DISASSOC_FROM_CLI:
77 return "DISASSOC_FROM_CLI";
84 static int hostapd_ctrl_iface_sta_mib(struct hostapd_data *hapd,
86 char *buf, size_t buflen)
94 ret = os_snprintf(buf + len, buflen - len, MACSTR "\nflags=",
96 if (os_snprintf_error(buflen - len, ret))
100 ret = ap_sta_flags_txt(sta->flags, buf + len, buflen - len);
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))
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))
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))
127 res = ieee802_11_get_mib_sta(hapd, sta, buf + len, buflen - len);
130 res = wpa_get_mib_sta(sta->wpa_sm, buf + len, buflen - len);
133 res = ieee802_1x_get_mib_sta(hapd, sta, buf + len, buflen - len);
136 res = hostapd_wps_get_mib_sta(hapd, sta->addr, buf + len,
140 res = hostapd_p2p_get_mib_sta(hapd, sta, buf + len, buflen - len);
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);
148 if (sta->sae && sta->sae->state == SAE_ACCEPTED) {
149 res = os_snprintf(buf + len, buflen - len, "sae_group=%d\n",
151 if (!os_snprintf_error(buflen - len, res))
154 #endif /* CONFIG_SAE */
156 if (sta->vlan_id > 0) {
157 res = os_snprintf(buf + len, buflen - len, "vlan_id=%d\n",
159 if (!os_snprintf_error(buflen - len, res))
167 int hostapd_ctrl_iface_sta_first(struct hostapd_data *hapd,
168 char *buf, size_t buflen)
170 return hostapd_ctrl_iface_sta_mib(hapd, hapd->sta_list, buf, buflen);
174 int hostapd_ctrl_iface_sta(struct hostapd_data *hapd, const char *txtaddr,
175 char *buf, size_t buflen)
180 struct sta_info *sta;
182 if (hwaddr_aton(txtaddr, addr)) {
183 ret = os_snprintf(buf, buflen, "FAIL\n");
184 if (os_snprintf_error(buflen, ret))
189 sta = ap_get_sta(hapd, addr);
193 pos = os_strchr(txtaddr, ' ');
197 #ifdef HOSTAPD_DUMP_STATE
198 if (os_strcmp(pos, "eapol") == 0) {
199 if (sta->eapol_sm == NULL)
201 return eapol_auth_dump_state(sta->eapol_sm, buf,
204 #endif /* HOSTAPD_DUMP_STATE */
209 return hostapd_ctrl_iface_sta_mib(hapd, sta, buf, buflen);
213 int hostapd_ctrl_iface_sta_next(struct hostapd_data *hapd, const char *txtaddr,
214 char *buf, size_t buflen)
217 struct sta_info *sta;
220 if (hwaddr_aton(txtaddr, addr) ||
221 (sta = ap_get_sta(hapd, addr)) == NULL) {
222 ret = os_snprintf(buf, buflen, "FAIL\n");
223 if (os_snprintf_error(buflen, ret))
231 return hostapd_ctrl_iface_sta_mib(hapd, sta->next, buf, buflen);
235 #ifdef CONFIG_P2P_MANAGER
236 static int p2p_manager_disconnect(struct hostapd_data *hapd, u16 stype,
237 u8 minor_reason_code, const u8 *addr)
239 struct ieee80211_mgmt *mgmt;
243 if (hapd->driver->send_frame == NULL)
246 mgmt = os_zalloc(sizeof(*mgmt) + 100);
250 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, stype);
251 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "P2P: Disconnect STA " MACSTR
252 " with minor reason code %u (stype=%u (%s))",
253 MAC2STR(addr), minor_reason_code, stype,
254 fc2str(mgmt->frame_control));
256 os_memcpy(mgmt->da, addr, ETH_ALEN);
257 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
258 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
259 if (stype == WLAN_FC_STYPE_DEAUTH) {
260 mgmt->u.deauth.reason_code =
261 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
262 pos = (u8 *) (&mgmt->u.deauth.reason_code + 1);
264 mgmt->u.disassoc.reason_code =
265 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
266 pos = (u8 *) (&mgmt->u.disassoc.reason_code + 1);
269 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
271 WPA_PUT_BE32(pos, P2P_IE_VENDOR_TYPE);
274 *pos++ = P2P_ATTR_MINOR_REASON_CODE;
275 WPA_PUT_LE16(pos, 1);
277 *pos++ = minor_reason_code;
279 ret = hapd->driver->send_frame(hapd->drv_priv, (u8 *) mgmt,
280 pos - (u8 *) mgmt, 1);
283 return ret < 0 ? -1 : 0;
285 #endif /* CONFIG_P2P_MANAGER */
288 int hostapd_ctrl_iface_deauthenticate(struct hostapd_data *hapd,
292 struct sta_info *sta;
294 u16 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
296 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DEAUTHENTICATE %s",
299 if (hwaddr_aton(txtaddr, addr))
302 pos = os_strstr(txtaddr, " reason=");
304 reason = atoi(pos + 8);
306 pos = os_strstr(txtaddr, " test=");
308 struct ieee80211_mgmt mgmt;
310 if (hapd->driver->send_frame == NULL)
314 os_memset(&mgmt, 0, sizeof(mgmt));
315 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
316 WLAN_FC_STYPE_DEAUTH);
317 os_memcpy(mgmt.da, addr, ETH_ALEN);
318 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
319 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
320 mgmt.u.deauth.reason_code = host_to_le16(reason);
321 if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
323 sizeof(mgmt.u.deauth),
329 #ifdef CONFIG_P2P_MANAGER
330 pos = os_strstr(txtaddr, " p2p=");
332 return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DEAUTH,
333 atoi(pos + 5), addr);
335 #endif /* CONFIG_P2P_MANAGER */
337 hostapd_drv_sta_deauth(hapd, addr, reason);
338 sta = ap_get_sta(hapd, addr);
340 ap_sta_deauthenticate(hapd, sta, reason);
341 else if (addr[0] == 0xff)
342 hostapd_free_stas(hapd);
348 int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd,
352 struct sta_info *sta;
354 u16 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
356 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DISASSOCIATE %s",
359 if (hwaddr_aton(txtaddr, addr))
362 pos = os_strstr(txtaddr, " reason=");
364 reason = atoi(pos + 8);
366 pos = os_strstr(txtaddr, " test=");
368 struct ieee80211_mgmt mgmt;
370 if (hapd->driver->send_frame == NULL)
374 os_memset(&mgmt, 0, sizeof(mgmt));
375 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
376 WLAN_FC_STYPE_DISASSOC);
377 os_memcpy(mgmt.da, addr, ETH_ALEN);
378 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
379 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
380 mgmt.u.disassoc.reason_code = host_to_le16(reason);
381 if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
383 sizeof(mgmt.u.deauth),
389 #ifdef CONFIG_P2P_MANAGER
390 pos = os_strstr(txtaddr, " p2p=");
392 return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DISASSOC,
393 atoi(pos + 5), addr);
395 #endif /* CONFIG_P2P_MANAGER */
397 hostapd_drv_sta_disassoc(hapd, addr, reason);
398 sta = ap_get_sta(hapd, addr);
400 ap_sta_disassociate(hapd, sta, reason);
401 else if (addr[0] == 0xff)
402 hostapd_free_stas(hapd);
408 int hostapd_ctrl_iface_status(struct hostapd_data *hapd, char *buf,
411 struct hostapd_iface *iface = hapd->iface;
415 ret = os_snprintf(buf + len, buflen - len,
419 "num_sta_non_erp=%d\n"
420 "num_sta_no_short_slot_time=%d\n"
421 "num_sta_no_short_preamble=%d\n"
423 "num_sta_ht_no_gf=%d\n"
425 "num_sta_ht_20_mhz=%d\n"
426 "num_sta_ht40_intolerant=%d\n"
429 hostapd_state_text(iface->state),
432 iface->num_sta_non_erp,
433 iface->num_sta_no_short_slot_time,
434 iface->num_sta_no_short_preamble,
436 iface->num_sta_ht_no_gf,
437 iface->num_sta_no_ht,
438 iface->num_sta_ht_20mhz,
439 iface->num_sta_ht40_intolerant,
442 if (os_snprintf_error(buflen - len, ret))
446 if (!iface->cac_started || !iface->dfs_cac_ms) {
447 ret = os_snprintf(buf + len, buflen - len,
448 "cac_time_seconds=%d\n"
449 "cac_time_left_seconds=N/A\n",
450 iface->dfs_cac_ms / 1000);
452 /* CAC started and CAC time set - calculate remaining time */
453 struct os_reltime now;
454 unsigned int left_time;
456 os_reltime_age(&iface->dfs_cac_start, &now);
457 left_time = iface->dfs_cac_ms / 1000 - now.sec;
458 ret = os_snprintf(buf + len, buflen - len,
459 "cac_time_seconds=%u\n"
460 "cac_time_left_seconds=%u\n",
461 iface->dfs_cac_ms / 1000,
464 if (os_snprintf_error(buflen - len, ret))
468 ret = os_snprintf(buf + len, buflen - len,
470 "secondary_channel=%d\n"
473 "vht_oper_chwidth=%d\n"
474 "vht_oper_centr_freq_seg0_idx=%d\n"
475 "vht_oper_centr_freq_seg1_idx=%d\n",
476 iface->conf->channel,
477 iface->conf->secondary_channel,
478 iface->conf->ieee80211n,
479 iface->conf->ieee80211ac,
480 iface->conf->vht_oper_chwidth,
481 iface->conf->vht_oper_centr_freq_seg0_idx,
482 iface->conf->vht_oper_centr_freq_seg1_idx);
483 if (os_snprintf_error(buflen - len, ret))
487 for (i = 0; i < iface->num_bss; i++) {
488 struct hostapd_data *bss = iface->bss[i];
489 ret = os_snprintf(buf + len, buflen - len,
491 "bssid[%d]=" MACSTR "\n"
494 (int) i, bss->conf->iface,
495 (int) i, MAC2STR(bss->own_addr),
497 wpa_ssid_txt(bss->conf->ssid.ssid,
498 bss->conf->ssid.ssid_len),
499 (int) i, bss->num_sta);
500 if (os_snprintf_error(buflen - len, ret))
509 int hostapd_parse_csa_settings(const char *pos,
510 struct csa_settings *settings)
514 os_memset(settings, 0, sizeof(*settings));
515 settings->cs_count = strtol(pos, &end, 10);
517 wpa_printf(MSG_ERROR, "chanswitch: invalid cs_count provided");
521 settings->freq_params.freq = atoi(end);
522 if (settings->freq_params.freq == 0) {
523 wpa_printf(MSG_ERROR, "chanswitch: invalid freq provided");
527 #define SET_CSA_SETTING(str) \
529 const char *pos2 = os_strstr(pos, " " #str "="); \
531 pos2 += sizeof(" " #str "=") - 1; \
532 settings->freq_params.str = atoi(pos2); \
536 SET_CSA_SETTING(center_freq1);
537 SET_CSA_SETTING(center_freq2);
538 SET_CSA_SETTING(bandwidth);
539 SET_CSA_SETTING(sec_channel_offset);
540 settings->freq_params.ht_enabled = !!os_strstr(pos, " ht");
541 settings->freq_params.vht_enabled = !!os_strstr(pos, " vht");
542 settings->block_tx = !!os_strstr(pos, " blocktx");
543 #undef SET_CSA_SETTING
549 int hostapd_ctrl_iface_stop_ap(struct hostapd_data *hapd)
551 return hostapd_drv_stop_ap(hapd);