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