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