X-Git-Url: https://git.tcpdump.org/tcpdump/blobdiff_plain/0cb34b7b447beb77d800246ad3742e8b1294b88f..e5ec0d6b59b02423ef6a29544bca34b62745c180:/print-snmp.c diff --git a/print-snmp.c b/print-snmp.c index 3ca21901..1b096dcf 100644 --- a/print-snmp.c +++ b/print-snmp.c @@ -276,28 +276,46 @@ static struct obj { * Currently, this includes the prefixes for the Internet MIB, the * private enterprises tree, and the experimental tree. */ +#define OID_FIRST_OCTET(x, y) (((x)*40) + (y)) /* X.690 8.19.4 */ + +#ifndef NO_ABREV_MIB +static const uint8_t mib_oid[] = { OID_FIRST_OCTET(1, 3), 6, 1, 2, 1 }; +#endif +#ifndef NO_ABREV_ENTER +static const uint8_t enterprises_oid[] = { OID_FIRST_OCTET(1, 3), 6, 1, 4, 1 }; +#endif +#ifndef NO_ABREV_EXPERI +static const uint8_t experimental_oid[] = { OID_FIRST_OCTET(1, 3), 6, 1, 3 }; +#endif +#ifndef NO_ABBREV_SNMPMODS +static const uint8_t snmpModules_oid[] = { OID_FIRST_OCTET(1, 3), 6, 1, 6, 3 }; +#endif + +#define OBJ_ABBREV_ENTRY(prefix, obj) \ + { prefix, &_ ## obj ## _obj, obj ## _oid, sizeof (obj ## _oid) } static const struct obj_abrev { const char *prefix; /* prefix for this abrev */ struct obj *node; /* pointer into object table */ - const char *oid; /* ASN.1 encoded OID */ + const uint8_t *oid; /* ASN.1 encoded OID */ + size_t oid_len; /* length of OID */ } obj_abrev_list[] = { #ifndef NO_ABREV_MIB /* .iso.org.dod.internet.mgmt.mib */ - { "", &_mib_obj, "\53\6\1\2\1" }, + OBJ_ABBREV_ENTRY("", mib), #endif #ifndef NO_ABREV_ENTER /* .iso.org.dod.internet.private.enterprises */ - { "E:", &_enterprises_obj, "\53\6\1\4\1" }, + OBJ_ABBREV_ENTRY("E:", enterprises), #endif #ifndef NO_ABREV_EXPERI /* .iso.org.dod.internet.experimental */ - { "X:", &_experimental_obj, "\53\6\1\3" }, + OBJ_ABBREV_ENTRY("X:", experimental), #endif #ifndef NO_ABBREV_SNMPMODS /* .iso.org.dod.internet.snmpV2.snmpModules */ - { "S:", &_snmpModules_obj, "\53\6\1\6\3" }, + OBJ_ABBREV_ENTRY("S:", snmpModules), #endif - { 0,0,0 } + { 0,0,0,0 } }; /* @@ -501,6 +519,7 @@ asn1_parse(netdissect_options *ndo, ND_PRINT((ndo, "[id?%c/%s/%d]", *Form[form], Class[class].name, id)); return -1; } + ND_TCHECK2(*p, elem->asnlen); switch (form) { case PRIMITIVE: @@ -521,7 +540,6 @@ asn1_parse(netdissect_options *ndo, ND_PRINT((ndo, "[asnlen=0]")); return -1; } - ND_TCHECK2(*p, elem->asnlen); if (*p & ASN_BIT8) /* negative */ data = -1; for (i = elem->asnlen; i-- > 0; p++) @@ -559,7 +577,6 @@ asn1_parse(netdissect_options *ndo, case GAUGE: case TIMETICKS: { register uint32_t data; - ND_TCHECK2(*p, elem->asnlen); elem->type = BE_UNS; data = 0; for (i = elem->asnlen; i-- > 0; p++) @@ -570,7 +587,6 @@ asn1_parse(netdissect_options *ndo, case COUNTER64: { register uint64_t data64; - ND_TCHECK2(*p, elem->asnlen); elem->type = BE_UNS64; data64 = 0; for (i = elem->asnlen; i-- > 0; p++) @@ -609,7 +625,6 @@ asn1_parse(netdissect_options *ndo, default: ND_PRINT((ndo, "[P/%s/%s]", Class[class].name, Class[class].Id[id])); - ND_TCHECK2(*p, elem->asnlen); elem->type = BE_OCTET; elem->data.raw = (const uint8_t *)p; break; @@ -733,51 +748,49 @@ asn1_print(netdissect_options *ndo, p = (const u_char *)elem->data.raw; i = asnlen; - if (!nd_smi_module_loaded) { - if (!ndo->ndo_nflag && asnlen > 2) { - const struct obj_abrev *a = &obj_abrev_list[0]; - for (; a->node; a++) { - size_t a_len = strlen(a->oid); - ND_TCHECK2(*p, a_len); - if (memcmp(a->oid, p, a_len) == 0) { - objp = a->node->child; - if (i < a_len) - goto trunc; - i -= a_len; - p += a_len; - ND_PRINT((ndo, "%s", a->prefix)); - first = 1; - break; - } + if (!ndo->ndo_nflag && asnlen > 2) { + const struct obj_abrev *a = &obj_abrev_list[0]; + for (; a->node; a++) { + if (i < a->oid_len) + continue; + if (!ND_TTEST2(*p, a->oid_len)) + continue; + if (memcmp(a->oid, p, a->oid_len) == 0) { + objp = a->node->child; + i -= a->oid_len; + p += a->oid_len; + ND_PRINT((ndo, "%s", a->prefix)); + first = 1; + break; } } + } - for (; i-- > 0; p++) { - ND_TCHECK(*p); - o = (o << ASN_SHIFT7) + (*p & ~ASN_BIT8); - if (*p & ASN_LONGLEN) - continue; - - /* - * first subitem encodes two items with - * 1st*OIDMUX+2nd - * (see X.690:1997 clause 8.19 for the details) - */ - if (first < 0) { - int s; - if (!ndo->ndo_nflag) - objp = mibroot; - first = 0; - s = o / OIDMUX; - if (s > 2) s = 2; - OBJ_PRINT(s, first); - o -= s * OIDMUX; - } - OBJ_PRINT(o, first); - if (--first < 0) - first = 0; - o = 0; + for (; i-- > 0; p++) { + ND_TCHECK(*p); + o = (o << ASN_SHIFT7) + (*p & ~ASN_BIT8); + if (*p & ASN_LONGLEN) + continue; + + /* + * first subitem encodes two items with + * 1st*OIDMUX+2nd + * (see X.690:1997 clause 8.19 for the details) + */ + if (first < 0) { + int s; + if (!ndo->ndo_nflag) + objp = mibroot; + first = 0; + s = o / OIDMUX; + if (s > 2) s = 2; + OBJ_PRINT(s, first); + o -= s * OIDMUX; } + OBJ_PRINT(o, first); + if (--first < 0) + first = 0; + o = 0; } break; } @@ -905,31 +918,29 @@ smi_decode_oid(netdissect_options *ndo, int o = 0, first = -1, i = asnlen; unsigned int firstval; - if (nd_smi_module_loaded) { - for (*oidlen = 0; i-- > 0; p++) { - ND_TCHECK(*p); - o = (o << ASN_SHIFT7) + (*p & ~ASN_BIT8); - if (*p & ASN_LONGLEN) - continue; + for (*oidlen = 0; i-- > 0; p++) { + ND_TCHECK(*p); + o = (o << ASN_SHIFT7) + (*p & ~ASN_BIT8); + if (*p & ASN_LONGLEN) + continue; - /* - * first subitem encodes two items with 1st*OIDMUX+2nd - * (see X.690:1997 clause 8.19 for the details) - */ - if (first < 0) { - first = 0; - firstval = o / OIDMUX; - if (firstval > 2) firstval = 2; - o -= firstval * OIDMUX; - if (*oidlen < oidsize) { - oid[(*oidlen)++] = firstval; - } - } + /* + * first subitem encodes two items with 1st*OIDMUX+2nd + * (see X.690:1997 clause 8.19 for the details) + */ + if (first < 0) { + first = 0; + firstval = o / OIDMUX; + if (firstval > 2) firstval = 2; + o -= firstval * OIDMUX; if (*oidlen < oidsize) { - oid[(*oidlen)++] = o; + oid[(*oidlen)++] = firstval; } - o = 0; } + if (*oidlen < oidsize) { + oid[(*oidlen)++] = o; + } + o = 0; } return 0; @@ -1036,6 +1047,10 @@ smi_print_variable(netdissect_options *ndo, SmiNode *smiNode = NULL; unsigned int i; + if (!nd_smi_module_loaded) { + *status = asn1_print(ndo, elem); + return NULL; + } *status = smi_decode_oid(ndo, elem, oid, sizeof(oid) / sizeof(unsigned int), &oidlen); if (*status < 0) @@ -1121,22 +1136,24 @@ smi_print_value(netdissect_options *ndo, if (smiType->basetype == SMI_BASETYPE_BITS) { /* print bit labels */ } else { - smi_decode_oid(ndo, elem, oid, - sizeof(oid)/sizeof(unsigned int), - &oidlen); - smiNode = smiGetNodeByOID(oidlen, oid); - if (smiNode) { - if (ndo->ndo_vflag) { - ND_PRINT((ndo, "%s::", smiGetNodeModule(smiNode)->name)); - } - ND_PRINT((ndo, "%s", smiNode->name)); - if (smiNode->oidlen < oidlen) { - for (i = smiNode->oidlen; - i < oidlen; i++) { - ND_PRINT((ndo, ".%u", oid[i])); + if (nd_smi_module_loaded && + smi_decode_oid(ndo, elem, oid, + sizeof(oid)/sizeof(unsigned int), + &oidlen) == 0) { + smiNode = smiGetNodeByOID(oidlen, oid); + if (smiNode) { + if (ndo->ndo_vflag) { + ND_PRINT((ndo, "%s::", smiGetNodeModule(smiNode)->name)); + } + ND_PRINT((ndo, "%s", smiNode->name)); + if (smiNode->oidlen < oidlen) { + for (i = smiNode->oidlen; + i < oidlen; i++) { + ND_PRINT((ndo, ".%u", oid[i])); + } } + done++; } - done++; } } break;