]> git.proxmox.com Git - mirror_frr.git/blobdiff - zebra/kernel_socket.c
Merge commit 'remotes/jocke/ospf_neighbour'; commit 'remotes/jocke/snmp_crosscompile'
[mirror_frr.git] / zebra / kernel_socket.c
index 9027cd6a1f43a6c611e9a73fb3e1df26ee6fba5e..e77b9b78b2ffac09e18ef00026696126e5894fd6 100644 (file)
@@ -36,6 +36,7 @@
 #include "zebra/interface.h"
 #include "zebra/zserv.h"
 #include "zebra/debug.h"
+#include "zebra/kernel_socket.h"
 
 extern struct zebra_privs_t zserv_privs;
 extern struct zebra_t zebrad;
@@ -55,7 +56,7 @@ extern struct zebra_t zebrad;
  * Given a pointer (sockaddr or void *), return the number of bytes
  * taken up by the sockaddr and any padding needed for alignment.
  */
-#if defined(HAVE_SA_LEN)
+#if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
 #define SAROUNDUP(X)   ROUNDUP(((struct sockaddr *)(X))->sa_len)
 #elif defined(HAVE_IPV6)
 /*
@@ -75,10 +76,57 @@ extern struct zebra_t zebrad;
         ROUNDUP(sizeof(struct sockaddr_in)):\
          (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
            ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr)))
-#endif /* HAVE_SA_LEN */
+#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
 
+/* We use an additional pointer in following, pdest, rather than (DEST)
+ * directly, because gcc will warn if the macro is expanded and DEST is NULL,
+ * complaining that memcpy is being passed a NULL value, despite the fact
+ * the if (NULL) makes it impossible.
+ */
+#define RTA_ADDR_GET(DEST, RTA, RTMADDRS, PNT) \
+  if ((RTMADDRS) & (RTA)) \
+    { \
+      void *pdest = (DEST); \
+      int len = SAROUNDUP ((PNT)); \
+      if ( ((DEST) != NULL) && \
+           af_check (((struct sockaddr *)(PNT))->sa_family)) \
+        memcpy (pdest, (PNT), len); \
+      (PNT) += len; \
+    }
+#define RTA_ATTR_GET(DEST, RTA, RTMADDRS, PNT) \
+  if ((RTMADDRS) & (RTA)) \
+    { \
+      void *pdest = (DEST); \
+      int len = SAROUNDUP ((PNT)); \
+      if ((DEST) != NULL) \
+        memcpy (pdest, (PNT), len); \
+      (PNT) += len; \
+    }
+
+#define RTA_NAME_GET(DEST, RTA, RTMADDRS, PNT, LEN) \
+  if ((RTMADDRS) & (RTA)) \
+    { \
+      u_char *pdest = (u_char *) (DEST); \
+      int len = SAROUNDUP ((PNT)); \
+      struct sockaddr_dl *sdl = (struct sockaddr_dl *)(PNT); \
+      if (IS_ZEBRA_DEBUG_KERNEL) \
+        zlog_debug ("%s: RTA_SDL_GET nlen %d, alen %d", \
+                    __func__, sdl->sdl_nlen, sdl->sdl_alen); \
+      if ( ((DEST) != NULL) && (sdl->sdl_family == AF_LINK) \
+           && (sdl->sdl_nlen < IFNAMSIZ) && (sdl->sdl_nlen <= len) ) \
+        { \
+          memcpy (pdest, sdl->sdl_data, sdl->sdl_nlen); \
+          pdest[sdl->sdl_nlen] = '\0'; \
+          (LEN) = sdl->sdl_nlen; \
+        } \
+      (PNT) += len; \
+    } \
+  else \
+    { \
+      (LEN) = 0; \
+    }
 /* Routing socket message types. */
-struct message rtm_type_str[] =
+const struct message rtm_type_str[] =
 {
   {RTM_ADD,      "RTM_ADD"},
   {RTM_DELETE,   "RTM_DELETE"},
@@ -88,8 +136,12 @@ struct message rtm_type_str[] =
   {RTM_REDIRECT, "RTM_REDIRECT"},
   {RTM_MISS,     "RTM_MISS"},
   {RTM_LOCK,     "RTM_LOCK"},
+#ifdef OLDADD
   {RTM_OLDADD,   "RTM_OLDADD"},
+#endif /* RTM_OLDADD */
+#ifdef RTM_OLDDEL
   {RTM_OLDDEL,   "RTM_OLDDEL"},
+#endif /* RTM_OLDDEL */
   {RTM_RESOLVE,  "RTM_RESOLVE"},
   {RTM_NEWADDR,  "RTM_NEWADDR"},
   {RTM_DELADDR,  "RTM_DELADDR"},
@@ -126,6 +178,9 @@ struct message rtm_flag_str[] =
   {RTF_LLINFO,    "LLINFO"},
   {RTF_STATIC,    "STATIC"},
   {RTF_BLACKHOLE, "BLACKHOLE"},
+#ifdef RTF_PRIVATE
+  {RTF_PRIVATE,          "PRIVATE"},
+#endif /* RTF_PRIVATE */
   {RTF_PROTO1,    "PROTO1"},
   {RTF_PROTO2,    "PROTO2"},
 #ifdef RTF_PRCLONING
@@ -149,6 +204,12 @@ struct message rtm_flag_str[] =
 #ifdef RTF_MULTICAST
   {RTF_MULTICAST, "MULTICAST"},
 #endif /* RTF_MULTICAST */
+#ifdef RTF_MULTIRT
+  {RTF_MULTIRT,   "MULTIRT"},
+#endif /* RTF_MULTIRT */
+#ifdef RTF_SETSRC
+  {RTF_SETSRC,    "SETSRC"},
+#endif /* RTF_SETSRC */
   {0,             NULL}
 };
 
