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"
15 #include "fst/fst_ctrl_iface.h"
17 #include "ieee802_1x.h"
19 #include "ieee802_11.h"
21 #include "wps_hostapd.h"
22 #include "p2p_hostapd.h"
23 #include "ctrl_iface_ap.h"
24 #include "ap_drv_ops.h"
28 static int hostapd_get_sta_tx_rx(struct hostapd_data *hapd,
30 char *buf, size_t buflen)
32 struct hostap_sta_driver_data data;
35 if (hostapd_drv_read_sta_data(hapd, &data, sta->addr) < 0)
38 ret = os_snprintf(buf, buflen, "rx_packets=%lu\ntx_packets=%lu\n"
39 "rx_bytes=%llu\ntx_bytes=%llu\ninactive_msec=%lu\n",
40 data.rx_packets, data.tx_packets,
41 data.rx_bytes, data.tx_bytes, data.inactive_msec);
42 if (os_snprintf_error(buflen, ret))
48 static int hostapd_get_sta_conn_time(struct sta_info *sta,
49 char *buf, size_t buflen)
51 struct os_reltime age;
54 if (!sta->connected_time.sec)
57 os_reltime_age(&sta->connected_time, &age);
59 ret = os_snprintf(buf, buflen, "connected_time=%u\n",
60 (unsigned int) age.sec);
61 if (os_snprintf_error(buflen, ret))
67 static const char * timeout_next_str(int val)
71 return "NULLFUNC POLL";
78 case STA_DISASSOC_FROM_CLI:
79 return "DISASSOC_FROM_CLI";
86 static int hostapd_ctrl_iface_sta_mib(struct hostapd_data *hapd,
88 char *buf, size_t buflen)
96 ret = os_snprintf(buf + len, buflen - len, MACSTR "\nflags=",
98 if (os_snprintf_error(buflen - len, ret))
102 ret = ap_sta_flags_txt(sta->flags, buf + len, buflen - len);
107 ret = os_snprintf(buf + len, buflen - len, "\naid=%d\ncapability=0x%x\n"
108 "listen_interval=%d\nsupported_rates=",
109 sta->aid, sta->capability, sta->listen_interval);
110 if (os_snprintf_error(buflen - len, ret))
114 for (i = 0; i < sta->supported_rates_len; i++) {
115 ret = os_snprintf(buf + len, buflen - len, "%02x%s",
116 sta->supported_rates[i],
117 i + 1 < sta->supported_rates_len ? " " : "");
118 if (os_snprintf_error(buflen - len, ret))
123 ret = os_snprintf(buf + len, buflen - len, "\ntimeout_next=%s\n",
124 timeout_next_str(sta->timeout_next));
125 if (os_snprintf_error(buflen - len, ret))
129 res = ieee802_11_get_mib_sta(hapd, sta, buf + len, buflen - len);
132 res = wpa_get_mib_sta(sta->wpa_sm, buf + len, buflen - len);
135 res = ieee802_1x_get_mib_sta(hapd, sta, buf + len, buflen - len);
138 res = hostapd_wps_get_mib_sta(hapd, sta->addr, buf + len,
142 res = hostapd_p2p_get_mib_sta(hapd, sta, buf + len, buflen - len);
146 len += hostapd_get_sta_tx_rx(hapd, sta, buf + len, buflen - len);
147 len += hostapd_get_sta_conn_time(sta, buf + len, buflen - len);
150 if (sta->sae && sta->sae->state == SAE_ACCEPTED) {
151 res = os_snprintf(buf + len, buflen - len, "sae_group=%d\n",
153 if (!os_snprintf_error(buflen - len, res))
156 #endif /* CONFIG_SAE */
158 if (sta->vlan_id > 0) {
159 res = os_snprintf(buf + len, buflen - len, "vlan_id=%d\n",
161 if (!os_snprintf_error(buflen - len, res))
165 res = mbo_ap_get_info(sta, buf + len, buflen - len);
169 if (sta->supp_op_classes &&
170 buflen - len > (unsigned) (17 + 2 * sta->supp_op_classes[0])) {
171 len += os_snprintf(buf + len, buflen - len, "supp_op_classes=");
172 len += wpa_snprintf_hex(buf + len, buflen - len,
173 sta->supp_op_classes + 1,
174 sta->supp_op_classes[0]);
175 len += os_snprintf(buf + len, buflen - len, "\n");
182 int hostapd_ctrl_iface_sta_first(struct hostapd_data *hapd,
183 char *buf, size_t buflen)
185 return hostapd_ctrl_iface_sta_mib(hapd, hapd->sta_list, buf, buflen);
189 int hostapd_ctrl_iface_sta(struct hostapd_data *hapd, const char *txtaddr,
190 char *buf, size_t buflen)
195 struct sta_info *sta;
197 if (hwaddr_aton(txtaddr, addr)) {
198 ret = os_snprintf(buf, buflen, "FAIL\n");
199 if (os_snprintf_error(buflen, ret))
204 sta = ap_get_sta(hapd, addr);
208 pos = os_strchr(txtaddr, ' ');
212 #ifdef HOSTAPD_DUMP_STATE
213 if (os_strcmp(pos, "eapol") == 0) {
214 if (sta->eapol_sm == NULL)
216 return eapol_auth_dump_state(sta->eapol_sm, buf,
219 #endif /* HOSTAPD_DUMP_STATE */
224 ret = hostapd_ctrl_iface_sta_mib(hapd, sta, buf, buflen);
225 ret += fst_ctrl_iface_mb_info(addr, buf + ret, buflen - ret);
231 int hostapd_ctrl_iface_sta_next(struct hostapd_data *hapd, const char *txtaddr,
232 char *buf, size_t buflen)
235 struct sta_info *sta;
238 if (hwaddr_aton(txtaddr, addr) ||
239 (sta = ap_get_sta(hapd, addr)) == NULL) {
240 ret = os_snprintf(buf, buflen, "FAIL\n");
241 if (os_snprintf_error(buflen, ret))
249 return hostapd_ctrl_iface_sta_mib(hapd, sta->next, buf, buflen);
253 #ifdef CONFIG_P2P_MANAGER
254 static int p2p_manager_disconnect(struct hostapd_data *hapd, u16 stype,
255 u8 minor_reason_code, const u8 *addr)
257 struct ieee80211_mgmt *mgmt;
261 if (hapd->driver->send_frame == NULL)
264 mgmt = os_zalloc(sizeof(*mgmt) + 100);
268 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, stype);
269 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "P2P: Disconnect STA " MACSTR
270 " with minor reason code %u (stype=%u (%s))",
271 MAC2STR(addr), minor_reason_code, stype,
272 fc2str(le_to_host16(mgmt->frame_control)));
274 os_memcpy(mgmt->da, addr, ETH_ALEN);
275 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
276 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
277 if (stype == WLAN_FC_STYPE_DEAUTH) {
278 mgmt->u.deauth.reason_code =
279 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
280 pos = (u8 *) (&mgmt->u.deauth.reason_code + 1);
282 mgmt->u.disassoc.reason_code =
283 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
284 pos = (u8 *) (&mgmt->u.disassoc.reason_code + 1);
287 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
289 WPA_PUT_BE32(pos, P2P_IE_VENDOR_TYPE);
292 *pos++ = P2P_ATTR_MINOR_REASON_CODE;
293 WPA_PUT_LE16(pos, 1);
295 *pos++ = minor_reason_code;
297 ret = hapd->driver->send_frame(hapd->drv_priv, (u8 *) mgmt,
298 pos - (u8 *) mgmt, 1);
301 return ret < 0 ? -1 : 0;
303 #endif /* CONFIG_P2P_MANAGER */
306 int hostapd_ctrl_iface_deauthenticate(struct hostapd_data *hapd,
310 struct sta_info *sta;
312 u16 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
314 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DEAUTHENTICATE %s",
317 if (hwaddr_aton(txtaddr, addr))
320 pos = os_strstr(txtaddr, " reason=");
322 reason = atoi(pos + 8);
324 pos = os_strstr(txtaddr, " test=");
326 struct ieee80211_mgmt mgmt;
328 if (hapd->driver->send_frame == NULL)
332 os_memset(&mgmt, 0, sizeof(mgmt));
333 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
334 WLAN_FC_STYPE_DEAUTH);
335 os_memcpy(mgmt.da, addr, ETH_ALEN);
336 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
337 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
338 mgmt.u.deauth.reason_code = host_to_le16(reason);
339 if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
341 sizeof(mgmt.u.deauth),
347 #ifdef CONFIG_P2P_MANAGER
348 pos = os_strstr(txtaddr, " p2p=");
350 return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DEAUTH,
351 atoi(pos + 5), addr);
353 #endif /* CONFIG_P2P_MANAGER */
355 if (os_strstr(txtaddr, " tx=0"))
356 hostapd_drv_sta_remove(hapd, addr);
358 hostapd_drv_sta_deauth(hapd, addr, reason);
359 sta = ap_get_sta(hapd, addr);
361 ap_sta_deauthenticate(hapd, sta, reason);
362 else if (addr[0] == 0xff)
363 hostapd_free_stas(hapd);
369 int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd,
373 struct sta_info *sta;
375 u16 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
377 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DISASSOCIATE %s",
380 if (hwaddr_aton(txtaddr, addr))
383 pos = os_strstr(txtaddr, " reason=");
385 reason = atoi(pos + 8);
387 pos = os_strstr(txtaddr, " test=");
389 struct ieee80211_mgmt mgmt;
391 if (hapd->driver->send_frame == NULL)
395 os_memset(&mgmt, 0, sizeof(mgmt));
396 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
397 WLAN_FC_STYPE_DISASSOC);
398 os_memcpy(mgmt.da, addr, ETH_ALEN);
399 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
400 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
401 mgmt.u.disassoc.reason_code = host_to_le16(reason);
402 if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
404 sizeof(mgmt.u.deauth),
410 #ifdef CONFIG_P2P_MANAGER
411 pos = os_strstr(txtaddr, " p2p=");
413 return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DISASSOC,
414 atoi(pos + 5), addr);
416 #endif /* CONFIG_P2P_MANAGER */
418 if (os_strstr(txtaddr, " tx=0"))
419 hostapd_drv_sta_remove(hapd, addr);
421 hostapd_drv_sta_disassoc(hapd, addr, reason);
422 sta = ap_get_sta(hapd, addr);
424 ap_sta_disassociate(hapd, sta, reason);
425 else if (addr[0] == 0xff)
426 hostapd_free_stas(hapd);
432 int hostapd_ctrl_iface_poll_sta(struct hostapd_data *hapd,
436 struct sta_info *sta;
438 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE POLL_STA %s", txtaddr);
440 if (hwaddr_aton(txtaddr, addr))
443 sta = ap_get_sta(hapd, addr);
447 hostapd_drv_poll_client(hapd, hapd->own_addr, addr,
448 sta->flags & WLAN_STA_WMM);
453 int hostapd_ctrl_iface_status(struct hostapd_data *hapd, char *buf,
456 struct hostapd_iface *iface = hapd->iface;
460 ret = os_snprintf(buf + len, buflen - len,
464 "num_sta_non_erp=%d\n"
465 "num_sta_no_short_slot_time=%d\n"
466 "num_sta_no_short_preamble=%d\n"
468 "num_sta_ht_no_gf=%d\n"
470 "num_sta_ht_20_mhz=%d\n"
471 "num_sta_ht40_intolerant=%d\n"
474 hostapd_state_text(iface->state),
477 iface->num_sta_non_erp,
478 iface->num_sta_no_short_slot_time,
479 iface->num_sta_no_short_preamble,
481 iface->num_sta_ht_no_gf,
482 iface->num_sta_no_ht,
483 iface->num_sta_ht_20mhz,
484 iface->num_sta_ht40_intolerant,
487 if (os_snprintf_error(buflen - len, ret))
491 if (!iface->cac_started || !iface->dfs_cac_ms) {
492 ret = os_snprintf(buf + len, buflen - len,
493 "cac_time_seconds=%d\n"
494 "cac_time_left_seconds=N/A\n",
495 iface->dfs_cac_ms / 1000);
497 /* CAC started and CAC time set - calculate remaining time */
498 struct os_reltime now;
499 unsigned int left_time;
501 os_reltime_age(&iface->dfs_cac_start, &now);
502 left_time = iface->dfs_cac_ms / 1000 - now.sec;
503 ret = os_snprintf(buf + len, buflen - len,
504 "cac_time_seconds=%u\n"
505 "cac_time_left_seconds=%u\n",
506 iface->dfs_cac_ms / 1000,
509 if (os_snprintf_error(buflen - len, ret))
513 ret = os_snprintf(buf + len, buflen - len,
515 "secondary_channel=%d\n"
518 iface->conf->channel,
519 iface->conf->ieee80211n && !hapd->conf->disable_11n ?
520 iface->conf->secondary_channel : 0,
521 iface->conf->ieee80211n && !hapd->conf->disable_11n,
522 iface->conf->ieee80211ac &&
523 !hapd->conf->disable_11ac);
524 if (os_snprintf_error(buflen - len, ret))
527 if (iface->conf->ieee80211ac && !hapd->conf->disable_11ac) {
528 ret = os_snprintf(buf + len, buflen - len,
529 "vht_oper_chwidth=%d\n"
530 "vht_oper_centr_freq_seg0_idx=%d\n"
531 "vht_oper_centr_freq_seg1_idx=%d\n",
532 iface->conf->vht_oper_chwidth,
533 iface->conf->vht_oper_centr_freq_seg0_idx,
534 iface->conf->vht_oper_centr_freq_seg1_idx);
535 if (os_snprintf_error(buflen - len, ret))
540 for (i = 0; i < iface->num_bss; i++) {
541 struct hostapd_data *bss = iface->bss[i];
542 ret = os_snprintf(buf + len, buflen - len,
544 "bssid[%d]=" MACSTR "\n"
547 (int) i, bss->conf->iface,
548 (int) i, MAC2STR(bss->own_addr),
550 wpa_ssid_txt(bss->conf->ssid.ssid,
551 bss->conf->ssid.ssid_len),
552 (int) i, bss->num_sta);
553 if (os_snprintf_error(buflen - len, ret))
562 int hostapd_parse_csa_settings(const char *pos,
563 struct csa_settings *settings)
567 os_memset(settings, 0, sizeof(*settings));
568 settings->cs_count = strtol(pos, &end, 10);
570 wpa_printf(MSG_ERROR, "chanswitch: invalid cs_count provided");
574 settings->freq_params.freq = atoi(end);
575 if (settings->freq_params.freq == 0) {
576 wpa_printf(MSG_ERROR, "chanswitch: invalid freq provided");
580 #define SET_CSA_SETTING(str) \
582 const char *pos2 = os_strstr(pos, " " #str "="); \
584 pos2 += sizeof(" " #str "=") - 1; \
585 settings->freq_params.str = atoi(pos2); \
589 SET_CSA_SETTING(center_freq1);
590 SET_CSA_SETTING(center_freq2);
591 SET_CSA_SETTING(bandwidth);
592 SET_CSA_SETTING(sec_channel_offset);
593 settings->freq_params.ht_enabled = !!os_strstr(pos, " ht");
594 settings->freq_params.vht_enabled = !!os_strstr(pos, " vht");
595 settings->block_tx = !!os_strstr(pos, " blocktx");
596 #undef SET_CSA_SETTING
602 int hostapd_ctrl_iface_stop_ap(struct hostapd_data *hapd)
604 return hostapd_drv_stop_ap(hapd);
608 int hostapd_ctrl_iface_pmksa_list(struct hostapd_data *hapd, char *buf,
611 return wpa_auth_pmksa_list(hapd->wpa_auth, buf, len);
615 void hostapd_ctrl_iface_pmksa_flush(struct hostapd_data *hapd)
617 wpa_auth_pmksa_flush(hapd->wpa_auth);