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
59 /* \summary: Simple Network Management Protocol (SNMP) printer */
65 #include "netdissect-stdinc.h"
75 #include "netdissect-ctype.h"
77 #define ND_LONGJMP_FROM_TCHECK
78 #include "netdissect.h"
81 #undef OPAQUE /* defined in <wingdi.h> */
85 * Universal ASN.1 types
86 * (we only care about the tag values for those allowed in the Internet SMI)
88 static const char *Universal
[] = {
101 "U-8","U-9","U-10","U-11", /* 8-11 */
102 "U-12","U-13","U-14","U-15", /* 12-15 */
109 * Application-wide ASN.1 types from the Internet SMI and their tags
111 static const char *Application
[] = {
128 * Context-specific ASN.1 types for the SNMP PDUs and their tags
130 static const char *Context
[] = {
151 #define NOTIFY_CLASS(x) (x == TRAP || x == V2TRAP || x == INFORMREQ)
152 #define READ_CLASS(x) (x == GETREQ || x == GETNEXTREQ || x == GETBULKREQ)
153 #define WRITE_CLASS(x) (x == SETREQ)
154 #define RESPONSE_CLASS(x) (x == GETRESP)
155 #define INTERNAL_CLASS(x) (x == REPORT)
158 * Context-specific ASN.1 types for the SNMP Exceptions and their tags
160 static const char *Exceptions
[] = {
162 #define NOSUCHOBJECT 0
164 #define NOSUCHINSTANCE 1
166 #define ENDOFMIBVIEW 2
170 * Private ASN.1 types
171 * The Internet SMI does not specify any
173 static const char *Private
[] = {
178 * error-status values for any SNMP PDU
180 static const char *ErrorStatus
[] = {
194 "resourceUnavailable",
197 "authorizationError",
201 #define DECODE_ErrorStatus(e) \
202 ( e >= 0 && (size_t)e < sizeof(ErrorStatus)/sizeof(ErrorStatus[0]) \
204 : (snprintf(errbuf, sizeof(errbuf), "err=%u", e), errbuf))
207 * generic-trap values in the SNMP Trap-PDU
209 static const char *GenericTrap
[] = {
214 "authenticationFailure",
217 #define GT_ENTERPRISE 6
219 #define DECODE_GenericTrap(t) \
220 ( t >= 0 && (size_t)t < sizeof(GenericTrap)/sizeof(GenericTrap[0]) \
222 : (snprintf(buf, sizeof(buf), "gt=%d", t), buf))
225 * ASN.1 type class table
226 * Ties together the preceding Universal, Application, Context, and Private
229 #define defineCLASS(x) { "x", x, sizeof(x)/sizeof(x[0]) } /* not ANSI-C */
230 static const struct {
235 defineCLASS(Universal
),
237 defineCLASS(Application
),
238 #define APPLICATION 1
239 defineCLASS(Context
),
241 defineCLASS(Private
),
243 defineCLASS(Exceptions
),
248 * defined forms for ASN.1 types
250 static const char *Form
[] = {
254 #define CONSTRUCTED 1
258 * A structure for the OID tree for the compiled-in MIB.
259 * This is stored as a general-order tree.
262 const char *desc
; /* name of object */
263 u_char oid
; /* sub-id following parent */
264 u_char type
; /* object type (unused) */
265 struct obj
*child
, *next
; /* child and next sibling pointers */
269 * Include the compiled in SNMP MIB. "mib.h" is produced by feeding
270 * RFC-1156 format files into "makemib". "mib.h" MUST define at least
271 * a value for `mibroot'.
273 * In particular, this is gross, as this is including initialized structures,
274 * and by right shouldn't be an "include" file.
279 * This defines a list of OIDs which will be abbreviated on output.
280 * Currently, this includes the prefixes for the Internet MIB, the
281 * private enterprises tree, and the experimental tree.
283 #define OID_FIRST_OCTET(x, y) (((x)*40) + (y)) /* X.690 8.19.4 */
286 static const uint8_t mib_oid
[] = { OID_FIRST_OCTET(1, 3), 6, 1, 2, 1 };
288 #ifndef NO_ABREV_ENTER
289 static const uint8_t enterprises_oid
[] = { OID_FIRST_OCTET(1, 3), 6, 1, 4, 1 };
291 #ifndef NO_ABREV_EXPERI
292 static const uint8_t experimental_oid
[] = { OID_FIRST_OCTET(1, 3), 6, 1, 3 };
294 #ifndef NO_ABBREV_SNMPMODS
295 static const uint8_t snmpModules_oid
[] = { OID_FIRST_OCTET(1, 3), 6, 1, 6, 3 };
298 #define OBJ_ABBREV_ENTRY(prefix, obj) \
299 { prefix, &_ ## obj ## _obj, obj ## _oid, sizeof (obj ## _oid) }
300 static const struct obj_abrev
{
301 const char *prefix
; /* prefix for this abrev */
302 struct obj
*node
; /* pointer into object table */
303 const uint8_t *oid
; /* ASN.1 encoded OID */
304 size_t oid_len
; /* length of OID */
305 } obj_abrev_list
[] = {
307 /* .iso.org.dod.internet.mgmt.mib */
308 OBJ_ABBREV_ENTRY("", mib
),
310 #ifndef NO_ABREV_ENTER
311 /* .iso.org.dod.internet.private.enterprises */
312 OBJ_ABBREV_ENTRY("E:", enterprises
),
314 #ifndef NO_ABREV_EXPERI
315 /* .iso.org.dod.internet.experimental */
316 OBJ_ABBREV_ENTRY("X:", experimental
),
318 #ifndef NO_ABBREV_SNMPMODS
319 /* .iso.org.dod.internet.snmpV2.snmpModules */
320 OBJ_ABBREV_ENTRY("S:", snmpModules
),
326 * This is used in the OID print routine to walk down the object tree
327 * rooted at `mibroot'.
329 #define OBJ_PRINT(o, suppressdot) \
333 if ((o) == objp->oid) \
335 } while ((objp = objp->next) != NULL); \
338 ND_PRINT(suppressdot?"%s":".%s", objp->desc); \
339 objp = objp->child; \
341 ND_PRINT(suppressdot?"%u":".%u", (o)); \
345 * This is the definition for the Any-Data-Type storage used purely for
346 * temporary internal representation while decoding an ASN.1 data stream.
358 u_char form
, class; /* tag info */
369 #define BE_INETADDR 8
372 #define BE_NOSUCHOBJECT 128
373 #define BE_NOSUCHINST 129
374 #define BE_ENDOFMIBVIEW 130
378 * SNMP versions recognized by this module
380 static const char *SnmpVersion
[] = {
382 #define SNMP_VERSION_1 0
384 #define SNMP_VERSION_2 1
386 #define SNMP_VERSION_2U 2
388 #define SNMP_VERSION_3 3
392 * Defaults for SNMP PDU components
394 #define DEF_COMMUNITY "public"
397 * constants for ASN.1 decoding
400 #define ASNLEN_INETADDR 4
403 #define ASN_BIT8 0x80
404 #define ASN_LONGLEN 0x80
406 #define ASN_ID_BITS 0x1f
407 #define ASN_FORM_BITS 0x20
408 #define ASN_FORM_SHIFT 5
409 #define ASN_CLASS_BITS 0xc0
410 #define ASN_CLASS_SHIFT 6
412 #define ASN_ID_EXT 0x1f /* extension ID in tag field */
415 * This decodes the next ASN.1 object in the stream pointed to by "p"
416 * (and of real-length "len") and stores the intermediate data in the
417 * provided BE object.
419 * This returns -l if it fails (i.e., the ASN.1 stream is not valid).
420 * O/w, this returns the number of bytes parsed from "p".
423 asn1_parse(netdissect_options
*ndo
,
424 const u_char
*p
, u_int len
, struct be
*elem
)
426 u_char form
, class, id
;
432 ND_PRINT("[nothing to parse]");
437 * it would be nice to use a bit field, but you can't depend on them.
438 * +---+---+---+---+---+---+---+---+
440 * +---+---+---+---+---+---+---+---+
443 id
= GET_U_1(p
) & ASN_ID_BITS
; /* lower 5 bits, range 00-1f */
445 form
= (GET_U_1(p
) & 0xe0) >> 5; /* move upper 3 bits to lower 3 */
446 class = form
>> 1; /* bits 7&6 -> bits 1&0, range 0-3 */
447 form
&= 0x1; /* bit 5 -> bit 0, range 0-1 */
449 form
= (u_char
)(GET_U_1(p
) & ASN_FORM_BITS
) >> ASN_FORM_SHIFT
;
450 class = (u_char
)(GET_U_1(p
) & ASN_CLASS_BITS
) >> ASN_CLASS_SHIFT
;
456 /* extended tag field */
457 if (id
== ASN_ID_EXT
) {
459 * The ID follows, as a sequence of octets with the
460 * 8th bit set and the remaining 7 bits being
461 * the next 7 bits of the value, terminated with
462 * an octet with the 8th bit not set.
464 * First, assemble all the octets with the 8th
465 * bit set. XXX - this doesn't handle a value
466 * that won't fit in 32 bits.
469 while (GET_U_1(p
) & ASN_BIT8
) {
471 ND_PRINT("[Xtagfield?]");
474 id
= (id
<< 7) | (GET_U_1(p
) & ~ASN_BIT8
);
480 ND_PRINT("[Xtagfield?]");
483 elem
->id
= id
= (id
<< 7) | GET_U_1(p
);
489 ND_PRINT("[no asnlen]");
492 elem
->asnlen
= GET_U_1(p
);
494 if (elem
->asnlen
& ASN_BIT8
) {
495 uint32_t noct
= elem
->asnlen
% ASN_BIT8
;
498 ND_PRINT("[asnlen? %d<%d]", len
, noct
);
501 for (; noct
!= 0; len
--, hdr
++, noct
--) {
502 elem
->asnlen
= (elem
->asnlen
<< ASN_SHIFT8
) | GET_U_1(p
);
506 if (len
< elem
->asnlen
) {
507 ND_PRINT("[len%d<asnlen%u]", len
, elem
->asnlen
);
510 if (form
>= sizeof(Form
)/sizeof(Form
[0])) {
511 ND_PRINT("[form?%d]", form
);
514 if (class >= sizeof(Class
)/sizeof(Class
[0])) {
515 ND_PRINT("[class?%c/%d]", *Form
[form
], class);
518 if ((int)id
>= Class
[class].numIDs
) {
519 ND_PRINT("[id?%c/%s/%d]", *Form
[form
], Class
[class].name
, id
);
522 ND_TCHECK_LEN(p
, elem
->asnlen
);
539 if (elem
->asnlen
== 0) {
540 ND_PRINT("[asnlen=0]");
543 if (GET_U_1(p
) & ASN_BIT8
) /* negative */
545 for (i
= elem
->asnlen
; i
!= 0; p
++, i
--)
546 data
= (data
<< ASN_SHIFT8
) | GET_U_1(p
);
547 elem
->data
.integer
= data
;
553 elem
->data
.raw
= (const uint8_t *)p
;
557 elem
->type
= BE_NULL
;
558 elem
->data
.raw
= NULL
;
562 elem
->type
= BE_OCTET
;
563 elem
->data
.raw
= (const uint8_t *)p
;
564 ND_PRINT("[P/U/%s]", Class
[class].Id
[id
]);
572 elem
->type
= BE_INETADDR
;
573 elem
->data
.raw
= (const uint8_t *)p
;
582 for (i
= elem
->asnlen
; i
!= 0; p
++, i
--)
583 data
= (data
<< 8) + GET_U_1(p
);
584 elem
->data
.uns
= data
;
590 elem
->type
= BE_UNS64
;
592 for (i
= elem
->asnlen
; i
!= 0; p
++, i
--)
593 data64
= (data64
<< 8) + GET_U_1(p
);
594 elem
->data
.uns64
= data64
;
599 elem
->type
= BE_OCTET
;
600 elem
->data
.raw
= (const uint8_t *)p
;
602 Class
[class].Id
[id
]);
610 elem
->type
= BE_NOSUCHOBJECT
;
611 elem
->data
.raw
= NULL
;
615 elem
->type
= BE_NOSUCHINST
;
616 elem
->data
.raw
= NULL
;
620 elem
->type
= BE_ENDOFMIBVIEW
;
621 elem
->data
.raw
= NULL
;
627 ND_PRINT("[P/%s/%s]", Class
[class].name
, Class
[class].Id
[id
]);
628 elem
->type
= BE_OCTET
;
629 elem
->data
.raw
= (const uint8_t *)p
;
640 elem
->data
.raw
= (const uint8_t *)p
;
644 elem
->type
= BE_OCTET
;
645 elem
->data
.raw
= (const uint8_t *)p
;
646 ND_PRINT("C/U/%s", Class
[class].Id
[id
]);
653 elem
->data
.raw
= (const uint8_t *)p
;
657 elem
->type
= BE_OCTET
;
658 elem
->data
.raw
= (const uint8_t *)p
;
659 ND_PRINT("C/%s/%s", Class
[class].name
, Class
[class].Id
[id
]);
666 return elem
->asnlen
+ hdr
;
673 asn1_print_octets(netdissect_options
*ndo
, struct be
*elem
)
675 const u_char
*p
= (const u_char
*)elem
->data
.raw
;
676 uint32_t asnlen
= elem
->asnlen
;
679 for (i
= asnlen
; i
!= 0; p
++, i
--)
680 ND_PRINT("_%.2x", GET_U_1(p
));
684 asn1_print_string(netdissect_options
*ndo
, struct be
*elem
)
686 int printable
= 1, first
= 1;
688 uint32_t asnlen
= elem
->asnlen
;
692 for (i
= asnlen
; printable
&& i
!= 0; p
++, i
--)
693 printable
= ND_ASCII_ISPRINT(GET_U_1(p
));
697 nd_printjn(ndo
, p
, asnlen
);
700 for (i
= asnlen
; i
!= 0; p
++, i
--) {
701 ND_PRINT(first
? "%.2x" : "_%.2x", GET_U_1(p
));
708 * Display the ASN.1 object represented by the BE object.
709 * This used to be an integral part of asn1_parse() before the intermediate
713 asn1_print(netdissect_options
*ndo
,
717 uint32_t asnlen
= elem
->asnlen
;
720 switch (elem
->type
) {
723 asn1_print_octets(ndo
, elem
);
733 p
= (const u_char
*)elem
->data
.raw
;
735 if (!ndo
->ndo_nflag
&& asnlen
> 2) {
736 const struct obj_abrev
*a
= &obj_abrev_list
[0];
737 for (; a
->node
; a
++) {
740 ND_TCHECK_LEN(p
, a
->oid_len
);
741 if (memcmp(a
->oid
, p
, a
->oid_len
) == 0) {
742 objp
= a
->node
->child
;
745 ND_PRINT("%s", a
->prefix
);
752 for (; i
!= 0; p
++, i
--) {
753 o
= (o
<< ASN_SHIFT7
) + (GET_U_1(p
) & ~ASN_BIT8
);
754 if (GET_U_1(p
) & ASN_LONGLEN
)
758 * first subitem encodes two items with
760 * (see X.690:1997 clause 8.19 for the details)
781 ND_PRINT("%d", elem
->data
.integer
);
785 ND_PRINT("%u", elem
->data
.uns
);
789 ND_PRINT("%" PRIu64
, elem
->data
.uns64
);
793 asn1_print_string(ndo
, elem
);
797 ND_PRINT("Seq(%u)", elem
->asnlen
);
801 if (asnlen
!= ASNLEN_INETADDR
)
802 ND_PRINT("[inetaddr len!=%d]", ASNLEN_INETADDR
);
803 p
= (const u_char
*)elem
->data
.raw
;
804 for (i
= asnlen
; i
!= 0; p
++, i
--) {
805 ND_PRINT((i
== asnlen
) ? "%u" : ".%u", GET_U_1(p
));
809 case BE_NOSUCHOBJECT
:
811 case BE_ENDOFMIBVIEW
:
812 ND_PRINT("[%s]", Class
[EXCEPTIONS
].Id
[elem
->id
]);
816 ND_PRINT("%s(%u)", Class
[CONTEXT
].Id
[elem
->id
], elem
->asnlen
);
820 ND_PRINT("[BE_ANY!?]");
827 /* This function now always returns 0. Don't make it void yet, as other
828 * code checks for negative result and this function might need to signal
829 * invalid data later.
836 * This is a brute force ASN.1 printer: recurses to dump an entire structure.
837 * This will work for any ASN.1 stream, not just an SNMP PDU.
839 * By adding newlines and spaces at the correct places, this would print in
842 * This is not currently used.
845 asn1_decode(u_char
*p
, u_int length
)
850 while (i
>= 0 && length
> 0) {
851 i
= asn1_parse(ndo
, p
, length
, &elem
);
854 if (asn1_print(ndo
, &elem
) < 0)
856 if (elem
.type
== BE_SEQ
|| elem
.type
== BE_PDU
) {
858 asn1_decode(elem
.data
.raw
, elem
.asnlen
);
871 SmiBasetype basetype
;
875 static const struct smi2be smi2betab
[] = {
876 { SMI_BASETYPE_INTEGER32
, BE_INT
},
877 { SMI_BASETYPE_OCTETSTRING
, BE_STR
},
878 { SMI_BASETYPE_OCTETSTRING
, BE_INETADDR
},
879 { SMI_BASETYPE_OBJECTIDENTIFIER
, BE_OID
},
880 { SMI_BASETYPE_UNSIGNED32
, BE_UNS
},
881 { SMI_BASETYPE_INTEGER64
, BE_NONE
},
882 { SMI_BASETYPE_UNSIGNED64
, BE_UNS64
},
883 { SMI_BASETYPE_FLOAT32
, BE_NONE
},
884 { SMI_BASETYPE_FLOAT64
, BE_NONE
},
885 { SMI_BASETYPE_FLOAT128
, BE_NONE
},
886 { SMI_BASETYPE_ENUM
, BE_INT
},
887 { SMI_BASETYPE_BITS
, BE_STR
},
888 { SMI_BASETYPE_UNKNOWN
, BE_NONE
}
892 smi_decode_oid(netdissect_options
*ndo
,
893 struct be
*elem
, unsigned int *oid
,
894 unsigned int oidsize
, unsigned int *oidlen
)
896 const u_char
*p
= (const u_char
*)elem
->data
.raw
;
897 uint32_t asnlen
= elem
->asnlen
;
899 int o
= 0, first
= -1;
900 unsigned int firstval
;
902 for (*oidlen
= 0; i
!= 0; p
++, i
--) {
903 o
= (o
<< ASN_SHIFT7
) + (GET_U_1(p
) & ~ASN_BIT8
);
904 if (GET_U_1(p
) & ASN_LONGLEN
)
908 * first subitem encodes two items with 1st*OIDMUX+2nd
909 * (see X.690:1997 clause 8.19 for the details)
913 firstval
= o
/ OIDMUX
;
914 if (firstval
> 2) firstval
= 2;
915 o
-= firstval
* OIDMUX
;
916 if (*oidlen
< oidsize
) {
917 oid
[(*oidlen
)++] = firstval
;
920 if (*oidlen
< oidsize
) {
921 oid
[(*oidlen
)++] = o
;
928 static int smi_check_type(SmiBasetype basetype
, int be
)
932 for (i
= 0; smi2betab
[i
].basetype
!= SMI_BASETYPE_UNKNOWN
; i
++) {
933 if (smi2betab
[i
].basetype
== basetype
&& smi2betab
[i
].be
== be
) {
941 static int smi_check_a_range(SmiType
*smiType
, SmiRange
*smiRange
,
946 switch (smiType
->basetype
) {
947 case SMI_BASETYPE_OBJECTIDENTIFIER
:
948 case SMI_BASETYPE_OCTETSTRING
:
949 if (smiRange
->minValue
.value
.unsigned32
950 == smiRange
->maxValue
.value
.unsigned32
) {
951 ok
= (elem
->asnlen
== smiRange
->minValue
.value
.unsigned32
);
953 ok
= (elem
->asnlen
>= smiRange
->minValue
.value
.unsigned32
954 && elem
->asnlen
<= smiRange
->maxValue
.value
.unsigned32
);
958 case SMI_BASETYPE_INTEGER32
:
959 ok
= (elem
->data
.integer
>= smiRange
->minValue
.value
.integer32
960 && elem
->data
.integer
<= smiRange
->maxValue
.value
.integer32
);
963 case SMI_BASETYPE_UNSIGNED32
:
964 ok
= (elem
->data
.uns
>= smiRange
->minValue
.value
.unsigned32
965 && elem
->data
.uns
<= smiRange
->maxValue
.value
.unsigned32
);
968 case SMI_BASETYPE_UNSIGNED64
:
972 /* case SMI_BASETYPE_INTEGER64: SMIng */
973 /* case SMI_BASETYPE_FLOAT32: SMIng */
974 /* case SMI_BASETYPE_FLOAT64: SMIng */
975 /* case SMI_BASETYPE_FLOAT128: SMIng */
977 case SMI_BASETYPE_ENUM
:
978 case SMI_BASETYPE_BITS
:
979 case SMI_BASETYPE_UNKNOWN
:
991 static int smi_check_range(SmiType
*smiType
, struct be
*elem
)
996 for (smiRange
= smiGetFirstRange(smiType
);
998 smiRange
= smiGetNextRange(smiRange
)) {
1000 ok
= smi_check_a_range(smiType
, smiRange
, elem
);
1008 SmiType
*parentType
;
1009 parentType
= smiGetParentType(smiType
);
1011 ok
= smi_check_range(parentType
, elem
);
1019 smi_print_variable(netdissect_options
*ndo
,
1020 struct be
*elem
, int *status
)
1022 unsigned int oid
[128], oidlen
;
1023 SmiNode
*smiNode
= NULL
;
1026 if (!nd_smi_module_loaded
) {
1027 *status
= asn1_print(ndo
, elem
);
1030 *status
= smi_decode_oid(ndo
, elem
, oid
, sizeof(oid
) / sizeof(unsigned int),
1034 smiNode
= smiGetNodeByOID(oidlen
, oid
);
1036 *status
= asn1_print(ndo
, elem
);
1039 if (ndo
->ndo_vflag
) {
1040 ND_PRINT("%s::", smiGetNodeModule(smiNode
)->name
);
1042 ND_PRINT("%s", smiNode
->name
);
1043 if (smiNode
->oidlen
< oidlen
) {
1044 for (i
= smiNode
->oidlen
; i
< oidlen
; i
++) {
1045 ND_PRINT(".%u", oid
[i
]);
1053 smi_print_value(netdissect_options
*ndo
,
1054 SmiNode
*smiNode
, u_short 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(ndo
, elem
);
1066 if (elem
->type
== BE_NOSUCHOBJECT
1067 || elem
->type
== BE_NOSUCHINST
1068 || elem
->type
== BE_ENDOFMIBVIEW
) {
1069 return asn1_print(ndo
, elem
);
1072 if (NOTIFY_CLASS(pduid
) && smiNode
->access
< SMI_ACCESS_NOTIFY
) {
1073 ND_PRINT("[notNotifyable]");
1076 if (READ_CLASS(pduid
) && smiNode
->access
< SMI_ACCESS_READ_ONLY
) {
1077 ND_PRINT("[notReadable]");
1080 if (WRITE_CLASS(pduid
) && smiNode
->access
< SMI_ACCESS_READ_WRITE
) {
1081 ND_PRINT("[notWritable]");
1084 if (RESPONSE_CLASS(pduid
)
1085 && smiNode
->access
== SMI_ACCESS_NOT_ACCESSIBLE
) {
1086 ND_PRINT("[noAccess]");
1089 smiType
= smiGetNodeType(smiNode
);
1091 return asn1_print(ndo
, elem
);
1094 if (! smi_check_type(smiType
->basetype
, elem
->type
)) {
1095 ND_PRINT("[wrongType]");
1098 if (! smi_check_range(smiType
, elem
)) {
1099 ND_PRINT("[outOfRange]");
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 if (nd_smi_module_loaded
&&
1116 smi_decode_oid(ndo
, elem
, oid
,
1117 sizeof(oid
)/sizeof(unsigned int),
1119 smiNode
= smiGetNodeByOID(oidlen
, oid
);
1121 if (ndo
->ndo_vflag
) {
1122 ND_PRINT("%s::", smiGetNodeModule(smiNode
)->name
);
1124 ND_PRINT("%s", smiNode
->name
);
1125 if (smiNode
->oidlen
< oidlen
) {
1126 for (i
= smiNode
->oidlen
;
1128 ND_PRINT(".%u", oid
[i
]);
1138 if (smiType
->basetype
== SMI_BASETYPE_ENUM
) {
1139 for (nn
= smiGetFirstNamedNumber(smiType
);
1141 nn
= smiGetNextNamedNumber(nn
)) {
1142 if (nn
->value
.value
.integer32
1143 == elem
->data
.integer
) {
1144 ND_PRINT("%s", nn
->name
);
1145 ND_PRINT("(%d)", elem
->data
.integer
);
1155 return asn1_print(ndo
, elem
);
1162 * General SNMP header
1164 * version INTEGER {version-1(0)},
1165 * community OCTET STRING,
1168 * PDUs for all but Trap: (see rfc1157 from page 15 on)
1170 * request-id INTEGER,
1171 * error-status INTEGER,
1172 * error-index INTEGER,
1173 * varbindlist SEQUENCE OF
1181 * enterprise OBJECT IDENTIFIER,
1182 * agent-addr NetworkAddress,
1183 * generic-trap INTEGER,
1184 * specific-trap INTEGER,
1185 * time-stamp TimeTicks,
1186 * varbindlist SEQUENCE OF
1195 * Decode SNMP varBind
1198 varbind_print(netdissect_options
*ndo
,
1199 u_short pduid
, const u_char
*np
, u_int length
)
1204 SmiNode
*smiNode
= NULL
;
1208 /* Sequence of varBind */
1209 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1211 if (elem
.type
!= BE_SEQ
) {
1212 ND_PRINT("[!SEQ of varbind]");
1213 asn1_print(ndo
, &elem
);
1216 if ((u_int
)count
< length
)
1217 ND_PRINT("[%d extra after SEQ of varbind]", length
- count
);
1219 length
= elem
.asnlen
;
1220 np
= (const u_char
*)elem
.data
.raw
;
1223 const u_char
*vbend
;
1229 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1231 if (elem
.type
!= BE_SEQ
) {
1232 ND_PRINT("[!varbind]");
1233 asn1_print(ndo
, &elem
);
1237 vblength
= length
- count
;
1239 length
= elem
.asnlen
;
1240 np
= (const u_char
*)elem
.data
.raw
;
1243 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1245 if (elem
.type
!= BE_OID
) {
1246 ND_PRINT("[objName!=OID]");
1247 asn1_print(ndo
, &elem
);
1251 smiNode
= smi_print_variable(ndo
, &elem
, &status
);
1253 status
= asn1_print(ndo
, &elem
);
1260 if (pduid
!= GETREQ
&& pduid
!= GETNEXTREQ
1261 && pduid
!= GETBULKREQ
)
1265 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1267 if (pduid
== GETREQ
|| pduid
== GETNEXTREQ
1268 || pduid
== GETBULKREQ
) {
1269 if (elem
.type
!= BE_NULL
) {
1270 ND_PRINT("[objVal!=NULL]");
1271 if (asn1_print(ndo
, &elem
) < 0)
1275 if (elem
.type
!= BE_NULL
) {
1277 status
= smi_print_value(ndo
, smiNode
, pduid
, &elem
);
1279 status
= asn1_print(ndo
, &elem
);
1291 * Decode SNMP PDUs: GetRequest, GetNextRequest, GetResponse, SetRequest,
1292 * GetBulk, Inform, V2Trap, and Report
1295 snmppdu_print(netdissect_options
*ndo
,
1296 u_short pduid
, const u_char
*np
, u_int length
)
1299 int count
= 0, error_status
;
1301 /* reqId (Integer) */
1302 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1304 if (elem
.type
!= BE_INT
) {
1305 ND_PRINT("[reqId!=INT]");
1306 asn1_print(ndo
, &elem
);
1310 ND_PRINT("R=%d ", elem
.data
.integer
);
1314 /* errorStatus (Integer) */
1315 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1317 if (elem
.type
!= BE_INT
) {
1318 ND_PRINT("[errorStatus!=INT]");
1319 asn1_print(ndo
, &elem
);
1323 if ((pduid
== GETREQ
|| pduid
== GETNEXTREQ
|| pduid
== SETREQ
1324 || pduid
== INFORMREQ
|| pduid
== V2TRAP
|| pduid
== REPORT
)
1325 && elem
.data
.integer
!= 0) {
1327 ND_PRINT("[errorStatus(%s)!=0]",
1328 DECODE_ErrorStatus(elem
.data
.integer
));
1329 } else if (pduid
== GETBULKREQ
) {
1330 ND_PRINT(" N=%d", elem
.data
.integer
);
1331 } else if (elem
.data
.integer
!= 0) {
1333 ND_PRINT(" %s", DECODE_ErrorStatus(elem
.data
.integer
));
1334 error_status
= elem
.data
.integer
;
1339 /* errorIndex (Integer) */
1340 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1342 if (elem
.type
!= BE_INT
) {
1343 ND_PRINT("[errorIndex!=INT]");
1344 asn1_print(ndo
, &elem
);
1347 if ((pduid
== GETREQ
|| pduid
== GETNEXTREQ
|| pduid
== SETREQ
1348 || pduid
== INFORMREQ
|| pduid
== V2TRAP
|| pduid
== REPORT
)
1349 && elem
.data
.integer
!= 0)
1350 ND_PRINT("[errorIndex(%d)!=0]", elem
.data
.integer
);
1351 else if (pduid
== GETBULKREQ
)
1352 ND_PRINT(" M=%d", elem
.data
.integer
);
1353 else if (elem
.data
.integer
!= 0) {
1355 ND_PRINT("[errorIndex(%d) w/o errorStatus]", elem
.data
.integer
);
1357 ND_PRINT("@%d", elem
.data
.integer
);
1358 } else if (error_status
) {
1359 ND_PRINT("[errorIndex==0]");
1364 varbind_print(ndo
, pduid
, np
, length
);
1368 * Decode SNMP Trap PDU
1371 trappdu_print(netdissect_options
*ndo
,
1372 const u_char
*np
, u_int length
)
1375 int count
= 0, generic
;
1379 /* enterprise (oid) */
1380 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1382 if (elem
.type
!= BE_OID
) {
1383 ND_PRINT("[enterprise!=OID]");
1384 asn1_print(ndo
, &elem
);
1387 if (asn1_print(ndo
, &elem
) < 0)
1394 /* agent-addr (inetaddr) */
1395 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1397 if (elem
.type
!= BE_INETADDR
) {
1398 ND_PRINT("[agent-addr!=INETADDR]");
1399 asn1_print(ndo
, &elem
);
1402 if (asn1_print(ndo
, &elem
) < 0)
1407 /* generic-trap (Integer) */
1408 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1410 if (elem
.type
!= BE_INT
) {
1411 ND_PRINT("[generic-trap!=INT]");
1412 asn1_print(ndo
, &elem
);
1415 generic
= elem
.data
.integer
;
1418 ND_PRINT(" %s", DECODE_GenericTrap(generic
));
1423 /* specific-trap (Integer) */
1424 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1426 if (elem
.type
!= BE_INT
) {
1427 ND_PRINT("[specific-trap!=INT]");
1428 asn1_print(ndo
, &elem
);
1431 if (generic
!= GT_ENTERPRISE
) {
1432 if (elem
.data
.integer
!= 0)
1433 ND_PRINT("[specific-trap(%d)!=0]", elem
.data
.integer
);
1435 ND_PRINT(" s=%d", elem
.data
.integer
);
1441 /* time-stamp (TimeTicks) */
1442 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1444 if (elem
.type
!= BE_UNS
) { /* XXX */
1445 ND_PRINT("[time-stamp!=TIMETICKS]");
1446 asn1_print(ndo
, &elem
);
1449 if (asn1_print(ndo
, &elem
) < 0)
1454 varbind_print(ndo
, TRAP
, np
, length
);
1458 * Decode arbitrary SNMP PDUs.
1461 pdu_print(netdissect_options
*ndo
,
1462 const u_char
*np
, u_int length
, int version
)
1468 if ((count
= asn1_parse(ndo
, np
, length
, &pdu
)) < 0)
1470 if (pdu
.type
!= BE_PDU
) {
1471 ND_PRINT("[no PDU]");
1474 if ((u_int
)count
< length
)
1475 ND_PRINT("[%d extra after PDU]", length
- count
);
1476 if (ndo
->ndo_vflag
) {
1479 if (asn1_print(ndo
, &pdu
) < 0)
1482 /* descend into PDU */
1483 length
= pdu
.asnlen
;
1484 np
= (const 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 ND_PRINT("[v2 PDU in v1 message]");
1493 if (version
== SNMP_VERSION_2
&& pdu
.id
== TRAP
) {
1494 ND_PRINT("[v1 PDU in v2 message]");
1500 trappdu_print(ndo
, np
, length
);
1510 snmppdu_print(ndo
, pdu
.id
, np
, length
);
1514 if (ndo
->ndo_vflag
) {
1520 * Decode a scoped SNMP PDU.
1523 scopedpdu_print(netdissect_options
*ndo
,
1524 const u_char
*np
, u_int length
, int version
)
1530 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1532 if (elem
.type
!= BE_SEQ
) {
1533 ND_PRINT("[!scoped PDU]");
1534 asn1_print(ndo
, &elem
);
1537 length
= elem
.asnlen
;
1538 np
= (const u_char
*)elem
.data
.raw
;
1540 /* contextEngineID (OCTET STRING) */
1541 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1543 if (elem
.type
!= BE_STR
) {
1544 ND_PRINT("[contextEngineID!=STR]");
1545 asn1_print(ndo
, &elem
);
1552 asn1_print_octets(ndo
, &elem
);
1555 /* contextName (OCTET STRING) */
1556 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1558 if (elem
.type
!= BE_STR
) {
1559 ND_PRINT("[contextName!=STR]");
1560 asn1_print(ndo
, &elem
);
1567 asn1_print_string(ndo
, &elem
);
1570 pdu_print(ndo
, np
, length
, version
);
1574 * Decode SNMP Community Header (SNMPv1 and SNMPv2c)
1577 community_print(netdissect_options
*ndo
,
1578 const u_char
*np
, u_int length
, int version
)
1583 /* Community (String) */
1584 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1586 if (elem
.type
!= BE_STR
) {
1587 ND_PRINT("[comm!=STR]");
1588 asn1_print(ndo
, &elem
);
1591 /* default community */
1592 if (!(elem
.asnlen
== sizeof(DEF_COMMUNITY
) - 1 &&
1593 strncmp((const char *)elem
.data
.str
, DEF_COMMUNITY
,
1594 sizeof(DEF_COMMUNITY
) - 1) == 0)) {
1597 asn1_print_string(ndo
, &elem
);
1603 pdu_print(ndo
, np
, length
, version
);
1607 * Decode SNMPv3 User-based Security Message Header (SNMPv3)
1610 usm_print(netdissect_options
*ndo
,
1611 const u_char
*np
, u_int length
)
1617 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1619 if (elem
.type
!= BE_SEQ
) {
1621 asn1_print(ndo
, &elem
);
1624 length
= elem
.asnlen
;
1625 np
= (const u_char
*)elem
.data
.raw
;
1627 /* msgAuthoritativeEngineID (OCTET STRING) */
1628 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1630 if (elem
.type
!= BE_STR
) {
1631 ND_PRINT("[msgAuthoritativeEngineID!=STR]");
1632 asn1_print(ndo
, &elem
);
1638 /* msgAuthoritativeEngineBoots (INTEGER) */
1639 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1641 if (elem
.type
!= BE_INT
) {
1642 ND_PRINT("[msgAuthoritativeEngineBoots!=INT]");
1643 asn1_print(ndo
, &elem
);
1647 ND_PRINT("B=%d ", elem
.data
.integer
);
1651 /* msgAuthoritativeEngineTime (INTEGER) */
1652 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1654 if (elem
.type
!= BE_INT
) {
1655 ND_PRINT("[msgAuthoritativeEngineTime!=INT]");
1656 asn1_print(ndo
, &elem
);
1660 ND_PRINT("T=%d ", elem
.data
.integer
);
1664 /* msgUserName (OCTET STRING) */
1665 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1667 if (elem
.type
!= BE_STR
) {
1668 ND_PRINT("[msgUserName!=STR]");
1669 asn1_print(ndo
, &elem
);
1676 asn1_print_string(ndo
, &elem
);
1679 /* msgAuthenticationParameters (OCTET STRING) */
1680 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1682 if (elem
.type
!= BE_STR
) {
1683 ND_PRINT("[msgAuthenticationParameters!=STR]");
1684 asn1_print(ndo
, &elem
);
1690 /* msgPrivacyParameters (OCTET STRING) */
1691 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1693 if (elem
.type
!= BE_STR
) {
1694 ND_PRINT("[msgPrivacyParameters!=STR]");
1695 asn1_print(ndo
, &elem
);
1701 if ((u_int
)count
< length
)
1702 ND_PRINT("[%d extra after usm SEQ]", length
- count
);
1706 * Decode SNMPv3 Message Header (SNMPv3)
1709 v3msg_print(netdissect_options
*ndo
,
1710 const u_char
*np
, u_int length
)
1716 const u_char
*xnp
= np
;
1717 int xlength
= length
;
1720 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1722 if (elem
.type
!= BE_SEQ
) {
1723 ND_PRINT("[!message]");
1724 asn1_print(ndo
, &elem
);
1727 length
= elem
.asnlen
;
1728 np
= (const u_char
*)elem
.data
.raw
;
1730 if (ndo
->ndo_vflag
) {
1734 /* msgID (INTEGER) */
1735 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1737 if (elem
.type
!= BE_INT
) {
1738 ND_PRINT("[msgID!=INT]");
1739 asn1_print(ndo
, &elem
);
1745 /* msgMaxSize (INTEGER) */
1746 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1748 if (elem
.type
!= BE_INT
) {
1749 ND_PRINT("[msgMaxSize!=INT]");
1750 asn1_print(ndo
, &elem
);
1756 /* msgFlags (OCTET STRING) */
1757 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1759 if (elem
.type
!= BE_STR
) {
1760 ND_PRINT("[msgFlags!=STR]");
1761 asn1_print(ndo
, &elem
);
1764 if (elem
.asnlen
!= 1) {
1765 ND_PRINT("[msgFlags size %d]", elem
.asnlen
);
1768 flags
= GET_U_1(elem
.data
.str
);
1769 if (flags
!= 0x00 && flags
!= 0x01 && flags
!= 0x03
1770 && flags
!= 0x04 && flags
!= 0x05 && flags
!= 0x07) {
1771 ND_PRINT("[msgFlags=0x%02X]", flags
);
1777 ND_PRINT("F=%s%s%s ",
1778 flags
& 0x01 ? "a" : "",
1779 flags
& 0x02 ? "p" : "",
1780 flags
& 0x04 ? "r" : "");
1782 /* msgSecurityModel (INTEGER) */
1783 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1785 if (elem
.type
!= BE_INT
) {
1786 ND_PRINT("[msgSecurityModel!=INT]");
1787 asn1_print(ndo
, &elem
);
1790 model
= elem
.data
.integer
;
1794 if ((u_int
)count
< length
)
1795 ND_PRINT("[%d extra after message SEQ]", length
- count
);
1797 if (ndo
->ndo_vflag
) {
1802 if (ndo
->ndo_vflag
) {
1806 ND_PRINT("[security model %d]", model
);
1810 np
= xnp
+ (np
- xnp
);
1811 length
= xlength
- (np
- xnp
);
1813 /* msgSecurityParameters (OCTET STRING) */
1814 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1816 if (elem
.type
!= BE_STR
) {
1817 ND_PRINT("[msgSecurityParameters!=STR]");
1818 asn1_print(ndo
, &elem
);
1825 usm_print(ndo
, elem
.data
.str
, elem
.asnlen
);
1826 if (ndo
->ndo_vflag
) {
1831 if (ndo
->ndo_vflag
) {
1832 ND_PRINT("{ ScopedPDU ");
1835 scopedpdu_print(ndo
, np
, length
, 3);
1837 if (ndo
->ndo_vflag
) {
1843 * Decode SNMP header and pass on to PDU printing routines
1846 snmp_print(netdissect_options
*ndo
,
1847 const u_char
*np
, u_int length
)
1853 ndo
->ndo_protocol
= "snmp";
1856 /* initial Sequence */
1857 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1859 if (elem
.type
!= BE_SEQ
) {
1860 ND_PRINT("[!init SEQ]");
1861 asn1_print(ndo
, &elem
);
1864 if ((u_int
)count
< length
)
1865 ND_PRINT("[%d extra after iSEQ]", length
- count
);
1867 length
= elem
.asnlen
;
1868 np
= (const u_char
*)elem
.data
.raw
;
1870 /* Version (INTEGER) */
1871 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1873 if (elem
.type
!= BE_INT
) {
1874 ND_PRINT("[version!=INT]");
1875 asn1_print(ndo
, &elem
);
1879 switch (elem
.data
.integer
) {
1880 case SNMP_VERSION_1
:
1881 case SNMP_VERSION_2
:
1882 case SNMP_VERSION_3
:
1884 ND_PRINT("{ %s ", SnmpVersion
[elem
.data
.integer
]);
1887 ND_PRINT("SNMP [version = %d]", elem
.data
.integer
);
1890 version
= elem
.data
.integer
;
1895 case SNMP_VERSION_1
:
1896 case SNMP_VERSION_2
:
1897 community_print(ndo
, np
, length
, version
);
1899 case SNMP_VERSION_3
:
1900 v3msg_print(ndo
, np
, length
);
1903 ND_PRINT("[version = %d]", elem
.data
.integer
);
1907 if (ndo
->ndo_vflag
) {