blob: e613c0449f2883357f1b65c7876b343707c21ea9 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / main()
Hai Shalom74f70d42019-02-11 14:42:39 -08003 * Copyright (c) 2002-2019, 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"
Roshan Pius3a1667e2018-07-03 15:17:14 -070027#include "ap/dpp_hostapd.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080028#include "fst/fst.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070029#include "config_file.h"
30#include "eap_register.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070031#include "ctrl_iface.h"
Roshan Piuscc817562017-12-22 14:45:05 -080032#ifdef CONFIG_CTRL_IFACE_HIDL
33#include "hidl.h"
34#endif /* CONFIG_CTRL_IFACE_HIDL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070035
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080036struct hapd_global {
37 void **drv_priv;
38 size_t drv_count;
39};
40
41static struct hapd_global global;
42
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070043
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044#ifndef CONFIG_NO_HOSTAPD_LOGGER
45static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
46 int level, const char *txt, size_t len)
47{
48 struct hostapd_data *hapd = ctx;
49 char *format, *module_str;
50 int maxlen;
51 int conf_syslog_level, conf_stdout_level;
52 unsigned int conf_syslog, conf_stdout;
53
54 maxlen = len + 100;
55 format = os_malloc(maxlen);
56 if (!format)
57 return;
58
59 if (hapd && hapd->conf) {
60 conf_syslog_level = hapd->conf->logger_syslog_level;
61 conf_stdout_level = hapd->conf->logger_stdout_level;
62 conf_syslog = hapd->conf->logger_syslog;
63 conf_stdout = hapd->conf->logger_stdout;
64 } else {
65 conf_syslog_level = conf_stdout_level = 0;
66 conf_syslog = conf_stdout = (unsigned int) -1;
67 }
68
69 switch (module) {
70 case HOSTAPD_MODULE_IEEE80211:
71 module_str = "IEEE 802.11";
72 break;
73 case HOSTAPD_MODULE_IEEE8021X:
74 module_str = "IEEE 802.1X";
75 break;
76 case HOSTAPD_MODULE_RADIUS:
77 module_str = "RADIUS";
78 break;
79 case HOSTAPD_MODULE_WPA:
80 module_str = "WPA";
81 break;
82 case HOSTAPD_MODULE_DRIVER:
83 module_str = "DRIVER";
84 break;
85 case HOSTAPD_MODULE_IAPP:
86 module_str = "IAPP";
87 break;
88 case HOSTAPD_MODULE_MLME:
89 module_str = "MLME";
90 break;
91 default:
92 module_str = NULL;
93 break;
94 }
95
96 if (hapd && hapd->conf && addr)
97 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
98 hapd->conf->iface, MAC2STR(addr),
Dmitry Shmidt96be6222014-02-13 10:16:51 -080099 module_str ? " " : "", module_str ? module_str : "",
100 txt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700101 else if (hapd && hapd->conf)
102 os_snprintf(format, maxlen, "%s:%s%s %s",
103 hapd->conf->iface, module_str ? " " : "",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700104 module_str ? module_str : "", txt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700105 else if (addr)
106 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
107 MAC2STR(addr), module_str ? " " : "",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700108 module_str ? module_str : "", txt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109 else
110 os_snprintf(format, maxlen, "%s%s%s",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700111 module_str ? module_str : "",
112 module_str ? ": " : "", txt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700113
Paul Stewart092955c2017-02-06 09:13:09 -0800114#ifdef CONFIG_DEBUG_SYSLOG
115 if (wpa_debug_syslog)
116 conf_stdout = 0;
117#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700118 if ((conf_stdout & module) && level >= conf_stdout_level) {
119 wpa_debug_print_timestamp();
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800120 wpa_printf(MSG_INFO, "%s", format);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700121 }
122
123#ifndef CONFIG_NATIVE_WINDOWS
124 if ((conf_syslog & module) && level >= conf_syslog_level) {
125 int priority;
126 switch (level) {
127 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
128 case HOSTAPD_LEVEL_DEBUG:
129 priority = LOG_DEBUG;
130 break;
131 case HOSTAPD_LEVEL_INFO:
132 priority = LOG_INFO;
133 break;
134 case HOSTAPD_LEVEL_NOTICE:
135 priority = LOG_NOTICE;
136 break;
137 case HOSTAPD_LEVEL_WARNING:
138 priority = LOG_WARNING;
139 break;
140 default:
141 priority = LOG_INFO;
142 break;
143 }
144 syslog(priority, "%s", format);
145 }
146#endif /* CONFIG_NATIVE_WINDOWS */
147
148 os_free(format);
149}
150#endif /* CONFIG_NO_HOSTAPD_LOGGER */
151
152
153/**
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800154 * hostapd_driver_init - Preparate driver interface
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700155 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700156static int hostapd_driver_init(struct hostapd_iface *iface)
157{
158 struct wpa_init_params params;
159 size_t i;
160 struct hostapd_data *hapd = iface->bss[0];
161 struct hostapd_bss_config *conf = hapd->conf;
162 u8 *b = conf->bssid;
163 struct wpa_driver_capa capa;
164
165 if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
166 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
167 return -1;
168 }
169
170 /* Initialize the driver interface */
171 if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
172 b = NULL;
173
174 os_memset(&params, 0, sizeof(params));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800175 for (i = 0; wpa_drivers[i]; i++) {
176 if (wpa_drivers[i] != hapd->driver)
177 continue;
178
179 if (global.drv_priv[i] == NULL &&
180 wpa_drivers[i]->global_init) {
Dmitry Shmidte4663042016-04-04 10:07:49 -0700181 global.drv_priv[i] =
182 wpa_drivers[i]->global_init(iface->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800183 if (global.drv_priv[i] == NULL) {
184 wpa_printf(MSG_ERROR, "Failed to initialize "
185 "driver '%s'",
186 wpa_drivers[i]->name);
187 return -1;
188 }
189 }
190
191 params.global_priv = global.drv_priv[i];
192 break;
193 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194 params.bssid = b;
195 params.ifname = hapd->conf->iface;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800196 params.driver_params = hapd->iconf->driver_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700197 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
198
199 params.num_bridge = hapd->iface->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700200 params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700201 if (params.bridge == NULL)
202 return -1;
203 for (i = 0; i < hapd->iface->num_bss; i++) {
204 struct hostapd_data *bss = hapd->iface->bss[i];
205 if (bss->conf->bridge[0])
206 params.bridge[i] = bss->conf->bridge;
207 }
208
209 params.own_addr = hapd->own_addr;
210
211 hapd->drv_priv = hapd->driver->hapd_init(hapd, &params);
212 os_free(params.bridge);
213 if (hapd->drv_priv == NULL) {
214 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
215 hapd->driver->name);
216 hapd->driver = NULL;
217 return -1;
218 }
219
220 if (hapd->driver->get_capa &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800221 hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
Dmitry Shmidt0207e232014-09-03 14:58:37 -0700222 struct wowlan_triggers *triggs;
223
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700224 iface->drv_flags = capa.flags;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800225 iface->smps_modes = capa.smps_modes;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800226 iface->probe_resp_offloads = capa.probe_resp_offloads;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700227 /*
228 * Use default extended capa values from per-radio information
229 */
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700230 iface->extended_capa = capa.extended_capa;
231 iface->extended_capa_mask = capa.extended_capa_mask;
232 iface->extended_capa_len = capa.extended_capa_len;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700233 iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
Dmitry Shmidt0207e232014-09-03 14:58:37 -0700234
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700235 /*
236 * Override extended capa with per-interface type (AP), if
237 * available from the driver.
238 */
239 hostapd_get_ext_capa(iface);
240
Dmitry Shmidt0207e232014-09-03 14:58:37 -0700241 triggs = wpa_get_wowlan_triggers(conf->wowlan_triggers, &capa);
242 if (triggs && hapd->driver->set_wowlan) {
243 if (hapd->driver->set_wowlan(hapd->drv_priv, triggs))
244 wpa_printf(MSG_ERROR, "set_wowlan failed");
245 }
246 os_free(triggs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800247 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248
249 return 0;
250}
251
252
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800253/**
254 * hostapd_interface_init - Read configuration file and init BSS data
255 *
256 * This function is used to parse configuration file for a full interface (one
257 * or more BSSes sharing the same radio) and allocate memory for the BSS
Hai Shalom74f70d42019-02-11 14:42:39 -0800258 * interfaces. No actual driver operations are started.
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800259 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700260static struct hostapd_iface *
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700261hostapd_interface_init(struct hapd_interfaces *interfaces, const char *if_name,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700262 const char *config_fname, int debug)
263{
264 struct hostapd_iface *iface;
265 int k;
266
267 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800268 iface = hostapd_init(interfaces, config_fname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700269 if (!iface)
270 return NULL;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700271
272 if (if_name) {
273 os_strlcpy(iface->conf->bss[0]->iface, if_name,
274 sizeof(iface->conf->bss[0]->iface));
275 }
276
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700277 iface->interfaces = interfaces;
278
279 for (k = 0; k < debug; k++) {
280 if (iface->bss[0]->conf->logger_stdout_level > 0)
281 iface->bss[0]->conf->logger_stdout_level--;
282 }
283
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800284 if (iface->conf->bss[0]->iface[0] == '\0' &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700285 !hostapd_drv_none(iface->bss[0])) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700286 wpa_printf(MSG_ERROR,
287 "Interface name not specified in %s, nor by '-i' parameter",
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700288 config_fname);
289 hostapd_interface_deinit_free(iface);
290 return NULL;
291 }
292
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293 return iface;
294}
295
296
297/**
298 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
299 */
300static void handle_term(int sig, void *signal_ctx)
301{
302 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
303 eloop_terminate();
304}
305
306
307#ifndef CONFIG_NATIVE_WINDOWS
308
309static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
310{
311 if (hostapd_reload_config(iface) < 0) {
312 wpa_printf(MSG_WARNING, "Failed to read new configuration "
313 "file - continuing with old.");
314 }
315 return 0;
316}
317
318
319/**
320 * handle_reload - SIGHUP handler to reload configuration
321 */
322static void handle_reload(int sig, void *signal_ctx)
323{
324 struct hapd_interfaces *interfaces = signal_ctx;
325 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
326 sig);
327 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
328}
329
330
331static void handle_dump_state(int sig, void *signal_ctx)
332{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800333 /* Not used anymore - ignore signal */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700334}
335#endif /* CONFIG_NATIVE_WINDOWS */
336
337
Jouni Malinen75ecf522011-06-27 15:19:46 -0700338static int hostapd_global_init(struct hapd_interfaces *interfaces,
339 const char *entropy_file)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700340{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800341 int i;
342
343 os_memset(&global, 0, sizeof(global));
344
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700345 hostapd_logger_register_cb(hostapd_logger_cb);
346
347 if (eap_server_register_methods()) {
348 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
349 return -1;
350 }
351
352 if (eloop_init()) {
353 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
354 return -1;
355 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700356 interfaces->eloop_initialized = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357
Jouni Malinen75ecf522011-06-27 15:19:46 -0700358 random_init(entropy_file);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700359
360#ifndef CONFIG_NATIVE_WINDOWS
361 eloop_register_signal(SIGHUP, handle_reload, interfaces);
362 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
363#endif /* CONFIG_NATIVE_WINDOWS */
364 eloop_register_signal_terminate(handle_term, interfaces);
365
366#ifndef CONFIG_NATIVE_WINDOWS
367 openlog("hostapd", 0, LOG_DAEMON);
368#endif /* CONFIG_NATIVE_WINDOWS */
369
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800370 for (i = 0; wpa_drivers[i]; i++)
371 global.drv_count++;
372 if (global.drv_count == 0) {
373 wpa_printf(MSG_ERROR, "No drivers enabled");
374 return -1;
375 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700376 global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800377 if (global.drv_priv == NULL)
378 return -1;
379
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700380 return 0;
381}
382
383
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700384static void hostapd_global_deinit(const char *pid_file, int eloop_initialized)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800386 int i;
387
388 for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
389 if (!global.drv_priv[i])
390 continue;
391 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
392 }
393 os_free(global.drv_priv);
394 global.drv_priv = NULL;
395
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700396#ifdef EAP_SERVER_TNC
397 tncs_global_deinit();
398#endif /* EAP_SERVER_TNC */
399
400 random_deinit();
401
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700402 if (eloop_initialized)
403 eloop_destroy();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700404
405#ifndef CONFIG_NATIVE_WINDOWS
406 closelog();
407#endif /* CONFIG_NATIVE_WINDOWS */
408
409 eap_server_unregister_methods();
410
411 os_daemonize_terminate(pid_file);
412}
413
414
415static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
416 const char *pid_file)
417{
418#ifdef EAP_SERVER_TNC
419 int tnc = 0;
420 size_t i, k;
421
422 for (i = 0; !tnc && i < ifaces->count; i++) {
423 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
424 if (ifaces->iface[i]->bss[0]->conf->tnc) {
425 tnc++;
426 break;
427 }
428 }
429 }
430
431 if (tnc && tncs_global_init() < 0) {
432 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
433 return -1;
434 }
435#endif /* EAP_SERVER_TNC */
436
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800437 if (daemonize) {
438 if (os_daemonize(pid_file)) {
439 wpa_printf(MSG_ERROR, "daemon: %s", strerror(errno));
440 return -1;
441 }
442 if (eloop_sock_requeue()) {
443 wpa_printf(MSG_ERROR, "eloop_sock_requeue: %s",
444 strerror(errno));
445 return -1;
446 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700447 }
448
449 eloop_run();
450
451 return 0;
452}
453
454
455static void show_version(void)
456{
457 fprintf(stderr,
458 "hostapd v" VERSION_STR "\n"
459 "User space daemon for IEEE 802.11 AP management,\n"
460 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
Hai Shalom74f70d42019-02-11 14:42:39 -0800461 "Copyright (c) 2002-2019, Jouni Malinen <j@w1.fi> "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700462 "and contributors\n");
463}
464
465
466static void usage(void)
467{
468 show_version();
469 fprintf(stderr,
470 "\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -0700471 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700472 "\\\n"
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700473 " [-g <global ctrl_iface>] [-G <group>]\\\n"
474 " [-i <comma-separated list of interface names>]\\\n"
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700475 " <configuration file(s)>\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700476 "\n"
477 "options:\n"
478 " -h show this usage\n"
479 " -d show more debug messages (-dd for even more)\n"
480 " -B run daemon in the background\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -0700481 " -e entropy file\n"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700482 " -g global control interface path\n"
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700483 " -G group for control interfaces\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700484 " -P PID file\n"
485 " -K include key data in debug messages\n"
486#ifdef CONFIG_DEBUG_FILE
487 " -f log output to debug file instead of stdout\n"
488#endif /* CONFIG_DEBUG_FILE */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800489#ifdef CONFIG_DEBUG_LINUX_TRACING
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800490 " -T record to Linux tracing in addition to logging\n"
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800491 " (records all messages regardless of debug verbosity)\n"
492#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700493 " -i list of interface names to use\n"
Paul Stewart092955c2017-02-06 09:13:09 -0800494#ifdef CONFIG_DEBUG_SYSLOG
495 " -s log output to syslog instead of stdout\n"
496#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800497 " -S start all the interfaces synchronously\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700498 " -t include timestamps in some debug messages\n"
499 " -v show hostapd version\n");
500
501 exit(1);
502}
503
504
505static const char * hostapd_msg_ifname_cb(void *ctx)
506{
507 struct hostapd_data *hapd = ctx;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800508 if (hapd && hapd->conf)
509 return hapd->conf->iface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700510 return NULL;
511}
512
513
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700514static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
515 const char *path)
516{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800517#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700518 char *pos;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800519#endif /* !CONFIG_CTRL_IFACE_UDP */
520
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700521 os_free(interfaces->global_iface_path);
522 interfaces->global_iface_path = os_strdup(path);
523 if (interfaces->global_iface_path == NULL)
524 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800525
526#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700527 pos = os_strrchr(interfaces->global_iface_path, '/');
528 if (pos == NULL) {
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700529 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
530 "file");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700531 os_free(interfaces->global_iface_path);
532 interfaces->global_iface_path = NULL;
533 return -1;
534 }
535
536 *pos = '\0';
537 interfaces->global_iface_name = pos + 1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800538#endif /* !CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700539
540 return 0;
541}
542
543
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700544static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
545 const char *group)
546{
547#ifndef CONFIG_NATIVE_WINDOWS
548 struct group *grp;
549 grp = getgrnam(group);
550 if (grp == NULL) {
551 wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
552 return -1;
553 }
554 interfaces->ctrl_iface_group = grp->gr_gid;
555#endif /* CONFIG_NATIVE_WINDOWS */
556 return 0;
557}
558
559
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700560static int hostapd_get_interface_names(char ***if_names,
561 size_t *if_names_size,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800562 char *arg)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700563{
564 char *if_name, *tmp, **nnames;
565 size_t i;
566
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800567 if (!arg)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700568 return -1;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800569 if_name = strtok_r(arg, ",", &tmp);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700570
571 while (if_name) {
572 nnames = os_realloc_array(*if_names, 1 + *if_names_size,
573 sizeof(char *));
574 if (!nnames)
575 goto fail;
576 *if_names = nnames;
577
578 (*if_names)[*if_names_size] = os_strdup(if_name);
579 if (!(*if_names)[*if_names_size])
580 goto fail;
581 (*if_names_size)++;
582 if_name = strtok_r(NULL, ",", &tmp);
583 }
584
585 return 0;
586
587fail:
588 for (i = 0; i < *if_names_size; i++)
589 os_free((*if_names)[i]);
590 os_free(*if_names);
591 *if_names = NULL;
592 *if_names_size = 0;
593 return -1;
594}
595
596
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800597#ifdef CONFIG_WPS
598static int gen_uuid(const char *txt_addr)
599{
600 u8 addr[ETH_ALEN];
601 u8 uuid[UUID_LEN];
602 char buf[100];
603
604 if (hwaddr_aton(txt_addr, addr) < 0)
605 return -1;
606
607 uuid_gen_mac_addr(addr, uuid);
608 if (uuid_bin2str(uuid, buf, sizeof(buf)) < 0)
609 return -1;
610
611 printf("%s\n", buf);
612
613 return 0;
614}
615#endif /* CONFIG_WPS */
616
617
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800618#ifndef HOSTAPD_CLEANUP_INTERVAL
619#define HOSTAPD_CLEANUP_INTERVAL 10
620#endif /* HOSTAPD_CLEANUP_INTERVAL */
621
622static int hostapd_periodic_call(struct hostapd_iface *iface, void *ctx)
623{
624 hostapd_periodic_iface(iface);
625 return 0;
626}
627
628
629/* Periodic cleanup tasks */
630static void hostapd_periodic(void *eloop_ctx, void *timeout_ctx)
631{
632 struct hapd_interfaces *interfaces = eloop_ctx;
633
634 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
635 hostapd_periodic, interfaces, NULL);
636 hostapd_for_each_interface(interfaces, hostapd_periodic_call, NULL);
637}
638
639
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700640int main(int argc, char *argv[])
641{
642 struct hapd_interfaces interfaces;
643 int ret = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800644 size_t i, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700645 int c, debug = 0, daemonize = 0;
646 char *pid_file = NULL;
647 const char *log_file = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700648 const char *entropy_file = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800649 char **bss_config = NULL, **tmp_bss;
650 size_t num_bss_configs = 0;
651#ifdef CONFIG_DEBUG_LINUX_TRACING
652 int enable_trace_dbg = 0;
653#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800654 int start_ifaces_in_sync = 0;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700655 char **if_names = NULL;
656 size_t if_names_size = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700657
658 if (os_program_init())
659 return -1;
660
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700661 os_memset(&interfaces, 0, sizeof(interfaces));
662 interfaces.reload_config = hostapd_reload_config;
663 interfaces.config_read_cb = hostapd_config_read;
664 interfaces.for_each_interface = hostapd_for_each_interface;
665 interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
666 interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
667 interfaces.driver_init = hostapd_driver_init;
668 interfaces.global_iface_path = NULL;
669 interfaces.global_iface_name = NULL;
670 interfaces.global_ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800671 dl_list_init(&interfaces.global_ctrl_dst);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700672#ifdef CONFIG_ETH_P_OUI
673 dl_list_init(&interfaces.eth_p_oui);
674#endif /* CONFIG_ETH_P_OUI */
Roshan Pius3a1667e2018-07-03 15:17:14 -0700675#ifdef CONFIG_DPP
676 hostapd_dpp_init_global(&interfaces);
677#endif /* CONFIG_DPP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700678
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700679 for (;;) {
Paul Stewart092955c2017-02-06 09:13:09 -0800680 c = getopt(argc, argv, "b:Bde:f:hi:KP:sSTtu:vg:G:");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700681 if (c < 0)
682 break;
683 switch (c) {
684 case 'h':
685 usage();
686 break;
687 case 'd':
688 debug++;
689 if (wpa_debug_level > 0)
690 wpa_debug_level--;
691 break;
692 case 'B':
693 daemonize++;
694 break;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700695 case 'e':
696 entropy_file = optarg;
697 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700698 case 'f':
699 log_file = optarg;
700 break;
701 case 'K':
702 wpa_debug_show_keys++;
703 break;
704 case 'P':
705 os_free(pid_file);
706 pid_file = os_rel2abs_path(optarg);
707 break;
708 case 't':
709 wpa_debug_timestamp++;
710 break;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800711#ifdef CONFIG_DEBUG_LINUX_TRACING
712 case 'T':
713 enable_trace_dbg = 1;
714 break;
715#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700716 case 'v':
717 show_version();
718 exit(1);
719 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700720 case 'g':
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700721 if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
722 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700723 break;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700724 case 'G':
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700725 if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
726 return -1;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700727 break;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800728 case 'b':
729 tmp_bss = os_realloc_array(bss_config,
730 num_bss_configs + 1,
731 sizeof(char *));
732 if (tmp_bss == NULL)
733 goto out;
734 bss_config = tmp_bss;
735 bss_config[num_bss_configs++] = optarg;
736 break;
Paul Stewart092955c2017-02-06 09:13:09 -0800737#ifdef CONFIG_DEBUG_SYSLOG
738 case 's':
739 wpa_debug_syslog = 1;
740 break;
741#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800742 case 'S':
743 start_ifaces_in_sync = 1;
744 break;
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800745#ifdef CONFIG_WPS
746 case 'u':
747 return gen_uuid(optarg);
748#endif /* CONFIG_WPS */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700749 case 'i':
750 if (hostapd_get_interface_names(&if_names,
751 &if_names_size, optarg))
752 goto out;
753 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700754 default:
755 usage();
756 break;
757 }
758 }
759
Roshan Piuscc817562017-12-22 14:45:05 -0800760#ifndef CONFIG_CTRL_IFACE_HIDL
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800761 if (optind == argc && interfaces.global_iface_path == NULL &&
762 num_bss_configs == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700763 usage();
Roshan Piuscc817562017-12-22 14:45:05 -0800764#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700765
766 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
767
768 if (log_file)
769 wpa_debug_open_file(log_file);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800770 else
771 wpa_debug_setup_stdout();
Paul Stewart092955c2017-02-06 09:13:09 -0800772#ifdef CONFIG_DEBUG_SYSLOG
773 if (wpa_debug_syslog)
774 wpa_debug_open_syslog();
775#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800776#ifdef CONFIG_DEBUG_LINUX_TRACING
777 if (enable_trace_dbg) {
778 int tret = wpa_debug_open_linux_tracing();
779 if (tret) {
780 wpa_printf(MSG_ERROR, "Failed to enable trace logging");
781 return -1;
782 }
783 }
784#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700785
786 interfaces.count = argc - optind;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800787 if (interfaces.count || num_bss_configs) {
788 interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700789 sizeof(struct hostapd_iface *));
790 if (interfaces.iface == NULL) {
791 wpa_printf(MSG_ERROR, "malloc failed");
792 return -1;
793 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700794 }
795
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700796 if (hostapd_global_init(&interfaces, entropy_file)) {
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700797 wpa_printf(MSG_ERROR, "Failed to initialize global context");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700798 return -1;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700799 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700800
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800801 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
802 hostapd_periodic, &interfaces, NULL);
803
804 if (fst_global_init()) {
805 wpa_printf(MSG_ERROR,
806 "Failed to initialize global FST context");
807 goto out;
808 }
809
810#if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE)
811 if (!fst_global_add_ctrl(fst_ctrl_cli))
812 wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl");
813#endif /* CONFIG_FST && CONFIG_CTRL_IFACE */
814
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800815 /* Allocate and parse configuration for full interface files */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700816 for (i = 0; i < interfaces.count; i++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700817 char *if_name = NULL;
818
819 if (i < if_names_size)
820 if_name = if_names[i];
821
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700822 interfaces.iface[i] = hostapd_interface_init(&interfaces,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700823 if_name,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700824 argv[optind + i],
825 debug);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700826 if (!interfaces.iface[i]) {
827 wpa_printf(MSG_ERROR, "Failed to initialize interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700828 goto out;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700829 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800830 if (start_ifaces_in_sync)
831 interfaces.iface[i]->need_to_start_in_sync = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700832 }
833
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800834 /* Allocate and parse configuration for per-BSS files */
835 for (i = 0; i < num_bss_configs; i++) {
836 struct hostapd_iface *iface;
837 char *fname;
838
839 wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
840 fname = os_strchr(bss_config[i], ':');
841 if (fname == NULL) {
842 wpa_printf(MSG_ERROR,
843 "Invalid BSS config identifier '%s'",
844 bss_config[i]);
845 goto out;
846 }
847 *fname++ = '\0';
848 iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
849 fname, debug);
850 if (iface == NULL)
851 goto out;
852 for (j = 0; j < interfaces.count; j++) {
853 if (interfaces.iface[j] == iface)
854 break;
855 }
856 if (j == interfaces.count) {
857 struct hostapd_iface **tmp;
858 tmp = os_realloc_array(interfaces.iface,
859 interfaces.count + 1,
860 sizeof(struct hostapd_iface *));
861 if (tmp == NULL) {
862 hostapd_interface_deinit_free(iface);
863 goto out;
864 }
865 interfaces.iface = tmp;
866 interfaces.iface[interfaces.count++] = iface;
867 }
868 }
869
870 /*
871 * Enable configured interfaces. Depending on channel configuration,
872 * this may complete full initialization before returning or use a
873 * callback mechanism to complete setup in case of operations like HT
874 * co-ex scans, ACS, or DFS are needed to determine channel parameters.
875 * In such case, the interface will be enabled from eloop context within
876 * hostapd_global_run().
877 */
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800878 interfaces.terminate_on_error = interfaces.count;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800879 for (i = 0; i < interfaces.count; i++) {
Hai Shalom74f70d42019-02-11 14:42:39 -0800880 if (hostapd_driver_init(interfaces.iface[i]) ||
881 hostapd_setup_interface(interfaces.iface[i]))
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800882 goto out;
883 }
884
Roshan Piuscc817562017-12-22 14:45:05 -0800885#ifdef CONFIG_CTRL_IFACE_HIDL
886 if (hostapd_hidl_init(&interfaces)) {
887 wpa_printf(MSG_ERROR, "Failed to initialize HIDL interface");
888 goto out;
889 }
Roshan Piuscc817562017-12-22 14:45:05 -0800890#endif /* CONFIG_CTRL_IFACE_HIDL */
Daichi Ueuracaa74a82018-02-14 22:25:39 +0900891 hostapd_global_ctrl_iface_init(&interfaces);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700892
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700893 if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
894 wpa_printf(MSG_ERROR, "Failed to start eloop");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700895 goto out;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700896 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700897
898 ret = 0;
899
900 out:
Roshan Piuscc817562017-12-22 14:45:05 -0800901#ifdef CONFIG_CTRL_IFACE_HIDL
902 hostapd_hidl_deinit(&interfaces);
Roshan Piuscc817562017-12-22 14:45:05 -0800903#endif /* CONFIG_CTRL_IFACE_HIDL */
Daichi Ueuracaa74a82018-02-14 22:25:39 +0900904 hostapd_global_ctrl_iface_deinit(&interfaces);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700905 /* Deinitialize all interfaces */
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800906 for (i = 0; i < interfaces.count; i++) {
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700907 if (!interfaces.iface[i])
908 continue;
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800909 interfaces.iface[i]->driver_ap_teardown =
910 !!(interfaces.iface[i]->drv_flags &
911 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912 hostapd_interface_deinit_free(interfaces.iface[i]);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800913 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700914 os_free(interfaces.iface);
915
Roshan Pius3a1667e2018-07-03 15:17:14 -0700916#ifdef CONFIG_DPP
917 hostapd_dpp_deinit_global(&interfaces);
918#endif /* CONFIG_DPP */
919
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700920 if (interfaces.eloop_initialized)
921 eloop_cancel_timeout(hostapd_periodic, &interfaces, NULL);
922 hostapd_global_deinit(pid_file, interfaces.eloop_initialized);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700923 os_free(pid_file);
924
Paul Stewart092955c2017-02-06 09:13:09 -0800925 wpa_debug_close_syslog();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926 if (log_file)
927 wpa_debug_close_file();
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800928 wpa_debug_close_linux_tracing();
929
930 os_free(bss_config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700931
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700932 for (i = 0; i < if_names_size; i++)
933 os_free(if_names[i]);
934 os_free(if_names);
935
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800936 fst_global_deinit();
937
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700938 os_program_deinit();
939
940 return ret;
941}