1826b6685b2e5a044dc9d71c2edb7757ca048ff1
[openwrt/staging/ynezz.git] / package / network / services / hostapd / patches / 120-reconfigure-wps-credentials.patch
1 From b389a77a0f6dccf495dbce5be9476000f6ec06a2 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rapha=C3=ABl=20M=C3=A9lotte?= <raphael.melotte@mind.be>
3 Date: Wed, 9 Dec 2020 19:55:53 +0100
4 Subject: [PATCH] wps: reconfigure credentials on reload
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 When new credentials are configured and hostapd is reconfigured using
10 SIGHUP (or reload on the ctrl_iface), also update the wps credentials.
11
12 Before these changes, when WPS is triggered the registar always serves
13 the credentials that were configured when hostapd started.
14
15 Signed-off-by: Raphaël Mélotte <raphael.melotte@mind.be>
16 ---
17 src/ap/wps_hostapd.c | 86 +++++++++++++++++++++++++++++++++++++++--
18 src/wps/wps.h | 6 +++
19 src/wps/wps_registrar.c | 29 ++++++++++++++
20 3 files changed, 118 insertions(+), 3 deletions(-)
21
22 --- a/src/ap/wps_hostapd.c
23 +++ b/src/ap/wps_hostapd.c
24 @@ -1375,6 +1375,43 @@ static void hostapd_wps_nfc_clear(struct
25 #endif /* CONFIG_WPS_NFC */
26 }
27
28 +int hostapd_wps_update_multi_ap(struct hostapd_data *hapd,
29 + struct wps_registrar *reg) {
30 + struct hostapd_bss_config *conf = hapd->conf;
31 + u8 *multi_ap_backhaul_network_key = NULL;
32 + size_t multi_ap_backhaul_network_key_len = 0;
33 + int ret = -1;
34 +
35 + if ((conf->multi_ap & FRONTHAUL_BSS) &&
36 + conf->multi_ap_backhaul_ssid.ssid_len) {
37 + if (conf->multi_ap_backhaul_ssid.wpa_passphrase) {
38 + multi_ap_backhaul_network_key =
39 + (u8 *) os_strdup(conf->multi_ap_backhaul_ssid.wpa_passphrase);
40 + if (multi_ap_backhaul_network_key == NULL)
41 + return -1;
42 + multi_ap_backhaul_network_key_len =
43 + os_strlen(conf->multi_ap_backhaul_ssid.wpa_passphrase);
44 + } else if (conf->multi_ap_backhaul_ssid.wpa_psk) {
45 + multi_ap_backhaul_network_key = os_malloc(2 * PMK_LEN + 1);
46 + if (multi_ap_backhaul_network_key == NULL)
47 + return -1;
48 + wpa_snprintf_hex((char *) multi_ap_backhaul_network_key,
49 + 2 * PMK_LEN + 1,
50 + conf->multi_ap_backhaul_ssid.wpa_psk->psk,
51 + PMK_LEN);
52 + multi_ap_backhaul_network_key_len = 2 * PMK_LEN;
53 + }
54 + ret = wps_registrar_update_multi_ap(reg,
55 + conf->multi_ap_backhaul_ssid.ssid,
56 + conf->multi_ap_backhaul_ssid.ssid_len,
57 + multi_ap_backhaul_network_key,
58 + multi_ap_backhaul_network_key_len);
59 + os_free(multi_ap_backhaul_network_key);
60 + }
61 + return ret;
62 +}
63 +
64 +
65
66 void hostapd_deinit_wps(struct hostapd_data *hapd)
67 {
68 @@ -1409,11 +1446,54 @@ void hostapd_update_wps(struct hostapd_d
69 hapd->wps->upc = hapd->conf->upc;
70 #endif /* CONFIG_WPS_UPNP */
71
72 - hostapd_wps_set_vendor_ext(hapd, hapd->wps);
73 - hostapd_wps_set_application_ext(hapd, hapd->wps);
74 + struct wps_context *wps = hapd->wps;
75 + struct hostapd_bss_config *conf = hapd->conf;
76 +
77 + os_memcpy(wps->ssid, conf->ssid.ssid, conf->ssid.ssid_len);
78 + wps->ssid_len = conf->ssid.ssid_len;
79 +
80 + /* Clear wps settings, then fill them again */
81 + os_free(wps->network_key);
82 + wps->network_key = NULL;
83 + wps->network_key_len = 0;
84 + wps->psk_set = 0;
85 + if (conf->ssid.wpa_psk_file) {
86 + /* Use per-device PSKs */
87 + } else if (conf->ssid.wpa_passphrase) {
88 + wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
89 + if (wps->network_key == NULL)
90 + return;
91 + wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
92 + } else if (conf->ssid.wpa_psk) {
93 + wps->network_key = os_malloc(2 * PMK_LEN + 1);
94 + if (wps->network_key == NULL)
95 + return;
96 + wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
97 + conf->ssid.wpa_psk->psk, PMK_LEN);
98 + wps->network_key_len = 2 * PMK_LEN;
99 +#ifdef CONFIG_WEP
100 + } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
101 + wps->network_key = os_malloc(conf->ssid.wep.len[0]);
102 + if (wps->network_key == NULL)
103 + return;
104 + os_memcpy(wps->network_key, conf->ssid.wep.key[0],
105 + conf->ssid.wep.len[0]);
106 + wps->network_key_len = conf->ssid.wep.len[0];
107 +#endif /* CONFIG_WEP */
108 + }
109 +
110 + if (conf->ssid.wpa_psk) {
111 + os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
112 + wps->psk_set = 1;
113 + }
114 +
115 + hostapd_wps_update_multi_ap(hapd, wps->registrar);
116 +
117 + hostapd_wps_set_vendor_ext(hapd, wps);
118 + hostapd_wps_set_application_ext(hapd, wps);
119
120 if (hapd->conf->wps_state)
121 - wps_registrar_update_ie(hapd->wps->registrar);
122 + wps_registrar_update_ie(wps->registrar);
123 else
124 hostapd_deinit_wps(hapd);
125 }
126 --- a/src/wps/wps.h
127 +++ b/src/wps/wps.h
128 @@ -938,6 +938,12 @@ struct wpabuf * wps_build_nfc_handover_s
129 struct wpabuf *nfc_dh_pubkey,
130 struct wpabuf *nfc_dev_pw);
131
132 +int wps_registrar_update_multi_ap(struct wps_registrar *reg,
133 + const u8 *multi_ap_backhaul_ssid,
134 + size_t multi_ap_backhaul_ssid_len,
135 + const u8 *multi_ap_backhaul_network_key,
136 + size_t multi_ap_backhaul_network_key_len);
137 +
138 /* ndef.c */
139 struct wpabuf * ndef_parse_wifi(const struct wpabuf *buf);
140 struct wpabuf * ndef_build_wifi(const struct wpabuf *buf);
141 --- a/src/wps/wps_registrar.c
142 +++ b/src/wps/wps_registrar.c
143 @@ -3669,6 +3669,35 @@ int wps_registrar_config_ap(struct wps_r
144 }
145
146
147 +int wps_registrar_update_multi_ap(struct wps_registrar *reg,
148 + const u8 *multi_ap_backhaul_ssid,
149 + size_t multi_ap_backhaul_ssid_len,
150 + const u8 *multi_ap_backhaul_network_key,
151 + size_t multi_ap_backhaul_network_key_len)
152 +{
153 + if (multi_ap_backhaul_ssid != NULL) {
154 + os_memcpy(reg->multi_ap_backhaul_ssid,
155 + multi_ap_backhaul_ssid,
156 + multi_ap_backhaul_ssid_len);
157 + reg->multi_ap_backhaul_ssid_len =
158 + multi_ap_backhaul_ssid_len;
159 + }
160 + os_free(reg->multi_ap_backhaul_network_key);
161 + reg->multi_ap_backhaul_network_key = NULL;
162 + reg->multi_ap_backhaul_network_key_len = 0;
163 +
164 + if (multi_ap_backhaul_network_key != NULL) {
165 + reg->multi_ap_backhaul_network_key =
166 + os_memdup(multi_ap_backhaul_network_key,
167 + multi_ap_backhaul_network_key_len);
168 + if (reg->multi_ap_backhaul_network_key == NULL)
169 + return -1;
170 + reg->multi_ap_backhaul_network_key_len =
171 + multi_ap_backhaul_network_key_len;
172 + }
173 + return 0;
174 +}
175 +
176 #ifdef CONFIG_WPS_NFC
177
178 int wps_registrar_add_nfc_pw_token(struct wps_registrar *reg,