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