]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-snmp.c
CI: Add warning exemptions for Sun C (suncc-5.15) on Solaris 10
[tcpdump] / print-snmp.c
index 8a13adcbb80da39d28b6de45d6aebca6415a1db3..e1cc97711967a4cccf26e2aaa72fcad1108f6413 100644 (file)
 
 /* \summary: Simple Network Management Protocol (SNMP) printer */
 
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
+#include <config.h>
 
-#include <netdissect-stdinc.h>
+#include "netdissect-stdinc.h"
 
 #include <stdio.h>
 #include <string.h>
+#include <limits.h>
 
 #ifdef USE_LIBSMI
 #include <smi.h>
 #endif
 
+#include "netdissect-ctype.h"
+
+#define ND_LONGJMP_FROM_TCHECK
 #include "netdissect.h"
 #include "extract.h"
 
 #undef OPAQUE  /* defined in <wingdi.h> */
 
-static const char tstr[] = "[|snmp]";
 
 /*
  * Universal ASN.1 types
@@ -421,15 +422,14 @@ asn1_parse(netdissect_options *ndo,
            const u_char *p, u_int len, struct be *elem)
 {
        u_char form, class, id;
-       int i, hdr;
+       u_int i, hdr;
 
        elem->asnlen = 0;
        elem->type = BE_ANY;
        if (len < 1) {
                ND_PRINT("[nothing to parse]");
-               return -1;
+               goto invalid;
        }
-       ND_TCHECK_1(p);
 
        /*
         * it would be nice to use a bit field, but you can't depend on them.
@@ -438,14 +438,14 @@ asn1_parse(netdissect_options *ndo,
         *  +---+---+---+---+---+---+---+---+
         *    7   6   5   4   3   2   1   0
         */
-       id = EXTRACT_U_1(p) & ASN_ID_BITS;              /* lower 5 bits, range 00-1f */
+       id = GET_U_1(p) & ASN_ID_BITS;          /* lower 5 bits, range 00-1f */
 #ifdef notdef
-       form = (EXTRACT_U_1(p) & 0xe0) >> 5;    /* move upper 3 bits to lower 3 */
+       form = (GET_U_1(p) & 0xe0) >> 5;        /* move upper 3 bits to lower 3 */
        class = form >> 1;              /* bits 7&6 -> bits 1&0, range 0-3 */
        form &= 0x1;                    /* bit 5 -> bit 0, range 0-1 */
 #else
-       form = (u_char)(EXTRACT_U_1(p) & ASN_FORM_BITS) >> ASN_FORM_SHIFT;
-       class = (u_char)(EXTRACT_U_1(p) & ASN_CLASS_BITS) >> ASN_CLASS_SHIFT;
+       form = (u_char)(GET_U_1(p) & ASN_FORM_BITS) >> ASN_FORM_SHIFT;
+       class = (u_char)(GET_U_1(p) & ASN_CLASS_BITS) >> ASN_CLASS_SHIFT;
 #endif
        elem->form = form;
        elem->class = class;
