wps_hostapd.c
Go to the documentation of this file.
00001 /*
00002  * hostapd / WPS integration
00003  * Copyright (c) 2008-2009, Jouni Malinen <j@w1.fi>
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 2 as
00007  * published by the Free Software Foundation.
00008  *
00009  * Alternatively, this software may be distributed under the terms of BSD
00010  * license.
00011  *
00012  * See README and COPYING for more details.
00013  */
00014 
00015 #include "utils/includes.h"
00016 
00017 #include "utils/common.h"
00018 #include "utils/eloop.h"
00019 #include "utils/uuid.h"
00020 #include "crypto/dh_groups.h"
00021 #include "common/wpa_ctrl.h"
00022 #include "common/ieee802_11_defs.h"
00023 #include "common/ieee802_11_common.h"
00024 #include "eapol_auth/eapol_auth_sm.h"
00025 #include "eapol_auth/eapol_auth_sm_i.h"
00026 #include "wps/wps.h"
00027 #include "wps/wps_defs.h"
00028 #include "wps/wps_dev_attr.h"
00029 #include "hostapd.h"
00030 #include "ap_config.h"
00031 #include "beacon.h"
00032 #include "sta_info.h"
00033 #include "wps_hostapd.h"
00034 
00035 
00036 #ifdef CONFIG_WPS_UPNP
00037 #include "wps/wps_upnp.h"
00038 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
00039                                  struct wps_context *wps);
00040 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd);
00041 #endif /* CONFIG_WPS_UPNP */
00042 
00043 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
00044                                     const u8 *ie, size_t ie_len);
00045 
00046 
00047 static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
00048                                   size_t psk_len)
00049 {
00050         struct hostapd_data *hapd = ctx;
00051         struct hostapd_wpa_psk *p;
00052         struct hostapd_ssid *ssid = &hapd->conf->ssid;
00053 
00054         wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA "
00055                    MACSTR, MAC2STR(mac_addr));
00056         wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
00057 
00058         if (psk_len != PMK_LEN) {
00059                 wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
00060                            (unsigned long) psk_len);
00061                 return -1;
00062         }
00063 
00064         /* Add the new PSK to runtime PSK list */
00065         p = os_zalloc(sizeof(*p));
00066         if (p == NULL)
00067                 return -1;
00068         os_memcpy(p->addr, mac_addr, ETH_ALEN);
00069         os_memcpy(p->psk, psk, PMK_LEN);
00070 
00071         p->next = ssid->wpa_psk;
00072         ssid->wpa_psk = p;
00073 
00074         if (ssid->wpa_psk_file) {
00075                 FILE *f;
00076                 char hex[PMK_LEN * 2 + 1];
00077                 /* Add the new PSK to PSK list file */
00078                 f = fopen(ssid->wpa_psk_file, "a");
00079                 if (f == NULL) {
00080                         wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
00081                                    "'%s'", ssid->wpa_psk_file);
00082                         return -1;
00083                 }
00084 
00085                 wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
00086                 fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
00087                 fclose(f);
00088         }
00089 
00090         return 0;
00091 }
00092 
00093 
00094 static int hostapd_wps_set_ie_cb(void *ctx, struct wpabuf *beacon_ie,
00095                                  struct wpabuf *probe_resp_ie)
00096 {
00097         struct hostapd_data *hapd = ctx;
00098         wpabuf_free(hapd->wps_beacon_ie);
00099         hapd->wps_beacon_ie = beacon_ie;
00100         wpabuf_free(hapd->wps_probe_resp_ie);
00101         hapd->wps_probe_resp_ie = probe_resp_ie;
00102         ieee802_11_set_beacon(hapd);
00103         return hapd->drv.set_ap_wps_ie(hapd);
00104 }
00105 
00106 
00107 static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
00108                                       const struct wps_device_data *dev)
00109 {
00110         struct hostapd_data *hapd = ctx;
00111         char uuid[40], txt[400];
00112         int len;
00113         char devtype[WPS_DEV_TYPE_BUFSIZE];
00114         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
00115                 return;
00116         wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
00117         len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
00118                           "%s " MACSTR " [%s|%s|%s|%s|%s|%s]",
00119                           uuid, MAC2STR(dev->mac_addr), dev->device_name,
00120                           dev->manufacturer, dev->model_name,
00121                           dev->model_number, dev->serial_number,
00122                           wps_dev_type_bin2str(dev->pri_dev_type, devtype,
00123                                                sizeof(devtype)));
00124         if (len > 0 && len < (int) sizeof(txt))
00125                 wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt);
00126 
00127         if (hapd->conf->wps_pin_requests) {
00128                 FILE *f;
00129                 struct os_time t;
00130                 f = fopen(hapd->conf->wps_pin_requests, "a");
00131                 if (f == NULL)
00132                         return;
00133                 os_get_time(&t);
00134                 fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
00135                         "\t%s\n",
00136                         t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
00137                         dev->manufacturer, dev->model_name, dev->model_number,
00138                         dev->serial_number,
00139                         wps_dev_type_bin2str(dev->pri_dev_type, devtype,
00140                                              sizeof(devtype)));
00141                 fclose(f);
00142         }
00143 }
00144 
00145 
00146 static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
00147                                        const u8 *uuid_e)
00148 {
00149         struct hostapd_data *hapd = ctx;
00150         char uuid[40];
00151         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
00152                 return;
00153         wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
00154                 MAC2STR(mac_addr), uuid);
00155         if (hapd->wps_reg_success_cb)
00156                 hapd->wps_reg_success_cb(hapd->wps_reg_success_cb_ctx,
00157                                          mac_addr, uuid_e);
00158 }
00159 
00160 
00161 static void hostapd_wps_enrollee_seen_cb(void *ctx, const u8 *addr,
00162                                          const u8 *uuid_e,
00163                                          const u8 *pri_dev_type,
00164                                          u16 config_methods,
00165                                          u16 dev_password_id, u8 request_type,
00166                                          const char *dev_name)
00167 {
00168         struct hostapd_data *hapd = ctx;
00169         char uuid[40];
00170         char devtype[WPS_DEV_TYPE_BUFSIZE];
00171         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
00172                 return;
00173         if (dev_name == NULL)
00174                 dev_name = "";
00175         wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ENROLLEE_SEEN MACSTR
00176                      " %s %s 0x%x %u %u [%s]",
00177                      MAC2STR(addr), uuid,
00178                      wps_dev_type_bin2str(pri_dev_type, devtype,
00179                                           sizeof(devtype)),
00180                      config_methods, dev_password_id, request_type, dev_name);
00181 }
00182 
00183 
00184 static int str_starts(const char *str, const char *start)
00185 {
00186         return os_strncmp(str, start, os_strlen(start)) == 0;
00187 }
00188 
00189 
00190 static void wps_reload_config(void *eloop_data, void *user_ctx)
00191 {
00192         struct hostapd_iface *iface = eloop_data;
00193 
00194         wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
00195         if (iface->reload_config(iface) < 0) {
00196                 wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
00197                            "configuration");
00198         }
00199 }
00200 
00201 
00202 static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
00203 {
00204         struct hostapd_data *hapd = ctx;
00205         FILE *oconf, *nconf;
00206         size_t len, i;
00207         char *tmp_fname;
00208         char buf[1024];
00209         int multi_bss;
00210         int wpa;
00211 
00212         wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
00213                         cred->cred_attr, cred->cred_attr_len);
00214 
00215         wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
00216         wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
00217         wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
00218                    cred->auth_type);
00219         wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
00220         wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
00221         wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
00222                         cred->key, cred->key_len);
00223         wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
00224                    MAC2STR(cred->mac_addr));
00225 
00226         if ((hapd->conf->wps_cred_processing == 1 ||
00227              hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
00228                 size_t blen = cred->cred_attr_len * 2 + 1;
00229                 char *_buf = os_malloc(blen);
00230                 if (_buf) {
00231                         wpa_snprintf_hex(_buf, blen,
00232                                          cred->cred_attr, cred->cred_attr_len);
00233                         wpa_msg(hapd->msg_ctx, MSG_INFO, "%s%s",
00234                                 WPS_EVENT_NEW_AP_SETTINGS, _buf);
00235                         os_free(_buf);
00236                 }
00237         } else
00238                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
00239 
00240         if (hapd->conf->wps_cred_processing == 1)
00241                 return 0;
00242 
00243         os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
00244         hapd->wps->ssid_len = cred->ssid_len;
00245         hapd->wps->encr_types = cred->encr_type;
00246         hapd->wps->auth_types = cred->auth_type;
00247         if (cred->key_len == 0) {
00248                 os_free(hapd->wps->network_key);
00249                 hapd->wps->network_key = NULL;
00250                 hapd->wps->network_key_len = 0;
00251         } else {
00252                 if (hapd->wps->network_key == NULL ||
00253                     hapd->wps->network_key_len < cred->key_len) {
00254                         hapd->wps->network_key_len = 0;
00255                         os_free(hapd->wps->network_key);
00256                         hapd->wps->network_key = os_malloc(cred->key_len);
00257                         if (hapd->wps->network_key == NULL)
00258                                 return -1;
00259                 }
00260                 hapd->wps->network_key_len = cred->key_len;
00261                 os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
00262         }
00263         hapd->wps->wps_state = WPS_STATE_CONFIGURED;
00264 
00265         len = os_strlen(hapd->iface->config_fname) + 5;
00266         tmp_fname = os_malloc(len);
00267         if (tmp_fname == NULL)
00268                 return -1;
00269         os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
00270 
00271         oconf = fopen(hapd->iface->config_fname, "r");
00272         if (oconf == NULL) {
00273                 wpa_printf(MSG_WARNING, "WPS: Could not open current "
00274                            "configuration file");
00275                 os_free(tmp_fname);
00276                 return -1;
00277         }
00278 
00279         nconf = fopen(tmp_fname, "w");
00280         if (nconf == NULL) {
00281                 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
00282                            "configuration file");
00283                 os_free(tmp_fname);
00284                 fclose(oconf);
00285                 return -1;
00286         }
00287 
00288         fprintf(nconf, "# WPS configuration - START\n");
00289 
00290         fprintf(nconf, "wps_state=2\n");
00291 
00292         fprintf(nconf, "ssid=");
00293         for (i = 0; i < cred->ssid_len; i++)
00294                 fputc(cred->ssid[i], nconf);
00295         fprintf(nconf, "\n");
00296 
00297         if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
00298             (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
00299                 wpa = 3;
00300         else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
00301                 wpa = 2;
00302         else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
00303                 wpa = 1;
00304         else
00305                 wpa = 0;
00306 
00307         if (wpa) {
00308                 char *prefix;
00309                 fprintf(nconf, "wpa=%d\n", wpa);
00310 
00311                 fprintf(nconf, "wpa_key_mgmt=");
00312                 prefix = "";
00313                 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
00314                         fprintf(nconf, "WPA-EAP");
00315                         prefix = " ";
00316                 }
00317                 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
00318                         fprintf(nconf, "%sWPA-PSK", prefix);
00319                 fprintf(nconf, "\n");
00320 
00321                 fprintf(nconf, "wpa_pairwise=");
00322                 prefix = "";
00323                 if (cred->encr_type & WPS_ENCR_AES) {
00324                         fprintf(nconf, "CCMP");
00325                         prefix = " ";
00326                 }
00327                 if (cred->encr_type & WPS_ENCR_TKIP) {
00328                         fprintf(nconf, "%sTKIP", prefix);
00329                 }
00330                 fprintf(nconf, "\n");
00331 
00332                 if (cred->key_len >= 8 && cred->key_len < 64) {
00333                         fprintf(nconf, "wpa_passphrase=");
00334                         for (i = 0; i < cred->key_len; i++)
00335                                 fputc(cred->key[i], nconf);
00336                         fprintf(nconf, "\n");
00337                 } else if (cred->key_len == 64) {
00338                         fprintf(nconf, "wpa_psk=");
00339                         for (i = 0; i < cred->key_len; i++)
00340                                 fputc(cred->key[i], nconf);
00341                         fprintf(nconf, "\n");
00342                 } else {
00343                         wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
00344                                    "for WPA/WPA2",
00345                                    (unsigned long) cred->key_len);
00346                 }
00347 
00348                 fprintf(nconf, "auth_algs=1\n");
00349         } else {
00350                 if ((cred->auth_type & WPS_AUTH_OPEN) &&
00351                     (cred->auth_type & WPS_AUTH_SHARED))
00352                         fprintf(nconf, "auth_algs=3\n");
00353                 else if (cred->auth_type & WPS_AUTH_SHARED)
00354                         fprintf(nconf, "auth_algs=2\n");
00355                 else
00356                         fprintf(nconf, "auth_algs=1\n");
00357 
00358                 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) {
00359                         int key_idx = cred->key_idx;
00360                         if (key_idx)
00361                                 key_idx--;
00362                         fprintf(nconf, "wep_default_key=%d\n", key_idx);
00363                         fprintf(nconf, "wep_key%d=", key_idx);
00364                         if (cred->key_len == 10 || cred->key_len == 26) {
00365                                 /* WEP key as a hex string */
00366                                 for (i = 0; i < cred->key_len; i++)
00367                                         fputc(cred->key[i], nconf);
00368                         } else {
00369                                 /* Raw WEP key; convert to hex */
00370                                 for (i = 0; i < cred->key_len; i++)
00371                                         fprintf(nconf, "%02x", cred->key[i]);
00372                         }
00373                         fprintf(nconf, "\n");
00374                 }
00375         }
00376 
00377         fprintf(nconf, "# WPS configuration - END\n");
00378 
00379         multi_bss = 0;
00380         while (fgets(buf, sizeof(buf), oconf)) {
00381                 if (os_strncmp(buf, "bss=", 4) == 0)
00382                         multi_bss = 1;
00383                 if (!multi_bss &&
00384                     (str_starts(buf, "ssid=") ||
00385                      str_starts(buf, "auth_algs=") ||
00386                      str_starts(buf, "wps_state=") ||
00387                      str_starts(buf, "wpa=") ||
00388                      str_starts(buf, "wpa_psk=") ||
00389                      str_starts(buf, "wpa_pairwise=") ||
00390                      str_starts(buf, "rsn_pairwise=") ||
00391                      str_starts(buf, "wpa_key_mgmt=") ||
00392                      str_starts(buf, "wpa_passphrase="))) {
00393                         fprintf(nconf, "#WPS# %s", buf);
00394                 } else
00395                         fprintf(nconf, "%s", buf);
00396         }
00397 
00398         fclose(nconf);
00399         fclose(oconf);
00400 
00401         if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
00402                 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
00403                            "configuration file: %s", strerror(errno));
00404                 os_free(tmp_fname);
00405                 return -1;
00406         }
00407 
00408         os_free(tmp_fname);
00409 
00410         /* Schedule configuration reload after short period of time to allow
00411          * EAP-WSC to be finished.
00412          */
00413         eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
00414                                NULL);
00415 
00416         /* TODO: dualband AP may need to update multiple configuration files */
00417 
00418         wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
00419 
00420         return 0;
00421 }
00422 
00423 
00424 static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
00425                                   struct wps_event_pwd_auth_fail *data)
00426 {
00427         FILE *f;
00428 
00429         if (!data->enrollee)
00430                 return;
00431 
00432         /*
00433          * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
00434          * if this happens multiple times.
00435          */
00436         hapd->ap_pin_failures++;
00437         if (hapd->ap_pin_failures < 4)
00438                 return;
00439 
00440         wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
00441         hapd->wps->ap_setup_locked = 1;
00442 
00443         wps_registrar_update_ie(hapd->wps->registrar);
00444 
00445         if (hapd->conf->wps_cred_processing == 1)
00446                 return;
00447 
00448         f = fopen(hapd->iface->config_fname, "a");
00449         if (f == NULL) {
00450                 wpa_printf(MSG_WARNING, "WPS: Could not append to the current "
00451                            "configuration file");
00452                 return;
00453         }
00454 
00455         fprintf(f, "# WPS AP Setup Locked based on possible attack\n");
00456         fprintf(f, "ap_setup_locked=1\n");
00457         fclose(f);
00458 
00459         /* TODO: dualband AP may need to update multiple configuration files */
00460 
00461         wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
00462 }
00463 
00464 
00465 static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
00466                                  union wps_event_data *data)
00467 {
00468         struct hostapd_data *hapd = ctx;
00469 
00470         if (event == WPS_EV_PWD_AUTH_FAIL)
00471                 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
00472 }
00473 
00474 
00475 static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
00476 {
00477         wpabuf_free(hapd->wps_beacon_ie);
00478         hapd->wps_beacon_ie = NULL;
00479 
00480         wpabuf_free(hapd->wps_probe_resp_ie);
00481         hapd->wps_probe_resp_ie = NULL;
00482 
00483         hapd->drv.set_ap_wps_ie(hapd);
00484 }
00485 
00486 
00487 int hostapd_init_wps(struct hostapd_data *hapd,
00488                      struct hostapd_bss_config *conf)
00489 {
00490         struct wps_context *wps;
00491         struct wps_registrar_config cfg;
00492 
00493         if (conf->wps_state == 0) {
00494                 hostapd_wps_clear_ies(hapd);
00495                 return 0;
00496         }
00497 
00498         wps = os_zalloc(sizeof(*wps));
00499         if (wps == NULL)
00500                 return -1;
00501 
00502         wps->cred_cb = hostapd_wps_cred_cb;
00503         wps->event_cb = hostapd_wps_event_cb;
00504         wps->cb_ctx = hapd;
00505 
00506         os_memset(&cfg, 0, sizeof(cfg));
00507         wps->wps_state = hapd->conf->wps_state;
00508         wps->ap_setup_locked = hapd->conf->ap_setup_locked;
00509         if (is_nil_uuid(hapd->conf->uuid)) {
00510                 uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
00511                 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
00512                             wps->uuid, UUID_LEN);
00513         } else
00514                 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
00515         wps->ssid_len = hapd->conf->ssid.ssid_len;
00516         os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
00517         wps->ap = 1;
00518         os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
00519         wps->dev.device_name = hapd->conf->device_name ?
00520                 os_strdup(hapd->conf->device_name) : NULL;
00521         wps->dev.manufacturer = hapd->conf->manufacturer ?
00522                 os_strdup(hapd->conf->manufacturer) : NULL;
00523         wps->dev.model_name = hapd->conf->model_name ?
00524                 os_strdup(hapd->conf->model_name) : NULL;
00525         wps->dev.model_number = hapd->conf->model_number ?
00526                 os_strdup(hapd->conf->model_number) : NULL;
00527         wps->dev.serial_number = hapd->conf->serial_number ?
00528                 os_strdup(hapd->conf->serial_number) : NULL;
00529         wps->config_methods =
00530                 wps_config_methods_str2bin(hapd->conf->config_methods);
00531         if (hapd->conf->device_type &&
00532             wps_dev_type_str2bin(hapd->conf->device_type,
00533                                  wps->dev.pri_dev_type) < 0) {
00534                 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
00535                 os_free(wps);
00536                 return -1;
00537         }
00538         wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
00539         wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
00540                 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
00541 
00542         if (conf->wpa & WPA_PROTO_RSN) {
00543                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
00544                         wps->auth_types |= WPS_AUTH_WPA2PSK;
00545                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
00546                         wps->auth_types |= WPS_AUTH_WPA2;
00547 
00548                 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
00549                         wps->encr_types |= WPS_ENCR_AES;
00550                 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
00551                         wps->encr_types |= WPS_ENCR_TKIP;
00552         }
00553 
00554         if (conf->wpa & WPA_PROTO_WPA) {
00555                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
00556                         wps->auth_types |= WPS_AUTH_WPAPSK;
00557                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
00558                         wps->auth_types |= WPS_AUTH_WPA;
00559 
00560                 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
00561                         wps->encr_types |= WPS_ENCR_AES;
00562                 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
00563                         wps->encr_types |= WPS_ENCR_TKIP;
00564         }
00565 
00566         if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
00567                 wps->encr_types |= WPS_ENCR_NONE;
00568                 wps->auth_types |= WPS_AUTH_OPEN;
00569         } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
00570                 wps->encr_types |= WPS_ENCR_WEP;
00571                 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
00572                         wps->auth_types |= WPS_AUTH_OPEN;
00573                 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
00574                         wps->auth_types |= WPS_AUTH_SHARED;
00575         } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
00576                 wps->auth_types |= WPS_AUTH_OPEN;
00577                 if (conf->default_wep_key_len)
00578                         wps->encr_types |= WPS_ENCR_WEP;
00579                 else
00580                         wps->encr_types |= WPS_ENCR_NONE;
00581         }
00582 
00583         if (conf->ssid.wpa_psk_file) {
00584                 /* Use per-device PSKs */
00585         } else if (conf->ssid.wpa_passphrase) {
00586                 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
00587                 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
00588         } else if (conf->ssid.wpa_psk) {
00589                 wps->network_key = os_malloc(2 * PMK_LEN + 1);
00590                 if (wps->network_key == NULL) {
00591                         os_free(wps);
00592                         return -1;
00593                 }
00594                 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
00595                                  conf->ssid.wpa_psk->psk, PMK_LEN);
00596                 wps->network_key_len = 2 * PMK_LEN;
00597         } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
00598                 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
00599                 if (wps->network_key == NULL) {
00600                         os_free(wps);
00601                         return -1;
00602                 }
00603                 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
00604                           conf->ssid.wep.len[0]);
00605                 wps->network_key_len = conf->ssid.wep.len[0];
00606         }
00607 
00608         if (conf->ssid.wpa_psk) {
00609                 os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
00610                 wps->psk_set = 1;
00611         }
00612 
00613         if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
00614                 /* Override parameters to enable security by default */
00615                 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
00616                 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
00617         }
00618 
00619         wps->ap_settings = conf->ap_settings;
00620         wps->ap_settings_len = conf->ap_settings_len;
00621 
00622         cfg.new_psk_cb = hostapd_wps_new_psk_cb;
00623         cfg.set_ie_cb = hostapd_wps_set_ie_cb;
00624         cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
00625         cfg.reg_success_cb = hostapd_wps_reg_success_cb;
00626         cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
00627         cfg.cb_ctx = hapd;
00628         cfg.skip_cred_build = conf->skip_cred_build;
00629         cfg.extra_cred = conf->extra_cred;
00630         cfg.extra_cred_len = conf->extra_cred_len;
00631         cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
00632                 conf->skip_cred_build;
00633         if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
00634                 cfg.static_wep_only = 1;
00635 
00636         wps->registrar = wps_registrar_init(wps, &cfg);
00637         if (wps->registrar == NULL) {
00638                 printf("Failed to initialize WPS Registrar\n");
00639                 os_free(wps->network_key);
00640                 os_free(wps);
00641                 return -1;
00642         }
00643 
00644 #ifdef CONFIG_WPS_UPNP
00645         wps->friendly_name = hapd->conf->friendly_name;
00646         wps->manufacturer_url = hapd->conf->manufacturer_url;
00647         wps->model_description = hapd->conf->model_description;
00648         wps->model_url = hapd->conf->model_url;
00649         wps->upc = hapd->conf->upc;
00650 
00651         if (hostapd_wps_upnp_init(hapd, wps) < 0) {
00652                 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
00653                 wps_registrar_deinit(wps->registrar);
00654                 os_free(wps->network_key);
00655                 os_free(wps);
00656                 return -1;
00657         }
00658 #endif /* CONFIG_WPS_UPNP */
00659 
00660         hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
00661 
00662         hapd->wps = wps;
00663 
00664         return 0;
00665 }
00666 
00667 
00668 void hostapd_deinit_wps(struct hostapd_data *hapd)
00669 {
00670         if (hapd->wps == NULL)
00671                 return;
00672 #ifdef CONFIG_WPS_UPNP
00673         hostapd_wps_upnp_deinit(hapd);
00674 #endif /* CONFIG_WPS_UPNP */
00675         wps_registrar_deinit(hapd->wps->registrar);
00676         os_free(hapd->wps->network_key);
00677         wps_device_data_free(&hapd->wps->dev);
00678         wpabuf_free(hapd->wps->dh_pubkey);
00679         wpabuf_free(hapd->wps->dh_privkey);
00680         wpabuf_free(hapd->wps->oob_conf.pubkey_hash);
00681         wpabuf_free(hapd->wps->oob_conf.dev_password);
00682         wps_free_pending_msgs(hapd->wps->upnp_msgs);
00683         os_free(hapd->wps);
00684         hapd->wps = NULL;
00685         hostapd_wps_clear_ies(hapd);
00686 }
00687 
00688 
00689 int hostapd_wps_add_pin(struct hostapd_data *hapd, const char *uuid,
00690                         const char *pin, int timeout)
00691 {
00692         u8 u[UUID_LEN];
00693         int any = 0;
00694 
00695         if (hapd->wps == NULL)
00696                 return -1;
00697         if (os_strcmp(uuid, "any") == 0)
00698                 any = 1;
00699         else if (uuid_str2bin(uuid, u))
00700                 return -1;
00701         return wps_registrar_add_pin(hapd->wps->registrar, any ? NULL : u,
00702                                      (const u8 *) pin, os_strlen(pin),
00703                                      timeout);
00704 }
00705 
00706 
00707 int hostapd_wps_button_pushed(struct hostapd_data *hapd)
00708 {
00709         if (hapd->wps == NULL)
00710                 return -1;
00711         return wps_registrar_button_pushed(hapd->wps->registrar);
00712 }
00713 
00714 
00715 #ifdef CONFIG_WPS_OOB
00716 int hostapd_wps_start_oob(struct hostapd_data *hapd, char *device_type,
00717                           char *path, char *method, char *name)
00718 {
00719         struct wps_context *wps = hapd->wps;
00720         struct oob_device_data *oob_dev;
00721 
00722         oob_dev = wps_get_oob_device(device_type);
00723         if (oob_dev == NULL)
00724                 return -1;
00725         oob_dev->device_path = path;
00726         oob_dev->device_name = name;
00727         wps->oob_conf.oob_method = wps_get_oob_method(method);
00728 
00729         if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) {
00730                 /*
00731                  * Use pre-configured DH keys in order to be able to write the
00732                  * key hash into the OOB file.
00733                  */
00734                 wpabuf_free(wps->dh_pubkey);
00735                 wpabuf_free(wps->dh_privkey);
00736                 wps->dh_privkey = NULL;
00737                 wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
00738                                          &wps->dh_privkey);
00739                 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
00740                 if (wps->dh_pubkey == NULL) {
00741                         wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
00742                                    "Diffie-Hellman handshake");
00743                         return -1;
00744                 }
00745         }
00746 
00747         if (wps_process_oob(wps, oob_dev, 1) < 0)
00748                 goto error;
00749 
00750         if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
00751              wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
00752             hostapd_wps_add_pin(hapd, "any",
00753                                 wpabuf_head(wps->oob_conf.dev_password), 0) <
00754             0)
00755                 goto error;
00756 
00757         return 0;
00758 
00759 error:
00760         wpabuf_free(wps->dh_pubkey);
00761         wps->dh_pubkey = NULL;
00762         wpabuf_free(wps->dh_privkey);
00763         wps->dh_privkey = NULL;
00764         return -1;
00765 }
00766 #endif /* CONFIG_WPS_OOB */
00767 
00768 
00769 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
00770                                     const u8 *ie, size_t ie_len)
00771 {
00772         struct hostapd_data *hapd = ctx;
00773         struct wpabuf *wps_ie;
00774 
00775         if (hapd->wps == NULL)
00776                 return 0;
00777 
00778         wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
00779         if (wps_ie == NULL)
00780                 return 0;
00781 
00782         if (wpabuf_len(wps_ie) > 0) {
00783                 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie);
00784 #ifdef CONFIG_WPS_UPNP
00785                 /* FIX: what exactly should be included in the WLANEvent?
00786                  * WPS attributes? Full ProbeReq frame? */
00787                 upnp_wps_device_send_wlan_event(hapd->wps_upnp, addr,
00788                                                 UPNP_WPS_WLANEVENT_TYPE_PROBE,
00789                                                 wps_ie);
00790 #endif /* CONFIG_WPS_UPNP */
00791         }
00792 
00793         wpabuf_free(wps_ie);
00794 
00795         return 0;
00796 }
00797 
00798 
00799 #ifdef CONFIG_WPS_UPNP
00800 
00801 static int hostapd_rx_req_put_wlan_response(
00802         void *priv, enum upnp_wps_wlanevent_type ev_type,
00803         const u8 *mac_addr, const struct wpabuf *msg,
00804         enum wps_msg_type msg_type)
00805 {
00806         struct hostapd_data *hapd = priv;
00807         struct sta_info *sta;
00808         struct upnp_pending_message *p;
00809 
00810         wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
00811                    MACSTR, ev_type, MAC2STR(mac_addr));
00812         wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
00813                     wpabuf_head(msg), wpabuf_len(msg));
00814         if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
00815                 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
00816                            "PutWLANResponse WLANEventType %d", ev_type);
00817                 return -1;
00818         }
00819 
00820         /*
00821          * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
00822          * server implementation for delivery to the peer.
00823          */
00824 
00825         sta = ap_get_sta(hapd, mac_addr);
00826         if (!sta) {
00827                 /*
00828                  * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
00829                  * Pick STA that is in an ongoing WPS registration without
00830                  * checking the MAC address.
00831                  */
00832                 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
00833                            "on NewWLANEventMAC; try wildcard match");
00834                 for (sta = hapd->sta_list; sta; sta = sta->next) {
00835                         if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
00836                                 break;
00837                 }
00838         }
00839 
00840         if (!sta) {
00841                 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
00842                 return 0;
00843         }
00844 
00845         p = os_zalloc(sizeof(*p));
00846         if (p == NULL)
00847                 return -1;
00848         os_memcpy(p->addr, sta->addr, ETH_ALEN);
00849         p->msg = wpabuf_dup(msg);
00850         p->type = msg_type;
00851         p->next = hapd->wps->upnp_msgs;
00852         hapd->wps->upnp_msgs = p;
00853 
00854         return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
00855 }
00856 
00857 
00858 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
00859                                  struct wps_context *wps)
00860 {
00861         struct upnp_wps_device_ctx *ctx;
00862 
00863         if (!hapd->conf->upnp_iface)
00864                 return 0;
00865         ctx = os_zalloc(sizeof(*ctx));
00866         if (ctx == NULL)
00867                 return -1;
00868 
00869         ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
00870         if (hapd->conf->ap_pin)
00871                 ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
00872 
00873         hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd);
00874         if (hapd->wps_upnp == NULL) {
00875                 os_free(ctx);
00876                 return -1;
00877         }
00878         wps->wps_upnp = hapd->wps_upnp;
00879 
00880         if (upnp_wps_device_start(hapd->wps_upnp, hapd->conf->upnp_iface)) {
00881                 upnp_wps_device_deinit(hapd->wps_upnp);
00882                 hapd->wps_upnp = NULL;
00883                 return -1;
00884         }
00885 
00886         return 0;
00887 }
00888 
00889 
00890 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
00891 {
00892         upnp_wps_device_deinit(hapd->wps_upnp);
00893 }
00894 
00895 #endif /* CONFIG_WPS_UPNP */
00896 
00897 
00898 int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
00899                             char *buf, size_t buflen)
00900 {
00901         if (hapd->wps == NULL)
00902                 return 0;
00903         return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
00904 }


wpa_supplicant
Author(s): Package maintained by Blaise Gassend
autogenerated on Thu Apr 24 2014 15:34:36