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