blob: 25dc20b4d6e490e39f067d28282406d0c36da38e [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / main()
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003 * Copyright (c) 2002-2016, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
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 Shmidtd80a4012015-11-05 16:35:40 -080027#include "fst/fst.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028#include "config_file.h"
29#include "eap_register.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030#include "ctrl_iface.h"
31
32
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080033struct hapd_global {
34 void **drv_priv;
35 size_t drv_count;
36};
37
38static struct hapd_global global;
39
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070041#ifndef CONFIG_NO_HOSTAPD_LOGGER
42static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
43 int level, const char *txt, size_t len)
44{
45 struct hostapd_data *hapd = ctx;
46 char *format, *module_str;
47 int maxlen;
48 int conf_syslog_level, conf_stdout_level;
49 unsigned int conf_syslog, conf_stdout;
50
51 maxlen = len + 100;
52 format = os_malloc(maxlen);
53 if (!format)
54 return;
55
56 if (hapd && hapd->conf) {
57 conf_syslog_level = hapd->conf->logger_syslog_level;
58 conf_stdout_level = hapd->conf->logger_stdout_level;
59 conf_syslog = hapd->conf->logger_syslog;
60 conf_stdout = hapd->conf->logger_stdout;
61 } else {
62 conf_syslog_level = conf_stdout_level = 0;
63 conf_syslog = conf_stdout = (unsigned int) -1;
64 }
65
66 switch (module) {
67 case HOSTAPD_MODULE_IEEE80211:
68 module_str = "IEEE 802.11";
69 break;
70 case HOSTAPD_MODULE_IEEE8021X:
71 module_str = "IEEE 802.1X";
72 break;
73 case HOSTAPD_MODULE_RADIUS:
74 module_str = "RADIUS";
75 break;
76 case HOSTAPD_MODULE_WPA:
77 module_str = "WPA";
78 break;
79 case HOSTAPD_MODULE_DRIVER:
80 module_str = "DRIVER";
81 break;
82 case HOSTAPD_MODULE_IAPP:
83 module_str = "IAPP";
84 break;
85 case HOSTAPD_MODULE_MLME:
86 module_str = "MLME";
87 break;
88 default:
89 module_str = NULL;
90 break;
91 }
92
93 if (hapd && hapd->conf && addr)
94 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
95 hapd->conf->iface, MAC2STR(addr),
Dmitry Shmidt96be6222014-02-13 10:16:51 -080096 module_str ? " " : "", module_str ? module_str : "",
97 txt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070098 else if (hapd && hapd->conf)
99 os_snprintf(format, maxlen, "%s:%s%s %s",
100 hapd->conf->iface, module_str ? " " : "",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700101 module_str ? module_str : "", txt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700102 else if (addr)
103 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
104 MAC2STR(addr), module_str ? " " : "",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700105 module_str ? module_str : "", txt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106 else
107 os_snprintf(format, maxlen, "%s%s%s",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700108 module_str ? module_str : "",
109 module_str ? ": " : "", txt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700110
111 if ((conf_stdout & module) && level >= conf_stdout_level) {
112 wpa_debug_print_timestamp();
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800113 wpa_printf(MSG_INFO, "%s", format);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700114 }
115
116#ifndef CONFIG_NATIVE_WINDOWS
117 if ((conf_syslog & module) && level >= conf_syslog_level) {
118 int priority;
119 switch (level) {
120 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
121 case HOSTAPD_LEVEL_DEBUG:
122 priority = LOG_DEBUG;
123 break;
124 case HOSTAPD_LEVEL_INFO:
125 priority = LOG_INFO;
126 break;
127 case HOSTAPD_LEVEL_NOTICE:
128 priority = LOG_NOTICE;
129 break;
130 case HOSTAPD_LEVEL_WARNING:
131 priority = LOG_WARNING;
132 break;
133 default:
134 priority = LOG_INFO;
135 break;
136 }
137 syslog(priority, "%s", format);
138 }
139#endif /* CONFIG_NATIVE_WINDOWS */
140
141 os_free(format);
142}
143#endif /* CONFIG_NO_HOSTAPD_LOGGER */
144
145
146/**
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800147 * hostapd_driver_init - Preparate driver interface
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700148 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700149static int hostapd_driver_init(struct hostapd_iface *iface)
150{
151 struct wpa_init_params params;
152 size_t i;
153 struct hostapd_data *hapd = iface->bss[0];
154 struct hostapd_bss_config *conf = hapd->conf;
155 u8 *b = conf->bssid;
156 struct wpa_driver_capa capa;
157
158 if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
159 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
160 return -1;
161 }
162
163 /* Initialize the driver interface */
164 if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
165 b = NULL;
166
167 os_memset(&params, 0, sizeof(params));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800168 for (i = 0; wpa_drivers[i]; i++) {
169 if (wpa_drivers[i] != hapd->driver)
170 continue;
171
172 if (global.drv_priv[i] == NULL &&
173 wpa_drivers[i]->global_init) {
174 global.drv_priv[i] = wpa_drivers[i]->global_init();
175 if (global.drv_priv[i] == NULL) {
176 wpa_printf(MSG_ERROR, "Failed to initialize "
177 "driver '%s'",
178 wpa_drivers[i]->name);
179 return -1;
180 }
181 }
182
183 params.global_priv = global.drv_priv[i];
184 break;
185 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700186 params.bssid = b;
187 params.ifname = hapd->conf->iface;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800188 params.driver_params = hapd->iconf->driver_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700189 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 Shmidt0207e232014-09-03 14:58:37 -0700214 struct wowlan_triggers *triggs;
215
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700216 iface->drv_flags = capa.flags;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800217 iface->smps_modes = capa.smps_modes;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800218 iface->probe_resp_offloads = capa.probe_resp_offloads;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700219 iface->extended_capa = capa.extended_capa;
220 iface->extended_capa_mask = capa.extended_capa_mask;
221 iface->extended_capa_len = capa.extended_capa_len;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700222 iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
Dmitry Shmidt0207e232014-09-03 14:58:37 -0700223
224 triggs = wpa_get_wowlan_triggers(conf->wowlan_triggers, &capa);
225 if (triggs && hapd->driver->set_wowlan) {
226 if (hapd->driver->set_wowlan(hapd->drv_priv, triggs))
227 wpa_printf(MSG_ERROR, "set_wowlan failed");
228 }
229 os_free(triggs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800230 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700231
232 return 0;
233}
234
235
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800236/**
237 * hostapd_interface_init - Read configuration file and init BSS data
238 *
239 * This function is used to parse configuration file for a full interface (one
240 * or more BSSes sharing the same radio) and allocate memory for the BSS
241 * interfaces. No actiual driver operations are started.
242 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700243static struct hostapd_iface *
244hostapd_interface_init(struct hapd_interfaces *interfaces,
245 const char *config_fname, int debug)
246{
247 struct hostapd_iface *iface;
248 int k;
249
250 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800251 iface = hostapd_init(interfaces, config_fname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700252 if (!iface)
253 return NULL;
254 iface->interfaces = interfaces;
255
256 for (k = 0; k < debug; k++) {
257 if (iface->bss[0]->conf->logger_stdout_level > 0)
258 iface->bss[0]->conf->logger_stdout_level--;
259 }
260
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800261 if (iface->conf->bss[0]->iface[0] == '\0' &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700262 !hostapd_drv_none(iface->bss[0])) {
263 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
264 config_fname);
265 hostapd_interface_deinit_free(iface);
266 return NULL;
267 }
268
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700269 return iface;
270}
271
272
273/**
274 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
275 */
276static void handle_term(int sig, void *signal_ctx)
277{
278 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
279 eloop_terminate();
280}
281
282
283#ifndef CONFIG_NATIVE_WINDOWS
284
285static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
286{
287 if (hostapd_reload_config(iface) < 0) {
288 wpa_printf(MSG_WARNING, "Failed to read new configuration "
289 "file - continuing with old.");
290 }
291 return 0;
292}
293
294
295/**
296 * handle_reload - SIGHUP handler to reload configuration
297 */
298static void handle_reload(int sig, void *signal_ctx)
299{
300 struct hapd_interfaces *interfaces = signal_ctx;
301 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
302 sig);
303 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
304}
305
306
307static void handle_dump_state(int sig, void *signal_ctx)
308{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800309 /* Not used anymore - ignore signal */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700310}
311#endif /* CONFIG_NATIVE_WINDOWS */
312
313
Jouni Malinen75ecf522011-06-27 15:19:46 -0700314static int hostapd_global_init(struct hapd_interfaces *interfaces,
315 const char *entropy_file)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700316{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800317 int i;
318
319 os_memset(&global, 0, sizeof(global));
320
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700321 hostapd_logger_register_cb(hostapd_logger_cb);
322
323 if (eap_server_register_methods()) {
324 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
325 return -1;
326 }
327
328 if (eloop_init()) {
329 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
330 return -1;
331 }
332
Jouni Malinen75ecf522011-06-27 15:19:46 -0700333 random_init(entropy_file);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700334
335#ifndef CONFIG_NATIVE_WINDOWS
336 eloop_register_signal(SIGHUP, handle_reload, interfaces);
337 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
338#endif /* CONFIG_NATIVE_WINDOWS */
339 eloop_register_signal_terminate(handle_term, interfaces);
340
341#ifndef CONFIG_NATIVE_WINDOWS
342 openlog("hostapd", 0, LOG_DAEMON);
343#endif /* CONFIG_NATIVE_WINDOWS */
344
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800345 for (i = 0; wpa_drivers[i]; i++)
346 global.drv_count++;
347 if (global.drv_count == 0) {
348 wpa_printf(MSG_ERROR, "No drivers enabled");
349 return -1;
350 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700351 global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800352 if (global.drv_priv == NULL)
353 return -1;
354
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700355 return 0;
356}
357
358
359static void hostapd_global_deinit(const char *pid_file)
360{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800361 int i;
362
363 for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
364 if (!global.drv_priv[i])
365 continue;
366 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
367 }
368 os_free(global.drv_priv);
369 global.drv_priv = NULL;
370
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700371#ifdef EAP_SERVER_TNC
372 tncs_global_deinit();
373#endif /* EAP_SERVER_TNC */
374
375 random_deinit();
376
377 eloop_destroy();
378
379#ifndef CONFIG_NATIVE_WINDOWS
380 closelog();
381#endif /* CONFIG_NATIVE_WINDOWS */
382
383 eap_server_unregister_methods();
384
385 os_daemonize_terminate(pid_file);
386}
387
388
389static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
390 const char *pid_file)
391{
392#ifdef EAP_SERVER_TNC
393 int tnc = 0;
394 size_t i, k;
395
396 for (i = 0; !tnc && i < ifaces->count; i++) {
397 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
398 if (ifaces->iface[i]->bss[0]->conf->tnc) {
399 tnc++;
400 break;
401 }
402 }
403 }
404
405 if (tnc && tncs_global_init() < 0) {
406 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
407 return -1;
408 }
409#endif /* EAP_SERVER_TNC */
410
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800411 if (daemonize) {
412 if (os_daemonize(pid_file)) {
413 wpa_printf(MSG_ERROR, "daemon: %s", strerror(errno));
414 return -1;
415 }
416 if (eloop_sock_requeue()) {
417 wpa_printf(MSG_ERROR, "eloop_sock_requeue: %s",
418 strerror(errno));
419 return -1;
420 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700421 }
422
423 eloop_run();
424
425 return 0;
426}
427
428
429static void show_version(void)
430{
431 fprintf(stderr,
432 "hostapd v" VERSION_STR "\n"
433 "User space daemon for IEEE 802.11 AP management,\n"
434 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800435 "Copyright (c) 2002-2016, Jouni Malinen <j@w1.fi> "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700436 "and contributors\n");
437}
438
439
440static void usage(void)
441{
442 show_version();
443 fprintf(stderr,
444 "\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -0700445 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700446 "\\\n"
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700447 " [-g <global ctrl_iface>] [-G <group>] \\\n"
448 " <configuration file(s)>\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700449 "\n"
450 "options:\n"
451 " -h show this usage\n"
452 " -d show more debug messages (-dd for even more)\n"
453 " -B run daemon in the background\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -0700454 " -e entropy file\n"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700455 " -g global control interface path\n"
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700456 " -G group for control interfaces\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700457 " -P PID file\n"
458 " -K include key data in debug messages\n"
459#ifdef CONFIG_DEBUG_FILE
460 " -f log output to debug file instead of stdout\n"
461#endif /* CONFIG_DEBUG_FILE */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800462#ifdef CONFIG_DEBUG_LINUX_TRACING
463 " -T = record to Linux tracing in addition to logging\n"
464 " (records all messages regardless of debug verbosity)\n"
465#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800466 " -S start all the interfaces synchronously\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700467 " -t include timestamps in some debug messages\n"
468 " -v show hostapd version\n");
469
470 exit(1);
471}
472
473
474static const char * hostapd_msg_ifname_cb(void *ctx)
475{
476 struct hostapd_data *hapd = ctx;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800477 if (hapd && hapd->conf)
478 return hapd->conf->iface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700479 return NULL;
480}
481
482
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700483static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
484 const char *path)
485{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800486#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700487 char *pos;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800488#endif /* !CONFIG_CTRL_IFACE_UDP */
489
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700490 os_free(interfaces->global_iface_path);
491 interfaces->global_iface_path = os_strdup(path);
492 if (interfaces->global_iface_path == NULL)
493 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800494
495#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700496 pos = os_strrchr(interfaces->global_iface_path, '/');
497 if (pos == NULL) {
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700498 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
499 "file");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700500 os_free(interfaces->global_iface_path);
501 interfaces->global_iface_path = NULL;
502 return -1;
503 }
504
505 *pos = '\0';
506 interfaces->global_iface_name = pos + 1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800507#endif /* !CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700508
509 return 0;
510}
511
512
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700513static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
514 const char *group)
515{
516#ifndef CONFIG_NATIVE_WINDOWS
517 struct group *grp;
518 grp = getgrnam(group);
519 if (grp == NULL) {
520 wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
521 return -1;
522 }
523 interfaces->ctrl_iface_group = grp->gr_gid;
524#endif /* CONFIG_NATIVE_WINDOWS */
525 return 0;
526}
527
528
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800529#ifdef CONFIG_WPS
530static int gen_uuid(const char *txt_addr)
531{
532 u8 addr[ETH_ALEN];
533 u8 uuid[UUID_LEN];
534 char buf[100];
535
536 if (hwaddr_aton(txt_addr, addr) < 0)
537 return -1;
538
539 uuid_gen_mac_addr(addr, uuid);
540 if (uuid_bin2str(uuid, buf, sizeof(buf)) < 0)
541 return -1;
542
543 printf("%s\n", buf);
544
545 return 0;
546}
547#endif /* CONFIG_WPS */
548
549
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800550#ifndef HOSTAPD_CLEANUP_INTERVAL
551#define HOSTAPD_CLEANUP_INTERVAL 10
552#endif /* HOSTAPD_CLEANUP_INTERVAL */
553
554static int hostapd_periodic_call(struct hostapd_iface *iface, void *ctx)
555{
556 hostapd_periodic_iface(iface);
557 return 0;
558}
559
560
561/* Periodic cleanup tasks */
562static void hostapd_periodic(void *eloop_ctx, void *timeout_ctx)
563{
564 struct hapd_interfaces *interfaces = eloop_ctx;
565
566 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
567 hostapd_periodic, interfaces, NULL);
568 hostapd_for_each_interface(interfaces, hostapd_periodic_call, NULL);
569}
570
571
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700572int main(int argc, char *argv[])
573{
574 struct hapd_interfaces interfaces;
575 int ret = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800576 size_t i, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700577 int c, debug = 0, daemonize = 0;
578 char *pid_file = NULL;
579 const char *log_file = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700580 const char *entropy_file = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800581 char **bss_config = NULL, **tmp_bss;
582 size_t num_bss_configs = 0;
583#ifdef CONFIG_DEBUG_LINUX_TRACING
584 int enable_trace_dbg = 0;
585#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800586 int start_ifaces_in_sync = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700587
588 if (os_program_init())
589 return -1;
590
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700591 os_memset(&interfaces, 0, sizeof(interfaces));
592 interfaces.reload_config = hostapd_reload_config;
593 interfaces.config_read_cb = hostapd_config_read;
594 interfaces.for_each_interface = hostapd_for_each_interface;
595 interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
596 interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
597 interfaces.driver_init = hostapd_driver_init;
598 interfaces.global_iface_path = NULL;
599 interfaces.global_iface_name = NULL;
600 interfaces.global_ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800601 dl_list_init(&interfaces.global_ctrl_dst);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700602
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700603 for (;;) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800604 c = getopt(argc, argv, "b:Bde:f:hKP:STtu:vg:G:");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700605 if (c < 0)
606 break;
607 switch (c) {
608 case 'h':
609 usage();
610 break;
611 case 'd':
612 debug++;
613 if (wpa_debug_level > 0)
614 wpa_debug_level--;
615 break;
616 case 'B':
617 daemonize++;
618 break;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700619 case 'e':
620 entropy_file = optarg;
621 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700622 case 'f':
623 log_file = optarg;
624 break;
625 case 'K':
626 wpa_debug_show_keys++;
627 break;
628 case 'P':
629 os_free(pid_file);
630 pid_file = os_rel2abs_path(optarg);
631 break;
632 case 't':
633 wpa_debug_timestamp++;
634 break;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800635#ifdef CONFIG_DEBUG_LINUX_TRACING
636 case 'T':
637 enable_trace_dbg = 1;
638 break;
639#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700640 case 'v':
641 show_version();
642 exit(1);
643 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700644 case 'g':
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700645 if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
646 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700647 break;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700648 case 'G':
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700649 if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
650 return -1;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700651 break;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800652 case 'b':
653 tmp_bss = os_realloc_array(bss_config,
654 num_bss_configs + 1,
655 sizeof(char *));
656 if (tmp_bss == NULL)
657 goto out;
658 bss_config = tmp_bss;
659 bss_config[num_bss_configs++] = optarg;
660 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800661 case 'S':
662 start_ifaces_in_sync = 1;
663 break;
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800664#ifdef CONFIG_WPS
665 case 'u':
666 return gen_uuid(optarg);
667#endif /* CONFIG_WPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700668 default:
669 usage();
670 break;
671 }
672 }
673
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800674 if (optind == argc && interfaces.global_iface_path == NULL &&
675 num_bss_configs == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700676 usage();
677
678 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
679
680 if (log_file)
681 wpa_debug_open_file(log_file);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800682 else
683 wpa_debug_setup_stdout();
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800684#ifdef CONFIG_DEBUG_LINUX_TRACING
685 if (enable_trace_dbg) {
686 int tret = wpa_debug_open_linux_tracing();
687 if (tret) {
688 wpa_printf(MSG_ERROR, "Failed to enable trace logging");
689 return -1;
690 }
691 }
692#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700693
694 interfaces.count = argc - optind;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800695 if (interfaces.count || num_bss_configs) {
696 interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700697 sizeof(struct hostapd_iface *));
698 if (interfaces.iface == NULL) {
699 wpa_printf(MSG_ERROR, "malloc failed");
700 return -1;
701 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700702 }
703
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700704 if (hostapd_global_init(&interfaces, entropy_file)) {
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700705 wpa_printf(MSG_ERROR, "Failed to initialize global context");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700706 return -1;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700707 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700708
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800709 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
710 hostapd_periodic, &interfaces, NULL);
711
712 if (fst_global_init()) {
713 wpa_printf(MSG_ERROR,
714 "Failed to initialize global FST context");
715 goto out;
716 }
717
718#if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE)
719 if (!fst_global_add_ctrl(fst_ctrl_cli))
720 wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl");
721#endif /* CONFIG_FST && CONFIG_CTRL_IFACE */
722
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800723 /* Allocate and parse configuration for full interface files */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700724 for (i = 0; i < interfaces.count; i++) {
725 interfaces.iface[i] = hostapd_interface_init(&interfaces,
726 argv[optind + i],
727 debug);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700728 if (!interfaces.iface[i]) {
729 wpa_printf(MSG_ERROR, "Failed to initialize interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700730 goto out;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700731 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800732 if (start_ifaces_in_sync)
733 interfaces.iface[i]->need_to_start_in_sync = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700734 }
735
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800736 /* Allocate and parse configuration for per-BSS files */
737 for (i = 0; i < num_bss_configs; i++) {
738 struct hostapd_iface *iface;
739 char *fname;
740
741 wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
742 fname = os_strchr(bss_config[i], ':');
743 if (fname == NULL) {
744 wpa_printf(MSG_ERROR,
745 "Invalid BSS config identifier '%s'",
746 bss_config[i]);
747 goto out;
748 }
749 *fname++ = '\0';
750 iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
751 fname, debug);
752 if (iface == NULL)
753 goto out;
754 for (j = 0; j < interfaces.count; j++) {
755 if (interfaces.iface[j] == iface)
756 break;
757 }
758 if (j == interfaces.count) {
759 struct hostapd_iface **tmp;
760 tmp = os_realloc_array(interfaces.iface,
761 interfaces.count + 1,
762 sizeof(struct hostapd_iface *));
763 if (tmp == NULL) {
764 hostapd_interface_deinit_free(iface);
765 goto out;
766 }
767 interfaces.iface = tmp;
768 interfaces.iface[interfaces.count++] = iface;
769 }
770 }
771
772 /*
773 * Enable configured interfaces. Depending on channel configuration,
774 * this may complete full initialization before returning or use a
775 * callback mechanism to complete setup in case of operations like HT
776 * co-ex scans, ACS, or DFS are needed to determine channel parameters.
777 * In such case, the interface will be enabled from eloop context within
778 * hostapd_global_run().
779 */
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800780 interfaces.terminate_on_error = interfaces.count;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800781 for (i = 0; i < interfaces.count; i++) {
782 if (hostapd_driver_init(interfaces.iface[i]) ||
783 hostapd_setup_interface(interfaces.iface[i]))
784 goto out;
785 }
786
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700787 hostapd_global_ctrl_iface_init(&interfaces);
788
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700789 if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
790 wpa_printf(MSG_ERROR, "Failed to start eloop");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700791 goto out;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700792 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700793
794 ret = 0;
795
796 out:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700797 hostapd_global_ctrl_iface_deinit(&interfaces);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700798 /* Deinitialize all interfaces */
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800799 for (i = 0; i < interfaces.count; i++) {
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700800 if (!interfaces.iface[i])
801 continue;
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800802 interfaces.iface[i]->driver_ap_teardown =
803 !!(interfaces.iface[i]->drv_flags &
804 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700805 hostapd_interface_deinit_free(interfaces.iface[i]);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800806 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700807 os_free(interfaces.iface);
808
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800809 eloop_cancel_timeout(hostapd_periodic, &interfaces, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700810 hostapd_global_deinit(pid_file);
811 os_free(pid_file);
812
813 if (log_file)
814 wpa_debug_close_file();
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800815 wpa_debug_close_linux_tracing();
816
817 os_free(bss_config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700818
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800819 fst_global_deinit();
820
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700821 os_program_deinit();
822
823 return ret;
824}