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