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 "\nflags=",
61 if (ret < 0 || (size_t) ret >= buflen - len)
65 ret = ap_sta_flags_txt(sta->flags, buf + len, buflen - len);
70 ret = os_snprintf(buf + len, buflen - len, "\n");
71 if (ret < 0 || (size_t) ret >= buflen - len)
75 res = ieee802_11_get_mib_sta(hapd, sta, buf + len, buflen - len);
78 res = wpa_get_mib_sta(sta->wpa_sm, buf + len, buflen - len);
81 res = ieee802_1x_get_mib_sta(hapd, sta, buf + len, buflen - len);
84 res = hostapd_wps_get_mib_sta(hapd, sta->addr, buf + len,
88 res = hostapd_p2p_get_mib_sta(hapd, sta, buf + len, buflen - len);
92 res = hostapd_get_sta_conn_time(sta, buf + len, buflen - len);
100 int hostapd_ctrl_iface_sta_first(struct hostapd_data *hapd,
101 char *buf, size_t buflen)
103 return hostapd_ctrl_iface_sta_mib(hapd, hapd->sta_list, buf, buflen);
107 int hostapd_ctrl_iface_sta(struct hostapd_data *hapd, const char *txtaddr,
108 char *buf, size_t buflen)
113 if (hwaddr_aton(txtaddr, addr)) {
114 ret = os_snprintf(buf, buflen, "FAIL\n");
115 if (ret < 0 || (size_t) ret >= buflen)
119 return hostapd_ctrl_iface_sta_mib(hapd, ap_get_sta(hapd, addr),
124 int hostapd_ctrl_iface_sta_next(struct hostapd_data *hapd, const char *txtaddr,
125 char *buf, size_t buflen)
128 struct sta_info *sta;
131 if (hwaddr_aton(txtaddr, addr) ||
132 (sta = ap_get_sta(hapd, addr)) == NULL) {
133 ret = os_snprintf(buf, buflen, "FAIL\n");
134 if (ret < 0 || (size_t) ret >= buflen)
138 return hostapd_ctrl_iface_sta_mib(hapd, sta->next, buf, buflen);
142 #ifdef CONFIG_P2P_MANAGER
143 static int p2p_manager_disconnect(struct hostapd_data *hapd, u16 stype,
144 u8 minor_reason_code, const u8 *addr)
146 struct ieee80211_mgmt *mgmt;
150 if (hapd->driver->send_frame == NULL)
153 mgmt = os_zalloc(sizeof(*mgmt) + 100);
157 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "P2P: Disconnect STA " MACSTR
158 " with minor reason code %u (stype=%u)",
159 MAC2STR(addr), minor_reason_code, stype);
161 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, stype);
162 os_memcpy(mgmt->da, addr, ETH_ALEN);
163 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
164 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
165 if (stype == WLAN_FC_STYPE_DEAUTH) {
166 mgmt->u.deauth.reason_code =
167 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
168 pos = (u8 *) (&mgmt->u.deauth.reason_code + 1);
170 mgmt->u.disassoc.reason_code =
171 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
172 pos = (u8 *) (&mgmt->u.disassoc.reason_code + 1);
175 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
177 WPA_PUT_BE24(pos, OUI_WFA);
179 *pos++ = P2P_OUI_TYPE;
181 *pos++ = P2P_ATTR_MINOR_REASON_CODE;
182 WPA_PUT_LE16(pos, 1);
184 *pos++ = minor_reason_code;
186 ret = hapd->driver->send_frame(hapd->drv_priv, (u8 *) mgmt,
187 pos - (u8 *) mgmt, 1);
190 return ret < 0 ? -1 : 0;
192 #endif /* CONFIG_P2P_MANAGER */
195 int hostapd_ctrl_iface_deauthenticate(struct hostapd_data *hapd,
199 struct sta_info *sta;
201 u16 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
203 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DEAUTHENTICATE %s",
206 if (hwaddr_aton(txtaddr, addr))
209 pos = os_strstr(txtaddr, " test=");
211 struct ieee80211_mgmt mgmt;
213 if (hapd->driver->send_frame == NULL)
217 os_memset(&mgmt, 0, sizeof(mgmt));
218 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
219 WLAN_FC_STYPE_DEAUTH);
220 os_memcpy(mgmt.da, addr, ETH_ALEN);
221 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
222 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
223 mgmt.u.deauth.reason_code =
224 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
225 if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
227 sizeof(mgmt.u.deauth),
233 #ifdef CONFIG_P2P_MANAGER
234 pos = os_strstr(txtaddr, " p2p=");
236 return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DEAUTH,
237 atoi(pos + 5), addr);
239 #endif /* CONFIG_P2P_MANAGER */
241 pos = os_strstr(txtaddr, " reason=");
243 reason = atoi(pos + 8);
245 hostapd_drv_sta_deauth(hapd, addr, reason);
246 sta = ap_get_sta(hapd, addr);
248 ap_sta_deauthenticate(hapd, sta, reason);
249 else if (addr[0] == 0xff)
250 hostapd_free_stas(hapd);
256 int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd,
260 struct sta_info *sta;
262 u16 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
264 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DISASSOCIATE %s",
267 if (hwaddr_aton(txtaddr, addr))
270 pos = os_strstr(txtaddr, " test=");
272 struct ieee80211_mgmt mgmt;
274 if (hapd->driver->send_frame == NULL)
278 os_memset(&mgmt, 0, sizeof(mgmt));
279 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
280 WLAN_FC_STYPE_DISASSOC);
281 os_memcpy(mgmt.da, addr, ETH_ALEN);
282 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
283 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
284 mgmt.u.disassoc.reason_code =
285 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
286 if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
288 sizeof(mgmt.u.deauth),
294 #ifdef CONFIG_P2P_MANAGER
295 pos = os_strstr(txtaddr, " p2p=");
297 return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DISASSOC,
298 atoi(pos + 5), addr);
300 #endif /* CONFIG_P2P_MANAGER */
302 pos = os_strstr(txtaddr, " reason=");
304 reason = atoi(pos + 8);
306 hostapd_drv_sta_disassoc(hapd, addr, reason);
307 sta = ap_get_sta(hapd, addr);
309 ap_sta_disassociate(hapd, sta, reason);
310 else if (addr[0] == 0xff)
311 hostapd_free_stas(hapd);
317 int hostapd_ctrl_iface_status(struct hostapd_data *hapd, char *buf,
320 struct hostapd_iface *iface = hapd->iface;
324 ret = os_snprintf(buf + len, buflen - len,
328 "num_sta_non_erp=%d\n"
329 "num_sta_no_short_slot_time=%d\n"
330 "num_sta_no_short_preamble=%d\n"
332 "num_sta_ht_no_gf=%d\n"
334 "num_sta_ht_20_mhz=%d\n"
337 hostapd_state_text(iface->state),
340 iface->num_sta_non_erp,
341 iface->num_sta_no_short_slot_time,
342 iface->num_sta_no_short_preamble,
344 iface->num_sta_ht_no_gf,
345 iface->num_sta_no_ht,
346 iface->num_sta_ht_20mhz,
349 if (ret < 0 || (size_t) ret >= buflen - len)
353 ret = os_snprintf(buf + len, buflen - len,
355 "secondary_channel=%d\n"
358 "vht_oper_chwidth=%d\n"
359 "vht_oper_centr_freq_seg0_idx=%d\n"
360 "vht_oper_centr_freq_seg1_idx=%d\n",
361 iface->conf->channel,
362 iface->conf->secondary_channel,
363 iface->conf->ieee80211n,
364 iface->conf->ieee80211ac,
365 iface->conf->vht_oper_chwidth,
366 iface->conf->vht_oper_centr_freq_seg0_idx,
367 iface->conf->vht_oper_centr_freq_seg1_idx);
368 if (ret < 0 || (size_t) ret >= buflen - len)
372 for (i = 0; i < iface->num_bss; i++) {
373 struct hostapd_data *bss = iface->bss[i];
374 ret = os_snprintf(buf + len, buflen - len,
376 "bssid[%d]=" MACSTR "\n"
379 (int) i, bss->conf->iface,
380 (int) i, MAC2STR(bss->own_addr),
382 wpa_ssid_txt(bss->conf->ssid.ssid,
383 bss->conf->ssid.ssid_len),
384 (int) i, bss->num_sta);
385 if (ret < 0 || (size_t) ret >= buflen - len)
394 int hostapd_parse_csa_settings(const char *pos,
395 struct csa_settings *settings)
402 os_memset(settings, 0, sizeof(*settings));
403 settings->cs_count = strtol(pos, &end, 10);
405 wpa_printf(MSG_ERROR, "chanswitch: invalid cs_count provided");
409 settings->freq_params.freq = atoi(end);
410 if (settings->freq_params.freq == 0) {
411 wpa_printf(MSG_ERROR, "chanswitch: invalid freq provided");
415 #define SET_CSA_SETTING(str) \
417 const char *pos2 = os_strstr(pos, " " #str "="); \
419 pos2 += sizeof(" " #str "=") - 1; \
420 settings->freq_params.str = atoi(pos2); \
424 SET_CSA_SETTING(center_freq1);
425 SET_CSA_SETTING(center_freq2);
426 SET_CSA_SETTING(bandwidth);
427 SET_CSA_SETTING(sec_channel_offset);
428 settings->freq_params.ht_enabled = !!os_strstr(pos, " ht");
429 settings->freq_params.vht_enabled = !!os_strstr(pos, " vht");
430 settings->block_tx = !!os_strstr(pos, " blocktx");
431 #undef SET_CSA_SETTING