blob: 620ca128a873fa133a47bb05b8077c37c1b4291d [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* Main routine of bgpd.
2 Copyright (C) 1996, 97, 98, 1999 Kunihiro Ishiguro
3
4This file is part of GNU Zebra.
5
6GNU Zebra is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11GNU Zebra is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Zebra; see the file COPYING. If not, write to the Free
18Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1902111-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"
gdt5e4fa162004-03-16 14:38:36 +000028#include <lib/version.h>
paul718e3742002-12-13 20:15:29 +000029#include "memory.h"
30#include "prefix.h"
31#include "log.h"
pauledd7c242003-06-04 13:59:38 +000032#include "privs.h"
paul2d75d052004-01-19 21:31:15 +000033#include "sigevent.h"
paul718e3742002-12-13 20:15:29 +000034
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. */
Stephen Hemminger372b3c72009-05-15 10:29:41 -070040static const struct option longopts[] =
paul718e3742002-12-13 20:15:29 +000041{
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 Jakma3a02d1f2007-11-01 14:29:11 +000046 { "listenon", required_argument, NULL, 'l'},
paul718e3742002-12-13 20:15:29 +000047 { "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'},
pauledd7c242003-06-04 13:59:38 +000051 { "user", required_argument, NULL, 'u'},
hassoc0652302004-11-25 19:33:48 +000052 { "group", required_argument, NULL, 'g'},
paul718e3742002-12-13 20:15:29 +000053 { "version", no_argument, NULL, 'v'},
Paul Jakma876b8be2006-10-15 23:35:57 +000054 { "dryrun", no_argument, NULL, 'C'},
paul718e3742002-12-13 20:15:29 +000055 { "help", no_argument, NULL, 'h'},
56 { 0 }
57};
58
paul2d75d052004-01-19 21:31:15 +000059/* signal definitions */
60void sighup (void);
61void sigint (void);
62void sigusr1 (void);
63
Stephen Hemminger372b3c72009-05-15 10:29:41 -070064static struct quagga_signal_t bgp_signals[] =
paul2d75d052004-01-19 21:31:15 +000065{
66 {
67 .signal = SIGHUP,
68 .handler = &sighup,
69 },
70 {
71 .signal = SIGUSR1,
72 .handler = &sigusr1,
73 },
74 {
75 .signal = SIGINT,
76 .handler = &sigint,
77 },
hassof571dab2004-03-22 08:55:25 +000078 {
79 .signal = SIGTERM,
80 .handler = &sigint,
81 },
paul2d75d052004-01-19 21:31:15 +000082};
83
paul718e3742002-12-13 20:15:29 +000084/* Configuration file and directory. */
paul718e3742002-12-13 20:15:29 +000085char config_default[] = SYSCONFDIR BGP_DEFAULT_CONFIG;
86
87/* Route retain mode flag. */
Stephen Hemminger372b3c72009-05-15 10:29:41 -070088static int retain_mode = 0;
paul718e3742002-12-13 20:15:29 +000089
90/* Master of threads. */
91struct thread_master *master;
92
93/* Manually specified configuration file name. */
94char *config_file = NULL;
95
96/* Process ID saved for use by init system */
Stephen Hemminger372b3c72009-05-15 10:29:41 -070097static const char *pid_file = PATH_BGPD_PID;
paul718e3742002-12-13 20:15:29 +000098
99/* VTY port number and address. */
100int vty_port = BGP_VTY_PORT;
101char *vty_addr = NULL;
102
pauledd7c242003-06-04 13:59:38 +0000103/* privileges */
Stephen Hemminger372b3c72009-05-15 10:29:41 -0700104static zebra_capabilities_t _caps_p [] =
pauledd7c242003-06-04 13:59:38 +0000105{
paul98f51632004-10-25 14:19:15 +0000106 ZCAP_BIND,
paulceacedb2005-09-29 14:39:32 +0000107 ZCAP_NET_RAW,
pauledd7c242003-06-04 13:59:38 +0000108};
109
110struct zebra_privs_t bgpd_privs =
111{
pauld81fadf2003-08-14 05:32:12 +0000112#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,
pauledd7c242003-06-04 13:59:38 +0000118#endif
119 .caps_p = _caps_p,
120 .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]),
121 .cap_num_i = 0,
122};
123
paul718e3742002-12-13 20:15:29 +0000124/* Help information display. */
125static void
126usage (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\
133Daemon which manages kernel routing table management and \
134redistribution 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 Jakma3a02d1f2007-11-01 14:29:11 +0000139-l, --listenon Listen on specified address (implies -n)\n\
paul718e3742002-12-13 20:15:29 +0000140-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\
hassoc0652302004-11-25 19:33:48 +0000144-u, --user User to run as\n\
145-g, --group Group to run as\n\
paul718e3742002-12-13 20:15:29 +0000146-v, --version Print program version\n\
Paul Jakma876b8be2006-10-15 23:35:57 +0000147-C, --dryrun Check configuration for validity and exit\n\
paul718e3742002-12-13 20:15:29 +0000148-h, --help Display this help and exit\n\
149\n\
150Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
151 }
152
153 exit (status);
154}
155
156/* SIGHUP handler. */
157void
paul2d75d052004-01-19 21:31:15 +0000158sighup (void)
paul718e3742002-12-13 20:15:29 +0000159{
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. */
hasso320ec102004-06-20 19:54:37 +0000168 vty_read_config (config_file, config_default);
paul718e3742002-12-13 20:15:29 +0000169
170 /* Create VTY's socket */
paul4fc4e7a2003-01-22 19:47:09 +0000171 vty_serv_sock (vty_addr, vty_port, BGP_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000172
173 /* Try to return to normal operation. */
174}
175
176/* SIGINT handler. */
177void
paul2d75d052004-01-19 21:31:15 +0000178sigint (void)
paul718e3742002-12-13 20:15:29 +0000179{
ajs887c44a2004-12-03 16:36:46 +0000180 zlog_notice ("Terminating on signal");
paul718e3742002-12-13 20:15:29 +0000181
182 if (! retain_mode)
183 bgp_terminate ();
184
185 exit (0);
186}
187
188/* SIGUSR1 handler. */
189void
paul2d75d052004-01-19 21:31:15 +0000190sigusr1 (void)
paul718e3742002-12-13 20:15:29 +0000191{
192 zlog_rotate (NULL);
193}
paul718e3742002-12-13 20:15:29 +0000194
195/* Main routine of bgpd. Treatment of argument and start bgp finite
196 state machine is handled at here. */
197int
198main (int argc, char **argv)
199{
200 char *p;
201 int opt;
202 int daemon_mode = 0;
Paul Jakma876b8be2006-10-15 23:35:57 +0000203 int dryrun = 0;
paul718e3742002-12-13 20:15:29 +0000204 char *progname;
205 struct thread thread;
Paul Jakma0d6b2ee2008-05-29 18:29:16 +0000206 int tmp_port;
paul718e3742002-12-13 20:15:29 +0000207
208 /* Set umask before anything for security */
209 umask (0027);
210
211 /* Preserve name of myself. */
212 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
213
ajs274a4a42004-12-07 15:39:31 +0000214 zlog_default = openzlog (progname, ZLOG_BGP,
paul718e3742002-12-13 20:15:29 +0000215 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
216
217 /* BGP master init. */
218 bgp_master_init ();
219
220 /* Command line argument treatment. */
221 while (1)
222 {
Paul Jakma3a02d1f2007-11-01 14:29:11 +0000223 opt = getopt_long (argc, argv, "df:i:hp:l:A:P:rnu:g:vC", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000224
225 if (opt == EOF)
226 break;
227
228 switch (opt)
229 {
230 case 0:
231 break;
232 case 'd':
233 daemon_mode = 1;
234 break;
235 case 'f':
236 config_file = optarg;
237 break;
238 case 'i':
239 pid_file = optarg;
240 break;
241 case 'p':
Paul Jakma0d6b2ee2008-05-29 18:29:16 +0000242 tmp_port = atoi (optarg);
243 if (tmp_port <= 0 || tmp_port > 0xffff)
244 bm->port = BGP_PORT_DEFAULT;
245 else
246 bm->port = tmp_port;
paul718e3742002-12-13 20:15:29 +0000247 break;
248 case 'A':
249 vty_addr = optarg;
250 break;
251 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000252 /* Deal with atoi() returning 0 on failure, and bgpd not
253 listening on bgp port... */
254 if (strcmp(optarg, "0") == 0)
255 {
256 vty_port = 0;
257 break;
258 }
259 vty_port = atoi (optarg);
Paul Jakma0d6b2ee2008-05-29 18:29:16 +0000260 if (vty_port <= 0 || vty_port > 0xffff)
261 vty_port = BGP_VTY_PORT;
paul718e3742002-12-13 20:15:29 +0000262 break;
263 case 'r':
264 retain_mode = 1;
265 break;
Paul Jakma3a02d1f2007-11-01 14:29:11 +0000266 case 'l':
267 bm->address = optarg;
268 /* listenon implies -n */
paul718e3742002-12-13 20:15:29 +0000269 case 'n':
270 bgp_option_set (BGP_OPT_NO_FIB);
271 break;
hassoc0652302004-11-25 19:33:48 +0000272 case 'u':
273 bgpd_privs.user = optarg;
274 break;
275 case 'g':
276 bgpd_privs.group = optarg;
277 break;
paul718e3742002-12-13 20:15:29 +0000278 case 'v':
279 print_version (progname);
280 exit (0);
281 break;
Paul Jakma876b8be2006-10-15 23:35:57 +0000282 case 'C':
283 dryrun = 1;
284 break;
paul718e3742002-12-13 20:15:29 +0000285 case 'h':
286 usage (progname, 0);
287 break;
288 default:
289 usage (progname, 1);
290 break;
291 }
292 }
293
294 /* Make thread master. */
295 master = bm->master;
296
297 /* Initializations. */
298 srand (time (NULL));
paul2d75d052004-01-19 21:31:15 +0000299 signal_init (master, Q_SIGC(bgp_signals), bgp_signals);
pauledd7c242003-06-04 13:59:38 +0000300 zprivs_init (&bgpd_privs);
paul718e3742002-12-13 20:15:29 +0000301 cmd_init (1);
paulb21b19c2003-06-15 01:28:29 +0000302 vty_init (master);
paul718e3742002-12-13 20:15:29 +0000303 memory_init ();
304
305 /* BGP related initialization. */
306 bgp_init ();
307
308 /* Sort CLI commands. */
309 sort_node ();
310
311 /* Parse config file. */
hasso320ec102004-06-20 19:54:37 +0000312 vty_read_config (config_file, config_default);
paul718e3742002-12-13 20:15:29 +0000313
Paul Jakma876b8be2006-10-15 23:35:57 +0000314 /* Start execution only if not in dry-run mode */
315 if(dryrun)
316 return(0);
317
paul718e3742002-12-13 20:15:29 +0000318 /* Turn into daemon if daemon_mode is set. */
Stephen Hemminger372b3c72009-05-15 10:29:41 -0700319 if (daemon_mode && daemon (0, 0) < 0)
320 {
321 zlog_err("BGPd daemon failed: %s", strerror(errno));
322 return (1);
323 }
324
paul718e3742002-12-13 20:15:29 +0000325
326 /* Process ID file creation. */
327 pid_output (pid_file);
328
329 /* Make bgp vty socket. */
330 vty_serv_sock (vty_addr, vty_port, BGP_VTYSH_PATH);
331
332 /* Print banner. */
Paul Jakma3a02d1f2007-11-01 14:29:11 +0000333 zlog_notice ("BGPd %s starting: vty@%d, bgp@%s:%d", QUAGGA_VERSION,
Paul Jakma7e992e92007-11-13 09:32:23 +0000334 vty_port,
Paul Jakmab63e6082007-11-21 21:25:34 +0000335 (bm->address ? bm->address : "<all>"),
Paul Jakma7e992e92007-11-13 09:32:23 +0000336 bm->port);
paul718e3742002-12-13 20:15:29 +0000337
338 /* Start finite state machine, here we go! */
339 while (thread_fetch (master, &thread))
340 thread_call (&thread);
341
342 /* Not reached. */
paule8e19462006-01-19 20:16:55 +0000343 return (0);
paul718e3742002-12-13 20:15:29 +0000344}