Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/ldpd retire union_ntoa, replace it with satos



details:   https://anonhg.NetBSD.org/src/rev/72f36e818763
branches:  trunk
changeset: 787937:72f36e818763
user:      kefren <kefren%NetBSD.org@localhost>
date:      Tue Jul 16 02:54:32 2013 +0000

description:
retire union_ntoa, replace it with satos
check for valid sizes on PF_ROUTE socket
minor comment update

diffstat:

 usr.sbin/ldpd/label.c       |    8 +-
 usr.sbin/ldpd/ldp_command.c |    6 +-
 usr.sbin/ldpd/ldp_errors.c  |   30 +++++++++-
 usr.sbin/ldpd/mpls_routes.c |  135 ++++++++++++++++++++++---------------------
 usr.sbin/ldpd/mpls_routes.h |    3 +-
 5 files changed, 105 insertions(+), 77 deletions(-)

diffs (truncated from 452 to 300 lines):

diff -r 7580b420f96d -r 72f36e818763 usr.sbin/ldpd/label.c
--- a/usr.sbin/ldpd/label.c     Tue Jul 16 02:10:43 2013 +0000
+++ b/usr.sbin/ldpd/label.c     Tue Jul 16 02:54:32 2013 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: label.c,v 1.6 2013/07/11 10:46:19 kefren Exp $ */
+/* $NetBSD: label.c,v 1.7 2013/07/16 02:54:32 kefren Exp $ */
 
 /*-
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -85,9 +85,9 @@
 
        SLIST_INSERT_HEAD(&label_head, l, labels);
 
-       strlcpy(spreftmp, union_ntoa(so_pref), INET_ADDRSTRLEN);
+       strlcpy(spreftmp, satos(&so_pref->sa), INET_ADDRSTRLEN);
        warnp("[label_add] added binding %d for %s/%s\n", l->binding,
-           union_ntoa(so_dest), spreftmp);
+           satos(&so_dest->sa), spreftmp);
 
        send_label_tlv_to_all(&(so_dest->sa),
            from_union_to_cidr(so_pref), l->binding);
@@ -99,7 +99,7 @@
 label_del(struct label * l)
 {
        warnp("[label_del] deleted binding %d for %s\n", l->binding,
-          union_ntoa(&l->so_dest));
+          satos(&l->so_dest.sa));
        SLIST_REMOVE(&label_head, l, label, labels);
        free(l);
 }
diff -r 7580b420f96d -r 72f36e818763 usr.sbin/ldpd/ldp_command.c
--- a/usr.sbin/ldpd/ldp_command.c       Tue Jul 16 02:10:43 2013 +0000
+++ b/usr.sbin/ldpd/ldp_command.c       Tue Jul 16 02:54:32 2013 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: ldp_command.c,v 1.10 2013/01/28 21:08:14 kefren Exp $ */
+/* $NetBSD: ldp_command.c,v 1.11 2013/07/16 02:54:32 kefren Exp $ */
 
 /*-
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -513,9 +513,9 @@
        writestr(s, sendspace);
        SLIST_FOREACH (l, &label_head, labels) {
                snprintf(sendspace, MAXSEND, "%d\t\t%s/", l->binding,
-                   union_ntoa(&l->so_dest));
+                   satos(&l->so_dest.sa));
                writestr(s, sendspace);
-               snprintf(sendspace, MAXSEND, "%s", union_ntoa(&l->so_pref));
+               snprintf(sendspace, MAXSEND, "%s", satos(&l->so_pref.sa));
                writestr(s, sendspace);
                if (l->p)
                        snprintf(sendspace, MAXSEND, "\t%s:%d\n",
diff -r 7580b420f96d -r 72f36e818763 usr.sbin/ldpd/ldp_errors.c
--- a/usr.sbin/ldpd/ldp_errors.c        Tue Jul 16 02:10:43 2013 +0000
+++ b/usr.sbin/ldpd/ldp_errors.c        Tue Jul 16 02:54:32 2013 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: ldp_errors.c,v 1.3 2013/01/26 17:29:55 kefren Exp $ */
+/* $NetBSD: ldp_errors.c,v 1.4 2013/07/16 02:54:32 kefren Exp $ */
 
 /*
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -30,10 +30,12 @@
  */
 
 #include <arpa/inet.h>
+#include <netmpls/mpls.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdarg.h>
+#include <string.h>
 #include <syslog.h>
 #include <unistd.h>
 
@@ -45,6 +47,7 @@
 static void do_syslog(int, const char*, va_list) __printflike(2, 0);
 static char satos_str[INET6_ADDRSTRLEN > INET_ADDRSTRLEN ? INET6_ADDRSTRLEN :
                INET_ADDRSTRLEN];
