Source-Changes-HG archive

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

[src/netbsd-8]: src/sys Pull up following revision(s) (requested by maxv in t...



details:   https://anonhg.NetBSD.org/src/rev/246ee25643b7
branches:  netbsd-8
changeset: 434632:246ee25643b7
user:      snj <snj%NetBSD.org@localhost>
date:      Mon Feb 12 18:18:00 2018 +0000

description:
Pull up following revision(s) (requested by maxv in ticket #546):
        sys/net/if_mpls.c: 1.31-1.33
        sys/netmpls/mpls_ttl.c: 1.9-1.11
Style, and fix several bugs:
 - ip4_check(), mpls_unlabel_inet() and mpls_unlabel_inet6() perform
   pullups, so we need to pass the updated pointers back
 - in mpls_lse() the route is not always freed
Looks a little better now.
--
Kick MPLS packets earlier.
--
Several changes:
 * Declare TRIM_LABEL as a function.
 * In mpls_unlabel_inet, copy the label locally. It's not incorrect to
   keep a pointer on the mbuf, but it's bug-friendly.
 * In mpls_label_inetX, fix the length check. Meanwhile add an XXX: we
   just want to make sure that m_copydata won't fail, but if we were
   guaranteed that m has M_PKTHDR set, we could simply check the length
   against m->m_pkthdr.len.
--
Style in MPLS.
--
Add XXX.

diffstat:

 sys/net/if_mpls.c      |  177 +++++++++++++++++++++++++++++-------------------
 sys/netmpls/mpls_ttl.c |  127 +++++++++++++++++++----------------
 2 files changed, 174 insertions(+), 130 deletions(-)

diffs (truncated from 682 to 300 lines):

diff -r 87d325dcf052 -r 246ee25643b7 sys/net/if_mpls.c
--- a/sys/net/if_mpls.c Mon Feb 12 05:40:25 2018 +0000
+++ b/sys/net/if_mpls.c Mon Feb 12 18:18:00 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_mpls.c,v 1.29.8.1 2017/12/10 10:10:25 snj Exp $ */
+/*     $NetBSD: if_mpls.c,v 1.29.8.2 2018/02/12 18:18:00 snj Exp $ */
 
 /*
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_mpls.c,v 1.29.8.1 2017/12/10 10:10:25 snj Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_mpls.c,v 1.29.8.2 2018/02/12 18:18:00 snj Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -74,37 +74,27 @@
 
 #include "ioconf.h"
 
-#define TRIM_LABEL do { \
-       m_adj(m, sizeof(union mpls_shim)); \
-       if (m->m_len < sizeof(union mpls_shim) && \
-           (m = m_pullup(m, sizeof(union mpls_shim))) == NULL) \
-               goto done; \
-       dst.smpls_addr.s_addr = ntohl(mtod(m, union mpls_shim *)->s_addr); \
-       } while (/* CONSTCOND */ 0)
-
-
 static int mpls_clone_create(struct if_clone *, int);
 static int mpls_clone_destroy(struct ifnet *);
 
 static struct if_clone mpls_if_cloner =
        IF_CLONE_INITIALIZER("mpls", mpls_clone_create, mpls_clone_destroy);
 
-
 static void mpls_input(struct ifnet *, struct mbuf *);
 static int mpls_output(struct ifnet *, struct mbuf *, const struct sockaddr *,
-       const struct rtentry *);
+    const struct rtentry *);
 static int mpls_ioctl(struct ifnet *, u_long, void *);
 static int mpls_send_frame(struct mbuf *, struct ifnet *,
     const struct rtentry *);
 static int mpls_lse(struct mbuf *);
 
 #ifdef INET
-static int mpls_unlabel_inet(struct mbuf *);
+static struct mbuf *mpls_unlabel_inet(struct mbuf *, int *error);
 static struct mbuf *mpls_label_inet(struct mbuf *, union mpls_shim *, uint);
 #endif
 
 #ifdef INET6
-static int mpls_unlabel_inet6(struct mbuf *);
+static struct mbuf *mpls_unlabel_inet6(struct mbuf *, int *error);
 static struct mbuf *mpls_label_inet6(struct mbuf *, union mpls_shim *, uint);
 #endif
 