@@ -159,7 +220,7 @@ int routing_sock = -1;
 /* #define DEBUG */
 
 /* Supported address family check. */
-static int
+static int inline
 af_check (int family)
 {
   if (family == AF_INET)
@@ -192,37 +253,61 @@ rtm_flag_dump (int flag)
 
 #ifdef RTM_IFANNOUNCE
 /* Interface adding function */
-int
+static int
 ifan_read (struct if_announcemsghdr *ifan)
 {
   struct interface *ifp;
-
+  
   ifp = if_lookup_by_index (ifan->ifan_index);
-  if (ifp == NULL && ifan->ifan_what == IFAN_ARRIVAL)
+  
+  if (ifp)
+    assert ( (ifp->ifindex == ifan->ifan_index) 
+             || (ifp->ifindex == IFINDEX_INTERNAL) );
+
+  if ( (ifp == NULL) 
+      || ((ifp->ifindex == IFINDEX_INTERNAL)
+          && (ifan->ifan_what == IFAN_ARRIVAL)) )
     {
+      if (IS_ZEBRA_DEBUG_KERNEL)
+        zlog_debug ("%s: creating interface for ifindex %d, name %s",
+                    __func__, ifan->ifan_index, ifan->ifan_name);
+      
       /* Create Interface */
-      ifp = if_get_by_name (ifan->ifan_name);
+      ifp = if_get_by_name_len(ifan->ifan_name,
+                              strnlen(ifan->ifan_name,
+                                      sizeof(ifan->ifan_name)));
       ifp->ifindex = ifan->ifan_index;
 
       if_add_update (ifp);
     }
   else if (ifp != NULL && ifan->ifan_what == IFAN_DEPARTURE)
-    {
-      if_delete_update (ifp);
-      if_delete (ifp);
-    }
+    if_delete_update (ifp);
 
   if_get_flags (ifp);
   if_get_mtu (ifp);
   if_get_metric (ifp);
 
   if (IS_ZEBRA_DEBUG_KERNEL)
-    zlog_debug ("interface %s index %d", ifp->name, ifp->ifindex);
+    zlog_debug ("%s: interface %s index %d", 
+                __func__, ifan->ifan_name, ifan->ifan_index);
 
   return 0;
 }
 #endif /* RTM_IFANNOUNCE */
 
+#ifdef HAVE_BSD_LINK_DETECT
+/* BSD link detect translation */
+static void
+bsd_linkdetect_translate (struct if_msghdr *ifm)
+{
+  if ((ifm->ifm_data.ifi_link_state >= LINK_STATE_UP) ||
+      (ifm->ifm_data.ifi_link_state == LINK_STATE_UNKNOWN))
+    SET_FLAG(ifm->ifm_flags, IFF_RUNNING);
+  else
+    UNSET_FLAG(ifm->ifm_flags, IFF_RUNNING);
+}
+#endif /* HAVE_BSD_LINK_DETECT */
+
 /*
  * Handle struct if_msghdr obtained from reading routing socket or
  * sysctl (from interface_list).  There may or may not be sockaddrs
@@ -232,11 +317,13 @@ int
 ifm_read (struct if_msghdr *ifm)
 {
   struct interface *ifp = NULL;
-  struct sockaddr_dl *sdl = NULL;
-  void *cp;
-  unsigned int i;
   char ifname[IFNAMSIZ];
-
+  short ifnlen = 0;
+  caddr_t *cp;
+  
+  /* terminate ifname at head (for strnlen) and tail (for safety) */
+  ifname[IFNAMSIZ - 1] = '\0';
+  
   /* paranoia: sanity check structure */
   if (ifm->ifm_msglen < sizeof(struct if_msghdr))
     {
@@ -263,95 +350,101 @@ ifm_read (struct if_msghdr *ifm)
        cp = cp + 12;
 #endif
 
+  RTA_ADDR_GET (NULL, RTA_DST, ifm->ifm_addrs, cp);
+  RTA_ADDR_GET (NULL, RTA_GATEWAY, ifm->ifm_addrs, cp);
+  RTA_ATTR_GET (NULL, RTA_NETMASK, ifm->ifm_addrs, cp);
+  RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifm_addrs, cp);
+  RTA_NAME_GET (ifname, RTA_IFP, ifm->ifm_addrs, cp, ifnlen);
+  RTA_ADDR_GET (NULL, RTA_IFA, ifm->ifm_addrs, cp);
+  RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifm_addrs, cp);
+  RTA_ADDR_GET (NULL, RTA_BRD, ifm->ifm_addrs, cp);
+  
+  if (IS_ZEBRA_DEBUG_KERNEL)
+    zlog_debug ("%s: sdl ifname %s", __func__, (ifnlen ? ifname : "(nil)"));
+  
   /* 
-   * Check for each sockaddr in turn, advancing over it.  After this
-   * loop, sdl should point to a sockaddr_dl iff one was present.
+   * Look up on ifindex first, because ifindices are the primary handle for
+   * interfaces across the user/kernel boundary, for most systems.  (Some
+   * messages, such as up/down status changes on NetBSD, do not include a
+   * sockaddr_dl).
    */
-  for (i = 1; i != 0; i <<= 1) 
+  if ( (ifp = if_lookup_by_index (ifm->ifm_index)) != NULL )
     {
-      if (i & ifm->ifm_addrs)
+      /* we have an ifp, verify that the name matches as some systems,
+       * eg Solaris, have a 1:many association of ifindex:ifname
+       * if they dont match, we dont have the correct ifp and should
+       * set it back to NULL to let next check do lookup by name
+       */
+      if (ifnlen && (strncmp (ifp->name, ifname, IFNAMSIZ) != 0) )
         {
-         if (i == RTA_IFP)
-           {
-             sdl = (struct sockaddr_dl *)cp;
-             break;
-            }
-         /* XXX warning: pointer of type `void *' used in arithmetic */
-         cp += SAROUNDUP(cp);
+          if (IS_ZEBRA_DEBUG_KERNEL)
+            zlog_debug ("%s: ifp name %s doesnt match sdl name %s",
+                        __func__, ifp->name, ifname);
+          ifp = NULL;
         }
     }
