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.
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.
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/>.
15 * This file incorporates work covered by the following copyright and
19 Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
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:
28 The above copyright notice and this permission notice shall be included in
29 all copies or substantial portions of the Software.
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
40 /* include zebra library */
53 #include "babel_main.h"
57 #include "babel_interface.h"
58 #include "neighbour.h"
63 #include "babel_zebra.h"
66 static void babel_init (int argc
, char **argv
);
67 static char *babel_get_progname(char *argv_0
);
68 static void babel_fail(void);
69 static void babel_init_random(void);
70 static void babel_replace_by_null(int fd
);
71 static void babel_init_signals(void);
72 static void babel_exit_properly(void);
73 static void babel_save_state_file(void);
76 struct thread_master
*master
; /* quagga's threads handler */
77 struct timeval babel_now
; /* current time */
79 unsigned char myid
[8]; /* unique id (mac address of an interface) */
82 int resend_delay
= -1;
83 static const char *pidfile
= PATH_BABELD_PID
;
85 const unsigned char zeroes
[16] = {0};
86 const unsigned char ones
[16] =
87 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
88 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
90 static const char *state_file
= DAEMON_VTY_DIR
"/babel-state";
92 unsigned char protocol_group
[16]; /* babel's link-local multicast address */
93 int protocol_port
; /* babel's port */
94 int protocol_socket
= -1; /* socket: communicate with others babeld */
96 static char babel_config_default
[] = SYSCONFDIR BABEL_DEFAULT_CONFIG
;
97 static char *babel_config_file
= NULL
;
98 static char *babel_vty_addr
= NULL
;
99 static int babel_vty_port
= BABEL_VTY_PORT
;
101 /* Babeld options. */
102 struct option longopts
[] =
104 { "daemon", no_argument
, NULL
, 'd'},
105 { "config_file", required_argument
, NULL
, 'f'},
106 { "pid_file", required_argument
, NULL
, 'i'},
107 { "socket", required_argument
, NULL
, 'z'},
108 { "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'},
117 /* babeld privileges */
118 static zebra_capabilities_t _caps_p
[] =
123 static struct zebra_privs_t babeld_privs
=
125 #if defined(QUAGGA_USER)
128 #if defined QUAGGA_GROUP
129 .group
= QUAGGA_GROUP
,
132 .vty_group
= VTY_GROUP
,
141 main(int argc
, char **argv
)
143 struct thread thread
;
144 /* and print banner too */
145 babel_init(argc
, argv
);
146 while (thread_fetch (master
, &thread
)) {
147 thread_call (&thread
);
153 babel_usage (char *progname
, int status
)
156 fprintf (stderr
, "Try `%s --help' for more information.\n", progname
);
159 printf ("Usage : %s [OPTION...]\n\
160 Daemon 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\
164 -z, --socket Set path of zebra socket\n\
165 -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\
172 Report bugs to %s\n", progname
, ZEBRA_BUG_ADDRESS
);
177 /* make initialisations witch don't need infos about kernel(interfaces, etc.) */
179 babel_init(int argc
, char **argv
)
182 int do_daemonise
= 0;
183 char *progname
= NULL
;
185 /* Set umask before anything for security */
187 progname
= babel_get_progname(argv
[0]);
189 /* set default log (lib/log.h) */
190 zlog_default
= openzlog(progname
, ZLOG_BABEL
, 0,
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
);
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;
203 opt
= getopt_long(argc
, argv
, "df:i:z:hA:P:u:g:v", longopts
, 0);
214 babel_config_file
= optarg
;
220 zclient_serv_path_set (optarg
);
223 babel_vty_addr
= optarg
;
226 babel_vty_port
= atoi (optarg
);
227 if (babel_vty_port
<= 0 || babel_vty_port
> 0xffff)
228 babel_vty_port
= BABEL_VTY_PORT
;
231 babeld_privs
.user
= optarg
;
234 babeld_privs
.group
= optarg
;
237 print_version (progname
);
241 babel_usage (progname
, 0);
244 babel_usage (progname
, 1);
249 /* create the threads handler */
250 master
= thread_master_create ();
253 zprivs_init (&babeld_privs
);
254 babel_init_signals();
259 resend_delay
= BABEL_DEFAULT_RESEND_DELAY
;
261 babel_replace_by_null(STDIN_FILENO
);
263 if (do_daemonise
&& daemonise() < 0) {
264 zlog_err("daemonise: %s", safe_strerror(errno
));
269 if (pid_output(pidfile
) < 0) {
270 zlog_err("error while writing pidfile");
274 /* init some quagga's dependencies, and babeld's commands */
275 babeld_quagga_init();
276 /* init zebra client's structure and it's commands */
277 /* this replace kernel_setup && kernel_setup_socket */
278 babelz_zebra_init(master
);
280 /* Get zebra configuration file. */
281 zlog_set_level (NULL
, ZLOG_DEST_STDOUT
, ZLOG_DISABLED
);
282 vty_read_config (babel_config_file
, babel_config_default
);
284 /* Create VTY socket */
285 vty_serv_sock (babel_vty_addr
, babel_vty_port
, BABEL_VTYSH_PATH
);
288 rc
= resize_receive_buffer(1500);
292 schedule_neighbours_check(5000, 1);
294 zlog_notice ("BABELd %s starting: vty@%d", BABEL_VERSION
, babel_vty_port
);
297 /* return the progname (without path, example: "./x/progname" --> "progname") */
299 babel_get_progname(char *argv_0
) {
300 char *p
= strrchr (argv_0
, '/');
301 return (p
? ++p
: argv_0
);
310 /* initialize random value, and set 'babel_now' by the way. */
312 babel_init_random(void)
318 rc
= read_random_bytes(&seed
, sizeof(seed
));
320 zlog_err("read(random): %s", safe_strerror(errno
));
324 seed
^= (babel_now
.tv_sec
^ babel_now
.tv_usec
);
329 close fd, and replace it by "/dev/null"
333 babel_replace_by_null(int fd
)
338 fd_null
= open("/dev/null", O_RDONLY
);
340 zlog_err("open(null): %s", safe_strerror(errno
));
344 rc
= dup2(fd_null
, fd
);
346 zlog_err("dup2(null, 0): %s", safe_strerror(errno
));
354 Load the state file: check last babeld's running state, usefull in case of
355 "/etc/init.d/babeld restart"
358 babel_load_state_file(void)
363 fd
= open(state_file
, O_RDONLY
);
364 if(fd
< 0 && errno
!= ENOENT
)
365 zlog_err("open(babel-state: %s)", safe_strerror(errno
));
366 rc
= unlink(state_file
);
367 if(fd
>= 0 && rc
< 0) {
368 zlog_err("unlink(babel-state): %s", safe_strerror(errno
));
369 /* If we couldn't unlink it, it's probably stale. */
378 rc
= read(fd
, buf
, 99);
380 zlog_err("read(babel-state): %s", safe_strerror(errno
));
383 rc
= sscanf(buf
, "%99s %d %ld\n", buf2
, &s
, &t
);
384 if(rc
== 3 && s
>= 0 && s
<= 0xFFFF) {
385 unsigned char sid
[8];
386 rc
= parse_eui64(buf2
, sid
);
388 zlog_err("Couldn't parse babel-state.");
390 struct timeval realnow
;
391 debugf(BABEL_DEBUG_COMMON
,
392 "Got %s %d %ld from babel-state.",
393 format_eui64(sid
), s
, t
);
394 gettimeofday(&realnow
, NULL
);
395 if(memcmp(sid
, myid
, 8) == 0)
396 myseqno
= seqno_plus(s
, 1);
398 zlog_err("ID mismatch in babel-state. id=%s; old=%s",
403 zlog_err("Couldn't parse babel-state.");
414 zlog_notice("Terminating on signal");
416 babel_exit_properly();
426 babel_init_signals(void)
428 static struct quagga_signal_t babel_signals
[] =
432 .handler
= &babel_sigusr1
,
436 .handler
= &babel_sigexit
,
440 .handler
= &babel_sigexit
,
444 signal_init (master
, array_size(babel_signals
), babel_signals
);
448 babel_exit_properly(void)
450 debugf(BABEL_DEBUG_COMMON
, "Exiting...");
451 usleep(roughly(10000));
454 /* Uninstall and flush all routes. */
455 debugf(BABEL_DEBUG_COMMON
, "Uninstall routes.");
457 babel_interface_close_all();
458 babel_zebra_close_connexion();
459 babel_save_state_file();
460 debugf(BABEL_DEBUG_COMMON
, "Remove pid file.");
463 debugf(BABEL_DEBUG_COMMON
, "Done.");
469 babel_save_state_file(void)
474 debugf(BABEL_DEBUG_COMMON
, "Save state file.");
475 fd
= open(state_file
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0644);
477 zlog_err("creat(babel-state): %s", safe_strerror(errno
));
480 struct timeval realnow
;
482 gettimeofday(&realnow
, NULL
);
483 rc
= snprintf(buf
, 100, "%s %d %ld\n",
484 format_eui64(myid
), (int)myseqno
,
485 (long)realnow
.tv_sec
);
486 if(rc
< 0 || rc
>= 100) {
487 zlog_err("write(babel-state): overflow.");
490 rc
= write(fd
, buf
, rc
);
492 zlog_err("write(babel-state): %s", safe_strerror(errno
));
502 show_babel_main_configuration (struct vty
*vty
)
507 "configuration file = %s%s"
508 "protocol informations:%s"
509 " multicast address = %s%s"
514 "allow_duplicates = %s%s"
515 "kernel_metric = %d%s",
516 pidfile
, VTY_NEWLINE
,
517 state_file
, VTY_NEWLINE
,
518 babel_config_file
? babel_config_file
: babel_config_default
,
521 format_address(protocol_group
), VTY_NEWLINE
,
522 protocol_port
, VTY_NEWLINE
,
523 babel_vty_addr
? babel_vty_addr
: "None",
525 babel_vty_port
, VTY_NEWLINE
,
526 format_eui64(myid
), VTY_NEWLINE
,
527 format_bool(allow_duplicates
), VTY_NEWLINE
,
528 kernel_metric
, VTY_NEWLINE
);