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"
74 #include "netdissect.h"
77 #undef OPAQUE /* defined in <wingdi.h> */
81 * Universal ASN.1 types
82 * (we only care about the tag values for those allowed in the Internet SMI)
84 static const char *Universal
[] = {
97 "U-8","U-9","U-10","U-11", /* 8-11 */
98 "U-12","U-13","U-14","U-15", /* 12-15 */
105 * Application-wide ASN.1 types from the Internet SMI and their tags
107 static const char *Application
[] = {
124 * Context-specific ASN.1 types for the SNMP PDUs and their tags
126 static const char *Context
[] = {
147 #define NOTIFY_CLASS(x) (x == TRAP || x == V2TRAP || x == INFORMREQ)
148 #define READ_CLASS(x) (x == GETREQ || x == GETNEXTREQ || x == GETBULKREQ)
149 #define WRITE_CLASS(x) (x == SETREQ)
150 #define RESPONSE_CLASS(x) (x == GETRESP)
151 #define INTERNAL_CLASS(x) (x == REPORT)
154 * Context-specific ASN.1 types for the SNMP Exceptions and their tags
156 static const char *Exceptions
[] = {
158 #define NOSUCHOBJECT 0
160 #define NOSUCHINSTANCE 1
162 #define ENDOFMIBVIEW 2
166 * Private ASN.1 types
167 * The Internet SMI does not specify any
169 static const char *Private
[] = {
174 * error-status values for any SNMP PDU
176 static const char *ErrorStatus
[] = {
190 "resourceUnavailable",
193 "authorizationError",
197 #define DECODE_ErrorStatus(e) \
198 ( e >= 0 && (size_t)e < sizeof(ErrorStatus)/sizeof(ErrorStatus[0]) \
200 : (nd_snprintf(errbuf, sizeof(errbuf), "err=%u", e), errbuf))
203 * generic-trap values in the SNMP Trap-PDU
205 static const char *GenericTrap
[] = {
210 "authenticationFailure",
213 #define GT_ENTERPRISE 6
215 #define DECODE_GenericTrap(t) \
216 ( t >= 0 && (size_t)t < sizeof(GenericTrap)/sizeof(GenericTrap[0]) \
218 : (nd_snprintf(buf, sizeof(buf), "gt=%d", t), buf))
221 * ASN.1 type class table
222 * Ties together the preceding Universal, Application, Context, and Private
225 #define defineCLASS(x) { "x", x, sizeof(x)/sizeof(x[0]) } /* not ANSI-C */
226 static const struct {
231 defineCLASS(Universal
),
233 defineCLASS(Application
),
234 #define APPLICATION 1
235 defineCLASS(Context
),
237 defineCLASS(Private
),
239 defineCLASS(Exceptions
),
244 * defined forms for ASN.1 types
246 static const char *Form
[] = {
250 #define CONSTRUCTED 1
254 * A structure for the OID tree for the compiled-in MIB.
255 * This is stored as a general-order tree.
258 const char *desc
; /* name of object */
259 u_char oid
; /* sub-id following parent */
260 u_char type
; /* object type (unused) */
261 struct obj
*child
, *next
; /* child and next sibling pointers */
265 * Include the compiled in SNMP MIB. "mib.h" is produced by feeding
266 * RFC-1156 format files into "makemib". "mib.h" MUST define at least
267 * a value for `mibroot'.
269 * In particular, this is gross, as this is including initialized structures,
270 * and by right shouldn't be an "include" file.
275 * This defines a list of OIDs which will be abbreviated on output.
276 * Currently, this includes the prefixes for the Internet MIB, the
277 * private enterprises tree, and the experimental tree.
279 #define OID_FIRST_OCTET(x, y) (((x)*40) + (y)) /* X.690 8.19.4 */
282 static const uint8_t mib_oid
[] = { OID_FIRST_OCTET(1, 3), 6, 1, 2, 1 };
284 #ifndef NO_ABREV_ENTER
285 static const uint8_t enterprises_oid
[] = { OID_FIRST_OCTET(1, 3), 6, 1, 4, 1 };
287 #ifndef NO_ABREV_EXPERI
288 static const uint8_t experimental_oid
[] = { OID_FIRST_OCTET(1, 3), 6, 1, 3 };
290 #ifndef NO_ABBREV_SNMPMODS
291 static const uint8_t snmpModules_oid
[] = { OID_FIRST_OCTET(1, 3), 6, 1, 6, 3 };
294 #define OBJ_ABBREV_ENTRY(prefix, obj) \
295 { prefix, &_ ## obj ## _obj, obj ## _oid, sizeof (obj ## _oid) }
296 static const struct obj_abrev
{
297 const char *prefix
; /* prefix for this abrev */
298 struct obj
*node
; /* pointer into object table */
299 const uint8_t *oid
; /* ASN.1 encoded OID */
300 size_t oid_len
; /* length of OID */
301 } obj_abrev_list
[] = {
303 /* .iso.org.dod.internet.mgmt.mib */
304 OBJ_ABBREV_ENTRY("", mib
),
306 #ifndef NO_ABREV_ENTER
307 /* .iso.org.dod.internet.private.enterprises */
308 OBJ_ABBREV_ENTRY("E:", enterprises
),
310 #ifndef NO_ABREV_EXPERI
311 /* .iso.org.dod.internet.experimental */
312 OBJ_ABBREV_ENTRY("X:", experimental
),
314 #ifndef NO_ABBREV_SNMPMODS
315 /* .iso.org.dod.internet.snmpV2.snmpModules */
316 OBJ_ABBREV_ENTRY("S:", snmpModules
),
322 * This is used in the OID print routine to walk down the object tree
323 * rooted at `mibroot'.
325 #define OBJ_PRINT(o, suppressdot) \
329 if ((o) == objp->oid) \
331 } while ((objp = objp->next) != NULL); \
334 ND_PRINT(suppressdot?"%s":".%s", objp->desc); \
335 objp = objp->child; \
337 ND_PRINT(suppressdot?"%u":".%u", (o)); \
341 * This is the definition for the Any-Data-Type storage used purely for
342 * temporary internal representation while decoding an ASN.1 data stream.
354 u_char form
, class; /* tag info */
365 #define BE_INETADDR 8
368 #define BE_NOSUCHOBJECT 128
369 #define BE_NOSUCHINST 129
370 #define BE_ENDOFMIBVIEW 130
374 * SNMP versions recognized by this module
376 static const char *SnmpVersion
[] = {
378 #define SNMP_VERSION_1 0
380 #define SNMP_VERSION_2 1
382 #define SNMP_VERSION_2U 2
384 #define SNMP_VERSION_3 3
388 * Defaults for SNMP PDU components
390 #define DEF_COMMUNITY "public"
393 * constants for ASN.1 decoding
396 #define ASNLEN_INETADDR 4
399 #define ASN_BIT8 0x80
400 #define ASN_LONGLEN 0x80
402 #define ASN_ID_BITS 0x1f
403 #define ASN_FORM_BITS 0x20
404 #define ASN_FORM_SHIFT 5
405 #define ASN_CLASS_BITS 0xc0
406 #define ASN_CLASS_SHIFT 6
408 #define ASN_ID_EXT 0x1f /* extension ID in tag field */
411 * This decodes the next ASN.1 object in the stream pointed to by "p"
412 * (and of real-length "len") and stores the intermediate data in the
413 * provided BE object.
415 * This returns -l if it fails (i.e., the ASN.1 stream is not valid).
416 * O/w, this returns the number of bytes parsed from "p".
419 asn1_parse(netdissect_options
*ndo
,
420 const u_char
*p
, u_int len
, struct be
*elem
)
422 u_char form
, class, id
;
428 ND_PRINT("[nothing to parse]");
434 * it would be nice to use a bit field, but you can't depend on them.
435 * +---+---+---+---+---+---+---+---+
437 * +---+---+---+---+---+---+---+---+
440 id
= EXTRACT_U_1(p
) & ASN_ID_BITS
; /* lower 5 bits, range 00-1f */
442 form
= (EXTRACT_U_1(p
) & 0xe0) >> 5; /* move upper 3 bits to lower 3 */
443 class = form
>> 1; /* bits 7&6 -> bits 1&0, range 0-3 */
444 form
&= 0x1; /* bit 5 -> bit 0, range 0-1 */
446 form
= (u_char
)(EXTRACT_U_1(p
) & ASN_FORM_BITS
) >> ASN_FORM_SHIFT
;
447 class = (u_char
)(EXTRACT_U_1(p
) & ASN_CLASS_BITS
) >> ASN_CLASS_SHIFT
;
453 /* extended tag field */
454 if (id
== ASN_ID_EXT
) {
456 * The ID follows, as a sequence of octets with the
457 * 8th bit set and the remaining 7 bits being
458 * the next 7 bits of the value, terminated with
459 * an octet with the 8th bit not set.
461 * First, assemble all the octets with the 8th
462 * bit set. XXX - this doesn't handle a value
463 * that won't fit in 32 bits.
467 while (EXTRACT_U_1(p
) & ASN_BIT8
) {
469 ND_PRINT("[Xtagfield?]");
472 id
= (id
<< 7) | (EXTRACT_U_1(p
) & ~ASN_BIT8
);
479 ND_PRINT("[Xtagfield?]");
483 elem
->id
= id
= (id
<< 7) | EXTRACT_U_1(p
);
489 ND_PRINT("[no asnlen]");
493 elem
->asnlen
= EXTRACT_U_1(p
);
495 if (elem
->asnlen
& ASN_BIT8
) {
496 uint32_t noct
= elem
->asnlen
% ASN_BIT8
;
499 ND_PRINT("[asnlen? %d<%d]", len
, noct
);
502 ND_TCHECK_LEN(p
, noct
);
503 for (; noct
-- > 0; len
--, hdr
++) {
504 elem
->asnlen
= (elem
->asnlen
<< ASN_SHIFT8
) | EXTRACT_U_1(p
);
508 if (len
< elem
->asnlen
) {
509 ND_PRINT("[len%d<asnlen%u]", len
, elem
->asnlen
);
512 if (form
>= sizeof(Form
)/sizeof(Form
[0])) {
513 ND_PRINT("[form?%d]", form
);
516 if (class >= sizeof(Class
)/sizeof(Class
[0])) {
517 ND_PRINT("[class?%c/%d]", *Form
[form
], class);
520 if ((int)id
>= Class
[class].numIDs
) {
521 ND_PRINT("[id?%c/%s/%d]", *Form
[form
], Class
[class].name
, id
);
524 ND_TCHECK_LEN(p
, elem
->asnlen
);
541 if (elem
->asnlen
== 0) {
542 ND_PRINT("[asnlen=0]");
545 if (EXTRACT_U_1(p
) & ASN_BIT8
) /* negative */
547 for (i
= elem
->asnlen
; i
!= 0; p
++, i
--)
548 data
= (data
<< ASN_SHIFT8
) | EXTRACT_U_1(p
);
549 elem
->data
.integer
= data
;
555 elem
->data
.raw
= (const uint8_t *)p
;
559 elem
->type
= BE_NULL
;
560 elem
->data
.raw
= NULL
;
564 elem
->type
= BE_OCTET
;
565 elem
->data
.raw
= (const uint8_t *)p
;
566 ND_PRINT("[P/U/%s]", Class
[class].Id
[id
]);
574 elem
->type
= BE_INETADDR
;
575 elem
->data
.raw
= (const uint8_t *)p
;
584 for (i
= elem
->asnlen
; i
!= 0; p
++, i
--)
585 data
= (data
<< 8) + EXTRACT_U_1(p
);
586 elem
->data
.uns
= data
;
592 elem
->type
= BE_UNS64
;
594 for (i
= elem
->asnlen
; i
!= 0; p
++, i
--)
595 data64
= (data64
<< 8) + EXTRACT_U_1(p
);
596 elem
->data
.uns64
= data64
;
601 elem
->type
= BE_OCTET
;
602 elem
->data
.raw
= (const uint8_t *)p
;
604 Class
[class].Id
[id
]);
612 elem
->type
= BE_NOSUCHOBJECT
;
613 elem
->data
.raw
= NULL
;
617 elem
->type
= BE_NOSUCHINST
;
618 elem
->data
.raw
= NULL
;
622 elem
->type
= BE_ENDOFMIBVIEW
;
623 elem
->data
.raw
= NULL
;
629 ND_PRINT("[P/%s/%s]", Class
[class].name
, Class
[class].Id
[id
]);
630 elem
->type
= BE_OCTET
;
631 elem
->data
.raw
= (const uint8_t *)p
;
642 elem
->data
.raw
= (const uint8_t *)p
;
646 elem
->type
= BE_OCTET
;
647 elem
->data
.raw
= (const uint8_t *)p
;
648 ND_PRINT("C/U/%s", Class
[class].Id
[id
]);
655 elem
->data
.raw
= (const uint8_t *)p
;
659 elem
->type
= BE_OCTET
;
660 elem
->data
.raw
= (const uint8_t *)p
;
661 ND_PRINT("C/%s/%s", Class
[class].name
, Class
[class].Id
[id
]);
668 return elem
->asnlen
+ hdr
;
676 asn1_print_octets(netdissect_options
*ndo
, struct be
*elem
)
678 const u_char
*p
= (const u_char
*)elem
->data
.raw
;
679 uint32_t asnlen
= elem
->asnlen
;
682 ND_TCHECK_LEN(p
, asnlen
);
683 for (i
= asnlen
; i
!= 0; p
++, i
--)
684 ND_PRINT("_%.2x", EXTRACT_U_1(p
));
693 asn1_print_string(netdissect_options
*ndo
, struct be
*elem
)
695 int printable
= 1, first
= 1;
697 uint32_t asnlen
= elem
->asnlen
;
701 ND_TCHECK_LEN(p
, asnlen
);
702 for (i
= asnlen
; printable
&& i
!= 0; p
++, i
--)
703 printable
= ND_ISPRINT(EXTRACT_U_1(p
));
707 if (nd_printn(ndo
, p
, asnlen
, ndo
->ndo_snapend
)) {
713 for (i
= asnlen
; i
!= 0; p
++, i
--) {
714 ND_PRINT(first
? "%.2x" : "_%.2x", EXTRACT_U_1(p
));
726 * Display the ASN.1 object represented by the BE object.
727 * This used to be an integral part of asn1_parse() before the intermediate
731 asn1_print(netdissect_options
*ndo
,
735 uint32_t asnlen
= elem
->asnlen
;
738 switch (elem
->type
) {
741 if (asn1_print_octets(ndo
, elem
) == -1)
749 int o
= 0, first
= -1;
751 p
= (const u_char
*)elem
->data
.raw
;
753 if (!ndo
->ndo_nflag
&& asnlen
> 2) {
754 const struct obj_abrev
*a
= &obj_abrev_list
[0];
755 for (; a
->node
; a
++) {
758 if (!ND_TTEST_LEN(p
, a
->oid_len
))
760 if (memcmp(a
->oid
, p
, a
->oid_len
) == 0) {
761 objp
= a
->node
->child
;
764 ND_PRINT("%s", a
->prefix
);
771 for (; i
!= 0; p
++, i
--) {
773 o
= (o
<< ASN_SHIFT7
) + (EXTRACT_U_1(p
) & ~ASN_BIT8
);
774 if (EXTRACT_U_1(p
) & ASN_LONGLEN
)
778 * first subitem encodes two items with
780 * (see X.690:1997 clause 8.19 for the details)
801 ND_PRINT("%d", elem
->data
.integer
);
805 ND_PRINT("%u", elem
->data
.uns
);
809 ND_PRINT("%" PRIu64
, elem
->data
.uns64
);
813 if (asn1_print_string(ndo
, elem
) == -1)
818 ND_PRINT("Seq(%u)", elem
->asnlen
);
822 if (asnlen
!= ASNLEN_INETADDR
)
823 ND_PRINT("[inetaddr len!=%d]", ASNLEN_INETADDR
);
824 p
= (const u_char
*)elem
->data
.raw
;
825 ND_TCHECK_LEN(p
, asnlen
);
826 for (i
= asnlen
; i
!= 0; p
++, i
--) {
827 ND_PRINT((i
== asnlen
) ? "%u" : ".%u", EXTRACT_U_1(p
));
831 case BE_NOSUCHOBJECT
:
833 case BE_ENDOFMIBVIEW
:
834 ND_PRINT("[%s]", Class
[EXCEPTIONS
].Id
[elem
->id
]);
838 ND_PRINT("%s(%u)", Class
[CONTEXT
].Id
[elem
->id
], elem
->asnlen
);
842 ND_PRINT("[BE_ANY!?]");
858 * This is a brute force ASN.1 printer: recurses to dump an entire structure.
859 * This will work for any ASN.1 stream, not just an SNMP PDU.
861 * By adding newlines and spaces at the correct places, this would print in
864 * This is not currently used.
867 asn1_decode(u_char
*p
, u_int length
)
872 while (i
>= 0 && length
> 0) {
873 i
= asn1_parse(ndo
, p
, length
, &elem
);
876 if (asn1_print(ndo
, &elem
) < 0)
878 if (elem
.type
== BE_SEQ
|| elem
.type
== BE_PDU
) {
880 asn1_decode(elem
.data
.raw
, elem
.asnlen
);
893 SmiBasetype basetype
;
897 static const struct smi2be smi2betab
[] = {
898 { SMI_BASETYPE_INTEGER32
, BE_INT
},
899 { SMI_BASETYPE_OCTETSTRING
, BE_STR
},
900 { SMI_BASETYPE_OCTETSTRING
, BE_INETADDR
},
901 { SMI_BASETYPE_OBJECTIDENTIFIER
, BE_OID
},
902 { SMI_BASETYPE_UNSIGNED32
, BE_UNS
},
903 { SMI_BASETYPE_INTEGER64
, BE_NONE
},
904 { SMI_BASETYPE_UNSIGNED64
, BE_UNS64
},
905 { SMI_BASETYPE_FLOAT32
, BE_NONE
},
906 { SMI_BASETYPE_FLOAT64
, BE_NONE
},
907 { SMI_BASETYPE_FLOAT128
, BE_NONE
},
908 { SMI_BASETYPE_ENUM
, BE_INT
},
909 { SMI_BASETYPE_BITS
, BE_STR
},
910 { SMI_BASETYPE_UNKNOWN
, BE_NONE
}
914 smi_decode_oid(netdissect_options
*ndo
,
915 struct be
*elem
, unsigned int *oid
,
916 unsigned int oidsize
, unsigned int *oidlen
)
918 const u_char
*p
= (const u_char
*)elem
->data
.raw
;
919 uint32_t asnlen
= elem
->asnlen
;
921 int o
= 0, first
= -1;
922 unsigned int firstval
;
924 for (*oidlen
= 0; i
!= 0; p
++, i
--) {
926 o
= (o
<< ASN_SHIFT7
) + (EXTRACT_U_1(p
) & ~ASN_BIT8
);
927 if (EXTRACT_U_1(p
) & ASN_LONGLEN
)
931 * first subitem encodes two items with 1st*OIDMUX+2nd
932 * (see X.690:1997 clause 8.19 for the details)
936 firstval
= o
/ OIDMUX
;
937 if (firstval
> 2) firstval
= 2;
938 o
-= firstval
* OIDMUX
;
939 if (*oidlen
< oidsize
) {
940 oid
[(*oidlen
)++] = firstval
;
943 if (*oidlen
< oidsize
) {
944 oid
[(*oidlen
)++] = o
;
955 static int smi_check_type(SmiBasetype basetype
, int be
)
959 for (i
= 0; smi2betab
[i
].basetype
!= SMI_BASETYPE_UNKNOWN
; i
++) {
960 if (smi2betab
[i
].basetype
== basetype
&& smi2betab
[i
].be
== be
) {
968 static int smi_check_a_range(SmiType
*smiType
, SmiRange
*smiRange
,
973 switch (smiType
->basetype
) {
974 case SMI_BASETYPE_OBJECTIDENTIFIER
:
975 case SMI_BASETYPE_OCTETSTRING
:
976 if (smiRange
->minValue
.value
.unsigned32
977 == smiRange
->maxValue
.value
.unsigned32
) {
978 ok
= (elem
->asnlen
== smiRange
->minValue
.value
.unsigned32
);
980 ok
= (elem
->asnlen
>= smiRange
->minValue
.value
.unsigned32
981 && elem
->asnlen
<= smiRange
->maxValue
.value
.unsigned32
);
985 case SMI_BASETYPE_INTEGER32
:
986 ok
= (elem
->data
.integer
>= smiRange
->minValue
.value
.integer32
987 && elem
->data
.integer
<= smiRange
->maxValue
.value
.integer32
);
990 case SMI_BASETYPE_UNSIGNED32
:
991 ok
= (elem
->data
.uns
>= smiRange
->minValue
.value
.unsigned32
992 && elem
->data
.uns
<= smiRange
->maxValue
.value
.unsigned32
);
995 case SMI_BASETYPE_UNSIGNED64
:
999 /* case SMI_BASETYPE_INTEGER64: SMIng */
1000 /* case SMI_BASETYPE_FLOAT32: SMIng */
1001 /* case SMI_BASETYPE_FLOAT64: SMIng */
1002 /* case SMI_BASETYPE_FLOAT128: SMIng */
1004 case SMI_BASETYPE_ENUM
:
1005 case SMI_BASETYPE_BITS
:
1006 case SMI_BASETYPE_UNKNOWN
:
1018 static int smi_check_range(SmiType
*smiType
, struct be
*elem
)
1023 for (smiRange
= smiGetFirstRange(smiType
);
1025 smiRange
= smiGetNextRange(smiRange
)) {
1027 ok
= smi_check_a_range(smiType
, smiRange
, elem
);
1035 SmiType
*parentType
;
1036 parentType
= smiGetParentType(smiType
);
1038 ok
= smi_check_range(parentType
, elem
);
1046 smi_print_variable(netdissect_options
*ndo
,
1047 struct be
*elem
, int *status
)
1049 unsigned int oid
[128], oidlen
;
1050 SmiNode
*smiNode
= NULL
;
1053 if (!nd_smi_module_loaded
) {
1054 *status
= asn1_print(ndo
, elem
);
1057 *status
= smi_decode_oid(ndo
, elem
, oid
, sizeof(oid
) / sizeof(unsigned int),
1061 smiNode
= smiGetNodeByOID(oidlen
, oid
);
1063 *status
= asn1_print(ndo
, elem
);
1066 if (ndo
->ndo_vflag
) {
1067 ND_PRINT("%s::", smiGetNodeModule(smiNode
)->name
);
1069 ND_PRINT("%s", smiNode
->name
);
1070 if (smiNode
->oidlen
< oidlen
) {
1071 for (i
= smiNode
->oidlen
; i
< oidlen
; i
++) {
1072 ND_PRINT(".%u", oid
[i
]);
1080 smi_print_value(netdissect_options
*ndo
,
1081 SmiNode
*smiNode
, u_short pduid
, struct be
*elem
)
1083 unsigned int i
, oid
[128], oidlen
;
1088 if (! smiNode
|| ! (smiNode
->nodekind
1089 & (SMI_NODEKIND_SCALAR
| SMI_NODEKIND_COLUMN
))) {
1090 return asn1_print(ndo
, elem
);
1093 if (elem
->type
== BE_NOSUCHOBJECT
1094 || elem
->type
== BE_NOSUCHINST
1095 || elem
->type
== BE_ENDOFMIBVIEW
) {
1096 return asn1_print(ndo
, elem
);
1099 if (NOTIFY_CLASS(pduid
) && smiNode
->access
< SMI_ACCESS_NOTIFY
) {
1100 ND_PRINT("[notNotifyable]");
1103 if (READ_CLASS(pduid
) && smiNode
->access
< SMI_ACCESS_READ_ONLY
) {
1104 ND_PRINT("[notReadable]");
1107 if (WRITE_CLASS(pduid
) && smiNode
->access
< SMI_ACCESS_READ_WRITE
) {
1108 ND_PRINT("[notWritable]");
1111 if (RESPONSE_CLASS(pduid
)
1112 && smiNode
->access
== SMI_ACCESS_NOT_ACCESSIBLE
) {
1113 ND_PRINT("[noAccess]");
1116 smiType
= smiGetNodeType(smiNode
);
1118 return asn1_print(ndo
, elem
);
1121 if (! smi_check_type(smiType
->basetype
, elem
->type
)) {
1122 ND_PRINT("[wrongType]");
1125 if (! smi_check_range(smiType
, elem
)) {
1126 ND_PRINT("[outOfRange]");
1129 /* resolve bits to named bits */
1131 /* check whether instance identifier is valid */
1133 /* apply display hints (integer, octetstring) */
1135 /* convert instance identifier to index type values */
1137 switch (elem
->type
) {
1139 if (smiType
->basetype
== SMI_BASETYPE_BITS
) {
1140 /* print bit labels */
1142 if (nd_smi_module_loaded
&&
1143 smi_decode_oid(ndo
, elem
, oid
,
1144 sizeof(oid
)/sizeof(unsigned int),
1146 smiNode
= smiGetNodeByOID(oidlen
, oid
);
1148 if (ndo
->ndo_vflag
) {
1149 ND_PRINT("%s::", smiGetNodeModule(smiNode
)->name
);
1151 ND_PRINT("%s", smiNode
->name
);
1152 if (smiNode
->oidlen
< oidlen
) {
1153 for (i
= smiNode
->oidlen
;
1155 ND_PRINT(".%u", oid
[i
]);
1165 if (smiType
->basetype
== SMI_BASETYPE_ENUM
) {
1166 for (nn
= smiGetFirstNamedNumber(smiType
);
1168 nn
= smiGetNextNamedNumber(nn
)) {
1169 if (nn
->value
.value
.integer32
1170 == elem
->data
.integer
) {
1171 ND_PRINT("%s", nn
->name
);
1172 ND_PRINT("(%d)", elem
->data
.integer
);
1182 return asn1_print(ndo
, elem
);
1189 * General SNMP header
1191 * version INTEGER {version-1(0)},
1192 * community OCTET STRING,
1195 * PDUs for all but Trap: (see rfc1157 from page 15 on)
1197 * request-id INTEGER,
1198 * error-status INTEGER,
1199 * error-index INTEGER,
1200 * varbindlist SEQUENCE OF
1208 * enterprise OBJECT IDENTIFIER,
1209 * agent-addr NetworkAddress,
1210 * generic-trap INTEGER,
1211 * specific-trap INTEGER,
1212 * time-stamp TimeTicks,
1213 * varbindlist SEQUENCE OF
1222 * Decode SNMP varBind
1225 varbind_print(netdissect_options
*ndo
,
1226 u_short pduid
, const u_char
*np
, u_int length
)
1231 SmiNode
*smiNode
= NULL
;
1235 /* Sequence of varBind */
1236 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1238 if (elem
.type
!= BE_SEQ
) {
1239 ND_PRINT("[!SEQ of varbind]");
1240 asn1_print(ndo
, &elem
);
1243 if ((u_int
)count
< length
)
1244 ND_PRINT("[%d extra after SEQ of varbind]", length
- count
);
1246 length
= elem
.asnlen
;
1247 np
= (const u_char
*)elem
.data
.raw
;
1249 for (ind
= 1; length
> 0; ind
++) {
1250 const u_char
*vbend
;
1256 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1258 if (elem
.type
!= BE_SEQ
) {
1259 ND_PRINT("[!varbind]");
1260 asn1_print(ndo
, &elem
);
1264 vblength
= length
- count
;
1266 length
= elem
.asnlen
;
1267 np
= (const u_char
*)elem
.data
.raw
;
1270 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1272 if (elem
.type
!= BE_OID
) {
1273 ND_PRINT("[objName!=OID]");
1274 asn1_print(ndo
, &elem
);
1278 smiNode
= smi_print_variable(ndo
, &elem
, &status
);
1280 status
= asn1_print(ndo
, &elem
);
1287 if (pduid
!= GETREQ
&& pduid
!= GETNEXTREQ
1288 && pduid
!= GETBULKREQ
)
1292 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1294 if (pduid
== GETREQ
|| pduid
== GETNEXTREQ
1295 || pduid
== GETBULKREQ
) {
1296 if (elem
.type
!= BE_NULL
) {
1297 ND_PRINT("[objVal!=NULL]");
1298 if (asn1_print(ndo
, &elem
) < 0)
1302 if (elem
.type
!= BE_NULL
) {
1304 status
= smi_print_value(ndo
, smiNode
, pduid
, &elem
);
1306 status
= asn1_print(ndo
, &elem
);
1318 * Decode SNMP PDUs: GetRequest, GetNextRequest, GetResponse, SetRequest,
1319 * GetBulk, Inform, V2Trap, and Report
1322 snmppdu_print(netdissect_options
*ndo
,
1323 u_short pduid
, const u_char
*np
, u_int length
)
1326 int count
= 0, error_status
;
1328 /* reqId (Integer) */
1329 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1331 if (elem
.type
!= BE_INT
) {
1332 ND_PRINT("[reqId!=INT]");
1333 asn1_print(ndo
, &elem
);
1337 ND_PRINT("R=%d ", elem
.data
.integer
);
1341 /* errorStatus (Integer) */
1342 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1344 if (elem
.type
!= BE_INT
) {
1345 ND_PRINT("[errorStatus!=INT]");
1346 asn1_print(ndo
, &elem
);
1350 if ((pduid
== GETREQ
|| pduid
== GETNEXTREQ
|| pduid
== SETREQ
1351 || pduid
== INFORMREQ
|| pduid
== V2TRAP
|| pduid
== REPORT
)
1352 && elem
.data
.integer
!= 0) {
1354 ND_PRINT("[errorStatus(%s)!=0]",
1355 DECODE_ErrorStatus(elem
.data
.integer
));
1356 } else if (pduid
== GETBULKREQ
) {
1357 ND_PRINT(" N=%d", elem
.data
.integer
);
1358 } else if (elem
.data
.integer
!= 0) {
1360 ND_PRINT(" %s", DECODE_ErrorStatus(elem
.data
.integer
));
1361 error_status
= elem
.data
.integer
;
1366 /* errorIndex (Integer) */
1367 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1369 if (elem
.type
!= BE_INT
) {
1370 ND_PRINT("[errorIndex!=INT]");
1371 asn1_print(ndo
, &elem
);
1374 if ((pduid
== GETREQ
|| pduid
== GETNEXTREQ
|| pduid
== SETREQ
1375 || pduid
== INFORMREQ
|| pduid
== V2TRAP
|| pduid
== REPORT
)
1376 && elem
.data
.integer
!= 0)
1377 ND_PRINT("[errorIndex(%d)!=0]", elem
.data
.integer
);
1378 else if (pduid
== GETBULKREQ
)
1379 ND_PRINT(" M=%d", elem
.data
.integer
);
1380 else if (elem
.data
.integer
!= 0) {
1382 ND_PRINT("[errorIndex(%d) w/o errorStatus]", elem
.data
.integer
);
1384 ND_PRINT("@%d", elem
.data
.integer
);
1385 } else if (error_status
) {
1386 ND_PRINT("[errorIndex==0]");
1391 varbind_print(ndo
, pduid
, np
, length
);
1396 * Decode SNMP Trap PDU
1399 trappdu_print(netdissect_options
*ndo
,
1400 const u_char
*np
, u_int length
)
1403 int count
= 0, generic
;
1407 /* enterprise (oid) */
1408 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1410 if (elem
.type
!= BE_OID
) {
1411 ND_PRINT("[enterprise!=OID]");
1412 asn1_print(ndo
, &elem
);
1415 if (asn1_print(ndo
, &elem
) < 0)
1422 /* agent-addr (inetaddr) */
1423 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1425 if (elem
.type
!= BE_INETADDR
) {
1426 ND_PRINT("[agent-addr!=INETADDR]");
1427 asn1_print(ndo
, &elem
);
1430 if (asn1_print(ndo
, &elem
) < 0)
1435 /* generic-trap (Integer) */
1436 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1438 if (elem
.type
!= BE_INT
) {
1439 ND_PRINT("[generic-trap!=INT]");
1440 asn1_print(ndo
, &elem
);
1443 generic
= elem
.data
.integer
;
1446 ND_PRINT(" %s", DECODE_GenericTrap(generic
));
1451 /* specific-trap (Integer) */
1452 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1454 if (elem
.type
!= BE_INT
) {
1455 ND_PRINT("[specific-trap!=INT]");
1456 asn1_print(ndo
, &elem
);
1459 if (generic
!= GT_ENTERPRISE
) {
1460 if (elem
.data
.integer
!= 0)
1461 ND_PRINT("[specific-trap(%d)!=0]", elem
.data
.integer
);
1463 ND_PRINT(" s=%d", elem
.data
.integer
);
1469 /* time-stamp (TimeTicks) */
1470 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1472 if (elem
.type
!= BE_UNS
) { /* XXX */
1473 ND_PRINT("[time-stamp!=TIMETICKS]");
1474 asn1_print(ndo
, &elem
);
1477 if (asn1_print(ndo
, &elem
) < 0)
1482 varbind_print(ndo
, TRAP
, np
, length
);
1487 * Decode arbitrary SNMP PDUs.
1490 pdu_print(netdissect_options
*ndo
,
1491 const u_char
*np
, u_int length
, int version
)
1497 if ((count
= asn1_parse(ndo
, np
, length
, &pdu
)) < 0)
1499 if (pdu
.type
!= BE_PDU
) {
1500 ND_PRINT("[no PDU]");
1503 if ((u_int
)count
< length
)
1504 ND_PRINT("[%d extra after PDU]", length
- count
);
1505 if (ndo
->ndo_vflag
) {
1508 if (asn1_print(ndo
, &pdu
) < 0)
1511 /* descend into PDU */
1512 length
= pdu
.asnlen
;
1513 np
= (const u_char
*)pdu
.data
.raw
;
1515 if (version
== SNMP_VERSION_1
&&
1516 (pdu
.id
== GETBULKREQ
|| pdu
.id
== INFORMREQ
||
1517 pdu
.id
== V2TRAP
|| pdu
.id
== REPORT
)) {
1518 ND_PRINT("[v2 PDU in v1 message]");
1522 if (version
== SNMP_VERSION_2
&& pdu
.id
== TRAP
) {
1523 ND_PRINT("[v1 PDU in v2 message]");
1529 trappdu_print(ndo
, np
, length
);
1539 snmppdu_print(ndo
, pdu
.id
, np
, length
);
1543 if (ndo
->ndo_vflag
) {
1549 * Decode a scoped SNMP PDU.
1552 scopedpdu_print(netdissect_options
*ndo
,
1553 const u_char
*np
, u_int length
, int version
)
1559 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1561 if (elem
.type
!= BE_SEQ
) {
1562 ND_PRINT("[!scoped PDU]");
1563 asn1_print(ndo
, &elem
);
1566 length
= elem
.asnlen
;
1567 np
= (const u_char
*)elem
.data
.raw
;
1569 /* contextEngineID (OCTET STRING) */
1570 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1572 if (elem
.type
!= BE_STR
) {
1573 ND_PRINT("[contextEngineID!=STR]");
1574 asn1_print(ndo
, &elem
);
1581 if (asn1_print_octets(ndo
, &elem
) == -1)
1585 /* contextName (OCTET STRING) */
1586 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1588 if (elem
.type
!= BE_STR
) {
1589 ND_PRINT("[contextName!=STR]");
1590 asn1_print(ndo
, &elem
);
1597 if (asn1_print_string(ndo
, &elem
) == -1)
1601 pdu_print(ndo
, np
, length
, version
);
1605 * Decode SNMP Community Header (SNMPv1 and SNMPv2c)
1608 community_print(netdissect_options
*ndo
,
1609 const u_char
*np
, u_int length
, int version
)
1614 /* Community (String) */
1615 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1617 if (elem
.type
!= BE_STR
) {
1618 ND_PRINT("[comm!=STR]");
1619 asn1_print(ndo
, &elem
);
1622 /* default community */
1623 if (!(elem
.asnlen
== sizeof(DEF_COMMUNITY
) - 1 &&
1624 strncmp((const char *)elem
.data
.str
, DEF_COMMUNITY
,
1625 sizeof(DEF_COMMUNITY
) - 1) == 0)) {
1628 if (asn1_print_string(ndo
, &elem
) == -1)
1635 pdu_print(ndo
, np
, length
, version
);
1639 * Decode SNMPv3 User-based Security Message Header (SNMPv3)
1642 usm_print(netdissect_options
*ndo
,
1643 const u_char
*np
, u_int length
)
1649 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1651 if (elem
.type
!= BE_SEQ
) {
1653 asn1_print(ndo
, &elem
);
1656 length
= elem
.asnlen
;
1657 np
= (const u_char
*)elem
.data
.raw
;
1659 /* msgAuthoritativeEngineID (OCTET STRING) */
1660 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1662 if (elem
.type
!= BE_STR
) {
1663 ND_PRINT("[msgAuthoritativeEngineID!=STR]");
1664 asn1_print(ndo
, &elem
);
1670 /* msgAuthoritativeEngineBoots (INTEGER) */
1671 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1673 if (elem
.type
!= BE_INT
) {
1674 ND_PRINT("[msgAuthoritativeEngineBoots!=INT]");
1675 asn1_print(ndo
, &elem
);
1679 ND_PRINT("B=%d ", elem
.data
.integer
);
1683 /* msgAuthoritativeEngineTime (INTEGER) */
1684 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1686 if (elem
.type
!= BE_INT
) {
1687 ND_PRINT("[msgAuthoritativeEngineTime!=INT]");
1688 asn1_print(ndo
, &elem
);
1692 ND_PRINT("T=%d ", elem
.data
.integer
);
1696 /* msgUserName (OCTET STRING) */
1697 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1699 if (elem
.type
!= BE_STR
) {
1700 ND_PRINT("[msgUserName!=STR]");
1701 asn1_print(ndo
, &elem
);
1708 if (asn1_print_string(ndo
, &elem
) == -1)
1712 /* msgAuthenticationParameters (OCTET STRING) */
1713 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1715 if (elem
.type
!= BE_STR
) {
1716 ND_PRINT("[msgAuthenticationParameters!=STR]");
1717 asn1_print(ndo
, &elem
);
1723 /* msgPrivacyParameters (OCTET STRING) */
1724 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1726 if (elem
.type
!= BE_STR
) {
1727 ND_PRINT("[msgPrivacyParameters!=STR]");
1728 asn1_print(ndo
, &elem
);
1734 if ((u_int
)count
< length
)
1735 ND_PRINT("[%d extra after usm SEQ]", length
- count
);
1739 * Decode SNMPv3 Message Header (SNMPv3)
1742 v3msg_print(netdissect_options
*ndo
,
1743 const u_char
*np
, u_int length
)
1749 const u_char
*xnp
= np
;
1750 int xlength
= length
;
1753 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1755 if (elem
.type
!= BE_SEQ
) {
1756 ND_PRINT("[!message]");
1757 asn1_print(ndo
, &elem
);
1760 length
= elem
.asnlen
;
1761 np
= (const u_char
*)elem
.data
.raw
;
1763 if (ndo
->ndo_vflag
) {
1767 /* msgID (INTEGER) */
1768 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1770 if (elem
.type
!= BE_INT
) {
1771 ND_PRINT("[msgID!=INT]");
1772 asn1_print(ndo
, &elem
);
1778 /* msgMaxSize (INTEGER) */
1779 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1781 if (elem
.type
!= BE_INT
) {
1782 ND_PRINT("[msgMaxSize!=INT]");
1783 asn1_print(ndo
, &elem
);
1789 /* msgFlags (OCTET STRING) */
1790 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1792 if (elem
.type
!= BE_STR
) {
1793 ND_PRINT("[msgFlags!=STR]");
1794 asn1_print(ndo
, &elem
);
1797 if (elem
.asnlen
!= 1) {
1798 ND_PRINT("[msgFlags size %d]", elem
.asnlen
);
1801 flags
= EXTRACT_U_1(elem
.data
.str
);
1802 if (flags
!= 0x00 && flags
!= 0x01 && flags
!= 0x03
1803 && flags
!= 0x04 && flags
!= 0x05 && flags
!= 0x07) {
1804 ND_PRINT("[msgFlags=0x%02X]", flags
);
1810 ND_PRINT("F=%s%s%s ",
1811 flags
& 0x01 ? "a" : "",
1812 flags
& 0x02 ? "p" : "",
1813 flags
& 0x04 ? "r" : "");
1815 /* msgSecurityModel (INTEGER) */
1816 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1818 if (elem
.type
!= BE_INT
) {
1819 ND_PRINT("[msgSecurityModel!=INT]");
1820 asn1_print(ndo
, &elem
);
1823 model
= elem
.data
.integer
;
1827 if ((u_int
)count
< length
)
1828 ND_PRINT("[%d extra after message SEQ]", length
- count
);
1830 if (ndo
->ndo_vflag
) {
1835 if (ndo
->ndo_vflag
) {
1839 ND_PRINT("[security model %d]", model
);
1843 np
= xnp
+ (np
- xnp
);
1844 length
= xlength
- (np
- xnp
);
1846 /* msgSecurityParameters (OCTET STRING) */
1847 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1849 if (elem
.type
!= BE_STR
) {
1850 ND_PRINT("[msgSecurityParameters!=STR]");
1851 asn1_print(ndo
, &elem
);
1858 usm_print(ndo
, elem
.data
.str
, elem
.asnlen
);
1859 if (ndo
->ndo_vflag
) {
1864 if (ndo
->ndo_vflag
) {
1865 ND_PRINT("{ ScopedPDU ");
1868 scopedpdu_print(ndo
, np
, length
, 3);
1870 if (ndo
->ndo_vflag
) {
1876 * Decode SNMP header and pass on to PDU printing routines
1879 snmp_print(netdissect_options
*ndo
,
1880 const u_char
*np
, u_int length
)
1886 ndo
->ndo_protocol
= "snmp";
1889 /* initial Sequence */
1890 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1892 if (elem
.type
!= BE_SEQ
) {
1893 ND_PRINT("[!init SEQ]");
1894 asn1_print(ndo
, &elem
);
1897 if ((u_int
)count
< length
)
1898 ND_PRINT("[%d extra after iSEQ]", length
- count
);
1900 length
= elem
.asnlen
;
1901 np
= (const u_char
*)elem
.data
.raw
;
1903 /* Version (INTEGER) */
1904 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1906 if (elem
.type
!= BE_INT
) {
1907 ND_PRINT("[version!=INT]");
1908 asn1_print(ndo
, &elem
);
1912 switch (elem
.data
.integer
) {
1913 case SNMP_VERSION_1
:
1914 case SNMP_VERSION_2
:
1915 case SNMP_VERSION_3
:
1917 ND_PRINT("{ %s ", SnmpVersion
[elem
.data
.integer
]);
1920 ND_PRINT("SNMP [version = %d]", elem
.data
.integer
);
1923 version
= elem
.data
.integer
;
1928 case SNMP_VERSION_1
:
1929 case SNMP_VERSION_2
:
1930 community_print(ndo
, np
, length
, version
);
1932 case SNMP_VERSION_3
:
1933 v3msg_print(ndo
, np
, length
);
1936 ND_PRINT("[version = %d]", elem
.data
.integer
);
1940 if (ndo
->ndo_vflag
) {