]> git.proxmox.com Git - mirror_frr.git/blobdiff - babeld/kernel.c
Merge pull request #5793 from ton31337/fix/formatting_show_bgp_summary_failed
[mirror_frr.git] / babeld / kernel.c
index da8fadac7cc2acc606153925ec1248119d01f453..d4c962af3b4390d38ce78ab57e1f742defe09003 100644 (file)
@@ -21,6 +21,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
 #include <sys/time.h>
 #include <sys/param.h>
 #include <time.h>
@@ -43,6 +47,7 @@ THE SOFTWARE.
 #include "vty.h"
 #include "memory.h"
 #include "thread.h"
+#include "nexthop.h"
 
 #include "util.h"
 #include "babel_interface.h"
@@ -50,13 +55,8 @@ THE SOFTWARE.
 
 
 static int
-kernel_route_v4(int add, const unsigned char *pref, unsigned short plen,
-                const unsigned char *gate, int ifindex,
-                unsigned int metric);
-static int
-kernel_route_v6(int add, const unsigned char *pref, unsigned short plen,
-                const unsigned char *gate, int ifindex,
-                unsigned int metric);
+zebra_route(int add, int familt, const unsigned char *pref, unsigned short plen,
+            const unsigned char *gate, int ifindex, unsigned int metric);
 
 int
 kernel_interface_operational(struct interface *interface)
@@ -77,13 +77,13 @@ kernel_interface_wireless(struct interface *interface)
 }
 
 int
