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\n",
40 data.rx_packets, data.tx_packets,
41 data.rx_bytes, data.tx_bytes);
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(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 hostapd_drv_sta_deauth(hapd, addr, reason);
356 sta = ap_get_sta(hapd, addr);
358 ap_sta_deauthenticate(hapd, sta, reason);
359 else if (addr[0] == 0xff)
360 hostapd_free_stas(hapd);
366 int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd,
370 struct sta_info *sta;
372 u16 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
374 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DISASSOCIATE %s",
377 if (hwaddr_aton(txtaddr, addr))
380 pos = os_strstr(txtaddr, " reason=");
382 reason = atoi(pos + 8);
384 pos = os_strstr(txtaddr, " test=");
386 struct ieee80211_mgmt mgmt;
388 if (hapd->driver->send_frame == NULL)
392 os_memset(&mgmt, 0, sizeof(mgmt));
393 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
394 WLAN_FC_STYPE_DISASSOC);
395 os_memcpy(mgmt.da, addr, ETH_ALEN);
396 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
397 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
398 mgmt.u.disassoc.reason_code = host_to_le16(reason);
399 if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
401 sizeof(mgmt.u.deauth),
407 #ifdef CONFIG_P2P_MANAGER
408 pos = os_strstr(txtaddr, " p2p=");
410 return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DISASSOC,
411 atoi(pos + 5), addr);
413 #endif /* CONFIG_P2P_MANAGER */
415 hostapd_drv_sta_disassoc(hapd, addr, reason);
416 sta = ap_get_sta(hapd, addr);
418 ap_sta_disassociate(hapd, sta, reason);
419 else if (addr[0] == 0xff)
420 hostapd_free_stas(hapd);
426 int hostapd_ctrl_iface_status(struct hostapd_data *hapd, char *buf,
429 struct hostapd_iface *iface = hapd->iface;
433 ret = os_snprintf(buf + len, buflen - len,
437 "num_sta_non_erp=%d\n"
438 "num_sta_no_short_slot_time=%d\n"
439 "num_sta_no_short_preamble=%d\n"
441 "num_sta_ht_no_gf=%d\n"
443 "num_sta_ht_20_mhz=%d\n"
444 "num_sta_ht40_intolerant=%d\n"
447 hostapd_state_text(iface->state),
450 iface->num_sta_non_erp,
451 iface->num_sta_no_short_slot_time,
452 iface->num_sta_no_short_preamble,
454 iface->num_sta_ht_no_gf,
455 iface->num_sta_no_ht,
456 iface->num_sta_ht_20mhz,
457 iface->num_sta_ht40_intolerant,
460 if (os_snprintf_error(buflen - len, ret))
464 if (!iface->cac_started || !iface->dfs_cac_ms) {
465 ret = os_snprintf(buf + len, buflen - len,
466 "cac_time_seconds=%d\n"
467 "cac_time_left_seconds=N/A\n",
468 iface->dfs_cac_ms / 1000);
470 /* CAC started and CAC time set - calculate remaining time */
471 struct os_reltime now;
472 unsigned int left_time;
474 os_reltime_age(&iface->dfs_cac_start, &now);
475 left_time = iface->dfs_cac_ms / 1000 - now.sec;
476 ret = os_snprintf(buf + len, buflen - len,
477 "cac_time_seconds=%u\n"
478 "cac_time_left_seconds=%u\n",
479 iface->dfs_cac_ms / 1000,
482 if (os_snprintf_error(buflen - len, ret))
486 ret = os_snprintf(buf + len, buflen - len,
488 "secondary_channel=%d\n"
491 "vht_oper_chwidth=%d\n"
492 "vht_oper_centr_freq_seg0_idx=%d\n"
493 "vht_oper_centr_freq_seg1_idx=%d\n",
494 iface->conf->channel,
495 iface->conf->secondary_channel,
496 iface->conf->ieee80211n,
497 iface->conf->ieee80211ac,
498 iface->conf->vht_oper_chwidth,
499 iface->conf->vht_oper_centr_freq_seg0_idx,
500 iface->conf->vht_oper_centr_freq_seg1_idx);
501 if (os_snprintf_error(buflen - len, ret))
505 for (i = 0; i < iface->num_bss; i++) {
506 struct hostapd_data *bss = iface->bss[i];
507 ret = os_snprintf(buf + len, buflen - len,
509 "bssid[%d]=" MACSTR "\n"
512 (int) i, bss->conf->iface,
513 (int) i, MAC2STR(bss->own_addr),
515 wpa_ssid_txt(bss->conf->ssid.ssid,
516 bss->conf->ssid.ssid_len),
517 (int) i, bss->num_sta);
518 if (os_snprintf_error(buflen - len, ret))
527 int hostapd_parse_csa_settings(const char *pos,
528 struct csa_settings *settings)
532 os_memset(settings, 0, sizeof(*settings));
533 settings->cs_count = strtol(pos, &end, 10);
535 wpa_printf(MSG_ERROR, "chanswitch: invalid cs_count provided");
539 settings->freq_params.freq = atoi(end);
540 if (settings->freq_params.freq == 0) {
541 wpa_printf(MSG_ERROR, "chanswitch: invalid freq provided");
545 #define SET_CSA_SETTING(str) \
547 const char *pos2 = os_strstr(pos, " " #str "="); \
549 pos2 += sizeof(" " #str "=") - 1; \
550 settings->freq_params.str = atoi(pos2); \
554 SET_CSA_SETTING(center_freq1);
555 SET_CSA_SETTING(center_freq2);
556 SET_CSA_SETTING(bandwidth);
557 SET_CSA_SETTING(sec_channel_offset);
558 settings->freq_params.ht_enabled = !!os_strstr(pos, " ht");
559 settings->freq_params.vht_enabled = !!os_strstr(pos, " vht");
560 settings->block_tx = !!os_strstr(pos, " blocktx");
561 #undef SET_CSA_SETTING
567 int hostapd_ctrl_iface_stop_ap(struct hostapd_data *hapd)
569 return hostapd_drv_stop_ap(hapd);