1 /*
2 * Control interface for shared AP commands
3 * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "common/ieee802_11_defs.h"
13 #include "hostapd.h"
14 #include "ieee802_1x.h"
15 #include "wpa_auth.h"
16 #include "ieee802_11.h"
17 #include "sta_info.h"
18 #include "wps_hostapd.h"
19 #include "p2p_hostapd.h"
20 #include "ctrl_iface_ap.h"
21 #include "ap_drv_ops.h"
22
23
hostapd_get_sta_conn_time(struct sta_info * sta,char * buf,size_t buflen)24 static int hostapd_get_sta_conn_time(struct sta_info *sta,
25 char *buf, size_t buflen)
26 {
27 struct os_time now, age;
28 int len = 0, ret;
29
30 if (!sta->connected_time.sec)
31 return 0;
32
33 os_get_time(&now);
34 os_time_sub(&now, &sta->connected_time, &age);
35
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)
39 return len;
40 len += ret;
41
42 return len;
43 }
44
45
hostapd_ctrl_iface_sta_mib(struct hostapd_data * hapd,struct sta_info * sta,char * buf,size_t buflen)46 static int hostapd_ctrl_iface_sta_mib(struct hostapd_data *hapd,
47 struct sta_info *sta,
48 char *buf, size_t buflen)
49 {
50 int len, res, ret;
51
52 if (sta == NULL) {
53 ret = os_snprintf(buf, buflen, "FAIL\n");
54 if (ret < 0 || (size_t) ret >= buflen)
55 return 0;
56 return ret;
57 }
58
59 len = 0;
60 ret = os_snprintf(buf + len, buflen - len, MACSTR "\n",
61 MAC2STR(sta->addr));
62 if (ret < 0 || (size_t) ret >= buflen - len)
63 return len;
64 len += ret;
65
66 res = ieee802_11_get_mib_sta(hapd, sta, buf + len, buflen - len);
67 if (res >= 0)
68 len += res;
69 res = wpa_get_mib_sta(sta->wpa_sm, buf + len, buflen - len);
70 if (res >= 0)
71 len += res;
72 res = ieee802_1x_get_mib_sta(hapd, sta, buf + len, buflen - len);
73 if (res >= 0)
74 len += res;
75 res = hostapd_wps_get_mib_sta(hapd, sta->addr, buf + len,
76 buflen - len);
77 if (res >= 0)
78 len += res;
79 res = hostapd_p2p_get_mib_sta(hapd, sta, buf + len, buflen - len);
80 if (res >= 0)
81 len += res;
82
83 res = hostapd_get_sta_conn_time(sta, buf + len, buflen - len);
84 if (res >= 0)
85 len += res;
86
87 return len;
88 }
89
90
hostapd_ctrl_iface_sta_first(struct hostapd_data * hapd,char * buf,size_t buflen)91 int hostapd_ctrl_iface_sta_first(struct hostapd_data *hapd,
92 char *buf, size_t buflen)
93 {
94 return hostapd_ctrl_iface_sta_mib(hapd, hapd->sta_list, buf, buflen);
95 }
96
97
hostapd_ctrl_iface_sta(struct hostapd_data * hapd,const char * txtaddr,char * buf,size_t buflen)98 int hostapd_ctrl_iface_sta(struct hostapd_data *hapd, const char *txtaddr,
99 char *buf, size_t buflen)
100 {
101 u8 addr[ETH_ALEN];
102 int ret;
103
104 if (hwaddr_aton(txtaddr, addr)) {
105 ret = os_snprintf(buf, buflen, "FAIL\n");
106 if (ret < 0 || (size_t) ret >= buflen)
107 return 0;
108 return ret;
109 }
110 return hostapd_ctrl_iface_sta_mib(hapd, ap_get_sta(hapd, addr),
111 buf, buflen);
112 }
113
114
hostapd_ctrl_iface_sta_next(struct hostapd_data * hapd,const char * txtaddr,char * buf,size_t buflen)115 int hostapd_ctrl_iface_sta_next(struct hostapd_data *hapd, const char *txtaddr,
116 char *buf, size_t buflen)
117 {
118 u8 addr[ETH_ALEN];
119 struct sta_info *sta;
120 int ret;
121
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)
126 return 0;
127 return ret;
128 }
129 return hostapd_ctrl_iface_sta_mib(hapd, sta->next, buf, buflen);
130 }
131
132
133 #ifdef CONFIG_P2P_MANAGER
p2p_manager_disconnect(struct hostapd_data * hapd,u16 stype,u8 minor_reason_code,const u8 * addr)134 static int p2p_manager_disconnect(struct hostapd_data *hapd, u16 stype,
135 u8 minor_reason_code, const u8 *addr)
136 {
137 struct ieee80211_mgmt *mgmt;
138 int ret;
139 u8 *pos;
140
141 if (hapd->driver->send_frame == NULL)
142 return -1;
143
144 mgmt = os_zalloc(sizeof(*mgmt) + 100);
145 if (mgmt == NULL)
146 return -1;
147
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);
151
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);
160 } else {
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);
164 }
165
166 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
167 *pos++ = 4 + 3 + 1;
168 WPA_PUT_BE24(pos, OUI_WFA);
169 pos += 3;
170 *pos++ = P2P_OUI_TYPE;
171
172 *pos++ = P2P_ATTR_MINOR_REASON_CODE;
173 WPA_PUT_LE16(pos, 1);
174 pos += 2;
175 *pos++ = minor_reason_code;
176
177 ret = hapd->driver->send_frame(hapd->drv_priv, (u8 *) mgmt,
178 pos - (u8 *) mgmt, 1);
179 os_free(mgmt);
180
181 return ret < 0 ? -1 : 0;
182 }
183 #endif /* CONFIG_P2P_MANAGER */
184
185
hostapd_ctrl_iface_deauthenticate(struct hostapd_data * hapd,const char * txtaddr)186 int hostapd_ctrl_iface_deauthenticate(struct hostapd_data *hapd,
187 const char *txtaddr)
188 {
189 u8 addr[ETH_ALEN];
190 struct sta_info *sta;
191 const char *pos;
192
193 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DEAUTHENTICATE %s",
194 txtaddr);
195
196 if (hwaddr_aton(txtaddr, addr))
197 return -1;
198
199 pos = os_strstr(txtaddr, " test=");
200 if (pos) {
201 struct ieee80211_mgmt mgmt;
202 int encrypt;
203 if (hapd->driver->send_frame == NULL)
204 return -1;
205 pos += 6;
206 encrypt = atoi(pos);
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,
216 IEEE80211_HDRLEN +
217 sizeof(mgmt.u.deauth),
218 encrypt) < 0)
219 return -1;
220 return 0;
221 }
222
223 #ifdef CONFIG_P2P_MANAGER
224 pos = os_strstr(txtaddr, " p2p=");
225 if (pos) {
226 return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DEAUTH,
227 atoi(pos + 5), addr);
228 }
229 #endif /* CONFIG_P2P_MANAGER */
230
231 hostapd_drv_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
232 sta = ap_get_sta(hapd, addr);
233 if (sta)
234 ap_sta_deauthenticate(hapd, sta,
235 WLAN_REASON_PREV_AUTH_NOT_VALID);
236 else if (addr[0] == 0xff)
237 hostapd_free_stas(hapd);
238
239 return 0;
240 }
241
242
hostapd_ctrl_iface_disassociate(struct hostapd_data * hapd,const char * txtaddr)243 int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd,
244 const char *txtaddr)
245 {
246 u8 addr[ETH_ALEN];
247 struct sta_info *sta;
248 const char *pos;
249
250 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DISASSOCIATE %s",
251 txtaddr);
252
253 if (hwaddr_aton(txtaddr, addr))
254 return -1;
255
256 pos = os_strstr(txtaddr, " test=");
257 if (pos) {
258 struct ieee80211_mgmt mgmt;
259 int encrypt;
260 if (hapd->driver->send_frame == NULL)
261 return -1;
262 pos += 6;
263 encrypt = atoi(pos);
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,
273 IEEE80211_HDRLEN +
274 sizeof(mgmt.u.deauth),
275 encrypt) < 0)
276 return -1;
277 return 0;
278 }
279
280 #ifdef CONFIG_P2P_MANAGER
281 pos = os_strstr(txtaddr, " p2p=");
282 if (pos) {
283 return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DISASSOC,
284 atoi(pos + 5), addr);
285 }
286 #endif /* CONFIG_P2P_MANAGER */
287
288 hostapd_drv_sta_disassoc(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
289 sta = ap_get_sta(hapd, addr);
290 if (sta)
291 ap_sta_disassociate(hapd, sta,
292 WLAN_REASON_PREV_AUTH_NOT_VALID);
293 else if (addr[0] == 0xff)
294 hostapd_free_stas(hapd);
295
296 return 0;
297 }
298