]> 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 #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
47 #define ZEBRA_PTM_SUPPORT
48
49 /* Zebra instance */
50 struct zebra_t zebrad =
51 {
52 .rtm_table_default = 0,
53 };
54
55 /* process id. */
56 pid_t pid;
57
58 /* Pacify zclient.o in libzebra, which expects this variable. */
59 struct thread_master *master;
60
61 /* Route retain mode flag. */
62 int retain_mode = 0;
63
64 /* Allow non-quagga entities to delete quagga routes */
65 int allow_delete = 0;
66
67 /* Don't delete kernel route. */
68 int keep_kernel_mode = 0;
69
70 #ifdef HAVE_NETLINK
71 /* Receive buffer size for netlink socket */
72 u_int32_t nl_rcvbufsize = 4194304;
73 #endif /* HAVE_NETLINK */
74
75 /* Command line options. */
76 struct option longopts[] =
77 {
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'},
90 #ifdef HAVE_NETLINK
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'},
96 { 0 }
97 };
98
99 zebra_capabilities_t _caps_p [] =
100 {
101 ZCAP_NET_ADMIN,
102 ZCAP_SYS_ADMIN,
103 ZCAP_NET_RAW,
104 };
105
106 /* zebra privileges to run with */
107 struct zebra_privs_t zserv_privs =
108 {
109 #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
110 .user = QUAGGA_USER,
111 .group = QUAGGA_GROUP,
112 #endif
113 #ifdef VTY_GROUP
114 .vty_group = VTY_GROUP,
115 #endif
116 .caps_p = _caps_p,
117 .cap_num_p = array_size(_caps_p),
118 .cap_num_i = 0
119 };
120
121 /* Default configuration file path. */
122 char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
123
124 /* Process ID saved for use by init system */
125 const char *pid_file = PATH_ZEBRA_PID;
126
127 /* Help information display. */
128 static void
129 usage (char *progname, int status)
130 {
131 if (status != 0)
132 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
133 else
134 {
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 "\
145 "zebra.\n"\
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 "\
150 "by zebra.\n"\
151 "-u, --user User to run as\n"\
152 "-g, --group Group to run as\n", progname);
153 #ifdef HAVE_NETLINK
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"\
158 "\n"\
159 "Report bugs to %s\n", ZEBRA_BUG_ADDRESS);
160 }
161
162 exit (status);
163 }
164
165 /* SIGHUP handler. */
166 static void
167 sighup (void)
168 {
169 zlog_info ("SIGHUP received");
170
171 /* Reload of config file. */
172 ;
173 }
174
175 /* SIGINT handler. */
176 static void
177 sigint (void)
178 {
179 zlog_notice ("Terminating on signal");
180
181 if (!retain_mode)
182 rib_close ();
183 #ifdef HAVE_IRDP
184 irdp_finish();
185 #endif
186
187 zebra_ptm_finish();
188 systemd_send_stopping();
189 exit (0);
190 }
191
192 /* SIGUSR1 handler. */
193 static void
194 sigusr1 (void)
195 {
196 zlog_rotate (NULL);
197 }
198
199 struct quagga_signal_t zebra_signals[] =
200 {
201 {
202 .signal = SIGHUP,
203 .handler = &sighup,
204 },
205 {
206 .signal = SIGUSR1,
207 .handler = &sigusr1,
208 },
209 {
210 .signal = SIGINT,
211 .handler = &sigint,
212 },
213 {
214 .signal = SIGTERM,
215 .handler = &sigint,
216 },
217 };
218
219 /* Callback upon creating a new VRF. */
220 static int
221 zebra_vrf_new (vrf_id_t vrf_id, const char *name, void **info)
222 {
223 struct zebra_vrf *zvrf = *info;
224
225 zlog_info ("ZVRF %s with id %u", name, vrf_id);
226
227 if (! zvrf)
228 {
229 zvrf = zebra_vrf_alloc (vrf_id, name);
230 *info = (void *)zvrf;
231 router_id_init (zvrf);
232 }
233
234 return 0;
235 }
236
237 static int
238 zebra_ns_enable (ns_id_t ns_id, void **info)
239 {
240 struct zebra_ns *zns = (struct zebra_ns *) (*info);
241 #ifdef HAVE_NETLINK
242 char nl_name[64];
243 #endif
244
245 #ifdef HAVE_NETLINK
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);
250
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);
254 #endif
255 zns->if_table = route_table_init ();
256 kernel_init (zns);
257 interface_list (zns);
258 route_read (zns);
259
260 return 0;
261 }
262
263 /* Callback upon enabling a VRF. */
264 static int
265 zebra_vrf_enable (vrf_id_t vrf_id, const char *name, void **info)
266 {
267 struct zebra_vrf *zvrf = (struct zebra_vrf *) (*info);
268
269 assert (zvrf);
270
271 #if defined (HAVE_RTADV)
272 rtadv_init (zvrf);
273 #endif
274
275 return 0;
276 }
277
278 /*
279 static int
280 zebra_ns_disable (ns_id_t ns_id, void **info)
281 {
282 struct zebra_ns *zns = (struct zebra_ns *) (*info);
283
284 kernel_terminate (zns);
285
286 return 0;
287 }
288 */
289
290 /* Callback upon disabling a VRF. */
291 static int
292 zebra_vrf_disable (vrf_id_t vrf_id, const char *name, void **info)
293 {
294 struct zebra_vrf *zvrf = (struct zebra_vrf *) (*info);
295 struct listnode *list_node;
296 struct interface *ifp;
297
298 assert (zvrf);
299
300 rib_close_table (zvrf->table[AFI_IP][SAFI_UNICAST]);
301 rib_close_table (zvrf->table[AFI_IP6][SAFI_UNICAST]);
302
303 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), list_node, ifp))
304 {
305 int operative = if_is_operative (ifp);
306 UNSET_FLAG (ifp->flags, IFF_UP);
307 if (operative)
308 if_down (ifp);
309 }
310
311 #if defined (HAVE_RTADV)
312 rtadv_terminate (zvrf);
313 #endif
314
315 list_delete_all_node (zvrf->rid_all_sorted_list);
316 list_delete_all_node (zvrf->rid_lo_sorted_list);
317
318 return 0;
319 }
320
321 /* Zebra VRF initialization. */
322 static void
323 zebra_vrf_init (void)
324 {
325 struct zebra_ns *zns;
326
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);
330
331 /* Default NS initialization */
332
333 zns = XCALLOC (MTYPE_ZEBRA_VRF, sizeof (struct zebra_ns));
334 dzns = zns; //Pending: Doing it all for the default namespace only for now.
335
336 vrf_init ();
337 zebra_ns_enable (0, (void **)&zns);
338 }
339
340 /* Main startup routine. */
341 int
342 main (int argc, char **argv)
343 {
344 char *p;
345 char *vty_addr = NULL;
346 int vty_port = ZEBRA_VTY_PORT;
347 int dryrun = 0;
348 int batch_mode = 0;
349 int daemon_mode = 0;
350 char *config_file = NULL;
351 char *progname;
352 struct thread thread;
353 char *zserv_path = NULL;
354
355 /* Set umask before anything for security */
356 umask (0027);
357
358 /* preserve my name */
359 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
360
361 zlog_default = openzlog (progname, ZLOG_ZEBRA, 0,
362 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
363
364 while (1)
365 {
366 int opt;
367
368 #ifdef HAVE_NETLINK
369 opt = getopt_long (argc, argv, "bdakf:i:z:hA:P:ru:g:vs:C", longopts, 0);
370 #else
371 opt = getopt_long (argc, argv, "bdakf:i:z:hA:P:ru:g:vC", longopts, 0);
372 #endif /* HAVE_NETLINK */
373
374 if (opt == EOF)
375 break;
376
377 switch (opt)
378 {
379 case 0:
380 break;
381 case 'b':
382 batch_mode = 1;
383 case 'd':
384 daemon_mode = 1;
385 break;
386 case 'a':
387 allow_delete = 1;
388 break;
389 case 'k':
390 keep_kernel_mode = 1;
391 break;
392 case 'C':
393 dryrun = 1;
394 break;
395 case 'f':
396 config_file = optarg;
397 break;
398 case 'A':
399 vty_addr = optarg;
400 break;
401 case 'i':
402 pid_file = optarg;
403 break;
404 case 'z':
405 zserv_path = optarg;
406 break;
407 case 'P':
408 /* Deal with atoi() returning 0 on failure, and zebra not
409 listening on zebra port... */
410 if (strcmp(optarg, "0") == 0)
411 {
412 vty_port = 0;
413 break;
414 }
415 vty_port = atoi (optarg);
416 if (vty_port <= 0 || vty_port > 0xffff)
417 vty_port = ZEBRA_VTY_PORT;
418 break;
419 case 'r':
420 retain_mode = 1;
421 break;
422 #ifdef HAVE_NETLINK
423 case 's':
424 nl_rcvbufsize = atoi (optarg);
425 break;
426 #endif /* HAVE_NETLINK */
427 case 'u':
428 zserv_privs.user = optarg;
429 break;
430 case 'g':
431 zserv_privs.group = optarg;
432 break;
433 case 'v':
434 print_version (progname);
435 exit (0);
436 break;
437 case 'h':
438 usage (progname, 0);
439 break;
440 default:
441 usage (progname, 1);
442 break;
443 }
444 }
445
446 /* Make master thread emulator. */
447 zebrad.master = thread_master_create ();
448
449 /* privs initialise */
450 zprivs_init (&zserv_privs);
451
452 /* Vty related initialize. */
453 signal_init (zebrad.master, array_size(zebra_signals), zebra_signals);
454 cmd_init (1);
455 vty_init (zebrad.master);
456 memory_init ();
457
458 /* Zebra related initialize. */
459 zebra_init ();
460 rib_init ();
461 zebra_if_init ();
462 zebra_debug_init ();
463 router_id_cmd_init ();
464 zebra_vty_init ();
465 access_list_init ();
466 prefix_list_init ();
467 #if defined (HAVE_RTADV)
468 rtadv_cmd_init ();
469 #endif
470 #ifdef HAVE_IRDP
471 irdp_init();
472 #endif
473 /* PTM socket */
474 #ifdef ZEBRA_PTM_SUPPORT
475 zebra_ptm_init();
476 #endif
477
478 /* For debug purpose. */
479 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
480
481 /* Initialize VRF module, and make kernel routing socket. */
482 zebra_vrf_init ();
483
484 #ifdef HAVE_SNMP
485 zebra_snmp_init ();
486 #endif /* HAVE_SNMP */
487
488 #ifdef HAVE_FPM
489 zfpm_init (zebrad.master, 1, 0);
490 #else
491 zfpm_init (zebrad.master, 0, 0);
492 #endif
493
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).
500 */
501 vty_read_config (config_file, config_default);
502
503 /* Don't start execution if we are in dry-run mode */
504 if (dryrun)
505 return(0);
506
507 /* Clean up rib. */
508 /* rib_weed_tables (); */
509
510 /* Exit when zebra is working in batch mode. */
511 if (batch_mode)
512 exit (0);
513
514 /* Daemonize. */
515 if (daemon_mode && daemon (0, 0) < 0)
516 {
517 zlog_err("Zebra daemon failed: %s", strerror(errno));
518 systemd_send_stopping ();
519 exit (1);
520 }
521
522 /* Output pid of zebra. */
523 pid_output (pid_file);
524
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.
533 */
534 if (! keep_kernel_mode)
535 rib_sweep_route ();
536
537 /* Needed for BSD routing socket. */
538 pid = getpid ();
539
540 /* This must be done only after locking pidfile (bug #403). */
541 zebra_zserv_socket_init (zserv_path);
542
543 /* Make vty server socket. */
544 vty_serv_sock (vty_addr, vty_port, ZEBRA_VTYSH_PATH);
545
546 /* Print banner. */
547 zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port);
548
549 while (thread_fetch (zebrad.master, &thread))
550 thread_call (&thread);
551
552 /* Not reached... */
553 return 0;
554 }