1 /* zebra daemon main routine.
2 * Copyright (C) 1997, 98 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
24 #include <lib/version.h>
38 #include "zebra/rib.h"
39 #include "zebra/zserv.h"
40 #include "zebra/debug.h"
41 #include "zebra/router-id.h"
42 #include "zebra/irdp.h"
43 #include "zebra/rtadv.h"
44 #include "zebra/zebra_fpm.h"
45 #include "zebra/zebra_ptm.h"
47 #define ZEBRA_PTM_SUPPORT
50 struct zebra_t zebrad
=
52 .rtm_table_default
= 0,
58 /* Pacify zclient.o in libzebra, which expects this variable. */
59 struct thread_master
*master
;
61 /* Route retain mode flag. */
64 /* Allow non-quagga entities to delete quagga routes */
67 /* Don't delete kernel route. */
68 int keep_kernel_mode
= 0;
71 /* Receive buffer size for netlink socket */
72 u_int32_t nl_rcvbufsize
= 4194304;
73 #endif /* HAVE_NETLINK */
75 /* Command line options. */
76 struct option longopts
[] =
78 { "batch", no_argument
, NULL
, 'b'},
79 { "daemon", no_argument
, NULL
, 'd'},
80 { "allow_delete", no_argument
, NULL
, 'a'},
81 { "keep_kernel", no_argument
, NULL
, 'k'},
82 { "config_file", required_argument
, NULL
, 'f'},
83 { "pid_file", required_argument
, NULL
, 'i'},
84 { "socket", required_argument
, NULL
, 'z'},
85 { "help", no_argument
, NULL
, 'h'},
86 { "vty_addr", required_argument
, NULL
, 'A'},
87 { "vty_port", required_argument
, NULL
, 'P'},
88 { "retain", no_argument
, NULL
, 'r'},
89 { "dryrun", no_argument
, NULL
, 'C'},
91 { "nl-bufsize", required_argument
, NULL
, 's'},
92 #endif /* HAVE_NETLINK */
93 { "user", required_argument
, NULL
, 'u'},
94 { "group", required_argument
, NULL
, 'g'},
95 { "version", no_argument
, NULL
, 'v'},
99 zebra_capabilities_t _caps_p
[] =
106 /* zebra privileges to run with */
107 struct zebra_privs_t zserv_privs
=
109 #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
111 .group
= QUAGGA_GROUP
,
114 .vty_group
= VTY_GROUP
,
117 .cap_num_p
= array_size(_caps_p
),
121 /* Default configuration file path. */
122 char config_default
[] = SYSCONFDIR DEFAULT_CONFIG_FILE
;
124 /* Process ID saved for use by init system */
125 const char *pid_file
= PATH_ZEBRA_PID
;
127 /* Help information display. */
129 usage (char *progname
, int status
)
132 fprintf (stderr
, "Try `%s --help' for more information.\n", progname
);
135 printf ("Usage : %s [OPTION...]\n\n"\
136 "Daemon which manages kernel routing table management and "\
137 "redistribution between different routing protocols.\n\n"\
138 "-b, --batch Runs in batch mode\n"\
139 "-d, --daemon Runs in daemon mode\n"\
140 "-a, --allow_delete Allow other processes to delete Quagga Routes\n" \
141 "-f, --config_file Set configuration file name\n"\
142 "-i, --pid_file Set process identifier file name\n"\
143 "-z, --socket Set path of zebra socket\n"\
144 "-k, --keep_kernel Don't delete old routes which installed by "\
146 "-C, --dryrun Check configuration for validity and exit\n"\
147 "-A, --vty_addr Set vty's bind address\n"\
148 "-P, --vty_port Set vty's port number\n"\
149 "-r, --retain When program terminates, retain added route "\
151 "-u, --user User to run as\n"\
152 "-g, --group Group to run as\n", progname
);
154 printf ("-s, --nl-bufsize Set netlink receive buffer size\n");
155 #endif /* HAVE_NETLINK */
156 printf ("-v, --version Print program version\n"\
157 "-h, --help Display this help and exit\n"\
159 "Report bugs to %s\n", ZEBRA_BUG_ADDRESS
);
165 /* SIGHUP handler. */
169 zlog_info ("SIGHUP received");
171 /* Reload of config file. */
175 /* SIGINT handler. */
179 zlog_notice ("Terminating on signal");
188 systemd_send_stopping();
192 /* SIGUSR1 handler. */
199 struct quagga_signal_t zebra_signals
[] =
219 /* Callback upon creating a new VRF. */
221 zebra_vrf_new (vrf_id_t vrf_id
, const char *name
, void **info
)
223 struct zebra_vrf
*zvrf
= *info
;
225 zlog_info ("ZVRF %s with id %u", name
, vrf_id
);
229 zvrf
= zebra_vrf_alloc (vrf_id
, name
);
230 *info
= (void *)zvrf
;
231 router_id_init (zvrf
);
238 zebra_ns_enable (ns_id_t ns_id
, void **info
)
240 struct zebra_ns
*zns
= (struct zebra_ns
*) (*info
);
246 /* Initialize netlink sockets */
247 snprintf (nl_name
, 64, "netlink-listen (NS %u)", ns_id
);
248 zns
->netlink
.sock
= -1;
249 zns
->netlink
.name
= XSTRDUP (MTYPE_NETLINK_NAME
, nl_name
);
251 snprintf (nl_name
, 64, "netlink-cmd (NS %u)", ns_id
);
252 zns
->netlink_cmd
.sock
= -1;
253 zns
->netlink_cmd
.name
= XSTRDUP (MTYPE_NETLINK_NAME
, nl_name
);
255 zns
->if_table
= route_table_init ();
257 interface_list (zns
);
263 /* Callback upon enabling a VRF. */
265 zebra_vrf_enable (vrf_id_t vrf_id
, const char *name
, void **info
)
267 struct zebra_vrf
*zvrf
= (struct zebra_vrf
*) (*info
);
271 #if defined (HAVE_RTADV)
280 zebra_ns_disable (ns_id_t ns_id, void **info)
282 struct zebra_ns *zns = (struct zebra_ns *) (*info);
284 kernel_terminate (zns);
290 /* Callback upon disabling a VRF. */
292 zebra_vrf_disable (vrf_id_t vrf_id
, const char *name
, void **info
)
294 struct zebra_vrf
*zvrf
= (struct zebra_vrf
*) (*info
);
295 struct listnode
*list_node
;
296 struct interface
*ifp
;
300 rib_close_table (zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
301 rib_close_table (zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
303 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), list_node
, ifp
))
305 int operative
= if_is_operative (ifp
);
306 UNSET_FLAG (ifp
->flags
, IFF_UP
);
311 #if defined (HAVE_RTADV)
312 rtadv_terminate (zvrf
);
315 list_delete_all_node (zvrf
->rid_all_sorted_list
);
316 list_delete_all_node (zvrf
->rid_lo_sorted_list
);
321 /* Zebra VRF initialization. */
323 zebra_vrf_init (void)
325 struct zebra_ns
*zns
;
327 vrf_add_hook (VRF_NEW_HOOK
, zebra_vrf_new
);
328 vrf_add_hook (VRF_ENABLE_HOOK
, zebra_vrf_enable
);
329 vrf_add_hook (VRF_DISABLE_HOOK
, zebra_vrf_disable
);
331 /* Default NS initialization */
333 zns
= XCALLOC (MTYPE_ZEBRA_VRF
, sizeof (struct zebra_ns
));
334 dzns
= zns
; //Pending: Doing it all for the default namespace only for now.
337 zebra_ns_enable (0, (void **)&zns
);
340 /* Main startup routine. */
342 main (int argc
, char **argv
)
345 char *vty_addr
= NULL
;
346 int vty_port
= ZEBRA_VTY_PORT
;
350 char *config_file
= NULL
;
352 struct thread thread
;
353 char *zserv_path
= NULL
;
355 /* Set umask before anything for security */
358 /* preserve my name */
359 progname
= ((p
= strrchr (argv
[0], '/')) ? ++p
: argv
[0]);
361 zlog_default
= openzlog (progname
, ZLOG_ZEBRA
, 0,
362 LOG_CONS
|LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
369 opt
= getopt_long (argc
, argv
, "bdakf:i:z:hA:P:ru:g:vs:C", longopts
, 0);
371 opt
= getopt_long (argc
, argv
, "bdakf:i:z:hA:P:ru:g:vC", longopts
, 0);
372 #endif /* HAVE_NETLINK */
390 keep_kernel_mode
= 1;
396 config_file
= optarg
;
408 /* Deal with atoi() returning 0 on failure, and zebra not
409 listening on zebra port... */
410 if (strcmp(optarg
, "0") == 0)
415 vty_port
= atoi (optarg
);
416 if (vty_port
<= 0 || vty_port
> 0xffff)
417 vty_port
= ZEBRA_VTY_PORT
;
424 nl_rcvbufsize
= atoi (optarg
);
426 #endif /* HAVE_NETLINK */
428 zserv_privs
.user
= optarg
;
431 zserv_privs
.group
= optarg
;
434 print_version (progname
);
446 /* Make master thread emulator. */
447 zebrad
.master
= thread_master_create ();
449 /* privs initialise */
450 zprivs_init (&zserv_privs
);
452 /* Vty related initialize. */
453 signal_init (zebrad
.master
, array_size(zebra_signals
), zebra_signals
);
455 vty_init (zebrad
.master
);
458 /* Zebra related initialize. */
463 router_id_cmd_init ();
467 #if defined (HAVE_RTADV)
474 #ifdef ZEBRA_PTM_SUPPORT
478 /* For debug purpose. */
479 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
481 /* Initialize VRF module, and make kernel routing socket. */
486 #endif /* HAVE_SNMP */
489 zfpm_init (zebrad
.master
, 1, 0);
491 zfpm_init (zebrad
.master
, 0, 0);
494 /* Process the configuration file. Among other configuration
495 * directives we can meet those installing static routes. Such
496 * requests will not be executed immediately, but queued in
497 * zebra->ribq structure until we enter the main execution loop.
498 * The notifications from kernel will show originating PID equal
499 * to that after daemon() completes (if ever called).
501 vty_read_config (config_file
, config_default
);
503 /* Don't start execution if we are in dry-run mode */
508 /* rib_weed_tables (); */
510 /* Exit when zebra is working in batch mode. */
515 if (daemon_mode
&& daemon (0, 0) < 0)
517 zlog_err("Zebra daemon failed: %s", strerror(errno
));
518 systemd_send_stopping ();
522 /* Output pid of zebra. */
523 pid_output (pid_file
);
525 systemd_send_started (zebrad
.master
);
526 /* After we have successfully acquired the pidfile, we can be sure
527 * about being the only copy of zebra process, which is submitting
528 * changes to the FIB.
529 * Clean up zebra-originated routes. The requests will be sent to OS
530 * immediately, so originating PID in notifications from kernel
531 * will be equal to the current getpid(). To know about such routes,
532 * we have to have route_read() called before.
534 if (! keep_kernel_mode
)
537 /* Needed for BSD routing socket. */
540 /* This must be done only after locking pidfile (bug #403). */
541 zebra_zserv_socket_init (zserv_path
);
543 /* Make vty server socket. */
544 vty_serv_sock (vty_addr
, vty_port
, ZEBRA_VTYSH_PATH
);
547 zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION
, vty_port
);
549 while (thread_fetch (zebrad
.master
, &thread
))
550 thread_call (&thread
);