WPS: Fix local configuration update after AP configuration
[wpasupplicant] / src / wps / wps_registrar.c
index d89cf17..9f9e0ac 100644 (file)
@@ -21,6 +21,8 @@
 #include "eloop.h"
 #include "wps_i.h"
 #include "wps_dev_attr.h"
+#include "wps_upnp.h"
+#include "crypto.h"
 
 
 struct wps_uuid_pin {
@@ -85,6 +87,8 @@ struct wps_registrar {
                         const u8 *probe_resp_ie, size_t probe_resp_ie_len);
        void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
                              const struct wps_device_data *dev);
+       void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
+                              const u8 *uuid_e);
        void *cb_ctx;
 
        struct wps_uuid_pin *pins;
@@ -92,11 +96,17 @@ struct wps_registrar {
 
        int skip_cred_build;
        struct wpabuf *extra_cred;
+       int disable_auto_conf;
+       int sel_reg_dev_password_id_override;
+       int sel_reg_config_methods_override;
+       int static_wep_only;
 };
 
 
 static int wps_set_ie(struct wps_registrar *reg);
 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
+static void wps_registrar_set_selected_timeout(void *eloop_ctx,
+                                              void *timeout_ctx);
 
 
 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
@@ -208,6 +218,31 @@ static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
 }
 
 
+#ifdef CONFIG_WPS_UPNP
+static void wps_registrar_free_pending_m2(struct wps_context *wps)
+{
+       struct upnp_pending_message *p, *p2, *prev = NULL;
+       p = wps->upnp_msgs;
+       while (p) {
+               if (p->type == WPS_M2 || p->type == WPS_M2D) {
+                       if (prev == NULL)
+                               wps->upnp_msgs = p->next;
+                       else
+                               prev->next = p->next;
+                       wpa_printf(MSG_DEBUG, "WPS UPnP: Drop pending M2/M2D");
+                       p2 = p;
+                       p = p->next;
+                       wpabuf_free(p2->msg);
+                       os_free(p2);
+                       continue;
+               }
+               prev = p;
+               p = p->next;
+       }
+}
+#endif /* CONFIG_WPS_UPNP */
+
+
 static int wps_build_ap_setup_locked(struct wps_context *wps,
                                     struct wpabuf *msg)
 {
@@ -240,6 +275,8 @@ static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
        u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
        if (!reg->selected_registrar)
                return 0;
+       if (reg->sel_reg_dev_password_id_override >= 0)
+               id = reg->sel_reg_dev_password_id_override;
        wpa_printf(MSG_DEBUG, "WPS:  * Device Password ID (%d)", id);
        wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
        wpabuf_put_be16(msg, 2);
@@ -257,6 +294,8 @@ static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
        methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
        if (reg->pbc)
                methods |= WPS_CONFIG_PUSHBUTTON;
+       if (reg->sel_reg_config_methods_override >= 0)
+               methods = reg->sel_reg_config_methods_override;
        wpa_printf(MSG_DEBUG, "WPS:  * Selected Registrar Config Methods (%x)",
                   methods);
        wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
@@ -325,6 +364,7 @@ wps_registrar_init(struct wps_context *wps,
        reg->new_psk_cb = cfg->new_psk_cb;
        reg->set_ie_cb = cfg->set_ie_cb;
        reg->pin_needed_cb = cfg->pin_needed_cb;
+       reg->reg_success_cb = cfg->reg_success_cb;
        reg->cb_ctx = cfg->cb_ctx;
        reg->skip_cred_build = cfg->skip_cred_build;
        if (cfg->extra_cred) {
@@ -335,6 +375,10 @@ wps_registrar_init(struct wps_context *wps,
                        return NULL;
                }
        }
+       reg->disable_auto_conf = cfg->disable_auto_conf;
+       reg->sel_reg_dev_password_id_override = -1;
+       reg->sel_reg_config_methods_override = -1;
+       reg->static_wep_only = cfg->static_wep_only;
 
        if (wps_set_ie(reg)) {
                wps_registrar_deinit(reg);
@@ -354,6 +398,7 @@ void wps_registrar_deinit(struct wps_registrar *reg)
        if (reg == NULL)
                return;
        eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
+       eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
        wps_free_pins(reg->pins);
        wps_free_pbc_sessions(reg->pbc_sessions);
        wpabuf_free(reg->extra_cred);
@@ -586,8 +631,9 @@ void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
                        "WPS: Probe Request with WPS data received",
                        wps_data);
 
-       if (wps_parse_msg(wps_data, &attr) < 0 ||
-           attr.version == NULL || *attr.version != WPS_VERSION) {
+       if (wps_parse_msg(wps_data, &attr) < 0)
+               return;
+       if (!wps_version_supported(attr.version)) {
                wpa_printf(MSG_DEBUG, "WPS: Unsupported ProbeReq WPS IE "
                           "version 0x%x", attr.version ? *attr.version : 0);
                return;
@@ -630,6 +676,16 @@ static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
 }
 
 
+static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
+                              const u8 *uuid_e)
+{
+       if (reg->reg_success_cb == NULL)
+               return;
+
+       reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e);
+}
+
+
 static int wps_cb_set_ie(struct wps_registrar *reg,
                         const struct wpabuf *beacon_ie,
                         const struct wpabuf *probe_resp_ie)
@@ -724,6 +780,28 @@ static int wps_set_ie(struct wps_registrar *reg)
                return -1;
        }
 
