1/*
2 * Wi-Fi Protected Setup - UPnP AP functionality
3 * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "eloop.h"
13#include "uuid.h"
14#include "wps_i.h"
15#include "wps_upnp.h"
16#include "wps_upnp_i.h"
17
18
19static void upnp_er_set_selected_timeout(void *eloop_ctx, void *timeout_ctx)
20{
21	struct subscription *s = eloop_ctx;
22	struct wps_registrar *reg = timeout_ctx;
23	wpa_printf(MSG_DEBUG, "WPS: SetSelectedRegistrar from ER timed out");
24	s->selected_registrar = 0;
25	wps_registrar_selected_registrar_changed(reg, 0);
26}
27
28
29int upnp_er_set_selected_registrar(struct wps_registrar *reg,
30				   struct subscription *s,
31				   const struct wpabuf *msg)
32{
33	struct wps_parse_attr attr;
34
35	wpa_hexdump_buf(MSG_MSGDUMP, "WPS: SetSelectedRegistrar attributes",
36			msg);
37	if (wps_validate_upnp_set_selected_registrar(msg) < 0 ||
38	    wps_parse_msg(msg, &attr) < 0)
39		return -1;
40
41	s->reg = reg;
42	eloop_cancel_timeout(upnp_er_set_selected_timeout, s, reg);
43
44	os_memset(s->authorized_macs, 0, sizeof(s->authorized_macs));
45	if (attr.selected_registrar == NULL || *attr.selected_registrar == 0) {
46		wpa_printf(MSG_DEBUG, "WPS: SetSelectedRegistrar: Disable "
47			   "Selected Registrar");
48		s->selected_registrar = 0;
49	} else {
50		s->selected_registrar = 1;
51		s->dev_password_id = attr.dev_password_id ?
52			WPA_GET_BE16(attr.dev_password_id) : DEV_PW_DEFAULT;
53		s->config_methods = attr.sel_reg_config_methods ?
54			WPA_GET_BE16(attr.sel_reg_config_methods) : -1;
55		if (attr.authorized_macs) {
56			int count = attr.authorized_macs_len / ETH_ALEN;
57			if (count > WPS_MAX_AUTHORIZED_MACS)
58				count = WPS_MAX_AUTHORIZED_MACS;
59			os_memcpy(s->authorized_macs, attr.authorized_macs,
60				  count * ETH_ALEN);
61		} else if (!attr.version2) {
62			wpa_printf(MSG_DEBUG, "WPS: Add broadcast "
63				   "AuthorizedMACs for WPS 1.0 ER");
64			os_memset(s->authorized_macs, 0xff, ETH_ALEN);
65		}
66		eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
67				       upnp_er_set_selected_timeout, s, reg);
68	}
69
70	wps_registrar_selected_registrar_changed(reg, 0);
71
72	return 0;
73}
74
75
76void upnp_er_remove_notification(struct wps_registrar *reg,
77				 struct subscription *s)
78{
79	s->selected_registrar = 0;
80	eloop_cancel_timeout(upnp_er_set_selected_timeout, s, reg);
81	if (reg)
82		wps_registrar_selected_registrar_changed(reg, 0);
83}
84