@@ -464,63 +464,58 @@ asn1_parse(netdissect_options *ndo,
                 * that won't fit in 32 bits.
                 */
                id = 0;
-               ND_TCHECK_1(p);
-               while (EXTRACT_U_1(p) & ASN_BIT8) {
+               while (GET_U_1(p) & ASN_BIT8) {
                        if (len < 1) {
                                ND_PRINT("[Xtagfield?]");
-                               return -1;
+                               goto invalid;
                        }
-                       id = (id << 7) | (EXTRACT_U_1(p) & ~ASN_BIT8);
+                       id = (id << 7) | (GET_U_1(p) & ~ASN_BIT8);
                        len--;
                        hdr++;
                        p++;
-                       ND_TCHECK_1(p);
                }
                if (len < 1) {
                        ND_PRINT("[Xtagfield?]");
-                       return -1;
+                       goto invalid;
                }
-               ND_TCHECK_1(p);
-               elem->id = id = (id << 7) | EXTRACT_U_1(p);
+               elem->id = id = (id << 7) | GET_U_1(p);
                --len;
                ++hdr;
                ++p;
        }
        if (len < 1) {
                ND_PRINT("[no asnlen]");
-               return -1;
+               goto invalid;
        }
-       ND_TCHECK_1(p);
-       elem->asnlen = EXTRACT_U_1(p);
+       elem->asnlen = GET_U_1(p);
        p++; len--; hdr++;
        if (elem->asnlen & ASN_BIT8) {
                uint32_t noct = elem->asnlen % ASN_BIT8;
                elem->asnlen = 0;
                if (len < noct) {
                        ND_PRINT("[asnlen? %d<%d]", len, noct);
-                       return -1;
+                       goto invalid;
                }
-               ND_TCHECK_LEN(p, noct);
-               for (; noct-- > 0; len--, hdr++) {
-                       elem->asnlen = (elem->asnlen << ASN_SHIFT8) | EXTRACT_U_1(p);
+               for (; noct != 0; len--, hdr++, noct--) {
+                       elem->asnlen = (elem->asnlen << ASN_SHIFT8) | GET_U_1(p);
                        p++;
                }
        }
        if (len < elem->asnlen) {
                ND_PRINT("[len%d<asnlen%u]", len, elem->asnlen);
-               return -1;
+               goto invalid;
        }
        if (form >= sizeof(Form)/sizeof(Form[0])) {
                ND_PRINT("[form?%d]", form);
-               return -1;
+               goto invalid;
        }
        if (class >= sizeof(Class)/sizeof(Class[0])) {
                ND_PRINT("[class?%c/%d]", *Form[form], class);
-               return -1;
+               goto invalid;
        }
        if ((int)id >= Class[class].numIDs) {
                ND_PRINT("[id?%c/%s/%d]", *Form[form], Class[class].name, id);
-               return -1;
+               goto invalid;
        }
        ND_TCHECK_LEN(p, elem->asnlen);
 
@@ -535,18 +530,18 @@ asn1_parse(netdissect_options *ndo,
                                break;
 
                        case INTEGER: {
-                               int32_t data;
+                               uint32_t data;
                                elem->type = BE_INT;
                                data = 0;
 
                                if (elem->asnlen == 0) {
                                        ND_PRINT("[asnlen=0]");
-                                       return -1;
+                                       goto invalid;
                                }
-                               if (EXTRACT_U_1(p) & ASN_BIT8)  /* negative */
-                                       data = -1;
-                               for (i = elem->asnlen; i-- > 0; p++)
-                                       data = (data << ASN_SHIFT8) | EXTRACT_U_1(p);
+                               if (GET_U_1(p) & ASN_BIT8)      /* negative */
+                                       data = UINT_MAX;
+                               for (i = elem->asnlen; i != 0; p++, i--)
+                                       data = (data << ASN_SHIFT8) | GET_U_1(p);
                                elem->data.integer = data;
                                break;
                        }
@@ -582,8 +577,8 @@ asn1_parse(netdissect_options *ndo,
                                uint32_t data;
                                elem->type = BE_UNS;
                                data = 0;
-                               for (i = elem->asnlen; i-- > 0; p++)
-                                       data = (data << 8) + EXTRACT_U_1(p);
+                               for (i = elem->asnlen; i != 0; p++, i--)
+                                       data = (data << 8) + GET_U_1(p);
                                elem->data.uns = data;
                                break;
                        }
@@ -592,8 +587,8 @@ asn1_parse(netdissect_options *ndo,
                                uint64_t data64;
                                elem->type = BE_UNS64;
                                data64 = 0;
-                               for (i = elem->asnlen; i-- > 0; p++)
-                                       data64 = (data64 << 8) + EXTRACT_U_1(p);
+                               for (i = elem->asnlen; i != 0; p++, i--)
+                                       data64 = (data64 << 8) + GET_U_1(p);
                                elem->data.uns64 = data64;
                                break;
                        }
@@ -668,29 +663,22 @@ asn1_parse(netdissect_options *ndo,
        len -= elem->asnlen;
        return elem->asnlen + hdr;
 
-trunc:
-       ND_PRINT("%s", tstr);
+invalid:
        return -1;
 }
 
-static int
+static void
 asn1_print_octets(netdissect_options *ndo, struct be *elem)
 {
        const u_char *p = (const u_char *)elem->data.raw;
        uint32_t asnlen = elem->asnlen;
        uint32_t i;
 
-       ND_TCHECK_LEN(p, asnlen);
-       for (i = asnlen; i-- > 0; p++)
-               ND_PRINT("_%.2x", EXTRACT_U_1(p));
-       return 0;
-
-trunc:
-       ND_PRINT("%s", tstr);
-       return -1;
+       for (i = asnlen; i != 0; p++, i--)
+               ND_PRINT("_%.2x", GET_U_1(p));
 }
 
-static int
+static void
 asn1_print_string(netdissect_options *ndo, struct be *elem)
 {
        int printable = 1, first = 1;
@@ -699,28 +687,19 @@ asn1_print_string(netdissect_options *ndo, struct be *elem)
        uint32_t i;
 
        p = elem->data.str;
-       ND_TCHECK_LEN(p, asnlen);
-       for (i = asnlen; printable && i-- > 0; p++)
-               printable = ND_ISPRINT(EXTRACT_U_1(p));
+       for (i = asnlen; printable && i != 0; p++, i--)
+               printable = ND_ASCII_ISPRINT(GET_U_1(p));
        p = elem->data.str;
        if (printable) {
                ND_PRINT("\"");
-               if (fn_printn(ndo, p, asnlen, ndo->ndo_snapend)) {
-                       ND_PRINT("\"");
-                       goto trunc;
-               }
+               nd_printjn(ndo, p, asnlen);
                ND_PRINT("\"");
        } else {
-               for (i = asnlen; i-- > 0; p++) {
-                       ND_PRINT(first ? "%.2x" : "_%.2x", EXTRACT_U_1(p));
+               for (i = asnlen; i != 0; p++, i--) {
+                       ND_PRINT(first ? "%.2x" : "_%.2x", GET_U_1(p));
                        first = 0;
                }
        }
-       return 0;
-
-trunc:
-       ND_PRINT("%s", tstr);
-       return -1;
 }
 
 /*
@@ -739,15 +718,15 @@ asn1_print(netdissect_options *ndo,
        switch (elem->type) {
 
        case BE_OCTET:
-               if (asn1_print_octets(ndo, elem) == -1)
-                       return -1;
+               asn1_print_octets(ndo, elem);
                break;
 
        case BE_NULL:
                break;
 
        case BE_OID: {
-               int o = 0, first = -1;
+               int first = -1;
+               uint32_t o = 0;
 
                p = (const u_char *)elem->data.raw;
                i = asnlen;
@@ -756,8 +735,7 @@ asn1_print(netdissect_options *ndo,
                        for (; a->node; a++) {
                                if (i < a->oid_len)
                                        continue;
-                               if (!ND_TTEST_LEN(p, a->oid_len))
-                                       continue;
+                               ND_TCHECK_LEN(p, a->oid_len);
                                if (memcmp(a->oid, p, a->oid_len) == 0) {
                                        objp = a->node->child;
                                        i -= a->oid_len;
@@ -769,10 +747,9 @@ asn1_print(netdissect_options *ndo,
                        }
                }
 
-               for (; i-- > 0; p++) {
-                       ND_TCHECK_1(p);
-                       o = (o << ASN_SHIFT7) + (EXTRACT_U_1(p) & ~ASN_BIT8);
-                       if (EXTRACT_U_1(p) & ASN_LONGLEN)
+               for (; i != 0; p++, i--) {
+                       o = (o << ASN_SHIFT7) + (GET_U_1(p) & ~ASN_BIT8);
+                       if (GET_U_1(p) & ASN_LONGLEN)
                                continue;
 
                        /*
@@ -811,8 +788,7 @@ asn1_print(netdissect_options *ndo,
                break;
 
        case BE_STR:
-               if (asn1_print_string(ndo, elem) == -1)
-                       return -1;
+               asn1_print_string(ndo, elem);
                break;
 
        case BE_SEQ:
@@ -823,9 +799,8 @@ asn1_print(netdissect_options *ndo,
                if (asnlen != ASNLEN_INETADDR)
                        ND_PRINT("[inetaddr len!=%d]", ASNLEN_INETADDR);
                p = (const u_char *)elem->data.raw;
-               ND_TCHECK_LEN(p, asnlen);
-               for (i = asnlen; i-- != 0; p++) {
-                       ND_PRINT((i == asnlen-1) ? "%u" : ".%u", EXTRACT_U_1(p));
+               for (i = asnlen; i != 0; p++, i--) {
+                       ND_PRINT((i == asnlen) ? "%u" : ".%u", GET_U_1(p));
                }
                break;
 
@@ -847,11 +822,11 @@ asn1_print(netdissect_options *ndo,
                ND_PRINT("[be!?]");
                break;
        }
+       /* This function now always returns 0. Don't make it void yet, as other
+        * code checks for negative result and this function might need to signal
+        * invalid data later.
+        */
        return 0;
-
-trunc:
-       ND_PRINT("%s", tstr);
-       return -1;
 }
 
 #ifdef notdef
@@ -870,7 +845,7 @@ asn1_decode(u_char *p, u_int length)
        struct be elem;
        int i = 0;
 
-       while (i >= 0 && length > 0) {
+       while (i >= 0 && length != 0) {
                i = asn1_parse(ndo, p, length, &elem);
                if (i >= 0) {
                        ND_PRINT(" ");
@@ -918,13 +893,13 @@ smi_decode_oid(netdissect_options *ndo,
 {
        const u_char *p = (const u_char *)elem->data.raw;
        uint32_t asnlen = elem->asnlen;
-       int o = 0, first = -1, i = asnlen;
+       uint32_t i = asnlen;
+       int o = 0, first = -1;
        unsigned int firstval;
 
-       for (*oidlen = 0; i-- > 0; p++) {
-               ND_TCHECK_1(p);
-               o = (o << ASN_SHIFT7) + (EXTRACT_U_1(p) & ~ASN_BIT8);
-               if (EXTRACT_U_1(p) & ASN_LONGLEN)
+       for (*oidlen = 0; i != 0; p++, i--) {
+               o = (o << ASN_SHIFT7) + (GET_U_1(p) & ~ASN_BIT8);
+               if (GET_U_1(p) & ASN_LONGLEN)
                    continue;
 
                /*
@@ -932,7 +907,7 @@ smi_decode_oid(netdissect_options *ndo,
                 * (see X.690:1997 clause 8.19 for the details)
                 */
                if (first < 0) {
-                       first = 0;
+                       first = 0;
                        firstval = o / OIDMUX;
                        if (firstval > 2) firstval = 2;
                        o -= firstval * OIDMUX;
@@ -946,10 +921,6 @@ smi_decode_oid(netdissect_options *ndo,
                o = 0;
        }
        return 0;
-
-trunc:
-       ND_PRINT("%s", tstr);
-       return -1;
 }
 
 static int smi_check_type(SmiBasetype basetype, int be)
@@ -1097,7 +1068,7 @@ smi_print_value(netdissect_options *ndo,
        }
 
        if (NOTIFY_CLASS(pduid) && smiNode->access < SMI_ACCESS_NOTIFY) {
-           ND_PRINT("[notNotifyable]");
+           ND_PRINT("[notNotifiable]");
        }
 
        if (READ_CLASS(pduid) && smiNode->access < SMI_ACCESS_READ_ONLY) {
@@ -1226,7 +1197,7 @@ varbind_print(netdissect_options *ndo,
               u_short pduid, const u_char *np, u_int length)
 {
        struct be elem;
-       int count = 0, ind;
+       int count = 0;
 #ifdef USE_LIBSMI
        SmiNode *smiNode = NULL;
 #endif
@@ -1246,7 +1217,7 @@ varbind_print(netdissect_options *ndo,
        length = elem.asnlen;
        np = (const u_char *)elem.data.raw;
 
-       for (ind = 1; length > 0; ind++) {
+       while (length) {
                const u_char *vbend;
                u_int vblength;
 
@@ -1389,7 +1360,6 @@ snmppdu_print(netdissect_options *ndo,
        np += count;
 
        varbind_print(ndo, pduid, np, length);
-       return;
 }
 
 /*
@@ -1480,7 +1450,6 @@ trappdu_print(netdissect_options *ndo,
        np += count;
 
        varbind_print(ndo, TRAP, np, length);
-       return;
 }
 
 /*
@@ -1578,8 +1547,7 @@ scopedpdu_print(netdissect_options *ndo,
        np += count;
 
        ND_PRINT("E=");
-       if (asn1_print_octets(ndo, &elem) == -1)
-               return;
+       asn1_print_octets(ndo, &elem);
        ND_PRINT(" ");
 
        /* contextName (OCTET STRING) */
@@ -1594,8 +1562,7 @@ scopedpdu_print(netdissect_options *ndo,
        np += count;
 
        ND_PRINT("C=");
-       if (asn1_print_string(ndo, &elem) == -1)
-               return;
+       asn1_print_string(ndo, &elem);
        ND_PRINT(" ");
 
        pdu_print(ndo, np, length, version);
@@ -1625,8 +1592,7 @@ community_print(netdissect_options *ndo,
                    sizeof(DEF_COMMUNITY) - 1) == 0)) {
                /* ! "public" */
                ND_PRINT("C=");
-               if (asn1_print_string(ndo, &elem) == -1)
-                       return;
+               asn1_print_string(ndo, &elem);
                ND_PRINT(" ");
        }
        length -= count;
@@ -1705,8 +1671,7 @@ usm_print(netdissect_options *ndo,
         np += count;
 
        ND_PRINT("U=");
-       if (asn1_print_string(ndo, &elem) == -1)
-               return;
+       asn1_print_string(ndo, &elem);
        ND_PRINT(" ");
 
        /* msgAuthenticationParameters (OCTET STRING) */
@@ -1798,7 +1763,7 @@ v3msg_print(netdissect_options *ndo,
                ND_PRINT("[msgFlags size %d]", elem.asnlen);
                return;
        }
-       flags = EXTRACT_U_1(elem.data.str);
+       flags = GET_U_1(elem.data.str);
        if (flags != 0x00 && flags != 0x01 && flags != 0x03
            && flags != 0x04 && flags != 0x05 && flags != 0x07) {
                ND_PRINT("[msgFlags=0x%02X]", flags);
@@ -1883,6 +1848,7 @@ snmp_print(netdissect_options *ndo,
        int count = 0;
        int version = 0;
 
+       ndo->ndo_protocol = "snmp";
        ND_PRINT(" ");
 
        /* initial Sequence */