LCOV - code coverage report
Current view: top level - ap - wps_hostapd.c (source / functions) Hit Total Coverage
Test: hostapd (AS) hwsim test run 1401872338 Lines: 23 1062 2.2 %
Date: 2014-06-04 Functions: 4 72 5.6 %

          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           0 : static int wps_for_each(struct hostapd_iface *iface, void *ctx)
      54             : {
      55           0 :         struct wps_for_each_data *data = ctx;
      56             :         size_t j;
      57             : 
      58           0 :         if (iface == NULL)
      59           0 :                 return 0;
      60           0 :         for (j = 0; j < iface->num_bss; j++) {
      61           0 :                 struct hostapd_data *hapd = iface->bss[j];
      62             :                 int ret;
      63             : 
      64           0 :                 if (hapd != data->calling_hapd &&
      65           0 :                     (hapd->conf->wps_independent ||
      66           0 :                      data->calling_hapd->conf->wps_independent))
      67           0 :                         continue;
      68             : 
      69           0 :                 ret = data->func(hapd, data->ctx);
      70           0 :                 if (ret)
      71           0 :                         return ret;
      72             :         }
      73             : 
      74           0 :         return 0;
      75             : }
      76             : 
      77             : 
      78           0 : static int hostapd_wps_for_each(struct hostapd_data *hapd,
      79             :                                 int (*func)(struct hostapd_data *h, void *ctx),
      80             :                                 void *ctx)
      81             : {
      82           0 :         struct hostapd_iface *iface = hapd->iface;
      83             :         struct wps_for_each_data data;
      84           0 :         data.func = func;
      85           0 :         data.ctx = ctx;
      86           0 :         data.calling_hapd = hapd;
      87           0 :         if (iface->interfaces == NULL ||
      88           0 :             iface->interfaces->for_each_interface == NULL)
      89           0 :                 return wps_for_each(iface, &data);
      90           0 :         return iface->interfaces->for_each_interface(iface->interfaces,
      91             :                                                      wps_for_each, &data);
      92             : }
      93             : 
      94             : 
      95           0 : 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           0 :         struct hostapd_data *hapd = ctx;
     100             :         struct hostapd_wpa_psk *p;
     101           0 :         struct hostapd_ssid *ssid = &hapd->conf->ssid;
     102             : 
     103           0 :         if (is_zero_ether_addr(p2p_dev_addr)) {
     104           0 :                 wpa_printf(MSG_DEBUG,
     105             :                            "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
     106           0 :                            MAC2STR(mac_addr));
     107             :         } else {
     108           0 :                 wpa_printf(MSG_DEBUG,
     109             :                            "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
     110             :                            " P2P Device Addr " MACSTR,
     111           0 :                            MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
     112             :         }
     113           0 :         wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
     114             : 
     115           0 :         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           0 :         p = os_zalloc(sizeof(*p));
     123           0 :         if (p == NULL)
     124           0 :                 return -1;
     125           0 :         os_memcpy(p->addr, mac_addr, ETH_ALEN);
     126           0 :         os_memcpy(p->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
     127           0 :         os_memcpy(p->psk, psk, PMK_LEN);
     128             : 
     129           0 :         if (hapd->new_psk_cb) {
     130           0 :                 hapd->new_psk_cb(hapd->new_psk_cb_ctx, mac_addr, p2p_dev_addr,
     131             :                                  psk, psk_len);
     132             :         }
     133             : 
     134           0 :         p->next = ssid->wpa_psk;
     135           0 :         ssid->wpa_psk = p;
     136             : 
     137           0 :         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           0 :                 f = fopen(ssid->wpa_psk_file, "a");
     142           0 :                 if (f == NULL) {
     143           0 :                         wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
     144             :                                    "'%s'", ssid->wpa_psk_file);
     145           0 :                         return -1;
     146             :                 }
     147             : 
     148           0 :                 wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
     149           0 :                 fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
     150           0 :                 fclose(f);
     151             :         }
     152             : 
     153           0 :         return 0;
     154             : }
     155             : 
     156             : 
     157           0 : static int hostapd_wps_set_ie_cb(void *ctx, struct wpabuf *beacon_ie,
     158             :                                  struct wpabuf *probe_resp_ie)
     159             : {
     160           0 :         struct hostapd_data *hapd = ctx;
     161           0 :         wpabuf_free(hapd->wps_beacon_ie);
     162           0 :         hapd->wps_beacon_ie = beacon_ie;
     163           0 :         wpabuf_free(hapd->wps_probe_resp_ie);
     164           0 :         hapd->wps_probe_resp_ie = probe_resp_ie;
     165           0 :         if (hapd->beacon_set_done)
     166           0 :                 ieee802_11_set_beacon(hapd);
     167           0 :         return hostapd_set_ap_wps_ie(hapd);
     168             : }
     169             : 
     170             : 
     171           0 : static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
     172             :                                       const struct wps_device_data *dev)
     173             : {
     174           0 :         struct hostapd_data *hapd = ctx;
     175             :         char uuid[40], txt[400];
     176             :         int len;
     177             :         char devtype[WPS_DEV_TYPE_BUFSIZE];
     178           0 :         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
     179           0 :                 return;
     180           0 :         wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
     181           0 :         len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
     182             :                           "%s " MACSTR " [%s|%s|%s|%s|%s|%s]",
     183           0 :                           uuid, MAC2STR(dev->mac_addr), dev->device_name,
     184             :                           dev->manufacturer, dev->model_name,
     185             :                           dev->model_number, dev->serial_number,
     186           0 :                           wps_dev_type_bin2str(dev->pri_dev_type, devtype,
     187             :                                                sizeof(devtype)));
     188           0 :         if (len > 0 && len < (int) sizeof(txt))
     189           0 :                 wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt);
     190             : 
     191           0 :         if (hapd->conf->wps_pin_requests) {
     192             :                 FILE *f;
     193             :                 struct os_time t;
     194           0 :                 f = fopen(hapd->conf->wps_pin_requests, "a");
     195           0 :                 if (f == NULL)
     196           0 :                         return;
     197           0 :                 os_get_time(&t);
     198           0 :                 fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
     199             :                         "\t%s\n",
     200           0 :                         t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
     201             :                         dev->manufacturer, dev->model_name, dev->model_number,
     202             :                         dev->serial_number,
     203           0 :                         wps_dev_type_bin2str(dev->pri_dev_type, devtype,
     204             :                                              sizeof(devtype)));
     205           0 :                 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           0 : static int wps_stop_registrar(struct hostapd_data *hapd, void *ctx)
     218             : {
     219           0 :         struct wps_stop_reg_data *data = ctx;
     220           0 :         if (hapd != data->current_hapd && hapd->wps != NULL)
     221           0 :                 wps_registrar_complete(hapd->wps->registrar, data->uuid_e,
     222             :                                        data->dev_pw, data->dev_pw_len);
     223           0 :         return 0;
     224             : }
     225             : 
     226             : 
     227           0 : 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           0 :         struct hostapd_data *hapd = ctx;
     232             :         char uuid[40];
     233             :         struct wps_stop_reg_data data;
     234           0 :         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
     235           0 :                 return;
     236           0 :         wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
     237           0 :                 MAC2STR(mac_addr), uuid);
     238           0 :         if (hapd->wps_reg_success_cb)
     239           0 :                 hapd->wps_reg_success_cb(hapd->wps_reg_success_cb_ctx,
     240             :                                          mac_addr, uuid_e);
     241           0 :         data.current_hapd = hapd;
     242           0 :         data.uuid_e = uuid_e;
     243           0 :         data.dev_pw = dev_pw;
     244           0 :         data.dev_pw_len = dev_pw_len;
     245           0 :         hostapd_wps_for_each(hapd, wps_stop_registrar, &data);
     246             : }
     247             : 
     248             : 
     249           0 : 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           0 :         struct hostapd_data *hapd = ctx;
     257             :         char uuid[40];
     258             :         char devtype[WPS_DEV_TYPE_BUFSIZE];
     259           0 :         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
     260           0 :                 return;
     261           0 :         if (dev_name == NULL)
     262           0 :                 dev_name = "";
     263           0 :         wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ENROLLEE_SEEN MACSTR
     264             :                      " %s %s 0x%x %u %u [%s]",
     265           0 :                      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           0 : static int str_starts(const char *str, const char *start)
     273             : {
     274           0 :         return os_strncmp(str, start, os_strlen(start)) == 0;
     275             : }
     276             : 
     277             : 
     278           0 : static void wps_reload_config(void *eloop_data, void *user_ctx)
     279             : {
     280           0 :         struct hostapd_iface *iface = eloop_data;
     281             : 
     282           0 :         wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
     283           0 :         if (iface->interfaces == NULL ||
     284           0 :             iface->interfaces->reload_config(iface) < 0) {
     285           0 :                 wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
     286             :                            "configuration");
     287             :         }
     288           0 : }
     289             : 
     290             : 
     291           0 : 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           0 :         if (eloop_deplete_timeout(0, 0, wps_reload_config, hapd->iface, NULL) ==
     300             :             1)
     301           0 :                 wpa_printf(MSG_DEBUG, "WPS: Reschedule immediate configuration reload");
     302           0 : }
     303             : 
     304             : 
     305           0 : static void hapd_new_ap_event(struct hostapd_data *hapd, const u8 *attr,
     306             :                               size_t attr_len)
     307             : {
     308           0 :         size_t blen = attr_len * 2 + 1;
     309           0 :         char *buf = os_malloc(blen);
     310           0 :         if (buf) {
     311           0 :                 wpa_snprintf_hex(buf, blen, attr, attr_len);
     312           0 :                 wpa_msg(hapd->msg_ctx, MSG_INFO,
     313             :                         WPS_EVENT_NEW_AP_SETTINGS "%s", buf);
     314           0 :                 os_free(buf);
     315             :         }
     316           0 : }
     317             : 
     318             : 
     319           0 : static int hapd_wps_reconfig_in_memory(struct hostapd_data *hapd,
     320             :                                        const struct wps_credential *cred)
     321             : {
     322           0 :         struct hostapd_bss_config *bss = hapd->conf;
     323             : 
     324           0 :         wpa_printf(MSG_DEBUG, "WPS: Updating in-memory configuration");
     325             : 
     326           0 :         bss->wps_state = 2;
     327           0 :         if (cred->ssid_len <= HOSTAPD_MAX_SSID_LEN) {
     328           0 :                 os_memcpy(bss->ssid.ssid, cred->ssid, cred->ssid_len);
     329           0 :                 bss->ssid.ssid_len = cred->ssid_len;
     330           0 :                 bss->ssid.ssid_set = 1;
     331             :         }
     332             : 
     333           0 :         if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
     334           0 :             (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
     335           0 :                 bss->wpa = 3;
     336           0 :         else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
     337           0 :                 bss->wpa = 2;
     338           0 :         else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
     339           0 :                 bss->wpa = 1;
     340             :         else
     341           0 :                 bss->wpa = 0;
     342             : 
     343           0 :         if (bss->wpa) {
     344           0 :                 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA))
     345           0 :                         bss->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X;
     346           0 :                 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
     347           0 :                         bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
     348             : 
     349           0 :                 bss->wpa_pairwise = 0;
     350           0 :                 if (cred->encr_type & WPS_ENCR_AES)
     351           0 :                         bss->wpa_pairwise |= WPA_CIPHER_CCMP;
     352           0 :                 if (cred->encr_type & WPS_ENCR_TKIP)
     353           0 :                         bss->wpa_pairwise |= WPA_CIPHER_TKIP;
     354           0 :                 bss->rsn_pairwise = bss->wpa_pairwise;
     355           0 :                 bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa,
     356             :                                                             bss->wpa_pairwise,
     357             :                                                             bss->rsn_pairwise);
     358             : 
     359           0 :                 if (cred->key_len >= 8 && cred->key_len < 64) {
     360           0 :                         os_free(bss->ssid.wpa_passphrase);
     361           0 :                         bss->ssid.wpa_passphrase = os_zalloc(cred->key_len + 1);
     362           0 :                         if (bss->ssid.wpa_passphrase)
     363           0 :                                 os_memcpy(bss->ssid.wpa_passphrase, cred->key,
     364             :                                           cred->key_len);
     365           0 :                         os_free(bss->ssid.wpa_psk);
     366           0 :                         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           0 :                 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           0 :                 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           0 :         eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
     392             :                                NULL);
     393             : 
     394           0 :         return 0;
     395             : }
     396             : 
     397             : 
     398           0 : static int hapd_wps_cred_cb(struct hostapd_data *hapd, void *ctx)
     399             : {
     400           0 :         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           0 :         if (hapd->wps == NULL)
     409           0 :                 return 0;
     410             : 
     411           0 :         wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
     412           0 :                         cred->cred_attr, cred->cred_attr_len);
     413             : 
     414           0 :         wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
     415           0 :         wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
     416           0 :         wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
     417           0 :                    cred->auth_type);
     418           0 :         wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
     419           0 :         wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
     420           0 :         wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
     421           0 :                         cred->key, cred->key_len);
     422           0 :         wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
     423           0 :                    MAC2STR(cred->mac_addr));
     424             : 
     425           0 :         if ((hapd->conf->wps_cred_processing == 1 ||
     426           0 :              hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
     427           0 :                 hapd_new_ap_event(hapd, cred->cred_attr, cred->cred_attr_len);
     428           0 :         } else if (hapd->conf->wps_cred_processing == 1 ||
     429           0 :                    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           0 :                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
     438             : 
     439           0 :         if (hapd->conf->wps_cred_processing == 1)
     440           0 :                 return 0;
     441             : 
     442           0 :         os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
     443           0 :         hapd->wps->ssid_len = cred->ssid_len;
     444           0 :         hapd->wps->encr_types = cred->encr_type;
     445           0 :         hapd->wps->auth_types = cred->auth_type;
     446           0 :         hapd->wps->ap_encr_type = cred->encr_type;
     447           0 :         hapd->wps->ap_auth_type = cred->auth_type;
     448           0 :         if (cred->key_len == 0) {
     449           0 :                 os_free(hapd->wps->network_key);
     450           0 :                 hapd->wps->network_key = NULL;
     451           0 :                 hapd->wps->network_key_len = 0;
     452             :         } else {
     453           0 :                 if (hapd->wps->network_key == NULL ||
     454           0 :                     hapd->wps->network_key_len < cred->key_len) {
     455           0 :                         hapd->wps->network_key_len = 0;
     456           0 :                         os_free(hapd->wps->network_key);
     457           0 :                         hapd->wps->network_key = os_malloc(cred->key_len);
     458           0 :                         if (hapd->wps->network_key == NULL)
     459           0 :                                 return -1;
     460             :                 }
     461           0 :                 hapd->wps->network_key_len = cred->key_len;
     462           0 :                 os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
     463             :         }
     464           0 :         hapd->wps->wps_state = WPS_STATE_CONFIGURED;
     465             : 
     466           0 :         if (hapd->iface->config_fname == NULL)
     467           0 :                 return hapd_wps_reconfig_in_memory(hapd, cred);
     468           0 :         len = os_strlen(hapd->iface->config_fname) + 5;
     469           0 :         tmp_fname = os_malloc(len);
     470           0 :         if (tmp_fname == NULL)
     471           0 :                 return -1;
     472           0 :         os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
     473             : 
     474           0 :         oconf = fopen(hapd->iface->config_fname, "r");
     475           0 :         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           0 :         nconf = fopen(tmp_fname, "w");
     483           0 :         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           0 :         fprintf(nconf, "# WPS configuration - START\n");
     492             : 
     493           0 :         fprintf(nconf, "wps_state=2\n");
     494             : 
     495           0 :         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           0 :                 fprintf(nconf, "ssid=");
     502           0 :                 for (i = 0; i < cred->ssid_len; i++)
     503           0 :                         fputc(cred->ssid[i], nconf);
     504           0 :                 fprintf(nconf, "\n");
     505             :         }
     506             : 
     507           0 :         if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
     508           0 :             (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
     509           0 :                 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           0 :         if (wpa) {
     518             :                 char *prefix;
     519           0 :                 fprintf(nconf, "wpa=%d\n", wpa);
     520             : 
     521           0 :                 fprintf(nconf, "wpa_key_mgmt=");
     522           0 :                 prefix = "";
     523           0 :                 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
     524           0 :                         fprintf(nconf, "WPA-EAP");
     525           0 :                         prefix = " ";
     526             :                 }
     527           0 :                 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
     528           0 :                         fprintf(nconf, "%sWPA-PSK", prefix);
     529           0 :                 fprintf(nconf, "\n");
     530             : 
     531           0 :                 fprintf(nconf, "wpa_pairwise=");
     532           0 :                 prefix = "";
     533           0 :                 if (cred->encr_type & WPS_ENCR_AES) {
     534           0 :                         fprintf(nconf, "CCMP");
     535           0 :                         prefix = " ";
     536             :                 }
     537           0 :                 if (cred->encr_type & WPS_ENCR_TKIP) {
     538           0 :                         fprintf(nconf, "%sTKIP", prefix);
     539             :                 }
     540           0 :                 fprintf(nconf, "\n");
     541             : 
     542           0 :                 if (cred->key_len >= 8 && cred->key_len < 64) {
     543           0 :                         fprintf(nconf, "wpa_passphrase=");
     544           0 :                         for (i = 0; i < cred->key_len; i++)
     545           0 :                                 fputc(cred->key[i], nconf);
     546           0 :                         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           0 :                 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           0 :         fprintf(nconf, "# WPS configuration - END\n");
     568             : 
     569           0 :         multi_bss = 0;
     570           0 :         while (fgets(buf, sizeof(buf), oconf)) {
     571           0 :                 if (os_strncmp(buf, "bss=", 4) == 0)
     572           0 :                         multi_bss = 1;
     573           0 :                 if (!multi_bss &&
     574           0 :                     (str_starts(buf, "ssid=") ||
     575           0 :                      str_starts(buf, "ssid2=") ||
     576           0 :                      str_starts(buf, "auth_algs=") ||
     577           0 :                      str_starts(buf, "wep_default_key=") ||
     578           0 :                      str_starts(buf, "wep_key") ||
     579           0 :                      str_starts(buf, "wps_state=") ||
     580           0 :                      str_starts(buf, "wpa=") ||
     581           0 :                      str_starts(buf, "wpa_psk=") ||
     582           0 :                      str_starts(buf, "wpa_pairwise=") ||
     583           0 :                      str_starts(buf, "rsn_pairwise=") ||
     584           0 :                      str_starts(buf, "wpa_key_mgmt=") ||
     585           0 :                      str_starts(buf, "wpa_passphrase="))) {
     586           0 :                         fprintf(nconf, "#WPS# %s", buf);
     587             :                 } else
     588           0 :                         fprintf(nconf, "%s", buf);
     589             :         }
     590             : 
     591           0 :         fclose(nconf);
     592           0 :         fclose(oconf);
     593             : 
     594           0 :         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           0 :         os_free(tmp_fname);
     602             : 
     603             :         /* Schedule configuration reload after short period of time to allow
     604             :          * EAP-WSC to be finished.
     605             :          */
     606           0 :         eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
     607             :                                NULL);
     608             : 
     609           0 :         wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
     610             : 
     611           0 :         return 0;
     612             : }
     613             : 
     614             : 
     615           0 : static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
     616             : {
     617           0 :         struct hostapd_data *hapd = ctx;
     618           0 :         return hostapd_wps_for_each(hapd, hapd_wps_cred_cb, (void *) cred);
     619             : }
     620             : 
     621             : 
     622           0 : static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx)
     623             : {
     624           0 :         struct hostapd_data *hapd = eloop_data;
     625             : 
     626           0 :         if (hapd->conf->ap_setup_locked)
     627           0 :                 return;
     628           0 :         if (hapd->ap_pin_failures_consecutive >= 10)
     629           0 :                 return;
     630             : 
     631           0 :         wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN");
     632           0 :         wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
     633           0 :         hapd->wps->ap_setup_locked = 0;
     634           0 :         wps_registrar_update_ie(hapd->wps->registrar);
     635             : }
     636             : 
     637             : 
     638           0 : static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx)
     639             : {
     640           0 :         struct wps_event_pwd_auth_fail *data = ctx;
     641             : 
     642           0 :         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           0 :         hapd->ap_pin_failures++;
     651           0 :         hapd->ap_pin_failures_consecutive++;
     652           0 :         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           0 :         if (hapd->ap_pin_failures < 3)
     656           0 :                 return 0;
     657             : 
     658           0 :         wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
     659           0 :         hapd->wps->ap_setup_locked = 1;
     660             : 
     661           0 :         wps_registrar_update_ie(hapd->wps->registrar);
     662             : 
     663           0 :         if (!hapd->conf->ap_setup_locked &&
     664           0 :             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           0 :         } else if (!hapd->conf->ap_setup_locked) {
     672           0 :                 if (hapd->ap_pin_lockout_time == 0)
     673           0 :                         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           0 :                 wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds",
     679             :                            hapd->ap_pin_lockout_time);
     680           0 :                 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
     681           0 :                 eloop_register_timeout(hapd->ap_pin_lockout_time, 0,
     682             :                                        hostapd_wps_reenable_ap_pin, hapd,
     683             :                                        NULL);
     684             :         }
     685             : 
     686           0 :         return 0;
     687             : }
     688             : 
     689             : 
     690           0 : 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           0 :         wpa_printf(MSG_DEBUG, "WPS: Authentication failure update");
     695           0 :         hapd->wps_stats.status = WPS_STATUS_FAILURE;
     696           0 :         hapd->wps_stats.failure_reason = WPS_EI_AUTH_FAILURE;
     697           0 :         os_memcpy(hapd->wps_stats.peer_addr, data->peer_macaddr, ETH_ALEN);
     698             : 
     699           0 :         hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data);
     700           0 : }
     701             : 
     702             : 
     703           0 : static int wps_ap_pin_success(struct hostapd_data *hapd, void *ctx)
     704             : {
     705           0 :         if (hapd->conf->ap_pin == NULL || hapd->wps == NULL)
     706           0 :                 return 0;
     707             : 
     708           0 :         if (hapd->ap_pin_failures_consecutive == 0)
     709           0 :                 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           0 : static void hostapd_wps_ap_pin_success(struct hostapd_data *hapd)
     721             : {
     722           0 :         hostapd_wps_for_each(hapd, wps_ap_pin_success, NULL);
     723           0 : }
     724             : 
     725             : 
     726           0 : static void hostapd_wps_event_pbc_overlap(struct hostapd_data *hapd)
     727             : {
     728             :         /* Update WPS Status - PBC Overlap */
     729           0 :         hapd->wps_stats.pbc_status = WPS_PBC_STATUS_OVERLAP;
     730           0 : }
     731             : 
     732             : 
     733           0 : static void hostapd_wps_event_pbc_timeout(struct hostapd_data *hapd)
     734             : {
     735             :         /* Update WPS PBC Status:PBC Timeout */
     736           0 :         hapd->wps_stats.pbc_status = WPS_PBC_STATUS_TIMEOUT;
     737           0 : }
     738             : 
     739             : 
     740           0 : static void hostapd_wps_event_pbc_active(struct hostapd_data *hapd)
     741             : {
     742             :         /* Update WPS PBC status - Active */
     743           0 :         hapd->wps_stats.pbc_status = WPS_PBC_STATUS_ACTIVE;
     744           0 : }
     745             : 
     746             : 
     747           0 : static void hostapd_wps_event_pbc_disable(struct hostapd_data *hapd)
     748             : {
     749             :         /* Update WPS PBC status - Active */
     750           0 :         hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE;
     751           0 : }
     752             : 
     753             : 
     754           0 : static void hostapd_wps_event_success(struct hostapd_data *hapd,
     755             :                                       struct wps_event_success *success)
     756             : {
     757             :         /* Update WPS status - Success */
     758           0 :         hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE;
     759           0 :         hapd->wps_stats.status = WPS_STATUS_SUCCESS;
     760           0 :         os_memcpy(hapd->wps_stats.peer_addr, success->peer_macaddr, ETH_ALEN);
     761           0 : }
     762             : 
     763             : 
     764           0 : static void hostapd_wps_event_fail(struct hostapd_data *hapd,
     765             :                                    struct wps_event_fail *fail)
     766             : {
     767             :         /* Update WPS status - Failure */
     768           0 :         hapd->wps_stats.status = WPS_STATUS_FAILURE;
     769           0 :         os_memcpy(hapd->wps_stats.peer_addr, fail->peer_macaddr, ETH_ALEN);
     770             : 
     771           0 :         hapd->wps_stats.failure_reason = fail->error_indication;
     772             : 
     773           0 :         if (fail->error_indication > 0 &&
     774           0 :             fail->error_indication < NUM_WPS_EI_VALUES) {
     775           0 :                 wpa_msg(hapd->msg_ctx, MSG_INFO,
     776             :                         WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
     777           0 :                         fail->msg, fail->config_error, fail->error_indication,
     778           0 :                         wps_ei_str(fail->error_indication));
     779             :         } else {
     780           0 :                 wpa_msg(hapd->msg_ctx, MSG_INFO,
     781             :                         WPS_EVENT_FAIL "msg=%d config_error=%d",
     782           0 :                         fail->msg, fail->config_error);
     783             :         }
     784           0 : }
     785             : 
     786             : 
     787           0 : static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
     788             :                                  union wps_event_data *data)
     789             : {
     790           0 :         struct hostapd_data *hapd = ctx;
     791             : 
     792           0 :         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           0 :                 hostapd_wps_event_fail(hapd, &data->fail);
     798           0 :                 break;
     799             :         case WPS_EV_SUCCESS:
     800           0 :                 hostapd_wps_event_success(hapd, &data->success);
     801           0 :                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_SUCCESS);
     802           0 :                 break;
     803             :         case WPS_EV_PWD_AUTH_FAIL:
     804           0 :                 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
     805           0 :                 break;
     806             :         case WPS_EV_PBC_OVERLAP:
     807           0 :                 hostapd_wps_event_pbc_overlap(hapd);
     808           0 :                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_OVERLAP);
     809           0 :                 break;
     810             :         case WPS_EV_PBC_TIMEOUT:
     811           0 :                 hostapd_wps_event_pbc_timeout(hapd);
     812           0 :                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_TIMEOUT);
     813           0 :                 break;
     814             :         case WPS_EV_PBC_ACTIVE:
     815           0 :                 hostapd_wps_event_pbc_active(hapd);
     816           0 :                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ACTIVE);
     817           0 :                 break;
     818             :         case WPS_EV_PBC_DISABLE:
     819           0 :                 hostapd_wps_event_pbc_disable(hapd);
     820           0 :                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_DISABLE);
     821           0 :                 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           0 :                 hostapd_wps_ap_pin_success(hapd);
     836           0 :                 break;
     837             :         }
     838           0 :         if (hapd->wps_event_cb)
     839           0 :                 hapd->wps_event_cb(hapd->wps_event_cb_ctx, event, data);
     840           0 : }
     841             : 
     842             : 
     843           0 : static int hostapd_wps_rf_band_cb(void *ctx)
     844             : {
     845           0 :         struct hostapd_data *hapd = ctx;
     846             : 
     847           0 :         return hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
     848           0 :                 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
     849             : }
     850             : 
     851             : 
     852           4 : static void hostapd_wps_clear_ies(struct hostapd_data *hapd, int deinit_only)
     853             : {
     854           4 :         wpabuf_free(hapd->wps_beacon_ie);
     855           4 :         hapd->wps_beacon_ie = NULL;
     856             : 
     857           4 :         wpabuf_free(hapd->wps_probe_resp_ie);
     858           4 :         hapd->wps_probe_resp_ie = NULL;
     859             : 
     860           4 :         if (deinit_only)
     861           6 :                 return;
     862             : 
     863           2 :         hostapd_set_ap_wps_ie(hapd);
     864             : }
     865             : 
     866             : 
     867           0 : static int get_uuid_cb(struct hostapd_iface *iface, void *ctx)
     868             : {
     869           0 :         const u8 **uuid = ctx;
     870             :         size_t j;
     871             : 
     872           0 :         if (iface == NULL)
     873           0 :                 return 0;
     874           0 :         for (j = 0; j < iface->num_bss; j++) {
     875           0 :                 struct hostapd_data *hapd = iface->bss[j];
     876           0 :                 if (hapd->wps && !hapd->conf->wps_independent &&
     877           0 :                     !is_nil_uuid(hapd->wps->uuid)) {
     878           0 :                         *uuid = hapd->wps->uuid;
     879           0 :                         return 1;
     880             :                 }
     881             :         }
     882             : 
     883           0 :         return 0;
     884             : }
     885             : 
     886             : 
     887           0 : static const u8 * get_own_uuid(struct hostapd_iface *iface)
     888             : {
     889             :         const u8 *uuid;
     890           0 :         if (iface->interfaces == NULL ||
     891           0 :             iface->interfaces->for_each_interface == NULL)
     892           0 :                 return NULL;
     893           0 :         uuid = NULL;
     894           0 :         iface->interfaces->for_each_interface(iface->interfaces, get_uuid_cb,
     895             :                                               &uuid);
     896           0 :         return uuid;
     897             : }
     898             : 
     899             : 
     900           0 : static int count_interface_cb(struct hostapd_iface *iface, void *ctx)
     901             : {
     902           0 :         int *count= ctx;
     903           0 :         (*count)++;
     904           0 :         return 0;
     905             : }
     906             : 
     907             : 
     908           0 : static int interface_count(struct hostapd_iface *iface)
     909             : {
     910           0 :         int count = 0;
     911           0 :         if (iface->interfaces == NULL ||
     912           0 :             iface->interfaces->for_each_interface == NULL)
     913           0 :                 return 0;
     914           0 :         iface->interfaces->for_each_interface(iface->interfaces,
     915             :                                               count_interface_cb, &count);
     916           0 :         return count;
     917             : }
     918             : 
     919             : 
     920           0 : static int hostapd_wps_set_vendor_ext(struct hostapd_data *hapd,
     921             :                                       struct wps_context *wps)
     922             : {
     923             :         int i;
     924             : 
     925           0 :         for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
     926           0 :                 wpabuf_free(wps->dev.vendor_ext[i]);
     927           0 :                 wps->dev.vendor_ext[i] = NULL;
     928             : 
     929           0 :                 if (hapd->conf->wps_vendor_ext[i] == NULL)
     930           0 :                         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           0 :         return 0;
     942             : }
     943             : 
     944             : 
     945           0 : static void hostapd_free_wps(struct wps_context *wps)
     946             : {
     947             :         int i;
     948             : 
     949           0 :         for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++)
     950           0 :                 wpabuf_free(wps->dev.vendor_ext[i]);
     951           0 :         wps_device_data_free(&wps->dev);
     952           0 :         os_free(wps->network_key);
     953           0 :         hostapd_wps_nfc_clear(wps);
     954           0 :         wpabuf_free(wps->dh_pubkey);
     955           0 :         wpabuf_free(wps->dh_privkey);
     956           0 :         os_free(wps);
     957           0 : }
     958             : 
     959             : 
     960           2 : 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           2 :         if (conf->wps_state == 0) {
     967           2 :                 hostapd_wps_clear_ies(hapd, 0);
     968           2 :                 return 0;
     969             :         }
     970             : 
     971           0 :         wps = os_zalloc(sizeof(*wps));
     972           0 :         if (wps == NULL)
     973           0 :                 return -1;
     974             : 
     975           0 :         wps->cred_cb = hostapd_wps_cred_cb;
     976           0 :         wps->event_cb = hostapd_wps_event_cb;
     977           0 :         wps->rf_band_cb = hostapd_wps_rf_band_cb;
     978           0 :         wps->cb_ctx = hapd;
     979             : 
     980           0 :         os_memset(&cfg, 0, sizeof(cfg));
     981           0 :         wps->wps_state = hapd->conf->wps_state;
     982           0 :         wps->ap_setup_locked = hapd->conf->ap_setup_locked;
     983           0 :         if (is_nil_uuid(hapd->conf->uuid)) {
     984             :                 const u8 *uuid;
     985           0 :                 uuid = get_own_uuid(hapd->iface);
     986           0 :                 if (uuid && !conf->wps_independent) {
     987           0 :                         os_memcpy(wps->uuid, uuid, UUID_LEN);
     988           0 :                         wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another "
     989           0 :                                     "interface", wps->uuid, UUID_LEN);
     990             :                 } else {
     991           0 :                         uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
     992           0 :                         wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
     993           0 :                                     "address", wps->uuid, UUID_LEN);
     994             :                 }
     995             :         } else {
     996           0 :                 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
     997           0 :                 wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID",
     998           0 :                             wps->uuid, UUID_LEN);
     999             :         }
    1000           0 :         wps->ssid_len = hapd->conf->ssid.ssid_len;
    1001           0 :         os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
    1002           0 :         wps->ap = 1;
    1003           0 :         os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
    1004           0 :         wps->dev.device_name = hapd->conf->device_name ?
    1005           0 :                 os_strdup(hapd->conf->device_name) : NULL;
    1006           0 :         wps->dev.manufacturer = hapd->conf->manufacturer ?
    1007           0 :                 os_strdup(hapd->conf->manufacturer) : NULL;
    1008           0 :         wps->dev.model_name = hapd->conf->model_name ?
    1009           0 :                 os_strdup(hapd->conf->model_name) : NULL;
    1010           0 :         wps->dev.model_number = hapd->conf->model_number ?
    1011           0 :                 os_strdup(hapd->conf->model_number) : NULL;
    1012           0 :         wps->dev.serial_number = hapd->conf->serial_number ?
    1013           0 :                 os_strdup(hapd->conf->serial_number) : NULL;
    1014           0 :         wps->config_methods =
    1015           0 :                 wps_config_methods_str2bin(hapd->conf->config_methods);
    1016           0 :         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           0 :         if ((wps->config_methods &
    1024             :              (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
    1025             :               WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
    1026           0 :                 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
    1027             :                            "virtual_push_button for WPS 2.0 compliance");
    1028           0 :                 wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
    1029             :         }
    1030           0 :         os_memcpy(wps->dev.pri_dev_type, hapd->conf->device_type,
    1031             :                   WPS_DEV_TYPE_LEN);
    1032             : 
    1033           0 :         if (hostapd_wps_set_vendor_ext(hapd, wps) < 0)
    1034           0 :                 goto fail;
    1035             : 
    1036           0 :         wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
    1037             : 
    1038           0 :         if (conf->wps_rf_bands) {
    1039           0 :                 wps->dev.rf_bands = conf->wps_rf_bands;
    1040             :         } else {
    1041           0 :                 wps->dev.rf_bands =
    1042           0 :                         hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
    1043             :                         WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
    1044             :         }
    1045             : 
    1046           0 :         if (conf->wpa & WPA_PROTO_RSN) {
    1047           0 :                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
    1048           0 :                         wps->auth_types |= WPS_AUTH_WPA2PSK;
    1049           0 :                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
    1050           0 :                         wps->auth_types |= WPS_AUTH_WPA2;
    1051             : 
    1052           0 :                 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
    1053           0 :                         wps->encr_types |= WPS_ENCR_AES;
    1054           0 :                 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
    1055           0 :                         wps->encr_types |= WPS_ENCR_TKIP;
    1056             :         }
    1057             : 
    1058           0 :         if (conf->wpa & WPA_PROTO_WPA) {
    1059           0 :                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
    1060           0 :                         wps->auth_types |= WPS_AUTH_WPAPSK;
    1061           0 :                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
    1062           0 :                         wps->auth_types |= WPS_AUTH_WPA;
    1063             : 
    1064           0 :                 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
    1065           0 :                         wps->encr_types |= WPS_ENCR_AES;
    1066           0 :                 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
    1067           0 :                         wps->encr_types |= WPS_ENCR_TKIP;
    1068             :         }
    1069             : 
    1070           0 :         if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
    1071           0 :                 wps->encr_types |= WPS_ENCR_NONE;
    1072           0 :                 wps->auth_types |= WPS_AUTH_OPEN;
    1073             :         }
    1074             : 
    1075           0 :         if (conf->ssid.wpa_psk_file) {
    1076             :                 /* Use per-device PSKs */
    1077           0 :         } else if (conf->ssid.wpa_passphrase) {
    1078           0 :                 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
    1079           0 :                 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
    1080           0 :         } else if (conf->ssid.wpa_psk) {
    1081           0 :                 wps->network_key = os_malloc(2 * PMK_LEN + 1);
    1082           0 :                 if (wps->network_key == NULL)
    1083           0 :                         goto fail;
    1084           0 :                 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
    1085           0 :                                  conf->ssid.wpa_psk->psk, PMK_LEN);
    1086           0 :                 wps->network_key_len = 2 * PMK_LEN;
    1087           0 :         } 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           0 :         if (conf->ssid.wpa_psk) {
    1097           0 :                 os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
    1098           0 :                 wps->psk_set = 1;
    1099             :         }
    1100             : 
    1101           0 :         wps->ap_auth_type = wps->auth_types;
    1102           0 :         wps->ap_encr_type = wps->encr_types;
    1103           0 :         if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
    1104             :                 /* Override parameters to enable security by default */
    1105           0 :                 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
    1106           0 :                 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
    1107             :         }
    1108             : 
    1109           0 :         wps->ap_settings = conf->ap_settings;
    1110           0 :         wps->ap_settings_len = conf->ap_settings_len;
    1111             : 
    1112           0 :         cfg.new_psk_cb = hostapd_wps_new_psk_cb;
    1113           0 :         cfg.set_ie_cb = hostapd_wps_set_ie_cb;
    1114           0 :         cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
    1115           0 :         cfg.reg_success_cb = hostapd_wps_reg_success_cb;
    1116           0 :         cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
    1117           0 :         cfg.cb_ctx = hapd;
    1118           0 :         cfg.skip_cred_build = conf->skip_cred_build;
    1119           0 :         cfg.extra_cred = conf->extra_cred;
    1120           0 :         cfg.extra_cred_len = conf->extra_cred_len;
    1121           0 :         cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
    1122           0 :                 conf->skip_cred_build;
    1123           0 :         if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
    1124           0 :                 cfg.static_wep_only = 1;
    1125           0 :         cfg.dualband = interface_count(hapd->iface) > 1;
    1126           0 :         if ((wps->dev.rf_bands & (WPS_RF_50GHZ | WPS_RF_24GHZ)) ==
    1127             :             (WPS_RF_50GHZ | WPS_RF_24GHZ))
    1128           0 :                 cfg.dualband = 1;
    1129           0 :         if (cfg.dualband)
    1130           0 :                 wpa_printf(MSG_DEBUG, "WPS: Dualband AP");
    1131           0 :         cfg.force_per_enrollee_psk = conf->force_per_enrollee_psk;
    1132             : 
    1133           0 :         wps->registrar = wps_registrar_init(wps, &cfg);
    1134           0 :         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           0 :         wps->friendly_name = hapd->conf->friendly_name;
    1141           0 :         wps->manufacturer_url = hapd->conf->manufacturer_url;
    1142           0 :         wps->model_description = hapd->conf->model_description;
    1143           0 :         wps->model_url = hapd->conf->model_url;
    1144           0 :         wps->upc = hapd->conf->upc;
    1145             : #endif /* CONFIG_WPS_UPNP */
    1146             : 
    1147           0 :         hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
    1148             : 
    1149           0 :         hapd->wps = wps;
    1150             : 
    1151           0 :         return 0;
    1152             : 
    1153             : fail:
    1154           0 :         hostapd_free_wps(wps);
    1155           0 :         return -1;
    1156             : }
    1157             : 
    1158             : 
    1159           2 : int hostapd_init_wps_complete(struct hostapd_data *hapd)
    1160             : {
    1161           2 :         struct wps_context *wps = hapd->wps;
    1162             : 
    1163           2 :         if (wps == NULL)
    1164           2 :                 return 0;
    1165             : 
    1166             : #ifdef CONFIG_WPS_UPNP
    1167           0 :         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           0 :         return 0;
    1177             : }
    1178             : 
    1179             : 
    1180           0 : static void hostapd_wps_nfc_clear(struct wps_context *wps)
    1181             : {
    1182             : #ifdef CONFIG_WPS_NFC
    1183           0 :         wpa_printf(MSG_DEBUG, "WPS: Clear NFC Tag context %p", wps);
    1184           0 :         wps->ap_nfc_dev_pw_id = 0;
    1185           0 :         wpabuf_free(wps->ap_nfc_dh_pubkey);
    1186           0 :         wps->ap_nfc_dh_pubkey = NULL;
    1187           0 :         wpabuf_free(wps->ap_nfc_dh_privkey);
    1188           0 :         wps->ap_nfc_dh_privkey = NULL;
    1189           0 :         wpabuf_free(wps->ap_nfc_dev_pw);
    1190           0 :         wps->ap_nfc_dev_pw = NULL;
    1191             : #endif /* CONFIG_WPS_NFC */
    1192           0 : }
    1193             : 
    1194             : 
    1195           2 : void hostapd_deinit_wps(struct hostapd_data *hapd)
    1196             : {
    1197           2 :         eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
    1198           2 :         eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
    1199           2 :         eloop_cancel_timeout(wps_reload_config, hapd->iface, NULL);
    1200           2 :         if (hapd->wps == NULL) {
    1201           2 :                 hostapd_wps_clear_ies(hapd, 1);
    1202           4 :                 return;
    1203             :         }
    1204             : #ifdef CONFIG_WPS_UPNP
    1205           0 :         hostapd_wps_upnp_deinit(hapd);
    1206             : #endif /* CONFIG_WPS_UPNP */
    1207           0 :         wps_registrar_deinit(hapd->wps->registrar);
    1208           0 :         wps_free_pending_msgs(hapd->wps->upnp_msgs);
    1209           0 :         hostapd_free_wps(hapd->wps);
    1210           0 :         hapd->wps = NULL;
    1211           0 :         hostapd_wps_clear_ies(hapd, 1);
    1212             : }
    1213             : 
    1214             : 
    1215           0 : void hostapd_update_wps(struct hostapd_data *hapd)
    1216             : {
    1217           0 :         if (hapd->wps == NULL)
    1218           0 :                 return;
    1219             : 
    1220             : #ifdef CONFIG_WPS_UPNP
    1221           0 :         hapd->wps->friendly_name = hapd->conf->friendly_name;
    1222           0 :         hapd->wps->manufacturer_url = hapd->conf->manufacturer_url;
    1223           0 :         hapd->wps->model_description = hapd->conf->model_description;
    1224           0 :         hapd->wps->model_url = hapd->conf->model_url;
    1225           0 :         hapd->wps->upc = hapd->conf->upc;
    1226             : #endif /* CONFIG_WPS_UPNP */
    1227             : 
    1228           0 :         hostapd_wps_set_vendor_ext(hapd, hapd->wps);
    1229             : 
    1230           0 :         if (hapd->conf->wps_state)
    1231           0 :                 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           0 : static int wps_add_pin(struct hostapd_data *hapd, void *ctx)
    1248             : {
    1249           0 :         struct wps_add_pin_data *data = ctx;
    1250             :         int ret;
    1251             : 
    1252           0 :         if (hapd->wps == NULL)
    1253           0 :                 return 0;
    1254           0 :         ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr,
    1255             :                                     data->uuid, data->pin, data->pin_len,
    1256             :                                     data->timeout);
    1257           0 :         if (ret == 0)
    1258           0 :                 data->added++;
    1259           0 :         return ret;
    1260             : }
    1261             : 
    1262             : 
    1263           0 : 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           0 :         data.addr = addr;
    1270           0 :         data.uuid = u;
    1271           0 :         data.pin = (const u8 *) pin;
    1272           0 :         data.pin_len = os_strlen(pin);
    1273           0 :         data.timeout = timeout;
    1274           0 :         data.added = 0;
    1275             : 
    1276           0 :         if (os_strcmp(uuid, "any") == 0)
    1277           0 :                 data.uuid = NULL;
    1278             :         else {
    1279           0 :                 if (uuid_str2bin(uuid, u))
    1280           0 :                         return -1;
    1281           0 :                 data.uuid = u;
    1282             :         }
    1283           0 :         if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0)
    1284           0 :                 return -1;
    1285           0 :         return data.added ? 0 : -1;
    1286             : }
    1287             : 
    1288             : 
    1289           0 : static int wps_button_pushed(struct hostapd_data *hapd, void *ctx)
    1290             : {
    1291           0 :         const u8 *p2p_dev_addr = ctx;
    1292           0 :         if (hapd->wps == NULL)
    1293           0 :                 return -1;
    1294           0 :         return wps_registrar_button_pushed(hapd->wps->registrar, p2p_dev_addr);
    1295             : }
    1296             : 
    1297             : 
    1298           0 : int hostapd_wps_button_pushed(struct hostapd_data *hapd,
    1299             :                               const u8 *p2p_dev_addr)
    1300             : {
    1301           0 :         return hostapd_wps_for_each(hapd, wps_button_pushed,
    1302             :                                     (void *) p2p_dev_addr);
    1303             : }
    1304             : 
    1305             : 
    1306           0 : static int wps_cancel(struct hostapd_data *hapd, void *ctx)
    1307             : {
    1308           0 :         if (hapd->wps == NULL)
    1309           0 :                 return -1;
    1310             : 
    1311           0 :         wps_registrar_wps_cancel(hapd->wps->registrar);
    1312           0 :         ap_for_each_sta(hapd, ap_sta_wps_cancel, NULL);
    1313             : 
    1314           0 :         return 0;
    1315             : }
    1316             : 
    1317             : 
    1318           0 : int hostapd_wps_cancel(struct hostapd_data *hapd)
    1319             : {
    1320           0 :         return hostapd_wps_for_each(hapd, wps_cancel, NULL);
    1321             : }
    1322             : 
    1323             : 
    1324           0 : 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           0 :         struct hostapd_data *hapd = ctx;
    1330             :         struct wpabuf *wps_ie;
    1331             :         struct ieee802_11_elems elems;
    1332             : 
    1333           0 :         if (hapd->wps == NULL)
    1334           0 :                 return 0;
    1335             : 
    1336           0 :         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           0 :         if (elems.ssid && elems.ssid_len > 0 &&
    1343           0 :             (elems.ssid_len != hapd->conf->ssid.ssid_len ||
    1344           0 :              os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
    1345             :              0))
    1346           0 :                 return 0; /* Not for us */
    1347             : 
    1348           0 :         wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
    1349           0 :         if (wps_ie == NULL)
    1350           0 :                 return 0;
    1351           0 :         if (wps_validate_probe_req(wps_ie, addr) < 0) {
    1352           0 :                 wpabuf_free(wps_ie);
    1353           0 :                 return 0;
    1354             :         }
    1355             : 
    1356           0 :         if (wpabuf_len(wps_ie) > 0) {
    1357           0 :                 int p2p_wildcard = 0;
    1358             : #ifdef CONFIG_P2P
    1359             :                 if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
    1360             :                     os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
    1361             :                               P2P_WILDCARD_SSID_LEN) == 0)
    1362             :                         p2p_wildcard = 1;
    1363             : #endif /* CONFIG_P2P */
    1364           0 :                 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           0 :                 if (!p2p_wildcard)
    1370           0 :                         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           0 :         wpabuf_free(wps_ie);
    1377             : 
    1378           0 :         return 0;
    1379             : }
    1380             : 
    1381             : 
    1382             : #ifdef CONFIG_WPS_UPNP
    1383             : 
    1384           0 : 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           0 :         struct hostapd_data *hapd = priv;
    1390             :         struct sta_info *sta;
    1391             :         struct upnp_pending_message *p;
    1392             : 
    1393           0 :         wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
    1394           0 :                    MACSTR, ev_type, MAC2STR(mac_addr));
    1395           0 :         wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
    1396             :                     wpabuf_head(msg), wpabuf_len(msg));
    1397           0 :         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           0 :         sta = ap_get_sta(hapd, mac_addr);
    1409             : #ifndef CONFIG_WPS_STRICT
    1410           0 :         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           0 :         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           0 :         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           0 :         p = os_zalloc(sizeof(*p));
    1441           0 :         if (p == NULL)
    1442           0 :                 return -1;
    1443           0 :         os_memcpy(p->addr, sta->addr, ETH_ALEN);
    1444           0 :         p->msg = wpabuf_dup(msg);
    1445           0 :         p->type = msg_type;
    1446           0 :         p->next = hapd->wps->upnp_msgs;
    1447           0 :         hapd->wps->upnp_msgs = p;
    1448             : 
    1449           0 :         return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
    1450             : }
    1451             : 
    1452             : 
    1453           0 : 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           0 :         if (!hapd->conf->upnp_iface)
    1459           0 :                 return 0;
    1460           0 :         ctx = os_zalloc(sizeof(*ctx));
    1461           0 :         if (ctx == NULL)
    1462           0 :                 return -1;
    1463             : 
    1464           0 :         ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
    1465           0 :         if (hapd->conf->ap_pin)
    1466           0 :                 ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
    1467             : 
    1468           0 :         hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd,
    1469           0 :                                               hapd->conf->upnp_iface);
    1470           0 :         if (hapd->wps_upnp == NULL)
    1471           0 :                 return -1;
    1472           0 :         wps->wps_upnp = hapd->wps_upnp;
    1473             : 
    1474           0 :         return 0;
    1475             : }
    1476             : 
    1477             : 
    1478           0 : static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
    1479             : {
    1480           0 :         upnp_wps_device_deinit(hapd->wps_upnp, hapd);
    1481           0 : }
    1482             : 
    1483             : #endif /* CONFIG_WPS_UPNP */
    1484             : 
    1485             : 
    1486           0 : int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
    1487             :                             char *buf, size_t buflen)
    1488             : {
    1489           0 :         if (hapd->wps == NULL)
    1490           0 :                 return 0;
    1491           0 :         return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
    1492             : }
    1493             : 
    1494             : 
    1495           0 : static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
    1496             : {
    1497           0 :         struct hostapd_data *hapd = eloop_data;
    1498           0 :         wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
    1499           0 :         hostapd_wps_ap_pin_disable(hapd);
    1500           0 :         wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_PIN_DISABLED);
    1501           0 : }
    1502             : 
    1503             : 
    1504           0 : static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
    1505             : {
    1506           0 :         wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
    1507           0 :         hapd->ap_pin_failures = 0;
    1508           0 :         hapd->ap_pin_failures_consecutive = 0;
    1509           0 :         hapd->conf->ap_setup_locked = 0;
    1510           0 :         if (hapd->wps->ap_setup_locked) {
    1511           0 :                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
    1512           0 :                 hapd->wps->ap_setup_locked = 0;
    1513           0 :                 wps_registrar_update_ie(hapd->wps->registrar);
    1514             :         }
    1515           0 :         eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
    1516           0 :         if (timeout > 0)
    1517           0 :                 eloop_register_timeout(timeout, 0,
    1518             :                                        hostapd_wps_ap_pin_timeout, hapd, NULL);
    1519           0 : }
    1520             : 
    1521             : 
    1522           0 : static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx)
    1523             : {
    1524           0 :         os_free(hapd->conf->ap_pin);
    1525           0 :         hapd->conf->ap_pin = NULL;
    1526             : #ifdef CONFIG_WPS_UPNP
    1527           0 :         upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
    1528             : #endif /* CONFIG_WPS_UPNP */
    1529           0 :         eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
    1530           0 :         return 0;
    1531             : }
    1532             : 
    1533             : 
    1534           0 : void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
    1535             : {
    1536           0 :         wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
    1537           0 :         hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL);
    1538           0 : }
    1539             : 
    1540             : 
    1541             : struct wps_ap_pin_data {
    1542             :         char pin_txt[9];
    1543             :         int timeout;
    1544             : };
    1545             : 
    1546             : 
    1547           0 : static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx)
    1548             : {
    1549           0 :         struct wps_ap_pin_data *data = ctx;
    1550           0 :         os_free(hapd->conf->ap_pin);
    1551           0 :         hapd->conf->ap_pin = os_strdup(data->pin_txt);
    1552             : #ifdef CONFIG_WPS_UPNP
    1553           0 :         upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt);
    1554             : #endif /* CONFIG_WPS_UPNP */
    1555           0 :         hostapd_wps_ap_pin_enable(hapd, data->timeout);
    1556           0 :         return 0;
    1557             : }
    1558             : 
    1559             : 
    1560           0 : 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           0 :         pin = wps_generate_pin();
    1566           0 :         os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%08u", pin);
    1567           0 :         data.timeout = timeout;
    1568           0 :         hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
    1569           0 :         return hapd->conf->ap_pin;
    1570             : }
    1571             : 
    1572             : 
    1573           0 : const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
    1574             : {
    1575           0 :         return hapd->conf->ap_pin;
    1576             : }
    1577             : 
    1578             : 
    1579           0 : 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           0 :         ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin);
    1586           0 :         if (ret < 0 || ret >= (int) sizeof(data.pin_txt))
    1587           0 :                 return -1;
    1588           0 :         data.timeout = timeout;
    1589           0 :         return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
    1590             : }
    1591             : 
    1592             : 
    1593           0 : static int wps_update_ie(struct hostapd_data *hapd, void *ctx)
    1594             : {
    1595           0 :         if (hapd->wps)
    1596           0 :                 wps_registrar_update_ie(hapd->wps->registrar);
    1597           0 :         return 0;
    1598             : }
    1599             : 
    1600             : 
    1601           0 : void hostapd_wps_update_ie(struct hostapd_data *hapd)
    1602             : {
    1603           0 :         hostapd_wps_for_each(hapd, wps_update_ie, NULL);
    1604           0 : }
    1605             : 
    1606             : 
    1607           0 : 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           0 :         os_memset(&cred, 0, sizeof(cred));
    1614             : 
    1615           0 :         len = os_strlen(ssid);
    1616           0 :         if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
    1617           0 :             hexstr2bin(ssid, cred.ssid, len / 2))
    1618           0 :                 return -1;
    1619           0 :         cred.ssid_len = len / 2;
    1620             : 
    1621           0 :         if (os_strncmp(auth, "OPEN", 4) == 0)
    1622           0 :                 cred.auth_type = WPS_AUTH_OPEN;
    1623           0 :         else if (os_strncmp(auth, "WPAPSK", 6) == 0)
    1624           0 :                 cred.auth_type = WPS_AUTH_WPAPSK;
    1625           0 :         else if (os_strncmp(auth, "WPA2PSK", 7) == 0)
    1626           0 :                 cred.auth_type = WPS_AUTH_WPA2PSK;
    1627             :         else
    1628           0 :                 return -1;
    1629             : 
    1630           0 :         if (encr) {
    1631           0 :                 if (os_strncmp(encr, "NONE", 4) == 0)
    1632           0 :                         cred.encr_type = WPS_ENCR_NONE;
    1633           0 :                 else if (os_strncmp(encr, "TKIP", 4) == 0)
    1634           0 :                         cred.encr_type = WPS_ENCR_TKIP;
    1635           0 :                 else if (os_strncmp(encr, "CCMP", 4) == 0)
    1636           0 :                         cred.encr_type = WPS_ENCR_AES;
    1637             :                 else
    1638           0 :                         return -1;
    1639             :         } else
    1640           0 :                 cred.encr_type = WPS_ENCR_NONE;
    1641             : 
    1642           0 :         if (key) {
    1643           0 :                 len = os_strlen(key);
    1644           0 :                 if ((len & 1) || len > 2 * sizeof(cred.key) ||
    1645           0 :                     hexstr2bin(key, cred.key, len / 2))
    1646           0 :                         return -1;
    1647           0 :                 cred.key_len = len / 2;
    1648             :         }
    1649             : 
    1650           0 :         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           0 : static int wps_add_nfc_password_token(struct hostapd_data *hapd, void *ctx)
    1664             : {
    1665           0 :         struct wps_nfc_password_token_data *data = ctx;
    1666             :         int ret;
    1667             : 
    1668           0 :         if (hapd->wps == NULL)
    1669           0 :                 return 0;
    1670           0 :         ret = wps_registrar_add_nfc_password_token(hapd->wps->registrar,
    1671             :                                                    data->oob_dev_pw,
    1672             :                                                    data->oob_dev_pw_len);
    1673           0 :         if (ret == 0)
    1674           0 :                 data->added++;
    1675           0 :         return ret;
    1676             : }
    1677             : 
    1678             : 
    1679           0 : 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           0 :         data.oob_dev_pw = attr->oob_dev_password;
    1685           0 :         data.oob_dev_pw_len = attr->oob_dev_password_len;
    1686           0 :         data.added = 0;
    1687           0 :         if (hostapd_wps_for_each(hapd, wps_add_nfc_password_token, &data) < 0)
    1688           0 :                 return -1;
    1689           0 :         return data.added ? 0 : -1;
    1690             : }
    1691             : 
    1692             : 
    1693           0 : 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           0 :         wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
    1699             : 
    1700           0 :         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           0 :         if (attr.oob_dev_password)
    1706           0 :                 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           0 : int hostapd_wps_nfc_tag_read(struct hostapd_data *hapd,
    1714             :                              const struct wpabuf *data)
    1715             : {
    1716           0 :         const struct wpabuf *wps = data;
    1717           0 :         struct wpabuf *tmp = NULL;
    1718             :         int ret;
    1719             : 
    1720           0 :         if (wpabuf_len(data) < 4)
    1721           0 :                 return -1;
    1722             : 
    1723           0 :         if (*wpabuf_head_u8(data) != 0x10) {
    1724             :                 /* Assume this contains full NDEF record */
    1725           0 :                 tmp = ndef_parse_wifi(data);
    1726           0 :                 if (tmp == NULL) {
    1727           0 :                         wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
    1728           0 :                         return -1;
    1729             :                 }
    1730           0 :                 wps = tmp;
    1731             :         }
    1732             : 
    1733           0 :         ret = hostapd_wps_nfc_tag_process(hapd, wps);
    1734           0 :         wpabuf_free(tmp);
    1735           0 :         return ret;
    1736             : }
    1737             : 
    1738             : 
    1739           0 : struct wpabuf * hostapd_wps_nfc_config_token(struct hostapd_data *hapd,
    1740             :                                              int ndef)
    1741             : {
    1742             :         struct wpabuf *ret;
    1743             : 
    1744           0 :         if (hapd->wps == NULL)
    1745           0 :                 return NULL;
    1746             : 
    1747           0 :         ret = wps_get_oob_cred(hapd->wps, hostapd_wps_rf_band_cb(hapd),
    1748           0 :                                hapd->iconf->channel);
    1749           0 :         if (ndef && ret) {
    1750             :                 struct wpabuf *tmp;
    1751           0 :                 tmp = ndef_build_wifi(ret);
    1752           0 :                 wpabuf_free(ret);
    1753           0 :                 if (tmp == NULL)
    1754           0 :                         return NULL;
    1755           0 :                 ret = tmp;
    1756             :         }
    1757             : 
    1758           0 :         return ret;
    1759             : }
    1760             : 
    1761             : 
    1762           0 : struct wpabuf * hostapd_wps_nfc_hs_cr(struct hostapd_data *hapd, int ndef)
    1763             : {
    1764             :         struct wpabuf *ret;
    1765             : 
    1766           0 :         if (hapd->wps == NULL)
    1767           0 :                 return NULL;
    1768             : 
    1769           0 :         if (hapd->conf->wps_nfc_dh_pubkey == NULL) {
    1770           0 :                 struct wps_context *wps = hapd->wps;
    1771           0 :                 if (wps_nfc_gen_dh(&hapd->conf->wps_nfc_dh_pubkey,
    1772           0 :                                    &hapd->conf->wps_nfc_dh_privkey) < 0)
    1773           0 :                         return NULL;
    1774           0 :                 hostapd_wps_nfc_clear(wps);
    1775           0 :                 wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
    1776           0 :                 wps->ap_nfc_dh_pubkey =
    1777           0 :                         wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
    1778           0 :                 wps->ap_nfc_dh_privkey =
    1779           0 :                         wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
    1780           0 :                 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           0 :         ret = wps_build_nfc_handover_sel(hapd->wps,
    1787           0 :                                          hapd->conf->wps_nfc_dh_pubkey,
    1788           0 :                                          hapd->own_addr, hapd->iface->freq);
    1789             : 
    1790           0 :         if (ndef && ret) {
    1791             :                 struct wpabuf *tmp;
    1792           0 :                 tmp = ndef_build_wifi(ret);
    1793           0 :                 wpabuf_free(ret);
    1794           0 :                 if (tmp == NULL)
    1795           0 :                         return NULL;
    1796           0 :                 ret = tmp;
    1797             :         }
    1798             : 
    1799           0 :         return ret;
    1800             : }
    1801             : 
    1802             : 
    1803           0 : 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           0 :         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           0 :         wps = ndef_parse_wifi(req);
    1820           0 :         if (wps == NULL)
    1821           0 :                 return -1;
    1822           0 :         wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
    1823             :                    "payload from NFC connection handover");
    1824           0 :         wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
    1825           0 :         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           0 :         pos = wpabuf_head(wps);
    1831           0 :         wsc_len = WPA_GET_BE16(pos);
    1832           0 :         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           0 :         pos += 2;
    1838             : 
    1839           0 :         wpa_hexdump(MSG_DEBUG,
    1840             :                     "WPS: WSC attributes in Wi-Fi Handover Request Message",
    1841             :                     pos, wsc_len);
    1842           0 :         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           0 :         wpabuf_set(&msg, pos, wsc_len);
    1849           0 :         ret = wps_parse_msg(&msg, &attr);
    1850           0 :         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           0 :         if (attr.oob_dev_password == NULL ||
    1857           0 :             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           0 :         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           0 :         wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
    1872             : 
    1873           0 :         wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
    1874           0 :                     attr.oob_dev_password, attr.oob_dev_password_len);
    1875           0 :         dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
    1876             :                                  WPS_OOB_PUBKEY_HASH_LEN);
    1877           0 :         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           0 :         wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
    1884           0 :                     attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
    1885             : 
    1886           0 :         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           0 :         wpabuf_free(wps);
    1893           0 :         return ret;
    1894             : }
    1895             : 
    1896             : 
    1897           0 : struct wpabuf * hostapd_wps_nfc_token_gen(struct hostapd_data *hapd, int ndef)
    1898             : {
    1899           0 :         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           0 :         return wps_nfc_token_gen(ndef, &hapd->conf->wps_nfc_dev_pw_id,
    1907           0 :                                  &hapd->conf->wps_nfc_dh_pubkey,
    1908           0 :                                  &hapd->conf->wps_nfc_dh_privkey,
    1909           0 :                                  &hapd->conf->wps_nfc_dev_pw);
    1910             : }
    1911             : 
    1912             : 
    1913           0 : int hostapd_wps_nfc_token_enable(struct hostapd_data *hapd)
    1914             : {
    1915           0 :         struct wps_context *wps = hapd->wps;
    1916             :         struct wpabuf *pw;
    1917             : 
    1918           0 :         if (wps == NULL)
    1919           0 :                 return -1;
    1920             : 
    1921           0 :         if (!hapd->conf->wps_nfc_dh_pubkey ||
    1922           0 :             !hapd->conf->wps_nfc_dh_privkey ||
    1923           0 :             !hapd->conf->wps_nfc_dev_pw ||
    1924           0 :             !hapd->conf->wps_nfc_dev_pw_id)
    1925           0 :                 return -1;
    1926             : 
    1927           0 :         hostapd_wps_nfc_clear(wps);
    1928           0 :         wpa_printf(MSG_DEBUG,
    1929             :                    "WPS: Enable NFC Tag (Dev Pw Id %u) for AP interface %s (context %p)",
    1930           0 :                    hapd->conf->wps_nfc_dev_pw_id, hapd->conf->iface, wps);
    1931           0 :         wps->ap_nfc_dev_pw_id = hapd->conf->wps_nfc_dev_pw_id;
    1932           0 :         wps->ap_nfc_dh_pubkey = wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
    1933           0 :         wps->ap_nfc_dh_privkey = wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
    1934           0 :         pw = hapd->conf->wps_nfc_dev_pw;
    1935           0 :         wps->ap_nfc_dev_pw = wpabuf_alloc(
    1936           0 :                 wpabuf_len(pw) * 2 + 1);
    1937           0 :         if (wps->ap_nfc_dev_pw) {
    1938           0 :                 wpa_snprintf_hex_uppercase(
    1939           0 :                         (char *) wpabuf_put(wps->ap_nfc_dev_pw,
    1940           0 :                                             wpabuf_len(pw) * 2),
    1941           0 :                         wpabuf_len(pw) * 2 + 1,
    1942           0 :                         wpabuf_head(pw), wpabuf_len(pw));
    1943             :         }
    1944             : 
    1945           0 :         if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey ||
    1946           0 :             !wps->ap_nfc_dev_pw) {
    1947           0 :                 hostapd_wps_nfc_clear(wps);
    1948           0 :                 return -1;
    1949             :         }
    1950             : 
    1951           0 :         return 0;
    1952             : }
    1953             : 
    1954             : 
    1955           0 : void hostapd_wps_nfc_token_disable(struct hostapd_data *hapd)
    1956             : {
    1957           0 :         wpa_printf(MSG_DEBUG, "WPS: Disable NFC token for AP interface %s",
    1958           0 :                    hapd->conf->iface);
    1959           0 :         hostapd_wps_nfc_clear(hapd->wps);
    1960           0 : }
    1961             : 
    1962             : #endif /* CONFIG_WPS_NFC */

Generated by: LCOV version 1.10