]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * hostapd / main() | |
3 | * Copyright (c) 2002-2019, Jouni Malinen <j@w1.fi> | |
4 | * | |
5 | * This software may be distributed under the terms of the BSD license. | |
6 | * See README for more details. | |
7 | */ | |
8 | ||
9 | #include "utils/includes.h" | |
10 | #ifndef CONFIG_NATIVE_WINDOWS | |
11 | #include <syslog.h> | |
12 | #include <grp.h> | |
13 | #endif /* CONFIG_NATIVE_WINDOWS */ | |
14 | ||
15 | #include "utils/common.h" | |
16 | #include "utils/eloop.h" | |
17 | #include "utils/uuid.h" | |
18 | #include "crypto/random.h" | |
19 | #include "crypto/tls.h" | |
20 | #include "common/version.h" | |
21 | #include "common/dpp.h" | |
22 | #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" | |
27 | #include "ap/ap_drv_ops.h" | |
28 | #include "ap/dpp_hostapd.h" | |
29 | #include "fst/fst.h" | |
30 | #include "config_file.h" | |
31 | #include "eap_register.h" | |
32 | #include "ctrl_iface.h" | |
33 | ||
34 | ||
35 | struct hapd_global { | |
36 | void **drv_priv; | |
37 | size_t drv_count; | |
38 | }; | |
39 | ||
40 | static struct hapd_global global; | |
41 | ||
42 | ||
43 | #ifndef CONFIG_NO_HOSTAPD_LOGGER | |
44 | static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module, | |
45 | int level, const char *txt, size_t len) | |
46 | { | |
47 | struct hostapd_data *hapd = ctx; | |
48 | char *format, *module_str; | |
49 | int maxlen; | |
50 | int conf_syslog_level, conf_stdout_level; | |
51 | unsigned int conf_syslog, conf_stdout; | |
52 | ||
53 | maxlen = len + 100; | |
54 | format = os_malloc(maxlen); | |
55 | if (!format) | |
56 | return; | |
57 | ||
58 | if (hapd && hapd->conf) { | |
59 | conf_syslog_level = hapd->conf->logger_syslog_level; | |
60 | conf_stdout_level = hapd->conf->logger_stdout_level; | |
61 | conf_syslog = hapd->conf->logger_syslog; | |
62 | conf_stdout = hapd->conf->logger_stdout; | |
63 | } else { | |
64 | conf_syslog_level = conf_stdout_level = 0; | |
65 | conf_syslog = conf_stdout = (unsigned int) -1; | |
66 | } | |
67 | ||
68 | switch (module) { | |
69 | case HOSTAPD_MODULE_IEEE80211: | |
70 | module_str = "IEEE 802.11"; | |
71 | break; | |
72 | case HOSTAPD_MODULE_IEEE8021X: | |
73 | module_str = "IEEE 802.1X"; | |
74 | break; | |
75 | case HOSTAPD_MODULE_RADIUS: | |
76 | module_str = "RADIUS"; | |
77 | break; | |
78 | case HOSTAPD_MODULE_WPA: | |
79 | module_str = "WPA"; | |
80 | break; | |
81 | case HOSTAPD_MODULE_DRIVER: | |
82 | module_str = "DRIVER"; | |
83 | break; | |
84 | case HOSTAPD_MODULE_MLME: | |
85 | module_str = "MLME"; | |
86 | break; | |
87 | default: | |
88 | module_str = NULL; | |
89 | break; | |
90 | } | |
91 | ||
92 | if (hapd && hapd->conf && addr) | |
93 | os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s", | |
94 | hapd->conf->iface, MAC2STR(addr), | |
95 | module_str ? " " : "", module_str ? module_str : "", | |
96 | txt); | |
97 | else if (hapd && hapd->conf) | |
98 | os_snprintf(format, maxlen, "%s:%s%s %s", | |
99 | hapd->conf->iface, module_str ? " " : "", | |
100 | module_str ? module_str : "", txt); | |
101 | else if (addr) | |
102 | os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s", | |
103 | MAC2STR(addr), module_str ? " " : "", | |
104 | module_str ? module_str : "", txt); | |
105 | else | |
106 | os_snprintf(format, maxlen, "%s%s%s", | |
107 | module_str ? module_str : "", | |
108 | module_str ? ": " : "", txt); | |
109 | ||
110 | #ifdef CONFIG_DEBUG_SYSLOG | |
111 | if (wpa_debug_syslog) | |
112 | conf_stdout = 0; | |
113 | #endif /* CONFIG_DEBUG_SYSLOG */ | |
114 | if ((conf_stdout & module) && level >= conf_stdout_level) { | |
115 | wpa_debug_print_timestamp(); | |
116 | wpa_printf(MSG_INFO, "%s", format); | |
117 | } | |
118 | ||
119 | #ifndef CONFIG_NATIVE_WINDOWS | |
120 | if ((conf_syslog & module) && level >= conf_syslog_level) { | |
121 | int priority; | |
122 | switch (level) { | |
123 | case HOSTAPD_LEVEL_DEBUG_VERBOSE: | |
124 | case HOSTAPD_LEVEL_DEBUG: | |
125 | priority = LOG_DEBUG; | |
126 | break; | |
127 | case HOSTAPD_LEVEL_INFO: | |
128 | priority = LOG_INFO; | |
129 | break; | |
130 | case HOSTAPD_LEVEL_NOTICE: | |
131 | priority = LOG_NOTICE; | |
132 | break; | |
133 | case HOSTAPD_LEVEL_WARNING: | |
134 | priority = LOG_WARNING; | |
135 | break; | |
136 | default: | |
137 | priority = LOG_INFO; | |
138 | break; | |
139 | } | |
140 | syslog(priority, "%s", format); | |
141 | } | |
142 | #endif /* CONFIG_NATIVE_WINDOWS */ | |
143 | ||
144 | os_free(format); | |
145 | } | |
146 | #endif /* CONFIG_NO_HOSTAPD_LOGGER */ | |
147 | ||
148 | ||
149 | /** | |
150 | * hostapd_driver_init - Preparate driver interface | |
151 | */ | |
152 | static int hostapd_driver_init(struct hostapd_iface *iface) | |
153 | { | |
154 | struct wpa_init_params params; | |
155 | size_t i; | |
156 | struct hostapd_data *hapd = iface->bss[0]; | |
157 | struct hostapd_bss_config *conf = hapd->conf; | |
158 | u8 *b = conf->bssid; | |
159 | struct wpa_driver_capa capa; | |
160 | ||
161 | if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) { | |
162 | wpa_printf(MSG_ERROR, "No hostapd driver wrapper available"); | |
163 | return -1; | |
164 | } | |
165 | ||
166 | /* Initialize the driver interface */ | |
167 | if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5])) | |
168 | b = NULL; | |
169 | ||
170 | os_memset(¶ms, 0, sizeof(params)); | |
171 | for (i = 0; wpa_drivers[i]; i++) { | |
172 | if (wpa_drivers[i] != hapd->driver) | |
173 | continue; | |
174 | ||
175 | if (global.drv_priv[i] == NULL && | |
176 | wpa_drivers[i]->global_init) { | |
177 | global.drv_priv[i] = | |
178 | wpa_drivers[i]->global_init(iface->interfaces); | |
179 | if (global.drv_priv[i] == NULL) { | |
180 | wpa_printf(MSG_ERROR, "Failed to initialize " | |
181 | "driver '%s'", | |
182 | wpa_drivers[i]->name); | |
183 | return -1; | |
184 | } | |
185 | } | |
186 | ||
187 | params.global_priv = global.drv_priv[i]; | |
188 | break; | |
189 | } | |
190 | params.bssid = b; | |
191 | params.ifname = hapd->conf->iface; | |
192 | params.driver_params = hapd->iconf->driver_params; | |
193 | params.use_pae_group_addr = hapd->conf->use_pae_group_addr; | |
194 | ||
195 | params.num_bridge = hapd->iface->num_bss; | |
196 | params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *)); | |
197 | if (params.bridge == NULL) | |
198 | return -1; | |
199 | for (i = 0; i < hapd->iface->num_bss; i++) { | |
200 | struct hostapd_data *bss = hapd->iface->bss[i]; | |
201 | if (bss->conf->bridge[0]) | |
202 | params.bridge[i] = bss->conf->bridge; | |
203 | } | |
204 | ||
205 | params.own_addr = hapd->own_addr; | |
206 | ||
207 | hapd->drv_priv = hapd->driver->hapd_init(hapd, ¶ms); | |
208 | os_free(params.bridge); | |
209 | if (hapd->drv_priv == NULL) { | |
210 | wpa_printf(MSG_ERROR, "%s driver initialization failed.", | |
211 | hapd->driver->name); | |
212 | hapd->driver = NULL; | |
213 | return -1; | |
214 | } | |
215 | ||
216 | if (hapd->driver->get_capa && | |
217 | hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) { | |
218 | struct wowlan_triggers *triggs; | |
219 | ||
220 | iface->drv_flags = capa.flags; | |
221 | iface->probe_resp_offloads = capa.probe_resp_offloads; | |
222 | /* | |
223 | * Use default extended capa values from per-radio information | |
224 | */ | |
225 | iface->extended_capa = capa.extended_capa; | |
226 | iface->extended_capa_mask = capa.extended_capa_mask; | |
227 | iface->extended_capa_len = capa.extended_capa_len; | |
228 | iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs; | |
229 | ||
230 | /* | |
231 | * Override extended capa with per-interface type (AP), if | |
232 | * available from the driver. | |
233 | */ | |
234 | hostapd_get_ext_capa(iface); | |
235 | ||
236 | triggs = wpa_get_wowlan_triggers(conf->wowlan_triggers, &capa); | |
237 | if (triggs && hapd->driver->set_wowlan) { | |
238 | if (hapd->driver->set_wowlan(hapd->drv_priv, triggs)) | |
239 | wpa_printf(MSG_ERROR, "set_wowlan failed"); | |
240 | } | |
241 | os_free(triggs); | |
242 | } | |
243 | ||
244 | return 0; | |
245 | } | |
246 | ||
247 | ||
248 | /** | |
249 | * hostapd_interface_init - Read configuration file and init BSS data | |
250 | * | |
251 | * This function is used to parse configuration file for a full interface (one | |
252 | * or more BSSes sharing the same radio) and allocate memory for the BSS | |
253 | * interfaces. No actual driver operations are started. | |
254 | */ | |
255 | static struct hostapd_iface * | |
256 | hostapd_interface_init(struct hapd_interfaces *interfaces, const char *if_name, | |
257 | const char *config_fname, int debug) | |
258 | { | |
259 | struct hostapd_iface *iface; | |
260 | int k; | |
261 | ||
262 | wpa_printf(MSG_DEBUG, "Configuration file: %s", config_fname); | |
263 | iface = hostapd_init(interfaces, config_fname); | |
264 | if (!iface) | |
265 | return NULL; | |
266 | ||
267 | if (if_name) { | |
268 | os_strlcpy(iface->conf->bss[0]->iface, if_name, | |
269 | sizeof(iface->conf->bss[0]->iface)); | |
270 | } | |
271 | ||
272 | iface->interfaces = interfaces; | |
273 | ||
274 | for (k = 0; k < debug; k++) { | |
275 | if (iface->bss[0]->conf->logger_stdout_level > 0) | |
276 | iface->bss[0]->conf->logger_stdout_level--; | |
277 | } | |
278 | ||
279 | if (iface->conf->bss[0]->iface[0] == '\0' && | |
280 | !hostapd_drv_none(iface->bss[0])) { | |
281 | wpa_printf(MSG_ERROR, | |
282 | "Interface name not specified in %s, nor by '-i' parameter", | |
283 | config_fname); | |
284 | hostapd_interface_deinit_free(iface); | |
285 | return NULL; | |
286 | } | |
287 | ||
288 | return iface; | |
289 | } | |
290 | ||
291 | ||
292 | /** | |
293 | * handle_term - SIGINT and SIGTERM handler to terminate hostapd process | |
294 | */ | |
295 | static void handle_term(int sig, void *signal_ctx) | |
296 | { | |
297 | wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig); | |
298 | eloop_terminate(); | |
299 | } | |
300 | ||
301 | ||
302 | #ifndef CONFIG_NATIVE_WINDOWS | |
303 | ||
304 | static int handle_reload_iface(struct hostapd_iface *iface, void *ctx) | |
305 | { | |
306 | if (hostapd_reload_config(iface) < 0) { | |
307 | wpa_printf(MSG_WARNING, "Failed to read new configuration " | |
308 | "file - continuing with old."); | |
309 | } | |
310 | return 0; | |
311 | } | |
312 | ||
313 | ||
314 | /** | |
315 | * handle_reload - SIGHUP handler to reload configuration | |
316 | */ | |
317 | static void handle_reload(int sig, void *signal_ctx) | |
318 | { | |
319 | struct hapd_interfaces *interfaces = signal_ctx; | |
320 | wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration", | |
321 | sig); | |
322 | hostapd_for_each_interface(interfaces, handle_reload_iface, NULL); | |
323 | } | |
324 | ||
325 | ||
326 | static void handle_dump_state(int sig, void *signal_ctx) | |
327 | { | |
328 | /* Not used anymore - ignore signal */ | |
329 | } | |
330 | #endif /* CONFIG_NATIVE_WINDOWS */ | |
331 | ||
332 | ||
333 | static int hostapd_global_init(struct hapd_interfaces *interfaces, | |
334 | const char *entropy_file) | |
335 | { | |
336 | int i; | |
337 | ||
338 | os_memset(&global, 0, sizeof(global)); | |
339 | ||
340 | hostapd_logger_register_cb(hostapd_logger_cb); | |
341 | ||
342 | if (eap_server_register_methods()) { | |
343 | wpa_printf(MSG_ERROR, "Failed to register EAP methods"); | |
344 | return -1; | |
345 | } | |
346 | ||
347 | if (eloop_init()) { | |
348 | wpa_printf(MSG_ERROR, "Failed to initialize event loop"); | |
349 | return -1; | |
350 | } | |
351 | interfaces->eloop_initialized = 1; | |
352 | ||
353 | random_init(entropy_file); | |
354 | ||
355 | #ifndef CONFIG_NATIVE_WINDOWS | |
356 | eloop_register_signal(SIGHUP, handle_reload, interfaces); | |
357 | eloop_register_signal(SIGUSR1, handle_dump_state, interfaces); | |
358 | #endif /* CONFIG_NATIVE_WINDOWS */ | |
359 | eloop_register_signal_terminate(handle_term, interfaces); | |
360 | ||
361 | #ifndef CONFIG_NATIVE_WINDOWS | |
362 | openlog("hostapd", 0, LOG_DAEMON); | |
363 | #endif /* CONFIG_NATIVE_WINDOWS */ | |
364 | ||
365 | for (i = 0; wpa_drivers[i]; i++) | |
366 | global.drv_count++; | |
367 | if (global.drv_count == 0) { | |
368 | wpa_printf(MSG_ERROR, "No drivers enabled"); | |
369 | return -1; | |
370 | } | |
371 | global.drv_priv = os_calloc(global.drv_count, sizeof(void *)); | |
372 | if (global.drv_priv == NULL) | |
373 | return -1; | |
374 | ||
375 | return 0; | |
376 | } | |
377 | ||
378 | ||
379 | static void hostapd_global_deinit(const char *pid_file, int eloop_initialized) | |
380 | { | |
381 | int i; | |
382 | ||
383 | for (i = 0; wpa_drivers[i] && global.drv_priv; i++) { | |
384 | if (!global.drv_priv[i]) | |
385 | continue; | |
386 | wpa_drivers[i]->global_deinit(global.drv_priv[i]); | |
387 | } | |
388 | os_free(global.drv_priv); | |
389 | global.drv_priv = NULL; | |
390 | ||
391 | #ifdef EAP_SERVER_TNC | |
392 | tncs_global_deinit(); | |
393 | #endif /* EAP_SERVER_TNC */ | |
394 | ||
395 | random_deinit(); | |
396 | ||
397 | if (eloop_initialized) | |
398 | eloop_destroy(); | |
399 | ||
400 | #ifndef CONFIG_NATIVE_WINDOWS | |
401 | closelog(); | |
402 | #endif /* CONFIG_NATIVE_WINDOWS */ | |
403 | ||
404 | eap_server_unregister_methods(); | |
405 | ||
406 | os_daemonize_terminate(pid_file); | |
407 | } | |
408 | ||
409 | ||
410 | static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize, | |
411 | const char *pid_file) | |
412 | { | |
413 | #ifdef EAP_SERVER_TNC | |
414 | int tnc = 0; | |
415 | size_t i, k; | |
416 | ||
417 | for (i = 0; !tnc && i < ifaces->count; i++) { | |
418 | for (k = 0; k < ifaces->iface[i]->num_bss; k++) { | |
419 | if (ifaces->iface[i]->bss[0]->conf->tnc) { | |
420 | tnc++; | |
421 | break; | |
422 | } | |
423 | } | |
424 | } | |
425 | ||
426 | if (tnc && tncs_global_init() < 0) { | |
427 | wpa_printf(MSG_ERROR, "Failed to initialize TNCS"); | |
428 | return -1; | |
429 | } | |
430 | #endif /* EAP_SERVER_TNC */ | |
431 | ||
432 | if (daemonize) { | |
433 | if (os_daemonize(pid_file)) { | |
434 | wpa_printf(MSG_ERROR, "daemon: %s", strerror(errno)); | |
435 | return -1; | |
436 | } | |
437 | if (eloop_sock_requeue()) { | |
438 | wpa_printf(MSG_ERROR, "eloop_sock_requeue: %s", | |
439 | strerror(errno)); | |
440 | return -1; | |
441 | } | |
442 | } | |
443 | ||
444 | eloop_run(); | |
445 | ||
446 | return 0; | |
447 | } | |
448 | ||
449 | ||
450 | static void show_version(void) | |
451 | { | |
452 | fprintf(stderr, | |
453 | "hostapd v%s\n" | |
454 | "User space daemon for IEEE 802.11 AP management,\n" | |
455 | "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n" | |
456 | "Copyright (c) 2002-2019, Jouni Malinen <j@w1.fi> " | |
457 | "and contributors\n", | |
458 | VERSION_STR); | |
459 | } | |
460 | ||
461 | ||
462 | static void usage(void) | |
463 | { | |
464 | show_version(); | |
465 | fprintf(stderr, | |
466 | "\n" | |
467 | "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] " | |
468 | "\\\n" | |
469 | " [-g <global ctrl_iface>] [-G <group>]\\\n" | |
470 | " [-i <comma-separated list of interface names>]\\\n" | |
471 | " <configuration file(s)>\n" | |
472 | "\n" | |
473 | "options:\n" | |
474 | " -h show this usage\n" | |
475 | " -d show more debug messages (-dd for even more)\n" | |
476 | " -B run daemon in the background\n" | |
477 | " -e entropy file\n" | |
478 | " -g global control interface path\n" | |
479 | " -G group for control interfaces\n" | |
480 | " -P PID file\n" | |
481 | " -K include key data in debug messages\n" | |
482 | #ifdef CONFIG_DEBUG_FILE | |
483 | " -f log output to debug file instead of stdout\n" | |
484 | #endif /* CONFIG_DEBUG_FILE */ | |
485 | #ifdef CONFIG_DEBUG_LINUX_TRACING | |
486 | " -T record to Linux tracing in addition to logging\n" | |
487 | " (records all messages regardless of debug verbosity)\n" | |
488 | #endif /* CONFIG_DEBUG_LINUX_TRACING */ | |
489 | " -i list of interface names to use\n" | |
490 | #ifdef CONFIG_DEBUG_SYSLOG | |
491 | " -s log output to syslog instead of stdout\n" | |
492 | #endif /* CONFIG_DEBUG_SYSLOG */ | |
493 | " -S start all the interfaces synchronously\n" | |
494 | " -t include timestamps in some debug messages\n" | |
495 | " -v show hostapd version\n"); | |
496 | ||
497 | exit(1); | |
498 | } | |
499 | ||
500 | ||
501 | static const char * hostapd_msg_ifname_cb(void *ctx) | |
502 | { | |
503 | struct hostapd_data *hapd = ctx; | |
504 | if (hapd && hapd->conf) | |
505 | return hapd->conf->iface; | |
506 | return NULL; | |
507 | } | |
508 | ||
509 | ||
510 | static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces, | |
511 | const char *path) | |
512 | { | |
513 | #ifndef CONFIG_CTRL_IFACE_UDP | |
514 | char *pos; | |
515 | #endif /* !CONFIG_CTRL_IFACE_UDP */ | |
516 | ||
517 | os_free(interfaces->global_iface_path); | |
518 | interfaces->global_iface_path = os_strdup(path); | |
519 | if (interfaces->global_iface_path == NULL) | |
520 | return -1; | |
521 | ||
522 | #ifndef CONFIG_CTRL_IFACE_UDP | |
523 | pos = os_strrchr(interfaces->global_iface_path, '/'); | |
524 | if (pos == NULL) { | |
525 | wpa_printf(MSG_ERROR, "No '/' in the global control interface " | |
526 | "file"); | |
527 | os_free(interfaces->global_iface_path); | |
528 | interfaces->global_iface_path = NULL; | |
529 | return -1; | |
530 | } | |
531 | ||
532 | *pos = '\0'; | |
533 | interfaces->global_iface_name = pos + 1; | |
534 | #endif /* !CONFIG_CTRL_IFACE_UDP */ | |
535 | ||
536 | return 0; | |
537 | } | |
538 | ||
539 | ||
540 | static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces, | |
541 | const char *group) | |
542 | { | |
543 | #ifndef CONFIG_NATIVE_WINDOWS | |
544 | struct group *grp; | |
545 | grp = getgrnam(group); | |
546 | if (grp == NULL) { | |
547 | wpa_printf(MSG_ERROR, "Unknown group '%s'", group); | |
548 | return -1; | |
549 | } | |
550 | interfaces->ctrl_iface_group = grp->gr_gid; | |
551 | #endif /* CONFIG_NATIVE_WINDOWS */ | |
552 | return 0; | |
553 | } | |
554 | ||
555 | ||
556 | static int hostapd_get_interface_names(char ***if_names, | |
557 | size_t *if_names_size, | |
558 | char *arg) | |
559 | { | |
560 | char *if_name, *tmp, **nnames; | |
561 | size_t i; | |
562 | ||
563 | if (!arg) | |
564 | return -1; | |
565 | if_name = strtok_r(arg, ",", &tmp); | |
566 | ||
567 | while (if_name) { | |
568 | nnames = os_realloc_array(*if_names, 1 + *if_names_size, | |
569 | sizeof(char *)); | |
570 | if (!nnames) | |
571 | goto fail; | |
572 | *if_names = nnames; | |
573 | ||
574 | (*if_names)[*if_names_size] = os_strdup(if_name); | |
575 | if (!(*if_names)[*if_names_size]) | |
576 | goto fail; | |
577 | (*if_names_size)++; | |
578 | if_name = strtok_r(NULL, ",", &tmp); | |
579 | } | |
580 | ||
581 | return 0; | |
582 | ||
583 | fail: | |
584 | for (i = 0; i < *if_names_size; i++) | |
585 | os_free((*if_names)[i]); | |
586 | os_free(*if_names); | |
587 | *if_names = NULL; | |
588 | *if_names_size = 0; | |
589 | return -1; | |
590 | } | |
591 | ||
592 | ||
593 | #ifdef CONFIG_WPS | |
594 | static int gen_uuid(const char *txt_addr) | |
595 | { | |
596 | u8 addr[ETH_ALEN]; | |
597 | u8 uuid[UUID_LEN]; | |
598 | char buf[100]; | |
599 | ||
600 | if (hwaddr_aton(txt_addr, addr) < 0) | |
601 | return -1; | |
602 | ||
603 | uuid_gen_mac_addr(addr, uuid); | |
604 | if (uuid_bin2str(uuid, buf, sizeof(buf)) < 0) | |
605 | return -1; | |
606 | ||
607 | printf("%s\n", buf); | |
608 | ||
609 | return 0; | |
610 | } | |
611 | #endif /* CONFIG_WPS */ | |
612 | ||
613 | ||
614 | #ifndef HOSTAPD_CLEANUP_INTERVAL | |
615 | #define HOSTAPD_CLEANUP_INTERVAL 10 | |
616 | #endif /* HOSTAPD_CLEANUP_INTERVAL */ | |
617 | ||
618 | static int hostapd_periodic_call(struct hostapd_iface *iface, void *ctx) | |
619 | { | |
620 | hostapd_periodic_iface(iface); | |
621 | return 0; | |
622 | } | |
623 | ||
624 | ||
625 | /* Periodic cleanup tasks */ | |
626 | static void hostapd_periodic(void *eloop_ctx, void *timeout_ctx) | |
627 | { | |
628 | struct hapd_interfaces *interfaces = eloop_ctx; | |
629 | ||
630 | eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0, | |
631 | hostapd_periodic, interfaces, NULL); | |
632 | hostapd_for_each_interface(interfaces, hostapd_periodic_call, NULL); | |
633 | } | |
634 | ||
635 | ||
636 | int main(int argc, char *argv[]) | |
637 | { | |
638 | struct hapd_interfaces interfaces; | |
639 | int ret = 1; | |
640 | size_t i, j; | |
641 | int c, debug = 0, daemonize = 0; | |
642 | char *pid_file = NULL; | |
643 | const char *log_file = NULL; | |
644 | const char *entropy_file = NULL; | |
645 | char **bss_config = NULL, **tmp_bss; | |
646 | size_t num_bss_configs = 0; | |
647 | #ifdef CONFIG_DEBUG_LINUX_TRACING | |
648 | int enable_trace_dbg = 0; | |
649 | #endif /* CONFIG_DEBUG_LINUX_TRACING */ | |
650 | int start_ifaces_in_sync = 0; | |
651 | char **if_names = NULL; | |
652 | size_t if_names_size = 0; | |
653 | #ifdef CONFIG_DPP | |
654 | struct dpp_global_config dpp_conf; | |
655 | #endif /* CONFIG_DPP */ | |
656 | ||
657 | if (os_program_init()) | |
658 | return -1; | |
659 | ||
660 | os_memset(&interfaces, 0, sizeof(interfaces)); | |
661 | interfaces.reload_config = hostapd_reload_config; | |
662 | interfaces.config_read_cb = hostapd_config_read; | |
663 | interfaces.for_each_interface = hostapd_for_each_interface; | |
664 | interfaces.ctrl_iface_init = hostapd_ctrl_iface_init; | |
665 | interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit; | |
666 | interfaces.driver_init = hostapd_driver_init; | |
667 | interfaces.global_iface_path = NULL; | |
668 | interfaces.global_iface_name = NULL; | |
669 | interfaces.global_ctrl_sock = -1; | |
670 | dl_list_init(&interfaces.global_ctrl_dst); | |
671 | #ifdef CONFIG_ETH_P_OUI | |
672 | dl_list_init(&interfaces.eth_p_oui); | |
673 | #endif /* CONFIG_ETH_P_OUI */ | |
674 | #ifdef CONFIG_DPP | |
675 | os_memset(&dpp_conf, 0, sizeof(dpp_conf)); | |
676 | /* TODO: dpp_conf.msg_ctx? */ | |
677 | interfaces.dpp = dpp_global_init(&dpp_conf); | |
678 | if (!interfaces.dpp) | |
679 | return -1; | |
680 | #endif /* CONFIG_DPP */ | |
681 | ||
682 | for (;;) { | |
683 | c = getopt(argc, argv, "b:Bde:f:hi:KP:sSTtu:vg:G:"); | |
684 | if (c < 0) | |
685 | break; | |
686 | switch (c) { | |
687 | case 'h': | |
688 | usage(); | |
689 | break; | |
690 | case 'd': | |
691 | debug++; | |
692 | if (wpa_debug_level > 0) | |
693 | wpa_debug_level--; | |
694 | break; | |
695 | case 'B': | |
696 | daemonize++; | |
697 | break; | |
698 | case 'e': | |
699 | entropy_file = optarg; | |
700 | break; | |
701 | case 'f': | |
702 | log_file = optarg; | |
703 | break; | |
704 | case 'K': | |
705 | wpa_debug_show_keys++; | |
706 | break; | |
707 | case 'P': | |
708 | os_free(pid_file); | |
709 | pid_file = os_rel2abs_path(optarg); | |
710 | break; | |
711 | case 't': | |
712 | wpa_debug_timestamp++; | |
713 | break; | |
714 | #ifdef CONFIG_DEBUG_LINUX_TRACING | |
715 | case 'T': | |
716 | enable_trace_dbg = 1; | |
717 | break; | |
718 | #endif /* CONFIG_DEBUG_LINUX_TRACING */ | |
719 | case 'v': | |
720 | show_version(); | |
721 | exit(1); | |
722 | break; | |
723 | case 'g': | |
724 | if (hostapd_get_global_ctrl_iface(&interfaces, optarg)) | |
725 | return -1; | |
726 | break; | |
727 | case 'G': | |
728 | if (hostapd_get_ctrl_iface_group(&interfaces, optarg)) | |
729 | return -1; | |
730 | break; | |
731 | case 'b': | |
732 | tmp_bss = os_realloc_array(bss_config, | |
733 | num_bss_configs + 1, | |
734 | sizeof(char *)); | |
735 | if (tmp_bss == NULL) | |
736 | goto out; | |
737 | bss_config = tmp_bss; | |
738 | bss_config[num_bss_configs++] = optarg; | |
739 | break; | |
740 | #ifdef CONFIG_DEBUG_SYSLOG | |
741 | case 's': | |
742 | wpa_debug_syslog = 1; | |
743 | break; | |
744 | #endif /* CONFIG_DEBUG_SYSLOG */ | |
745 | case 'S': | |
746 | start_ifaces_in_sync = 1; | |
747 | break; | |
748 | #ifdef CONFIG_WPS | |
749 | case 'u': | |
750 | return gen_uuid(optarg); | |
751 | #endif /* CONFIG_WPS */ | |
752 | case 'i': | |
753 | if (hostapd_get_interface_names(&if_names, | |
754 | &if_names_size, optarg)) | |
755 | goto out; | |
756 | break; | |
757 | default: | |
758 | usage(); | |
759 | break; | |
760 | } | |
761 | } | |
762 | ||
763 | if (optind == argc && interfaces.global_iface_path == NULL && | |
764 | num_bss_configs == 0) | |
765 | usage(); | |
766 | ||
767 | wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb); | |
768 | ||
769 | if (log_file) | |
770 | wpa_debug_open_file(log_file); | |
771 | if (!log_file && !wpa_debug_syslog) | |
772 | wpa_debug_setup_stdout(); | |
773 | #ifdef CONFIG_DEBUG_SYSLOG | |
774 | if (wpa_debug_syslog) | |
775 | wpa_debug_open_syslog(); | |
776 | #endif /* CONFIG_DEBUG_SYSLOG */ | |
777 | #ifdef CONFIG_DEBUG_LINUX_TRACING | |
778 | if (enable_trace_dbg) { | |
779 | int tret = wpa_debug_open_linux_tracing(); | |
780 | if (tret) { | |
781 | wpa_printf(MSG_ERROR, "Failed to enable trace logging"); | |
782 | return -1; | |
783 | } | |
784 | } | |
785 | #endif /* CONFIG_DEBUG_LINUX_TRACING */ | |
786 | ||
787 | interfaces.count = argc - optind; | |
788 | if (interfaces.count || num_bss_configs) { | |
789 | interfaces.iface = os_calloc(interfaces.count + num_bss_configs, | |
790 | sizeof(struct hostapd_iface *)); | |
791 | if (interfaces.iface == NULL) { | |
792 | wpa_printf(MSG_ERROR, "malloc failed"); | |
793 | return -1; | |
794 | } | |
795 | } | |
796 | ||
797 | if (hostapd_global_init(&interfaces, entropy_file)) { | |
798 | wpa_printf(MSG_ERROR, "Failed to initialize global context"); | |
799 | return -1; | |
800 | } | |
801 | ||
802 | eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0, | |
803 | hostapd_periodic, &interfaces, NULL); | |
804 | ||
805 | if (fst_global_init()) { | |
806 | wpa_printf(MSG_ERROR, | |
807 | "Failed to initialize global FST context"); | |
808 | goto out; | |
809 | } | |
810 | ||
811 | #if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE) | |
812 | if (!fst_global_add_ctrl(fst_ctrl_cli)) | |
813 | wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl"); | |
814 | #endif /* CONFIG_FST && CONFIG_CTRL_IFACE */ | |
815 | ||
816 | /* Allocate and parse configuration for full interface files */ | |
817 | for (i = 0; i < interfaces.count; i++) { | |
818 | char *if_name = NULL; | |
819 | ||
820 | if (i < if_names_size) | |
821 | if_name = if_names[i]; | |
822 | ||
823 | interfaces.iface[i] = hostapd_interface_init(&interfaces, | |
824 | if_name, | |
825 | argv[optind + i], | |
826 | debug); | |
827 | if (!interfaces.iface[i]) { | |
828 | wpa_printf(MSG_ERROR, "Failed to initialize interface"); | |
829 | goto out; | |
830 | } | |
831 | if (start_ifaces_in_sync) | |
832 | interfaces.iface[i]->need_to_start_in_sync = 1; | |
833 | } | |
834 | ||
835 | /* Allocate and parse configuration for per-BSS files */ | |
836 | for (i = 0; i < num_bss_configs; i++) { | |
837 | struct hostapd_iface *iface; | |
838 | char *fname; | |
839 | ||
840 | wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]); | |
841 | fname = os_strchr(bss_config[i], ':'); | |
842 | if (fname == NULL) { | |
843 | wpa_printf(MSG_ERROR, | |
844 | "Invalid BSS config identifier '%s'", | |
845 | bss_config[i]); | |
846 | goto out; | |
847 | } | |
848 | *fname++ = '\0'; | |
849 | iface = hostapd_interface_init_bss(&interfaces, bss_config[i], | |
850 | fname, debug); | |
851 | if (iface == NULL) | |
852 | goto out; | |
853 | for (j = 0; j < interfaces.count; j++) { | |
854 | if (interfaces.iface[j] == iface) | |
855 | break; | |
856 | } | |
857 | if (j == interfaces.count) { | |
858 | struct hostapd_iface **tmp; | |
859 | tmp = os_realloc_array(interfaces.iface, | |
860 | interfaces.count + 1, | |
861 | sizeof(struct hostapd_iface *)); | |
862 | if (tmp == NULL) { | |
863 | hostapd_interface_deinit_free(iface); | |
864 | goto out; | |
865 | } | |
866 | interfaces.iface = tmp; | |
867 | interfaces.iface[interfaces.count++] = iface; | |
868 | } | |
869 | } | |
870 | ||
871 | /* | |
872 | * Enable configured interfaces. Depending on channel configuration, | |
873 | * this may complete full initialization before returning or use a | |
874 | * callback mechanism to complete setup in case of operations like HT | |
875 | * co-ex scans, ACS, or DFS are needed to determine channel parameters. | |
876 | * In such case, the interface will be enabled from eloop context within | |
877 | * hostapd_global_run(). | |
878 | */ | |
879 | interfaces.terminate_on_error = interfaces.count; | |
880 | for (i = 0; i < interfaces.count; i++) { | |
881 | if (hostapd_driver_init(interfaces.iface[i]) || | |
882 | hostapd_setup_interface(interfaces.iface[i])) | |
883 | goto out; | |
884 | } | |
885 | ||
886 | hostapd_global_ctrl_iface_init(&interfaces); | |
887 | ||
888 | if (hostapd_global_run(&interfaces, daemonize, pid_file)) { | |
889 | wpa_printf(MSG_ERROR, "Failed to start eloop"); | |
890 | goto out; | |
891 | } | |
892 | ||
893 | ret = 0; | |
894 | ||
895 | out: | |
896 | hostapd_global_ctrl_iface_deinit(&interfaces); | |
897 | /* Deinitialize all interfaces */ | |
898 | for (i = 0; i < interfaces.count; i++) { | |
899 | if (!interfaces.iface[i]) | |
900 | continue; | |
901 | interfaces.iface[i]->driver_ap_teardown = | |
902 | !!(interfaces.iface[i]->drv_flags & | |
903 | WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT); | |
904 | hostapd_interface_deinit_free(interfaces.iface[i]); | |
905 | } | |
906 | os_free(interfaces.iface); | |
907 | ||
908 | #ifdef CONFIG_DPP | |
909 | dpp_global_deinit(interfaces.dpp); | |
910 | #endif /* CONFIG_DPP */ | |
911 | ||
912 | if (interfaces.eloop_initialized) | |
913 | eloop_cancel_timeout(hostapd_periodic, &interfaces, NULL); | |
914 | hostapd_global_deinit(pid_file, interfaces.eloop_initialized); | |
915 | os_free(pid_file); | |
916 | ||
917 | wpa_debug_close_syslog(); | |
918 | if (log_file) | |
919 | wpa_debug_close_file(); | |
920 | wpa_debug_close_linux_tracing(); | |
921 | ||
922 | os_free(bss_config); | |
923 | ||
924 | for (i = 0; i < if_names_size; i++) | |
925 | os_free(if_names[i]); | |
926 | os_free(if_names); | |
927 | ||
928 | fst_global_deinit(); | |
929 | ||
930 | os_program_deinit(); | |
931 | ||
932 | return ret; | |
933 | } |