]> git.proxmox.com Git - mirror_frr.git/blob - babeld/babel_main.c
babeld: Initial import, for Babel routing protocol.
[mirror_frr.git] / babeld / babel_main.c
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
19 Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
20
21 Permission is hereby granted, free of charge, to any person obtaining a copy
22 of this software and associated documentation files (the "Software"), to deal
23 in the Software without restriction, including without limitation the rights
24 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25 copies of the Software, and to permit persons to whom the Software is
26 furnished to do so, subject to the following conditions:
27
28 The above copyright notice and this permission notice shall be included in
29 all copies or substantial portions of the Software.
30
31 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
37 THE 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
66 static void babel_init (int argc, char **argv);
67 static void babel_usage (char *progname);
68 static char *babel_get_progname(char *argv_0);
69 static void babel_fail(void);
70 static void babel_init_random(void);
71 static void babel_replace_by_null(int fd);
72 static void babel_load_state_file(void);
73 static void babel_init_signals(void);
74 static void babel_exit_properly(void);
75 static void babel_save_state_file(void);
76
77
78 struct thread_master *master; /* quagga's threads handler */
79 struct timeval babel_now; /* current time */
80
81 unsigned char myid[8]; /* unique id (mac address of an interface) */
82 int debug = BABEL_DEBUG_COMMON;
83
84 time_t reboot_time;
85 int idle_time = 320;
86 int link_detect = 0;
87 int wireless_hello_interval = -1;
88 int wired_hello_interval = -1;
89 int idle_hello_interval = -1;
90 char *pidfile = "/var/run/babeld.pid";
91
92 const unsigned char zeroes[16] = {0};
93 const unsigned char ones[16] =
94 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
95 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
96
97 static char *state_file = "/var/lib/babeld/babel-state";
98
99 unsigned char protocol_group[16]; /* babel's link-local multicast address */
100 int protocol_port; /* babel's port */
101 int protocol_socket = -1; /* socket: communicate with others babeld */
102
103 static char babel_config_default[] = SYSCONFDIR BABEL_DEFAULT_CONFIG;
104 static char *babel_config_file = NULL;
105 static char *babel_vty_addr = NULL;
106 static int babel_vty_port = BABEL_VTY_PORT;
107
108 /* Babeld options. */
109 struct 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 */
124 static zebra_capabilities_t _caps_p [] =
125 {
126 ZCAP_NET_RAW,
127 ZCAP_BIND
128 };
129 static 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
146 int
147 main(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.) */
159 static void
160 babel_init(int argc, char **argv)
161 {
162 int rc, opt;
163 int do_daemonise = 0;
164 char *progname = NULL;
165 char *pidfile = PATH_BABELD_PID;
166
167 /* Set umask before anything for security */
168 umask (0027);
169 progname = babel_get_progname(argv[0]);
170
171 /* set default log (lib/log.h) */
172 zlog_default = openzlog(progname, ZLOG_BABEL,
173 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
174 /* set log destination as stdout until the config file is read */
175 zlog_set_level(NULL, ZLOG_DEST_STDOUT, LOG_WARNING);
176
177 babel_init_random();
178
179 /* set the Babel's default link-local multicast address and Babel's port */
180 parse_address("ff02:0:0:0:0:0:1:6", protocol_group, NULL);
181 protocol_port = 6696;
182
183 /* get options */
184 while(1) {
185 opt = getopt_long(argc, argv, "df:i:hA:P:u:g:v", longopts, 0);
186 if(opt < 0)
187 break;
188
189 switch(opt) {
190 case 0:
191 break;
192 case 'd':
193 do_daemonise = -1;
194 break;
195 case 'f':
196 babel_config_file = optarg;
197 break;
198 case 'i':
199 pidfile = optarg;
200 break;
201 case 'A':
202 babel_vty_addr = optarg;
203 break;
204 case 'P':
205 babel_vty_port = atoi (optarg);
206 if (babel_vty_port < 0 || babel_vty_port > 0xffff
207 || (babel_vty_port == 0 && optarg[0] != '0'/*atoi error*/))
208 babel_vty_port = BABEL_VTY_PORT;
209 break;
210 case 'u':
211 babeld_privs.user = optarg;
212 break;
213 case 'g':
214 babeld_privs.group = optarg;
215 break;
216 case 'v':
217 print_version (progname);
218 exit (0);
219 break;
220 case 'h':
221 babel_usage (progname);
222 break;
223 default:
224 babel_usage(progname);
225 break;
226 }
227 }
228
229 /* create the threads handler */
230 master = thread_master_create ();
231
232 /* Library inits. */
233 zprivs_init (&babeld_privs);
234 babel_init_signals();
235 cmd_init (1);
236 vty_init (master);
237 memory_init ();
238
239 /* babeld inits (default options) */
240 /* set default interval's values */
241 if(wireless_hello_interval <= 0)
242 wireless_hello_interval = 4000;
243 wireless_hello_interval = MAX(wireless_hello_interval, 5);
244
245 if(wired_hello_interval <= 0)
246 wired_hello_interval = 4000;
247 wired_hello_interval = MAX(wired_hello_interval, 5);
248
249 /* an assertion */
250 if(parasitic && allow_duplicates >= 0) {
251 /* Too difficult to get right. */
252 zlog_err("Sorry, -P and -A are incompatible.");
253 exit(1);
254 }
255
256 babel_replace_by_null(STDIN_FILENO);
257
258 if (do_daemonise && daemonise() < 0) {
259 zlog_err("daemonise: %s", safe_strerror(errno));
260 exit (1);
261 }
262
263 /* write pid file */
264 if (pid_output(pidfile) < 0) {
265 zlog_err("error while writing pidfile");
266 exit (1);
267 };
268
269 /* init some quagga's dependencies, and babeld's commands */
270 babeld_quagga_init();
271 /* init zebra client's structure and it's commands */
272 /* this replace kernel_setup && kernel_setup_socket */
273 babelz_zebra_init ();
274
275 /* Sort all installed commands. */
276 sort_node ();
277
278 /* Get zebra configuration file. */
279 zlog_set_level (NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
280 vty_read_config (babel_config_file, babel_config_default);
281
282 myseqno = (random() & 0xFFFF);
283 babel_load_state_file();
284
285 /* Create VTY socket */
286 vty_serv_sock (babel_vty_addr, babel_vty_port, BABEL_VTYSH_PATH);
287
288 /* init buffer */
289 rc = resize_receive_buffer(1500);
290 if(rc < 0)
291 babel_fail();
292
293 schedule_neighbours_check(5000, 1);
294
295 zlog_notice ("BABELd %s starting: vty@%d", BABEL_VERSION, babel_vty_port);
296 }
297
298 /* return the progname (without path, example: "./x/progname" --> "progname") */
299 static char *
300 babel_get_progname(char *argv_0) {
301 char *p = strrchr (argv_0, '/');
302 return (p ? ++p : argv_0);
303 }
304
305 static void
306 babel_usage(char *progname)
307 {
308 fprintf(stderr,
309 "Syntax: %s "
310 "[-m multicast_address] [-p port] [-S state-file]\n"
311 " "
312 "[-h hello] [-H wired_hello] [-i idle_hello]\n"
313 " "
314 "[-k metric] [-A metric] [-s] [-P] [-l] [-w] [-d level] [-g port]\n"
315 " "
316 "[-t table] [-T table] [-c file] [-C statement]\n"
317 " "
318 "[-D] [-L logfile] [-I pidfile]\n"
319 " "
320 "[id] interface...\n",
321 progname);
322 exit(1);
323 }
324
325 static void
326 babel_fail(void)
327 {
328 exit(1);
329 }
330
331 /* initialize random value, and set 'babel_now' by the way. */
332 static void
333 babel_init_random(void)
334 {
335 gettime(&babel_now);
336 int rc;
337 unsigned int seed;
338
339 rc = read_random_bytes(&seed, sizeof(seed));
340 if(rc < 0) {
341 zlog_err("read(random): %s", safe_strerror(errno));
342 seed = 42;
343 }
344
345 seed ^= (babel_now.tv_sec ^ babel_now.tv_usec);
346 srandom(seed);
347 }
348
349 /*
350 close fd, and replace it by "/dev/null"
351 exit if error
352 */
353 static void
354 babel_replace_by_null(int fd)
355 {
356 int fd_null;
357 int rc;
358
359 fd_null = open("/dev/null", O_RDONLY);
360 if(fd_null < 0) {
361 zlog_err("open(null): %s", safe_strerror(errno));
362 exit(1);
363 }
364
365 rc = dup2(fd_null, fd);
366 if(rc < 0) {
367 zlog_err("dup2(null, 0): %s", safe_strerror(errno));
368 exit(1);
369 }
370
371 close(fd_null);
372 }
373
374 /*
375 Load the state file: check last babeld's running state, usefull in case of
376 "/etc/init.d/babeld restart"
377 */
378 static void
379 babel_load_state_file(void)
380 {
381 reboot_time = babel_now.tv_sec;
382 int fd;
383 int rc;
384
385 fd = open(state_file, O_RDONLY);
386 if(fd < 0 && errno != ENOENT)
387 zlog_err("open(babel-state: %s)", safe_strerror(errno));
388 rc = unlink(state_file);
389 if(fd >= 0 && rc < 0) {
390 zlog_err("unlink(babel-state): %s", safe_strerror(errno));
391 /* If we couldn't unlink it, it's probably stale. */
392 close(fd);
393 fd = -1;
394 }
395 if(fd >= 0) {
396 char buf[100];
397 char buf2[100];
398 int s;
399 long t;
400 rc = read(fd, buf, 99);
401 if(rc < 0) {
402 zlog_err("read(babel-state): %s", safe_strerror(errno));
403 } else {
404 buf[rc] = '\0';
405 rc = sscanf(buf, "%99s %d %ld\n", buf2, &s, &t);
406 if(rc == 3 && s >= 0 && s <= 0xFFFF) {
407 unsigned char sid[8];
408 rc = parse_eui64(buf2, sid);
409 if(rc < 0) {
410 zlog_err("Couldn't parse babel-state.");
411 } else {
412 struct timeval realnow;
413 debugf(BABEL_DEBUG_COMMON,
414 "Got %s %d %ld from babel-state.",
415 format_eui64(sid), s, t);
416 gettimeofday(&realnow, NULL);
417 if(memcmp(sid, myid, 8) == 0)
418 myseqno = seqno_plus(s, 1);
419 else
420 zlog_err("ID mismatch in babel-state.");
421 /* Convert realtime into monotonic time. */
422 if(t >= 1176800000L && t <= realnow.tv_sec)
423 reboot_time = babel_now.tv_sec - (realnow.tv_sec - t);
424 }
425 } else {
426 zlog_err("Couldn't parse babel-state.");
427 }
428 }
429 close(fd);
430 fd = -1;
431 }
432 }
433
434 static void
435 babel_sigexit(void)
436 {
437 zlog_notice("Terminating on signal");
438
439 babel_exit_properly();
440 }
441
442 static void
443 babel_sigusr1 (void)
444 {
445 zlog_rotate (NULL);
446 }
447
448 static void
449 babel_init_signals(void)
450 {
451 static struct quagga_signal_t babel_signals[] =
452 {
453 {
454 .signal = SIGUSR1,
455 .handler = &babel_sigusr1,
456 },
457 {
458 .signal = SIGINT,
459 .handler = &babel_sigexit,
460 },
461 {
462 .signal = SIGTERM,
463 .handler = &babel_sigexit,
464 },
465 };
466
467 signal_init (master, Q_SIGC(babel_signals), babel_signals);
468 }
469
470 static void
471 babel_exit_properly(void)
472 {
473 debugf(BABEL_DEBUG_COMMON, "Exiting...");
474 usleep(roughly(10000));
475 gettime(&babel_now);
476
477 /* Uninstall and flush all routes. */
478 debugf(BABEL_DEBUG_COMMON, "Uninstall routes.");
479 babel_uninstall_all_routes();
480 babel_interface_close_all();
481 babel_zebra_close_connexion();
482 babel_save_state_file();
483 debugf(BABEL_DEBUG_COMMON, "Remove pid file.");
484 if(pidfile)
485 unlink(pidfile);
486 debugf(BABEL_DEBUG_COMMON, "Done.");
487
488 exit(0);
489 }
490
491 static void
492 babel_save_state_file(void)
493 {
494 int fd;
495 int rc;
496
497 debugf(BABEL_DEBUG_COMMON, "Save state file.");
498 fd = open(state_file, O_WRONLY | O_TRUNC | O_CREAT, 0644);
499 if(fd < 0) {
500 zlog_err("creat(babel-state): %s", safe_strerror(errno));
501 unlink(state_file);
502 } else {
503 struct timeval realnow;
504 char buf[100];
505 gettimeofday(&realnow, NULL);
506 rc = snprintf(buf, 100, "%s %d %ld\n",
507 format_eui64(myid), (int)myseqno,
508 (long)realnow.tv_sec);
509 if(rc < 0 || rc >= 100) {
510 zlog_err("write(babel-state): overflow.");
511 unlink(state_file);
512 } else {
513 rc = write(fd, buf, rc);
514 if(rc < 0) {
515 zlog_err("write(babel-state): %s", safe_strerror(errno));
516 unlink(state_file);
517 }
518 fsync(fd);
519 }
520 close(fd);
521 }
522 }