blob: 85c1fd1c6a2374795633fe93f0edf171fa4f4313 [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"
Hai Shalom021b0b52019-04-10 11:17:58 -070021#include "common/dpp.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include "drivers/driver.h"
23#include "eap_server/eap.h"
24#include "eap_server/tncs.h"
25#include "ap/hostapd.h"
26#include "ap/ap_config.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070027#include "ap/ap_drv_ops.h"
Roshan Pius3a1667e2018-07-03 15:17:14 -070028#include "ap/dpp_hostapd.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080029#include "fst/fst.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030#include "config_file.h"
31#include "eap_register.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070032#include "ctrl_iface.h"
Roshan Piuscc817562017-12-22 14:45:05 -080033#ifdef CONFIG_CTRL_IFACE_HIDL
34#include "hidl.h"
35#endif /* CONFIG_CTRL_IFACE_HIDL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080037struct hapd_global {
38 void **drv_priv;
39 size_t drv_count;
40};
41
42static struct hapd_global global;
43
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045#ifndef CONFIG_NO_HOSTAPD_LOGGER
46static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
47 int level, const char *txt, size_t len)
48{
49 struct hostapd_data *hapd = ctx;
50 char *format, *module_str;
51 int maxlen;
52 int conf_syslog_level, conf_stdout_level;
53 unsigned int conf_syslog, conf_stdout;
54
55 maxlen = len + 100;
56 format = os_malloc(maxlen);
57 if (!format)
58 return;
59
60 if (hapd && hapd->conf) {
61 conf_syslog_level = hapd->conf->logger_syslog_level;
62 conf_stdout_level = hapd->conf->logger_stdout_level;
63 conf_syslog = hapd->conf->logger_syslog;
64 conf_stdout = hapd->conf->logger_stdout;
65 } else {
66 conf_syslog_level = conf_stdout_level = 0;
67 conf_syslog = conf_stdout = (unsigned int) -1;
68 }
69
70 switch (module) {
71 case HOSTAPD_MODULE_IEEE80211:
72 module_str = "IEEE 802.11";
73 break;
74 case HOSTAPD_MODULE_IEEE8021X:
75 module_str = "IEEE 802.1X";
76 break;
77 case HOSTAPD_MODULE_RADIUS:
78 module_str = "RADIUS";
79 break;
80 case HOSTAPD_MODULE_WPA:
81 module_str = "WPA";
82 break;
83 case HOSTAPD_MODULE_DRIVER:
84 module_str = "DRIVER";
85 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070086 case HOSTAPD_MODULE_MLME:
87 module_str = "MLME";
88 break;
89 default:
90 module_str = NULL;
91 break;
92 }
93
94 if (hapd && hapd->conf && addr)
95 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
96 hapd->conf->iface, MAC2STR(addr),
Dmitry Shmidt96be6222014-02-13 10:16:51 -080097 module_str ? " " : "", module_str ? module_str : "",
98 txt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070099 else if (hapd && hapd->conf)
100 os_snprintf(format, maxlen, "%s:%s%s %s",
101 hapd->conf->iface, module_str ? " " : "",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700102 module_str ? module_str : "", txt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700103 else if (addr)
104 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
105 MAC2STR(addr), module_str ? " " : "",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700106 module_str ? module_str : "", txt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700107 else
108 os_snprintf(format, maxlen, "%s%s%s",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700109 module_str ? module_str : "",
110 module_str ? ": " : "", txt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111
Paul Stewart092955c2017-02-06 09:13:09 -0800112#ifdef CONFIG_DEBUG_SYSLOG
113 if (wpa_debug_syslog)
114 conf_stdout = 0;
115#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700116 if ((conf_stdout & module) && level >= conf_stdout_level) {
117 wpa_debug_print_timestamp();
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800118 wpa_printf(MSG_INFO, "%s", format);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700119 }
120
121#ifndef CONFIG_NATIVE_WINDOWS
122 if ((conf_syslog & module) && level >= conf_syslog_level) {
123 int priority;
124 switch (level) {
125 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
126 case HOSTAPD_LEVEL_DEBUG:
127 priority = LOG_DEBUG;
128 break;
129 case HOSTAPD_LEVEL_INFO:
130 priority = LOG_INFO;
131 break;
132 case HOSTAPD_LEVEL_NOTICE:
133 priority = LOG_NOTICE;
134 break;
135 case HOSTAPD_LEVEL_WARNING:
136 priority = LOG_WARNING;
137 break;
138 default:
139 priority = LOG_INFO;
140 break;
141 }
142 syslog(priority, "%s", format);
143 }
144#endif /* CONFIG_NATIVE_WINDOWS */
145
146 os_free(format);
147}
148#endif /* CONFIG_NO_HOSTAPD_LOGGER */
149
150
151/**
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800152 * hostapd_driver_init - Preparate driver interface
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700154static int hostapd_driver_init(struct hostapd_iface *iface)
155{
156 struct wpa_init_params params;
157 size_t i;
158 struct hostapd_data *hapd = iface->bss[0];
159 struct hostapd_bss_config *conf = hapd->conf;
160 u8 *b = conf->bssid;
161 struct wpa_driver_capa capa;
162
163 if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
164 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
165 return -1;
166 }
167
168 /* Initialize the driver interface */
169 if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
170 b = NULL;
171
172 os_memset(&params, 0, sizeof(params));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800173 for (i = 0; wpa_drivers[i]; i++) {
174 if (wpa_drivers[i] != hapd->driver)
175 continue;
176
177 if (global.drv_priv[i] == NULL &&
178 wpa_drivers[i]->global_init) {
Dmitry Shmidte4663042016-04-04 10:07:49 -0700179 global.drv_priv[i] =
180 wpa_drivers[i]->global_init(iface->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800181 if (global.drv_priv[i] == NULL) {
182 wpa_printf(MSG_ERROR, "Failed to initialize "
183 "driver '%s'",
184 wpa_drivers[i]->name);
185 return -1;
186 }
187 }
188
189 params.global_priv = global.drv_priv[i];
190 break;
191 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700192 params.bssid = b;
193 params.ifname = hapd->conf->iface;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800194 params.driver_params = hapd->iconf->driver_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700195 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
196
197 params.num_bridge = hapd->iface->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700198 params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700199 if (params.bridge == NULL)
200 return -1;
201 for (i = 0; i < hapd->iface->num_bss; i++) {
202 struct hostapd_data *bss = hapd->iface->bss[i];
203 if (bss->conf->bridge[0])
204 params.bridge[i] = bss->conf->bridge;
205 }
206
207 params.own_addr = hapd->own_addr;
208
209 hapd->drv_priv = hapd->driver->hapd_init(hapd, &params);
210 os_free(params.bridge);
211 if (hapd->drv_priv == NULL) {
212 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
213 hapd->driver->name);
214 hapd->driver = NULL;
215 return -1;
216 }
217
218 if (hapd->driver->get_capa &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800219 hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
Dmitry Shmidt0207e232014-09-03 14:58:37 -0700220 struct wowlan_triggers *triggs;
221
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222 iface->drv_flags = capa.flags;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800223 iface->probe_resp_offloads = capa.probe_resp_offloads;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700224 /*
225 * Use default extended capa values from per-radio information
226 */
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700227 iface->extended_capa = capa.extended_capa;
228 iface->extended_capa_mask = capa.extended_capa_mask;
229 iface->extended_capa_len = capa.extended_capa_len;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700230 iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
Dmitry Shmidt0207e232014-09-03 14:58:37 -0700231
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700232 /*
233 * Override extended capa with per-interface type (AP), if
234 * available from the driver.
235 */
236 hostapd_get_ext_capa(iface);
237
Dmitry Shmidt0207e232014-09-03 14:58:37 -0700238 triggs = wpa_get_wowlan_triggers(conf->wowlan_triggers, &capa);
239 if (triggs && hapd->driver->set_wowlan) {
240 if (hapd->driver->set_wowlan(hapd->drv_priv, triggs))
241 wpa_printf(MSG_ERROR, "set_wowlan failed");
242 }
243 os_free(triggs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800244 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245
246 return 0;
247}
248
249
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800250/**
251 * hostapd_interface_init - Read configuration file and init BSS data
252 *
253 * This function is used to parse configuration file for a full interface (one
254 * or more BSSes sharing the same radio) and allocate memory for the BSS
Hai Shalom74f70d42019-02-11 14:42:39 -0800255 * interfaces. No actual driver operations are started.
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800256 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700257static struct hostapd_iface *
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700258hostapd_interface_init(struct hapd_interfaces *interfaces, const char *if_name,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700259 const char *config_fname, int debug)
260{
261 struct hostapd_iface *iface;
262 int k;
263
Hai Shalomfdcde762020-04-02 11:19:20 -0700264 wpa_printf(MSG_DEBUG, "Configuration file: %s", config_fname);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800265 iface = hostapd_init(interfaces, config_fname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700266 if (!iface)
267 return NULL;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700268
269 if (if_name) {
270 os_strlcpy(iface->conf->bss[0]->iface, if_name,
271 sizeof(iface->conf->bss[0]->iface));
272 }
273
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274 iface->interfaces = interfaces;
275
276 for (k = 0; k < debug; k++) {
277 if (iface->bss[0]->conf->logger_stdout_level > 0)
278 iface->bss[0]->conf->logger_stdout_level--;
279 }
280
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800281 if (iface->conf->bss[0]->iface[0] == '\0' &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700282 !hostapd_drv_none(iface->bss[0])) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700283 wpa_printf(MSG_ERROR,
284 "Interface name not specified in %s, nor by '-i' parameter",
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700285 config_fname);
286 hostapd_interface_deinit_free(iface);
287 return NULL;
288 }
289
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700290 return iface;
291}
292
293
294/**
295 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
296 */
297static void handle_term(int sig, void *signal_ctx)
298{
299 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
300 eloop_terminate();
301}
302
303
304#ifndef CONFIG_NATIVE_WINDOWS
305
306static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
307{
308 if (hostapd_reload_config(iface) < 0) {
309 wpa_printf(MSG_WARNING, "Failed to read new configuration "
310 "file - continuing with old.");
311 }
312 return 0;
313}
314
315
316/**
317 * handle_reload - SIGHUP handler to reload configuration
318 */
319static void handle_reload(int sig, void *signal_ctx)
320{
321 struct hapd_interfaces *interfaces = signal_ctx;
322 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
323 sig);
324 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
325}
326
327
328static void handle_dump_state(int sig, void *signal_ctx)
329{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800330 /* Not used anymore - ignore signal */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700331}
332#endif /* CONFIG_NATIVE_WINDOWS */
333
334
Jouni Malinen75ecf522011-06-27 15:19:46 -0700335static int hostapd_global_init(struct hapd_interfaces *interfaces,
336 const char *entropy_file)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700337{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800338 int i;
339
340 os_memset(&global, 0, sizeof(global));
341
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700342 hostapd_logger_register_cb(hostapd_logger_cb);
343
344 if (eap_server_register_methods()) {
345 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
346 return -1;
347 }
348
349 if (eloop_init()) {
350 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
351 return -1;
352 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700353 interfaces->eloop_initialized = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354
Jouni Malinen75ecf522011-06-27 15:19:46 -0700355 random_init(entropy_file);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700356
357#ifndef CONFIG_NATIVE_WINDOWS
358 eloop_register_signal(SIGHUP, handle_reload, interfaces);
359 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
360#endif /* CONFIG_NATIVE_WINDOWS */
361 eloop_register_signal_terminate(handle_term, interfaces);
362
363#ifndef CONFIG_NATIVE_WINDOWS
364 openlog("hostapd", 0, LOG_DAEMON);
365#endif /* CONFIG_NATIVE_WINDOWS */
366
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800367 for (i = 0; wpa_drivers[i]; i++)
368 global.drv_count++;
369 if (global.drv_count == 0) {
370 wpa_printf(MSG_ERROR, "No drivers enabled");
371 return -1;
372 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700373 global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800374 if (global.drv_priv == NULL)
375 return -1;
376
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700377 return 0;
378}
379
380
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700381static void hostapd_global_deinit(const char *pid_file, int eloop_initialized)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700382{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800383 int i;
384
385 for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
386 if (!global.drv_priv[i])
387 continue;
388 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
389 }
390 os_free(global.drv_priv);
391 global.drv_priv = NULL;
392
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700393#ifdef EAP_SERVER_TNC
394 tncs_global_deinit();
395#endif /* EAP_SERVER_TNC */
396
397 random_deinit();
398
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700399 if (eloop_initialized)
400 eloop_destroy();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700401
402#ifndef CONFIG_NATIVE_WINDOWS
403 closelog();
404#endif /* CONFIG_NATIVE_WINDOWS */
405
406 eap_server_unregister_methods();
407
408 os_daemonize_terminate(pid_file);
409}
410
411
412static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
413 const char *pid_file)
414{
415#ifdef EAP_SERVER_TNC
416 int tnc = 0;
417 size_t i, k;
418
419 for (i = 0; !tnc && i < ifaces->count; i++) {
420 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
421 if (ifaces->iface[i]->bss[0]->conf->tnc) {
422 tnc++;
423 break;
424 }
425 }
426 }
427
428 if (tnc && tncs_global_init() < 0) {
429 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
430 return -1;
431 }
432#endif /* EAP_SERVER_TNC */
433
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800434 if (daemonize) {
435 if (os_daemonize(pid_file)) {
436 wpa_printf(MSG_ERROR, "daemon: %s", strerror(errno));
437 return -1;
438 }
439 if (eloop_sock_requeue()) {
440 wpa_printf(MSG_ERROR, "eloop_sock_requeue: %s",
441 strerror(errno));
442 return -1;
443 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700444 }
445
446 eloop_run();
447
448 return 0;
449}
450
451
452static void show_version(void)
453{
454 fprintf(stderr,
Hai Shalomfdcde762020-04-02 11:19:20 -0700455 "hostapd v%s\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700456 "User space daemon for IEEE 802.11 AP management,\n"
457 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
Hai Shalom74f70d42019-02-11 14:42:39 -0800458 "Copyright (c) 2002-2019, Jouni Malinen <j@w1.fi> "
Hai Shalomfdcde762020-04-02 11:19:20 -0700459 "and contributors\n",
460 VERSION_STR);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700461}
462
463
464static void usage(void)
465{
466 show_version();
467 fprintf(stderr,
468 "\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -0700469 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700470 "\\\n"
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700471 " [-g <global ctrl_iface>] [-G <group>]\\\n"
472 " [-i <comma-separated list of interface names>]\\\n"
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700473 " <configuration file(s)>\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700474 "\n"
475 "options:\n"
476 " -h show this usage\n"
477 " -d show more debug messages (-dd for even more)\n"
478 " -B run daemon in the background\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -0700479 " -e entropy file\n"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700480 " -g global control interface path\n"
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700481 " -G group for control interfaces\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700482 " -P PID file\n"
483 " -K include key data in debug messages\n"
484#ifdef CONFIG_DEBUG_FILE
485 " -f log output to debug file instead of stdout\n"
486#endif /* CONFIG_DEBUG_FILE */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800487#ifdef CONFIG_DEBUG_LINUX_TRACING
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800488 " -T record to Linux tracing in addition to logging\n"
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800489 " (records all messages regardless of debug verbosity)\n"
490#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700491 " -i list of interface names to use\n"
Paul Stewart092955c2017-02-06 09:13:09 -0800492#ifdef CONFIG_DEBUG_SYSLOG
493 " -s log output to syslog instead of stdout\n"
494#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800495 " -S start all the interfaces synchronously\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700496 " -t include timestamps in some debug messages\n"
497 " -v show hostapd version\n");
498
499 exit(1);
500}
501
502
503static const char * hostapd_msg_ifname_cb(void *ctx)
504{
505 struct hostapd_data *hapd = ctx;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800506 if (hapd && hapd->conf)
507 return hapd->conf->iface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700508 return NULL;
509}
510
511
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700512static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
513 const char *path)
514{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800515#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700516 char *pos;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800517#endif /* !CONFIG_CTRL_IFACE_UDP */
518
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700519 os_free(interfaces->global_iface_path);
520 interfaces->global_iface_path = os_strdup(path);
521 if (interfaces->global_iface_path == NULL)
522 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800523
524#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700525 pos = os_strrchr(interfaces->global_iface_path, '/');
526 if (pos == NULL) {
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700527 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
528 "file");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700529 os_free(interfaces->global_iface_path);
530 interfaces->global_iface_path = NULL;
531 return -1;
532 }
533
534 *pos = '\0';
535 interfaces->global_iface_name = pos + 1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800536#endif /* !CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700537
538 return 0;
539}
540
541
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700542static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
543 const char *group)
544{
545#ifndef CONFIG_NATIVE_WINDOWS
546 struct group *grp;
547 grp = getgrnam(group);
548 if (grp == NULL) {
549 wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
550 return -1;
551 }
552 interfaces->ctrl_iface_group = grp->gr_gid;
553#endif /* CONFIG_NATIVE_WINDOWS */
554 return 0;
555}
556
557
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700558static int hostapd_get_interface_names(char ***if_names,
559 size_t *if_names_size,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800560 char *arg)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700561{
562 char *if_name, *tmp, **nnames;
563 size_t i;
564
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800565 if (!arg)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700566 return -1;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800567 if_name = strtok_r(arg, ",", &tmp);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700568
569 while (if_name) {
570 nnames = os_realloc_array(*if_names, 1 + *if_names_size,
571 sizeof(char *));
572 if (!nnames)
573 goto fail;
574 *if_names = nnames;
575
576 (*if_names)[*if_names_size] = os_strdup(if_name);
577 if (!(*if_names)[*if_names_size])
578 goto fail;
579 (*if_names_size)++;
580 if_name = strtok_r(NULL, ",", &tmp);
581 }
582
583 return 0;
584
585fail:
586 for (i = 0; i < *if_names_size; i++)
587 os_free((*if_names)[i]);
588 os_free(*if_names);
589 *if_names = NULL;
590 *if_names_size = 0;
591 return -1;
592}
593
594
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800595#ifdef CONFIG_WPS
596static int gen_uuid(const char *txt_addr)
597{
598 u8 addr[ETH_ALEN];
599 u8 uuid[UUID_LEN];
600 char buf[100];
601
602 if (hwaddr_aton(txt_addr, addr) < 0)
603 return -1;
604
605 uuid_gen_mac_addr(addr, uuid);
606 if (uuid_bin2str(uuid, buf, sizeof(buf)) < 0)
607 return -1;
608
609 printf("%s\n", buf);
610
611 return 0;
612}
613#endif /* CONFIG_WPS */
614
615
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800616#ifndef HOSTAPD_CLEANUP_INTERVAL
617#define HOSTAPD_CLEANUP_INTERVAL 10
618#endif /* HOSTAPD_CLEANUP_INTERVAL */
619
620static int hostapd_periodic_call(struct hostapd_iface *iface, void *ctx)
621{
622 hostapd_periodic_iface(iface);
623 return 0;
624}
625
626
627/* Periodic cleanup tasks */
628static void hostapd_periodic(void *eloop_ctx, void *timeout_ctx)
629{
630 struct hapd_interfaces *interfaces = eloop_ctx;
631
632 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
633 hostapd_periodic, interfaces, NULL);
634 hostapd_for_each_interface(interfaces, hostapd_periodic_call, NULL);
635}
636
637
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700638int main(int argc, char *argv[])
639{
640 struct hapd_interfaces interfaces;
641 int ret = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800642 size_t i, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700643 int c, debug = 0, daemonize = 0;
644 char *pid_file = NULL;
645 const char *log_file = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700646 const char *entropy_file = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800647 char **bss_config = NULL, **tmp_bss;
648 size_t num_bss_configs = 0;
649#ifdef CONFIG_DEBUG_LINUX_TRACING
650 int enable_trace_dbg = 0;
651#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800652 int start_ifaces_in_sync = 0;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700653 char **if_names = NULL;
654 size_t if_names_size = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -0700655#ifdef CONFIG_DPP
656 struct dpp_global_config dpp_conf;
657#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700658
659 if (os_program_init())
660 return -1;
661
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700662 os_memset(&interfaces, 0, sizeof(interfaces));
663 interfaces.reload_config = hostapd_reload_config;
664 interfaces.config_read_cb = hostapd_config_read;
665 interfaces.for_each_interface = hostapd_for_each_interface;
666 interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
667 interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
668 interfaces.driver_init = hostapd_driver_init;
669 interfaces.global_iface_path = NULL;
670 interfaces.global_iface_name = NULL;
671 interfaces.global_ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800672 dl_list_init(&interfaces.global_ctrl_dst);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700673#ifdef CONFIG_ETH_P_OUI
674 dl_list_init(&interfaces.eth_p_oui);
675#endif /* CONFIG_ETH_P_OUI */
Roshan Pius3a1667e2018-07-03 15:17:14 -0700676#ifdef CONFIG_DPP
Hai Shalom81f62d82019-07-22 12:10:00 -0700677 os_memset(&dpp_conf, 0, sizeof(dpp_conf));
678 /* TODO: dpp_conf.msg_ctx? */
679 interfaces.dpp = dpp_global_init(&dpp_conf);
Hai Shalom021b0b52019-04-10 11:17:58 -0700680 if (!interfaces.dpp)
681 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700682#endif /* CONFIG_DPP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700683
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700684 for (;;) {
Paul Stewart092955c2017-02-06 09:13:09 -0800685 c = getopt(argc, argv, "b:Bde:f:hi:KP:sSTtu:vg:G:");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700686 if (c < 0)
687 break;
688 switch (c) {
689 case 'h':
690 usage();
691 break;
692 case 'd':
693 debug++;
694 if (wpa_debug_level > 0)
695 wpa_debug_level--;
696 break;
697 case 'B':
698 daemonize++;
699 break;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700700 case 'e':
701 entropy_file = optarg;
702 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700703 case 'f':
704 log_file = optarg;
705 break;
706 case 'K':
707 wpa_debug_show_keys++;
708 break;
709 case 'P':
710 os_free(pid_file);
711 pid_file = os_rel2abs_path(optarg);
712 break;
713 case 't':
714 wpa_debug_timestamp++;
715 break;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800716#ifdef CONFIG_DEBUG_LINUX_TRACING
717 case 'T':
718 enable_trace_dbg = 1;
719 break;
720#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700721 case 'v':
722 show_version();
723 exit(1);
724 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700725 case 'g':
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700726 if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
727 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700728 break;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700729 case 'G':
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700730 if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
731 return -1;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700732 break;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800733 case 'b':
734 tmp_bss = os_realloc_array(bss_config,
735 num_bss_configs + 1,
736 sizeof(char *));
737 if (tmp_bss == NULL)
738 goto out;
739 bss_config = tmp_bss;
740 bss_config[num_bss_configs++] = optarg;
741 break;
Paul Stewart092955c2017-02-06 09:13:09 -0800742#ifdef CONFIG_DEBUG_SYSLOG
743 case 's':
744 wpa_debug_syslog = 1;
745 break;
746#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800747 case 'S':
748 start_ifaces_in_sync = 1;
749 break;
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800750#ifdef CONFIG_WPS
751 case 'u':
752 return gen_uuid(optarg);
753#endif /* CONFIG_WPS */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700754 case 'i':
755 if (hostapd_get_interface_names(&if_names,
756 &if_names_size, optarg))
757 goto out;
758 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700759 default:
760 usage();
761 break;
762 }
763 }
764
Roshan Piuscc817562017-12-22 14:45:05 -0800765#ifndef CONFIG_CTRL_IFACE_HIDL
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800766 if (optind == argc && interfaces.global_iface_path == NULL &&
767 num_bss_configs == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700768 usage();
Roshan Piuscc817562017-12-22 14:45:05 -0800769#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700770
771 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
772
773 if (log_file)
774 wpa_debug_open_file(log_file);
Hai Shalomfdcde762020-04-02 11:19:20 -0700775 if (!log_file && !wpa_debug_syslog)
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800776 wpa_debug_setup_stdout();
Paul Stewart092955c2017-02-06 09:13:09 -0800777#ifdef CONFIG_DEBUG_SYSLOG
778 if (wpa_debug_syslog)
779 wpa_debug_open_syslog();
780#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800781#ifdef CONFIG_DEBUG_LINUX_TRACING
782 if (enable_trace_dbg) {
783 int tret = wpa_debug_open_linux_tracing();
784 if (tret) {
785 wpa_printf(MSG_ERROR, "Failed to enable trace logging");
786 return -1;
787 }
788 }
789#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700790
791 interfaces.count = argc - optind;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800792 if (interfaces.count || num_bss_configs) {
793 interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700794 sizeof(struct hostapd_iface *));
795 if (interfaces.iface == NULL) {
796 wpa_printf(MSG_ERROR, "malloc failed");
797 return -1;
798 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700799 }
800
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700801 if (hostapd_global_init(&interfaces, entropy_file)) {
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700802 wpa_printf(MSG_ERROR, "Failed to initialize global context");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700803 return -1;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700804 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700805
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800806 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
807 hostapd_periodic, &interfaces, NULL);
808
809 if (fst_global_init()) {
810 wpa_printf(MSG_ERROR,
811 "Failed to initialize global FST context");
812 goto out;
813 }
814
815#if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE)
816 if (!fst_global_add_ctrl(fst_ctrl_cli))
817 wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl");
818#endif /* CONFIG_FST && CONFIG_CTRL_IFACE */
819
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800820 /* Allocate and parse configuration for full interface files */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700821 for (i = 0; i < interfaces.count; i++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700822 char *if_name = NULL;
823
824 if (i < if_names_size)
825 if_name = if_names[i];
826
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700827 interfaces.iface[i] = hostapd_interface_init(&interfaces,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700828 if_name,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700829 argv[optind + i],
830 debug);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700831 if (!interfaces.iface[i]) {
832 wpa_printf(MSG_ERROR, "Failed to initialize interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700833 goto out;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700834 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800835 if (start_ifaces_in_sync)
836 interfaces.iface[i]->need_to_start_in_sync = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700837 }
838
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800839 /* Allocate and parse configuration for per-BSS files */
840 for (i = 0; i < num_bss_configs; i++) {
841 struct hostapd_iface *iface;
842 char *fname;
843
844 wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
845 fname = os_strchr(bss_config[i], ':');
846 if (fname == NULL) {
847 wpa_printf(MSG_ERROR,
848 "Invalid BSS config identifier '%s'",
849 bss_config[i]);
850 goto out;
851 }
852 *fname++ = '\0';
853 iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
854 fname, debug);
855 if (iface == NULL)
856 goto out;
857 for (j = 0; j < interfaces.count; j++) {
858 if (interfaces.iface[j] == iface)
859 break;
860 }
861 if (j == interfaces.count) {
862 struct hostapd_iface **tmp;
863 tmp = os_realloc_array(interfaces.iface,
864 interfaces.count + 1,
865 sizeof(struct hostapd_iface *));
866 if (tmp == NULL) {
867 hostapd_interface_deinit_free(iface);
868 goto out;
869 }
870 interfaces.iface = tmp;
871 interfaces.iface[interfaces.count++] = iface;
872 }
873 }
874
875 /*
876 * Enable configured interfaces. Depending on channel configuration,
877 * this may complete full initialization before returning or use a
878 * callback mechanism to complete setup in case of operations like HT
879 * co-ex scans, ACS, or DFS are needed to determine channel parameters.
880 * In such case, the interface will be enabled from eloop context within
881 * hostapd_global_run().
882 */
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800883 interfaces.terminate_on_error = interfaces.count;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800884 for (i = 0; i < interfaces.count; i++) {
Hai Shalom74f70d42019-02-11 14:42:39 -0800885 if (hostapd_driver_init(interfaces.iface[i]) ||
886 hostapd_setup_interface(interfaces.iface[i]))
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800887 goto out;
888 }
889
Roshan Piuscc817562017-12-22 14:45:05 -0800890#ifdef CONFIG_CTRL_IFACE_HIDL
891 if (hostapd_hidl_init(&interfaces)) {
892 wpa_printf(MSG_ERROR, "Failed to initialize HIDL interface");
893 goto out;
894 }
Roshan Piuscc817562017-12-22 14:45:05 -0800895#endif /* CONFIG_CTRL_IFACE_HIDL */
Daichi Ueuracaa74a82018-02-14 22:25:39 +0900896 hostapd_global_ctrl_iface_init(&interfaces);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700897
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700898 if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
899 wpa_printf(MSG_ERROR, "Failed to start eloop");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700900 goto out;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700901 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700902
903 ret = 0;
904
905 out:
Roshan Piuscc817562017-12-22 14:45:05 -0800906#ifdef CONFIG_CTRL_IFACE_HIDL
907 hostapd_hidl_deinit(&interfaces);
Roshan Piuscc817562017-12-22 14:45:05 -0800908#endif /* CONFIG_CTRL_IFACE_HIDL */
Daichi Ueuracaa74a82018-02-14 22:25:39 +0900909 hostapd_global_ctrl_iface_deinit(&interfaces);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700910 /* Deinitialize all interfaces */
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800911 for (i = 0; i < interfaces.count; i++) {
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700912 if (!interfaces.iface[i])
913 continue;
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800914 interfaces.iface[i]->driver_ap_teardown =
915 !!(interfaces.iface[i]->drv_flags &
916 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917 hostapd_interface_deinit_free(interfaces.iface[i]);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800918 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700919 os_free(interfaces.iface);
920
Roshan Pius3a1667e2018-07-03 15:17:14 -0700921#ifdef CONFIG_DPP
Hai Shalom021b0b52019-04-10 11:17:58 -0700922 dpp_global_deinit(interfaces.dpp);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700923#endif /* CONFIG_DPP */
924
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700925 if (interfaces.eloop_initialized)
926 eloop_cancel_timeout(hostapd_periodic, &interfaces, NULL);
927 hostapd_global_deinit(pid_file, interfaces.eloop_initialized);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928 os_free(pid_file);
929
Paul Stewart092955c2017-02-06 09:13:09 -0800930 wpa_debug_close_syslog();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700931 if (log_file)
932 wpa_debug_close_file();
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800933 wpa_debug_close_linux_tracing();
934
935 os_free(bss_config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700936
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700937 for (i = 0; i < if_names_size; i++)
938 os_free(if_names[i]);
939 os_free(if_names);
940
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800941 fst_global_deinit();
942
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700943 os_program_deinit();
944
945 return ret;
946}