main.c 11 KB

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