2 * Wi-Fi Protected Setup - Registrar
3 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
20 #include "ieee802_11_defs.h"
23 #include "wps_dev_attr.h"
29 struct wps_uuid_pin *next;
30 u8 uuid[WPS_UUID_LEN];
38 static void wps_free_pin(struct wps_uuid_pin *pin)
45 static void wps_free_pins(struct wps_uuid_pin *pins)
47 struct wps_uuid_pin *pin, *prev;
58 struct wps_pbc_session {
59 struct wps_pbc_session *next;
61 u8 uuid_e[WPS_UUID_LEN];
62 struct os_time timestamp;
66 static void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
68 struct wps_pbc_session *prev;
78 struct wps_registrar {
79 struct wps_context *wps;
82 int selected_registrar;
84 int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
86 int (*set_ie_cb)(void *ctx, const u8 *beacon_ie, size_t beacon_ie_len,
87 const u8 *probe_resp_ie, size_t probe_resp_ie_len);
88 void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
89 const struct wps_device_data *dev);
90 void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
94 struct wps_uuid_pin *pins;
95 struct wps_pbc_session *pbc_sessions;
98 struct wpabuf *extra_cred;
99 int disable_auto_conf;
100 int sel_reg_dev_password_id_override;
101 int sel_reg_config_methods_override;
105 static int wps_set_ie(struct wps_registrar *reg);
106 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
107 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
111 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
112 const u8 *addr, const u8 *uuid_e)
114 struct wps_pbc_session *pbc, *prev = NULL;
119 pbc = reg->pbc_sessions;
121 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
122 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
124 prev->next = pbc->next;
126 reg->pbc_sessions = pbc->next;
134 pbc = os_zalloc(sizeof(*pbc));
137 os_memcpy(pbc->addr, addr, ETH_ALEN);
139 os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
142 pbc->next = reg->pbc_sessions;
143 reg->pbc_sessions = pbc;
144 pbc->timestamp = now;
146 /* remove entries that have timed out */
151 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
153 wps_free_pbc_sessions(pbc);
162 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
163 const u8 *addr, const u8 *uuid_e)
165 struct wps_pbc_session *pbc, *prev = NULL;
167 pbc = reg->pbc_sessions;
169 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
170 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
172 prev->next = pbc->next;
174 reg->pbc_sessions = pbc->next;
184 static int wps_registrar_pbc_overlap(struct wps_registrar *reg,
185 const u8 *addr, const u8 *uuid_e)
188 struct wps_pbc_session *pbc;
193 for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
194 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME)
196 if (addr == NULL || os_memcmp(addr, pbc->addr, ETH_ALEN) ||
198 os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN))
205 return count > 1 ? 1 : 0;
209 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
211 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
213 wpabuf_put_be16(msg, ATTR_WPS_STATE);
214 wpabuf_put_be16(msg, 1);
215 wpabuf_put_u8(msg, wps->wps_state);
220 #ifdef CONFIG_WPS_UPNP
221 static void wps_registrar_free_pending_m2(struct wps_context *wps)
223 struct upnp_pending_message *p, *p2, *prev = NULL;
226 if (p->type == WPS_M2 || p->type == WPS_M2D) {
228 wps->upnp_msgs = p->next;
230 prev->next = p->next;
231 wpa_printf(MSG_DEBUG, "WPS UPnP: Drop pending M2/M2D");
234 wpabuf_free(p2->msg);
242 #endif /* CONFIG_WPS_UPNP */
245 static int wps_build_ap_setup_locked(struct wps_context *wps,
248 if (wps->ap_setup_locked) {
249 wpa_printf(MSG_DEBUG, "WPS: * AP Setup Locked");
250 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
251 wpabuf_put_be16(msg, 1);
252 wpabuf_put_u8(msg, 1);
258 static int wps_build_selected_registrar(struct wps_registrar *reg,
261 if (!reg->selected_registrar)
263 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar");
264 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
265 wpabuf_put_be16(msg, 1);
266 wpabuf_put_u8(msg, 1);
271 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
274 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
275 if (!reg->selected_registrar)
277 if (reg->sel_reg_dev_password_id_override >= 0)
278 id = reg->sel_reg_dev_password_id_override;
279 wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
280 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
281 wpabuf_put_be16(msg, 2);
282 wpabuf_put_be16(msg, id);
287 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
291 if (!reg->selected_registrar)
293 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
295 methods |= WPS_CONFIG_PUSHBUTTON;
296 if (reg->sel_reg_config_methods_override >= 0)
297 methods = reg->sel_reg_config_methods_override;
298 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
300 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
301 wpabuf_put_be16(msg, 2);
302 wpabuf_put_be16(msg, methods);
307 static int wps_build_probe_config_methods(struct wps_registrar *reg,
312 wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
313 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
314 wpabuf_put_be16(msg, 2);
315 wpabuf_put_be16(msg, methods);
320 static int wps_build_config_methods_r(struct wps_registrar *reg,
324 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
326 methods |= WPS_CONFIG_PUSHBUTTON;
327 return wps_build_config_methods(msg, methods);
331 static int wps_build_resp_type(struct wps_registrar *reg, struct wpabuf *msg)
333 u8 resp = reg->wps->ap ? WPS_RESP_AP : WPS_RESP_REGISTRAR;
334 wpa_printf(MSG_DEBUG, "WPS: * Response Type (%d)", resp);
335 wpabuf_put_be16(msg, ATTR_RESPONSE_TYPE);
336 wpabuf_put_be16(msg, 1);
337 wpabuf_put_u8(msg, resp);
343 * wps_registrar_init - Initialize WPS Registrar data
344 * @wps: Pointer to longterm WPS context
345 * @cfg: Registrar configuration
346 * Returns: Pointer to allocated Registrar data or %NULL on failure
348 * This function is used to initialize WPS Registrar functionality. It can be
349 * used for a single Registrar run (e.g., when run in a supplicant) or multiple
350 * runs (e.g., when run as an internal Registrar in an AP). Caller is
351 * responsible for freeing the returned data with wps_registrar_deinit() when
352 * Registrar functionality is not needed anymore.
354 struct wps_registrar *
355 wps_registrar_init(struct wps_context *wps,
356 const struct wps_registrar_config *cfg)
358 struct wps_registrar *reg = os_zalloc(sizeof(*reg));
363 reg->new_psk_cb = cfg->new_psk_cb;
364 reg->set_ie_cb = cfg->set_ie_cb;
365 reg->pin_needed_cb = cfg->pin_needed_cb;
366 reg->reg_success_cb = cfg->reg_success_cb;
367 reg->cb_ctx = cfg->cb_ctx;
368 reg->skip_cred_build = cfg->skip_cred_build;
369 if (cfg->extra_cred) {
370 reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
371 cfg->extra_cred_len);
372 if (reg->extra_cred == NULL) {
377 reg->disable_auto_conf = cfg->disable_auto_conf;
378 reg->sel_reg_dev_password_id_override = -1;
379 reg->sel_reg_config_methods_override = -1;
381 if (wps_set_ie(reg)) {
382 wps_registrar_deinit(reg);
391 * wps_registrar_deinit - Deinitialize WPS Registrar data
392 * @reg: Registrar data from wps_registrar_init()
394 void wps_registrar_deinit(struct wps_registrar *reg)
398 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
399 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
400 wps_free_pins(reg->pins);
401 wps_free_pbc_sessions(reg->pbc_sessions);
402 wpabuf_free(reg->extra_cred);
408 * wps_registrar_add_pin - Configure a new PIN for Registrar
409 * @reg: Registrar data from wps_registrar_init()
410 * @uuid: UUID-E or %NULL for wildcard (any UUID)
411 * @pin: PIN (Device Password)
412 * @pin_len: Length of pin in octets
413 * Returns: 0 on success, -1 on failure
415 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
416 const u8 *pin, size_t pin_len)
418 struct wps_uuid_pin *p;
420 p = os_zalloc(sizeof(*p));
424 p->wildcard_uuid = 1;
426 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
427 p->pin = os_malloc(pin_len);
428 if (p->pin == NULL) {
432 os_memcpy(p->pin, pin, pin_len);
433 p->pin_len = pin_len;
438 wpa_printf(MSG_DEBUG, "WPS: A new PIN configured");
439 wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
440 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
441 reg->selected_registrar = 1;
450 * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
451 * @reg: Registrar data from wps_registrar_init()
453 * Returns: 0 on success, -1 on failure (e.g., PIN not found)
455 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
457 struct wps_uuid_pin *pin, *prev;
462 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
464 reg->pins = pin->next;
466 prev->next = pin->next;
467 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
468 pin->uuid, WPS_UUID_LEN);
480 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
481 const u8 *uuid, size_t *pin_len)
483 struct wps_uuid_pin *pin;
487 if (!pin->wildcard_uuid &&
488 os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0)
494 /* Check for wildcard UUIDs since none of the UUID-specific
498 if (pin->wildcard_uuid == 1) {
499 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
500 "PIN. Assigned it for this UUID-E");
501 pin->wildcard_uuid = 2;
502 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
513 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
514 * that could otherwise avoid PIN invalidations.
517 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
518 "allow concurrent re-use");
521 *pin_len = pin->pin_len;
528 * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
529 * @reg: Registrar data from wps_registrar_init()
531 * Returns: 0 on success, -1 on failure
533 * PINs are locked to enforce only one concurrent use. This function unlocks a
534 * PIN to allow it to be used again. If the specified PIN was configured using
535 * a wildcard UUID, it will be removed instead of allowing multiple uses.
537 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
539 struct wps_uuid_pin *pin;
543 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
544 if (pin->wildcard_uuid == 2) {
545 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
547 return wps_registrar_invalidate_pin(reg, uuid);
559 static void wps_registrar_stop_pbc(struct wps_registrar *reg)
561 reg->selected_registrar = 0;
567 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
569 struct wps_registrar *reg = eloop_ctx;
571 wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
572 wps_registrar_stop_pbc(reg);
577 * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
578 * @reg: Registrar data from wps_registrar_init()
579 * Returns: 0 on success, -1 on failure
581 * This function is called on an AP when a push button is pushed to activate
582 * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
583 * or when a PBC registration is completed.
585 int wps_registrar_button_pushed(struct wps_registrar *reg)
587 if (wps_registrar_pbc_overlap(reg, NULL, NULL)) {
588 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
592 wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
593 reg->selected_registrar = 1;
597 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
598 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
604 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
606 wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
607 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
608 wps_registrar_stop_pbc(reg);
613 * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
614 * @reg: Registrar data from wps_registrar_init()
615 * @addr: MAC address of the Probe Request sender
616 * @wps_data: WPS IE contents
618 * This function is called on an AP when a Probe Request with WPS IE is
619 * received. This is used to track PBC mode use and to detect possible overlap
620 * situation with other WPS APs.
622 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
623 const struct wpabuf *wps_data)
625 struct wps_parse_attr attr;
628 wpa_hexdump_buf(MSG_MSGDUMP,
629 "WPS: Probe Request with WPS data received",
632 if (wps_parse_msg(wps_data, &attr) < 0)
634 if (!wps_version_supported(attr.version)) {
635 wpa_printf(MSG_DEBUG, "WPS: Unsupported ProbeReq WPS IE "
636 "version 0x%x", attr.version ? *attr.version : 0);
640 if (attr.config_methods == NULL) {
641 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
646 methods = WPA_GET_BE16(attr.config_methods);
647 if (!(methods & WPS_CONFIG_PUSHBUTTON))
648 return; /* Not PBC */
650 wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
651 MACSTR, MAC2STR(addr));
653 wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
657 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
658 const u8 *psk, size_t psk_len)
660 if (reg->new_psk_cb == NULL)
663 return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
667 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
668 const struct wps_device_data *dev)
670 if (reg->pin_needed_cb == NULL)
673 reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
677 static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
680 if (reg->reg_success_cb == NULL)
683 reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e);
687 static int wps_cb_set_ie(struct wps_registrar *reg,
688 const struct wpabuf *beacon_ie,
689 const struct wpabuf *probe_resp_ie)
691 if (reg->set_ie_cb == NULL)
694 return reg->set_ie_cb(reg->cb_ctx, wpabuf_head(beacon_ie),
695 wpabuf_len(beacon_ie),
696 wpabuf_head(probe_resp_ie),
697 wpabuf_len(probe_resp_ie));
701 /* Encapsulate WPS IE data with one (or more, if needed) IE headers */
702 static struct wpabuf * wps_ie_encapsulate(struct wpabuf *data)
707 ie = wpabuf_alloc(wpabuf_len(data) + 100);
713 pos = wpabuf_head(data);
714 end = pos + wpabuf_len(data);
717 size_t frag_len = end - pos;
720 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
721 wpabuf_put_u8(ie, 4 + frag_len);
722 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
723 wpabuf_put_data(ie, pos, frag_len);
733 static int wps_set_ie(struct wps_registrar *reg)
735 struct wpabuf *beacon;
736 struct wpabuf *probe;
739 wpa_printf(MSG_DEBUG, "WPS: Build Beacon and Probe Response IEs");
741 beacon = wpabuf_alloc(300);
744 probe = wpabuf_alloc(400);
750 if (wps_build_version(beacon) ||
751 wps_build_wps_state(reg->wps, beacon) ||
752 wps_build_ap_setup_locked(reg->wps, beacon) ||
753 wps_build_selected_registrar(reg, beacon) ||
754 wps_build_sel_reg_dev_password_id(reg, beacon) ||
755 wps_build_sel_reg_config_methods(reg, beacon) ||
756 wps_build_version(probe) ||
757 wps_build_wps_state(reg->wps, probe) ||
758 wps_build_ap_setup_locked(reg->wps, probe) ||
759 wps_build_selected_registrar(reg, probe) ||
760 wps_build_sel_reg_dev_password_id(reg, probe) ||
761 wps_build_sel_reg_config_methods(reg, probe) ||
762 wps_build_resp_type(reg, probe) ||
763 wps_build_uuid_e(probe, reg->wps->uuid) ||
764 wps_build_device_attrs(®->wps->dev, probe) ||
765 wps_build_probe_config_methods(reg, probe) ||
766 wps_build_rf_bands(®->wps->dev, probe)) {
772 beacon = wps_ie_encapsulate(beacon);
773 probe = wps_ie_encapsulate(probe);
775 if (!beacon || !probe) {
781 ret = wps_cb_set_ie(reg, beacon, probe);
789 static int wps_get_dev_password(struct wps_data *wps)
794 os_free(wps->dev_password);
795 wps->dev_password = NULL;
798 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
799 pin = (const u8 *) "00000000";
802 pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
806 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
808 wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
813 wps->dev_password = os_malloc(pin_len);
814 if (wps->dev_password == NULL)
816 os_memcpy(wps->dev_password, pin, pin_len);
817 wps->dev_password_len = pin_len;
823 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
825 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
826 wpabuf_put_be16(msg, ATTR_UUID_R);
827 wpabuf_put_be16(msg, WPS_UUID_LEN);
828 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
833 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
839 if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
841 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
842 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
843 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
845 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
846 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
847 "R-Hash derivation");
851 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
852 wpabuf_put_be16(msg, ATTR_R_HASH1);
853 wpabuf_put_be16(msg, SHA256_MAC_LEN);
854 hash = wpabuf_put(msg, SHA256_MAC_LEN);
855 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
856 addr[0] = wps->snonce;
857 len[0] = WPS_SECRET_NONCE_LEN;
859 len[1] = WPS_PSK_LEN;
860 addr[2] = wpabuf_head(wps->dh_pubkey_e);
861 len[2] = wpabuf_len(wps->dh_pubkey_e);
862 addr[3] = wpabuf_head(wps->dh_pubkey_r);
863 len[3] = wpabuf_len(wps->dh_pubkey_r);
864 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
865 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
867 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
868 wpabuf_put_be16(msg, ATTR_R_HASH2);
869 wpabuf_put_be16(msg, SHA256_MAC_LEN);
870 hash = wpabuf_put(msg, SHA256_MAC_LEN);
871 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
872 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
874 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
875 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
881 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
883 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
884 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
885 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
886 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
891 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
893 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
894 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
895 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
896 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
897 WPS_SECRET_NONCE_LEN);
902 static int wps_build_cred_network_idx(struct wpabuf *msg,
903 struct wps_credential *cred)
905 wpa_printf(MSG_DEBUG, "WPS: * Network Index");
906 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
907 wpabuf_put_be16(msg, 1);
908 wpabuf_put_u8(msg, 1);
913 static int wps_build_cred_ssid(struct wpabuf *msg,
914 struct wps_credential *cred)
916 wpa_printf(MSG_DEBUG, "WPS: * SSID");
917 wpabuf_put_be16(msg, ATTR_SSID);
918 wpabuf_put_be16(msg, cred->ssid_len);
919 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
924 static int wps_build_cred_auth_type(struct wpabuf *msg,
925 struct wps_credential *cred)
927 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
929 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
930 wpabuf_put_be16(msg, 2);
931 wpabuf_put_be16(msg, cred->auth_type);
936 static int wps_build_cred_encr_type(struct wpabuf *msg,
937 struct wps_credential *cred)
939 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
941 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
942 wpabuf_put_be16(msg, 2);
943 wpabuf_put_be16(msg, cred->encr_type);
948 static int wps_build_cred_network_key(struct wpabuf *msg,
949 struct wps_credential *cred)
951 wpa_printf(MSG_DEBUG, "WPS: * Network Key");
952 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
953 wpabuf_put_be16(msg, cred->key_len);
954 wpabuf_put_data(msg, cred->key, cred->key_len);
959 static int wps_build_cred_mac_addr(struct wpabuf *msg,
960 struct wps_credential *cred)
962 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (" MACSTR ")",
963 MAC2STR(cred->mac_addr));
964 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
965 wpabuf_put_be16(msg, ETH_ALEN);
966 wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
971 static int wps_build_credential(struct wpabuf *msg,
972 struct wps_credential *cred)
974 if (wps_build_cred_network_idx(msg, cred) ||
975 wps_build_cred_ssid(msg, cred) ||
976 wps_build_cred_auth_type(msg, cred) ||
977 wps_build_cred_encr_type(msg, cred) ||
978 wps_build_cred_network_key(msg, cred) ||
979 wps_build_cred_mac_addr(msg, cred))
985 int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
989 if (wps->wps->registrar->skip_cred_build)
990 goto skip_cred_build;
992 wpa_printf(MSG_DEBUG, "WPS: * Credential");
993 os_memset(&wps->cred, 0, sizeof(wps->cred));
995 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
996 wps->cred.ssid_len = wps->wps->ssid_len;
998 /* Select the best authentication and encryption type */
999 if (wps->auth_type & WPS_AUTH_WPA2PSK)
1000 wps->auth_type = WPS_AUTH_WPA2PSK;
1001 else if (wps->auth_type & WPS_AUTH_WPAPSK)
1002 wps->auth_type = WPS_AUTH_WPAPSK;
1003 else if (wps->auth_type & WPS_AUTH_OPEN)
1004 wps->auth_type = WPS_AUTH_OPEN;
1005 else if (wps->auth_type & WPS_AUTH_SHARED)
1006 wps->auth_type = WPS_AUTH_SHARED;
1008 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
1012 wps->cred.auth_type = wps->auth_type;
1014 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
1015 wps->auth_type == WPS_AUTH_WPAPSK) {
1016 if (wps->encr_type & WPS_ENCR_AES)
1017 wps->encr_type = WPS_ENCR_AES;
1018 else if (wps->encr_type & WPS_ENCR_TKIP)
1019 wps->encr_type = WPS_ENCR_TKIP;
1021 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1022 "type for WPA/WPA2");
1026 if (wps->encr_type & WPS_ENCR_WEP)
1027 wps->encr_type = WPS_ENCR_WEP;
1028 else if (wps->encr_type & WPS_ENCR_NONE)
1029 wps->encr_type = WPS_ENCR_NONE;
1031 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1032 "type for non-WPA/WPA2 mode");
1036 wps->cred.encr_type = wps->encr_type;
1037 /* Set MAC address in the Credential to be the AP's address (BSSID) */
1038 os_memcpy(wps->cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN);
1040 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
1041 !wps->wps->registrar->disable_auto_conf) {
1043 /* Generate a random passphrase */
1044 if (os_get_random(r, sizeof(r)) < 0)
1046 os_free(wps->new_psk);
1047 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
1048 if (wps->new_psk == NULL)
1050 wps->new_psk_len--; /* remove newline */
1051 while (wps->new_psk_len &&
1052 wps->new_psk[wps->new_psk_len - 1] == '=')
1054 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
1055 wps->new_psk, wps->new_psk_len);
1056 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
1057 wps->cred.key_len = wps->new_psk_len;
1058 } else if (wps->wps->network_key) {
1059 os_memcpy(wps->cred.key, wps->wps->network_key,
1060 wps->wps->network_key_len);
1061 wps->cred.key_len = wps->wps->network_key_len;
1062 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
1064 /* Generate a random per-device PSK */
1065 os_free(wps->new_psk);
1066 wps->new_psk_len = 32;
1067 wps->new_psk = os_malloc(wps->new_psk_len);
1068 if (wps->new_psk == NULL)
1070 if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
1071 os_free(wps->new_psk);
1072 wps->new_psk = NULL;
1075 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
1076 wps->new_psk, wps->new_psk_len);
1077 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
1079 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
1080 wps->cred.key_len = wps->new_psk_len * 2;
1083 cred = wpabuf_alloc(200);
1087 if (wps_build_credential(cred, &wps->cred)) {
1092 wpabuf_put_be16(msg, ATTR_CRED);
1093 wpabuf_put_be16(msg, wpabuf_len(cred));
1094 wpabuf_put_buf(msg, cred);
1098 if (wps->wps->registrar->extra_cred) {
1099 wpa_printf(MSG_DEBUG, "WPS: * Credential (pre-configured)");
1100 wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
1107 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
1109 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
1111 if (wps_build_credential(msg, &wps->cred))
1118 static struct wpabuf * wps_build_m2(struct wps_data *wps)
1122 if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
1124 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
1125 wps->nonce_r, WPS_NONCE_LEN);
1126 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1128 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1129 msg = wpabuf_alloc(1000);
1133 if (wps_build_version(msg) ||
1134 wps_build_msg_type(msg, WPS_M2) ||
1135 wps_build_enrollee_nonce(wps, msg) ||
1136 wps_build_registrar_nonce(wps, msg) ||
1137 wps_build_uuid_r(wps, msg) ||
1138 wps_build_public_key(wps, msg) ||
1139 wps_derive_keys(wps) ||
1140 wps_build_auth_type_flags(wps, msg) ||
1141 wps_build_encr_type_flags(wps, msg) ||
1142 wps_build_conn_type_flags(wps, msg) ||
1143 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1144 wps_build_device_attrs(&wps->wps->dev, msg) ||
1145 wps_build_rf_bands(&wps->wps->dev, msg) ||
1146 wps_build_assoc_state(wps, msg) ||
1147 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1148 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
1149 wps_build_os_version(&wps->wps->dev, msg) ||
1150 wps_build_authenticator(wps, msg)) {
1155 wps->state = RECV_M3;
1160 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1163 u16 err = WPS_CFG_NO_ERROR;
1165 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1166 msg = wpabuf_alloc(1000);
1170 if (wps->wps->ap && wps->wps->ap_setup_locked)
1171 err = WPS_CFG_SETUP_LOCKED;
1173 if (wps_build_version(msg) ||
1174 wps_build_msg_type(msg, WPS_M2D) ||
1175 wps_build_enrollee_nonce(wps, msg) ||
1176 wps_build_registrar_nonce(wps, msg) ||
1177 wps_build_uuid_r(wps, msg) ||
1178 wps_build_auth_type_flags(wps, msg) ||
1179 wps_build_encr_type_flags(wps, msg) ||
1180 wps_build_conn_type_flags(wps, msg) ||
1181 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1182 wps_build_device_attrs(&wps->wps->dev, msg) ||
1183 wps_build_rf_bands(&wps->wps->dev, msg) ||
1184 wps_build_assoc_state(wps, msg) ||
1185 wps_build_config_error(msg, err) ||
1186 wps_build_os_version(&wps->wps->dev, msg)) {
1191 wps->state = RECV_M2D_ACK;
1196 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1198 struct wpabuf *msg, *plain;
1200 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1202 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1204 plain = wpabuf_alloc(200);
1208 msg = wpabuf_alloc(1000);
1214 if (wps_build_version(msg) ||
1215 wps_build_msg_type(msg, WPS_M4) ||
1216 wps_build_enrollee_nonce(wps, msg) ||
1217 wps_build_r_hash(wps, msg) ||
1218 wps_build_r_snonce1(wps, plain) ||
1219 wps_build_key_wrap_auth(wps, plain) ||
1220 wps_build_encr_settings(wps, msg, plain) ||
1221 wps_build_authenticator(wps, msg)) {
1228 wps->state = RECV_M5;
1233 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1235 struct wpabuf *msg, *plain;
1237 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1239 plain = wpabuf_alloc(200);
1243 msg = wpabuf_alloc(1000);
1249 if (wps_build_version(msg) ||
1250 wps_build_msg_type(msg, WPS_M6) ||
1251 wps_build_enrollee_nonce(wps, msg) ||
1252 wps_build_r_snonce2(wps, plain) ||
1253 wps_build_key_wrap_auth(wps, plain) ||
1254 wps_build_encr_settings(wps, msg, plain) ||
1255 wps_build_authenticator(wps, msg)) {
1262 wps->wps_pin_revealed = 1;
1263 wps->state = RECV_M7;
1268 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1270 struct wpabuf *msg, *plain;
1272 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1274 plain = wpabuf_alloc(500);
1278 msg = wpabuf_alloc(1000);
1284 if (wps_build_version(msg) ||
1285 wps_build_msg_type(msg, WPS_M8) ||
1286 wps_build_enrollee_nonce(wps, msg) ||
1287 (wps->wps->ap && wps_build_cred(wps, plain)) ||
1288 (!wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
1289 wps_build_key_wrap_auth(wps, plain) ||
1290 wps_build_encr_settings(wps, msg, plain) ||
1291 wps_build_authenticator(wps, msg)) {
1298 wps->state = RECV_DONE;
1303 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
1307 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
1309 msg = wpabuf_alloc(1000);
1313 if (wps_build_version(msg) ||
1314 wps_build_msg_type(msg, WPS_WSC_ACK) ||
1315 wps_build_enrollee_nonce(wps, msg) ||
1316 wps_build_registrar_nonce(wps, msg)) {
1325 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
1329 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
1331 msg = wpabuf_alloc(1000);
1335 if (wps_build_version(msg) ||
1336 wps_build_msg_type(msg, WPS_WSC_NACK) ||
1337 wps_build_enrollee_nonce(wps, msg) ||
1338 wps_build_registrar_nonce(wps, msg) ||
1339 wps_build_config_error(msg, wps->config_error)) {
1348 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1349 enum wsc_op_code *op_code)
1353 #ifdef CONFIG_WPS_UPNP
1354 if (wps->wps->wps_upnp) {
1355 struct upnp_pending_message *p, *prev = NULL;
1356 if (wps->ext_reg > 1)
1357 wps_registrar_free_pending_m2(wps->wps);
1358 p = wps->wps->upnp_msgs;
1359 /* TODO: check pending message MAC address */
1360 while (p && p->next) {
1365 wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
1370 wps->wps->upnp_msgs = NULL;
1374 if (wps->ext_reg == 0)
1380 wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
1381 "pending message available");
1384 #endif /* CONFIG_WPS_UPNP */
1386 switch (wps->state) {
1388 if (wps_get_dev_password(wps) < 0)
1389 msg = wps_build_m2d(wps);
1391 msg = wps_build_m2(wps);
1395 msg = wps_build_m2d(wps);
1399 msg = wps_build_m4(wps);
1403 msg = wps_build_m6(wps);
1407 msg = wps_build_m8(wps);
1411 msg = wps_build_wsc_ack(wps);
1415 msg = wps_build_wsc_nack(wps);
1416 *op_code = WSC_NACK;
1419 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
1420 "a message", wps->state);
1425 if (*op_code == WSC_MSG && msg) {
1426 /* Save a copy of the last message for Authenticator derivation
1428 wpabuf_free(wps->last_msg);
1429 wps->last_msg = wpabuf_dup(msg);
1436 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
1438 if (e_nonce == NULL) {
1439 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
1443 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
1444 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
1445 wps->nonce_e, WPS_NONCE_LEN);
1451 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
1453 if (r_nonce == NULL) {
1454 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
1458 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
1459 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
1467 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
1469 if (uuid_e == NULL) {
1470 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
1474 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
1475 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
1481 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
1483 if (pw_id == NULL) {
1484 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
1488 wps->dev_pw_id = WPA_GET_BE16(pw_id);
1489 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
1495 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
1497 if (e_hash1 == NULL) {
1498 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
1502 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
1503 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
1509 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
1511 if (e_hash2 == NULL) {
1512 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
1516 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
1517 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
1523 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
1525 u8 hash[SHA256_MAC_LEN];
1529 if (e_snonce1 == NULL) {
1530 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
1534 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
1535 WPS_SECRET_NONCE_LEN);
1537 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
1538 addr[0] = e_snonce1;
1539 len[0] = WPS_SECRET_NONCE_LEN;
1540 addr[1] = wps->psk1;
1541 len[1] = WPS_PSK_LEN;
1542 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1543 len[2] = wpabuf_len(wps->dh_pubkey_e);
1544 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1545 len[3] = wpabuf_len(wps->dh_pubkey_r);
1546 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1548 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
1549 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
1550 "not match with the pre-committed value");
1551 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1552 wps_pwd_auth_fail_event(wps->wps, 0, 1);
1556 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
1557 "half of the device password");
1563 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
1565 u8 hash[SHA256_MAC_LEN];
1569 if (e_snonce2 == NULL) {
1570 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
1574 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
1575 WPS_SECRET_NONCE_LEN);
1577 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
1578 addr[0] = e_snonce2;
1579 len[0] = WPS_SECRET_NONCE_LEN;
1580 addr[1] = wps->psk2;
1581 len[1] = WPS_PSK_LEN;
1582 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1583 len[2] = wpabuf_len(wps->dh_pubkey_e);
1584 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1585 len[3] = wpabuf_len(wps->dh_pubkey_r);
1586 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1588 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
1589 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
1590 "not match with the pre-committed value");
1591 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
1592 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1593 wps_pwd_auth_fail_event(wps->wps, 0, 2);
1597 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
1598 "half of the device password");
1599 wps->wps_pin_revealed = 0;
1600 wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
1606 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
1608 if (mac_addr == NULL) {
1609 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
1613 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
1615 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
1616 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
1622 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
1625 if (pk == NULL || pk_len == 0) {
1626 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
1630 if (wps->wps->oob_conf.pubkey_hash != NULL) {
1632 u8 hash[WPS_HASH_LEN];
1635 sha256_vector(1, addr, &pk_len, hash);
1637 wpabuf_head(wps->wps->oob_conf.pubkey_hash),
1638 WPS_OOB_PUBKEY_HASH_LEN) != 0) {
1639 wpa_printf(MSG_ERROR, "WPS: Public Key hash error");
1644 wpabuf_free(wps->dh_pubkey_e);
1645 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
1646 if (wps->dh_pubkey_e == NULL)
1653 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
1658 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
1663 auth_types = WPA_GET_BE16(auth);
1665 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
1667 wps->auth_type = wps->wps->auth_types & auth_types;
1668 if (wps->auth_type == 0) {
1669 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1670 "authentication types (own 0x%x Enrollee 0x%x)",
1671 wps->wps->auth_types, auth_types);
1679 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
1684 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
1689 encr_types = WPA_GET_BE16(encr);
1691 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
1693 wps->encr_type = wps->wps->encr_types & encr_types;
1694 if (wps->encr_type == 0) {
1695 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1696 "encryption types");
1704 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
1707 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
1712 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
1719 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
1723 if (methods == NULL) {
1724 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
1728 m = WPA_GET_BE16(methods);
1730 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x", m);
1736 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
1738 if (state == NULL) {
1739 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
1744 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
1751 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
1755 if (assoc == NULL) {
1756 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
1760 a = WPA_GET_BE16(assoc);
1761 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
1767 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
1772 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
1776 e = WPA_GET_BE16(err);
1777 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
1783 static enum wps_process_res wps_process_m1(struct wps_data *wps,
1784 struct wps_parse_attr *attr)
1786 wpa_printf(MSG_DEBUG, "WPS: Received M1");
1788 if (wps->state != RECV_M1) {
1789 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1790 "receiving M1", wps->state);
1794 if (wps_process_uuid_e(wps, attr->uuid_e) ||
1795 wps_process_mac_addr(wps, attr->mac_addr) ||
1796 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1797 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
1798 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
1799 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
1800 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
1801 wps_process_config_methods(wps, attr->config_methods) ||
1802 wps_process_wps_state(wps, attr->wps_state) ||
1803 wps_process_device_attrs(&wps->peer_dev, attr) ||
1804 wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
1805 wps_process_assoc_state(wps, attr->assoc_state) ||
1806 wps_process_dev_password_id(wps, attr->dev_password_id) ||
1807 wps_process_config_error(wps, attr->config_error) ||
1808 wps_process_os_version(&wps->peer_dev, attr->os_version))
1811 if (wps->dev_pw_id < 0x10 &&
1812 wps->dev_pw_id != DEV_PW_DEFAULT &&
1813 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
1814 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
1815 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
1816 (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
1817 !wps->wps->registrar->pbc)) {
1818 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
1820 wps->state = SEND_M2D;
1821 return WPS_CONTINUE;
1824 if (wps->dev_pw_id >= 0x10 &&
1825 wps->dev_pw_id != wps->wps->oob_dev_pw_id) {
1826 wpa_printf(MSG_DEBUG, "WPS: OOB Device Password ID "
1827 "%d mismatch", wps->dev_pw_id);
1828 wps->state = SEND_M2D;
1829 return WPS_CONTINUE;
1832 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
1833 if (wps_registrar_pbc_overlap(wps->wps->registrar,
1834 wps->mac_addr_e, wps->uuid_e)) {
1835 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
1837 wps->state = SEND_M2D;
1838 return WPS_CONTINUE;
1840 wps_registrar_add_pbc_session(wps->wps->registrar,
1841 wps->mac_addr_e, wps->uuid_e);
1845 wps->state = SEND_M2;
1846 return WPS_CONTINUE;
1850 static enum wps_process_res wps_process_m3(struct wps_data *wps,
1851 const struct wpabuf *msg,
1852 struct wps_parse_attr *attr)
1854 wpa_printf(MSG_DEBUG, "WPS: Received M3");
1856 if (wps->state != RECV_M3) {
1857 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1858 "receiving M3", wps->state);
1859 wps->state = SEND_WSC_NACK;
1860 return WPS_CONTINUE;
1863 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1864 wps_process_authenticator(wps, attr->authenticator, msg) ||
1865 wps_process_e_hash1(wps, attr->e_hash1) ||
1866 wps_process_e_hash2(wps, attr->e_hash2)) {
1867 wps->state = SEND_WSC_NACK;
1868 return WPS_CONTINUE;
1871 wps->state = SEND_M4;
1872 return WPS_CONTINUE;
1876 static enum wps_process_res wps_process_m5(struct wps_data *wps,
1877 const struct wpabuf *msg,
1878 struct wps_parse_attr *attr)
1880 struct wpabuf *decrypted;
1881 struct wps_parse_attr eattr;
1883 wpa_printf(MSG_DEBUG, "WPS: Received M5");
1885 if (wps->state != RECV_M5) {
1886 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1887 "receiving M5", wps->state);
1888 wps->state = SEND_WSC_NACK;
1889 return WPS_CONTINUE;
1892 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1893 wps_process_authenticator(wps, attr->authenticator, msg)) {
1894 wps->state = SEND_WSC_NACK;
1895 return WPS_CONTINUE;
1898 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1899 attr->encr_settings_len);
1900 if (decrypted == NULL) {
1901 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1902 "Settings attribute");
1903 wps->state = SEND_WSC_NACK;
1904 return WPS_CONTINUE;
1907 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1909 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1910 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1911 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
1912 wpabuf_free(decrypted);
1913 wps->state = SEND_WSC_NACK;
1914 return WPS_CONTINUE;
1916 wpabuf_free(decrypted);
1918 wps->state = SEND_M6;
1919 return WPS_CONTINUE;
1923 static int wps_process_ap_settings_r(struct wps_data *wps,
1924 struct wps_parse_attr *attr)
1929 /* AP Settings Attributes in M7 when Enrollee is an AP */
1930 if (wps_process_ap_settings(attr, &wps->cred) < 0)
1933 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
1936 * TODO: Provide access to AP settings and allow changes before sending
1937 * out M8. For now, just copy the settings unchanged into M8.
1944 static enum wps_process_res wps_process_m7(struct wps_data *wps,
1945 const struct wpabuf *msg,
1946 struct wps_parse_attr *attr)
1948 struct wpabuf *decrypted;
1949 struct wps_parse_attr eattr;
1951 wpa_printf(MSG_DEBUG, "WPS: Received M7");
1953 if (wps->state != RECV_M7) {
1954 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1955 "receiving M7", wps->state);
1956 wps->state = SEND_WSC_NACK;
1957 return WPS_CONTINUE;
1960 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1961 wps_process_authenticator(wps, attr->authenticator, msg)) {
1962 wps->state = SEND_WSC_NACK;
1963 return WPS_CONTINUE;
1966 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1967 attr->encr_settings_len);
1968 if (decrypted == NULL) {
1969 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1970 "Settings attribute");
1971 wps->state = SEND_WSC_NACK;
1972 return WPS_CONTINUE;
1975 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1977 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1978 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1979 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
1980 wps_process_ap_settings_r(wps, &eattr)) {
1981 wpabuf_free(decrypted);
1982 wps->state = SEND_WSC_NACK;
1983 return WPS_CONTINUE;
1986 wpabuf_free(decrypted);
1988 wps->state = SEND_M8;
1989 return WPS_CONTINUE;
1993 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
1994 const struct wpabuf *msg)
1996 struct wps_parse_attr attr;
1997 enum wps_process_res ret = WPS_CONTINUE;
1999 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
2001 if (wps_parse_msg(msg, &attr) < 0)
2004 if (!wps_version_supported(attr.version)) {
2005 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2006 attr.version ? *attr.version : 0);
2010 if (attr.msg_type == NULL) {
2011 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2015 if (*attr.msg_type != WPS_M1 &&
2016 (attr.registrar_nonce == NULL ||
2017 os_memcmp(wps->nonce_r, attr.registrar_nonce,
2018 WPS_NONCE_LEN != 0))) {
2019 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2023 switch (*attr.msg_type) {
2025 #ifdef CONFIG_WPS_UPNP
2026 if (wps->wps->wps_upnp && attr.mac_addr) {
2027 /* Remove old pending messages when starting new run */
2028 wps_free_pending_msgs(wps->wps->upnp_msgs);
2029 wps->wps->upnp_msgs = NULL;
2031 upnp_wps_device_send_wlan_event(
2032 wps->wps->wps_upnp, attr.mac_addr,
2033 UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
2035 #endif /* CONFIG_WPS_UPNP */
2036 ret = wps_process_m1(wps, &attr);
2039 ret = wps_process_m3(wps, msg, &attr);
2040 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2041 wps_fail_event(wps->wps, WPS_M3);
2044 ret = wps_process_m5(wps, msg, &attr);
2045 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2046 wps_fail_event(wps->wps, WPS_M5);
2049 ret = wps_process_m7(wps, msg, &attr);
2050 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2051 wps_fail_event(wps->wps, WPS_M7);
2054 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
2059 if (ret == WPS_CONTINUE) {
2060 /* Save a copy of the last message for Authenticator derivation
2062 wpabuf_free(wps->last_msg);
2063 wps->last_msg = wpabuf_dup(msg);
2070 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
2071 const struct wpabuf *msg)
2073 struct wps_parse_attr attr;
2075 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
2077 if (wps_parse_msg(msg, &attr) < 0)
2080 if (!wps_version_supported(attr.version)) {
2081 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2082 attr.version ? *attr.version : 0);
2086 if (attr.msg_type == NULL) {
2087 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2091 if (*attr.msg_type != WPS_WSC_ACK) {
2092 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2097 #ifdef CONFIG_WPS_UPNP
2098 if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
2099 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2100 if (wps->wps->upnp_msgs)
2101 return WPS_CONTINUE;
2102 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2103 "external Registrar");
2106 #endif /* CONFIG_WPS_UPNP */
2108 if (attr.registrar_nonce == NULL ||
2109 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2111 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2115 if (attr.enrollee_nonce == NULL ||
2116 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2117 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2121 if (wps->state == RECV_M2D_ACK) {
2122 #ifdef CONFIG_WPS_UPNP
2123 if (wps->wps->wps_upnp &&
2124 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2125 if (wps->wps->upnp_msgs)
2126 return WPS_CONTINUE;
2127 if (wps->ext_reg == 0)
2129 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2130 "external Registrar");
2133 #endif /* CONFIG_WPS_UPNP */
2135 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
2136 "terminate negotiation");
2143 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
2144 const struct wpabuf *msg)
2146 struct wps_parse_attr attr;
2149 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
2151 old_state = wps->state;
2152 wps->state = SEND_WSC_NACK;
2154 if (wps_parse_msg(msg, &attr) < 0)
2157 if (!wps_version_supported(attr.version)) {
2158 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2159 attr.version ? *attr.version : 0);
2163 if (attr.msg_type == NULL) {
2164 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2168 if (*attr.msg_type != WPS_WSC_NACK) {
2169 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2174 #ifdef CONFIG_WPS_UPNP
2175 if (wps->wps->wps_upnp && wps->ext_reg) {
2176 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2177 "Registrar terminated by the Enrollee");
2180 #endif /* CONFIG_WPS_UPNP */
2182 if (attr.registrar_nonce == NULL ||
2183 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2185 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2189 if (attr.enrollee_nonce == NULL ||
2190 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2191 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2195 if (attr.config_error == NULL) {
2196 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
2201 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
2202 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
2204 switch (old_state) {
2206 wps_fail_event(wps->wps, WPS_M2);
2209 wps_fail_event(wps->wps, WPS_M4);
2212 wps_fail_event(wps->wps, WPS_M6);
2215 wps_fail_event(wps->wps, WPS_M8);
2225 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
2226 const struct wpabuf *msg)
2228 struct wps_parse_attr attr;
2230 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
2232 if (wps->state != RECV_DONE &&
2233 (!wps->wps->wps_upnp || !wps->ext_reg)) {
2234 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2235 "receiving WSC_Done", wps->state);
2239 if (wps_parse_msg(msg, &attr) < 0)
2242 if (!wps_version_supported(attr.version)) {
2243 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2244 attr.version ? *attr.version : 0);
2248 if (attr.msg_type == NULL) {
2249 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2253 if (*attr.msg_type != WPS_WSC_DONE) {
2254 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2259 #ifdef CONFIG_WPS_UPNP
2260 if (wps->wps->wps_upnp && wps->ext_reg) {
2261 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2262 "Registrar completed successfully");
2265 #endif /* CONFIG_WPS_UPNP */
2267 if (attr.registrar_nonce == NULL ||
2268 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2270 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2274 if (attr.enrollee_nonce == NULL ||
2275 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2276 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2280 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
2282 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
2283 wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
2284 struct wps_credential cred;
2286 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
2287 "on first Enrollee connection");
2289 os_memset(&cred, 0, sizeof(cred));
2290 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
2291 cred.ssid_len = wps->wps->ssid_len;
2292 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
2293 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
2294 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
2295 cred.key_len = wps->new_psk_len;
2297 wps->wps->wps_state = WPS_STATE_CONFIGURED;
2298 wpa_hexdump_ascii_key(MSG_DEBUG,
2299 "WPS: Generated random passphrase",
2300 wps->new_psk, wps->new_psk_len);
2301 if (wps->wps->cred_cb)
2302 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
2304 os_free(wps->new_psk);
2305 wps->new_psk = NULL;
2308 if (!wps->wps->ap) {
2309 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based "
2310 "on the modified AP configuration");
2311 if (wps->wps->cred_cb)
2312 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2316 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
2317 wps->new_psk, wps->new_psk_len)) {
2318 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
2321 os_free(wps->new_psk);
2322 wps->new_psk = NULL;
2325 wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e);
2328 wps_registrar_remove_pbc_session(wps->wps->registrar,
2329 wps->mac_addr_e, wps->uuid_e);
2330 wps_registrar_pbc_completed(wps->wps->registrar);
2333 wps_success_event(wps->wps);
2339 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
2340 enum wsc_op_code op_code,
2341 const struct wpabuf *msg)
2343 enum wps_process_res ret;
2345 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
2347 (unsigned long) wpabuf_len(msg), op_code);
2349 #ifdef CONFIG_WPS_UPNP
2350 if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
2351 struct wps_parse_attr attr;
2352 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
2353 *attr.msg_type == WPS_M3)
2354 wps->ext_reg = 2; /* past M2/M2D phase */
2356 if (wps->ext_reg > 1)
2357 wps_registrar_free_pending_m2(wps->wps);
2358 if (wps->wps->wps_upnp && wps->ext_reg &&
2359 wps->wps->upnp_msgs == NULL &&
2360 (op_code == WSC_MSG || op_code == WSC_Done)) {
2361 struct wps_parse_attr attr;
2363 if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
2366 type = *attr.msg_type;
2367 wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
2368 " to external Registrar for processing", type);
2369 upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
2371 UPNP_WPS_WLANEVENT_TYPE_EAP,
2373 if (op_code == WSC_MSG)
2375 } else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
2376 wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
2377 "external Registrar");
2378 return WPS_CONTINUE;
2380 #endif /* CONFIG_WPS_UPNP */
2384 return wps_process_wsc_msg(wps, msg);
2386 return wps_process_wsc_ack(wps, msg);
2388 return wps_process_wsc_nack(wps, msg);
2390 ret = wps_process_wsc_done(wps, msg);
2391 if (ret == WPS_FAILURE) {
2392 wps->state = SEND_WSC_NACK;
2393 wps_fail_event(wps->wps, WPS_WSC_DONE);
2397 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
2403 int wps_registrar_update_ie(struct wps_registrar *reg)
2405 return wps_set_ie(reg);
2409 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
2412 struct wps_registrar *reg = eloop_ctx;
2414 wpa_printf(MSG_DEBUG, "WPS: SetSelectedRegistrar timed out - "
2415 "unselect Registrar");
2416 reg->selected_registrar = 0;
2418 reg->sel_reg_dev_password_id_override = -1;
2419 reg->sel_reg_config_methods_override = -1;
2425 * wps_registrar_set_selected_registrar - Notification of SetSelectedRegistrar
2426 * @reg: Registrar data from wps_registrar_init()
2427 * @msg: Received message from SetSelectedRegistrar
2428 * @msg_len: Length of msg in octets
2429 * Returns: 0 on success, -1 on failure
2431 * This function is called when an AP receives a SetSelectedRegistrar UPnP
2434 int wps_registrar_set_selected_registrar(struct wps_registrar *reg,
2435 const struct wpabuf *msg)
2437 struct wps_parse_attr attr;
2439 wpa_hexdump_buf(MSG_MSGDUMP, "WPS: SetSelectedRegistrar attributes",
2442 if (wps_parse_msg(msg, &attr) < 0)
2444 if (!wps_version_supported(attr.version)) {
2445 wpa_printf(MSG_DEBUG, "WPS: Unsupported SetSelectedRegistrar "
2446 "version 0x%x", attr.version ? *attr.version : 0);
2450 if (attr.selected_registrar == NULL ||
2451 *attr.selected_registrar == 0) {
2452 wpa_printf(MSG_DEBUG, "WPS: SetSelectedRegistrar: Disable "
2453 "Selected Registrar");
2454 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg,
2456 wps_registrar_set_selected_timeout(reg, NULL);
2460 reg->selected_registrar = 1;
2461 reg->sel_reg_dev_password_id_override = attr.dev_password_id ?
2462 WPA_GET_BE16(attr.dev_password_id) : DEV_PW_DEFAULT;
2463 reg->sel_reg_config_methods_override = attr.sel_reg_config_methods ?
2464 WPA_GET_BE16(attr.sel_reg_config_methods) : -1;
2467 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
2468 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
2469 wps_registrar_set_selected_timeout,