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 #ifdef CONFIG_DRIVER_NDIS 202 void driver_ndis_init_ops(void); 203 driver_ndis_init_ops(); 204 #endif /* CONFIG_DRIVER_NDIS */ 205 206 for (;;) { 207 c = getopt(argc, argv, 208 "b:Bc:C:D:de:f:g:G:hi:I:KLMm:No:O:p:P:qsTtuvW"); 209 if (c < 0) 210 break; 211 switch (c) { 212 case 'b': 213 iface->bridge_ifname = optarg; 214 break; 215 case 'B': 216 params.daemonize++; 217 break; 218 case 'c': 219 iface->confname = optarg; 220 break; 221 case 'C': 222 iface->ctrl_interface = optarg; 223 break; 224 case 'D': 225 iface->driver = optarg; 226 break; 227 case 'd': 228 #ifdef CONFIG_NO_STDOUT_DEBUG 229 printf("Debugging disabled with " 230 "CONFIG_NO_STDOUT_DEBUG=y build time " 231 "option.\n"); 232 goto out; 233 #else /* CONFIG_NO_STDOUT_DEBUG */ 234 params.wpa_debug_level--; 235 break; 236 #endif /* CONFIG_NO_STDOUT_DEBUG */ 237 case 'e': 238 params.entropy_file = optarg; 239 break; 240 #ifdef CONFIG_DEBUG_FILE 241 case 'f': 242 params.wpa_debug_file_path = optarg; 243 break; 244 #endif /* CONFIG_DEBUG_FILE */ 245 case 'g': 246 params.ctrl_interface = optarg; 247 break; 248 case 'G': 249 params.ctrl_interface_group = optarg; 250 break; 251 case 'h': 252 usage(); 253 exitcode = 0; 254 goto out; 255 case 'i': 256 iface->ifname = optarg; 257 break; 258 case 'I': 259 iface->confanother = optarg; 260 break; 261 case 'K': 262 params.wpa_debug_show_keys++; 263 break; 264 case 'L': 265 license(); 266 exitcode = 0; 267 goto out; 268 #ifdef CONFIG_P2P 269 case 'm': 270 params.conf_p2p_dev = optarg; 271 break; 272 #endif /* CONFIG_P2P */ 273 case 'o': 274 params.override_driver = optarg; 275 break; 276 case 'O': 277 params.override_ctrl_interface = optarg; 278 break; 279 case 'p': 280 iface->driver_param = optarg; 281 break; 282 case 'P': 283 os_free(params.pid_file); 284 params.pid_file = os_rel2abs_path(optarg); 285 break; 286 case 'q': 287 params.wpa_debug_level++; 288 break; 289 #ifdef CONFIG_DEBUG_SYSLOG 290 case 's': 291 params.wpa_debug_syslog++; 292 break; 293 #endif /* CONFIG_DEBUG_SYSLOG */ 294 #ifdef CONFIG_DEBUG_LINUX_TRACING 295 case 'T': 296 params.wpa_debug_tracing++; 297 break; 298 #endif /* CONFIG_DEBUG_LINUX_TRACING */ 299 case 't': 300 params.wpa_debug_timestamp++; 301 break; 302 #ifdef CONFIG_CTRL_IFACE_DBUS_NEW 303 case 'u': 304 params.dbus_ctrl_interface = 1; 305 break; 306 #endif /* CONFIG_CTRL_IFACE_DBUS_NEW */ 307 case 'v': 308 printf("%s\n", wpa_supplicant_version); 309 exitcode = 0; 310 goto out; 311 case 'W': 312 params.wait_for_monitor++; 313 break; 314 #ifdef CONFIG_MATCH_IFACE 315 case 'M': 316 params.match_iface_count++; 317 iface = os_realloc_array(params.match_ifaces, 318 params.match_iface_count, 319 sizeof(struct wpa_interface)); 320 if (!iface) 321 goto out; 322 params.match_ifaces = iface; 323 iface = ¶ms.match_ifaces[params.match_iface_count - 324 1]; 325 os_memset(iface, 0, sizeof(*iface)); 326 break; 327 #endif /* CONFIG_MATCH_IFACE */ 328 case 'N': 329 iface_count++; 330 iface = os_realloc_array(ifaces, iface_count, 331 sizeof(struct wpa_interface)); 332 if (iface == NULL) 333 goto out; 334 ifaces = iface; 335 iface = &ifaces[iface_count - 1]; 336 os_memset(iface, 0, sizeof(*iface)); 337 break; 338 default: 339 usage(); 340 exitcode = 0; 341 goto out; 342 } 343 } 344 345 exitcode = 0; 346 global = wpa_supplicant_init(¶ms); 347 if (global == NULL) { 348 wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant"); 349 exitcode = -1; 350 goto out; 351 } else { 352 wpa_printf(MSG_INFO, "Successfully initialized " 353 "wpa_supplicant"); 354 } 355 356 if (fst_global_init()) { 357 wpa_printf(MSG_ERROR, "Failed to initialize FST"); 358 exitcode = -1; 359 goto out; 360 } 361 362 #if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE) 363 if (!fst_global_add_ctrl(fst_ctrl_cli)) 364 wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl"); 365 #endif 366 367 for (i = 0; exitcode == 0 && i < iface_count; i++) { 368 struct wpa_supplicant *wpa_s; 369 370 if ((ifaces[i].confname == NULL && 371 ifaces[i].ctrl_interface == NULL) || 372 ifaces[i].ifname == NULL) { 373 if (iface_count == 1 && (params.ctrl_interface || 374 #ifdef CONFIG_MATCH_IFACE 375 params.match_iface_count || 376 #endif /* CONFIG_MATCH_IFACE */ 377 params.dbus_ctrl_interface)) 378 break; 379 usage(); 380 exitcode = -1; 381 break; 382 } 383 wpa_s = wpa_supplicant_add_iface(global, &ifaces[i], NULL); 384 if (wpa_s == NULL) { 385 exitcode = -1; 386 break; 387 } 388 } 389 390 #ifdef CONFIG_MATCH_IFACE 391 if (exitcode == 0) 392 exitcode = wpa_supplicant_init_match(global); 393 #endif /* CONFIG_MATCH_IFACE */ 394 395 if (exitcode == 0) 396 exitcode = wpa_supplicant_run(global); 397 398 wpa_supplicant_deinit(global); 399 400 fst_global_deinit(); 401 402 out: 403 wpa_supplicant_fd_workaround(0); 404 os_free(ifaces); 405 #ifdef CONFIG_MATCH_IFACE 406 os_free(params.match_ifaces); 407 #endif /* CONFIG_MATCH_IFACE */ 408 os_free(params.pid_file); 409 410 os_program_deinit(); 411 412 return exitcode; 413 } 414