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
60 static const char rcsid
[] _U_
=
61 "@(#) $Header: /tcpdump/master/tcpdump/print-snmp.c,v 1.63 2005-04-18 00:07:31 guy Exp $ (LBL)";
68 #include <tcpdump-stdinc.h>
77 #include "interface.h"
78 #include "addrtoname.h"
80 #undef OPAQUE /* defined in <wingdi.h> */
83 * Universal ASN.1 types
84 * (we only care about the tag values for those allowed in the Internet SMI)
86 const char *Universal
[] = {
99 "U-8","U-9","U-10","U-11", /* 8-11 */
100 "U-12","U-13","U-14","U-15", /* 12-15 */
107 * Application-wide ASN.1 types from the Internet SMI and their tags
109 const char *Application
[] = {
126 * Context-specific ASN.1 types for the SNMP PDUs and their tags
128 const char *Context
[] = {
149 #define NOTIFY_CLASS(x) (x == TRAP || x == V2TRAP || x == INFORMREQ)
150 #define READ_CLASS(x) (x == GETREQ || x == GETNEXTREQ || x == GETBULKREQ)
151 #define WRITE_CLASS(x) (x == SETREQ)
152 #define RESPONSE_CLASS(x) (x == GETRESP)
153 #define INTERNAL_CLASS(x) (x == REPORT)
156 * Context-specific ASN.1 types for the SNMP Exceptions and their tags
158 const char *Exceptions
[] = {
160 #define NOSUCHOBJECT 0
162 #define NOSUCHINSTANCE 1
164 #define ENDOFMIBVIEW 2
168 * Private ASN.1 types
169 * The Internet SMI does not specify any
171 const char *Private
[] = {
176 * error-status values for any SNMP PDU
178 const char *ErrorStatus
[] = {
192 "resourceUnavailable",
195 "authorizationError",
199 #define DECODE_ErrorStatus(e) \
200 ( e >= 0 && (size_t)e < sizeof(ErrorStatus)/sizeof(ErrorStatus[0]) \
202 : (snprintf(errbuf, sizeof(errbuf), "err=%u", e), errbuf))
205 * generic-trap values in the SNMP Trap-PDU
207 const char *GenericTrap
[] = {
212 "authenticationFailure",
215 #define GT_ENTERPRISE 6
217 #define DECODE_GenericTrap(t) \
218 ( t >= 0 && (size_t)t < sizeof(GenericTrap)/sizeof(GenericTrap[0]) \
220 : (snprintf(buf, sizeof(buf), "gt=%d", t), buf))
223 * ASN.1 type class table
224 * Ties together the preceding Universal, Application, Context, and Private
227 #define defineCLASS(x) { "x", x, sizeof(x)/sizeof(x[0]) } /* not ANSI-C */
233 defineCLASS(Universal
),
235 defineCLASS(Application
),
236 #define APPLICATION 1
237 defineCLASS(Context
),
239 defineCLASS(Private
),
241 defineCLASS(Exceptions
),
246 * defined forms for ASN.1 types
248 const char *Form
[] = {
252 #define CONSTRUCTED 1
256 * A structure for the OID tree for the compiled-in MIB.
257 * This is stored as a general-order tree.
260 const char *desc
; /* name of object */
261 u_char oid
; /* sub-id following parent */
262 u_char type
; /* object type (unused) */
263 struct obj
*child
, *next
; /* child and next sibling pointers */
267 * Include the compiled in SNMP MIB. "mib.h" is produced by feeding
268 * RFC-1156 format files into "makemib". "mib.h" MUST define at least
269 * a value for `mibroot'.
271 * In particular, this is gross, as this is including initialized structures,
272 * and by right shouldn't be an "include" file.
277 * This defines a list of OIDs which will be abbreviated on output.
278 * Currently, this includes the prefixes for the Internet MIB, the
279 * private enterprises tree, and the experimental tree.
282 const char *prefix
; /* prefix for this abrev */
283 struct obj
*node
; /* pointer into object table */
284 const char *oid
; /* ASN.1 encoded OID */
285 } obj_abrev_list
[] = {
287 /* .iso.org.dod.internet.mgmt.mib */
288 { "", &_mib_obj
, "\53\6\1\2\1" },
290 #ifndef NO_ABREV_ENTER
291 /* .iso.org.dod.internet.private.enterprises */
292 { "E:", &_enterprises_obj
, "\53\6\1\4\1" },
294 #ifndef NO_ABREV_EXPERI
295 /* .iso.org.dod.internet.experimental */
296 { "X:", &_experimental_obj
, "\53\6\1\3" },
298 #ifndef NO_ABBREV_SNMPMODS
299 /* .iso.org.dod.internet.snmpV2.snmpModules */
300 { "S:", &_snmpModules_obj
, "\53\6\1\6\3" },
306 * This is used in the OID print routine to walk down the object tree
307 * rooted at `mibroot'.
309 #define OBJ_PRINT(o, suppressdot) \
313 if ((o) == objp->oid) \
315 } while ((objp = objp->next) != NULL); \
318 printf(suppressdot?"%s":".%s", objp->desc); \
319 objp = objp->child; \
321 printf(suppressdot?"%u":".%u", (o)); \
325 * This is the definition for the Any-Data-Type storage used purely for
326 * temporary internal representation while decoding an ASN.1 data stream.
341 u_char form
, class; /* tag info */
352 #define BE_INETADDR 8
355 #define BE_NOSUCHOBJECT 128
356 #define BE_NOSUCHINST 129
357 #define BE_ENDOFMIBVIEW 130
361 * SNMP versions recognized by this module
363 const char *SnmpVersion
[] = {
365 #define SNMP_VERSION_1 0
367 #define SNMP_VERSION_2 1
369 #define SNMP_VERSION_2U 2
371 #define SNMP_VERSION_3 3
375 * Defaults for SNMP PDU components
377 #define DEF_COMMUNITY "public"
380 * constants for ASN.1 decoding
383 #define ASNLEN_INETADDR 4
386 #define ASN_BIT8 0x80
387 #define ASN_LONGLEN 0x80
389 #define ASN_ID_BITS 0x1f
390 #define ASN_FORM_BITS 0x20
391 #define ASN_FORM_SHIFT 5
392 #define ASN_CLASS_BITS 0xc0
393 #define ASN_CLASS_SHIFT 6
395 #define ASN_ID_EXT 0x1f /* extension ID in tag field */
398 * This decodes the next ASN.1 object in the stream pointed to by "p"
399 * (and of real-length "len") and stores the intermediate data in the
400 * provided BE object.
402 * This returns -l if it fails (i.e., the ASN.1 stream is not valid).
403 * O/w, this returns the number of bytes parsed from "p".
406 asn1_parse(register const u_char
*p
, u_int len
, struct be
*elem
)
408 u_char form
, class, id
;
414 fputs("[nothing to parse]", stdout
);
420 * it would be nice to use a bit field, but you can't depend on them.
421 * +---+---+---+---+---+---+---+---+
423 * +---+---+---+---+---+---+---+---+
426 id
= *p
& ASN_ID_BITS
; /* lower 5 bits, range 00-1f */
428 form
= (*p
& 0xe0) >> 5; /* move upper 3 bits to lower 3 */
429 class = form
>> 1; /* bits 7&6 -> bits 1&0, range 0-3 */
430 form
&= 0x1; /* bit 5 -> bit 0, range 0-1 */
432 form
= (u_char
)(*p
& ASN_FORM_BITS
) >> ASN_FORM_SHIFT
;
433 class = (u_char
)(*p
& ASN_CLASS_BITS
) >> ASN_CLASS_SHIFT
;
439 /* extended tag field */
440 if (id
== ASN_ID_EXT
) {
442 * The ID follows, as a sequence of octets with the
443 * 8th bit set and the remaining 7 bits being
444 * the next 7 bits of the value, terminated with
445 * an octet with the 8th bit not set.
447 * First, assemble all the octets with the 8th
448 * bit set. XXX - this doesn't handle a value
449 * that won't fit in 32 bits.
451 for (id
= 0; *p
& ASN_BIT8
; len
--, hdr
++, p
++) {
453 fputs("[Xtagfield?]", stdout
);
457 id
= (id
<< 7) | (*p
& ~ASN_BIT8
);
460 fputs("[Xtagfield?]", stdout
);
464 elem
->id
= id
= (id
<< 7) | *p
;
470 fputs("[no asnlen]", stdout
);
476 if (elem
->asnlen
& ASN_BIT8
) {
477 u_int32_t noct
= elem
->asnlen
% ASN_BIT8
;
480 printf("[asnlen? %d<%d]", len
, noct
);
484 for (; noct
-- > 0; len
--, hdr
++)
485 elem
->asnlen
= (elem
->asnlen
<< ASN_SHIFT8
) | *p
++;
487 if (len
< elem
->asnlen
) {
488 printf("[len%d<asnlen%u]", len
, elem
->asnlen
);
491 if (form
>= sizeof(Form
)/sizeof(Form
[0])) {
492 printf("[form?%d]", form
);
495 if (class >= sizeof(Class
)/sizeof(Class
[0])) {
496 printf("[class?%c/%d]", *Form
[form
], class);
499 if ((int)id
>= Class
[class].numIDs
) {
500 printf("[id?%c/%s/%d]", *Form
[form
], Class
[class].name
, id
);
515 register int32_t data
;
519 TCHECK2(*p
, elem
->asnlen
);
520 if (*p
& ASN_BIT8
) /* negative */
522 for (i
= elem
->asnlen
; i
-- > 0; p
++)
523 data
= (data
<< ASN_SHIFT8
) | *p
;
524 elem
->data
.integer
= data
;
530 elem
->data
.raw
= (caddr_t
)p
;
534 elem
->type
= BE_NULL
;
535 elem
->data
.raw
= NULL
;
539 elem
->type
= BE_OCTET
;
540 elem
->data
.raw
= (caddr_t
)p
;
542 Class
[class].Id
[id
]);
550 elem
->type
= BE_INETADDR
;
551 elem
->data
.raw
= (caddr_t
)p
;
557 register u_int32_t data
;
558 TCHECK2(*p
, elem
->asnlen
);
561 for (i
= elem
->asnlen
; i
-- > 0; p
++)
562 data
= (data
<< 8) + *p
;
563 elem
->data
.uns
= data
;
568 register u_int32_t high
, low
;
569 TCHECK2(*p
, elem
->asnlen
);
570 elem
->type
= BE_UNS64
;
572 for (i
= elem
->asnlen
; i
-- > 0; p
++) {
574 ((low
& 0xFF000000) >> 24);
575 low
= (low
<< 8) | *p
;
577 elem
->data
.uns64
.high
= high
;
578 elem
->data
.uns64
.low
= low
;
583 elem
->type
= BE_OCTET
;
584 elem
->data
.raw
= (caddr_t
)p
;
586 Class
[class].Id
[id
]);
594 elem
->type
= BE_NOSUCHOBJECT
;
595 elem
->data
.raw
= NULL
;
599 elem
->type
= BE_NOSUCHINST
;
600 elem
->data
.raw
= NULL
;
604 elem
->type
= BE_ENDOFMIBVIEW
;
605 elem
->data
.raw
= NULL
;
612 Class
[class].name
, Class
[class].Id
[id
]);
613 TCHECK2(*p
, elem
->asnlen
);
614 elem
->type
= BE_OCTET
;
615 elem
->data
.raw
= (caddr_t
)p
;
626 elem
->data
.raw
= (caddr_t
)p
;
630 elem
->type
= BE_OCTET
;
631 elem
->data
.raw
= (caddr_t
)p
;
632 printf("C/U/%s", Class
[class].Id
[id
]);
639 elem
->data
.raw
= (caddr_t
)p
;
643 elem
->type
= BE_OCTET
;
644 elem
->data
.raw
= (caddr_t
)p
;
646 Class
[class].name
, Class
[class].Id
[id
]);
653 return elem
->asnlen
+ hdr
;
656 fputs("[|snmp]", stdout
);
661 * Display the ASN.1 object represented by the BE object.
662 * This used to be an integral part of asn1_parse() before the intermediate
666 asn1_print(struct be
*elem
)
668 u_char
*p
= (u_char
*)elem
->data
.raw
;
669 u_int32_t asnlen
= elem
->asnlen
;
672 switch (elem
->type
) {
676 for (i
= asnlen
; i
-- > 0; p
++)
684 int o
= 0, first
= -1, i
= asnlen
;
686 if (!sflag
&& !nflag
&& asnlen
> 2) {
687 struct obj_abrev
*a
= &obj_abrev_list
[0];
688 size_t a_len
= strlen(a
->oid
);
689 for (; a
->node
; a
++) {
691 if (memcmp(a
->oid
, (char *)p
, a_len
) == 0) {
692 objp
= a
->node
->child
;
695 fputs(a
->prefix
, stdout
);
702 for (; !sflag
&& i
-- > 0; p
++) {
704 o
= (o
<< ASN_SHIFT7
) + (*p
& ~ASN_BIT8
);
705 if (*p
& ASN_LONGLEN
)
709 * first subitem encodes two items with 1st*OIDMUX+2nd
710 * (see X.690:1997 clause 8.19 for the details)
731 printf("%d", elem
->data
.integer
);
735 printf("%u", elem
->data
.uns
);
738 case BE_UNS64
: { /* idea borrowed from by Marshall Rose */
741 char *cpf
, *cpl
, last
[6], first
[30];
742 if (elem
->data
.uns64
.high
== 0) {
743 printf("%u", elem
->data
.uns64
.low
);
746 d
= elem
->data
.uns64
.high
* 4294967296.0; /* 2^32 */
747 if (elem
->data
.uns64
.high
<= 0x1fffff) {
748 d
+= elem
->data
.uns64
.low
;
749 #if 0 /*is looks illegal, but what is the intention?*/
756 d
+= (elem
->data
.uns64
.low
& 0xfffff000);
757 #if 0 /*is looks illegal, but what is the intention?*/
758 snprintf(first
, sizeof(first
), "%.f", d
);
760 snprintf(first
, sizeof(first
), "%f", d
);
762 snprintf(last
, sizeof(last
), "%5.5d",
763 elem
->data
.uns64
.low
& 0xfff);
764 for (carry
= 0, cpf
= first
+strlen(first
)-1, cpl
= last
+4;
767 j
= carry
+ (*cpf
- '0') + (*cpl
- '0');
776 fputs(first
, stdout
);
781 register int printable
= 1, first
= 1;
782 const u_char
*p
= elem
->data
.str
;
784 for (i
= asnlen
; printable
&& i
-- > 0; p
++)
785 printable
= isprint(*p
) || isspace(*p
);
789 (void)fn_print(p
, p
+ asnlen
);
792 for (i
= asnlen
; i
-- > 0; p
++) {
793 printf(first
? "%.2x" : "_%.2x", *p
);
800 printf("Seq(%u)", elem
->asnlen
);
804 if (asnlen
!= ASNLEN_INETADDR
)
805 printf("[inetaddr len!=%d]", ASNLEN_INETADDR
);
807 for (i
= asnlen
; i
-- != 0; p
++) {
808 printf((i
== asnlen
-1) ? "%u" : ".%u", *p
);
812 case BE_NOSUCHOBJECT
:
814 case BE_ENDOFMIBVIEW
:
815 printf("[%s]", Class
[EXCEPTIONS
].Id
[elem
->id
]);
820 Class
[CONTEXT
].Id
[elem
->id
], elem
->asnlen
);
824 fputs("[BE_ANY!?]", stdout
);
828 fputs("[be!?]", stdout
);
834 fputs("[|snmp]", stdout
);
840 * This is a brute force ASN.1 printer: recurses to dump an entire structure.
841 * This will work for any ASN.1 stream, not just an SNMP PDU.
843 * By adding newlines and spaces at the correct places, this would print in
846 * This is not currently used.
849 asn1_decode(u_char
*p
, u_int length
)
854 while (i
>= 0 && length
> 0) {
855 i
= asn1_parse(p
, length
, &elem
);
858 if (asn1_print(&elem
) < 0)
860 if (elem
.type
== BE_SEQ
|| elem
.type
== BE_PDU
) {
862 asn1_decode(elem
.data
.raw
, elem
.asnlen
);
875 SmiBasetype basetype
;
879 static struct smi2be smi2betab
[] = {
880 { SMI_BASETYPE_INTEGER32
, BE_INT
},
881 { SMI_BASETYPE_OCTETSTRING
, BE_STR
},
882 { SMI_BASETYPE_OCTETSTRING
, BE_INETADDR
},
883 { SMI_BASETYPE_OBJECTIDENTIFIER
, BE_OID
},
884 { SMI_BASETYPE_UNSIGNED32
, BE_UNS
},
885 { SMI_BASETYPE_INTEGER64
, BE_NONE
},
886 { SMI_BASETYPE_UNSIGNED64
, BE_UNS64
},
887 { SMI_BASETYPE_FLOAT32
, BE_NONE
},
888 { SMI_BASETYPE_FLOAT64
, BE_NONE
},
889 { SMI_BASETYPE_FLOAT128
, BE_NONE
},
890 { SMI_BASETYPE_ENUM
, BE_INT
},
891 { SMI_BASETYPE_BITS
, BE_STR
},
892 { SMI_BASETYPE_UNKNOWN
, BE_NONE
}
896 smi_decode_oid(struct be
*elem
, unsigned int *oid
,
897 unsigned int oidsize
, unsigned int *oidlen
)
899 u_char
*p
= (u_char
*)elem
->data
.raw
;
900 u_int32_t asnlen
= elem
->asnlen
;
901 int o
= 0, first
= -1, i
= asnlen
;
903 for (*oidlen
= 0; sflag
&& i
-- > 0; p
++) {
905 o
= (o
<< ASN_SHIFT7
) + (*p
& ~ASN_BIT8
);
906 if (*p
& ASN_LONGLEN
)
910 * first subitem encodes two items with 1st*OIDMUX+2nd
911 * (see X.690:1997 clause 8.19 for the details)
915 if (*oidlen
< oidsize
) {
916 oid
[*oidlen
] = o
/ OIDMUX
;
917 if (oid
[*oidlen
] > 2) oid
[*oidlen
] = 2;
919 o
-= oid
[*oidlen
] * OIDMUX
;
920 if (*oidlen
< oidsize
) (*oidlen
)++;
922 if (*oidlen
< oidsize
) {
923 oid
[(*oidlen
)++] = o
;
930 fputs("[|snmp]", stdout
);
934 static int smi_check_type(SmiBasetype basetype
, int be
)
938 for (i
= 0; smi2betab
[i
].basetype
!= SMI_BASETYPE_UNKNOWN
; i
++) {
939 if (smi2betab
[i
].basetype
== basetype
&& smi2betab
[i
].be
== be
) {
947 static int smi_check_a_range(SmiType
*smiType
, SmiRange
*smiRange
,
952 switch (smiType
->basetype
) {
953 case SMI_BASETYPE_OBJECTIDENTIFIER
:
954 case SMI_BASETYPE_OCTETSTRING
:
955 if (smiRange
->minValue
.value
.unsigned32
956 == smiRange
->maxValue
.value
.unsigned32
) {
957 ok
= (elem
->asnlen
== smiRange
->minValue
.value
.unsigned32
);
959 ok
= (elem
->asnlen
>= smiRange
->minValue
.value
.unsigned32
960 && elem
->asnlen
<= smiRange
->maxValue
.value
.unsigned32
);
964 case SMI_BASETYPE_INTEGER32
:
965 ok
= (elem
->data
.integer
>= smiRange
->minValue
.value
.integer32
966 && elem
->data
.integer
<= smiRange
->maxValue
.value
.integer32
);
969 case SMI_BASETYPE_UNSIGNED32
:
970 ok
= (elem
->data
.uns
>= smiRange
->minValue
.value
.unsigned32
971 && elem
->data
.uns
<= smiRange
->maxValue
.value
.unsigned32
);
974 case SMI_BASETYPE_UNSIGNED64
:
978 /* case SMI_BASETYPE_INTEGER64: SMIng */
979 /* case SMI_BASETYPE_FLOAT32: SMIng */
980 /* case SMI_BASETYPE_FLOAT64: SMIng */
981 /* case SMI_BASETYPE_FLOAT128: SMIng */
983 case SMI_BASETYPE_ENUM
:
984 case SMI_BASETYPE_BITS
:
985 case SMI_BASETYPE_UNKNOWN
:
997 static int smi_check_range(SmiType
*smiType
, struct be
*elem
)
1002 for (smiRange
= smiGetFirstRange(smiType
);
1004 smiRange
= smiGetNextRange(smiRange
)) {
1006 ok
= smi_check_a_range(smiType
, smiRange
, elem
);
1014 SmiType
*parentType
;
1015 parentType
= smiGetParentType(smiType
);
1017 ok
= smi_check_range(parentType
, elem
);
1024 static SmiNode
*smi_print_variable(struct be
*elem
, int *status
)
1026 unsigned int oid
[128], oidlen
;
1027 SmiNode
*smiNode
= NULL
;
1030 *status
= smi_decode_oid(elem
, oid
, sizeof(oid
)/sizeof(unsigned int),
1034 smiNode
= smiGetNodeByOID(oidlen
, oid
);
1036 *status
= asn1_print(elem
);
1040 fputs(smiGetNodeModule(smiNode
)->name
, stdout
);
1041 fputs("::", stdout
);
1043 fputs(smiNode
->name
, stdout
);
1044 if (smiNode
->oidlen
< oidlen
) {
1045 for (i
= smiNode
->oidlen
; i
< oidlen
; i
++) {
1046 printf(".%u", oid
[i
]);
1054 smi_print_value(SmiNode
*smiNode
, u_char pduid
, struct be
*elem
)
1056 unsigned int i
, oid
[128], oidlen
;
1061 if (! smiNode
|| ! (smiNode
->nodekind
1062 & (SMI_NODEKIND_SCALAR
| SMI_NODEKIND_COLUMN
))) {
1063 return asn1_print(elem
);
1066 if (elem
->type
== BE_NOSUCHOBJECT
1067 || elem
->type
== BE_NOSUCHINST
1068 || elem
->type
== BE_ENDOFMIBVIEW
) {
1069 return asn1_print(elem
);
1072 if (NOTIFY_CLASS(pduid
) && smiNode
->access
< SMI_ACCESS_NOTIFY
) {
1073 fputs("[notNotifyable]", stdout
);
1076 if (READ_CLASS(pduid
) && smiNode
->access
< SMI_ACCESS_READ_ONLY
) {
1077 fputs("[notReadable]", stdout
);
1080 if (WRITE_CLASS(pduid
) && smiNode
->access
< SMI_ACCESS_READ_WRITE
) {
1081 fputs("[notWritable]", stdout
);
1084 if (RESPONSE_CLASS(pduid
)
1085 && smiNode
->access
== SMI_ACCESS_NOT_ACCESSIBLE
) {
1086 fputs("[noAccess]", stdout
);
1089 smiType
= smiGetNodeType(smiNode
);
1091 return asn1_print(elem
);
1094 if (! smi_check_type(smiType
->basetype
, elem
->type
)) {
1095 fputs("[wrongType]", stdout
);
1098 if (! smi_check_range(smiType
, elem
)) {
1099 fputs("[outOfRange]", stdout
);
1102 /* resolve bits to named bits */
1104 /* check whether instance identifier is valid */
1106 /* apply display hints (integer, octetstring) */
1108 /* convert instance identifier to index type values */
1110 switch (elem
->type
) {
1112 if (smiType
->basetype
== SMI_BASETYPE_BITS
) {
1113 /* print bit labels */
1115 smi_decode_oid(elem
, oid
,
1116 sizeof(oid
)/sizeof(unsigned int),
1118 smiNode
= smiGetNodeByOID(oidlen
, oid
);
1121 fputs(smiGetNodeModule(smiNode
)->name
, stdout
);
1122 fputs("::", stdout
);
1124 fputs(smiNode
->name
, stdout
);
1125 if (smiNode
->oidlen
< oidlen
) {
1126 for (i
= smiNode
->oidlen
;
1128 printf(".%u", oid
[i
]);
1137 if (smiType
->basetype
== SMI_BASETYPE_ENUM
) {
1138 for (nn
= smiGetFirstNamedNumber(smiType
);
1140 nn
= smiGetNextNamedNumber(nn
)) {
1141 if (nn
->value
.value
.integer32
1142 == elem
->data
.integer
) {
1143 fputs(nn
->name
, stdout
);
1144 printf("(%d)", elem
->data
.integer
);
1154 return asn1_print(elem
);
1161 * General SNMP header
1163 * version INTEGER {version-1(0)},
1164 * community OCTET STRING,
1167 * PDUs for all but Trap: (see rfc1157 from page 15 on)
1169 * request-id INTEGER,
1170 * error-status INTEGER,
1171 * error-index INTEGER,
1172 * varbindlist SEQUENCE OF
1180 * enterprise OBJECT IDENTIFIER,
1181 * agent-addr NetworkAddress,
1182 * generic-trap INTEGER,
1183 * specific-trap INTEGER,
1184 * time-stamp TimeTicks,
1185 * varbindlist SEQUENCE OF
1194 * Decode SNMP varBind
1197 varbind_print(u_char pduid
, const u_char
*np
, u_int length
)
1202 SmiNode
*smiNode
= NULL
;
1206 /* Sequence of varBind */
1207 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1209 if (elem
.type
!= BE_SEQ
) {
1210 fputs("[!SEQ of varbind]", stdout
);
1214 if ((u_int
)count
< length
)
1215 printf("[%d extra after SEQ of varbind]", length
- count
);
1217 length
= elem
.asnlen
;
1218 np
= (u_char
*)elem
.data
.raw
;
1220 for (ind
= 1; length
> 0; ind
++) {
1221 const u_char
*vbend
;
1227 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1229 if (elem
.type
!= BE_SEQ
) {
1230 fputs("[!varbind]", stdout
);
1235 vblength
= length
- count
;
1237 length
= elem
.asnlen
;
1238 np
= (u_char
*)elem
.data
.raw
;
1241 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1243 if (elem
.type
!= BE_OID
) {
1244 fputs("[objName!=OID]", stdout
);
1249 smiNode
= smi_print_variable(&elem
, &status
);
1251 status
= asn1_print(&elem
);
1258 if (pduid
!= GETREQ
&& pduid
!= GETNEXTREQ
1259 && pduid
!= GETBULKREQ
)
1263 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1265 if (pduid
== GETREQ
|| pduid
== GETNEXTREQ
1266 || pduid
== GETBULKREQ
) {
1267 if (elem
.type
!= BE_NULL
) {
1268 fputs("[objVal!=NULL]", stdout
);
1269 if (asn1_print(&elem
) < 0)
1273 if (elem
.type
!= BE_NULL
) {
1275 status
= smi_print_value(smiNode
, pduid
, &elem
);
1277 status
= asn1_print(&elem
);
1289 * Decode SNMP PDUs: GetRequest, GetNextRequest, GetResponse, SetRequest,
1290 * GetBulk, Inform, V2Trap, and Report
1293 snmppdu_print(u_short pduid
, const u_char
*np
, u_int length
)
1296 int count
= 0, error
;
1298 /* reqId (Integer) */
1299 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1301 if (elem
.type
!= BE_INT
) {
1302 fputs("[reqId!=INT]", stdout
);
1307 printf("R=%d ", elem
.data
.integer
);
1311 /* errorStatus (Integer) */
1312 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1314 if (elem
.type
!= BE_INT
) {
1315 fputs("[errorStatus!=INT]", stdout
);
1320 if ((pduid
== GETREQ
|| pduid
== GETNEXTREQ
|| pduid
== SETREQ
1321 || pduid
== INFORMREQ
|| pduid
== V2TRAP
|| pduid
== REPORT
)
1322 && elem
.data
.integer
!= 0) {
1324 printf("[errorStatus(%s)!=0]",
1325 DECODE_ErrorStatus(elem
.data
.integer
));
1326 } else if (pduid
== GETBULKREQ
) {
1327 printf(" N=%d", elem
.data
.integer
);
1328 } else if (elem
.data
.integer
!= 0) {
1330 printf(" %s", DECODE_ErrorStatus(elem
.data
.integer
));
1331 error
= elem
.data
.integer
;
1336 /* errorIndex (Integer) */
1337 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1339 if (elem
.type
!= BE_INT
) {
1340 fputs("[errorIndex!=INT]", stdout
);
1344 if ((pduid
== GETREQ
|| pduid
== GETNEXTREQ
|| pduid
== SETREQ
1345 || pduid
== INFORMREQ
|| pduid
== V2TRAP
|| pduid
== REPORT
)
1346 && elem
.data
.integer
!= 0)
1347 printf("[errorIndex(%d)!=0]", elem
.data
.integer
);
1348 else if (pduid
== GETBULKREQ
)
1349 printf(" M=%d", elem
.data
.integer
);
1350 else if (elem
.data
.integer
!= 0) {
1352 printf("[errorIndex(%d) w/o errorStatus]",
1355 printf("@%d", elem
.data
.integer
);
1356 error
= elem
.data
.integer
;
1359 fputs("[errorIndex==0]", stdout
);
1365 varbind_print(pduid
, np
, length
);
1370 * Decode SNMP Trap PDU
1373 trappdu_print(const u_char
*np
, u_int length
)
1376 int count
= 0, generic
;
1380 /* enterprise (oid) */
1381 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1383 if (elem
.type
!= BE_OID
) {
1384 fputs("[enterprise!=OID]", stdout
);
1388 if (asn1_print(&elem
) < 0)
1395 /* agent-addr (inetaddr) */
1396 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1398 if (elem
.type
!= BE_INETADDR
) {
1399 fputs("[agent-addr!=INETADDR]", stdout
);
1403 if (asn1_print(&elem
) < 0)
1408 /* generic-trap (Integer) */
1409 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1411 if (elem
.type
!= BE_INT
) {
1412 fputs("[generic-trap!=INT]", stdout
);
1416 generic
= elem
.data
.integer
;
1419 printf(" %s", DECODE_GenericTrap(generic
));
1424 /* specific-trap (Integer) */
1425 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1427 if (elem
.type
!= BE_INT
) {
1428 fputs("[specific-trap!=INT]", stdout
);
1432 if (generic
!= GT_ENTERPRISE
) {
1433 if (elem
.data
.integer
!= 0)
1434 printf("[specific-trap(%d)!=0]", elem
.data
.integer
);
1436 printf(" s=%d", elem
.data
.integer
);
1442 /* time-stamp (TimeTicks) */
1443 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1445 if (elem
.type
!= BE_UNS
) { /* XXX */
1446 fputs("[time-stamp!=TIMETICKS]", stdout
);
1450 if (asn1_print(&elem
) < 0)
1455 varbind_print (TRAP
, np
, length
);
1460 * Decode arbitrary SNMP PDUs.
1463 pdu_print(const u_char
*np
, u_int length
, int version
)
1469 if ((count
= asn1_parse(np
, length
, &pdu
)) < 0)
1471 if (pdu
.type
!= BE_PDU
) {
1472 fputs("[no PDU]", stdout
);
1475 if ((u_int
)count
< length
)
1476 printf("[%d extra after PDU]", length
- count
);
1478 fputs("{ ", stdout
);
1480 if (asn1_print(&pdu
) < 0)
1483 /* descend into PDU */
1484 length
= pdu
.asnlen
;
1485 np
= (u_char
*)pdu
.data
.raw
;
1487 if (version
== SNMP_VERSION_1
&&
1488 (pdu
.id
== GETBULKREQ
|| pdu
.id
== INFORMREQ
||
1489 pdu
.id
== V2TRAP
|| pdu
.id
== REPORT
)) {
1490 printf("[v2 PDU in v1 message]");
1494 if (version
== SNMP_VERSION_2
&& pdu
.id
== TRAP
) {
1495 printf("[v1 PDU in v2 message]");
1501 trappdu_print(np
, length
);
1511 snmppdu_print(pdu
.id
, np
, length
);
1516 fputs(" } ", stdout
);
1521 * Decode a scoped SNMP PDU.
1524 scopedpdu_print(const u_char
*np
, u_int length
, int version
)
1530 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1532 if (elem
.type
!= BE_SEQ
) {
1533 fputs("[!scoped PDU]", stdout
);
1537 length
= elem
.asnlen
;
1538 np
= (u_char
*)elem
.data
.raw
;
1540 /* contextEngineID (OCTET STRING) */
1541 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1543 if (elem
.type
!= BE_STR
) {
1544 fputs("[contextEngineID!=STR]", stdout
);
1551 fputs("E= ", stdout
);
1552 for (i
= 0; i
< (int)elem
.asnlen
; i
++) {
1553 printf("0x%02X", elem
.data
.str
[i
]);
1557 /* contextName (OCTET STRING) */
1558 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1560 if (elem
.type
!= BE_STR
) {
1561 fputs("[contextName!=STR]", stdout
);
1568 printf("C=%.*s ", (int)elem
.asnlen
, elem
.data
.str
);
1570 pdu_print(np
, length
, version
);
1574 * Decode SNMP Community Header (SNMPv1 and SNMPv2c)
1577 community_print(const u_char
*np
, u_int length
, int version
)
1582 /* Community (String) */
1583 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1585 if (elem
.type
!= BE_STR
) {
1586 fputs("[comm!=STR]", stdout
);
1590 /* default community */
1591 if (!(elem
.asnlen
== sizeof(DEF_COMMUNITY
) - 1 &&
1592 strncmp((char *)elem
.data
.str
, DEF_COMMUNITY
,
1593 sizeof(DEF_COMMUNITY
) - 1) == 0))
1595 printf("C=%.*s ", (int)elem
.asnlen
, elem
.data
.str
);
1599 pdu_print(np
, length
, version
);
1603 * Decode SNMPv3 User-based Security Message Header (SNMPv3)
1606 usm_print(const u_char
*np
, u_int length
)
1612 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1614 if (elem
.type
!= BE_SEQ
) {
1615 fputs("[!usm]", stdout
);
1619 length
= elem
.asnlen
;
1620 np
= (u_char
*)elem
.data
.raw
;
1622 /* msgAuthoritativeEngineID (OCTET STRING) */
1623 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1625 if (elem
.type
!= BE_STR
) {
1626 fputs("[msgAuthoritativeEngineID!=STR]", stdout
);
1633 /* msgAuthoritativeEngineBoots (INTEGER) */
1634 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1636 if (elem
.type
!= BE_INT
) {
1637 fputs("[msgAuthoritativeEngineBoots!=INT]", stdout
);
1642 printf("B=%d ", elem
.data
.integer
);
1646 /* msgAuthoritativeEngineTime (INTEGER) */
1647 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1649 if (elem
.type
!= BE_INT
) {
1650 fputs("[msgAuthoritativeEngineTime!=INT]", stdout
);
1655 printf("T=%d ", elem
.data
.integer
);
1659 /* msgUserName (OCTET STRING) */
1660 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1662 if (elem
.type
!= BE_STR
) {
1663 fputs("[msgUserName!=STR]", stdout
);
1670 printf("U=%.*s ", (int)elem
.asnlen
, elem
.data
.str
);
1672 /* msgAuthenticationParameters (OCTET STRING) */
1673 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1675 if (elem
.type
!= BE_STR
) {
1676 fputs("[msgAuthenticationParameters!=STR]", stdout
);
1683 /* msgPrivacyParameters (OCTET STRING) */
1684 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1686 if (elem
.type
!= BE_STR
) {
1687 fputs("[msgPrivacyParameters!=STR]", stdout
);
1694 if ((u_int
)count
< length
)
1695 printf("[%d extra after usm SEQ]", length
- count
);
1699 * Decode SNMPv3 Message Header (SNMPv3)
1702 v3msg_print(const u_char
*np
, u_int length
)
1708 const u_char
*xnp
= np
;
1709 int xlength
= length
;
1712 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1714 if (elem
.type
!= BE_SEQ
) {
1715 fputs("[!message]", stdout
);
1719 length
= elem
.asnlen
;
1720 np
= (u_char
*)elem
.data
.raw
;
1723 fputs("{ ", stdout
);
1726 /* msgID (INTEGER) */
1727 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1729 if (elem
.type
!= BE_INT
) {
1730 fputs("[msgID!=INT]", stdout
);
1737 /* msgMaxSize (INTEGER) */
1738 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1740 if (elem
.type
!= BE_INT
) {
1741 fputs("[msgMaxSize!=INT]", stdout
);
1748 /* msgFlags (OCTET STRING) */
1749 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1751 if (elem
.type
!= BE_STR
) {
1752 fputs("[msgFlags!=STR]", stdout
);
1756 if (elem
.asnlen
!= 1) {
1757 printf("[msgFlags size %d]", elem
.asnlen
);
1760 flags
= elem
.data
.str
[0];
1761 if (flags
!= 0x00 && flags
!= 0x01 && flags
!= 0x03
1762 && flags
!= 0x04 && flags
!= 0x05 && flags
!= 0x07) {
1763 printf("[msgFlags=0x%02X]", flags
);
1769 fputs("F=", stdout
);
1770 if (flags
& 0x01) fputs("a", stdout
);
1771 if (flags
& 0x02) fputs("p", stdout
);
1772 if (flags
& 0x04) fputs("r", stdout
);
1775 /* msgSecurityModel (INTEGER) */
1776 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1778 if (elem
.type
!= BE_INT
) {
1779 fputs("[msgSecurityModel!=INT]", stdout
);
1783 model
= elem
.data
.integer
;
1787 if ((u_int
)count
< length
)
1788 printf("[%d extra after message SEQ]", length
- count
);
1791 fputs("} ", stdout
);
1796 fputs("{ USM ", stdout
);
1799 printf("[security model %d]", model
);
1803 np
= xnp
+ (np
- xnp
);
1804 length
= xlength
- (np
- xnp
);
1806 /* msgSecurityParameters (OCTET STRING) */
1807 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1809 if (elem
.type
!= BE_STR
) {
1810 fputs("[msgSecurityParameters!=STR]", stdout
);
1818 usm_print(elem
.data
.str
, elem
.asnlen
);
1820 fputs("} ", stdout
);
1825 fputs("{ ScopedPDU ", stdout
);
1828 scopedpdu_print(np
, length
, 3);
1831 fputs("} ", stdout
);
1836 * Decode SNMP header and pass on to PDU printing routines
1839 snmp_print(const u_char
*np
, u_int length
)
1847 /* initial Sequence */
1848 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1850 if (elem
.type
!= BE_SEQ
) {
1851 fputs("[!init SEQ]", stdout
);
1855 if ((u_int
)count
< length
)
1856 printf("[%d extra after iSEQ]", length
- count
);
1858 length
= elem
.asnlen
;
1859 np
= (u_char
*)elem
.data
.raw
;
1861 /* Version (INTEGER) */
1862 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1864 if (elem
.type
!= BE_INT
) {
1865 fputs("[version!=INT]", stdout
);
1870 switch (elem
.data
.integer
) {
1871 case SNMP_VERSION_1
:
1872 case SNMP_VERSION_2
:
1873 case SNMP_VERSION_3
:
1875 printf("{ %s ", SnmpVersion
[elem
.data
.integer
]);
1878 printf("[version = %d]", elem
.data
.integer
);
1881 version
= elem
.data
.integer
;
1886 case SNMP_VERSION_1
:
1887 case SNMP_VERSION_2
:
1888 community_print(np
, length
, version
);
1890 case SNMP_VERSION_3
:
1891 v3msg_print(np
, length
);
1894 printf("[version = %d]", elem
.data
.integer
);
1899 fputs("} ", stdout
);