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"
28 struct wps_uuid_pin *next;
29 u8 uuid[WPS_UUID_LEN];
37 static void wps_free_pin(struct wps_uuid_pin *pin)
44 static void wps_free_pins(struct wps_uuid_pin *pins)
46 struct wps_uuid_pin *pin, *prev;
57 struct wps_pbc_session {
58 struct wps_pbc_session *next;
60 u8 uuid_e[WPS_UUID_LEN];
61 struct os_time timestamp;
65 static void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
67 struct wps_pbc_session *prev;
77 struct wps_registrar {
78 struct wps_context *wps;
81 int selected_registrar;
83 int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
85 int (*set_ie_cb)(void *ctx, const u8 *beacon_ie, size_t beacon_ie_len,
86 const u8 *probe_resp_ie, size_t probe_resp_ie_len);
87 void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
88 const struct wps_device_data *dev);
89 void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
93 struct wps_uuid_pin *pins;
94 struct wps_pbc_session *pbc_sessions;
97 struct wpabuf *extra_cred;
98 int disable_auto_conf;
99 int sel_reg_dev_password_id_override;
100 int sel_reg_config_methods_override;
104 static int wps_set_ie(struct wps_registrar *reg);
105 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
106 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
110 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
111 const u8 *addr, const u8 *uuid_e)
113 struct wps_pbc_session *pbc, *prev = NULL;
118 pbc = reg->pbc_sessions;
120 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
121 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
123 prev->next = pbc->next;
125 reg->pbc_sessions = pbc->next;
133 pbc = os_zalloc(sizeof(*pbc));
136 os_memcpy(pbc->addr, addr, ETH_ALEN);
138 os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
141 pbc->next = reg->pbc_sessions;
142 reg->pbc_sessions = pbc;
143 pbc->timestamp = now;
145 /* remove entries that have timed out */
150 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
152 wps_free_pbc_sessions(pbc);
161 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
162 const u8 *addr, const u8 *uuid_e)
164 struct wps_pbc_session *pbc, *prev = NULL;
166 pbc = reg->pbc_sessions;
168 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
169 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
171 prev->next = pbc->next;
173 reg->pbc_sessions = pbc->next;
183 static int wps_registrar_pbc_overlap(struct wps_registrar *reg,
184 const u8 *addr, const u8 *uuid_e)
187 struct wps_pbc_session *pbc;
192 for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
193 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME)
195 if (addr == NULL || os_memcmp(addr, pbc->addr, ETH_ALEN) ||
197 os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN))
204 return count > 1 ? 1 : 0;
208 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
210 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
212 wpabuf_put_be16(msg, ATTR_WPS_STATE);
213 wpabuf_put_be16(msg, 1);
214 wpabuf_put_u8(msg, wps->wps_state);
219 #ifdef CONFIG_WPS_UPNP
220 static void wps_registrar_free_pending_m2(struct wps_context *wps)
222 struct upnp_pending_message *p, *p2, *prev = NULL;
225 if (p->type == WPS_M2 || p->type == WPS_M2D) {
227 wps->upnp_msgs = p->next;
229 prev->next = p->next;
230 wpa_printf(MSG_DEBUG, "WPS UPnP: Drop pending M2/M2D");
233 wpabuf_free(p2->msg);
241 #endif /* CONFIG_WPS_UPNP */
244 static int wps_build_ap_setup_locked(struct wps_context *wps,
247 if (wps->ap_setup_locked) {
248 wpa_printf(MSG_DEBUG, "WPS: * AP Setup Locked");
249 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
250 wpabuf_put_be16(msg, 1);
251 wpabuf_put_u8(msg, 1);
257 static int wps_build_selected_registrar(struct wps_registrar *reg,
260 if (!reg->selected_registrar)
262 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar");
263 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
264 wpabuf_put_be16(msg, 1);
265 wpabuf_put_u8(msg, 1);
270 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
273 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
274 if (!reg->selected_registrar)
276 if (reg->sel_reg_dev_password_id_override >= 0)
277 id = reg->sel_reg_dev_password_id_override;
278 wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
279 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
280 wpabuf_put_be16(msg, 2);
281 wpabuf_put_be16(msg, id);
286 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
290 if (!reg->selected_registrar)
292 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
294 methods |= WPS_CONFIG_PUSHBUTTON;
295 if (reg->sel_reg_config_methods_override >= 0)
296 methods = reg->sel_reg_config_methods_override;
297 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
299 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
300 wpabuf_put_be16(msg, 2);
301 wpabuf_put_be16(msg, methods);
306 static int wps_build_probe_config_methods(struct wps_registrar *reg,
311 wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
312 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
313 wpabuf_put_be16(msg, 2);
314 wpabuf_put_be16(msg, methods);
319 static int wps_build_config_methods_r(struct wps_registrar *reg,
323 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
325 methods |= WPS_CONFIG_PUSHBUTTON;
326 return wps_build_config_methods(msg, methods);
330 static int wps_build_resp_type(struct wps_registrar *reg, struct wpabuf *msg)
332 u8 resp = reg->wps->ap ? WPS_RESP_AP : WPS_RESP_REGISTRAR;
333 wpa_printf(MSG_DEBUG, "WPS: * Response Type (%d)", resp);
334 wpabuf_put_be16(msg, ATTR_RESPONSE_TYPE);
335 wpabuf_put_be16(msg, 1);
336 wpabuf_put_u8(msg, resp);
342 * wps_registrar_init - Initialize WPS Registrar data
343 * @wps: Pointer to longterm WPS context
344 * @cfg: Registrar configuration
345 * Returns: Pointer to allocated Registrar data or %NULL on failure
347 * This function is used to initialize WPS Registrar functionality. It can be
348 * used for a single Registrar run (e.g., when run in a supplicant) or multiple
349 * runs (e.g., when run as an internal Registrar in an AP). Caller is
350 * responsible for freeing the returned data with wps_registrar_deinit() when
351 * Registrar functionality is not needed anymore.
353 struct wps_registrar *
354 wps_registrar_init(struct wps_context *wps,
355 const struct wps_registrar_config *cfg)
357 struct wps_registrar *reg = os_zalloc(sizeof(*reg));
362 reg->new_psk_cb = cfg->new_psk_cb;
363 reg->set_ie_cb = cfg->set_ie_cb;
364 reg->pin_needed_cb = cfg->pin_needed_cb;
365 reg->reg_success_cb = cfg->reg_success_cb;
366 reg->cb_ctx = cfg->cb_ctx;
367 reg->skip_cred_build = cfg->skip_cred_build;
368 if (cfg->extra_cred) {
369 reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
370 cfg->extra_cred_len);
371 if (reg->extra_cred == NULL) {
376 reg->disable_auto_conf = cfg->disable_auto_conf;
377 reg->sel_reg_dev_password_id_override = -1;
379 if (wps_set_ie(reg)) {
380 wps_registrar_deinit(reg);
389 * wps_registrar_deinit - Deinitialize WPS Registrar data
390 * @reg: Registrar data from wps_registrar_init()
392 void wps_registrar_deinit(struct wps_registrar *reg)
396 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
397 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
398 wps_free_pins(reg->pins);
399 wps_free_pbc_sessions(reg->pbc_sessions);
400 wpabuf_free(reg->extra_cred);
406 * wps_registrar_add_pin - Configure a new PIN for Registrar
407 * @reg: Registrar data from wps_registrar_init()
408 * @uuid: UUID-E or %NULL for wildcard (any UUID)
409 * @pin: PIN (Device Password)
410 * @pin_len: Length of pin in octets
411 * Returns: 0 on success, -1 on failure
413 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
414 const u8 *pin, size_t pin_len)
416 struct wps_uuid_pin *p;
418 p = os_zalloc(sizeof(*p));
422 p->wildcard_uuid = 1;
424 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
425 p->pin = os_malloc(pin_len);
426 if (p->pin == NULL) {
430 os_memcpy(p->pin, pin, pin_len);
431 p->pin_len = pin_len;
436 wpa_printf(MSG_DEBUG, "WPS: A new PIN configured");
437 wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
438 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
439 reg->selected_registrar = 1;
448 * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
449 * @reg: Registrar data from wps_registrar_init()
451 * Returns: 0 on success, -1 on failure (e.g., PIN not found)
453 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
455 struct wps_uuid_pin *pin, *prev;
460 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
462 reg->pins = pin->next;
464 prev->next = pin->next;
465 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
466 pin->uuid, WPS_UUID_LEN);
478 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
479 const u8 *uuid, size_t *pin_len)
481 struct wps_uuid_pin *pin;
485 if (!pin->wildcard_uuid &&
486 os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0)
492 /* Check for wildcard UUIDs since none of the UUID-specific
496 if (pin->wildcard_uuid == 1) {
497 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
498 "PIN. Assigned it for this UUID-E");
499 pin->wildcard_uuid = 2;
500 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
511 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
512 * that could otherwise avoid PIN invalidations.
515 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
516 "allow concurrent re-use");
519 *pin_len = pin->pin_len;
526 * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
527 * @reg: Registrar data from wps_registrar_init()
529 * Returns: 0 on success, -1 on failure
531 * PINs are locked to enforce only one concurrent use. This function unlocks a
532 * PIN to allow it to be used again. If the specified PIN was configured using
533 * a wildcard UUID, it will be removed instead of allowing multiple uses.
535 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
537 struct wps_uuid_pin *pin;
541 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
542 if (pin->wildcard_uuid == 2) {
543 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
545 return wps_registrar_invalidate_pin(reg, uuid);
557 static void wps_registrar_stop_pbc(struct wps_registrar *reg)
559 reg->selected_registrar = 0;
565 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
567 struct wps_registrar *reg = eloop_ctx;
569 wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
570 wps_registrar_stop_pbc(reg);
575 * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
576 * @reg: Registrar data from wps_registrar_init()
577 * Returns: 0 on success, -1 on failure
579 * This function is called on an AP when a push button is pushed to activate
580 * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
581 * or when a PBC registration is completed.
583 int wps_registrar_button_pushed(struct wps_registrar *reg)
585 if (wps_registrar_pbc_overlap(reg, NULL, NULL)) {
586 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
590 wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
591 reg->selected_registrar = 1;
595 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
596 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
602 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
604 wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
605 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
606 wps_registrar_stop_pbc(reg);
611 * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
612 * @reg: Registrar data from wps_registrar_init()
613 * @addr: MAC address of the Probe Request sender
614 * @wps_data: WPS IE contents
616 * This function is called on an AP when a Probe Request with WPS IE is
617 * received. This is used to track PBC mode use and to detect possible overlap
618 * situation with other WPS APs.
620 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
621 const struct wpabuf *wps_data)
623 struct wps_parse_attr attr;
626 wpa_hexdump_buf(MSG_MSGDUMP,
627 "WPS: Probe Request with WPS data received",
630 if (wps_parse_msg(wps_data, &attr) < 0)
632 if (!wps_version_supported(attr.version)) {
633 wpa_printf(MSG_DEBUG, "WPS: Unsupported ProbeReq WPS IE "
634 "version 0x%x", attr.version ? *attr.version : 0);
638 if (attr.config_methods == NULL) {
639 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
644 methods = WPA_GET_BE16(attr.config_methods);
645 if (!(methods & WPS_CONFIG_PUSHBUTTON))
646 return; /* Not PBC */
648 wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
649 MACSTR, MAC2STR(addr));
651 wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
655 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
656 const u8 *psk, size_t psk_len)
658 if (reg->new_psk_cb == NULL)
661 return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
665 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
666 const struct wps_device_data *dev)
668 if (reg->pin_needed_cb == NULL)
671 reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
675 static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
678 if (reg->reg_success_cb == NULL)
681 reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e);
685 static int wps_cb_set_ie(struct wps_registrar *reg,
686 const struct wpabuf *beacon_ie,
687 const struct wpabuf *probe_resp_ie)
689 if (reg->set_ie_cb == NULL)
692 return reg->set_ie_cb(reg->cb_ctx, wpabuf_head(beacon_ie),
693 wpabuf_len(beacon_ie),
694 wpabuf_head(probe_resp_ie),
695 wpabuf_len(probe_resp_ie));
699 /* Encapsulate WPS IE data with one (or more, if needed) IE headers */
700 static struct wpabuf * wps_ie_encapsulate(struct wpabuf *data)
705 ie = wpabuf_alloc(wpabuf_len(data) + 100);
711 pos = wpabuf_head(data);
712 end = pos + wpabuf_len(data);
715 size_t frag_len = end - pos;
718 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
719 wpabuf_put_u8(ie, 4 + frag_len);
720 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
721 wpabuf_put_data(ie, pos, frag_len);
731 static int wps_set_ie(struct wps_registrar *reg)
733 struct wpabuf *beacon;
734 struct wpabuf *probe;
737 wpa_printf(MSG_DEBUG, "WPS: Build Beacon and Probe Response IEs");
739 beacon = wpabuf_alloc(300);
742 probe = wpabuf_alloc(400);
748 if (wps_build_version(beacon) ||
749 wps_build_wps_state(reg->wps, beacon) ||
750 wps_build_ap_setup_locked(reg->wps, beacon) ||
751 wps_build_selected_registrar(reg, beacon) ||
752 wps_build_sel_reg_dev_password_id(reg, beacon) ||
753 wps_build_sel_reg_config_methods(reg, beacon) ||
754 wps_build_version(probe) ||
755 wps_build_wps_state(reg->wps, probe) ||
756 wps_build_ap_setup_locked(reg->wps, probe) ||
757 wps_build_selected_registrar(reg, probe) ||
758 wps_build_sel_reg_dev_password_id(reg, probe) ||
759 wps_build_sel_reg_config_methods(reg, probe) ||
760 wps_build_resp_type(reg, probe) ||
761 wps_build_uuid_e(probe, reg->wps->uuid) ||
762 wps_build_device_attrs(®->wps->dev, probe) ||
763 wps_build_probe_config_methods(reg, probe) ||
764 wps_build_rf_bands(®->wps->dev, probe)) {
770 beacon = wps_ie_encapsulate(beacon);
771 probe = wps_ie_encapsulate(probe);
773 if (!beacon || !probe) {
779 ret = wps_cb_set_ie(reg, beacon, probe);
787 static int wps_get_dev_password(struct wps_data *wps)
792 os_free(wps->dev_password);
793 wps->dev_password = NULL;
796 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
797 pin = (const u8 *) "00000000";
800 pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
804 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
806 wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
811 wps->dev_password = os_malloc(pin_len);
812 if (wps->dev_password == NULL)
814 os_memcpy(wps->dev_password, pin, pin_len);
815 wps->dev_password_len = pin_len;
821 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
823 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
824 wpabuf_put_be16(msg, ATTR_UUID_R);
825 wpabuf_put_be16(msg, WPS_UUID_LEN);
826 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
831 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
837 if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
839 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
840 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
841 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
843 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
844 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
845 "R-Hash derivation");
849 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
850 wpabuf_put_be16(msg, ATTR_R_HASH1);
851 wpabuf_put_be16(msg, SHA256_MAC_LEN);
852 hash = wpabuf_put(msg, SHA256_MAC_LEN);
853 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
854 addr[0] = wps->snonce;
855 len[0] = WPS_SECRET_NONCE_LEN;
857 len[1] = WPS_PSK_LEN;
858 addr[2] = wpabuf_head(wps->dh_pubkey_e);
859 len[2] = wpabuf_len(wps->dh_pubkey_e);
860 addr[3] = wpabuf_head(wps->dh_pubkey_r);
861 len[3] = wpabuf_len(wps->dh_pubkey_r);
862 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
863 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
865 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
866 wpabuf_put_be16(msg, ATTR_R_HASH2);
867 wpabuf_put_be16(msg, SHA256_MAC_LEN);
868 hash = wpabuf_put(msg, SHA256_MAC_LEN);
869 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
870 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
872 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
873 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
879 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
881 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
882 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
883 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
884 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
889 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
891 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
892 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
893 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
894 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
895 WPS_SECRET_NONCE_LEN);
900 static int wps_build_cred_network_idx(struct wpabuf *msg,
901 struct wps_credential *cred)
903 wpa_printf(MSG_DEBUG, "WPS: * Network Index");
904 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
905 wpabuf_put_be16(msg, 1);
906 wpabuf_put_u8(msg, 1);
911 static int wps_build_cred_ssid(struct wpabuf *msg,
912 struct wps_credential *cred)
914 wpa_printf(MSG_DEBUG, "WPS: * SSID");
915 wpabuf_put_be16(msg, ATTR_SSID);
916 wpabuf_put_be16(msg, cred->ssid_len);
917 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
922 static int wps_build_cred_auth_type(struct wpabuf *msg,
923 struct wps_credential *cred)
925 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
927 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
928 wpabuf_put_be16(msg, 2);
929 wpabuf_put_be16(msg, cred->auth_type);
934 static int wps_build_cred_encr_type(struct wpabuf *msg,
935 struct wps_credential *cred)
937 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
939 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
940 wpabuf_put_be16(msg, 2);
941 wpabuf_put_be16(msg, cred->encr_type);
946 static int wps_build_cred_network_key(struct wpabuf *msg,
947 struct wps_credential *cred)
949 wpa_printf(MSG_DEBUG, "WPS: * Network Key");
950 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
951 wpabuf_put_be16(msg, cred->key_len);
952 wpabuf_put_data(msg, cred->key, cred->key_len);
957 static int wps_build_cred_mac_addr(struct wpabuf *msg,
958 struct wps_credential *cred)
960 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (" MACSTR ")",
961 MAC2STR(cred->mac_addr));
962 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
963 wpabuf_put_be16(msg, ETH_ALEN);
964 wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
969 static int wps_build_credential(struct wpabuf *msg,
970 struct wps_credential *cred)
972 if (wps_build_cred_network_idx(msg, cred) ||
973 wps_build_cred_ssid(msg, cred) ||
974 wps_build_cred_auth_type(msg, cred) ||
975 wps_build_cred_encr_type(msg, cred) ||
976 wps_build_cred_network_key(msg, cred) ||
977 wps_build_cred_mac_addr(msg, cred))
983 static int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
987 if (wps->wps->registrar->skip_cred_build)
988 goto skip_cred_build;
990 wpa_printf(MSG_DEBUG, "WPS: * Credential");
991 os_memset(&wps->cred, 0, sizeof(wps->cred));
993 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
994 wps->cred.ssid_len = wps->wps->ssid_len;
996 /* Select the best authentication and encryption type */
997 if (wps->auth_type & WPS_AUTH_WPA2PSK)
998 wps->auth_type = WPS_AUTH_WPA2PSK;
999 else if (wps->auth_type & WPS_AUTH_WPAPSK)
1000 wps->auth_type = WPS_AUTH_WPAPSK;
1001 else if (wps->auth_type & WPS_AUTH_OPEN)
1002 wps->auth_type = WPS_AUTH_OPEN;
1003 else if (wps->auth_type & WPS_AUTH_SHARED)
1004 wps->auth_type = WPS_AUTH_SHARED;
1006 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
1010 wps->cred.auth_type = wps->auth_type;
1012 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
1013 wps->auth_type == WPS_AUTH_WPAPSK) {
1014 if (wps->encr_type & WPS_ENCR_AES)
1015 wps->encr_type = WPS_ENCR_AES;
1016 else if (wps->encr_type & WPS_ENCR_TKIP)
1017 wps->encr_type = WPS_ENCR_TKIP;
1019 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1020 "type for WPA/WPA2");
1024 if (wps->encr_type & WPS_ENCR_WEP)
1025 wps->encr_type = WPS_ENCR_WEP;
1026 else if (wps->encr_type & WPS_ENCR_NONE)
1027 wps->encr_type = WPS_ENCR_NONE;
1029 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1030 "type for non-WPA/WPA2 mode");
1034 wps->cred.encr_type = wps->encr_type;
1035 /* Set MAC address in the Credential to be the AP's address (BSSID) */
1036 os_memcpy(wps->cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN);
1038 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
1039 !wps->wps->registrar->disable_auto_conf) {
1041 /* Generate a random passphrase */
1042 if (os_get_random(r, sizeof(r)) < 0)
1044 os_free(wps->new_psk);
1045 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
1046 if (wps->new_psk == NULL)
1048 wps->new_psk_len--; /* remove newline */
1049 while (wps->new_psk_len &&
1050 wps->new_psk[wps->new_psk_len - 1] == '=')
1052 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
1053 wps->new_psk, wps->new_psk_len);
1054 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
1055 wps->cred.key_len = wps->new_psk_len;
1056 } else if (wps->wps->network_key) {
1057 os_memcpy(wps->cred.key, wps->wps->network_key,
1058 wps->wps->network_key_len);
1059 wps->cred.key_len = wps->wps->network_key_len;
1060 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
1062 /* Generate a random per-device PSK */
1063 os_free(wps->new_psk);
1064 wps->new_psk_len = 32;
1065 wps->new_psk = os_malloc(wps->new_psk_len);
1066 if (wps->new_psk == NULL)
1068 if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
1069 os_free(wps->new_psk);
1070 wps->new_psk = NULL;
1073 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
1074 wps->new_psk, wps->new_psk_len);
1075 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
1077 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
1078 wps->cred.key_len = wps->new_psk_len * 2;
1081 cred = wpabuf_alloc(200);
1085 if (wps_build_credential(cred, &wps->cred)) {
1090 wpabuf_put_be16(msg, ATTR_CRED);
1091 wpabuf_put_be16(msg, wpabuf_len(cred));
1092 wpabuf_put_buf(msg, cred);
1096 if (wps->wps->registrar->extra_cred) {
1097 wpa_printf(MSG_DEBUG, "WPS: * Credential (pre-configured)");
1098 wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
1105 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
1107 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
1109 if (wps_build_credential(msg, &wps->cred))
1116 static struct wpabuf * wps_build_m2(struct wps_data *wps)
1120 if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
1122 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
1123 wps->nonce_r, WPS_NONCE_LEN);
1124 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1126 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1127 msg = wpabuf_alloc(1000);
1131 if (wps_build_version(msg) ||
1132 wps_build_msg_type(msg, WPS_M2) ||
1133 wps_build_enrollee_nonce(wps, msg) ||
1134 wps_build_registrar_nonce(wps, msg) ||
1135 wps_build_uuid_r(wps, msg) ||
1136 wps_build_public_key(wps, msg) ||
1137 wps_derive_keys(wps) ||
1138 wps_build_auth_type_flags(wps, msg) ||
1139 wps_build_encr_type_flags(wps, msg) ||
1140 wps_build_conn_type_flags(wps, msg) ||
1141 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1142 wps_build_device_attrs(&wps->wps->dev, msg) ||
1143 wps_build_rf_bands(&wps->wps->dev, msg) ||
1144 wps_build_assoc_state(wps, msg) ||
1145 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1146 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
1147 wps_build_os_version(&wps->wps->dev, msg) ||
1148 wps_build_authenticator(wps, msg)) {
1153 wps->state = RECV_M3;
1158 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1161 u16 err = WPS_CFG_NO_ERROR;
1163 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1164 msg = wpabuf_alloc(1000);
1168 if (wps->wps->ap && wps->wps->ap_setup_locked)
1169 err = WPS_CFG_SETUP_LOCKED;
1171 if (wps_build_version(msg) ||
1172 wps_build_msg_type(msg, WPS_M2D) ||
1173 wps_build_enrollee_nonce(wps, msg) ||
1174 wps_build_registrar_nonce(wps, msg) ||
1175 wps_build_uuid_r(wps, msg) ||
1176 wps_build_auth_type_flags(wps, msg) ||
1177 wps_build_encr_type_flags(wps, msg) ||
1178 wps_build_conn_type_flags(wps, msg) ||
1179 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1180 wps_build_device_attrs(&wps->wps->dev, msg) ||
1181 wps_build_rf_bands(&wps->wps->dev, msg) ||
1182 wps_build_assoc_state(wps, msg) ||
1183 wps_build_config_error(msg, err) ||
1184 wps_build_os_version(&wps->wps->dev, msg)) {
1189 wps->state = RECV_M2D_ACK;
1194 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1196 struct wpabuf *msg, *plain;
1198 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1200 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1202 plain = wpabuf_alloc(200);
1206 msg = wpabuf_alloc(1000);
1212 if (wps_build_version(msg) ||
1213 wps_build_msg_type(msg, WPS_M4) ||
1214 wps_build_enrollee_nonce(wps, msg) ||
1215 wps_build_r_hash(wps, msg) ||
1216 wps_build_r_snonce1(wps, plain) ||
1217 wps_build_key_wrap_auth(wps, plain) ||
1218 wps_build_encr_settings(wps, msg, plain) ||
1219 wps_build_authenticator(wps, msg)) {
1226 wps->state = RECV_M5;
1231 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1233 struct wpabuf *msg, *plain;
1235 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1237 plain = wpabuf_alloc(200);
1241 msg = wpabuf_alloc(1000);
1247 if (wps_build_version(msg) ||
1248 wps_build_msg_type(msg, WPS_M6) ||
1249 wps_build_enrollee_nonce(wps, msg) ||
1250 wps_build_r_snonce2(wps, plain) ||
1251 wps_build_key_wrap_auth(wps, plain) ||
1252 wps_build_encr_settings(wps, msg, plain) ||
1253 wps_build_authenticator(wps, msg)) {
1260 wps->wps_pin_revealed = 1;
1261 wps->state = RECV_M7;
1266 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1268 struct wpabuf *msg, *plain;
1270 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1272 plain = wpabuf_alloc(500);
1276 msg = wpabuf_alloc(1000);
1282 if (wps_build_version(msg) ||
1283 wps_build_msg_type(msg, WPS_M8) ||
1284 wps_build_enrollee_nonce(wps, msg) ||
1285 (wps->wps->ap && wps_build_cred(wps, plain)) ||
1286 (!wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
1287 wps_build_key_wrap_auth(wps, plain) ||
1288 wps_build_encr_settings(wps, msg, plain) ||
1289 wps_build_authenticator(wps, msg)) {
1296 wps->state = RECV_DONE;
1301 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
1305 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
1307 msg = wpabuf_alloc(1000);
1311 if (wps_build_version(msg) ||
1312 wps_build_msg_type(msg, WPS_WSC_ACK) ||
1313 wps_build_enrollee_nonce(wps, msg) ||
1314 wps_build_registrar_nonce(wps, msg)) {
1323 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
1327 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
1329 msg = wpabuf_alloc(1000);
1333 if (wps_build_version(msg) ||
1334 wps_build_msg_type(msg, WPS_WSC_NACK) ||
1335 wps_build_enrollee_nonce(wps, msg) ||
1336 wps_build_registrar_nonce(wps, msg) ||
1337 wps_build_config_error(msg, wps->config_error)) {
1346 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1347 enum wsc_op_code *op_code)
1351 #ifdef CONFIG_WPS_UPNP
1352 if (wps->wps->wps_upnp) {
1353 struct upnp_pending_message *p, *prev = NULL;
1354 if (wps->ext_reg > 1)
1355 wps_registrar_free_pending_m2(wps->wps);
1356 p = wps->wps->upnp_msgs;
1357 /* TODO: check pending message MAC address */
1358 while (p && p->next) {
1363 wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
1368 wps->wps->upnp_msgs = NULL;
1372 if (wps->ext_reg == 0)
1378 wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
1379 "pending message available");
1382 #endif /* CONFIG_WPS_UPNP */
1384 switch (wps->state) {
1386 if (wps_get_dev_password(wps) < 0)
1387 msg = wps_build_m2d(wps);
1389 msg = wps_build_m2(wps);
1393 msg = wps_build_m2d(wps);
1397 msg = wps_build_m4(wps);
1401 msg = wps_build_m6(wps);
1405 msg = wps_build_m8(wps);
1409 msg = wps_build_wsc_ack(wps);
1413 msg = wps_build_wsc_nack(wps);
1414 *op_code = WSC_NACK;
1417 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
1418 "a message", wps->state);
1423 if (*op_code == WSC_MSG && msg) {
1424 /* Save a copy of the last message for Authenticator derivation
1426 wpabuf_free(wps->last_msg);
1427 wps->last_msg = wpabuf_dup(msg);
1434 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
1436 if (e_nonce == NULL) {
1437 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
1441 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
1442 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
1443 wps->nonce_e, WPS_NONCE_LEN);
1449 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
1451 if (r_nonce == NULL) {
1452 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
1456 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
1457 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
1465 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
1467 if (uuid_e == NULL) {
1468 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
1472 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
1473 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
1479 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
1481 if (pw_id == NULL) {
1482 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
1486 wps->dev_pw_id = WPA_GET_BE16(pw_id);
1487 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
1493 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
1495 if (e_hash1 == NULL) {
1496 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
1500 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
1501 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
1507 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
1509 if (e_hash2 == NULL) {
1510 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
1514 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
1515 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
1521 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
1523 u8 hash[SHA256_MAC_LEN];
1527 if (e_snonce1 == NULL) {
1528 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
1532 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
1533 WPS_SECRET_NONCE_LEN);
1535 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
1536 addr[0] = e_snonce1;
1537 len[0] = WPS_SECRET_NONCE_LEN;
1538 addr[1] = wps->psk1;
1539 len[1] = WPS_PSK_LEN;
1540 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1541 len[2] = wpabuf_len(wps->dh_pubkey_e);
1542 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1543 len[3] = wpabuf_len(wps->dh_pubkey_r);
1544 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1546 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
1547 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
1548 "not match with the pre-committed value");
1549 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1550 wps_pwd_auth_fail_event(wps->wps, 0, 1);
1554 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
1555 "half of the device password");
1561 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
1563 u8 hash[SHA256_MAC_LEN];
1567 if (e_snonce2 == NULL) {
1568 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
1572 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
1573 WPS_SECRET_NONCE_LEN);
1575 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
1576 addr[0] = e_snonce2;
1577 len[0] = WPS_SECRET_NONCE_LEN;
1578 addr[1] = wps->psk2;
1579 len[1] = WPS_PSK_LEN;
1580 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1581 len[2] = wpabuf_len(wps->dh_pubkey_e);
1582 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1583 len[3] = wpabuf_len(wps->dh_pubkey_r);
1584 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1586 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
1587 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
1588 "not match with the pre-committed value");
1589 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
1590 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1591 wps_pwd_auth_fail_event(wps->wps, 0, 2);
1595 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
1596 "half of the device password");
1597 wps->wps_pin_revealed = 0;
1598 wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
1604 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
1606 if (mac_addr == NULL) {
1607 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
1611 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
1613 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
1614 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
1620 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
1623 if (pk == NULL || pk_len == 0) {
1624 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
1628 wpabuf_free(wps->dh_pubkey_e);
1629 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
1630 if (wps->dh_pubkey_e == NULL)
1637 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
1642 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
1647 auth_types = WPA_GET_BE16(auth);
1649 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
1651 wps->auth_type = wps->wps->auth_types & auth_types;
1652 if (wps->auth_type == 0) {
1653 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1654 "authentication types (own 0x%x Enrollee 0x%x)",
1655 wps->wps->auth_types, auth_types);
1663 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
1668 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
1673 encr_types = WPA_GET_BE16(encr);
1675 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
1677 wps->encr_type = wps->wps->encr_types & encr_types;
1678 if (wps->encr_type == 0) {
1679 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1680 "encryption types");
1688 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
1691 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
1696 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
1703 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
1707 if (methods == NULL) {
1708 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
1712 m = WPA_GET_BE16(methods);
1714 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x", m);
1720 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
1722 if (state == NULL) {
1723 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
1728 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
1735 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
1739 if (assoc == NULL) {
1740 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
1744 a = WPA_GET_BE16(assoc);
1745 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
1751 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
1756 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
1760 e = WPA_GET_BE16(err);
1761 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
1767 static enum wps_process_res wps_process_m1(struct wps_data *wps,
1768 struct wps_parse_attr *attr)
1770 wpa_printf(MSG_DEBUG, "WPS: Received M1");
1772 if (wps->state != RECV_M1) {
1773 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1774 "receiving M1", wps->state);
1778 if (wps_process_uuid_e(wps, attr->uuid_e) ||
1779 wps_process_mac_addr(wps, attr->mac_addr) ||
1780 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1781 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
1782 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
1783 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
1784 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
1785 wps_process_config_methods(wps, attr->config_methods) ||
1786 wps_process_wps_state(wps, attr->wps_state) ||
1787 wps_process_device_attrs(&wps->peer_dev, attr) ||
1788 wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
1789 wps_process_assoc_state(wps, attr->assoc_state) ||
1790 wps_process_dev_password_id(wps, attr->dev_password_id) ||
1791 wps_process_config_error(wps, attr->config_error) ||
1792 wps_process_os_version(&wps->peer_dev, attr->os_version))
1795 if (wps->dev_pw_id != DEV_PW_DEFAULT &&
1796 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
1797 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
1798 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
1799 (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
1800 !wps->wps->registrar->pbc)) {
1801 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
1803 wps->state = SEND_M2D;
1804 return WPS_CONTINUE;
1807 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
1808 if (wps_registrar_pbc_overlap(wps->wps->registrar,
1809 wps->mac_addr_e, wps->uuid_e)) {
1810 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
1812 wps->state = SEND_M2D;
1813 return WPS_CONTINUE;
1815 wps_registrar_add_pbc_session(wps->wps->registrar,
1816 wps->mac_addr_e, wps->uuid_e);
1820 wps->state = SEND_M2;
1821 return WPS_CONTINUE;
1825 static enum wps_process_res wps_process_m3(struct wps_data *wps,
1826 const struct wpabuf *msg,
1827 struct wps_parse_attr *attr)
1829 wpa_printf(MSG_DEBUG, "WPS: Received M3");
1831 if (wps->state != RECV_M3) {
1832 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1833 "receiving M3", wps->state);
1834 wps->state = SEND_WSC_NACK;
1835 return WPS_CONTINUE;
1838 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1839 wps_process_authenticator(wps, attr->authenticator, msg) ||
1840 wps_process_e_hash1(wps, attr->e_hash1) ||
1841 wps_process_e_hash2(wps, attr->e_hash2)) {
1842 wps->state = SEND_WSC_NACK;
1843 return WPS_CONTINUE;
1846 wps->state = SEND_M4;
1847 return WPS_CONTINUE;
1851 static enum wps_process_res wps_process_m5(struct wps_data *wps,
1852 const struct wpabuf *msg,
1853 struct wps_parse_attr *attr)
1855 struct wpabuf *decrypted;
1856 struct wps_parse_attr eattr;
1858 wpa_printf(MSG_DEBUG, "WPS: Received M5");
1860 if (wps->state != RECV_M5) {
1861 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1862 "receiving M5", wps->state);
1863 wps->state = SEND_WSC_NACK;
1864 return WPS_CONTINUE;
1867 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1868 wps_process_authenticator(wps, attr->authenticator, msg)) {
1869 wps->state = SEND_WSC_NACK;
1870 return WPS_CONTINUE;
1873 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1874 attr->encr_settings_len);
1875 if (decrypted == NULL) {
1876 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1877 "Settings attribute");
1878 wps->state = SEND_WSC_NACK;
1879 return WPS_CONTINUE;
1882 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1884 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1885 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1886 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
1887 wpabuf_free(decrypted);
1888 wps->state = SEND_WSC_NACK;
1889 return WPS_CONTINUE;
1891 wpabuf_free(decrypted);
1893 wps->state = SEND_M6;
1894 return WPS_CONTINUE;
1898 static int wps_process_ap_settings_r(struct wps_data *wps,
1899 struct wps_parse_attr *attr)
1904 /* AP Settings Attributes in M7 when Enrollee is an AP */
1905 if (wps_process_ap_settings(attr, &wps->cred) < 0)
1908 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
1911 * TODO: Provide access to AP settings and allow changes before sending
1912 * out M8. For now, just copy the settings unchanged into M8.
1919 static enum wps_process_res wps_process_m7(struct wps_data *wps,
1920 const struct wpabuf *msg,
1921 struct wps_parse_attr *attr)
1923 struct wpabuf *decrypted;
1924 struct wps_parse_attr eattr;
1926 wpa_printf(MSG_DEBUG, "WPS: Received M7");
1928 if (wps->state != RECV_M7) {
1929 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1930 "receiving M7", wps->state);
1931 wps->state = SEND_WSC_NACK;
1932 return WPS_CONTINUE;
1935 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1936 wps_process_authenticator(wps, attr->authenticator, msg)) {
1937 wps->state = SEND_WSC_NACK;
1938 return WPS_CONTINUE;
1941 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1942 attr->encr_settings_len);
1943 if (decrypted == NULL) {
1944 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1945 "Settings attribute");
1946 wps->state = SEND_WSC_NACK;
1947 return WPS_CONTINUE;
1950 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1952 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1953 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1954 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
1955 wps_process_ap_settings_r(wps, &eattr)) {
1956 wpabuf_free(decrypted);
1957 wps->state = SEND_WSC_NACK;
1958 return WPS_CONTINUE;
1961 wpabuf_free(decrypted);
1963 wps->state = SEND_M8;
1964 return WPS_CONTINUE;
1968 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
1969 const struct wpabuf *msg)
1971 struct wps_parse_attr attr;
1972 enum wps_process_res ret = WPS_CONTINUE;
1974 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
1976 if (wps_parse_msg(msg, &attr) < 0)
1979 if (!wps_version_supported(attr.version)) {
1980 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1981 attr.version ? *attr.version : 0);
1985 if (attr.msg_type == NULL) {
1986 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1990 if (*attr.msg_type != WPS_M1 &&
1991 (attr.registrar_nonce == NULL ||
1992 os_memcmp(wps->nonce_r, attr.registrar_nonce,
1993 WPS_NONCE_LEN != 0))) {
1994 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1998 switch (*attr.msg_type) {
2000 #ifdef CONFIG_WPS_UPNP
2001 if (wps->wps->wps_upnp && attr.mac_addr) {
2002 /* Remove old pending messages when starting new run */
2003 wps_free_pending_msgs(wps->wps->upnp_msgs);
2004 wps->wps->upnp_msgs = NULL;
2006 upnp_wps_device_send_wlan_event(
2007 wps->wps->wps_upnp, attr.mac_addr,
2008 UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
2010 #endif /* CONFIG_WPS_UPNP */
2011 ret = wps_process_m1(wps, &attr);
2014 ret = wps_process_m3(wps, msg, &attr);
2015 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2016 wps_fail_event(wps->wps, WPS_M3);
2019 ret = wps_process_m5(wps, msg, &attr);
2020 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2021 wps_fail_event(wps->wps, WPS_M5);
2024 ret = wps_process_m7(wps, msg, &attr);
2025 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2026 wps_fail_event(wps->wps, WPS_M7);
2029 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
2034 if (ret == WPS_CONTINUE) {
2035 /* Save a copy of the last message for Authenticator derivation
2037 wpabuf_free(wps->last_msg);
2038 wps->last_msg = wpabuf_dup(msg);
2045 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
2046 const struct wpabuf *msg)
2048 struct wps_parse_attr attr;
2050 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
2052 if (wps_parse_msg(msg, &attr) < 0)
2055 if (!wps_version_supported(attr.version)) {
2056 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2057 attr.version ? *attr.version : 0);
2061 if (attr.msg_type == NULL) {
2062 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2066 if (*attr.msg_type != WPS_WSC_ACK) {
2067 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2072 #ifdef CONFIG_WPS_UPNP
2073 if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
2074 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2075 if (wps->wps->upnp_msgs)
2076 return WPS_CONTINUE;
2077 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2078 "external Registrar");
2081 #endif /* CONFIG_WPS_UPNP */
2083 if (attr.registrar_nonce == NULL ||
2084 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2086 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2090 if (attr.enrollee_nonce == NULL ||
2091 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2092 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2096 if (wps->state == RECV_M2D_ACK) {
2097 #ifdef CONFIG_WPS_UPNP
2098 if (wps->wps->wps_upnp &&
2099 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2100 if (wps->wps->upnp_msgs)
2101 return WPS_CONTINUE;
2102 if (wps->ext_reg == 0)
2104 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2105 "external Registrar");
2108 #endif /* CONFIG_WPS_UPNP */
2110 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
2111 "terminate negotiation");
2118 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
2119 const struct wpabuf *msg)
2121 struct wps_parse_attr attr;
2124 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
2126 old_state = wps->state;
2127 wps->state = SEND_WSC_NACK;
2129 if (wps_parse_msg(msg, &attr) < 0)
2132 if (!wps_version_supported(attr.version)) {
2133 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2134 attr.version ? *attr.version : 0);
2138 if (attr.msg_type == NULL) {
2139 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2143 if (*attr.msg_type != WPS_WSC_NACK) {
2144 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2149 #ifdef CONFIG_WPS_UPNP
2150 if (wps->wps->wps_upnp && wps->ext_reg) {
2151 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2152 "Registrar terminated by the Enrollee");
2155 #endif /* CONFIG_WPS_UPNP */
2157 if (attr.registrar_nonce == NULL ||
2158 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2160 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2164 if (attr.enrollee_nonce == NULL ||
2165 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2166 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2170 if (attr.config_error == NULL) {
2171 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
2176 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
2177 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
2179 switch (old_state) {
2181 wps_fail_event(wps->wps, WPS_M2);
2184 wps_fail_event(wps->wps, WPS_M4);
2187 wps_fail_event(wps->wps, WPS_M6);
2190 wps_fail_event(wps->wps, WPS_M8);
2200 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
2201 const struct wpabuf *msg)
2203 struct wps_parse_attr attr;
2205 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
2207 if (wps->state != RECV_DONE &&
2208 (!wps->wps->wps_upnp || !wps->ext_reg)) {
2209 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2210 "receiving WSC_Done", wps->state);
2214 if (wps_parse_msg(msg, &attr) < 0)
2217 if (!wps_version_supported(attr.version)) {
2218 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2219 attr.version ? *attr.version : 0);
2223 if (attr.msg_type == NULL) {
2224 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2228 if (*attr.msg_type != WPS_WSC_DONE) {
2229 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2234 #ifdef CONFIG_WPS_UPNP
2235 if (wps->wps->wps_upnp && wps->ext_reg) {
2236 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2237 "Registrar completed successfully");
2240 #endif /* CONFIG_WPS_UPNP */
2242 if (attr.registrar_nonce == NULL ||
2243 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2245 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2249 if (attr.enrollee_nonce == NULL ||
2250 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2251 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2255 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
2257 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
2258 wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
2259 struct wps_credential cred;
2261 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
2262 "on first Enrollee connection");
2264 os_memset(&cred, 0, sizeof(cred));
2265 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
2266 cred.ssid_len = wps->wps->ssid_len;
2267 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
2268 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
2269 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
2270 cred.key_len = wps->new_psk_len;
2272 wps->wps->wps_state = WPS_STATE_CONFIGURED;
2273 wpa_hexdump_ascii_key(MSG_DEBUG,
2274 "WPS: Generated random passphrase",
2275 wps->new_psk, wps->new_psk_len);
2276 if (wps->wps->cred_cb)
2277 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
2279 os_free(wps->new_psk);
2280 wps->new_psk = NULL;
2283 if (!wps->wps->ap) {
2284 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based "
2285 "on the modified AP configuration");
2286 if (wps->wps->cred_cb)
2287 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2291 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
2292 wps->new_psk, wps->new_psk_len)) {
2293 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
2296 os_free(wps->new_psk);
2297 wps->new_psk = NULL;
2300 wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e);
2303 wps_registrar_remove_pbc_session(wps->wps->registrar,
2304 wps->mac_addr_e, wps->uuid_e);
2305 wps_registrar_pbc_completed(wps->wps->registrar);
2308 wps_success_event(wps->wps);
2314 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
2315 enum wsc_op_code op_code,
2316 const struct wpabuf *msg)
2318 enum wps_process_res ret;
2320 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
2322 (unsigned long) wpabuf_len(msg), op_code);
2324 #ifdef CONFIG_WPS_UPNP
2325 if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
2326 struct wps_parse_attr attr;
2327 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
2328 *attr.msg_type == WPS_M3)
2329 wps->ext_reg = 2; /* past M2/M2D phase */
2331 if (wps->ext_reg > 1)
2332 wps_registrar_free_pending_m2(wps->wps);
2333 if (wps->wps->wps_upnp && wps->ext_reg &&
2334 wps->wps->upnp_msgs == NULL &&
2335 (op_code == WSC_MSG || op_code == WSC_Done)) {
2336 struct wps_parse_attr attr;
2338 if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
2341 type = *attr.msg_type;
2342 wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
2343 " to external Registrar for processing", type);
2344 upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
2346 UPNP_WPS_WLANEVENT_TYPE_EAP,
2348 if (op_code == WSC_MSG)
2350 } else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
2351 wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
2352 "external Registrar");
2353 return WPS_CONTINUE;
2355 #endif /* CONFIG_WPS_UPNP */
2359 return wps_process_wsc_msg(wps, msg);
2361 return wps_process_wsc_ack(wps, msg);
2363 return wps_process_wsc_nack(wps, msg);
2365 ret = wps_process_wsc_done(wps, msg);
2366 if (ret == WPS_FAILURE) {
2367 wps->state = SEND_WSC_NACK;
2368 wps_fail_event(wps->wps, WPS_WSC_DONE);
2372 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
2378 int wps_registrar_update_ie(struct wps_registrar *reg)
2380 return wps_set_ie(reg);
2384 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
2387 struct wps_registrar *reg = eloop_ctx;
2389 wpa_printf(MSG_DEBUG, "WPS: SetSelectedRegistrar timed out - "
2390 "unselect Registrar");
2391 reg->selected_registrar = 0;
2393 reg->sel_reg_dev_password_id_override = -1;
2394 reg->sel_reg_config_methods_override = -1;
2400 * wps_registrar_set_selected_registrar - Notification of SetSelectedRegistrar
2401 * @reg: Registrar data from wps_registrar_init()
2402 * @msg: Received message from SetSelectedRegistrar
2403 * @msg_len: Length of msg in octets
2404 * Returns: 0 on success, -1 on failure
2406 * This function is called when an AP receives a SetSelectedRegistrar UPnP
2409 int wps_registrar_set_selected_registrar(struct wps_registrar *reg,
2410 const struct wpabuf *msg)
2412 struct wps_parse_attr attr;
2414 wpa_hexdump_buf(MSG_MSGDUMP, "WPS: SetSelectedRegistrar attributes",
2417 if (wps_parse_msg(msg, &attr) < 0)
2419 if (!wps_version_supported(attr.version)) {
2420 wpa_printf(MSG_DEBUG, "WPS: Unsupported SetSelectedRegistrar "
2421 "version 0x%x", attr.version ? *attr.version : 0);
2425 if (attr.selected_registrar == NULL ||
2426 *attr.selected_registrar == 0) {
2427 wpa_printf(MSG_DEBUG, "WPS: SetSelectedRegistrar: Disable "
2428 "Selected Registrar");
2429 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg,
2431 wps_registrar_set_selected_timeout(reg, NULL);
2435 reg->selected_registrar = 1;
2436 reg->sel_reg_dev_password_id_override = attr.dev_password_id ?
2437 WPA_GET_BE16(attr.dev_password_id) : DEV_PW_DEFAULT;
2438 reg->sel_reg_config_methods_override = attr.sel_reg_config_methods ?
2439 WPA_GET_BE16(attr.sel_reg_config_methods) : -1;
2442 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
2443 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
2444 wps_registrar_set_selected_timeout,