From Kikuchi Ayamura: include <ucd-snmp/ucd-snmp-config.h> to fix IRIX
[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.
10  *
11  * $Id: packet-snmp.c,v 1.91 2002/04/01 21:59:59 guy Exp $
12  *
13  * Ethereal - Network traffic analyzer
14  * By Gerald Combs <gerald@ethereal.com>
15  * Copyright 1998 Gerald Combs
16  *
17  * Some stuff from:
18  * 
19  * GXSNMP -- An snmp mangament application
20  * Copyright (C) 1998 Gregory McLean & Jochen Friedrich
21  * Beholder RMON ethernet network monitor,Copyright (C) 1993 DNPAP group
22  *
23  * This program is free software; you can redistribute it and/or
24  * modify it under the terms of the GNU General Public License
25  * as published by the Free Software Foundation; either version 2
26  * of the License, or (at your option) any later version.
27  * 
28  * This program is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31  * GNU General Public License for more details.
32  * 
33  * You should have received a copy of the GNU General Public License
34  * along with this program; if not, write to the Free Software
35  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
36  */
37
38 #ifdef HAVE_CONFIG_H
39 # include "config.h"
40 #endif
41
42 #include <stdio.h>
43 #include <string.h>
44 #include <ctype.h>
45
46 #ifdef HAVE_SYS_TYPES_H
47 # include <sys/types.h>
48 #endif
49
50 #ifdef HAVE_NETINET_IN_H
51 # include <netinet/in.h>
52 #endif
53
54 #include <glib.h>
55
56 #include <epan/packet.h>
57 #include <epan/strutil.h>
58 #include <epan/conversation.h>
59 #include "etypes.h"
60 #include "packet-ipx.h"
61
62 #ifdef HAVE_UCD_SNMP
63 # include <ucd-snmp/ucd-snmp-config.h>
64 # include <ucd-snmp/asn1.h>
65 # include <ucd-snmp/snmp_api.h>
66 # include <ucd-snmp/snmp_impl.h>
67 # include <ucd-snmp/mib.h>
68 # include <ucd-snmp/default_store.h>
69 # include <ucd-snmp/read_config.h>
70 # include <ucd-snmp/tools.h>
71
72    /*
73     * Define values "sprint_realloc_value()" expects.
74     */
75 # define VALTYPE_INTEGER        ASN_INTEGER
76 # define VALTYPE_COUNTER        ASN_COUNTER
77 # define VALTYPE_GAUGE          ASN_GAUGE
78 # define VALTYPE_TIMETICKS      ASN_TIMETICKS
79 # define VALTYPE_STRING         ASN_OCTET_STR
80 # define VALTYPE_IPADDR         ASN_IPADDRESS
81 # define VALTYPE_OPAQUE         ASN_OPAQUE
82 # define VALTYPE_NSAP           ASN_NSAP
83 # define VALTYPE_OBJECTID       ASN_OBJECT_ID
84 # define VALTYPE_BITSTR         ASN_BIT_STR
85 # define VALTYPE_COUNTER64      ASN_COUNTER64
86
87 #endif
88
89 #include "asn1.h"
90
91 #include "packet-snmp.h"
92 #include "format-oid.h"
93
94 /* Null string of type "guchar[]". */
95 static const guchar nullstring[] = "";
96
97 /* Take a pointer that may be null and return a pointer that's not null
98    by turning null pointers into pointers to the above null string. */
99 #define SAFE_STRING(s)  (((s) != NULL) ? (s) : nullstring)
100
101 static int proto_snmp = -1;
102 static int proto_smux = -1;
103
104 static gint ett_snmp = -1;
105 static gint ett_smux = -1;
106 static gint ett_parameters = -1;
107 static gint ett_parameters_qos = -1;
108 static gint ett_global = -1;
109 static gint ett_flags = -1;
110 static gint ett_secur = -1;
111
112 static int hf_snmpv3_flags = -1;
113 static int hf_snmpv3_flags_auth = -1;
114 static int hf_snmpv3_flags_crypt = -1;
115 static int hf_snmpv3_flags_report = -1;
116
117 static dissector_handle_t snmp_handle;
118 static dissector_handle_t data_handle;
119
120 #define TH_AUTH   0x01
121 #define TH_CRYPT  0x02
122 #define TH_REPORT 0x04
123
124 static const true_false_string flags_set_truth = {
125   "Set",
126   "Not set"
127 };
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,tvb_new_subset(tvb, offset,-1,tvb_reported_length_remaining(tvb,offset)), pinfo, tree);
445         }
446 }
447
448 gchar *
449 format_oid(subid_t *oid, guint oid_length)
450 {
451         char *result;
452         int result_len;
453         int len;
454         unsigned int i;
455         char *buf;
456 #ifdef HAVE_UCD_SNMP
457         u_char *oid_string;
458         size_t oid_string_len;
459         size_t oid_out_len;
460 #endif
461
462         result_len = oid_length * 22;
463
464 #ifdef HAVE_UCD_SNMP
465         /*
466          * Get the decoded form of the OID, and add its length to the
467          * length of the result string.
468          *
469          * XXX - check for "malloc" and "sprint_realloc_objid()" failure.
470          */
471         oid_string_len = 256;
472         oid_string = malloc(oid_string_len);
473         *oid_string = '\0';
474         oid_out_len = 0;
475         sprint_realloc_objid(&oid_string, &oid_string_len, &oid_out_len, 1,
476             oid, oid_length);
477         result_len += strlen(oid_string) + 3;
478 #endif
479
480         result = g_malloc(result_len + 1);
481         buf = result;
482         len = sprintf(buf, "%lu", (unsigned long)oid[0]);
483         buf += len;
484         for (i = 1; i < oid_length;i++) {
485                 len = sprintf(buf, ".%lu", (unsigned long)oid[i]);
486                 buf += len;
487         }
488
489 #ifdef HAVE_UCD_SNMP
490         /*
491          * Append the decoded form of the OID.
492          */
493         sprintf(buf, " (%s)", oid_string);
494         free(oid_string);
495 #endif
496
497         return result;
498 }
499
500 #ifdef HAVE_UCD_SNMP
501 static u_char *
502 check_var_length(guint vb_length, guint required_length)
503 {
504         u_char *buf;
505         static const char badlen_fmt[] = "Length is %u, should be %u";
506
507         if (vb_length != required_length) {
508                 /* Enough room for the largest "Length is XXX,
509                    should be XXX" message - 10 digits for each
510                    XXX. */
511                 buf = malloc(sizeof badlen_fmt + 10 + 10);
512                 sprintf(buf, badlen_fmt, vb_length, required_length);
513                 return buf;
514         }
515         return NULL;    /* length is OK */
516 }
517
518 static u_char *
519 format_var(struct variable_list *variable, subid_t *variable_oid,
520     guint variable_oid_length, gushort vb_type, guint val_len)
521 {
522         u_char *buf;
523         size_t buf_len;
524         size_t out_len;
525
526         switch (vb_type) {
527
528         case SNMP_IPADDR:
529                 /* Length has to be 4 bytes. */
530                 buf = check_var_length(val_len, 4);
531                 if (buf != NULL)
532                         return buf;     /* it's not 4 bytes */
533                 break;
534
535         case SNMP_COUNTER64:
536                 /* Length has to be 8 bytes. */
537                 buf = check_var_length(val_len, 8);
538                 if (buf != NULL)
539                         return buf;     /* it's not 8 bytes */
540                 break;
541
542         default:
543                 break;
544         }
545
546         variable->next_variable = NULL;
547         variable->name = variable_oid;
548         variable->name_length = variable_oid_length;
549         switch (vb_type) {
550
551         case SNMP_INTEGER:
552                 variable->type = VALTYPE_INTEGER;
553                 break;
554
555         case SNMP_COUNTER:
556                 variable->type = VALTYPE_COUNTER;
557                 break;
558
559         case SNMP_GAUGE:
560                 variable->type = VALTYPE_GAUGE;
561                 break;
562
563         case SNMP_TIMETICKS:
564                 variable->type = VALTYPE_TIMETICKS;
565                 break;
566
567         case SNMP_OCTETSTR:
568                 variable->type = VALTYPE_STRING;
569                 break;
570
571         case SNMP_IPADDR:
572                 variable->type = VALTYPE_IPADDR;
573                 break;
574
575         case SNMP_OPAQUE:
576                 variable->type = VALTYPE_OPAQUE;
577                 break;
578
579         case SNMP_NSAP:
580                 variable->type = VALTYPE_NSAP;
581                 break;
582
583         case SNMP_OBJECTID:
584                 variable->type = VALTYPE_OBJECTID;
585                 break;
586
587         case SNMP_BITSTR:
588                 variable->type = VALTYPE_BITSTR;
589                 break;
590
591         case SNMP_COUNTER64:
592                 variable->type = VALTYPE_COUNTER64;
593                 break;
594         }
595         variable->val_len = val_len;
596
597         /*
598          * XXX - check for "malloc" and "sprint_realloc_objid()" failure.
599          */
600         buf_len = 256;
601         buf = malloc(buf_len);
602         *buf = '\0';
603         out_len = 0;
604         sprint_realloc_value(&buf, &buf_len, &out_len, 1,  variable_oid,
605             variable_oid_length, variable);
606         return buf;
607 }
608 #endif
609
610 static int
611 snmp_variable_decode(proto_tree *snmp_tree,
612     subid_t *variable_oid
613 #ifndef HAVE_UCD_SNMP
614         _U_
615 #endif
616     ,
617     guint variable_oid_length
618 #ifndef HAVE_UCD_SNMP
619         _U_
620 #endif
621     ,
622     ASN1_SCK *asn1, int offset, guint *lengthp)
623 {
624         int start;
625         guint length;
626         gboolean def;
627         guint vb_length;
628         gushort vb_type;
629         gchar *vb_type_name;
630         int ret;
631         guint cls, con, tag;
632
633         gint32 vb_integer_value;
634         guint32 vb_uinteger_value;
635
636         guint8 *vb_octet_string;
637
638         subid_t *vb_oid;
639         guint vb_oid_length;
640
641         gchar *vb_display_string;
642
643 #ifdef HAVE_UCD_SNMP
644         struct variable_list variable;
645         long value;
646 #endif  /* HAVE_UCD_SNMP */
647         unsigned int i;
648         gchar *buf;
649         int len;
650
651         /* parse the type of the object */
652         start = asn1->offset;
653         ret = asn1_header_decode (asn1, &cls, &con, &tag, &def, &vb_length);
654         if (ret != ASN1_ERR_NOERROR)
655                 return ret;
656         if (!def)
657                 return ASN1_ERR_LENGTH_NOT_DEFINITE;
658
659         /* Convert the class, constructed flag, and tag to a type. */
660         vb_type_name = snmp_tag_cls2syntax(tag, cls, &vb_type);
661         if (vb_type_name == NULL) {
662                 /*
663                  * Unsupported type.
664                  * Dissect the value as an opaque string of octets.
665                  */
666                 vb_type_name = "unsupported type";
667                 vb_type = SNMP_OPAQUE;
668         }
669
670         /* parse the value */
671         switch (vb_type) {
672
673         case SNMP_INTEGER:
674                 ret = asn1_int32_value_decode(asn1, vb_length,
675                     &vb_integer_value);
676                 if (ret != ASN1_ERR_NOERROR)
677                         return ret;
678                 length = asn1->offset - start;
679                 if (snmp_tree) {
680 #ifdef HAVE_UCD_SNMP
681                         value = vb_integer_value;
682                         variable.val.integer = &value;
683                         vb_display_string = format_var(&variable,
684                             variable_oid, variable_oid_length, vb_type,
685                             vb_length);
686                         proto_tree_add_text(snmp_tree, asn1->tvb, offset,
687                             length,
688                             "Value: %s", vb_display_string);
689                         free(vb_display_string);
690 #else /* HAVE_UCD_SNMP */
691                         proto_tree_add_text(snmp_tree, asn1->tvb, offset,
692                             length,
693                             "Value: %s: %d (%#x)", vb_type_name,
694                             vb_integer_value, vb_integer_value);
695 #endif /* HAVE_UCD_SNMP */
696                 }
697                 break;
698
699         case SNMP_COUNTER:
700         case SNMP_GAUGE:
701         case SNMP_TIMETICKS:
702                 ret = asn1_uint32_value_decode(asn1, vb_length,
703                     &vb_uinteger_value);
704                 if (ret != ASN1_ERR_NOERROR)
705                         return ret;
706                 length = asn1->offset - start;
707                 if (snmp_tree) {
708 #ifdef HAVE_UCD_SNMP
709                         value = vb_uinteger_value;
710                         variable.val.integer = &value;
711                         vb_display_string = format_var(&variable,
712                             variable_oid, variable_oid_length, vb_type,
713                             vb_length);
714                         proto_tree_add_text(snmp_tree, asn1->tvb, offset,
715                             length,
716                             "Value: %s", vb_display_string);
717                         free(vb_display_string);
718 #else /* HAVE_UCD_SNMP */
719                         proto_tree_add_text(snmp_tree, asn1->tvb, offset,
720                             length,
721                             "Value: %s: %u (%#x)", vb_type_name,
722                             vb_uinteger_value, vb_uinteger_value);
723 #endif /* HAVE_UCD_SNMP */
724                 }
725                 break;
726
727         case SNMP_OCTETSTR:
728         case SNMP_IPADDR:
729         case SNMP_OPAQUE:
730         case SNMP_NSAP:
731         case SNMP_BITSTR:
732         case SNMP_COUNTER64:
733                 ret = asn1_string_value_decode (asn1, vb_length,
734                     &vb_octet_string);
735                 if (ret != ASN1_ERR_NOERROR)
736                         return ret;
737                 length = asn1->offset - start;
738                 if (snmp_tree) {
739 #ifdef HAVE_UCD_SNMP
740                         variable.val.string = vb_octet_string;
741                         vb_display_string = format_var(&variable,
742                             variable_oid, variable_oid_length, vb_type,
743                             vb_length);
744                         proto_tree_add_text(snmp_tree, asn1->tvb, offset,
745                             length,
746                             "Value: %s", vb_display_string);
747                         free(vb_display_string);
748 #else /* HAVE_UCD_SNMP */
749                         /*
750                          * If some characters are not printable, display
751                          * the string as bytes.
752                          */
753                         for (i = 0; i < vb_length; i++) {
754                                 if (!(isprint(vb_octet_string[i])
755                                     || isspace(vb_octet_string[i])))
756                                         break;
757                         }
758                         if (i < vb_length) {
759                                 /*
760                                  * We stopped, due to a non-printable
761                                  * character, before we got to the end
762                                  * of the string.
763                                  */
764                                 vb_display_string = g_malloc(4*vb_length);
765                                 buf = &vb_display_string[0];
766                                 len = sprintf(buf, "%03u", vb_octet_string[0]);
767                                 buf += len;
768                                 for (i = 1; i < vb_length; i++) {
769                                         len = sprintf(buf, ".%03u",
770                                             vb_octet_string[i]);
771                                         buf += len;
772                                 }
773                                 proto_tree_add_text(snmp_tree, asn1->tvb, offset,
774                                     length,
775                                     "Value: %s: %s", vb_type_name,
776                                     vb_display_string);
777                                 g_free(vb_display_string);
778                         } else {
779                                 proto_tree_add_text(snmp_tree, asn1->tvb, offset,
780                                     length,
781                                     "Value: %s: %.*s", vb_type_name,
782                                     (int)vb_length,
783                                     SAFE_STRING(vb_octet_string));
784                         }
785 #endif /* HAVE_UCD_SNMP */
786                 }
787                 g_free(vb_octet_string);
788                 break;
789
790         case SNMP_NULL:
791                 ret = asn1_null_decode (asn1, vb_length);
792                 if (ret != ASN1_ERR_NOERROR)
793                         return ret;
794                 length = asn1->offset - start;
795                 if (snmp_tree) {
796                         proto_tree_add_text(snmp_tree, asn1->tvb, offset, length,
797                             "Value: %s", vb_type_name);
798                 }
799                 break;
800
801         case SNMP_OBJECTID:
802                 ret = asn1_oid_value_decode (asn1, vb_length, &vb_oid,
803                     &vb_oid_length);
804                 if (ret != ASN1_ERR_NOERROR)
805                         return ret;
806                 length = asn1->offset - start;
807                 if (snmp_tree) {
808 #ifdef HAVE_UCD_SNMP
809                         variable.val.objid = vb_oid;
810                         vb_display_string = format_var(&variable,
811                             variable_oid, variable_oid_length, vb_type,
812                             vb_oid_length * sizeof (subid_t));
813                         proto_tree_add_text(snmp_tree, asn1->tvb, offset,
814                             length,
815                             "Value: %s", vb_display_string);
816                         free(vb_display_string);
817 #else /* HAVE_UCD_SNMP */
818                         vb_display_string = format_oid(vb_oid, vb_oid_length);
819                         proto_tree_add_text(snmp_tree, asn1->tvb, offset,
820                             length,
821                             "Value: %s: %s", vb_type_name, vb_display_string);
822                         g_free(vb_display_string);
823 #endif /* HAVE_UCD_SNMP */
824                 }
825                 g_free(vb_oid);
826                 break;
827
828         case SNMP_NOSUCHOBJECT:
829                 length = asn1->offset - start;
830                 if (snmp_tree) {
831                         proto_tree_add_text(snmp_tree, asn1->tvb, offset, length,
832                             "Value: %s: no such object", vb_type_name);
833                 }
834                 break;
835
836         case SNMP_NOSUCHINSTANCE:
837                 length = asn1->offset - start;
838                 if (snmp_tree) {
839                         proto_tree_add_text(snmp_tree, asn1->tvb, offset, length,
840                             "Value: %s: no such instance", vb_type_name);
841                 }
842                 break;
843
844         case SNMP_ENDOFMIBVIEW:
845                 length = asn1->offset - start;
846                 if (snmp_tree) {
847                         proto_tree_add_text(snmp_tree, asn1->tvb, offset, length,
848                             "Value: %s: end of mib view", vb_type_name);
849                 }
850                 break;
851
852         default:
853                 g_assert_not_reached();
854                 return ASN1_ERR_WRONG_TYPE;
855         }
856         *lengthp = length;
857         return ASN1_ERR_NOERROR;
858 }
859
860 static void
861 dissect_common_pdu(tvbuff_t *tvb, int offset, packet_info *pinfo,
862     proto_tree *tree, ASN1_SCK asn1, guint pdu_type, int start)
863 {
864         gboolean def;
865         guint length;
866         guint sequence_length;
867
868         guint32 request_id;
869
870         guint32 error_status;
871
872         guint32 error_index;
873
874         char *pdu_type_string;
875
876         subid_t *enterprise;
877         guint enterprise_length;
878
879         guint8 *agent_address;
880         guint agent_address_length;
881
882         guint32 trap_type;
883
884         guint32 specific_type;
885
886         guint timestamp;
887         guint timestamp_length;
888
889         gchar *oid_string;
890
891         guint variable_bindings_length;
892
893         int vb_index;
894         guint variable_length;
895         subid_t *variable_oid;
896         guint variable_oid_length;
897
898         int ret;
899         guint cls, con, tag;
900
901         pdu_type_string = val_to_str(pdu_type, pdu_types,
902             "Unknown PDU type %#x");
903         if (check_col(pinfo->cinfo, COL_INFO))
904                 col_add_str(pinfo->cinfo, COL_INFO, pdu_type_string);
905         length = asn1.offset - start;
906         if (tree) {
907                 proto_tree_add_text(tree, tvb, offset, length,
908                     "PDU type: %s", pdu_type_string);
909         }
910         offset += length;
911
912         /* get the fields in the PDU preceeding the variable-bindings sequence */
913         switch (pdu_type) {
914
915         case SNMP_MSG_GET:
916         case SNMP_MSG_GETNEXT:
917         case SNMP_MSG_RESPONSE:
918         case SNMP_MSG_SET:
919         case SNMP_MSG_GETBULK:
920         case SNMP_MSG_INFORM:
921         case SNMP_MSG_TRAP2:
922         case SNMP_MSG_REPORT:
923                 /* request id */
924                 ret = asn1_uint32_decode (&asn1, &request_id, &length);
925                 if (ret != ASN1_ERR_NOERROR) {
926                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
927                             "request ID", ret);
928                         return;
929                 }
930                 if (tree) {
931                         proto_tree_add_text(tree, tvb, offset, length,
932                             "Request Id: %#x", request_id);
933                 }
934                 offset += length;
935                 
936                 /* error status, or getbulk non-repeaters */
937                 ret = asn1_uint32_decode (&asn1, &error_status, &length);
938                 if (ret != ASN1_ERR_NOERROR) {
939                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
940                             (pdu_type == SNMP_MSG_GETBULK) ? "non-repeaters"
941                                                            : "error status",
942                             ret);
943                         return;
944                 }
945                 if (tree) {
946                         if (pdu_type == SNMP_MSG_GETBULK) {
947                                 proto_tree_add_text(tree, tvb, offset,
948                                     length, "Non-repeaters: %u", error_status);
949                         } else {
950                                 proto_tree_add_text(tree, tvb, offset,
951                                     length, "Error Status: %s",
952                                     val_to_str(error_status, error_statuses,
953                                       "Unknown (%d)"));
954                         }
955                 }
956                 offset += length;
957
958                 /* error index, or getbulk max-repetitions */
959                 ret = asn1_uint32_decode (&asn1, &error_index, &length);
960                 if (ret != ASN1_ERR_NOERROR) {
961                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
962                             (pdu_type == SNMP_MSG_GETBULK) ? "max repetitions"
963                                                            : "error index",
964                             ret);
965                         return;
966                 }
967                 if (tree) {
968                         if (pdu_type == SNMP_MSG_GETBULK) {
969                                 proto_tree_add_text(tree, tvb, offset,
970                                     length, "Max repetitions: %u", error_index);
971                         } else {
972                                 proto_tree_add_text(tree, tvb, offset,
973                                     length, "Error Index: %u", error_index);
974                         }
975                 }
976                 offset += length;
977                 break;
978
979         case SNMP_MSG_TRAP:
980                 /* enterprise */
981                 ret = asn1_oid_decode (&asn1, &enterprise, &enterprise_length,
982                     &length);
983                 if (ret != ASN1_ERR_NOERROR) {
984                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
985                             "enterprise OID", ret);
986                         return;
987                 }
988                 if (tree) {
989                         oid_string = format_oid(enterprise, enterprise_length);
990                         proto_tree_add_text(tree, tvb, offset, length,
991                             "Enterprise: %s", oid_string);
992                         g_free(oid_string);
993                 }
994                 g_free(enterprise);
995                 offset += length;
996
997                 /* agent address */
998                 start = asn1.offset;
999                 ret = asn1_header_decode (&asn1, &cls, &con, &tag,
1000                     &def, &agent_address_length);
1001                 if (ret != ASN1_ERR_NOERROR) {
1002                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1003                             "agent address", ret);
1004                         return;
1005                 }
1006                 if (!((cls == ASN1_APL && con == ASN1_PRI && tag == SNMP_IPA) ||
1007                     (cls == ASN1_UNI && con == ASN1_PRI && tag == ASN1_OTS))) {
1008                         /* GXSNMP 0.0.15 says the latter is "needed for
1009                            Banyan" */
1010                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1011                             "agent_address", ASN1_ERR_WRONG_TYPE);
1012                         return;
1013                 }
1014                 if (agent_address_length != 4) {
1015                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1016                             "agent_address", ASN1_ERR_WRONG_LENGTH_FOR_TYPE);
1017                         return;
1018                 }
1019                 ret = asn1_string_value_decode (&asn1,
1020                     agent_address_length, &agent_address);
1021                 if (ret != ASN1_ERR_NOERROR) {
1022                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1023                             "agent address", ret);
1024                         return;
1025                 }
1026                 length = asn1.offset - start;
1027                 if (tree) {
1028                         if (agent_address_length != 4) {
1029                                 proto_tree_add_text(tree, tvb, offset,
1030                                     length,
1031                                     "Agent address: <length is %u, not 4>",
1032                                     agent_address_length);
1033                         } else {
1034                                 proto_tree_add_text(tree, tvb, offset,
1035                                     length,
1036                                     "Agent address: %s",
1037                                     ip_to_str(agent_address));
1038                         }
1039                 }
1040                 g_free(agent_address);
1041                 offset += length;
1042                 
1043                 /* generic trap type */
1044                 ret = asn1_uint32_decode (&asn1, &trap_type, &length);
1045                 if (ret != ASN1_ERR_NOERROR) {
1046                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1047                             "generic trap type", ret);
1048                         return;
1049                 }
1050                 if (tree) {
1051                         proto_tree_add_text(tree, tvb, offset, length,
1052                             "Trap type: %s",
1053                             val_to_str(trap_type, trap_types, "Unknown (%u)"));
1054                 }               
1055                 offset += length;
1056                 
1057                 /* specific trap type */
1058                 ret = asn1_uint32_decode (&asn1, &specific_type, &length);
1059                 if (ret != ASN1_ERR_NOERROR) {
1060                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1061                             "specific trap type", ret);
1062                         return;
1063                 }
1064                 if (tree) {
1065                         proto_tree_add_text(tree, tvb, offset, length,
1066                             "Specific trap type: %u (%#x)",
1067                             specific_type, specific_type);
1068                 }               
1069                 offset += length;
1070                 
1071                 /* timestamp */
1072                 start = asn1.offset;
1073                 ret = asn1_header_decode (&asn1, &cls, &con, &tag,
1074                     &def, &timestamp_length);
1075                 if (ret != ASN1_ERR_NOERROR) {
1076                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1077                             "timestamp", ret);
1078                         return;
1079                 }
1080                 if (!((cls == ASN1_APL && con == ASN1_PRI && tag == SNMP_TIT) ||
1081                     (cls == ASN1_UNI && con == ASN1_PRI && tag == ASN1_INT))) {
1082                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1083                             "timestamp", ASN1_ERR_WRONG_TYPE);
1084                         return;
1085                 }
1086                 ret = asn1_uint32_value_decode(&asn1, timestamp_length,
1087                     &timestamp);
1088                 if (ret != ASN1_ERR_NOERROR) {
1089                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1090                             "timestamp", ret);
1091                         return;
1092                 }
1093                 length = asn1.offset - start;
1094                 if (tree) {
1095                         proto_tree_add_text(tree, tvb, offset, length,
1096                             "Timestamp: %u", timestamp);
1097                 }               
1098                 offset += length;
1099                 break;
1100         }
1101
1102         /* variable bindings */
1103         /* get header for variable-bindings sequence */
1104         ret = asn1_sequence_decode(&asn1, &variable_bindings_length, &length);
1105         if (ret != ASN1_ERR_NOERROR) {
1106                 dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1107                         "variable bindings header", ret);
1108                 return;
1109         }
1110         offset += length;
1111
1112         /* loop on variable bindings */
1113         vb_index = 0;
1114         while (variable_bindings_length > 0) {
1115                 vb_index++;
1116                 sequence_length = 0;
1117
1118                 /* parse type */
1119                 ret = asn1_sequence_decode(&asn1, &variable_length, &length);
1120                 if (ret != ASN1_ERR_NOERROR) {
1121                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1122                                 "variable binding header", ret);
1123                         return;
1124                 }
1125                 sequence_length += length;
1126
1127                 /* parse object identifier */
1128                 ret = asn1_oid_decode (&asn1, &variable_oid,
1129                     &variable_oid_length, &length);
1130                 if (ret != ASN1_ERR_NOERROR) {
1131                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1132                             "variable binding OID", ret);
1133                         return;
1134                 }
1135                 sequence_length += length;
1136
1137                 if (tree) {
1138                         oid_string = format_oid(variable_oid,
1139                             variable_oid_length);
1140                         proto_tree_add_text(tree, tvb, offset, sequence_length,
1141                             "Object identifier %d: %s", vb_index, oid_string);
1142                         g_free(oid_string);
1143                 }
1144                 offset += sequence_length;
1145                 variable_bindings_length -= sequence_length;
1146                                 
1147                 /* Parse the variable's value */
1148                 ret = snmp_variable_decode(tree, variable_oid,
1149                     variable_oid_length, &asn1, offset, &length);
1150                 if (ret != ASN1_ERR_NOERROR) {
1151                         dissect_snmp_parse_error(tvb, offset, pinfo, tree,
1152                             "variable", ret);
1153                         return;
1154                 }
1155                 offset += length;
1156                 variable_bindings_length -= length;
1157         }
1158 }
1159
1160 static const value_string qos_vals[] = {
1161         { 0x0,  "No authentication or privacy" },
1162         { 0x1,  "Authentication, no privacy" },
1163         { 0x2,  "Authentication and privacy" },
1164         { 0x3,  "Authentication and privacy" },
1165         { 0,    NULL },
1166 };
1167
1168 static void
1169 dissect_snmp2u_parameters(proto_tree *tree, tvbuff_t *tvb, int offset, int length,
1170     guchar *parameters, int parameters_length)
1171 {
1172         proto_item *item;
1173         proto_tree *parameters_tree;
1174         proto_tree *qos_tree;
1175         guint8 model;
1176         guint8 qos;
1177         guint8 len;
1178
1179         item = proto_tree_add_text(tree, tvb, offset, length,
1180             "Parameters");
1181         parameters_tree = proto_item_add_subtree(item, ett_parameters);
1182         offset += length - parameters_length;
1183
1184         if (parameters_length < 1)
1185                 return;
1186         model = *parameters;
1187         proto_tree_add_text(parameters_tree, tvb, offset, 1,
1188             "model: %u", model);
1189         offset += 1;
1190         parameters += 1;
1191         parameters_length -= 1;
1192         if (model != 1) {
1193                 /* Unknown model. */
1194                 proto_tree_add_text(parameters_tree, tvb, offset,
1195                     parameters_length, "parameters: %s",
1196                     bytes_to_str(parameters, parameters_length));
1197                 return;
1198         }
1199
1200         if (parameters_length < 1)
1201                 return;
1202         qos = *parameters;
1203         item = proto_tree_add_text(parameters_tree, tvb, offset, 1,
1204             "qoS: 0x%x", qos);
1205         qos_tree = proto_item_add_subtree(item, ett_parameters_qos);
1206         proto_tree_add_text(qos_tree, tvb, offset, 1, "%s",
1207             decode_boolean_bitfield(qos, 0x04,
1208                 8, "Generation of report PDU allowed",
1209                    "Generation of report PDU not allowed"));
1210         proto_tree_add_text(qos_tree, tvb, offset, 1, "%s",
1211             decode_enumerated_bitfield(qos, 0x03,
1212                 8, qos_vals, "%s"));
1213         offset += 1;
1214         parameters += 1;
1215         parameters_length -= 1;
1216
1217         if (parameters_length < 12)
1218                 return;
1219         proto_tree_add_text(parameters_tree, tvb, offset, 12,
1220             "agentID: %s", bytes_to_str(parameters, 12));
1221         offset += 12;
1222         parameters += 12;
1223         parameters_length -= 12;
1224
1225         if (parameters_length < 4)
1226                 return;
1227         proto_tree_add_text(parameters_tree, tvb, offset, 4,
1228             "agentBoots: %u", pntohl(parameters));
1229         offset += 4;
1230         parameters += 4;
1231         parameters_length -= 4;
1232
1233         if (parameters_length < 4)
1234                 return;
1235         proto_tree_add_text(parameters_tree, tvb, offset, 4,
1236             "agentTime: %u", pntohl(parameters));
1237         offset += 4;
1238         parameters += 4;
1239         parameters_length -= 4;
1240
1241         if (parameters_length < 2)
1242                 return;
1243         proto_tree_add_text(parameters_tree, tvb, offset, 2,
1244             "maxSize: %u", pntohs(parameters));
1245         offset += 2;
1246         parameters += 2;
1247         parameters_length -= 2;
1248
1249         if (parameters_length < 1)
1250                 return;
1251         len = *parameters;
1252         proto_tree_add_text(parameters_tree, tvb, offset, 1,
1253             "userLen: %u", len);
1254         offset += 1;
1255         parameters += 1;
1256         parameters_length -= 1;
1257
1258         if (parameters_length < len)
1259                 return;
1260         proto_tree_add_text(parameters_tree, tvb, offset, len,
1261             "userName: %.*s", len, parameters);
1262         offset += len;
1263         parameters += len;
1264         parameters_length -= len;
1265
1266         if (parameters_length < 1)
1267                 return;
1268         len = *parameters;
1269         proto_tree_add_text(parameters_tree, tvb, offset, 1,
1270             "authLen: %u", len);
1271         offset += 1;
1272         parameters += 1;
1273         parameters_length -= 1;
1274
1275         if (parameters_length < len)
1276                 return;
1277         proto_tree_add_text(parameters_tree, tvb, offset, len,
1278             "authDigest: %s", bytes_to_str(parameters, len));
1279         offset += len;
1280         parameters += len;
1281         parameters_length -= len;
1282
1283         if (parameters_length < 1)
1284                 return;
1285         proto_tree_add_text(parameters_tree, tvb, offset, parameters_length,
1286             "contextSelector: %s", bytes_to_str(parameters, parameters_length));
1287 }
1288
1289 void
1290 dissect_snmp_pdu(tvbuff_t *tvb, int offset, packet_info *pinfo,
1291     proto_tree *tree, char *proto_name, int proto, gint ett)
1292 {
1293         ASN1_SCK asn1;
1294         int start;
1295         gboolean def;
1296         gboolean encrypted;
1297         guint length;
1298         guint message_length;
1299         guint global_length;
1300
1301         guint32 version;
1302         guint32 msgid;
1303         guint32 msgmax;
1304         guint32 msgsec;
1305         guint32 engineboots;
1306         guint32 enginetime;
1307
1308         guchar *msgflags;
1309         guchar *community;
1310         guchar *secparm;
1311         guchar *cengineid;
1312         guchar *cname;
1313         guchar *cryptpdu;
1314         guchar *aengineid;
1315         guchar *username;
1316         guchar *authpar;
1317         guchar *privpar;
1318         int msgflags_length;
1319         int community_length;
1320         int secparm_length;
1321         int cengineid_length;
1322         int cname_length;
1323         int cryptpdu_length;
1324         int aengineid_length;
1325         int username_length;
1326         int authpar_length;
1327         int privpar_length;
1328
1329         guint pdu_type;
1330         guint pdu_length;
1331
1332         proto_tree *snmp_tree = NULL;
1333         proto_tree *global_tree = NULL;
1334         proto_tree *flags_tree = NULL;
1335         proto_tree *secur_tree = NULL;
1336         proto_item *item = NULL;
1337         int ret;
1338         guint cls, con, tag;
1339
1340         if (check_col(pinfo->cinfo, COL_PROTOCOL))
1341                 col_add_str(pinfo->cinfo, COL_PROTOCOL, proto_name);
1342
1343         if (tree) {
1344                 item = proto_tree_add_item(tree, proto, tvb, offset, -1, FALSE);
1345                 snmp_tree = proto_item_add_subtree(item, ett);
1346         }
1347
1348         /* NOTE: we have to parse the message piece by piece, since the
1349          * capture length may be less than the message length: a 'global'
1350          * parsing is likely to fail.
1351          */
1352         /* parse the SNMP header */
1353         asn1_open(&asn1, tvb, offset);
1354         ret = asn1_sequence_decode(&asn1, &message_length, &length);
1355         if (ret != ASN1_ERR_NOERROR) {
1356                 dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1357                         "message header", ret);
1358                 return;
1359         }
1360         offset += length;
1361
1362         ret = asn1_uint32_decode (&asn1, &version, &length);
1363         if (ret != ASN1_ERR_NOERROR) {
1364                 dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1365                     "version number", ret);
1366                 return;
1367         }
1368         if (snmp_tree) {
1369                 proto_tree_add_text(snmp_tree, tvb, offset, length,
1370                     "Version: %s",
1371                     val_to_str(version, versions, "Unknown version %#x"));
1372         }
1373         offset += length;
1374
1375
1376         switch (version) {
1377         case SNMP_VERSION_1:
1378         case SNMP_VERSION_2c:
1379                 ret = asn1_octet_string_decode (&asn1, &community, 
1380                     &community_length, &length);
1381                 if (ret != ASN1_ERR_NOERROR) {
1382                         dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree, 
1383                             "community", ret);
1384                         return;
1385                 }
1386                 if (tree) {
1387                         proto_tree_add_text(snmp_tree, tvb, offset, length,
1388                             "Community: %.*s", community_length,
1389                             SAFE_STRING(community));
1390                 }
1391                 g_free(community);
1392                 offset += length;
1393                 break;
1394         case SNMP_VERSION_2u:
1395                 ret = asn1_octet_string_decode (&asn1, &community, 
1396                     &community_length, &length);
1397                 if (tree) {
1398                         dissect_snmp2u_parameters(snmp_tree, tvb, offset, length,
1399                             community, community_length);
1400                 }
1401                 g_free(community);
1402                 offset += length;
1403                 break;
1404         case SNMP_VERSION_3:
1405                 ret = asn1_sequence_decode(&asn1, &global_length, &length);
1406                 if (ret != ASN1_ERR_NOERROR) {
1407                         dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1408                                 "message global header", ret);
1409                         return;
1410                 }
1411                 if (snmp_tree) {
1412                         item = proto_tree_add_text(snmp_tree, tvb, offset,
1413                             global_length + length, "Message Global Header");
1414                         global_tree = proto_item_add_subtree(item, ett_global);
1415                         proto_tree_add_text(global_tree, tvb, offset,
1416                             length,
1417                             "Message Global Header Length: %d", global_length);
1418                 }
1419                 offset += length;
1420                 ret = asn1_uint32_decode (&asn1, &msgid, &length);
1421                 if (ret != ASN1_ERR_NOERROR) {
1422                         dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree, 
1423                             "message id", ret);
1424                         return;
1425                 }
1426                 if (global_tree) {
1427                         proto_tree_add_text(global_tree, tvb, offset,
1428                             length, "Message ID: %d", msgid);
1429                 }
1430                 offset += length;
1431                 ret = asn1_uint32_decode (&asn1, &msgmax, &length);
1432                 if (ret != ASN1_ERR_NOERROR) {
1433                         dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree, 
1434                             "message max size", ret);
1435                         return;
1436                 }
1437                 if (global_tree) {
1438                         proto_tree_add_text(global_tree, tvb, offset,
1439                             length, "Message Max Size: %d", msgmax);
1440                 }
1441                 offset += length;
1442                 ret = asn1_octet_string_decode (&asn1, &msgflags, 
1443                     &msgflags_length, &length);
1444                 if (ret != ASN1_ERR_NOERROR) {
1445                         dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree, 
1446                             "message flags", ret);
1447                         return;
1448                 }
1449                 if (msgflags_length != 1) {
1450                         dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1451                             "message flags wrong length", ret);
1452                         g_free(msgflags);
1453                         return;
1454                 }
1455                 if (global_tree) {
1456                         item = proto_tree_add_uint_format(global_tree,
1457                             hf_snmpv3_flags, tvb, offset, length,
1458                             msgflags[0], "Flags: 0x%02x", msgflags[0]);
1459                         flags_tree = proto_item_add_subtree(item, ett_flags);
1460                         proto_tree_add_boolean(flags_tree, hf_snmpv3_flags_report,
1461                             tvb, offset, length, msgflags[0]);
1462                         proto_tree_add_boolean(flags_tree, hf_snmpv3_flags_crypt,
1463                             tvb, offset, length, msgflags[0]);
1464                         proto_tree_add_boolean(flags_tree, hf_snmpv3_flags_auth,
1465                             tvb, offset, length, msgflags[0]);
1466                 }
1467                 encrypted = msgflags[0] & TH_CRYPT;
1468                 g_free(msgflags);
1469                 offset += length;
1470                 ret = asn1_uint32_decode (&asn1, &msgsec, &length);
1471                 if (ret != ASN1_ERR_NOERROR) {
1472                         dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree, 
1473                             "message security model", ret);
1474                         return;
1475                 }
1476                 if (global_tree) {
1477                         proto_tree_add_text(global_tree, tvb, offset,
1478                             length, "Message Security Model: %s",
1479                             val_to_str(msgsec, sec_models,
1480                             "Unknown model %#x"));
1481                 }
1482                 offset += length;
1483                 switch(msgsec) {
1484                 case SNMP_SEC_USM:
1485                         start = asn1.offset;
1486                         ret = asn1_header_decode (&asn1, &cls, &con, &tag,
1487                             &def, &secparm_length);
1488                         length = asn1.offset - start;
1489                         if (cls != ASN1_UNI && con != ASN1_PRI && 
1490                             tag != ASN1_OTS) {
1491                                 dissect_snmp_parse_error(tvb, offset, pinfo,
1492                                     snmp_tree, "Message Security Parameters",
1493                                     ASN1_ERR_WRONG_TYPE);
1494                                 return;
1495                         }
1496                         if (snmp_tree) {
1497                                 item = proto_tree_add_text(snmp_tree, tvb,
1498                                     offset, secparm_length + length,
1499                                     "Message Security Parameters");
1500                                 secur_tree = proto_item_add_subtree(item,
1501                                     ett_secur);
1502                                 proto_tree_add_text(secur_tree, tvb, offset,
1503                                     length, 
1504                                     "Message Security Parameters Length: %d",
1505                                     secparm_length);
1506                         }
1507                         offset += length;
1508                         ret = asn1_sequence_decode(&asn1, &secparm_length,
1509                             &length);
1510                         if (ret != ASN1_ERR_NOERROR) {
1511                                 dissect_snmp_parse_error(tvb, offset, pinfo,
1512                                     snmp_tree, "USM sequence header", ret);
1513                                 return;
1514                         }
1515                         offset += length;
1516                         ret = asn1_octet_string_decode (&asn1, &aengineid, 
1517                             &aengineid_length, &length);
1518                         if (ret != ASN1_ERR_NOERROR) {
1519                                 dissect_snmp_parse_error(tvb, offset, pinfo,
1520                                     snmp_tree, "authoritative engine id", ret);
1521                                 return;
1522                         }
1523                         if (secur_tree) {
1524                                 proto_tree_add_text(secur_tree, tvb, offset,
1525                                     length, "Authoritative Engine ID: %s",
1526                                     bytes_to_str(aengineid, aengineid_length));
1527                         }
1528                         g_free(aengineid);
1529                         offset += length;
1530                         ret = asn1_uint32_decode (&asn1, &engineboots, &length);
1531                         if (ret != ASN1_ERR_NOERROR) {
1532                                 dissect_snmp_parse_error(tvb, offset, pinfo,
1533                                     snmp_tree, "engine boots", ret);
1534                                 return;
1535                         }
1536                         if (secur_tree) {
1537                                 proto_tree_add_text(secur_tree, tvb,
1538                                     offset, length, "Engine Boots: %d", 
1539                                     engineboots);
1540                         }
1541                         offset += length;
1542                         ret = asn1_uint32_decode (&asn1, &enginetime, &length);
1543                         if (ret != ASN1_ERR_NOERROR) {
1544                                 dissect_snmp_parse_error(tvb, offset, pinfo,
1545                                     snmp_tree,  "engine time", ret);
1546                                 return;
1547                         }
1548                         if (secur_tree) {
1549                                 proto_tree_add_text(secur_tree, tvb,
1550                                     offset, length, "Engine Time: %d", 
1551                                     enginetime);
1552                         }
1553                         offset += length;
1554                         ret = asn1_octet_string_decode (&asn1, &username, 
1555                             &username_length, &length);
1556                         if (ret != ASN1_ERR_NOERROR) {
1557                                 dissect_snmp_parse_error(tvb, offset, pinfo,
1558                                     snmp_tree, "user name", ret);
1559                                 return;
1560                         }
1561                         if (secur_tree) {
1562                                 proto_tree_add_text(secur_tree, tvb, offset,
1563                                     length, "User Name: %.*s", 
1564                                     username_length,
1565                                     SAFE_STRING(username));
1566                         }
1567                         g_free(username);
1568                         offset += length;
1569                         ret = asn1_octet_string_decode (&asn1, &authpar, 
1570                             &authpar_length, &length);
1571                         if (ret != ASN1_ERR_NOERROR) {
1572                                 dissect_snmp_parse_error(tvb, offset, pinfo,
1573                                     snmp_tree, "authentication parameter", ret);
1574                                 return;
1575                         }
1576                         if (secur_tree) {
1577                                 proto_tree_add_text(secur_tree, tvb, offset,
1578                                     length, "Authentication Parameter: %s",
1579                                     bytes_to_str(authpar, authpar_length));
1580                         }
1581                         g_free(authpar);
1582                         offset += length;
1583                         ret = asn1_octet_string_decode (&asn1, &privpar, 
1584                             &privpar_length, &length);
1585                         if (ret != ASN1_ERR_NOERROR) {
1586                                 dissect_snmp_parse_error(tvb, offset, pinfo,
1587                                     snmp_tree, "privacy parameter", ret);
1588                                 return;
1589                         }
1590                         if (secur_tree) {
1591                                 proto_tree_add_text(secur_tree, tvb, offset,
1592                                     length, "Privacy Parameter: %s",
1593                                     bytes_to_str(privpar, privpar_length));
1594                         }
1595                         g_free(privpar);
1596                         offset += length;
1597                         break;
1598                 default:
1599                         ret = asn1_octet_string_decode (&asn1, 
1600                             &secparm, &secparm_length, &length);
1601                         if (ret != ASN1_ERR_NOERROR) {
1602                                 dissect_snmp_parse_error(tvb, offset, pinfo,
1603                                     snmp_tree, "Message Security Parameters",
1604                                     ret);
1605                                 return;
1606                         }
1607                         if (snmp_tree) {
1608                                 proto_tree_add_text(snmp_tree, tvb, offset,
1609                                     length,
1610                                     "Message Security Parameters Data"
1611                                     " (%d bytes)", secparm_length);
1612                         }
1613                         g_free(secparm);
1614                         offset += length;
1615                         break;
1616                 }
1617                 /* PDU starts here */
1618                 if (encrypted) {
1619                         ret = asn1_octet_string_decode (&asn1, &cryptpdu,
1620                             &cryptpdu_length, &length);
1621                         if (ret != ASN1_ERR_NOERROR) {
1622                                 dissect_snmp_parse_error(tvb, offset, pinfo,
1623                                     snmp_tree, "encrypted PDU header", ret);
1624                                 return;
1625                         }
1626                         proto_tree_add_text(snmp_tree, tvb, offset, length,
1627                             "Encrypted PDU (%d bytes)", length);
1628                         g_free(cryptpdu);
1629                         if (check_col(pinfo->cinfo, COL_INFO))
1630                                 col_set_str(pinfo->cinfo, COL_INFO, "Encrypted PDU");
1631                         return;
1632                 }
1633                 ret = asn1_sequence_decode(&asn1, &global_length, &length);
1634                 if (ret != ASN1_ERR_NOERROR) {
1635                         dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1636                                 "PDU header", ret);
1637                         return;
1638                 }
1639                 offset += length;
1640                 ret = asn1_octet_string_decode (&asn1, &cengineid, 
1641                     &cengineid_length, &length);
1642                 if (ret != ASN1_ERR_NOERROR) {
1643                         dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree, 
1644                             "context engine id", ret);
1645                         return;
1646                 }
1647                 if (snmp_tree) {
1648                         proto_tree_add_text(snmp_tree, tvb, offset, length,
1649                             "Context Engine ID: %s",
1650                             bytes_to_str(cengineid, cengineid_length));
1651                 }
1652                 g_free(cengineid);
1653                 offset += length;
1654                 ret = asn1_octet_string_decode (&asn1, &cname, 
1655                     &cname_length, &length);
1656                 if (ret != ASN1_ERR_NOERROR) {
1657                         dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree, 
1658                             "context name", ret);
1659                         return;
1660                 }
1661                 if (snmp_tree) {
1662                         proto_tree_add_text(snmp_tree, tvb, offset, length,
1663                             "Context Name: %.*s", cname_length,
1664                             SAFE_STRING(cname));
1665                 }
1666                 g_free(cname);
1667                 offset += length;
1668                 break;
1669         default:
1670                 dissect_snmp_error(tvb, offset, pinfo, snmp_tree,
1671                     "PDU for unknown version of SNMP");
1672                 return;
1673         }
1674
1675         start = asn1.offset;
1676         ret = asn1_header_decode (&asn1, &cls, &con, &pdu_type, &def,
1677             &pdu_length);
1678         if (ret != ASN1_ERR_NOERROR) {
1679                 dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1680                     "PDU type", ret);
1681                 return;
1682         }
1683         if (cls != ASN1_CTX || con != ASN1_CON) {
1684                 dissect_snmp_parse_error(tvb, offset, pinfo, snmp_tree,
1685                     "PDU type", ASN1_ERR_WRONG_TYPE);
1686                 return;
1687         }
1688         dissect_common_pdu(tvb, offset, pinfo, snmp_tree, asn1, pdu_type, start);
1689 }
1690
1691 static void
1692 dissect_smux_pdu(tvbuff_t *tvb, int offset, packet_info *pinfo,
1693     proto_tree *tree, int proto, gint ett)
1694 {
1695         ASN1_SCK asn1;
1696         int start;
1697         gboolean def;
1698         guint length;
1699
1700         guint pdu_type;
1701         char *pdu_type_string;
1702         guint pdu_length;
1703
1704         guint32 version;
1705         guint32 cause;
1706         guint32 priority;
1707         guint32 operation;
1708         guint32 commit;
1709
1710         guchar *password;
1711         int password_length;
1712
1713         guchar *application;
1714         int application_length;
1715
1716         subid_t *regid;
1717         guint regid_length;
1718
1719         gchar *oid_string;
1720
1721         proto_tree *smux_tree = NULL;
1722         proto_item *item = NULL;
1723         int ret;
1724         guint cls, con;
1725
1726         if (check_col(pinfo->cinfo, COL_PROTOCOL))
1727                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "SMUX");
1728
1729         if (tree) {
1730                 item = proto_tree_add_item(tree, proto, tvb, offset, -1, FALSE);
1731                 smux_tree = proto_item_add_subtree(item, ett);
1732         }
1733
1734         /* NOTE: we have to parse the message piece by piece, since the
1735          * capture length may be less than the message length: a 'global'
1736          * parsing is likely to fail.
1737          */
1738         /* parse the SNMP header */
1739         asn1_open(&asn1, tvb, offset);
1740         start = asn1.offset;
1741         ret = asn1_header_decode (&asn1, &cls, &con, &pdu_type, &def,
1742             &pdu_length);
1743         if (ret != ASN1_ERR_NOERROR) {
1744                 dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1745                     "PDU type", ret);
1746                 return;
1747         }
1748
1749         /* Dissect SMUX here */
1750         if (cls == ASN1_APL && con == ASN1_CON && pdu_type == SMUX_MSG_OPEN) {
1751                 pdu_type_string = val_to_str(pdu_type, smux_types,
1752                     "Unknown PDU type %#x");
1753                 if (check_col(pinfo->cinfo, COL_INFO))
1754                         col_add_str(pinfo->cinfo, COL_INFO, pdu_type_string);
1755                 length = asn1.offset - start;
1756                 if (tree) {
1757                         proto_tree_add_text(smux_tree, tvb, offset, length,
1758                             "PDU type: %s", pdu_type_string);
1759                 }
1760                 offset += length;
1761                 ret = asn1_uint32_decode (&asn1, &version, &length);
1762                 if (ret != ASN1_ERR_NOERROR) {
1763                         dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1764                             "version", ret);
1765                         return;
1766                 }
1767                 if (tree) {
1768                         proto_tree_add_text(smux_tree, tvb, offset, length,
1769                             "Version: %d", version);
1770                 }
1771                 offset += length;
1772
1773                 ret = asn1_oid_decode (&asn1, &regid, &regid_length, &length);
1774                 if (ret != ASN1_ERR_NOERROR) {
1775                         dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1776                             "registration OID", ret);
1777                         return;
1778                 }
1779                 if (tree) {
1780                         oid_string = format_oid(regid, regid_length);
1781                         proto_tree_add_text(smux_tree, tvb, offset, length,
1782                             "Registration: %s", oid_string);
1783                         g_free(oid_string);
1784                 }
1785                 g_free(regid);
1786                 offset += length;
1787
1788                 ret = asn1_octet_string_decode (&asn1, &application, 
1789                     &application_length, &length);
1790                 if (ret != ASN1_ERR_NOERROR) {
1791                         dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree, 
1792                             "application", ret);
1793                         return;
1794                 }
1795                 if (tree) {
1796                         proto_tree_add_text(smux_tree, tvb, offset, length,
1797                             "Application: %.*s", application_length,
1798                              SAFE_STRING(application));
1799                 }
1800                 g_free(application);
1801                 offset += length;
1802
1803                 ret = asn1_octet_string_decode (&asn1, &password, 
1804                     &password_length, &length);
1805                 if (ret != ASN1_ERR_NOERROR) {
1806                         dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree, 
1807                             "password", ret);
1808                         return;
1809                 }
1810                 if (tree) {
1811                         proto_tree_add_text(smux_tree, tvb, offset, length,
1812                             "Password: %.*s", password_length,
1813                             SAFE_STRING(password));
1814                 }
1815                 g_free(password);
1816                 offset += length;
1817                 return;
1818         }
1819         if (cls == ASN1_APL && con == ASN1_PRI && pdu_type == SMUX_MSG_CLOSE) {
1820                 pdu_type_string = val_to_str(pdu_type, smux_types,
1821                     "Unknown PDU type %#x");
1822                 if (check_col(pinfo->cinfo, COL_INFO))
1823                         col_add_str(pinfo->cinfo, COL_INFO, pdu_type_string);
1824                 length = asn1.offset - start;
1825                 if (tree) {
1826                         proto_tree_add_text(smux_tree, tvb, offset, length,
1827                             "PDU type: %s", pdu_type_string);
1828                 }
1829                 offset += length;
1830                 ret = asn1_uint32_value_decode (&asn1, pdu_length, &cause);
1831                 if (ret != ASN1_ERR_NOERROR) {
1832                         dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1833                             "cause", ret);
1834                         return;
1835                 }
1836                 if (tree) {
1837                         proto_tree_add_text(smux_tree, tvb, offset,
1838                             pdu_length, "Cause: %s",
1839                             val_to_str(cause, smux_close, 
1840                                 "Unknown cause %#x"));
1841                 }
1842                 offset += pdu_length;
1843                 return;
1844         }
1845         if (cls == ASN1_APL && con == ASN1_CON && pdu_type == SMUX_MSG_RREQ) {
1846                 pdu_type_string = val_to_str(pdu_type, smux_types,
1847                     "Unknown PDU type %#x");
1848                 if (check_col(pinfo->cinfo, COL_INFO))
1849                         col_add_str(pinfo->cinfo, COL_INFO, pdu_type_string);
1850                 length = asn1.offset - start;
1851                 if (tree) {
1852                         proto_tree_add_text(smux_tree, tvb, offset, length,
1853                             "PDU type: %s", pdu_type_string);
1854                 }
1855                 offset += length;
1856                 ret = asn1_oid_decode (&asn1, &regid, &regid_length, &length);
1857                 if (ret != ASN1_ERR_NOERROR) {
1858                         dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1859                             "registration subtree", ret);
1860                         return;
1861                 }
1862                 if (tree) {
1863                         oid_string = format_oid(regid, regid_length);
1864                         proto_tree_add_text(smux_tree, tvb, offset, length,
1865                             "Registration: %s", oid_string);
1866                         g_free(oid_string);
1867                 }
1868                 g_free(regid);
1869                 offset += length;
1870
1871                 ret = asn1_uint32_decode (&asn1, &priority, &length);
1872                 if (ret != ASN1_ERR_NOERROR) {
1873                         dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1874                             "priority", ret);
1875                         return;
1876                 }
1877                 if (tree) {
1878                         proto_tree_add_text(smux_tree, tvb, offset, length,
1879                             "Priority: %d", priority);
1880                 }
1881                 offset += length;
1882
1883                 ret = asn1_uint32_decode (&asn1, &operation, &length);
1884                 if (ret != ASN1_ERR_NOERROR) {
1885                         dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1886                             "operation", ret);
1887                         return;
1888                 }
1889                 if (tree) {
1890                         proto_tree_add_text(smux_tree, tvb, offset, length,
1891                             "Operation: %s", 
1892                             val_to_str(operation, smux_rreq, 
1893                                 "Unknown operation %#x"));
1894                 }
1895                 offset += length;
1896                 return;
1897         }
1898         if (cls == ASN1_APL && con == ASN1_PRI && pdu_type == SMUX_MSG_RRSP) {
1899                 pdu_type_string = val_to_str(pdu_type, smux_types,
1900                     "Unknown PDU type %#x");
1901                 if (check_col(pinfo->cinfo, COL_INFO))
1902                         col_add_str(pinfo->cinfo, COL_INFO, pdu_type_string);
1903                 length = asn1.offset - start;
1904                 if (tree) {
1905                         proto_tree_add_text(smux_tree, tvb, offset, length,
1906                             "PDU type: %s", pdu_type_string);
1907                 }
1908                 offset += length;
1909                 ret = asn1_uint32_value_decode (&asn1, pdu_length, &priority);
1910                 if (ret != ASN1_ERR_NOERROR) {
1911                         dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1912                             "priority", ret);
1913                         return;
1914                 }
1915                 if (tree) {
1916                         proto_tree_add_text(smux_tree, tvb, offset,
1917                             pdu_length, "%s",
1918                             val_to_str(priority, smux_prio, 
1919                                 "Priority: %#x"));
1920                 }
1921                 offset += pdu_length;
1922                 return;
1923         }
1924         if (cls == ASN1_APL && con == ASN1_PRI && pdu_type == SMUX_MSG_SOUT) {
1925                 pdu_type_string = val_to_str(pdu_type, smux_types,
1926                     "Unknown PDU type %#x");
1927                 if (check_col(pinfo->cinfo, COL_INFO))
1928                         col_add_str(pinfo->cinfo, COL_INFO, pdu_type_string);
1929                 length = asn1.offset - start;
1930                 if (tree) {
1931                         proto_tree_add_text(smux_tree, tvb, offset, length,
1932                             "PDU type: %s", pdu_type_string);
1933                 }
1934                 offset += length;
1935                 ret = asn1_uint32_value_decode (&asn1, pdu_length, &commit);
1936                 if (ret != ASN1_ERR_NOERROR) {
1937                         dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1938                             "commit", ret);
1939                         return;
1940                 }
1941                 if (tree) {
1942                         proto_tree_add_text(smux_tree, tvb, offset,
1943                             pdu_length, "%s",
1944                             val_to_str(commit, smux_sout, 
1945                                 "Unknown SOUT Value: %#x"));
1946                 }
1947                 offset += pdu_length;
1948                 return;
1949         }
1950         if (cls != ASN1_CTX || con != ASN1_CON) {
1951                 dissect_snmp_parse_error(tvb, offset, pinfo, smux_tree,
1952                     "PDU type", ASN1_ERR_WRONG_TYPE);
1953                 return;
1954         }
1955         dissect_common_pdu(tvb, offset, pinfo, smux_tree, asn1, pdu_type, start);
1956 }
1957
1958 static void
1959 dissect_snmp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) 
1960 {
1961         conversation_t  *conversation;
1962
1963         /*
1964          * The first SNMP packet goes to the SNMP port; the second one
1965          * may come from some *other* port, but goes back to the same
1966          * IP address and port as the ones from which the first packet
1967          * came; all subsequent packets presumably go between those two
1968          * IP addresses and ports.
1969          *
1970          * If this packet went to the SNMP port, we check to see if
1971          * there's already a conversation with one address/port pair
1972          * matching the source IP address and port of this packet,
1973          * the other address matching the destination IP address of this
1974          * packet, and any destination port.
1975          *
1976          * If not, we create one, with its address 1/port 1 pair being
1977          * the source address/port of this packet, its address 2 being
1978          * the destination address of this packet, and its port 2 being
1979          * wildcarded, and give it the SNMP dissector as a dissector.
1980          */
1981         if (pinfo->destport == UDP_PORT_SNMP) {
1982           conversation = find_conversation(&pinfo->src, &pinfo->dst, PT_UDP,
1983                                            pinfo->srcport, 0, NO_PORT_B);
1984           if (conversation == NULL) {
1985             conversation = conversation_new(&pinfo->src, &pinfo->dst, PT_UDP,
1986                                             pinfo->srcport, 0, NO_PORT2);
1987             conversation_set_dissector(conversation, snmp_handle);
1988           }
1989         }
1990
1991         dissect_snmp_pdu(tvb, 0, pinfo, tree, "SNMP", proto_snmp, ett_snmp);
1992 }
1993
1994 static void
1995 dissect_smux(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) 
1996 {
1997         dissect_smux_pdu(tvb, 0, pinfo, tree, proto_smux, ett_smux);
1998 }
1999
2000 void
2001 proto_register_snmp(void)
2002 {
2003         static hf_register_info hf[] = {
2004                 { &hf_snmpv3_flags,
2005                 { "SNMPv3 Flags", "snmpv3.flags", FT_UINT8, BASE_HEX, NULL,
2006                     0x0, "", HFILL }},
2007                 { &hf_snmpv3_flags_auth,
2008                 { "Authenticated", "snmpv3.flags.auth", FT_BOOLEAN, 8,
2009                     TFS(&flags_set_truth), TH_AUTH, "", HFILL }},
2010                 { &hf_snmpv3_flags_crypt,
2011                 { "Encrypted", "snmpv3.flags.crypt", FT_BOOLEAN, 8,
2012                     TFS(&flags_set_truth), TH_CRYPT, "", HFILL }},
2013                 { &hf_snmpv3_flags_report,
2014                 { "Reportable", "snmpv3.flags.report", FT_BOOLEAN, 8,
2015                     TFS(&flags_set_truth), TH_REPORT, "", HFILL }},
2016         };
2017         static gint *ett[] = {
2018                 &ett_snmp,
2019                 &ett_smux,
2020                 &ett_parameters,
2021                 &ett_parameters_qos,
2022                 &ett_global,
2023                 &ett_flags,
2024                 &ett_secur,
2025         };
2026
2027 #ifdef HAVE_UCD_SNMP
2028         /*
2029          * Suppress warnings about unknown tokens - we aren't initializing
2030          * UCD SNMP in its entirety, we're just initializing the
2031          * MIB-handling part because that's all we're using, which
2032          * means that entries in the configuration file for other
2033          * pars of the library will not be handled, and we don't want
2034          * the config file reading code to whine about that.
2035          */
2036         ds_set_boolean(DS_LIBRARY_ID, DS_LIB_NO_TOKEN_WARNINGS, TRUE);
2037         ds_set_int(DS_LIBRARY_ID, DS_LIB_PRINT_SUFFIX_ONLY, 2);
2038         register_mib_handlers();
2039         read_premib_configs();
2040         init_mib();
2041         read_configs();
2042 #endif /* HAVE_UCD_SNMP */
2043         proto_snmp = proto_register_protocol("Simple Network Management Protocol",
2044             "SNMP", "snmp");
2045         proto_smux = proto_register_protocol("SNMP Multiplex Protocol",
2046             "SMUX", "smux");
2047         proto_register_field_array(proto_snmp, hf, array_length(hf));
2048         proto_register_subtree_array(ett, array_length(ett));
2049         snmp_handle = create_dissector_handle(dissect_snmp, proto_snmp);
2050 }
2051
2052 void
2053 proto_reg_handoff_snmp(void)
2054 {
2055         dissector_handle_t smux_handle;
2056
2057         dissector_add("udp.port", UDP_PORT_SNMP, snmp_handle);
2058         dissector_add("udp.port", UDP_PORT_SNMP_TRAP, snmp_handle);
2059         smux_handle = create_dissector_handle(dissect_smux, proto_smux);
2060         dissector_add("tcp.port", TCP_PORT_SMUX, smux_handle);
2061         dissector_add("ethertype", ETHERTYPE_SNMP, snmp_handle);
2062         dissector_add("ipx.socket", IPX_SOCKET_SNMP_AGENT, snmp_handle);
2063         dissector_add("ipx.socket", IPX_SOCKET_SNMP_SINK, snmp_handle);
2064         data_handle = find_dissector("data");
2065 }