Source-Changes-HG archive

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

[src/trunk]: src/sys/net Several changes:



details:   https://anonhg.NetBSD.org/src/rev/f4ca0cc77b04
branches:  trunk
changeset: 829152:f4ca0cc77b04
user:      maxv <maxv%NetBSD.org@localhost>
date:      Fri Jan 19 15:04:29 2018 +0000

description:
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.

diffstat:

 sys/net/if_mpls.c |  68 +++++++++++++++++++++++++++++++++---------------------
 1 files changed, 41 insertions(+), 27 deletions(-)

diffs (167 lines):

diff -r 80ac97934f31 -r f4ca0cc77b04 sys/net/if_mpls.c
--- a/sys/net/if_mpls.c Fri Jan 19 14:30:09 2018 +0000
+++ b/sys/net/if_mpls.c Fri Jan 19 15:04:29 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_mpls.c,v 1.32 2017/12/09 10:30:30 maxv Exp $ */
+/*     $NetBSD: if_mpls.c,v 1.33 2018/01/19 15:04:29 maxv 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.32 2017/12/09 10:30:30 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_mpls.c,v 1.33 2018/01/19 15:04:29 maxv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -74,22 +74,12 @@
 
 #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 *);
@@ -303,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;
        }
@@ -347,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
  */
@@ -384,14 +388,21 @@
        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;
        }
 
@@ -428,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;
@@ -537,13 +548,12 @@
 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));
@@ -576,12 +586,12 @@
 
                /* 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 */
@@ -611,9 +621,11 @@
        struct ip iphdr;
 
        if (mpls_mapttl_inet || mpls_mapprec_inet) {
-               if ((m->m_len < sizeof(struct ip)) &&
+               /* XXX Maybe just check m->m_pkthdr.len instead? */
+               if ((m->m_len < offset + sizeof(struct ip)) &&
                    (m = m_pullup(m, offset + sizeof(struct ip))) == 0)
-                       return NULL; /* XXX */
+                       return NULL;
+
                m_copydata(m, offset, sizeof(struct ip), &iphdr);
 
                /* Map TTL */
@@ -674,9 +686,11 @@
        struct ip6_hdr ip6h;
 
        if (mpls_mapttl_inet6 || mpls_mapclass_inet6) {
-               if (m->m_len < sizeof(struct ip6_hdr) &&
+               /* XXX Maybe just check m->m_pkthdr.len instead? */
+               if ((m->m_len < offset + sizeof(struct ip6_hdr)) &&
                    (m = m_pullup(m, offset + sizeof(struct ip6_hdr))) == 0)
                        return NULL;
+
                m_copydata(m, offset, sizeof(struct ip6_hdr), &ip6h);
 
                if (mpls_mapttl_inet6)



Home | Main Index | Thread Index | Old Index