• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Control interface for shared AP commands
3  * Copyright (c) 2004-2019, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "utils/includes.h"
10 
11 #include "utils/common.h"
12 #include "common/ieee802_11_defs.h"
13 #include "common/sae.h"
14 #include "eapol_auth/eapol_auth_sm.h"
15 #include "fst/fst_ctrl_iface.h"
16 #include "hostapd.h"
17 #include "ieee802_1x.h"
18 #include "wpa_auth.h"
19 #include "ieee802_11.h"
20 #include "sta_info.h"
21 #include "wps_hostapd.h"
22 #include "p2p_hostapd.h"
23 #include "ctrl_iface_ap.h"
24 #include "ap_drv_ops.h"
25 #include "mbo_ap.h"
26 #include "taxonomy.h"
27 #include "wnm_ap.h"
28 
29 
hostapd_write_ht_mcs_bitmask(char * buf,size_t buflen,size_t curr_len,const u8 * mcs_set)30 static size_t hostapd_write_ht_mcs_bitmask(char *buf, size_t buflen,
31 					   size_t curr_len, const u8 *mcs_set)
32 {
33 	int ret;
34 	size_t len = curr_len;
35 
36 	ret = os_snprintf(buf + len, buflen - len,
37 			  "ht_mcs_bitmask=");
38 	if (os_snprintf_error(buflen - len, ret))
39 		return len;
40 	len += ret;
41 
42 	/* 77 first bits (+ 3 reserved bits) */
43 	len += wpa_snprintf_hex(buf + len, buflen - len, mcs_set, 10);
44 
45 	ret = os_snprintf(buf + len, buflen - len, "\n");
46 	if (os_snprintf_error(buflen - len, ret))
47 		return curr_len;
48 	len += ret;
49 
50 	return len;
51 }
52 
53 
hostapd_get_sta_conn_time(struct sta_info * sta,struct hostap_sta_driver_data * data,char * buf,size_t buflen)54 static int hostapd_get_sta_conn_time(struct sta_info *sta,
55 				     struct hostap_sta_driver_data *data,
56 				     char *buf, size_t buflen)
57 {
58 	struct os_reltime age;
59 	unsigned long secs;
60 	int ret;
61 
62 	if (sta->connected_time.sec) {
63 		/* Locally maintained time in AP mode */
64 		os_reltime_age(&sta->connected_time, &age);
65 		secs = (unsigned long) age.sec;
66 	} else if (data->flags & STA_DRV_DATA_CONN_TIME) {
67 		/* Time from the driver in mesh mode */
68 		secs = data->connected_sec;
69 	} else {
70 		return 0;
71 	}
72 
73 	ret = os_snprintf(buf, buflen, "connected_time=%lu\n", secs);
74 	if (os_snprintf_error(buflen, ret))
75 		return 0;
76 	return ret;
77 }
78 
79 
hostapd_get_sta_info(struct hostapd_data * hapd,struct sta_info * sta,char * buf,size_t buflen)80 static int hostapd_get_sta_info(struct hostapd_data *hapd,
81 				struct sta_info *sta,
82 				char *buf, size_t buflen)
83 {
84 	struct hostap_sta_driver_data data;
85 	int ret;
86 	int len = 0;
87 
88 	if (hostapd_drv_read_sta_data(hapd, &data, sta->addr) < 0)
89 		return 0;
90 
91 	ret = os_snprintf(buf, buflen, "rx_packets=%lu\ntx_packets=%lu\n"
92 			  "rx_bytes=%llu\ntx_bytes=%llu\ninactive_msec=%lu\n"
93 			  "signal=%d\n",
94 			  data.rx_packets, data.tx_packets,
95 			  data.rx_bytes, data.tx_bytes, data.inactive_msec,
96 			  data.signal);
97 	if (os_snprintf_error(buflen, ret))
98 		return 0;
99 	len += ret;
100 
101 	ret = os_snprintf(buf + len, buflen - len, "rx_rate_info=%lu",
102 			  data.current_rx_rate / 100);
103 	if (os_snprintf_error(buflen - len, ret))
104 		return len;
105 	len += ret;
106 	if (data.flags & STA_DRV_DATA_RX_MCS) {
107 		ret = os_snprintf(buf + len, buflen - len, " mcs %u",
108 				  data.rx_mcs);
109 		if (!os_snprintf_error(buflen - len, ret))
110 			len += ret;
111 	}
112 	if (data.flags & STA_DRV_DATA_RX_VHT_MCS) {
113 		ret = os_snprintf(buf + len, buflen - len, " vhtmcs %u",
114 				  data.rx_vhtmcs);
115 		if (!os_snprintf_error(buflen - len, ret))
116 			len += ret;
117 	}
118 	if (data.flags & STA_DRV_DATA_RX_VHT_NSS) {
119 		ret = os_snprintf(buf + len, buflen - len, " vhtnss %u",
120 				  data.rx_vht_nss);
121 		if (!os_snprintf_error(buflen - len, ret))
122 			len += ret;
123 	}
124 	if (data.flags & STA_DRV_DATA_RX_SHORT_GI) {
125 		ret = os_snprintf(buf + len, buflen - len, " shortGI");
126 		if (!os_snprintf_error(buflen - len, ret))
127 			len += ret;
128 	}
129 	ret = os_snprintf(buf + len, buflen - len, "\n");
130 	if (!os_snprintf_error(buflen - len, ret))
131 		len += ret;
132 
133 	ret = os_snprintf(buf + len, buflen - len, "tx_rate_info=%lu",
134 			  data.current_tx_rate / 100);
135 	if (os_snprintf_error(buflen - len, ret))
136 		return len;
137 	len += ret;
138 	if (data.flags & STA_DRV_DATA_TX_MCS) {
139 		ret = os_snprintf(buf + len, buflen - len, " mcs %u",
140 				  data.tx_mcs);
141 		if (!os_snprintf_error(buflen - len, ret))
142 			len += ret;
143 	}
144 	if (data.flags & STA_DRV_DATA_TX_VHT_MCS) {
145 		ret = os_snprintf(buf + len, buflen - len, " vhtmcs %u",
146 				  data.tx_vhtmcs);
147 		if (!os_snprintf_error(buflen - len, ret))
148 			len += ret;
149 	}
150 	if (data.flags & STA_DRV_DATA_TX_VHT_NSS) {
151 		ret = os_snprintf(buf + len, buflen - len, " vhtnss %u",
152 				  data.tx_vht_nss);
153 		if (!os_snprintf_error(buflen - len, ret))
154 			len += ret;
155 	}
156 	if (data.flags & STA_DRV_DATA_TX_SHORT_GI) {
157 		ret = os_snprintf(buf + len, buflen - len, " shortGI");
158 		if (!os_snprintf_error(buflen - len, ret))
159 			len += ret;
160 	}
161 	ret = os_snprintf(buf + len, buflen - len, "\n");
162 	if (!os_snprintf_error(buflen - len, ret))
163 		len += ret;
164 
165 	if ((sta->flags & WLAN_STA_VHT) && sta->vht_capabilities) {
166 		ret = os_snprintf(buf + len, buflen - len,
167 				  "rx_vht_mcs_map=%04x\n"
168 				  "tx_vht_mcs_map=%04x\n",
169 				  le_to_host16(sta->vht_capabilities->
170 					       vht_supported_mcs_set.rx_map),
171 				  le_to_host16(sta->vht_capabilities->
172 					       vht_supported_mcs_set.tx_map));
173 		if (!os_snprintf_error(buflen - len, ret))
174 			len += ret;
175 	}
176 
177 	if ((sta->flags & WLAN_STA_HT) && sta->ht_capabilities) {
178 		len = hostapd_write_ht_mcs_bitmask(buf, buflen, len,
179 						   sta->ht_capabilities->
180 						   supported_mcs_set);
181 	}
182 
183 	if (data.flags & STA_DRV_DATA_LAST_ACK_RSSI) {
184 		ret = os_snprintf(buf + len, buflen - len,
185 				  "last_ack_signal=%d\n", data.last_ack_rssi);
186 		if (!os_snprintf_error(buflen - len, ret))
187 			len += ret;
188 	}
189 
190 	len += hostapd_get_sta_conn_time(sta, &data, buf + len, buflen - len);
191 
192 	return len;
193 }
194 
195 
timeout_next_str(int val)196 static const char * timeout_next_str(int val)
197 {
198 	switch (val) {
199 	case STA_NULLFUNC:
200 		return "NULLFUNC POLL";
201 	case STA_DISASSOC:
202 		return "DISASSOC";
203 	case STA_DEAUTH:
204 		return "DEAUTH";
205 	case STA_REMOVE:
206 		return "REMOVE";
207 	case STA_DISASSOC_FROM_CLI:
208 		return "DISASSOC_FROM_CLI";
209 	default:
210 		return "?";
211 	}
212 }
213 
214 
hw_mode_str(enum hostapd_hw_mode mode)215 static const char * hw_mode_str(enum hostapd_hw_mode mode)
216 {
217 	switch (mode) {
218 	case HOSTAPD_MODE_IEEE80211B:
219 		return "b";
220 	case HOSTAPD_MODE_IEEE80211G:
221 		return "g";
222 	case HOSTAPD_MODE_IEEE80211A:
223 		return "a";
224 	case HOSTAPD_MODE_IEEE80211AD:
225 		return "ad";
226 	case HOSTAPD_MODE_IEEE80211ANY:
227 		return "any";
228 	case NUM_HOSTAPD_MODES:
229 		return "invalid";
230 	}
231 	return "unknown";
232 }
233 
234 
hostapd_ctrl_iface_sta_mib(struct hostapd_data * hapd,struct sta_info * sta,char * buf,size_t buflen)235 static int hostapd_ctrl_iface_sta_mib(struct hostapd_data *hapd,
236 				      struct sta_info *sta,
237 				      char *buf, size_t buflen)
238 {
239 	int len, res, ret, i;
240 	const char *keyid;
241 	const u8 *dpp_pkhash;
242 
243 	if (!sta)
244 		return 0;
245 
246 	len = 0;
247 	ret = os_snprintf(buf + len, buflen - len, MACSTR "\nflags=",
248 			  MAC2STR(sta->addr));
249 	if (os_snprintf_error(buflen - len, ret))
250 		return len;
251 	len += ret;
252 
253 	ret = ap_sta_flags_txt(sta->flags, buf + len, buflen - len);
254 	if (ret < 0)
255 		return len;
256 	len += ret;
257 
258 	ret = os_snprintf(buf + len, buflen - len, "\naid=%d\ncapability=0x%x\n"
259 			  "listen_interval=%d\nsupported_rates=",
260 			  sta->aid, sta->capability, sta->listen_interval);
261 	if (os_snprintf_error(buflen - len, ret))
262 		return len;
263 	len += ret;
264 
265 	for (i = 0; i < sta->supported_rates_len; i++) {
266 		ret = os_snprintf(buf + len, buflen - len, "%02x%s",
267 				  sta->supported_rates[i],
268 				  i + 1 < sta->supported_rates_len ? " " : "");
269 		if (os_snprintf_error(buflen - len, ret))
270 			return len;
271 		len += ret;
272 	}
273 
274 	ret = os_snprintf(buf + len, buflen - len, "\ntimeout_next=%s\n",
275 			  timeout_next_str(sta->timeout_next));
276 	if (os_snprintf_error(buflen - len, ret))
277 		return len;
278 	len += ret;
279 
280 	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_msg_global_only(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 	wpa_printf(MSG_DEBUG, "P2P: Disconnect STA " MACSTR_SEC
584 		" with minor reason code %u (stype=%u (%s))",
585 		MAC2STR_SEC(addr), minor_reason_code, stype,
586 		fc2str(le_to_host16(mgmt->frame_control)));
587 
588 	os_memcpy(mgmt->da, addr, ETH_ALEN);
589 	os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
590 	os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
591 	if (stype == WLAN_FC_STYPE_DEAUTH) {
592 		mgmt->u.deauth.reason_code =
593 			host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
594 		pos = mgmt->u.deauth.variable;
595 	} else {
596 		mgmt->u.disassoc.reason_code =
597 			host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
598 		pos = mgmt->u.disassoc.variable;
599 	}
600 
601 	*pos++ = WLAN_EID_VENDOR_SPECIFIC;
602 	*pos++ = 4 + 3 + 1;
603 	WPA_PUT_BE32(pos, P2P_IE_VENDOR_TYPE);
604 	pos += 4;
605 
606 	*pos++ = P2P_ATTR_MINOR_REASON_CODE;
607 	WPA_PUT_LE16(pos, 1);
608 	pos += 2;
609 	*pos++ = minor_reason_code;
610 
611 	ret = hostapd_drv_send_mlme(hapd, mgmt, pos - (u8 *) mgmt, 0, NULL, 0,
612 				    0);
613 	os_free(mgmt);
614 
615 	return ret < 0 ? -1 : 0;
616 }
617 #endif /* CONFIG_P2P_MANAGER */
618 
619 
hostapd_ctrl_iface_deauthenticate(struct hostapd_data * hapd,const char * txtaddr)620 int hostapd_ctrl_iface_deauthenticate(struct hostapd_data *hapd,
621 				      const char *txtaddr)
622 {
623 	u8 addr[ETH_ALEN];
624 	struct sta_info *sta;
625 	const char *pos;
626 	u16 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
627 
628 	wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DEAUTHENTICATE %s",
629 		txtaddr);
630 
631 	if (hwaddr_aton(txtaddr, addr))
632 		return -1;
633 
634 	pos = os_strstr(txtaddr, " reason=");
635 	if (pos)
636 		reason = atoi(pos + 8);
637 
638 	pos = os_strstr(txtaddr, " test=");
639 	if (pos) {
640 		struct ieee80211_mgmt mgmt;
641 		int encrypt;
642 
643 		pos += 6;
644 		encrypt = atoi(pos);
645 		os_memset(&mgmt, 0, sizeof(mgmt));
646 		mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
647 						  WLAN_FC_STYPE_DEAUTH);
648 		os_memcpy(mgmt.da, addr, ETH_ALEN);
649 		os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
650 		os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
651 		mgmt.u.deauth.reason_code = host_to_le16(reason);
652 		if (hostapd_drv_send_mlme(hapd, (u8 *) &mgmt,
653 					  IEEE80211_HDRLEN +
654 					  sizeof(mgmt.u.deauth),
655 					  0, NULL, 0, !encrypt) < 0)
656 			return -1;
657 		return 0;
658 	}
659 
660 #ifdef CONFIG_P2P_MANAGER
661 	pos = os_strstr(txtaddr, " p2p=");
662 	if (pos) {
663 		return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DEAUTH,
664 					      atoi(pos + 5), addr);
665 	}
666 #endif /* CONFIG_P2P_MANAGER */
667 
668 	if (os_strstr(txtaddr, " tx=0"))
669 		hostapd_drv_sta_remove(hapd, addr);
670 	else
671 		hostapd_drv_sta_deauth(hapd, addr, reason);
672 	sta = ap_get_sta(hapd, addr);
673 	if (sta)
674 		ap_sta_deauthenticate(hapd, sta, reason);
675 	else if (addr[0] == 0xff)
676 		hostapd_free_stas(hapd);
677 
678 	return 0;
679 }
680 
681 
hostapd_ctrl_iface_disassociate(struct hostapd_data * hapd,const char * txtaddr)682 int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd,
683 				    const char *txtaddr)
684 {
685 	u8 addr[ETH_ALEN];
686 	struct sta_info *sta;
687 	const char *pos;
688 	u16 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
689 
690 	wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DISASSOCIATE %s",
691 		txtaddr);
692 
693 	if (hwaddr_aton(txtaddr, addr))
694 		return -1;
695 
696 	pos = os_strstr(txtaddr, " reason=");
697 	if (pos)
698 		reason = atoi(pos + 8);
699 
700 	pos = os_strstr(txtaddr, " test=");
701 	if (pos) {
702 		struct ieee80211_mgmt mgmt;
703 		int encrypt;
704 
705 		pos += 6;
706 		encrypt = atoi(pos);
707 		os_memset(&mgmt, 0, sizeof(mgmt));
708 		mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
709 						  WLAN_FC_STYPE_DISASSOC);
710 		os_memcpy(mgmt.da, addr, ETH_ALEN);
711 		os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
712 		os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
713 		mgmt.u.disassoc.reason_code = host_to_le16(reason);
714 		if (hostapd_drv_send_mlme(hapd, (u8 *) &mgmt,
715 					  IEEE80211_HDRLEN +
716 					  sizeof(mgmt.u.deauth),
717 					  0, NULL, 0, !encrypt) < 0)
718 			return -1;
719 		return 0;
720 	}
721 
722 #ifdef CONFIG_P2P_MANAGER
723 	pos = os_strstr(txtaddr, " p2p=");
724 	if (pos) {
725 		return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DISASSOC,
726 					      atoi(pos + 5), addr);
727 	}
728 #endif /* CONFIG_P2P_MANAGER */
729 
730 	if (os_strstr(txtaddr, " tx=0"))
731 		hostapd_drv_sta_remove(hapd, addr);
732 	else
733 		hostapd_drv_sta_disassoc(hapd, addr, reason);
734 	sta = ap_get_sta(hapd, addr);
735 	if (sta)
736 		ap_sta_disassociate(hapd, sta, reason);
737 	else if (addr[0] == 0xff)
738 		hostapd_free_stas(hapd);
739 
740 	return 0;
741 }
742 
743 
744 #ifdef CONFIG_TAXONOMY
hostapd_ctrl_iface_signature(struct hostapd_data * hapd,const char * txtaddr,char * buf,size_t buflen)745 int hostapd_ctrl_iface_signature(struct hostapd_data *hapd,
746 				 const char *txtaddr,
747 				 char *buf, size_t buflen)
748 {
749 	u8 addr[ETH_ALEN];
750 	struct sta_info *sta;
751 
752 	wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE SIGNATURE %s", txtaddr);
753 
754 	if (hwaddr_aton(txtaddr, addr))
755 		return -1;
756 
757 	sta = ap_get_sta(hapd, addr);
758 	if (!sta)
759 		return -1;
760 
761 	return retrieve_sta_taxonomy(hapd, sta, buf, buflen);
762 }
763 #endif /* CONFIG_TAXONOMY */
764 
765 
hostapd_ctrl_iface_poll_sta(struct hostapd_data * hapd,const char * txtaddr)766 int hostapd_ctrl_iface_poll_sta(struct hostapd_data *hapd,
767 				const char *txtaddr)
768 {
769 	u8 addr[ETH_ALEN];
770 	struct sta_info *sta;
771 
772 	wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE POLL_STA %s", txtaddr);
773 
774 	if (hwaddr_aton(txtaddr, addr))
775 		return -1;
776 
777 	sta = ap_get_sta(hapd, addr);
778 	if (!sta)
779 		return -1;
780 
781 	hostapd_drv_poll_client(hapd, hapd->own_addr, addr,
782 				sta->flags & WLAN_STA_WMM);
783 	return 0;
784 }
785 
786 
hostapd_ctrl_iface_status(struct hostapd_data * hapd,char * buf,size_t buflen)787 int hostapd_ctrl_iface_status(struct hostapd_data *hapd, char *buf,
788 			      size_t buflen)
789 {
790 	struct hostapd_iface *iface = hapd->iface;
791 	struct hostapd_hw_modes *mode = iface->current_mode;
792 	struct hostapd_config *iconf = hapd->iconf;
793 	int len = 0, ret, j;
794 	size_t i;
795 
796 	ret = os_snprintf(buf + len, buflen - len,
797 			  "state=%s\n"
798 			  "phy=%s\n"
799 			  "freq=%d\n"
800 			  "num_sta_non_erp=%d\n"
801 			  "num_sta_no_short_slot_time=%d\n"
802 			  "num_sta_no_short_preamble=%d\n"
803 			  "olbc=%d\n"
804 			  "num_sta_ht_no_gf=%d\n"
805 			  "num_sta_no_ht=%d\n"
806 			  "num_sta_ht_20_mhz=%d\n"
807 			  "num_sta_ht40_intolerant=%d\n"
808 			  "olbc_ht=%d\n"
809 			  "ht_op_mode=0x%x\n",
810 			  hostapd_state_text(iface->state),
811 			  iface->phy,
812 			  iface->freq,
813 			  iface->num_sta_non_erp,
814 			  iface->num_sta_no_short_slot_time,
815 			  iface->num_sta_no_short_preamble,
816 			  iface->olbc,
817 			  iface->num_sta_ht_no_gf,
818 			  iface->num_sta_no_ht,
819 			  iface->num_sta_ht_20mhz,
820 			  iface->num_sta_ht40_intolerant,
821 			  iface->olbc_ht,
822 			  iface->ht_op_mode);
823 	if (os_snprintf_error(buflen - len, ret))
824 		return len;
825 	len += ret;
826 
827 	if (mode) {
828 		ret = os_snprintf(buf + len, buflen - len, "hw_mode=%s\n",
829 				  hw_mode_str(mode->mode));
830 		if (os_snprintf_error(buflen - len, ret))
831 			return len;
832 		len += ret;
833 	}
834 
835 	if (iconf->country[0] && iconf->country[1]) {
836 		ret = os_snprintf(buf + len, buflen - len,
837 				  "country_code=%c%c\ncountry3=0x%X\n",
838 				  iconf->country[0], iconf->country[1],
839 				  iconf->country[2]);
840 		if (os_snprintf_error(buflen - len, ret))
841 			return len;
842 		len += ret;
843 	}
844 
845 	if (!iface->cac_started || !iface->dfs_cac_ms) {
846 		ret = os_snprintf(buf + len, buflen - len,
847 				  "cac_time_seconds=%d\n"
848 				  "cac_time_left_seconds=N/A\n",
849 				  iface->dfs_cac_ms / 1000);
850 	} else {
851 		/* CAC started and CAC time set - calculate remaining time */
852 		struct os_reltime now;
853 		long left_time;
854 
855 		os_reltime_age(&iface->dfs_cac_start, &now);
856 		left_time = (long) iface->dfs_cac_ms / 1000 - now.sec;
857 		ret = os_snprintf(buf + len, buflen - len,
858 				  "cac_time_seconds=%u\n"
859 				  "cac_time_left_seconds=%lu\n",
860 				  iface->dfs_cac_ms / 1000,
861 				  left_time > 0 ? left_time : 0);
862 	}
863 	if (os_snprintf_error(buflen - len, ret))
864 		return len;
865 	len += ret;
866 
867 	ret = os_snprintf(buf + len, buflen - len,
868 			  "channel=%u\n"
869 			  "edmg_enable=%d\n"
870 			  "edmg_channel=%d\n"
871 			  "secondary_channel=%d\n"
872 			  "ieee80211n=%d\n"
873 			  "ieee80211ac=%d\n"
874 			  "ieee80211ax=%d\n"
875 			  "ieee80211be=%d\n"
876 			  "beacon_int=%u\n"
877 			  "dtim_period=%d\n",
878 			  iface->conf->channel,
879 			  iface->conf->enable_edmg,
880 			  iface->conf->edmg_channel,
881 			  iface->conf->ieee80211n && !hapd->conf->disable_11n ?
882 			  iface->conf->secondary_channel : 0,
883 			  iface->conf->ieee80211n && !hapd->conf->disable_11n,
884 			  iface->conf->ieee80211ac &&
885 			  !hapd->conf->disable_11ac,
886 			  iface->conf->ieee80211ax &&
887 			  !hapd->conf->disable_11ax,
888 			  iface->conf->ieee80211be &&
889 			  !hapd->conf->disable_11be,
890 			  iface->conf->beacon_int,
891 			  hapd->conf->dtim_period);
892 	if (os_snprintf_error(buflen - len, ret))
893 		return len;
894 	len += ret;
895 
896 #ifdef CONFIG_IEEE80211BE
897 	if (iface->conf->ieee80211be && !hapd->conf->disable_11be) {
898 		ret = os_snprintf(buf + len, buflen - len,
899 				  "eht_oper_chwidth=%d\n"
900 				  "eht_oper_centr_freq_seg0_idx=%d\n",
901 				  iface->conf->eht_oper_chwidth,
902 				  iface->conf->eht_oper_centr_freq_seg0_idx);
903 		if (os_snprintf_error(buflen - len, ret))
904 			return len;
905 		len += ret;
906 
907 		if (is_6ghz_op_class(iface->conf->op_class) &&
908 		    hostapd_get_oper_chwidth(iface->conf) ==
909 		    CONF_OPER_CHWIDTH_320MHZ) {
910 			ret = os_snprintf(buf + len, buflen - len,
911 					  "eht_bw320_offset=%d\n",
912 					  iface->conf->eht_bw320_offset);
913 			if (os_snprintf_error(buflen - len, ret))
914 				return len;
915 			len += ret;
916 		}
917 
918 		if (hapd->conf->mld_ap) {
919 			struct hostapd_data *link_bss;
920 
921 			ret = os_snprintf(buf + len, buflen - len,
922 					  "num_links=%d\n",
923 					  hapd->mld->num_links);
924 			if (os_snprintf_error(buflen - len, ret))
925 				return len;
926 			len += ret;
927 
928 			/* Self BSS */
929 			ret = os_snprintf(buf + len, buflen - len,
930 					  "link_id=%d\n"
931 					  "link_addr=" MACSTR "\n",
932 					  hapd->mld_link_id,
933 					  MAC2STR(hapd->own_addr));
934 			if (os_snprintf_error(buflen - len, ret))
935 				return len;
936 			len += ret;
937 
938 			/* Partner BSSs */
939 			for_each_mld_link(link_bss, hapd) {
940 				if (link_bss == hapd)
941 					continue;
942 
943 				ret = os_snprintf(buf + len, buflen - len,
944 						  "partner_link[%d]=" MACSTR
945 						  "\n",
946 						  link_bss->mld_link_id,
947 						  MAC2STR(link_bss->own_addr));
948 				if (os_snprintf_error(buflen - len, ret))
949 					return len;
950 				len += ret;
951 			}
952 		}
953 	}
954 #endif /* CONFIG_IEEE80211BE */
955 
956 #ifdef CONFIG_IEEE80211AX
957 	if (iface->conf->ieee80211ax && !hapd->conf->disable_11ax) {
958 		ret = os_snprintf(buf + len, buflen - len,
959 				  "he_oper_chwidth=%d\n"
960 				  "he_oper_centr_freq_seg0_idx=%d\n"
961 				  "he_oper_centr_freq_seg1_idx=%d\n",
962 				  iface->conf->he_oper_chwidth,
963 				  iface->conf->he_oper_centr_freq_seg0_idx,
964 				  iface->conf->he_oper_centr_freq_seg1_idx);
965 		if (os_snprintf_error(buflen - len, ret))
966 			return len;
967 		len += ret;
968 
969 		if (!iconf->he_op.he_bss_color_disabled &&
970 		    iconf->he_op.he_bss_color) {
971 			ret = os_snprintf(buf + len, buflen - len,
972 					  "he_bss_color=%d\n",
973 					  iconf->he_op.he_bss_color);
974 			if (os_snprintf_error(buflen - len, ret))
975 				return len;
976 			len += ret;
977 		}
978 	}
979 #endif /* CONFIG_IEEE80211AX */
980 
981 	if (iface->conf->ieee80211ac && !hapd->conf->disable_11ac) {
982 		ret = os_snprintf(buf + len, buflen - len,
983 				  "vht_oper_chwidth=%d\n"
984 				  "vht_oper_centr_freq_seg0_idx=%d\n"
985 				  "vht_oper_centr_freq_seg1_idx=%d\n"
986 				  "vht_caps_info=%08x\n",
987 				  iface->conf->vht_oper_chwidth,
988 				  iface->conf->vht_oper_centr_freq_seg0_idx,
989 				  iface->conf->vht_oper_centr_freq_seg1_idx,
990 				  iface->conf->vht_capab);
991 		if (os_snprintf_error(buflen - len, ret))
992 			return len;
993 		len += ret;
994 	}
995 
996 	if (iface->conf->ieee80211ac && !hapd->conf->disable_11ac && mode) {
997 		u16 rxmap = WPA_GET_LE16(&mode->vht_mcs_set[0]);
998 		u16 txmap = WPA_GET_LE16(&mode->vht_mcs_set[4]);
999 
1000 		ret = os_snprintf(buf + len, buflen - len,
1001 				  "rx_vht_mcs_map=%04x\n"
1002 				  "tx_vht_mcs_map=%04x\n",
1003 				  rxmap, txmap);
1004 		if (os_snprintf_error(buflen - len, ret))
1005 			return len;
1006 		len += ret;
1007 	}
1008 
1009 	if (iface->conf->ieee80211n && !hapd->conf->disable_11n) {
1010 		ret = os_snprintf(buf + len, buflen - len,
1011 				  "ht_caps_info=%04x\n",
1012 				  hapd->iconf->ht_capab);
1013 		if (os_snprintf_error(buflen - len, ret))
1014 			return len;
1015 		len += ret;
1016 	}
1017 
1018 	if (iface->conf->ieee80211n && !hapd->conf->disable_11n && mode) {
1019 		len = hostapd_write_ht_mcs_bitmask(buf, buflen, len,
1020 						   mode->mcs_set);
1021 	}
1022 
1023 	if (iface->current_rates && iface->num_rates) {
1024 		ret = os_snprintf(buf + len, buflen - len, "supported_rates=");
1025 		if (os_snprintf_error(buflen - len, ret))
1026 			return len;
1027 		len += ret;
1028 
1029 		for (j = 0; j < iface->num_rates; j++) {
1030 			ret = os_snprintf(buf + len, buflen - len, "%s%02x",
1031 					  j > 0 ? " " : "",
1032 					  iface->current_rates[j].rate / 5);
1033 			if (os_snprintf_error(buflen - len, ret))
1034 				return len;
1035 			len += ret;
1036 		}
1037 		ret = os_snprintf(buf + len, buflen - len, "\n");
1038 		if (os_snprintf_error(buflen - len, ret))
1039 			return len;
1040 		len += ret;
1041 	}
1042 
1043 	for (j = 0; mode && j < mode->num_channels; j++) {
1044 		if (mode->channels[j].freq == iface->freq) {
1045 			ret = os_snprintf(buf + len, buflen - len,
1046 					  "max_txpower=%u\n",
1047 					  mode->channels[j].max_tx_power);
1048 			if (os_snprintf_error(buflen - len, ret))
1049 				return len;
1050 			len += ret;
1051 			break;
1052 		}
1053 	}
1054 
1055 	for (i = 0; i < iface->num_bss; i++) {
1056 		struct hostapd_data *bss = iface->bss[i];
1057 		ret = os_snprintf(buf + len, buflen - len,
1058 				  "bss[%d]=%s\n"
1059 				  "bssid[%d]=" MACSTR "\n"
1060 				  "ssid[%d]=%s\n"
1061 				  "num_sta[%d]=%d\n",
1062 				  (int) i, bss->conf->iface,
1063 				  (int) i, MAC2STR(bss->own_addr),
1064 				  (int) i,
1065 				  wpa_ssid_txt(bss->conf->ssid.ssid,
1066 					       bss->conf->ssid.ssid_len),
1067 				  (int) i, bss->num_sta);
1068 		if (os_snprintf_error(buflen - len, ret))
1069 			return len;
1070 		len += ret;
1071 
1072 #ifdef CONFIG_IEEE80211BE
1073 		if (bss->conf->mld_ap) {
1074 			ret = os_snprintf(buf + len, buflen - len,
1075 					  "mld_addr[%d]=" MACSTR "\n"
1076 					  "mld_id[%d]=%d\n"
1077 					  "mld_link_id[%d]=%d\n",
1078 					  (int) i, MAC2STR(bss->mld->mld_addr),
1079 					  (int) i, hostapd_get_mld_id(bss),
1080 					  (int) i, bss->mld_link_id);
1081 			if (os_snprintf_error(buflen - len, ret))
1082 				return len;
1083 			len += ret;
1084 		}
1085 #endif /* CONFIG_IEEE80211BE */
1086 	}
1087 
1088 	if (hapd->conf->chan_util_avg_period) {
1089 		ret = os_snprintf(buf + len, buflen - len,
1090 				  "chan_util_avg=%u\n",
1091 				  iface->chan_util_average);
1092 		if (os_snprintf_error(buflen - len, ret))
1093 			return len;
1094 		len += ret;
1095 	}
1096 
1097 	return len;
1098 }
1099 
1100 
hostapd_parse_csa_settings(const char * pos,struct csa_settings * settings)1101 int hostapd_parse_csa_settings(const char *pos,
1102 			       struct csa_settings *settings)
1103 {
1104 	char *end;
1105 
1106 	os_memset(settings, 0, sizeof(*settings));
1107 	settings->cs_count = strtol(pos, &end, 10);
1108 	if (pos == end) {
1109 		wpa_printf(MSG_ERROR, "chanswitch: invalid cs_count provided");
1110 		return -1;
1111 	}
1112 
1113 	settings->freq_params.freq = atoi(end);
1114 	if (settings->freq_params.freq == 0) {
1115 		wpa_printf(MSG_ERROR, "chanswitch: invalid freq provided");
1116 		return -1;
1117 	}
1118 
1119 #define SET_CSA_SETTING(str) \
1120 	do { \
1121 		const char *pos2 = os_strstr(pos, " " #str "="); \
1122 		if (pos2) { \
1123 			pos2 += sizeof(" " #str "=") - 1; \
1124 			settings->freq_params.str = atoi(pos2); \
1125 		} \
1126 	} while (0)
1127 
1128 #define SET_CSA_SETTING_EXT(str) \
1129 	do { \
1130 		const char *pos2 = os_strstr(pos, " " #str "="); \
1131 		if (pos2) { \
1132 			pos2 += sizeof(" " #str "=") - 1; \
1133 			settings->str = atoi(pos2); \
1134 		} \
1135 	} while (0)
1136 
1137 	SET_CSA_SETTING(center_freq1);
1138 	SET_CSA_SETTING(center_freq2);
1139 	SET_CSA_SETTING(bandwidth);
1140 	SET_CSA_SETTING(sec_channel_offset);
1141 	SET_CSA_SETTING_EXT(punct_bitmap);
1142 	settings->freq_params.ht_enabled = !!os_strstr(pos, " ht");
1143 	settings->freq_params.vht_enabled = !!os_strstr(pos, " vht");
1144 	settings->freq_params.he_enabled = !!os_strstr(pos, " he");
1145 	settings->freq_params.eht_enabled = !!os_strstr(pos, " eht");
1146 	settings->block_tx = !!os_strstr(pos, " blocktx");
1147 #undef SET_CSA_SETTING
1148 #undef SET_CSA_SETTING_EXT
1149 
1150 	return 0;
1151 }
1152 
1153 
hostapd_ctrl_iface_stop_ap(struct hostapd_data * hapd)1154 int hostapd_ctrl_iface_stop_ap(struct hostapd_data *hapd)
1155 {
1156 	return hostapd_drv_stop_ap(hapd);
1157 }
1158 
1159 
hostapd_ctrl_iface_pmksa_list(struct hostapd_data * hapd,char * buf,size_t len)1160 int hostapd_ctrl_iface_pmksa_list(struct hostapd_data *hapd, char *buf,
1161 				  size_t len)
1162 {
1163 	return wpa_auth_pmksa_list(hapd->wpa_auth, buf, len);
1164 }
1165 
1166 
hostapd_ctrl_iface_pmksa_flush(struct hostapd_data * hapd)1167 void hostapd_ctrl_iface_pmksa_flush(struct hostapd_data *hapd)
1168 {
1169 	wpa_auth_pmksa_flush(hapd->wpa_auth);
1170 }
1171 
1172 
hostapd_ctrl_iface_pmksa_add(struct hostapd_data * hapd,char * cmd)1173 int hostapd_ctrl_iface_pmksa_add(struct hostapd_data *hapd, char *cmd)
1174 {
1175 	u8 spa[ETH_ALEN];
1176 	u8 pmkid[PMKID_LEN];
1177 	u8 pmk[PMK_LEN_MAX];
1178 	size_t pmk_len;
1179 	char *pos, *pos2;
1180 	int akmp = 0, expiration = 0;
1181 
1182 	/*
1183 	 * Entry format:
1184 	 * <STA addr> <PMKID> <PMK> <expiration in seconds> <akmp>
1185 	 */
1186 
1187 	if (hwaddr_aton(cmd, spa))
1188 		return -1;
1189 
1190 	pos = os_strchr(cmd, ' ');
1191 	if (!pos)
1192 		return -1;
1193 	pos++;
1194 
1195 	if (hexstr2bin(pos, pmkid, PMKID_LEN) < 0)
1196 		return -1;
1197 
1198 	pos = os_strchr(pos, ' ');
1199 	if (!pos)
1200 		return -1;
1201 	pos++;
1202 
1203 	pos2 = os_strchr(pos, ' ');
1204 	if (!pos2)
1205 		return -1;
1206 	pmk_len = (pos2 - pos) / 2;
1207 	if (pmk_len < PMK_LEN || pmk_len > PMK_LEN_MAX ||
1208 	    hexstr2bin(pos, pmk, pmk_len) < 0)
1209 		return -1;
1210 
1211 	pos = pos2 + 1;
1212 
1213 	if (sscanf(pos, "%d %d", &expiration, &akmp) != 2)
1214 		return -1;
1215 
1216 	return wpa_auth_pmksa_add2(hapd->wpa_auth, spa, pmk, pmk_len,
1217 				   pmkid, expiration, akmp, NULL);
1218 }
1219 
1220 
1221 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
1222 #ifdef CONFIG_MESH
1223 
hostapd_ctrl_iface_pmksa_list_mesh(struct hostapd_data * hapd,const u8 * addr,char * buf,size_t len)1224 int hostapd_ctrl_iface_pmksa_list_mesh(struct hostapd_data *hapd,
1225 				       const u8 *addr, char *buf, size_t len)
1226 {
1227 	return wpa_auth_pmksa_list_mesh(hapd->wpa_auth, addr, buf, len);
1228 }
1229 
1230 
hostapd_ctrl_iface_pmksa_create_entry(const u8 * aa,char * cmd)1231 void * hostapd_ctrl_iface_pmksa_create_entry(const u8 *aa, char *cmd)
1232 {
1233 	u8 spa[ETH_ALEN];
1234 	u8 pmkid[PMKID_LEN];
1235 	u8 pmk[PMK_LEN_MAX];
1236 	char *pos;
1237 	int expiration;
1238 
1239 	/*
1240 	 * Entry format:
1241 	 * <BSSID> <PMKID> <PMK> <expiration in seconds>
1242 	 */
1243 
1244 	if (hwaddr_aton(cmd, spa))
1245 		return NULL;
1246 
1247 	pos = os_strchr(cmd, ' ');
1248 	if (!pos)
1249 		return NULL;
1250 	pos++;
1251 
1252 	if (hexstr2bin(pos, pmkid, PMKID_LEN) < 0)
1253 		return NULL;
1254 
1255 	pos = os_strchr(pos, ' ');
1256 	if (!pos)
1257 		return NULL;
1258 	pos++;
1259 
1260 	if (hexstr2bin(pos, pmk, PMK_LEN) < 0)
1261 		return NULL;
1262 
1263 	pos = os_strchr(pos, ' ');
1264 	if (!pos)
1265 		return NULL;
1266 	pos++;
1267 
1268 	if (sscanf(pos, "%d", &expiration) != 1)
1269 		return NULL;
1270 
1271 	return wpa_auth_pmksa_create_entry(aa, spa, pmk, PMK_LEN,
1272 					   WPA_KEY_MGMT_SAE, pmkid, expiration);
1273 }
1274 
1275 #endif /* CONFIG_MESH */
1276 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
1277 
1278 
1279 #ifdef CONFIG_WNM_AP
1280 
hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data * hapd,const char * cmd)1281 int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
1282 					 const char *cmd)
1283 {
1284 	u8 addr[ETH_ALEN];
1285 	int disassoc_timer;
1286 	struct sta_info *sta;
1287 
1288 	if (hwaddr_aton(cmd, addr))
1289 		return -1;
1290 	if (cmd[17] != ' ')
1291 		return -1;
1292 	disassoc_timer = atoi(cmd + 17);
1293 
1294 	sta = ap_get_sta(hapd, addr);
1295 	if (sta == NULL) {
1296 		wpa_printf(MSG_DEBUG, "Station " MACSTR_SEC
1297 			   " not found for disassociation imminent message",
1298 			   MAC2STR_SEC(addr));
1299 		return -1;
1300 	}
1301 
1302 	return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
1303 }
1304 
1305 
hostapd_ctrl_iface_ess_disassoc(struct hostapd_data * hapd,const char * cmd)1306 int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
1307 				    const char *cmd)
1308 {
1309 	u8 addr[ETH_ALEN];
1310 	const char *url, *timerstr;
1311 	int disassoc_timer;
1312 	struct sta_info *sta;
1313 
1314 	if (hwaddr_aton(cmd, addr))
1315 		return -1;
1316 
1317 	sta = ap_get_sta(hapd, addr);
1318 	if (sta == NULL) {
1319 		wpa_printf(MSG_DEBUG, "Station " MACSTR_SEC
1320 			   " not found for ESS disassociation imminent message",
1321 			   MAC2STR_SEC(addr));
1322 		return -1;
1323 	}
1324 
1325 	timerstr = cmd + 17;
1326 	if (*timerstr != ' ')
1327 		return -1;
1328 	timerstr++;
1329 	disassoc_timer = atoi(timerstr);
1330 	if (disassoc_timer < 0 || disassoc_timer > 65535)
1331 		return -1;
1332 
1333 	url = os_strchr(timerstr, ' ');
1334 	if (url == NULL)
1335 		return -1;
1336 	url++;
1337 
1338 	return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
1339 }
1340 
1341 
hostapd_ctrl_iface_bss_tm_req(struct hostapd_data * hapd,const char * cmd)1342 int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data *hapd,
1343 				  const char *cmd)
1344 {
1345 	u8 addr[ETH_ALEN];
1346 	const char *pos, *end;
1347 	int disassoc_timer = 0;
1348 	struct sta_info *sta;
1349 	u8 req_mode = 0, valid_int = 0x01, dialog_token = 0x01;
1350 	u8 bss_term_dur[12];
1351 	char *url = NULL;
1352 	int ret;
1353 	u8 nei_rep[1000];
1354 	int nei_len;
1355 	u8 mbo[10];
1356 	size_t mbo_len = 0;
1357 
1358 	if (hwaddr_aton(cmd, addr)) {
1359 		wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
1360 		return -1;
1361 	}
1362 
1363 	sta = ap_get_sta(hapd, addr);
1364 	if (sta == NULL) {
1365 		wpa_printf(MSG_DEBUG, "Station " MACSTR_SEC
1366 			   " not found for BSS TM Request message",
1367 			   MAC2STR_SEC(addr));
1368 		return -1;
1369 	}
1370 
1371 	pos = os_strstr(cmd, " disassoc_timer=");
1372 	if (pos) {
1373 		pos += 16;
1374 		disassoc_timer = atoi(pos);
1375 		if (disassoc_timer < 0 || disassoc_timer > 65535) {
1376 			wpa_printf(MSG_DEBUG, "Invalid disassoc_timer");
1377 			return -1;
1378 		}
1379 	}
1380 
1381 	pos = os_strstr(cmd, " valid_int=");
1382 	if (pos) {
1383 		pos += 11;
1384 		valid_int = atoi(pos);
1385 	}
1386 
1387 	pos = os_strstr(cmd, " dialog_token=");
1388 	if (pos) {
1389 		pos += 14;
1390 		dialog_token = atoi(pos);
1391 	}
1392 
1393 	pos = os_strstr(cmd, " bss_term=");
1394 	if (pos) {
1395 		pos += 10;
1396 		req_mode |= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED;
1397 		/* TODO: TSF configurable/learnable */
1398 		bss_term_dur[0] = 4; /* Subelement ID */
1399 		bss_term_dur[1] = 10; /* Length */
1400 		os_memset(&bss_term_dur[2], 0, 8);
1401 		end = os_strchr(pos, ',');
1402 		if (end == NULL) {
1403 			wpa_printf(MSG_DEBUG, "Invalid bss_term data");
1404 			return -1;
1405 		}
1406 		end++;
1407 		WPA_PUT_LE16(&bss_term_dur[10], atoi(end));
1408 	}
1409 
1410 	nei_len = ieee802_11_parse_candidate_list(cmd, nei_rep,
1411 						  sizeof(nei_rep));
1412 	if (nei_len < 0)
1413 		return -1;
1414 
1415 	pos = os_strstr(cmd, " url=");
1416 	if (pos) {
1417 		size_t len;
1418 		pos += 5;
1419 		end = os_strchr(pos, ' ');
1420 		if (end)
1421 			len = end - pos;
1422 		else
1423 			len = os_strlen(pos);
1424 		url = os_malloc(len + 1);
1425 		if (url == NULL)
1426 			return -1;
1427 		os_memcpy(url, pos, len);
1428 		url[len] = '\0';
1429 		req_mode |= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
1430 	}
1431 
1432 	if (os_strstr(cmd, " pref=1"))
1433 		req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
1434 	if (os_strstr(cmd, " abridged=1"))
1435 		req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
1436 	if (os_strstr(cmd, " disassoc_imminent=1"))
1437 		req_mode |= WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
1438 	if (os_strstr(cmd, " link_removal_imminent=1"))
1439 		req_mode |= WNM_BSS_TM_REQ_LINK_REMOVAL_IMMINENT;
1440 
1441 #ifdef CONFIG_MBO
1442 	pos = os_strstr(cmd, "mbo=");
1443 	if (pos) {
1444 		unsigned int mbo_reason, cell_pref, reassoc_delay;
1445 		u8 *mbo_pos = mbo;
1446 
1447 		ret = sscanf(pos, "mbo=%u:%u:%u", &mbo_reason,
1448 			     &reassoc_delay, &cell_pref);
1449 		if (ret != 3) {
1450 			wpa_printf(MSG_DEBUG,
1451 				   "MBO requires three arguments: mbo=<reason>:<reassoc_delay>:<cell_pref>");
1452 			ret = -1;
1453 			goto fail;
1454 		}
1455 
1456 		if (mbo_reason > MBO_TRANSITION_REASON_PREMIUM_AP) {
1457 			wpa_printf(MSG_DEBUG,
1458 				   "Invalid MBO transition reason code %u",
1459 				   mbo_reason);
1460 			ret = -1;
1461 			goto fail;
1462 		}
1463 
1464 		/* Valid values for Cellular preference are: 0, 1, 255 */
1465 		if (cell_pref != 0 && cell_pref != 1 && cell_pref != 255) {
1466 			wpa_printf(MSG_DEBUG,
1467 				   "Invalid MBO cellular capability %u",
1468 				   cell_pref);
1469 			ret = -1;
1470 			goto fail;
1471 		}
1472 
1473 		if (reassoc_delay > 65535 ||
1474 		    (reassoc_delay &&
1475 		     !(req_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT))) {
1476 			wpa_printf(MSG_DEBUG,
1477 				   "MBO: Assoc retry delay is only valid in disassoc imminent mode");
1478 			ret = -1;
1479 			goto fail;
1480 		}
1481 
1482 		*mbo_pos++ = MBO_ATTR_ID_TRANSITION_REASON;
1483 		*mbo_pos++ = 1;
1484 		*mbo_pos++ = mbo_reason;
1485 		*mbo_pos++ = MBO_ATTR_ID_CELL_DATA_PREF;
1486 		*mbo_pos++ = 1;
1487 		*mbo_pos++ = cell_pref;
1488 
1489 		if (reassoc_delay) {
1490 			*mbo_pos++ = MBO_ATTR_ID_ASSOC_RETRY_DELAY;
1491 			*mbo_pos++ = 2;
1492 			WPA_PUT_LE16(mbo_pos, reassoc_delay);
1493 			mbo_pos += 2;
1494 		}
1495 
1496 		mbo_len = mbo_pos - mbo;
1497 	}
1498 #endif /* CONFIG_MBO */
1499 
1500 	ret = wnm_send_bss_tm_req(hapd, sta, req_mode, disassoc_timer,
1501 				  valid_int, bss_term_dur, dialog_token, url,
1502 				  nei_len ? nei_rep : NULL, nei_len,
1503 				  mbo_len ? mbo : NULL, mbo_len);
1504 #ifdef CONFIG_MBO
1505 fail:
1506 #endif /* CONFIG_MBO */
1507 	os_free(url);
1508 	return ret;
1509 }
1510 
1511 #endif /* CONFIG_WNM_AP */
1512 
1513 
hostapd_ctrl_iface_acl_del_mac(struct mac_acl_entry ** acl,int * num,const char * txtaddr)1514 int hostapd_ctrl_iface_acl_del_mac(struct mac_acl_entry **acl, int *num,
1515 				   const char *txtaddr)
1516 {
1517 	u8 addr[ETH_ALEN];
1518 	struct vlan_description vlan_id;
1519 
1520 	if (!(*num))
1521 		return 0;
1522 
1523 	if (hwaddr_aton(txtaddr, addr))
1524 		return -1;
1525 
1526 	if (hostapd_maclist_found(*acl, *num, addr, &vlan_id))
1527 		hostapd_remove_acl_mac(acl, num, addr);
1528 
1529 	return 0;
1530 }
1531 
1532 
hostapd_ctrl_iface_acl_clear_list(struct mac_acl_entry ** acl,int * num)1533 void hostapd_ctrl_iface_acl_clear_list(struct mac_acl_entry **acl,
1534 				       int *num)
1535 {
1536 	while (*num)
1537 		hostapd_remove_acl_mac(acl, num, (*acl)[0].addr);
1538 }
1539 
1540 
hostapd_ctrl_iface_acl_show_mac(struct mac_acl_entry * acl,int num,char * buf,size_t buflen)1541 int hostapd_ctrl_iface_acl_show_mac(struct mac_acl_entry *acl, int num,
1542 				    char *buf, size_t buflen)
1543 {
1544 	int i = 0, len = 0, ret = 0;
1545 
1546 	if (!acl)
1547 		return 0;
1548 
1549 	while (i < num) {
1550 		ret = os_snprintf(buf + len, buflen - len,
1551 				  MACSTR " VLAN_ID=%d\n",
1552 				  MAC2STR(acl[i].addr),
1553 				  acl[i].vlan_id.untagged);
1554 		if (ret < 0 || (size_t) ret >= buflen - len)
1555 			return len;
1556 		i++;
1557 		len += ret;
1558 	}
1559 	return len;
1560 }
1561 
1562 
hostapd_ctrl_iface_acl_add_mac(struct mac_acl_entry ** acl,int * num,const char * cmd)1563 int hostapd_ctrl_iface_acl_add_mac(struct mac_acl_entry **acl, int *num,
1564 				   const char *cmd)
1565 {
1566 	u8 addr[ETH_ALEN];
1567 	struct vlan_description vlan_id;
1568 	int ret = 0, vlanid = 0;
1569 	const char *pos;
1570 
1571 	if (hwaddr_aton(cmd, addr))
1572 		return -1;
1573 
1574 	pos = os_strstr(cmd, "VLAN_ID=");
1575 	if (pos)
1576 		vlanid = atoi(pos + 8);
1577 
1578 	if (!hostapd_maclist_found(*acl, *num, addr, &vlan_id)) {
1579 		ret = hostapd_add_acl_maclist(acl, num, vlanid, addr);
1580 		if (ret != -1 && *acl)
1581 			qsort(*acl, *num, sizeof(**acl), hostapd_acl_comp);
1582 	}
1583 
1584 	return ret < 0 ? -1 : 0;
1585 }
1586 
1587 
hostapd_disassoc_accept_mac(struct hostapd_data * hapd)1588 int hostapd_disassoc_accept_mac(struct hostapd_data *hapd)
1589 {
1590 	struct sta_info *sta;
1591 	struct vlan_description vlan_id;
1592 
1593 	if (hapd->conf->macaddr_acl != DENY_UNLESS_ACCEPTED)
1594 		return 0;
1595 
1596 	for (sta = hapd->sta_list; sta; sta = sta->next) {
1597 		if (!hostapd_maclist_found(hapd->conf->accept_mac,
1598 					   hapd->conf->num_accept_mac,
1599 					   sta->addr, &vlan_id) ||
1600 		    (vlan_id.notempty &&
1601 		     vlan_compare(&vlan_id, sta->vlan_desc)))
1602 			ap_sta_disconnect(hapd, sta, sta->addr,
1603 					  WLAN_REASON_UNSPECIFIED);
1604 	}
1605 
1606 	return 0;
1607 }
1608 
1609 
hostapd_disassoc_deny_mac(struct hostapd_data * hapd)1610 int hostapd_disassoc_deny_mac(struct hostapd_data *hapd)
1611 {
1612 	struct sta_info *sta;
1613 	struct vlan_description vlan_id;
1614 
1615 	for (sta = hapd->sta_list; sta; sta = sta->next) {
1616 		if (hostapd_maclist_found(hapd->conf->deny_mac,
1617 					  hapd->conf->num_deny_mac, sta->addr,
1618 					  &vlan_id) &&
1619 		    (!vlan_id.notempty ||
1620 		     !vlan_compare(&vlan_id, sta->vlan_desc)))
1621 			ap_sta_disconnect(hapd, sta, sta->addr,
1622 					  WLAN_REASON_UNSPECIFIED);
1623 	}
1624 
1625 	return 0;
1626 }
1627