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