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