2 Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #include "distribute.h"
32 #include "lib_errors.h"
34 #include "babel_main.h"
39 #include "babel_interface.h"
40 #include "neighbour.h"
44 #include "babel_filter.h"
45 #include "babel_zebra.h"
46 #include "babel_memory.h"
47 #include "babel_errors.h"
49 static int babel_init_routing_process(struct thread
*thread
);
50 static void babel_get_myid(void);
51 static void babel_initial_noise(void);
52 static int babel_read_protocol (struct thread
*thread
);
53 static int babel_main_loop(struct thread
*thread
);
54 static void babel_set_timer(struct timeval
*timeout
);
55 static void babel_fill_with_next_timeout(struct timeval
*tv
);
58 /* Informations relative to the babel running daemon. */
59 static struct babel
*babel_routing_process
= NULL
;
60 static unsigned char *receive_buffer
= NULL
;
61 static int receive_buffer_size
= 0;
64 struct timeval check_neighbours_timeout
;
65 static time_t expiry_time
;
66 static time_t source_expiry_time
;
68 /* Babel node structure. */
69 static struct cmd_node cmd_babel_node
=
72 .prompt
= "%s(config-router)# ",
76 /* print current babel configuration on vty */
78 babel_config_write (struct vty
*vty
)
84 /* list enabled debug modes */
85 lines
+= debug_babel_config_write (vty
);
87 if (!babel_routing_process
)
89 vty_out (vty
, "router babel\n");
90 if (diversity_kind
!= DIVERSITY_NONE
)
92 vty_out (vty
, " babel diversity\n");
95 if (diversity_factor
!= BABEL_DEFAULT_DIVERSITY_FACTOR
)
97 vty_out (vty
, " babel diversity-factor %d\n",diversity_factor
);
100 if (resend_delay
!= BABEL_DEFAULT_RESEND_DELAY
)
102 vty_out (vty
, " babel resend-delay %u\n", resend_delay
);
105 if (smoothing_half_life
!= BABEL_DEFAULT_SMOOTHING_HALF_LIFE
)
107 vty_out (vty
, " babel smoothing-half-life %u\n",
108 smoothing_half_life
);
111 /* list enabled interfaces */
112 lines
= 1 + babel_enable_if_config_write (vty
);
113 /* list redistributed protocols */
114 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
115 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
116 if (i
!= zclient
->redist_default
&&
117 vrf_bitmap_check (zclient
->redist
[afi
][i
], VRF_DEFAULT
)) {
118 vty_out (vty
, " redistribute %s %s\n",
119 (afi
== AFI_IP
) ? "ipv4" : "ipv6",
120 zebra_route_string(i
));
126 lines
+= config_write_distribute (vty
);
133 babel_create_routing_process (void)
135 assert (babel_routing_process
== NULL
);
137 /* Allocaste Babel instance. */
138 babel_routing_process
= XCALLOC (MTYPE_BABEL
, sizeof (struct babel
));
140 /* Initialize timeouts */
142 expiry_time
= babel_now
.tv_sec
+ roughly(30);
143 source_expiry_time
= babel_now
.tv_sec
+ roughly(300);
145 /* Make socket for Babel protocol. */
146 protocol_socket
= babel_socket(protocol_port
);
147 if (protocol_socket
< 0) {
148 flog_err_sys(LIB_ERR_SOCKET
, "Couldn't create link local socket: %s",
149 safe_strerror(errno
));
154 thread_add_read(master
, &babel_read_protocol
, NULL
, protocol_socket
, &babel_routing_process
->t_read
);
155 /* wait a little: zebra will announce interfaces, addresses, routes... */
156 thread_add_timer_msec(master
, babel_init_routing_process
, NULL
, 200L, &babel_routing_process
->t_update
);
160 XFREE(MTYPE_BABEL
, babel_routing_process
);
161 babel_routing_process
= NULL
;
165 /* thread reading entries form others babel daemons */
167 babel_read_protocol (struct thread
*thread
)
170 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
171 struct interface
*ifp
= NULL
;
172 struct sockaddr_in6 sin6
;
174 assert(babel_routing_process
!= NULL
);
175 assert(protocol_socket
>= 0);
177 rc
= babel_recv(protocol_socket
,
178 receive_buffer
, receive_buffer_size
,
179 (struct sockaddr
*)&sin6
, sizeof(sin6
));
181 if(errno
!= EAGAIN
&& errno
!= EINTR
) {
182 flog_err_sys(LIB_ERR_SOCKET
, "recv: %s", safe_strerror(errno
));
185 FOR_ALL_INTERFACES(vrf
, ifp
) {
188 if(ifp
->ifindex
== (ifindex_t
)sin6
.sin6_scope_id
) {
189 parse_packet((unsigned char*)&sin6
.sin6_addr
, ifp
,
197 thread_add_read(master
, &babel_read_protocol
, NULL
, protocol_socket
, &babel_routing_process
->t_read
);
201 /* Zebra will give some information, especially about interfaces. This function
202 must be call with a litte timeout wich may give zebra the time to do his job,
203 making these inits have sense. */
205 babel_init_routing_process(struct thread
*thread
)
207 myseqno
= (random() & 0xFFFF);
209 babel_load_state_file();
210 debugf(BABEL_DEBUG_COMMON
, "My ID is : %s.", format_eui64(myid
));
211 babel_initial_noise();
212 babel_main_loop(thread
);/* this function self-add to the t_update thread */
216 /* fill "myid" with an unique id (only if myid != {0}). */
220 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
221 struct interface
*ifp
= NULL
;
225 /* if we already have an id (from state file), we return. */
226 if (memcmp(myid
, zeroes
, 8) != 0) {
230 FOR_ALL_INTERFACES(vrf
, ifp
) {
231 /* ifp->ifindex is not necessarily valid at this point */
232 int ifindex
= if_nametoindex(ifp
->name
);
234 unsigned char eui
[8];
235 rc
= if_eui64(ifindex
, eui
);
238 memcpy(myid
, eui
, 8);
243 /* We failed to get a global EUI64 from the interfaces we were given.
244 Let's try to find an interface with a MAC address. */
245 for(i
= 1; i
< 256; i
++) {
246 char buf
[IF_NAMESIZE
], *ifname
;
247 unsigned char eui
[8];
248 ifname
= if_indextoname(i
, buf
);
251 rc
= if_eui64(i
, eui
);
254 memcpy(myid
, eui
, 8);
258 flog_err(BABEL_ERR_CONFIG
,
259 "Warning: couldn't find router id -- using random value.");
261 rc
= read_random_bytes(myid
, 8);
263 flog_err(BABEL_ERR_CONFIG
, "read(random): %s (cannot assign an ID)",
264 safe_strerror(errno
));
267 /* Clear group and global bits */
271 /* Make some noise so that others notice us, and send retractions in
272 case we were restarted recently */
274 babel_initial_noise(void)
276 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
277 struct interface
*ifp
= NULL
;
279 FOR_ALL_INTERFACES(vrf
, ifp
) {
282 /* Apply jitter before we send the first message. */
283 usleep(roughly(10000));
286 send_wildcard_retraction(ifp
);
289 FOR_ALL_INTERFACES(vrf
, ifp
) {
292 usleep(roughly(10000));
295 send_wildcard_retraction(ifp
);
296 send_self_update(ifp
);
297 send_request(ifp
, NULL
, 0);
303 /* Delete all the added babel routes, make babeld only speak to zebra. */
305 babel_clean_routing_process()
308 babel_interface_close_all();
311 if (babel_routing_process
->t_read
!= NULL
) {
312 thread_cancel(babel_routing_process
->t_read
);
314 if (babel_routing_process
->t_update
!= NULL
) {
315 thread_cancel(babel_routing_process
->t_update
);
318 XFREE(MTYPE_BABEL
, babel_routing_process
);
319 babel_routing_process
= NULL
;
322 /* Function used with timeout. */
324 babel_main_loop(struct thread
*thread
)
327 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
328 struct interface
*ifp
= NULL
;
333 /* timeouts --------------------------------------------------------- */
334 /* get the next timeout */
335 babel_fill_with_next_timeout(&tv
);
336 /* if there is no timeout, we must wait. */
337 if(timeval_compare(&tv
, &babel_now
) > 0) {
338 timeval_minus(&tv
, &tv
, &babel_now
);
339 debugf(BABEL_DEBUG_TIMEOUT
, "babel main loop : timeout: %lld msecs",
340 (long long)tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000);
341 /* it happens often to have less than 1 ms, it's bad. */
342 timeval_add_msec(&tv
, &tv
, 300);
343 babel_set_timer(&tv
);
349 /* update database -------------------------------------------------- */
350 if(timeval_compare(&check_neighbours_timeout
, &babel_now
) < 0) {
352 msecs
= check_neighbours();
353 /* Multiply by 3/2 to allow neighbours to expire. */
354 msecs
= MAX(3 * msecs
/ 2, 10);
355 schedule_neighbours_check(msecs
, 1);
358 if(babel_now
.tv_sec
>= expiry_time
) {
361 expiry_time
= babel_now
.tv_sec
+ roughly(30);
364 if(babel_now
.tv_sec
>= source_expiry_time
) {
366 source_expiry_time
= babel_now
.tv_sec
+ roughly(300);
369 FOR_ALL_INTERFACES(vrf
, ifp
) {
370 babel_interface_nfo
*babel_ifp
= NULL
;
373 babel_ifp
= babel_get_if_nfo(ifp
);
374 if(timeval_compare(&babel_now
, &babel_ifp
->hello_timeout
) >= 0)
376 if(timeval_compare(&babel_now
, &babel_ifp
->update_timeout
) >= 0)
377 send_update(ifp
, 0, NULL
, 0);
378 if(timeval_compare(&babel_now
,
379 &babel_ifp
->update_flush_timeout
) >= 0)
383 if(resend_time
.tv_sec
!= 0) {
384 if(timeval_compare(&babel_now
, &resend_time
) >= 0)
388 if(unicast_flush_timeout
.tv_sec
!= 0) {
389 if(timeval_compare(&babel_now
, &unicast_flush_timeout
) >= 0)
393 FOR_ALL_INTERFACES(vrf
, ifp
) {
394 babel_interface_nfo
*babel_ifp
= NULL
;
397 babel_ifp
= babel_get_if_nfo(ifp
);
398 if(babel_ifp
->flush_timeout
.tv_sec
!= 0) {
399 if(timeval_compare(&babel_now
, &babel_ifp
->flush_timeout
) >= 0)
405 assert(0); /* this line should never be reach */
409 printIfMin(struct timeval
*tv
, int cmd
, const char *tag
, const char *ifname
)
411 static struct timeval curr_tv
;
412 static char buffer
[200];
413 static const char *curr_tag
= NULL
;
416 case 0: /* reset timeval */
419 snprintf(buffer
, 200L, "interface: %s; %s", ifname
, tag
);
425 case 1: /* take the min */
426 if (tv
->tv_sec
== 0 && tv
->tv_usec
== 0) { /* if (tv == ∞) */
429 if (tv
->tv_sec
< curr_tv
.tv_sec
||(tv
->tv_sec
== curr_tv
.tv_sec
&&
430 tv
->tv_usec
< curr_tv
.tv_usec
)) {
433 snprintf(buffer
, 200L, "interface: %s; %s", ifname
, tag
);
440 case 2: /* print message */
441 debugf(BABEL_DEBUG_TIMEOUT
, "next timeout due to: %s", curr_tag
);
449 babel_fill_with_next_timeout(struct timeval
*tv
)
451 #if (defined NO_DEBUG)
452 #define printIfMin(a,b,c,d)
454 #define printIfMin(a,b,c,d) \
455 if (UNLIKELY(debug & BABEL_DEBUG_TIMEOUT)) {printIfMin(a,b,c,d);}
457 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
458 struct interface
*ifp
= NULL
;
460 *tv
= check_neighbours_timeout
;
461 printIfMin(tv
, 0, "check_neighbours_timeout", NULL
);
462 timeval_min_sec(tv
, expiry_time
);
463 printIfMin(tv
, 1, "expiry_time", NULL
);
464 timeval_min_sec(tv
, source_expiry_time
);
465 printIfMin(tv
, 1, "source_expiry_time", NULL
);
466 timeval_min(tv
, &resend_time
);
467 printIfMin(tv
, 1, "resend_time", NULL
);
468 FOR_ALL_INTERFACES(vrf
, ifp
) {
469 babel_interface_nfo
*babel_ifp
= NULL
;
472 babel_ifp
= babel_get_if_nfo(ifp
);
473 timeval_min(tv
, &babel_ifp
->flush_timeout
);
474 printIfMin(tv
, 1, "flush_timeout", ifp
->name
);
475 timeval_min(tv
, &babel_ifp
->hello_timeout
);
476 printIfMin(tv
, 1, "hello_timeout", ifp
->name
);
477 timeval_min(tv
, &babel_ifp
->update_timeout
);
478 printIfMin(tv
, 1, "update_timeout", ifp
->name
);
479 timeval_min(tv
, &babel_ifp
->update_flush_timeout
);
480 printIfMin(tv
, 1, "update_flush_timeout",ifp
->name
);
482 timeval_min(tv
, &unicast_flush_timeout
);
483 printIfMin(tv
, 1, "unicast_flush_timeout", NULL
);
484 printIfMin(tv
, 2, NULL
, NULL
);
489 /* set the t_update thread of the babel routing process to be launch in
490 'timeout' (approximate at the milisecond) */
492 babel_set_timer(struct timeval
*timeout
)
494 long msecs
= timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000;
495 if (babel_routing_process
->t_update
!= NULL
) {
496 thread_cancel(babel_routing_process
->t_update
);
498 thread_add_timer_msec(master
, babel_main_loop
, NULL
, msecs
, &babel_routing_process
->t_update
);
502 schedule_neighbours_check(int msecs
, int override
)
504 struct timeval timeout
;
506 timeval_add_msec(&timeout
, &babel_now
, msecs
);
508 check_neighbours_timeout
= timeout
;
510 timeval_min(&check_neighbours_timeout
, &timeout
);
514 resize_receive_buffer(int size
)
516 if(size
<= receive_buffer_size
)
519 if(receive_buffer
== NULL
) {
520 receive_buffer
= malloc(size
);
521 if(receive_buffer
== NULL
) {
522 flog_err(BABEL_ERR_MEMORY
, "malloc(receive_buffer): %s",
523 safe_strerror(errno
));
526 receive_buffer_size
= size
;
529 new = realloc(receive_buffer
, size
);
531 flog_err(BABEL_ERR_MEMORY
, "realloc(receive_buffer): %s",
532 safe_strerror(errno
));
535 receive_buffer
= new;
536 receive_buffer_size
= size
;
542 babel_distribute_update (struct distribute
*dist
)
544 struct interface
*ifp
;
545 babel_interface_nfo
*babel_ifp
;
552 ifp
= if_lookup_by_name (dist
->ifname
, VRF_DEFAULT
);
556 babel_ifp
= babel_get_if_nfo(ifp
);
558 for (type
= 0; type
< DISTRIBUTE_MAX
; type
++) {
559 family
= type
== DISTRIBUTE_V4_IN
|| type
== DISTRIBUTE_V4_OUT
?
561 if (dist
->list
[type
])
562 babel_ifp
->list
[type
] = access_list_lookup (family
,
565 babel_ifp
->list
[type
] = NULL
;
566 if (dist
->prefix
[type
])
567 babel_ifp
->prefix
[type
] = prefix_list_lookup (family
,
570 babel_ifp
->prefix
[type
] = NULL
;
575 babel_distribute_update_interface (struct interface
*ifp
)
577 struct distribute
*dist
;
579 dist
= distribute_lookup (ifp
->name
);
581 babel_distribute_update (dist
);
584 /* Update all interface's distribute list. */
586 babel_distribute_update_all (struct prefix_list
*notused
)
588 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
589 struct interface
*ifp
;
591 FOR_ALL_INTERFACES (vrf
, ifp
)
592 babel_distribute_update_interface (ifp
);
596 babel_distribute_update_all_wrapper (struct access_list
*notused
)
598 babel_distribute_update_all(NULL
);
603 DEFUN_NOSH (router_babel
,
606 "Enable a routing process\n"
607 "Make Babel instance command\n")
611 vty
->node
= BABEL_NODE
;
613 if (!babel_routing_process
) {
614 ret
= babel_create_routing_process ();
616 /* Notice to user we couldn't create Babel. */
618 zlog_warn ("can't create Babel");
627 DEFUN (no_router_babel
,
631 "Disable a routing process\n"
632 "Remove Babel instance command\n")
634 if(babel_routing_process
)
635 babel_clean_routing_process();
639 /* [Babel Command] */
640 DEFUN (babel_diversity
,
644 "Enable diversity-aware routing.\n")
646 diversity_kind
= DIVERSITY_CHANNEL
;
650 /* [Babel Command] */
651 DEFUN (no_babel_diversity
,
652 no_babel_diversity_cmd
,
653 "no babel diversity",
656 "Disable diversity-aware routing.\n")
658 diversity_kind
= DIVERSITY_NONE
;
662 /* [Babel Command] */
663 DEFUN (babel_diversity_factor
,
664 babel_diversity_factor_cmd
,
665 "babel diversity-factor (1-256)",
667 "Set the diversity factor.\n"
668 "Factor in units of 1/256.\n")
672 factor
= strtoul(argv
[2]->arg
, NULL
, 10);
674 diversity_factor
= factor
;
678 /* [Babel Command] */
679 DEFUN (babel_set_resend_delay
,
680 babel_set_resend_delay_cmd
,
681 "babel resend-delay (20-655340)",
683 "Time before resending a message\n"
688 interval
= strtoul(argv
[2]->arg
, NULL
, 10);
690 resend_delay
= interval
;
694 /* [Babel Command] */
695 DEFUN (babel_set_smoothing_half_life
,
696 babel_set_smoothing_half_life_cmd
,
697 "babel smoothing-half-life (0-65534)",
699 "Smoothing half-life\n"
700 "Seconds (0 to disable)\n")
704 seconds
= strtoul(argv
[2]->arg
, NULL
, 10);
706 change_smoothing_half_life(seconds
);
711 babeld_quagga_init(void)
714 install_node(&cmd_babel_node
, &babel_config_write
);
716 install_element(CONFIG_NODE
, &router_babel_cmd
);
717 install_element(CONFIG_NODE
, &no_router_babel_cmd
);
719 install_default(BABEL_NODE
);
720 install_element(BABEL_NODE
, &babel_diversity_cmd
);
721 install_element(BABEL_NODE
, &no_babel_diversity_cmd
);
722 install_element(BABEL_NODE
, &babel_diversity_factor_cmd
);
723 install_element(BABEL_NODE
, &babel_set_resend_delay_cmd
);
724 install_element(BABEL_NODE
, &babel_set_smoothing_half_life_cmd
);
728 /* Access list install. */
730 access_list_add_hook (babel_distribute_update_all_wrapper
);
731 access_list_delete_hook (babel_distribute_update_all_wrapper
);
733 /* Prefix list initialize.*/
735 prefix_list_add_hook (babel_distribute_update_all
);
736 prefix_list_delete_hook (babel_distribute_update_all
);
738 /* Distribute list install. */
739 distribute_list_init (BABEL_NODE
);
740 distribute_list_add_hook (babel_distribute_update
);
741 distribute_list_delete_hook (babel_distribute_update
);
744 /* Stubs to adapt Babel's filtering calls to Quagga's infrastructure. */
747 input_filter(const unsigned char *id
,
748 const unsigned char *prefix
, unsigned short plen
,
749 const unsigned char *neigh
, unsigned int ifindex
)
751 return babel_filter(0, prefix
, plen
, ifindex
);
755 output_filter(const unsigned char *id
, const unsigned char *prefix
,
756 unsigned short plen
, unsigned int ifindex
)
758 return babel_filter(1, prefix
, plen
, ifindex
);
761 /* There's no redistribute filter in Quagga -- the zebra daemon does its
764 redistribute_filter(const unsigned char *prefix
, unsigned short plen
,
765 unsigned int ifindex
, int proto
)