-
-  /* Ensure that sdl, if present, is actually a sockaddr_dl. */
-  if (sdl != NULL && sdl->sdl_family != AF_LINK)
-    {
-      zlog_err ("ifm_read: sockaddr_dl bad AF %d\n",
-               sdl->sdl_family);
-      return -1;
-    }
-
-  /* 
-   * Look up on ifindex first, because ifindices are the primary
-   * handle for interfaces across the user/kernel boundary.  (Some
-   * messages, such as up/down status changes on NetBSD, do not
-   * include a sockaddr_dl).
-   */
-  ifp = if_lookup_by_index (ifm->ifm_index);
-
+  
   /* 
-   * If lookup by index was unsuccessful and we have a name, try
-   * looking up by name.  Interfaces specified in the configuration
-   * file for which the ifindex has not been determined will have
-   * ifindex == -1, and such interfaces are found by this search, and
-   * then their ifindex values can be filled in.
+   * If we dont have an ifp, try looking up by name.  Particularly as some
+   * systems (Solaris) have a 1:many mapping of ifindex:ifname - the ifname
+   * is therefore our unique handle to that interface.
+   *
+   * Interfaces specified in the configuration file for which the ifindex
+   * has not been determined will have ifindex == IFINDEX_INTERNAL, and such
+   * interfaces are found by this search, and then their ifindex values can
+   * be filled in.
    */
-  if (ifp == NULL && sdl != NULL)
-    {
-      /*
-       * paranoia: sanity check name length.  nlen does not include
-       * trailing zero, but IFNAMSIZ max length does.
-       */
-      if (sdl->sdl_nlen >= IFNAMSIZ)
-       {
-         zlog_err ("ifm_read: illegal sdl_nlen %d\n", sdl->sdl_nlen);
-         return -1;
-       }
-
-      memcpy (ifname, sdl->sdl_data, sdl->sdl_nlen);
-      ifname[sdl->sdl_nlen] = '\0';
-      ifp = if_lookup_by_name (ifname);
-    }
+  if ( (ifp == NULL) && ifnlen)
+    ifp = if_lookup_by_name (ifname);
 
   /*
-   * If ifp does not exist or has an invalid index (-1), create or
-   * fill in an interface.
+   * If ifp still does not exist or has an invalid index (IFINDEX_INTERNAL),
+   * create or fill in an interface.
    */
-  /*
-   * XXX warning: comparison between signed and unsigned
-   * ifindex should probably be signed
-   */
-  if ((ifp == NULL) || (ifp->ifindex == -1))
+  if ((ifp == NULL) || (ifp->ifindex == IFINDEX_INTERNAL))
     {
       /*
        * To create or fill in an interface, a sockaddr_dl (via
        * RTA_IFP) is required.
        */
-      if (sdl == NULL)
+      if (!ifnlen)
        {
-         zlog_warn ("Interface index %d (new) missing RTA_IFP sockaddr_dl\n",
+         zlog_warn ("Interface index %d (new) missing ifname\n",
                     ifm->ifm_index);
          return -1;
        }
 
+#ifndef RTM_IFANNOUNCE
+      /* Down->Down interface should be ignored here.
+       * See further comment below.
+       */
+      if (!CHECK_FLAG (ifm->ifm_flags, IFF_UP))
+        return 0;
+#endif /* !RTM_IFANNOUNCE */
+      
       if (ifp == NULL)
-       /* Interface that zebra was not previously aware of, so create. */ 
-       ifp = if_create (sdl->sdl_data, sdl->sdl_nlen);
+        {
+         /* Interface that zebra was not previously aware of, so create. */ 
+         ifp = if_create (ifname, ifnlen);
+         if (IS_ZEBRA_DEBUG_KERNEL)
+           zlog_debug ("%s: creating ifp for ifindex %d", 
+                       __func__, ifm->ifm_index);
+        }
 
+      if (IS_ZEBRA_DEBUG_KERNEL)
+        zlog_debug ("%s: updated/created ifp, ifname %s, ifindex %d",
+                    __func__, ifp->name, ifp->ifindex);
       /* 
        * Fill in newly created interface structure, or larval
-       * structure with ifindex -1.
+       * structure with ifindex IFINDEX_INTERNAL.
        */
       ifp->ifindex = ifm->ifm_index;
-      ifp->flags = ifm->ifm_flags;
+      
+#ifdef HAVE_BSD_LINK_DETECT /* translate BSD kernel msg for link-state */
+      bsd_linkdetect_translate(ifm);
+#endif /* HAVE_BSD_LINK_DETECT */
+
+      if_flags_update (ifp, ifm->ifm_flags);
 #if defined(__bsdi__)
       if_kvm_get_mtu (ifp);
 #else
@@ -359,13 +452,6 @@ ifm_read (struct if_msghdr *ifm)
 #endif /* __bsdi__ */
       if_get_metric (ifp);
 
-      /* 
-       * XXX sockaddr_dl contents can be larger than the structure
-       * definition, so the user of the stored structure must be
-       * careful not to read off the end.
-       */
-      memcpy (&ifp->sdl, sdl, sizeof (struct sockaddr_dl));
-
       if_add_update (ifp);
     }
   else
@@ -377,73 +463,144 @@ ifm_read (struct if_msghdr *ifm)
      * but apparently do not trigger action.)
      */
     {
+      if (ifp->ifindex != ifm->ifm_index)
+        {
+          zlog_warn ("%s: index mismatch, ifname %s, ifp index %d, "
+                     "ifm index %d", 
+                     __func__, ifp->name, ifp->ifindex, ifm->ifm_index);
+          return -1;
+        }
+      
+#ifdef HAVE_BSD_LINK_DETECT /* translate BSD kernel msg for link-state */
+      bsd_linkdetect_translate(ifm);
+#endif /* HAVE_BSD_LINK_DETECT */
+
+      /* update flags and handle operative->inoperative transition, if any */
+      if_flags_update (ifp, ifm->ifm_flags);
+      
+#ifndef RTM_IFANNOUNCE
+      if (!if_is_up (ifp))
+          {
+            /* No RTM_IFANNOUNCE on this platform, so we can never
+             * distinguish between ~IFF_UP and delete. We must presume
+             * it has been deleted.
+             * Eg, Solaris will not notify us of unplumb.
+             *
+             * XXX: Fixme - this should be runtime detected
+             * So that a binary compiled on a system with IFANNOUNCE
+             * will still behave correctly if run on a platform without
+             */
+            if_delete_update (ifp);
+          }
+#endif /* RTM_IFANNOUNCE */
       if (if_is_up (ifp))
-       {
-         ifp->flags = ifm->ifm_flags;
-         if (! if_is_up (ifp))
-           if_down (ifp);
-       }
-      else
-       {
-         ifp->flags = ifm->ifm_flags;
-         if (if_is_up (ifp))
-           if_up (ifp);
-       }
+      {
+#if defined(__bsdi__)
+        if_kvm_get_mtu (ifp);
+#else
+        if_get_mtu (ifp);
+#endif /* __bsdi__ */
+        if_get_metric (ifp);
+      }
     }
