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