main.c 11 KB

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