-  
+
 #ifdef HAVE_NET_RT_IFLIST
   ifp->stats = ifm->ifm_data;
 #endif /* HAVE_NET_RT_IFLIST */
 
   if (IS_ZEBRA_DEBUG_KERNEL)
-    zlog_debug ("interface %s index %d", ifp->name, ifp->ifindex);
+    zlog_debug ("%s: interface %s index %d", 
+                __func__, ifp->name, ifp->ifindex);
 
   return 0;
 }
 \f
 /* Address read from struct ifa_msghdr. */
-void
+static void
 ifam_read_mesg (struct ifa_msghdr *ifm,
                union sockunion *addr,
                union sockunion *mask,
-               union sockunion *dest)
+               union sockunion *brd,
+               char *ifname,
+               short *ifnlen)
 {
   caddr_t pnt, end;
+  union sockunion dst;
+  union sockunion gateway;
 
   pnt = (caddr_t)(ifm + 1);
   end = ((caddr_t)ifm) + ifm->ifam_msglen;
 
-#define IFAMADDRGET(X,R) \
-    if (ifm->ifam_addrs & (R)) \
-      { \
-        int len = SAROUNDUP(pnt); \
-        if (((X) != NULL) && af_check (((struct sockaddr *)pnt)->sa_family)) \
-          memcpy ((caddr_t)(X), pnt, len); \
-        pnt += len; \
-      }
-#define IFAMMASKGET(X,R) \
-    if (ifm->ifam_addrs & (R)) \
-      { \
-       int len = SAROUNDUP(pnt); \
-        if ((X) != NULL) \
-         memcpy ((caddr_t)(X), pnt, len); \
-       pnt += len; \
-      }
-
   /* Be sure structure is cleared */
   memset (mask, 0, sizeof (union sockunion));
   memset (addr, 0, sizeof (union sockunion));
-  memset (dest, 0, sizeof (union sockunion));
+  memset (brd, 0, sizeof (union sockunion));
+  memset (&dst, 0, sizeof (union sockunion));
+  memset (&gateway, 0, sizeof (union sockunion));
 
   /* We fetch each socket variable into sockunion. */
-  IFAMADDRGET (NULL, RTA_DST);
-  IFAMADDRGET (NULL, RTA_GATEWAY);
-  IFAMMASKGET (mask, RTA_NETMASK);
-  IFAMADDRGET (NULL, RTA_GENMASK);
-  IFAMADDRGET (NULL, RTA_IFP);
-  IFAMADDRGET (addr, RTA_IFA);
-  IFAMADDRGET (NULL, RTA_AUTHOR);
-  IFAMADDRGET (dest, RTA_BRD);
+  RTA_ADDR_GET (&dst, RTA_DST, ifm->ifam_addrs, pnt);
+  RTA_ADDR_GET (&gateway, RTA_GATEWAY, ifm->ifam_addrs, pnt);
+  RTA_ATTR_GET (mask, RTA_NETMASK, ifm->ifam_addrs, pnt);
+  RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifam_addrs, pnt);
+  RTA_NAME_GET (ifname, RTA_IFP, ifm->ifam_addrs, pnt, *ifnlen);
+  RTA_ADDR_GET (addr, RTA_IFA, ifm->ifam_addrs, pnt);
+  RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifam_addrs, pnt);
+  RTA_ADDR_GET (brd, RTA_BRD, ifm->ifam_addrs, pnt);
+
+  if (IS_ZEBRA_DEBUG_KERNEL)
+    {
+      switch (sockunion_family(addr))
+        {
+       case AF_INET:
+         {
+           char buf[4][INET_ADDRSTRLEN];
+           zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, "
+                       "ifam_flags 0x%x, addr %s/%d broad %s dst %s "
+                       "gateway %s",
+                       __func__, ifm->ifam_index,
+                       (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs,
+                       ifm->ifam_flags,
+                       inet_ntop(AF_INET,&addr->sin.sin_addr,
+                                 buf[0],sizeof(buf[0])),
+                       ip_masklen(mask->sin.sin_addr),
+                       inet_ntop(AF_INET,&brd->sin.sin_addr,
+                                 buf[1],sizeof(buf[1])),
+                       inet_ntop(AF_INET,&dst.sin.sin_addr,
+                                 buf[2],sizeof(buf[2])),
+                       inet_ntop(AF_INET,&gateway.sin.sin_addr,
+                                 buf[3],sizeof(buf[3])));
+         }
+         break;
+#ifdef HAVE_IPV6
+       case AF_INET6:
+         {
+           char buf[4][INET6_ADDRSTRLEN];
+           zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, "
+                       "ifam_flags 0x%x, addr %s/%d broad %s dst %s "
+                       "gateway %s",
+                       __func__, ifm->ifam_index, 
+                       (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs,
+                       ifm->ifam_flags,
+                       inet_ntop(AF_INET6,&addr->sin6.sin6_addr,
+                                 buf[0],sizeof(buf[0])),
+                       ip6_masklen(mask->sin6.sin6_addr),
+                       inet_ntop(AF_INET6,&brd->sin6.sin6_addr,
+                                 buf[1],sizeof(buf[1])),
+                       inet_ntop(AF_INET6,&dst.sin6.sin6_addr,
+                                 buf[2],sizeof(buf[2])),
+                       inet_ntop(AF_INET6,&gateway.sin6.sin6_addr,
+                                 buf[3],sizeof(buf[3])));
+         }
+         break;
+#endif /* HAVE_IPV6 */
+        default:
+         zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x",
+                     __func__, ifm->ifam_index, 
+                     (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs);
+         break;
+        }
+    }
 
   /* Assert read up end point matches to end point */
   if (pnt != end)
