blob: f2db0b3795688f0ceb646baae64d1f1529b2eb7d [file] [log] [blame]
Paul Jakma57345092011-12-25 17:52:09 +01001/*
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
19Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
20
21Permission is hereby granted, free of charge, to any person obtaining a copy
22of this software and associated documentation files (the "Software"), to deal
23in the Software without restriction, including without limitation the rights
24to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25copies of the Software, and to permit persons to whom the Software is
26furnished to do so, subject to the following conditions:
27
28The above copyright notice and this permission notice shall be included in
29all copies or substantial portions of the Software.
30
31THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
37THE SOFTWARE.
38*/
39
40/* include zebra library */
41#include <zebra.h>
42#include "getopt.h"
43#include "if.h"
44#include "log.h"
45#include "thread.h"
46#include "privs.h"
47#include "sigevent.h"
48#include "version.h"
49#include "command.h"
50#include "vty.h"
51#include "memory.h"
52
53#include "babel_main.h"
54#include "babeld.h"
55#include "util.h"
56#include "kernel.h"
57#include "babel_interface.h"
58#include "neighbour.h"
59#include "route.h"
60#include "xroute.h"
61#include "message.h"
62#include "resend.h"
63#include "babel_zebra.h"
64
65
66static void babel_init (int argc, char **argv);
Paul Jakma57345092011-12-25 17:52:09 +010067static char *babel_get_progname(char *argv_0);
68static void babel_fail(void);
69static void babel_init_random(void);
70static void babel_replace_by_null(int fd);
Paul Jakma57345092011-12-25 17:52:09 +010071static void babel_init_signals(void);
72static void babel_exit_properly(void);
73static void babel_save_state_file(void);
74
75
76struct thread_master *master; /* quagga's threads handler */
77struct timeval babel_now; /* current time */
78
79unsigned char myid[8]; /* unique id (mac address of an interface) */
80int debug = BABEL_DEBUG_COMMON;
81
Juliusz Chroboczek52d54422012-02-11 13:08:00 +010082int default_wireless_hello_interval = -1;
83int default_wired_hello_interval = -1;
84int resend_delay = -1;
Denis Ovsienko87c271c2012-01-10 15:58:04 +040085static const char *pidfile = PATH_BABELD_PID;
Paul Jakma57345092011-12-25 17:52:09 +010086
87const unsigned char zeroes[16] = {0};
88const unsigned char ones[16] =
89 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
90 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
91
Matthieu Boutier72db20b2012-01-18 22:06:10 +010092static const char *state_file = DAEMON_VTY_DIR "/babel-state";
Paul Jakma57345092011-12-25 17:52:09 +010093
94unsigned char protocol_group[16]; /* babel's link-local multicast address */
95int protocol_port; /* babel's port */
96int protocol_socket = -1; /* socket: communicate with others babeld */
97
98static char babel_config_default[] = SYSCONFDIR BABEL_DEFAULT_CONFIG;
99static char *babel_config_file = NULL;
100static char *babel_vty_addr = NULL;
101static int babel_vty_port = BABEL_VTY_PORT;
102
103/* Babeld options. */
104struct option longopts[] =
105{
106 { "daemon", no_argument, NULL, 'd'},
107 { "config_file", required_argument, NULL, 'f'},
108 { "pid_file", required_argument, NULL, 'i'},
Denis Ovsienko8f3607f2012-01-17 17:04:00 +0400109 { "socket", required_argument, NULL, 'z'},
Paul Jakma57345092011-12-25 17:52:09 +0100110 { "help", no_argument, NULL, 'h'},
111 { "vty_addr", required_argument, NULL, 'A'},
112 { "vty_port", required_argument, NULL, 'P'},
113 { "user", required_argument, NULL, 'u'},
114 { "group", required_argument, NULL, 'g'},
115 { "version", no_argument, NULL, 'v'},
116 { 0 }
117};
118
119/* babeld privileges */
120static zebra_capabilities_t _caps_p [] =
121{
122 ZCAP_NET_RAW,
123 ZCAP_BIND
124};
125static struct zebra_privs_t babeld_privs =
126{
127#if defined(QUAGGA_USER)
128 .user = QUAGGA_USER,
129#endif
130#if defined QUAGGA_GROUP
131 .group = QUAGGA_GROUP,
132#endif
133#ifdef VTY_GROUP
134 .vty_group = VTY_GROUP,
135#endif
136 .caps_p = _caps_p,
137 .cap_num_p = 2,
138 .cap_num_i = 0
139};
140
141
142int
143main(int argc, char **argv)
144{
145 struct thread thread;
146 /* and print banner too */
147 babel_init(argc, argv);
148 while (thread_fetch (master, &thread)) {
149 thread_call (&thread);
150 }
151 return 0;
152}
153
Denis Ovsienko446d73b2012-01-17 17:00:20 +0400154static void
155babel_usage (char *progname, int status)
156{
157 if (status != 0)
158 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
159 else
160 {
161 printf ("Usage : %s [OPTION...]\n\
162Daemon which manages Babel routing protocol.\n\n\
163-d, --daemon Runs in daemon mode\n\
164-f, --config_file Set configuration file name\n\
165-i, --pid_file Set process identifier file name\n\
Denis Ovsienko8f3607f2012-01-17 17:04:00 +0400166-z, --socket Set path of zebra socket\n\
Denis Ovsienko446d73b2012-01-17 17:00:20 +0400167-A, --vty_addr Set vty's bind address\n\
168-P, --vty_port Set vty's port number\n\
169-u, --user User to run as\n\
170-g, --group Group to run as\n\
171-v, --version Print program version\n\
172-h, --help Display this help and exit\n\
173\n\
174Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
175 }
176 exit (status);
177}
178
Paul Jakma57345092011-12-25 17:52:09 +0100179/* make initialisations witch don't need infos about kernel(interfaces, etc.) */
180static void
181babel_init(int argc, char **argv)
182{
183 int rc, opt;
184 int do_daemonise = 0;
185 char *progname = NULL;
Paul Jakma57345092011-12-25 17:52:09 +0100186
187 /* Set umask before anything for security */
188 umask (0027);
189 progname = babel_get_progname(argv[0]);
190
191 /* set default log (lib/log.h) */
192 zlog_default = openzlog(progname, ZLOG_BABEL,
193 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
194 /* set log destination as stdout until the config file is read */
195 zlog_set_level(NULL, ZLOG_DEST_STDOUT, LOG_WARNING);
196
197 babel_init_random();
198
199 /* set the Babel's default link-local multicast address and Babel's port */
200 parse_address("ff02:0:0:0:0:0:1:6", protocol_group, NULL);
201 protocol_port = 6696;
202
203 /* get options */
204 while(1) {
Denis Ovsienko8f3607f2012-01-17 17:04:00 +0400205 opt = getopt_long(argc, argv, "df:i:z:hA:P:u:g:v", longopts, 0);
Paul Jakma57345092011-12-25 17:52:09 +0100206 if(opt < 0)
207 break;
208
209 switch(opt) {
210 case 0:
211 break;
212 case 'd':
213 do_daemonise = -1;
214 break;
215 case 'f':
216 babel_config_file = optarg;
217 break;
218 case 'i':
219 pidfile = optarg;
220 break;
Denis Ovsienko8f3607f2012-01-17 17:04:00 +0400221 case 'z':
222 zclient_serv_path_set (optarg);
223 break;
Paul Jakma57345092011-12-25 17:52:09 +0100224 case 'A':
225 babel_vty_addr = optarg;
226 break;
227 case 'P':
228 babel_vty_port = atoi (optarg);
Juliusz Chroboczek38846de2012-02-07 05:43:36 +0100229 if (babel_vty_port <= 0 || babel_vty_port > 0xffff)
Paul Jakma57345092011-12-25 17:52:09 +0100230 babel_vty_port = BABEL_VTY_PORT;
231 break;
232 case 'u':
233 babeld_privs.user = optarg;
234 break;
235 case 'g':
236 babeld_privs.group = optarg;
237 break;
238 case 'v':
239 print_version (progname);
240 exit (0);
241 break;
242 case 'h':
Denis Ovsienko446d73b2012-01-17 17:00:20 +0400243 babel_usage (progname, 0);
Paul Jakma57345092011-12-25 17:52:09 +0100244 break;
245 default:
Denis Ovsienko446d73b2012-01-17 17:00:20 +0400246 babel_usage (progname, 1);
Paul Jakma57345092011-12-25 17:52:09 +0100247 break;
248 }
249 }
250
251 /* create the threads handler */
252 master = thread_master_create ();
253
254 /* Library inits. */
255 zprivs_init (&babeld_privs);
256 babel_init_signals();
257 cmd_init (1);
258 vty_init (master);
259 memory_init ();
260
Juliusz Chroboczek52d54422012-02-11 13:08:00 +0100261 if(default_wireless_hello_interval <= 0)
262 default_wireless_hello_interval = 4000;
263 default_wireless_hello_interval = MAX(default_wireless_hello_interval, 5);
Paul Jakma57345092011-12-25 17:52:09 +0100264
Juliusz Chroboczek52d54422012-02-11 13:08:00 +0100265 if(default_wired_hello_interval <= 0)
266 default_wired_hello_interval = 4000;
267 default_wired_hello_interval = MAX(default_wired_hello_interval, 5);
Paul Jakma57345092011-12-25 17:52:09 +0100268
Juliusz Chroboczek52d54422012-02-11 13:08:00 +0100269 resend_delay = 2000;
270 resend_delay = MIN(resend_delay, default_wireless_hello_interval / 2);
271 resend_delay = MIN(resend_delay, default_wired_hello_interval / 2);
272 resend_delay = MAX(resend_delay, 20);
273
Paul Jakma57345092011-12-25 17:52:09 +0100274 if(parasitic && allow_duplicates >= 0) {
275 /* Too difficult to get right. */
276 zlog_err("Sorry, -P and -A are incompatible.");
277 exit(1);
278 }
279
280 babel_replace_by_null(STDIN_FILENO);
281
282 if (do_daemonise && daemonise() < 0) {
283 zlog_err("daemonise: %s", safe_strerror(errno));
284 exit (1);
285 }
286
287 /* write pid file */
288 if (pid_output(pidfile) < 0) {
289 zlog_err("error while writing pidfile");
290 exit (1);
291 };
292
293 /* init some quagga's dependencies, and babeld's commands */
294 babeld_quagga_init();
295 /* init zebra client's structure and it's commands */
296 /* this replace kernel_setup && kernel_setup_socket */
297 babelz_zebra_init ();
298
299 /* Sort all installed commands. */
300 sort_node ();
301
302 /* Get zebra configuration file. */
303 zlog_set_level (NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
304 vty_read_config (babel_config_file, babel_config_default);
305
Paul Jakma57345092011-12-25 17:52:09 +0100306 /* Create VTY socket */
307 vty_serv_sock (babel_vty_addr, babel_vty_port, BABEL_VTYSH_PATH);
308
309 /* init buffer */
310 rc = resize_receive_buffer(1500);
311 if(rc < 0)
312 babel_fail();
313
314 schedule_neighbours_check(5000, 1);
315
316 zlog_notice ("BABELd %s starting: vty@%d", BABEL_VERSION, babel_vty_port);
317}
318
319/* return the progname (without path, example: "./x/progname" --> "progname") */
320static char *
321babel_get_progname(char *argv_0) {
322 char *p = strrchr (argv_0, '/');
323 return (p ? ++p : argv_0);
324}
325
326static void
Paul Jakma57345092011-12-25 17:52:09 +0100327babel_fail(void)
328{
329 exit(1);
330}
331
332/* initialize random value, and set 'babel_now' by the way. */
333static void
334babel_init_random(void)
335{
336 gettime(&babel_now);
337 int rc;
338 unsigned int seed;
339
340 rc = read_random_bytes(&seed, sizeof(seed));
341 if(rc < 0) {
342 zlog_err("read(random): %s", safe_strerror(errno));
343 seed = 42;
344 }
345
346 seed ^= (babel_now.tv_sec ^ babel_now.tv_usec);
347 srandom(seed);
348}
349
350/*
351 close fd, and replace it by "/dev/null"
352 exit if error
353 */
354static void
355babel_replace_by_null(int fd)
356{
357 int fd_null;
358 int rc;
359
360 fd_null = open("/dev/null", O_RDONLY);
361 if(fd_null < 0) {
362 zlog_err("open(null): %s", safe_strerror(errno));
363 exit(1);
364 }
365
366 rc = dup2(fd_null, fd);
367 if(rc < 0) {
368 zlog_err("dup2(null, 0): %s", safe_strerror(errno));
369 exit(1);
370 }
371
372 close(fd_null);
373}
374
375/*
376 Load the state file: check last babeld's running state, usefull in case of
377 "/etc/init.d/babeld restart"
378 */
Matthieu Boutier69394542012-01-28 10:35:12 +0100379void
Paul Jakma57345092011-12-25 17:52:09 +0100380babel_load_state_file(void)
381{
Matthieu Boutierb5d43c92012-01-19 22:38:56 +0100382 time_t reboot_time;
Paul Jakma57345092011-12-25 17:52:09 +0100383 reboot_time = babel_now.tv_sec;
384 int fd;
385 int rc;
386
387 fd = open(state_file, O_RDONLY);
388 if(fd < 0 && errno != ENOENT)
389 zlog_err("open(babel-state: %s)", safe_strerror(errno));
390 rc = unlink(state_file);
391 if(fd >= 0 && rc < 0) {
392 zlog_err("unlink(babel-state): %s", safe_strerror(errno));
393 /* If we couldn't unlink it, it's probably stale. */
394 close(fd);
395 fd = -1;
396 }
397 if(fd >= 0) {
398 char buf[100];
399 char buf2[100];
400 int s;
401 long t;
402 rc = read(fd, buf, 99);
403 if(rc < 0) {
404 zlog_err("read(babel-state): %s", safe_strerror(errno));
405 } else {
406 buf[rc] = '\0';
407 rc = sscanf(buf, "%99s %d %ld\n", buf2, &s, &t);
408 if(rc == 3 && s >= 0 && s <= 0xFFFF) {
409 unsigned char sid[8];
410 rc = parse_eui64(buf2, sid);
411 if(rc < 0) {
412 zlog_err("Couldn't parse babel-state.");
413 } else {
414 struct timeval realnow;
415 debugf(BABEL_DEBUG_COMMON,
416 "Got %s %d %ld from babel-state.",
417 format_eui64(sid), s, t);
418 gettimeofday(&realnow, NULL);
419 if(memcmp(sid, myid, 8) == 0)
420 myseqno = seqno_plus(s, 1);
421 else
Matthieu Boutier210f6f62012-01-28 00:07:14 +0100422 zlog_err("ID mismatch in babel-state. id=%s; old=%s",
423 format_eui64(myid),
424 format_eui64(sid));
Paul Jakma57345092011-12-25 17:52:09 +0100425 /* Convert realtime into monotonic time. */
426 if(t >= 1176800000L && t <= realnow.tv_sec)
427 reboot_time = babel_now.tv_sec - (realnow.tv_sec - t);
428 }
429 } else {
430 zlog_err("Couldn't parse babel-state.");
431 }
432 }
433 close(fd);
434 fd = -1;
435 }
436}
437
438static void
439babel_sigexit(void)
440{
441 zlog_notice("Terminating on signal");
442
443 babel_exit_properly();
444}
445
446static void
447babel_sigusr1 (void)
448{
449 zlog_rotate (NULL);
450}
451
452static void
453babel_init_signals(void)
454{
455 static struct quagga_signal_t babel_signals[] =
456 {
457 {
458 .signal = SIGUSR1,
459 .handler = &babel_sigusr1,
460 },
461 {
462 .signal = SIGINT,
463 .handler = &babel_sigexit,
464 },
465 {
466 .signal = SIGTERM,
467 .handler = &babel_sigexit,
468 },
469 };
470
471 signal_init (master, Q_SIGC(babel_signals), babel_signals);
472}
473
474static void
475babel_exit_properly(void)
476{
477 debugf(BABEL_DEBUG_COMMON, "Exiting...");
478 usleep(roughly(10000));
479 gettime(&babel_now);
480
481 /* Uninstall and flush all routes. */
482 debugf(BABEL_DEBUG_COMMON, "Uninstall routes.");
Matthieu Boutierc35fafd2012-01-23 23:46:32 +0100483 flush_all_routes();
Paul Jakma57345092011-12-25 17:52:09 +0100484 babel_interface_close_all();
485 babel_zebra_close_connexion();
486 babel_save_state_file();
487 debugf(BABEL_DEBUG_COMMON, "Remove pid file.");
488 if(pidfile)
489 unlink(pidfile);
490 debugf(BABEL_DEBUG_COMMON, "Done.");
491
492 exit(0);
493}
494
495static void
496babel_save_state_file(void)
497{
498 int fd;
499 int rc;
500
501 debugf(BABEL_DEBUG_COMMON, "Save state file.");
502 fd = open(state_file, O_WRONLY | O_TRUNC | O_CREAT, 0644);
503 if(fd < 0) {
504 zlog_err("creat(babel-state): %s", safe_strerror(errno));
505 unlink(state_file);
506 } else {
507 struct timeval realnow;
508 char buf[100];
509 gettimeofday(&realnow, NULL);
510 rc = snprintf(buf, 100, "%s %d %ld\n",
511 format_eui64(myid), (int)myseqno,
512 (long)realnow.tv_sec);
513 if(rc < 0 || rc >= 100) {
514 zlog_err("write(babel-state): overflow.");
515 unlink(state_file);
516 } else {
517 rc = write(fd, buf, rc);
518 if(rc < 0) {
519 zlog_err("write(babel-state): %s", safe_strerror(errno));
520 unlink(state_file);
521 }
522 fsync(fd);
523 }
524 close(fd);
525 }
526}
Matthieu Boutierd3351d12012-01-19 22:36:56 +0100527
528void
529show_babel_main_configuration (struct vty *vty)
530{
531#ifdef NO_DEBUG
532 vty_out(vty, "No debug.%s", VTY_NEWLINE);
533#else
534 vty_out(vty, "Activated debug options:");
535 if (debug == BABEL_DEBUG_ALL) {
536 vty_out(vty, " all%s", VTY_NEWLINE);
537 } else {
538 vty_out(vty, "%s%s%s%s%s%s%s%s%s%s%s%s%s",
539 debug & BABEL_DEBUG_COMMON ? VTY_NEWLINE : "",
540 debug & BABEL_DEBUG_COMMON ? " common" : "",
541 debug & BABEL_DEBUG_KERNEL ? VTY_NEWLINE : "",
542 debug & BABEL_DEBUG_KERNEL ? " kernel" : "",
543 debug & BABEL_DEBUG_FILTER ? VTY_NEWLINE : "",
544 debug & BABEL_DEBUG_FILTER ? " filter" : "",
545 debug & BABEL_DEBUG_TIMEOUT ? VTY_NEWLINE : "",
546 debug & BABEL_DEBUG_TIMEOUT ? " timeout" : "",
547 debug & BABEL_DEBUG_IF ? VTY_NEWLINE : "",
548 debug & BABEL_DEBUG_IF ? " interface": "",
549 debug & BABEL_DEBUG_ROUTE ? VTY_NEWLINE : "",
550 debug & BABEL_DEBUG_ROUTE ? " route" : "",
551 VTY_NEWLINE);
552 }
553#endif
554
555 vty_out(vty,
556 "pid file = %s%s"
557 "state file = %s%s"
558 "configuration file = %s%s"
559 "protocol informations:%s"
560 " multicast address = %s%s"
561 " port = %d%s"
562 "vty address = %s%s"
563 "vty port = %d%s"
564 "id = %s%s"
Matthieu Boutierd3351d12012-01-19 22:36:56 +0100565 "parasitic = %s%s"
566 "split-horizon = %s%s"
567 "allow_duplicates = %s%s"
568 "kernel_metric = %d%s",
569 pidfile, VTY_NEWLINE,
570 state_file, VTY_NEWLINE,
571 babel_config_file ? babel_config_file : babel_config_default,
572 VTY_NEWLINE,
573 VTY_NEWLINE,
574 format_address(protocol_group), VTY_NEWLINE,
575 protocol_port, VTY_NEWLINE,
576 babel_vty_addr ? babel_vty_addr : "None",
577 VTY_NEWLINE,
578 babel_vty_port, VTY_NEWLINE,
579 format_eui64(myid), VTY_NEWLINE,
Matthieu Boutierd3351d12012-01-19 22:36:56 +0100580 format_bool(parasitic), VTY_NEWLINE,
581 format_bool(split_horizon), VTY_NEWLINE,
582 format_bool(allow_duplicates), VTY_NEWLINE,
583 kernel_metric, VTY_NEWLINE);
584}