main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. char *zserv_path = NULL;
  260. char *fpm_format = NULL;
  261. /* Set umask before anything for security */
  262. umask (0027);
  263. /* preserve my name */
  264. progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
  265. zlog_default = openzlog (progname, ZLOG_ZEBRA,
  266. LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
  267. while (1)
  268. {
  269. int opt;
  270. #ifdef HAVE_NETLINK
  271. opt = getopt_long (argc, argv, "bdkf:F:i:z:hA:P:ru:g:vs:C", longopts, 0);
  272. #else
  273. opt = getopt_long (argc, argv, "bdkf:F:i:z:hA:P:ru:g:vC", longopts, 0);
  274. #endif /* HAVE_NETLINK */
  275. if (opt == EOF)
  276. break;
  277. switch (opt)
  278. {
  279. case 0:
  280. break;
  281. case 'b':
  282. batch_mode = 1;
  283. case 'd':
  284. daemon_mode = 1;
  285. break;
  286. case 'k':
  287. keep_kernel_mode = 1;
  288. break;
  289. case 'C':
  290. dryrun = 1;
  291. break;
  292. case 'f':
  293. config_file = optarg;
  294. break;
  295. case 'F':
  296. fpm_format = optarg;
  297. break;
  298. case 'A':
  299. vty_addr = optarg;
  300. break;
  301. case 'i':
  302. pid_file = optarg;
  303. break;
  304. case 'z':
  305. zserv_path = optarg;
  306. break;
  307. case 'P':
  308. /* Deal with atoi() returning 0 on failure, and zebra not
  309. listening on zebra port... */
  310. if (strcmp(optarg, "0") == 0)
  311. {
  312. vty_port = 0;
  313. break;
  314. }
  315. vty_port = atoi (optarg);
  316. if (vty_port <= 0 || vty_port > 0xffff)
  317. vty_port = ZEBRA_VTY_PORT;
  318. break;
  319. case 'r':
  320. retain_mode = 1;
  321. break;
  322. #ifdef HAVE_NETLINK
  323. case 's':
  324. nl_rcvbufsize = atoi (optarg);
  325. break;
  326. #endif /* HAVE_NETLINK */
  327. case 'u':
  328. zserv_privs.user = optarg;
  329. break;
  330. case 'g':
  331. zserv_privs.group = optarg;
  332. break;
  333. case 'v':
  334. print_version (progname);
  335. exit (0);
  336. break;
  337. case 'h':
  338. usage (progname, 0);
  339. break;
  340. default:
  341. usage (progname, 1);
  342. break;
  343. }
  344. }
  345. /* Make master thread emulator. */
  346. zebrad.master = thread_master_create ();
  347. /* privs initialise */
  348. zprivs_init (&zserv_privs);
  349. /* Vty related initialize. */
  350. signal_init (zebrad.master, array_size(zebra_signals), zebra_signals);
  351. cmd_init (1);
  352. vty_init (zebrad.master);
  353. memory_init ();
  354. /* Zebra related initialize. */
  355. zebra_init ();
  356. rib_init ();
  357. zebra_if_init ();
  358. zebra_debug_init ();
  359. router_id_cmd_init ();
  360. zebra_vty_init ();
  361. access_list_init ();
  362. prefix_list_init ();
  363. #if defined (HAVE_RTADV)
  364. rtadv_cmd_init ();
  365. #endif
  366. #ifdef HAVE_IRDP
  367. irdp_init();
  368. #endif
  369. /* For debug purpose. */
  370. /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
  371. /* Initialize VRF module, and make kernel routing socket. */
  372. zebra_vrf_init ();
  373. #ifdef HAVE_SNMP
  374. zebra_snmp_init ();
  375. #endif /* HAVE_SNMP */
  376. #ifdef HAVE_FPM
  377. zfpm_init (zebrad.master, 1, 0, fpm_format);
  378. #else
  379. zfpm_init (zebrad.master, 0, 0, fpm_format);
  380. #endif
  381. /* Process the configuration file. Among other configuration
  382. * directives we can meet those installing static routes. Such
  383. * requests will not be executed immediately, but queued in
  384. * zebra->ribq structure until we enter the main execution loop.
  385. * The notifications from kernel will show originating PID equal
  386. * to that after daemon() completes (if ever called).
  387. */
  388. vty_read_config (config_file, config_default);
  389. /* Don't start execution if we are in dry-run mode */
  390. if (dryrun)
  391. return(0);
  392. /* Count up events for interfaces */
  393. if_startup_count_up ();
  394. /* Clean up rib. */
  395. rib_weed_tables ();
  396. /* Exit when zebra is working in batch mode. */
  397. if (batch_mode)
  398. exit (0);
  399. /* Daemonize. */
  400. if (daemon_mode && daemon (0, 0) < 0)
  401. {
  402. zlog_err("Zebra daemon failed: %s", strerror(errno));
  403. exit (1);
  404. }
  405. /* Output pid of zebra. */
  406. pid_output (pid_file);
  407. /* After we have successfully acquired the pidfile, we can be sure
  408. * about being the only copy of zebra process, which is submitting
  409. * changes to the FIB.
  410. * Clean up zebra-originated routes. The requests will be sent to OS
  411. * immediately, so originating PID in notifications from kernel
  412. * will be equal to the current getpid(). To know about such routes,
  413. * we have to have route_read() called before.
  414. */
  415. if (! keep_kernel_mode)
  416. rib_sweep_route ();
  417. /* Needed for BSD routing socket. */
  418. pid = getpid ();
  419. /* This must be done only after locking pidfile (bug #403). */
  420. zebra_zserv_socket_init (zserv_path);
  421. /* Make vty server socket. */
  422. vty_serv_sock (vty_addr, vty_port, ZEBRA_VTYSH_PATH);
  423. /* Print banner. */
  424. zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port);
  425. thread_main (zebrad.master);
  426. return 0;
  427. }