main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. #ifdef RTADV
  204. rtadv_init (zvrf);
  205. #endif
  206. kernel_init (zvrf);
  207. interface_list (zvrf);
  208. route_read (zvrf);
  209. return 0;
  210. }
  211. /* Callback upon disabling a VRF. */
  212. static int
  213. zebra_vrf_disable (vrf_id_t vrf_id, void **info)
  214. {
  215. struct zebra_vrf *zvrf = (struct zebra_vrf *) (*info);
  216. struct listnode *list_node;
  217. struct interface *ifp;
  218. assert (zvrf);
  219. rib_close_table (zvrf->table[AFI_IP][SAFI_UNICAST]);
  220. rib_close_table (zvrf->table[AFI_IP6][SAFI_UNICAST]);
  221. for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), list_node, ifp))
  222. {
  223. int operative = if_is_operative (ifp);
  224. UNSET_FLAG (ifp->flags, IFF_UP);
  225. if (operative)
  226. if_down (ifp);
  227. }
  228. #ifdef RTADV
  229. rtadv_terminate (zvrf);
  230. #endif
  231. kernel_terminate (zvrf);
  232. list_delete_all_node (zvrf->rid_all_sorted_list);
  233. list_delete_all_node (zvrf->rid_lo_sorted_list);
  234. return 0;
  235. }
  236. /* Zebra VRF initialization. */
  237. static void
  238. zebra_vrf_init (void)
  239. {
  240. vrf_add_hook (VRF_NEW_HOOK, zebra_vrf_new);
  241. vrf_add_hook (VRF_ENABLE_HOOK, zebra_vrf_enable);
  242. vrf_add_hook (VRF_DISABLE_HOOK, zebra_vrf_disable);
  243. vrf_init ();
  244. }
  245. /* Main startup routine. */
  246. int
  247. main (int argc, char **argv)
  248. {
  249. char *p;
  250. char *vty_addr = NULL;
  251. int vty_port = ZEBRA_VTY_PORT;
  252. int dryrun = 0;
  253. int batch_mode = 0;
  254. int daemon_mode = 0;
  255. char *config_file = NULL;
  256. char *progname;
  257. struct thread thread;
  258. char *zserv_path = NULL;
  259. /* Set umask before anything for security */
  260. umask (0027);
  261. /* preserve my name */
  262. progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
  263. zlog_default = openzlog (progname, ZLOG_ZEBRA,
  264. LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
  265. while (1)
  266. {
  267. int opt;
  268. #ifdef HAVE_NETLINK
  269. opt = getopt_long (argc, argv, "bdkf:i:z:hA:P:ru:g:vs:C", longopts, 0);
  270. #else
  271. opt = getopt_long (argc, argv, "bdkf:i:z:hA:P:ru:g:vC", longopts, 0);
  272. #endif /* HAVE_NETLINK */
  273. if (opt == EOF)
  274. break;
  275. switch (opt)
  276. {
  277. case 0:
  278. break;
  279. case 'b':
  280. batch_mode = 1;
  281. case 'd':
  282. daemon_mode = 1;
  283. break;
  284. case 'k':
  285. keep_kernel_mode = 1;
  286. break;
  287. case 'C':
  288. dryrun = 1;
  289. break;
  290. case 'f':
  291. config_file = optarg;
  292. break;
  293. case 'A':
  294. vty_addr = optarg;
  295. break;
  296. case 'i':
  297. pid_file = optarg;
  298. break;
  299. case 'z':
  300. zserv_path = optarg;
  301. break;
  302. case 'P':
  303. /* Deal with atoi() returning 0 on failure, and zebra not
  304. listening on zebra port... */
  305. if (strcmp(optarg, "0") == 0)
  306. {
  307. vty_port = 0;
  308. break;
  309. }
  310. vty_port = atoi (optarg);
  311. if (vty_port <= 0 || vty_port > 0xffff)
  312. vty_port = ZEBRA_VTY_PORT;
  313. break;
  314. case 'r':
  315. retain_mode = 1;
  316. break;
  317. #ifdef HAVE_NETLINK
  318. case 's':
  319. nl_rcvbufsize = atoi (optarg);
  320. break;
  321. #endif /* HAVE_NETLINK */
  322. case 'u':
  323. zserv_privs.user = optarg;
  324. break;
  325. case 'g':
  326. zserv_privs.group = optarg;
  327. break;
  328. case 'v':
  329. print_version (progname);
  330. exit (0);
  331. break;
  332. case 'h':
  333. usage (progname, 0);
  334. break;
  335. default:
  336. usage (progname, 1);
  337. break;
  338. }
  339. }
  340. /* Make master thread emulator. */
  341. zebrad.master = thread_master_create ();
  342. /* privs initialise */
  343. zprivs_init (&zserv_privs);
  344. /* Vty related initialize. */
  345. signal_init (zebrad.master, array_size(zebra_signals), zebra_signals);
  346. cmd_init (1);
  347. vty_init (zebrad.master);
  348. memory_init ();
  349. /* Zebra related initialize. */
  350. zebra_init ();
  351. rib_init ();
  352. zebra_if_init ();
  353. zebra_debug_init ();
  354. router_id_cmd_init ();
  355. zebra_vty_init ();
  356. access_list_init ();
  357. prefix_list_init ();
  358. #ifdef RTADV
  359. rtadv_cmd_init ();
  360. #endif
  361. #ifdef HAVE_IRDP
  362. irdp_init();
  363. #endif
  364. /* For debug purpose. */
  365. /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
  366. /* Initialize VRF module, and make kernel routing socket. */
  367. zebra_vrf_init ();
  368. #ifdef HAVE_SNMP
  369. zebra_snmp_init ();
  370. #endif /* HAVE_SNMP */
  371. #ifdef HAVE_FPM
  372. zfpm_init (zebrad.master, 1, 0);
  373. #else
  374. zfpm_init (zebrad.master, 0, 0);
  375. #endif
  376. /* Process the configuration file. Among other configuration
  377. * directives we can meet those installing static routes. Such
  378. * requests will not be executed immediately, but queued in
  379. * zebra->ribq structure until we enter the main execution loop.
  380. * The notifications from kernel will show originating PID equal
  381. * to that after daemon() completes (if ever called).
  382. */
  383. vty_read_config (config_file, config_default);
  384. /* Don't start execution if we are in dry-run mode */
  385. if (dryrun)
  386. return(0);
  387. /* Clean up rib. */
  388. rib_weed_tables ();
  389. /* Exit when zebra is working in batch mode. */
  390. if (batch_mode)
  391. exit (0);
  392. /* Daemonize. */
  393. if (daemon_mode && daemon (0, 0) < 0)
  394. {
  395. zlog_err("Zebra daemon failed: %s", strerror(errno));
  396. exit (1);
  397. }
  398. /* Output pid of zebra. */
  399. pid_output (pid_file);
  400. /* After we have successfully acquired the pidfile, we can be sure
  401. * about being the only copy of zebra process, which is submitting
  402. * changes to the FIB.
  403. * Clean up zebra-originated routes. The requests will be sent to OS
  404. * immediately, so originating PID in notifications from kernel
  405. * will be equal to the current getpid(). To know about such routes,
  406. * we have to have route_read() called before.
  407. */
  408. if (! keep_kernel_mode)
  409. rib_sweep_route ();
  410. /* Needed for BSD routing socket. */
  411. pid = getpid ();
  412. /* This must be done only after locking pidfile (bug #403). */
  413. zebra_zserv_socket_init (zserv_path);
  414. /* Make vty server socket. */
  415. vty_serv_sock (vty_addr, vty_port, ZEBRA_VTYSH_PATH);
  416. /* Print banner. */
  417. zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port);
  418. while (thread_fetch (zebrad.master, &thread))
  419. thread_call (&thread);
  420. /* Not reached... */
  421. return 0;
  422. }