Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * hostapd / UNIX domain socket -based control interface |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3 | * Copyright (c) 2004-2018, Jouni Malinen <j@w1.fi> |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4 | * |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5 | * This software may be distributed under the terms of the BSD license. |
| 6 | * See README for more details. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "utils/includes.h" |
| 10 | |
| 11 | #ifndef CONFIG_NATIVE_WINDOWS |
| 12 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 13 | #ifdef CONFIG_TESTING_OPTIONS |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 14 | #ifdef __NetBSD__ |
| 15 | #include <net/if_ether.h> |
| 16 | #else |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 17 | #include <net/ethernet.h> |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 18 | #endif |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 19 | #include <netinet/ip.h> |
| 20 | #endif /* CONFIG_TESTING_OPTIONS */ |
| 21 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 22 | #include <sys/un.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <stddef.h> |
| 25 | |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 26 | #ifdef CONFIG_CTRL_IFACE_UDP |
| 27 | #include <netdb.h> |
| 28 | #endif /* CONFIG_CTRL_IFACE_UDP */ |
| 29 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 30 | #include "utils/common.h" |
| 31 | #include "utils/eloop.h" |
Dmitry Shmidt | 4ae50e6 | 2016-06-27 13:48:39 -0700 | [diff] [blame] | 32 | #include "utils/module_tests.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 33 | #include "common/version.h" |
| 34 | #include "common/ieee802_11_defs.h" |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 35 | #include "common/ctrl_iface_common.h" |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 36 | #ifdef CONFIG_DPP |
| 37 | #include "common/dpp.h" |
| 38 | #endif /* CONFIG_DPP */ |
| 39 | #include "common/wpa_ctrl.h" |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 40 | #include "common/ptksa_cache.h" |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 41 | #include "crypto/tls.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 42 | #include "drivers/driver.h" |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 43 | #include "eapol_auth/eapol_auth_sm.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 44 | #include "radius/radius_client.h" |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 45 | #include "radius/radius_server.h" |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 46 | #include "l2_packet/l2_packet.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 47 | #include "ap/hostapd.h" |
| 48 | #include "ap/ap_config.h" |
| 49 | #include "ap/ieee802_1x.h" |
| 50 | #include "ap/wpa_auth.h" |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 51 | #include "ap/pmksa_cache_auth.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 52 | #include "ap/ieee802_11.h" |
| 53 | #include "ap/sta_info.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 54 | #include "ap/wps_hostapd.h" |
| 55 | #include "ap/ctrl_iface_ap.h" |
| 56 | #include "ap/ap_drv_ops.h" |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 57 | #include "ap/hs20.h" |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 58 | #include "ap/wnm_ap.h" |
Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 59 | #include "ap/wpa_auth.h" |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 60 | #include "ap/beacon.h" |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 61 | #include "ap/neighbor_db.h" |
| 62 | #include "ap/rrm.h" |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 63 | #include "ap/dpp_hostapd.h" |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 64 | #include "ap/dfs.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 65 | #include "wps/wps_defs.h" |
| 66 | #include "wps/wps.h" |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 67 | #include "fst/fst_ctrl_iface.h" |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 68 | #include "config_file.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 69 | #include "ctrl_iface.h" |
| 70 | |
| 71 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 72 | #define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256 |
| 73 | |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 74 | #ifdef CONFIG_CTRL_IFACE_UDP |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 75 | #define HOSTAPD_CTRL_IFACE_PORT 8877 |
| 76 | #define HOSTAPD_CTRL_IFACE_PORT_LIMIT 50 |
| 77 | #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT 8878 |
| 78 | #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT 50 |
| 79 | #endif /* CONFIG_CTRL_IFACE_UDP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 80 | |
| 81 | static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level, |
Anton Nayshtut | f715e8d | 2014-11-16 16:52:49 +0200 | [diff] [blame] | 82 | enum wpa_msg_type type, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 83 | const char *buf, size_t len); |
| 84 | |
| 85 | |
| 86 | static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd, |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 87 | struct sockaddr_storage *from, |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 88 | socklen_t fromlen, const char *input) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 89 | { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 90 | return ctrl_iface_attach(&hapd->ctrl_dst, from, fromlen, input); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | |
| 94 | static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd, |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 95 | struct sockaddr_storage *from, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 96 | socklen_t fromlen) |
| 97 | { |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 98 | return ctrl_iface_detach(&hapd->ctrl_dst, from, fromlen); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | |
| 102 | static int hostapd_ctrl_iface_level(struct hostapd_data *hapd, |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 103 | struct sockaddr_storage *from, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 104 | socklen_t fromlen, |
| 105 | char *level) |
| 106 | { |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 107 | return ctrl_iface_level(&hapd->ctrl_dst, from, fromlen, level); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | |
| 111 | static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd, |
| 112 | const char *txtaddr) |
| 113 | { |
| 114 | u8 addr[ETH_ALEN]; |
| 115 | struct sta_info *sta; |
| 116 | |
| 117 | wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr); |
| 118 | |
| 119 | if (hwaddr_aton(txtaddr, addr)) |
| 120 | return -1; |
| 121 | |
| 122 | sta = ap_get_sta(hapd, addr); |
| 123 | if (sta) |
| 124 | return 0; |
| 125 | |
| 126 | wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface " |
| 127 | "notification", MAC2STR(addr)); |
| 128 | sta = ap_sta_add(hapd, addr); |
| 129 | if (sta == NULL) |
| 130 | return -1; |
| 131 | |
| 132 | hostapd_new_assoc_sta(hapd, sta, 0); |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 137 | #ifdef NEED_AP_MLME |
| 138 | static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd, |
| 139 | const char *txtaddr) |
| 140 | { |
| 141 | u8 addr[ETH_ALEN]; |
| 142 | u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN]; |
| 143 | |
| 144 | wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr); |
| 145 | |
| 146 | if (hwaddr_aton(txtaddr, addr) || |
| 147 | os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) |
| 148 | return -1; |
| 149 | |
| 150 | ieee802_11_send_sa_query_req(hapd, addr, trans_id); |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | #endif /* NEED_AP_MLME */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 155 | |
| 156 | |
| 157 | #ifdef CONFIG_WPS |
| 158 | static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt) |
| 159 | { |
| 160 | char *pin = os_strchr(txt, ' '); |
| 161 | char *timeout_txt; |
| 162 | int timeout; |
| 163 | u8 addr_buf[ETH_ALEN], *addr = NULL; |
| 164 | char *pos; |
| 165 | |
| 166 | if (pin == NULL) |
| 167 | return -1; |
| 168 | *pin++ = '\0'; |
| 169 | |
| 170 | timeout_txt = os_strchr(pin, ' '); |
| 171 | if (timeout_txt) { |
| 172 | *timeout_txt++ = '\0'; |
| 173 | timeout = atoi(timeout_txt); |
| 174 | pos = os_strchr(timeout_txt, ' '); |
| 175 | if (pos) { |
| 176 | *pos++ = '\0'; |
| 177 | if (hwaddr_aton(pos, addr_buf) == 0) |
| 178 | addr = addr_buf; |
| 179 | } |
| 180 | } else |
| 181 | timeout = 0; |
| 182 | |
| 183 | return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout); |
| 184 | } |
| 185 | |
| 186 | |
| 187 | static int hostapd_ctrl_iface_wps_check_pin( |
| 188 | struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen) |
| 189 | { |
| 190 | char pin[9]; |
| 191 | size_t len; |
| 192 | char *pos; |
| 193 | int ret; |
| 194 | |
| 195 | wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN", |
| 196 | (u8 *) cmd, os_strlen(cmd)); |
| 197 | for (pos = cmd, len = 0; *pos != '\0'; pos++) { |
| 198 | if (*pos < '0' || *pos > '9') |
| 199 | continue; |
| 200 | pin[len++] = *pos; |
| 201 | if (len == 9) { |
| 202 | wpa_printf(MSG_DEBUG, "WPS: Too long PIN"); |
| 203 | return -1; |
| 204 | } |
| 205 | } |
| 206 | if (len != 4 && len != 8) { |
| 207 | wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len); |
| 208 | return -1; |
| 209 | } |
| 210 | pin[len] = '\0'; |
| 211 | |
| 212 | if (len == 8) { |
| 213 | unsigned int pin_val; |
| 214 | pin_val = atoi(pin); |
| 215 | if (!wps_pin_valid(pin_val)) { |
| 216 | wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit"); |
| 217 | ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n"); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 218 | if (os_snprintf_error(buflen, ret)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 219 | return -1; |
| 220 | return ret; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | ret = os_snprintf(buf, buflen, "%s", pin); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 225 | if (os_snprintf_error(buflen, ret)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 226 | return -1; |
| 227 | |
| 228 | return ret; |
| 229 | } |
| 230 | |
| 231 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 232 | #ifdef CONFIG_WPS_NFC |
| 233 | static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd, |
| 234 | char *pos) |
| 235 | { |
| 236 | size_t len; |
| 237 | struct wpabuf *buf; |
| 238 | int ret; |
| 239 | |
| 240 | len = os_strlen(pos); |
| 241 | if (len & 0x01) |
| 242 | return -1; |
| 243 | len /= 2; |
| 244 | |
| 245 | buf = wpabuf_alloc(len); |
| 246 | if (buf == NULL) |
| 247 | return -1; |
| 248 | if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) { |
| 249 | wpabuf_free(buf); |
| 250 | return -1; |
| 251 | } |
| 252 | |
| 253 | ret = hostapd_wps_nfc_tag_read(hapd, buf); |
| 254 | wpabuf_free(buf); |
| 255 | |
| 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | |
| 260 | static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd, |
| 261 | char *cmd, char *reply, |
| 262 | size_t max_len) |
| 263 | { |
| 264 | int ndef; |
| 265 | struct wpabuf *buf; |
| 266 | int res; |
| 267 | |
| 268 | if (os_strcmp(cmd, "WPS") == 0) |
| 269 | ndef = 0; |
| 270 | else if (os_strcmp(cmd, "NDEF") == 0) |
| 271 | ndef = 1; |
| 272 | else |
| 273 | return -1; |
| 274 | |
| 275 | buf = hostapd_wps_nfc_config_token(hapd, ndef); |
| 276 | if (buf == NULL) |
| 277 | return -1; |
| 278 | |
| 279 | res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf), |
| 280 | wpabuf_len(buf)); |
| 281 | reply[res++] = '\n'; |
| 282 | reply[res] = '\0'; |
| 283 | |
| 284 | wpabuf_free(buf); |
| 285 | |
| 286 | return res; |
| 287 | } |
| 288 | |
| 289 | |
| 290 | static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd, |
| 291 | char *reply, size_t max_len, |
| 292 | int ndef) |
| 293 | { |
| 294 | struct wpabuf *buf; |
| 295 | int res; |
| 296 | |
| 297 | buf = hostapd_wps_nfc_token_gen(hapd, ndef); |
| 298 | if (buf == NULL) |
| 299 | return -1; |
| 300 | |
| 301 | res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf), |
| 302 | wpabuf_len(buf)); |
| 303 | reply[res++] = '\n'; |
| 304 | reply[res] = '\0'; |
| 305 | |
| 306 | wpabuf_free(buf); |
| 307 | |
| 308 | return res; |
| 309 | } |
| 310 | |
| 311 | |
| 312 | static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd, |
| 313 | char *cmd, char *reply, |
| 314 | size_t max_len) |
| 315 | { |
| 316 | if (os_strcmp(cmd, "WPS") == 0) |
| 317 | return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply, |
| 318 | max_len, 0); |
| 319 | |
| 320 | if (os_strcmp(cmd, "NDEF") == 0) |
| 321 | return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply, |
| 322 | max_len, 1); |
| 323 | |
| 324 | if (os_strcmp(cmd, "enable") == 0) |
| 325 | return hostapd_wps_nfc_token_enable(hapd); |
| 326 | |
| 327 | if (os_strcmp(cmd, "disable") == 0) { |
| 328 | hostapd_wps_nfc_token_disable(hapd); |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | return -1; |
| 333 | } |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 334 | |
| 335 | |
| 336 | static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd, |
| 337 | char *cmd, char *reply, |
| 338 | size_t max_len) |
| 339 | { |
| 340 | struct wpabuf *buf; |
| 341 | int res; |
| 342 | char *pos; |
| 343 | int ndef; |
| 344 | |
| 345 | pos = os_strchr(cmd, ' '); |
| 346 | if (pos == NULL) |
| 347 | return -1; |
| 348 | *pos++ = '\0'; |
| 349 | |
| 350 | if (os_strcmp(cmd, "WPS") == 0) |
| 351 | ndef = 0; |
| 352 | else if (os_strcmp(cmd, "NDEF") == 0) |
| 353 | ndef = 1; |
| 354 | else |
| 355 | return -1; |
| 356 | |
| 357 | if (os_strcmp(pos, "WPS-CR") == 0) |
| 358 | buf = hostapd_wps_nfc_hs_cr(hapd, ndef); |
| 359 | else |
| 360 | buf = NULL; |
| 361 | if (buf == NULL) |
| 362 | return -1; |
| 363 | |
| 364 | res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf), |
| 365 | wpabuf_len(buf)); |
| 366 | reply[res++] = '\n'; |
| 367 | reply[res] = '\0'; |
| 368 | |
| 369 | wpabuf_free(buf); |
| 370 | |
| 371 | return res; |
| 372 | } |
| 373 | |
| 374 | |
| 375 | static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd, |
| 376 | char *cmd) |
| 377 | { |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 378 | size_t len; |
| 379 | struct wpabuf *req, *sel; |
| 380 | int ret; |
| 381 | char *pos, *role, *type, *pos2; |
| 382 | |
| 383 | role = cmd; |
| 384 | pos = os_strchr(role, ' '); |
| 385 | if (pos == NULL) |
| 386 | return -1; |
| 387 | *pos++ = '\0'; |
| 388 | |
| 389 | type = pos; |
| 390 | pos = os_strchr(type, ' '); |
| 391 | if (pos == NULL) |
| 392 | return -1; |
| 393 | *pos++ = '\0'; |
| 394 | |
| 395 | pos2 = os_strchr(pos, ' '); |
| 396 | if (pos2 == NULL) |
| 397 | return -1; |
| 398 | *pos2++ = '\0'; |
| 399 | |
| 400 | len = os_strlen(pos); |
| 401 | if (len & 0x01) |
| 402 | return -1; |
| 403 | len /= 2; |
| 404 | |
| 405 | req = wpabuf_alloc(len); |
| 406 | if (req == NULL) |
| 407 | return -1; |
| 408 | if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) { |
| 409 | wpabuf_free(req); |
| 410 | return -1; |
| 411 | } |
| 412 | |
| 413 | len = os_strlen(pos2); |
| 414 | if (len & 0x01) { |
| 415 | wpabuf_free(req); |
| 416 | return -1; |
| 417 | } |
| 418 | len /= 2; |
| 419 | |
| 420 | sel = wpabuf_alloc(len); |
| 421 | if (sel == NULL) { |
| 422 | wpabuf_free(req); |
| 423 | return -1; |
| 424 | } |
| 425 | if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) { |
| 426 | wpabuf_free(req); |
| 427 | wpabuf_free(sel); |
| 428 | return -1; |
| 429 | } |
| 430 | |
| 431 | if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) { |
| 432 | ret = hostapd_wps_nfc_report_handover(hapd, req, sel); |
| 433 | } else { |
| 434 | wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover " |
| 435 | "reported: role=%s type=%s", role, type); |
| 436 | ret = -1; |
| 437 | } |
| 438 | wpabuf_free(req); |
| 439 | wpabuf_free(sel); |
| 440 | |
| 441 | return ret; |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 442 | } |
| 443 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 444 | #endif /* CONFIG_WPS_NFC */ |
| 445 | |
| 446 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 447 | static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt, |
| 448 | char *buf, size_t buflen) |
| 449 | { |
| 450 | int timeout = 300; |
| 451 | char *pos; |
| 452 | const char *pin_txt; |
| 453 | |
| 454 | pos = os_strchr(txt, ' '); |
| 455 | if (pos) |
| 456 | *pos++ = '\0'; |
| 457 | |
| 458 | if (os_strcmp(txt, "disable") == 0) { |
| 459 | hostapd_wps_ap_pin_disable(hapd); |
| 460 | return os_snprintf(buf, buflen, "OK\n"); |
| 461 | } |
| 462 | |
| 463 | if (os_strcmp(txt, "random") == 0) { |
| 464 | if (pos) |
| 465 | timeout = atoi(pos); |
| 466 | pin_txt = hostapd_wps_ap_pin_random(hapd, timeout); |
| 467 | if (pin_txt == NULL) |
| 468 | return -1; |
| 469 | return os_snprintf(buf, buflen, "%s", pin_txt); |
| 470 | } |
| 471 | |
| 472 | if (os_strcmp(txt, "get") == 0) { |
| 473 | pin_txt = hostapd_wps_ap_pin_get(hapd); |
| 474 | if (pin_txt == NULL) |
| 475 | return -1; |
| 476 | return os_snprintf(buf, buflen, "%s", pin_txt); |
| 477 | } |
| 478 | |
| 479 | if (os_strcmp(txt, "set") == 0) { |
| 480 | char *pin; |
| 481 | if (pos == NULL) |
| 482 | return -1; |
| 483 | pin = pos; |
| 484 | pos = os_strchr(pos, ' '); |
| 485 | if (pos) { |
| 486 | *pos++ = '\0'; |
| 487 | timeout = atoi(pos); |
| 488 | } |
| 489 | if (os_strlen(pin) > buflen) |
| 490 | return -1; |
| 491 | if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0) |
| 492 | return -1; |
| 493 | return os_snprintf(buf, buflen, "%s", pin); |
| 494 | } |
| 495 | |
| 496 | return -1; |
| 497 | } |
| 498 | |
| 499 | |
| 500 | static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt) |
| 501 | { |
| 502 | char *pos; |
| 503 | char *ssid, *auth, *encr = NULL, *key = NULL; |
| 504 | |
| 505 | ssid = txt; |
| 506 | pos = os_strchr(txt, ' '); |
| 507 | if (!pos) |
| 508 | return -1; |
| 509 | *pos++ = '\0'; |
| 510 | |
| 511 | auth = pos; |
| 512 | pos = os_strchr(pos, ' '); |
| 513 | if (pos) { |
| 514 | *pos++ = '\0'; |
| 515 | encr = pos; |
| 516 | pos = os_strchr(pos, ' '); |
| 517 | if (pos) { |
| 518 | *pos++ = '\0'; |
| 519 | key = pos; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | return hostapd_wps_config_ap(hapd, ssid, auth, encr, key); |
| 524 | } |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 525 | |
| 526 | |
| 527 | static const char * pbc_status_str(enum pbc_status status) |
| 528 | { |
| 529 | switch (status) { |
| 530 | case WPS_PBC_STATUS_DISABLE: |
| 531 | return "Disabled"; |
| 532 | case WPS_PBC_STATUS_ACTIVE: |
| 533 | return "Active"; |
| 534 | case WPS_PBC_STATUS_TIMEOUT: |
| 535 | return "Timed-out"; |
| 536 | case WPS_PBC_STATUS_OVERLAP: |
| 537 | return "Overlap"; |
| 538 | default: |
| 539 | return "Unknown"; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | |
| 544 | static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd, |
| 545 | char *buf, size_t buflen) |
| 546 | { |
| 547 | int ret; |
| 548 | char *pos, *end; |
| 549 | |
| 550 | pos = buf; |
| 551 | end = buf + buflen; |
| 552 | |
| 553 | ret = os_snprintf(pos, end - pos, "PBC Status: %s\n", |
| 554 | pbc_status_str(hapd->wps_stats.pbc_status)); |
| 555 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 556 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 557 | return pos - buf; |
| 558 | pos += ret; |
| 559 | |
| 560 | ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n", |
| 561 | (hapd->wps_stats.status == WPS_STATUS_SUCCESS ? |
| 562 | "Success": |
| 563 | (hapd->wps_stats.status == WPS_STATUS_FAILURE ? |
| 564 | "Failed" : "None"))); |
| 565 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 566 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 567 | return pos - buf; |
| 568 | pos += ret; |
| 569 | |
| 570 | /* If status == Failure - Add possible Reasons */ |
| 571 | if(hapd->wps_stats.status == WPS_STATUS_FAILURE && |
| 572 | hapd->wps_stats.failure_reason > 0) { |
| 573 | ret = os_snprintf(pos, end - pos, |
| 574 | "Failure Reason: %s\n", |
| 575 | wps_ei_str(hapd->wps_stats.failure_reason)); |
| 576 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 577 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 578 | return pos - buf; |
| 579 | pos += ret; |
| 580 | } |
| 581 | |
| 582 | if (hapd->wps_stats.status) { |
| 583 | ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n", |
| 584 | MAC2STR(hapd->wps_stats.peer_addr)); |
| 585 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 586 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 587 | return pos - buf; |
| 588 | pos += ret; |
| 589 | } |
| 590 | |
| 591 | return pos - buf; |
| 592 | } |
| 593 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 594 | #endif /* CONFIG_WPS */ |
| 595 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 596 | #ifdef CONFIG_HS20 |
| 597 | |
| 598 | static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd, |
| 599 | const char *cmd) |
| 600 | { |
| 601 | u8 addr[ETH_ALEN]; |
| 602 | const char *url; |
| 603 | |
| 604 | if (hwaddr_aton(cmd, addr)) |
| 605 | return -1; |
| 606 | url = cmd + 17; |
| 607 | if (*url == '\0') { |
| 608 | url = NULL; |
| 609 | } else { |
| 610 | if (*url != ' ') |
| 611 | return -1; |
| 612 | url++; |
| 613 | if (*url == '\0') |
| 614 | url = NULL; |
| 615 | } |
| 616 | |
| 617 | return hs20_send_wnm_notification(hapd, addr, 1, url); |
| 618 | } |
| 619 | |
| 620 | |
| 621 | static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd, |
| 622 | const char *cmd) |
| 623 | { |
| 624 | u8 addr[ETH_ALEN]; |
| 625 | int code, reauth_delay, ret; |
| 626 | const char *pos; |
| 627 | size_t url_len; |
| 628 | struct wpabuf *req; |
| 629 | |
| 630 | /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */ |
| 631 | if (hwaddr_aton(cmd, addr)) |
| 632 | return -1; |
| 633 | |
| 634 | pos = os_strchr(cmd, ' '); |
| 635 | if (pos == NULL) |
| 636 | return -1; |
| 637 | pos++; |
| 638 | code = atoi(pos); |
| 639 | |
| 640 | pos = os_strchr(pos, ' '); |
| 641 | if (pos == NULL) |
| 642 | return -1; |
| 643 | pos++; |
| 644 | reauth_delay = atoi(pos); |
| 645 | |
| 646 | url_len = 0; |
| 647 | pos = os_strchr(pos, ' '); |
| 648 | if (pos) { |
| 649 | pos++; |
| 650 | url_len = os_strlen(pos); |
| 651 | } |
| 652 | |
| 653 | req = wpabuf_alloc(4 + url_len); |
| 654 | if (req == NULL) |
| 655 | return -1; |
| 656 | wpabuf_put_u8(req, code); |
| 657 | wpabuf_put_le16(req, reauth_delay); |
| 658 | wpabuf_put_u8(req, url_len); |
| 659 | if (pos) |
| 660 | wpabuf_put_data(req, pos, url_len); |
| 661 | |
| 662 | wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR |
| 663 | " to indicate imminent deauthentication (code=%d " |
| 664 | "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay); |
| 665 | ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req); |
| 666 | wpabuf_free(req); |
| 667 | return ret; |
| 668 | } |
| 669 | |
| 670 | #endif /* CONFIG_HS20 */ |
| 671 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 672 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 673 | #ifdef CONFIG_INTERWORKING |
| 674 | |
| 675 | static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd, |
| 676 | const char *cmd) |
| 677 | { |
| 678 | u8 qos_map_set[16 + 2 * 21], count = 0; |
| 679 | const char *pos = cmd; |
| 680 | int val, ret; |
| 681 | |
| 682 | for (;;) { |
| 683 | if (count == sizeof(qos_map_set)) { |
| 684 | wpa_printf(MSG_ERROR, "Too many qos_map_set parameters"); |
| 685 | return -1; |
| 686 | } |
| 687 | |
| 688 | val = atoi(pos); |
| 689 | if (val < 0 || val > 255) { |
| 690 | wpa_printf(MSG_INFO, "Invalid QoS Map Set"); |
| 691 | return -1; |
| 692 | } |
| 693 | |
| 694 | qos_map_set[count++] = val; |
| 695 | pos = os_strchr(pos, ','); |
| 696 | if (!pos) |
| 697 | break; |
| 698 | pos++; |
| 699 | } |
| 700 | |
| 701 | if (count < 16 || count & 1) { |
| 702 | wpa_printf(MSG_INFO, "Invalid QoS Map Set"); |
| 703 | return -1; |
| 704 | } |
| 705 | |
| 706 | ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count); |
| 707 | if (ret) { |
| 708 | wpa_printf(MSG_INFO, "Failed to set QoS Map Set"); |
| 709 | return -1; |
| 710 | } |
| 711 | |
| 712 | os_memcpy(hapd->conf->qos_map_set, qos_map_set, count); |
| 713 | hapd->conf->qos_map_set_len = count; |
| 714 | |
| 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | |
| 719 | static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd, |
| 720 | const char *cmd) |
| 721 | { |
| 722 | u8 addr[ETH_ALEN]; |
| 723 | struct sta_info *sta; |
| 724 | struct wpabuf *buf; |
| 725 | u8 *qos_map_set = hapd->conf->qos_map_set; |
| 726 | u8 qos_map_set_len = hapd->conf->qos_map_set_len; |
| 727 | int ret; |
| 728 | |
| 729 | if (!qos_map_set_len) { |
| 730 | wpa_printf(MSG_INFO, "QoS Map Set is not set"); |
| 731 | return -1; |
| 732 | } |
| 733 | |
| 734 | if (hwaddr_aton(cmd, addr)) |
| 735 | return -1; |
| 736 | |
| 737 | sta = ap_get_sta(hapd, addr); |
| 738 | if (sta == NULL) { |
| 739 | wpa_printf(MSG_DEBUG, "Station " MACSTR " not found " |
| 740 | "for QoS Map Configuration message", |
| 741 | MAC2STR(addr)); |
| 742 | return -1; |
| 743 | } |
| 744 | |
| 745 | if (!sta->qos_map_enabled) { |
| 746 | wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate " |
| 747 | "support for QoS Map", MAC2STR(addr)); |
| 748 | return -1; |
| 749 | } |
| 750 | |
| 751 | buf = wpabuf_alloc(2 + 2 + qos_map_set_len); |
| 752 | if (buf == NULL) |
| 753 | return -1; |
| 754 | |
| 755 | wpabuf_put_u8(buf, WLAN_ACTION_QOS); |
| 756 | wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG); |
| 757 | |
| 758 | /* QoS Map Set Element */ |
| 759 | wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET); |
| 760 | wpabuf_put_u8(buf, qos_map_set_len); |
| 761 | wpabuf_put_data(buf, qos_map_set, qos_map_set_len); |
| 762 | |
| 763 | ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr, |
| 764 | wpabuf_head(buf), wpabuf_len(buf)); |
| 765 | wpabuf_free(buf); |
| 766 | |
| 767 | return ret; |
| 768 | } |
| 769 | |
| 770 | #endif /* CONFIG_INTERWORKING */ |
| 771 | |
| 772 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 773 | #ifdef CONFIG_WNM_AP |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 774 | |
Hai Shalom | 39ba6fc | 2019-01-22 12:40:38 -0800 | [diff] [blame] | 775 | static int hostapd_ctrl_iface_coloc_intf_req(struct hostapd_data *hapd, |
| 776 | const char *cmd) |
| 777 | { |
| 778 | u8 addr[ETH_ALEN]; |
| 779 | struct sta_info *sta; |
| 780 | const char *pos; |
| 781 | unsigned int auto_report, timeout; |
| 782 | |
| 783 | if (hwaddr_aton(cmd, addr)) { |
| 784 | wpa_printf(MSG_DEBUG, "Invalid STA MAC address"); |
| 785 | return -1; |
| 786 | } |
| 787 | |
| 788 | sta = ap_get_sta(hapd, addr); |
| 789 | if (!sta) { |
| 790 | wpa_printf(MSG_DEBUG, "Station " MACSTR |
| 791 | " not found for Collocated Interference Request", |
| 792 | MAC2STR(addr)); |
| 793 | return -1; |
| 794 | } |
| 795 | |
| 796 | pos = cmd + 17; |
| 797 | if (*pos != ' ') |
| 798 | return -1; |
| 799 | pos++; |
| 800 | auto_report = atoi(pos); |
| 801 | pos = os_strchr(pos, ' '); |
| 802 | if (!pos) |
| 803 | return -1; |
| 804 | pos++; |
| 805 | timeout = atoi(pos); |
| 806 | |
| 807 | return wnm_send_coloc_intf_req(hapd, sta, auto_report, timeout); |
| 808 | } |
| 809 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 810 | #endif /* CONFIG_WNM_AP */ |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 811 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 812 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 813 | static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data *hapd, |
| 814 | char *buf, size_t buflen) |
| 815 | { |
| 816 | int ret = 0; |
| 817 | char *pos, *end; |
| 818 | |
| 819 | pos = buf; |
| 820 | end = buf + buflen; |
| 821 | |
| 822 | WPA_ASSERT(hapd->conf->wpa_key_mgmt); |
| 823 | |
| 824 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) { |
| 825 | ret = os_snprintf(pos, end - pos, "WPA-PSK "); |
| 826 | if (os_snprintf_error(end - pos, ret)) |
| 827 | return pos - buf; |
| 828 | pos += ret; |
| 829 | } |
| 830 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) { |
| 831 | ret = os_snprintf(pos, end - pos, "WPA-EAP "); |
| 832 | if (os_snprintf_error(end - pos, ret)) |
| 833 | return pos - buf; |
| 834 | pos += ret; |
| 835 | } |
Dmitry Shmidt | abb90a3 | 2016-12-05 15:34:39 -0800 | [diff] [blame] | 836 | #ifdef CONFIG_IEEE80211R_AP |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 837 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) { |
| 838 | ret = os_snprintf(pos, end - pos, "FT-PSK "); |
| 839 | if (os_snprintf_error(end - pos, ret)) |
| 840 | return pos - buf; |
| 841 | pos += ret; |
| 842 | } |
| 843 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) { |
| 844 | ret = os_snprintf(pos, end - pos, "FT-EAP "); |
| 845 | if (os_snprintf_error(end - pos, ret)) |
| 846 | return pos - buf; |
| 847 | pos += ret; |
| 848 | } |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 849 | #ifdef CONFIG_SHA384 |
| 850 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) { |
| 851 | ret = os_snprintf(pos, end - pos, "FT-EAP-SHA384 "); |
| 852 | if (os_snprintf_error(end - pos, ret)) |
| 853 | return pos - buf; |
| 854 | pos += ret; |
| 855 | } |
| 856 | #endif /* CONFIG_SHA384 */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 857 | #ifdef CONFIG_SAE |
| 858 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) { |
| 859 | ret = os_snprintf(pos, end - pos, "FT-SAE "); |
| 860 | if (os_snprintf_error(end - pos, ret)) |
| 861 | return pos - buf; |
| 862 | pos += ret; |
| 863 | } |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame^] | 864 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE_EXT_KEY) { |
| 865 | ret = os_snprintf(pos, end - pos, "FT-SAE-EXT-KEY "); |
| 866 | if (os_snprintf_error(end - pos, ret)) |
| 867 | return pos - buf; |
| 868 | pos += ret; |
| 869 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 870 | #endif /* CONFIG_SAE */ |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 871 | #ifdef CONFIG_FILS |
| 872 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) { |
| 873 | ret = os_snprintf(pos, end - pos, "FT-FILS-SHA256 "); |
| 874 | if (os_snprintf_error(end - pos, ret)) |
| 875 | return pos - buf; |
| 876 | pos += ret; |
| 877 | } |
| 878 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) { |
| 879 | ret = os_snprintf(pos, end - pos, "FT-FILS-SHA384 "); |
| 880 | if (os_snprintf_error(end - pos, ret)) |
| 881 | return pos - buf; |
| 882 | pos += ret; |
| 883 | } |
| 884 | #endif /* CONFIG_FILS */ |
Dmitry Shmidt | abb90a3 | 2016-12-05 15:34:39 -0800 | [diff] [blame] | 885 | #endif /* CONFIG_IEEE80211R_AP */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 886 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) { |
| 887 | ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 "); |
| 888 | if (os_snprintf_error(end - pos, ret)) |
| 889 | return pos - buf; |
| 890 | pos += ret; |
| 891 | } |
| 892 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) { |
| 893 | ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 "); |
| 894 | if (os_snprintf_error(end - pos, ret)) |
| 895 | return pos - buf; |
| 896 | pos += ret; |
| 897 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 898 | #ifdef CONFIG_SAE |
| 899 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) { |
| 900 | ret = os_snprintf(pos, end - pos, "SAE "); |
| 901 | if (os_snprintf_error(end - pos, ret)) |
| 902 | return pos - buf; |
| 903 | pos += ret; |
| 904 | } |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame^] | 905 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) { |
| 906 | ret = os_snprintf(pos, end - pos, "SAE-EXT-KEY "); |
| 907 | if (os_snprintf_error(end - pos, ret)) |
| 908 | return pos - buf; |
| 909 | pos += ret; |
| 910 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 911 | #endif /* CONFIG_SAE */ |
| 912 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) { |
| 913 | ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B "); |
| 914 | if (os_snprintf_error(end - pos, ret)) |
| 915 | return pos - buf; |
| 916 | pos += ret; |
| 917 | } |
| 918 | if (hapd->conf->wpa_key_mgmt & |
| 919 | WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) { |
| 920 | ret = os_snprintf(pos, end - pos, |
| 921 | "WPA-EAP-SUITE-B-192 "); |
| 922 | if (os_snprintf_error(end - pos, ret)) |
| 923 | return pos - buf; |
| 924 | pos += ret; |
| 925 | } |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 926 | #ifdef CONFIG_FILS |
| 927 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA256) { |
| 928 | ret = os_snprintf(pos, end - pos, "FILS-SHA256 "); |
| 929 | if (os_snprintf_error(end - pos, ret)) |
| 930 | return pos - buf; |
| 931 | pos += ret; |
| 932 | } |
| 933 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA384) { |
| 934 | ret = os_snprintf(pos, end - pos, "FILS-SHA384 "); |
| 935 | if (os_snprintf_error(end - pos, ret)) |
| 936 | return pos - buf; |
| 937 | pos += ret; |
| 938 | } |
| 939 | #endif /* CONFIG_FILS */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 940 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 941 | #ifdef CONFIG_OWE |
| 942 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) { |
| 943 | ret = os_snprintf(pos, end - pos, "OWE "); |
| 944 | if (os_snprintf_error(end - pos, ret)) |
| 945 | return pos - buf; |
| 946 | pos += ret; |
| 947 | } |
| 948 | #endif /* CONFIG_OWE */ |
| 949 | |
| 950 | #ifdef CONFIG_DPP |
| 951 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) { |
| 952 | ret = os_snprintf(pos, end - pos, "DPP "); |
| 953 | if (os_snprintf_error(end - pos, ret)) |
| 954 | return pos - buf; |
| 955 | pos += ret; |
| 956 | } |
| 957 | #endif /* CONFIG_DPP */ |
| 958 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 959 | if (pos > buf && *(pos - 1) == ' ') { |
| 960 | *(pos - 1) = '\0'; |
| 961 | pos--; |
| 962 | } |
| 963 | |
| 964 | return pos - buf; |
| 965 | } |
| 966 | |
| 967 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 968 | static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd, |
| 969 | char *buf, size_t buflen) |
| 970 | { |
| 971 | int ret; |
| 972 | char *pos, *end; |
| 973 | |
| 974 | pos = buf; |
| 975 | end = buf + buflen; |
| 976 | |
| 977 | ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n" |
| 978 | "ssid=%s\n", |
| 979 | MAC2STR(hapd->own_addr), |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 980 | wpa_ssid_txt(hapd->conf->ssid.ssid, |
| 981 | hapd->conf->ssid.ssid_len)); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 982 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 983 | return pos - buf; |
| 984 | pos += ret; |
| 985 | |
| 986 | #ifdef CONFIG_WPS |
| 987 | ret = os_snprintf(pos, end - pos, "wps_state=%s\n", |
| 988 | hapd->conf->wps_state == 0 ? "disabled" : |
| 989 | (hapd->conf->wps_state == 1 ? "not configured" : |
| 990 | "configured")); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 991 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 992 | return pos - buf; |
| 993 | pos += ret; |
| 994 | |
| 995 | if (hapd->conf->wps_state && hapd->conf->wpa && |
| 996 | hapd->conf->ssid.wpa_passphrase) { |
| 997 | ret = os_snprintf(pos, end - pos, "passphrase=%s\n", |
| 998 | hapd->conf->ssid.wpa_passphrase); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 999 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1000 | return pos - buf; |
| 1001 | pos += ret; |
| 1002 | } |
| 1003 | |
| 1004 | if (hapd->conf->wps_state && hapd->conf->wpa && |
| 1005 | hapd->conf->ssid.wpa_psk && |
| 1006 | hapd->conf->ssid.wpa_psk->group) { |
| 1007 | char hex[PMK_LEN * 2 + 1]; |
| 1008 | wpa_snprintf_hex(hex, sizeof(hex), |
| 1009 | hapd->conf->ssid.wpa_psk->psk, PMK_LEN); |
| 1010 | ret = os_snprintf(pos, end - pos, "psk=%s\n", hex); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1011 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1012 | return pos - buf; |
| 1013 | pos += ret; |
| 1014 | } |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1015 | |
| 1016 | if (hapd->conf->multi_ap) { |
| 1017 | struct hostapd_ssid *ssid = &hapd->conf->multi_ap_backhaul_ssid; |
| 1018 | |
| 1019 | ret = os_snprintf(pos, end - pos, "multi_ap=%d\n", |
| 1020 | hapd->conf->multi_ap); |
| 1021 | if (os_snprintf_error(end - pos, ret)) |
| 1022 | return pos - buf; |
| 1023 | pos += ret; |
| 1024 | |
| 1025 | if (ssid->ssid_len) { |
| 1026 | ret = os_snprintf(pos, end - pos, |
| 1027 | "multi_ap_backhaul_ssid=%s\n", |
| 1028 | wpa_ssid_txt(ssid->ssid, |
| 1029 | ssid->ssid_len)); |
| 1030 | if (os_snprintf_error(end - pos, ret)) |
| 1031 | return pos - buf; |
| 1032 | pos += ret; |
| 1033 | } |
| 1034 | |
| 1035 | if (hapd->conf->wps_state && hapd->conf->wpa && |
| 1036 | ssid->wpa_passphrase) { |
| 1037 | ret = os_snprintf(pos, end - pos, |
| 1038 | "multi_ap_backhaul_wpa_passphrase=%s\n", |
| 1039 | ssid->wpa_passphrase); |
| 1040 | if (os_snprintf_error(end - pos, ret)) |
| 1041 | return pos - buf; |
| 1042 | pos += ret; |
| 1043 | } |
| 1044 | |
| 1045 | if (hapd->conf->wps_state && hapd->conf->wpa && |
| 1046 | ssid->wpa_psk && |
| 1047 | ssid->wpa_psk->group) { |
| 1048 | char hex[PMK_LEN * 2 + 1]; |
| 1049 | |
| 1050 | wpa_snprintf_hex(hex, sizeof(hex), ssid->wpa_psk->psk, |
| 1051 | PMK_LEN); |
| 1052 | ret = os_snprintf(pos, end - pos, |
| 1053 | "multi_ap_backhaul_wpa_psk=%s\n", |
| 1054 | hex); |
| 1055 | forced_memzero(hex, sizeof(hex)); |
| 1056 | if (os_snprintf_error(end - pos, ret)) |
| 1057 | return pos - buf; |
| 1058 | pos += ret; |
| 1059 | } |
| 1060 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1061 | #endif /* CONFIG_WPS */ |
| 1062 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1063 | if (hapd->conf->wpa) { |
| 1064 | ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa); |
| 1065 | if (os_snprintf_error(end - pos, ret)) |
| 1066 | return pos - buf; |
| 1067 | pos += ret; |
| 1068 | } |
| 1069 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1070 | if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) { |
| 1071 | ret = os_snprintf(pos, end - pos, "key_mgmt="); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1072 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1073 | return pos - buf; |
| 1074 | pos += ret; |
| 1075 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1076 | pos += hostapd_ctrl_iface_get_key_mgmt(hapd, pos, end - pos); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1077 | |
| 1078 | ret = os_snprintf(pos, end - pos, "\n"); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1079 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1080 | return pos - buf; |
| 1081 | pos += ret; |
| 1082 | } |
| 1083 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1084 | if (hapd->conf->wpa) { |
| 1085 | ret = os_snprintf(pos, end - pos, "group_cipher=%s\n", |
| 1086 | wpa_cipher_txt(hapd->conf->wpa_group)); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1087 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1088 | return pos - buf; |
| 1089 | pos += ret; |
| 1090 | } |
| 1091 | |
| 1092 | if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) { |
| 1093 | ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher="); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1094 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1095 | return pos - buf; |
| 1096 | pos += ret; |
| 1097 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1098 | ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise, |
| 1099 | " "); |
| 1100 | if (ret < 0) |
| 1101 | return pos - buf; |
| 1102 | pos += ret; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1103 | |
| 1104 | ret = os_snprintf(pos, end - pos, "\n"); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1105 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1106 | return pos - buf; |
| 1107 | pos += ret; |
| 1108 | } |
| 1109 | |
| 1110 | if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) { |
| 1111 | ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher="); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1112 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1113 | return pos - buf; |
| 1114 | pos += ret; |
| 1115 | |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 1116 | ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise, |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1117 | " "); |
| 1118 | if (ret < 0) |
| 1119 | return pos - buf; |
| 1120 | pos += ret; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1121 | |
| 1122 | ret = os_snprintf(pos, end - pos, "\n"); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1123 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1124 | return pos - buf; |
| 1125 | pos += ret; |
| 1126 | } |
| 1127 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1128 | if (hapd->conf->wpa && hapd->conf->wpa_deny_ptk0_rekey) { |
| 1129 | ret = os_snprintf(pos, end - pos, "wpa_deny_ptk0_rekey=%d\n", |
| 1130 | hapd->conf->wpa_deny_ptk0_rekey); |
| 1131 | if (os_snprintf_error(end - pos, ret)) |
| 1132 | return pos - buf; |
| 1133 | pos += ret; |
| 1134 | } |
| 1135 | |
| 1136 | if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->extended_key_id) { |
| 1137 | ret = os_snprintf(pos, end - pos, "extended_key_id=%d\n", |
| 1138 | hapd->conf->extended_key_id); |
| 1139 | if (os_snprintf_error(end - pos, ret)) |
| 1140 | return pos - buf; |
| 1141 | pos += ret; |
| 1142 | } |
| 1143 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1144 | return pos - buf; |
| 1145 | } |
| 1146 | |
| 1147 | |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 1148 | static int hostapd_ctrl_iface_set_band(struct hostapd_data *hapd, |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1149 | const char *bands) |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 1150 | { |
| 1151 | union wpa_event_data event; |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1152 | u32 setband_mask = WPA_SETBAND_AUTO; |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 1153 | |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1154 | /* |
| 1155 | * For example: |
| 1156 | * SET setband 2G,6G |
| 1157 | * SET setband 5G |
| 1158 | * SET setband AUTO |
| 1159 | */ |
| 1160 | if (!os_strstr(bands, "AUTO")) { |
| 1161 | if (os_strstr(bands, "5G")) |
| 1162 | setband_mask |= WPA_SETBAND_5G; |
| 1163 | if (os_strstr(bands, "6G")) |
| 1164 | setband_mask |= WPA_SETBAND_6G; |
| 1165 | if (os_strstr(bands, "2G")) |
| 1166 | setband_mask |= WPA_SETBAND_2G; |
| 1167 | if (setband_mask == WPA_SETBAND_AUTO) |
| 1168 | return -1; |
| 1169 | } |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 1170 | |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1171 | if (hostapd_drv_set_band(hapd, setband_mask) == 0) { |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 1172 | os_memset(&event, 0, sizeof(event)); |
| 1173 | event.channel_list_changed.initiator = REGDOM_SET_BY_USER; |
| 1174 | event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN; |
| 1175 | wpa_supplicant_event(hapd, EVENT_CHANNEL_LIST_CHANGED, &event); |
| 1176 | } |
| 1177 | |
| 1178 | return 0; |
| 1179 | } |
| 1180 | |
| 1181 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1182 | static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd) |
| 1183 | { |
| 1184 | char *value; |
| 1185 | int ret = 0; |
| 1186 | |
| 1187 | value = os_strchr(cmd, ' '); |
| 1188 | if (value == NULL) |
| 1189 | return -1; |
| 1190 | *value++ = '\0'; |
| 1191 | |
| 1192 | wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value); |
| 1193 | if (0) { |
| 1194 | #ifdef CONFIG_WPS_TESTING |
| 1195 | } else if (os_strcasecmp(cmd, "wps_version_number") == 0) { |
| 1196 | long int val; |
| 1197 | val = strtol(value, NULL, 0); |
| 1198 | if (val < 0 || val > 0xff) { |
| 1199 | ret = -1; |
| 1200 | wpa_printf(MSG_DEBUG, "WPS: Invalid " |
| 1201 | "wps_version_number %ld", val); |
| 1202 | } else { |
| 1203 | wps_version_number = val; |
| 1204 | wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS " |
| 1205 | "version %u.%u", |
| 1206 | (wps_version_number & 0xf0) >> 4, |
| 1207 | wps_version_number & 0x0f); |
| 1208 | hostapd_wps_update_ie(hapd); |
| 1209 | } |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 1210 | } else if (os_strcasecmp(cmd, "wps_testing_stub_cred") == 0) { |
| 1211 | wps_testing_stub_cred = atoi(value); |
| 1212 | wpa_printf(MSG_DEBUG, "WPS: Testing - stub_cred=%d", |
| 1213 | wps_testing_stub_cred); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1214 | } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) { |
| 1215 | wps_corrupt_pkhash = atoi(value); |
| 1216 | wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d", |
| 1217 | wps_corrupt_pkhash); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1218 | #endif /* CONFIG_WPS_TESTING */ |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1219 | #ifdef CONFIG_TESTING_OPTIONS |
| 1220 | } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) { |
| 1221 | hapd->ext_mgmt_frame_handling = atoi(value); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1222 | } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) { |
| 1223 | hapd->ext_eapol_frame_io = atoi(value); |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 1224 | } else if (os_strcasecmp(cmd, "force_backlog_bytes") == 0) { |
| 1225 | hapd->force_backlog_bytes = atoi(value); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1226 | #ifdef CONFIG_DPP |
| 1227 | } else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) { |
| 1228 | os_free(hapd->dpp_config_obj_override); |
| 1229 | hapd->dpp_config_obj_override = os_strdup(value); |
| 1230 | } else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) { |
| 1231 | os_free(hapd->dpp_discovery_override); |
| 1232 | hapd->dpp_discovery_override = os_strdup(value); |
| 1233 | } else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) { |
| 1234 | os_free(hapd->dpp_groups_override); |
| 1235 | hapd->dpp_groups_override = os_strdup(value); |
| 1236 | } else if (os_strcasecmp(cmd, |
| 1237 | "dpp_ignore_netaccesskey_mismatch") == 0) { |
| 1238 | hapd->dpp_ignore_netaccesskey_mismatch = atoi(value); |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1239 | } else if (os_strcasecmp(cmd, "dpp_test") == 0) { |
| 1240 | dpp_test = atoi(value); |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame] | 1241 | } else if (os_strcasecmp(cmd, "dpp_version_override") == 0) { |
| 1242 | dpp_version_override = atoi(value); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1243 | #endif /* CONFIG_DPP */ |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1244 | #endif /* CONFIG_TESTING_OPTIONS */ |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 1245 | #ifdef CONFIG_MBO |
| 1246 | } else if (os_strcasecmp(cmd, "mbo_assoc_disallow") == 0) { |
| 1247 | int val; |
| 1248 | |
| 1249 | if (!hapd->conf->mbo_enabled) |
| 1250 | return -1; |
| 1251 | |
| 1252 | val = atoi(value); |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 1253 | if (val < 0 || val > MBO_ASSOC_DISALLOW_REASON_LOW_RSSI) |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 1254 | return -1; |
| 1255 | |
| 1256 | hapd->mbo_assoc_disallow = val; |
| 1257 | ieee802_11_update_beacons(hapd->iface); |
| 1258 | |
| 1259 | /* |
| 1260 | * TODO: Need to configure drivers that do AP MLME offload with |
| 1261 | * disallowing station logic. |
| 1262 | */ |
| 1263 | #endif /* CONFIG_MBO */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1264 | #ifdef CONFIG_DPP |
| 1265 | } else if (os_strcasecmp(cmd, "dpp_configurator_params") == 0) { |
| 1266 | os_free(hapd->dpp_configurator_params); |
| 1267 | hapd->dpp_configurator_params = os_strdup(value); |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1268 | #ifdef CONFIG_DPP2 |
| 1269 | dpp_controller_set_params(hapd->iface->interfaces->dpp, value); |
| 1270 | #endif /* CONFIG_DPP2 */ |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 1271 | } else if (os_strcasecmp(cmd, "dpp_init_max_tries") == 0) { |
| 1272 | hapd->dpp_init_max_tries = atoi(value); |
| 1273 | } else if (os_strcasecmp(cmd, "dpp_init_retry_time") == 0) { |
| 1274 | hapd->dpp_init_retry_time = atoi(value); |
| 1275 | } else if (os_strcasecmp(cmd, "dpp_resp_wait_time") == 0) { |
| 1276 | hapd->dpp_resp_wait_time = atoi(value); |
| 1277 | } else if (os_strcasecmp(cmd, "dpp_resp_max_tries") == 0) { |
| 1278 | hapd->dpp_resp_max_tries = atoi(value); |
| 1279 | } else if (os_strcasecmp(cmd, "dpp_resp_retry_time") == 0) { |
| 1280 | hapd->dpp_resp_retry_time = atoi(value); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1281 | #endif /* CONFIG_DPP */ |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 1282 | } else if (os_strcasecmp(cmd, "setband") == 0) { |
| 1283 | ret = hostapd_ctrl_iface_set_band(hapd, value); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1284 | } else { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1285 | ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value); |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1286 | if (ret) |
| 1287 | return ret; |
| 1288 | |
| 1289 | if (os_strcasecmp(cmd, "deny_mac_file") == 0) { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1290 | hostapd_disassoc_deny_mac(hapd); |
| 1291 | } else if (os_strcasecmp(cmd, "accept_mac_file") == 0) { |
| 1292 | hostapd_disassoc_accept_mac(hapd); |
Hai Shalom | 39ba6fc | 2019-01-22 12:40:38 -0800 | [diff] [blame] | 1293 | } else if (os_strncmp(cmd, "wme_ac_", 7) == 0 || |
| 1294 | os_strncmp(cmd, "wmm_ac_", 7) == 0) { |
| 1295 | hapd->parameter_set_count++; |
| 1296 | if (ieee802_11_update_beacons(hapd->iface)) |
| 1297 | wpa_printf(MSG_DEBUG, |
| 1298 | "Failed to update beacons with WMM parameters"); |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1299 | } else if (os_strcmp(cmd, "wpa_passphrase") == 0 || |
| 1300 | os_strcmp(cmd, "sae_password") == 0 || |
| 1301 | os_strcmp(cmd, "sae_pwe") == 0) { |
| 1302 | if (hapd->started) |
| 1303 | hostapd_setup_sae_pt(hapd->conf); |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1304 | } else if (os_strcasecmp(cmd, "transition_disable") == 0) { |
| 1305 | wpa_auth_set_transition_disable(hapd->wpa_auth, |
| 1306 | hapd->conf->transition_disable); |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1307 | } |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 1308 | |
| 1309 | #ifdef CONFIG_TESTING_OPTIONS |
| 1310 | if (os_strcmp(cmd, "ft_rsnxe_used") == 0) |
| 1311 | wpa_auth_set_ft_rsnxe_used(hapd->wpa_auth, |
| 1312 | hapd->conf->ft_rsnxe_used); |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1313 | else if (os_strcmp(cmd, "oci_freq_override_eapol_m3") == 0) |
| 1314 | wpa_auth_set_ocv_override_freq( |
| 1315 | hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_EAPOL_M3, |
| 1316 | atoi(value)); |
| 1317 | else if (os_strcmp(cmd, "oci_freq_override_eapol_g1") == 0) |
| 1318 | wpa_auth_set_ocv_override_freq( |
| 1319 | hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_EAPOL_G1, |
| 1320 | atoi(value)); |
| 1321 | else if (os_strcmp(cmd, "oci_freq_override_ft_assoc") == 0) |
| 1322 | wpa_auth_set_ocv_override_freq( |
| 1323 | hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_FT_ASSOC, |
| 1324 | atoi(value)); |
| 1325 | else if (os_strcmp(cmd, "oci_freq_override_fils_assoc") == 0) |
| 1326 | wpa_auth_set_ocv_override_freq( |
| 1327 | hapd->wpa_auth, |
| 1328 | WPA_AUTH_OCV_OVERRIDE_FILS_ASSOC, atoi(value)); |
Kai Shi | e75b065 | 2020-11-24 20:31:29 -0800 | [diff] [blame] | 1329 | else if (os_strcasecmp(cmd, "skip_send_eapol") == 0) |
| 1330 | wpa_auth_set_skip_send_eapol(hapd->wpa_auth, atoi(value)); |
| 1331 | else if (os_strcasecmp(cmd, "enable_eapol_large_timeout") == 0) |
| 1332 | wpa_auth_set_enable_eapol_large_timeout(hapd->wpa_auth, atoi(value)); |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 1333 | #endif /* CONFIG_TESTING_OPTIONS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | return ret; |
| 1337 | } |
| 1338 | |
| 1339 | |
| 1340 | static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd, |
| 1341 | char *buf, size_t buflen) |
| 1342 | { |
| 1343 | int res; |
| 1344 | |
| 1345 | wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd); |
| 1346 | |
| 1347 | if (os_strcmp(cmd, "version") == 0) { |
| 1348 | res = os_snprintf(buf, buflen, "%s", VERSION_STR); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1349 | if (os_snprintf_error(buflen, res)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1350 | return -1; |
| 1351 | return res; |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 1352 | } else if (os_strcmp(cmd, "tls_library") == 0) { |
| 1353 | res = tls_get_library_version(buf, buflen); |
| 1354 | if (os_snprintf_error(buflen, res)) |
| 1355 | return -1; |
| 1356 | return res; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | return -1; |
| 1360 | } |
| 1361 | |
| 1362 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1363 | static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface) |
| 1364 | { |
| 1365 | if (hostapd_enable_iface(iface) < 0) { |
| 1366 | wpa_printf(MSG_ERROR, "Enabling of interface failed"); |
| 1367 | return -1; |
| 1368 | } |
| 1369 | return 0; |
| 1370 | } |
| 1371 | |
| 1372 | |
| 1373 | static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface) |
| 1374 | { |
| 1375 | if (hostapd_reload_iface(iface) < 0) { |
| 1376 | wpa_printf(MSG_ERROR, "Reloading of interface failed"); |
| 1377 | return -1; |
| 1378 | } |
| 1379 | return 0; |
| 1380 | } |
| 1381 | |
| 1382 | |
| 1383 | static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface) |
| 1384 | { |
| 1385 | if (hostapd_disable_iface(iface) < 0) { |
| 1386 | wpa_printf(MSG_ERROR, "Disabling of interface failed"); |
| 1387 | return -1; |
| 1388 | } |
| 1389 | return 0; |
| 1390 | } |
| 1391 | |
| 1392 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 1393 | static int |
| 1394 | hostapd_ctrl_iface_kick_mismatch_psk_sta_iter(struct hostapd_data *hapd, |
| 1395 | struct sta_info *sta, void *ctx) |
| 1396 | { |
| 1397 | struct hostapd_wpa_psk *psk; |
| 1398 | const u8 *pmk; |
| 1399 | int pmk_len; |
| 1400 | int pmk_match; |
| 1401 | int sta_match; |
| 1402 | int bss_match; |
| 1403 | int reason; |
| 1404 | |
| 1405 | pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len); |
| 1406 | |
| 1407 | for (psk = hapd->conf->ssid.wpa_psk; pmk && psk; psk = psk->next) { |
| 1408 | pmk_match = PMK_LEN == pmk_len && |
| 1409 | os_memcmp(psk->psk, pmk, pmk_len) == 0; |
| 1410 | sta_match = psk->group == 0 && |
| 1411 | os_memcmp(sta->addr, psk->addr, ETH_ALEN) == 0; |
| 1412 | bss_match = psk->group == 1; |
| 1413 | |
| 1414 | if (pmk_match && (sta_match || bss_match)) |
| 1415 | return 0; |
| 1416 | } |
| 1417 | |
| 1418 | wpa_printf(MSG_INFO, "STA " MACSTR |
| 1419 | " PSK/passphrase no longer valid - disconnect", |
| 1420 | MAC2STR(sta->addr)); |
| 1421 | reason = WLAN_REASON_PREV_AUTH_NOT_VALID; |
| 1422 | hostapd_drv_sta_deauth(hapd, sta->addr, reason); |
| 1423 | ap_sta_deauthenticate(hapd, sta, reason); |
| 1424 | |
| 1425 | return 0; |
| 1426 | } |
| 1427 | |
| 1428 | |
| 1429 | static int hostapd_ctrl_iface_reload_wpa_psk(struct hostapd_data *hapd) |
| 1430 | { |
| 1431 | struct hostapd_bss_config *conf = hapd->conf; |
| 1432 | int err; |
| 1433 | |
| 1434 | hostapd_config_clear_wpa_psk(&conf->ssid.wpa_psk); |
| 1435 | |
| 1436 | err = hostapd_setup_wpa_psk(conf); |
| 1437 | if (err < 0) { |
| 1438 | wpa_printf(MSG_ERROR, "Reloading WPA-PSK passwords failed: %d", |
| 1439 | err); |
| 1440 | return -1; |
| 1441 | } |
| 1442 | |
| 1443 | ap_for_each_sta(hapd, hostapd_ctrl_iface_kick_mismatch_psk_sta_iter, |
| 1444 | NULL); |
| 1445 | |
| 1446 | return 0; |
| 1447 | } |
| 1448 | |
| 1449 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1450 | #ifdef CONFIG_TESTING_OPTIONS |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1451 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1452 | static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd) |
| 1453 | { |
| 1454 | union wpa_event_data data; |
| 1455 | char *pos, *param; |
| 1456 | enum wpa_event_type event; |
| 1457 | |
| 1458 | wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd); |
| 1459 | |
| 1460 | os_memset(&data, 0, sizeof(data)); |
| 1461 | |
| 1462 | param = os_strchr(cmd, ' '); |
| 1463 | if (param == NULL) |
| 1464 | return -1; |
| 1465 | *param++ = '\0'; |
| 1466 | |
| 1467 | if (os_strcmp(cmd, "DETECTED") == 0) |
| 1468 | event = EVENT_DFS_RADAR_DETECTED; |
| 1469 | else if (os_strcmp(cmd, "CAC-FINISHED") == 0) |
| 1470 | event = EVENT_DFS_CAC_FINISHED; |
| 1471 | else if (os_strcmp(cmd, "CAC-ABORTED") == 0) |
| 1472 | event = EVENT_DFS_CAC_ABORTED; |
| 1473 | else if (os_strcmp(cmd, "NOP-FINISHED") == 0) |
| 1474 | event = EVENT_DFS_NOP_FINISHED; |
| 1475 | else { |
| 1476 | wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s", |
| 1477 | cmd); |
| 1478 | return -1; |
| 1479 | } |
| 1480 | |
| 1481 | pos = os_strstr(param, "freq="); |
| 1482 | if (pos) |
| 1483 | data.dfs_event.freq = atoi(pos + 5); |
| 1484 | |
| 1485 | pos = os_strstr(param, "ht_enabled=1"); |
| 1486 | if (pos) |
| 1487 | data.dfs_event.ht_enabled = 1; |
| 1488 | |
| 1489 | pos = os_strstr(param, "chan_offset="); |
| 1490 | if (pos) |
| 1491 | data.dfs_event.chan_offset = atoi(pos + 12); |
| 1492 | |
| 1493 | pos = os_strstr(param, "chan_width="); |
| 1494 | if (pos) |
| 1495 | data.dfs_event.chan_width = atoi(pos + 11); |
| 1496 | |
| 1497 | pos = os_strstr(param, "cf1="); |
| 1498 | if (pos) |
| 1499 | data.dfs_event.cf1 = atoi(pos + 4); |
| 1500 | |
| 1501 | pos = os_strstr(param, "cf2="); |
| 1502 | if (pos) |
| 1503 | data.dfs_event.cf2 = atoi(pos + 4); |
| 1504 | |
| 1505 | wpa_supplicant_event(hapd, event, &data); |
| 1506 | |
| 1507 | return 0; |
| 1508 | } |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1509 | |
| 1510 | |
| 1511 | static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd) |
| 1512 | { |
| 1513 | size_t len; |
| 1514 | u8 *buf; |
| 1515 | int res; |
| 1516 | |
| 1517 | wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd); |
| 1518 | |
| 1519 | len = os_strlen(cmd); |
| 1520 | if (len & 1) |
| 1521 | return -1; |
| 1522 | len /= 2; |
| 1523 | |
| 1524 | buf = os_malloc(len); |
| 1525 | if (buf == NULL) |
| 1526 | return -1; |
| 1527 | |
| 1528 | if (hexstr2bin(cmd, buf, len) < 0) { |
| 1529 | os_free(buf); |
| 1530 | return -1; |
| 1531 | } |
| 1532 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1533 | res = hostapd_drv_send_mlme(hapd, buf, len, 0, NULL, 0, 0); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1534 | os_free(buf); |
| 1535 | return res; |
| 1536 | } |
| 1537 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1538 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1539 | static int hostapd_ctrl_iface_mgmt_tx_status_process(struct hostapd_data *hapd, |
| 1540 | char *cmd) |
| 1541 | { |
| 1542 | char *pos, *param; |
| 1543 | size_t len; |
| 1544 | u8 *buf; |
| 1545 | int stype = 0, ok = 0; |
| 1546 | union wpa_event_data event; |
| 1547 | |
| 1548 | if (!hapd->ext_mgmt_frame_handling) |
| 1549 | return -1; |
| 1550 | |
| 1551 | /* stype=<val> ok=<0/1> buf=<frame hexdump> */ |
| 1552 | |
| 1553 | wpa_printf(MSG_DEBUG, "External MGMT TX status process: %s", cmd); |
| 1554 | |
| 1555 | pos = cmd; |
| 1556 | param = os_strstr(pos, "stype="); |
| 1557 | if (param) { |
| 1558 | param += 6; |
| 1559 | stype = atoi(param); |
| 1560 | } |
| 1561 | |
| 1562 | param = os_strstr(pos, " ok="); |
| 1563 | if (param) { |
| 1564 | param += 4; |
| 1565 | ok = atoi(param); |
| 1566 | } |
| 1567 | |
| 1568 | param = os_strstr(pos, " buf="); |
| 1569 | if (!param) |
| 1570 | return -1; |
| 1571 | param += 5; |
| 1572 | |
| 1573 | len = os_strlen(param); |
| 1574 | if (len & 1) |
| 1575 | return -1; |
| 1576 | len /= 2; |
| 1577 | |
| 1578 | buf = os_malloc(len); |
| 1579 | if (!buf || hexstr2bin(param, buf, len) < 0) { |
| 1580 | os_free(buf); |
| 1581 | return -1; |
| 1582 | } |
| 1583 | |
| 1584 | os_memset(&event, 0, sizeof(event)); |
| 1585 | event.tx_status.type = WLAN_FC_TYPE_MGMT; |
| 1586 | event.tx_status.data = buf; |
| 1587 | event.tx_status.data_len = len; |
| 1588 | event.tx_status.stype = stype; |
| 1589 | event.tx_status.ack = ok; |
| 1590 | hapd->ext_mgmt_frame_handling = 0; |
| 1591 | wpa_supplicant_event(hapd, EVENT_TX_STATUS, &event); |
| 1592 | hapd->ext_mgmt_frame_handling = 1; |
| 1593 | |
| 1594 | os_free(buf); |
| 1595 | |
| 1596 | return 0; |
| 1597 | } |
| 1598 | |
| 1599 | |
Dmitry Shmidt | abb90a3 | 2016-12-05 15:34:39 -0800 | [diff] [blame] | 1600 | static int hostapd_ctrl_iface_mgmt_rx_process(struct hostapd_data *hapd, |
| 1601 | char *cmd) |
| 1602 | { |
| 1603 | char *pos, *param; |
| 1604 | size_t len; |
| 1605 | u8 *buf; |
| 1606 | int freq = 0, datarate = 0, ssi_signal = 0; |
| 1607 | union wpa_event_data event; |
| 1608 | |
| 1609 | if (!hapd->ext_mgmt_frame_handling) |
| 1610 | return -1; |
| 1611 | |
| 1612 | /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */ |
| 1613 | |
| 1614 | wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd); |
| 1615 | |
| 1616 | pos = cmd; |
| 1617 | param = os_strstr(pos, "freq="); |
| 1618 | if (param) { |
| 1619 | param += 5; |
| 1620 | freq = atoi(param); |
| 1621 | } |
| 1622 | |
| 1623 | param = os_strstr(pos, " datarate="); |
| 1624 | if (param) { |
| 1625 | param += 10; |
| 1626 | datarate = atoi(param); |
| 1627 | } |
| 1628 | |
| 1629 | param = os_strstr(pos, " ssi_signal="); |
| 1630 | if (param) { |
| 1631 | param += 12; |
| 1632 | ssi_signal = atoi(param); |
| 1633 | } |
| 1634 | |
| 1635 | param = os_strstr(pos, " frame="); |
| 1636 | if (param == NULL) |
| 1637 | return -1; |
| 1638 | param += 7; |
| 1639 | |
| 1640 | len = os_strlen(param); |
| 1641 | if (len & 1) |
| 1642 | return -1; |
| 1643 | len /= 2; |
| 1644 | |
| 1645 | buf = os_malloc(len); |
| 1646 | if (buf == NULL) |
| 1647 | return -1; |
| 1648 | |
| 1649 | if (hexstr2bin(param, buf, len) < 0) { |
| 1650 | os_free(buf); |
| 1651 | return -1; |
| 1652 | } |
| 1653 | |
| 1654 | os_memset(&event, 0, sizeof(event)); |
| 1655 | event.rx_mgmt.freq = freq; |
| 1656 | event.rx_mgmt.frame = buf; |
| 1657 | event.rx_mgmt.frame_len = len; |
| 1658 | event.rx_mgmt.ssi_signal = ssi_signal; |
| 1659 | event.rx_mgmt.datarate = datarate; |
| 1660 | hapd->ext_mgmt_frame_handling = 0; |
| 1661 | wpa_supplicant_event(hapd, EVENT_RX_MGMT, &event); |
| 1662 | hapd->ext_mgmt_frame_handling = 1; |
| 1663 | |
| 1664 | os_free(buf); |
| 1665 | |
| 1666 | return 0; |
| 1667 | } |
| 1668 | |
| 1669 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1670 | static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd) |
| 1671 | { |
| 1672 | char *pos; |
| 1673 | u8 src[ETH_ALEN], *buf; |
| 1674 | int used; |
| 1675 | size_t len; |
| 1676 | |
| 1677 | wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd); |
| 1678 | |
| 1679 | pos = cmd; |
| 1680 | used = hwaddr_aton2(pos, src); |
| 1681 | if (used < 0) |
| 1682 | return -1; |
| 1683 | pos += used; |
| 1684 | while (*pos == ' ') |
| 1685 | pos++; |
| 1686 | |
| 1687 | len = os_strlen(pos); |
| 1688 | if (len & 1) |
| 1689 | return -1; |
| 1690 | len /= 2; |
| 1691 | |
| 1692 | buf = os_malloc(len); |
| 1693 | if (buf == NULL) |
| 1694 | return -1; |
| 1695 | |
| 1696 | if (hexstr2bin(pos, buf, len) < 0) { |
| 1697 | os_free(buf); |
| 1698 | return -1; |
| 1699 | } |
| 1700 | |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 1701 | ieee802_1x_receive(hapd, src, buf, len, FRAME_ENCRYPTION_UNKNOWN); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1702 | os_free(buf); |
| 1703 | |
| 1704 | return 0; |
| 1705 | } |
| 1706 | |
| 1707 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 1708 | static int hostapd_ctrl_iface_eapol_tx(struct hostapd_data *hapd, char *cmd) |
| 1709 | { |
| 1710 | char *pos, *pos2; |
| 1711 | u8 dst[ETH_ALEN], *buf; |
| 1712 | int used, ret; |
| 1713 | size_t len; |
| 1714 | unsigned int prev; |
| 1715 | int encrypt = 0; |
| 1716 | |
| 1717 | wpa_printf(MSG_DEBUG, "External EAPOL TX: %s", cmd); |
| 1718 | |
| 1719 | pos = cmd; |
| 1720 | used = hwaddr_aton2(pos, dst); |
| 1721 | if (used < 0) |
| 1722 | return -1; |
| 1723 | pos += used; |
| 1724 | while (*pos == ' ') |
| 1725 | pos++; |
| 1726 | |
| 1727 | pos2 = os_strchr(pos, ' '); |
| 1728 | if (pos2) { |
| 1729 | len = pos2 - pos; |
| 1730 | encrypt = os_strstr(pos2, "encrypt=1") != NULL; |
| 1731 | } else { |
| 1732 | len = os_strlen(pos); |
| 1733 | } |
| 1734 | if (len & 1) |
| 1735 | return -1; |
| 1736 | len /= 2; |
| 1737 | |
| 1738 | buf = os_malloc(len); |
| 1739 | if (!buf || hexstr2bin(pos, buf, len) < 0) { |
| 1740 | os_free(buf); |
| 1741 | return -1; |
| 1742 | } |
| 1743 | |
| 1744 | prev = hapd->ext_eapol_frame_io; |
| 1745 | hapd->ext_eapol_frame_io = 0; |
| 1746 | ret = hostapd_wpa_auth_send_eapol(hapd, dst, buf, len, encrypt); |
| 1747 | hapd->ext_eapol_frame_io = prev; |
| 1748 | os_free(buf); |
| 1749 | |
| 1750 | return ret; |
| 1751 | } |
| 1752 | |
| 1753 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1754 | static u16 ipv4_hdr_checksum(const void *buf, size_t len) |
| 1755 | { |
| 1756 | size_t i; |
| 1757 | u32 sum = 0; |
| 1758 | const u16 *pos = buf; |
| 1759 | |
| 1760 | for (i = 0; i < len / 2; i++) |
| 1761 | sum += *pos++; |
| 1762 | |
| 1763 | while (sum >> 16) |
| 1764 | sum = (sum & 0xffff) + (sum >> 16); |
| 1765 | |
| 1766 | return sum ^ 0xffff; |
| 1767 | } |
| 1768 | |
| 1769 | |
| 1770 | #define HWSIM_PACKETLEN 1500 |
| 1771 | #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header)) |
| 1772 | |
Dmitry Shmidt | 4ae50e6 | 2016-06-27 13:48:39 -0700 | [diff] [blame] | 1773 | static void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf, |
| 1774 | size_t len) |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1775 | { |
| 1776 | struct hostapd_data *hapd = ctx; |
| 1777 | const struct ether_header *eth; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1778 | struct ip ip; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1779 | const u8 *pos; |
| 1780 | unsigned int i; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1781 | char extra[30]; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1782 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1783 | if (len < sizeof(*eth) + sizeof(ip) || len > HWSIM_PACKETLEN) { |
| 1784 | wpa_printf(MSG_DEBUG, |
| 1785 | "test data: RX - ignore unexpected length %d", |
| 1786 | (int) len); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1787 | return; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1788 | } |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1789 | |
| 1790 | eth = (const struct ether_header *) buf; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1791 | os_memcpy(&ip, eth + 1, sizeof(ip)); |
| 1792 | pos = &buf[sizeof(*eth) + sizeof(ip)]; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1793 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1794 | if (ip.ip_hl != 5 || ip.ip_v != 4 || |
| 1795 | ntohs(ip.ip_len) > HWSIM_IP_LEN) { |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1796 | wpa_printf(MSG_DEBUG, |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1797 | "test data: RX - ignore unexpected IP header"); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1798 | return; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1799 | } |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1800 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1801 | for (i = 0; i < ntohs(ip.ip_len) - sizeof(ip); i++) { |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1802 | if (*pos != (u8) i) { |
| 1803 | wpa_printf(MSG_DEBUG, |
| 1804 | "test data: RX - ignore mismatching payload"); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1805 | return; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1806 | } |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1807 | pos++; |
| 1808 | } |
| 1809 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1810 | extra[0] = '\0'; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1811 | if (ntohs(ip.ip_len) != HWSIM_IP_LEN) |
| 1812 | os_snprintf(extra, sizeof(extra), " len=%d", ntohs(ip.ip_len)); |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1813 | wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR "%s", |
| 1814 | MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost), extra); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | |
| 1818 | static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd, |
| 1819 | char *cmd) |
| 1820 | { |
| 1821 | int enabled = atoi(cmd); |
| 1822 | char *pos; |
| 1823 | const char *ifname; |
| 1824 | |
| 1825 | if (!enabled) { |
| 1826 | if (hapd->l2_test) { |
| 1827 | l2_packet_deinit(hapd->l2_test); |
| 1828 | hapd->l2_test = NULL; |
| 1829 | wpa_dbg(hapd->msg_ctx, MSG_DEBUG, |
| 1830 | "test data: Disabled"); |
| 1831 | } |
| 1832 | return 0; |
| 1833 | } |
| 1834 | |
| 1835 | if (hapd->l2_test) |
| 1836 | return 0; |
| 1837 | |
| 1838 | pos = os_strstr(cmd, " ifname="); |
| 1839 | if (pos) |
| 1840 | ifname = pos + 8; |
| 1841 | else |
| 1842 | ifname = hapd->conf->iface; |
| 1843 | |
| 1844 | hapd->l2_test = l2_packet_init(ifname, hapd->own_addr, |
| 1845 | ETHERTYPE_IP, hostapd_data_test_rx, |
| 1846 | hapd, 1); |
| 1847 | if (hapd->l2_test == NULL) |
| 1848 | return -1; |
| 1849 | |
| 1850 | wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled"); |
| 1851 | |
| 1852 | return 0; |
| 1853 | } |
| 1854 | |
| 1855 | |
| 1856 | static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd) |
| 1857 | { |
| 1858 | u8 dst[ETH_ALEN], src[ETH_ALEN]; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1859 | char *pos, *pos2; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1860 | int used; |
| 1861 | long int val; |
| 1862 | u8 tos; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1863 | u8 buf[2 + HWSIM_PACKETLEN]; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1864 | struct ether_header *eth; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1865 | struct ip *ip; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1866 | u8 *dpos; |
| 1867 | unsigned int i; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1868 | size_t send_len = HWSIM_IP_LEN; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1869 | |
| 1870 | if (hapd->l2_test == NULL) |
| 1871 | return -1; |
| 1872 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1873 | /* format: <dst> <src> <tos> [len=<length>] */ |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1874 | |
| 1875 | pos = cmd; |
| 1876 | used = hwaddr_aton2(pos, dst); |
| 1877 | if (used < 0) |
| 1878 | return -1; |
| 1879 | pos += used; |
| 1880 | while (*pos == ' ') |
| 1881 | pos++; |
| 1882 | used = hwaddr_aton2(pos, src); |
| 1883 | if (used < 0) |
| 1884 | return -1; |
| 1885 | pos += used; |
| 1886 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1887 | val = strtol(pos, &pos2, 0); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1888 | if (val < 0 || val > 0xff) |
| 1889 | return -1; |
| 1890 | tos = val; |
| 1891 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1892 | pos = os_strstr(pos2, " len="); |
| 1893 | if (pos) { |
| 1894 | i = atoi(pos + 5); |
| 1895 | if (i < sizeof(*ip) || i > HWSIM_IP_LEN) |
| 1896 | return -1; |
| 1897 | send_len = i; |
| 1898 | } |
| 1899 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1900 | eth = (struct ether_header *) &buf[2]; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1901 | os_memcpy(eth->ether_dhost, dst, ETH_ALEN); |
| 1902 | os_memcpy(eth->ether_shost, src, ETH_ALEN); |
| 1903 | eth->ether_type = htons(ETHERTYPE_IP); |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1904 | ip = (struct ip *) (eth + 1); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1905 | os_memset(ip, 0, sizeof(*ip)); |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1906 | ip->ip_hl = 5; |
| 1907 | ip->ip_v = 4; |
| 1908 | ip->ip_ttl = 64; |
| 1909 | ip->ip_tos = tos; |
| 1910 | ip->ip_len = htons(send_len); |
| 1911 | ip->ip_p = 1; |
| 1912 | ip->ip_src.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1); |
| 1913 | ip->ip_dst.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2); |
| 1914 | ip->ip_sum = ipv4_hdr_checksum(ip, sizeof(*ip)); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1915 | dpos = (u8 *) (ip + 1); |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1916 | for (i = 0; i < send_len - sizeof(*ip); i++) |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1917 | *dpos++ = i; |
| 1918 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1919 | if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2], |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1920 | sizeof(struct ether_header) + send_len) < 0) |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1921 | return -1; |
| 1922 | |
| 1923 | wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR |
| 1924 | " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos); |
| 1925 | |
| 1926 | return 0; |
| 1927 | } |
| 1928 | |
| 1929 | |
| 1930 | static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd, |
| 1931 | char *cmd) |
| 1932 | { |
| 1933 | u8 *buf; |
| 1934 | struct ether_header *eth; |
| 1935 | struct l2_packet_data *l2 = NULL; |
| 1936 | size_t len; |
| 1937 | u16 ethertype; |
| 1938 | int res = -1; |
| 1939 | const char *ifname = hapd->conf->iface; |
| 1940 | |
| 1941 | if (os_strncmp(cmd, "ifname=", 7) == 0) { |
| 1942 | cmd += 7; |
| 1943 | ifname = cmd; |
| 1944 | cmd = os_strchr(cmd, ' '); |
| 1945 | if (cmd == NULL) |
| 1946 | return -1; |
| 1947 | *cmd++ = '\0'; |
| 1948 | } |
| 1949 | |
| 1950 | len = os_strlen(cmd); |
| 1951 | if (len & 1 || len < ETH_HLEN * 2) |
| 1952 | return -1; |
| 1953 | len /= 2; |
| 1954 | |
| 1955 | buf = os_malloc(len); |
| 1956 | if (buf == NULL) |
| 1957 | return -1; |
| 1958 | |
| 1959 | if (hexstr2bin(cmd, buf, len) < 0) |
| 1960 | goto done; |
| 1961 | |
| 1962 | eth = (struct ether_header *) buf; |
| 1963 | ethertype = ntohs(eth->ether_type); |
| 1964 | |
| 1965 | l2 = l2_packet_init(ifname, hapd->own_addr, ethertype, |
| 1966 | hostapd_data_test_rx, hapd, 1); |
| 1967 | if (l2 == NULL) |
| 1968 | goto done; |
| 1969 | |
| 1970 | res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len); |
| 1971 | wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res); |
| 1972 | done: |
| 1973 | if (l2) |
| 1974 | l2_packet_deinit(l2); |
| 1975 | os_free(buf); |
| 1976 | |
| 1977 | return res < 0 ? -1 : 0; |
| 1978 | } |
| 1979 | |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 1980 | |
| 1981 | static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd) |
| 1982 | { |
| 1983 | #ifdef WPA_TRACE_BFD |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 1984 | char *pos; |
| 1985 | |
| 1986 | wpa_trace_fail_after = atoi(cmd); |
| 1987 | pos = os_strchr(cmd, ':'); |
| 1988 | if (pos) { |
| 1989 | pos++; |
| 1990 | os_strlcpy(wpa_trace_fail_func, pos, |
| 1991 | sizeof(wpa_trace_fail_func)); |
| 1992 | } else { |
| 1993 | wpa_trace_fail_after = 0; |
| 1994 | } |
| 1995 | |
| 1996 | return 0; |
| 1997 | #else /* WPA_TRACE_BFD */ |
| 1998 | return -1; |
| 1999 | #endif /* WPA_TRACE_BFD */ |
| 2000 | } |
| 2001 | |
| 2002 | |
| 2003 | static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd, |
| 2004 | char *buf, size_t buflen) |
| 2005 | { |
| 2006 | #ifdef WPA_TRACE_BFD |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 2007 | return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after, |
| 2008 | wpa_trace_fail_func); |
| 2009 | #else /* WPA_TRACE_BFD */ |
| 2010 | return -1; |
| 2011 | #endif /* WPA_TRACE_BFD */ |
| 2012 | } |
| 2013 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2014 | |
| 2015 | static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd) |
| 2016 | { |
| 2017 | #ifdef WPA_TRACE_BFD |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2018 | char *pos; |
| 2019 | |
| 2020 | wpa_trace_test_fail_after = atoi(cmd); |
| 2021 | pos = os_strchr(cmd, ':'); |
| 2022 | if (pos) { |
| 2023 | pos++; |
| 2024 | os_strlcpy(wpa_trace_test_fail_func, pos, |
| 2025 | sizeof(wpa_trace_test_fail_func)); |
| 2026 | } else { |
| 2027 | wpa_trace_test_fail_after = 0; |
| 2028 | } |
| 2029 | |
| 2030 | return 0; |
| 2031 | #else /* WPA_TRACE_BFD */ |
| 2032 | return -1; |
| 2033 | #endif /* WPA_TRACE_BFD */ |
| 2034 | } |
| 2035 | |
| 2036 | |
| 2037 | static int hostapd_ctrl_get_fail(struct hostapd_data *hapd, |
| 2038 | char *buf, size_t buflen) |
| 2039 | { |
| 2040 | #ifdef WPA_TRACE_BFD |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2041 | return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after, |
| 2042 | wpa_trace_test_fail_func); |
| 2043 | #else /* WPA_TRACE_BFD */ |
| 2044 | return -1; |
| 2045 | #endif /* WPA_TRACE_BFD */ |
| 2046 | } |
| 2047 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2048 | |
| 2049 | static int hostapd_ctrl_reset_pn(struct hostapd_data *hapd, const char *cmd) |
| 2050 | { |
| 2051 | struct sta_info *sta; |
| 2052 | u8 addr[ETH_ALEN]; |
| 2053 | u8 zero[WPA_TK_MAX_LEN]; |
| 2054 | |
| 2055 | os_memset(zero, 0, sizeof(zero)); |
| 2056 | |
| 2057 | if (hwaddr_aton(cmd, addr)) |
| 2058 | return -1; |
| 2059 | |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame] | 2060 | if (is_broadcast_ether_addr(addr) && os_strstr(cmd, " BIGTK")) { |
| 2061 | if (hapd->last_bigtk_alg == WPA_ALG_NONE) |
| 2062 | return -1; |
| 2063 | |
| 2064 | wpa_printf(MSG_INFO, "TESTING: Reset BIPN for BIGTK"); |
| 2065 | |
| 2066 | /* First, use a zero key to avoid any possible duplicate key |
| 2067 | * avoidance in the driver. */ |
| 2068 | if (hostapd_drv_set_key(hapd->conf->iface, hapd, |
| 2069 | hapd->last_bigtk_alg, |
| 2070 | broadcast_ether_addr, |
| 2071 | hapd->last_bigtk_key_idx, 0, 1, NULL, 0, |
| 2072 | zero, hapd->last_bigtk_len, |
| 2073 | KEY_FLAG_GROUP_TX_DEFAULT) < 0) |
| 2074 | return -1; |
| 2075 | |
| 2076 | /* Set the previously configured key to reset its TSC */ |
| 2077 | return hostapd_drv_set_key(hapd->conf->iface, hapd, |
| 2078 | hapd->last_bigtk_alg, |
| 2079 | broadcast_ether_addr, |
| 2080 | hapd->last_bigtk_key_idx, 0, 1, NULL, |
| 2081 | 0, hapd->last_bigtk, |
| 2082 | hapd->last_bigtk_len, |
| 2083 | KEY_FLAG_GROUP_TX_DEFAULT); |
| 2084 | } |
| 2085 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2086 | if (is_broadcast_ether_addr(addr) && os_strstr(cmd, "IGTK")) { |
| 2087 | if (hapd->last_igtk_alg == WPA_ALG_NONE) |
| 2088 | return -1; |
| 2089 | |
| 2090 | wpa_printf(MSG_INFO, "TESTING: Reset IPN for IGTK"); |
| 2091 | |
| 2092 | /* First, use a zero key to avoid any possible duplicate key |
| 2093 | * avoidance in the driver. */ |
| 2094 | if (hostapd_drv_set_key(hapd->conf->iface, hapd, |
| 2095 | hapd->last_igtk_alg, |
| 2096 | broadcast_ether_addr, |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2097 | hapd->last_igtk_key_idx, 0, 1, NULL, 0, |
| 2098 | zero, hapd->last_igtk_len, |
| 2099 | KEY_FLAG_GROUP_TX_DEFAULT) < 0) |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2100 | return -1; |
| 2101 | |
| 2102 | /* Set the previously configured key to reset its TSC */ |
| 2103 | return hostapd_drv_set_key(hapd->conf->iface, hapd, |
| 2104 | hapd->last_igtk_alg, |
| 2105 | broadcast_ether_addr, |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2106 | hapd->last_igtk_key_idx, 0, 1, NULL, |
| 2107 | 0, hapd->last_igtk, |
| 2108 | hapd->last_igtk_len, |
| 2109 | KEY_FLAG_GROUP_TX_DEFAULT); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2110 | } |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2111 | |
| 2112 | if (is_broadcast_ether_addr(addr)) { |
| 2113 | if (hapd->last_gtk_alg == WPA_ALG_NONE) |
| 2114 | return -1; |
| 2115 | |
| 2116 | wpa_printf(MSG_INFO, "TESTING: Reset PN for GTK"); |
| 2117 | |
| 2118 | /* First, use a zero key to avoid any possible duplicate key |
| 2119 | * avoidance in the driver. */ |
| 2120 | if (hostapd_drv_set_key(hapd->conf->iface, hapd, |
| 2121 | hapd->last_gtk_alg, |
| 2122 | broadcast_ether_addr, |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2123 | hapd->last_gtk_key_idx, 0, 1, NULL, 0, |
| 2124 | zero, hapd->last_gtk_len, |
| 2125 | KEY_FLAG_GROUP_TX_DEFAULT) < 0) |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2126 | return -1; |
| 2127 | |
| 2128 | /* Set the previously configured key to reset its TSC */ |
| 2129 | return hostapd_drv_set_key(hapd->conf->iface, hapd, |
| 2130 | hapd->last_gtk_alg, |
| 2131 | broadcast_ether_addr, |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2132 | hapd->last_gtk_key_idx, 0, 1, NULL, |
| 2133 | 0, hapd->last_gtk, |
| 2134 | hapd->last_gtk_len, |
| 2135 | KEY_FLAG_GROUP_TX_DEFAULT); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2136 | } |
| 2137 | |
| 2138 | sta = ap_get_sta(hapd, addr); |
| 2139 | if (!sta) |
| 2140 | return -1; |
| 2141 | |
| 2142 | if (sta->last_tk_alg == WPA_ALG_NONE) |
| 2143 | return -1; |
| 2144 | |
| 2145 | wpa_printf(MSG_INFO, "TESTING: Reset PN for " MACSTR, |
| 2146 | MAC2STR(sta->addr)); |
| 2147 | |
| 2148 | /* First, use a zero key to avoid any possible duplicate key avoidance |
| 2149 | * in the driver. */ |
| 2150 | if (hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg, |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2151 | sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0, |
| 2152 | zero, sta->last_tk_len, |
| 2153 | KEY_FLAG_PAIRWISE_RX_TX) < 0) |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2154 | return -1; |
| 2155 | |
| 2156 | /* Set the previously configured key to reset its TSC/RSC */ |
| 2157 | return hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg, |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2158 | sta->addr, sta->last_tk_key_idx, 0, 1, NULL, |
| 2159 | 0, sta->last_tk, sta->last_tk_len, |
| 2160 | KEY_FLAG_PAIRWISE_RX_TX); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2161 | } |
| 2162 | |
| 2163 | |
| 2164 | static int hostapd_ctrl_set_key(struct hostapd_data *hapd, const char *cmd) |
| 2165 | { |
| 2166 | u8 addr[ETH_ALEN]; |
| 2167 | const char *pos = cmd; |
| 2168 | enum wpa_alg alg; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2169 | enum key_flag key_flag; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2170 | int idx, set_tx; |
| 2171 | u8 seq[6], key[WPA_TK_MAX_LEN]; |
| 2172 | size_t key_len; |
| 2173 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2174 | /* parameters: alg addr idx set_tx seq key key_flag */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2175 | |
| 2176 | alg = atoi(pos); |
| 2177 | pos = os_strchr(pos, ' '); |
| 2178 | if (!pos) |
| 2179 | return -1; |
| 2180 | pos++; |
| 2181 | if (hwaddr_aton(pos, addr)) |
| 2182 | return -1; |
| 2183 | pos += 17; |
| 2184 | if (*pos != ' ') |
| 2185 | return -1; |
| 2186 | pos++; |
| 2187 | idx = atoi(pos); |
| 2188 | pos = os_strchr(pos, ' '); |
| 2189 | if (!pos) |
| 2190 | return -1; |
| 2191 | pos++; |
| 2192 | set_tx = atoi(pos); |
| 2193 | pos = os_strchr(pos, ' '); |
| 2194 | if (!pos) |
| 2195 | return -1; |
| 2196 | pos++; |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 2197 | if (hexstr2bin(pos, seq, sizeof(seq)) < 0) |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2198 | return -1; |
| 2199 | pos += 2 * 6; |
| 2200 | if (*pos != ' ') |
| 2201 | return -1; |
| 2202 | pos++; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2203 | if (!os_strchr(pos, ' ')) |
| 2204 | return -1; |
| 2205 | key_len = (os_strchr(pos, ' ') - pos) / 2; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2206 | if (hexstr2bin(pos, key, key_len) < 0) |
| 2207 | return -1; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2208 | pos += 2 * key_len; |
| 2209 | if (*pos != ' ') |
| 2210 | return -1; |
| 2211 | |
| 2212 | pos++; |
| 2213 | key_flag = atoi(pos); |
| 2214 | pos = os_strchr(pos, ' '); |
| 2215 | if (pos) |
| 2216 | return -1; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2217 | |
| 2218 | wpa_printf(MSG_INFO, "TESTING: Set key"); |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2219 | return hostapd_drv_set_key(hapd->conf->iface, hapd, alg, addr, idx, 0, |
| 2220 | set_tx, seq, 6, key, key_len, key_flag); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | |
| 2224 | static void restore_tk(void *ctx1, void *ctx2) |
| 2225 | { |
| 2226 | struct hostapd_data *hapd = ctx1; |
| 2227 | struct sta_info *sta = ctx2; |
| 2228 | |
| 2229 | wpa_printf(MSG_INFO, "TESTING: Restore TK for " MACSTR, |
| 2230 | MAC2STR(sta->addr)); |
| 2231 | /* This does not really restore the TSC properly, so this will result |
| 2232 | * in replay protection issues for now since there is no clean way of |
| 2233 | * preventing encryption of a single EAPOL frame. */ |
| 2234 | hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg, |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2235 | sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0, |
| 2236 | sta->last_tk, sta->last_tk_len, |
| 2237 | KEY_FLAG_PAIRWISE_RX_TX); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2238 | } |
| 2239 | |
| 2240 | |
| 2241 | static int hostapd_ctrl_resend_m1(struct hostapd_data *hapd, const char *cmd) |
| 2242 | { |
| 2243 | struct sta_info *sta; |
| 2244 | u8 addr[ETH_ALEN]; |
| 2245 | int plain = os_strstr(cmd, "plaintext") != NULL; |
| 2246 | |
| 2247 | if (hwaddr_aton(cmd, addr)) |
| 2248 | return -1; |
| 2249 | |
| 2250 | sta = ap_get_sta(hapd, addr); |
| 2251 | if (!sta || !sta->wpa_sm) |
| 2252 | return -1; |
| 2253 | |
| 2254 | if (plain && sta->last_tk_alg == WPA_ALG_NONE) |
| 2255 | plain = 0; /* no need for special processing */ |
| 2256 | if (plain) { |
| 2257 | wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR, |
| 2258 | MAC2STR(sta->addr)); |
| 2259 | hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE, |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2260 | sta->addr, sta->last_tk_key_idx, 0, 0, NULL, |
| 2261 | 0, NULL, 0, KEY_FLAG_PAIRWISE); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2262 | } |
| 2263 | |
| 2264 | wpa_printf(MSG_INFO, "TESTING: Send M1 to " MACSTR, MAC2STR(sta->addr)); |
| 2265 | return wpa_auth_resend_m1(sta->wpa_sm, |
| 2266 | os_strstr(cmd, "change-anonce") != NULL, |
| 2267 | plain ? restore_tk : NULL, hapd, sta); |
| 2268 | } |
| 2269 | |
| 2270 | |
| 2271 | static int hostapd_ctrl_resend_m3(struct hostapd_data *hapd, const char *cmd) |
| 2272 | { |
| 2273 | struct sta_info *sta; |
| 2274 | u8 addr[ETH_ALEN]; |
| 2275 | int plain = os_strstr(cmd, "plaintext") != NULL; |
| 2276 | |
| 2277 | if (hwaddr_aton(cmd, addr)) |
| 2278 | return -1; |
| 2279 | |
| 2280 | sta = ap_get_sta(hapd, addr); |
| 2281 | if (!sta || !sta->wpa_sm) |
| 2282 | return -1; |
| 2283 | |
| 2284 | if (plain && sta->last_tk_alg == WPA_ALG_NONE) |
| 2285 | plain = 0; /* no need for special processing */ |
| 2286 | if (plain) { |
| 2287 | wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR, |
| 2288 | MAC2STR(sta->addr)); |
| 2289 | hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE, |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2290 | sta->addr, sta->last_tk_key_idx, 0, 0, NULL, |
| 2291 | 0, NULL, 0, KEY_FLAG_PAIRWISE); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2292 | } |
| 2293 | |
| 2294 | wpa_printf(MSG_INFO, "TESTING: Send M3 to " MACSTR, MAC2STR(sta->addr)); |
| 2295 | return wpa_auth_resend_m3(sta->wpa_sm, |
| 2296 | plain ? restore_tk : NULL, hapd, sta); |
| 2297 | } |
| 2298 | |
| 2299 | |
| 2300 | static int hostapd_ctrl_resend_group_m1(struct hostapd_data *hapd, |
| 2301 | const char *cmd) |
| 2302 | { |
| 2303 | struct sta_info *sta; |
| 2304 | u8 addr[ETH_ALEN]; |
| 2305 | int plain = os_strstr(cmd, "plaintext") != NULL; |
| 2306 | |
| 2307 | if (hwaddr_aton(cmd, addr)) |
| 2308 | return -1; |
| 2309 | |
| 2310 | sta = ap_get_sta(hapd, addr); |
| 2311 | if (!sta || !sta->wpa_sm) |
| 2312 | return -1; |
| 2313 | |
| 2314 | if (plain && sta->last_tk_alg == WPA_ALG_NONE) |
| 2315 | plain = 0; /* no need for special processing */ |
| 2316 | if (plain) { |
| 2317 | wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR, |
| 2318 | MAC2STR(sta->addr)); |
| 2319 | hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE, |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2320 | sta->addr, sta->last_tk_key_idx, 0, 0, NULL, |
| 2321 | 0, NULL, 0, KEY_FLAG_PAIRWISE); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2322 | } |
| 2323 | |
| 2324 | wpa_printf(MSG_INFO, |
| 2325 | "TESTING: Send group M1 for the same GTK and zero RSC to " |
| 2326 | MACSTR, MAC2STR(sta->addr)); |
| 2327 | return wpa_auth_resend_group_m1(sta->wpa_sm, |
| 2328 | plain ? restore_tk : NULL, hapd, sta); |
| 2329 | } |
| 2330 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2331 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2332 | static int hostapd_ctrl_rekey_ptk(struct hostapd_data *hapd, const char *cmd) |
| 2333 | { |
| 2334 | struct sta_info *sta; |
| 2335 | u8 addr[ETH_ALEN]; |
| 2336 | |
| 2337 | if (hwaddr_aton(cmd, addr)) |
| 2338 | return -1; |
| 2339 | |
| 2340 | sta = ap_get_sta(hapd, addr); |
| 2341 | if (!sta || !sta->wpa_sm) |
| 2342 | return -1; |
| 2343 | |
| 2344 | return wpa_auth_rekey_ptk(hapd->wpa_auth, sta->wpa_sm); |
| 2345 | } |
| 2346 | |
| 2347 | |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 2348 | static int hostapd_ctrl_get_pmksa_pmk(struct hostapd_data *hapd, const u8 *addr, |
| 2349 | char *buf, size_t buflen) |
| 2350 | { |
| 2351 | struct rsn_pmksa_cache_entry *pmksa; |
| 2352 | |
| 2353 | pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, addr, NULL); |
| 2354 | if (!pmksa) |
| 2355 | return -1; |
| 2356 | |
| 2357 | return wpa_snprintf_hex(buf, buflen, pmksa->pmk, pmksa->pmk_len); |
| 2358 | } |
| 2359 | |
| 2360 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2361 | static int hostapd_ctrl_get_pmk(struct hostapd_data *hapd, const char *cmd, |
| 2362 | char *buf, size_t buflen) |
| 2363 | { |
| 2364 | struct sta_info *sta; |
| 2365 | u8 addr[ETH_ALEN]; |
| 2366 | const u8 *pmk; |
| 2367 | int pmk_len; |
| 2368 | |
| 2369 | if (hwaddr_aton(cmd, addr)) |
| 2370 | return -1; |
| 2371 | |
| 2372 | sta = ap_get_sta(hapd, addr); |
| 2373 | if (!sta || !sta->wpa_sm) { |
| 2374 | wpa_printf(MSG_DEBUG, "No STA WPA state machine for " MACSTR, |
| 2375 | MAC2STR(addr)); |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 2376 | return hostapd_ctrl_get_pmksa_pmk(hapd, addr, buf, buflen); |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2377 | } |
| 2378 | pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len); |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 2379 | if (!pmk || !pmk_len) { |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2380 | wpa_printf(MSG_DEBUG, "No PMK stored for " MACSTR, |
| 2381 | MAC2STR(addr)); |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 2382 | return hostapd_ctrl_get_pmksa_pmk(hapd, addr, buf, buflen); |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2383 | } |
| 2384 | |
| 2385 | return wpa_snprintf_hex(buf, buflen, pmk, pmk_len); |
| 2386 | } |
| 2387 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2388 | |
| 2389 | static int hostapd_ctrl_register_frame(struct hostapd_data *hapd, |
| 2390 | const char *cmd) |
| 2391 | { |
| 2392 | u16 type; |
| 2393 | char *pos, *end; |
| 2394 | u8 match[10]; |
| 2395 | size_t match_len; |
| 2396 | bool multicast = false; |
| 2397 | |
| 2398 | type = strtol(cmd, &pos, 16); |
| 2399 | if (*pos != ' ') |
| 2400 | return -1; |
| 2401 | pos++; |
| 2402 | end = os_strchr(pos, ' '); |
| 2403 | if (end) { |
| 2404 | match_len = end - pos; |
| 2405 | multicast = os_strstr(end, "multicast") != NULL; |
| 2406 | } else { |
| 2407 | match_len = os_strlen(pos) / 2; |
| 2408 | } |
| 2409 | if (hexstr2bin(pos, match, match_len)) |
| 2410 | return -1; |
| 2411 | |
| 2412 | return hostapd_drv_register_frame(hapd, type, match, match_len, |
| 2413 | multicast); |
| 2414 | } |
| 2415 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 2416 | #endif /* CONFIG_TESTING_OPTIONS */ |
| 2417 | |
| 2418 | |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 2419 | #ifdef NEED_AP_MLME |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2420 | static int hostapd_ctrl_check_freq_params(struct hostapd_freq_params *params) |
| 2421 | { |
| 2422 | switch (params->bandwidth) { |
| 2423 | case 0: |
| 2424 | /* bandwidth not specified: use 20 MHz by default */ |
| 2425 | /* fall-through */ |
| 2426 | case 20: |
| 2427 | if (params->center_freq1 && |
| 2428 | params->center_freq1 != params->freq) |
| 2429 | return -1; |
| 2430 | |
| 2431 | if (params->center_freq2 || params->sec_channel_offset) |
| 2432 | return -1; |
| 2433 | break; |
| 2434 | case 40: |
| 2435 | if (params->center_freq2 || !params->sec_channel_offset) |
| 2436 | return -1; |
| 2437 | |
| 2438 | if (!params->center_freq1) |
| 2439 | break; |
| 2440 | switch (params->sec_channel_offset) { |
| 2441 | case 1: |
| 2442 | if (params->freq + 10 != params->center_freq1) |
| 2443 | return -1; |
| 2444 | break; |
| 2445 | case -1: |
| 2446 | if (params->freq - 10 != params->center_freq1) |
| 2447 | return -1; |
| 2448 | break; |
| 2449 | default: |
| 2450 | return -1; |
| 2451 | } |
| 2452 | break; |
| 2453 | case 80: |
| 2454 | if (!params->center_freq1 || !params->sec_channel_offset) |
| 2455 | return 1; |
| 2456 | |
| 2457 | switch (params->sec_channel_offset) { |
| 2458 | case 1: |
| 2459 | if (params->freq - 10 != params->center_freq1 && |
| 2460 | params->freq + 30 != params->center_freq1) |
| 2461 | return 1; |
| 2462 | break; |
| 2463 | case -1: |
| 2464 | if (params->freq + 10 != params->center_freq1 && |
| 2465 | params->freq - 30 != params->center_freq1) |
| 2466 | return -1; |
| 2467 | break; |
| 2468 | default: |
| 2469 | return -1; |
| 2470 | } |
| 2471 | |
| 2472 | /* Adjacent and overlapped are not allowed for 80+80 */ |
| 2473 | if (params->center_freq2 && |
| 2474 | params->center_freq1 - params->center_freq2 <= 80 && |
| 2475 | params->center_freq2 - params->center_freq1 <= 80) |
| 2476 | return 1; |
| 2477 | break; |
| 2478 | case 160: |
| 2479 | if (!params->center_freq1 || params->center_freq2 || |
| 2480 | !params->sec_channel_offset) |
| 2481 | return -1; |
| 2482 | |
| 2483 | switch (params->sec_channel_offset) { |
| 2484 | case 1: |
| 2485 | if (params->freq + 70 != params->center_freq1 && |
| 2486 | params->freq + 30 != params->center_freq1 && |
| 2487 | params->freq - 10 != params->center_freq1 && |
| 2488 | params->freq - 50 != params->center_freq1) |
| 2489 | return -1; |
| 2490 | break; |
| 2491 | case -1: |
| 2492 | if (params->freq + 50 != params->center_freq1 && |
| 2493 | params->freq + 10 != params->center_freq1 && |
| 2494 | params->freq - 30 != params->center_freq1 && |
| 2495 | params->freq - 70 != params->center_freq1) |
| 2496 | return -1; |
| 2497 | break; |
| 2498 | default: |
| 2499 | return -1; |
| 2500 | } |
| 2501 | break; |
| 2502 | default: |
| 2503 | return -1; |
| 2504 | } |
| 2505 | |
| 2506 | return 0; |
| 2507 | } |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 2508 | #endif /* NEED_AP_MLME */ |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2509 | |
| 2510 | |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 2511 | static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface, |
| 2512 | char *pos) |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 2513 | { |
| 2514 | #ifdef NEED_AP_MLME |
| 2515 | struct csa_settings settings; |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 2516 | int ret; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2517 | int dfs_range = 0; |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 2518 | unsigned int i; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2519 | int bandwidth; |
| 2520 | u8 chan; |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 2521 | |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 2522 | ret = hostapd_parse_csa_settings(pos, &settings); |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 2523 | if (ret) |
| 2524 | return ret; |
| 2525 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2526 | ret = hostapd_ctrl_check_freq_params(&settings.freq_params); |
| 2527 | if (ret) { |
| 2528 | wpa_printf(MSG_INFO, |
| 2529 | "chanswitch: invalid frequency settings provided"); |
| 2530 | return ret; |
| 2531 | } |
| 2532 | |
| 2533 | switch (settings.freq_params.bandwidth) { |
| 2534 | case 40: |
| 2535 | bandwidth = CHAN_WIDTH_40; |
| 2536 | break; |
| 2537 | case 80: |
| 2538 | if (settings.freq_params.center_freq2) |
| 2539 | bandwidth = CHAN_WIDTH_80P80; |
| 2540 | else |
| 2541 | bandwidth = CHAN_WIDTH_80; |
| 2542 | break; |
| 2543 | case 160: |
| 2544 | bandwidth = CHAN_WIDTH_160; |
| 2545 | break; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 2546 | case 320: |
| 2547 | bandwidth = CHAN_WIDTH_320; |
| 2548 | break; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2549 | default: |
| 2550 | bandwidth = CHAN_WIDTH_20; |
| 2551 | break; |
| 2552 | } |
| 2553 | |
| 2554 | if (settings.freq_params.center_freq1) |
| 2555 | dfs_range += hostapd_is_dfs_overlap( |
| 2556 | iface, bandwidth, settings.freq_params.center_freq1); |
| 2557 | else |
| 2558 | dfs_range += hostapd_is_dfs_overlap( |
| 2559 | iface, bandwidth, settings.freq_params.freq); |
| 2560 | |
| 2561 | if (settings.freq_params.center_freq2) |
| 2562 | dfs_range += hostapd_is_dfs_overlap( |
| 2563 | iface, bandwidth, settings.freq_params.center_freq2); |
| 2564 | |
| 2565 | if (dfs_range) { |
| 2566 | ret = ieee80211_freq_to_chan(settings.freq_params.freq, &chan); |
| 2567 | if (ret == NUM_HOSTAPD_MODES) { |
| 2568 | wpa_printf(MSG_ERROR, |
| 2569 | "Failed to get channel for (freq=%d, sec_channel_offset=%d, bw=%d)", |
| 2570 | settings.freq_params.freq, |
| 2571 | settings.freq_params.sec_channel_offset, |
| 2572 | settings.freq_params.bandwidth); |
| 2573 | return -1; |
| 2574 | } |
| 2575 | |
| 2576 | settings.freq_params.channel = chan; |
| 2577 | |
| 2578 | wpa_printf(MSG_DEBUG, |
| 2579 | "DFS/CAC to (channel=%u, freq=%d, sec_channel_offset=%d, bw=%d, center_freq1=%d)", |
| 2580 | settings.freq_params.channel, |
| 2581 | settings.freq_params.freq, |
| 2582 | settings.freq_params.sec_channel_offset, |
| 2583 | settings.freq_params.bandwidth, |
| 2584 | settings.freq_params.center_freq1); |
| 2585 | |
| 2586 | /* Perform CAC and switch channel */ |
| 2587 | hostapd_switch_channel_fallback(iface, &settings.freq_params); |
| 2588 | return 0; |
| 2589 | } |
| 2590 | |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 2591 | for (i = 0; i < iface->num_bss; i++) { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2592 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 2593 | /* Save CHAN_SWITCH VHT, HE, and EHT config */ |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 2594 | hostapd_chan_switch_config(iface->bss[i], |
| 2595 | &settings.freq_params); |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2596 | |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 2597 | ret = hostapd_switch_channel(iface->bss[i], &settings); |
| 2598 | if (ret) { |
| 2599 | /* FIX: What do we do if CSA fails in the middle of |
| 2600 | * submitting multi-BSS CSA requests? */ |
| 2601 | return ret; |
| 2602 | } |
| 2603 | } |
| 2604 | |
| 2605 | return 0; |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 2606 | #else /* NEED_AP_MLME */ |
| 2607 | return -1; |
| 2608 | #endif /* NEED_AP_MLME */ |
| 2609 | } |
| 2610 | |
| 2611 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2612 | static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply, |
| 2613 | int reply_size, const char *param) |
| 2614 | { |
| 2615 | #ifdef RADIUS_SERVER |
| 2616 | if (os_strcmp(param, "radius_server") == 0) { |
| 2617 | return radius_server_get_mib(hapd->radius_srv, reply, |
| 2618 | reply_size); |
| 2619 | } |
| 2620 | #endif /* RADIUS_SERVER */ |
| 2621 | return -1; |
| 2622 | } |
| 2623 | |
| 2624 | |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 2625 | static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd, |
| 2626 | char *buf, size_t buflen) |
| 2627 | { |
| 2628 | int ret; |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 2629 | char *pos, *temp = NULL; |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 2630 | u8 *data = NULL; |
| 2631 | unsigned int vendor_id, subcmd; |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 2632 | enum nested_attr nested_attr_flag = NESTED_ATTR_UNSPECIFIED; |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 2633 | struct wpabuf *reply; |
| 2634 | size_t data_len = 0; |
| 2635 | |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 2636 | /** |
| 2637 | * cmd: <vendor id> <subcommand id> [<hex formatted data>] |
| 2638 | * [nested=<0|1>] |
| 2639 | */ |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 2640 | vendor_id = strtoul(cmd, &pos, 16); |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 2641 | if (!isblank((unsigned char) *pos)) |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 2642 | return -EINVAL; |
| 2643 | |
| 2644 | subcmd = strtoul(pos, &pos, 10); |
| 2645 | |
| 2646 | if (*pos != '\0') { |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 2647 | if (!isblank((unsigned char) *pos++)) |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 2648 | return -EINVAL; |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 2649 | |
| 2650 | temp = os_strchr(pos, ' '); |
| 2651 | data_len = temp ? (size_t) (temp - pos) : os_strlen(pos); |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | if (data_len) { |
| 2655 | data_len /= 2; |
| 2656 | data = os_malloc(data_len); |
| 2657 | if (!data) |
| 2658 | return -ENOBUFS; |
| 2659 | |
| 2660 | if (hexstr2bin(pos, data, data_len)) { |
| 2661 | wpa_printf(MSG_DEBUG, |
| 2662 | "Vendor command: wrong parameter format"); |
| 2663 | os_free(data); |
| 2664 | return -EINVAL; |
| 2665 | } |
| 2666 | } |
| 2667 | |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 2668 | pos = os_strstr(cmd, "nested="); |
| 2669 | if (pos) |
| 2670 | nested_attr_flag = atoi(pos + 7) ? NESTED_ATTR_USED : |
| 2671 | NESTED_ATTR_NOT_USED; |
| 2672 | |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 2673 | reply = wpabuf_alloc((buflen - 1) / 2); |
| 2674 | if (!reply) { |
| 2675 | os_free(data); |
| 2676 | return -ENOBUFS; |
| 2677 | } |
| 2678 | |
| 2679 | ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len, |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 2680 | nested_attr_flag, reply); |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 2681 | |
| 2682 | if (ret == 0) |
| 2683 | ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply), |
| 2684 | wpabuf_len(reply)); |
| 2685 | |
| 2686 | wpabuf_free(reply); |
| 2687 | os_free(data); |
| 2688 | |
| 2689 | return ret; |
| 2690 | } |
| 2691 | |
| 2692 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2693 | static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd, |
| 2694 | const char *cmd) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2695 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2696 | u8 addr[ETH_ALEN]; |
| 2697 | struct sta_info *sta; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2698 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2699 | if (hwaddr_aton(cmd, addr)) |
| 2700 | return -1; |
| 2701 | |
| 2702 | sta = ap_get_sta(hapd, addr); |
| 2703 | if (!sta || !sta->eapol_sm) |
| 2704 | return -1; |
| 2705 | |
| 2706 | eapol_auth_reauthenticate(sta->eapol_sm); |
| 2707 | return 0; |
| 2708 | } |
| 2709 | |
| 2710 | |
| 2711 | static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd) |
| 2712 | { |
| 2713 | u8 addr[ETH_ALEN]; |
| 2714 | struct sta_info *sta; |
| 2715 | char *pos = cmd, *param; |
| 2716 | |
| 2717 | if (hwaddr_aton(pos, addr) || pos[17] != ' ') |
| 2718 | return -1; |
| 2719 | pos += 18; |
| 2720 | param = pos; |
| 2721 | pos = os_strchr(pos, ' '); |
| 2722 | if (!pos) |
| 2723 | return -1; |
| 2724 | *pos++ = '\0'; |
| 2725 | |
| 2726 | sta = ap_get_sta(hapd, addr); |
| 2727 | if (!sta || !sta->eapol_sm) |
| 2728 | return -1; |
| 2729 | |
| 2730 | return eapol_auth_set_conf(sta->eapol_sm, param, pos); |
| 2731 | } |
| 2732 | |
| 2733 | |
| 2734 | static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd, |
| 2735 | char *buf, size_t buflen) |
| 2736 | { |
| 2737 | char *pos, *end, *stamp; |
| 2738 | int ret; |
| 2739 | |
| 2740 | /* cmd: "LOG_LEVEL [<level>]" */ |
| 2741 | if (*cmd == '\0') { |
| 2742 | pos = buf; |
| 2743 | end = buf + buflen; |
| 2744 | ret = os_snprintf(pos, end - pos, "Current level: %s\n" |
| 2745 | "Timestamp: %d\n", |
| 2746 | debug_level_str(wpa_debug_level), |
| 2747 | wpa_debug_timestamp); |
| 2748 | if (os_snprintf_error(end - pos, ret)) |
| 2749 | ret = 0; |
| 2750 | |
| 2751 | return ret; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2752 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2753 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2754 | while (*cmd == ' ') |
| 2755 | cmd++; |
| 2756 | |
| 2757 | stamp = os_strchr(cmd, ' '); |
| 2758 | if (stamp) { |
| 2759 | *stamp++ = '\0'; |
| 2760 | while (*stamp == ' ') { |
| 2761 | stamp++; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2762 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2763 | } |
| 2764 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2765 | if (os_strlen(cmd)) { |
| 2766 | int level = str_to_debug_level(cmd); |
| 2767 | if (level < 0) |
| 2768 | return -1; |
| 2769 | wpa_debug_level = level; |
| 2770 | } |
| 2771 | |
| 2772 | if (stamp && os_strlen(stamp)) |
| 2773 | wpa_debug_timestamp = atoi(stamp); |
| 2774 | |
| 2775 | os_memcpy(buf, "OK\n", 3); |
| 2776 | return 3; |
| 2777 | } |
| 2778 | |
| 2779 | |
| 2780 | #ifdef NEED_AP_MLME |
| 2781 | static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd, |
| 2782 | char *buf, size_t buflen) |
| 2783 | { |
| 2784 | struct hostapd_iface *iface = hapd->iface; |
| 2785 | char *pos, *end; |
| 2786 | struct hostapd_sta_info *info; |
| 2787 | struct os_reltime now; |
| 2788 | |
Dmitry Shmidt | 7d17530 | 2016-09-06 13:11:34 -0700 | [diff] [blame] | 2789 | if (!iface->num_sta_seen) |
| 2790 | return 0; |
| 2791 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2792 | sta_track_expire(iface, 0); |
| 2793 | |
| 2794 | pos = buf; |
| 2795 | end = buf + buflen; |
| 2796 | |
| 2797 | os_get_reltime(&now); |
| 2798 | dl_list_for_each_reverse(info, &iface->sta_seen, |
| 2799 | struct hostapd_sta_info, list) { |
| 2800 | struct os_reltime age; |
| 2801 | int ret; |
| 2802 | |
| 2803 | os_reltime_sub(&now, &info->last_seen, &age); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2804 | ret = os_snprintf(pos, end - pos, MACSTR " %u %d\n", |
| 2805 | MAC2STR(info->addr), (unsigned int) age.sec, |
| 2806 | info->ssi_signal); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2807 | if (os_snprintf_error(end - pos, ret)) |
| 2808 | break; |
| 2809 | pos += ret; |
| 2810 | } |
| 2811 | |
| 2812 | return pos - buf; |
| 2813 | } |
| 2814 | #endif /* NEED_AP_MLME */ |
| 2815 | |
| 2816 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 2817 | static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd, |
| 2818 | const char *cmd) |
| 2819 | { |
| 2820 | u8 addr[ETH_ALEN]; |
| 2821 | |
| 2822 | if (hwaddr_aton(cmd, addr)) { |
| 2823 | wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address"); |
| 2824 | return -1; |
| 2825 | } |
| 2826 | |
| 2827 | return hostapd_send_lci_req(hapd, addr); |
| 2828 | } |
| 2829 | |
| 2830 | |
Dmitry Shmidt | 4ae50e6 | 2016-06-27 13:48:39 -0700 | [diff] [blame] | 2831 | static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd) |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 2832 | { |
| 2833 | u8 addr[ETH_ALEN]; |
| 2834 | char *token, *context = NULL; |
| 2835 | int random_interval, min_ap; |
| 2836 | u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS]; |
| 2837 | unsigned int n_responders; |
| 2838 | |
| 2839 | token = str_token(cmd, " ", &context); |
| 2840 | if (!token || hwaddr_aton(token, addr)) { |
| 2841 | wpa_printf(MSG_INFO, |
| 2842 | "CTRL: REQ_RANGE - Bad destination address"); |
| 2843 | return -1; |
| 2844 | } |
| 2845 | |
| 2846 | token = str_token(cmd, " ", &context); |
| 2847 | if (!token) |
| 2848 | return -1; |
| 2849 | |
| 2850 | random_interval = atoi(token); |
| 2851 | if (random_interval < 0 || random_interval > 0xffff) |
| 2852 | return -1; |
| 2853 | |
| 2854 | token = str_token(cmd, " ", &context); |
| 2855 | if (!token) |
| 2856 | return -1; |
| 2857 | |
| 2858 | min_ap = atoi(token); |
| 2859 | if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP) |
| 2860 | return -1; |
| 2861 | |
| 2862 | n_responders = 0; |
| 2863 | while ((token = str_token(cmd, " ", &context))) { |
| 2864 | if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) { |
| 2865 | wpa_printf(MSG_INFO, |
| 2866 | "CTRL: REQ_RANGE: Too many responders"); |
| 2867 | return -1; |
| 2868 | } |
| 2869 | |
| 2870 | if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) { |
| 2871 | wpa_printf(MSG_INFO, |
| 2872 | "CTRL: REQ_RANGE: Bad responder address"); |
| 2873 | return -1; |
| 2874 | } |
| 2875 | |
| 2876 | n_responders++; |
| 2877 | } |
| 2878 | |
| 2879 | if (!n_responders) { |
| 2880 | wpa_printf(MSG_INFO, |
| 2881 | "CTRL: REQ_RANGE - No FTM responder address"); |
| 2882 | return -1; |
| 2883 | } |
| 2884 | |
| 2885 | return hostapd_send_range_req(hapd, addr, random_interval, min_ap, |
| 2886 | responders, n_responders); |
| 2887 | } |
| 2888 | |
| 2889 | |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 2890 | static int hostapd_ctrl_iface_req_beacon(struct hostapd_data *hapd, |
| 2891 | const char *cmd, char *reply, |
| 2892 | size_t reply_size) |
| 2893 | { |
| 2894 | u8 addr[ETH_ALEN]; |
| 2895 | const char *pos; |
| 2896 | struct wpabuf *req; |
| 2897 | int ret; |
| 2898 | u8 req_mode = 0; |
| 2899 | |
| 2900 | if (hwaddr_aton(cmd, addr)) |
| 2901 | return -1; |
| 2902 | pos = os_strchr(cmd, ' '); |
| 2903 | if (!pos) |
| 2904 | return -1; |
| 2905 | pos++; |
| 2906 | if (os_strncmp(pos, "req_mode=", 9) == 0) { |
| 2907 | int val = hex2byte(pos + 9); |
| 2908 | |
| 2909 | if (val < 0) |
| 2910 | return -1; |
| 2911 | req_mode = val; |
| 2912 | pos += 11; |
| 2913 | pos = os_strchr(pos, ' '); |
| 2914 | if (!pos) |
| 2915 | return -1; |
| 2916 | pos++; |
| 2917 | } |
| 2918 | req = wpabuf_parse_bin(pos); |
| 2919 | if (!req) |
| 2920 | return -1; |
| 2921 | |
| 2922 | ret = hostapd_send_beacon_req(hapd, addr, req_mode, req); |
| 2923 | wpabuf_free(req); |
| 2924 | if (ret >= 0) |
| 2925 | ret = os_snprintf(reply, reply_size, "%d", ret); |
| 2926 | return ret; |
| 2927 | } |
| 2928 | |
| 2929 | |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 2930 | static int hostapd_ctrl_iface_show_neighbor(struct hostapd_data *hapd, |
| 2931 | char *buf, size_t buflen) |
| 2932 | { |
| 2933 | if (!(hapd->conf->radio_measurements[0] & |
| 2934 | WLAN_RRM_CAPS_NEIGHBOR_REPORT)) { |
| 2935 | wpa_printf(MSG_ERROR, |
| 2936 | "CTRL: SHOW_NEIGHBOR: Neighbor report is not enabled"); |
| 2937 | return -1; |
| 2938 | } |
| 2939 | |
| 2940 | return hostapd_neighbor_show(hapd, buf, buflen); |
| 2941 | } |
| 2942 | |
| 2943 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 2944 | static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf) |
| 2945 | { |
| 2946 | struct wpa_ssid_value ssid; |
| 2947 | u8 bssid[ETH_ALEN]; |
| 2948 | struct wpabuf *nr, *lci = NULL, *civic = NULL; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2949 | int stationary = 0; |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2950 | int bss_parameters = 0; |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 2951 | char *tmp; |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2952 | int ret = -1; |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 2953 | |
| 2954 | if (!(hapd->conf->radio_measurements[0] & |
| 2955 | WLAN_RRM_CAPS_NEIGHBOR_REPORT)) { |
| 2956 | wpa_printf(MSG_ERROR, |
| 2957 | "CTRL: SET_NEIGHBOR: Neighbor report is not enabled"); |
| 2958 | return -1; |
| 2959 | } |
| 2960 | |
| 2961 | if (hwaddr_aton(buf, bssid)) { |
| 2962 | wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID"); |
| 2963 | return -1; |
| 2964 | } |
| 2965 | |
| 2966 | tmp = os_strstr(buf, "ssid="); |
| 2967 | if (!tmp || ssid_parse(tmp + 5, &ssid)) { |
| 2968 | wpa_printf(MSG_ERROR, |
| 2969 | "CTRL: SET_NEIGHBOR: Bad or missing SSID"); |
| 2970 | return -1; |
| 2971 | } |
| 2972 | buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' '); |
| 2973 | if (!buf) |
| 2974 | return -1; |
| 2975 | |
| 2976 | tmp = os_strstr(buf, "nr="); |
| 2977 | if (!tmp) { |
| 2978 | wpa_printf(MSG_ERROR, |
| 2979 | "CTRL: SET_NEIGHBOR: Missing Neighbor Report element"); |
| 2980 | return -1; |
| 2981 | } |
| 2982 | |
| 2983 | buf = os_strchr(tmp, ' '); |
| 2984 | if (buf) |
| 2985 | *buf++ = '\0'; |
| 2986 | |
| 2987 | nr = wpabuf_parse_bin(tmp + 3); |
| 2988 | if (!nr) { |
| 2989 | wpa_printf(MSG_ERROR, |
| 2990 | "CTRL: SET_NEIGHBOR: Bad Neighbor Report element"); |
| 2991 | return -1; |
| 2992 | } |
| 2993 | |
| 2994 | if (!buf) |
| 2995 | goto set; |
| 2996 | |
| 2997 | tmp = os_strstr(buf, "lci="); |
| 2998 | if (tmp) { |
| 2999 | buf = os_strchr(tmp, ' '); |
| 3000 | if (buf) |
| 3001 | *buf++ = '\0'; |
| 3002 | lci = wpabuf_parse_bin(tmp + 4); |
| 3003 | if (!lci) { |
| 3004 | wpa_printf(MSG_ERROR, |
| 3005 | "CTRL: SET_NEIGHBOR: Bad LCI subelement"); |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3006 | goto fail; |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 3007 | } |
| 3008 | } |
| 3009 | |
| 3010 | if (!buf) |
| 3011 | goto set; |
| 3012 | |
| 3013 | tmp = os_strstr(buf, "civic="); |
| 3014 | if (tmp) { |
| 3015 | buf = os_strchr(tmp, ' '); |
| 3016 | if (buf) |
| 3017 | *buf++ = '\0'; |
| 3018 | civic = wpabuf_parse_bin(tmp + 6); |
| 3019 | if (!civic) { |
| 3020 | wpa_printf(MSG_ERROR, |
| 3021 | "CTRL: SET_NEIGHBOR: Bad civic subelement"); |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3022 | goto fail; |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 3023 | } |
| 3024 | } |
| 3025 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 3026 | if (!buf) |
| 3027 | goto set; |
| 3028 | |
| 3029 | if (os_strstr(buf, "stat")) |
| 3030 | stationary = 1; |
| 3031 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3032 | tmp = os_strstr(buf, "bss_parameter="); |
| 3033 | if (tmp) { |
| 3034 | bss_parameters = atoi(tmp + 14); |
| 3035 | if (bss_parameters < 0 || bss_parameters > 0xff) { |
| 3036 | wpa_printf(MSG_ERROR, |
| 3037 | "CTRL: SET_NEIGHBOR: Bad bss_parameters subelement"); |
| 3038 | goto fail; |
| 3039 | } |
| 3040 | } |
| 3041 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 3042 | set: |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 3043 | ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic, |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3044 | stationary, bss_parameters); |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 3045 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3046 | fail: |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 3047 | wpabuf_free(nr); |
| 3048 | wpabuf_free(lci); |
| 3049 | wpabuf_free(civic); |
| 3050 | |
| 3051 | return ret; |
| 3052 | } |
| 3053 | |
| 3054 | |
| 3055 | static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd, |
| 3056 | char *buf) |
| 3057 | { |
| 3058 | struct wpa_ssid_value ssid; |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 3059 | struct wpa_ssid_value *ssidp = NULL; |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 3060 | u8 bssid[ETH_ALEN]; |
| 3061 | char *tmp; |
| 3062 | |
| 3063 | if (hwaddr_aton(buf, bssid)) { |
| 3064 | wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID"); |
| 3065 | return -1; |
| 3066 | } |
| 3067 | |
| 3068 | tmp = os_strstr(buf, "ssid="); |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 3069 | if (tmp) { |
| 3070 | ssidp = &ssid; |
| 3071 | if (ssid_parse(tmp + 5, &ssid)) { |
| 3072 | wpa_printf(MSG_ERROR, |
| 3073 | "CTRL: REMOVE_NEIGHBOR: Bad SSID"); |
| 3074 | return -1; |
| 3075 | } |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 3076 | } |
| 3077 | |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 3078 | return hostapd_neighbor_remove(hapd, bssid, ssidp); |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 3079 | } |
| 3080 | |
| 3081 | |
Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 3082 | static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf, |
| 3083 | size_t buflen) |
| 3084 | { |
| 3085 | int ret, i; |
| 3086 | char *pos, *end; |
| 3087 | |
| 3088 | ret = os_snprintf(buf, buflen, "%016llX:\n", |
| 3089 | (long long unsigned) iface->drv_flags); |
| 3090 | if (os_snprintf_error(buflen, ret)) |
| 3091 | return -1; |
| 3092 | |
| 3093 | pos = buf + ret; |
| 3094 | end = buf + buflen; |
| 3095 | |
| 3096 | for (i = 0; i < 64; i++) { |
| 3097 | if (iface->drv_flags & (1LLU << i)) { |
| 3098 | ret = os_snprintf(pos, end - pos, "%s\n", |
| 3099 | driver_flag_to_string(1LLU << i)); |
| 3100 | if (os_snprintf_error(end - pos, ret)) |
| 3101 | return -1; |
| 3102 | pos += ret; |
| 3103 | } |
| 3104 | } |
| 3105 | |
| 3106 | return pos - buf; |
| 3107 | } |
| 3108 | |
| 3109 | |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 3110 | static int hostapd_ctrl_driver_flags2(struct hostapd_iface *iface, char *buf, |
| 3111 | size_t buflen) |
| 3112 | { |
| 3113 | int ret, i; |
| 3114 | char *pos, *end; |
| 3115 | |
| 3116 | ret = os_snprintf(buf, buflen, "%016llX:\n", |
| 3117 | (long long unsigned) iface->drv_flags2); |
| 3118 | if (os_snprintf_error(buflen, ret)) |
| 3119 | return -1; |
| 3120 | |
| 3121 | pos = buf + ret; |
| 3122 | end = buf + buflen; |
| 3123 | |
| 3124 | for (i = 0; i < 64; i++) { |
| 3125 | if (iface->drv_flags2 & (1LLU << i)) { |
| 3126 | ret = os_snprintf(pos, end - pos, "%s\n", |
| 3127 | driver_flag2_to_string(1LLU << i)); |
| 3128 | if (os_snprintf_error(end - pos, ret)) |
| 3129 | return -1; |
| 3130 | pos += ret; |
| 3131 | } |
| 3132 | } |
| 3133 | |
| 3134 | return pos - buf; |
| 3135 | } |
| 3136 | |
| 3137 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 3138 | static int hostapd_ctrl_iface_get_capability(struct hostapd_data *hapd, |
| 3139 | const char *field, char *buf, |
| 3140 | size_t buflen) |
| 3141 | { |
| 3142 | wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s'", field); |
| 3143 | |
| 3144 | #ifdef CONFIG_DPP |
| 3145 | if (os_strcmp(field, "dpp") == 0) { |
| 3146 | int res; |
| 3147 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3148 | #ifdef CONFIG_DPP3 |
| 3149 | res = os_snprintf(buf, buflen, "DPP=3"); |
| 3150 | #elif defined(CONFIG_DPP2) |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 3151 | res = os_snprintf(buf, buflen, "DPP=2"); |
| 3152 | #else /* CONFIG_DPP2 */ |
| 3153 | res = os_snprintf(buf, buflen, "DPP=1"); |
| 3154 | #endif /* CONFIG_DPP2 */ |
| 3155 | if (os_snprintf_error(buflen, res)) |
| 3156 | return -1; |
| 3157 | return res; |
| 3158 | } |
| 3159 | #endif /* CONFIG_DPP */ |
| 3160 | |
| 3161 | wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'", |
| 3162 | field); |
| 3163 | |
| 3164 | return -1; |
| 3165 | } |
| 3166 | |
| 3167 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3168 | #ifdef ANDROID |
| 3169 | static int hostapd_ctrl_iface_driver_cmd(struct hostapd_data *hapd, char *cmd, |
| 3170 | char *buf, size_t buflen) |
| 3171 | { |
| 3172 | int ret; |
| 3173 | |
| 3174 | ret = hostapd_drv_driver_cmd(hapd, cmd, buf, buflen); |
| 3175 | if (ret == 0) { |
| 3176 | ret = os_snprintf(buf, buflen, "%s\n", "OK"); |
| 3177 | if (os_snprintf_error(buflen, ret)) |
| 3178 | ret = -1; |
| 3179 | } |
| 3180 | return ret; |
| 3181 | } |
| 3182 | #endif /* ANDROID */ |
| 3183 | |
| 3184 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3185 | static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd, |
| 3186 | char *buf, char *reply, |
| 3187 | int reply_size, |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 3188 | struct sockaddr_storage *from, |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3189 | socklen_t fromlen) |
| 3190 | { |
| 3191 | int reply_len, res; |
| 3192 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3193 | os_memcpy(reply, "OK\n", 3); |
| 3194 | reply_len = 3; |
| 3195 | |
| 3196 | if (os_strcmp(buf, "PING") == 0) { |
| 3197 | os_memcpy(reply, "PONG\n", 5); |
| 3198 | reply_len = 5; |
| 3199 | } else if (os_strncmp(buf, "RELOG", 5) == 0) { |
| 3200 | if (wpa_debug_reopen_file() < 0) |
| 3201 | reply_len = -1; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3202 | } else if (os_strncmp(buf, "NOTE ", 5) == 0) { |
| 3203 | wpa_printf(MSG_INFO, "NOTE: %s", buf + 5); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 3204 | } else if (os_strcmp(buf, "STATUS") == 0) { |
| 3205 | reply_len = hostapd_ctrl_iface_status(hapd, reply, |
| 3206 | reply_size); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 3207 | } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) { |
| 3208 | reply_len = hostapd_drv_status(hapd, reply, reply_size); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3209 | } else if (os_strcmp(buf, "MIB") == 0) { |
| 3210 | reply_len = ieee802_11_get_mib(hapd, reply, reply_size); |
| 3211 | if (reply_len >= 0) { |
| 3212 | res = wpa_get_mib(hapd->wpa_auth, reply + reply_len, |
| 3213 | reply_size - reply_len); |
| 3214 | if (res < 0) |
| 3215 | reply_len = -1; |
| 3216 | else |
| 3217 | reply_len += res; |
| 3218 | } |
| 3219 | if (reply_len >= 0) { |
| 3220 | res = ieee802_1x_get_mib(hapd, reply + reply_len, |
| 3221 | reply_size - reply_len); |
| 3222 | if (res < 0) |
| 3223 | reply_len = -1; |
| 3224 | else |
| 3225 | reply_len += res; |
| 3226 | } |
| 3227 | #ifndef CONFIG_NO_RADIUS |
| 3228 | if (reply_len >= 0) { |
| 3229 | res = radius_client_get_mib(hapd->radius, |
| 3230 | reply + reply_len, |
| 3231 | reply_size - reply_len); |
| 3232 | if (res < 0) |
| 3233 | reply_len = -1; |
| 3234 | else |
| 3235 | reply_len += res; |
| 3236 | } |
| 3237 | #endif /* CONFIG_NO_RADIUS */ |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 3238 | } else if (os_strncmp(buf, "MIB ", 4) == 0) { |
| 3239 | reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size, |
| 3240 | buf + 4); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3241 | } else if (os_strcmp(buf, "STA-FIRST") == 0) { |
| 3242 | reply_len = hostapd_ctrl_iface_sta_first(hapd, reply, |
| 3243 | reply_size); |
| 3244 | } else if (os_strncmp(buf, "STA ", 4) == 0) { |
| 3245 | reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply, |
| 3246 | reply_size); |
| 3247 | } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) { |
| 3248 | reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply, |
| 3249 | reply_size); |
| 3250 | } else if (os_strcmp(buf, "ATTACH") == 0) { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3251 | if (hostapd_ctrl_iface_attach(hapd, from, fromlen, NULL)) |
| 3252 | reply_len = -1; |
| 3253 | } else if (os_strncmp(buf, "ATTACH ", 7) == 0) { |
| 3254 | if (hostapd_ctrl_iface_attach(hapd, from, fromlen, buf + 7)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3255 | reply_len = -1; |
| 3256 | } else if (os_strcmp(buf, "DETACH") == 0) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3257 | if (hostapd_ctrl_iface_detach(hapd, from, fromlen)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3258 | reply_len = -1; |
| 3259 | } else if (os_strncmp(buf, "LEVEL ", 6) == 0) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3260 | if (hostapd_ctrl_iface_level(hapd, from, fromlen, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3261 | buf + 6)) |
| 3262 | reply_len = -1; |
| 3263 | } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) { |
| 3264 | if (hostapd_ctrl_iface_new_sta(hapd, buf + 8)) |
| 3265 | reply_len = -1; |
| 3266 | } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) { |
| 3267 | if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15)) |
| 3268 | reply_len = -1; |
| 3269 | } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) { |
| 3270 | if (hostapd_ctrl_iface_disassociate(hapd, buf + 13)) |
| 3271 | reply_len = -1; |
Dmitry Shmidt | aca489e | 2016-09-28 15:44:14 -0700 | [diff] [blame] | 3272 | #ifdef CONFIG_TAXONOMY |
| 3273 | } else if (os_strncmp(buf, "SIGNATURE ", 10) == 0) { |
| 3274 | reply_len = hostapd_ctrl_iface_signature(hapd, buf + 10, |
| 3275 | reply, reply_size); |
| 3276 | #endif /* CONFIG_TAXONOMY */ |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 3277 | } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) { |
| 3278 | if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9)) |
| 3279 | reply_len = -1; |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 3280 | } else if (os_strcmp(buf, "STOP_AP") == 0) { |
| 3281 | if (hostapd_ctrl_iface_stop_ap(hapd)) |
| 3282 | reply_len = -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3283 | #ifdef NEED_AP_MLME |
| 3284 | } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) { |
| 3285 | if (hostapd_ctrl_iface_sa_query(hapd, buf + 9)) |
| 3286 | reply_len = -1; |
| 3287 | #endif /* NEED_AP_MLME */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3288 | #ifdef CONFIG_WPS |
| 3289 | } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) { |
| 3290 | if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8)) |
| 3291 | reply_len = -1; |
| 3292 | } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) { |
| 3293 | reply_len = hostapd_ctrl_iface_wps_check_pin( |
| 3294 | hapd, buf + 14, reply, reply_size); |
| 3295 | } else if (os_strcmp(buf, "WPS_PBC") == 0) { |
| 3296 | if (hostapd_wps_button_pushed(hapd, NULL)) |
| 3297 | reply_len = -1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3298 | } else if (os_strcmp(buf, "WPS_CANCEL") == 0) { |
| 3299 | if (hostapd_wps_cancel(hapd)) |
| 3300 | reply_len = -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3301 | } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) { |
| 3302 | reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11, |
| 3303 | reply, reply_size); |
| 3304 | } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) { |
| 3305 | if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0) |
| 3306 | reply_len = -1; |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 3307 | } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) { |
| 3308 | reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply, |
| 3309 | reply_size); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3310 | #ifdef CONFIG_WPS_NFC |
| 3311 | } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) { |
| 3312 | if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17)) |
| 3313 | reply_len = -1; |
| 3314 | } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) { |
| 3315 | reply_len = hostapd_ctrl_iface_wps_nfc_config_token( |
| 3316 | hapd, buf + 21, reply, reply_size); |
| 3317 | } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) { |
| 3318 | reply_len = hostapd_ctrl_iface_wps_nfc_token( |
| 3319 | hapd, buf + 14, reply, reply_size); |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 3320 | } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) { |
| 3321 | reply_len = hostapd_ctrl_iface_nfc_get_handover_sel( |
| 3322 | hapd, buf + 21, reply, reply_size); |
| 3323 | } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) { |
| 3324 | if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20)) |
| 3325 | reply_len = -1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3326 | #endif /* CONFIG_WPS_NFC */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3327 | #endif /* CONFIG_WPS */ |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 3328 | #ifdef CONFIG_INTERWORKING |
| 3329 | } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) { |
| 3330 | if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16)) |
| 3331 | reply_len = -1; |
| 3332 | } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) { |
| 3333 | if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18)) |
| 3334 | reply_len = -1; |
| 3335 | #endif /* CONFIG_INTERWORKING */ |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 3336 | #ifdef CONFIG_HS20 |
| 3337 | } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) { |
| 3338 | if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15)) |
| 3339 | reply_len = -1; |
| 3340 | } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) { |
| 3341 | if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16)) |
| 3342 | reply_len = -1; |
| 3343 | #endif /* CONFIG_HS20 */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3344 | #ifdef CONFIG_WNM_AP |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 3345 | } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) { |
| 3346 | if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18)) |
| 3347 | reply_len = -1; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3348 | } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) { |
| 3349 | if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13)) |
| 3350 | reply_len = -1; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3351 | } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) { |
| 3352 | if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11)) |
| 3353 | reply_len = -1; |
Hai Shalom | 39ba6fc | 2019-01-22 12:40:38 -0800 | [diff] [blame] | 3354 | } else if (os_strncmp(buf, "COLOC_INTF_REQ ", 15) == 0) { |
| 3355 | if (hostapd_ctrl_iface_coloc_intf_req(hapd, buf + 15)) |
| 3356 | reply_len = -1; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3357 | #endif /* CONFIG_WNM_AP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3358 | } else if (os_strcmp(buf, "GET_CONFIG") == 0) { |
| 3359 | reply_len = hostapd_ctrl_iface_get_config(hapd, reply, |
| 3360 | reply_size); |
| 3361 | } else if (os_strncmp(buf, "SET ", 4) == 0) { |
| 3362 | if (hostapd_ctrl_iface_set(hapd, buf + 4)) |
| 3363 | reply_len = -1; |
| 3364 | } else if (os_strncmp(buf, "GET ", 4) == 0) { |
| 3365 | reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply, |
| 3366 | reply_size); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3367 | } else if (os_strncmp(buf, "ENABLE", 6) == 0) { |
| 3368 | if (hostapd_ctrl_iface_enable(hapd->iface)) |
| 3369 | reply_len = -1; |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 3370 | } else if (os_strcmp(buf, "RELOAD_WPA_PSK") == 0) { |
| 3371 | if (hostapd_ctrl_iface_reload_wpa_psk(hapd)) |
| 3372 | reply_len = -1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3373 | } else if (os_strncmp(buf, "RELOAD", 6) == 0) { |
| 3374 | if (hostapd_ctrl_iface_reload(hapd->iface)) |
| 3375 | reply_len = -1; |
| 3376 | } else if (os_strncmp(buf, "DISABLE", 7) == 0) { |
| 3377 | if (hostapd_ctrl_iface_disable(hapd->iface)) |
| 3378 | reply_len = -1; |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 3379 | } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) { |
| 3380 | if (ieee802_11_set_beacon(hapd)) |
| 3381 | reply_len = -1; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 3382 | #ifdef CONFIG_TESTING_OPTIONS |
| 3383 | } else if (os_strncmp(buf, "RADAR ", 6) == 0) { |
| 3384 | if (hostapd_ctrl_iface_radar(hapd, buf + 6)) |
| 3385 | reply_len = -1; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 3386 | } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) { |
| 3387 | if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8)) |
| 3388 | reply_len = -1; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3389 | } else if (os_strncmp(buf, "MGMT_TX_STATUS_PROCESS ", 23) == 0) { |
| 3390 | if (hostapd_ctrl_iface_mgmt_tx_status_process(hapd, |
| 3391 | buf + 23) < 0) |
| 3392 | reply_len = -1; |
Dmitry Shmidt | abb90a3 | 2016-12-05 15:34:39 -0800 | [diff] [blame] | 3393 | } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) { |
| 3394 | if (hostapd_ctrl_iface_mgmt_rx_process(hapd, buf + 16) < 0) |
| 3395 | reply_len = -1; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3396 | } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) { |
| 3397 | if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0) |
| 3398 | reply_len = -1; |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3399 | } else if (os_strncmp(buf, "EAPOL_TX ", 9) == 0) { |
| 3400 | if (hostapd_ctrl_iface_eapol_tx(hapd, buf + 9) < 0) |
| 3401 | reply_len = -1; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3402 | } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) { |
| 3403 | if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0) |
| 3404 | reply_len = -1; |
| 3405 | } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) { |
| 3406 | if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0) |
| 3407 | reply_len = -1; |
| 3408 | } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) { |
| 3409 | if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0) |
| 3410 | reply_len = -1; |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 3411 | } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) { |
| 3412 | if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0) |
| 3413 | reply_len = -1; |
| 3414 | } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) { |
| 3415 | reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply, |
| 3416 | reply_size); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3417 | } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) { |
| 3418 | if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0) |
| 3419 | reply_len = -1; |
| 3420 | } else if (os_strcmp(buf, "GET_FAIL") == 0) { |
| 3421 | reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3422 | } else if (os_strncmp(buf, "RESET_PN ", 9) == 0) { |
| 3423 | if (hostapd_ctrl_reset_pn(hapd, buf + 9) < 0) |
| 3424 | reply_len = -1; |
| 3425 | } else if (os_strncmp(buf, "SET_KEY ", 8) == 0) { |
| 3426 | if (hostapd_ctrl_set_key(hapd, buf + 8) < 0) |
| 3427 | reply_len = -1; |
| 3428 | } else if (os_strncmp(buf, "RESEND_M1 ", 10) == 0) { |
| 3429 | if (hostapd_ctrl_resend_m1(hapd, buf + 10) < 0) |
| 3430 | reply_len = -1; |
| 3431 | } else if (os_strncmp(buf, "RESEND_M3 ", 10) == 0) { |
| 3432 | if (hostapd_ctrl_resend_m3(hapd, buf + 10) < 0) |
| 3433 | reply_len = -1; |
| 3434 | } else if (os_strncmp(buf, "RESEND_GROUP_M1 ", 16) == 0) { |
| 3435 | if (hostapd_ctrl_resend_group_m1(hapd, buf + 16) < 0) |
| 3436 | reply_len = -1; |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3437 | } else if (os_strncmp(buf, "REKEY_PTK ", 10) == 0) { |
| 3438 | if (hostapd_ctrl_rekey_ptk(hapd, buf + 10) < 0) |
| 3439 | reply_len = -1; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3440 | } else if (os_strcmp(buf, "REKEY_GTK") == 0) { |
| 3441 | if (wpa_auth_rekey_gtk(hapd->wpa_auth) < 0) |
| 3442 | reply_len = -1; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 3443 | } else if (os_strncmp(buf, "GET_PMK ", 8) == 0) { |
| 3444 | reply_len = hostapd_ctrl_get_pmk(hapd, buf + 8, reply, |
| 3445 | reply_size); |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3446 | } else if (os_strncmp(buf, "REGISTER_FRAME ", 15) == 0) { |
| 3447 | if (hostapd_ctrl_register_frame(hapd, buf + 16) < 0) |
| 3448 | reply_len = -1; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 3449 | #endif /* CONFIG_TESTING_OPTIONS */ |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 3450 | } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) { |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 3451 | if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12)) |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 3452 | reply_len = -1; |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 3453 | } else if (os_strncmp(buf, "VENDOR ", 7) == 0) { |
| 3454 | reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply, |
| 3455 | reply_size); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3456 | } else if (os_strcmp(buf, "ERP_FLUSH") == 0) { |
| 3457 | ieee802_1x_erp_flush(hapd); |
| 3458 | #ifdef RADIUS_SERVER |
| 3459 | radius_server_erp_flush(hapd->radius_srv); |
| 3460 | #endif /* RADIUS_SERVER */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3461 | } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) { |
| 3462 | if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13)) |
| 3463 | reply_len = -1; |
| 3464 | } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) { |
| 3465 | if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10)) |
| 3466 | reply_len = -1; |
| 3467 | } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) { |
| 3468 | reply_len = hostapd_ctrl_iface_log_level( |
| 3469 | hapd, buf + 9, reply, reply_size); |
| 3470 | #ifdef NEED_AP_MLME |
| 3471 | } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) { |
| 3472 | reply_len = hostapd_ctrl_iface_track_sta_list( |
| 3473 | hapd, reply, reply_size); |
| 3474 | #endif /* NEED_AP_MLME */ |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 3475 | } else if (os_strcmp(buf, "PMKSA") == 0) { |
| 3476 | reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply, |
| 3477 | reply_size); |
| 3478 | } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) { |
| 3479 | hostapd_ctrl_iface_pmksa_flush(hapd); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3480 | } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) { |
| 3481 | if (hostapd_ctrl_iface_pmksa_add(hapd, buf + 10) < 0) |
| 3482 | reply_len = -1; |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 3483 | } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) { |
| 3484 | if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13)) |
| 3485 | reply_len = -1; |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 3486 | } else if (os_strcmp(buf, "SHOW_NEIGHBOR") == 0) { |
| 3487 | reply_len = hostapd_ctrl_iface_show_neighbor(hapd, reply, |
| 3488 | reply_size); |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 3489 | } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) { |
| 3490 | if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16)) |
| 3491 | reply_len = -1; |
| 3492 | } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) { |
| 3493 | if (hostapd_ctrl_iface_req_lci(hapd, buf + 8)) |
| 3494 | reply_len = -1; |
| 3495 | } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) { |
| 3496 | if (hostapd_ctrl_iface_req_range(hapd, buf + 10)) |
| 3497 | reply_len = -1; |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 3498 | } else if (os_strncmp(buf, "REQ_BEACON ", 11) == 0) { |
| 3499 | reply_len = hostapd_ctrl_iface_req_beacon(hapd, buf + 11, |
| 3500 | reply, reply_size); |
Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 3501 | } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) { |
| 3502 | reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply, |
| 3503 | reply_size); |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 3504 | } else if (os_strcmp(buf, "DRIVER_FLAGS2") == 0) { |
| 3505 | reply_len = hostapd_ctrl_driver_flags2(hapd->iface, reply, |
| 3506 | reply_size); |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 3507 | } else if (os_strcmp(buf, "TERMINATE") == 0) { |
| 3508 | eloop_terminate(); |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3509 | } else if (os_strncmp(buf, "ACCEPT_ACL ", 11) == 0) { |
| 3510 | if (os_strncmp(buf + 11, "ADD_MAC ", 8) == 0) { |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 3511 | if (hostapd_ctrl_iface_acl_add_mac( |
| 3512 | &hapd->conf->accept_mac, |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3513 | &hapd->conf->num_accept_mac, buf + 19) || |
| 3514 | hostapd_set_acl(hapd)) |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 3515 | reply_len = -1; |
| 3516 | } else if (os_strncmp((buf + 11), "DEL_MAC ", 8) == 0) { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3517 | if (hostapd_ctrl_iface_acl_del_mac( |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3518 | &hapd->conf->accept_mac, |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3519 | &hapd->conf->num_accept_mac, buf + 19) || |
| 3520 | hostapd_set_acl(hapd) || |
| 3521 | hostapd_disassoc_accept_mac(hapd)) |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3522 | reply_len = -1; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3523 | } else if (os_strcmp(buf + 11, "SHOW") == 0) { |
| 3524 | reply_len = hostapd_ctrl_iface_acl_show_mac( |
| 3525 | hapd->conf->accept_mac, |
| 3526 | hapd->conf->num_accept_mac, reply, reply_size); |
| 3527 | } else if (os_strcmp(buf + 11, "CLEAR") == 0) { |
| 3528 | hostapd_ctrl_iface_acl_clear_list( |
| 3529 | &hapd->conf->accept_mac, |
| 3530 | &hapd->conf->num_accept_mac); |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3531 | if (hostapd_set_acl(hapd) || |
| 3532 | hostapd_disassoc_accept_mac(hapd)) |
| 3533 | reply_len = -1; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3534 | } |
| 3535 | } else if (os_strncmp(buf, "DENY_ACL ", 9) == 0) { |
| 3536 | if (os_strncmp(buf + 9, "ADD_MAC ", 8) == 0) { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3537 | if (hostapd_ctrl_iface_acl_add_mac( |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3538 | &hapd->conf->deny_mac, |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3539 | &hapd->conf->num_deny_mac, buf + 17) || |
| 3540 | hostapd_set_acl(hapd) || |
| 3541 | hostapd_disassoc_deny_mac(hapd)) |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 3542 | reply_len = -1; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3543 | } else if (os_strncmp(buf + 9, "DEL_MAC ", 8) == 0) { |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 3544 | if (hostapd_ctrl_iface_acl_del_mac( |
| 3545 | &hapd->conf->deny_mac, |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3546 | &hapd->conf->num_deny_mac, buf + 17) || |
| 3547 | hostapd_set_acl(hapd)) |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 3548 | reply_len = -1; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3549 | } else if (os_strcmp(buf + 9, "SHOW") == 0) { |
| 3550 | reply_len = hostapd_ctrl_iface_acl_show_mac( |
| 3551 | hapd->conf->deny_mac, |
| 3552 | hapd->conf->num_deny_mac, reply, reply_size); |
| 3553 | } else if (os_strcmp(buf + 9, "CLEAR") == 0) { |
| 3554 | hostapd_ctrl_iface_acl_clear_list( |
| 3555 | &hapd->conf->deny_mac, |
| 3556 | &hapd->conf->num_deny_mac); |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3557 | if (hostapd_set_acl(hapd)) |
| 3558 | reply_len = -1; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3559 | } |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3560 | #ifdef CONFIG_DPP |
| 3561 | } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) { |
| 3562 | res = hostapd_dpp_qr_code(hapd, buf + 12); |
| 3563 | if (res < 0) { |
| 3564 | reply_len = -1; |
| 3565 | } else { |
| 3566 | reply_len = os_snprintf(reply, reply_size, "%d", res); |
| 3567 | if (os_snprintf_error(reply_size, reply_len)) |
| 3568 | reply_len = -1; |
| 3569 | } |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 3570 | } else if (os_strncmp(buf, "DPP_NFC_URI ", 12) == 0) { |
| 3571 | res = hostapd_dpp_nfc_uri(hapd, buf + 12); |
| 3572 | if (res < 0) { |
| 3573 | reply_len = -1; |
| 3574 | } else { |
| 3575 | reply_len = os_snprintf(reply, reply_size, "%d", res); |
| 3576 | if (os_snprintf_error(reply_size, reply_len)) |
| 3577 | reply_len = -1; |
| 3578 | } |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 3579 | } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_REQ ", 21) == 0) { |
| 3580 | res = hostapd_dpp_nfc_handover_req(hapd, buf + 20); |
| 3581 | if (res < 0) { |
| 3582 | reply_len = -1; |
| 3583 | } else { |
| 3584 | reply_len = os_snprintf(reply, reply_size, "%d", res); |
| 3585 | if (os_snprintf_error(reply_size, reply_len)) |
| 3586 | reply_len = -1; |
| 3587 | } |
| 3588 | } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_SEL ", 21) == 0) { |
| 3589 | res = hostapd_dpp_nfc_handover_sel(hapd, buf + 20); |
| 3590 | if (res < 0) { |
| 3591 | reply_len = -1; |
| 3592 | } else { |
| 3593 | reply_len = os_snprintf(reply, reply_size, "%d", res); |
| 3594 | if (os_snprintf_error(reply_size, reply_len)) |
| 3595 | reply_len = -1; |
| 3596 | } |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3597 | } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) { |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 3598 | res = dpp_bootstrap_gen(hapd->iface->interfaces->dpp, buf + 18); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3599 | if (res < 0) { |
| 3600 | reply_len = -1; |
| 3601 | } else { |
| 3602 | reply_len = os_snprintf(reply, reply_size, "%d", res); |
| 3603 | if (os_snprintf_error(reply_size, reply_len)) |
| 3604 | reply_len = -1; |
| 3605 | } |
| 3606 | } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) { |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 3607 | if (dpp_bootstrap_remove(hapd->iface->interfaces->dpp, |
| 3608 | buf + 21) < 0) |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3609 | reply_len = -1; |
| 3610 | } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) { |
| 3611 | const char *uri; |
| 3612 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 3613 | uri = dpp_bootstrap_get_uri(hapd->iface->interfaces->dpp, |
| 3614 | atoi(buf + 22)); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3615 | if (!uri) { |
| 3616 | reply_len = -1; |
| 3617 | } else { |
| 3618 | reply_len = os_snprintf(reply, reply_size, "%s", uri); |
| 3619 | if (os_snprintf_error(reply_size, reply_len)) |
| 3620 | reply_len = -1; |
| 3621 | } |
| 3622 | } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) { |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 3623 | reply_len = dpp_bootstrap_info(hapd->iface->interfaces->dpp, |
| 3624 | atoi(buf + 19), |
| 3625 | reply, reply_size); |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 3626 | } else if (os_strncmp(buf, "DPP_BOOTSTRAP_SET ", 18) == 0) { |
| 3627 | if (dpp_bootstrap_set(hapd->iface->interfaces->dpp, |
| 3628 | atoi(buf + 18), |
| 3629 | os_strchr(buf + 18, ' ')) < 0) |
| 3630 | reply_len = -1; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3631 | } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) { |
| 3632 | if (hostapd_dpp_auth_init(hapd, buf + 13) < 0) |
| 3633 | reply_len = -1; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3634 | } else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) { |
| 3635 | if (hostapd_dpp_listen(hapd, buf + 11) < 0) |
| 3636 | reply_len = -1; |
| 3637 | } else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) { |
| 3638 | hostapd_dpp_stop(hapd); |
| 3639 | hostapd_dpp_listen_stop(hapd); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3640 | } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) { |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 3641 | res = dpp_configurator_add(hapd->iface->interfaces->dpp, |
| 3642 | buf + 20); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3643 | if (res < 0) { |
| 3644 | reply_len = -1; |
| 3645 | } else { |
| 3646 | reply_len = os_snprintf(reply, reply_size, "%d", res); |
| 3647 | if (os_snprintf_error(reply_size, reply_len)) |
| 3648 | reply_len = -1; |
| 3649 | } |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3650 | } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SET ", 21) == 0) { |
| 3651 | if (dpp_configurator_set(hapd->iface->interfaces->dpp, |
| 3652 | buf + 20) < 0) |
| 3653 | reply_len = -1; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3654 | } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) { |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 3655 | if (dpp_configurator_remove(hapd->iface->interfaces->dpp, |
| 3656 | buf + 24) < 0) |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3657 | reply_len = -1; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3658 | } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) { |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 3659 | if (hostapd_dpp_configurator_sign(hapd, buf + 21) < 0) |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3660 | reply_len = -1; |
| 3661 | } else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) { |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 3662 | reply_len = dpp_configurator_get_key_id( |
| 3663 | hapd->iface->interfaces->dpp, |
| 3664 | atoi(buf + 25), |
| 3665 | reply, reply_size); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3666 | } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) { |
| 3667 | res = hostapd_dpp_pkex_add(hapd, buf + 12); |
| 3668 | if (res < 0) { |
| 3669 | reply_len = -1; |
| 3670 | } else { |
| 3671 | reply_len = os_snprintf(reply, reply_size, "%d", res); |
| 3672 | if (os_snprintf_error(reply_size, reply_len)) |
| 3673 | reply_len = -1; |
| 3674 | } |
| 3675 | } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) { |
| 3676 | if (hostapd_dpp_pkex_remove(hapd, buf + 16) < 0) |
| 3677 | reply_len = -1; |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame] | 3678 | #ifdef CONFIG_DPP2 |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3679 | } else if (os_strncmp(buf, "DPP_CONTROLLER_START ", 21) == 0) { |
| 3680 | if (hostapd_dpp_controller_start(hapd, buf + 20) < 0) |
| 3681 | reply_len = -1; |
| 3682 | } else if (os_strcmp(buf, "DPP_CONTROLLER_START") == 0) { |
| 3683 | if (hostapd_dpp_controller_start(hapd, NULL) < 0) |
| 3684 | reply_len = -1; |
| 3685 | } else if (os_strcmp(buf, "DPP_CONTROLLER_STOP") == 0) { |
| 3686 | dpp_controller_stop(hapd->iface->interfaces->dpp); |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame] | 3687 | } else if (os_strncmp(buf, "DPP_CHIRP ", 10) == 0) { |
| 3688 | if (hostapd_dpp_chirp(hapd, buf + 9) < 0) |
| 3689 | reply_len = -1; |
| 3690 | } else if (os_strcmp(buf, "DPP_STOP_CHIRP") == 0) { |
| 3691 | hostapd_dpp_chirp_stop(hapd); |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame^] | 3692 | } else if (os_strncmp(buf, "DPP_RELAY_ADD_CONTROLLER ", 25) == 0) { |
| 3693 | if (hostapd_dpp_add_controller(hapd, buf + 25) < 0) |
| 3694 | reply_len = -1; |
| 3695 | } else if (os_strncmp(buf, "DPP_RELAY_REMOVE_CONTROLLER ", 28) == 0) { |
| 3696 | hostapd_dpp_remove_controller(hapd, buf + 28); |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame] | 3697 | #endif /* CONFIG_DPP2 */ |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame^] | 3698 | #ifdef CONFIG_DPP3 |
| 3699 | } else if (os_strcmp(buf, "DPP_PUSH_BUTTON") == 0) { |
| 3700 | if (hostapd_dpp_push_button(hapd, NULL) < 0) |
| 3701 | reply_len = -1; |
| 3702 | } else if (os_strncmp(buf, "DPP_PUSH_BUTTON ", 16) == 0) { |
| 3703 | if (hostapd_dpp_push_button(hapd, buf + 15) < 0) |
| 3704 | reply_len = -1; |
| 3705 | #endif /* CONFIG_DPP3 */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3706 | #endif /* CONFIG_DPP */ |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3707 | #ifdef RADIUS_SERVER |
| 3708 | } else if (os_strncmp(buf, "DAC_REQUEST ", 12) == 0) { |
| 3709 | if (radius_server_dac_request(hapd->radius_srv, buf + 12) < 0) |
| 3710 | reply_len = -1; |
| 3711 | #endif /* RADIUS_SERVER */ |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 3712 | } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) { |
| 3713 | reply_len = hostapd_ctrl_iface_get_capability( |
| 3714 | hapd, buf + 15, reply, reply_size); |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 3715 | #ifdef CONFIG_PASN |
| 3716 | } else if (os_strcmp(buf, "PTKSA_CACHE_LIST") == 0) { |
| 3717 | reply_len = ptksa_cache_list(hapd->ptksa, reply, reply_size); |
| 3718 | #endif /* CONFIG_PASN */ |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3719 | #ifdef ANDROID |
| 3720 | } else if (os_strncmp(buf, "DRIVER ", 7) == 0) { |
| 3721 | reply_len = hostapd_ctrl_iface_driver_cmd(hapd, buf + 7, reply, |
| 3722 | reply_size); |
| 3723 | #endif /* ANDROID */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3724 | } else { |
| 3725 | os_memcpy(reply, "UNKNOWN COMMAND\n", 16); |
| 3726 | reply_len = 16; |
| 3727 | } |
| 3728 | |
| 3729 | if (reply_len < 0) { |
| 3730 | os_memcpy(reply, "FAIL\n", 5); |
| 3731 | reply_len = 5; |
| 3732 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3733 | |
| 3734 | return reply_len; |
| 3735 | } |
| 3736 | |
| 3737 | |
| 3738 | static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx, |
| 3739 | void *sock_ctx) |
| 3740 | { |
| 3741 | struct hostapd_data *hapd = eloop_ctx; |
| 3742 | char buf[4096]; |
| 3743 | int res; |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 3744 | struct sockaddr_storage from; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3745 | socklen_t fromlen = sizeof(from); |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 3746 | char *reply, *pos = buf; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3747 | const int reply_size = 4096; |
| 3748 | int reply_len; |
| 3749 | int level = MSG_DEBUG; |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 3750 | #ifdef CONFIG_CTRL_IFACE_UDP |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 3751 | unsigned char lcookie[CTRL_IFACE_COOKIE_LEN]; |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 3752 | #endif /* CONFIG_CTRL_IFACE_UDP */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3753 | |
| 3754 | res = recvfrom(sock, buf, sizeof(buf) - 1, 0, |
| 3755 | (struct sockaddr *) &from, &fromlen); |
| 3756 | if (res < 0) { |
| 3757 | wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s", |
| 3758 | strerror(errno)); |
| 3759 | return; |
| 3760 | } |
| 3761 | buf[res] = '\0'; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3762 | |
| 3763 | reply = os_malloc(reply_size); |
| 3764 | if (reply == NULL) { |
| 3765 | if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from, |
| 3766 | fromlen) < 0) { |
| 3767 | wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s", |
| 3768 | strerror(errno)); |
| 3769 | } |
| 3770 | return; |
| 3771 | } |
| 3772 | |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 3773 | #ifdef CONFIG_CTRL_IFACE_UDP |
| 3774 | if (os_strcmp(buf, "GET_COOKIE") == 0) { |
| 3775 | os_memcpy(reply, "COOKIE=", 7); |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 3776 | wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1, |
| 3777 | hapd->ctrl_iface_cookie, |
| 3778 | CTRL_IFACE_COOKIE_LEN); |
| 3779 | reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN; |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 3780 | goto done; |
| 3781 | } |
| 3782 | |
| 3783 | if (os_strncmp(buf, "COOKIE=", 7) != 0 || |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 3784 | hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) { |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 3785 | wpa_printf(MSG_DEBUG, |
| 3786 | "CTRL: No cookie in the request - drop request"); |
| 3787 | os_free(reply); |
| 3788 | return; |
| 3789 | } |
| 3790 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 3791 | if (os_memcmp(hapd->ctrl_iface_cookie, lcookie, |
| 3792 | CTRL_IFACE_COOKIE_LEN) != 0) { |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 3793 | wpa_printf(MSG_DEBUG, |
| 3794 | "CTRL: Invalid cookie in the request - drop request"); |
| 3795 | os_free(reply); |
| 3796 | return; |
| 3797 | } |
| 3798 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 3799 | pos = buf + 7 + 2 * CTRL_IFACE_COOKIE_LEN; |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 3800 | while (*pos == ' ') |
| 3801 | pos++; |
| 3802 | #endif /* CONFIG_CTRL_IFACE_UDP */ |
| 3803 | |
| 3804 | if (os_strcmp(pos, "PING") == 0) |
| 3805 | level = MSG_EXCESSIVE; |
| 3806 | wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res); |
| 3807 | |
| 3808 | reply_len = hostapd_ctrl_iface_receive_process(hapd, pos, |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3809 | reply, reply_size, |
| 3810 | &from, fromlen); |
| 3811 | |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 3812 | #ifdef CONFIG_CTRL_IFACE_UDP |
| 3813 | done: |
| 3814 | #endif /* CONFIG_CTRL_IFACE_UDP */ |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3815 | if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, |
| 3816 | fromlen) < 0) { |
| 3817 | wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s", |
| 3818 | strerror(errno)); |
| 3819 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3820 | os_free(reply); |
| 3821 | } |
| 3822 | |
| 3823 | |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 3824 | #ifndef CONFIG_CTRL_IFACE_UDP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3825 | static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd) |
| 3826 | { |
| 3827 | char *buf; |
| 3828 | size_t len; |
| 3829 | |
| 3830 | if (hapd->conf->ctrl_interface == NULL) |
| 3831 | return NULL; |
| 3832 | |
| 3833 | len = os_strlen(hapd->conf->ctrl_interface) + |
| 3834 | os_strlen(hapd->conf->iface) + 2; |
| 3835 | buf = os_malloc(len); |
| 3836 | if (buf == NULL) |
| 3837 | return NULL; |
| 3838 | |
| 3839 | os_snprintf(buf, len, "%s/%s", |
| 3840 | hapd->conf->ctrl_interface, hapd->conf->iface); |
| 3841 | buf[len - 1] = '\0'; |
| 3842 | return buf; |
| 3843 | } |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 3844 | #endif /* CONFIG_CTRL_IFACE_UDP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3845 | |
| 3846 | |
Dmitry Shmidt | 7a53dbb | 2015-06-11 13:13:53 -0700 | [diff] [blame] | 3847 | static void hostapd_ctrl_iface_msg_cb(void *ctx, int level, |
| 3848 | enum wpa_msg_type type, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3849 | const char *txt, size_t len) |
| 3850 | { |
| 3851 | struct hostapd_data *hapd = ctx; |
| 3852 | if (hapd == NULL) |
| 3853 | return; |
Anton Nayshtut | f715e8d | 2014-11-16 16:52:49 +0200 | [diff] [blame] | 3854 | hostapd_ctrl_iface_send(hapd, level, type, txt, len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3855 | } |
| 3856 | |
| 3857 | |
| 3858 | int hostapd_ctrl_iface_init(struct hostapd_data *hapd) |
| 3859 | { |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 3860 | #ifdef CONFIG_CTRL_IFACE_UDP |
| 3861 | int port = HOSTAPD_CTRL_IFACE_PORT; |
| 3862 | char p[32] = { 0 }; |
| 3863 | char port_str[40], *tmp; |
| 3864 | char *pos; |
| 3865 | struct addrinfo hints = { 0 }, *res, *saveres; |
| 3866 | int n; |
| 3867 | |
| 3868 | if (hapd->ctrl_sock > -1) { |
| 3869 | wpa_printf(MSG_DEBUG, "ctrl_iface already exists!"); |
| 3870 | return 0; |
| 3871 | } |
| 3872 | |
| 3873 | if (hapd->conf->ctrl_interface == NULL) |
| 3874 | return 0; |
| 3875 | |
| 3876 | pos = os_strstr(hapd->conf->ctrl_interface, "udp:"); |
| 3877 | if (pos) { |
| 3878 | pos += 4; |
| 3879 | port = atoi(pos); |
| 3880 | if (port <= 0) { |
| 3881 | wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port"); |
| 3882 | goto fail; |
| 3883 | } |
| 3884 | } |
| 3885 | |
| 3886 | dl_list_init(&hapd->ctrl_dst); |
| 3887 | hapd->ctrl_sock = -1; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 3888 | os_get_random(hapd->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN); |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 3889 | |
| 3890 | #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE |
| 3891 | hints.ai_flags = AI_PASSIVE; |
| 3892 | #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */ |
| 3893 | |
| 3894 | #ifdef CONFIG_CTRL_IFACE_UDP_IPV6 |
| 3895 | hints.ai_family = AF_INET6; |
| 3896 | #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ |
| 3897 | hints.ai_family = AF_INET; |
| 3898 | #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ |
| 3899 | hints.ai_socktype = SOCK_DGRAM; |
| 3900 | |
| 3901 | try_again: |
| 3902 | os_snprintf(p, sizeof(p), "%d", port); |
| 3903 | n = getaddrinfo(NULL, p, &hints, &res); |
| 3904 | if (n) { |
| 3905 | wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n)); |
| 3906 | goto fail; |
| 3907 | } |
| 3908 | |
| 3909 | saveres = res; |
| 3910 | hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype, |
| 3911 | res->ai_protocol); |
| 3912 | if (hapd->ctrl_sock < 0) { |
| 3913 | wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno)); |
| 3914 | goto fail; |
| 3915 | } |
| 3916 | |
| 3917 | if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) { |
| 3918 | port--; |
| 3919 | if ((HOSTAPD_CTRL_IFACE_PORT - port) < |
| 3920 | HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos) |
| 3921 | goto try_again; |
| 3922 | wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno)); |
| 3923 | goto fail; |
| 3924 | } |
| 3925 | |
| 3926 | freeaddrinfo(saveres); |
| 3927 | |
| 3928 | os_snprintf(port_str, sizeof(port_str), "udp:%d", port); |
| 3929 | tmp = os_strdup(port_str); |
| 3930 | if (tmp) { |
| 3931 | os_free(hapd->conf->ctrl_interface); |
| 3932 | hapd->conf->ctrl_interface = tmp; |
| 3933 | } |
| 3934 | wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port); |
| 3935 | |
| 3936 | if (eloop_register_read_sock(hapd->ctrl_sock, |
| 3937 | hostapd_ctrl_iface_receive, hapd, NULL) < |
| 3938 | 0) { |
| 3939 | hostapd_ctrl_iface_deinit(hapd); |
| 3940 | return -1; |
| 3941 | } |
| 3942 | |
| 3943 | hapd->msg_ctx = hapd; |
| 3944 | wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb); |
| 3945 | |
| 3946 | return 0; |
| 3947 | |
| 3948 | fail: |
| 3949 | if (hapd->ctrl_sock >= 0) |
| 3950 | close(hapd->ctrl_sock); |
| 3951 | return -1; |
| 3952 | #else /* CONFIG_CTRL_IFACE_UDP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3953 | struct sockaddr_un addr; |
| 3954 | int s = -1; |
| 3955 | char *fname = NULL; |
| 3956 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3957 | if (hapd->ctrl_sock > -1) { |
| 3958 | wpa_printf(MSG_DEBUG, "ctrl_iface already exists!"); |
| 3959 | return 0; |
| 3960 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3961 | |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 3962 | dl_list_init(&hapd->ctrl_dst); |
| 3963 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3964 | if (hapd->conf->ctrl_interface == NULL) |
| 3965 | return 0; |
| 3966 | |
| 3967 | if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) { |
| 3968 | if (errno == EEXIST) { |
| 3969 | wpa_printf(MSG_DEBUG, "Using existing control " |
| 3970 | "interface directory."); |
| 3971 | } else { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3972 | wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s", |
| 3973 | strerror(errno)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3974 | goto fail; |
| 3975 | } |
| 3976 | } |
| 3977 | |
| 3978 | if (hapd->conf->ctrl_interface_gid_set && |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 3979 | lchown(hapd->conf->ctrl_interface, -1, |
| 3980 | hapd->conf->ctrl_interface_gid) < 0) { |
| 3981 | wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s", |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3982 | strerror(errno)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3983 | return -1; |
| 3984 | } |
| 3985 | |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame] | 3986 | if (!hapd->conf->ctrl_interface_gid_set && |
| 3987 | hapd->iface->interfaces->ctrl_iface_group && |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 3988 | lchown(hapd->conf->ctrl_interface, -1, |
| 3989 | hapd->iface->interfaces->ctrl_iface_group) < 0) { |
| 3990 | wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s", |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3991 | strerror(errno)); |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame] | 3992 | return -1; |
| 3993 | } |
| 3994 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 3995 | #ifdef ANDROID |
| 3996 | /* |
| 3997 | * Android is using umask 0077 which would leave the control interface |
| 3998 | * directory without group access. This breaks things since Wi-Fi |
| 3999 | * framework assumes that this directory can be accessed by other |
| 4000 | * applications in the wifi group. Fix this by adding group access even |
| 4001 | * if umask value would prevent this. |
| 4002 | */ |
| 4003 | if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) { |
| 4004 | wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s", |
| 4005 | strerror(errno)); |
| 4006 | /* Try to continue anyway */ |
| 4007 | } |
| 4008 | #endif /* ANDROID */ |
| 4009 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4010 | if (os_strlen(hapd->conf->ctrl_interface) + 1 + |
| 4011 | os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path)) |
| 4012 | goto fail; |
| 4013 | |
| 4014 | s = socket(PF_UNIX, SOCK_DGRAM, 0); |
| 4015 | if (s < 0) { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4016 | wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4017 | goto fail; |
| 4018 | } |
| 4019 | |
| 4020 | os_memset(&addr, 0, sizeof(addr)); |
| 4021 | #ifdef __FreeBSD__ |
| 4022 | addr.sun_len = sizeof(addr); |
| 4023 | #endif /* __FreeBSD__ */ |
| 4024 | addr.sun_family = AF_UNIX; |
| 4025 | fname = hostapd_ctrl_iface_path(hapd); |
| 4026 | if (fname == NULL) |
| 4027 | goto fail; |
| 4028 | os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path)); |
| 4029 | if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 4030 | wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s", |
| 4031 | strerror(errno)); |
| 4032 | if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 4033 | wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not" |
| 4034 | " allow connections - assuming it was left" |
| 4035 | "over from forced program termination"); |
| 4036 | if (unlink(fname) < 0) { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4037 | wpa_printf(MSG_ERROR, |
| 4038 | "Could not unlink existing ctrl_iface socket '%s': %s", |
| 4039 | fname, strerror(errno)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4040 | goto fail; |
| 4041 | } |
| 4042 | if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < |
| 4043 | 0) { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4044 | wpa_printf(MSG_ERROR, |
| 4045 | "hostapd-ctrl-iface: bind(PF_UNIX): %s", |
| 4046 | strerror(errno)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4047 | goto fail; |
| 4048 | } |
| 4049 | wpa_printf(MSG_DEBUG, "Successfully replaced leftover " |
| 4050 | "ctrl_iface socket '%s'", fname); |
| 4051 | } else { |
| 4052 | wpa_printf(MSG_INFO, "ctrl_iface exists and seems to " |
| 4053 | "be in use - cannot override it"); |
| 4054 | wpa_printf(MSG_INFO, "Delete '%s' manually if it is " |
| 4055 | "not used anymore", fname); |
| 4056 | os_free(fname); |
| 4057 | fname = NULL; |
| 4058 | goto fail; |
| 4059 | } |
| 4060 | } |
| 4061 | |
| 4062 | if (hapd->conf->ctrl_interface_gid_set && |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 4063 | lchown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) { |
| 4064 | wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s", |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4065 | strerror(errno)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4066 | goto fail; |
| 4067 | } |
| 4068 | |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame] | 4069 | if (!hapd->conf->ctrl_interface_gid_set && |
| 4070 | hapd->iface->interfaces->ctrl_iface_group && |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 4071 | lchown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) { |
| 4072 | wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s", |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4073 | strerror(errno)); |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame] | 4074 | goto fail; |
| 4075 | } |
| 4076 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4077 | if (chmod(fname, S_IRWXU | S_IRWXG) < 0) { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4078 | wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s", |
| 4079 | strerror(errno)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4080 | goto fail; |
| 4081 | } |
| 4082 | os_free(fname); |
| 4083 | |
| 4084 | hapd->ctrl_sock = s; |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 4085 | if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd, |
| 4086 | NULL) < 0) { |
| 4087 | hostapd_ctrl_iface_deinit(hapd); |
| 4088 | return -1; |
| 4089 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4090 | hapd->msg_ctx = hapd; |
| 4091 | wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb); |
| 4092 | |
| 4093 | return 0; |
| 4094 | |
| 4095 | fail: |
| 4096 | if (s >= 0) |
| 4097 | close(s); |
| 4098 | if (fname) { |
| 4099 | unlink(fname); |
| 4100 | os_free(fname); |
| 4101 | } |
| 4102 | return -1; |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4103 | #endif /* CONFIG_CTRL_IFACE_UDP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4104 | } |
| 4105 | |
| 4106 | |
| 4107 | void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd) |
| 4108 | { |
| 4109 | struct wpa_ctrl_dst *dst, *prev; |
| 4110 | |
| 4111 | if (hapd->ctrl_sock > -1) { |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4112 | #ifndef CONFIG_CTRL_IFACE_UDP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4113 | char *fname; |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4114 | #endif /* !CONFIG_CTRL_IFACE_UDP */ |
| 4115 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4116 | eloop_unregister_read_sock(hapd->ctrl_sock); |
| 4117 | close(hapd->ctrl_sock); |
| 4118 | hapd->ctrl_sock = -1; |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4119 | #ifndef CONFIG_CTRL_IFACE_UDP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4120 | fname = hostapd_ctrl_iface_path(hapd); |
| 4121 | if (fname) |
| 4122 | unlink(fname); |
| 4123 | os_free(fname); |
| 4124 | |
| 4125 | if (hapd->conf->ctrl_interface && |
| 4126 | rmdir(hapd->conf->ctrl_interface) < 0) { |
| 4127 | if (errno == ENOTEMPTY) { |
| 4128 | wpa_printf(MSG_DEBUG, "Control interface " |
| 4129 | "directory not empty - leaving it " |
| 4130 | "behind"); |
| 4131 | } else { |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 4132 | wpa_printf(MSG_ERROR, |
| 4133 | "rmdir[ctrl_interface=%s]: %s", |
| 4134 | hapd->conf->ctrl_interface, |
| 4135 | strerror(errno)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4136 | } |
| 4137 | } |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4138 | #endif /* !CONFIG_CTRL_IFACE_UDP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4139 | } |
| 4140 | |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4141 | dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst, |
| 4142 | list) |
| 4143 | os_free(dst); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4144 | |
| 4145 | #ifdef CONFIG_TESTING_OPTIONS |
| 4146 | l2_packet_deinit(hapd->l2_test); |
| 4147 | hapd->l2_test = NULL; |
| 4148 | #endif /* CONFIG_TESTING_OPTIONS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4149 | } |
| 4150 | |
| 4151 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4152 | static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces, |
| 4153 | char *buf) |
| 4154 | { |
| 4155 | if (hostapd_add_iface(interfaces, buf) < 0) { |
| 4156 | wpa_printf(MSG_ERROR, "Adding interface %s failed", buf); |
| 4157 | return -1; |
| 4158 | } |
| 4159 | return 0; |
| 4160 | } |
| 4161 | |
| 4162 | |
| 4163 | static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces, |
| 4164 | char *buf) |
| 4165 | { |
| 4166 | if (hostapd_remove_iface(interfaces, buf) < 0) { |
| 4167 | wpa_printf(MSG_ERROR, "Removing interface %s failed", buf); |
| 4168 | return -1; |
| 4169 | } |
| 4170 | return 0; |
| 4171 | } |
| 4172 | |
| 4173 | |
Anton Nayshtut | f715e8d | 2014-11-16 16:52:49 +0200 | [diff] [blame] | 4174 | static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces, |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4175 | struct sockaddr_storage *from, |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4176 | socklen_t fromlen, char *input) |
Anton Nayshtut | f715e8d | 2014-11-16 16:52:49 +0200 | [diff] [blame] | 4177 | { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4178 | return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen, |
| 4179 | input); |
Anton Nayshtut | f715e8d | 2014-11-16 16:52:49 +0200 | [diff] [blame] | 4180 | } |
| 4181 | |
| 4182 | |
| 4183 | static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces, |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4184 | struct sockaddr_storage *from, |
Anton Nayshtut | f715e8d | 2014-11-16 16:52:49 +0200 | [diff] [blame] | 4185 | socklen_t fromlen) |
| 4186 | { |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4187 | return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen); |
Anton Nayshtut | f715e8d | 2014-11-16 16:52:49 +0200 | [diff] [blame] | 4188 | } |
| 4189 | |
| 4190 | |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4191 | static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces) |
| 4192 | { |
| 4193 | #ifdef CONFIG_WPS_TESTING |
| 4194 | wps_version_number = 0x20; |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 4195 | wps_testing_stub_cred = 0; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4196 | wps_corrupt_pkhash = 0; |
| 4197 | #endif /* CONFIG_WPS_TESTING */ |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4198 | |
| 4199 | #ifdef CONFIG_TESTING_OPTIONS |
| 4200 | #ifdef CONFIG_DPP |
| 4201 | dpp_test = DPP_TEST_DISABLED; |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 4202 | #ifdef CONFIG_DPP3 |
| 4203 | dpp_version_override = 3; |
| 4204 | #elif defined(CONFIG_DPP2) |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame] | 4205 | dpp_version_override = 2; |
| 4206 | #else /* CONFIG_DPP2 */ |
| 4207 | dpp_version_override = 1; |
| 4208 | #endif /* CONFIG_DPP2 */ |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4209 | #endif /* CONFIG_DPP */ |
| 4210 | #endif /* CONFIG_TESTING_OPTIONS */ |
| 4211 | |
| 4212 | #ifdef CONFIG_DPP |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 4213 | dpp_global_clear(interfaces->dpp); |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame^] | 4214 | #ifdef CONFIG_DPP3 |
| 4215 | { |
| 4216 | int i; |
| 4217 | |
| 4218 | for (i = 0; i < DPP_PB_INFO_COUNT; i++) { |
| 4219 | struct dpp_pb_info *info; |
| 4220 | |
| 4221 | info = &interfaces->dpp_pb[i]; |
| 4222 | info->rx_time.sec = 0; |
| 4223 | info->rx_time.usec = 0; |
| 4224 | } |
| 4225 | } |
| 4226 | #endif /* CONFIG_DPP3 */ |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4227 | #endif /* CONFIG_DPP */ |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4228 | } |
| 4229 | |
| 4230 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4231 | #ifdef CONFIG_FST |
| 4232 | |
| 4233 | static int |
| 4234 | hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces, |
| 4235 | const char *cmd) |
| 4236 | { |
| 4237 | char ifname[IFNAMSIZ + 1]; |
| 4238 | struct fst_iface_cfg cfg; |
| 4239 | struct hostapd_data *hapd; |
| 4240 | struct fst_wpa_obj iface_obj; |
| 4241 | |
| 4242 | if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) { |
| 4243 | hapd = hostapd_get_iface(interfaces, ifname); |
| 4244 | if (hapd) { |
| 4245 | if (hapd->iface->fst) { |
| 4246 | wpa_printf(MSG_INFO, "FST: Already attached"); |
| 4247 | return -1; |
| 4248 | } |
| 4249 | fst_hostapd_fill_iface_obj(hapd, &iface_obj); |
| 4250 | hapd->iface->fst = fst_attach(ifname, hapd->own_addr, |
| 4251 | &iface_obj, &cfg); |
| 4252 | if (hapd->iface->fst) |
| 4253 | return 0; |
| 4254 | } |
| 4255 | } |
| 4256 | |
| 4257 | return -EINVAL; |
| 4258 | } |
| 4259 | |
| 4260 | |
| 4261 | static int |
| 4262 | hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces, |
| 4263 | const char *cmd) |
| 4264 | { |
| 4265 | char ifname[IFNAMSIZ + 1]; |
| 4266 | struct hostapd_data * hapd; |
| 4267 | |
| 4268 | if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) { |
| 4269 | hapd = hostapd_get_iface(interfaces, ifname); |
| 4270 | if (hapd) { |
| 4271 | if (!fst_iface_detach(ifname)) { |
| 4272 | hapd->iface->fst = NULL; |
| 4273 | hapd->iface->fst_ies = NULL; |
| 4274 | return 0; |
| 4275 | } |
| 4276 | } |
| 4277 | } |
| 4278 | |
| 4279 | return -EINVAL; |
| 4280 | } |
| 4281 | |
| 4282 | #endif /* CONFIG_FST */ |
| 4283 | |
| 4284 | |
| 4285 | static struct hostapd_data * |
| 4286 | hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces, |
| 4287 | const char *ifname) |
| 4288 | { |
| 4289 | size_t i, j; |
| 4290 | |
| 4291 | for (i = 0; i < interfaces->count; i++) { |
| 4292 | struct hostapd_iface *iface = interfaces->iface[i]; |
| 4293 | |
| 4294 | for (j = 0; j < iface->num_bss; j++) { |
| 4295 | struct hostapd_data *hapd; |
| 4296 | |
| 4297 | hapd = iface->bss[j]; |
| 4298 | if (os_strcmp(ifname, hapd->conf->iface) == 0) |
| 4299 | return hapd; |
| 4300 | } |
| 4301 | } |
| 4302 | |
| 4303 | return NULL; |
| 4304 | } |
| 4305 | |
| 4306 | |
| 4307 | static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd, |
| 4308 | struct hostapd_data *dst_hapd, |
| 4309 | const char *param) |
| 4310 | { |
| 4311 | int res; |
| 4312 | char *value; |
| 4313 | |
| 4314 | value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN); |
| 4315 | if (!value) { |
| 4316 | wpa_printf(MSG_ERROR, |
| 4317 | "DUP: cannot allocate buffer to stringify %s", |
| 4318 | param); |
| 4319 | goto error_return; |
| 4320 | } |
| 4321 | |
| 4322 | if (os_strcmp(param, "wpa") == 0) { |
| 4323 | os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d", |
| 4324 | src_hapd->conf->wpa); |
| 4325 | } else if (os_strcmp(param, "wpa_key_mgmt") == 0 && |
| 4326 | src_hapd->conf->wpa_key_mgmt) { |
| 4327 | res = hostapd_ctrl_iface_get_key_mgmt( |
| 4328 | src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN); |
| 4329 | if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res)) |
| 4330 | goto error_stringify; |
| 4331 | } else if (os_strcmp(param, "wpa_pairwise") == 0 && |
| 4332 | src_hapd->conf->wpa_pairwise) { |
| 4333 | res = wpa_write_ciphers(value, |
| 4334 | value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN, |
| 4335 | src_hapd->conf->wpa_pairwise, " "); |
| 4336 | if (res < 0) |
| 4337 | goto error_stringify; |
| 4338 | } else if (os_strcmp(param, "rsn_pairwise") == 0 && |
| 4339 | src_hapd->conf->rsn_pairwise) { |
| 4340 | res = wpa_write_ciphers(value, |
| 4341 | value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN, |
| 4342 | src_hapd->conf->rsn_pairwise, " "); |
| 4343 | if (res < 0) |
| 4344 | goto error_stringify; |
| 4345 | } else if (os_strcmp(param, "wpa_passphrase") == 0 && |
| 4346 | src_hapd->conf->ssid.wpa_passphrase) { |
| 4347 | os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s", |
| 4348 | src_hapd->conf->ssid.wpa_passphrase); |
| 4349 | } else if (os_strcmp(param, "wpa_psk") == 0 && |
| 4350 | src_hapd->conf->ssid.wpa_psk_set) { |
| 4351 | wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, |
| 4352 | src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN); |
| 4353 | } else { |
| 4354 | wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param); |
| 4355 | goto error_return; |
| 4356 | } |
| 4357 | |
| 4358 | res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value); |
| 4359 | os_free(value); |
| 4360 | return res; |
| 4361 | |
| 4362 | error_stringify: |
| 4363 | wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param); |
| 4364 | error_return: |
| 4365 | os_free(value); |
| 4366 | return -1; |
| 4367 | } |
| 4368 | |
| 4369 | |
| 4370 | static int |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4371 | hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces, |
| 4372 | const char *input, |
| 4373 | char *reply, int reply_size) |
| 4374 | { |
| 4375 | size_t i, j; |
| 4376 | int res; |
| 4377 | char *pos, *end; |
| 4378 | struct hostapd_iface *iface; |
| 4379 | int show_ctrl = 0; |
| 4380 | |
| 4381 | if (input) |
| 4382 | show_ctrl = !!os_strstr(input, "ctrl"); |
| 4383 | |
| 4384 | pos = reply; |
| 4385 | end = reply + reply_size; |
| 4386 | |
| 4387 | for (i = 0; i < interfaces->count; i++) { |
| 4388 | iface = interfaces->iface[i]; |
| 4389 | |
| 4390 | for (j = 0; j < iface->num_bss; j++) { |
| 4391 | struct hostapd_bss_config *conf; |
| 4392 | |
| 4393 | conf = iface->conf->bss[j]; |
| 4394 | if (show_ctrl) |
| 4395 | res = os_snprintf(pos, end - pos, |
| 4396 | "%s ctrl_iface=%s\n", |
| 4397 | conf->iface, |
| 4398 | conf->ctrl_interface ? |
| 4399 | conf->ctrl_interface : "N/A"); |
| 4400 | else |
| 4401 | res = os_snprintf(pos, end - pos, "%s\n", |
| 4402 | conf->iface); |
| 4403 | if (os_snprintf_error(end - pos, res)) { |
| 4404 | *pos = '\0'; |
| 4405 | return pos - reply; |
| 4406 | } |
| 4407 | pos += res; |
| 4408 | } |
| 4409 | } |
| 4410 | |
| 4411 | return pos - reply; |
| 4412 | } |
| 4413 | |
| 4414 | |
| 4415 | static int |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4416 | hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces, |
| 4417 | char *cmd) |
| 4418 | { |
| 4419 | char *p_start = cmd, *p_end; |
| 4420 | struct hostapd_data *src_hapd, *dst_hapd; |
| 4421 | |
| 4422 | /* cmd: "<src ifname> <dst ifname> <variable name> */ |
| 4423 | |
| 4424 | p_end = os_strchr(p_start, ' '); |
| 4425 | if (!p_end) { |
| 4426 | wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'", |
| 4427 | cmd); |
| 4428 | return -1; |
| 4429 | } |
| 4430 | |
| 4431 | *p_end = '\0'; |
| 4432 | src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start); |
| 4433 | if (!src_hapd) { |
| 4434 | wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'", |
| 4435 | p_start); |
| 4436 | return -1; |
| 4437 | } |
| 4438 | |
| 4439 | p_start = p_end + 1; |
| 4440 | p_end = os_strchr(p_start, ' '); |
| 4441 | if (!p_end) { |
| 4442 | wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'", |
| 4443 | cmd); |
| 4444 | return -1; |
| 4445 | } |
| 4446 | |
| 4447 | *p_end = '\0'; |
| 4448 | dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start); |
| 4449 | if (!dst_hapd) { |
| 4450 | wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'", |
| 4451 | p_start); |
| 4452 | return -1; |
| 4453 | } |
| 4454 | |
| 4455 | p_start = p_end + 1; |
| 4456 | return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start); |
| 4457 | } |
| 4458 | |
| 4459 | |
| 4460 | static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces, |
| 4461 | const char *ifname, |
| 4462 | char *buf, char *reply, |
| 4463 | int reply_size, |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4464 | struct sockaddr_storage *from, |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4465 | socklen_t fromlen) |
| 4466 | { |
| 4467 | struct hostapd_data *hapd; |
| 4468 | |
| 4469 | hapd = hostapd_interfaces_get_hapd(interfaces, ifname); |
| 4470 | if (hapd == NULL) { |
| 4471 | int res; |
| 4472 | |
| 4473 | res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n"); |
| 4474 | if (os_snprintf_error(reply_size, res)) |
| 4475 | return -1; |
| 4476 | return res; |
| 4477 | } |
| 4478 | |
| 4479 | return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size, |
| 4480 | from, fromlen); |
| 4481 | } |
| 4482 | |
| 4483 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4484 | static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx, |
| 4485 | void *sock_ctx) |
| 4486 | { |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 4487 | struct hapd_interfaces *interfaces = eloop_ctx; |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4488 | char buffer[256], *buf = buffer; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4489 | int res; |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4490 | struct sockaddr_storage from; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4491 | socklen_t fromlen = sizeof(from); |
Anton Nayshtut | f715e8d | 2014-11-16 16:52:49 +0200 | [diff] [blame] | 4492 | char *reply; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4493 | int reply_len; |
Anton Nayshtut | f715e8d | 2014-11-16 16:52:49 +0200 | [diff] [blame] | 4494 | const int reply_size = 4096; |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4495 | #ifdef CONFIG_CTRL_IFACE_UDP |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 4496 | unsigned char lcookie[CTRL_IFACE_COOKIE_LEN]; |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4497 | #endif /* CONFIG_CTRL_IFACE_UDP */ |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4498 | |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4499 | res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4500 | (struct sockaddr *) &from, &fromlen); |
| 4501 | if (res < 0) { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4502 | wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s", |
| 4503 | strerror(errno)); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4504 | return; |
| 4505 | } |
| 4506 | buf[res] = '\0'; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 4507 | wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4508 | |
Anton Nayshtut | f715e8d | 2014-11-16 16:52:49 +0200 | [diff] [blame] | 4509 | reply = os_malloc(reply_size); |
| 4510 | if (reply == NULL) { |
| 4511 | if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from, |
| 4512 | fromlen) < 0) { |
| 4513 | wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s", |
| 4514 | strerror(errno)); |
| 4515 | } |
| 4516 | return; |
| 4517 | } |
| 4518 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4519 | os_memcpy(reply, "OK\n", 3); |
| 4520 | reply_len = 3; |
| 4521 | |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4522 | #ifdef CONFIG_CTRL_IFACE_UDP |
| 4523 | if (os_strcmp(buf, "GET_COOKIE") == 0) { |
| 4524 | os_memcpy(reply, "COOKIE=", 7); |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 4525 | wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1, |
| 4526 | interfaces->ctrl_iface_cookie, |
| 4527 | CTRL_IFACE_COOKIE_LEN); |
| 4528 | reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN; |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4529 | goto send_reply; |
| 4530 | } |
| 4531 | |
| 4532 | if (os_strncmp(buf, "COOKIE=", 7) != 0 || |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 4533 | hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) { |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4534 | wpa_printf(MSG_DEBUG, |
| 4535 | "CTRL: No cookie in the request - drop request"); |
| 4536 | os_free(reply); |
| 4537 | return; |
| 4538 | } |
| 4539 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 4540 | if (os_memcmp(interfaces->ctrl_iface_cookie, lcookie, |
| 4541 | CTRL_IFACE_COOKIE_LEN) != 0) { |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4542 | wpa_printf(MSG_DEBUG, |
| 4543 | "CTRL: Invalid cookie in the request - drop request"); |
| 4544 | os_free(reply); |
| 4545 | return; |
| 4546 | } |
| 4547 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 4548 | buf += 7 + 2 * CTRL_IFACE_COOKIE_LEN; |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4549 | while (*buf == ' ') |
| 4550 | buf++; |
| 4551 | #endif /* CONFIG_CTRL_IFACE_UDP */ |
| 4552 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4553 | if (os_strncmp(buf, "IFNAME=", 7) == 0) { |
| 4554 | char *pos = os_strchr(buf + 7, ' '); |
| 4555 | |
| 4556 | if (pos) { |
| 4557 | *pos++ = '\0'; |
| 4558 | reply_len = hostapd_global_ctrl_iface_ifname( |
| 4559 | interfaces, buf + 7, pos, reply, reply_size, |
| 4560 | &from, fromlen); |
| 4561 | goto send_reply; |
| 4562 | } |
| 4563 | } |
| 4564 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4565 | if (os_strcmp(buf, "PING") == 0) { |
| 4566 | os_memcpy(reply, "PONG\n", 5); |
| 4567 | reply_len = 5; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 4568 | } else if (os_strncmp(buf, "RELOG", 5) == 0) { |
| 4569 | if (wpa_debug_reopen_file() < 0) |
| 4570 | reply_len = -1; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4571 | } else if (os_strcmp(buf, "FLUSH") == 0) { |
| 4572 | hostapd_ctrl_iface_flush(interfaces); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4573 | } else if (os_strncmp(buf, "ADD ", 4) == 0) { |
| 4574 | if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0) |
| 4575 | reply_len = -1; |
| 4576 | } else if (os_strncmp(buf, "REMOVE ", 7) == 0) { |
| 4577 | if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0) |
| 4578 | reply_len = -1; |
Anton Nayshtut | f715e8d | 2014-11-16 16:52:49 +0200 | [diff] [blame] | 4579 | } else if (os_strcmp(buf, "ATTACH") == 0) { |
| 4580 | if (hostapd_global_ctrl_iface_attach(interfaces, &from, |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4581 | fromlen, NULL)) |
| 4582 | reply_len = -1; |
| 4583 | } else if (os_strncmp(buf, "ATTACH ", 7) == 0) { |
| 4584 | if (hostapd_global_ctrl_iface_attach(interfaces, &from, |
| 4585 | fromlen, buf + 7)) |
Anton Nayshtut | f715e8d | 2014-11-16 16:52:49 +0200 | [diff] [blame] | 4586 | reply_len = -1; |
| 4587 | } else if (os_strcmp(buf, "DETACH") == 0) { |
| 4588 | if (hostapd_global_ctrl_iface_detach(interfaces, &from, |
| 4589 | fromlen)) |
| 4590 | reply_len = -1; |
Dmitry Shmidt | 7f93d6f | 2014-02-21 11:22:49 -0800 | [diff] [blame] | 4591 | #ifdef CONFIG_MODULE_TESTS |
| 4592 | } else if (os_strcmp(buf, "MODULE_TESTS") == 0) { |
Dmitry Shmidt | 7f93d6f | 2014-02-21 11:22:49 -0800 | [diff] [blame] | 4593 | if (hapd_module_tests() < 0) |
| 4594 | reply_len = -1; |
| 4595 | #endif /* CONFIG_MODULE_TESTS */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4596 | #ifdef CONFIG_FST |
| 4597 | } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) { |
| 4598 | if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11)) |
| 4599 | reply_len = os_snprintf(reply, reply_size, "OK\n"); |
| 4600 | else |
| 4601 | reply_len = -1; |
| 4602 | } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) { |
| 4603 | if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11)) |
| 4604 | reply_len = os_snprintf(reply, reply_size, "OK\n"); |
| 4605 | else |
| 4606 | reply_len = -1; |
| 4607 | } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) { |
| 4608 | reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size); |
| 4609 | #endif /* CONFIG_FST */ |
| 4610 | } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) { |
| 4611 | if (!hostapd_global_ctrl_iface_dup_network(interfaces, |
| 4612 | buf + 12)) |
| 4613 | reply_len = os_snprintf(reply, reply_size, "OK\n"); |
| 4614 | else |
| 4615 | reply_len = -1; |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4616 | } else if (os_strncmp(buf, "INTERFACES", 10) == 0) { |
| 4617 | reply_len = hostapd_global_ctrl_iface_interfaces( |
| 4618 | interfaces, buf + 10, reply, sizeof(buffer)); |
| 4619 | } else if (os_strcmp(buf, "TERMINATE") == 0) { |
| 4620 | eloop_terminate(); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4621 | } else { |
| 4622 | wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command " |
| 4623 | "ignored"); |
| 4624 | reply_len = -1; |
| 4625 | } |
| 4626 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4627 | send_reply: |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4628 | if (reply_len < 0) { |
| 4629 | os_memcpy(reply, "FAIL\n", 5); |
| 4630 | reply_len = 5; |
| 4631 | } |
| 4632 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4633 | if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, |
| 4634 | fromlen) < 0) { |
| 4635 | wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s", |
| 4636 | strerror(errno)); |
| 4637 | } |
Anton Nayshtut | f715e8d | 2014-11-16 16:52:49 +0200 | [diff] [blame] | 4638 | os_free(reply); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4639 | } |
| 4640 | |
| 4641 | |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4642 | #ifndef CONFIG_CTRL_IFACE_UDP |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4643 | static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface) |
| 4644 | { |
| 4645 | char *buf; |
| 4646 | size_t len; |
| 4647 | |
| 4648 | if (interface->global_iface_path == NULL) |
| 4649 | return NULL; |
| 4650 | |
| 4651 | len = os_strlen(interface->global_iface_path) + |
| 4652 | os_strlen(interface->global_iface_name) + 2; |
| 4653 | buf = os_malloc(len); |
| 4654 | if (buf == NULL) |
| 4655 | return NULL; |
| 4656 | |
| 4657 | os_snprintf(buf, len, "%s/%s", interface->global_iface_path, |
| 4658 | interface->global_iface_name); |
| 4659 | buf[len - 1] = '\0'; |
| 4660 | return buf; |
| 4661 | } |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4662 | #endif /* CONFIG_CTRL_IFACE_UDP */ |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4663 | |
| 4664 | |
| 4665 | int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface) |
| 4666 | { |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4667 | #ifdef CONFIG_CTRL_IFACE_UDP |
| 4668 | int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT; |
| 4669 | char p[32] = { 0 }; |
| 4670 | char *pos; |
| 4671 | struct addrinfo hints = { 0 }, *res, *saveres; |
| 4672 | int n; |
| 4673 | |
| 4674 | if (interface->global_ctrl_sock > -1) { |
| 4675 | wpa_printf(MSG_DEBUG, "ctrl_iface already exists!"); |
| 4676 | return 0; |
| 4677 | } |
| 4678 | |
| 4679 | if (interface->global_iface_path == NULL) |
| 4680 | return 0; |
| 4681 | |
| 4682 | pos = os_strstr(interface->global_iface_path, "udp:"); |
| 4683 | if (pos) { |
| 4684 | pos += 4; |
| 4685 | port = atoi(pos); |
| 4686 | if (port <= 0) { |
| 4687 | wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port"); |
| 4688 | goto fail; |
| 4689 | } |
| 4690 | } |
| 4691 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 4692 | os_get_random(interface->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN); |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4693 | |
| 4694 | #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE |
| 4695 | hints.ai_flags = AI_PASSIVE; |
| 4696 | #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */ |
| 4697 | |
| 4698 | #ifdef CONFIG_CTRL_IFACE_UDP_IPV6 |
| 4699 | hints.ai_family = AF_INET6; |
| 4700 | #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ |
| 4701 | hints.ai_family = AF_INET; |
| 4702 | #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ |
| 4703 | hints.ai_socktype = SOCK_DGRAM; |
| 4704 | |
| 4705 | try_again: |
| 4706 | os_snprintf(p, sizeof(p), "%d", port); |
| 4707 | n = getaddrinfo(NULL, p, &hints, &res); |
| 4708 | if (n) { |
| 4709 | wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n)); |
| 4710 | goto fail; |
| 4711 | } |
| 4712 | |
| 4713 | saveres = res; |
| 4714 | interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype, |
| 4715 | res->ai_protocol); |
| 4716 | if (interface->global_ctrl_sock < 0) { |
| 4717 | wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno)); |
| 4718 | goto fail; |
| 4719 | } |
| 4720 | |
| 4721 | if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) < |
| 4722 | 0) { |
| 4723 | port++; |
| 4724 | if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) < |
| 4725 | HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos) |
| 4726 | goto try_again; |
| 4727 | wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno)); |
| 4728 | goto fail; |
| 4729 | } |
| 4730 | |
| 4731 | freeaddrinfo(saveres); |
| 4732 | |
| 4733 | wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port); |
| 4734 | |
| 4735 | if (eloop_register_read_sock(interface->global_ctrl_sock, |
| 4736 | hostapd_global_ctrl_iface_receive, |
| 4737 | interface, NULL) < 0) { |
| 4738 | hostapd_global_ctrl_iface_deinit(interface); |
| 4739 | return -1; |
| 4740 | } |
| 4741 | |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 4742 | wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb); |
| 4743 | |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4744 | return 0; |
| 4745 | |
| 4746 | fail: |
| 4747 | if (interface->global_ctrl_sock >= 0) |
| 4748 | close(interface->global_ctrl_sock); |
| 4749 | return -1; |
| 4750 | #else /* CONFIG_CTRL_IFACE_UDP */ |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4751 | struct sockaddr_un addr; |
| 4752 | int s = -1; |
| 4753 | char *fname = NULL; |
| 4754 | |
| 4755 | if (interface->global_iface_path == NULL) { |
| 4756 | wpa_printf(MSG_DEBUG, "ctrl_iface not configured!"); |
| 4757 | return 0; |
| 4758 | } |
| 4759 | |
| 4760 | if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) { |
| 4761 | if (errno == EEXIST) { |
| 4762 | wpa_printf(MSG_DEBUG, "Using existing control " |
| 4763 | "interface directory."); |
| 4764 | } else { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4765 | wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s", |
| 4766 | strerror(errno)); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4767 | goto fail; |
| 4768 | } |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame] | 4769 | } else if (interface->ctrl_iface_group && |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 4770 | lchown(interface->global_iface_path, -1, |
| 4771 | interface->ctrl_iface_group) < 0) { |
| 4772 | wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s", |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4773 | strerror(errno)); |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame] | 4774 | goto fail; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4775 | } |
| 4776 | |
| 4777 | if (os_strlen(interface->global_iface_path) + 1 + |
| 4778 | os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path)) |
| 4779 | goto fail; |
| 4780 | |
| 4781 | s = socket(PF_UNIX, SOCK_DGRAM, 0); |
| 4782 | if (s < 0) { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4783 | wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno)); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4784 | goto fail; |
| 4785 | } |
| 4786 | |
| 4787 | os_memset(&addr, 0, sizeof(addr)); |
| 4788 | #ifdef __FreeBSD__ |
| 4789 | addr.sun_len = sizeof(addr); |
| 4790 | #endif /* __FreeBSD__ */ |
| 4791 | addr.sun_family = AF_UNIX; |
| 4792 | fname = hostapd_global_ctrl_iface_path(interface); |
| 4793 | if (fname == NULL) |
| 4794 | goto fail; |
| 4795 | os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path)); |
| 4796 | if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 4797 | wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s", |
| 4798 | strerror(errno)); |
| 4799 | if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 4800 | wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not" |
| 4801 | " allow connections - assuming it was left" |
| 4802 | "over from forced program termination"); |
| 4803 | if (unlink(fname) < 0) { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4804 | wpa_printf(MSG_ERROR, |
| 4805 | "Could not unlink existing ctrl_iface socket '%s': %s", |
| 4806 | fname, strerror(errno)); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4807 | goto fail; |
| 4808 | } |
| 4809 | if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < |
| 4810 | 0) { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4811 | wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s", |
| 4812 | strerror(errno)); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4813 | goto fail; |
| 4814 | } |
| 4815 | wpa_printf(MSG_DEBUG, "Successfully replaced leftover " |
| 4816 | "ctrl_iface socket '%s'", fname); |
| 4817 | } else { |
| 4818 | wpa_printf(MSG_INFO, "ctrl_iface exists and seems to " |
| 4819 | "be in use - cannot override it"); |
| 4820 | wpa_printf(MSG_INFO, "Delete '%s' manually if it is " |
| 4821 | "not used anymore", fname); |
| 4822 | os_free(fname); |
| 4823 | fname = NULL; |
| 4824 | goto fail; |
| 4825 | } |
| 4826 | } |
| 4827 | |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame] | 4828 | if (interface->ctrl_iface_group && |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 4829 | lchown(fname, -1, interface->ctrl_iface_group) < 0) { |
| 4830 | wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s", |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4831 | strerror(errno)); |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame] | 4832 | goto fail; |
| 4833 | } |
| 4834 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4835 | if (chmod(fname, S_IRWXU | S_IRWXG) < 0) { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4836 | wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s", |
| 4837 | strerror(errno)); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4838 | goto fail; |
| 4839 | } |
| 4840 | os_free(fname); |
| 4841 | |
| 4842 | interface->global_ctrl_sock = s; |
| 4843 | eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive, |
| 4844 | interface, NULL); |
| 4845 | |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 4846 | wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb); |
| 4847 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4848 | return 0; |
| 4849 | |
| 4850 | fail: |
| 4851 | if (s >= 0) |
| 4852 | close(s); |
| 4853 | if (fname) { |
| 4854 | unlink(fname); |
| 4855 | os_free(fname); |
| 4856 | } |
| 4857 | return -1; |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4858 | #endif /* CONFIG_CTRL_IFACE_UDP */ |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4859 | } |
| 4860 | |
| 4861 | |
| 4862 | void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces) |
| 4863 | { |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4864 | #ifndef CONFIG_CTRL_IFACE_UDP |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4865 | char *fname = NULL; |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4866 | #endif /* CONFIG_CTRL_IFACE_UDP */ |
Anton Nayshtut | f715e8d | 2014-11-16 16:52:49 +0200 | [diff] [blame] | 4867 | struct wpa_ctrl_dst *dst, *prev; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4868 | |
| 4869 | if (interfaces->global_ctrl_sock > -1) { |
| 4870 | eloop_unregister_read_sock(interfaces->global_ctrl_sock); |
| 4871 | close(interfaces->global_ctrl_sock); |
| 4872 | interfaces->global_ctrl_sock = -1; |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4873 | #ifndef CONFIG_CTRL_IFACE_UDP |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4874 | fname = hostapd_global_ctrl_iface_path(interfaces); |
| 4875 | if (fname) { |
| 4876 | unlink(fname); |
| 4877 | os_free(fname); |
| 4878 | } |
| 4879 | |
| 4880 | if (interfaces->global_iface_path && |
| 4881 | rmdir(interfaces->global_iface_path) < 0) { |
| 4882 | if (errno == ENOTEMPTY) { |
| 4883 | wpa_printf(MSG_DEBUG, "Control interface " |
| 4884 | "directory not empty - leaving it " |
| 4885 | "behind"); |
| 4886 | } else { |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 4887 | wpa_printf(MSG_ERROR, |
| 4888 | "rmdir[ctrl_interface=%s]: %s", |
| 4889 | interfaces->global_iface_path, |
| 4890 | strerror(errno)); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4891 | } |
| 4892 | } |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4893 | #endif /* CONFIG_CTRL_IFACE_UDP */ |
Anton Nayshtut | f715e8d | 2014-11-16 16:52:49 +0200 | [diff] [blame] | 4894 | } |
| 4895 | |
| 4896 | os_free(interfaces->global_iface_path); |
| 4897 | interfaces->global_iface_path = NULL; |
| 4898 | |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4899 | dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst, |
| 4900 | struct wpa_ctrl_dst, list) |
| 4901 | os_free(dst); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4902 | } |
| 4903 | |
| 4904 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4905 | static int hostapd_ctrl_check_event_enabled(struct wpa_ctrl_dst *dst, |
| 4906 | const char *buf) |
| 4907 | { |
| 4908 | /* Enable Probe Request events based on explicit request. |
| 4909 | * Other events are enabled by default. |
| 4910 | */ |
| 4911 | if (str_starts(buf, RX_PROBE_REQUEST)) |
| 4912 | return !!(dst->events & WPA_EVENT_RX_PROBE_REQUEST); |
| 4913 | return 1; |
| 4914 | } |
| 4915 | |
| 4916 | |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 4917 | static void hostapd_ctrl_iface_send_internal(int sock, struct dl_list *ctrl_dst, |
| 4918 | const char *ifname, int level, |
| 4919 | const char *buf, size_t len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4920 | { |
| 4921 | struct wpa_ctrl_dst *dst, *next; |
| 4922 | struct msghdr msg; |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 4923 | int idx, res; |
| 4924 | struct iovec io[5]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4925 | char levelstr[10]; |
| 4926 | |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 4927 | if (sock < 0 || dl_list_empty(ctrl_dst)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4928 | return; |
| 4929 | |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 4930 | res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level); |
| 4931 | if (os_snprintf_error(sizeof(levelstr), res)) |
| 4932 | return; |
| 4933 | idx = 0; |
| 4934 | if (ifname) { |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 4935 | io[idx].iov_base = "IFNAME="; |
| 4936 | io[idx].iov_len = 7; |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 4937 | idx++; |
| 4938 | io[idx].iov_base = (char *) ifname; |
| 4939 | io[idx].iov_len = os_strlen(ifname); |
| 4940 | idx++; |
| 4941 | io[idx].iov_base = " "; |
| 4942 | io[idx].iov_len = 1; |
| 4943 | idx++; |
| 4944 | } |
| 4945 | io[idx].iov_base = levelstr; |
| 4946 | io[idx].iov_len = os_strlen(levelstr); |
| 4947 | idx++; |
| 4948 | io[idx].iov_base = (char *) buf; |
| 4949 | io[idx].iov_len = len; |
| 4950 | idx++; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4951 | os_memset(&msg, 0, sizeof(msg)); |
| 4952 | msg.msg_iov = io; |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 4953 | msg.msg_iovlen = idx; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4954 | |
| 4955 | idx = 0; |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4956 | dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4957 | if ((level >= dst->debug_level) && |
| 4958 | hostapd_ctrl_check_event_enabled(dst, buf)) { |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 4959 | sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send", |
| 4960 | &dst->addr, dst->addrlen); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4961 | msg.msg_name = &dst->addr; |
| 4962 | msg.msg_namelen = dst->addrlen; |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 4963 | if (sendmsg(sock, &msg, 0) < 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4964 | int _errno = errno; |
| 4965 | wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: " |
| 4966 | "%d - %s", |
| 4967 | idx, errno, strerror(errno)); |
| 4968 | dst->errors++; |
| 4969 | if (dst->errors > 10 || _errno == ENOENT) { |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 4970 | ctrl_iface_detach(ctrl_dst, |
| 4971 | &dst->addr, |
| 4972 | dst->addrlen); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4973 | } |
| 4974 | } else |
| 4975 | dst->errors = 0; |
| 4976 | } |
| 4977 | idx++; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4978 | } |
| 4979 | } |
| 4980 | |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 4981 | |
| 4982 | static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level, |
| 4983 | enum wpa_msg_type type, |
| 4984 | const char *buf, size_t len) |
| 4985 | { |
| 4986 | if (type != WPA_MSG_NO_GLOBAL) { |
| 4987 | hostapd_ctrl_iface_send_internal( |
| 4988 | hapd->iface->interfaces->global_ctrl_sock, |
| 4989 | &hapd->iface->interfaces->global_ctrl_dst, |
| 4990 | type != WPA_MSG_PER_INTERFACE ? |
| 4991 | NULL : hapd->conf->iface, |
| 4992 | level, buf, len); |
| 4993 | } |
| 4994 | |
| 4995 | if (type != WPA_MSG_ONLY_GLOBAL) { |
| 4996 | hostapd_ctrl_iface_send_internal( |
| 4997 | hapd->ctrl_sock, &hapd->ctrl_dst, |
| 4998 | NULL, level, buf, len); |
| 4999 | } |
| 5000 | } |
| 5001 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5002 | #endif /* CONFIG_NATIVE_WINDOWS */ |