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
46 #include "distribute.h"
51 #include "babel_main.h"
56 #include "babel_interface.h"
57 #include "neighbour.h"
61 #include "babel_filter.h"
62 #include "babel_zebra.h"
65 static int babel_init_routing_process(struct thread
*thread
);
66 static void babel_get_myid(void);
67 static void babel_initial_noise(void);
68 static int babel_read_protocol (struct thread
*thread
);
69 static int babel_main_loop(struct thread
*thread
);
70 static void babel_set_timer(struct timeval
*timeout
);
71 static void babel_fill_with_next_timeout(struct timeval
*tv
);
74 /* Informations relative to the babel running daemon. */
75 static struct babel
*babel_routing_process
= NULL
;
76 static unsigned char *receive_buffer
= NULL
;
77 static int receive_buffer_size
= 0;
80 struct timeval check_neighbours_timeout
;
81 static time_t expiry_time
;
82 static time_t source_expiry_time
;
84 /* Babel node structure. */
85 static struct cmd_node cmd_babel_node
=
88 .prompt
= "%s(config-router)# ",
92 /* print current babel configuration on vty */
94 babel_config_write (struct vty
*vty
)
99 /* list enabled debug modes */
100 lines
+= debug_babel_config_write (vty
);
102 if (!babel_routing_process
)
104 vty_out (vty
, "router babel%s", VTY_NEWLINE
);
105 /* list enabled interfaces */
106 lines
= 1 + babel_enable_if_config_write (vty
);
107 /* list redistributed protocols */
108 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
109 if (i
!= zclient
->redist_default
&& zclient
->redist
[i
])
111 vty_out (vty
, " redistribute %s%s", zebra_route_string (i
), VTY_NEWLINE
);
120 babel_create_routing_process (void)
122 assert (babel_routing_process
== NULL
);
124 /* Allocaste Babel instance. */
125 babel_routing_process
= XCALLOC (MTYPE_BABEL
, sizeof (struct babel
));
127 /* Initialize timeouts */
129 expiry_time
= babel_now
.tv_sec
+ roughly(30);
130 source_expiry_time
= babel_now
.tv_sec
+ roughly(300);
132 /* Make socket for Babel protocol. */
133 protocol_socket
= babel_socket(protocol_port
);
134 if (protocol_socket
< 0) {
135 zlog_err("Couldn't create link local socket: %s", safe_strerror(errno
));
140 babel_routing_process
->t_read
=
141 thread_add_read(master
, &babel_read_protocol
, NULL
, protocol_socket
);
142 /* wait a little: zebra will announce interfaces, addresses, routes... */
143 babel_routing_process
->t_update
=
144 thread_add_timer_msec(master
, &babel_init_routing_process
, NULL
, 200L);
148 XFREE(MTYPE_BABEL
, babel_routing_process
);
149 babel_routing_process
= NULL
;
153 /* thread reading entries form others babel daemons */
155 babel_read_protocol (struct thread
*thread
)
158 struct interface
*ifp
= NULL
;
159 struct sockaddr_in6 sin6
;
160 struct listnode
*linklist_node
= NULL
;
162 assert(babel_routing_process
!= NULL
);
163 assert(protocol_socket
>= 0);
165 rc
= babel_recv(protocol_socket
,
166 receive_buffer
, receive_buffer_size
,
167 (struct sockaddr
*)&sin6
, sizeof(sin6
));
169 if(errno
!= EAGAIN
&& errno
!= EINTR
) {
170 zlog_err("recv: %s", safe_strerror(errno
));
173 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
176 if(ifp
->ifindex
== sin6
.sin6_scope_id
) {
177 parse_packet((unsigned char*)&sin6
.sin6_addr
, ifp
,
185 babel_routing_process
->t_read
=
186 thread_add_read(master
, &babel_read_protocol
, NULL
, protocol_socket
);
190 /* Zebra will give some information, especially about interfaces. This function
191 must be call with a litte timeout wich may give zebra the time to do his job,
192 making these inits have sense. */
194 babel_init_routing_process(struct thread
*thread
)
196 myseqno
= (random() & 0xFFFF);
198 babel_load_state_file();
199 debugf(BABEL_DEBUG_COMMON
, "My ID is : %s.", format_eui64(myid
));
200 babel_initial_noise();
201 babel_main_loop(thread
);/* this function self-add to the t_update thread */
205 /* fill "myid" with an unique id (only if myid != {0}). */
209 struct interface
*ifp
= NULL
;
210 struct listnode
*linklist_node
= NULL
;
214 /* if we already have an id (from state file), we return. */
215 if (memcmp(myid
, zeroes
, 8) != 0) {
219 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
220 /* ifp->ifindex is not necessarily valid at this point */
221 int ifindex
= if_nametoindex(ifp
->name
);
223 unsigned char eui
[8];
224 rc
= if_eui64(ifp
->name
, ifindex
, eui
);
227 memcpy(myid
, eui
, 8);
232 /* We failed to get a global EUI64 from the interfaces we were given.
233 Let's try to find an interface with a MAC address. */
234 for(i
= 1; i
< 256; i
++) {
235 char buf
[IF_NAMESIZE
], *ifname
;
236 unsigned char eui
[8];
237 ifname
= if_indextoname(i
, buf
);
240 rc
= if_eui64(ifname
, i
, eui
);
243 memcpy(myid
, eui
, 8);
247 zlog_err("Warning: couldn't find router id -- using random value.");
249 rc
= read_random_bytes(myid
, 8);
251 zlog_err("read(random): %s (cannot assign an ID)",safe_strerror(errno
));
254 /* Clear group and global bits */
258 /* Make some noise so that others notice us, and send retractions in
259 case we were restarted recently */
261 babel_initial_noise(void)
263 struct interface
*ifp
= NULL
;
264 struct listnode
*linklist_node
= NULL
;
266 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
269 /* Apply jitter before we send the first message. */
270 usleep(roughly(10000));
273 send_wildcard_retraction(ifp
);
276 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
279 usleep(roughly(10000));
282 send_wildcard_retraction(ifp
);
283 send_self_update(ifp
);
284 send_request(ifp
, NULL
, 0);
290 /* Delete all the added babel routes, make babeld only speak to zebra. */
292 babel_clean_routing_process()
295 babel_interface_close_all();
298 if (babel_routing_process
->t_read
!= NULL
) {
299 thread_cancel(babel_routing_process
->t_read
);
301 if (babel_routing_process
->t_update
!= NULL
) {
302 thread_cancel(babel_routing_process
->t_update
);
305 XFREE(MTYPE_BABEL
, babel_routing_process
);
306 babel_routing_process
= NULL
;
309 /* Function used with timeout. */
311 babel_main_loop(struct thread
*thread
)
314 struct interface
*ifp
= NULL
;
315 struct listnode
*linklist_node
= NULL
;
320 /* timeouts --------------------------------------------------------- */
321 /* get the next timeout */
322 babel_fill_with_next_timeout(&tv
);
323 /* if there is no timeout, we must wait. */
324 if(timeval_compare(&tv
, &babel_now
) > 0) {
325 timeval_minus(&tv
, &tv
, &babel_now
);
326 debugf(BABEL_DEBUG_TIMEOUT
, "babel main loop : timeout: %ld msecs",
327 tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000);
328 /* it happens often to have less than 1 ms, it's bad. */
329 timeval_add_msec(&tv
, &tv
, 300);
330 babel_set_timer(&tv
);
336 /* update database -------------------------------------------------- */
337 if(timeval_compare(&check_neighbours_timeout
, &babel_now
) < 0) {
339 msecs
= check_neighbours();
340 msecs
= MAX(msecs
, 10);
341 schedule_neighbours_check(msecs
, 1);
344 if(babel_now
.tv_sec
>= expiry_time
) {
347 expiry_time
= babel_now
.tv_sec
+ roughly(30);
350 if(babel_now
.tv_sec
>= source_expiry_time
) {
352 source_expiry_time
= babel_now
.tv_sec
+ roughly(300);
355 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
356 babel_interface_nfo
*babel_ifp
= NULL
;
359 babel_ifp
= babel_get_if_nfo(ifp
);
360 if(timeval_compare(&babel_now
, &babel_ifp
->hello_timeout
) >= 0)
362 if(timeval_compare(&babel_now
, &babel_ifp
->update_timeout
) >= 0)
363 send_update(ifp
, 0, NULL
, 0);
364 if(timeval_compare(&babel_now
,
365 &babel_ifp
->update_flush_timeout
) >= 0)
369 if(resend_time
.tv_sec
!= 0) {
370 if(timeval_compare(&babel_now
, &resend_time
) >= 0)
374 if(unicast_flush_timeout
.tv_sec
!= 0) {
375 if(timeval_compare(&babel_now
, &unicast_flush_timeout
) >= 0)
379 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
380 babel_interface_nfo
*babel_ifp
= NULL
;
383 babel_ifp
= babel_get_if_nfo(ifp
);
384 if(babel_ifp
->flush_timeout
.tv_sec
!= 0) {
385 if(timeval_compare(&babel_now
, &babel_ifp
->flush_timeout
) >= 0)
391 assert(0); /* this line should never be reach */
395 printIfMin(struct timeval
*tv
, int cmd
, const char *tag
, const char *ifname
)
397 static struct timeval curr_tv
;
398 static char buffer
[200];
399 static const char *curr_tag
= NULL
;
402 case 0: /* reset timeval */
405 snprintf(buffer
, 200L, "interface: %s; %s", ifname
, tag
);
411 case 1: /* take the min */
412 if (tv
->tv_sec
== 0 && tv
->tv_usec
== 0) { /* if (tv == ∞) */
415 if (tv
->tv_sec
< curr_tv
.tv_sec
||(tv
->tv_sec
== curr_tv
.tv_sec
&&
416 tv
->tv_usec
< curr_tv
.tv_usec
)) {
419 snprintf(buffer
, 200L, "interface: %s; %s", ifname
, tag
);
426 case 2: /* print message */
427 debugf(BABEL_DEBUG_TIMEOUT
, "next timeout due to: %s", curr_tag
);
435 babel_fill_with_next_timeout(struct timeval
*tv
)
437 #if (defined NO_DEBUG)
438 #define printIfMin(a,b,c,d)
440 #define printIfMin(a,b,c,d) \
441 if (UNLIKELY(debug & BABEL_DEBUG_TIMEOUT)) {printIfMin(a,b,c,d);}
443 struct interface
*ifp
= NULL
;
444 struct listnode
*linklist_node
= NULL
;
446 *tv
= check_neighbours_timeout
;
447 printIfMin(tv
, 0, "check_neighbours_timeout", NULL
);
448 timeval_min_sec(tv
, expiry_time
);
449 printIfMin(tv
, 1, "expiry_time", NULL
);
450 timeval_min_sec(tv
, source_expiry_time
);
451 printIfMin(tv
, 1, "source_expiry_time", NULL
);
452 timeval_min(tv
, &resend_time
);
453 printIfMin(tv
, 1, "resend_time", NULL
);
454 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
455 babel_interface_nfo
*babel_ifp
= NULL
;
458 babel_ifp
= babel_get_if_nfo(ifp
);
459 timeval_min(tv
, &babel_ifp
->flush_timeout
);
460 printIfMin(tv
, 1, "flush_timeout", ifp
->name
);
461 timeval_min(tv
, &babel_ifp
->hello_timeout
);
462 printIfMin(tv
, 1, "hello_timeout", ifp
->name
);
463 timeval_min(tv
, &babel_ifp
->update_timeout
);
464 printIfMin(tv
, 1, "update_timeout", ifp
->name
);
465 timeval_min(tv
, &babel_ifp
->update_flush_timeout
);
466 printIfMin(tv
, 1, "update_flush_timeout",ifp
->name
);
468 timeval_min(tv
, &unicast_flush_timeout
);
469 printIfMin(tv
, 1, "unicast_flush_timeout", NULL
);
470 printIfMin(tv
, 2, NULL
, NULL
);
475 /* set the t_update thread of the babel routing process to be launch in
476 'timeout' (approximate at the milisecond) */
478 babel_set_timer(struct timeval
*timeout
)
480 long msecs
= timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000;
481 if (babel_routing_process
->t_update
!= NULL
) {
482 thread_cancel(babel_routing_process
->t_update
);
484 babel_routing_process
->t_update
=
485 thread_add_timer_msec(master
, &babel_main_loop
, NULL
, msecs
);
488 /* Schedule a neighbours check after roughly 3/2 times msecs have elapsed. */
490 schedule_neighbours_check(int msecs
, int override
)
492 struct timeval timeout
;
494 timeval_add_msec(&timeout
, &babel_now
, roughly(msecs
* 3 / 2));
496 check_neighbours_timeout
= timeout
;
498 timeval_min(&check_neighbours_timeout
, &timeout
);
502 resize_receive_buffer(int size
)
504 if(size
<= receive_buffer_size
)
507 if(receive_buffer
== NULL
) {
508 receive_buffer
= malloc(size
);
509 if(receive_buffer
== NULL
) {
510 zlog_err("malloc(receive_buffer): %s", safe_strerror(errno
));
513 receive_buffer_size
= size
;
516 new = realloc(receive_buffer
, size
);
518 zlog_err("realloc(receive_buffer): %s", safe_strerror(errno
));
521 receive_buffer
= new;
522 receive_buffer_size
= size
;
528 babel_distribute_update (struct distribute
*dist
)
530 struct interface
*ifp
;
531 babel_interface_nfo
*babel_ifp
;
532 struct access_list
*alist
;
533 struct prefix_list
*plist
;
538 ifp
= if_lookup_by_name (dist
->ifname
);
542 babel_ifp
= babel_get_if_nfo(ifp
);
544 if (dist
->list
[DISTRIBUTE_IN
]) {
545 alist
= access_list_lookup (AFI_IP6
, dist
->list
[DISTRIBUTE_IN
]);
547 babel_ifp
->list
[BABEL_FILTER_IN
] = alist
;
549 babel_ifp
->list
[BABEL_FILTER_IN
] = NULL
;
551 babel_ifp
->list
[BABEL_FILTER_IN
] = NULL
;
554 if (dist
->list
[DISTRIBUTE_OUT
]) {
555 alist
= access_list_lookup (AFI_IP6
, dist
->list
[DISTRIBUTE_OUT
]);
557 babel_ifp
->list
[BABEL_FILTER_OUT
] = alist
;
559 babel_ifp
->list
[BABEL_FILTER_OUT
] = NULL
;
561 babel_ifp
->list
[BABEL_FILTER_OUT
] = NULL
;
564 if (dist
->prefix
[DISTRIBUTE_IN
]) {
565 plist
= prefix_list_lookup (AFI_IP6
, dist
->prefix
[DISTRIBUTE_IN
]);
567 babel_ifp
->prefix
[BABEL_FILTER_IN
] = plist
;
569 babel_ifp
->prefix
[BABEL_FILTER_IN
] = NULL
;
571 babel_ifp
->prefix
[BABEL_FILTER_IN
] = NULL
;
574 if (dist
->prefix
[DISTRIBUTE_OUT
]) {
575 plist
= prefix_list_lookup (AFI_IP6
, dist
->prefix
[DISTRIBUTE_OUT
]);
577 babel_ifp
->prefix
[BABEL_FILTER_OUT
] = plist
;
579 babel_ifp
->prefix
[BABEL_FILTER_OUT
] = NULL
;
581 babel_ifp
->prefix
[BABEL_FILTER_OUT
] = NULL
;
586 babel_distribute_update_interface (struct interface
*ifp
)
588 struct distribute
*dist
;
590 dist
= distribute_lookup (ifp
->name
);
592 babel_distribute_update (dist
);
595 /* Update all interface's distribute list. */
597 babel_distribute_update_all (struct prefix_list
*notused
)
599 struct interface
*ifp
;
600 struct listnode
*node
;
602 for (ALL_LIST_ELEMENTS_RO (iflist
, node
, ifp
))
603 babel_distribute_update_interface (ifp
);
607 babel_distribute_update_all_wrapper (struct access_list
*notused
)
609 babel_distribute_update_all(NULL
);
617 "Enable a routing process\n"
618 "Make Babel instance command\n"
623 vty
->node
= BABEL_NODE
;
625 if (!babel_routing_process
) {
626 ret
= babel_create_routing_process ();
628 /* Notice to user we couldn't create Babel. */
630 zlog_warn ("can't create Babel");
639 DEFUN (no_router_babel
,
643 "Disable a routing process\n"
644 "Remove Babel instance command\n"
647 if(babel_routing_process
)
648 babel_clean_routing_process();
652 /* [Babel Command] */
653 DEFUN (babel_set_resend_delay
,
654 babel_set_resend_delay_cmd
,
655 "babel resend-delay <20-655340>",
657 "Time before resending a message\n"
662 VTY_GET_INTEGER_RANGE("milliseconds", interval
, argv
[0], 20, 10 * 0xFFFE);
664 resend_delay
= interval
;
669 babeld_quagga_init(void)
672 install_node(&cmd_babel_node
, &babel_config_write
);
674 install_element(CONFIG_NODE
, &router_babel_cmd
);
675 install_element(CONFIG_NODE
, &no_router_babel_cmd
);
677 install_default(BABEL_NODE
);
678 install_element(BABEL_NODE
, &babel_set_resend_delay_cmd
);
682 /* Access list install. */
684 access_list_add_hook (babel_distribute_update_all_wrapper
);
685 access_list_delete_hook (babel_distribute_update_all_wrapper
);
687 /* Prefix list initialize.*/
689 prefix_list_add_hook (babel_distribute_update_all
);
690 prefix_list_delete_hook (babel_distribute_update_all
);
692 /* Distribute list install. */
693 distribute_list_init (BABEL_NODE
);
694 distribute_list_add_hook (babel_distribute_update
);
695 distribute_list_delete_hook (babel_distribute_update
);
698 /* Stubs to adapt Babel's filtering calls to Quagga's infrastructure. */
701 input_filter(const unsigned char *id
,
702 const unsigned char *prefix
, unsigned short plen
,
703 const unsigned char *neigh
, unsigned int ifindex
)
705 return babel_filter(0, prefix
, plen
, ifindex
);
709 output_filter(const unsigned char *id
, const unsigned char *prefix
,
710 unsigned short plen
, unsigned int ifindex
)
712 return babel_filter(1, prefix
, plen
, ifindex
);
715 /* There's no redistribute filter in Quagga -- the zebra daemon does its
718 redistribute_filter(const unsigned char *prefix
, unsigned short plen
,
719 unsigned int ifindex
, int proto
)