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-ctype.h"
76 #include "netdissect.h"
79 #undef OPAQUE /* defined in <wingdi.h> */
83 * Universal ASN.1 types
84 * (we only care about the tag values for those allowed in the Internet SMI)
86 static const char *Universal
[] = {
99 "U-8","U-9","U-10","U-11", /* 8-11 */
100 "U-12","U-13","U-14","U-15", /* 12-15 */
107 * Application-wide ASN.1 types from the Internet SMI and their tags
109 static const char *Application
[] = {
126 * Context-specific ASN.1 types for the SNMP PDUs and their tags
128 static const char *Context
[] = {
149 #define NOTIFY_CLASS(x) (x == TRAP || x == V2TRAP || x == INFORMREQ)
150 #define READ_CLASS(x) (x == GETREQ || x == GETNEXTREQ || x == GETBULKREQ)
151 #define WRITE_CLASS(x) (x == SETREQ)
152 #define RESPONSE_CLASS(x) (x == GETRESP)
153 #define INTERNAL_CLASS(x) (x == REPORT)
156 * Context-specific ASN.1 types for the SNMP Exceptions and their tags
158 static const char *Exceptions
[] = {
160 #define NOSUCHOBJECT 0
162 #define NOSUCHINSTANCE 1
164 #define ENDOFMIBVIEW 2
168 * Private ASN.1 types
169 * The Internet SMI does not specify any
171 static const char *Private
[] = {
176 * error-status values for any SNMP PDU
178 static const char *ErrorStatus
[] = {
192 "resourceUnavailable",
195 "authorizationError",
199 #define DECODE_ErrorStatus(e) \
200 ( e >= 0 && (size_t)e < sizeof(ErrorStatus)/sizeof(ErrorStatus[0]) \
202 : (snprintf(errbuf, sizeof(errbuf), "err=%u", e), errbuf))
205 * generic-trap values in the SNMP Trap-PDU
207 static const char *GenericTrap
[] = {
212 "authenticationFailure",
215 #define GT_ENTERPRISE 6
217 #define DECODE_GenericTrap(t) \
218 ( t >= 0 && (size_t)t < sizeof(GenericTrap)/sizeof(GenericTrap[0]) \
220 : (snprintf(buf, sizeof(buf), "gt=%d", t), buf))
223 * ASN.1 type class table
224 * Ties together the preceding Universal, Application, Context, and Private
227 #define defineCLASS(x) { "x", x, sizeof(x)/sizeof(x[0]) } /* not ANSI-C */
228 static const struct {
233 defineCLASS(Universal
),
235 defineCLASS(Application
),
236 #define APPLICATION 1
237 defineCLASS(Context
),
239 defineCLASS(Private
),
241 defineCLASS(Exceptions
),
246 * defined forms for ASN.1 types
248 static const char *Form
[] = {
252 #define CONSTRUCTED 1
256 * A structure for the OID tree for the compiled-in MIB.
257 * This is stored as a general-order tree.
260 const char *desc
; /* name of object */
261 u_char oid
; /* sub-id following parent */
262 u_char type
; /* object type (unused) */
263 struct obj
*child
, *next
; /* child and next sibling pointers */
267 * Include the compiled in SNMP MIB. "mib.h" is produced by feeding
268 * RFC-1156 format files into "makemib". "mib.h" MUST define at least
269 * a value for `mibroot'.
271 * In particular, this is gross, as this is including initialized structures,
272 * and by right shouldn't be an "include" file.
277 * This defines a list of OIDs which will be abbreviated on output.
278 * Currently, this includes the prefixes for the Internet MIB, the
279 * private enterprises tree, and the experimental tree.
281 #define OID_FIRST_OCTET(x, y) (((x)*40) + (y)) /* X.690 8.19.4 */
284 static const uint8_t mib_oid
[] = { OID_FIRST_OCTET(1, 3), 6, 1, 2, 1 };
286 #ifndef NO_ABREV_ENTER
287 static const uint8_t enterprises_oid
[] = { OID_FIRST_OCTET(1, 3), 6, 1, 4, 1 };
289 #ifndef NO_ABREV_EXPERI
290 static const uint8_t experimental_oid
[] = { OID_FIRST_OCTET(1, 3), 6, 1, 3 };
292 #ifndef NO_ABBREV_SNMPMODS
293 static const uint8_t snmpModules_oid
[] = { OID_FIRST_OCTET(1, 3), 6, 1, 6, 3 };
296 #define OBJ_ABBREV_ENTRY(prefix, obj) \
297 { prefix, &_ ## obj ## _obj, obj ## _oid, sizeof (obj ## _oid) }
298 static const struct obj_abrev
{
299 const char *prefix
; /* prefix for this abrev */
300 struct obj
*node
; /* pointer into object table */
301 const uint8_t *oid
; /* ASN.1 encoded OID */
302 size_t oid_len
; /* length of OID */
303 } obj_abrev_list
[] = {
305 /* .iso.org.dod.internet.mgmt.mib */
306 OBJ_ABBREV_ENTRY("", mib
),
308 #ifndef NO_ABREV_ENTER
309 /* .iso.org.dod.internet.private.enterprises */
310 OBJ_ABBREV_ENTRY("E:", enterprises
),
312 #ifndef NO_ABREV_EXPERI
313 /* .iso.org.dod.internet.experimental */
314 OBJ_ABBREV_ENTRY("X:", experimental
),
316 #ifndef NO_ABBREV_SNMPMODS
317 /* .iso.org.dod.internet.snmpV2.snmpModules */
318 OBJ_ABBREV_ENTRY("S:", snmpModules
),
324 * This is used in the OID print routine to walk down the object tree
325 * rooted at `mibroot'.
327 #define OBJ_PRINT(o, suppressdot) \
331 if ((o) == objp->oid) \
333 } while ((objp = objp->next) != NULL); \
336 ND_PRINT(suppressdot?"%s":".%s", objp->desc); \
337 objp = objp->child; \
339 ND_PRINT(suppressdot?"%u":".%u", (o)); \
343 * This is the definition for the Any-Data-Type storage used purely for
344 * temporary internal representation while decoding an ASN.1 data stream.
356 u_char form
, class; /* tag info */
367 #define BE_INETADDR 8
370 #define BE_NOSUCHOBJECT 128
371 #define BE_NOSUCHINST 129
372 #define BE_ENDOFMIBVIEW 130
376 * SNMP versions recognized by this module
378 static const char *SnmpVersion
[] = {
380 #define SNMP_VERSION_1 0
382 #define SNMP_VERSION_2 1
384 #define SNMP_VERSION_2U 2
386 #define SNMP_VERSION_3 3
390 * Defaults for SNMP PDU components
392 #define DEF_COMMUNITY "public"
395 * constants for ASN.1 decoding
398 #define ASNLEN_INETADDR 4
401 #define ASN_BIT8 0x80
402 #define ASN_LONGLEN 0x80
404 #define ASN_ID_BITS 0x1f
405 #define ASN_FORM_BITS 0x20
406 #define ASN_FORM_SHIFT 5
407 #define ASN_CLASS_BITS 0xc0
408 #define ASN_CLASS_SHIFT 6
410 #define ASN_ID_EXT 0x1f /* extension ID in tag field */
413 * This decodes the next ASN.1 object in the stream pointed to by "p"
414 * (and of real-length "len") and stores the intermediate data in the
415 * provided BE object.
417 * This returns -l if it fails (i.e., the ASN.1 stream is not valid).
418 * O/w, this returns the number of bytes parsed from "p".
421 asn1_parse(netdissect_options
*ndo
,
422 const u_char
*p
, u_int len
, struct be
*elem
)
424 u_char form
, class, id
;
430 ND_PRINT("[nothing to parse]");
435 * it would be nice to use a bit field, but you can't depend on them.
436 * +---+---+---+---+---+---+---+---+
438 * +---+---+---+---+---+---+---+---+
441 id
= GET_U_1(p
) & ASN_ID_BITS
; /* lower 5 bits, range 00-1f */
443 form
= (GET_U_1(p
) & 0xe0) >> 5; /* move upper 3 bits to lower 3 */
444 class = form
>> 1; /* bits 7&6 -> bits 1&0, range 0-3 */
445 form
&= 0x1; /* bit 5 -> bit 0, range 0-1 */
447 form
= (u_char
)(GET_U_1(p
) & ASN_FORM_BITS
) >> ASN_FORM_SHIFT
;
448 class = (u_char
)(GET_U_1(p
) & ASN_CLASS_BITS
) >> ASN_CLASS_SHIFT
;
454 /* extended tag field */
455 if (id
== ASN_ID_EXT
) {
457 * The ID follows, as a sequence of octets with the
458 * 8th bit set and the remaining 7 bits being
459 * the next 7 bits of the value, terminated with
460 * an octet with the 8th bit not set.
462 * First, assemble all the octets with the 8th
463 * bit set. XXX - this doesn't handle a value
464 * that won't fit in 32 bits.
467 while (GET_U_1(p
) & ASN_BIT8
) {
469 ND_PRINT("[Xtagfield?]");
472 id
= (id
<< 7) | (GET_U_1(p
) & ~ASN_BIT8
);
479 ND_PRINT("[Xtagfield?]");
482 elem
->id
= id
= (id
<< 7) | GET_U_1(p
);
488 ND_PRINT("[no asnlen]");
491 elem
->asnlen
= GET_U_1(p
);
493 if (elem
->asnlen
& ASN_BIT8
) {
494 uint32_t noct
= elem
->asnlen
% ASN_BIT8
;
497 ND_PRINT("[asnlen? %d<%d]", len
, noct
);
500 ND_TCHECK_LEN(p
, 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
;
674 asn1_print_octets(netdissect_options
*ndo
, struct be
*elem
)
676 const u_char
*p
= (const u_char
*)elem
->data
.raw
;
677 uint32_t asnlen
= elem
->asnlen
;
680 ND_TCHECK_LEN(p
, asnlen
);
681 for (i
= asnlen
; i
!= 0; p
++, i
--)
682 ND_PRINT("_%.2x", GET_U_1(p
));
691 asn1_print_string(netdissect_options
*ndo
, struct be
*elem
)
693 int printable
= 1, first
= 1;
695 uint32_t asnlen
= elem
->asnlen
;
699 ND_TCHECK_LEN(p
, asnlen
);
700 for (i
= asnlen
; printable
&& i
!= 0; p
++, i
--)
701 printable
= ND_ASCII_ISPRINT(GET_U_1(p
));
705 if (nd_printn(ndo
, p
, asnlen
, ndo
->ndo_snapend
)) {
711 for (i
= asnlen
; i
!= 0; p
++, i
--) {
712 ND_PRINT(first
? "%.2x" : "_%.2x", GET_U_1(p
));
724 * Display the ASN.1 object represented by the BE object.
725 * This used to be an integral part of asn1_parse() before the intermediate
729 asn1_print(netdissect_options
*ndo
,
733 uint32_t asnlen
= elem
->asnlen
;
736 switch (elem
->type
) {
739 if (asn1_print_octets(ndo
, elem
) == -1)
747 int o
= 0, first
= -1;
749 p
= (const u_char
*)elem
->data
.raw
;
751 if (!ndo
->ndo_nflag
&& asnlen
> 2) {
752 const struct obj_abrev
*a
= &obj_abrev_list
[0];
753 for (; a
->node
; a
++) {
756 if (!ND_TTEST_LEN(p
, a
->oid_len
))
758 if (memcmp(a
->oid
, p
, a
->oid_len
) == 0) {
759 objp
= a
->node
->child
;
762 ND_PRINT("%s", a
->prefix
);
769 for (; i
!= 0; p
++, i
--) {
770 o
= (o
<< ASN_SHIFT7
) + (GET_U_1(p
) & ~ASN_BIT8
);
771 if (GET_U_1(p
) & ASN_LONGLEN
)
775 * first subitem encodes two items with
777 * (see X.690:1997 clause 8.19 for the details)
798 ND_PRINT("%d", elem
->data
.integer
);
802 ND_PRINT("%u", elem
->data
.uns
);
806 ND_PRINT("%" PRIu64
, elem
->data
.uns64
);
810 if (asn1_print_string(ndo
, elem
) == -1)
815 ND_PRINT("Seq(%u)", elem
->asnlen
);
819 if (asnlen
!= ASNLEN_INETADDR
)
820 ND_PRINT("[inetaddr len!=%d]", ASNLEN_INETADDR
);
821 p
= (const u_char
*)elem
->data
.raw
;
822 ND_TCHECK_LEN(p
, asnlen
);
823 for (i
= asnlen
; i
!= 0; p
++, i
--) {
824 ND_PRINT((i
== asnlen
) ? "%u" : ".%u", GET_U_1(p
));
828 case BE_NOSUCHOBJECT
:
830 case BE_ENDOFMIBVIEW
:
831 ND_PRINT("[%s]", Class
[EXCEPTIONS
].Id
[elem
->id
]);
835 ND_PRINT("%s(%u)", Class
[CONTEXT
].Id
[elem
->id
], elem
->asnlen
);
839 ND_PRINT("[BE_ANY!?]");
855 * This is a brute force ASN.1 printer: recurses to dump an entire structure.
856 * This will work for any ASN.1 stream, not just an SNMP PDU.
858 * By adding newlines and spaces at the correct places, this would print in
861 * This is not currently used.
864 asn1_decode(u_char
*p
, u_int length
)
869 while (i
>= 0 && length
> 0) {
870 i
= asn1_parse(ndo
, p
, length
, &elem
);
873 if (asn1_print(ndo
, &elem
) < 0)
875 if (elem
.type
== BE_SEQ
|| elem
.type
== BE_PDU
) {
877 asn1_decode(elem
.data
.raw
, elem
.asnlen
);
890 SmiBasetype basetype
;
894 static const struct smi2be smi2betab
[] = {
895 { SMI_BASETYPE_INTEGER32
, BE_INT
},
896 { SMI_BASETYPE_OCTETSTRING
, BE_STR
},
897 { SMI_BASETYPE_OCTETSTRING
, BE_INETADDR
},
898 { SMI_BASETYPE_OBJECTIDENTIFIER
, BE_OID
},
899 { SMI_BASETYPE_UNSIGNED32
, BE_UNS
},
900 { SMI_BASETYPE_INTEGER64
, BE_NONE
},
901 { SMI_BASETYPE_UNSIGNED64
, BE_UNS64
},
902 { SMI_BASETYPE_FLOAT32
, BE_NONE
},
903 { SMI_BASETYPE_FLOAT64
, BE_NONE
},
904 { SMI_BASETYPE_FLOAT128
, BE_NONE
},
905 { SMI_BASETYPE_ENUM
, BE_INT
},
906 { SMI_BASETYPE_BITS
, BE_STR
},
907 { SMI_BASETYPE_UNKNOWN
, BE_NONE
}
911 smi_decode_oid(netdissect_options
*ndo
,
912 struct be
*elem
, unsigned int *oid
,
913 unsigned int oidsize
, unsigned int *oidlen
)
915 const u_char
*p
= (const u_char
*)elem
->data
.raw
;
916 uint32_t asnlen
= elem
->asnlen
;
918 int o
= 0, first
= -1;
919 unsigned int firstval
;
921 for (*oidlen
= 0; i
!= 0; p
++, i
--) {
922 o
= (o
<< ASN_SHIFT7
) + (GET_U_1(p
) & ~ASN_BIT8
);
923 if (GET_U_1(p
) & ASN_LONGLEN
)
927 * first subitem encodes two items with 1st*OIDMUX+2nd
928 * (see X.690:1997 clause 8.19 for the details)
932 firstval
= o
/ OIDMUX
;
933 if (firstval
> 2) firstval
= 2;
934 o
-= firstval
* OIDMUX
;
935 if (*oidlen
< oidsize
) {
936 oid
[(*oidlen
)++] = firstval
;
939 if (*oidlen
< oidsize
) {
940 oid
[(*oidlen
)++] = o
;
947 static int smi_check_type(SmiBasetype basetype
, int be
)
951 for (i
= 0; smi2betab
[i
].basetype
!= SMI_BASETYPE_UNKNOWN
; i
++) {
952 if (smi2betab
[i
].basetype
== basetype
&& smi2betab
[i
].be
== be
) {
960 static int smi_check_a_range(SmiType
*smiType
, SmiRange
*smiRange
,
965 switch (smiType
->basetype
) {
966 case SMI_BASETYPE_OBJECTIDENTIFIER
:
967 case SMI_BASETYPE_OCTETSTRING
:
968 if (smiRange
->minValue
.value
.unsigned32
969 == smiRange
->maxValue
.value
.unsigned32
) {
970 ok
= (elem
->asnlen
== smiRange
->minValue
.value
.unsigned32
);
972 ok
= (elem
->asnlen
>= smiRange
->minValue
.value
.unsigned32
973 && elem
->asnlen
<= smiRange
->maxValue
.value
.unsigned32
);
977 case SMI_BASETYPE_INTEGER32
:
978 ok
= (elem
->data
.integer
>= smiRange
->minValue
.value
.integer32
979 && elem
->data
.integer
<= smiRange
->maxValue
.value
.integer32
);
982 case SMI_BASETYPE_UNSIGNED32
:
983 ok
= (elem
->data
.uns
>= smiRange
->minValue
.value
.unsigned32
984 && elem
->data
.uns
<= smiRange
->maxValue
.value
.unsigned32
);
987 case SMI_BASETYPE_UNSIGNED64
:
991 /* case SMI_BASETYPE_INTEGER64: SMIng */
992 /* case SMI_BASETYPE_FLOAT32: SMIng */
993 /* case SMI_BASETYPE_FLOAT64: SMIng */
994 /* case SMI_BASETYPE_FLOAT128: SMIng */
996 case SMI_BASETYPE_ENUM
:
997 case SMI_BASETYPE_BITS
:
998 case SMI_BASETYPE_UNKNOWN
:
1010 static int smi_check_range(SmiType
*smiType
, struct be
*elem
)
1015 for (smiRange
= smiGetFirstRange(smiType
);
1017 smiRange
= smiGetNextRange(smiRange
)) {
1019 ok
= smi_check_a_range(smiType
, smiRange
, elem
);
1027 SmiType
*parentType
;
1028 parentType
= smiGetParentType(smiType
);
1030 ok
= smi_check_range(parentType
, elem
);
1038 smi_print_variable(netdissect_options
*ndo
,
1039 struct be
*elem
, int *status
)
1041 unsigned int oid
[128], oidlen
;
1042 SmiNode
*smiNode
= NULL
;
1045 if (!nd_smi_module_loaded
) {
1046 *status
= asn1_print(ndo
, elem
);
1049 *status
= smi_decode_oid(ndo
, elem
, oid
, sizeof(oid
) / sizeof(unsigned int),
1053 smiNode
= smiGetNodeByOID(oidlen
, oid
);
1055 *status
= asn1_print(ndo
, elem
);
1058 if (ndo
->ndo_vflag
) {
1059 ND_PRINT("%s::", smiGetNodeModule(smiNode
)->name
);
1061 ND_PRINT("%s", smiNode
->name
);
1062 if (smiNode
->oidlen
< oidlen
) {
1063 for (i
= smiNode
->oidlen
; i
< oidlen
; i
++) {
1064 ND_PRINT(".%u", oid
[i
]);
1072 smi_print_value(netdissect_options
*ndo
,
1073 SmiNode
*smiNode
, u_short pduid
, struct be
*elem
)
1075 unsigned int i
, oid
[128], oidlen
;
1080 if (! smiNode
|| ! (smiNode
->nodekind
1081 & (SMI_NODEKIND_SCALAR
| SMI_NODEKIND_COLUMN
))) {
1082 return asn1_print(ndo
, elem
);
1085 if (elem
->type
== BE_NOSUCHOBJECT
1086 || elem
->type
== BE_NOSUCHINST
1087 || elem
->type
== BE_ENDOFMIBVIEW
) {
1088 return asn1_print(ndo
, elem
);
1091 if (NOTIFY_CLASS(pduid
) && smiNode
->access
< SMI_ACCESS_NOTIFY
) {
1092 ND_PRINT("[notNotifyable]");
1095 if (READ_CLASS(pduid
) && smiNode
->access
< SMI_ACCESS_READ_ONLY
) {
1096 ND_PRINT("[notReadable]");
1099 if (WRITE_CLASS(pduid
) && smiNode
->access
< SMI_ACCESS_READ_WRITE
) {
1100 ND_PRINT("[notWritable]");
1103 if (RESPONSE_CLASS(pduid
)
1104 && smiNode
->access
== SMI_ACCESS_NOT_ACCESSIBLE
) {
1105 ND_PRINT("[noAccess]");
1108 smiType
= smiGetNodeType(smiNode
);
1110 return asn1_print(ndo
, elem
);
1113 if (! smi_check_type(smiType
->basetype
, elem
->type
)) {
1114 ND_PRINT("[wrongType]");
1117 if (! smi_check_range(smiType
, elem
)) {
1118 ND_PRINT("[outOfRange]");
1121 /* resolve bits to named bits */
1123 /* check whether instance identifier is valid */
1125 /* apply display hints (integer, octetstring) */
1127 /* convert instance identifier to index type values */
1129 switch (elem
->type
) {
1131 if (smiType
->basetype
== SMI_BASETYPE_BITS
) {
1132 /* print bit labels */
1134 if (nd_smi_module_loaded
&&
1135 smi_decode_oid(ndo
, elem
, oid
,
1136 sizeof(oid
)/sizeof(unsigned int),
1138 smiNode
= smiGetNodeByOID(oidlen
, oid
);
1140 if (ndo
->ndo_vflag
) {
1141 ND_PRINT("%s::", smiGetNodeModule(smiNode
)->name
);
1143 ND_PRINT("%s", smiNode
->name
);
1144 if (smiNode
->oidlen
< oidlen
) {
1145 for (i
= smiNode
->oidlen
;
1147 ND_PRINT(".%u", oid
[i
]);
1157 if (smiType
->basetype
== SMI_BASETYPE_ENUM
) {
1158 for (nn
= smiGetFirstNamedNumber(smiType
);
1160 nn
= smiGetNextNamedNumber(nn
)) {
1161 if (nn
->value
.value
.integer32
1162 == elem
->data
.integer
) {
1163 ND_PRINT("%s", nn
->name
);
1164 ND_PRINT("(%d)", elem
->data
.integer
);
1174 return asn1_print(ndo
, elem
);
1181 * General SNMP header
1183 * version INTEGER {version-1(0)},
1184 * community OCTET STRING,
1187 * PDUs for all but Trap: (see rfc1157 from page 15 on)
1189 * request-id INTEGER,
1190 * error-status INTEGER,
1191 * error-index INTEGER,
1192 * varbindlist SEQUENCE OF
1200 * enterprise OBJECT IDENTIFIER,
1201 * agent-addr NetworkAddress,
1202 * generic-trap INTEGER,
1203 * specific-trap INTEGER,
1204 * time-stamp TimeTicks,
1205 * varbindlist SEQUENCE OF
1214 * Decode SNMP varBind
1217 varbind_print(netdissect_options
*ndo
,
1218 u_short pduid
, const u_char
*np
, u_int length
)
1223 SmiNode
*smiNode
= NULL
;
1227 /* Sequence of varBind */
1228 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1230 if (elem
.type
!= BE_SEQ
) {
1231 ND_PRINT("[!SEQ of varbind]");
1232 asn1_print(ndo
, &elem
);
1235 if ((u_int
)count
< length
)
1236 ND_PRINT("[%d extra after SEQ of varbind]", length
- count
);
1238 length
= elem
.asnlen
;
1239 np
= (const u_char
*)elem
.data
.raw
;
1241 for (ind
= 1; length
> 0; ind
++) {
1242 const u_char
*vbend
;
1248 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1250 if (elem
.type
!= BE_SEQ
) {
1251 ND_PRINT("[!varbind]");
1252 asn1_print(ndo
, &elem
);
1256 vblength
= length
- count
;
1258 length
= elem
.asnlen
;
1259 np
= (const u_char
*)elem
.data
.raw
;
1262 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1264 if (elem
.type
!= BE_OID
) {
1265 ND_PRINT("[objName!=OID]");
1266 asn1_print(ndo
, &elem
);
1270 smiNode
= smi_print_variable(ndo
, &elem
, &status
);
1272 status
= asn1_print(ndo
, &elem
);
1279 if (pduid
!= GETREQ
&& pduid
!= GETNEXTREQ
1280 && pduid
!= GETBULKREQ
)
1284 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1286 if (pduid
== GETREQ
|| pduid
== GETNEXTREQ
1287 || pduid
== GETBULKREQ
) {
1288 if (elem
.type
!= BE_NULL
) {
1289 ND_PRINT("[objVal!=NULL]");
1290 if (asn1_print(ndo
, &elem
) < 0)
1294 if (elem
.type
!= BE_NULL
) {
1296 status
= smi_print_value(ndo
, smiNode
, pduid
, &elem
);
1298 status
= asn1_print(ndo
, &elem
);
1310 * Decode SNMP PDUs: GetRequest, GetNextRequest, GetResponse, SetRequest,
1311 * GetBulk, Inform, V2Trap, and Report
1314 snmppdu_print(netdissect_options
*ndo
,
1315 u_short pduid
, const u_char
*np
, u_int length
)
1318 int count
= 0, error_status
;
1320 /* reqId (Integer) */
1321 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1323 if (elem
.type
!= BE_INT
) {
1324 ND_PRINT("[reqId!=INT]");
1325 asn1_print(ndo
, &elem
);
1329 ND_PRINT("R=%d ", elem
.data
.integer
);
1333 /* errorStatus (Integer) */
1334 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1336 if (elem
.type
!= BE_INT
) {
1337 ND_PRINT("[errorStatus!=INT]");
1338 asn1_print(ndo
, &elem
);
1342 if ((pduid
== GETREQ
|| pduid
== GETNEXTREQ
|| pduid
== SETREQ
1343 || pduid
== INFORMREQ
|| pduid
== V2TRAP
|| pduid
== REPORT
)
1344 && elem
.data
.integer
!= 0) {
1346 ND_PRINT("[errorStatus(%s)!=0]",
1347 DECODE_ErrorStatus(elem
.data
.integer
));
1348 } else if (pduid
== GETBULKREQ
) {
1349 ND_PRINT(" N=%d", elem
.data
.integer
);
1350 } else if (elem
.data
.integer
!= 0) {
1352 ND_PRINT(" %s", DECODE_ErrorStatus(elem
.data
.integer
));
1353 error_status
= elem
.data
.integer
;
1358 /* errorIndex (Integer) */
1359 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1361 if (elem
.type
!= BE_INT
) {
1362 ND_PRINT("[errorIndex!=INT]");
1363 asn1_print(ndo
, &elem
);
1366 if ((pduid
== GETREQ
|| pduid
== GETNEXTREQ
|| pduid
== SETREQ
1367 || pduid
== INFORMREQ
|| pduid
== V2TRAP
|| pduid
== REPORT
)
1368 && elem
.data
.integer
!= 0)
1369 ND_PRINT("[errorIndex(%d)!=0]", elem
.data
.integer
);
1370 else if (pduid
== GETBULKREQ
)
1371 ND_PRINT(" M=%d", elem
.data
.integer
);
1372 else if (elem
.data
.integer
!= 0) {
1374 ND_PRINT("[errorIndex(%d) w/o errorStatus]", elem
.data
.integer
);
1376 ND_PRINT("@%d", elem
.data
.integer
);
1377 } else if (error_status
) {
1378 ND_PRINT("[errorIndex==0]");
1383 varbind_print(ndo
, pduid
, np
, length
);
1388 * Decode SNMP Trap PDU
1391 trappdu_print(netdissect_options
*ndo
,
1392 const u_char
*np
, u_int length
)
1395 int count
= 0, generic
;
1399 /* enterprise (oid) */
1400 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1402 if (elem
.type
!= BE_OID
) {
1403 ND_PRINT("[enterprise!=OID]");
1404 asn1_print(ndo
, &elem
);
1407 if (asn1_print(ndo
, &elem
) < 0)
1414 /* agent-addr (inetaddr) */
1415 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1417 if (elem
.type
!= BE_INETADDR
) {
1418 ND_PRINT("[agent-addr!=INETADDR]");
1419 asn1_print(ndo
, &elem
);
1422 if (asn1_print(ndo
, &elem
) < 0)
1427 /* generic-trap (Integer) */
1428 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1430 if (elem
.type
!= BE_INT
) {
1431 ND_PRINT("[generic-trap!=INT]");
1432 asn1_print(ndo
, &elem
);
1435 generic
= elem
.data
.integer
;
1438 ND_PRINT(" %s", DECODE_GenericTrap(generic
));
1443 /* specific-trap (Integer) */
1444 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1446 if (elem
.type
!= BE_INT
) {
1447 ND_PRINT("[specific-trap!=INT]");
1448 asn1_print(ndo
, &elem
);
1451 if (generic
!= GT_ENTERPRISE
) {
1452 if (elem
.data
.integer
!= 0)
1453 ND_PRINT("[specific-trap(%d)!=0]", elem
.data
.integer
);
1455 ND_PRINT(" s=%d", elem
.data
.integer
);
1461 /* time-stamp (TimeTicks) */
1462 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1464 if (elem
.type
!= BE_UNS
) { /* XXX */
1465 ND_PRINT("[time-stamp!=TIMETICKS]");
1466 asn1_print(ndo
, &elem
);
1469 if (asn1_print(ndo
, &elem
) < 0)
1474 varbind_print(ndo
, TRAP
, np
, length
);
1479 * Decode arbitrary SNMP PDUs.
1482 pdu_print(netdissect_options
*ndo
,
1483 const u_char
*np
, u_int length
, int version
)
1489 if ((count
= asn1_parse(ndo
, np
, length
, &pdu
)) < 0)
1491 if (pdu
.type
!= BE_PDU
) {
1492 ND_PRINT("[no PDU]");
1495 if ((u_int
)count
< length
)
1496 ND_PRINT("[%d extra after PDU]", length
- count
);
1497 if (ndo
->ndo_vflag
) {
1500 if (asn1_print(ndo
, &pdu
) < 0)
1503 /* descend into PDU */
1504 length
= pdu
.asnlen
;
1505 np
= (const u_char
*)pdu
.data
.raw
;
1507 if (version
== SNMP_VERSION_1
&&
1508 (pdu
.id
== GETBULKREQ
|| pdu
.id
== INFORMREQ
||
1509 pdu
.id
== V2TRAP
|| pdu
.id
== REPORT
)) {
1510 ND_PRINT("[v2 PDU in v1 message]");
1514 if (version
== SNMP_VERSION_2
&& pdu
.id
== TRAP
) {
1515 ND_PRINT("[v1 PDU in v2 message]");
1521 trappdu_print(ndo
, np
, length
);
1531 snmppdu_print(ndo
, pdu
.id
, np
, length
);
1535 if (ndo
->ndo_vflag
) {
1541 * Decode a scoped SNMP PDU.
1544 scopedpdu_print(netdissect_options
*ndo
,
1545 const u_char
*np
, u_int length
, int version
)
1551 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1553 if (elem
.type
!= BE_SEQ
) {
1554 ND_PRINT("[!scoped PDU]");
1555 asn1_print(ndo
, &elem
);
1558 length
= elem
.asnlen
;
1559 np
= (const u_char
*)elem
.data
.raw
;
1561 /* contextEngineID (OCTET STRING) */
1562 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1564 if (elem
.type
!= BE_STR
) {
1565 ND_PRINT("[contextEngineID!=STR]");
1566 asn1_print(ndo
, &elem
);
1573 if (asn1_print_octets(ndo
, &elem
) == -1)
1577 /* contextName (OCTET STRING) */
1578 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1580 if (elem
.type
!= BE_STR
) {
1581 ND_PRINT("[contextName!=STR]");
1582 asn1_print(ndo
, &elem
);
1589 if (asn1_print_string(ndo
, &elem
) == -1)
1593 pdu_print(ndo
, np
, length
, version
);
1597 * Decode SNMP Community Header (SNMPv1 and SNMPv2c)
1600 community_print(netdissect_options
*ndo
,
1601 const u_char
*np
, u_int length
, int version
)
1606 /* Community (String) */
1607 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1609 if (elem
.type
!= BE_STR
) {
1610 ND_PRINT("[comm!=STR]");
1611 asn1_print(ndo
, &elem
);
1614 /* default community */
1615 if (!(elem
.asnlen
== sizeof(DEF_COMMUNITY
) - 1 &&
1616 strncmp((const char *)elem
.data
.str
, DEF_COMMUNITY
,
1617 sizeof(DEF_COMMUNITY
) - 1) == 0)) {
1620 if (asn1_print_string(ndo
, &elem
) == -1)
1627 pdu_print(ndo
, np
, length
, version
);
1631 * Decode SNMPv3 User-based Security Message Header (SNMPv3)
1634 usm_print(netdissect_options
*ndo
,
1635 const u_char
*np
, u_int length
)
1641 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1643 if (elem
.type
!= BE_SEQ
) {
1645 asn1_print(ndo
, &elem
);
1648 length
= elem
.asnlen
;
1649 np
= (const u_char
*)elem
.data
.raw
;
1651 /* msgAuthoritativeEngineID (OCTET STRING) */
1652 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1654 if (elem
.type
!= BE_STR
) {
1655 ND_PRINT("[msgAuthoritativeEngineID!=STR]");
1656 asn1_print(ndo
, &elem
);
1662 /* msgAuthoritativeEngineBoots (INTEGER) */
1663 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1665 if (elem
.type
!= BE_INT
) {
1666 ND_PRINT("[msgAuthoritativeEngineBoots!=INT]");
1667 asn1_print(ndo
, &elem
);
1671 ND_PRINT("B=%d ", elem
.data
.integer
);
1675 /* msgAuthoritativeEngineTime (INTEGER) */
1676 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1678 if (elem
.type
!= BE_INT
) {
1679 ND_PRINT("[msgAuthoritativeEngineTime!=INT]");
1680 asn1_print(ndo
, &elem
);
1684 ND_PRINT("T=%d ", elem
.data
.integer
);
1688 /* msgUserName (OCTET STRING) */
1689 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1691 if (elem
.type
!= BE_STR
) {
1692 ND_PRINT("[msgUserName!=STR]");
1693 asn1_print(ndo
, &elem
);
1700 if (asn1_print_string(ndo
, &elem
) == -1)
1704 /* msgAuthenticationParameters (OCTET STRING) */
1705 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1707 if (elem
.type
!= BE_STR
) {
1708 ND_PRINT("[msgAuthenticationParameters!=STR]");
1709 asn1_print(ndo
, &elem
);
1715 /* msgPrivacyParameters (OCTET STRING) */
1716 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1718 if (elem
.type
!= BE_STR
) {
1719 ND_PRINT("[msgPrivacyParameters!=STR]");
1720 asn1_print(ndo
, &elem
);
1726 if ((u_int
)count
< length
)
1727 ND_PRINT("[%d extra after usm SEQ]", length
- count
);
1731 * Decode SNMPv3 Message Header (SNMPv3)
1734 v3msg_print(netdissect_options
*ndo
,
1735 const u_char
*np
, u_int length
)
1741 const u_char
*xnp
= np
;
1742 int xlength
= length
;
1745 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1747 if (elem
.type
!= BE_SEQ
) {
1748 ND_PRINT("[!message]");
1749 asn1_print(ndo
, &elem
);
1752 length
= elem
.asnlen
;
1753 np
= (const u_char
*)elem
.data
.raw
;
1755 if (ndo
->ndo_vflag
) {
1759 /* msgID (INTEGER) */
1760 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1762 if (elem
.type
!= BE_INT
) {
1763 ND_PRINT("[msgID!=INT]");
1764 asn1_print(ndo
, &elem
);
1770 /* msgMaxSize (INTEGER) */
1771 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1773 if (elem
.type
!= BE_INT
) {
1774 ND_PRINT("[msgMaxSize!=INT]");
1775 asn1_print(ndo
, &elem
);
1781 /* msgFlags (OCTET STRING) */
1782 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1784 if (elem
.type
!= BE_STR
) {
1785 ND_PRINT("[msgFlags!=STR]");
1786 asn1_print(ndo
, &elem
);
1789 if (elem
.asnlen
!= 1) {
1790 ND_PRINT("[msgFlags size %d]", elem
.asnlen
);
1793 flags
= GET_U_1(elem
.data
.str
);
1794 if (flags
!= 0x00 && flags
!= 0x01 && flags
!= 0x03
1795 && flags
!= 0x04 && flags
!= 0x05 && flags
!= 0x07) {
1796 ND_PRINT("[msgFlags=0x%02X]", flags
);
1802 ND_PRINT("F=%s%s%s ",
1803 flags
& 0x01 ? "a" : "",
1804 flags
& 0x02 ? "p" : "",
1805 flags
& 0x04 ? "r" : "");
1807 /* msgSecurityModel (INTEGER) */
1808 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1810 if (elem
.type
!= BE_INT
) {
1811 ND_PRINT("[msgSecurityModel!=INT]");
1812 asn1_print(ndo
, &elem
);
1815 model
= elem
.data
.integer
;
1819 if ((u_int
)count
< length
)
1820 ND_PRINT("[%d extra after message SEQ]", length
- count
);
1822 if (ndo
->ndo_vflag
) {
1827 if (ndo
->ndo_vflag
) {
1831 ND_PRINT("[security model %d]", model
);
1835 np
= xnp
+ (np
- xnp
);
1836 length
= xlength
- (np
- xnp
);
1838 /* msgSecurityParameters (OCTET STRING) */
1839 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1841 if (elem
.type
!= BE_STR
) {
1842 ND_PRINT("[msgSecurityParameters!=STR]");
1843 asn1_print(ndo
, &elem
);
1850 usm_print(ndo
, elem
.data
.str
, elem
.asnlen
);
1851 if (ndo
->ndo_vflag
) {
1856 if (ndo
->ndo_vflag
) {
1857 ND_PRINT("{ ScopedPDU ");
1860 scopedpdu_print(ndo
, np
, length
, 3);
1862 if (ndo
->ndo_vflag
) {
1868 * Decode SNMP header and pass on to PDU printing routines
1871 snmp_print(netdissect_options
*ndo
,
1872 const u_char
*np
, u_int length
)
1878 ndo
->ndo_protocol
= "snmp";
1881 /* initial Sequence */
1882 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1884 if (elem
.type
!= BE_SEQ
) {
1885 ND_PRINT("[!init SEQ]");
1886 asn1_print(ndo
, &elem
);
1889 if ((u_int
)count
< length
)
1890 ND_PRINT("[%d extra after iSEQ]", length
- count
);
1892 length
= elem
.asnlen
;
1893 np
= (const u_char
*)elem
.data
.raw
;
1895 /* Version (INTEGER) */
1896 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1898 if (elem
.type
!= BE_INT
) {
1899 ND_PRINT("[version!=INT]");
1900 asn1_print(ndo
, &elem
);
1904 switch (elem
.data
.integer
) {
1905 case SNMP_VERSION_1
:
1906 case SNMP_VERSION_2
:
1907 case SNMP_VERSION_3
:
1909 ND_PRINT("{ %s ", SnmpVersion
[elem
.data
.integer
]);
1912 ND_PRINT("SNMP [version = %d]", elem
.data
.integer
);
1915 version
= elem
.data
.integer
;
1920 case SNMP_VERSION_1
:
1921 case SNMP_VERSION_2
:
1922 community_print(ndo
, np
, length
, version
);
1924 case SNMP_VERSION_3
:
1925 v3msg_print(ndo
, np
, length
);
1928 ND_PRINT("[version = %d]", elem
.data
.integer
);
1932 if (ndo
->ndo_vflag
) {