paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * OSPFd main routine. |
| 3 | * Copyright (C) 1998, 99 Kunihiro Ishiguro, Toshiaki Takada |
| 4 | * |
| 5 | * This file is part of GNU Zebra. |
| 6 | * |
| 7 | * GNU Zebra is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the |
| 9 | * Free Software Foundation; either version 2, or (at your option) any |
| 10 | * later version. |
| 11 | * |
| 12 | * GNU Zebra is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 19 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 20 | * 02111-1307, USA. |
| 21 | */ |
| 22 | |
| 23 | #include <zebra.h> |
| 24 | |
gdt | 5e4fa16 | 2004-03-16 14:38:36 +0000 | [diff] [blame] | 25 | #include <lib/version.h> |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 26 | #include "getopt.h" |
| 27 | #include "thread.h" |
| 28 | #include "prefix.h" |
| 29 | #include "linklist.h" |
| 30 | #include "if.h" |
| 31 | #include "vector.h" |
| 32 | #include "vty.h" |
| 33 | #include "command.h" |
| 34 | #include "filter.h" |
| 35 | #include "plist.h" |
| 36 | #include "stream.h" |
| 37 | #include "log.h" |
| 38 | #include "memory.h" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 39 | #include "privs.h" |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 40 | #include "sigevent.h" |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 41 | #include "zclient.h" |
Feng Lu | 126215c | 2015-05-22 11:39:58 +0200 | [diff] [blame] | 42 | #include "vrf.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 43 | |
| 44 | #include "ospfd/ospfd.h" |
| 45 | #include "ospfd/ospf_interface.h" |
| 46 | #include "ospfd/ospf_asbr.h" |
| 47 | #include "ospfd/ospf_lsa.h" |
| 48 | #include "ospfd/ospf_lsdb.h" |
| 49 | #include "ospfd/ospf_neighbor.h" |
| 50 | #include "ospfd/ospf_dump.h" |
| 51 | #include "ospfd/ospf_zebra.h" |
| 52 | #include "ospfd/ospf_vty.h" |
| 53 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 54 | /* ospfd privileges */ |
| 55 | zebra_capabilities_t _caps_p [] = |
| 56 | { |
paul | ceacedb | 2005-09-29 14:39:32 +0000 | [diff] [blame] | 57 | ZCAP_NET_RAW, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 58 | ZCAP_BIND, |
paul | ceacedb | 2005-09-29 14:39:32 +0000 | [diff] [blame] | 59 | ZCAP_NET_ADMIN, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | struct zebra_privs_t ospfd_privs = |
| 63 | { |
paul | d81fadf | 2003-08-14 05:32:12 +0000 | [diff] [blame] | 64 | #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP) |
| 65 | .user = QUAGGA_USER, |
| 66 | .group = QUAGGA_GROUP, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 67 | #endif |
| 68 | #if defined(VTY_GROUP) |
| 69 | .vty_group = VTY_GROUP, |
| 70 | #endif |
| 71 | .caps_p = _caps_p, |
Balaji.G | 837d16c | 2012-09-26 14:09:10 +0530 | [diff] [blame] | 72 | .cap_num_p = array_size(_caps_p), |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 73 | .cap_num_i = 0 |
| 74 | }; |
| 75 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 76 | /* Configuration filename and directory. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 77 | char config_default[] = SYSCONFDIR OSPF_DEFAULT_CONFIG; |
| 78 | |
| 79 | /* OSPFd options. */ |
| 80 | struct option longopts[] = |
| 81 | { |
| 82 | { "daemon", no_argument, NULL, 'd'}, |
| 83 | { "config_file", required_argument, NULL, 'f'}, |
| 84 | { "pid_file", required_argument, NULL, 'i'}, |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 85 | { "socket", required_argument, NULL, 'z'}, |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 86 | { "dryrun", no_argument, NULL, 'C'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 87 | { "help", no_argument, NULL, 'h'}, |
| 88 | { "vty_addr", required_argument, NULL, 'A'}, |
| 89 | { "vty_port", required_argument, NULL, 'P'}, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 90 | { "user", required_argument, NULL, 'u'}, |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 91 | { "group", required_argument, NULL, 'g'}, |
hasso | c3abdb7 | 2004-10-11 16:27:03 +0000 | [diff] [blame] | 92 | { "apiserver", no_argument, NULL, 'a'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 93 | { "version", no_argument, NULL, 'v'}, |
| 94 | { 0 } |
| 95 | }; |
| 96 | |
| 97 | /* OSPFd program name */ |
| 98 | |
| 99 | /* Master of threads. */ |
| 100 | struct thread_master *master; |
| 101 | |
| 102 | /* Process ID saved for use by init system */ |
hasso | eb1ce60 | 2004-10-08 08:17:22 +0000 | [diff] [blame] | 103 | const char *pid_file = PATH_OSPFD_PID; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 104 | |
hasso | d68614d | 2004-10-13 09:32:48 +0000 | [diff] [blame] | 105 | #ifdef SUPPORT_OSPF_API |
hasso | f4d58ce | 2004-10-12 06:13:54 +0000 | [diff] [blame] | 106 | extern int ospf_apiserver_enable; |
hasso | d68614d | 2004-10-13 09:32:48 +0000 | [diff] [blame] | 107 | #endif /* SUPPORT_OSPF_API */ |
hasso | c3abdb7 | 2004-10-11 16:27:03 +0000 | [diff] [blame] | 108 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 109 | /* Help information display. */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 110 | static void __attribute__ ((noreturn)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 111 | usage (char *progname, int status) |
| 112 | { |
| 113 | if (status != 0) |
| 114 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); |
| 115 | else |
| 116 | { |
| 117 | printf ("Usage : %s [OPTION...]\n\ |
| 118 | Daemon which manages OSPF.\n\n\ |
| 119 | -d, --daemon Runs in daemon mode\n\ |
| 120 | -f, --config_file Set configuration file name\n\ |
| 121 | -i, --pid_file Set process identifier file name\n\ |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 122 | -z, --socket Set path of zebra socket\n\ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 123 | -A, --vty_addr Set vty's bind address\n\ |
| 124 | -P, --vty_port Set vty's port number\n\ |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 125 | -u, --user User to run as\n\ |
| 126 | -g, --group Group to run as\n\ |
hasso | c3abdb7 | 2004-10-11 16:27:03 +0000 | [diff] [blame] | 127 | -a. --apiserver Enable OSPF apiserver\n\ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 128 | -v, --version Print program version\n\ |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 129 | -C, --dryrun Check configuration for validity and exit\n\ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 130 | -h, --help Display this help and exit\n\ |
| 131 | \n\ |
| 132 | Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS); |
| 133 | } |
| 134 | exit (status); |
| 135 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 136 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 137 | /* SIGHUP handler. */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 138 | static void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 139 | sighup (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 140 | { |
| 141 | zlog (NULL, LOG_INFO, "SIGHUP received"); |
| 142 | } |
| 143 | |
paul | 88d6cf3 | 2005-10-29 12:50:09 +0000 | [diff] [blame] | 144 | /* SIGINT / SIGTERM handler. */ |
| 145 | static void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 146 | sigint (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 147 | { |
ajs | 887c44a | 2004-12-03 16:36:46 +0000 | [diff] [blame] | 148 | zlog_notice ("Terminating on signal"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 149 | ospf_terminate (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* SIGUSR1 handler. */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 153 | static void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 154 | sigusr1 (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 155 | { |
| 156 | zlog_rotate (NULL); |
| 157 | } |
| 158 | |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 159 | struct quagga_signal_t ospf_signals[] = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 160 | { |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 161 | { |
| 162 | .signal = SIGHUP, |
| 163 | .handler = &sighup, |
| 164 | }, |
| 165 | { |
| 166 | .signal = SIGUSR1, |
| 167 | .handler = &sigusr1, |
| 168 | }, |
| 169 | { |
| 170 | .signal = SIGINT, |
| 171 | .handler = &sigint, |
| 172 | }, |
hasso | f571dab | 2004-03-22 08:55:25 +0000 | [diff] [blame] | 173 | { |
| 174 | .signal = SIGTERM, |
| 175 | .handler = &sigint, |
| 176 | }, |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 177 | }; |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 178 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 179 | /* OSPFd main routine. */ |
| 180 | int |
| 181 | main (int argc, char **argv) |
| 182 | { |
| 183 | char *p; |
| 184 | char *vty_addr = NULL; |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 185 | int vty_port = OSPF_VTY_PORT; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 186 | int daemon_mode = 0; |
| 187 | char *config_file = NULL; |
| 188 | char *progname; |
| 189 | struct thread thread; |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 190 | int dryrun = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 191 | |
| 192 | /* Set umask before anything for security */ |
| 193 | umask (0027); |
| 194 | |
David Lamparter | 822d8f5 | 2012-12-04 11:11:41 -0800 | [diff] [blame] | 195 | #ifdef SUPPORT_OSPF_API |
| 196 | /* OSPF apiserver is disabled by default. */ |
| 197 | ospf_apiserver_enable = 0; |
| 198 | #endif /* SUPPORT_OSPF_API */ |
| 199 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 200 | /* get program name */ |
| 201 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); |
| 202 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 203 | while (1) |
| 204 | { |
| 205 | int opt; |
| 206 | |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 207 | opt = getopt_long (argc, argv, "df:i:z:hA:P:u:g:avC", longopts, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 208 | |
| 209 | if (opt == EOF) |
| 210 | break; |
| 211 | |
| 212 | switch (opt) |
| 213 | { |
| 214 | case 0: |
| 215 | break; |
| 216 | case 'd': |
| 217 | daemon_mode = 1; |
| 218 | break; |
| 219 | case 'f': |
| 220 | config_file = optarg; |
| 221 | break; |
| 222 | case 'A': |
| 223 | vty_addr = optarg; |
| 224 | break; |
| 225 | case 'i': |
| 226 | pid_file = optarg; |
| 227 | break; |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 228 | case 'z': |
| 229 | zclient_serv_path_set (optarg); |
| 230 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 231 | case 'P': |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 232 | /* Deal with atoi() returning 0 on failure, and ospfd not |
| 233 | listening on ospfd port... */ |
| 234 | if (strcmp(optarg, "0") == 0) |
| 235 | { |
| 236 | vty_port = 0; |
| 237 | break; |
| 238 | } |
| 239 | vty_port = atoi (optarg); |
Paul Jakma | 0d6b2ee | 2008-05-29 18:29:16 +0000 | [diff] [blame] | 240 | if (vty_port <= 0 || vty_port > 0xffff) |
| 241 | vty_port = OSPF_VTY_PORT; |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 242 | break; |
hasso | c3abdb7 | 2004-10-11 16:27:03 +0000 | [diff] [blame] | 243 | case 'u': |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 244 | ospfd_privs.user = optarg; |
| 245 | break; |
| 246 | case 'g': |
| 247 | ospfd_privs.group = optarg; |
hasso | c3abdb7 | 2004-10-11 16:27:03 +0000 | [diff] [blame] | 248 | break; |
hasso | d68614d | 2004-10-13 09:32:48 +0000 | [diff] [blame] | 249 | #ifdef SUPPORT_OSPF_API |
hasso | c3abdb7 | 2004-10-11 16:27:03 +0000 | [diff] [blame] | 250 | case 'a': |
| 251 | ospf_apiserver_enable = 1; |
| 252 | break; |
hasso | d68614d | 2004-10-13 09:32:48 +0000 | [diff] [blame] | 253 | #endif /* SUPPORT_OSPF_API */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 254 | case 'v': |
| 255 | print_version (progname); |
| 256 | exit (0); |
| 257 | break; |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 258 | case 'C': |
| 259 | dryrun = 1; |
| 260 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 261 | case 'h': |
| 262 | usage (progname, 0); |
| 263 | break; |
| 264 | default: |
| 265 | usage (progname, 1); |
| 266 | break; |
| 267 | } |
| 268 | } |
| 269 | |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 270 | /* Invoked by a priviledged user? -- endo. */ |
| 271 | if (geteuid () != 0) |
| 272 | { |
| 273 | errno = EPERM; |
| 274 | perror (progname); |
| 275 | exit (1); |
| 276 | } |
| 277 | |
| 278 | zlog_default = openzlog (progname, ZLOG_OSPF, |
| 279 | LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); |
| 280 | |
| 281 | /* OSPF master init. */ |
| 282 | ospf_master_init (); |
| 283 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 284 | /* Initializations. */ |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 285 | master = om->master; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 286 | |
| 287 | /* Library inits. */ |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 288 | zprivs_init (&ospfd_privs); |
Balaji.G | 837d16c | 2012-09-26 14:09:10 +0530 | [diff] [blame] | 289 | signal_init (master, array_size(ospf_signals), ospf_signals); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 290 | cmd_init (1); |
| 291 | debug_init (); |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 292 | vty_init (master); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 293 | memory_init (); |
Feng Lu | 126215c | 2015-05-22 11:39:58 +0200 | [diff] [blame] | 294 | vrf_init (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 295 | |
| 296 | access_list_init (); |
| 297 | prefix_list_init (); |
| 298 | |
| 299 | /* OSPFd inits. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 300 | ospf_if_init (); |
| 301 | ospf_zebra_init (); |
| 302 | |
| 303 | /* OSPF vty inits. */ |
| 304 | ospf_vty_init (); |
| 305 | ospf_vty_show_init (); |
| 306 | |
| 307 | ospf_route_map_init (); |
| 308 | #ifdef HAVE_SNMP |
| 309 | ospf_snmp_init (); |
| 310 | #endif /* HAVE_SNMP */ |
| 311 | #ifdef HAVE_OPAQUE_LSA |
| 312 | ospf_opaque_init (); |
| 313 | #endif /* HAVE_OPAQUE_LSA */ |
| 314 | |
Vipin Kumar | 4bab680 | 2014-09-30 14:32:22 -0700 | [diff] [blame] | 315 | /* Need to initialize the default ospf structure, so the interface mode |
| 316 | commands can be duly processed if they are received before 'router ospf', |
| 317 | when quagga(ospfd) is restarted */ |
| 318 | if (!ospf_get()) |
| 319 | { |
| 320 | zlog_err("OSPF instance init failed: %s", strerror(errno)); |
| 321 | exit (1); |
| 322 | } |
| 323 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 324 | /* Get configuration file. */ |
hasso | 320ec10 | 2004-06-20 19:54:37 +0000 | [diff] [blame] | 325 | vty_read_config (config_file, config_default); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 326 | |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 327 | /* Start execution only if not in dry-run mode */ |
| 328 | if (dryrun) |
| 329 | return(0); |
| 330 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 331 | /* Change to the daemon program. */ |
Stephen Hemminger | 065de90 | 2009-08-07 11:13:49 -0700 | [diff] [blame] | 332 | if (daemon_mode && daemon (0, 0) < 0) |
| 333 | { |
| 334 | zlog_err("OSPFd daemon failed: %s", strerror(errno)); |
| 335 | exit (1); |
| 336 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 337 | |
| 338 | /* Process id file create. */ |
| 339 | pid_output (pid_file); |
| 340 | |
| 341 | /* Create VTY socket */ |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 342 | vty_serv_sock (vty_addr, vty_port, OSPF_VTYSH_PATH); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 343 | |
| 344 | /* Print banner. */ |
ajs | 887c44a | 2004-12-03 16:36:46 +0000 | [diff] [blame] | 345 | zlog_notice ("OSPFd %s starting: vty@%d", QUAGGA_VERSION, vty_port); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 346 | |
| 347 | /* Fetch next active thread. */ |
| 348 | while (thread_fetch (master, &thread)) |
| 349 | thread_call (&thread); |
| 350 | |
| 351 | /* Not reached. */ |
paul | e8e1946 | 2006-01-19 20:16:55 +0000 | [diff] [blame] | 352 | return (0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 353 | } |
| 354 | |