+       if (reg->static_wep_only) {
+               /*
+                * Windows XP and Vista clients can get confused about
+                * EAP-Identity/Request when they probe the network with
+                * EAPOL-Start. In such a case, they may assume the network is
+                * using IEEE 802.1X and prompt user for a certificate while
+                * the correct (non-WPS) behavior would be to ask for the
+                * static WEP key. As a workaround, use Microsoft Provisioning
+                * IE to advertise that legacy 802.1X is not supported.
+                */
+               const u8 ms_wps[7] = {
+                       WLAN_EID_VENDOR_SPECIFIC, 5,
+                       /* Microsoft Provisioning IE (00:50:f2:5) */
+                       0x00, 0x50, 0xf2, 5,
+                       0x00 /* no legacy 802.1X or MS WPS */
+               };
+               wpa_printf(MSG_DEBUG, "WPS: Add Microsoft Provisioning IE "
+                          "into Beacon/Probe Response frames");
+               wpabuf_put_data(beacon, ms_wps, sizeof(ms_wps));
+               wpabuf_put_data(probe, ms_wps, sizeof(ms_wps));
+       }
+
        ret = wps_cb_set_ie(reg, beacon, probe);
        wpabuf_free(beacon);
        wpabuf_free(probe);
@@ -905,7 +983,8 @@ static int wps_build_cred_network_key(struct wpabuf *msg,
 static int wps_build_cred_mac_addr(struct wpabuf *msg,
                                   struct wps_credential *cred)
 {
-       wpa_printf(MSG_DEBUG, "WPS:  * MAC Address");
+       wpa_printf(MSG_DEBUG, "WPS:  * MAC Address (" MACSTR ")",
+                  MAC2STR(cred->mac_addr));
        wpabuf_put_be16(msg, ATTR_MAC_ADDR);
        wpabuf_put_be16(msg, ETH_ALEN);
        wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
@@ -927,7 +1006,7 @@ static int wps_build_credential(struct wpabuf *msg,
 }
 
 
-static int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
+int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
 {
        struct wpabuf *cred;
 
@@ -979,9 +1058,11 @@ static int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
                }
        }
        wps->cred.encr_type = wps->encr_type;
-       os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
+       /* Set MAC address in the Credential to be the AP's address (BSSID) */
+       os_memcpy(wps->cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN);
 
-       if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap) {
+       if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
+           !wps->wps->registrar->disable_auto_conf) {
                u8 r[16];
                /* Generate a random passphrase */
                if (os_get_random(r, sizeof(r)) < 0)
@@ -1088,7 +1169,7 @@ static struct wpabuf * wps_build_m2(struct wps_data *wps)
            wps_build_rf_bands(&wps->wps->dev, msg) ||
            wps_build_assoc_state(wps, msg) ||
            wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
-           wps_build_dev_password_id(msg, DEV_PW_DEFAULT) ||
+           wps_build_dev_password_id(msg, wps->dev_pw_id) ||
            wps_build_os_version(&wps->wps->dev, msg) ||
            wps_build_authenticator(wps, msg)) {
                wpabuf_free(msg);
@@ -1293,6 +1374,39 @@ struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
 {
        struct wpabuf *msg;
 
+#ifdef CONFIG_WPS_UPNP
+       if (wps->wps->wps_upnp) {
+               struct upnp_pending_message *p, *prev = NULL;
+               if (wps->ext_reg > 1)
+                       wps_registrar_free_pending_m2(wps->wps);
+               p = wps->wps->upnp_msgs;
+               /* TODO: check pending message MAC address */
+               while (p && p->next) {
+                       prev = p;
+                       p = p->next;
+               }
+               if (p) {
+                       wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
+                                  "UPnP");
+                       if (prev)
+                               prev->next = NULL;
+                       else
+                               wps->wps->upnp_msgs = NULL;
+                       msg = p->msg;
+                       os_free(p);
+                       *op_code = WSC_MSG;
+                       if (wps->ext_reg == 0)
+                               wps->ext_reg = 1;
+                       return msg;
+               }
+       }
+       if (wps->ext_reg) {
+               wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
+                          "pending message available");
+               return NULL;
+       }
+#endif /* CONFIG_WPS_UPNP */
+
        switch (wps->state) {
        case SEND_M2:
                if (wps_get_dev_password(wps) < 0)
@@ -1459,6 +1573,7 @@ static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
                wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
                           "not match with the pre-committed value");
                wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
