blob: ce94d0572417f0f111a150f127c4383ca3bf89a5 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / main()
Dmitry Shmidt29333592017-01-09 12:27:11 -08003 * Copyright (c) 2002-2017, 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
Paul Stewart092955c2017-02-06 09:13:09 -0800111#ifdef CONFIG_DEBUG_SYSLOG
112 if (wpa_debug_syslog)
113 conf_stdout = 0;
114#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700115 if ((conf_stdout & module) && level >= conf_stdout_level) {
116 wpa_debug_print_timestamp();
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800117 wpa_printf(MSG_INFO, "%s", format);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700118 }
119
120#ifndef CONFIG_NATIVE_WINDOWS
121 if ((conf_syslog & module) && level >= conf_syslog_level) {
122 int priority;
123 switch (level) {
124 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
125 case HOSTAPD_LEVEL_DEBUG:
126 priority = LOG_DEBUG;
127 break;
128 case HOSTAPD_LEVEL_INFO:
129 priority = LOG_INFO;
130 break;
131 case HOSTAPD_LEVEL_NOTICE:
132 priority = LOG_NOTICE;
133 break;
134 case HOSTAPD_LEVEL_WARNING:
135 priority = LOG_WARNING;
136 break;
137 default:
138 priority = LOG_INFO;
139 break;
140 }
141 syslog(priority, "%s", format);
142 }
143#endif /* CONFIG_NATIVE_WINDOWS */
144
145 os_free(format);
146}
147#endif /* CONFIG_NO_HOSTAPD_LOGGER */
148
149
150/**
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800151 * hostapd_driver_init - Preparate driver interface
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700152 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153static int hostapd_driver_init(struct hostapd_iface *iface)
154{
155 struct wpa_init_params params;
156 size_t i;
157 struct hostapd_data *hapd = iface->bss[0];
158 struct hostapd_bss_config *conf = hapd->conf;
159 u8 *b = conf->bssid;
160 struct wpa_driver_capa capa;
161
162 if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
163 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
164 return -1;
165 }
166
167 /* Initialize the driver interface */
168 if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
169 b = NULL;
170
171 os_memset(&params, 0, sizeof(params));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800172 for (i = 0; wpa_drivers[i]; i++) {
173 if (wpa_drivers[i] != hapd->driver)
174 continue;
175
176 if (global.drv_priv[i] == NULL &&
177 wpa_drivers[i]->global_init) {
Dmitry Shmidte4663042016-04-04 10:07:49 -0700178 global.drv_priv[i] =
179 wpa_drivers[i]->global_init(iface->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800180 if (global.drv_priv[i] == NULL) {
181 wpa_printf(MSG_ERROR, "Failed to initialize "
182 "driver '%s'",
183 wpa_drivers[i]->name);
184 return -1;
185 }
186 }
187
188 params.global_priv = global.drv_priv[i];
189 break;
190 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700191 params.bssid = b;
192 params.ifname = hapd->conf->iface;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800193 params.driver_params = hapd->iconf->driver_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
195
196 params.num_bridge = hapd->iface->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700197 params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700198 if (params.bridge == NULL)
199 return -1;
200 for (i = 0; i < hapd->iface->num_bss; i++) {
201 struct hostapd_data *bss = hapd->iface->bss[i];
202 if (bss->conf->bridge[0])
203 params.bridge[i] = bss->conf->bridge;
204 }
205
206 params.own_addr = hapd->own_addr;
207
208 hapd->drv_priv = hapd->driver->hapd_init(hapd, &params);
209 os_free(params.bridge);
210 if (hapd->drv_priv == NULL) {
211 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
212 hapd->driver->name);
213 hapd->driver = NULL;
214 return -1;
215 }
216
217 if (hapd->driver->get_capa &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800218 hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
Dmitry Shmidt0207e232014-09-03 14:58:37 -0700219 struct wowlan_triggers *triggs;
220
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700221 iface->drv_flags = capa.flags;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800222 iface->smps_modes = capa.smps_modes;
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
255 * interfaces. No actiual driver operations are started.
256 */
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
264 wpa_printf(MSG_ERROR, "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,
455 "hostapd v" VERSION_STR "\n"
456 "User space daemon for IEEE 802.11 AP management,\n"
457 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
Dmitry Shmidt29333592017-01-09 12:27:11 -0800458 "Copyright (c) 2002-2017, Jouni Malinen <j@w1.fi> "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700459 "and contributors\n");
460}
461
462
463static void usage(void)
464{
465 show_version();
466 fprintf(stderr,
467 "\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -0700468 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700469 "\\\n"
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700470 " [-g <global ctrl_iface>] [-G <group>]\\\n"
471 " [-i <comma-separated list of interface names>]\\\n"
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700472 " <configuration file(s)>\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700473 "\n"
474 "options:\n"
475 " -h show this usage\n"
476 " -d show more debug messages (-dd for even more)\n"
477 " -B run daemon in the background\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -0700478 " -e entropy file\n"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700479 " -g global control interface path\n"
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700480 " -G group for control interfaces\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700481 " -P PID file\n"
482 " -K include key data in debug messages\n"
483#ifdef CONFIG_DEBUG_FILE
484 " -f log output to debug file instead of stdout\n"
485#endif /* CONFIG_DEBUG_FILE */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800486#ifdef CONFIG_DEBUG_LINUX_TRACING
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800487 " -T record to Linux tracing in addition to logging\n"
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800488 " (records all messages regardless of debug verbosity)\n"
489#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700490 " -i list of interface names to use\n"
Paul Stewart092955c2017-02-06 09:13:09 -0800491#ifdef CONFIG_DEBUG_SYSLOG
492 " -s log output to syslog instead of stdout\n"
493#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800494 " -S start all the interfaces synchronously\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700495 " -t include timestamps in some debug messages\n"
496 " -v show hostapd version\n");
497
498 exit(1);
499}
500
501
502static const char * hostapd_msg_ifname_cb(void *ctx)
503{
504 struct hostapd_data *hapd = ctx;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800505 if (hapd && hapd->conf)
506 return hapd->conf->iface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700507 return NULL;
508}
509
510
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700511static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
512 const char *path)
513{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800514#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700515 char *pos;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800516#endif /* !CONFIG_CTRL_IFACE_UDP */
517
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700518 os_free(interfaces->global_iface_path);
519 interfaces->global_iface_path = os_strdup(path);
520 if (interfaces->global_iface_path == NULL)
521 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800522
523#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700524 pos = os_strrchr(interfaces->global_iface_path, '/');
525 if (pos == NULL) {
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700526 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
527 "file");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700528 os_free(interfaces->global_iface_path);
529 interfaces->global_iface_path = NULL;
530 return -1;
531 }
532
533 *pos = '\0';
534 interfaces->global_iface_name = pos + 1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800535#endif /* !CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700536
537 return 0;
538}
539
540
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700541static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
542 const char *group)
543{
544#ifndef CONFIG_NATIVE_WINDOWS
545 struct group *grp;
546 grp = getgrnam(group);
547 if (grp == NULL) {
548 wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
549 return -1;
550 }
551 interfaces->ctrl_iface_group = grp->gr_gid;
552#endif /* CONFIG_NATIVE_WINDOWS */
553 return 0;
554}
555
556
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700557static int hostapd_get_interface_names(char ***if_names,
558 size_t *if_names_size,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800559 char *arg)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700560{
561 char *if_name, *tmp, **nnames;
562 size_t i;
563
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800564 if (!arg)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700565 return -1;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800566 if_name = strtok_r(arg, ",", &tmp);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700567
568 while (if_name) {
569 nnames = os_realloc_array(*if_names, 1 + *if_names_size,
570 sizeof(char *));
571 if (!nnames)
572 goto fail;
573 *if_names = nnames;
574
575 (*if_names)[*if_names_size] = os_strdup(if_name);
576 if (!(*if_names)[*if_names_size])
577 goto fail;
578 (*if_names_size)++;
579 if_name = strtok_r(NULL, ",", &tmp);
580 }
581
582 return 0;
583
584fail:
585 for (i = 0; i < *if_names_size; i++)
586 os_free((*if_names)[i]);
587 os_free(*if_names);
588 *if_names = NULL;
589 *if_names_size = 0;
590 return -1;
591}
592
593
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800594#ifdef CONFIG_WPS
595static int gen_uuid(const char *txt_addr)
596{
597 u8 addr[ETH_ALEN];
598 u8 uuid[UUID_LEN];
599 char buf[100];
600
601 if (hwaddr_aton(txt_addr, addr) < 0)
602 return -1;
603
604 uuid_gen_mac_addr(addr, uuid);
605 if (uuid_bin2str(uuid, buf, sizeof(buf)) < 0)
606 return -1;
607
608 printf("%s\n", buf);
609
610 return 0;
611}
612#endif /* CONFIG_WPS */
613
614
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800615#ifndef HOSTAPD_CLEANUP_INTERVAL
616#define HOSTAPD_CLEANUP_INTERVAL 10
617#endif /* HOSTAPD_CLEANUP_INTERVAL */
618
619static int hostapd_periodic_call(struct hostapd_iface *iface, void *ctx)
620{
621 hostapd_periodic_iface(iface);
622 return 0;
623}
624
625
626/* Periodic cleanup tasks */
627static void hostapd_periodic(void *eloop_ctx, void *timeout_ctx)
628{
629 struct hapd_interfaces *interfaces = eloop_ctx;
630
631 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
632 hostapd_periodic, interfaces, NULL);
633 hostapd_for_each_interface(interfaces, hostapd_periodic_call, NULL);
634}
635
636
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700637int main(int argc, char *argv[])
638{
639 struct hapd_interfaces interfaces;
640 int ret = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800641 size_t i, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700642 int c, debug = 0, daemonize = 0;
643 char *pid_file = NULL;
644 const char *log_file = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700645 const char *entropy_file = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800646 char **bss_config = NULL, **tmp_bss;
647 size_t num_bss_configs = 0;
648#ifdef CONFIG_DEBUG_LINUX_TRACING
649 int enable_trace_dbg = 0;
650#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800651 int start_ifaces_in_sync = 0;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700652 char **if_names = NULL;
653 size_t if_names_size = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700654
655 if (os_program_init())
656 return -1;
657
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700658 os_memset(&interfaces, 0, sizeof(interfaces));
659 interfaces.reload_config = hostapd_reload_config;
660 interfaces.config_read_cb = hostapd_config_read;
661 interfaces.for_each_interface = hostapd_for_each_interface;
662 interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
663 interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
664 interfaces.driver_init = hostapd_driver_init;
665 interfaces.global_iface_path = NULL;
666 interfaces.global_iface_name = NULL;
667 interfaces.global_ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800668 dl_list_init(&interfaces.global_ctrl_dst);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700669#ifdef CONFIG_ETH_P_OUI
670 dl_list_init(&interfaces.eth_p_oui);
671#endif /* CONFIG_ETH_P_OUI */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700672
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700673 for (;;) {
Paul Stewart092955c2017-02-06 09:13:09 -0800674 c = getopt(argc, argv, "b:Bde:f:hi:KP:sSTtu:vg:G:");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700675 if (c < 0)
676 break;
677 switch (c) {
678 case 'h':
679 usage();
680 break;
681 case 'd':
682 debug++;
683 if (wpa_debug_level > 0)
684 wpa_debug_level--;
685 break;
686 case 'B':
687 daemonize++;
688 break;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700689 case 'e':
690 entropy_file = optarg;
691 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700692 case 'f':
693 log_file = optarg;
694 break;
695 case 'K':
696 wpa_debug_show_keys++;
697 break;
698 case 'P':
699 os_free(pid_file);
700 pid_file = os_rel2abs_path(optarg);
701 break;
702 case 't':
703 wpa_debug_timestamp++;
704 break;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800705#ifdef CONFIG_DEBUG_LINUX_TRACING
706 case 'T':
707 enable_trace_dbg = 1;
708 break;
709#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700710 case 'v':
711 show_version();
712 exit(1);
713 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700714 case 'g':
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700715 if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
716 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700717 break;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700718 case 'G':
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700719 if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
720 return -1;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700721 break;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800722 case 'b':
723 tmp_bss = os_realloc_array(bss_config,
724 num_bss_configs + 1,
725 sizeof(char *));
726 if (tmp_bss == NULL)
727 goto out;
728 bss_config = tmp_bss;
729 bss_config[num_bss_configs++] = optarg;
730 break;
Paul Stewart092955c2017-02-06 09:13:09 -0800731#ifdef CONFIG_DEBUG_SYSLOG
732 case 's':
733 wpa_debug_syslog = 1;
734 break;
735#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800736 case 'S':
737 start_ifaces_in_sync = 1;
738 break;
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800739#ifdef CONFIG_WPS
740 case 'u':
741 return gen_uuid(optarg);
742#endif /* CONFIG_WPS */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700743 case 'i':
744 if (hostapd_get_interface_names(&if_names,
745 &if_names_size, optarg))
746 goto out;
747 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700748 default:
749 usage();
750 break;
751 }
752 }
753
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800754 if (optind == argc && interfaces.global_iface_path == NULL &&
755 num_bss_configs == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700756 usage();
757
758 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
759
760 if (log_file)
761 wpa_debug_open_file(log_file);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800762 else
763 wpa_debug_setup_stdout();
Paul Stewart092955c2017-02-06 09:13:09 -0800764#ifdef CONFIG_DEBUG_SYSLOG
765 if (wpa_debug_syslog)
766 wpa_debug_open_syslog();
767#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800768#ifdef CONFIG_DEBUG_LINUX_TRACING
769 if (enable_trace_dbg) {
770 int tret = wpa_debug_open_linux_tracing();
771 if (tret) {
772 wpa_printf(MSG_ERROR, "Failed to enable trace logging");
773 return -1;
774 }
775 }
776#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700777
778 interfaces.count = argc - optind;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800779 if (interfaces.count || num_bss_configs) {
780 interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700781 sizeof(struct hostapd_iface *));
782 if (interfaces.iface == NULL) {
783 wpa_printf(MSG_ERROR, "malloc failed");
784 return -1;
785 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700786 }
787
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700788 if (hostapd_global_init(&interfaces, entropy_file)) {
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700789 wpa_printf(MSG_ERROR, "Failed to initialize global context");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700790 return -1;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700791 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700792
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800793 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
794 hostapd_periodic, &interfaces, NULL);
795
796 if (fst_global_init()) {
797 wpa_printf(MSG_ERROR,
798 "Failed to initialize global FST context");
799 goto out;
800 }
801
802#if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE)
803 if (!fst_global_add_ctrl(fst_ctrl_cli))
804 wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl");
805#endif /* CONFIG_FST && CONFIG_CTRL_IFACE */
806
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800807 /* Allocate and parse configuration for full interface files */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700808 for (i = 0; i < interfaces.count; i++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700809 char *if_name = NULL;
810
811 if (i < if_names_size)
812 if_name = if_names[i];
813
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700814 interfaces.iface[i] = hostapd_interface_init(&interfaces,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700815 if_name,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700816 argv[optind + i],
817 debug);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700818 if (!interfaces.iface[i]) {
819 wpa_printf(MSG_ERROR, "Failed to initialize interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700820 goto out;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700821 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800822 if (start_ifaces_in_sync)
823 interfaces.iface[i]->need_to_start_in_sync = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700824 }
825
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800826 /* Allocate and parse configuration for per-BSS files */
827 for (i = 0; i < num_bss_configs; i++) {
828 struct hostapd_iface *iface;
829 char *fname;
830
831 wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
832 fname = os_strchr(bss_config[i], ':');
833 if (fname == NULL) {
834 wpa_printf(MSG_ERROR,
835 "Invalid BSS config identifier '%s'",
836 bss_config[i]);
837 goto out;
838 }
839 *fname++ = '\0';
840 iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
841 fname, debug);
842 if (iface == NULL)
843 goto out;
844 for (j = 0; j < interfaces.count; j++) {
845 if (interfaces.iface[j] == iface)
846 break;
847 }
848 if (j == interfaces.count) {
849 struct hostapd_iface **tmp;
850 tmp = os_realloc_array(interfaces.iface,
851 interfaces.count + 1,
852 sizeof(struct hostapd_iface *));
853 if (tmp == NULL) {
854 hostapd_interface_deinit_free(iface);
855 goto out;
856 }
857 interfaces.iface = tmp;
858 interfaces.iface[interfaces.count++] = iface;
859 }
860 }
861
862 /*
863 * Enable configured interfaces. Depending on channel configuration,
864 * this may complete full initialization before returning or use a
865 * callback mechanism to complete setup in case of operations like HT
866 * co-ex scans, ACS, or DFS are needed to determine channel parameters.
867 * In such case, the interface will be enabled from eloop context within
868 * hostapd_global_run().
869 */
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800870 interfaces.terminate_on_error = interfaces.count;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800871 for (i = 0; i < interfaces.count; i++) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700872 if (hostapd_driver_init(interfaces.iface[i]))
873 goto out;
874#ifdef CONFIG_MBO
875 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
876 struct hostapd_data *hapd = interfaces.iface[i]->bss[j];
877
878 if (hapd && (hapd->conf->oce & OCE_STA_CFON) &&
879 (interfaces.iface[i]->drv_flags &
880 WPA_DRIVER_FLAGS_OCE_STA_CFON))
881 hapd->enable_oce = OCE_STA_CFON;
882
883 if (hapd && (hapd->conf->oce & OCE_AP) &&
884 (interfaces.iface[i]->drv_flags &
885 WPA_DRIVER_FLAGS_OCE_STA_CFON)) {
886 /* TODO: Need to add OCE-AP support */
887 wpa_printf(MSG_ERROR,
888 "OCE-AP feature is not yet supported");
889 }
890 }
891#endif /* CONFIG_MBO */
892 if (hostapd_setup_interface(interfaces.iface[i]))
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800893 goto out;
894 }
895
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700896 hostapd_global_ctrl_iface_init(&interfaces);
897
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:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700906 hostapd_global_ctrl_iface_deinit(&interfaces);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700907 /* Deinitialize all interfaces */
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800908 for (i = 0; i < interfaces.count; i++) {
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700909 if (!interfaces.iface[i])
910 continue;
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800911 interfaces.iface[i]->driver_ap_teardown =
912 !!(interfaces.iface[i]->drv_flags &
913 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700914 hostapd_interface_deinit_free(interfaces.iface[i]);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800915 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700916 os_free(interfaces.iface);
917
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700918 if (interfaces.eloop_initialized)
919 eloop_cancel_timeout(hostapd_periodic, &interfaces, NULL);
920 hostapd_global_deinit(pid_file, interfaces.eloop_initialized);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700921 os_free(pid_file);
922
Paul Stewart092955c2017-02-06 09:13:09 -0800923 wpa_debug_close_syslog();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700924 if (log_file)
925 wpa_debug_close_file();
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800926 wpa_debug_close_linux_tracing();
927
928 os_free(bss_config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700930 for (i = 0; i < if_names_size; i++)
931 os_free(if_names[i]);
932 os_free(if_names);
933
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800934 fst_global_deinit();
935
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700936 os_program_deinit();
937
938 return ret;
939}