blob: 2c8dbd30a274e927ab7615b6e66c0a0898297d09 [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) {
Dmitry Shmidte4663042016-04-04 10:07:49 -0700174 global.drv_priv[i] =
175 wpa_drivers[i]->global_init(iface->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800176 if (global.drv_priv[i] == NULL) {
177 wpa_printf(MSG_ERROR, "Failed to initialize "
178 "driver '%s'",
179 wpa_drivers[i]->name);
180 return -1;
181 }
182 }
183
184 params.global_priv = global.drv_priv[i];
185 break;
186 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700187 params.bssid = b;
188 params.ifname = hapd->conf->iface;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800189 params.driver_params = hapd->iconf->driver_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700190 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
191
192 params.num_bridge = hapd->iface->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700193 params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194 if (params.bridge == NULL)
195 return -1;
196 for (i = 0; i < hapd->iface->num_bss; i++) {
197 struct hostapd_data *bss = hapd->iface->bss[i];
198 if (bss->conf->bridge[0])
199 params.bridge[i] = bss->conf->bridge;
200 }
201
202 params.own_addr = hapd->own_addr;
203
204 hapd->drv_priv = hapd->driver->hapd_init(hapd, &params);
205 os_free(params.bridge);
206 if (hapd->drv_priv == NULL) {
207 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
208 hapd->driver->name);
209 hapd->driver = NULL;
210 return -1;
211 }
212
213 if (hapd->driver->get_capa &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800214 hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
Dmitry Shmidt0207e232014-09-03 14:58:37 -0700215 struct wowlan_triggers *triggs;
216
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700217 iface->drv_flags = capa.flags;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800218 iface->smps_modes = capa.smps_modes;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800219 iface->probe_resp_offloads = capa.probe_resp_offloads;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700220 /*
221 * Use default extended capa values from per-radio information
222 */
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700223 iface->extended_capa = capa.extended_capa;
224 iface->extended_capa_mask = capa.extended_capa_mask;
225 iface->extended_capa_len = capa.extended_capa_len;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700226 iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
Dmitry Shmidt0207e232014-09-03 14:58:37 -0700227
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700228 /*
229 * Override extended capa with per-interface type (AP), if
230 * available from the driver.
231 */
232 hostapd_get_ext_capa(iface);
233
Dmitry Shmidt0207e232014-09-03 14:58:37 -0700234 triggs = wpa_get_wowlan_triggers(conf->wowlan_triggers, &capa);
235 if (triggs && hapd->driver->set_wowlan) {
236 if (hapd->driver->set_wowlan(hapd->drv_priv, triggs))
237 wpa_printf(MSG_ERROR, "set_wowlan failed");
238 }
239 os_free(triggs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800240 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700241
242 return 0;
243}
244
245
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800246/**
247 * hostapd_interface_init - Read configuration file and init BSS data
248 *
249 * This function is used to parse configuration file for a full interface (one
250 * or more BSSes sharing the same radio) and allocate memory for the BSS
251 * interfaces. No actiual driver operations are started.
252 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700253static struct hostapd_iface *
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700254hostapd_interface_init(struct hapd_interfaces *interfaces, const char *if_name,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700255 const char *config_fname, int debug)
256{
257 struct hostapd_iface *iface;
258 int k;
259
260 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800261 iface = hostapd_init(interfaces, config_fname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700262 if (!iface)
263 return NULL;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700264
265 if (if_name) {
266 os_strlcpy(iface->conf->bss[0]->iface, if_name,
267 sizeof(iface->conf->bss[0]->iface));
268 }
269
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700270 iface->interfaces = interfaces;
271
272 for (k = 0; k < debug; k++) {
273 if (iface->bss[0]->conf->logger_stdout_level > 0)
274 iface->bss[0]->conf->logger_stdout_level--;
275 }
276
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800277 if (iface->conf->bss[0]->iface[0] == '\0' &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700278 !hostapd_drv_none(iface->bss[0])) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700279 wpa_printf(MSG_ERROR,
280 "Interface name not specified in %s, nor by '-i' parameter",
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700281 config_fname);
282 hostapd_interface_deinit_free(iface);
283 return NULL;
284 }
285
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700286 return iface;
287}
288
289
290/**
291 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
292 */
293static void handle_term(int sig, void *signal_ctx)
294{
295 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
296 eloop_terminate();
297}
298
299
300#ifndef CONFIG_NATIVE_WINDOWS
301
302static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
303{
304 if (hostapd_reload_config(iface) < 0) {
305 wpa_printf(MSG_WARNING, "Failed to read new configuration "
306 "file - continuing with old.");
307 }
308 return 0;
309}
310
311
312/**
313 * handle_reload - SIGHUP handler to reload configuration
314 */
315static void handle_reload(int sig, void *signal_ctx)
316{
317 struct hapd_interfaces *interfaces = signal_ctx;
318 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
319 sig);
320 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
321}
322
323
324static void handle_dump_state(int sig, void *signal_ctx)
325{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800326 /* Not used anymore - ignore signal */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700327}
328#endif /* CONFIG_NATIVE_WINDOWS */
329
330
Jouni Malinen75ecf522011-06-27 15:19:46 -0700331static int hostapd_global_init(struct hapd_interfaces *interfaces,
332 const char *entropy_file)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700333{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800334 int i;
335
336 os_memset(&global, 0, sizeof(global));
337
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700338 hostapd_logger_register_cb(hostapd_logger_cb);
339
340 if (eap_server_register_methods()) {
341 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
342 return -1;
343 }
344
345 if (eloop_init()) {
346 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
347 return -1;
348 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700349 interfaces->eloop_initialized = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700350
Jouni Malinen75ecf522011-06-27 15:19:46 -0700351 random_init(entropy_file);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352
353#ifndef CONFIG_NATIVE_WINDOWS
354 eloop_register_signal(SIGHUP, handle_reload, interfaces);
355 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
356#endif /* CONFIG_NATIVE_WINDOWS */
357 eloop_register_signal_terminate(handle_term, interfaces);
358
359#ifndef CONFIG_NATIVE_WINDOWS
360 openlog("hostapd", 0, LOG_DAEMON);
361#endif /* CONFIG_NATIVE_WINDOWS */
362
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800363 for (i = 0; wpa_drivers[i]; i++)
364 global.drv_count++;
365 if (global.drv_count == 0) {
366 wpa_printf(MSG_ERROR, "No drivers enabled");
367 return -1;
368 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700369 global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800370 if (global.drv_priv == NULL)
371 return -1;
372
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700373 return 0;
374}
375
376
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700377static void hostapd_global_deinit(const char *pid_file, int eloop_initialized)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700378{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800379 int i;
380
381 for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
382 if (!global.drv_priv[i])
383 continue;
384 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
385 }
386 os_free(global.drv_priv);
387 global.drv_priv = NULL;
388
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700389#ifdef EAP_SERVER_TNC
390 tncs_global_deinit();
391#endif /* EAP_SERVER_TNC */
392
393 random_deinit();
394
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700395 if (eloop_initialized)
396 eloop_destroy();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700397
398#ifndef CONFIG_NATIVE_WINDOWS
399 closelog();
400#endif /* CONFIG_NATIVE_WINDOWS */
401
402 eap_server_unregister_methods();
403
404 os_daemonize_terminate(pid_file);
405}
406
407
408static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
409 const char *pid_file)
410{
411#ifdef EAP_SERVER_TNC
412 int tnc = 0;
413 size_t i, k;
414
415 for (i = 0; !tnc && i < ifaces->count; i++) {
416 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
417 if (ifaces->iface[i]->bss[0]->conf->tnc) {
418 tnc++;
419 break;
420 }
421 }
422 }
423
424 if (tnc && tncs_global_init() < 0) {
425 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
426 return -1;
427 }
428#endif /* EAP_SERVER_TNC */
429
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800430 if (daemonize) {
431 if (os_daemonize(pid_file)) {
432 wpa_printf(MSG_ERROR, "daemon: %s", strerror(errno));
433 return -1;
434 }
435 if (eloop_sock_requeue()) {
436 wpa_printf(MSG_ERROR, "eloop_sock_requeue: %s",
437 strerror(errno));
438 return -1;
439 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700440 }
441
442 eloop_run();
443
444 return 0;
445}
446
447
448static void show_version(void)
449{
450 fprintf(stderr,
451 "hostapd v" VERSION_STR "\n"
452 "User space daemon for IEEE 802.11 AP management,\n"
453 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800454 "Copyright (c) 2002-2016, Jouni Malinen <j@w1.fi> "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700455 "and contributors\n");
456}
457
458
459static void usage(void)
460{
461 show_version();
462 fprintf(stderr,
463 "\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -0700464 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700465 "\\\n"
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700466 " [-g <global ctrl_iface>] [-G <group>]\\\n"
467 " [-i <comma-separated list of interface names>]\\\n"
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700468 " <configuration file(s)>\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700469 "\n"
470 "options:\n"
471 " -h show this usage\n"
472 " -d show more debug messages (-dd for even more)\n"
473 " -B run daemon in the background\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -0700474 " -e entropy file\n"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700475 " -g global control interface path\n"
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700476 " -G group for control interfaces\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700477 " -P PID file\n"
478 " -K include key data in debug messages\n"
479#ifdef CONFIG_DEBUG_FILE
480 " -f log output to debug file instead of stdout\n"
481#endif /* CONFIG_DEBUG_FILE */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800482#ifdef CONFIG_DEBUG_LINUX_TRACING
483 " -T = record to Linux tracing in addition to logging\n"
484 " (records all messages regardless of debug verbosity)\n"
485#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700486 " -i list of interface names to use\n"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800487 " -S start all the interfaces synchronously\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700488 " -t include timestamps in some debug messages\n"
489 " -v show hostapd version\n");
490
491 exit(1);
492}
493
494
495static const char * hostapd_msg_ifname_cb(void *ctx)
496{
497 struct hostapd_data *hapd = ctx;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800498 if (hapd && hapd->conf)
499 return hapd->conf->iface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700500 return NULL;
501}
502
503
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700504static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
505 const char *path)
506{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800507#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700508 char *pos;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800509#endif /* !CONFIG_CTRL_IFACE_UDP */
510
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700511 os_free(interfaces->global_iface_path);
512 interfaces->global_iface_path = os_strdup(path);
513 if (interfaces->global_iface_path == NULL)
514 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800515
516#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700517 pos = os_strrchr(interfaces->global_iface_path, '/');
518 if (pos == NULL) {
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700519 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
520 "file");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700521 os_free(interfaces->global_iface_path);
522 interfaces->global_iface_path = NULL;
523 return -1;
524 }
525
526 *pos = '\0';
527 interfaces->global_iface_name = pos + 1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800528#endif /* !CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700529
530 return 0;
531}
532
533
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700534static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
535 const char *group)
536{
537#ifndef CONFIG_NATIVE_WINDOWS
538 struct group *grp;
539 grp = getgrnam(group);
540 if (grp == NULL) {
541 wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
542 return -1;
543 }
544 interfaces->ctrl_iface_group = grp->gr_gid;
545#endif /* CONFIG_NATIVE_WINDOWS */
546 return 0;
547}
548
549
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700550static int hostapd_get_interface_names(char ***if_names,
551 size_t *if_names_size,
552 char *optarg)
553{
554 char *if_name, *tmp, **nnames;
555 size_t i;
556
557 if (!optarg)
558 return -1;
559 if_name = strtok_r(optarg, ",", &tmp);
560
561 while (if_name) {
562 nnames = os_realloc_array(*if_names, 1 + *if_names_size,
563 sizeof(char *));
564 if (!nnames)
565 goto fail;
566 *if_names = nnames;
567
568 (*if_names)[*if_names_size] = os_strdup(if_name);
569 if (!(*if_names)[*if_names_size])
570 goto fail;
571 (*if_names_size)++;
572 if_name = strtok_r(NULL, ",", &tmp);
573 }
574
575 return 0;
576
577fail:
578 for (i = 0; i < *if_names_size; i++)
579 os_free((*if_names)[i]);
580 os_free(*if_names);
581 *if_names = NULL;
582 *if_names_size = 0;
583 return -1;
584}
585
586
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800587#ifdef CONFIG_WPS
588static int gen_uuid(const char *txt_addr)
589{
590 u8 addr[ETH_ALEN];
591 u8 uuid[UUID_LEN];
592 char buf[100];
593
594 if (hwaddr_aton(txt_addr, addr) < 0)
595 return -1;
596
597 uuid_gen_mac_addr(addr, uuid);
598 if (uuid_bin2str(uuid, buf, sizeof(buf)) < 0)
599 return -1;
600
601 printf("%s\n", buf);
602
603 return 0;
604}
605#endif /* CONFIG_WPS */
606
607
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800608#ifndef HOSTAPD_CLEANUP_INTERVAL
609#define HOSTAPD_CLEANUP_INTERVAL 10
610#endif /* HOSTAPD_CLEANUP_INTERVAL */
611
612static int hostapd_periodic_call(struct hostapd_iface *iface, void *ctx)
613{
614 hostapd_periodic_iface(iface);
615 return 0;
616}
617
618
619/* Periodic cleanup tasks */
620static void hostapd_periodic(void *eloop_ctx, void *timeout_ctx)
621{
622 struct hapd_interfaces *interfaces = eloop_ctx;
623
624 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
625 hostapd_periodic, interfaces, NULL);
626 hostapd_for_each_interface(interfaces, hostapd_periodic_call, NULL);
627}
628
629
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700630int main(int argc, char *argv[])
631{
632 struct hapd_interfaces interfaces;
633 int ret = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800634 size_t i, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700635 int c, debug = 0, daemonize = 0;
636 char *pid_file = NULL;
637 const char *log_file = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700638 const char *entropy_file = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800639 char **bss_config = NULL, **tmp_bss;
640 size_t num_bss_configs = 0;
641#ifdef CONFIG_DEBUG_LINUX_TRACING
642 int enable_trace_dbg = 0;
643#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800644 int start_ifaces_in_sync = 0;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700645 char **if_names = NULL;
646 size_t if_names_size = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700647
648 if (os_program_init())
649 return -1;
650
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700651 os_memset(&interfaces, 0, sizeof(interfaces));
652 interfaces.reload_config = hostapd_reload_config;
653 interfaces.config_read_cb = hostapd_config_read;
654 interfaces.for_each_interface = hostapd_for_each_interface;
655 interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
656 interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
657 interfaces.driver_init = hostapd_driver_init;
658 interfaces.global_iface_path = NULL;
659 interfaces.global_iface_name = NULL;
660 interfaces.global_ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800661 dl_list_init(&interfaces.global_ctrl_dst);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700662
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700663 for (;;) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700664 c = getopt(argc, argv, "b:Bde:f:hi:KP:STtu:vg:G:");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700665 if (c < 0)
666 break;
667 switch (c) {
668 case 'h':
669 usage();
670 break;
671 case 'd':
672 debug++;
673 if (wpa_debug_level > 0)
674 wpa_debug_level--;
675 break;
676 case 'B':
677 daemonize++;
678 break;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700679 case 'e':
680 entropy_file = optarg;
681 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700682 case 'f':
683 log_file = optarg;
684 break;
685 case 'K':
686 wpa_debug_show_keys++;
687 break;
688 case 'P':
689 os_free(pid_file);
690 pid_file = os_rel2abs_path(optarg);
691 break;
692 case 't':
693 wpa_debug_timestamp++;
694 break;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800695#ifdef CONFIG_DEBUG_LINUX_TRACING
696 case 'T':
697 enable_trace_dbg = 1;
698 break;
699#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700700 case 'v':
701 show_version();
702 exit(1);
703 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700704 case 'g':
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700705 if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
706 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700707 break;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700708 case 'G':
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700709 if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
710 return -1;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700711 break;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800712 case 'b':
713 tmp_bss = os_realloc_array(bss_config,
714 num_bss_configs + 1,
715 sizeof(char *));
716 if (tmp_bss == NULL)
717 goto out;
718 bss_config = tmp_bss;
719 bss_config[num_bss_configs++] = optarg;
720 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800721 case 'S':
722 start_ifaces_in_sync = 1;
723 break;
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800724#ifdef CONFIG_WPS
725 case 'u':
726 return gen_uuid(optarg);
727#endif /* CONFIG_WPS */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700728 case 'i':
729 if (hostapd_get_interface_names(&if_names,
730 &if_names_size, optarg))
731 goto out;
732 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700733 default:
734 usage();
735 break;
736 }
737 }
738
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800739 if (optind == argc && interfaces.global_iface_path == NULL &&
740 num_bss_configs == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700741 usage();
742
743 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
744
745 if (log_file)
746 wpa_debug_open_file(log_file);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800747 else
748 wpa_debug_setup_stdout();
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800749#ifdef CONFIG_DEBUG_LINUX_TRACING
750 if (enable_trace_dbg) {
751 int tret = wpa_debug_open_linux_tracing();
752 if (tret) {
753 wpa_printf(MSG_ERROR, "Failed to enable trace logging");
754 return -1;
755 }
756 }
757#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700758
759 interfaces.count = argc - optind;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800760 if (interfaces.count || num_bss_configs) {
761 interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700762 sizeof(struct hostapd_iface *));
763 if (interfaces.iface == NULL) {
764 wpa_printf(MSG_ERROR, "malloc failed");
765 return -1;
766 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700767 }
768
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700769 if (hostapd_global_init(&interfaces, entropy_file)) {
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700770 wpa_printf(MSG_ERROR, "Failed to initialize global context");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700771 return -1;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700772 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700773
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800774 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
775 hostapd_periodic, &interfaces, NULL);
776
777 if (fst_global_init()) {
778 wpa_printf(MSG_ERROR,
779 "Failed to initialize global FST context");
780 goto out;
781 }
782
783#if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE)
784 if (!fst_global_add_ctrl(fst_ctrl_cli))
785 wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl");
786#endif /* CONFIG_FST && CONFIG_CTRL_IFACE */
787
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800788 /* Allocate and parse configuration for full interface files */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700789 for (i = 0; i < interfaces.count; i++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700790 char *if_name = NULL;
791
792 if (i < if_names_size)
793 if_name = if_names[i];
794
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700795 interfaces.iface[i] = hostapd_interface_init(&interfaces,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700796 if_name,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700797 argv[optind + i],
798 debug);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700799 if (!interfaces.iface[i]) {
800 wpa_printf(MSG_ERROR, "Failed to initialize interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700801 goto out;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700802 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800803 if (start_ifaces_in_sync)
804 interfaces.iface[i]->need_to_start_in_sync = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700805 }
806
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800807 /* Allocate and parse configuration for per-BSS files */
808 for (i = 0; i < num_bss_configs; i++) {
809 struct hostapd_iface *iface;
810 char *fname;
811
812 wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
813 fname = os_strchr(bss_config[i], ':');
814 if (fname == NULL) {
815 wpa_printf(MSG_ERROR,
816 "Invalid BSS config identifier '%s'",
817 bss_config[i]);
818 goto out;
819 }
820 *fname++ = '\0';
821 iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
822 fname, debug);
823 if (iface == NULL)
824 goto out;
825 for (j = 0; j < interfaces.count; j++) {
826 if (interfaces.iface[j] == iface)
827 break;
828 }
829 if (j == interfaces.count) {
830 struct hostapd_iface **tmp;
831 tmp = os_realloc_array(interfaces.iface,
832 interfaces.count + 1,
833 sizeof(struct hostapd_iface *));
834 if (tmp == NULL) {
835 hostapd_interface_deinit_free(iface);
836 goto out;
837 }
838 interfaces.iface = tmp;
839 interfaces.iface[interfaces.count++] = iface;
840 }
841 }
842
843 /*
844 * Enable configured interfaces. Depending on channel configuration,
845 * this may complete full initialization before returning or use a
846 * callback mechanism to complete setup in case of operations like HT
847 * co-ex scans, ACS, or DFS are needed to determine channel parameters.
848 * In such case, the interface will be enabled from eloop context within
849 * hostapd_global_run().
850 */
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800851 interfaces.terminate_on_error = interfaces.count;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800852 for (i = 0; i < interfaces.count; i++) {
853 if (hostapd_driver_init(interfaces.iface[i]) ||
854 hostapd_setup_interface(interfaces.iface[i]))
855 goto out;
856 }
857
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700858 hostapd_global_ctrl_iface_init(&interfaces);
859
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700860 if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
861 wpa_printf(MSG_ERROR, "Failed to start eloop");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700862 goto out;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700863 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700864
865 ret = 0;
866
867 out:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700868 hostapd_global_ctrl_iface_deinit(&interfaces);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700869 /* Deinitialize all interfaces */
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800870 for (i = 0; i < interfaces.count; i++) {
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700871 if (!interfaces.iface[i])
872 continue;
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800873 interfaces.iface[i]->driver_ap_teardown =
874 !!(interfaces.iface[i]->drv_flags &
875 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700876 hostapd_interface_deinit_free(interfaces.iface[i]);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800877 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700878 os_free(interfaces.iface);
879
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700880 if (interfaces.eloop_initialized)
881 eloop_cancel_timeout(hostapd_periodic, &interfaces, NULL);
882 hostapd_global_deinit(pid_file, interfaces.eloop_initialized);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700883 os_free(pid_file);
884
885 if (log_file)
886 wpa_debug_close_file();
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800887 wpa_debug_close_linux_tracing();
888
889 os_free(bss_config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700890
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700891 for (i = 0; i < if_names_size; i++)
892 os_free(if_names[i]);
893 os_free(if_names);
894
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800895 fst_global_deinit();
896
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700897 os_program_deinit();
898
899 return ret;
900}