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 	if (sta->max_idle_period) {
281 		ret = os_snprintf(buf + len, buflen - len,
282 				  "max_idle_period=%d\n", sta->max_idle_period);
283 		if (os_snprintf_error(buflen - len, ret))
284 			return len;
285 		len += ret;
286 	}
287 
288 	res = ieee802_11_get_mib_sta(hapd, sta, buf + len, buflen - len);
289 	if (res >= 0)
290 		len += res;
291 	res = wpa_get_mib_sta(sta->wpa_sm, buf + len, buflen - len);
292 	if (res >= 0)
293 		len += res;
294 	res = ieee802_1x_get_mib_sta(hapd, sta, buf + len, buflen - len);
295 	if (res >= 0)
296 		len += res;
297 	res = hostapd_wps_get_mib_sta(hapd, sta->addr, buf + len,
298 				      buflen - len);
299 	if (res >= 0)
300 		len += res;
301 	res = hostapd_p2p_get_mib_sta(hapd, sta, buf + len, buflen - len);
302 	if (res >= 0)
303 		len += res;
304 
305 	len += hostapd_get_sta_info(hapd, sta, buf + len, buflen - len);
306 
307 #ifdef CONFIG_SAE
308 	if (sta->sae && sta->sae->state == SAE_ACCEPTED) {
309 		res = os_snprintf(buf + len, buflen - len, "sae_group=%d\n",
310 				  sta->sae->group);
311 		if (!os_snprintf_error(buflen - len, res))
312 			len += res;
313 	}
314 
315 	if (sta->sae && sta->sae->tmp) {
316 		const u8 *pos;
317 		unsigned int j, count;
318 		struct wpabuf *groups = sta->sae->tmp->peer_rejected_groups;
319 
320 		res = os_snprintf(buf + len, buflen - len,
321 				  "sae_rejected_groups=");
322 		if (!os_snprintf_error(buflen - len, res))
323 			len += res;
324 
325 		if (groups) {
326 			pos = wpabuf_head(groups);
327 			count = wpabuf_len(groups) / 2;
328 		} else {
329 			pos = NULL;
330 			count = 0;
331 		}
332 		for (j = 0; pos && j < count; j++) {
333 			res = os_snprintf(buf + len, buflen - len, "%s%d",
334 					  j == 0 ? "" : " ", WPA_GET_LE16(pos));
335 			if (!os_snprintf_error(buflen - len, res))
336 				len += res;
337 			pos += 2;
338 		}
339 
340 		res = os_snprintf(buf + len, buflen - len, "\n");
341 		if (!os_snprintf_error(buflen - len, res))
342 			len += res;
343 	}
344 #endif /* CONFIG_SAE */
345 
346 	if (sta->vlan_id > 0) {
347 		res = os_snprintf(buf + len, buflen - len, "vlan_id=%d\n",
348 				  sta->vlan_id);
349 		if (!os_snprintf_error(buflen - len, res))
350 			len += res;
351 	}
352 
353 	res = mbo_ap_get_info(sta, buf + len, buflen - len);
354 	if (res >= 0)
355 		len += res;
356 
357 	if (sta->supp_op_classes &&
358 	    buflen - len > (unsigned) (17 + 2 * sta->supp_op_classes[0])) {
359 		res = os_snprintf(buf + len, buflen - len, "supp_op_classes=");
360 		if (!os_snprintf_error(buflen - len, res))
361 			len += res;
362 		len += wpa_snprintf_hex(buf + len, buflen - len,
363 					sta->supp_op_classes + 1,
364 					sta->supp_op_classes[0]);
365 		res = os_snprintf(buf + len, buflen - len, "\n");
366 		if (!os_snprintf_error(buflen - len, res))
367 			len += res;
368 	}
369 
370 	if (sta->power_capab) {
371 		ret = os_snprintf(buf + len, buflen - len,
372 				  "min_txpower=%d\n"
373 				  "max_txpower=%d\n",
374 				  sta->min_tx_power, sta->max_tx_power);
375 		if (!os_snprintf_error(buflen - len, ret))
376 			len += ret;
377 	}
378 
379 #ifdef CONFIG_IEEE80211AX
380 	if ((sta->flags & WLAN_STA_HE) && sta->he_capab) {
381 		res = os_snprintf(buf + len, buflen - len, "he_capab=");
382 		if (!os_snprintf_error(buflen - len, res))
383 			len += res;
384 		len += wpa_snprintf_hex(buf + len, buflen - len,
385 					(const u8 *) sta->he_capab,
386 					sta->he_capab_len);
387 		res = os_snprintf(buf + len, buflen - len, "\n");
388 		if (!os_snprintf_error(buflen - len, res))
389 			len += res;
390 	}
391 #endif /* CONFIG_IEEE80211AX */
392 
393 #ifdef CONFIG_IEEE80211BE
394 	if ((sta->flags & WLAN_STA_EHT) && sta->eht_capab) {
395 		res = os_snprintf(buf + len, buflen - len, "eht_capab=");
396 		if (!os_snprintf_error(buflen - len, res))
397 			len += res;
398 		len += wpa_snprintf_hex(buf + len, buflen - len,
399 					(const u8 *) sta->eht_capab,
400 					sta->eht_capab_len);
401 		res = os_snprintf(buf + len, buflen - len, "\n");
402 		if (!os_snprintf_error(buflen - len, res))
403 			len += res;
404 	}
405 #endif /* CONFIG_IEEE80211BE */
406 
407 #ifdef CONFIG_IEEE80211AC
408 	if ((sta->flags & WLAN_STA_VHT) && sta->vht_capabilities) {
409 		res = os_snprintf(buf + len, buflen - len,
410 				  "vht_caps_info=0x%08x\n",
411 				  le_to_host32(sta->vht_capabilities->
412 					       vht_capabilities_info));
413 		if (!os_snprintf_error(buflen - len, res))
414 			len += res;
415 
416 		res = os_snprintf(buf + len, buflen - len, "vht_capab=");
417 		if (!os_snprintf_error(buflen - len, res))
418 			len += res;
419 		len += wpa_snprintf_hex(buf + len, buflen - len,
420 					(const u8 *) sta->vht_capabilities,
421 					sizeof(*sta->vht_capabilities));
422 		res = os_snprintf(buf + len, buflen - len, "\n");
423 		if (!os_snprintf_error(buflen - len, res))
424 			len += res;
425 	}
426 #endif /* CONFIG_IEEE80211AC */
427 
428 	if ((sta->flags & WLAN_STA_HT) && sta->ht_capabilities) {
429 		res = os_snprintf(buf + len, buflen - len,
430 				  "ht_caps_info=0x%04x\n",
431 				  le_to_host16(sta->ht_capabilities->
432 					       ht_capabilities_info));
433 		if (!os_snprintf_error(buflen - len, res))
434 			len += res;
435 	}
436 
437 	if (sta->ext_capability &&
438 	    buflen - len > (unsigned) (11 + 2 * sta->ext_capability[0])) {
439 		res = os_snprintf(buf + len, buflen - len, "ext_capab=");
440 		if (!os_snprintf_error(buflen - len, res))
441 			len += res;
442 		len += wpa_snprintf_hex(buf + len, buflen - len,
443 					sta->ext_capability + 1,
444 					sta->ext_capability[0]);
445 		res = os_snprintf(buf + len, buflen - len, "\n");
446 		if (!os_snprintf_error(buflen - len, res))
447 			len += res;
448 	}
449 
450 	if (sta->flags & WLAN_STA_WDS && sta->ifname_wds) {
451 		ret = os_snprintf(buf + len, buflen - len,
452 				  "wds_sta_ifname=%s\n", sta->ifname_wds);
453 		if (!os_snprintf_error(buflen - len, ret))
454 			len += ret;
455 	}
456 
457 	keyid = ap_sta_wpa_get_keyid(hapd, sta);
458 	if (keyid) {
459 		ret = os_snprintf(buf + len, buflen - len, "keyid=%s\n", keyid);
460 		if (!os_snprintf_error(buflen - len, ret))
461 			len += ret;
462 	}
463 
464 	dpp_pkhash = ap_sta_wpa_get_dpp_pkhash(hapd, sta);
465 	if (dpp_pkhash) {
466 		ret = os_snprintf(buf + len, buflen - len, "dpp_pkhash=");
467 		if (!os_snprintf_error(buflen - len, ret))
468 			len += ret;
469 		len += wpa_snprintf_hex(buf + len, buflen - len, dpp_pkhash,
470 					SHA256_MAC_LEN);
471 		ret = os_snprintf(buf + len, buflen - len, "\n");
472 		if (!os_snprintf_error(buflen - len, ret))
473 			len += ret;
474 	}
475 
476 #ifdef CONFIG_IEEE80211BE
477 	if (sta->mld_info.mld_sta) {
478 		u16 mld_sta_capa = sta->mld_info.common_info.mld_capa;
479 		u8 max_simul_links = mld_sta_capa &
480 			EHT_ML_MLD_CAPA_MAX_NUM_SIM_LINKS_MASK;
481 
482 		for (i = 0; i < MAX_NUM_MLD_LINKS; ++i) {
483 			if (!sta->mld_info.links[i].valid)
484 				continue;
485 			ret = os_snprintf(
486 				buf + len, buflen - len,
487 				"peer_addr[%d]=" MACSTR "\n",
488 				i, MAC2STR(sta->mld_info.links[i].peer_addr));
489 			if (!os_snprintf_error(buflen - len, ret))
490 				len += ret;
491 		}
492 
493 		ret = os_snprintf(buf + len, buflen - len,
494 				  "max_simul_links=%d\n", max_simul_links);
495 		if (!os_snprintf_error(buflen - len, ret))
496 			len += ret;
497 	}
498 #endif /* CONFIG_IEEE80211BE */
499 
500 	return len;
501 }
502 
503 
hostapd_ctrl_iface_sta_first(struct hostapd_data * hapd,char * buf,size_t buflen)504 int hostapd_ctrl_iface_sta_first(struct hostapd_data *hapd,
505 				 char *buf, size_t buflen)
506 {
507 	return hostapd_ctrl_iface_sta_mib(hapd, hapd->sta_list, buf, buflen);
508 }
509 
510 
hostapd_ctrl_iface_sta(struct hostapd_data * hapd,const char * txtaddr,char * buf,size_t buflen)511 int hostapd_ctrl_iface_sta(struct hostapd_data *hapd, const char *txtaddr,
512 			   char *buf, size_t buflen)
513 {
514 	u8 addr[ETH_ALEN];
515 	int ret;
516 	const char *pos;
517 	struct sta_info *sta;
518 
519 	if (hwaddr_aton(txtaddr, addr)) {
520 		ret = os_snprintf(buf, buflen, "FAIL\n");
521 		if (os_snprintf_error(buflen, ret))
522 			return 0;
523 		return ret;
524 	}
525 
526 	sta = ap_get_sta(hapd, addr);
527 	if (sta == NULL)
528 		return -1;
529 
530 	pos = os_strchr(txtaddr, ' ');
531 	if (pos) {
532 		pos++;
533 
534 #ifdef HOSTAPD_DUMP_STATE
535 		if (os_strcmp(pos, "eapol") == 0) {
536 			if (sta->eapol_sm == NULL)
537 				return -1;
538 			return eapol_auth_dump_state(sta->eapol_sm, buf,
539 						     buflen);
540 		}
541 #endif /* HOSTAPD_DUMP_STATE */
542 
543 		return -1;
544 	}
545 
546 	ret = hostapd_ctrl_iface_sta_mib(hapd, sta, buf, buflen);
547 	ret += fst_ctrl_iface_mb_info(addr, buf + ret, buflen - ret);
548 
549 	return ret;
550 }
551 
552 
hostapd_ctrl_iface_sta_next(struct hostapd_data * hapd,const char * txtaddr,char * buf,size_t buflen)553 int hostapd_ctrl_iface_sta_next(struct hostapd_data *hapd, const char *txtaddr,
554 				char *buf, size_t buflen)
555 {
556 	u8 addr[ETH_ALEN];
557 	struct sta_info *sta;
558 	int ret;
559 
560 	if (hwaddr_aton(txtaddr, addr) ||
561 	    (sta = ap_get_sta(hapd, addr)) == NULL) {
562 		ret = os_snprintf(buf, buflen, "FAIL\n");
563 		if (os_snprintf_error(buflen, ret))
564 			return 0;
565 		return ret;
566 	}
567 
568 	if (!sta->next)
569 		return 0;
570 
571 	return hostapd_ctrl_iface_sta_mib(hapd, sta->next, buf, buflen);
572 }
573 
574 
575 #ifdef CONFIG_P2P_MANAGER
p2p_manager_disconnect(struct hostapd_data * hapd,u16 stype,u8 minor_reason_code,const u8 * addr)576 static int p2p_manager_disconnect(struct hostapd_data *hapd, u16 stype,
577 				  u8 minor_reason_code, const u8 *addr)
578 {
579 	struct ieee80211_mgmt *mgmt;
580 	int ret;
581 	u8 *pos;
582 
583 	mgmt = os_zalloc(sizeof(*mgmt) + 100);
584 	if (mgmt == NULL)
585 		return -1;
586 
587 	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, stype);
588 	wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "P2P: Disconnect STA " MACSTR
589 		" with minor reason code %u (stype=%u (%s))",
590 		MAC2STR(addr), minor_reason_code, stype,
591 		fc2str(le_to_host16(mgmt->frame_control)));
592 
593 	os_memcpy(mgmt->da, addr, ETH_ALEN);
594 	os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
595 	os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
596 	if (stype == WLAN_FC_STYPE_DEAUTH) {
597 		mgmt->u.deauth.reason_code =
598 			host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
599 		pos = mgmt->u.deauth.variable;
600 	} else {
601 		mgmt->u.disassoc.reason_code =
602 			host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
603 		pos = mgmt->u.disassoc.variable;
604 	}
605 
606 	*pos++ = WLAN_EID_VENDOR_SPECIFIC;
607 	*pos++ = 4 + 3 + 1;
608 	WPA_PUT_BE32(pos, P2P_IE_VENDOR_TYPE);
609 	pos += 4;
610 
611 	*pos++ = P2P_ATTR_MINOR_REASON_CODE;
612 	WPA_PUT_LE16(pos, 1);
613 	pos += 2;
614 	*pos++ = minor_reason_code;
615 
616 	ret = hostapd_drv_send_mlme(hapd, mgmt, pos - (u8 *) mgmt, 0, NULL, 0,
617 				    0);
618 	os_free(mgmt);
619 
620 	return ret < 0 ? -1 : 0;
621 }
622 #endif /* CONFIG_P2P_MANAGER */
623 
624 
hostapd_ctrl_iface_deauthenticate(struct hostapd_data * hapd,const char * txtaddr)625 int hostapd_ctrl_iface_deauthenticate(struct hostapd_data *hapd,
626 				      const char *txtaddr)
627 {
628 	u8 addr[ETH_ALEN];
629 	struct sta_info *sta;
630 	const char *pos;
631 	u16 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
632 
633 	wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DEAUTHENTICATE %s",
634 		txtaddr);
635 
636 	if (hwaddr_aton(txtaddr, addr))
637 		return -1;
638 
639 	pos = os_strstr(txtaddr, " reason=");
640 	if (pos)
641 		reason = atoi(pos + 8);
642 
643 	pos = os_strstr(txtaddr, " test=");
644 	if (pos) {
645 		struct ieee80211_mgmt mgmt;
646 		int encrypt;
647 
648 		pos += 6;
649 		encrypt = atoi(pos);
650 		os_memset(&mgmt, 0, sizeof(mgmt));
651 		mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
652 						  WLAN_FC_STYPE_DEAUTH);
653 		os_memcpy(mgmt.da, addr, ETH_ALEN);
654 		os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
655 		os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
656 		mgmt.u.deauth.reason_code = host_to_le16(reason);
657 		if (hostapd_drv_send_mlme(hapd, (u8 *) &mgmt,
658 					  IEEE80211_HDRLEN +
659 					  sizeof(mgmt.u.deauth),
660 					  0, NULL, 0, !encrypt) < 0)
661 			return -1;
662 		return 0;
663 	}
664 
665 #ifdef CONFIG_P2P_MANAGER
666 	pos = os_strstr(txtaddr, " p2p=");
667 	if (pos) {
668 		return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DEAUTH,
669 					      atoi(pos + 5), addr);
670 	}
671 #endif /* CONFIG_P2P_MANAGER */
672 
673 	sta = ap_get_sta(hapd, addr);
674 	if (os_strstr(txtaddr, " tx=0")) {
675 		hostapd_drv_sta_remove(hapd, addr);
676 		if (sta)
677 			ap_free_sta(hapd, sta);
678 	} else {
679 		hostapd_drv_sta_deauth(hapd, addr, reason);
680 		if (sta)
681 			ap_sta_deauthenticate(hapd, sta, reason);
682 		else if (addr[0] == 0xff)
683 			hostapd_free_stas(hapd);
684 	}
685 
686 	return 0;
687 }
688 
689 
hostapd_ctrl_iface_disassociate(struct hostapd_data * hapd,const char * txtaddr)690 int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd,
691 				    const char *txtaddr)
692 {
693 	u8 addr[ETH_ALEN];
694 	struct sta_info *sta;
695 	const char *pos;
696 	u16 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
697 
698 	wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DISASSOCIATE %s",
699 		txtaddr);
700 
701 	if (hwaddr_aton(txtaddr, addr))
702 		return -1;
703 
704 	pos = os_strstr(txtaddr, " reason=");
705 	if (pos)
706 		reason = atoi(pos + 8);
707 
708 	pos = os_strstr(txtaddr, " test=");
709 	if (pos) {
710 		struct ieee80211_mgmt mgmt;
711 		int encrypt;
712 
713 		pos += 6;
714 		encrypt = atoi(pos);
715 		os_memset(&mgmt, 0, sizeof(mgmt));
716 		mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
717 						  WLAN_FC_STYPE_DISASSOC);
718 		os_memcpy(mgmt.da, addr, ETH_ALEN);
719 		os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
720 		os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
721 		mgmt.u.disassoc.reason_code = host_to_le16(reason);
722 		if (hostapd_drv_send_mlme(hapd, (u8 *) &mgmt,
723 					  IEEE80211_HDRLEN +
724 					  sizeof(mgmt.u.deauth),
725 					  0, NULL, 0, !encrypt) < 0)
726 			return -1;
727 		return 0;
728 	}
729 
730 #ifdef CONFIG_P2P_MANAGER
731 	pos = os_strstr(txtaddr, " p2p=");
732 	if (pos) {
733 		return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DISASSOC,
734 					      atoi(pos + 5), addr);
735 	}
736 #endif /* CONFIG_P2P_MANAGER */
737 
738 	sta = ap_get_sta(hapd, addr);
739 	if (os_strstr(txtaddr, " tx=0")) {
740 		hostapd_drv_sta_remove(hapd, addr);
741 		if (sta)
742 			ap_free_sta(hapd, sta);
743 	} else {
744 		hostapd_drv_sta_disassoc(hapd, addr, reason);
745 		if (sta)
746 			ap_sta_disassociate(hapd, sta, reason);
747 		else if (addr[0] == 0xff)
748 			hostapd_free_stas(hapd);
749 	}
750 
751 	return 0;
752 }
753 
754 
755 #ifdef CONFIG_TAXONOMY
hostapd_ctrl_iface_signature(struct hostapd_data * hapd,const char * txtaddr,char * buf,size_t buflen)756 int hostapd_ctrl_iface_signature(struct hostapd_data *hapd,
757 				 const char *txtaddr,
758 				 char *buf, size_t buflen)
759 {
760 	u8 addr[ETH_ALEN];
761 	struct sta_info *sta;
762 
763 	wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE SIGNATURE %s", txtaddr);
764 
765 	if (hwaddr_aton(txtaddr, addr))
766 		return -1;
767 
768 	sta = ap_get_sta(hapd, addr);
769 	if (!sta)
770 		return -1;
771 
772 	return retrieve_sta_taxonomy(hapd, sta, buf, buflen);
773 }
774 #endif /* CONFIG_TAXONOMY */
775 
776 
hostapd_ctrl_iface_poll_sta(struct hostapd_data * hapd,const char * txtaddr)777 int hostapd_ctrl_iface_poll_sta(struct hostapd_data *hapd,
778 				const char *txtaddr)
779 {
780 	u8 addr[ETH_ALEN];
781 	struct sta_info *sta;
782 
783 	wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE POLL_STA %s", txtaddr);
784 
785 	if (hwaddr_aton(txtaddr, addr))
786 		return -1;
787 
788 	sta = ap_get_sta(hapd, addr);
789 	if (!sta)
790 		return -1;
791 
792 	hostapd_drv_poll_client(hapd, hapd->own_addr, addr,
793 				sta->flags & WLAN_STA_WMM);
794 	return 0;
795 }
796 
797 
hostapd_ctrl_iface_status(struct hostapd_data * hapd,char * buf,size_t buflen)798 int hostapd_ctrl_iface_status(struct hostapd_data *hapd, char *buf,
799 			      size_t buflen)
800 {
801 	struct hostapd_iface *iface = hapd->iface;
802 	struct hostapd_hw_modes *mode = iface->current_mode;
803 	struct hostapd_config *iconf = hapd->iconf;
804 	int len = 0, ret, j;
805 	size_t i;
806 
807 	ret = os_snprintf(buf + len, buflen - len,
808 			  "state=%s\n"
809 			  "phy=%s\n"
810 			  "freq=%d\n"
811 			  "num_sta_non_erp=%d\n"
812 			  "num_sta_no_short_slot_time=%d\n"
813 			  "num_sta_no_short_preamble=%d\n"
814 			  "olbc=%d\n"
815 			  "num_sta_ht_no_gf=%d\n"
816 			  "num_sta_no_ht=%d\n"
817 			  "num_sta_ht_20_mhz=%d\n"
818 			  "num_sta_ht40_intolerant=%d\n"
819 			  "olbc_ht=%d\n"
820 			  "ht_op_mode=0x%x\n",
821 			  hostapd_state_text(iface->state),
822 			  iface->phy,
823 			  iface->freq,
824 			  iface->num_sta_non_erp,
825 			  iface->num_sta_no_short_slot_time,
826 			  iface->num_sta_no_short_preamble,
827 			  iface->olbc,
828 			  iface->num_sta_ht_no_gf,
829 			  iface->num_sta_no_ht,
830 			  iface->num_sta_ht_20mhz,
831 			  iface->num_sta_ht40_intolerant,
832 			  iface->olbc_ht,
833 			  iface->ht_op_mode);
834 	if (os_snprintf_error(buflen - len, ret))
835 		return len;
836 	len += ret;
837 
838 	if (mode) {
839 		ret = os_snprintf(buf + len, buflen - len, "hw_mode=%s\n",
840 				  hw_mode_str(mode->mode));
841 		if (os_snprintf_error(buflen - len, ret))
842 			return len;
843 		len += ret;
844 	}
845 
846 	if (iconf->country[0] && iconf->country[1]) {
847 		ret = os_snprintf(buf + len, buflen - len,
848 				  "country_code=%c%c\ncountry3=0x%X\n",
849 				  iconf->country[0], iconf->country[1],
850 				  iconf->country[2]);
851 		if (os_snprintf_error(buflen - len, ret))
852 			return len;
853 		len += ret;
854 	}
855 
856 	if (!iface->cac_started || !iface->dfs_cac_ms) {
857 		ret = os_snprintf(buf + len, buflen - len,
858 				  "cac_time_seconds=%d\n"
859 				  "cac_time_left_seconds=N/A\n",
860 				  iface->dfs_cac_ms / 1000);
861 	} else {
862 		/* CAC started and CAC time set - calculate remaining time */
863 		struct os_reltime now;
864 		long left_time;
865 
866 		os_reltime_age(&iface->dfs_cac_start, &now);
867 		left_time = (long) iface->dfs_cac_ms / 1000 - now.sec;
868 		ret = os_snprintf(buf + len, buflen - len,
869 				  "cac_time_seconds=%u\n"
870 				  "cac_time_left_seconds=%lu\n",
871 				  iface->dfs_cac_ms / 1000,
872 				  left_time > 0 ? left_time : 0);
873 	}
874 	if (os_snprintf_error(buflen - len, ret))
875 		return len;
876 	len += ret;
877 
878 	ret = os_snprintf(buf + len, buflen - len,
879 			  "channel=%u\n"
880 			  "edmg_enable=%d\n"
881 			  "edmg_channel=%d\n"
882 			  "secondary_channel=%d\n"
883 			  "ieee80211n=%d\n"
884 			  "ieee80211ac=%d\n"
885 			  "ieee80211ax=%d\n"
886 			  "ieee80211be=%d\n"
887 			  "beacon_int=%u\n"
888 			  "dtim_period=%d\n",
889 			  iface->conf->channel,
890 			  iface->conf->enable_edmg,
891 			  iface->conf->edmg_channel,
892 			  iface->conf->ieee80211n && !hapd->conf->disable_11n ?
893 			  iface->conf->secondary_channel : 0,
894 			  iface->conf->ieee80211n && !hapd->conf->disable_11n,
895 			  iface->conf->ieee80211ac &&
896 			  !hapd->conf->disable_11ac,
897 			  iface->conf->ieee80211ax &&
898 			  !hapd->conf->disable_11ax,
899 			  iface->conf->ieee80211be &&
900 			  !hapd->conf->disable_11be,
901 			  iface->conf->beacon_int,
902 			  hapd->conf->dtim_period);
903 	if (os_snprintf_error(buflen - len, ret))
904 		return len;
905 	len += ret;
906 
907 #ifdef CONFIG_IEEE80211BE
908 	if (iface->conf->ieee80211be && !hapd->conf->disable_11be) {
909 		ret = os_snprintf(buf + len, buflen - len,
910 				  "eht_oper_chwidth=%d\n"
911 				  "eht_oper_centr_freq_seg0_idx=%d\n",
912 				  iface->conf->eht_oper_chwidth,
913 				  iface->conf->eht_oper_centr_freq_seg0_idx);
914 		if (os_snprintf_error(buflen - len, ret))
915 			return len;
916 		len += ret;
917 
918 		if (is_6ghz_op_class(iface->conf->op_class) &&
919 		    hostapd_get_oper_chwidth(iface->conf) ==
920 		    CONF_OPER_CHWIDTH_320MHZ) {
921 			ret = os_snprintf(buf + len, buflen - len,
922 					  "eht_bw320_offset=%d\n",
923 					  iface->conf->eht_bw320_offset);
924 			if (os_snprintf_error(buflen - len, ret))
925 				return len;
926 			len += ret;
927 		}
928 
929 		if (hapd->iconf->punct_bitmap) {
930 			ret = os_snprintf(buf + len, buflen - len,
931 					  "punct_bitmap=0x%x\n",
932 					  hapd->iconf->punct_bitmap);
933 			if (os_snprintf_error(buflen - len, ret))
934 				return len;
935 			len += ret;
936 		}
937 
938 		if (hapd->conf->mld_ap) {
939 			struct hostapd_data *link_bss;
940 
941 			ret = os_snprintf(buf + len, buflen - len,
942 					  "num_links=%d\n",
943 					  hapd->mld->num_links);
944 			if (os_snprintf_error(buflen - len, ret))
945 				return len;
946 			len += ret;
947 
948 			/* Self BSS */
949 			ret = os_snprintf(buf + len, buflen - len,
950 					  "link_id=%d\n"
951 					  "link_addr=" MACSTR "\n",
952 					  hapd->mld_link_id,
953 					  MAC2STR(hapd->own_addr));
954 			if (os_snprintf_error(buflen - len, ret))
955 				return len;
956 			len += ret;
957 
958 			/* Partner BSSs */
959 			for_each_mld_link(link_bss, hapd) {
960 				if (link_bss == hapd)
961 					continue;
962 
963 				ret = os_snprintf(buf + len, buflen - len,
964 						  "partner_link[%d]=" MACSTR
965 						  "\n",
966 						  link_bss->mld_link_id,
967 						  MAC2STR(link_bss->own_addr));
968 				if (os_snprintf_error(buflen - len, ret))
969 					return len;
970 				len += ret;
971 			}
972 
973 			ret = os_snprintf(buf + len, buflen - len,
974 					  "ap_mld_type=%s\n",
975 					  (hapd->iface->mld_mld_capa &
976 					   EHT_ML_MLD_CAPA_AP_MLD_TYPE_IND_MASK)
977 					  ? "NSTR" : "STR");
978 			if (os_snprintf_error(buflen - len, ret))
979 				return len;
980 			len += ret;
981 		}
982 	}
983 #endif /* CONFIG_IEEE80211BE */
984 
985 #ifdef CONFIG_IEEE80211AX
986 	if (iface->conf->ieee80211ax && !hapd->conf->disable_11ax) {
987 		ret = os_snprintf(buf + len, buflen - len,
988 				  "he_oper_chwidth=%d\n"
989 				  "he_oper_centr_freq_seg0_idx=%d\n"
990 				  "he_oper_centr_freq_seg1_idx=%d\n",
991 				  iface->conf->he_oper_chwidth,
992 				  iface->conf->he_oper_centr_freq_seg0_idx,
993 				  iface->conf->he_oper_centr_freq_seg1_idx);
994 		if (os_snprintf_error(buflen - len, ret))
995 			return len;
996 		len += ret;
997 
998 		if (!iconf->he_op.he_bss_color_disabled &&
999 		    iconf->he_op.he_bss_color) {
1000 			ret = os_snprintf(buf + len, buflen - len,
1001 					  "he_bss_color=%d\n",
1002 					  iconf->he_op.he_bss_color);
1003 			if (os_snprintf_error(buflen - len, ret))
1004 				return len;
1005 			len += ret;
1006 		}
1007 	}
1008 #endif /* CONFIG_IEEE80211AX */
1009 
1010 	if (iface->conf->ieee80211ac && !hapd->conf->disable_11ac) {
1011 		ret = os_snprintf(buf + len, buflen - len,
1012 				  "vht_oper_chwidth=%d\n"
1013 				  "vht_oper_centr_freq_seg0_idx=%d\n"
1014 				  "vht_oper_centr_freq_seg1_idx=%d\n"
1015 				  "vht_caps_info=%08x\n",
1016 				  iface->conf->vht_oper_chwidth,
1017 				  iface->conf->vht_oper_centr_freq_seg0_idx,
1018 				  iface->conf->vht_oper_centr_freq_seg1_idx,
1019 				  iface->conf->vht_capab);
1020 		if (os_snprintf_error(buflen - len, ret))
1021 			return len;
1022 		len += ret;
1023 	}
1024 
1025 	if (iface->conf->ieee80211ac && !hapd->conf->disable_11ac && mode) {
1026 		u16 rxmap = WPA_GET_LE16(&mode->vht_mcs_set[0]);
1027 		u16 txmap = WPA_GET_LE16(&mode->vht_mcs_set[4]);
1028 
1029 		ret = os_snprintf(buf + len, buflen - len,
1030 				  "rx_vht_mcs_map=%04x\n"
1031 				  "tx_vht_mcs_map=%04x\n",
1032 				  rxmap, txmap);
1033 		if (os_snprintf_error(buflen - len, ret))
1034 			return len;
1035 		len += ret;
1036 	}
1037 
1038 	if (iface->conf->ieee80211n && !hapd->conf->disable_11n) {
1039 		ret = os_snprintf(buf + len, buflen - len,
1040 				  "ht_caps_info=%04x\n",
1041 				  hapd->iconf->ht_capab);
1042 		if (os_snprintf_error(buflen - len, ret))
1043 			return len;
1044 		len += ret;
1045 	}
1046 
1047 	if (iface->conf->ieee80211n && !hapd->conf->disable_11n && mode) {
1048 		len = hostapd_write_ht_mcs_bitmask(buf, buflen, len,
1049 						   mode->mcs_set);
1050 	}
1051 
1052 	if (iface->current_rates && iface->num_rates) {
1053 		ret = os_snprintf(buf + len, buflen - len, "supported_rates=");
1054 		if (os_snprintf_error(buflen - len, ret))
1055 			return len;
1056 		len += ret;
1057 
1058 		for (j = 0; j < iface->num_rates; j++) {
1059 			ret = os_snprintf(buf + len, buflen - len, "%s%02x",
1060 					  j > 0 ? " " : "",
1061 					  iface->current_rates[j].rate / 5);
1062 			if (os_snprintf_error(buflen - len, ret))
1063 				return len;
1064 			len += ret;
1065 		}
1066 		ret = os_snprintf(buf + len, buflen - len, "\n");
1067 		if (os_snprintf_error(buflen - len, ret))
1068 			return len;
1069 		len += ret;
1070 	}
1071 
1072 	for (j = 0; mode && j < mode->num_channels; j++) {
1073 		if (mode->channels[j].freq == iface->freq) {
1074 			ret = os_snprintf(buf + len, buflen - len,
1075 					  "max_txpower=%u\n",
1076 					  mode->channels[j].max_tx_power);
1077 			if (os_snprintf_error(buflen - len, ret))
1078 				return len;
1079 			len += ret;
1080 			break;
1081 		}
1082 	}
1083 
1084 	for (i = 0; i < iface->num_bss; i++) {
1085 		struct hostapd_data *bss = iface->bss[i];
1086 		ret = os_snprintf(buf + len, buflen - len,
1087 				  "bss[%d]=%s\n"
1088 				  "bssid[%d]=" MACSTR "\n"
1089 				  "ssid[%d]=%s\n"
1090 				  "num_sta[%d]=%d\n",
1091 				  (int) i, bss->conf->iface,
1092 				  (int) i, MAC2STR(bss->own_addr),
1093 				  (int) i,
1094 				  wpa_ssid_txt(bss->conf->ssid.ssid,
1095 					       bss->conf->ssid.ssid_len),
1096 				  (int) i, bss->num_sta);
1097 		if (os_snprintf_error(buflen - len, ret))
1098 			return len;
1099 		len += ret;
1100 
1101 #ifdef CONFIG_IEEE80211BE
1102 		if (bss->conf->mld_ap) {
1103 			ret = os_snprintf(buf + len, buflen - len,
1104 					  "mld_addr[%d]=" MACSTR "\n"
1105 					  "mld_id[%d]=%d\n"
1106 					  "mld_link_id[%d]=%d\n",
1107 					  (int) i, MAC2STR(bss->mld->mld_addr),
1108 					  (int) i, hostapd_get_mld_id(bss),
1109 					  (int) i, bss->mld_link_id);
1110 			if (os_snprintf_error(buflen - len, ret))
1111 				return len;
1112 			len += ret;
1113 		}
1114 #endif /* CONFIG_IEEE80211BE */
1115 	}
1116 
1117 	if (hapd->conf->chan_util_avg_period) {
1118 		ret = os_snprintf(buf + len, buflen - len,
1119 				  "chan_util_avg=%u\n",
1120 				  iface->chan_util_average);
1121 		if (os_snprintf_error(buflen - len, ret))
1122 			return len;
1123 		len += ret;
1124 	}
1125 
1126 	return len;
1127 }
1128 
1129 
hostapd_parse_csa_settings(const char * pos,struct csa_settings * settings)1130 int hostapd_parse_csa_settings(const char *pos,
1131 			       struct csa_settings *settings)
1132 {
1133 	char *end;
1134 
1135 	os_memset(settings, 0, sizeof(*settings));
1136 	settings->cs_count = strtol(pos, &end, 10);
1137 	if (pos == end) {
1138 		wpa_printf(MSG_ERROR, "chanswitch: invalid cs_count provided");
1139 		return -1;
1140 	}
1141 
1142 	settings->freq_params.freq = atoi(end);
1143 	if (settings->freq_params.freq == 0) {
1144 		wpa_printf(MSG_ERROR, "chanswitch: invalid freq provided");
1145 		return -1;
1146 	}
1147 
1148 #define SET_CSA_SETTING(str) \
1149 	do { \
1150 		const char *pos2 = os_strstr(pos, " " #str "="); \
1151 		if (pos2) { \
1152 			pos2 += sizeof(" " #str "=") - 1; \
1153 			settings->freq_params.str = atoi(pos2); \
1154 		} \
1155 	} while (0)
1156 
1157 	SET_CSA_SETTING(center_freq1);
1158 	SET_CSA_SETTING(center_freq2);
1159 	SET_CSA_SETTING(bandwidth);
1160 	SET_CSA_SETTING(sec_channel_offset);
1161 	SET_CSA_SETTING(punct_bitmap);
1162 	settings->freq_params.ht_enabled = !!os_strstr(pos, " ht");
1163 	settings->freq_params.vht_enabled = !!os_strstr(pos, " vht");
1164 	settings->freq_params.eht_enabled = !!os_strstr(pos, " eht");
1165 	settings->freq_params.he_enabled = !!os_strstr(pos, " he") ||
1166 		settings->freq_params.eht_enabled;
1167 	settings->block_tx = !!os_strstr(pos, " blocktx");
1168 #undef SET_CSA_SETTING
1169 
1170 	return 0;
1171 }
1172 
1173 
hostapd_ctrl_iface_stop_ap(struct hostapd_data * hapd)1174 int hostapd_ctrl_iface_stop_ap(struct hostapd_data *hapd)
1175 {
1176 	return hostapd_drv_stop_ap(hapd);
1177 }
1178 
1179 
hostapd_ctrl_iface_pmksa_list(struct hostapd_data * hapd,char * buf,size_t len)1180 int hostapd_ctrl_iface_pmksa_list(struct hostapd_data *hapd, char *buf,
1181 				  size_t len)
1182 {
1183 	return wpa_auth_pmksa_list(hapd->wpa_auth, buf, len);
1184 }
1185 
1186 
hostapd_ctrl_iface_pmksa_flush(struct hostapd_data * hapd)1187 void hostapd_ctrl_iface_pmksa_flush(struct hostapd_data *hapd)
1188 {
1189 	wpa_auth_pmksa_flush(hapd->wpa_auth);
1190 }
1191 
1192 
hostapd_ctrl_iface_pmksa_add(struct hostapd_data * hapd,char * cmd)1193 int hostapd_ctrl_iface_pmksa_add(struct hostapd_data *hapd, char *cmd)
1194 {
1195 	u8 spa[ETH_ALEN];
1196 	u8 pmkid[PMKID_LEN];
1197 	u8 pmk[PMK_LEN_MAX];
1198 	size_t pmk_len;
1199 	char *pos, *pos2;
1200 	int akmp = 0, expiration = 0;
1201 	int ret;
1202 
1203 	/*
1204 	 * Entry format:
1205 	 * <STA addr> <PMKID> <PMK> <expiration in seconds> <akmp>
1206 	 */
1207 
1208 	if (hwaddr_aton(cmd, spa))
1209 		return -1;
1210 
1211 	pos = os_strchr(cmd, ' ');
1212 	if (!pos)
1213 		return -1;
1214 	pos++;
1215 
1216 	if (hexstr2bin(pos, pmkid, PMKID_LEN) < 0)
1217 		return -1;
1218 
1219 	pos = os_strchr(pos, ' ');
1220 	if (!pos)
1221 		return -1;
1222 	pos++;
1223 
1224 	pos2 = os_strchr(pos, ' ');
1225 	if (!pos2)
1226 		return -1;
1227 	pmk_len = (pos2 - pos) / 2;
1228 	if (pmk_len < PMK_LEN || pmk_len > PMK_LEN_MAX ||
1229 	    hexstr2bin(pos, pmk, pmk_len) < 0)
1230 		return -1;
1231 
1232 	pos = pos2 + 1;
1233 
1234 	if (sscanf(pos, "%d %d", &expiration, &akmp) != 2)
1235 		return -1;
1236 
1237 	ret = wpa_auth_pmksa_add2(hapd->wpa_auth, spa, pmk, pmk_len,
1238 				  pmkid, expiration, akmp, NULL, false);
1239 	if (ret)
1240 		return ret;
1241 
1242 #ifdef CONFIG_IEEE80211BE
1243 	if (hapd->conf->mld_ap)
1244 		ret = wpa_auth_pmksa_add2(hapd->wpa_auth, spa, pmk, pmk_len,
1245 					  pmkid, expiration, akmp, NULL, true);
1246 #endif /* CONFIG_IEEE80211BE */
1247 
1248 	return ret;
1249 }
1250 
1251 
1252 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
1253 #ifdef CONFIG_MESH
1254 
hostapd_ctrl_iface_pmksa_list_mesh(struct hostapd_data * hapd,const u8 * addr,char * buf,size_t len)1255 int hostapd_ctrl_iface_pmksa_list_mesh(struct hostapd_data *hapd,
1256 				       const u8 *addr, char *buf, size_t len)
1257 {
1258 	return wpa_auth_pmksa_list_mesh(hapd->wpa_auth, addr, buf, len);
1259 }
1260 
1261 
hostapd_ctrl_iface_pmksa_create_entry(const u8 * aa,char * cmd)1262 void * hostapd_ctrl_iface_pmksa_create_entry(const u8 *aa, char *cmd)
1263 {
1264 	u8 spa[ETH_ALEN];
1265 	u8 pmkid[PMKID_LEN];
1266 	u8 pmk[PMK_LEN_MAX];
1267 	char *pos;
1268 	int expiration;
1269 
1270 	/*
1271 	 * Entry format:
1272 	 * <BSSID> <PMKID> <PMK> <expiration in seconds>
1273 	 */
1274 
1275 	if (hwaddr_aton(cmd, spa))
1276 		return NULL;
1277 
1278 	pos = os_strchr(cmd, ' ');
1279 	if (!pos)
1280 		return NULL;
1281 	pos++;
1282 
1283 	if (hexstr2bin(pos, pmkid, PMKID_LEN) < 0)
1284 		return NULL;
1285 
1286 	pos = os_strchr(pos, ' ');
1287 	if (!pos)
1288 		return NULL;
1289 	pos++;
1290 
1291 	if (hexstr2bin(pos, pmk, PMK_LEN) < 0)
1292 		return NULL;
1293 
1294 	pos = os_strchr(pos, ' ');
1295 	if (!pos)
1296 		return NULL;
1297 	pos++;
1298 
1299 	if (sscanf(pos, "%d", &expiration) != 1)
1300 		return NULL;
1301 
1302 	return wpa_auth_pmksa_create_entry(aa, spa, pmk, PMK_LEN,
1303 					   WPA_KEY_MGMT_SAE, pmkid, expiration);
1304 }
1305 
1306 #endif /* CONFIG_MESH */
1307 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
1308 
1309 
1310 #ifdef CONFIG_WNM_AP
1311 
hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data * hapd,const char * cmd)1312 int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
1313 					 const char *cmd)
1314 {
1315 	u8 addr[ETH_ALEN];
1316 	int disassoc_timer;
1317 	struct sta_info *sta;
1318 
1319 	if (hwaddr_aton(cmd, addr))
1320 		return -1;
1321 	if (cmd[17] != ' ')
1322 		return -1;
1323 	disassoc_timer = atoi(cmd + 17);
1324 
1325 	sta = ap_get_sta(hapd, addr);
1326 	if (sta == NULL) {
1327 		wpa_printf(MSG_DEBUG, "Station " MACSTR
1328 			   " not found for disassociation imminent message",
1329 			   MAC2STR(addr));
1330 		return -1;
1331 	}
1332 
1333 	return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
1334 }
1335 
1336 
hostapd_ctrl_iface_ess_disassoc(struct hostapd_data * hapd,const char * cmd)1337 int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
1338 				    const char *cmd)
1339 {
1340 	u8 addr[ETH_ALEN];
1341 	const char *url, *timerstr;
1342 	int disassoc_timer;
1343 	struct sta_info *sta;
1344 
1345 	if (hwaddr_aton(cmd, addr))
1346 		return -1;
1347 
1348 	sta = ap_get_sta(hapd, addr);
1349 	if (sta == NULL) {
1350 		wpa_printf(MSG_DEBUG, "Station " MACSTR
1351 			   " not found for ESS disassociation imminent message",
1352 			   MAC2STR(addr));
1353 		return -1;
1354 	}
1355 
1356 	timerstr = cmd + 17;
1357 	if (*timerstr != ' ')
1358 		return -1;
1359 	timerstr++;
1360 	disassoc_timer = atoi(timerstr);
1361 	if (disassoc_timer < 0 || disassoc_timer > 65535)
1362 		return -1;
1363 
1364 	url = os_strchr(timerstr, ' ');
1365 	if (url == NULL)
1366 		return -1;
1367 	url++;
1368 
1369 	return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
1370 }
1371 
1372 
hostapd_ctrl_iface_bss_tm_req(struct hostapd_data * hapd,const char * cmd)1373 int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data *hapd,
1374 				  const char *cmd)
1375 {
1376 	u8 addr[ETH_ALEN];
1377 	const char *pos, *end;
1378 	int disassoc_timer = 0;
1379 	struct sta_info *sta;
1380 	u8 req_mode = 0, valid_int = 0x01, dialog_token = 0x01;
1381 	u8 bss_term_dur[12];
1382 	char *url = NULL;
1383 	int ret;
1384 	u8 nei_rep[1000];
1385 	int nei_len;
1386 	u8 mbo[10];
1387 	size_t mbo_len = 0;
1388 
1389 	if (hwaddr_aton(cmd, addr)) {
1390 		wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
1391 		return -1;
1392 	}
1393 
1394 	sta = ap_get_sta(hapd, addr);
1395 	if (sta == NULL) {
1396 		wpa_printf(MSG_DEBUG, "Station " MACSTR
1397 			   " not found for BSS TM Request message",
1398 			   MAC2STR(addr));
1399 		return -1;
1400 	}
1401 
1402 	pos = os_strstr(cmd, " disassoc_timer=");
1403 	if (pos) {
1404 		pos += 16;
1405 		disassoc_timer = atoi(pos);
1406 		if (disassoc_timer < 0 || disassoc_timer > 65535) {
1407 			wpa_printf(MSG_DEBUG, "Invalid disassoc_timer");
1408 			return -1;
1409 		}
1410 	}
1411 
1412 	pos = os_strstr(cmd, " valid_int=");
1413 	if (pos) {
1414 		pos += 11;
1415 		valid_int = atoi(pos);
1416 	}
1417 
1418 	pos = os_strstr(cmd, " dialog_token=");
1419 	if (pos) {
1420 		pos += 14;
1421 		dialog_token = atoi(pos);
1422 	}
1423 
1424 	pos = os_strstr(cmd, " bss_term=");
1425 	if (pos) {
1426 		pos += 10;
1427 		req_mode |= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED;
1428 		/* TODO: TSF configurable/learnable */
1429 		bss_term_dur[0] = 4; /* Subelement ID */
1430 		bss_term_dur[1] = 10; /* Length */
1431 		os_memset(&bss_term_dur[2], 0, 8);
1432 		end = os_strchr(pos, ',');
1433 		if (end == NULL) {
1434 			wpa_printf(MSG_DEBUG, "Invalid bss_term data");
1435 			return -1;
1436 		}
1437 		end++;
1438 		WPA_PUT_LE16(&bss_term_dur[10], atoi(end));
1439 	}
1440 
1441 	nei_len = ieee802_11_parse_candidate_list(cmd, nei_rep,
1442 						  sizeof(nei_rep));
1443 	if (nei_len < 0)
1444 		return -1;
1445 
1446 	pos = os_strstr(cmd, " url=");
1447 	if (pos) {
1448 		size_t len;
1449 		pos += 5;
1450 		end = os_strchr(pos, ' ');
1451 		if (end)
1452 			len = end - pos;
1453 		else
1454 			len = os_strlen(pos);
1455 		url = os_malloc(len + 1);
1456 		if (url == NULL)
1457 			return -1;
1458 		os_memcpy(url, pos, len);
1459 		url[len] = '\0';
1460 		req_mode |= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
1461 	}
1462 
1463 	if (os_strstr(cmd, " pref=1"))
1464 		req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
1465 	if (os_strstr(cmd, " abridged=1"))
1466 		req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
1467 	if (os_strstr(cmd, " disassoc_imminent=1"))
1468 		req_mode |= WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
1469 	if (os_strstr(cmd, " link_removal_imminent=1"))
1470 		req_mode |= WNM_BSS_TM_REQ_LINK_REMOVAL_IMMINENT;
1471 
1472 #ifdef CONFIG_MBO
1473 	pos = os_strstr(cmd, "mbo=");
1474 	if (pos) {
1475 		unsigned int mbo_reason, cell_pref, reassoc_delay;
1476 		u8 *mbo_pos = mbo;
1477 
1478 		ret = sscanf(pos, "mbo=%u:%u:%u", &mbo_reason,
1479 			     &reassoc_delay, &cell_pref);
1480 		if (ret != 3) {
1481 			wpa_printf(MSG_DEBUG,
1482 				   "MBO requires three arguments: mbo=<reason>:<reassoc_delay>:<cell_pref>");
1483 			ret = -1;
1484 			goto fail;
1485 		}
1486 
1487 		if (mbo_reason > MBO_TRANSITION_REASON_PREMIUM_AP) {
1488 			wpa_printf(MSG_DEBUG,
1489 				   "Invalid MBO transition reason code %u",
1490 				   mbo_reason);
1491 			ret = -1;
1492 			goto fail;
1493 		}
1494 
1495 		/* Valid values for Cellular preference are: 0, 1, 255 */
1496 		if (cell_pref != 0 && cell_pref != 1 && cell_pref != 255) {
1497 			wpa_printf(MSG_DEBUG,
1498 				   "Invalid MBO cellular capability %u",
1499 				   cell_pref);
1500 			ret = -1;
1501 			goto fail;
1502 		}
1503 
1504 		if (reassoc_delay > 65535 ||
1505 		    (reassoc_delay &&
1506 		     !(req_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT))) {
1507 			wpa_printf(MSG_DEBUG,
1508 				   "MBO: Assoc retry delay is only valid in disassoc imminent mode");
1509 			ret = -1;
1510 			goto fail;
1511 		}
1512 
1513 		*mbo_pos++ = MBO_ATTR_ID_TRANSITION_REASON;
1514 		*mbo_pos++ = 1;
1515 		*mbo_pos++ = mbo_reason;
1516 		*mbo_pos++ = MBO_ATTR_ID_CELL_DATA_PREF;
1517 		*mbo_pos++ = 1;
1518 		*mbo_pos++ = cell_pref;
1519 
1520 		if (reassoc_delay) {
1521 			*mbo_pos++ = MBO_ATTR_ID_ASSOC_RETRY_DELAY;
1522 			*mbo_pos++ = 2;
1523 			WPA_PUT_LE16(mbo_pos, reassoc_delay);
1524 			mbo_pos += 2;
1525 		}
1526 
1527 		mbo_len = mbo_pos - mbo;
1528 	}
1529 #endif /* CONFIG_MBO */
1530 
1531 	ret = wnm_send_bss_tm_req(hapd, sta, req_mode, disassoc_timer,
1532 				  valid_int, bss_term_dur, dialog_token, url,
1533 				  nei_len ? nei_rep : NULL, nei_len,
1534 				  mbo_len ? mbo : NULL, mbo_len);
1535 #ifdef CONFIG_MBO
1536 fail:
1537 #endif /* CONFIG_MBO */
1538 	os_free(url);
1539 	return ret;
1540 }
1541 
1542 #endif /* CONFIG_WNM_AP */
1543 
1544 
hostapd_ctrl_iface_acl_del_mac(struct mac_acl_entry ** acl,int * num,const char * txtaddr)1545 int hostapd_ctrl_iface_acl_del_mac(struct mac_acl_entry **acl, int *num,
1546 				   const char *txtaddr)
1547 {
1548 	u8 addr[ETH_ALEN];
1549 	struct vlan_description vlan_id;
1550 
1551 	if (!(*num))
1552 		return 0;
1553 
1554 	if (hwaddr_aton(txtaddr, addr))
1555 		return -1;
1556 
1557 	if (hostapd_maclist_found(*acl, *num, addr, &vlan_id))
1558 		hostapd_remove_acl_mac(acl, num, addr);
1559 
1560 	return 0;
1561 }
1562 
1563 
hostapd_ctrl_iface_acl_clear_list(struct mac_acl_entry ** acl,int * num)1564 void hostapd_ctrl_iface_acl_clear_list(struct mac_acl_entry **acl,
1565 				       int *num)
1566 {
1567 	while (*num)
1568 		hostapd_remove_acl_mac(acl, num, (*acl)[0].addr);
1569 }
1570 
1571 
hostapd_ctrl_iface_acl_show_mac(struct mac_acl_entry * acl,int num,char * buf,size_t buflen)1572 int hostapd_ctrl_iface_acl_show_mac(struct mac_acl_entry *acl, int num,
1573 				    char *buf, size_t buflen)
1574 {
1575 	int i = 0, len = 0, ret = 0;
1576 
1577 	if (!acl)
1578 		return 0;
1579 
1580 	while (i < num) {
1581 		ret = os_snprintf(buf + len, buflen - len,
1582 				  MACSTR " VLAN_ID=%d\n",
1583 				  MAC2STR(acl[i].addr),
1584 				  acl[i].vlan_id.untagged);
1585 		if (ret < 0 || (size_t) ret >= buflen - len)
1586 			return len;
1587 		i++;
1588 		len += ret;
1589 	}
1590 	return len;
1591 }
1592 
1593 
hostapd_ctrl_iface_acl_add_mac(struct mac_acl_entry ** acl,int * num,const char * cmd)1594 int hostapd_ctrl_iface_acl_add_mac(struct mac_acl_entry **acl, int *num,
1595 				   const char *cmd)
1596 {
1597 	u8 addr[ETH_ALEN];
1598 	struct vlan_description vlan_id;
1599 	int ret = 0, vlanid = 0;
1600 	const char *pos;
1601 
1602 	if (hwaddr_aton(cmd, addr))
1603 		return -1;
1604 
1605 	pos = os_strstr(cmd, "VLAN_ID=");
1606 	if (pos)
1607 		vlanid = atoi(pos + 8);
1608 
1609 	if (!hostapd_maclist_found(*acl, *num, addr, &vlan_id)) {
1610 		ret = hostapd_add_acl_maclist(acl, num, vlanid, addr);
1611 		if (ret != -1 && *acl)
1612 			qsort(*acl, *num, sizeof(**acl), hostapd_acl_comp);
1613 	}
1614 
1615 	return ret < 0 ? -1 : 0;
1616 }
1617 
1618 
hostapd_disassoc_accept_mac(struct hostapd_data * hapd)1619 int hostapd_disassoc_accept_mac(struct hostapd_data *hapd)
1620 {
1621 	struct sta_info *sta;
1622 	struct vlan_description vlan_id;
1623 
1624 	if (hapd->conf->macaddr_acl != DENY_UNLESS_ACCEPTED)
1625 		return 0;
1626 
1627 	for (sta = hapd->sta_list; sta; sta = sta->next) {
1628 		if (!hostapd_maclist_found(hapd->conf->accept_mac,
1629 					   hapd->conf->num_accept_mac,
1630 					   sta->addr, &vlan_id) ||
1631 		    (vlan_id.notempty &&
1632 		     vlan_compare(&vlan_id, sta->vlan_desc)))
1633 			ap_sta_disconnect(hapd, sta, sta->addr,
1634 					  WLAN_REASON_UNSPECIFIED);
1635 	}
1636 
1637 	return 0;
1638 }
1639 
1640 
hostapd_disassoc_deny_mac(struct hostapd_data * hapd)1641 int hostapd_disassoc_deny_mac(struct hostapd_data *hapd)
1642 {
1643 	struct sta_info *sta;
1644 	struct vlan_description vlan_id;
1645 
1646 	for (sta = hapd->sta_list; sta; sta = sta->next) {
1647 		if (hostapd_maclist_found(hapd->conf->deny_mac,
1648 					  hapd->conf->num_deny_mac, sta->addr,
1649 					  &vlan_id) &&
1650 		    (!vlan_id.notempty ||
1651 		     !vlan_compare(&vlan_id, sta->vlan_desc)))
1652 			ap_sta_disconnect(hapd, sta, sta->addr,
1653 					  WLAN_REASON_UNSPECIFIED);
1654 	}
1655 
1656 	return 0;
1657 }
1658