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"
64 static int babel_init_routing_process(struct thread
*thread
);
65 static void babel_get_myid(void);
66 static void babel_initial_noise(void);
67 static int babel_read_protocol (struct thread
*thread
);
68 static int babel_main_loop(struct thread
*thread
);
69 static void babel_set_timer(struct timeval
*timeout
);
70 static void babel_fill_with_next_timeout(struct timeval
*tv
);
73 /* Informations relative to the babel running daemon. */
74 static struct babel
*babel_routing_process
= NULL
;
75 static unsigned char *receive_buffer
= NULL
;
76 static int receive_buffer_size
= 0;
79 struct timeval check_neighbours_timeout
;
80 static time_t expiry_time
;
81 static time_t source_expiry_time
;
83 /* Babel node structure. */
84 static struct cmd_node cmd_babel_node
=
87 .prompt
= "%s(config-router)# ",
91 /* print current babel configuration on vty */
93 babel_config_write (struct vty
*vty
)
100 babel_create_routing_process (void)
102 assert (babel_routing_process
== NULL
);
104 /* Allocaste Babel instance. */
105 babel_routing_process
= XCALLOC (MTYPE_BABEL
, sizeof (struct babel
));
107 /* Initialize timeouts */
109 expiry_time
= babel_now
.tv_sec
+ roughly(30);
110 source_expiry_time
= babel_now
.tv_sec
+ roughly(300);
112 /* Make socket for Babel protocol. */
113 protocol_socket
= babel_socket(protocol_port
);
114 if (protocol_socket
< 0) {
115 zlog_err("Couldn't create link local socket: %s", safe_strerror(errno
));
120 babel_routing_process
->t_read
=
121 thread_add_read(master
, &babel_read_protocol
, NULL
, protocol_socket
);
122 /* wait a little: zebra will announce interfaces, addresses, routes... */
123 babel_routing_process
->t_update
=
124 thread_add_timer_msec(master
, &babel_init_routing_process
, NULL
, 200L);
128 XFREE(MTYPE_BABEL
, babel_routing_process
);
129 babel_routing_process
= NULL
;
133 /* thread reading entries form others babel daemons */
135 babel_read_protocol (struct thread
*thread
)
138 struct interface
*ifp
= NULL
;
139 struct sockaddr_in6 sin6
;
140 struct listnode
*linklist_node
= NULL
;
142 assert(babel_routing_process
!= NULL
);
143 assert(protocol_socket
>= 0);
145 rc
= babel_recv(protocol_socket
,
146 receive_buffer
, receive_buffer_size
,
147 (struct sockaddr
*)&sin6
, sizeof(sin6
));
149 if(errno
!= EAGAIN
&& errno
!= EINTR
) {
150 zlog_err("recv: %s", safe_strerror(errno
));
153 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
156 if(ifp
->ifindex
== sin6
.sin6_scope_id
) {
157 parse_packet((unsigned char*)&sin6
.sin6_addr
, ifp
,
165 babel_routing_process
->t_read
=
166 thread_add_read(master
, &babel_read_protocol
, NULL
, protocol_socket
);
170 /* Zebra will give some information, especially about interfaces. This function
171 must be call with a litte timeout wich may give zebra the time to do his job,
172 making these inits have sense. */
174 babel_init_routing_process(struct thread
*thread
)
176 myseqno
= (random() & 0xFFFF);
178 babel_load_state_file();
179 debugf(BABEL_DEBUG_COMMON
, "My ID is : %s.", format_eui64(myid
));
180 babel_initial_noise();
181 babel_main_loop(thread
);/* this function self-add to the t_update thread */
185 /* fill "myid" with an unique id (only if myid != {0}). */
189 struct interface
*ifp
= NULL
;
190 struct listnode
*linklist_node
= NULL
;
194 /* if we already have an id (from state file), we return. */
195 if (memcmp(myid
, zeroes
, 8) != 0) {
199 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
200 /* ifp->ifindex is not necessarily valid at this point */
201 int ifindex
= if_nametoindex(ifp
->name
);
203 unsigned char eui
[8];
204 rc
= if_eui64(ifp
->name
, ifindex
, eui
);
207 memcpy(myid
, eui
, 8);
212 /* We failed to get a global EUI64 from the interfaces we were given.
213 Let's try to find an interface with a MAC address. */
214 for(i
= 1; i
< 256; i
++) {
215 char buf
[IF_NAMESIZE
], *ifname
;
216 unsigned char eui
[8];
217 ifname
= if_indextoname(i
, buf
);
220 rc
= if_eui64(ifname
, i
, eui
);
223 memcpy(myid
, eui
, 8);
227 zlog_err("Warning: couldn't find router id -- using random value.");
229 rc
= read_random_bytes(myid
, 8);
231 zlog_err("read(random): %s (cannot assign an ID)",safe_strerror(errno
));
234 /* Clear group and global bits */
238 /* Make some noise so that others notice us, and send retractions in
239 case we were restarted recently */
241 babel_initial_noise(void)
243 struct interface
*ifp
= NULL
;
244 struct listnode
*linklist_node
= NULL
;
246 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
249 /* Apply jitter before we send the first message. */
250 usleep(roughly(10000));
253 send_wildcard_retraction(ifp
);
256 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
259 usleep(roughly(10000));
262 send_wildcard_retraction(ifp
);
263 send_self_update(ifp
);
264 send_request(ifp
, NULL
, 0);
270 /* Delete all the added babel routes, make babeld only speak to zebra. */
272 babel_clean_routing_process()
275 babel_interface_close_all();
278 if (babel_routing_process
->t_read
!= NULL
) {
279 thread_cancel(babel_routing_process
->t_read
);
281 if (babel_routing_process
->t_update
!= NULL
) {
282 thread_cancel(babel_routing_process
->t_update
);
285 XFREE(MTYPE_BABEL
, babel_routing_process
);
286 babel_routing_process
= NULL
;
289 /* Function used with timeout. */
291 babel_main_loop(struct thread
*thread
)
294 struct interface
*ifp
= NULL
;
295 struct listnode
*linklist_node
= NULL
;
300 /* timeouts --------------------------------------------------------- */
301 /* get the next timeout */
302 babel_fill_with_next_timeout(&tv
);
303 /* if there is no timeout, we must wait. */
304 if(timeval_compare(&tv
, &babel_now
) > 0) {
305 timeval_minus(&tv
, &tv
, &babel_now
);
306 debugf(BABEL_DEBUG_TIMEOUT
, "babel main loop : timeout: %ld msecs",
307 tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000);
308 /* it happens often to have less than 1 ms, it's bad. */
309 timeval_add_msec(&tv
, &tv
, 300);
310 babel_set_timer(&tv
);
316 /* update database -------------------------------------------------- */
317 if(timeval_compare(&check_neighbours_timeout
, &babel_now
) < 0) {
319 msecs
= check_neighbours();
320 msecs
= MAX(msecs
, 10);
321 schedule_neighbours_check(msecs
, 1);
324 if(babel_now
.tv_sec
>= expiry_time
) {
327 expiry_time
= babel_now
.tv_sec
+ roughly(30);
330 if(babel_now
.tv_sec
>= source_expiry_time
) {
332 source_expiry_time
= babel_now
.tv_sec
+ roughly(300);
335 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
336 babel_interface_nfo
*babel_ifp
= NULL
;
339 babel_ifp
= babel_get_if_nfo(ifp
);
340 if(timeval_compare(&babel_now
, &babel_ifp
->hello_timeout
) >= 0)
342 if(timeval_compare(&babel_now
, &babel_ifp
->update_timeout
) >= 0)
343 send_update(ifp
, 0, NULL
, 0);
344 if(timeval_compare(&babel_now
,
345 &babel_ifp
->update_flush_timeout
) >= 0)
349 if(resend_time
.tv_sec
!= 0) {
350 if(timeval_compare(&babel_now
, &resend_time
) >= 0)
354 if(unicast_flush_timeout
.tv_sec
!= 0) {
355 if(timeval_compare(&babel_now
, &unicast_flush_timeout
) >= 0)
359 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
360 babel_interface_nfo
*babel_ifp
= NULL
;
363 babel_ifp
= babel_get_if_nfo(ifp
);
364 if(babel_ifp
->flush_timeout
.tv_sec
!= 0) {
365 if(timeval_compare(&babel_now
, &babel_ifp
->flush_timeout
) >= 0)
371 assert(0); /* this line should never be reach */
375 printIfMin(struct timeval
*tv
, int cmd
, const char *tag
, const char *ifname
)
377 static struct timeval curr_tv
;
378 static char buffer
[200];
379 static const char *curr_tag
= NULL
;
382 case 0: /* reset timeval */
385 snprintf(buffer
, 200L, "interface: %s; %s", ifname
, tag
);
391 case 1: /* take the min */
392 if (tv
->tv_sec
== 0 && tv
->tv_usec
== 0) { /* if (tv == ∞) */
395 if (tv
->tv_sec
< curr_tv
.tv_sec
||(tv
->tv_sec
== curr_tv
.tv_sec
&&
396 tv
->tv_usec
< curr_tv
.tv_usec
)) {
399 snprintf(buffer
, 200L, "interface: %s; %s", ifname
, tag
);
406 case 2: /* print message */
407 debugf(BABEL_DEBUG_TIMEOUT
, "next timeout due to: %s", curr_tag
);
415 babel_fill_with_next_timeout(struct timeval
*tv
)
417 #if (defined NO_DEBUG)
418 #define printIfMin(a,b,c,d)
420 #define printIfMin(a,b,c,d) \
421 if (UNLIKELY(debug & BABEL_DEBUG_TIMEOUT)) {printIfMin(a,b,c,d);}
423 struct interface
*ifp
= NULL
;
424 struct listnode
*linklist_node
= NULL
;
426 *tv
= check_neighbours_timeout
;
427 printIfMin(tv
, 0, "check_neighbours_timeout", NULL
);
428 timeval_min_sec(tv
, expiry_time
);
429 printIfMin(tv
, 1, "expiry_time", NULL
);
430 timeval_min_sec(tv
, source_expiry_time
);
431 printIfMin(tv
, 1, "source_expiry_time", NULL
);
432 timeval_min(tv
, &resend_time
);
433 printIfMin(tv
, 1, "resend_time", NULL
);
434 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
435 babel_interface_nfo
*babel_ifp
= NULL
;
438 babel_ifp
= babel_get_if_nfo(ifp
);
439 timeval_min(tv
, &babel_ifp
->flush_timeout
);
440 printIfMin(tv
, 1, "flush_timeout", ifp
->name
);
441 timeval_min(tv
, &babel_ifp
->hello_timeout
);
442 printIfMin(tv
, 1, "hello_timeout", ifp
->name
);
443 timeval_min(tv
, &babel_ifp
->update_timeout
);
444 printIfMin(tv
, 1, "update_timeout", ifp
->name
);
445 timeval_min(tv
, &babel_ifp
->update_flush_timeout
);
446 printIfMin(tv
, 1, "update_flush_timeout",ifp
->name
);
448 timeval_min(tv
, &unicast_flush_timeout
);
449 printIfMin(tv
, 1, "unicast_flush_timeout", NULL
);
450 printIfMin(tv
, 2, NULL
, NULL
);
455 /* set the t_update thread of the babel routing process to be launch in
456 'timeout' (approximate at the milisecond) */
458 babel_set_timer(struct timeval
*timeout
)
460 long msecs
= timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000;
461 if (babel_routing_process
->t_update
!= NULL
) {
462 thread_cancel(babel_routing_process
->t_update
);
464 babel_routing_process
->t_update
=
465 thread_add_timer_msec(master
, &babel_main_loop
, NULL
, msecs
);
468 /* Schedule a neighbours check after roughly 3/2 times msecs have elapsed. */
470 schedule_neighbours_check(int msecs
, int override
)
472 struct timeval timeout
;
474 timeval_add_msec(&timeout
, &babel_now
, roughly(msecs
* 3 / 2));
476 check_neighbours_timeout
= timeout
;
478 timeval_min(&check_neighbours_timeout
, &timeout
);
482 resize_receive_buffer(int size
)
484 if(size
<= receive_buffer_size
)
487 if(receive_buffer
== NULL
) {
488 receive_buffer
= malloc(size
);
489 if(receive_buffer
== NULL
) {
490 zlog_err("malloc(receive_buffer): %s", safe_strerror(errno
));
493 receive_buffer_size
= size
;
496 new = realloc(receive_buffer
, size
);
498 zlog_err("realloc(receive_buffer): %s", safe_strerror(errno
));
501 receive_buffer
= new;
502 receive_buffer_size
= size
;
508 babel_distribute_update (struct distribute
*dist
)
510 struct interface
*ifp
;
511 babel_interface_nfo
*babel_ifp
;
512 struct access_list
*alist
;
513 struct prefix_list
*plist
;
518 ifp
= if_lookup_by_name (dist
->ifname
);
522 babel_ifp
= babel_get_if_nfo(ifp
);
524 if (dist
->list
[DISTRIBUTE_IN
]) {
525 alist
= access_list_lookup (AFI_IP6
, dist
->list
[DISTRIBUTE_IN
]);
527 babel_ifp
->list
[BABEL_FILTER_IN
] = alist
;
529 babel_ifp
->list
[BABEL_FILTER_IN
] = NULL
;
531 babel_ifp
->list
[BABEL_FILTER_IN
] = NULL
;
534 if (dist
->list
[DISTRIBUTE_OUT
]) {
535 alist
= access_list_lookup (AFI_IP6
, dist
->list
[DISTRIBUTE_OUT
]);
537 babel_ifp
->list
[BABEL_FILTER_OUT
] = alist
;
539 babel_ifp
->list
[BABEL_FILTER_OUT
] = NULL
;
541 babel_ifp
->list
[BABEL_FILTER_OUT
] = NULL
;
544 if (dist
->prefix
[DISTRIBUTE_IN
]) {
545 plist
= prefix_list_lookup (AFI_IP6
, dist
->prefix
[DISTRIBUTE_IN
]);
547 babel_ifp
->prefix
[BABEL_FILTER_IN
] = plist
;
549 babel_ifp
->prefix
[BABEL_FILTER_IN
] = NULL
;
551 babel_ifp
->prefix
[BABEL_FILTER_IN
] = NULL
;
554 if (dist
->prefix
[DISTRIBUTE_OUT
]) {
555 plist
= prefix_list_lookup (AFI_IP6
, dist
->prefix
[DISTRIBUTE_OUT
]);
557 babel_ifp
->prefix
[BABEL_FILTER_OUT
] = plist
;
559 babel_ifp
->prefix
[BABEL_FILTER_OUT
] = NULL
;
561 babel_ifp
->prefix
[BABEL_FILTER_OUT
] = NULL
;
566 babel_distribute_update_interface (struct interface
*ifp
)
568 struct distribute
*dist
;
570 dist
= distribute_lookup (ifp
->name
);
572 babel_distribute_update (dist
);
575 /* Update all interface's distribute list. */
577 babel_distribute_update_all (struct prefix_list
*notused
)
579 struct interface
*ifp
;
580 struct listnode
*node
;
582 for (ALL_LIST_ELEMENTS_RO (iflist
, node
, ifp
))
583 babel_distribute_update_interface (ifp
);
587 babel_distribute_update_all_wrapper (struct access_list
*notused
)
589 babel_distribute_update_all(NULL
);
597 "Enable a routing process\n"
598 "Make Babel instance command\n"
603 vty
->node
= BABEL_NODE
;
605 if (!babel_routing_process
) {
606 ret
= babel_create_routing_process ();
608 /* Notice to user we couldn't create Babel. */
610 zlog_warn ("can't create Babel");
619 DEFUN (no_router_babel
,
623 "Disable a routing process\n"
624 "Remove Babel instance command\n"
627 if(babel_routing_process
)
628 babel_clean_routing_process();
632 /* [Babel Command] */
633 DEFUN (babel_set_resend_delay
,
634 babel_set_resend_delay_cmd
,
635 "babel resend-delay <20-655340>",
637 "Time before resending a message\n"
642 VTY_GET_INTEGER_RANGE("milliseconds", interval
, argv
[0], 20, 10 * 0xFFFE);
644 resend_delay
= interval
;
649 babeld_quagga_init(void)
652 install_node(&cmd_babel_node
, &babel_config_write
);
654 install_element(CONFIG_NODE
, &router_babel_cmd
);
655 install_element(CONFIG_NODE
, &no_router_babel_cmd
);
657 install_default(BABEL_NODE
);
658 install_element(BABEL_NODE
, &babel_set_resend_delay_cmd
);
662 /* Access list install. */
664 access_list_add_hook (babel_distribute_update_all_wrapper
);
665 access_list_delete_hook (babel_distribute_update_all_wrapper
);
667 /* Prefix list initialize.*/
669 prefix_list_add_hook (babel_distribute_update_all
);
670 prefix_list_delete_hook (babel_distribute_update_all
);
672 /* Distribute list install. */
673 distribute_list_init (BABEL_NODE
);
674 distribute_list_add_hook (babel_distribute_update
);
675 distribute_list_delete_hook (babel_distribute_update
);
678 /* Stubs to adapt Babel's filtering calls to Quagga's infrastructure. */
681 input_filter(const unsigned char *id
,
682 const unsigned char *prefix
, unsigned short plen
,
683 const unsigned char *neigh
, unsigned int ifindex
)
685 return babel_filter(0, prefix
, plen
, ifindex
);
689 output_filter(const unsigned char *id
, const unsigned char *prefix
,
690 unsigned short plen
, unsigned int ifindex
)
692 return babel_filter(1, prefix
, plen
, ifindex
);
695 /* There's no redistribute filter in Quagga -- the zebra daemon does its
698 redistribute_filter(const unsigned char *prefix
, unsigned short plen
,
699 unsigned int ifindex
, int proto
)
705 show_babeld_configuration (struct vty
*vty
)
707 vty_out(vty
, "babeld running process %s.%s",
708 babel_routing_process
? "enable" : "disable", VTY_NEWLINE
);