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 */
160 int hostapd_ctrl_iface_sta_first(struct hostapd_data *hapd,
161 char *buf, size_t buflen)
163 return hostapd_ctrl_iface_sta_mib(hapd, hapd->sta_list, buf, buflen);
167 int hostapd_ctrl_iface_sta(struct hostapd_data *hapd, const char *txtaddr,
168 char *buf, size_t buflen)
173 struct sta_info *sta;
175 if (hwaddr_aton(txtaddr, addr)) {
176 ret = os_snprintf(buf, buflen, "FAIL\n");
177 if (os_snprintf_error(buflen, ret))
182 sta = ap_get_sta(hapd, addr);
186 pos = os_strchr(txtaddr, ' ');
190 #ifdef HOSTAPD_DUMP_STATE
191 if (os_strcmp(pos, "eapol") == 0) {
192 if (sta->eapol_sm == NULL)
194 return eapol_auth_dump_state(sta->eapol_sm, buf,
197 #endif /* HOSTAPD_DUMP_STATE */
202 return hostapd_ctrl_iface_sta_mib(hapd, sta, buf, buflen);
206 int hostapd_ctrl_iface_sta_next(struct hostapd_data *hapd, const char *txtaddr,
207 char *buf, size_t buflen)
210 struct sta_info *sta;
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))
224 return hostapd_ctrl_iface_sta_mib(hapd, sta->next, buf, buflen);
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)
232 struct ieee80211_mgmt *mgmt;
236 if (hapd->driver->send_frame == NULL)
239 mgmt = os_zalloc(sizeof(*mgmt) + 100);
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));
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);
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);
262 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
264 WPA_PUT_BE32(pos, P2P_IE_VENDOR_TYPE);
267 *pos++ = P2P_ATTR_MINOR_REASON_CODE;
268 WPA_PUT_LE16(pos, 1);
270 *pos++ = minor_reason_code;
272 ret = hapd->driver->send_frame(hapd->drv_priv, (u8 *) mgmt,
273 pos - (u8 *) mgmt, 1);
276 return ret < 0 ? -1 : 0;
278 #endif /* CONFIG_P2P_MANAGER */
281 int hostapd_ctrl_iface_deauthenticate(struct hostapd_data *hapd,
285 struct sta_info *sta;
287 u16 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
289 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DEAUTHENTICATE %s",
292 if (hwaddr_aton(txtaddr, addr))
295 pos = os_strstr(txtaddr, " reason=");
297 reason = atoi(pos + 8);
299 pos = os_strstr(txtaddr, " test=");
301 struct ieee80211_mgmt mgmt;
303 if (hapd->driver->send_frame == NULL)
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,
316 sizeof(mgmt.u.deauth),
322 #ifdef CONFIG_P2P_MANAGER
323 pos = os_strstr(txtaddr, " p2p=");
325 return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DEAUTH,
326 atoi(pos + 5), addr);
328 #endif /* CONFIG_P2P_MANAGER */
330 hostapd_drv_sta_deauth(hapd, addr, reason);
331 sta = ap_get_sta(hapd, addr);
333 ap_sta_deauthenticate(hapd, sta, reason);
334 else if (addr[0] == 0xff)
335 hostapd_free_stas(hapd);
341 int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd,
345 struct sta_info *sta;
347 u16 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
349 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DISASSOCIATE %s",
352 if (hwaddr_aton(txtaddr, addr))
355 pos = os_strstr(txtaddr, " reason=");
357 reason = atoi(pos + 8);
359 pos = os_strstr(txtaddr, " test=");
361 struct ieee80211_mgmt mgmt;
363 if (hapd->driver->send_frame == NULL)
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,
376 sizeof(mgmt.u.deauth),
382 #ifdef CONFIG_P2P_MANAGER
383 pos = os_strstr(txtaddr, " p2p=");
385 return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DISASSOC,
386 atoi(pos + 5), addr);
388 #endif /* CONFIG_P2P_MANAGER */
390 hostapd_drv_sta_disassoc(hapd, addr, reason);
391 sta = ap_get_sta(hapd, addr);
393 ap_sta_disassociate(hapd, sta, reason);
394 else if (addr[0] == 0xff)
395 hostapd_free_stas(hapd);
401 int hostapd_ctrl_iface_status(struct hostapd_data *hapd, char *buf,
404 struct hostapd_iface *iface = hapd->iface;
408 ret = os_snprintf(buf + len, buflen - len,
412 "num_sta_non_erp=%d\n"
413 "num_sta_no_short_slot_time=%d\n"
414 "num_sta_no_short_preamble=%d\n"
416 "num_sta_ht_no_gf=%d\n"
418 "num_sta_ht_20_mhz=%d\n"
419 "num_sta_ht40_intolerant=%d\n"
422 hostapd_state_text(iface->state),
425 iface->num_sta_non_erp,
426 iface->num_sta_no_short_slot_time,
427 iface->num_sta_no_short_preamble,
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,
435 if (os_snprintf_error(buflen - len, ret))
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);
445 /* CAC started and CAC time set - calculate remaining time */
446 struct os_reltime now;
447 unsigned int left_time;
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,
457 if (os_snprintf_error(buflen - len, ret))
461 ret = os_snprintf(buf + len, buflen - len,
463 "secondary_channel=%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))
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,
484 "bssid[%d]=" MACSTR "\n"
487 (int) i, bss->conf->iface,
488 (int) i, MAC2STR(bss->own_addr),
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))
502 int hostapd_parse_csa_settings(const char *pos,
503 struct csa_settings *settings)
507 os_memset(settings, 0, sizeof(*settings));
508 settings->cs_count = strtol(pos, &end, 10);
510 wpa_printf(MSG_ERROR, "chanswitch: invalid cs_count provided");
514 settings->freq_params.freq = atoi(end);
515 if (settings->freq_params.freq == 0) {
516 wpa_printf(MSG_ERROR, "chanswitch: invalid freq provided");
520 #define SET_CSA_SETTING(str) \
522 const char *pos2 = os_strstr(pos, " " #str "="); \
524 pos2 += sizeof(" " #str "=") - 1; \
525 settings->freq_params.str = atoi(pos2); \
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
542 int hostapd_ctrl_iface_stop_ap(struct hostapd_data *hapd)
544 return hostapd_drv_stop_ap(hapd);