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