blob: 0fcba749d03a194007f90b52e1d97865ba80f0c2 [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) */
Denis Ovsienkoa14ef5e2012-02-11 21:06:16 +040080int debug = 0;
Paul Jakma57345092011-12-25 17:52:09 +010081
Juliusz Chroboczek52d54422012-02-11 13:08:00 +010082int resend_delay = -1;
Denis Ovsienko87c271c2012-01-10 15:58:04 +040083static const char *pidfile = PATH_BABELD_PID;
Paul Jakma57345092011-12-25 17:52:09 +010084
85const unsigned char zeroes[16] = {0};
86const unsigned char ones[16] =
87 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
88 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
89
Matthieu Boutier72db20b2012-01-18 22:06:10 +010090static const char *state_file = DAEMON_VTY_DIR "/babel-state";
Paul Jakma57345092011-12-25 17:52:09 +010091
92unsigned char protocol_group[16]; /* babel's link-local multicast address */
93int protocol_port; /* babel's port */
94int protocol_socket = -1; /* socket: communicate with others babeld */
95
96static char babel_config_default[] = SYSCONFDIR BABEL_DEFAULT_CONFIG;
97static char *babel_config_file = NULL;
98static char *babel_vty_addr = NULL;
99static int babel_vty_port = BABEL_VTY_PORT;
100
101/* Babeld options. */
102struct option longopts[] =
103{
104 { "daemon", no_argument, NULL, 'd'},
105 { "config_file", required_argument, NULL, 'f'},
106 { "pid_file", required_argument, NULL, 'i'},
Denis Ovsienko8f3607f2012-01-17 17:04:00 +0400107 { "socket", required_argument, NULL, 'z'},
Paul Jakma57345092011-12-25 17:52:09 +0100108 { "help", no_argument, NULL, 'h'},
109 { "vty_addr", required_argument, NULL, 'A'},
110 { "vty_port", required_argument, NULL, 'P'},
111 { "user", required_argument, NULL, 'u'},
112 { "group", required_argument, NULL, 'g'},
113 { "version", no_argument, NULL, 'v'},
114 { 0 }
115};
116
117/* babeld privileges */
118static zebra_capabilities_t _caps_p [] =
119{
120 ZCAP_NET_RAW,
121 ZCAP_BIND
122};
123static struct zebra_privs_t babeld_privs =
124{
125#if defined(QUAGGA_USER)
126 .user = QUAGGA_USER,
127#endif
128#if defined QUAGGA_GROUP
129 .group = QUAGGA_GROUP,
130#endif
131#ifdef VTY_GROUP
132 .vty_group = VTY_GROUP,
133#endif
134 .caps_p = _caps_p,
135 .cap_num_p = 2,
136 .cap_num_i = 0
137};
138
139
140int
141main(int argc, char **argv)
142{
143 struct thread thread;
144 /* and print banner too */
145 babel_init(argc, argv);
146 while (thread_fetch (master, &thread)) {
147 thread_call (&thread);
148 }
149 return 0;
150}
151
Denis Ovsienko446d73b2012-01-17 17:00:20 +0400152static void
153babel_usage (char *progname, int status)
154{
155 if (status != 0)
156 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
157 else
158 {
159 printf ("Usage : %s [OPTION...]\n\
160Daemon which manages Babel routing protocol.\n\n\
161-d, --daemon Runs in daemon mode\n\
162-f, --config_file Set configuration file name\n\
163-i, --pid_file Set process identifier file name\n\
Denis Ovsienko8f3607f2012-01-17 17:04:00 +0400164-z, --socket Set path of zebra socket\n\
Denis Ovsienko446d73b2012-01-17 17:00:20 +0400165-A, --vty_addr Set vty's bind address\n\
166-P, --vty_port Set vty's port number\n\
167-u, --user User to run as\n\
168-g, --group Group to run as\n\
169-v, --version Print program version\n\
170-h, --help Display this help and exit\n\
171\n\
172Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
173 }
174 exit (status);
175}
176
Paul Jakma57345092011-12-25 17:52:09 +0100177/* make initialisations witch don't need infos about kernel(interfaces, etc.) */
178static void
179babel_init(int argc, char **argv)
180{
181 int rc, opt;
182 int do_daemonise = 0;
183 char *progname = NULL;
Paul Jakma57345092011-12-25 17:52:09 +0100184
185 /* Set umask before anything for security */
186 umask (0027);
187 progname = babel_get_progname(argv[0]);
188
189 /* set default log (lib/log.h) */
190 zlog_default = openzlog(progname, ZLOG_BABEL,
191 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
192 /* set log destination as stdout until the config file is read */
193 zlog_set_level(NULL, ZLOG_DEST_STDOUT, LOG_WARNING);
194
195 babel_init_random();
196
197 /* set the Babel's default link-local multicast address and Babel's port */
198 parse_address("ff02:0:0:0:0:0:1:6", protocol_group, NULL);
199 protocol_port = 6696;
200
201 /* get options */
202 while(1) {
Denis Ovsienko8f3607f2012-01-17 17:04:00 +0400203 opt = getopt_long(argc, argv, "df:i:z:hA:P:u:g:v", longopts, 0);
Paul Jakma57345092011-12-25 17:52:09 +0100204 if(opt < 0)
205 break;
206
207 switch(opt) {
208 case 0:
209 break;
210 case 'd':
211 do_daemonise = -1;
212 break;
213 case 'f':
214 babel_config_file = optarg;
215 break;
216 case 'i':
217 pidfile = optarg;
218 break;
Denis Ovsienko8f3607f2012-01-17 17:04:00 +0400219 case 'z':
220 zclient_serv_path_set (optarg);
221 break;
Paul Jakma57345092011-12-25 17:52:09 +0100222 case 'A':
223 babel_vty_addr = optarg;
224 break;
225 case 'P':
226 babel_vty_port = atoi (optarg);
Juliusz Chroboczek38846de2012-02-07 05:43:36 +0100227 if (babel_vty_port <= 0 || babel_vty_port > 0xffff)
Paul Jakma57345092011-12-25 17:52:09 +0100228 babel_vty_port = BABEL_VTY_PORT;
229 break;
230 case 'u':
231 babeld_privs.user = optarg;
232 break;
233 case 'g':
234 babeld_privs.group = optarg;
235 break;
236 case 'v':
237 print_version (progname);
238 exit (0);
239 break;
240 case 'h':
Denis Ovsienko446d73b2012-01-17 17:00:20 +0400241 babel_usage (progname, 0);
Paul Jakma57345092011-12-25 17:52:09 +0100242 break;
243 default:
Denis Ovsienko446d73b2012-01-17 17:00:20 +0400244 babel_usage (progname, 1);
Paul Jakma57345092011-12-25 17:52:09 +0100245 break;
246 }
247 }
248
249 /* create the threads handler */
250 master = thread_master_create ();
251
252 /* Library inits. */
253 zprivs_init (&babeld_privs);
254 babel_init_signals();
255 cmd_init (1);
256 vty_init (master);
257 memory_init ();
258
Juliusz Chroboczek36329c02012-02-14 08:49:57 +0100259 resend_delay = BABEL_DEFAULT_RESEND_DELAY;
Juliusz Chroboczek52d54422012-02-11 13:08:00 +0100260
Paul Jakma57345092011-12-25 17:52:09 +0100261 if(parasitic && allow_duplicates >= 0) {
262 /* Too difficult to get right. */
263 zlog_err("Sorry, -P and -A are incompatible.");
264 exit(1);
265 }
266
267 babel_replace_by_null(STDIN_FILENO);
268
269 if (do_daemonise && daemonise() < 0) {
270 zlog_err("daemonise: %s", safe_strerror(errno));
271 exit (1);
272 }
273
274 /* write pid file */
275 if (pid_output(pidfile) < 0) {
276 zlog_err("error while writing pidfile");
277 exit (1);
278 };
279
280 /* init some quagga's dependencies, and babeld's commands */
281 babeld_quagga_init();
282 /* init zebra client's structure and it's commands */
283 /* this replace kernel_setup && kernel_setup_socket */
284 babelz_zebra_init ();
285
286 /* Sort all installed commands. */
287 sort_node ();
288
289 /* Get zebra configuration file. */
290 zlog_set_level (NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
291 vty_read_config (babel_config_file, babel_config_default);
292
Paul Jakma57345092011-12-25 17:52:09 +0100293 /* Create VTY socket */
294 vty_serv_sock (babel_vty_addr, babel_vty_port, BABEL_VTYSH_PATH);
295
296 /* init buffer */
297 rc = resize_receive_buffer(1500);
298 if(rc < 0)
299 babel_fail();
300
301 schedule_neighbours_check(5000, 1);
302
303 zlog_notice ("BABELd %s starting: vty@%d", BABEL_VERSION, babel_vty_port);
304}
305
306/* return the progname (without path, example: "./x/progname" --> "progname") */
307static char *
308babel_get_progname(char *argv_0) {
309 char *p = strrchr (argv_0, '/');
310 return (p ? ++p : argv_0);
311}
312
313static void
Paul Jakma57345092011-12-25 17:52:09 +0100314babel_fail(void)
315{
316 exit(1);
317}
318
319/* initialize random value, and set 'babel_now' by the way. */
320static void
321babel_init_random(void)
322{
323 gettime(&babel_now);
324 int rc;
325 unsigned int seed;
326
327 rc = read_random_bytes(&seed, sizeof(seed));
328 if(rc < 0) {
329 zlog_err("read(random): %s", safe_strerror(errno));
330 seed = 42;
331 }
332
333 seed ^= (babel_now.tv_sec ^ babel_now.tv_usec);
334 srandom(seed);
335}
336
337/*
338 close fd, and replace it by "/dev/null"
339 exit if error
340 */
341static void
342babel_replace_by_null(int fd)
343{
344 int fd_null;
345 int rc;
346
347 fd_null = open("/dev/null", O_RDONLY);
348 if(fd_null < 0) {
349 zlog_err("open(null): %s", safe_strerror(errno));
350 exit(1);
351 }
352
353 rc = dup2(fd_null, fd);
354 if(rc < 0) {
355 zlog_err("dup2(null, 0): %s", safe_strerror(errno));
356 exit(1);
357 }
358
359 close(fd_null);
360}
361
362/*
363 Load the state file: check last babeld's running state, usefull in case of
364 "/etc/init.d/babeld restart"
365 */
Matthieu Boutier69394542012-01-28 10:35:12 +0100366void
Paul Jakma57345092011-12-25 17:52:09 +0100367babel_load_state_file(void)
368{
Paul Jakma57345092011-12-25 17:52:09 +0100369 int fd;
370 int rc;
371
372 fd = open(state_file, O_RDONLY);
373 if(fd < 0 && errno != ENOENT)
374 zlog_err("open(babel-state: %s)", safe_strerror(errno));
375 rc = unlink(state_file);
376 if(fd >= 0 && rc < 0) {
377 zlog_err("unlink(babel-state): %s", safe_strerror(errno));
378 /* If we couldn't unlink it, it's probably stale. */
379 close(fd);
380 fd = -1;
381 }
382 if(fd >= 0) {
383 char buf[100];
384 char buf2[100];
385 int s;
386 long t;
387 rc = read(fd, buf, 99);
388 if(rc < 0) {
389 zlog_err("read(babel-state): %s", safe_strerror(errno));
390 } else {
391 buf[rc] = '\0';
392 rc = sscanf(buf, "%99s %d %ld\n", buf2, &s, &t);
393 if(rc == 3 && s >= 0 && s <= 0xFFFF) {
394 unsigned char sid[8];
395 rc = parse_eui64(buf2, sid);
396 if(rc < 0) {
397 zlog_err("Couldn't parse babel-state.");
398 } else {
399 struct timeval realnow;
400 debugf(BABEL_DEBUG_COMMON,
401 "Got %s %d %ld from babel-state.",
402 format_eui64(sid), s, t);
403 gettimeofday(&realnow, NULL);
404 if(memcmp(sid, myid, 8) == 0)
405 myseqno = seqno_plus(s, 1);
406 else
Matthieu Boutier210f6f62012-01-28 00:07:14 +0100407 zlog_err("ID mismatch in babel-state. id=%s; old=%s",
408 format_eui64(myid),
409 format_eui64(sid));
Paul Jakma57345092011-12-25 17:52:09 +0100410 }
411 } else {
412 zlog_err("Couldn't parse babel-state.");
413 }
414 }
415 close(fd);
416 fd = -1;
417 }
418}
419
420static void
421babel_sigexit(void)
422{
423 zlog_notice("Terminating on signal");
424
425 babel_exit_properly();
426}
427
428static void
429babel_sigusr1 (void)
430{
431 zlog_rotate (NULL);
432}
433
434static void
435babel_init_signals(void)
436{
437 static struct quagga_signal_t babel_signals[] =
438 {
439 {
440 .signal = SIGUSR1,
441 .handler = &babel_sigusr1,
442 },
443 {
444 .signal = SIGINT,
445 .handler = &babel_sigexit,
446 },
447 {
448 .signal = SIGTERM,
449 .handler = &babel_sigexit,
450 },
451 };
452
453 signal_init (master, Q_SIGC(babel_signals), babel_signals);
454}
455
456static void
457babel_exit_properly(void)
458{
459 debugf(BABEL_DEBUG_COMMON, "Exiting...");
460 usleep(roughly(10000));
461 gettime(&babel_now);
462
463 /* Uninstall and flush all routes. */
464 debugf(BABEL_DEBUG_COMMON, "Uninstall routes.");
Matthieu Boutierc35fafd2012-01-23 23:46:32 +0100465 flush_all_routes();
Paul Jakma57345092011-12-25 17:52:09 +0100466 babel_interface_close_all();
467 babel_zebra_close_connexion();
468 babel_save_state_file();
469 debugf(BABEL_DEBUG_COMMON, "Remove pid file.");
470 if(pidfile)
471 unlink(pidfile);
472 debugf(BABEL_DEBUG_COMMON, "Done.");
473
474 exit(0);
475}
476
477static void
478babel_save_state_file(void)
479{
480 int fd;
481 int rc;
482
483 debugf(BABEL_DEBUG_COMMON, "Save state file.");
484 fd = open(state_file, O_WRONLY | O_TRUNC | O_CREAT, 0644);
485 if(fd < 0) {
486 zlog_err("creat(babel-state): %s", safe_strerror(errno));
487 unlink(state_file);
488 } else {
489 struct timeval realnow;
490 char buf[100];
491 gettimeofday(&realnow, NULL);
492 rc = snprintf(buf, 100, "%s %d %ld\n",
493 format_eui64(myid), (int)myseqno,
494 (long)realnow.tv_sec);
495 if(rc < 0 || rc >= 100) {
496 zlog_err("write(babel-state): overflow.");
497 unlink(state_file);
498 } else {
499 rc = write(fd, buf, rc);
500 if(rc < 0) {
501 zlog_err("write(babel-state): %s", safe_strerror(errno));
502 unlink(state_file);
503 }
504 fsync(fd);
505 }
506 close(fd);
507 }
508}
Matthieu Boutierd3351d12012-01-19 22:36:56 +0100509
510void
511show_babel_main_configuration (struct vty *vty)
512{
Matthieu Boutierd3351d12012-01-19 22:36:56 +0100513 vty_out(vty,
514 "pid file = %s%s"
515 "state file = %s%s"
516 "configuration file = %s%s"
517 "protocol informations:%s"
518 " multicast address = %s%s"
519 " port = %d%s"
520 "vty address = %s%s"
521 "vty port = %d%s"
522 "id = %s%s"
Matthieu Boutierd3351d12012-01-19 22:36:56 +0100523 "parasitic = %s%s"
Juliusz Chroboczek9c298c72012-02-11 14:06:24 +0100524 "resend-delay = %d%s"
Matthieu Boutierd3351d12012-01-19 22:36:56 +0100525 "allow_duplicates = %s%s"
526 "kernel_metric = %d%s",
527 pidfile, VTY_NEWLINE,
528 state_file, VTY_NEWLINE,
529 babel_config_file ? babel_config_file : babel_config_default,
530 VTY_NEWLINE,
531 VTY_NEWLINE,
532 format_address(protocol_group), VTY_NEWLINE,
533 protocol_port, VTY_NEWLINE,
534 babel_vty_addr ? babel_vty_addr : "None",
535 VTY_NEWLINE,
536 babel_vty_port, VTY_NEWLINE,
537 format_eui64(myid), VTY_NEWLINE,
Matthieu Boutierd3351d12012-01-19 22:36:56 +0100538 format_bool(parasitic), VTY_NEWLINE,
Juliusz Chroboczek9c298c72012-02-11 14:06:24 +0100539 resend_delay, VTY_NEWLINE,
Matthieu Boutierd3351d12012-01-19 22:36:56 +0100540 format_bool(allow_duplicates), VTY_NEWLINE,
541 kernel_metric, VTY_NEWLINE);
542}