@@ -211,7 +201,6 @@
 void
 mplsintr(void)
 {
-
        struct mbuf *m;
 
        for (;;) {
@@ -279,7 +268,7 @@
                psize += sizeof(mh);
        }
 
-       switch(dst->sa_family) {
+       switch (dst->sa_family) {
 #ifdef INET
        case AF_INET:
                m = mpls_label_inet(m, &mh, psize - sizeof(struct sockaddr_mpls));
@@ -304,7 +293,7 @@
        ifp->if_opackets++;
        ifp->if_obytes += m->m_pkthdr.len;
 
-       if ((rt1=rtalloc1(rt->rt_gateway, 1)) == NULL) {
+       if ((rt1 = rtalloc1(rt->rt_gateway, 1)) == NULL) {
                m_freem(m);
                return EHOSTUNREACH;
        }
@@ -348,6 +337,20 @@
        return error;
 }
 
+static inline struct mbuf *
+mpls_trim_label(struct mbuf *m, union mpls_shim *sh)
+{
+       m_adj(m, sizeof(union mpls_shim));
+
+       if (m->m_len < sizeof(union mpls_shim) &&
+           (m = m_pullup(m, sizeof(union mpls_shim))) == NULL)
+               return NULL;
+
+       sh->s_addr = ntohl(mtod(m, union mpls_shim *)->s_addr);
+
+       return m;
+}
+
 /*
  * MPLS Label Switch Engine
  */
@@ -361,6 +364,12 @@
        uint psize = sizeof(struct sockaddr_mpls);
        bool push_back_alert = false;
 
+       /* If we're not accepting MPLS frames, leave now. */
+       if (!mpls_frame_accept) {
+               error = EINVAL;
+               goto done;
+       }
+
        if (m->m_len < sizeof(union mpls_shim) &&
            (m = m_pullup(m, sizeof(union mpls_shim))) == NULL)
                goto done;
@@ -369,26 +378,31 @@
        dst.smpls_family = AF_MPLS;
        dst.smpls_addr.s_addr = ntohl(mtod(m, union mpls_shim *)->s_addr);
 
-       /* Check if we're accepting MPLS Frames */
        error = EINVAL;
-       if (!mpls_frame_accept)
-               goto done;
 
        /* TTL decrement */
        if ((m = mpls_ttl_dec(m)) == NULL)
                goto done;
 
        /* RFC 4182 */
-       if (mpls_rfc4182 != 0)
-               while((dst.smpls_addr.shim.label == MPLS_LABEL_IPV4NULL ||
+       if (mpls_rfc4182 != 0) {
+               while ((dst.smpls_addr.shim.label == MPLS_LABEL_IPV4NULL ||
                    dst.smpls_addr.shim.label == MPLS_LABEL_IPV6NULL) &&
-                   __predict_false(dst.smpls_addr.shim.bos == 0))
-                       TRIM_LABEL;
+                   __predict_false(dst.smpls_addr.shim.bos == 0)) {
+                       m = mpls_trim_label(m, &dst.smpls_addr);
+                       if (m == NULL) {
+                               goto done;
+                       }
+               }
+       }
 
        /* RFC 3032 Section 2.1 Page 4 */
        if (__predict_false(dst.smpls_addr.shim.label == MPLS_LABEL_RTALERT) &&
            dst.smpls_addr.shim.bos == 0) {
-               TRIM_LABEL;
+               m = mpls_trim_label(m, &dst.smpls_addr);
+               if (m == NULL) {
+                       goto done;
+               }
                push_back_alert = true;
        }
 
@@ -398,15 +412,17 @@
 #ifdef INET
                case MPLS_LABEL_IPV4NULL:
                        /* Pop shim and push mbuf to IP stack */
-                       if (dst.smpls_addr.shim.bos)
-                               error = mpls_unlabel_inet(m);
+                       if (dst.smpls_addr.shim.bos) {
+                               m = mpls_unlabel_inet(m, &error);
+                       }
                        break;
 #endif
 #ifdef INET6
                case MPLS_LABEL_IPV6NULL:
                        /* Pop shim and push mbuf to IPv6 stack */
-                       if (dst.smpls_addr.shim.bos)
-                               error = mpls_unlabel_inet6(m);
+                       if (dst.smpls_addr.shim.bos) {
+                               m = mpls_unlabel_inet6(m, &error);
+                       }
                        break;
 #endif
                case MPLS_LABEL_RTALERT:        /* Yeah, I'm all alerted */
@@ -423,9 +439,9 @@
                goto done;
 
        /* Get a route to dst */
-       dst.smpls_addr.shim.ttl =
-           dst.smpls_addr.shim.bos =
-           dst.smpls_addr.shim.exp = 0;
+       dst.smpls_addr.shim.ttl = 0;
+       dst.smpls_addr.shim.bos = 0;
+       dst.smpls_addr.shim.exp = 0;
        dst.smpls_addr.s_addr = htonl(dst.smpls_addr.s_addr);
        if ((rt = rtalloc1((const struct sockaddr*)&dst, 1)) == NULL)
                goto done;
@@ -460,8 +476,10 @@
                tshim.shim.bos = tshim.shim.exp = 0;
                tshim.shim.ttl = mpls_defttl;
                if (tshim.shim.label != MPLS_LABEL_IMPLNULL &&
-                   ((m = mpls_prepend_shim(m, &tshim)) == NULL))
-                       return ENOBUFS;
+                   ((m = mpls_prepend_shim(m, &tshim)) == NULL)) {
+                       error = ENOBUFS;
+                       goto done;
+               }
                psize += sizeof(tshim);
        }
 
@@ -471,8 +489,10 @@
                tshim.s_addr = MPLS_LABEL_RTALERT;
                tshim.shim.bos = tshim.shim.exp = 0;
                tshim.shim.ttl = mpls_defttl;
-               if ((m = mpls_prepend_shim(m, &tshim)) == NULL)
-                       return ENOBUFS;
+               if ((m = mpls_prepend_shim(m, &tshim)) == NULL) {
+                       error = ENOBUFS;
+                       goto done;
+               }
        }
 
        if ((rt->rt_flags & RTF_GATEWAY) == 0) {
@@ -523,67 +543,73 @@
        return 0;
 }
 
-
-
 #ifdef INET
-static int
-mpls_unlabel_inet(struct mbuf *m)
+static struct mbuf *
+mpls_unlabel_inet(struct mbuf *m, int *error)
 {
        struct ip *iph;
-       union mpls_shim *ms;
+       union mpls_shim ms;
        int iphlen;
 
        if (mpls_mapttl_inet || mpls_mapprec_inet) {
-
                /* get shim info */
-               ms = mtod(m, union mpls_shim *);
-               ms->s_addr = ntohl(ms->s_addr);
+               ms.s_addr = ntohl(mtod(m, union mpls_shim *)->s_addr);
 
                /* and get rid of it */
                m_adj(m, sizeof(union mpls_shim));
 
                /* get ip header */
-               if (m->m_len < sizeof (struct ip) &&
-                   (m = m_pullup(m, sizeof(struct ip))) == NULL)
-                       return ENOBUFS;
+               if (m->m_len < sizeof(struct ip) &&
+                   (m = m_pullup(m, sizeof(struct ip))) == NULL) {
+                       *error = ENOBUFS;
+                       return NULL;
+               }
+
                iph = mtod(m, struct ip *);
                iphlen = iph->ip_hl << 2;
 
                /* get it all */
                if (m->m_len < iphlen) {
-                       if ((m = m_pullup(m, iphlen)) == NULL)
-                               return ENOBUFS;
+                       if ((m = m_pullup(m, iphlen)) == NULL) {
+                               *error = ENOBUFS;
+                               return NULL;
+                       }
                        iph = mtod(m, struct ip *);
                }
 
                /* check ipsum */
                if (in_cksum(m, iphlen) != 0) {
                        m_freem(m);
-                       return EINVAL;
+                       *error = EINVAL;
+                       return NULL;
                }
 
                /* set IP ttl from MPLS ttl */
                if (mpls_mapttl_inet)
-                       iph->ip_ttl = ms->shim.ttl;
+                       iph->ip_ttl = ms.shim.ttl;
 
                /* set IP Precedence from MPLS Exp */
                if (mpls_mapprec_inet) {
                        iph->ip_tos = (iph->ip_tos << 3) >> 3;
-                       iph->ip_tos |= ms->shim.exp << 5;
+                       iph->ip_tos |= ms.shim.exp << 5;
                }
 
                /* reset ipsum because we modified TTL and TOS */
                iph->ip_sum = 0;
                iph->ip_sum = in_cksum(m, iphlen);
-       } else
+       } else {
                m_adj(m, sizeof(union mpls_shim));
+       }
 
        /* Put it on IP queue */



Home | Main Index | Thread Index | Old Index