Merge from vendor branch OPENSSH:
[dragonfly.git] / contrib / hostapd-0.4.9 / hostapd.c
1 /*
2  * Host AP (software wireless LAN access point) user space daemon for
3  * Host AP kernel driver
4  * Copyright (c) 2002-2005, Jouni Malinen <jkmaline@cc.hut.fi>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * Alternatively, this software may be distributed under the terms of BSD
11  * license.
12  *
13  * See README and COPYING for more details.
14  */
15
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <netinet/in.h>
20 #include <string.h>
21 #include <signal.h>
22 #include <time.h>
23 #include <syslog.h>
24 #include <stdarg.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <arpa/inet.h>
28
29 #include "eloop.h"
30 #include "hostapd.h"
31 #include "ieee802_1x.h"
32 #include "ieee802_11.h"
33 #include "accounting.h"
34 #include "eapol_sm.h"
35 #include "iapp.h"
36 #include "ap.h"
37 #include "ieee802_11_auth.h"
38 #include "sta_info.h"
39 #include "driver.h"
40 #include "radius_client.h"
41 #include "radius_server.h"
42 #include "wpa.h"
43 #include "ctrl_iface.h"
44 #include "tls.h"
45 #include "eap_sim_db.h"
46 #include "version.h"
47 #include "hostap_common.h"
48
49
50 struct hapd_interfaces {
51         int count;
52         hostapd **hapd;
53 };
54
55 unsigned char rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
56
57
58 extern int wpa_debug_level;
59 extern int wpa_debug_show_keys;
60 extern int wpa_debug_timestamp;
61
62
63 void hostapd_logger(struct hostapd_data *hapd, const u8 *addr,
64                     unsigned int module, int level, const char *fmt, ...)
65 {
66         char *format, *module_str;
67         int maxlen;
68         va_list ap;
69         int conf_syslog_level, conf_stdout_level;
70         unsigned int conf_syslog, conf_stdout;
71
72         maxlen = strlen(fmt) + 100;
73         format = malloc(maxlen);
74         if (!format)
75                 return;
76
77         if (hapd && hapd->conf) {
78                 conf_syslog_level = hapd->conf->logger_syslog_level;
79                 conf_stdout_level = hapd->conf->logger_stdout_level;
80                 conf_syslog = hapd->conf->logger_syslog;
81                 conf_stdout = hapd->conf->logger_stdout;
82         } else {
83                 conf_syslog_level = conf_stdout_level = 0;
84                 conf_syslog = conf_stdout = (unsigned int) -1;
85         }
86
87         switch (module) {
88         case HOSTAPD_MODULE_IEEE80211:
89                 module_str = "IEEE 802.11";
90                 break;
91         case HOSTAPD_MODULE_IEEE8021X:
92                 module_str = "IEEE 802.1X";
93                 break;
94         case HOSTAPD_MODULE_RADIUS:
95                 module_str = "RADIUS";
96                 break;
97         case HOSTAPD_MODULE_WPA:
98                 module_str = "WPA";
99                 break;
100         case HOSTAPD_MODULE_DRIVER:
101                 module_str = "DRIVER";
102                 break;
103         case HOSTAPD_MODULE_IAPP:
104                 module_str = "IAPP";
105                 break;
106         default:
107                 module_str = NULL;
108                 break;
109         }
110
111         if (hapd && hapd->conf && addr)
112                 snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
113                          hapd->conf->iface, MAC2STR(addr),
114                          module_str ? " " : "", module_str, fmt);
115         else if (hapd && hapd->conf)
116                 snprintf(format, maxlen, "%s:%s%s %s",
117                          hapd->conf->iface, module_str ? " " : "",
118                          module_str, fmt);
119         else if (addr)
120                 snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
121                          MAC2STR(addr), module_str ? " " : "",
122                          module_str, fmt);
123         else
124                 snprintf(format, maxlen, "%s%s%s",
125                          module_str, module_str ? ": " : "", fmt);
126
127         if ((conf_stdout & module) && level >= conf_stdout_level) {
128                 wpa_debug_print_timestamp();
129                 va_start(ap, fmt);
130                 vprintf(format, ap);
131                 va_end(ap);
132                 printf("\n");
133         }
134
135         if ((conf_syslog & module) && level >= conf_syslog_level) {
136                 int priority;
137                 switch (level) {
138                 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
139                 case HOSTAPD_LEVEL_DEBUG:
140                         priority = LOG_DEBUG;
141                         break;
142                 case HOSTAPD_LEVEL_INFO:
143                         priority = LOG_INFO;
144                         break;
145                 case HOSTAPD_LEVEL_NOTICE:
146                         priority = LOG_NOTICE;
147                         break;
148                 case HOSTAPD_LEVEL_WARNING:
149                         priority = LOG_WARNING;
150                         break;
151                 default:
152                         priority = LOG_INFO;
153                         break;
154                 }
155                 va_start(ap, fmt);
156                 vsyslog(priority, format, ap);
157                 va_end(ap);
158         }
159
160         free(format);
161 }
162
163
164 const char * hostapd_ip_txt(const struct hostapd_ip_addr *addr, char *buf,
165                             size_t buflen)
166 {
167         if (buflen == 0 || addr == NULL)
168                 return NULL;
169
170         if (addr->af == AF_INET) {
171                 snprintf(buf, buflen, "%s", inet_ntoa(addr->u.v4));
172         } else {
173                 buf[0] = '\0';
174         }
175 #ifdef CONFIG_IPV6
176         if (addr->af == AF_INET6) {
177                 if (inet_ntop(AF_INET6, &addr->u.v6, buf, buflen) == NULL)
178                         buf[0] = '\0';
179         }
180 #endif /* CONFIG_IPV6 */
181
182         return buf;
183 }
184
185
186 static void hostapd_deauth_all_stas(hostapd *hapd)
187 {
188 #if 0
189         u8 addr[ETH_ALEN];
190
191         memset(addr, 0xff, ETH_ALEN);
192         hostapd_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
193 #else
194         /* New Prism2.5/3 STA firmware versions seem to have issues with this
195          * broadcast deauth frame. This gets the firmware in odd state where
196          * nothing works correctly, so let's skip sending this for a while
197          * until the issue has been resolved. */
198 #endif
199 }
200
201
202 /* This function will be called whenever a station associates with the AP */
203 void hostapd_new_assoc_sta(hostapd *hapd, struct sta_info *sta, int reassoc)
204 {
205         if (hapd->tkip_countermeasures) {
206                 hostapd_sta_deauth(hapd, sta->addr,
207                                    WLAN_REASON_MICHAEL_MIC_FAILURE);
208                 return;
209         }
210
211         /* IEEE 802.11F (IAPP) */
212         if (hapd->conf->ieee802_11f)
213                 iapp_new_station(hapd->iapp, sta);
214
215         /* Start accounting here, if IEEE 802.1X and WPA are not used.
216          * IEEE 802.1X/WPA code will start accounting after the station has
217          * been authorized. */
218         if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
219                 accounting_sta_start(hapd, sta);
220
221         /* Start IEEE 802.1X authentication process for new stations */
222         ieee802_1x_new_station(hapd, sta);
223         if (reassoc)
224                 wpa_sm_event(hapd, sta, WPA_REAUTH);
225         else
226                 wpa_new_station(hapd, sta);
227 }
228
229
230 static void handle_term(int sig, void *eloop_ctx, void *signal_ctx)
231 {
232         printf("Signal %d received - terminating\n", sig);
233         eloop_terminate();
234 }
235
236
237 static void handle_reload(int sig, void *eloop_ctx, void *signal_ctx)
238 {
239         struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
240         struct hostapd_config *newconf;
241         int i;
242
243         printf("Signal %d received - reloading configuration\n", sig);
244
245         for (i = 0; i < hapds->count; i++) {
246                 hostapd *hapd = hapds->hapd[i];
247                 newconf = hostapd_config_read(hapd->config_fname);
248                 if (newconf == NULL) {
249                         printf("Failed to read new configuration file - "
250                                "continuing with old.\n");
251                         continue;
252                 }
253                 /* TODO: update dynamic data based on changed configuration
254                  * items (e.g., open/close sockets, remove stations added to
255                  * deny list, etc.) */
256                 radius_client_flush(hapd->radius);
257                 hostapd_config_free(hapd->conf);
258                 hapd->conf = newconf;
259         }
260 }
261
262
263 #ifdef HOSTAPD_DUMP_STATE
264 static void hostapd_dump_state(hostapd *hapd)
265 {
266         FILE *f;
267         time_t now;
268         struct sta_info *sta;
269         int i;
270         char *buf;
271
272         if (!hapd->conf->dump_log_name) {
273                 printf("Dump file not defined - ignoring dump request\n");
274                 return;
275         }
276
277         printf("Dumping hostapd state to '%s'\n", hapd->conf->dump_log_name);
278         f = fopen(hapd->conf->dump_log_name, "w");
279         if (f == NULL) {
280                 printf("Could not open dump file '%s' for writing.\n",
281                        hapd->conf->dump_log_name);
282                 return;
283         }
284
285         time(&now);
286         fprintf(f, "hostapd state dump - %s", ctime(&now));
287
288         for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
289                 fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
290
291                 fprintf(f,
292                         "  AID=%d flags=0x%x %s%s%s%s%s%s\n"
293                         "  capability=0x%x listen_interval=%d\n",
294                         sta->aid,
295                         sta->flags,
296                         (sta->flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
297                         (sta->flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
298                         (sta->flags & WLAN_STA_PS ? "[PS]" : ""),
299                         (sta->flags & WLAN_STA_TIM ? "[TIM]" : ""),
300                         (sta->flags & WLAN_STA_PERM ? "[PERM]" : ""),
301                         (sta->flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" :
302                          ""),
303                         sta->capability,
304                         sta->listen_interval);
305
306                 fprintf(f, "  supported_rates=");
307                 for (i = 0; i < sizeof(sta->supported_rates); i++)
308                         if (sta->supported_rates[i] != 0)
309                                 fprintf(f, "%02x ", sta->supported_rates[i]);
310                 fprintf(f, "%s%s%s%s\n",
311                         (sta->tx_supp_rates & WLAN_RATE_1M ? "[1M]" : ""),
312                         (sta->tx_supp_rates & WLAN_RATE_2M ? "[2M]" : ""),
313                         (sta->tx_supp_rates & WLAN_RATE_5M5 ? "[5.5M]" : ""),
314                         (sta->tx_supp_rates & WLAN_RATE_11M ? "[11M]" : ""));
315
316                 fprintf(f,
317                         "  timeout_next=%s\n",
318                         (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
319                          (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
320                           "DEAUTH")));
321
322                 ieee802_1x_dump_state(f, "  ", sta);
323         }
324
325         buf = malloc(4096);
326         if (buf) {
327                 int count = radius_client_get_mib(hapd->radius, buf, 4096);
328                 if (count < 0)
329                         count = 0;
330                 else if (count > 4095)
331                         count = 4095;
332                 buf[count] = '\0';
333                 fprintf(f, "%s", buf);
334
335                 count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
336                 if (count < 0)
337                         count = 0;
338                 else if (count > 4095)
339                         count = 4095;
340                 buf[count] = '\0';
341                 fprintf(f, "%s", buf);
342                 free(buf);
343         }
344         fclose(f);
345 }
346 #endif /* HOSTAPD_DUMP_STATE */
347
348
349 static void handle_dump_state(int sig, void *eloop_ctx, void *signal_ctx)
350 {
351 #ifdef HOSTAPD_DUMP_STATE
352         struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
353         int i;
354
355         for (i = 0; i < hapds->count; i++) {
356                 hostapd *hapd = hapds->hapd[i];
357                 hostapd_dump_state(hapd);
358         }
359 #endif /* HOSTAPD_DUMP_STATE */
360 }
361
362
363 static void hostapd_cleanup(struct hostapd_data *hapd)
364 {
365         hostapd_ctrl_iface_deinit(hapd);
366
367         free(hapd->default_wep_key);
368         hapd->default_wep_key = NULL;
369         iapp_deinit(hapd->iapp);
370         accounting_deinit(hapd);
371         wpa_deinit(hapd);
372         ieee802_1x_deinit(hapd);
373         hostapd_acl_deinit(hapd);
374         radius_client_deinit(hapd->radius);
375         hapd->radius = NULL;
376         radius_server_deinit(hapd->radius_srv);
377         hapd->radius_srv = NULL;
378
379         hostapd_wireless_event_deinit(hapd);
380
381         if (hapd->driver)
382                 hostapd_driver_deinit(hapd);
383
384         hostapd_config_free(hapd->conf);
385         hapd->conf = NULL;
386
387         free(hapd->config_fname);
388
389 #ifdef EAP_TLS_FUNCS
390         if (hapd->ssl_ctx) {
391                 tls_deinit(hapd->ssl_ctx);
392                 hapd->ssl_ctx = NULL;
393         }
394 #endif /* EAP_TLS_FUNCS */
395
396         if (hapd->eap_sim_db_priv)
397                 eap_sim_db_deinit(hapd->eap_sim_db_priv);
398 }
399
400
401 static int hostapd_flush_old_stations(hostapd *hapd)
402 {
403         int ret = 0;
404
405         printf("Flushing old station entries\n");
406         if (hostapd_flush(hapd)) {
407                 printf("Could not connect to kernel driver.\n");
408                 ret = -1;
409         }
410         printf("Deauthenticate all stations\n");
411         hostapd_deauth_all_stas(hapd);
412
413         return ret;
414 }
415
416
417 static int hostapd_setup_interface(struct hostapd_data *hapd)
418 {
419         struct hostapd_config *conf = hapd->conf;
420         u8 ssid[HOSTAPD_SSID_LEN + 1];
421         int ssid_len, set_ssid;
422         int ret = 0;
423
424         if (hostapd_driver_init(hapd)) {
425                 printf("%s driver initialization failed.\n",
426                         hapd->driver ? hapd->driver->name : "Unknown");
427                 hapd->driver = NULL;
428                 return -1;
429         }
430
431         /*
432          * Fetch the SSID from the system and use it or,
433          * if one was specified in the config file, verify they
434          * match.
435          */
436         ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
437         if (ssid_len < 0) {
438                 printf("Could not read SSID from system\n");
439                 return -1;
440         }
441         if (conf->ssid_set) {
442                 /*
443                  * If SSID is specified in the config file and it differs
444                  * from what is being used then force installation of the
445                  * new SSID.
446                  */
447                 set_ssid = (conf->ssid_len != ssid_len ||
448                             memcmp(conf->ssid, ssid, ssid_len) != 0);
449         } else {
450                 /*
451                  * No SSID in the config file; just use the one we got
452                  * from the system.
453                  */
454                 set_ssid = 0;
455                 conf->ssid_len = ssid_len;
456                 memcpy(conf->ssid, ssid, conf->ssid_len);
457                 conf->ssid[conf->ssid_len] = '\0';
458         }
459
460         printf("Using interface %s with hwaddr " MACSTR " and ssid '%s'\n",
461                hapd->conf->iface, MAC2STR(hapd->own_addr), hapd->conf->ssid);
462
463         if (hostapd_setup_wpa_psk(conf)) {
464                 printf("WPA-PSK setup failed.\n");
465                 return -1;
466         }
467
468         /* Set SSID for the kernel driver (to be used in beacon and probe
469          * response frames) */
470         if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid,
471                                          conf->ssid_len)) {
472                 printf("Could not set SSID for kernel driver\n");
473                 return -1;
474         }
475
476         if (HOSTAPD_DEBUG_COND(HOSTAPD_DEBUG_MSGDUMPS))
477                 conf->radius->msg_dumps = 1;
478         hapd->radius = radius_client_init(hapd, conf->radius);
479         if (hapd->radius == NULL) {
480                 printf("RADIUS client initialization failed.\n");
481                 return -1;
482         }
483         if (conf->radius_server_clients) {
484                 struct radius_server_conf srv;
485                 memset(&srv, 0, sizeof(srv));
486                 srv.client_file = conf->radius_server_clients;
487                 srv.auth_port = conf->radius_server_auth_port;
488                 srv.hostapd_conf = conf;
489                 srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
490                 srv.ssl_ctx = hapd->ssl_ctx;
491                 srv.ipv6 = conf->radius_server_ipv6;
492                 hapd->radius_srv = radius_server_init(&srv);
493                 if (hapd->radius_srv == NULL) {
494                         printf("RADIUS server initialization failed.\n");
495                         return -1;
496                 }
497         }
498         if (hostapd_acl_init(hapd)) {
499                 printf("ACL initialization failed.\n");
500                 return -1;
501         }
502         if (ieee802_1x_init(hapd)) {
503                 printf("IEEE 802.1X initialization failed.\n");
504                 return -1;
505         }
506
507         if (hapd->conf->wpa && wpa_init(hapd)) {
508                 printf("WPA initialization failed.\n");
509                 return -1;
510         }
511
512         if (accounting_init(hapd)) {
513                 printf("Accounting initialization failed.\n");
514                 return -1;
515         }
516
517         if (hapd->conf->ieee802_11f &&
518             (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
519                 printf("IEEE 802.11F (IAPP) initialization failed.\n");
520                 return -1;
521         }
522
523         if (hostapd_wireless_event_init(hapd) < 0)
524                 return -1;
525
526         if (hostapd_flush_old_stations(hapd))
527                 return -1;
528
529         if (hostapd_ctrl_iface_init(hapd)) {
530                 printf("Failed to setup control interface\n");
531                 ret = -1;
532         }
533
534         return ret;
535 }
536
537
538 struct driver {
539         struct driver *next;
540         char *name;
541         const struct driver_ops *ops;
542 };
543 static struct driver *drivers = NULL;
544
545 void driver_register(const char *name, const struct driver_ops *ops)
546 {
547         struct driver *d;
548
549         d = malloc(sizeof(struct driver));
550         if (d == NULL) {
551                 printf("Failed to register driver %s!\n", name);
552                 return;
553         }
554         d->name = strdup(name);
555         if (d->name == NULL) {
556                 printf("Failed to register driver %s!\n", name);
557                 free(d);
558                 return;
559         }
560         d->ops = ops;
561
562         d->next = drivers;
563         drivers = d;
564 }
565
566
567 void driver_unregister(const char *name)
568 {
569         struct driver *p, **pp;
570
571         for (pp = &drivers; (p = *pp) != NULL; pp = &p->next) {
572                 if (strcasecmp(p->name, name) == 0) {
573                         *pp = p->next;
574                         p->next = NULL;
575                         free(p->name);
576                         free(p);
577                         break;
578                 }
579         }
580 }
581
582
583 static void driver_unregister_all(void)
584 {
585         struct driver *p, *pp;
586         p = drivers;
587         drivers = NULL;
588         while (p) {
589                 pp = p;
590                 p = p->next;
591                 free(pp->name);
592                 free(pp);
593         }
594 }
595
596
597 const struct driver_ops * driver_lookup(const char *name)
598 {
599         struct driver *p;
600
601         if (strcmp(name, "default") == 0) {
602                 p = drivers;
603                 while (p && p->next)
604                         p = p->next;
605                 return p->ops;
606         }
607
608         for (p = drivers; p != NULL; p = p->next) {
609                 if (strcasecmp(p->name, name) == 0)
610                         return p->ops;
611         }
612
613         return NULL;
614 }
615
616
617 static void show_version(void)
618 {
619         fprintf(stderr,
620                 "hostapd v" VERSION_STR "\n"
621                 "User space daemon for IEEE 802.11 AP management,\n"
622                 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
623                 "Copyright (c) 2002-2005, Jouni Malinen <jkmaline@cc.hut.fi> "
624                 "and contributors\n");
625 }
626
627
628 static void usage(void)
629 {
630         show_version();
631         fprintf(stderr,
632                 "\n"
633                 "usage: hostapd [-hdBKt] <configuration file(s)>\n"
634                 "\n"
635                 "options:\n"
636                 "   -h   show this usage\n"
637                 "   -d   show more debug messages (-dd for even more)\n"
638                 "   -B   run daemon in the background\n"
639                 "   -K   include key data in debug messages\n"
640                 "   -t   include timestamps in some debug messages\n"
641                 "   -v   show hostapd version\n");
642
643         exit(1);
644 }
645
646
647 static hostapd * hostapd_init(const char *config_file)
648 {
649         hostapd *hapd;
650
651         hapd = malloc(sizeof(*hapd));
652         if (hapd == NULL) {
653                 printf("Could not allocate memory for hostapd data\n");
654                 goto fail;
655         }
656         memset(hapd, 0, sizeof(*hapd));
657
658         hapd->config_fname = strdup(config_file);
659         if (hapd->config_fname == NULL) {
660                 printf("Could not allocate memory for config_fname\n");
661                 goto fail;
662         }
663
664         hapd->conf = hostapd_config_read(hapd->config_fname);
665         if (hapd->conf == NULL) {
666                 goto fail;
667         }
668
669         if (hapd->conf->individual_wep_key_len > 0) {
670                 /* use key0 in individual key and key1 in broadcast key */
671                 hapd->default_wep_key_idx = 1;
672         }
673
674 #ifdef EAP_TLS_FUNCS
675         if (hapd->conf->eap_server &&
676             (hapd->conf->ca_cert || hapd->conf->server_cert)) {
677                 hapd->ssl_ctx = tls_init(NULL);
678                 if (hapd->ssl_ctx == NULL) {
679                         printf("Failed to initialize TLS\n");
680                         goto fail;
681                 }
682                 if (tls_global_ca_cert(hapd->ssl_ctx, hapd->conf->ca_cert)) {
683                         printf("Failed to load CA certificate (%s)\n",
684                                 hapd->conf->ca_cert);
685                         goto fail;
686                 }
687                 if (tls_global_client_cert(hapd->ssl_ctx,
688                                            hapd->conf->server_cert)) {
689                         printf("Failed to load server certificate (%s)\n",
690                                 hapd->conf->server_cert);
691                         goto fail;
692                 }
693                 if (tls_global_private_key(hapd->ssl_ctx,
694                                            hapd->conf->private_key,
695                                            hapd->conf->private_key_passwd)) {
696                         printf("Failed to load private key (%s)\n",
697                                hapd->conf->private_key);
698                         goto fail;
699                 }
700                 if (tls_global_set_verify(hapd->ssl_ctx,
701                                           hapd->conf->check_crl)) {
702                         printf("Failed to enable check_crl\n");
703                         goto fail;
704                 }
705         }
706 #endif /* EAP_TLS_FUNCS */
707
708         if (hapd->conf->eap_sim_db) {
709                 hapd->eap_sim_db_priv =
710                         eap_sim_db_init(hapd->conf->eap_sim_db);
711                 if (hapd->eap_sim_db_priv == NULL) {
712                         printf("Failed to initialize EAP-SIM database "
713                                "interface\n");
714                         goto fail;
715                 }
716         }
717
718         if (hapd->conf->assoc_ap)
719                 hapd->assoc_ap_state = WAIT_BEACON;
720
721         /* FIX: need to fix this const vs. not */
722         hapd->driver = (struct driver_ops *) hapd->conf->driver;
723
724         return hapd;
725
726 fail:
727         if (hapd) {
728                 if (hapd->ssl_ctx)
729                         tls_deinit(hapd->ssl_ctx);
730                 if (hapd->conf)
731                         hostapd_config_free(hapd->conf);
732                 free(hapd->config_fname);
733                 free(hapd);
734         }
735         return NULL;
736 }
737
738
739 void register_drivers(void);
740
741 int main(int argc, char *argv[])
742 {
743         struct hapd_interfaces interfaces;
744         int ret = 1, i, j;
745         int c, debug = 0, daemonize = 0;
746
747         for (;;) {
748                 c = getopt(argc, argv, "BdhKtv");
749                 if (c < 0)
750                         break;
751                 switch (c) {
752                 case 'h':
753                         usage();
754                         break;
755                 case 'd':
756                         debug++;
757                         break;
758                 case 'B':
759                         daemonize++;
760                         break;
761                 case 'K':
762                         wpa_debug_show_keys++;
763                         break;
764                 case 't':
765                         wpa_debug_timestamp++;
766                         break;
767                 case 'v':
768                         show_version();
769                         exit(1);
770                         break;
771
772                 default:
773                         usage();
774                         break;
775                 }
776         }
777
778         if (optind == argc)
779                 usage();
780
781         register_drivers();             /* NB: generated by Makefile */
782
783         interfaces.count = argc - optind;
784
785         interfaces.hapd = malloc(interfaces.count * sizeof(hostapd *));
786         if (interfaces.hapd == NULL) {
787                 printf("malloc failed\n");
788                 exit(1);
789         }
790
791         eloop_init(&interfaces);
792         eloop_register_signal(SIGHUP, handle_reload, NULL);
793         eloop_register_signal(SIGINT, handle_term, NULL);
794         eloop_register_signal(SIGTERM, handle_term, NULL);
795         eloop_register_signal(SIGUSR1, handle_dump_state, NULL);
796
797         for (i = 0; i < interfaces.count; i++) {
798                 printf("Configuration file: %s\n", argv[optind + i]);
799                 interfaces.hapd[i] = hostapd_init(argv[optind + i]);
800                 if (!interfaces.hapd[i])
801                         goto out;
802                 for (j = 0; j < debug; j++) {
803                         if (interfaces.hapd[i]->conf->logger_stdout_level > 0)
804                                 interfaces.hapd[i]->conf->
805                                         logger_stdout_level--;
806                         interfaces.hapd[i]->conf->debug++;
807                 }
808                 if (hostapd_setup_interface(interfaces.hapd[i]))
809                         goto out;
810                 wpa_debug_level -= interfaces.hapd[0]->conf->debug;
811         }
812
813         if (daemonize && daemon(0, 0)) {
814                 perror("daemon");
815                 goto out;
816         }
817
818         openlog("hostapd", 0, LOG_DAEMON);
819
820         eloop_run();
821
822         for (i = 0; i < interfaces.count; i++) {
823                 hostapd_free_stas(interfaces.hapd[i]);
824                 hostapd_flush_old_stations(interfaces.hapd[i]);
825         }
826
827         ret = 0;
828
829  out:
830         for (i = 0; i < interfaces.count; i++) {
831                 if (!interfaces.hapd[i])
832                         continue;
833
834                 hostapd_cleanup(interfaces.hapd[i]);
835                 free(interfaces.hapd[i]);
836         }
837         free(interfaces.hapd);
838
839         eloop_destroy();
840
841         closelog();
842
843         driver_unregister_all();
844
845         return ret;
846 }