]> git.proxmox.com Git - mirror_frr.git/blob - zebra/main.c
Merge remote-tracking branch 'origin/cmaster' into cmaster-next
[mirror_frr.git] / zebra / main.c
1 /* zebra daemon main routine.
2 * Copyright (C) 1997, 98 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
22 #include <zebra.h>
23
24 #include <lib/version.h>
25 #include "getopt.h"
26 #include "command.h"
27 #include "thread.h"
28 #include "filter.h"
29 #include "memory.h"
30 #include "zebra_memory.h"
31 #include "memory_vty.h"
32 #include "prefix.h"
33 #include "log.h"
34 #include "plist.h"
35 #include "privs.h"
36 #include "sigevent.h"
37 #include "vrf.h"
38
39 #include "zebra/rib.h"
40 #include "zebra/zserv.h"
41 #include "zebra/debug.h"
42 #include "zebra/router-id.h"
43 #include "zebra/irdp.h"
44 #include "zebra/rtadv.h"
45 #include "zebra/zebra_fpm.h"
46 #include "zebra/zebra_ptm.h"
47 #include "zebra/zebra_ns.h"
48 #include "zebra/redistribute.h"
49 #include "zebra/zebra_mpls.h"
50
51 #define ZEBRA_PTM_SUPPORT
52
53 /* Zebra instance */
54 struct zebra_t zebrad =
55 {
56 .rtm_table_default = 0,
57 };
58
59 /* process id. */
60 pid_t pid;
61
62 /* Pacify zclient.o in libzebra, which expects this variable. */
63 struct thread_master *master;
64
65 /* Route retain mode flag. */
66 int retain_mode = 0;
67
68 /* Allow non-quagga entities to delete quagga routes */
69 int allow_delete = 0;
70
71 /* Don't delete kernel route. */
72 int keep_kernel_mode = 0;
73
74 #ifdef HAVE_NETLINK
75 /* Receive buffer size for netlink socket */
76 u_int32_t nl_rcvbufsize = 4194304;
77 #endif /* HAVE_NETLINK */
78
79 /* Command line options. */
80 struct option longopts[] =
81 {
82 { "batch", no_argument, NULL, 'b'},
83 { "daemon", no_argument, NULL, 'd'},
84 { "allow_delete", no_argument, NULL, 'a'},
85 { "keep_kernel", no_argument, NULL, 'k'},
86 { "fpm_format", required_argument, NULL, 'F'},
87 { "config_file", required_argument, NULL, 'f'},
88 { "pid_file", required_argument, NULL, 'i'},
89 { "socket", required_argument, NULL, 'z'},
90 { "help", no_argument, NULL, 'h'},
91 { "vty_addr", required_argument, NULL, 'A'},
92 { "vty_port", required_argument, NULL, 'P'},
93 { "retain", no_argument, NULL, 'r'},
94 { "dryrun", no_argument, NULL, 'C'},
95 #ifdef HAVE_NETLINK
96 { "nl-bufsize", required_argument, NULL, 's'},
97 #endif /* HAVE_NETLINK */
98 { "user", required_argument, NULL, 'u'},
99 { "group", required_argument, NULL, 'g'},
100 { "version", no_argument, NULL, 'v'},
101 { 0 }
102 };
103
104 zebra_capabilities_t _caps_p [] =
105 {
106 ZCAP_NET_ADMIN,
107 ZCAP_SYS_ADMIN,
108 ZCAP_NET_RAW,
109 };
110
111 /* zebra privileges to run with */
112 struct zebra_privs_t zserv_privs =
113 {
114 #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
115 .user = QUAGGA_USER,
116 .group = QUAGGA_GROUP,
117 #endif
118 #ifdef VTY_GROUP
119 .vty_group = VTY_GROUP,
120 #endif
121 .caps_p = _caps_p,
122 .cap_num_p = array_size(_caps_p),
123 .cap_num_i = 0
124 };
125
126 /* Default configuration file path. */
127 char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
128
129 /* Process ID saved for use by init system */
130 const char *pid_file = PATH_ZEBRA_PID;
131
132 /* Help information display. */
133 static void
134 usage (char *progname, int status)
135 {
136 if (status != 0)
137 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
138 else
139 {
140 printf ("Usage : %s [OPTION...]\n\n"\
141 "Daemon which manages kernel routing table management and "\
142 "redistribution between different routing protocols.\n\n"\
143 "-b, --batch Runs in batch mode\n"\
144 "-d, --daemon Runs in daemon mode\n"\
145 "-a, --allow_delete Allow other processes to delete Quagga Routes\n" \
146 "-f, --config_file Set configuration file name\n"\
147 "-F, --fpm_format Set fpm format to 'netlink' or 'protobuf'\n"\
148 "-i, --pid_file Set process identifier file name\n"\
149 "-z, --socket Set path of zebra socket\n"\
150 "-k, --keep_kernel Don't delete old routes which installed by "\
151 "zebra.\n"\
152 "-C, --dryrun Check configuration for validity and exit\n"\
153 "-A, --vty_addr Set vty's bind address\n"\
154 "-P, --vty_port Set vty's port number\n"\
155 "-r, --retain When program terminates, retain added route "\
156 "by zebra.\n"\
157 "-u, --user User to run as\n"\
158 "-g, --group Group to run as\n", progname);
159 #ifdef HAVE_NETLINK
160 printf ("-s, --nl-bufsize Set netlink receive buffer size\n");
161 #endif /* HAVE_NETLINK */
162 printf ("-v, --version Print program version\n"\
163 "-h, --help Display this help and exit\n"\
164 "\n"\
165 "Report bugs to %s\n", ZEBRA_BUG_ADDRESS);
166 }
167
168 exit (status);
169 }
170
171 /* SIGHUP handler. */
172 static void
173 sighup (void)
174 {
175 zlog_info ("SIGHUP received");
176
177 /* Reload of config file. */
178 ;
179 }
180
181 /* SIGINT handler. */
182 static void
183 sigint (void)
184 {
185 struct zebra_ns *zns;
186
187 zlog_notice ("Terminating on signal");
188
189 if (!retain_mode)
190 rib_close ();
191 #ifdef HAVE_IRDP
192 irdp_finish();
193 #endif
194
195 zebra_ptm_finish();
196
197 zns = zebra_ns_lookup (NS_DEFAULT);
198 zebra_ns_disable (0, (void **)&zns);
199 exit (0);
200 }
201
202 /* SIGUSR1 handler. */
203 static void
204 sigusr1 (void)
205 {
206 zlog_rotate (NULL);
207 }
208
209 struct quagga_signal_t zebra_signals[] =
210 {
211 {
212 .signal = SIGHUP,
213 .handler = &sighup,
214 },
215 {
216 .signal = SIGUSR1,
217 .handler = &sigusr1,
218 },
219 {
220 .signal = SIGINT,
221 .handler = &sigint,
222 },
223 {
224 .signal = SIGTERM,
225 .handler = &sigint,
226 },
227 };
228
229 /* Main startup routine. */
230 int
231 main (int argc, char **argv)
232 {
233 char *p;
234 char *vty_addr = NULL;
235 int vty_port = ZEBRA_VTY_PORT;
236 int dryrun = 0;
237 int batch_mode = 0;
238 int daemon_mode = 0;
239 char *config_file = NULL;
240 char *progname;
241 struct thread thread;
242 char *zserv_path = NULL;
243 char *fpm_format = NULL;
244
245 /* Set umask before anything for security */
246 umask (0027);
247
248 /* preserve my name */
249 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
250
251 zlog_default = openzlog (progname, ZLOG_ZEBRA, 0,
252 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
253 zprivs_init (&zserv_privs);
254 #if defined(HAVE_CUMULUS)
255 zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
256 #endif
257
258 while (1)
259 {
260 int opt;
261
262 #ifdef HAVE_NETLINK
263 opt = getopt_long (argc, argv, "bdakf:F:i:z:hA:P:ru:g:vs:C", longopts, 0);
264 #else
265 opt = getopt_long (argc, argv, "bdakf:F:i:z:hA:P:ru:g:vC", longopts, 0);
266 #endif /* HAVE_NETLINK */
267
268 if (opt == EOF)
269 break;
270
271 switch (opt)
272 {
273 case 0:
274 break;
275 case 'b':
276 batch_mode = 1;
277 case 'd':
278 daemon_mode = 1;
279 break;
280 case 'a':
281 allow_delete = 1;
282 break;
283 case 'k':
284 keep_kernel_mode = 1;
285 break;
286 case 'C':
287 dryrun = 1;
288 break;
289 case 'f':
290 config_file = optarg;
291 break;
292 case 'F':
293 fpm_format = optarg;
294 break;
295 case 'A':
296 vty_addr = optarg;
297 break;
298 case 'i':
299 pid_file = optarg;
300 break;
301 case 'z':
302 zserv_path = optarg;
303 break;
304 case 'P':
305 /* Deal with atoi() returning 0 on failure, and zebra not
306 listening on zebra port... */
307 if (strcmp(optarg, "0") == 0)
308 {
309 vty_port = 0;
310 break;
311 }
312 vty_port = atoi (optarg);
313 if (vty_port <= 0 || vty_port > 0xffff)
314 vty_port = ZEBRA_VTY_PORT;
315 break;
316 case 'r':
317 retain_mode = 1;
318 break;
319 #ifdef HAVE_NETLINK
320 case 's':
321 nl_rcvbufsize = atoi (optarg);
322 break;
323 #endif /* HAVE_NETLINK */
324 case 'u':
325 zserv_privs.user = optarg;
326 break;
327 case 'g':
328 zserv_privs.group = optarg;
329 break;
330 case 'v':
331 print_version (progname);
332 exit (0);
333 break;
334 case 'h':
335 usage (progname, 0);
336 break;
337 default:
338 usage (progname, 1);
339 break;
340 }
341 }
342
343 /* Make master thread emulator. */
344 zebrad.master = thread_master_create ();
345
346 /* Vty related initialize. */
347 signal_init (zebrad.master, array_size(zebra_signals), zebra_signals);
348 cmd_init (1);
349 vty_init (zebrad.master);
350 memory_init ();
351
352 /* Zebra related initialize. */
353 zebra_init ();
354 rib_init ();
355 zebra_if_init ();
356 zebra_debug_init ();
357 router_id_cmd_init ();
358 zebra_vty_init ();
359 access_list_init ();
360 prefix_list_init ();
361 #if defined (HAVE_RTADV)
362 rtadv_cmd_init ();
363 #endif
364 #ifdef HAVE_IRDP
365 irdp_init();
366 #endif
367 /* PTM socket */
368 #ifdef ZEBRA_PTM_SUPPORT
369 zebra_ptm_init();
370 #endif
371
372 zebra_mpls_init ();
373 zebra_mpls_vty_init ();
374
375 /* For debug purpose. */
376 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
377
378 /* Initialize NS( and implicitly the VRF module), and make kernel routing socket. */
379 zebra_ns_init ();
380
381 #ifdef HAVE_SNMP
382 zebra_snmp_init ();
383 #endif /* HAVE_SNMP */
384
385 #ifdef HAVE_FPM
386 zfpm_init (zebrad.master, 1, 0, fpm_format);
387 #else
388 zfpm_init (zebrad.master, 0, 0, fpm_format);
389 #endif
390
391 /* Process the configuration file. Among other configuration
392 * directives we can meet those installing static routes. Such
393 * requests will not be executed immediately, but queued in
394 * zebra->ribq structure until we enter the main execution loop.
395 * The notifications from kernel will show originating PID equal
396 * to that after daemon() completes (if ever called).
397 */
398 vty_read_config (config_file, config_default);
399
400 /* Don't start execution if we are in dry-run mode */
401 if (dryrun)
402 return(0);
403
404 /* Clean up rib. */
405 /* rib_weed_tables (); */
406
407 /* Exit when zebra is working in batch mode. */
408 if (batch_mode)
409 exit (0);
410
411 /* Daemonize. */
412 if (daemon_mode && daemon (0, 0) < 0)
413 {
414 zlog_err("Zebra daemon failed: %s", strerror(errno));
415 exit (1);
416 }
417
418 /* Output pid of zebra. */
419 pid_output (pid_file);
420
421 /* After we have successfully acquired the pidfile, we can be sure
422 * about being the only copy of zebra process, which is submitting
423 * changes to the FIB.
424 * Clean up zebra-originated routes. The requests will be sent to OS
425 * immediately, so originating PID in notifications from kernel
426 * will be equal to the current getpid(). To know about such routes,
427 * we have to have route_read() called before.
428 */
429 if (! keep_kernel_mode)
430 rib_sweep_route ();
431
432 /* Needed for BSD routing socket. */
433 pid = getpid ();
434
435 /* This must be done only after locking pidfile (bug #403). */
436 zebra_zserv_socket_init (zserv_path);
437
438 /* Make vty server socket. */
439 vty_serv_sock (vty_addr, vty_port, ZEBRA_VTYSH_PATH);
440
441 /* Print banner. */
442 zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port);
443
444 while (thread_fetch (zebrad.master, &thread))
445 thread_call (&thread);
446
447 /* Not reached... */
448 return 0;
449 }