+static char *mpls_ntoa(const struct sockaddr_mpls *smpls);
 
 void 
 debugp(const char *fmt, ...)
@@ -134,8 +137,33 @@
                                return "INET6 ERROR";
                        break;
                }
+               case AF_LINK:
+               {
+                       strlcpy(satos_str,
+                           link_ntoa((const struct sockaddr_dl *)sa),
+                           sizeof(satos_str));
+                       break;
+               }
+               case AF_MPLS:
+               {
+                       strlcpy(satos_str,
+                           mpls_ntoa((const struct sockaddr_mpls *)sa),
+                           sizeof(satos_str));
+                       break;
+               }
                default:
                        return "UNKNOWN AF";
        }
        return satos_str;
 }
+
+static char *
+mpls_ntoa(const struct sockaddr_mpls *smpls)
+{
+       static char ret[10];
+       union mpls_shim ms2;
+
+       ms2.s_addr = ntohl(smpls->smpls_addr.s_addr);
+       snprintf(ret, sizeof(ret), "%d", ms2.shim.label);
+       return ret;
+}
diff -r 7580b420f96d -r 72f36e818763 usr.sbin/ldpd/mpls_routes.c
--- a/usr.sbin/ldpd/mpls_routes.c       Tue Jul 16 02:10:43 2013 +0000
+++ b/usr.sbin/ldpd/mpls_routes.c       Tue Jul 16 02:54:32 2013 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: mpls_routes.c,v 1.14 2013/07/12 08:55:52 kefren Exp $ */
+/* $NetBSD: mpls_routes.c,v 1.15 2013/07/16 02:54:32 kefren Exp $ */
 
 /*-
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -68,7 +68,6 @@
 static int read_route_socket(char *, int);
 void   mask_addr(union sockunion *);
 int    compare_sockunion(const union sockunion *, const union sockunion *);
-char * mpls_ntoa(union mpls_shim);
 static int check_if_addr_updown(struct rt_msg *, uint);
 
 extern struct sockaddr mplssockaddr;
@@ -79,9 +78,25 @@
        do { l = RT_ROUNDUP(u->sa.sa_len); memcpy(cp, u, l); cp += l;} while(0);
 #define NEXTADDR2(u) \
        do { l = RT_ROUNDUP(u.sa_len); memcpy(cp, &u, l); cp += l; } while(0);
-#define GETNEXT(sunion) \
-       (union sockunion *) ((char *) (sunion)  + \
-       RT_ROUNDUP((sunion)->sa.sa_len))
+
+#define CHECK_LEN(sunion) \
+       if (size_cp + sunion->sa.sa_len > rlen) \
+               return LDP_E_ROUTE_ERROR; \
+       else \
+               size_cp += sunion->sa.sa_len;
+
+#define CHECK_MINSA \
+       if (size_cp + sizeof(sa_family_t) + sizeof(uint8_t) > rlen) \
+               return LDP_E_ROUTE_ERROR;
+
+#define GETNEXT(dstunion, origunion) \
+       do { \
+       CHECK_MINSA \
+       dstunion = (union sockunion *) ((char *) (origunion)  + \
+       RT_ROUNDUP((origunion)->sa.sa_len)); \
+       CHECK_LEN(dstunion) \
+       } while (0);
+
 
 static int 
 read_route_socket(char *s, int max)
@@ -267,35 +282,6 @@
            (a << 16) >> 24, (a << 24) >> 24);  
 }
 
-char *
-mpls_ntoa(const union mpls_shim ms)
-{
-       static char     ret[255];
-       union mpls_shim ms2;
-
-       ms2.s_addr = ntohl(ms.s_addr);
-       snprintf(ret, sizeof(ret), "%d", ms2.shim.label);
-       return ret;
-}
-
-char *
-union_ntoa(const union sockunion * so)
-{
-       static char defret[] = "Unknown family address";
-
-       switch (so->sa.sa_family) {
-       case AF_INET:
-               return inet_ntoa(so->sin.sin_addr);
-       case AF_LINK:
-               return link_ntoa(&so->sdl);
-       case AF_MPLS:
-               return mpls_ntoa(so->smpls.smpls_addr);
-       }
-       fatalp("Unknown family address in union_ntoa: %d\n",
-              so->sa.sa_family);
-       return defret;
-}
-
 /* From src/sbin/route/route.c */
 static const char *
 route_strerror(int error)
