2 * Control interface for shared AP commands
3 * Copyright (c) 2004-2013, 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"
14 #include "ieee802_1x.h"
16 #include "ieee802_11.h"
18 #include "wps_hostapd.h"
19 #include "p2p_hostapd.h"
20 #include "ctrl_iface_ap.h"
21 #include "ap_drv_ops.h"
24 static int hostapd_get_sta_conn_time(struct sta_info *sta,
25 char *buf, size_t buflen)
27 struct os_reltime age;
30 if (!sta->connected_time.sec)
33 os_reltime_age(&sta->connected_time, &age);
35 ret = os_snprintf(buf + len, buflen - len, "connected_time=%u\n",
36 (unsigned int) age.sec);
37 if (ret < 0 || (size_t) ret >= buflen - len)
45 static int hostapd_ctrl_iface_sta_mib(struct hostapd_data *hapd,
47 char *buf, size_t buflen)
52 ret = os_snprintf(buf, buflen, "FAIL\n");
53 if (ret < 0 || (size_t) ret >= buflen)
59 ret = os_snprintf(buf + len, buflen - len, MACSTR "\n",
61 if (ret < 0 || (size_t) ret >= buflen - len)
65 res = ieee802_11_get_mib_sta(hapd, sta, buf + len, buflen - len);
68 res = wpa_get_mib_sta(sta->wpa_sm, buf + len, buflen - len);
71 res = ieee802_1x_get_mib_sta(hapd, sta, buf + len, buflen - len);
74 res = hostapd_wps_get_mib_sta(hapd, sta->addr, buf + len,
78 res = hostapd_p2p_get_mib_sta(hapd, sta, buf + len, buflen - len);
82 res = hostapd_get_sta_conn_time(sta, buf + len, buflen - len);
90 int hostapd_ctrl_iface_sta_first(struct hostapd_data *hapd,
91 char *buf, size_t buflen)
93 return hostapd_ctrl_iface_sta_mib(hapd, hapd->sta_list, buf, buflen);
97 int hostapd_ctrl_iface_sta(struct hostapd_data *hapd, const char *txtaddr,
98 char *buf, size_t buflen)
103 if (hwaddr_aton(txtaddr, addr)) {
104 ret = os_snprintf(buf, buflen, "FAIL\n");
105 if (ret < 0 || (size_t) ret >= buflen)
109 return hostapd_ctrl_iface_sta_mib(hapd, ap_get_sta(hapd, addr),
114 int hostapd_ctrl_iface_sta_next(struct hostapd_data *hapd, const char *txtaddr,
115 char *buf, size_t buflen)
118 struct sta_info *sta;
121 if (hwaddr_aton(txtaddr, addr) ||
122 (sta = ap_get_sta(hapd, addr)) == NULL) {
123 ret = os_snprintf(buf, buflen, "FAIL\n");
124 if (ret < 0 || (size_t) ret >= buflen)
128 return hostapd_ctrl_iface_sta_mib(hapd, sta->next, buf, buflen);
132 #ifdef CONFIG_P2P_MANAGER
133 static int p2p_manager_disconnect(struct hostapd_data *hapd, u16 stype,
134 u8 minor_reason_code, const u8 *addr)
136 struct ieee80211_mgmt *mgmt;
140 if (hapd->driver->send_frame == NULL)
143 mgmt = os_zalloc(sizeof(*mgmt) + 100);
147 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "P2P: Disconnect STA " MACSTR
148 " with minor reason code %u (stype=%u)",
149 MAC2STR(addr), minor_reason_code, stype);
151 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, stype);
152 os_memcpy(mgmt->da, addr, ETH_ALEN);
153 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
154 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
155 if (stype == WLAN_FC_STYPE_DEAUTH) {
156 mgmt->u.deauth.reason_code =
157 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
158 pos = (u8 *) (&mgmt->u.deauth.reason_code + 1);
160 mgmt->u.disassoc.reason_code =
161 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
162 pos = (u8 *) (&mgmt->u.disassoc.reason_code + 1);
165 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
167 WPA_PUT_BE24(pos, OUI_WFA);
169 *pos++ = P2P_OUI_TYPE;
171 *pos++ = P2P_ATTR_MINOR_REASON_CODE;
172 WPA_PUT_LE16(pos, 1);
174 *pos++ = minor_reason_code;
176 ret = hapd->driver->send_frame(hapd->drv_priv, (u8 *) mgmt,
177 pos - (u8 *) mgmt, 1);
180 return ret < 0 ? -1 : 0;
182 #endif /* CONFIG_P2P_MANAGER */
185 int hostapd_ctrl_iface_deauthenticate(struct hostapd_data *hapd,
189 struct sta_info *sta;
191 u16 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
193 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DEAUTHENTICATE %s",
196 if (hwaddr_aton(txtaddr, addr))
199 pos = os_strstr(txtaddr, " test=");
201 struct ieee80211_mgmt mgmt;
203 if (hapd->driver->send_frame == NULL)
207 os_memset(&mgmt, 0, sizeof(mgmt));
208 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
209 WLAN_FC_STYPE_DEAUTH);
210 os_memcpy(mgmt.da, addr, ETH_ALEN);
211 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
212 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
213 mgmt.u.deauth.reason_code =
214 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
215 if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
217 sizeof(mgmt.u.deauth),
223 #ifdef CONFIG_P2P_MANAGER
224 pos = os_strstr(txtaddr, " p2p=");
226 return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DEAUTH,
227 atoi(pos + 5), addr);
229 #endif /* CONFIG_P2P_MANAGER */
231 pos = os_strstr(txtaddr, " reason=");
233 reason = atoi(pos + 8);
235 hostapd_drv_sta_deauth(hapd, addr, reason);
236 sta = ap_get_sta(hapd, addr);
238 ap_sta_deauthenticate(hapd, sta, reason);
239 else if (addr[0] == 0xff)
240 hostapd_free_stas(hapd);
246 int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd,
250 struct sta_info *sta;
252 u16 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
254 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DISASSOCIATE %s",
257 if (hwaddr_aton(txtaddr, addr))
260 pos = os_strstr(txtaddr, " test=");
262 struct ieee80211_mgmt mgmt;
264 if (hapd->driver->send_frame == NULL)
268 os_memset(&mgmt, 0, sizeof(mgmt));
269 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
270 WLAN_FC_STYPE_DISASSOC);
271 os_memcpy(mgmt.da, addr, ETH_ALEN);
272 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
273 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
274 mgmt.u.disassoc.reason_code =
275 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
276 if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
278 sizeof(mgmt.u.deauth),
284 #ifdef CONFIG_P2P_MANAGER
285 pos = os_strstr(txtaddr, " p2p=");
287 return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DISASSOC,
288 atoi(pos + 5), addr);
290 #endif /* CONFIG_P2P_MANAGER */
292 pos = os_strstr(txtaddr, " reason=");
294 reason = atoi(pos + 8);
296 hostapd_drv_sta_disassoc(hapd, addr, reason);
297 sta = ap_get_sta(hapd, addr);
299 ap_sta_disassociate(hapd, sta, reason);
300 else if (addr[0] == 0xff)
301 hostapd_free_stas(hapd);
307 int hostapd_ctrl_iface_status(struct hostapd_data *hapd, char *buf,
310 struct hostapd_iface *iface = hapd->iface;
314 ret = os_snprintf(buf + len, buflen - len,
318 "num_sta_non_erp=%d\n"
319 "num_sta_no_short_slot_time=%d\n"
320 "num_sta_no_short_preamble=%d\n"
322 "num_sta_ht_no_gf=%d\n"
324 "num_sta_ht_20_mhz=%d\n"
327 hostapd_state_text(iface->state),
330 iface->num_sta_non_erp,
331 iface->num_sta_no_short_slot_time,
332 iface->num_sta_no_short_preamble,
334 iface->num_sta_ht_no_gf,
335 iface->num_sta_no_ht,
336 iface->num_sta_ht_20mhz,
339 if (ret < 0 || (size_t) ret >= buflen - len)
343 ret = os_snprintf(buf + len, buflen - len,
345 "secondary_channel=%d\n"
348 "vht_oper_chwidth=%d\n"
349 "vht_oper_centr_freq_seg0_idx=%d\n"
350 "vht_oper_centr_freq_seg1_idx=%d\n",
351 iface->conf->channel,
352 iface->conf->secondary_channel,
353 iface->conf->ieee80211n,
354 iface->conf->ieee80211ac,
355 iface->conf->vht_oper_chwidth,
356 iface->conf->vht_oper_centr_freq_seg0_idx,
357 iface->conf->vht_oper_centr_freq_seg1_idx);
358 if (ret < 0 || (size_t) ret >= buflen - len)
362 for (i = 0; i < iface->num_bss; i++) {
363 struct hostapd_data *bss = iface->bss[i];
364 ret = os_snprintf(buf + len, buflen - len,
366 "bssid[%d]=" MACSTR "\n"
369 (int) i, bss->conf->iface,
370 (int) i, MAC2STR(bss->own_addr),
372 wpa_ssid_txt(bss->conf->ssid.ssid,
373 bss->conf->ssid.ssid_len),
374 (int) i, bss->num_sta);
375 if (ret < 0 || (size_t) ret >= buflen - len)
384 int hostapd_parse_csa_settings(const char *pos,
385 struct csa_settings *settings)
392 os_memset(settings, 0, sizeof(*settings));
393 settings->cs_count = strtol(pos, &end, 10);
395 wpa_printf(MSG_ERROR, "chanswitch: invalid cs_count provided");
399 settings->freq_params.freq = atoi(end);
400 if (settings->freq_params.freq == 0) {
401 wpa_printf(MSG_ERROR, "chanswitch: invalid freq provided");
405 #define SET_CSA_SETTING(str) \
407 const char *pos2 = os_strstr(pos, " " #str "="); \
409 pos2 += sizeof(" " #str "=") - 1; \
410 settings->freq_params.str = atoi(pos2); \
414 SET_CSA_SETTING(center_freq1);
415 SET_CSA_SETTING(center_freq2);
416 SET_CSA_SETTING(bandwidth);
417 SET_CSA_SETTING(sec_channel_offset);
418 settings->freq_params.ht_enabled = !!os_strstr(pos, " ht");
419 settings->freq_params.vht_enabled = !!os_strstr(pos, " vht");
420 settings->block_tx = !!os_strstr(pos, " blocktx");
421 #undef SET_CSA_SETTING