+               wps_pwd_auth_fail_event(wps->wps, 0, 1);
                return -1;
        }
 
@@ -1499,6 +1614,7 @@ static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
                           "not match with the pre-committed value");
                wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
                wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
+               wps_pwd_auth_fail_event(wps->wps, 0, 2);
                return -1;
        }
 
@@ -1535,6 +1651,22 @@ static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
                return -1;
        }
 
+#ifdef CONFIG_WPS_OOB
+       if (wps->wps->oob_conf.pubkey_hash != NULL) {
+               const u8 *addr[1];
+               u8 hash[WPS_HASH_LEN];
+
+               addr[0] = pk;
+               sha256_vector(1, addr, &pk_len, hash);
+               if (os_memcmp(hash,
+                             wpabuf_head(wps->wps->oob_conf.pubkey_hash),
+                             WPS_OOB_PUBKEY_HASH_LEN) != 0) {
+                       wpa_printf(MSG_ERROR, "WPS: Public Key hash error");
+                       return -1;
+               }
+       }
+#endif /* CONFIG_WPS_OOB */
+
        wpabuf_free(wps->dh_pubkey_e);
        wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
        if (wps->dh_pubkey_e == NULL)
@@ -1702,7 +1834,8 @@ static enum wps_process_res wps_process_m1(struct wps_data *wps,
            wps_process_os_version(&wps->peer_dev, attr->os_version))
                return WPS_FAILURE;
 