-kernel_route(int operation, const unsigned char *pref, unsigned short plen,
-             const unsigned char *gate, int ifindex, unsigned int metric,
-             const unsigned char *newgate, int newifindex,
+kernel_route(enum babel_kernel_routes operation, const unsigned char *pref,
+            unsigned short plen, const unsigned char *gate, int ifindex,
+            unsigned int metric, const unsigned char *newgate, int newifindex,
              unsigned int newmetric)
 {
     int rc;
-    int ipv4;
+    int family;
 
     /* Check that the protocol family is consistent. */
     if(plen >= 96 && v4mapped(pref)) {
@@ -91,172 +91,129 @@ kernel_route(int operation, const unsigned char *pref, unsigned short plen,
             errno = EINVAL;
             return -1;
         }
-        ipv4 = 1;
+        family = AF_INET;
     } else {
         if(v4mapped(gate)) {
             errno = EINVAL;
             return -1;
         }
-        ipv4 = 0;
+        family = AF_INET6;
     }
 
     switch (operation) {
         case ROUTE_ADD:
-            return ipv4 ?
-                   kernel_route_v4(1, pref, plen, gate, ifindex, metric):
-                   kernel_route_v6(1, pref, plen, gate, ifindex, metric);
+            return zebra_route(1, family, pref, plen, gate, ifindex, metric);
             break;
         case ROUTE_FLUSH:
-            return ipv4 ?
-                   kernel_route_v4(0, pref, plen, gate, ifindex, metric):
-                   kernel_route_v6(0, pref, plen, gate, ifindex, metric);
+            return zebra_route(0, family, pref, plen, gate, ifindex, metric);
             break;
         case ROUTE_MODIFY:
             if(newmetric == metric && memcmp(newgate, gate, 16) == 0 &&
                newifindex == ifindex)
                 return 0;
             debugf(BABEL_DEBUG_ROUTE, "Modify route: delete old; add new.");
-            rc = ipv4 ?
-                kernel_route_v4(0, pref, plen, gate, ifindex, metric):
-                kernel_route_v6(0, pref, plen, gate, ifindex, metric);
-
+            rc = zebra_route(0, family, pref, plen, gate, ifindex, metric);
             if (rc < 0)
                 return -1;
 
-            rc = ipv4 ?
-                kernel_route_v4(1, pref, plen, newgate, newifindex, newmetric):
-                kernel_route_v6(1, pref, plen, newgate, newifindex, newmetric);
-
+            rc = zebra_route(1, family, pref, plen, newgate, newifindex,
+                             newmetric);
             return rc;
             break;
-        default:
-            zlog_err("this should never appens (false value - kernel_route)");
-            assert(0);
-            exit(1);
-            break;
-    }
-}
-
-static int
-kernel_route_v4(int add,
-                const unsigned char *pref, unsigned short plen,
-                const unsigned char *gate, int ifindex, unsigned int metric)
-{
-    struct zapi_ipv4 api;               /* quagga's communication system */
-    struct prefix_ipv4 quagga_prefix;   /* quagga's prefix */
-    struct in_addr babel_prefix_addr;   /* babeld's prefix addr */
-    struct in_addr nexthop;             /* next router to go */
-    struct in_addr *nexthop_pointer = &nexthop; /* it's an array! */
-
-    /* convert to be understandable by quagga */
-    /* convert given addresses */
-    uchar_to_inaddr(&babel_prefix_addr, pref);
-    uchar_to_inaddr(&nexthop, gate);
-
-    /* make prefix structure */
-    memset (&quagga_prefix, 0, sizeof(quagga_prefix));
-    quagga_prefix.family = AF_INET;
-    IPV4_ADDR_COPY (&quagga_prefix.prefix, &babel_prefix_addr);
-    quagga_prefix.prefixlen = plen - 96; /* our plen is for v4mapped's addr */
-    apply_mask_ipv4(&quagga_prefix);
-
-    memset(&api, 0, sizeof(api));
-    api.type  = ZEBRA_ROUTE_BABEL;
-    api.flags = 0;
-    api.message = 0;
-    api.instance = 0;
-    api.safi = SAFI_UNICAST;
-    api.vrf_id = VRF_DEFAULT;
-
-    SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
-    SET_FLAG(api.message, ZAPI_MESSAGE_IFINDEX);
-    if(metric >= KERNEL_INFINITY) {
-        api.flags = ZEBRA_FLAG_REJECT;
-        api.nexthop_num = 0;
-       api.ifindex_num = 0;
-    } else {
-        api.nexthop_num = 1;
-       api.ifindex_num = 1;
-        api.nexthop = &nexthop_pointer;
-       api.ifindex = &ifindex;
-        SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
-        api.metric = metric;
     }
 
-    debugf(BABEL_DEBUG_ROUTE, "%s route (ipv4) to zebra",
-           add ? "adding" : "removing" );
-    return zapi_ipv4_route (add ? ZEBRA_IPV4_ROUTE_ADD :
-                                  ZEBRA_IPV4_ROUTE_DELETE,
-                            zclient, &quagga_prefix, &api);
+    return 0;
 }
 
 static int
-kernel_route_v6(int add, const unsigned char *pref, unsigned short plen,
-                const unsigned char *gate, int ifindex, unsigned int metric)
+zebra_route(int add, int family, const unsigned char *pref, unsigned short plen,
+            const unsigned char *gate, int ifindex, unsigned int metric)
 {
-    ifindex_t tmp_ifindex = ifindex;    /* (for typing) */
-    struct zapi_ipv6 api;               /* quagga's communication system */
-    struct prefix_ipv6 quagga_prefix;   /* quagga's prefix */
-    struct in6_addr babel_prefix_addr;  /* babeld's prefix addr */
-    struct in6_addr nexthop;            /* next router to go */
-    struct in6_addr *nexthop_pointer = &nexthop;
+    struct zapi_route api;               /* quagga's communication system */
+    struct prefix quagga_prefix;         /* quagga's prefix */
+    union g_addr babel_prefix_addr;      /* babeld's prefix addr */
+    struct zapi_nexthop *api_nh;         /* next router to go - no ECMP */
+
+    api_nh = &api.nexthops[0];
 
     /* convert to be understandable by quagga */
     /* convert given addresses */
-    uchar_to_in6addr(&babel_prefix_addr, pref);
-    uchar_to_in6addr(&nexthop, gate);
+    switch (family) {
+    case AF_INET:
+        uchar_to_inaddr(&babel_prefix_addr.ipv4, pref);
+        break;
+    case AF_INET6:
+        uchar_to_in6addr(&babel_prefix_addr.ipv6, pref);
+        break;
+    }
 
     /* make prefix structure */
     memset (&quagga_prefix, 0, sizeof(quagga_prefix));
-    quagga_prefix.family = AF_INET6;
-    IPV6_ADDR_COPY (&quagga_prefix.prefix, &babel_prefix_addr);
-    quagga_prefix.prefixlen = plen;
-    apply_mask_ipv6(&quagga_prefix);
+    quagga_prefix.family = family;
+    switch (family) {
+    case AF_INET:
+        IPV4_ADDR_COPY (&quagga_prefix.u.prefix4, &babel_prefix_addr.ipv4);
+        /* our plen is for v4mapped's addr */
+        quagga_prefix.prefixlen = plen - 96;
+        break;
+    case AF_INET6:
+        IPV6_ADDR_COPY (&quagga_prefix.u.prefix6, &babel_prefix_addr.ipv6);
+        quagga_prefix.prefixlen = plen;
+        break;
+    }
+    apply_mask(&quagga_prefix);
 
     memset(&api, 0, sizeof(api));
     api.type  = ZEBRA_ROUTE_BABEL;
-    api.flags = 0;
-    api.message = 0;
-    api.instance = 0;
     api.safi = SAFI_UNICAST;
     api.vrf_id = VRF_DEFAULT;
+    api.prefix = quagga_prefix;
 
     if(metric >= KERNEL_INFINITY) {
-        api.flags = ZEBRA_FLAG_REJECT;
-        api.nexthop_num = 0;
-        api.ifindex_num = 0;
+       zapi_route_set_blackhole(&api, BLACKHOLE_REJECT);
     } else {
         SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
         api.nexthop_num = 1;
-        api.nexthop = &nexthop_pointer;
-        SET_FLAG(api.message, ZAPI_MESSAGE_IFINDEX);
-        api.ifindex_num = 1;
-        api.ifindex = &tmp_ifindex;
+        api_nh->ifindex = ifindex;
+       api_nh->vrf_id = VRF_DEFAULT;
+       switch (family) {
+        case AF_INET:
+            uchar_to_inaddr(&api_nh->gate.ipv4, gate);
+            if (IPV4_ADDR_SAME (&api_nh->gate.ipv4, &quagga_prefix.u.prefix4) &&
+                    quagga_prefix.prefixlen == 32) {
+                api_nh->type = NEXTHOP_TYPE_IFINDEX;
+            } else {
+                api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
+            }
+            break;
+        case AF_INET6:
+            uchar_to_in6addr(&api_nh->gate.ipv6, gate);
+            /* difference to IPv4: always leave the linklocal as nexthop */
+            api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
+            break;
+        }
         SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
         api.metric = metric;
     }
 
-    debugf(BABEL_DEBUG_ROUTE, "%s route (ipv6) to zebra",
-           add ? "adding" : "removing" );
-    return zapi_ipv6_route (add ? ZEBRA_IPV6_ROUTE_ADD :
-                           ZEBRA_IPV6_ROUTE_DELETE,
-                            zclient, &quagga_prefix, NULL, &api);
+    debugf(BABEL_DEBUG_ROUTE, "%s route (%s) to zebra",
+           add ? "adding" : "removing",
+           (family == AF_INET) ? "ipv4" : "ipv6");
+    return zclient_route_send (add ? ZEBRA_ROUTE_ADD : ZEBRA_ROUTE_DELETE,
+                               zclient, &api);
 }
 
 int
-if_eui64(char *ifname, int ifindex, unsigned char *eui)
+if_eui64(int ifindex, unsigned char *eui)
 {
     struct interface *ifp = if_lookup_by_index(ifindex, VRF_DEFAULT);
     if (ifp == NULL) {
         return -1;
     }
-#ifdef HAVE_STRUCT_SOCKADDR_DL
-    u_char len = ifp->sdl.sdl_alen;
-    char *tmp = ifp->sdl.sdl_data + ifp->sdl.sdl_nlen;
-#else
-    u_char len = (u_char) ifp->hw_addr_len;
+
+    uint8_t len = (uint8_t)ifp->hw_addr_len;
     char *tmp = (void*) ifp->hw_addr;
-#endif
+
     if (len == 8) {
         memcpy(eui, tmp, 8);
         eui[0] ^= 2;