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 #include "netdissect.h"
80 #undef OPAQUE /* defined in <wingdi.h> */
84 * Universal ASN.1 types
85 * (we only care about the tag values for those allowed in the Internet SMI)
87 static const char *Universal
[] = {
100 "U-8","U-9","U-10","U-11", /* 8-11 */
101 "U-12","U-13","U-14","U-15", /* 12-15 */
108 * Application-wide ASN.1 types from the Internet SMI and their tags
110 static const char *Application
[] = {
127 * Context-specific ASN.1 types for the SNMP PDUs and their tags
129 static const char *Context
[] = {
150 #define NOTIFY_CLASS(x) (x == TRAP || x == V2TRAP || x == INFORMREQ)
151 #define READ_CLASS(x) (x == GETREQ || x == GETNEXTREQ || x == GETBULKREQ)
152 #define WRITE_CLASS(x) (x == SETREQ)
153 #define RESPONSE_CLASS(x) (x == GETRESP)
154 #define INTERNAL_CLASS(x) (x == REPORT)
157 * Context-specific ASN.1 types for the SNMP Exceptions and their tags
159 static const char *Exceptions
[] = {
161 #define NOSUCHOBJECT 0
163 #define NOSUCHINSTANCE 1
165 #define ENDOFMIBVIEW 2
169 * Private ASN.1 types
170 * The Internet SMI does not specify any
172 static const char *Private
[] = {
177 * error-status values for any SNMP PDU
179 static const char *ErrorStatus
[] = {
193 "resourceUnavailable",
196 "authorizationError",
200 #define DECODE_ErrorStatus(e) \
201 ( e >= 0 && (size_t)e < sizeof(ErrorStatus)/sizeof(ErrorStatus[0]) \
203 : (snprintf(errbuf, sizeof(errbuf), "err=%u", e), errbuf))
206 * generic-trap values in the SNMP Trap-PDU
208 static const char *GenericTrap
[] = {
213 "authenticationFailure",
216 #define GT_ENTERPRISE 6
218 #define DECODE_GenericTrap(t) \
219 ( t >= 0 && (size_t)t < sizeof(GenericTrap)/sizeof(GenericTrap[0]) \
221 : (snprintf(buf, sizeof(buf), "gt=%d", t), buf))
224 * ASN.1 type class table
225 * Ties together the preceding Universal, Application, Context, and Private
228 #define defineCLASS(x) { "x", x, sizeof(x)/sizeof(x[0]) } /* not ANSI-C */
229 static const struct {
234 defineCLASS(Universal
),
236 defineCLASS(Application
),
237 #define APPLICATION 1
238 defineCLASS(Context
),
240 defineCLASS(Private
),
242 defineCLASS(Exceptions
),
247 * defined forms for ASN.1 types
249 static const char *Form
[] = {
253 #define CONSTRUCTED 1
257 * A structure for the OID tree for the compiled-in MIB.
258 * This is stored as a general-order tree.
261 const char *desc
; /* name of object */
262 u_char oid
; /* sub-id following parent */
263 u_char type
; /* object type (unused) */
264 struct obj
*child
, *next
; /* child and next sibling pointers */
268 * Include the compiled in SNMP MIB. "mib.h" is produced by feeding
269 * RFC-1156 format files into "makemib". "mib.h" MUST define at least
270 * a value for `mibroot'.
272 * In particular, this is gross, as this is including initialized structures,
273 * and by right shouldn't be an "include" file.
278 * This defines a list of OIDs which will be abbreviated on output.
279 * Currently, this includes the prefixes for the Internet MIB, the
280 * private enterprises tree, and the experimental tree.
282 #define OID_FIRST_OCTET(x, y) (((x)*40) + (y)) /* X.690 8.19.4 */
285 static const uint8_t mib_oid
[] = { OID_FIRST_OCTET(1, 3), 6, 1, 2, 1 };
287 #ifndef NO_ABREV_ENTER
288 static const uint8_t enterprises_oid
[] = { OID_FIRST_OCTET(1, 3), 6, 1, 4, 1 };
290 #ifndef NO_ABREV_EXPERI
291 static const uint8_t experimental_oid
[] = { OID_FIRST_OCTET(1, 3), 6, 1, 3 };
293 #ifndef NO_ABBREV_SNMPMODS
294 static const uint8_t snmpModules_oid
[] = { OID_FIRST_OCTET(1, 3), 6, 1, 6, 3 };
297 #define OBJ_ABBREV_ENTRY(prefix, obj) \
298 { prefix, &_ ## obj ## _obj, obj ## _oid, sizeof (obj ## _oid) }
299 static const struct obj_abrev
{
300 const char *prefix
; /* prefix for this abrev */
301 struct obj
*node
; /* pointer into object table */
302 const uint8_t *oid
; /* ASN.1 encoded OID */
303 size_t oid_len
; /* length of OID */
304 } obj_abrev_list
[] = {
306 /* .iso.org.dod.internet.mgmt.mib */
307 OBJ_ABBREV_ENTRY("", mib
),
309 #ifndef NO_ABREV_ENTER
310 /* .iso.org.dod.internet.private.enterprises */
311 OBJ_ABBREV_ENTRY("E:", enterprises
),
313 #ifndef NO_ABREV_EXPERI
314 /* .iso.org.dod.internet.experimental */
315 OBJ_ABBREV_ENTRY("X:", experimental
),
317 #ifndef NO_ABBREV_SNMPMODS
318 /* .iso.org.dod.internet.snmpV2.snmpModules */
319 OBJ_ABBREV_ENTRY("S:", snmpModules
),
325 * This is used in the OID print routine to walk down the object tree
326 * rooted at `mibroot'.
328 #define OBJ_PRINT(o, suppressdot) \
332 if ((o) == objp->oid) \
334 } while ((objp = objp->next) != NULL); \
337 ND_PRINT(suppressdot?"%s":".%s", objp->desc); \
338 objp = objp->child; \
340 ND_PRINT(suppressdot?"%u":".%u", (o)); \
344 * This is the definition for the Any-Data-Type storage used purely for
345 * temporary internal representation while decoding an ASN.1 data stream.
357 u_char form
, class; /* tag info */
368 #define BE_INETADDR 8
371 #define BE_NOSUCHOBJECT 128
372 #define BE_NOSUCHINST 129
373 #define BE_ENDOFMIBVIEW 130
377 * SNMP versions recognized by this module
379 static const char *SnmpVersion
[] = {
381 #define SNMP_VERSION_1 0
383 #define SNMP_VERSION_2 1
385 #define SNMP_VERSION_2U 2
387 #define SNMP_VERSION_3 3
391 * Defaults for SNMP PDU components
393 #define DEF_COMMUNITY "public"
396 * constants for ASN.1 decoding
399 #define ASNLEN_INETADDR 4
402 #define ASN_BIT8 0x80
403 #define ASN_LONGLEN 0x80
405 #define ASN_ID_BITS 0x1f
406 #define ASN_FORM_BITS 0x20
407 #define ASN_FORM_SHIFT 5
408 #define ASN_CLASS_BITS 0xc0
409 #define ASN_CLASS_SHIFT 6
411 #define ASN_ID_EXT 0x1f /* extension ID in tag field */
414 * This decodes the next ASN.1 object in the stream pointed to by "p"
415 * (and of real-length "len") and stores the intermediate data in the
416 * provided BE object.
418 * This returns -l if it fails (i.e., the ASN.1 stream is not valid).
419 * O/w, this returns the number of bytes parsed from "p".
422 asn1_parse(netdissect_options
*ndo
,
423 const u_char
*p
, u_int len
, struct be
*elem
)
425 u_char form
, class, id
;
431 ND_PRINT("[nothing to parse]");
436 * it would be nice to use a bit field, but you can't depend on them.
437 * +---+---+---+---+---+---+---+---+
439 * +---+---+---+---+---+---+---+---+
442 id
= GET_U_1(p
) & ASN_ID_BITS
; /* lower 5 bits, range 00-1f */
444 form
= (GET_U_1(p
) & 0xe0) >> 5; /* move upper 3 bits to lower 3 */
445 class = form
>> 1; /* bits 7&6 -> bits 1&0, range 0-3 */
446 form
&= 0x1; /* bit 5 -> bit 0, range 0-1 */
448 form
= (u_char
)(GET_U_1(p
) & ASN_FORM_BITS
) >> ASN_FORM_SHIFT
;
449 class = (u_char
)(GET_U_1(p
) & ASN_CLASS_BITS
) >> ASN_CLASS_SHIFT
;
455 /* extended tag field */
456 if (id
== ASN_ID_EXT
) {
458 * The ID follows, as a sequence of octets with the
459 * 8th bit set and the remaining 7 bits being
460 * the next 7 bits of the value, terminated with
461 * an octet with the 8th bit not set.
463 * First, assemble all the octets with the 8th
464 * bit set. XXX - this doesn't handle a value
465 * that won't fit in 32 bits.
468 while (GET_U_1(p
) & ASN_BIT8
) {
470 ND_PRINT("[Xtagfield?]");
473 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)
750 p
= (const u_char
*)elem
->data
.raw
;
752 if (!ndo
->ndo_nflag
&& asnlen
> 2) {
753 const struct obj_abrev
*a
= &obj_abrev_list
[0];
754 for (; a
->node
; a
++) {
757 if (!ND_TTEST_LEN(p
, a
->oid_len
))
759 if (memcmp(a
->oid
, p
, a
->oid_len
) == 0) {
760 objp
= a
->node
->child
;
763 ND_PRINT("%s", a
->prefix
);
770 for (; i
!= 0; p
++, i
--) {
771 o
= (o
<< ASN_SHIFT7
) + (GET_U_1(p
) & ~ASN_BIT8
);
772 if (GET_U_1(p
) & ASN_LONGLEN
)
776 * first subitem encodes two items with
778 * (see X.690:1997 clause 8.19 for the details)
799 ND_PRINT("%d", elem
->data
.integer
);
803 ND_PRINT("%u", elem
->data
.uns
);
807 ND_PRINT("%" PRIu64
, elem
->data
.uns64
);
811 if (asn1_print_string(ndo
, elem
) == -1)
816 ND_PRINT("Seq(%u)", elem
->asnlen
);
820 if (asnlen
!= ASNLEN_INETADDR
)
821 ND_PRINT("[inetaddr len!=%d]", ASNLEN_INETADDR
);
822 p
= (const u_char
*)elem
->data
.raw
;
823 ND_TCHECK_LEN(p
, asnlen
);
824 for (i
= asnlen
; i
!= 0; p
++, i
--) {
825 ND_PRINT((i
== asnlen
) ? "%u" : ".%u", GET_U_1(p
));
829 case BE_NOSUCHOBJECT
:
831 case BE_ENDOFMIBVIEW
:
832 ND_PRINT("[%s]", Class
[EXCEPTIONS
].Id
[elem
->id
]);
836 ND_PRINT("%s(%u)", Class
[CONTEXT
].Id
[elem
->id
], elem
->asnlen
);
840 ND_PRINT("[BE_ANY!?]");
856 * This is a brute force ASN.1 printer: recurses to dump an entire structure.
857 * This will work for any ASN.1 stream, not just an SNMP PDU.
859 * By adding newlines and spaces at the correct places, this would print in
862 * This is not currently used.
865 asn1_decode(u_char
*p
, u_int length
)
870 while (i
>= 0 && length
> 0) {
871 i
= asn1_parse(ndo
, p
, length
, &elem
);
874 if (asn1_print(ndo
, &elem
) < 0)
876 if (elem
.type
== BE_SEQ
|| elem
.type
== BE_PDU
) {
878 asn1_decode(elem
.data
.raw
, elem
.asnlen
);
891 SmiBasetype basetype
;
895 static const struct smi2be smi2betab
[] = {
896 { SMI_BASETYPE_INTEGER32
, BE_INT
},
897 { SMI_BASETYPE_OCTETSTRING
, BE_STR
},
898 { SMI_BASETYPE_OCTETSTRING
, BE_INETADDR
},
899 { SMI_BASETYPE_OBJECTIDENTIFIER
, BE_OID
},
900 { SMI_BASETYPE_UNSIGNED32
, BE_UNS
},
901 { SMI_BASETYPE_INTEGER64
, BE_NONE
},
902 { SMI_BASETYPE_UNSIGNED64
, BE_UNS64
},
903 { SMI_BASETYPE_FLOAT32
, BE_NONE
},
904 { SMI_BASETYPE_FLOAT64
, BE_NONE
},
905 { SMI_BASETYPE_FLOAT128
, BE_NONE
},
906 { SMI_BASETYPE_ENUM
, BE_INT
},
907 { SMI_BASETYPE_BITS
, BE_STR
},
908 { SMI_BASETYPE_UNKNOWN
, BE_NONE
}
912 smi_decode_oid(netdissect_options
*ndo
,
913 struct be
*elem
, unsigned int *oid
,
914 unsigned int oidsize
, unsigned int *oidlen
)
916 const u_char
*p
= (const u_char
*)elem
->data
.raw
;
917 uint32_t asnlen
= elem
->asnlen
;
919 int o
= 0, first
= -1;
920 unsigned int firstval
;
922 for (*oidlen
= 0; i
!= 0; p
++, i
--) {
923 o
= (o
<< ASN_SHIFT7
) + (GET_U_1(p
) & ~ASN_BIT8
);
924 if (GET_U_1(p
) & ASN_LONGLEN
)
928 * first subitem encodes two items with 1st*OIDMUX+2nd
929 * (see X.690:1997 clause 8.19 for the details)
933 firstval
= o
/ OIDMUX
;
934 if (firstval
> 2) firstval
= 2;
935 o
-= firstval
* OIDMUX
;
936 if (*oidlen
< oidsize
) {
937 oid
[(*oidlen
)++] = firstval
;
940 if (*oidlen
< oidsize
) {
941 oid
[(*oidlen
)++] = o
;
948 static int smi_check_type(SmiBasetype basetype
, int be
)
952 for (i
= 0; smi2betab
[i
].basetype
!= SMI_BASETYPE_UNKNOWN
; i
++) {
953 if (smi2betab
[i
].basetype
== basetype
&& smi2betab
[i
].be
== be
) {
961 static int smi_check_a_range(SmiType
*smiType
, SmiRange
*smiRange
,
966 switch (smiType
->basetype
) {
967 case SMI_BASETYPE_OBJECTIDENTIFIER
:
968 case SMI_BASETYPE_OCTETSTRING
:
969 if (smiRange
->minValue
.value
.unsigned32
970 == smiRange
->maxValue
.value
.unsigned32
) {
971 ok
= (elem
->asnlen
== smiRange
->minValue
.value
.unsigned32
);
973 ok
= (elem
->asnlen
>= smiRange
->minValue
.value
.unsigned32
974 && elem
->asnlen
<= smiRange
->maxValue
.value
.unsigned32
);
978 case SMI_BASETYPE_INTEGER32
:
979 ok
= (elem
->data
.integer
>= smiRange
->minValue
.value
.integer32
980 && elem
->data
.integer
<= smiRange
->maxValue
.value
.integer32
);
983 case SMI_BASETYPE_UNSIGNED32
:
984 ok
= (elem
->data
.uns
>= smiRange
->minValue
.value
.unsigned32
985 && elem
->data
.uns
<= smiRange
->maxValue
.value
.unsigned32
);
988 case SMI_BASETYPE_UNSIGNED64
:
992 /* case SMI_BASETYPE_INTEGER64: SMIng */
993 /* case SMI_BASETYPE_FLOAT32: SMIng */
994 /* case SMI_BASETYPE_FLOAT64: SMIng */
995 /* case SMI_BASETYPE_FLOAT128: SMIng */
997 case SMI_BASETYPE_ENUM
:
998 case SMI_BASETYPE_BITS
:
999 case SMI_BASETYPE_UNKNOWN
:
1011 static int smi_check_range(SmiType
*smiType
, struct be
*elem
)
1016 for (smiRange
= smiGetFirstRange(smiType
);
1018 smiRange
= smiGetNextRange(smiRange
)) {
1020 ok
= smi_check_a_range(smiType
, smiRange
, elem
);
1028 SmiType
*parentType
;
1029 parentType
= smiGetParentType(smiType
);
1031 ok
= smi_check_range(parentType
, elem
);
1039 smi_print_variable(netdissect_options
*ndo
,
1040 struct be
*elem
, int *status
)
1042 unsigned int oid
[128], oidlen
;
1043 SmiNode
*smiNode
= NULL
;
1046 if (!nd_smi_module_loaded
) {
1047 *status
= asn1_print(ndo
, elem
);
1050 *status
= smi_decode_oid(ndo
, elem
, oid
, sizeof(oid
) / sizeof(unsigned int),
1054 smiNode
= smiGetNodeByOID(oidlen
, oid
);
1056 *status
= asn1_print(ndo
, elem
);
1059 if (ndo
->ndo_vflag
) {
1060 ND_PRINT("%s::", smiGetNodeModule(smiNode
)->name
);
1062 ND_PRINT("%s", smiNode
->name
);
1063 if (smiNode
->oidlen
< oidlen
) {
1064 for (i
= smiNode
->oidlen
; i
< oidlen
; i
++) {
1065 ND_PRINT(".%u", oid
[i
]);
1073 smi_print_value(netdissect_options
*ndo
,
1074 SmiNode
*smiNode
, u_short pduid
, struct be
*elem
)
1076 unsigned int i
, oid
[128], oidlen
;
1081 if (! smiNode
|| ! (smiNode
->nodekind
1082 & (SMI_NODEKIND_SCALAR
| SMI_NODEKIND_COLUMN
))) {
1083 return asn1_print(ndo
, elem
);
1086 if (elem
->type
== BE_NOSUCHOBJECT
1087 || elem
->type
== BE_NOSUCHINST
1088 || elem
->type
== BE_ENDOFMIBVIEW
) {
1089 return asn1_print(ndo
, elem
);
1092 if (NOTIFY_CLASS(pduid
) && smiNode
->access
< SMI_ACCESS_NOTIFY
) {
1093 ND_PRINT("[notNotifiable]");
1096 if (READ_CLASS(pduid
) && smiNode
->access
< SMI_ACCESS_READ_ONLY
) {
1097 ND_PRINT("[notReadable]");
1100 if (WRITE_CLASS(pduid
) && smiNode
->access
< SMI_ACCESS_READ_WRITE
) {
1101 ND_PRINT("[notWritable]");
1104 if (RESPONSE_CLASS(pduid
)
1105 && smiNode
->access
== SMI_ACCESS_NOT_ACCESSIBLE
) {
1106 ND_PRINT("[noAccess]");
1109 smiType
= smiGetNodeType(smiNode
);
1111 return asn1_print(ndo
, elem
);
1114 if (! smi_check_type(smiType
->basetype
, elem
->type
)) {
1115 ND_PRINT("[wrongType]");
1118 if (! smi_check_range(smiType
, elem
)) {
1119 ND_PRINT("[outOfRange]");
1122 /* resolve bits to named bits */
1124 /* check whether instance identifier is valid */
1126 /* apply display hints (integer, octetstring) */
1128 /* convert instance identifier to index type values */
1130 switch (elem
->type
) {
1132 if (smiType
->basetype
== SMI_BASETYPE_BITS
) {
1133 /* print bit labels */
1135 if (nd_smi_module_loaded
&&
1136 smi_decode_oid(ndo
, elem
, oid
,
1137 sizeof(oid
)/sizeof(unsigned int),
1139 smiNode
= smiGetNodeByOID(oidlen
, oid
);
1141 if (ndo
->ndo_vflag
) {
1142 ND_PRINT("%s::", smiGetNodeModule(smiNode
)->name
);
1144 ND_PRINT("%s", smiNode
->name
);
1145 if (smiNode
->oidlen
< oidlen
) {
1146 for (i
= smiNode
->oidlen
;
1148 ND_PRINT(".%u", oid
[i
]);
1158 if (smiType
->basetype
== SMI_BASETYPE_ENUM
) {
1159 for (nn
= smiGetFirstNamedNumber(smiType
);
1161 nn
= smiGetNextNamedNumber(nn
)) {
1162 if (nn
->value
.value
.integer32
1163 == elem
->data
.integer
) {
1164 ND_PRINT("%s", nn
->name
);
1165 ND_PRINT("(%d)", elem
->data
.integer
);
1175 return asn1_print(ndo
, elem
);
1182 * General SNMP header
1184 * version INTEGER {version-1(0)},
1185 * community OCTET STRING,
1188 * PDUs for all but Trap: (see rfc1157 from page 15 on)
1190 * request-id INTEGER,
1191 * error-status INTEGER,
1192 * error-index INTEGER,
1193 * varbindlist SEQUENCE OF
1201 * enterprise OBJECT IDENTIFIER,
1202 * agent-addr NetworkAddress,
1203 * generic-trap INTEGER,
1204 * specific-trap INTEGER,
1205 * time-stamp TimeTicks,
1206 * varbindlist SEQUENCE OF
1215 * Decode SNMP varBind
1218 varbind_print(netdissect_options
*ndo
,
1219 u_short pduid
, const u_char
*np
, u_int length
)
1224 SmiNode
*smiNode
= NULL
;
1228 /* Sequence of varBind */
1229 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1231 if (elem
.type
!= BE_SEQ
) {
1232 ND_PRINT("[!SEQ of varbind]");
1233 asn1_print(ndo
, &elem
);
1236 if ((u_int
)count
< length
)
1237 ND_PRINT("[%d extra after SEQ of varbind]", length
- count
);
1239 length
= elem
.asnlen
;
1240 np
= (const u_char
*)elem
.data
.raw
;
1243 const u_char
*vbend
;
1249 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1251 if (elem
.type
!= BE_SEQ
) {
1252 ND_PRINT("[!varbind]");
1253 asn1_print(ndo
, &elem
);
1257 vblength
= length
- count
;
1259 length
= elem
.asnlen
;
1260 np
= (const u_char
*)elem
.data
.raw
;
1263 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1265 if (elem
.type
!= BE_OID
) {
1266 ND_PRINT("[objName!=OID]");
1267 asn1_print(ndo
, &elem
);
1271 smiNode
= smi_print_variable(ndo
, &elem
, &status
);
1273 status
= asn1_print(ndo
, &elem
);
1280 if (pduid
!= GETREQ
&& pduid
!= GETNEXTREQ
1281 && pduid
!= GETBULKREQ
)
1285 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1287 if (pduid
== GETREQ
|| pduid
== GETNEXTREQ
1288 || pduid
== GETBULKREQ
) {
1289 if (elem
.type
!= BE_NULL
) {
1290 ND_PRINT("[objVal!=NULL]");
1291 if (asn1_print(ndo
, &elem
) < 0)
1295 if (elem
.type
!= BE_NULL
) {
1297 status
= smi_print_value(ndo
, smiNode
, pduid
, &elem
);
1299 status
= asn1_print(ndo
, &elem
);
1311 * Decode SNMP PDUs: GetRequest, GetNextRequest, GetResponse, SetRequest,
1312 * GetBulk, Inform, V2Trap, and Report
1315 snmppdu_print(netdissect_options
*ndo
,
1316 u_short pduid
, const u_char
*np
, u_int length
)
1319 int count
= 0, error_status
;
1321 /* reqId (Integer) */
1322 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1324 if (elem
.type
!= BE_INT
) {
1325 ND_PRINT("[reqId!=INT]");
1326 asn1_print(ndo
, &elem
);
1330 ND_PRINT("R=%d ", elem
.data
.integer
);
1334 /* errorStatus (Integer) */
1335 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1337 if (elem
.type
!= BE_INT
) {
1338 ND_PRINT("[errorStatus!=INT]");
1339 asn1_print(ndo
, &elem
);
1343 if ((pduid
== GETREQ
|| pduid
== GETNEXTREQ
|| pduid
== SETREQ
1344 || pduid
== INFORMREQ
|| pduid
== V2TRAP
|| pduid
== REPORT
)
1345 && elem
.data
.integer
!= 0) {
1347 ND_PRINT("[errorStatus(%s)!=0]",
1348 DECODE_ErrorStatus(elem
.data
.integer
));
1349 } else if (pduid
== GETBULKREQ
) {
1350 ND_PRINT(" N=%d", elem
.data
.integer
);
1351 } else if (elem
.data
.integer
!= 0) {
1353 ND_PRINT(" %s", DECODE_ErrorStatus(elem
.data
.integer
));
1354 error_status
= elem
.data
.integer
;
1359 /* errorIndex (Integer) */
1360 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1362 if (elem
.type
!= BE_INT
) {
1363 ND_PRINT("[errorIndex!=INT]");
1364 asn1_print(ndo
, &elem
);
1367 if ((pduid
== GETREQ
|| pduid
== GETNEXTREQ
|| pduid
== SETREQ
1368 || pduid
== INFORMREQ
|| pduid
== V2TRAP
|| pduid
== REPORT
)
1369 && elem
.data
.integer
!= 0)
1370 ND_PRINT("[errorIndex(%d)!=0]", elem
.data
.integer
);
1371 else if (pduid
== GETBULKREQ
)
1372 ND_PRINT(" M=%d", elem
.data
.integer
);
1373 else if (elem
.data
.integer
!= 0) {
1375 ND_PRINT("[errorIndex(%d) w/o errorStatus]", elem
.data
.integer
);
1377 ND_PRINT("@%d", elem
.data
.integer
);
1378 } else if (error_status
) {
1379 ND_PRINT("[errorIndex==0]");
1384 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
);
1478 * Decode arbitrary SNMP PDUs.
1481 pdu_print(netdissect_options
*ndo
,
1482 const u_char
*np
, u_int length
, int version
)
1488 if ((count
= asn1_parse(ndo
, np
, length
, &pdu
)) < 0)
1490 if (pdu
.type
!= BE_PDU
) {
1491 ND_PRINT("[no PDU]");
1494 if ((u_int
)count
< length
)
1495 ND_PRINT("[%d extra after PDU]", length
- count
);
1496 if (ndo
->ndo_vflag
) {
1499 if (asn1_print(ndo
, &pdu
) < 0)
1502 /* descend into PDU */
1503 length
= pdu
.asnlen
;
1504 np
= (const u_char
*)pdu
.data
.raw
;
1506 if (version
== SNMP_VERSION_1
&&
1507 (pdu
.id
== GETBULKREQ
|| pdu
.id
== INFORMREQ
||
1508 pdu
.id
== V2TRAP
|| pdu
.id
== REPORT
)) {
1509 ND_PRINT("[v2 PDU in v1 message]");
1513 if (version
== SNMP_VERSION_2
&& pdu
.id
== TRAP
) {
1514 ND_PRINT("[v1 PDU in v2 message]");
1520 trappdu_print(ndo
, np
, length
);
1530 snmppdu_print(ndo
, pdu
.id
, np
, length
);
1534 if (ndo
->ndo_vflag
) {
1540 * Decode a scoped SNMP PDU.
1543 scopedpdu_print(netdissect_options
*ndo
,
1544 const u_char
*np
, u_int length
, int version
)
1550 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1552 if (elem
.type
!= BE_SEQ
) {
1553 ND_PRINT("[!scoped PDU]");
1554 asn1_print(ndo
, &elem
);
1557 length
= elem
.asnlen
;
1558 np
= (const u_char
*)elem
.data
.raw
;
1560 /* contextEngineID (OCTET STRING) */
1561 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1563 if (elem
.type
!= BE_STR
) {
1564 ND_PRINT("[contextEngineID!=STR]");
1565 asn1_print(ndo
, &elem
);
1572 if (asn1_print_octets(ndo
, &elem
) == -1)
1576 /* contextName (OCTET STRING) */
1577 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1579 if (elem
.type
!= BE_STR
) {
1580 ND_PRINT("[contextName!=STR]");
1581 asn1_print(ndo
, &elem
);
1588 if (asn1_print_string(ndo
, &elem
) == -1)
1592 pdu_print(ndo
, np
, length
, version
);
1596 * Decode SNMP Community Header (SNMPv1 and SNMPv2c)
1599 community_print(netdissect_options
*ndo
,
1600 const u_char
*np
, u_int length
, int version
)
1605 /* Community (String) */
1606 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1608 if (elem
.type
!= BE_STR
) {
1609 ND_PRINT("[comm!=STR]");
1610 asn1_print(ndo
, &elem
);
1613 /* default community */
1614 if (!(elem
.asnlen
== sizeof(DEF_COMMUNITY
) - 1 &&
1615 strncmp((const char *)elem
.data
.str
, DEF_COMMUNITY
,
1616 sizeof(DEF_COMMUNITY
) - 1) == 0)) {
1619 if (asn1_print_string(ndo
, &elem
) == -1)
1626 pdu_print(ndo
, np
, length
, version
);
1630 * Decode SNMPv3 User-based Security Message Header (SNMPv3)
1633 usm_print(netdissect_options
*ndo
,
1634 const u_char
*np
, u_int length
)
1640 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1642 if (elem
.type
!= BE_SEQ
) {
1644 asn1_print(ndo
, &elem
);
1647 length
= elem
.asnlen
;
1648 np
= (const u_char
*)elem
.data
.raw
;
1650 /* msgAuthoritativeEngineID (OCTET STRING) */
1651 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1653 if (elem
.type
!= BE_STR
) {
1654 ND_PRINT("[msgAuthoritativeEngineID!=STR]");
1655 asn1_print(ndo
, &elem
);
1661 /* msgAuthoritativeEngineBoots (INTEGER) */
1662 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1664 if (elem
.type
!= BE_INT
) {
1665 ND_PRINT("[msgAuthoritativeEngineBoots!=INT]");
1666 asn1_print(ndo
, &elem
);
1670 ND_PRINT("B=%d ", elem
.data
.integer
);
1674 /* msgAuthoritativeEngineTime (INTEGER) */
1675 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1677 if (elem
.type
!= BE_INT
) {
1678 ND_PRINT("[msgAuthoritativeEngineTime!=INT]");
1679 asn1_print(ndo
, &elem
);
1683 ND_PRINT("T=%d ", elem
.data
.integer
);
1687 /* msgUserName (OCTET STRING) */
1688 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1690 if (elem
.type
!= BE_STR
) {
1691 ND_PRINT("[msgUserName!=STR]");
1692 asn1_print(ndo
, &elem
);
1699 if (asn1_print_string(ndo
, &elem
) == -1)
1703 /* msgAuthenticationParameters (OCTET STRING) */
1704 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1706 if (elem
.type
!= BE_STR
) {
1707 ND_PRINT("[msgAuthenticationParameters!=STR]");
1708 asn1_print(ndo
, &elem
);
1714 /* msgPrivacyParameters (OCTET STRING) */
1715 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1717 if (elem
.type
!= BE_STR
) {
1718 ND_PRINT("[msgPrivacyParameters!=STR]");
1719 asn1_print(ndo
, &elem
);
1725 if ((u_int
)count
< length
)
1726 ND_PRINT("[%d extra after usm SEQ]", length
- count
);
1730 * Decode SNMPv3 Message Header (SNMPv3)
1733 v3msg_print(netdissect_options
*ndo
,
1734 const u_char
*np
, u_int length
)
1740 const u_char
*xnp
= np
;
1741 int xlength
= length
;
1744 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1746 if (elem
.type
!= BE_SEQ
) {
1747 ND_PRINT("[!message]");
1748 asn1_print(ndo
, &elem
);
1751 length
= elem
.asnlen
;
1752 np
= (const u_char
*)elem
.data
.raw
;
1754 if (ndo
->ndo_vflag
) {
1758 /* msgID (INTEGER) */
1759 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1761 if (elem
.type
!= BE_INT
) {
1762 ND_PRINT("[msgID!=INT]");
1763 asn1_print(ndo
, &elem
);
1769 /* msgMaxSize (INTEGER) */
1770 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1772 if (elem
.type
!= BE_INT
) {
1773 ND_PRINT("[msgMaxSize!=INT]");
1774 asn1_print(ndo
, &elem
);
1780 /* msgFlags (OCTET STRING) */
1781 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1783 if (elem
.type
!= BE_STR
) {
1784 ND_PRINT("[msgFlags!=STR]");
1785 asn1_print(ndo
, &elem
);
1788 if (elem
.asnlen
!= 1) {
1789 ND_PRINT("[msgFlags size %d]", elem
.asnlen
);
1792 flags
= GET_U_1(elem
.data
.str
);
1793 if (flags
!= 0x00 && flags
!= 0x01 && flags
!= 0x03
1794 && flags
!= 0x04 && flags
!= 0x05 && flags
!= 0x07) {
1795 ND_PRINT("[msgFlags=0x%02X]", flags
);
1801 ND_PRINT("F=%s%s%s ",
1802 flags
& 0x01 ? "a" : "",
1803 flags
& 0x02 ? "p" : "",
1804 flags
& 0x04 ? "r" : "");
1806 /* msgSecurityModel (INTEGER) */
1807 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1809 if (elem
.type
!= BE_INT
) {
1810 ND_PRINT("[msgSecurityModel!=INT]");
1811 asn1_print(ndo
, &elem
);
1814 model
= elem
.data
.integer
;
1818 if ((u_int
)count
< length
)
1819 ND_PRINT("[%d extra after message SEQ]", length
- count
);
1821 if (ndo
->ndo_vflag
) {
1826 if (ndo
->ndo_vflag
) {
1830 ND_PRINT("[security model %d]", model
);
1834 np
= xnp
+ (np
- xnp
);
1835 length
= xlength
- (np
- xnp
);
1837 /* msgSecurityParameters (OCTET STRING) */
1838 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1840 if (elem
.type
!= BE_STR
) {
1841 ND_PRINT("[msgSecurityParameters!=STR]");
1842 asn1_print(ndo
, &elem
);
1849 usm_print(ndo
, elem
.data
.str
, elem
.asnlen
);
1850 if (ndo
->ndo_vflag
) {
1855 if (ndo
->ndo_vflag
) {
1856 ND_PRINT("{ ScopedPDU ");
1859 scopedpdu_print(ndo
, np
, length
, 3);
1861 if (ndo
->ndo_vflag
) {
1867 * Decode SNMP header and pass on to PDU printing routines
1870 snmp_print(netdissect_options
*ndo
,
1871 const u_char
*np
, u_int length
)
1877 ndo
->ndo_protocol
= "snmp";
1880 /* initial Sequence */
1881 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1883 if (elem
.type
!= BE_SEQ
) {
1884 ND_PRINT("[!init SEQ]");
1885 asn1_print(ndo
, &elem
);
1888 if ((u_int
)count
< length
)
1889 ND_PRINT("[%d extra after iSEQ]", length
- count
);
1891 length
= elem
.asnlen
;
1892 np
= (const u_char
*)elem
.data
.raw
;
1894 /* Version (INTEGER) */
1895 if ((count
= asn1_parse(ndo
, np
, length
, &elem
)) < 0)
1897 if (elem
.type
!= BE_INT
) {
1898 ND_PRINT("[version!=INT]");
1899 asn1_print(ndo
, &elem
);
1903 switch (elem
.data
.integer
) {
1904 case SNMP_VERSION_1
:
1905 case SNMP_VERSION_2
:
1906 case SNMP_VERSION_3
:
1908 ND_PRINT("{ %s ", SnmpVersion
[elem
.data
.integer
]);
1911 ND_PRINT("SNMP [version = %d]", elem
.data
.integer
);
1914 version
= elem
.data
.integer
;
1919 case SNMP_VERSION_1
:
1920 case SNMP_VERSION_2
:
1921 community_print(ndo
, np
, length
, version
);
1923 case SNMP_VERSION_3
:
1924 v3msg_print(ndo
, np
, length
);
1927 ND_PRINT("[version = %d]", elem
.data
.integer
);
1931 if (ndo
->ndo_vflag
) {