@@ -454,35 +611,56 @@ ifam_read_mesg (struct ifa_msghdr *ifm,
 int
 ifam_read (struct ifa_msghdr *ifam)
 {
-  struct interface *ifp;
-  union sockunion addr, mask, gate;
-
-  /* Check does this interface exist or not. */
-  ifp = if_lookup_by_index (ifam->ifam_index);
-  if (ifp == NULL) 
+  struct interface *ifp = NULL;
+  union sockunion addr, mask, brd;
+  char ifname[INTERFACE_NAMSIZ];
+  short ifnlen = 0;
+  char isalias = 0;
+  int flags = 0;
+  
+  ifname[0] = ifname[INTERFACE_NAMSIZ - 1] = '\0';
+  
+  /* Allocate and read address information. */
+  ifam_read_mesg (ifam, &addr, &mask, &brd, ifname, &ifnlen);
+  
+  if ((ifp = if_lookup_by_index(ifam->ifam_index)) == NULL)
     {
-      zlog_warn ("no interface for index %d", ifam->ifam_index); 
+      zlog_warn ("%s: no interface for ifname %s, index %d", 
+                 __func__, ifname, ifam->ifam_index);
       return -1;
     }
-
-  /* Allocate and read address information. */
-  ifam_read_mesg (ifam, &addr, &mask, &gate);
-
-  /* Check interface flag for implicit up of the interface. */
-  if_refresh (ifp);
+  
+  if (ifnlen && strncmp (ifp->name, ifname, INTERFACE_NAMSIZ))
+    isalias = 1;
+  
+  /* N.B. The info in ifa_msghdr does not tell us whether the RTA_BRD
+     field contains a broadcast address or a peer address, so we are forced to
+     rely upon the interface type. */
+  if (if_is_pointopoint(ifp))
+    SET_FLAG(flags, ZEBRA_IFA_PEER);
+
+#if 0
+  /* it might seem cute to grab the interface metric here, however
+   * we're processing an address update message, and so some systems
+   * (e.g. FBSD) dont bother to fill in ifam_metric. Disabled, but left
+   * in deliberately, as comment.
+   */
+  ifp->metric = ifam->ifam_metric;
+#endif
 
   /* Add connected address. */
   switch (sockunion_family (&addr))
     {
     case AF_INET:
       if (ifam->ifam_type == RTM_NEWADDR)
-       connected_add_ipv4 (ifp, 0, &addr.sin.sin_addr, 
+       connected_add_ipv4 (ifp, flags, &addr.sin.sin_addr, 
                            ip_masklen (mask.sin.sin_addr),
-                           &gate.sin.sin_addr, NULL);
+                           &brd.sin.sin_addr,
+                           (isalias ? ifname : NULL));
       else
-       connected_delete_ipv4 (ifp, 0, &addr.sin.sin_addr, 
+       connected_delete_ipv4 (ifp, flags, &addr.sin.sin_addr, 
                               ip_masklen (mask.sin.sin_addr),
-                              &gate.sin.sin_addr, NULL);
+                              &brd.sin.sin_addr);
       break;
 #ifdef HAVE_IPV6
     case AF_INET6:
@@ -492,30 +670,53 @@ ifam_read (struct ifa_msghdr *ifam)
        SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0);
 
       if (ifam->ifam_type == RTM_NEWADDR)
-       connected_add_ipv6 (ifp,
-                           &addr.sin6.sin6_addr, 
+       connected_add_ipv6 (ifp, flags, &addr.sin6.sin6_addr, 
                            ip6_masklen (mask.sin6.sin6_addr),
-                           &gate.sin6.sin6_addr);
+                           &brd.sin6.sin6_addr,
+                           (isalias ? ifname : NULL));
       else
        connected_delete_ipv6 (ifp,
                               &addr.sin6.sin6_addr, 
                               ip6_masklen (mask.sin6.sin6_addr),
-                              &gate.sin6.sin6_addr);
+                              &brd.sin6.sin6_addr);
       break;
 #endif /* HAVE_IPV6 */
     default:
       /* Unsupported family silently ignore... */
       break;
     }
