2 * The olsr.org Optimized Link-State Routing daemon(olsrd)
3 * Copyright (c) 2004, Andreas Tonnesen(andreto@olsr.org)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of olsr.org, olsrd nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
33 * Visit http://www.olsr.org for more information.
35 * If you find this software useful feel free to make a donation
36 * to the project. For more information see the website or contact
37 * the copyright holders.
51 #include "scheduler.h"
53 #include "generate_msg.h"
54 #include "plugin_loader.h"
57 #include "build_msg.h"
60 #include "mpr_selector_set.h"
62 #include "olsr_niit.h"
65 #include <linux/types.h>
66 #include <linux/rtnetlink.h>
67 #include "kernel_routes.h"
69 #endif /* __linux__ */
74 #define olsr_shutdown(x) SignalHandler(x)
75 #define close(x) closesocket(x)
76 int __stdcall SignalHandler(unsigned long signo) __attribute__ ((noreturn));
77 void ListInterfaces(void);
78 void DisableIcmpRedirects(void);
79 bool olsr_win32_end_request = false;
80 bool olsr_win32_end_flag = false;
82 static void olsr_shutdown(int) __attribute__ ((noreturn));
85 #if defined __ANDROID__
86 #define DEFAULT_LOCKFILE_PREFIX "/data/local/olsrd"
87 #elif defined linux || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__
88 #define DEFAULT_LOCKFILE_PREFIX "/var/run/olsrd"
90 #define DEFAULT_LOCKFILE_PREFIX "C:\\olsrd"
91 #else /* defined _WIN32 */
92 #define DEFAULT_LOCKFILE_PREFIX "olsrd"
93 #endif /* defined _WIN32 */
96 * Local function prototypes
98 void olsr_reconfigure(int signo) __attribute__ ((noreturn));
100 static void print_usage(bool error);
102 static int set_default_ifcnfs(struct olsr_if *, struct if_config_options *);
104 static int olsr_process_arguments(int, char *[], struct olsrd_config *,
105 struct if_config_options *);
108 static char **olsr_argv;
112 copyright_string[] __attribute__ ((unused)) =
113 "The olsr.org Optimized Link-State Routing daemon(olsrd) Copyright (c) 2004, Andreas Tonnesen(andreto@olsr.org) All rights reserved.";
115 /* Data for OLSR locking */
117 static int lock_fd = 0;
119 static char lock_file_name[FILENAME_MAX];
120 struct olsr_cookie_info *def_timer_ci = NULL;
123 * Creates a zero-length locking file and use fcntl to
124 * place an exclusive lock over it. The lock will be
125 * automatically erased when the olsrd process ends,
126 * so it will even work well with a SIGKILL.
128 * Additionally the lock can be killed by removing the
131 static int olsr_create_lock_file(bool noExitOnFail) {
136 lck = CreateFile(lock_file_name,
137 GENERIC_READ | GENERIC_WRITE,
138 FILE_SHARE_READ | FILE_SHARE_WRITE,
141 FILE_ATTRIBUTE_NORMAL |
142 FILE_FLAG_DELETE_ON_CLOSE,
144 CreateEvent(NULL, TRUE, FALSE, lock_file_name);
145 if (INVALID_HANDLE_VALUE == lck || ERROR_ALREADY_EXISTS == GetLastError()) {
151 "Error, cannot create OLSR lock '%s'.\n",
156 "Error, cannot aquire OLSR lock '%s'.\n"
157 "Another OLSR instance might be running.\n",
160 olsr_exit("", EXIT_FAILURE);
163 success = LockFile( lck, 0, 0, 0, 0);
171 "Error, cannot aquire OLSR lock '%s'.\n"
172 "Another OLSR instance might be running.\n",
174 olsr_exit("", EXIT_FAILURE);
180 /* create file for lock */
181 lock_fd = open(lock_file_name, O_WRONLY | O_CREAT, S_IRWXU);
187 "Error, cannot create OLSR lock '%s'.\n",
189 olsr_exit("", EXIT_FAILURE);
192 /* create exclusive lock for the whole file */
193 lck.l_type = F_WRLCK;
194 lck.l_whence = SEEK_SET;
199 if (fcntl(lock_fd, F_SETLK, &lck) == -1) {
205 "Error, cannot aquire OLSR lock '%s'.\n"
206 "Another OLSR instance might be running.\n",
208 olsr_exit("", EXIT_FAILURE);
215 * Write the current PID to the configured PID file (if one is configured)
217 static void writePidFile(void) {
218 if (olsr_cnf->pidfile) {
219 char buf[PATH_MAX + 256];
221 /* create / open the PID file */
223 mode_t mode = S_IRUSR | S_IWUSR;
225 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
227 int fd = open(olsr_cnf->pidfile, O_CREAT | O_WRONLY, mode);
229 snprintf(buf, sizeof(buf), "Could not open PID file %s", olsr_cnf->pidfile);
236 pid_t pid = getpid();
237 int chars = snprintf(buf, sizeof(buf), "%d", (int)pid);
238 ssize_t chars_written = write(fd, buf, chars);
239 if (chars_written != chars) {
241 snprintf(buf, sizeof(buf), "Could not write the PID %d to the PID file %s", (int)pid, olsr_cnf->pidfile);
243 if (remove(olsr_cnf->pidfile) < 0) {
244 snprintf(buf, sizeof(buf), "Could not remove the PID file %s", olsr_cnf->pidfile);
252 snprintf(buf, sizeof(buf), "Could not close PID file %s", olsr_cnf->pidfile);
254 if (remove(olsr_cnf->pidfile) < 0) {
255 snprintf(buf, sizeof(buf), "Could not remove the PID file %s", olsr_cnf->pidfile);
264 * loads a config file
265 * @return <0 if load failed, 0 otherwise
268 olsrmain_load_config(char *file) {
271 if (stat(file, &statbuf) < 0) {
272 fprintf(stderr, "Could not find specified config file %s!\n%s\n\n",
273 file, strerror(errno));
277 if (olsrd_parse_cnf(file) < 0) {
278 fprintf(stderr, "Error while reading config file %s!\n", file);
284 static void initRandom(void) {
285 unsigned int seed = (unsigned int)time(NULL);
290 randomFile = open("/dev/urandom", O_RDONLY);
291 if (randomFile == -1) {
292 randomFile = open("/dev/random", O_RDONLY);
295 if (randomFile != -1) {
296 if (read(randomFile, &seed, sizeof(seed)) != sizeof(seed)) {
297 ; /* to fix an 'unused result' compiler warning */
310 int main(int argc, char *argv[]) {
311 struct if_config_options *default_ifcnf;
312 char conf_file_name[FILENAME_MAX];
313 struct ipaddr_str buf;
314 bool loadedConfig = false;
318 struct interface *ifn;
319 #endif /* __linux__ */
324 #endif /* __linux__ */
326 /* paranoia checks */
327 assert(sizeof(uint8_t) == 1);
328 assert(sizeof(uint16_t) == 2);
329 assert(sizeof(uint32_t) == 4);
330 assert(sizeof(int8_t) == 1);
331 assert(sizeof(int16_t) == 2);
332 assert(sizeof(int32_t) == 4);
334 printf("\n *** %s ***\n Build date: %s on %s\n http://www.olsr.org\n\n",
335 olsrd_version, build_date, build_host);
338 if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "/?") == 0) {
342 if (strcmp(argv[1], "-v") == 0) {
347 debug_handle = stdout;
351 setbuf(stdout, NULL);
352 setbuf(stderr, NULL);
355 /* Check if user is root */
357 fprintf(stderr, "You must be root(uid = 0) to run olsrd!\nExiting\n\n");
361 DisableIcmpRedirects();
363 if (WSAStartup(0x0202, &WsaData)) {
364 fprintf(stderr, "Could not initialize WinSock.\n");
365 olsr_exit(__func__, EXIT_FAILURE);
370 olsr_openlog("olsrd");
372 /* setup random seed */
375 /* Init widely used statics */
376 memset(&all_zero, 0, sizeof(union olsr_ip_addr));
379 * Set configfile name and
380 * check if a configfile name was given as parameter
384 GetWindowsDirectory(conf_file_name, FILENAME_MAX - 11);
386 conf_file_name[0] = 0;
389 len = strlen(conf_file_name);
391 if (len == 0 || conf_file_name[len - 1] != '\\')
392 conf_file_name[len++] = '\\';
394 strscpy(conf_file_name + len, "olsrd.conf", sizeof(conf_file_name) - len);
396 strscpy(conf_file_name, OLSRD_GLOBAL_CONF_FILE, sizeof(conf_file_name));
399 olsr_cnf = olsrd_get_default_cnf();
400 for (i=1; i < argc-1;) {
401 if (strcmp(argv[i], "-f") == 0) {
404 if (olsrmain_load_config(argv[i+1]) < 0) {
409 memmove(&argv[i], &argv[i+2], sizeof(*argv) * (argc-i-1));
419 * set up configuration prior to processing commandline options
421 if (!loadedConfig && olsrmain_load_config(conf_file_name) == 0) {
426 olsrd_free_cnf(olsr_cnf);
427 olsr_cnf = olsrd_get_default_cnf();
430 default_ifcnf = get_default_if_config();
431 if (default_ifcnf == NULL) {
432 fprintf(stderr, "No default ifconfig found!\n");
436 /* Initialize timers */
440 * Process olsrd options.
442 if (olsr_process_arguments(argc, argv, olsr_cnf, default_ifcnf) < 0) {
444 olsr_exit(__func__, EXIT_FAILURE);
448 * Set configuration for command-line specified interfaces
450 set_default_ifcnfs(olsr_cnf->interfaces, default_ifcnf);
452 /* free the default ifcnf */
455 /* Sanity check configuration */
456 if (olsrd_sanity_check_cnf(olsr_cnf) < 0) {
457 fprintf(stderr, "Bad configuration!\n");
458 olsr_exit(__func__, EXIT_FAILURE);
462 * Establish file lock to prevent multiple instances
464 if (olsr_cnf->lock_file) {
465 strscpy(lock_file_name, olsr_cnf->lock_file, sizeof(lock_file_name));
468 #ifdef DEFAULT_LOCKFILE_PREFIX
469 strscpy(lock_file_name, DEFAULT_LOCKFILE_PREFIX, sizeof(lock_file_name));
470 #else /* DEFAULT_LOCKFILE_PREFIX */
471 strscpy(lock_file_name, conf_file_name, sizeof(lock_file_name));
472 #endif /* DEFAULT_LOCKFILE_PREFIX */
473 l = strlen(lock_file_name);
474 snprintf(&lock_file_name[l], sizeof(lock_file_name) - l, "-ipv%d.lock",
475 olsr_cnf->ip_version == AF_INET ? 4 : 6);
479 * Print configuration
481 if (olsr_cnf->debug_level > 1) {
482 olsrd_print_cnf(olsr_cnf);
485 def_timer_ci = olsr_alloc_cookie("Default Timer Cookie", OLSR_COOKIE_TYPE_TIMER);
488 * socket for ioctl calls
490 olsr_cnf->ioctl_s = socket(olsr_cnf->ip_version, SOCK_DGRAM, 0);
491 if (olsr_cnf->ioctl_s < 0) {
493 olsr_syslog(OLSR_LOG_ERR, "ioctl socket: %m");
495 olsr_exit(__func__, 0);
498 olsr_cnf->rtnl_s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
499 if (olsr_cnf->rtnl_s < 0) {
500 olsr_syslog(OLSR_LOG_ERR, "rtnetlink socket: %m");
501 olsr_exit(__func__, 0);
504 if (fcntl(olsr_cnf->rtnl_s, F_SETFL, O_NONBLOCK)) {
505 olsr_syslog(OLSR_LOG_INFO, "rtnetlink could not be set to nonblocking");
508 if ((olsr_cnf->rt_monitor_socket = rtnetlink_register_socket(RTMGRP_LINK)) < 0) {
509 olsr_syslog(OLSR_LOG_ERR, "rtmonitor socket: %m");
510 olsr_exit(__func__, 0);
512 #endif /* __linux__ */
515 * create routing socket
517 #if defined __FreeBSD__ || defined __FreeBSD_kernel__ || defined __APPLE__ || defined __NetBSD__ || defined __OpenBSD__
518 olsr_cnf->rts = socket(PF_ROUTE, SOCK_RAW, 0);
519 if (olsr_cnf->rts < 0) {
520 olsr_syslog(OLSR_LOG_ERR, "routing socket: %m");
521 olsr_exit(__func__, 0);
523 #endif /* defined __FreeBSD__ || defined __FreeBSD_kernel__ || defined __APPLE__ || defined __NetBSD__ || defined __OpenBSD__ */
526 /* initialize gateway system */
527 if (olsr_cnf->smart_gw_active) {
528 if (olsr_init_gateways()) {
529 olsr_exit("Cannot initialize gateway tunnels", 1);
533 /* initialize niit if index */
534 if (olsr_cnf->use_niit) {
537 #endif /* __linux__ */
539 /* Init empty TC timer */
540 set_empty_tc_timer(GET_TIMESTAMP(0));
542 /* enable ip forwarding on host */
543 /* Disable redirects globally (not for WIN32) */
544 net_os_set_global_ifoptions();
546 /* Initialize parser */
549 /* Initialize route-exporter */
550 olsr_init_export_route();
552 /* Initialize message sequencnumber */
555 /* Initialize dynamic willingness calculation */
556 olsr_init_willingness();
559 *Set up willingness/APM
561 if (olsr_cnf->willingness_auto) {
562 if (apm_init() < 0) {
563 OLSR_PRINTF(1, "Could not read APM info - setting default willingness(%d)\n", WILL_DEFAULT);
565 olsr_syslog(OLSR_LOG_ERR,
566 "Could not read APM info - setting default willingness(%d)\n",
569 olsr_cnf->willingness_auto = 0;
570 olsr_cnf->willingness = WILL_DEFAULT;
572 olsr_cnf->willingness = olsr_calculate_willingness();
574 OLSR_PRINTF(1, "Willingness set to %d - next update in %.1f secs\n", olsr_cnf->willingness, (double)olsr_cnf->will_int);
581 /* Initializing networkinterfaces */
582 if (!olsr_init_interfacedb()) {
583 if (olsr_cnf->allow_no_interfaces) {
586 "No interfaces detected! This might be intentional, but it also might mean that your configuration is fubar.\nI will continue after 5 seconds...\n");
587 olsr_startup_sleep(5);
589 fprintf(stderr, "No interfaces detected!\nBailing out!\n");
590 olsr_exit(__func__, EXIT_FAILURE);
595 /* startup gateway system */
596 if (olsr_cnf->smart_gw_active) {
597 if (olsr_startup_gateways()) {
598 olsr_exit("Cannot startup gateway tunnels", 1);
601 #endif /* __linux__ */
603 olsr_do_startup_sleep();
605 /* Print heartbeat to stdout */
608 if (olsr_cnf->debug_level > 0 && isatty(STDOUT_FILENO)) {
609 olsr_start_timer(STDOUT_PULSE_INT, 0, OLSR_TIMER_PERIODIC,
610 &generate_stdout_pulse, NULL, 0);
612 #endif /* !defined WINCE */
614 /* Initialize the IPC socket */
616 if (olsr_cnf->ipc_connections > 0) {
618 olsr_exit("ipc_init failure", 1);
621 /* Initialisation of different tables to be used. */
626 if (olsr_cnf->debug_level == 0 && !olsr_cnf->no_fork) {
627 printf("%s detaching from the current process...\n", olsrd_version);
628 if (daemon(0, 0) < 0) {
629 printf("daemon(3) failed: %s\n", strerror(errno));
638 * Create locking file for olsrd, will be cleared after olsrd exits
640 for (i=5; i>=0; i--) {
641 OLSR_PRINTF(3, "Trying to get olsrd lock...\n");
642 if (!olsr_cnf->host_emul && olsr_create_lock_file(i > 0) == 0) {
643 /* lock sucessfully created */
652 OLSR_PRINTF(1, "Main address: %s\n\n", olsr_ip_to_string(&buf, &olsr_cnf->main_addr));
655 /* create policy routing rules with priorities if necessary */
656 if (DEF_RT_NONE != olsr_cnf->rt_table_pri) {
657 olsr_os_policy_rule(olsr_cnf->ip_version,
658 olsr_cnf->rt_table, olsr_cnf->rt_table_pri, NULL, true);
660 if (DEF_RT_NONE != olsr_cnf->rt_table_tunnel_pri) {
661 olsr_os_policy_rule(olsr_cnf->ip_version,
662 olsr_cnf->rt_table_tunnel, olsr_cnf->rt_table_tunnel_pri, NULL, true);
664 if (DEF_RT_NONE != olsr_cnf->rt_table_default_pri) {
665 olsr_os_policy_rule(olsr_cnf->ip_version,
666 olsr_cnf->rt_table_default, olsr_cnf->rt_table_default_pri, NULL, true);
669 /* rule to default table on all olsrd interfaces */
670 if (DEF_RT_NONE != olsr_cnf->rt_table_defaultolsr_pri) {
671 for (ifn = ifnet; ifn; ifn = ifn->int_next) {
672 olsr_os_policy_rule(olsr_cnf->ip_version,
673 olsr_cnf->rt_table_default, olsr_cnf->rt_table_defaultolsr_pri, ifn->int_name, true);
677 /* trigger gateway selection */
678 if (olsr_cnf->smart_gw_active) {
679 olsr_trigger_inetgw_startup();
682 /* trigger niit static route setup */
683 if (olsr_cnf->use_niit) {
684 olsr_setup_niit_routes();
687 /* create lo:olsr interface */
688 if (olsr_cnf->use_src_ip_routes) {
689 olsr_os_localhost_if(&olsr_cnf->main_addr, true);
691 #endif /* __linux__ */
693 /* Start syslog entry */
694 olsr_syslog(OLSR_LOG_INFO, "%s successfully started", olsrd_version);
700 /* ctrl-C and friends */
703 SetConsoleCtrlHandler(SignalHandler, true);
706 signal(SIGHUP, olsr_reconfigure);
707 signal(SIGINT, olsr_shutdown);
708 signal(SIGQUIT, olsr_shutdown);
709 signal(SIGILL, olsr_shutdown);
710 signal(SIGABRT, olsr_shutdown);
711 // signal(SIGSEGV, olsr_shutdown);
712 signal(SIGTERM, olsr_shutdown);
713 signal(SIGPIPE, SIG_IGN);
714 // Ignoring SIGUSR1 and SIGUSR1 by default to be able to use them in plugins
715 signal(SIGUSR1, SIG_IGN);
716 signal(SIGUSR2, SIG_IGN);
719 link_changes = false;
721 /* Starting scheduler */
724 /* Like we're ever going to reach this ;-) */
730 * Reconfigure olsrd. Currently kind of a hack...
732 *@param signo the signal that triggered this callback
734 void olsr_reconfigure(int signo __attribute__ ((unused))) {
735 /* if we are started with -nofork, we do not want to go into the
736 * background here. So we can simply stop on -HUP
738 olsr_syslog(OLSR_LOG_INFO, "sot: olsr_reconfigure()\n");
739 if (!olsr_cnf->no_fork) {
746 sigaddset(&sigs, SIGHUP);
747 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
748 for (i = sysconf(_SC_OPEN_MAX); --i > STDERR_FILENO;) {
751 printf("Restarting %s\n", olsr_argv[0]);
752 olsr_syslog(OLSR_LOG_INFO, "Restarting %s\n", olsr_argv[0]);
753 execv(olsr_argv[0], olsr_argv);
754 olsr_syslog(OLSR_LOG_ERR, "execv(%s) fails: %s!\n", olsr_argv[0],
757 olsr_syslog(OLSR_LOG_INFO, "RECONFIGURING!\n");
764 static void olsr_shutdown_messages(void) {
765 struct interface *ifn;
768 for (ifn = ifnet; ifn; ifn = ifn->int_next) {
769 /* clean output buffer */
772 /* send 'I'm gone' messages */
773 if (olsr_cnf->lq_level > 0) {
774 olsr_output_lq_tc(ifn);
775 olsr_output_lq_hello(ifn);
786 *Function called at shutdown. Signal handler
788 * @param signo the signal that triggered this call
792 SignalHandler(unsigned long signo)
794 static void olsr_shutdown(int signo __attribute__ ((unused)))
797 struct interface *ifn;
800 OLSR_PRINTF(1, "Received signal %d - shutting down\n", (int)signo);
803 OLSR_PRINTF(1, "Waiting for the scheduler to stop.\n");
805 olsr_win32_end_request = TRUE;
807 while (!olsr_win32_end_flag)
810 OLSR_PRINTF(1, "Scheduler stopped.\n");
813 /* clear all links and send empty hellos/tcs */
814 olsr_reset_all_links();
816 /* deactivate fisheye and immediate TCs */
817 olsr_cnf->lq_fish = 0;
818 for (ifn = ifnet; ifn; ifn = ifn->int_next) {
819 ifn->immediate_send_tc = false;
821 increase_local_ansn();
823 /* send first shutdown message burst */
824 olsr_shutdown_messages();
826 /* delete all routes */
827 olsr_delete_all_kernel_routes();
829 /* send second shutdown message burst */
830 olsr_shutdown_messages();
832 /* now try to cleanup the rest of the mess */
833 olsr_delete_all_tc_entries();
835 olsr_delete_all_mid_entries();
838 /* trigger gateway selection */
839 if (olsr_cnf->smart_gw_active) {
840 olsr_shutdown_gateways();
841 olsr_cleanup_gateways();
844 /* trigger niit static route cleanup */
845 if (olsr_cnf->use_niit) {
846 olsr_cleanup_niit_routes();
849 /* cleanup lo:olsr interface */
850 if (olsr_cnf->use_src_ip_routes) {
851 olsr_os_localhost_if(&olsr_cnf->main_addr, false);
853 #endif /* __linux__ */
855 olsr_destroy_parser();
857 OLSR_PRINTF(1, "Closing sockets...\n");
859 /* front-end IPC socket */
860 if (olsr_cnf->ipc_connections > 0) {
865 for (ifn = ifnet; ifn; ifn = ifn->int_next) {
866 close(ifn->olsr_socket);
867 close(ifn->send_socket);
870 if (DEF_RT_NONE != olsr_cnf->rt_table_defaultolsr_pri) {
871 olsr_os_policy_rule(olsr_cnf->ip_version, olsr_cnf->rt_table_default,
872 olsr_cnf->rt_table_defaultolsr_pri, ifn->int_name, false);
874 #endif /* __linux__ */
877 /* Closing plug-ins */
878 olsr_close_plugins();
880 /* Reset network settings */
881 net_os_restore_ifoptions();
884 close(olsr_cnf->ioctl_s);
887 if (DEF_RT_NONE != olsr_cnf->rt_table_pri) {
888 olsr_os_policy_rule(olsr_cnf->ip_version,
889 olsr_cnf->rt_table, olsr_cnf->rt_table_pri, NULL, false);
891 if (DEF_RT_NONE != olsr_cnf->rt_table_tunnel_pri) {
892 olsr_os_policy_rule(olsr_cnf->ip_version,
893 olsr_cnf->rt_table_tunnel, olsr_cnf->rt_table_tunnel_pri, NULL, false);
895 if (DEF_RT_NONE != olsr_cnf->rt_table_default_pri) {
896 olsr_os_policy_rule(olsr_cnf->ip_version,
897 olsr_cnf->rt_table_default, olsr_cnf->rt_table_default_pri, NULL, false);
899 close(olsr_cnf->rtnl_s);
900 close (olsr_cnf->rt_monitor_socket);
901 #endif /* __linux__ */
903 #if defined __FreeBSD__ || defined __FreeBSD_kernel__ || defined __APPLE__ || defined __NetBSD__ || defined __OpenBSD__
905 close(olsr_cnf->rts);
906 #endif /* defined __FreeBSD__ || defined __FreeBSD_kernel__ || defined __APPLE__ || defined __NetBSD__ || defined __OpenBSD__ */
908 /* Free cookies and memory pools attached. */
909 OLSR_PRINTF(0, "Free all memory...\n");
910 olsr_delete_all_cookies();
912 olsr_syslog(OLSR_LOG_INFO, "%s stopped", olsrd_version);
914 OLSR_PRINTF(1, "\n <<<< %s - terminating >>>>\n http://www.olsr.org\n", olsrd_version);
916 exit_value = olsr_cnf->exit_value;
917 olsrd_free_cnf(olsr_cnf);
923 * Print the command line usage
925 static void print_usage(bool error) {
929 "usage: olsrd [-f <configfile>] [ -i interface1 interface2 ... ]\n"
930 " [-d <debug_level>] [-ipv6] [-multi <IPv6 multicast address>]\n"
931 " [-lql <LQ level>] [-lqw <LQ winsize>] [-lqnt <nat threshold>]\n"
932 " [-bcast <broadcastaddr>] [-ipc] [-delgw]\n"
933 " [-hint <hello interval (secs)>] [-tcint <tc interval (secs)>]\n"
934 " [-midint <mid interval (secs)>] [-hnaint <hna interval (secs)>]\n"
935 " [-T <Polling Rate (secs)>] [-nofork] [-hemu <ip_address>]\n"
936 " [-lql <LQ level>] [-lqa <LQ aging factor>]\n"
937 " [-pidfile <pid file>]\n",
938 error ? "Error in command line parameters!\n" : "");
942 * Sets the provided configuration on all unconfigured
945 * @param ifs a linked list of interfaces to check and possible update
946 * @param cnf the default configuration to set on unconfigured interfaces
948 int set_default_ifcnfs(struct olsr_if *ifs, struct if_config_options *cnf) {
952 if (ifs->cnf == NULL) {
953 ifs->cnf = olsr_malloc(sizeof(struct if_config_options),
954 "Set default config");
963 #define NEXT_ARG do { argv++;argc--; } while (0)
964 #define CHECK_ARGC do { if(!argc) { \
966 fprintf(stderr, "You must provide a parameter when using the %s switch!\n", *argv); \
967 olsr_exit(__func__, EXIT_FAILURE); \
971 * Process command line arguments passed to olsrd
974 static int olsr_process_arguments(int argc, char *argv[],
975 struct olsrd_config *cnf, struct if_config_options *ifcnf) {
982 if (strcmp(*argv, "-int") == 0) {
991 if (strcmp(*argv, "-f") == 0) {
992 fprintf(stderr, "Configfilename must ALWAYS be first argument!\n\n");
993 olsr_exit(__func__, EXIT_FAILURE);
999 if (strcmp(*argv, "-ipv6") == 0) {
1000 cnf->ip_version = AF_INET6;
1007 if (strcmp(*argv, "-bcast") == 0) {
1012 if (inet_aton(*argv, &in) == 0) {
1013 printf("Invalid broadcast address! %s\nSkipping it!\n", *argv);
1016 memcpy(&ifcnf->ipv4_multicast.v4, &in.s_addr, sizeof(ifcnf->ipv4_multicast.v4));
1023 if (strcmp(*argv, "-lql") == 0) {
1028 /* Sanity checking is done later */
1029 sscanf(*argv, "%d", &tmp_lq_level);
1030 olsr_cnf->lq_level = tmp_lq_level;
1037 if (strcmp(*argv, "-lqa") == 0) {
1042 sscanf(*argv, "%f", &tmp_lq_aging);
1044 if (tmp_lq_aging < (float)MIN_LQ_AGING || tmp_lq_aging > (float)MAX_LQ_AGING) {
1045 printf("LQ aging factor %f not allowed. Range [%f-%f]\n", (double)tmp_lq_aging,
1046 (double)MIN_LQ_AGING, (double)MAX_LQ_AGING);
1047 olsr_exit(__func__, EXIT_FAILURE);
1049 olsr_cnf->lq_aging = tmp_lq_aging;
1056 if (strcmp(*argv, "-lqnt") == 0) {
1057 float tmp_lq_nat_thresh;
1061 sscanf(*argv, "%f", &tmp_lq_nat_thresh);
1063 if (tmp_lq_nat_thresh < 0.1f || tmp_lq_nat_thresh > 1.0f) {
1064 printf("NAT threshold %f not allowed. Range [%f-%f]\n",
1065 (double)tmp_lq_nat_thresh, (double)0.1, (double)1.0);
1066 olsr_exit(__func__, EXIT_FAILURE);
1068 olsr_cnf->lq_nat_thresh = tmp_lq_nat_thresh;
1073 * Enable additional debugging information to be logged.
1075 if (strcmp(*argv, "-d") == 0) {
1079 sscanf(*argv, "%d", &cnf->debug_level);
1084 * Interfaces to be used by olsrd.
1086 if (strcmp(*argv, "-i") == 0) {
1090 if (*argv[0] == '-') {
1091 fprintf(stderr, "You must provide an interface label!\n");
1092 olsr_exit(__func__, EXIT_FAILURE);
1094 printf("Queuing if %s\n", *argv);
1095 olsr_create_olsrif(*argv, false);
1097 while ((argc - 1) && (argv[1][0] != '-')) {
1099 printf("Queuing if %s\n", *argv);
1100 olsr_create_olsrif(*argv, false);
1106 * Set the hello interval to be used by olsrd.
1109 if (strcmp(*argv, "-hint") == 0) {
1112 sscanf(*argv, "%f", &ifcnf->hello_params.emission_interval);
1113 ifcnf->hello_params.validity_time = ifcnf->hello_params.emission_interval
1119 * Set the HNA interval to be used by olsrd.
1122 if (strcmp(*argv, "-hnaint") == 0) {
1125 sscanf(*argv, "%f", &ifcnf->hna_params.emission_interval);
1126 ifcnf->hna_params.validity_time = ifcnf->hna_params.emission_interval * 3;
1131 * Set the MID interval to be used by olsrd.
1134 if (strcmp(*argv, "-midint") == 0) {
1137 sscanf(*argv, "%f", &ifcnf->mid_params.emission_interval);
1138 ifcnf->mid_params.validity_time = ifcnf->mid_params.emission_interval * 3;
1143 * Set the tc interval to be used by olsrd.
1146 if (strcmp(*argv, "-tcint") == 0) {
1149 sscanf(*argv, "%f", &ifcnf->tc_params.emission_interval);
1150 ifcnf->tc_params.validity_time = ifcnf->tc_params.emission_interval * 3;
1155 * Set the polling interval to be used by olsrd.
1157 if (strcmp(*argv, "-T") == 0) {
1160 sscanf(*argv, "%f", &cnf->pollrate);
1165 * Should we set up and send on a IPC socket for the front-end?
1167 if (strcmp(*argv, "-ipc") == 0) {
1168 cnf->ipc_connections = 1;
1173 * IPv6 multicast addr
1175 if (strcmp(*argv, "-multi") == 0) {
1176 struct in6_addr in6;
1179 if (inet_pton(AF_INET6, *argv, &in6) <= 0) {
1180 fprintf(stderr, "Failed converting IP address %s\n", *argv);
1184 memcpy(&ifcnf->ipv6_multicast, &in6, sizeof(struct in6_addr));
1192 if (strcmp(*argv, "-hemu") == 0) {
1194 struct olsr_if *ifa;
1198 if (inet_pton(AF_INET, *argv, &in) <= 0) {
1199 fprintf(stderr, "Failed converting IP address %s\n", *argv);
1202 /* Add hemu interface */
1204 ifa = olsr_create_olsrif("hcif01", true);
1209 ifa->cnf = get_default_if_config();
1210 ifa->host_emul = true;
1211 memset(&ifa->hemu_ip, 0, sizeof(ifa->hemu_ip));
1212 memcpy(&ifa->hemu_ip, &in, sizeof(in));
1213 cnf->host_emul = true;
1219 * Delete possible default GWs
1221 if (strcmp(*argv, "-delgw") == 0) {
1222 olsr_cnf->del_gws = true;
1226 if (strcmp(*argv, "-nofork") == 0) {
1227 cnf->no_fork = true;
1231 if (strcmp(*argv, "-pidfile") == 0) {
1235 cnf->pidfile = *argv;
1247 * indent-tabs-mode: nil