-       if (wps->dev_pw_id != DEV_PW_DEFAULT &&
+       if (wps->dev_pw_id < 0x10 &&
+           wps->dev_pw_id != DEV_PW_DEFAULT &&
            wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
            wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
            wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
@@ -1714,6 +1847,16 @@ static enum wps_process_res wps_process_m1(struct wps_data *wps,
                return WPS_CONTINUE;
        }
 
+#ifdef CONFIG_WPS_OOB
+       if (wps->dev_pw_id >= 0x10 &&
+           wps->dev_pw_id != wps->wps->oob_dev_pw_id) {
+               wpa_printf(MSG_DEBUG, "WPS: OOB Device Password ID "
+                          "%d mismatch", wps->dev_pw_id);
+               wps->state = SEND_M2D;
+               return WPS_CONTINUE;
+       }
+#endif /* CONFIG_WPS_OOB */
+
        if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
                if (wps_registrar_pbc_overlap(wps->wps->registrar,
                                              wps->mac_addr_e, wps->uuid_e)) {
@@ -1886,7 +2029,7 @@ static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
        if (wps_parse_msg(msg, &attr) < 0)
                return WPS_FAILURE;
 
-       if (attr.version == NULL || *attr.version != WPS_VERSION) {
+       if (!wps_version_supported(attr.version)) {
                wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
                           attr.version ? *attr.version : 0);
                return WPS_FAILURE;
@@ -1907,6 +2050,17 @@ static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
 
        switch (*attr.msg_type) {
        case WPS_M1:
+#ifdef CONFIG_WPS_UPNP
+               if (wps->wps->wps_upnp && attr.mac_addr) {
+                       /* Remove old pending messages when starting new run */
+                       wps_free_pending_msgs(wps->wps->upnp_msgs);
+                       wps->wps->upnp_msgs = NULL;
+
+                       upnp_wps_device_send_wlan_event(
+                               wps->wps->wps_upnp, attr.mac_addr,
+                               UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
+               }
+#endif /* CONFIG_WPS_UPNP */
                ret = wps_process_m1(wps, &attr);
                break;
        case WPS_M3:
@@ -1951,7 +2105,7 @@ static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
        if (wps_parse_msg(msg, &attr) < 0)
                return WPS_FAILURE;
 
-       if (attr.version == NULL || *attr.version != WPS_VERSION) {
+       if (!wps_version_supported(attr.version)) {
                wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
                           attr.version ? *attr.version : 0);
                return WPS_FAILURE;
@@ -1968,6 +2122,17 @@ static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
                return WPS_FAILURE;
        }
 
+#ifdef CONFIG_WPS_UPNP
+       if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
+           upnp_wps_subscribers(wps->wps->wps_upnp)) {
+               if (wps->wps->upnp_msgs)
+                       return WPS_CONTINUE;
+               wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
+                          "external Registrar");
+               return WPS_PENDING;
+       }
+#endif /* CONFIG_WPS_UPNP */
+
        if (attr.registrar_nonce == NULL ||
            os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
        {
@@ -1982,8 +2147,18 @@ static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
        }
 
        if (wps->state == RECV_M2D_ACK) {
-               /* TODO: support for multiple registrars and sending of
-                * multiple M2/M2D messages */
+#ifdef CONFIG_WPS_UPNP
+               if (wps->wps->wps_upnp &&
+                   upnp_wps_subscribers(wps->wps->wps_upnp)) {
+                       if (wps->wps->upnp_msgs)
+                               return WPS_CONTINUE;
+                       if (wps->ext_reg == 0)
+                               wps->ext_reg = 1;
+                       wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
+                                  "external Registrar");
+                       return WPS_PENDING;
+               }
+#endif /* CONFIG_WPS_UPNP */
 
                wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
                           "terminate negotiation");
@@ -2007,7 +2182,7 @@ static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
        if (wps_parse_msg(msg, &attr) < 0)
                return WPS_FAILURE;
 
-       if (attr.version == NULL || *attr.version != WPS_VERSION) {
+       if (!wps_version_supported(attr.version)) {
                wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
                           attr.version ? *attr.version : 0);
                return WPS_FAILURE;
@@ -2024,6 +2199,14 @@ static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
                return WPS_FAILURE;
        }
 
+#ifdef CONFIG_WPS_UPNP
+       if (wps->wps->wps_upnp && wps->ext_reg) {
+               wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
+                          "Registrar terminated by the Enrollee");
+               return WPS_FAILURE;
+       }
+#endif /* CONFIG_WPS_UPNP */
+
        if (attr.registrar_nonce == NULL ||
            os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
        {
@@ -2074,7 +2257,8 @@ static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
 
        wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
 
-       if (wps->state != RECV_DONE) {
+       if (wps->state != RECV_DONE &&
+           (!wps->wps->wps_upnp || !wps->ext_reg)) {
                wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
                           "receiving WSC_Done", wps->state);
                return WPS_FAILURE;
@@ -2083,7 +2267,7 @@ static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
        if (wps_parse_msg(msg, &attr) < 0)
                return WPS_FAILURE;
 
-       if (attr.version == NULL || *attr.version != WPS_VERSION) {
+       if (!wps_version_supported(attr.version)) {
                wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
                           attr.version ? *attr.version : 0);
                return WPS_FAILURE;
@@ -2100,6 +2284,14 @@ static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
                return WPS_FAILURE;
        }
 
+#ifdef CONFIG_WPS_UPNP
+       if (wps->wps->wps_upnp && wps->ext_reg) {
+               wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
+                          "Registrar completed successfully");
+               return WPS_DONE;
+       }
+#endif /* CONFIG_WPS_UPNP */
+
        if (attr.registrar_nonce == NULL ||
            os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
        {
@@ -2116,7 +2308,7 @@ static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
        wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
 
        if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
-           wps->wps->ap) {
+           wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
                struct wps_credential cred;
 
                wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
@@ -2142,6 +2334,19 @@ static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
        }
 
        if (!wps->wps->ap) {
+               /*
+                * Update credential to only include a single authentication
+                * and encryption type in case the AP configuration includes
+                * more than one option.
+                */
+               if (wps->cred.auth_type & WPS_AUTH_WPA2PSK)
+                       wps->cred.auth_type = WPS_AUTH_WPA2PSK;
+               else if (wps->cred.auth_type & WPS_AUTH_WPAPSK)
+                       wps->cred.auth_type = WPS_AUTH_WPAPSK;
+               if (wps->cred.encr_type & WPS_ENCR_AES)
+                       wps->cred.encr_type = WPS_ENCR_AES;
+               else if (wps->cred.encr_type & WPS_ENCR_TKIP)
+                       wps->cred.encr_type = WPS_ENCR_TKIP;
                wpa_printf(MSG_DEBUG, "WPS: Update local configuration based "
                           "on the modified AP configuration");
                if (wps->wps->cred_cb)
@@ -2158,6 +2363,8 @@ static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
                wps->new_psk = NULL;
        }
 
+       wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e);
+
        if (wps->pbc) {
                wps_registrar_remove_pbc_session(wps->wps->registrar,
                                                 wps->mac_addr_e, wps->uuid_e);
@@ -2180,6 +2387,39 @@ enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
                   "op_code=%d)",
                   (unsigned long) wpabuf_len(msg), op_code);
 
+#ifdef CONFIG_WPS_UPNP
+       if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
+               struct wps_parse_attr attr;
+               if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
+                   *attr.msg_type == WPS_M3)
+                       wps->ext_reg = 2; /* past M2/M2D phase */
+       }
+       if (wps->ext_reg > 1)
+               wps_registrar_free_pending_m2(wps->wps);
+       if (wps->wps->wps_upnp && wps->ext_reg &&
+           wps->wps->upnp_msgs == NULL &&
+           (op_code == WSC_MSG || op_code == WSC_Done)) {
+               struct wps_parse_attr attr;
+               int type;
+               if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
+                       type = -1;
+               else
+                       type = *attr.msg_type;
+               wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
+                          " to external Registrar for processing", type);
+               upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
+                                               wps->mac_addr_e,
+                                               UPNP_WPS_WLANEVENT_TYPE_EAP,
+                                               msg);
+               if (op_code == WSC_MSG)
+                       return WPS_PENDING;
+       } else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
+               wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
+                          "external Registrar");
+               return WPS_CONTINUE;
+       }
+#endif /* CONFIG_WPS_UPNP */
+
        switch (op_code) {
        case WSC_MSG:
                return wps_process_wsc_msg(wps, msg);
@@ -2199,3 +2439,74 @@ enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
                return WPS_FAILURE;
        }
 }
