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 if (resend_delay
!= BABEL_DEFAULT_RESEND_DELAY
)
107 vty_out (vty
, " babel resend-delay %u%s", resend_delay
, VTY_NEWLINE
);
110 /* list enabled interfaces */
111 lines
= 1 + babel_enable_if_config_write (vty
);
112 /* list redistributed protocols */
113 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
114 if (i
!= zclient
->redist_default
&& zclient
->redist
[i
])
116 vty_out (vty
, " redistribute %s%s", zebra_route_string (i
), VTY_NEWLINE
);
125 babel_create_routing_process (void)
127 assert (babel_routing_process
== NULL
);
129 /* Allocaste Babel instance. */
130 babel_routing_process
= XCALLOC (MTYPE_BABEL
, sizeof (struct babel
));
132 /* Initialize timeouts */
134 expiry_time
= babel_now
.tv_sec
+ roughly(30);
135 source_expiry_time
= babel_now
.tv_sec
+ roughly(300);
137 /* Make socket for Babel protocol. */
138 protocol_socket
= babel_socket(protocol_port
);
139 if (protocol_socket
< 0) {
140 zlog_err("Couldn't create link local socket: %s", safe_strerror(errno
));
145 babel_routing_process
->t_read
=
146 thread_add_read(master
, &babel_read_protocol
, NULL
, protocol_socket
);
147 /* wait a little: zebra will announce interfaces, addresses, routes... */
148 babel_routing_process
->t_update
=
149 thread_add_timer_msec(master
, &babel_init_routing_process
, NULL
, 200L);
153 XFREE(MTYPE_BABEL
, babel_routing_process
);
154 babel_routing_process
= NULL
;
158 /* thread reading entries form others babel daemons */
160 babel_read_protocol (struct thread
*thread
)
163 struct interface
*ifp
= NULL
;
164 struct sockaddr_in6 sin6
;
165 struct listnode
*linklist_node
= NULL
;
167 assert(babel_routing_process
!= NULL
);
168 assert(protocol_socket
>= 0);
170 rc
= babel_recv(protocol_socket
,
171 receive_buffer
, receive_buffer_size
,
172 (struct sockaddr
*)&sin6
, sizeof(sin6
));
174 if(errno
!= EAGAIN
&& errno
!= EINTR
) {
175 zlog_err("recv: %s", safe_strerror(errno
));
178 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
181 if(ifp
->ifindex
== sin6
.sin6_scope_id
) {
182 parse_packet((unsigned char*)&sin6
.sin6_addr
, ifp
,
190 babel_routing_process
->t_read
=
191 thread_add_read(master
, &babel_read_protocol
, NULL
, protocol_socket
);
195 /* Zebra will give some information, especially about interfaces. This function
196 must be call with a litte timeout wich may give zebra the time to do his job,
197 making these inits have sense. */
199 babel_init_routing_process(struct thread
*thread
)
201 myseqno
= (random() & 0xFFFF);
203 babel_load_state_file();
204 debugf(BABEL_DEBUG_COMMON
, "My ID is : %s.", format_eui64(myid
));
205 babel_initial_noise();
206 babel_main_loop(thread
);/* this function self-add to the t_update thread */
210 /* fill "myid" with an unique id (only if myid != {0}). */
214 struct interface
*ifp
= NULL
;
215 struct listnode
*linklist_node
= NULL
;
219 /* if we already have an id (from state file), we return. */
220 if (memcmp(myid
, zeroes
, 8) != 0) {
224 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
225 /* ifp->ifindex is not necessarily valid at this point */
226 int ifindex
= if_nametoindex(ifp
->name
);
228 unsigned char eui
[8];
229 rc
= if_eui64(ifp
->name
, ifindex
, eui
);
232 memcpy(myid
, eui
, 8);
237 /* We failed to get a global EUI64 from the interfaces we were given.
238 Let's try to find an interface with a MAC address. */
239 for(i
= 1; i
< 256; i
++) {
240 char buf
[IF_NAMESIZE
], *ifname
;
241 unsigned char eui
[8];
242 ifname
= if_indextoname(i
, buf
);
245 rc
= if_eui64(ifname
, i
, eui
);
248 memcpy(myid
, eui
, 8);
252 zlog_err("Warning: couldn't find router id -- using random value.");
254 rc
= read_random_bytes(myid
, 8);
256 zlog_err("read(random): %s (cannot assign an ID)",safe_strerror(errno
));
259 /* Clear group and global bits */
263 /* Make some noise so that others notice us, and send retractions in
264 case we were restarted recently */
266 babel_initial_noise(void)
268 struct interface
*ifp
= NULL
;
269 struct listnode
*linklist_node
= NULL
;
271 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
274 /* Apply jitter before we send the first message. */
275 usleep(roughly(10000));
278 send_wildcard_retraction(ifp
);
281 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
284 usleep(roughly(10000));
287 send_wildcard_retraction(ifp
);
288 send_self_update(ifp
);
289 send_request(ifp
, NULL
, 0);
295 /* Delete all the added babel routes, make babeld only speak to zebra. */
297 babel_clean_routing_process()
300 babel_interface_close_all();
303 if (babel_routing_process
->t_read
!= NULL
) {
304 thread_cancel(babel_routing_process
->t_read
);
306 if (babel_routing_process
->t_update
!= NULL
) {
307 thread_cancel(babel_routing_process
->t_update
);
310 XFREE(MTYPE_BABEL
, babel_routing_process
);
311 babel_routing_process
= NULL
;
314 /* Function used with timeout. */
316 babel_main_loop(struct thread
*thread
)
319 struct interface
*ifp
= NULL
;
320 struct listnode
*linklist_node
= NULL
;
325 /* timeouts --------------------------------------------------------- */
326 /* get the next timeout */
327 babel_fill_with_next_timeout(&tv
);
328 /* if there is no timeout, we must wait. */
329 if(timeval_compare(&tv
, &babel_now
) > 0) {
330 timeval_minus(&tv
, &tv
, &babel_now
);
331 debugf(BABEL_DEBUG_TIMEOUT
, "babel main loop : timeout: %ld msecs",
332 tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000);
333 /* it happens often to have less than 1 ms, it's bad. */
334 timeval_add_msec(&tv
, &tv
, 300);
335 babel_set_timer(&tv
);
341 /* update database -------------------------------------------------- */
342 if(timeval_compare(&check_neighbours_timeout
, &babel_now
) < 0) {
344 msecs
= check_neighbours();
345 msecs
= MAX(msecs
, 10);
346 schedule_neighbours_check(msecs
, 1);
349 if(babel_now
.tv_sec
>= expiry_time
) {
352 expiry_time
= babel_now
.tv_sec
+ roughly(30);
355 if(babel_now
.tv_sec
>= source_expiry_time
) {
357 source_expiry_time
= babel_now
.tv_sec
+ roughly(300);
360 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
361 babel_interface_nfo
*babel_ifp
= NULL
;
364 babel_ifp
= babel_get_if_nfo(ifp
);
365 if(timeval_compare(&babel_now
, &babel_ifp
->hello_timeout
) >= 0)
367 if(timeval_compare(&babel_now
, &babel_ifp
->update_timeout
) >= 0)
368 send_update(ifp
, 0, NULL
, 0);
369 if(timeval_compare(&babel_now
,
370 &babel_ifp
->update_flush_timeout
) >= 0)
374 if(resend_time
.tv_sec
!= 0) {
375 if(timeval_compare(&babel_now
, &resend_time
) >= 0)
379 if(unicast_flush_timeout
.tv_sec
!= 0) {
380 if(timeval_compare(&babel_now
, &unicast_flush_timeout
) >= 0)
384 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
385 babel_interface_nfo
*babel_ifp
= NULL
;
388 babel_ifp
= babel_get_if_nfo(ifp
);
389 if(babel_ifp
->flush_timeout
.tv_sec
!= 0) {
390 if(timeval_compare(&babel_now
, &babel_ifp
->flush_timeout
) >= 0)
396 assert(0); /* this line should never be reach */
400 printIfMin(struct timeval
*tv
, int cmd
, const char *tag
, const char *ifname
)
402 static struct timeval curr_tv
;
403 static char buffer
[200];
404 static const char *curr_tag
= NULL
;
407 case 0: /* reset timeval */
410 snprintf(buffer
, 200L, "interface: %s; %s", ifname
, tag
);
416 case 1: /* take the min */
417 if (tv
->tv_sec
== 0 && tv
->tv_usec
== 0) { /* if (tv == ∞) */
420 if (tv
->tv_sec
< curr_tv
.tv_sec
||(tv
->tv_sec
== curr_tv
.tv_sec
&&
421 tv
->tv_usec
< curr_tv
.tv_usec
)) {
424 snprintf(buffer
, 200L, "interface: %s; %s", ifname
, tag
);
431 case 2: /* print message */
432 debugf(BABEL_DEBUG_TIMEOUT
, "next timeout due to: %s", curr_tag
);
440 babel_fill_with_next_timeout(struct timeval
*tv
)
442 #if (defined NO_DEBUG)
443 #define printIfMin(a,b,c,d)
445 #define printIfMin(a,b,c,d) \
446 if (UNLIKELY(debug & BABEL_DEBUG_TIMEOUT)) {printIfMin(a,b,c,d);}
448 struct interface
*ifp
= NULL
;
449 struct listnode
*linklist_node
= NULL
;
451 *tv
= check_neighbours_timeout
;
452 printIfMin(tv
, 0, "check_neighbours_timeout", NULL
);
453 timeval_min_sec(tv
, expiry_time
);
454 printIfMin(tv
, 1, "expiry_time", NULL
);
455 timeval_min_sec(tv
, source_expiry_time
);
456 printIfMin(tv
, 1, "source_expiry_time", NULL
);
457 timeval_min(tv
, &resend_time
);
458 printIfMin(tv
, 1, "resend_time", NULL
);
459 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
460 babel_interface_nfo
*babel_ifp
= NULL
;
463 babel_ifp
= babel_get_if_nfo(ifp
);
464 timeval_min(tv
, &babel_ifp
->flush_timeout
);
465 printIfMin(tv
, 1, "flush_timeout", ifp
->name
);
466 timeval_min(tv
, &babel_ifp
->hello_timeout
);
467 printIfMin(tv
, 1, "hello_timeout", ifp
->name
);
468 timeval_min(tv
, &babel_ifp
->update_timeout
);
469 printIfMin(tv
, 1, "update_timeout", ifp
->name
);
470 timeval_min(tv
, &babel_ifp
->update_flush_timeout
);
471 printIfMin(tv
, 1, "update_flush_timeout",ifp
->name
);
473 timeval_min(tv
, &unicast_flush_timeout
);
474 printIfMin(tv
, 1, "unicast_flush_timeout", NULL
);
475 printIfMin(tv
, 2, NULL
, NULL
);
480 /* set the t_update thread of the babel routing process to be launch in
481 'timeout' (approximate at the milisecond) */
483 babel_set_timer(struct timeval
*timeout
)
485 long msecs
= timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000;
486 if (babel_routing_process
->t_update
!= NULL
) {
487 thread_cancel(babel_routing_process
->t_update
);
489 babel_routing_process
->t_update
=
490 thread_add_timer_msec(master
, &babel_main_loop
, NULL
, msecs
);
493 /* Schedule a neighbours check after roughly 3/2 times msecs have elapsed. */
495 schedule_neighbours_check(int msecs
, int override
)
497 struct timeval timeout
;
499 timeval_add_msec(&timeout
, &babel_now
, roughly(msecs
* 3 / 2));
501 check_neighbours_timeout
= timeout
;
503 timeval_min(&check_neighbours_timeout
, &timeout
);
507 resize_receive_buffer(int size
)
509 if(size
<= receive_buffer_size
)
512 if(receive_buffer
== NULL
) {
513 receive_buffer
= malloc(size
);
514 if(receive_buffer
== NULL
) {
515 zlog_err("malloc(receive_buffer): %s", safe_strerror(errno
));
518 receive_buffer_size
= size
;
521 new = realloc(receive_buffer
, size
);
523 zlog_err("realloc(receive_buffer): %s", safe_strerror(errno
));
526 receive_buffer
= new;
527 receive_buffer_size
= size
;
533 babel_distribute_update (struct distribute
*dist
)
535 struct interface
*ifp
;
536 babel_interface_nfo
*babel_ifp
;
537 struct access_list
*alist
;
538 struct prefix_list
*plist
;
543 ifp
= if_lookup_by_name (dist
->ifname
);
547 babel_ifp
= babel_get_if_nfo(ifp
);
549 if (dist
->list
[DISTRIBUTE_IN
]) {
550 alist
= access_list_lookup (AFI_IP6
, dist
->list
[DISTRIBUTE_IN
]);
552 babel_ifp
->list
[BABEL_FILTER_IN
] = alist
;
554 babel_ifp
->list
[BABEL_FILTER_IN
] = NULL
;
556 babel_ifp
->list
[BABEL_FILTER_IN
] = NULL
;
559 if (dist
->list
[DISTRIBUTE_OUT
]) {
560 alist
= access_list_lookup (AFI_IP6
, dist
->list
[DISTRIBUTE_OUT
]);
562 babel_ifp
->list
[BABEL_FILTER_OUT
] = alist
;
564 babel_ifp
->list
[BABEL_FILTER_OUT
] = NULL
;
566 babel_ifp
->list
[BABEL_FILTER_OUT
] = NULL
;
569 if (dist
->prefix
[DISTRIBUTE_IN
]) {
570 plist
= prefix_list_lookup (AFI_IP6
, dist
->prefix
[DISTRIBUTE_IN
]);
572 babel_ifp
->prefix
[BABEL_FILTER_IN
] = plist
;
574 babel_ifp
->prefix
[BABEL_FILTER_IN
] = NULL
;
576 babel_ifp
->prefix
[BABEL_FILTER_IN
] = NULL
;
579 if (dist
->prefix
[DISTRIBUTE_OUT
]) {
580 plist
= prefix_list_lookup (AFI_IP6
, dist
->prefix
[DISTRIBUTE_OUT
]);
582 babel_ifp
->prefix
[BABEL_FILTER_OUT
] = plist
;
584 babel_ifp
->prefix
[BABEL_FILTER_OUT
] = NULL
;
586 babel_ifp
->prefix
[BABEL_FILTER_OUT
] = NULL
;
591 babel_distribute_update_interface (struct interface
*ifp
)
593 struct distribute
*dist
;
595 dist
= distribute_lookup (ifp
->name
);
597 babel_distribute_update (dist
);
600 /* Update all interface's distribute list. */
602 babel_distribute_update_all (struct prefix_list
*notused
)
604 struct interface
*ifp
;
605 struct listnode
*node
;
607 for (ALL_LIST_ELEMENTS_RO (iflist
, node
, ifp
))
608 babel_distribute_update_interface (ifp
);
612 babel_distribute_update_all_wrapper (struct access_list
*notused
)
614 babel_distribute_update_all(NULL
);
622 "Enable a routing process\n"
623 "Make Babel instance command\n"
628 vty
->node
= BABEL_NODE
;
630 if (!babel_routing_process
) {
631 ret
= babel_create_routing_process ();
633 /* Notice to user we couldn't create Babel. */
635 zlog_warn ("can't create Babel");
644 DEFUN (no_router_babel
,
648 "Disable a routing process\n"
649 "Remove Babel instance command\n"
652 if(babel_routing_process
)
653 babel_clean_routing_process();
657 /* [Babel Command] */
658 DEFUN (babel_set_resend_delay
,
659 babel_set_resend_delay_cmd
,
660 "babel resend-delay <20-655340>",
662 "Time before resending a message\n"
667 VTY_GET_INTEGER_RANGE("milliseconds", interval
, argv
[0], 20, 10 * 0xFFFE);
669 resend_delay
= interval
;
674 babeld_quagga_init(void)
677 install_node(&cmd_babel_node
, &babel_config_write
);
679 install_element(CONFIG_NODE
, &router_babel_cmd
);
680 install_element(CONFIG_NODE
, &no_router_babel_cmd
);
682 install_default(BABEL_NODE
);
683 install_element(BABEL_NODE
, &babel_set_resend_delay_cmd
);
687 /* Access list install. */
689 access_list_add_hook (babel_distribute_update_all_wrapper
);
690 access_list_delete_hook (babel_distribute_update_all_wrapper
);
692 /* Prefix list initialize.*/
694 prefix_list_add_hook (babel_distribute_update_all
);
695 prefix_list_delete_hook (babel_distribute_update_all
);
697 /* Distribute list install. */
698 distribute_list_init (BABEL_NODE
);
699 distribute_list_add_hook (babel_distribute_update
);
700 distribute_list_delete_hook (babel_distribute_update
);
703 /* Stubs to adapt Babel's filtering calls to Quagga's infrastructure. */
706 input_filter(const unsigned char *id
,
707 const unsigned char *prefix
, unsigned short plen
,
708 const unsigned char *neigh
, unsigned int ifindex
)
710 return babel_filter(0, prefix
, plen
, ifindex
);
714 output_filter(const unsigned char *id
, const unsigned char *prefix
,
715 unsigned short plen
, unsigned int ifindex
)
717 return babel_filter(1, prefix
, plen
, ifindex
);
720 /* There's no redistribute filter in Quagga -- the zebra daemon does its
723 redistribute_filter(const unsigned char *prefix
, unsigned short plen
,
724 unsigned int ifindex
, int proto
)