]> git.proxmox.com Git - mirror_frr.git/blobdiff - babeld/babeld.c
Merge pull request #4027 from pguibert6WIND/fix_interface_rtadv
[mirror_frr.git] / babeld / babeld.c
index b2f8176aabeb341bd5b8ca7acf41a978efb8c172..39451b435a44ec59b7876dafcb2dbdf39cc0ac42 100644 (file)
@@ -29,6 +29,7 @@ THE SOFTWARE.
 #include "prefix.h"
 #include "filter.h"
 #include "plist.h"
+#include "lib_errors.h"
 
 #include "babel_main.h"
 #include "babeld.h"
@@ -43,6 +44,7 @@ THE SOFTWARE.
 #include "babel_filter.h"
 #include "babel_zebra.h"
 #include "babel_memory.h"
+#include "babel_errors.h"
 
 static int babel_init_routing_process(struct thread *thread);
 static void babel_get_myid(void);
@@ -51,7 +53,8 @@ static int babel_read_protocol (struct thread *thread);
 static int babel_main_loop(struct thread *thread);
 static void babel_set_timer(struct timeval *timeout);
 static void babel_fill_with_next_timeout(struct timeval *tv);
-
+static void
+babel_distribute_update (struct distribute_ctx *ctx, struct distribute *dist);
 
 /* Informations relative to the babel running daemon. */
 static struct babel *babel_routing_process = NULL;
@@ -76,6 +79,7 @@ static int
 babel_config_write (struct vty *vty)
 {
     int lines = 0;
+    int afi;
     int i;
 
     /* list enabled debug modes */
@@ -108,15 +112,19 @@ babel_config_write (struct vty *vty)
     /* list enabled interfaces */
     lines = 1 + babel_enable_if_config_write (vty);
     /* list redistributed protocols */
-    for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
-        if (i != zclient->redist_default &&
-           vrf_bitmap_check (zclient->redist[AFI_IP][i], VRF_DEFAULT))
-        {
-            vty_out (vty, " redistribute %s\n", zebra_route_string(i));
-            lines++;
+    for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
+        for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
+            if (i != zclient->redist_default &&
+                vrf_bitmap_check (zclient->redist[afi][i], VRF_DEFAULT)) {
+                vty_out (vty, " redistribute %s %s\n",
+                         (afi == AFI_IP) ? "ipv4" : "ipv6",
+                         zebra_route_string(i));
+                lines++;
+            }
         }
+    }
 
-    lines += config_write_distribute (vty);
+    lines += config_write_distribute (vty, babel_routing_process->distribute_ctx);
 
     return lines;
 }
@@ -138,7 +146,8 @@ babel_create_routing_process (void)
     /* Make socket for Babel protocol. */
     protocol_socket = babel_socket(protocol_port);
     if (protocol_socket < 0) {
-        zlog_err("Couldn't create link local socket: %s", safe_strerror(errno));
+        flog_err_sys(EC_LIB_SOCKET, "Couldn't create link local socket: %s",
+                 safe_strerror(errno));
         goto fail;
     }
 
@@ -146,8 +155,12 @@ babel_create_routing_process (void)
     thread_add_read(master, &babel_read_protocol, NULL, protocol_socket, &babel_routing_process->t_read);
     /* wait a little: zebra will announce interfaces, addresses, routes... */
     thread_add_timer_msec(master, babel_init_routing_process, NULL, 200L, &babel_routing_process->t_update);
-    return 0;
 
+    /* Distribute list install. */
+    babel_routing_process->distribute_ctx = distribute_list_ctx_create (vrf_lookup_by_id(VRF_DEFAULT));
+    distribute_list_add_hook (babel_routing_process->distribute_ctx, babel_distribute_update);
+    distribute_list_delete_hook (babel_routing_process->distribute_ctx, babel_distribute_update);
+    return 0;
 fail:
     XFREE(MTYPE_BABEL, babel_routing_process);
     babel_routing_process = NULL;
