babel_main.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * This file is free software: you may copy, redistribute and/or modify it
  3. * under the terms of the GNU General Public License as published by the
  4. * Free Software Foundation, either version 2 of the License, or (at your
  5. * option) any later version.
  6. *
  7. * This file is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. * General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. *
  15. * This file incorporates work covered by the following copyright and
  16. * permission notice:
  17. *
  18. Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
  19. Permission is hereby granted, free of charge, to any person obtaining a copy
  20. of this software and associated documentation files (the "Software"), to deal
  21. in the Software without restriction, including without limitation the rights
  22. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  23. copies of the Software, and to permit persons to whom the Software is
  24. furnished to do so, subject to the following conditions:
  25. The above copyright notice and this permission notice shall be included in
  26. all copies or substantial portions of the Software.
  27. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  30. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  31. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  32. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  33. THE SOFTWARE.
  34. */
  35. /* include zebra library */
  36. #include <zebra.h>
  37. #include "getopt.h"
  38. #include "if.h"
  39. #include "log.h"
  40. #include "thread.h"
  41. #include "privs.h"
  42. #include "sigevent.h"
  43. #include "version.h"
  44. #include "command.h"
  45. #include "vty.h"
  46. #include "memory.h"
  47. #include "babel_main.h"
  48. #include "babeld.h"
  49. #include "util.h"
  50. #include "kernel.h"
  51. #include "babel_interface.h"
  52. #include "neighbour.h"
  53. #include "route.h"
  54. #include "xroute.h"
  55. #include "message.h"
  56. #include "resend.h"
  57. #include "babel_zebra.h"
  58. static void babel_init (int argc, char **argv);
  59. static char *babel_get_progname(char *argv_0);
  60. static void babel_fail(void);
  61. static void babel_init_random(void);
  62. static void babel_replace_by_null(int fd);
  63. static void babel_init_signals(void);
  64. static void babel_exit_properly(void);
  65. static void babel_save_state_file(void);
  66. struct thread_master *master; /* quagga's threads handler */
  67. struct timeval babel_now; /* current time */
  68. unsigned char myid[8]; /* unique id (mac address of an interface) */
  69. int debug = 0;
  70. int resend_delay = -1;
  71. static const char *pidfile = PATH_BABELD_PID;
  72. const unsigned char zeroes[16] = {0};
  73. const unsigned char ones[16] =
  74. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  75. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  76. static const char *state_file = DAEMON_VTY_DIR "/babel-state";
  77. unsigned char protocol_group[16]; /* babel's link-local multicast address */
  78. int protocol_port; /* babel's port */
  79. int protocol_socket = -1; /* socket: communicate with others babeld */
  80. static char babel_config_default[] = SYSCONFDIR BABEL_DEFAULT_CONFIG;
  81. static char *babel_config_file = NULL;
  82. static char *babel_vty_addr = NULL;
  83. static int babel_vty_port = BABEL_VTY_PORT;
  84. /* Babeld options. */
  85. struct option longopts[] =
  86. {
  87. { "daemon", no_argument, NULL, 'd'},
  88. { "config_file", required_argument, NULL, 'f'},
  89. { "pid_file", required_argument, NULL, 'i'},
  90. { "socket", required_argument, NULL, 'z'},
  91. { "help", no_argument, NULL, 'h'},
  92. { "vty_addr", required_argument, NULL, 'A'},
  93. { "vty_port", required_argument, NULL, 'P'},
  94. { "user", required_argument, NULL, 'u'},
  95. { "group", required_argument, NULL, 'g'},
  96. { "version", no_argument, NULL, 'v'},
  97. { 0 }
  98. };
  99. /* babeld privileges */
  100. static zebra_capabilities_t _caps_p [] =
  101. {
  102. ZCAP_NET_RAW,
  103. ZCAP_BIND
  104. };
  105. static struct zebra_privs_t babeld_privs =
  106. {
  107. #if defined(QUAGGA_USER)
  108. .user = QUAGGA_USER,
  109. #endif
  110. #if defined QUAGGA_GROUP
  111. .group = QUAGGA_GROUP,
  112. #endif
  113. #ifdef VTY_GROUP
  114. .vty_group = VTY_GROUP,
  115. #endif
  116. .caps_p = _caps_p,
  117. .cap_num_p = 2,
  118. .cap_num_i = 0
  119. };
  120. int
  121. main(int argc, char **argv)
  122. {
  123. struct thread thread;
  124. /* and print banner too */
  125. babel_init(argc, argv);
  126. while (thread_fetch (master, &thread)) {
  127. thread_call (&thread);
  128. }
  129. return 0;
  130. }
  131. static void
  132. babel_usage (char *progname, int status)
  133. {
  134. if (status != 0)
  135. fprintf (stderr, "Try `%s --help' for more information.\n", progname);
  136. else
  137. {
  138. printf ("Usage : %s [OPTION...]\n\
  139. Daemon which manages Babel routing protocol.\n\n\
  140. -d, --daemon Runs in daemon mode\n\
  141. -f, --config_file Set configuration file name\n\
  142. -i, --pid_file Set process identifier file name\n\
  143. -z, --socket Set path of zebra socket\n\
  144. -A, --vty_addr Set vty's bind address\n\
  145. -P, --vty_port Set vty's port number\n\
  146. -u, --user User to run as\n\
  147. -g, --group Group to run as\n\
  148. -v, --version Print program version\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. /* make initialisations witch don't need infos about kernel(interfaces, etc.) */
  156. static void
  157. babel_init(int argc, char **argv)
  158. {
  159. int rc, opt;
  160. int do_daemonise = 0;
  161. char *progname = NULL;
  162. /* Set umask before anything for security */
  163. umask (0027);
  164. progname = babel_get_progname(argv[0]);
  165. /* set default log (lib/log.h) */
  166. zlog_default = openzlog(progname, ZLOG_BABEL,
  167. LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
  168. /* set log destination as stdout until the config file is read */
  169. zlog_set_level(NULL, ZLOG_DEST_STDOUT, LOG_WARNING);
  170. babel_init_random();
  171. /* set the Babel's default link-local multicast address and Babel's port */
  172. parse_address("ff02:0:0:0:0:0:1:6", protocol_group, NULL);
  173. protocol_port = 6696;
  174. /* get options */
  175. while(1) {
  176. opt = getopt_long(argc, argv, "df:i:z:hA:P:u:g:v", longopts, 0);
  177. if(opt < 0)
  178. break;
  179. switch(opt) {
  180. case 0:
  181. break;
  182. case 'd':
  183. do_daemonise = -1;
  184. break;
  185. case 'f':
  186. babel_config_file = optarg;
  187. break;
  188. case 'i':
  189. pidfile = optarg;
  190. break;
  191. case 'z':
  192. zclient_serv_path_set (optarg);
  193. break;
  194. case 'A':
  195. babel_vty_addr = optarg;
  196. break;
  197. case 'P':
  198. babel_vty_port = atoi (optarg);
  199. if (babel_vty_port <= 0 || babel_vty_port > 0xffff)
  200. babel_vty_port = BABEL_VTY_PORT;
  201. break;
  202. case 'u':
  203. babeld_privs.user = optarg;
  204. break;
  205. case 'g':
  206. babeld_privs.group = optarg;
  207. break;
  208. case 'v':
  209. print_version (progname);
  210. exit (0);
  211. break;
  212. case 'h':
  213. babel_usage (progname, 0);
  214. break;
  215. default:
  216. babel_usage (progname, 1);
  217. break;
  218. }
  219. }
  220. /* create the threads handler */
  221. master = thread_master_create ();
  222. /* Library inits. */
  223. zprivs_init (&babeld_privs);
  224. babel_init_signals();
  225. cmd_init (1);
  226. vty_init (master);
  227. memory_init ();
  228. resend_delay = BABEL_DEFAULT_RESEND_DELAY;
  229. babel_replace_by_null(STDIN_FILENO);
  230. if (do_daemonise && daemonise() < 0) {
  231. zlog_err("daemonise: %s", safe_strerror(errno));
  232. exit (1);
  233. }
  234. /* write pid file */
  235. if (pid_output(pidfile) < 0) {
  236. zlog_err("error while writing pidfile");
  237. exit (1);
  238. };
  239. /* init some quagga's dependencies, and babeld's commands */
  240. babeld_quagga_init();
  241. /* init zebra client's structure and it's commands */
  242. /* this replace kernel_setup && kernel_setup_socket */
  243. babelz_zebra_init ();
  244. /* Sort all installed commands. */
  245. sort_node ();
  246. /* Get zebra configuration file. */
  247. zlog_set_level (NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
  248. vty_read_config (babel_config_file, babel_config_default);
  249. /* Create VTY socket */
  250. vty_serv_sock (babel_vty_addr, babel_vty_port, BABEL_VTYSH_PATH);
  251. /* init buffer */
  252. rc = resize_receive_buffer(1500);
  253. if(rc < 0)
  254. babel_fail();
  255. schedule_neighbours_check(5000, 1);
  256. zlog_notice ("BABELd %s starting: vty@%d", BABEL_VERSION, babel_vty_port);
  257. }
  258. /* return the progname (without path, example: "./x/progname" --> "progname") */
  259. static char *
  260. babel_get_progname(char *argv_0) {
  261. char *p = strrchr (argv_0, '/');
  262. return (p ? ++p : argv_0);
  263. }
  264. static void
  265. babel_fail(void)
  266. {
  267. exit(1);
  268. }
  269. /* initialize random value, and set 'babel_now' by the way. */
  270. static void
  271. babel_init_random(void)
  272. {
  273. gettime(&babel_now);
  274. int rc;
  275. unsigned int seed;
  276. rc = read_random_bytes(&seed, sizeof(seed));
  277. if(rc < 0) {
  278. zlog_err("read(random): %s", safe_strerror(errno));
  279. seed = 42;
  280. }
  281. seed ^= (babel_now.tv_sec ^ babel_now.tv_usec);
  282. srandom(seed);
  283. }
  284. /*
  285. close fd, and replace it by "/dev/null"
  286. exit if error
  287. */
  288. static void
  289. babel_replace_by_null(int fd)
  290. {
  291. int fd_null;
  292. int rc;
  293. fd_null = open("/dev/null", O_RDONLY);
  294. if(fd_null < 0) {
  295. zlog_err("open(null): %s", safe_strerror(errno));
  296. exit(1);
  297. }
  298. rc = dup2(fd_null, fd);
  299. if(rc < 0) {
  300. zlog_err("dup2(null, 0): %s", safe_strerror(errno));
  301. exit(1);
  302. }
  303. close(fd_null);
  304. }
  305. /*
  306. Load the state file: check last babeld's running state, usefull in case of
  307. "/etc/init.d/babeld restart"
  308. */
  309. void
  310. babel_load_state_file(void)
  311. {
  312. int fd;
  313. int rc;
  314. fd = open(state_file, O_RDONLY);
  315. if(fd < 0 && errno != ENOENT)
  316. zlog_err("open(babel-state: %s)", safe_strerror(errno));
  317. rc = unlink(state_file);
  318. if(fd >= 0 && rc < 0) {
  319. zlog_err("unlink(babel-state): %s", safe_strerror(errno));
  320. /* If we couldn't unlink it, it's probably stale. */
  321. close(fd);
  322. fd = -1;
  323. }
  324. if(fd >= 0) {
  325. char buf[100];
  326. char buf2[100];
  327. int s;
  328. long t;
  329. rc = read(fd, buf, 99);
  330. if(rc < 0) {
  331. zlog_err("read(babel-state): %s", safe_strerror(errno));
  332. } else {
  333. buf[rc] = '\0';
  334. rc = sscanf(buf, "%99s %d %ld\n", buf2, &s, &t);
  335. if(rc == 3 && s >= 0 && s <= 0xFFFF) {
  336. unsigned char sid[8];
  337. rc = parse_eui64(buf2, sid);
  338. if(rc < 0) {
  339. zlog_err("Couldn't parse babel-state.");
  340. } else {
  341. struct timeval realnow;
  342. debugf(BABEL_DEBUG_COMMON,
  343. "Got %s %d %ld from babel-state.",
  344. format_eui64(sid), s, t);
  345. gettimeofday(&realnow, NULL);
  346. if(memcmp(sid, myid, 8) == 0)
  347. myseqno = seqno_plus(s, 1);
  348. else
  349. zlog_err("ID mismatch in babel-state. id=%s; old=%s",
  350. format_eui64(myid),
  351. format_eui64(sid));
  352. }
  353. } else {
  354. zlog_err("Couldn't parse babel-state.");
  355. }
  356. }
  357. close(fd);
  358. fd = -1;
  359. }
  360. }
  361. static void
  362. babel_sigexit(void)
  363. {
  364. zlog_notice("Terminating on signal");
  365. babel_exit_properly();
  366. }
  367. static void
  368. babel_sigusr1 (void)
  369. {
  370. zlog_rotate (NULL);
  371. }
  372. static void
  373. babel_init_signals(void)
  374. {
  375. static struct quagga_signal_t babel_signals[] =
  376. {
  377. {
  378. .signal = SIGUSR1,
  379. .handler = &babel_sigusr1,
  380. },
  381. {
  382. .signal = SIGINT,
  383. .handler = &babel_sigexit,
  384. },
  385. {
  386. .signal = SIGTERM,
  387. .handler = &babel_sigexit,
  388. },
  389. };
  390. signal_init (master, array_size(babel_signals), babel_signals);
  391. }
  392. static void
  393. babel_exit_properly(void)
  394. {
  395. debugf(BABEL_DEBUG_COMMON, "Exiting...");
  396. usleep(roughly(10000));
  397. gettime(&babel_now);
  398. /* Uninstall and flush all routes. */
  399. debugf(BABEL_DEBUG_COMMON, "Uninstall routes.");
  400. flush_all_routes();
  401. babel_interface_close_all();
  402. babel_zebra_close_connexion();
  403. babel_save_state_file();
  404. debugf(BABEL_DEBUG_COMMON, "Remove pid file.");
  405. if(pidfile)
  406. unlink(pidfile);
  407. debugf(BABEL_DEBUG_COMMON, "Done.");
  408. exit(0);
  409. }
  410. static void
  411. babel_save_state_file(void)
  412. {
  413. int fd;
  414. int rc;
  415. debugf(BABEL_DEBUG_COMMON, "Save state file.");
  416. fd = open(state_file, O_WRONLY | O_TRUNC | O_CREAT, 0644);
  417. if(fd < 0) {
  418. zlog_err("creat(babel-state): %s", safe_strerror(errno));
  419. unlink(state_file);
  420. } else {
  421. struct timeval realnow;
  422. char buf[100];
  423. gettimeofday(&realnow, NULL);
  424. rc = snprintf(buf, 100, "%s %d %ld\n",
  425. format_eui64(myid), (int)myseqno,
  426. (long)realnow.tv_sec);
  427. if(rc < 0 || rc >= 100) {
  428. zlog_err("write(babel-state): overflow.");
  429. unlink(state_file);
  430. } else {
  431. rc = write(fd, buf, rc);
  432. if(rc < 0) {
  433. zlog_err("write(babel-state): %s", safe_strerror(errno));
  434. unlink(state_file);
  435. }
  436. fsync(fd);
  437. }
  438. close(fd);
  439. }
  440. }
  441. void
  442. show_babel_main_configuration (struct vty *vty)
  443. {
  444. vty_out(vty,
  445. "pid file = %s%s"
  446. "state file = %s%s"
  447. "configuration file = %s%s"
  448. "protocol informations:%s"
  449. " multicast address = %s%s"
  450. " port = %d%s"
  451. "vty address = %s%s"
  452. "vty port = %d%s"
  453. "id = %s%s"
  454. "allow_duplicates = %s%s"
  455. "kernel_metric = %d%s",
  456. pidfile, VTY_NEWLINE,
  457. state_file, VTY_NEWLINE,
  458. babel_config_file ? babel_config_file : babel_config_default,
  459. VTY_NEWLINE,
  460. VTY_NEWLINE,
  461. format_address(protocol_group), VTY_NEWLINE,
  462. protocol_port, VTY_NEWLINE,
  463. babel_vty_addr ? babel_vty_addr : "None",
  464. VTY_NEWLINE,
  465. babel_vty_port, VTY_NEWLINE,
  466. format_eui64(myid), VTY_NEWLINE,
  467. format_bool(allow_duplicates), VTY_NEWLINE,
  468. kernel_metric, VTY_NEWLINE);
  469. }