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