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>
39 #include "bgpd/bgpd.h"
40 #include "bgpd/bgp_attr.h"
41 #include "bgpd/bgp_mplsvpn.h"
42 #include "bgpd/bgp_aspath.h"
43 #include "bgpd/bgp_dump.h"
44 #include "bgpd/bgp_route.h"
45 #include "bgpd/bgp_nexthop.h"
46 #include "bgpd/bgp_regex.h"
47 #include "bgpd/bgp_clist.h"
48 #include "bgpd/bgp_debug.h"
49 #include "bgpd/bgp_filter.h"
51 /* bgpd options, we use GNU getopt library. */
52 static const struct option longopts
[] =
54 { "daemon", no_argument
, NULL
, 'd'},
55 { "config_file", required_argument
, NULL
, 'f'},
56 { "pid_file", required_argument
, NULL
, 'i'},
57 { "bgp_port", required_argument
, NULL
, 'p'},
58 { "listenon", required_argument
, NULL
, 'l'},
59 { "vty_addr", required_argument
, NULL
, 'A'},
60 { "vty_port", required_argument
, NULL
, 'P'},
61 { "retain", no_argument
, NULL
, 'r'},
62 { "no_kernel", no_argument
, NULL
, 'n'},
63 { "user", required_argument
, NULL
, 'u'},
64 { "group", required_argument
, NULL
, 'g'},
65 { "version", no_argument
, NULL
, 'v'},
66 { "dryrun", no_argument
, NULL
, 'C'},
67 { "help", no_argument
, NULL
, 'h'},
71 /* signal definitions */
76 static void bgp_exit (int);
78 static struct quagga_signal_t bgp_signals
[] =
98 /* Configuration file and directory. */
99 char config_default
[] = SYSCONFDIR BGP_DEFAULT_CONFIG
;
101 /* Route retain mode flag. */
102 static int retain_mode
= 0;
104 /* Master of threads. */
105 struct thread_master
*master
;
107 /* Manually specified configuration file name. */
108 char *config_file
= NULL
;
110 /* Process ID saved for use by init system */
111 static const char *pid_file
= PATH_BGPD_PID
;
113 /* VTY port number and address. */
114 int vty_port
= BGP_VTY_PORT
;
115 char *vty_addr
= NULL
;
118 static zebra_capabilities_t _caps_p
[] =
124 struct zebra_privs_t bgpd_privs
=
126 #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
128 .group
= QUAGGA_GROUP
,
131 .vty_group
= VTY_GROUP
,
134 .cap_num_p
= sizeof(_caps_p
)/sizeof(_caps_p
[0]),
138 /* Help information display. */
140 usage (char *progname
, int status
)
143 fprintf (stderr
, "Try `%s --help' for more information.\n", progname
);
146 printf ("Usage : %s [OPTION...]\n\n\
147 Daemon which manages kernel routing table management and \
148 redistribution between different routing protocols.\n\n\
149 -d, --daemon Runs in daemon mode\n\
150 -f, --config_file Set configuration file name\n\
151 -i, --pid_file Set process identifier file name\n\
152 -p, --bgp_port Set bgp protocol's port number\n\
153 -l, --listenon Listen on specified address (implies -n)\n\
154 -A, --vty_addr Set vty's bind address\n\
155 -P, --vty_port Set vty's port number\n\
156 -r, --retain When program terminates, retain added route by bgpd.\n\
157 -n, --no_kernel Do not install route to kernel.\n\
158 -u, --user User to run as\n\
159 -g, --group Group to run as\n\
160 -v, --version Print program version\n\
161 -C, --dryrun Check configuration for validity and exit\n\
162 -h, --help Display this help and exit\n\
164 Report bugs to %s\n", progname
, ZEBRA_BUG_ADDRESS
);
170 /* SIGHUP handler. */
174 zlog (NULL
, LOG_INFO
, "SIGHUP received");
176 /* Terminate all thread. */
179 zlog_info ("bgpd restarting!");
181 /* Reload config file. */
182 vty_read_config (config_file
, config_default
);
184 /* Create VTY's socket */
185 vty_serv_sock (vty_addr
, vty_port
, BGP_VTYSH_PATH
);
187 /* Try to return to normal operation. */
190 /* SIGINT handler. */
194 zlog_notice ("Terminating on signal");
202 /* SIGUSR1 handler. */
210 Try to free up allocations we know about so that diagnostic tools such as
211 valgrind are able to better illuminate leaks.
213 Zebra route removal and protocol teardown are not meant to be done here.
214 For example, "retain_mode" may be set.
217 bgp_exit (int status
)
220 struct listnode
*node
, *nnode
;
222 struct interface
*ifp
;
223 extern struct zclient
*zclient
;
224 extern struct zclient
*zlookup
;
226 /* it only makes sense for this to be called on a clean exit */
227 assert (status
== 0);
229 /* reverse bgp_master_init */
230 for (ALL_LIST_ELEMENTS (bm
->bgp
, node
, nnode
, bgp
))
234 /* reverse bgp_master_init */
235 for (ALL_LIST_ELEMENTS_RO(bm
->listen_sockets
, node
, socket
))
237 if (close ((int)(long)socket
) == -1)
238 zlog_err ("close (%d): %s", (int)(long)socket
, safe_strerror (errno
));
240 list_delete (bm
->listen_sockets
);
242 /* reverse bgp_zebra_init/if_init */
244 if_add_hook (IF_DELETE_HOOK
, NULL
);
245 for (ALL_LIST_ELEMENTS (iflist
, node
, nnode
, ifp
))
249 /* reverse bgp_attr_init */
252 /* reverse bgp_dump_init */
255 /* reverse bgp_route_init */
258 /* reverse bgp_route_map_init/route_map_init */
261 /* reverse bgp_scan_init */
264 /* reverse access_list_init */
265 access_list_add_hook (NULL
);
266 access_list_delete_hook (NULL
);
267 access_list_reset ();
269 /* reverse bgp_filter_init */
270 as_list_add_hook (NULL
);
271 as_list_delete_hook (NULL
);
274 /* reverse prefix_list_init */
275 prefix_list_add_hook (NULL
);
276 prefix_list_delete_hook (NULL
);
277 prefix_list_reset ();
279 /* reverse community_list_init */
280 community_list_terminate (bgp_clist
);
285 zclient_free (zclient
);
287 zclient_free (zlookup
);
289 /* reverse bgp_master_init */
291 thread_master_free (master
);
294 closezlog (zlog_default
);
296 if (CONF_BGP_DEBUG (normal
, NORMAL
))
297 log_memstats_stderr ("bgpd");
302 /* Main routine of bgpd. Treatment of argument and start bgp finite
303 state machine is handled at here. */
305 main (int argc
, char **argv
)
312 struct thread thread
;
315 /* Set umask before anything for security */
318 /* Preserve name of myself. */
319 progname
= ((p
= strrchr (argv
[0], '/')) ? ++p
: argv
[0]);
321 zlog_default
= openzlog (progname
, ZLOG_BGP
,
322 LOG_CONS
|LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
324 /* BGP master init. */
327 /* Command line argument treatment. */
330 opt
= getopt_long (argc
, argv
, "df:i:hp:l:A:P:rnu:g:vC", longopts
, 0);
343 config_file
= optarg
;
349 tmp_port
= atoi (optarg
);
350 if (tmp_port
<= 0 || tmp_port
> 0xffff)
351 bm
->port
= BGP_PORT_DEFAULT
;
359 /* Deal with atoi() returning 0 on failure, and bgpd not
360 listening on bgp port... */
361 if (strcmp(optarg
, "0") == 0)
366 vty_port
= atoi (optarg
);
367 if (vty_port
<= 0 || vty_port
> 0xffff)
368 vty_port
= BGP_VTY_PORT
;
374 bm
->address
= optarg
;
375 /* listenon implies -n */
377 bgp_option_set (BGP_OPT_NO_FIB
);
380 bgpd_privs
.user
= optarg
;
383 bgpd_privs
.group
= optarg
;
386 print_version (progname
);
401 /* Make thread master. */
404 /* Initializations. */
406 signal_init (master
, Q_SIGC(bgp_signals
), bgp_signals
);
407 zprivs_init (&bgpd_privs
);
412 /* BGP related initialization. */
415 /* Sort CLI commands. */
418 /* Parse config file. */
419 vty_read_config (config_file
, config_default
);
421 /* Start execution only if not in dry-run mode */
425 /* Turn into daemon if daemon_mode is set. */
426 if (daemon_mode
&& daemon (0, 0) < 0)
428 zlog_err("BGPd daemon failed: %s", strerror(errno
));
433 /* Process ID file creation. */
434 pid_output (pid_file
);
436 /* Make bgp vty socket. */
437 vty_serv_sock (vty_addr
, vty_port
, BGP_VTYSH_PATH
);
440 zlog_notice ("BGPd %s starting: vty@%d, bgp@%s:%d", QUAGGA_VERSION
,
442 (bm
->address
? bm
->address
: "<all>"),
445 /* Start finite state machine, here we go! */
446 while (thread_fetch (master
, &thread
))
447 thread_call (&thread
);