blob: 5a1b0a9e9295460b75ea54427345974f48d10c20 [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"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028#include "ctrl_iface.h"
29
30
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080031struct hapd_global {
32 void **drv_priv;
33 size_t drv_count;
34};
35
36static struct hapd_global global;
37
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070038
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039#ifndef CONFIG_NO_HOSTAPD_LOGGER
40static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
41 int level, const char *txt, size_t len)
42{
43 struct hostapd_data *hapd = ctx;
44 char *format, *module_str;
45 int maxlen;
46 int conf_syslog_level, conf_stdout_level;
47 unsigned int conf_syslog, conf_stdout;
48
49 maxlen = len + 100;
50 format = os_malloc(maxlen);
51 if (!format)
52 return;
53
54 if (hapd && hapd->conf) {
55 conf_syslog_level = hapd->conf->logger_syslog_level;
56 conf_stdout_level = hapd->conf->logger_stdout_level;
57 conf_syslog = hapd->conf->logger_syslog;
58 conf_stdout = hapd->conf->logger_stdout;
59 } else {
60 conf_syslog_level = conf_stdout_level = 0;
61 conf_syslog = conf_stdout = (unsigned int) -1;
62 }
63
64 switch (module) {
65 case HOSTAPD_MODULE_IEEE80211:
66 module_str = "IEEE 802.11";
67 break;
68 case HOSTAPD_MODULE_IEEE8021X:
69 module_str = "IEEE 802.1X";
70 break;
71 case HOSTAPD_MODULE_RADIUS:
72 module_str = "RADIUS";
73 break;
74 case HOSTAPD_MODULE_WPA:
75 module_str = "WPA";
76 break;
77 case HOSTAPD_MODULE_DRIVER:
78 module_str = "DRIVER";
79 break;
80 case HOSTAPD_MODULE_IAPP:
81 module_str = "IAPP";
82 break;
83 case HOSTAPD_MODULE_MLME:
84 module_str = "MLME";
85 break;
86 default:
87 module_str = NULL;
88 break;
89 }
90
91 if (hapd && hapd->conf && addr)
92 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
93 hapd->conf->iface, MAC2STR(addr),
94 module_str ? " " : "", module_str, txt);
95 else if (hapd && hapd->conf)
96 os_snprintf(format, maxlen, "%s:%s%s %s",
97 hapd->conf->iface, module_str ? " " : "",
98 module_str, txt);
99 else if (addr)
100 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
101 MAC2STR(addr), module_str ? " " : "",
102 module_str, txt);
103 else
104 os_snprintf(format, maxlen, "%s%s%s",
105 module_str, module_str ? ": " : "", txt);
106
107 if ((conf_stdout & module) && level >= conf_stdout_level) {
108 wpa_debug_print_timestamp();
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800109 wpa_printf(MSG_INFO, "%s", format);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700110 }
111
112#ifndef CONFIG_NATIVE_WINDOWS
113 if ((conf_syslog & module) && level >= conf_syslog_level) {
114 int priority;
115 switch (level) {
116 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
117 case HOSTAPD_LEVEL_DEBUG:
118 priority = LOG_DEBUG;
119 break;
120 case HOSTAPD_LEVEL_INFO:
121 priority = LOG_INFO;
122 break;
123 case HOSTAPD_LEVEL_NOTICE:
124 priority = LOG_NOTICE;
125 break;
126 case HOSTAPD_LEVEL_WARNING:
127 priority = LOG_WARNING;
128 break;
129 default:
130 priority = LOG_INFO;
131 break;
132 }
133 syslog(priority, "%s", format);
134 }
135#endif /* CONFIG_NATIVE_WINDOWS */
136
137 os_free(format);
138}
139#endif /* CONFIG_NO_HOSTAPD_LOGGER */
140
141
142/**
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800143 * hostapd_driver_init - Preparate driver interface
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700144 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700145static int hostapd_driver_init(struct hostapd_iface *iface)
146{
147 struct wpa_init_params params;
148 size_t i;
149 struct hostapd_data *hapd = iface->bss[0];
150 struct hostapd_bss_config *conf = hapd->conf;
151 u8 *b = conf->bssid;
152 struct wpa_driver_capa capa;
153
154 if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
155 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
156 return -1;
157 }
158
159 /* Initialize the driver interface */
160 if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
161 b = NULL;
162
163 os_memset(&params, 0, sizeof(params));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800164 for (i = 0; wpa_drivers[i]; i++) {
165 if (wpa_drivers[i] != hapd->driver)
166 continue;
167
168 if (global.drv_priv[i] == NULL &&
169 wpa_drivers[i]->global_init) {
170 global.drv_priv[i] = wpa_drivers[i]->global_init();
171 if (global.drv_priv[i] == NULL) {
172 wpa_printf(MSG_ERROR, "Failed to initialize "
173 "driver '%s'",
174 wpa_drivers[i]->name);
175 return -1;
176 }
177 }
178
179 params.global_priv = global.drv_priv[i];
180 break;
181 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700182 params.bssid = b;
183 params.ifname = hapd->conf->iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700184 params.ssid = hapd->conf->ssid.ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700185 params.ssid_len = hapd->conf->ssid.ssid_len;
186 params.test_socket = hapd->conf->test_socket;
187 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
188
189 params.num_bridge = hapd->iface->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700190 params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700191 if (params.bridge == NULL)
192 return -1;
193 for (i = 0; i < hapd->iface->num_bss; i++) {
194 struct hostapd_data *bss = hapd->iface->bss[i];
195 if (bss->conf->bridge[0])
196 params.bridge[i] = bss->conf->bridge;
197 }
198
199 params.own_addr = hapd->own_addr;
200
201 hapd->drv_priv = hapd->driver->hapd_init(hapd, &params);
202 os_free(params.bridge);
203 if (hapd->drv_priv == NULL) {
204 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
205 hapd->driver->name);
206 hapd->driver = NULL;
207 return -1;
208 }
209
210 if (hapd->driver->get_capa &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800211 hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700212 iface->drv_flags = capa.flags;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800213 iface->probe_resp_offloads = capa.probe_resp_offloads;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700214 iface->extended_capa = capa.extended_capa;
215 iface->extended_capa_mask = capa.extended_capa_mask;
216 iface->extended_capa_len = capa.extended_capa_len;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700217 iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800218 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700219
220 return 0;
221}
222
223
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800224/**
225 * hostapd_interface_init - Read configuration file and init BSS data
226 *
227 * This function is used to parse configuration file for a full interface (one
228 * or more BSSes sharing the same radio) and allocate memory for the BSS
229 * interfaces. No actiual driver operations are started.
230 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700231static struct hostapd_iface *
232hostapd_interface_init(struct hapd_interfaces *interfaces,
233 const char *config_fname, int debug)
234{
235 struct hostapd_iface *iface;
236 int k;
237
238 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800239 iface = hostapd_init(interfaces, config_fname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240 if (!iface)
241 return NULL;
242 iface->interfaces = interfaces;
243
244 for (k = 0; k < debug; k++) {
245 if (iface->bss[0]->conf->logger_stdout_level > 0)
246 iface->bss[0]->conf->logger_stdout_level--;
247 }
248
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800249 if (iface->conf->bss[0]->iface[0] == '\0' &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700250 !hostapd_drv_none(iface->bss[0])) {
251 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
252 config_fname);
253 hostapd_interface_deinit_free(iface);
254 return NULL;
255 }
256
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700257 return iface;
258}
259
260
261/**
262 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
263 */
264static void handle_term(int sig, void *signal_ctx)
265{
266 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
267 eloop_terminate();
268}
269
270
271#ifndef CONFIG_NATIVE_WINDOWS
272
273static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
274{
275 if (hostapd_reload_config(iface) < 0) {
276 wpa_printf(MSG_WARNING, "Failed to read new configuration "
277 "file - continuing with old.");
278 }
279 return 0;
280}
281
282
283/**
284 * handle_reload - SIGHUP handler to reload configuration
285 */
286static void handle_reload(int sig, void *signal_ctx)
287{
288 struct hapd_interfaces *interfaces = signal_ctx;
289 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
290 sig);
291 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
292}
293
294
295static void handle_dump_state(int sig, void *signal_ctx)
296{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800297 /* Not used anymore - ignore signal */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700298}
299#endif /* CONFIG_NATIVE_WINDOWS */
300
301
Jouni Malinen75ecf522011-06-27 15:19:46 -0700302static int hostapd_global_init(struct hapd_interfaces *interfaces,
303 const char *entropy_file)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700304{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800305 int i;
306
307 os_memset(&global, 0, sizeof(global));
308
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700309 hostapd_logger_register_cb(hostapd_logger_cb);
310
311 if (eap_server_register_methods()) {
312 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
313 return -1;
314 }
315
316 if (eloop_init()) {
317 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
318 return -1;
319 }
320
Jouni Malinen75ecf522011-06-27 15:19:46 -0700321 random_init(entropy_file);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700322
323#ifndef CONFIG_NATIVE_WINDOWS
324 eloop_register_signal(SIGHUP, handle_reload, interfaces);
325 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
326#endif /* CONFIG_NATIVE_WINDOWS */
327 eloop_register_signal_terminate(handle_term, interfaces);
328
329#ifndef CONFIG_NATIVE_WINDOWS
330 openlog("hostapd", 0, LOG_DAEMON);
331#endif /* CONFIG_NATIVE_WINDOWS */
332
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800333 for (i = 0; wpa_drivers[i]; i++)
334 global.drv_count++;
335 if (global.drv_count == 0) {
336 wpa_printf(MSG_ERROR, "No drivers enabled");
337 return -1;
338 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700339 global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800340 if (global.drv_priv == NULL)
341 return -1;
342
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700343 return 0;
344}
345
346
347static void hostapd_global_deinit(const char *pid_file)
348{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800349 int i;
350
351 for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
352 if (!global.drv_priv[i])
353 continue;
354 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
355 }
356 os_free(global.drv_priv);
357 global.drv_priv = NULL;
358
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700359#ifdef EAP_SERVER_TNC
360 tncs_global_deinit();
361#endif /* EAP_SERVER_TNC */
362
363 random_deinit();
364
365 eloop_destroy();
366
367#ifndef CONFIG_NATIVE_WINDOWS
368 closelog();
369#endif /* CONFIG_NATIVE_WINDOWS */
370
371 eap_server_unregister_methods();
372
373 os_daemonize_terminate(pid_file);
374}
375
376
377static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
378 const char *pid_file)
379{
380#ifdef EAP_SERVER_TNC
381 int tnc = 0;
382 size_t i, k;
383
384 for (i = 0; !tnc && i < ifaces->count; i++) {
385 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
386 if (ifaces->iface[i]->bss[0]->conf->tnc) {
387 tnc++;
388 break;
389 }
390 }
391 }
392
393 if (tnc && tncs_global_init() < 0) {
394 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
395 return -1;
396 }
397#endif /* EAP_SERVER_TNC */
398
399 if (daemonize && os_daemonize(pid_file)) {
400 perror("daemon");
401 return -1;
402 }
403
404 eloop_run();
405
406 return 0;
407}
408
409
410static void show_version(void)
411{
412 fprintf(stderr,
413 "hostapd v" VERSION_STR "\n"
414 "User space daemon for IEEE 802.11 AP management,\n"
415 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800416 "Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi> "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700417 "and contributors\n");
418}
419
420
421static void usage(void)
422{
423 show_version();
424 fprintf(stderr,
425 "\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -0700426 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700427 "\\\n"
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700428 " [-g <global ctrl_iface>] [-G <group>] \\\n"
429 " <configuration file(s)>\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700430 "\n"
431 "options:\n"
432 " -h show this usage\n"
433 " -d show more debug messages (-dd for even more)\n"
434 " -B run daemon in the background\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -0700435 " -e entropy file\n"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700436 " -g global control interface path\n"
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700437 " -G group for control interfaces\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700438 " -P PID file\n"
439 " -K include key data in debug messages\n"
440#ifdef CONFIG_DEBUG_FILE
441 " -f log output to debug file instead of stdout\n"
442#endif /* CONFIG_DEBUG_FILE */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800443#ifdef CONFIG_DEBUG_LINUX_TRACING
444 " -T = record to Linux tracing in addition to logging\n"
445 " (records all messages regardless of debug verbosity)\n"
446#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700447 " -t include timestamps in some debug messages\n"
448 " -v show hostapd version\n");
449
450 exit(1);
451}
452
453
454static const char * hostapd_msg_ifname_cb(void *ctx)
455{
456 struct hostapd_data *hapd = ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800457 if (hapd && hapd->iconf && hapd->iconf->bss &&
458 hapd->iconf->num_bss > 0 && hapd->iconf->bss[0])
459 return hapd->iconf->bss[0]->iface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700460 return NULL;
461}
462
463
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700464static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
465 const char *path)
466{
467 char *pos;
468 os_free(interfaces->global_iface_path);
469 interfaces->global_iface_path = os_strdup(path);
470 if (interfaces->global_iface_path == NULL)
471 return -1;
472 pos = os_strrchr(interfaces->global_iface_path, '/');
473 if (pos == NULL) {
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700474 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
475 "file");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700476 os_free(interfaces->global_iface_path);
477 interfaces->global_iface_path = NULL;
478 return -1;
479 }
480
481 *pos = '\0';
482 interfaces->global_iface_name = pos + 1;
483
484 return 0;
485}
486
487
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700488static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
489 const char *group)
490{
491#ifndef CONFIG_NATIVE_WINDOWS
492 struct group *grp;
493 grp = getgrnam(group);
494 if (grp == NULL) {
495 wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
496 return -1;
497 }
498 interfaces->ctrl_iface_group = grp->gr_gid;
499#endif /* CONFIG_NATIVE_WINDOWS */
500 return 0;
501}
502
503
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700504int main(int argc, char *argv[])
505{
506 struct hapd_interfaces interfaces;
507 int ret = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800508 size_t i, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700509 int c, debug = 0, daemonize = 0;
510 char *pid_file = NULL;
511 const char *log_file = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700512 const char *entropy_file = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800513 char **bss_config = NULL, **tmp_bss;
514 size_t num_bss_configs = 0;
515#ifdef CONFIG_DEBUG_LINUX_TRACING
516 int enable_trace_dbg = 0;
517#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700518
519 if (os_program_init())
520 return -1;
521
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700522 os_memset(&interfaces, 0, sizeof(interfaces));
523 interfaces.reload_config = hostapd_reload_config;
524 interfaces.config_read_cb = hostapd_config_read;
525 interfaces.for_each_interface = hostapd_for_each_interface;
526 interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
527 interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
528 interfaces.driver_init = hostapd_driver_init;
529 interfaces.global_iface_path = NULL;
530 interfaces.global_iface_name = NULL;
531 interfaces.global_ctrl_sock = -1;
532
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700533 for (;;) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800534 c = getopt(argc, argv, "b:Bde:f:hKP:Ttvg:G:");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700535 if (c < 0)
536 break;
537 switch (c) {
538 case 'h':
539 usage();
540 break;
541 case 'd':
542 debug++;
543 if (wpa_debug_level > 0)
544 wpa_debug_level--;
545 break;
546 case 'B':
547 daemonize++;
548 break;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700549 case 'e':
550 entropy_file = optarg;
551 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700552 case 'f':
553 log_file = optarg;
554 break;
555 case 'K':
556 wpa_debug_show_keys++;
557 break;
558 case 'P':
559 os_free(pid_file);
560 pid_file = os_rel2abs_path(optarg);
561 break;
562 case 't':
563 wpa_debug_timestamp++;
564 break;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800565#ifdef CONFIG_DEBUG_LINUX_TRACING
566 case 'T':
567 enable_trace_dbg = 1;
568 break;
569#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700570 case 'v':
571 show_version();
572 exit(1);
573 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700574 case 'g':
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700575 if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
576 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700577 break;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700578 case 'G':
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700579 if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
580 return -1;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700581 break;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800582 case 'b':
583 tmp_bss = os_realloc_array(bss_config,
584 num_bss_configs + 1,
585 sizeof(char *));
586 if (tmp_bss == NULL)
587 goto out;
588 bss_config = tmp_bss;
589 bss_config[num_bss_configs++] = optarg;
590 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700591 default:
592 usage();
593 break;
594 }
595 }
596
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800597 if (optind == argc && interfaces.global_iface_path == NULL &&
598 num_bss_configs == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700599 usage();
600
601 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
602
603 if (log_file)
604 wpa_debug_open_file(log_file);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800605#ifdef CONFIG_DEBUG_LINUX_TRACING
606 if (enable_trace_dbg) {
607 int tret = wpa_debug_open_linux_tracing();
608 if (tret) {
609 wpa_printf(MSG_ERROR, "Failed to enable trace logging");
610 return -1;
611 }
612 }
613#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700614
615 interfaces.count = argc - optind;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800616 if (interfaces.count || num_bss_configs) {
617 interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700618 sizeof(struct hostapd_iface *));
619 if (interfaces.iface == NULL) {
620 wpa_printf(MSG_ERROR, "malloc failed");
621 return -1;
622 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700623 }
624
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700625 if (hostapd_global_init(&interfaces, entropy_file)) {
626 wpa_printf(MSG_ERROR, "Failed to initilize global context");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700627 return -1;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700628 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700629
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800630 /* Allocate and parse configuration for full interface files */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700631 for (i = 0; i < interfaces.count; i++) {
632 interfaces.iface[i] = hostapd_interface_init(&interfaces,
633 argv[optind + i],
634 debug);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700635 if (!interfaces.iface[i]) {
636 wpa_printf(MSG_ERROR, "Failed to initialize interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700637 goto out;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700638 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700639 }
640
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800641 /* Allocate and parse configuration for per-BSS files */
642 for (i = 0; i < num_bss_configs; i++) {
643 struct hostapd_iface *iface;
644 char *fname;
645
646 wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
647 fname = os_strchr(bss_config[i], ':');
648 if (fname == NULL) {
649 wpa_printf(MSG_ERROR,
650 "Invalid BSS config identifier '%s'",
651 bss_config[i]);
652 goto out;
653 }
654 *fname++ = '\0';
655 iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
656 fname, debug);
657 if (iface == NULL)
658 goto out;
659 for (j = 0; j < interfaces.count; j++) {
660 if (interfaces.iface[j] == iface)
661 break;
662 }
663 if (j == interfaces.count) {
664 struct hostapd_iface **tmp;
665 tmp = os_realloc_array(interfaces.iface,
666 interfaces.count + 1,
667 sizeof(struct hostapd_iface *));
668 if (tmp == NULL) {
669 hostapd_interface_deinit_free(iface);
670 goto out;
671 }
672 interfaces.iface = tmp;
673 interfaces.iface[interfaces.count++] = iface;
674 }
675 }
676
677 /*
678 * Enable configured interfaces. Depending on channel configuration,
679 * this may complete full initialization before returning or use a
680 * callback mechanism to complete setup in case of operations like HT
681 * co-ex scans, ACS, or DFS are needed to determine channel parameters.
682 * In such case, the interface will be enabled from eloop context within
683 * hostapd_global_run().
684 */
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800685 interfaces.terminate_on_error = interfaces.count;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800686 for (i = 0; i < interfaces.count; i++) {
687 if (hostapd_driver_init(interfaces.iface[i]) ||
688 hostapd_setup_interface(interfaces.iface[i]))
689 goto out;
690 }
691
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700692 hostapd_global_ctrl_iface_init(&interfaces);
693
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700694 if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
695 wpa_printf(MSG_ERROR, "Failed to start eloop");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700696 goto out;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700697 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700698
699 ret = 0;
700
701 out:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700702 hostapd_global_ctrl_iface_deinit(&interfaces);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700703 /* Deinitialize all interfaces */
704 for (i = 0; i < interfaces.count; i++)
705 hostapd_interface_deinit_free(interfaces.iface[i]);
706 os_free(interfaces.iface);
707
708 hostapd_global_deinit(pid_file);
709 os_free(pid_file);
710
711 if (log_file)
712 wpa_debug_close_file();
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800713 wpa_debug_close_linux_tracing();
714
715 os_free(bss_config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700716
717 os_program_deinit();
718
719 return ret;
720}