1 /* Main routine of bgpd.
2 Copyright (C) 1996, 97, 98, 1999 Kunihiro Ishiguro
4 This file is part of GNU Zebra.
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
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.
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
28 #include <lib/version.h>
40 #include "workqueue.h"
42 #include "bgpd/bgpd.h"
43 #include "bgpd/bgp_attr.h"
44 #include "bgpd/bgp_mplsvpn.h"
45 #include "bgpd/bgp_aspath.h"
46 #include "bgpd/bgp_dump.h"
47 #include "bgpd/bgp_route.h"
48 #include "bgpd/bgp_nexthop.h"
49 #include "bgpd/bgp_regex.h"
50 #include "bgpd/bgp_clist.h"
51 #include "bgpd/bgp_debug.h"
52 #include "bgpd/bgp_filter.h"
53 #include "bgpd/bgp_zebra.h"
55 /* bgpd options, we use GNU getopt library. */
56 static const struct option longopts[] =
58 { "daemon", no_argument, NULL, 'd'},
59 { "config_file", required_argument, NULL, 'f'},
60 { "pid_file", required_argument, NULL, 'i'},
61 { "socket", required_argument, NULL, 'z'},
62 { "bgp_port", required_argument, NULL, 'p'},
63 { "listenon", required_argument, NULL, 'l'},
64 { "vty_addr", required_argument, NULL, 'A'},
65 { "vty_port", required_argument, NULL, 'P'},
66 { "retain", no_argument, NULL, 'r'},
67 { "no_kernel", no_argument, NULL, 'n'},
68 { "user", required_argument, NULL, 'u'},
69 { "group", required_argument, NULL, 'g'},
70 { "skip_runas", no_argument, NULL, 'S'},
71 { "version", no_argument, NULL, 'v'},
72 { "dryrun", no_argument, NULL, 'C'},
73 { "help", no_argument, NULL, 'h'},
77 /* signal definitions */
82 static void bgp_exit (int);
84 static struct quagga_signal_t bgp_signals[] =
104 /* Configuration file and directory. */
105 char config_default[] = SYSCONFDIR BGP_DEFAULT_CONFIG;
107 /* Route retain mode flag. */
108 static int retain_mode = 0;
110 /* Manually specified configuration file name. */
111 char *config_file = NULL;
113 /* Process ID saved for use by init system */
114 static const char *pid_file = PATH_BGPD_PID;
116 /* VTY port number and address. */
117 int vty_port = BGP_VTY_PORT;
118 char *vty_addr = NULL;
121 static zebra_capabilities_t _caps_p [] =
128 struct zebra_privs_t bgpd_privs =
130 #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
132 .group = QUAGGA_GROUP,
135 .vty_group = VTY_GROUP,
138 .cap_num_p = array_size(_caps_p),
142 /* Help information display. */
144 usage (char *progname, int status)
147 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
150 printf ("Usage : %s [OPTION...]\n\n\
151 Daemon which manages kernel routing table management and \
152 redistribution between different routing protocols.\n\n\
153 -d, --daemon Runs in daemon mode\n\
154 -f, --config_file Set configuration file name\n\
155 -i, --pid_file Set process identifier file name\n\
156 -z, --socket Set path of zebra socket\n\
157 -p, --bgp_port Set bgp protocol's port number\n\
158 -l, --listenon Listen on specified address (implies -n)\n\
159 -A, --vty_addr Set vty's bind address\n\
160 -P, --vty_port Set vty's port number\n\
161 -r, --retain When program terminates, retain added route by bgpd.\n\
162 -n, --no_kernel Do not install route to kernel.\n\
163 -u, --user User to run as\n\
164 -g, --group Group to run as\n\
165 -S, --skip_runas Skip user and group run as\n\
166 -v, --version Print program version\n\
167 -C, --dryrun Check configuration for validity and exit\n\
168 -h, --help Display this help and exit\n\
170 Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
176 /* SIGHUP handler. */
180 zlog (NULL, LOG_INFO, "SIGHUP received");
182 /* Terminate all thread. */
185 zlog_info ("bgpd restarting!");
187 /* Reload config file. */
188 vty_read_config (config_file, config_default);
190 /* Create VTY's socket */
191 vty_serv_sock (vty_addr, vty_port, BGP_VTYSH_PATH);
193 /* Try to return to normal operation. */
196 /* SIGINT handler. */
200 zlog_notice ("Terminating on signal");
205 if (bgpd_privs.user) /* NULL if skip_runas flag set */
206 zprivs_terminate (&bgpd_privs);
212 /* SIGUSR1 handler. */
220 Try to free up allocations we know about so that diagnostic tools such as
221 valgrind are able to better illuminate leaks.
223 Zebra route removal and protocol teardown are not meant to be done here.
224 For example, "retain_mode" may be set.
227 bgp_exit (int status)
230 struct listnode *node, *nnode;
232 struct interface *ifp;
234 /* it only makes sense for this to be called on a clean exit */
235 assert (status == 0);
237 /* reverse bgp_master_init */
238 for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
244 * bgp_delete can re-allocate the process queues after they were
245 * deleted in bgp_terminate. delete them again.
247 * It might be better to ensure the RIBs (including static routes)
248 * are cleared by bgp_terminate() during its call to bgp_cleanup_routes(),
249 * which currently only deletes the kernel routes.
251 if (bm->process_main_queue)
253 work_queue_free (bm->process_main_queue);
254 bm->process_main_queue = NULL;
256 if (bm->process_rsclient_queue)
258 work_queue_free (bm->process_rsclient_queue);
259 bm->process_rsclient_queue = NULL;
262 /* reverse bgp_master_init */
263 for (ALL_LIST_ELEMENTS_RO(bm->listen_sockets, node, socket))
265 if (close ((int)(long)socket) == -1)
266 zlog_err ("close (%d): %s", (int)(long)socket, safe_strerror (errno));
268 list_delete (bm->listen_sockets);
270 /* reverse bgp_zebra_init/if_init */
272 if_add_hook (IF_DELETE_HOOK, NULL);
273 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
275 struct listnode *c_node, *c_nnode;
278 for (ALL_LIST_ELEMENTS (ifp->connected, c_node, c_nnode, c))
279 bgp_connected_delete (c);
282 /* reverse bgp_attr_init */
285 /* reverse bgp_dump_init */
288 /* reverse bgp_route_init */
291 /* reverse bgp_route_map_init/route_map_init */
294 /* reverse access_list_init */
295 access_list_add_hook (NULL);
296 access_list_delete_hook (NULL);
297 access_list_reset ();
299 /* reverse bgp_filter_init */
300 as_list_add_hook (NULL);
301 as_list_delete_hook (NULL);
304 /* reverse prefix_list_init */
305 prefix_list_add_hook (NULL);
306 prefix_list_delete_hook (NULL);
307 prefix_list_reset ();
309 /* reverse community_list_init */
310 community_list_terminate (bgp_clist);
315 bgp_address_destroy();
319 stream_free (bgp_nexthop_buf);
320 if (bgp_ifindices_buf)
321 stream_free (bgp_ifindices_buf);
323 /* reverse bgp_scan_init */
326 /* reverse bgp_master_init */
328 thread_master_free (bm->master);
331 closezlog (zlog_default);
333 if (CONF_BGP_DEBUG (normal, NORMAL))
334 log_memstats_stderr ("bgpd");
339 /* Main routine of bgpd. Treatment of argument and start bgp finite
340 state machine is handled at here. */
342 main (int argc, char **argv)
352 /* Set umask before anything for security */
355 /* Preserve name of myself. */
356 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
358 zlog_default = openzlog (progname, ZLOG_BGP,
359 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
361 /* BGP master init. */
364 /* Command line argument treatment. */
367 opt = getopt_long (argc, argv, "df:i:z:hp:l:A:P:rnu:g:vCS", longopts, 0);
380 config_file = optarg;
386 zclient_serv_path_set (optarg);
389 tmp_port = atoi (optarg);
390 if (tmp_port <= 0 || tmp_port > 0xffff)
391 bm->port = BGP_PORT_DEFAULT;
399 /* Deal with atoi() returning 0 on failure, and bgpd not
400 listening on bgp port... */
401 if (strcmp(optarg, "0") == 0)
406 vty_port = atoi (optarg);
407 if (vty_port <= 0 || vty_port > 0xffff)
408 vty_port = BGP_VTY_PORT;
414 bm->address = optarg;
415 /* listenon implies -n */
417 bgp_option_set (BGP_OPT_NO_FIB);
420 bgpd_privs.user = optarg;
423 bgpd_privs.group = optarg;
425 case 'S': /* skip run as = override bgpd_privs */
429 print_version (progname);
444 /* Initializations. */
445 srandom (time (NULL));
446 signal_init (bm->master, array_size(bgp_signals), bgp_signals);
448 memset (&bgpd_privs, 0, sizeof (bgpd_privs));
449 zprivs_init (&bgpd_privs);
451 vty_init (bm->master);
455 /* BGP related initialization. */
458 /* Parse config file. */
459 vty_read_config (config_file, config_default);
461 /* Start execution only if not in dry-run mode */
465 /* Turn into daemon if daemon_mode is set. */
466 if (daemon_mode && daemon (0, 0) < 0)
468 zlog_err("BGPd daemon failed: %s", strerror(errno));
473 /* Process ID file creation. */
474 pid_output (pid_file);
476 /* Make bgp vty socket. */
477 vty_serv_sock (vty_addr, vty_port, BGP_VTYSH_PATH);
480 zlog_notice ("BGPd %s starting: vty@%d, bgp@%s:%d pid %d", QUAGGA_VERSION,
482 (bm->address ? bm->address : "<all>"),
486 /* Start finite state machine, here we go! */
487 thread_main (bm->master);