blob: fb82a0238b981648f22a3502ae9fb4c64d5fcd26 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / main()
3 * Copyright (c) 2002-2011, Jouni Malinen <j@w1.fi>
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10#ifndef CONFIG_NATIVE_WINDOWS
11#include <syslog.h>
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -070012#include <grp.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070013#endif /* CONFIG_NATIVE_WINDOWS */
14
15#include "utils/common.h"
16#include "utils/eloop.h"
17#include "crypto/random.h"
18#include "crypto/tls.h"
19#include "common/version.h"
20#include "drivers/driver.h"
21#include "eap_server/eap.h"
22#include "eap_server/tncs.h"
23#include "ap/hostapd.h"
24#include "ap/ap_config.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070025#include "ap/ap_drv_ops.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026#include "config_file.h"
27#include "eap_register.h"
28#include "dump_state.h"
29#include "ctrl_iface.h"
30
31
32extern int wpa_debug_level;
33extern int wpa_debug_show_keys;
34extern int wpa_debug_timestamp;
35
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080036extern struct wpa_driver_ops *wpa_drivers[];
37
38
39struct hapd_global {
40 void **drv_priv;
41 size_t drv_count;
42};
43
44static struct hapd_global global;
45
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070046
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070047#ifndef CONFIG_NO_HOSTAPD_LOGGER
48static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
49 int level, const char *txt, size_t len)
50{
51 struct hostapd_data *hapd = ctx;
52 char *format, *module_str;
53 int maxlen;
54 int conf_syslog_level, conf_stdout_level;
55 unsigned int conf_syslog, conf_stdout;
56
57 maxlen = len + 100;
58 format = os_malloc(maxlen);
59 if (!format)
60 return;
61
62 if (hapd && hapd->conf) {
63 conf_syslog_level = hapd->conf->logger_syslog_level;
64 conf_stdout_level = hapd->conf->logger_stdout_level;
65 conf_syslog = hapd->conf->logger_syslog;
66 conf_stdout = hapd->conf->logger_stdout;
67 } else {
68 conf_syslog_level = conf_stdout_level = 0;
69 conf_syslog = conf_stdout = (unsigned int) -1;
70 }
71
72 switch (module) {
73 case HOSTAPD_MODULE_IEEE80211:
74 module_str = "IEEE 802.11";
75 break;
76 case HOSTAPD_MODULE_IEEE8021X:
77 module_str = "IEEE 802.1X";
78 break;
79 case HOSTAPD_MODULE_RADIUS:
80 module_str = "RADIUS";
81 break;
82 case HOSTAPD_MODULE_WPA:
83 module_str = "WPA";
84 break;
85 case HOSTAPD_MODULE_DRIVER:
86 module_str = "DRIVER";
87 break;
88 case HOSTAPD_MODULE_IAPP:
89 module_str = "IAPP";
90 break;
91 case HOSTAPD_MODULE_MLME:
92 module_str = "MLME";
93 break;
94 default:
95 module_str = NULL;
96 break;
97 }
98
99 if (hapd && hapd->conf && addr)
100 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
101 hapd->conf->iface, MAC2STR(addr),
102 module_str ? " " : "", module_str, txt);
103 else if (hapd && hapd->conf)
104 os_snprintf(format, maxlen, "%s:%s%s %s",
105 hapd->conf->iface, module_str ? " " : "",
106 module_str, txt);
107 else if (addr)
108 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
109 MAC2STR(addr), module_str ? " " : "",
110 module_str, txt);
111 else
112 os_snprintf(format, maxlen, "%s%s%s",
113 module_str, module_str ? ": " : "", txt);
114
115 if ((conf_stdout & module) && level >= conf_stdout_level) {
116 wpa_debug_print_timestamp();
117 printf("%s\n", format);
118 }
119
120#ifndef CONFIG_NATIVE_WINDOWS
121 if ((conf_syslog & module) && level >= conf_syslog_level) {
122 int priority;
123 switch (level) {
124 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
125 case HOSTAPD_LEVEL_DEBUG:
126 priority = LOG_DEBUG;
127 break;
128 case HOSTAPD_LEVEL_INFO:
129 priority = LOG_INFO;
130 break;
131 case HOSTAPD_LEVEL_NOTICE:
132 priority = LOG_NOTICE;
133 break;
134 case HOSTAPD_LEVEL_WARNING:
135 priority = LOG_WARNING;
136 break;
137 default:
138 priority = LOG_INFO;
139 break;
140 }
141 syslog(priority, "%s", format);
142 }
143#endif /* CONFIG_NATIVE_WINDOWS */
144
145 os_free(format);
146}
147#endif /* CONFIG_NO_HOSTAPD_LOGGER */
148
149
150/**
151 * hostapd_init - Allocate and initialize per-interface data
152 * @config_file: Path to the configuration file
153 * Returns: Pointer to the allocated interface data or %NULL on failure
154 *
155 * This function is used to allocate main data structures for per-interface
156 * data. The allocated data buffer will be freed by calling
157 * hostapd_cleanup_iface().
158 */
159static struct hostapd_iface * hostapd_init(const char *config_file)
160{
161 struct hostapd_iface *hapd_iface = NULL;
162 struct hostapd_config *conf = NULL;
163 struct hostapd_data *hapd;
164 size_t i;
165
166 hapd_iface = os_zalloc(sizeof(*hapd_iface));
167 if (hapd_iface == NULL)
168 goto fail;
169
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700170 hapd_iface->config_fname = os_strdup(config_file);
171 if (hapd_iface->config_fname == NULL)
172 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700173
174 conf = hostapd_config_read(hapd_iface->config_fname);
175 if (conf == NULL)
176 goto fail;
177 hapd_iface->conf = conf;
178
179 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700180 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700181 sizeof(struct hostapd_data *));
182 if (hapd_iface->bss == NULL)
183 goto fail;
184
185 for (i = 0; i < conf->num_bss; i++) {
186 hapd = hapd_iface->bss[i] =
187 hostapd_alloc_bss_data(hapd_iface, conf,
188 &conf->bss[i]);
189 if (hapd == NULL)
190 goto fail;
191 hapd->msg_ctx = hapd;
192 }
193
194 return hapd_iface;
195
196fail:
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700197 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
198 config_file);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700199 if (conf)
200 hostapd_config_free(conf);
201 if (hapd_iface) {
202 os_free(hapd_iface->config_fname);
203 os_free(hapd_iface->bss);
204 os_free(hapd_iface);
205 }
206 return NULL;
207}
208
209
210static int hostapd_driver_init(struct hostapd_iface *iface)
211{
212 struct wpa_init_params params;
213 size_t i;
214 struct hostapd_data *hapd = iface->bss[0];
215 struct hostapd_bss_config *conf = hapd->conf;
216 u8 *b = conf->bssid;
217 struct wpa_driver_capa capa;
218
219 if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
220 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
221 return -1;
222 }
223
224 /* Initialize the driver interface */
225 if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
226 b = NULL;
227
228 os_memset(&params, 0, sizeof(params));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800229 for (i = 0; wpa_drivers[i]; i++) {
230 if (wpa_drivers[i] != hapd->driver)
231 continue;
232
233 if (global.drv_priv[i] == NULL &&
234 wpa_drivers[i]->global_init) {
235 global.drv_priv[i] = wpa_drivers[i]->global_init();
236 if (global.drv_priv[i] == NULL) {
237 wpa_printf(MSG_ERROR, "Failed to initialize "
238 "driver '%s'",
239 wpa_drivers[i]->name);
240 return -1;
241 }
242 }
243
244 params.global_priv = global.drv_priv[i];
245 break;
246 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700247 params.bssid = b;
248 params.ifname = hapd->conf->iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700249 params.ssid = hapd->conf->ssid.ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700250 params.ssid_len = hapd->conf->ssid.ssid_len;
251 params.test_socket = hapd->conf->test_socket;
252 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
253
254 params.num_bridge = hapd->iface->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700255 params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700256 if (params.bridge == NULL)
257 return -1;
258 for (i = 0; i < hapd->iface->num_bss; i++) {
259 struct hostapd_data *bss = hapd->iface->bss[i];
260 if (bss->conf->bridge[0])
261 params.bridge[i] = bss->conf->bridge;
262 }
263
264 params.own_addr = hapd->own_addr;
265
266 hapd->drv_priv = hapd->driver->hapd_init(hapd, &params);
267 os_free(params.bridge);
268 if (hapd->drv_priv == NULL) {
269 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
270 hapd->driver->name);
271 hapd->driver = NULL;
272 return -1;
273 }
274
275 if (hapd->driver->get_capa &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800276 hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700277 iface->drv_flags = capa.flags;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800278 iface->probe_resp_offloads = capa.probe_resp_offloads;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700279 iface->extended_capa = capa.extended_capa;
280 iface->extended_capa_mask = capa.extended_capa_mask;
281 iface->extended_capa_len = capa.extended_capa_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800282 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700283
284 return 0;
285}
286
287
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700288static struct hostapd_iface *
289hostapd_interface_init(struct hapd_interfaces *interfaces,
290 const char *config_fname, int debug)
291{
292 struct hostapd_iface *iface;
293 int k;
294
295 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
296 iface = hostapd_init(config_fname);
297 if (!iface)
298 return NULL;
299 iface->interfaces = interfaces;
300
301 for (k = 0; k < debug; k++) {
302 if (iface->bss[0]->conf->logger_stdout_level > 0)
303 iface->bss[0]->conf->logger_stdout_level--;
304 }
305
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700306 if (iface->conf->bss[0].iface[0] == '\0' &&
307 !hostapd_drv_none(iface->bss[0])) {
308 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
309 config_fname);
310 hostapd_interface_deinit_free(iface);
311 return NULL;
312 }
313
314 if (hostapd_driver_init(iface) ||
315 hostapd_setup_interface(iface)) {
316 hostapd_interface_deinit_free(iface);
317 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700318 }
319
320 return iface;
321}
322
323
324/**
325 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
326 */
327static void handle_term(int sig, void *signal_ctx)
328{
329 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
330 eloop_terminate();
331}
332
333
334#ifndef CONFIG_NATIVE_WINDOWS
335
336static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
337{
338 if (hostapd_reload_config(iface) < 0) {
339 wpa_printf(MSG_WARNING, "Failed to read new configuration "
340 "file - continuing with old.");
341 }
342 return 0;
343}
344
345
346/**
347 * handle_reload - SIGHUP handler to reload configuration
348 */
349static void handle_reload(int sig, void *signal_ctx)
350{
351 struct hapd_interfaces *interfaces = signal_ctx;
352 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
353 sig);
354 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
355}
356
357
358static void handle_dump_state(int sig, void *signal_ctx)
359{
360#ifdef HOSTAPD_DUMP_STATE
361 struct hapd_interfaces *interfaces = signal_ctx;
362 hostapd_for_each_interface(interfaces, handle_dump_state_iface, NULL);
363#endif /* HOSTAPD_DUMP_STATE */
364}
365#endif /* CONFIG_NATIVE_WINDOWS */
366
367
Jouni Malinen75ecf522011-06-27 15:19:46 -0700368static int hostapd_global_init(struct hapd_interfaces *interfaces,
369 const char *entropy_file)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700370{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800371 int i;
372
373 os_memset(&global, 0, sizeof(global));
374
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375 hostapd_logger_register_cb(hostapd_logger_cb);
376
377 if (eap_server_register_methods()) {
378 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
379 return -1;
380 }
381
382 if (eloop_init()) {
383 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
384 return -1;
385 }
386
Jouni Malinen75ecf522011-06-27 15:19:46 -0700387 random_init(entropy_file);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700388
389#ifndef CONFIG_NATIVE_WINDOWS
390 eloop_register_signal(SIGHUP, handle_reload, interfaces);
391 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
392#endif /* CONFIG_NATIVE_WINDOWS */
393 eloop_register_signal_terminate(handle_term, interfaces);
394
395#ifndef CONFIG_NATIVE_WINDOWS
396 openlog("hostapd", 0, LOG_DAEMON);
397#endif /* CONFIG_NATIVE_WINDOWS */
398
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800399 for (i = 0; wpa_drivers[i]; i++)
400 global.drv_count++;
401 if (global.drv_count == 0) {
402 wpa_printf(MSG_ERROR, "No drivers enabled");
403 return -1;
404 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700405 global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800406 if (global.drv_priv == NULL)
407 return -1;
408
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700409 return 0;
410}
411
412
413static void hostapd_global_deinit(const char *pid_file)
414{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800415 int i;
416
417 for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
418 if (!global.drv_priv[i])
419 continue;
420 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
421 }
422 os_free(global.drv_priv);
423 global.drv_priv = NULL;
424
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700425#ifdef EAP_SERVER_TNC
426 tncs_global_deinit();
427#endif /* EAP_SERVER_TNC */
428
429 random_deinit();
430
431 eloop_destroy();
432
433#ifndef CONFIG_NATIVE_WINDOWS
434 closelog();
435#endif /* CONFIG_NATIVE_WINDOWS */
436
437 eap_server_unregister_methods();
438
439 os_daemonize_terminate(pid_file);
440}
441
442
443static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
444 const char *pid_file)
445{
446#ifdef EAP_SERVER_TNC
447 int tnc = 0;
448 size_t i, k;
449
450 for (i = 0; !tnc && i < ifaces->count; i++) {
451 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
452 if (ifaces->iface[i]->bss[0]->conf->tnc) {
453 tnc++;
454 break;
455 }
456 }
457 }
458
459 if (tnc && tncs_global_init() < 0) {
460 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
461 return -1;
462 }
463#endif /* EAP_SERVER_TNC */
464
465 if (daemonize && os_daemonize(pid_file)) {
466 perror("daemon");
467 return -1;
468 }
469
470 eloop_run();
471
472 return 0;
473}
474
475
476static void show_version(void)
477{
478 fprintf(stderr,
479 "hostapd v" VERSION_STR "\n"
480 "User space daemon for IEEE 802.11 AP management,\n"
481 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800482 "Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi> "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700483 "and contributors\n");
484}
485
486
487static void usage(void)
488{
489 show_version();
490 fprintf(stderr,
491 "\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -0700492 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700493 "\\\n"
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700494 " [-g <global ctrl_iface>] [-G <group>] \\\n"
495 " <configuration file(s)>\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700496 "\n"
497 "options:\n"
498 " -h show this usage\n"
499 " -d show more debug messages (-dd for even more)\n"
500 " -B run daemon in the background\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -0700501 " -e entropy file\n"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700502 " -g global control interface path\n"
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700503 " -G group for control interfaces\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700504 " -P PID file\n"
505 " -K include key data in debug messages\n"
506#ifdef CONFIG_DEBUG_FILE
507 " -f log output to debug file instead of stdout\n"
508#endif /* CONFIG_DEBUG_FILE */
509 " -t include timestamps in some debug messages\n"
510 " -v show hostapd version\n");
511
512 exit(1);
513}
514
515
516static const char * hostapd_msg_ifname_cb(void *ctx)
517{
518 struct hostapd_data *hapd = ctx;
519 if (hapd && hapd->iconf && hapd->iconf->bss)
520 return hapd->iconf->bss->iface;
521 return NULL;
522}
523
524
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700525static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
526 const char *path)
527{
528 char *pos;
529 os_free(interfaces->global_iface_path);
530 interfaces->global_iface_path = os_strdup(path);
531 if (interfaces->global_iface_path == NULL)
532 return -1;
533 pos = os_strrchr(interfaces->global_iface_path, '/');
534 if (pos == NULL) {
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700535 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
536 "file");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700537 os_free(interfaces->global_iface_path);
538 interfaces->global_iface_path = NULL;
539 return -1;
540 }
541
542 *pos = '\0';
543 interfaces->global_iface_name = pos + 1;
544
545 return 0;
546}
547
548
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700549static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
550 const char *group)
551{
552#ifndef CONFIG_NATIVE_WINDOWS
553 struct group *grp;
554 grp = getgrnam(group);
555 if (grp == NULL) {
556 wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
557 return -1;
558 }
559 interfaces->ctrl_iface_group = grp->gr_gid;
560#endif /* CONFIG_NATIVE_WINDOWS */
561 return 0;
562}
563
564
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700565int main(int argc, char *argv[])
566{
567 struct hapd_interfaces interfaces;
568 int ret = 1;
569 size_t i;
570 int c, debug = 0, daemonize = 0;
571 char *pid_file = NULL;
572 const char *log_file = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700573 const char *entropy_file = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700574
575 if (os_program_init())
576 return -1;
577
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700578 os_memset(&interfaces, 0, sizeof(interfaces));
579 interfaces.reload_config = hostapd_reload_config;
580 interfaces.config_read_cb = hostapd_config_read;
581 interfaces.for_each_interface = hostapd_for_each_interface;
582 interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
583 interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
584 interfaces.driver_init = hostapd_driver_init;
585 interfaces.global_iface_path = NULL;
586 interfaces.global_iface_name = NULL;
587 interfaces.global_ctrl_sock = -1;
588
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700589 for (;;) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700590 c = getopt(argc, argv, "Bde:f:hKP:tvg:G:");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700591 if (c < 0)
592 break;
593 switch (c) {
594 case 'h':
595 usage();
596 break;
597 case 'd':
598 debug++;
599 if (wpa_debug_level > 0)
600 wpa_debug_level--;
601 break;
602 case 'B':
603 daemonize++;
604 break;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700605 case 'e':
606 entropy_file = optarg;
607 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700608 case 'f':
609 log_file = optarg;
610 break;
611 case 'K':
612 wpa_debug_show_keys++;
613 break;
614 case 'P':
615 os_free(pid_file);
616 pid_file = os_rel2abs_path(optarg);
617 break;
618 case 't':
619 wpa_debug_timestamp++;
620 break;
621 case 'v':
622 show_version();
623 exit(1);
624 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700625 case 'g':
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700626 if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
627 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700628 break;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700629 case 'G':
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700630 if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
631 return -1;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700632 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700633 default:
634 usage();
635 break;
636 }
637 }
638
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700639 if (optind == argc && interfaces.global_iface_path == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700640 usage();
641
642 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
643
644 if (log_file)
645 wpa_debug_open_file(log_file);
646
647 interfaces.count = argc - optind;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700648 if (interfaces.count) {
649 interfaces.iface = os_calloc(interfaces.count,
650 sizeof(struct hostapd_iface *));
651 if (interfaces.iface == NULL) {
652 wpa_printf(MSG_ERROR, "malloc failed");
653 return -1;
654 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700655 }
656
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700657 if (hostapd_global_init(&interfaces, entropy_file)) {
658 wpa_printf(MSG_ERROR, "Failed to initilize global context");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700659 return -1;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700660 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700661
662 /* Initialize interfaces */
663 for (i = 0; i < interfaces.count; i++) {
664 interfaces.iface[i] = hostapd_interface_init(&interfaces,
665 argv[optind + i],
666 debug);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700667 if (!interfaces.iface[i]) {
668 wpa_printf(MSG_ERROR, "Failed to initialize interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700669 goto out;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700670 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700671 }
672
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700673 hostapd_global_ctrl_iface_init(&interfaces);
674
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700675 if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
676 wpa_printf(MSG_ERROR, "Failed to start eloop");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700677 goto out;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700678 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700679
680 ret = 0;
681
682 out:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700683 hostapd_global_ctrl_iface_deinit(&interfaces);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700684 /* Deinitialize all interfaces */
685 for (i = 0; i < interfaces.count; i++)
686 hostapd_interface_deinit_free(interfaces.iface[i]);
687 os_free(interfaces.iface);
688
689 hostapd_global_deinit(pid_file);
690 os_free(pid_file);
691
692 if (log_file)
693 wpa_debug_close_file();
694
695 os_program_deinit();
696
697 return ret;
698}