bgp_main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /* Main routine of bgpd.
  2. Copyright (C) 1996, 97, 98, 1999 Kunihiro Ishiguro
  3. This file is part of GNU Zebra.
  4. GNU Zebra is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. GNU Zebra is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Zebra; see the file COPYING. If not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. 02111-1307, USA. */
  16. #include <zebra.h>
  17. #include "vector.h"
  18. #include "vty.h"
  19. #include "command.h"
  20. #include "getopt.h"
  21. #include "thread.h"
  22. #include <lib/version.h>
  23. #include "memory.h"
  24. #include "prefix.h"
  25. #include "log.h"
  26. #include "privs.h"
  27. #include "sigevent.h"
  28. #include "zclient.h"
  29. #include "routemap.h"
  30. #include "filter.h"
  31. #include "plist.h"
  32. #include "stream.h"
  33. #include "vrf.h"
  34. #include "workqueue.h"
  35. #include "bgpd/bgpd.h"
  36. #include "bgpd/bgp_attr.h"
  37. #include "bgpd/bgp_mplsvpn.h"
  38. #include "bgpd/bgp_aspath.h"
  39. #include "bgpd/bgp_dump.h"
  40. #include "bgpd/bgp_route.h"
  41. #include "bgpd/bgp_nexthop.h"
  42. #include "bgpd/bgp_regex.h"
  43. #include "bgpd/bgp_clist.h"
  44. #include "bgpd/bgp_debug.h"
  45. #include "bgpd/bgp_filter.h"
  46. #include "bgpd/bgp_zebra.h"
  47. /* bgpd options, we use GNU getopt library. */
  48. static const struct option longopts[] =
  49. {
  50. { "daemon", no_argument, NULL, 'd'},
  51. { "config_file", required_argument, NULL, 'f'},
  52. { "pid_file", required_argument, NULL, 'i'},
  53. { "socket", required_argument, NULL, 'z'},
  54. { "bgp_port", required_argument, NULL, 'p'},
  55. { "listenon", required_argument, NULL, 'l'},
  56. { "vty_addr", required_argument, NULL, 'A'},
  57. { "vty_port", required_argument, NULL, 'P'},
  58. { "retain", no_argument, NULL, 'r'},
  59. { "no_kernel", no_argument, NULL, 'n'},
  60. { "user", required_argument, NULL, 'u'},
  61. { "group", required_argument, NULL, 'g'},
  62. { "skip_runas", no_argument, NULL, 'S'},
  63. { "version", no_argument, NULL, 'v'},
  64. { "dryrun", no_argument, NULL, 'C'},
  65. { "help", no_argument, NULL, 'h'},
  66. { 0 }
  67. };
  68. /* signal definitions */
  69. void sighup (void);
  70. void sigint (void);
  71. void sigusr1 (void);
  72. static void bgp_exit (int);
  73. static struct quagga_signal_t bgp_signals[] =
  74. {
  75. {
  76. .signal = SIGHUP,
  77. .handler = &sighup,
  78. },
  79. {
  80. .signal = SIGUSR1,
  81. .handler = &sigusr1,
  82. },
  83. {
  84. .signal = SIGINT,
  85. .handler = &sigint,
  86. },
  87. {
  88. .signal = SIGTERM,
  89. .handler = &sigint,
  90. },
  91. };
  92. /* Configuration file and directory. */
  93. char config_default[] = SYSCONFDIR BGP_DEFAULT_CONFIG;
  94. /* Route retain mode flag. */
  95. static int retain_mode = 0;
  96. /* Manually specified configuration file name. */
  97. char *config_file = NULL;
  98. /* Process ID saved for use by init system */
  99. static const char *pid_file = PATH_BGPD_PID;
  100. /* VTY port number and address. */
  101. int vty_port = BGP_VTY_PORT;
  102. char *vty_addr = NULL;
  103. /* privileges */
  104. static zebra_capabilities_t _caps_p [] =
  105. {
  106. ZCAP_BIND,
  107. ZCAP_NET_RAW,
  108. ZCAP_NET_ADMIN,
  109. };
  110. struct zebra_privs_t bgpd_privs =
  111. {
  112. #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
  113. .user = QUAGGA_USER,
  114. .group = QUAGGA_GROUP,
  115. #endif
  116. #ifdef VTY_GROUP
  117. .vty_group = VTY_GROUP,
  118. #endif
  119. .caps_p = _caps_p,
  120. .cap_num_p = array_size(_caps_p),
  121. .cap_num_i = 0,
  122. };
  123. /* Help information display. */
  124. static void
  125. usage (char *progname, int status)
  126. {
  127. if (status != 0)
  128. fprintf (stderr, "Try `%s --help' for more information.\n", progname);
  129. else
  130. {
  131. printf ("Usage : %s [OPTION...]\n\n\
  132. Daemon which manages kernel routing table management and \
  133. redistribution between different routing protocols.\n\n\
  134. -d, --daemon Runs in daemon mode\n\
  135. -f, --config_file Set configuration file name\n\
  136. -i, --pid_file Set process identifier file name\n\
  137. -z, --socket Set path of zebra socket\n\
  138. -p, --bgp_port Set bgp protocol's port number\n\
  139. -l, --listenon Listen on specified address (implies -n)\n\
  140. -A, --vty_addr Set vty's bind address\n\
  141. -P, --vty_port Set vty's port number\n\
  142. -r, --retain When program terminates, retain added route by bgpd.\n\
  143. -n, --no_kernel Do not install route to kernel.\n\
  144. -u, --user User to run as\n\
  145. -g, --group Group to run as\n\
  146. -S, --skip_runas Skip user and group run as\n\
  147. -v, --version Print program version\n\
  148. -C, --dryrun Check configuration for validity and exit\n\
  149. -h, --help Display this help and exit\n\
  150. \n\
  151. Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
  152. }
  153. exit (status);
  154. }
  155. /* SIGHUP handler. */
  156. void
  157. sighup (void)
  158. {
  159. zlog (NULL, LOG_INFO, "SIGHUP received");
  160. /* Terminate all thread. */
  161. bgp_terminate ();
  162. bgp_reset ();
  163. zlog_info ("bgpd restarting!");
  164. /* Reload config file. */
  165. vty_read_config (config_file, config_default);
  166. /* Create VTY's socket */
  167. vty_serv_sock (vty_addr, vty_port, BGP_VTYSH_PATH);
  168. /* Try to return to normal operation. */
  169. }
  170. /* SIGINT handler. */
  171. void
  172. sigint (void)
  173. {
  174. zlog_notice ("Terminating on signal");
  175. if (! retain_mode)
  176. {
  177. bgp_terminate ();
  178. if (bgpd_privs.user) /* NULL if skip_runas flag set */
  179. zprivs_terminate (&bgpd_privs);
  180. }
  181. bgp_exit (0);
  182. }
  183. /* SIGUSR1 handler. */
  184. void
  185. sigusr1 (void)
  186. {
  187. zlog_rotate (NULL);
  188. }
  189. /*
  190. Try to free up allocations we know about so that diagnostic tools such as
  191. valgrind are able to better illuminate leaks.
  192. Zebra route removal and protocol teardown are not meant to be done here.
  193. For example, "retain_mode" may be set.
  194. */
  195. static void
  196. bgp_exit (int status)
  197. {
  198. struct bgp *bgp;
  199. struct listnode *node, *nnode;
  200. int *socket;
  201. struct interface *ifp;
  202. /* it only makes sense for this to be called on a clean exit */
  203. assert (status == 0);
  204. /* reverse bgp_master_init */
  205. for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
  206. bgp_delete (bgp);
  207. list_free (bm->bgp);
  208. bm->bgp = NULL;
  209. /*
  210. * bgp_delete can re-allocate the process queues after they were
  211. * deleted in bgp_terminate. delete them again.
  212. *
  213. * It might be better to ensure the RIBs (including static routes)
  214. * are cleared by bgp_terminate() during its call to bgp_cleanup_routes(),
  215. * which currently only deletes the kernel routes.
  216. */
  217. if (bm->process_main_queue)
  218. {
  219. work_queue_free (bm->process_main_queue);
  220. bm->process_main_queue = NULL;
  221. }
  222. if (bm->process_rsclient_queue)
  223. {
  224. work_queue_free (bm->process_rsclient_queue);
  225. bm->process_rsclient_queue = NULL;
  226. }
  227. /* reverse bgp_master_init */
  228. for (ALL_LIST_ELEMENTS_RO(bm->listen_sockets, node, socket))
  229. {
  230. if (close ((int)(long)socket) == -1)
  231. zlog_err ("close (%d): %s", (int)(long)socket, safe_strerror (errno));
  232. }
  233. list_delete (bm->listen_sockets);
  234. /* reverse bgp_zebra_init/if_init */
  235. if (retain_mode)
  236. if_add_hook (IF_DELETE_HOOK, NULL);
  237. for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
  238. {
  239. struct listnode *c_node, *c_nnode;
  240. struct connected *c;
  241. for (ALL_LIST_ELEMENTS (ifp->connected, c_node, c_nnode, c))
  242. bgp_connected_delete (c);
  243. }
  244. /* reverse bgp_attr_init */
  245. bgp_attr_finish ();
  246. /* reverse bgp_dump_init */
  247. bgp_dump_finish ();
  248. /* reverse bgp_route_init */
  249. bgp_route_finish ();
  250. /* reverse bgp_route_map_init/route_map_init */
  251. route_map_finish ();
  252. /* reverse access_list_init */
  253. access_list_add_hook (NULL);
  254. access_list_delete_hook (NULL);
  255. access_list_reset ();
  256. /* reverse bgp_filter_init */
  257. as_list_add_hook (NULL);
  258. as_list_delete_hook (NULL);
  259. bgp_filter_reset ();
  260. /* reverse prefix_list_init */
  261. prefix_list_add_hook (NULL);
  262. prefix_list_delete_hook (NULL);
  263. prefix_list_reset ();
  264. /* reverse community_list_init */
  265. community_list_terminate (bgp_clist);
  266. vrf_terminate ();
  267. cmd_terminate ();
  268. vty_terminate ();
  269. bgp_address_destroy();
  270. bgp_scan_destroy();
  271. bgp_zebra_destroy();
  272. if (bgp_nexthop_buf)
  273. stream_free (bgp_nexthop_buf);
  274. if (bgp_ifindices_buf)
  275. stream_free (bgp_ifindices_buf);
  276. /* reverse bgp_scan_init */
  277. bgp_scan_finish ();
  278. /* reverse bgp_master_init */
  279. if (bm->master)
  280. thread_master_free (bm->master);
  281. if (zlog_default)
  282. closezlog (zlog_default);
  283. if (CONF_BGP_DEBUG (normal, NORMAL))
  284. log_memstats_stderr ("bgpd");
  285. exit (status);
  286. }
  287. /* Main routine of bgpd. Treatment of argument and start bgp finite
  288. state machine is handled at here. */
  289. int
  290. main (int argc, char **argv)
  291. {
  292. char *p;
  293. int opt;
  294. int daemon_mode = 0;
  295. int dryrun = 0;
  296. char *progname;
  297. int tmp_port;
  298. int skip_runas = 0;
  299. /* Set umask before anything for security */
  300. umask (0027);
  301. /* Preserve name of myself. */
  302. progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
  303. zlog_default = openzlog (progname, ZLOG_BGP,
  304. LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
  305. /* BGP master init. */
  306. bgp_master_init ();
  307. /* Command line argument treatment. */
  308. while (1)
  309. {
  310. opt = getopt_long (argc, argv, "df:i:z:hp:l:A:P:rnu:g:vCS", longopts, 0);
  311. if (opt == EOF)
  312. break;
  313. switch (opt)
  314. {
  315. case 0:
  316. break;
  317. case 'd':
  318. daemon_mode = 1;
  319. break;
  320. case 'f':
  321. config_file = optarg;
  322. break;
  323. case 'i':
  324. pid_file = optarg;
  325. break;
  326. case 'z':
  327. zclient_serv_path_set (optarg);
  328. break;
  329. case 'p':
  330. tmp_port = atoi (optarg);
  331. if (tmp_port <= 0 || tmp_port > 0xffff)
  332. bm->port = BGP_PORT_DEFAULT;
  333. else
  334. bm->port = tmp_port;
  335. break;
  336. case 'A':
  337. vty_addr = optarg;
  338. break;
  339. case 'P':
  340. /* Deal with atoi() returning 0 on failure, and bgpd not
  341. listening on bgp port... */
  342. if (strcmp(optarg, "0") == 0)
  343. {
  344. vty_port = 0;
  345. break;
  346. }
  347. vty_port = atoi (optarg);
  348. if (vty_port <= 0 || vty_port > 0xffff)
  349. vty_port = BGP_VTY_PORT;
  350. break;
  351. case 'r':
  352. retain_mode = 1;
  353. break;
  354. case 'l':
  355. bm->address = optarg;
  356. /* listenon implies -n */
  357. case 'n':
  358. bgp_option_set (BGP_OPT_NO_FIB);
  359. break;
  360. case 'u':
  361. bgpd_privs.user = optarg;
  362. break;
  363. case 'g':
  364. bgpd_privs.group = optarg;
  365. break;
  366. case 'S': /* skip run as = override bgpd_privs */
  367. skip_runas = 1;
  368. break;
  369. case 'v':
  370. print_version (progname);
  371. exit (0);
  372. break;
  373. case 'C':
  374. dryrun = 1;
  375. break;
  376. case 'h':
  377. usage (progname, 0);
  378. break;
  379. default:
  380. usage (progname, 1);
  381. break;
  382. }
  383. }
  384. /* Initializations. */
  385. srandom (time (NULL));
  386. signal_init (bm->master, array_size(bgp_signals), bgp_signals);
  387. if (skip_runas)
  388. memset (&bgpd_privs, 0, sizeof (bgpd_privs));
  389. zprivs_init (&bgpd_privs);
  390. cmd_init (1);
  391. vty_init (bm->master);
  392. memory_init ();
  393. vrf_init ();
  394. /* BGP related initialization. */
  395. bgp_init ();
  396. /* Parse config file. */
  397. vty_read_config (config_file, config_default);
  398. /* Start execution only if not in dry-run mode */
  399. if(dryrun)
  400. return(0);
  401. /* Turn into daemon if daemon_mode is set. */
  402. if (daemon_mode && daemon (0, 0) < 0)
  403. {
  404. zlog_err("BGPd daemon failed: %s", strerror(errno));
  405. return (1);
  406. }
  407. /* Process ID file creation. */
  408. pid_output (pid_file);
  409. /* Make bgp vty socket. */
  410. vty_serv_sock (vty_addr, vty_port, BGP_VTYSH_PATH);
  411. /* Print banner. */
  412. zlog_notice ("BGPd %s starting: vty@%d, bgp@%s:%d pid %d", QUAGGA_VERSION,
  413. vty_port,
  414. (bm->address ? bm->address : "<all>"),
  415. bm->port,
  416. getpid ());
  417. /* Start finite state machine, here we go! */
  418. thread_main (bm->master);
  419. /* Not reached. */
  420. return (0);
  421. }