Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * hostapd / WPS integration |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame^] | 3 | * Copyright (c) 2008-2016, 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 | #include "utils/common.h" |
| 12 | #include "utils/eloop.h" |
| 13 | #include "utils/uuid.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 14 | #include "common/wpa_ctrl.h" |
| 15 | #include "common/ieee802_11_defs.h" |
| 16 | #include "common/ieee802_11_common.h" |
| 17 | #include "eapol_auth/eapol_auth_sm.h" |
| 18 | #include "eapol_auth/eapol_auth_sm_i.h" |
| 19 | #include "wps/wps.h" |
| 20 | #include "wps/wps_defs.h" |
| 21 | #include "wps/wps_dev_attr.h" |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 22 | #include "wps/wps_attr_parse.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 23 | #include "hostapd.h" |
| 24 | #include "ap_config.h" |
| 25 | #include "ap_drv_ops.h" |
| 26 | #include "beacon.h" |
| 27 | #include "sta_info.h" |
| 28 | #include "wps_hostapd.h" |
| 29 | |
| 30 | |
| 31 | #ifdef CONFIG_WPS_UPNP |
| 32 | #include "wps/wps_upnp.h" |
| 33 | static int hostapd_wps_upnp_init(struct hostapd_data *hapd, |
| 34 | struct wps_context *wps); |
| 35 | static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd); |
| 36 | #endif /* CONFIG_WPS_UPNP */ |
| 37 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 38 | static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da, |
| 39 | const u8 *bssid, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 40 | const u8 *ie, size_t ie_len, |
| 41 | int ssi_signal); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 42 | static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx); |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 43 | static void hostapd_wps_nfc_clear(struct wps_context *wps); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 44 | |
| 45 | |
| 46 | struct wps_for_each_data { |
| 47 | int (*func)(struct hostapd_data *h, void *ctx); |
| 48 | void *ctx; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 49 | struct hostapd_data *calling_hapd; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | |
| 53 | static int wps_for_each(struct hostapd_iface *iface, void *ctx) |
| 54 | { |
| 55 | struct wps_for_each_data *data = ctx; |
| 56 | size_t j; |
| 57 | |
| 58 | if (iface == NULL) |
| 59 | return 0; |
| 60 | for (j = 0; j < iface->num_bss; j++) { |
| 61 | struct hostapd_data *hapd = iface->bss[j]; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 62 | int ret; |
| 63 | |
| 64 | if (hapd != data->calling_hapd && |
| 65 | (hapd->conf->wps_independent || |
| 66 | data->calling_hapd->conf->wps_independent)) |
| 67 | continue; |
| 68 | |
| 69 | ret = data->func(hapd, data->ctx); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 70 | if (ret) |
| 71 | return ret; |
| 72 | } |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | static int hostapd_wps_for_each(struct hostapd_data *hapd, |
| 79 | int (*func)(struct hostapd_data *h, void *ctx), |
| 80 | void *ctx) |
| 81 | { |
| 82 | struct hostapd_iface *iface = hapd->iface; |
| 83 | struct wps_for_each_data data; |
| 84 | data.func = func; |
| 85 | data.ctx = ctx; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 86 | data.calling_hapd = hapd; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 87 | if (iface->interfaces == NULL || |
| 88 | iface->interfaces->for_each_interface == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 89 | return wps_for_each(iface, &data); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 90 | return iface->interfaces->for_each_interface(iface->interfaces, |
| 91 | wps_for_each, &data); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 95 | static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, |
| 96 | const u8 *p2p_dev_addr, const u8 *psk, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 97 | size_t psk_len) |
| 98 | { |
| 99 | struct hostapd_data *hapd = ctx; |
| 100 | struct hostapd_wpa_psk *p; |
| 101 | struct hostapd_ssid *ssid = &hapd->conf->ssid; |
| 102 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 103 | if (is_zero_ether_addr(p2p_dev_addr)) { |
| 104 | wpa_printf(MSG_DEBUG, |
| 105 | "Received new WPA/WPA2-PSK from WPS for STA " MACSTR, |
| 106 | MAC2STR(mac_addr)); |
| 107 | } else { |
| 108 | wpa_printf(MSG_DEBUG, |
| 109 | "Received new WPA/WPA2-PSK from WPS for STA " MACSTR |
| 110 | " P2P Device Addr " MACSTR, |
| 111 | MAC2STR(mac_addr), MAC2STR(p2p_dev_addr)); |
| 112 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 113 | wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len); |
| 114 | |
| 115 | if (psk_len != PMK_LEN) { |
| 116 | wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu", |
| 117 | (unsigned long) psk_len); |
| 118 | return -1; |
| 119 | } |
| 120 | |
| 121 | /* Add the new PSK to runtime PSK list */ |
| 122 | p = os_zalloc(sizeof(*p)); |
| 123 | if (p == NULL) |
| 124 | return -1; |
| 125 | os_memcpy(p->addr, mac_addr, ETH_ALEN); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 126 | os_memcpy(p->p2p_dev_addr, p2p_dev_addr, ETH_ALEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 127 | os_memcpy(p->psk, psk, PMK_LEN); |
| 128 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 129 | if (hapd->new_psk_cb) { |
| 130 | hapd->new_psk_cb(hapd->new_psk_cb_ctx, mac_addr, p2p_dev_addr, |
| 131 | psk, psk_len); |
| 132 | } |
| 133 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 134 | p->next = ssid->wpa_psk; |
| 135 | ssid->wpa_psk = p; |
| 136 | |
| 137 | if (ssid->wpa_psk_file) { |
| 138 | FILE *f; |
| 139 | char hex[PMK_LEN * 2 + 1]; |
| 140 | /* Add the new PSK to PSK list file */ |
| 141 | f = fopen(ssid->wpa_psk_file, "a"); |
| 142 | if (f == NULL) { |
| 143 | wpa_printf(MSG_DEBUG, "Failed to add the PSK to " |
| 144 | "'%s'", ssid->wpa_psk_file); |
| 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len); |
| 149 | fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex); |
| 150 | fclose(f); |
| 151 | } |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | static int hostapd_wps_set_ie_cb(void *ctx, struct wpabuf *beacon_ie, |
| 158 | struct wpabuf *probe_resp_ie) |
| 159 | { |
| 160 | struct hostapd_data *hapd = ctx; |
| 161 | wpabuf_free(hapd->wps_beacon_ie); |
| 162 | hapd->wps_beacon_ie = beacon_ie; |
| 163 | wpabuf_free(hapd->wps_probe_resp_ie); |
| 164 | hapd->wps_probe_resp_ie = probe_resp_ie; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 165 | if (hapd->beacon_set_done) |
| 166 | ieee802_11_set_beacon(hapd); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 167 | return hostapd_set_ap_wps_ie(hapd); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e, |
| 172 | const struct wps_device_data *dev) |
| 173 | { |
| 174 | struct hostapd_data *hapd = ctx; |
| 175 | char uuid[40], txt[400]; |
| 176 | int len; |
| 177 | char devtype[WPS_DEV_TYPE_BUFSIZE]; |
| 178 | if (uuid_bin2str(uuid_e, uuid, sizeof(uuid))) |
| 179 | return; |
| 180 | wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid); |
| 181 | len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED |
| 182 | "%s " MACSTR " [%s|%s|%s|%s|%s|%s]", |
| 183 | uuid, MAC2STR(dev->mac_addr), dev->device_name, |
| 184 | dev->manufacturer, dev->model_name, |
| 185 | dev->model_number, dev->serial_number, |
| 186 | wps_dev_type_bin2str(dev->pri_dev_type, devtype, |
| 187 | sizeof(devtype))); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 188 | if (!os_snprintf_error(sizeof(txt), len)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 189 | wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt); |
| 190 | |
| 191 | if (hapd->conf->wps_pin_requests) { |
| 192 | FILE *f; |
| 193 | struct os_time t; |
| 194 | f = fopen(hapd->conf->wps_pin_requests, "a"); |
| 195 | if (f == NULL) |
| 196 | return; |
| 197 | os_get_time(&t); |
| 198 | fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s" |
| 199 | "\t%s\n", |
| 200 | t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name, |
| 201 | dev->manufacturer, dev->model_name, dev->model_number, |
| 202 | dev->serial_number, |
| 203 | wps_dev_type_bin2str(dev->pri_dev_type, devtype, |
| 204 | sizeof(devtype))); |
| 205 | fclose(f); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 210 | struct wps_stop_reg_data { |
| 211 | struct hostapd_data *current_hapd; |
| 212 | const u8 *uuid_e; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 213 | const u8 *dev_pw; |
| 214 | size_t dev_pw_len; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | static int wps_stop_registrar(struct hostapd_data *hapd, void *ctx) |
| 218 | { |
| 219 | struct wps_stop_reg_data *data = ctx; |
| 220 | if (hapd != data->current_hapd && hapd->wps != NULL) |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 221 | wps_registrar_complete(hapd->wps->registrar, data->uuid_e, |
| 222 | data->dev_pw, data->dev_pw_len); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 227 | static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 228 | const u8 *uuid_e, const u8 *dev_pw, |
| 229 | size_t dev_pw_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 230 | { |
| 231 | struct hostapd_data *hapd = ctx; |
| 232 | char uuid[40]; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 233 | struct wps_stop_reg_data data; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 234 | if (uuid_bin2str(uuid_e, uuid, sizeof(uuid))) |
| 235 | return; |
| 236 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s", |
| 237 | MAC2STR(mac_addr), uuid); |
| 238 | if (hapd->wps_reg_success_cb) |
| 239 | hapd->wps_reg_success_cb(hapd->wps_reg_success_cb_ctx, |
| 240 | mac_addr, uuid_e); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 241 | data.current_hapd = hapd; |
| 242 | data.uuid_e = uuid_e; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 243 | data.dev_pw = dev_pw; |
| 244 | data.dev_pw_len = dev_pw_len; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 245 | hostapd_wps_for_each(hapd, wps_stop_registrar, &data); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | |
| 249 | static void hostapd_wps_enrollee_seen_cb(void *ctx, const u8 *addr, |
| 250 | const u8 *uuid_e, |
| 251 | const u8 *pri_dev_type, |
| 252 | u16 config_methods, |
| 253 | u16 dev_password_id, u8 request_type, |
| 254 | const char *dev_name) |
| 255 | { |
| 256 | struct hostapd_data *hapd = ctx; |
| 257 | char uuid[40]; |
| 258 | char devtype[WPS_DEV_TYPE_BUFSIZE]; |
| 259 | if (uuid_bin2str(uuid_e, uuid, sizeof(uuid))) |
| 260 | return; |
| 261 | if (dev_name == NULL) |
| 262 | dev_name = ""; |
| 263 | wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ENROLLEE_SEEN MACSTR |
| 264 | " %s %s 0x%x %u %u [%s]", |
| 265 | MAC2STR(addr), uuid, |
| 266 | wps_dev_type_bin2str(pri_dev_type, devtype, |
| 267 | sizeof(devtype)), |
| 268 | config_methods, dev_password_id, request_type, dev_name); |
| 269 | } |
| 270 | |
| 271 | |
| 272 | static int str_starts(const char *str, const char *start) |
| 273 | { |
| 274 | return os_strncmp(str, start, os_strlen(start)) == 0; |
| 275 | } |
| 276 | |
| 277 | |
| 278 | static void wps_reload_config(void *eloop_data, void *user_ctx) |
| 279 | { |
| 280 | struct hostapd_iface *iface = eloop_data; |
| 281 | |
| 282 | wpa_printf(MSG_DEBUG, "WPS: Reload configuration data"); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 283 | if (iface->interfaces == NULL || |
| 284 | iface->interfaces->reload_config(iface) < 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 285 | wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated " |
| 286 | "configuration"); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 291 | void hostapd_wps_eap_completed(struct hostapd_data *hapd) |
| 292 | { |
| 293 | /* |
| 294 | * Reduce race condition of the station trying to reconnect immediately |
| 295 | * after AP reconfiguration through WPS by rescheduling the reload |
| 296 | * timeout to happen after EAP completion rather than the originally |
| 297 | * scheduled 100 ms after new configuration became known. |
| 298 | */ |
| 299 | if (eloop_deplete_timeout(0, 0, wps_reload_config, hapd->iface, NULL) == |
| 300 | 1) |
| 301 | wpa_printf(MSG_DEBUG, "WPS: Reschedule immediate configuration reload"); |
| 302 | } |
| 303 | |
| 304 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 305 | static void hapd_new_ap_event(struct hostapd_data *hapd, const u8 *attr, |
| 306 | size_t attr_len) |
| 307 | { |
| 308 | size_t blen = attr_len * 2 + 1; |
| 309 | char *buf = os_malloc(blen); |
| 310 | if (buf) { |
| 311 | wpa_snprintf_hex(buf, blen, attr, attr_len); |
| 312 | wpa_msg(hapd->msg_ctx, MSG_INFO, |
| 313 | WPS_EVENT_NEW_AP_SETTINGS "%s", buf); |
| 314 | os_free(buf); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 319 | static int hapd_wps_reconfig_in_memory(struct hostapd_data *hapd, |
| 320 | const struct wps_credential *cred) |
| 321 | { |
| 322 | struct hostapd_bss_config *bss = hapd->conf; |
| 323 | |
| 324 | wpa_printf(MSG_DEBUG, "WPS: Updating in-memory configuration"); |
| 325 | |
| 326 | bss->wps_state = 2; |
Dmitry Shmidt | 9d9e602 | 2015-04-23 10:34:55 -0700 | [diff] [blame] | 327 | if (cred->ssid_len <= SSID_MAX_LEN) { |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 328 | os_memcpy(bss->ssid.ssid, cred->ssid, cred->ssid_len); |
| 329 | bss->ssid.ssid_len = cred->ssid_len; |
| 330 | bss->ssid.ssid_set = 1; |
| 331 | } |
| 332 | |
| 333 | if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) && |
| 334 | (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))) |
| 335 | bss->wpa = 3; |
| 336 | else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) |
| 337 | bss->wpa = 2; |
| 338 | else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)) |
| 339 | bss->wpa = 1; |
| 340 | else |
| 341 | bss->wpa = 0; |
| 342 | |
| 343 | if (bss->wpa) { |
| 344 | if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) |
| 345 | bss->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X; |
| 346 | if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK)) |
| 347 | bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK; |
| 348 | |
| 349 | bss->wpa_pairwise = 0; |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 350 | if (cred->encr_type & WPS_ENCR_AES) { |
| 351 | if (hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD) |
| 352 | bss->wpa_pairwise |= WPA_CIPHER_GCMP; |
| 353 | else |
| 354 | bss->wpa_pairwise |= WPA_CIPHER_CCMP; |
| 355 | } |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 356 | if (cred->encr_type & WPS_ENCR_TKIP) |
| 357 | bss->wpa_pairwise |= WPA_CIPHER_TKIP; |
| 358 | bss->rsn_pairwise = bss->wpa_pairwise; |
| 359 | bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa, |
| 360 | bss->wpa_pairwise, |
| 361 | bss->rsn_pairwise); |
| 362 | |
| 363 | if (cred->key_len >= 8 && cred->key_len < 64) { |
| 364 | os_free(bss->ssid.wpa_passphrase); |
| 365 | bss->ssid.wpa_passphrase = os_zalloc(cred->key_len + 1); |
| 366 | if (bss->ssid.wpa_passphrase) |
| 367 | os_memcpy(bss->ssid.wpa_passphrase, cred->key, |
| 368 | cred->key_len); |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 369 | hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk); |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 370 | } else if (cred->key_len == 64) { |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 371 | hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk); |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 372 | bss->ssid.wpa_psk = |
| 373 | os_zalloc(sizeof(struct hostapd_wpa_psk)); |
| 374 | if (bss->ssid.wpa_psk && |
| 375 | hexstr2bin((const char *) cred->key, |
| 376 | bss->ssid.wpa_psk->psk, PMK_LEN) == 0) { |
| 377 | bss->ssid.wpa_psk->group = 1; |
| 378 | os_free(bss->ssid.wpa_passphrase); |
| 379 | bss->ssid.wpa_passphrase = NULL; |
| 380 | } |
| 381 | } |
| 382 | bss->auth_algs = 1; |
| 383 | } else { |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 384 | /* |
| 385 | * WPS 2.0 does not allow WEP to be configured, so no need to |
| 386 | * process that option here either. |
| 387 | */ |
| 388 | bss->auth_algs = 1; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /* Schedule configuration reload after short period of time to allow |
| 392 | * EAP-WSC to be finished. |
| 393 | */ |
| 394 | eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface, |
| 395 | NULL); |
| 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 401 | static int hapd_wps_cred_cb(struct hostapd_data *hapd, void *ctx) |
| 402 | { |
| 403 | const struct wps_credential *cred = ctx; |
| 404 | FILE *oconf, *nconf; |
| 405 | size_t len, i; |
| 406 | char *tmp_fname; |
| 407 | char buf[1024]; |
| 408 | int multi_bss; |
| 409 | int wpa; |
| 410 | |
| 411 | if (hapd->wps == NULL) |
| 412 | return 0; |
| 413 | |
| 414 | wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute", |
| 415 | cred->cred_attr, cred->cred_attr_len); |
| 416 | |
| 417 | wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings"); |
| 418 | wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len); |
| 419 | wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x", |
| 420 | cred->auth_type); |
| 421 | wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type); |
| 422 | wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx); |
| 423 | wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key", |
| 424 | cred->key, cred->key_len); |
| 425 | wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR, |
| 426 | MAC2STR(cred->mac_addr)); |
| 427 | |
| 428 | if ((hapd->conf->wps_cred_processing == 1 || |
| 429 | hapd->conf->wps_cred_processing == 2) && cred->cred_attr) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 430 | hapd_new_ap_event(hapd, cred->cred_attr, cred->cred_attr_len); |
| 431 | } else if (hapd->conf->wps_cred_processing == 1 || |
| 432 | hapd->conf->wps_cred_processing == 2) { |
| 433 | struct wpabuf *attr; |
| 434 | attr = wpabuf_alloc(200); |
| 435 | if (attr && wps_build_credential_wrap(attr, cred) == 0) |
| 436 | hapd_new_ap_event(hapd, wpabuf_head_u8(attr), |
| 437 | wpabuf_len(attr)); |
| 438 | wpabuf_free(attr); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 439 | } else |
| 440 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS); |
| 441 | |
| 442 | if (hapd->conf->wps_cred_processing == 1) |
| 443 | return 0; |
| 444 | |
| 445 | os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len); |
| 446 | hapd->wps->ssid_len = cred->ssid_len; |
| 447 | hapd->wps->encr_types = cred->encr_type; |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame^] | 448 | hapd->wps->encr_types_rsn = cred->encr_type; |
| 449 | hapd->wps->encr_types_wpa = cred->encr_type; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 450 | hapd->wps->auth_types = cred->auth_type; |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 451 | hapd->wps->ap_encr_type = cred->encr_type; |
| 452 | hapd->wps->ap_auth_type = cred->auth_type; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 453 | if (cred->key_len == 0) { |
| 454 | os_free(hapd->wps->network_key); |
| 455 | hapd->wps->network_key = NULL; |
| 456 | hapd->wps->network_key_len = 0; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 457 | } else if ((cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK)) && |
| 458 | (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN)) { |
| 459 | wpa_printf(MSG_INFO, "WPS: Invalid key length %lu for WPA/WPA2", |
| 460 | (unsigned long) cred->key_len); |
| 461 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 462 | } else { |
| 463 | if (hapd->wps->network_key == NULL || |
| 464 | hapd->wps->network_key_len < cred->key_len) { |
| 465 | hapd->wps->network_key_len = 0; |
| 466 | os_free(hapd->wps->network_key); |
| 467 | hapd->wps->network_key = os_malloc(cred->key_len); |
| 468 | if (hapd->wps->network_key == NULL) |
| 469 | return -1; |
| 470 | } |
| 471 | hapd->wps->network_key_len = cred->key_len; |
| 472 | os_memcpy(hapd->wps->network_key, cred->key, cred->key_len); |
| 473 | } |
| 474 | hapd->wps->wps_state = WPS_STATE_CONFIGURED; |
| 475 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 476 | if (hapd->iface->config_fname == NULL) |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 477 | return hapd_wps_reconfig_in_memory(hapd, cred); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 478 | len = os_strlen(hapd->iface->config_fname) + 5; |
| 479 | tmp_fname = os_malloc(len); |
| 480 | if (tmp_fname == NULL) |
| 481 | return -1; |
| 482 | os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname); |
| 483 | |
| 484 | oconf = fopen(hapd->iface->config_fname, "r"); |
| 485 | if (oconf == NULL) { |
| 486 | wpa_printf(MSG_WARNING, "WPS: Could not open current " |
| 487 | "configuration file"); |
| 488 | os_free(tmp_fname); |
| 489 | return -1; |
| 490 | } |
| 491 | |
| 492 | nconf = fopen(tmp_fname, "w"); |
| 493 | if (nconf == NULL) { |
| 494 | wpa_printf(MSG_WARNING, "WPS: Could not write updated " |
| 495 | "configuration file"); |
| 496 | os_free(tmp_fname); |
| 497 | fclose(oconf); |
| 498 | return -1; |
| 499 | } |
| 500 | |
| 501 | fprintf(nconf, "# WPS configuration - START\n"); |
| 502 | |
| 503 | fprintf(nconf, "wps_state=2\n"); |
| 504 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 505 | if (is_hex(cred->ssid, cred->ssid_len)) { |
| 506 | fprintf(nconf, "ssid2="); |
| 507 | for (i = 0; i < cred->ssid_len; i++) |
| 508 | fprintf(nconf, "%02x", cred->ssid[i]); |
| 509 | fprintf(nconf, "\n"); |
| 510 | } else { |
| 511 | fprintf(nconf, "ssid="); |
| 512 | for (i = 0; i < cred->ssid_len; i++) |
| 513 | fputc(cred->ssid[i], nconf); |
| 514 | fprintf(nconf, "\n"); |
| 515 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 516 | |
| 517 | if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) && |
| 518 | (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))) |
| 519 | wpa = 3; |
| 520 | else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) |
| 521 | wpa = 2; |
| 522 | else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)) |
| 523 | wpa = 1; |
| 524 | else |
| 525 | wpa = 0; |
| 526 | |
| 527 | if (wpa) { |
| 528 | char *prefix; |
| 529 | fprintf(nconf, "wpa=%d\n", wpa); |
| 530 | |
| 531 | fprintf(nconf, "wpa_key_mgmt="); |
| 532 | prefix = ""; |
| 533 | if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) { |
| 534 | fprintf(nconf, "WPA-EAP"); |
| 535 | prefix = " "; |
| 536 | } |
| 537 | if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK)) |
| 538 | fprintf(nconf, "%sWPA-PSK", prefix); |
| 539 | fprintf(nconf, "\n"); |
| 540 | |
| 541 | fprintf(nconf, "wpa_pairwise="); |
| 542 | prefix = ""; |
| 543 | if (cred->encr_type & WPS_ENCR_AES) { |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 544 | if (hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD) |
| 545 | fprintf(nconf, "GCMP"); |
| 546 | else |
| 547 | fprintf(nconf, "CCMP"); |
| 548 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 549 | prefix = " "; |
| 550 | } |
| 551 | if (cred->encr_type & WPS_ENCR_TKIP) { |
| 552 | fprintf(nconf, "%sTKIP", prefix); |
| 553 | } |
| 554 | fprintf(nconf, "\n"); |
| 555 | |
| 556 | if (cred->key_len >= 8 && cred->key_len < 64) { |
| 557 | fprintf(nconf, "wpa_passphrase="); |
| 558 | for (i = 0; i < cred->key_len; i++) |
| 559 | fputc(cred->key[i], nconf); |
| 560 | fprintf(nconf, "\n"); |
| 561 | } else if (cred->key_len == 64) { |
| 562 | fprintf(nconf, "wpa_psk="); |
| 563 | for (i = 0; i < cred->key_len; i++) |
| 564 | fputc(cred->key[i], nconf); |
| 565 | fprintf(nconf, "\n"); |
| 566 | } else { |
| 567 | wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu " |
| 568 | "for WPA/WPA2", |
| 569 | (unsigned long) cred->key_len); |
| 570 | } |
| 571 | |
| 572 | fprintf(nconf, "auth_algs=1\n"); |
| 573 | } else { |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 574 | /* |
| 575 | * WPS 2.0 does not allow WEP to be configured, so no need to |
| 576 | * process that option here either. |
| 577 | */ |
| 578 | fprintf(nconf, "auth_algs=1\n"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | fprintf(nconf, "# WPS configuration - END\n"); |
| 582 | |
| 583 | multi_bss = 0; |
| 584 | while (fgets(buf, sizeof(buf), oconf)) { |
| 585 | if (os_strncmp(buf, "bss=", 4) == 0) |
| 586 | multi_bss = 1; |
| 587 | if (!multi_bss && |
| 588 | (str_starts(buf, "ssid=") || |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 589 | str_starts(buf, "ssid2=") || |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 590 | str_starts(buf, "auth_algs=") || |
| 591 | str_starts(buf, "wep_default_key=") || |
| 592 | str_starts(buf, "wep_key") || |
| 593 | str_starts(buf, "wps_state=") || |
| 594 | str_starts(buf, "wpa=") || |
| 595 | str_starts(buf, "wpa_psk=") || |
| 596 | str_starts(buf, "wpa_pairwise=") || |
| 597 | str_starts(buf, "rsn_pairwise=") || |
| 598 | str_starts(buf, "wpa_key_mgmt=") || |
| 599 | str_starts(buf, "wpa_passphrase="))) { |
| 600 | fprintf(nconf, "#WPS# %s", buf); |
| 601 | } else |
| 602 | fprintf(nconf, "%s", buf); |
| 603 | } |
| 604 | |
| 605 | fclose(nconf); |
| 606 | fclose(oconf); |
| 607 | |
| 608 | if (rename(tmp_fname, hapd->iface->config_fname) < 0) { |
| 609 | wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated " |
| 610 | "configuration file: %s", strerror(errno)); |
| 611 | os_free(tmp_fname); |
| 612 | return -1; |
| 613 | } |
| 614 | |
| 615 | os_free(tmp_fname); |
| 616 | |
| 617 | /* Schedule configuration reload after short period of time to allow |
| 618 | * EAP-WSC to be finished. |
| 619 | */ |
| 620 | eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface, |
| 621 | NULL); |
| 622 | |
| 623 | wpa_printf(MSG_DEBUG, "WPS: AP configuration updated"); |
| 624 | |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | |
| 629 | static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred) |
| 630 | { |
| 631 | struct hostapd_data *hapd = ctx; |
| 632 | return hostapd_wps_for_each(hapd, hapd_wps_cred_cb, (void *) cred); |
| 633 | } |
| 634 | |
| 635 | |
| 636 | static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx) |
| 637 | { |
| 638 | struct hostapd_data *hapd = eloop_data; |
| 639 | |
| 640 | if (hapd->conf->ap_setup_locked) |
| 641 | return; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 642 | if (hapd->ap_pin_failures_consecutive >= 10) |
| 643 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 644 | |
| 645 | wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN"); |
| 646 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED); |
| 647 | hapd->wps->ap_setup_locked = 0; |
| 648 | wps_registrar_update_ie(hapd->wps->registrar); |
| 649 | } |
| 650 | |
| 651 | |
| 652 | static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx) |
| 653 | { |
| 654 | struct wps_event_pwd_auth_fail *data = ctx; |
| 655 | |
| 656 | if (!data->enrollee || hapd->conf->ap_pin == NULL || hapd->wps == NULL) |
| 657 | return 0; |
| 658 | |
| 659 | /* |
| 660 | * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup |
| 661 | * for some time if this happens multiple times to slow down brute |
| 662 | * force attacks. |
| 663 | */ |
| 664 | hapd->ap_pin_failures++; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 665 | hapd->ap_pin_failures_consecutive++; |
| 666 | wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u " |
| 667 | "(%u consecutive)", |
| 668 | hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 669 | if (hapd->ap_pin_failures < 3) |
| 670 | return 0; |
| 671 | |
| 672 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED); |
| 673 | hapd->wps->ap_setup_locked = 1; |
| 674 | |
| 675 | wps_registrar_update_ie(hapd->wps->registrar); |
| 676 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 677 | if (!hapd->conf->ap_setup_locked && |
| 678 | hapd->ap_pin_failures_consecutive >= 10) { |
| 679 | /* |
| 680 | * In indefinite lockdown - disable automatic AP PIN |
| 681 | * reenablement. |
| 682 | */ |
| 683 | eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL); |
| 684 | wpa_printf(MSG_DEBUG, "WPS: AP PIN disabled indefinitely"); |
| 685 | } else if (!hapd->conf->ap_setup_locked) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 686 | if (hapd->ap_pin_lockout_time == 0) |
| 687 | hapd->ap_pin_lockout_time = 60; |
| 688 | else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 && |
| 689 | (hapd->ap_pin_failures % 3) == 0) |
| 690 | hapd->ap_pin_lockout_time *= 2; |
| 691 | |
| 692 | wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds", |
| 693 | hapd->ap_pin_lockout_time); |
| 694 | eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL); |
| 695 | eloop_register_timeout(hapd->ap_pin_lockout_time, 0, |
| 696 | hostapd_wps_reenable_ap_pin, hapd, |
| 697 | NULL); |
| 698 | } |
| 699 | |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | |
| 704 | static void hostapd_pwd_auth_fail(struct hostapd_data *hapd, |
| 705 | struct wps_event_pwd_auth_fail *data) |
| 706 | { |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 707 | /* Update WPS Status - Authentication Failure */ |
| 708 | wpa_printf(MSG_DEBUG, "WPS: Authentication failure update"); |
| 709 | hapd->wps_stats.status = WPS_STATUS_FAILURE; |
| 710 | hapd->wps_stats.failure_reason = WPS_EI_AUTH_FAILURE; |
| 711 | os_memcpy(hapd->wps_stats.peer_addr, data->peer_macaddr, ETH_ALEN); |
| 712 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 713 | hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data); |
| 714 | } |
| 715 | |
| 716 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 717 | static int wps_ap_pin_success(struct hostapd_data *hapd, void *ctx) |
| 718 | { |
| 719 | if (hapd->conf->ap_pin == NULL || hapd->wps == NULL) |
| 720 | return 0; |
| 721 | |
| 722 | if (hapd->ap_pin_failures_consecutive == 0) |
| 723 | return 0; |
| 724 | |
| 725 | wpa_printf(MSG_DEBUG, "WPS: Clear consecutive AP PIN failure counter " |
| 726 | "- total validation failures %u (%u consecutive)", |
| 727 | hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive); |
| 728 | hapd->ap_pin_failures_consecutive = 0; |
| 729 | |
| 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | |
| 734 | static void hostapd_wps_ap_pin_success(struct hostapd_data *hapd) |
| 735 | { |
| 736 | hostapd_wps_for_each(hapd, wps_ap_pin_success, NULL); |
| 737 | } |
| 738 | |
| 739 | |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 740 | static void hostapd_wps_event_pbc_overlap(struct hostapd_data *hapd) |
| 741 | { |
| 742 | /* Update WPS Status - PBC Overlap */ |
| 743 | hapd->wps_stats.pbc_status = WPS_PBC_STATUS_OVERLAP; |
| 744 | } |
| 745 | |
| 746 | |
| 747 | static void hostapd_wps_event_pbc_timeout(struct hostapd_data *hapd) |
| 748 | { |
| 749 | /* Update WPS PBC Status:PBC Timeout */ |
| 750 | hapd->wps_stats.pbc_status = WPS_PBC_STATUS_TIMEOUT; |
| 751 | } |
| 752 | |
| 753 | |
| 754 | static void hostapd_wps_event_pbc_active(struct hostapd_data *hapd) |
| 755 | { |
| 756 | /* Update WPS PBC status - Active */ |
| 757 | hapd->wps_stats.pbc_status = WPS_PBC_STATUS_ACTIVE; |
| 758 | } |
| 759 | |
| 760 | |
| 761 | static void hostapd_wps_event_pbc_disable(struct hostapd_data *hapd) |
| 762 | { |
| 763 | /* Update WPS PBC status - Active */ |
| 764 | hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE; |
| 765 | } |
| 766 | |
| 767 | |
| 768 | static void hostapd_wps_event_success(struct hostapd_data *hapd, |
| 769 | struct wps_event_success *success) |
| 770 | { |
| 771 | /* Update WPS status - Success */ |
| 772 | hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE; |
| 773 | hapd->wps_stats.status = WPS_STATUS_SUCCESS; |
| 774 | os_memcpy(hapd->wps_stats.peer_addr, success->peer_macaddr, ETH_ALEN); |
| 775 | } |
| 776 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 777 | |
| 778 | static void hostapd_wps_event_fail(struct hostapd_data *hapd, |
| 779 | struct wps_event_fail *fail) |
| 780 | { |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 781 | /* Update WPS status - Failure */ |
| 782 | hapd->wps_stats.status = WPS_STATUS_FAILURE; |
| 783 | os_memcpy(hapd->wps_stats.peer_addr, fail->peer_macaddr, ETH_ALEN); |
| 784 | |
| 785 | hapd->wps_stats.failure_reason = fail->error_indication; |
| 786 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 787 | if (fail->error_indication > 0 && |
| 788 | fail->error_indication < NUM_WPS_EI_VALUES) { |
| 789 | wpa_msg(hapd->msg_ctx, MSG_INFO, |
| 790 | WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)", |
| 791 | fail->msg, fail->config_error, fail->error_indication, |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 792 | wps_ei_str(fail->error_indication)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 793 | } else { |
| 794 | wpa_msg(hapd->msg_ctx, MSG_INFO, |
| 795 | WPS_EVENT_FAIL "msg=%d config_error=%d", |
| 796 | fail->msg, fail->config_error); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | |
| 801 | static void hostapd_wps_event_cb(void *ctx, enum wps_event event, |
| 802 | union wps_event_data *data) |
| 803 | { |
| 804 | struct hostapd_data *hapd = ctx; |
| 805 | |
| 806 | switch (event) { |
| 807 | case WPS_EV_M2D: |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 808 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_M2D); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 809 | break; |
| 810 | case WPS_EV_FAIL: |
| 811 | hostapd_wps_event_fail(hapd, &data->fail); |
| 812 | break; |
| 813 | case WPS_EV_SUCCESS: |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 814 | hostapd_wps_event_success(hapd, &data->success); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 815 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_SUCCESS); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 816 | break; |
| 817 | case WPS_EV_PWD_AUTH_FAIL: |
| 818 | hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail); |
| 819 | break; |
| 820 | case WPS_EV_PBC_OVERLAP: |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 821 | hostapd_wps_event_pbc_overlap(hapd); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 822 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_OVERLAP); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 823 | break; |
| 824 | case WPS_EV_PBC_TIMEOUT: |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 825 | hostapd_wps_event_pbc_timeout(hapd); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 826 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_TIMEOUT); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 827 | break; |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 828 | case WPS_EV_PBC_ACTIVE: |
| 829 | hostapd_wps_event_pbc_active(hapd); |
| 830 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ACTIVE); |
| 831 | break; |
| 832 | case WPS_EV_PBC_DISABLE: |
| 833 | hostapd_wps_event_pbc_disable(hapd); |
| 834 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_DISABLE); |
| 835 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 836 | case WPS_EV_ER_AP_ADD: |
| 837 | break; |
| 838 | case WPS_EV_ER_AP_REMOVE: |
| 839 | break; |
| 840 | case WPS_EV_ER_ENROLLEE_ADD: |
| 841 | break; |
| 842 | case WPS_EV_ER_ENROLLEE_REMOVE: |
| 843 | break; |
| 844 | case WPS_EV_ER_AP_SETTINGS: |
| 845 | break; |
| 846 | case WPS_EV_ER_SET_SELECTED_REGISTRAR: |
| 847 | break; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 848 | case WPS_EV_AP_PIN_SUCCESS: |
| 849 | hostapd_wps_ap_pin_success(hapd); |
| 850 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 851 | } |
| 852 | if (hapd->wps_event_cb) |
| 853 | hapd->wps_event_cb(hapd->wps_event_cb_ctx, event, data); |
| 854 | } |
| 855 | |
| 856 | |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 857 | static int hostapd_wps_rf_band_cb(void *ctx) |
| 858 | { |
| 859 | struct hostapd_data *hapd = ctx; |
| 860 | |
| 861 | return hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ? |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 862 | WPS_RF_50GHZ : |
| 863 | hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD ? |
| 864 | WPS_RF_60GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */ |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 868 | static void hostapd_wps_clear_ies(struct hostapd_data *hapd, int deinit_only) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 869 | { |
| 870 | wpabuf_free(hapd->wps_beacon_ie); |
| 871 | hapd->wps_beacon_ie = NULL; |
| 872 | |
| 873 | wpabuf_free(hapd->wps_probe_resp_ie); |
| 874 | hapd->wps_probe_resp_ie = NULL; |
| 875 | |
Dmitry Shmidt | dda10c2 | 2015-03-24 16:05:01 -0700 | [diff] [blame] | 876 | if (deinit_only) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 877 | if (hapd->drv_priv) |
| 878 | hostapd_reset_ap_wps_ie(hapd); |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 879 | return; |
Dmitry Shmidt | dda10c2 | 2015-03-24 16:05:01 -0700 | [diff] [blame] | 880 | } |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 881 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 882 | hostapd_set_ap_wps_ie(hapd); |
| 883 | } |
| 884 | |
| 885 | |
| 886 | static int get_uuid_cb(struct hostapd_iface *iface, void *ctx) |
| 887 | { |
| 888 | const u8 **uuid = ctx; |
| 889 | size_t j; |
| 890 | |
| 891 | if (iface == NULL) |
| 892 | return 0; |
| 893 | for (j = 0; j < iface->num_bss; j++) { |
| 894 | struct hostapd_data *hapd = iface->bss[j]; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 895 | if (hapd->wps && !hapd->conf->wps_independent && |
| 896 | !is_nil_uuid(hapd->wps->uuid)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 897 | *uuid = hapd->wps->uuid; |
| 898 | return 1; |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | return 0; |
| 903 | } |
| 904 | |
| 905 | |
| 906 | static const u8 * get_own_uuid(struct hostapd_iface *iface) |
| 907 | { |
| 908 | const u8 *uuid; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 909 | if (iface->interfaces == NULL || |
| 910 | iface->interfaces->for_each_interface == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 911 | return NULL; |
| 912 | uuid = NULL; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 913 | iface->interfaces->for_each_interface(iface->interfaces, get_uuid_cb, |
| 914 | &uuid); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 915 | return uuid; |
| 916 | } |
| 917 | |
| 918 | |
| 919 | static int count_interface_cb(struct hostapd_iface *iface, void *ctx) |
| 920 | { |
| 921 | int *count= ctx; |
| 922 | (*count)++; |
| 923 | return 0; |
| 924 | } |
| 925 | |
| 926 | |
| 927 | static int interface_count(struct hostapd_iface *iface) |
| 928 | { |
| 929 | int count = 0; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 930 | if (iface->interfaces == NULL || |
| 931 | iface->interfaces->for_each_interface == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 932 | return 0; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 933 | iface->interfaces->for_each_interface(iface->interfaces, |
| 934 | count_interface_cb, &count); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 935 | return count; |
| 936 | } |
| 937 | |
| 938 | |
| 939 | static int hostapd_wps_set_vendor_ext(struct hostapd_data *hapd, |
| 940 | struct wps_context *wps) |
| 941 | { |
| 942 | int i; |
| 943 | |
| 944 | for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) { |
| 945 | wpabuf_free(wps->dev.vendor_ext[i]); |
| 946 | wps->dev.vendor_ext[i] = NULL; |
| 947 | |
| 948 | if (hapd->conf->wps_vendor_ext[i] == NULL) |
| 949 | continue; |
| 950 | |
| 951 | wps->dev.vendor_ext[i] = |
| 952 | wpabuf_dup(hapd->conf->wps_vendor_ext[i]); |
| 953 | if (wps->dev.vendor_ext[i] == NULL) { |
| 954 | while (--i >= 0) |
| 955 | wpabuf_free(wps->dev.vendor_ext[i]); |
| 956 | return -1; |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | return 0; |
| 961 | } |
| 962 | |
| 963 | |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 964 | static void hostapd_free_wps(struct wps_context *wps) |
| 965 | { |
| 966 | int i; |
| 967 | |
| 968 | for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) |
| 969 | wpabuf_free(wps->dev.vendor_ext[i]); |
| 970 | wps_device_data_free(&wps->dev); |
| 971 | os_free(wps->network_key); |
| 972 | hostapd_wps_nfc_clear(wps); |
| 973 | wpabuf_free(wps->dh_pubkey); |
| 974 | wpabuf_free(wps->dh_privkey); |
| 975 | os_free(wps); |
| 976 | } |
| 977 | |
| 978 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 979 | int hostapd_init_wps(struct hostapd_data *hapd, |
| 980 | struct hostapd_bss_config *conf) |
| 981 | { |
| 982 | struct wps_context *wps; |
| 983 | struct wps_registrar_config cfg; |
| 984 | |
| 985 | if (conf->wps_state == 0) { |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 986 | hostapd_wps_clear_ies(hapd, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 987 | return 0; |
| 988 | } |
| 989 | |
| 990 | wps = os_zalloc(sizeof(*wps)); |
| 991 | if (wps == NULL) |
| 992 | return -1; |
| 993 | |
| 994 | wps->cred_cb = hostapd_wps_cred_cb; |
| 995 | wps->event_cb = hostapd_wps_event_cb; |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 996 | wps->rf_band_cb = hostapd_wps_rf_band_cb; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 997 | wps->cb_ctx = hapd; |
| 998 | |
| 999 | os_memset(&cfg, 0, sizeof(cfg)); |
| 1000 | wps->wps_state = hapd->conf->wps_state; |
| 1001 | wps->ap_setup_locked = hapd->conf->ap_setup_locked; |
| 1002 | if (is_nil_uuid(hapd->conf->uuid)) { |
| 1003 | const u8 *uuid; |
| 1004 | uuid = get_own_uuid(hapd->iface); |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 1005 | if (uuid && !conf->wps_independent) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1006 | os_memcpy(wps->uuid, uuid, UUID_LEN); |
| 1007 | wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another " |
| 1008 | "interface", wps->uuid, UUID_LEN); |
| 1009 | } else { |
| 1010 | uuid_gen_mac_addr(hapd->own_addr, wps->uuid); |
| 1011 | wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC " |
| 1012 | "address", wps->uuid, UUID_LEN); |
| 1013 | } |
| 1014 | } else { |
| 1015 | os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN); |
| 1016 | wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID", |
| 1017 | wps->uuid, UUID_LEN); |
| 1018 | } |
| 1019 | wps->ssid_len = hapd->conf->ssid.ssid_len; |
| 1020 | os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len); |
| 1021 | wps->ap = 1; |
| 1022 | os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN); |
| 1023 | wps->dev.device_name = hapd->conf->device_name ? |
| 1024 | os_strdup(hapd->conf->device_name) : NULL; |
| 1025 | wps->dev.manufacturer = hapd->conf->manufacturer ? |
| 1026 | os_strdup(hapd->conf->manufacturer) : NULL; |
| 1027 | wps->dev.model_name = hapd->conf->model_name ? |
| 1028 | os_strdup(hapd->conf->model_name) : NULL; |
| 1029 | wps->dev.model_number = hapd->conf->model_number ? |
| 1030 | os_strdup(hapd->conf->model_number) : NULL; |
| 1031 | wps->dev.serial_number = hapd->conf->serial_number ? |
| 1032 | os_strdup(hapd->conf->serial_number) : NULL; |
| 1033 | wps->config_methods = |
| 1034 | wps_config_methods_str2bin(hapd->conf->config_methods); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1035 | if ((wps->config_methods & |
| 1036 | (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY | |
| 1037 | WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) { |
| 1038 | wpa_printf(MSG_INFO, "WPS: Converting display to " |
| 1039 | "virtual_display for WPS 2.0 compliance"); |
| 1040 | wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY; |
| 1041 | } |
| 1042 | if ((wps->config_methods & |
| 1043 | (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON | |
| 1044 | WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) { |
| 1045 | wpa_printf(MSG_INFO, "WPS: Converting push_button to " |
| 1046 | "virtual_push_button for WPS 2.0 compliance"); |
| 1047 | wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON; |
| 1048 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1049 | os_memcpy(wps->dev.pri_dev_type, hapd->conf->device_type, |
| 1050 | WPS_DEV_TYPE_LEN); |
| 1051 | |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 1052 | if (hostapd_wps_set_vendor_ext(hapd, wps) < 0) |
| 1053 | goto fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1054 | |
| 1055 | wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1056 | |
| 1057 | if (conf->wps_rf_bands) { |
| 1058 | wps->dev.rf_bands = conf->wps_rf_bands; |
| 1059 | } else { |
| 1060 | wps->dev.rf_bands = |
| 1061 | hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ? |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 1062 | WPS_RF_50GHZ : |
| 1063 | hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD ? |
| 1064 | WPS_RF_60GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1065 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1066 | |
| 1067 | if (conf->wpa & WPA_PROTO_RSN) { |
| 1068 | if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) |
| 1069 | wps->auth_types |= WPS_AUTH_WPA2PSK; |
| 1070 | if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) |
| 1071 | wps->auth_types |= WPS_AUTH_WPA2; |
| 1072 | |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame^] | 1073 | if (conf->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1074 | wps->encr_types |= WPS_ENCR_AES; |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame^] | 1075 | wps->encr_types_rsn |= WPS_ENCR_AES; |
| 1076 | } |
| 1077 | if (conf->rsn_pairwise & WPA_CIPHER_TKIP) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1078 | wps->encr_types |= WPS_ENCR_TKIP; |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame^] | 1079 | wps->encr_types_rsn |= WPS_ENCR_TKIP; |
| 1080 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | if (conf->wpa & WPA_PROTO_WPA) { |
| 1084 | if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) |
| 1085 | wps->auth_types |= WPS_AUTH_WPAPSK; |
| 1086 | if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) |
| 1087 | wps->auth_types |= WPS_AUTH_WPA; |
| 1088 | |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame^] | 1089 | if (conf->wpa_pairwise & WPA_CIPHER_CCMP) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1090 | wps->encr_types |= WPS_ENCR_AES; |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame^] | 1091 | wps->encr_types_wpa |= WPS_ENCR_AES; |
| 1092 | } |
| 1093 | if (conf->wpa_pairwise & WPA_CIPHER_TKIP) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1094 | wps->encr_types |= WPS_ENCR_TKIP; |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame^] | 1095 | wps->encr_types_wpa |= WPS_ENCR_TKIP; |
| 1096 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | if (conf->ssid.security_policy == SECURITY_PLAINTEXT) { |
| 1100 | wps->encr_types |= WPS_ENCR_NONE; |
| 1101 | wps->auth_types |= WPS_AUTH_OPEN; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | if (conf->ssid.wpa_psk_file) { |
| 1105 | /* Use per-device PSKs */ |
| 1106 | } else if (conf->ssid.wpa_passphrase) { |
| 1107 | wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase); |
| 1108 | wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase); |
| 1109 | } else if (conf->ssid.wpa_psk) { |
| 1110 | wps->network_key = os_malloc(2 * PMK_LEN + 1); |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 1111 | if (wps->network_key == NULL) |
| 1112 | goto fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1113 | wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1, |
| 1114 | conf->ssid.wpa_psk->psk, PMK_LEN); |
| 1115 | wps->network_key_len = 2 * PMK_LEN; |
| 1116 | } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) { |
| 1117 | wps->network_key = os_malloc(conf->ssid.wep.len[0]); |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 1118 | if (wps->network_key == NULL) |
| 1119 | goto fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1120 | os_memcpy(wps->network_key, conf->ssid.wep.key[0], |
| 1121 | conf->ssid.wep.len[0]); |
| 1122 | wps->network_key_len = conf->ssid.wep.len[0]; |
| 1123 | } |
| 1124 | |
| 1125 | if (conf->ssid.wpa_psk) { |
| 1126 | os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN); |
| 1127 | wps->psk_set = 1; |
| 1128 | } |
| 1129 | |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 1130 | wps->ap_auth_type = wps->auth_types; |
| 1131 | wps->ap_encr_type = wps->encr_types; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1132 | if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) { |
| 1133 | /* Override parameters to enable security by default */ |
| 1134 | wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK; |
| 1135 | wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP; |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame^] | 1136 | wps->encr_types_rsn = WPS_ENCR_AES | WPS_ENCR_TKIP; |
| 1137 | wps->encr_types_wpa = WPS_ENCR_AES | WPS_ENCR_TKIP; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | wps->ap_settings = conf->ap_settings; |
| 1141 | wps->ap_settings_len = conf->ap_settings_len; |
| 1142 | |
| 1143 | cfg.new_psk_cb = hostapd_wps_new_psk_cb; |
| 1144 | cfg.set_ie_cb = hostapd_wps_set_ie_cb; |
| 1145 | cfg.pin_needed_cb = hostapd_wps_pin_needed_cb; |
| 1146 | cfg.reg_success_cb = hostapd_wps_reg_success_cb; |
| 1147 | cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb; |
| 1148 | cfg.cb_ctx = hapd; |
| 1149 | cfg.skip_cred_build = conf->skip_cred_build; |
| 1150 | cfg.extra_cred = conf->extra_cred; |
| 1151 | cfg.extra_cred_len = conf->extra_cred_len; |
| 1152 | cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) && |
| 1153 | conf->skip_cred_build; |
| 1154 | if (conf->ssid.security_policy == SECURITY_STATIC_WEP) |
| 1155 | cfg.static_wep_only = 1; |
| 1156 | cfg.dualband = interface_count(hapd->iface) > 1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1157 | if ((wps->dev.rf_bands & (WPS_RF_50GHZ | WPS_RF_24GHZ)) == |
| 1158 | (WPS_RF_50GHZ | WPS_RF_24GHZ)) |
| 1159 | cfg.dualband = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1160 | if (cfg.dualband) |
| 1161 | wpa_printf(MSG_DEBUG, "WPS: Dualband AP"); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1162 | cfg.force_per_enrollee_psk = conf->force_per_enrollee_psk; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1163 | |
| 1164 | wps->registrar = wps_registrar_init(wps, &cfg); |
| 1165 | if (wps->registrar == NULL) { |
| 1166 | wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar"); |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 1167 | goto fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | #ifdef CONFIG_WPS_UPNP |
| 1171 | wps->friendly_name = hapd->conf->friendly_name; |
| 1172 | wps->manufacturer_url = hapd->conf->manufacturer_url; |
| 1173 | wps->model_description = hapd->conf->model_description; |
| 1174 | wps->model_url = hapd->conf->model_url; |
| 1175 | wps->upc = hapd->conf->upc; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1176 | #endif /* CONFIG_WPS_UPNP */ |
| 1177 | |
| 1178 | hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd); |
| 1179 | |
| 1180 | hapd->wps = wps; |
| 1181 | |
| 1182 | return 0; |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 1183 | |
| 1184 | fail: |
| 1185 | hostapd_free_wps(wps); |
| 1186 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | |
Jouni Malinen | 87fd279 | 2011-05-16 18:35:42 +0300 | [diff] [blame] | 1190 | int hostapd_init_wps_complete(struct hostapd_data *hapd) |
| 1191 | { |
| 1192 | struct wps_context *wps = hapd->wps; |
| 1193 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1194 | if (wps == NULL) |
Jouni Malinen | 87fd279 | 2011-05-16 18:35:42 +0300 | [diff] [blame] | 1195 | return 0; |
| 1196 | |
| 1197 | #ifdef CONFIG_WPS_UPNP |
| 1198 | if (hostapd_wps_upnp_init(hapd, wps) < 0) { |
| 1199 | wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP"); |
| 1200 | wps_registrar_deinit(wps->registrar); |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 1201 | hostapd_free_wps(wps); |
Jouni Malinen | 87fd279 | 2011-05-16 18:35:42 +0300 | [diff] [blame] | 1202 | hapd->wps = NULL; |
| 1203 | return -1; |
| 1204 | } |
| 1205 | #endif /* CONFIG_WPS_UPNP */ |
| 1206 | |
| 1207 | return 0; |
| 1208 | } |
| 1209 | |
| 1210 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1211 | static void hostapd_wps_nfc_clear(struct wps_context *wps) |
| 1212 | { |
| 1213 | #ifdef CONFIG_WPS_NFC |
Dmitry Shmidt | 5460547 | 2013-11-08 11:10:19 -0800 | [diff] [blame] | 1214 | wpa_printf(MSG_DEBUG, "WPS: Clear NFC Tag context %p", wps); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1215 | wps->ap_nfc_dev_pw_id = 0; |
| 1216 | wpabuf_free(wps->ap_nfc_dh_pubkey); |
| 1217 | wps->ap_nfc_dh_pubkey = NULL; |
| 1218 | wpabuf_free(wps->ap_nfc_dh_privkey); |
| 1219 | wps->ap_nfc_dh_privkey = NULL; |
| 1220 | wpabuf_free(wps->ap_nfc_dev_pw); |
| 1221 | wps->ap_nfc_dev_pw = NULL; |
| 1222 | #endif /* CONFIG_WPS_NFC */ |
| 1223 | } |
| 1224 | |
| 1225 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1226 | void hostapd_deinit_wps(struct hostapd_data *hapd) |
| 1227 | { |
| 1228 | eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL); |
| 1229 | eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1230 | eloop_cancel_timeout(wps_reload_config, hapd->iface, NULL); |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 1231 | if (hapd->wps == NULL) { |
| 1232 | hostapd_wps_clear_ies(hapd, 1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1233 | return; |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 1234 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1235 | #ifdef CONFIG_WPS_UPNP |
| 1236 | hostapd_wps_upnp_deinit(hapd); |
| 1237 | #endif /* CONFIG_WPS_UPNP */ |
| 1238 | wps_registrar_deinit(hapd->wps->registrar); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1239 | wps_free_pending_msgs(hapd->wps->upnp_msgs); |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 1240 | hostapd_free_wps(hapd->wps); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1241 | hapd->wps = NULL; |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 1242 | hostapd_wps_clear_ies(hapd, 1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | |
| 1246 | void hostapd_update_wps(struct hostapd_data *hapd) |
| 1247 | { |
| 1248 | if (hapd->wps == NULL) |
| 1249 | return; |
| 1250 | |
| 1251 | #ifdef CONFIG_WPS_UPNP |
| 1252 | hapd->wps->friendly_name = hapd->conf->friendly_name; |
| 1253 | hapd->wps->manufacturer_url = hapd->conf->manufacturer_url; |
| 1254 | hapd->wps->model_description = hapd->conf->model_description; |
| 1255 | hapd->wps->model_url = hapd->conf->model_url; |
| 1256 | hapd->wps->upc = hapd->conf->upc; |
| 1257 | #endif /* CONFIG_WPS_UPNP */ |
| 1258 | |
| 1259 | hostapd_wps_set_vendor_ext(hapd, hapd->wps); |
| 1260 | |
| 1261 | if (hapd->conf->wps_state) |
| 1262 | wps_registrar_update_ie(hapd->wps->registrar); |
| 1263 | else |
| 1264 | hostapd_deinit_wps(hapd); |
| 1265 | } |
| 1266 | |
| 1267 | |
| 1268 | struct wps_add_pin_data { |
| 1269 | const u8 *addr; |
| 1270 | const u8 *uuid; |
| 1271 | const u8 *pin; |
| 1272 | size_t pin_len; |
| 1273 | int timeout; |
| 1274 | int added; |
| 1275 | }; |
| 1276 | |
| 1277 | |
| 1278 | static int wps_add_pin(struct hostapd_data *hapd, void *ctx) |
| 1279 | { |
| 1280 | struct wps_add_pin_data *data = ctx; |
| 1281 | int ret; |
| 1282 | |
| 1283 | if (hapd->wps == NULL) |
| 1284 | return 0; |
| 1285 | ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr, |
| 1286 | data->uuid, data->pin, data->pin_len, |
| 1287 | data->timeout); |
| 1288 | if (ret == 0) |
| 1289 | data->added++; |
| 1290 | return ret; |
| 1291 | } |
| 1292 | |
| 1293 | |
| 1294 | int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr, |
| 1295 | const char *uuid, const char *pin, int timeout) |
| 1296 | { |
| 1297 | u8 u[UUID_LEN]; |
| 1298 | struct wps_add_pin_data data; |
| 1299 | |
| 1300 | data.addr = addr; |
| 1301 | data.uuid = u; |
| 1302 | data.pin = (const u8 *) pin; |
| 1303 | data.pin_len = os_strlen(pin); |
| 1304 | data.timeout = timeout; |
| 1305 | data.added = 0; |
| 1306 | |
| 1307 | if (os_strcmp(uuid, "any") == 0) |
| 1308 | data.uuid = NULL; |
| 1309 | else { |
| 1310 | if (uuid_str2bin(uuid, u)) |
| 1311 | return -1; |
| 1312 | data.uuid = u; |
| 1313 | } |
| 1314 | if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0) |
| 1315 | return -1; |
| 1316 | return data.added ? 0 : -1; |
| 1317 | } |
| 1318 | |
| 1319 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1320 | struct wps_button_pushed_ctx { |
| 1321 | const u8 *p2p_dev_addr; |
| 1322 | unsigned int count; |
| 1323 | }; |
| 1324 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1325 | static int wps_button_pushed(struct hostapd_data *hapd, void *ctx) |
| 1326 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1327 | struct wps_button_pushed_ctx *data = ctx; |
| 1328 | |
| 1329 | if (hapd->wps) { |
| 1330 | data->count++; |
| 1331 | return wps_registrar_button_pushed(hapd->wps->registrar, |
| 1332 | data->p2p_dev_addr); |
| 1333 | } |
| 1334 | |
| 1335 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | |
| 1339 | int hostapd_wps_button_pushed(struct hostapd_data *hapd, |
| 1340 | const u8 *p2p_dev_addr) |
| 1341 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1342 | struct wps_button_pushed_ctx ctx; |
| 1343 | int ret; |
| 1344 | |
| 1345 | os_memset(&ctx, 0, sizeof(ctx)); |
| 1346 | ctx.p2p_dev_addr = p2p_dev_addr; |
| 1347 | ret = hostapd_wps_for_each(hapd, wps_button_pushed, &ctx); |
| 1348 | if (ret == 0 && !ctx.count) |
| 1349 | ret = -1; |
| 1350 | return ret; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1354 | struct wps_cancel_ctx { |
| 1355 | unsigned int count; |
| 1356 | }; |
| 1357 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1358 | static int wps_cancel(struct hostapd_data *hapd, void *ctx) |
| 1359 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1360 | struct wps_cancel_ctx *data = ctx; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1361 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1362 | if (hapd->wps) { |
| 1363 | data->count++; |
| 1364 | wps_registrar_wps_cancel(hapd->wps->registrar); |
| 1365 | ap_for_each_sta(hapd, ap_sta_wps_cancel, NULL); |
| 1366 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1367 | |
| 1368 | return 0; |
| 1369 | } |
| 1370 | |
| 1371 | |
| 1372 | int hostapd_wps_cancel(struct hostapd_data *hapd) |
| 1373 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1374 | struct wps_cancel_ctx ctx; |
| 1375 | int ret; |
| 1376 | |
| 1377 | os_memset(&ctx, 0, sizeof(ctx)); |
| 1378 | ret = hostapd_wps_for_each(hapd, wps_cancel, &ctx); |
| 1379 | if (ret == 0 && !ctx.count) |
| 1380 | ret = -1; |
| 1381 | return ret; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1385 | static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da, |
| 1386 | const u8 *bssid, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1387 | const u8 *ie, size_t ie_len, |
| 1388 | int ssi_signal) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1389 | { |
| 1390 | struct hostapd_data *hapd = ctx; |
| 1391 | struct wpabuf *wps_ie; |
| 1392 | struct ieee802_11_elems elems; |
| 1393 | |
| 1394 | if (hapd->wps == NULL) |
| 1395 | return 0; |
| 1396 | |
| 1397 | if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) { |
| 1398 | wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from " |
| 1399 | MACSTR, MAC2STR(addr)); |
| 1400 | return 0; |
| 1401 | } |
| 1402 | |
| 1403 | if (elems.ssid && elems.ssid_len > 0 && |
| 1404 | (elems.ssid_len != hapd->conf->ssid.ssid_len || |
| 1405 | os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) != |
| 1406 | 0)) |
| 1407 | return 0; /* Not for us */ |
| 1408 | |
| 1409 | wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA); |
| 1410 | if (wps_ie == NULL) |
| 1411 | return 0; |
| 1412 | if (wps_validate_probe_req(wps_ie, addr) < 0) { |
| 1413 | wpabuf_free(wps_ie); |
| 1414 | return 0; |
| 1415 | } |
| 1416 | |
| 1417 | if (wpabuf_len(wps_ie) > 0) { |
| 1418 | int p2p_wildcard = 0; |
| 1419 | #ifdef CONFIG_P2P |
| 1420 | if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN && |
| 1421 | os_memcmp(elems.ssid, P2P_WILDCARD_SSID, |
| 1422 | P2P_WILDCARD_SSID_LEN) == 0) |
| 1423 | p2p_wildcard = 1; |
| 1424 | #endif /* CONFIG_P2P */ |
| 1425 | wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie, |
| 1426 | p2p_wildcard); |
| 1427 | #ifdef CONFIG_WPS_UPNP |
| 1428 | /* FIX: what exactly should be included in the WLANEvent? |
| 1429 | * WPS attributes? Full ProbeReq frame? */ |
| 1430 | if (!p2p_wildcard) |
| 1431 | upnp_wps_device_send_wlan_event( |
| 1432 | hapd->wps_upnp, addr, |
| 1433 | UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie); |
| 1434 | #endif /* CONFIG_WPS_UPNP */ |
| 1435 | } |
| 1436 | |
| 1437 | wpabuf_free(wps_ie); |
| 1438 | |
| 1439 | return 0; |
| 1440 | } |
| 1441 | |
| 1442 | |
| 1443 | #ifdef CONFIG_WPS_UPNP |
| 1444 | |
| 1445 | static int hostapd_rx_req_put_wlan_response( |
| 1446 | void *priv, enum upnp_wps_wlanevent_type ev_type, |
| 1447 | const u8 *mac_addr, const struct wpabuf *msg, |
| 1448 | enum wps_msg_type msg_type) |
| 1449 | { |
| 1450 | struct hostapd_data *hapd = priv; |
| 1451 | struct sta_info *sta; |
| 1452 | struct upnp_pending_message *p; |
| 1453 | |
| 1454 | wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr=" |
| 1455 | MACSTR, ev_type, MAC2STR(mac_addr)); |
| 1456 | wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage", |
| 1457 | wpabuf_head(msg), wpabuf_len(msg)); |
| 1458 | if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) { |
| 1459 | wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected " |
| 1460 | "PutWLANResponse WLANEventType %d", ev_type); |
| 1461 | return -1; |
| 1462 | } |
| 1463 | |
| 1464 | /* |
| 1465 | * EAP response to ongoing to WPS Registration. Send it to EAP-WSC |
| 1466 | * server implementation for delivery to the peer. |
| 1467 | */ |
| 1468 | |
| 1469 | sta = ap_get_sta(hapd, mac_addr); |
| 1470 | #ifndef CONFIG_WPS_STRICT |
| 1471 | if (!sta) { |
| 1472 | /* |
| 1473 | * Workaround - Intel wsccmd uses bogus NewWLANEventMAC: |
| 1474 | * Pick STA that is in an ongoing WPS registration without |
| 1475 | * checking the MAC address. |
| 1476 | */ |
| 1477 | wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based " |
| 1478 | "on NewWLANEventMAC; try wildcard match"); |
| 1479 | for (sta = hapd->sta_list; sta; sta = sta->next) { |
| 1480 | if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS)) |
| 1481 | break; |
| 1482 | } |
| 1483 | } |
| 1484 | #endif /* CONFIG_WPS_STRICT */ |
| 1485 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1486 | if (!sta || !(sta->flags & WLAN_STA_WPS)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1487 | wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found"); |
| 1488 | return 0; |
| 1489 | } |
| 1490 | |
Dmitry Shmidt | 13ca8d8 | 2014-02-20 10:18:40 -0800 | [diff] [blame] | 1491 | if (!sta->eapol_sm) { |
| 1492 | /* |
| 1493 | * This can happen, e.g., if an ER sends an extra message after |
| 1494 | * the station has disassociated (but not fully |
| 1495 | * deauthenticated). |
| 1496 | */ |
| 1497 | wpa_printf(MSG_DEBUG, "WPS UPnP: Matching STA did not have EAPOL state machine initialized"); |
| 1498 | return 0; |
| 1499 | } |
| 1500 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1501 | p = os_zalloc(sizeof(*p)); |
| 1502 | if (p == NULL) |
| 1503 | return -1; |
| 1504 | os_memcpy(p->addr, sta->addr, ETH_ALEN); |
| 1505 | p->msg = wpabuf_dup(msg); |
| 1506 | p->type = msg_type; |
| 1507 | p->next = hapd->wps->upnp_msgs; |
| 1508 | hapd->wps->upnp_msgs = p; |
| 1509 | |
| 1510 | return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap); |
| 1511 | } |
| 1512 | |
| 1513 | |
| 1514 | static int hostapd_wps_upnp_init(struct hostapd_data *hapd, |
| 1515 | struct wps_context *wps) |
| 1516 | { |
| 1517 | struct upnp_wps_device_ctx *ctx; |
| 1518 | |
| 1519 | if (!hapd->conf->upnp_iface) |
| 1520 | return 0; |
| 1521 | ctx = os_zalloc(sizeof(*ctx)); |
| 1522 | if (ctx == NULL) |
| 1523 | return -1; |
| 1524 | |
| 1525 | ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response; |
| 1526 | if (hapd->conf->ap_pin) |
| 1527 | ctx->ap_pin = os_strdup(hapd->conf->ap_pin); |
| 1528 | |
| 1529 | hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd, |
| 1530 | hapd->conf->upnp_iface); |
| 1531 | if (hapd->wps_upnp == NULL) |
| 1532 | return -1; |
| 1533 | wps->wps_upnp = hapd->wps_upnp; |
| 1534 | |
| 1535 | return 0; |
| 1536 | } |
| 1537 | |
| 1538 | |
| 1539 | static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd) |
| 1540 | { |
| 1541 | upnp_wps_device_deinit(hapd->wps_upnp, hapd); |
| 1542 | } |
| 1543 | |
| 1544 | #endif /* CONFIG_WPS_UPNP */ |
| 1545 | |
| 1546 | |
| 1547 | int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr, |
| 1548 | char *buf, size_t buflen) |
| 1549 | { |
| 1550 | if (hapd->wps == NULL) |
| 1551 | return 0; |
| 1552 | return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen); |
| 1553 | } |
| 1554 | |
| 1555 | |
| 1556 | static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx) |
| 1557 | { |
| 1558 | struct hostapd_data *hapd = eloop_data; |
| 1559 | wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out"); |
| 1560 | hostapd_wps_ap_pin_disable(hapd); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1561 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_PIN_DISABLED); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | |
| 1565 | static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout) |
| 1566 | { |
| 1567 | wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout); |
| 1568 | hapd->ap_pin_failures = 0; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1569 | hapd->ap_pin_failures_consecutive = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1570 | hapd->conf->ap_setup_locked = 0; |
| 1571 | if (hapd->wps->ap_setup_locked) { |
| 1572 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED); |
| 1573 | hapd->wps->ap_setup_locked = 0; |
| 1574 | wps_registrar_update_ie(hapd->wps->registrar); |
| 1575 | } |
| 1576 | eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL); |
| 1577 | if (timeout > 0) |
| 1578 | eloop_register_timeout(timeout, 0, |
| 1579 | hostapd_wps_ap_pin_timeout, hapd, NULL); |
| 1580 | } |
| 1581 | |
| 1582 | |
| 1583 | static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx) |
| 1584 | { |
| 1585 | os_free(hapd->conf->ap_pin); |
| 1586 | hapd->conf->ap_pin = NULL; |
| 1587 | #ifdef CONFIG_WPS_UPNP |
| 1588 | upnp_wps_set_ap_pin(hapd->wps_upnp, NULL); |
| 1589 | #endif /* CONFIG_WPS_UPNP */ |
| 1590 | eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL); |
| 1591 | return 0; |
| 1592 | } |
| 1593 | |
| 1594 | |
| 1595 | void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd) |
| 1596 | { |
| 1597 | wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN"); |
| 1598 | hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL); |
| 1599 | } |
| 1600 | |
| 1601 | |
| 1602 | struct wps_ap_pin_data { |
| 1603 | char pin_txt[9]; |
| 1604 | int timeout; |
| 1605 | }; |
| 1606 | |
| 1607 | |
| 1608 | static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx) |
| 1609 | { |
| 1610 | struct wps_ap_pin_data *data = ctx; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1611 | |
| 1612 | if (!hapd->wps) |
| 1613 | return 0; |
| 1614 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1615 | os_free(hapd->conf->ap_pin); |
| 1616 | hapd->conf->ap_pin = os_strdup(data->pin_txt); |
| 1617 | #ifdef CONFIG_WPS_UPNP |
| 1618 | upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt); |
| 1619 | #endif /* CONFIG_WPS_UPNP */ |
| 1620 | hostapd_wps_ap_pin_enable(hapd, data->timeout); |
| 1621 | return 0; |
| 1622 | } |
| 1623 | |
| 1624 | |
| 1625 | const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout) |
| 1626 | { |
| 1627 | unsigned int pin; |
| 1628 | struct wps_ap_pin_data data; |
| 1629 | |
| 1630 | pin = wps_generate_pin(); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1631 | os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%08u", pin); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1632 | data.timeout = timeout; |
| 1633 | hostapd_wps_for_each(hapd, wps_ap_pin_set, &data); |
| 1634 | return hapd->conf->ap_pin; |
| 1635 | } |
| 1636 | |
| 1637 | |
| 1638 | const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd) |
| 1639 | { |
| 1640 | return hapd->conf->ap_pin; |
| 1641 | } |
| 1642 | |
| 1643 | |
| 1644 | int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin, |
| 1645 | int timeout) |
| 1646 | { |
| 1647 | struct wps_ap_pin_data data; |
| 1648 | int ret; |
| 1649 | |
| 1650 | ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1651 | if (os_snprintf_error(sizeof(data.pin_txt), ret)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1652 | return -1; |
| 1653 | data.timeout = timeout; |
| 1654 | return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data); |
| 1655 | } |
| 1656 | |
| 1657 | |
| 1658 | static int wps_update_ie(struct hostapd_data *hapd, void *ctx) |
| 1659 | { |
| 1660 | if (hapd->wps) |
| 1661 | wps_registrar_update_ie(hapd->wps->registrar); |
| 1662 | return 0; |
| 1663 | } |
| 1664 | |
| 1665 | |
| 1666 | void hostapd_wps_update_ie(struct hostapd_data *hapd) |
| 1667 | { |
| 1668 | hostapd_wps_for_each(hapd, wps_update_ie, NULL); |
| 1669 | } |
| 1670 | |
| 1671 | |
| 1672 | int hostapd_wps_config_ap(struct hostapd_data *hapd, const char *ssid, |
| 1673 | const char *auth, const char *encr, const char *key) |
| 1674 | { |
| 1675 | struct wps_credential cred; |
| 1676 | size_t len; |
| 1677 | |
| 1678 | os_memset(&cred, 0, sizeof(cred)); |
| 1679 | |
| 1680 | len = os_strlen(ssid); |
| 1681 | if ((len & 1) || len > 2 * sizeof(cred.ssid) || |
| 1682 | hexstr2bin(ssid, cred.ssid, len / 2)) |
| 1683 | return -1; |
| 1684 | cred.ssid_len = len / 2; |
| 1685 | |
| 1686 | if (os_strncmp(auth, "OPEN", 4) == 0) |
| 1687 | cred.auth_type = WPS_AUTH_OPEN; |
| 1688 | else if (os_strncmp(auth, "WPAPSK", 6) == 0) |
| 1689 | cred.auth_type = WPS_AUTH_WPAPSK; |
| 1690 | else if (os_strncmp(auth, "WPA2PSK", 7) == 0) |
| 1691 | cred.auth_type = WPS_AUTH_WPA2PSK; |
| 1692 | else |
| 1693 | return -1; |
| 1694 | |
| 1695 | if (encr) { |
| 1696 | if (os_strncmp(encr, "NONE", 4) == 0) |
| 1697 | cred.encr_type = WPS_ENCR_NONE; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1698 | else if (os_strncmp(encr, "TKIP", 4) == 0) |
| 1699 | cred.encr_type = WPS_ENCR_TKIP; |
| 1700 | else if (os_strncmp(encr, "CCMP", 4) == 0) |
| 1701 | cred.encr_type = WPS_ENCR_AES; |
| 1702 | else |
| 1703 | return -1; |
| 1704 | } else |
| 1705 | cred.encr_type = WPS_ENCR_NONE; |
| 1706 | |
| 1707 | if (key) { |
| 1708 | len = os_strlen(key); |
| 1709 | if ((len & 1) || len > 2 * sizeof(cred.key) || |
| 1710 | hexstr2bin(key, cred.key, len / 2)) |
| 1711 | return -1; |
| 1712 | cred.key_len = len / 2; |
| 1713 | } |
| 1714 | |
| 1715 | return wps_registrar_config_ap(hapd->wps->registrar, &cred); |
| 1716 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1717 | |
| 1718 | |
| 1719 | #ifdef CONFIG_WPS_NFC |
| 1720 | |
| 1721 | struct wps_nfc_password_token_data { |
| 1722 | const u8 *oob_dev_pw; |
| 1723 | size_t oob_dev_pw_len; |
| 1724 | int added; |
| 1725 | }; |
| 1726 | |
| 1727 | |
| 1728 | static int wps_add_nfc_password_token(struct hostapd_data *hapd, void *ctx) |
| 1729 | { |
| 1730 | struct wps_nfc_password_token_data *data = ctx; |
| 1731 | int ret; |
| 1732 | |
| 1733 | if (hapd->wps == NULL) |
| 1734 | return 0; |
| 1735 | ret = wps_registrar_add_nfc_password_token(hapd->wps->registrar, |
| 1736 | data->oob_dev_pw, |
| 1737 | data->oob_dev_pw_len); |
| 1738 | if (ret == 0) |
| 1739 | data->added++; |
| 1740 | return ret; |
| 1741 | } |
| 1742 | |
| 1743 | |
| 1744 | static int hostapd_wps_add_nfc_password_token(struct hostapd_data *hapd, |
| 1745 | struct wps_parse_attr *attr) |
| 1746 | { |
| 1747 | struct wps_nfc_password_token_data data; |
| 1748 | |
| 1749 | data.oob_dev_pw = attr->oob_dev_password; |
| 1750 | data.oob_dev_pw_len = attr->oob_dev_password_len; |
| 1751 | data.added = 0; |
| 1752 | if (hostapd_wps_for_each(hapd, wps_add_nfc_password_token, &data) < 0) |
| 1753 | return -1; |
| 1754 | return data.added ? 0 : -1; |
| 1755 | } |
| 1756 | |
| 1757 | |
| 1758 | static int hostapd_wps_nfc_tag_process(struct hostapd_data *hapd, |
| 1759 | const struct wpabuf *wps) |
| 1760 | { |
| 1761 | struct wps_parse_attr attr; |
| 1762 | |
| 1763 | wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps); |
| 1764 | |
| 1765 | if (wps_parse_msg(wps, &attr)) { |
| 1766 | wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag"); |
| 1767 | return -1; |
| 1768 | } |
| 1769 | |
| 1770 | if (attr.oob_dev_password) |
| 1771 | return hostapd_wps_add_nfc_password_token(hapd, &attr); |
| 1772 | |
| 1773 | wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag"); |
| 1774 | return -1; |
| 1775 | } |
| 1776 | |
| 1777 | |
| 1778 | int hostapd_wps_nfc_tag_read(struct hostapd_data *hapd, |
| 1779 | const struct wpabuf *data) |
| 1780 | { |
| 1781 | const struct wpabuf *wps = data; |
| 1782 | struct wpabuf *tmp = NULL; |
| 1783 | int ret; |
| 1784 | |
| 1785 | if (wpabuf_len(data) < 4) |
| 1786 | return -1; |
| 1787 | |
| 1788 | if (*wpabuf_head_u8(data) != 0x10) { |
| 1789 | /* Assume this contains full NDEF record */ |
| 1790 | tmp = ndef_parse_wifi(data); |
| 1791 | if (tmp == NULL) { |
| 1792 | wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF"); |
| 1793 | return -1; |
| 1794 | } |
| 1795 | wps = tmp; |
| 1796 | } |
| 1797 | |
| 1798 | ret = hostapd_wps_nfc_tag_process(hapd, wps); |
| 1799 | wpabuf_free(tmp); |
| 1800 | return ret; |
| 1801 | } |
| 1802 | |
| 1803 | |
| 1804 | struct wpabuf * hostapd_wps_nfc_config_token(struct hostapd_data *hapd, |
| 1805 | int ndef) |
| 1806 | { |
| 1807 | struct wpabuf *ret; |
| 1808 | |
| 1809 | if (hapd->wps == NULL) |
| 1810 | return NULL; |
| 1811 | |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1812 | ret = wps_get_oob_cred(hapd->wps, hostapd_wps_rf_band_cb(hapd), |
| 1813 | hapd->iconf->channel); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1814 | if (ndef && ret) { |
| 1815 | struct wpabuf *tmp; |
| 1816 | tmp = ndef_build_wifi(ret); |
| 1817 | wpabuf_free(ret); |
| 1818 | if (tmp == NULL) |
| 1819 | return NULL; |
| 1820 | ret = tmp; |
| 1821 | } |
| 1822 | |
| 1823 | return ret; |
| 1824 | } |
| 1825 | |
| 1826 | |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 1827 | struct wpabuf * hostapd_wps_nfc_hs_cr(struct hostapd_data *hapd, int ndef) |
| 1828 | { |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1829 | struct wpabuf *ret; |
| 1830 | |
| 1831 | if (hapd->wps == NULL) |
| 1832 | return NULL; |
| 1833 | |
| 1834 | if (hapd->conf->wps_nfc_dh_pubkey == NULL) { |
| 1835 | struct wps_context *wps = hapd->wps; |
| 1836 | if (wps_nfc_gen_dh(&hapd->conf->wps_nfc_dh_pubkey, |
| 1837 | &hapd->conf->wps_nfc_dh_privkey) < 0) |
| 1838 | return NULL; |
| 1839 | hostapd_wps_nfc_clear(wps); |
| 1840 | wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER; |
| 1841 | wps->ap_nfc_dh_pubkey = |
| 1842 | wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey); |
| 1843 | wps->ap_nfc_dh_privkey = |
| 1844 | wpabuf_dup(hapd->conf->wps_nfc_dh_privkey); |
| 1845 | if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) { |
| 1846 | hostapd_wps_nfc_clear(wps); |
| 1847 | return NULL; |
| 1848 | } |
| 1849 | } |
| 1850 | |
| 1851 | ret = wps_build_nfc_handover_sel(hapd->wps, |
| 1852 | hapd->conf->wps_nfc_dh_pubkey, |
| 1853 | hapd->own_addr, hapd->iface->freq); |
| 1854 | |
| 1855 | if (ndef && ret) { |
| 1856 | struct wpabuf *tmp; |
| 1857 | tmp = ndef_build_wifi(ret); |
| 1858 | wpabuf_free(ret); |
| 1859 | if (tmp == NULL) |
| 1860 | return NULL; |
| 1861 | ret = tmp; |
| 1862 | } |
| 1863 | |
| 1864 | return ret; |
| 1865 | } |
| 1866 | |
| 1867 | |
| 1868 | int hostapd_wps_nfc_report_handover(struct hostapd_data *hapd, |
| 1869 | const struct wpabuf *req, |
| 1870 | const struct wpabuf *sel) |
| 1871 | { |
| 1872 | struct wpabuf *wps; |
| 1873 | int ret = -1; |
| 1874 | u16 wsc_len; |
| 1875 | const u8 *pos; |
| 1876 | struct wpabuf msg; |
| 1877 | struct wps_parse_attr attr; |
| 1878 | u16 dev_pw_id; |
| 1879 | |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 1880 | /* |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1881 | * Enrollee/station is always initiator of the NFC connection handover, |
| 1882 | * so use the request message here to find Enrollee public key hash. |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 1883 | */ |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1884 | wps = ndef_parse_wifi(req); |
| 1885 | if (wps == NULL) |
| 1886 | return -1; |
| 1887 | wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc " |
| 1888 | "payload from NFC connection handover"); |
| 1889 | wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps); |
| 1890 | if (wpabuf_len(wps) < 2) { |
| 1891 | wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request " |
| 1892 | "Message"); |
| 1893 | goto out; |
| 1894 | } |
| 1895 | pos = wpabuf_head(wps); |
| 1896 | wsc_len = WPA_GET_BE16(pos); |
| 1897 | if (wsc_len > wpabuf_len(wps) - 2) { |
| 1898 | wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) " |
| 1899 | "in rt Wi-Fi Handover Request Message", wsc_len); |
| 1900 | goto out; |
| 1901 | } |
| 1902 | pos += 2; |
| 1903 | |
| 1904 | wpa_hexdump(MSG_DEBUG, |
| 1905 | "WPS: WSC attributes in Wi-Fi Handover Request Message", |
| 1906 | pos, wsc_len); |
| 1907 | if (wsc_len < wpabuf_len(wps) - 2) { |
| 1908 | wpa_hexdump(MSG_DEBUG, |
| 1909 | "WPS: Ignore extra data after WSC attributes", |
| 1910 | pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len); |
| 1911 | } |
| 1912 | |
| 1913 | wpabuf_set(&msg, pos, wsc_len); |
| 1914 | ret = wps_parse_msg(&msg, &attr); |
| 1915 | if (ret < 0) { |
| 1916 | wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in " |
| 1917 | "Wi-Fi Handover Request Message"); |
| 1918 | goto out; |
| 1919 | } |
| 1920 | |
| 1921 | if (attr.oob_dev_password == NULL || |
| 1922 | attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) { |
| 1923 | wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password " |
| 1924 | "included in Wi-Fi Handover Request Message"); |
| 1925 | ret = -1; |
| 1926 | goto out; |
| 1927 | } |
| 1928 | |
| 1929 | if (attr.uuid_e == NULL) { |
| 1930 | wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi " |
| 1931 | "Handover Request Message"); |
| 1932 | ret = -1; |
| 1933 | goto out; |
| 1934 | } |
| 1935 | |
| 1936 | wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN); |
| 1937 | |
| 1938 | wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password", |
| 1939 | attr.oob_dev_password, attr.oob_dev_password_len); |
| 1940 | dev_pw_id = WPA_GET_BE16(attr.oob_dev_password + |
| 1941 | WPS_OOB_PUBKEY_HASH_LEN); |
| 1942 | if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) { |
| 1943 | wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID " |
| 1944 | "%u in Wi-Fi Handover Request Message", dev_pw_id); |
| 1945 | ret = -1; |
| 1946 | goto out; |
| 1947 | } |
| 1948 | wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash", |
| 1949 | attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN); |
| 1950 | |
| 1951 | ret = wps_registrar_add_nfc_pw_token(hapd->wps->registrar, |
| 1952 | attr.oob_dev_password, |
| 1953 | DEV_PW_NFC_CONNECTION_HANDOVER, |
| 1954 | NULL, 0, 1); |
| 1955 | |
| 1956 | out: |
| 1957 | wpabuf_free(wps); |
| 1958 | return ret; |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1962 | struct wpabuf * hostapd_wps_nfc_token_gen(struct hostapd_data *hapd, int ndef) |
| 1963 | { |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 1964 | if (hapd->conf->wps_nfc_pw_from_config) { |
| 1965 | return wps_nfc_token_build(ndef, |
| 1966 | hapd->conf->wps_nfc_dev_pw_id, |
| 1967 | hapd->conf->wps_nfc_dh_pubkey, |
| 1968 | hapd->conf->wps_nfc_dev_pw); |
| 1969 | } |
| 1970 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1971 | return wps_nfc_token_gen(ndef, &hapd->conf->wps_nfc_dev_pw_id, |
| 1972 | &hapd->conf->wps_nfc_dh_pubkey, |
| 1973 | &hapd->conf->wps_nfc_dh_privkey, |
| 1974 | &hapd->conf->wps_nfc_dev_pw); |
| 1975 | } |
| 1976 | |
| 1977 | |
| 1978 | int hostapd_wps_nfc_token_enable(struct hostapd_data *hapd) |
| 1979 | { |
| 1980 | struct wps_context *wps = hapd->wps; |
Dmitry Shmidt | 33e38bf | 2013-02-27 12:56:00 -0800 | [diff] [blame] | 1981 | struct wpabuf *pw; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1982 | |
| 1983 | if (wps == NULL) |
| 1984 | return -1; |
| 1985 | |
| 1986 | if (!hapd->conf->wps_nfc_dh_pubkey || |
| 1987 | !hapd->conf->wps_nfc_dh_privkey || |
| 1988 | !hapd->conf->wps_nfc_dev_pw || |
| 1989 | !hapd->conf->wps_nfc_dev_pw_id) |
| 1990 | return -1; |
| 1991 | |
| 1992 | hostapd_wps_nfc_clear(wps); |
Dmitry Shmidt | 5460547 | 2013-11-08 11:10:19 -0800 | [diff] [blame] | 1993 | wpa_printf(MSG_DEBUG, |
| 1994 | "WPS: Enable NFC Tag (Dev Pw Id %u) for AP interface %s (context %p)", |
| 1995 | hapd->conf->wps_nfc_dev_pw_id, hapd->conf->iface, wps); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1996 | wps->ap_nfc_dev_pw_id = hapd->conf->wps_nfc_dev_pw_id; |
| 1997 | wps->ap_nfc_dh_pubkey = wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey); |
| 1998 | wps->ap_nfc_dh_privkey = wpabuf_dup(hapd->conf->wps_nfc_dh_privkey); |
Dmitry Shmidt | 33e38bf | 2013-02-27 12:56:00 -0800 | [diff] [blame] | 1999 | pw = hapd->conf->wps_nfc_dev_pw; |
| 2000 | wps->ap_nfc_dev_pw = wpabuf_alloc( |
| 2001 | wpabuf_len(pw) * 2 + 1); |
| 2002 | if (wps->ap_nfc_dev_pw) { |
| 2003 | wpa_snprintf_hex_uppercase( |
| 2004 | (char *) wpabuf_put(wps->ap_nfc_dev_pw, |
| 2005 | wpabuf_len(pw) * 2), |
| 2006 | wpabuf_len(pw) * 2 + 1, |
| 2007 | wpabuf_head(pw), wpabuf_len(pw)); |
| 2008 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2009 | |
| 2010 | if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey || |
| 2011 | !wps->ap_nfc_dev_pw) { |
| 2012 | hostapd_wps_nfc_clear(wps); |
| 2013 | return -1; |
| 2014 | } |
| 2015 | |
| 2016 | return 0; |
| 2017 | } |
| 2018 | |
| 2019 | |
| 2020 | void hostapd_wps_nfc_token_disable(struct hostapd_data *hapd) |
| 2021 | { |
Dmitry Shmidt | 5460547 | 2013-11-08 11:10:19 -0800 | [diff] [blame] | 2022 | wpa_printf(MSG_DEBUG, "WPS: Disable NFC token for AP interface %s", |
| 2023 | hapd->conf->iface); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2024 | hostapd_wps_nfc_clear(hapd->wps); |
| 2025 | } |
| 2026 | |
| 2027 | #endif /* CONFIG_WPS_NFC */ |