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