137b6425dcb6f82d830e56d03037d0c8c46735a3
[obnox/wireshark/wip.git] / packet-snmp.c
1 /* packet-snmp.c
2  * Routines for SNMP (simple network management protocol)
3  * Copyright (C) 1998 Didier Jorand
4  *
5  * See RFC 1157 for SNMPv1.
6  *
7  * See RFCs 1901, 1905, and 1906 for SNMPv2c.
8  *
9  * See RFCs 1905, 1906, 1909, and 1910 for SNMPv2u [historic].
10  *
11  * See RFCs 2570-2576 for SNMPv3
12  *
13  * $Id: packet-snmp.c,v 1.112 2003/09/04 05:16:18 guy Exp $
14  *
15  * Ethereal - Network traffic analyzer
16  * By Gerald Combs <gerald@ethereal.com>
17  * Copyright 1998 Gerald Combs
18  *
19  * Some stuff from:
20  *
21  * GXSNMP -- An snmp mangament application
22  * Copyright (C) 1998 Gregory McLean & Jochen Friedrich
23  * Beholder RMON ethernet network monitor,Copyright (C) 1993 DNPAP group
24  *
25  * This program is free software; you can redistribute it and/or
26  * modify it under the terms of the GNU General Public License
27  * as published by the Free Software Foundation; either version 2
28  * of the License, or (at your option) any later version.
29  *
30  * This program is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  * GNU General Public License for more details.
34  *
35  * You should have received a copy of the GNU General Public License
36  * along with this program; if not, write to the Free Software
37  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
38  */
39
40 #ifdef HAVE_CONFIG_H
41 # include "config.h"
42 #endif
43
44 #include <stdio.h>
45 #include <string.h>
46 #include <ctype.h>
47
48 #include <glib.h>
49
50 #include <epan/packet.h>
51 #include <epan/strutil.h>
52 #include <epan/conversation.h>
53 #include "etypes.h"
54 #include "prefs.h"
55 #include "packet-ipx.h"
56 #include "packet-hpext.h"
57
58 #ifdef HAVE_SOME_SNMP
59
60 #ifdef HAVE_NET_SNMP
61 # include <net-snmp/net-snmp-config.h>
62 # include <net-snmp/mib_api.h>
63 # include <net-snmp/library/default_store.h>
64 # include <net-snmp/config_api.h>
65 #else /* HAVE_NET_SNMP */
66 # include <ucd-snmp/ucd-snmp-config.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/read_config.h>
73 # include <ucd-snmp/tools.h>
74 #endif /* HAVE_NET_SNMP */
75
76 #ifndef NETSNMP_DS_LIBRARY_ID
77 # define NETSNMP_DS_LIBRARY_ID DS_LIBRARY_ID
78 # define NETSNMP_DS_LIB_NO_TOKEN_WARNINGS DS_LIB_NO_TOKEN_WARNINGS
79 # define NETSNMP_DS_LIB_PRINT_SUFFIX_ONLY DS_LIB_PRINT_SUFFIX_ONLY
80 # define netsnmp_ds_set_boolean ds_set_boolean
81 # define netsnmp_ds_set_int ds_set_int
82 #endif
83
84 #ifdef WIN32
85 # include <epan/filesystem.h>
86 #endif /* WIN32 */
87
88    /*
89     * Define values "sprint_realloc_value()" expects.
90     */
91 # define VALTYPE_INTEGER        ASN_INTEGER
92 # define VALTYPE_COUNTER        ASN_COUNTER
93 # define VALTYPE_GAUGE          ASN_GAUGE
94 # define VALTYPE_TIMETICKS      ASN_TIMETICKS
95 # define VALTYPE_STRING         ASN_OCTET_STR
96 # define VALTYPE_IPADDR         ASN_IPADDRESS
97 # define VALTYPE_OPAQUE         ASN_OPAQUE
98 # define VALTYPE_NSAP           ASN_NSAP
99 # define VALTYPE_OBJECTID       ASN_OBJECT_ID
100 # define VALTYPE_BITSTR         ASN_BIT_STR
101 # define VALTYPE_COUNTER64      ASN_COUNTER64
102
103 #endif /* HAVE_SOME_SNMP */
104
105 #include "asn1.h"
106
107 #include "packet-snmp.h"
108 #include "format-oid.h"
109
110 /* Take a pointer that may be null and return a pointer that's not null
111    by turning null pointers into pointers to the above null string,
112    and, if the argument pointer wasn't null, make sure we handle
113    non-printable characters in the string by escaping them. */
114 #define SAFE_STRING(s, l)       (((s) != NULL) ? format_text((s), (l)) : "")
115
116 static int proto_snmp = -1;
117
118 static gboolean display_oid = TRUE;
119
120 static gint ett_snmp = -1;
121 static gint ett_parameters = -1;
122 static gint ett_parameters_qos = -1;
123 static gint ett_global = -1;
124 static gint ett_flags = -1;
125 static gint ett_secur = -1;
126
127 static int hf_snmp_version = -1;
128 static int hf_snmp_community = -1;
129 static int hf_snmp_request_id = -1;
130 static int hf_snmp_pdutype = -1;
131 static int hf_snmp_agent = -1;
132 static int hf_snmp_enterprise = -1;
133 static int hf_snmp_error_status = -1;
134 static int hf_snmp_oid = -1;
135 static int hf_snmp_traptype = -1;
136 static int hf_snmp_spectraptype = -1;
137 static int hf_snmp_timestamp = -1;
138 static int hf_snmpv3_flags = -1;
139 static int hf_snmpv3_flags_auth = -1;
140 static int hf_snmpv3_flags_crypt = -1;
141 static int hf_snmpv3_flags_report = -1;
142
143 static int proto_smux = -1;
144
145 static gint ett_smux = -1;
146
147 static int hf_smux_version = -1;
148 static int hf_smux_pdutype = -1;
149
150 static dissector_handle_t snmp_handle;
151 static dissector_handle_t data_handle;
152
153 #define TH_AUTH   0x01
154 #define TH_CRYPT  0x02
155 #define TH_REPORT 0x04
156
157 #define UDP_PORT_SNMP           161
158 #define UDP_PORT_SNMP_TRAP      162
159 #define TCP_PORT_SMUX           199
160
161 /* Protocol version numbers */
162 #define SNMP_VERSION_1  0
163 #define SNMP_VERSION_2c 1
164 #define SNMP_VERSION_2u 2
165 #define SNMP_VERSION_3  3
166
167 static const value_string versions[] = {
168         { SNMP_VERSION_1,       "1" },
169         { SNMP_VERSION_2c,      "2C" },
170         { SNMP_VERSION_2u,      "2U" },
171         { SNMP_VERSION_3,       "3" },
172         { 0,                    NULL },
173 };
174
175 /* PDU types */
176 #define SNMP_MSG_GET            0
177 #define SNMP_MSG_GETNEXT        1
178 #define SNMP_MSG_RESPONSE       2
179 #define SNMP_MSG_SET            3
180 #define SNMP_MSG_TRAP           4
181
182 #define SNMP_MSG_GETBULK        5
183 #define SNMP_MSG_INFORM         6
184 #define SNMP_MSG_TRAP2          7
185 #define SNMP_MSG_REPORT         8
186
187 static const value_string pdu_types[] = {
188         { SNMP_MSG_GET,         "GET" },
189         { SNMP_MSG_GETNEXT,     "GET-NEXT" },
190         { SNMP_MSG_SET,         "SET" },
191         { SNMP_MSG_RESPONSE,    "RESPONSE" },
192         { SNMP_MSG_TRAP,        "TRAP-V1" },
193         { SNMP_MSG_GETBULK,     "GETBULK" },
194         { SNMP_MSG_INFORM,      "INFORM" },
195         { SNMP_MSG_TRAP2,       "TRAP-V2" },
196         { SNMP_MSG_REPORT,      "REPORT" },
197         { 0,                    NULL }
198 };
199
200 /* SMUX PDU types */
201 #define SMUX_MSG_OPEN           0
202 #define SMUX_MSG_CLOSE          1
203 #define SMUX_MSG_RREQ           2
204 #define SMUX_MSG_RRSP           3
205 #define SMUX_MSG_SOUT           4
206
207 static const value_string smux_types[] = {
208         { SMUX_MSG_OPEN,        "Open" },
209         { SMUX_MSG_CLOSE,       "Close" },
210         { SMUX_MSG_RREQ,        "Registration Request" },
211         { SMUX_MSG_RRSP,        "Registration Response" },
212         { SMUX_MSG_SOUT,        "Commit Or Rollback" },
213         { 0,                    NULL }
214 };
215
216 /* SMUX Closing causes */
217 #define SMUX_CLOSE_DOWN                 0
218 #define SMUX_CLOSE_VERSION              1
219 #define SMUX_CLOSE_PACKET               2
220 #define SMUX_CLOSE_PROTOCOL             3
221 #define SMUX_CLOSE_INTERNAL             4
222 #define SMUX_CLOSE_NOAUTH               5
223
224 static const value_string smux_close[] = {
225         { SMUX_CLOSE_DOWN,      "Going down" },
226         { SMUX_CLOSE_VERSION,   "Unsupported Version" },
227         { SMUX_CLOSE_PACKET,    "Packet Format Error" },
228         { SMUX_CLOSE_PROTOCOL,  "Protocol Error" },
229         { SMUX_CLOSE_INTERNAL,  "Internal Error" },
230         { SMUX_CLOSE_NOAUTH,    "Unauthorized" },
231         { 0,                    NULL }
232 };
233
234 /* SMUX Request codes */
235 #define SMUX_RREQ_DELETE                0
236 #define SMUX_RREQ_READONLY              1
237 #define SMUX_RREQ_READWRITE             2
238
239 static const value_string smux_rreq[] = {
240         { SMUX_RREQ_DELETE,     "Delete" },
241         { SMUX_RREQ_READONLY,   "Read Only" },
242         { SMUX_RREQ_READWRITE,  "Read Write" },
243         { 0,                    NULL }
244 };
245
246 static const value_string smux_prio[] = {
247         { -1,                           "Failure" },
248         { 0,                            NULL }
249 };
250
251 /* SMUX SOut codes */
252 #define SMUX_SOUT_COMMIT                0
253 #define SMUX_SOUT_ROLLBACK              1
254
255 static const value_string smux_sout[] = {
256         { SMUX_SOUT_COMMIT,             "Commit" },
257         { SMUX_SOUT_ROLLBACK,           "Rollback" },
258         { 0,                            NULL }
259 };
260
261 /* Error status values */
262 #ifndef SNMP_ERR_NOERROR
263 #define SNMP_ERR_NOERROR                0
264 #endif
265 #ifndef SNMP_ERR_TOOBIG
266 #define SNMP_ERR_TOOBIG                 1
267 #endif
268 #ifndef SNMP_ERR_NOSUCHNAME
269 #define SNMP_ERR_NOSUCHNAME             2
270 #endif
271 #ifndef SNMP_ERR_BADVALUE
272 #define SNMP_ERR_BADVALUE               3
273 #endif
274 #ifndef SNMP_ERR_READONLY
275 #define SNMP_ERR_READONLY               4
276 #endif
277 #ifndef SNMP_ERR_GENERR
278 #define SNMP_ERR_GENERR                 5
279 #endif
280 #ifndef SNMP_ERR_NOACCESS
281 #define SNMP_ERR_NOACCESS               6
282 #endif
283 #ifndef SNMP_ERR_WRONGTYPE
284 #define SNMP_ERR_WRONGTYPE              7
285 #endif
286 #ifndef SNMP_ERR_WRONGLENGTH
287 #define SNMP_ERR_WRONGLENGTH            8
288 #endif
289 #ifndef SNMP_ERR_WRONGENCODING
290 #define SNMP_ERR_WRONGENCODING          9
291 #endif
292 #ifndef SNMP_ERR_WRONGVALUE
293 #define SNMP_ERR_WRONGVALUE             10
294 #endif
295 #ifndef SNMP_ERR_NOCREATION
296 #define SNMP_ERR_NOCREATION             11
297 #endif
298 #ifndef SNMP_ERR_INCONSISTENTVALUE
299 #define SNMP_ERR_INCONSISTENTVALUE      12
300 #endif
301 #ifndef SNMP_ERR_RESOURCEUNAVAILABLE
302 #define SNMP_ERR_RESOURCEUNAVAILABLE    13
303 #endif
304 #ifndef SNMP_ERR_COMMITFAILED
305 #define SNMP_ERR_COMMITFAILED           14
306 #endif
307 #ifndef SNMP_ERR_UNDOFAILED
308 #define SNMP_ERR_UNDOFAILED             15
309 #endif
310 #ifndef SNMP_ERR_AUTHORIZATIONERROR
311 #define SNMP_ERR_AUTHORIZATIONERROR     16
312 #endif
313 #ifndef SNMP_ERR_NOTWRITABLE
314 #define SNMP_ERR_NOTWRITABLE            17
315 #endif
316 #ifndef SNMP_ERR_INCONSISTENTNAME
317 #define SNMP_ERR_INCONSISTENTNAME       18
318 #endif
319
320 static const value_string error_statuses[] = {
321         { SNMP_ERR_NOERROR,             "NO ERROR" },
322         { SNMP_ERR_TOOBIG,              "TOOBIG" },
323         { SNMP_ERR_NOSUCHNAME,          "NO SUCH NAME" },
324         { SNMP_ERR_BADVALUE,            "BAD VALUE" },
325         { SNMP_ERR_READONLY,            "READ ONLY" },
326         { SNMP_ERR_GENERR,              "GENERIC ERROR" },
327         { SNMP_ERR_NOACCESS,            "NO ACCESS" },
328         { SNMP_ERR_WRONGTYPE,           "WRONG TYPE" },
329         { SNMP_ERR_WRONGLENGTH,         "WRONG LENGTH" },
330         { SNMP_ERR_WRONGENCODING,       "WRONG ENCODING" },
331         { SNMP_ERR_WRONGVALUE,          "WRONG VALUE" },
332         { SNMP_ERR_NOCREATION,          "NO CREATION" },
333         { SNMP_ERR_INCONSISTENTVALUE,   "INCONSISTENT VALUE" },
334         { SNMP_ERR_RESOURCEUNAVAILABLE, "RESOURCE UNAVAILABLE" },
335         { SNMP_ERR_COMMITFAILED,        "COMMIT FAILED" },
336         { SNMP_ERR_UNDOFAILED,          "UNDO FAILED" },
337         { SNMP_ERR_AUTHORIZATIONERROR,  "AUTHORIZATION ERROR" },
338         { SNMP_ERR_NOTWRITABLE,         "NOT WRITABLE" },
339         { SNMP_ERR_INCONSISTENTNAME,    "INCONSISTENT NAME" },
340         { 0,                            NULL }
341 };
342
343 /* General SNMP V1 Traps */
344
345 #ifndef SNMP_TRAP_COLDSTART
346 #define SNMP_TRAP_COLDSTART             0
347 #endif
348 #ifndef SNMP_TRAP_WARMSTART
349 #define SNMP_TRAP_WARMSTART             1
350 #endif
351 #ifndef SNMP_TRAP_LINKDOWN
352 #define SNMP_TRAP_LINKDOWN              2
353 #endif
354 #ifndef SNMP_TRAP_LINKUP
355 #define SNMP_TRAP_LINKUP                3
356 #endif
357 #ifndef SNMP_TRAP_AUTHFAIL
358 #define SNMP_TRAP_AUTHFAIL              4
359 #endif
360 #ifndef SNMP_TRAP_EGPNEIGHBORLOSS
361 #define SNMP_TRAP_EGPNEIGHBORLOSS       5
362 #endif
363 #ifndef SNMP_TRAP_ENTERPRISESPECIFIC
364 #define SNMP_TRAP_ENTERPRISESPECIFIC    6
365 #endif
366
367 static const value_string trap_types[] = {
368         { SNMP_TRAP_COLDSTART,          "COLD START" },
369         { SNMP_TRAP_WARMSTART,          "WARM START" },
370         { SNMP_TRAP_LINKDOWN,           "LINK DOWN" },
371         { SNMP_TRAP_LINKUP,             "LINK UP" },
372         { SNMP_TRAP_AUTHFAIL,           "AUTHENTICATION FAILED" },
373         { SNMP_TRAP_EGPNEIGHBORLOSS,    "EGP NEIGHBORLOSS" },
374         { SNMP_TRAP_ENTERPRISESPECIFIC, "ENTERPRISE SPECIFIC" },
375         { 0,                            NULL }
376 };
377
378 /* Security Models */
379
380 #define SNMP_SEC_ANY                    0
381 #define SNMP_SEC_V1                     1
382 #define SNMP_SEC_V2C                    2
383 #define SNMP_SEC_USM                    3
384
385 static const value_string sec_models[] = {
386         { SNMP_SEC_ANY,                 "Any" },
387         { SNMP_SEC_V1,                  "V1" },
388         { SNMP_SEC_V2C,                 "V2C" },
389         { SNMP_SEC_USM,                 "USM" },
390         { 0,                            NULL }
391 };
392
393 /* SNMP Tags */
394
395 #define SNMP_IPA    0           /* IP Address */
396 #define SNMP_CNT    1           /* Counter (Counter32) */
397 #define SNMP_GGE    2           /* Gauge (Gauge32) */
398 #define SNMP_TIT    3           /* TimeTicks */
399 #define SNMP_OPQ    4           /* Opaque */
400 #define SNMP_NSP    5           /* NsapAddress */
401 #define SNMP_C64    6           /* Counter64 */
402 #define SNMP_U32    7           /* Uinteger32 */
403
404 #define SERR_NSO    0
405 #define SERR_NSI    1
406 #define SERR_EOM    2
407
408 /* SNMPv1 Types */
409
410 #define SNMP_NULL                0
411 #define SNMP_INTEGER             1    /* l  */
412 #define SNMP_OCTETSTR            2    /* c  */
413 #define SNMP_DISPLAYSTR          2    /* c  */
414 #define SNMP_OBJECTID            3    /* ul */
415 #define SNMP_IPADDR              4    /* uc */
416 #define SNMP_COUNTER             5    /* ul */
417 #define SNMP_GAUGE               6    /* ul */
418 #define SNMP_TIMETICKS           7    /* ul */
419 #define SNMP_OPAQUE              8    /* c  */
420
421 /* additional SNMPv2 Types */
422
423 #define SNMP_UINTEGER            5    /* ul */
424 #define SNMP_BITSTR              9    /* uc */
425 #define SNMP_NSAP               10    /* uc */
426 #define SNMP_COUNTER64          11    /* ul */
427 #define SNMP_NOSUCHOBJECT       12
428 #define SNMP_NOSUCHINSTANCE     13
429 #define SNMP_ENDOFMIBVIEW       14
430
431 typedef struct _SNMP_CNV SNMP_CNV;
432
433 struct _SNMP_CNV
434 {
435   guint class;
436   guint tag;
437   gint  syntax;
438   gchar *name;
439 };
440
441 static SNMP_CNV SnmpCnv [] =
442 {
443   {ASN1_UNI, ASN1_NUL, SNMP_NULL,      "NULL"},
444   {ASN1_UNI, ASN1_INT, SNMP_INTEGER,   "INTEGER"},
445   {ASN1_UNI, ASN1_OTS, SNMP_OCTETSTR,  "OCTET STRING"},
446   {ASN1_UNI, ASN1_OJI, SNMP_OBJECTID,  "OBJECTID"},
447   {ASN1_APL, SNMP_IPA, SNMP_IPADDR,    "IPADDR"},
448   {ASN1_APL, SNMP_CNT, SNMP_COUNTER,   "COUNTER"},  /* Counter32 */
449   {ASN1_APL, SNMP_GGE, SNMP_GAUGE,     "GAUGE"},    /* Gauge32 == Unsigned32  */
450   {ASN1_APL, SNMP_TIT, SNMP_TIMETICKS, "TIMETICKS"},
451   {ASN1_APL, SNMP_OPQ, SNMP_OPAQUE,    "OPAQUE"},
452
453 /* SNMPv2 data types and errors */
454
455   {ASN1_UNI, ASN1_BTS, SNMP_BITSTR,         "BITSTR"},
456   {ASN1_APL, SNMP_C64, SNMP_COUNTER64,      "COUNTER64"},
457   {ASN1_CTX, SERR_NSO, SNMP_NOSUCHOBJECT,   "NOSUCHOBJECT"},
458   {ASN1_CTX, SERR_NSI, SNMP_NOSUCHINSTANCE, "NOSUCHINSTANCE"},
459   {ASN1_CTX, SERR_EOM, SNMP_ENDOFMIBVIEW,   "ENDOFMIBVIEW"},
460   {0,       0,         -1,                  NULL}
461 };
462
463 /*
464  * NAME:        g_snmp_tag_cls2syntax
465  * SYNOPSIS:    gboolean g_snmp_tag_cls2syntax
466  *                  (
467  *                      guint    tag,
468  *                      guint    cls,
469  *                      gushort *syntax
470  *                  )
471  * DESCRIPTION: Converts ASN1 tag and class to Syntax tag and name.
472  *              See SnmpCnv for conversion.
473  * RETURNS:     name on success, NULL on failure
474  */
475
476 static gchar *
477 snmp_tag_cls2syntax ( guint tag, guint cls, gushort *syntax)
478 {
479     SNMP_CNV *cnv;
480
481     cnv = SnmpCnv;
482     while (cnv->syntax != -1)
483     {
484         if (cnv->tag == tag && cnv->class == cls)
485         {
486             *syntax = cnv->syntax;
487             return cnv->name;
488         }
489         cnv++;
490     }
491     return NULL;
492 }
493
494 static void
495 dissect_snmp_parse_error(tvbuff_t *tvb, int offset, packet_info *pinfo,
496                    proto_tree *tree, const char *field_name, int ret)
497 {
498         char *errstr;
499
500         errstr = asn1_err_to_str(ret);
501
502         if (check_col(pinfo->cinfo, COL_INFO)) {
503                 col_add_fstr(pinfo->cinfo, COL_INFO,
504                     "ERROR: Couldn't parse %s: %s", field_name, errstr);
505         }
506         if (tree != NULL) {
507                 proto_tree_add_text(tree, tvb, offset, 0,
508                     "ERROR: Couldn't parse %s: %s", field_name, errstr);
509                 call_dissector(data_handle,
510                     tvb_new_subset(tvb, offset, -1, -1), pinfo, tree);
511         }
512 }
513
514 static void
515 dissect_snmp_error(tvbuff_t *tvb, int offset, packet_info *pinfo,
516                    proto_tree *tree, const char *message)
517 {
518         if (check_col(pinfo->cinfo, COL_INFO))
519                 col_add_str(pinfo->cinfo, COL_INFO, message);
520
521         if (tree != NULL) {
522                 proto_tree_add_text(tree, tvb, offset, 0, "%s", message);
523                 call_dissector(data_handle,
524                     tvb_new_subset(tvb, offset, -1, -1), pinfo, tree);
525         }
526 }
527
528 gchar *
529 format_oid(subid_t *oid, guint oid_length)
530 {
531         char *result;
532         int result_len;
533         int len;
534         unsigned int i;
535         char *buf;
536 #ifdef HAVE_SOME_SNMP
537         guchar *oid_string;
538         size_t oid_string_len;
539         size_t oid_out_len;
540 #endif
541
542         result_len = oid_length * 22;
543
544 #ifdef HAVE_SOME_SNMP
545         /*
546          * Get the decoded form of the OID, and add its length to the
547          * length of the result string.
548          *
549          * XXX - check for "sprint_realloc_objid()" failure.
550          */
551         oid_string_len = 256;
552         oid_string = g_malloc(oid_string_len);
553         *oid_string = '\0';
554         oid_out_len = 0;
555         sprint_realloc_objid(&oid_string, &oid_string_len, &oid_out_len, 1,
556             oid, oid_length);
557         result_len += strlen(oid_string) + 3;
558 #endif
559
560         result = g_malloc(result_len + 1);
561         buf = result;
562         len = sprintf(buf, "%lu", (unsigned long)oid[0]);
563         buf += len;
564         for (i = 1; i < oid_length;i++) {
565                 len = sprintf(buf, ".%lu", (unsigned long)oid[i]);
566                 buf += len;
567         }
568
569 #ifdef HAVE_SOME_SNMP
570         /*
571          * Append the decoded form of the OID.
572          */
573         sprintf(buf, " (%s)", oid_string);
574         g_free(oid_string);
575 #endif
576
577         return result;
578 }
579
580 /* returns the decoded (can be NULL) and non_decoded OID strings,
581    returned pointers shall be freed by the caller */
582 void 
583 new_format_oid(subid_t *oid, guint oid_length, 
584                gchar **non_decoded, gchar **decoded)
585 {
586         int len;
587         unsigned int i;
588         char *buf;
589
590 #ifdef HAVE_SOME_SNMP
591         guchar *oid_string;
592         size_t oid_string_len;
593         size_t oid_out_len;
594
595         /*
596          * Get the decoded form of the OID, and add its length to the
597          * length of the result string.
598          */
599
600         oid_string_len = 256;
601         oid_string = g_malloc(oid_string_len);
602         *oid_string = '\0';
603         oid_out_len = 0;
604         sprint_realloc_objid(&oid_string, &oid_string_len, &oid_out_len, 1,
605                              oid, oid_length);
606         *decoded = oid_string;
607 #else
608         *decoded = NULL;
609 #endif
610
611         *non_decoded = g_malloc(oid_length * 22 + 1);
612         buf = *non_decoded;
613         len = sprintf(buf, "%lu", (unsigned long)oid[0]);
614         buf += len;
615         for (i = 1; i < oid_length; i++) {
616           len = sprintf(buf, ".%lu", (unsigned long)oid[i]);
617           buf += len;
618         }
619 }
620
621 #ifdef HAVE_SOME_SNMP
622 static guchar *
623 check_var_length(guint vb_length, guint required_length)
624 {
625         guchar *buf;
626         static const char badlen_fmt[] = "Length is %u, should be %u";
627
628         if (vb_length != required_length) {
629                 /* Enough room for the largest "Length is XXX,
630                    should be XXX" message - 10 digits for each
631                    XXX. */
632                 buf = g_malloc(sizeof badlen_fmt + 10 + 10);
633                 sprintf(buf, badlen_fmt, vb_length, required_length);
634                 return buf;
635         }
636         return NULL;    /* length is OK */
637 }
638
639 static guchar *
640 format_var(struct variable_list *variable, subid_t *variable_oid,
641     guint variable_oid_length, gushort vb_type, guint val_len)
642 {
643         guchar *buf;
644         size_t buf_len;
645         size_t out_len;
646
647         switch (vb_type) {
648
649         case SNMP_IPADDR:
650                 /* Length has to be 4 bytes. */
651                 buf = check_var_length(val_len, 4);
652                 if (buf != NULL)
653                         return buf;     /* it's not 4 bytes */
654                 break;
655
656         case SNMP_COUNTER64:
657                 /* Length has to be 8 bytes. */
658                 buf = check_var_length(val_len, 8);
659                 if (buf != NULL)
660                         return buf;     /* it's not 8 bytes */
661                 break;
662
663         default:
664                 break;
665         }
666
667         variable->next_variable = NULL;
668         variable->name = variable_oid;
669         variable->name_length = variable_oid_length;
670         switch (vb_type) {
671
672         case SNMP_INTEGER:
673                 variable->type = VALTYPE_INTEGER;
674                 break;
675
676         case SNMP_COUNTER:
677                 variable->type = VALTYPE_COUNTER;
678                 break;
679
680         case SNMP_GAUGE:
681                 variable->type = VALTYPE_GAUGE;
682                 break;
683
684         case SNMP_TIMETICKS:
685                 variable->type = VALTYPE_TIMETICKS;
686                 break;
687
688         case SNMP_OCTETSTR:
689                 variable->type = VALTYPE_STRING;
690                 break;
691
692         case SNMP_IPADDR:
693                 variable->type = VALTYPE_IPADDR;
694                 break;
695
696         case SNMP_OPAQUE:
697                 variable->type = VALTYPE_OPAQUE;
698                 break;
699
700         case SNMP_NSAP:
701                 variable->type = VALTYPE_NSAP;
702                 break;
703
704         case SNMP_OBJECTID:
705                 variable->type = VALTYPE_OBJECTID;
706                 break;
707
708         case SNMP_BITSTR:
709                 variable->type = VALTYPE_BITSTR;
710                 break;
711
712         case SNMP_COUNTER64:
713                 variable->type = VALTYPE_COUNTER64;
714                 break;
715         }
716         variable->val_len = val_len;
717
718         /*
719          * XXX - check for "sprint_realloc_objid()" failure.
720          */
721         buf_len = 256;
722         buf = g_malloc(buf_len);
723         *buf = '\0';
724         out_len = 0;
725         sprint_realloc_value(&buf, &buf_len, &out_len, 1,  variable_oid,
726             variable_oid_length, variable);
727         return buf;
728 }
729 #endif
730
731 static int
732 snmp_variable_decode(proto_tree *snmp_tree,
733     subid_t *variable_oid
734 #ifndef HAVE_SOME_SNMP
735         _U_
736 #endif
737     ,
738     guint variable_oid_length
739 #ifndef HAVE_SOME_SNMP
740         _U_
741 #endif
742     ,
743     ASN1_SCK *asn1, int offset, guint *lengthp)
744 {
745         int start;
746         guint length;
747         gboolean def;
748         guint vb_length;
749         gushort vb_type;
750         gchar *vb_type_name;
751         int ret;
752         guint cls, con, tag;
753
754         gint32 vb_integer_value;
755         guint32 vb_uinteger_value;
756
757         guint8 *vb_octet_string;
758
759         subid_t *vb_oid;
760         guint vb_oid_length;
761
762         gchar *vb_display_string;
763
764 #ifdef HAVE_SOME_SNMP
765         struct variable_list variable;
766         long value;
767 #else /* HAVE_SOME_SNMP */
768         unsigned int i;
769         gchar *buf;
770         int len;
771 #endif  /* HAVE_SOME_SNMP */
772
773         /* parse the type of the object */
774         start = asn1->offset;
775         ret = asn1_header_decode (asn1, &cls, &con, &tag, &def, &vb_length);
776         if (ret != ASN1_ERR_NOERROR)
777                 return ret;
778         if (!def)
779                 return ASN1_ERR_LENGTH_NOT_DEFINITE;
780
781         /* Convert the class, constructed flag, and tag to a type. */
782         vb_type_name = snmp_tag_cls2syntax(tag, cls, &vb_type);
783         if (vb_type_name == NULL) {
784                 /*
785                  * Unsupported type.
786                  * Dissect the value as an opaque string of octets.
787                  */
788                 vb_type_name = "unsupported type";
789                 vb_type = SNMP_OPAQUE;
790         }
791
792         /* parse the value */
793         switch (vb_type) {
794
795         case SNMP_INTEGER:
796                 ret = asn1_int32_value_decode(asn1, vb_length,
797                     &vb_integer_value);
798                 if (ret != ASN1_ERR_NOERROR)
799                         return ret;
800                 length = asn1->offset - start;
801                 if (snmp_tree) {
802 #ifdef HAVE_SOME_SNMP
803                         value = vb_integer_value;
804                         variable.val.integer = &value;
805                         vb_display_string = format_var(&variable,
806                             variable_oid, variable_oid_length, vb_type,
807                             vb_length);
808                         proto_tree_add_text(snmp_tree, asn1->tvb, offset,
809                             length,
810                             "Value: %s", vb_display_string);
811                         g_free(vb_display_string);
812 #else /* HAVE_SOME_SNMP */
813                         proto_tree_add_text(snmp_tree, asn1->tvb, offset,
814                             length,
815                             "Value: %s: %d (%#x)", vb_type_name,
816                             vb_integer_value, vb_integer_value);
817 #endif /* HAVE_SOME_SNMP */
818                 }
819                 break;
820
821         case SNMP_COUNTER:
822         case SNMP_GAUGE:
823         case SNMP_TIMETICKS:
824                 ret = asn1_uint32_value_decode(asn1, vb_length,
825                     &vb_uinteger_value);
826                 if (ret != ASN1_ERR_NOERROR)
827                         return ret;
828                 length = asn1->offset - start;
829                 if (snmp_tree) {
830 #ifdef HAVE_SOME_SNMP
831                         value = vb_uinteger_value;
832                         variable.val.integer = &value;
833                         vb_display_string = format_var(&variable,
834                             variable_oid, variable_oid_length, vb_type,
835                             vb_length);
836                         proto_tree_add_text(snmp_tree, asn1->tvb, offset,
837                             length,
838                             "Value: %s", vb_display_string);
839                         g_free(vb_display_string);
840 #else /* HAVE_SOME_SNMP */
841                         proto_tree_add_text(snmp_tree, asn1->tvb, offset,
842                             length,
843                             "Value: %s: %u (%#x)", vb_type_name,
844                             vb_uinteger_value, vb_uinteger_value);
845 #endif /* HAVE_SOME_SNMP */
846                 }
847                 break;
848
849         case SNMP_OCTETSTR:
850         case SNMP_IPADDR:
851         case SNMP_OPAQUE:
852         case SNMP_NSAP:
853         case SNMP_BITSTR:
854         case SNMP_COUNTER64:
855                 ret = asn1_string_value_decode (asn1, vb_length,
856                     &vb_octet_string);
857                 if (ret != ASN1_ERR_NOERROR)
858                         return ret;
859                 length = asn1->offset - start;
860                 if (snmp_tree) {
861 #ifdef HAVE_SOME_SNMP
862                         variable.val.string = vb_octet_string;
863                         vb_display_string = format_var(&variable,
864                             variable_oid, variable_oid_length, vb_type,
865                             vb_length);
866                         proto_tree_add_text(snmp_tree, asn1->tvb, offset,
867                             length,
868                             "Value: %s", vb_display_string);
869                         g_free(vb_display_string);
870 #else /* HAVE_SOME_SNMP */
871                         /*
872                          * If some characters are not printable, display
873                          * the string as bytes.
874                          */
875                         for (i = 0; i < vb_length; i++) {
876                                 if (!(isprint(vb_octet_string[i])
877                                     || isspace(vb_octet_string[i])))
878                                         break;
879                         }
880                         if (i < vb_length) {
881                                 /*
882                                  * We stopped, due to a non-printable
883                                  * character, before we got to the end
884                                  * of the string.
885                                  */
886                                 vb_display_string = g_malloc(4*vb_length);
887                                 buf = &vb_display_string[0];
888                                 len = sprintf(buf, "%03u", vb_octet_string[0]);
889                                 buf += len;
890                                 for (i = 1; i < vb_length; i++) {
891                                         len = sprintf(buf, ".%03u",
892                                             vb_octet_string[i]);
893                                         buf += len;
894                                 }
895                                 proto_tree_add_text(snmp_tree, asn1->tvb, offset,
896                                     length,
897                                     "Value: %s: %s", vb_type_name,
898                                     vb_display_string);
899                                 g_free(vb_display_string);
900                         } else {
901                                 proto_tree_add_text(snmp_tree, asn1->tvb, offset,
902                                     length,
903                                     "Value: %s: %s", vb_type_name,
904                                     SAFE_STRING(vb_octet_string, vb_length));
905                         }
906 #endif /* HAVE_SOME_SNMP */
907                 }
908                 g_free(vb_octet_string);
909                 break;
910
911         case SNMP_NULL:
912                 ret = asn1_null_decode (asn1, vb_length);
913                 if (ret != ASN1_ERR_NOERROR)
914                         return ret;
915                 length = asn1->offset - start;
916                 if (snmp_tree) {
917                         proto_tree_add_text(snmp_tree, asn1->tvb, offset, length,
918                             "Value: %s", vb_type_name);
919                 }
920                 break;
921
922         case SNMP_OBJECTID:
923                 ret = asn1_oid_value_decode (asn1, vb_length, &vb_oid,
924                     &vb_oid_length);
925                 if (ret != ASN1_ERR_NOERROR)
926                         return ret;
927                 length = asn1->offset - start;
928                 if (snmp_tree) {
929 #ifdef HAVE_SOME_SNMP
930                         variable.val.objid = vb_oid;
931                         vb_display_string = format_var(&variable,
932                             variable_oid, variable_oid_length, vb_type,
933                             vb_oid_length * sizeof (subid_t));
934                         proto_tree_add_text(snmp_tree, asn1->tvb, offset,
935                             length,
936                             "Value: %s", vb_display_string);
937                         g_free(vb_display_string);
938 #else /* HAVE_SOME_SNMP */
939                         vb_display_string = format_oid(vb_oid, vb_oid_length);
940                         proto_tree_add_text(snmp_tree, asn1->tvb, offset,
941                             length,
942                             "Value: %s: %s", vb_type_name, vb_display_string);
943                         g_free(vb_display_string);
944 #endif /* HAVE_SOME_SNMP */
945                 }
946                 g_free(vb_oid);
947                 break;
948
949         case SNMP_NOSUCHOBJECT:
950                 length = asn1->offset - start;
951                 if (snmp_tree) {
952                         proto_tree_add_text(snmp_tree, asn1->tvb, offset, length,
953                             "Value: %s: no such object", vb_type_name);
954                 }
955                 break;
956
957         case SNMP_NOSUCHINSTANCE:
958                 length = asn1->offset - start;
959                 if (snmp_tree) {
960                         proto_tree_add_text(snmp_tree, asn1->tvb, offset, length,
961                             "Value: %s: no such instance", vb_type_name);
962                 }
963                 break;
964
965         case SNMP_ENDOFMIBVIEW:
966                 length = asn1->offset - start;
967                 if (snmp_tree) {
968                         proto_tree_add_text(snmp_tree, asn1->tvb, offset, length,
969                             "Value: %s: end of mib view", vb_type_name);
970                 }
971                 break;
972
973         default:
974                 g_assert_not_reached();
975                 return ASN1_ERR_WRONG_TYPE;
976         }
977         *lengthp = length;
978         return ASN1_ERR_NOERROR;
979 }
980
981 static void
982 dissect_common_pdu(tvbuff_t *tvb, int offset, packet_info *pinfo,
983     proto_tree *tree, ASN1_SCK asn1, guint pdu_type, int start)
984 {
985         gboolean def;
986         guint length;
987         guint sequence_length;
988
989         guint32 request_id;
990
991         guint32 error_status;
992
993         guint32 error_index;
994
995         char *pdu_type_string;
996
997         subid_t *enterprise;
998         guint enterprise_length;
999
1000         guint32 agent_ipaddr;
1001
1002         guint8 *agent_address;
1003         guint agent_address_length;
1004
1005         guint32 trap_type;
1006
1007         guint32 specific_type;
1008
1009         guint timestamp;
1010         guint timestamp_length;
1011
1012         gchar *oid_string;
1013
1014         guint variable_bindings_length;
1015
1016         int vb_index;
1017         guint variable_length;
1018         subid_t *variable_oid;
1019         guint variable_oid_length;
1020
1021         int ret;
1022         guint cls, con, tag;
1023
1024         pdu_type_string = val_to_str(pdu_type, pdu_types,
1025             "Unknown PDU type %#x");
1026         if (check_col(pinfo->cinfo, COL_INFO))
1027                 col_add_str(pinfo->cinfo, COL_INFO, pdu_type_string);
1028         length = asn1.offset - start;
1029         if (tree) {
1030                 proto_tree_add_uint(tree, hf_snmp_pdutype, tvb, offset, length,
1031                     pdu_type);
1032         }
1033         offset += length;
1034
1035         /* get the fields in the PDU preceeding the variable-bindings sequence */
1036         switch (pdu_type) {
1037
1038         case SNMP_MSG_GET:
1039         case SNMP_MSG_GETNEXT:
1040         case SNMP_MSG_RESPONSE:
1041         case SNMP_MSG_SET:
1042         case SNMP_MSG_GETBULK:
1043         case SNMP_MSG_INFORM:
1044         case SNMP_MSG_TRAP2:
1045         case SNMP_MSG_REPORT:
1046                 /* request id */
1047                 ret = asn1_uint32_decode (&asn1, &request_id, &length);
1048                 if (ret != ASN1_ERR_NOERROR) {
1049                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1050                             "request ID", ret);
1051                         return;
1052                 }
1053                 if (tree) {
1054                         proto_tree_add_uint(tree, hf_snmp_request_id,
1055                                 tvb, offset, length, request_id);
1056                 }
1057                 offset += length;
1058
1059                 /* error status, or getbulk non-repeaters */
1060                 ret = asn1_uint32_decode (&asn1, &error_status, &length);
1061                 if (ret != ASN1_ERR_NOERROR) {
1062                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1063                             (pdu_type == SNMP_MSG_GETBULK) ? "non-repeaters"
1064                                                            : "error status",
1065                             ret);
1066                         return;
1067                 }
1068                 if (tree) {
1069                         if (pdu_type == SNMP_MSG_GETBULK) {
1070                                 proto_tree_add_text(tree, tvb, offset,
1071                                     length, "Non-repeaters: %u", error_status);
1072                         } else {
1073                                 proto_tree_add_uint(tree, 
1074                                                     hf_snmp_error_status,
1075                                                     tvb, offset,
1076                                                     length, error_status);
1077                         }
1078                 }
1079                 offset += length;
1080
1081                 /* error index, or getbulk max-repetitions */
1082                 ret = asn1_uint32_decode (&asn1, &error_index, &length);
1083                 if (ret != ASN1_ERR_NOERROR) {
1084                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1085                             (pdu_type == SNMP_MSG_GETBULK) ? "max repetitions"
1086                                                            : "error index",
1087                             ret);
1088                         return;
1089                 }
1090                 if (tree) {
1091                         if (pdu_type == SNMP_MSG_GETBULK) {
1092                                 proto_tree_add_text(tree, tvb, offset,
1093                                     length, "Max repetitions: %u", error_index);
1094                         } else {
1095                                 proto_tree_add_text(tree, tvb, offset,
1096                                     length, "Error Index: %u", error_index);
1097                         }
1098                 }
1099                 offset += length;
1100                 break;
1101
1102         case SNMP_MSG_TRAP:
1103                 /* enterprise */
1104                 ret = asn1_oid_decode (&asn1, &enterprise, &enterprise_length,
1105                     &length);
1106                 if (ret != ASN1_ERR_NOERROR) {
1107                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1108                             "enterprise OID", ret);
1109                         return;
1110                 }
1111                 if (tree) {
1112                         oid_string = format_oid(enterprise, enterprise_length);
1113                         proto_tree_add_string(tree, hf_snmp_enterprise, tvb,
1114                             offset, length, oid_string);
1115                         g_free(oid_string);
1116                 }
1117                 g_free(enterprise);
1118                 offset += length;
1119
1120                 /* agent address */
1121                 start = asn1.offset;
1122                 ret = asn1_header_decode (&asn1, &cls, &con, &tag,
1123                     &def, &agent_address_length);
1124                 if (ret != ASN1_ERR_NOERROR) {
1125                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1126                             "agent address", ret);
1127                         return;
1128                 }
1129                 if (!((cls == ASN1_APL && con == ASN1_PRI && tag == SNMP_IPA) ||
1130                     (cls == ASN1_UNI && con == ASN1_PRI && tag == ASN1_OTS))) {
1131                         /* GXSNMP 0.0.15 says the latter is "needed for
1132                            Banyan" */
1133                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1134                             "agent_address", ASN1_ERR_WRONG_TYPE);
1135                         return;
1136                 }
1137                 if (agent_address_length != 4) {
1138                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1139                             "agent_address", ASN1_ERR_WRONG_LENGTH_FOR_TYPE);
1140                         return;
1141                 }
1142                 ret = asn1_string_value_decode (&asn1,
1143                     agent_address_length, &agent_address);
1144                 if (ret != ASN1_ERR_NOERROR) {
1145                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1146                             "agent address", ret);
1147                         return;
1148                 }
1149                 length = asn1.offset - start;
1150                 if (tree) {
1151                         if (agent_address_length != 4) {
1152                                 proto_tree_add_text(tree, tvb, offset,
1153                                     length,
1154                                     "Agent address: <length is %u, not 4>",
1155                                     agent_address_length);
1156                         } else {
1157                                 memcpy((guint8 *)&agent_ipaddr, agent_address,
1158                                     agent_address_length);
1159                                 proto_tree_add_ipv4(tree, hf_snmp_agent, tvb,
1160                                     offset, length, agent_ipaddr);
1161                         }
1162                 }
1163                 g_free(agent_address);
1164                 offset += length;
1165
1166                 /* generic trap type */
1167                 ret = asn1_uint32_decode (&asn1, &trap_type, &length);
1168                 if (ret != ASN1_ERR_NOERROR) {
1169                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1170                             "generic trap type", ret);
1171                         return;
1172                 }
1173                 if (tree) {
1174                         proto_tree_add_uint(tree, hf_snmp_traptype, tvb,
1175                             offset, length, trap_type);
1176                 }
1177                 offset += length;
1178
1179                 /* specific trap type */
1180                 ret = asn1_uint32_decode (&asn1, &specific_type, &length);
1181                 if (ret != ASN1_ERR_NOERROR) {
1182                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1183                             "specific trap type", ret);
1184                         return;
1185                 }
1186                 if (tree) {
1187                         proto_tree_add_uint(tree, hf_snmp_spectraptype, tvb,
1188                             offset, length, specific_type);
1189                 }
1190                 offset += length;
1191
1192                 /* timestamp */
1193                 start = asn1.offset;
1194                 ret = asn1_header_decode (&asn1, &cls, &con, &tag,
1195                     &def, &timestamp_length);
1196                 if (ret != ASN1_ERR_NOERROR) {
1197                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1198                             "timestamp", ret);
1199                         return;
1200                 }
1201                 if (!((cls == ASN1_APL && con == ASN1_PRI && tag == SNMP_TIT) ||
1202                     (cls == ASN1_UNI && con == ASN1_PRI && tag == ASN1_INT))) {
1203                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1204                             "timestamp", ASN1_ERR_WRONG_TYPE);
1205                         return;
1206                 }
1207                 ret = asn1_uint32_value_decode(&asn1, timestamp_length,
1208                     &timestamp);
1209                 if (ret != ASN1_ERR_NOERROR) {
1210                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1211                             "timestamp", ret);
1212                         return;
1213                 }
1214                 length = asn1.offset - start;
1215                 if (tree) {
1216                         proto_tree_add_uint(tree, hf_snmp_timestamp, tvb,
1217                             offset, length, timestamp);
1218                 }
1219                 offset += length;
1220                 break;
1221         }
1222
1223         /* variable bindings */
1224         /* get header for variable-bindings sequence */
1225         ret = asn1_sequence_decode(&asn1, &variable_bindings_length, &length);
1226         if (ret != ASN1_ERR_NOERROR) {
1227                 dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1228                         "variable bindings header", ret);
1229                 return;
1230         }
1231         offset += length;
1232
1233         /* loop on variable bindings */
1234         vb_index = 0;
1235         while (variable_bindings_length > 0) {
1236                 vb_index++;
1237                 sequence_length = 0;
1238
1239                 /* parse type */
1240                 ret = asn1_sequence_decode(&asn1, &variable_length, &length);
1241                 if (ret != ASN1_ERR_NOERROR) {
1242                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1243                                 "variable binding header", ret);
1244                         return;
1245                 }
1246                 sequence_length += length;
1247
1248                 /* parse object identifier */
1249                 ret = asn1_oid_decode (&asn1, &variable_oid,
1250                     &variable_oid_length, &length);
1251                 if (ret != ASN1_ERR_NOERROR) {
1252                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1253                             "variable binding OID", ret);
1254                         return;
1255                 }
1256                 sequence_length += length;
1257
1258                 if (display_oid || tree) {
1259
1260                   gchar *decoded_oid;
1261                   gchar *non_decoded_oid;
1262
1263                   new_format_oid(variable_oid, variable_oid_length,
1264                                  &non_decoded_oid, &decoded_oid);
1265                   
1266                   if (display_oid && check_col(pinfo->cinfo, COL_INFO)) {
1267                     col_append_fstr(pinfo->cinfo, COL_INFO, 
1268                                     " %s", 
1269                                     (decoded_oid == NULL) ? non_decoded_oid :
1270                                     decoded_oid);
1271                   }
1272                   
1273                   if (tree) {
1274                     if (decoded_oid) {
1275                       proto_tree_add_string_format(tree, hf_snmp_oid,
1276                                                    tvb, offset, 
1277                                                    sequence_length, 
1278                                                    decoded_oid,
1279                                                    "Object identifier %d: %s (%s)", 
1280                                                    vb_index, 
1281                                                    non_decoded_oid,
1282                                                    decoded_oid);
1283                       /* add also the non decoded oid string */
1284                       proto_tree_add_string_hidden(tree, hf_snmp_oid,
1285                                                    tvb, offset, 
1286                                                    sequence_length,
1287                                                    non_decoded_oid);
1288                     } else {
1289                       proto_tree_add_string_format(tree, hf_snmp_oid,
1290                                                    tvb, offset, 
1291                                                    sequence_length, 
1292                                                    non_decoded_oid,
1293                                                    "Object identifier %d: %s", 
1294                                                    vb_index, 
1295                                                    non_decoded_oid);
1296                     }
1297                   }
1298                   
1299                   if (decoded_oid) g_free(decoded_oid);
1300                   g_free(non_decoded_oid);
1301
1302                 }
1303
1304                 offset += sequence_length;
1305                 variable_bindings_length -= sequence_length;
1306
1307                 /* Parse the variable's value */
1308                 ret = snmp_variable_decode(tree, variable_oid,
1309                     variable_oid_length, &asn1, offset, &length);
1310                 if (ret != ASN1_ERR_NOERROR) {
1311                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1312                             "variable", ret);
1313                         return;
1314                 }
1315                 offset += length;
1316                 variable_bindings_length -= length;
1317         }
1318 }
1319
1320 static const value_string qos_vals[] = {
1321         { 0x0,  "No authentication or privacy" },
1322         { 0x1,  "Authentication, no privacy" },
1323         { 0x2,  "Authentication and privacy" },
1324         { 0x3,  "Authentication and privacy" },
1325         { 0,    NULL },
1326 };
1327
1328 static void
1329 dissect_snmp2u_parameters(proto_tree *tree, tvbuff_t *tvb, int offset, int length,
1330     guchar *parameters, int parameters_length)
1331 {
1332         proto_item *item;
1333         proto_tree *parameters_tree;
1334         proto_tree *qos_tree;
1335         guint8 model;
1336         guint8 qos;
1337         guint8 len;
1338
1339         item = proto_tree_add_text(tree, tvb, offset, length,
1340             "Parameters");
1341         parameters_tree = proto_item_add_subtree(item, ett_parameters);
1342         offset += length - parameters_length;
1343
1344         if (parameters_length < 1)
1345                 return;
1346         model = *parameters;
1347         proto_tree_add_text(parameters_tree, tvb, offset, 1,
1348             "model: %u", model);
1349         offset += 1;
1350         parameters += 1;
1351         parameters_length -= 1;
1352         if (model != 1) {
1353                 /* Unknown model. */
1354                 proto_tree_add_text(parameters_tree, tvb, offset,                   parameters_length, "parameters: %s",
1355                     bytes_to_str(parameters, parameters_length));
1356                 return;
1357         }
1358
1359         if (parameters_length < 1)
1360                 return;
1361         qos = *parameters;
1362         item = proto_tree_add_text(parameters_tree, tvb, offset, 1,
1363             "qoS: 0x%x", qos);
1364         qos_tree = proto_item_add_subtree(item, ett_parameters_qos);
1365         proto_tree_add_text(qos_tree, tvb, offset, 1, "%s",
1366             decode_boolean_bitfield(qos, 0x04,
1367                 8, "Generation of report PDU allowed",
1368                    "Generation of report PDU not allowed"));
1369         proto_tree_add_text(qos_tree, tvb, offset, 1, "%s",
1370             decode_enumerated_bitfield(qos, 0x03,
1371                 8, qos_vals, "%s"));
1372         offset += 1;
1373         parameters += 1;
1374         parameters_length -= 1;
1375
1376         if (parameters_length < 12)
1377                 return;
1378         proto_tree_add_text(parameters_tree, tvb, offset, 12,
1379             "agentID: %s", bytes_to_str(parameters, 12));
1380         offset += 12;
1381         parameters += 12;
1382         parameters_length -= 12;
1383
1384         if (parameters_length < 4)
1385                 return;
1386         proto_tree_add_text(parameters_tree, tvb, offset, 4,
1387             "agentBoots: %u", pntohl(parameters));
1388         offset += 4;
1389         parameters += 4;
1390         parameters_length -= 4;
1391
1392         if (parameters_length < 4)
1393                 return;
1394         proto_tree_add_text(parameters_tree, tvb, offset, 4,
1395             "agentTime: %u", pntohl(parameters));
1396         offset += 4;
1397         parameters += 4;
1398         parameters_length -= 4;
1399
1400         if (parameters_length < 2)
1401                 return;
1402         proto_tree_add_text(parameters_tree, tvb, offset, 2,
1403             "maxSize: %u", pntohs(parameters));
1404         offset += 2;
1405         parameters += 2;
1406         parameters_length -= 2;
1407
1408         if (parameters_length < 1)
1409                 return;
1410         len = *parameters;
1411         proto_tree_add_text(parameters_tree, tvb, offset, 1,
1412             "userLen: %u", len);
1413         offset += 1;
1414         parameters += 1;
1415         parameters_length -= 1;
1416
1417         if (parameters_length < len)
1418                 return;
1419         proto_tree_add_text(parameters_tree, tvb, offset, len,
1420             "userName: %.*s", len, parameters);
1421         offset += len;
1422         parameters += len;
1423         parameters_length -= len;
1424
1425         if (parameters_length < 1)
1426                 return;
1427         len = *parameters;
1428         proto_tree_add_text(parameters_tree, tvb, offset, 1,
1429             "authLen: %u", len);
1430         offset += 1;
1431         parameters += 1;
1432         parameters_length -= 1;
1433
1434         if (parameters_length < len)
1435                 return;
1436         proto_tree_add_text(parameters_tree, tvb, offset, len,
1437             "authDigest: %s", bytes_to_str(parameters, len));
1438         offset += len;
1439         parameters += len;
1440         parameters_length -= len;
1441
1442         if (parameters_length < 1)
1443                 return;
1444         proto_tree_add_text(parameters_tree, tvb, offset, parameters_length,
1445             "contextSelector: %s", bytes_to_str(parameters, parameters_length));
1446 }
1447
1448 void
1449 dissect_snmp_pdu(tvbuff_t *tvb, int offset, packet_info *pinfo,
1450     proto_tree *tree, char *proto_name, int proto, gint ett)
1451 {
1452         ASN1_SCK asn1;
1453         int start;
1454         gboolean def;
1455         gboolean encrypted;
1456         guint length;
1457         guint message_length;
1458         guint global_length;
1459
1460         guint32 version;
1461         guint32 msgid;
1462         guint32 msgmax;
1463         guint32 msgsec;
1464         guint32 engineboots;
1465         guint32 enginetime;
1466
1467         guchar *msgflags;
1468         guchar *commustr;
1469         guchar *community;
1470         guchar *secparm;
1471         guchar *cengineid;
1472         guchar *cname;
1473         guchar *cryptpdu;
1474         guchar *aengineid;
1475         guchar *username;
1476         guchar *authpar;
1477         guchar *privpar;
1478         int msgflags_length;
1479         int community_length;
1480         int secparm_length;
1481         int cengineid_length;
1482         int cname_length;
1483         int cryptpdu_length;
1484         int aengineid_length;
1485         int username_length;
1486         int authpar_length;
1487         int privpar_length;
1488
1489         guint pdu_type;
1490         guint pdu_length;
1491
1492         proto_tree *snmp_tree = NULL;
1493         proto_tree *global_tree = NULL;
1494         proto_tree *flags_tree = NULL;
1495         proto_tree *secur_tree = NULL;
1496         proto_item *item = NULL;
1497         int ret;
1498         guint cls, con, tag;
1499
1500         if (check_col(pinfo->cinfo, COL_PROTOCOL))
1501                 col_add_str(pinfo->cinfo, COL_PROTOCOL, proto_name);
1502
1503         if (tree) {
1504                 item = proto_tree_add_item(tree, proto, tvb, offset, -1, FALSE);
1505                 snmp_tree = proto_item_add_subtree(item, ett);
1506         }
1507
1508         /* NOTE: we have to parse the message piece by piece, since the
1509          * capture length may be less than the message length: a 'global'
1510          * parsing is likely to fail.
1511          */
1512         /* parse the SNMP header */
1513         asn1_open(&asn1, tvb, offset);
1514         ret = asn1_sequence_decode(&asn1, &message_length, &length);
1515         if (ret != ASN1_ERR_NOERROR) {
1516                 dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1517                         "message header", ret);
1518                 return;
1519         }
1520         offset += length;
1521
1522         ret = asn1_uint32_decode (&asn1, &version, &length);
1523         if (ret != ASN1_ERR_NOERROR) {
1524                 dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1525                     "version number", ret);
1526                 return;
1527         }
1528         if (snmp_tree) {
1529                 proto_tree_add_uint(snmp_tree, hf_snmp_version, tvb, offset,
1530                     length, version);
1531         }
1532         offset += length;
1533
1534
1535         switch (version) {
1536         case SNMP_VERSION_1:
1537         case SNMP_VERSION_2c:
1538                 ret = asn1_octet_string_decode (&asn1, &community,
1539                     &community_length, &length);
1540                 if (ret != ASN1_ERR_NOERROR) {
1541                         dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1542                             "community", ret);
1543                         return;
1544                 }
1545                 if (tree) {
1546                         commustr = g_malloc(community_length+1);
1547                         memcpy(commustr, community, community_length);
1548                         commustr[community_length] = '\0';
1549
1550                         proto_tree_add_string(snmp_tree, hf_snmp_community,
1551                             tvb, offset, length, commustr);
1552                         g_free(commustr);
1553                 }
1554                 g_free(community);
1555                 offset += length;
1556                 break;
1557         case SNMP_VERSION_2u:
1558                 ret = asn1_octet_string_decode (&asn1, &community,
1559                     &community_length, &length);
1560                 if (tree) {
1561                         dissect_snmp2u_parameters(snmp_tree, tvb, offset, length,
1562                             community, community_length);
1563                 }
1564                 g_free(community);
1565                 offset += length;
1566                 break;
1567         case SNMP_VERSION_3:
1568                 ret = asn1_sequence_decode(&asn1, &global_length, &length);
1569                 if (ret != ASN1_ERR_NOERROR) {
1570                         dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1571                                 "message global header", ret);
1572                         return;
1573                 }
1574                 if (snmp_tree) {
1575                         item = proto_tree_add_text(snmp_tree, tvb, offset,
1576                             global_length + length, "Message Global Header");
1577                         global_tree = proto_item_add_subtree(item, ett_global);
1578                         proto_tree_add_text(global_tree, tvb, offset,
1579                             length,
1580                             "Message Global Header Length: %d", global_length);
1581                 }
1582                 offset += length;
1583                 ret = asn1_uint32_decode (&asn1, &msgid, &length);
1584                 if (ret != ASN1_ERR_NOERROR) {
1585                         dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1586                             "message id", ret);
1587                         return;
1588                 }
1589                 if (global_tree) {
1590                         proto_tree_add_text(global_tree, tvb, offset,
1591                             length, "Message ID: %d", msgid);
1592                 }
1593                 offset += length;
1594                 ret = asn1_uint32_decode (&asn1, &msgmax, &length);
1595                 if (ret != ASN1_ERR_NOERROR) {
1596                         dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1597                             "message max size", ret);
1598                         return;
1599                 }
1600                 if (global_tree) {
1601                         proto_tree_add_text(global_tree, tvb, offset,
1602                             length, "Message Max Size: %d", msgmax);
1603                 }
1604                 offset += length;
1605                 ret = asn1_octet_string_decode (&asn1, &msgflags,
1606                     &msgflags_length, &length);
1607                 if (ret != ASN1_ERR_NOERROR) {
1608                         dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1609                             "message flags", ret);
1610                         return;
1611                 }
1612                 if (msgflags_length != 1) {
1613                         dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1614                             "message flags wrong length", ret);
1615                         g_free(msgflags);
1616                         return;
1617                 }
1618                 if (global_tree) {
1619                         item = proto_tree_add_uint_format(global_tree,
1620                             hf_snmpv3_flags, tvb, offset, length,
1621                             msgflags[0], "Flags: 0x%02x", msgflags[0]);
1622                         flags_tree = proto_item_add_subtree(item, ett_flags);
1623                         proto_tree_add_boolean(flags_tree, hf_snmpv3_flags_report,
1624                             tvb, offset, length, msgflags[0]);
1625                         proto_tree_add_boolean(flags_tree, hf_snmpv3_flags_crypt,
1626                             tvb, offset, length, msgflags[0]);
1627                         proto_tree_add_boolean(flags_tree, hf_snmpv3_flags_auth,
1628                             tvb, offset, length, msgflags[0]);
1629                 }
1630                 encrypted = msgflags[0] & TH_CRYPT;
1631                 g_free(msgflags);
1632                 offset += length;
1633                 ret = asn1_uint32_decode (&asn1, &msgsec, &length);
1634                 if (ret != ASN1_ERR_NOERROR) {
1635                         dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1636                             "message security model", ret);
1637                         return;
1638                 }
1639                 if (global_tree) {
1640                         proto_tree_add_text(global_tree, tvb, offset,
1641                             length, "Message Security Model: %s",
1642                             val_to_str(msgsec, sec_models,
1643                             "Unknown model %#x"));
1644                 }
1645                 offset += length;
1646                 switch(msgsec) {
1647                 case SNMP_SEC_USM:
1648                         start = asn1.offset;
1649                         ret = asn1_header_decode (&asn1, &cls, &con, &tag,
1650                             &def, &secparm_length);
1651                         length = asn1.offset - start;
1652                         if (cls != ASN1_UNI && con != ASN1_PRI &&
1653                             tag != ASN1_OTS) {
1654                                 dissect_snmp_parse_error(tvb, offset, pinfo,
1655                                     snmp_tree, "Message Security Parameters",
1656                                     ASN1_ERR_WRONG_TYPE);
1657                                 return;
1658                         }
1659                         if (snmp_tree) {
1660                                 item = proto_tree_add_text(snmp_tree, tvb,
1661                                     offset, secparm_length + length,
1662                                     "Message Security Parameters");
1663                                 secur_tree = proto_item_add_subtree(item,
1664                                     ett_secur);
1665                                 proto_tree_add_text(secur_tree, tvb, offset,
1666                                     length,
1667                                     "Message Security Parameters Length: %d",
1668                                     secparm_length);
1669                         }
1670                         offset += length;
1671                         ret = asn1_sequence_decode(&asn1, &secparm_length,
1672                             &length);
1673                         if (ret != ASN1_ERR_NOERROR) {
1674                                 dissect_snmp_parse_error(tvb, offset, pinfo,
1675                                     snmp_tree, "USM sequence header", ret);
1676                                 return;
1677                         }
1678                         offset += length;
1679                         ret = asn1_octet_string_decode (&asn1, &aengineid,
1680                             &aengineid_length, &length);
1681                         if (ret != ASN1_ERR_NOERROR) {
1682                                 dissect_snmp_parse_error(tvb, offset, pinfo,
1683                                     snmp_tree, "authoritative engine id", ret);
1684                                 return;
1685                         }
1686                         if (secur_tree) {
1687                                 proto_tree_add_text(secur_tree, tvb, offset,
1688                                     length, "Authoritative Engine ID: %s",
1689                                     bytes_to_str(aengineid, aengineid_length));
1690                         }
1691                         g_free(aengineid);
1692                         offset += length;
1693                         ret = asn1_uint32_decode (&asn1, &engineboots, &length);
1694                         if (ret != ASN1_ERR_NOERROR) {
1695                                 dissect_snmp_parse_error(tvb, offset, pinfo,
1696                                     snmp_tree, "engine boots", ret);
1697                                 return;
1698                         }
1699                         if (secur_tree) {
1700                                 proto_tree_add_text(secur_tree, tvb,
1701                                     offset, length, "Engine Boots: %d",
1702                                     engineboots);
1703                         }
1704                         offset += length;
1705                         ret = asn1_uint32_decode (&asn1, &enginetime, &length);
1706                         if (ret != ASN1_ERR_NOERROR) {
1707                                 dissect_snmp_parse_error(tvb, offset, pinfo,
1708                                     snmp_tree,  "engine time", ret);
1709                                 return;
1710                         }
1711                         if (secur_tree) {
1712                                 proto_tree_add_text(secur_tree, tvb,
1713                                     offset, length, "Engine Time: %d",
1714                                     enginetime);
1715                         }
1716                         offset += length;
1717                         ret = asn1_octet_string_decode (&asn1, &username,
1718                             &username_length, &length);
1719                         if (ret != ASN1_ERR_NOERROR) {
1720                                 dissect_snmp_parse_error(tvb, offset, pinfo,
1721                                     snmp_tree, "user name", ret);
1722                                 return;
1723                         }
1724                         if (secur_tree) {
1725                                 proto_tree_add_text(secur_tree, tvb, offset,
1726                                     length, "User Name: %s",
1727                                     SAFE_STRING(username, username_length));
1728                         }
1729                         g_free(username);
1730                         offset += length;
1731                         ret = asn1_octet_string_decode (&asn1, &authpar,
1732                             &authpar_length, &length);
1733                         if (ret != ASN1_ERR_NOERROR) {
1734                                 dissect_snmp_parse_error(tvb, offset, pinfo,
1735                                     snmp_tree, "authentication parameter", ret);
1736                                 return;
1737                         }
1738                         if (secur_tree) {
1739                                 proto_tree_add_text(secur_tree, tvb, offset,
1740                                     length, "Authentication Parameter: %s",
1741                                     bytes_to_str(authpar, authpar_length));
1742                         }
1743                         g_free(authpar);
1744                         offset += length;
1745                         ret = asn1_octet_string_decode (&asn1, &privpar,
1746                             &privpar_length, &length);
1747                         if (ret != ASN1_ERR_NOERROR) {
1748                                 dissect_snmp_parse_error(tvb, offset, pinfo,
1749                                     snmp_tree, "privacy parameter", ret);
1750                                 return;
1751                         }
1752                         if (secur_tree) {
1753                                 proto_tree_add_text(secur_tree, tvb, offset,
1754                                     length, "Privacy Parameter: %s",
1755                                     bytes_to_str(privpar, privpar_length));
1756                         }
1757                         g_free(privpar);
1758                         offset += length;
1759                         break;
1760                 default:
1761                         ret = asn1_octet_string_decode (&asn1,
1762                             &secparm, &secparm_length, &length);
1763                         if (ret != ASN1_ERR_NOERROR) {
1764                                 dissect_snmp_parse_error(tvb, offset, pinfo,
1765                                     snmp_tree, "Message Security Parameters",
1766                                     ret);
1767                                 return;
1768                         }
1769                         if (snmp_tree) {
1770                                 proto_tree_add_text(snmp_tree, tvb, offset,
1771                                     length,
1772                                     "Message Security Parameters Data"
1773                                     " (%d bytes)", secparm_length);
1774                         }
1775                         g_free(secparm);
1776                         offset += length;
1777                         break;
1778                 }
1779                 /* PDU starts here */
1780                 if (encrypted) {
1781                         ret = asn1_octet_string_decode (&asn1, &cryptpdu,
1782                             &cryptpdu_length, &length);
1783                         if (ret != ASN1_ERR_NOERROR) {
1784                                 dissect_snmp_parse_error(tvb, offset, pinfo,
1785                                     snmp_tree, "encrypted PDU header", ret);
1786                                 return;
1787                         }
1788                         proto_tree_add_text(snmp_tree, tvb, offset, length,
1789                             "Encrypted PDU (%d bytes)", length);
1790                         g_free(cryptpdu);
1791                         if (check_col(pinfo->cinfo, COL_INFO))
1792                                 col_set_str(pinfo->cinfo, COL_INFO, "Encrypted PDU");
1793                         return;
1794                 }
1795                 ret = asn1_sequence_decode(&asn1, &global_length, &length);
1796                 if (ret != ASN1_ERR_NOERROR) {
1797                         dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1798                                 "PDU header", ret);
1799                         return;
1800                 }
1801                 offset += length;
1802                 ret = asn1_octet_string_decode (&asn1, &cengineid,
1803                     &cengineid_length, &length);
1804                 if (ret != ASN1_ERR_NOERROR) {
1805                         dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1806                             "context engine id", ret);
1807                         return;
1808                 }
1809                 if (snmp_tree) {
1810                         proto_tree_add_text(snmp_tree, tvb, offset, length,
1811                             "Context Engine ID: %s",
1812                             bytes_to_str(cengineid, cengineid_length));
1813                 }
1814                 g_free(cengineid);
1815                 offset += length;
1816                 ret = asn1_octet_string_decode (&asn1, &cname,
1817                     &cname_length, &length);
1818                 if (ret != ASN1_ERR_NOERROR) {
1819                         dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1820                             "context name", ret);
1821                         return;
1822                 }
1823                 if (snmp_tree) {
1824                         proto_tree_add_text(snmp_tree, tvb, offset, length,
1825                             "Context Name: %s",
1826                             SAFE_STRING(cname, cname_length));
1827                 }
1828                 g_free(cname);
1829                 offset += length;
1830                 break;
1831         default:
1832                 dissect_snmp_error(tvb, offset, pinfo, snmp_tree,
1833                     "PDU for unknown version of SNMP");
1834                 return;
1835         }
1836
1837         start = asn1.offset;
1838         ret = asn1_header_decode (&asn1, &cls, &con, &pdu_type, &def,
1839             &pdu_length);
1840         if (ret != ASN1_ERR_NOERROR) {
1841                 dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1842                     "PDU type", ret);
1843                 return;
1844         }
1845         if (cls != ASN1_CTX || con != ASN1_CON) {
1846                 dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1847                     "PDU type", ASN1_ERR_WRONG_TYPE);
1848                 return;
1849         }
1850         dissect_common_pdu(tvb, offset, pinfo, snmp_tree, asn1, pdu_type, start);
1851 }
1852
1853 static void
1854 dissect_smux_pdu(tvbuff_t *tvb, int offset, packet_info *pinfo,
1855     proto_tree *tree, int proto, gint ett)
1856 {
1857         ASN1_SCK asn1;
1858         int start;
1859         gboolean def;
1860         guint length;
1861
1862         guint pdu_type;
1863         char *pdu_type_string;
1864         guint pdu_length;
1865
1866         guint32 version;
1867         guint32 cause;
1868         guint32 priority;
1869         guint32 operation;
1870         guint32 commit;
1871
1872         guchar *password;
1873         int password_length;
1874
1875         guchar *application;
1876         int application_length;
1877
1878         subid_t *regid;
1879         guint regid_length;
1880
1881         gchar *oid_string;
1882
1883         proto_tree *smux_tree = NULL;
1884         proto_item *item = NULL;
1885         int ret;
1886         guint cls, con;
1887
1888         if (check_col(pinfo->cinfo, COL_PROTOCOL))
1889                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "SMUX");
1890
1891         if (tree) {
1892                 item = proto_tree_add_item(tree, proto, tvb, offset, -1, FALSE);
1893                 smux_tree = proto_item_add_subtree(item, ett);
1894         }
1895
1896         /* NOTE: we have to parse the message piece by piece, since the
1897          * capture length may be less than the message length: a 'global'
1898          * parsing is likely to fail.
1899          */
1900         /* parse the SNMP header */
1901         asn1_open(&asn1, tvb, offset);
1902         start = asn1.offset;
1903         ret = asn1_header_decode (&asn1, &cls, &con, &pdu_type, &def,
1904             &pdu_length);
1905         if (ret != ASN1_ERR_NOERROR) {
1906                 dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1907                     "PDU type", ret);
1908                 return;
1909         }
1910
1911         /* Dissect SMUX here */
1912         if (cls == ASN1_APL && con == ASN1_CON && pdu_type == SMUX_MSG_OPEN) {
1913                 pdu_type_string = val_to_str(pdu_type, smux_types,
1914                     "Unknown PDU type %#x");
1915                 if (check_col(pinfo->cinfo, COL_INFO))
1916                         col_add_str(pinfo->cinfo, COL_INFO, pdu_type_string);
1917                 length = asn1.offset - start;
1918                 if (tree) {
1919                         proto_tree_add_uint(smux_tree, hf_smux_pdutype, tvb,
1920                             offset, length, pdu_type);
1921                 }
1922                 offset += length;
1923                 ret = asn1_uint32_decode (&asn1, &version, &length);
1924                 if (ret != ASN1_ERR_NOERROR) {
1925                         dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1926                             "version", ret);
1927                         return;
1928                 }
1929                 if (tree) {
1930                         proto_tree_add_uint(smux_tree, hf_smux_version, tvb,
1931                             offset, length, version);
1932                 }
1933                 offset += length;
1934
1935                 ret = asn1_oid_decode (&asn1, &regid, &regid_length, &length);
1936                 if (ret != ASN1_ERR_NOERROR) {
1937                         dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1938                             "registration OID", ret);
1939                         return;
1940                 }
1941                 if (tree) {
1942                         oid_string = format_oid(regid, regid_length);
1943                         proto_tree_add_text(smux_tree, tvb, offset, length,
1944                             "Registration: %s", oid_string);
1945                         g_free(oid_string);
1946                 }
1947                 g_free(regid);
1948                 offset += length;
1949
1950                 ret = asn1_octet_string_decode (&asn1, &application,
1951                     &application_length, &length);
1952                 if (ret != ASN1_ERR_NOERROR) {
1953                         dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1954                             "application", ret);
1955                         return;
1956                 }
1957                 if (tree) {
1958                         proto_tree_add_text(smux_tree, tvb, offset, length,
1959                             "Application: %s",
1960                              SAFE_STRING(application, application_length));
1961                 }
1962                 g_free(application);
1963                 offset += length;
1964
1965                 ret = asn1_octet_string_decode (&asn1, &password,
1966                     &password_length, &length);
1967                 if (ret != ASN1_ERR_NOERROR) {
1968                         dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1969                             "password", ret);
1970                         return;
1971                 }
1972                 if (tree) {
1973                         proto_tree_add_text(smux_tree, tvb, offset, length,
1974                             "Password: %s",
1975                             SAFE_STRING(password, password_length));
1976                 }
1977                 g_free(password);
1978                 offset += length;
1979                 return;
1980         }
1981         if (cls == ASN1_APL && con == ASN1_PRI && pdu_type == SMUX_MSG_CLOSE) {
1982                 pdu_type_string = val_to_str(pdu_type, smux_types,
1983                     "Unknown PDU type %#x");
1984                 if (check_col(pinfo->cinfo, COL_INFO))
1985                         col_add_str(pinfo->cinfo, COL_INFO, pdu_type_string);
1986                 length = asn1.offset - start;
1987                 if (tree) {
1988                         proto_tree_add_uint(smux_tree, hf_smux_pdutype, tvb,
1989                             offset, length, pdu_type);
1990                 }
1991                 offset += length;
1992                 ret = asn1_uint32_value_decode (&asn1, pdu_length, &cause);
1993                 if (ret != ASN1_ERR_NOERROR) {
1994                         dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1995                             "cause", ret);
1996                         return;
1997                 }
1998                 if (tree) {
1999                         proto_tree_add_text(smux_tree, tvb, offset,
2000                             pdu_length, "Cause: %s",
2001                             val_to_str(cause, smux_close,
2002                                 "Unknown cause %#x"));
2003                 }
2004                 offset += pdu_length;
2005                 return;
2006         }
2007         if (cls == ASN1_APL && con == ASN1_CON && pdu_type == SMUX_MSG_RREQ) {
2008                 pdu_type_string = val_to_str(pdu_type, smux_types,
2009                     "Unknown PDU type %#x");
2010                 if (check_col(pinfo->cinfo, COL_INFO))
2011                         col_add_str(pinfo->cinfo, COL_INFO, pdu_type_string);
2012                 length = asn1.offset - start;
2013                 if (tree) {
2014                         proto_tree_add_uint(smux_tree, hf_smux_pdutype, tvb,
2015                             offset, length, pdu_type);
2016                 }
2017                 offset += length;
2018                 ret = asn1_oid_decode (&asn1, &regid, &regid_length, &length);
2019                 if (ret != ASN1_ERR_NOERROR) {
2020                         dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
2021                             "registration subtree", ret);
2022                         return;
2023                 }
2024                 if (tree) {
2025                         oid_string = format_oid(regid, regid_length);
2026                         proto_tree_add_text(smux_tree, tvb, offset, length,
2027                             "Registration: %s", oid_string);
2028                         g_free(oid_string);
2029                 }
2030                 g_free(regid);
2031                 offset += length;
2032
2033                 ret = asn1_uint32_decode (&asn1, &priority, &length);
2034                 if (ret != ASN1_ERR_NOERROR) {
2035                         dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
2036                             "priority", ret);
2037                         return;
2038                 }
2039                 if (tree) {
2040                         proto_tree_add_text(smux_tree, tvb, offset, length,
2041                             "Priority: %d", priority);
2042                 }
2043                 offset += length;
2044
2045                 ret = asn1_uint32_decode (&asn1, &operation, &length);
2046                 if (ret != ASN1_ERR_NOERROR) {
2047                         dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
2048                             "operation", ret);
2049                         return;
2050                 }
2051                 if (tree) {
2052                         proto_tree_add_text(smux_tree, tvb, offset, length,
2053                             "Operation: %s",
2054                             val_to_str(operation, smux_rreq,
2055                                 "Unknown operation %#x"));
2056                 }
2057                 offset += length;
2058                 return;
2059         }
2060         if (cls == ASN1_APL && con == ASN1_PRI && pdu_type == SMUX_MSG_RRSP) {
2061                 pdu_type_string = val_to_str(pdu_type, smux_types,
2062                     "Unknown PDU type %#x");
2063                 if (check_col(pinfo->cinfo, COL_INFO))
2064                         col_add_str(pinfo->cinfo, COL_INFO, pdu_type_string);
2065                 length = asn1.offset - start;
2066                 if (tree) {
2067                         proto_tree_add_uint(smux_tree, hf_smux_pdutype, tvb,
2068                             offset, length, pdu_type);
2069                 }
2070                 offset += length;
2071                 ret = asn1_uint32_value_decode (&asn1, pdu_length, &priority);
2072                 if (ret != ASN1_ERR_NOERROR) {
2073                         dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
2074                             "priority", ret);
2075                         return;
2076                 }
2077                 if (tree) {
2078                         proto_tree_add_text(smux_tree, tvb, offset,
2079                             pdu_length, "%s",
2080                             val_to_str(priority, smux_prio,
2081                                 "Priority: %#x"));
2082                 }
2083                 offset += pdu_length;
2084                 return;
2085         }
2086         if (cls == ASN1_APL && con == ASN1_PRI && pdu_type == SMUX_MSG_SOUT) {
2087                 pdu_type_string = val_to_str(pdu_type, smux_types,
2088                     "Unknown PDU type %#x");
2089                 if (check_col(pinfo->cinfo, COL_INFO))
2090                         col_add_str(pinfo->cinfo, COL_INFO, pdu_type_string);
2091                 length = asn1.offset - start;
2092                 if (tree) {
2093                         proto_tree_add_uint(smux_tree, hf_smux_pdutype, tvb,
2094                             offset, length, pdu_type);
2095                 }
2096                 offset += length;
2097                 ret = asn1_uint32_value_decode (&asn1, pdu_length, &commit);
2098                 if (ret != ASN1_ERR_NOERROR) {
2099                         dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
2100                             "commit", ret);
2101                         return;
2102                 }
2103                 if (tree) {
2104                         proto_tree_add_text(smux_tree, tvb, offset,
2105                             pdu_length, "%s",
2106                             val_to_str(commit, smux_sout,
2107                                 "Unknown SOUT Value: %#x"));
2108                 }
2109                 offset += pdu_length;
2110                 return;
2111         }
2112         if (cls != ASN1_CTX || con != ASN1_CON) {
2113                 dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
2114                     "PDU type", ASN1_ERR_WRONG_TYPE);
2115                 return;
2116         }
2117         dissect_common_pdu(tvb, offset, pinfo, smux_tree, asn1, pdu_type, start);
2118 }
2119
2120 static void
2121 dissect_snmp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2122 {
2123         conversation_t  *conversation;
2124
2125         /*
2126          * The first SNMP packet goes to the SNMP port; the second one
2127          * may come from some *other* port, but goes back to the same
2128          * IP address and port as the ones from which the first packet
2129          * came; all subsequent packets presumably go between those two
2130          * IP addresses and ports.
2131          *
2132          * If this packet went to the SNMP port, we check to see if
2133          * there's already a conversation with one address/port pair
2134          * matching the source IP address and port of this packet,
2135          * the other address matching the destination IP address of this
2136          * packet, and any destination port.
2137          *
2138          * If not, we create one, with its address 1/port 1 pair being
2139          * the source address/port of this packet, its address 2 being
2140          * the destination address of this packet, and its port 2 being
2141          * wildcarded, and give it the SNMP dissector as a dissector.
2142          */
2143         if (pinfo->destport == UDP_PORT_SNMP) {
2144           conversation = find_conversation(&pinfo->src, &pinfo->dst, PT_UDP,
2145                                            pinfo->srcport, 0, NO_PORT_B);
2146           if (conversation == NULL) {
2147             conversation = conversation_new(&pinfo->src, &pinfo->dst, PT_UDP,
2148                                             pinfo->srcport, 0, NO_PORT2);
2149             conversation_set_dissector(conversation, snmp_handle);
2150           }
2151         }
2152
2153         dissect_snmp_pdu(tvb, 0, pinfo, tree, "SNMP", proto_snmp, ett_snmp);
2154 }
2155
2156 static void
2157 dissect_smux(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2158 {
2159         dissect_smux_pdu(tvb, 0, pinfo, tree, proto_smux, ett_smux);
2160 }
2161
2162 void
2163 proto_register_snmp(void)
2164 {
2165 #if defined(WIN32) && defined(HAVE_SOME_SNMP)
2166         char *mib_path;
2167 #define MIB_PATH_APPEND "snmp\\mibs"
2168 #endif
2169
2170         static hf_register_info hf[] = {
2171                 { &hf_snmp_version,
2172                 { "Version", "snmp.version", FT_UINT8, BASE_DEC, VALS(versions),
2173                     0x0, "", HFILL }},
2174                 { &hf_snmp_community,
2175                 { "Community", "snmp.community", FT_STRING, BASE_NONE, NULL,
2176                     0x0, "", HFILL }},
2177                 { &hf_snmp_request_id,
2178                 { "Request Id", "snmp.id", FT_UINT32, BASE_HEX, NULL,
2179                     0x0, "Id for this transaction", HFILL }},
2180                 { &hf_snmp_pdutype,
2181                 { "PDU type", "snmp.pdutype", FT_UINT8, BASE_DEC, VALS(pdu_types),
2182                     0x0, "", HFILL }},
2183                 { &hf_snmp_agent,
2184                 { "Agent address", "snmp.agent", FT_IPv4, BASE_NONE, NULL,
2185                     0x0, "", HFILL }},
2186                 { &hf_snmp_enterprise,
2187                 { "Enterprise", "snmp.enterprise", FT_STRING, BASE_NONE, NULL,
2188                     0x0, "", HFILL }},
2189                 { &hf_snmp_error_status,
2190                 { "Error Status", "snmp.error", FT_UINT8, BASE_DEC, VALS(error_statuses),
2191                     0x0, "", HFILL }},
2192                 { &hf_snmp_oid,
2193                 { "Object identifier", "snmp.oid", FT_STRING, BASE_NONE, NULL,
2194                     0x0, "", HFILL }},
2195                 { &hf_snmp_traptype,
2196                 { "Trap type", "snmp.traptype", FT_UINT8, BASE_DEC, VALS(trap_types),
2197                     0x0, "", HFILL }},
2198                 { &hf_snmp_spectraptype,
2199                 { "Specific trap type", "snmp.spectraptype", FT_UINT32, BASE_DEC, NULL,
2200                     0x0, "", HFILL }},
2201                 { &hf_snmp_timestamp,
2202                 { "Timestamp", "snmp.timestamp", FT_UINT8, BASE_DEC, NULL,
2203                     0x0, "", HFILL }},
2204                 { &hf_snmpv3_flags,
2205                 { "SNMPv3 Flags", "snmpv3.flags", FT_UINT8, BASE_HEX, NULL,
2206                     0x0, "", HFILL }},
2207                 { &hf_snmpv3_flags_auth,
2208                 { "Authenticated", "snmpv3.flags.auth", FT_BOOLEAN, 8,
2209                     TFS(&flags_set_truth), TH_AUTH, "", HFILL }},
2210                 { &hf_snmpv3_flags_crypt,
2211                 { "Encrypted", "snmpv3.flags.crypt", FT_BOOLEAN, 8,
2212                     TFS(&flags_set_truth), TH_CRYPT, "", HFILL }},
2213                 { &hf_snmpv3_flags_report,
2214                 { "Reportable", "snmpv3.flags.report", FT_BOOLEAN, 8,
2215                     TFS(&flags_set_truth), TH_REPORT, "", HFILL }},
2216         };
2217         static gint *ett[] = {
2218                 &ett_snmp,
2219                 &ett_parameters,
2220                 &ett_parameters_qos,
2221                 &ett_global,
2222                 &ett_flags,
2223                 &ett_secur,
2224         };
2225         module_t *snmp_module;
2226
2227 #ifdef HAVE_SOME_SNMP
2228
2229 #ifdef WIN32
2230         /* Set MIBDIRS so that the SNMP library can find its mibs. */
2231         /* XXX - Should we set MIBS or MIBFILES as well? */
2232
2233         mib_path = g_malloc (strlen(get_datafile_dir()) + strlen(MIB_PATH_APPEND) + 20);
2234         sprintf (mib_path, "MIBDIRS=%s\\%s", get_datafile_dir(), MIB_PATH_APPEND);
2235         /* Amazingly enough, Windows does not provide setenv(). */
2236         if (getenv("MIBDIRS") == NULL)
2237                 _putenv(mib_path);
2238         g_free(mib_path);
2239
2240 #endif  /* WIN32 */
2241
2242         /*
2243          * Suppress warnings about unknown tokens - we aren't initializing
2244          * UCD SNMP in its entirety, we're just initializing the
2245          * MIB-handling part because that's all we're using, which
2246          * means that entries in the configuration file for other
2247          * pars of the library will not be handled, and we don't want
2248          * the config file reading code to whine about that.
2249          */
2250         netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
2251                                NETSNMP_DS_LIB_NO_TOKEN_WARNINGS, TRUE);
2252         netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
2253                            NETSNMP_DS_LIB_PRINT_SUFFIX_ONLY, 2);
2254         register_mib_handlers();
2255         read_premib_configs();
2256         init_mib();
2257         read_configs();
2258 #endif /* HAVE_SOME_SNMP */
2259         proto_snmp = proto_register_protocol("Simple Network Management Protocol",
2260             "SNMP", "snmp");
2261         proto_register_field_array(proto_snmp, hf, array_length(hf));
2262         proto_register_subtree_array(ett, array_length(ett));
2263         snmp_handle = create_dissector_handle(dissect_snmp, proto_snmp);
2264
2265         /* Register configuration preferences */
2266         snmp_module = prefs_register_protocol(proto_snmp, NULL);
2267         prefs_register_bool_preference(snmp_module, "display_oid",
2268                 "Show SNMP OID in info column",
2269                 "Whether the SNMP OID should be shown in the info column",
2270                 &display_oid);
2271 }
2272
2273 void
2274 proto_reg_handoff_snmp(void)
2275 {
2276         dissector_add("udp.port", UDP_PORT_SNMP, snmp_handle);
2277         dissector_add("udp.port", UDP_PORT_SNMP_TRAP, snmp_handle);
2278         dissector_add("ethertype", ETHERTYPE_SNMP, snmp_handle);
2279         dissector_add("ipx.socket", IPX_SOCKET_SNMP_AGENT, snmp_handle);
2280         dissector_add("ipx.socket", IPX_SOCKET_SNMP_SINK, snmp_handle);
2281         dissector_add("hpext.dxsap", HPEXT_SNMP, snmp_handle);
2282         data_handle = find_dissector("data");
2283 }
2284
2285 void
2286 proto_register_smux(void)
2287 {
2288         static hf_register_info hf[] = {
2289                 { &hf_smux_version,
2290                 { "Version", "smux.version", FT_UINT8, BASE_DEC, NULL,
2291                     0x0, "", HFILL }},
2292                 { &hf_smux_pdutype,
2293                 { "PDU type", "smux.pdutype", FT_UINT8, BASE_DEC, VALS(smux_types),
2294                     0x0, "", HFILL }},
2295         };
2296         static gint *ett[] = {
2297                 &ett_smux,
2298         };
2299
2300         proto_smux = proto_register_protocol("SNMP Multiplex Protocol",
2301             "SMUX", "smux");
2302         proto_register_field_array(proto_smux, hf, array_length(hf));
2303         proto_register_subtree_array(ett, array_length(ett));
2304 }
2305
2306 void
2307 proto_reg_handoff_smux(void)
2308 {
2309         dissector_handle_t smux_handle;
2310
2311         smux_handle = create_dissector_handle(dissect_smux, proto_smux);
2312         dissector_add("tcp.port", TCP_PORT_SMUX, smux_handle);
2313 }