@@ -159,9 +172,9 @@ static int
 babel_read_protocol (struct thread *thread)
 {
     int rc;
+    struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
     struct interface *ifp = NULL;
     struct sockaddr_in6 sin6;
-    struct listnode *linklist_node = NULL;
 
     assert(babel_routing_process != NULL);
     assert(protocol_socket >= 0);
@@ -171,10 +184,10 @@ babel_read_protocol (struct thread *thread)
                     (struct sockaddr*)&sin6, sizeof(sin6));
     if(rc < 0) {
         if(errno != EAGAIN && errno != EINTR) {
-            zlog_err("recv: %s", safe_strerror(errno));
+            flog_err_sys(EC_LIB_SOCKET, "recv: %s", safe_strerror(errno));
         }
     } else {
-        FOR_ALL_INTERFACES(ifp, linklist_node) {
+        FOR_ALL_INTERFACES(vrf, ifp) {
             if(!if_up(ifp))
                 continue;
             if(ifp->ifindex == (ifindex_t)sin6.sin6_scope_id) {
@@ -209,8 +222,8 @@ babel_init_routing_process(struct thread *thread)
 static void
 babel_get_myid(void)
 {
+    struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
     struct interface *ifp = NULL;
-    struct listnode *linklist_node = NULL;
     int rc;
     int i;
 
@@ -219,12 +232,12 @@ babel_get_myid(void)
         return;
     }
 
-    FOR_ALL_INTERFACES(ifp, linklist_node) {
+    FOR_ALL_INTERFACES(vrf, ifp) {
         /* ifp->ifindex is not necessarily valid at this point */
         int ifindex = if_nametoindex(ifp->name);
         if(ifindex > 0) {
             unsigned char eui[8];
-            rc = if_eui64(ifp->name, ifindex, eui);
+            rc = if_eui64(ifindex, eui);
             if(rc < 0)
                 continue;
             memcpy(myid, eui, 8);
@@ -240,18 +253,20 @@ babel_get_myid(void)
         ifname = if_indextoname(i, buf);
         if(ifname == NULL)
             continue;
-        rc = if_eui64(ifname, i, eui);
+        rc = if_eui64(i, eui);
         if(rc < 0)
             continue;
         memcpy(myid, eui, 8);
         return;
     }
 
-    zlog_err("Warning: couldn't find router id -- using random value.");
+    flog_err(EC_BABEL_CONFIG,
+             "Warning: couldn't find router id -- using random value.");
 
     rc = read_random_bytes(myid, 8);
     if(rc < 0) {
-        zlog_err("read(random): %s (cannot assign an ID)",safe_strerror(errno));
+        flog_err(EC_BABEL_CONFIG, "read(random): %s (cannot assign an ID)",
+                 safe_strerror(errno));
         exit(1);
     }
     /* Clear group and global bits */
@@ -263,10 +278,10 @@ babel_get_myid(void)
 static void
 babel_initial_noise(void)
 {
+    struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
     struct interface *ifp = NULL;
-    struct listnode *linklist_node = NULL;
 
-    FOR_ALL_INTERFACES(ifp, linklist_node) {
+    FOR_ALL_INTERFACES(vrf, ifp) {
         if(!if_up(ifp))
             continue;
         /* Apply jitter before we send the first message. */
@@ -276,7 +291,7 @@ babel_initial_noise(void)
         send_wildcard_retraction(ifp);
     }
 
-    FOR_ALL_INTERFACES(ifp, linklist_node) {
+    FOR_ALL_INTERFACES(vrf, ifp) {
         if(!if_up(ifp))
             continue;
         usleep(roughly(10000));
@@ -292,7 +307,7 @@ babel_initial_noise(void)
 
 /* Delete all the added babel routes, make babeld only speak to zebra. */
 static void
-babel_clean_routing_process()
+babel_clean_routing_process(void)
 {
     flush_all_routes();
     babel_interface_close_all();
@@ -305,6 +320,7 @@ babel_clean_routing_process()
         thread_cancel(babel_routing_process->t_update);
     }
 
+    distribute_list_delete(&babel_routing_process->distribute_ctx);
     XFREE(MTYPE_BABEL, babel_routing_process);
     babel_routing_process = NULL;
 }
@@ -314,8 +330,8 @@ static int
 babel_main_loop(struct thread *thread)
 {
     struct timeval tv;
+    struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
     struct interface *ifp = NULL;
-    struct listnode *linklist_node = NULL;
 
     while(1) {
         gettime(&babel_now);
@@ -326,8 +342,8 @@ babel_main_loop(struct thread *thread)
         /* if there is no timeout, we must wait. */
         if(timeval_compare(&tv, &babel_now) > 0) {
             timeval_minus(&tv, &tv, &babel_now);
-            debugf(BABEL_DEBUG_TIMEOUT, "babel main loop : timeout: %ld msecs",
-                   tv.tv_sec * 1000 + tv.tv_usec / 1000);
+            debugf(BABEL_DEBUG_TIMEOUT, "babel main loop : timeout: %lld msecs",
+                   (long long)tv.tv_sec * 1000 + tv.tv_usec / 1000);
             /* it happens often to have less than 1 ms, it's bad. */
             timeval_add_msec(&tv, &tv, 300);
             babel_set_timer(&tv);
@@ -356,7 +372,7 @@ babel_main_loop(struct thread *thread)
             source_expiry_time = babel_now.tv_sec + roughly(300);
         }
 
-        FOR_ALL_INTERFACES(ifp, linklist_node) {
+        FOR_ALL_INTERFACES(vrf, ifp) {
             babel_interface_nfo *babel_ifp = NULL;
             if(!if_up(ifp))
                 continue;
@@ -380,7 +396,7 @@ babel_main_loop(struct thread *thread)
                 flush_unicast(1);
         }
 
-        FOR_ALL_INTERFACES(ifp, linklist_node) {
+        FOR_ALL_INTERFACES(vrf, ifp) {
             babel_interface_nfo *babel_ifp = NULL;
             if(!if_up(ifp))
                 continue;
@@ -444,8 +460,8 @@ babel_fill_with_next_timeout(struct timeval *tv)
 #define printIfMin(a,b,c,d) \
   if (UNLIKELY(debug & BABEL_DEBUG_TIMEOUT)) {printIfMin(a,b,c,d);}
 
+    struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
     struct interface *ifp = NULL;
-    struct listnode *linklist_node = NULL;
 
     *tv = check_neighbours_timeout;
     printIfMin(tv, 0, "check_neighbours_timeout", NULL);
@@ -455,7 +471,7 @@ babel_fill_with_next_timeout(struct timeval *tv)
     printIfMin(tv, 1, "source_expiry_time", NULL);
     timeval_min(tv, &resend_time);
     printIfMin(tv, 1, "resend_time", NULL);
-    FOR_ALL_INTERFACES(ifp, linklist_node) {
+    FOR_ALL_INTERFACES(vrf, ifp) {
         babel_interface_nfo *babel_ifp = NULL;
         if(!if_up(ifp))
             continue;
@@ -509,7 +525,8 @@ resize_receive_buffer(int size)
     if(receive_buffer == NULL) {
         receive_buffer = malloc(size);
         if(receive_buffer == NULL) {
-            zlog_err("malloc(receive_buffer): %s", safe_strerror(errno));
+            flog_err(EC_BABEL_MEMORY, "malloc(receive_buffer): %s",
+                     safe_strerror(errno));
             return -1;
         }
         receive_buffer_size = size;
@@ -517,7 +534,8 @@ resize_receive_buffer(int size)
         unsigned char *new;
         new = realloc(receive_buffer, size);
         if(new == NULL) {
-            zlog_err("realloc(receive_buffer): %s", safe_strerror(errno));
+            flog_err(EC_BABEL_MEMORY, "realloc(receive_buffer): %s",
+                     safe_strerror(errno));
             return -1;
         }
         receive_buffer = new;
@@ -527,7 +545,7 @@ resize_receive_buffer(int size)
 }
 
 static void
-babel_distribute_update (struct distribute *dist)
+babel_distribute_update (struct distribute_ctx *ctx, struct distribute *dist)
 {
     struct interface *ifp;
     babel_interface_nfo *babel_ifp;
@@ -562,21 +580,22 @@ babel_distribute_update (struct distribute *dist)
 static void
 babel_distribute_update_interface (struct interface *ifp)
 {
-    struct distribute *dist;
+    struct distribute *dist = NULL;
 
-    dist = distribute_lookup (ifp->name);
+    if (babel_routing_process)
+        dist = distribute_lookup(babel_routing_process->distribute_ctx, ifp->name);
     if (dist)
-        babel_distribute_update (dist);
+        babel_distribute_update (babel_routing_process->distribute_ctx, dist);
 }
 
 /* Update all interface's distribute list. */
 static void
 babel_distribute_update_all (struct prefix_list *notused)
 {
+    struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
     struct interface *ifp;
-    struct listnode *node;
 
-    for (ALL_LIST_ELEMENTS_RO (vrf_iflist(VRF_DEFAULT), node, ifp))
+    FOR_ALL_INTERFACES (vrf, ifp)
         babel_distribute_update_interface (ifp);
 }
 
@@ -724,9 +743,7 @@ babeld_quagga_init(void)
     prefix_list_delete_hook (babel_distribute_update_all);
 
     /* Distribute list install. */
-    distribute_list_init (BABEL_NODE);
-    distribute_list_add_hook (babel_distribute_update);
-    distribute_list_delete_hook (babel_distribute_update);
+    distribute_list_init(BABEL_NODE);
 }
 
 /* Stubs to adapt Babel's filtering calls to Quagga's infrastructure. */
@@ -755,3 +772,7 @@ redistribute_filter(const unsigned char *prefix, unsigned short plen,
     return 0;
 }
 
+struct babel *babel_lookup(void)
+{
+    return babel_routing_process;
+}