2 * Control interface for shared AP commands
3 * Copyright (c) 2004-2009, 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;
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 hostapd_drv_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
232 sta = ap_get_sta(hapd, addr);
234 ap_sta_deauthenticate(hapd, sta,
235 WLAN_REASON_PREV_AUTH_NOT_VALID);
236 else if (addr[0] == 0xff)
237 hostapd_free_stas(hapd);
243 int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd,
247 struct sta_info *sta;
250 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DISASSOCIATE %s",
253 if (hwaddr_aton(txtaddr, addr))
256 pos = os_strstr(txtaddr, " test=");
258 struct ieee80211_mgmt mgmt;
260 if (hapd->driver->send_frame == NULL)
264 os_memset(&mgmt, 0, sizeof(mgmt));
265 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
266 WLAN_FC_STYPE_DISASSOC);
267 os_memcpy(mgmt.da, addr, ETH_ALEN);
268 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
269 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
270 mgmt.u.disassoc.reason_code =
271 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
272 if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
274 sizeof(mgmt.u.deauth),
280 #ifdef CONFIG_P2P_MANAGER
281 pos = os_strstr(txtaddr, " p2p=");
283 return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DISASSOC,
284 atoi(pos + 5), addr);
286 #endif /* CONFIG_P2P_MANAGER */
288 hostapd_drv_sta_disassoc(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
289 sta = ap_get_sta(hapd, addr);
291 ap_sta_disassociate(hapd, sta,
292 WLAN_REASON_PREV_AUTH_NOT_VALID);
293 else if (addr[0] == 0xff)
294 hostapd_free_stas(hapd);