+  
+  /* Check interface flag for implicit up of the interface. */
+  if_refresh (ifp);
+
+#ifdef SUNOS_5
+  /* In addition to lacking IFANNOUNCE, on SUNOS IFF_UP is strange. 
+   * See comments for SUNOS_5 in interface.c::if_flags_mangle.
+   * 
+   * Here we take care of case where the real IFF_UP was previously
+   * unset (as kept in struct zebra_if.primary_state) and the mangled
+   * IFF_UP (ie IFF_UP set || listcount(connected) has now transitioned
+   * to unset due to the lost non-primary address having DELADDR'd.
+   *
+   * we must delete the interface, because in between here and next
+   * event for this interface-name the administrator could unplumb
+   * and replumb the interface.
+   */
+  if (!if_is_up (ifp))
+    if_delete_update (ifp);
+#endif /* SUNOS_5 */
+  
   return 0;
 }
 \f
 /* Interface function for reading kernel routing table information. */
-int
+static int
 rtm_read_mesg (struct rt_msghdr *rtm,
               union sockunion *dest,
               union sockunion *mask,
-              union sockunion *gate)
+              union sockunion *gate,
+              char *ifname,
+              short *ifnlen)
 {
   caddr_t pnt, end;
 
@@ -528,38 +729,21 @@ rtm_read_mesg (struct rt_msghdr *rtm,
       zlog (NULL, LOG_WARNING,
              "Routing message version different %d should be %d."
              "This may cause problem\n", rtm->rtm_version, RTM_VERSION);
-
-#define RTMADDRGET(X,R) \
-    if (rtm->rtm_addrs & (R)) \
-      { \
-       int len = SAROUNDUP (pnt); \
-        if (((X) != NULL) && af_check (((struct sockaddr *)pnt)->sa_family)) \
-         memcpy ((caddr_t)(X), pnt, len); \
-       pnt += len; \
-      }
-#define RTMMASKGET(X,R) \
-    if (rtm->rtm_addrs & (R)) \
-      { \
-       int len = SAROUNDUP (pnt); \
-        if ((X) != NULL) \
-         memcpy ((caddr_t)(X), pnt, len); \
-       pnt += len; \
-      }
-
+  
   /* Be sure structure is cleared */
   memset (dest, 0, sizeof (union sockunion));
   memset (gate, 0, sizeof (union sockunion));
   memset (mask, 0, sizeof (union sockunion));
 
   /* We fetch each socket variable into sockunion. */
-  RTMADDRGET (dest, RTA_DST);
-  RTMADDRGET (gate, RTA_GATEWAY);
-  RTMMASKGET (mask, RTA_NETMASK);
-  RTMADDRGET (NULL, RTA_GENMASK);
-  RTMADDRGET (NULL, RTA_IFP);
-  RTMADDRGET (NULL, RTA_IFA);
-  RTMADDRGET (NULL, RTA_AUTHOR);
-  RTMADDRGET (NULL, RTA_BRD);
+  RTA_ADDR_GET (dest, RTA_DST, rtm->rtm_addrs, pnt);
+  RTA_ADDR_GET (gate, RTA_GATEWAY, rtm->rtm_addrs, pnt);
+  RTA_ATTR_GET (mask, RTA_NETMASK, rtm->rtm_addrs, pnt);
+  RTA_ADDR_GET (NULL, RTA_GENMASK, rtm->rtm_addrs, pnt);
+  RTA_NAME_GET (ifname, RTA_IFP, rtm->rtm_addrs, pnt, *ifnlen);
+  RTA_ADDR_GET (NULL, RTA_IFA, rtm->rtm_addrs, pnt);
+  RTA_ADDR_GET (NULL, RTA_AUTHOR, rtm->rtm_addrs, pnt);
+  RTA_ADDR_GET (NULL, RTA_BRD, rtm->rtm_addrs, pnt);
 
   /* If there is netmask information set it's family same as
      destination family*/
@@ -579,17 +763,19 @@ rtm_read (struct rt_msghdr *rtm)
   int flags;
   u_char zebra_flags;
   union sockunion dest, mask, gate;
+  char ifname[INTERFACE_NAMSIZ + 1];
+  short ifnlen = 0;
 
   zebra_flags = 0;
 
-  /* Discard self send message. */
-  if (rtm->rtm_type != RTM_GET 
-      && (rtm->rtm_pid == pid || rtm->rtm_pid == old_pid))
-    return;
-
   /* Read destination and netmask and gateway from rtm message
      structure. */
-  flags = rtm_read_mesg (rtm, &dest, &mask, &gate);
+  flags = rtm_read_mesg (rtm, &dest, &mask, &gate, ifname, &ifnlen);
+  if (!(flags & RTF_DONE))
+    return;
+  if (IS_ZEBRA_DEBUG_KERNEL)
+    zlog_debug ("%s: got rtm of type %d (%s)", __func__, rtm->rtm_type,
+      lookup (rtm_type_str, rtm->rtm_type));
 
 #ifdef RTF_CLONED      /*bsdi, netbsd 1.6*/
   if (flags & RTF_CLONED)
@@ -630,10 +816,89 @@ rtm_read (struct rt_msghdr *rtm)
        p.prefixlen = IPV4_MAX_PREFIXLEN;
       else
        p.prefixlen = ip_masklen (mask.sin.sin_addr);
+      
+      /* Catch self originated messages and match them against our current RIB.
+       * At the same time, ignore unconfirmed messages, they should be tracked
+       * by rtm_write() and kernel_rtm_ipv4().
+       */
+      if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid)
+      {
+        char buf[INET_ADDRSTRLEN], gate_buf[INET_ADDRSTRLEN];
+        int ret;
+        if (! IS_ZEBRA_DEBUG_RIB)
+          return;
+        ret = rib_lookup_ipv4_route (&p, &gate); 
+        inet_ntop (AF_INET, &p.prefix, buf, INET_ADDRSTRLEN);
+        switch (rtm->rtm_type)
+        {
+          case RTM_ADD:
+          case RTM_GET:
+          case RTM_CHANGE:
+            /* The kernel notifies us about a new route in FIB created by us.
+               Do we have a correspondent entry in our RIB? */
+            switch (ret)
+            {
+              case ZEBRA_RIB_NOTFOUND:
+                zlog_debug ("%s: %s %s/%d: desync: RR isn't yet in RIB, while already in FIB",
+                  __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
+                break;
+              case ZEBRA_RIB_FOUND_CONNECTED:
+              case ZEBRA_RIB_FOUND_NOGATE:
+                inet_ntop (AF_INET, &gate.sin.sin_addr, gate_buf, INET_ADDRSTRLEN);
+                zlog_debug ("%s: %s %s/%d: desync: RR is in RIB, but gate differs (ours is %s)",
+                  __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen, gate_buf);
+                break;
+              case ZEBRA_RIB_FOUND_EXACT: /* RIB RR == FIB RR */
+                zlog_debug ("%s: %s %s/%d: done Ok",
+                  __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
+                rib_lookup_and_dump (&p);
+                return;
+                break;
+            }
+            break;
+          case RTM_DELETE:
+            /* The kernel notifies us about a route deleted by us. Do we still
+               have it in the RIB? Do we have anything instead? */
+            switch (ret)
+            {
+              case ZEBRA_RIB_FOUND_EXACT:
+                zlog_debug ("%s: %s %s/%d: desync: RR is still in RIB, while already not in FIB",
+                  __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
+                rib_lookup_and_dump (&p);
+                break;
+              case ZEBRA_RIB_FOUND_CONNECTED:
+              case ZEBRA_RIB_FOUND_NOGATE:
+                zlog_debug ("%s: %s %s/%d: desync: RR is still in RIB, plus gate differs",
+                  __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
+                rib_lookup_and_dump (&p);
+                break;
+              case ZEBRA_RIB_NOTFOUND: /* RIB RR == FIB RR */
+                zlog_debug ("%s: %s %s/%d: done Ok",
+                  __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
+                rib_lookup_and_dump (&p);
+                return;
+                break;
+            }
+            break;
+          default:
+            zlog_debug ("%s: %s/%d: warning: loopback RTM of type %s received",
+              __func__, buf, p.prefixlen, lookup (rtm_type_str, rtm->rtm_type));
+        }
+        return;
+      }
 
-      if (rtm->rtm_type == RTM_GET || rtm->rtm_type == RTM_ADD)
+      /* Change, delete the old prefix, we have no further information
+       * to specify the route really
+       */
+      if (rtm->rtm_type == RTM_CHANGE)
+        rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
+                         NULL, 0, 0);
+      
+      if (rtm->rtm_type == RTM_GET 
+          || rtm->rtm_type == RTM_ADD
+          || rtm->rtm_type == RTM_CHANGE)
        rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, 
-                     &p, &gate.sin.sin_addr, 0, 0, 0, 0);
+                     &p, &gate.sin.sin_addr, NULL, 0, 0, 0, 0);
       else
        rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, 
                      &p, &gate.sin.sin_addr, 0, 0);
@@ -641,6 +906,11 @@ rtm_read (struct rt_msghdr *rtm)
 #ifdef HAVE_IPV6
   if (dest.sa.sa_family == AF_INET6)
     {
+      /* One day we might have a debug section here like one in the
+       * IPv4 case above. Just ignore own messages at the moment.
+       */
+      if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid)
+        return;
       struct prefix_ipv6 p;
       unsigned int ifindex = 0;
 
@@ -659,9 +929,18 @@ rtm_read (struct rt_msghdr *rtm)
        }
 #endif /* KAME */
 
-      if (rtm->rtm_type == RTM_GET || rtm->rtm_type == RTM_ADD)
+      /* CHANGE: delete the old prefix, we have no further information
+       * to specify the route really
+       */
+      if (rtm->rtm_type == RTM_CHANGE)
+        rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
+                         NULL, 0, 0);
+      
+      if (rtm->rtm_type == RTM_GET 
+          || rtm->rtm_type == RTM_ADD
+          || rtm->rtm_type == RTM_CHANGE)
        rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
