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 1401264779 Lines: 883 1065 82.9 %
Date: 2014-05-28 Functions: 72 72 100.0 %

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

Generated by: LCOV version 1.10