blob: 4e9fe404d5a9086b1ec09c2653e07d654d6a97a4 [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();
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800117 wpa_printf(MSG_INFO, "%s", format);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700118 }
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/**
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800151 * hostapd_driver_init - Preparate driver interface
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700152 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153static int hostapd_driver_init(struct hostapd_iface *iface)
154{
155 struct wpa_init_params params;
156 size_t i;
157 struct hostapd_data *hapd = iface->bss[0];
158 struct hostapd_bss_config *conf = hapd->conf;
159 u8 *b = conf->bssid;
160 struct wpa_driver_capa capa;
161
162 if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
163 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
164 return -1;
165 }
166
167 /* Initialize the driver interface */
168 if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
169 b = NULL;
170
171 os_memset(&params, 0, sizeof(params));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800172 for (i = 0; wpa_drivers[i]; i++) {
173 if (wpa_drivers[i] != hapd->driver)
174 continue;
175
176 if (global.drv_priv[i] == NULL &&
177 wpa_drivers[i]->global_init) {
178 global.drv_priv[i] = wpa_drivers[i]->global_init();
179 if (global.drv_priv[i] == NULL) {
180 wpa_printf(MSG_ERROR, "Failed to initialize "
181 "driver '%s'",
182 wpa_drivers[i]->name);
183 return -1;
184 }
185 }
186
187 params.global_priv = global.drv_priv[i];
188 break;
189 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700190 params.bssid = b;
191 params.ifname = hapd->conf->iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700192 params.ssid = hapd->conf->ssid.ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700193 params.ssid_len = hapd->conf->ssid.ssid_len;
194 params.test_socket = hapd->conf->test_socket;
195 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
196
197 params.num_bridge = hapd->iface->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700198 params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700199 if (params.bridge == NULL)
200 return -1;
201 for (i = 0; i < hapd->iface->num_bss; i++) {
202 struct hostapd_data *bss = hapd->iface->bss[i];
203 if (bss->conf->bridge[0])
204 params.bridge[i] = bss->conf->bridge;
205 }
206
207 params.own_addr = hapd->own_addr;
208
209 hapd->drv_priv = hapd->driver->hapd_init(hapd, &params);
210 os_free(params.bridge);
211 if (hapd->drv_priv == NULL) {
212 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
213 hapd->driver->name);
214 hapd->driver = NULL;
215 return -1;
216 }
217
218 if (hapd->driver->get_capa &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800219 hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700220 iface->drv_flags = capa.flags;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800221 iface->probe_resp_offloads = capa.probe_resp_offloads;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700222 iface->extended_capa = capa.extended_capa;
223 iface->extended_capa_mask = capa.extended_capa_mask;
224 iface->extended_capa_len = capa.extended_capa_len;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700225 iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800226 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700227
228 return 0;
229}
230
231
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800232/**
233 * hostapd_interface_init - Read configuration file and init BSS data
234 *
235 * This function is used to parse configuration file for a full interface (one
236 * or more BSSes sharing the same radio) and allocate memory for the BSS
237 * interfaces. No actiual driver operations are started.
238 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700239static struct hostapd_iface *
240hostapd_interface_init(struct hapd_interfaces *interfaces,
241 const char *config_fname, int debug)
242{
243 struct hostapd_iface *iface;
244 int k;
245
246 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800247 iface = hostapd_init(interfaces, config_fname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248 if (!iface)
249 return NULL;
250 iface->interfaces = interfaces;
251
252 for (k = 0; k < debug; k++) {
253 if (iface->bss[0]->conf->logger_stdout_level > 0)
254 iface->bss[0]->conf->logger_stdout_level--;
255 }
256
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800257 if (iface->conf->bss[0]->iface[0] == '\0' &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700258 !hostapd_drv_none(iface->bss[0])) {
259 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
260 config_fname);
261 hostapd_interface_deinit_free(iface);
262 return NULL;
263 }
264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265 return iface;
266}
267
268
269/**
270 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
271 */
272static void handle_term(int sig, void *signal_ctx)
273{
274 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
275 eloop_terminate();
276}
277
278
279#ifndef CONFIG_NATIVE_WINDOWS
280
281static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
282{
283 if (hostapd_reload_config(iface) < 0) {
284 wpa_printf(MSG_WARNING, "Failed to read new configuration "
285 "file - continuing with old.");
286 }
287 return 0;
288}
289
290
291/**
292 * handle_reload - SIGHUP handler to reload configuration
293 */
294static void handle_reload(int sig, void *signal_ctx)
295{
296 struct hapd_interfaces *interfaces = signal_ctx;
297 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
298 sig);
299 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
300}
301
302
303static void handle_dump_state(int sig, void *signal_ctx)
304{
305#ifdef HOSTAPD_DUMP_STATE
306 struct hapd_interfaces *interfaces = signal_ctx;
307 hostapd_for_each_interface(interfaces, handle_dump_state_iface, NULL);
308#endif /* HOSTAPD_DUMP_STATE */
309}
310#endif /* CONFIG_NATIVE_WINDOWS */
311
312
Jouni Malinen75ecf522011-06-27 15:19:46 -0700313static int hostapd_global_init(struct hapd_interfaces *interfaces,
314 const char *entropy_file)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700315{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800316 int i;
317
318 os_memset(&global, 0, sizeof(global));
319
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700320 hostapd_logger_register_cb(hostapd_logger_cb);
321
322 if (eap_server_register_methods()) {
323 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
324 return -1;
325 }
326
327 if (eloop_init()) {
328 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
329 return -1;
330 }
331
Jouni Malinen75ecf522011-06-27 15:19:46 -0700332 random_init(entropy_file);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700333
334#ifndef CONFIG_NATIVE_WINDOWS
335 eloop_register_signal(SIGHUP, handle_reload, interfaces);
336 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
337#endif /* CONFIG_NATIVE_WINDOWS */
338 eloop_register_signal_terminate(handle_term, interfaces);
339
340#ifndef CONFIG_NATIVE_WINDOWS
341 openlog("hostapd", 0, LOG_DAEMON);
342#endif /* CONFIG_NATIVE_WINDOWS */
343
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800344 for (i = 0; wpa_drivers[i]; i++)
345 global.drv_count++;
346 if (global.drv_count == 0) {
347 wpa_printf(MSG_ERROR, "No drivers enabled");
348 return -1;
349 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700350 global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800351 if (global.drv_priv == NULL)
352 return -1;
353
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354 return 0;
355}
356
357
358static void hostapd_global_deinit(const char *pid_file)
359{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800360 int i;
361
362 for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
363 if (!global.drv_priv[i])
364 continue;
365 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
366 }
367 os_free(global.drv_priv);
368 global.drv_priv = NULL;
369
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700370#ifdef EAP_SERVER_TNC
371 tncs_global_deinit();
372#endif /* EAP_SERVER_TNC */
373
374 random_deinit();
375
376 eloop_destroy();
377
378#ifndef CONFIG_NATIVE_WINDOWS
379 closelog();
380#endif /* CONFIG_NATIVE_WINDOWS */
381
382 eap_server_unregister_methods();
383
384 os_daemonize_terminate(pid_file);
385}
386
387
388static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
389 const char *pid_file)
390{
391#ifdef EAP_SERVER_TNC
392 int tnc = 0;
393 size_t i, k;
394
395 for (i = 0; !tnc && i < ifaces->count; i++) {
396 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
397 if (ifaces->iface[i]->bss[0]->conf->tnc) {
398 tnc++;
399 break;
400 }
401 }
402 }
403
404 if (tnc && tncs_global_init() < 0) {
405 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
406 return -1;
407 }
408#endif /* EAP_SERVER_TNC */
409
410 if (daemonize && os_daemonize(pid_file)) {
411 perror("daemon");
412 return -1;
413 }
414
415 eloop_run();
416
417 return 0;
418}
419
420
421static void show_version(void)
422{
423 fprintf(stderr,
424 "hostapd v" VERSION_STR "\n"
425 "User space daemon for IEEE 802.11 AP management,\n"
426 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800427 "Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi> "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700428 "and contributors\n");
429}
430
431
432static void usage(void)
433{
434 show_version();
435 fprintf(stderr,
436 "\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -0700437 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700438 "\\\n"
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700439 " [-g <global ctrl_iface>] [-G <group>] \\\n"
440 " <configuration file(s)>\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700441 "\n"
442 "options:\n"
443 " -h show this usage\n"
444 " -d show more debug messages (-dd for even more)\n"
445 " -B run daemon in the background\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -0700446 " -e entropy file\n"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700447 " -g global control interface path\n"
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700448 " -G group for control interfaces\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700449 " -P PID file\n"
450 " -K include key data in debug messages\n"
451#ifdef CONFIG_DEBUG_FILE
452 " -f log output to debug file instead of stdout\n"
453#endif /* CONFIG_DEBUG_FILE */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800454#ifdef CONFIG_DEBUG_LINUX_TRACING
455 " -T = record to Linux tracing in addition to logging\n"
456 " (records all messages regardless of debug verbosity)\n"
457#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458 " -t include timestamps in some debug messages\n"
459 " -v show hostapd version\n");
460
461 exit(1);
462}
463
464
465static const char * hostapd_msg_ifname_cb(void *ctx)
466{
467 struct hostapd_data *hapd = ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800468 if (hapd && hapd->iconf && hapd->iconf->bss &&
469 hapd->iconf->num_bss > 0 && hapd->iconf->bss[0])
470 return hapd->iconf->bss[0]->iface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700471 return NULL;
472}
473
474
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700475static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
476 const char *path)
477{
478 char *pos;
479 os_free(interfaces->global_iface_path);
480 interfaces->global_iface_path = os_strdup(path);
481 if (interfaces->global_iface_path == NULL)
482 return -1;
483 pos = os_strrchr(interfaces->global_iface_path, '/');
484 if (pos == NULL) {
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700485 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
486 "file");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700487 os_free(interfaces->global_iface_path);
488 interfaces->global_iface_path = NULL;
489 return -1;
490 }
491
492 *pos = '\0';
493 interfaces->global_iface_name = pos + 1;
494
495 return 0;
496}
497
498
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700499static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
500 const char *group)
501{
502#ifndef CONFIG_NATIVE_WINDOWS
503 struct group *grp;
504 grp = getgrnam(group);
505 if (grp == NULL) {
506 wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
507 return -1;
508 }
509 interfaces->ctrl_iface_group = grp->gr_gid;
510#endif /* CONFIG_NATIVE_WINDOWS */
511 return 0;
512}
513
514
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700515int main(int argc, char *argv[])
516{
517 struct hapd_interfaces interfaces;
518 int ret = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800519 size_t i, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700520 int c, debug = 0, daemonize = 0;
521 char *pid_file = NULL;
522 const char *log_file = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700523 const char *entropy_file = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800524 char **bss_config = NULL, **tmp_bss;
525 size_t num_bss_configs = 0;
526#ifdef CONFIG_DEBUG_LINUX_TRACING
527 int enable_trace_dbg = 0;
528#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700529
530 if (os_program_init())
531 return -1;
532
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700533 os_memset(&interfaces, 0, sizeof(interfaces));
534 interfaces.reload_config = hostapd_reload_config;
535 interfaces.config_read_cb = hostapd_config_read;
536 interfaces.for_each_interface = hostapd_for_each_interface;
537 interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
538 interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
539 interfaces.driver_init = hostapd_driver_init;
540 interfaces.global_iface_path = NULL;
541 interfaces.global_iface_name = NULL;
542 interfaces.global_ctrl_sock = -1;
543
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700544 for (;;) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800545 c = getopt(argc, argv, "b:Bde:f:hKP:Ttvg:G:");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700546 if (c < 0)
547 break;
548 switch (c) {
549 case 'h':
550 usage();
551 break;
552 case 'd':
553 debug++;
554 if (wpa_debug_level > 0)
555 wpa_debug_level--;
556 break;
557 case 'B':
558 daemonize++;
559 break;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700560 case 'e':
561 entropy_file = optarg;
562 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700563 case 'f':
564 log_file = optarg;
565 break;
566 case 'K':
567 wpa_debug_show_keys++;
568 break;
569 case 'P':
570 os_free(pid_file);
571 pid_file = os_rel2abs_path(optarg);
572 break;
573 case 't':
574 wpa_debug_timestamp++;
575 break;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800576#ifdef CONFIG_DEBUG_LINUX_TRACING
577 case 'T':
578 enable_trace_dbg = 1;
579 break;
580#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700581 case 'v':
582 show_version();
583 exit(1);
584 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700585 case 'g':
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700586 if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
587 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700588 break;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700589 case 'G':
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700590 if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
591 return -1;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700592 break;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800593 case 'b':
594 tmp_bss = os_realloc_array(bss_config,
595 num_bss_configs + 1,
596 sizeof(char *));
597 if (tmp_bss == NULL)
598 goto out;
599 bss_config = tmp_bss;
600 bss_config[num_bss_configs++] = optarg;
601 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700602 default:
603 usage();
604 break;
605 }
606 }
607
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800608 if (optind == argc && interfaces.global_iface_path == NULL &&
609 num_bss_configs == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700610 usage();
611
612 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
613
614 if (log_file)
615 wpa_debug_open_file(log_file);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800616#ifdef CONFIG_DEBUG_LINUX_TRACING
617 if (enable_trace_dbg) {
618 int tret = wpa_debug_open_linux_tracing();
619 if (tret) {
620 wpa_printf(MSG_ERROR, "Failed to enable trace logging");
621 return -1;
622 }
623 }
624#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700625
626 interfaces.count = argc - optind;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800627 if (interfaces.count || num_bss_configs) {
628 interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700629 sizeof(struct hostapd_iface *));
630 if (interfaces.iface == NULL) {
631 wpa_printf(MSG_ERROR, "malloc failed");
632 return -1;
633 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700634 }
635
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700636 if (hostapd_global_init(&interfaces, entropy_file)) {
637 wpa_printf(MSG_ERROR, "Failed to initilize global context");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700638 return -1;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700639 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700640
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800641 /* Allocate and parse configuration for full interface files */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700642 for (i = 0; i < interfaces.count; i++) {
643 interfaces.iface[i] = hostapd_interface_init(&interfaces,
644 argv[optind + i],
645 debug);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700646 if (!interfaces.iface[i]) {
647 wpa_printf(MSG_ERROR, "Failed to initialize interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700648 goto out;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700649 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700650 }
651
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800652 /* Allocate and parse configuration for per-BSS files */
653 for (i = 0; i < num_bss_configs; i++) {
654 struct hostapd_iface *iface;
655 char *fname;
656
657 wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
658 fname = os_strchr(bss_config[i], ':');
659 if (fname == NULL) {
660 wpa_printf(MSG_ERROR,
661 "Invalid BSS config identifier '%s'",
662 bss_config[i]);
663 goto out;
664 }
665 *fname++ = '\0';
666 iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
667 fname, debug);
668 if (iface == NULL)
669 goto out;
670 for (j = 0; j < interfaces.count; j++) {
671 if (interfaces.iface[j] == iface)
672 break;
673 }
674 if (j == interfaces.count) {
675 struct hostapd_iface **tmp;
676 tmp = os_realloc_array(interfaces.iface,
677 interfaces.count + 1,
678 sizeof(struct hostapd_iface *));
679 if (tmp == NULL) {
680 hostapd_interface_deinit_free(iface);
681 goto out;
682 }
683 interfaces.iface = tmp;
684 interfaces.iface[interfaces.count++] = iface;
685 }
686 }
687
688 /*
689 * Enable configured interfaces. Depending on channel configuration,
690 * this may complete full initialization before returning or use a
691 * callback mechanism to complete setup in case of operations like HT
692 * co-ex scans, ACS, or DFS are needed to determine channel parameters.
693 * In such case, the interface will be enabled from eloop context within
694 * hostapd_global_run().
695 */
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800696 interfaces.terminate_on_error = interfaces.count;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800697 for (i = 0; i < interfaces.count; i++) {
698 if (hostapd_driver_init(interfaces.iface[i]) ||
699 hostapd_setup_interface(interfaces.iface[i]))
700 goto out;
701 }
702
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700703 hostapd_global_ctrl_iface_init(&interfaces);
704
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700705 if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
706 wpa_printf(MSG_ERROR, "Failed to start eloop");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700707 goto out;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700708 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700709
710 ret = 0;
711
712 out:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700713 hostapd_global_ctrl_iface_deinit(&interfaces);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700714 /* Deinitialize all interfaces */
715 for (i = 0; i < interfaces.count; i++)
716 hostapd_interface_deinit_free(interfaces.iface[i]);
717 os_free(interfaces.iface);
718
719 hostapd_global_deinit(pid_file);
720 os_free(pid_file);
721
722 if (log_file)
723 wpa_debug_close_file();
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800724 wpa_debug_close_linux_tracing();
725
726 os_free(bss_config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700727
728 os_program_deinit();
729
730 return ret;
731}