Source-Changes-HG archive

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

[src/trunk]: src/sys/net Refactoring functions for RCR and RCN



details:   https://anonhg.NetBSD.org/src/rev/303fc56fc013
branches:  trunk
changeset: 946370:303fc56fc013
user:      yamaguchi <yamaguchi%NetBSD.org@localhost>
date:      Wed Nov 25 09:16:20 2020 +0000

description:
Refactoring functions for RCR and RCN

diffstat:

 sys/net/if_spppsubr.c |  435 ++++++++++++++++++++++++++++++-------------------
 1 files changed, 268 insertions(+), 167 deletions(-)

diffs (truncated from 817 to 300 lines):

diff -r 6d57356b16ed -r 303fc56fc013 sys/net/if_spppsubr.c
--- a/sys/net/if_spppsubr.c     Wed Nov 25 09:12:50 2020 +0000
+++ b/sys/net/if_spppsubr.c     Wed Nov 25 09:16:20 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_spppsubr.c,v 1.192 2020/11/25 09:12:50 yamaguchi Exp $       */
+/*     $NetBSD: if_spppsubr.c,v 1.193 2020/11/25 09:16:20 yamaguchi Exp $       */
 
 /*
  * Synchronous PPP/Cisco link level subroutines.
@@ -41,7 +41,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_spppsubr.c,v 1.192 2020/11/25 09:12:50 yamaguchi Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_spppsubr.c,v 1.193 2020/11/25 09:16:20 yamaguchi Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_inet.h"
@@ -2294,21 +2294,30 @@
  * transition decision in the state automaton.)
  */
 static int
-sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
+sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int origlen)
 {
        STDDCL;
-       u_char *buf, *r, *p, l, blen;
-       int origlen, rlen;
+       u_char *buf, *r, *p, l, blen, type;
+       int len, rlen;
        uint32_t nmagic;
        u_short authproto;
 
        KASSERT(SPPP_WLOCKED(sp));
 
-       len -= 4;
-       origlen = len;
-       buf = r = malloc (blen = len, M_TEMP, M_NOWAIT);
-       if (! buf)
-               return (0);
+       if (origlen < sizeof(*h))
+               return 0;
+
+       origlen -= sizeof(*h);
+       type = 0;
+
+       if (origlen <= 0)
+               return 0;
+       else
+               blen = origlen;
+
+       buf = kmem_intr_alloc(blen, KM_NOSLEEP);
+       if (buf == NULL)
+               return 0;
 
        if (debug)
                log(LOG_DEBUG, "%s: lcp parse opts:",
@@ -2316,7 +2325,13 @@
 
        /* pass 1: check for things that need to be rejected */
        p = (void *)(h + 1);
-       for (rlen = 0; len > 1 && (l = p[1]) != 0; len -= l, p += l) {
+       r = buf;
+       rlen = 0;
+       for (len = origlen; len > 1; len-= l, p += l) {
+               l = p[1];
+               if (l == 0)
+                       break;
+
                /* Sanity check option length */
                if (l > len) {
                        /*
@@ -2326,11 +2341,12 @@
                        addlog("%s: received malicious LCP option 0x%02x, "
                            "length 0x%02x, (len: 0x%02x) dropping.\n", ifp->if_xname,
                            p[0], l, len);
-                       goto drop;
+                       rlen = -1;
+                       goto end;
                }
                if (debug)
                        addlog(" %s", sppp_lcp_opt_name(*p));
-               switch (*p) {
+               switch (p[0]) {
                case LCP_OPT_MAGIC:
                        /* Magic number. */
                        /* fall through, both are same length */
@@ -2418,12 +2434,13 @@
                r += l;
                rlen += l;
        }
-       if (rlen) {
-               if (debug)
-                       addlog(" send conf-rej\n");
-               sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
+
+       if (rlen > 0) {
+               type = CONF_REJ;
                goto end;
-       } else if (debug)
+       }
+
+       if (debug)
                addlog("\n");
 
        /*
@@ -2435,11 +2452,16 @@
                    ifp->if_xname);
 
        p = (void *)(h + 1);
-       len = origlen;
-       for (rlen = 0; len > 1 && (l = p[1]) != 0; len -= l, p += l) {
+       r = buf;
+       rlen = 0;
+       for (len = origlen; len > 0; len -= l, p += l) {
+               l = p[1];
+               if (l == 0)
+                       break;
+
                if (debug)
                        addlog(" %s", sppp_lcp_opt_name(*p));
-               switch (*p) {
+               switch (p[0]) {
                case LCP_OPT_MAGIC:
                        /* Magic number -- extract. */
                        nmagic = (uint32_t)p[2] << 24 |
@@ -2560,34 +2582,41 @@
                r += l;
                rlen += l;
        }
-       if (rlen) {
+
+       if (rlen > 0) {
                if (++sp->scp[IDX_LCP].fail_counter >= sp->lcp.max_failure) {
                        if (debug)
-                               addlog(" max_failure (%d) exceeded, "
-                                      "send conf-rej\n",
-                                      sp->lcp.max_failure);
-                       sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
+                               addlog(" max_failure (%d) exceeded, ",
+                                   sp->lcp.max_failure);
+                       type = CONF_REJ;
                } else {
-                       if (debug)
-                               addlog(" send conf-nak\n");
-                       sppp_cp_send(sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
+                       type = CONF_NAK;
                }
-               goto end;
        } else {
-               if (debug)
-                       addlog(" send conf-ack\n");
+               type = CONF_ACK;
+               rlen = origlen;
+               memcpy(r, h + 1, rlen);
                sp->scp[IDX_LCP].fail_counter = 0;
                sp->pp_loopcnt = 0;
-               sppp_cp_send(sp, PPP_LCP, CONF_ACK, h->ident, origlen, h + 1);
+       }
+
+end:
+       if (rlen > 0) {
+               if (debug)
+                       addlog("send %s", sppp_cp_type_name(type));
+               sppp_cp_send(sp, PPP_LCP, type, h->ident, rlen, buf);
        }
 
- end:
-       free(buf, M_TEMP);
-       return (rlen == 0);
-
- drop:
-       free(buf, M_TEMP);
-       return -1;
+       if (debug)
+               addlog("\n");
+
+       kmem_free(buf, blen);
+
+       if (rlen > 0)
+               return -1;
+       if (type != CONF_ACK)
+               return 0;
+       return 1;
 }
 
 /*
@@ -2598,15 +2627,15 @@
 sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
 {
        STDDCL;
-       u_char *buf, *p, l;
+       u_char *p, l;
 
        KASSERT(SPPP_WLOCKED(sp));
 
-       len -= 4;
-       buf = malloc (len, M_TEMP, M_NOWAIT);
-       if (!buf)
+       if (len <= sizeof(*h))
                return;
 
+       len -= sizeof(*h);
+
        if (debug)
                log(LOG_DEBUG, "%s: lcp rej opts:",
                    ifp->if_xname);
@@ -2621,11 +2650,11 @@
                         */
                        addlog("%s: received malicious LCP option, "
                            "dropping.\n", ifp->if_xname);
-                       goto drop;
+                       goto end;
                }
                if (debug)
                        addlog(" %s", sppp_lcp_opt_name(*p));
-               switch (*p) {
+               switch (p[0]) {
                case LCP_OPT_MAGIC:
                        /* Magic number -- can't use it, use 0 */
                        sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
@@ -2668,8 +2697,7 @@
        }
        if (debug)
                addlog("\n");
-drop:
-       free(buf, M_TEMP);
+end:
        return;
 }
 
@@ -2681,15 +2709,14 @@
 sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
 {
        STDDCL;
-       u_char *buf, *p, l, blen;
+       u_char *p, l;
        uint32_t magic;
 
        KASSERT(SPPP_WLOCKED(sp));
 
-       len -= 4;
-       buf = malloc (blen = len, M_TEMP, M_NOWAIT);
-       if (!buf)
+       if (len <= sizeof(*h))
                return;
+       len -= sizeof(*h);
 
        if (debug)
                log(LOG_DEBUG, "%s: lcp nak opts:",
@@ -2705,11 +2732,11 @@
                         */
                        addlog("%s: received malicious LCP option, "
                            "dropping.\n", ifp->if_xname);
-                       goto drop;
+                       goto end;
                }
                if (debug)
                        addlog(" %s", sppp_lcp_opt_name(*p));
-               switch (*p) {
+               switch (p[0]) {
                case LCP_OPT_MAGIC:
                        /* Magic number -- renegotiate */
                        if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
@@ -2761,8 +2788,7 @@
        }
        if (debug)
                addlog("\n");
-drop:
-       free(buf, M_TEMP);
+end:
        return;
 }
 
@@ -3092,42 +3118,54 @@
  * transition decision in the state automaton.)
  */
 static int
-sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
+sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int origlen)
 {
-       u_char *buf, *r, *p, l, blen;
+       u_char *buf, *r, *p, l, blen, type;
        struct ifnet *ifp = &sp->pp_if;
-       int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
+       int rlen, len, debug = ifp->if_flags & IFF_DEBUG;
        uint32_t hisaddr, desiredaddr;
 
        KASSERT(SPPP_WLOCKED(sp));
 
-       len -= 4;
-       origlen = len;
+       if (origlen < sizeof(*h))
+               return 0;
+
+       origlen -= sizeof(*h);
+       type = 0;
+
        /*
         * Make sure to allocate a buf that can at least hold a



Home | Main Index | Thread Index | Old Index