paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* Main routine of bgpd. |
| 2 | Copyright (C) 1996, 97, 98, 1999 Kunihiro Ishiguro |
| 3 | |
| 4 | This file is part of GNU Zebra. |
| 5 | |
| 6 | GNU Zebra is free software; you can redistribute it and/or modify it |
| 7 | under the terms of the GNU General Public License as published by the |
| 8 | Free Software Foundation; either version 2, or (at your option) any |
| 9 | later version. |
| 10 | |
| 11 | GNU Zebra is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 18 | Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 19 | 02111-1307, USA. */ |
| 20 | |
| 21 | #include <zebra.h> |
| 22 | |
| 23 | #include "vector.h" |
| 24 | #include "vty.h" |
| 25 | #include "command.h" |
| 26 | #include "getopt.h" |
| 27 | #include "thread.h" |
gdt | 5e4fa16 | 2004-03-16 14:38:36 +0000 | [diff] [blame] | 28 | #include <lib/version.h> |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 29 | #include "memory.h" |
| 30 | #include "prefix.h" |
| 31 | #include "log.h" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 32 | #include "privs.h" |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 33 | #include "sigevent.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 34 | |
| 35 | #include "bgpd/bgpd.h" |
| 36 | #include "bgpd/bgp_attr.h" |
| 37 | #include "bgpd/bgp_mplsvpn.h" |
| 38 | |
| 39 | /* bgpd options, we use GNU getopt library. */ |
| 40 | struct option longopts[] = |
| 41 | { |
| 42 | { "daemon", no_argument, NULL, 'd'}, |
| 43 | { "config_file", required_argument, NULL, 'f'}, |
| 44 | { "pid_file", required_argument, NULL, 'i'}, |
| 45 | { "bgp_port", required_argument, NULL, 'p'}, |
Paul Jakma | 3a02d1f | 2007-11-01 14:29:11 +0000 | [diff] [blame] | 46 | { "listenon", required_argument, NULL, 'l'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 47 | { "vty_addr", required_argument, NULL, 'A'}, |
| 48 | { "vty_port", required_argument, NULL, 'P'}, |
| 49 | { "retain", no_argument, NULL, 'r'}, |
| 50 | { "no_kernel", no_argument, NULL, 'n'}, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 51 | { "user", required_argument, NULL, 'u'}, |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 52 | { "group", required_argument, NULL, 'g'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 53 | { "version", no_argument, NULL, 'v'}, |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 54 | { "dryrun", no_argument, NULL, 'C'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 55 | { "help", no_argument, NULL, 'h'}, |
| 56 | { 0 } |
| 57 | }; |
| 58 | |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 59 | /* signal definitions */ |
| 60 | void sighup (void); |
| 61 | void sigint (void); |
| 62 | void sigusr1 (void); |
| 63 | |
| 64 | struct quagga_signal_t bgp_signals[] = |
| 65 | { |
| 66 | { |
| 67 | .signal = SIGHUP, |
| 68 | .handler = &sighup, |
| 69 | }, |
| 70 | { |
| 71 | .signal = SIGUSR1, |
| 72 | .handler = &sigusr1, |
| 73 | }, |
| 74 | { |
| 75 | .signal = SIGINT, |
| 76 | .handler = &sigint, |
| 77 | }, |
hasso | f571dab | 2004-03-22 08:55:25 +0000 | [diff] [blame] | 78 | { |
| 79 | .signal = SIGTERM, |
| 80 | .handler = &sigint, |
| 81 | }, |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 84 | /* Configuration file and directory. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 85 | char config_default[] = SYSCONFDIR BGP_DEFAULT_CONFIG; |
| 86 | |
| 87 | /* Route retain mode flag. */ |
| 88 | int retain_mode = 0; |
| 89 | |
| 90 | /* Master of threads. */ |
| 91 | struct thread_master *master; |
| 92 | |
| 93 | /* Manually specified configuration file name. */ |
| 94 | char *config_file = NULL; |
| 95 | |
| 96 | /* Process ID saved for use by init system */ |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 97 | const char *pid_file = PATH_BGPD_PID; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 98 | |
| 99 | /* VTY port number and address. */ |
| 100 | int vty_port = BGP_VTY_PORT; |
| 101 | char *vty_addr = NULL; |
| 102 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 103 | /* privileges */ |
| 104 | zebra_capabilities_t _caps_p [] = |
| 105 | { |
paul | 98f5163 | 2004-10-25 14:19:15 +0000 | [diff] [blame] | 106 | ZCAP_BIND, |
paul | ceacedb | 2005-09-29 14:39:32 +0000 | [diff] [blame] | 107 | ZCAP_NET_RAW, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | struct zebra_privs_t bgpd_privs = |
| 111 | { |
paul | d81fadf | 2003-08-14 05:32:12 +0000 | [diff] [blame] | 112 | #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP) |
| 113 | .user = QUAGGA_USER, |
| 114 | .group = QUAGGA_GROUP, |
| 115 | #endif |
| 116 | #ifdef VTY_GROUP |
| 117 | .vty_group = VTY_GROUP, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 118 | #endif |
| 119 | .caps_p = _caps_p, |
| 120 | .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]), |
| 121 | .cap_num_i = 0, |
| 122 | }; |
| 123 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 124 | /* Help information display. */ |
| 125 | static void |
| 126 | usage (char *progname, int status) |
| 127 | { |
| 128 | if (status != 0) |
| 129 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); |
| 130 | else |
| 131 | { |
| 132 | printf ("Usage : %s [OPTION...]\n\n\ |
| 133 | Daemon which manages kernel routing table management and \ |
| 134 | redistribution between different routing protocols.\n\n\ |
| 135 | -d, --daemon Runs in daemon mode\n\ |
| 136 | -f, --config_file Set configuration file name\n\ |
| 137 | -i, --pid_file Set process identifier file name\n\ |
| 138 | -p, --bgp_port Set bgp protocol's port number\n\ |
Paul Jakma | 3a02d1f | 2007-11-01 14:29:11 +0000 | [diff] [blame] | 139 | -l, --listenon Listen on specified address (implies -n)\n\ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 140 | -A, --vty_addr Set vty's bind address\n\ |
| 141 | -P, --vty_port Set vty's port number\n\ |
| 142 | -r, --retain When program terminates, retain added route by bgpd.\n\ |
| 143 | -n, --no_kernel Do not install route to kernel.\n\ |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 144 | -u, --user User to run as\n\ |
| 145 | -g, --group Group to run as\n\ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 146 | -v, --version Print program version\n\ |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 147 | -C, --dryrun Check configuration for validity and exit\n\ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 148 | -h, --help Display this help and exit\n\ |
| 149 | \n\ |
| 150 | Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS); |
| 151 | } |
| 152 | |
| 153 | exit (status); |
| 154 | } |
| 155 | |
| 156 | /* SIGHUP handler. */ |
| 157 | void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 158 | sighup (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 159 | { |
| 160 | zlog (NULL, LOG_INFO, "SIGHUP received"); |
| 161 | |
| 162 | /* Terminate all thread. */ |
| 163 | bgp_terminate (); |
| 164 | bgp_reset (); |
| 165 | zlog_info ("bgpd restarting!"); |
| 166 | |
| 167 | /* Reload config file. */ |
hasso | 320ec10 | 2004-06-20 19:54:37 +0000 | [diff] [blame] | 168 | vty_read_config (config_file, config_default); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 169 | |
| 170 | /* Create VTY's socket */ |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 171 | vty_serv_sock (vty_addr, vty_port, BGP_VTYSH_PATH); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 172 | |
| 173 | /* Try to return to normal operation. */ |
| 174 | } |
| 175 | |
| 176 | /* SIGINT handler. */ |
| 177 | void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 178 | sigint (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 179 | { |
ajs | 887c44a | 2004-12-03 16:36:46 +0000 | [diff] [blame] | 180 | zlog_notice ("Terminating on signal"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 181 | |
| 182 | if (! retain_mode) |
| 183 | bgp_terminate (); |
| 184 | |
| 185 | exit (0); |
| 186 | } |
| 187 | |
| 188 | /* SIGUSR1 handler. */ |
| 189 | void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 190 | sigusr1 (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 191 | { |
| 192 | zlog_rotate (NULL); |
| 193 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 194 | |
| 195 | /* Main routine of bgpd. Treatment of argument and start bgp finite |
| 196 | state machine is handled at here. */ |
| 197 | int |
| 198 | main (int argc, char **argv) |
| 199 | { |
| 200 | char *p; |
| 201 | int opt; |
| 202 | int daemon_mode = 0; |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 203 | int dryrun = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 204 | char *progname; |
| 205 | struct thread thread; |
| 206 | |
| 207 | /* Set umask before anything for security */ |
| 208 | umask (0027); |
| 209 | |
| 210 | /* Preserve name of myself. */ |
| 211 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); |
| 212 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 213 | zlog_default = openzlog (progname, ZLOG_BGP, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 214 | LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); |
| 215 | |
| 216 | /* BGP master init. */ |
| 217 | bgp_master_init (); |
| 218 | |
| 219 | /* Command line argument treatment. */ |
| 220 | while (1) |
| 221 | { |
Paul Jakma | 3a02d1f | 2007-11-01 14:29:11 +0000 | [diff] [blame] | 222 | opt = getopt_long (argc, argv, "df:i:hp:l:A:P:rnu:g:vC", longopts, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 223 | |
| 224 | if (opt == EOF) |
| 225 | break; |
| 226 | |
| 227 | switch (opt) |
| 228 | { |
| 229 | case 0: |
| 230 | break; |
| 231 | case 'd': |
| 232 | daemon_mode = 1; |
| 233 | break; |
| 234 | case 'f': |
| 235 | config_file = optarg; |
| 236 | break; |
| 237 | case 'i': |
| 238 | pid_file = optarg; |
| 239 | break; |
| 240 | case 'p': |
| 241 | bm->port = atoi (optarg); |
| 242 | break; |
| 243 | case 'A': |
| 244 | vty_addr = optarg; |
| 245 | break; |
| 246 | case 'P': |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 247 | /* Deal with atoi() returning 0 on failure, and bgpd not |
| 248 | listening on bgp port... */ |
| 249 | if (strcmp(optarg, "0") == 0) |
| 250 | { |
| 251 | vty_port = 0; |
| 252 | break; |
| 253 | } |
| 254 | vty_port = atoi (optarg); |
| 255 | vty_port = (vty_port ? vty_port : BGP_VTY_PORT); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 256 | break; |
| 257 | case 'r': |
| 258 | retain_mode = 1; |
| 259 | break; |
Paul Jakma | 3a02d1f | 2007-11-01 14:29:11 +0000 | [diff] [blame] | 260 | case 'l': |
| 261 | bm->address = optarg; |
| 262 | /* listenon implies -n */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 263 | case 'n': |
| 264 | bgp_option_set (BGP_OPT_NO_FIB); |
| 265 | break; |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 266 | case 'u': |
| 267 | bgpd_privs.user = optarg; |
| 268 | break; |
| 269 | case 'g': |
| 270 | bgpd_privs.group = optarg; |
| 271 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 272 | case 'v': |
| 273 | print_version (progname); |
| 274 | exit (0); |
| 275 | break; |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 276 | case 'C': |
| 277 | dryrun = 1; |
| 278 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 279 | case 'h': |
| 280 | usage (progname, 0); |
| 281 | break; |
| 282 | default: |
| 283 | usage (progname, 1); |
| 284 | break; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /* Make thread master. */ |
| 289 | master = bm->master; |
| 290 | |
| 291 | /* Initializations. */ |
| 292 | srand (time (NULL)); |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 293 | signal_init (master, Q_SIGC(bgp_signals), bgp_signals); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 294 | zprivs_init (&bgpd_privs); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 295 | cmd_init (1); |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 296 | vty_init (master); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 297 | memory_init (); |
| 298 | |
| 299 | /* BGP related initialization. */ |
| 300 | bgp_init (); |
| 301 | |
| 302 | /* Sort CLI commands. */ |
| 303 | sort_node (); |
| 304 | |
| 305 | /* Parse config file. */ |
hasso | 320ec10 | 2004-06-20 19:54:37 +0000 | [diff] [blame] | 306 | vty_read_config (config_file, config_default); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 307 | |
Paul Jakma | 876b8be | 2006-10-15 23:35:57 +0000 | [diff] [blame] | 308 | /* Start execution only if not in dry-run mode */ |
| 309 | if(dryrun) |
| 310 | return(0); |
| 311 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 312 | /* Turn into daemon if daemon_mode is set. */ |
| 313 | if (daemon_mode) |
| 314 | daemon (0, 0); |
| 315 | |
| 316 | /* Process ID file creation. */ |
| 317 | pid_output (pid_file); |
| 318 | |
| 319 | /* Make bgp vty socket. */ |
| 320 | vty_serv_sock (vty_addr, vty_port, BGP_VTYSH_PATH); |
| 321 | |
| 322 | /* Print banner. */ |
Paul Jakma | 3a02d1f | 2007-11-01 14:29:11 +0000 | [diff] [blame] | 323 | zlog_notice ("BGPd %s starting: vty@%d, bgp@%s:%d", QUAGGA_VERSION, |
| 324 | vty_port, bm->address, bm->port); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 325 | |
| 326 | /* Start finite state machine, here we go! */ |
| 327 | while (thread_fetch (master, &thread)) |
| 328 | thread_call (&thread); |
| 329 | |
| 330 | /* Not reached. */ |
paul | e8e1946 | 2006-01-19 20:16:55 +0000 | [diff] [blame] | 331 | return (0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 332 | } |