blob: a9d7da5115d7c49444cd2b9277e544955174a8f4 [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"
Dmitry Shmidt96be6222014-02-13 10:16:51 -080017#include "utils/uuid.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include "crypto/random.h"
19#include "crypto/tls.h"
20#include "common/version.h"
21#include "drivers/driver.h"
22#include "eap_server/eap.h"
23#include "eap_server/tncs.h"
24#include "ap/hostapd.h"
25#include "ap/ap_config.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070026#include "ap/ap_drv_ops.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027#include "config_file.h"
28#include "eap_register.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070029#include "ctrl_iface.h"
30
31
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080032struct hapd_global {
33 void **drv_priv;
34 size_t drv_count;
35};
36
37static struct hapd_global global;
38
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040#ifndef CONFIG_NO_HOSTAPD_LOGGER
41static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
42 int level, const char *txt, size_t len)
43{
44 struct hostapd_data *hapd = ctx;
45 char *format, *module_str;
46 int maxlen;
47 int conf_syslog_level, conf_stdout_level;
48 unsigned int conf_syslog, conf_stdout;
49
50 maxlen = len + 100;
51 format = os_malloc(maxlen);
52 if (!format)
53 return;
54
55 if (hapd && hapd->conf) {
56 conf_syslog_level = hapd->conf->logger_syslog_level;
57 conf_stdout_level = hapd->conf->logger_stdout_level;
58 conf_syslog = hapd->conf->logger_syslog;
59 conf_stdout = hapd->conf->logger_stdout;
60 } else {
61 conf_syslog_level = conf_stdout_level = 0;
62 conf_syslog = conf_stdout = (unsigned int) -1;
63 }
64
65 switch (module) {
66 case HOSTAPD_MODULE_IEEE80211:
67 module_str = "IEEE 802.11";
68 break;
69 case HOSTAPD_MODULE_IEEE8021X:
70 module_str = "IEEE 802.1X";
71 break;
72 case HOSTAPD_MODULE_RADIUS:
73 module_str = "RADIUS";
74 break;
75 case HOSTAPD_MODULE_WPA:
76 module_str = "WPA";
77 break;
78 case HOSTAPD_MODULE_DRIVER:
79 module_str = "DRIVER";
80 break;
81 case HOSTAPD_MODULE_IAPP:
82 module_str = "IAPP";
83 break;
84 case HOSTAPD_MODULE_MLME:
85 module_str = "MLME";
86 break;
87 default:
88 module_str = NULL;
89 break;
90 }
91
92 if (hapd && hapd->conf && addr)
93 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
94 hapd->conf->iface, MAC2STR(addr),
Dmitry Shmidt96be6222014-02-13 10:16:51 -080095 module_str ? " " : "", module_str ? module_str : "",
96 txt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070097 else if (hapd && hapd->conf)
98 os_snprintf(format, maxlen, "%s:%s%s %s",
99 hapd->conf->iface, module_str ? " " : "",
100 module_str, txt);
101 else if (addr)
102 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
103 MAC2STR(addr), module_str ? " " : "",
104 module_str, txt);
105 else
106 os_snprintf(format, maxlen, "%s%s%s",
107 module_str, module_str ? ": " : "", txt);
108
109 if ((conf_stdout & module) && level >= conf_stdout_level) {
110 wpa_debug_print_timestamp();
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800111 wpa_printf(MSG_INFO, "%s", format);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700112 }
113
114#ifndef CONFIG_NATIVE_WINDOWS
115 if ((conf_syslog & module) && level >= conf_syslog_level) {
116 int priority;
117 switch (level) {
118 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
119 case HOSTAPD_LEVEL_DEBUG:
120 priority = LOG_DEBUG;
121 break;
122 case HOSTAPD_LEVEL_INFO:
123 priority = LOG_INFO;
124 break;
125 case HOSTAPD_LEVEL_NOTICE:
126 priority = LOG_NOTICE;
127 break;
128 case HOSTAPD_LEVEL_WARNING:
129 priority = LOG_WARNING;
130 break;
131 default:
132 priority = LOG_INFO;
133 break;
134 }
135 syslog(priority, "%s", format);
136 }
137#endif /* CONFIG_NATIVE_WINDOWS */
138
139 os_free(format);
140}
141#endif /* CONFIG_NO_HOSTAPD_LOGGER */
142
143
144/**
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800145 * hostapd_driver_init - Preparate driver interface
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700146 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700147static int hostapd_driver_init(struct hostapd_iface *iface)
148{
149 struct wpa_init_params params;
150 size_t i;
151 struct hostapd_data *hapd = iface->bss[0];
152 struct hostapd_bss_config *conf = hapd->conf;
153 u8 *b = conf->bssid;
154 struct wpa_driver_capa capa;
155
156 if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
157 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
158 return -1;
159 }
160
161 /* Initialize the driver interface */
162 if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
163 b = NULL;
164
165 os_memset(&params, 0, sizeof(params));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800166 for (i = 0; wpa_drivers[i]; i++) {
167 if (wpa_drivers[i] != hapd->driver)
168 continue;
169
170 if (global.drv_priv[i] == NULL &&
171 wpa_drivers[i]->global_init) {
172 global.drv_priv[i] = wpa_drivers[i]->global_init();
173 if (global.drv_priv[i] == NULL) {
174 wpa_printf(MSG_ERROR, "Failed to initialize "
175 "driver '%s'",
176 wpa_drivers[i]->name);
177 return -1;
178 }
179 }
180
181 params.global_priv = global.drv_priv[i];
182 break;
183 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184 params.bssid = b;
185 params.ifname = hapd->conf->iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700186 params.ssid = hapd->conf->ssid.ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700187 params.ssid_len = hapd->conf->ssid.ssid_len;
188 params.test_socket = hapd->conf->test_socket;
189 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
190
191 params.num_bridge = hapd->iface->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700192 params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700193 if (params.bridge == NULL)
194 return -1;
195 for (i = 0; i < hapd->iface->num_bss; i++) {
196 struct hostapd_data *bss = hapd->iface->bss[i];
197 if (bss->conf->bridge[0])
198 params.bridge[i] = bss->conf->bridge;
199 }
200
201 params.own_addr = hapd->own_addr;
202
203 hapd->drv_priv = hapd->driver->hapd_init(hapd, &params);
204 os_free(params.bridge);
205 if (hapd->drv_priv == NULL) {
206 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
207 hapd->driver->name);
208 hapd->driver = NULL;
209 return -1;
210 }
211
212 if (hapd->driver->get_capa &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800213 hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700214 iface->drv_flags = capa.flags;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800215 iface->probe_resp_offloads = capa.probe_resp_offloads;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700216 iface->extended_capa = capa.extended_capa;
217 iface->extended_capa_mask = capa.extended_capa_mask;
218 iface->extended_capa_len = capa.extended_capa_len;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700219 iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800220 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700221
222 return 0;
223}
224
225
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800226/**
227 * hostapd_interface_init - Read configuration file and init BSS data
228 *
229 * This function is used to parse configuration file for a full interface (one
230 * or more BSSes sharing the same radio) and allocate memory for the BSS
231 * interfaces. No actiual driver operations are started.
232 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700233static struct hostapd_iface *
234hostapd_interface_init(struct hapd_interfaces *interfaces,
235 const char *config_fname, int debug)
236{
237 struct hostapd_iface *iface;
238 int k;
239
240 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800241 iface = hostapd_init(interfaces, config_fname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700242 if (!iface)
243 return NULL;
244 iface->interfaces = interfaces;
245
246 for (k = 0; k < debug; k++) {
247 if (iface->bss[0]->conf->logger_stdout_level > 0)
248 iface->bss[0]->conf->logger_stdout_level--;
249 }
250
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800251 if (iface->conf->bss[0]->iface[0] == '\0' &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700252 !hostapd_drv_none(iface->bss[0])) {
253 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
254 config_fname);
255 hostapd_interface_deinit_free(iface);
256 return NULL;
257 }
258
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700259 return iface;
260}
261
262
263/**
264 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
265 */
266static void handle_term(int sig, void *signal_ctx)
267{
268 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
269 eloop_terminate();
270}
271
272
273#ifndef CONFIG_NATIVE_WINDOWS
274
275static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
276{
277 if (hostapd_reload_config(iface) < 0) {
278 wpa_printf(MSG_WARNING, "Failed to read new configuration "
279 "file - continuing with old.");
280 }
281 return 0;
282}
283
284
285/**
286 * handle_reload - SIGHUP handler to reload configuration
287 */
288static void handle_reload(int sig, void *signal_ctx)
289{
290 struct hapd_interfaces *interfaces = signal_ctx;
291 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
292 sig);
293 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
294}
295
296
297static void handle_dump_state(int sig, void *signal_ctx)
298{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800299 /* Not used anymore - ignore signal */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700300}
301#endif /* CONFIG_NATIVE_WINDOWS */
302
303
Jouni Malinen75ecf522011-06-27 15:19:46 -0700304static int hostapd_global_init(struct hapd_interfaces *interfaces,
305 const char *entropy_file)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700306{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800307 int i;
308
309 os_memset(&global, 0, sizeof(global));
310
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700311 hostapd_logger_register_cb(hostapd_logger_cb);
312
313 if (eap_server_register_methods()) {
314 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
315 return -1;
316 }
317
318 if (eloop_init()) {
319 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
320 return -1;
321 }
322
Jouni Malinen75ecf522011-06-27 15:19:46 -0700323 random_init(entropy_file);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324
325#ifndef CONFIG_NATIVE_WINDOWS
326 eloop_register_signal(SIGHUP, handle_reload, interfaces);
327 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
328#endif /* CONFIG_NATIVE_WINDOWS */
329 eloop_register_signal_terminate(handle_term, interfaces);
330
331#ifndef CONFIG_NATIVE_WINDOWS
332 openlog("hostapd", 0, LOG_DAEMON);
333#endif /* CONFIG_NATIVE_WINDOWS */
334
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800335 for (i = 0; wpa_drivers[i]; i++)
336 global.drv_count++;
337 if (global.drv_count == 0) {
338 wpa_printf(MSG_ERROR, "No drivers enabled");
339 return -1;
340 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700341 global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800342 if (global.drv_priv == NULL)
343 return -1;
344
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700345 return 0;
346}
347
348
349static void hostapd_global_deinit(const char *pid_file)
350{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800351 int i;
352
353 for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
354 if (!global.drv_priv[i])
355 continue;
356 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
357 }
358 os_free(global.drv_priv);
359 global.drv_priv = NULL;
360
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700361#ifdef EAP_SERVER_TNC
362 tncs_global_deinit();
363#endif /* EAP_SERVER_TNC */
364
365 random_deinit();
366
367 eloop_destroy();
368
369#ifndef CONFIG_NATIVE_WINDOWS
370 closelog();
371#endif /* CONFIG_NATIVE_WINDOWS */
372
373 eap_server_unregister_methods();
374
375 os_daemonize_terminate(pid_file);
376}
377
378
379static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
380 const char *pid_file)
381{
382#ifdef EAP_SERVER_TNC
383 int tnc = 0;
384 size_t i, k;
385
386 for (i = 0; !tnc && i < ifaces->count; i++) {
387 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
388 if (ifaces->iface[i]->bss[0]->conf->tnc) {
389 tnc++;
390 break;
391 }
392 }
393 }
394
395 if (tnc && tncs_global_init() < 0) {
396 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
397 return -1;
398 }
399#endif /* EAP_SERVER_TNC */
400
401 if (daemonize && os_daemonize(pid_file)) {
402 perror("daemon");
403 return -1;
404 }
405
406 eloop_run();
407
408 return 0;
409}
410
411
412static void show_version(void)
413{
414 fprintf(stderr,
415 "hostapd v" VERSION_STR "\n"
416 "User space daemon for IEEE 802.11 AP management,\n"
417 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800418 "Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi> "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700419 "and contributors\n");
420}
421
422
423static void usage(void)
424{
425 show_version();
426 fprintf(stderr,
427 "\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -0700428 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700429 "\\\n"
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700430 " [-g <global ctrl_iface>] [-G <group>] \\\n"
431 " <configuration file(s)>\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700432 "\n"
433 "options:\n"
434 " -h show this usage\n"
435 " -d show more debug messages (-dd for even more)\n"
436 " -B run daemon in the background\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -0700437 " -e entropy file\n"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700438 " -g global control interface path\n"
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700439 " -G group for control interfaces\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700440 " -P PID file\n"
441 " -K include key data in debug messages\n"
442#ifdef CONFIG_DEBUG_FILE
443 " -f log output to debug file instead of stdout\n"
444#endif /* CONFIG_DEBUG_FILE */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800445#ifdef CONFIG_DEBUG_LINUX_TRACING
446 " -T = record to Linux tracing in addition to logging\n"
447 " (records all messages regardless of debug verbosity)\n"
448#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700449 " -t include timestamps in some debug messages\n"
450 " -v show hostapd version\n");
451
452 exit(1);
453}
454
455
456static const char * hostapd_msg_ifname_cb(void *ctx)
457{
458 struct hostapd_data *hapd = ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800459 if (hapd && hapd->iconf && hapd->iconf->bss &&
460 hapd->iconf->num_bss > 0 && hapd->iconf->bss[0])
461 return hapd->iconf->bss[0]->iface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700462 return NULL;
463}
464
465
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700466static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
467 const char *path)
468{
469 char *pos;
470 os_free(interfaces->global_iface_path);
471 interfaces->global_iface_path = os_strdup(path);
472 if (interfaces->global_iface_path == NULL)
473 return -1;
474 pos = os_strrchr(interfaces->global_iface_path, '/');
475 if (pos == NULL) {
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700476 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
477 "file");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700478 os_free(interfaces->global_iface_path);
479 interfaces->global_iface_path = NULL;
480 return -1;
481 }
482
483 *pos = '\0';
484 interfaces->global_iface_name = pos + 1;
485
486 return 0;
487}
488
489
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700490static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
491 const char *group)
492{
493#ifndef CONFIG_NATIVE_WINDOWS
494 struct group *grp;
495 grp = getgrnam(group);
496 if (grp == NULL) {
497 wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
498 return -1;
499 }
500 interfaces->ctrl_iface_group = grp->gr_gid;
501#endif /* CONFIG_NATIVE_WINDOWS */
502 return 0;
503}
504
505
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800506#ifdef CONFIG_WPS
507static int gen_uuid(const char *txt_addr)
508{
509 u8 addr[ETH_ALEN];
510 u8 uuid[UUID_LEN];
511 char buf[100];
512
513 if (hwaddr_aton(txt_addr, addr) < 0)
514 return -1;
515
516 uuid_gen_mac_addr(addr, uuid);
517 if (uuid_bin2str(uuid, buf, sizeof(buf)) < 0)
518 return -1;
519
520 printf("%s\n", buf);
521
522 return 0;
523}
524#endif /* CONFIG_WPS */
525
526
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700527int main(int argc, char *argv[])
528{
529 struct hapd_interfaces interfaces;
530 int ret = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800531 size_t i, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700532 int c, debug = 0, daemonize = 0;
533 char *pid_file = NULL;
534 const char *log_file = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700535 const char *entropy_file = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800536 char **bss_config = NULL, **tmp_bss;
537 size_t num_bss_configs = 0;
538#ifdef CONFIG_DEBUG_LINUX_TRACING
539 int enable_trace_dbg = 0;
540#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700541
542 if (os_program_init())
543 return -1;
544
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700545 os_memset(&interfaces, 0, sizeof(interfaces));
546 interfaces.reload_config = hostapd_reload_config;
547 interfaces.config_read_cb = hostapd_config_read;
548 interfaces.for_each_interface = hostapd_for_each_interface;
549 interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
550 interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
551 interfaces.driver_init = hostapd_driver_init;
552 interfaces.global_iface_path = NULL;
553 interfaces.global_iface_name = NULL;
554 interfaces.global_ctrl_sock = -1;
555
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700556 for (;;) {
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800557 c = getopt(argc, argv, "b:Bde:f:hKP:Ttu:vg:G:");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700558 if (c < 0)
559 break;
560 switch (c) {
561 case 'h':
562 usage();
563 break;
564 case 'd':
565 debug++;
566 if (wpa_debug_level > 0)
567 wpa_debug_level--;
568 break;
569 case 'B':
570 daemonize++;
571 break;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700572 case 'e':
573 entropy_file = optarg;
574 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700575 case 'f':
576 log_file = optarg;
577 break;
578 case 'K':
579 wpa_debug_show_keys++;
580 break;
581 case 'P':
582 os_free(pid_file);
583 pid_file = os_rel2abs_path(optarg);
584 break;
585 case 't':
586 wpa_debug_timestamp++;
587 break;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800588#ifdef CONFIG_DEBUG_LINUX_TRACING
589 case 'T':
590 enable_trace_dbg = 1;
591 break;
592#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700593 case 'v':
594 show_version();
595 exit(1);
596 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700597 case 'g':
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700598 if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
599 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700600 break;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700601 case 'G':
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700602 if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
603 return -1;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700604 break;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800605 case 'b':
606 tmp_bss = os_realloc_array(bss_config,
607 num_bss_configs + 1,
608 sizeof(char *));
609 if (tmp_bss == NULL)
610 goto out;
611 bss_config = tmp_bss;
612 bss_config[num_bss_configs++] = optarg;
613 break;
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800614#ifdef CONFIG_WPS
615 case 'u':
616 return gen_uuid(optarg);
617#endif /* CONFIG_WPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700618 default:
619 usage();
620 break;
621 }
622 }
623
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800624 if (optind == argc && interfaces.global_iface_path == NULL &&
625 num_bss_configs == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700626 usage();
627
628 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
629
630 if (log_file)
631 wpa_debug_open_file(log_file);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800632#ifdef CONFIG_DEBUG_LINUX_TRACING
633 if (enable_trace_dbg) {
634 int tret = wpa_debug_open_linux_tracing();
635 if (tret) {
636 wpa_printf(MSG_ERROR, "Failed to enable trace logging");
637 return -1;
638 }
639 }
640#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700641
642 interfaces.count = argc - optind;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800643 if (interfaces.count || num_bss_configs) {
644 interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700645 sizeof(struct hostapd_iface *));
646 if (interfaces.iface == NULL) {
647 wpa_printf(MSG_ERROR, "malloc failed");
648 return -1;
649 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700650 }
651
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700652 if (hostapd_global_init(&interfaces, entropy_file)) {
653 wpa_printf(MSG_ERROR, "Failed to initilize global context");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700654 return -1;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700655 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700656
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800657 /* Allocate and parse configuration for full interface files */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700658 for (i = 0; i < interfaces.count; i++) {
659 interfaces.iface[i] = hostapd_interface_init(&interfaces,
660 argv[optind + i],
661 debug);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700662 if (!interfaces.iface[i]) {
663 wpa_printf(MSG_ERROR, "Failed to initialize interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700664 goto out;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700665 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700666 }
667
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800668 /* Allocate and parse configuration for per-BSS files */
669 for (i = 0; i < num_bss_configs; i++) {
670 struct hostapd_iface *iface;
671 char *fname;
672
673 wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
674 fname = os_strchr(bss_config[i], ':');
675 if (fname == NULL) {
676 wpa_printf(MSG_ERROR,
677 "Invalid BSS config identifier '%s'",
678 bss_config[i]);
679 goto out;
680 }
681 *fname++ = '\0';
682 iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
683 fname, debug);
684 if (iface == NULL)
685 goto out;
686 for (j = 0; j < interfaces.count; j++) {
687 if (interfaces.iface[j] == iface)
688 break;
689 }
690 if (j == interfaces.count) {
691 struct hostapd_iface **tmp;
692 tmp = os_realloc_array(interfaces.iface,
693 interfaces.count + 1,
694 sizeof(struct hostapd_iface *));
695 if (tmp == NULL) {
696 hostapd_interface_deinit_free(iface);
697 goto out;
698 }
699 interfaces.iface = tmp;
700 interfaces.iface[interfaces.count++] = iface;
701 }
702 }
703
704 /*
705 * Enable configured interfaces. Depending on channel configuration,
706 * this may complete full initialization before returning or use a
707 * callback mechanism to complete setup in case of operations like HT
708 * co-ex scans, ACS, or DFS are needed to determine channel parameters.
709 * In such case, the interface will be enabled from eloop context within
710 * hostapd_global_run().
711 */
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800712 interfaces.terminate_on_error = interfaces.count;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800713 for (i = 0; i < interfaces.count; i++) {
714 if (hostapd_driver_init(interfaces.iface[i]) ||
715 hostapd_setup_interface(interfaces.iface[i]))
716 goto out;
717 }
718
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700719 hostapd_global_ctrl_iface_init(&interfaces);
720
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700721 if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
722 wpa_printf(MSG_ERROR, "Failed to start eloop");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700723 goto out;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700724 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700725
726 ret = 0;
727
728 out:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700729 hostapd_global_ctrl_iface_deinit(&interfaces);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700730 /* Deinitialize all interfaces */
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800731 for (i = 0; i < interfaces.count; i++) {
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700732 if (!interfaces.iface[i])
733 continue;
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800734 interfaces.iface[i]->driver_ap_teardown =
735 !!(interfaces.iface[i]->drv_flags &
736 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700737 hostapd_interface_deinit_free(interfaces.iface[i]);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800738 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700739 os_free(interfaces.iface);
740
741 hostapd_global_deinit(pid_file);
742 os_free(pid_file);
743
744 if (log_file)
745 wpa_debug_close_file();
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800746 wpa_debug_close_linux_tracing();
747
748 os_free(bss_config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700749
750 os_program_deinit();
751
752 return ret;
753}