+
+
+int wps_registrar_update_ie(struct wps_registrar *reg)
+{
+       return wps_set_ie(reg);
+}
+
+
+static void wps_registrar_set_selected_timeout(void *eloop_ctx,
+                                              void *timeout_ctx)
+{
+       struct wps_registrar *reg = eloop_ctx;
+
+       wpa_printf(MSG_DEBUG, "WPS: SetSelectedRegistrar timed out - "
+                  "unselect Registrar");
+       reg->selected_registrar = 0;
+       reg->pbc = 0;
+       reg->sel_reg_dev_password_id_override = -1;
+       reg->sel_reg_config_methods_override = -1;
+       wps_set_ie(reg);
+}
+
+
+/**
+ * wps_registrar_set_selected_registrar - Notification of SetSelectedRegistrar
+ * @reg: Registrar data from wps_registrar_init()
+ * @msg: Received message from SetSelectedRegistrar
+ * Returns: 0 on success, -1 on failure
+ *
+ * This function is called when an AP receives a SetSelectedRegistrar UPnP
+ * message.
+ */
+int wps_registrar_set_selected_registrar(struct wps_registrar *reg,
+                                        const struct wpabuf *msg)
+{
+       struct wps_parse_attr attr;
+
+       wpa_hexdump_buf(MSG_MSGDUMP, "WPS: SetSelectedRegistrar attributes",
+                       msg);
+
+       if (wps_parse_msg(msg, &attr) < 0)
+               return -1;
+       if (!wps_version_supported(attr.version)) {
+               wpa_printf(MSG_DEBUG, "WPS: Unsupported SetSelectedRegistrar "
+                          "version 0x%x", attr.version ? *attr.version : 0);
+               return -1;
+       }
+
+       if (attr.selected_registrar == NULL ||
+           *attr.selected_registrar == 0) {
+               wpa_printf(MSG_DEBUG, "WPS: SetSelectedRegistrar: Disable "
+                          "Selected Registrar");
+               eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg,
+                                    NULL);
+               wps_registrar_set_selected_timeout(reg, NULL);
+               return 0;
+       }
+
+       reg->selected_registrar = 1;
+       reg->sel_reg_dev_password_id_override = attr.dev_password_id ?
+               WPA_GET_BE16(attr.dev_password_id) : DEV_PW_DEFAULT;
+       reg->sel_reg_config_methods_override = attr.sel_reg_config_methods ?
+               WPA_GET_BE16(attr.sel_reg_config_methods) : -1;
+       wps_set_ie(reg);
+
+       eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
+       eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
+                              wps_registrar_set_selected_timeout,
+                              reg, NULL);
+       return 0;
+}