2 * Wi-Fi Protected Setup - Registrar
3 * Copyright (c) 2008-2009, 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.
15 #include "utils/includes.h"
17 #include "utils/common.h"
18 #include "utils/base64.h"
19 #include "utils/eloop.h"
20 #include "utils/uuid.h"
21 #include "utils/list.h"
22 #include "crypto/crypto.h"
23 #include "crypto/sha256.h"
24 #include "common/ieee802_11_defs.h"
26 #include "wps_dev_attr.h"
28 #include "wps_upnp_i.h"
30 #ifndef CONFIG_WPS_STRICT
31 #define WPS_WORKAROUNDS
32 #endif /* CONFIG_WPS_STRICT */
36 u8 uuid[WPS_UUID_LEN];
40 #define PIN_LOCKED BIT(0)
41 #define PIN_EXPIRES BIT(1)
43 struct os_time expiration;
44 u8 enrollee_addr[ETH_ALEN];
48 static void wps_free_pin(struct wps_uuid_pin *pin)
55 static void wps_remove_pin(struct wps_uuid_pin *pin)
57 dl_list_del(&pin->list);
62 static void wps_free_pins(struct dl_list *pins)
64 struct wps_uuid_pin *pin, *prev;
65 dl_list_for_each_safe(pin, prev, pins, struct wps_uuid_pin, list)
70 struct wps_pbc_session {
71 struct wps_pbc_session *next;
73 u8 uuid_e[WPS_UUID_LEN];
74 struct os_time timestamp;
78 static void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
80 struct wps_pbc_session *prev;
90 struct wps_registrar_device {
91 struct wps_registrar_device *next;
92 struct wps_device_data dev;
93 u8 uuid[WPS_UUID_LEN];
97 struct wps_registrar {
98 struct wps_context *wps;
101 int selected_registrar;
103 int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
105 int (*set_ie_cb)(void *ctx, struct wpabuf *beacon_ie,
106 struct wpabuf *probe_resp_ie);
107 void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
108 const struct wps_device_data *dev);
109 void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
111 void (*set_sel_reg_cb)(void *ctx, int sel_reg, u16 dev_passwd_id,
112 u16 sel_reg_config_methods);
113 void (*enrollee_seen_cb)(void *ctx, const u8 *addr, const u8 *uuid_e,
114 const u8 *pri_dev_type, u16 config_methods,
115 u16 dev_password_id, u8 request_type,
116 const char *dev_name);
120 struct wps_pbc_session *pbc_sessions;
123 struct wpabuf *extra_cred;
124 int disable_auto_conf;
126 int sel_reg_dev_password_id_override;
127 int sel_reg_config_methods_override;
131 struct wps_registrar_device *devices;
133 int force_pbc_overlap;
135 u8 authorized_macs[WPS_MAX_AUTHORIZED_MACS][ETH_ALEN];
136 u8 authorized_macs_union[WPS_MAX_AUTHORIZED_MACS][ETH_ALEN];
140 static int wps_set_ie(struct wps_registrar *reg);
141 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
142 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
146 static void wps_registrar_add_authorized_mac(struct wps_registrar *reg,
150 wpa_printf(MSG_DEBUG, "WPS: Add authorized MAC " MACSTR,
152 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++)
153 if (os_memcmp(reg->authorized_macs[i], addr, ETH_ALEN) == 0) {
154 wpa_printf(MSG_DEBUG, "WPS: Authorized MAC was "
155 "already in the list");
156 return; /* already in list */
158 for (i = WPS_MAX_AUTHORIZED_MACS - 1; i > 0; i--)
159 os_memcpy(reg->authorized_macs[i], reg->authorized_macs[i - 1],
161 os_memcpy(reg->authorized_macs[0], addr, ETH_ALEN);
162 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs",
163 (u8 *) reg->authorized_macs, sizeof(reg->authorized_macs));
167 static void wps_registrar_remove_authorized_mac(struct wps_registrar *reg,
171 wpa_printf(MSG_DEBUG, "WPS: Remove authorized MAC " MACSTR,
173 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++) {
174 if (os_memcmp(reg->authorized_macs, addr, ETH_ALEN) == 0)
177 if (i == WPS_MAX_AUTHORIZED_MACS) {
178 wpa_printf(MSG_DEBUG, "WPS: Authorized MAC was not in the "
180 return; /* not in the list */
182 for (; i + 1 < WPS_MAX_AUTHORIZED_MACS; i++)
183 os_memcpy(reg->authorized_macs[i], reg->authorized_macs[i + 1],
185 os_memset(reg->authorized_macs[WPS_MAX_AUTHORIZED_MACS - 1], 0,
187 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs",
188 (u8 *) reg->authorized_macs, sizeof(reg->authorized_macs));
192 static void wps_free_devices(struct wps_registrar_device *dev)
194 struct wps_registrar_device *prev;
199 wps_device_data_free(&prev->dev);
205 static struct wps_registrar_device * wps_device_get(struct wps_registrar *reg,
208 struct wps_registrar_device *dev;
210 for (dev = reg->devices; dev; dev = dev->next) {
211 if (os_memcmp(dev->dev.mac_addr, addr, ETH_ALEN) == 0)
218 static void wps_device_clone_data(struct wps_device_data *dst,
219 struct wps_device_data *src)
221 os_memcpy(dst->mac_addr, src->mac_addr, ETH_ALEN);
222 os_memcpy(dst->pri_dev_type, src->pri_dev_type, WPS_DEV_TYPE_LEN);
224 #define WPS_STRDUP(n) \
226 dst->n = src->n ? os_strdup(src->n) : NULL
228 WPS_STRDUP(device_name);
229 WPS_STRDUP(manufacturer);
230 WPS_STRDUP(model_name);
231 WPS_STRDUP(model_number);
232 WPS_STRDUP(serial_number);
237 int wps_device_store(struct wps_registrar *reg,
238 struct wps_device_data *dev, const u8 *uuid)
240 struct wps_registrar_device *d;
242 d = wps_device_get(reg, dev->mac_addr);
244 d = os_zalloc(sizeof(*d));
247 d->next = reg->devices;
251 wps_device_clone_data(&d->dev, dev);
252 os_memcpy(d->uuid, uuid, WPS_UUID_LEN);
258 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
259 const u8 *addr, const u8 *uuid_e)
261 struct wps_pbc_session *pbc, *prev = NULL;
266 pbc = reg->pbc_sessions;
268 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
269 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
271 prev->next = pbc->next;
273 reg->pbc_sessions = pbc->next;
281 pbc = os_zalloc(sizeof(*pbc));
284 os_memcpy(pbc->addr, addr, ETH_ALEN);
286 os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
289 pbc->next = reg->pbc_sessions;
290 reg->pbc_sessions = pbc;
291 pbc->timestamp = now;
293 /* remove entries that have timed out */
298 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
300 wps_free_pbc_sessions(pbc);
309 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
310 const u8 *addr, const u8 *uuid_e)
312 struct wps_pbc_session *pbc, *prev = NULL;
314 pbc = reg->pbc_sessions;
316 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
317 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
319 prev->next = pbc->next;
321 reg->pbc_sessions = pbc->next;
331 int wps_registrar_pbc_overlap(struct wps_registrar *reg,
332 const u8 *addr, const u8 *uuid_e)
335 struct wps_pbc_session *pbc;
340 for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
341 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME)
343 if (addr == NULL || os_memcmp(addr, pbc->addr, ETH_ALEN) ||
345 os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN))
352 return count > 1 ? 1 : 0;
356 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
358 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
360 wpabuf_put_be16(msg, ATTR_WPS_STATE);
361 wpabuf_put_be16(msg, 1);
362 wpabuf_put_u8(msg, wps->wps_state);
367 #ifdef CONFIG_WPS_UPNP
368 static void wps_registrar_free_pending_m2(struct wps_context *wps)
370 struct upnp_pending_message *p, *p2, *prev = NULL;
373 if (p->type == WPS_M2 || p->type == WPS_M2D) {
375 wps->upnp_msgs = p->next;
377 prev->next = p->next;
378 wpa_printf(MSG_DEBUG, "WPS UPnP: Drop pending M2/M2D");
381 wpabuf_free(p2->msg);
389 #endif /* CONFIG_WPS_UPNP */
392 static int wps_build_ap_setup_locked(struct wps_context *wps,
395 if (wps->ap_setup_locked) {
396 wpa_printf(MSG_DEBUG, "WPS: * AP Setup Locked");
397 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
398 wpabuf_put_be16(msg, 1);
399 wpabuf_put_u8(msg, 1);
405 static int wps_build_selected_registrar(struct wps_registrar *reg,
408 if (!reg->sel_reg_union)
410 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar");
411 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
412 wpabuf_put_be16(msg, 1);
413 wpabuf_put_u8(msg, 1);
418 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
421 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
422 if (!reg->sel_reg_union)
424 if (reg->sel_reg_dev_password_id_override >= 0)
425 id = reg->sel_reg_dev_password_id_override;
426 wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
427 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
428 wpabuf_put_be16(msg, 2);
429 wpabuf_put_be16(msg, id);
434 static int wps_build_sel_pbc_reg_uuid_e(struct wps_registrar *reg,
437 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
438 if (!reg->sel_reg_union)
440 if (reg->sel_reg_dev_password_id_override >= 0)
441 id = reg->sel_reg_dev_password_id_override;
442 if (id != DEV_PW_PUSHBUTTON || !reg->dualband)
444 return wps_build_uuid_e(msg, reg->wps->uuid);
448 static void wps_set_pushbutton(u16 *methods, u16 conf_methods)
450 *methods |= WPS_CONFIG_PUSHBUTTON;
452 if (conf_methods & WPS_CONFIG_VIRT_PUSHBUTTON)
453 *methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
454 if (conf_methods & WPS_CONFIG_PHY_PUSHBUTTON)
455 *methods |= WPS_CONFIG_PHY_PUSHBUTTON;
456 if ((*methods & WPS_CONFIG_VIRT_PUSHBUTTON) !=
457 WPS_CONFIG_VIRT_PUSHBUTTON ||
458 (*methods & WPS_CONFIG_PHY_PUSHBUTTON) !=
459 WPS_CONFIG_PHY_PUSHBUTTON) {
461 * Required to include virtual/physical flag, but we were not
462 * configured with push button type, so have to default to one
465 *methods |= WPS_CONFIG_PHY_PUSHBUTTON;
467 #endif /* CONFIG_WPS2 */
471 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
475 if (!reg->sel_reg_union)
477 methods = reg->wps->config_methods;
478 methods &= ~WPS_CONFIG_PUSHBUTTON;
480 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
481 WPS_CONFIG_PHY_PUSHBUTTON);
482 #endif /* CONFIG_WPS2 */
484 wps_set_pushbutton(&methods, reg->wps->config_methods);
485 if (reg->sel_reg_config_methods_override >= 0)
486 methods = reg->sel_reg_config_methods_override;
487 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
489 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
490 wpabuf_put_be16(msg, 2);
491 wpabuf_put_be16(msg, methods);
496 static int wps_build_probe_config_methods(struct wps_registrar *reg,
501 * These are the methods that the AP supports as an Enrollee for adding
502 * external Registrars.
504 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
506 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
507 WPS_CONFIG_PHY_PUSHBUTTON);
508 #endif /* CONFIG_WPS2 */
509 wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
510 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
511 wpabuf_put_be16(msg, 2);
512 wpabuf_put_be16(msg, methods);
517 static int wps_build_config_methods_r(struct wps_registrar *reg,
521 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
523 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
524 WPS_CONFIG_PHY_PUSHBUTTON);
525 #endif /* CONFIG_WPS2 */
527 wps_set_pushbutton(&methods, reg->wps->config_methods);
528 return wps_build_config_methods(msg, methods);
532 const u8 * wps_authorized_macs(struct wps_registrar *reg, size_t *count)
537 while (*count < WPS_MAX_AUTHORIZED_MACS) {
538 if (is_zero_ether_addr(reg->authorized_macs_union[*count]))
542 #endif /* CONFIG_WPS2 */
544 return (const u8 *) reg->authorized_macs_union;
549 * wps_registrar_init - Initialize WPS Registrar data
550 * @wps: Pointer to longterm WPS context
551 * @cfg: Registrar configuration
552 * Returns: Pointer to allocated Registrar data or %NULL on failure
554 * This function is used to initialize WPS Registrar functionality. It can be
555 * used for a single Registrar run (e.g., when run in a supplicant) or multiple
556 * runs (e.g., when run as an internal Registrar in an AP). Caller is
557 * responsible for freeing the returned data with wps_registrar_deinit() when
558 * Registrar functionality is not needed anymore.
560 struct wps_registrar *
561 wps_registrar_init(struct wps_context *wps,
562 const struct wps_registrar_config *cfg)
564 struct wps_registrar *reg = os_zalloc(sizeof(*reg));
568 dl_list_init(®->pins);
570 reg->new_psk_cb = cfg->new_psk_cb;
571 reg->set_ie_cb = cfg->set_ie_cb;
572 reg->pin_needed_cb = cfg->pin_needed_cb;
573 reg->reg_success_cb = cfg->reg_success_cb;
574 reg->set_sel_reg_cb = cfg->set_sel_reg_cb;
575 reg->enrollee_seen_cb = cfg->enrollee_seen_cb;
576 reg->cb_ctx = cfg->cb_ctx;
577 reg->skip_cred_build = cfg->skip_cred_build;
578 if (cfg->extra_cred) {
579 reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
580 cfg->extra_cred_len);
581 if (reg->extra_cred == NULL) {
586 reg->disable_auto_conf = cfg->disable_auto_conf;
587 reg->sel_reg_dev_password_id_override = -1;
588 reg->sel_reg_config_methods_override = -1;
589 reg->static_wep_only = cfg->static_wep_only;
590 reg->dualband = cfg->dualband;
592 if (wps_set_ie(reg)) {
593 wps_registrar_deinit(reg);
602 * wps_registrar_deinit - Deinitialize WPS Registrar data
603 * @reg: Registrar data from wps_registrar_init()
605 void wps_registrar_deinit(struct wps_registrar *reg)
609 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
610 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
611 wps_free_pins(®->pins);
612 wps_free_pbc_sessions(reg->pbc_sessions);
613 wpabuf_free(reg->extra_cred);
614 wps_free_devices(reg->devices);
620 * wps_registrar_add_pin - Configure a new PIN for Registrar
621 * @reg: Registrar data from wps_registrar_init()
622 * @addr: Enrollee MAC address or %NULL if not known
623 * @uuid: UUID-E or %NULL for wildcard (any UUID)
624 * @pin: PIN (Device Password)
625 * @pin_len: Length of pin in octets
626 * @timeout: Time (in seconds) when the PIN will be invalidated; 0 = no timeout
627 * Returns: 0 on success, -1 on failure
629 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *addr,
630 const u8 *uuid, const u8 *pin, size_t pin_len,
633 struct wps_uuid_pin *p;
635 p = os_zalloc(sizeof(*p));
639 os_memcpy(p->enrollee_addr, addr, ETH_ALEN);
641 p->wildcard_uuid = 1;
643 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
644 p->pin = os_malloc(pin_len);
645 if (p->pin == NULL) {
649 os_memcpy(p->pin, pin, pin_len);
650 p->pin_len = pin_len;
653 p->flags |= PIN_EXPIRES;
654 os_get_time(&p->expiration);
655 p->expiration.sec += timeout;
658 dl_list_add(®->pins, &p->list);
660 wpa_printf(MSG_DEBUG, "WPS: A new PIN configured (timeout=%d)",
662 wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
663 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
664 reg->selected_registrar = 1;
667 wps_registrar_add_authorized_mac(reg, addr);
669 wps_registrar_add_authorized_mac(
670 reg, (u8 *) "\xff\xff\xff\xff\xff\xff");
671 wps_registrar_selected_registrar_changed(reg);
672 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
673 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
674 wps_registrar_set_selected_timeout,
681 static void wps_registrar_remove_pin(struct wps_registrar *reg,
682 struct wps_uuid_pin *pin)
685 u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
687 if (is_zero_ether_addr(pin->enrollee_addr))
690 addr = pin->enrollee_addr;
691 wps_registrar_remove_authorized_mac(reg, addr);
693 wps_registrar_selected_registrar_changed(reg);
697 static void wps_registrar_expire_pins(struct wps_registrar *reg)
699 struct wps_uuid_pin *pin, *prev;
703 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
705 if ((pin->flags & PIN_EXPIRES) &&
706 os_time_before(&pin->expiration, &now)) {
707 wpa_hexdump(MSG_DEBUG, "WPS: Expired PIN for UUID",
708 pin->uuid, WPS_UUID_LEN);
709 wps_registrar_remove_pin(reg, pin);
716 * wps_registrar_invalidate_wildcard_pin - Invalidate a wildcard PIN
717 * @reg: Registrar data from wps_registrar_init()
718 * Returns: 0 on success, -1 if not wildcard PIN is enabled
720 static int wps_registrar_invalidate_wildcard_pin(struct wps_registrar *reg)
722 struct wps_uuid_pin *pin, *prev;
724 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
726 if (pin->wildcard_uuid) {
727 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
728 pin->uuid, WPS_UUID_LEN);
729 wps_registrar_remove_pin(reg, pin);
739 * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
740 * @reg: Registrar data from wps_registrar_init()
742 * Returns: 0 on success, -1 on failure (e.g., PIN not found)
744 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
746 struct wps_uuid_pin *pin, *prev;
748 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
750 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
751 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
752 pin->uuid, WPS_UUID_LEN);
753 wps_registrar_remove_pin(reg, pin);
762 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
763 const u8 *uuid, size_t *pin_len)
765 struct wps_uuid_pin *pin, *found = NULL;
767 wps_registrar_expire_pins(reg);
769 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
770 if (!pin->wildcard_uuid &&
771 os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
778 /* Check for wildcard UUIDs since none of the UUID-specific
780 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
781 if (pin->wildcard_uuid == 1) {
782 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
783 "PIN. Assigned it for this UUID-E");
784 pin->wildcard_uuid = 2;
785 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
796 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
797 * that could otherwise avoid PIN invalidations.
799 if (found->flags & PIN_LOCKED) {
800 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
801 "allow concurrent re-use");
804 *pin_len = found->pin_len;
805 found->flags |= PIN_LOCKED;
811 * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
812 * @reg: Registrar data from wps_registrar_init()
814 * Returns: 0 on success, -1 on failure
816 * PINs are locked to enforce only one concurrent use. This function unlocks a
817 * PIN to allow it to be used again. If the specified PIN was configured using
818 * a wildcard UUID, it will be removed instead of allowing multiple uses.
820 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
822 struct wps_uuid_pin *pin;
824 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
825 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
826 if (pin->wildcard_uuid == 2) {
827 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
829 return wps_registrar_invalidate_pin(reg, uuid);
831 pin->flags &= ~PIN_LOCKED;
840 static void wps_registrar_stop_pbc(struct wps_registrar *reg)
842 reg->selected_registrar = 0;
844 wps_registrar_selected_registrar_changed(reg);
848 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
850 struct wps_registrar *reg = eloop_ctx;
852 wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
853 wps_pbc_timeout_event(reg->wps);
854 wps_registrar_stop_pbc(reg);
859 * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
860 * @reg: Registrar data from wps_registrar_init()
861 * Returns: 0 on success, -1 on failure
863 * This function is called on an AP when a push button is pushed to activate
864 * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
865 * or when a PBC registration is completed.
867 int wps_registrar_button_pushed(struct wps_registrar *reg)
869 if (wps_registrar_pbc_overlap(reg, NULL, NULL)) {
870 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
872 wps_pbc_overlap_event(reg->wps);
875 wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
876 reg->force_pbc_overlap = 0;
877 reg->selected_registrar = 1;
879 wps_registrar_selected_registrar_changed(reg);
881 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
882 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
888 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
890 wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
891 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
892 wps_registrar_stop_pbc(reg);
896 static void wps_registrar_pin_completed(struct wps_registrar *reg)
898 wpa_printf(MSG_DEBUG, "WPS: PIN completed using internal Registrar");
899 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
900 reg->selected_registrar = 0;
901 wps_registrar_selected_registrar_changed(reg);
905 int wps_registrar_wps_cancel(struct wps_registrar *reg)
908 wpa_printf(MSG_DEBUG, "WPS: PBC is set - cancelling it");
909 wps_registrar_pbc_timeout(reg, NULL);
911 } else if (reg->selected_registrar) {
913 wpa_printf(MSG_DEBUG, "WPS: PIN is set - cancelling it");
914 wps_registrar_pin_completed(reg);
915 wps_registrar_invalidate_wildcard_pin(reg);
923 * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
924 * @reg: Registrar data from wps_registrar_init()
925 * @addr: MAC address of the Probe Request sender
926 * @wps_data: WPS IE contents
928 * This function is called on an AP when a Probe Request with WPS IE is
929 * received. This is used to track PBC mode use and to detect possible overlap
930 * situation with other WPS APs.
932 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
933 const struct wpabuf *wps_data,
936 struct wps_parse_attr attr;
938 wpa_hexdump_buf(MSG_MSGDUMP,
939 "WPS: Probe Request with WPS data received",
942 if (wps_parse_msg(wps_data, &attr) < 0)
945 if (attr.config_methods == NULL) {
946 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
951 if (attr.dev_password_id == NULL) {
952 wpa_printf(MSG_DEBUG, "WPS: No Device Password Id attribute "
957 if (reg->enrollee_seen_cb && attr.uuid_e &&
958 attr.primary_dev_type && attr.request_type && !p2p_wildcard) {
959 char *dev_name = NULL;
961 dev_name = os_zalloc(attr.dev_name_len + 1);
963 os_memcpy(dev_name, attr.dev_name,
967 reg->enrollee_seen_cb(reg->cb_ctx, addr, attr.uuid_e,
968 attr.primary_dev_type,
969 WPA_GET_BE16(attr.config_methods),
970 WPA_GET_BE16(attr.dev_password_id),
971 *attr.request_type, dev_name);
975 if (WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON)
976 return; /* Not PBC */
978 wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
979 MACSTR, MAC2STR(addr));
980 if (attr.uuid_e == NULL) {
981 wpa_printf(MSG_DEBUG, "WPS: Invalid Probe Request WPS IE: No "
986 wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
987 if (wps_registrar_pbc_overlap(reg, addr, attr.uuid_e)) {
988 wpa_printf(MSG_DEBUG, "WPS: PBC session overlap detected");
989 reg->force_pbc_overlap = 1;
990 wps_pbc_overlap_event(reg->wps);
995 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
996 const u8 *psk, size_t psk_len)
998 if (reg->new_psk_cb == NULL)
1001 return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
1005 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
1006 const struct wps_device_data *dev)
1008 if (reg->pin_needed_cb == NULL)
1011 reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
1015 static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
1018 if (reg->reg_success_cb == NULL)
1021 reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e);
1025 static int wps_cb_set_ie(struct wps_registrar *reg, struct wpabuf *beacon_ie,
1026 struct wpabuf *probe_resp_ie)
1028 return reg->set_ie_cb(reg->cb_ctx, beacon_ie, probe_resp_ie);
1032 static void wps_cb_set_sel_reg(struct wps_registrar *reg)
1035 if (reg->set_sel_reg_cb == NULL)
1038 if (reg->selected_registrar) {
1039 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
1041 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
1042 WPS_CONFIG_PHY_PUSHBUTTON);
1043 #endif /* CONFIG_WPS2 */
1045 wps_set_pushbutton(&methods, reg->wps->config_methods);
1048 wpa_printf(MSG_DEBUG, "WPS: wps_cb_set_sel_reg: sel_reg=%d "
1049 "config_methods=0x%x pbc=%d methods=0x%x",
1050 reg->selected_registrar, reg->wps->config_methods,
1053 reg->set_sel_reg_cb(reg->cb_ctx, reg->selected_registrar,
1054 reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT,
1059 static int wps_set_ie(struct wps_registrar *reg)
1061 struct wpabuf *beacon;
1062 struct wpabuf *probe;
1063 const u8 *auth_macs;
1066 if (reg->set_ie_cb == NULL)
1069 beacon = wpabuf_alloc(400);
1072 probe = wpabuf_alloc(500);
1073 if (probe == NULL) {
1074 wpabuf_free(beacon);
1078 auth_macs = wps_authorized_macs(reg, &count);
1080 wpa_printf(MSG_DEBUG, "WPS: Build Beacon IEs");
1082 if (wps_build_version(beacon) ||
1083 wps_build_wps_state(reg->wps, beacon) ||
1084 wps_build_ap_setup_locked(reg->wps, beacon) ||
1085 wps_build_selected_registrar(reg, beacon) ||
1086 wps_build_sel_reg_dev_password_id(reg, beacon) ||
1087 wps_build_sel_reg_config_methods(reg, beacon) ||
1088 wps_build_sel_pbc_reg_uuid_e(reg, beacon) ||
1089 (reg->dualband && wps_build_rf_bands(®->wps->dev, beacon)) ||
1090 wps_build_wfa_ext(beacon, 0, auth_macs, count)) {
1091 wpabuf_free(beacon);
1096 wpa_printf(MSG_DEBUG, "WPS: Build Probe Response IEs");
1098 if (wps_build_version(probe) ||
1099 wps_build_wps_state(reg->wps, probe) ||
1100 wps_build_ap_setup_locked(reg->wps, probe) ||
1101 wps_build_selected_registrar(reg, probe) ||
1102 wps_build_sel_reg_dev_password_id(reg, probe) ||
1103 wps_build_sel_reg_config_methods(reg, probe) ||
1104 wps_build_resp_type(probe, reg->wps->ap ? WPS_RESP_AP :
1105 WPS_RESP_REGISTRAR) ||
1106 wps_build_uuid_e(probe, reg->wps->uuid) ||
1107 wps_build_device_attrs(®->wps->dev, probe) ||
1108 wps_build_probe_config_methods(reg, probe) ||
1109 wps_build_rf_bands(®->wps->dev, probe) ||
1110 wps_build_wfa_ext(probe, 0, auth_macs, count)) {
1111 wpabuf_free(beacon);
1117 if (wps_build_dev_name(®->wps->dev, beacon) ||
1118 wps_build_primary_dev_type(®->wps->dev, beacon)) {
1119 wpabuf_free(beacon);
1123 #endif /* CONFIG_P2P */
1125 beacon = wps_ie_encapsulate(beacon);
1126 probe = wps_ie_encapsulate(probe);
1128 if (!beacon || !probe) {
1129 wpabuf_free(beacon);
1134 if (reg->static_wep_only) {
1136 * Windows XP and Vista clients can get confused about
1137 * EAP-Identity/Request when they probe the network with
1138 * EAPOL-Start. In such a case, they may assume the network is
1139 * using IEEE 802.1X and prompt user for a certificate while
1140 * the correct (non-WPS) behavior would be to ask for the
1141 * static WEP key. As a workaround, use Microsoft Provisioning
1142 * IE to advertise that legacy 802.1X is not supported.
1144 const u8 ms_wps[7] = {
1145 WLAN_EID_VENDOR_SPECIFIC, 5,
1146 /* Microsoft Provisioning IE (00:50:f2:5) */
1147 0x00, 0x50, 0xf2, 5,
1148 0x00 /* no legacy 802.1X or MS WPS */
1150 wpa_printf(MSG_DEBUG, "WPS: Add Microsoft Provisioning IE "
1151 "into Beacon/Probe Response frames");
1152 wpabuf_put_data(beacon, ms_wps, sizeof(ms_wps));
1153 wpabuf_put_data(probe, ms_wps, sizeof(ms_wps));
1156 return wps_cb_set_ie(reg, beacon, probe);
1160 static int wps_get_dev_password(struct wps_data *wps)
1165 os_free(wps->dev_password);
1166 wps->dev_password = NULL;
1169 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
1170 pin = (const u8 *) "00000000";
1173 pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
1177 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
1179 wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
1184 wps->dev_password = os_malloc(pin_len);
1185 if (wps->dev_password == NULL)
1187 os_memcpy(wps->dev_password, pin, pin_len);
1188 wps->dev_password_len = pin_len;
1194 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
1196 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
1197 wpabuf_put_be16(msg, ATTR_UUID_R);
1198 wpabuf_put_be16(msg, WPS_UUID_LEN);
1199 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
1204 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
1210 if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
1212 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
1213 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
1214 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
1216 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
1217 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
1218 "R-Hash derivation");
1222 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
1223 wpabuf_put_be16(msg, ATTR_R_HASH1);
1224 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1225 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1226 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
1227 addr[0] = wps->snonce;
1228 len[0] = WPS_SECRET_NONCE_LEN;
1229 addr[1] = wps->psk1;
1230 len[1] = WPS_PSK_LEN;
1231 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1232 len[2] = wpabuf_len(wps->dh_pubkey_e);
1233 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1234 len[3] = wpabuf_len(wps->dh_pubkey_r);
1235 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1236 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
1238 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
1239 wpabuf_put_be16(msg, ATTR_R_HASH2);
1240 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1241 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1242 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
1243 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
1244 addr[1] = wps->psk2;
1245 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1246 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
1252 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
1254 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
1255 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
1256 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1257 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
1262 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
1264 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
1265 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
1266 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1267 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
1268 WPS_SECRET_NONCE_LEN);
1273 static int wps_build_cred_network_idx(struct wpabuf *msg,
1274 const struct wps_credential *cred)
1276 wpa_printf(MSG_DEBUG, "WPS: * Network Index");
1277 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
1278 wpabuf_put_be16(msg, 1);
1279 wpabuf_put_u8(msg, 1);
1284 static int wps_build_cred_ssid(struct wpabuf *msg,
1285 const struct wps_credential *cred)
1287 wpa_printf(MSG_DEBUG, "WPS: * SSID");
1288 wpabuf_put_be16(msg, ATTR_SSID);
1289 wpabuf_put_be16(msg, cred->ssid_len);
1290 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
1295 static int wps_build_cred_auth_type(struct wpabuf *msg,
1296 const struct wps_credential *cred)
1298 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
1300 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
1301 wpabuf_put_be16(msg, 2);
1302 wpabuf_put_be16(msg, cred->auth_type);
1307 static int wps_build_cred_encr_type(struct wpabuf *msg,
1308 const struct wps_credential *cred)
1310 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
1312 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
1313 wpabuf_put_be16(msg, 2);
1314 wpabuf_put_be16(msg, cred->encr_type);
1319 static int wps_build_cred_network_key(struct wpabuf *msg,
1320 const struct wps_credential *cred)
1322 wpa_printf(MSG_DEBUG, "WPS: * Network Key (len=%d)",
1323 (int) cred->key_len);
1324 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
1325 wpabuf_put_be16(msg, cred->key_len);
1326 wpabuf_put_data(msg, cred->key, cred->key_len);
1331 static int wps_build_cred_mac_addr(struct wpabuf *msg,
1332 const struct wps_credential *cred)
1334 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (" MACSTR ")",
1335 MAC2STR(cred->mac_addr));
1336 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
1337 wpabuf_put_be16(msg, ETH_ALEN);
1338 wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
1343 static int wps_build_credential(struct wpabuf *msg,
1344 const struct wps_credential *cred)
1346 if (wps_build_cred_network_idx(msg, cred) ||
1347 wps_build_cred_ssid(msg, cred) ||
1348 wps_build_cred_auth_type(msg, cred) ||
1349 wps_build_cred_encr_type(msg, cred) ||
1350 wps_build_cred_network_key(msg, cred) ||
1351 wps_build_cred_mac_addr(msg, cred))
1357 int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
1359 struct wpabuf *cred;
1361 if (wps->wps->registrar->skip_cred_build)
1362 goto skip_cred_build;
1364 wpa_printf(MSG_DEBUG, "WPS: * Credential");
1365 if (wps->use_cred) {
1366 os_memcpy(&wps->cred, wps->use_cred, sizeof(wps->cred));
1369 os_memset(&wps->cred, 0, sizeof(wps->cred));
1371 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
1372 wps->cred.ssid_len = wps->wps->ssid_len;
1374 /* Select the best authentication and encryption type */
1375 if (wps->auth_type & WPS_AUTH_WPA2PSK)
1376 wps->auth_type = WPS_AUTH_WPA2PSK;
1377 else if (wps->auth_type & WPS_AUTH_WPAPSK)
1378 wps->auth_type = WPS_AUTH_WPAPSK;
1379 else if (wps->auth_type & WPS_AUTH_OPEN)
1380 wps->auth_type = WPS_AUTH_OPEN;
1381 else if (wps->auth_type & WPS_AUTH_SHARED)
1382 wps->auth_type = WPS_AUTH_SHARED;
1384 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
1388 wps->cred.auth_type = wps->auth_type;
1390 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
1391 wps->auth_type == WPS_AUTH_WPAPSK) {
1392 if (wps->encr_type & WPS_ENCR_AES)
1393 wps->encr_type = WPS_ENCR_AES;
1394 else if (wps->encr_type & WPS_ENCR_TKIP)
1395 wps->encr_type = WPS_ENCR_TKIP;
1397 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1398 "type for WPA/WPA2");
1402 if (wps->encr_type & WPS_ENCR_WEP)
1403 wps->encr_type = WPS_ENCR_WEP;
1404 else if (wps->encr_type & WPS_ENCR_NONE)
1405 wps->encr_type = WPS_ENCR_NONE;
1407 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1408 "type for non-WPA/WPA2 mode");
1412 wps->cred.encr_type = wps->encr_type;
1414 * Set MAC address in the Credential to be the Enrollee's MAC address
1416 os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
1418 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
1419 !wps->wps->registrar->disable_auto_conf) {
1421 /* Generate a random passphrase */
1422 if (os_get_random(r, sizeof(r)) < 0)
1424 os_free(wps->new_psk);
1425 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
1426 if (wps->new_psk == NULL)
1428 wps->new_psk_len--; /* remove newline */
1429 while (wps->new_psk_len &&
1430 wps->new_psk[wps->new_psk_len - 1] == '=')
1432 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
1433 wps->new_psk, wps->new_psk_len);
1434 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
1435 wps->cred.key_len = wps->new_psk_len;
1436 } else if (wps->use_psk_key && wps->wps->psk_set) {
1438 wpa_printf(MSG_DEBUG, "WPS: Use PSK format for Network Key");
1439 wpa_snprintf_hex(hex, sizeof(hex), wps->wps->psk, 32);
1440 os_memcpy(wps->cred.key, hex, 32 * 2);
1441 wps->cred.key_len = 32 * 2;
1442 } else if (wps->wps->network_key) {
1443 os_memcpy(wps->cred.key, wps->wps->network_key,
1444 wps->wps->network_key_len);
1445 wps->cred.key_len = wps->wps->network_key_len;
1446 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
1448 /* Generate a random per-device PSK */
1449 os_free(wps->new_psk);
1450 wps->new_psk_len = 32;
1451 wps->new_psk = os_malloc(wps->new_psk_len);
1452 if (wps->new_psk == NULL)
1454 if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
1455 os_free(wps->new_psk);
1456 wps->new_psk = NULL;
1459 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
1460 wps->new_psk, wps->new_psk_len);
1461 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
1463 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
1464 wps->cred.key_len = wps->new_psk_len * 2;
1468 #ifdef CONFIG_WPS_TESTING_EXTRA_CRED
1469 cred = wpabuf_alloc(200);
1471 struct wps_credential dummy;
1472 wpa_printf(MSG_DEBUG, "WPS: Add dummy credential");
1473 os_memset(&dummy, 0, sizeof(dummy));
1474 os_memcpy(dummy.ssid, "dummy", 5);
1476 dummy.auth_type = WPS_AUTH_WPA2PSK;
1477 dummy.encr_type = WPS_ENCR_AES;
1478 os_memcpy(dummy.key, "dummy psk", 9);
1480 os_memcpy(dummy.mac_addr, wps->mac_addr_e, ETH_ALEN);
1481 wps_build_credential(cred, &dummy);
1482 wpa_hexdump_buf(MSG_DEBUG, "WPS: Dummy Credential", cred);
1484 wpabuf_put_be16(msg, ATTR_CRED);
1485 wpabuf_put_be16(msg, wpabuf_len(cred));
1486 wpabuf_put_buf(msg, cred);
1490 #endif /* CONFIG_WPS_TESTING_EXTRA_CRED */
1492 cred = wpabuf_alloc(200);
1496 if (wps_build_credential(cred, &wps->cred)) {
1501 wpabuf_put_be16(msg, ATTR_CRED);
1502 wpabuf_put_be16(msg, wpabuf_len(cred));
1503 wpabuf_put_buf(msg, cred);
1507 if (wps->wps->registrar->extra_cred) {
1508 wpa_printf(MSG_DEBUG, "WPS: * Credential (pre-configured)");
1509 wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
1516 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
1518 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
1520 if (wps_build_credential(msg, &wps->cred))
1527 static struct wpabuf * wps_build_m2(struct wps_data *wps)
1531 if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
1533 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
1534 wps->nonce_r, WPS_NONCE_LEN);
1535 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1537 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1538 msg = wpabuf_alloc(1000);
1542 if (wps_build_version(msg) ||
1543 wps_build_msg_type(msg, WPS_M2) ||
1544 wps_build_enrollee_nonce(wps, msg) ||
1545 wps_build_registrar_nonce(wps, msg) ||
1546 wps_build_uuid_r(wps, msg) ||
1547 wps_build_public_key(wps, msg) ||
1548 wps_derive_keys(wps) ||
1549 wps_build_auth_type_flags(wps, msg) ||
1550 wps_build_encr_type_flags(wps, msg) ||
1551 wps_build_conn_type_flags(wps, msg) ||
1552 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1553 wps_build_device_attrs(&wps->wps->dev, msg) ||
1554 wps_build_rf_bands(&wps->wps->dev, msg) ||
1555 wps_build_assoc_state(wps, msg) ||
1556 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1557 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
1558 wps_build_os_version(&wps->wps->dev, msg) ||
1559 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1560 wps_build_authenticator(wps, msg)) {
1566 wps->state = RECV_M3;
1571 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1574 u16 err = wps->config_error;
1576 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1577 msg = wpabuf_alloc(1000);
1581 if (wps->wps->ap && wps->wps->ap_setup_locked &&
1582 err == WPS_CFG_NO_ERROR)
1583 err = WPS_CFG_SETUP_LOCKED;
1585 if (wps_build_version(msg) ||
1586 wps_build_msg_type(msg, WPS_M2D) ||
1587 wps_build_enrollee_nonce(wps, msg) ||
1588 wps_build_registrar_nonce(wps, msg) ||
1589 wps_build_uuid_r(wps, msg) ||
1590 wps_build_auth_type_flags(wps, msg) ||
1591 wps_build_encr_type_flags(wps, msg) ||
1592 wps_build_conn_type_flags(wps, msg) ||
1593 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1594 wps_build_device_attrs(&wps->wps->dev, msg) ||
1595 wps_build_rf_bands(&wps->wps->dev, msg) ||
1596 wps_build_assoc_state(wps, msg) ||
1597 wps_build_config_error(msg, err) ||
1598 wps_build_os_version(&wps->wps->dev, msg) ||
1599 wps_build_wfa_ext(msg, 0, NULL, 0)) {
1604 wps->state = RECV_M2D_ACK;
1609 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1611 struct wpabuf *msg, *plain;
1613 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1615 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1617 plain = wpabuf_alloc(200);
1621 msg = wpabuf_alloc(1000);
1627 if (wps_build_version(msg) ||
1628 wps_build_msg_type(msg, WPS_M4) ||
1629 wps_build_enrollee_nonce(wps, msg) ||
1630 wps_build_r_hash(wps, msg) ||
1631 wps_build_r_snonce1(wps, plain) ||
1632 wps_build_key_wrap_auth(wps, plain) ||
1633 wps_build_encr_settings(wps, msg, plain) ||
1634 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1635 wps_build_authenticator(wps, msg)) {
1642 wps->state = RECV_M5;
1647 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1649 struct wpabuf *msg, *plain;
1651 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1653 plain = wpabuf_alloc(200);
1657 msg = wpabuf_alloc(1000);
1663 if (wps_build_version(msg) ||
1664 wps_build_msg_type(msg, WPS_M6) ||
1665 wps_build_enrollee_nonce(wps, msg) ||
1666 wps_build_r_snonce2(wps, plain) ||
1667 wps_build_key_wrap_auth(wps, plain) ||
1668 wps_build_encr_settings(wps, msg, plain) ||
1669 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1670 wps_build_authenticator(wps, msg)) {
1677 wps->wps_pin_revealed = 1;
1678 wps->state = RECV_M7;
1683 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1685 struct wpabuf *msg, *plain;
1687 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1689 plain = wpabuf_alloc(500);
1693 msg = wpabuf_alloc(1000);
1699 if (wps_build_version(msg) ||
1700 wps_build_msg_type(msg, WPS_M8) ||
1701 wps_build_enrollee_nonce(wps, msg) ||
1702 ((wps->wps->ap || wps->er) && wps_build_cred(wps, plain)) ||
1703 (!wps->wps->ap && !wps->er && wps_build_ap_settings(wps, plain)) ||
1704 wps_build_key_wrap_auth(wps, plain) ||
1705 wps_build_encr_settings(wps, msg, plain) ||
1706 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1707 wps_build_authenticator(wps, msg)) {
1714 wps->state = RECV_DONE;
1719 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
1723 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
1725 msg = wpabuf_alloc(1000);
1729 if (wps_build_version(msg) ||
1730 wps_build_msg_type(msg, WPS_WSC_ACK) ||
1731 wps_build_enrollee_nonce(wps, msg) ||
1732 wps_build_registrar_nonce(wps, msg) ||
1733 wps_build_wfa_ext(msg, 0, NULL, 0)) {
1742 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
1746 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
1748 msg = wpabuf_alloc(1000);
1752 if (wps_build_version(msg) ||
1753 wps_build_msg_type(msg, WPS_WSC_NACK) ||
1754 wps_build_enrollee_nonce(wps, msg) ||
1755 wps_build_registrar_nonce(wps, msg) ||
1756 wps_build_config_error(msg, wps->config_error) ||
1757 wps_build_wfa_ext(msg, 0, NULL, 0)) {
1766 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1767 enum wsc_op_code *op_code)
1771 #ifdef CONFIG_WPS_UPNP
1772 if (!wps->int_reg && wps->wps->wps_upnp) {
1773 struct upnp_pending_message *p, *prev = NULL;
1774 if (wps->ext_reg > 1)
1775 wps_registrar_free_pending_m2(wps->wps);
1776 p = wps->wps->upnp_msgs;
1777 /* TODO: check pending message MAC address */
1778 while (p && p->next) {
1783 wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
1788 wps->wps->upnp_msgs = NULL;
1795 *op_code = WSC_NACK;
1802 if (wps->ext_reg == 0)
1808 wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
1809 "pending message available");
1812 #endif /* CONFIG_WPS_UPNP */
1814 switch (wps->state) {
1816 if (wps_get_dev_password(wps) < 0)
1817 msg = wps_build_m2d(wps);
1819 msg = wps_build_m2(wps);
1823 msg = wps_build_m2d(wps);
1827 msg = wps_build_m4(wps);
1831 msg = wps_build_m6(wps);
1835 msg = wps_build_m8(wps);
1839 msg = wps_build_wsc_ack(wps);
1843 msg = wps_build_wsc_nack(wps);
1844 *op_code = WSC_NACK;
1847 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
1848 "a message", wps->state);
1853 if (*op_code == WSC_MSG && msg) {
1854 /* Save a copy of the last message for Authenticator derivation
1856 wpabuf_free(wps->last_msg);
1857 wps->last_msg = wpabuf_dup(msg);
1864 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
1866 if (e_nonce == NULL) {
1867 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
1871 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
1872 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
1873 wps->nonce_e, WPS_NONCE_LEN);
1879 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
1881 if (r_nonce == NULL) {
1882 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
1886 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
1887 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
1895 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
1897 if (uuid_e == NULL) {
1898 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
1902 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
1903 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
1909 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
1911 if (pw_id == NULL) {
1912 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
1916 wps->dev_pw_id = WPA_GET_BE16(pw_id);
1917 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
1923 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
1925 if (e_hash1 == NULL) {
1926 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
1930 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
1931 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
1937 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
1939 if (e_hash2 == NULL) {
1940 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
1944 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
1945 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
1951 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
1953 u8 hash[SHA256_MAC_LEN];
1957 if (e_snonce1 == NULL) {
1958 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
1962 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
1963 WPS_SECRET_NONCE_LEN);
1965 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
1966 addr[0] = e_snonce1;
1967 len[0] = WPS_SECRET_NONCE_LEN;
1968 addr[1] = wps->psk1;
1969 len[1] = WPS_PSK_LEN;
1970 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1971 len[2] = wpabuf_len(wps->dh_pubkey_e);
1972 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1973 len[3] = wpabuf_len(wps->dh_pubkey_r);
1974 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1976 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
1977 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
1978 "not match with the pre-committed value");
1979 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1980 wps_pwd_auth_fail_event(wps->wps, 0, 1);
1984 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
1985 "half of the device password");
1991 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
1993 u8 hash[SHA256_MAC_LEN];
1997 if (e_snonce2 == NULL) {
1998 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
2002 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
2003 WPS_SECRET_NONCE_LEN);
2005 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
2006 addr[0] = e_snonce2;
2007 len[0] = WPS_SECRET_NONCE_LEN;
2008 addr[1] = wps->psk2;
2009 len[1] = WPS_PSK_LEN;
2010 addr[2] = wpabuf_head(wps->dh_pubkey_e);
2011 len[2] = wpabuf_len(wps->dh_pubkey_e);
2012 addr[3] = wpabuf_head(wps->dh_pubkey_r);
2013 len[3] = wpabuf_len(wps->dh_pubkey_r);
2014 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
2016 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
2017 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
2018 "not match with the pre-committed value");
2019 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
2020 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
2021 wps_pwd_auth_fail_event(wps->wps, 0, 2);
2025 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
2026 "half of the device password");
2027 wps->wps_pin_revealed = 0;
2028 wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
2034 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
2036 if (mac_addr == NULL) {
2037 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
2041 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
2043 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
2044 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
2050 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
2053 if (pk == NULL || pk_len == 0) {
2054 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
2058 #ifdef CONFIG_WPS_OOB
2059 if (wps->wps->oob_conf.pubkey_hash != NULL) {
2061 u8 hash[WPS_HASH_LEN];
2064 sha256_vector(1, addr, &pk_len, hash);
2066 wpabuf_head(wps->wps->oob_conf.pubkey_hash),
2067 WPS_OOB_PUBKEY_HASH_LEN) != 0) {
2068 wpa_printf(MSG_ERROR, "WPS: Public Key hash error");
2072 #endif /* CONFIG_WPS_OOB */
2074 wpabuf_free(wps->dh_pubkey_e);
2075 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
2076 if (wps->dh_pubkey_e == NULL)
2083 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
2088 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
2093 auth_types = WPA_GET_BE16(auth);
2095 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
2097 wps->auth_type = wps->wps->auth_types & auth_types;
2098 if (wps->auth_type == 0) {
2099 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
2100 "authentication types (own 0x%x Enrollee 0x%x)",
2101 wps->wps->auth_types, auth_types);
2102 #ifdef WPS_WORKAROUNDS
2104 * Some deployed implementations seem to advertise incorrect
2105 * information in this attribute. For example, Linksys WRT350N
2106 * seems to have a byteorder bug that breaks this negotiation.
2107 * In order to interoperate with existing implementations,
2108 * assume that the Enrollee supports everything we do.
2110 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
2111 "does not advertise supported authentication types "
2113 wps->auth_type = wps->wps->auth_types;
2114 #else /* WPS_WORKAROUNDS */
2116 #endif /* WPS_WORKAROUNDS */
2123 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
2128 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
2133 encr_types = WPA_GET_BE16(encr);
2135 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
2137 wps->encr_type = wps->wps->encr_types & encr_types;
2138 if (wps->encr_type == 0) {
2139 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
2140 "encryption types (own 0x%x Enrollee 0x%x)",
2141 wps->wps->encr_types, encr_types);
2142 #ifdef WPS_WORKAROUNDS
2144 * Some deployed implementations seem to advertise incorrect
2145 * information in this attribute. For example, Linksys WRT350N
2146 * seems to have a byteorder bug that breaks this negotiation.
2147 * In order to interoperate with existing implementations,
2148 * assume that the Enrollee supports everything we do.
2150 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
2151 "does not advertise supported encryption types "
2153 wps->encr_type = wps->wps->encr_types;
2154 #else /* WPS_WORKAROUNDS */
2156 #endif /* WPS_WORKAROUNDS */
2163 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
2166 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
2171 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
2178 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
2182 if (methods == NULL) {
2183 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
2187 m = WPA_GET_BE16(methods);
2189 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x"
2190 "%s%s%s%s%s%s%s%s%s", m,
2191 m & WPS_CONFIG_USBA ? " [USBA]" : "",
2192 m & WPS_CONFIG_ETHERNET ? " [Ethernet]" : "",
2193 m & WPS_CONFIG_LABEL ? " [Label]" : "",
2194 m & WPS_CONFIG_DISPLAY ? " [Display]" : "",
2195 m & WPS_CONFIG_EXT_NFC_TOKEN ? " [Ext NFC Token]" : "",
2196 m & WPS_CONFIG_INT_NFC_TOKEN ? " [Int NFC Token]" : "",
2197 m & WPS_CONFIG_NFC_INTERFACE ? " [NFC]" : "",
2198 m & WPS_CONFIG_PUSHBUTTON ? " [PBC]" : "",
2199 m & WPS_CONFIG_KEYPAD ? " [Keypad]" : "");
2201 if (!(m & WPS_CONFIG_DISPLAY) && !wps->use_psk_key) {
2203 * The Enrollee does not have a display so it is unlikely to be
2204 * able to show the passphrase to a user and as such, could
2205 * benefit from receiving PSK to reduce key derivation time.
2207 wpa_printf(MSG_DEBUG, "WPS: Prefer PSK format key due to "
2208 "Enrollee not supporting display");
2209 wps->use_psk_key = 1;
2216 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
2218 if (state == NULL) {
2219 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
2224 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
2231 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
2235 if (assoc == NULL) {
2236 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
2240 a = WPA_GET_BE16(assoc);
2241 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
2247 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
2252 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
2256 e = WPA_GET_BE16(err);
2257 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
2263 static enum wps_process_res wps_process_m1(struct wps_data *wps,
2264 struct wps_parse_attr *attr)
2266 wpa_printf(MSG_DEBUG, "WPS: Received M1");
2268 if (wps->state != RECV_M1) {
2269 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2270 "receiving M1", wps->state);
2274 if (wps_process_uuid_e(wps, attr->uuid_e) ||
2275 wps_process_mac_addr(wps, attr->mac_addr) ||
2276 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
2277 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
2278 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
2279 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
2280 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
2281 wps_process_config_methods(wps, attr->config_methods) ||
2282 wps_process_wps_state(wps, attr->wps_state) ||
2283 wps_process_device_attrs(&wps->peer_dev, attr) ||
2284 wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
2285 wps_process_assoc_state(wps, attr->assoc_state) ||
2286 wps_process_dev_password_id(wps, attr->dev_password_id) ||
2287 wps_process_config_error(wps, attr->config_error) ||
2288 wps_process_os_version(&wps->peer_dev, attr->os_version))
2291 if (wps->dev_pw_id < 0x10 &&
2292 wps->dev_pw_id != DEV_PW_DEFAULT &&
2293 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
2294 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
2295 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
2296 (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
2297 !wps->wps->registrar->pbc)) {
2298 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
2300 wps->state = SEND_M2D;
2301 return WPS_CONTINUE;
2304 #ifdef CONFIG_WPS_OOB
2305 if (wps->dev_pw_id >= 0x10 &&
2306 wps->dev_pw_id != wps->wps->oob_dev_pw_id) {
2307 wpa_printf(MSG_DEBUG, "WPS: OOB Device Password ID "
2308 "%d mismatch", wps->dev_pw_id);
2309 wps->state = SEND_M2D;
2310 return WPS_CONTINUE;
2312 #endif /* CONFIG_WPS_OOB */
2314 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
2315 if (wps->wps->registrar->force_pbc_overlap ||
2316 wps_registrar_pbc_overlap(wps->wps->registrar,
2317 wps->mac_addr_e, wps->uuid_e)) {
2318 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
2320 wps->state = SEND_M2D;
2321 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2322 wps_pbc_overlap_event(wps->wps);
2323 wps->wps->registrar->force_pbc_overlap = 1;
2324 return WPS_CONTINUE;
2326 wps_registrar_add_pbc_session(wps->wps->registrar,
2327 wps->mac_addr_e, wps->uuid_e);
2331 #ifdef WPS_WORKAROUNDS
2333 * It looks like Mac OS X 10.6.3 and 10.6.4 do not like Network Key in
2334 * passphrase format. To avoid interop issues, force PSK format to be
2337 if (!wps->use_psk_key &&
2338 wps->peer_dev.manufacturer &&
2339 os_strncmp(wps->peer_dev.manufacturer, "Apple ", 6) == 0 &&
2340 wps->peer_dev.model_name &&
2341 os_strcmp(wps->peer_dev.model_name, "AirPort") == 0) {
2342 wpa_printf(MSG_DEBUG, "WPS: Workaround - Force Network Key in "
2344 wps->use_psk_key = 1;
2346 #endif /* WPS_WORKAROUNDS */
2348 wps->state = SEND_M2;
2349 return WPS_CONTINUE;
2353 static enum wps_process_res wps_process_m3(struct wps_data *wps,
2354 const struct wpabuf *msg,
2355 struct wps_parse_attr *attr)
2357 wpa_printf(MSG_DEBUG, "WPS: Received M3");
2359 if (wps->state != RECV_M3) {
2360 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2361 "receiving M3", wps->state);
2362 wps->state = SEND_WSC_NACK;
2363 return WPS_CONTINUE;
2366 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
2367 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2369 wps->state = SEND_WSC_NACK;
2370 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2371 return WPS_CONTINUE;
2374 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2375 wps_process_authenticator(wps, attr->authenticator, msg) ||
2376 wps_process_e_hash1(wps, attr->e_hash1) ||
2377 wps_process_e_hash2(wps, attr->e_hash2)) {
2378 wps->state = SEND_WSC_NACK;
2379 return WPS_CONTINUE;
2382 wps->state = SEND_M4;
2383 return WPS_CONTINUE;
2387 static enum wps_process_res wps_process_m5(struct wps_data *wps,
2388 const struct wpabuf *msg,
2389 struct wps_parse_attr *attr)
2391 struct wpabuf *decrypted;
2392 struct wps_parse_attr eattr;
2394 wpa_printf(MSG_DEBUG, "WPS: Received M5");
2396 if (wps->state != RECV_M5) {
2397 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2398 "receiving M5", wps->state);
2399 wps->state = SEND_WSC_NACK;
2400 return WPS_CONTINUE;
2403 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
2404 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2406 wps->state = SEND_WSC_NACK;
2407 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2408 return WPS_CONTINUE;
2411 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2412 wps_process_authenticator(wps, attr->authenticator, msg)) {
2413 wps->state = SEND_WSC_NACK;
2414 return WPS_CONTINUE;
2417 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2418 attr->encr_settings_len);
2419 if (decrypted == NULL) {
2420 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
2421 "Settings attribute");
2422 wps->state = SEND_WSC_NACK;
2423 return WPS_CONTINUE;
2426 if (wps_validate_m5_encr(decrypted) < 0) {
2427 wpabuf_free(decrypted);
2428 wps->state = SEND_WSC_NACK;
2429 return WPS_CONTINUE;
2432 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2434 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2435 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2436 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
2437 wpabuf_free(decrypted);
2438 wps->state = SEND_WSC_NACK;
2439 return WPS_CONTINUE;
2441 wpabuf_free(decrypted);
2443 wps->state = SEND_M6;
2444 return WPS_CONTINUE;
2448 static void wps_sta_cred_cb(struct wps_data *wps)
2451 * Update credential to only include a single authentication and
2452 * encryption type in case the AP configuration includes more than one
2455 if (wps->cred.auth_type & WPS_AUTH_WPA2PSK)
2456 wps->cred.auth_type = WPS_AUTH_WPA2PSK;
2457 else if (wps->cred.auth_type & WPS_AUTH_WPAPSK)
2458 wps->cred.auth_type = WPS_AUTH_WPAPSK;
2459 if (wps->cred.encr_type & WPS_ENCR_AES)
2460 wps->cred.encr_type = WPS_ENCR_AES;
2461 else if (wps->cred.encr_type & WPS_ENCR_TKIP)
2462 wps->cred.encr_type = WPS_ENCR_TKIP;
2463 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based on the "
2464 "AP configuration");
2465 if (wps->wps->cred_cb)
2466 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2470 static void wps_cred_update(struct wps_credential *dst,
2471 struct wps_credential *src)
2473 os_memcpy(dst->ssid, src->ssid, sizeof(dst->ssid));
2474 dst->ssid_len = src->ssid_len;
2475 dst->auth_type = src->auth_type;
2476 dst->encr_type = src->encr_type;
2477 dst->key_idx = src->key_idx;
2478 os_memcpy(dst->key, src->key, sizeof(dst->key));
2479 dst->key_len = src->key_len;
2483 static int wps_process_ap_settings_r(struct wps_data *wps,
2484 struct wps_parse_attr *attr)
2486 if (wps->wps->ap || wps->er)
2489 /* AP Settings Attributes in M7 when Enrollee is an AP */
2490 if (wps_process_ap_settings(attr, &wps->cred) < 0)
2493 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
2495 if (wps->new_ap_settings) {
2496 wpa_printf(MSG_INFO, "WPS: Update AP configuration based on "
2498 wps_cred_update(&wps->cred, wps->new_ap_settings);
2502 * Use the AP PIN only to receive the current AP settings, not
2503 * to reconfigure the AP.
2505 if (wps->ap_settings_cb) {
2506 wps->ap_settings_cb(wps->ap_settings_cb_ctx,
2510 wps_sta_cred_cb(wps);
2516 static enum wps_process_res wps_process_m7(struct wps_data *wps,
2517 const struct wpabuf *msg,
2518 struct wps_parse_attr *attr)
2520 struct wpabuf *decrypted;
2521 struct wps_parse_attr eattr;
2523 wpa_printf(MSG_DEBUG, "WPS: Received M7");
2525 if (wps->state != RECV_M7) {
2526 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2527 "receiving M7", wps->state);
2528 wps->state = SEND_WSC_NACK;
2529 return WPS_CONTINUE;
2532 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
2533 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2535 wps->state = SEND_WSC_NACK;
2536 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2537 return WPS_CONTINUE;
2540 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2541 wps_process_authenticator(wps, attr->authenticator, msg)) {
2542 wps->state = SEND_WSC_NACK;
2543 return WPS_CONTINUE;
2546 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2547 attr->encr_settings_len);
2548 if (decrypted == NULL) {
2549 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypt Encrypted "
2550 "Settings attribute");
2551 wps->state = SEND_WSC_NACK;
2552 return WPS_CONTINUE;
2555 if (wps_validate_m7_encr(decrypted, wps->wps->ap || wps->er) < 0) {
2556 wpabuf_free(decrypted);
2557 wps->state = SEND_WSC_NACK;
2558 return WPS_CONTINUE;
2561 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2563 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2564 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2565 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
2566 wps_process_ap_settings_r(wps, &eattr)) {
2567 wpabuf_free(decrypted);
2568 wps->state = SEND_WSC_NACK;
2569 return WPS_CONTINUE;
2572 wpabuf_free(decrypted);
2574 wps->state = SEND_M8;
2575 return WPS_CONTINUE;
2579 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
2580 const struct wpabuf *msg)
2582 struct wps_parse_attr attr;
2583 enum wps_process_res ret = WPS_CONTINUE;
2585 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
2587 if (wps_parse_msg(msg, &attr) < 0)
2590 if (attr.msg_type == NULL) {
2591 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2595 if (*attr.msg_type != WPS_M1 &&
2596 (attr.registrar_nonce == NULL ||
2597 os_memcmp(wps->nonce_r, attr.registrar_nonce,
2598 WPS_NONCE_LEN != 0))) {
2599 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2603 switch (*attr.msg_type) {
2605 if (wps_validate_m1(msg) < 0)
2607 #ifdef CONFIG_WPS_UPNP
2608 if (wps->wps->wps_upnp && attr.mac_addr) {
2609 /* Remove old pending messages when starting new run */
2610 wps_free_pending_msgs(wps->wps->upnp_msgs);
2611 wps->wps->upnp_msgs = NULL;
2613 upnp_wps_device_send_wlan_event(
2614 wps->wps->wps_upnp, attr.mac_addr,
2615 UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
2617 #endif /* CONFIG_WPS_UPNP */
2618 ret = wps_process_m1(wps, &attr);
2621 if (wps_validate_m3(msg) < 0)
2623 ret = wps_process_m3(wps, msg, &attr);
2624 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2625 wps_fail_event(wps->wps, WPS_M3);
2628 if (wps_validate_m5(msg) < 0)
2630 ret = wps_process_m5(wps, msg, &attr);
2631 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2632 wps_fail_event(wps->wps, WPS_M5);
2635 if (wps_validate_m7(msg) < 0)
2637 ret = wps_process_m7(wps, msg, &attr);
2638 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2639 wps_fail_event(wps->wps, WPS_M7);
2642 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
2647 if (ret == WPS_CONTINUE) {
2648 /* Save a copy of the last message for Authenticator derivation
2650 wpabuf_free(wps->last_msg);
2651 wps->last_msg = wpabuf_dup(msg);
2658 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
2659 const struct wpabuf *msg)
2661 struct wps_parse_attr attr;
2663 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
2665 if (wps_parse_msg(msg, &attr) < 0)
2668 if (attr.msg_type == NULL) {
2669 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2673 if (*attr.msg_type != WPS_WSC_ACK) {
2674 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2679 #ifdef CONFIG_WPS_UPNP
2680 if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
2681 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2682 if (wps->wps->upnp_msgs)
2683 return WPS_CONTINUE;
2684 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2685 "external Registrar");
2688 #endif /* CONFIG_WPS_UPNP */
2690 if (attr.registrar_nonce == NULL ||
2691 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2693 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2697 if (attr.enrollee_nonce == NULL ||
2698 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2699 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2703 if (wps->state == RECV_M2D_ACK) {
2704 #ifdef CONFIG_WPS_UPNP
2705 if (wps->wps->wps_upnp &&
2706 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2707 if (wps->wps->upnp_msgs)
2708 return WPS_CONTINUE;
2709 if (wps->ext_reg == 0)
2711 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2712 "external Registrar");
2715 #endif /* CONFIG_WPS_UPNP */
2717 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
2718 "terminate negotiation");
2725 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
2726 const struct wpabuf *msg)
2728 struct wps_parse_attr attr;
2731 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
2733 old_state = wps->state;
2734 wps->state = SEND_WSC_NACK;
2736 if (wps_parse_msg(msg, &attr) < 0)
2739 if (attr.msg_type == NULL) {
2740 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2744 if (*attr.msg_type != WPS_WSC_NACK) {
2745 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2750 #ifdef CONFIG_WPS_UPNP
2751 if (wps->wps->wps_upnp && wps->ext_reg) {
2752 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2753 "Registrar terminated by the Enrollee");
2756 #endif /* CONFIG_WPS_UPNP */
2758 if (attr.registrar_nonce == NULL ||
2759 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2761 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2765 if (attr.enrollee_nonce == NULL ||
2766 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2767 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2771 if (attr.config_error == NULL) {
2772 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
2777 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
2778 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
2780 switch (old_state) {
2782 wps_fail_event(wps->wps, WPS_M2);
2785 wps_fail_event(wps->wps, WPS_M4);
2788 wps_fail_event(wps->wps, WPS_M6);
2791 wps_fail_event(wps->wps, WPS_M8);
2801 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
2802 const struct wpabuf *msg)
2804 struct wps_parse_attr attr;
2806 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
2808 if (wps->state != RECV_DONE &&
2809 (!wps->wps->wps_upnp || !wps->ext_reg)) {
2810 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2811 "receiving WSC_Done", wps->state);
2815 if (wps_parse_msg(msg, &attr) < 0)
2818 if (attr.msg_type == NULL) {
2819 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2823 if (*attr.msg_type != WPS_WSC_DONE) {
2824 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2829 #ifdef CONFIG_WPS_UPNP
2830 if (wps->wps->wps_upnp && wps->ext_reg) {
2831 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2832 "Registrar completed successfully");
2833 wps_device_store(wps->wps->registrar, &wps->peer_dev,
2837 #endif /* CONFIG_WPS_UPNP */
2839 if (attr.registrar_nonce == NULL ||
2840 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2842 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2846 if (attr.enrollee_nonce == NULL ||
2847 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2848 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2852 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
2853 wps_device_store(wps->wps->registrar, &wps->peer_dev,
2856 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
2857 wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
2858 struct wps_credential cred;
2860 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
2861 "on first Enrollee connection");
2863 os_memset(&cred, 0, sizeof(cred));
2864 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
2865 cred.ssid_len = wps->wps->ssid_len;
2866 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
2867 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
2868 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
2869 cred.key_len = wps->new_psk_len;
2871 wps->wps->wps_state = WPS_STATE_CONFIGURED;
2872 wpa_hexdump_ascii_key(MSG_DEBUG,
2873 "WPS: Generated random passphrase",
2874 wps->new_psk, wps->new_psk_len);
2875 if (wps->wps->cred_cb)
2876 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
2878 os_free(wps->new_psk);
2879 wps->new_psk = NULL;
2882 if (!wps->wps->ap && !wps->er)
2883 wps_sta_cred_cb(wps);
2886 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
2887 wps->new_psk, wps->new_psk_len)) {
2888 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
2891 os_free(wps->new_psk);
2892 wps->new_psk = NULL;
2895 wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e);
2898 wps_registrar_remove_pbc_session(wps->wps->registrar,
2899 wps->mac_addr_e, wps->uuid_e);
2900 wps_registrar_pbc_completed(wps->wps->registrar);
2902 wps_registrar_pin_completed(wps->wps->registrar);
2904 /* TODO: maintain AuthorizedMACs somewhere separately for each ER and
2905 * merge them into APs own list.. */
2907 wps_success_event(wps->wps);
2913 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
2914 enum wsc_op_code op_code,
2915 const struct wpabuf *msg)
2917 enum wps_process_res ret;
2919 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
2921 (unsigned long) wpabuf_len(msg), op_code);
2923 #ifdef CONFIG_WPS_UPNP
2924 if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
2925 struct wps_parse_attr attr;
2926 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
2927 *attr.msg_type == WPS_M3)
2928 wps->ext_reg = 2; /* past M2/M2D phase */
2930 if (wps->ext_reg > 1)
2931 wps_registrar_free_pending_m2(wps->wps);
2932 if (wps->wps->wps_upnp && wps->ext_reg &&
2933 wps->wps->upnp_msgs == NULL &&
2934 (op_code == WSC_MSG || op_code == WSC_Done || op_code == WSC_NACK))
2936 struct wps_parse_attr attr;
2938 if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
2941 type = *attr.msg_type;
2942 wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
2943 " to external Registrar for processing", type);
2944 upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
2946 UPNP_WPS_WLANEVENT_TYPE_EAP,
2948 if (op_code == WSC_MSG)
2950 } else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
2951 wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
2952 "external Registrar");
2953 return WPS_CONTINUE;
2955 #endif /* CONFIG_WPS_UPNP */
2959 return wps_process_wsc_msg(wps, msg);
2961 if (wps_validate_wsc_ack(msg) < 0)
2963 return wps_process_wsc_ack(wps, msg);
2965 if (wps_validate_wsc_nack(msg) < 0)
2967 return wps_process_wsc_nack(wps, msg);
2969 if (wps_validate_wsc_done(msg) < 0)
2971 ret = wps_process_wsc_done(wps, msg);
2972 if (ret == WPS_FAILURE) {
2973 wps->state = SEND_WSC_NACK;
2974 wps_fail_event(wps->wps, WPS_WSC_DONE);
2978 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
2984 int wps_registrar_update_ie(struct wps_registrar *reg)
2986 return wps_set_ie(reg);
2990 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
2993 struct wps_registrar *reg = eloop_ctx;
2995 wpa_printf(MSG_DEBUG, "WPS: Selected Registrar timeout - "
2996 "unselect internal Registrar");
2997 reg->selected_registrar = 0;
2999 wps_registrar_selected_registrar_changed(reg);
3003 #ifdef CONFIG_WPS_UPNP
3004 static void wps_registrar_sel_reg_add(struct wps_registrar *reg,
3005 struct subscription *s)
3008 wpa_printf(MSG_DEBUG, "WPS: External Registrar selected (dev_pw_id=%d "
3009 "config_methods=0x%x)",
3010 s->dev_password_id, s->config_methods);
3011 reg->sel_reg_union = 1;
3012 if (reg->sel_reg_dev_password_id_override != DEV_PW_PUSHBUTTON)
3013 reg->sel_reg_dev_password_id_override = s->dev_password_id;
3014 if (reg->sel_reg_config_methods_override == -1)
3015 reg->sel_reg_config_methods_override = 0;
3016 reg->sel_reg_config_methods_override |= s->config_methods;
3017 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++)
3018 if (is_zero_ether_addr(reg->authorized_macs_union[i]))
3020 for (j = 0; i < WPS_MAX_AUTHORIZED_MACS && j < WPS_MAX_AUTHORIZED_MACS;
3022 if (is_zero_ether_addr(s->authorized_macs[j]))
3024 wpa_printf(MSG_DEBUG, "WPS: Add authorized MAC into union: "
3025 MACSTR, MAC2STR(s->authorized_macs[j]));
3026 os_memcpy(reg->authorized_macs_union[i],
3027 s->authorized_macs[j], ETH_ALEN);
3030 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs union",
3031 (u8 *) reg->authorized_macs_union,
3032 sizeof(reg->authorized_macs_union));
3034 #endif /* CONFIG_WPS_UPNP */
3037 static void wps_registrar_sel_reg_union(struct wps_registrar *reg)
3039 #ifdef CONFIG_WPS_UPNP
3040 struct subscription *s;
3042 if (reg->wps->wps_upnp == NULL)
3045 dl_list_for_each(s, ®->wps->wps_upnp->subscriptions,
3046 struct subscription, list) {
3047 struct subscr_addr *sa;
3048 sa = dl_list_first(&s->addr_list, struct subscr_addr, list);
3050 wpa_printf(MSG_DEBUG, "WPS: External Registrar %s:%d",
3051 inet_ntoa(sa->saddr.sin_addr),
3052 ntohs(sa->saddr.sin_port));
3054 if (s->selected_registrar)
3055 wps_registrar_sel_reg_add(reg, s);
3057 wpa_printf(MSG_DEBUG, "WPS: External Registrar not "
3060 #endif /* CONFIG_WPS_UPNP */
3065 * wps_registrar_selected_registrar_changed - SetSelectedRegistrar change
3066 * @reg: Registrar data from wps_registrar_init()
3068 * This function is called when selected registrar state changes, e.g., when an
3069 * AP receives a SetSelectedRegistrar UPnP message.
3071 void wps_registrar_selected_registrar_changed(struct wps_registrar *reg)
3073 wpa_printf(MSG_DEBUG, "WPS: Selected registrar information changed");
3075 reg->sel_reg_union = reg->selected_registrar;
3076 reg->sel_reg_dev_password_id_override = -1;
3077 reg->sel_reg_config_methods_override = -1;
3078 os_memcpy(reg->authorized_macs_union, reg->authorized_macs,
3079 WPS_MAX_AUTHORIZED_MACS * ETH_ALEN);
3080 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs union (start with own)",
3081 (u8 *) reg->authorized_macs_union,
3082 sizeof(reg->authorized_macs_union));
3083 if (reg->selected_registrar) {
3086 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
3088 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
3089 WPS_CONFIG_PHY_PUSHBUTTON);
3090 #endif /* CONFIG_WPS2 */
3092 reg->sel_reg_dev_password_id_override =
3094 wps_set_pushbutton(&methods, reg->wps->config_methods);
3096 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar selected "
3097 "(pbc=%d)", reg->pbc);
3098 reg->sel_reg_config_methods_override = methods;
3100 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar not selected");
3102 wps_registrar_sel_reg_union(reg);
3105 wps_cb_set_sel_reg(reg);
3109 int wps_registrar_get_info(struct wps_registrar *reg, const u8 *addr,
3110 char *buf, size_t buflen)
3112 struct wps_registrar_device *d;
3115 char devtype[WPS_DEV_TYPE_BUFSIZE];
3117 d = wps_device_get(reg, addr);
3120 if (uuid_bin2str(d->uuid, uuid, sizeof(uuid)))
3123 ret = os_snprintf(buf + len, buflen - len,
3125 "wpsPrimaryDeviceType=%s\n"
3126 "wpsDeviceName=%s\n"
3127 "wpsManufacturer=%s\n"
3129 "wpsModelNumber=%s\n"
3130 "wpsSerialNumber=%s\n",
3132 wps_dev_type_bin2str(d->dev.pri_dev_type, devtype,
3134 d->dev.device_name ? d->dev.device_name : "",
3135 d->dev.manufacturer ? d->dev.manufacturer : "",
3136 d->dev.model_name ? d->dev.model_name : "",
3137 d->dev.model_number ? d->dev.model_number : "",
3138 d->dev.serial_number ? d->dev.serial_number : "");
3139 if (ret < 0 || (size_t) ret >= buflen - len)