-                     &p, &gate.sin6.sin6_addr, ifindex, 0);
+                     &p, &gate.sin6.sin6_addr, ifindex, 0, 0, 0);
       else
        rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
                         &p, &gate.sin6.sin6_addr, ifindex, 0);
@@ -670,7 +949,9 @@ rtm_read (struct rt_msghdr *rtm)
 }
 
 /* Interface function for the kernel routing table updates.  Support
-   for RTM_CHANGE will be needed. */
+ * for RTM_CHANGE will be needed.
+ * Exported only for rt_socket.c
+ */
 int
 rtm_write (int message,
           union sockunion *dest,
@@ -683,10 +964,6 @@ rtm_write (int message,
   int ret;
   caddr_t pnt;
   struct interface *ifp;
-  struct sockaddr_in tmp_gate;
-#ifdef HAVE_IPV6
-  struct sockaddr_in6 tmp_gate6;
-#endif /* HAVE_IPV6 */
 
   /* Sequencial number of routing message. */
   static int msg_seq = 0;
@@ -698,20 +975,6 @@ rtm_write (int message,
     char buf[512];
   } msg;
   
-  memset (&tmp_gate, 0, sizeof (struct sockaddr_in));
-  tmp_gate.sin_family = AF_INET;
-#ifdef HAVE_SIN_LEN
-  tmp_gate.sin_len = sizeof (struct sockaddr_in);
-#endif /* HAVE_SIN_LEN */
-
-#ifdef HAVE_IPV6
-  memset (&tmp_gate6, 0, sizeof (struct sockaddr_in6));
-  tmp_gate6.sin6_family = AF_INET6;
-#ifdef SIN6_LEN
-  tmp_gate6.sin6_len = sizeof (struct sockaddr_in6);
-#endif /* SIN6_LEN */
-#endif /* HAVE_IPV6 */
-
   if (routing_sock < 0)
     return ZEBRA_ERR_EPERM;
 
@@ -746,7 +1009,13 @@ rtm_write (int message,
     {
       if (!ifp)
         {
-          zlog_warn ("no gateway found for interface index %d", index);
+          char dest_buf[INET_ADDRSTRLEN] = "NULL", mask_buf[INET_ADDRSTRLEN] = "255.255.255.255";
+          if (dest)
+            inet_ntop (AF_INET, &dest->sin.sin_addr, dest_buf, INET_ADDRSTRLEN);
+          if (mask)
+            inet_ntop (AF_INET, &mask->sin.sin_addr, mask_buf, INET_ADDRSTRLEN);
+          zlog_warn ("%s: %s/%s: gate == NULL and no gateway found for ifindex %d",
+            __func__, dest_buf, mask_buf, index);
           return -1;
         }
       gate = (union sockunion *) & ifp->sdl;
@@ -767,7 +1036,7 @@ rtm_write (int message,
     msg.rtm.rtm_flags |= RTF_REJECT;
 
 
-#ifdef HAVE_SIN_LEN
+#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
 #define SOCKADDRSET(X,R) \
   if (msg.rtm.rtm_addrs & (R)) \
     { \
@@ -779,11 +1048,11 @@ rtm_write (int message,
 #define SOCKADDRSET(X,R) \
   if (msg.rtm.rtm_addrs & (R)) \
     { \
-      int len = ROUNDUP (sizeof((X)->sa)); \
+      int len = SAROUNDUP (X); \
       memcpy (pnt, (caddr_t)(X), len); \
       pnt += len; \
     }
-#endif /* HAVE_SIN_LEN */
+#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
 
   pnt = (caddr_t) msg.buf;
 
@@ -802,11 +1071,13 @@ rtm_write (int message,
        return ZEBRA_ERR_RTEXIST;
       if (errno == ENETUNREACH)
        return ZEBRA_ERR_RTUNREACH;
+      if (errno == ESRCH)
+       return ZEBRA_ERR_RTNOEXIST;
       
-      zlog_warn ("write : %s (%d)", safe_strerror (errno), errno);
-      return -1;
+      zlog_warn ("%s: write : %s (%d)", __func__, safe_strerror (errno), errno);
+      return ZEBRA_ERR_KERNEL;
     }
-  return 0;
+  return ZEBRA_ERR_NOERROR;
 }
 
 \f
@@ -817,20 +1088,10 @@ rtm_write (int message,
 static void
 rtmsg_debug (struct rt_msghdr *rtm)
 {
-  const char *type = "Unknown";
-  struct message *mes;
-
-  for (mes = rtm_type_str; mes->str; mes++)
-    if (mes->key == rtm->rtm_type)
-      {
-       type = mes->str;
-       break;
-      }
-
-  zlog_debug ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, type);
+  zlog_debug ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, lookup (rtm_type_str, rtm->rtm_type));
   rtm_flag_dump (rtm->rtm_flags);
   zlog_debug ("Kernel: message seq %d", rtm->rtm_seq);
-  zlog_debug ("Kernel: pid %d", rtm->rtm_pid);
+  zlog_debug ("Kernel: pid %d, rtm_addrs 0x%x", rtm->rtm_pid, rtm->rtm_addrs);
 }
 
 /* This is pretty gross, better suggestions welcome -- mhandler */
@@ -843,7 +1104,7 @@ rtmsg_debug (struct rt_msghdr *rtm)
 #endif /* RTAX_MAX */
 
 /* Kernel routing table and interface updates via routing socket. */
-int
+static int
 kernel_read (struct thread *thread)
 {
   int sock;
@@ -927,6 +1188,7 @@ kernel_read (struct thread *thread)
     {
     case RTM_ADD:
     case RTM_DELETE:
+    case RTM_CHANGE:
       rtm_read (rtm);
       break;
     case RTM_IFINFO:
@@ -950,8 +1212,8 @@ kernel_read (struct thread *thread)
 }
 
 /* Make routing socket. */
-void
-routing_socket ()
+static void
+routing_socket (void)
 {
   if ( zserv_privs.change (ZPRIVS_RAISE) )
     zlog_err ("routing_socket: Can't raise privileges");
@@ -966,8 +1228,13 @@ routing_socket ()
       return;
     }
 
-  if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0) 
-    zlog_warn ("Can't set O_NONBLOCK to routing socket");
+  /* XXX: Socket should be NONBLOCK, however as we currently 
+   * discard failed writes, this will lead to inconsistencies.
+   * For now, socket must be blocking.
+   */
+  /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0) 
+    zlog_warn ("Can't set O_NONBLOCK to routing socket");*/
+    
   if ( zserv_privs.change (ZPRIVS_LOWER) )
     zlog_err ("routing_socket: Can't lower privileges");
 
@@ -978,7 +1245,7 @@ routing_socket ()
 /* Exported interface function.  This function simply calls
    routing_socket (). */
 void
-kernel_init ()
+kernel_init (void)
 {
   routing_socket ();
 }