• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Control interface for shared AP commands
3  * Copyright (c) 2004-2019, 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 "common/sae.h"
14 #include "eapol_auth/eapol_auth_sm.h"
15 #include "fst/fst_ctrl_iface.h"
16 #include "hostapd.h"
17 #include "ieee802_1x.h"
18 #include "wpa_auth.h"
19 #include "ieee802_11.h"
20 #include "sta_info.h"
21 #include "wps_hostapd.h"
22 #include "p2p_hostapd.h"
23 #include "ctrl_iface_ap.h"
24 #include "ap_drv_ops.h"
25 #include "mbo_ap.h"
26 #include "taxonomy.h"
27 #include "wnm_ap.h"
28 
29 
hostapd_write_ht_mcs_bitmask(char * buf,size_t buflen,size_t curr_len,const u8 * mcs_set)30 static size_t hostapd_write_ht_mcs_bitmask(char *buf, size_t buflen,
31 					   size_t curr_len, const u8 *mcs_set)
32 {
33 	int ret;
34 	size_t len = curr_len;
35 
36 	ret = os_snprintf(buf + len, buflen - len,
37 			  "ht_mcs_bitmask=");
38 	if (os_snprintf_error(buflen - len, ret))
39 		return len;
40 	len += ret;
41 
42 	/* 77 first bits (+ 3 reserved bits) */
43 	len += wpa_snprintf_hex(buf + len, buflen - len, mcs_set, 10);
44 
45 	ret = os_snprintf(buf + len, buflen - len, "\n");
46 	if (os_snprintf_error(buflen - len, ret))
47 		return curr_len;
48 	len += ret;
49 
50 	return len;
51 }
52 
53 
hostapd_get_sta_conn_time(struct sta_info * sta,struct hostap_sta_driver_data * data,char * buf,size_t buflen)54 static int hostapd_get_sta_conn_time(struct sta_info *sta,
55 				     struct hostap_sta_driver_data *data,
56 				     char *buf, size_t buflen)
57 {
58 	struct os_reltime age;
59 	unsigned long secs;
60 	int ret;
61 
62 	if (sta->connected_time.sec) {
63 		/* Locally maintained time in AP mode */
64 		os_reltime_age(&sta->connected_time, &age);
65 		secs = (unsigned long) age.sec;
66 	} else if (data->flags & STA_DRV_DATA_CONN_TIME) {
67 		/* Time from the driver in mesh mode */
68 		secs = data->connected_sec;
69 	} else {
70 		return 0;
71 	}
72 
73 	ret = os_snprintf(buf, buflen, "connected_time=%lu\n", secs);
74 	if (os_snprintf_error(buflen, ret))
75 		return 0;
76 	return ret;
77 }
78 
79 
hostapd_get_sta_info(struct hostapd_data * hapd,struct sta_info * sta,char * buf,size_t buflen)80 static int hostapd_get_sta_info(struct hostapd_data *hapd,
81 				struct sta_info *sta,
82 				char *buf, size_t buflen)
83 {
84 	struct hostap_sta_driver_data data;
85 	int ret;
86 	int len = 0;
87 
88 	if (hostapd_drv_read_sta_data(hapd, &data, sta->addr) < 0)
89 		return 0;
90 
91 	ret = os_snprintf(buf, buflen, "rx_packets=%lu\ntx_packets=%lu\n"
92 			  "rx_bytes=%llu\ntx_bytes=%llu\ninactive_msec=%lu\n"
93 			  "signal=%d\n",
94 			  data.rx_packets, data.tx_packets,
95 			  data.rx_bytes, data.tx_bytes, data.inactive_msec,
96 			  data.signal);
97 	if (os_snprintf_error(buflen, ret))
98 		return 0;
99 	len += ret;
100 
101 	ret = os_snprintf(buf + len, buflen - len, "rx_rate_info=%lu",
102 			  data.current_rx_rate / 100);
103 	if (os_snprintf_error(buflen - len, ret))
104 		return len;
105 	len += ret;
106 	if (data.flags & STA_DRV_DATA_RX_MCS) {
107 		ret = os_snprintf(buf + len, buflen - len, " mcs %u",
108 				  data.rx_mcs);
109 		if (!os_snprintf_error(buflen - len, ret))
110 			len += ret;
111 	}
112 	if (data.flags & STA_DRV_DATA_RX_VHT_MCS) {
113 		ret = os_snprintf(buf + len, buflen - len, " vhtmcs %u",
114 				  data.rx_vhtmcs);
115 		if (!os_snprintf_error(buflen - len, ret))
116 			len += ret;
117 	}
118 	if (data.flags & STA_DRV_DATA_RX_VHT_NSS) {
119 		ret = os_snprintf(buf + len, buflen - len, " vhtnss %u",
120 				  data.rx_vht_nss);
121 		if (!os_snprintf_error(buflen - len, ret))
122 			len += ret;
123 	}
124 	if (data.flags & STA_DRV_DATA_RX_SHORT_GI) {
125 		ret = os_snprintf(buf + len, buflen - len, " shortGI");
126 		if (!os_snprintf_error(buflen - len, ret))
127 			len += ret;
128 	}
129 	ret = os_snprintf(buf + len, buflen - len, "\n");
130 	if (!os_snprintf_error(buflen - len, ret))
131 		len += ret;
132 
133 	ret = os_snprintf(buf + len, buflen - len, "tx_rate_info=%lu",
134 			  data.current_tx_rate / 100);
135 	if (os_snprintf_error(buflen - len, ret))
136 		return len;
137 	len += ret;
138 	if (data.flags & STA_DRV_DATA_TX_MCS) {
139 		ret = os_snprintf(buf + len, buflen - len, " mcs %u",
140 				  data.tx_mcs);
141 		if (!os_snprintf_error(buflen - len, ret))
142 			len += ret;
143 	}
144 	if (data.flags & STA_DRV_DATA_TX_VHT_MCS) {
145 		ret = os_snprintf(buf + len, buflen - len, " vhtmcs %u",
146 				  data.tx_vhtmcs);
147 		if (!os_snprintf_error(buflen - len, ret))
148 			len += ret;
149 	}
150 	if (data.flags & STA_DRV_DATA_TX_VHT_NSS) {
151 		ret = os_snprintf(buf + len, buflen - len, " vhtnss %u",
152 				  data.tx_vht_nss);
153 		if (!os_snprintf_error(buflen - len, ret))
154 			len += ret;
155 	}
156 	if (data.flags & STA_DRV_DATA_TX_SHORT_GI) {
157 		ret = os_snprintf(buf + len, buflen - len, " shortGI");
158 		if (!os_snprintf_error(buflen - len, ret))
159 			len += ret;
160 	}
161 	ret = os_snprintf(buf + len, buflen - len, "\n");
162 	if (!os_snprintf_error(buflen - len, ret))
163 		len += ret;
164 
165 	if ((sta->flags & WLAN_STA_VHT) && sta->vht_capabilities) {
166 		ret = os_snprintf(buf + len, buflen - len,
167 				  "rx_vht_mcs_map=%04x\n"
168 				  "tx_vht_mcs_map=%04x\n",
169 				  le_to_host16(sta->vht_capabilities->
170 					       vht_supported_mcs_set.rx_map),
171 				  le_to_host16(sta->vht_capabilities->
172 					       vht_supported_mcs_set.tx_map));
173 		if (!os_snprintf_error(buflen - len, ret))
174 			len += ret;
175 	}
176 
177 	if ((sta->flags & WLAN_STA_HT) && sta->ht_capabilities) {
178 		len = hostapd_write_ht_mcs_bitmask(buf, buflen, len,
179 						   sta->ht_capabilities->
180 						   supported_mcs_set);
181 	}
182 
183 	if (data.flags & STA_DRV_DATA_LAST_ACK_RSSI) {
184 		ret = os_snprintf(buf + len, buflen - len,
185 				  "last_ack_signal=%d\n", data.last_ack_rssi);
186 		if (!os_snprintf_error(buflen - len, ret))
187 			len += ret;
188 	}
189 
190 	len += hostapd_get_sta_conn_time(sta, &data, buf + len, buflen - len);
191 
192 	return len;
193 }
194 
195 
timeout_next_str(int val)196 static const char * timeout_next_str(int val)
197 {
198 	switch (val) {
199 	case STA_NULLFUNC:
200 		return "NULLFUNC POLL";
201 	case STA_DISASSOC:
202 		return "DISASSOC";
203 	case STA_DEAUTH:
204 		return "DEAUTH";
205 	case STA_REMOVE:
206 		return "REMOVE";
207 	case STA_DISASSOC_FROM_CLI:
208 		return "DISASSOC_FROM_CLI";
209 	default:
210 		return "?";
211 	}
212 }
213 
214 
hw_mode_str(enum hostapd_hw_mode mode)215 static const char * hw_mode_str(enum hostapd_hw_mode mode)
216 {
217 	switch (mode) {
218 	case HOSTAPD_MODE_IEEE80211B:
219 		return "b";
220 	case HOSTAPD_MODE_IEEE80211G:
221 		return "g";
222 	case HOSTAPD_MODE_IEEE80211A:
223 		return "a";
224 	case HOSTAPD_MODE_IEEE80211AD:
225 		return "ad";
226 	case HOSTAPD_MODE_IEEE80211ANY:
227 		return "any";
228 	case NUM_HOSTAPD_MODES:
229 		return "invalid";
230 	}
231 	return "unknown";
232 }
233 
234 
hostapd_ctrl_iface_sta_mib(struct hostapd_data * hapd,struct sta_info * sta,char * buf,size_t buflen)235 static int hostapd_ctrl_iface_sta_mib(struct hostapd_data *hapd,
236 				      struct sta_info *sta,
237 				      char *buf, size_t buflen)
238 {
239 	int len, res, ret, i;
240 	const char *keyid;
241 	const u8 *dpp_pkhash;
242 
243 	if (!sta)
244 		return 0;
245 
246 	len = 0;
247 	ret = os_snprintf(buf + len, buflen - len, MACSTR "\nflags=",
248 			  MAC2STR(sta->addr));
249 	if (os_snprintf_error(buflen - len, ret))
250 		return len;
251 	len += ret;
252 
253 	ret = ap_sta_flags_txt(sta->flags, buf + len, buflen - len);
254 	if (ret < 0)
255 		return len;
256 	len += ret;
257 
258 	ret = os_snprintf(buf + len, buflen - len, "\naid=%d\ncapability=0x%x\n"
259 			  "listen_interval=%d\nsupported_rates=",
260 			  sta->aid, sta->capability, sta->listen_interval);
261 	if (os_snprintf_error(buflen - len, ret))
262 		return len;
263 	len += ret;
264 
265 	for (i = 0; i < sta->supported_rates_len; i++) {
266 		ret = os_snprintf(buf + len, buflen - len, "%02x%s",
267 				  sta->supported_rates[i],
268 				  i + 1 < sta->supported_rates_len ? " " : "");
269 		if (os_snprintf_error(buflen - len, ret))
270 			return len;
271 		len += ret;
272 	}
273 
274 	ret = os_snprintf(buf + len, buflen - len, "\ntimeout_next=%s\n",
275 			  timeout_next_str(sta->timeout_next));
276 	if (os_snprintf_error(buflen - len, ret))
277 		return len;
278 	len += ret;
279 
280 	res = ieee802_11_get_mib_sta(hapd, sta, buf + len, buflen - len);
281 	if (res >= 0)
282 		len += res;
283 	res = wpa_get_mib_sta(sta->wpa_sm, buf + len, buflen - len);
284 	if (res >= 0)
285 		len += res;
286 	res = ieee802_1x_get_mib_sta(hapd, sta, buf + len, buflen - len);
287 	if (res >= 0)
288 		len += res;
289 	res = hostapd_wps_get_mib_sta(hapd, sta->addr, buf + len,
290 				      buflen - len);
291 	if (res >= 0)
292 		len += res;
293 	res = hostapd_p2p_get_mib_sta(hapd, sta, buf + len, buflen - len);
294 	if (res >= 0)
295 		len += res;
296 
297 	len += hostapd_get_sta_info(hapd, sta, buf + len, buflen - len);
298 
299 #ifdef CONFIG_SAE
300 	if (sta->sae && sta->sae->state == SAE_ACCEPTED) {
301 		res = os_snprintf(buf + len, buflen - len, "sae_group=%d\n",
302 				  sta->sae->group);
303 		if (!os_snprintf_error(buflen - len, res))
304 			len += res;
305 	}
306 
307 	if (sta->sae && sta->sae->tmp) {
308 		const u8 *pos;
309 		unsigned int j, count;
310 		struct wpabuf *groups = sta->sae->tmp->peer_rejected_groups;
311 
312 		res = os_snprintf(buf + len, buflen - len,
313 				  "sae_rejected_groups=");
314 		if (!os_snprintf_error(buflen - len, res))
315 			len += res;
316 
317 		if (groups) {
318 			pos = wpabuf_head(groups);
319 			count = wpabuf_len(groups) / 2;
320 		} else {
321 			pos = NULL;
322 			count = 0;
323 		}
324 		for (j = 0; pos && j < count; j++) {
325 			res = os_snprintf(buf + len, buflen - len, "%s%d",
326 					  j == 0 ? "" : " ", WPA_GET_LE16(pos));
327 			if (!os_snprintf_error(buflen - len, res))
328 				len += res;
329 			pos += 2;
330 		}
331 
332 		res = os_snprintf(buf + len, buflen - len, "\n");
333 		if (!os_snprintf_error(buflen - len, res))
334 			len += res;
335 	}
336 #endif /* CONFIG_SAE */
337 
338 	if (sta->vlan_id > 0) {
339 		res = os_snprintf(buf + len, buflen - len, "vlan_id=%d\n",
340 				  sta->vlan_id);
341 		if (!os_snprintf_error(buflen - len, res))
342 			len += res;
343 	}
344 
345 	res = mbo_ap_get_info(sta, buf + len, buflen - len);
346 	if (res >= 0)
347 		len += res;
348 
349 	if (sta->supp_op_classes &&
350 	    buflen - len > (unsigned) (17 + 2 * sta->supp_op_classes[0])) {
351 		len += os_snprintf(buf + len, buflen - len, "supp_op_classes=");
352 		len += wpa_snprintf_hex(buf + len, buflen - len,
353 					sta->supp_op_classes + 1,
354 					sta->supp_op_classes[0]);
355 		len += os_snprintf(buf + len, buflen - len, "\n");
356 	}
357 
358 	if (sta->power_capab) {
359 		ret = os_snprintf(buf + len, buflen - len,
360 				  "min_txpower=%d\n"
361 				  "max_txpower=%d\n",
362 				  sta->min_tx_power, sta->max_tx_power);
363 		if (!os_snprintf_error(buflen - len, ret))
364 			len += ret;
365 	}
366 
367 #ifdef CONFIG_IEEE80211AC
368 	if ((sta->flags & WLAN_STA_VHT) && sta->vht_capabilities) {
369 		res = os_snprintf(buf + len, buflen - len,
370 				  "vht_caps_info=0x%08x\n",
371 				  le_to_host32(sta->vht_capabilities->
372 					       vht_capabilities_info));
373 		if (!os_snprintf_error(buflen - len, res))
374 			len += res;
375 	}
376 #endif /* CONFIG_IEEE80211AC */
377 
378 	if ((sta->flags & WLAN_STA_HT) && sta->ht_capabilities) {
379 		res = os_snprintf(buf + len, buflen - len,
380 				  "ht_caps_info=0x%04x\n",
381 				  le_to_host16(sta->ht_capabilities->
382 					       ht_capabilities_info));
383 		if (!os_snprintf_error(buflen - len, res))
384 			len += res;
385 	}
386 
387 	if (sta->ext_capability &&
388 	    buflen - len > (unsigned) (11 + 2 * sta->ext_capability[0])) {
389 		len += os_snprintf(buf + len, buflen - len, "ext_capab=");
390 		len += wpa_snprintf_hex(buf + len, buflen - len,
391 					sta->ext_capability + 1,
392 					sta->ext_capability[0]);
393 		len += os_snprintf(buf + len, buflen - len, "\n");
394 	}
395 
396 	if (sta->flags & WLAN_STA_WDS && sta->ifname_wds) {
397 		ret = os_snprintf(buf + len, buflen - len,
398 				  "wds_sta_ifname=%s\n", sta->ifname_wds);
399 		if (!os_snprintf_error(buflen - len, ret))
400 			len += ret;
401 	}
402 
403 	keyid = ap_sta_wpa_get_keyid(hapd, sta);
404 	if (keyid) {
405 		ret = os_snprintf(buf + len, buflen - len, "keyid=%s\n", keyid);
406 		if (!os_snprintf_error(buflen - len, ret))
407 			len += ret;
408 	}
409 
410 	dpp_pkhash = ap_sta_wpa_get_dpp_pkhash(hapd, sta);
411 	if (dpp_pkhash) {
412 		ret = os_snprintf(buf + len, buflen - len, "dpp_pkhash=");
413 		if (!os_snprintf_error(buflen - len, ret))
414 			len += ret;
415 		len += wpa_snprintf_hex(buf + len, buflen - len, dpp_pkhash,
416 					SHA256_MAC_LEN);
417 		ret = os_snprintf(buf + len, buflen - len, "\n");
418 		if (!os_snprintf_error(buflen - len, ret))
419 			len += ret;
420 	}
421 
422 	return len;
423 }
424 
425 
hostapd_ctrl_iface_sta_first(struct hostapd_data * hapd,char * buf,size_t buflen)426 int hostapd_ctrl_iface_sta_first(struct hostapd_data *hapd,
427 				 char *buf, size_t buflen)
428 {
429 	return hostapd_ctrl_iface_sta_mib(hapd, hapd->sta_list, buf, buflen);
430 }
431 
432 
hostapd_ctrl_iface_sta(struct hostapd_data * hapd,const char * txtaddr,char * buf,size_t buflen)433 int hostapd_ctrl_iface_sta(struct hostapd_data *hapd, const char *txtaddr,
434 			   char *buf, size_t buflen)
435 {
436 	u8 addr[ETH_ALEN];
437 	int ret;
438 	const char *pos;
439 	struct sta_info *sta;
440 
441 	if (hwaddr_aton(txtaddr, addr)) {
442 		ret = os_snprintf(buf, buflen, "FAIL\n");
443 		if (os_snprintf_error(buflen, ret))
444 			return 0;
445 		return ret;
446 	}
447 
448 	sta = ap_get_sta(hapd, addr);
449 	if (sta == NULL)
450 		return -1;
451 
452 	pos = os_strchr(txtaddr, ' ');
453 	if (pos) {
454 		pos++;
455 
456 #ifdef HOSTAPD_DUMP_STATE
457 		if (os_strcmp(pos, "eapol") == 0) {
458 			if (sta->eapol_sm == NULL)
459 				return -1;
460 			return eapol_auth_dump_state(sta->eapol_sm, buf,
461 						     buflen);
462 		}
463 #endif /* HOSTAPD_DUMP_STATE */
464 
465 		return -1;
466 	}
467 
468 	ret = hostapd_ctrl_iface_sta_mib(hapd, sta, buf, buflen);
469 	ret += fst_ctrl_iface_mb_info(addr, buf + ret, buflen - ret);
470 
471 	return ret;
472 }
473 
474 
hostapd_ctrl_iface_sta_next(struct hostapd_data * hapd,const char * txtaddr,char * buf,size_t buflen)475 int hostapd_ctrl_iface_sta_next(struct hostapd_data *hapd, const char *txtaddr,
476 				char *buf, size_t buflen)
477 {
478 	u8 addr[ETH_ALEN];
479 	struct sta_info *sta;
480 	int ret;
481 
482 	if (hwaddr_aton(txtaddr, addr) ||
483 	    (sta = ap_get_sta(hapd, addr)) == NULL) {
484 		ret = os_snprintf(buf, buflen, "FAIL\n");
485 		if (os_snprintf_error(buflen, ret))
486 			return 0;
487 		return ret;
488 	}
489 
490 	if (!sta->next)
491 		return 0;
492 
493 	return hostapd_ctrl_iface_sta_mib(hapd, sta->next, buf, buflen);
494 }
495 
496 
497 #ifdef CONFIG_P2P_MANAGER
p2p_manager_disconnect(struct hostapd_data * hapd,u16 stype,u8 minor_reason_code,const u8 * addr)498 static int p2p_manager_disconnect(struct hostapd_data *hapd, u16 stype,
499 				  u8 minor_reason_code, const u8 *addr)
500 {
501 	struct ieee80211_mgmt *mgmt;
502 	int ret;
503 	u8 *pos;
504 
505 	mgmt = os_zalloc(sizeof(*mgmt) + 100);
506 	if (mgmt == NULL)
507 		return -1;
508 
509 	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, stype);
510 	wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "P2P: Disconnect STA " MACSTR
511 		" with minor reason code %u (stype=%u (%s))",
512 		MAC2STR(addr), minor_reason_code, stype,
513 		fc2str(le_to_host16(mgmt->frame_control)));
514 
515 	os_memcpy(mgmt->da, addr, ETH_ALEN);
516 	os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
517 	os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
518 	if (stype == WLAN_FC_STYPE_DEAUTH) {
519 		mgmt->u.deauth.reason_code =
520 			host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
521 		pos = mgmt->u.deauth.variable;
522 	} else {
523 		mgmt->u.disassoc.reason_code =
524 			host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
525 		pos = mgmt->u.disassoc.variable;
526 	}
527 
528 	*pos++ = WLAN_EID_VENDOR_SPECIFIC;
529 	*pos++ = 4 + 3 + 1;
530 	WPA_PUT_BE32(pos, P2P_IE_VENDOR_TYPE);
531 	pos += 4;
532 
533 	*pos++ = P2P_ATTR_MINOR_REASON_CODE;
534 	WPA_PUT_LE16(pos, 1);
535 	pos += 2;
536 	*pos++ = minor_reason_code;
537 
538 	ret = hostapd_drv_send_mlme(hapd, mgmt, pos - (u8 *) mgmt, 0, NULL, 0,
539 				    0);
540 	os_free(mgmt);
541 
542 	return ret < 0 ? -1 : 0;
543 }
544 #endif /* CONFIG_P2P_MANAGER */
545 
546 
hostapd_ctrl_iface_deauthenticate(struct hostapd_data * hapd,const char * txtaddr)547 int hostapd_ctrl_iface_deauthenticate(struct hostapd_data *hapd,
548 				      const char *txtaddr)
549 {
550 	u8 addr[ETH_ALEN];
551 	struct sta_info *sta;
552 	const char *pos;
553 	u16 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
554 
555 	wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DEAUTHENTICATE %s",
556 		txtaddr);
557 
558 	if (hwaddr_aton(txtaddr, addr))
559 		return -1;
560 
561 	pos = os_strstr(txtaddr, " reason=");
562 	if (pos)
563 		reason = atoi(pos + 8);
564 
565 	pos = os_strstr(txtaddr, " test=");
566 	if (pos) {
567 		struct ieee80211_mgmt mgmt;
568 		int encrypt;
569 
570 		pos += 6;
571 		encrypt = atoi(pos);
572 		os_memset(&mgmt, 0, sizeof(mgmt));
573 		mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
574 						  WLAN_FC_STYPE_DEAUTH);
575 		os_memcpy(mgmt.da, addr, ETH_ALEN);
576 		os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
577 		os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
578 		mgmt.u.deauth.reason_code = host_to_le16(reason);
579 		if (hostapd_drv_send_mlme(hapd, (u8 *) &mgmt,
580 					  IEEE80211_HDRLEN +
581 					  sizeof(mgmt.u.deauth),
582 					  0, NULL, 0, !encrypt) < 0)
583 			return -1;
584 		return 0;
585 	}
586 
587 #ifdef CONFIG_P2P_MANAGER
588 	pos = os_strstr(txtaddr, " p2p=");
589 	if (pos) {
590 		return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DEAUTH,
591 					      atoi(pos + 5), addr);
592 	}
593 #endif /* CONFIG_P2P_MANAGER */
594 
595 	if (os_strstr(txtaddr, " tx=0"))
596 		hostapd_drv_sta_remove(hapd, addr);
597 	else
598 		hostapd_drv_sta_deauth(hapd, addr, reason);
599 	sta = ap_get_sta(hapd, addr);
600 	if (sta)
601 		ap_sta_deauthenticate(hapd, sta, reason);
602 	else if (addr[0] == 0xff)
603 		hostapd_free_stas(hapd);
604 
605 	return 0;
606 }
607 
608 
hostapd_ctrl_iface_disassociate(struct hostapd_data * hapd,const char * txtaddr)609 int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd,
610 				    const char *txtaddr)
611 {
612 	u8 addr[ETH_ALEN];
613 	struct sta_info *sta;
614 	const char *pos;
615 	u16 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
616 
617 	wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DISASSOCIATE %s",
618 		txtaddr);
619 
620 	if (hwaddr_aton(txtaddr, addr))
621 		return -1;
622 
623 	pos = os_strstr(txtaddr, " reason=");
624 	if (pos)
625 		reason = atoi(pos + 8);
626 
627 	pos = os_strstr(txtaddr, " test=");
628 	if (pos) {
629 		struct ieee80211_mgmt mgmt;
630 		int encrypt;
631 
632 		pos += 6;
633 		encrypt = atoi(pos);
634 		os_memset(&mgmt, 0, sizeof(mgmt));
635 		mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
636 						  WLAN_FC_STYPE_DISASSOC);
637 		os_memcpy(mgmt.da, addr, ETH_ALEN);
638 		os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
639 		os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
640 		mgmt.u.disassoc.reason_code = host_to_le16(reason);
641 		if (hostapd_drv_send_mlme(hapd, (u8 *) &mgmt,
642 					  IEEE80211_HDRLEN +
643 					  sizeof(mgmt.u.deauth),
644 					  0, NULL, 0, !encrypt) < 0)
645 			return -1;
646 		return 0;
647 	}
648 
649 #ifdef CONFIG_P2P_MANAGER
650 	pos = os_strstr(txtaddr, " p2p=");
651 	if (pos) {
652 		return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DISASSOC,
653 					      atoi(pos + 5), addr);
654 	}
655 #endif /* CONFIG_P2P_MANAGER */
656 
657 	if (os_strstr(txtaddr, " tx=0"))
658 		hostapd_drv_sta_remove(hapd, addr);
659 	else
660 		hostapd_drv_sta_disassoc(hapd, addr, reason);
661 	sta = ap_get_sta(hapd, addr);
662 	if (sta)
663 		ap_sta_disassociate(hapd, sta, reason);
664 	else if (addr[0] == 0xff)
665 		hostapd_free_stas(hapd);
666 
667 	return 0;
668 }
669 
670 
671 #ifdef CONFIG_TAXONOMY
hostapd_ctrl_iface_signature(struct hostapd_data * hapd,const char * txtaddr,char * buf,size_t buflen)672 int hostapd_ctrl_iface_signature(struct hostapd_data *hapd,
673 				 const char *txtaddr,
674 				 char *buf, size_t buflen)
675 {
676 	u8 addr[ETH_ALEN];
677 	struct sta_info *sta;
678 
679 	wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE SIGNATURE %s", txtaddr);
680 
681 	if (hwaddr_aton(txtaddr, addr))
682 		return -1;
683 
684 	sta = ap_get_sta(hapd, addr);
685 	if (!sta)
686 		return -1;
687 
688 	return retrieve_sta_taxonomy(hapd, sta, buf, buflen);
689 }
690 #endif /* CONFIG_TAXONOMY */
691 
692 
hostapd_ctrl_iface_poll_sta(struct hostapd_data * hapd,const char * txtaddr)693 int hostapd_ctrl_iface_poll_sta(struct hostapd_data *hapd,
694 				const char *txtaddr)
695 {
696 	u8 addr[ETH_ALEN];
697 	struct sta_info *sta;
698 
699 	wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE POLL_STA %s", txtaddr);
700 
701 	if (hwaddr_aton(txtaddr, addr))
702 		return -1;
703 
704 	sta = ap_get_sta(hapd, addr);
705 	if (!sta)
706 		return -1;
707 
708 	hostapd_drv_poll_client(hapd, hapd->own_addr, addr,
709 				sta->flags & WLAN_STA_WMM);
710 	return 0;
711 }
712 
713 
hostapd_ctrl_iface_status(struct hostapd_data * hapd,char * buf,size_t buflen)714 int hostapd_ctrl_iface_status(struct hostapd_data *hapd, char *buf,
715 			      size_t buflen)
716 {
717 	struct hostapd_iface *iface = hapd->iface;
718 	struct hostapd_hw_modes *mode = iface->current_mode;
719 	struct hostapd_config *iconf = hapd->iconf;
720 	int len = 0, ret, j;
721 	size_t i;
722 
723 	ret = os_snprintf(buf + len, buflen - len,
724 			  "state=%s\n"
725 			  "phy=%s\n"
726 			  "freq=%d\n"
727 			  "num_sta_non_erp=%d\n"
728 			  "num_sta_no_short_slot_time=%d\n"
729 			  "num_sta_no_short_preamble=%d\n"
730 			  "olbc=%d\n"
731 			  "num_sta_ht_no_gf=%d\n"
732 			  "num_sta_no_ht=%d\n"
733 			  "num_sta_ht_20_mhz=%d\n"
734 			  "num_sta_ht40_intolerant=%d\n"
735 			  "olbc_ht=%d\n"
736 			  "ht_op_mode=0x%x\n",
737 			  hostapd_state_text(iface->state),
738 			  iface->phy,
739 			  iface->freq,
740 			  iface->num_sta_non_erp,
741 			  iface->num_sta_no_short_slot_time,
742 			  iface->num_sta_no_short_preamble,
743 			  iface->olbc,
744 			  iface->num_sta_ht_no_gf,
745 			  iface->num_sta_no_ht,
746 			  iface->num_sta_ht_20mhz,
747 			  iface->num_sta_ht40_intolerant,
748 			  iface->olbc_ht,
749 			  iface->ht_op_mode);
750 	if (os_snprintf_error(buflen - len, ret))
751 		return len;
752 	len += ret;
753 
754 	if (mode) {
755 		ret = os_snprintf(buf + len, buflen - len, "hw_mode=%s\n",
756 				  hw_mode_str(mode->mode));
757 		if (os_snprintf_error(buflen - len, ret))
758 			return len;
759 		len += ret;
760 	}
761 
762 	if (iconf->country[0] && iconf->country[1]) {
763 		ret = os_snprintf(buf + len, buflen - len,
764 				  "country_code=%c%c\ncountry3=0x%X\n",
765 				  iconf->country[0], iconf->country[1],
766 				  iconf->country[2]);
767 		if (os_snprintf_error(buflen - len, ret))
768 			return len;
769 		len += ret;
770 	}
771 
772 	if (!iface->cac_started || !iface->dfs_cac_ms) {
773 		ret = os_snprintf(buf + len, buflen - len,
774 				  "cac_time_seconds=%d\n"
775 				  "cac_time_left_seconds=N/A\n",
776 				  iface->dfs_cac_ms / 1000);
777 	} else {
778 		/* CAC started and CAC time set - calculate remaining time */
779 		struct os_reltime now;
780 		long left_time;
781 
782 		os_reltime_age(&iface->dfs_cac_start, &now);
783 		left_time = (long) iface->dfs_cac_ms / 1000 - now.sec;
784 		ret = os_snprintf(buf + len, buflen - len,
785 				  "cac_time_seconds=%u\n"
786 				  "cac_time_left_seconds=%lu\n",
787 				  iface->dfs_cac_ms / 1000,
788 				  left_time > 0 ? left_time : 0);
789 	}
790 	if (os_snprintf_error(buflen - len, ret))
791 		return len;
792 	len += ret;
793 
794 	ret = os_snprintf(buf + len, buflen - len,
795 			  "channel=%u\n"
796 			  "edmg_enable=%d\n"
797 			  "edmg_channel=%d\n"
798 			  "secondary_channel=%d\n"
799 			  "ieee80211n=%d\n"
800 			  "ieee80211ac=%d\n"
801 			  "ieee80211ax=%d\n"
802 			  "ieee80211be=%d\n"
803 			  "beacon_int=%u\n"
804 			  "dtim_period=%d\n",
805 			  iface->conf->channel,
806 			  iface->conf->enable_edmg,
807 			  iface->conf->edmg_channel,
808 			  iface->conf->ieee80211n && !hapd->conf->disable_11n ?
809 			  iface->conf->secondary_channel : 0,
810 			  iface->conf->ieee80211n && !hapd->conf->disable_11n,
811 			  iface->conf->ieee80211ac &&
812 			  !hapd->conf->disable_11ac,
813 			  iface->conf->ieee80211ax &&
814 			  !hapd->conf->disable_11ax,
815 			  iface->conf->ieee80211be &&
816 			  !hapd->conf->disable_11be,
817 			  iface->conf->beacon_int,
818 			  hapd->conf->dtim_period);
819 	if (os_snprintf_error(buflen - len, ret))
820 		return len;
821 	len += ret;
822 
823 #ifdef CONFIG_IEEE80211BE
824 	if (iface->conf->ieee80211be && !hapd->conf->disable_11be) {
825 		ret = os_snprintf(buf + len, buflen - len,
826 				  "eht_oper_chwidth=%d\n"
827 				  "eht_oper_centr_freq_seg0_idx=%d\n",
828 				  iface->conf->eht_oper_chwidth,
829 				  iface->conf->eht_oper_centr_freq_seg0_idx);
830 		if (os_snprintf_error(buflen - len, ret))
831 			return len;
832 		len += ret;
833 	}
834 #endif /* CONFIG_IEEE80211BE */
835 
836 #ifdef CONFIG_IEEE80211AX
837 	if (iface->conf->ieee80211ax && !hapd->conf->disable_11ax) {
838 		ret = os_snprintf(buf + len, buflen - len,
839 				  "he_oper_chwidth=%d\n"
840 				  "he_oper_centr_freq_seg0_idx=%d\n"
841 				  "he_oper_centr_freq_seg1_idx=%d\n",
842 				  iface->conf->he_oper_chwidth,
843 				  iface->conf->he_oper_centr_freq_seg0_idx,
844 				  iface->conf->he_oper_centr_freq_seg1_idx);
845 		if (os_snprintf_error(buflen - len, ret))
846 			return len;
847 		len += ret;
848 
849 		if (!iconf->he_op.he_bss_color_disabled &&
850 		    iconf->he_op.he_bss_color) {
851 			ret = os_snprintf(buf + len, buflen - len,
852 					  "he_bss_color=%d\n",
853 					  iconf->he_op.he_bss_color);
854 			if (os_snprintf_error(buflen - len, ret))
855 				return len;
856 			len += ret;
857 		}
858 	}
859 #endif /* CONFIG_IEEE80211AX */
860 
861 	if (iface->conf->ieee80211ac && !hapd->conf->disable_11ac) {
862 		ret = os_snprintf(buf + len, buflen - len,
863 				  "vht_oper_chwidth=%d\n"
864 				  "vht_oper_centr_freq_seg0_idx=%d\n"
865 				  "vht_oper_centr_freq_seg1_idx=%d\n"
866 				  "vht_caps_info=%08x\n",
867 				  iface->conf->vht_oper_chwidth,
868 				  iface->conf->vht_oper_centr_freq_seg0_idx,
869 				  iface->conf->vht_oper_centr_freq_seg1_idx,
870 				  iface->conf->vht_capab);
871 		if (os_snprintf_error(buflen - len, ret))
872 			return len;
873 		len += ret;
874 	}
875 
876 	if (iface->conf->ieee80211ac && !hapd->conf->disable_11ac && mode) {
877 		u16 rxmap = WPA_GET_LE16(&mode->vht_mcs_set[0]);
878 		u16 txmap = WPA_GET_LE16(&mode->vht_mcs_set[4]);
879 
880 		ret = os_snprintf(buf + len, buflen - len,
881 				  "rx_vht_mcs_map=%04x\n"
882 				  "tx_vht_mcs_map=%04x\n",
883 				  rxmap, txmap);
884 		if (os_snprintf_error(buflen - len, ret))
885 			return len;
886 		len += ret;
887 	}
888 
889 	if (iface->conf->ieee80211n && !hapd->conf->disable_11n) {
890 		ret = os_snprintf(buf + len, buflen - len,
891 				  "ht_caps_info=%04x\n",
892 				  hapd->iconf->ht_capab);
893 		if (os_snprintf_error(buflen - len, ret))
894 			return len;
895 		len += ret;
896 	}
897 
898 	if (iface->conf->ieee80211n && !hapd->conf->disable_11n && mode) {
899 		len = hostapd_write_ht_mcs_bitmask(buf, buflen, len,
900 						   mode->mcs_set);
901 	}
902 
903 	if (iface->current_rates && iface->num_rates) {
904 		ret = os_snprintf(buf + len, buflen - len, "supported_rates=");
905 		if (os_snprintf_error(buflen - len, ret))
906 			return len;
907 		len += ret;
908 
909 		for (j = 0; j < iface->num_rates; j++) {
910 			ret = os_snprintf(buf + len, buflen - len, "%s%02x",
911 					  j > 0 ? " " : "",
912 					  iface->current_rates[j].rate / 5);
913 			if (os_snprintf_error(buflen - len, ret))
914 				return len;
915 			len += ret;
916 		}
917 		ret = os_snprintf(buf + len, buflen - len, "\n");
918 		if (os_snprintf_error(buflen - len, ret))
919 			return len;
920 		len += ret;
921 	}
922 
923 	for (j = 0; mode && j < mode->num_channels; j++) {
924 		if (mode->channels[j].freq == iface->freq) {
925 			ret = os_snprintf(buf + len, buflen - len,
926 					  "max_txpower=%u\n",
927 					  mode->channels[j].max_tx_power);
928 			if (os_snprintf_error(buflen - len, ret))
929 				return len;
930 			len += ret;
931 			break;
932 		}
933 	}
934 
935 	for (i = 0; i < iface->num_bss; i++) {
936 		struct hostapd_data *bss = iface->bss[i];
937 		ret = os_snprintf(buf + len, buflen - len,
938 				  "bss[%d]=%s\n"
939 				  "bssid[%d]=" MACSTR "\n"
940 				  "ssid[%d]=%s\n"
941 				  "num_sta[%d]=%d\n",
942 				  (int) i, bss->conf->iface,
943 				  (int) i, MAC2STR(bss->own_addr),
944 				  (int) i,
945 				  wpa_ssid_txt(bss->conf->ssid.ssid,
946 					       bss->conf->ssid.ssid_len),
947 				  (int) i, bss->num_sta);
948 		if (os_snprintf_error(buflen - len, ret))
949 			return len;
950 		len += ret;
951 
952 #ifdef CONFIG_IEEE80211BE
953 		if (bss->conf->mld_ap) {
954 			ret = os_snprintf(buf + len, buflen - len,
955 					  "mld_addr[%d]=" MACSTR "\n"
956 					  "mld_id[%d]=%d\n"
957 					  "mld_link_id[%d]=%d\n",
958 					  (int) i, MAC2STR(bss->mld_addr),
959 					  (int) i, bss->conf->mld_id,
960 					  (int) i, bss->mld_link_id);
961 			if (os_snprintf_error(buflen - len, ret))
962 				return len;
963 			len += ret;
964 		}
965 #endif /* CONFIG_IEEE80211BE */
966 	}
967 
968 	if (hapd->conf->chan_util_avg_period) {
969 		ret = os_snprintf(buf + len, buflen - len,
970 				  "chan_util_avg=%u\n",
971 				  iface->chan_util_average);
972 		if (os_snprintf_error(buflen - len, ret))
973 			return len;
974 		len += ret;
975 	}
976 
977 	return len;
978 }
979 
980 
hostapd_parse_csa_settings(const char * pos,struct csa_settings * settings)981 int hostapd_parse_csa_settings(const char *pos,
982 			       struct csa_settings *settings)
983 {
984 	char *end;
985 
986 	os_memset(settings, 0, sizeof(*settings));
987 	settings->cs_count = strtol(pos, &end, 10);
988 	if (pos == end) {
989 		wpa_printf(MSG_ERROR, "chanswitch: invalid cs_count provided");
990 		return -1;
991 	}
992 
993 	settings->freq_params.freq = atoi(end);
994 	if (settings->freq_params.freq == 0) {
995 		wpa_printf(MSG_ERROR, "chanswitch: invalid freq provided");
996 		return -1;
997 	}
998 
999 #define SET_CSA_SETTING(str) \
1000 	do { \
1001 		const char *pos2 = os_strstr(pos, " " #str "="); \
1002 		if (pos2) { \
1003 			pos2 += sizeof(" " #str "=") - 1; \
1004 			settings->freq_params.str = atoi(pos2); \
1005 		} \
1006 	} while (0)
1007 
1008 #define SET_CSA_SETTING_EXT(str) \
1009 	do { \
1010 		const char *pos2 = os_strstr(pos, " " #str "="); \
1011 		if (pos2) { \
1012 			pos2 += sizeof(" " #str "=") - 1; \
1013 			settings->str = atoi(pos2); \
1014 		} \
1015 	} while (0)
1016 
1017 	SET_CSA_SETTING(center_freq1);
1018 	SET_CSA_SETTING(center_freq2);
1019 	SET_CSA_SETTING(bandwidth);
1020 	SET_CSA_SETTING(sec_channel_offset);
1021 	SET_CSA_SETTING_EXT(punct_bitmap);
1022 	settings->freq_params.ht_enabled = !!os_strstr(pos, " ht");
1023 	settings->freq_params.vht_enabled = !!os_strstr(pos, " vht");
1024 	settings->freq_params.he_enabled = !!os_strstr(pos, " he");
1025 	settings->freq_params.eht_enabled = !!os_strstr(pos, " eht");
1026 	settings->block_tx = !!os_strstr(pos, " blocktx");
1027 #undef SET_CSA_SETTING
1028 #undef SET_CSA_SETTING_EXT
1029 
1030 	return 0;
1031 }
1032 
1033 
hostapd_ctrl_iface_stop_ap(struct hostapd_data * hapd)1034 int hostapd_ctrl_iface_stop_ap(struct hostapd_data *hapd)
1035 {
1036 	return hostapd_drv_stop_ap(hapd);
1037 }
1038 
1039 
hostapd_ctrl_iface_pmksa_list(struct hostapd_data * hapd,char * buf,size_t len)1040 int hostapd_ctrl_iface_pmksa_list(struct hostapd_data *hapd, char *buf,
1041 				  size_t len)
1042 {
1043 	return wpa_auth_pmksa_list(hapd->wpa_auth, buf, len);
1044 }
1045 
1046 
hostapd_ctrl_iface_pmksa_flush(struct hostapd_data * hapd)1047 void hostapd_ctrl_iface_pmksa_flush(struct hostapd_data *hapd)
1048 {
1049 	wpa_auth_pmksa_flush(hapd->wpa_auth);
1050 }
1051 
1052 
hostapd_ctrl_iface_pmksa_add(struct hostapd_data * hapd,char * cmd)1053 int hostapd_ctrl_iface_pmksa_add(struct hostapd_data *hapd, char *cmd)
1054 {
1055 	u8 spa[ETH_ALEN];
1056 	u8 pmkid[PMKID_LEN];
1057 	u8 pmk[PMK_LEN_MAX];
1058 	size_t pmk_len;
1059 	char *pos, *pos2;
1060 	int akmp = 0, expiration = 0;
1061 
1062 	/*
1063 	 * Entry format:
1064 	 * <STA addr> <PMKID> <PMK> <expiration in seconds> <akmp>
1065 	 */
1066 
1067 	if (hwaddr_aton(cmd, spa))
1068 		return -1;
1069 
1070 	pos = os_strchr(cmd, ' ');
1071 	if (!pos)
1072 		return -1;
1073 	pos++;
1074 
1075 	if (hexstr2bin(pos, pmkid, PMKID_LEN) < 0)
1076 		return -1;
1077 
1078 	pos = os_strchr(pos, ' ');
1079 	if (!pos)
1080 		return -1;
1081 	pos++;
1082 
1083 	pos2 = os_strchr(pos, ' ');
1084 	if (!pos2)
1085 		return -1;
1086 	pmk_len = (pos2 - pos) / 2;
1087 	if (pmk_len < PMK_LEN || pmk_len > PMK_LEN_MAX ||
1088 	    hexstr2bin(pos, pmk, pmk_len) < 0)
1089 		return -1;
1090 
1091 	pos = pos2 + 1;
1092 
1093 	if (sscanf(pos, "%d %d", &expiration, &akmp) != 2)
1094 		return -1;
1095 
1096 	return wpa_auth_pmksa_add2(hapd->wpa_auth, spa, pmk, pmk_len,
1097 				   pmkid, expiration, akmp);
1098 }
1099 
1100 
1101 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
1102 #ifdef CONFIG_MESH
1103 
hostapd_ctrl_iface_pmksa_list_mesh(struct hostapd_data * hapd,const u8 * addr,char * buf,size_t len)1104 int hostapd_ctrl_iface_pmksa_list_mesh(struct hostapd_data *hapd,
1105 				       const u8 *addr, char *buf, size_t len)
1106 {
1107 	return wpa_auth_pmksa_list_mesh(hapd->wpa_auth, addr, buf, len);
1108 }
1109 
1110 
hostapd_ctrl_iface_pmksa_create_entry(const u8 * aa,char * cmd)1111 void * hostapd_ctrl_iface_pmksa_create_entry(const u8 *aa, char *cmd)
1112 {
1113 	u8 spa[ETH_ALEN];
1114 	u8 pmkid[PMKID_LEN];
1115 	u8 pmk[PMK_LEN_MAX];
1116 	char *pos;
1117 	int expiration;
1118 
1119 	/*
1120 	 * Entry format:
1121 	 * <BSSID> <PMKID> <PMK> <expiration in seconds>
1122 	 */
1123 
1124 	if (hwaddr_aton(cmd, spa))
1125 		return NULL;
1126 
1127 	pos = os_strchr(cmd, ' ');
1128 	if (!pos)
1129 		return NULL;
1130 	pos++;
1131 
1132 	if (hexstr2bin(pos, pmkid, PMKID_LEN) < 0)
1133 		return NULL;
1134 
1135 	pos = os_strchr(pos, ' ');
1136 	if (!pos)
1137 		return NULL;
1138 	pos++;
1139 
1140 	if (hexstr2bin(pos, pmk, PMK_LEN) < 0)
1141 		return NULL;
1142 
1143 	pos = os_strchr(pos, ' ');
1144 	if (!pos)
1145 		return NULL;
1146 	pos++;
1147 
1148 	if (sscanf(pos, "%d", &expiration) != 1)
1149 		return NULL;
1150 
1151 	return wpa_auth_pmksa_create_entry(aa, spa, pmk, PMK_LEN,
1152 					   WPA_KEY_MGMT_SAE, pmkid, expiration);
1153 }
1154 
1155 #endif /* CONFIG_MESH */
1156 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
1157 
1158 
1159 #ifdef CONFIG_WNM_AP
1160 
hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data * hapd,const char * cmd)1161 int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
1162 					 const char *cmd)
1163 {
1164 	u8 addr[ETH_ALEN];
1165 	int disassoc_timer;
1166 	struct sta_info *sta;
1167 
1168 	if (hwaddr_aton(cmd, addr))
1169 		return -1;
1170 	if (cmd[17] != ' ')
1171 		return -1;
1172 	disassoc_timer = atoi(cmd + 17);
1173 
1174 	sta = ap_get_sta(hapd, addr);
1175 	if (sta == NULL) {
1176 		wpa_printf(MSG_DEBUG, "Station " MACSTR
1177 			   " not found for disassociation imminent message",
1178 			   MAC2STR(addr));
1179 		return -1;
1180 	}
1181 
1182 	return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
1183 }
1184 
1185 
hostapd_ctrl_iface_ess_disassoc(struct hostapd_data * hapd,const char * cmd)1186 int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
1187 				    const char *cmd)
1188 {
1189 	u8 addr[ETH_ALEN];
1190 	const char *url, *timerstr;
1191 	int disassoc_timer;
1192 	struct sta_info *sta;
1193 
1194 	if (hwaddr_aton(cmd, addr))
1195 		return -1;
1196 
1197 	sta = ap_get_sta(hapd, addr);
1198 	if (sta == NULL) {
1199 		wpa_printf(MSG_DEBUG, "Station " MACSTR
1200 			   " not found for ESS disassociation imminent message",
1201 			   MAC2STR(addr));
1202 		return -1;
1203 	}
1204 
1205 	timerstr = cmd + 17;
1206 	if (*timerstr != ' ')
1207 		return -1;
1208 	timerstr++;
1209 	disassoc_timer = atoi(timerstr);
1210 	if (disassoc_timer < 0 || disassoc_timer > 65535)
1211 		return -1;
1212 
1213 	url = os_strchr(timerstr, ' ');
1214 	if (url == NULL)
1215 		return -1;
1216 	url++;
1217 
1218 	return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
1219 }
1220 
1221 
hostapd_ctrl_iface_bss_tm_req(struct hostapd_data * hapd,const char * cmd)1222 int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data *hapd,
1223 				  const char *cmd)
1224 {
1225 	u8 addr[ETH_ALEN];
1226 	const char *pos, *end;
1227 	int disassoc_timer = 0;
1228 	struct sta_info *sta;
1229 	u8 req_mode = 0, valid_int = 0x01, dialog_token = 0x01;
1230 	u8 bss_term_dur[12];
1231 	char *url = NULL;
1232 	int ret;
1233 	u8 nei_rep[1000];
1234 	int nei_len;
1235 	u8 mbo[10];
1236 	size_t mbo_len = 0;
1237 
1238 	if (hwaddr_aton(cmd, addr)) {
1239 		wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
1240 		return -1;
1241 	}
1242 
1243 	sta = ap_get_sta(hapd, addr);
1244 	if (sta == NULL) {
1245 		wpa_printf(MSG_DEBUG, "Station " MACSTR
1246 			   " not found for BSS TM Request message",
1247 			   MAC2STR(addr));
1248 		return -1;
1249 	}
1250 
1251 	pos = os_strstr(cmd, " disassoc_timer=");
1252 	if (pos) {
1253 		pos += 16;
1254 		disassoc_timer = atoi(pos);
1255 		if (disassoc_timer < 0 || disassoc_timer > 65535) {
1256 			wpa_printf(MSG_DEBUG, "Invalid disassoc_timer");
1257 			return -1;
1258 		}
1259 	}
1260 
1261 	pos = os_strstr(cmd, " valid_int=");
1262 	if (pos) {
1263 		pos += 11;
1264 		valid_int = atoi(pos);
1265 	}
1266 
1267 	pos = os_strstr(cmd, " dialog_token=");
1268 	if (pos) {
1269 		pos += 14;
1270 		dialog_token = atoi(pos);
1271 	}
1272 
1273 	pos = os_strstr(cmd, " bss_term=");
1274 	if (pos) {
1275 		pos += 10;
1276 		req_mode |= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED;
1277 		/* TODO: TSF configurable/learnable */
1278 		bss_term_dur[0] = 4; /* Subelement ID */
1279 		bss_term_dur[1] = 10; /* Length */
1280 		os_memset(&bss_term_dur[2], 0, 8);
1281 		end = os_strchr(pos, ',');
1282 		if (end == NULL) {
1283 			wpa_printf(MSG_DEBUG, "Invalid bss_term data");
1284 			return -1;
1285 		}
1286 		end++;
1287 		WPA_PUT_LE16(&bss_term_dur[10], atoi(end));
1288 	}
1289 
1290 	nei_len = ieee802_11_parse_candidate_list(cmd, nei_rep,
1291 						  sizeof(nei_rep));
1292 	if (nei_len < 0)
1293 		return -1;
1294 
1295 	pos = os_strstr(cmd, " url=");
1296 	if (pos) {
1297 		size_t len;
1298 		pos += 5;
1299 		end = os_strchr(pos, ' ');
1300 		if (end)
1301 			len = end - pos;
1302 		else
1303 			len = os_strlen(pos);
1304 		url = os_malloc(len + 1);
1305 		if (url == NULL)
1306 			return -1;
1307 		os_memcpy(url, pos, len);
1308 		url[len] = '\0';
1309 		req_mode |= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
1310 	}
1311 
1312 	if (os_strstr(cmd, " pref=1"))
1313 		req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
1314 	if (os_strstr(cmd, " abridged=1"))
1315 		req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
1316 	if (os_strstr(cmd, " disassoc_imminent=1"))
1317 		req_mode |= WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
1318 
1319 #ifdef CONFIG_MBO
1320 	pos = os_strstr(cmd, "mbo=");
1321 	if (pos) {
1322 		unsigned int mbo_reason, cell_pref, reassoc_delay;
1323 		u8 *mbo_pos = mbo;
1324 
1325 		ret = sscanf(pos, "mbo=%u:%u:%u", &mbo_reason,
1326 			     &reassoc_delay, &cell_pref);
1327 		if (ret != 3) {
1328 			wpa_printf(MSG_DEBUG,
1329 				   "MBO requires three arguments: mbo=<reason>:<reassoc_delay>:<cell_pref>");
1330 			ret = -1;
1331 			goto fail;
1332 		}
1333 
1334 		if (mbo_reason > MBO_TRANSITION_REASON_PREMIUM_AP) {
1335 			wpa_printf(MSG_DEBUG,
1336 				   "Invalid MBO transition reason code %u",
1337 				   mbo_reason);
1338 			ret = -1;
1339 			goto fail;
1340 		}
1341 
1342 		/* Valid values for Cellular preference are: 0, 1, 255 */
1343 		if (cell_pref != 0 && cell_pref != 1 && cell_pref != 255) {
1344 			wpa_printf(MSG_DEBUG,
1345 				   "Invalid MBO cellular capability %u",
1346 				   cell_pref);
1347 			ret = -1;
1348 			goto fail;
1349 		}
1350 
1351 		if (reassoc_delay > 65535 ||
1352 		    (reassoc_delay &&
1353 		     !(req_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT))) {
1354 			wpa_printf(MSG_DEBUG,
1355 				   "MBO: Assoc retry delay is only valid in disassoc imminent mode");
1356 			ret = -1;
1357 			goto fail;
1358 		}
1359 
1360 		*mbo_pos++ = MBO_ATTR_ID_TRANSITION_REASON;
1361 		*mbo_pos++ = 1;
1362 		*mbo_pos++ = mbo_reason;
1363 		*mbo_pos++ = MBO_ATTR_ID_CELL_DATA_PREF;
1364 		*mbo_pos++ = 1;
1365 		*mbo_pos++ = cell_pref;
1366 
1367 		if (reassoc_delay) {
1368 			*mbo_pos++ = MBO_ATTR_ID_ASSOC_RETRY_DELAY;
1369 			*mbo_pos++ = 2;
1370 			WPA_PUT_LE16(mbo_pos, reassoc_delay);
1371 			mbo_pos += 2;
1372 		}
1373 
1374 		mbo_len = mbo_pos - mbo;
1375 	}
1376 #endif /* CONFIG_MBO */
1377 
1378 	ret = wnm_send_bss_tm_req(hapd, sta, req_mode, disassoc_timer,
1379 				  valid_int, bss_term_dur, dialog_token, url,
1380 				  nei_len ? nei_rep : NULL, nei_len,
1381 				  mbo_len ? mbo : NULL, mbo_len);
1382 #ifdef CONFIG_MBO
1383 fail:
1384 #endif /* CONFIG_MBO */
1385 	os_free(url);
1386 	return ret;
1387 }
1388 
1389 #endif /* CONFIG_WNM_AP */
1390 
1391 
hostapd_ctrl_iface_acl_del_mac(struct mac_acl_entry ** acl,int * num,const char * txtaddr)1392 int hostapd_ctrl_iface_acl_del_mac(struct mac_acl_entry **acl, int *num,
1393 				   const char *txtaddr)
1394 {
1395 	u8 addr[ETH_ALEN];
1396 	struct vlan_description vlan_id;
1397 
1398 	if (!(*num))
1399 		return 0;
1400 
1401 	if (hwaddr_aton(txtaddr, addr))
1402 		return -1;
1403 
1404 	if (hostapd_maclist_found(*acl, *num, addr, &vlan_id))
1405 		hostapd_remove_acl_mac(acl, num, addr);
1406 
1407 	return 0;
1408 }
1409 
1410 
hostapd_ctrl_iface_acl_clear_list(struct mac_acl_entry ** acl,int * num)1411 void hostapd_ctrl_iface_acl_clear_list(struct mac_acl_entry **acl,
1412 				       int *num)
1413 {
1414 	while (*num)
1415 		hostapd_remove_acl_mac(acl, num, (*acl)[0].addr);
1416 }
1417 
1418 
hostapd_ctrl_iface_acl_show_mac(struct mac_acl_entry * acl,int num,char * buf,size_t buflen)1419 int hostapd_ctrl_iface_acl_show_mac(struct mac_acl_entry *acl, int num,
1420 				    char *buf, size_t buflen)
1421 {
1422 	int i = 0, len = 0, ret = 0;
1423 
1424 	if (!acl)
1425 		return 0;
1426 
1427 	while (i < num) {
1428 		ret = os_snprintf(buf + len, buflen - len,
1429 				  MACSTR " VLAN_ID=%d\n",
1430 				  MAC2STR(acl[i].addr),
1431 				  acl[i].vlan_id.untagged);
1432 		if (ret < 0 || (size_t) ret >= buflen - len)
1433 			return len;
1434 		i++;
1435 		len += ret;
1436 	}
1437 	return len;
1438 }
1439 
1440 
hostapd_ctrl_iface_acl_add_mac(struct mac_acl_entry ** acl,int * num,const char * cmd)1441 int hostapd_ctrl_iface_acl_add_mac(struct mac_acl_entry **acl, int *num,
1442 				   const char *cmd)
1443 {
1444 	u8 addr[ETH_ALEN];
1445 	struct vlan_description vlan_id;
1446 	int ret = 0, vlanid = 0;
1447 	const char *pos;
1448 
1449 	if (hwaddr_aton(cmd, addr))
1450 		return -1;
1451 
1452 	pos = os_strstr(cmd, "VLAN_ID=");
1453 	if (pos)
1454 		vlanid = atoi(pos + 8);
1455 
1456 	if (!hostapd_maclist_found(*acl, *num, addr, &vlan_id)) {
1457 		ret = hostapd_add_acl_maclist(acl, num, vlanid, addr);
1458 		if (ret != -1 && *acl)
1459 			qsort(*acl, *num, sizeof(**acl), hostapd_acl_comp);
1460 	}
1461 
1462 	return ret < 0 ? -1 : 0;
1463 }
1464 
1465 
hostapd_disassoc_accept_mac(struct hostapd_data * hapd)1466 int hostapd_disassoc_accept_mac(struct hostapd_data *hapd)
1467 {
1468 	struct sta_info *sta;
1469 	struct vlan_description vlan_id;
1470 
1471 	if (hapd->conf->macaddr_acl != DENY_UNLESS_ACCEPTED)
1472 		return 0;
1473 
1474 	for (sta = hapd->sta_list; sta; sta = sta->next) {
1475 		if (!hostapd_maclist_found(hapd->conf->accept_mac,
1476 					   hapd->conf->num_accept_mac,
1477 					   sta->addr, &vlan_id) ||
1478 		    (vlan_id.notempty &&
1479 		     vlan_compare(&vlan_id, sta->vlan_desc)))
1480 			ap_sta_disconnect(hapd, sta, sta->addr,
1481 					  WLAN_REASON_UNSPECIFIED);
1482 	}
1483 
1484 	return 0;
1485 }
1486 
1487 
hostapd_disassoc_deny_mac(struct hostapd_data * hapd)1488 int hostapd_disassoc_deny_mac(struct hostapd_data *hapd)
1489 {
1490 	struct sta_info *sta;
1491 	struct vlan_description vlan_id;
1492 
1493 	for (sta = hapd->sta_list; sta; sta = sta->next) {
1494 		if (hostapd_maclist_found(hapd->conf->deny_mac,
1495 					  hapd->conf->num_deny_mac, sta->addr,
1496 					  &vlan_id) &&
1497 		    (!vlan_id.notempty ||
1498 		     !vlan_compare(&vlan_id, sta->vlan_desc)))
1499 			ap_sta_disconnect(hapd, sta, sta->addr,
1500 					  WLAN_REASON_UNSPECIFIED);
1501 	}
1502 
1503 	return 0;
1504 }
1505