2 * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by John Robert LoVerso.
11 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
12 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
13 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
15 * This implementation has been influenced by the CMU SNMP release,
16 * by Steve Waldbusser. However, this shares no code with that system.
17 * Additional ASN.1 insight gained from Marshall T. Rose's _The_Open_Book_.
18 * Earlier forms of this implementation were derived and/or inspired by an
19 * awk script originally written by C. Philip Wood of LANL (but later
20 * heavily modified by John Robert LoVerso). The copyright notice for
21 * that work is preserved below, even though it may not rightly apply
24 * Support for SNMPv2c/SNMPv3 and the ability to link the module against
25 * the libsmi was added by J. Schoenwaelder, Copyright (c) 1999.
27 * This started out as a very simple program, but the incremental decoding
28 * (into the BE structure) complicated things.
30 # Los Alamos National Laboratory
32 # Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996, 1997
33 # This software was produced under a U.S. Government contract
34 # (W-7405-ENG-36) by Los Alamos National Laboratory, which is
35 # operated by the University of California for the U.S. Department
36 # of Energy. The U.S. Government is licensed to use, reproduce,
37 # and distribute this software. Permission is granted to the
38 # public to copy and use this software without charge, provided
39 # that this Notice and any statement of authorship are reproduced
40 # on all copies. Neither the Government nor the University makes
41 # any warranty, express or implied, or assumes any liability or
42 # responsibility for the use of this software.
43 # @(#)snmp.awk.x 1.1 (LANL) 1/15/90
47 static const char rcsid
[] =
48 "@(#) $Header: /tcpdump/master/tcpdump/print-snmp.c,v 1.40 2000-01-17 06:24:26 itojun Exp $ (LBL)";
55 #include <sys/param.h>
69 #include "interface.h"
70 #include "addrtoname.h"
73 * Universal ASN.1 types
74 * (we only care about the tag values for those allowed in the Internet SMI)
89 "U-8","U-9","U-10","U-11", /* 8-11 */
90 "U-12","U-13","U-14","U-15", /* 12-15 */
97 * Application-wide ASN.1 types from the Internet SMI and their tags
99 char *Application
[] = {
116 * Context-specific ASN.1 types for the SNMP PDUs and their tags
139 #define NOTIFY_CLASS(x) (x == TRAP || x == V2TRAP || x == INFORMREQ)
140 #define READ_CLASS(x) (x == GETREQ || x == GETNEXTREQ || x == GETBULKREQ)
141 #define WRITE_CLASS(x) (x == SETREQ)
142 #define RESPONSE_CLASS(x) (x == GETRESP)
143 #define INTERNAL_CLASS(x) (x == REPORT)
146 * Context-specific ASN.1 types for the SNMP Exceptions and their tags
148 char *Exceptions
[] = {
150 #define NOSUCHOBJECT 0
152 #define NOSUCHINSTANCE 1
154 #define ENDOFMIBVIEW 2
158 * Private ASN.1 types
159 * The Internet SMI does not specify any
166 * error-status values for any SNMP PDU
168 char *ErrorStatus
[] = {
182 "resourceUnavailable",
185 "authorizationError",
189 #define DECODE_ErrorStatus(e) \
190 ( e >= 0 && e < sizeof(ErrorStatus)/sizeof(ErrorStatus[0]) \
192 : (snprintf(errbuf, sizeof(errbuf), "err=%u", e), errbuf))
195 * generic-trap values in the SNMP Trap-PDU
197 char *GenericTrap
[] = {
202 "authenticationFailure",
205 #define GT_ENTERPRISE 7
207 #define DECODE_GenericTrap(t) \
208 ( t >= 0 && t < sizeof(GenericTrap)/sizeof(GenericTrap[0]) \
210 : (snprintf(buf, sizeof(buf), "gt=%d", t), buf))
213 * ASN.1 type class table
214 * Ties together the preceding Universal, Application, Context, and Private
217 #define defineCLASS(x) { "x", x, sizeof(x)/sizeof(x[0]) } /* not ANSI-C */
223 defineCLASS(Universal
),
225 defineCLASS(Application
),
226 #define APPLICATION 1
227 defineCLASS(Context
),
229 defineCLASS(Private
),
231 defineCLASS(Exceptions
),
236 * defined forms for ASN.1 types
242 #define CONSTRUCTED 1
246 * A structure for the OID tree for the compiled-in MIB.
247 * This is stored as a general-order tree.
250 char *desc
; /* name of object */
251 u_char oid
; /* sub-id following parent */
252 u_char type
; /* object type (unused) */
253 struct obj
*child
, *next
; /* child and next sibling pointers */
257 * Include the compiled in SNMP MIB. "mib.h" is produced by feeding
258 * RFC-1156 format files into "makemib". "mib.h" MUST define at least
259 * a value for `mibroot'.
261 * In particular, this is gross, as this is including initialized structures,
262 * and by right shouldn't be an "include" file.
267 * This defines a list of OIDs which will be abbreviated on output.
268 * Currently, this includes the prefixes for the Internet MIB, the
269 * private enterprises tree, and the experimental tree.
272 char *prefix
; /* prefix for this abrev */
273 struct obj
*node
; /* pointer into object table */
274 char *oid
; /* ASN.1 encoded OID */
275 } obj_abrev_list
[] = {
277 /* .iso.org.dod.internet.mgmt.mib */
278 { "", &_mib_obj
, "\53\6\1\2\1" },
280 #ifndef NO_ABREV_ENTER
281 /* .iso.org.dod.internet.private.enterprises */
282 { "E:", &_enterprises_obj
, "\53\6\1\4\1" },
284 #ifndef NO_ABREV_EXPERI
285 /* .iso.org.dod.internet.experimental */
286 { "X:", &_experimental_obj
, "\53\6\1\3" },
288 #ifndef NO_ABBREV_SNMPMODS
289 /* .iso.org.dod.internet.snmpV2.snmpModules */
290 { "S:", &_snmpModules_obj
, "\53\6\1\6\3" },
296 * This is used in the OID print routine to walk down the object tree
297 * rooted at `mibroot'.
299 #define OBJ_PRINT(o, suppressdot) \
303 if ((o) == objp->oid) \
305 } while ((objp = objp->next) != NULL); \
308 printf(suppressdot?"%s":".%s", objp->desc); \
309 objp = objp->child; \
311 printf(suppressdot?"%u":".%u", (o)); \
315 * This is the definition for the Any-Data-Type storage used purely for
316 * temporary internal representation while decoding an ASN.1 data stream.
331 u_char form
, class; /* tag info */
342 #define BE_INETADDR 8
345 #define BE_NOSUCHOBJECT 128
346 #define BE_NOSUCHINST 129
347 #define BE_ENDOFMIBVIEW 130
351 * SNMP versions recognized by this module
353 char *SnmpVersion
[] = {
355 #define SNMP_VERSION_1 0
357 #define SNMP_VERSION_2 1
359 #define SNMP_VERSION_2U 2
361 #define SNMP_VERSION_3 3
365 * Defaults for SNMP PDU components
367 #define DEF_COMMUNITY "public"
370 * constants for ASN.1 decoding
373 #define ASNLEN_INETADDR 4
376 #define ASN_BIT8 0x80
377 #define ASN_LONGLEN 0x80
379 #define ASN_ID_BITS 0x1f
380 #define ASN_FORM_BITS 0x20
381 #define ASN_FORM_SHIFT 5
382 #define ASN_CLASS_BITS 0xc0
383 #define ASN_CLASS_SHIFT 6
385 #define ASN_ID_EXT 0x1f /* extension ID in tag field */
388 * truncated==1 means the packet was complete, but we don't have all of
391 static int truncated
;
392 #define ifNotTruncated if (truncated) fputs("[|snmp]", stdout); else
395 * This decodes the next ASN.1 object in the stream pointed to by "p"
396 * (and of real-length "len") and stores the intermediate data in the
397 * provided BE object.
399 * This returns -l if it fails (i.e., the ASN.1 stream is not valid).
400 * O/w, this returns the number of bytes parsed from "p".
403 asn1_parse(register const u_char
*p
, u_int len
, struct be
*elem
)
405 u_char form
, class, id
;
411 ifNotTruncated
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
;
437 /* extended tag field */
438 if (id
== ASN_ID_EXT
) {
439 for (id
= 0; *p
& ASN_BIT8
&& len
> 0; len
--, hdr
++, p
++) {
442 id
= (id
<< 7) | (*p
& ~ASN_BIT8
);
444 if (len
== 0 && *p
& ASN_BIT8
) {
445 ifNotTruncated
fputs("[Xtagfield?]", stdout
);
448 elem
->id
= id
= (id
<< 7) | *p
;
454 ifNotTruncated
fputs("[no asnlen]", stdout
);
461 if (elem
->asnlen
& ASN_BIT8
) {
462 int noct
= elem
->asnlen
% ASN_BIT8
;
465 ifNotTruncated
printf("[asnlen? %d<%d]", len
, noct
);
468 for (; noct
-- > 0; len
--, hdr
++) {
471 elem
->asnlen
= (elem
->asnlen
<< ASN_SHIFT8
) | *p
++;
474 if (len
< elem
->asnlen
) {
476 printf("[len%d<asnlen%u]", len
, elem
->asnlen
);
479 /* maybe should check at least 4? */
482 if (form
>= sizeof(Form
)/sizeof(Form
[0])) {
483 ifNotTruncated
printf("[form?%d]", form
);
486 if (class >= sizeof(Class
)/sizeof(Class
[0])) {
487 ifNotTruncated
printf("[class?%c/%d]", *Form
[form
], class);
490 if ((int)id
>= Class
[class].numIDs
) {
491 ifNotTruncated
printf("[id?%c/%s/%d]", *Form
[form
],
492 Class
[class].name
, id
);
507 register int32_t data
;
511 if (*p
& ASN_BIT8
) /* negative */
513 for (i
= elem
->asnlen
; i
-- > 0; p
++)
514 data
= (data
<< ASN_SHIFT8
) | *p
;
515 elem
->data
.integer
= data
;
521 elem
->data
.raw
= (caddr_t
)p
;
525 elem
->type
= BE_NULL
;
526 elem
->data
.raw
= NULL
;
530 elem
->type
= BE_OCTET
;
531 elem
->data
.raw
= (caddr_t
)p
;
533 Class
[class].Id
[id
]);
541 elem
->type
= BE_INETADDR
;
542 elem
->data
.raw
= (caddr_t
)p
;
548 register u_int32_t data
;
551 for (i
= elem
->asnlen
; i
-- > 0; p
++)
552 data
= (data
<< 8) + *p
;
553 elem
->data
.uns
= data
;
558 register u_int32_t high
, low
;
559 elem
->type
= BE_UNS64
;
561 for (i
= elem
->asnlen
; i
-- > 0; p
++) {
563 ((low
& 0xFF000000) >> 24);
564 low
= (low
<< 8) | *p
;
566 elem
->data
.uns64
.high
= high
;
567 elem
->data
.uns64
.low
= low
;
572 elem
->type
= BE_OCTET
;
573 elem
->data
.raw
= (caddr_t
)p
;
575 Class
[class].Id
[id
]);
583 elem
->type
= BE_NOSUCHOBJECT
;
584 elem
->data
.raw
= NULL
;
588 elem
->type
= BE_NOSUCHINST
;
589 elem
->data
.raw
= NULL
;
593 elem
->type
= BE_ENDOFMIBVIEW
;
594 elem
->data
.raw
= NULL
;
600 elem
->type
= BE_OCTET
;
601 elem
->data
.raw
= (caddr_t
)p
;
603 Class
[class].name
, Class
[class].Id
[id
]);
614 elem
->data
.raw
= (caddr_t
)p
;
618 elem
->type
= BE_OCTET
;
619 elem
->data
.raw
= (caddr_t
)p
;
620 printf("C/U/%s", Class
[class].Id
[id
]);
627 elem
->data
.raw
= (caddr_t
)p
;
631 elem
->type
= BE_OCTET
;
632 elem
->data
.raw
= (caddr_t
)p
;
634 Class
[class].name
, Class
[class].Id
[id
]);
641 return elem
->asnlen
+ hdr
;
645 * Display the ASN.1 object represented by the BE object.
646 * This used to be an integral part of asn1_parse() before the intermediate
650 asn1_print(struct be
*elem
)
652 u_char
*p
= (u_char
*)elem
->data
.raw
;
653 u_int32_t asnlen
= elem
->asnlen
;
656 switch (elem
->type
) {
659 for (i
= asnlen
; i
-- > 0; p
++);
667 int o
= 0, first
= -1, i
= asnlen
;
669 if (!sflag
&& !nflag
&& asnlen
> 2) {
670 struct obj_abrev
*a
= &obj_abrev_list
[0];
671 for (; a
->node
; a
++) {
672 if (!memcmp(a
->oid
, (char *)p
,
674 objp
= a
->node
->child
;
677 fputs(a
->prefix
, stdout
);
684 for (; !sflag
&& i
-- > 0; p
++) {
685 o
= (o
<< ASN_SHIFT7
) + (*p
& ~ASN_BIT8
);
686 if (*p
& ASN_LONGLEN
)
690 * first subitem encodes two items with 1st*OIDMUX+2nd
696 OBJ_PRINT(o
/OIDMUX
, first
);
708 printf("%d", elem
->data
.integer
);
712 printf("%u", elem
->data
.uns
);
715 case BE_UNS64
: { /* idea borrowed from by Marshall Rose */
718 char *cpf
, *cpl
, last
[6], first
[30];
719 if (elem
->data
.uns64
.high
== 0) {
720 printf("%u", elem
->data
.uns64
.low
);
723 d
= elem
->data
.uns64
.high
* 4294967296.0; /* 2^32 */
724 if (elem
->data
.uns64
.high
<= 0x1fffff) {
725 d
+= elem
->data
.uns64
.low
;
726 #if 0 /*is looks illegal, but what is the intention???*/
733 d
+= (elem
->data
.uns64
.low
& 0xfffff000);
734 #if 0 /*is looks illegal, but what is the intention???*/
735 snprintf(first
, sizeof(first
), "%.f", d
);
737 snprintf(first
, sizeof(first
), "%f", d
);
739 snprintf(last
, sizeof(last
), "%5.5d",
740 elem
->data
.uns64
.low
& 0xfff);
741 for (carry
= 0, cpf
= first
+strlen(first
)-1, cpl
= last
+4;
744 j
= carry
+ (*cpf
- '0') + (*cpl
- '0');
753 fputs(first
, stdout
);
758 register int printable
= 1, first
= 1;
759 const u_char
*p
= elem
->data
.str
;
760 for (i
= asnlen
; printable
&& i
-- > 0; p
++)
761 printable
= isprint(*p
) || isspace(*p
);
765 (void)fn_print(p
, p
+ asnlen
);
768 for (i
= asnlen
; i
-- > 0; p
++) {
769 printf(first
? "%.2x" : "_%.2x", *p
);
776 printf("Seq(%u)", elem
->asnlen
);
780 if (asnlen
!= ASNLEN_INETADDR
)
781 printf("[inetaddr len!=%d]", ASNLEN_INETADDR
);
782 for (i
= asnlen
; i
-- > 0; p
++) {
783 printf((i
== asnlen
-1) ? "%u" : ".%u", *p
);
787 case BE_NOSUCHOBJECT
:
789 case BE_ENDOFMIBVIEW
:
790 printf("[%s]", Class
[EXCEPTIONS
].Id
[elem
->id
]);
795 Class
[CONTEXT
].Id
[elem
->id
], elem
->asnlen
);
799 fputs("[BE_ANY!?]", stdout
);
803 fputs("[be!?]", stdout
);
810 * This is a brute force ASN.1 printer: recurses to dump an entire structure.
811 * This will work for any ASN.1 stream, not just an SNMP PDU.
813 * By adding newlines and spaces at the correct places, this would print in
816 * This is not currently used.
819 asn1_decode(u_char
*p
, u_int length
)
824 while (i
>= 0 && length
> 0) {
825 i
= asn1_parse(p
, length
, &elem
);
829 if (elem
.type
== BE_SEQ
|| elem
.type
== BE_PDU
) {
831 asn1_decode(elem
.data
.raw
, elem
.asnlen
);
844 SmiBasetype basetype
;
848 struct smi2be smi2betab
[] = {
849 { SMI_BASETYPE_INTEGER32
, BE_INT
},
850 { SMI_BASETYPE_OCTETSTRING
, BE_STR
},
851 { SMI_BASETYPE_OCTETSTRING
, BE_INETADDR
},
852 { SMI_BASETYPE_OBJECTIDENTIFIER
, BE_OID
},
853 { SMI_BASETYPE_UNSIGNED32
, BE_UNS
},
854 { SMI_BASETYPE_INTEGER64
, BE_NONE
},
855 { SMI_BASETYPE_UNSIGNED64
, BE_UNS64
},
856 { SMI_BASETYPE_FLOAT32
, BE_NONE
},
857 { SMI_BASETYPE_FLOAT64
, BE_NONE
},
858 { SMI_BASETYPE_FLOAT128
, BE_NONE
},
859 { SMI_BASETYPE_ENUM
, BE_INT
},
860 { SMI_BASETYPE_BITS
, BE_STR
},
861 { SMI_BASETYPE_UNKNOWN
, BE_NONE
}
864 static void smi_decode_oid(struct be
*elem
, unsigned int *oid
,
865 unsigned int *oidlen
)
867 u_char
*p
= (u_char
*)elem
->data
.raw
;
868 u_int32_t asnlen
= elem
->asnlen
;
869 int o
= 0, first
= -1, i
= asnlen
;
871 for (*oidlen
= 0; sflag
&& i
-- > 0; p
++) {
872 o
= (o
<< ASN_SHIFT7
) + (*p
& ~ASN_BIT8
);
873 if (*p
& ASN_LONGLEN
)
877 * first subitem encodes two items with 1st*OIDMUX+2nd
881 oid
[(*oidlen
)++] = o
/OIDMUX
;
884 oid
[(*oidlen
)++] = o
;
889 static int smi_check_type(SmiBasetype basetype
, int be
)
893 for (i
= 0; smi2betab
[i
].basetype
!= SMI_BASETYPE_UNKNOWN
; i
++) {
894 if (smi2betab
[i
].basetype
== basetype
&& smi2betab
[i
].be
== be
) {
902 static int smi_check_a_range(SmiType
*smiType
, SmiRange
*smiRange
,
907 switch (smiType
->basetype
) {
908 case SMI_BASETYPE_OBJECTIDENTIFIER
:
909 case SMI_BASETYPE_OCTETSTRING
:
910 if (smiRange
->minValue
.value
.unsigned32
911 == smiRange
->maxValue
.value
.unsigned32
) {
912 ok
= (elem
->asnlen
== smiRange
->minValue
.value
.unsigned32
);
914 ok
= (elem
->asnlen
>= smiRange
->minValue
.value
.unsigned32
915 && elem
->asnlen
<= smiRange
->maxValue
.value
.unsigned32
);
919 case SMI_BASETYPE_INTEGER32
:
920 ok
= (elem
->data
.integer
>= smiRange
->minValue
.value
.integer32
921 && elem
->data
.integer
<= smiRange
->maxValue
.value
.integer32
);
924 case SMI_BASETYPE_UNSIGNED32
:
925 ok
= (elem
->data
.uns
>= smiRange
->minValue
.value
.unsigned32
926 && elem
->data
.uns
<= smiRange
->maxValue
.value
.unsigned32
);
929 case SMI_BASETYPE_UNSIGNED64
:
933 /* case SMI_BASETYPE_INTEGER64: SMIng */
934 /* case SMI_BASETYPE_FLOAT32: SMIng */
935 /* case SMI_BASETYPE_FLOAT64: SMIng */
936 /* case SMI_BASETYPE_FLOAT128: SMIng */
938 case SMI_BASETYPE_ENUM
:
939 case SMI_BASETYPE_BITS
:
940 case SMI_BASETYPE_UNKNOWN
:
948 static int smi_check_range(SmiType
*smiType
, struct be
*elem
)
953 for (smiRange
= smiGetFirstRange(smiType
->module
, smiType
->name
);
955 smiRange
= smiGetNextRange(smiRange
)) {
957 ok
= smi_check_a_range(smiType
, smiRange
, elem
);
960 smiFreeRange(smiRange
);
965 if (ok
&& smiType
->parentmodule
&& smiType
->parentname
) {
967 parentType
= smiGetType(smiType
->parentmodule
,
968 smiType
->parentname
);
970 ok
= smi_check_range(parentType
, elem
);
971 smiFreeType(parentType
);
978 static SmiNode
*smi_print_variable(struct be
*elem
)
980 unsigned int oid
[128], oidlen
;
981 SmiNode
*smiNode
= NULL
;
984 smi_decode_oid(elem
, oid
, &oidlen
);
985 smiNode
= smiGetNodeByOID(oidlen
, oid
);
991 fputs(smiNode
->module
, stdout
);
994 fputs(smiNode
->name
, stdout
);
995 if (smiNode
->oidlen
< oidlen
) {
996 for (i
= smiNode
->oidlen
; i
< oidlen
; i
++) {
997 printf(".%u", oid
[i
]);
1003 static void smi_print_value(SmiNode
*smiNode
, u_char pduid
, struct be
*elem
)
1005 unsigned int oid
[128], oidlen
;
1010 if (! smiNode
|| ! (smiNode
->nodekind
1011 & (SMI_NODEKIND_SCALAR
| SMI_NODEKIND_COLUMN
))) {
1016 if (NOTIFY_CLASS(pduid
) && smiNode
->access
< SMI_ACCESS_NOTIFY
) {
1017 fputs("[notNotifyable]", stdout
);
1020 if (READ_CLASS(pduid
) && smiNode
->access
< SMI_ACCESS_READ_ONLY
) {
1021 fputs("[notReadable]", stdout
);
1024 if (WRITE_CLASS(pduid
) && smiNode
->access
< SMI_ACCESS_READ_WRITE
) {
1025 fputs("[notWritable]", stdout
);
1028 if (RESPONSE_CLASS(pduid
)
1029 && smiNode
->access
== SMI_ACCESS_NOT_ACCESSIBLE
) {
1030 fputs("[noAccess]", stdout
);
1033 if (! smi_check_type(smiNode
->basetype
, elem
->type
)) {
1034 fputs("[wrongType]", stdout
);
1037 smiType
= smiGetType(smiNode
->typemodule
, smiNode
->typename
);
1043 if (! smi_check_range(smiType
, elem
)) {
1044 fputs("[wrongLength]", stdout
);
1047 /* resolve bits to named bits */
1049 /* check whether instance identifier is valid */
1051 /* apply display hints (integer, octetstring) */
1053 /* convert instance identifier to index type values */
1055 switch (elem
->type
) {
1057 if (smiNode
->basetype
== SMI_BASETYPE_BITS
1058 && smiNode
->typemodule
&& smiNode
->typename
) {
1059 /* print bit labels */
1061 smi_decode_oid(elem
, oid
, &oidlen
);
1062 smiNode
= smiGetNodeByOID(oidlen
, oid
);
1065 fputs(smiNode
->module
, stdout
);
1066 fputs("::", stdout
);
1068 fputs(smiNode
->name
, stdout
);
1069 if (smiNode
->oidlen
< oidlen
) {
1070 for (i
= smiNode
->oidlen
;
1072 printf(".%u", oid
[i
]);
1081 if (smiNode
->basetype
== SMI_BASETYPE_ENUM
1082 && smiNode
->typemodule
&& smiNode
->typename
) {
1083 for (nn
= smiGetFirstNamedNumber(smiNode
->typemodule
,
1086 nn
= smiGetNextNamedNumber(nn
)) {
1087 if (nn
->value
.value
.integer32
1088 == elem
->data
.integer
) {
1089 fputs(nn
->name
, stdout
);
1090 printf("(%d)", elem
->data
.integer
);
1104 smiFreeType(smiType
);
1110 * General SNMP header
1112 * version INTEGER {version-1(0)},
1113 * community OCTET STRING,
1116 * PDUs for all but Trap: (see rfc1157 from page 15 on)
1118 * request-id INTEGER,
1119 * error-status INTEGER,
1120 * error-index INTEGER,
1121 * varbindlist SEQUENCE OF
1129 * enterprise OBJECT IDENTIFIER,
1130 * agent-addr NetworkAddress,
1131 * generic-trap INTEGER,
1132 * specific-trap INTEGER,
1133 * time-stamp TimeTicks,
1134 * varbindlist SEQUENCE OF
1143 * Decode SNMP varBind
1146 varbind_print(u_char pduid
, const u_char
*np
, u_int length
)
1151 SmiNode
*smiNode
= NULL
;
1154 /* Sequence of varBind */
1155 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1157 if (elem
.type
!= BE_SEQ
) {
1158 fputs("[!SEQ of varbind]", stdout
);
1163 printf("[%d extra after SEQ of varbind]", length
- count
);
1165 length
= elem
.asnlen
;
1166 np
= (u_char
*)elem
.data
.raw
;
1168 for (ind
= 1; length
> 0; ind
++) {
1169 const u_char
*vbend
;
1175 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1177 if (elem
.type
!= BE_SEQ
) {
1178 fputs("[!varbind]", stdout
);
1183 vblength
= length
- count
;
1185 length
= elem
.asnlen
;
1186 np
= (u_char
*)elem
.data
.raw
;
1189 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1191 if (elem
.type
!= BE_OID
) {
1192 fputs("[objName!=OID]", stdout
);
1197 smiNode
= smi_print_variable(&elem
);
1204 if (pduid
!= GETREQ
&& pduid
!= GETNEXTREQ
1205 && pduid
!= GETBULKREQ
)
1209 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1211 if (pduid
== GETREQ
|| pduid
== GETNEXTREQ
1212 || pduid
== GETBULKREQ
) {
1213 if (elem
.type
!= BE_NULL
) {
1214 fputs("[objVal!=NULL]", stdout
);
1218 if (elem
.type
!= BE_NULL
) {
1220 smi_print_value(smiNode
, pduid
, &elem
);
1221 smiFreeNode(smiNode
);
1233 * Decode SNMP PDUs: GetRequest, GetNextRequest, GetResponse, SetRequest,
1234 * GetBulk, Inform, V2Trap, and Report
1237 snmppdu_print(u_char pduid
, const u_char
*np
, u_int length
)
1240 int count
= 0, error
;
1242 /* reqId (Integer) */
1243 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1245 if (elem
.type
!= BE_INT
) {
1246 fputs("[reqId!=INT]", stdout
);
1250 /* ignore the reqId */
1254 /* errorStatus (Integer) */
1255 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1257 if (elem
.type
!= BE_INT
) {
1258 fputs("[errorStatus!=INT]", stdout
);
1263 if ((pduid
== GETREQ
|| pduid
== GETNEXTREQ
|| pduid
== SETREQ
1264 || pduid
== INFORMREQ
|| pduid
== V2TRAP
|| pduid
== REPORT
)
1265 && elem
.data
.integer
!= 0) {
1267 printf("[errorStatus(%s)!=0]",
1268 DECODE_ErrorStatus(elem
.data
.integer
));
1269 } else if (pduid
== GETBULKREQ
) {
1270 printf(" N=%d", elem
.data
.integer
);
1271 } else if (elem
.data
.integer
!= 0) {
1273 printf(" %s", DECODE_ErrorStatus(elem
.data
.integer
));
1274 error
= elem
.data
.integer
;
1279 /* errorIndex (Integer) */
1280 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1282 if (elem
.type
!= BE_INT
) {
1283 fputs("[errorIndex!=INT]", stdout
);
1287 if ((pduid
== GETREQ
|| pduid
== GETNEXTREQ
|| pduid
== SETREQ
1288 || pduid
== INFORMREQ
|| pduid
== V2TRAP
|| pduid
== REPORT
)
1289 && elem
.data
.integer
!= 0)
1290 printf("[errorIndex(%d)!=0]", elem
.data
.integer
);
1291 else if (pduid
== GETBULKREQ
)
1292 printf(" M=%d", elem
.data
.integer
);
1293 else if (elem
.data
.integer
!= 0) {
1295 printf("[errorIndex(%d) w/o errorStatus]",
1298 printf("@%d", elem
.data
.integer
);
1299 error
= elem
.data
.integer
;
1302 fputs("[errorIndex==0]", stdout
);
1308 varbind_print(pduid
, np
, length
);
1313 * Decode SNMP Trap PDU
1316 trappdu_print(const u_char
*np
, u_int length
)
1319 int count
= 0, generic
;
1323 /* enterprise (oid) */
1324 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1326 if (elem
.type
!= BE_OID
) {
1327 fputs("[enterprise!=OID]", stdout
);
1337 /* agent-addr (inetaddr) */
1338 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1340 if (elem
.type
!= BE_INETADDR
) {
1341 fputs("[agent-addr!=INETADDR]", stdout
);
1349 /* generic-trap (Integer) */
1350 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1352 if (elem
.type
!= BE_INT
) {
1353 fputs("[generic-trap!=INT]", stdout
);
1357 generic
= elem
.data
.integer
;
1360 printf(" %s", DECODE_GenericTrap(generic
));
1365 /* specific-trap (Integer) */
1366 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1368 if (elem
.type
!= BE_INT
) {
1369 fputs("[specific-trap!=INT]", stdout
);
1373 if (generic
!= GT_ENTERPRISE
) {
1374 if (elem
.data
.integer
!= 0)
1375 printf("[specific-trap(%d)!=0]", elem
.data
.integer
);
1377 printf(" s=%d", elem
.data
.integer
);
1383 /* time-stamp (TimeTicks) */
1384 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1386 if (elem
.type
!= BE_UNS
) { /* XXX */
1387 fputs("[time-stamp!=TIMETICKS]", stdout
);
1395 varbind_print (TRAP
, np
, length
);
1400 * Decode arbitrary SNMP PDUs.
1403 pdu_print(const u_char
*np
, u_int length
, int version
)
1409 if ((count
= asn1_parse(np
, length
, &pdu
)) < 0)
1411 if (pdu
.type
!= BE_PDU
) {
1412 fputs("[no PDU]", stdout
);
1416 printf("[%d extra after PDU]", length
- count
);
1418 /* descend into PDU */
1419 length
= pdu
.asnlen
;
1420 np
= (u_char
*)pdu
.data
.raw
;
1422 if (version
== SNMP_VERSION_1
&&
1423 (pdu
.id
== GETBULKREQ
|| pdu
.id
== INFORMREQ
||
1424 pdu
.id
== V2TRAP
|| pdu
.id
== REPORT
)) {
1425 printf("[v2 PDU in v1 message]");
1429 if (version
== SNMP_VERSION_2
&& pdu
.id
== TRAP
) {
1430 printf("[v1 PDU in v2 message]");
1436 trappdu_print(np
, length
);
1446 snmppdu_print(pdu
.id
, np
, length
);
1452 * Decode a scoped SNMP PDU.
1455 scopedpdu_print(const u_char
*np
, u_int length
, int version
)
1461 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1463 if (elem
.type
!= BE_SEQ
) {
1464 fputs("[!scoped PDU]", stdout
);
1468 length
= elem
.asnlen
;
1469 np
= (u_char
*)elem
.data
.raw
;
1471 /* contextEngineID (OCTET STRING) */
1472 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1474 if (elem
.type
!= BE_STR
) {
1475 fputs("[contextEngineID!=STR]", stdout
);
1482 fputs("E= ", stdout
);
1483 for (i
= 0; i
< (int)elem
.asnlen
; i
++) {
1484 printf("0x%02X", elem
.data
.str
[i
]);
1488 /* contextName (OCTET STRING) */
1489 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1491 if (elem
.type
!= BE_STR
) {
1492 fputs("[contextName!=STR]", stdout
);
1499 printf("C=%.*s ", (int)elem
.asnlen
, elem
.data
.str
);
1501 pdu_print(np
, length
, version
);
1505 * Decode SNMP Community Header (SNMPv1 and SNMPv2c)
1508 community_print(const u_char
*np
, u_int length
, int version
)
1513 /* Community (String) */
1514 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1516 if (elem
.type
!= BE_STR
) {
1517 fputs("[comm!=STR]", stdout
);
1521 /* default community */
1522 if (strncmp((char *)elem
.data
.str
, DEF_COMMUNITY
,
1523 sizeof(DEF_COMMUNITY
) - 1))
1525 printf("C=%.*s ", (int)elem
.asnlen
, elem
.data
.str
);
1529 pdu_print(np
, length
, version
);
1533 * Decode SNMPv3 User-based Security Message Header (SNMPv3)
1536 usm_print(const u_char
*np
, u_int length
)
1542 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1544 if (elem
.type
!= BE_SEQ
) {
1545 fputs("[!usm]", stdout
);
1549 length
= elem
.asnlen
;
1550 np
= (u_char
*)elem
.data
.raw
;
1552 /* msgAuthoritativeEngineID (OCTET STRING) */
1553 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1555 if (elem
.type
!= BE_STR
) {
1556 fputs("[msgAuthoritativeEngineID!=STR]", stdout
);
1563 /* msgAuthoritativeEngineBoots (INTEGER) */
1564 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1566 if (elem
.type
!= BE_INT
) {
1567 fputs("[msgAuthoritativeEngineBoots!=INT]", stdout
);
1572 printf("B=%d ", elem
.data
.integer
);
1576 /* msgAuthoritativeEngineTime (INTEGER) */
1577 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1579 if (elem
.type
!= BE_INT
) {
1580 fputs("[msgAuthoritativeEngineTime!=INT]", stdout
);
1585 printf("T=%d ", elem
.data
.integer
);
1589 /* msgUserName (OCTET STRING) */
1590 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1592 if (elem
.type
!= BE_STR
) {
1593 fputs("[msgUserName!=STR]", stdout
);
1600 printf("U=%.*s ", (int)elem
.asnlen
, elem
.data
.str
);
1602 /* msgAuthenticationParameters (OCTET STRING) */
1603 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1605 if (elem
.type
!= BE_STR
) {
1606 fputs("[msgAuthenticationParameters!=STR]", stdout
);
1613 /* msgPrivacyParameters (OCTET STRING) */
1614 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1616 if (elem
.type
!= BE_STR
) {
1617 fputs("[msgPrivacyParameters!=STR]", stdout
);
1625 printf("[%d extra after usm SEQ]", length
- count
);
1629 * Decode SNMPv3 Message Header (SNMPv3)
1632 v3msg_print(const u_char
*np
, u_int length
)
1638 const u_char
*xnp
= np
;
1639 int xlength
= length
;
1642 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1644 if (elem
.type
!= BE_SEQ
) {
1645 fputs("[!message]", stdout
);
1649 length
= elem
.asnlen
;
1650 np
= (u_char
*)elem
.data
.raw
;
1652 /* msgID (INTEGER) */
1653 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1655 if (elem
.type
!= BE_INT
) {
1656 fputs("[msgID!=INT]", stdout
);
1663 /* msgMaxSize (INTEGER) */
1664 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1666 if (elem
.type
!= BE_INT
) {
1667 fputs("[msgMaxSize!=INT]", stdout
);
1674 /* msgFlags (OCTET STRING) */
1675 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1677 if (elem
.type
!= BE_STR
) {
1678 fputs("[msgFlags!=STR]", stdout
);
1682 if (elem
.asnlen
!= 1) {
1683 printf("[msgFlags size %d]", elem
.asnlen
);
1686 flags
= elem
.data
.str
[0];
1687 if (flags
!= 0x00 && flags
!= 0x01 && flags
!= 0x03
1688 && flags
!= 0x04 && flags
!= 0x05 && flags
!= 0x07) {
1689 printf("[msgFlags=0x%02X]", flags
);
1695 fputs("F=", stdout
);
1696 if (flags
& 0x01) fputs("a", stdout
);
1697 if (flags
& 0x02) fputs("p", stdout
);
1698 if (flags
& 0x04) fputs("r", stdout
);
1701 /* msgSecurityModel (INTEGER) */
1702 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1704 if (elem
.type
!= BE_INT
) {
1705 fputs("[msgSecurityModel!=INT]", stdout
);
1709 model
= elem
.data
.integer
;
1714 printf("[%d extra after message SEQ]", length
- count
);
1718 fputs("USM ", stdout
);
1721 printf("[security model %d]", model
);
1725 np
= xnp
+ (np
- xnp
);
1726 length
= xlength
- (np
- xnp
);
1728 /* msgSecurityParameters (OCTET STRING) */
1729 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1731 if (elem
.type
!= BE_STR
) {
1732 fputs("[msgSecurityParameters!=STR]", stdout
);
1740 usm_print(elem
.data
.str
, elem
.asnlen
);
1744 fputs("ScopedPDU ", stdout
);
1747 scopedpdu_print(np
, length
, 3);
1751 * Decode SNMP header and pass on to PDU printing routines
1754 snmp_print(const u_char
*np
, u_int length
)
1762 /* truncated packet? */
1763 if (np
+ length
> snapend
) {
1765 length
= snapend
- np
;
1770 /* initial Sequence */
1771 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1773 if (elem
.type
!= BE_SEQ
) {
1774 fputs("[!init SEQ]", stdout
);
1779 printf("[%d extra after iSEQ]", length
- count
);
1781 length
= elem
.asnlen
;
1782 np
= (u_char
*)elem
.data
.raw
;
1784 /* Version (INTEGER) */
1785 if ((count
= asn1_parse(np
, length
, &elem
)) < 0)
1787 if (elem
.type
!= BE_INT
) {
1788 fputs("[version!=INT]", stdout
);
1793 switch (elem
.data
.integer
) {
1794 case SNMP_VERSION_1
:
1795 case SNMP_VERSION_2
:
1796 case SNMP_VERSION_3
:
1798 printf("%s ", SnmpVersion
[elem
.data
.integer
]);
1801 printf("[version = %d]", elem
.data
.integer
);
1804 version
= elem
.data
.integer
;
1809 case SNMP_VERSION_1
:
1810 case SNMP_VERSION_2
:
1811 community_print(np
, length
, version
);
1813 case SNMP_VERSION_3
:
1814 v3msg_print(np
, length
);
1817 printf("[version = %d]", elem
.data
.integer
);