LCOV - code coverage report
Current view: top level - src/ap - wps_hostapd.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1475438200 Lines: 940 1097 85.7 %
Date: 2016-10-02 Functions: 71 71 100.0 %

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

Generated by: LCOV version 1.10