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"
66 static int babel_init_routing_process(struct thread
*thread
);
67 static void babel_get_myid(void);
68 static void babel_initial_noise(void);
69 static int babel_read_protocol (struct thread
*thread
);
70 static int babel_main_loop(struct thread
*thread
);
71 static void babel_set_timer(struct timeval
*timeout
);
72 static void babel_fill_with_next_timeout(struct timeval
*tv
);
75 /* Informations relative to the babel running daemon. */
76 static struct babel
*babel_routing_process
= NULL
;
77 static unsigned char *receive_buffer
= NULL
;
78 static int receive_buffer_size
= 0;
81 struct timeval check_neighbours_timeout
;
82 static time_t expiry_time
;
83 static time_t source_expiry_time
;
85 /* Babel node structure. */
86 static struct cmd_node cmd_babel_node
=
89 .prompt
= "%s(config-router)# ",
93 /* print current babel configuration on vty */
95 babel_config_write (struct vty
*vty
)
101 /* list enabled debug modes */
102 lines
+= debug_babel_config_write (vty
);
104 if (!babel_routing_process
)
106 vty_out (vty
, "router babel%s", VTY_NEWLINE
);
107 if (resend_delay
!= BABEL_DEFAULT_RESEND_DELAY
)
109 vty_out (vty
, " babel resend-delay %u%s", resend_delay
, VTY_NEWLINE
);
112 /* list enabled interfaces */
113 lines
= 1 + babel_enable_if_config_write (vty
);
114 /* list redistributed protocols */
115 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
116 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
117 if (i
!= zclient
->redist_default
&&
118 vrf_bitmap_check (zclient
->redist
[afi
][i
], VRF_DEFAULT
) )
120 vty_out (vty
, " redistribute %s%s", zebra_route_string (i
), VTY_NEWLINE
);
129 babel_create_routing_process (void)
131 assert (babel_routing_process
== NULL
);
133 /* Allocaste Babel instance. */
134 babel_routing_process
= XCALLOC (MTYPE_BABEL
, sizeof (struct babel
));
136 /* Initialize timeouts */
138 expiry_time
= babel_now
.tv_sec
+ roughly(30);
139 source_expiry_time
= babel_now
.tv_sec
+ roughly(300);
141 /* Make socket for Babel protocol. */
142 protocol_socket
= babel_socket(protocol_port
);
143 if (protocol_socket
< 0) {
144 zlog_err("Couldn't create link local socket: %s", safe_strerror(errno
));
149 babel_routing_process
->t_read
=
150 thread_add_read(master
, &babel_read_protocol
, NULL
, protocol_socket
);
151 /* wait a little: zebra will announce interfaces, addresses, routes... */
152 babel_routing_process
->t_update
=
153 thread_add_timer_msec(master
, &babel_init_routing_process
, NULL
, 200L);
157 XFREE(MTYPE_BABEL
, babel_routing_process
);
158 babel_routing_process
= NULL
;
162 /* thread reading entries form others babel daemons */
164 babel_read_protocol (struct thread
*thread
)
167 struct interface
*ifp
= NULL
;
168 struct sockaddr_in6 sin6
;
169 struct listnode
*linklist_node
= NULL
;
171 assert(babel_routing_process
!= NULL
);
172 assert(protocol_socket
>= 0);
174 rc
= babel_recv(protocol_socket
,
175 receive_buffer
, receive_buffer_size
,
176 (struct sockaddr
*)&sin6
, sizeof(sin6
));
178 if(errno
!= EAGAIN
&& errno
!= EINTR
) {
179 zlog_err("recv: %s", safe_strerror(errno
));
182 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
185 if(ifp
->ifindex
== sin6
.sin6_scope_id
) {
186 parse_packet((unsigned char*)&sin6
.sin6_addr
, ifp
,
194 babel_routing_process
->t_read
=
195 thread_add_read(master
, &babel_read_protocol
, NULL
, protocol_socket
);
199 /* Zebra will give some information, especially about interfaces. This function
200 must be call with a litte timeout wich may give zebra the time to do his job,
201 making these inits have sense. */
203 babel_init_routing_process(struct thread
*thread
)
205 myseqno
= (random() & 0xFFFF);
207 babel_load_state_file();
208 debugf(BABEL_DEBUG_COMMON
, "My ID is : %s.", format_eui64(myid
));
209 babel_initial_noise();
210 babel_main_loop(thread
);/* this function self-add to the t_update thread */
214 /* fill "myid" with an unique id (only if myid != {0}). */
218 struct interface
*ifp
= NULL
;
219 struct listnode
*linklist_node
= NULL
;
223 /* if we already have an id (from state file), we return. */
224 if (memcmp(myid
, zeroes
, 8) != 0) {
228 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
229 /* ifp->ifindex is not necessarily valid at this point */
230 int ifindex
= if_nametoindex(ifp
->name
);
232 unsigned char eui
[8];
233 rc
= if_eui64(ifp
->name
, ifindex
, eui
);
236 memcpy(myid
, eui
, 8);
241 /* We failed to get a global EUI64 from the interfaces we were given.
242 Let's try to find an interface with a MAC address. */
243 for(i
= 1; i
< 256; i
++) {
244 char buf
[IF_NAMESIZE
], *ifname
;
245 unsigned char eui
[8];
246 ifname
= if_indextoname(i
, buf
);
249 rc
= if_eui64(ifname
, i
, eui
);
252 memcpy(myid
, eui
, 8);
256 zlog_err("Warning: couldn't find router id -- using random value.");
258 rc
= read_random_bytes(myid
, 8);
260 zlog_err("read(random): %s (cannot assign an ID)",safe_strerror(errno
));
263 /* Clear group and global bits */
267 /* Make some noise so that others notice us, and send retractions in
268 case we were restarted recently */
270 babel_initial_noise(void)
272 struct interface
*ifp
= NULL
;
273 struct listnode
*linklist_node
= NULL
;
275 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
278 /* Apply jitter before we send the first message. */
279 usleep(roughly(10000));
282 send_wildcard_retraction(ifp
);
285 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
288 usleep(roughly(10000));
291 send_wildcard_retraction(ifp
);
292 send_self_update(ifp
);
293 send_request(ifp
, NULL
, 0);
299 /* Delete all the added babel routes, make babeld only speak to zebra. */
301 babel_clean_routing_process()
304 babel_interface_close_all();
307 if (babel_routing_process
->t_read
!= NULL
) {
308 thread_cancel(babel_routing_process
->t_read
);
310 if (babel_routing_process
->t_update
!= NULL
) {
311 thread_cancel(babel_routing_process
->t_update
);
314 XFREE(MTYPE_BABEL
, babel_routing_process
);
315 babel_routing_process
= NULL
;
318 /* Function used with timeout. */
320 babel_main_loop(struct thread
*thread
)
323 struct interface
*ifp
= NULL
;
324 struct listnode
*linklist_node
= NULL
;
329 /* timeouts --------------------------------------------------------- */
330 /* get the next timeout */
331 babel_fill_with_next_timeout(&tv
);
332 /* if there is no timeout, we must wait. */
333 if(timeval_compare(&tv
, &babel_now
) > 0) {
334 timeval_minus(&tv
, &tv
, &babel_now
);
335 debugf(BABEL_DEBUG_TIMEOUT
, "babel main loop : timeout: %ld msecs",
336 tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000);
337 /* it happens often to have less than 1 ms, it's bad. */
338 timeval_add_msec(&tv
, &tv
, 300);
339 babel_set_timer(&tv
);
345 /* update database -------------------------------------------------- */
346 if(timeval_compare(&check_neighbours_timeout
, &babel_now
) < 0) {
348 msecs
= check_neighbours();
349 msecs
= MAX(msecs
, 10);
350 schedule_neighbours_check(msecs
, 1);
353 if(babel_now
.tv_sec
>= expiry_time
) {
356 expiry_time
= babel_now
.tv_sec
+ roughly(30);
359 if(babel_now
.tv_sec
>= source_expiry_time
) {
361 source_expiry_time
= babel_now
.tv_sec
+ roughly(300);
364 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
365 babel_interface_nfo
*babel_ifp
= NULL
;
368 babel_ifp
= babel_get_if_nfo(ifp
);
369 if(timeval_compare(&babel_now
, &babel_ifp
->hello_timeout
) >= 0)
371 if(timeval_compare(&babel_now
, &babel_ifp
->update_timeout
) >= 0)
372 send_update(ifp
, 0, NULL
, 0);
373 if(timeval_compare(&babel_now
,
374 &babel_ifp
->update_flush_timeout
) >= 0)
378 if(resend_time
.tv_sec
!= 0) {
379 if(timeval_compare(&babel_now
, &resend_time
) >= 0)
383 if(unicast_flush_timeout
.tv_sec
!= 0) {
384 if(timeval_compare(&babel_now
, &unicast_flush_timeout
) >= 0)
388 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
389 babel_interface_nfo
*babel_ifp
= NULL
;
392 babel_ifp
= babel_get_if_nfo(ifp
);
393 if(babel_ifp
->flush_timeout
.tv_sec
!= 0) {
394 if(timeval_compare(&babel_now
, &babel_ifp
->flush_timeout
) >= 0)
400 assert(0); /* this line should never be reach */
404 printIfMin(struct timeval
*tv
, int cmd
, const char *tag
, const char *ifname
)
406 static struct timeval curr_tv
;
407 static char buffer
[200];
408 static const char *curr_tag
= NULL
;
411 case 0: /* reset timeval */
414 snprintf(buffer
, 200L, "interface: %s; %s", ifname
, tag
);
420 case 1: /* take the min */
421 if (tv
->tv_sec
== 0 && tv
->tv_usec
== 0) { /* if (tv == ∞) */
424 if (tv
->tv_sec
< curr_tv
.tv_sec
||(tv
->tv_sec
== curr_tv
.tv_sec
&&
425 tv
->tv_usec
< curr_tv
.tv_usec
)) {
428 snprintf(buffer
, 200L, "interface: %s; %s", ifname
, tag
);
435 case 2: /* print message */
436 debugf(BABEL_DEBUG_TIMEOUT
, "next timeout due to: %s", curr_tag
);
444 babel_fill_with_next_timeout(struct timeval
*tv
)
446 #if (defined NO_DEBUG)
447 #define printIfMin(a,b,c,d)
449 #define printIfMin(a,b,c,d) \
450 if (UNLIKELY(debug & BABEL_DEBUG_TIMEOUT)) {printIfMin(a,b,c,d);}
452 struct interface
*ifp
= NULL
;
453 struct listnode
*linklist_node
= NULL
;
455 *tv
= check_neighbours_timeout
;
456 printIfMin(tv
, 0, "check_neighbours_timeout", NULL
);
457 timeval_min_sec(tv
, expiry_time
);
458 printIfMin(tv
, 1, "expiry_time", NULL
);
459 timeval_min_sec(tv
, source_expiry_time
);
460 printIfMin(tv
, 1, "source_expiry_time", NULL
);
461 timeval_min(tv
, &resend_time
);
462 printIfMin(tv
, 1, "resend_time", NULL
);
463 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
464 babel_interface_nfo
*babel_ifp
= NULL
;
467 babel_ifp
= babel_get_if_nfo(ifp
);
468 timeval_min(tv
, &babel_ifp
->flush_timeout
);
469 printIfMin(tv
, 1, "flush_timeout", ifp
->name
);
470 timeval_min(tv
, &babel_ifp
->hello_timeout
);
471 printIfMin(tv
, 1, "hello_timeout", ifp
->name
);
472 timeval_min(tv
, &babel_ifp
->update_timeout
);
473 printIfMin(tv
, 1, "update_timeout", ifp
->name
);
474 timeval_min(tv
, &babel_ifp
->update_flush_timeout
);
475 printIfMin(tv
, 1, "update_flush_timeout",ifp
->name
);
477 timeval_min(tv
, &unicast_flush_timeout
);
478 printIfMin(tv
, 1, "unicast_flush_timeout", NULL
);
479 printIfMin(tv
, 2, NULL
, NULL
);
484 /* set the t_update thread of the babel routing process to be launch in
485 'timeout' (approximate at the milisecond) */
487 babel_set_timer(struct timeval
*timeout
)
489 long msecs
= timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000;
490 if (babel_routing_process
->t_update
!= NULL
) {
491 thread_cancel(babel_routing_process
->t_update
);
493 babel_routing_process
->t_update
=
494 thread_add_timer_msec(master
, &babel_main_loop
, NULL
, msecs
);
497 /* Schedule a neighbours check after roughly 3/2 times msecs have elapsed. */
499 schedule_neighbours_check(int msecs
, int override
)
501 struct timeval timeout
;
503 timeval_add_msec(&timeout
, &babel_now
, roughly(msecs
* 3 / 2));
505 check_neighbours_timeout
= timeout
;
507 timeval_min(&check_neighbours_timeout
, &timeout
);
511 resize_receive_buffer(int size
)
513 if(size
<= receive_buffer_size
)
516 if(receive_buffer
== NULL
) {
517 receive_buffer
= malloc(size
);
518 if(receive_buffer
== NULL
) {
519 zlog_err("malloc(receive_buffer): %s", safe_strerror(errno
));
522 receive_buffer_size
= size
;
525 new = realloc(receive_buffer
, size
);
527 zlog_err("realloc(receive_buffer): %s", safe_strerror(errno
));
530 receive_buffer
= new;
531 receive_buffer_size
= size
;
537 babel_distribute_update (struct distribute
*dist
)
539 struct interface
*ifp
;
540 babel_interface_nfo
*babel_ifp
;
541 struct access_list
*alist
;
542 struct prefix_list
*plist
;
547 ifp
= if_lookup_by_name (dist
->ifname
);
551 babel_ifp
= babel_get_if_nfo(ifp
);
553 if (dist
->list
[DISTRIBUTE_IN
]) {
554 alist
= access_list_lookup (AFI_IP6
, dist
->list
[DISTRIBUTE_IN
]);
556 babel_ifp
->list
[BABEL_FILTER_IN
] = alist
;
558 babel_ifp
->list
[BABEL_FILTER_IN
] = NULL
;
560 babel_ifp
->list
[BABEL_FILTER_IN
] = NULL
;
563 if (dist
->list
[DISTRIBUTE_OUT
]) {
564 alist
= access_list_lookup (AFI_IP6
, dist
->list
[DISTRIBUTE_OUT
]);
566 babel_ifp
->list
[BABEL_FILTER_OUT
] = alist
;
568 babel_ifp
->list
[BABEL_FILTER_OUT
] = NULL
;
570 babel_ifp
->list
[BABEL_FILTER_OUT
] = NULL
;
573 if (dist
->prefix
[DISTRIBUTE_IN
]) {
574 plist
= prefix_list_lookup (AFI_IP6
, dist
->prefix
[DISTRIBUTE_IN
]);
576 babel_ifp
->prefix
[BABEL_FILTER_IN
] = plist
;
578 babel_ifp
->prefix
[BABEL_FILTER_IN
] = NULL
;
580 babel_ifp
->prefix
[BABEL_FILTER_IN
] = NULL
;
583 if (dist
->prefix
[DISTRIBUTE_OUT
]) {
584 plist
= prefix_list_lookup (AFI_IP6
, dist
->prefix
[DISTRIBUTE_OUT
]);
586 babel_ifp
->prefix
[BABEL_FILTER_OUT
] = plist
;
588 babel_ifp
->prefix
[BABEL_FILTER_OUT
] = NULL
;
590 babel_ifp
->prefix
[BABEL_FILTER_OUT
] = NULL
;
595 babel_distribute_update_interface (struct interface
*ifp
)
597 struct distribute
*dist
;
599 dist
= distribute_lookup (ifp
->name
);
601 babel_distribute_update (dist
);
604 /* Update all interface's distribute list. */
606 babel_distribute_update_all (struct prefix_list
*notused
)
608 struct interface
*ifp
;
609 struct listnode
*node
;
611 for (ALL_LIST_ELEMENTS_RO (iflist
, node
, ifp
))
612 babel_distribute_update_interface (ifp
);
616 babel_distribute_update_all_wrapper (struct access_list
*notused
)
618 babel_distribute_update_all(NULL
);
626 "Enable a routing process\n"
627 "Make Babel instance command\n"
632 vty
->node
= BABEL_NODE
;
634 if (!babel_routing_process
) {
635 ret
= babel_create_routing_process ();
637 /* Notice to user we couldn't create Babel. */
639 zlog_warn ("can't create Babel");
648 DEFUN (no_router_babel
,
652 "Disable a routing process\n"
653 "Remove Babel instance command\n"
656 if(babel_routing_process
)
657 babel_clean_routing_process();
661 /* [Babel Command] */
662 DEFUN (babel_set_resend_delay
,
663 babel_set_resend_delay_cmd
,
664 "babel resend-delay <20-655340>",
666 "Time before resending a message\n"
671 VTY_GET_INTEGER_RANGE("milliseconds", interval
, argv
[0], 20, 10 * 0xFFFE);
673 resend_delay
= interval
;
678 babeld_quagga_init(void)
681 install_node(&cmd_babel_node
, &babel_config_write
);
683 install_element(CONFIG_NODE
, &router_babel_cmd
);
684 install_element(CONFIG_NODE
, &no_router_babel_cmd
);
686 install_default(BABEL_NODE
);
687 install_element(BABEL_NODE
, &babel_set_resend_delay_cmd
);
691 /* Access list install. */
693 access_list_add_hook (babel_distribute_update_all_wrapper
);
694 access_list_delete_hook (babel_distribute_update_all_wrapper
);
696 /* Prefix list initialize.*/
698 prefix_list_add_hook (babel_distribute_update_all
);
699 prefix_list_delete_hook (babel_distribute_update_all
);
701 /* Distribute list install. */
702 distribute_list_init (BABEL_NODE
);
703 distribute_list_add_hook (babel_distribute_update
);
704 distribute_list_delete_hook (babel_distribute_update
);
707 /* Stubs to adapt Babel's filtering calls to Quagga's infrastructure. */
710 input_filter(const unsigned char *id
,
711 const unsigned char *prefix
, unsigned short plen
,
712 const unsigned char *neigh
, unsigned int ifindex
)
714 return babel_filter(0, prefix
, plen
, ifindex
);
718 output_filter(const unsigned char *id
, const unsigned char *prefix
,
719 unsigned short plen
, unsigned int ifindex
)
721 return babel_filter(1, prefix
, plen
, ifindex
);
724 /* There's no redistribute filter in Quagga -- the zebra daemon does its
727 redistribute_filter(const unsigned char *prefix
, unsigned short plen
,
728 unsigned int ifindex
, int proto
)