LCOV - code coverage report
Current view: top level - src/ap - wps_hostapd.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1443382998 Lines: 923 1089 84.8 %
Date: 2015-09-27 Functions: 72 72 100.0 %

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

Generated by: LCOV version 1.10