bgp_main.c 11 KB

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