]>
Commit | Line | Data |
---|---|---|
6fc6879b JM |
1 | /* |
2 | * WPA Supplicant / Configuration parser and common functions | |
f64adcd7 | 3 | * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi> |
6fc6879b | 4 | * |
0f3d578e JM |
5 | * This software may be distributed under the terms of the BSD license. |
6 | * See README for more details. | |
6fc6879b JM |
7 | */ |
8 | ||
9 | #include "includes.h" | |
10 | ||
11 | #include "common.h" | |
121adf9c | 12 | #include "utils/uuid.h" |
25ef8529 | 13 | #include "utils/ip_addr.h" |
03da66bd | 14 | #include "crypto/sha1.h" |
3acb5005 | 15 | #include "rsn_supp/wpa.h" |
6fc6879b | 16 | #include "eap_peer/eap.h" |
21d996f7 | 17 | #include "p2p/p2p.h" |
6fc6879b JM |
18 | #include "config.h" |
19 | ||
20 | ||
21 | #if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE) | |
22 | #define NO_CONFIG_WRITE | |
23 | #endif | |
24 | ||
25 | /* | |
26 | * Structure for network configuration parsing. This data is used to implement | |
27 | * a generic parser for each network block variable. The table of configuration | |
28 | * variables is defined below in this file (ssid_fields[]). | |
29 | */ | |
30 | struct parse_data { | |
31 | /* Configuration variable name */ | |
32 | char *name; | |
33 | ||
34 | /* Parser function for this variable */ | |
35 | int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid, | |
36 | int line, const char *value); | |
37 | ||
38 | #ifndef NO_CONFIG_WRITE | |
39 | /* Writer function (i.e., to get the variable in text format from | |
40 | * internal presentation). */ | |
41 | char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid); | |
42 | #endif /* NO_CONFIG_WRITE */ | |
43 | ||
44 | /* Variable specific parameters for the parser. */ | |
45 | void *param1, *param2, *param3, *param4; | |
46 | ||
47 | /* 0 = this variable can be included in debug output and ctrl_iface | |
48 | * 1 = this variable contains key/private data and it must not be | |
49 | * included in debug output unless explicitly requested. In | |
50 | * addition, this variable will not be readable through the | |
51 | * ctrl_iface. | |
52 | */ | |
53 | int key_data; | |
54 | }; | |
55 | ||
56 | ||
6fc6879b JM |
57 | static int wpa_config_parse_str(const struct parse_data *data, |
58 | struct wpa_ssid *ssid, | |
59 | int line, const char *value) | |
60 | { | |
61 | size_t res_len, *dst_len; | |
62 | char **dst, *tmp; | |
63 | ||
b56c0546 JM |
64 | if (os_strcmp(value, "NULL") == 0) { |
65 | wpa_printf(MSG_DEBUG, "Unset configuration string '%s'", | |
66 | data->name); | |
67 | tmp = NULL; | |
68 | res_len = 0; | |
69 | goto set; | |
70 | } | |
71 | ||
6fc6879b JM |
72 | tmp = wpa_config_parse_string(value, &res_len); |
73 | if (tmp == NULL) { | |
74 | wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.", | |
75 | line, data->name, | |
76 | data->key_data ? "[KEY DATA REMOVED]" : value); | |
77 | return -1; | |
78 | } | |
79 | ||
80 | if (data->key_data) { | |
81 | wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name, | |
82 | (u8 *) tmp, res_len); | |
83 | } else { | |
84 | wpa_hexdump_ascii(MSG_MSGDUMP, data->name, | |
85 | (u8 *) tmp, res_len); | |
86 | } | |
87 | ||
88 | if (data->param3 && res_len < (size_t) data->param3) { | |
89 | wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu " | |
90 | "min_len=%ld)", line, data->name, | |
91 | (unsigned long) res_len, (long) data->param3); | |
92 | os_free(tmp); | |
93 | return -1; | |
94 | } | |
95 | ||
96 | if (data->param4 && res_len > (size_t) data->param4) { | |
97 | wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu " | |
98 | "max_len=%ld)", line, data->name, | |
99 | (unsigned long) res_len, (long) data->param4); | |
100 | os_free(tmp); | |
101 | return -1; | |
102 | } | |
103 | ||
b56c0546 | 104 | set: |
6fc6879b JM |
105 | dst = (char **) (((u8 *) ssid) + (long) data->param1); |
106 | dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2); | |
107 | os_free(*dst); | |
108 | *dst = tmp; | |
109 | if (data->param2) | |
110 | *dst_len = res_len; | |
111 | ||
112 | return 0; | |
113 | } | |
114 | ||
115 | ||
116 | #ifndef NO_CONFIG_WRITE | |
6fc6879b JM |
117 | static char * wpa_config_write_string_ascii(const u8 *value, size_t len) |
118 | { | |
119 | char *buf; | |
120 | ||
121 | buf = os_malloc(len + 3); | |
122 | if (buf == NULL) | |
123 | return NULL; | |
124 | buf[0] = '"'; | |
125 | os_memcpy(buf + 1, value, len); | |
126 | buf[len + 1] = '"'; | |
127 | buf[len + 2] = '\0'; | |
128 | ||
129 | return buf; | |
130 | } | |
131 | ||
132 | ||
133 | static char * wpa_config_write_string_hex(const u8 *value, size_t len) | |
134 | { | |
135 | char *buf; | |
136 | ||
137 | buf = os_zalloc(2 * len + 1); | |
138 | if (buf == NULL) | |
139 | return NULL; | |
140 | wpa_snprintf_hex(buf, 2 * len + 1, value, len); | |
141 | ||
142 | return buf; | |
143 | } | |
144 | ||
145 | ||
146 | static char * wpa_config_write_string(const u8 *value, size_t len) | |
147 | { | |
148 | if (value == NULL) | |
149 | return NULL; | |
150 | ||
151 | if (is_hex(value, len)) | |
152 | return wpa_config_write_string_hex(value, len); | |
153 | else | |
154 | return wpa_config_write_string_ascii(value, len); | |
155 | } | |
156 | ||
157 | ||
158 | static char * wpa_config_write_str(const struct parse_data *data, | |
159 | struct wpa_ssid *ssid) | |
160 | { | |
161 | size_t len; | |
162 | char **src; | |
163 | ||
164 | src = (char **) (((u8 *) ssid) + (long) data->param1); | |
165 | if (*src == NULL) | |
166 | return NULL; | |
167 | ||
168 | if (data->param2) | |
169 | len = *((size_t *) (((u8 *) ssid) + (long) data->param2)); | |
170 | else | |
171 | len = os_strlen(*src); | |
172 | ||
173 | return wpa_config_write_string((const u8 *) *src, len); | |
174 | } | |
175 | #endif /* NO_CONFIG_WRITE */ | |
176 | ||
177 | ||
178 | static int wpa_config_parse_int(const struct parse_data *data, | |
179 | struct wpa_ssid *ssid, | |
180 | int line, const char *value) | |
181 | { | |
eae3a584 JB |
182 | int val, *dst; |
183 | char *end; | |
6fc6879b JM |
184 | |
185 | dst = (int *) (((u8 *) ssid) + (long) data->param1); | |
eae3a584 JB |
186 | val = strtol(value, &end, 0); |
187 | if (*end) { | |
188 | wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"", | |
189 | line, value); | |
190 | return -1; | |
191 | } | |
192 | *dst = val; | |
6fc6879b JM |
193 | wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst); |
194 | ||
195 | if (data->param3 && *dst < (long) data->param3) { | |
196 | wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d " | |
197 | "min_value=%ld)", line, data->name, *dst, | |
198 | (long) data->param3); | |
199 | *dst = (long) data->param3; | |
200 | return -1; | |
201 | } | |
202 | ||
203 | if (data->param4 && *dst > (long) data->param4) { | |
204 | wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d " | |
205 | "max_value=%ld)", line, data->name, *dst, | |
206 | (long) data->param4); | |
207 | *dst = (long) data->param4; | |
208 | return -1; | |
209 | } | |
210 | ||
211 | return 0; | |
212 | } | |
213 | ||
214 | ||
215 | #ifndef NO_CONFIG_WRITE | |
216 | static char * wpa_config_write_int(const struct parse_data *data, | |
217 | struct wpa_ssid *ssid) | |
218 | { | |
219 | int *src, res; | |
220 | char *value; | |
221 | ||
222 | src = (int *) (((u8 *) ssid) + (long) data->param1); | |
223 | ||
224 | value = os_malloc(20); | |
225 | if (value == NULL) | |
226 | return NULL; | |
227 | res = os_snprintf(value, 20, "%d", *src); | |
228 | if (res < 0 || res >= 20) { | |
229 | os_free(value); | |
230 | return NULL; | |
231 | } | |
232 | value[20 - 1] = '\0'; | |
233 | return value; | |
234 | } | |
235 | #endif /* NO_CONFIG_WRITE */ | |
236 | ||
237 | ||
238 | static int wpa_config_parse_bssid(const struct parse_data *data, | |
239 | struct wpa_ssid *ssid, int line, | |
240 | const char *value) | |
241 | { | |
c0a321c5 WJL |
242 | if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 || |
243 | os_strcmp(value, "any") == 0) { | |
244 | ssid->bssid_set = 0; | |
245 | wpa_printf(MSG_MSGDUMP, "BSSID any"); | |
246 | return 0; | |
247 | } | |
6fc6879b JM |
248 | if (hwaddr_aton(value, ssid->bssid)) { |
249 | wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.", | |
250 | line, value); | |
251 | return -1; | |
252 | } | |
253 | ssid->bssid_set = 1; | |
254 | wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN); | |
255 | return 0; | |
256 | } | |
257 | ||
258 | ||
259 | #ifndef NO_CONFIG_WRITE | |
260 | static char * wpa_config_write_bssid(const struct parse_data *data, | |
261 | struct wpa_ssid *ssid) | |
262 | { | |
263 | char *value; | |
264 | int res; | |
265 | ||
266 | if (!ssid->bssid_set) | |
267 | return NULL; | |
268 | ||
269 | value = os_malloc(20); | |
270 | if (value == NULL) | |
271 | return NULL; | |
272 | res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid)); | |
273 | if (res < 0 || res >= 20) { | |
274 | os_free(value); | |
275 | return NULL; | |
276 | } | |
277 | value[20 - 1] = '\0'; | |
278 | return value; | |
279 | } | |
280 | #endif /* NO_CONFIG_WRITE */ | |
281 | ||
282 | ||
283 | static int wpa_config_parse_psk(const struct parse_data *data, | |
284 | struct wpa_ssid *ssid, int line, | |
285 | const char *value) | |
286 | { | |
9173b16f JM |
287 | #ifdef CONFIG_EXT_PASSWORD |
288 | if (os_strncmp(value, "ext:", 4) == 0) { | |
19c48da0 | 289 | str_clear_free(ssid->passphrase); |
9173b16f JM |
290 | ssid->passphrase = NULL; |
291 | ssid->psk_set = 0; | |
292 | os_free(ssid->ext_psk); | |
293 | ssid->ext_psk = os_strdup(value + 4); | |
294 | if (ssid->ext_psk == NULL) | |
295 | return -1; | |
296 | wpa_printf(MSG_DEBUG, "PSK: External password '%s'", | |
297 | ssid->ext_psk); | |
298 | return 0; | |
299 | } | |
300 | #endif /* CONFIG_EXT_PASSWORD */ | |
301 | ||
6fc6879b JM |
302 | if (*value == '"') { |
303 | #ifndef CONFIG_NO_PBKDF2 | |
304 | const char *pos; | |
305 | size_t len; | |
306 | ||
307 | value++; | |
308 | pos = os_strrchr(value, '"'); | |
309 | if (pos) | |
310 | len = pos - value; | |
311 | else | |
312 | len = os_strlen(value); | |
313 | if (len < 8 || len > 63) { | |
314 | wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase " | |
315 | "length %lu (expected: 8..63) '%s'.", | |
316 | line, (unsigned long) len, value); | |
317 | return -1; | |
318 | } | |
319 | wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)", | |
320 | (u8 *) value, len); | |
321 | if (ssid->passphrase && os_strlen(ssid->passphrase) == len && | |
322 | os_memcmp(ssid->passphrase, value, len) == 0) | |
323 | return 0; | |
324 | ssid->psk_set = 0; | |
19c48da0 | 325 | str_clear_free(ssid->passphrase); |
5e24dc8a | 326 | ssid->passphrase = dup_binstr(value, len); |
6fc6879b JM |
327 | if (ssid->passphrase == NULL) |
328 | return -1; | |
6fc6879b JM |
329 | return 0; |
330 | #else /* CONFIG_NO_PBKDF2 */ | |
331 | wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not " | |
332 | "supported.", line); | |
333 | return -1; | |
334 | #endif /* CONFIG_NO_PBKDF2 */ | |
335 | } | |
336 | ||
337 | if (hexstr2bin(value, ssid->psk, PMK_LEN) || | |
338 | value[PMK_LEN * 2] != '\0') { | |
339 | wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.", | |
340 | line, value); | |
341 | return -1; | |
342 | } | |
343 | ||
19c48da0 | 344 | str_clear_free(ssid->passphrase); |
6fc6879b JM |
345 | ssid->passphrase = NULL; |
346 | ||
347 | ssid->psk_set = 1; | |
348 | wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN); | |
349 | return 0; | |
350 | } | |
351 | ||
352 | ||
353 | #ifndef NO_CONFIG_WRITE | |
354 | static char * wpa_config_write_psk(const struct parse_data *data, | |
355 | struct wpa_ssid *ssid) | |
356 | { | |
9173b16f JM |
357 | #ifdef CONFIG_EXT_PASSWORD |
358 | if (ssid->ext_psk) { | |
359 | size_t len = 4 + os_strlen(ssid->ext_psk) + 1; | |
360 | char *buf = os_malloc(len); | |
361 | if (buf == NULL) | |
362 | return NULL; | |
363 | os_snprintf(buf, len, "ext:%s", ssid->ext_psk); | |
364 | return buf; | |
365 | } | |
366 | #endif /* CONFIG_EXT_PASSWORD */ | |
367 | ||
6fc6879b JM |
368 | if (ssid->passphrase) |
369 | return wpa_config_write_string_ascii( | |
370 | (const u8 *) ssid->passphrase, | |
371 | os_strlen(ssid->passphrase)); | |
372 | ||
373 | if (ssid->psk_set) | |
374 | return wpa_config_write_string_hex(ssid->psk, PMK_LEN); | |
375 | ||
376 | return NULL; | |
377 | } | |
378 | #endif /* NO_CONFIG_WRITE */ | |
379 | ||
380 | ||
381 | static int wpa_config_parse_proto(const struct parse_data *data, | |
382 | struct wpa_ssid *ssid, int line, | |
383 | const char *value) | |
384 | { | |
385 | int val = 0, last, errors = 0; | |
386 | char *start, *end, *buf; | |
387 | ||
388 | buf = os_strdup(value); | |
389 | if (buf == NULL) | |
390 | return -1; | |
391 | start = buf; | |
392 | ||
393 | while (*start != '\0') { | |
394 | while (*start == ' ' || *start == '\t') | |
395 | start++; | |
396 | if (*start == '\0') | |
397 | break; | |
398 | end = start; | |
399 | while (*end != ' ' && *end != '\t' && *end != '\0') | |
400 | end++; | |
401 | last = *end == '\0'; | |
402 | *end = '\0'; | |
403 | if (os_strcmp(start, "WPA") == 0) | |
404 | val |= WPA_PROTO_WPA; | |
405 | else if (os_strcmp(start, "RSN") == 0 || | |
406 | os_strcmp(start, "WPA2") == 0) | |
407 | val |= WPA_PROTO_RSN; | |
df0f01d9 JM |
408 | else if (os_strcmp(start, "OSEN") == 0) |
409 | val |= WPA_PROTO_OSEN; | |
6fc6879b JM |
410 | else { |
411 | wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'", | |
412 | line, start); | |
413 | errors++; | |
414 | } | |
415 | ||
416 | if (last) | |
417 | break; | |
418 | start = end + 1; | |
419 | } | |
420 | os_free(buf); | |
421 | ||
422 | if (val == 0) { | |
423 | wpa_printf(MSG_ERROR, | |
424 | "Line %d: no proto values configured.", line); | |
425 | errors++; | |
426 | } | |
427 | ||
428 | wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val); | |
429 | ssid->proto = val; | |
430 | return errors ? -1 : 0; | |
431 | } | |
432 | ||
433 | ||
434 | #ifndef NO_CONFIG_WRITE | |
435 | static char * wpa_config_write_proto(const struct parse_data *data, | |
436 | struct wpa_ssid *ssid) | |
437 | { | |
290ea6a7 | 438 | int ret; |
6fc6879b JM |
439 | char *buf, *pos, *end; |
440 | ||
3141b82c | 441 | pos = buf = os_zalloc(20); |
6fc6879b JM |
442 | if (buf == NULL) |
443 | return NULL; | |
3141b82c | 444 | end = buf + 20; |
6fc6879b JM |
445 | |
446 | if (ssid->proto & WPA_PROTO_WPA) { | |
290ea6a7 JM |
447 | ret = os_snprintf(pos, end - pos, "%sWPA", |
448 | pos == buf ? "" : " "); | |
6fc6879b JM |
449 | if (ret < 0 || ret >= end - pos) |
450 | return buf; | |
451 | pos += ret; | |
6fc6879b JM |
452 | } |
453 | ||
454 | if (ssid->proto & WPA_PROTO_RSN) { | |
290ea6a7 JM |
455 | ret = os_snprintf(pos, end - pos, "%sRSN", |
456 | pos == buf ? "" : " "); | |
6fc6879b JM |
457 | if (ret < 0 || ret >= end - pos) |
458 | return buf; | |
459 | pos += ret; | |
6fc6879b JM |
460 | } |
461 | ||
3141b82c | 462 | if (ssid->proto & WPA_PROTO_OSEN) { |
290ea6a7 JM |
463 | ret = os_snprintf(pos, end - pos, "%sOSEN", |
464 | pos == buf ? "" : " "); | |
3141b82c JM |
465 | if (ret < 0 || ret >= end - pos) |
466 | return buf; | |
467 | pos += ret; | |
3141b82c JM |
468 | } |
469 | ||
a1e46f32 JM |
470 | if (pos == buf) { |
471 | os_free(buf); | |
472 | buf = NULL; | |
473 | } | |
474 | ||
6fc6879b JM |
475 | return buf; |
476 | } | |
477 | #endif /* NO_CONFIG_WRITE */ | |
478 | ||
479 | ||
480 | static int wpa_config_parse_key_mgmt(const struct parse_data *data, | |
481 | struct wpa_ssid *ssid, int line, | |
482 | const char *value) | |
483 | { | |
484 | int val = 0, last, errors = 0; | |
485 | char *start, *end, *buf; | |
486 | ||
487 | buf = os_strdup(value); | |
488 | if (buf == NULL) | |
489 | return -1; | |
490 | start = buf; | |
491 | ||
492 | while (*start != '\0') { | |
493 | while (*start == ' ' || *start == '\t') | |
494 | start++; | |
495 | if (*start == '\0') | |
496 | break; | |
497 | end = start; | |
498 | while (*end != ' ' && *end != '\t' && *end != '\0') | |
499 | end++; | |
500 | last = *end == '\0'; | |
501 | *end = '\0'; | |
502 | if (os_strcmp(start, "WPA-PSK") == 0) | |
503 | val |= WPA_KEY_MGMT_PSK; | |
504 | else if (os_strcmp(start, "WPA-EAP") == 0) | |
505 | val |= WPA_KEY_MGMT_IEEE8021X; | |
506 | else if (os_strcmp(start, "IEEE8021X") == 0) | |
507 | val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA; | |
508 | else if (os_strcmp(start, "NONE") == 0) | |
509 | val |= WPA_KEY_MGMT_NONE; | |
510 | else if (os_strcmp(start, "WPA-NONE") == 0) | |
511 | val |= WPA_KEY_MGMT_WPA_NONE; | |
512 | #ifdef CONFIG_IEEE80211R | |
513 | else if (os_strcmp(start, "FT-PSK") == 0) | |
514 | val |= WPA_KEY_MGMT_FT_PSK; | |
515 | else if (os_strcmp(start, "FT-EAP") == 0) | |
516 | val |= WPA_KEY_MGMT_FT_IEEE8021X; | |
517 | #endif /* CONFIG_IEEE80211R */ | |
56586197 JM |
518 | #ifdef CONFIG_IEEE80211W |
519 | else if (os_strcmp(start, "WPA-PSK-SHA256") == 0) | |
520 | val |= WPA_KEY_MGMT_PSK_SHA256; | |
521 | else if (os_strcmp(start, "WPA-EAP-SHA256") == 0) | |
522 | val |= WPA_KEY_MGMT_IEEE8021X_SHA256; | |
523 | #endif /* CONFIG_IEEE80211W */ | |
ad08c363 JM |
524 | #ifdef CONFIG_WPS |
525 | else if (os_strcmp(start, "WPS") == 0) | |
526 | val |= WPA_KEY_MGMT_WPS; | |
527 | #endif /* CONFIG_WPS */ | |
c10347f2 JM |
528 | #ifdef CONFIG_SAE |
529 | else if (os_strcmp(start, "SAE") == 0) | |
530 | val |= WPA_KEY_MGMT_SAE; | |
531 | else if (os_strcmp(start, "FT-SAE") == 0) | |
532 | val |= WPA_KEY_MGMT_FT_SAE; | |
533 | #endif /* CONFIG_SAE */ | |
df0f01d9 JM |
534 | #ifdef CONFIG_HS20 |
535 | else if (os_strcmp(start, "OSEN") == 0) | |
536 | val |= WPA_KEY_MGMT_OSEN; | |
537 | #endif /* CONFIG_HS20 */ | |
6fc6879b JM |
538 | else { |
539 | wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'", | |
540 | line, start); | |
541 | errors++; | |
542 | } | |
543 | ||
544 | if (last) | |
545 | break; | |
546 | start = end + 1; | |
547 | } | |
548 | os_free(buf); | |
549 | ||
550 | if (val == 0) { | |
551 | wpa_printf(MSG_ERROR, | |
552 | "Line %d: no key_mgmt values configured.", line); | |
553 | errors++; | |
554 | } | |
555 | ||
556 | wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val); | |
557 | ssid->key_mgmt = val; | |
558 | return errors ? -1 : 0; | |
559 | } | |
560 | ||
561 | ||
562 | #ifndef NO_CONFIG_WRITE | |
563 | static char * wpa_config_write_key_mgmt(const struct parse_data *data, | |
564 | struct wpa_ssid *ssid) | |
565 | { | |
566 | char *buf, *pos, *end; | |
567 | int ret; | |
568 | ||
50793929 | 569 | pos = buf = os_zalloc(100); |
6fc6879b JM |
570 | if (buf == NULL) |
571 | return NULL; | |
50793929 | 572 | end = buf + 100; |
6fc6879b JM |
573 | |
574 | if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) { | |
575 | ret = os_snprintf(pos, end - pos, "%sWPA-PSK", | |
576 | pos == buf ? "" : " "); | |
577 | if (ret < 0 || ret >= end - pos) { | |
578 | end[-1] = '\0'; | |
579 | return buf; | |
580 | } | |
581 | pos += ret; | |
582 | } | |
583 | ||
584 | if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) { | |
585 | ret = os_snprintf(pos, end - pos, "%sWPA-EAP", | |
586 | pos == buf ? "" : " "); | |
587 | if (ret < 0 || ret >= end - pos) { | |
588 | end[-1] = '\0'; | |
589 | return buf; | |
590 | } | |
591 | pos += ret; | |
592 | } | |
593 | ||
594 | if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) { | |
595 | ret = os_snprintf(pos, end - pos, "%sIEEE8021X", | |
596 | pos == buf ? "" : " "); | |
597 | if (ret < 0 || ret >= end - pos) { | |
598 | end[-1] = '\0'; | |
599 | return buf; | |
600 | } | |
601 | pos += ret; | |
602 | } | |
603 | ||
604 | if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) { | |
605 | ret = os_snprintf(pos, end - pos, "%sNONE", | |
606 | pos == buf ? "" : " "); | |
607 | if (ret < 0 || ret >= end - pos) { | |
608 | end[-1] = '\0'; | |
609 | return buf; | |
610 | } | |
611 | pos += ret; | |
612 | } | |
613 | ||
614 | if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) { | |
615 | ret = os_snprintf(pos, end - pos, "%sWPA-NONE", | |
616 | pos == buf ? "" : " "); | |
617 | if (ret < 0 || ret >= end - pos) { | |
618 | end[-1] = '\0'; | |
619 | return buf; | |
620 | } | |
621 | pos += ret; | |
622 | } | |
623 | ||
624 | #ifdef CONFIG_IEEE80211R | |
50793929 PF |
625 | if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK) { |
626 | ret = os_snprintf(pos, end - pos, "%sFT-PSK", | |
627 | pos == buf ? "" : " "); | |
628 | if (ret < 0 || ret >= end - pos) { | |
629 | end[-1] = '\0'; | |
630 | return buf; | |
631 | } | |
632 | pos += ret; | |
633 | } | |
6fc6879b | 634 | |
50793929 PF |
635 | if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) { |
636 | ret = os_snprintf(pos, end - pos, "%sFT-EAP", | |
637 | pos == buf ? "" : " "); | |
638 | if (ret < 0 || ret >= end - pos) { | |
639 | end[-1] = '\0'; | |
640 | return buf; | |
641 | } | |
642 | pos += ret; | |
643 | } | |
6fc6879b JM |
644 | #endif /* CONFIG_IEEE80211R */ |
645 | ||
56586197 | 646 | #ifdef CONFIG_IEEE80211W |
50793929 PF |
647 | if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256) { |
648 | ret = os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256", | |
649 | pos == buf ? "" : " "); | |
650 | if (ret < 0 || ret >= end - pos) { | |
651 | end[-1] = '\0'; | |
652 | return buf; | |
653 | } | |
654 | pos += ret; | |
655 | } | |
56586197 | 656 | |
50793929 PF |
657 | if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) { |
658 | ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256", | |
659 | pos == buf ? "" : " "); | |
660 | if (ret < 0 || ret >= end - pos) { | |
661 | end[-1] = '\0'; | |
662 | return buf; | |
663 | } | |
664 | pos += ret; | |
665 | } | |
56586197 JM |
666 | #endif /* CONFIG_IEEE80211W */ |
667 | ||
728fae16 | 668 | #ifdef CONFIG_WPS |
50793929 PF |
669 | if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) { |
670 | ret = os_snprintf(pos, end - pos, "%sWPS", | |
671 | pos == buf ? "" : " "); | |
672 | if (ret < 0 || ret >= end - pos) { | |
673 | end[-1] = '\0'; | |
674 | return buf; | |
675 | } | |
676 | pos += ret; | |
677 | } | |
728fae16 JM |
678 | #endif /* CONFIG_WPS */ |
679 | ||
d3fd563f TP |
680 | #ifdef CONFIG_SAE |
681 | if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) { | |
682 | ret = os_snprintf(pos, end - pos, "%sSAE", | |
683 | pos == buf ? "" : " "); | |
684 | if (ret < 0 || ret >= end - pos) { | |
685 | end[-1] = '\0'; | |
686 | return buf; | |
687 | } | |
688 | pos += ret; | |
689 | } | |
690 | ||
691 | if (ssid->key_mgmt & WPA_KEY_MGMT_FT_SAE) { | |
692 | ret = os_snprintf(pos, end - pos, "%sFT-SAE", | |
693 | pos == buf ? "" : " "); | |
694 | if (ret < 0 || ret >= end - pos) { | |
695 | end[-1] = '\0'; | |
696 | return buf; | |
697 | } | |
698 | pos += ret; | |
699 | } | |
700 | #endif /* CONFIG_SAE */ | |
701 | ||
2d6ee86f JM |
702 | #ifdef CONFIG_HS20 |
703 | if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN) { | |
704 | ret = os_snprintf(pos, end - pos, "%sOSEN", | |
705 | pos == buf ? "" : " "); | |
706 | if (ret < 0 || ret >= end - pos) { | |
707 | end[-1] = '\0'; | |
708 | return buf; | |
709 | } | |
710 | pos += ret; | |
711 | } | |
712 | #endif /* CONFIG_HS20 */ | |
713 | ||
a1e46f32 JM |
714 | if (pos == buf) { |
715 | os_free(buf); | |
716 | buf = NULL; | |
717 | } | |
718 | ||
6fc6879b JM |
719 | return buf; |
720 | } | |
721 | #endif /* NO_CONFIG_WRITE */ | |
722 | ||
723 | ||
724 | static int wpa_config_parse_cipher(int line, const char *value) | |
725 | { | |
a39c78be JM |
726 | int val = wpa_parse_cipher(value); |
727 | if (val < 0) { | |
728 | wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.", | |
729 | line, value); | |
6fc6879b | 730 | return -1; |
6fc6879b | 731 | } |
6fc6879b JM |
732 | if (val == 0) { |
733 | wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.", | |
734 | line); | |
735 | return -1; | |
736 | } | |
737 | return val; | |
738 | } | |
739 | ||
740 | ||
741 | #ifndef NO_CONFIG_WRITE | |
742 | static char * wpa_config_write_cipher(int cipher) | |
743 | { | |
0282a8c4 | 744 | char *buf = os_zalloc(50); |
6fc6879b JM |
745 | if (buf == NULL) |
746 | return NULL; | |
6fc6879b | 747 | |
0282a8c4 JM |
748 | if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) { |
749 | os_free(buf); | |
750 | return NULL; | |
6fc6879b JM |
751 | } |
752 | ||
753 | return buf; | |
754 | } | |
755 | #endif /* NO_CONFIG_WRITE */ | |
756 | ||
757 | ||
758 | static int wpa_config_parse_pairwise(const struct parse_data *data, | |
759 | struct wpa_ssid *ssid, int line, | |
760 | const char *value) | |
761 | { | |
762 | int val; | |
763 | val = wpa_config_parse_cipher(line, value); | |
764 | if (val == -1) | |
765 | return -1; | |
03145326 | 766 | if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) { |
6fc6879b JM |
767 | wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher " |
768 | "(0x%x).", line, val); | |
769 | return -1; | |
770 | } | |
771 | ||
772 | wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val); | |
773 | ssid->pairwise_cipher = val; | |
774 | return 0; | |
775 | } | |
776 | ||
777 | ||
778 | #ifndef NO_CONFIG_WRITE | |
779 | static char * wpa_config_write_pairwise(const struct parse_data *data, | |
780 | struct wpa_ssid *ssid) | |
781 | { | |
782 | return wpa_config_write_cipher(ssid->pairwise_cipher); | |
783 | } | |
784 | #endif /* NO_CONFIG_WRITE */ | |
785 | ||
786 | ||
787 | static int wpa_config_parse_group(const struct parse_data *data, | |
788 | struct wpa_ssid *ssid, int line, | |
789 | const char *value) | |
790 | { | |
791 | int val; | |
792 | val = wpa_config_parse_cipher(line, value); | |
793 | if (val == -1) | |
794 | return -1; | |
03145326 | 795 | if (val & ~WPA_ALLOWED_GROUP_CIPHERS) { |
6fc6879b JM |
796 | wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher " |
797 | "(0x%x).", line, val); | |
798 | return -1; | |
799 | } | |
800 | ||
801 | wpa_printf(MSG_MSGDUMP, "group: 0x%x", val); | |
802 | ssid->group_cipher = val; | |
803 | return 0; | |
804 | } | |
805 | ||
806 | ||
807 | #ifndef NO_CONFIG_WRITE | |
808 | static char * wpa_config_write_group(const struct parse_data *data, | |
809 | struct wpa_ssid *ssid) | |
810 | { | |
811 | return wpa_config_write_cipher(ssid->group_cipher); | |
812 | } | |
813 | #endif /* NO_CONFIG_WRITE */ | |
814 | ||
815 | ||
816 | static int wpa_config_parse_auth_alg(const struct parse_data *data, | |
817 | struct wpa_ssid *ssid, int line, | |
818 | const char *value) | |
819 | { | |
820 | int val = 0, last, errors = 0; | |
821 | char *start, *end, *buf; | |
822 | ||
823 | buf = os_strdup(value); | |
824 | if (buf == NULL) | |
825 | return -1; | |
826 | start = buf; | |
827 | ||
828 | while (*start != '\0') { | |
829 | while (*start == ' ' || *start == '\t') | |
830 | start++; | |
831 | if (*start == '\0') | |
832 | break; | |
833 | end = start; | |
834 | while (*end != ' ' && *end != '\t' && *end != '\0') | |
835 | end++; | |
836 | last = *end == '\0'; | |
837 | *end = '\0'; | |
838 | if (os_strcmp(start, "OPEN") == 0) | |
839 | val |= WPA_AUTH_ALG_OPEN; | |
840 | else if (os_strcmp(start, "SHARED") == 0) | |
841 | val |= WPA_AUTH_ALG_SHARED; | |
842 | else if (os_strcmp(start, "LEAP") == 0) | |
843 | val |= WPA_AUTH_ALG_LEAP; | |
844 | else { | |
845 | wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'", | |
846 | line, start); | |
847 | errors++; | |
848 | } | |
849 | ||
850 | if (last) | |
851 | break; | |
852 | start = end + 1; | |
853 | } | |
854 | os_free(buf); | |
855 | ||
856 | if (val == 0) { | |
857 | wpa_printf(MSG_ERROR, | |
858 | "Line %d: no auth_alg values configured.", line); | |
859 | errors++; | |
860 | } | |
861 | ||
862 | wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val); | |
863 | ssid->auth_alg = val; | |
864 | return errors ? -1 : 0; | |
865 | } | |
866 | ||
867 | ||
868 | #ifndef NO_CONFIG_WRITE | |
869 | static char * wpa_config_write_auth_alg(const struct parse_data *data, | |
870 | struct wpa_ssid *ssid) | |
871 | { | |
872 | char *buf, *pos, *end; | |
873 | int ret; | |
874 | ||
875 | pos = buf = os_zalloc(30); | |
876 | if (buf == NULL) | |
877 | return NULL; | |
878 | end = buf + 30; | |
879 | ||
880 | if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) { | |
881 | ret = os_snprintf(pos, end - pos, "%sOPEN", | |
882 | pos == buf ? "" : " "); | |
883 | if (ret < 0 || ret >= end - pos) { | |
884 | end[-1] = '\0'; | |
885 | return buf; | |
886 | } | |
887 | pos += ret; | |
888 | } | |
889 | ||
890 | if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) { | |
891 | ret = os_snprintf(pos, end - pos, "%sSHARED", | |
892 | pos == buf ? "" : " "); | |
893 | if (ret < 0 || ret >= end - pos) { | |
894 | end[-1] = '\0'; | |
895 | return buf; | |
896 | } | |
897 | pos += ret; | |
898 | } | |
899 | ||
900 | if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) { | |
901 | ret = os_snprintf(pos, end - pos, "%sLEAP", | |
902 | pos == buf ? "" : " "); | |
903 | if (ret < 0 || ret >= end - pos) { | |
904 | end[-1] = '\0'; | |
905 | return buf; | |
906 | } | |
907 | pos += ret; | |
908 | } | |
909 | ||
a1e46f32 JM |
910 | if (pos == buf) { |
911 | os_free(buf); | |
912 | buf = NULL; | |
913 | } | |
914 | ||
6fc6879b JM |
915 | return buf; |
916 | } | |
917 | #endif /* NO_CONFIG_WRITE */ | |
918 | ||
919 | ||
625f202a | 920 | static int * wpa_config_parse_int_array(const char *value) |
d3a98225 JM |
921 | { |
922 | int *freqs; | |
923 | size_t used, len; | |
924 | const char *pos; | |
925 | ||
926 | used = 0; | |
927 | len = 10; | |
f9884c09 | 928 | freqs = os_calloc(len + 1, sizeof(int)); |
d3a98225 | 929 | if (freqs == NULL) |
b766a9a2 | 930 | return NULL; |
d3a98225 JM |
931 | |
932 | pos = value; | |
933 | while (pos) { | |
934 | while (*pos == ' ') | |
935 | pos++; | |
936 | if (used == len) { | |
937 | int *n; | |
938 | size_t i; | |
067ffa26 | 939 | n = os_realloc_array(freqs, len * 2 + 1, sizeof(int)); |
d3a98225 JM |
940 | if (n == NULL) { |
941 | os_free(freqs); | |
b766a9a2 | 942 | return NULL; |
d3a98225 JM |
943 | } |
944 | for (i = len; i <= len * 2; i++) | |
945 | n[i] = 0; | |
946 | freqs = n; | |
947 | len *= 2; | |
948 | } | |
949 | ||
950 | freqs[used] = atoi(pos); | |
951 | if (freqs[used] == 0) | |
952 | break; | |
953 | used++; | |
954 | pos = os_strchr(pos + 1, ' '); | |
955 | } | |
956 | ||
b766a9a2 JM |
957 | return freqs; |
958 | } | |
959 | ||
960 | ||
961 | static int wpa_config_parse_scan_freq(const struct parse_data *data, | |
962 | struct wpa_ssid *ssid, int line, | |
963 | const char *value) | |
964 | { | |
965 | int *freqs; | |
966 | ||
625f202a | 967 | freqs = wpa_config_parse_int_array(value); |
b766a9a2 JM |
968 | if (freqs == NULL) |
969 | return -1; | |
6668efda JM |
970 | if (freqs[0] == 0) { |
971 | os_free(freqs); | |
972 | freqs = NULL; | |
973 | } | |
d3a98225 JM |
974 | os_free(ssid->scan_freq); |
975 | ssid->scan_freq = freqs; | |
976 | ||
977 | return 0; | |
978 | } | |
979 | ||
980 | ||
b766a9a2 JM |
981 | static int wpa_config_parse_freq_list(const struct parse_data *data, |
982 | struct wpa_ssid *ssid, int line, | |
983 | const char *value) | |
984 | { | |
985 | int *freqs; | |
986 | ||
625f202a | 987 | freqs = wpa_config_parse_int_array(value); |
b766a9a2 JM |
988 | if (freqs == NULL) |
989 | return -1; | |
6668efda JM |
990 | if (freqs[0] == 0) { |
991 | os_free(freqs); | |
992 | freqs = NULL; | |
993 | } | |
b766a9a2 JM |
994 | os_free(ssid->freq_list); |
995 | ssid->freq_list = freqs; | |
996 | ||
997 | return 0; | |
998 | } | |
999 | ||
1000 | ||
d3a98225 | 1001 | #ifndef NO_CONFIG_WRITE |
b766a9a2 JM |
1002 | static char * wpa_config_write_freqs(const struct parse_data *data, |
1003 | const int *freqs) | |
d3a98225 JM |
1004 | { |
1005 | char *buf, *pos, *end; | |
1006 | int i, ret; | |
1007 | size_t count; | |
1008 | ||
b766a9a2 | 1009 | if (freqs == NULL) |
d3a98225 JM |
1010 | return NULL; |
1011 | ||
1012 | count = 0; | |
b766a9a2 | 1013 | for (i = 0; freqs[i]; i++) |
d3a98225 JM |
1014 | count++; |
1015 | ||
1016 | pos = buf = os_zalloc(10 * count + 1); | |
1017 | if (buf == NULL) | |
1018 | return NULL; | |
1019 | end = buf + 10 * count + 1; | |
1020 | ||
b766a9a2 | 1021 | for (i = 0; freqs[i]; i++) { |
d3a98225 | 1022 | ret = os_snprintf(pos, end - pos, "%s%u", |
b766a9a2 | 1023 | i == 0 ? "" : " ", freqs[i]); |
d3a98225 JM |
1024 | if (ret < 0 || ret >= end - pos) { |
1025 | end[-1] = '\0'; | |
1026 | return buf; | |
1027 | } | |
1028 | pos += ret; | |
1029 | } | |
1030 | ||
1031 | return buf; | |
1032 | } | |
b766a9a2 JM |
1033 | |
1034 | ||
1035 | static char * wpa_config_write_scan_freq(const struct parse_data *data, | |
1036 | struct wpa_ssid *ssid) | |
1037 | { | |
1038 | return wpa_config_write_freqs(data, ssid->scan_freq); | |
1039 | } | |
1040 | ||
1041 | ||
1042 | static char * wpa_config_write_freq_list(const struct parse_data *data, | |
1043 | struct wpa_ssid *ssid) | |
1044 | { | |
1045 | return wpa_config_write_freqs(data, ssid->freq_list); | |
1046 | } | |
d3a98225 JM |
1047 | #endif /* NO_CONFIG_WRITE */ |
1048 | ||
1049 | ||
6fc6879b JM |
1050 | #ifdef IEEE8021X_EAPOL |
1051 | static int wpa_config_parse_eap(const struct parse_data *data, | |
1052 | struct wpa_ssid *ssid, int line, | |
1053 | const char *value) | |
1054 | { | |
1055 | int last, errors = 0; | |
1056 | char *start, *end, *buf; | |
1057 | struct eap_method_type *methods = NULL, *tmp; | |
1058 | size_t num_methods = 0; | |
1059 | ||
1060 | buf = os_strdup(value); | |
1061 | if (buf == NULL) | |
1062 | return -1; | |
1063 | start = buf; | |
1064 | ||
1065 | while (*start != '\0') { | |
1066 | while (*start == ' ' || *start == '\t') | |
1067 | start++; | |
1068 | if (*start == '\0') | |
1069 | break; | |
1070 | end = start; | |
1071 | while (*end != ' ' && *end != '\t' && *end != '\0') | |
1072 | end++; | |
1073 | last = *end == '\0'; | |
1074 | *end = '\0'; | |
1075 | tmp = methods; | |
067ffa26 JM |
1076 | methods = os_realloc_array(methods, num_methods + 1, |
1077 | sizeof(*methods)); | |
6fc6879b JM |
1078 | if (methods == NULL) { |
1079 | os_free(tmp); | |
1080 | os_free(buf); | |
1081 | return -1; | |
1082 | } | |
1083 | methods[num_methods].method = eap_peer_get_type( | |
1084 | start, &methods[num_methods].vendor); | |
1085 | if (methods[num_methods].vendor == EAP_VENDOR_IETF && | |
1086 | methods[num_methods].method == EAP_TYPE_NONE) { | |
1087 | wpa_printf(MSG_ERROR, "Line %d: unknown EAP method " | |
1088 | "'%s'", line, start); | |
1089 | wpa_printf(MSG_ERROR, "You may need to add support for" | |
1090 | " this EAP method during wpa_supplicant\n" | |
1091 | "build time configuration.\n" | |
1092 | "See README for more information."); | |
1093 | errors++; | |
1094 | } else if (methods[num_methods].vendor == EAP_VENDOR_IETF && | |
1095 | methods[num_methods].method == EAP_TYPE_LEAP) | |
1096 | ssid->leap++; | |
1097 | else | |
1098 | ssid->non_leap++; | |
1099 | num_methods++; | |
1100 | if (last) | |
1101 | break; | |
1102 | start = end + 1; | |
1103 | } | |
1104 | os_free(buf); | |
1105 | ||
1106 | tmp = methods; | |
067ffa26 | 1107 | methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods)); |
6fc6879b JM |
1108 | if (methods == NULL) { |
1109 | os_free(tmp); | |
1110 | return -1; | |
1111 | } | |
1112 | methods[num_methods].vendor = EAP_VENDOR_IETF; | |
1113 | methods[num_methods].method = EAP_TYPE_NONE; | |
1114 | num_methods++; | |
1115 | ||
1116 | wpa_hexdump(MSG_MSGDUMP, "eap methods", | |
1117 | (u8 *) methods, num_methods * sizeof(*methods)); | |
bb8b1bb0 | 1118 | os_free(ssid->eap.eap_methods); |
6fc6879b JM |
1119 | ssid->eap.eap_methods = methods; |
1120 | return errors ? -1 : 0; | |
1121 | } | |
1122 | ||
1123 | ||
1124 | static char * wpa_config_write_eap(const struct parse_data *data, | |
1125 | struct wpa_ssid *ssid) | |
1126 | { | |
1127 | int i, ret; | |
1128 | char *buf, *pos, *end; | |
1129 | const struct eap_method_type *eap_methods = ssid->eap.eap_methods; | |
1130 | const char *name; | |
1131 | ||
1132 | if (eap_methods == NULL) | |
1133 | return NULL; | |
1134 | ||
1135 | pos = buf = os_zalloc(100); | |
1136 | if (buf == NULL) | |
1137 | return NULL; | |
1138 | end = buf + 100; | |
1139 | ||
1140 | for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF || | |
1141 | eap_methods[i].method != EAP_TYPE_NONE; i++) { | |
1142 | name = eap_get_name(eap_methods[i].vendor, | |
1143 | eap_methods[i].method); | |
1144 | if (name) { | |
1145 | ret = os_snprintf(pos, end - pos, "%s%s", | |
1146 | pos == buf ? "" : " ", name); | |
1147 | if (ret < 0 || ret >= end - pos) | |
1148 | break; | |
1149 | pos += ret; | |
1150 | } | |
1151 | } | |
1152 | ||
1153 | end[-1] = '\0'; | |
1154 | ||
1155 | return buf; | |
1156 | } | |
1157 | ||
1158 | ||
1159 | static int wpa_config_parse_password(const struct parse_data *data, | |
1160 | struct wpa_ssid *ssid, int line, | |
1161 | const char *value) | |
1162 | { | |
1163 | u8 *hash; | |
1164 | ||
b56c0546 JM |
1165 | if (os_strcmp(value, "NULL") == 0) { |
1166 | wpa_printf(MSG_DEBUG, "Unset configuration string 'password'"); | |
19c48da0 | 1167 | bin_clear_free(ssid->eap.password, ssid->eap.password_len); |
b56c0546 JM |
1168 | ssid->eap.password = NULL; |
1169 | ssid->eap.password_len = 0; | |
1170 | return 0; | |
1171 | } | |
1172 | ||
0ebb23e3 JM |
1173 | #ifdef CONFIG_EXT_PASSWORD |
1174 | if (os_strncmp(value, "ext:", 4) == 0) { | |
1175 | char *name = os_strdup(value + 4); | |
1176 | if (name == NULL) | |
1177 | return -1; | |
19c48da0 | 1178 | bin_clear_free(ssid->eap.password, ssid->eap.password_len); |
0ebb23e3 JM |
1179 | ssid->eap.password = (u8 *) name; |
1180 | ssid->eap.password_len = os_strlen(name); | |
1181 | ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH; | |
1182 | ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD; | |
1183 | return 0; | |
1184 | } | |
1185 | #endif /* CONFIG_EXT_PASSWORD */ | |
1186 | ||
6fc6879b JM |
1187 | if (os_strncmp(value, "hash:", 5) != 0) { |
1188 | char *tmp; | |
1189 | size_t res_len; | |
1190 | ||
1191 | tmp = wpa_config_parse_string(value, &res_len); | |
1192 | if (tmp == NULL) { | |
1193 | wpa_printf(MSG_ERROR, "Line %d: failed to parse " | |
1194 | "password.", line); | |
1195 | return -1; | |
1196 | } | |
556f5a2a HS |
1197 | wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name, |
1198 | (u8 *) tmp, res_len); | |
6fc6879b | 1199 | |
19c48da0 | 1200 | bin_clear_free(ssid->eap.password, ssid->eap.password_len); |
6fc6879b JM |
1201 | ssid->eap.password = (u8 *) tmp; |
1202 | ssid->eap.password_len = res_len; | |
1203 | ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH; | |
0ebb23e3 | 1204 | ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD; |
6fc6879b JM |
1205 | |
1206 | return 0; | |
1207 | } | |
1208 | ||
1209 | ||
1210 | /* NtPasswordHash: hash:<32 hex digits> */ | |
1211 | if (os_strlen(value + 5) != 2 * 16) { | |
1212 | wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length " | |
1213 | "(expected 32 hex digits)", line); | |
1214 | return -1; | |
1215 | } | |
1216 | ||
1217 | hash = os_malloc(16); | |
1218 | if (hash == NULL) | |
1219 | return -1; | |
1220 | ||
1221 | if (hexstr2bin(value + 5, hash, 16)) { | |
1222 | os_free(hash); | |
1223 | wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line); | |
1224 | return -1; | |
1225 | } | |
1226 | ||
1227 | wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16); | |
1228 | ||
19c48da0 | 1229 | bin_clear_free(ssid->eap.password, ssid->eap.password_len); |
6fc6879b JM |
1230 | ssid->eap.password = hash; |
1231 | ssid->eap.password_len = 16; | |
1232 | ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH; | |
0ebb23e3 | 1233 | ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD; |
6fc6879b JM |
1234 | |
1235 | return 0; | |
1236 | } | |
1237 | ||
1238 | ||
1239 | static char * wpa_config_write_password(const struct parse_data *data, | |
1240 | struct wpa_ssid *ssid) | |
1241 | { | |
1242 | char *buf; | |
1243 | ||
1244 | if (ssid->eap.password == NULL) | |
1245 | return NULL; | |
1246 | ||
0ebb23e3 JM |
1247 | #ifdef CONFIG_EXT_PASSWORD |
1248 | if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) { | |
1249 | buf = os_zalloc(4 + ssid->eap.password_len + 1); | |
1250 | if (buf == NULL) | |
1251 | return NULL; | |
1252 | os_memcpy(buf, "ext:", 4); | |
1253 | os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len); | |
1254 | return buf; | |
1255 | } | |
1256 | #endif /* CONFIG_EXT_PASSWORD */ | |
1257 | ||
6fc6879b JM |
1258 | if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) { |
1259 | return wpa_config_write_string( | |
1260 | ssid->eap.password, ssid->eap.password_len); | |
1261 | } | |
1262 | ||
1263 | buf = os_malloc(5 + 32 + 1); | |
1264 | if (buf == NULL) | |
1265 | return NULL; | |
1266 | ||
1267 | os_memcpy(buf, "hash:", 5); | |
1268 | wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16); | |
1269 | ||
1270 | return buf; | |
1271 | } | |
1272 | #endif /* IEEE8021X_EAPOL */ | |
1273 | ||
1274 | ||
1275 | static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line, | |
1276 | const char *value, int idx) | |
1277 | { | |
1278 | char *buf, title[20]; | |
1279 | int res; | |
1280 | ||
1281 | buf = wpa_config_parse_string(value, len); | |
1282 | if (buf == NULL) { | |
1283 | wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.", | |
1284 | line, idx, value); | |
1285 | return -1; | |
1286 | } | |
1287 | if (*len > MAX_WEP_KEY_LEN) { | |
1288 | wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.", | |
1289 | line, idx, value); | |
1290 | os_free(buf); | |
1291 | return -1; | |
1292 | } | |
fea7c3a0 JM |
1293 | if (*len && *len != 5 && *len != 13 && *len != 16) { |
1294 | wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - " | |
1295 | "this network block will be ignored", | |
1296 | line, (unsigned int) *len); | |
1297 | } | |
6fc6879b | 1298 | os_memcpy(key, buf, *len); |
19c48da0 | 1299 | str_clear_free(buf); |
6fc6879b JM |
1300 | res = os_snprintf(title, sizeof(title), "wep_key%d", idx); |
1301 | if (res >= 0 && (size_t) res < sizeof(title)) | |
1302 | wpa_hexdump_key(MSG_MSGDUMP, title, key, *len); | |
1303 | return 0; | |
1304 | } | |
1305 | ||
1306 | ||
1307 | static int wpa_config_parse_wep_key0(const struct parse_data *data, | |
1308 | struct wpa_ssid *ssid, int line, | |
1309 | const char *value) | |
1310 | { | |
1311 | return wpa_config_parse_wep_key(ssid->wep_key[0], | |
1312 | &ssid->wep_key_len[0], line, | |
1313 | value, 0); | |
1314 | } | |
1315 | ||
1316 | ||
1317 | static int wpa_config_parse_wep_key1(const struct parse_data *data, | |
1318 | struct wpa_ssid *ssid, int line, | |
1319 | const char *value) | |
1320 | { | |
1321 | return wpa_config_parse_wep_key(ssid->wep_key[1], | |
1322 | &ssid->wep_key_len[1], line, | |
1323 | value, 1); | |
1324 | } | |
1325 | ||
1326 | ||
1327 | static int wpa_config_parse_wep_key2(const struct parse_data *data, | |
1328 | struct wpa_ssid *ssid, int line, | |
1329 | const char *value) | |
1330 | { | |
1331 | return wpa_config_parse_wep_key(ssid->wep_key[2], | |
1332 | &ssid->wep_key_len[2], line, | |
1333 | value, 2); | |
1334 | } | |
1335 | ||
1336 | ||
1337 | static int wpa_config_parse_wep_key3(const struct parse_data *data, | |
1338 | struct wpa_ssid *ssid, int line, | |
1339 | const char *value) | |
1340 | { | |
1341 | return wpa_config_parse_wep_key(ssid->wep_key[3], | |
1342 | &ssid->wep_key_len[3], line, | |
1343 | value, 3); | |
1344 | } | |
1345 | ||
1346 | ||
1347 | #ifndef NO_CONFIG_WRITE | |
1348 | static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx) | |
1349 | { | |
1350 | if (ssid->wep_key_len[idx] == 0) | |
1351 | return NULL; | |
1352 | return wpa_config_write_string(ssid->wep_key[idx], | |
1353 | ssid->wep_key_len[idx]); | |
1354 | } | |
1355 | ||
1356 | ||
1357 | static char * wpa_config_write_wep_key0(const struct parse_data *data, | |
1358 | struct wpa_ssid *ssid) | |
1359 | { | |
1360 | return wpa_config_write_wep_key(ssid, 0); | |
1361 | } | |
1362 | ||
1363 | ||
1364 | static char * wpa_config_write_wep_key1(const struct parse_data *data, | |
1365 | struct wpa_ssid *ssid) | |
1366 | { | |
1367 | return wpa_config_write_wep_key(ssid, 1); | |
1368 | } | |
1369 | ||
1370 | ||
1371 | static char * wpa_config_write_wep_key2(const struct parse_data *data, | |
1372 | struct wpa_ssid *ssid) | |
1373 | { | |
1374 | return wpa_config_write_wep_key(ssid, 2); | |
1375 | } | |
1376 | ||
1377 | ||
1378 | static char * wpa_config_write_wep_key3(const struct parse_data *data, | |
1379 | struct wpa_ssid *ssid) | |
1380 | { | |
1381 | return wpa_config_write_wep_key(ssid, 3); | |
1382 | } | |
1383 | #endif /* NO_CONFIG_WRITE */ | |
1384 | ||
1385 | ||
fbdcfd57 JM |
1386 | #ifdef CONFIG_P2P |
1387 | ||
9ec87666 JM |
1388 | static int wpa_config_parse_go_p2p_dev_addr(const struct parse_data *data, |
1389 | struct wpa_ssid *ssid, int line, | |
1390 | const char *value) | |
1391 | { | |
1392 | if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 || | |
1393 | os_strcmp(value, "any") == 0) { | |
1394 | os_memset(ssid->go_p2p_dev_addr, 0, ETH_ALEN); | |
1395 | wpa_printf(MSG_MSGDUMP, "GO P2P Device Address any"); | |
1396 | return 0; | |
1397 | } | |
1398 | if (hwaddr_aton(value, ssid->go_p2p_dev_addr)) { | |
1399 | wpa_printf(MSG_ERROR, "Line %d: Invalid GO P2P Device Address '%s'.", | |
1400 | line, value); | |
1401 | return -1; | |
1402 | } | |
1403 | ssid->bssid_set = 1; | |
1404 | wpa_printf(MSG_MSGDUMP, "GO P2P Device Address " MACSTR, | |
1405 | MAC2STR(ssid->go_p2p_dev_addr)); | |
1406 | return 0; | |
1407 | } | |
1408 | ||
1409 | ||
1410 | #ifndef NO_CONFIG_WRITE | |
1411 | static char * wpa_config_write_go_p2p_dev_addr(const struct parse_data *data, | |
1412 | struct wpa_ssid *ssid) | |
1413 | { | |
1414 | char *value; | |
1415 | int res; | |
1416 | ||
1417 | if (is_zero_ether_addr(ssid->go_p2p_dev_addr)) | |
1418 | return NULL; | |
1419 | ||
1420 | value = os_malloc(20); | |
1421 | if (value == NULL) | |
1422 | return NULL; | |
1423 | res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->go_p2p_dev_addr)); | |
1424 | if (res < 0 || res >= 20) { | |
1425 | os_free(value); | |
1426 | return NULL; | |
1427 | } | |
1428 | value[20 - 1] = '\0'; | |
1429 | return value; | |
1430 | } | |
1431 | #endif /* NO_CONFIG_WRITE */ | |
1432 | ||
1433 | ||
fbdcfd57 JM |
1434 | static int wpa_config_parse_p2p_client_list(const struct parse_data *data, |
1435 | struct wpa_ssid *ssid, int line, | |
1436 | const char *value) | |
1437 | { | |
1438 | const char *pos; | |
1439 | u8 *buf, *n, addr[ETH_ALEN]; | |
1440 | size_t count; | |
1441 | ||
1442 | buf = NULL; | |
1443 | count = 0; | |
1444 | ||
1445 | pos = value; | |
1446 | while (pos && *pos) { | |
1447 | while (*pos == ' ') | |
1448 | pos++; | |
1449 | ||
1450 | if (hwaddr_aton(pos, addr)) { | |
22316795 JM |
1451 | if (count == 0) { |
1452 | wpa_printf(MSG_ERROR, "Line %d: Invalid " | |
1453 | "p2p_client_list address '%s'.", | |
1454 | line, value); | |
1455 | os_free(buf); | |
1456 | return -1; | |
1457 | } | |
1458 | /* continue anyway since this could have been from a | |
1459 | * truncated configuration file line */ | |
1460 | wpa_printf(MSG_INFO, "Line %d: Ignore likely " | |
1461 | "truncated p2p_client_list address '%s'", | |
1462 | line, pos); | |
fbdcfd57 | 1463 | } else { |
067ffa26 | 1464 | n = os_realloc_array(buf, count + 1, ETH_ALEN); |
fbdcfd57 JM |
1465 | if (n == NULL) { |
1466 | os_free(buf); | |
1467 | return -1; | |
1468 | } | |
1469 | buf = n; | |
22316795 JM |
1470 | os_memmove(buf + ETH_ALEN, buf, count * ETH_ALEN); |
1471 | os_memcpy(buf, addr, ETH_ALEN); | |
fbdcfd57 JM |
1472 | count++; |
1473 | wpa_hexdump(MSG_MSGDUMP, "p2p_client_list", | |
1474 | addr, ETH_ALEN); | |
1475 | } | |
1476 | ||
1477 | pos = os_strchr(pos, ' '); | |
1478 | } | |
1479 | ||
1480 | os_free(ssid->p2p_client_list); | |
1481 | ssid->p2p_client_list = buf; | |
1482 | ssid->num_p2p_clients = count; | |
1483 | ||
1484 | return 0; | |
1485 | } | |
1486 | ||
1487 | ||
1488 | #ifndef NO_CONFIG_WRITE | |
1489 | static char * wpa_config_write_p2p_client_list(const struct parse_data *data, | |
1490 | struct wpa_ssid *ssid) | |
1491 | { | |
1492 | char *value, *end, *pos; | |
1493 | int res; | |
1494 | size_t i; | |
1495 | ||
1496 | if (ssid->p2p_client_list == NULL || ssid->num_p2p_clients == 0) | |
1497 | return NULL; | |
1498 | ||
1499 | value = os_malloc(20 * ssid->num_p2p_clients); | |
1500 | if (value == NULL) | |
1501 | return NULL; | |
1502 | pos = value; | |
1503 | end = value + 20 * ssid->num_p2p_clients; | |
1504 | ||
22316795 | 1505 | for (i = ssid->num_p2p_clients; i > 0; i--) { |
fbdcfd57 JM |
1506 | res = os_snprintf(pos, end - pos, MACSTR " ", |
1507 | MAC2STR(ssid->p2p_client_list + | |
22316795 | 1508 | (i - 1) * ETH_ALEN)); |
fbdcfd57 JM |
1509 | if (res < 0 || res >= end - pos) { |
1510 | os_free(value); | |
1511 | return NULL; | |
1512 | } | |
1513 | pos += res; | |
1514 | } | |
1515 | ||
1516 | if (pos > value) | |
1517 | pos[-1] = '\0'; | |
1518 | ||
1519 | return value; | |
1520 | } | |
1521 | #endif /* NO_CONFIG_WRITE */ | |
1522 | ||
01a57fe4 JM |
1523 | |
1524 | static int wpa_config_parse_psk_list(const struct parse_data *data, | |
1525 | struct wpa_ssid *ssid, int line, | |
1526 | const char *value) | |
1527 | { | |
1528 | struct psk_list_entry *p; | |
1529 | const char *pos; | |
1530 | ||
1531 | p = os_zalloc(sizeof(*p)); | |
1532 | if (p == NULL) | |
1533 | return -1; | |
1534 | ||
1535 | pos = value; | |
1536 | if (os_strncmp(pos, "P2P-", 4) == 0) { | |
1537 | p->p2p = 1; | |
1538 | pos += 4; | |
1539 | } | |
1540 | ||
1541 | if (hwaddr_aton(pos, p->addr)) { | |
1542 | wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list address '%s'", | |
1543 | line, pos); | |
1544 | os_free(p); | |
1545 | return -1; | |
1546 | } | |
1547 | pos += 17; | |
1548 | if (*pos != '-') { | |
1549 | wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list '%s'", | |
1550 | line, pos); | |
1551 | os_free(p); | |
1552 | return -1; | |
1553 | } | |
1554 | pos++; | |
1555 | ||
1556 | if (hexstr2bin(pos, p->psk, PMK_LEN) || pos[PMK_LEN * 2] != '\0') { | |
1557 | wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list PSK '%s'", | |
1558 | line, pos); | |
1559 | os_free(p); | |
1560 | return -1; | |
1561 | } | |
1562 | ||
1563 | dl_list_add(&ssid->psk_list, &p->list); | |
1564 | ||
1565 | return 0; | |
1566 | } | |
1567 | ||
1568 | ||
1569 | #ifndef NO_CONFIG_WRITE | |
1570 | static char * wpa_config_write_psk_list(const struct parse_data *data, | |
1571 | struct wpa_ssid *ssid) | |
1572 | { | |
1573 | return NULL; | |
1574 | } | |
1575 | #endif /* NO_CONFIG_WRITE */ | |
1576 | ||
fbdcfd57 JM |
1577 | #endif /* CONFIG_P2P */ |
1578 | ||
6fc6879b JM |
1579 | /* Helper macros for network block parser */ |
1580 | ||
1581 | #ifdef OFFSET | |
1582 | #undef OFFSET | |
1583 | #endif /* OFFSET */ | |
1584 | /* OFFSET: Get offset of a variable within the wpa_ssid structure */ | |
1585 | #define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v) | |
1586 | ||
1587 | /* STR: Define a string variable for an ASCII string; f = field name */ | |
1588 | #ifdef NO_CONFIG_WRITE | |
1589 | #define _STR(f) #f, wpa_config_parse_str, OFFSET(f) | |
1590 | #define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f) | |
1591 | #else /* NO_CONFIG_WRITE */ | |
1592 | #define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f) | |
1593 | #define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f) | |
1594 | #endif /* NO_CONFIG_WRITE */ | |
1595 | #define STR(f) _STR(f), NULL, NULL, NULL, 0 | |
1596 | #define STRe(f) _STRe(f), NULL, NULL, NULL, 0 | |
1597 | #define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1 | |
1598 | #define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1 | |
1599 | ||
1600 | /* STR_LEN: Define a string variable with a separate variable for storing the | |
1601 | * data length. Unlike STR(), this can be used to store arbitrary binary data | |
1602 | * (i.e., even nul termination character). */ | |
1603 | #define _STR_LEN(f) _STR(f), OFFSET(f ## _len) | |
1604 | #define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len) | |
1605 | #define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0 | |
1606 | #define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0 | |
1607 | #define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1 | |
1608 | ||
1609 | /* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length | |
1610 | * explicitly specified. */ | |
1611 | #define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max) | |
1612 | #define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0 | |
1613 | #define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1 | |
1614 | ||
1615 | #ifdef NO_CONFIG_WRITE | |
1616 | #define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0 | |
1617 | #define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0 | |
1618 | #else /* NO_CONFIG_WRITE */ | |
1619 | #define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \ | |
1620 | OFFSET(f), (void *) 0 | |
1621 | #define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \ | |
1622 | OFFSET(eap.f), (void *) 0 | |
1623 | #endif /* NO_CONFIG_WRITE */ | |
1624 | ||
1625 | /* INT: Define an integer variable */ | |
1626 | #define INT(f) _INT(f), NULL, NULL, 0 | |
1627 | #define INTe(f) _INTe(f), NULL, NULL, 0 | |
1628 | ||
1629 | /* INT_RANGE: Define an integer variable with allowed value range */ | |
1630 | #define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0 | |
1631 | ||
1632 | /* FUNC: Define a configuration variable that uses a custom function for | |
1633 | * parsing and writing the value. */ | |
1634 | #ifdef NO_CONFIG_WRITE | |
1635 | #define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL | |
1636 | #else /* NO_CONFIG_WRITE */ | |
1637 | #define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \ | |
1638 | NULL, NULL, NULL, NULL | |
1639 | #endif /* NO_CONFIG_WRITE */ | |
1640 | #define FUNC(f) _FUNC(f), 0 | |
1641 | #define FUNC_KEY(f) _FUNC(f), 1 | |
1642 | ||
1643 | /* | |
1644 | * Table of network configuration variables. This table is used to parse each | |
1645 | * network configuration variable, e.g., each line in wpa_supplicant.conf file | |
1646 | * that is inside a network block. | |
1647 | * | |
1648 | * This table is generated using the helper macros defined above and with | |
1649 | * generous help from the C pre-processor. The field name is stored as a string | |
1650 | * into .name and for STR and INT types, the offset of the target buffer within | |
1651 | * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar | |
1652 | * offset to the field containing the length of the configuration variable. | |
1653 | * .param3 and .param4 can be used to mark the allowed range (length for STR | |
1654 | * and value for INT). | |
1655 | * | |
1656 | * For each configuration line in wpa_supplicant.conf, the parser goes through | |
1657 | * this table and select the entry that matches with the field name. The parser | |
1658 | * function (.parser) is then called to parse the actual value of the field. | |
1659 | * | |
1660 | * This kind of mechanism makes it easy to add new configuration parameters, | |
1661 | * since only one line needs to be added into this table and into the | |
1662 | * struct wpa_ssid definition if the new variable is either a string or | |
1663 | * integer. More complex types will need to use their own parser and writer | |
1664 | * functions. | |
1665 | */ | |
1666 | static const struct parse_data ssid_fields[] = { | |
1667 | { STR_RANGE(ssid, 0, MAX_SSID_LEN) }, | |
1668 | { INT_RANGE(scan_ssid, 0, 1) }, | |
1669 | { FUNC(bssid) }, | |
1670 | { FUNC_KEY(psk) }, | |
1671 | { FUNC(proto) }, | |
1672 | { FUNC(key_mgmt) }, | |
1f6c0ab8 | 1673 | { INT(bg_scan_period) }, |
6fc6879b JM |
1674 | { FUNC(pairwise) }, |
1675 | { FUNC(group) }, | |
1676 | { FUNC(auth_alg) }, | |
d3a98225 | 1677 | { FUNC(scan_freq) }, |
b766a9a2 | 1678 | { FUNC(freq_list) }, |
6fc6879b JM |
1679 | #ifdef IEEE8021X_EAPOL |
1680 | { FUNC(eap) }, | |
1681 | { STR_LENe(identity) }, | |
1682 | { STR_LENe(anonymous_identity) }, | |
556f5a2a | 1683 | { FUNC_KEY(password) }, |
6fc6879b JM |
1684 | { STRe(ca_cert) }, |
1685 | { STRe(ca_path) }, | |
1686 | { STRe(client_cert) }, | |
1687 | { STRe(private_key) }, | |
1688 | { STR_KEYe(private_key_passwd) }, | |
1689 | { STRe(dh_file) }, | |
1690 | { STRe(subject_match) }, | |
1691 | { STRe(altsubject_match) }, | |
01f809c7 | 1692 | { STRe(domain_suffix_match) }, |
6fc6879b JM |
1693 | { STRe(ca_cert2) }, |
1694 | { STRe(ca_path2) }, | |
1695 | { STRe(client_cert2) }, | |
1696 | { STRe(private_key2) }, | |
1697 | { STR_KEYe(private_key2_passwd) }, | |
1698 | { STRe(dh_file2) }, | |
1699 | { STRe(subject_match2) }, | |
1700 | { STRe(altsubject_match2) }, | |
01f809c7 | 1701 | { STRe(domain_suffix_match2) }, |
6fc6879b JM |
1702 | { STRe(phase1) }, |
1703 | { STRe(phase2) }, | |
1704 | { STRe(pcsc) }, | |
1705 | { STR_KEYe(pin) }, | |
1706 | { STRe(engine_id) }, | |
1707 | { STRe(key_id) }, | |
61ee0f71 DS |
1708 | { STRe(cert_id) }, |
1709 | { STRe(ca_cert_id) }, | |
98842d51 CL |
1710 | { STR_KEYe(pin2) }, |
1711 | { STRe(engine2_id) }, | |
61ee0f71 DS |
1712 | { STRe(key2_id) }, |
1713 | { STRe(cert2_id) }, | |
1714 | { STRe(ca_cert2_id) }, | |
6fc6879b | 1715 | { INTe(engine) }, |
98842d51 | 1716 | { INTe(engine2) }, |
6fc6879b | 1717 | { INT(eapol_flags) }, |
13f6a07e | 1718 | { INTe(sim_num) }, |
07e2de31 | 1719 | { STRe(openssl_ciphers) }, |
6fc6879b JM |
1720 | #endif /* IEEE8021X_EAPOL */ |
1721 | { FUNC_KEY(wep_key0) }, | |
1722 | { FUNC_KEY(wep_key1) }, | |
1723 | { FUNC_KEY(wep_key2) }, | |
1724 | { FUNC_KEY(wep_key3) }, | |
1725 | { INT(wep_tx_keyidx) }, | |
1726 | { INT(priority) }, | |
1727 | #ifdef IEEE8021X_EAPOL | |
1728 | { INT(eap_workaround) }, | |
1729 | { STRe(pac_file) }, | |
1730 | { INTe(fragment_size) }, | |
080585c0 | 1731 | { INTe(ocsp) }, |
6fc6879b | 1732 | #endif /* IEEE8021X_EAPOL */ |
2c5d725c | 1733 | { INT_RANGE(mode, 0, 4) }, |
6fc6879b | 1734 | { INT_RANGE(proactive_key_caching, 0, 1) }, |
4dac0245 | 1735 | { INT_RANGE(disabled, 0, 2) }, |
6fc6879b JM |
1736 | { STR(id_str) }, |
1737 | #ifdef CONFIG_IEEE80211W | |
1738 | { INT_RANGE(ieee80211w, 0, 2) }, | |
1739 | #endif /* CONFIG_IEEE80211W */ | |
1740 | { INT_RANGE(peerkey, 0, 1) }, | |
1741 | { INT_RANGE(mixed_cell, 0, 1) }, | |
7829894c | 1742 | { INT_RANGE(frequency, 0, 65000) }, |
60b94c98 JM |
1743 | { INT(wpa_ptk_rekey) }, |
1744 | { STR(bgscan) }, | |
e62f4ed0 | 1745 | { INT_RANGE(ignore_broadcast_ssid, 0, 2) }, |
fbdcfd57 | 1746 | #ifdef CONFIG_P2P |
9ec87666 | 1747 | { FUNC(go_p2p_dev_addr) }, |
fbdcfd57 | 1748 | { FUNC(p2p_client_list) }, |
01a57fe4 | 1749 | { FUNC(psk_list) }, |
fbdcfd57 | 1750 | #endif /* CONFIG_P2P */ |
80e8a5ee BG |
1751 | #ifdef CONFIG_HT_OVERRIDES |
1752 | { INT_RANGE(disable_ht, 0, 1) }, | |
1753 | { INT_RANGE(disable_ht40, -1, 1) }, | |
a90497f8 | 1754 | { INT_RANGE(disable_sgi, 0, 1) }, |
39a5800f | 1755 | { INT_RANGE(disable_ldpc, 0, 1) }, |
d41cc8cc | 1756 | { INT_RANGE(ht40_intolerant, 0, 1) }, |
80e8a5ee BG |
1757 | { INT_RANGE(disable_max_amsdu, -1, 1) }, |
1758 | { INT_RANGE(ampdu_factor, -1, 3) }, | |
1759 | { INT_RANGE(ampdu_density, -1, 7) }, | |
1760 | { STR(ht_mcs) }, | |
1761 | #endif /* CONFIG_HT_OVERRIDES */ | |
e9ee8dc3 JB |
1762 | #ifdef CONFIG_VHT_OVERRIDES |
1763 | { INT_RANGE(disable_vht, 0, 1) }, | |
1764 | { INT(vht_capa) }, | |
1765 | { INT(vht_capa_mask) }, | |
1766 | { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) }, | |
1767 | { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) }, | |
1768 | { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) }, | |
1769 | { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) }, | |
1770 | { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) }, | |
1771 | { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) }, | |
1772 | { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) }, | |
1773 | { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) }, | |
1774 | { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) }, | |
1775 | { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) }, | |
1776 | { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) }, | |
1777 | { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) }, | |
1778 | { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) }, | |
1779 | { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) }, | |
1780 | { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) }, | |
1781 | { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) }, | |
1782 | #endif /* CONFIG_VHT_OVERRIDES */ | |
07f53b8c | 1783 | { INT(ap_max_inactivity) }, |
fdfb1c8b | 1784 | { INT(dtim_period) }, |
18206e02 | 1785 | { INT(beacon_int) }, |
dd10abcc HW |
1786 | #ifdef CONFIG_MACSEC |
1787 | { INT_RANGE(macsec_policy, 0, 1) }, | |
1788 | #endif /* CONFIG_MACSEC */ | |
e376290c DS |
1789 | #ifdef CONFIG_HS20 |
1790 | { INT(update_identifier) }, | |
1791 | #endif /* CONFIG_HS20 */ | |
a313d17d | 1792 | { INT_RANGE(mac_addr, 0, 2) }, |
6fc6879b JM |
1793 | }; |
1794 | ||
1795 | #undef OFFSET | |
1796 | #undef _STR | |
1797 | #undef STR | |
1798 | #undef STR_KEY | |
1799 | #undef _STR_LEN | |
1800 | #undef STR_LEN | |
1801 | #undef STR_LEN_KEY | |
1802 | #undef _STR_RANGE | |
1803 | #undef STR_RANGE | |
1804 | #undef STR_RANGE_KEY | |
1805 | #undef _INT | |
1806 | #undef INT | |
1807 | #undef INT_RANGE | |
1808 | #undef _FUNC | |
1809 | #undef FUNC | |
1810 | #undef FUNC_KEY | |
e7ecab4a | 1811 | #define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields) |
6fc6879b JM |
1812 | |
1813 | ||
1814 | /** | |
1815 | * wpa_config_add_prio_network - Add a network to priority lists | |
1816 | * @config: Configuration data from wpa_config_read() | |
1817 | * @ssid: Pointer to the network configuration to be added to the list | |
1818 | * Returns: 0 on success, -1 on failure | |
1819 | * | |
1820 | * This function is used to add a network block to the priority list of | |
1821 | * networks. This must be called for each network when reading in the full | |
1822 | * configuration. In addition, this can be used indirectly when updating | |
1823 | * priorities by calling wpa_config_update_prio_list(). | |
1824 | */ | |
1825 | int wpa_config_add_prio_network(struct wpa_config *config, | |
1826 | struct wpa_ssid *ssid) | |
1827 | { | |
1828 | int prio; | |
1829 | struct wpa_ssid *prev, **nlist; | |
1830 | ||
1831 | /* | |
1832 | * Add to an existing priority list if one is available for the | |
1833 | * configured priority level for this network. | |
1834 | */ | |
1835 | for (prio = 0; prio < config->num_prio; prio++) { | |
1836 | prev = config->pssid[prio]; | |
1837 | if (prev->priority == ssid->priority) { | |
1838 | while (prev->pnext) | |
1839 | prev = prev->pnext; | |
1840 | prev->pnext = ssid; | |
1841 | return 0; | |
1842 | } | |
1843 | } | |
1844 | ||
1845 | /* First network for this priority - add a new priority list */ | |
067ffa26 JM |
1846 | nlist = os_realloc_array(config->pssid, config->num_prio + 1, |
1847 | sizeof(struct wpa_ssid *)); | |
6fc6879b JM |
1848 | if (nlist == NULL) |
1849 | return -1; | |
1850 | ||
1851 | for (prio = 0; prio < config->num_prio; prio++) { | |
2c60ca73 JM |
1852 | if (nlist[prio]->priority < ssid->priority) { |
1853 | os_memmove(&nlist[prio + 1], &nlist[prio], | |
1854 | (config->num_prio - prio) * | |
1855 | sizeof(struct wpa_ssid *)); | |
6fc6879b | 1856 | break; |
2c60ca73 | 1857 | } |
6fc6879b JM |
1858 | } |
1859 | ||
6fc6879b JM |
1860 | nlist[prio] = ssid; |
1861 | config->num_prio++; | |
1862 | config->pssid = nlist; | |
1863 | ||
1864 | return 0; | |
1865 | } | |
1866 | ||
1867 | ||
1868 | /** | |
1869 | * wpa_config_update_prio_list - Update network priority list | |
1870 | * @config: Configuration data from wpa_config_read() | |
1871 | * Returns: 0 on success, -1 on failure | |
1872 | * | |
1873 | * This function is called to update the priority list of networks in the | |
1874 | * configuration when a network is being added or removed. This is also called | |
1875 | * if a priority for a network is changed. | |
1876 | */ | |
aa53509f | 1877 | int wpa_config_update_prio_list(struct wpa_config *config) |
6fc6879b JM |
1878 | { |
1879 | struct wpa_ssid *ssid; | |
1880 | int ret = 0; | |
1881 | ||
1882 | os_free(config->pssid); | |
1883 | config->pssid = NULL; | |
1884 | config->num_prio = 0; | |
1885 | ||
1886 | ssid = config->ssid; | |
1887 | while (ssid) { | |
1888 | ssid->pnext = NULL; | |
1889 | if (wpa_config_add_prio_network(config, ssid) < 0) | |
1890 | ret = -1; | |
1891 | ssid = ssid->next; | |
1892 | } | |
1893 | ||
1894 | return ret; | |
1895 | } | |
1896 | ||
1897 | ||
1898 | #ifdef IEEE8021X_EAPOL | |
1899 | static void eap_peer_config_free(struct eap_peer_config *eap) | |
1900 | { | |
1901 | os_free(eap->eap_methods); | |
19c48da0 | 1902 | bin_clear_free(eap->identity, eap->identity_len); |
6fc6879b | 1903 | os_free(eap->anonymous_identity); |
19c48da0 | 1904 | bin_clear_free(eap->password, eap->password_len); |
6fc6879b JM |
1905 | os_free(eap->ca_cert); |
1906 | os_free(eap->ca_path); | |
1907 | os_free(eap->client_cert); | |
1908 | os_free(eap->private_key); | |
19c48da0 | 1909 | str_clear_free(eap->private_key_passwd); |
6fc6879b JM |
1910 | os_free(eap->dh_file); |
1911 | os_free(eap->subject_match); | |
1912 | os_free(eap->altsubject_match); | |
01f809c7 | 1913 | os_free(eap->domain_suffix_match); |
6fc6879b JM |
1914 | os_free(eap->ca_cert2); |
1915 | os_free(eap->ca_path2); | |
1916 | os_free(eap->client_cert2); | |
1917 | os_free(eap->private_key2); | |
19c48da0 | 1918 | str_clear_free(eap->private_key2_passwd); |
6fc6879b JM |
1919 | os_free(eap->dh_file2); |
1920 | os_free(eap->subject_match2); | |
1921 | os_free(eap->altsubject_match2); | |
01f809c7 | 1922 | os_free(eap->domain_suffix_match2); |
6fc6879b JM |
1923 | os_free(eap->phase1); |
1924 | os_free(eap->phase2); | |
1925 | os_free(eap->pcsc); | |
19c48da0 | 1926 | str_clear_free(eap->pin); |
6fc6879b JM |
1927 | os_free(eap->engine_id); |
1928 | os_free(eap->key_id); | |
61ee0f71 DS |
1929 | os_free(eap->cert_id); |
1930 | os_free(eap->ca_cert_id); | |
1931 | os_free(eap->key2_id); | |
1932 | os_free(eap->cert2_id); | |
1933 | os_free(eap->ca_cert2_id); | |
19c48da0 | 1934 | str_clear_free(eap->pin2); |
98842d51 | 1935 | os_free(eap->engine2_id); |
6fc6879b JM |
1936 | os_free(eap->otp); |
1937 | os_free(eap->pending_req_otp); | |
1938 | os_free(eap->pac_file); | |
19c48da0 JM |
1939 | bin_clear_free(eap->new_password, eap->new_password_len); |
1940 | str_clear_free(eap->external_sim_resp); | |
07e2de31 | 1941 | os_free(eap->openssl_ciphers); |
6fc6879b JM |
1942 | } |
1943 | #endif /* IEEE8021X_EAPOL */ | |
1944 | ||
1945 | ||
1946 | /** | |
1947 | * wpa_config_free_ssid - Free network/ssid configuration data | |
1948 | * @ssid: Configuration data for the network | |
1949 | * | |
1950 | * This function frees all resources allocated for the network configuration | |
1951 | * data. | |
1952 | */ | |
1953 | void wpa_config_free_ssid(struct wpa_ssid *ssid) | |
1954 | { | |
01a57fe4 JM |
1955 | struct psk_list_entry *psk; |
1956 | ||
6fc6879b | 1957 | os_free(ssid->ssid); |
19c48da0 JM |
1958 | os_memset(ssid->psk, 0, sizeof(ssid->psk)); |
1959 | str_clear_free(ssid->passphrase); | |
9173b16f | 1960 | os_free(ssid->ext_psk); |
6fc6879b JM |
1961 | #ifdef IEEE8021X_EAPOL |
1962 | eap_peer_config_free(&ssid->eap); | |
1963 | #endif /* IEEE8021X_EAPOL */ | |
1964 | os_free(ssid->id_str); | |
d3a98225 | 1965 | os_free(ssid->scan_freq); |
b766a9a2 | 1966 | os_free(ssid->freq_list); |
60b94c98 | 1967 | os_free(ssid->bgscan); |
fbdcfd57 | 1968 | os_free(ssid->p2p_client_list); |
80e8a5ee BG |
1969 | #ifdef CONFIG_HT_OVERRIDES |
1970 | os_free(ssid->ht_mcs); | |
1971 | #endif /* CONFIG_HT_OVERRIDES */ | |
01a57fe4 JM |
1972 | while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry, |
1973 | list))) { | |
1974 | dl_list_del(&psk->list); | |
1975 | os_free(psk); | |
1976 | } | |
6fc6879b JM |
1977 | os_free(ssid); |
1978 | } | |
1979 | ||
1980 | ||
1bb7b8e8 JM |
1981 | void wpa_config_free_cred(struct wpa_cred *cred) |
1982 | { | |
463c8ffb JM |
1983 | size_t i; |
1984 | ||
1bb7b8e8 | 1985 | os_free(cred->realm); |
19c48da0 JM |
1986 | str_clear_free(cred->username); |
1987 | str_clear_free(cred->password); | |
1bb7b8e8 | 1988 | os_free(cred->ca_cert); |
11e4f46a JM |
1989 | os_free(cred->client_cert); |
1990 | os_free(cred->private_key); | |
19c48da0 | 1991 | str_clear_free(cred->private_key_passwd); |
1bb7b8e8 | 1992 | os_free(cred->imsi); |
19c48da0 | 1993 | str_clear_free(cred->milenage); |
463c8ffb JM |
1994 | for (i = 0; i < cred->num_domain; i++) |
1995 | os_free(cred->domain[i]); | |
1bb7b8e8 | 1996 | os_free(cred->domain); |
ac1bc549 | 1997 | os_free(cred->domain_suffix_match); |
8ca93c59 JM |
1998 | os_free(cred->eap_method); |
1999 | os_free(cred->phase1); | |
2000 | os_free(cred->phase2); | |
dbea8ac7 | 2001 | os_free(cred->excluded_ssid); |
bc00053c | 2002 | os_free(cred->roaming_partner); |
aa26ba68 | 2003 | os_free(cred->provisioning_sp); |
33fb8c52 JM |
2004 | for (i = 0; i < cred->num_req_conn_capab; i++) |
2005 | os_free(cred->req_conn_capab_port[i]); | |
2006 | os_free(cred->req_conn_capab_port); | |
2007 | os_free(cred->req_conn_capab_proto); | |
1bb7b8e8 JM |
2008 | os_free(cred); |
2009 | } | |
2010 | ||
2011 | ||
d9bb2821 JM |
2012 | void wpa_config_flush_blobs(struct wpa_config *config) |
2013 | { | |
2014 | #ifndef CONFIG_NO_CONFIG_BLOBS | |
2015 | struct wpa_config_blob *blob, *prev; | |
2016 | ||
2017 | blob = config->blobs; | |
2018 | config->blobs = NULL; | |
2019 | while (blob) { | |
2020 | prev = blob; | |
2021 | blob = blob->next; | |
2022 | wpa_config_free_blob(prev); | |
2023 | } | |
2024 | #endif /* CONFIG_NO_CONFIG_BLOBS */ | |
2025 | } | |
2026 | ||
2027 | ||
6fc6879b JM |
2028 | /** |
2029 | * wpa_config_free - Free configuration data | |
2030 | * @config: Configuration data from wpa_config_read() | |
2031 | * | |
2032 | * This function frees all resources allocated for the configuration data by | |
2033 | * wpa_config_read(). | |
2034 | */ | |
2035 | void wpa_config_free(struct wpa_config *config) | |
2036 | { | |
6fc6879b | 2037 | struct wpa_ssid *ssid, *prev = NULL; |
1bb7b8e8 | 2038 | struct wpa_cred *cred, *cprev; |
e3768e7c | 2039 | |
6fc6879b JM |
2040 | ssid = config->ssid; |
2041 | while (ssid) { | |
2042 | prev = ssid; | |
2043 | ssid = ssid->next; | |
2044 | wpa_config_free_ssid(prev); | |
2045 | } | |
2046 | ||
1bb7b8e8 JM |
2047 | cred = config->cred; |
2048 | while (cred) { | |
2049 | cprev = cred; | |
2050 | cred = cred->next; | |
2051 | wpa_config_free_cred(cprev); | |
2052 | } | |
2053 | ||
d9bb2821 | 2054 | wpa_config_flush_blobs(config); |
6fc6879b | 2055 | |
71dd3b78 | 2056 | wpabuf_free(config->wps_vendor_ext_m1); |
6fc6879b JM |
2057 | os_free(config->ctrl_interface); |
2058 | os_free(config->ctrl_interface_group); | |
6fc6879b JM |
2059 | os_free(config->opensc_engine_path); |
2060 | os_free(config->pkcs11_engine_path); | |
2061 | os_free(config->pkcs11_module_path); | |
07e2de31 | 2062 | os_free(config->openssl_ciphers); |
f64adcd7 | 2063 | os_free(config->pcsc_reader); |
19c48da0 | 2064 | str_clear_free(config->pcsc_pin); |
6fc6879b | 2065 | os_free(config->driver_param); |
3c0b7aa4 JM |
2066 | os_free(config->device_name); |
2067 | os_free(config->manufacturer); | |
2068 | os_free(config->model_name); | |
2069 | os_free(config->model_number); | |
2070 | os_free(config->serial_number); | |
c0e4dd9e | 2071 | os_free(config->config_methods); |
e3768e7c | 2072 | os_free(config->p2p_ssid_postfix); |
6fc6879b | 2073 | os_free(config->pssid); |
21d996f7 | 2074 | os_free(config->p2p_pref_chan); |
556b30da | 2075 | os_free(config->p2p_no_go_freq.range); |
b0786fba | 2076 | os_free(config->autoscan); |
f5ffc348 | 2077 | os_free(config->freq_list); |
3f2c8ba6 JM |
2078 | wpabuf_free(config->wps_nfc_dh_pubkey); |
2079 | wpabuf_free(config->wps_nfc_dh_privkey); | |
2080 | wpabuf_free(config->wps_nfc_dev_pw); | |
306ae225 | 2081 | os_free(config->ext_password_backend); |
625f202a | 2082 | os_free(config->sae_groups); |
18a2eaab | 2083 | wpabuf_free(config->ap_vendor_elements); |
b572df86 | 2084 | os_free(config->osu_dir); |
e4fa8b12 | 2085 | os_free(config->wowlan_triggers); |
6fc6879b JM |
2086 | os_free(config); |
2087 | } | |
2088 | ||
2089 | ||
7c49fdd0 SL |
2090 | /** |
2091 | * wpa_config_foreach_network - Iterate over each configured network | |
2092 | * @config: Configuration data from wpa_config_read() | |
2093 | * @func: Callback function to process each network | |
2094 | * @arg: Opaque argument to pass to callback function | |
2095 | * | |
2096 | * Iterate over the set of configured networks calling the specified | |
2097 | * function for each item. We guard against callbacks removing the | |
2098 | * supplied network. | |
2099 | */ | |
2100 | void wpa_config_foreach_network(struct wpa_config *config, | |
2101 | void (*func)(void *, struct wpa_ssid *), | |
2102 | void *arg) | |
2103 | { | |
2104 | struct wpa_ssid *ssid, *next; | |
2105 | ||
2106 | ssid = config->ssid; | |
2107 | while (ssid) { | |
2108 | next = ssid->next; | |
2109 | func(arg, ssid); | |
2110 | ssid = next; | |
2111 | } | |
2112 | } | |
2113 | ||
2114 | ||
6fc6879b JM |
2115 | /** |
2116 | * wpa_config_get_network - Get configured network based on id | |
2117 | * @config: Configuration data from wpa_config_read() | |
2118 | * @id: Unique network id to search for | |
2119 | * Returns: Network configuration or %NULL if not found | |
2120 | */ | |
2121 | struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id) | |
2122 | { | |
2123 | struct wpa_ssid *ssid; | |
2124 | ||
2125 | ssid = config->ssid; | |
2126 | while (ssid) { | |
2127 | if (id == ssid->id) | |
2128 | break; | |
2129 | ssid = ssid->next; | |
2130 | } | |
2131 | ||
2132 | return ssid; | |
2133 | } | |
2134 | ||
2135 | ||
2136 | /** | |
2137 | * wpa_config_add_network - Add a new network with empty configuration | |
2138 | * @config: Configuration data from wpa_config_read() | |
2139 | * Returns: The new network configuration or %NULL if operation failed | |
2140 | */ | |
2141 | struct wpa_ssid * wpa_config_add_network(struct wpa_config *config) | |
2142 | { | |
2143 | int id; | |
2144 | struct wpa_ssid *ssid, *last = NULL; | |
2145 | ||
2146 | id = -1; | |
2147 | ssid = config->ssid; | |
2148 | while (ssid) { | |
2149 | if (ssid->id > id) | |
2150 | id = ssid->id; | |
2151 | last = ssid; | |
2152 | ssid = ssid->next; | |
2153 | } | |
2154 | id++; | |
2155 | ||
2156 | ssid = os_zalloc(sizeof(*ssid)); | |
2157 | if (ssid == NULL) | |
2158 | return NULL; | |
2159 | ssid->id = id; | |
01a57fe4 | 2160 | dl_list_init(&ssid->psk_list); |
6fc6879b JM |
2161 | if (last) |
2162 | last->next = ssid; | |
2163 | else | |
2164 | config->ssid = ssid; | |
2165 | ||
2166 | wpa_config_update_prio_list(config); | |
2167 | ||
2168 | return ssid; | |
2169 | } | |
2170 | ||
2171 | ||
2172 | /** | |
2173 | * wpa_config_remove_network - Remove a configured network based on id | |
2174 | * @config: Configuration data from wpa_config_read() | |
2175 | * @id: Unique network id to search for | |
2176 | * Returns: 0 on success, or -1 if the network was not found | |
2177 | */ | |
2178 | int wpa_config_remove_network(struct wpa_config *config, int id) | |
2179 | { | |
2180 | struct wpa_ssid *ssid, *prev = NULL; | |
2181 | ||
2182 | ssid = config->ssid; | |
2183 | while (ssid) { | |
2184 | if (id == ssid->id) | |
2185 | break; | |
2186 | prev = ssid; | |
2187 | ssid = ssid->next; | |
2188 | } | |
2189 | ||
2190 | if (ssid == NULL) | |
2191 | return -1; | |
2192 | ||
2193 | if (prev) | |
2194 | prev->next = ssid->next; | |
2195 | else | |
2196 | config->ssid = ssid->next; | |
2197 | ||
2198 | wpa_config_update_prio_list(config); | |
2199 | wpa_config_free_ssid(ssid); | |
2200 | return 0; | |
2201 | } | |
2202 | ||
2203 | ||
2204 | /** | |
2205 | * wpa_config_set_network_defaults - Set network default values | |
2206 | * @ssid: Pointer to network configuration data | |
2207 | */ | |
2208 | void wpa_config_set_network_defaults(struct wpa_ssid *ssid) | |
2209 | { | |
2210 | ssid->proto = DEFAULT_PROTO; | |
2211 | ssid->pairwise_cipher = DEFAULT_PAIRWISE; | |
2212 | ssid->group_cipher = DEFAULT_GROUP; | |
2213 | ssid->key_mgmt = DEFAULT_KEY_MGMT; | |
1f6c0ab8 | 2214 | ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD; |
6fc6879b JM |
2215 | #ifdef IEEE8021X_EAPOL |
2216 | ssid->eapol_flags = DEFAULT_EAPOL_FLAGS; | |
2217 | ssid->eap_workaround = DEFAULT_EAP_WORKAROUND; | |
2218 | ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE; | |
13f6a07e | 2219 | ssid->eap.sim_num = DEFAULT_USER_SELECTED_SIM; |
6fc6879b | 2220 | #endif /* IEEE8021X_EAPOL */ |
80e8a5ee BG |
2221 | #ifdef CONFIG_HT_OVERRIDES |
2222 | ssid->disable_ht = DEFAULT_DISABLE_HT; | |
2223 | ssid->disable_ht40 = DEFAULT_DISABLE_HT40; | |
a90497f8 | 2224 | ssid->disable_sgi = DEFAULT_DISABLE_SGI; |
39a5800f | 2225 | ssid->disable_ldpc = DEFAULT_DISABLE_LDPC; |
80e8a5ee BG |
2226 | ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU; |
2227 | ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR; | |
2228 | ssid->ampdu_density = DEFAULT_AMPDU_DENSITY; | |
2229 | #endif /* CONFIG_HT_OVERRIDES */ | |
e9ee8dc3 JB |
2230 | #ifdef CONFIG_VHT_OVERRIDES |
2231 | ssid->vht_rx_mcs_nss_1 = -1; | |
2232 | ssid->vht_rx_mcs_nss_2 = -1; | |
2233 | ssid->vht_rx_mcs_nss_3 = -1; | |
2234 | ssid->vht_rx_mcs_nss_4 = -1; | |
2235 | ssid->vht_rx_mcs_nss_5 = -1; | |
2236 | ssid->vht_rx_mcs_nss_6 = -1; | |
2237 | ssid->vht_rx_mcs_nss_7 = -1; | |
2238 | ssid->vht_rx_mcs_nss_8 = -1; | |
2239 | ssid->vht_tx_mcs_nss_1 = -1; | |
2240 | ssid->vht_tx_mcs_nss_2 = -1; | |
2241 | ssid->vht_tx_mcs_nss_3 = -1; | |
2242 | ssid->vht_tx_mcs_nss_4 = -1; | |
2243 | ssid->vht_tx_mcs_nss_5 = -1; | |
2244 | ssid->vht_tx_mcs_nss_6 = -1; | |
2245 | ssid->vht_tx_mcs_nss_7 = -1; | |
2246 | ssid->vht_tx_mcs_nss_8 = -1; | |
2247 | #endif /* CONFIG_VHT_OVERRIDES */ | |
6e202021 | 2248 | ssid->proactive_key_caching = -1; |
62d49803 JM |
2249 | #ifdef CONFIG_IEEE80211W |
2250 | ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT; | |
2251 | #endif /* CONFIG_IEEE80211W */ | |
c267753b | 2252 | ssid->mac_addr = -1; |
6fc6879b JM |
2253 | } |
2254 | ||
2255 | ||
2256 | /** | |
2257 | * wpa_config_set - Set a variable in network configuration | |
2258 | * @ssid: Pointer to network configuration data | |
2259 | * @var: Variable name, e.g., "ssid" | |
2260 | * @value: Variable value | |
2261 | * @line: Line number in configuration file or 0 if not used | |
2262 | * Returns: 0 on success, -1 on failure | |
2263 | * | |
2264 | * This function can be used to set network configuration variables based on | |
2265 | * both the configuration file and management interface input. The value | |
2266 | * parameter must be in the same format as the text-based configuration file is | |
2267 | * using. For example, strings are using double quotation marks. | |
2268 | */ | |
2269 | int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value, | |
2270 | int line) | |
2271 | { | |
2272 | size_t i; | |
2273 | int ret = 0; | |
2274 | ||
2275 | if (ssid == NULL || var == NULL || value == NULL) | |
2276 | return -1; | |
2277 | ||
2278 | for (i = 0; i < NUM_SSID_FIELDS; i++) { | |
2279 | const struct parse_data *field = &ssid_fields[i]; | |
2280 | if (os_strcmp(var, field->name) != 0) | |
2281 | continue; | |
2282 | ||
2283 | if (field->parser(field, ssid, line, value)) { | |
2284 | if (line) { | |
2285 | wpa_printf(MSG_ERROR, "Line %d: failed to " | |
2286 | "parse %s '%s'.", line, var, value); | |
2287 | } | |
2288 | ret = -1; | |
2289 | } | |
2290 | break; | |
2291 | } | |
2292 | if (i == NUM_SSID_FIELDS) { | |
2293 | if (line) { | |
2294 | wpa_printf(MSG_ERROR, "Line %d: unknown network field " | |
2295 | "'%s'.", line, var); | |
2296 | } | |
2297 | ret = -1; | |
2298 | } | |
2299 | ||
2300 | return ret; | |
2301 | } | |
2302 | ||
2303 | ||
67e1b984 JM |
2304 | int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var, |
2305 | const char *value) | |
2306 | { | |
2307 | size_t len; | |
2308 | char *buf; | |
2309 | int ret; | |
2310 | ||
2311 | len = os_strlen(value); | |
2312 | buf = os_malloc(len + 3); | |
2313 | if (buf == NULL) | |
2314 | return -1; | |
2315 | buf[0] = '"'; | |
2316 | os_memcpy(buf + 1, value, len); | |
2317 | buf[len + 1] = '"'; | |
2318 | buf[len + 2] = '\0'; | |
2319 | ret = wpa_config_set(ssid, var, buf, 0); | |
2320 | os_free(buf); | |
2321 | return ret; | |
2322 | } | |
2323 | ||
2324 | ||
3d3d3056 WS |
2325 | /** |
2326 | * wpa_config_get_all - Get all options from network configuration | |
2327 | * @ssid: Pointer to network configuration data | |
2328 | * @get_keys: Determines if keys/passwords will be included in returned list | |
d1c8ac88 | 2329 | * (if they may be exported) |
3d3d3056 WS |
2330 | * Returns: %NULL terminated list of all set keys and their values in the form |
2331 | * of [key1, val1, key2, val2, ... , NULL] | |
2332 | * | |
2333 | * This function can be used to get list of all configured network properties. | |
2334 | * The caller is responsible for freeing the returned list and all its | |
2335 | * elements. | |
2336 | */ | |
2337 | char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys) | |
2338 | { | |
2339 | const struct parse_data *field; | |
2340 | char *key, *value; | |
2341 | size_t i; | |
2342 | char **props; | |
2343 | int fields_num; | |
2344 | ||
d1c8ac88 JB |
2345 | get_keys = get_keys && ssid->export_keys; |
2346 | ||
f9884c09 | 2347 | props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *)); |
3d3d3056 WS |
2348 | if (!props) |
2349 | return NULL; | |
2350 | ||
2351 | fields_num = 0; | |
2352 | for (i = 0; i < NUM_SSID_FIELDS; i++) { | |
2353 | field = &ssid_fields[i]; | |
2354 | if (field->key_data && !get_keys) | |
2355 | continue; | |
2356 | value = field->writer(field, ssid); | |
04746865 | 2357 | if (value == NULL) |
3d3d3056 | 2358 | continue; |
04746865 JM |
2359 | if (os_strlen(value) == 0) { |
2360 | os_free(value); | |
2361 | continue; | |
2362 | } | |
3d3d3056 WS |
2363 | |
2364 | key = os_strdup(field->name); | |
04746865 JM |
2365 | if (key == NULL) { |
2366 | os_free(value); | |
3d3d3056 | 2367 | goto err; |
04746865 | 2368 | } |
3d3d3056 WS |
2369 | |
2370 | props[fields_num * 2] = key; | |
2371 | props[fields_num * 2 + 1] = value; | |
2372 | ||
2373 | fields_num++; | |
2374 | } | |
2375 | ||
2376 | return props; | |
2377 | ||
2378 | err: | |
2379 | value = *props; | |
2380 | while (value) | |
2381 | os_free(value++); | |
2382 | os_free(props); | |
2383 | return NULL; | |
2384 | } | |
2385 | ||
2386 | ||
6fc6879b JM |
2387 | #ifndef NO_CONFIG_WRITE |
2388 | /** | |
2389 | * wpa_config_get - Get a variable in network configuration | |
2390 | * @ssid: Pointer to network configuration data | |
2391 | * @var: Variable name, e.g., "ssid" | |
2392 | * Returns: Value of the variable or %NULL on failure | |
2393 | * | |
2394 | * This function can be used to get network configuration variables. The | |
2395 | * returned value is a copy of the configuration variable in text format, i.e,. | |
2396 | * the same format that the text-based configuration file and wpa_config_set() | |
2397 | * are using for the value. The caller is responsible for freeing the returned | |
2398 | * value. | |
2399 | */ | |
2400 | char * wpa_config_get(struct wpa_ssid *ssid, const char *var) | |
2401 | { | |
2402 | size_t i; | |
2403 | ||
2404 | if (ssid == NULL || var == NULL) | |
2405 | return NULL; | |
2406 | ||
2407 | for (i = 0; i < NUM_SSID_FIELDS; i++) { | |
2408 | const struct parse_data *field = &ssid_fields[i]; | |
2409 | if (os_strcmp(var, field->name) == 0) | |
2410 | return field->writer(field, ssid); | |
2411 | } | |
2412 | ||
2413 | return NULL; | |
2414 | } | |
2415 | ||
2416 | ||
2417 | /** | |
2418 | * wpa_config_get_no_key - Get a variable in network configuration (no keys) | |
2419 | * @ssid: Pointer to network configuration data | |
2420 | * @var: Variable name, e.g., "ssid" | |
2421 | * Returns: Value of the variable or %NULL on failure | |
2422 | * | |
2423 | * This function can be used to get network configuration variable like | |
2424 | * wpa_config_get(). The only difference is that this functions does not expose | |
2425 | * key/password material from the configuration. In case a key/password field | |
2426 | * is requested, the returned value is an empty string or %NULL if the variable | |
2427 | * is not set or "*" if the variable is set (regardless of its value). The | |
2428 | * returned value is a copy of the configuration variable in text format, i.e,. | |
2429 | * the same format that the text-based configuration file and wpa_config_set() | |
2430 | * are using for the value. The caller is responsible for freeing the returned | |
2431 | * value. | |
2432 | */ | |
2433 | char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var) | |
2434 | { | |
2435 | size_t i; | |
2436 | ||
2437 | if (ssid == NULL || var == NULL) | |
2438 | return NULL; | |
2439 | ||
2440 | for (i = 0; i < NUM_SSID_FIELDS; i++) { | |
2441 | const struct parse_data *field = &ssid_fields[i]; | |
2442 | if (os_strcmp(var, field->name) == 0) { | |
2443 | char *res = field->writer(field, ssid); | |
2444 | if (field->key_data) { | |
2445 | if (res && res[0]) { | |
2446 | wpa_printf(MSG_DEBUG, "Do not allow " | |
2447 | "key_data field to be " | |
2448 | "exposed"); | |
19c48da0 | 2449 | str_clear_free(res); |
6fc6879b JM |
2450 | return os_strdup("*"); |
2451 | } | |
2452 | ||
2453 | os_free(res); | |
2454 | return NULL; | |
2455 | } | |
2456 | return res; | |
2457 | } | |
2458 | } | |
2459 | ||
2460 | return NULL; | |
2461 | } | |
2462 | #endif /* NO_CONFIG_WRITE */ | |
2463 | ||
2464 | ||
2465 | /** | |
2466 | * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID | |
2467 | * @ssid: Pointer to network configuration data | |
2468 | * | |
2469 | * This function must be called to update WPA PSK when either SSID or the | |
2470 | * passphrase has changed for the network configuration. | |
2471 | */ | |
2472 | void wpa_config_update_psk(struct wpa_ssid *ssid) | |
2473 | { | |
2474 | #ifndef CONFIG_NO_PBKDF2 | |
986de33d | 2475 | pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096, |
6fc6879b JM |
2476 | ssid->psk, PMK_LEN); |
2477 | wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)", | |
2478 | ssid->psk, PMK_LEN); | |
2479 | ssid->psk_set = 1; | |
2480 | #endif /* CONFIG_NO_PBKDF2 */ | |
2481 | } | |
2482 | ||
2483 | ||
33fb8c52 JM |
2484 | static int wpa_config_set_cred_req_conn_capab(struct wpa_cred *cred, |
2485 | const char *value) | |
2486 | { | |
2487 | u8 *proto; | |
2488 | int **port; | |
2489 | int *ports, *nports; | |
2490 | const char *pos; | |
2491 | unsigned int num_ports; | |
2492 | ||
2493 | proto = os_realloc_array(cred->req_conn_capab_proto, | |
2494 | cred->num_req_conn_capab + 1, sizeof(u8)); | |
2495 | if (proto == NULL) | |
2496 | return -1; | |
2497 | cred->req_conn_capab_proto = proto; | |
2498 | ||
2499 | port = os_realloc_array(cred->req_conn_capab_port, | |
2500 | cred->num_req_conn_capab + 1, sizeof(int *)); | |
2501 | if (port == NULL) | |
2502 | return -1; | |
2503 | cred->req_conn_capab_port = port; | |
2504 | ||
2505 | proto[cred->num_req_conn_capab] = atoi(value); | |
2506 | ||
2507 | pos = os_strchr(value, ':'); | |
2508 | if (pos == NULL) { | |
2509 | port[cred->num_req_conn_capab] = NULL; | |
2510 | cred->num_req_conn_capab++; | |
2511 | return 0; | |
2512 | } | |
2513 | pos++; | |
2514 | ||
2515 | ports = NULL; | |
2516 | num_ports = 0; | |
2517 | ||
2518 | while (*pos) { | |
2519 | nports = os_realloc_array(ports, num_ports + 1, sizeof(int)); | |
2520 | if (nports == NULL) { | |
2521 | os_free(ports); | |
2522 | return -1; | |
2523 | } | |
2524 | ports = nports; | |
2525 | ports[num_ports++] = atoi(pos); | |
2526 | ||
2527 | pos = os_strchr(pos, ','); | |
2528 | if (pos == NULL) | |
2529 | break; | |
2530 | pos++; | |
2531 | } | |
2532 | ||
2533 | nports = os_realloc_array(ports, num_ports + 1, sizeof(int)); | |
2534 | if (nports == NULL) { | |
2535 | os_free(ports); | |
2536 | return -1; | |
2537 | } | |
2538 | ports = nports; | |
2539 | ports[num_ports] = -1; | |
2540 | ||
2541 | port[cred->num_req_conn_capab] = ports; | |
2542 | cred->num_req_conn_capab++; | |
2543 | return 0; | |
2544 | } | |
2545 | ||
2546 | ||
1bb7b8e8 JM |
2547 | int wpa_config_set_cred(struct wpa_cred *cred, const char *var, |
2548 | const char *value, int line) | |
2549 | { | |
2550 | char *val; | |
2551 | size_t len; | |
2552 | ||
03ed3324 JM |
2553 | if (os_strcmp(var, "temporary") == 0) { |
2554 | cred->temporary = atoi(value); | |
2555 | return 0; | |
2556 | } | |
2557 | ||
1a712d2f JM |
2558 | if (os_strcmp(var, "priority") == 0) { |
2559 | cred->priority = atoi(value); | |
2560 | return 0; | |
2561 | } | |
2562 | ||
74794891 JM |
2563 | if (os_strcmp(var, "sp_priority") == 0) { |
2564 | int prio = atoi(value); | |
2565 | if (prio < 0 || prio > 255) | |
2566 | return -1; | |
2567 | cred->sp_priority = prio; | |
2568 | return 0; | |
2569 | } | |
2570 | ||
d7b01abd JM |
2571 | if (os_strcmp(var, "pcsc") == 0) { |
2572 | cred->pcsc = atoi(value); | |
2573 | return 0; | |
2574 | } | |
2575 | ||
8ca93c59 JM |
2576 | if (os_strcmp(var, "eap") == 0) { |
2577 | struct eap_method_type method; | |
2578 | method.method = eap_peer_get_type(value, &method.vendor); | |
2579 | if (method.vendor == EAP_VENDOR_IETF && | |
2580 | method.method == EAP_TYPE_NONE) { | |
2581 | wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' " | |
2582 | "for a credential", line, value); | |
2583 | return -1; | |
2584 | } | |
2585 | os_free(cred->eap_method); | |
2586 | cred->eap_method = os_malloc(sizeof(*cred->eap_method)); | |
2587 | if (cred->eap_method == NULL) | |
2588 | return -1; | |
2589 | os_memcpy(cred->eap_method, &method, sizeof(method)); | |
2590 | return 0; | |
2591 | } | |
2592 | ||
02af9c90 JM |
2593 | if (os_strcmp(var, "password") == 0 && |
2594 | os_strncmp(value, "ext:", 4) == 0) { | |
19c48da0 | 2595 | str_clear_free(cred->password); |
02af9c90 JM |
2596 | cred->password = os_strdup(value); |
2597 | cred->ext_password = 1; | |
2598 | return 0; | |
2599 | } | |
2600 | ||
f9cd147d JM |
2601 | if (os_strcmp(var, "update_identifier") == 0) { |
2602 | cred->update_identifier = atoi(value); | |
2603 | return 0; | |
2604 | } | |
2605 | ||
4cad9df1 JM |
2606 | if (os_strcmp(var, "min_dl_bandwidth_home") == 0) { |
2607 | cred->min_dl_bandwidth_home = atoi(value); | |
2608 | return 0; | |
2609 | } | |
2610 | ||
2611 | if (os_strcmp(var, "min_ul_bandwidth_home") == 0) { | |
2612 | cred->min_ul_bandwidth_home = atoi(value); | |
2613 | return 0; | |
2614 | } | |
2615 | ||
2616 | if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0) { | |
2617 | cred->min_dl_bandwidth_roaming = atoi(value); | |
2618 | return 0; | |
2619 | } | |
2620 | ||
2621 | if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0) { | |
2622 | cred->min_ul_bandwidth_roaming = atoi(value); | |
2623 | return 0; | |
2624 | } | |
2625 | ||
a45b2dc5 JM |
2626 | if (os_strcmp(var, "max_bss_load") == 0) { |
2627 | cred->max_bss_load = atoi(value); | |
2628 | return 0; | |
2629 | } | |
2630 | ||
33fb8c52 JM |
2631 | if (os_strcmp(var, "req_conn_capab") == 0) |
2632 | return wpa_config_set_cred_req_conn_capab(cred, value); | |
2633 | ||
cf6d08a6 JM |
2634 | if (os_strcmp(var, "ocsp") == 0) { |
2635 | cred->ocsp = atoi(value); | |
2636 | return 0; | |
2637 | } | |
2638 | ||
13f6a07e NJ |
2639 | if (os_strcmp(var, "sim_num") == 0) { |
2640 | cred->sim_num = atoi(value); | |
2641 | return 0; | |
2642 | } | |
2643 | ||
1bb7b8e8 | 2644 | val = wpa_config_parse_string(value, &len); |
7d86e537 JM |
2645 | if (val == NULL) { |
2646 | wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string " | |
2647 | "value '%s'.", line, var, value); | |
1bb7b8e8 | 2648 | return -1; |
7d86e537 | 2649 | } |
1bb7b8e8 JM |
2650 | |
2651 | if (os_strcmp(var, "realm") == 0) { | |
2652 | os_free(cred->realm); | |
2653 | cred->realm = val; | |
2654 | return 0; | |
2655 | } | |
2656 | ||
2657 | if (os_strcmp(var, "username") == 0) { | |
19c48da0 | 2658 | str_clear_free(cred->username); |
1bb7b8e8 JM |
2659 | cred->username = val; |
2660 | return 0; | |
2661 | } | |
2662 | ||
2663 | if (os_strcmp(var, "password") == 0) { | |
19c48da0 | 2664 | str_clear_free(cred->password); |
1bb7b8e8 | 2665 | cred->password = val; |
02af9c90 | 2666 | cred->ext_password = 0; |
1bb7b8e8 JM |
2667 | return 0; |
2668 | } | |
2669 | ||
2670 | if (os_strcmp(var, "ca_cert") == 0) { | |
2671 | os_free(cred->ca_cert); | |
2672 | cred->ca_cert = val; | |
2673 | return 0; | |
2674 | } | |
2675 | ||
11e4f46a JM |
2676 | if (os_strcmp(var, "client_cert") == 0) { |
2677 | os_free(cred->client_cert); | |
2678 | cred->client_cert = val; | |
2679 | return 0; | |
2680 | } | |
2681 | ||
2682 | if (os_strcmp(var, "private_key") == 0) { | |
2683 | os_free(cred->private_key); | |
2684 | cred->private_key = val; | |
2685 | return 0; | |
2686 | } | |
2687 | ||
2688 | if (os_strcmp(var, "private_key_passwd") == 0) { | |
19c48da0 | 2689 | str_clear_free(cred->private_key_passwd); |
11e4f46a JM |
2690 | cred->private_key_passwd = val; |
2691 | return 0; | |
2692 | } | |
2693 | ||
1bb7b8e8 JM |
2694 | if (os_strcmp(var, "imsi") == 0) { |
2695 | os_free(cred->imsi); | |
2696 | cred->imsi = val; | |
2697 | return 0; | |
2698 | } | |
2699 | ||
2700 | if (os_strcmp(var, "milenage") == 0) { | |
19c48da0 | 2701 | str_clear_free(cred->milenage); |
1bb7b8e8 JM |
2702 | cred->milenage = val; |
2703 | return 0; | |
2704 | } | |
2705 | ||
ac1bc549 JM |
2706 | if (os_strcmp(var, "domain_suffix_match") == 0) { |
2707 | os_free(cred->domain_suffix_match); | |
2708 | cred->domain_suffix_match = val; | |
2709 | return 0; | |
2710 | } | |
2711 | ||
1bb7b8e8 | 2712 | if (os_strcmp(var, "domain") == 0) { |
463c8ffb JM |
2713 | char **new_domain; |
2714 | new_domain = os_realloc_array(cred->domain, | |
2715 | cred->num_domain + 1, | |
2716 | sizeof(char *)); | |
2717 | if (new_domain == NULL) { | |
2718 | os_free(val); | |
2719 | return -1; | |
2720 | } | |
2721 | new_domain[cred->num_domain++] = val; | |
2722 | cred->domain = new_domain; | |
1bb7b8e8 JM |
2723 | return 0; |
2724 | } | |
2725 | ||
8ca93c59 JM |
2726 | if (os_strcmp(var, "phase1") == 0) { |
2727 | os_free(cred->phase1); | |
2728 | cred->phase1 = val; | |
2729 | return 0; | |
2730 | } | |
2731 | ||
2732 | if (os_strcmp(var, "phase2") == 0) { | |
2733 | os_free(cred->phase2); | |
2734 | cred->phase2 = val; | |
2735 | return 0; | |
2736 | } | |
2737 | ||
955567bc JM |
2738 | if (os_strcmp(var, "roaming_consortium") == 0) { |
2739 | if (len < 3 || len > sizeof(cred->roaming_consortium)) { | |
2740 | wpa_printf(MSG_ERROR, "Line %d: invalid " | |
2741 | "roaming_consortium length %d (3..15 " | |
2742 | "expected)", line, (int) len); | |
2743 | os_free(val); | |
2744 | return -1; | |
2745 | } | |
2746 | os_memcpy(cred->roaming_consortium, val, len); | |
2747 | cred->roaming_consortium_len = len; | |
2748 | os_free(val); | |
2749 | return 0; | |
2750 | } | |
2751 | ||
f47c1452 JM |
2752 | if (os_strcmp(var, "required_roaming_consortium") == 0) { |
2753 | if (len < 3 || len > sizeof(cred->required_roaming_consortium)) | |
2754 | { | |
2755 | wpa_printf(MSG_ERROR, "Line %d: invalid " | |
2756 | "required_roaming_consortium length %d " | |
2757 | "(3..15 expected)", line, (int) len); | |
2758 | os_free(val); | |
2759 | return -1; | |
2760 | } | |
2761 | os_memcpy(cred->required_roaming_consortium, val, len); | |
2762 | cred->required_roaming_consortium_len = len; | |
2763 | os_free(val); | |
2764 | return 0; | |
2765 | } | |
2766 | ||
dbea8ac7 JM |
2767 | if (os_strcmp(var, "excluded_ssid") == 0) { |
2768 | struct excluded_ssid *e; | |
2769 | ||
2770 | if (len > MAX_SSID_LEN) { | |
2771 | wpa_printf(MSG_ERROR, "Line %d: invalid " | |
2772 | "excluded_ssid length %d", line, (int) len); | |
2773 | os_free(val); | |
2774 | return -1; | |
2775 | } | |
2776 | ||
2777 | e = os_realloc_array(cred->excluded_ssid, | |
2778 | cred->num_excluded_ssid + 1, | |
2779 | sizeof(struct excluded_ssid)); | |
2780 | if (e == NULL) { | |
2781 | os_free(val); | |
2782 | return -1; | |
2783 | } | |
2784 | cred->excluded_ssid = e; | |
2785 | ||
2786 | e = &cred->excluded_ssid[cred->num_excluded_ssid++]; | |
2787 | os_memcpy(e->ssid, val, len); | |
2788 | e->ssid_len = len; | |
2789 | ||
2790 | os_free(val); | |
2791 | ||
2792 | return 0; | |
2793 | } | |
2794 | ||
bc00053c JM |
2795 | if (os_strcmp(var, "roaming_partner") == 0) { |
2796 | struct roaming_partner *p; | |
2797 | char *pos; | |
2798 | ||
2799 | p = os_realloc_array(cred->roaming_partner, | |
2800 | cred->num_roaming_partner + 1, | |
2801 | sizeof(struct roaming_partner)); | |
2802 | if (p == NULL) { | |
2803 | os_free(val); | |
2804 | return -1; | |
2805 | } | |
2806 | cred->roaming_partner = p; | |
2807 | ||
2808 | p = &cred->roaming_partner[cred->num_roaming_partner]; | |
2809 | ||
2810 | pos = os_strchr(val, ','); | |
2811 | if (pos == NULL) { | |
2812 | os_free(val); | |
2813 | return -1; | |
2814 | } | |
2815 | *pos++ = '\0'; | |
2816 | if (pos - val - 1 >= (int) sizeof(p->fqdn)) { | |
2817 | os_free(val); | |
2818 | return -1; | |
2819 | } | |
2820 | os_memcpy(p->fqdn, val, pos - val); | |
2821 | ||
2822 | p->exact_match = atoi(pos); | |
2823 | ||
2824 | pos = os_strchr(pos, ','); | |
2825 | if (pos == NULL) { | |
2826 | os_free(val); | |
2827 | return -1; | |
2828 | } | |
2829 | *pos++ = '\0'; | |
2830 | ||
2831 | p->priority = atoi(pos); | |
2832 | ||
2833 | pos = os_strchr(pos, ','); | |
2834 | if (pos == NULL) { | |
2835 | os_free(val); | |
2836 | return -1; | |
2837 | } | |
2838 | *pos++ = '\0'; | |
2839 | ||
2840 | if (os_strlen(pos) >= sizeof(p->country)) { | |
2841 | os_free(val); | |
2842 | return -1; | |
2843 | } | |
2844 | os_memcpy(p->country, pos, os_strlen(pos) + 1); | |
2845 | ||
2846 | cred->num_roaming_partner++; | |
2847 | os_free(val); | |
2848 | ||
2849 | return 0; | |
2850 | } | |
2851 | ||
aa26ba68 JM |
2852 | if (os_strcmp(var, "provisioning_sp") == 0) { |
2853 | os_free(cred->provisioning_sp); | |
2854 | cred->provisioning_sp = val; | |
2855 | return 0; | |
2856 | } | |
2857 | ||
1bb7b8e8 JM |
2858 | if (line) { |
2859 | wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.", | |
2860 | line, var); | |
2861 | } | |
2862 | ||
f4b2d69b JM |
2863 | os_free(val); |
2864 | ||
1bb7b8e8 JM |
2865 | return -1; |
2866 | } | |
2867 | ||
2868 | ||
8f03ac90 | 2869 | static char * alloc_int_str(int val) |
c880ab87 JM |
2870 | { |
2871 | char *buf; | |
2872 | ||
2873 | buf = os_malloc(20); | |
2874 | if (buf == NULL) | |
2875 | return NULL; | |
2876 | os_snprintf(buf, 20, "%d", val); | |
2877 | return buf; | |
2878 | } | |
2879 | ||
2880 | ||
8f03ac90 | 2881 | static char * alloc_strdup(const char *str) |
c880ab87 JM |
2882 | { |
2883 | if (str == NULL) | |
2884 | return NULL; | |
2885 | return os_strdup(str); | |
2886 | } | |
2887 | ||
2888 | ||
2889 | char * wpa_config_get_cred_no_key(struct wpa_cred *cred, const char *var) | |
2890 | { | |
2891 | if (os_strcmp(var, "temporary") == 0) | |
2892 | return alloc_int_str(cred->temporary); | |
2893 | ||
2894 | if (os_strcmp(var, "priority") == 0) | |
2895 | return alloc_int_str(cred->priority); | |
2896 | ||
2897 | if (os_strcmp(var, "sp_priority") == 0) | |
2898 | return alloc_int_str(cred->sp_priority); | |
2899 | ||
2900 | if (os_strcmp(var, "pcsc") == 0) | |
2901 | return alloc_int_str(cred->pcsc); | |
2902 | ||
2903 | if (os_strcmp(var, "eap") == 0) { | |
2904 | if (!cred->eap_method) | |
2905 | return NULL; | |
2906 | return alloc_strdup(eap_get_name(cred->eap_method[0].vendor, | |
2907 | cred->eap_method[0].method)); | |
2908 | } | |
2909 | ||
2910 | if (os_strcmp(var, "update_identifier") == 0) | |
2911 | return alloc_int_str(cred->update_identifier); | |
2912 | ||
2913 | if (os_strcmp(var, "min_dl_bandwidth_home") == 0) | |
2914 | return alloc_int_str(cred->min_dl_bandwidth_home); | |
2915 | ||
2916 | if (os_strcmp(var, "min_ul_bandwidth_home") == 0) | |
2917 | return alloc_int_str(cred->min_ul_bandwidth_home); | |
2918 | ||
2919 | if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0) | |
2920 | return alloc_int_str(cred->min_dl_bandwidth_roaming); | |
2921 | ||
2922 | if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0) | |
2923 | return alloc_int_str(cred->min_ul_bandwidth_roaming); | |
2924 | ||
2925 | if (os_strcmp(var, "max_bss_load") == 0) | |
2926 | return alloc_int_str(cred->max_bss_load); | |
2927 | ||
2928 | if (os_strcmp(var, "req_conn_capab") == 0) { | |
2929 | unsigned int i; | |
2930 | char *buf, *end, *pos; | |
2931 | int ret; | |
2932 | ||
2933 | if (!cred->num_req_conn_capab) | |
2934 | return NULL; | |
2935 | ||
2936 | buf = os_malloc(4000); | |
2937 | if (buf == NULL) | |
2938 | return NULL; | |
2939 | pos = buf; | |
2940 | end = pos + 4000; | |
2941 | for (i = 0; i < cred->num_req_conn_capab; i++) { | |
2942 | int *ports; | |
2943 | ||
2944 | ret = os_snprintf(pos, end - pos, "%s%u", | |
2945 | i > 0 ? "\n" : "", | |
2946 | cred->req_conn_capab_proto[i]); | |
2947 | if (ret < 0 || ret >= end - pos) | |
2948 | return buf; | |
2949 | pos += ret; | |
2950 | ||
2951 | ports = cred->req_conn_capab_port[i]; | |
2952 | if (ports) { | |
2953 | int j; | |
2954 | for (j = 0; ports[j] != -1; j++) { | |
2955 | ret = os_snprintf(pos, end - pos, | |
2956 | "%s%d", | |
2957 | j > 0 ? "," : ":", | |
2958 | ports[j]); | |
2959 | if (ret < 0 || ret >= end - pos) | |
2960 | return buf; | |
2961 | pos += ret; | |
2962 | } | |
2963 | } | |
2964 | } | |
2965 | ||
2966 | return buf; | |
2967 | } | |
2968 | ||
2969 | if (os_strcmp(var, "ocsp") == 0) | |
2970 | return alloc_int_str(cred->ocsp); | |
2971 | ||
2972 | if (os_strcmp(var, "realm") == 0) | |
2973 | return alloc_strdup(cred->realm); | |
2974 | ||
2975 | if (os_strcmp(var, "username") == 0) | |
2976 | return alloc_strdup(cred->username); | |
2977 | ||
2978 | if (os_strcmp(var, "password") == 0) { | |
2979 | if (!cred->password) | |
2980 | return NULL; | |
2981 | return alloc_strdup("*"); | |
2982 | } | |
2983 | ||
2984 | if (os_strcmp(var, "ca_cert") == 0) | |
2985 | return alloc_strdup(cred->ca_cert); | |
2986 | ||
2987 | if (os_strcmp(var, "client_cert") == 0) | |
2988 | return alloc_strdup(cred->client_cert); | |
2989 | ||
2990 | if (os_strcmp(var, "private_key") == 0) | |
2991 | return alloc_strdup(cred->private_key); | |
2992 | ||
2993 | if (os_strcmp(var, "private_key_passwd") == 0) { | |
2994 | if (!cred->private_key_passwd) | |
2995 | return NULL; | |
2996 | return alloc_strdup("*"); | |
2997 | } | |
2998 | ||
2999 | if (os_strcmp(var, "imsi") == 0) | |
3000 | return alloc_strdup(cred->imsi); | |
3001 | ||
3002 | if (os_strcmp(var, "milenage") == 0) { | |
3003 | if (!(cred->milenage)) | |
3004 | return NULL; | |
3005 | return alloc_strdup("*"); | |
3006 | } | |
3007 | ||
3008 | if (os_strcmp(var, "domain_suffix_match") == 0) | |
3009 | return alloc_strdup(cred->domain_suffix_match); | |
3010 | ||
3011 | if (os_strcmp(var, "domain") == 0) { | |
3012 | unsigned int i; | |
3013 | char *buf, *end, *pos; | |
3014 | int ret; | |
3015 | ||
3016 | if (!cred->num_domain) | |
3017 | return NULL; | |
3018 | ||
3019 | buf = os_malloc(4000); | |
3020 | if (buf == NULL) | |
3021 | return NULL; | |
3022 | pos = buf; | |
3023 | end = pos + 4000; | |
3024 | ||
3025 | for (i = 0; i < cred->num_domain; i++) { | |
3026 | ret = os_snprintf(pos, end - pos, "%s%s", | |
3027 | i > 0 ? "\n" : "", cred->domain[i]); | |
3028 | if (ret < 0 || ret >= end - pos) | |
3029 | return buf; | |
3030 | pos += ret; | |
3031 | } | |
3032 | ||
3033 | return buf; | |
3034 | } | |
3035 | ||
3036 | if (os_strcmp(var, "phase1") == 0) | |
3037 | return alloc_strdup(cred->phase1); | |
3038 | ||
3039 | if (os_strcmp(var, "phase2") == 0) | |
3040 | return alloc_strdup(cred->phase2); | |
3041 | ||
3042 | if (os_strcmp(var, "roaming_consortium") == 0) { | |
3043 | size_t buflen; | |
3044 | char *buf; | |
3045 | ||
3046 | if (!cred->roaming_consortium_len) | |
3047 | return NULL; | |
3048 | buflen = cred->roaming_consortium_len * 2 + 1; | |
3049 | buf = os_malloc(buflen); | |
3050 | if (buf == NULL) | |
3051 | return NULL; | |
3052 | wpa_snprintf_hex(buf, buflen, cred->roaming_consortium, | |
3053 | cred->roaming_consortium_len); | |
3054 | return buf; | |
3055 | } | |
3056 | ||
3057 | if (os_strcmp(var, "required_roaming_consortium") == 0) { | |
3058 | size_t buflen; | |
3059 | char *buf; | |
3060 | ||
3061 | if (!cred->required_roaming_consortium_len) | |
3062 | return NULL; | |
3063 | buflen = cred->required_roaming_consortium_len * 2 + 1; | |
3064 | buf = os_malloc(buflen); | |
3065 | if (buf == NULL) | |
3066 | return NULL; | |
3067 | wpa_snprintf_hex(buf, buflen, cred->required_roaming_consortium, | |
3068 | cred->required_roaming_consortium_len); | |
3069 | return buf; | |
3070 | } | |
3071 | ||
3072 | if (os_strcmp(var, "excluded_ssid") == 0) { | |
3073 | unsigned int i; | |
3074 | char *buf, *end, *pos; | |
3075 | ||
3076 | if (!cred->num_excluded_ssid) | |
3077 | return NULL; | |
3078 | ||
3079 | buf = os_malloc(4000); | |
3080 | if (buf == NULL) | |
3081 | return NULL; | |
3082 | pos = buf; | |
3083 | end = pos + 4000; | |
3084 | ||
3085 | for (i = 0; i < cred->num_excluded_ssid; i++) { | |
3086 | struct excluded_ssid *e; | |
3087 | int ret; | |
3088 | ||
3089 | e = &cred->excluded_ssid[i]; | |
3090 | ret = os_snprintf(pos, end - pos, "%s%s", | |
3091 | i > 0 ? "\n" : "", | |
3092 | wpa_ssid_txt(e->ssid, e->ssid_len)); | |
3093 | if (ret < 0 || ret >= end - pos) | |
3094 | return buf; | |
3095 | pos += ret; | |
3096 | } | |
3097 | ||
3098 | return buf; | |
3099 | } | |
3100 | ||
3101 | if (os_strcmp(var, "roaming_partner") == 0) { | |
3102 | unsigned int i; | |
3103 | char *buf, *end, *pos; | |
3104 | ||
3105 | if (!cred->num_roaming_partner) | |
3106 | return NULL; | |
3107 | ||
3108 | buf = os_malloc(4000); | |
3109 | if (buf == NULL) | |
3110 | return NULL; | |
3111 | pos = buf; | |
3112 | end = pos + 4000; | |
3113 | ||
3114 | for (i = 0; i < cred->num_roaming_partner; i++) { | |
3115 | struct roaming_partner *p; | |
3116 | int ret; | |
3117 | ||
3118 | p = &cred->roaming_partner[i]; | |
3119 | ret = os_snprintf(pos, end - pos, "%s%s,%d,%u,%s", | |
3120 | i > 0 ? "\n" : "", | |
3121 | p->fqdn, p->exact_match, p->priority, | |
3122 | p->country); | |
3123 | if (ret < 0 || ret >= end - pos) | |
3124 | return buf; | |
3125 | pos += ret; | |
3126 | } | |
3127 | ||
3128 | return buf; | |
3129 | } | |
3130 | ||
3131 | if (os_strcmp(var, "provisioning_sp") == 0) | |
3132 | return alloc_strdup(cred->provisioning_sp); | |
3133 | ||
3134 | return NULL; | |
3135 | } | |
3136 | ||
3137 | ||
d94c9ee6 JM |
3138 | struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id) |
3139 | { | |
3140 | struct wpa_cred *cred; | |
3141 | ||
3142 | cred = config->cred; | |
3143 | while (cred) { | |
3144 | if (id == cred->id) | |
3145 | break; | |
3146 | cred = cred->next; | |
3147 | } | |
3148 | ||
3149 | return cred; | |
3150 | } | |
3151 | ||
3152 | ||
3153 | struct wpa_cred * wpa_config_add_cred(struct wpa_config *config) | |
3154 | { | |
3155 | int id; | |
3156 | struct wpa_cred *cred, *last = NULL; | |
3157 | ||
3158 | id = -1; | |
3159 | cred = config->cred; | |
3160 | while (cred) { | |
3161 | if (cred->id > id) | |
3162 | id = cred->id; | |
3163 | last = cred; | |
3164 | cred = cred->next; | |
3165 | } | |
3166 | id++; | |
3167 | ||
3168 | cred = os_zalloc(sizeof(*cred)); | |
3169 | if (cred == NULL) | |
3170 | return NULL; | |
3171 | cred->id = id; | |
13f6a07e | 3172 | cred->sim_num = DEFAULT_USER_SELECTED_SIM; |
d94c9ee6 JM |
3173 | if (last) |
3174 | last->next = cred; | |
3175 | else | |
3176 | config->cred = cred; | |
3177 | ||
3178 | return cred; | |
3179 | } | |
3180 | ||
3181 | ||
3182 | int wpa_config_remove_cred(struct wpa_config *config, int id) | |
3183 | { | |
3184 | struct wpa_cred *cred, *prev = NULL; | |
3185 | ||
3186 | cred = config->cred; | |
3187 | while (cred) { | |
3188 | if (id == cred->id) | |
3189 | break; | |
3190 | prev = cred; | |
3191 | cred = cred->next; | |
3192 | } | |
3193 | ||
3194 | if (cred == NULL) | |
3195 | return -1; | |
3196 | ||
3197 | if (prev) | |
3198 | prev->next = cred->next; | |
3199 | else | |
3200 | config->cred = cred->next; | |
3201 | ||
3202 | wpa_config_free_cred(cred); | |
3203 | return 0; | |
3204 | } | |
3205 | ||
3206 | ||
6fc6879b JM |
3207 | #ifndef CONFIG_NO_CONFIG_BLOBS |
3208 | /** | |
3209 | * wpa_config_get_blob - Get a named configuration blob | |
3210 | * @config: Configuration data from wpa_config_read() | |
3211 | * @name: Name of the blob | |
3212 | * Returns: Pointer to blob data or %NULL if not found | |
3213 | */ | |
3214 | const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config, | |
3215 | const char *name) | |
3216 | { | |
3217 | struct wpa_config_blob *blob = config->blobs; | |
3218 | ||
3219 | while (blob) { | |
3220 | if (os_strcmp(blob->name, name) == 0) | |
3221 | return blob; | |
3222 | blob = blob->next; | |
3223 | } | |
3224 | return NULL; | |
3225 | } | |
3226 | ||
3227 | ||
3228 | /** | |
3229 | * wpa_config_set_blob - Set or add a named configuration blob | |
3230 | * @config: Configuration data from wpa_config_read() | |
3231 | * @blob: New value for the blob | |
3232 | * | |
3233 | * Adds a new configuration blob or replaces the current value of an existing | |
3234 | * blob. | |
3235 | */ | |
3236 | void wpa_config_set_blob(struct wpa_config *config, | |
3237 | struct wpa_config_blob *blob) | |
3238 | { | |
3239 | wpa_config_remove_blob(config, blob->name); | |
3240 | blob->next = config->blobs; | |
3241 | config->blobs = blob; | |
3242 | } | |
3243 | ||
3244 | ||
3245 | /** | |
3246 | * wpa_config_free_blob - Free blob data | |
3247 | * @blob: Pointer to blob to be freed | |
3248 | */ | |
3249 | void wpa_config_free_blob(struct wpa_config_blob *blob) | |
3250 | { | |
3251 | if (blob) { | |
3252 | os_free(blob->name); | |
19c48da0 | 3253 | bin_clear_free(blob->data, blob->len); |
6fc6879b JM |
3254 | os_free(blob); |
3255 | } | |
3256 | } | |
3257 | ||
3258 | ||
3259 | /** | |
3260 | * wpa_config_remove_blob - Remove a named configuration blob | |
3261 | * @config: Configuration data from wpa_config_read() | |
3262 | * @name: Name of the blob to remove | |
3263 | * Returns: 0 if blob was removed or -1 if blob was not found | |
3264 | */ | |
3265 | int wpa_config_remove_blob(struct wpa_config *config, const char *name) | |
3266 | { | |
3267 | struct wpa_config_blob *pos = config->blobs, *prev = NULL; | |
3268 | ||
3269 | while (pos) { | |
3270 | if (os_strcmp(pos->name, name) == 0) { | |
3271 | if (prev) | |
3272 | prev->next = pos->next; | |
3273 | else | |
3274 | config->blobs = pos->next; | |
3275 | wpa_config_free_blob(pos); | |
3276 | return 0; | |
3277 | } | |
3278 | prev = pos; | |
3279 | pos = pos->next; | |
3280 | } | |
3281 | ||
3282 | return -1; | |
3283 | } | |
3284 | #endif /* CONFIG_NO_CONFIG_BLOBS */ | |
3285 | ||
3286 | ||
3287 | /** | |
3288 | * wpa_config_alloc_empty - Allocate an empty configuration | |
3289 | * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain | |
3290 | * socket | |
3291 | * @driver_param: Driver parameters | |
3292 | * Returns: Pointer to allocated configuration data or %NULL on failure | |
3293 | */ | |
3294 | struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface, | |
3295 | const char *driver_param) | |
3296 | { | |
3297 | struct wpa_config *config; | |
c26effe1 YD |
3298 | const int aCWmin = 4, aCWmax = 10; |
3299 | const struct hostapd_wmm_ac_params ac_bk = | |
3300 | { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */ | |
3301 | const struct hostapd_wmm_ac_params ac_be = | |
3302 | { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */ | |
3303 | const struct hostapd_wmm_ac_params ac_vi = /* video traffic */ | |
623ecdd5 | 3304 | { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 }; |
c26effe1 | 3305 | const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */ |
623ecdd5 | 3306 | { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 }; |
6fc6879b JM |
3307 | |
3308 | config = os_zalloc(sizeof(*config)); | |
3309 | if (config == NULL) | |
3310 | return NULL; | |
3311 | config->eapol_version = DEFAULT_EAPOL_VERSION; | |
3312 | config->ap_scan = DEFAULT_AP_SCAN; | |
3313 | config->fast_reauth = DEFAULT_FAST_REAUTH; | |
e3768e7c | 3314 | config->p2p_go_intent = DEFAULT_P2P_GO_INTENT; |
0f66abd2 | 3315 | config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS; |
462a7439 | 3316 | config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY; |
e3bd6e9d | 3317 | config->p2p_optimize_listen_chan = DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN; |
c9c38b09 | 3318 | config->bss_max_count = DEFAULT_BSS_MAX_COUNT; |
78633c37 SL |
3319 | config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE; |
3320 | config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT; | |
dae608d5 | 3321 | config->max_num_sta = DEFAULT_MAX_NUM_STA; |
11540c0b | 3322 | config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE; |
6124e858 | 3323 | config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ; |
c26effe1 YD |
3324 | config->wmm_ac_params[0] = ac_be; |
3325 | config->wmm_ac_params[1] = ac_bk; | |
3326 | config->wmm_ac_params[2] = ac_vi; | |
3327 | config->wmm_ac_params[3] = ac_vo; | |
d3b20469 | 3328 | config->p2p_search_delay = DEFAULT_P2P_SEARCH_DELAY; |
c267753b | 3329 | config->rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME; |
6fc6879b JM |
3330 | |
3331 | if (ctrl_interface) | |
3332 | config->ctrl_interface = os_strdup(ctrl_interface); | |
3333 | if (driver_param) | |
3334 | config->driver_param = os_strdup(driver_param); | |
3335 | ||
3336 | return config; | |
3337 | } | |
3338 | ||
3339 | ||
3340 | #ifndef CONFIG_NO_STDOUT_DEBUG | |
3341 | /** | |
3342 | * wpa_config_debug_dump_networks - Debug dump of configured networks | |
3343 | * @config: Configuration data from wpa_config_read() | |
3344 | */ | |
3345 | void wpa_config_debug_dump_networks(struct wpa_config *config) | |
3346 | { | |
3347 | int prio; | |
3348 | struct wpa_ssid *ssid; | |
3349 | ||
3350 | for (prio = 0; prio < config->num_prio; prio++) { | |
3351 | ssid = config->pssid[prio]; | |
3352 | wpa_printf(MSG_DEBUG, "Priority group %d", | |
3353 | ssid->priority); | |
3354 | while (ssid) { | |
3355 | wpa_printf(MSG_DEBUG, " id=%d ssid='%s'", | |
3356 | ssid->id, | |
3357 | wpa_ssid_txt(ssid->ssid, ssid->ssid_len)); | |
3358 | ssid = ssid->pnext; | |
3359 | } | |
3360 | } | |
3361 | } | |
3362 | #endif /* CONFIG_NO_STDOUT_DEBUG */ | |
121adf9c JM |
3363 | |
3364 | ||
3365 | struct global_parse_data { | |
3366 | char *name; | |
3367 | int (*parser)(const struct global_parse_data *data, | |
3368 | struct wpa_config *config, int line, const char *value); | |
3369 | void *param1, *param2, *param3; | |
1d47214a | 3370 | unsigned int changed_flag; |
121adf9c JM |
3371 | }; |
3372 | ||
3373 | ||
3374 | static int wpa_global_config_parse_int(const struct global_parse_data *data, | |
3375 | struct wpa_config *config, int line, | |
3376 | const char *pos) | |
3377 | { | |
eae3a584 JB |
3378 | int val, *dst; |
3379 | char *end; | |
3380 | ||
121adf9c | 3381 | dst = (int *) (((u8 *) config) + (long) data->param1); |
eae3a584 JB |
3382 | val = strtol(pos, &end, 0); |
3383 | if (*end) { | |
3384 | wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"", | |
3385 | line, pos); | |
3386 | return -1; | |
3387 | } | |
3388 | *dst = val; | |
3389 | ||
121adf9c JM |
3390 | wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst); |
3391 | ||
3392 | if (data->param2 && *dst < (long) data->param2) { | |
3393 | wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d " | |
3394 | "min_value=%ld)", line, data->name, *dst, | |
3395 | (long) data->param2); | |
3396 | *dst = (long) data->param2; | |
3397 | return -1; | |
3398 | } | |
3399 | ||
3400 | if (data->param3 && *dst > (long) data->param3) { | |
3401 | wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d " | |
3402 | "max_value=%ld)", line, data->name, *dst, | |
3403 | (long) data->param3); | |
3404 | *dst = (long) data->param3; | |
3405 | return -1; | |
3406 | } | |
3407 | ||
3408 | return 0; | |
3409 | } | |
3410 | ||
3411 | ||
3412 | static int wpa_global_config_parse_str(const struct global_parse_data *data, | |
3413 | struct wpa_config *config, int line, | |
3414 | const char *pos) | |
3415 | { | |
3416 | size_t len; | |
3417 | char **dst, *tmp; | |
3418 | ||
3419 | len = os_strlen(pos); | |
3420 | if (data->param2 && len < (size_t) data->param2) { | |
3421 | wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu " | |
3422 | "min_len=%ld)", line, data->name, | |
3423 | (unsigned long) len, (long) data->param2); | |
3424 | return -1; | |
3425 | } | |
3426 | ||
3427 | if (data->param3 && len > (size_t) data->param3) { | |
3428 | wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu " | |
3429 | "max_len=%ld)", line, data->name, | |
3430 | (unsigned long) len, (long) data->param3); | |
3431 | return -1; | |
3432 | } | |
3433 | ||
3434 | tmp = os_strdup(pos); | |
3435 | if (tmp == NULL) | |
3436 | return -1; | |
3437 | ||
3438 | dst = (char **) (((u8 *) config) + (long) data->param1); | |
3439 | os_free(*dst); | |
3440 | *dst = tmp; | |
3441 | wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst); | |
3442 | ||
3443 | return 0; | |
3444 | } | |
3445 | ||
3446 | ||
31392709 HD |
3447 | static int wpa_config_process_bgscan(const struct global_parse_data *data, |
3448 | struct wpa_config *config, int line, | |
3449 | const char *pos) | |
3450 | { | |
3451 | size_t len; | |
3452 | char *tmp; | |
04c366cb | 3453 | int res; |
31392709 HD |
3454 | |
3455 | tmp = wpa_config_parse_string(pos, &len); | |
3456 | if (tmp == NULL) { | |
3457 | wpa_printf(MSG_ERROR, "Line %d: failed to parse %s", | |
3458 | line, data->name); | |
3459 | return -1; | |
3460 | } | |
3461 | ||
04c366cb EL |
3462 | res = wpa_global_config_parse_str(data, config, line, tmp); |
3463 | os_free(tmp); | |
3464 | return res; | |
31392709 HD |
3465 | } |
3466 | ||
3467 | ||
3f2c8ba6 JM |
3468 | static int wpa_global_config_parse_bin(const struct global_parse_data *data, |
3469 | struct wpa_config *config, int line, | |
3470 | const char *pos) | |
3471 | { | |
3472 | size_t len; | |
3473 | struct wpabuf **dst, *tmp; | |
3474 | ||
3475 | len = os_strlen(pos); | |
3476 | if (len & 0x01) | |
3477 | return -1; | |
3478 | ||
3479 | tmp = wpabuf_alloc(len / 2); | |
3480 | if (tmp == NULL) | |
3481 | return -1; | |
3482 | ||
3483 | if (hexstr2bin(pos, wpabuf_put(tmp, len / 2), len / 2)) { | |
3484 | wpabuf_free(tmp); | |
3485 | return -1; | |
3486 | } | |
3487 | ||
3488 | dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1); | |
3489 | wpabuf_free(*dst); | |
3490 | *dst = tmp; | |
3491 | wpa_printf(MSG_DEBUG, "%s", data->name); | |
3492 | ||
3493 | return 0; | |
3494 | } | |
3495 | ||
3496 | ||
f5ffc348 BG |
3497 | static int wpa_config_process_freq_list(const struct global_parse_data *data, |
3498 | struct wpa_config *config, int line, | |
3499 | const char *value) | |
3500 | { | |
3501 | int *freqs; | |
3502 | ||
3503 | freqs = wpa_config_parse_int_array(value); | |
3504 | if (freqs == NULL) | |
3505 | return -1; | |
6668efda JM |
3506 | if (freqs[0] == 0) { |
3507 | os_free(freqs); | |
3508 | freqs = NULL; | |
3509 | } | |
f5ffc348 BG |
3510 | os_free(config->freq_list); |
3511 | config->freq_list = freqs; | |
3512 | return 0; | |
3513 | } | |
3514 | ||
3515 | ||
25ef8529 JM |
3516 | #ifdef CONFIG_P2P |
3517 | static int wpa_global_config_parse_ipv4(const struct global_parse_data *data, | |
3518 | struct wpa_config *config, int line, | |
3519 | const char *pos) | |
3520 | { | |
3521 | u32 *dst; | |
3522 | struct hostapd_ip_addr addr; | |
3523 | ||
3524 | if (hostapd_parse_ip_addr(pos, &addr) < 0) | |
3525 | return -1; | |
3526 | if (addr.af != AF_INET) | |
3527 | return -1; | |
3528 | ||
3529 | dst = (u32 *) (((u8 *) config) + (long) data->param1); | |
3530 | os_memcpy(dst, &addr.u.v4.s_addr, 4); | |
3531 | wpa_printf(MSG_DEBUG, "%s = 0x%x", data->name, | |
3532 | WPA_GET_BE32((u8 *) dst)); | |
3533 | ||
3534 | return 0; | |
3535 | } | |
3536 | #endif /* CONFIG_P2P */ | |
3537 | ||
3538 | ||
121adf9c JM |
3539 | static int wpa_config_process_country(const struct global_parse_data *data, |
3540 | struct wpa_config *config, int line, | |
3541 | const char *pos) | |
3542 | { | |
3543 | if (!pos[0] || !pos[1]) { | |
3544 | wpa_printf(MSG_DEBUG, "Invalid country set"); | |
3545 | return -1; | |
3546 | } | |
3547 | config->country[0] = pos[0]; | |
3548 | config->country[1] = pos[1]; | |
3549 | wpa_printf(MSG_DEBUG, "country='%c%c'", | |
3550 | config->country[0], config->country[1]); | |
3551 | return 0; | |
3552 | } | |
3553 | ||
3554 | ||
3555 | static int wpa_config_process_load_dynamic_eap( | |
3556 | const struct global_parse_data *data, struct wpa_config *config, | |
3557 | int line, const char *so) | |
3558 | { | |
3559 | int ret; | |
3560 | wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so); | |
3561 | ret = eap_peer_method_load(so); | |
3562 | if (ret == -2) { | |
3563 | wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not " | |
3564 | "reloading."); | |
3565 | } else if (ret) { | |
3566 | wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP " | |
3567 | "method '%s'.", line, so); | |
3568 | return -1; | |
3569 | } | |
3570 | ||
3571 | return 0; | |
3572 | } | |
3573 | ||
3574 | ||
3575 | #ifdef CONFIG_WPS | |
3576 | ||
3577 | static int wpa_config_process_uuid(const struct global_parse_data *data, | |
3578 | struct wpa_config *config, int line, | |
3579 | const char *pos) | |
3580 | { | |
3581 | char buf[40]; | |
3582 | if (uuid_str2bin(pos, config->uuid)) { | |
3583 | wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line); | |
3584 | return -1; | |
3585 | } | |
3586 | uuid_bin2str(config->uuid, buf, sizeof(buf)); | |
3587 | wpa_printf(MSG_DEBUG, "uuid=%s", buf); | |
3588 | return 0; | |
3589 | } | |
3590 | ||
3591 | ||
2f646b6e JB |
3592 | static int wpa_config_process_device_type( |
3593 | const struct global_parse_data *data, | |
3594 | struct wpa_config *config, int line, const char *pos) | |
3595 | { | |
3596 | return wps_dev_type_str2bin(pos, config->device_type); | |
3597 | } | |
3598 | ||
3599 | ||
121adf9c JM |
3600 | static int wpa_config_process_os_version(const struct global_parse_data *data, |
3601 | struct wpa_config *config, int line, | |
3602 | const char *pos) | |
3603 | { | |
3604 | if (hexstr2bin(pos, config->os_version, 4)) { | |
3605 | wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line); | |
3606 | return -1; | |
3607 | } | |
3608 | wpa_printf(MSG_DEBUG, "os_version=%08x", | |
3609 | WPA_GET_BE32(config->os_version)); | |
3610 | return 0; | |
3611 | } | |
3612 | ||
71dd3b78 AS |
3613 | |
3614 | static int wpa_config_process_wps_vendor_ext_m1( | |
3615 | const struct global_parse_data *data, | |
3616 | struct wpa_config *config, int line, const char *pos) | |
3617 | { | |
3618 | struct wpabuf *tmp; | |
3619 | int len = os_strlen(pos) / 2; | |
3620 | u8 *p; | |
3621 | ||
3622 | if (!len) { | |
3623 | wpa_printf(MSG_ERROR, "Line %d: " | |
3624 | "invalid wps_vendor_ext_m1", line); | |
3625 | return -1; | |
3626 | } | |
3627 | ||
3628 | tmp = wpabuf_alloc(len); | |
3629 | if (tmp) { | |
3630 | p = wpabuf_put(tmp, len); | |
3631 | ||
3632 | if (hexstr2bin(pos, p, len)) { | |
3633 | wpa_printf(MSG_ERROR, "Line %d: " | |
3634 | "invalid wps_vendor_ext_m1", line); | |
3635 | wpabuf_free(tmp); | |
3636 | return -1; | |
3637 | } | |
3638 | ||
3639 | wpabuf_free(config->wps_vendor_ext_m1); | |
3640 | config->wps_vendor_ext_m1 = tmp; | |
3641 | } else { | |
3642 | wpa_printf(MSG_ERROR, "Can not allocate " | |
3643 | "memory for wps_vendor_ext_m1"); | |
3644 | return -1; | |
3645 | } | |
3646 | ||
3647 | return 0; | |
3648 | } | |
3649 | ||
121adf9c JM |
3650 | #endif /* CONFIG_WPS */ |
3651 | ||
e3768e7c JM |
3652 | #ifdef CONFIG_P2P |
3653 | static int wpa_config_process_sec_device_type( | |
3654 | const struct global_parse_data *data, | |
3655 | struct wpa_config *config, int line, const char *pos) | |
3656 | { | |
3657 | int idx; | |
3658 | ||
2f646b6e | 3659 | if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) { |
e3768e7c JM |
3660 | wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type " |
3661 | "items", line); | |
3662 | return -1; | |
3663 | } | |
3664 | ||
2f646b6e JB |
3665 | idx = config->num_sec_device_types; |
3666 | ||
3667 | if (wps_dev_type_str2bin(pos, config->sec_device_type[idx])) | |
e3768e7c | 3668 | return -1; |
2f646b6e JB |
3669 | |
3670 | config->num_sec_device_types++; | |
e3768e7c JM |
3671 | return 0; |
3672 | } | |
21d996f7 JM |
3673 | |
3674 | ||
3675 | static int wpa_config_process_p2p_pref_chan( | |
3676 | const struct global_parse_data *data, | |
3677 | struct wpa_config *config, int line, const char *pos) | |
3678 | { | |
3679 | struct p2p_channel *pref = NULL, *n; | |
3680 | unsigned int num = 0; | |
3681 | const char *pos2; | |
3682 | u8 op_class, chan; | |
3683 | ||
3684 | /* format: class:chan,class:chan,... */ | |
3685 | ||
3686 | while (*pos) { | |
3687 | op_class = atoi(pos); | |
3688 | pos2 = os_strchr(pos, ':'); | |
3689 | if (pos2 == NULL) | |
3690 | goto fail; | |
3691 | pos2++; | |
3692 | chan = atoi(pos2); | |
3693 | ||
067ffa26 JM |
3694 | n = os_realloc_array(pref, num + 1, |
3695 | sizeof(struct p2p_channel)); | |
21d996f7 JM |
3696 | if (n == NULL) |
3697 | goto fail; | |
3698 | pref = n; | |
3699 | pref[num].op_class = op_class; | |
3700 | pref[num].chan = chan; | |
3701 | num++; | |
3702 | ||
3703 | pos = os_strchr(pos2, ','); | |
3704 | if (pos == NULL) | |
3705 | break; | |
3706 | pos++; | |
3707 | } | |
3708 | ||
3709 | os_free(config->p2p_pref_chan); | |
3710 | config->p2p_pref_chan = pref; | |
3711 | config->num_p2p_pref_chan = num; | |
3712 | wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs", | |
3713 | (u8 *) config->p2p_pref_chan, | |
3714 | config->num_p2p_pref_chan * sizeof(struct p2p_channel)); | |
3715 | ||
3716 | return 0; | |
3717 | ||
3718 | fail: | |
3719 | os_free(pref); | |
3720 | wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line); | |
3721 | return -1; | |
3722 | } | |
556b30da JM |
3723 | |
3724 | ||
3725 | static int wpa_config_process_p2p_no_go_freq( | |
3726 | const struct global_parse_data *data, | |
3727 | struct wpa_config *config, int line, const char *pos) | |
3728 | { | |
3729 | int ret; | |
3730 | ||
3731 | ret = freq_range_list_parse(&config->p2p_no_go_freq, pos); | |
3732 | if (ret < 0) { | |
3733 | wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line); | |
3734 | return -1; | |
3735 | } | |
3736 | ||
3737 | wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items", | |
3738 | config->p2p_no_go_freq.num); | |
3739 | ||
3740 | return 0; | |
3741 | } | |
3742 | ||
e3768e7c JM |
3743 | #endif /* CONFIG_P2P */ |
3744 | ||
121adf9c | 3745 | |
46ee0427 JM |
3746 | static int wpa_config_process_hessid( |
3747 | const struct global_parse_data *data, | |
3748 | struct wpa_config *config, int line, const char *pos) | |
3749 | { | |
3750 | if (hwaddr_aton2(pos, config->hessid) < 0) { | |
3751 | wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'", | |
3752 | line, pos); | |
3753 | return -1; | |
3754 | } | |
3755 | ||
3756 | return 0; | |
3757 | } | |
3758 | ||
3759 | ||
625f202a JM |
3760 | static int wpa_config_process_sae_groups( |
3761 | const struct global_parse_data *data, | |
3762 | struct wpa_config *config, int line, const char *pos) | |
3763 | { | |
3764 | int *groups = wpa_config_parse_int_array(pos); | |
3765 | if (groups == NULL) { | |
3766 | wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'", | |
3767 | line, pos); | |
3768 | return -1; | |
3769 | } | |
3770 | ||
3771 | os_free(config->sae_groups); | |
3772 | config->sae_groups = groups; | |
3773 | ||
3774 | return 0; | |
3775 | } | |
3776 | ||
3777 | ||
18a2eaab JM |
3778 | static int wpa_config_process_ap_vendor_elements( |
3779 | const struct global_parse_data *data, | |
3780 | struct wpa_config *config, int line, const char *pos) | |
3781 | { | |
3782 | struct wpabuf *tmp; | |
3783 | int len = os_strlen(pos) / 2; | |
3784 | u8 *p; | |
3785 | ||
3786 | if (!len) { | |
3787 | wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements", | |
3788 | line); | |
3789 | return -1; | |
3790 | } | |
3791 | ||
3792 | tmp = wpabuf_alloc(len); | |
3793 | if (tmp) { | |
3794 | p = wpabuf_put(tmp, len); | |
3795 | ||
3796 | if (hexstr2bin(pos, p, len)) { | |
3797 | wpa_printf(MSG_ERROR, "Line %d: invalid " | |
3798 | "ap_vendor_elements", line); | |
3799 | wpabuf_free(tmp); | |
3800 | return -1; | |
3801 | } | |
3802 | ||
3803 | wpabuf_free(config->ap_vendor_elements); | |
3804 | config->ap_vendor_elements = tmp; | |
3805 | } else { | |
3806 | wpa_printf(MSG_ERROR, "Cannot allocate memory for " | |
3807 | "ap_vendor_elements"); | |
3808 | return -1; | |
3809 | } | |
3810 | ||
3811 | return 0; | |
3812 | } | |
3813 | ||
3814 | ||
298f5185 | 3815 | #ifdef CONFIG_CTRL_IFACE |
ea61aa1d JM |
3816 | static int wpa_config_process_no_ctrl_interface( |
3817 | const struct global_parse_data *data, | |
3818 | struct wpa_config *config, int line, const char *pos) | |
3819 | { | |
3820 | wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL"); | |
3821 | os_free(config->ctrl_interface); | |
3822 | config->ctrl_interface = NULL; | |
3823 | return 0; | |
3824 | } | |
298f5185 | 3825 | #endif /* CONFIG_CTRL_IFACE */ |
ea61aa1d JM |
3826 | |
3827 | ||
121adf9c JM |
3828 | #ifdef OFFSET |
3829 | #undef OFFSET | |
3830 | #endif /* OFFSET */ | |
3831 | /* OFFSET: Get offset of a variable within the wpa_config structure */ | |
3832 | #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v) | |
3833 | ||
3834 | #define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL | |
3835 | #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL | |
3836 | #define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f) | |
3837 | #define INT(f) _INT(f), NULL, NULL | |
3838 | #define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max | |
3839 | #define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f) | |
3840 | #define STR(f) _STR(f), NULL, NULL | |
3841 | #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max | |
3f2c8ba6 | 3842 | #define BIN(f) #f, wpa_global_config_parse_bin, OFFSET(f), NULL, NULL |
25ef8529 | 3843 | #define IPV4(f) #f, wpa_global_config_parse_ipv4, OFFSET(f), NULL, NULL |
121adf9c JM |
3844 | |
3845 | static const struct global_parse_data global_fields[] = { | |
3846 | #ifdef CONFIG_CTRL_IFACE | |
1d47214a | 3847 | { STR(ctrl_interface), 0 }, |
ea61aa1d | 3848 | { FUNC_NO_VAR(no_ctrl_interface), 0 }, |
1d47214a | 3849 | { STR(ctrl_interface_group), 0 } /* deprecated */, |
121adf9c | 3850 | #endif /* CONFIG_CTRL_IFACE */ |
0836c04b HW |
3851 | #ifdef CONFIG_MACSEC |
3852 | { INT_RANGE(eapol_version, 1, 3), 0 }, | |
3853 | #else /* CONFIG_MACSEC */ | |
1d47214a | 3854 | { INT_RANGE(eapol_version, 1, 2), 0 }, |
0836c04b | 3855 | #endif /* CONFIG_MACSEC */ |
1d47214a | 3856 | { INT(ap_scan), 0 }, |
31392709 | 3857 | { FUNC(bgscan), 0 }, |
54ddd743 | 3858 | { INT(disable_scan_offload), 0 }, |
1d47214a JM |
3859 | { INT(fast_reauth), 0 }, |
3860 | { STR(opensc_engine_path), 0 }, | |
3861 | { STR(pkcs11_engine_path), 0 }, | |
3862 | { STR(pkcs11_module_path), 0 }, | |
07e2de31 | 3863 | { STR(openssl_ciphers), 0 }, |
f64adcd7 JM |
3864 | { STR(pcsc_reader), 0 }, |
3865 | { STR(pcsc_pin), 0 }, | |
a5d44ac0 | 3866 | { INT(external_sim), 0 }, |
1d47214a JM |
3867 | { STR(driver_param), 0 }, |
3868 | { INT(dot11RSNAConfigPMKLifetime), 0 }, | |
3869 | { INT(dot11RSNAConfigPMKReauthThreshold), 0 }, | |
3870 | { INT(dot11RSNAConfigSATimeout), 0 }, | |
121adf9c | 3871 | #ifndef CONFIG_NO_CONFIG_WRITE |
1d47214a | 3872 | { INT(update_config), 0 }, |
121adf9c | 3873 | #endif /* CONFIG_NO_CONFIG_WRITE */ |
1d47214a | 3874 | { FUNC_NO_VAR(load_dynamic_eap), 0 }, |
121adf9c | 3875 | #ifdef CONFIG_WPS |
1d47214a JM |
3876 | { FUNC(uuid), CFG_CHANGED_UUID }, |
3877 | { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME }, | |
1c9cb49f JM |
3878 | { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING }, |
3879 | { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING }, | |
3880 | { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING }, | |
3881 | { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING }, | |
2f646b6e | 3882 | { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE }, |
1d47214a JM |
3883 | { FUNC(os_version), CFG_CHANGED_OS_VERSION }, |
3884 | { STR(config_methods), CFG_CHANGED_CONFIG_METHODS }, | |
3885 | { INT_RANGE(wps_cred_processing, 0, 2), 0 }, | |
71dd3b78 | 3886 | { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION }, |
121adf9c | 3887 | #endif /* CONFIG_WPS */ |
e3768e7c JM |
3888 | #ifdef CONFIG_P2P |
3889 | { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE }, | |
3890 | { INT(p2p_listen_reg_class), 0 }, | |
3891 | { INT(p2p_listen_channel), 0 }, | |
00eb2993 JM |
3892 | { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL }, |
3893 | { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL }, | |
e3768e7c JM |
3894 | { INT_RANGE(p2p_go_intent, 0, 15), 0 }, |
3895 | { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX }, | |
3896 | { INT_RANGE(persistent_reconnect, 0, 1), 0 }, | |
0f66abd2 | 3897 | { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS }, |
3071e181 | 3898 | { INT(p2p_group_idle), 0 }, |
1b928f96 JM |
3899 | { INT_RANGE(p2p_passphrase_len, 8, 63), |
3900 | CFG_CHANGED_P2P_PASSPHRASE_LEN }, | |
21d996f7 | 3901 | { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN }, |
556b30da | 3902 | { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN }, |
51e9f228 | 3903 | { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 }, |
e3bd6e9d | 3904 | { INT_RANGE(p2p_optimize_listen_chan, 0, 1), 0 }, |
a93a15bb | 3905 | { INT(p2p_go_ht40), 0 }, |
20ea1ca4 | 3906 | { INT(p2p_go_vht), 0 }, |
7a808c7e | 3907 | { INT(p2p_disabled), 0 }, |
d76cd41a | 3908 | { INT(p2p_no_group_iface), 0 }, |
b277a2be | 3909 | { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 }, |
25ef8529 JM |
3910 | { IPV4(ip_addr_go), 0 }, |
3911 | { IPV4(ip_addr_mask), 0 }, | |
3912 | { IPV4(ip_addr_start), 0 }, | |
3913 | { IPV4(ip_addr_end), 0 }, | |
e3768e7c | 3914 | #endif /* CONFIG_P2P */ |
1d47214a JM |
3915 | { FUNC(country), CFG_CHANGED_COUNTRY }, |
3916 | { INT(bss_max_count), 0 }, | |
78633c37 SL |
3917 | { INT(bss_expiration_age), 0 }, |
3918 | { INT(bss_expiration_scan_count), 0 }, | |
dae608d5 | 3919 | { INT_RANGE(filter_ssids, 0, 1), 0 }, |
bf8d6d24 | 3920 | { INT_RANGE(filter_rssi, -100, 0), 0 }, |
0d7e5a3a | 3921 | { INT(max_num_sta), 0 }, |
46ee0427 | 3922 | { INT_RANGE(disassoc_low_ack, 0, 1), 0 }, |
66aadbd7 JK |
3923 | #ifdef CONFIG_HS20 |
3924 | { INT_RANGE(hs20, 0, 1), 0 }, | |
3925 | #endif /* CONFIG_HS20 */ | |
46ee0427 | 3926 | { INT_RANGE(interworking, 0, 1), 0 }, |
11540c0b | 3927 | { FUNC(hessid), 0 }, |
1298c145 | 3928 | { INT_RANGE(access_network_type, 0, 15), 0 }, |
b0786fba | 3929 | { INT_RANGE(pbc_in_m1, 0, 1), 0 }, |
3f2c8ba6 | 3930 | { STR(autoscan), 0 }, |
042ec551 JM |
3931 | { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff), |
3932 | CFG_CHANGED_NFC_PASSWORD_TOKEN }, | |
3933 | { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN }, | |
3934 | { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN }, | |
3935 | { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN }, | |
462a7439 ES |
3936 | { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND }, |
3937 | { INT(p2p_go_max_inactivity), 0 }, | |
4d5bda5f | 3938 | { INT_RANGE(auto_interworking, 0, 1), 0 }, |
6e202021 | 3939 | { INT(okc), 0 }, |
62d49803 | 3940 | { INT(pmf), 0 }, |
625f202a | 3941 | { FUNC(sae_groups), 0 }, |
18206e02 JM |
3942 | { INT(dtim_period), 0 }, |
3943 | { INT(beacon_int), 0 }, | |
18a2eaab | 3944 | { FUNC(ap_vendor_elements), 0 }, |
4342326f | 3945 | { INT_RANGE(ignore_old_scan_res, 0, 1), 0 }, |
f5ffc348 | 3946 | { FUNC(freq_list), 0 }, |
6124e858 | 3947 | { INT(scan_cur_freq), 0 }, |
4aa81868 | 3948 | { INT(sched_scan_interval), 0 }, |
800d5872 | 3949 | { INT(tdls_external_control), 0}, |
b572df86 | 3950 | { STR(osu_dir), 0 }, |
e4fa8b12 | 3951 | { STR(wowlan_triggers), 0 }, |
d3b20469 | 3952 | { INT(p2p_search_delay), 0}, |
c267753b JM |
3953 | { INT(mac_addr), 0 }, |
3954 | { INT(rand_addr_lifetime), 0 }, | |
3955 | { INT(preassoc_mac_addr), 0 }, | |
121adf9c JM |
3956 | }; |
3957 | ||
3958 | #undef FUNC | |
3959 | #undef _INT | |
3960 | #undef INT | |
3961 | #undef INT_RANGE | |
3962 | #undef _STR | |
3963 | #undef STR | |
3964 | #undef STR_RANGE | |
3f2c8ba6 | 3965 | #undef BIN |
25ef8529 | 3966 | #undef IPV4 |
e7ecab4a | 3967 | #define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields) |
121adf9c JM |
3968 | |
3969 | ||
3970 | int wpa_config_process_global(struct wpa_config *config, char *pos, int line) | |
3971 | { | |
3972 | size_t i; | |
3973 | int ret = 0; | |
3974 | ||
3975 | for (i = 0; i < NUM_GLOBAL_FIELDS; i++) { | |
3976 | const struct global_parse_data *field = &global_fields[i]; | |
3977 | size_t flen = os_strlen(field->name); | |
3978 | if (os_strncmp(pos, field->name, flen) != 0 || | |
3979 | pos[flen] != '=') | |
3980 | continue; | |
3981 | ||
3982 | if (field->parser(field, config, line, pos + flen + 1)) { | |
3983 | wpa_printf(MSG_ERROR, "Line %d: failed to " | |
3984 | "parse '%s'.", line, pos); | |
3985 | ret = -1; | |
3986 | } | |
042ec551 JM |
3987 | if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN) |
3988 | config->wps_nfc_pw_from_config = 1; | |
1d47214a | 3989 | config->changed_parameters |= field->changed_flag; |
121adf9c JM |
3990 | break; |
3991 | } | |
3992 | if (i == NUM_GLOBAL_FIELDS) { | |
c26effe1 YD |
3993 | #ifdef CONFIG_AP |
3994 | if (os_strncmp(pos, "wmm_ac_", 7) == 0) { | |
3995 | char *tmp = os_strchr(pos, '='); | |
3996 | if (tmp == NULL) { | |
3997 | if (line < 0) | |
3998 | return -1; | |
3999 | wpa_printf(MSG_ERROR, "Line %d: invalid line " | |
4000 | "'%s'", line, pos); | |
4001 | return -1; | |
4002 | } | |
4003 | *tmp++ = '\0'; | |
4004 | if (hostapd_config_wmm_ac(config->wmm_ac_params, pos, | |
4005 | tmp)) { | |
4006 | wpa_printf(MSG_ERROR, "Line %d: invalid WMM " | |
4007 | "AC item", line); | |
4008 | return -1; | |
4009 | } | |
4010 | } | |
4011 | #endif /* CONFIG_AP */ | |
1d47214a JM |
4012 | if (line < 0) |
4013 | return -1; | |
121adf9c JM |
4014 | wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.", |
4015 | line, pos); | |
4016 | ret = -1; | |
4017 | } | |
4018 | ||
4019 | return ret; | |
4020 | } |