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