LCOV - code coverage report
Current view: top level - hostapd - main.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1388943092 Lines: 226 355 63.7 %
Date: 2014-01-05 Functions: 11 16 68.8 %
Branches: 114 211 54.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * hostapd / main()
       3                 :            :  * Copyright (c) 2002-2011, 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 "crypto/random.h"
      18                 :            : #include "crypto/tls.h"
      19                 :            : #include "common/version.h"
      20                 :            : #include "drivers/driver.h"
      21                 :            : #include "eap_server/eap.h"
      22                 :            : #include "eap_server/tncs.h"
      23                 :            : #include "ap/hostapd.h"
      24                 :            : #include "ap/ap_config.h"
      25                 :            : #include "ap/ap_drv_ops.h"
      26                 :            : #include "config_file.h"
      27                 :            : #include "eap_register.h"
      28                 :            : #include "ctrl_iface.h"
      29                 :            : 
      30                 :            : 
      31                 :            : struct hapd_global {
      32                 :            :         void **drv_priv;
      33                 :            :         size_t drv_count;
      34                 :            : };
      35                 :            : 
      36                 :            : static struct hapd_global global;
      37                 :            : 
      38                 :            : 
      39                 :            : #ifndef CONFIG_NO_HOSTAPD_LOGGER
      40                 :      12594 : static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
      41                 :            :                               int level, const char *txt, size_t len)
      42                 :            : {
      43                 :      12594 :         struct hostapd_data *hapd = ctx;
      44                 :            :         char *format, *module_str;
      45                 :            :         int maxlen;
      46                 :            :         int conf_syslog_level, conf_stdout_level;
      47                 :            :         unsigned int conf_syslog, conf_stdout;
      48                 :            : 
      49                 :      12594 :         maxlen = len + 100;
      50                 :      12594 :         format = os_malloc(maxlen);
      51         [ -  + ]:      12594 :         if (!format)
      52                 :      12594 :                 return;
      53                 :            : 
      54 [ +  - ][ +  - ]:      12594 :         if (hapd && hapd->conf) {
      55                 :      12594 :                 conf_syslog_level = hapd->conf->logger_syslog_level;
      56                 :      12594 :                 conf_stdout_level = hapd->conf->logger_stdout_level;
      57                 :      12594 :                 conf_syslog = hapd->conf->logger_syslog;
      58                 :      12594 :                 conf_stdout = hapd->conf->logger_stdout;
      59                 :            :         } else {
      60                 :          0 :                 conf_syslog_level = conf_stdout_level = 0;
      61                 :          0 :                 conf_syslog = conf_stdout = (unsigned int) -1;
      62                 :            :         }
      63                 :            : 
      64   [ +  +  +  +  :      12594 :         switch (module) {
             -  -  +  - ]
      65                 :            :         case HOSTAPD_MODULE_IEEE80211:
      66                 :       1621 :                 module_str = "IEEE 802.11";
      67                 :       1621 :                 break;
      68                 :            :         case HOSTAPD_MODULE_IEEE8021X:
      69                 :       3620 :                 module_str = "IEEE 802.1X";
      70                 :       3620 :                 break;
      71                 :            :         case HOSTAPD_MODULE_RADIUS:
      72                 :       3560 :                 module_str = "RADIUS";
      73                 :       3560 :                 break;
      74                 :            :         case HOSTAPD_MODULE_WPA:
      75                 :       1853 :                 module_str = "WPA";
      76                 :       1853 :                 break;
      77                 :            :         case HOSTAPD_MODULE_DRIVER:
      78                 :          0 :                 module_str = "DRIVER";
      79                 :          0 :                 break;
      80                 :            :         case HOSTAPD_MODULE_IAPP:
      81                 :          0 :                 module_str = "IAPP";
      82                 :          0 :                 break;
      83                 :            :         case HOSTAPD_MODULE_MLME:
      84                 :       1940 :                 module_str = "MLME";
      85                 :       1940 :                 break;
      86                 :            :         default:
      87                 :          0 :                 module_str = NULL;
      88                 :          0 :                 break;
      89                 :            :         }
      90                 :            : 
      91 [ +  - ][ +  - ]:      12594 :         if (hapd && hapd->conf && addr)
                 [ +  + ]
      92         [ +  - ]:       9963 :                 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
      93                 :      69741 :                             hapd->conf->iface, MAC2STR(addr),
      94                 :            :                             module_str ? " " : "", module_str, txt);
      95 [ +  - ][ +  - ]:       2631 :         else if (hapd && hapd->conf)
      96         [ +  - ]:       2631 :                 os_snprintf(format, maxlen, "%s:%s%s %s",
      97                 :       2631 :                             hapd->conf->iface, module_str ? " " : "",
      98                 :            :                             module_str, txt);
      99         [ #  # ]:          0 :         else if (addr)
     100         [ #  # ]:          0 :                 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
     101                 :          0 :                             MAC2STR(addr), module_str ? " " : "",
     102                 :            :                             module_str, txt);
     103                 :            :         else
     104         [ #  # ]:          0 :                 os_snprintf(format, maxlen, "%s%s%s",
     105                 :            :                             module_str, module_str ? ": " : "", txt);
     106                 :            : 
     107 [ +  - ][ +  + ]:      12594 :         if ((conf_stdout & module) && level >= conf_stdout_level) {
     108                 :      12113 :                 wpa_debug_print_timestamp();
     109                 :      12113 :                 wpa_printf(MSG_INFO, "%s", format);
     110                 :            :         }
     111                 :            : 
     112                 :            : #ifndef CONFIG_NATIVE_WINDOWS
     113 [ +  - ][ +  + ]:      12594 :         if ((conf_syslog & module) && level >= conf_syslog_level) {
     114                 :            :                 int priority;
     115   [ -  +  -  +  :       1437 :                 switch (level) {
                      - ]
     116                 :            :                 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
     117                 :            :                 case HOSTAPD_LEVEL_DEBUG:
     118                 :          0 :                         priority = LOG_DEBUG;
     119                 :          0 :                         break;
     120                 :            :                 case HOSTAPD_LEVEL_INFO:
     121                 :       1380 :                         priority = LOG_INFO;
     122                 :       1380 :                         break;
     123                 :            :                 case HOSTAPD_LEVEL_NOTICE:
     124                 :          0 :                         priority = LOG_NOTICE;
     125                 :          0 :                         break;
     126                 :            :                 case HOSTAPD_LEVEL_WARNING:
     127                 :         57 :                         priority = LOG_WARNING;
     128                 :         57 :                         break;
     129                 :            :                 default:
     130                 :          0 :                         priority = LOG_INFO;
     131                 :          0 :                         break;
     132                 :            :                 }
     133                 :       1437 :                 syslog(priority, "%s", format);
     134                 :            :         }
     135                 :            : #endif /* CONFIG_NATIVE_WINDOWS */
     136                 :            : 
     137                 :      12594 :         os_free(format);
     138                 :            : }
     139                 :            : #endif /* CONFIG_NO_HOSTAPD_LOGGER */
     140                 :            : 
     141                 :            : 
     142                 :            : /**
     143                 :            :  * hostapd_driver_init - Preparate driver interface
     144                 :            :  */
     145                 :        204 : static int hostapd_driver_init(struct hostapd_iface *iface)
     146                 :            : {
     147                 :            :         struct wpa_init_params params;
     148                 :            :         size_t i;
     149                 :        204 :         struct hostapd_data *hapd = iface->bss[0];
     150                 :        204 :         struct hostapd_bss_config *conf = hapd->conf;
     151                 :        204 :         u8 *b = conf->bssid;
     152                 :            :         struct wpa_driver_capa capa;
     153                 :            : 
     154 [ +  - ][ -  + ]:        204 :         if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
     155                 :          0 :                 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
     156                 :          0 :                 return -1;
     157                 :            :         }
     158                 :            : 
     159                 :            :         /* Initialize the driver interface */
     160         [ +  + ]:        204 :         if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
     161                 :        198 :                 b = NULL;
     162                 :            : 
     163                 :        204 :         os_memset(&params, 0, sizeof(params));
     164         [ +  - ]:        206 :         for (i = 0; wpa_drivers[i]; i++) {
     165         [ +  + ]:        206 :                 if (wpa_drivers[i] != hapd->driver)
     166                 :          2 :                         continue;
     167                 :            : 
     168 [ +  + ][ +  + ]:        204 :                 if (global.drv_priv[i] == NULL &&
     169                 :          2 :                     wpa_drivers[i]->global_init) {
     170                 :          1 :                         global.drv_priv[i] = wpa_drivers[i]->global_init();
     171         [ -  + ]:          1 :                         if (global.drv_priv[i] == NULL) {
     172                 :          0 :                                 wpa_printf(MSG_ERROR, "Failed to initialize "
     173                 :            :                                            "driver '%s'",
     174                 :          0 :                                            wpa_drivers[i]->name);
     175                 :          0 :                                 return -1;
     176                 :            :                         }
     177                 :            :                 }
     178                 :            : 
     179                 :        204 :                 params.global_priv = global.drv_priv[i];
     180                 :        204 :                 break;
     181                 :            :         }
     182                 :        204 :         params.bssid = b;
     183                 :        204 :         params.ifname = hapd->conf->iface;
     184                 :        204 :         params.ssid = hapd->conf->ssid.ssid;
     185                 :        204 :         params.ssid_len = hapd->conf->ssid.ssid_len;
     186                 :        204 :         params.test_socket = hapd->conf->test_socket;
     187                 :        204 :         params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
     188                 :            : 
     189                 :        204 :         params.num_bridge = hapd->iface->num_bss;
     190                 :        204 :         params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
     191         [ -  + ]:        204 :         if (params.bridge == NULL)
     192                 :          0 :                 return -1;
     193         [ +  + ]:        414 :         for (i = 0; i < hapd->iface->num_bss; i++) {
     194                 :        210 :                 struct hostapd_data *bss = hapd->iface->bss[i];
     195         [ -  + ]:        210 :                 if (bss->conf->bridge[0])
     196                 :          0 :                         params.bridge[i] = bss->conf->bridge;
     197                 :            :         }
     198                 :            : 
     199                 :        204 :         params.own_addr = hapd->own_addr;
     200                 :            : 
     201                 :        204 :         hapd->drv_priv = hapd->driver->hapd_init(hapd, &params);
     202                 :        204 :         os_free(params.bridge);
     203         [ -  + ]:        204 :         if (hapd->drv_priv == NULL) {
     204                 :          0 :                 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
     205                 :          0 :                            hapd->driver->name);
     206                 :          0 :                 hapd->driver = NULL;
     207                 :          0 :                 return -1;
     208                 :            :         }
     209                 :            : 
     210   [ +  +  +  - ]:        407 :         if (hapd->driver->get_capa &&
     211                 :        203 :             hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
     212                 :        203 :                 iface->drv_flags = capa.flags;
     213                 :        203 :                 iface->probe_resp_offloads = capa.probe_resp_offloads;
     214                 :        203 :                 iface->extended_capa = capa.extended_capa;
     215                 :        203 :                 iface->extended_capa_mask = capa.extended_capa_mask;
     216                 :        203 :                 iface->extended_capa_len = capa.extended_capa_len;
     217                 :        203 :                 iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
     218                 :            :         }
     219                 :            : 
     220                 :        204 :         return 0;
     221                 :            : }
     222                 :            : 
     223                 :            : 
     224                 :            : /**
     225                 :            :  * hostapd_interface_init - Read configuration file and init BSS data
     226                 :            :  *
     227                 :            :  * This function is used to parse configuration file for a full interface (one
     228                 :            :  * or more BSSes sharing the same radio) and allocate memory for the BSS
     229                 :            :  * interfaces. No actiual driver operations are started.
     230                 :            :  */
     231                 :            : static struct hostapd_iface *
     232                 :          1 : hostapd_interface_init(struct hapd_interfaces *interfaces,
     233                 :            :                        const char *config_fname, int debug)
     234                 :            : {
     235                 :            :         struct hostapd_iface *iface;
     236                 :            :         int k;
     237                 :            : 
     238                 :          1 :         wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
     239                 :          1 :         iface = hostapd_init(interfaces, config_fname);
     240         [ -  + ]:          1 :         if (!iface)
     241                 :          0 :                 return NULL;
     242                 :          1 :         iface->interfaces = interfaces;
     243                 :            : 
     244         [ +  + ]:          3 :         for (k = 0; k < debug; k++) {
     245         [ +  - ]:          2 :                 if (iface->bss[0]->conf->logger_stdout_level > 0)
     246                 :          2 :                         iface->bss[0]->conf->logger_stdout_level--;
     247                 :            :         }
     248                 :            : 
     249   [ +  -  -  + ]:          2 :         if (iface->conf->bss[0]->iface[0] == '\0' &&
     250                 :          1 :             !hostapd_drv_none(iface->bss[0])) {
     251                 :          0 :                 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
     252                 :            :                            config_fname);
     253                 :          0 :                 hostapd_interface_deinit_free(iface);
     254                 :          0 :                 return NULL;
     255                 :            :         }
     256                 :            : 
     257                 :          1 :         return iface;
     258                 :            : }
     259                 :            : 
     260                 :            : 
     261                 :            : /**
     262                 :            :  * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
     263                 :            :  */
     264                 :          2 : static void handle_term(int sig, void *signal_ctx)
     265                 :            : {
     266                 :          2 :         wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
     267                 :          2 :         eloop_terminate();
     268                 :          2 : }
     269                 :            : 
     270                 :            : 
     271                 :            : #ifndef CONFIG_NATIVE_WINDOWS
     272                 :            : 
     273                 :          0 : static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
     274                 :            : {
     275         [ #  # ]:          0 :         if (hostapd_reload_config(iface) < 0) {
     276                 :          0 :                 wpa_printf(MSG_WARNING, "Failed to read new configuration "
     277                 :            :                            "file - continuing with old.");
     278                 :            :         }
     279                 :          0 :         return 0;
     280                 :            : }
     281                 :            : 
     282                 :            : 
     283                 :            : /**
     284                 :            :  * handle_reload - SIGHUP handler to reload configuration
     285                 :            :  */
     286                 :          0 : static void handle_reload(int sig, void *signal_ctx)
     287                 :            : {
     288                 :          0 :         struct hapd_interfaces *interfaces = signal_ctx;
     289                 :          0 :         wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
     290                 :            :                    sig);
     291                 :          0 :         hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
     292                 :          0 : }
     293                 :            : 
     294                 :            : 
     295                 :          0 : static void handle_dump_state(int sig, void *signal_ctx)
     296                 :            : {
     297                 :            :         /* Not used anymore - ignore signal */
     298                 :          0 : }
     299                 :            : #endif /* CONFIG_NATIVE_WINDOWS */
     300                 :            : 
     301                 :            : 
     302                 :          2 : static int hostapd_global_init(struct hapd_interfaces *interfaces,
     303                 :            :                                const char *entropy_file)
     304                 :            : {
     305                 :            :         int i;
     306                 :            : 
     307                 :          2 :         os_memset(&global, 0, sizeof(global));
     308                 :            : 
     309                 :          2 :         hostapd_logger_register_cb(hostapd_logger_cb);
     310                 :            : 
     311         [ -  + ]:          2 :         if (eap_server_register_methods()) {
     312                 :          0 :                 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
     313                 :          0 :                 return -1;
     314                 :            :         }
     315                 :            : 
     316         [ -  + ]:          2 :         if (eloop_init()) {
     317                 :          0 :                 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
     318                 :          0 :                 return -1;
     319                 :            :         }
     320                 :            : 
     321                 :            :         random_init(entropy_file);
     322                 :            : 
     323                 :            : #ifndef CONFIG_NATIVE_WINDOWS
     324                 :          2 :         eloop_register_signal(SIGHUP, handle_reload, interfaces);
     325                 :          2 :         eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
     326                 :            : #endif /* CONFIG_NATIVE_WINDOWS */
     327                 :          2 :         eloop_register_signal_terminate(handle_term, interfaces);
     328                 :            : 
     329                 :            : #ifndef CONFIG_NATIVE_WINDOWS
     330                 :          2 :         openlog("hostapd", 0, LOG_DAEMON);
     331                 :            : #endif /* CONFIG_NATIVE_WINDOWS */
     332                 :            : 
     333         [ +  + ]:          8 :         for (i = 0; wpa_drivers[i]; i++)
     334                 :          6 :                 global.drv_count++;
     335         [ -  + ]:          2 :         if (global.drv_count == 0) {
     336                 :          0 :                 wpa_printf(MSG_ERROR, "No drivers enabled");
     337                 :          0 :                 return -1;
     338                 :            :         }
     339                 :          2 :         global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
     340         [ -  + ]:          2 :         if (global.drv_priv == NULL)
     341                 :          0 :                 return -1;
     342                 :            : 
     343                 :          2 :         return 0;
     344                 :            : }
     345                 :            : 
     346                 :            : 
     347                 :          2 : static void hostapd_global_deinit(const char *pid_file)
     348                 :            : {
     349                 :            :         int i;
     350                 :            : 
     351 [ +  + ][ +  - ]:          8 :         for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
     352         [ +  + ]:          6 :                 if (!global.drv_priv[i])
     353                 :          5 :                         continue;
     354                 :          1 :                 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
     355                 :            :         }
     356                 :          2 :         os_free(global.drv_priv);
     357                 :          2 :         global.drv_priv = NULL;
     358                 :            : 
     359                 :            : #ifdef EAP_SERVER_TNC
     360                 :          2 :         tncs_global_deinit();
     361                 :            : #endif /* EAP_SERVER_TNC */
     362                 :            : 
     363                 :            :         random_deinit();
     364                 :            : 
     365                 :          2 :         eloop_destroy();
     366                 :            : 
     367                 :            : #ifndef CONFIG_NATIVE_WINDOWS
     368                 :          2 :         closelog();
     369                 :            : #endif /* CONFIG_NATIVE_WINDOWS */
     370                 :            : 
     371                 :          2 :         eap_server_unregister_methods();
     372                 :            : 
     373                 :          2 :         os_daemonize_terminate(pid_file);
     374                 :          2 : }
     375                 :            : 
     376                 :            : 
     377                 :          2 : static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
     378                 :            :                               const char *pid_file)
     379                 :            : {
     380                 :            : #ifdef EAP_SERVER_TNC
     381                 :          2 :         int tnc = 0;
     382                 :            :         size_t i, k;
     383                 :            : 
     384 [ +  - ][ +  + ]:          3 :         for (i = 0; !tnc && i < ifaces->count; i++) {
     385         [ +  + ]:          2 :                 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
     386         [ -  + ]:          1 :                         if (ifaces->iface[i]->bss[0]->conf->tnc) {
     387                 :          0 :                                 tnc++;
     388                 :          0 :                                 break;
     389                 :            :                         }
     390                 :            :                 }
     391                 :            :         }
     392                 :            : 
     393 [ -  + ][ #  # ]:          2 :         if (tnc && tncs_global_init() < 0) {
     394                 :          0 :                 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
     395                 :          0 :                 return -1;
     396                 :            :         }
     397                 :            : #endif /* EAP_SERVER_TNC */
     398                 :            : 
     399 [ -  + ][ #  # ]:          2 :         if (daemonize && os_daemonize(pid_file)) {
     400                 :          0 :                 perror("daemon");
     401                 :          0 :                 return -1;
     402                 :            :         }
     403                 :            : 
     404                 :          2 :         eloop_run();
     405                 :            : 
     406                 :          2 :         return 0;
     407                 :            : }
     408                 :            : 
     409                 :            : 
     410                 :          0 : static void show_version(void)
     411                 :            : {
     412                 :          0 :         fprintf(stderr,
     413                 :            :                 "hostapd v" VERSION_STR "\n"
     414                 :            :                 "User space daemon for IEEE 802.11 AP management,\n"
     415                 :            :                 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
     416                 :            :                 "Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi> "
     417                 :            :                 "and contributors\n");
     418                 :          0 : }
     419                 :            : 
     420                 :            : 
     421                 :          0 : static void usage(void)
     422                 :            : {
     423                 :          0 :         show_version();
     424                 :          0 :         fprintf(stderr,
     425                 :            :                 "\n"
     426                 :            :                 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
     427                 :            :                 "\\\n"
     428                 :            :                 "         [-g <global ctrl_iface>] [-G <group>] \\\n"
     429                 :            :                 "         <configuration file(s)>\n"
     430                 :            :                 "\n"
     431                 :            :                 "options:\n"
     432                 :            :                 "   -h   show this usage\n"
     433                 :            :                 "   -d   show more debug messages (-dd for even more)\n"
     434                 :            :                 "   -B   run daemon in the background\n"
     435                 :            :                 "   -e   entropy file\n"
     436                 :            :                 "   -g   global control interface path\n"
     437                 :            :                 "   -G   group for control interfaces\n"
     438                 :            :                 "   -P   PID file\n"
     439                 :            :                 "   -K   include key data in debug messages\n"
     440                 :            : #ifdef CONFIG_DEBUG_FILE
     441                 :            :                 "   -f   log output to debug file instead of stdout\n"
     442                 :            : #endif /* CONFIG_DEBUG_FILE */
     443                 :            : #ifdef CONFIG_DEBUG_LINUX_TRACING
     444                 :            :                 "   -T = record to Linux tracing in addition to logging\n"
     445                 :            :                 "        (records all messages regardless of debug verbosity)\n"
     446                 :            : #endif /* CONFIG_DEBUG_LINUX_TRACING */
     447                 :            :                 "   -t   include timestamps in some debug messages\n"
     448                 :            :                 "   -v   show hostapd version\n");
     449                 :            : 
     450                 :          0 :         exit(1);
     451                 :            : }
     452                 :            : 
     453                 :            : 
     454                 :      10043 : static const char * hostapd_msg_ifname_cb(void *ctx)
     455                 :            : {
     456                 :      10043 :         struct hostapd_data *hapd = ctx;
     457 [ +  - ][ +  - ]:      10043 :         if (hapd && hapd->iconf && hapd->iconf->bss &&
         [ +  - ][ +  - ]
     458         [ +  - ]:      10043 :             hapd->iconf->num_bss > 0 && hapd->iconf->bss[0])
     459                 :      10043 :                 return hapd->iconf->bss[0]->iface;
     460                 :      10043 :         return NULL;
     461                 :            : }
     462                 :            : 
     463                 :            : 
     464                 :          1 : static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
     465                 :            :                                          const char *path)
     466                 :            : {
     467                 :            :         char *pos;
     468                 :          1 :         os_free(interfaces->global_iface_path);
     469                 :          1 :         interfaces->global_iface_path = os_strdup(path);
     470         [ -  + ]:          1 :         if (interfaces->global_iface_path == NULL)
     471                 :          0 :                 return -1;
     472                 :          1 :         pos = os_strrchr(interfaces->global_iface_path, '/');
     473         [ -  + ]:          1 :         if (pos == NULL) {
     474                 :          0 :                 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
     475                 :            :                            "file");
     476                 :          0 :                 os_free(interfaces->global_iface_path);
     477                 :          0 :                 interfaces->global_iface_path = NULL;
     478                 :          0 :                 return -1;
     479                 :            :         }
     480                 :            : 
     481                 :          1 :         *pos = '\0';
     482                 :          1 :         interfaces->global_iface_name = pos + 1;
     483                 :            : 
     484                 :          1 :         return 0;
     485                 :            : }
     486                 :            : 
     487                 :            : 
     488                 :          1 : static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
     489                 :            :                                         const char *group)
     490                 :            : {
     491                 :            : #ifndef CONFIG_NATIVE_WINDOWS
     492                 :            :         struct group *grp;
     493                 :          1 :         grp = getgrnam(group);
     494         [ -  + ]:          1 :         if (grp == NULL) {
     495                 :          0 :                 wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
     496                 :          0 :                 return -1;
     497                 :            :         }
     498                 :          1 :         interfaces->ctrl_iface_group = grp->gr_gid;
     499                 :            : #endif /* CONFIG_NATIVE_WINDOWS */
     500                 :          1 :         return 0;
     501                 :            : }
     502                 :            : 
     503                 :            : 
     504                 :          2 : int main(int argc, char *argv[])
     505                 :            : {
     506                 :            :         struct hapd_interfaces interfaces;
     507                 :          2 :         int ret = 1;
     508                 :            :         size_t i, j;
     509                 :          2 :         int c, debug = 0, daemonize = 0;
     510                 :          2 :         char *pid_file = NULL;
     511                 :          2 :         const char *log_file = NULL;
     512                 :          2 :         const char *entropy_file = NULL;
     513                 :          2 :         char **bss_config = NULL, **tmp_bss;
     514                 :          2 :         size_t num_bss_configs = 0;
     515                 :            : #ifdef CONFIG_DEBUG_LINUX_TRACING
     516                 :          2 :         int enable_trace_dbg = 0;
     517                 :            : #endif /* CONFIG_DEBUG_LINUX_TRACING */
     518                 :            : 
     519         [ -  + ]:          2 :         if (os_program_init())
     520                 :          0 :                 return -1;
     521                 :            : 
     522                 :          2 :         os_memset(&interfaces, 0, sizeof(interfaces));
     523                 :          2 :         interfaces.reload_config = hostapd_reload_config;
     524                 :          2 :         interfaces.config_read_cb = hostapd_config_read;
     525                 :          2 :         interfaces.for_each_interface = hostapd_for_each_interface;
     526                 :          2 :         interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
     527                 :          2 :         interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
     528                 :          2 :         interfaces.driver_init = hostapd_driver_init;
     529                 :          2 :         interfaces.global_iface_path = NULL;
     530                 :          2 :         interfaces.global_iface_name = NULL;
     531                 :          2 :         interfaces.global_ctrl_sock = -1;
     532                 :            : 
     533                 :            :         for (;;) {
     534                 :         17 :                 c = getopt(argc, argv, "b:Bde:f:hKP:Ttvg:G:");
     535         [ +  + ]:         17 :                 if (c < 0)
     536                 :          2 :                         break;
     537   [ -  +  -  -  :         15 :                 switch (c) {
          +  +  -  +  -  
             -  +  +  -  
                      - ]
     538                 :            :                 case 'h':
     539                 :          0 :                         usage();
     540                 :          0 :                         break;
     541                 :            :                 case 'd':
     542                 :          6 :                         debug++;
     543         [ +  + ]:          6 :                         if (wpa_debug_level > 0)
     544                 :          5 :                                 wpa_debug_level--;
     545                 :          6 :                         break;
     546                 :            :                 case 'B':
     547                 :          0 :                         daemonize++;
     548                 :          0 :                         break;
     549                 :            :                 case 'e':
     550                 :          0 :                         entropy_file = optarg;
     551                 :          0 :                         break;
     552                 :            :                 case 'f':
     553                 :          1 :                         log_file = optarg;
     554                 :          1 :                         break;
     555                 :            :                 case 'K':
     556                 :          3 :                         wpa_debug_show_keys++;
     557                 :          3 :                         break;
     558                 :            :                 case 'P':
     559                 :          0 :                         os_free(pid_file);
     560                 :          0 :                         pid_file = os_rel2abs_path(optarg);
     561                 :          0 :                         break;
     562                 :            :                 case 't':
     563                 :          3 :                         wpa_debug_timestamp++;
     564                 :          3 :                         break;
     565                 :            : #ifdef CONFIG_DEBUG_LINUX_TRACING
     566                 :            :                 case 'T':
     567                 :          0 :                         enable_trace_dbg = 1;
     568                 :          0 :                         break;
     569                 :            : #endif /* CONFIG_DEBUG_LINUX_TRACING */
     570                 :            :                 case 'v':
     571                 :          0 :                         show_version();
     572                 :          0 :                         exit(1);
     573                 :            :                         break;
     574                 :            :                 case 'g':
     575         [ -  + ]:          1 :                         if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
     576                 :          0 :                                 return -1;
     577                 :          1 :                         break;
     578                 :            :                 case 'G':
     579         [ -  + ]:          1 :                         if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
     580                 :          0 :                                 return -1;
     581                 :          1 :                         break;
     582                 :            :                 case 'b':
     583                 :          0 :                         tmp_bss = os_realloc_array(bss_config,
     584                 :            :                                                    num_bss_configs + 1,
     585                 :            :                                                    sizeof(char *));
     586         [ #  # ]:          0 :                         if (tmp_bss == NULL)
     587                 :          0 :                                 goto out;
     588                 :          0 :                         bss_config = tmp_bss;
     589                 :          0 :                         bss_config[num_bss_configs++] = optarg;
     590                 :          0 :                         break;
     591                 :            :                 default:
     592                 :          0 :                         usage();
     593                 :          0 :                         break;
     594                 :            :                 }
     595                 :         15 :         }
     596                 :            : 
     597 [ +  + ][ -  + ]:          2 :         if (optind == argc && interfaces.global_iface_path == NULL &&
                 [ #  # ]
     598                 :            :             num_bss_configs == 0)
     599                 :          0 :                 usage();
     600                 :            : 
     601                 :          2 :         wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
     602                 :            : 
     603         [ +  + ]:          2 :         if (log_file)
     604                 :          1 :                 wpa_debug_open_file(log_file);
     605                 :            : #ifdef CONFIG_DEBUG_LINUX_TRACING
     606         [ -  + ]:          2 :         if (enable_trace_dbg) {
     607                 :          0 :                 int tret = wpa_debug_open_linux_tracing();
     608         [ #  # ]:          0 :                 if (tret) {
     609                 :          0 :                         wpa_printf(MSG_ERROR, "Failed to enable trace logging");
     610                 :          0 :                         return -1;
     611                 :            :                 }
     612                 :            :         }
     613                 :            : #endif /* CONFIG_DEBUG_LINUX_TRACING */
     614                 :            : 
     615                 :          2 :         interfaces.count = argc - optind;
     616 [ +  + ][ -  + ]:          2 :         if (interfaces.count || num_bss_configs) {
     617                 :          1 :                 interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
     618                 :            :                                              sizeof(struct hostapd_iface *));
     619         [ -  + ]:          1 :                 if (interfaces.iface == NULL) {
     620                 :          0 :                         wpa_printf(MSG_ERROR, "malloc failed");
     621                 :          0 :                         return -1;
     622                 :            :                 }
     623                 :            :         }
     624                 :            : 
     625         [ -  + ]:          2 :         if (hostapd_global_init(&interfaces, entropy_file)) {
     626                 :          0 :                 wpa_printf(MSG_ERROR, "Failed to initilize global context");
     627                 :          0 :                 return -1;
     628                 :            :         }
     629                 :            : 
     630                 :            :         /* Allocate and parse configuration for full interface files */
     631         [ +  + ]:          3 :         for (i = 0; i < interfaces.count; i++) {
     632                 :          1 :                 interfaces.iface[i] = hostapd_interface_init(&interfaces,
     633                 :          1 :                                                              argv[optind + i],
     634                 :            :                                                              debug);
     635         [ -  + ]:          1 :                 if (!interfaces.iface[i]) {
     636                 :          0 :                         wpa_printf(MSG_ERROR, "Failed to initialize interface");
     637                 :          0 :                         goto out;
     638                 :            :                 }
     639                 :            :         }
     640                 :            : 
     641                 :            :         /* Allocate and parse configuration for per-BSS files */
     642         [ -  + ]:          2 :         for (i = 0; i < num_bss_configs; i++) {
     643                 :            :                 struct hostapd_iface *iface;
     644                 :            :                 char *fname;
     645                 :            : 
     646                 :          0 :                 wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
     647                 :          0 :                 fname = os_strchr(bss_config[i], ':');
     648         [ #  # ]:          0 :                 if (fname == NULL) {
     649                 :          0 :                         wpa_printf(MSG_ERROR,
     650                 :            :                                    "Invalid BSS config identifier '%s'",
     651                 :          0 :                                    bss_config[i]);
     652                 :          0 :                         goto out;
     653                 :            :                 }
     654                 :          0 :                 *fname++ = '\0';
     655                 :          0 :                 iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
     656                 :            :                                                    fname, debug);
     657         [ #  # ]:          0 :                 if (iface == NULL)
     658                 :          0 :                         goto out;
     659         [ #  # ]:          0 :                 for (j = 0; j < interfaces.count; j++) {
     660         [ #  # ]:          0 :                         if (interfaces.iface[j] == iface)
     661                 :          0 :                                 break;
     662                 :            :                 }
     663         [ #  # ]:          0 :                 if (j == interfaces.count) {
     664                 :            :                         struct hostapd_iface **tmp;
     665                 :          0 :                         tmp = os_realloc_array(interfaces.iface,
     666                 :          0 :                                                interfaces.count + 1,
     667                 :            :                                                sizeof(struct hostapd_iface *));
     668         [ #  # ]:          0 :                         if (tmp == NULL) {
     669                 :          0 :                                 hostapd_interface_deinit_free(iface);
     670                 :          0 :                                 goto out;
     671                 :            :                         }
     672                 :          0 :                         interfaces.iface = tmp;
     673                 :          0 :                         interfaces.iface[interfaces.count++] = iface;
     674                 :            :                 }
     675                 :            :         }
     676                 :            : 
     677                 :            :         /*
     678                 :            :          * Enable configured interfaces. Depending on channel configuration,
     679                 :            :          * this may complete full initialization before returning or use a
     680                 :            :          * callback mechanism to complete setup in case of operations like HT
     681                 :            :          * co-ex scans, ACS, or DFS are needed to determine channel parameters.
     682                 :            :          * In such case, the interface will be enabled from eloop context within
     683                 :            :          * hostapd_global_run().
     684                 :            :          */
     685                 :          2 :         interfaces.terminate_on_error = interfaces.count;
     686         [ +  + ]:          3 :         for (i = 0; i < interfaces.count; i++) {
     687   [ +  -  +  - ]:          2 :                 if (hostapd_driver_init(interfaces.iface[i]) ||
     688                 :          1 :                     hostapd_setup_interface(interfaces.iface[i]))
     689                 :            :                         goto out;
     690                 :            :         }
     691                 :            : 
     692                 :          2 :         hostapd_global_ctrl_iface_init(&interfaces);
     693                 :            : 
     694         [ -  + ]:          2 :         if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
     695                 :          0 :                 wpa_printf(MSG_ERROR, "Failed to start eloop");
     696                 :          0 :                 goto out;
     697                 :            :         }
     698                 :            : 
     699                 :          2 :         ret = 0;
     700                 :            : 
     701                 :            :  out:
     702                 :          2 :         hostapd_global_ctrl_iface_deinit(&interfaces);
     703                 :            :         /* Deinitialize all interfaces */
     704         [ +  + ]:          3 :         for (i = 0; i < interfaces.count; i++)
     705                 :          1 :                 hostapd_interface_deinit_free(interfaces.iface[i]);
     706                 :          2 :         os_free(interfaces.iface);
     707                 :            : 
     708                 :          2 :         hostapd_global_deinit(pid_file);
     709                 :          2 :         os_free(pid_file);
     710                 :            : 
     711         [ +  + ]:          2 :         if (log_file)
     712                 :          1 :                 wpa_debug_close_file();
     713                 :          2 :         wpa_debug_close_linux_tracing();
     714                 :            : 
     715                 :          2 :         os_free(bss_config);
     716                 :            : 
     717                 :          2 :         os_program_deinit();
     718                 :            : 
     719                 :          2 :         return ret;
     720                 :            : }

Generated by: LCOV version 1.9