2 * Routines for SNMP (simple network management protocol)
3 * Copyright (C) 1998 Didier Jorand
5 * See RFC 1157 for SNMPv1.
7 * See RFCs 1901, 1905, and 1906 for SNMPv2c.
9 * See RFCs 1905, 1906, 1909, and 1910 for SNMPv2u.
11 * $Id: packet-snmp.c,v 1.83 2002/03/10 22:18:12 guy Exp $
13 * Ethereal - Network traffic analyzer
14 * By Gerald Combs <gerald@ethereal.com>
15 * Copyright 1998 Gerald Combs
19 * GXSNMP -- An snmp mangament application
20 * Copyright (C) 1998 Gregory McLean & Jochen Friedrich
21 * Beholder RMON ethernet network monitor,Copyright (C) 1993 DNPAP group
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License
25 * as published by the Free Software Foundation; either version 2
26 * of the License, or (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
46 #ifdef HAVE_SYS_TYPES_H
47 # include <sys/types.h>
50 #ifdef HAVE_NETINET_IN_H
51 # include <netinet/in.h>
60 #include <epan/packet.h>
61 #include <epan/strutil.h>
62 #include <epan/conversation.h>
64 #include "packet-ipx.h"
66 #ifdef HAVE_UCD_SNMP_SNMP_H
67 # include <ucd-snmp/asn1.h>
68 # include <ucd-snmp/snmp_api.h>
69 # include <ucd-snmp/snmp_impl.h>
70 # include <ucd-snmp/mib.h>
71 # include <ucd-snmp/default_store.h>
72 # include <ucd-snmp/tools.h>
75 * Define values "sprint_realloc_value()" expects.
77 # define VALTYPE_INTEGER ASN_INTEGER
78 # define VALTYPE_COUNTER ASN_COUNTER
79 # define VALTYPE_GAUGE ASN_GAUGE
80 # define VALTYPE_TIMETICKS ASN_TIMETICKS
81 # define VALTYPE_STRING ASN_OCTET_STR
82 # define VALTYPE_IPADDR ASN_IPADDRESS
83 # define VALTYPE_OPAQUE ASN_OPAQUE
84 # define VALTYPE_NSAP ASN_NSAP
85 # define VALTYPE_OBJECTID ASN_OBJECT_ID
86 # define VALTYPE_BITSTR ASN_BIT_STR
87 # define VALTYPE_COUNTER64 ASN_COUNTER64
93 #include "packet-snmp.h"
95 /* Null string of type "guchar[]". */
96 static const guchar nullstring[] = "";
98 /* Take a pointer that may be null and return a pointer that's not null
99 by turning null pointers into pointers to the above null string. */
100 #define SAFE_STRING(s) (((s) != NULL) ? (s) : nullstring)
102 static int proto_snmp = -1;
103 static int proto_smux = -1;
105 static gint ett_snmp = -1;
106 static gint ett_smux = -1;
107 static gint ett_parameters = -1;
108 static gint ett_parameters_qos = -1;
109 static gint ett_global = -1;
110 static gint ett_flags = -1;
111 static gint ett_secur = -1;
113 static int hf_snmpv3_flags = -1;
114 static int hf_snmpv3_flags_auth = -1;
115 static int hf_snmpv3_flags_crypt = -1;
116 static int hf_snmpv3_flags_report = -1;
118 static dissector_handle_t snmp_handle;
119 static dissector_handle_t data_handle;
122 #define TH_CRYPT 0x02
123 #define TH_REPORT 0x04
125 static const true_false_string flags_set_truth = {
130 #define UDP_PORT_SNMP 161
131 #define UDP_PORT_SNMP_TRAP 162
132 #define TCP_PORT_SMUX 199
134 /* Protocol version numbers */
135 #define SNMP_VERSION_1 0
136 #define SNMP_VERSION_2c 1
137 #define SNMP_VERSION_2u 2
138 #define SNMP_VERSION_3 3
140 static const value_string versions[] = {
141 { SNMP_VERSION_1, "1" },
142 { SNMP_VERSION_2c, "2C" },
143 { SNMP_VERSION_2u, "2U" },
144 { SNMP_VERSION_3, "3" },
149 #define SNMP_MSG_GET 0
150 #define SNMP_MSG_GETNEXT 1
151 #define SNMP_MSG_RESPONSE 2
152 #define SNMP_MSG_SET 3
153 #define SNMP_MSG_TRAP 4
155 #define SNMP_MSG_GETBULK 5
156 #define SNMP_MSG_INFORM 6
157 #define SNMP_MSG_TRAP2 7
158 #define SNMP_MSG_REPORT 8
160 static const value_string pdu_types[] = {
161 { SNMP_MSG_GET, "GET" },
162 { SNMP_MSG_GETNEXT, "GET-NEXT" },
163 { SNMP_MSG_SET, "SET" },
164 { SNMP_MSG_RESPONSE, "RESPONSE" },
165 { SNMP_MSG_TRAP, "TRAP-V1" },
166 { SNMP_MSG_GETBULK, "GETBULK" },
167 { SNMP_MSG_INFORM, "INFORM" },
168 { SNMP_MSG_TRAP2, "TRAP-V2" },
169 { SNMP_MSG_REPORT, "REPORT" },
174 #define SMUX_MSG_OPEN 0
175 #define SMUX_MSG_CLOSE 1
176 #define SMUX_MSG_RREQ 2
177 #define SMUX_MSG_RRSP 3
178 #define SMUX_MSG_SOUT 4
180 static const value_string smux_types[] = {
181 { SMUX_MSG_OPEN, "Open" },
182 { SMUX_MSG_CLOSE, "Close" },
183 { SMUX_MSG_RREQ, "Registration Request" },
184 { SMUX_MSG_RRSP, "Registration Response" },
185 { SMUX_MSG_SOUT, "Commit Or Rollback" },
189 /* SMUX Closing causes */
190 #define SMUX_CLOSE_DOWN 0
191 #define SMUX_CLOSE_VERSION 1
192 #define SMUX_CLOSE_PACKET 2
193 #define SMUX_CLOSE_PROTOCOL 3
194 #define SMUX_CLOSE_INTERNAL 4
195 #define SMUX_CLOSE_NOAUTH 5
197 static const value_string smux_close[] = {
198 { SMUX_CLOSE_DOWN, "Going down" },
199 { SMUX_CLOSE_VERSION, "Unsupported Version" },
200 { SMUX_CLOSE_PACKET, "Packet Format Error" },
201 { SMUX_CLOSE_PROTOCOL, "Protocol Error" },
202 { SMUX_CLOSE_INTERNAL, "Internal Error" },
203 { SMUX_CLOSE_NOAUTH, "Unauthorized" },
207 /* SMUX Request codes */
208 #define SMUX_RREQ_DELETE 0
209 #define SMUX_RREQ_READONLY 1
210 #define SMUX_RREQ_READWRITE 2
212 static const value_string smux_rreq[] = {
213 { SMUX_RREQ_DELETE, "Delete" },
214 { SMUX_RREQ_READONLY, "Read Only" },
215 { SMUX_RREQ_READWRITE, "Read Write" },
219 static const value_string smux_prio[] = {
224 /* SMUX SOut codes */
225 #define SMUX_SOUT_COMMIT 0
226 #define SMUX_SOUT_ROLLBACK 1
228 static const value_string smux_sout[] = {
229 { SMUX_SOUT_COMMIT, "Commit" },
230 { SMUX_SOUT_ROLLBACK, "Rollback" },
234 /* Error status values */
235 #define SNMP_ERR_NOERROR 0
236 #define SNMP_ERR_TOOBIG 1
237 #define SNMP_ERR_NOSUCHNAME 2
238 #define SNMP_ERR_BADVALUE 3
239 #define SNMP_ERR_READONLY 4
240 #define SNMP_ERR_GENERROR 5
242 #define SNMP_ERR_NOACCESS 6
243 #define SNMP_ERR_WRONGTYPE 7
244 #define SNMP_ERR_WRONGLENGTH 8
245 #define SNMP_ERR_WRONGENCODING 9
246 #define SNMP_ERR_WRONGVALUE 10
247 #define SNMP_ERR_NOCREATION 11
248 #define SNMP_ERR_INCONSISTENTVALUE 12
249 #define SNMP_ERR_RESOURCEUNAVAILABLE 13
250 #define SNMP_ERR_COMMITFAILED 14
251 #define SNMP_ERR_UNDOFAILED 15
252 #define SNMP_ERR_AUTHORIZATIONERROR 16
253 #define SNMP_ERR_NOTWRITABLE 17
254 #define SNMP_ERR_INCONSISTENTNAME 18
256 static const value_string error_statuses[] = {
257 { SNMP_ERR_NOERROR, "NO ERROR" },
258 { SNMP_ERR_TOOBIG, "TOOBIG" },
259 { SNMP_ERR_NOSUCHNAME, "NO SUCH NAME" },
260 { SNMP_ERR_BADVALUE, "BAD VALUE" },
261 { SNMP_ERR_READONLY, "READ ONLY" },
262 { SNMP_ERR_GENERROR, "GENERIC ERROR" },
263 { SNMP_ERR_NOACCESS, "NO ACCESS" },
264 { SNMP_ERR_WRONGTYPE, "WRONG TYPE" },
265 { SNMP_ERR_WRONGLENGTH, "WRONG LENGTH" },
266 { SNMP_ERR_WRONGENCODING, "WRONG ENCODING" },
267 { SNMP_ERR_WRONGVALUE, "WRONG VALUE" },
268 { SNMP_ERR_NOCREATION, "NO CREATION" },
269 { SNMP_ERR_INCONSISTENTVALUE, "INCONSISTENT VALUE" },
270 { SNMP_ERR_RESOURCEUNAVAILABLE, "RESOURCE UNAVAILABLE" },
271 { SNMP_ERR_COMMITFAILED, "COMMIT FAILED" },
272 { SNMP_ERR_UNDOFAILED, "UNDO FAILED" },
273 { SNMP_ERR_AUTHORIZATIONERROR, "AUTHORIZATION ERROR" },
274 { SNMP_ERR_NOTWRITABLE, "NOT WRITABLE" },
275 { SNMP_ERR_INCONSISTENTNAME, "INCONSISTENT NAME" },
279 /* General SNMP V1 Traps */
281 #define SNMP_TRAP_COLDSTART 0
282 #define SNMP_TRAP_WARMSTART 1
283 #define SNMP_TRAP_LINKDOWN 2
284 #define SNMP_TRAP_LINKUP 3
285 #define SNMP_TRAP_AUTHFAIL 4
286 #define SNMP_TRAP_EGPNEIGHBORLOSS 5
287 #define SNMP_TRAP_ENTERPRISESPECIFIC 6
289 static const value_string trap_types[] = {
290 { SNMP_TRAP_COLDSTART, "COLD START" },
291 { SNMP_TRAP_WARMSTART, "WARM START" },
292 { SNMP_TRAP_LINKDOWN, "LINK DOWN" },
293 { SNMP_TRAP_LINKUP, "LINK UP" },
294 { SNMP_TRAP_AUTHFAIL, "AUTHENTICATION FAILED" },
295 { SNMP_TRAP_EGPNEIGHBORLOSS, "EGP NEIGHBORLOSS" },
296 { SNMP_TRAP_ENTERPRISESPECIFIC, "ENTERPRISE SPECIFIC" },
300 /* Security Models */
302 #define SNMP_SEC_ANY 0
303 #define SNMP_SEC_V1 1
304 #define SNMP_SEC_V2C 2
305 #define SNMP_SEC_USM 3
307 static const value_string sec_models[] = {
308 { SNMP_SEC_ANY, "Any" },
309 { SNMP_SEC_V1, "V1" },
310 { SNMP_SEC_V2C, "V2C" },
311 { SNMP_SEC_USM, "USM" },
317 #define SNMP_IPA 0 /* IP Address */
318 #define SNMP_CNT 1 /* Counter (Counter32) */
319 #define SNMP_GGE 2 /* Gauge (Gauge32) */
320 #define SNMP_TIT 3 /* TimeTicks */
321 #define SNMP_OPQ 4 /* Opaque */
322 #define SNMP_NSP 5 /* NsapAddress */
323 #define SNMP_C64 6 /* Counter64 */
324 #define SNMP_U32 7 /* Uinteger32 */
333 #define SNMP_INTEGER 1 /* l */
334 #define SNMP_OCTETSTR 2 /* c */
335 #define SNMP_DISPLAYSTR 2 /* c */
336 #define SNMP_OBJECTID 3 /* ul */
337 #define SNMP_IPADDR 4 /* uc */
338 #define SNMP_COUNTER 5 /* ul */
339 #define SNMP_GAUGE 6 /* ul */
340 #define SNMP_TIMETICKS 7 /* ul */
341 #define SNMP_OPAQUE 8 /* c */
343 /* additional SNMPv2 Types */
345 #define SNMP_UINTEGER 5 /* ul */
346 #define SNMP_BITSTR 9 /* uc */
347 #define SNMP_NSAP 10 /* uc */
348 #define SNMP_COUNTER64 11 /* ul */
349 #define SNMP_NOSUCHOBJECT 12
350 #define SNMP_NOSUCHINSTANCE 13
351 #define SNMP_ENDOFMIBVIEW 14
353 typedef struct _SNMP_CNV SNMP_CNV;
363 static SNMP_CNV SnmpCnv [] =
365 {ASN1_UNI, ASN1_NUL, SNMP_NULL, "NULL"},
366 {ASN1_UNI, ASN1_INT, SNMP_INTEGER, "INTEGER"},
367 {ASN1_UNI, ASN1_OTS, SNMP_OCTETSTR, "OCTET STRING"},
368 {ASN1_UNI, ASN1_OJI, SNMP_OBJECTID, "OBJECTID"},
369 {ASN1_APL, SNMP_IPA, SNMP_IPADDR, "IPADDR"},
370 {ASN1_APL, SNMP_CNT, SNMP_COUNTER, "COUNTER"}, /* Counter32 */
371 {ASN1_APL, SNMP_GGE, SNMP_GAUGE, "GAUGE"}, /* Gauge32 == Unsigned32 */
372 {ASN1_APL, SNMP_TIT, SNMP_TIMETICKS, "TIMETICKS"},
373 {ASN1_APL, SNMP_OPQ, SNMP_OPAQUE, "OPAQUE"},
375 /* SNMPv2 data types and errors */
377 {ASN1_UNI, ASN1_BTS, SNMP_BITSTR, "BITSTR"},
378 {ASN1_APL, SNMP_C64, SNMP_COUNTER64, "COUNTER64"},
379 {ASN1_CTX, SERR_NSO, SNMP_NOSUCHOBJECT, "NOSUCHOBJECT"},
380 {ASN1_CTX, SERR_NSI, SNMP_NOSUCHINSTANCE, "NOSUCHINSTANCE"},
381 {ASN1_CTX, SERR_EOM, SNMP_ENDOFMIBVIEW, "ENDOFMIBVIEW"},
386 * NAME: g_snmp_tag_cls2syntax
387 * SYNOPSIS: gboolean g_snmp_tag_cls2syntax
393 * DESCRIPTION: Converts ASN1 tag and class to Syntax tag and name.
394 * See SnmpCnv for conversion.
395 * RETURNS: name on success, NULL on failure
399 snmp_tag_cls2syntax ( guint tag, guint cls, gushort *syntax)
404 while (cnv->syntax != -1)
406 if (cnv->tag == tag && cnv->class == cls)
408 *syntax = cnv->syntax;
417 dissect_snmp_parse_error(tvbuff_t *tvb, int offset, packet_info *pinfo,
418 proto_tree *tree, const char *field_name, int ret)
422 errstr = asn1_err_to_str(ret);
424 if (check_col(pinfo->cinfo, COL_INFO)) {
425 col_add_fstr(pinfo->cinfo, COL_INFO,
426 "ERROR: Couldn't parse %s: %s", field_name, errstr);
429 proto_tree_add_text(tree, tvb, offset, 0,
430 "ERROR: Couldn't parse %s: %s", field_name, errstr);
431 call_dissector(data_handle,
432 tvb_new_subset(tvb, offset, -1, -1), pinfo, tree);
437 dissect_snmp_error(tvbuff_t *tvb, int offset, packet_info *pinfo,
438 proto_tree *tree, const char *message)
440 if (check_col(pinfo->cinfo, COL_INFO))
441 col_add_str(pinfo->cinfo, COL_INFO, message);
444 proto_tree_add_text(tree, tvb, offset, 0, "%s", message);
445 call_dissector(data_handle,tvb_new_subset(tvb, offset,-1,tvb_reported_length_remaining(tvb,offset)), pinfo, tree);
450 format_oid(subid_t *oid, guint oid_length)
458 result_len = oid_length * 22;
459 result = g_malloc(result_len + 1);
461 len = sprintf(buf, "%lu", (unsigned long)oid[0]);
463 for (i = 1; i < oid_length;i++) {
464 len = sprintf(buf, ".%lu", (unsigned long)oid[i]);
470 #ifdef HAVE_UCD_SNMP_SNMP_H
472 check_var_length(guint vb_length, guint required_length)
475 static const char badlen_fmt[] = "Length is %u, should be %u";
477 if (vb_length != required_length) {
478 /* Enough room for the largest "Length is XXX,
479 should be XXX" message - 10 digits for each
481 buf = malloc(sizeof badlen_fmt + 10 + 10);
482 sprintf(buf, badlen_fmt, vb_length, required_length);
485 return NULL; /* length is OK */
489 format_var(struct variable_list *variable, subid_t *variable_oid,
490 guint variable_oid_length, gushort vb_type, guint vb_length)
499 /* Length has to be 4 bytes. */
500 buf = check_var_length(vb_length, 4);
502 return buf; /* it's not 4 bytes */
506 /* Length has to be 8 bytes. */
507 buf = check_var_length(vb_length, 8);
509 return buf; /* it's not 8 bytes */
516 variable->next_variable = NULL;
517 variable->name = variable_oid;
518 variable->name_length = variable_oid_length;
522 variable->type = VALTYPE_INTEGER;
526 variable->type = VALTYPE_COUNTER;
530 variable->type = VALTYPE_GAUGE;
534 variable->type = VALTYPE_TIMETICKS;
538 variable->type = VALTYPE_STRING;
542 variable->type = VALTYPE_IPADDR;
546 variable->type = VALTYPE_OPAQUE;
550 variable->type = VALTYPE_NSAP;
554 variable->type = VALTYPE_OBJECTID;
555 vb_length *= sizeof (subid_t); /* XXX - necessary? */
559 variable->type = VALTYPE_BITSTR;
563 variable->type = VALTYPE_COUNTER64;
566 variable->val_len = vb_length;
569 * XXX - check for "malloc" and "sprint_realloc_objid()" failure.
572 buf = malloc(buf_len);
575 sprint_realloc_value(&buf, &buf_len, &out_len, 1, variable_oid,
576 variable_oid_length, variable);
582 snmp_variable_decode(proto_tree *snmp_tree, subid_t *variable_oid,
583 guint variable_oid_length, ASN1_SCK *asn1, int offset, guint *lengthp)
594 gint32 vb_integer_value;
595 guint32 vb_uinteger_value;
597 guint8 *vb_octet_string;
602 gchar *vb_display_string;
604 #ifdef HAVE_UCD_SNMP_SNMP_H
605 struct variable_list variable;
607 #endif /* HAVE_UCD_SNMP_SNMP_H */
612 /* parse the type of the object */
613 start = asn1->offset;
614 ret = asn1_header_decode (asn1, &cls, &con, &tag, &def, &vb_length);
615 if (ret != ASN1_ERR_NOERROR)
618 return ASN1_ERR_LENGTH_NOT_DEFINITE;
620 /* Convert the class, constructed flag, and tag to a type. */
621 vb_type_name = snmp_tag_cls2syntax(tag, cls, &vb_type);
622 if (vb_type_name == NULL) {
625 * Dissect the value as an opaque string of octets.
627 vb_type_name = "unsupported type";
628 vb_type = SNMP_OPAQUE;
631 /* parse the value */
635 ret = asn1_int32_value_decode(asn1, vb_length,
637 if (ret != ASN1_ERR_NOERROR)
639 length = asn1->offset - start;
641 #ifdef HAVE_UCD_SNMP_SNMP_H
642 value = vb_integer_value;
643 variable.val.integer = &value;
644 vb_display_string = format_var(&variable,
645 variable_oid, variable_oid_length, vb_type,
647 proto_tree_add_text(snmp_tree, asn1->tvb, offset,
649 "Value: %s", vb_display_string);
650 g_free(vb_display_string);
651 #else /* HAVE_UCD_SNMP_SNMP_H */
652 proto_tree_add_text(snmp_tree, asn1->tvb, offset,
654 "Value: %s: %d (%#x)", vb_type_name,
655 vb_integer_value, vb_integer_value);
656 #endif /* HAVE_UCD_SNMP_SNMP_H */
663 ret = asn1_uint32_value_decode(asn1, vb_length,
665 if (ret != ASN1_ERR_NOERROR)
667 length = asn1->offset - start;
669 #ifdef HAVE_UCD_SNMP_SNMP_H
670 value = vb_uinteger_value;
671 variable.val.integer = &value;
672 vb_display_string = format_var(&variable,
673 variable_oid, variable_oid_length, vb_type,
675 proto_tree_add_text(snmp_tree, asn1->tvb, offset,
677 "Value: %s", vb_display_string);
678 g_free(vb_display_string);
679 #else /* HAVE_UCD_SNMP_SNMP_H */
680 proto_tree_add_text(snmp_tree, asn1->tvb, offset,
682 "Value: %s: %u (%#x)", vb_type_name,
683 vb_uinteger_value, vb_uinteger_value);
684 #endif /* HAVE_UCD_SNMP_SNMP_H */
694 ret = asn1_string_value_decode (asn1, vb_length,
696 if (ret != ASN1_ERR_NOERROR)
698 length = asn1->offset - start;
700 #ifdef HAVE_UCD_SNMP_SNMP_H
701 variable.val.string = vb_octet_string;
702 vb_display_string = format_var(&variable,
703 variable_oid, variable_oid_length, vb_type,
705 proto_tree_add_text(snmp_tree, asn1->tvb, offset,
707 "Value: %s", vb_display_string);
708 g_free(vb_display_string);
709 #else /* HAVE_UCD_SNMP_SNMP_H */
711 * If some characters are not printable, display
712 * the string as bytes.
714 for (i = 0; i < vb_length; i++) {
715 if (!(isprint(vb_octet_string[i])
716 || isspace(vb_octet_string[i])))
721 * We stopped, due to a non-printable
722 * character, before we got to the end
725 vb_display_string = g_malloc(4*vb_length);
726 buf = &vb_display_string[0];
727 len = sprintf(buf, "%03u", vb_octet_string[0]);
729 for (i = 1; i < vb_length; i++) {
730 len = sprintf(buf, ".%03u",
734 proto_tree_add_text(snmp_tree, asn1->tvb, offset,
736 "Value: %s: %s", vb_type_name,
738 g_free(vb_display_string);
740 proto_tree_add_text(snmp_tree, asn1->tvb, offset,
742 "Value: %s: %.*s", vb_type_name,
744 SAFE_STRING(vb_octet_string));
746 #endif /* HAVE_UCD_SNMP_SNMP_H */
748 g_free(vb_octet_string);
752 ret = asn1_null_decode (asn1, vb_length);
753 if (ret != ASN1_ERR_NOERROR)
755 length = asn1->offset - start;
757 proto_tree_add_text(snmp_tree, asn1->tvb, offset, length,
758 "Value: %s", vb_type_name);
763 ret = asn1_oid_value_decode (asn1, vb_length, &vb_oid,
765 if (ret != ASN1_ERR_NOERROR)
767 length = asn1->offset - start;
769 #ifdef HAVE_UCD_SNMP_SNMP_H
770 variable.val.objid = vb_oid;
771 vb_display_string = format_var(&variable,
772 variable_oid, variable_oid_length, vb_type,
774 proto_tree_add_text(snmp_tree, asn1->tvb, offset,
776 "Value: %s", vb_display_string);
777 #else /* HAVE_UCD_SNMP_SNMP_H */
778 vb_display_string = format_oid(vb_oid, vb_oid_length);
779 proto_tree_add_text(snmp_tree, asn1->tvb, offset,
781 "Value: %s: %s", vb_type_name, vb_display_string);
782 #endif /* HAVE_UCD_SNMP_SNMP_H */
783 g_free(vb_display_string);
788 case SNMP_NOSUCHOBJECT:
789 length = asn1->offset - start;
791 proto_tree_add_text(snmp_tree, asn1->tvb, offset, length,
792 "Value: %s: no such object", vb_type_name);
796 case SNMP_NOSUCHINSTANCE:
797 length = asn1->offset - start;
799 proto_tree_add_text(snmp_tree, asn1->tvb, offset, length,
800 "Value: %s: no such instance", vb_type_name);
804 case SNMP_ENDOFMIBVIEW:
805 length = asn1->offset - start;
807 proto_tree_add_text(snmp_tree, asn1->tvb, offset, length,
808 "Value: %s: end of mib view", vb_type_name);
813 g_assert_not_reached();
814 return ASN1_ERR_WRONG_TYPE;
817 return ASN1_ERR_NOERROR;
821 dissect_common_pdu(tvbuff_t *tvb, int offset, packet_info *pinfo,
822 proto_tree *tree, ASN1_SCK asn1, guint pdu_type, int start)
826 guint sequence_length;
830 guint32 error_status;
834 char *pdu_type_string;
837 guint enterprise_length;
839 guint8 *agent_address;
840 guint agent_address_length;
844 guint32 specific_type;
847 guint timestamp_length;
851 guint variable_bindings_length;
854 guint variable_length;
855 subid_t *variable_oid;
856 guint variable_oid_length;
857 #ifdef HAVE_UCD_SNMP_SNMP_H
858 u_char *vb_oid_string;
859 size_t vb_oid_string_len;
860 size_t vb_oid_out_len;
866 pdu_type_string = val_to_str(pdu_type, pdu_types,
867 "Unknown PDU type %#x");
868 if (check_col(pinfo->cinfo, COL_INFO))
869 col_add_str(pinfo->cinfo, COL_INFO, pdu_type_string);
870 length = asn1.offset - start;
872 proto_tree_add_text(tree, tvb, offset, length,
873 "PDU type: %s", pdu_type_string);
877 /* get the fields in the PDU preceeding the variable-bindings sequence */
881 case SNMP_MSG_GETNEXT:
882 case SNMP_MSG_RESPONSE:
884 case SNMP_MSG_GETBULK:
885 case SNMP_MSG_INFORM:
887 case SNMP_MSG_REPORT:
889 ret = asn1_uint32_decode (&asn1, &request_id, &length);
890 if (ret != ASN1_ERR_NOERROR) {
891 dissect_snmp_parse_error(tvb, offset, pinfo, tree,
896 proto_tree_add_text(tree, tvb, offset, length,
897 "Request Id: %#x", request_id);
901 /* error status, or getbulk non-repeaters */
902 ret = asn1_uint32_decode (&asn1, &error_status, &length);
903 if (ret != ASN1_ERR_NOERROR) {
904 dissect_snmp_parse_error(tvb, offset, pinfo, tree,
905 (pdu_type == SNMP_MSG_GETBULK) ? "non-repeaters"
911 if (pdu_type == SNMP_MSG_GETBULK) {
912 proto_tree_add_text(tree, tvb, offset,
913 length, "Non-repeaters: %u", error_status);
915 proto_tree_add_text(tree, tvb, offset,
916 length, "Error Status: %s",
917 val_to_str(error_status, error_statuses,
923 /* error index, or getbulk max-repetitions */
924 ret = asn1_uint32_decode (&asn1, &error_index, &length);
925 if (ret != ASN1_ERR_NOERROR) {
926 dissect_snmp_parse_error(tvb, offset, pinfo, tree,
927 (pdu_type == SNMP_MSG_GETBULK) ? "max repetitions"
933 if (pdu_type == SNMP_MSG_GETBULK) {
934 proto_tree_add_text(tree, tvb, offset,
935 length, "Max repetitions: %u", error_index);
937 proto_tree_add_text(tree, tvb, offset,
938 length, "Error Index: %u", error_index);
946 ret = asn1_oid_decode (&asn1, &enterprise, &enterprise_length,
948 if (ret != ASN1_ERR_NOERROR) {
949 dissect_snmp_parse_error(tvb, offset, pinfo, tree,
950 "enterprise OID", ret);
954 oid_string = format_oid(enterprise, enterprise_length);
955 #ifdef HAVE_UCD_SNMP_SNMP_H
957 * XXX - check for "malloc" and
958 * "sprint_realloc_objid()" failure.
960 vb_oid_string_len = 256;
961 vb_oid_string = malloc(vb_oid_string_len);
962 *vb_oid_string = '\0';
964 sprint_realloc_objid(&vb_oid_string, &vb_oid_string_len,
965 &vb_oid_out_len, 1, enterprise,
967 proto_tree_add_text(tree, tvb, offset, length,
968 "Enterprise: %s (%s)", oid_string, vb_oid_string);
970 #else /* HAVE_UCD_SNMP_SNMP_H */
971 proto_tree_add_text(tree, tvb, offset, length,
972 "Enterprise: %s", oid_string);
973 #endif /* HAVE_UCD_SNMP_SNMP_H */
981 ret = asn1_header_decode (&asn1, &cls, &con, &tag,
982 &def, &agent_address_length);
983 if (ret != ASN1_ERR_NOERROR) {
984 dissect_snmp_parse_error(tvb, offset, pinfo, tree,
985 "agent address", ret);
988 if (!((cls == ASN1_APL && con == ASN1_PRI && tag == SNMP_IPA) ||
989 (cls == ASN1_UNI && con == ASN1_PRI && tag == ASN1_OTS))) {
990 /* GXSNMP 0.0.15 says the latter is "needed for
992 dissect_snmp_parse_error(tvb, offset, pinfo, tree,
993 "agent_address", ASN1_ERR_WRONG_TYPE);
996 if (agent_address_length != 4) {
997 dissect_snmp_parse_error(tvb, offset, pinfo, tree,
998 "agent_address", ASN1_ERR_WRONG_LENGTH_FOR_TYPE);
1001 ret = asn1_string_value_decode (&asn1,
1002 agent_address_length, &agent_address);
1003 if (ret != ASN1_ERR_NOERROR) {
1004 dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1005 "agent address", ret);
1008 length = asn1.offset - start;
1010 if (agent_address_length != 4) {
1011 proto_tree_add_text(tree, tvb, offset,
1013 "Agent address: <length is %u, not 4>",
1014 agent_address_length);
1016 proto_tree_add_text(tree, tvb, offset,
1018 "Agent address: %s",
1019 ip_to_str(agent_address));
1022 g_free(agent_address);
1025 /* generic trap type */
1026 ret = asn1_uint32_decode (&asn1, &trap_type, &length);
1027 if (ret != ASN1_ERR_NOERROR) {
1028 dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1029 "generic trap type", ret);
1033 proto_tree_add_text(tree, tvb, offset, length,
1035 val_to_str(trap_type, trap_types, "Unknown (%u)"));
1039 /* specific trap type */
1040 ret = asn1_uint32_decode (&asn1, &specific_type, &length);
1041 if (ret != ASN1_ERR_NOERROR) {
1042 dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1043 "specific trap type", ret);
1047 proto_tree_add_text(tree, tvb, offset, length,
1048 "Specific trap type: %u (%#x)",
1049 specific_type, specific_type);
1054 start = asn1.offset;
1055 ret = asn1_header_decode (&asn1, &cls, &con, &tag,
1056 &def, ×tamp_length);
1057 if (ret != ASN1_ERR_NOERROR) {
1058 dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1062 if (!((cls == ASN1_APL && con == ASN1_PRI && tag == SNMP_TIT) ||
1063 (cls == ASN1_UNI && con == ASN1_PRI && tag == ASN1_INT))) {
1064 dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1065 "timestamp", ASN1_ERR_WRONG_TYPE);
1068 ret = asn1_uint32_value_decode(&asn1, timestamp_length,
1070 if (ret != ASN1_ERR_NOERROR) {
1071 dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1075 length = asn1.offset - start;
1077 proto_tree_add_text(tree, tvb, offset, length,
1078 "Timestamp: %u", timestamp);
1084 /* variable bindings */
1085 /* get header for variable-bindings sequence */
1086 ret = asn1_sequence_decode(&asn1, &variable_bindings_length, &length);
1087 if (ret != ASN1_ERR_NOERROR) {
1088 dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1089 "variable bindings header", ret);
1094 /* loop on variable bindings */
1096 while (variable_bindings_length > 0) {
1098 sequence_length = 0;
1101 ret = asn1_sequence_decode(&asn1, &variable_length, &length);
1102 if (ret != ASN1_ERR_NOERROR) {
1103 dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1104 "variable binding header", ret);
1107 sequence_length += length;
1109 /* parse object identifier */
1110 ret = asn1_oid_decode (&asn1, &variable_oid,
1111 &variable_oid_length, &length);
1112 if (ret != ASN1_ERR_NOERROR) {
1113 dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1114 "variable binding OID", ret);
1117 sequence_length += length;
1120 oid_string = format_oid(variable_oid,
1121 variable_oid_length);
1123 #ifdef HAVE_UCD_SNMP_SNMP_H
1125 * XXX - check for "malloc" and
1126 * "sprint_realloc_objid()" failure.
1128 vb_oid_string_len = 256;
1129 vb_oid_string = malloc(vb_oid_string_len);
1130 *vb_oid_string = '\0';
1132 sprint_realloc_objid(&vb_oid_string, &vb_oid_string_len,
1133 &vb_oid_out_len, 1, variable_oid,
1134 variable_oid_length);
1135 proto_tree_add_text(tree, tvb, offset, sequence_length,
1136 "Object identifier %d: %s (%s)", vb_index,
1137 oid_string, vb_oid_string);
1138 free(vb_oid_string);
1139 #else /* HAVE_UCD_SNMP_SNMP_H */
1140 proto_tree_add_text(tree, tvb, offset, sequence_length,
1141 "Object identifier %d: %s", vb_index,
1143 #endif /* HAVE_UCD_SNMP_SNMP_H */
1146 offset += sequence_length;
1147 variable_bindings_length -= sequence_length;
1149 /* Parse the variable's value */
1150 ret = snmp_variable_decode(tree, variable_oid,
1151 variable_oid_length, &asn1, offset, &length);
1152 if (ret != ASN1_ERR_NOERROR) {
1153 dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1158 variable_bindings_length -= length;
1162 static const value_string qos_vals[] = {
1163 { 0x0, "No authentication or privacy" },
1164 { 0x1, "Authentication, no privacy" },
1165 { 0x2, "Authentication and privacy" },
1166 { 0x3, "Authentication and privacy" },
1171 dissect_snmp2u_parameters(proto_tree *tree, tvbuff_t *tvb, int offset, int length,
1172 guchar *parameters, int parameters_length)
1175 proto_tree *parameters_tree;
1176 proto_tree *qos_tree;
1181 item = proto_tree_add_text(tree, tvb, offset, length,
1183 parameters_tree = proto_item_add_subtree(item, ett_parameters);
1184 offset += length - parameters_length;
1186 if (parameters_length < 1)
1188 model = *parameters;
1189 proto_tree_add_text(parameters_tree, tvb, offset, 1,
1190 "model: %u", model);
1193 parameters_length -= 1;
1195 /* Unknown model. */
1196 proto_tree_add_text(parameters_tree, tvb, offset,
1197 parameters_length, "parameters: %s",
1198 bytes_to_str(parameters, parameters_length));
1202 if (parameters_length < 1)
1205 item = proto_tree_add_text(parameters_tree, tvb, offset, 1,
1207 qos_tree = proto_item_add_subtree(item, ett_parameters_qos);
1208 proto_tree_add_text(qos_tree, tvb, offset, 1, "%s",
1209 decode_boolean_bitfield(qos, 0x04,
1210 8, "Generation of report PDU allowed",
1211 "Generation of report PDU not allowed"));
1212 proto_tree_add_text(qos_tree, tvb, offset, 1, "%s",
1213 decode_enumerated_bitfield(qos, 0x03,
1214 8, qos_vals, "%s"));
1217 parameters_length -= 1;
1219 if (parameters_length < 12)
1221 proto_tree_add_text(parameters_tree, tvb, offset, 12,
1222 "agentID: %s", bytes_to_str(parameters, 12));
1225 parameters_length -= 12;
1227 if (parameters_length < 4)
1229 proto_tree_add_text(parameters_tree, tvb, offset, 4,
1230 "agentBoots: %u", pntohl(parameters));
1233 parameters_length -= 4;
1235 if (parameters_length < 4)
1237 proto_tree_add_text(parameters_tree, tvb, offset, 4,
1238 "agentTime: %u", pntohl(parameters));
1241 parameters_length -= 4;
1243 if (parameters_length < 2)
1245 proto_tree_add_text(parameters_tree, tvb, offset, 2,
1246 "maxSize: %u", pntohs(parameters));
1249 parameters_length -= 2;
1251 if (parameters_length < 1)
1254 proto_tree_add_text(parameters_tree, tvb, offset, 1,
1255 "userLen: %u", len);
1258 parameters_length -= 1;
1260 if (parameters_length < len)
1262 proto_tree_add_text(parameters_tree, tvb, offset, len,
1263 "userName: %.*s", len, parameters);
1266 parameters_length -= len;
1268 if (parameters_length < 1)
1271 proto_tree_add_text(parameters_tree, tvb, offset, 1,
1272 "authLen: %u", len);
1275 parameters_length -= 1;
1277 if (parameters_length < len)
1279 proto_tree_add_text(parameters_tree, tvb, offset, len,
1280 "authDigest: %s", bytes_to_str(parameters, len));
1283 parameters_length -= len;
1285 if (parameters_length < 1)
1287 proto_tree_add_text(parameters_tree, tvb, offset, parameters_length,
1288 "contextSelector: %s", bytes_to_str(parameters, parameters_length));
1292 dissect_snmp_pdu(tvbuff_t *tvb, int offset, packet_info *pinfo,
1293 proto_tree *tree, char *proto_name, int proto, gint ett)
1300 guint message_length;
1301 guint global_length;
1307 guint32 engineboots;
1320 int msgflags_length;
1321 int community_length;
1323 int cengineid_length;
1325 int cryptpdu_length;
1326 int aengineid_length;
1327 int username_length;
1334 proto_tree *snmp_tree = NULL;
1335 proto_tree *global_tree = NULL;
1336 proto_tree *flags_tree = NULL;
1337 proto_tree *secur_tree = NULL;
1338 proto_item *item = NULL;
1340 guint cls, con, tag;
1342 if (check_col(pinfo->cinfo, COL_PROTOCOL))
1343 col_add_str(pinfo->cinfo, COL_PROTOCOL, proto_name);
1346 item = proto_tree_add_item(tree, proto, tvb, offset, -1, FALSE);
1347 snmp_tree = proto_item_add_subtree(item, ett);
1350 /* NOTE: we have to parse the message piece by piece, since the
1351 * capture length may be less than the message length: a 'global'
1352 * parsing is likely to fail.
1354 /* parse the SNMP header */
1355 asn1_open(&asn1, tvb, offset);
1356 ret = asn1_sequence_decode(&asn1, &message_length, &length);
1357 if (ret != ASN1_ERR_NOERROR) {
1358 dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1359 "message header", ret);
1364 ret = asn1_uint32_decode (&asn1, &version, &length);
1365 if (ret != ASN1_ERR_NOERROR) {
1366 dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1367 "version number", ret);
1371 proto_tree_add_text(snmp_tree, tvb, offset, length,
1373 val_to_str(version, versions, "Unknown version %#x"));
1379 case SNMP_VERSION_1:
1380 case SNMP_VERSION_2c:
1381 ret = asn1_octet_string_decode (&asn1, &community,
1382 &community_length, &length);
1383 if (ret != ASN1_ERR_NOERROR) {
1384 dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1389 proto_tree_add_text(snmp_tree, tvb, offset, length,
1390 "Community: %.*s", community_length,
1391 SAFE_STRING(community));
1396 case SNMP_VERSION_2u:
1397 ret = asn1_octet_string_decode (&asn1, &community,
1398 &community_length, &length);
1400 dissect_snmp2u_parameters(snmp_tree, tvb, offset, length,
1401 community, community_length);
1406 case SNMP_VERSION_3:
1407 ret = asn1_sequence_decode(&asn1, &global_length, &length);
1408 if (ret != ASN1_ERR_NOERROR) {
1409 dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1410 "message global header", ret);
1414 item = proto_tree_add_text(snmp_tree, tvb, offset,
1415 global_length + length, "Message Global Header");
1416 global_tree = proto_item_add_subtree(item, ett_global);
1417 proto_tree_add_text(global_tree, tvb, offset,
1419 "Message Global Header Length: %d", global_length);
1422 ret = asn1_uint32_decode (&asn1, &msgid, &length);
1423 if (ret != ASN1_ERR_NOERROR) {
1424 dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1429 proto_tree_add_text(global_tree, tvb, offset,
1430 length, "Message ID: %d", msgid);
1433 ret = asn1_uint32_decode (&asn1, &msgmax, &length);
1434 if (ret != ASN1_ERR_NOERROR) {
1435 dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1436 "message max size", ret);
1440 proto_tree_add_text(global_tree, tvb, offset,
1441 length, "Message Max Size: %d", msgmax);
1444 ret = asn1_octet_string_decode (&asn1, &msgflags,
1445 &msgflags_length, &length);
1446 if (ret != ASN1_ERR_NOERROR) {
1447 dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1448 "message flags", ret);
1451 if (msgflags_length != 1) {
1452 dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1453 "message flags wrong length", ret);
1458 item = proto_tree_add_uint_format(global_tree,
1459 hf_snmpv3_flags, tvb, offset, length,
1460 msgflags[0], "Flags: 0x%02x", msgflags[0]);
1461 flags_tree = proto_item_add_subtree(item, ett_flags);
1462 proto_tree_add_boolean(flags_tree, hf_snmpv3_flags_report,
1463 tvb, offset, length, msgflags[0]);
1464 proto_tree_add_boolean(flags_tree, hf_snmpv3_flags_crypt,
1465 tvb, offset, length, msgflags[0]);
1466 proto_tree_add_boolean(flags_tree, hf_snmpv3_flags_auth,
1467 tvb, offset, length, msgflags[0]);
1469 encrypted = msgflags[0] & TH_CRYPT;
1472 ret = asn1_uint32_decode (&asn1, &msgsec, &length);
1473 if (ret != ASN1_ERR_NOERROR) {
1474 dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1475 "message security model", ret);
1479 proto_tree_add_text(global_tree, tvb, offset,
1480 length, "Message Security Model: %s",
1481 val_to_str(msgsec, sec_models,
1482 "Unknown model %#x"));
1487 start = asn1.offset;
1488 ret = asn1_header_decode (&asn1, &cls, &con, &tag,
1489 &def, &secparm_length);
1490 length = asn1.offset - start;
1491 if (cls != ASN1_UNI && con != ASN1_PRI &&
1493 dissect_snmp_parse_error(tvb, offset, pinfo,
1494 snmp_tree, "Message Security Parameters",
1495 ASN1_ERR_WRONG_TYPE);
1499 item = proto_tree_add_text(snmp_tree, tvb,
1500 offset, secparm_length + length,
1501 "Message Security Parameters");
1502 secur_tree = proto_item_add_subtree(item,
1504 proto_tree_add_text(secur_tree, tvb, offset,
1506 "Message Security Parameters Length: %d",
1510 ret = asn1_sequence_decode(&asn1, &secparm_length,
1512 if (ret != ASN1_ERR_NOERROR) {
1513 dissect_snmp_parse_error(tvb, offset, pinfo,
1514 snmp_tree, "USM sequence header", ret);
1518 ret = asn1_octet_string_decode (&asn1, &aengineid,
1519 &aengineid_length, &length);
1520 if (ret != ASN1_ERR_NOERROR) {
1521 dissect_snmp_parse_error(tvb, offset, pinfo,
1522 snmp_tree, "authoritative engine id", ret);
1526 proto_tree_add_text(secur_tree, tvb, offset,
1527 length, "Authoritative Engine ID: %s",
1528 bytes_to_str(aengineid, aengineid_length));
1532 ret = asn1_uint32_decode (&asn1, &engineboots, &length);
1533 if (ret != ASN1_ERR_NOERROR) {
1534 dissect_snmp_parse_error(tvb, offset, pinfo,
1535 snmp_tree, "engine boots", ret);
1539 proto_tree_add_text(secur_tree, tvb,
1540 offset, length, "Engine Boots: %d",
1544 ret = asn1_uint32_decode (&asn1, &enginetime, &length);
1545 if (ret != ASN1_ERR_NOERROR) {
1546 dissect_snmp_parse_error(tvb, offset, pinfo,
1547 snmp_tree, "engine time", ret);
1551 proto_tree_add_text(secur_tree, tvb,
1552 offset, length, "Engine Time: %d",
1556 ret = asn1_octet_string_decode (&asn1, &username,
1557 &username_length, &length);
1558 if (ret != ASN1_ERR_NOERROR) {
1559 dissect_snmp_parse_error(tvb, offset, pinfo,
1560 snmp_tree, "user name", ret);
1564 proto_tree_add_text(secur_tree, tvb, offset,
1565 length, "User Name: %.*s",
1567 SAFE_STRING(username));
1571 ret = asn1_octet_string_decode (&asn1, &authpar,
1572 &authpar_length, &length);
1573 if (ret != ASN1_ERR_NOERROR) {
1574 dissect_snmp_parse_error(tvb, offset, pinfo,
1575 snmp_tree, "authentication parameter", ret);
1579 proto_tree_add_text(secur_tree, tvb, offset,
1580 length, "Authentication Parameter: %s",
1581 bytes_to_str(authpar, authpar_length));
1585 ret = asn1_octet_string_decode (&asn1, &privpar,
1586 &privpar_length, &length);
1587 if (ret != ASN1_ERR_NOERROR) {
1588 dissect_snmp_parse_error(tvb, offset, pinfo,
1589 snmp_tree, "privacy parameter", ret);
1593 proto_tree_add_text(secur_tree, tvb, offset,
1594 length, "Privacy Parameter: %s",
1595 bytes_to_str(privpar, privpar_length));
1601 ret = asn1_octet_string_decode (&asn1,
1602 &secparm, &secparm_length, &length);
1603 if (ret != ASN1_ERR_NOERROR) {
1604 dissect_snmp_parse_error(tvb, offset, pinfo,
1605 snmp_tree, "Message Security Parameters",
1610 proto_tree_add_text(snmp_tree, tvb, offset,
1612 "Message Security Parameters Data"
1613 " (%d bytes)", secparm_length);
1619 /* PDU starts here */
1621 ret = asn1_octet_string_decode (&asn1, &cryptpdu,
1622 &cryptpdu_length, &length);
1623 if (ret != ASN1_ERR_NOERROR) {
1624 dissect_snmp_parse_error(tvb, offset, pinfo,
1625 snmp_tree, "encrypted PDU header", ret);
1628 proto_tree_add_text(snmp_tree, tvb, offset, length,
1629 "Encrypted PDU (%d bytes)", length);
1631 if (check_col(pinfo->cinfo, COL_INFO))
1632 col_set_str(pinfo->cinfo, COL_INFO, "Encrypted PDU");
1635 ret = asn1_sequence_decode(&asn1, &global_length, &length);
1636 if (ret != ASN1_ERR_NOERROR) {
1637 dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1642 ret = asn1_octet_string_decode (&asn1, &cengineid,
1643 &cengineid_length, &length);
1644 if (ret != ASN1_ERR_NOERROR) {
1645 dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1646 "context engine id", ret);
1650 proto_tree_add_text(snmp_tree, tvb, offset, length,
1651 "Context Engine ID: %s",
1652 bytes_to_str(cengineid, cengineid_length));
1656 ret = asn1_octet_string_decode (&asn1, &cname,
1657 &cname_length, &length);
1658 if (ret != ASN1_ERR_NOERROR) {
1659 dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1660 "context name", ret);
1664 proto_tree_add_text(snmp_tree, tvb, offset, length,
1665 "Context Name: %.*s", cname_length,
1666 SAFE_STRING(cname));
1672 dissect_snmp_error(tvb, offset, pinfo, snmp_tree,
1673 "PDU for unknown version of SNMP");
1677 start = asn1.offset;
1678 ret = asn1_header_decode (&asn1, &cls, &con, &pdu_type, &def,
1680 if (ret != ASN1_ERR_NOERROR) {
1681 dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1685 if (cls != ASN1_CTX || con != ASN1_CON) {
1686 dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1687 "PDU type", ASN1_ERR_WRONG_TYPE);
1690 dissect_common_pdu(tvb, offset, pinfo, snmp_tree, asn1, pdu_type, start);
1694 dissect_smux_pdu(tvbuff_t *tvb, int offset, packet_info *pinfo,
1695 proto_tree *tree, int proto, gint ett)
1703 char *pdu_type_string;
1713 int password_length;
1715 guchar *application;
1716 int application_length;
1723 proto_tree *smux_tree = NULL;
1724 proto_item *item = NULL;
1728 if (check_col(pinfo->cinfo, COL_PROTOCOL))
1729 col_set_str(pinfo->cinfo, COL_PROTOCOL, "SMUX");
1732 item = proto_tree_add_item(tree, proto, tvb, offset, -1, FALSE);
1733 smux_tree = proto_item_add_subtree(item, ett);
1736 /* NOTE: we have to parse the message piece by piece, since the
1737 * capture length may be less than the message length: a 'global'
1738 * parsing is likely to fail.
1740 /* parse the SNMP header */
1741 asn1_open(&asn1, tvb, offset);
1742 start = asn1.offset;
1743 ret = asn1_header_decode (&asn1, &cls, &con, &pdu_type, &def,
1745 if (ret != ASN1_ERR_NOERROR) {
1746 dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1751 /* Dissect SMUX here */
1752 if (cls == ASN1_APL && con == ASN1_CON && pdu_type == SMUX_MSG_OPEN) {
1753 pdu_type_string = val_to_str(pdu_type, smux_types,
1754 "Unknown PDU type %#x");
1755 if (check_col(pinfo->cinfo, COL_INFO))
1756 col_add_str(pinfo->cinfo, COL_INFO, pdu_type_string);
1757 length = asn1.offset - start;
1759 proto_tree_add_text(smux_tree, tvb, offset, length,
1760 "PDU type: %s", pdu_type_string);
1763 ret = asn1_uint32_decode (&asn1, &version, &length);
1764 if (ret != ASN1_ERR_NOERROR) {
1765 dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1770 proto_tree_add_text(smux_tree, tvb, offset, length,
1771 "Version: %d", version);
1775 ret = asn1_oid_decode (&asn1, ®id, ®id_length, &length);
1776 if (ret != ASN1_ERR_NOERROR) {
1777 dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1778 "registration OID", ret);
1782 oid_string = format_oid(regid, regid_length);
1783 proto_tree_add_text(smux_tree, tvb, offset, length,
1784 "Registration: %s", oid_string);
1790 ret = asn1_octet_string_decode (&asn1, &application,
1791 &application_length, &length);
1792 if (ret != ASN1_ERR_NOERROR) {
1793 dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1794 "application", ret);
1798 proto_tree_add_text(smux_tree, tvb, offset, length,
1799 "Application: %.*s", application_length,
1800 SAFE_STRING(application));
1802 g_free(application);
1805 ret = asn1_octet_string_decode (&asn1, &password,
1806 &password_length, &length);
1807 if (ret != ASN1_ERR_NOERROR) {
1808 dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1813 proto_tree_add_text(smux_tree, tvb, offset, length,
1814 "Password: %.*s", password_length,
1815 SAFE_STRING(password));
1821 if (cls == ASN1_APL && con == ASN1_PRI && pdu_type == SMUX_MSG_CLOSE) {
1822 pdu_type_string = val_to_str(pdu_type, smux_types,
1823 "Unknown PDU type %#x");
1824 if (check_col(pinfo->cinfo, COL_INFO))
1825 col_add_str(pinfo->cinfo, COL_INFO, pdu_type_string);
1826 length = asn1.offset - start;
1828 proto_tree_add_text(smux_tree, tvb, offset, length,
1829 "PDU type: %s", pdu_type_string);
1832 ret = asn1_uint32_value_decode (&asn1, pdu_length, &cause);
1833 if (ret != ASN1_ERR_NOERROR) {
1834 dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1839 proto_tree_add_text(smux_tree, tvb, offset,
1840 pdu_length, "Cause: %s",
1841 val_to_str(cause, smux_close,
1842 "Unknown cause %#x"));
1844 offset += pdu_length;
1847 if (cls == ASN1_APL && con == ASN1_CON && pdu_type == SMUX_MSG_RREQ) {
1848 pdu_type_string = val_to_str(pdu_type, smux_types,
1849 "Unknown PDU type %#x");
1850 if (check_col(pinfo->cinfo, COL_INFO))
1851 col_add_str(pinfo->cinfo, COL_INFO, pdu_type_string);
1852 length = asn1.offset - start;
1854 proto_tree_add_text(smux_tree, tvb, offset, length,
1855 "PDU type: %s", pdu_type_string);
1858 ret = asn1_oid_decode (&asn1, ®id, ®id_length, &length);
1859 if (ret != ASN1_ERR_NOERROR) {
1860 dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1861 "registration subtree", ret);
1865 oid_string = format_oid(regid, regid_length);
1866 proto_tree_add_text(smux_tree, tvb, offset, length,
1867 "Registration: %s", oid_string);
1873 ret = asn1_uint32_decode (&asn1, &priority, &length);
1874 if (ret != ASN1_ERR_NOERROR) {
1875 dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1880 proto_tree_add_text(smux_tree, tvb, offset, length,
1881 "Priority: %d", priority);
1885 ret = asn1_uint32_decode (&asn1, &operation, &length);
1886 if (ret != ASN1_ERR_NOERROR) {
1887 dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1892 proto_tree_add_text(smux_tree, tvb, offset, length,
1894 val_to_str(operation, smux_rreq,
1895 "Unknown operation %#x"));
1900 if (cls == ASN1_APL && con == ASN1_PRI && pdu_type == SMUX_MSG_RRSP) {
1901 pdu_type_string = val_to_str(pdu_type, smux_types,
1902 "Unknown PDU type %#x");
1903 if (check_col(pinfo->cinfo, COL_INFO))
1904 col_add_str(pinfo->cinfo, COL_INFO, pdu_type_string);
1905 length = asn1.offset - start;
1907 proto_tree_add_text(smux_tree, tvb, offset, length,
1908 "PDU type: %s", pdu_type_string);
1911 ret = asn1_uint32_value_decode (&asn1, pdu_length, &priority);
1912 if (ret != ASN1_ERR_NOERROR) {
1913 dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1918 proto_tree_add_text(smux_tree, tvb, offset,
1920 val_to_str(priority, smux_prio,
1923 offset += pdu_length;
1926 if (cls == ASN1_APL && con == ASN1_PRI && pdu_type == SMUX_MSG_SOUT) {
1927 pdu_type_string = val_to_str(pdu_type, smux_types,
1928 "Unknown PDU type %#x");
1929 if (check_col(pinfo->cinfo, COL_INFO))
1930 col_add_str(pinfo->cinfo, COL_INFO, pdu_type_string);
1931 length = asn1.offset - start;
1933 proto_tree_add_text(smux_tree, tvb, offset, length,
1934 "PDU type: %s", pdu_type_string);
1937 ret = asn1_uint32_value_decode (&asn1, pdu_length, &commit);
1938 if (ret != ASN1_ERR_NOERROR) {
1939 dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1944 proto_tree_add_text(smux_tree, tvb, offset,
1946 val_to_str(commit, smux_sout,
1947 "Unknown SOUT Value: %#x"));
1949 offset += pdu_length;
1952 if (cls != ASN1_CTX || con != ASN1_CON) {
1953 dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1954 "PDU type", ASN1_ERR_WRONG_TYPE);
1957 dissect_common_pdu(tvb, offset, pinfo, smux_tree, asn1, pdu_type, start);
1961 dissect_snmp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1963 conversation_t *conversation;
1966 * The first SNMP packet goes to the SNMP port; the second one
1967 * may come from some *other* port, but goes back to the same
1968 * IP address and port as the ones from which the first packet
1969 * came; all subsequent packets presumably go between those two
1970 * IP addresses and ports.
1972 * If this packet went to the SNMP port, we check to see if
1973 * there's already a conversation with one address/port pair
1974 * matching the source IP address and port of this packet,
1975 * the other address matching the destination IP address of this
1976 * packet, and any destination port.
1978 * If not, we create one, with its address 1/port 1 pair being
1979 * the source address/port of this packet, its address 2 being
1980 * the destination address of this packet, and its port 2 being
1981 * wildcarded, and give it the SNMP dissector as a dissector.
1983 if (pinfo->destport == UDP_PORT_SNMP) {
1984 conversation = find_conversation(&pinfo->src, &pinfo->dst, PT_UDP,
1985 pinfo->srcport, 0, NO_PORT_B);
1986 if (conversation == NULL) {
1987 conversation = conversation_new(&pinfo->src, &pinfo->dst, PT_UDP,
1988 pinfo->srcport, 0, NO_PORT2);
1989 conversation_set_dissector(conversation, snmp_handle);
1993 dissect_snmp_pdu(tvb, 0, pinfo, tree, "SNMP", proto_snmp, ett_snmp);
1997 dissect_smux(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1999 dissect_smux_pdu(tvb, 0, pinfo, tree, proto_smux, ett_smux);
2003 proto_register_snmp(void)
2005 static hf_register_info hf[] = {
2007 { "SNMPv3 Flags", "snmpv3.flags", FT_UINT8, BASE_HEX, NULL,
2009 { &hf_snmpv3_flags_auth,
2010 { "Authenticated", "snmpv3.flags.auth", FT_BOOLEAN, 8,
2011 TFS(&flags_set_truth), TH_AUTH, "", HFILL }},
2012 { &hf_snmpv3_flags_crypt,
2013 { "Encrypted", "snmpv3.flags.crypt", FT_BOOLEAN, 8,
2014 TFS(&flags_set_truth), TH_CRYPT, "", HFILL }},
2015 { &hf_snmpv3_flags_report,
2016 { "Reportable", "snmpv3.flags.report", FT_BOOLEAN, 8,
2017 TFS(&flags_set_truth), TH_REPORT, "", HFILL }},
2019 static gint *ett[] = {
2023 &ett_parameters_qos,
2029 #ifdef HAVE_UCD_SNMP_SNMP_H
2031 snmp_set_suffix_only(2);
2032 #endif /* HAVE_UCD_SNMP_SNMP_H */
2033 proto_snmp = proto_register_protocol("Simple Network Management Protocol",
2035 proto_smux = proto_register_protocol("SNMP Multiplex Protocol",
2037 proto_register_field_array(proto_snmp, hf, array_length(hf));
2038 proto_register_subtree_array(ett, array_length(ett));
2039 snmp_handle = create_dissector_handle(dissect_snmp, proto_snmp);
2043 proto_reg_handoff_snmp(void)
2045 dissector_handle_t smux_handle;
2047 dissector_add("udp.port", UDP_PORT_SNMP, snmp_handle);
2048 dissector_add("udp.port", UDP_PORT_SNMP_TRAP, snmp_handle);
2049 smux_handle = create_dissector_handle(dissect_smux, proto_smux);
2050 dissector_add("tcp.port", TCP_PORT_SMUX, smux_handle);
2051 dissector_add("ethertype", ETHERTYPE_SNMP, snmp_handle);
2052 dissector_add("ipx.socket", IPX_SOCKET_SNMP_AGENT, snmp_handle);
2053 dissector_add("ipx.socket", IPX_SOCKET_SNMP_SINK, snmp_handle);
2054 data_handle = find_dissector("data");