Add preliminary RADIUS dynamic authorization server (RFC 5176)
[mech_eap.git] / src / ap / ap_config.c
1 /*
2  * hostapd / Configuration helper functions
3  * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "crypto/sha1.h"
13 #include "radius/radius_client.h"
14 #include "common/ieee802_11_defs.h"
15 #include "common/eapol_common.h"
16 #include "eap_common/eap_wsc_common.h"
17 #include "eap_server/eap.h"
18 #include "wpa_auth.h"
19 #include "sta_info.h"
20 #include "ap_config.h"
21
22
23 static void hostapd_config_free_vlan(struct hostapd_bss_config *bss)
24 {
25         struct hostapd_vlan *vlan, *prev;
26
27         vlan = bss->vlan;
28         prev = NULL;
29         while (vlan) {
30                 prev = vlan;
31                 vlan = vlan->next;
32                 os_free(prev);
33         }
34
35         bss->vlan = NULL;
36 }
37
38
39 void hostapd_config_defaults_bss(struct hostapd_bss_config *bss)
40 {
41         bss->logger_syslog_level = HOSTAPD_LEVEL_INFO;
42         bss->logger_stdout_level = HOSTAPD_LEVEL_INFO;
43         bss->logger_syslog = (unsigned int) -1;
44         bss->logger_stdout = (unsigned int) -1;
45
46         bss->auth_algs = WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED;
47
48         bss->wep_rekeying_period = 300;
49         /* use key0 in individual key and key1 in broadcast key */
50         bss->broadcast_key_idx_min = 1;
51         bss->broadcast_key_idx_max = 2;
52         bss->eap_reauth_period = 3600;
53
54         bss->wpa_group_rekey = 600;
55         bss->wpa_gmk_rekey = 86400;
56         bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
57         bss->wpa_pairwise = WPA_CIPHER_TKIP;
58         bss->wpa_group = WPA_CIPHER_TKIP;
59         bss->rsn_pairwise = 0;
60
61         bss->max_num_sta = MAX_STA_COUNT;
62
63         bss->dtim_period = 2;
64
65         bss->radius_server_auth_port = 1812;
66         bss->ap_max_inactivity = AP_MAX_INACTIVITY;
67         bss->eapol_version = EAPOL_VERSION;
68
69         bss->max_listen_interval = 65535;
70
71         bss->pwd_group = 19; /* ECC: GF(p=256) */
72
73 #ifdef CONFIG_IEEE80211W
74         bss->assoc_sa_query_max_timeout = 1000;
75         bss->assoc_sa_query_retry_timeout = 201;
76 #endif /* CONFIG_IEEE80211W */
77 #ifdef EAP_SERVER_FAST
78          /* both anonymous and authenticated provisioning */
79         bss->eap_fast_prov = 3;
80         bss->pac_key_lifetime = 7 * 24 * 60 * 60;
81         bss->pac_key_refresh_time = 1 * 24 * 60 * 60;
82 #endif /* EAP_SERVER_FAST */
83
84         /* Set to -1 as defaults depends on HT in setup */
85         bss->wmm_enabled = -1;
86
87 #ifdef CONFIG_IEEE80211R
88         bss->ft_over_ds = 1;
89 #endif /* CONFIG_IEEE80211R */
90 }
91
92
93 struct hostapd_config * hostapd_config_defaults(void)
94 {
95 #define ecw2cw(ecw) ((1 << (ecw)) - 1)
96
97         struct hostapd_config *conf;
98         struct hostapd_bss_config *bss;
99         const int aCWmin = 4, aCWmax = 10;
100         const struct hostapd_wmm_ac_params ac_bk =
101                 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
102         const struct hostapd_wmm_ac_params ac_be =
103                 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
104         const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
105                 { aCWmin - 1, aCWmin, 2, 3000 / 32, 1 };
106         const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
107                 { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 1 };
108         const struct hostapd_tx_queue_params txq_bk =
109                 { 7, ecw2cw(aCWmin), ecw2cw(aCWmax), 0 };
110         const struct hostapd_tx_queue_params txq_be =
111                 { 3, ecw2cw(aCWmin), 4 * (ecw2cw(aCWmin) + 1) - 1, 0};
112         const struct hostapd_tx_queue_params txq_vi =
113                 { 1, (ecw2cw(aCWmin) + 1) / 2 - 1, ecw2cw(aCWmin), 30};
114         const struct hostapd_tx_queue_params txq_vo =
115                 { 1, (ecw2cw(aCWmin) + 1) / 4 - 1,
116                   (ecw2cw(aCWmin) + 1) / 2 - 1, 15};
117
118 #undef ecw2cw
119
120         conf = os_zalloc(sizeof(*conf));
121         bss = os_zalloc(sizeof(*bss));
122         if (conf == NULL || bss == NULL) {
123                 wpa_printf(MSG_ERROR, "Failed to allocate memory for "
124                            "configuration data.");
125                 os_free(conf);
126                 os_free(bss);
127                 return NULL;
128         }
129
130         bss->radius = os_zalloc(sizeof(*bss->radius));
131         if (bss->radius == NULL) {
132                 os_free(conf);
133                 os_free(bss);
134                 return NULL;
135         }
136
137         hostapd_config_defaults_bss(bss);
138
139         conf->num_bss = 1;
140         conf->bss = bss;
141
142         conf->beacon_int = 100;
143         conf->rts_threshold = -1; /* use driver default: 2347 */
144         conf->fragm_threshold = -1; /* user driver default: 2346 */
145         conf->send_probe_response = 1;
146
147         conf->wmm_ac_params[0] = ac_be;
148         conf->wmm_ac_params[1] = ac_bk;
149         conf->wmm_ac_params[2] = ac_vi;
150         conf->wmm_ac_params[3] = ac_vo;
151
152         conf->tx_queue[0] = txq_vo;
153         conf->tx_queue[1] = txq_vi;
154         conf->tx_queue[2] = txq_be;
155         conf->tx_queue[3] = txq_bk;
156
157         conf->ht_capab = HT_CAP_INFO_SMPS_DISABLED;
158
159         return conf;
160 }
161
162
163 int hostapd_mac_comp(const void *a, const void *b)
164 {
165         return os_memcmp(a, b, sizeof(macaddr));
166 }
167
168
169 int hostapd_mac_comp_empty(const void *a)
170 {
171         macaddr empty = { 0 };
172         return os_memcmp(a, empty, sizeof(macaddr));
173 }
174
175
176 static int hostapd_config_read_wpa_psk(const char *fname,
177                                        struct hostapd_ssid *ssid)
178 {
179         FILE *f;
180         char buf[128], *pos;
181         int line = 0, ret = 0, len, ok;
182         u8 addr[ETH_ALEN];
183         struct hostapd_wpa_psk *psk;
184
185         if (!fname)
186                 return 0;
187
188         f = fopen(fname, "r");
189         if (!f) {
190                 wpa_printf(MSG_ERROR, "WPA PSK file '%s' not found.", fname);
191                 return -1;
192         }
193
194         while (fgets(buf, sizeof(buf), f)) {
195                 line++;
196
197                 if (buf[0] == '#')
198                         continue;
199                 pos = buf;
200                 while (*pos != '\0') {
201                         if (*pos == '\n') {
202                                 *pos = '\0';
203                                 break;
204                         }
205                         pos++;
206                 }
207                 if (buf[0] == '\0')
208                         continue;
209
210                 if (hwaddr_aton(buf, addr)) {
211                         wpa_printf(MSG_ERROR, "Invalid MAC address '%s' on "
212                                    "line %d in '%s'", buf, line, fname);
213                         ret = -1;
214                         break;
215                 }
216
217                 psk = os_zalloc(sizeof(*psk));
218                 if (psk == NULL) {
219                         wpa_printf(MSG_ERROR, "WPA PSK allocation failed");
220                         ret = -1;
221                         break;
222                 }
223                 if (is_zero_ether_addr(addr))
224                         psk->group = 1;
225                 else
226                         os_memcpy(psk->addr, addr, ETH_ALEN);
227
228                 pos = buf + 17;
229                 if (*pos == '\0') {
230                         wpa_printf(MSG_ERROR, "No PSK on line %d in '%s'",
231                                    line, fname);
232                         os_free(psk);
233                         ret = -1;
234                         break;
235                 }
236                 pos++;
237
238                 ok = 0;
239                 len = os_strlen(pos);
240                 if (len == 64 && hexstr2bin(pos, psk->psk, PMK_LEN) == 0)
241                         ok = 1;
242                 else if (len >= 8 && len < 64) {
243                         pbkdf2_sha1(pos, ssid->ssid, ssid->ssid_len,
244                                     4096, psk->psk, PMK_LEN);
245                         ok = 1;
246                 }
247                 if (!ok) {
248                         wpa_printf(MSG_ERROR, "Invalid PSK '%s' on line %d in "
249                                    "'%s'", pos, line, fname);
250                         os_free(psk);
251                         ret = -1;
252                         break;
253                 }
254
255                 psk->next = ssid->wpa_psk;
256                 ssid->wpa_psk = psk;
257         }
258
259         fclose(f);
260
261         return ret;
262 }
263
264
265 static int hostapd_derive_psk(struct hostapd_ssid *ssid)
266 {
267         ssid->wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
268         if (ssid->wpa_psk == NULL) {
269                 wpa_printf(MSG_ERROR, "Unable to alloc space for PSK");
270                 return -1;
271         }
272         wpa_hexdump_ascii(MSG_DEBUG, "SSID",
273                           (u8 *) ssid->ssid, ssid->ssid_len);
274         wpa_hexdump_ascii_key(MSG_DEBUG, "PSK (ASCII passphrase)",
275                               (u8 *) ssid->wpa_passphrase,
276                               os_strlen(ssid->wpa_passphrase));
277         pbkdf2_sha1(ssid->wpa_passphrase,
278                     ssid->ssid, ssid->ssid_len,
279                     4096, ssid->wpa_psk->psk, PMK_LEN);
280         wpa_hexdump_key(MSG_DEBUG, "PSK (from passphrase)",
281                         ssid->wpa_psk->psk, PMK_LEN);
282         return 0;
283 }
284
285
286 int hostapd_setup_wpa_psk(struct hostapd_bss_config *conf)
287 {
288         struct hostapd_ssid *ssid = &conf->ssid;
289
290         if (ssid->wpa_passphrase != NULL) {
291                 if (ssid->wpa_psk != NULL) {
292                         wpa_printf(MSG_DEBUG, "Using pre-configured WPA PSK "
293                                    "instead of passphrase");
294                 } else {
295                         wpa_printf(MSG_DEBUG, "Deriving WPA PSK based on "
296                                    "passphrase");
297                         if (hostapd_derive_psk(ssid) < 0)
298                                 return -1;
299                 }
300                 ssid->wpa_psk->group = 1;
301         }
302
303         if (ssid->wpa_psk_file) {
304                 if (hostapd_config_read_wpa_psk(ssid->wpa_psk_file,
305                                                 &conf->ssid))
306                         return -1;
307         }
308
309         return 0;
310 }
311
312
313 int hostapd_wep_key_cmp(struct hostapd_wep_keys *a, struct hostapd_wep_keys *b)
314 {
315         int i;
316
317         if (a->idx != b->idx || a->default_len != b->default_len)
318                 return 1;
319         for (i = 0; i < NUM_WEP_KEYS; i++)
320                 if (a->len[i] != b->len[i] ||
321                     os_memcmp(a->key[i], b->key[i], a->len[i]) != 0)
322                         return 1;
323         return 0;
324 }
325
326
327 static void hostapd_config_free_radius(struct hostapd_radius_server *servers,
328                                        int num_servers)
329 {
330         int i;
331
332         for (i = 0; i < num_servers; i++) {
333                 os_free(servers[i].shared_secret);
334         }
335         os_free(servers);
336 }
337
338
339 struct hostapd_radius_attr *
340 hostapd_config_get_radius_attr(struct hostapd_radius_attr *attr, u8 type)
341 {
342         for (; attr; attr = attr->next) {
343                 if (attr->type == type)
344                         return attr;
345         }
346         return NULL;
347 }
348
349
350 static void hostapd_config_free_radius_attr(struct hostapd_radius_attr *attr)
351 {
352         struct hostapd_radius_attr *prev;
353
354         while (attr) {
355                 prev = attr;
356                 attr = attr->next;
357                 wpabuf_free(prev->val);
358                 os_free(prev);
359         }
360 }
361
362
363 static void hostapd_config_free_eap_user(struct hostapd_eap_user *user)
364 {
365         os_free(user->identity);
366         os_free(user->password);
367         os_free(user);
368 }
369
370
371 static void hostapd_config_free_wep(struct hostapd_wep_keys *keys)
372 {
373         int i;
374         for (i = 0; i < NUM_WEP_KEYS; i++) {
375                 os_free(keys->key[i]);
376                 keys->key[i] = NULL;
377         }
378 }
379
380
381 static void hostapd_config_free_bss(struct hostapd_bss_config *conf)
382 {
383         struct hostapd_wpa_psk *psk, *prev;
384         struct hostapd_eap_user *user, *prev_user;
385
386         if (conf == NULL)
387                 return;
388
389         psk = conf->ssid.wpa_psk;
390         while (psk) {
391                 prev = psk;
392                 psk = psk->next;
393                 os_free(prev);
394         }
395
396         os_free(conf->ssid.wpa_passphrase);
397         os_free(conf->ssid.wpa_psk_file);
398         hostapd_config_free_wep(&conf->ssid.wep);
399 #ifdef CONFIG_FULL_DYNAMIC_VLAN
400         os_free(conf->ssid.vlan_tagged_interface);
401 #endif /* CONFIG_FULL_DYNAMIC_VLAN */
402
403         user = conf->eap_user;
404         while (user) {
405                 prev_user = user;
406                 user = user->next;
407                 hostapd_config_free_eap_user(prev_user);
408         }
409
410         os_free(conf->dump_log_name);
411         os_free(conf->eap_req_id_text);
412         os_free(conf->accept_mac);
413         os_free(conf->deny_mac);
414         os_free(conf->nas_identifier);
415         hostapd_config_free_radius(conf->radius->auth_servers,
416                                    conf->radius->num_auth_servers);
417         hostapd_config_free_radius(conf->radius->acct_servers,
418                                    conf->radius->num_acct_servers);
419         hostapd_config_free_radius_attr(conf->radius_auth_req_attr);
420         hostapd_config_free_radius_attr(conf->radius_acct_req_attr);
421         os_free(conf->rsn_preauth_interfaces);
422         os_free(conf->ctrl_interface);
423         os_free(conf->ca_cert);
424         os_free(conf->server_cert);
425         os_free(conf->private_key);
426         os_free(conf->private_key_passwd);
427         os_free(conf->dh_file);
428         os_free(conf->pac_opaque_encr_key);
429         os_free(conf->eap_fast_a_id);
430         os_free(conf->eap_fast_a_id_info);
431         os_free(conf->eap_sim_db);
432         os_free(conf->radius_server_clients);
433         os_free(conf->test_socket);
434         os_free(conf->radius);
435         os_free(conf->radius_das_shared_secret);
436         hostapd_config_free_vlan(conf);
437         if (conf->ssid.dyn_vlan_keys) {
438                 struct hostapd_ssid *ssid = &conf->ssid;
439                 size_t i;
440                 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
441                         if (ssid->dyn_vlan_keys[i] == NULL)
442                                 continue;
443                         hostapd_config_free_wep(ssid->dyn_vlan_keys[i]);
444                         os_free(ssid->dyn_vlan_keys[i]);
445                 }
446                 os_free(ssid->dyn_vlan_keys);
447                 ssid->dyn_vlan_keys = NULL;
448         }
449
450         os_free(conf->time_zone);
451
452 #ifdef CONFIG_IEEE80211R
453         {
454                 struct ft_remote_r0kh *r0kh, *r0kh_prev;
455                 struct ft_remote_r1kh *r1kh, *r1kh_prev;
456
457                 r0kh = conf->r0kh_list;
458                 conf->r0kh_list = NULL;
459                 while (r0kh) {
460                         r0kh_prev = r0kh;
461                         r0kh = r0kh->next;
462                         os_free(r0kh_prev);
463                 }
464
465                 r1kh = conf->r1kh_list;
466                 conf->r1kh_list = NULL;
467                 while (r1kh) {
468                         r1kh_prev = r1kh;
469                         r1kh = r1kh->next;
470                         os_free(r1kh_prev);
471                 }
472         }
473 #endif /* CONFIG_IEEE80211R */
474
475 #ifdef CONFIG_WPS
476         os_free(conf->wps_pin_requests);
477         os_free(conf->device_name);
478         os_free(conf->manufacturer);
479         os_free(conf->model_name);
480         os_free(conf->model_number);
481         os_free(conf->serial_number);
482         os_free(conf->config_methods);
483         os_free(conf->ap_pin);
484         os_free(conf->extra_cred);
485         os_free(conf->ap_settings);
486         os_free(conf->upnp_iface);
487         os_free(conf->friendly_name);
488         os_free(conf->manufacturer_url);
489         os_free(conf->model_description);
490         os_free(conf->model_url);
491         os_free(conf->upc);
492 #endif /* CONFIG_WPS */
493
494         os_free(conf->roaming_consortium);
495         os_free(conf->venue_name);
496
497 #ifdef CONFIG_RADIUS_TEST
498         os_free(conf->dump_msk_file);
499 #endif /* CONFIG_RADIUS_TEST */
500 }
501
502
503 /**
504  * hostapd_config_free - Free hostapd configuration
505  * @conf: Configuration data from hostapd_config_read().
506  */
507 void hostapd_config_free(struct hostapd_config *conf)
508 {
509         size_t i;
510
511         if (conf == NULL)
512                 return;
513
514         for (i = 0; i < conf->num_bss; i++)
515                 hostapd_config_free_bss(&conf->bss[i]);
516         os_free(conf->bss);
517         os_free(conf->supported_rates);
518         os_free(conf->basic_rates);
519
520         os_free(conf);
521 }
522
523
524 /**
525  * hostapd_maclist_found - Find a MAC address from a list
526  * @list: MAC address list
527  * @num_entries: Number of addresses in the list
528  * @addr: Address to search for
529  * @vlan_id: Buffer for returning VLAN ID or %NULL if not needed
530  * Returns: 1 if address is in the list or 0 if not.
531  *
532  * Perform a binary search for given MAC address from a pre-sorted list.
533  */
534 int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries,
535                           const u8 *addr, int *vlan_id)
536 {
537         int start, end, middle, res;
538
539         start = 0;
540         end = num_entries - 1;
541
542         while (start <= end) {
543                 middle = (start + end) / 2;
544                 res = os_memcmp(list[middle].addr, addr, ETH_ALEN);
545                 if (res == 0) {
546                         if (vlan_id)
547                                 *vlan_id = list[middle].vlan_id;
548                         return 1;
549                 }
550                 if (res < 0)
551                         start = middle + 1;
552                 else
553                         end = middle - 1;
554         }
555
556         return 0;
557 }
558
559
560 int hostapd_rate_found(int *list, int rate)
561 {
562         int i;
563
564         if (list == NULL)
565                 return 0;
566
567         for (i = 0; list[i] >= 0; i++)
568                 if (list[i] == rate)
569                         return 1;
570
571         return 0;
572 }
573
574
575 const char * hostapd_get_vlan_id_ifname(struct hostapd_vlan *vlan, int vlan_id)
576 {
577         struct hostapd_vlan *v = vlan;
578         while (v) {
579                 if (v->vlan_id == vlan_id || v->vlan_id == VLAN_ID_WILDCARD)
580                         return v->ifname;
581                 v = v->next;
582         }
583         return NULL;
584 }
585
586
587 const u8 * hostapd_get_psk(const struct hostapd_bss_config *conf,
588                            const u8 *addr, const u8 *prev_psk)
589 {
590         struct hostapd_wpa_psk *psk;
591         int next_ok = prev_psk == NULL;
592
593         for (psk = conf->ssid.wpa_psk; psk != NULL; psk = psk->next) {
594                 if (next_ok &&
595                     (psk->group || os_memcmp(psk->addr, addr, ETH_ALEN) == 0))
596                         return psk->psk;
597
598                 if (psk->psk == prev_psk)
599                         next_ok = 1;
600         }
601
602         return NULL;
603 }
604
605
606 const struct hostapd_eap_user *
607 hostapd_get_eap_user(const struct hostapd_bss_config *conf, const u8 *identity,
608                      size_t identity_len, int phase2)
609 {
610         struct hostapd_eap_user *user = conf->eap_user;
611
612 #ifdef CONFIG_WPS
613         if (conf->wps_state && identity_len == WSC_ID_ENROLLEE_LEN &&
614             os_memcmp(identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN) == 0) {
615                 static struct hostapd_eap_user wsc_enrollee;
616                 os_memset(&wsc_enrollee, 0, sizeof(wsc_enrollee));
617                 wsc_enrollee.methods[0].method = eap_server_get_type(
618                         "WSC", &wsc_enrollee.methods[0].vendor);
619                 return &wsc_enrollee;
620         }
621
622         if (conf->wps_state && identity_len == WSC_ID_REGISTRAR_LEN &&
623             os_memcmp(identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) == 0) {
624                 static struct hostapd_eap_user wsc_registrar;
625                 os_memset(&wsc_registrar, 0, sizeof(wsc_registrar));
626                 wsc_registrar.methods[0].method = eap_server_get_type(
627                         "WSC", &wsc_registrar.methods[0].vendor);
628                 wsc_registrar.password = (u8 *) conf->ap_pin;
629                 wsc_registrar.password_len = conf->ap_pin ?
630                         os_strlen(conf->ap_pin) : 0;
631                 return &wsc_registrar;
632         }
633 #endif /* CONFIG_WPS */
634
635         while (user) {
636                 if (!phase2 && user->identity == NULL) {
637                         /* Wildcard match */
638                         break;
639                 }
640
641                 if (user->phase2 == !!phase2 && user->wildcard_prefix &&
642                     identity_len >= user->identity_len &&
643                     os_memcmp(user->identity, identity, user->identity_len) ==
644                     0) {
645                         /* Wildcard prefix match */
646                         break;
647                 }
648
649                 if (user->phase2 == !!phase2 &&
650                     user->identity_len == identity_len &&
651                     os_memcmp(user->identity, identity, identity_len) == 0)
652                         break;
653                 user = user->next;
654         }
655
656         return user;
657 }