2 * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996, 1997
3 * John Robert LoVerso. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * This implementation has been influenced by the CMU SNMP release,
29 * by Steve Waldbusser. However, this shares no code with that system.
30 * Additional ASN.1 insight gained from Marshall T. Rose's _The_Open_Book_.
31 * Earlier forms of this implementation were derived and/or inspired by an
32 * awk script originally written by C. Philip Wood of LANL (but later
33 * heavily modified by John Robert LoVerso). The copyright notice for
34 * that work is preserved below, even though it may not rightly apply
37 * Support for SNMPv2c/SNMPv3 and the ability to link the module against
38 * the libsmi was added by J. Schoenwaelder, Copyright (c) 1999.
40 * This started out as a very simple program, but the incremental decoding
41 * (into the BE structure) complicated things.
43 # Los Alamos National Laboratory
45 # Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996, 1997
46 # This software was produced under a U.S. Government contract
47 # (W-7405-ENG-36) by Los Alamos National Laboratory, which is
48 # operated by the University of California for the U.S. Department
49 # of Energy. The U.S. Government is licensed to use, reproduce,
50 # and distribute this software. Permission is granted to the
51 # public to copy and use this software without charge, provided
52 # that this Notice and any statement of authorship are reproduced
53 # on all copies. Neither the Government nor the University makes
54 # any warranty, express or implied, or assumes any liability or
55 # responsibility for the use of this software.
56 # @(#)snmp.awk.x 1.1 (LANL) 1/15/90
63 #include <tcpdump-stdinc.h>
72 #include "interface.h"
74 #undef OPAQUE /* defined in <wingdi.h> */
76 static const char tstr
[] = "[|snmp]";
79 * Universal ASN.1 types
80 * (we only care about the tag values for those allowed in the Internet SMI)
82 static const char *Universal
[] = {
95 "U-8","U-9","U-10","U-11", /* 8-11 */
96 "U-12","U-13","U-14","U-15", /* 12-15 */
103 * Application-wide ASN.1 types from the Internet SMI and their tags
105 static const char *Application
[] = {
122 * Context-specific ASN.1 types for the SNMP PDUs and their tags
124 static const char *Context
[] = {
145 #define NOTIFY_CLASS(x) (x == TRAP || x == V2TRAP || x == INFORMREQ)
146 #define READ_CLASS(x) (x == GETREQ || x == GETNEXTREQ || x == GETBULKREQ)
147 #define WRITE_CLASS(x) (x == SETREQ)
148 #define RESPONSE_CLASS(x) (x == GETRESP)
149 #define INTERNAL_CLASS(x) (x == REPORT)
152 * Context-specific ASN.1 types for the SNMP Exceptions and their tags
154 static const char *Exceptions
[] = {
156 #define NOSUCHOBJECT 0
158 #define NOSUCHINSTANCE 1
160 #define ENDOFMIBVIEW 2
164 * Private ASN.1 types
165 * The Internet SMI does not specify any
167 static const char *Private
[] = {
172 * error-status values for any SNMP PDU
174 static const char *ErrorStatus
[] = {
188 "resourceUnavailable",
191 "authorizationError",
195 #define DECODE_ErrorStatus(e) \
196 ( e >= 0 && (size_t)e < sizeof(ErrorStatus)/sizeof(ErrorStatus[0]) \
198 : (snprintf(errbuf, sizeof(errbuf), "err=%u", e), errbuf))
201 * generic-trap values in the SNMP Trap-PDU
203 static const char *GenericTrap
[] = {
208 "authenticationFailure",
211 #define GT_ENTERPRISE 6
213 #define DECODE_GenericTrap(t) \
214 ( t >= 0 && (size_t)t < sizeof(GenericTrap)/sizeof(GenericTrap[0]) \
216 : (snprintf(buf, sizeof(buf), "gt=%d", t), buf))
219 * ASN.1 type class table
220 * Ties together the preceding Universal, Application, Context, and Private
223 #define defineCLASS(x) { "x", x, sizeof(x)/sizeof(x[0]) } /* not ANSI-C */
224 static const struct {
229 defineCLASS(Universal
),
231 defineCLASS(Application
),
232 #define APPLICATION 1
233 defineCLASS(Context
),
235 defineCLASS(Private
),
237 defineCLASS(Exceptions
),
242 * defined forms for ASN.1 types
244 static const char *Form
[] = {
248 #define CONSTRUCTED 1
252 * A structure for the OID tree for the compiled-in MIB.
253 * This is stored as a general-order tree.
256 const char *desc
; /* name of object */
257 u_char oid
; /* sub-id following parent */
258 u_char type
; /* object type (unused) */
259 struct obj
*child
, *next
; /* child and next sibling pointers */
263 * Include the compiled in SNMP MIB. "mib.h" is produced by feeding
264 * RFC-1156 format files into "makemib". "mib.h" MUST define at least
265 * a value for `mibroot'.
267 * In particular, this is gross, as this is including initialized structures,
268 * and by right shouldn't be an "include" file.
273 * This defines a list of OIDs which will be abbreviated on output.
274 * Currently, this includes the prefixes for the Internet MIB, the
275 * private enterprises tree, and the experimental tree.
277 static const struct obj_abrev
{
278 const char *prefix
; /* prefix for this abrev */
279 struct obj
*node
; /* pointer into object table */
280 const char *oid
; /* ASN.1 encoded OID */
281 } obj_abrev_list
[] = {
283 /* .iso.org.dod.internet.mgmt.mib */
284 { "", &_mib_obj
, "\53\6\1\2\1" },
286 #ifndef NO_ABREV_ENTER
287 /* .iso.org.dod.internet.private.enterprises */
288 { "E:", &_enterprises_obj
, "\53\6\1\4\1" },
290 #ifndef NO_ABREV_EXPERI
291 /* .iso.org.dod.internet.experimental */
292 { "X:", &_experimental_obj
, "\53\6\1\3" },
294 #ifndef NO_ABBREV_SNMPMODS
295 /* .iso.org.dod.internet.snmpV2.snmpModules */
296 { "S:", &_snmpModules_obj
, "\53\6\1\6\3" },
302 * This is used in the OID print routine to walk down the object tree
303 * rooted at `mibroot'.
305 #define OBJ_PRINT(o, suppressdot) \
309 if ((o) == objp->oid) \
311 } while ((objp = objp->next) != NULL); \
314 printf(suppressdot?"%s":".%s", objp->desc); \
315 objp = objp->child; \
317 printf(suppressdot?"%u":".%u", (o)); \
321 * This is the definition for the Any-Data-Type storage used purely for
322 * temporary internal representation while decoding an ASN.1 data stream.
337 u_char form
, class; /* tag info */
348 #define BE_INETADDR 8
351 #define BE_NOSUCHOBJECT 128
352 #define BE_NOSUCHINST 129
353 #define BE_ENDOFMIBVIEW 130
357 * SNMP versions recognized by this module
359 static const char *SnmpVersion
[] = {
361 #define SNMP_VERSION_1 0
363 #define SNMP_VERSION_2 1
365 #define SNMP_VERSION_2U 2
367 #define SNMP_VERSION_3 3
371 * Defaults for SNMP PDU components
373 #define DEF_COMMUNITY "public"
376 * constants for ASN.1 decoding
379 #define ASNLEN_INETADDR 4
382 #define ASN_BIT8 0x80
383 #define ASN_LONGLEN 0x80
385 #define ASN_ID_BITS 0x1f
386 #define ASN_FORM_BITS 0x20
387 #define ASN_FORM_SHIFT 5
388 #define ASN_CLASS_BITS 0xc0
389 #define ASN_CLASS_SHIFT 6
391 #define ASN_ID_EXT 0x1f /* extension ID in tag field */
394 * This decodes the next ASN.1 object in the stream pointed to by "p"
395 * (and of real-length "len") and stores the intermediate data in the
396 * provided BE object.
398 * This returns -l if it fails (i.e., the ASN.1 stream is not valid).
399 * O/w, this returns the number of bytes parsed from "p".
402 asn1_parse(register const u_char
*p
, u_int len
, struct be
*elem
)
404 u_char form
, class, id
;
410 fputs("[nothing to parse]", stdout
);
416 * it would be nice to use a bit field, but you can't depend on them.
417 * +---+---+---+---+---+---+---+---+
419 * +---+---+---+---+---+---+---+---+
422 id
= *p
& ASN_ID_BITS
; /* lower 5 bits, range 00-1f */
424 form
= (*p
& 0xe0) >> 5; /* move upper 3 bits to lower 3 */
425 class = form
>> 1; /* bits 7&6 -> bits 1&0, range 0-3 */
426 form
&= 0x1; /* bit 5 -> bit 0, range 0-1 */
428 form
= (u_char
)(*p
& ASN_FORM_BITS
) >> ASN_FORM_SHIFT
;
429 class = (u_char
)(*p
& ASN_CLASS_BITS
) >> ASN_CLASS_SHIFT
;
435 /* extended tag field */
436 if (id
== ASN_ID_EXT
) {
438 * The ID follows, as a sequence of octets with the
439 * 8th bit set and the remaining 7 bits being
440 * the next 7 bits of the value, terminated with
441 * an octet with the 8th bit not set.
443 * First, assemble all the octets with the 8th
444 * bit set. XXX - this doesn't handle a value
445 * that won't fit in 32 bits.
447 for (id
= 0; *p
& ASN_BIT8
; len
--, hdr
++, p
++) {
449 fputs("[Xtagfield?]", stdout
);
453 id
= (id
<< 7) | (*p
& ~ASN_BIT8
);
456 fputs("[Xtagfield?]", stdout
);
460 elem
->id
= id
= (id
<< 7) | *p
;
466 fputs("[no asnlen]", stdout
);
472 if (elem
->asnlen
& ASN_BIT8
) {
473 u_int32_t noct
= elem
->asnlen
% ASN_BIT8
;
476 printf("[asnlen? %d<%d]", len
, noct
);
480 for (; noct
-- > 0; len
--, hdr
++)
481 elem
->asnlen
= (elem
->asnlen
<< ASN_SHIFT8
) | *p
++;
483 if (len
< elem
->asnlen
) {
484 printf("[len%d<asnlen%u]", len
, elem
->asnlen
);
487 if (form
>= sizeof(Form
)/sizeof(Form
[0])) {
488 printf("[form?%d]", form
);
491 if (class >= sizeof(Class
)/sizeof(Class
[0])) {
492 printf("[class?%c/%d]", *Form
[form
], class);
495 if ((int)id
>= Class
[class].numIDs
) {
496 printf("[id?%c/%s/%d]", *Form
[form
], Class
[class].name
, id
);
511 register int32_t data
;
515 TCHECK2(*p
, elem
->asnlen
);
516 if (*p
& ASN_BIT8
) /* negative */
518 for (i
= elem
->asnlen
; i
-- > 0; p
++)
519 data
= (data
<< ASN_SHIFT8
) | *p
;
520 elem
->data
.integer
= data
;
526 elem
->data
.raw
= (caddr_t
)p
;
530 elem
->type
= BE_NULL
;
531 elem
->data
.raw
= NULL
;
535 elem
->type
= BE_OCTET
;
536 elem
->data
.raw
= (caddr_t
)p
;
538 Class
[class].Id
[id
]);
546 elem
->type
= BE_INETADDR
;
547 elem
->data
.raw
= (caddr_t
)p
;
553 register u_int32_t data
;
554 TCHECK2(*p
, elem
->asnlen
);
557 for (i
= elem
->asnlen
; i
-- > 0; p
++)
558 data
= (data
<< 8) + *p
;
559 elem
->data
.uns
= data
;
564 register u_int32_t high
, low
;
565 TCHECK2(*p
, elem
->asnlen
);
566 elem
->type
= BE_UNS64
;
568 for (i
= elem
->asnlen
; i
-- > 0; p
++) {
570 ((low
& 0xFF000000) >> 24);
571 low
= (low
<< 8) | *p
;
573 elem
->data
.uns64
.high
= high
;
574 elem
->data
.uns64
.low
= low
;
579 elem
->type
= BE_OCTET
;
580 elem
->data
.raw
= (caddr_t
)p
;
582 Class
[class].Id
[id
]);
590 elem
->type
= BE_NOSUCHOBJECT
;
591 elem
->data
.raw
= NULL
;
595 elem
->type
= BE_NOSUCHINST
;
596 elem
->data
.raw
= NULL
;
600 elem
->type
= BE_ENDOFMIBVIEW
;
601 elem
->data
.raw
= NULL
;
608 Class
[class].name
, Class
[class].Id
[id
]);
609 TCHECK2(*p
, elem
->asnlen
);
610 elem
->type
= BE_OCTET
;
611 elem
->data
.raw
= (caddr_t
)p
;
622 elem
->data
.raw
= (caddr_t
)p
;
626 elem
->type
= BE_OCTET
;
627 elem
->data
.raw
= (caddr_t
)p
;
628 printf("C/U/%s", Class
[class].Id
[id
]);
635 elem
->data
.raw
= (caddr_t
)p
;
639 elem
->type
= BE_OCTET
;
640 elem
->data
.raw
= (caddr_t
)p
;
642 Class
[class].name
, Class
[class].Id
[id
]);
649 return elem
->asnlen
+ hdr
;
657 * Display the ASN.1 object represented by the BE object.
658 * This used to be an integral part of asn1_parse() before the intermediate
662 asn1_print(struct be
*elem
)
664 u_char
*p
= (u_char
*)elem
->data
.raw
;
665 u_int32_t asnlen
= elem
->asnlen
;
668 switch (elem
->type
) {
672 for (i
= asnlen
; i
-- > 0; p
++)
680 int o
= 0, first
= -1, i
= asnlen
;
682 if (!sflag
&& !nflag
&& asnlen
> 2) {
683 const struct obj_abrev
*a
= &obj_abrev_list
[0];
684 size_t a_len
= strlen(a
->oid
);
685 for (; a
->node
; a
++) {
687 if (memcmp(a
->oid
, (char *)p
, a_len
) == 0) {
688 objp
= a
->node
->child
;
691 fputs(a
->prefix
, stdout
);
698 for (; !sflag
&& i
-- > 0; p
++) {
700 o
= (o
<< ASN_SHIFT7
) + (*p
& ~ASN_BIT8
);
701 if (*p
& ASN_LONGLEN
)
705 * first subitem encodes two items with 1st*OIDMUX+2nd
706 * (see X.690:1997 clause 8.19 for the details)
727 printf("%d", elem
->data
.integer
);
731 printf("%u", elem
->data
.uns
);
734 case BE_UNS64
: { /* idea borrowed from by Marshall Rose */
737 char *cpf
, *cpl
, last
[6], first
[30];
738 if (elem
->data
.uns64
.high
== 0) {
739 printf("%u", elem
->data
.uns64
.low
);
742 d
= elem
->data
.uns64
.high
* 4294967296.0; /* 2^32 */
743 if (elem
->data
.uns64
.high
<= 0x1fffff) {
744 d
+= elem
->data
.uns64
.low
;
745 #if 0 /*is looks illegal, but what is the intention?*/
752 d
+= (elem
->data
.uns64
.low
& 0xfffff000);
753 #if 0 /*is looks illegal, but what is the intention?*/
754 snprintf(first
, sizeof(first
), "%.f", d
);
756 snprintf(first
, sizeof(first
), "%f", d
);
758 snprintf(last
, sizeof(last
), "%5.5d",
759 elem
->data
.uns64
.low
& 0xfff);
760 for (carry
= 0, cpf
= first
+strlen(first
)-1, cpl
= last
+4;
763 j
= carry
+ (*cpf
- '0') + (*cpl
- '0');
772 fputs(first
, stdout
);
777 register int printable
= 1, first
= 1;
778 const u_char
*p
= elem
->data
.str
;
780 for (i
= asnlen
; printable
&& i
-- > 0; p
++)
781 printable
= ND_ISPRINT(*p
);
785 if (fn_printn(p
, asnlen
, snapend
)) {
791 for (i
= asnlen
; i
-- > 0; p
++) {
792 printf(first
? "%.2x" : "_%.2x", *p
);
799 printf("Seq(%u)", elem
->asnlen
);
803 if (asnlen
!= ASNLEN_INETADDR
)
804 printf("[inetaddr len!=%d]", ASNLEN_INETADDR
);
806 for (i
= asnlen
; i
-- != 0; p
++) {
807 printf((i
== asnlen
-1) ? "%u" : ".%u", *p
);
811 case BE_NOSUCHOBJECT
:
813 case BE_ENDOFMIBVIEW
:
814 printf("[%s]", Class
[EXCEPTIONS
].Id
[elem
->id
]);
819 Class
[CONTEXT
].Id
[elem
->id
], elem
->asnlen
);
823 fputs("[BE_ANY!?]", stdout
);
827 fputs("[be!?]", stdout
);
839 * This is a brute force ASN.1 printer: recurses to dump an entire structure.
840 * This will work for any ASN.1 stream, not just an SNMP PDU.
842 * By adding newlines and spaces at the correct places, this would print in
845 * This is not currently used.
848 asn1_decode(u_char
*p
, u_int length
)
853 while (i
>= 0 && length
> 0) {
854 i
= asn1_parse(p
, length
, &elem
);
857 if (asn1_print(&elem
) < 0)
859 if (elem
.type
== BE_SEQ
|| elem
.type
== BE_PDU
) {
861 asn1_decode(elem
.data
.raw
, elem
.asnlen
);
874 SmiBasetype basetype
;
878 static const struct smi2be smi2betab
[] = {
879 { SMI_BASETYPE_INTEGER32
, BE_INT
},
880 { SMI_BASETYPE_OCTETSTRING
, BE_STR
},
881 { SMI_BASETYPE_OCTETSTRING
, BE_INETADDR
},
882 { SMI_BASETYPE_OBJECTIDENTIFIER
, BE_OID
},
883 { SMI_BASETYPE_UNSIGNED32
, BE_UNS
},
884 { SMI_BASETYPE_INTEGER64
, BE_NONE
},
885 { SMI_BASETYPE_UNSIGNED64
, BE_UNS64
},
886 { SMI_BASETYPE_FLOAT32
, BE_NONE
},
887 { SMI_BASETYPE_FLOAT64
, BE_NONE
},
888 { SMI_BASETYPE_FLOAT128
, BE_NONE
},
889 { SMI_BASETYPE_ENUM
, BE_INT
},
890 { SMI_BASETYPE_BITS
, BE_STR
},
891 { SMI_BASETYPE_UNKNOWN
, BE_NONE
}
895 smi_decode_oid(struct be
*elem
, unsigned int *oid
,
896 unsigned int oidsize
, unsigned int *oidlen
)
898 u_char
*p
= (u_char
*)elem
->data
.raw
;
899 u_int32_t asnlen
= elem
->asnlen
;
900 int o
= 0, first
= -1, i
= asnlen
;
902 for (*oidlen
= 0; sflag
&& i
-- > 0; p
++) {
904 o
= (o
<< ASN_SHIFT7
) + (*p
& ~ASN_BIT8
);
905 if (*p
& ASN_LONGLEN
)
909 * first subitem encodes two items with 1st*OIDMUX+2nd
910 * (see X.690:1997 clause 8.19 for the details)
914 if (*oidlen
< oidsize
) {
915 oid
[*oidlen
] = o
/ OIDMUX
;
916 if (oid
[*oidlen
] > 2) oid
[*oidlen
] = 2;
918 o
-= oid
[*oidlen
] * OIDMUX
;
919 if (*oidlen
< oidsize
) (*oidlen
)++;
921 if (*oidlen
< oidsize
) {
922 oid
[(*oidlen
)++] = o
;
933 static int smi_check_type(SmiBasetype basetype
, int be
)
937 for (i
= 0; smi2betab
[i
].basetype
!= SMI_BASETYPE_UNKNOWN
; i
++) {
938 if (smi2betab
[i
].basetype
== basetype
&& smi2betab
[i
].be
== be
) {
946 static int smi_check_a_range(SmiType
*smiType
, SmiRange
*smiRange
,
951 switch (smiType
->basetype
) {
952 case SMI_BASETYPE_OBJECTIDENTIFIER
:
953 case SMI_BASETYPE_OCTETSTRING
:
954 if (smiRange
->minValue
.value
.unsigned32
955 == smiRange
->maxValue
.value
.unsigned32
) {
956 ok
= (elem
->asnlen
== smiRange
->minValue
.value
.unsigned32
);
958 ok
= (elem
->asnlen
>= smiRange
->minValue
.value
.unsigned32
959 && elem
->asnlen
<= smiRange
->maxValue
.value
.unsigned32
);
963 case SMI_BASETYPE_INTEGER32
:
964 ok
= (elem
->data
.integer
>= smiRange
->minValue
.value
.integer32
965 && elem
->data
.integer
<= smiRange
->maxValue
.value
.integer32
);
968 case SMI_BASETYPE_UNSIGNED32
:
969 ok
= (elem
->data
.uns
>= smiRange
->minValue
.value
.unsigned32
970 && elem
->data
.uns
<= smiRange
->maxValue
.value
.unsigned32
);
973 case SMI_BASETYPE_UNSIGNED64
:
977 /* case SMI_BASETYPE_INTEGER64: SMIng */
978 /* case SMI_BASETYPE_FLOAT32: SMIng */
979 /* case SMI_BASETYPE_FLOAT64: SMIng */
980 /* case SMI_BASETYPE_FLOAT128: SMIng */
982 case SMI_BASETYPE_ENUM
:
983 case SMI_BASETYPE_BITS
:
984 case SMI_BASETYPE_UNKNOWN
:
996 static int smi_check_range(SmiType
*smiType
, struct be
*elem
)
1001 for (smiRange
= smiGetFirstRange(smiType
);
1003 smiRange
= smiGetNextRange(smiRange
)) {
1005 ok
= smi_check_a_range(smiType
, smiRange
, elem
);
1013 SmiType
*parentType
;
1014 parentType
= smiGetParentType(smiType
);
1016 ok
= smi_check_range(parentType
, elem
);
1023 static SmiNode
*smi_print_variable(struct be
*elem
, int *status
)
1025 unsigned int oid
[128], oidlen
;
1026 SmiNode
*smiNode
= NULL
;
1029 *status
= smi_decode_oid(elem
, oid
, sizeof(oid
)/sizeof(unsigned int),
1033 smiNode
= smiGetNodeByOID(oidlen
, oid
);
1035 *status
= asn1_print(elem
);
1039 fputs(smiGetNodeModule(smiNode
)->name
, stdout
);
1040 fputs("::", stdout
);
1042 fputs(smiNode
->name
, stdout
);
1043 if (smiNode
->oidlen
< oidlen
) {
1044 for (i
= smiNode
->oidlen
; i
< oidlen
; i
++) {
1045 printf(".%u", oid
[i
]);
1053 smi_print_value(SmiNode
*smiNode
, u_char pduid
, struct be
*elem
)
1055 unsigned int i
, oid
[128], oidlen
;
1060 if (! smiNode
|| ! (smiNode
->nodekind
1061 & (SMI_NODEKIND_SCALAR
| SMI_NODEKIND_COLUMN
))) {
1062 return asn1_print(elem
);
1065 if (elem
->type
== BE_NOSUCHOBJECT
1066 || elem
->type
== BE_NOSUCHINST
1067 || elem
->type
== BE_ENDOFMIBVIEW
) {
1068 return asn1_print(elem
);
1071 if (NOTIFY_CLASS(pduid
) && smiNode
->access
< SMI_ACCESS_NOTIFY
) {
1072 fputs("[notNotifyable]", stdout
);
1075 if (READ_CLASS(pduid
) && smiNode
->access
< SMI_ACCESS_READ_ONLY
) {
1076 fputs("[notReadable]", stdout
);
1079 if (WRITE_CLASS(pduid
) && smiNode
->access
< SMI_ACCESS_READ_WRITE
) {
1080 fputs("[notWritable]", stdout
);
1083 if (RESPONSE_CLASS(pduid
)
1084 && smiNode
->access
== SMI_ACCESS_NOT_ACCESSIBLE
) {
1085 fputs("[noAccess]", stdout
);
1088 smiType
= smiGetNodeType(smiNode
);
1090 return asn1_print(elem
);
1093 if (! smi_check_type(smiType
->basetype
, elem
->type
)) {
1094 fputs("[wrongType]", stdout
);
1097 if (! smi_check_range(smiType
, elem
)) {
1098 fputs("[outOfRange]", stdout
);
1101 /* resolve bits to named bits */
1103 /* check whether instance identifier is valid */
1105 /* apply display hints (integer, octetstring) */
1107 /* convert instance identifier to index type values */
1109 switch (elem
->type
) {
1111 if (smiType
->basetype
== SMI_BASETYPE_BITS
) {
1112 /* print bit labels */
1114 smi_decode_oid(elem
, oid
,
1115 sizeof(oid
)/sizeof(unsigned int),
1117 smiNode
= smiGetNodeByOID(oidlen
, oid
);
1120 fputs(smiGetNodeModule(smiNode
)->name
, stdout
);
1121 fputs("::", stdout
);
1123 fputs(smiNode
->name
, stdout
);
1124 if (smiNode
->oidlen
< oidlen
) {
1125 for (i
= smiNode
->oidlen
;
1127 printf(".%u", oid
[i
]);
1136 if (smiType
->basetype
== SMI_BASETYPE_ENUM
) {
1137 for (nn
= smiGetFirstNamedNumber(smiType
);
1139 nn
= smiGetNextNamedNumber(nn
)) {
1140 if (nn
->value
.value
.integer32
1141 == elem
->data
.integer
) {
1142 fputs(nn
->name
, stdout
);
1143 printf("(%d)", elem
->data
.integer
);
1153 return asn1_print(elem
);
1160 * General SNMP header
1162 * version INTEGER {version-1(0)},
1163 * community OCTET STRING,
1166 * PDUs for all but Trap: (see rfc1157 from page 15 on)
1168 * request-id INTEGER,
1169 * error-status INTEGER,
1170 * error-index INTEGER,
1171 * varbindlist SEQUENCE OF
1179 * enterprise OBJECT IDENTIFIER,
1180 * agent-addr NetworkAddress,
1181 * generic-trap INTEGER,
1182 * specific-trap INTEGER,
1183 * time-stamp TimeTicks,
1184 * varbindlist SEQUENCE OF
1193 * Decode SNMP varBind
1196 varbind_print(u_char pduid
, const u_char
*np
, u_int length
)
1201 SmiNode
*smiNode
= NULL
;
1205 /* Sequence of varBind */
1206 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1208 if (elem
.type
!= BE_SEQ
) {
1209 fputs("[!SEQ of varbind]", stdout
);
1213 if ((u_int
)count
< length
)
1214 printf("[%d extra after SEQ of varbind]", length
- count
);
1216 length
= elem
.asnlen
;
1217 np
= (u_char
*)elem
.data
.raw
;
1219 for (ind
= 1; length
> 0; ind
++) {
1220 const u_char
*vbend
;
1226 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1228 if (elem
.type
!= BE_SEQ
) {
1229 fputs("[!varbind]", stdout
);
1234 vblength
= length
- count
;
1236 length
= elem
.asnlen
;
1237 np
= (u_char
*)elem
.data
.raw
;
1240 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1242 if (elem
.type
!= BE_OID
) {
1243 fputs("[objName!=OID]", stdout
);
1248 smiNode
= smi_print_variable(&elem
, &status
);
1250 status
= asn1_print(&elem
);
1257 if (pduid
!= GETREQ
&& pduid
!= GETNEXTREQ
1258 && pduid
!= GETBULKREQ
)
1262 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1264 if (pduid
== GETREQ
|| pduid
== GETNEXTREQ
1265 || pduid
== GETBULKREQ
) {
1266 if (elem
.type
!= BE_NULL
) {
1267 fputs("[objVal!=NULL]", stdout
);
1268 if (asn1_print(&elem
) < 0)
1272 if (elem
.type
!= BE_NULL
) {
1274 status
= smi_print_value(smiNode
, pduid
, &elem
);
1276 status
= asn1_print(&elem
);
1288 * Decode SNMP PDUs: GetRequest, GetNextRequest, GetResponse, SetRequest,
1289 * GetBulk, Inform, V2Trap, and Report
1292 snmppdu_print(u_short pduid
, const u_char
*np
, u_int length
)
1295 int count
= 0, error
;
1297 /* reqId (Integer) */
1298 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1300 if (elem
.type
!= BE_INT
) {
1301 fputs("[reqId!=INT]", stdout
);
1306 printf("R=%d ", elem
.data
.integer
);
1310 /* errorStatus (Integer) */
1311 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1313 if (elem
.type
!= BE_INT
) {
1314 fputs("[errorStatus!=INT]", stdout
);
1319 if ((pduid
== GETREQ
|| pduid
== GETNEXTREQ
|| pduid
== SETREQ
1320 || pduid
== INFORMREQ
|| pduid
== V2TRAP
|| pduid
== REPORT
)
1321 && elem
.data
.integer
!= 0) {
1323 printf("[errorStatus(%s)!=0]",
1324 DECODE_ErrorStatus(elem
.data
.integer
));
1325 } else if (pduid
== GETBULKREQ
) {
1326 printf(" N=%d", elem
.data
.integer
);
1327 } else if (elem
.data
.integer
!= 0) {
1329 printf(" %s", DECODE_ErrorStatus(elem
.data
.integer
));
1330 error
= elem
.data
.integer
;
1335 /* errorIndex (Integer) */
1336 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1338 if (elem
.type
!= BE_INT
) {
1339 fputs("[errorIndex!=INT]", stdout
);
1343 if ((pduid
== GETREQ
|| pduid
== GETNEXTREQ
|| pduid
== SETREQ
1344 || pduid
== INFORMREQ
|| pduid
== V2TRAP
|| pduid
== REPORT
)
1345 && elem
.data
.integer
!= 0)
1346 printf("[errorIndex(%d)!=0]", elem
.data
.integer
);
1347 else if (pduid
== GETBULKREQ
)
1348 printf(" M=%d", elem
.data
.integer
);
1349 else if (elem
.data
.integer
!= 0) {
1351 printf("[errorIndex(%d) w/o errorStatus]",
1354 printf("@%d", elem
.data
.integer
);
1355 error
= elem
.data
.integer
;
1358 fputs("[errorIndex==0]", stdout
);
1364 varbind_print(pduid
, np
, length
);
1369 * Decode SNMP Trap PDU
1372 trappdu_print(const u_char
*np
, u_int length
)
1375 int count
= 0, generic
;
1379 /* enterprise (oid) */
1380 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1382 if (elem
.type
!= BE_OID
) {
1383 fputs("[enterprise!=OID]", stdout
);
1387 if (asn1_print(&elem
) < 0)
1394 /* agent-addr (inetaddr) */
1395 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1397 if (elem
.type
!= BE_INETADDR
) {
1398 fputs("[agent-addr!=INETADDR]", stdout
);
1402 if (asn1_print(&elem
) < 0)
1407 /* generic-trap (Integer) */
1408 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1410 if (elem
.type
!= BE_INT
) {
1411 fputs("[generic-trap!=INT]", stdout
);
1415 generic
= elem
.data
.integer
;
1418 printf(" %s", DECODE_GenericTrap(generic
));
1423 /* specific-trap (Integer) */
1424 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1426 if (elem
.type
!= BE_INT
) {
1427 fputs("[specific-trap!=INT]", stdout
);
1431 if (generic
!= GT_ENTERPRISE
) {
1432 if (elem
.data
.integer
!= 0)
1433 printf("[specific-trap(%d)!=0]", elem
.data
.integer
);
1435 printf(" s=%d", elem
.data
.integer
);
1441 /* time-stamp (TimeTicks) */
1442 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1444 if (elem
.type
!= BE_UNS
) { /* XXX */
1445 fputs("[time-stamp!=TIMETICKS]", stdout
);
1449 if (asn1_print(&elem
) < 0)
1454 varbind_print (TRAP
, np
, length
);
1459 * Decode arbitrary SNMP PDUs.
1462 pdu_print(const u_char
*np
, u_int length
, int version
)
1468 if ((count
= asn1_parse(np
, length
, &pdu
)) < 0)
1470 if (pdu
.type
!= BE_PDU
) {
1471 fputs("[no PDU]", stdout
);
1474 if ((u_int
)count
< length
)
1475 printf("[%d extra after PDU]", length
- count
);
1477 fputs("{ ", stdout
);
1479 if (asn1_print(&pdu
) < 0)
1482 /* descend into PDU */
1483 length
= pdu
.asnlen
;
1484 np
= (u_char
*)pdu
.data
.raw
;
1486 if (version
== SNMP_VERSION_1
&&
1487 (pdu
.id
== GETBULKREQ
|| pdu
.id
== INFORMREQ
||
1488 pdu
.id
== V2TRAP
|| pdu
.id
== REPORT
)) {
1489 printf("[v2 PDU in v1 message]");
1493 if (version
== SNMP_VERSION_2
&& pdu
.id
== TRAP
) {
1494 printf("[v1 PDU in v2 message]");
1500 trappdu_print(np
, length
);
1510 snmppdu_print(pdu
.id
, np
, length
);
1515 fputs(" } ", stdout
);
1520 * Decode a scoped SNMP PDU.
1523 scopedpdu_print(const u_char
*np
, u_int length
, int version
)
1529 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1531 if (elem
.type
!= BE_SEQ
) {
1532 fputs("[!scoped PDU]", stdout
);
1536 length
= elem
.asnlen
;
1537 np
= (u_char
*)elem
.data
.raw
;
1539 /* contextEngineID (OCTET STRING) */
1540 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1542 if (elem
.type
!= BE_STR
) {
1543 fputs("[contextEngineID!=STR]", stdout
);
1550 fputs("E= ", stdout
);
1551 for (i
= 0; i
< (int)elem
.asnlen
; i
++) {
1552 printf("0x%02X", elem
.data
.str
[i
]);
1556 /* contextName (OCTET STRING) */
1557 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1559 if (elem
.type
!= BE_STR
) {
1560 fputs("[contextName!=STR]", stdout
);
1567 printf("C=%.*s ", (int)elem
.asnlen
, elem
.data
.str
);
1569 pdu_print(np
, length
, version
);
1573 * Decode SNMP Community Header (SNMPv1 and SNMPv2c)
1576 community_print(const u_char
*np
, u_int length
, int version
)
1581 /* Community (String) */
1582 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1584 if (elem
.type
!= BE_STR
) {
1585 fputs("[comm!=STR]", stdout
);
1589 /* default community */
1590 if (!(elem
.asnlen
== sizeof(DEF_COMMUNITY
) - 1 &&
1591 strncmp((char *)elem
.data
.str
, DEF_COMMUNITY
,
1592 sizeof(DEF_COMMUNITY
) - 1) == 0))
1594 printf("C=%.*s ", (int)elem
.asnlen
, elem
.data
.str
);
1598 pdu_print(np
, length
, version
);
1602 * Decode SNMPv3 User-based Security Message Header (SNMPv3)
1605 usm_print(const u_char
*np
, u_int length
)
1611 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1613 if (elem
.type
!= BE_SEQ
) {
1614 fputs("[!usm]", stdout
);
1618 length
= elem
.asnlen
;
1619 np
= (u_char
*)elem
.data
.raw
;
1621 /* msgAuthoritativeEngineID (OCTET STRING) */
1622 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1624 if (elem
.type
!= BE_STR
) {
1625 fputs("[msgAuthoritativeEngineID!=STR]", stdout
);
1632 /* msgAuthoritativeEngineBoots (INTEGER) */
1633 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1635 if (elem
.type
!= BE_INT
) {
1636 fputs("[msgAuthoritativeEngineBoots!=INT]", stdout
);
1641 printf("B=%d ", elem
.data
.integer
);
1645 /* msgAuthoritativeEngineTime (INTEGER) */
1646 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1648 if (elem
.type
!= BE_INT
) {
1649 fputs("[msgAuthoritativeEngineTime!=INT]", stdout
);
1654 printf("T=%d ", elem
.data
.integer
);
1658 /* msgUserName (OCTET STRING) */
1659 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1661 if (elem
.type
!= BE_STR
) {
1662 fputs("[msgUserName!=STR]", stdout
);
1669 printf("U=%.*s ", (int)elem
.asnlen
, elem
.data
.str
);
1671 /* msgAuthenticationParameters (OCTET STRING) */
1672 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1674 if (elem
.type
!= BE_STR
) {
1675 fputs("[msgAuthenticationParameters!=STR]", stdout
);
1682 /* msgPrivacyParameters (OCTET STRING) */
1683 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1685 if (elem
.type
!= BE_STR
) {
1686 fputs("[msgPrivacyParameters!=STR]", stdout
);
1693 if ((u_int
)count
< length
)
1694 printf("[%d extra after usm SEQ]", length
- count
);
1698 * Decode SNMPv3 Message Header (SNMPv3)
1701 v3msg_print(const u_char
*np
, u_int length
)
1707 const u_char
*xnp
= np
;
1708 int xlength
= length
;
1711 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1713 if (elem
.type
!= BE_SEQ
) {
1714 fputs("[!message]", stdout
);
1718 length
= elem
.asnlen
;
1719 np
= (u_char
*)elem
.data
.raw
;
1722 fputs("{ ", stdout
);
1725 /* msgID (INTEGER) */
1726 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1728 if (elem
.type
!= BE_INT
) {
1729 fputs("[msgID!=INT]", stdout
);
1736 /* msgMaxSize (INTEGER) */
1737 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1739 if (elem
.type
!= BE_INT
) {
1740 fputs("[msgMaxSize!=INT]", stdout
);
1747 /* msgFlags (OCTET STRING) */
1748 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1750 if (elem
.type
!= BE_STR
) {
1751 fputs("[msgFlags!=STR]", stdout
);
1755 if (elem
.asnlen
!= 1) {
1756 printf("[msgFlags size %d]", elem
.asnlen
);
1759 flags
= elem
.data
.str
[0];
1760 if (flags
!= 0x00 && flags
!= 0x01 && flags
!= 0x03
1761 && flags
!= 0x04 && flags
!= 0x05 && flags
!= 0x07) {
1762 printf("[msgFlags=0x%02X]", flags
);
1768 fputs("F=", stdout
);
1769 if (flags
& 0x01) fputs("a", stdout
);
1770 if (flags
& 0x02) fputs("p", stdout
);
1771 if (flags
& 0x04) fputs("r", stdout
);
1774 /* msgSecurityModel (INTEGER) */
1775 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1777 if (elem
.type
!= BE_INT
) {
1778 fputs("[msgSecurityModel!=INT]", stdout
);
1782 model
= elem
.data
.integer
;
1786 if ((u_int
)count
< length
)
1787 printf("[%d extra after message SEQ]", length
- count
);
1790 fputs("} ", stdout
);
1795 fputs("{ USM ", stdout
);
1798 printf("[security model %d]", model
);
1802 np
= xnp
+ (np
- xnp
);
1803 length
= xlength
- (np
- xnp
);
1805 /* msgSecurityParameters (OCTET STRING) */
1806 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1808 if (elem
.type
!= BE_STR
) {
1809 fputs("[msgSecurityParameters!=STR]", stdout
);
1817 usm_print(elem
.data
.str
, elem
.asnlen
);
1819 fputs("} ", stdout
);
1824 fputs("{ ScopedPDU ", stdout
);
1827 scopedpdu_print(np
, length
, 3);
1830 fputs("} ", stdout
);
1835 * Decode SNMP header and pass on to PDU printing routines
1838 snmp_print(const u_char
*np
, u_int length
)
1846 /* initial Sequence */
1847 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1849 if (elem
.type
!= BE_SEQ
) {
1850 fputs("[!init SEQ]", stdout
);
1854 if ((u_int
)count
< length
)
1855 printf("[%d extra after iSEQ]", length
- count
);
1857 length
= elem
.asnlen
;
1858 np
= (u_char
*)elem
.data
.raw
;
1860 /* Version (INTEGER) */
1861 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1863 if (elem
.type
!= BE_INT
) {
1864 fputs("[version!=INT]", stdout
);
1869 switch (elem
.data
.integer
) {
1870 case SNMP_VERSION_1
:
1871 case SNMP_VERSION_2
:
1872 case SNMP_VERSION_3
:
1874 printf("{ %s ", SnmpVersion
[elem
.data
.integer
]);
1877 printf("[version = %d]", elem
.data
.integer
);
1880 version
= elem
.data
.integer
;
1885 case SNMP_VERSION_1
:
1886 case SNMP_VERSION_2
:
1887 community_print(np
, length
, version
);
1889 case SNMP_VERSION_3
:
1890 v3msg_print(np
, length
);
1893 printf("[version = %d]", elem
.data
.integer
);
1898 fputs("} ", stdout
);