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_time now, age;
30 if (!sta->connected_time.sec)
34 os_time_sub(&now, &sta->connected_time, &age);
36 ret = os_snprintf(buf + len, buflen - len, "connected_time=%u\n",
37 (unsigned int) age.sec);
38 if (ret < 0 || (size_t) ret >= buflen - len)
46 static int hostapd_ctrl_iface_sta_mib(struct hostapd_data *hapd,
48 char *buf, size_t buflen)
53 ret = os_snprintf(buf, buflen, "FAIL\n");
54 if (ret < 0 || (size_t) ret >= buflen)
60 ret = os_snprintf(buf + len, buflen - len, MACSTR "\n",
62 if (ret < 0 || (size_t) ret >= buflen - len)
66 res = ieee802_11_get_mib_sta(hapd, sta, buf + len, buflen - len);
69 res = wpa_get_mib_sta(sta->wpa_sm, buf + len, buflen - len);
72 res = ieee802_1x_get_mib_sta(hapd, sta, buf + len, buflen - len);
75 res = hostapd_wps_get_mib_sta(hapd, sta->addr, buf + len,
79 res = hostapd_p2p_get_mib_sta(hapd, sta, buf + len, buflen - len);
83 res = hostapd_get_sta_conn_time(sta, buf + len, buflen - len);
91 int hostapd_ctrl_iface_sta_first(struct hostapd_data *hapd,
92 char *buf, size_t buflen)
94 return hostapd_ctrl_iface_sta_mib(hapd, hapd->sta_list, buf, buflen);
98 int hostapd_ctrl_iface_sta(struct hostapd_data *hapd, const char *txtaddr,
99 char *buf, size_t buflen)
104 if (hwaddr_aton(txtaddr, addr)) {
105 ret = os_snprintf(buf, buflen, "FAIL\n");
106 if (ret < 0 || (size_t) ret >= buflen)
110 return hostapd_ctrl_iface_sta_mib(hapd, ap_get_sta(hapd, addr),
115 int hostapd_ctrl_iface_sta_next(struct hostapd_data *hapd, const char *txtaddr,
116 char *buf, size_t buflen)
119 struct sta_info *sta;
122 if (hwaddr_aton(txtaddr, addr) ||
123 (sta = ap_get_sta(hapd, addr)) == NULL) {
124 ret = os_snprintf(buf, buflen, "FAIL\n");
125 if (ret < 0 || (size_t) ret >= buflen)
129 return hostapd_ctrl_iface_sta_mib(hapd, sta->next, buf, buflen);
133 #ifdef CONFIG_P2P_MANAGER
134 static int p2p_manager_disconnect(struct hostapd_data *hapd, u16 stype,
135 u8 minor_reason_code, const u8 *addr)
137 struct ieee80211_mgmt *mgmt;
141 if (hapd->driver->send_frame == NULL)
144 mgmt = os_zalloc(sizeof(*mgmt) + 100);
148 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "P2P: Disconnect STA " MACSTR
149 " with minor reason code %u (stype=%u)",
150 MAC2STR(addr), minor_reason_code, stype);
152 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, stype);
153 os_memcpy(mgmt->da, addr, ETH_ALEN);
154 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
155 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
156 if (stype == WLAN_FC_STYPE_DEAUTH) {
157 mgmt->u.deauth.reason_code =
158 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
159 pos = (u8 *) (&mgmt->u.deauth.reason_code + 1);
161 mgmt->u.disassoc.reason_code =
162 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
163 pos = (u8 *) (&mgmt->u.disassoc.reason_code + 1);
166 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
168 WPA_PUT_BE24(pos, OUI_WFA);
170 *pos++ = P2P_OUI_TYPE;
172 *pos++ = P2P_ATTR_MINOR_REASON_CODE;
173 WPA_PUT_LE16(pos, 1);
175 *pos++ = minor_reason_code;
177 ret = hapd->driver->send_frame(hapd->drv_priv, (u8 *) mgmt,
178 pos - (u8 *) mgmt, 1);
181 return ret < 0 ? -1 : 0;
183 #endif /* CONFIG_P2P_MANAGER */
186 int hostapd_ctrl_iface_deauthenticate(struct hostapd_data *hapd,
190 struct sta_info *sta;
192 u16 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
194 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DEAUTHENTICATE %s",
197 if (hwaddr_aton(txtaddr, addr))
200 pos = os_strstr(txtaddr, " test=");
202 struct ieee80211_mgmt mgmt;
204 if (hapd->driver->send_frame == NULL)
208 os_memset(&mgmt, 0, sizeof(mgmt));
209 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
210 WLAN_FC_STYPE_DEAUTH);
211 os_memcpy(mgmt.da, addr, ETH_ALEN);
212 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
213 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
214 mgmt.u.deauth.reason_code =
215 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
216 if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
218 sizeof(mgmt.u.deauth),
224 #ifdef CONFIG_P2P_MANAGER
225 pos = os_strstr(txtaddr, " p2p=");
227 return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DEAUTH,
228 atoi(pos + 5), addr);
230 #endif /* CONFIG_P2P_MANAGER */
232 pos = os_strstr(txtaddr, " reason=");
234 reason = atoi(pos + 8);
236 hostapd_drv_sta_deauth(hapd, addr, reason);
237 sta = ap_get_sta(hapd, addr);
239 ap_sta_deauthenticate(hapd, sta, reason);
240 else if (addr[0] == 0xff)
241 hostapd_free_stas(hapd);
247 int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd,
251 struct sta_info *sta;
253 u16 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
255 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DISASSOCIATE %s",
258 if (hwaddr_aton(txtaddr, addr))
261 pos = os_strstr(txtaddr, " test=");
263 struct ieee80211_mgmt mgmt;
265 if (hapd->driver->send_frame == NULL)
269 os_memset(&mgmt, 0, sizeof(mgmt));
270 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
271 WLAN_FC_STYPE_DISASSOC);
272 os_memcpy(mgmt.da, addr, ETH_ALEN);
273 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
274 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
275 mgmt.u.disassoc.reason_code =
276 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
277 if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
279 sizeof(mgmt.u.deauth),
285 #ifdef CONFIG_P2P_MANAGER
286 pos = os_strstr(txtaddr, " p2p=");
288 return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DISASSOC,
289 atoi(pos + 5), addr);
291 #endif /* CONFIG_P2P_MANAGER */
293 pos = os_strstr(txtaddr, " reason=");
295 reason = atoi(pos + 8);
297 hostapd_drv_sta_disassoc(hapd, addr, reason);
298 sta = ap_get_sta(hapd, addr);
300 ap_sta_disassociate(hapd, sta, reason);
301 else if (addr[0] == 0xff)
302 hostapd_free_stas(hapd);
308 int hostapd_ctrl_iface_status(struct hostapd_data *hapd, char *buf,
311 struct hostapd_iface *iface = hapd->iface;
315 ret = os_snprintf(buf + len, buflen - len,
319 "num_sta_non_erp=%d\n"
320 "num_sta_no_short_slot_time=%d\n"
321 "num_sta_no_short_preamble=%d\n"
323 "num_sta_ht_no_gf=%d\n"
325 "num_sta_ht_20_mhz=%d\n"
328 hostapd_state_text(iface->state),
331 iface->num_sta_non_erp,
332 iface->num_sta_no_short_slot_time,
333 iface->num_sta_no_short_preamble,
335 iface->num_sta_ht_no_gf,
336 iface->num_sta_no_ht,
337 iface->num_sta_ht_20mhz,
340 if (ret < 0 || (size_t) ret >= buflen - len)
344 ret = os_snprintf(buf + len, buflen - len,
346 "secondary_channel=%d\n"
349 "vht_oper_chwidth=%d\n"
350 "vht_oper_centr_freq_seg0_idx=%d\n"
351 "vht_oper_centr_freq_seg1_idx=%d\n",
352 iface->conf->channel,
353 iface->conf->secondary_channel,
354 iface->conf->ieee80211n,
355 iface->conf->ieee80211ac,
356 iface->conf->vht_oper_chwidth,
357 iface->conf->vht_oper_centr_freq_seg0_idx,
358 iface->conf->vht_oper_centr_freq_seg1_idx);
359 if (ret < 0 || (size_t) ret >= buflen - len)
363 for (i = 0; i < iface->num_bss; i++) {
364 struct hostapd_data *bss = iface->bss[i];
365 ret = os_snprintf(buf + len, buflen - len,
367 "bssid[%d]=" MACSTR "\n"
370 (int) i, bss->conf->iface,
371 (int) i, MAC2STR(bss->own_addr),
373 wpa_ssid_txt(bss->conf->ssid.ssid,
374 bss->conf->ssid.ssid_len),
375 (int) i, bss->num_sta);
376 if (ret < 0 || (size_t) ret >= buflen - len)