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