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