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
)
177 debugf(BABEL_DEBUG_COMMON
, "My ID is : %s.", format_eui64(myid
));
178 babel_initial_noise();
179 babel_main_loop(thread
);/* this function self-add to the t_update thread */
183 /* fill "myid" with an unique id (only if myid != {0}). */
187 struct interface
*ifp
= NULL
;
188 struct listnode
*linklist_node
= NULL
;
192 /* if we already have an id (from state file), we return. */
193 if (memcmp(myid
, zeroes
, 8) != 0) {
197 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
198 /* ifp->ifindex is not necessarily valid at this point */
199 int ifindex
= if_nametoindex(ifp
->name
);
201 unsigned char eui
[8];
202 rc
= if_eui64(ifp
->name
, ifindex
, eui
);
205 memcpy(myid
, eui
, 8);
210 /* We failed to get a global EUI64 from the interfaces we were given.
211 Let's try to find an interface with a MAC address. */
212 for(i
= 1; i
< 256; i
++) {
213 char buf
[IF_NAMESIZE
], *ifname
;
214 unsigned char eui
[8];
215 ifname
= if_indextoname(i
, buf
);
218 rc
= if_eui64(ifname
, i
, eui
);
221 memcpy(myid
, eui
, 8);
225 zlog_err("Warning: couldn't find router id -- using random value.");
227 rc
= read_random_bytes(myid
, 8);
229 zlog_err("read(random): %s (cannot assign an ID)",safe_strerror(errno
));
232 /* Clear group and global bits */
236 /* Make some noise so that others notice us, and send retractions in
237 case we were restarted recently */
239 babel_initial_noise(void)
241 struct interface
*ifp
= NULL
;
242 struct listnode
*linklist_node
= NULL
;
244 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
247 /* Apply jitter before we send the first message. */
248 usleep(roughly(10000));
251 send_wildcard_retraction(ifp
);
254 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
257 usleep(roughly(10000));
260 send_wildcard_retraction(ifp
);
261 send_self_update(ifp
);
262 send_request(ifp
, NULL
, 0);
268 /* Delete all the added babel routes, make babeld only speak to zebra. */
270 babel_clean_routing_process()
272 babel_uninstall_all_routes();
273 babel_interface_close_all();
276 if (babel_routing_process
->t_read
!= NULL
) {
277 thread_cancel(babel_routing_process
->t_read
);
279 if (babel_routing_process
->t_update
!= NULL
) {
280 thread_cancel(babel_routing_process
->t_update
);
283 XFREE(MTYPE_BABEL
, babel_routing_process
);
284 babel_routing_process
= NULL
;
287 /* Function used with timeout. */
289 babel_main_loop(struct thread
*thread
)
292 struct interface
*ifp
= NULL
;
293 struct listnode
*linklist_node
= NULL
;
298 /* timeouts --------------------------------------------------------- */
299 /* get the next timeout */
300 babel_fill_with_next_timeout(&tv
);
301 /* if there is no timeout, we must wait. */
302 if(timeval_compare(&tv
, &babel_now
) > 0) {
303 timeval_minus(&tv
, &tv
, &babel_now
);
304 debugf(BABEL_DEBUG_TIMEOUT
, "babel main loop : timeout: %ld msecs",
305 tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000);
306 /* it happens often to have less than 1 ms, it's bad. */
307 timeval_add_msec(&tv
, &tv
, 300);
308 babel_set_timer(&tv
);
314 /* update database -------------------------------------------------- */
315 if(timeval_compare(&check_neighbours_timeout
, &babel_now
) < 0) {
317 msecs
= check_neighbours();
318 msecs
= MAX(msecs
, 10);
319 schedule_neighbours_check(msecs
, 1);
322 if(babel_now
.tv_sec
>= expiry_time
) {
325 expiry_time
= babel_now
.tv_sec
+ roughly(30);
328 if(babel_now
.tv_sec
>= source_expiry_time
) {
330 source_expiry_time
= babel_now
.tv_sec
+ roughly(300);
333 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
334 babel_interface_nfo
*babel_ifp
= NULL
;
337 babel_ifp
= babel_get_if_nfo(ifp
);
338 if(timeval_compare(&babel_now
, &babel_ifp
->hello_timeout
) >= 0)
340 if(timeval_compare(&babel_now
, &babel_ifp
->update_timeout
) >= 0)
341 send_update(ifp
, 0, NULL
, 0);
342 if(timeval_compare(&babel_now
,
343 &babel_ifp
->update_flush_timeout
) >= 0)
347 if(resend_time
.tv_sec
!= 0) {
348 if(timeval_compare(&babel_now
, &resend_time
) >= 0)
352 if(unicast_flush_timeout
.tv_sec
!= 0) {
353 if(timeval_compare(&babel_now
, &unicast_flush_timeout
) >= 0)
357 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
358 babel_interface_nfo
*babel_ifp
= NULL
;
361 babel_ifp
= babel_get_if_nfo(ifp
);
362 if(babel_ifp
->flush_timeout
.tv_sec
!= 0) {
363 if(timeval_compare(&babel_now
, &babel_ifp
->flush_timeout
) >= 0)
369 assert(0); /* this line should never be reach */
373 printIfMin(struct timeval
*tv
, int cmd
, const char *tag
, const char *ifname
)
375 static struct timeval curr_tv
;
376 static char buffer
[200];
377 static const char *curr_tag
= NULL
;
380 case 0: /* reset timeval */
383 snprintf(buffer
, 200L, "interface: %s; %s", ifname
, tag
);
389 case 1: /* take the min */
390 if (tv
->tv_sec
== 0 && tv
->tv_usec
== 0) { /* if (tv == ∞) */
393 if (tv
->tv_sec
< curr_tv
.tv_sec
||(tv
->tv_sec
== curr_tv
.tv_sec
&&
394 tv
->tv_usec
< curr_tv
.tv_usec
)) {
397 snprintf(buffer
, 200L, "interface: %s; %s", ifname
, tag
);
404 case 2: /* print message */
405 debugf(BABEL_DEBUG_TIMEOUT
, "next timeout due to: %s", curr_tag
);
413 babel_fill_with_next_timeout(struct timeval
*tv
)
415 #if (defined NO_DEBUG)
416 #define printIfMin(a,b,c,d)
418 #define printIfMin(a,b,c,d) \
419 if (UNLIKELY(debug & BABEL_DEBUG_TIMEOUT)) {printIfMin(a,b,c,d);}
421 struct interface
*ifp
= NULL
;
422 struct listnode
*linklist_node
= NULL
;
424 *tv
= check_neighbours_timeout
;
425 printIfMin(tv
, 0, "check_neighbours_timeout", NULL
);
426 timeval_min_sec(tv
, expiry_time
);
427 printIfMin(tv
, 1, "expiry_time", NULL
);
428 timeval_min_sec(tv
, source_expiry_time
);
429 printIfMin(tv
, 1, "source_expiry_time", NULL
);
430 timeval_min(tv
, &resend_time
);
431 printIfMin(tv
, 1, "resend_time", NULL
);
432 FOR_ALL_INTERFACES(ifp
, linklist_node
) {
433 babel_interface_nfo
*babel_ifp
= NULL
;
436 babel_ifp
= babel_get_if_nfo(ifp
);
437 timeval_min(tv
, &babel_ifp
->flush_timeout
);
438 printIfMin(tv
, 1, "flush_timeout", ifp
->name
);
439 timeval_min(tv
, &babel_ifp
->hello_timeout
);
440 printIfMin(tv
, 1, "hello_timeout", ifp
->name
);
441 timeval_min(tv
, &babel_ifp
->update_timeout
);
442 printIfMin(tv
, 1, "update_timeout", ifp
->name
);
443 timeval_min(tv
, &babel_ifp
->update_flush_timeout
);
444 printIfMin(tv
, 1, "update_flush_timeout",ifp
->name
);
446 timeval_min(tv
, &unicast_flush_timeout
);
447 printIfMin(tv
, 1, "unicast_flush_timeout", NULL
);
448 printIfMin(tv
, 2, NULL
, NULL
);
453 /* set the t_update thread of the babel routing process to be launch in
454 'timeout' (approximate at the milisecond) */
456 babel_set_timer(struct timeval
*timeout
)
458 long msecs
= timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000;
459 if (babel_routing_process
->t_update
!= NULL
) {
460 thread_cancel(babel_routing_process
->t_update
);
462 babel_routing_process
->t_update
=
463 thread_add_timer_msec(master
, &babel_main_loop
, NULL
, msecs
);
466 /* Schedule a neighbours check after roughly 3/2 times msecs have elapsed. */
468 schedule_neighbours_check(int msecs
, int override
)
470 struct timeval timeout
;
472 timeval_add_msec(&timeout
, &babel_now
, roughly(msecs
* 3 / 2));
474 check_neighbours_timeout
= timeout
;
476 timeval_min(&check_neighbours_timeout
, &timeout
);
480 resize_receive_buffer(int size
)
482 if(size
<= receive_buffer_size
)
485 if(receive_buffer
== NULL
) {
486 receive_buffer
= malloc(size
);
487 if(receive_buffer
== NULL
) {
488 zlog_err("malloc(receive_buffer): %s", safe_strerror(errno
));
491 receive_buffer_size
= size
;
494 new = realloc(receive_buffer
, size
);
496 zlog_err("realloc(receive_buffer): %s", safe_strerror(errno
));
499 receive_buffer
= new;
500 receive_buffer_size
= size
;
506 babel_distribute_update (struct distribute
*dist
)
508 struct interface
*ifp
;
509 babel_interface_nfo
*babel_ifp
;
510 struct access_list
*alist
;
511 struct prefix_list
*plist
;
516 ifp
= if_lookup_by_name (dist
->ifname
);
520 babel_ifp
= babel_get_if_nfo(ifp
);
522 if (dist
->list
[DISTRIBUTE_IN
]) {
523 alist
= access_list_lookup (AFI_IP6
, dist
->list
[DISTRIBUTE_IN
]);
525 babel_ifp
->list
[BABEL_FILTER_IN
] = alist
;
527 babel_ifp
->list
[BABEL_FILTER_IN
] = NULL
;
529 babel_ifp
->list
[BABEL_FILTER_IN
] = NULL
;
532 if (dist
->list
[DISTRIBUTE_OUT
]) {
533 alist
= access_list_lookup (AFI_IP6
, dist
->list
[DISTRIBUTE_OUT
]);
535 babel_ifp
->list
[BABEL_FILTER_OUT
] = alist
;
537 babel_ifp
->list
[BABEL_FILTER_OUT
] = NULL
;
539 babel_ifp
->list
[BABEL_FILTER_OUT
] = NULL
;
542 if (dist
->prefix
[DISTRIBUTE_IN
]) {
543 plist
= prefix_list_lookup (AFI_IP6
, dist
->prefix
[DISTRIBUTE_IN
]);
545 babel_ifp
->prefix
[BABEL_FILTER_IN
] = plist
;
547 babel_ifp
->prefix
[BABEL_FILTER_IN
] = NULL
;
549 babel_ifp
->prefix
[BABEL_FILTER_IN
] = NULL
;
552 if (dist
->prefix
[DISTRIBUTE_OUT
]) {
553 plist
= prefix_list_lookup (AFI_IP6
, dist
->prefix
[DISTRIBUTE_OUT
]);
555 babel_ifp
->prefix
[BABEL_FILTER_OUT
] = plist
;
557 babel_ifp
->prefix
[BABEL_FILTER_OUT
] = NULL
;
559 babel_ifp
->prefix
[BABEL_FILTER_OUT
] = NULL
;
564 babel_distribute_update_interface (struct interface
*ifp
)
566 struct distribute
*dist
;
568 dist
= distribute_lookup (ifp
->name
);
570 babel_distribute_update (dist
);
573 /* Update all interface's distribute list. */
575 babel_distribute_update_all (struct prefix_list
*notused
)
577 struct interface
*ifp
;
578 struct listnode
*node
;
580 for (ALL_LIST_ELEMENTS_RO (iflist
, node
, ifp
))
581 babel_distribute_update_interface (ifp
);
585 babel_distribute_update_all_wrapper (struct access_list
*notused
)
587 babel_distribute_update_all(NULL
);
595 "Enable a routing process\n"
596 "Make Babel instance command\n")
600 vty
->node
= BABEL_NODE
;
602 if (!babel_routing_process
) {
603 ret
= babel_create_routing_process ();
605 /* Notice to user we couldn't create Babel. */
607 zlog_warn ("can't create Babel");
615 DEFUN (no_router_babel
,
619 "Disable a routing process\n"
620 "Remove Babel instance command\n")
622 if(babel_routing_process
)
623 babel_clean_routing_process();
627 /* [Babel Command] */
628 DEFUN (babel_set_protocol_group
,
629 babel_set_protocol_group_cmd
,
630 "protocol group ADDR",
631 "Set the protocol group, default is ff02::1:6.\n"
637 ret
= str2prefix (argv
[0], &p
);
639 /* Given string is: */
640 if (ret
) { /* an IPv4 or v6 network */
641 if (p
.family
!= AF_INET6
) {
644 in6addr_to_uchar(protocol_group
, &p
.u
.prefix6
);
645 } else { /* an interface name */
650 vty_out (vty
, "%s must be an ipv6 address%s", argv
[0],
658 /* [Babel Command] */
659 DEFUN (babel_set_protocol_port
,
660 babel_set_protocol_port_cmd
,
661 "protocol port <1-65535>",
662 "Set the protocol port (default is defined in RFC).\n"
665 int port
= atoi(argv
[0]);
666 protocol_port
= port
;
673 babeld_quagga_init(void)
676 install_node(&cmd_babel_node
, &babel_config_write
);
678 install_element(CONFIG_NODE
, &router_babel_cmd
);
679 install_element(CONFIG_NODE
, &no_router_babel_cmd
);
681 install_default(BABEL_NODE
);
685 /* Access list install. */
687 access_list_add_hook (babel_distribute_update_all_wrapper
);
688 access_list_delete_hook (babel_distribute_update_all_wrapper
);
690 /* Prefix list initialize.*/
692 prefix_list_add_hook (babel_distribute_update_all
);
693 prefix_list_delete_hook (babel_distribute_update_all
);
695 /* Distribute list install. */
696 distribute_list_init (BABEL_NODE
);
697 distribute_list_add_hook (babel_distribute_update
);
698 distribute_list_delete_hook (babel_distribute_update
);
701 int /* DEPRECATED: for compatibility with old babeld (configuration.{c,h})*/
702 input_filter(const unsigned char *id
,
703 const unsigned char *prefix
, unsigned short plen
,
704 const unsigned char *neigh
, unsigned int ifindex
)
706 struct interface
*ifp
= NULL
;
708 p
.family
= v4mapped(prefix
) ? AF_INET
: AF_INET6
;
710 if (p
.family
== AF_INET
) {
711 uchar_to_inaddr(&p
.u
.prefix4
, prefix
);
713 uchar_to_in6addr(&p
.u
.prefix6
, prefix
);
716 ifp
= if_lookup_by_index(ifindex
);
718 return babel_filter_in(&p
, babel_get_if_nfo(ifp
));
721 return babel_filter_in(&p
, NULL
);
724 int /* DEPRECATED: for compatibility with old babeld */
725 output_filter(const unsigned char *id
, const unsigned char *prefix
,
726 unsigned short plen
, unsigned int ifindex
)
728 struct interface
*ifp
= NULL
;
730 p
.family
= v4mapped(prefix
) ? AF_INET
: AF_INET6
;
732 if (p
.family
== AF_INET
) {
733 uchar_to_inaddr(&p
.u
.prefix4
, prefix
);
735 uchar_to_in6addr(&p
.u
.prefix6
, prefix
);
738 ifp
= if_lookup_by_index(ifindex
);
740 return babel_filter_out(&p
, babel_get_if_nfo(ifp
));
743 return babel_filter_out(&p
, NULL
);
746 int /* DEPRECATED: for compatibility with old babeld */
747 redistribute_filter(const unsigned char *prefix
, unsigned short plen
,
748 unsigned int ifindex
, int proto
)
750 struct interface
*ifp
= NULL
;
752 p
.family
= v4mapped(prefix
) ? AF_INET
: AF_INET6
;
754 if (p
.family
== AF_INET
) {
755 uchar_to_inaddr(&p
.u
.prefix4
, prefix
);
757 uchar_to_in6addr(&p
.u
.prefix6
, prefix
);
760 ifp
= if_lookup_by_index(ifindex
);
762 return babel_filter_redistribute(&p
,babel_get_if_nfo(ifp
));
765 return babel_filter_redistribute(&p
, NULL
);