1 /* 2 * WPA Supplicant / main() function for UNIX like OSes and MinGW 3 * Copyright (c) 2003-2013, 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 "includes.h" 10 #ifdef __linux__ 11 #include <fcntl.h> 12 #endif /* __linux__ */ 13 14 #include "common.h" 15 #include "fst/fst.h" 16 #include "wpa_supplicant_i.h" 17 #include "driver_i.h" 18 #include "p2p_supplicant.h" 19 20 21 static void usage(void) 22 { 23 int i; 24 printf("%s\n\n%s\n" 25 "usage:\n" 26 " wpa_supplicant [-BddhKLqq" 27 #ifdef CONFIG_DEBUG_SYSLOG 28 "s" 29 #endif /* CONFIG_DEBUG_SYSLOG */ 30 "t" 31 #ifdef CONFIG_CTRL_IFACE_DBUS_NEW 32 "u" 33 #endif /* CONFIG_CTRL_IFACE_DBUS_NEW */ 34 "vW] [-P<pid file>] " 35 "[-g<global ctrl>] \\\n" 36 " [-G<group>] \\\n" 37 " -i<ifname> -c<config file> [-C<ctrl>] [-D<driver>] " 38 "[-p<driver_param>] \\\n" 39 " [-b<br_ifname>] [-e<entropy file>]" 40 #ifdef CONFIG_DEBUG_FILE 41 " [-f<debug file>]" 42 #endif /* CONFIG_DEBUG_FILE */ 43 " \\\n" 44 " [-o<override driver>] [-O<override ctrl>] \\\n" 45 " [-N -i<ifname> -c<conf> [-C<ctrl>] " 46 "[-D<driver>] \\\n" 47 #ifdef CONFIG_P2P 48 " [-m<P2P Device config file>] \\\n" 49 #endif /* CONFIG_P2P */ 50 " [-p<driver_param>] [-b<br_ifname>] [-I<config file>] " 51 "...]\n" 52 "\n" 53 "drivers:\n", 54 wpa_supplicant_version, wpa_supplicant_license); 55 56 for (i = 0; wpa_drivers[i]; i++) { 57 printf(" %s = %s\n", 58 wpa_drivers[i]->name, 59 wpa_drivers[i]->desc); 60 } 61 62 #ifndef CONFIG_NO_STDOUT_DEBUG 63 printf("options:\n" 64 " -b = optional bridge interface name\n" 65 " -B = run daemon in the background\n" 66 " -c = Configuration file\n" 67 " -C = ctrl_interface parameter (only used if -c is not)\n" 68 " -d = increase debugging verbosity (-dd even more)\n" 69 " -D = driver name (can be multiple drivers: bsd,wired)\n" 70 " -e = entropy file\n" 71 #ifdef CONFIG_DEBUG_FILE 72 " -f = log output to debug file instead of stdout\n" 73 #endif /* CONFIG_DEBUG_FILE */ 74 " -g = global ctrl_interface\n" 75 " -G = global ctrl_interface group\n" 76 " -h = show this help text\n" 77 " -i = interface name\n" 78 " -I = additional configuration file\n" 79 " -K = include keys (passwords, etc.) in debug output\n" 80 " -L = show license (BSD)\n" 81 #ifdef CONFIG_P2P 82 " -m = Configuration file for the P2P Device interface\n" 83 #endif /* CONFIG_P2P */ 84 #ifdef CONFIG_MATCH_IFACE 85 " -M = start describing new matching interface\n" 86 #endif /* CONFIG_MATCH_IFACE */ 87 " -N = start describing new interface\n" 88 " -o = override driver parameter for new interfaces\n" 89 " -O = override ctrl_interface parameter for new interfaces\n" 90 " -p = driver parameters\n" 91 " -P = PID file\n" 92 " -q = decrease debugging verbosity (-qq even less)\n" 93 #ifdef CONFIG_DEBUG_SYSLOG 94 " -s = log output to syslog instead of stdout\n" 95 #endif /* CONFIG_DEBUG_SYSLOG */ 96 " -t = include timestamp in debug messages\n" 97 #ifdef CONFIG_DEBUG_LINUX_TRACING 98 " -T = record to Linux tracing in addition to logging\n" 99 " (records all messages regardless of debug verbosity)\n" 100 #endif /* CONFIG_DEBUG_LINUX_TRACING */ 101 #ifdef CONFIG_CTRL_IFACE_DBUS_NEW 102 " -u = enable DBus control interface\n" 103 #endif /* CONFIG_CTRL_IFACE_DBUS_NEW */ 104 " -v = show version\n" 105 " -W = wait for a control interface monitor before starting\n"); 106 107 printf("example:\n" 108 " wpa_supplicant -Dbsd -iwlan0 -c/etc/wpa_supplicant.conf\n"); 109 #endif /* CONFIG_NO_STDOUT_DEBUG */ 110 } 111 112 113 static void license(void) 114 { 115 #ifndef CONFIG_NO_STDOUT_DEBUG 116 printf("%s\n\n%s%s%s%s%s\n", 117 wpa_supplicant_version, 118 wpa_supplicant_full_license1, 119 wpa_supplicant_full_license2, 120 wpa_supplicant_full_license3, 121 wpa_supplicant_full_license4, 122 wpa_supplicant_full_license5); 123 #endif /* CONFIG_NO_STDOUT_DEBUG */ 124 } 125 126 127 static void wpa_supplicant_fd_workaround(int start) 128 { 129 #ifdef __linux__ 130 static int fd[3] = { -1, -1, -1 }; 131 int i; 132 /* When started from pcmcia-cs scripts, wpa_supplicant might start with 133 * fd 0, 1, and 2 closed. This will cause some issues because many 134 * places in wpa_supplicant are still printing out to stdout. As a 135 * workaround, make sure that fd's 0, 1, and 2 are not used for other 136 * sockets. */ 137 if (start) { 138 for (i = 0; i < 3; i++) { 139 fd[i] = open("/dev/null", O_RDWR); 140 if (fd[i] > 2) { 141 close(fd[i]); 142 fd[i] = -1; 143 break; 144 } 145 } 146 } else { 147 for (i = 0; i < 3; i++) { 148 if (fd[i] >= 0) { 149 close(fd[i]); 150 fd[i] = -1; 151 } 152 } 153 } 154 #endif /* __linux__ */ 155 } 156 157 158 #ifdef CONFIG_MATCH_IFACE 159 static int wpa_supplicant_init_match(struct wpa_global *global) 160 { 161 /* 162 * The assumption is that the first driver is the primary driver and 163 * will handle the arrival / departure of interfaces. 164 */ 165 if (wpa_drivers[0]->global_init && !global->drv_priv[0]) { 166 global->drv_priv[0] = wpa_drivers[0]->global_init(global); 167 if (!global->drv_priv[0]) { 168 wpa_printf(MSG_ERROR, 169 "Failed to initialize driver '%s'", 170 wpa_drivers[0]->name); 171 return -1; 172 } 173 } 174 175 return 0; 176 } 177 #endif /* CONFIG_MATCH_IFACE */ 178 179 180 int main(int argc, char *argv[]) 181 { 182 int c, i; 183 struct wpa_interface *ifaces, *iface; 184 int iface_count, exitcode = -1; 185 struct wpa_params params; 186 struct wpa_global *global; 187 188 if (os_program_init()) 189 return -1; 190 191 os_memset(¶ms, 0, sizeof(params)); 192 params.wpa_debug_level = MSG_INFO; 193 194 iface = ifaces = os_zalloc(sizeof(struct wpa_interface)); 195 if (ifaces == NULL) 196 return -1; 197 iface_count = 1; 198 199 wpa_supplicant_fd_workaround(1); 200 201 for (;;) { 202 c = getopt(argc, argv, 203 "b:Bc:C:D:de:f:g:G:hi:I:KLMm:No:O:p:P:qsTtuvW"); 204 if (c < 0) 205 break; 206 switch (c) { 207 case 'b': 208 iface->bridge_ifname = optarg; 209 break; 210 case 'B': 211 params.daemonize++; 212 break; 213 case 'c': 214 iface->confname = optarg; 215 break; 216 case 'C': 217 iface->ctrl_interface = optarg; 218 break; 219 case 'D': 220 iface->driver = optarg; 221 break; 222 case 'd': 223 #ifdef CONFIG_NO_STDOUT_DEBUG 224 printf("Debugging disabled with " 225 "CONFIG_NO_STDOUT_DEBUG=y build time " 226 "option.\n"); 227 goto out; 228 #else /* CONFIG_NO_STDOUT_DEBUG */ 229 params.wpa_debug_level--; 230 break; 231 #endif /* CONFIG_NO_STDOUT_DEBUG */ 232 case 'e': 233 params.entropy_file = optarg; 234 break; 235 #ifdef CONFIG_DEBUG_FILE 236 case 'f': 237 params.wpa_debug_file_path = optarg; 238 break; 239 #endif /* CONFIG_DEBUG_FILE */ 240 case 'g': 241 params.ctrl_interface = optarg; 242 break; 243 case 'G': 244 params.ctrl_interface_group = optarg; 245 break; 246 case 'h': 247 usage(); 248 exitcode = 0; 249 goto out; 250 case 'i': 251 iface->ifname = optarg; 252 break; 253 case 'I': 254 iface->confanother = optarg; 255 break; 256 case 'K': 257 params.wpa_debug_show_keys++; 258 break; 259 case 'L': 260 license(); 261 exitcode = 0; 262 goto out; 263 #ifdef CONFIG_P2P 264 case 'm': 265 params.conf_p2p_dev = optarg; 266 break; 267 #endif /* CONFIG_P2P */ 268 case 'o': 269 params.override_driver = optarg; 270 break; 271 case 'O': 272 params.override_ctrl_interface = optarg; 273 break; 274 case 'p': 275 iface->driver_param = optarg; 276 break; 277 case 'P': 278 os_free(params.pid_file); 279 params.pid_file = os_rel2abs_path(optarg); 280 break; 281 case 'q': 282 params.wpa_debug_level++; 283 break; 284 #ifdef CONFIG_DEBUG_SYSLOG 285 case 's': 286 params.wpa_debug_syslog++; 287 break; 288 #endif /* CONFIG_DEBUG_SYSLOG */ 289 #ifdef CONFIG_DEBUG_LINUX_TRACING 290 case 'T': 291 params.wpa_debug_tracing++; 292 break; 293 #endif /* CONFIG_DEBUG_LINUX_TRACING */ 294 case 't': 295 params.wpa_debug_timestamp++; 296 break; 297 #ifdef CONFIG_CTRL_IFACE_DBUS_NEW 298 case 'u': 299 params.dbus_ctrl_interface = 1; 300 break; 301 #endif /* CONFIG_CTRL_IFACE_DBUS_NEW */ 302 case 'v': 303 printf("%s\n", wpa_supplicant_version); 304 exitcode = 0; 305 goto out; 306 case 'W': 307 params.wait_for_monitor++; 308 break; 309 #ifdef CONFIG_MATCH_IFACE 310 case 'M': 311 params.match_iface_count++; 312 iface = os_realloc_array(params.match_ifaces, 313 params.match_iface_count, 314 sizeof(struct wpa_interface)); 315 if (!iface) 316 goto out; 317 params.match_ifaces = iface; 318 iface = ¶ms.match_ifaces[params.match_iface_count - 319 1]; 320 os_memset(iface, 0, sizeof(*iface)); 321 break; 322 #endif /* CONFIG_MATCH_IFACE */ 323 case 'N': 324 iface_count++; 325 iface = os_realloc_array(ifaces, iface_count, 326 sizeof(struct wpa_interface)); 327 if (iface == NULL) 328 goto out; 329 ifaces = iface; 330 iface = &ifaces[iface_count - 1]; 331 os_memset(iface, 0, sizeof(*iface)); 332 break; 333 default: 334 usage(); 335 exitcode = 0; 336 goto out; 337 } 338 } 339 340 exitcode = 0; 341 global = wpa_supplicant_init(¶ms); 342 if (global == NULL) { 343 wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant"); 344 exitcode = -1; 345 goto out; 346 } else { 347 wpa_printf(MSG_INFO, "Successfully initialized " 348 "wpa_supplicant"); 349 } 350 351 if (fst_global_init()) { 352 wpa_printf(MSG_ERROR, "Failed to initialize FST"); 353 exitcode = -1; 354 goto out; 355 } 356 357 #if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE) 358 if (!fst_global_add_ctrl(fst_ctrl_cli)) 359 wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl"); 360 #endif 361 362 for (i = 0; exitcode == 0 && i < iface_count; i++) { 363 struct wpa_supplicant *wpa_s; 364 365 if ((ifaces[i].confname == NULL && 366 ifaces[i].ctrl_interface == NULL) || 367 ifaces[i].ifname == NULL) { 368 if (iface_count == 1 && (params.ctrl_interface || 369 #ifdef CONFIG_MATCH_IFACE 370 params.match_iface_count || 371 #endif /* CONFIG_MATCH_IFACE */ 372 params.dbus_ctrl_interface)) 373 break; 374 usage(); 375 exitcode = -1; 376 break; 377 } 378 wpa_s = wpa_supplicant_add_iface(global, &ifaces[i], NULL); 379 if (wpa_s == NULL) { 380 exitcode = -1; 381 break; 382 } 383 } 384 385 #ifdef CONFIG_MATCH_IFACE 386 if (exitcode == 0) 387 exitcode = wpa_supplicant_init_match(global); 388 #endif /* CONFIG_MATCH_IFACE */ 389 390 if (exitcode == 0) 391 exitcode = wpa_supplicant_run(global); 392 393 wpa_supplicant_deinit(global); 394 395 fst_global_deinit(); 396 397 out: 398 wpa_supplicant_fd_workaround(0); 399 os_free(ifaces); 400 #ifdef CONFIG_MATCH_IFACE 401 os_free(params.match_ifaces); 402 #endif /* CONFIG_MATCH_IFACE */ 403 os_free(params.pid_file); 404 405 os_program_deinit(); 406 407 return exitcode; 408 } 409