@@ -386,11 +372,11 @@
 
        if ((rlen = write(route_socket, (char *) &rm, l)) < l) {
                warnp("Error adding a route: %s\n", route_strerror(errno));
-               warnp("Destination was: %s\n", union_ntoa(so_dest));
+               warnp("Destination was: %s\n", satos(&so_dest->sa));
                if (so_prefix)
-                       warnp("Prefix was: %s\n", union_ntoa(so_prefix));
+                       warnp("Prefix was: %s\n", satos(&so_prefix->sa));
                if (so_gate)
-                       warnp("Gateway was: %s\n", union_ntoa(so_gate));
+                       warnp("Gateway was: %s\n", satos(&so_gate->sa));
                rv = LDP_E_ROUTE_ERROR;
        }
        if (fr == FREESO) {
@@ -461,11 +447,11 @@
                strlcpy(spreftmp, inet_ntoa(so_pref->sin.sin_addr),
                    INET_ADDRSTRLEN);
                warnp("Error deleting route(%s): %s/%s",
-                   route_strerror(errno), union_ntoa(so_dest),
+                   route_strerror(errno), satos(&so_dest->sa),
                    spreftmp);
            } else
                warnp("Error deleting route(%s) : %s",
-                   route_strerror(errno), union_ntoa(so_dest));
+                   route_strerror(errno), satos(&so_dest->sa));
            return LDP_E_NO_SUCH_ROUTE;
        }
        return LDP_E_OK;
@@ -559,8 +545,8 @@
        if (exact_match) {
                su = (union sockunion*)(rg->m_space);
                if (compare_sockunion(so_dest, su)) {
-                       debugp("Dest %s ", union_ntoa(so_dest));
-                       debugp("not like %s\n", union_ntoa(su));
+                       debugp("Dest %s ", satos(&so_dest->sa));
+                       debugp("not like %s\n", satos(&su->sa));
                        return LDP_E_NO_SUCH_ROUTE;
                }
        }
@@ -575,6 +561,7 @@
        union sockunion *so_dest = NULL, *so_gate = NULL, *so_pref = NULL;
        int             so_pref_allocated = 0;
        int             prefixlen;
+       size_t          size_cp;
        struct peer_map *pm;
        struct label    *lab;
        char            dest[50], gate[50], pref[50], oper[50];
@@ -589,8 +576,8 @@
            rg->m_rtm.rtm_type == RTM_DELADDR)
                return check_if_addr_updown(rg, rlen);
 
-       if (rlen < sizeof(struct rt_msghdr))
-               return LDP_E_ROUTE_ERROR;
+       size_cp = sizeof(struct rt_msghdr);
+       CHECK_MINSA;
 
        if (rg->m_rtm.rtm_pid == getpid() ||
            ((rg->m_rtm.rtm_flags & RTF_DONE) == 0))
@@ -603,8 +590,10 @@
        if (so_dest->sa.sa_family != AF_INET)
                return LDP_E_OK;/* We don't care about non-IP changes */
 
+       CHECK_LEN(so_dest);
+
        if (rg->m_rtm.rtm_addrs & RTA_GATEWAY) {
-               so_gate = GETNEXT(so_dest);
+               GETNEXT(so_gate, so_dest);
                if ((so_gate->sa.sa_family != AF_INET) &&
                    (so_gate->sa.sa_family != AF_MPLS))
                        return LDP_E_OK;
@@ -614,7 +603,7 @@
                        so_pref = so_gate;
                else
                        so_pref = so_dest;
-               so_pref = GETNEXT(so_pref);
+               GETNEXT(so_pref, so_pref);
        }
        if (!(rg->m_rtm.rtm_flags & RTF_GATEWAY)) {
                if (rg->m_rtm.rtm_addrs & RTA_GENMASK) {
@@ -673,7 +662,7 @@
                        }
                } else  /* We already know about this prefix */
                        debugp("Binding already there for prefix %s/%d !\n",
-                             union_ntoa(so_dest), prefixlen);
+                             satos(&so_dest->sa), prefixlen);
                break;
        case RTM_DELETE:
                if (!so_gate)
@@ -701,11 +690,11 @@
        /* Rest is just for debug */
 
        if (so_dest)
-               strlcpy(dest, union_ntoa(so_dest), 16);
+               strlcpy(dest, satos(&so_dest->sa), sizeof(dest));
        if (so_pref)
-               snprintf(pref, 3, "%d", prefixlen);
+               snprintf(pref, sizeof(pref), "%d", prefixlen);
        if (so_gate)
-               strlcpy(gate, union_ntoa(so_gate), 16);
+               strlcpy(gate, satos(&so_gate->sa), sizeof(gate));
 
        switch (rg->m_rtm.rtm_type) {



Home | Main Index | Thread Index | Old Index