]> The Tcpdump Group git mirrors - tcpdump/commitdiff
DECnet: Modernize packet parsing style.
authorDenis Ovsienko <[email protected]>
Fri, 16 Oct 2020 20:10:50 +0000 (21:10 +0100)
committerDenis Ovsienko <[email protected]>
Fri, 16 Oct 2020 20:10:50 +0000 (21:10 +0100)
Enable ND_LONGJMP_FROM_TCHECK. Remove a number of redundant
ND_TCHECK_SIZE() instances. Add and use standard invalid sections, in a
few cases print the reason for declaring the packet invalid. Make a few
functions void. Lose an unused structure. Update a test case.

print-decnet.c
tests/decnet.out

index 500cca35fc16bfbc650fe07191ec8822d4646883..85ccf267892bd0e229fafa61c8c7842330f0667e 100644 (file)
@@ -31,6 +31,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#define ND_LONGJMP_FROM_TCHECK
 #include "netdissect.h"
 #include "extract.h"
 #include "addrtoname.h"
@@ -157,18 +158,6 @@ union routehdr
 /*
  * Define control message formats.
  */
-struct initmsgIII                      /* phase III initialization message */
-  {
-    byte       inIII_flags;            /* route flags */
-    word       inIII_src;              /* source node address */
-    byte       inIII_info;             /* routing layer information */
-    word       inIII_blksize;          /* maximum data link block size */
-    byte       inIII_vers;             /* version number */
-    byte       inIII_eco;              /* ECO number */
-    byte       inIII_ueco;             /* user ECO number */
-    byte       inIII_rsvd;             /* reserved image field */
-  };
-
 struct initmsg                         /* initialization message */
   {
     byte       in_flags;               /* route flags */
@@ -469,10 +458,10 @@ struct dcmsg                              /* disconnect confirm message */
 /* Forwards */
 static int print_decnet_ctlmsg(netdissect_options *, const union routehdr *, u_int, u_int);
 static void print_t_info(netdissect_options *, u_int);
-static int print_l1_routes(netdissect_options *, const u_char *, u_int);
-static int print_l2_routes(netdissect_options *, const u_char *, u_int);
+static void print_l1_routes(netdissect_options *, const u_char *, u_int);
+static void print_l2_routes(netdissect_options *, const u_char *, u_int);
 static void print_i_info(netdissect_options *, u_int);
-static int print_elist(const u_char *, u_int);
+static void print_elist(const u_char *, u_int);
 static int print_nsp(netdissect_options *, const u_char *, u_int);
 static void print_reason(netdissect_options *, u_int);
 
@@ -490,18 +479,18 @@ decnet_print(netdissect_options *ndo,
 
        ndo->ndo_protocol = "decnet";
        if (length < sizeof(struct shorthdr)) {
-               nd_print_trunc(ndo);
-               return;
+               ND_PRINT(" (length %u < %u)", length, (unsigned)sizeof(struct shorthdr));
+               goto invalid;
        }
 
        pktlen = GET_LE_U_2(ap);
        if (pktlen < sizeof(struct shorthdr)) {
-               nd_print_trunc(ndo);
-               return;
+               ND_PRINT(" (pktlen %u < %u)", pktlen, (unsigned)sizeof(struct shorthdr));
+               goto invalid;
        }
        if (pktlen > length) {
-               nd_print_trunc(ndo);
-               return;
+               ND_PRINT(" (pktlen %u > %u)", pktlen, length);
+               goto invalid;
        }
        length = pktlen;
 
@@ -514,8 +503,8 @@ decnet_print(netdissect_options *ndo,
            if (ndo->ndo_vflag)
                ND_PRINT("[pad:%u] ", padlen);
            if (length < padlen + 2) {
-               nd_print_trunc(ndo);
-               return;
+               ND_PRINT(" (length %u < %u)", length, padlen + 2);
+               goto invalid;
            }
            ND_TCHECK_LEN(ap + sizeof(short), padlen);
            ap += padlen;
@@ -534,15 +523,15 @@ decnet_print(netdissect_options *ndo,
        /* is it a control message? */
        if (mflags & RMF_CTLMSG) {
                if (!print_decnet_ctlmsg(ndo, rhp, length, caplen))
-                       goto trunc;
+                       goto invalid;
                return;
        }
 
        switch (mflags & RMF_MASK) {
        case RMF_LONG:
            if (length < sizeof(struct longhdr)) {
-               nd_print_trunc(ndo);
-               return;
+               ND_PRINT(" (length %u < %u)", length, (unsigned)sizeof(struct longhdr));
+               goto invalid;
            }
            ND_TCHECK_SIZE(&rhp->rh_long);
            dst =
@@ -554,7 +543,6 @@ decnet_print(netdissect_options *ndo,
            nsplen = length - sizeof(struct longhdr);
            break;
        case RMF_SHORT:
-           ND_TCHECK_SIZE(&rhp->rh_short);
            dst = GET_LE_U_2(rhp->rh_short.sh_dst);
            src = GET_LE_U_2(rhp->rh_short.sh_src);
            hops = (GET_U_1(rhp->rh_short.sh_visits) & VIS_MASK)+1;
@@ -580,11 +568,11 @@ decnet_print(netdissect_options *ndo,
        }
 
        if (!print_nsp(ndo, nspp, nsplen))
-               goto trunc;
+               goto invalid;
        return;
 
-trunc:
-       nd_print_trunc(ndo);
+invalid:
+       nd_print_invalid(ndo);
 }
 
 static int
@@ -599,13 +587,12 @@ print_decnet_ctlmsg(netdissect_options *ndo,
        u_int info, blksize, eco, ueco, hello, other, vers;
        u_int priority;
        const u_char *rhpx = (const u_char *)rhp;
-       int ret;
 
        switch (mflags & RMF_CTLMASK) {
        case RMF_INIT:
            ND_PRINT("init ");
            if (length < sizeof(struct initmsg))
-               goto trunc;
+               goto invalid;
            ND_TCHECK_SIZE(&cmp->cm_init);
            src = GET_LE_U_2(cmp->cm_init.in_src);
            info = GET_U_1(cmp->cm_init.in_info);
@@ -618,52 +605,47 @@ print_decnet_ctlmsg(netdissect_options *ndo,
            ND_PRINT("src %sblksize %u vers %u eco %u ueco %u hello %u",
                        dnaddr_string(ndo, src), blksize, vers, eco, ueco,
                        hello);
-           ret = 1;
            break;
        case RMF_VER:
            ND_PRINT("verification ");
            if (length < sizeof(struct verifmsg))
-               goto trunc;
-           ND_TCHECK_SIZE(&cmp->cm_ver);
+               goto invalid;
            src = GET_LE_U_2(cmp->cm_ver.ve_src);
            other = GET_U_1(cmp->cm_ver.ve_fcnval);
            ND_PRINT("src %s fcnval %o", dnaddr_string(ndo, src), other);
-           ret = 1;
            break;
        case RMF_TEST:
            ND_PRINT("test ");
            if (length < sizeof(struct testmsg))
-               goto trunc;
-           ND_TCHECK_SIZE(&cmp->cm_test);
+               goto invalid;
            src = GET_LE_U_2(cmp->cm_test.te_src);
            other = GET_U_1(cmp->cm_test.te_data);
            ND_PRINT("src %s data %o", dnaddr_string(ndo, src), other);
-           ret = 1;
            break;
        case RMF_L1ROUT:
            ND_PRINT("lev-1-routing ");
            if (length < sizeof(struct l1rout))
-               goto trunc;
+               goto invalid;
            ND_TCHECK_SIZE(&cmp->cm_l1rou);
            src = GET_LE_U_2(cmp->cm_l1rou.r1_src);
            ND_PRINT("src %s ", dnaddr_string(ndo, src));
-           ret = print_l1_routes(ndo, &(rhpx[sizeof(struct l1rout)]),
+           print_l1_routes(ndo, &(rhpx[sizeof(struct l1rout)]),
                                length - sizeof(struct l1rout));
            break;
        case RMF_L2ROUT:
            ND_PRINT("lev-2-routing ");
            if (length < sizeof(struct l2rout))
-               goto trunc;
+               goto invalid;
            ND_TCHECK_SIZE(&cmp->cm_l2rout);
            src = GET_LE_U_2(cmp->cm_l2rout.r2_src);
            ND_PRINT("src %s ", dnaddr_string(ndo, src));
-           ret = print_l2_routes(ndo, &(rhpx[sizeof(struct l2rout)]),
+           print_l2_routes(ndo, &(rhpx[sizeof(struct l2rout)]),
                                length - sizeof(struct l2rout));
            break;
        case RMF_RHELLO:
            ND_PRINT("router-hello ");
            if (length < sizeof(struct rhellomsg))
-               goto trunc;
+               goto invalid;
            ND_TCHECK_SIZE(&cmp->cm_rhello);
            vers = GET_U_1(cmp->cm_rhello.rh_vers);
            eco = GET_U_1(cmp->cm_rhello.rh_eco);
@@ -678,14 +660,13 @@ print_decnet_ctlmsg(netdissect_options *ndo,
            ND_PRINT("vers %u eco %u ueco %u src %s blksize %u pri %u hello %u",
                        vers, eco, ueco, dnaddr_string(ndo, src),
                        blksize, priority, hello);
-           ret = print_elist(&(rhpx[sizeof(struct rhellomsg)]),
+           print_elist(&(rhpx[sizeof(struct rhellomsg)]),
                                length - sizeof(struct rhellomsg));
            break;
        case RMF_EHELLO:
            ND_PRINT("endnode-hello ");
            if (length < sizeof(struct ehellomsg))
-               goto trunc;
-           ND_TCHECK_SIZE(&cmp->cm_ehello);
+               goto invalid;
            vers = GET_U_1(cmp->cm_ehello.eh_vers);
            eco = GET_U_1(cmp->cm_ehello.eh_eco);
            ueco = GET_U_1(cmp->cm_ehello.eh_ueco);
@@ -702,18 +683,16 @@ print_decnet_ctlmsg(netdissect_options *ndo,
            ND_PRINT("vers %u eco %u ueco %u src %s blksize %u rtr %s hello %u data %o",
                        vers, eco, ueco, dnaddr_string(ndo, src),
                        blksize, dnaddr_string(ndo, dst), hello, other);
-           ret = 1;
            break;
 
        default:
            ND_PRINT("unknown control message");
            ND_DEFAULTPRINT((const u_char *)rhp, ND_MIN(length, caplen));
-           ret = 1;
            break;
        }
-       return (ret);
+       return (1);
 
-trunc:
+invalid:
        return (0);
 }
 
@@ -734,7 +713,7 @@ print_t_info(netdissect_options *ndo,
            ND_PRINT("blo ");
 }
 
-static int
+static void
 print_l1_routes(netdissect_options *ndo,
                 const u_char *rp, u_int len)
 {
@@ -747,7 +726,7 @@ print_l1_routes(netdissect_options *ndo,
            ND_TCHECK_LEN(rp, 3 * sizeof(short));
            count = GET_LE_U_2(rp);
            if (count > 1024)
-               return (1);     /* seems to be bogus from here on */
+               return; /* seems to be bogus from here on */
            rp += sizeof(short);
            len -= sizeof(short);
            id = GET_LE_U_2(rp);
@@ -759,13 +738,9 @@ print_l1_routes(netdissect_options *ndo,
            ND_PRINT("{ids %u-%u cost %u hops %u} ", id, id + count,
                            RI_COST(info), RI_HOPS(info));
        }
-       return (1);
-
-trunc:
-       return (0);
 }
 
-static int
+static void
 print_l2_routes(netdissect_options *ndo,
                 const u_char *rp, u_int len)
 {
@@ -778,7 +753,7 @@ print_l2_routes(netdissect_options *ndo,
            ND_TCHECK_LEN(rp, 3 * sizeof(short));
            count = GET_LE_U_2(rp);
            if (count > 1024)
-               return (1);     /* seems to be bogus from here on */
+               return; /* seems to be bogus from here on */
            rp += sizeof(short);
            len -= sizeof(short);
            area = GET_LE_U_2(rp);
@@ -790,10 +765,6 @@ print_l2_routes(netdissect_options *ndo,
            ND_PRINT("{areas %u-%u cost %u hops %u} ", area, area + count,
                            RI_COST(info), RI_HOPS(info));
        }
-       return (1);
-
-trunc:
-       return (0);
 }
 
 static void
@@ -815,11 +786,10 @@ print_i_info(netdissect_options *ndo,
            ND_PRINT("blo ");
 }
 
-static int
+static void
 print_elist(const u_char *elp _U_, u_int len _U_)
 {
        /* Not enough examples available for me to debug this */
-       return (1);
 }
 
 static int
@@ -829,9 +799,10 @@ print_nsp(netdissect_options *ndo,
        const struct nsphdr *nsphp = (const struct nsphdr *)nspp;
        u_int dst, src, flags;
 
-       if (nsplen < sizeof(struct nsphdr))
-               goto trunc;
-       ND_TCHECK_SIZE(nsphp);
+       if (nsplen < sizeof(struct nsphdr)) {
+               ND_PRINT(" (nsplen %u < %u)", nsplen, (unsigned)sizeof(struct nsphdr));
+               goto invalid;
+       }
        flags = GET_U_1(nsphp->nh_flags);
        dst = GET_LE_U_2(nsphp->nh_dst);
        src = GET_LE_U_2(nsphp->nh_src);
@@ -850,7 +821,7 @@ print_nsp(netdissect_options *ndo,
                    u_int data_off = sizeof(struct minseghdr);
 
                    if (nsplen < data_off)
-                       goto trunc;
+                       goto invalid;
                    ack = GET_LE_U_2(shp->sh_seq[0]);
                    if (ack & SGQ_ACK) {        /* acknum field */
                        if ((ack & SGQ_NAK) == SGQ_NAK)
@@ -859,7 +830,7 @@ print_nsp(netdissect_options *ndo,
                            ND_PRINT("ack %u ", ack & SGQ_MASK);
                        data_off += sizeof(short);
                        if (nsplen < data_off)
-                           goto trunc;
+                           goto invalid;
                        ack = GET_LE_U_2(shp->sh_seq[1]);
                        if (ack & SGQ_OACK) {   /* ackoth field */
                            if ((ack & SGQ_ONAK) == SGQ_ONAK)
@@ -868,7 +839,7 @@ print_nsp(netdissect_options *ndo,
                                ND_PRINT("oack %u ", ack & SGQ_MASK);
                            data_off += sizeof(short);
                            if (nsplen < data_off)
-                               goto trunc;
+                               goto invalid;
                            ack = GET_LE_U_2(shp->sh_seq[2]);
                        }
                    }
@@ -883,7 +854,7 @@ print_nsp(netdissect_options *ndo,
                    u_int data_off = sizeof(struct minseghdr);
 
                    if (nsplen < data_off)
-                       goto trunc;
+                       goto invalid;
                    ack = GET_LE_U_2(shp->sh_seq[0]);
                    if (ack & SGQ_ACK) {        /* acknum field */
                        if ((ack & SGQ_NAK) == SGQ_NAK)
@@ -892,7 +863,7 @@ print_nsp(netdissect_options *ndo,
                            ND_PRINT("ack %u ", ack & SGQ_MASK);
                        data_off += sizeof(short);
                        if (nsplen < data_off)
-                           goto trunc;
+                           goto invalid;
                        ack = GET_LE_U_2(shp->sh_seq[1]);
                        if (ack & SGQ_OACK) {   /* ackdat field */
                            if ((ack & SGQ_ONAK) == SGQ_ONAK)
@@ -901,7 +872,7 @@ print_nsp(netdissect_options *ndo,
                                ND_PRINT("ackdat %u ", ack & SGQ_MASK);
                            data_off += sizeof(short);
                            if (nsplen < data_off)
-                               goto trunc;
+                               goto invalid;
                            ack = GET_LE_U_2(shp->sh_seq[2]);
                        }
                    }
@@ -918,7 +889,7 @@ print_nsp(netdissect_options *ndo,
                    u_int lsflags, fcval;
 
                    if (nsplen < sizeof(struct seghdr) + sizeof(struct lsmsg))
-                       goto trunc;
+                       goto invalid;
                    ack = GET_LE_U_2(shp->sh_seq[0]);
                    if (ack & SGQ_ACK) {        /* acknum field */
                        if ((ack & SGQ_NAK) == SGQ_NAK)
@@ -935,7 +906,6 @@ print_nsp(netdissect_options *ndo,
                        }
                    }
                    ND_PRINT("seg %u ", ack & SGQ_MASK);
-                   ND_TCHECK_SIZE(lsmp);
                    lsflags = GET_U_1(lsmp->ls_lsflags);
                    fcval = GET_U_1(lsmp->ls_fcval);
                    switch (lsflags & LSI_MASK) {
@@ -978,7 +948,7 @@ print_nsp(netdissect_options *ndo,
                    u_int ack;
 
                    if (nsplen < sizeof(struct ackmsg))
-                       goto trunc;
+                       goto invalid;
                    ND_TCHECK_SIZE(amp);
                    ack = GET_LE_U_2(amp->ak_acknum[0]);
                    if (ack & SGQ_ACK) {        /* acknum field */
@@ -1003,7 +973,7 @@ print_nsp(netdissect_options *ndo,
                    u_int ack;
 
                    if (nsplen < sizeof(struct ackmsg))
-                       goto trunc;
+                       goto invalid;
                    ND_TCHECK_SIZE(amp);
                    ack = GET_LE_U_2(amp->ak_acknum[0]);
                    if (ack & SGQ_ACK) {        /* acknum field */
@@ -1043,8 +1013,7 @@ print_nsp(netdissect_options *ndo,
                    u_int services, info, segsize;
 
                    if (nsplen < sizeof(struct cimsg))
-                       goto trunc;
-                   ND_TCHECK_SIZE(cimp);
+                       goto invalid;
                    services = GET_U_1(cimp->ci_services);
                    info = GET_U_1(cimp->ci_info);
                    segsize = GET_LE_U_2(cimp->ci_segsize);
@@ -1084,8 +1053,7 @@ print_nsp(netdissect_options *ndo,
                    u_int segsize, optlen;
 
                    if (nsplen < sizeof(struct ccmsg))
-                       goto trunc;
-                   ND_TCHECK_SIZE(ccmp);
+                       goto invalid;
                    services = GET_U_1(ccmp->cc_services);
                    info = GET_U_1(ccmp->cc_info);
                    segsize = GET_LE_U_2(ccmp->cc_segsize);
@@ -1129,8 +1097,7 @@ print_nsp(netdissect_options *ndo,
                    u_int optlen;
 
                    if (nsplen < sizeof(struct dimsg))
-                       goto trunc;
-                   ND_TCHECK_SIZE(dimp);
+                       goto invalid;
                    reason = GET_LE_U_2(dimp->di_reason);
                    optlen = GET_U_1(dimp->di_optlen);
 
@@ -1146,7 +1113,6 @@ print_nsp(netdissect_options *ndo,
                    const struct dcmsg *dcmp = (const struct dcmsg *)nspp;
                    u_int reason;
 
-                   ND_TCHECK_SIZE(dcmp);
                    reason = GET_LE_U_2(dcmp->dc_reason);
 
                    print_reason(ndo, reason);
@@ -1163,7 +1129,7 @@ print_nsp(netdissect_options *ndo,
        }
        return (1);
 
-trunc:
+invalid:
        return (0);
 }
 
index 3eef546d30c96ad3e743dd9da19c54e447e7167c..8b67361116bbee4aa6baaa7dd878213ee12872fd 100644 (file)
@@ -4,7 +4,7 @@
     4  19:34:55.597814 endnode-hello endnode vers 2 eco 0 ueco 0 src 1.1 blksize 16434 rtr 0.0 hello 10 data 2
     5  19:35:05.597817 endnode-hello endnode vers 2 eco 0 ueco 0 src 1.1 blksize 16434 rtr 0.0 hello 10 data 2
     6  19:35:11.338778 1.1 > 1.1 34 conn-initiate 8195>0 ver 4.1 segsize 16403 
-    7  19:35:11.339327 1.1 > 1.1 9  [|decnet]
+    7  19:35:11.339327 1.1 > 1.1 9  (nsplen 3 < 5) (invalid)
     8  19:35:11.339336 1.1 > 1.1 16 conn-confirm 8196>8195 ver 4.1 segsize 16403 
     9  19:35:11.339342 1.1 > 1.1 19 link-service 8195>8196 ack 0 ackdat 0 seg 1 dat seg count 0 
    10  19:35:11.339350 1.1 > 1.1 15 ils-ack 8196>8195 ack 1 ackdat 0 
@@ -18,7 +18,7 @@
    18  19:35:11.342876 1.1 > 1.1 15 data-ack 8195>8196 ack 2 oack 0 
    19  19:35:15.597819 endnode-hello endnode vers 2 eco 0 ueco 0 src 1.1 blksize 16434 rtr 0.0 hello 10 data 2
    20  19:35:16.403594 1.1 > 1.1 34 conn-initiate 8197>0 ver 4.1 segsize 16403 
-   21  19:35:16.403633 1.1 > 1.1 9  [|decnet]
+   21  19:35:16.403633 1.1 > 1.1 9  (nsplen 3 < 5) (invalid)
    22  19:35:16.403641 1.1 > 1.1 16 conn-confirm 8198>8197 ver 4.1 segsize 16403 
    23  19:35:16.403647 1.1 > 1.1 19 link-service 8197>8198 ack 0 ackdat 0 seg 1 dat seg count 0 
    24  19:35:16.403656 1.1 > 1.1 15 ils-ack 8198>8197 ack 1 ackdat 0