blob: fd1d94f4ffae212ddc7c9279362b5aada6275f8f [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);
71static void babel_load_state_file(void);
72static void babel_init_signals(void);
73static void babel_exit_properly(void);
74static void babel_save_state_file(void);
75
76
77struct thread_master *master; /* quagga's threads handler */
78struct timeval babel_now; /* current time */
79
80unsigned char myid[8]; /* unique id (mac address of an interface) */
81int debug = BABEL_DEBUG_COMMON;
82
83time_t reboot_time;
84int idle_time = 320;
85int link_detect = 0;
86int wireless_hello_interval = -1;
87int wired_hello_interval = -1;
88int idle_hello_interval = -1;
Denis Ovsienko87c271c2012-01-10 15:58:04 +040089static const char *pidfile = PATH_BABELD_PID;
Paul Jakma57345092011-12-25 17:52:09 +010090
91const unsigned char zeroes[16] = {0};
92const unsigned char ones[16] =
93 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
94 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
95
Denis Ovsienko87c271c2012-01-10 15:58:04 +040096static const char *state_file = "/var/lib/babeld/babel-state";
Paul Jakma57345092011-12-25 17:52:09 +010097
98unsigned char protocol_group[16]; /* babel's link-local multicast address */
99int protocol_port; /* babel's port */
100int protocol_socket = -1; /* socket: communicate with others babeld */
101
102static char babel_config_default[] = SYSCONFDIR BABEL_DEFAULT_CONFIG;
103static char *babel_config_file = NULL;
104static char *babel_vty_addr = NULL;
105static int babel_vty_port = BABEL_VTY_PORT;
106
107/* Babeld options. */
108struct option longopts[] =
109{
110 { "daemon", no_argument, NULL, 'd'},
111 { "config_file", required_argument, NULL, 'f'},
112 { "pid_file", required_argument, NULL, 'i'},
113 { "help", no_argument, NULL, 'h'},
114 { "vty_addr", required_argument, NULL, 'A'},
115 { "vty_port", required_argument, NULL, 'P'},
116 { "user", required_argument, NULL, 'u'},
117 { "group", required_argument, NULL, 'g'},
118 { "version", no_argument, NULL, 'v'},
119 { 0 }
120};
121
122/* babeld privileges */
123static zebra_capabilities_t _caps_p [] =
124{
125 ZCAP_NET_RAW,
126 ZCAP_BIND
127};
128static struct zebra_privs_t babeld_privs =
129{
130#if defined(QUAGGA_USER)
131 .user = QUAGGA_USER,
132#endif
133#if defined QUAGGA_GROUP
134 .group = QUAGGA_GROUP,
135#endif
136#ifdef VTY_GROUP
137 .vty_group = VTY_GROUP,
138#endif
139 .caps_p = _caps_p,
140 .cap_num_p = 2,
141 .cap_num_i = 0
142};
143
144
145int
146main(int argc, char **argv)
147{
148 struct thread thread;
149 /* and print banner too */
150 babel_init(argc, argv);
151 while (thread_fetch (master, &thread)) {
152 thread_call (&thread);
153 }
154 return 0;
155}
156
Denis Ovsienko446d73b2012-01-17 17:00:20 +0400157static void
158babel_usage (char *progname, int status)
159{
160 if (status != 0)
161 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
162 else
163 {
164 printf ("Usage : %s [OPTION...]\n\
165Daemon which manages Babel routing protocol.\n\n\
166-d, --daemon Runs in daemon mode\n\
167-f, --config_file Set configuration file name\n\
168-i, --pid_file Set process identifier file name\n\
169-A, --vty_addr Set vty's bind address\n\
170-P, --vty_port Set vty's port number\n\
171-u, --user User to run as\n\
172-g, --group Group to run as\n\
173-v, --version Print program version\n\
174-h, --help Display this help and exit\n\
175\n\
176Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
177 }
178 exit (status);
179}
180
Paul Jakma57345092011-12-25 17:52:09 +0100181/* make initialisations witch don't need infos about kernel(interfaces, etc.) */
182static void
183babel_init(int argc, char **argv)
184{
185 int rc, opt;
186 int do_daemonise = 0;
187 char *progname = NULL;
Paul Jakma57345092011-12-25 17:52:09 +0100188
189 /* Set umask before anything for security */
190 umask (0027);
191 progname = babel_get_progname(argv[0]);
192
193 /* set default log (lib/log.h) */
194 zlog_default = openzlog(progname, ZLOG_BABEL,
195 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
196 /* set log destination as stdout until the config file is read */
197 zlog_set_level(NULL, ZLOG_DEST_STDOUT, LOG_WARNING);
198
199 babel_init_random();
200
201 /* set the Babel's default link-local multicast address and Babel's port */
202 parse_address("ff02:0:0:0:0:0:1:6", protocol_group, NULL);
203 protocol_port = 6696;
204
205 /* get options */
206 while(1) {
207 opt = getopt_long(argc, argv, "df:i:hA:P:u:g:v", longopts, 0);
208 if(opt < 0)
209 break;
210
211 switch(opt) {
212 case 0:
213 break;
214 case 'd':
215 do_daemonise = -1;
216 break;
217 case 'f':
218 babel_config_file = optarg;
219 break;
220 case 'i':
221 pidfile = optarg;
222 break;
223 case 'A':
224 babel_vty_addr = optarg;
225 break;
226 case 'P':
227 babel_vty_port = atoi (optarg);
228 if (babel_vty_port < 0 || babel_vty_port > 0xffff
229 || (babel_vty_port == 0 && optarg[0] != '0'/*atoi error*/))
230 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
261 /* babeld inits (default options) */
262 /* set default interval's values */
263 if(wireless_hello_interval <= 0)
264 wireless_hello_interval = 4000;
265 wireless_hello_interval = MAX(wireless_hello_interval, 5);
266
267 if(wired_hello_interval <= 0)
268 wired_hello_interval = 4000;
269 wired_hello_interval = MAX(wired_hello_interval, 5);
270
271 /* an assertion */
272 if(parasitic && allow_duplicates >= 0) {
273 /* Too difficult to get right. */
274 zlog_err("Sorry, -P and -A are incompatible.");
275 exit(1);
276 }
277
278 babel_replace_by_null(STDIN_FILENO);
279
280 if (do_daemonise && daemonise() < 0) {
281 zlog_err("daemonise: %s", safe_strerror(errno));
282 exit (1);
283 }
284
285 /* write pid file */
286 if (pid_output(pidfile) < 0) {
287 zlog_err("error while writing pidfile");
288 exit (1);
289 };
290
291 /* init some quagga's dependencies, and babeld's commands */
292 babeld_quagga_init();
293 /* init zebra client's structure and it's commands */
294 /* this replace kernel_setup && kernel_setup_socket */
295 babelz_zebra_init ();
296
297 /* Sort all installed commands. */
298 sort_node ();
299
300 /* Get zebra configuration file. */
301 zlog_set_level (NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
302 vty_read_config (babel_config_file, babel_config_default);
303
304 myseqno = (random() & 0xFFFF);
305 babel_load_state_file();
306
307 /* Create VTY socket */
308 vty_serv_sock (babel_vty_addr, babel_vty_port, BABEL_VTYSH_PATH);
309
310 /* init buffer */
311 rc = resize_receive_buffer(1500);
312 if(rc < 0)
313 babel_fail();
314
315 schedule_neighbours_check(5000, 1);
316
317 zlog_notice ("BABELd %s starting: vty@%d", BABEL_VERSION, babel_vty_port);
318}
319
320/* return the progname (without path, example: "./x/progname" --> "progname") */
321static char *
322babel_get_progname(char *argv_0) {
323 char *p = strrchr (argv_0, '/');
324 return (p ? ++p : argv_0);
325}
326
327static void
Paul Jakma57345092011-12-25 17:52:09 +0100328babel_fail(void)
329{
330 exit(1);
331}
332
333/* initialize random value, and set 'babel_now' by the way. */
334static void
335babel_init_random(void)
336{
337 gettime(&babel_now);
338 int rc;
339 unsigned int seed;
340
341 rc = read_random_bytes(&seed, sizeof(seed));
342 if(rc < 0) {
343 zlog_err("read(random): %s", safe_strerror(errno));
344 seed = 42;
345 }
346
347 seed ^= (babel_now.tv_sec ^ babel_now.tv_usec);
348 srandom(seed);
349}
350
351/*
352 close fd, and replace it by "/dev/null"
353 exit if error
354 */
355static void
356babel_replace_by_null(int fd)
357{
358 int fd_null;
359 int rc;
360
361 fd_null = open("/dev/null", O_RDONLY);
362 if(fd_null < 0) {
363 zlog_err("open(null): %s", safe_strerror(errno));
364 exit(1);
365 }
366
367 rc = dup2(fd_null, fd);
368 if(rc < 0) {
369 zlog_err("dup2(null, 0): %s", safe_strerror(errno));
370 exit(1);
371 }
372
373 close(fd_null);
374}
375
376/*
377 Load the state file: check last babeld's running state, usefull in case of
378 "/etc/init.d/babeld restart"
379 */
380static void
381babel_load_state_file(void)
382{
383 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
422 zlog_err("ID mismatch in babel-state.");
423 /* Convert realtime into monotonic time. */
424 if(t >= 1176800000L && t <= realnow.tv_sec)
425 reboot_time = babel_now.tv_sec - (realnow.tv_sec - t);
426 }
427 } else {
428 zlog_err("Couldn't parse babel-state.");
429 }
430 }
431 close(fd);
432 fd = -1;
433 }
434}
435
436static void
437babel_sigexit(void)
438{
439 zlog_notice("Terminating on signal");
440
441 babel_exit_properly();
442}
443
444static void
445babel_sigusr1 (void)
446{
447 zlog_rotate (NULL);
448}
449
450static void
451babel_init_signals(void)
452{
453 static struct quagga_signal_t babel_signals[] =
454 {
455 {
456 .signal = SIGUSR1,
457 .handler = &babel_sigusr1,
458 },
459 {
460 .signal = SIGINT,
461 .handler = &babel_sigexit,
462 },
463 {
464 .signal = SIGTERM,
465 .handler = &babel_sigexit,
466 },
467 };
468
469 signal_init (master, Q_SIGC(babel_signals), babel_signals);
470}
471
472static void
473babel_exit_properly(void)
474{
475 debugf(BABEL_DEBUG_COMMON, "Exiting...");
476 usleep(roughly(10000));
477 gettime(&babel_now);
478
479 /* Uninstall and flush all routes. */
480 debugf(BABEL_DEBUG_COMMON, "Uninstall routes.");
481 babel_uninstall_all_routes();
482 babel_interface_close_all();
483 babel_zebra_close_connexion();
484 babel_save_state_file();
485 debugf(BABEL_DEBUG_COMMON, "Remove pid file.");
486 if(pidfile)
487 unlink(pidfile);
488 debugf(BABEL_DEBUG_COMMON, "Done.");
489
490 exit(0);
491}
492
493static void
494babel_save_state_file(void)
495{
496 int fd;
497 int rc;
498
499 debugf(BABEL_DEBUG_COMMON, "Save state file.");
500 fd = open(state_file, O_WRONLY | O_TRUNC | O_CREAT, 0644);
501 if(fd < 0) {
502 zlog_err("creat(babel-state): %s", safe_strerror(errno));
503 unlink(state_file);
504 } else {
505 struct timeval realnow;
506 char buf[100];
507 gettimeofday(&realnow, NULL);
508 rc = snprintf(buf, 100, "%s %d %ld\n",
509 format_eui64(myid), (int)myseqno,
510 (long)realnow.tv_sec);
511 if(rc < 0 || rc >= 100) {
512 zlog_err("write(babel-state): overflow.");
513 unlink(state_file);
514 } else {
515 rc = write(fd, buf, rc);
516 if(rc < 0) {
517 zlog_err("write(babel-state): %s", safe_strerror(errno));
518 unlink(state_file);
519 }
520 fsync(fd);
521 }
522 close(fd);
523 }
524}